duktape.c 3.0 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105561065610756108561095611056111561125611356114561155611656117561185611956120561215612256123561245612556126561275612856129561305613156132561335613456135561365613756138561395614056141561425614356144561455614656147561485614956150561515615256153561545615556156561575615856159561605616156162561635616456165561665616756168561695617056171561725617356174561755617656177561785617956180561815618256183561845618556186561875618856189561905619156192561935619456195561965619756198561995620056201562025620356204562055620656207562085620956210562115621256213562145621556216562175621856219562205622156222562235622456225562265622756228562295623056231562325623356234562355623656237562385623956240562415624256243562445624556246562475624856249562505625156252562535625456255562565625756258562595626056261562625626356264562655626656267562685626956270562715627256273562745627556276562775627856279562805628156282562835628456285562865628756288562895629056291562925629356294562955629656297562985629956300563015630256303563045630556306563075630856309563105631156312563135631456315563165631756318563195632056321563225632356324563255632656327563285632956330563315633256333563345633556336563375633856339563405634156342563435634456345563465634756348563495635056351563525635356354563555635656357563585635956360563615636256363563645636556366563675636856369563705637156372563735637456375563765637756378563795638056381563825638356384563855638656387563885638956390563915639256393563945639556396563975639856399564005640156402564035640456405564065640756408564095641056411564125641356414564155641656417564185641956420564215642256423564245642556426564275642856429564305643156432564335643456435564365643756438564395644056441564425644356444564455644656447564485644956450564515645256453564545645556456564575645856459564605646156462564635646456465564665646756468564695647056471564725647356474564755647656477564785647956480564815648256483564845648556486564875648856489564905649156492564935649456495564965649756498564995650056501565025650356504565055650656507565085650956510565115651256513565145651556516565175651856519565205652156522565235652456525565265652756528565295653056531565325653356534565355653656537565385653956540565415654256543565445654556546565475654856549565505655156552565535655456555565565655756558565595656056561565625656356564565655656656567565685656956570565715657256573565745657556576565775657856579565805658156582565835658456585565865658756588565895659056591565925659356594565955659656597565985659956600566015660256603566045660556606566075660856609566105661156612566135661456615566165661756618566195662056621566225662356624566255662656627566285662956630566315663256633566345663556636566375663856639566405664156642566435664456645566465664756648566495665056651566525665356654566555665656657566585665956660566615666256663566645666556666566675666856669566705667156672566735667456675566765667756678566795668056681566825668356684566855668656687566885668956690566915669256693566945669556696566975669856699567005670156702567035670456705567065670756708567095671056711567125671356714567155671656717567185671956720567215672256723567245672556726567275672856729567305673156732567335673456735567365673756738567395674056741567425674356744567455674656747567485674956750567515675256753567545675556756567575675856759567605676156762567635676456765567665676756768567695677056771567725677356774567755677656777567785677956780567815678256783567845678556786567875678856789567905679156792567935679456795567965679756798567995680056801568025680356804568055680656807568085680956810568115681256813568145681556816568175681856819568205682156822568235682456825568265682756828568295683056831568325683356834568355683656837568385683956840568415684256843568445684556846568475684856849568505685156852568535685456855568565685756858568595686056861568625686356864568655686656867568685686956870568715687256873568745687556876568775687856879568805688156882568835688456885568865688756888568895689056891568925689356894568955689656897568985689956900569015690256903569045690556906569075690856909569105691156912569135691456915569165691756918569195692056921569225692356924569255692656927569285692956930569315693256933569345693556936569375693856939569405694156942569435694456945569465694756948569495695056951569525695356954569555695656957569585695956960569615696256963569645696556966569675696856969569705697156972569735697456975569765697756978569795698056981569825698356984569855698656987569885698956990569915699256993569945699556996569975699856999570005700157002570035700457005570065700757008570095701057011570125701357014570155701657017570185701957020570215702257023570245702557026570275702857029570305703157032570335703457035570365703757038570395704057041570425704357044570455704657047570485704957050570515705257053570545705557056570575705857059570605706157062570635706457065570665706757068570695707057071570725707357074570755707657077570785707957080570815708257083570845708557086570875708857089570905709157092570935709457095570965709757098570995710057101571025710357104571055710657107571085710957110571115711257113571145711557116571175711857119571205712157122571235712457125571265712757128571295713057131571325713357134571355713657137571385713957140571415714257143571445714557146571475714857149571505715157152571535715457155571565715757158571595716057161571625716357164571655716657167571685716957170571715717257173571745717557176571775717857179571805718157182571835718457185571865718757188571895719057191571925719357194571955719657197571985719957200572015720257203572045720557206572075720857209572105721157212572135721457215572165721757218572195722057221572225722357224572255722657227572285722957230572315723257233572345723557236572375723857239572405724157242572435724457245572465724757248572495725057251572525725357254572555725657257572585725957260572615726257263572645726557266572675726857269572705727157272572735727457275572765727757278572795728057281572825728357284572855728657287572885728957290572915729257293572945729557296572975729857299573005730157302573035730457305573065730757308573095731057311573125731357314573155731657317573185731957320573215732257323573245732557326573275732857329573305733157332573335733457335573365733757338573395734057341573425734357344573455734657347573485734957350573515735257353573545735557356573575735857359573605736157362573635736457365573665736757368573695737057371573725737357374573755737657377573785737957380573815738257383573845738557386573875738857389573905739157392573935739457395573965739757398573995740057401574025740357404574055740657407574085740957410574115741257413574145741557416574175741857419574205742157422574235742457425574265742757428574295743057431574325743357434574355743657437574385743957440574415744257443574445744557446574475744857449574505745157452574535745457455574565745757458574595746057461574625746357464574655746657467574685746957470574715747257473574745747557476574775747857479574805748157482574835748457485574865748757488574895749057491574925749357494574955749657497574985749957500575015750257503575045750557506575075750857509575105751157512575135751457515575165751757518575195752057521575225752357524575255752657527575285752957530575315753257533575345753557536575375753857539575405754157542575435754457545575465754757548575495755057551575525755357554575555755657557575585755957560575615756257563575645756557566575675756857569575705757157572575735757457575575765757757578575795758057581575825758357584575855758657587575885758957590575915759257593575945759557596575975759857599576005760157602576035760457605576065760757608576095761057611576125761357614576155761657617576185761957620576215762257623576245762557626576275762857629576305763157632576335763457635576365763757638576395764057641576425764357644576455764657647576485764957650576515765257653576545765557656576575765857659576605766157662576635766457665576665766757668576695767057671576725767357674576755767657677576785767957680576815768257683576845768557686576875768857689576905769157692576935769457695576965769757698576995770057701577025770357704577055770657707577085770957710577115771257713577145771557716577175771857719577205772157722577235772457725577265772757728577295773057731577325773357734577355773657737577385773957740577415774257743577445774557746577475774857749577505775157752577535775457755577565775757758577595776057761577625776357764577655776657767577685776957770577715777257773577745777557776577775777857779577805778157782577835778457785577865778757788577895779057791577925779357794577955779657797577985779957800578015780257803578045780557806578075780857809578105781157812578135781457815578165781757818578195782057821578225782357824578255782657827578285782957830578315783257833578345783557836578375783857839578405784157842578435784457845578465784757848578495785057851578525785357854578555785657857578585785957860578615786257863578645786557866578675786857869578705787157872578735787457875578765787757878578795788057881578825788357884578855788657887578885788957890578915789257893578945789557896578975789857899579005790157902579035790457905579065790757908579095791057911579125791357914579155791657917579185791957920579215792257923579245792557926579275792857929579305793157932579335793457935579365793757938579395794057941579425794357944579455794657947579485794957950579515795257953579545795557956579575795857959579605796157962579635796457965579665796757968579695797057971579725797357974579755797657977579785797957980579815798257983579845798557986579875798857989579905799157992579935799457995579965799757998579995800058001580025800358004580055800658007580085800958010580115801258013580145801558016580175801858019580205802158022580235802458025580265802758028580295803058031580325803358034580355803658037580385803958040580415804258043580445804558046580475804858049580505805158052580535805458055580565805758058580595806058061580625806358064580655806658067580685806958070580715807258073580745807558076580775807858079580805808158082580835808458085580865808758088580895809058091580925809358094580955809658097580985809958100581015810258103581045810558106581075810858109581105811158112581135811458115581165811758118581195812058121581225812358124581255812658127581285812958130581315813258133581345813558136581375813858139581405814158142581435814458145581465814758148581495815058151581525815358154581555815658157581585815958160581615816258163581645816558166581675816858169581705817158172581735817458175581765817758178581795818058181581825818358184581855818658187581885818958190581915819258193581945819558196581975819858199582005820158202582035820458205582065820758208582095821058211582125821358214582155821658217582185821958220582215822258223582245822558226582275822858229582305823158232582335823458235582365823758238582395824058241582425824358244582455824658247582485824958250582515825258253582545825558256582575825858259582605826158262582635826458265582665826758268582695827058271582725827358274582755827658277582785827958280582815828258283582845828558286582875828858289582905829158292582935829458295582965829758298582995830058301583025830358304583055830658307583085830958310583115831258313583145831558316583175831858319583205832158322583235832458325583265832758328583295833058331583325833358334583355833658337583385833958340583415834258343583445834558346583475834858349583505835158352583535835458355583565835758358583595836058361583625836358364583655836658367583685836958370583715837258373583745837558376583775837858379583805838158382583835838458385583865838758388583895839058391583925839358394583955839658397583985839958400584015840258403584045840558406584075840858409584105841158412584135841458415584165841758418584195842058421584225842358424584255842658427584285842958430584315843258433584345843558436584375843858439584405844158442584435844458445584465844758448584495845058451584525845358454584555845658457584585845958460584615846258463584645846558466584675846858469584705847158472584735847458475584765847758478584795848058481584825848358484584855848658487584885848958490584915849258493584945849558496584975849858499585005850158502585035850458505585065850758508585095851058511585125851358514585155851658517585185851958520585215852258523585245852558526585275852858529585305853158532585335853458535585365853758538585395854058541585425854358544585455854658547585485854958550585515855258553585545855558556585575855858559585605856158562585635856458565585665856758568585695857058571585725857358574585755857658577585785857958580585815858258583585845858558586585875858858589585905859158592585935859458595585965859758598585995860058601586025860358604586055860658607586085860958610586115861258613586145861558616586175861858619586205862158622586235862458625586265862758628586295863058631586325863358634586355863658637586385863958640586415864258643586445864558646586475864858649586505865158652586535865458655586565865758658586595866058661586625866358664586655866658667586685866958670586715867258673586745867558676586775867858679586805868158682586835868458685586865868758688586895869058691586925869358694586955869658697586985869958700587015870258703587045870558706587075870858709587105871158712587135871458715587165871758718587195872058721587225872358724587255872658727587285872958730587315873258733587345873558736587375873858739587405874158742587435874458745587465874758748587495875058751587525875358754587555875658757587585875958760587615876258763587645876558766587675876858769587705877158772587735877458775587765877758778587795878058781587825878358784587855878658787587885878958790587915879258793587945879558796587975879858799588005880158802588035880458805588065880758808588095881058811588125881358814588155881658817588185881958820588215882258823588245882558826588275882858829588305883158832588335883458835588365883758838588395884058841588425884358844588455884658847588485884958850588515885258853588545885558856588575885858859588605886158862588635886458865588665886758868588695887058871588725887358874588755887658877588785887958880588815888258883588845888558886588875888858889588905889158892588935889458895588965889758898588995890058901589025890358904589055890658907589085890958910589115891258913589145891558916589175891858919589205892158922589235892458925589265892758928589295893058931589325893358934589355893658937589385893958940589415894258943589445894558946589475894858949589505895158952589535895458955589565895758958589595896058961589625896358964589655896658967589685896958970589715897258973589745897558976589775897858979589805898158982589835898458985589865898758988589895899058991589925899358994589955899658997589985899959000590015900259003590045900559006590075900859009590105901159012590135901459015590165901759018590195902059021590225902359024590255902659027590285902959030590315903259033590345903559036590375903859039590405904159042590435904459045590465904759048590495905059051590525905359054590555905659057590585905959060590615906259063590645906559066590675906859069590705907159072590735907459075590765907759078590795908059081590825908359084590855908659087590885908959090590915909259093590945909559096590975909859099591005910159102591035910459105591065910759108591095911059111591125911359114591155911659117591185911959120591215912259123591245912559126591275912859129591305913159132591335913459135591365913759138591395914059141591425914359144591455914659147591485914959150591515915259153591545915559156591575915859159591605916159162591635916459165591665916759168591695917059171591725917359174591755917659177591785917959180591815918259183591845918559186591875918859189591905919159192591935919459195591965919759198591995920059201592025920359204592055920659207592085920959210592115921259213592145921559216592175921859219592205922159222592235922459225592265922759228592295923059231592325923359234592355923659237592385923959240592415924259243592445924559246592475924859249592505925159252592535925459255592565925759258592595926059261592625926359264592655926659267592685926959270592715927259273592745927559276592775927859279592805928159282592835928459285592865928759288592895929059291592925929359294592955929659297592985929959300593015930259303593045930559306593075930859309593105931159312593135931459315593165931759318593195932059321593225932359324593255932659327593285932959330593315933259333593345933559336593375933859339593405934159342593435934459345593465934759348593495935059351593525935359354593555935659357593585935959360593615936259363593645936559366593675936859369593705937159372593735937459375593765937759378593795938059381593825938359384593855938659387593885938959390593915939259393593945939559396593975939859399594005940159402594035940459405594065940759408594095941059411594125941359414594155941659417594185941959420594215942259423594245942559426594275942859429594305943159432594335943459435594365943759438594395944059441594425944359444594455944659447594485944959450594515945259453594545945559456594575945859459594605946159462594635946459465594665946759468594695947059471594725947359474594755947659477594785947959480594815948259483594845948559486594875948859489594905949159492594935949459495594965949759498594995950059501595025950359504595055950659507595085950959510595115951259513595145951559516595175951859519595205952159522595235952459525595265952759528595295953059531595325953359534595355953659537595385953959540595415954259543595445954559546595475954859549595505955159552595535955459555595565955759558595595956059561595625956359564595655956659567595685956959570595715957259573595745957559576595775957859579595805958159582595835958459585595865958759588595895959059591595925959359594595955959659597595985959959600596015960259603596045960559606596075960859609596105961159612596135961459615596165961759618596195962059621596225962359624596255962659627596285962959630596315963259633596345963559636596375963859639596405964159642596435964459645596465964759648596495965059651596525965359654596555965659657596585965959660596615966259663596645966559666596675966859669596705967159672596735967459675596765967759678596795968059681596825968359684596855968659687596885968959690596915969259693596945969559696596975969859699597005970159702597035970459705597065970759708597095971059711597125971359714597155971659717597185971959720597215972259723597245972559726597275972859729597305973159732597335973459735597365973759738597395974059741597425974359744597455974659747597485974959750597515975259753597545975559756597575975859759597605976159762597635976459765597665976759768597695977059771597725977359774597755977659777597785977959780597815978259783597845978559786597875978859789597905979159792597935979459795597965979759798597995980059801598025980359804598055980659807598085980959810598115981259813598145981559816598175981859819598205982159822598235982459825598265982759828598295983059831598325983359834598355983659837598385983959840598415984259843598445984559846598475984859849598505985159852598535985459855598565985759858598595986059861598625986359864598655986659867598685986959870598715987259873598745987559876598775987859879598805988159882598835988459885598865988759888598895989059891598925989359894598955989659897598985989959900599015990259903599045990559906599075990859909599105991159912599135991459915599165991759918599195992059921599225992359924599255992659927599285992959930599315993259933599345993559936599375993859939599405994159942599435994459945599465994759948599495995059951599525995359954599555995659957599585995959960599615996259963599645996559966599675996859969599705997159972599735997459975599765997759978599795998059981599825998359984599855998659987599885998959990599915999259993599945999559996599975999859999600006000160002600036000460005600066000760008600096001060011600126001360014600156001660017600186001960020600216002260023600246002560026600276002860029600306003160032600336003460035600366003760038600396004060041600426004360044600456004660047600486004960050600516005260053600546005560056600576005860059600606006160062600636006460065600666006760068600696007060071600726007360074600756007660077600786007960080600816008260083600846008560086600876008860089600906009160092600936009460095600966009760098600996010060101601026010360104601056010660107601086010960110601116011260113601146011560116601176011860119601206012160122601236012460125601266012760128601296013060131601326013360134601356013660137601386013960140601416014260143601446014560146601476014860149601506015160152601536015460155601566015760158601596016060161601626016360164601656016660167601686016960170601716017260173601746017560176601776017860179601806018160182601836018460185601866018760188601896019060191601926019360194601956019660197601986019960200602016020260203602046020560206602076020860209602106021160212602136021460215602166021760218602196022060221602226022360224602256022660227602286022960230602316023260233602346023560236602376023860239602406024160242602436024460245602466024760248602496025060251602526025360254602556025660257602586025960260602616026260263602646026560266602676026860269602706027160272602736027460275602766027760278602796028060281602826028360284602856028660287602886028960290602916029260293602946029560296602976029860299603006030160302603036030460305603066030760308603096031060311603126031360314603156031660317603186031960320603216032260323603246032560326603276032860329603306033160332603336033460335603366033760338603396034060341603426034360344603456034660347603486034960350603516035260353603546035560356603576035860359603606036160362603636036460365603666036760368603696037060371603726037360374603756037660377603786037960380603816038260383603846038560386603876038860389603906039160392603936039460395603966039760398603996040060401604026040360404604056040660407604086040960410604116041260413604146041560416604176041860419604206042160422604236042460425604266042760428604296043060431604326043360434604356043660437604386043960440604416044260443604446044560446604476044860449604506045160452604536045460455604566045760458604596046060461604626046360464604656046660467604686046960470604716047260473604746047560476604776047860479604806048160482604836048460485604866048760488604896049060491604926049360494604956049660497604986049960500605016050260503605046050560506605076050860509605106051160512605136051460515605166051760518605196052060521605226052360524605256052660527605286052960530605316053260533605346053560536605376053860539605406054160542605436054460545605466054760548605496055060551605526055360554605556055660557605586055960560605616056260563605646056560566605676056860569605706057160572605736057460575605766057760578605796058060581605826058360584605856058660587605886058960590605916059260593605946059560596605976059860599606006060160602606036060460605606066060760608606096061060611606126061360614606156061660617606186061960620606216062260623606246062560626606276062860629606306063160632606336063460635606366063760638606396064060641606426064360644606456064660647606486064960650606516065260653606546065560656606576065860659606606066160662606636066460665606666066760668606696067060671606726067360674606756067660677606786067960680606816068260683606846068560686606876068860689606906069160692606936069460695606966069760698606996070060701607026070360704607056070660707607086070960710607116071260713607146071560716607176071860719607206072160722607236072460725607266072760728607296073060731607326073360734607356073660737607386073960740607416074260743607446074560746607476074860749607506075160752607536075460755607566075760758607596076060761607626076360764607656076660767607686076960770607716077260773607746077560776607776077860779607806078160782607836078460785607866078760788607896079060791607926079360794607956079660797607986079960800608016080260803608046080560806608076080860809608106081160812608136081460815608166081760818608196082060821608226082360824608256082660827608286082960830608316083260833608346083560836608376083860839608406084160842608436084460845608466084760848608496085060851608526085360854608556085660857608586085960860608616086260863608646086560866608676086860869608706087160872608736087460875608766087760878608796088060881608826088360884608856088660887608886088960890608916089260893608946089560896608976089860899609006090160902609036090460905609066090760908609096091060911609126091360914609156091660917609186091960920609216092260923609246092560926609276092860929609306093160932609336093460935609366093760938609396094060941609426094360944609456094660947609486094960950609516095260953609546095560956609576095860959609606096160962609636096460965609666096760968609696097060971609726097360974609756097660977609786097960980609816098260983609846098560986609876098860989609906099160992609936099460995609966099760998609996100061001610026100361004610056100661007610086100961010610116101261013610146101561016610176101861019610206102161022610236102461025610266102761028610296103061031610326103361034610356103661037610386103961040610416104261043610446104561046610476104861049610506105161052610536105461055610566105761058610596106061061610626106361064610656106661067610686106961070610716107261073610746107561076610776107861079610806108161082610836108461085610866108761088610896109061091610926109361094610956109661097610986109961100611016110261103611046110561106611076110861109611106111161112611136111461115611166111761118611196112061121611226112361124611256112661127611286112961130611316113261133611346113561136611376113861139611406114161142611436114461145611466114761148611496115061151611526115361154611556115661157611586115961160611616116261163611646116561166611676116861169611706117161172611736117461175611766117761178611796118061181611826118361184611856118661187611886118961190611916119261193611946119561196611976119861199612006120161202612036120461205612066120761208612096121061211612126121361214612156121661217612186121961220612216122261223612246122561226612276122861229612306123161232612336123461235612366123761238612396124061241612426124361244612456124661247612486124961250612516125261253612546125561256612576125861259612606126161262612636126461265612666126761268612696127061271612726127361274612756127661277612786127961280612816128261283612846128561286612876128861289612906129161292612936129461295612966129761298612996130061301613026130361304613056130661307613086130961310613116131261313613146131561316613176131861319613206132161322613236132461325613266132761328613296133061331613326133361334613356133661337613386133961340613416134261343613446134561346613476134861349613506135161352613536135461355613566135761358613596136061361613626136361364613656136661367613686136961370613716137261373613746137561376613776137861379613806138161382613836138461385613866138761388613896139061391613926139361394613956139661397613986139961400614016140261403614046140561406614076140861409614106141161412614136141461415614166141761418614196142061421614226142361424614256142661427614286142961430614316143261433614346143561436614376143861439614406144161442614436144461445614466144761448614496145061451614526145361454614556145661457614586145961460614616146261463614646146561466614676146861469614706147161472614736147461475614766147761478614796148061481614826148361484614856148661487614886148961490614916149261493614946149561496614976149861499615006150161502615036150461505615066150761508615096151061511615126151361514615156151661517615186151961520615216152261523615246152561526615276152861529615306153161532615336153461535615366153761538615396154061541615426154361544615456154661547615486154961550615516155261553615546155561556615576155861559615606156161562615636156461565615666156761568615696157061571615726157361574615756157661577615786157961580615816158261583615846158561586615876158861589615906159161592615936159461595615966159761598615996160061601616026160361604616056160661607616086160961610616116161261613616146161561616616176161861619616206162161622616236162461625616266162761628616296163061631616326163361634616356163661637616386163961640616416164261643616446164561646616476164861649616506165161652616536165461655616566165761658616596166061661616626166361664616656166661667616686166961670616716167261673616746167561676616776167861679616806168161682616836168461685616866168761688616896169061691616926169361694616956169661697616986169961700617016170261703617046170561706617076170861709617106171161712617136171461715617166171761718617196172061721617226172361724617256172661727617286172961730617316173261733617346173561736617376173861739617406174161742617436174461745617466174761748617496175061751617526175361754617556175661757617586175961760617616176261763617646176561766617676176861769617706177161772617736177461775617766177761778617796178061781617826178361784617856178661787617886178961790617916179261793617946179561796617976179861799618006180161802618036180461805618066180761808618096181061811618126181361814618156181661817618186181961820618216182261823618246182561826618276182861829618306183161832618336183461835618366183761838618396184061841618426184361844618456184661847618486184961850618516185261853618546185561856618576185861859618606186161862618636186461865618666186761868618696187061871618726187361874618756187661877618786187961880618816188261883618846188561886618876188861889618906189161892618936189461895618966189761898618996190061901619026190361904619056190661907619086190961910619116191261913619146191561916619176191861919619206192161922619236192461925619266192761928619296193061931619326193361934619356193661937619386193961940619416194261943619446194561946619476194861949619506195161952619536195461955619566195761958619596196061961619626196361964619656196661967619686196961970619716197261973619746197561976619776197861979619806198161982619836198461985619866198761988619896199061991619926199361994619956199661997619986199962000620016200262003620046200562006620076200862009620106201162012620136201462015620166201762018620196202062021620226202362024620256202662027620286202962030620316203262033620346203562036620376203862039620406204162042620436204462045620466204762048620496205062051620526205362054620556205662057620586205962060620616206262063620646206562066620676206862069620706207162072620736207462075620766207762078620796208062081620826208362084620856208662087620886208962090620916209262093620946209562096620976209862099621006210162102621036210462105621066210762108621096211062111621126211362114621156211662117621186211962120621216212262123621246212562126621276212862129621306213162132621336213462135621366213762138621396214062141621426214362144621456214662147621486214962150621516215262153621546215562156621576215862159621606216162162621636216462165621666216762168621696217062171621726217362174621756217662177621786217962180621816218262183621846218562186621876218862189621906219162192621936219462195621966219762198621996220062201622026220362204622056220662207622086220962210622116221262213622146221562216622176221862219622206222162222622236222462225622266222762228622296223062231622326223362234622356223662237622386223962240622416224262243622446224562246622476224862249622506225162252622536225462255622566225762258622596226062261622626226362264622656226662267622686226962270622716227262273622746227562276622776227862279622806228162282622836228462285622866228762288622896229062291622926229362294622956229662297622986229962300623016230262303623046230562306623076230862309623106231162312623136231462315623166231762318623196232062321623226232362324623256232662327623286232962330623316233262333623346233562336623376233862339623406234162342623436234462345623466234762348623496235062351623526235362354623556235662357623586235962360623616236262363623646236562366623676236862369623706237162372623736237462375623766237762378623796238062381623826238362384623856238662387623886238962390623916239262393623946239562396623976239862399624006240162402624036240462405624066240762408624096241062411624126241362414624156241662417624186241962420624216242262423624246242562426624276242862429624306243162432624336243462435624366243762438624396244062441624426244362444624456244662447624486244962450624516245262453624546245562456624576245862459624606246162462624636246462465624666246762468624696247062471624726247362474624756247662477624786247962480624816248262483624846248562486624876248862489624906249162492624936249462495624966249762498624996250062501625026250362504625056250662507625086250962510625116251262513625146251562516625176251862519625206252162522625236252462525625266252762528625296253062531625326253362534625356253662537625386253962540625416254262543625446254562546625476254862549625506255162552625536255462555625566255762558625596256062561625626256362564625656256662567625686256962570625716257262573625746257562576625776257862579625806258162582625836258462585625866258762588625896259062591625926259362594625956259662597625986259962600626016260262603626046260562606626076260862609626106261162612626136261462615626166261762618626196262062621626226262362624626256262662627626286262962630626316263262633626346263562636626376263862639626406264162642626436264462645626466264762648626496265062651626526265362654626556265662657626586265962660626616266262663626646266562666626676266862669626706267162672626736267462675626766267762678626796268062681626826268362684626856268662687626886268962690626916269262693626946269562696626976269862699627006270162702627036270462705627066270762708627096271062711627126271362714627156271662717627186271962720627216272262723627246272562726627276272862729627306273162732627336273462735627366273762738627396274062741627426274362744627456274662747627486274962750627516275262753627546275562756627576275862759627606276162762627636276462765627666276762768627696277062771627726277362774627756277662777627786277962780627816278262783627846278562786627876278862789627906279162792627936279462795627966279762798627996280062801628026280362804628056280662807628086280962810628116281262813628146281562816628176281862819628206282162822628236282462825628266282762828628296283062831628326283362834628356283662837628386283962840628416284262843628446284562846628476284862849628506285162852628536285462855628566285762858628596286062861628626286362864628656286662867628686286962870628716287262873628746287562876628776287862879628806288162882628836288462885628866288762888628896289062891628926289362894628956289662897628986289962900629016290262903629046290562906629076290862909629106291162912629136291462915629166291762918629196292062921629226292362924629256292662927629286292962930629316293262933629346293562936629376293862939629406294162942629436294462945629466294762948629496295062951629526295362954629556295662957629586295962960629616296262963629646296562966629676296862969629706297162972629736297462975629766297762978629796298062981629826298362984629856298662987629886298962990629916299262993629946299562996629976299862999630006300163002630036300463005630066300763008630096301063011630126301363014630156301663017630186301963020630216302263023630246302563026630276302863029630306303163032630336303463035630366303763038630396304063041630426304363044630456304663047630486304963050630516305263053630546305563056630576305863059630606306163062630636306463065630666306763068630696307063071630726307363074630756307663077630786307963080630816308263083630846308563086630876308863089630906309163092630936309463095630966309763098630996310063101631026310363104631056310663107631086310963110631116311263113631146311563116631176311863119631206312163122631236312463125631266312763128631296313063131631326313363134631356313663137631386313963140631416314263143631446314563146631476314863149631506315163152631536315463155631566315763158631596316063161631626316363164631656316663167631686316963170631716317263173631746317563176631776317863179631806318163182631836318463185631866318763188631896319063191631926319363194631956319663197631986319963200632016320263203632046320563206632076320863209632106321163212632136321463215632166321763218632196322063221632226322363224632256322663227632286322963230632316323263233632346323563236632376323863239632406324163242632436324463245632466324763248632496325063251632526325363254632556325663257632586325963260632616326263263632646326563266632676326863269632706327163272632736327463275632766327763278632796328063281632826328363284632856328663287632886328963290632916329263293632946329563296632976329863299633006330163302633036330463305633066330763308633096331063311633126331363314633156331663317633186331963320633216332263323633246332563326633276332863329633306333163332633336333463335633366333763338633396334063341633426334363344633456334663347633486334963350633516335263353633546335563356633576335863359633606336163362633636336463365633666336763368633696337063371633726337363374633756337663377633786337963380633816338263383633846338563386633876338863389633906339163392633936339463395633966339763398633996340063401634026340363404634056340663407634086340963410634116341263413634146341563416634176341863419634206342163422634236342463425634266342763428634296343063431634326343363434634356343663437634386343963440634416344263443634446344563446634476344863449634506345163452634536345463455634566345763458634596346063461634626346363464634656346663467634686346963470634716347263473634746347563476634776347863479634806348163482634836348463485634866348763488634896349063491634926349363494634956349663497634986349963500635016350263503635046350563506635076350863509635106351163512635136351463515635166351763518635196352063521635226352363524635256352663527635286352963530635316353263533635346353563536635376353863539635406354163542635436354463545635466354763548635496355063551635526355363554635556355663557635586355963560635616356263563635646356563566635676356863569635706357163572635736357463575635766357763578635796358063581635826358363584635856358663587635886358963590635916359263593635946359563596635976359863599636006360163602636036360463605636066360763608636096361063611636126361363614636156361663617636186361963620636216362263623636246362563626636276362863629636306363163632636336363463635636366363763638636396364063641636426364363644636456364663647636486364963650636516365263653636546365563656636576365863659636606366163662636636366463665636666366763668636696367063671636726367363674636756367663677636786367963680636816368263683636846368563686636876368863689636906369163692636936369463695636966369763698636996370063701637026370363704637056370663707637086370963710637116371263713637146371563716637176371863719637206372163722637236372463725637266372763728637296373063731637326373363734637356373663737637386373963740637416374263743637446374563746637476374863749637506375163752637536375463755637566375763758637596376063761637626376363764637656376663767637686376963770637716377263773637746377563776637776377863779637806378163782637836378463785637866378763788637896379063791637926379363794637956379663797637986379963800638016380263803638046380563806638076380863809638106381163812638136381463815638166381763818638196382063821638226382363824638256382663827638286382963830638316383263833638346383563836638376383863839638406384163842638436384463845638466384763848638496385063851638526385363854638556385663857638586385963860638616386263863638646386563866638676386863869638706387163872638736387463875638766387763878638796388063881638826388363884638856388663887638886388963890638916389263893638946389563896638976389863899639006390163902639036390463905639066390763908639096391063911639126391363914639156391663917639186391963920639216392263923639246392563926639276392863929639306393163932639336393463935639366393763938639396394063941639426394363944639456394663947639486394963950639516395263953639546395563956639576395863959639606396163962639636396463965639666396763968639696397063971639726397363974639756397663977639786397963980639816398263983639846398563986639876398863989639906399163992639936399463995639966399763998639996400064001640026400364004640056400664007640086400964010640116401264013640146401564016640176401864019640206402164022640236402464025640266402764028640296403064031640326403364034640356403664037640386403964040640416404264043640446404564046640476404864049640506405164052640536405464055640566405764058640596406064061640626406364064640656406664067640686406964070640716407264073640746407564076640776407864079640806408164082640836408464085640866408764088640896409064091640926409364094640956409664097640986409964100641016410264103641046410564106641076410864109641106411164112641136411464115641166411764118641196412064121641226412364124641256412664127641286412964130641316413264133641346413564136641376413864139641406414164142641436414464145641466414764148641496415064151641526415364154641556415664157641586415964160641616416264163641646416564166641676416864169641706417164172641736417464175641766417764178641796418064181641826418364184641856418664187641886418964190641916419264193641946419564196641976419864199642006420164202642036420464205642066420764208642096421064211642126421364214642156421664217642186421964220642216422264223642246422564226642276422864229642306423164232642336423464235642366423764238642396424064241642426424364244642456424664247642486424964250642516425264253642546425564256642576425864259642606426164262642636426464265642666426764268642696427064271642726427364274642756427664277642786427964280642816428264283642846428564286642876428864289642906429164292642936429464295642966429764298642996430064301643026430364304643056430664307643086430964310643116431264313643146431564316643176431864319643206432164322643236432464325643266432764328643296433064331643326433364334643356433664337643386433964340643416434264343643446434564346643476434864349643506435164352643536435464355643566435764358643596436064361643626436364364643656436664367643686436964370643716437264373643746437564376643776437864379643806438164382643836438464385643866438764388643896439064391643926439364394643956439664397643986439964400644016440264403644046440564406644076440864409644106441164412644136441464415644166441764418644196442064421644226442364424644256442664427644286442964430644316443264433644346443564436644376443864439644406444164442644436444464445644466444764448644496445064451644526445364454644556445664457644586445964460644616446264463644646446564466644676446864469644706447164472644736447464475644766447764478644796448064481644826448364484644856448664487644886448964490644916449264493644946449564496644976449864499645006450164502645036450464505645066450764508645096451064511645126451364514645156451664517645186451964520645216452264523645246452564526645276452864529645306453164532645336453464535645366453764538645396454064541645426454364544645456454664547645486454964550645516455264553645546455564556645576455864559645606456164562645636456464565645666456764568645696457064571645726457364574645756457664577645786457964580645816458264583645846458564586645876458864589645906459164592645936459464595645966459764598645996460064601646026460364604646056460664607646086460964610646116461264613646146461564616646176461864619646206462164622646236462464625646266462764628646296463064631646326463364634646356463664637646386463964640646416464264643646446464564646646476464864649646506465164652646536465464655646566465764658646596466064661646626466364664646656466664667646686466964670646716467264673646746467564676646776467864679646806468164682646836468464685646866468764688646896469064691646926469364694646956469664697646986469964700647016470264703647046470564706647076470864709647106471164712647136471464715647166471764718647196472064721647226472364724647256472664727647286472964730647316473264733647346473564736647376473864739647406474164742647436474464745647466474764748647496475064751647526475364754647556475664757647586475964760647616476264763647646476564766647676476864769647706477164772647736477464775647766477764778647796478064781647826478364784647856478664787647886478964790647916479264793647946479564796647976479864799648006480164802648036480464805648066480764808648096481064811648126481364814648156481664817648186481964820648216482264823648246482564826648276482864829648306483164832648336483464835648366483764838648396484064841648426484364844648456484664847648486484964850648516485264853648546485564856648576485864859648606486164862648636486464865648666486764868648696487064871648726487364874648756487664877648786487964880648816488264883648846488564886648876488864889648906489164892648936489464895648966489764898648996490064901649026490364904649056490664907649086490964910649116491264913649146491564916649176491864919649206492164922649236492464925649266492764928649296493064931649326493364934649356493664937649386493964940649416494264943649446494564946649476494864949649506495164952649536495464955649566495764958649596496064961649626496364964649656496664967649686496964970649716497264973649746497564976649776497864979649806498164982649836498464985649866498764988649896499064991649926499364994649956499664997649986499965000650016500265003650046500565006650076500865009650106501165012650136501465015650166501765018650196502065021650226502365024650256502665027650286502965030650316503265033650346503565036650376503865039650406504165042650436504465045650466504765048650496505065051650526505365054650556505665057650586505965060650616506265063650646506565066650676506865069650706507165072650736507465075650766507765078650796508065081650826508365084650856508665087650886508965090650916509265093650946509565096650976509865099651006510165102651036510465105651066510765108651096511065111651126511365114651156511665117651186511965120651216512265123651246512565126651276512865129651306513165132651336513465135651366513765138651396514065141651426514365144651456514665147651486514965150651516515265153651546515565156651576515865159651606516165162651636516465165651666516765168651696517065171651726517365174651756517665177651786517965180651816518265183651846518565186651876518865189651906519165192651936519465195651966519765198651996520065201652026520365204652056520665207652086520965210652116521265213652146521565216652176521865219652206522165222652236522465225652266522765228652296523065231652326523365234652356523665237652386523965240652416524265243652446524565246652476524865249652506525165252652536525465255652566525765258652596526065261652626526365264652656526665267652686526965270652716527265273652746527565276652776527865279652806528165282652836528465285652866528765288652896529065291652926529365294652956529665297652986529965300653016530265303653046530565306653076530865309653106531165312653136531465315653166531765318653196532065321653226532365324653256532665327653286532965330653316533265333653346533565336653376533865339653406534165342653436534465345653466534765348653496535065351653526535365354653556535665357653586535965360653616536265363653646536565366653676536865369653706537165372653736537465375653766537765378653796538065381653826538365384653856538665387653886538965390653916539265393653946539565396653976539865399654006540165402654036540465405654066540765408654096541065411654126541365414654156541665417654186541965420654216542265423654246542565426654276542865429654306543165432654336543465435654366543765438654396544065441654426544365444654456544665447654486544965450654516545265453654546545565456654576545865459654606546165462654636546465465654666546765468654696547065471654726547365474654756547665477654786547965480654816548265483654846548565486654876548865489654906549165492654936549465495654966549765498654996550065501655026550365504655056550665507655086550965510655116551265513655146551565516655176551865519655206552165522655236552465525655266552765528655296553065531655326553365534655356553665537655386553965540655416554265543655446554565546655476554865549655506555165552655536555465555655566555765558655596556065561655626556365564655656556665567655686556965570655716557265573655746557565576655776557865579655806558165582655836558465585655866558765588655896559065591655926559365594655956559665597655986559965600656016560265603656046560565606656076560865609656106561165612656136561465615656166561765618656196562065621656226562365624656256562665627656286562965630656316563265633656346563565636656376563865639656406564165642656436564465645656466564765648656496565065651656526565365654656556565665657656586565965660656616566265663656646566565666656676566865669656706567165672656736567465675656766567765678656796568065681656826568365684656856568665687656886568965690656916569265693656946569565696656976569865699657006570165702657036570465705657066570765708657096571065711657126571365714657156571665717657186571965720657216572265723657246572565726657276572865729657306573165732657336573465735657366573765738657396574065741657426574365744657456574665747657486574965750657516575265753657546575565756657576575865759657606576165762657636576465765657666576765768657696577065771657726577365774657756577665777657786577965780657816578265783657846578565786657876578865789657906579165792657936579465795657966579765798657996580065801658026580365804658056580665807658086580965810658116581265813658146581565816658176581865819658206582165822658236582465825658266582765828658296583065831658326583365834658356583665837658386583965840658416584265843658446584565846658476584865849658506585165852658536585465855658566585765858658596586065861658626586365864658656586665867658686586965870658716587265873658746587565876658776587865879658806588165882658836588465885658866588765888658896589065891658926589365894658956589665897658986589965900659016590265903659046590565906659076590865909659106591165912659136591465915659166591765918659196592065921659226592365924659256592665927659286592965930659316593265933659346593565936659376593865939659406594165942659436594465945659466594765948659496595065951659526595365954659556595665957659586595965960659616596265963659646596565966659676596865969659706597165972659736597465975659766597765978659796598065981659826598365984659856598665987659886598965990659916599265993659946599565996659976599865999660006600166002660036600466005660066600766008660096601066011660126601366014660156601666017660186601966020660216602266023660246602566026660276602866029660306603166032660336603466035660366603766038660396604066041660426604366044660456604666047660486604966050660516605266053660546605566056660576605866059660606606166062660636606466065660666606766068660696607066071660726607366074660756607666077660786607966080660816608266083660846608566086660876608866089660906609166092660936609466095660966609766098660996610066101661026610366104661056610666107661086610966110661116611266113661146611566116661176611866119661206612166122661236612466125661266612766128661296613066131661326613366134661356613666137661386613966140661416614266143661446614566146661476614866149661506615166152661536615466155661566615766158661596616066161661626616366164661656616666167661686616966170661716617266173661746617566176661776617866179661806618166182661836618466185661866618766188661896619066191661926619366194661956619666197661986619966200662016620266203662046620566206662076620866209662106621166212662136621466215662166621766218662196622066221662226622366224662256622666227662286622966230662316623266233662346623566236662376623866239662406624166242662436624466245662466624766248662496625066251662526625366254662556625666257662586625966260662616626266263662646626566266662676626866269662706627166272662736627466275662766627766278662796628066281662826628366284662856628666287662886628966290662916629266293662946629566296662976629866299663006630166302663036630466305663066630766308663096631066311663126631366314663156631666317663186631966320663216632266323663246632566326663276632866329663306633166332663336633466335663366633766338663396634066341663426634366344663456634666347663486634966350663516635266353663546635566356663576635866359663606636166362663636636466365663666636766368663696637066371663726637366374663756637666377663786637966380663816638266383663846638566386663876638866389663906639166392663936639466395663966639766398663996640066401664026640366404664056640666407664086640966410664116641266413664146641566416664176641866419664206642166422664236642466425664266642766428664296643066431664326643366434664356643666437664386643966440664416644266443664446644566446664476644866449664506645166452664536645466455664566645766458664596646066461664626646366464664656646666467664686646966470664716647266473664746647566476664776647866479664806648166482664836648466485664866648766488664896649066491664926649366494664956649666497664986649966500665016650266503665046650566506665076650866509665106651166512665136651466515665166651766518665196652066521665226652366524665256652666527665286652966530665316653266533665346653566536665376653866539665406654166542665436654466545665466654766548665496655066551665526655366554665556655666557665586655966560665616656266563665646656566566665676656866569665706657166572665736657466575665766657766578665796658066581665826658366584665856658666587665886658966590665916659266593665946659566596665976659866599666006660166602666036660466605666066660766608666096661066611666126661366614666156661666617666186661966620666216662266623666246662566626666276662866629666306663166632666336663466635666366663766638666396664066641666426664366644666456664666647666486664966650666516665266653666546665566656666576665866659666606666166662666636666466665666666666766668666696667066671666726667366674666756667666677666786667966680666816668266683666846668566686666876668866689666906669166692666936669466695666966669766698666996670066701667026670366704667056670666707667086670966710667116671266713667146671566716667176671866719667206672166722667236672466725667266672766728667296673066731667326673366734667356673666737667386673966740667416674266743667446674566746667476674866749667506675166752667536675466755667566675766758667596676066761667626676366764667656676666767667686676966770667716677266773667746677566776667776677866779667806678166782667836678466785667866678766788667896679066791667926679366794667956679666797667986679966800668016680266803668046680566806668076680866809668106681166812668136681466815668166681766818668196682066821668226682366824668256682666827668286682966830668316683266833668346683566836668376683866839668406684166842668436684466845668466684766848668496685066851668526685366854668556685666857668586685966860668616686266863668646686566866668676686866869668706687166872668736687466875668766687766878668796688066881668826688366884668856688666887668886688966890668916689266893668946689566896668976689866899669006690166902669036690466905669066690766908669096691066911669126691366914669156691666917669186691966920669216692266923669246692566926669276692866929669306693166932669336693466935669366693766938669396694066941669426694366944669456694666947669486694966950669516695266953669546695566956669576695866959669606696166962669636696466965669666696766968669696697066971669726697366974669756697666977669786697966980669816698266983669846698566986669876698866989669906699166992669936699466995669966699766998669996700067001670026700367004670056700667007670086700967010670116701267013670146701567016670176701867019670206702167022670236702467025670266702767028670296703067031670326703367034670356703667037670386703967040670416704267043670446704567046670476704867049670506705167052670536705467055670566705767058670596706067061670626706367064670656706667067670686706967070670716707267073670746707567076670776707867079670806708167082670836708467085670866708767088670896709067091670926709367094670956709667097670986709967100671016710267103671046710567106671076710867109671106711167112671136711467115671166711767118671196712067121671226712367124671256712667127671286712967130671316713267133671346713567136671376713867139671406714167142671436714467145671466714767148671496715067151671526715367154671556715667157671586715967160671616716267163671646716567166671676716867169671706717167172671736717467175671766717767178671796718067181671826718367184671856718667187671886718967190671916719267193671946719567196671976719867199672006720167202672036720467205672066720767208672096721067211672126721367214672156721667217672186721967220672216722267223672246722567226672276722867229672306723167232672336723467235672366723767238672396724067241672426724367244672456724667247672486724967250672516725267253672546725567256672576725867259672606726167262672636726467265672666726767268672696727067271672726727367274672756727667277672786727967280672816728267283672846728567286672876728867289672906729167292672936729467295672966729767298672996730067301673026730367304673056730667307673086730967310673116731267313673146731567316673176731867319673206732167322673236732467325673266732767328673296733067331673326733367334673356733667337673386733967340673416734267343673446734567346673476734867349673506735167352673536735467355673566735767358673596736067361673626736367364673656736667367673686736967370673716737267373673746737567376673776737867379673806738167382673836738467385673866738767388673896739067391673926739367394673956739667397673986739967400674016740267403674046740567406674076740867409674106741167412674136741467415674166741767418674196742067421674226742367424674256742667427674286742967430674316743267433674346743567436674376743867439674406744167442674436744467445674466744767448674496745067451674526745367454674556745667457674586745967460674616746267463674646746567466674676746867469674706747167472674736747467475674766747767478674796748067481674826748367484674856748667487674886748967490674916749267493674946749567496674976749867499675006750167502675036750467505675066750767508675096751067511675126751367514675156751667517675186751967520675216752267523675246752567526675276752867529675306753167532675336753467535675366753767538675396754067541675426754367544675456754667547675486754967550675516755267553675546755567556675576755867559675606756167562675636756467565675666756767568675696757067571675726757367574675756757667577675786757967580675816758267583675846758567586675876758867589675906759167592675936759467595675966759767598675996760067601676026760367604676056760667607676086760967610676116761267613676146761567616676176761867619676206762167622676236762467625676266762767628676296763067631676326763367634676356763667637676386763967640676416764267643676446764567646676476764867649676506765167652676536765467655676566765767658676596766067661676626766367664676656766667667676686766967670676716767267673676746767567676676776767867679676806768167682676836768467685676866768767688676896769067691676926769367694676956769667697676986769967700677016770267703677046770567706677076770867709677106771167712677136771467715677166771767718677196772067721677226772367724677256772667727677286772967730677316773267733677346773567736677376773867739677406774167742677436774467745677466774767748677496775067751677526775367754677556775667757677586775967760677616776267763677646776567766677676776867769677706777167772677736777467775677766777767778677796778067781677826778367784677856778667787677886778967790677916779267793677946779567796677976779867799678006780167802678036780467805678066780767808678096781067811678126781367814678156781667817678186781967820678216782267823678246782567826678276782867829678306783167832678336783467835678366783767838678396784067841678426784367844678456784667847678486784967850678516785267853678546785567856678576785867859678606786167862678636786467865678666786767868678696787067871678726787367874678756787667877678786787967880678816788267883678846788567886678876788867889678906789167892678936789467895678966789767898678996790067901679026790367904679056790667907679086790967910679116791267913679146791567916679176791867919679206792167922679236792467925679266792767928679296793067931679326793367934679356793667937679386793967940679416794267943679446794567946679476794867949679506795167952679536795467955679566795767958679596796067961679626796367964679656796667967679686796967970679716797267973679746797567976679776797867979679806798167982679836798467985679866798767988679896799067991679926799367994679956799667997679986799968000680016800268003680046800568006680076800868009680106801168012680136801468015680166801768018680196802068021680226802368024680256802668027680286802968030680316803268033680346803568036680376803868039680406804168042680436804468045680466804768048680496805068051680526805368054680556805668057680586805968060680616806268063680646806568066680676806868069680706807168072680736807468075680766807768078680796808068081680826808368084680856808668087680886808968090680916809268093680946809568096680976809868099681006810168102681036810468105681066810768108681096811068111681126811368114681156811668117681186811968120681216812268123681246812568126681276812868129681306813168132681336813468135681366813768138681396814068141681426814368144681456814668147681486814968150681516815268153681546815568156681576815868159681606816168162681636816468165681666816768168681696817068171681726817368174681756817668177681786817968180681816818268183681846818568186681876818868189681906819168192681936819468195681966819768198681996820068201682026820368204682056820668207682086820968210682116821268213682146821568216682176821868219682206822168222682236822468225682266822768228682296823068231682326823368234682356823668237682386823968240682416824268243682446824568246682476824868249682506825168252682536825468255682566825768258682596826068261682626826368264682656826668267682686826968270682716827268273682746827568276682776827868279682806828168282682836828468285682866828768288682896829068291682926829368294682956829668297682986829968300683016830268303683046830568306683076830868309683106831168312683136831468315683166831768318683196832068321683226832368324683256832668327683286832968330683316833268333683346833568336683376833868339683406834168342683436834468345683466834768348683496835068351683526835368354683556835668357683586835968360683616836268363683646836568366683676836868369683706837168372683736837468375683766837768378683796838068381683826838368384683856838668387683886838968390683916839268393683946839568396683976839868399684006840168402684036840468405684066840768408684096841068411684126841368414684156841668417684186841968420684216842268423684246842568426684276842868429684306843168432684336843468435684366843768438684396844068441684426844368444684456844668447684486844968450684516845268453684546845568456684576845868459684606846168462684636846468465684666846768468684696847068471684726847368474684756847668477684786847968480684816848268483684846848568486684876848868489684906849168492684936849468495684966849768498684996850068501685026850368504685056850668507685086850968510685116851268513685146851568516685176851868519685206852168522685236852468525685266852768528685296853068531685326853368534685356853668537685386853968540685416854268543685446854568546685476854868549685506855168552685536855468555685566855768558685596856068561685626856368564685656856668567685686856968570685716857268573685746857568576685776857868579685806858168582685836858468585685866858768588685896859068591685926859368594685956859668597685986859968600686016860268603686046860568606686076860868609686106861168612686136861468615686166861768618686196862068621686226862368624686256862668627686286862968630686316863268633686346863568636686376863868639686406864168642686436864468645686466864768648686496865068651686526865368654686556865668657686586865968660686616866268663686646866568666686676866868669686706867168672686736867468675686766867768678686796868068681686826868368684686856868668687686886868968690686916869268693686946869568696686976869868699687006870168702687036870468705687066870768708687096871068711687126871368714687156871668717687186871968720687216872268723687246872568726687276872868729687306873168732687336873468735687366873768738687396874068741687426874368744687456874668747687486874968750687516875268753687546875568756687576875868759687606876168762687636876468765687666876768768687696877068771687726877368774687756877668777687786877968780687816878268783687846878568786687876878868789687906879168792687936879468795687966879768798687996880068801688026880368804688056880668807688086880968810688116881268813688146881568816688176881868819688206882168822688236882468825688266882768828688296883068831688326883368834688356883668837688386883968840688416884268843688446884568846688476884868849688506885168852688536885468855688566885768858688596886068861688626886368864688656886668867688686886968870688716887268873688746887568876688776887868879688806888168882688836888468885688866888768888688896889068891688926889368894688956889668897688986889968900689016890268903689046890568906689076890868909689106891168912689136891468915689166891768918689196892068921689226892368924689256892668927689286892968930689316893268933689346893568936689376893868939689406894168942689436894468945689466894768948689496895068951689526895368954689556895668957689586895968960689616896268963689646896568966689676896868969689706897168972689736897468975689766897768978689796898068981689826898368984689856898668987689886898968990689916899268993689946899568996689976899868999690006900169002690036900469005690066900769008690096901069011690126901369014690156901669017690186901969020690216902269023690246902569026690276902869029690306903169032690336903469035690366903769038690396904069041690426904369044690456904669047690486904969050690516905269053690546905569056690576905869059690606906169062690636906469065690666906769068690696907069071690726907369074690756907669077690786907969080690816908269083690846908569086690876908869089690906909169092690936909469095690966909769098690996910069101691026910369104691056910669107691086910969110691116911269113691146911569116691176911869119691206912169122691236912469125691266912769128691296913069131691326913369134691356913669137691386913969140691416914269143691446914569146691476914869149691506915169152691536915469155691566915769158691596916069161691626916369164691656916669167691686916969170691716917269173691746917569176691776917869179691806918169182691836918469185691866918769188691896919069191691926919369194691956919669197691986919969200692016920269203692046920569206692076920869209692106921169212692136921469215692166921769218692196922069221692226922369224692256922669227692286922969230692316923269233692346923569236692376923869239692406924169242692436924469245692466924769248692496925069251692526925369254692556925669257692586925969260692616926269263692646926569266692676926869269692706927169272692736927469275692766927769278692796928069281692826928369284692856928669287692886928969290692916929269293692946929569296692976929869299693006930169302693036930469305693066930769308693096931069311693126931369314693156931669317693186931969320693216932269323693246932569326693276932869329693306933169332693336933469335693366933769338693396934069341693426934369344693456934669347693486934969350693516935269353693546935569356693576935869359693606936169362693636936469365693666936769368693696937069371693726937369374693756937669377693786937969380693816938269383693846938569386693876938869389693906939169392693936939469395693966939769398693996940069401694026940369404694056940669407694086940969410694116941269413694146941569416694176941869419694206942169422694236942469425694266942769428694296943069431694326943369434694356943669437694386943969440694416944269443694446944569446694476944869449694506945169452694536945469455694566945769458694596946069461694626946369464694656946669467694686946969470694716947269473694746947569476694776947869479694806948169482694836948469485694866948769488694896949069491694926949369494694956949669497694986949969500695016950269503695046950569506695076950869509695106951169512695136951469515695166951769518695196952069521695226952369524695256952669527695286952969530695316953269533695346953569536695376953869539695406954169542695436954469545695466954769548695496955069551695526955369554695556955669557695586955969560695616956269563695646956569566695676956869569695706957169572695736957469575695766957769578695796958069581695826958369584695856958669587695886958969590695916959269593695946959569596695976959869599696006960169602696036960469605696066960769608696096961069611696126961369614696156961669617696186961969620696216962269623696246962569626696276962869629696306963169632696336963469635696366963769638696396964069641696426964369644696456964669647696486964969650696516965269653696546965569656696576965869659696606966169662696636966469665696666966769668696696967069671696726967369674696756967669677696786967969680696816968269683696846968569686696876968869689696906969169692696936969469695696966969769698696996970069701697026970369704697056970669707697086970969710697116971269713697146971569716697176971869719697206972169722697236972469725697266972769728697296973069731697326973369734697356973669737697386973969740697416974269743697446974569746697476974869749697506975169752697536975469755697566975769758697596976069761697626976369764697656976669767697686976969770697716977269773697746977569776697776977869779697806978169782697836978469785697866978769788697896979069791697926979369794697956979669797697986979969800698016980269803698046980569806698076980869809698106981169812698136981469815698166981769818698196982069821698226982369824698256982669827698286982969830698316983269833698346983569836698376983869839698406984169842698436984469845698466984769848698496985069851698526985369854698556985669857698586985969860698616986269863698646986569866698676986869869698706987169872698736987469875698766987769878698796988069881698826988369884698856988669887698886988969890698916989269893698946989569896698976989869899699006990169902699036990469905699066990769908699096991069911699126991369914699156991669917699186991969920699216992269923699246992569926699276992869929699306993169932699336993469935699366993769938699396994069941699426994369944699456994669947699486994969950699516995269953699546995569956699576995869959699606996169962699636996469965699666996769968699696997069971699726997369974699756997669977699786997969980699816998269983699846998569986699876998869989699906999169992699936999469995699966999769998699997000070001700027000370004700057000670007700087000970010700117001270013700147001570016700177001870019700207002170022700237002470025700267002770028700297003070031700327003370034700357003670037700387003970040700417004270043700447004570046700477004870049700507005170052700537005470055700567005770058700597006070061700627006370064700657006670067700687006970070700717007270073700747007570076700777007870079700807008170082700837008470085700867008770088700897009070091700927009370094700957009670097700987009970100701017010270103701047010570106701077010870109701107011170112701137011470115701167011770118701197012070121701227012370124701257012670127701287012970130701317013270133701347013570136701377013870139701407014170142701437014470145701467014770148701497015070151701527015370154701557015670157701587015970160701617016270163701647016570166701677016870169701707017170172701737017470175701767017770178701797018070181701827018370184701857018670187701887018970190701917019270193701947019570196701977019870199702007020170202702037020470205702067020770208702097021070211702127021370214702157021670217702187021970220702217022270223702247022570226702277022870229702307023170232702337023470235702367023770238702397024070241702427024370244702457024670247702487024970250702517025270253702547025570256702577025870259702607026170262702637026470265702667026770268702697027070271702727027370274702757027670277702787027970280702817028270283702847028570286702877028870289702907029170292702937029470295702967029770298702997030070301703027030370304703057030670307703087030970310703117031270313703147031570316703177031870319703207032170322703237032470325703267032770328703297033070331703327033370334703357033670337703387033970340703417034270343703447034570346703477034870349703507035170352703537035470355703567035770358703597036070361703627036370364703657036670367703687036970370703717037270373703747037570376703777037870379703807038170382703837038470385703867038770388703897039070391703927039370394703957039670397703987039970400704017040270403704047040570406704077040870409704107041170412704137041470415704167041770418704197042070421704227042370424704257042670427704287042970430704317043270433704347043570436704377043870439704407044170442704437044470445704467044770448704497045070451704527045370454704557045670457704587045970460704617046270463704647046570466704677046870469704707047170472704737047470475704767047770478704797048070481704827048370484704857048670487704887048970490704917049270493704947049570496704977049870499705007050170502705037050470505705067050770508705097051070511705127051370514705157051670517705187051970520705217052270523705247052570526705277052870529705307053170532705337053470535705367053770538705397054070541705427054370544705457054670547705487054970550705517055270553705547055570556705577055870559705607056170562705637056470565705667056770568705697057070571705727057370574705757057670577705787057970580705817058270583705847058570586705877058870589705907059170592705937059470595705967059770598705997060070601706027060370604706057060670607706087060970610706117061270613706147061570616706177061870619706207062170622706237062470625706267062770628706297063070631706327063370634706357063670637706387063970640706417064270643706447064570646706477064870649706507065170652706537065470655706567065770658706597066070661706627066370664706657066670667706687066970670706717067270673706747067570676706777067870679706807068170682706837068470685706867068770688706897069070691706927069370694706957069670697706987069970700707017070270703707047070570706707077070870709707107071170712707137071470715707167071770718707197072070721707227072370724707257072670727707287072970730707317073270733707347073570736707377073870739707407074170742707437074470745707467074770748707497075070751707527075370754707557075670757707587075970760707617076270763707647076570766707677076870769707707077170772707737077470775707767077770778707797078070781707827078370784707857078670787707887078970790707917079270793707947079570796707977079870799708007080170802708037080470805708067080770808708097081070811708127081370814708157081670817708187081970820708217082270823708247082570826708277082870829708307083170832708337083470835708367083770838708397084070841708427084370844708457084670847708487084970850708517085270853708547085570856708577085870859708607086170862708637086470865708667086770868708697087070871708727087370874708757087670877708787087970880708817088270883708847088570886708877088870889708907089170892708937089470895708967089770898708997090070901709027090370904709057090670907709087090970910709117091270913709147091570916709177091870919709207092170922709237092470925709267092770928709297093070931709327093370934709357093670937709387093970940709417094270943709447094570946709477094870949709507095170952709537095470955709567095770958709597096070961709627096370964709657096670967709687096970970709717097270973709747097570976709777097870979709807098170982709837098470985709867098770988709897099070991709927099370994709957099670997709987099971000710017100271003710047100571006710077100871009710107101171012710137101471015710167101771018710197102071021710227102371024710257102671027710287102971030710317103271033710347103571036710377103871039710407104171042710437104471045710467104771048710497105071051710527105371054710557105671057710587105971060710617106271063710647106571066710677106871069710707107171072710737107471075710767107771078710797108071081710827108371084710857108671087710887108971090710917109271093710947109571096710977109871099711007110171102711037110471105711067110771108711097111071111711127111371114711157111671117711187111971120711217112271123711247112571126711277112871129711307113171132711337113471135711367113771138711397114071141711427114371144711457114671147711487114971150711517115271153711547115571156711577115871159711607116171162711637116471165711667116771168711697117071171711727117371174711757117671177711787117971180711817118271183711847118571186711877118871189711907119171192711937119471195711967119771198711997120071201712027120371204712057120671207712087120971210712117121271213712147121571216712177121871219712207122171222712237122471225712267122771228712297123071231712327123371234712357123671237712387123971240712417124271243712447124571246712477124871249712507125171252712537125471255712567125771258712597126071261712627126371264712657126671267712687126971270712717127271273712747127571276712777127871279712807128171282712837128471285712867128771288712897129071291712927129371294712957129671297712987129971300713017130271303713047130571306713077130871309713107131171312713137131471315713167131771318713197132071321713227132371324713257132671327713287132971330713317133271333713347133571336713377133871339713407134171342713437134471345713467134771348713497135071351713527135371354713557135671357713587135971360713617136271363713647136571366713677136871369713707137171372713737137471375713767137771378713797138071381713827138371384713857138671387713887138971390713917139271393713947139571396713977139871399714007140171402714037140471405714067140771408714097141071411714127141371414714157141671417714187141971420714217142271423714247142571426714277142871429714307143171432714337143471435714367143771438714397144071441714427144371444714457144671447714487144971450714517145271453714547145571456714577145871459714607146171462714637146471465714667146771468714697147071471714727147371474714757147671477714787147971480714817148271483714847148571486714877148871489714907149171492714937149471495714967149771498714997150071501715027150371504715057150671507715087150971510715117151271513715147151571516715177151871519715207152171522715237152471525715267152771528715297153071531715327153371534715357153671537715387153971540715417154271543715447154571546715477154871549715507155171552715537155471555715567155771558715597156071561715627156371564715657156671567715687156971570715717157271573715747157571576715777157871579715807158171582715837158471585715867158771588715897159071591715927159371594715957159671597715987159971600716017160271603716047160571606716077160871609716107161171612716137161471615716167161771618716197162071621716227162371624716257162671627716287162971630716317163271633716347163571636716377163871639716407164171642716437164471645716467164771648716497165071651716527165371654716557165671657716587165971660716617166271663716647166571666716677166871669716707167171672716737167471675716767167771678716797168071681716827168371684716857168671687716887168971690716917169271693716947169571696716977169871699717007170171702717037170471705717067170771708717097171071711717127171371714717157171671717717187171971720717217172271723717247172571726717277172871729717307173171732717337173471735717367173771738717397174071741717427174371744717457174671747717487174971750717517175271753717547175571756717577175871759717607176171762717637176471765717667176771768717697177071771717727177371774717757177671777717787177971780717817178271783717847178571786717877178871789717907179171792717937179471795717967179771798717997180071801718027180371804718057180671807718087180971810718117181271813718147181571816718177181871819718207182171822718237182471825718267182771828718297183071831718327183371834718357183671837718387183971840718417184271843718447184571846718477184871849718507185171852718537185471855718567185771858718597186071861718627186371864718657186671867718687186971870718717187271873718747187571876718777187871879718807188171882718837188471885718867188771888718897189071891718927189371894718957189671897718987189971900719017190271903719047190571906719077190871909719107191171912719137191471915719167191771918719197192071921719227192371924719257192671927719287192971930719317193271933719347193571936719377193871939719407194171942719437194471945719467194771948719497195071951719527195371954719557195671957719587195971960719617196271963719647196571966719677196871969719707197171972719737197471975719767197771978719797198071981719827198371984719857198671987719887198971990719917199271993719947199571996719977199871999720007200172002720037200472005720067200772008720097201072011720127201372014720157201672017720187201972020720217202272023720247202572026720277202872029720307203172032720337203472035720367203772038720397204072041720427204372044720457204672047720487204972050720517205272053720547205572056720577205872059720607206172062720637206472065720667206772068720697207072071720727207372074720757207672077720787207972080720817208272083720847208572086720877208872089720907209172092720937209472095720967209772098720997210072101721027210372104721057210672107721087210972110721117211272113721147211572116721177211872119721207212172122721237212472125721267212772128721297213072131721327213372134721357213672137721387213972140721417214272143721447214572146721477214872149721507215172152721537215472155721567215772158721597216072161721627216372164721657216672167721687216972170721717217272173721747217572176721777217872179721807218172182721837218472185721867218772188721897219072191721927219372194721957219672197721987219972200722017220272203722047220572206722077220872209722107221172212722137221472215722167221772218722197222072221722227222372224722257222672227722287222972230722317223272233722347223572236722377223872239722407224172242722437224472245722467224772248722497225072251722527225372254722557225672257722587225972260722617226272263722647226572266722677226872269722707227172272722737227472275722767227772278722797228072281722827228372284722857228672287722887228972290722917229272293722947229572296722977229872299723007230172302723037230472305723067230772308723097231072311723127231372314723157231672317723187231972320723217232272323723247232572326723277232872329723307233172332723337233472335723367233772338723397234072341723427234372344723457234672347723487234972350723517235272353723547235572356723577235872359723607236172362723637236472365723667236772368723697237072371723727237372374723757237672377723787237972380723817238272383723847238572386723877238872389723907239172392723937239472395723967239772398723997240072401724027240372404724057240672407724087240972410724117241272413724147241572416724177241872419724207242172422724237242472425724267242772428724297243072431724327243372434724357243672437724387243972440724417244272443724447244572446724477244872449724507245172452724537245472455724567245772458724597246072461724627246372464724657246672467724687246972470724717247272473724747247572476724777247872479724807248172482724837248472485724867248772488724897249072491724927249372494724957249672497724987249972500725017250272503725047250572506725077250872509725107251172512725137251472515725167251772518725197252072521725227252372524725257252672527725287252972530725317253272533725347253572536725377253872539725407254172542725437254472545725467254772548725497255072551725527255372554725557255672557725587255972560725617256272563725647256572566725677256872569725707257172572725737257472575725767257772578725797258072581725827258372584725857258672587725887258972590725917259272593725947259572596725977259872599726007260172602726037260472605726067260772608726097261072611726127261372614726157261672617726187261972620726217262272623726247262572626726277262872629726307263172632726337263472635726367263772638726397264072641726427264372644726457264672647726487264972650726517265272653726547265572656726577265872659726607266172662726637266472665726667266772668726697267072671726727267372674726757267672677726787267972680726817268272683726847268572686726877268872689726907269172692726937269472695726967269772698726997270072701727027270372704727057270672707727087270972710727117271272713727147271572716727177271872719727207272172722727237272472725727267272772728727297273072731727327273372734727357273672737727387273972740727417274272743727447274572746727477274872749727507275172752727537275472755727567275772758727597276072761727627276372764727657276672767727687276972770727717277272773727747277572776727777277872779727807278172782727837278472785727867278772788727897279072791727927279372794727957279672797727987279972800728017280272803728047280572806728077280872809728107281172812728137281472815728167281772818728197282072821728227282372824728257282672827728287282972830728317283272833728347283572836728377283872839728407284172842728437284472845728467284772848728497285072851728527285372854728557285672857728587285972860728617286272863728647286572866728677286872869728707287172872728737287472875728767287772878728797288072881728827288372884728857288672887728887288972890728917289272893728947289572896728977289872899729007290172902729037290472905729067290772908729097291072911729127291372914729157291672917729187291972920729217292272923729247292572926729277292872929729307293172932729337293472935729367293772938729397294072941729427294372944729457294672947729487294972950729517295272953729547295572956729577295872959729607296172962729637296472965729667296772968729697297072971729727297372974729757297672977729787297972980729817298272983729847298572986729877298872989729907299172992729937299472995729967299772998729997300073001730027300373004730057300673007730087300973010730117301273013730147301573016730177301873019730207302173022730237302473025730267302773028730297303073031730327303373034730357303673037730387303973040730417304273043730447304573046730477304873049730507305173052730537305473055730567305773058730597306073061730627306373064730657306673067730687306973070730717307273073730747307573076730777307873079730807308173082730837308473085730867308773088730897309073091730927309373094730957309673097730987309973100731017310273103731047310573106731077310873109731107311173112731137311473115731167311773118731197312073121731227312373124731257312673127731287312973130731317313273133731347313573136731377313873139731407314173142731437314473145731467314773148731497315073151731527315373154731557315673157731587315973160731617316273163731647316573166731677316873169731707317173172731737317473175731767317773178731797318073181731827318373184731857318673187731887318973190731917319273193731947319573196731977319873199732007320173202732037320473205732067320773208732097321073211732127321373214732157321673217732187321973220732217322273223732247322573226732277322873229732307323173232732337323473235732367323773238732397324073241732427324373244732457324673247732487324973250732517325273253732547325573256732577325873259732607326173262732637326473265732667326773268732697327073271732727327373274732757327673277732787327973280732817328273283732847328573286732877328873289732907329173292732937329473295732967329773298732997330073301733027330373304733057330673307733087330973310733117331273313733147331573316733177331873319733207332173322733237332473325733267332773328733297333073331733327333373334733357333673337733387333973340733417334273343733447334573346733477334873349733507335173352733537335473355733567335773358733597336073361733627336373364733657336673367733687336973370733717337273373733747337573376733777337873379733807338173382733837338473385733867338773388733897339073391733927339373394733957339673397733987339973400734017340273403734047340573406734077340873409734107341173412734137341473415734167341773418734197342073421734227342373424734257342673427734287342973430734317343273433734347343573436734377343873439734407344173442734437344473445734467344773448734497345073451734527345373454734557345673457734587345973460734617346273463734647346573466734677346873469734707347173472734737347473475734767347773478734797348073481734827348373484734857348673487734887348973490734917349273493734947349573496734977349873499735007350173502735037350473505735067350773508735097351073511735127351373514735157351673517735187351973520735217352273523735247352573526735277352873529735307353173532735337353473535735367353773538735397354073541735427354373544735457354673547735487354973550735517355273553735547355573556735577355873559735607356173562735637356473565735667356773568735697357073571735727357373574735757357673577735787357973580735817358273583735847358573586735877358873589735907359173592735937359473595735967359773598735997360073601736027360373604736057360673607736087360973610736117361273613736147361573616736177361873619736207362173622736237362473625736267362773628736297363073631736327363373634736357363673637736387363973640736417364273643736447364573646736477364873649736507365173652736537365473655736567365773658736597366073661736627366373664736657366673667736687366973670736717367273673736747367573676736777367873679736807368173682736837368473685736867368773688736897369073691736927369373694736957369673697736987369973700737017370273703737047370573706737077370873709737107371173712737137371473715737167371773718737197372073721737227372373724737257372673727737287372973730737317373273733737347373573736737377373873739737407374173742737437374473745737467374773748737497375073751737527375373754737557375673757737587375973760737617376273763737647376573766737677376873769737707377173772737737377473775737767377773778737797378073781737827378373784737857378673787737887378973790737917379273793737947379573796737977379873799738007380173802738037380473805738067380773808738097381073811738127381373814738157381673817738187381973820738217382273823738247382573826738277382873829738307383173832738337383473835738367383773838738397384073841738427384373844738457384673847738487384973850738517385273853738547385573856738577385873859738607386173862738637386473865738667386773868738697387073871738727387373874738757387673877738787387973880738817388273883738847388573886738877388873889738907389173892738937389473895738967389773898738997390073901739027390373904739057390673907739087390973910739117391273913739147391573916739177391873919739207392173922739237392473925739267392773928739297393073931739327393373934739357393673937739387393973940739417394273943739447394573946739477394873949739507395173952739537395473955739567395773958739597396073961739627396373964739657396673967739687396973970739717397273973739747397573976739777397873979739807398173982739837398473985739867398773988739897399073991739927399373994739957399673997739987399974000740017400274003740047400574006740077400874009740107401174012740137401474015740167401774018740197402074021740227402374024740257402674027740287402974030740317403274033740347403574036740377403874039740407404174042740437404474045740467404774048740497405074051740527405374054740557405674057740587405974060740617406274063740647406574066740677406874069740707407174072740737407474075740767407774078740797408074081740827408374084740857408674087740887408974090740917409274093740947409574096740977409874099741007410174102741037410474105741067410774108741097411074111741127411374114741157411674117741187411974120741217412274123741247412574126741277412874129741307413174132741337413474135741367413774138741397414074141741427414374144741457414674147741487414974150741517415274153741547415574156741577415874159741607416174162741637416474165741667416774168741697417074171741727417374174741757417674177741787417974180741817418274183741847418574186741877418874189741907419174192741937419474195741967419774198741997420074201742027420374204742057420674207742087420974210742117421274213742147421574216742177421874219742207422174222742237422474225742267422774228742297423074231742327423374234742357423674237742387423974240742417424274243742447424574246742477424874249742507425174252742537425474255742567425774258742597426074261742627426374264742657426674267742687426974270742717427274273742747427574276742777427874279742807428174282742837428474285742867428774288742897429074291742927429374294742957429674297742987429974300743017430274303743047430574306743077430874309743107431174312743137431474315743167431774318743197432074321743227432374324743257432674327743287432974330743317433274333743347433574336743377433874339743407434174342743437434474345743467434774348743497435074351743527435374354743557435674357743587435974360743617436274363743647436574366743677436874369743707437174372743737437474375743767437774378743797438074381743827438374384743857438674387743887438974390743917439274393743947439574396743977439874399744007440174402744037440474405744067440774408744097441074411744127441374414744157441674417744187441974420744217442274423744247442574426744277442874429744307443174432744337443474435744367443774438744397444074441744427444374444744457444674447744487444974450744517445274453744547445574456744577445874459744607446174462744637446474465744667446774468744697447074471744727447374474744757447674477744787447974480744817448274483744847448574486744877448874489744907449174492744937449474495744967449774498744997450074501745027450374504745057450674507745087450974510745117451274513745147451574516745177451874519745207452174522745237452474525745267452774528745297453074531745327453374534745357453674537745387453974540745417454274543745447454574546745477454874549745507455174552745537455474555745567455774558745597456074561745627456374564745657456674567745687456974570745717457274573745747457574576745777457874579745807458174582745837458474585745867458774588745897459074591745927459374594745957459674597745987459974600746017460274603746047460574606746077460874609746107461174612746137461474615746167461774618746197462074621746227462374624746257462674627746287462974630746317463274633746347463574636746377463874639746407464174642746437464474645746467464774648746497465074651746527465374654746557465674657746587465974660746617466274663746647466574666746677466874669746707467174672746737467474675746767467774678746797468074681746827468374684746857468674687746887468974690746917469274693746947469574696746977469874699747007470174702747037470474705747067470774708747097471074711747127471374714747157471674717747187471974720747217472274723747247472574726747277472874729747307473174732747337473474735747367473774738747397474074741747427474374744747457474674747747487474974750747517475274753747547475574756747577475874759747607476174762747637476474765747667476774768747697477074771747727477374774747757477674777747787477974780747817478274783747847478574786747877478874789747907479174792747937479474795747967479774798747997480074801748027480374804748057480674807748087480974810748117481274813748147481574816748177481874819748207482174822748237482474825748267482774828748297483074831748327483374834748357483674837748387483974840748417484274843748447484574846748477484874849748507485174852748537485474855748567485774858748597486074861748627486374864748657486674867748687486974870748717487274873748747487574876748777487874879748807488174882748837488474885748867488774888748897489074891748927489374894748957489674897748987489974900749017490274903749047490574906749077490874909749107491174912749137491474915749167491774918749197492074921749227492374924749257492674927749287492974930749317493274933749347493574936749377493874939749407494174942749437494474945749467494774948749497495074951749527495374954749557495674957749587495974960749617496274963749647496574966749677496874969749707497174972749737497474975749767497774978749797498074981749827498374984749857498674987749887498974990749917499274993749947499574996749977499874999750007500175002750037500475005750067500775008750097501075011750127501375014750157501675017750187501975020750217502275023750247502575026750277502875029750307503175032750337503475035750367503775038750397504075041750427504375044750457504675047750487504975050750517505275053750547505575056750577505875059750607506175062750637506475065750667506775068750697507075071750727507375074750757507675077750787507975080750817508275083750847508575086750877508875089750907509175092750937509475095750967509775098750997510075101751027510375104751057510675107751087510975110751117511275113751147511575116751177511875119751207512175122751237512475125751267512775128751297513075131751327513375134751357513675137751387513975140751417514275143751447514575146751477514875149751507515175152751537515475155751567515775158751597516075161751627516375164751657516675167751687516975170751717517275173751747517575176751777517875179751807518175182751837518475185751867518775188751897519075191751927519375194751957519675197751987519975200752017520275203752047520575206752077520875209752107521175212752137521475215752167521775218752197522075221752227522375224752257522675227752287522975230752317523275233752347523575236752377523875239752407524175242752437524475245752467524775248752497525075251752527525375254752557525675257752587525975260752617526275263752647526575266752677526875269752707527175272752737527475275752767527775278752797528075281752827528375284752857528675287752887528975290752917529275293752947529575296752977529875299753007530175302753037530475305753067530775308753097531075311753127531375314753157531675317753187531975320753217532275323753247532575326753277532875329753307533175332753337533475335753367533775338753397534075341753427534375344753457534675347753487534975350753517535275353753547535575356753577535875359753607536175362753637536475365753667536775368753697537075371753727537375374753757537675377753787537975380753817538275383753847538575386753877538875389753907539175392753937539475395753967539775398753997540075401754027540375404754057540675407754087540975410754117541275413754147541575416754177541875419754207542175422754237542475425754267542775428754297543075431754327543375434754357543675437754387543975440754417544275443754447544575446754477544875449754507545175452754537545475455754567545775458754597546075461754627546375464754657546675467754687546975470754717547275473754747547575476754777547875479754807548175482754837548475485754867548775488754897549075491754927549375494754957549675497754987549975500755017550275503755047550575506755077550875509755107551175512755137551475515755167551775518755197552075521755227552375524755257552675527755287552975530755317553275533755347553575536755377553875539755407554175542755437554475545755467554775548755497555075551755527555375554755557555675557755587555975560755617556275563755647556575566755677556875569755707557175572755737557475575755767557775578755797558075581755827558375584755857558675587755887558975590755917559275593755947559575596755977559875599756007560175602756037560475605756067560775608756097561075611756127561375614756157561675617756187561975620756217562275623756247562575626756277562875629756307563175632756337563475635756367563775638756397564075641756427564375644756457564675647756487564975650756517565275653756547565575656756577565875659756607566175662756637566475665756667566775668756697567075671756727567375674756757567675677756787567975680756817568275683756847568575686756877568875689756907569175692756937569475695756967569775698756997570075701757027570375704757057570675707757087570975710757117571275713757147571575716757177571875719757207572175722757237572475725757267572775728757297573075731757327573375734757357573675737757387573975740757417574275743757447574575746757477574875749757507575175752757537575475755757567575775758757597576075761757627576375764757657576675767757687576975770757717577275773757747577575776757777577875779757807578175782757837578475785757867578775788757897579075791757927579375794757957579675797757987579975800758017580275803758047580575806758077580875809758107581175812758137581475815758167581775818758197582075821758227582375824758257582675827758287582975830758317583275833758347583575836758377583875839758407584175842758437584475845758467584775848758497585075851758527585375854758557585675857758587585975860758617586275863758647586575866758677586875869758707587175872758737587475875758767587775878758797588075881758827588375884758857588675887758887588975890758917589275893758947589575896758977589875899759007590175902759037590475905759067590775908759097591075911759127591375914759157591675917759187591975920759217592275923759247592575926759277592875929759307593175932759337593475935759367593775938759397594075941759427594375944759457594675947759487594975950759517595275953759547595575956759577595875959759607596175962759637596475965759667596775968759697597075971759727597375974759757597675977759787597975980759817598275983759847598575986759877598875989759907599175992759937599475995759967599775998759997600076001760027600376004760057600676007760087600976010760117601276013760147601576016760177601876019760207602176022760237602476025760267602776028760297603076031760327603376034760357603676037760387603976040760417604276043760447604576046760477604876049760507605176052760537605476055760567605776058760597606076061760627606376064760657606676067760687606976070760717607276073760747607576076760777607876079760807608176082760837608476085760867608776088760897609076091760927609376094760957609676097760987609976100761017610276103761047610576106761077610876109761107611176112761137611476115761167611776118761197612076121761227612376124761257612676127761287612976130761317613276133761347613576136761377613876139761407614176142761437614476145761467614776148761497615076151761527615376154761557615676157761587615976160761617616276163761647616576166761677616876169761707617176172761737617476175761767617776178761797618076181761827618376184761857618676187761887618976190761917619276193761947619576196761977619876199762007620176202762037620476205762067620776208762097621076211762127621376214762157621676217762187621976220762217622276223762247622576226762277622876229762307623176232762337623476235762367623776238762397624076241762427624376244762457624676247762487624976250762517625276253762547625576256762577625876259762607626176262762637626476265762667626776268762697627076271762727627376274762757627676277762787627976280762817628276283762847628576286762877628876289762907629176292762937629476295762967629776298762997630076301763027630376304763057630676307763087630976310763117631276313763147631576316763177631876319763207632176322763237632476325763267632776328763297633076331763327633376334763357633676337763387633976340763417634276343763447634576346763477634876349763507635176352763537635476355763567635776358763597636076361763627636376364763657636676367763687636976370763717637276373763747637576376763777637876379763807638176382763837638476385763867638776388763897639076391763927639376394763957639676397763987639976400764017640276403764047640576406764077640876409764107641176412764137641476415764167641776418764197642076421764227642376424764257642676427764287642976430764317643276433764347643576436764377643876439764407644176442764437644476445764467644776448764497645076451764527645376454764557645676457764587645976460764617646276463764647646576466764677646876469764707647176472764737647476475764767647776478764797648076481764827648376484764857648676487764887648976490764917649276493764947649576496764977649876499765007650176502765037650476505765067650776508765097651076511765127651376514765157651676517765187651976520765217652276523765247652576526765277652876529765307653176532765337653476535765367653776538765397654076541765427654376544765457654676547765487654976550765517655276553765547655576556765577655876559765607656176562765637656476565765667656776568765697657076571765727657376574765757657676577765787657976580765817658276583765847658576586765877658876589765907659176592765937659476595765967659776598765997660076601766027660376604766057660676607766087660976610766117661276613766147661576616766177661876619766207662176622766237662476625766267662776628766297663076631766327663376634766357663676637766387663976640766417664276643766447664576646766477664876649766507665176652766537665476655766567665776658766597666076661766627666376664766657666676667766687666976670766717667276673766747667576676766777667876679766807668176682766837668476685766867668776688766897669076691766927669376694766957669676697766987669976700767017670276703767047670576706767077670876709767107671176712767137671476715767167671776718767197672076721767227672376724767257672676727767287672976730767317673276733767347673576736767377673876739767407674176742767437674476745767467674776748767497675076751767527675376754767557675676757767587675976760767617676276763767647676576766767677676876769767707677176772767737677476775767767677776778767797678076781767827678376784767857678676787767887678976790767917679276793767947679576796767977679876799768007680176802768037680476805768067680776808768097681076811768127681376814768157681676817768187681976820768217682276823768247682576826768277682876829768307683176832768337683476835768367683776838768397684076841768427684376844768457684676847768487684976850768517685276853768547685576856768577685876859768607686176862768637686476865768667686776868768697687076871768727687376874768757687676877768787687976880768817688276883768847688576886768877688876889768907689176892768937689476895768967689776898768997690076901769027690376904769057690676907769087690976910769117691276913769147691576916769177691876919769207692176922769237692476925769267692776928769297693076931769327693376934769357693676937769387693976940769417694276943769447694576946769477694876949769507695176952769537695476955769567695776958769597696076961769627696376964769657696676967769687696976970769717697276973769747697576976769777697876979769807698176982769837698476985769867698776988769897699076991769927699376994769957699676997769987699977000770017700277003770047700577006770077700877009770107701177012770137701477015770167701777018770197702077021770227702377024770257702677027770287702977030770317703277033770347703577036770377703877039770407704177042770437704477045770467704777048770497705077051770527705377054770557705677057770587705977060770617706277063770647706577066770677706877069770707707177072770737707477075770767707777078770797708077081770827708377084770857708677087770887708977090770917709277093770947709577096770977709877099771007710177102771037710477105771067710777108771097711077111771127711377114771157711677117771187711977120771217712277123771247712577126771277712877129771307713177132771337713477135771367713777138771397714077141771427714377144771457714677147771487714977150771517715277153771547715577156771577715877159771607716177162771637716477165771667716777168771697717077171771727717377174771757717677177771787717977180771817718277183771847718577186771877718877189771907719177192771937719477195771967719777198771997720077201772027720377204772057720677207772087720977210772117721277213772147721577216772177721877219772207722177222772237722477225772267722777228772297723077231772327723377234772357723677237772387723977240772417724277243772447724577246772477724877249772507725177252772537725477255772567725777258772597726077261772627726377264772657726677267772687726977270772717727277273772747727577276772777727877279772807728177282772837728477285772867728777288772897729077291772927729377294772957729677297772987729977300773017730277303773047730577306773077730877309773107731177312773137731477315773167731777318773197732077321773227732377324773257732677327773287732977330773317733277333773347733577336773377733877339773407734177342773437734477345773467734777348773497735077351773527735377354773557735677357773587735977360773617736277363773647736577366773677736877369773707737177372773737737477375773767737777378773797738077381773827738377384773857738677387773887738977390773917739277393773947739577396773977739877399774007740177402774037740477405774067740777408774097741077411774127741377414774157741677417774187741977420774217742277423774247742577426774277742877429774307743177432774337743477435774367743777438774397744077441774427744377444774457744677447774487744977450774517745277453774547745577456774577745877459774607746177462774637746477465774667746777468774697747077471774727747377474774757747677477774787747977480774817748277483774847748577486774877748877489774907749177492774937749477495774967749777498774997750077501775027750377504775057750677507775087750977510775117751277513775147751577516775177751877519775207752177522775237752477525775267752777528775297753077531775327753377534775357753677537775387753977540775417754277543775447754577546775477754877549775507755177552775537755477555775567755777558775597756077561775627756377564775657756677567775687756977570775717757277573775747757577576775777757877579775807758177582775837758477585775867758777588775897759077591775927759377594775957759677597775987759977600776017760277603776047760577606776077760877609776107761177612776137761477615776167761777618776197762077621776227762377624776257762677627776287762977630776317763277633776347763577636776377763877639776407764177642776437764477645776467764777648776497765077651776527765377654776557765677657776587765977660776617766277663776647766577666776677766877669776707767177672776737767477675776767767777678776797768077681776827768377684776857768677687776887768977690776917769277693776947769577696776977769877699777007770177702777037770477705777067770777708777097771077711777127771377714777157771677717777187771977720777217772277723777247772577726777277772877729777307773177732777337773477735777367773777738777397774077741777427774377744777457774677747777487774977750777517775277753777547775577756777577775877759777607776177762777637776477765777667776777768777697777077771777727777377774777757777677777777787777977780777817778277783777847778577786777877778877789777907779177792777937779477795777967779777798777997780077801778027780377804778057780677807778087780977810778117781277813778147781577816778177781877819778207782177822778237782477825778267782777828778297783077831778327783377834778357783677837778387783977840778417784277843778447784577846778477784877849778507785177852778537785477855778567785777858778597786077861778627786377864778657786677867778687786977870778717787277873778747787577876778777787877879778807788177882778837788477885778867788777888778897789077891778927789377894778957789677897778987789977900779017790277903779047790577906779077790877909779107791177912779137791477915779167791777918779197792077921779227792377924779257792677927779287792977930779317793277933779347793577936779377793877939779407794177942779437794477945779467794777948779497795077951779527795377954779557795677957779587795977960779617796277963779647796577966779677796877969779707797177972779737797477975779767797777978779797798077981779827798377984779857798677987779887798977990779917799277993779947799577996779977799877999780007800178002780037800478005780067800778008780097801078011780127801378014780157801678017780187801978020780217802278023780247802578026780277802878029780307803178032780337803478035780367803778038780397804078041780427804378044780457804678047780487804978050780517805278053780547805578056780577805878059780607806178062780637806478065780667806778068780697807078071780727807378074780757807678077780787807978080780817808278083780847808578086780877808878089780907809178092780937809478095780967809778098780997810078101781027810378104781057810678107781087810978110781117811278113781147811578116781177811878119781207812178122781237812478125781267812778128781297813078131781327813378134781357813678137781387813978140781417814278143781447814578146781477814878149781507815178152781537815478155781567815778158781597816078161781627816378164781657816678167781687816978170781717817278173781747817578176781777817878179781807818178182781837818478185781867818778188781897819078191781927819378194781957819678197781987819978200782017820278203782047820578206782077820878209782107821178212782137821478215782167821778218782197822078221782227822378224782257822678227782287822978230782317823278233782347823578236782377823878239782407824178242782437824478245782467824778248782497825078251782527825378254782557825678257782587825978260782617826278263782647826578266782677826878269782707827178272782737827478275782767827778278782797828078281782827828378284782857828678287782887828978290782917829278293782947829578296782977829878299783007830178302783037830478305783067830778308783097831078311783127831378314783157831678317783187831978320783217832278323783247832578326783277832878329783307833178332783337833478335783367833778338783397834078341783427834378344783457834678347783487834978350783517835278353783547835578356783577835878359783607836178362783637836478365783667836778368783697837078371783727837378374783757837678377783787837978380783817838278383783847838578386783877838878389783907839178392783937839478395783967839778398783997840078401784027840378404784057840678407784087840978410784117841278413784147841578416784177841878419784207842178422784237842478425784267842778428784297843078431784327843378434784357843678437784387843978440784417844278443784447844578446784477844878449784507845178452784537845478455784567845778458784597846078461784627846378464784657846678467784687846978470784717847278473784747847578476784777847878479784807848178482784837848478485784867848778488784897849078491784927849378494784957849678497784987849978500785017850278503785047850578506785077850878509785107851178512785137851478515785167851778518785197852078521785227852378524785257852678527785287852978530785317853278533785347853578536785377853878539785407854178542785437854478545785467854778548785497855078551785527855378554785557855678557785587855978560785617856278563785647856578566785677856878569785707857178572785737857478575785767857778578785797858078581785827858378584785857858678587785887858978590785917859278593785947859578596785977859878599786007860178602786037860478605786067860778608786097861078611786127861378614786157861678617786187861978620786217862278623786247862578626786277862878629786307863178632786337863478635786367863778638786397864078641786427864378644786457864678647786487864978650786517865278653786547865578656786577865878659786607866178662786637866478665786667866778668786697867078671786727867378674786757867678677786787867978680786817868278683786847868578686786877868878689786907869178692786937869478695786967869778698786997870078701787027870378704787057870678707787087870978710787117871278713787147871578716787177871878719787207872178722787237872478725787267872778728787297873078731787327873378734787357873678737787387873978740787417874278743787447874578746787477874878749787507875178752787537875478755787567875778758787597876078761787627876378764787657876678767787687876978770787717877278773787747877578776787777877878779787807878178782787837878478785787867878778788787897879078791787927879378794787957879678797787987879978800788017880278803788047880578806788077880878809788107881178812788137881478815788167881778818788197882078821788227882378824788257882678827788287882978830788317883278833788347883578836788377883878839788407884178842788437884478845788467884778848788497885078851788527885378854788557885678857788587885978860788617886278863788647886578866788677886878869788707887178872788737887478875788767887778878788797888078881788827888378884788857888678887788887888978890788917889278893788947889578896788977889878899789007890178902789037890478905789067890778908789097891078911789127891378914789157891678917789187891978920789217892278923789247892578926789277892878929789307893178932789337893478935789367893778938789397894078941789427894378944789457894678947789487894978950789517895278953789547895578956789577895878959789607896178962789637896478965789667896778968789697897078971789727897378974789757897678977789787897978980789817898278983789847898578986789877898878989789907899178992789937899478995789967899778998789997900079001790027900379004790057900679007790087900979010790117901279013790147901579016790177901879019790207902179022790237902479025790267902779028790297903079031790327903379034790357903679037790387903979040790417904279043790447904579046790477904879049790507905179052790537905479055790567905779058790597906079061790627906379064790657906679067790687906979070790717907279073790747907579076790777907879079790807908179082790837908479085790867908779088790897909079091790927909379094790957909679097790987909979100791017910279103791047910579106791077910879109791107911179112791137911479115791167911779118791197912079121791227912379124791257912679127791287912979130791317913279133791347913579136791377913879139791407914179142791437914479145791467914779148791497915079151791527915379154791557915679157791587915979160791617916279163791647916579166791677916879169791707917179172791737917479175791767917779178791797918079181791827918379184791857918679187791887918979190791917919279193791947919579196791977919879199792007920179202792037920479205792067920779208792097921079211792127921379214792157921679217792187921979220792217922279223792247922579226792277922879229792307923179232792337923479235792367923779238792397924079241792427924379244792457924679247792487924979250792517925279253792547925579256792577925879259792607926179262792637926479265792667926779268792697927079271792727927379274792757927679277792787927979280792817928279283792847928579286792877928879289792907929179292792937929479295792967929779298792997930079301793027930379304793057930679307793087930979310793117931279313793147931579316793177931879319793207932179322793237932479325793267932779328793297933079331793327933379334793357933679337793387933979340793417934279343793447934579346793477934879349793507935179352793537935479355793567935779358793597936079361793627936379364793657936679367793687936979370793717937279373793747937579376793777937879379793807938179382793837938479385793867938779388793897939079391793927939379394793957939679397793987939979400794017940279403794047940579406794077940879409794107941179412794137941479415794167941779418794197942079421794227942379424794257942679427794287942979430794317943279433794347943579436794377943879439794407944179442794437944479445794467944779448794497945079451794527945379454794557945679457794587945979460794617946279463794647946579466794677946879469794707947179472794737947479475794767947779478794797948079481794827948379484794857948679487794887948979490794917949279493794947949579496794977949879499795007950179502795037950479505795067950779508795097951079511795127951379514795157951679517795187951979520795217952279523795247952579526795277952879529795307953179532795337953479535795367953779538795397954079541795427954379544795457954679547795487954979550795517955279553795547955579556795577955879559795607956179562795637956479565795667956779568795697957079571795727957379574795757957679577795787957979580795817958279583795847958579586795877958879589795907959179592795937959479595795967959779598795997960079601796027960379604796057960679607796087960979610796117961279613796147961579616796177961879619796207962179622796237962479625796267962779628796297963079631796327963379634796357963679637796387963979640796417964279643796447964579646796477964879649796507965179652796537965479655796567965779658796597966079661796627966379664796657966679667796687966979670796717967279673796747967579676796777967879679796807968179682796837968479685796867968779688796897969079691796927969379694796957969679697796987969979700797017970279703797047970579706797077970879709797107971179712797137971479715797167971779718797197972079721797227972379724797257972679727797287972979730797317973279733797347973579736797377973879739797407974179742797437974479745797467974779748797497975079751797527975379754797557975679757797587975979760797617976279763797647976579766797677976879769797707977179772797737977479775797767977779778797797978079781797827978379784797857978679787797887978979790797917979279793797947979579796797977979879799798007980179802798037980479805798067980779808798097981079811798127981379814798157981679817798187981979820798217982279823798247982579826798277982879829798307983179832798337983479835798367983779838798397984079841798427984379844798457984679847798487984979850798517985279853798547985579856798577985879859798607986179862798637986479865798667986779868798697987079871798727987379874798757987679877798787987979880798817988279883798847988579886798877988879889798907989179892798937989479895798967989779898798997990079901799027990379904799057990679907799087990979910799117991279913799147991579916799177991879919799207992179922799237992479925799267992779928799297993079931799327993379934799357993679937799387993979940799417994279943799447994579946799477994879949799507995179952799537995479955799567995779958799597996079961799627996379964799657996679967799687996979970799717997279973799747997579976799777997879979799807998179982799837998479985799867998779988799897999079991799927999379994799957999679997799987999980000800018000280003800048000580006800078000880009800108001180012800138001480015800168001780018800198002080021800228002380024800258002680027800288002980030800318003280033800348003580036800378003880039800408004180042800438004480045800468004780048800498005080051800528005380054800558005680057800588005980060800618006280063800648006580066800678006880069800708007180072800738007480075800768007780078800798008080081800828008380084800858008680087800888008980090800918009280093800948009580096800978009880099801008010180102801038010480105801068010780108801098011080111801128011380114801158011680117801188011980120801218012280123801248012580126801278012880129801308013180132801338013480135801368013780138801398014080141801428014380144801458014680147801488014980150801518015280153801548015580156801578015880159801608016180162801638016480165801668016780168801698017080171801728017380174801758017680177801788017980180801818018280183801848018580186801878018880189801908019180192801938019480195801968019780198801998020080201802028020380204802058020680207802088020980210802118021280213802148021580216802178021880219802208022180222802238022480225802268022780228802298023080231802328023380234802358023680237802388023980240802418024280243802448024580246802478024880249802508025180252802538025480255802568025780258802598026080261802628026380264802658026680267802688026980270802718027280273802748027580276802778027880279802808028180282802838028480285802868028780288802898029080291802928029380294802958029680297802988029980300803018030280303803048030580306803078030880309803108031180312803138031480315803168031780318803198032080321803228032380324803258032680327803288032980330803318033280333803348033580336803378033880339803408034180342803438034480345803468034780348803498035080351803528035380354803558035680357803588035980360803618036280363803648036580366803678036880369803708037180372803738037480375803768037780378803798038080381803828038380384803858038680387803888038980390803918039280393803948039580396803978039880399804008040180402804038040480405804068040780408804098041080411804128041380414804158041680417804188041980420804218042280423804248042580426804278042880429804308043180432804338043480435804368043780438804398044080441804428044380444804458044680447804488044980450804518045280453804548045580456804578045880459804608046180462804638046480465804668046780468804698047080471804728047380474804758047680477804788047980480804818048280483804848048580486804878048880489804908049180492804938049480495804968049780498804998050080501805028050380504805058050680507805088050980510805118051280513805148051580516805178051880519805208052180522805238052480525805268052780528805298053080531805328053380534805358053680537805388053980540805418054280543805448054580546805478054880549805508055180552805538055480555805568055780558805598056080561805628056380564805658056680567805688056980570805718057280573805748057580576805778057880579805808058180582805838058480585805868058780588805898059080591805928059380594805958059680597805988059980600806018060280603806048060580606806078060880609806108061180612806138061480615806168061780618806198062080621806228062380624806258062680627806288062980630806318063280633806348063580636806378063880639806408064180642806438064480645806468064780648806498065080651806528065380654806558065680657806588065980660806618066280663806648066580666806678066880669806708067180672806738067480675806768067780678806798068080681806828068380684806858068680687806888068980690806918069280693806948069580696806978069880699807008070180702807038070480705807068070780708807098071080711807128071380714807158071680717807188071980720807218072280723807248072580726807278072880729807308073180732807338073480735807368073780738807398074080741807428074380744807458074680747807488074980750807518075280753807548075580756807578075880759807608076180762807638076480765807668076780768807698077080771807728077380774807758077680777807788077980780807818078280783807848078580786807878078880789807908079180792807938079480795807968079780798807998080080801808028080380804808058080680807808088080980810808118081280813808148081580816808178081880819808208082180822808238082480825808268082780828808298083080831808328083380834808358083680837808388083980840808418084280843808448084580846808478084880849808508085180852808538085480855808568085780858808598086080861808628086380864808658086680867808688086980870808718087280873808748087580876808778087880879808808088180882808838088480885808868088780888808898089080891808928089380894808958089680897808988089980900809018090280903809048090580906809078090880909809108091180912809138091480915809168091780918809198092080921809228092380924809258092680927809288092980930809318093280933809348093580936809378093880939809408094180942809438094480945809468094780948809498095080951809528095380954809558095680957809588095980960809618096280963809648096580966809678096880969809708097180972809738097480975809768097780978809798098080981809828098380984809858098680987809888098980990809918099280993809948099580996809978099880999810008100181002810038100481005810068100781008810098101081011810128101381014810158101681017810188101981020810218102281023810248102581026810278102881029810308103181032810338103481035810368103781038810398104081041810428104381044810458104681047810488104981050810518105281053810548105581056810578105881059810608106181062810638106481065810668106781068810698107081071810728107381074810758107681077810788107981080810818108281083810848108581086810878108881089810908109181092810938109481095810968109781098810998110081101811028110381104811058110681107811088110981110811118111281113811148111581116811178111881119811208112181122811238112481125811268112781128811298113081131811328113381134811358113681137811388113981140811418114281143811448114581146811478114881149811508115181152811538115481155811568115781158811598116081161811628116381164811658116681167811688116981170811718117281173811748117581176811778117881179811808118181182811838118481185811868118781188811898119081191811928119381194811958119681197811988119981200812018120281203812048120581206812078120881209812108121181212812138121481215812168121781218812198122081221812228122381224812258122681227812288122981230812318123281233812348123581236812378123881239812408124181242812438124481245812468124781248812498125081251812528125381254812558125681257812588125981260812618126281263812648126581266812678126881269812708127181272812738127481275812768127781278812798128081281812828128381284812858128681287812888128981290812918129281293812948129581296812978129881299813008130181302813038130481305813068130781308813098131081311813128131381314813158131681317813188131981320813218132281323813248132581326813278132881329813308133181332813338133481335813368133781338813398134081341813428134381344813458134681347813488134981350813518135281353813548135581356813578135881359813608136181362813638136481365813668136781368813698137081371813728137381374813758137681377813788137981380813818138281383813848138581386813878138881389813908139181392813938139481395813968139781398813998140081401814028140381404814058140681407814088140981410814118141281413814148141581416814178141881419814208142181422814238142481425814268142781428814298143081431814328143381434814358143681437814388143981440814418144281443814448144581446814478144881449814508145181452814538145481455814568145781458814598146081461814628146381464814658146681467814688146981470814718147281473814748147581476814778147881479814808148181482814838148481485814868148781488814898149081491814928149381494814958149681497814988149981500815018150281503815048150581506815078150881509815108151181512815138151481515815168151781518815198152081521815228152381524815258152681527815288152981530815318153281533815348153581536815378153881539815408154181542815438154481545815468154781548815498155081551815528155381554815558155681557
  1. /*
  2. * Single source autogenerated distributable for Duktape 1.3.99.
  3. * Git commit df5e369cbd20005700281f008e49ab09725677ef (v1.3.0-202-gdf5e369-dirty).
  4. * Git branch master.
  5. *
  6. * See Duktape AUTHORS.rst and LICENSE.txt for copyright and
  7. * licensing information.
  8. */
  9. /* LICENSE.txt */
  10. /*
  11. * ===============
  12. * Duktape license
  13. * ===============
  14. *
  15. * (http://opensource.org/licenses/MIT)
  16. *
  17. * Copyright (c) 2013-2015 by Duktape authors (see AUTHORS.rst)
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining a copy
  20. * of this software and associated documentation files (the "Software"), to deal
  21. * in the Software without restriction, including without limitation the rights
  22. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  23. * copies of the Software, and to permit persons to whom the Software is
  24. * furnished to do so, subject to the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be included in
  27. * all copies or substantial portions of the Software.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  32. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  34. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  35. * THE SOFTWARE.
  36. */
  37. /* AUTHORS.rst */
  38. /*
  39. * ===============
  40. * Duktape authors
  41. * ===============
  42. *
  43. * Copyright
  44. * =========
  45. *
  46. * Duktape copyrights are held by its authors. Each author has a copyright
  47. * to their contribution, and agrees to irrevocably license the contribution
  48. * under the Duktape ``LICENSE.txt``.
  49. *
  50. * Authors
  51. * =======
  52. *
  53. * Please include an e-mail address, a link to your GitHub profile, or something
  54. * similar to allow your contribution to be identified accurately.
  55. *
  56. * The following people have contributed code, website contents, or Wiki contents,
  57. * and agreed to irrevocably license their contributions under the Duktape
  58. * ``LICENSE.txt`` (in order of appearance):
  59. *
  60. * * Sami Vaarala <[email protected]>
  61. * * Niki Dobrev
  62. * * Andreas \u00d6man <[email protected]>
  63. * * L\u00e1szl\u00f3 Lang\u00f3 <[email protected]>
  64. * * Legimet <[email protected]>
  65. * * Karl Skomski <[email protected]>
  66. * * Bruce Pascoe <[email protected]>
  67. *
  68. * Other contributions
  69. * ===================
  70. *
  71. * The following people have contributed something other than code (e.g. reported
  72. * bugs, provided ideas, etc; roughly in order of appearance):
  73. *
  74. * * Greg Burns
  75. * * Anthony Rabine
  76. * * Carlos Costa
  77. * * Aur\u00e9lien Bouilland
  78. * * Preet Desai (Pris Matic)
  79. * * judofyr (http://www.reddit.com/user/judofyr)
  80. * * Jason Woofenden
  81. * * Micha\u0142 Przyby\u015b
  82. * * Anthony Howe
  83. * * Conrad Pankoff
  84. * * Jim Schimpf
  85. * * Rajaran Gaunker (https://github.com/zimbabao)
  86. * * Andreas \u00d6man
  87. * * Doug Sanden
  88. * * Josh Engebretson (https://github.com/JoshEngebretson)
  89. * * Remo Eichenberger (https://github.com/remoe)
  90. * * Mamod Mehyar (https://github.com/mamod)
  91. * * David Demelier (https://github.com/markand)
  92. * * Tim Caswell (https://github.com/creationix)
  93. * * Mitchell Blank Jr (https://github.com/mitchblank)
  94. * * https://github.com/yushli
  95. * * Seo Sanghyeon (https://github.com/sanxiyn)
  96. * * Han ChoongWoo (https://github.com/tunz)
  97. * * Joshua Peek (https://github.com/josh)
  98. * * Bruce E. Pascoe (https://github.com/fatcerberus)
  99. * * https://github.com/Kelledin
  100. * * https://github.com/sstruchtrup
  101. * * Michael Drake (https://github.com/tlsa)
  102. * * https://github.com/chris-y
  103. *
  104. * If you are accidentally missing from this list, send me an e-mail
  105. * (``[email protected]``) and I'll fix the omission.
  106. */
  107. #line 1 "duk_internal.h"
  108. /*
  109. * Top-level include file to be used for all (internal) source files.
  110. *
  111. * Source files should not include individual header files, as they
  112. * have not been designed to be individually included.
  113. */
  114. #ifndef DUK_INTERNAL_H_INCLUDED
  115. #define DUK_INTERNAL_H_INCLUDED
  116. /*
  117. * The 'duktape.h' header provides the public API, but also handles all
  118. * compiler and platform specific feature detection, Duktape feature
  119. * resolution, inclusion of system headers, etc. These have been merged
  120. * because the public API is also dependent on e.g. detecting appropriate
  121. * C types which is quite platform/compiler specific especially for a non-C99
  122. * build. The public API is also dependent on the resolved feature set.
  123. *
  124. * Some actions taken by the merged header (such as including system headers)
  125. * are not appropriate for building a user application. The define
  126. * DUK_COMPILING_DUKTAPE allows the merged header to skip/include some
  127. * sections depending on what is being built.
  128. */
  129. #define DUK_COMPILING_DUKTAPE
  130. #include "duktape.h"
  131. /*
  132. * User declarations, e.g. prototypes for user functions used by Duktape
  133. * macros. Concretely, if DUK_USE_PANIC_HANDLER is used and the macro
  134. * value calls a user function, it needs to be declared for Duktape
  135. * compilation to avoid warnings.
  136. */
  137. DUK_USE_USER_DECLARE()
  138. /*
  139. * Duktape includes (other than duk_features.h)
  140. *
  141. * The header files expect to be included in an order which satisfies header
  142. * dependencies correctly (the headers themselves don't include any other
  143. * includes). Forward declarations are used to break circular struct/typedef
  144. * dependencies.
  145. */
  146. #line 1 "duk_replacements.h"
  147. #ifndef DUK_REPLACEMENTS_H_INCLUDED
  148. #define DUK_REPLACEMENTS_H_INCLUDED
  149. #ifdef DUK_USE_REPL_FPCLASSIFY
  150. DUK_INTERNAL_DECL int duk_repl_fpclassify(double x);
  151. #endif
  152. #ifdef DUK_USE_REPL_SIGNBIT
  153. DUK_INTERNAL_DECL int duk_repl_signbit(double x);
  154. #endif
  155. #ifdef DUK_USE_REPL_ISFINITE
  156. DUK_INTERNAL_DECL int duk_repl_isfinite(double x);
  157. #endif
  158. #ifdef DUK_USE_REPL_ISNAN
  159. DUK_INTERNAL_DECL int duk_repl_isnan(double x);
  160. #endif
  161. #ifdef DUK_USE_REPL_ISINF
  162. DUK_INTERNAL_DECL int duk_repl_isinf(double x);
  163. #endif
  164. #endif /* DUK_REPLACEMENTS_H_INCLUDED */
  165. #line 1 "duk_jmpbuf.h"
  166. /*
  167. * Wrapper for jmp_buf.
  168. *
  169. * This is used because jmp_buf is an array type for backward compatibility.
  170. * Wrapping jmp_buf in a struct makes pointer references, sizeof, etc,
  171. * behave more intuitively.
  172. *
  173. * http://en.wikipedia.org/wiki/Setjmp.h#Member_types
  174. */
  175. #ifndef DUK_JMPBUF_H_INCLUDED
  176. #define DUK_JMPBUF_H_INCLUDED
  177. struct duk_jmpbuf {
  178. #if defined(DUK_USE_SETJMP) || defined(DUK_USE_UNDERSCORE_SETJMP)
  179. jmp_buf jb;
  180. #elif defined(DUK_USE_SIGSETJMP)
  181. sigjmp_buf jb;
  182. #else
  183. #error internal error, no long control transfer provider
  184. #endif
  185. };
  186. #endif /* DUK_JMPBUF_H_INCLUDED */
  187. #line 1 "duk_forwdecl.h"
  188. /*
  189. * Forward declarations for all Duktape structures.
  190. */
  191. #ifndef DUK_FORWDECL_H_INCLUDED
  192. #define DUK_FORWDECL_H_INCLUDED
  193. /*
  194. * Forward declarations
  195. */
  196. struct duk_jmpbuf;
  197. /* duk_tval intentionally skipped */
  198. struct duk_heaphdr;
  199. struct duk_heaphdr_string;
  200. struct duk_hstring;
  201. struct duk_hstring_external;
  202. struct duk_hobject;
  203. struct duk_hcompiledfunction;
  204. struct duk_hnativefunction;
  205. struct duk_hthread;
  206. struct duk_hbufferobject;
  207. struct duk_hbuffer;
  208. struct duk_hbuffer_fixed;
  209. struct duk_hbuffer_dynamic;
  210. struct duk_hbuffer_external;
  211. struct duk_propaccessor;
  212. union duk_propvalue;
  213. struct duk_propdesc;
  214. struct duk_heap;
  215. struct duk_breakpoint;
  216. struct duk_activation;
  217. struct duk_catcher;
  218. struct duk_strcache;
  219. struct duk_ljstate;
  220. struct duk_strtab_entry;
  221. #ifdef DUK_USE_DEBUG
  222. struct duk_fixedbuffer;
  223. #endif
  224. struct duk_bitdecoder_ctx;
  225. struct duk_bitencoder_ctx;
  226. struct duk_bufwriter_ctx;
  227. struct duk_token;
  228. struct duk_re_token;
  229. struct duk_lexer_point;
  230. struct duk_lexer_ctx;
  231. struct duk_lexer_codepoint;
  232. struct duk_compiler_instr;
  233. struct duk_compiler_func;
  234. struct duk_compiler_ctx;
  235. struct duk_re_matcher_ctx;
  236. struct duk_re_compiler_ctx;
  237. typedef struct duk_jmpbuf duk_jmpbuf;
  238. /* duk_tval intentionally skipped */
  239. typedef struct duk_heaphdr duk_heaphdr;
  240. typedef struct duk_heaphdr_string duk_heaphdr_string;
  241. typedef struct duk_hstring duk_hstring;
  242. typedef struct duk_hstring_external duk_hstring_external;
  243. typedef struct duk_hobject duk_hobject;
  244. typedef struct duk_hcompiledfunction duk_hcompiledfunction;
  245. typedef struct duk_hnativefunction duk_hnativefunction;
  246. typedef struct duk_hbufferobject duk_hbufferobject;
  247. typedef struct duk_hthread duk_hthread;
  248. typedef struct duk_hbuffer duk_hbuffer;
  249. typedef struct duk_hbuffer_fixed duk_hbuffer_fixed;
  250. typedef struct duk_hbuffer_dynamic duk_hbuffer_dynamic;
  251. typedef struct duk_hbuffer_external duk_hbuffer_external;
  252. typedef struct duk_propaccessor duk_propaccessor;
  253. typedef union duk_propvalue duk_propvalue;
  254. typedef struct duk_propdesc duk_propdesc;
  255. typedef struct duk_heap duk_heap;
  256. typedef struct duk_breakpoint duk_breakpoint;
  257. typedef struct duk_activation duk_activation;
  258. typedef struct duk_catcher duk_catcher;
  259. typedef struct duk_strcache duk_strcache;
  260. typedef struct duk_ljstate duk_ljstate;
  261. typedef struct duk_strtab_entry duk_strtab_entry;
  262. #ifdef DUK_USE_DEBUG
  263. typedef struct duk_fixedbuffer duk_fixedbuffer;
  264. #endif
  265. typedef struct duk_bitdecoder_ctx duk_bitdecoder_ctx;
  266. typedef struct duk_bitencoder_ctx duk_bitencoder_ctx;
  267. typedef struct duk_bufwriter_ctx duk_bufwriter_ctx;
  268. typedef struct duk_token duk_token;
  269. typedef struct duk_re_token duk_re_token;
  270. typedef struct duk_lexer_point duk_lexer_point;
  271. typedef struct duk_lexer_ctx duk_lexer_ctx;
  272. typedef struct duk_lexer_codepoint duk_lexer_codepoint;
  273. typedef struct duk_compiler_instr duk_compiler_instr;
  274. typedef struct duk_compiler_func duk_compiler_func;
  275. typedef struct duk_compiler_ctx duk_compiler_ctx;
  276. typedef struct duk_re_matcher_ctx duk_re_matcher_ctx;
  277. typedef struct duk_re_compiler_ctx duk_re_compiler_ctx;
  278. #endif /* DUK_FORWDECL_H_INCLUDED */
  279. #line 1 "duk_builtins.h"
  280. /*
  281. * Automatically generated by genbuiltins.py, do not edit!
  282. */
  283. #ifndef DUK_BUILTINS_H_INCLUDED
  284. #define DUK_BUILTINS_H_INCLUDED
  285. #if defined(DUK_USE_DOUBLE_LE)
  286. #if !defined(DUK_SINGLE_FILE)
  287. DUK_INTERNAL_DECL const duk_uint8_t duk_strings_data[2624];
  288. #endif /* !DUK_SINGLE_FILE */
  289. #define DUK_STRDATA_DATA_LENGTH 2624
  290. #define DUK_STRDATA_MAX_STRLEN 24
  291. #define DUK_STRIDX_UC_LOGGER 0 /* 'Logger' */
  292. #define DUK_STRIDX_UC_THREAD 1 /* 'Thread' */
  293. #define DUK_STRIDX_UC_POINTER 2 /* 'Pointer' */
  294. #define DUK_STRIDX_DEC_ENV 3 /* 'DecEnv' */
  295. #define DUK_STRIDX_OBJ_ENV 4 /* 'ObjEnv' */
  296. #define DUK_STRIDX_FLOAT64_ARRAY 5 /* 'Float64Array' */
  297. #define DUK_STRIDX_FLOAT32_ARRAY 6 /* 'Float32Array' */
  298. #define DUK_STRIDX_UINT32_ARRAY 7 /* 'Uint32Array' */
  299. #define DUK_STRIDX_INT32_ARRAY 8 /* 'Int32Array' */
  300. #define DUK_STRIDX_UINT16_ARRAY 9 /* 'Uint16Array' */
  301. #define DUK_STRIDX_INT16_ARRAY 10 /* 'Int16Array' */
  302. #define DUK_STRIDX_UINT8_CLAMPED_ARRAY 11 /* 'Uint8ClampedArray' */
  303. #define DUK_STRIDX_UINT8_ARRAY 12 /* 'Uint8Array' */
  304. #define DUK_STRIDX_INT8_ARRAY 13 /* 'Int8Array' */
  305. #define DUK_STRIDX_DATA_VIEW 14 /* 'DataView' */
  306. #define DUK_STRIDX_ARRAY_BUFFER 15 /* 'ArrayBuffer' */
  307. #define DUK_STRIDX_UC_BUFFER 16 /* 'Buffer' */
  308. #define DUK_STRIDX_EMPTY_STRING 17 /* '' */
  309. #define DUK_STRIDX_GLOBAL 18 /* 'global' */
  310. #define DUK_STRIDX_UC_ARGUMENTS 19 /* 'Arguments' */
  311. #define DUK_STRIDX_JSON 20 /* 'JSON' */
  312. #define DUK_STRIDX_MATH 21 /* 'Math' */
  313. #define DUK_STRIDX_UC_ERROR 22 /* 'Error' */
  314. #define DUK_STRIDX_REG_EXP 23 /* 'RegExp' */
  315. #define DUK_STRIDX_DATE 24 /* 'Date' */
  316. #define DUK_STRIDX_UC_NUMBER 25 /* 'Number' */
  317. #define DUK_STRIDX_UC_BOOLEAN 26 /* 'Boolean' */
  318. #define DUK_STRIDX_UC_STRING 27 /* 'String' */
  319. #define DUK_STRIDX_ARRAY 28 /* 'Array' */
  320. #define DUK_STRIDX_UC_FUNCTION 29 /* 'Function' */
  321. #define DUK_STRIDX_UC_OBJECT 30 /* 'Object' */
  322. #define DUK_STRIDX_UC_NULL 31 /* 'Null' */
  323. #define DUK_STRIDX_UC_UNDEFINED 32 /* 'Undefined' */
  324. #define DUK_STRIDX_JSON_EXT_FUNCTION2 33 /* '{_func:true}' */
  325. #define DUK_STRIDX_JSON_EXT_FUNCTION1 34 /* '{"_func":true}' */
  326. #define DUK_STRIDX_JSON_EXT_NEGINF 35 /* '{"_ninf":true}' */
  327. #define DUK_STRIDX_JSON_EXT_POSINF 36 /* '{"_inf":true}' */
  328. #define DUK_STRIDX_JSON_EXT_NAN 37 /* '{"_nan":true}' */
  329. #define DUK_STRIDX_JSON_EXT_UNDEFINED 38 /* '{"_undef":true}' */
  330. #define DUK_STRIDX_TO_LOG_STRING 39 /* 'toLogString' */
  331. #define DUK_STRIDX_CLOG 40 /* 'clog' */
  332. #define DUK_STRIDX_LC_L 41 /* 'l' */
  333. #define DUK_STRIDX_LC_N 42 /* 'n' */
  334. #define DUK_STRIDX_LC_FATAL 43 /* 'fatal' */
  335. #define DUK_STRIDX_LC_ERROR 44 /* 'error' */
  336. #define DUK_STRIDX_LC_WARN 45 /* 'warn' */
  337. #define DUK_STRIDX_LC_DEBUG 46 /* 'debug' */
  338. #define DUK_STRIDX_LC_TRACE 47 /* 'trace' */
  339. #define DUK_STRIDX_RAW 48 /* 'raw' */
  340. #define DUK_STRIDX_FMT 49 /* 'fmt' */
  341. #define DUK_STRIDX_CURRENT 50 /* 'current' */
  342. #define DUK_STRIDX_RESUME 51 /* 'resume' */
  343. #define DUK_STRIDX_COMPACT 52 /* 'compact' */
  344. #define DUK_STRIDX_JC 53 /* 'jc' */
  345. #define DUK_STRIDX_JX 54 /* 'jx' */
  346. #define DUK_STRIDX_BASE64 55 /* 'base64' */
  347. #define DUK_STRIDX_HEX 56 /* 'hex' */
  348. #define DUK_STRIDX_DEC 57 /* 'dec' */
  349. #define DUK_STRIDX_ENC 58 /* 'enc' */
  350. #define DUK_STRIDX_FIN 59 /* 'fin' */
  351. #define DUK_STRIDX_GC 60 /* 'gc' */
  352. #define DUK_STRIDX_ACT 61 /* 'act' */
  353. #define DUK_STRIDX_LC_INFO 62 /* 'info' */
  354. #define DUK_STRIDX_VERSION 63 /* 'version' */
  355. #define DUK_STRIDX_ENV 64 /* 'env' */
  356. #define DUK_STRIDX_MOD_LOADED 65 /* 'modLoaded' */
  357. #define DUK_STRIDX_MOD_SEARCH 66 /* 'modSearch' */
  358. #define DUK_STRIDX_ERR_THROW 67 /* 'errThrow' */
  359. #define DUK_STRIDX_ERR_CREATE 68 /* 'errCreate' */
  360. #define DUK_STRIDX_COMPILE 69 /* 'compile' */
  361. #define DUK_STRIDX_INT_REGBASE 70 /* '\x00Regbase' */
  362. #define DUK_STRIDX_INT_THREAD 71 /* '\x00Thread' */
  363. #define DUK_STRIDX_INT_HANDLER 72 /* '\x00Handler' */
  364. #define DUK_STRIDX_INT_FINALIZER 73 /* '\x00Finalizer' */
  365. #define DUK_STRIDX_INT_CALLEE 74 /* '\x00Callee' */
  366. #define DUK_STRIDX_INT_MAP 75 /* '\x00Map' */
  367. #define DUK_STRIDX_INT_ARGS 76 /* '\x00Args' */
  368. #define DUK_STRIDX_INT_THIS 77 /* '\x00This' */
  369. #define DUK_STRIDX_INT_PC2LINE 78 /* '\x00Pc2line' */
  370. #define DUK_STRIDX_INT_SOURCE 79 /* '\x00Source' */
  371. #define DUK_STRIDX_INT_VARENV 80 /* '\x00Varenv' */
  372. #define DUK_STRIDX_INT_LEXENV 81 /* '\x00Lexenv' */
  373. #define DUK_STRIDX_INT_VARMAP 82 /* '\x00Varmap' */
  374. #define DUK_STRIDX_INT_FORMALS 83 /* '\x00Formals' */
  375. #define DUK_STRIDX_INT_BYTECODE 84 /* '\x00Bytecode' */
  376. #define DUK_STRIDX_INT_NEXT 85 /* '\x00Next' */
  377. #define DUK_STRIDX_INT_TARGET 86 /* '\x00Target' */
  378. #define DUK_STRIDX_INT_VALUE 87 /* '\x00Value' */
  379. #define DUK_STRIDX_LC_POINTER 88 /* 'pointer' */
  380. #define DUK_STRIDX_INT_TRACEDATA 89 /* '\x00Tracedata' */
  381. #define DUK_STRIDX_LINE_NUMBER 90 /* 'lineNumber' */
  382. #define DUK_STRIDX_FILE_NAME 91 /* 'fileName' */
  383. #define DUK_STRIDX_PC 92 /* 'pc' */
  384. #define DUK_STRIDX_STACK 93 /* 'stack' */
  385. #define DUK_STRIDX_THROW_TYPE_ERROR 94 /* 'ThrowTypeError' */
  386. #define DUK_STRIDX_DUKTAPE 95 /* 'Duktape' */
  387. #define DUK_STRIDX_SET_FLOAT64 96 /* 'setFloat64' */
  388. #define DUK_STRIDX_SET_FLOAT32 97 /* 'setFloat32' */
  389. #define DUK_STRIDX_SET_UINT32 98 /* 'setUint32' */
  390. #define DUK_STRIDX_SET_INT32 99 /* 'setInt32' */
  391. #define DUK_STRIDX_SET_UINT16 100 /* 'setUint16' */
  392. #define DUK_STRIDX_SET_INT16 101 /* 'setInt16' */
  393. #define DUK_STRIDX_SET_UINT8 102 /* 'setUint8' */
  394. #define DUK_STRIDX_SET_INT8 103 /* 'setInt8' */
  395. #define DUK_STRIDX_GET_FLOAT64 104 /* 'getFloat64' */
  396. #define DUK_STRIDX_GET_FLOAT32 105 /* 'getFloat32' */
  397. #define DUK_STRIDX_GET_UINT32 106 /* 'getUint32' */
  398. #define DUK_STRIDX_GET_INT32 107 /* 'getInt32' */
  399. #define DUK_STRIDX_GET_UINT16 108 /* 'getUint16' */
  400. #define DUK_STRIDX_GET_INT16 109 /* 'getInt16' */
  401. #define DUK_STRIDX_GET_UINT8 110 /* 'getUint8' */
  402. #define DUK_STRIDX_GET_INT8 111 /* 'getInt8' */
  403. #define DUK_STRIDX_SUBARRAY 112 /* 'subarray' */
  404. #define DUK_STRIDX_BYTES_PER_ELEMENT 113 /* 'BYTES_PER_ELEMENT' */
  405. #define DUK_STRIDX_BYTE_OFFSET 114 /* 'byteOffset' */
  406. #define DUK_STRIDX_LC_BUFFER 115 /* 'buffer' */
  407. #define DUK_STRIDX_IS_VIEW 116 /* 'isView' */
  408. #define DUK_STRIDX_DATA 117 /* 'data' */
  409. #define DUK_STRIDX_TYPE 118 /* 'type' */
  410. #define DUK_STRIDX_WRITE_INT_BE 119 /* 'writeIntBE' */
  411. #define DUK_STRIDX_WRITE_INT_LE 120 /* 'writeIntLE' */
  412. #define DUK_STRIDX_WRITE_UINT_BE 121 /* 'writeUIntBE' */
  413. #define DUK_STRIDX_WRITE_UINT_LE 122 /* 'writeUIntLE' */
  414. #define DUK_STRIDX_WRITE_DOUBLE_BE 123 /* 'writeDoubleBE' */
  415. #define DUK_STRIDX_WRITE_DOUBLE_LE 124 /* 'writeDoubleLE' */
  416. #define DUK_STRIDX_WRITE_FLOAT_BE 125 /* 'writeFloatBE' */
  417. #define DUK_STRIDX_WRITE_FLOAT_LE 126 /* 'writeFloatLE' */
  418. #define DUK_STRIDX_WRITE_INT32_BE 127 /* 'writeInt32BE' */
  419. #define DUK_STRIDX_WRITE_INT32_LE 128 /* 'writeInt32LE' */
  420. #define DUK_STRIDX_WRITE_UINT32_BE 129 /* 'writeUInt32BE' */
  421. #define DUK_STRIDX_WRITE_UINT32_LE 130 /* 'writeUInt32LE' */
  422. #define DUK_STRIDX_WRITE_INT16_BE 131 /* 'writeInt16BE' */
  423. #define DUK_STRIDX_WRITE_INT16_LE 132 /* 'writeInt16LE' */
  424. #define DUK_STRIDX_WRITE_UINT16_BE 133 /* 'writeUInt16BE' */
  425. #define DUK_STRIDX_WRITE_UINT16_LE 134 /* 'writeUInt16LE' */
  426. #define DUK_STRIDX_WRITE_INT8 135 /* 'writeInt8' */
  427. #define DUK_STRIDX_WRITE_UINT8 136 /* 'writeUInt8' */
  428. #define DUK_STRIDX_READ_INT_BE 137 /* 'readIntBE' */
  429. #define DUK_STRIDX_READ_INT_LE 138 /* 'readIntLE' */
  430. #define DUK_STRIDX_READ_UINT_BE 139 /* 'readUIntBE' */
  431. #define DUK_STRIDX_READ_UINT_LE 140 /* 'readUIntLE' */
  432. #define DUK_STRIDX_READ_DOUBLE_BE 141 /* 'readDoubleBE' */
  433. #define DUK_STRIDX_READ_DOUBLE_LE 142 /* 'readDoubleLE' */
  434. #define DUK_STRIDX_READ_FLOAT_BE 143 /* 'readFloatBE' */
  435. #define DUK_STRIDX_READ_FLOAT_LE 144 /* 'readFloatLE' */
  436. #define DUK_STRIDX_READ_INT32_BE 145 /* 'readInt32BE' */
  437. #define DUK_STRIDX_READ_INT32_LE 146 /* 'readInt32LE' */
  438. #define DUK_STRIDX_READ_UINT32_BE 147 /* 'readUInt32BE' */
  439. #define DUK_STRIDX_READ_UINT32_LE 148 /* 'readUInt32LE' */
  440. #define DUK_STRIDX_READ_INT16_BE 149 /* 'readInt16BE' */
  441. #define DUK_STRIDX_READ_INT16_LE 150 /* 'readInt16LE' */
  442. #define DUK_STRIDX_READ_UINT16_BE 151 /* 'readUInt16BE' */
  443. #define DUK_STRIDX_READ_UINT16_LE 152 /* 'readUInt16LE' */
  444. #define DUK_STRIDX_READ_INT8 153 /* 'readInt8' */
  445. #define DUK_STRIDX_READ_UINT8 154 /* 'readUInt8' */
  446. #define DUK_STRIDX_COPY 155 /* 'copy' */
  447. #define DUK_STRIDX_EQUALS 156 /* 'equals' */
  448. #define DUK_STRIDX_FILL 157 /* 'fill' */
  449. #define DUK_STRIDX_WRITE 158 /* 'write' */
  450. #define DUK_STRIDX_COMPARE 159 /* 'compare' */
  451. #define DUK_STRIDX_BYTE_LENGTH 160 /* 'byteLength' */
  452. #define DUK_STRIDX_IS_BUFFER 161 /* 'isBuffer' */
  453. #define DUK_STRIDX_IS_ENCODING 162 /* 'isEncoding' */
  454. #define DUK_STRIDX_EXPORTS 163 /* 'exports' */
  455. #define DUK_STRIDX_ID 164 /* 'id' */
  456. #define DUK_STRIDX_REQUIRE 165 /* 'require' */
  457. #define DUK_STRIDX___PROTO__ 166 /* '__proto__' */
  458. #define DUK_STRIDX_SET_PROTOTYPE_OF 167 /* 'setPrototypeOf' */
  459. #define DUK_STRIDX_OWN_KEYS 168 /* 'ownKeys' */
  460. #define DUK_STRIDX_ENUMERATE 169 /* 'enumerate' */
  461. #define DUK_STRIDX_DELETE_PROPERTY 170 /* 'deleteProperty' */
  462. #define DUK_STRIDX_HAS 171 /* 'has' */
  463. #define DUK_STRIDX_PROXY 172 /* 'Proxy' */
  464. #define DUK_STRIDX_CALLEE 173 /* 'callee' */
  465. #define DUK_STRIDX_INVALID_DATE 174 /* 'Invalid Date' */
  466. #define DUK_STRIDX_BRACKETED_ELLIPSIS 175 /* '[...]' */
  467. #define DUK_STRIDX_NEWLINE_TAB 176 /* '\n\t' */
  468. #define DUK_STRIDX_SPACE 177 /* ' ' */
  469. #define DUK_STRIDX_COMMA 178 /* ',' */
  470. #define DUK_STRIDX_MINUS_ZERO 179 /* '-0' */
  471. #define DUK_STRIDX_PLUS_ZERO 180 /* '+0' */
  472. #define DUK_STRIDX_ZERO 181 /* '0' */
  473. #define DUK_STRIDX_MINUS_INFINITY 182 /* '-Infinity' */
  474. #define DUK_STRIDX_PLUS_INFINITY 183 /* '+Infinity' */
  475. #define DUK_STRIDX_INFINITY 184 /* 'Infinity' */
  476. #define DUK_STRIDX_LC_OBJECT 185 /* 'object' */
  477. #define DUK_STRIDX_LC_STRING 186 /* 'string' */
  478. #define DUK_STRIDX_LC_NUMBER 187 /* 'number' */
  479. #define DUK_STRIDX_LC_BOOLEAN 188 /* 'boolean' */
  480. #define DUK_STRIDX_LC_UNDEFINED 189 /* 'undefined' */
  481. #define DUK_STRIDX_STRINGIFY 190 /* 'stringify' */
  482. #define DUK_STRIDX_TAN 191 /* 'tan' */
  483. #define DUK_STRIDX_SQRT 192 /* 'sqrt' */
  484. #define DUK_STRIDX_SIN 193 /* 'sin' */
  485. #define DUK_STRIDX_ROUND 194 /* 'round' */
  486. #define DUK_STRIDX_RANDOM 195 /* 'random' */
  487. #define DUK_STRIDX_POW 196 /* 'pow' */
  488. #define DUK_STRIDX_MIN 197 /* 'min' */
  489. #define DUK_STRIDX_MAX 198 /* 'max' */
  490. #define DUK_STRIDX_LOG 199 /* 'log' */
  491. #define DUK_STRIDX_FLOOR 200 /* 'floor' */
  492. #define DUK_STRIDX_EXP 201 /* 'exp' */
  493. #define DUK_STRIDX_COS 202 /* 'cos' */
  494. #define DUK_STRIDX_CEIL 203 /* 'ceil' */
  495. #define DUK_STRIDX_ATAN2 204 /* 'atan2' */
  496. #define DUK_STRIDX_ATAN 205 /* 'atan' */
  497. #define DUK_STRIDX_ASIN 206 /* 'asin' */
  498. #define DUK_STRIDX_ACOS 207 /* 'acos' */
  499. #define DUK_STRIDX_ABS 208 /* 'abs' */
  500. #define DUK_STRIDX_SQRT2 209 /* 'SQRT2' */
  501. #define DUK_STRIDX_SQRT1_2 210 /* 'SQRT1_2' */
  502. #define DUK_STRIDX_PI 211 /* 'PI' */
  503. #define DUK_STRIDX_LOG10E 212 /* 'LOG10E' */
  504. #define DUK_STRIDX_LOG2E 213 /* 'LOG2E' */
  505. #define DUK_STRIDX_LN2 214 /* 'LN2' */
  506. #define DUK_STRIDX_LN10 215 /* 'LN10' */
  507. #define DUK_STRIDX_E 216 /* 'E' */
  508. #define DUK_STRIDX_MESSAGE 217 /* 'message' */
  509. #define DUK_STRIDX_NAME 218 /* 'name' */
  510. #define DUK_STRIDX_INPUT 219 /* 'input' */
  511. #define DUK_STRIDX_INDEX 220 /* 'index' */
  512. #define DUK_STRIDX_ESCAPED_EMPTY_REGEXP 221 /* '(?:)' */
  513. #define DUK_STRIDX_LAST_INDEX 222 /* 'lastIndex' */
  514. #define DUK_STRIDX_MULTILINE 223 /* 'multiline' */
  515. #define DUK_STRIDX_IGNORE_CASE 224 /* 'ignoreCase' */
  516. #define DUK_STRIDX_SOURCE 225 /* 'source' */
  517. #define DUK_STRIDX_TEST 226 /* 'test' */
  518. #define DUK_STRIDX_EXEC 227 /* 'exec' */
  519. #define DUK_STRIDX_TO_GMT_STRING 228 /* 'toGMTString' */
  520. #define DUK_STRIDX_SET_YEAR 229 /* 'setYear' */
  521. #define DUK_STRIDX_GET_YEAR 230 /* 'getYear' */
  522. #define DUK_STRIDX_TO_JSON 231 /* 'toJSON' */
  523. #define DUK_STRIDX_TO_ISO_STRING 232 /* 'toISOString' */
  524. #define DUK_STRIDX_TO_UTC_STRING 233 /* 'toUTCString' */
  525. #define DUK_STRIDX_SET_UTC_FULL_YEAR 234 /* 'setUTCFullYear' */
  526. #define DUK_STRIDX_SET_FULL_YEAR 235 /* 'setFullYear' */
  527. #define DUK_STRIDX_SET_UTC_MONTH 236 /* 'setUTCMonth' */
  528. #define DUK_STRIDX_SET_MONTH 237 /* 'setMonth' */
  529. #define DUK_STRIDX_SET_UTC_DATE 238 /* 'setUTCDate' */
  530. #define DUK_STRIDX_SET_DATE 239 /* 'setDate' */
  531. #define DUK_STRIDX_SET_UTC_HOURS 240 /* 'setUTCHours' */
  532. #define DUK_STRIDX_SET_HOURS 241 /* 'setHours' */
  533. #define DUK_STRIDX_SET_UTC_MINUTES 242 /* 'setUTCMinutes' */
  534. #define DUK_STRIDX_SET_MINUTES 243 /* 'setMinutes' */
  535. #define DUK_STRIDX_SET_UTC_SECONDS 244 /* 'setUTCSeconds' */
  536. #define DUK_STRIDX_SET_SECONDS 245 /* 'setSeconds' */
  537. #define DUK_STRIDX_SET_UTC_MILLISECONDS 246 /* 'setUTCMilliseconds' */
  538. #define DUK_STRIDX_SET_MILLISECONDS 247 /* 'setMilliseconds' */
  539. #define DUK_STRIDX_SET_TIME 248 /* 'setTime' */
  540. #define DUK_STRIDX_GET_TIMEZONE_OFFSET 249 /* 'getTimezoneOffset' */
  541. #define DUK_STRIDX_GET_UTC_MILLISECONDS 250 /* 'getUTCMilliseconds' */
  542. #define DUK_STRIDX_GET_MILLISECONDS 251 /* 'getMilliseconds' */
  543. #define DUK_STRIDX_GET_UTC_SECONDS 252 /* 'getUTCSeconds' */
  544. #define DUK_STRIDX_GET_SECONDS 253 /* 'getSeconds' */
  545. #define DUK_STRIDX_GET_UTC_MINUTES 254 /* 'getUTCMinutes' */
  546. #define DUK_STRIDX_GET_MINUTES 255 /* 'getMinutes' */
  547. #define DUK_STRIDX_GET_UTC_HOURS 256 /* 'getUTCHours' */
  548. #define DUK_STRIDX_GET_HOURS 257 /* 'getHours' */
  549. #define DUK_STRIDX_GET_UTC_DAY 258 /* 'getUTCDay' */
  550. #define DUK_STRIDX_GET_DAY 259 /* 'getDay' */
  551. #define DUK_STRIDX_GET_UTC_DATE 260 /* 'getUTCDate' */
  552. #define DUK_STRIDX_GET_DATE 261 /* 'getDate' */
  553. #define DUK_STRIDX_GET_UTC_MONTH 262 /* 'getUTCMonth' */
  554. #define DUK_STRIDX_GET_MONTH 263 /* 'getMonth' */
  555. #define DUK_STRIDX_GET_UTC_FULL_YEAR 264 /* 'getUTCFullYear' */
  556. #define DUK_STRIDX_GET_FULL_YEAR 265 /* 'getFullYear' */
  557. #define DUK_STRIDX_GET_TIME 266 /* 'getTime' */
  558. #define DUK_STRIDX_TO_LOCALE_TIME_STRING 267 /* 'toLocaleTimeString' */
  559. #define DUK_STRIDX_TO_LOCALE_DATE_STRING 268 /* 'toLocaleDateString' */
  560. #define DUK_STRIDX_TO_TIME_STRING 269 /* 'toTimeString' */
  561. #define DUK_STRIDX_TO_DATE_STRING 270 /* 'toDateString' */
  562. #define DUK_STRIDX_NOW 271 /* 'now' */
  563. #define DUK_STRIDX_UTC 272 /* 'UTC' */
  564. #define DUK_STRIDX_PARSE 273 /* 'parse' */
  565. #define DUK_STRIDX_TO_PRECISION 274 /* 'toPrecision' */
  566. #define DUK_STRIDX_TO_EXPONENTIAL 275 /* 'toExponential' */
  567. #define DUK_STRIDX_TO_FIXED 276 /* 'toFixed' */
  568. #define DUK_STRIDX_POSITIVE_INFINITY 277 /* 'POSITIVE_INFINITY' */
  569. #define DUK_STRIDX_NEGATIVE_INFINITY 278 /* 'NEGATIVE_INFINITY' */
  570. #define DUK_STRIDX_NAN 279 /* 'NaN' */
  571. #define DUK_STRIDX_MIN_VALUE 280 /* 'MIN_VALUE' */
  572. #define DUK_STRIDX_MAX_VALUE 281 /* 'MAX_VALUE' */
  573. #define DUK_STRIDX_SUBSTR 282 /* 'substr' */
  574. #define DUK_STRIDX_TRIM 283 /* 'trim' */
  575. #define DUK_STRIDX_TO_LOCALE_UPPER_CASE 284 /* 'toLocaleUpperCase' */
  576. #define DUK_STRIDX_TO_UPPER_CASE 285 /* 'toUpperCase' */
  577. #define DUK_STRIDX_TO_LOCALE_LOWER_CASE 286 /* 'toLocaleLowerCase' */
  578. #define DUK_STRIDX_TO_LOWER_CASE 287 /* 'toLowerCase' */
  579. #define DUK_STRIDX_SUBSTRING 288 /* 'substring' */
  580. #define DUK_STRIDX_SPLIT 289 /* 'split' */
  581. #define DUK_STRIDX_SEARCH 290 /* 'search' */
  582. #define DUK_STRIDX_REPLACE 291 /* 'replace' */
  583. #define DUK_STRIDX_MATCH 292 /* 'match' */
  584. #define DUK_STRIDX_LOCALE_COMPARE 293 /* 'localeCompare' */
  585. #define DUK_STRIDX_CHAR_CODE_AT 294 /* 'charCodeAt' */
  586. #define DUK_STRIDX_CHAR_AT 295 /* 'charAt' */
  587. #define DUK_STRIDX_FROM_CHAR_CODE 296 /* 'fromCharCode' */
  588. #define DUK_STRIDX_REDUCE_RIGHT 297 /* 'reduceRight' */
  589. #define DUK_STRIDX_REDUCE 298 /* 'reduce' */
  590. #define DUK_STRIDX_FILTER 299 /* 'filter' */
  591. #define DUK_STRIDX_MAP 300 /* 'map' */
  592. #define DUK_STRIDX_FOR_EACH 301 /* 'forEach' */
  593. #define DUK_STRIDX_SOME 302 /* 'some' */
  594. #define DUK_STRIDX_EVERY 303 /* 'every' */
  595. #define DUK_STRIDX_LAST_INDEX_OF 304 /* 'lastIndexOf' */
  596. #define DUK_STRIDX_INDEX_OF 305 /* 'indexOf' */
  597. #define DUK_STRIDX_UNSHIFT 306 /* 'unshift' */
  598. #define DUK_STRIDX_SPLICE 307 /* 'splice' */
  599. #define DUK_STRIDX_SORT 308 /* 'sort' */
  600. #define DUK_STRIDX_SLICE 309 /* 'slice' */
  601. #define DUK_STRIDX_SHIFT 310 /* 'shift' */
  602. #define DUK_STRIDX_REVERSE 311 /* 'reverse' */
  603. #define DUK_STRIDX_PUSH 312 /* 'push' */
  604. #define DUK_STRIDX_POP 313 /* 'pop' */
  605. #define DUK_STRIDX_JOIN 314 /* 'join' */
  606. #define DUK_STRIDX_CONCAT 315 /* 'concat' */
  607. #define DUK_STRIDX_IS_ARRAY 316 /* 'isArray' */
  608. #define DUK_STRIDX_LC_ARGUMENTS 317 /* 'arguments' */
  609. #define DUK_STRIDX_CALLER 318 /* 'caller' */
  610. #define DUK_STRIDX_BIND 319 /* 'bind' */
  611. #define DUK_STRIDX_CALL 320 /* 'call' */
  612. #define DUK_STRIDX_APPLY 321 /* 'apply' */
  613. #define DUK_STRIDX_PROPERTY_IS_ENUMERABLE 322 /* 'propertyIsEnumerable' */
  614. #define DUK_STRIDX_IS_PROTOTYPE_OF 323 /* 'isPrototypeOf' */
  615. #define DUK_STRIDX_HAS_OWN_PROPERTY 324 /* 'hasOwnProperty' */
  616. #define DUK_STRIDX_VALUE_OF 325 /* 'valueOf' */
  617. #define DUK_STRIDX_TO_LOCALE_STRING 326 /* 'toLocaleString' */
  618. #define DUK_STRIDX_TO_STRING 327 /* 'toString' */
  619. #define DUK_STRIDX_CONSTRUCTOR 328 /* 'constructor' */
  620. #define DUK_STRIDX_SET 329 /* 'set' */
  621. #define DUK_STRIDX_GET 330 /* 'get' */
  622. #define DUK_STRIDX_ENUMERABLE 331 /* 'enumerable' */
  623. #define DUK_STRIDX_CONFIGURABLE 332 /* 'configurable' */
  624. #define DUK_STRIDX_WRITABLE 333 /* 'writable' */
  625. #define DUK_STRIDX_VALUE 334 /* 'value' */
  626. #define DUK_STRIDX_KEYS 335 /* 'keys' */
  627. #define DUK_STRIDX_IS_EXTENSIBLE 336 /* 'isExtensible' */
  628. #define DUK_STRIDX_IS_FROZEN 337 /* 'isFrozen' */
  629. #define DUK_STRIDX_IS_SEALED 338 /* 'isSealed' */
  630. #define DUK_STRIDX_PREVENT_EXTENSIONS 339 /* 'preventExtensions' */
  631. #define DUK_STRIDX_FREEZE 340 /* 'freeze' */
  632. #define DUK_STRIDX_SEAL 341 /* 'seal' */
  633. #define DUK_STRIDX_DEFINE_PROPERTIES 342 /* 'defineProperties' */
  634. #define DUK_STRIDX_DEFINE_PROPERTY 343 /* 'defineProperty' */
  635. #define DUK_STRIDX_CREATE 344 /* 'create' */
  636. #define DUK_STRIDX_GET_OWN_PROPERTY_NAMES 345 /* 'getOwnPropertyNames' */
  637. #define DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR 346 /* 'getOwnPropertyDescriptor' */
  638. #define DUK_STRIDX_GET_PROTOTYPE_OF 347 /* 'getPrototypeOf' */
  639. #define DUK_STRIDX_PROTOTYPE 348 /* 'prototype' */
  640. #define DUK_STRIDX_LENGTH 349 /* 'length' */
  641. #define DUK_STRIDX_ALERT 350 /* 'alert' */
  642. #define DUK_STRIDX_PRINT 351 /* 'print' */
  643. #define DUK_STRIDX_UNESCAPE 352 /* 'unescape' */
  644. #define DUK_STRIDX_ESCAPE 353 /* 'escape' */
  645. #define DUK_STRIDX_ENCODE_URI_COMPONENT 354 /* 'encodeURIComponent' */
  646. #define DUK_STRIDX_ENCODE_URI 355 /* 'encodeURI' */
  647. #define DUK_STRIDX_DECODE_URI_COMPONENT 356 /* 'decodeURIComponent' */
  648. #define DUK_STRIDX_DECODE_URI 357 /* 'decodeURI' */
  649. #define DUK_STRIDX_IS_FINITE 358 /* 'isFinite' */
  650. #define DUK_STRIDX_IS_NAN 359 /* 'isNaN' */
  651. #define DUK_STRIDX_PARSE_FLOAT 360 /* 'parseFloat' */
  652. #define DUK_STRIDX_PARSE_INT 361 /* 'parseInt' */
  653. #define DUK_STRIDX_EVAL 362 /* 'eval' */
  654. #define DUK_STRIDX_URI_ERROR 363 /* 'URIError' */
  655. #define DUK_STRIDX_TYPE_ERROR 364 /* 'TypeError' */
  656. #define DUK_STRIDX_SYNTAX_ERROR 365 /* 'SyntaxError' */
  657. #define DUK_STRIDX_REFERENCE_ERROR 366 /* 'ReferenceError' */
  658. #define DUK_STRIDX_RANGE_ERROR 367 /* 'RangeError' */
  659. #define DUK_STRIDX_EVAL_ERROR 368 /* 'EvalError' */
  660. #define DUK_STRIDX_BREAK 369 /* 'break' */
  661. #define DUK_STRIDX_CASE 370 /* 'case' */
  662. #define DUK_STRIDX_CATCH 371 /* 'catch' */
  663. #define DUK_STRIDX_CONTINUE 372 /* 'continue' */
  664. #define DUK_STRIDX_DEBUGGER 373 /* 'debugger' */
  665. #define DUK_STRIDX_DEFAULT 374 /* 'default' */
  666. #define DUK_STRIDX_DELETE 375 /* 'delete' */
  667. #define DUK_STRIDX_DO 376 /* 'do' */
  668. #define DUK_STRIDX_ELSE 377 /* 'else' */
  669. #define DUK_STRIDX_FINALLY 378 /* 'finally' */
  670. #define DUK_STRIDX_FOR 379 /* 'for' */
  671. #define DUK_STRIDX_LC_FUNCTION 380 /* 'function' */
  672. #define DUK_STRIDX_IF 381 /* 'if' */
  673. #define DUK_STRIDX_IN 382 /* 'in' */
  674. #define DUK_STRIDX_INSTANCEOF 383 /* 'instanceof' */
  675. #define DUK_STRIDX_NEW 384 /* 'new' */
  676. #define DUK_STRIDX_RETURN 385 /* 'return' */
  677. #define DUK_STRIDX_SWITCH 386 /* 'switch' */
  678. #define DUK_STRIDX_THIS 387 /* 'this' */
  679. #define DUK_STRIDX_THROW 388 /* 'throw' */
  680. #define DUK_STRIDX_TRY 389 /* 'try' */
  681. #define DUK_STRIDX_TYPEOF 390 /* 'typeof' */
  682. #define DUK_STRIDX_VAR 391 /* 'var' */
  683. #define DUK_STRIDX_CONST 392 /* 'const' */
  684. #define DUK_STRIDX_VOID 393 /* 'void' */
  685. #define DUK_STRIDX_WHILE 394 /* 'while' */
  686. #define DUK_STRIDX_WITH 395 /* 'with' */
  687. #define DUK_STRIDX_CLASS 396 /* 'class' */
  688. #define DUK_STRIDX_ENUM 397 /* 'enum' */
  689. #define DUK_STRIDX_EXPORT 398 /* 'export' */
  690. #define DUK_STRIDX_EXTENDS 399 /* 'extends' */
  691. #define DUK_STRIDX_IMPORT 400 /* 'import' */
  692. #define DUK_STRIDX_SUPER 401 /* 'super' */
  693. #define DUK_STRIDX_LC_NULL 402 /* 'null' */
  694. #define DUK_STRIDX_TRUE 403 /* 'true' */
  695. #define DUK_STRIDX_FALSE 404 /* 'false' */
  696. #define DUK_STRIDX_IMPLEMENTS 405 /* 'implements' */
  697. #define DUK_STRIDX_INTERFACE 406 /* 'interface' */
  698. #define DUK_STRIDX_LET 407 /* 'let' */
  699. #define DUK_STRIDX_PACKAGE 408 /* 'package' */
  700. #define DUK_STRIDX_PRIVATE 409 /* 'private' */
  701. #define DUK_STRIDX_PROTECTED 410 /* 'protected' */
  702. #define DUK_STRIDX_PUBLIC 411 /* 'public' */
  703. #define DUK_STRIDX_STATIC 412 /* 'static' */
  704. #define DUK_STRIDX_YIELD 413 /* 'yield' */
  705. #define DUK_HEAP_STRING_UC_LOGGER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_LOGGER)
  706. #define DUK_HTHREAD_STRING_UC_LOGGER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_LOGGER)
  707. #define DUK_HEAP_STRING_UC_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_THREAD)
  708. #define DUK_HTHREAD_STRING_UC_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_THREAD)
  709. #define DUK_HEAP_STRING_UC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_POINTER)
  710. #define DUK_HTHREAD_STRING_UC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_POINTER)
  711. #define DUK_HEAP_STRING_DEC_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEC_ENV)
  712. #define DUK_HTHREAD_STRING_DEC_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEC_ENV)
  713. #define DUK_HEAP_STRING_OBJ_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OBJ_ENV)
  714. #define DUK_HTHREAD_STRING_OBJ_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OBJ_ENV)
  715. #define DUK_HEAP_STRING_FLOAT64_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT64_ARRAY)
  716. #define DUK_HTHREAD_STRING_FLOAT64_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT64_ARRAY)
  717. #define DUK_HEAP_STRING_FLOAT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT32_ARRAY)
  718. #define DUK_HTHREAD_STRING_FLOAT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT32_ARRAY)
  719. #define DUK_HEAP_STRING_UINT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT32_ARRAY)
  720. #define DUK_HTHREAD_STRING_UINT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT32_ARRAY)
  721. #define DUK_HEAP_STRING_INT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT32_ARRAY)
  722. #define DUK_HTHREAD_STRING_INT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT32_ARRAY)
  723. #define DUK_HEAP_STRING_UINT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT16_ARRAY)
  724. #define DUK_HTHREAD_STRING_UINT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT16_ARRAY)
  725. #define DUK_HEAP_STRING_INT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT16_ARRAY)
  726. #define DUK_HTHREAD_STRING_INT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT16_ARRAY)
  727. #define DUK_HEAP_STRING_UINT8_CLAMPED_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_CLAMPED_ARRAY)
  728. #define DUK_HTHREAD_STRING_UINT8_CLAMPED_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_CLAMPED_ARRAY)
  729. #define DUK_HEAP_STRING_UINT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_ARRAY)
  730. #define DUK_HTHREAD_STRING_UINT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_ARRAY)
  731. #define DUK_HEAP_STRING_INT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT8_ARRAY)
  732. #define DUK_HTHREAD_STRING_INT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT8_ARRAY)
  733. #define DUK_HEAP_STRING_DATA_VIEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA_VIEW)
  734. #define DUK_HTHREAD_STRING_DATA_VIEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA_VIEW)
  735. #define DUK_HEAP_STRING_ARRAY_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY_BUFFER)
  736. #define DUK_HTHREAD_STRING_ARRAY_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY_BUFFER)
  737. #define DUK_HEAP_STRING_UC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BUFFER)
  738. #define DUK_HTHREAD_STRING_UC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BUFFER)
  739. #define DUK_HEAP_STRING_EMPTY_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EMPTY_STRING)
  740. #define DUK_HTHREAD_STRING_EMPTY_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EMPTY_STRING)
  741. #define DUK_HEAP_STRING_GLOBAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GLOBAL)
  742. #define DUK_HTHREAD_STRING_GLOBAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GLOBAL)
  743. #define DUK_HEAP_STRING_UC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ARGUMENTS)
  744. #define DUK_HTHREAD_STRING_UC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ARGUMENTS)
  745. #define DUK_HEAP_STRING_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON)
  746. #define DUK_HTHREAD_STRING_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON)
  747. #define DUK_HEAP_STRING_MATH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MATH)
  748. #define DUK_HTHREAD_STRING_MATH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MATH)
  749. #define DUK_HEAP_STRING_UC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ERROR)
  750. #define DUK_HTHREAD_STRING_UC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ERROR)
  751. #define DUK_HEAP_STRING_REG_EXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REG_EXP)
  752. #define DUK_HTHREAD_STRING_REG_EXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REG_EXP)
  753. #define DUK_HEAP_STRING_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATE)
  754. #define DUK_HTHREAD_STRING_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATE)
  755. #define DUK_HEAP_STRING_UC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NUMBER)
  756. #define DUK_HTHREAD_STRING_UC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NUMBER)
  757. #define DUK_HEAP_STRING_UC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BOOLEAN)
  758. #define DUK_HTHREAD_STRING_UC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BOOLEAN)
  759. #define DUK_HEAP_STRING_UC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_STRING)
  760. #define DUK_HTHREAD_STRING_UC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_STRING)
  761. #define DUK_HEAP_STRING_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY)
  762. #define DUK_HTHREAD_STRING_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY)
  763. #define DUK_HEAP_STRING_UC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_FUNCTION)
  764. #define DUK_HTHREAD_STRING_UC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_FUNCTION)
  765. #define DUK_HEAP_STRING_UC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_OBJECT)
  766. #define DUK_HTHREAD_STRING_UC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_OBJECT)
  767. #define DUK_HEAP_STRING_UC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NULL)
  768. #define DUK_HTHREAD_STRING_UC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NULL)
  769. #define DUK_HEAP_STRING_UC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_UNDEFINED)
  770. #define DUK_HTHREAD_STRING_UC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_UNDEFINED)
  771. #define DUK_HEAP_STRING_JSON_EXT_FUNCTION2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION2)
  772. #define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION2)
  773. #define DUK_HEAP_STRING_JSON_EXT_FUNCTION1(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION1)
  774. #define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION1(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION1)
  775. #define DUK_HEAP_STRING_JSON_EXT_NEGINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NEGINF)
  776. #define DUK_HTHREAD_STRING_JSON_EXT_NEGINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NEGINF)
  777. #define DUK_HEAP_STRING_JSON_EXT_POSINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_POSINF)
  778. #define DUK_HTHREAD_STRING_JSON_EXT_POSINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_POSINF)
  779. #define DUK_HEAP_STRING_JSON_EXT_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NAN)
  780. #define DUK_HTHREAD_STRING_JSON_EXT_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NAN)
  781. #define DUK_HEAP_STRING_JSON_EXT_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_UNDEFINED)
  782. #define DUK_HTHREAD_STRING_JSON_EXT_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_UNDEFINED)
  783. #define DUK_HEAP_STRING_TO_LOG_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOG_STRING)
  784. #define DUK_HTHREAD_STRING_TO_LOG_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOG_STRING)
  785. #define DUK_HEAP_STRING_CLOG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLOG)
  786. #define DUK_HTHREAD_STRING_CLOG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLOG)
  787. #define DUK_HEAP_STRING_LC_L(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_L)
  788. #define DUK_HTHREAD_STRING_LC_L(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_L)
  789. #define DUK_HEAP_STRING_LC_N(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_N)
  790. #define DUK_HTHREAD_STRING_LC_N(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_N)
  791. #define DUK_HEAP_STRING_LC_FATAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FATAL)
  792. #define DUK_HTHREAD_STRING_LC_FATAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FATAL)
  793. #define DUK_HEAP_STRING_LC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ERROR)
  794. #define DUK_HTHREAD_STRING_LC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ERROR)
  795. #define DUK_HEAP_STRING_LC_WARN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_WARN)
  796. #define DUK_HTHREAD_STRING_LC_WARN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_WARN)
  797. #define DUK_HEAP_STRING_LC_DEBUG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_DEBUG)
  798. #define DUK_HTHREAD_STRING_LC_DEBUG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_DEBUG)
  799. #define DUK_HEAP_STRING_LC_TRACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_TRACE)
  800. #define DUK_HTHREAD_STRING_LC_TRACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_TRACE)
  801. #define DUK_HEAP_STRING_RAW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RAW)
  802. #define DUK_HTHREAD_STRING_RAW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RAW)
  803. #define DUK_HEAP_STRING_FMT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FMT)
  804. #define DUK_HTHREAD_STRING_FMT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FMT)
  805. #define DUK_HEAP_STRING_CURRENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CURRENT)
  806. #define DUK_HTHREAD_STRING_CURRENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CURRENT)
  807. #define DUK_HEAP_STRING_RESUME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RESUME)
  808. #define DUK_HTHREAD_STRING_RESUME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RESUME)
  809. #define DUK_HEAP_STRING_COMPACT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPACT)
  810. #define DUK_HTHREAD_STRING_COMPACT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPACT)
  811. #define DUK_HEAP_STRING_JC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JC)
  812. #define DUK_HTHREAD_STRING_JC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JC)
  813. #define DUK_HEAP_STRING_JX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JX)
  814. #define DUK_HTHREAD_STRING_JX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JX)
  815. #define DUK_HEAP_STRING_BASE64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BASE64)
  816. #define DUK_HTHREAD_STRING_BASE64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BASE64)
  817. #define DUK_HEAP_STRING_HEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HEX)
  818. #define DUK_HTHREAD_STRING_HEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HEX)
  819. #define DUK_HEAP_STRING_DEC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEC)
  820. #define DUK_HTHREAD_STRING_DEC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEC)
  821. #define DUK_HEAP_STRING_ENC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENC)
  822. #define DUK_HTHREAD_STRING_ENC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENC)
  823. #define DUK_HEAP_STRING_FIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FIN)
  824. #define DUK_HTHREAD_STRING_FIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FIN)
  825. #define DUK_HEAP_STRING_GC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GC)
  826. #define DUK_HTHREAD_STRING_GC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GC)
  827. #define DUK_HEAP_STRING_ACT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ACT)
  828. #define DUK_HTHREAD_STRING_ACT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ACT)
  829. #define DUK_HEAP_STRING_LC_INFO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_INFO)
  830. #define DUK_HTHREAD_STRING_LC_INFO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_INFO)
  831. #define DUK_HEAP_STRING_VERSION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VERSION)
  832. #define DUK_HTHREAD_STRING_VERSION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VERSION)
  833. #define DUK_HEAP_STRING_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENV)
  834. #define DUK_HTHREAD_STRING_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENV)
  835. #define DUK_HEAP_STRING_MOD_LOADED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MOD_LOADED)
  836. #define DUK_HTHREAD_STRING_MOD_LOADED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MOD_LOADED)
  837. #define DUK_HEAP_STRING_MOD_SEARCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MOD_SEARCH)
  838. #define DUK_HTHREAD_STRING_MOD_SEARCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MOD_SEARCH)
  839. #define DUK_HEAP_STRING_ERR_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_THROW)
  840. #define DUK_HTHREAD_STRING_ERR_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_THROW)
  841. #define DUK_HEAP_STRING_ERR_CREATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_CREATE)
  842. #define DUK_HTHREAD_STRING_ERR_CREATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_CREATE)
  843. #define DUK_HEAP_STRING_COMPILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPILE)
  844. #define DUK_HTHREAD_STRING_COMPILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPILE)
  845. #define DUK_HEAP_STRING_INT_REGBASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_REGBASE)
  846. #define DUK_HTHREAD_STRING_INT_REGBASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_REGBASE)
  847. #define DUK_HEAP_STRING_INT_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_THREAD)
  848. #define DUK_HTHREAD_STRING_INT_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_THREAD)
  849. #define DUK_HEAP_STRING_INT_HANDLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_HANDLER)
  850. #define DUK_HTHREAD_STRING_INT_HANDLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_HANDLER)
  851. #define DUK_HEAP_STRING_INT_FINALIZER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FINALIZER)
  852. #define DUK_HTHREAD_STRING_INT_FINALIZER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FINALIZER)
  853. #define DUK_HEAP_STRING_INT_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_CALLEE)
  854. #define DUK_HTHREAD_STRING_INT_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_CALLEE)
  855. #define DUK_HEAP_STRING_INT_MAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_MAP)
  856. #define DUK_HTHREAD_STRING_INT_MAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_MAP)
  857. #define DUK_HEAP_STRING_INT_ARGS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_ARGS)
  858. #define DUK_HTHREAD_STRING_INT_ARGS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_ARGS)
  859. #define DUK_HEAP_STRING_INT_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_THIS)
  860. #define DUK_HTHREAD_STRING_INT_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_THIS)
  861. #define DUK_HEAP_STRING_INT_PC2LINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_PC2LINE)
  862. #define DUK_HTHREAD_STRING_INT_PC2LINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_PC2LINE)
  863. #define DUK_HEAP_STRING_INT_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_SOURCE)
  864. #define DUK_HTHREAD_STRING_INT_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_SOURCE)
  865. #define DUK_HEAP_STRING_INT_VARENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARENV)
  866. #define DUK_HTHREAD_STRING_INT_VARENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARENV)
  867. #define DUK_HEAP_STRING_INT_LEXENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_LEXENV)
  868. #define DUK_HTHREAD_STRING_INT_LEXENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_LEXENV)
  869. #define DUK_HEAP_STRING_INT_VARMAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARMAP)
  870. #define DUK_HTHREAD_STRING_INT_VARMAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARMAP)
  871. #define DUK_HEAP_STRING_INT_FORMALS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FORMALS)
  872. #define DUK_HTHREAD_STRING_INT_FORMALS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FORMALS)
  873. #define DUK_HEAP_STRING_INT_BYTECODE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_BYTECODE)
  874. #define DUK_HTHREAD_STRING_INT_BYTECODE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_BYTECODE)
  875. #define DUK_HEAP_STRING_INT_NEXT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_NEXT)
  876. #define DUK_HTHREAD_STRING_INT_NEXT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_NEXT)
  877. #define DUK_HEAP_STRING_INT_TARGET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TARGET)
  878. #define DUK_HTHREAD_STRING_INT_TARGET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TARGET)
  879. #define DUK_HEAP_STRING_INT_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VALUE)
  880. #define DUK_HTHREAD_STRING_INT_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VALUE)
  881. #define DUK_HEAP_STRING_LC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_POINTER)
  882. #define DUK_HTHREAD_STRING_LC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_POINTER)
  883. #define DUK_HEAP_STRING_INT_TRACEDATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TRACEDATA)
  884. #define DUK_HTHREAD_STRING_INT_TRACEDATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TRACEDATA)
  885. #define DUK_HEAP_STRING_LINE_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LINE_NUMBER)
  886. #define DUK_HTHREAD_STRING_LINE_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LINE_NUMBER)
  887. #define DUK_HEAP_STRING_FILE_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILE_NAME)
  888. #define DUK_HTHREAD_STRING_FILE_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILE_NAME)
  889. #define DUK_HEAP_STRING_PC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PC)
  890. #define DUK_HTHREAD_STRING_PC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PC)
  891. #define DUK_HEAP_STRING_STACK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STACK)
  892. #define DUK_HTHREAD_STRING_STACK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STACK)
  893. #define DUK_HEAP_STRING_THROW_TYPE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THROW_TYPE_ERROR)
  894. #define DUK_HTHREAD_STRING_THROW_TYPE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THROW_TYPE_ERROR)
  895. #define DUK_HEAP_STRING_DUKTAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DUKTAPE)
  896. #define DUK_HTHREAD_STRING_DUKTAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DUKTAPE)
  897. #define DUK_HEAP_STRING_SET_FLOAT64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FLOAT64)
  898. #define DUK_HTHREAD_STRING_SET_FLOAT64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FLOAT64)
  899. #define DUK_HEAP_STRING_SET_FLOAT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FLOAT32)
  900. #define DUK_HTHREAD_STRING_SET_FLOAT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FLOAT32)
  901. #define DUK_HEAP_STRING_SET_UINT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT32)
  902. #define DUK_HTHREAD_STRING_SET_UINT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT32)
  903. #define DUK_HEAP_STRING_SET_INT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT32)
  904. #define DUK_HTHREAD_STRING_SET_INT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT32)
  905. #define DUK_HEAP_STRING_SET_UINT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT16)
  906. #define DUK_HTHREAD_STRING_SET_UINT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT16)
  907. #define DUK_HEAP_STRING_SET_INT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT16)
  908. #define DUK_HTHREAD_STRING_SET_INT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT16)
  909. #define DUK_HEAP_STRING_SET_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT8)
  910. #define DUK_HTHREAD_STRING_SET_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT8)
  911. #define DUK_HEAP_STRING_SET_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT8)
  912. #define DUK_HTHREAD_STRING_SET_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT8)
  913. #define DUK_HEAP_STRING_GET_FLOAT64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FLOAT64)
  914. #define DUK_HTHREAD_STRING_GET_FLOAT64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FLOAT64)
  915. #define DUK_HEAP_STRING_GET_FLOAT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FLOAT32)
  916. #define DUK_HTHREAD_STRING_GET_FLOAT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FLOAT32)
  917. #define DUK_HEAP_STRING_GET_UINT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT32)
  918. #define DUK_HTHREAD_STRING_GET_UINT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT32)
  919. #define DUK_HEAP_STRING_GET_INT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT32)
  920. #define DUK_HTHREAD_STRING_GET_INT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT32)
  921. #define DUK_HEAP_STRING_GET_UINT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT16)
  922. #define DUK_HTHREAD_STRING_GET_UINT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT16)
  923. #define DUK_HEAP_STRING_GET_INT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT16)
  924. #define DUK_HTHREAD_STRING_GET_INT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT16)
  925. #define DUK_HEAP_STRING_GET_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT8)
  926. #define DUK_HTHREAD_STRING_GET_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT8)
  927. #define DUK_HEAP_STRING_GET_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT8)
  928. #define DUK_HTHREAD_STRING_GET_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT8)
  929. #define DUK_HEAP_STRING_SUBARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBARRAY)
  930. #define DUK_HTHREAD_STRING_SUBARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBARRAY)
  931. #define DUK_HEAP_STRING_BYTES_PER_ELEMENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTES_PER_ELEMENT)
  932. #define DUK_HTHREAD_STRING_BYTES_PER_ELEMENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTES_PER_ELEMENT)
  933. #define DUK_HEAP_STRING_BYTE_OFFSET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTE_OFFSET)
  934. #define DUK_HTHREAD_STRING_BYTE_OFFSET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTE_OFFSET)
  935. #define DUK_HEAP_STRING_LC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BUFFER)
  936. #define DUK_HTHREAD_STRING_LC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BUFFER)
  937. #define DUK_HEAP_STRING_IS_VIEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_VIEW)
  938. #define DUK_HTHREAD_STRING_IS_VIEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_VIEW)
  939. #define DUK_HEAP_STRING_DATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA)
  940. #define DUK_HTHREAD_STRING_DATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA)
  941. #define DUK_HEAP_STRING_TYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPE)
  942. #define DUK_HTHREAD_STRING_TYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPE)
  943. #define DUK_HEAP_STRING_WRITE_INT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT_BE)
  944. #define DUK_HTHREAD_STRING_WRITE_INT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT_BE)
  945. #define DUK_HEAP_STRING_WRITE_INT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT_LE)
  946. #define DUK_HTHREAD_STRING_WRITE_INT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT_LE)
  947. #define DUK_HEAP_STRING_WRITE_UINT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT_BE)
  948. #define DUK_HTHREAD_STRING_WRITE_UINT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT_BE)
  949. #define DUK_HEAP_STRING_WRITE_UINT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT_LE)
  950. #define DUK_HTHREAD_STRING_WRITE_UINT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT_LE)
  951. #define DUK_HEAP_STRING_WRITE_DOUBLE_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_DOUBLE_BE)
  952. #define DUK_HTHREAD_STRING_WRITE_DOUBLE_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_DOUBLE_BE)
  953. #define DUK_HEAP_STRING_WRITE_DOUBLE_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_DOUBLE_LE)
  954. #define DUK_HTHREAD_STRING_WRITE_DOUBLE_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_DOUBLE_LE)
  955. #define DUK_HEAP_STRING_WRITE_FLOAT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_FLOAT_BE)
  956. #define DUK_HTHREAD_STRING_WRITE_FLOAT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_FLOAT_BE)
  957. #define DUK_HEAP_STRING_WRITE_FLOAT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_FLOAT_LE)
  958. #define DUK_HTHREAD_STRING_WRITE_FLOAT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_FLOAT_LE)
  959. #define DUK_HEAP_STRING_WRITE_INT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT32_BE)
  960. #define DUK_HTHREAD_STRING_WRITE_INT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT32_BE)
  961. #define DUK_HEAP_STRING_WRITE_INT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT32_LE)
  962. #define DUK_HTHREAD_STRING_WRITE_INT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT32_LE)
  963. #define DUK_HEAP_STRING_WRITE_UINT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT32_BE)
  964. #define DUK_HTHREAD_STRING_WRITE_UINT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT32_BE)
  965. #define DUK_HEAP_STRING_WRITE_UINT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT32_LE)
  966. #define DUK_HTHREAD_STRING_WRITE_UINT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT32_LE)
  967. #define DUK_HEAP_STRING_WRITE_INT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT16_BE)
  968. #define DUK_HTHREAD_STRING_WRITE_INT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT16_BE)
  969. #define DUK_HEAP_STRING_WRITE_INT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT16_LE)
  970. #define DUK_HTHREAD_STRING_WRITE_INT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT16_LE)
  971. #define DUK_HEAP_STRING_WRITE_UINT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT16_BE)
  972. #define DUK_HTHREAD_STRING_WRITE_UINT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT16_BE)
  973. #define DUK_HEAP_STRING_WRITE_UINT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT16_LE)
  974. #define DUK_HTHREAD_STRING_WRITE_UINT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT16_LE)
  975. #define DUK_HEAP_STRING_WRITE_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT8)
  976. #define DUK_HTHREAD_STRING_WRITE_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT8)
  977. #define DUK_HEAP_STRING_WRITE_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT8)
  978. #define DUK_HTHREAD_STRING_WRITE_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT8)
  979. #define DUK_HEAP_STRING_READ_INT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT_BE)
  980. #define DUK_HTHREAD_STRING_READ_INT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT_BE)
  981. #define DUK_HEAP_STRING_READ_INT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT_LE)
  982. #define DUK_HTHREAD_STRING_READ_INT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT_LE)
  983. #define DUK_HEAP_STRING_READ_UINT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT_BE)
  984. #define DUK_HTHREAD_STRING_READ_UINT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT_BE)
  985. #define DUK_HEAP_STRING_READ_UINT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT_LE)
  986. #define DUK_HTHREAD_STRING_READ_UINT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT_LE)
  987. #define DUK_HEAP_STRING_READ_DOUBLE_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_DOUBLE_BE)
  988. #define DUK_HTHREAD_STRING_READ_DOUBLE_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_DOUBLE_BE)
  989. #define DUK_HEAP_STRING_READ_DOUBLE_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_DOUBLE_LE)
  990. #define DUK_HTHREAD_STRING_READ_DOUBLE_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_DOUBLE_LE)
  991. #define DUK_HEAP_STRING_READ_FLOAT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_FLOAT_BE)
  992. #define DUK_HTHREAD_STRING_READ_FLOAT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_FLOAT_BE)
  993. #define DUK_HEAP_STRING_READ_FLOAT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_FLOAT_LE)
  994. #define DUK_HTHREAD_STRING_READ_FLOAT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_FLOAT_LE)
  995. #define DUK_HEAP_STRING_READ_INT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT32_BE)
  996. #define DUK_HTHREAD_STRING_READ_INT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT32_BE)
  997. #define DUK_HEAP_STRING_READ_INT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT32_LE)
  998. #define DUK_HTHREAD_STRING_READ_INT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT32_LE)
  999. #define DUK_HEAP_STRING_READ_UINT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT32_BE)
  1000. #define DUK_HTHREAD_STRING_READ_UINT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT32_BE)
  1001. #define DUK_HEAP_STRING_READ_UINT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT32_LE)
  1002. #define DUK_HTHREAD_STRING_READ_UINT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT32_LE)
  1003. #define DUK_HEAP_STRING_READ_INT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT16_BE)
  1004. #define DUK_HTHREAD_STRING_READ_INT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT16_BE)
  1005. #define DUK_HEAP_STRING_READ_INT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT16_LE)
  1006. #define DUK_HTHREAD_STRING_READ_INT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT16_LE)
  1007. #define DUK_HEAP_STRING_READ_UINT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT16_BE)
  1008. #define DUK_HTHREAD_STRING_READ_UINT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT16_BE)
  1009. #define DUK_HEAP_STRING_READ_UINT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT16_LE)
  1010. #define DUK_HTHREAD_STRING_READ_UINT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT16_LE)
  1011. #define DUK_HEAP_STRING_READ_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT8)
  1012. #define DUK_HTHREAD_STRING_READ_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT8)
  1013. #define DUK_HEAP_STRING_READ_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT8)
  1014. #define DUK_HTHREAD_STRING_READ_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT8)
  1015. #define DUK_HEAP_STRING_COPY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COPY)
  1016. #define DUK_HTHREAD_STRING_COPY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COPY)
  1017. #define DUK_HEAP_STRING_EQUALS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EQUALS)
  1018. #define DUK_HTHREAD_STRING_EQUALS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EQUALS)
  1019. #define DUK_HEAP_STRING_FILL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILL)
  1020. #define DUK_HTHREAD_STRING_FILL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILL)
  1021. #define DUK_HEAP_STRING_WRITE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE)
  1022. #define DUK_HTHREAD_STRING_WRITE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE)
  1023. #define DUK_HEAP_STRING_COMPARE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPARE)
  1024. #define DUK_HTHREAD_STRING_COMPARE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPARE)
  1025. #define DUK_HEAP_STRING_BYTE_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTE_LENGTH)
  1026. #define DUK_HTHREAD_STRING_BYTE_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTE_LENGTH)
  1027. #define DUK_HEAP_STRING_IS_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_BUFFER)
  1028. #define DUK_HTHREAD_STRING_IS_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_BUFFER)
  1029. #define DUK_HEAP_STRING_IS_ENCODING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_ENCODING)
  1030. #define DUK_HTHREAD_STRING_IS_ENCODING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_ENCODING)
  1031. #define DUK_HEAP_STRING_EXPORTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORTS)
  1032. #define DUK_HTHREAD_STRING_EXPORTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORTS)
  1033. #define DUK_HEAP_STRING_ID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ID)
  1034. #define DUK_HTHREAD_STRING_ID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ID)
  1035. #define DUK_HEAP_STRING_REQUIRE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REQUIRE)
  1036. #define DUK_HTHREAD_STRING_REQUIRE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REQUIRE)
  1037. #define DUK_HEAP_STRING___PROTO__(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX___PROTO__)
  1038. #define DUK_HTHREAD_STRING___PROTO__(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX___PROTO__)
  1039. #define DUK_HEAP_STRING_SET_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_PROTOTYPE_OF)
  1040. #define DUK_HTHREAD_STRING_SET_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_PROTOTYPE_OF)
  1041. #define DUK_HEAP_STRING_OWN_KEYS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OWN_KEYS)
  1042. #define DUK_HTHREAD_STRING_OWN_KEYS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OWN_KEYS)
  1043. #define DUK_HEAP_STRING_ENUMERATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERATE)
  1044. #define DUK_HTHREAD_STRING_ENUMERATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERATE)
  1045. #define DUK_HEAP_STRING_DELETE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE_PROPERTY)
  1046. #define DUK_HTHREAD_STRING_DELETE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE_PROPERTY)
  1047. #define DUK_HEAP_STRING_HAS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HAS)
  1048. #define DUK_HTHREAD_STRING_HAS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HAS)
  1049. #define DUK_HEAP_STRING_PROXY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROXY)
  1050. #define DUK_HTHREAD_STRING_PROXY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROXY)
  1051. #define DUK_HEAP_STRING_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLEE)
  1052. #define DUK_HTHREAD_STRING_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLEE)
  1053. #define DUK_HEAP_STRING_INVALID_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INVALID_DATE)
  1054. #define DUK_HTHREAD_STRING_INVALID_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INVALID_DATE)
  1055. #define DUK_HEAP_STRING_BRACKETED_ELLIPSIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BRACKETED_ELLIPSIS)
  1056. #define DUK_HTHREAD_STRING_BRACKETED_ELLIPSIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BRACKETED_ELLIPSIS)
  1057. #define DUK_HEAP_STRING_NEWLINE_TAB(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEWLINE_TAB)
  1058. #define DUK_HTHREAD_STRING_NEWLINE_TAB(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEWLINE_TAB)
  1059. #define DUK_HEAP_STRING_SPACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPACE)
  1060. #define DUK_HTHREAD_STRING_SPACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPACE)
  1061. #define DUK_HEAP_STRING_COMMA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMMA)
  1062. #define DUK_HTHREAD_STRING_COMMA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMMA)
  1063. #define DUK_HEAP_STRING_MINUS_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_ZERO)
  1064. #define DUK_HTHREAD_STRING_MINUS_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_ZERO)
  1065. #define DUK_HEAP_STRING_PLUS_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PLUS_ZERO)
  1066. #define DUK_HTHREAD_STRING_PLUS_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PLUS_ZERO)
  1067. #define DUK_HEAP_STRING_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ZERO)
  1068. #define DUK_HTHREAD_STRING_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ZERO)
  1069. #define DUK_HEAP_STRING_MINUS_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_INFINITY)
  1070. #define DUK_HTHREAD_STRING_MINUS_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_INFINITY)
  1071. #define DUK_HEAP_STRING_PLUS_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PLUS_INFINITY)
  1072. #define DUK_HTHREAD_STRING_PLUS_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PLUS_INFINITY)
  1073. #define DUK_HEAP_STRING_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INFINITY)
  1074. #define DUK_HTHREAD_STRING_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INFINITY)
  1075. #define DUK_HEAP_STRING_LC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_OBJECT)
  1076. #define DUK_HTHREAD_STRING_LC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_OBJECT)
  1077. #define DUK_HEAP_STRING_LC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_STRING)
  1078. #define DUK_HTHREAD_STRING_LC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_STRING)
  1079. #define DUK_HEAP_STRING_LC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NUMBER)
  1080. #define DUK_HTHREAD_STRING_LC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NUMBER)
  1081. #define DUK_HEAP_STRING_LC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BOOLEAN)
  1082. #define DUK_HTHREAD_STRING_LC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BOOLEAN)
  1083. #define DUK_HEAP_STRING_LC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_UNDEFINED)
  1084. #define DUK_HTHREAD_STRING_LC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_UNDEFINED)
  1085. #define DUK_HEAP_STRING_STRINGIFY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STRINGIFY)
  1086. #define DUK_HTHREAD_STRING_STRINGIFY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STRINGIFY)
  1087. #define DUK_HEAP_STRING_TAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TAN)
  1088. #define DUK_HTHREAD_STRING_TAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TAN)
  1089. #define DUK_HEAP_STRING_SQRT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT)
  1090. #define DUK_HTHREAD_STRING_SQRT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT)
  1091. #define DUK_HEAP_STRING_SIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SIN)
  1092. #define DUK_HTHREAD_STRING_SIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SIN)
  1093. #define DUK_HEAP_STRING_ROUND(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ROUND)
  1094. #define DUK_HTHREAD_STRING_ROUND(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ROUND)
  1095. #define DUK_HEAP_STRING_RANDOM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RANDOM)
  1096. #define DUK_HTHREAD_STRING_RANDOM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RANDOM)
  1097. #define DUK_HEAP_STRING_POW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POW)
  1098. #define DUK_HTHREAD_STRING_POW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POW)
  1099. #define DUK_HEAP_STRING_MIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MIN)
  1100. #define DUK_HTHREAD_STRING_MIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MIN)
  1101. #define DUK_HEAP_STRING_MAX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAX)
  1102. #define DUK_HTHREAD_STRING_MAX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAX)
  1103. #define DUK_HEAP_STRING_LOG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG)
  1104. #define DUK_HTHREAD_STRING_LOG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG)
  1105. #define DUK_HEAP_STRING_FLOOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOOR)
  1106. #define DUK_HTHREAD_STRING_FLOOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOOR)
  1107. #define DUK_HEAP_STRING_EXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXP)
  1108. #define DUK_HTHREAD_STRING_EXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXP)
  1109. #define DUK_HEAP_STRING_COS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COS)
  1110. #define DUK_HTHREAD_STRING_COS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COS)
  1111. #define DUK_HEAP_STRING_CEIL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CEIL)
  1112. #define DUK_HTHREAD_STRING_CEIL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CEIL)
  1113. #define DUK_HEAP_STRING_ATAN2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ATAN2)
  1114. #define DUK_HTHREAD_STRING_ATAN2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ATAN2)
  1115. #define DUK_HEAP_STRING_ATAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ATAN)
  1116. #define DUK_HTHREAD_STRING_ATAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ATAN)
  1117. #define DUK_HEAP_STRING_ASIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ASIN)
  1118. #define DUK_HTHREAD_STRING_ASIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ASIN)
  1119. #define DUK_HEAP_STRING_ACOS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ACOS)
  1120. #define DUK_HTHREAD_STRING_ACOS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ACOS)
  1121. #define DUK_HEAP_STRING_ABS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ABS)
  1122. #define DUK_HTHREAD_STRING_ABS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ABS)
  1123. #define DUK_HEAP_STRING_SQRT2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT2)
  1124. #define DUK_HTHREAD_STRING_SQRT2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT2)
  1125. #define DUK_HEAP_STRING_SQRT1_2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT1_2)
  1126. #define DUK_HTHREAD_STRING_SQRT1_2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT1_2)
  1127. #define DUK_HEAP_STRING_PI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PI)
  1128. #define DUK_HTHREAD_STRING_PI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PI)
  1129. #define DUK_HEAP_STRING_LOG10E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG10E)
  1130. #define DUK_HTHREAD_STRING_LOG10E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG10E)
  1131. #define DUK_HEAP_STRING_LOG2E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG2E)
  1132. #define DUK_HTHREAD_STRING_LOG2E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG2E)
  1133. #define DUK_HEAP_STRING_LN2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LN2)
  1134. #define DUK_HTHREAD_STRING_LN2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LN2)
  1135. #define DUK_HEAP_STRING_LN10(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LN10)
  1136. #define DUK_HTHREAD_STRING_LN10(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LN10)
  1137. #define DUK_HEAP_STRING_E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_E)
  1138. #define DUK_HTHREAD_STRING_E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_E)
  1139. #define DUK_HEAP_STRING_MESSAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MESSAGE)
  1140. #define DUK_HTHREAD_STRING_MESSAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MESSAGE)
  1141. #define DUK_HEAP_STRING_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAME)
  1142. #define DUK_HTHREAD_STRING_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAME)
  1143. #define DUK_HEAP_STRING_INPUT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INPUT)
  1144. #define DUK_HTHREAD_STRING_INPUT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INPUT)
  1145. #define DUK_HEAP_STRING_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INDEX)
  1146. #define DUK_HTHREAD_STRING_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INDEX)
  1147. #define DUK_HEAP_STRING_ESCAPED_EMPTY_REGEXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ESCAPED_EMPTY_REGEXP)
  1148. #define DUK_HTHREAD_STRING_ESCAPED_EMPTY_REGEXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ESCAPED_EMPTY_REGEXP)
  1149. #define DUK_HEAP_STRING_LAST_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LAST_INDEX)
  1150. #define DUK_HTHREAD_STRING_LAST_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LAST_INDEX)
  1151. #define DUK_HEAP_STRING_MULTILINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MULTILINE)
  1152. #define DUK_HTHREAD_STRING_MULTILINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MULTILINE)
  1153. #define DUK_HEAP_STRING_IGNORE_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IGNORE_CASE)
  1154. #define DUK_HTHREAD_STRING_IGNORE_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IGNORE_CASE)
  1155. #define DUK_HEAP_STRING_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SOURCE)
  1156. #define DUK_HTHREAD_STRING_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SOURCE)
  1157. #define DUK_HEAP_STRING_TEST(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TEST)
  1158. #define DUK_HTHREAD_STRING_TEST(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TEST)
  1159. #define DUK_HEAP_STRING_EXEC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXEC)
  1160. #define DUK_HTHREAD_STRING_EXEC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXEC)
  1161. #define DUK_HEAP_STRING_TO_GMT_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_GMT_STRING)
  1162. #define DUK_HTHREAD_STRING_TO_GMT_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_GMT_STRING)
  1163. #define DUK_HEAP_STRING_SET_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_YEAR)
  1164. #define DUK_HTHREAD_STRING_SET_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_YEAR)
  1165. #define DUK_HEAP_STRING_GET_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_YEAR)
  1166. #define DUK_HTHREAD_STRING_GET_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_YEAR)
  1167. #define DUK_HEAP_STRING_TO_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_JSON)
  1168. #define DUK_HTHREAD_STRING_TO_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_JSON)
  1169. #define DUK_HEAP_STRING_TO_ISO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_ISO_STRING)
  1170. #define DUK_HTHREAD_STRING_TO_ISO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_ISO_STRING)
  1171. #define DUK_HEAP_STRING_TO_UTC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_UTC_STRING)
  1172. #define DUK_HTHREAD_STRING_TO_UTC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_UTC_STRING)
  1173. #define DUK_HEAP_STRING_SET_UTC_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_FULL_YEAR)
  1174. #define DUK_HTHREAD_STRING_SET_UTC_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_FULL_YEAR)
  1175. #define DUK_HEAP_STRING_SET_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FULL_YEAR)
  1176. #define DUK_HTHREAD_STRING_SET_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FULL_YEAR)
  1177. #define DUK_HEAP_STRING_SET_UTC_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MONTH)
  1178. #define DUK_HTHREAD_STRING_SET_UTC_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MONTH)
  1179. #define DUK_HEAP_STRING_SET_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MONTH)
  1180. #define DUK_HTHREAD_STRING_SET_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MONTH)
  1181. #define DUK_HEAP_STRING_SET_UTC_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_DATE)
  1182. #define DUK_HTHREAD_STRING_SET_UTC_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_DATE)
  1183. #define DUK_HEAP_STRING_SET_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_DATE)
  1184. #define DUK_HTHREAD_STRING_SET_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_DATE)
  1185. #define DUK_HEAP_STRING_SET_UTC_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_HOURS)
  1186. #define DUK_HTHREAD_STRING_SET_UTC_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_HOURS)
  1187. #define DUK_HEAP_STRING_SET_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_HOURS)
  1188. #define DUK_HTHREAD_STRING_SET_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_HOURS)
  1189. #define DUK_HEAP_STRING_SET_UTC_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MINUTES)
  1190. #define DUK_HTHREAD_STRING_SET_UTC_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MINUTES)
  1191. #define DUK_HEAP_STRING_SET_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MINUTES)
  1192. #define DUK_HTHREAD_STRING_SET_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MINUTES)
  1193. #define DUK_HEAP_STRING_SET_UTC_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_SECONDS)
  1194. #define DUK_HTHREAD_STRING_SET_UTC_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_SECONDS)
  1195. #define DUK_HEAP_STRING_SET_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_SECONDS)
  1196. #define DUK_HTHREAD_STRING_SET_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_SECONDS)
  1197. #define DUK_HEAP_STRING_SET_UTC_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MILLISECONDS)
  1198. #define DUK_HTHREAD_STRING_SET_UTC_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MILLISECONDS)
  1199. #define DUK_HEAP_STRING_SET_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MILLISECONDS)
  1200. #define DUK_HTHREAD_STRING_SET_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MILLISECONDS)
  1201. #define DUK_HEAP_STRING_SET_TIME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_TIME)
  1202. #define DUK_HTHREAD_STRING_SET_TIME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_TIME)
  1203. #define DUK_HEAP_STRING_GET_TIMEZONE_OFFSET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_TIMEZONE_OFFSET)
  1204. #define DUK_HTHREAD_STRING_GET_TIMEZONE_OFFSET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_TIMEZONE_OFFSET)
  1205. #define DUK_HEAP_STRING_GET_UTC_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MILLISECONDS)
  1206. #define DUK_HTHREAD_STRING_GET_UTC_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MILLISECONDS)
  1207. #define DUK_HEAP_STRING_GET_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MILLISECONDS)
  1208. #define DUK_HTHREAD_STRING_GET_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MILLISECONDS)
  1209. #define DUK_HEAP_STRING_GET_UTC_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_SECONDS)
  1210. #define DUK_HTHREAD_STRING_GET_UTC_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_SECONDS)
  1211. #define DUK_HEAP_STRING_GET_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_SECONDS)
  1212. #define DUK_HTHREAD_STRING_GET_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_SECONDS)
  1213. #define DUK_HEAP_STRING_GET_UTC_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MINUTES)
  1214. #define DUK_HTHREAD_STRING_GET_UTC_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MINUTES)
  1215. #define DUK_HEAP_STRING_GET_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MINUTES)
  1216. #define DUK_HTHREAD_STRING_GET_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MINUTES)
  1217. #define DUK_HEAP_STRING_GET_UTC_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_HOURS)
  1218. #define DUK_HTHREAD_STRING_GET_UTC_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_HOURS)
  1219. #define DUK_HEAP_STRING_GET_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_HOURS)
  1220. #define DUK_HTHREAD_STRING_GET_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_HOURS)
  1221. #define DUK_HEAP_STRING_GET_UTC_DAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_DAY)
  1222. #define DUK_HTHREAD_STRING_GET_UTC_DAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_DAY)
  1223. #define DUK_HEAP_STRING_GET_DAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_DAY)
  1224. #define DUK_HTHREAD_STRING_GET_DAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_DAY)
  1225. #define DUK_HEAP_STRING_GET_UTC_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_DATE)
  1226. #define DUK_HTHREAD_STRING_GET_UTC_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_DATE)
  1227. #define DUK_HEAP_STRING_GET_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_DATE)
  1228. #define DUK_HTHREAD_STRING_GET_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_DATE)
  1229. #define DUK_HEAP_STRING_GET_UTC_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MONTH)
  1230. #define DUK_HTHREAD_STRING_GET_UTC_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MONTH)
  1231. #define DUK_HEAP_STRING_GET_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MONTH)
  1232. #define DUK_HTHREAD_STRING_GET_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MONTH)
  1233. #define DUK_HEAP_STRING_GET_UTC_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_FULL_YEAR)
  1234. #define DUK_HTHREAD_STRING_GET_UTC_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_FULL_YEAR)
  1235. #define DUK_HEAP_STRING_GET_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FULL_YEAR)
  1236. #define DUK_HTHREAD_STRING_GET_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FULL_YEAR)
  1237. #define DUK_HEAP_STRING_GET_TIME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_TIME)
  1238. #define DUK_HTHREAD_STRING_GET_TIME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_TIME)
  1239. #define DUK_HEAP_STRING_TO_LOCALE_TIME_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_TIME_STRING)
  1240. #define DUK_HTHREAD_STRING_TO_LOCALE_TIME_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_TIME_STRING)
  1241. #define DUK_HEAP_STRING_TO_LOCALE_DATE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_DATE_STRING)
  1242. #define DUK_HTHREAD_STRING_TO_LOCALE_DATE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_DATE_STRING)
  1243. #define DUK_HEAP_STRING_TO_TIME_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_TIME_STRING)
  1244. #define DUK_HTHREAD_STRING_TO_TIME_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_TIME_STRING)
  1245. #define DUK_HEAP_STRING_TO_DATE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_DATE_STRING)
  1246. #define DUK_HTHREAD_STRING_TO_DATE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_DATE_STRING)
  1247. #define DUK_HEAP_STRING_NOW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NOW)
  1248. #define DUK_HTHREAD_STRING_NOW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NOW)
  1249. #define DUK_HEAP_STRING_UTC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UTC)
  1250. #define DUK_HTHREAD_STRING_UTC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UTC)
  1251. #define DUK_HEAP_STRING_PARSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE)
  1252. #define DUK_HTHREAD_STRING_PARSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE)
  1253. #define DUK_HEAP_STRING_TO_PRECISION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_PRECISION)
  1254. #define DUK_HTHREAD_STRING_TO_PRECISION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_PRECISION)
  1255. #define DUK_HEAP_STRING_TO_EXPONENTIAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_EXPONENTIAL)
  1256. #define DUK_HTHREAD_STRING_TO_EXPONENTIAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_EXPONENTIAL)
  1257. #define DUK_HEAP_STRING_TO_FIXED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_FIXED)
  1258. #define DUK_HTHREAD_STRING_TO_FIXED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_FIXED)
  1259. #define DUK_HEAP_STRING_POSITIVE_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POSITIVE_INFINITY)
  1260. #define DUK_HTHREAD_STRING_POSITIVE_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POSITIVE_INFINITY)
  1261. #define DUK_HEAP_STRING_NEGATIVE_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEGATIVE_INFINITY)
  1262. #define DUK_HTHREAD_STRING_NEGATIVE_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEGATIVE_INFINITY)
  1263. #define DUK_HEAP_STRING_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAN)
  1264. #define DUK_HTHREAD_STRING_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAN)
  1265. #define DUK_HEAP_STRING_MIN_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MIN_VALUE)
  1266. #define DUK_HTHREAD_STRING_MIN_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MIN_VALUE)
  1267. #define DUK_HEAP_STRING_MAX_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAX_VALUE)
  1268. #define DUK_HTHREAD_STRING_MAX_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAX_VALUE)
  1269. #define DUK_HEAP_STRING_SUBSTR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBSTR)
  1270. #define DUK_HTHREAD_STRING_SUBSTR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBSTR)
  1271. #define DUK_HEAP_STRING_TRIM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRIM)
  1272. #define DUK_HTHREAD_STRING_TRIM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRIM)
  1273. #define DUK_HEAP_STRING_TO_LOCALE_UPPER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_UPPER_CASE)
  1274. #define DUK_HTHREAD_STRING_TO_LOCALE_UPPER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_UPPER_CASE)
  1275. #define DUK_HEAP_STRING_TO_UPPER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_UPPER_CASE)
  1276. #define DUK_HTHREAD_STRING_TO_UPPER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_UPPER_CASE)
  1277. #define DUK_HEAP_STRING_TO_LOCALE_LOWER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_LOWER_CASE)
  1278. #define DUK_HTHREAD_STRING_TO_LOCALE_LOWER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_LOWER_CASE)
  1279. #define DUK_HEAP_STRING_TO_LOWER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOWER_CASE)
  1280. #define DUK_HTHREAD_STRING_TO_LOWER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOWER_CASE)
  1281. #define DUK_HEAP_STRING_SUBSTRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBSTRING)
  1282. #define DUK_HTHREAD_STRING_SUBSTRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBSTRING)
  1283. #define DUK_HEAP_STRING_SPLIT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPLIT)
  1284. #define DUK_HTHREAD_STRING_SPLIT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPLIT)
  1285. #define DUK_HEAP_STRING_SEARCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SEARCH)
  1286. #define DUK_HTHREAD_STRING_SEARCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SEARCH)
  1287. #define DUK_HEAP_STRING_REPLACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REPLACE)
  1288. #define DUK_HTHREAD_STRING_REPLACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REPLACE)
  1289. #define DUK_HEAP_STRING_MATCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MATCH)
  1290. #define DUK_HTHREAD_STRING_MATCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MATCH)
  1291. #define DUK_HEAP_STRING_LOCALE_COMPARE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOCALE_COMPARE)
  1292. #define DUK_HTHREAD_STRING_LOCALE_COMPARE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOCALE_COMPARE)
  1293. #define DUK_HEAP_STRING_CHAR_CODE_AT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CHAR_CODE_AT)
  1294. #define DUK_HTHREAD_STRING_CHAR_CODE_AT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CHAR_CODE_AT)
  1295. #define DUK_HEAP_STRING_CHAR_AT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CHAR_AT)
  1296. #define DUK_HTHREAD_STRING_CHAR_AT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CHAR_AT)
  1297. #define DUK_HEAP_STRING_FROM_CHAR_CODE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FROM_CHAR_CODE)
  1298. #define DUK_HTHREAD_STRING_FROM_CHAR_CODE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FROM_CHAR_CODE)
  1299. #define DUK_HEAP_STRING_REDUCE_RIGHT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REDUCE_RIGHT)
  1300. #define DUK_HTHREAD_STRING_REDUCE_RIGHT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REDUCE_RIGHT)
  1301. #define DUK_HEAP_STRING_REDUCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REDUCE)
  1302. #define DUK_HTHREAD_STRING_REDUCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REDUCE)
  1303. #define DUK_HEAP_STRING_FILTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILTER)
  1304. #define DUK_HTHREAD_STRING_FILTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILTER)
  1305. #define DUK_HEAP_STRING_MAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAP)
  1306. #define DUK_HTHREAD_STRING_MAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAP)
  1307. #define DUK_HEAP_STRING_FOR_EACH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FOR_EACH)
  1308. #define DUK_HTHREAD_STRING_FOR_EACH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FOR_EACH)
  1309. #define DUK_HEAP_STRING_SOME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SOME)
  1310. #define DUK_HTHREAD_STRING_SOME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SOME)
  1311. #define DUK_HEAP_STRING_EVERY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVERY)
  1312. #define DUK_HTHREAD_STRING_EVERY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVERY)
  1313. #define DUK_HEAP_STRING_LAST_INDEX_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LAST_INDEX_OF)
  1314. #define DUK_HTHREAD_STRING_LAST_INDEX_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LAST_INDEX_OF)
  1315. #define DUK_HEAP_STRING_INDEX_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INDEX_OF)
  1316. #define DUK_HTHREAD_STRING_INDEX_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INDEX_OF)
  1317. #define DUK_HEAP_STRING_UNSHIFT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UNSHIFT)
  1318. #define DUK_HTHREAD_STRING_UNSHIFT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UNSHIFT)
  1319. #define DUK_HEAP_STRING_SPLICE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPLICE)
  1320. #define DUK_HTHREAD_STRING_SPLICE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPLICE)
  1321. #define DUK_HEAP_STRING_SORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SORT)
  1322. #define DUK_HTHREAD_STRING_SORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SORT)
  1323. #define DUK_HEAP_STRING_SLICE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SLICE)
  1324. #define DUK_HTHREAD_STRING_SLICE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SLICE)
  1325. #define DUK_HEAP_STRING_SHIFT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SHIFT)
  1326. #define DUK_HTHREAD_STRING_SHIFT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SHIFT)
  1327. #define DUK_HEAP_STRING_REVERSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REVERSE)
  1328. #define DUK_HTHREAD_STRING_REVERSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REVERSE)
  1329. #define DUK_HEAP_STRING_PUSH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PUSH)
  1330. #define DUK_HTHREAD_STRING_PUSH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PUSH)
  1331. #define DUK_HEAP_STRING_POP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POP)
  1332. #define DUK_HTHREAD_STRING_POP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POP)
  1333. #define DUK_HEAP_STRING_JOIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JOIN)
  1334. #define DUK_HTHREAD_STRING_JOIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JOIN)
  1335. #define DUK_HEAP_STRING_CONCAT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONCAT)
  1336. #define DUK_HTHREAD_STRING_CONCAT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONCAT)
  1337. #define DUK_HEAP_STRING_IS_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_ARRAY)
  1338. #define DUK_HTHREAD_STRING_IS_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_ARRAY)
  1339. #define DUK_HEAP_STRING_LC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ARGUMENTS)
  1340. #define DUK_HTHREAD_STRING_LC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ARGUMENTS)
  1341. #define DUK_HEAP_STRING_CALLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLER)
  1342. #define DUK_HTHREAD_STRING_CALLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLER)
  1343. #define DUK_HEAP_STRING_BIND(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BIND)
  1344. #define DUK_HTHREAD_STRING_BIND(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BIND)
  1345. #define DUK_HEAP_STRING_CALL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALL)
  1346. #define DUK_HTHREAD_STRING_CALL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALL)
  1347. #define DUK_HEAP_STRING_APPLY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_APPLY)
  1348. #define DUK_HTHREAD_STRING_APPLY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_APPLY)
  1349. #define DUK_HEAP_STRING_PROPERTY_IS_ENUMERABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROPERTY_IS_ENUMERABLE)
  1350. #define DUK_HTHREAD_STRING_PROPERTY_IS_ENUMERABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROPERTY_IS_ENUMERABLE)
  1351. #define DUK_HEAP_STRING_IS_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_PROTOTYPE_OF)
  1352. #define DUK_HTHREAD_STRING_IS_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_PROTOTYPE_OF)
  1353. #define DUK_HEAP_STRING_HAS_OWN_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HAS_OWN_PROPERTY)
  1354. #define DUK_HTHREAD_STRING_HAS_OWN_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HAS_OWN_PROPERTY)
  1355. #define DUK_HEAP_STRING_VALUE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE_OF)
  1356. #define DUK_HTHREAD_STRING_VALUE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE_OF)
  1357. #define DUK_HEAP_STRING_TO_LOCALE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_STRING)
  1358. #define DUK_HTHREAD_STRING_TO_LOCALE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_STRING)
  1359. #define DUK_HEAP_STRING_TO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_STRING)
  1360. #define DUK_HTHREAD_STRING_TO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_STRING)
  1361. #define DUK_HEAP_STRING_CONSTRUCTOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONSTRUCTOR)
  1362. #define DUK_HTHREAD_STRING_CONSTRUCTOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONSTRUCTOR)
  1363. #define DUK_HEAP_STRING_SET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET)
  1364. #define DUK_HTHREAD_STRING_SET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET)
  1365. #define DUK_HEAP_STRING_GET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET)
  1366. #define DUK_HTHREAD_STRING_GET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET)
  1367. #define DUK_HEAP_STRING_ENUMERABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERABLE)
  1368. #define DUK_HTHREAD_STRING_ENUMERABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERABLE)
  1369. #define DUK_HEAP_STRING_CONFIGURABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONFIGURABLE)
  1370. #define DUK_HTHREAD_STRING_CONFIGURABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONFIGURABLE)
  1371. #define DUK_HEAP_STRING_WRITABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITABLE)
  1372. #define DUK_HTHREAD_STRING_WRITABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITABLE)
  1373. #define DUK_HEAP_STRING_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE)
  1374. #define DUK_HTHREAD_STRING_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE)
  1375. #define DUK_HEAP_STRING_KEYS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_KEYS)
  1376. #define DUK_HTHREAD_STRING_KEYS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_KEYS)
  1377. #define DUK_HEAP_STRING_IS_EXTENSIBLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_EXTENSIBLE)
  1378. #define DUK_HTHREAD_STRING_IS_EXTENSIBLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_EXTENSIBLE)
  1379. #define DUK_HEAP_STRING_IS_FROZEN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_FROZEN)
  1380. #define DUK_HTHREAD_STRING_IS_FROZEN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_FROZEN)
  1381. #define DUK_HEAP_STRING_IS_SEALED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_SEALED)
  1382. #define DUK_HTHREAD_STRING_IS_SEALED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_SEALED)
  1383. #define DUK_HEAP_STRING_PREVENT_EXTENSIONS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PREVENT_EXTENSIONS)
  1384. #define DUK_HTHREAD_STRING_PREVENT_EXTENSIONS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PREVENT_EXTENSIONS)
  1385. #define DUK_HEAP_STRING_FREEZE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FREEZE)
  1386. #define DUK_HTHREAD_STRING_FREEZE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FREEZE)
  1387. #define DUK_HEAP_STRING_SEAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SEAL)
  1388. #define DUK_HTHREAD_STRING_SEAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SEAL)
  1389. #define DUK_HEAP_STRING_DEFINE_PROPERTIES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFINE_PROPERTIES)
  1390. #define DUK_HTHREAD_STRING_DEFINE_PROPERTIES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFINE_PROPERTIES)
  1391. #define DUK_HEAP_STRING_DEFINE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFINE_PROPERTY)
  1392. #define DUK_HTHREAD_STRING_DEFINE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFINE_PROPERTY)
  1393. #define DUK_HEAP_STRING_CREATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CREATE)
  1394. #define DUK_HTHREAD_STRING_CREATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CREATE)
  1395. #define DUK_HEAP_STRING_GET_OWN_PROPERTY_NAMES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_OWN_PROPERTY_NAMES)
  1396. #define DUK_HTHREAD_STRING_GET_OWN_PROPERTY_NAMES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_OWN_PROPERTY_NAMES)
  1397. #define DUK_HEAP_STRING_GET_OWN_PROPERTY_DESCRIPTOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR)
  1398. #define DUK_HTHREAD_STRING_GET_OWN_PROPERTY_DESCRIPTOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR)
  1399. #define DUK_HEAP_STRING_GET_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_PROTOTYPE_OF)
  1400. #define DUK_HTHREAD_STRING_GET_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_PROTOTYPE_OF)
  1401. #define DUK_HEAP_STRING_PROTOTYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTOTYPE)
  1402. #define DUK_HTHREAD_STRING_PROTOTYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTOTYPE)
  1403. #define DUK_HEAP_STRING_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LENGTH)
  1404. #define DUK_HTHREAD_STRING_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LENGTH)
  1405. #define DUK_HEAP_STRING_ALERT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ALERT)
  1406. #define DUK_HTHREAD_STRING_ALERT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ALERT)
  1407. #define DUK_HEAP_STRING_PRINT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PRINT)
  1408. #define DUK_HTHREAD_STRING_PRINT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PRINT)
  1409. #define DUK_HEAP_STRING_UNESCAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UNESCAPE)
  1410. #define DUK_HTHREAD_STRING_UNESCAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UNESCAPE)
  1411. #define DUK_HEAP_STRING_ESCAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ESCAPE)
  1412. #define DUK_HTHREAD_STRING_ESCAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ESCAPE)
  1413. #define DUK_HEAP_STRING_ENCODE_URI_COMPONENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENCODE_URI_COMPONENT)
  1414. #define DUK_HTHREAD_STRING_ENCODE_URI_COMPONENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENCODE_URI_COMPONENT)
  1415. #define DUK_HEAP_STRING_ENCODE_URI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENCODE_URI)
  1416. #define DUK_HTHREAD_STRING_ENCODE_URI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENCODE_URI)
  1417. #define DUK_HEAP_STRING_DECODE_URI_COMPONENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DECODE_URI_COMPONENT)
  1418. #define DUK_HTHREAD_STRING_DECODE_URI_COMPONENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DECODE_URI_COMPONENT)
  1419. #define DUK_HEAP_STRING_DECODE_URI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DECODE_URI)
  1420. #define DUK_HTHREAD_STRING_DECODE_URI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DECODE_URI)
  1421. #define DUK_HEAP_STRING_IS_FINITE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_FINITE)
  1422. #define DUK_HTHREAD_STRING_IS_FINITE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_FINITE)
  1423. #define DUK_HEAP_STRING_IS_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_NAN)
  1424. #define DUK_HTHREAD_STRING_IS_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_NAN)
  1425. #define DUK_HEAP_STRING_PARSE_FLOAT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE_FLOAT)
  1426. #define DUK_HTHREAD_STRING_PARSE_FLOAT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE_FLOAT)
  1427. #define DUK_HEAP_STRING_PARSE_INT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE_INT)
  1428. #define DUK_HTHREAD_STRING_PARSE_INT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE_INT)
  1429. #define DUK_HEAP_STRING_EVAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVAL)
  1430. #define DUK_HTHREAD_STRING_EVAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVAL)
  1431. #define DUK_HEAP_STRING_URI_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_URI_ERROR)
  1432. #define DUK_HTHREAD_STRING_URI_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_URI_ERROR)
  1433. #define DUK_HEAP_STRING_TYPE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPE_ERROR)
  1434. #define DUK_HTHREAD_STRING_TYPE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPE_ERROR)
  1435. #define DUK_HEAP_STRING_SYNTAX_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SYNTAX_ERROR)
  1436. #define DUK_HTHREAD_STRING_SYNTAX_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SYNTAX_ERROR)
  1437. #define DUK_HEAP_STRING_REFERENCE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REFERENCE_ERROR)
  1438. #define DUK_HTHREAD_STRING_REFERENCE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REFERENCE_ERROR)
  1439. #define DUK_HEAP_STRING_RANGE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RANGE_ERROR)
  1440. #define DUK_HTHREAD_STRING_RANGE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RANGE_ERROR)
  1441. #define DUK_HEAP_STRING_EVAL_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVAL_ERROR)
  1442. #define DUK_HTHREAD_STRING_EVAL_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVAL_ERROR)
  1443. #define DUK_HEAP_STRING_BREAK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BREAK)
  1444. #define DUK_HTHREAD_STRING_BREAK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BREAK)
  1445. #define DUK_HEAP_STRING_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CASE)
  1446. #define DUK_HTHREAD_STRING_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CASE)
  1447. #define DUK_HEAP_STRING_CATCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CATCH)
  1448. #define DUK_HTHREAD_STRING_CATCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CATCH)
  1449. #define DUK_HEAP_STRING_CONTINUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONTINUE)
  1450. #define DUK_HTHREAD_STRING_CONTINUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONTINUE)
  1451. #define DUK_HEAP_STRING_DEBUGGER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEBUGGER)
  1452. #define DUK_HTHREAD_STRING_DEBUGGER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEBUGGER)
  1453. #define DUK_HEAP_STRING_DEFAULT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFAULT)
  1454. #define DUK_HTHREAD_STRING_DEFAULT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFAULT)
  1455. #define DUK_HEAP_STRING_DELETE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE)
  1456. #define DUK_HTHREAD_STRING_DELETE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE)
  1457. #define DUK_HEAP_STRING_DO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DO)
  1458. #define DUK_HTHREAD_STRING_DO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DO)
  1459. #define DUK_HEAP_STRING_ELSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ELSE)
  1460. #define DUK_HTHREAD_STRING_ELSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ELSE)
  1461. #define DUK_HEAP_STRING_FINALLY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FINALLY)
  1462. #define DUK_HTHREAD_STRING_FINALLY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FINALLY)
  1463. #define DUK_HEAP_STRING_FOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FOR)
  1464. #define DUK_HTHREAD_STRING_FOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FOR)
  1465. #define DUK_HEAP_STRING_LC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FUNCTION)
  1466. #define DUK_HTHREAD_STRING_LC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FUNCTION)
  1467. #define DUK_HEAP_STRING_IF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IF)
  1468. #define DUK_HTHREAD_STRING_IF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IF)
  1469. #define DUK_HEAP_STRING_IN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IN)
  1470. #define DUK_HTHREAD_STRING_IN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IN)
  1471. #define DUK_HEAP_STRING_INSTANCEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INSTANCEOF)
  1472. #define DUK_HTHREAD_STRING_INSTANCEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INSTANCEOF)
  1473. #define DUK_HEAP_STRING_NEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEW)
  1474. #define DUK_HTHREAD_STRING_NEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEW)
  1475. #define DUK_HEAP_STRING_RETURN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RETURN)
  1476. #define DUK_HTHREAD_STRING_RETURN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RETURN)
  1477. #define DUK_HEAP_STRING_SWITCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SWITCH)
  1478. #define DUK_HTHREAD_STRING_SWITCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SWITCH)
  1479. #define DUK_HEAP_STRING_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THIS)
  1480. #define DUK_HTHREAD_STRING_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THIS)
  1481. #define DUK_HEAP_STRING_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THROW)
  1482. #define DUK_HTHREAD_STRING_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THROW)
  1483. #define DUK_HEAP_STRING_TRY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRY)
  1484. #define DUK_HTHREAD_STRING_TRY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRY)
  1485. #define DUK_HEAP_STRING_TYPEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPEOF)
  1486. #define DUK_HTHREAD_STRING_TYPEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPEOF)
  1487. #define DUK_HEAP_STRING_VAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VAR)
  1488. #define DUK_HTHREAD_STRING_VAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VAR)
  1489. #define DUK_HEAP_STRING_CONST(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONST)
  1490. #define DUK_HTHREAD_STRING_CONST(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONST)
  1491. #define DUK_HEAP_STRING_VOID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VOID)
  1492. #define DUK_HTHREAD_STRING_VOID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VOID)
  1493. #define DUK_HEAP_STRING_WHILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WHILE)
  1494. #define DUK_HTHREAD_STRING_WHILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WHILE)
  1495. #define DUK_HEAP_STRING_WITH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WITH)
  1496. #define DUK_HTHREAD_STRING_WITH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WITH)
  1497. #define DUK_HEAP_STRING_CLASS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLASS)
  1498. #define DUK_HTHREAD_STRING_CLASS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLASS)
  1499. #define DUK_HEAP_STRING_ENUM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUM)
  1500. #define DUK_HTHREAD_STRING_ENUM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUM)
  1501. #define DUK_HEAP_STRING_EXPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORT)
  1502. #define DUK_HTHREAD_STRING_EXPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORT)
  1503. #define DUK_HEAP_STRING_EXTENDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXTENDS)
  1504. #define DUK_HTHREAD_STRING_EXTENDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXTENDS)
  1505. #define DUK_HEAP_STRING_IMPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPORT)
  1506. #define DUK_HTHREAD_STRING_IMPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPORT)
  1507. #define DUK_HEAP_STRING_SUPER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUPER)
  1508. #define DUK_HTHREAD_STRING_SUPER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUPER)
  1509. #define DUK_HEAP_STRING_LC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NULL)
  1510. #define DUK_HTHREAD_STRING_LC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NULL)
  1511. #define DUK_HEAP_STRING_TRUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRUE)
  1512. #define DUK_HTHREAD_STRING_TRUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRUE)
  1513. #define DUK_HEAP_STRING_FALSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FALSE)
  1514. #define DUK_HTHREAD_STRING_FALSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FALSE)
  1515. #define DUK_HEAP_STRING_IMPLEMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPLEMENTS)
  1516. #define DUK_HTHREAD_STRING_IMPLEMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPLEMENTS)
  1517. #define DUK_HEAP_STRING_INTERFACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INTERFACE)
  1518. #define DUK_HTHREAD_STRING_INTERFACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INTERFACE)
  1519. #define DUK_HEAP_STRING_LET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LET)
  1520. #define DUK_HTHREAD_STRING_LET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LET)
  1521. #define DUK_HEAP_STRING_PACKAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PACKAGE)
  1522. #define DUK_HTHREAD_STRING_PACKAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PACKAGE)
  1523. #define DUK_HEAP_STRING_PRIVATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PRIVATE)
  1524. #define DUK_HTHREAD_STRING_PRIVATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PRIVATE)
  1525. #define DUK_HEAP_STRING_PROTECTED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTECTED)
  1526. #define DUK_HTHREAD_STRING_PROTECTED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTECTED)
  1527. #define DUK_HEAP_STRING_PUBLIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PUBLIC)
  1528. #define DUK_HTHREAD_STRING_PUBLIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PUBLIC)
  1529. #define DUK_HEAP_STRING_STATIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STATIC)
  1530. #define DUK_HTHREAD_STRING_STATIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STATIC)
  1531. #define DUK_HEAP_STRING_YIELD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_YIELD)
  1532. #define DUK_HTHREAD_STRING_YIELD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_YIELD)
  1533. #define DUK_HEAP_NUM_STRINGS 414
  1534. #define DUK_STRIDX_START_RESERVED 369
  1535. #define DUK_STRIDX_START_STRICT_RESERVED 405
  1536. #define DUK_STRIDX_END_RESERVED 414 /* exclusive endpoint */
  1537. #if !defined(DUK_SINGLE_FILE)
  1538. DUK_INTERNAL_DECL const duk_c_function duk_bi_native_functions[147];
  1539. DUK_INTERNAL_DECL const duk_uint8_t duk_builtins_data[1952];
  1540. #ifdef DUK_USE_BUILTIN_INITJS
  1541. DUK_INTERNAL_DECL const duk_uint8_t duk_initjs_data[1757];
  1542. #endif /* DUK_USE_BUILTIN_INITJS */
  1543. #endif /* !DUK_SINGLE_FILE */
  1544. #define DUK_BUILTINS_DATA_LENGTH 1952
  1545. #ifdef DUK_USE_BUILTIN_INITJS
  1546. #define DUK_BUILTIN_INITJS_DATA_LENGTH 1757
  1547. #endif /* DUK_USE_BUILTIN_INITJS */
  1548. #define DUK_BIDX_GLOBAL 0
  1549. #define DUK_BIDX_GLOBAL_ENV 1
  1550. #define DUK_BIDX_OBJECT_CONSTRUCTOR 2
  1551. #define DUK_BIDX_OBJECT_PROTOTYPE 3
  1552. #define DUK_BIDX_FUNCTION_CONSTRUCTOR 4
  1553. #define DUK_BIDX_FUNCTION_PROTOTYPE 5
  1554. #define DUK_BIDX_ARRAY_CONSTRUCTOR 6
  1555. #define DUK_BIDX_ARRAY_PROTOTYPE 7
  1556. #define DUK_BIDX_STRING_CONSTRUCTOR 8
  1557. #define DUK_BIDX_STRING_PROTOTYPE 9
  1558. #define DUK_BIDX_BOOLEAN_CONSTRUCTOR 10
  1559. #define DUK_BIDX_BOOLEAN_PROTOTYPE 11
  1560. #define DUK_BIDX_NUMBER_CONSTRUCTOR 12
  1561. #define DUK_BIDX_NUMBER_PROTOTYPE 13
  1562. #define DUK_BIDX_DATE_CONSTRUCTOR 14
  1563. #define DUK_BIDX_DATE_PROTOTYPE 15
  1564. #define DUK_BIDX_REGEXP_CONSTRUCTOR 16
  1565. #define DUK_BIDX_REGEXP_PROTOTYPE 17
  1566. #define DUK_BIDX_ERROR_CONSTRUCTOR 18
  1567. #define DUK_BIDX_ERROR_PROTOTYPE 19
  1568. #define DUK_BIDX_EVAL_ERROR_CONSTRUCTOR 20
  1569. #define DUK_BIDX_EVAL_ERROR_PROTOTYPE 21
  1570. #define DUK_BIDX_RANGE_ERROR_CONSTRUCTOR 22
  1571. #define DUK_BIDX_RANGE_ERROR_PROTOTYPE 23
  1572. #define DUK_BIDX_REFERENCE_ERROR_CONSTRUCTOR 24
  1573. #define DUK_BIDX_REFERENCE_ERROR_PROTOTYPE 25
  1574. #define DUK_BIDX_SYNTAX_ERROR_CONSTRUCTOR 26
  1575. #define DUK_BIDX_SYNTAX_ERROR_PROTOTYPE 27
  1576. #define DUK_BIDX_TYPE_ERROR_CONSTRUCTOR 28
  1577. #define DUK_BIDX_TYPE_ERROR_PROTOTYPE 29
  1578. #define DUK_BIDX_URI_ERROR_CONSTRUCTOR 30
  1579. #define DUK_BIDX_URI_ERROR_PROTOTYPE 31
  1580. #define DUK_BIDX_MATH 32
  1581. #define DUK_BIDX_JSON 33
  1582. #define DUK_BIDX_TYPE_ERROR_THROWER 34
  1583. #define DUK_BIDX_PROXY_CONSTRUCTOR 35
  1584. #define DUK_BIDX_DUKTAPE 36
  1585. #define DUK_BIDX_THREAD_CONSTRUCTOR 37
  1586. #define DUK_BIDX_THREAD_PROTOTYPE 38
  1587. #define DUK_BIDX_BUFFER_CONSTRUCTOR 39
  1588. #define DUK_BIDX_BUFFER_PROTOTYPE 40
  1589. #define DUK_BIDX_POINTER_CONSTRUCTOR 41
  1590. #define DUK_BIDX_POINTER_PROTOTYPE 42
  1591. #define DUK_BIDX_LOGGER_CONSTRUCTOR 43
  1592. #define DUK_BIDX_LOGGER_PROTOTYPE 44
  1593. #define DUK_BIDX_DOUBLE_ERROR 45
  1594. #define DUK_BIDX_ARRAYBUFFER_CONSTRUCTOR 46
  1595. #define DUK_BIDX_ARRAYBUFFER_PROTOTYPE 47
  1596. #define DUK_BIDX_DATAVIEW_CONSTRUCTOR 48
  1597. #define DUK_BIDX_DATAVIEW_PROTOTYPE 49
  1598. #define DUK_BIDX_TYPEDARRAY_PROTOTYPE 50
  1599. #define DUK_BIDX_INT8ARRAY_CONSTRUCTOR 51
  1600. #define DUK_BIDX_INT8ARRAY_PROTOTYPE 52
  1601. #define DUK_BIDX_UINT8ARRAY_CONSTRUCTOR 53
  1602. #define DUK_BIDX_UINT8ARRAY_PROTOTYPE 54
  1603. #define DUK_BIDX_UINT8CLAMPEDARRAY_CONSTRUCTOR 55
  1604. #define DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE 56
  1605. #define DUK_BIDX_INT16ARRAY_CONSTRUCTOR 57
  1606. #define DUK_BIDX_INT16ARRAY_PROTOTYPE 58
  1607. #define DUK_BIDX_UINT16ARRAY_CONSTRUCTOR 59
  1608. #define DUK_BIDX_UINT16ARRAY_PROTOTYPE 60
  1609. #define DUK_BIDX_INT32ARRAY_CONSTRUCTOR 61
  1610. #define DUK_BIDX_INT32ARRAY_PROTOTYPE 62
  1611. #define DUK_BIDX_UINT32ARRAY_CONSTRUCTOR 63
  1612. #define DUK_BIDX_UINT32ARRAY_PROTOTYPE 64
  1613. #define DUK_BIDX_FLOAT32ARRAY_CONSTRUCTOR 65
  1614. #define DUK_BIDX_FLOAT32ARRAY_PROTOTYPE 66
  1615. #define DUK_BIDX_FLOAT64ARRAY_CONSTRUCTOR 67
  1616. #define DUK_BIDX_FLOAT64ARRAY_PROTOTYPE 68
  1617. #define DUK_BIDX_NODEJS_BUFFER_CONSTRUCTOR 69
  1618. #define DUK_BIDX_NODEJS_BUFFER_PROTOTYPE 70
  1619. #define DUK_NUM_BUILTINS 71
  1620. #elif defined(DUK_USE_DOUBLE_BE)
  1621. #if !defined(DUK_SINGLE_FILE)
  1622. DUK_INTERNAL_DECL const duk_uint8_t duk_strings_data[2624];
  1623. #endif /* !DUK_SINGLE_FILE */
  1624. #define DUK_STRDATA_DATA_LENGTH 2624
  1625. #define DUK_STRDATA_MAX_STRLEN 24
  1626. #define DUK_STRIDX_UC_LOGGER 0 /* 'Logger' */
  1627. #define DUK_STRIDX_UC_THREAD 1 /* 'Thread' */
  1628. #define DUK_STRIDX_UC_POINTER 2 /* 'Pointer' */
  1629. #define DUK_STRIDX_DEC_ENV 3 /* 'DecEnv' */
  1630. #define DUK_STRIDX_OBJ_ENV 4 /* 'ObjEnv' */
  1631. #define DUK_STRIDX_FLOAT64_ARRAY 5 /* 'Float64Array' */
  1632. #define DUK_STRIDX_FLOAT32_ARRAY 6 /* 'Float32Array' */
  1633. #define DUK_STRIDX_UINT32_ARRAY 7 /* 'Uint32Array' */
  1634. #define DUK_STRIDX_INT32_ARRAY 8 /* 'Int32Array' */
  1635. #define DUK_STRIDX_UINT16_ARRAY 9 /* 'Uint16Array' */
  1636. #define DUK_STRIDX_INT16_ARRAY 10 /* 'Int16Array' */
  1637. #define DUK_STRIDX_UINT8_CLAMPED_ARRAY 11 /* 'Uint8ClampedArray' */
  1638. #define DUK_STRIDX_UINT8_ARRAY 12 /* 'Uint8Array' */
  1639. #define DUK_STRIDX_INT8_ARRAY 13 /* 'Int8Array' */
  1640. #define DUK_STRIDX_DATA_VIEW 14 /* 'DataView' */
  1641. #define DUK_STRIDX_ARRAY_BUFFER 15 /* 'ArrayBuffer' */
  1642. #define DUK_STRIDX_UC_BUFFER 16 /* 'Buffer' */
  1643. #define DUK_STRIDX_EMPTY_STRING 17 /* '' */
  1644. #define DUK_STRIDX_GLOBAL 18 /* 'global' */
  1645. #define DUK_STRIDX_UC_ARGUMENTS 19 /* 'Arguments' */
  1646. #define DUK_STRIDX_JSON 20 /* 'JSON' */
  1647. #define DUK_STRIDX_MATH 21 /* 'Math' */
  1648. #define DUK_STRIDX_UC_ERROR 22 /* 'Error' */
  1649. #define DUK_STRIDX_REG_EXP 23 /* 'RegExp' */
  1650. #define DUK_STRIDX_DATE 24 /* 'Date' */
  1651. #define DUK_STRIDX_UC_NUMBER 25 /* 'Number' */
  1652. #define DUK_STRIDX_UC_BOOLEAN 26 /* 'Boolean' */
  1653. #define DUK_STRIDX_UC_STRING 27 /* 'String' */
  1654. #define DUK_STRIDX_ARRAY 28 /* 'Array' */
  1655. #define DUK_STRIDX_UC_FUNCTION 29 /* 'Function' */
  1656. #define DUK_STRIDX_UC_OBJECT 30 /* 'Object' */
  1657. #define DUK_STRIDX_UC_NULL 31 /* 'Null' */
  1658. #define DUK_STRIDX_UC_UNDEFINED 32 /* 'Undefined' */
  1659. #define DUK_STRIDX_JSON_EXT_FUNCTION2 33 /* '{_func:true}' */
  1660. #define DUK_STRIDX_JSON_EXT_FUNCTION1 34 /* '{"_func":true}' */
  1661. #define DUK_STRIDX_JSON_EXT_NEGINF 35 /* '{"_ninf":true}' */
  1662. #define DUK_STRIDX_JSON_EXT_POSINF 36 /* '{"_inf":true}' */
  1663. #define DUK_STRIDX_JSON_EXT_NAN 37 /* '{"_nan":true}' */
  1664. #define DUK_STRIDX_JSON_EXT_UNDEFINED 38 /* '{"_undef":true}' */
  1665. #define DUK_STRIDX_TO_LOG_STRING 39 /* 'toLogString' */
  1666. #define DUK_STRIDX_CLOG 40 /* 'clog' */
  1667. #define DUK_STRIDX_LC_L 41 /* 'l' */
  1668. #define DUK_STRIDX_LC_N 42 /* 'n' */
  1669. #define DUK_STRIDX_LC_FATAL 43 /* 'fatal' */
  1670. #define DUK_STRIDX_LC_ERROR 44 /* 'error' */
  1671. #define DUK_STRIDX_LC_WARN 45 /* 'warn' */
  1672. #define DUK_STRIDX_LC_DEBUG 46 /* 'debug' */
  1673. #define DUK_STRIDX_LC_TRACE 47 /* 'trace' */
  1674. #define DUK_STRIDX_RAW 48 /* 'raw' */
  1675. #define DUK_STRIDX_FMT 49 /* 'fmt' */
  1676. #define DUK_STRIDX_CURRENT 50 /* 'current' */
  1677. #define DUK_STRIDX_RESUME 51 /* 'resume' */
  1678. #define DUK_STRIDX_COMPACT 52 /* 'compact' */
  1679. #define DUK_STRIDX_JC 53 /* 'jc' */
  1680. #define DUK_STRIDX_JX 54 /* 'jx' */
  1681. #define DUK_STRIDX_BASE64 55 /* 'base64' */
  1682. #define DUK_STRIDX_HEX 56 /* 'hex' */
  1683. #define DUK_STRIDX_DEC 57 /* 'dec' */
  1684. #define DUK_STRIDX_ENC 58 /* 'enc' */
  1685. #define DUK_STRIDX_FIN 59 /* 'fin' */
  1686. #define DUK_STRIDX_GC 60 /* 'gc' */
  1687. #define DUK_STRIDX_ACT 61 /* 'act' */
  1688. #define DUK_STRIDX_LC_INFO 62 /* 'info' */
  1689. #define DUK_STRIDX_VERSION 63 /* 'version' */
  1690. #define DUK_STRIDX_ENV 64 /* 'env' */
  1691. #define DUK_STRIDX_MOD_LOADED 65 /* 'modLoaded' */
  1692. #define DUK_STRIDX_MOD_SEARCH 66 /* 'modSearch' */
  1693. #define DUK_STRIDX_ERR_THROW 67 /* 'errThrow' */
  1694. #define DUK_STRIDX_ERR_CREATE 68 /* 'errCreate' */
  1695. #define DUK_STRIDX_COMPILE 69 /* 'compile' */
  1696. #define DUK_STRIDX_INT_REGBASE 70 /* '\x00Regbase' */
  1697. #define DUK_STRIDX_INT_THREAD 71 /* '\x00Thread' */
  1698. #define DUK_STRIDX_INT_HANDLER 72 /* '\x00Handler' */
  1699. #define DUK_STRIDX_INT_FINALIZER 73 /* '\x00Finalizer' */
  1700. #define DUK_STRIDX_INT_CALLEE 74 /* '\x00Callee' */
  1701. #define DUK_STRIDX_INT_MAP 75 /* '\x00Map' */
  1702. #define DUK_STRIDX_INT_ARGS 76 /* '\x00Args' */
  1703. #define DUK_STRIDX_INT_THIS 77 /* '\x00This' */
  1704. #define DUK_STRIDX_INT_PC2LINE 78 /* '\x00Pc2line' */
  1705. #define DUK_STRIDX_INT_SOURCE 79 /* '\x00Source' */
  1706. #define DUK_STRIDX_INT_VARENV 80 /* '\x00Varenv' */
  1707. #define DUK_STRIDX_INT_LEXENV 81 /* '\x00Lexenv' */
  1708. #define DUK_STRIDX_INT_VARMAP 82 /* '\x00Varmap' */
  1709. #define DUK_STRIDX_INT_FORMALS 83 /* '\x00Formals' */
  1710. #define DUK_STRIDX_INT_BYTECODE 84 /* '\x00Bytecode' */
  1711. #define DUK_STRIDX_INT_NEXT 85 /* '\x00Next' */
  1712. #define DUK_STRIDX_INT_TARGET 86 /* '\x00Target' */
  1713. #define DUK_STRIDX_INT_VALUE 87 /* '\x00Value' */
  1714. #define DUK_STRIDX_LC_POINTER 88 /* 'pointer' */
  1715. #define DUK_STRIDX_INT_TRACEDATA 89 /* '\x00Tracedata' */
  1716. #define DUK_STRIDX_LINE_NUMBER 90 /* 'lineNumber' */
  1717. #define DUK_STRIDX_FILE_NAME 91 /* 'fileName' */
  1718. #define DUK_STRIDX_PC 92 /* 'pc' */
  1719. #define DUK_STRIDX_STACK 93 /* 'stack' */
  1720. #define DUK_STRIDX_THROW_TYPE_ERROR 94 /* 'ThrowTypeError' */
  1721. #define DUK_STRIDX_DUKTAPE 95 /* 'Duktape' */
  1722. #define DUK_STRIDX_SET_FLOAT64 96 /* 'setFloat64' */
  1723. #define DUK_STRIDX_SET_FLOAT32 97 /* 'setFloat32' */
  1724. #define DUK_STRIDX_SET_UINT32 98 /* 'setUint32' */
  1725. #define DUK_STRIDX_SET_INT32 99 /* 'setInt32' */
  1726. #define DUK_STRIDX_SET_UINT16 100 /* 'setUint16' */
  1727. #define DUK_STRIDX_SET_INT16 101 /* 'setInt16' */
  1728. #define DUK_STRIDX_SET_UINT8 102 /* 'setUint8' */
  1729. #define DUK_STRIDX_SET_INT8 103 /* 'setInt8' */
  1730. #define DUK_STRIDX_GET_FLOAT64 104 /* 'getFloat64' */
  1731. #define DUK_STRIDX_GET_FLOAT32 105 /* 'getFloat32' */
  1732. #define DUK_STRIDX_GET_UINT32 106 /* 'getUint32' */
  1733. #define DUK_STRIDX_GET_INT32 107 /* 'getInt32' */
  1734. #define DUK_STRIDX_GET_UINT16 108 /* 'getUint16' */
  1735. #define DUK_STRIDX_GET_INT16 109 /* 'getInt16' */
  1736. #define DUK_STRIDX_GET_UINT8 110 /* 'getUint8' */
  1737. #define DUK_STRIDX_GET_INT8 111 /* 'getInt8' */
  1738. #define DUK_STRIDX_SUBARRAY 112 /* 'subarray' */
  1739. #define DUK_STRIDX_BYTES_PER_ELEMENT 113 /* 'BYTES_PER_ELEMENT' */
  1740. #define DUK_STRIDX_BYTE_OFFSET 114 /* 'byteOffset' */
  1741. #define DUK_STRIDX_LC_BUFFER 115 /* 'buffer' */
  1742. #define DUK_STRIDX_IS_VIEW 116 /* 'isView' */
  1743. #define DUK_STRIDX_DATA 117 /* 'data' */
  1744. #define DUK_STRIDX_TYPE 118 /* 'type' */
  1745. #define DUK_STRIDX_WRITE_INT_BE 119 /* 'writeIntBE' */
  1746. #define DUK_STRIDX_WRITE_INT_LE 120 /* 'writeIntLE' */
  1747. #define DUK_STRIDX_WRITE_UINT_BE 121 /* 'writeUIntBE' */
  1748. #define DUK_STRIDX_WRITE_UINT_LE 122 /* 'writeUIntLE' */
  1749. #define DUK_STRIDX_WRITE_DOUBLE_BE 123 /* 'writeDoubleBE' */
  1750. #define DUK_STRIDX_WRITE_DOUBLE_LE 124 /* 'writeDoubleLE' */
  1751. #define DUK_STRIDX_WRITE_FLOAT_BE 125 /* 'writeFloatBE' */
  1752. #define DUK_STRIDX_WRITE_FLOAT_LE 126 /* 'writeFloatLE' */
  1753. #define DUK_STRIDX_WRITE_INT32_BE 127 /* 'writeInt32BE' */
  1754. #define DUK_STRIDX_WRITE_INT32_LE 128 /* 'writeInt32LE' */
  1755. #define DUK_STRIDX_WRITE_UINT32_BE 129 /* 'writeUInt32BE' */
  1756. #define DUK_STRIDX_WRITE_UINT32_LE 130 /* 'writeUInt32LE' */
  1757. #define DUK_STRIDX_WRITE_INT16_BE 131 /* 'writeInt16BE' */
  1758. #define DUK_STRIDX_WRITE_INT16_LE 132 /* 'writeInt16LE' */
  1759. #define DUK_STRIDX_WRITE_UINT16_BE 133 /* 'writeUInt16BE' */
  1760. #define DUK_STRIDX_WRITE_UINT16_LE 134 /* 'writeUInt16LE' */
  1761. #define DUK_STRIDX_WRITE_INT8 135 /* 'writeInt8' */
  1762. #define DUK_STRIDX_WRITE_UINT8 136 /* 'writeUInt8' */
  1763. #define DUK_STRIDX_READ_INT_BE 137 /* 'readIntBE' */
  1764. #define DUK_STRIDX_READ_INT_LE 138 /* 'readIntLE' */
  1765. #define DUK_STRIDX_READ_UINT_BE 139 /* 'readUIntBE' */
  1766. #define DUK_STRIDX_READ_UINT_LE 140 /* 'readUIntLE' */
  1767. #define DUK_STRIDX_READ_DOUBLE_BE 141 /* 'readDoubleBE' */
  1768. #define DUK_STRIDX_READ_DOUBLE_LE 142 /* 'readDoubleLE' */
  1769. #define DUK_STRIDX_READ_FLOAT_BE 143 /* 'readFloatBE' */
  1770. #define DUK_STRIDX_READ_FLOAT_LE 144 /* 'readFloatLE' */
  1771. #define DUK_STRIDX_READ_INT32_BE 145 /* 'readInt32BE' */
  1772. #define DUK_STRIDX_READ_INT32_LE 146 /* 'readInt32LE' */
  1773. #define DUK_STRIDX_READ_UINT32_BE 147 /* 'readUInt32BE' */
  1774. #define DUK_STRIDX_READ_UINT32_LE 148 /* 'readUInt32LE' */
  1775. #define DUK_STRIDX_READ_INT16_BE 149 /* 'readInt16BE' */
  1776. #define DUK_STRIDX_READ_INT16_LE 150 /* 'readInt16LE' */
  1777. #define DUK_STRIDX_READ_UINT16_BE 151 /* 'readUInt16BE' */
  1778. #define DUK_STRIDX_READ_UINT16_LE 152 /* 'readUInt16LE' */
  1779. #define DUK_STRIDX_READ_INT8 153 /* 'readInt8' */
  1780. #define DUK_STRIDX_READ_UINT8 154 /* 'readUInt8' */
  1781. #define DUK_STRIDX_COPY 155 /* 'copy' */
  1782. #define DUK_STRIDX_EQUALS 156 /* 'equals' */
  1783. #define DUK_STRIDX_FILL 157 /* 'fill' */
  1784. #define DUK_STRIDX_WRITE 158 /* 'write' */
  1785. #define DUK_STRIDX_COMPARE 159 /* 'compare' */
  1786. #define DUK_STRIDX_BYTE_LENGTH 160 /* 'byteLength' */
  1787. #define DUK_STRIDX_IS_BUFFER 161 /* 'isBuffer' */
  1788. #define DUK_STRIDX_IS_ENCODING 162 /* 'isEncoding' */
  1789. #define DUK_STRIDX_EXPORTS 163 /* 'exports' */
  1790. #define DUK_STRIDX_ID 164 /* 'id' */
  1791. #define DUK_STRIDX_REQUIRE 165 /* 'require' */
  1792. #define DUK_STRIDX___PROTO__ 166 /* '__proto__' */
  1793. #define DUK_STRIDX_SET_PROTOTYPE_OF 167 /* 'setPrototypeOf' */
  1794. #define DUK_STRIDX_OWN_KEYS 168 /* 'ownKeys' */
  1795. #define DUK_STRIDX_ENUMERATE 169 /* 'enumerate' */
  1796. #define DUK_STRIDX_DELETE_PROPERTY 170 /* 'deleteProperty' */
  1797. #define DUK_STRIDX_HAS 171 /* 'has' */
  1798. #define DUK_STRIDX_PROXY 172 /* 'Proxy' */
  1799. #define DUK_STRIDX_CALLEE 173 /* 'callee' */
  1800. #define DUK_STRIDX_INVALID_DATE 174 /* 'Invalid Date' */
  1801. #define DUK_STRIDX_BRACKETED_ELLIPSIS 175 /* '[...]' */
  1802. #define DUK_STRIDX_NEWLINE_TAB 176 /* '\n\t' */
  1803. #define DUK_STRIDX_SPACE 177 /* ' ' */
  1804. #define DUK_STRIDX_COMMA 178 /* ',' */
  1805. #define DUK_STRIDX_MINUS_ZERO 179 /* '-0' */
  1806. #define DUK_STRIDX_PLUS_ZERO 180 /* '+0' */
  1807. #define DUK_STRIDX_ZERO 181 /* '0' */
  1808. #define DUK_STRIDX_MINUS_INFINITY 182 /* '-Infinity' */
  1809. #define DUK_STRIDX_PLUS_INFINITY 183 /* '+Infinity' */
  1810. #define DUK_STRIDX_INFINITY 184 /* 'Infinity' */
  1811. #define DUK_STRIDX_LC_OBJECT 185 /* 'object' */
  1812. #define DUK_STRIDX_LC_STRING 186 /* 'string' */
  1813. #define DUK_STRIDX_LC_NUMBER 187 /* 'number' */
  1814. #define DUK_STRIDX_LC_BOOLEAN 188 /* 'boolean' */
  1815. #define DUK_STRIDX_LC_UNDEFINED 189 /* 'undefined' */
  1816. #define DUK_STRIDX_STRINGIFY 190 /* 'stringify' */
  1817. #define DUK_STRIDX_TAN 191 /* 'tan' */
  1818. #define DUK_STRIDX_SQRT 192 /* 'sqrt' */
  1819. #define DUK_STRIDX_SIN 193 /* 'sin' */
  1820. #define DUK_STRIDX_ROUND 194 /* 'round' */
  1821. #define DUK_STRIDX_RANDOM 195 /* 'random' */
  1822. #define DUK_STRIDX_POW 196 /* 'pow' */
  1823. #define DUK_STRIDX_MIN 197 /* 'min' */
  1824. #define DUK_STRIDX_MAX 198 /* 'max' */
  1825. #define DUK_STRIDX_LOG 199 /* 'log' */
  1826. #define DUK_STRIDX_FLOOR 200 /* 'floor' */
  1827. #define DUK_STRIDX_EXP 201 /* 'exp' */
  1828. #define DUK_STRIDX_COS 202 /* 'cos' */
  1829. #define DUK_STRIDX_CEIL 203 /* 'ceil' */
  1830. #define DUK_STRIDX_ATAN2 204 /* 'atan2' */
  1831. #define DUK_STRIDX_ATAN 205 /* 'atan' */
  1832. #define DUK_STRIDX_ASIN 206 /* 'asin' */
  1833. #define DUK_STRIDX_ACOS 207 /* 'acos' */
  1834. #define DUK_STRIDX_ABS 208 /* 'abs' */
  1835. #define DUK_STRIDX_SQRT2 209 /* 'SQRT2' */
  1836. #define DUK_STRIDX_SQRT1_2 210 /* 'SQRT1_2' */
  1837. #define DUK_STRIDX_PI 211 /* 'PI' */
  1838. #define DUK_STRIDX_LOG10E 212 /* 'LOG10E' */
  1839. #define DUK_STRIDX_LOG2E 213 /* 'LOG2E' */
  1840. #define DUK_STRIDX_LN2 214 /* 'LN2' */
  1841. #define DUK_STRIDX_LN10 215 /* 'LN10' */
  1842. #define DUK_STRIDX_E 216 /* 'E' */
  1843. #define DUK_STRIDX_MESSAGE 217 /* 'message' */
  1844. #define DUK_STRIDX_NAME 218 /* 'name' */
  1845. #define DUK_STRIDX_INPUT 219 /* 'input' */
  1846. #define DUK_STRIDX_INDEX 220 /* 'index' */
  1847. #define DUK_STRIDX_ESCAPED_EMPTY_REGEXP 221 /* '(?:)' */
  1848. #define DUK_STRIDX_LAST_INDEX 222 /* 'lastIndex' */
  1849. #define DUK_STRIDX_MULTILINE 223 /* 'multiline' */
  1850. #define DUK_STRIDX_IGNORE_CASE 224 /* 'ignoreCase' */
  1851. #define DUK_STRIDX_SOURCE 225 /* 'source' */
  1852. #define DUK_STRIDX_TEST 226 /* 'test' */
  1853. #define DUK_STRIDX_EXEC 227 /* 'exec' */
  1854. #define DUK_STRIDX_TO_GMT_STRING 228 /* 'toGMTString' */
  1855. #define DUK_STRIDX_SET_YEAR 229 /* 'setYear' */
  1856. #define DUK_STRIDX_GET_YEAR 230 /* 'getYear' */
  1857. #define DUK_STRIDX_TO_JSON 231 /* 'toJSON' */
  1858. #define DUK_STRIDX_TO_ISO_STRING 232 /* 'toISOString' */
  1859. #define DUK_STRIDX_TO_UTC_STRING 233 /* 'toUTCString' */
  1860. #define DUK_STRIDX_SET_UTC_FULL_YEAR 234 /* 'setUTCFullYear' */
  1861. #define DUK_STRIDX_SET_FULL_YEAR 235 /* 'setFullYear' */
  1862. #define DUK_STRIDX_SET_UTC_MONTH 236 /* 'setUTCMonth' */
  1863. #define DUK_STRIDX_SET_MONTH 237 /* 'setMonth' */
  1864. #define DUK_STRIDX_SET_UTC_DATE 238 /* 'setUTCDate' */
  1865. #define DUK_STRIDX_SET_DATE 239 /* 'setDate' */
  1866. #define DUK_STRIDX_SET_UTC_HOURS 240 /* 'setUTCHours' */
  1867. #define DUK_STRIDX_SET_HOURS 241 /* 'setHours' */
  1868. #define DUK_STRIDX_SET_UTC_MINUTES 242 /* 'setUTCMinutes' */
  1869. #define DUK_STRIDX_SET_MINUTES 243 /* 'setMinutes' */
  1870. #define DUK_STRIDX_SET_UTC_SECONDS 244 /* 'setUTCSeconds' */
  1871. #define DUK_STRIDX_SET_SECONDS 245 /* 'setSeconds' */
  1872. #define DUK_STRIDX_SET_UTC_MILLISECONDS 246 /* 'setUTCMilliseconds' */
  1873. #define DUK_STRIDX_SET_MILLISECONDS 247 /* 'setMilliseconds' */
  1874. #define DUK_STRIDX_SET_TIME 248 /* 'setTime' */
  1875. #define DUK_STRIDX_GET_TIMEZONE_OFFSET 249 /* 'getTimezoneOffset' */
  1876. #define DUK_STRIDX_GET_UTC_MILLISECONDS 250 /* 'getUTCMilliseconds' */
  1877. #define DUK_STRIDX_GET_MILLISECONDS 251 /* 'getMilliseconds' */
  1878. #define DUK_STRIDX_GET_UTC_SECONDS 252 /* 'getUTCSeconds' */
  1879. #define DUK_STRIDX_GET_SECONDS 253 /* 'getSeconds' */
  1880. #define DUK_STRIDX_GET_UTC_MINUTES 254 /* 'getUTCMinutes' */
  1881. #define DUK_STRIDX_GET_MINUTES 255 /* 'getMinutes' */
  1882. #define DUK_STRIDX_GET_UTC_HOURS 256 /* 'getUTCHours' */
  1883. #define DUK_STRIDX_GET_HOURS 257 /* 'getHours' */
  1884. #define DUK_STRIDX_GET_UTC_DAY 258 /* 'getUTCDay' */
  1885. #define DUK_STRIDX_GET_DAY 259 /* 'getDay' */
  1886. #define DUK_STRIDX_GET_UTC_DATE 260 /* 'getUTCDate' */
  1887. #define DUK_STRIDX_GET_DATE 261 /* 'getDate' */
  1888. #define DUK_STRIDX_GET_UTC_MONTH 262 /* 'getUTCMonth' */
  1889. #define DUK_STRIDX_GET_MONTH 263 /* 'getMonth' */
  1890. #define DUK_STRIDX_GET_UTC_FULL_YEAR 264 /* 'getUTCFullYear' */
  1891. #define DUK_STRIDX_GET_FULL_YEAR 265 /* 'getFullYear' */
  1892. #define DUK_STRIDX_GET_TIME 266 /* 'getTime' */
  1893. #define DUK_STRIDX_TO_LOCALE_TIME_STRING 267 /* 'toLocaleTimeString' */
  1894. #define DUK_STRIDX_TO_LOCALE_DATE_STRING 268 /* 'toLocaleDateString' */
  1895. #define DUK_STRIDX_TO_TIME_STRING 269 /* 'toTimeString' */
  1896. #define DUK_STRIDX_TO_DATE_STRING 270 /* 'toDateString' */
  1897. #define DUK_STRIDX_NOW 271 /* 'now' */
  1898. #define DUK_STRIDX_UTC 272 /* 'UTC' */
  1899. #define DUK_STRIDX_PARSE 273 /* 'parse' */
  1900. #define DUK_STRIDX_TO_PRECISION 274 /* 'toPrecision' */
  1901. #define DUK_STRIDX_TO_EXPONENTIAL 275 /* 'toExponential' */
  1902. #define DUK_STRIDX_TO_FIXED 276 /* 'toFixed' */
  1903. #define DUK_STRIDX_POSITIVE_INFINITY 277 /* 'POSITIVE_INFINITY' */
  1904. #define DUK_STRIDX_NEGATIVE_INFINITY 278 /* 'NEGATIVE_INFINITY' */
  1905. #define DUK_STRIDX_NAN 279 /* 'NaN' */
  1906. #define DUK_STRIDX_MIN_VALUE 280 /* 'MIN_VALUE' */
  1907. #define DUK_STRIDX_MAX_VALUE 281 /* 'MAX_VALUE' */
  1908. #define DUK_STRIDX_SUBSTR 282 /* 'substr' */
  1909. #define DUK_STRIDX_TRIM 283 /* 'trim' */
  1910. #define DUK_STRIDX_TO_LOCALE_UPPER_CASE 284 /* 'toLocaleUpperCase' */
  1911. #define DUK_STRIDX_TO_UPPER_CASE 285 /* 'toUpperCase' */
  1912. #define DUK_STRIDX_TO_LOCALE_LOWER_CASE 286 /* 'toLocaleLowerCase' */
  1913. #define DUK_STRIDX_TO_LOWER_CASE 287 /* 'toLowerCase' */
  1914. #define DUK_STRIDX_SUBSTRING 288 /* 'substring' */
  1915. #define DUK_STRIDX_SPLIT 289 /* 'split' */
  1916. #define DUK_STRIDX_SEARCH 290 /* 'search' */
  1917. #define DUK_STRIDX_REPLACE 291 /* 'replace' */
  1918. #define DUK_STRIDX_MATCH 292 /* 'match' */
  1919. #define DUK_STRIDX_LOCALE_COMPARE 293 /* 'localeCompare' */
  1920. #define DUK_STRIDX_CHAR_CODE_AT 294 /* 'charCodeAt' */
  1921. #define DUK_STRIDX_CHAR_AT 295 /* 'charAt' */
  1922. #define DUK_STRIDX_FROM_CHAR_CODE 296 /* 'fromCharCode' */
  1923. #define DUK_STRIDX_REDUCE_RIGHT 297 /* 'reduceRight' */
  1924. #define DUK_STRIDX_REDUCE 298 /* 'reduce' */
  1925. #define DUK_STRIDX_FILTER 299 /* 'filter' */
  1926. #define DUK_STRIDX_MAP 300 /* 'map' */
  1927. #define DUK_STRIDX_FOR_EACH 301 /* 'forEach' */
  1928. #define DUK_STRIDX_SOME 302 /* 'some' */
  1929. #define DUK_STRIDX_EVERY 303 /* 'every' */
  1930. #define DUK_STRIDX_LAST_INDEX_OF 304 /* 'lastIndexOf' */
  1931. #define DUK_STRIDX_INDEX_OF 305 /* 'indexOf' */
  1932. #define DUK_STRIDX_UNSHIFT 306 /* 'unshift' */
  1933. #define DUK_STRIDX_SPLICE 307 /* 'splice' */
  1934. #define DUK_STRIDX_SORT 308 /* 'sort' */
  1935. #define DUK_STRIDX_SLICE 309 /* 'slice' */
  1936. #define DUK_STRIDX_SHIFT 310 /* 'shift' */
  1937. #define DUK_STRIDX_REVERSE 311 /* 'reverse' */
  1938. #define DUK_STRIDX_PUSH 312 /* 'push' */
  1939. #define DUK_STRIDX_POP 313 /* 'pop' */
  1940. #define DUK_STRIDX_JOIN 314 /* 'join' */
  1941. #define DUK_STRIDX_CONCAT 315 /* 'concat' */
  1942. #define DUK_STRIDX_IS_ARRAY 316 /* 'isArray' */
  1943. #define DUK_STRIDX_LC_ARGUMENTS 317 /* 'arguments' */
  1944. #define DUK_STRIDX_CALLER 318 /* 'caller' */
  1945. #define DUK_STRIDX_BIND 319 /* 'bind' */
  1946. #define DUK_STRIDX_CALL 320 /* 'call' */
  1947. #define DUK_STRIDX_APPLY 321 /* 'apply' */
  1948. #define DUK_STRIDX_PROPERTY_IS_ENUMERABLE 322 /* 'propertyIsEnumerable' */
  1949. #define DUK_STRIDX_IS_PROTOTYPE_OF 323 /* 'isPrototypeOf' */
  1950. #define DUK_STRIDX_HAS_OWN_PROPERTY 324 /* 'hasOwnProperty' */
  1951. #define DUK_STRIDX_VALUE_OF 325 /* 'valueOf' */
  1952. #define DUK_STRIDX_TO_LOCALE_STRING 326 /* 'toLocaleString' */
  1953. #define DUK_STRIDX_TO_STRING 327 /* 'toString' */
  1954. #define DUK_STRIDX_CONSTRUCTOR 328 /* 'constructor' */
  1955. #define DUK_STRIDX_SET 329 /* 'set' */
  1956. #define DUK_STRIDX_GET 330 /* 'get' */
  1957. #define DUK_STRIDX_ENUMERABLE 331 /* 'enumerable' */
  1958. #define DUK_STRIDX_CONFIGURABLE 332 /* 'configurable' */
  1959. #define DUK_STRIDX_WRITABLE 333 /* 'writable' */
  1960. #define DUK_STRIDX_VALUE 334 /* 'value' */
  1961. #define DUK_STRIDX_KEYS 335 /* 'keys' */
  1962. #define DUK_STRIDX_IS_EXTENSIBLE 336 /* 'isExtensible' */
  1963. #define DUK_STRIDX_IS_FROZEN 337 /* 'isFrozen' */
  1964. #define DUK_STRIDX_IS_SEALED 338 /* 'isSealed' */
  1965. #define DUK_STRIDX_PREVENT_EXTENSIONS 339 /* 'preventExtensions' */
  1966. #define DUK_STRIDX_FREEZE 340 /* 'freeze' */
  1967. #define DUK_STRIDX_SEAL 341 /* 'seal' */
  1968. #define DUK_STRIDX_DEFINE_PROPERTIES 342 /* 'defineProperties' */
  1969. #define DUK_STRIDX_DEFINE_PROPERTY 343 /* 'defineProperty' */
  1970. #define DUK_STRIDX_CREATE 344 /* 'create' */
  1971. #define DUK_STRIDX_GET_OWN_PROPERTY_NAMES 345 /* 'getOwnPropertyNames' */
  1972. #define DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR 346 /* 'getOwnPropertyDescriptor' */
  1973. #define DUK_STRIDX_GET_PROTOTYPE_OF 347 /* 'getPrototypeOf' */
  1974. #define DUK_STRIDX_PROTOTYPE 348 /* 'prototype' */
  1975. #define DUK_STRIDX_LENGTH 349 /* 'length' */
  1976. #define DUK_STRIDX_ALERT 350 /* 'alert' */
  1977. #define DUK_STRIDX_PRINT 351 /* 'print' */
  1978. #define DUK_STRIDX_UNESCAPE 352 /* 'unescape' */
  1979. #define DUK_STRIDX_ESCAPE 353 /* 'escape' */
  1980. #define DUK_STRIDX_ENCODE_URI_COMPONENT 354 /* 'encodeURIComponent' */
  1981. #define DUK_STRIDX_ENCODE_URI 355 /* 'encodeURI' */
  1982. #define DUK_STRIDX_DECODE_URI_COMPONENT 356 /* 'decodeURIComponent' */
  1983. #define DUK_STRIDX_DECODE_URI 357 /* 'decodeURI' */
  1984. #define DUK_STRIDX_IS_FINITE 358 /* 'isFinite' */
  1985. #define DUK_STRIDX_IS_NAN 359 /* 'isNaN' */
  1986. #define DUK_STRIDX_PARSE_FLOAT 360 /* 'parseFloat' */
  1987. #define DUK_STRIDX_PARSE_INT 361 /* 'parseInt' */
  1988. #define DUK_STRIDX_EVAL 362 /* 'eval' */
  1989. #define DUK_STRIDX_URI_ERROR 363 /* 'URIError' */
  1990. #define DUK_STRIDX_TYPE_ERROR 364 /* 'TypeError' */
  1991. #define DUK_STRIDX_SYNTAX_ERROR 365 /* 'SyntaxError' */
  1992. #define DUK_STRIDX_REFERENCE_ERROR 366 /* 'ReferenceError' */
  1993. #define DUK_STRIDX_RANGE_ERROR 367 /* 'RangeError' */
  1994. #define DUK_STRIDX_EVAL_ERROR 368 /* 'EvalError' */
  1995. #define DUK_STRIDX_BREAK 369 /* 'break' */
  1996. #define DUK_STRIDX_CASE 370 /* 'case' */
  1997. #define DUK_STRIDX_CATCH 371 /* 'catch' */
  1998. #define DUK_STRIDX_CONTINUE 372 /* 'continue' */
  1999. #define DUK_STRIDX_DEBUGGER 373 /* 'debugger' */
  2000. #define DUK_STRIDX_DEFAULT 374 /* 'default' */
  2001. #define DUK_STRIDX_DELETE 375 /* 'delete' */
  2002. #define DUK_STRIDX_DO 376 /* 'do' */
  2003. #define DUK_STRIDX_ELSE 377 /* 'else' */
  2004. #define DUK_STRIDX_FINALLY 378 /* 'finally' */
  2005. #define DUK_STRIDX_FOR 379 /* 'for' */
  2006. #define DUK_STRIDX_LC_FUNCTION 380 /* 'function' */
  2007. #define DUK_STRIDX_IF 381 /* 'if' */
  2008. #define DUK_STRIDX_IN 382 /* 'in' */
  2009. #define DUK_STRIDX_INSTANCEOF 383 /* 'instanceof' */
  2010. #define DUK_STRIDX_NEW 384 /* 'new' */
  2011. #define DUK_STRIDX_RETURN 385 /* 'return' */
  2012. #define DUK_STRIDX_SWITCH 386 /* 'switch' */
  2013. #define DUK_STRIDX_THIS 387 /* 'this' */
  2014. #define DUK_STRIDX_THROW 388 /* 'throw' */
  2015. #define DUK_STRIDX_TRY 389 /* 'try' */
  2016. #define DUK_STRIDX_TYPEOF 390 /* 'typeof' */
  2017. #define DUK_STRIDX_VAR 391 /* 'var' */
  2018. #define DUK_STRIDX_CONST 392 /* 'const' */
  2019. #define DUK_STRIDX_VOID 393 /* 'void' */
  2020. #define DUK_STRIDX_WHILE 394 /* 'while' */
  2021. #define DUK_STRIDX_WITH 395 /* 'with' */
  2022. #define DUK_STRIDX_CLASS 396 /* 'class' */
  2023. #define DUK_STRIDX_ENUM 397 /* 'enum' */
  2024. #define DUK_STRIDX_EXPORT 398 /* 'export' */
  2025. #define DUK_STRIDX_EXTENDS 399 /* 'extends' */
  2026. #define DUK_STRIDX_IMPORT 400 /* 'import' */
  2027. #define DUK_STRIDX_SUPER 401 /* 'super' */
  2028. #define DUK_STRIDX_LC_NULL 402 /* 'null' */
  2029. #define DUK_STRIDX_TRUE 403 /* 'true' */
  2030. #define DUK_STRIDX_FALSE 404 /* 'false' */
  2031. #define DUK_STRIDX_IMPLEMENTS 405 /* 'implements' */
  2032. #define DUK_STRIDX_INTERFACE 406 /* 'interface' */
  2033. #define DUK_STRIDX_LET 407 /* 'let' */
  2034. #define DUK_STRIDX_PACKAGE 408 /* 'package' */
  2035. #define DUK_STRIDX_PRIVATE 409 /* 'private' */
  2036. #define DUK_STRIDX_PROTECTED 410 /* 'protected' */
  2037. #define DUK_STRIDX_PUBLIC 411 /* 'public' */
  2038. #define DUK_STRIDX_STATIC 412 /* 'static' */
  2039. #define DUK_STRIDX_YIELD 413 /* 'yield' */
  2040. #define DUK_HEAP_STRING_UC_LOGGER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_LOGGER)
  2041. #define DUK_HTHREAD_STRING_UC_LOGGER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_LOGGER)
  2042. #define DUK_HEAP_STRING_UC_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_THREAD)
  2043. #define DUK_HTHREAD_STRING_UC_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_THREAD)
  2044. #define DUK_HEAP_STRING_UC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_POINTER)
  2045. #define DUK_HTHREAD_STRING_UC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_POINTER)
  2046. #define DUK_HEAP_STRING_DEC_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEC_ENV)
  2047. #define DUK_HTHREAD_STRING_DEC_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEC_ENV)
  2048. #define DUK_HEAP_STRING_OBJ_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OBJ_ENV)
  2049. #define DUK_HTHREAD_STRING_OBJ_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OBJ_ENV)
  2050. #define DUK_HEAP_STRING_FLOAT64_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT64_ARRAY)
  2051. #define DUK_HTHREAD_STRING_FLOAT64_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT64_ARRAY)
  2052. #define DUK_HEAP_STRING_FLOAT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT32_ARRAY)
  2053. #define DUK_HTHREAD_STRING_FLOAT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT32_ARRAY)
  2054. #define DUK_HEAP_STRING_UINT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT32_ARRAY)
  2055. #define DUK_HTHREAD_STRING_UINT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT32_ARRAY)
  2056. #define DUK_HEAP_STRING_INT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT32_ARRAY)
  2057. #define DUK_HTHREAD_STRING_INT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT32_ARRAY)
  2058. #define DUK_HEAP_STRING_UINT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT16_ARRAY)
  2059. #define DUK_HTHREAD_STRING_UINT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT16_ARRAY)
  2060. #define DUK_HEAP_STRING_INT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT16_ARRAY)
  2061. #define DUK_HTHREAD_STRING_INT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT16_ARRAY)
  2062. #define DUK_HEAP_STRING_UINT8_CLAMPED_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_CLAMPED_ARRAY)
  2063. #define DUK_HTHREAD_STRING_UINT8_CLAMPED_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_CLAMPED_ARRAY)
  2064. #define DUK_HEAP_STRING_UINT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_ARRAY)
  2065. #define DUK_HTHREAD_STRING_UINT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_ARRAY)
  2066. #define DUK_HEAP_STRING_INT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT8_ARRAY)
  2067. #define DUK_HTHREAD_STRING_INT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT8_ARRAY)
  2068. #define DUK_HEAP_STRING_DATA_VIEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA_VIEW)
  2069. #define DUK_HTHREAD_STRING_DATA_VIEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA_VIEW)
  2070. #define DUK_HEAP_STRING_ARRAY_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY_BUFFER)
  2071. #define DUK_HTHREAD_STRING_ARRAY_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY_BUFFER)
  2072. #define DUK_HEAP_STRING_UC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BUFFER)
  2073. #define DUK_HTHREAD_STRING_UC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BUFFER)
  2074. #define DUK_HEAP_STRING_EMPTY_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EMPTY_STRING)
  2075. #define DUK_HTHREAD_STRING_EMPTY_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EMPTY_STRING)
  2076. #define DUK_HEAP_STRING_GLOBAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GLOBAL)
  2077. #define DUK_HTHREAD_STRING_GLOBAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GLOBAL)
  2078. #define DUK_HEAP_STRING_UC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ARGUMENTS)
  2079. #define DUK_HTHREAD_STRING_UC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ARGUMENTS)
  2080. #define DUK_HEAP_STRING_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON)
  2081. #define DUK_HTHREAD_STRING_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON)
  2082. #define DUK_HEAP_STRING_MATH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MATH)
  2083. #define DUK_HTHREAD_STRING_MATH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MATH)
  2084. #define DUK_HEAP_STRING_UC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ERROR)
  2085. #define DUK_HTHREAD_STRING_UC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ERROR)
  2086. #define DUK_HEAP_STRING_REG_EXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REG_EXP)
  2087. #define DUK_HTHREAD_STRING_REG_EXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REG_EXP)
  2088. #define DUK_HEAP_STRING_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATE)
  2089. #define DUK_HTHREAD_STRING_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATE)
  2090. #define DUK_HEAP_STRING_UC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NUMBER)
  2091. #define DUK_HTHREAD_STRING_UC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NUMBER)
  2092. #define DUK_HEAP_STRING_UC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BOOLEAN)
  2093. #define DUK_HTHREAD_STRING_UC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BOOLEAN)
  2094. #define DUK_HEAP_STRING_UC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_STRING)
  2095. #define DUK_HTHREAD_STRING_UC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_STRING)
  2096. #define DUK_HEAP_STRING_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY)
  2097. #define DUK_HTHREAD_STRING_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY)
  2098. #define DUK_HEAP_STRING_UC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_FUNCTION)
  2099. #define DUK_HTHREAD_STRING_UC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_FUNCTION)
  2100. #define DUK_HEAP_STRING_UC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_OBJECT)
  2101. #define DUK_HTHREAD_STRING_UC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_OBJECT)
  2102. #define DUK_HEAP_STRING_UC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NULL)
  2103. #define DUK_HTHREAD_STRING_UC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NULL)
  2104. #define DUK_HEAP_STRING_UC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_UNDEFINED)
  2105. #define DUK_HTHREAD_STRING_UC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_UNDEFINED)
  2106. #define DUK_HEAP_STRING_JSON_EXT_FUNCTION2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION2)
  2107. #define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION2)
  2108. #define DUK_HEAP_STRING_JSON_EXT_FUNCTION1(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION1)
  2109. #define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION1(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION1)
  2110. #define DUK_HEAP_STRING_JSON_EXT_NEGINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NEGINF)
  2111. #define DUK_HTHREAD_STRING_JSON_EXT_NEGINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NEGINF)
  2112. #define DUK_HEAP_STRING_JSON_EXT_POSINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_POSINF)
  2113. #define DUK_HTHREAD_STRING_JSON_EXT_POSINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_POSINF)
  2114. #define DUK_HEAP_STRING_JSON_EXT_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NAN)
  2115. #define DUK_HTHREAD_STRING_JSON_EXT_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NAN)
  2116. #define DUK_HEAP_STRING_JSON_EXT_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_UNDEFINED)
  2117. #define DUK_HTHREAD_STRING_JSON_EXT_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_UNDEFINED)
  2118. #define DUK_HEAP_STRING_TO_LOG_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOG_STRING)
  2119. #define DUK_HTHREAD_STRING_TO_LOG_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOG_STRING)
  2120. #define DUK_HEAP_STRING_CLOG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLOG)
  2121. #define DUK_HTHREAD_STRING_CLOG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLOG)
  2122. #define DUK_HEAP_STRING_LC_L(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_L)
  2123. #define DUK_HTHREAD_STRING_LC_L(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_L)
  2124. #define DUK_HEAP_STRING_LC_N(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_N)
  2125. #define DUK_HTHREAD_STRING_LC_N(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_N)
  2126. #define DUK_HEAP_STRING_LC_FATAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FATAL)
  2127. #define DUK_HTHREAD_STRING_LC_FATAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FATAL)
  2128. #define DUK_HEAP_STRING_LC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ERROR)
  2129. #define DUK_HTHREAD_STRING_LC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ERROR)
  2130. #define DUK_HEAP_STRING_LC_WARN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_WARN)
  2131. #define DUK_HTHREAD_STRING_LC_WARN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_WARN)
  2132. #define DUK_HEAP_STRING_LC_DEBUG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_DEBUG)
  2133. #define DUK_HTHREAD_STRING_LC_DEBUG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_DEBUG)
  2134. #define DUK_HEAP_STRING_LC_TRACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_TRACE)
  2135. #define DUK_HTHREAD_STRING_LC_TRACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_TRACE)
  2136. #define DUK_HEAP_STRING_RAW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RAW)
  2137. #define DUK_HTHREAD_STRING_RAW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RAW)
  2138. #define DUK_HEAP_STRING_FMT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FMT)
  2139. #define DUK_HTHREAD_STRING_FMT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FMT)
  2140. #define DUK_HEAP_STRING_CURRENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CURRENT)
  2141. #define DUK_HTHREAD_STRING_CURRENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CURRENT)
  2142. #define DUK_HEAP_STRING_RESUME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RESUME)
  2143. #define DUK_HTHREAD_STRING_RESUME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RESUME)
  2144. #define DUK_HEAP_STRING_COMPACT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPACT)
  2145. #define DUK_HTHREAD_STRING_COMPACT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPACT)
  2146. #define DUK_HEAP_STRING_JC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JC)
  2147. #define DUK_HTHREAD_STRING_JC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JC)
  2148. #define DUK_HEAP_STRING_JX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JX)
  2149. #define DUK_HTHREAD_STRING_JX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JX)
  2150. #define DUK_HEAP_STRING_BASE64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BASE64)
  2151. #define DUK_HTHREAD_STRING_BASE64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BASE64)
  2152. #define DUK_HEAP_STRING_HEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HEX)
  2153. #define DUK_HTHREAD_STRING_HEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HEX)
  2154. #define DUK_HEAP_STRING_DEC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEC)
  2155. #define DUK_HTHREAD_STRING_DEC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEC)
  2156. #define DUK_HEAP_STRING_ENC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENC)
  2157. #define DUK_HTHREAD_STRING_ENC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENC)
  2158. #define DUK_HEAP_STRING_FIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FIN)
  2159. #define DUK_HTHREAD_STRING_FIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FIN)
  2160. #define DUK_HEAP_STRING_GC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GC)
  2161. #define DUK_HTHREAD_STRING_GC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GC)
  2162. #define DUK_HEAP_STRING_ACT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ACT)
  2163. #define DUK_HTHREAD_STRING_ACT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ACT)
  2164. #define DUK_HEAP_STRING_LC_INFO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_INFO)
  2165. #define DUK_HTHREAD_STRING_LC_INFO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_INFO)
  2166. #define DUK_HEAP_STRING_VERSION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VERSION)
  2167. #define DUK_HTHREAD_STRING_VERSION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VERSION)
  2168. #define DUK_HEAP_STRING_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENV)
  2169. #define DUK_HTHREAD_STRING_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENV)
  2170. #define DUK_HEAP_STRING_MOD_LOADED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MOD_LOADED)
  2171. #define DUK_HTHREAD_STRING_MOD_LOADED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MOD_LOADED)
  2172. #define DUK_HEAP_STRING_MOD_SEARCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MOD_SEARCH)
  2173. #define DUK_HTHREAD_STRING_MOD_SEARCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MOD_SEARCH)
  2174. #define DUK_HEAP_STRING_ERR_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_THROW)
  2175. #define DUK_HTHREAD_STRING_ERR_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_THROW)
  2176. #define DUK_HEAP_STRING_ERR_CREATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_CREATE)
  2177. #define DUK_HTHREAD_STRING_ERR_CREATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_CREATE)
  2178. #define DUK_HEAP_STRING_COMPILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPILE)
  2179. #define DUK_HTHREAD_STRING_COMPILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPILE)
  2180. #define DUK_HEAP_STRING_INT_REGBASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_REGBASE)
  2181. #define DUK_HTHREAD_STRING_INT_REGBASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_REGBASE)
  2182. #define DUK_HEAP_STRING_INT_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_THREAD)
  2183. #define DUK_HTHREAD_STRING_INT_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_THREAD)
  2184. #define DUK_HEAP_STRING_INT_HANDLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_HANDLER)
  2185. #define DUK_HTHREAD_STRING_INT_HANDLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_HANDLER)
  2186. #define DUK_HEAP_STRING_INT_FINALIZER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FINALIZER)
  2187. #define DUK_HTHREAD_STRING_INT_FINALIZER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FINALIZER)
  2188. #define DUK_HEAP_STRING_INT_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_CALLEE)
  2189. #define DUK_HTHREAD_STRING_INT_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_CALLEE)
  2190. #define DUK_HEAP_STRING_INT_MAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_MAP)
  2191. #define DUK_HTHREAD_STRING_INT_MAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_MAP)
  2192. #define DUK_HEAP_STRING_INT_ARGS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_ARGS)
  2193. #define DUK_HTHREAD_STRING_INT_ARGS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_ARGS)
  2194. #define DUK_HEAP_STRING_INT_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_THIS)
  2195. #define DUK_HTHREAD_STRING_INT_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_THIS)
  2196. #define DUK_HEAP_STRING_INT_PC2LINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_PC2LINE)
  2197. #define DUK_HTHREAD_STRING_INT_PC2LINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_PC2LINE)
  2198. #define DUK_HEAP_STRING_INT_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_SOURCE)
  2199. #define DUK_HTHREAD_STRING_INT_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_SOURCE)
  2200. #define DUK_HEAP_STRING_INT_VARENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARENV)
  2201. #define DUK_HTHREAD_STRING_INT_VARENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARENV)
  2202. #define DUK_HEAP_STRING_INT_LEXENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_LEXENV)
  2203. #define DUK_HTHREAD_STRING_INT_LEXENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_LEXENV)
  2204. #define DUK_HEAP_STRING_INT_VARMAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARMAP)
  2205. #define DUK_HTHREAD_STRING_INT_VARMAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARMAP)
  2206. #define DUK_HEAP_STRING_INT_FORMALS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FORMALS)
  2207. #define DUK_HTHREAD_STRING_INT_FORMALS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FORMALS)
  2208. #define DUK_HEAP_STRING_INT_BYTECODE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_BYTECODE)
  2209. #define DUK_HTHREAD_STRING_INT_BYTECODE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_BYTECODE)
  2210. #define DUK_HEAP_STRING_INT_NEXT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_NEXT)
  2211. #define DUK_HTHREAD_STRING_INT_NEXT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_NEXT)
  2212. #define DUK_HEAP_STRING_INT_TARGET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TARGET)
  2213. #define DUK_HTHREAD_STRING_INT_TARGET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TARGET)
  2214. #define DUK_HEAP_STRING_INT_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VALUE)
  2215. #define DUK_HTHREAD_STRING_INT_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VALUE)
  2216. #define DUK_HEAP_STRING_LC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_POINTER)
  2217. #define DUK_HTHREAD_STRING_LC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_POINTER)
  2218. #define DUK_HEAP_STRING_INT_TRACEDATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TRACEDATA)
  2219. #define DUK_HTHREAD_STRING_INT_TRACEDATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TRACEDATA)
  2220. #define DUK_HEAP_STRING_LINE_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LINE_NUMBER)
  2221. #define DUK_HTHREAD_STRING_LINE_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LINE_NUMBER)
  2222. #define DUK_HEAP_STRING_FILE_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILE_NAME)
  2223. #define DUK_HTHREAD_STRING_FILE_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILE_NAME)
  2224. #define DUK_HEAP_STRING_PC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PC)
  2225. #define DUK_HTHREAD_STRING_PC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PC)
  2226. #define DUK_HEAP_STRING_STACK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STACK)
  2227. #define DUK_HTHREAD_STRING_STACK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STACK)
  2228. #define DUK_HEAP_STRING_THROW_TYPE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THROW_TYPE_ERROR)
  2229. #define DUK_HTHREAD_STRING_THROW_TYPE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THROW_TYPE_ERROR)
  2230. #define DUK_HEAP_STRING_DUKTAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DUKTAPE)
  2231. #define DUK_HTHREAD_STRING_DUKTAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DUKTAPE)
  2232. #define DUK_HEAP_STRING_SET_FLOAT64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FLOAT64)
  2233. #define DUK_HTHREAD_STRING_SET_FLOAT64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FLOAT64)
  2234. #define DUK_HEAP_STRING_SET_FLOAT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FLOAT32)
  2235. #define DUK_HTHREAD_STRING_SET_FLOAT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FLOAT32)
  2236. #define DUK_HEAP_STRING_SET_UINT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT32)
  2237. #define DUK_HTHREAD_STRING_SET_UINT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT32)
  2238. #define DUK_HEAP_STRING_SET_INT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT32)
  2239. #define DUK_HTHREAD_STRING_SET_INT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT32)
  2240. #define DUK_HEAP_STRING_SET_UINT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT16)
  2241. #define DUK_HTHREAD_STRING_SET_UINT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT16)
  2242. #define DUK_HEAP_STRING_SET_INT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT16)
  2243. #define DUK_HTHREAD_STRING_SET_INT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT16)
  2244. #define DUK_HEAP_STRING_SET_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT8)
  2245. #define DUK_HTHREAD_STRING_SET_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT8)
  2246. #define DUK_HEAP_STRING_SET_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT8)
  2247. #define DUK_HTHREAD_STRING_SET_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT8)
  2248. #define DUK_HEAP_STRING_GET_FLOAT64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FLOAT64)
  2249. #define DUK_HTHREAD_STRING_GET_FLOAT64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FLOAT64)
  2250. #define DUK_HEAP_STRING_GET_FLOAT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FLOAT32)
  2251. #define DUK_HTHREAD_STRING_GET_FLOAT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FLOAT32)
  2252. #define DUK_HEAP_STRING_GET_UINT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT32)
  2253. #define DUK_HTHREAD_STRING_GET_UINT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT32)
  2254. #define DUK_HEAP_STRING_GET_INT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT32)
  2255. #define DUK_HTHREAD_STRING_GET_INT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT32)
  2256. #define DUK_HEAP_STRING_GET_UINT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT16)
  2257. #define DUK_HTHREAD_STRING_GET_UINT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT16)
  2258. #define DUK_HEAP_STRING_GET_INT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT16)
  2259. #define DUK_HTHREAD_STRING_GET_INT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT16)
  2260. #define DUK_HEAP_STRING_GET_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT8)
  2261. #define DUK_HTHREAD_STRING_GET_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT8)
  2262. #define DUK_HEAP_STRING_GET_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT8)
  2263. #define DUK_HTHREAD_STRING_GET_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT8)
  2264. #define DUK_HEAP_STRING_SUBARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBARRAY)
  2265. #define DUK_HTHREAD_STRING_SUBARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBARRAY)
  2266. #define DUK_HEAP_STRING_BYTES_PER_ELEMENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTES_PER_ELEMENT)
  2267. #define DUK_HTHREAD_STRING_BYTES_PER_ELEMENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTES_PER_ELEMENT)
  2268. #define DUK_HEAP_STRING_BYTE_OFFSET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTE_OFFSET)
  2269. #define DUK_HTHREAD_STRING_BYTE_OFFSET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTE_OFFSET)
  2270. #define DUK_HEAP_STRING_LC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BUFFER)
  2271. #define DUK_HTHREAD_STRING_LC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BUFFER)
  2272. #define DUK_HEAP_STRING_IS_VIEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_VIEW)
  2273. #define DUK_HTHREAD_STRING_IS_VIEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_VIEW)
  2274. #define DUK_HEAP_STRING_DATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA)
  2275. #define DUK_HTHREAD_STRING_DATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA)
  2276. #define DUK_HEAP_STRING_TYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPE)
  2277. #define DUK_HTHREAD_STRING_TYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPE)
  2278. #define DUK_HEAP_STRING_WRITE_INT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT_BE)
  2279. #define DUK_HTHREAD_STRING_WRITE_INT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT_BE)
  2280. #define DUK_HEAP_STRING_WRITE_INT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT_LE)
  2281. #define DUK_HTHREAD_STRING_WRITE_INT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT_LE)
  2282. #define DUK_HEAP_STRING_WRITE_UINT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT_BE)
  2283. #define DUK_HTHREAD_STRING_WRITE_UINT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT_BE)
  2284. #define DUK_HEAP_STRING_WRITE_UINT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT_LE)
  2285. #define DUK_HTHREAD_STRING_WRITE_UINT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT_LE)
  2286. #define DUK_HEAP_STRING_WRITE_DOUBLE_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_DOUBLE_BE)
  2287. #define DUK_HTHREAD_STRING_WRITE_DOUBLE_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_DOUBLE_BE)
  2288. #define DUK_HEAP_STRING_WRITE_DOUBLE_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_DOUBLE_LE)
  2289. #define DUK_HTHREAD_STRING_WRITE_DOUBLE_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_DOUBLE_LE)
  2290. #define DUK_HEAP_STRING_WRITE_FLOAT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_FLOAT_BE)
  2291. #define DUK_HTHREAD_STRING_WRITE_FLOAT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_FLOAT_BE)
  2292. #define DUK_HEAP_STRING_WRITE_FLOAT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_FLOAT_LE)
  2293. #define DUK_HTHREAD_STRING_WRITE_FLOAT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_FLOAT_LE)
  2294. #define DUK_HEAP_STRING_WRITE_INT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT32_BE)
  2295. #define DUK_HTHREAD_STRING_WRITE_INT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT32_BE)
  2296. #define DUK_HEAP_STRING_WRITE_INT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT32_LE)
  2297. #define DUK_HTHREAD_STRING_WRITE_INT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT32_LE)
  2298. #define DUK_HEAP_STRING_WRITE_UINT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT32_BE)
  2299. #define DUK_HTHREAD_STRING_WRITE_UINT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT32_BE)
  2300. #define DUK_HEAP_STRING_WRITE_UINT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT32_LE)
  2301. #define DUK_HTHREAD_STRING_WRITE_UINT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT32_LE)
  2302. #define DUK_HEAP_STRING_WRITE_INT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT16_BE)
  2303. #define DUK_HTHREAD_STRING_WRITE_INT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT16_BE)
  2304. #define DUK_HEAP_STRING_WRITE_INT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT16_LE)
  2305. #define DUK_HTHREAD_STRING_WRITE_INT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT16_LE)
  2306. #define DUK_HEAP_STRING_WRITE_UINT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT16_BE)
  2307. #define DUK_HTHREAD_STRING_WRITE_UINT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT16_BE)
  2308. #define DUK_HEAP_STRING_WRITE_UINT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT16_LE)
  2309. #define DUK_HTHREAD_STRING_WRITE_UINT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT16_LE)
  2310. #define DUK_HEAP_STRING_WRITE_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT8)
  2311. #define DUK_HTHREAD_STRING_WRITE_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT8)
  2312. #define DUK_HEAP_STRING_WRITE_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT8)
  2313. #define DUK_HTHREAD_STRING_WRITE_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT8)
  2314. #define DUK_HEAP_STRING_READ_INT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT_BE)
  2315. #define DUK_HTHREAD_STRING_READ_INT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT_BE)
  2316. #define DUK_HEAP_STRING_READ_INT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT_LE)
  2317. #define DUK_HTHREAD_STRING_READ_INT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT_LE)
  2318. #define DUK_HEAP_STRING_READ_UINT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT_BE)
  2319. #define DUK_HTHREAD_STRING_READ_UINT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT_BE)
  2320. #define DUK_HEAP_STRING_READ_UINT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT_LE)
  2321. #define DUK_HTHREAD_STRING_READ_UINT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT_LE)
  2322. #define DUK_HEAP_STRING_READ_DOUBLE_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_DOUBLE_BE)
  2323. #define DUK_HTHREAD_STRING_READ_DOUBLE_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_DOUBLE_BE)
  2324. #define DUK_HEAP_STRING_READ_DOUBLE_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_DOUBLE_LE)
  2325. #define DUK_HTHREAD_STRING_READ_DOUBLE_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_DOUBLE_LE)
  2326. #define DUK_HEAP_STRING_READ_FLOAT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_FLOAT_BE)
  2327. #define DUK_HTHREAD_STRING_READ_FLOAT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_FLOAT_BE)
  2328. #define DUK_HEAP_STRING_READ_FLOAT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_FLOAT_LE)
  2329. #define DUK_HTHREAD_STRING_READ_FLOAT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_FLOAT_LE)
  2330. #define DUK_HEAP_STRING_READ_INT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT32_BE)
  2331. #define DUK_HTHREAD_STRING_READ_INT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT32_BE)
  2332. #define DUK_HEAP_STRING_READ_INT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT32_LE)
  2333. #define DUK_HTHREAD_STRING_READ_INT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT32_LE)
  2334. #define DUK_HEAP_STRING_READ_UINT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT32_BE)
  2335. #define DUK_HTHREAD_STRING_READ_UINT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT32_BE)
  2336. #define DUK_HEAP_STRING_READ_UINT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT32_LE)
  2337. #define DUK_HTHREAD_STRING_READ_UINT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT32_LE)
  2338. #define DUK_HEAP_STRING_READ_INT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT16_BE)
  2339. #define DUK_HTHREAD_STRING_READ_INT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT16_BE)
  2340. #define DUK_HEAP_STRING_READ_INT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT16_LE)
  2341. #define DUK_HTHREAD_STRING_READ_INT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT16_LE)
  2342. #define DUK_HEAP_STRING_READ_UINT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT16_BE)
  2343. #define DUK_HTHREAD_STRING_READ_UINT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT16_BE)
  2344. #define DUK_HEAP_STRING_READ_UINT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT16_LE)
  2345. #define DUK_HTHREAD_STRING_READ_UINT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT16_LE)
  2346. #define DUK_HEAP_STRING_READ_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT8)
  2347. #define DUK_HTHREAD_STRING_READ_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT8)
  2348. #define DUK_HEAP_STRING_READ_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT8)
  2349. #define DUK_HTHREAD_STRING_READ_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT8)
  2350. #define DUK_HEAP_STRING_COPY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COPY)
  2351. #define DUK_HTHREAD_STRING_COPY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COPY)
  2352. #define DUK_HEAP_STRING_EQUALS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EQUALS)
  2353. #define DUK_HTHREAD_STRING_EQUALS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EQUALS)
  2354. #define DUK_HEAP_STRING_FILL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILL)
  2355. #define DUK_HTHREAD_STRING_FILL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILL)
  2356. #define DUK_HEAP_STRING_WRITE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE)
  2357. #define DUK_HTHREAD_STRING_WRITE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE)
  2358. #define DUK_HEAP_STRING_COMPARE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPARE)
  2359. #define DUK_HTHREAD_STRING_COMPARE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPARE)
  2360. #define DUK_HEAP_STRING_BYTE_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTE_LENGTH)
  2361. #define DUK_HTHREAD_STRING_BYTE_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTE_LENGTH)
  2362. #define DUK_HEAP_STRING_IS_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_BUFFER)
  2363. #define DUK_HTHREAD_STRING_IS_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_BUFFER)
  2364. #define DUK_HEAP_STRING_IS_ENCODING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_ENCODING)
  2365. #define DUK_HTHREAD_STRING_IS_ENCODING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_ENCODING)
  2366. #define DUK_HEAP_STRING_EXPORTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORTS)
  2367. #define DUK_HTHREAD_STRING_EXPORTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORTS)
  2368. #define DUK_HEAP_STRING_ID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ID)
  2369. #define DUK_HTHREAD_STRING_ID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ID)
  2370. #define DUK_HEAP_STRING_REQUIRE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REQUIRE)
  2371. #define DUK_HTHREAD_STRING_REQUIRE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REQUIRE)
  2372. #define DUK_HEAP_STRING___PROTO__(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX___PROTO__)
  2373. #define DUK_HTHREAD_STRING___PROTO__(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX___PROTO__)
  2374. #define DUK_HEAP_STRING_SET_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_PROTOTYPE_OF)
  2375. #define DUK_HTHREAD_STRING_SET_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_PROTOTYPE_OF)
  2376. #define DUK_HEAP_STRING_OWN_KEYS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OWN_KEYS)
  2377. #define DUK_HTHREAD_STRING_OWN_KEYS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OWN_KEYS)
  2378. #define DUK_HEAP_STRING_ENUMERATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERATE)
  2379. #define DUK_HTHREAD_STRING_ENUMERATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERATE)
  2380. #define DUK_HEAP_STRING_DELETE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE_PROPERTY)
  2381. #define DUK_HTHREAD_STRING_DELETE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE_PROPERTY)
  2382. #define DUK_HEAP_STRING_HAS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HAS)
  2383. #define DUK_HTHREAD_STRING_HAS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HAS)
  2384. #define DUK_HEAP_STRING_PROXY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROXY)
  2385. #define DUK_HTHREAD_STRING_PROXY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROXY)
  2386. #define DUK_HEAP_STRING_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLEE)
  2387. #define DUK_HTHREAD_STRING_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLEE)
  2388. #define DUK_HEAP_STRING_INVALID_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INVALID_DATE)
  2389. #define DUK_HTHREAD_STRING_INVALID_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INVALID_DATE)
  2390. #define DUK_HEAP_STRING_BRACKETED_ELLIPSIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BRACKETED_ELLIPSIS)
  2391. #define DUK_HTHREAD_STRING_BRACKETED_ELLIPSIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BRACKETED_ELLIPSIS)
  2392. #define DUK_HEAP_STRING_NEWLINE_TAB(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEWLINE_TAB)
  2393. #define DUK_HTHREAD_STRING_NEWLINE_TAB(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEWLINE_TAB)
  2394. #define DUK_HEAP_STRING_SPACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPACE)
  2395. #define DUK_HTHREAD_STRING_SPACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPACE)
  2396. #define DUK_HEAP_STRING_COMMA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMMA)
  2397. #define DUK_HTHREAD_STRING_COMMA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMMA)
  2398. #define DUK_HEAP_STRING_MINUS_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_ZERO)
  2399. #define DUK_HTHREAD_STRING_MINUS_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_ZERO)
  2400. #define DUK_HEAP_STRING_PLUS_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PLUS_ZERO)
  2401. #define DUK_HTHREAD_STRING_PLUS_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PLUS_ZERO)
  2402. #define DUK_HEAP_STRING_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ZERO)
  2403. #define DUK_HTHREAD_STRING_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ZERO)
  2404. #define DUK_HEAP_STRING_MINUS_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_INFINITY)
  2405. #define DUK_HTHREAD_STRING_MINUS_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_INFINITY)
  2406. #define DUK_HEAP_STRING_PLUS_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PLUS_INFINITY)
  2407. #define DUK_HTHREAD_STRING_PLUS_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PLUS_INFINITY)
  2408. #define DUK_HEAP_STRING_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INFINITY)
  2409. #define DUK_HTHREAD_STRING_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INFINITY)
  2410. #define DUK_HEAP_STRING_LC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_OBJECT)
  2411. #define DUK_HTHREAD_STRING_LC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_OBJECT)
  2412. #define DUK_HEAP_STRING_LC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_STRING)
  2413. #define DUK_HTHREAD_STRING_LC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_STRING)
  2414. #define DUK_HEAP_STRING_LC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NUMBER)
  2415. #define DUK_HTHREAD_STRING_LC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NUMBER)
  2416. #define DUK_HEAP_STRING_LC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BOOLEAN)
  2417. #define DUK_HTHREAD_STRING_LC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BOOLEAN)
  2418. #define DUK_HEAP_STRING_LC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_UNDEFINED)
  2419. #define DUK_HTHREAD_STRING_LC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_UNDEFINED)
  2420. #define DUK_HEAP_STRING_STRINGIFY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STRINGIFY)
  2421. #define DUK_HTHREAD_STRING_STRINGIFY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STRINGIFY)
  2422. #define DUK_HEAP_STRING_TAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TAN)
  2423. #define DUK_HTHREAD_STRING_TAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TAN)
  2424. #define DUK_HEAP_STRING_SQRT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT)
  2425. #define DUK_HTHREAD_STRING_SQRT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT)
  2426. #define DUK_HEAP_STRING_SIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SIN)
  2427. #define DUK_HTHREAD_STRING_SIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SIN)
  2428. #define DUK_HEAP_STRING_ROUND(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ROUND)
  2429. #define DUK_HTHREAD_STRING_ROUND(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ROUND)
  2430. #define DUK_HEAP_STRING_RANDOM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RANDOM)
  2431. #define DUK_HTHREAD_STRING_RANDOM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RANDOM)
  2432. #define DUK_HEAP_STRING_POW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POW)
  2433. #define DUK_HTHREAD_STRING_POW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POW)
  2434. #define DUK_HEAP_STRING_MIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MIN)
  2435. #define DUK_HTHREAD_STRING_MIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MIN)
  2436. #define DUK_HEAP_STRING_MAX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAX)
  2437. #define DUK_HTHREAD_STRING_MAX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAX)
  2438. #define DUK_HEAP_STRING_LOG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG)
  2439. #define DUK_HTHREAD_STRING_LOG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG)
  2440. #define DUK_HEAP_STRING_FLOOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOOR)
  2441. #define DUK_HTHREAD_STRING_FLOOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOOR)
  2442. #define DUK_HEAP_STRING_EXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXP)
  2443. #define DUK_HTHREAD_STRING_EXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXP)
  2444. #define DUK_HEAP_STRING_COS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COS)
  2445. #define DUK_HTHREAD_STRING_COS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COS)
  2446. #define DUK_HEAP_STRING_CEIL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CEIL)
  2447. #define DUK_HTHREAD_STRING_CEIL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CEIL)
  2448. #define DUK_HEAP_STRING_ATAN2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ATAN2)
  2449. #define DUK_HTHREAD_STRING_ATAN2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ATAN2)
  2450. #define DUK_HEAP_STRING_ATAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ATAN)
  2451. #define DUK_HTHREAD_STRING_ATAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ATAN)
  2452. #define DUK_HEAP_STRING_ASIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ASIN)
  2453. #define DUK_HTHREAD_STRING_ASIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ASIN)
  2454. #define DUK_HEAP_STRING_ACOS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ACOS)
  2455. #define DUK_HTHREAD_STRING_ACOS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ACOS)
  2456. #define DUK_HEAP_STRING_ABS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ABS)
  2457. #define DUK_HTHREAD_STRING_ABS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ABS)
  2458. #define DUK_HEAP_STRING_SQRT2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT2)
  2459. #define DUK_HTHREAD_STRING_SQRT2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT2)
  2460. #define DUK_HEAP_STRING_SQRT1_2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT1_2)
  2461. #define DUK_HTHREAD_STRING_SQRT1_2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT1_2)
  2462. #define DUK_HEAP_STRING_PI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PI)
  2463. #define DUK_HTHREAD_STRING_PI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PI)
  2464. #define DUK_HEAP_STRING_LOG10E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG10E)
  2465. #define DUK_HTHREAD_STRING_LOG10E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG10E)
  2466. #define DUK_HEAP_STRING_LOG2E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG2E)
  2467. #define DUK_HTHREAD_STRING_LOG2E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG2E)
  2468. #define DUK_HEAP_STRING_LN2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LN2)
  2469. #define DUK_HTHREAD_STRING_LN2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LN2)
  2470. #define DUK_HEAP_STRING_LN10(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LN10)
  2471. #define DUK_HTHREAD_STRING_LN10(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LN10)
  2472. #define DUK_HEAP_STRING_E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_E)
  2473. #define DUK_HTHREAD_STRING_E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_E)
  2474. #define DUK_HEAP_STRING_MESSAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MESSAGE)
  2475. #define DUK_HTHREAD_STRING_MESSAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MESSAGE)
  2476. #define DUK_HEAP_STRING_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAME)
  2477. #define DUK_HTHREAD_STRING_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAME)
  2478. #define DUK_HEAP_STRING_INPUT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INPUT)
  2479. #define DUK_HTHREAD_STRING_INPUT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INPUT)
  2480. #define DUK_HEAP_STRING_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INDEX)
  2481. #define DUK_HTHREAD_STRING_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INDEX)
  2482. #define DUK_HEAP_STRING_ESCAPED_EMPTY_REGEXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ESCAPED_EMPTY_REGEXP)
  2483. #define DUK_HTHREAD_STRING_ESCAPED_EMPTY_REGEXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ESCAPED_EMPTY_REGEXP)
  2484. #define DUK_HEAP_STRING_LAST_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LAST_INDEX)
  2485. #define DUK_HTHREAD_STRING_LAST_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LAST_INDEX)
  2486. #define DUK_HEAP_STRING_MULTILINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MULTILINE)
  2487. #define DUK_HTHREAD_STRING_MULTILINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MULTILINE)
  2488. #define DUK_HEAP_STRING_IGNORE_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IGNORE_CASE)
  2489. #define DUK_HTHREAD_STRING_IGNORE_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IGNORE_CASE)
  2490. #define DUK_HEAP_STRING_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SOURCE)
  2491. #define DUK_HTHREAD_STRING_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SOURCE)
  2492. #define DUK_HEAP_STRING_TEST(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TEST)
  2493. #define DUK_HTHREAD_STRING_TEST(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TEST)
  2494. #define DUK_HEAP_STRING_EXEC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXEC)
  2495. #define DUK_HTHREAD_STRING_EXEC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXEC)
  2496. #define DUK_HEAP_STRING_TO_GMT_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_GMT_STRING)
  2497. #define DUK_HTHREAD_STRING_TO_GMT_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_GMT_STRING)
  2498. #define DUK_HEAP_STRING_SET_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_YEAR)
  2499. #define DUK_HTHREAD_STRING_SET_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_YEAR)
  2500. #define DUK_HEAP_STRING_GET_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_YEAR)
  2501. #define DUK_HTHREAD_STRING_GET_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_YEAR)
  2502. #define DUK_HEAP_STRING_TO_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_JSON)
  2503. #define DUK_HTHREAD_STRING_TO_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_JSON)
  2504. #define DUK_HEAP_STRING_TO_ISO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_ISO_STRING)
  2505. #define DUK_HTHREAD_STRING_TO_ISO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_ISO_STRING)
  2506. #define DUK_HEAP_STRING_TO_UTC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_UTC_STRING)
  2507. #define DUK_HTHREAD_STRING_TO_UTC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_UTC_STRING)
  2508. #define DUK_HEAP_STRING_SET_UTC_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_FULL_YEAR)
  2509. #define DUK_HTHREAD_STRING_SET_UTC_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_FULL_YEAR)
  2510. #define DUK_HEAP_STRING_SET_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FULL_YEAR)
  2511. #define DUK_HTHREAD_STRING_SET_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FULL_YEAR)
  2512. #define DUK_HEAP_STRING_SET_UTC_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MONTH)
  2513. #define DUK_HTHREAD_STRING_SET_UTC_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MONTH)
  2514. #define DUK_HEAP_STRING_SET_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MONTH)
  2515. #define DUK_HTHREAD_STRING_SET_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MONTH)
  2516. #define DUK_HEAP_STRING_SET_UTC_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_DATE)
  2517. #define DUK_HTHREAD_STRING_SET_UTC_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_DATE)
  2518. #define DUK_HEAP_STRING_SET_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_DATE)
  2519. #define DUK_HTHREAD_STRING_SET_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_DATE)
  2520. #define DUK_HEAP_STRING_SET_UTC_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_HOURS)
  2521. #define DUK_HTHREAD_STRING_SET_UTC_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_HOURS)
  2522. #define DUK_HEAP_STRING_SET_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_HOURS)
  2523. #define DUK_HTHREAD_STRING_SET_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_HOURS)
  2524. #define DUK_HEAP_STRING_SET_UTC_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MINUTES)
  2525. #define DUK_HTHREAD_STRING_SET_UTC_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MINUTES)
  2526. #define DUK_HEAP_STRING_SET_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MINUTES)
  2527. #define DUK_HTHREAD_STRING_SET_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MINUTES)
  2528. #define DUK_HEAP_STRING_SET_UTC_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_SECONDS)
  2529. #define DUK_HTHREAD_STRING_SET_UTC_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_SECONDS)
  2530. #define DUK_HEAP_STRING_SET_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_SECONDS)
  2531. #define DUK_HTHREAD_STRING_SET_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_SECONDS)
  2532. #define DUK_HEAP_STRING_SET_UTC_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MILLISECONDS)
  2533. #define DUK_HTHREAD_STRING_SET_UTC_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MILLISECONDS)
  2534. #define DUK_HEAP_STRING_SET_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MILLISECONDS)
  2535. #define DUK_HTHREAD_STRING_SET_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MILLISECONDS)
  2536. #define DUK_HEAP_STRING_SET_TIME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_TIME)
  2537. #define DUK_HTHREAD_STRING_SET_TIME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_TIME)
  2538. #define DUK_HEAP_STRING_GET_TIMEZONE_OFFSET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_TIMEZONE_OFFSET)
  2539. #define DUK_HTHREAD_STRING_GET_TIMEZONE_OFFSET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_TIMEZONE_OFFSET)
  2540. #define DUK_HEAP_STRING_GET_UTC_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MILLISECONDS)
  2541. #define DUK_HTHREAD_STRING_GET_UTC_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MILLISECONDS)
  2542. #define DUK_HEAP_STRING_GET_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MILLISECONDS)
  2543. #define DUK_HTHREAD_STRING_GET_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MILLISECONDS)
  2544. #define DUK_HEAP_STRING_GET_UTC_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_SECONDS)
  2545. #define DUK_HTHREAD_STRING_GET_UTC_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_SECONDS)
  2546. #define DUK_HEAP_STRING_GET_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_SECONDS)
  2547. #define DUK_HTHREAD_STRING_GET_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_SECONDS)
  2548. #define DUK_HEAP_STRING_GET_UTC_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MINUTES)
  2549. #define DUK_HTHREAD_STRING_GET_UTC_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MINUTES)
  2550. #define DUK_HEAP_STRING_GET_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MINUTES)
  2551. #define DUK_HTHREAD_STRING_GET_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MINUTES)
  2552. #define DUK_HEAP_STRING_GET_UTC_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_HOURS)
  2553. #define DUK_HTHREAD_STRING_GET_UTC_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_HOURS)
  2554. #define DUK_HEAP_STRING_GET_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_HOURS)
  2555. #define DUK_HTHREAD_STRING_GET_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_HOURS)
  2556. #define DUK_HEAP_STRING_GET_UTC_DAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_DAY)
  2557. #define DUK_HTHREAD_STRING_GET_UTC_DAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_DAY)
  2558. #define DUK_HEAP_STRING_GET_DAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_DAY)
  2559. #define DUK_HTHREAD_STRING_GET_DAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_DAY)
  2560. #define DUK_HEAP_STRING_GET_UTC_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_DATE)
  2561. #define DUK_HTHREAD_STRING_GET_UTC_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_DATE)
  2562. #define DUK_HEAP_STRING_GET_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_DATE)
  2563. #define DUK_HTHREAD_STRING_GET_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_DATE)
  2564. #define DUK_HEAP_STRING_GET_UTC_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MONTH)
  2565. #define DUK_HTHREAD_STRING_GET_UTC_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MONTH)
  2566. #define DUK_HEAP_STRING_GET_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MONTH)
  2567. #define DUK_HTHREAD_STRING_GET_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MONTH)
  2568. #define DUK_HEAP_STRING_GET_UTC_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_FULL_YEAR)
  2569. #define DUK_HTHREAD_STRING_GET_UTC_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_FULL_YEAR)
  2570. #define DUK_HEAP_STRING_GET_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FULL_YEAR)
  2571. #define DUK_HTHREAD_STRING_GET_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FULL_YEAR)
  2572. #define DUK_HEAP_STRING_GET_TIME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_TIME)
  2573. #define DUK_HTHREAD_STRING_GET_TIME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_TIME)
  2574. #define DUK_HEAP_STRING_TO_LOCALE_TIME_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_TIME_STRING)
  2575. #define DUK_HTHREAD_STRING_TO_LOCALE_TIME_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_TIME_STRING)
  2576. #define DUK_HEAP_STRING_TO_LOCALE_DATE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_DATE_STRING)
  2577. #define DUK_HTHREAD_STRING_TO_LOCALE_DATE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_DATE_STRING)
  2578. #define DUK_HEAP_STRING_TO_TIME_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_TIME_STRING)
  2579. #define DUK_HTHREAD_STRING_TO_TIME_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_TIME_STRING)
  2580. #define DUK_HEAP_STRING_TO_DATE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_DATE_STRING)
  2581. #define DUK_HTHREAD_STRING_TO_DATE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_DATE_STRING)
  2582. #define DUK_HEAP_STRING_NOW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NOW)
  2583. #define DUK_HTHREAD_STRING_NOW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NOW)
  2584. #define DUK_HEAP_STRING_UTC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UTC)
  2585. #define DUK_HTHREAD_STRING_UTC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UTC)
  2586. #define DUK_HEAP_STRING_PARSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE)
  2587. #define DUK_HTHREAD_STRING_PARSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE)
  2588. #define DUK_HEAP_STRING_TO_PRECISION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_PRECISION)
  2589. #define DUK_HTHREAD_STRING_TO_PRECISION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_PRECISION)
  2590. #define DUK_HEAP_STRING_TO_EXPONENTIAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_EXPONENTIAL)
  2591. #define DUK_HTHREAD_STRING_TO_EXPONENTIAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_EXPONENTIAL)
  2592. #define DUK_HEAP_STRING_TO_FIXED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_FIXED)
  2593. #define DUK_HTHREAD_STRING_TO_FIXED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_FIXED)
  2594. #define DUK_HEAP_STRING_POSITIVE_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POSITIVE_INFINITY)
  2595. #define DUK_HTHREAD_STRING_POSITIVE_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POSITIVE_INFINITY)
  2596. #define DUK_HEAP_STRING_NEGATIVE_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEGATIVE_INFINITY)
  2597. #define DUK_HTHREAD_STRING_NEGATIVE_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEGATIVE_INFINITY)
  2598. #define DUK_HEAP_STRING_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAN)
  2599. #define DUK_HTHREAD_STRING_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAN)
  2600. #define DUK_HEAP_STRING_MIN_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MIN_VALUE)
  2601. #define DUK_HTHREAD_STRING_MIN_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MIN_VALUE)
  2602. #define DUK_HEAP_STRING_MAX_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAX_VALUE)
  2603. #define DUK_HTHREAD_STRING_MAX_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAX_VALUE)
  2604. #define DUK_HEAP_STRING_SUBSTR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBSTR)
  2605. #define DUK_HTHREAD_STRING_SUBSTR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBSTR)
  2606. #define DUK_HEAP_STRING_TRIM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRIM)
  2607. #define DUK_HTHREAD_STRING_TRIM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRIM)
  2608. #define DUK_HEAP_STRING_TO_LOCALE_UPPER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_UPPER_CASE)
  2609. #define DUK_HTHREAD_STRING_TO_LOCALE_UPPER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_UPPER_CASE)
  2610. #define DUK_HEAP_STRING_TO_UPPER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_UPPER_CASE)
  2611. #define DUK_HTHREAD_STRING_TO_UPPER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_UPPER_CASE)
  2612. #define DUK_HEAP_STRING_TO_LOCALE_LOWER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_LOWER_CASE)
  2613. #define DUK_HTHREAD_STRING_TO_LOCALE_LOWER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_LOWER_CASE)
  2614. #define DUK_HEAP_STRING_TO_LOWER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOWER_CASE)
  2615. #define DUK_HTHREAD_STRING_TO_LOWER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOWER_CASE)
  2616. #define DUK_HEAP_STRING_SUBSTRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBSTRING)
  2617. #define DUK_HTHREAD_STRING_SUBSTRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBSTRING)
  2618. #define DUK_HEAP_STRING_SPLIT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPLIT)
  2619. #define DUK_HTHREAD_STRING_SPLIT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPLIT)
  2620. #define DUK_HEAP_STRING_SEARCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SEARCH)
  2621. #define DUK_HTHREAD_STRING_SEARCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SEARCH)
  2622. #define DUK_HEAP_STRING_REPLACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REPLACE)
  2623. #define DUK_HTHREAD_STRING_REPLACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REPLACE)
  2624. #define DUK_HEAP_STRING_MATCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MATCH)
  2625. #define DUK_HTHREAD_STRING_MATCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MATCH)
  2626. #define DUK_HEAP_STRING_LOCALE_COMPARE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOCALE_COMPARE)
  2627. #define DUK_HTHREAD_STRING_LOCALE_COMPARE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOCALE_COMPARE)
  2628. #define DUK_HEAP_STRING_CHAR_CODE_AT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CHAR_CODE_AT)
  2629. #define DUK_HTHREAD_STRING_CHAR_CODE_AT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CHAR_CODE_AT)
  2630. #define DUK_HEAP_STRING_CHAR_AT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CHAR_AT)
  2631. #define DUK_HTHREAD_STRING_CHAR_AT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CHAR_AT)
  2632. #define DUK_HEAP_STRING_FROM_CHAR_CODE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FROM_CHAR_CODE)
  2633. #define DUK_HTHREAD_STRING_FROM_CHAR_CODE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FROM_CHAR_CODE)
  2634. #define DUK_HEAP_STRING_REDUCE_RIGHT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REDUCE_RIGHT)
  2635. #define DUK_HTHREAD_STRING_REDUCE_RIGHT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REDUCE_RIGHT)
  2636. #define DUK_HEAP_STRING_REDUCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REDUCE)
  2637. #define DUK_HTHREAD_STRING_REDUCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REDUCE)
  2638. #define DUK_HEAP_STRING_FILTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILTER)
  2639. #define DUK_HTHREAD_STRING_FILTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILTER)
  2640. #define DUK_HEAP_STRING_MAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAP)
  2641. #define DUK_HTHREAD_STRING_MAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAP)
  2642. #define DUK_HEAP_STRING_FOR_EACH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FOR_EACH)
  2643. #define DUK_HTHREAD_STRING_FOR_EACH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FOR_EACH)
  2644. #define DUK_HEAP_STRING_SOME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SOME)
  2645. #define DUK_HTHREAD_STRING_SOME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SOME)
  2646. #define DUK_HEAP_STRING_EVERY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVERY)
  2647. #define DUK_HTHREAD_STRING_EVERY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVERY)
  2648. #define DUK_HEAP_STRING_LAST_INDEX_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LAST_INDEX_OF)
  2649. #define DUK_HTHREAD_STRING_LAST_INDEX_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LAST_INDEX_OF)
  2650. #define DUK_HEAP_STRING_INDEX_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INDEX_OF)
  2651. #define DUK_HTHREAD_STRING_INDEX_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INDEX_OF)
  2652. #define DUK_HEAP_STRING_UNSHIFT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UNSHIFT)
  2653. #define DUK_HTHREAD_STRING_UNSHIFT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UNSHIFT)
  2654. #define DUK_HEAP_STRING_SPLICE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPLICE)
  2655. #define DUK_HTHREAD_STRING_SPLICE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPLICE)
  2656. #define DUK_HEAP_STRING_SORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SORT)
  2657. #define DUK_HTHREAD_STRING_SORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SORT)
  2658. #define DUK_HEAP_STRING_SLICE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SLICE)
  2659. #define DUK_HTHREAD_STRING_SLICE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SLICE)
  2660. #define DUK_HEAP_STRING_SHIFT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SHIFT)
  2661. #define DUK_HTHREAD_STRING_SHIFT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SHIFT)
  2662. #define DUK_HEAP_STRING_REVERSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REVERSE)
  2663. #define DUK_HTHREAD_STRING_REVERSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REVERSE)
  2664. #define DUK_HEAP_STRING_PUSH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PUSH)
  2665. #define DUK_HTHREAD_STRING_PUSH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PUSH)
  2666. #define DUK_HEAP_STRING_POP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POP)
  2667. #define DUK_HTHREAD_STRING_POP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POP)
  2668. #define DUK_HEAP_STRING_JOIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JOIN)
  2669. #define DUK_HTHREAD_STRING_JOIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JOIN)
  2670. #define DUK_HEAP_STRING_CONCAT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONCAT)
  2671. #define DUK_HTHREAD_STRING_CONCAT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONCAT)
  2672. #define DUK_HEAP_STRING_IS_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_ARRAY)
  2673. #define DUK_HTHREAD_STRING_IS_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_ARRAY)
  2674. #define DUK_HEAP_STRING_LC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ARGUMENTS)
  2675. #define DUK_HTHREAD_STRING_LC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ARGUMENTS)
  2676. #define DUK_HEAP_STRING_CALLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLER)
  2677. #define DUK_HTHREAD_STRING_CALLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLER)
  2678. #define DUK_HEAP_STRING_BIND(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BIND)
  2679. #define DUK_HTHREAD_STRING_BIND(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BIND)
  2680. #define DUK_HEAP_STRING_CALL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALL)
  2681. #define DUK_HTHREAD_STRING_CALL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALL)
  2682. #define DUK_HEAP_STRING_APPLY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_APPLY)
  2683. #define DUK_HTHREAD_STRING_APPLY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_APPLY)
  2684. #define DUK_HEAP_STRING_PROPERTY_IS_ENUMERABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROPERTY_IS_ENUMERABLE)
  2685. #define DUK_HTHREAD_STRING_PROPERTY_IS_ENUMERABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROPERTY_IS_ENUMERABLE)
  2686. #define DUK_HEAP_STRING_IS_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_PROTOTYPE_OF)
  2687. #define DUK_HTHREAD_STRING_IS_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_PROTOTYPE_OF)
  2688. #define DUK_HEAP_STRING_HAS_OWN_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HAS_OWN_PROPERTY)
  2689. #define DUK_HTHREAD_STRING_HAS_OWN_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HAS_OWN_PROPERTY)
  2690. #define DUK_HEAP_STRING_VALUE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE_OF)
  2691. #define DUK_HTHREAD_STRING_VALUE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE_OF)
  2692. #define DUK_HEAP_STRING_TO_LOCALE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_STRING)
  2693. #define DUK_HTHREAD_STRING_TO_LOCALE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_STRING)
  2694. #define DUK_HEAP_STRING_TO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_STRING)
  2695. #define DUK_HTHREAD_STRING_TO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_STRING)
  2696. #define DUK_HEAP_STRING_CONSTRUCTOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONSTRUCTOR)
  2697. #define DUK_HTHREAD_STRING_CONSTRUCTOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONSTRUCTOR)
  2698. #define DUK_HEAP_STRING_SET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET)
  2699. #define DUK_HTHREAD_STRING_SET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET)
  2700. #define DUK_HEAP_STRING_GET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET)
  2701. #define DUK_HTHREAD_STRING_GET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET)
  2702. #define DUK_HEAP_STRING_ENUMERABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERABLE)
  2703. #define DUK_HTHREAD_STRING_ENUMERABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERABLE)
  2704. #define DUK_HEAP_STRING_CONFIGURABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONFIGURABLE)
  2705. #define DUK_HTHREAD_STRING_CONFIGURABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONFIGURABLE)
  2706. #define DUK_HEAP_STRING_WRITABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITABLE)
  2707. #define DUK_HTHREAD_STRING_WRITABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITABLE)
  2708. #define DUK_HEAP_STRING_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE)
  2709. #define DUK_HTHREAD_STRING_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE)
  2710. #define DUK_HEAP_STRING_KEYS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_KEYS)
  2711. #define DUK_HTHREAD_STRING_KEYS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_KEYS)
  2712. #define DUK_HEAP_STRING_IS_EXTENSIBLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_EXTENSIBLE)
  2713. #define DUK_HTHREAD_STRING_IS_EXTENSIBLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_EXTENSIBLE)
  2714. #define DUK_HEAP_STRING_IS_FROZEN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_FROZEN)
  2715. #define DUK_HTHREAD_STRING_IS_FROZEN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_FROZEN)
  2716. #define DUK_HEAP_STRING_IS_SEALED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_SEALED)
  2717. #define DUK_HTHREAD_STRING_IS_SEALED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_SEALED)
  2718. #define DUK_HEAP_STRING_PREVENT_EXTENSIONS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PREVENT_EXTENSIONS)
  2719. #define DUK_HTHREAD_STRING_PREVENT_EXTENSIONS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PREVENT_EXTENSIONS)
  2720. #define DUK_HEAP_STRING_FREEZE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FREEZE)
  2721. #define DUK_HTHREAD_STRING_FREEZE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FREEZE)
  2722. #define DUK_HEAP_STRING_SEAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SEAL)
  2723. #define DUK_HTHREAD_STRING_SEAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SEAL)
  2724. #define DUK_HEAP_STRING_DEFINE_PROPERTIES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFINE_PROPERTIES)
  2725. #define DUK_HTHREAD_STRING_DEFINE_PROPERTIES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFINE_PROPERTIES)
  2726. #define DUK_HEAP_STRING_DEFINE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFINE_PROPERTY)
  2727. #define DUK_HTHREAD_STRING_DEFINE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFINE_PROPERTY)
  2728. #define DUK_HEAP_STRING_CREATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CREATE)
  2729. #define DUK_HTHREAD_STRING_CREATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CREATE)
  2730. #define DUK_HEAP_STRING_GET_OWN_PROPERTY_NAMES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_OWN_PROPERTY_NAMES)
  2731. #define DUK_HTHREAD_STRING_GET_OWN_PROPERTY_NAMES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_OWN_PROPERTY_NAMES)
  2732. #define DUK_HEAP_STRING_GET_OWN_PROPERTY_DESCRIPTOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR)
  2733. #define DUK_HTHREAD_STRING_GET_OWN_PROPERTY_DESCRIPTOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR)
  2734. #define DUK_HEAP_STRING_GET_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_PROTOTYPE_OF)
  2735. #define DUK_HTHREAD_STRING_GET_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_PROTOTYPE_OF)
  2736. #define DUK_HEAP_STRING_PROTOTYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTOTYPE)
  2737. #define DUK_HTHREAD_STRING_PROTOTYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTOTYPE)
  2738. #define DUK_HEAP_STRING_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LENGTH)
  2739. #define DUK_HTHREAD_STRING_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LENGTH)
  2740. #define DUK_HEAP_STRING_ALERT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ALERT)
  2741. #define DUK_HTHREAD_STRING_ALERT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ALERT)
  2742. #define DUK_HEAP_STRING_PRINT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PRINT)
  2743. #define DUK_HTHREAD_STRING_PRINT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PRINT)
  2744. #define DUK_HEAP_STRING_UNESCAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UNESCAPE)
  2745. #define DUK_HTHREAD_STRING_UNESCAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UNESCAPE)
  2746. #define DUK_HEAP_STRING_ESCAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ESCAPE)
  2747. #define DUK_HTHREAD_STRING_ESCAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ESCAPE)
  2748. #define DUK_HEAP_STRING_ENCODE_URI_COMPONENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENCODE_URI_COMPONENT)
  2749. #define DUK_HTHREAD_STRING_ENCODE_URI_COMPONENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENCODE_URI_COMPONENT)
  2750. #define DUK_HEAP_STRING_ENCODE_URI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENCODE_URI)
  2751. #define DUK_HTHREAD_STRING_ENCODE_URI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENCODE_URI)
  2752. #define DUK_HEAP_STRING_DECODE_URI_COMPONENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DECODE_URI_COMPONENT)
  2753. #define DUK_HTHREAD_STRING_DECODE_URI_COMPONENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DECODE_URI_COMPONENT)
  2754. #define DUK_HEAP_STRING_DECODE_URI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DECODE_URI)
  2755. #define DUK_HTHREAD_STRING_DECODE_URI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DECODE_URI)
  2756. #define DUK_HEAP_STRING_IS_FINITE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_FINITE)
  2757. #define DUK_HTHREAD_STRING_IS_FINITE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_FINITE)
  2758. #define DUK_HEAP_STRING_IS_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_NAN)
  2759. #define DUK_HTHREAD_STRING_IS_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_NAN)
  2760. #define DUK_HEAP_STRING_PARSE_FLOAT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE_FLOAT)
  2761. #define DUK_HTHREAD_STRING_PARSE_FLOAT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE_FLOAT)
  2762. #define DUK_HEAP_STRING_PARSE_INT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE_INT)
  2763. #define DUK_HTHREAD_STRING_PARSE_INT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE_INT)
  2764. #define DUK_HEAP_STRING_EVAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVAL)
  2765. #define DUK_HTHREAD_STRING_EVAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVAL)
  2766. #define DUK_HEAP_STRING_URI_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_URI_ERROR)
  2767. #define DUK_HTHREAD_STRING_URI_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_URI_ERROR)
  2768. #define DUK_HEAP_STRING_TYPE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPE_ERROR)
  2769. #define DUK_HTHREAD_STRING_TYPE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPE_ERROR)
  2770. #define DUK_HEAP_STRING_SYNTAX_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SYNTAX_ERROR)
  2771. #define DUK_HTHREAD_STRING_SYNTAX_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SYNTAX_ERROR)
  2772. #define DUK_HEAP_STRING_REFERENCE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REFERENCE_ERROR)
  2773. #define DUK_HTHREAD_STRING_REFERENCE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REFERENCE_ERROR)
  2774. #define DUK_HEAP_STRING_RANGE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RANGE_ERROR)
  2775. #define DUK_HTHREAD_STRING_RANGE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RANGE_ERROR)
  2776. #define DUK_HEAP_STRING_EVAL_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVAL_ERROR)
  2777. #define DUK_HTHREAD_STRING_EVAL_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVAL_ERROR)
  2778. #define DUK_HEAP_STRING_BREAK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BREAK)
  2779. #define DUK_HTHREAD_STRING_BREAK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BREAK)
  2780. #define DUK_HEAP_STRING_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CASE)
  2781. #define DUK_HTHREAD_STRING_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CASE)
  2782. #define DUK_HEAP_STRING_CATCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CATCH)
  2783. #define DUK_HTHREAD_STRING_CATCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CATCH)
  2784. #define DUK_HEAP_STRING_CONTINUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONTINUE)
  2785. #define DUK_HTHREAD_STRING_CONTINUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONTINUE)
  2786. #define DUK_HEAP_STRING_DEBUGGER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEBUGGER)
  2787. #define DUK_HTHREAD_STRING_DEBUGGER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEBUGGER)
  2788. #define DUK_HEAP_STRING_DEFAULT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFAULT)
  2789. #define DUK_HTHREAD_STRING_DEFAULT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFAULT)
  2790. #define DUK_HEAP_STRING_DELETE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE)
  2791. #define DUK_HTHREAD_STRING_DELETE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE)
  2792. #define DUK_HEAP_STRING_DO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DO)
  2793. #define DUK_HTHREAD_STRING_DO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DO)
  2794. #define DUK_HEAP_STRING_ELSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ELSE)
  2795. #define DUK_HTHREAD_STRING_ELSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ELSE)
  2796. #define DUK_HEAP_STRING_FINALLY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FINALLY)
  2797. #define DUK_HTHREAD_STRING_FINALLY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FINALLY)
  2798. #define DUK_HEAP_STRING_FOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FOR)
  2799. #define DUK_HTHREAD_STRING_FOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FOR)
  2800. #define DUK_HEAP_STRING_LC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FUNCTION)
  2801. #define DUK_HTHREAD_STRING_LC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FUNCTION)
  2802. #define DUK_HEAP_STRING_IF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IF)
  2803. #define DUK_HTHREAD_STRING_IF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IF)
  2804. #define DUK_HEAP_STRING_IN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IN)
  2805. #define DUK_HTHREAD_STRING_IN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IN)
  2806. #define DUK_HEAP_STRING_INSTANCEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INSTANCEOF)
  2807. #define DUK_HTHREAD_STRING_INSTANCEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INSTANCEOF)
  2808. #define DUK_HEAP_STRING_NEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEW)
  2809. #define DUK_HTHREAD_STRING_NEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEW)
  2810. #define DUK_HEAP_STRING_RETURN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RETURN)
  2811. #define DUK_HTHREAD_STRING_RETURN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RETURN)
  2812. #define DUK_HEAP_STRING_SWITCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SWITCH)
  2813. #define DUK_HTHREAD_STRING_SWITCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SWITCH)
  2814. #define DUK_HEAP_STRING_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THIS)
  2815. #define DUK_HTHREAD_STRING_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THIS)
  2816. #define DUK_HEAP_STRING_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THROW)
  2817. #define DUK_HTHREAD_STRING_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THROW)
  2818. #define DUK_HEAP_STRING_TRY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRY)
  2819. #define DUK_HTHREAD_STRING_TRY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRY)
  2820. #define DUK_HEAP_STRING_TYPEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPEOF)
  2821. #define DUK_HTHREAD_STRING_TYPEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPEOF)
  2822. #define DUK_HEAP_STRING_VAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VAR)
  2823. #define DUK_HTHREAD_STRING_VAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VAR)
  2824. #define DUK_HEAP_STRING_CONST(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONST)
  2825. #define DUK_HTHREAD_STRING_CONST(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONST)
  2826. #define DUK_HEAP_STRING_VOID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VOID)
  2827. #define DUK_HTHREAD_STRING_VOID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VOID)
  2828. #define DUK_HEAP_STRING_WHILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WHILE)
  2829. #define DUK_HTHREAD_STRING_WHILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WHILE)
  2830. #define DUK_HEAP_STRING_WITH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WITH)
  2831. #define DUK_HTHREAD_STRING_WITH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WITH)
  2832. #define DUK_HEAP_STRING_CLASS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLASS)
  2833. #define DUK_HTHREAD_STRING_CLASS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLASS)
  2834. #define DUK_HEAP_STRING_ENUM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUM)
  2835. #define DUK_HTHREAD_STRING_ENUM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUM)
  2836. #define DUK_HEAP_STRING_EXPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORT)
  2837. #define DUK_HTHREAD_STRING_EXPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORT)
  2838. #define DUK_HEAP_STRING_EXTENDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXTENDS)
  2839. #define DUK_HTHREAD_STRING_EXTENDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXTENDS)
  2840. #define DUK_HEAP_STRING_IMPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPORT)
  2841. #define DUK_HTHREAD_STRING_IMPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPORT)
  2842. #define DUK_HEAP_STRING_SUPER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUPER)
  2843. #define DUK_HTHREAD_STRING_SUPER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUPER)
  2844. #define DUK_HEAP_STRING_LC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NULL)
  2845. #define DUK_HTHREAD_STRING_LC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NULL)
  2846. #define DUK_HEAP_STRING_TRUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRUE)
  2847. #define DUK_HTHREAD_STRING_TRUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRUE)
  2848. #define DUK_HEAP_STRING_FALSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FALSE)
  2849. #define DUK_HTHREAD_STRING_FALSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FALSE)
  2850. #define DUK_HEAP_STRING_IMPLEMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPLEMENTS)
  2851. #define DUK_HTHREAD_STRING_IMPLEMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPLEMENTS)
  2852. #define DUK_HEAP_STRING_INTERFACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INTERFACE)
  2853. #define DUK_HTHREAD_STRING_INTERFACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INTERFACE)
  2854. #define DUK_HEAP_STRING_LET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LET)
  2855. #define DUK_HTHREAD_STRING_LET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LET)
  2856. #define DUK_HEAP_STRING_PACKAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PACKAGE)
  2857. #define DUK_HTHREAD_STRING_PACKAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PACKAGE)
  2858. #define DUK_HEAP_STRING_PRIVATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PRIVATE)
  2859. #define DUK_HTHREAD_STRING_PRIVATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PRIVATE)
  2860. #define DUK_HEAP_STRING_PROTECTED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTECTED)
  2861. #define DUK_HTHREAD_STRING_PROTECTED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTECTED)
  2862. #define DUK_HEAP_STRING_PUBLIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PUBLIC)
  2863. #define DUK_HTHREAD_STRING_PUBLIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PUBLIC)
  2864. #define DUK_HEAP_STRING_STATIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STATIC)
  2865. #define DUK_HTHREAD_STRING_STATIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STATIC)
  2866. #define DUK_HEAP_STRING_YIELD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_YIELD)
  2867. #define DUK_HTHREAD_STRING_YIELD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_YIELD)
  2868. #define DUK_HEAP_NUM_STRINGS 414
  2869. #define DUK_STRIDX_START_RESERVED 369
  2870. #define DUK_STRIDX_START_STRICT_RESERVED 405
  2871. #define DUK_STRIDX_END_RESERVED 414 /* exclusive endpoint */
  2872. #if !defined(DUK_SINGLE_FILE)
  2873. DUK_INTERNAL_DECL const duk_c_function duk_bi_native_functions[147];
  2874. DUK_INTERNAL_DECL const duk_uint8_t duk_builtins_data[1952];
  2875. #ifdef DUK_USE_BUILTIN_INITJS
  2876. DUK_INTERNAL_DECL const duk_uint8_t duk_initjs_data[1757];
  2877. #endif /* DUK_USE_BUILTIN_INITJS */
  2878. #endif /* !DUK_SINGLE_FILE */
  2879. #define DUK_BUILTINS_DATA_LENGTH 1952
  2880. #ifdef DUK_USE_BUILTIN_INITJS
  2881. #define DUK_BUILTIN_INITJS_DATA_LENGTH 1757
  2882. #endif /* DUK_USE_BUILTIN_INITJS */
  2883. #define DUK_BIDX_GLOBAL 0
  2884. #define DUK_BIDX_GLOBAL_ENV 1
  2885. #define DUK_BIDX_OBJECT_CONSTRUCTOR 2
  2886. #define DUK_BIDX_OBJECT_PROTOTYPE 3
  2887. #define DUK_BIDX_FUNCTION_CONSTRUCTOR 4
  2888. #define DUK_BIDX_FUNCTION_PROTOTYPE 5
  2889. #define DUK_BIDX_ARRAY_CONSTRUCTOR 6
  2890. #define DUK_BIDX_ARRAY_PROTOTYPE 7
  2891. #define DUK_BIDX_STRING_CONSTRUCTOR 8
  2892. #define DUK_BIDX_STRING_PROTOTYPE 9
  2893. #define DUK_BIDX_BOOLEAN_CONSTRUCTOR 10
  2894. #define DUK_BIDX_BOOLEAN_PROTOTYPE 11
  2895. #define DUK_BIDX_NUMBER_CONSTRUCTOR 12
  2896. #define DUK_BIDX_NUMBER_PROTOTYPE 13
  2897. #define DUK_BIDX_DATE_CONSTRUCTOR 14
  2898. #define DUK_BIDX_DATE_PROTOTYPE 15
  2899. #define DUK_BIDX_REGEXP_CONSTRUCTOR 16
  2900. #define DUK_BIDX_REGEXP_PROTOTYPE 17
  2901. #define DUK_BIDX_ERROR_CONSTRUCTOR 18
  2902. #define DUK_BIDX_ERROR_PROTOTYPE 19
  2903. #define DUK_BIDX_EVAL_ERROR_CONSTRUCTOR 20
  2904. #define DUK_BIDX_EVAL_ERROR_PROTOTYPE 21
  2905. #define DUK_BIDX_RANGE_ERROR_CONSTRUCTOR 22
  2906. #define DUK_BIDX_RANGE_ERROR_PROTOTYPE 23
  2907. #define DUK_BIDX_REFERENCE_ERROR_CONSTRUCTOR 24
  2908. #define DUK_BIDX_REFERENCE_ERROR_PROTOTYPE 25
  2909. #define DUK_BIDX_SYNTAX_ERROR_CONSTRUCTOR 26
  2910. #define DUK_BIDX_SYNTAX_ERROR_PROTOTYPE 27
  2911. #define DUK_BIDX_TYPE_ERROR_CONSTRUCTOR 28
  2912. #define DUK_BIDX_TYPE_ERROR_PROTOTYPE 29
  2913. #define DUK_BIDX_URI_ERROR_CONSTRUCTOR 30
  2914. #define DUK_BIDX_URI_ERROR_PROTOTYPE 31
  2915. #define DUK_BIDX_MATH 32
  2916. #define DUK_BIDX_JSON 33
  2917. #define DUK_BIDX_TYPE_ERROR_THROWER 34
  2918. #define DUK_BIDX_PROXY_CONSTRUCTOR 35
  2919. #define DUK_BIDX_DUKTAPE 36
  2920. #define DUK_BIDX_THREAD_CONSTRUCTOR 37
  2921. #define DUK_BIDX_THREAD_PROTOTYPE 38
  2922. #define DUK_BIDX_BUFFER_CONSTRUCTOR 39
  2923. #define DUK_BIDX_BUFFER_PROTOTYPE 40
  2924. #define DUK_BIDX_POINTER_CONSTRUCTOR 41
  2925. #define DUK_BIDX_POINTER_PROTOTYPE 42
  2926. #define DUK_BIDX_LOGGER_CONSTRUCTOR 43
  2927. #define DUK_BIDX_LOGGER_PROTOTYPE 44
  2928. #define DUK_BIDX_DOUBLE_ERROR 45
  2929. #define DUK_BIDX_ARRAYBUFFER_CONSTRUCTOR 46
  2930. #define DUK_BIDX_ARRAYBUFFER_PROTOTYPE 47
  2931. #define DUK_BIDX_DATAVIEW_CONSTRUCTOR 48
  2932. #define DUK_BIDX_DATAVIEW_PROTOTYPE 49
  2933. #define DUK_BIDX_TYPEDARRAY_PROTOTYPE 50
  2934. #define DUK_BIDX_INT8ARRAY_CONSTRUCTOR 51
  2935. #define DUK_BIDX_INT8ARRAY_PROTOTYPE 52
  2936. #define DUK_BIDX_UINT8ARRAY_CONSTRUCTOR 53
  2937. #define DUK_BIDX_UINT8ARRAY_PROTOTYPE 54
  2938. #define DUK_BIDX_UINT8CLAMPEDARRAY_CONSTRUCTOR 55
  2939. #define DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE 56
  2940. #define DUK_BIDX_INT16ARRAY_CONSTRUCTOR 57
  2941. #define DUK_BIDX_INT16ARRAY_PROTOTYPE 58
  2942. #define DUK_BIDX_UINT16ARRAY_CONSTRUCTOR 59
  2943. #define DUK_BIDX_UINT16ARRAY_PROTOTYPE 60
  2944. #define DUK_BIDX_INT32ARRAY_CONSTRUCTOR 61
  2945. #define DUK_BIDX_INT32ARRAY_PROTOTYPE 62
  2946. #define DUK_BIDX_UINT32ARRAY_CONSTRUCTOR 63
  2947. #define DUK_BIDX_UINT32ARRAY_PROTOTYPE 64
  2948. #define DUK_BIDX_FLOAT32ARRAY_CONSTRUCTOR 65
  2949. #define DUK_BIDX_FLOAT32ARRAY_PROTOTYPE 66
  2950. #define DUK_BIDX_FLOAT64ARRAY_CONSTRUCTOR 67
  2951. #define DUK_BIDX_FLOAT64ARRAY_PROTOTYPE 68
  2952. #define DUK_BIDX_NODEJS_BUFFER_CONSTRUCTOR 69
  2953. #define DUK_BIDX_NODEJS_BUFFER_PROTOTYPE 70
  2954. #define DUK_NUM_BUILTINS 71
  2955. #elif defined(DUK_USE_DOUBLE_ME)
  2956. #if !defined(DUK_SINGLE_FILE)
  2957. DUK_INTERNAL_DECL const duk_uint8_t duk_strings_data[2624];
  2958. #endif /* !DUK_SINGLE_FILE */
  2959. #define DUK_STRDATA_DATA_LENGTH 2624
  2960. #define DUK_STRDATA_MAX_STRLEN 24
  2961. #define DUK_STRIDX_UC_LOGGER 0 /* 'Logger' */
  2962. #define DUK_STRIDX_UC_THREAD 1 /* 'Thread' */
  2963. #define DUK_STRIDX_UC_POINTER 2 /* 'Pointer' */
  2964. #define DUK_STRIDX_DEC_ENV 3 /* 'DecEnv' */
  2965. #define DUK_STRIDX_OBJ_ENV 4 /* 'ObjEnv' */
  2966. #define DUK_STRIDX_FLOAT64_ARRAY 5 /* 'Float64Array' */
  2967. #define DUK_STRIDX_FLOAT32_ARRAY 6 /* 'Float32Array' */
  2968. #define DUK_STRIDX_UINT32_ARRAY 7 /* 'Uint32Array' */
  2969. #define DUK_STRIDX_INT32_ARRAY 8 /* 'Int32Array' */
  2970. #define DUK_STRIDX_UINT16_ARRAY 9 /* 'Uint16Array' */
  2971. #define DUK_STRIDX_INT16_ARRAY 10 /* 'Int16Array' */
  2972. #define DUK_STRIDX_UINT8_CLAMPED_ARRAY 11 /* 'Uint8ClampedArray' */
  2973. #define DUK_STRIDX_UINT8_ARRAY 12 /* 'Uint8Array' */
  2974. #define DUK_STRIDX_INT8_ARRAY 13 /* 'Int8Array' */
  2975. #define DUK_STRIDX_DATA_VIEW 14 /* 'DataView' */
  2976. #define DUK_STRIDX_ARRAY_BUFFER 15 /* 'ArrayBuffer' */
  2977. #define DUK_STRIDX_UC_BUFFER 16 /* 'Buffer' */
  2978. #define DUK_STRIDX_EMPTY_STRING 17 /* '' */
  2979. #define DUK_STRIDX_GLOBAL 18 /* 'global' */
  2980. #define DUK_STRIDX_UC_ARGUMENTS 19 /* 'Arguments' */
  2981. #define DUK_STRIDX_JSON 20 /* 'JSON' */
  2982. #define DUK_STRIDX_MATH 21 /* 'Math' */
  2983. #define DUK_STRIDX_UC_ERROR 22 /* 'Error' */
  2984. #define DUK_STRIDX_REG_EXP 23 /* 'RegExp' */
  2985. #define DUK_STRIDX_DATE 24 /* 'Date' */
  2986. #define DUK_STRIDX_UC_NUMBER 25 /* 'Number' */
  2987. #define DUK_STRIDX_UC_BOOLEAN 26 /* 'Boolean' */
  2988. #define DUK_STRIDX_UC_STRING 27 /* 'String' */
  2989. #define DUK_STRIDX_ARRAY 28 /* 'Array' */
  2990. #define DUK_STRIDX_UC_FUNCTION 29 /* 'Function' */
  2991. #define DUK_STRIDX_UC_OBJECT 30 /* 'Object' */
  2992. #define DUK_STRIDX_UC_NULL 31 /* 'Null' */
  2993. #define DUK_STRIDX_UC_UNDEFINED 32 /* 'Undefined' */
  2994. #define DUK_STRIDX_JSON_EXT_FUNCTION2 33 /* '{_func:true}' */
  2995. #define DUK_STRIDX_JSON_EXT_FUNCTION1 34 /* '{"_func":true}' */
  2996. #define DUK_STRIDX_JSON_EXT_NEGINF 35 /* '{"_ninf":true}' */
  2997. #define DUK_STRIDX_JSON_EXT_POSINF 36 /* '{"_inf":true}' */
  2998. #define DUK_STRIDX_JSON_EXT_NAN 37 /* '{"_nan":true}' */
  2999. #define DUK_STRIDX_JSON_EXT_UNDEFINED 38 /* '{"_undef":true}' */
  3000. #define DUK_STRIDX_TO_LOG_STRING 39 /* 'toLogString' */
  3001. #define DUK_STRIDX_CLOG 40 /* 'clog' */
  3002. #define DUK_STRIDX_LC_L 41 /* 'l' */
  3003. #define DUK_STRIDX_LC_N 42 /* 'n' */
  3004. #define DUK_STRIDX_LC_FATAL 43 /* 'fatal' */
  3005. #define DUK_STRIDX_LC_ERROR 44 /* 'error' */
  3006. #define DUK_STRIDX_LC_WARN 45 /* 'warn' */
  3007. #define DUK_STRIDX_LC_DEBUG 46 /* 'debug' */
  3008. #define DUK_STRIDX_LC_TRACE 47 /* 'trace' */
  3009. #define DUK_STRIDX_RAW 48 /* 'raw' */
  3010. #define DUK_STRIDX_FMT 49 /* 'fmt' */
  3011. #define DUK_STRIDX_CURRENT 50 /* 'current' */
  3012. #define DUK_STRIDX_RESUME 51 /* 'resume' */
  3013. #define DUK_STRIDX_COMPACT 52 /* 'compact' */
  3014. #define DUK_STRIDX_JC 53 /* 'jc' */
  3015. #define DUK_STRIDX_JX 54 /* 'jx' */
  3016. #define DUK_STRIDX_BASE64 55 /* 'base64' */
  3017. #define DUK_STRIDX_HEX 56 /* 'hex' */
  3018. #define DUK_STRIDX_DEC 57 /* 'dec' */
  3019. #define DUK_STRIDX_ENC 58 /* 'enc' */
  3020. #define DUK_STRIDX_FIN 59 /* 'fin' */
  3021. #define DUK_STRIDX_GC 60 /* 'gc' */
  3022. #define DUK_STRIDX_ACT 61 /* 'act' */
  3023. #define DUK_STRIDX_LC_INFO 62 /* 'info' */
  3024. #define DUK_STRIDX_VERSION 63 /* 'version' */
  3025. #define DUK_STRIDX_ENV 64 /* 'env' */
  3026. #define DUK_STRIDX_MOD_LOADED 65 /* 'modLoaded' */
  3027. #define DUK_STRIDX_MOD_SEARCH 66 /* 'modSearch' */
  3028. #define DUK_STRIDX_ERR_THROW 67 /* 'errThrow' */
  3029. #define DUK_STRIDX_ERR_CREATE 68 /* 'errCreate' */
  3030. #define DUK_STRIDX_COMPILE 69 /* 'compile' */
  3031. #define DUK_STRIDX_INT_REGBASE 70 /* '\x00Regbase' */
  3032. #define DUK_STRIDX_INT_THREAD 71 /* '\x00Thread' */
  3033. #define DUK_STRIDX_INT_HANDLER 72 /* '\x00Handler' */
  3034. #define DUK_STRIDX_INT_FINALIZER 73 /* '\x00Finalizer' */
  3035. #define DUK_STRIDX_INT_CALLEE 74 /* '\x00Callee' */
  3036. #define DUK_STRIDX_INT_MAP 75 /* '\x00Map' */
  3037. #define DUK_STRIDX_INT_ARGS 76 /* '\x00Args' */
  3038. #define DUK_STRIDX_INT_THIS 77 /* '\x00This' */
  3039. #define DUK_STRIDX_INT_PC2LINE 78 /* '\x00Pc2line' */
  3040. #define DUK_STRIDX_INT_SOURCE 79 /* '\x00Source' */
  3041. #define DUK_STRIDX_INT_VARENV 80 /* '\x00Varenv' */
  3042. #define DUK_STRIDX_INT_LEXENV 81 /* '\x00Lexenv' */
  3043. #define DUK_STRIDX_INT_VARMAP 82 /* '\x00Varmap' */
  3044. #define DUK_STRIDX_INT_FORMALS 83 /* '\x00Formals' */
  3045. #define DUK_STRIDX_INT_BYTECODE 84 /* '\x00Bytecode' */
  3046. #define DUK_STRIDX_INT_NEXT 85 /* '\x00Next' */
  3047. #define DUK_STRIDX_INT_TARGET 86 /* '\x00Target' */
  3048. #define DUK_STRIDX_INT_VALUE 87 /* '\x00Value' */
  3049. #define DUK_STRIDX_LC_POINTER 88 /* 'pointer' */
  3050. #define DUK_STRIDX_INT_TRACEDATA 89 /* '\x00Tracedata' */
  3051. #define DUK_STRIDX_LINE_NUMBER 90 /* 'lineNumber' */
  3052. #define DUK_STRIDX_FILE_NAME 91 /* 'fileName' */
  3053. #define DUK_STRIDX_PC 92 /* 'pc' */
  3054. #define DUK_STRIDX_STACK 93 /* 'stack' */
  3055. #define DUK_STRIDX_THROW_TYPE_ERROR 94 /* 'ThrowTypeError' */
  3056. #define DUK_STRIDX_DUKTAPE 95 /* 'Duktape' */
  3057. #define DUK_STRIDX_SET_FLOAT64 96 /* 'setFloat64' */
  3058. #define DUK_STRIDX_SET_FLOAT32 97 /* 'setFloat32' */
  3059. #define DUK_STRIDX_SET_UINT32 98 /* 'setUint32' */
  3060. #define DUK_STRIDX_SET_INT32 99 /* 'setInt32' */
  3061. #define DUK_STRIDX_SET_UINT16 100 /* 'setUint16' */
  3062. #define DUK_STRIDX_SET_INT16 101 /* 'setInt16' */
  3063. #define DUK_STRIDX_SET_UINT8 102 /* 'setUint8' */
  3064. #define DUK_STRIDX_SET_INT8 103 /* 'setInt8' */
  3065. #define DUK_STRIDX_GET_FLOAT64 104 /* 'getFloat64' */
  3066. #define DUK_STRIDX_GET_FLOAT32 105 /* 'getFloat32' */
  3067. #define DUK_STRIDX_GET_UINT32 106 /* 'getUint32' */
  3068. #define DUK_STRIDX_GET_INT32 107 /* 'getInt32' */
  3069. #define DUK_STRIDX_GET_UINT16 108 /* 'getUint16' */
  3070. #define DUK_STRIDX_GET_INT16 109 /* 'getInt16' */
  3071. #define DUK_STRIDX_GET_UINT8 110 /* 'getUint8' */
  3072. #define DUK_STRIDX_GET_INT8 111 /* 'getInt8' */
  3073. #define DUK_STRIDX_SUBARRAY 112 /* 'subarray' */
  3074. #define DUK_STRIDX_BYTES_PER_ELEMENT 113 /* 'BYTES_PER_ELEMENT' */
  3075. #define DUK_STRIDX_BYTE_OFFSET 114 /* 'byteOffset' */
  3076. #define DUK_STRIDX_LC_BUFFER 115 /* 'buffer' */
  3077. #define DUK_STRIDX_IS_VIEW 116 /* 'isView' */
  3078. #define DUK_STRIDX_DATA 117 /* 'data' */
  3079. #define DUK_STRIDX_TYPE 118 /* 'type' */
  3080. #define DUK_STRIDX_WRITE_INT_BE 119 /* 'writeIntBE' */
  3081. #define DUK_STRIDX_WRITE_INT_LE 120 /* 'writeIntLE' */
  3082. #define DUK_STRIDX_WRITE_UINT_BE 121 /* 'writeUIntBE' */
  3083. #define DUK_STRIDX_WRITE_UINT_LE 122 /* 'writeUIntLE' */
  3084. #define DUK_STRIDX_WRITE_DOUBLE_BE 123 /* 'writeDoubleBE' */
  3085. #define DUK_STRIDX_WRITE_DOUBLE_LE 124 /* 'writeDoubleLE' */
  3086. #define DUK_STRIDX_WRITE_FLOAT_BE 125 /* 'writeFloatBE' */
  3087. #define DUK_STRIDX_WRITE_FLOAT_LE 126 /* 'writeFloatLE' */
  3088. #define DUK_STRIDX_WRITE_INT32_BE 127 /* 'writeInt32BE' */
  3089. #define DUK_STRIDX_WRITE_INT32_LE 128 /* 'writeInt32LE' */
  3090. #define DUK_STRIDX_WRITE_UINT32_BE 129 /* 'writeUInt32BE' */
  3091. #define DUK_STRIDX_WRITE_UINT32_LE 130 /* 'writeUInt32LE' */
  3092. #define DUK_STRIDX_WRITE_INT16_BE 131 /* 'writeInt16BE' */
  3093. #define DUK_STRIDX_WRITE_INT16_LE 132 /* 'writeInt16LE' */
  3094. #define DUK_STRIDX_WRITE_UINT16_BE 133 /* 'writeUInt16BE' */
  3095. #define DUK_STRIDX_WRITE_UINT16_LE 134 /* 'writeUInt16LE' */
  3096. #define DUK_STRIDX_WRITE_INT8 135 /* 'writeInt8' */
  3097. #define DUK_STRIDX_WRITE_UINT8 136 /* 'writeUInt8' */
  3098. #define DUK_STRIDX_READ_INT_BE 137 /* 'readIntBE' */
  3099. #define DUK_STRIDX_READ_INT_LE 138 /* 'readIntLE' */
  3100. #define DUK_STRIDX_READ_UINT_BE 139 /* 'readUIntBE' */
  3101. #define DUK_STRIDX_READ_UINT_LE 140 /* 'readUIntLE' */
  3102. #define DUK_STRIDX_READ_DOUBLE_BE 141 /* 'readDoubleBE' */
  3103. #define DUK_STRIDX_READ_DOUBLE_LE 142 /* 'readDoubleLE' */
  3104. #define DUK_STRIDX_READ_FLOAT_BE 143 /* 'readFloatBE' */
  3105. #define DUK_STRIDX_READ_FLOAT_LE 144 /* 'readFloatLE' */
  3106. #define DUK_STRIDX_READ_INT32_BE 145 /* 'readInt32BE' */
  3107. #define DUK_STRIDX_READ_INT32_LE 146 /* 'readInt32LE' */
  3108. #define DUK_STRIDX_READ_UINT32_BE 147 /* 'readUInt32BE' */
  3109. #define DUK_STRIDX_READ_UINT32_LE 148 /* 'readUInt32LE' */
  3110. #define DUK_STRIDX_READ_INT16_BE 149 /* 'readInt16BE' */
  3111. #define DUK_STRIDX_READ_INT16_LE 150 /* 'readInt16LE' */
  3112. #define DUK_STRIDX_READ_UINT16_BE 151 /* 'readUInt16BE' */
  3113. #define DUK_STRIDX_READ_UINT16_LE 152 /* 'readUInt16LE' */
  3114. #define DUK_STRIDX_READ_INT8 153 /* 'readInt8' */
  3115. #define DUK_STRIDX_READ_UINT8 154 /* 'readUInt8' */
  3116. #define DUK_STRIDX_COPY 155 /* 'copy' */
  3117. #define DUK_STRIDX_EQUALS 156 /* 'equals' */
  3118. #define DUK_STRIDX_FILL 157 /* 'fill' */
  3119. #define DUK_STRIDX_WRITE 158 /* 'write' */
  3120. #define DUK_STRIDX_COMPARE 159 /* 'compare' */
  3121. #define DUK_STRIDX_BYTE_LENGTH 160 /* 'byteLength' */
  3122. #define DUK_STRIDX_IS_BUFFER 161 /* 'isBuffer' */
  3123. #define DUK_STRIDX_IS_ENCODING 162 /* 'isEncoding' */
  3124. #define DUK_STRIDX_EXPORTS 163 /* 'exports' */
  3125. #define DUK_STRIDX_ID 164 /* 'id' */
  3126. #define DUK_STRIDX_REQUIRE 165 /* 'require' */
  3127. #define DUK_STRIDX___PROTO__ 166 /* '__proto__' */
  3128. #define DUK_STRIDX_SET_PROTOTYPE_OF 167 /* 'setPrototypeOf' */
  3129. #define DUK_STRIDX_OWN_KEYS 168 /* 'ownKeys' */
  3130. #define DUK_STRIDX_ENUMERATE 169 /* 'enumerate' */
  3131. #define DUK_STRIDX_DELETE_PROPERTY 170 /* 'deleteProperty' */
  3132. #define DUK_STRIDX_HAS 171 /* 'has' */
  3133. #define DUK_STRIDX_PROXY 172 /* 'Proxy' */
  3134. #define DUK_STRIDX_CALLEE 173 /* 'callee' */
  3135. #define DUK_STRIDX_INVALID_DATE 174 /* 'Invalid Date' */
  3136. #define DUK_STRIDX_BRACKETED_ELLIPSIS 175 /* '[...]' */
  3137. #define DUK_STRIDX_NEWLINE_TAB 176 /* '\n\t' */
  3138. #define DUK_STRIDX_SPACE 177 /* ' ' */
  3139. #define DUK_STRIDX_COMMA 178 /* ',' */
  3140. #define DUK_STRIDX_MINUS_ZERO 179 /* '-0' */
  3141. #define DUK_STRIDX_PLUS_ZERO 180 /* '+0' */
  3142. #define DUK_STRIDX_ZERO 181 /* '0' */
  3143. #define DUK_STRIDX_MINUS_INFINITY 182 /* '-Infinity' */
  3144. #define DUK_STRIDX_PLUS_INFINITY 183 /* '+Infinity' */
  3145. #define DUK_STRIDX_INFINITY 184 /* 'Infinity' */
  3146. #define DUK_STRIDX_LC_OBJECT 185 /* 'object' */
  3147. #define DUK_STRIDX_LC_STRING 186 /* 'string' */
  3148. #define DUK_STRIDX_LC_NUMBER 187 /* 'number' */
  3149. #define DUK_STRIDX_LC_BOOLEAN 188 /* 'boolean' */
  3150. #define DUK_STRIDX_LC_UNDEFINED 189 /* 'undefined' */
  3151. #define DUK_STRIDX_STRINGIFY 190 /* 'stringify' */
  3152. #define DUK_STRIDX_TAN 191 /* 'tan' */
  3153. #define DUK_STRIDX_SQRT 192 /* 'sqrt' */
  3154. #define DUK_STRIDX_SIN 193 /* 'sin' */
  3155. #define DUK_STRIDX_ROUND 194 /* 'round' */
  3156. #define DUK_STRIDX_RANDOM 195 /* 'random' */
  3157. #define DUK_STRIDX_POW 196 /* 'pow' */
  3158. #define DUK_STRIDX_MIN 197 /* 'min' */
  3159. #define DUK_STRIDX_MAX 198 /* 'max' */
  3160. #define DUK_STRIDX_LOG 199 /* 'log' */
  3161. #define DUK_STRIDX_FLOOR 200 /* 'floor' */
  3162. #define DUK_STRIDX_EXP 201 /* 'exp' */
  3163. #define DUK_STRIDX_COS 202 /* 'cos' */
  3164. #define DUK_STRIDX_CEIL 203 /* 'ceil' */
  3165. #define DUK_STRIDX_ATAN2 204 /* 'atan2' */
  3166. #define DUK_STRIDX_ATAN 205 /* 'atan' */
  3167. #define DUK_STRIDX_ASIN 206 /* 'asin' */
  3168. #define DUK_STRIDX_ACOS 207 /* 'acos' */
  3169. #define DUK_STRIDX_ABS 208 /* 'abs' */
  3170. #define DUK_STRIDX_SQRT2 209 /* 'SQRT2' */
  3171. #define DUK_STRIDX_SQRT1_2 210 /* 'SQRT1_2' */
  3172. #define DUK_STRIDX_PI 211 /* 'PI' */
  3173. #define DUK_STRIDX_LOG10E 212 /* 'LOG10E' */
  3174. #define DUK_STRIDX_LOG2E 213 /* 'LOG2E' */
  3175. #define DUK_STRIDX_LN2 214 /* 'LN2' */
  3176. #define DUK_STRIDX_LN10 215 /* 'LN10' */
  3177. #define DUK_STRIDX_E 216 /* 'E' */
  3178. #define DUK_STRIDX_MESSAGE 217 /* 'message' */
  3179. #define DUK_STRIDX_NAME 218 /* 'name' */
  3180. #define DUK_STRIDX_INPUT 219 /* 'input' */
  3181. #define DUK_STRIDX_INDEX 220 /* 'index' */
  3182. #define DUK_STRIDX_ESCAPED_EMPTY_REGEXP 221 /* '(?:)' */
  3183. #define DUK_STRIDX_LAST_INDEX 222 /* 'lastIndex' */
  3184. #define DUK_STRIDX_MULTILINE 223 /* 'multiline' */
  3185. #define DUK_STRIDX_IGNORE_CASE 224 /* 'ignoreCase' */
  3186. #define DUK_STRIDX_SOURCE 225 /* 'source' */
  3187. #define DUK_STRIDX_TEST 226 /* 'test' */
  3188. #define DUK_STRIDX_EXEC 227 /* 'exec' */
  3189. #define DUK_STRIDX_TO_GMT_STRING 228 /* 'toGMTString' */
  3190. #define DUK_STRIDX_SET_YEAR 229 /* 'setYear' */
  3191. #define DUK_STRIDX_GET_YEAR 230 /* 'getYear' */
  3192. #define DUK_STRIDX_TO_JSON 231 /* 'toJSON' */
  3193. #define DUK_STRIDX_TO_ISO_STRING 232 /* 'toISOString' */
  3194. #define DUK_STRIDX_TO_UTC_STRING 233 /* 'toUTCString' */
  3195. #define DUK_STRIDX_SET_UTC_FULL_YEAR 234 /* 'setUTCFullYear' */
  3196. #define DUK_STRIDX_SET_FULL_YEAR 235 /* 'setFullYear' */
  3197. #define DUK_STRIDX_SET_UTC_MONTH 236 /* 'setUTCMonth' */
  3198. #define DUK_STRIDX_SET_MONTH 237 /* 'setMonth' */
  3199. #define DUK_STRIDX_SET_UTC_DATE 238 /* 'setUTCDate' */
  3200. #define DUK_STRIDX_SET_DATE 239 /* 'setDate' */
  3201. #define DUK_STRIDX_SET_UTC_HOURS 240 /* 'setUTCHours' */
  3202. #define DUK_STRIDX_SET_HOURS 241 /* 'setHours' */
  3203. #define DUK_STRIDX_SET_UTC_MINUTES 242 /* 'setUTCMinutes' */
  3204. #define DUK_STRIDX_SET_MINUTES 243 /* 'setMinutes' */
  3205. #define DUK_STRIDX_SET_UTC_SECONDS 244 /* 'setUTCSeconds' */
  3206. #define DUK_STRIDX_SET_SECONDS 245 /* 'setSeconds' */
  3207. #define DUK_STRIDX_SET_UTC_MILLISECONDS 246 /* 'setUTCMilliseconds' */
  3208. #define DUK_STRIDX_SET_MILLISECONDS 247 /* 'setMilliseconds' */
  3209. #define DUK_STRIDX_SET_TIME 248 /* 'setTime' */
  3210. #define DUK_STRIDX_GET_TIMEZONE_OFFSET 249 /* 'getTimezoneOffset' */
  3211. #define DUK_STRIDX_GET_UTC_MILLISECONDS 250 /* 'getUTCMilliseconds' */
  3212. #define DUK_STRIDX_GET_MILLISECONDS 251 /* 'getMilliseconds' */
  3213. #define DUK_STRIDX_GET_UTC_SECONDS 252 /* 'getUTCSeconds' */
  3214. #define DUK_STRIDX_GET_SECONDS 253 /* 'getSeconds' */
  3215. #define DUK_STRIDX_GET_UTC_MINUTES 254 /* 'getUTCMinutes' */
  3216. #define DUK_STRIDX_GET_MINUTES 255 /* 'getMinutes' */
  3217. #define DUK_STRIDX_GET_UTC_HOURS 256 /* 'getUTCHours' */
  3218. #define DUK_STRIDX_GET_HOURS 257 /* 'getHours' */
  3219. #define DUK_STRIDX_GET_UTC_DAY 258 /* 'getUTCDay' */
  3220. #define DUK_STRIDX_GET_DAY 259 /* 'getDay' */
  3221. #define DUK_STRIDX_GET_UTC_DATE 260 /* 'getUTCDate' */
  3222. #define DUK_STRIDX_GET_DATE 261 /* 'getDate' */
  3223. #define DUK_STRIDX_GET_UTC_MONTH 262 /* 'getUTCMonth' */
  3224. #define DUK_STRIDX_GET_MONTH 263 /* 'getMonth' */
  3225. #define DUK_STRIDX_GET_UTC_FULL_YEAR 264 /* 'getUTCFullYear' */
  3226. #define DUK_STRIDX_GET_FULL_YEAR 265 /* 'getFullYear' */
  3227. #define DUK_STRIDX_GET_TIME 266 /* 'getTime' */
  3228. #define DUK_STRIDX_TO_LOCALE_TIME_STRING 267 /* 'toLocaleTimeString' */
  3229. #define DUK_STRIDX_TO_LOCALE_DATE_STRING 268 /* 'toLocaleDateString' */
  3230. #define DUK_STRIDX_TO_TIME_STRING 269 /* 'toTimeString' */
  3231. #define DUK_STRIDX_TO_DATE_STRING 270 /* 'toDateString' */
  3232. #define DUK_STRIDX_NOW 271 /* 'now' */
  3233. #define DUK_STRIDX_UTC 272 /* 'UTC' */
  3234. #define DUK_STRIDX_PARSE 273 /* 'parse' */
  3235. #define DUK_STRIDX_TO_PRECISION 274 /* 'toPrecision' */
  3236. #define DUK_STRIDX_TO_EXPONENTIAL 275 /* 'toExponential' */
  3237. #define DUK_STRIDX_TO_FIXED 276 /* 'toFixed' */
  3238. #define DUK_STRIDX_POSITIVE_INFINITY 277 /* 'POSITIVE_INFINITY' */
  3239. #define DUK_STRIDX_NEGATIVE_INFINITY 278 /* 'NEGATIVE_INFINITY' */
  3240. #define DUK_STRIDX_NAN 279 /* 'NaN' */
  3241. #define DUK_STRIDX_MIN_VALUE 280 /* 'MIN_VALUE' */
  3242. #define DUK_STRIDX_MAX_VALUE 281 /* 'MAX_VALUE' */
  3243. #define DUK_STRIDX_SUBSTR 282 /* 'substr' */
  3244. #define DUK_STRIDX_TRIM 283 /* 'trim' */
  3245. #define DUK_STRIDX_TO_LOCALE_UPPER_CASE 284 /* 'toLocaleUpperCase' */
  3246. #define DUK_STRIDX_TO_UPPER_CASE 285 /* 'toUpperCase' */
  3247. #define DUK_STRIDX_TO_LOCALE_LOWER_CASE 286 /* 'toLocaleLowerCase' */
  3248. #define DUK_STRIDX_TO_LOWER_CASE 287 /* 'toLowerCase' */
  3249. #define DUK_STRIDX_SUBSTRING 288 /* 'substring' */
  3250. #define DUK_STRIDX_SPLIT 289 /* 'split' */
  3251. #define DUK_STRIDX_SEARCH 290 /* 'search' */
  3252. #define DUK_STRIDX_REPLACE 291 /* 'replace' */
  3253. #define DUK_STRIDX_MATCH 292 /* 'match' */
  3254. #define DUK_STRIDX_LOCALE_COMPARE 293 /* 'localeCompare' */
  3255. #define DUK_STRIDX_CHAR_CODE_AT 294 /* 'charCodeAt' */
  3256. #define DUK_STRIDX_CHAR_AT 295 /* 'charAt' */
  3257. #define DUK_STRIDX_FROM_CHAR_CODE 296 /* 'fromCharCode' */
  3258. #define DUK_STRIDX_REDUCE_RIGHT 297 /* 'reduceRight' */
  3259. #define DUK_STRIDX_REDUCE 298 /* 'reduce' */
  3260. #define DUK_STRIDX_FILTER 299 /* 'filter' */
  3261. #define DUK_STRIDX_MAP 300 /* 'map' */
  3262. #define DUK_STRIDX_FOR_EACH 301 /* 'forEach' */
  3263. #define DUK_STRIDX_SOME 302 /* 'some' */
  3264. #define DUK_STRIDX_EVERY 303 /* 'every' */
  3265. #define DUK_STRIDX_LAST_INDEX_OF 304 /* 'lastIndexOf' */
  3266. #define DUK_STRIDX_INDEX_OF 305 /* 'indexOf' */
  3267. #define DUK_STRIDX_UNSHIFT 306 /* 'unshift' */
  3268. #define DUK_STRIDX_SPLICE 307 /* 'splice' */
  3269. #define DUK_STRIDX_SORT 308 /* 'sort' */
  3270. #define DUK_STRIDX_SLICE 309 /* 'slice' */
  3271. #define DUK_STRIDX_SHIFT 310 /* 'shift' */
  3272. #define DUK_STRIDX_REVERSE 311 /* 'reverse' */
  3273. #define DUK_STRIDX_PUSH 312 /* 'push' */
  3274. #define DUK_STRIDX_POP 313 /* 'pop' */
  3275. #define DUK_STRIDX_JOIN 314 /* 'join' */
  3276. #define DUK_STRIDX_CONCAT 315 /* 'concat' */
  3277. #define DUK_STRIDX_IS_ARRAY 316 /* 'isArray' */
  3278. #define DUK_STRIDX_LC_ARGUMENTS 317 /* 'arguments' */
  3279. #define DUK_STRIDX_CALLER 318 /* 'caller' */
  3280. #define DUK_STRIDX_BIND 319 /* 'bind' */
  3281. #define DUK_STRIDX_CALL 320 /* 'call' */
  3282. #define DUK_STRIDX_APPLY 321 /* 'apply' */
  3283. #define DUK_STRIDX_PROPERTY_IS_ENUMERABLE 322 /* 'propertyIsEnumerable' */
  3284. #define DUK_STRIDX_IS_PROTOTYPE_OF 323 /* 'isPrototypeOf' */
  3285. #define DUK_STRIDX_HAS_OWN_PROPERTY 324 /* 'hasOwnProperty' */
  3286. #define DUK_STRIDX_VALUE_OF 325 /* 'valueOf' */
  3287. #define DUK_STRIDX_TO_LOCALE_STRING 326 /* 'toLocaleString' */
  3288. #define DUK_STRIDX_TO_STRING 327 /* 'toString' */
  3289. #define DUK_STRIDX_CONSTRUCTOR 328 /* 'constructor' */
  3290. #define DUK_STRIDX_SET 329 /* 'set' */
  3291. #define DUK_STRIDX_GET 330 /* 'get' */
  3292. #define DUK_STRIDX_ENUMERABLE 331 /* 'enumerable' */
  3293. #define DUK_STRIDX_CONFIGURABLE 332 /* 'configurable' */
  3294. #define DUK_STRIDX_WRITABLE 333 /* 'writable' */
  3295. #define DUK_STRIDX_VALUE 334 /* 'value' */
  3296. #define DUK_STRIDX_KEYS 335 /* 'keys' */
  3297. #define DUK_STRIDX_IS_EXTENSIBLE 336 /* 'isExtensible' */
  3298. #define DUK_STRIDX_IS_FROZEN 337 /* 'isFrozen' */
  3299. #define DUK_STRIDX_IS_SEALED 338 /* 'isSealed' */
  3300. #define DUK_STRIDX_PREVENT_EXTENSIONS 339 /* 'preventExtensions' */
  3301. #define DUK_STRIDX_FREEZE 340 /* 'freeze' */
  3302. #define DUK_STRIDX_SEAL 341 /* 'seal' */
  3303. #define DUK_STRIDX_DEFINE_PROPERTIES 342 /* 'defineProperties' */
  3304. #define DUK_STRIDX_DEFINE_PROPERTY 343 /* 'defineProperty' */
  3305. #define DUK_STRIDX_CREATE 344 /* 'create' */
  3306. #define DUK_STRIDX_GET_OWN_PROPERTY_NAMES 345 /* 'getOwnPropertyNames' */
  3307. #define DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR 346 /* 'getOwnPropertyDescriptor' */
  3308. #define DUK_STRIDX_GET_PROTOTYPE_OF 347 /* 'getPrototypeOf' */
  3309. #define DUK_STRIDX_PROTOTYPE 348 /* 'prototype' */
  3310. #define DUK_STRIDX_LENGTH 349 /* 'length' */
  3311. #define DUK_STRIDX_ALERT 350 /* 'alert' */
  3312. #define DUK_STRIDX_PRINT 351 /* 'print' */
  3313. #define DUK_STRIDX_UNESCAPE 352 /* 'unescape' */
  3314. #define DUK_STRIDX_ESCAPE 353 /* 'escape' */
  3315. #define DUK_STRIDX_ENCODE_URI_COMPONENT 354 /* 'encodeURIComponent' */
  3316. #define DUK_STRIDX_ENCODE_URI 355 /* 'encodeURI' */
  3317. #define DUK_STRIDX_DECODE_URI_COMPONENT 356 /* 'decodeURIComponent' */
  3318. #define DUK_STRIDX_DECODE_URI 357 /* 'decodeURI' */
  3319. #define DUK_STRIDX_IS_FINITE 358 /* 'isFinite' */
  3320. #define DUK_STRIDX_IS_NAN 359 /* 'isNaN' */
  3321. #define DUK_STRIDX_PARSE_FLOAT 360 /* 'parseFloat' */
  3322. #define DUK_STRIDX_PARSE_INT 361 /* 'parseInt' */
  3323. #define DUK_STRIDX_EVAL 362 /* 'eval' */
  3324. #define DUK_STRIDX_URI_ERROR 363 /* 'URIError' */
  3325. #define DUK_STRIDX_TYPE_ERROR 364 /* 'TypeError' */
  3326. #define DUK_STRIDX_SYNTAX_ERROR 365 /* 'SyntaxError' */
  3327. #define DUK_STRIDX_REFERENCE_ERROR 366 /* 'ReferenceError' */
  3328. #define DUK_STRIDX_RANGE_ERROR 367 /* 'RangeError' */
  3329. #define DUK_STRIDX_EVAL_ERROR 368 /* 'EvalError' */
  3330. #define DUK_STRIDX_BREAK 369 /* 'break' */
  3331. #define DUK_STRIDX_CASE 370 /* 'case' */
  3332. #define DUK_STRIDX_CATCH 371 /* 'catch' */
  3333. #define DUK_STRIDX_CONTINUE 372 /* 'continue' */
  3334. #define DUK_STRIDX_DEBUGGER 373 /* 'debugger' */
  3335. #define DUK_STRIDX_DEFAULT 374 /* 'default' */
  3336. #define DUK_STRIDX_DELETE 375 /* 'delete' */
  3337. #define DUK_STRIDX_DO 376 /* 'do' */
  3338. #define DUK_STRIDX_ELSE 377 /* 'else' */
  3339. #define DUK_STRIDX_FINALLY 378 /* 'finally' */
  3340. #define DUK_STRIDX_FOR 379 /* 'for' */
  3341. #define DUK_STRIDX_LC_FUNCTION 380 /* 'function' */
  3342. #define DUK_STRIDX_IF 381 /* 'if' */
  3343. #define DUK_STRIDX_IN 382 /* 'in' */
  3344. #define DUK_STRIDX_INSTANCEOF 383 /* 'instanceof' */
  3345. #define DUK_STRIDX_NEW 384 /* 'new' */
  3346. #define DUK_STRIDX_RETURN 385 /* 'return' */
  3347. #define DUK_STRIDX_SWITCH 386 /* 'switch' */
  3348. #define DUK_STRIDX_THIS 387 /* 'this' */
  3349. #define DUK_STRIDX_THROW 388 /* 'throw' */
  3350. #define DUK_STRIDX_TRY 389 /* 'try' */
  3351. #define DUK_STRIDX_TYPEOF 390 /* 'typeof' */
  3352. #define DUK_STRIDX_VAR 391 /* 'var' */
  3353. #define DUK_STRIDX_CONST 392 /* 'const' */
  3354. #define DUK_STRIDX_VOID 393 /* 'void' */
  3355. #define DUK_STRIDX_WHILE 394 /* 'while' */
  3356. #define DUK_STRIDX_WITH 395 /* 'with' */
  3357. #define DUK_STRIDX_CLASS 396 /* 'class' */
  3358. #define DUK_STRIDX_ENUM 397 /* 'enum' */
  3359. #define DUK_STRIDX_EXPORT 398 /* 'export' */
  3360. #define DUK_STRIDX_EXTENDS 399 /* 'extends' */
  3361. #define DUK_STRIDX_IMPORT 400 /* 'import' */
  3362. #define DUK_STRIDX_SUPER 401 /* 'super' */
  3363. #define DUK_STRIDX_LC_NULL 402 /* 'null' */
  3364. #define DUK_STRIDX_TRUE 403 /* 'true' */
  3365. #define DUK_STRIDX_FALSE 404 /* 'false' */
  3366. #define DUK_STRIDX_IMPLEMENTS 405 /* 'implements' */
  3367. #define DUK_STRIDX_INTERFACE 406 /* 'interface' */
  3368. #define DUK_STRIDX_LET 407 /* 'let' */
  3369. #define DUK_STRIDX_PACKAGE 408 /* 'package' */
  3370. #define DUK_STRIDX_PRIVATE 409 /* 'private' */
  3371. #define DUK_STRIDX_PROTECTED 410 /* 'protected' */
  3372. #define DUK_STRIDX_PUBLIC 411 /* 'public' */
  3373. #define DUK_STRIDX_STATIC 412 /* 'static' */
  3374. #define DUK_STRIDX_YIELD 413 /* 'yield' */
  3375. #define DUK_HEAP_STRING_UC_LOGGER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_LOGGER)
  3376. #define DUK_HTHREAD_STRING_UC_LOGGER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_LOGGER)
  3377. #define DUK_HEAP_STRING_UC_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_THREAD)
  3378. #define DUK_HTHREAD_STRING_UC_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_THREAD)
  3379. #define DUK_HEAP_STRING_UC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_POINTER)
  3380. #define DUK_HTHREAD_STRING_UC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_POINTER)
  3381. #define DUK_HEAP_STRING_DEC_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEC_ENV)
  3382. #define DUK_HTHREAD_STRING_DEC_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEC_ENV)
  3383. #define DUK_HEAP_STRING_OBJ_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OBJ_ENV)
  3384. #define DUK_HTHREAD_STRING_OBJ_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OBJ_ENV)
  3385. #define DUK_HEAP_STRING_FLOAT64_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT64_ARRAY)
  3386. #define DUK_HTHREAD_STRING_FLOAT64_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT64_ARRAY)
  3387. #define DUK_HEAP_STRING_FLOAT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT32_ARRAY)
  3388. #define DUK_HTHREAD_STRING_FLOAT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT32_ARRAY)
  3389. #define DUK_HEAP_STRING_UINT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT32_ARRAY)
  3390. #define DUK_HTHREAD_STRING_UINT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT32_ARRAY)
  3391. #define DUK_HEAP_STRING_INT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT32_ARRAY)
  3392. #define DUK_HTHREAD_STRING_INT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT32_ARRAY)
  3393. #define DUK_HEAP_STRING_UINT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT16_ARRAY)
  3394. #define DUK_HTHREAD_STRING_UINT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT16_ARRAY)
  3395. #define DUK_HEAP_STRING_INT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT16_ARRAY)
  3396. #define DUK_HTHREAD_STRING_INT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT16_ARRAY)
  3397. #define DUK_HEAP_STRING_UINT8_CLAMPED_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_CLAMPED_ARRAY)
  3398. #define DUK_HTHREAD_STRING_UINT8_CLAMPED_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_CLAMPED_ARRAY)
  3399. #define DUK_HEAP_STRING_UINT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_ARRAY)
  3400. #define DUK_HTHREAD_STRING_UINT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_ARRAY)
  3401. #define DUK_HEAP_STRING_INT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT8_ARRAY)
  3402. #define DUK_HTHREAD_STRING_INT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT8_ARRAY)
  3403. #define DUK_HEAP_STRING_DATA_VIEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA_VIEW)
  3404. #define DUK_HTHREAD_STRING_DATA_VIEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA_VIEW)
  3405. #define DUK_HEAP_STRING_ARRAY_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY_BUFFER)
  3406. #define DUK_HTHREAD_STRING_ARRAY_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY_BUFFER)
  3407. #define DUK_HEAP_STRING_UC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BUFFER)
  3408. #define DUK_HTHREAD_STRING_UC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BUFFER)
  3409. #define DUK_HEAP_STRING_EMPTY_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EMPTY_STRING)
  3410. #define DUK_HTHREAD_STRING_EMPTY_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EMPTY_STRING)
  3411. #define DUK_HEAP_STRING_GLOBAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GLOBAL)
  3412. #define DUK_HTHREAD_STRING_GLOBAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GLOBAL)
  3413. #define DUK_HEAP_STRING_UC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ARGUMENTS)
  3414. #define DUK_HTHREAD_STRING_UC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ARGUMENTS)
  3415. #define DUK_HEAP_STRING_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON)
  3416. #define DUK_HTHREAD_STRING_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON)
  3417. #define DUK_HEAP_STRING_MATH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MATH)
  3418. #define DUK_HTHREAD_STRING_MATH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MATH)
  3419. #define DUK_HEAP_STRING_UC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ERROR)
  3420. #define DUK_HTHREAD_STRING_UC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ERROR)
  3421. #define DUK_HEAP_STRING_REG_EXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REG_EXP)
  3422. #define DUK_HTHREAD_STRING_REG_EXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REG_EXP)
  3423. #define DUK_HEAP_STRING_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATE)
  3424. #define DUK_HTHREAD_STRING_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATE)
  3425. #define DUK_HEAP_STRING_UC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NUMBER)
  3426. #define DUK_HTHREAD_STRING_UC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NUMBER)
  3427. #define DUK_HEAP_STRING_UC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BOOLEAN)
  3428. #define DUK_HTHREAD_STRING_UC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BOOLEAN)
  3429. #define DUK_HEAP_STRING_UC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_STRING)
  3430. #define DUK_HTHREAD_STRING_UC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_STRING)
  3431. #define DUK_HEAP_STRING_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY)
  3432. #define DUK_HTHREAD_STRING_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY)
  3433. #define DUK_HEAP_STRING_UC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_FUNCTION)
  3434. #define DUK_HTHREAD_STRING_UC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_FUNCTION)
  3435. #define DUK_HEAP_STRING_UC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_OBJECT)
  3436. #define DUK_HTHREAD_STRING_UC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_OBJECT)
  3437. #define DUK_HEAP_STRING_UC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NULL)
  3438. #define DUK_HTHREAD_STRING_UC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NULL)
  3439. #define DUK_HEAP_STRING_UC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_UNDEFINED)
  3440. #define DUK_HTHREAD_STRING_UC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_UNDEFINED)
  3441. #define DUK_HEAP_STRING_JSON_EXT_FUNCTION2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION2)
  3442. #define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION2)
  3443. #define DUK_HEAP_STRING_JSON_EXT_FUNCTION1(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION1)
  3444. #define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION1(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION1)
  3445. #define DUK_HEAP_STRING_JSON_EXT_NEGINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NEGINF)
  3446. #define DUK_HTHREAD_STRING_JSON_EXT_NEGINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NEGINF)
  3447. #define DUK_HEAP_STRING_JSON_EXT_POSINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_POSINF)
  3448. #define DUK_HTHREAD_STRING_JSON_EXT_POSINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_POSINF)
  3449. #define DUK_HEAP_STRING_JSON_EXT_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NAN)
  3450. #define DUK_HTHREAD_STRING_JSON_EXT_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NAN)
  3451. #define DUK_HEAP_STRING_JSON_EXT_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_UNDEFINED)
  3452. #define DUK_HTHREAD_STRING_JSON_EXT_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_UNDEFINED)
  3453. #define DUK_HEAP_STRING_TO_LOG_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOG_STRING)
  3454. #define DUK_HTHREAD_STRING_TO_LOG_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOG_STRING)
  3455. #define DUK_HEAP_STRING_CLOG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLOG)
  3456. #define DUK_HTHREAD_STRING_CLOG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLOG)
  3457. #define DUK_HEAP_STRING_LC_L(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_L)
  3458. #define DUK_HTHREAD_STRING_LC_L(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_L)
  3459. #define DUK_HEAP_STRING_LC_N(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_N)
  3460. #define DUK_HTHREAD_STRING_LC_N(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_N)
  3461. #define DUK_HEAP_STRING_LC_FATAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FATAL)
  3462. #define DUK_HTHREAD_STRING_LC_FATAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FATAL)
  3463. #define DUK_HEAP_STRING_LC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ERROR)
  3464. #define DUK_HTHREAD_STRING_LC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ERROR)
  3465. #define DUK_HEAP_STRING_LC_WARN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_WARN)
  3466. #define DUK_HTHREAD_STRING_LC_WARN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_WARN)
  3467. #define DUK_HEAP_STRING_LC_DEBUG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_DEBUG)
  3468. #define DUK_HTHREAD_STRING_LC_DEBUG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_DEBUG)
  3469. #define DUK_HEAP_STRING_LC_TRACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_TRACE)
  3470. #define DUK_HTHREAD_STRING_LC_TRACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_TRACE)
  3471. #define DUK_HEAP_STRING_RAW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RAW)
  3472. #define DUK_HTHREAD_STRING_RAW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RAW)
  3473. #define DUK_HEAP_STRING_FMT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FMT)
  3474. #define DUK_HTHREAD_STRING_FMT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FMT)
  3475. #define DUK_HEAP_STRING_CURRENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CURRENT)
  3476. #define DUK_HTHREAD_STRING_CURRENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CURRENT)
  3477. #define DUK_HEAP_STRING_RESUME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RESUME)
  3478. #define DUK_HTHREAD_STRING_RESUME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RESUME)
  3479. #define DUK_HEAP_STRING_COMPACT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPACT)
  3480. #define DUK_HTHREAD_STRING_COMPACT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPACT)
  3481. #define DUK_HEAP_STRING_JC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JC)
  3482. #define DUK_HTHREAD_STRING_JC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JC)
  3483. #define DUK_HEAP_STRING_JX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JX)
  3484. #define DUK_HTHREAD_STRING_JX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JX)
  3485. #define DUK_HEAP_STRING_BASE64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BASE64)
  3486. #define DUK_HTHREAD_STRING_BASE64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BASE64)
  3487. #define DUK_HEAP_STRING_HEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HEX)
  3488. #define DUK_HTHREAD_STRING_HEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HEX)
  3489. #define DUK_HEAP_STRING_DEC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEC)
  3490. #define DUK_HTHREAD_STRING_DEC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEC)
  3491. #define DUK_HEAP_STRING_ENC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENC)
  3492. #define DUK_HTHREAD_STRING_ENC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENC)
  3493. #define DUK_HEAP_STRING_FIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FIN)
  3494. #define DUK_HTHREAD_STRING_FIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FIN)
  3495. #define DUK_HEAP_STRING_GC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GC)
  3496. #define DUK_HTHREAD_STRING_GC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GC)
  3497. #define DUK_HEAP_STRING_ACT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ACT)
  3498. #define DUK_HTHREAD_STRING_ACT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ACT)
  3499. #define DUK_HEAP_STRING_LC_INFO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_INFO)
  3500. #define DUK_HTHREAD_STRING_LC_INFO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_INFO)
  3501. #define DUK_HEAP_STRING_VERSION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VERSION)
  3502. #define DUK_HTHREAD_STRING_VERSION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VERSION)
  3503. #define DUK_HEAP_STRING_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENV)
  3504. #define DUK_HTHREAD_STRING_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENV)
  3505. #define DUK_HEAP_STRING_MOD_LOADED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MOD_LOADED)
  3506. #define DUK_HTHREAD_STRING_MOD_LOADED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MOD_LOADED)
  3507. #define DUK_HEAP_STRING_MOD_SEARCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MOD_SEARCH)
  3508. #define DUK_HTHREAD_STRING_MOD_SEARCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MOD_SEARCH)
  3509. #define DUK_HEAP_STRING_ERR_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_THROW)
  3510. #define DUK_HTHREAD_STRING_ERR_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_THROW)
  3511. #define DUK_HEAP_STRING_ERR_CREATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_CREATE)
  3512. #define DUK_HTHREAD_STRING_ERR_CREATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_CREATE)
  3513. #define DUK_HEAP_STRING_COMPILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPILE)
  3514. #define DUK_HTHREAD_STRING_COMPILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPILE)
  3515. #define DUK_HEAP_STRING_INT_REGBASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_REGBASE)
  3516. #define DUK_HTHREAD_STRING_INT_REGBASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_REGBASE)
  3517. #define DUK_HEAP_STRING_INT_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_THREAD)
  3518. #define DUK_HTHREAD_STRING_INT_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_THREAD)
  3519. #define DUK_HEAP_STRING_INT_HANDLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_HANDLER)
  3520. #define DUK_HTHREAD_STRING_INT_HANDLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_HANDLER)
  3521. #define DUK_HEAP_STRING_INT_FINALIZER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FINALIZER)
  3522. #define DUK_HTHREAD_STRING_INT_FINALIZER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FINALIZER)
  3523. #define DUK_HEAP_STRING_INT_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_CALLEE)
  3524. #define DUK_HTHREAD_STRING_INT_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_CALLEE)
  3525. #define DUK_HEAP_STRING_INT_MAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_MAP)
  3526. #define DUK_HTHREAD_STRING_INT_MAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_MAP)
  3527. #define DUK_HEAP_STRING_INT_ARGS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_ARGS)
  3528. #define DUK_HTHREAD_STRING_INT_ARGS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_ARGS)
  3529. #define DUK_HEAP_STRING_INT_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_THIS)
  3530. #define DUK_HTHREAD_STRING_INT_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_THIS)
  3531. #define DUK_HEAP_STRING_INT_PC2LINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_PC2LINE)
  3532. #define DUK_HTHREAD_STRING_INT_PC2LINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_PC2LINE)
  3533. #define DUK_HEAP_STRING_INT_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_SOURCE)
  3534. #define DUK_HTHREAD_STRING_INT_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_SOURCE)
  3535. #define DUK_HEAP_STRING_INT_VARENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARENV)
  3536. #define DUK_HTHREAD_STRING_INT_VARENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARENV)
  3537. #define DUK_HEAP_STRING_INT_LEXENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_LEXENV)
  3538. #define DUK_HTHREAD_STRING_INT_LEXENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_LEXENV)
  3539. #define DUK_HEAP_STRING_INT_VARMAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARMAP)
  3540. #define DUK_HTHREAD_STRING_INT_VARMAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARMAP)
  3541. #define DUK_HEAP_STRING_INT_FORMALS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FORMALS)
  3542. #define DUK_HTHREAD_STRING_INT_FORMALS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FORMALS)
  3543. #define DUK_HEAP_STRING_INT_BYTECODE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_BYTECODE)
  3544. #define DUK_HTHREAD_STRING_INT_BYTECODE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_BYTECODE)
  3545. #define DUK_HEAP_STRING_INT_NEXT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_NEXT)
  3546. #define DUK_HTHREAD_STRING_INT_NEXT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_NEXT)
  3547. #define DUK_HEAP_STRING_INT_TARGET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TARGET)
  3548. #define DUK_HTHREAD_STRING_INT_TARGET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TARGET)
  3549. #define DUK_HEAP_STRING_INT_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VALUE)
  3550. #define DUK_HTHREAD_STRING_INT_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VALUE)
  3551. #define DUK_HEAP_STRING_LC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_POINTER)
  3552. #define DUK_HTHREAD_STRING_LC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_POINTER)
  3553. #define DUK_HEAP_STRING_INT_TRACEDATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TRACEDATA)
  3554. #define DUK_HTHREAD_STRING_INT_TRACEDATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TRACEDATA)
  3555. #define DUK_HEAP_STRING_LINE_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LINE_NUMBER)
  3556. #define DUK_HTHREAD_STRING_LINE_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LINE_NUMBER)
  3557. #define DUK_HEAP_STRING_FILE_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILE_NAME)
  3558. #define DUK_HTHREAD_STRING_FILE_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILE_NAME)
  3559. #define DUK_HEAP_STRING_PC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PC)
  3560. #define DUK_HTHREAD_STRING_PC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PC)
  3561. #define DUK_HEAP_STRING_STACK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STACK)
  3562. #define DUK_HTHREAD_STRING_STACK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STACK)
  3563. #define DUK_HEAP_STRING_THROW_TYPE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THROW_TYPE_ERROR)
  3564. #define DUK_HTHREAD_STRING_THROW_TYPE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THROW_TYPE_ERROR)
  3565. #define DUK_HEAP_STRING_DUKTAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DUKTAPE)
  3566. #define DUK_HTHREAD_STRING_DUKTAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DUKTAPE)
  3567. #define DUK_HEAP_STRING_SET_FLOAT64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FLOAT64)
  3568. #define DUK_HTHREAD_STRING_SET_FLOAT64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FLOAT64)
  3569. #define DUK_HEAP_STRING_SET_FLOAT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FLOAT32)
  3570. #define DUK_HTHREAD_STRING_SET_FLOAT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FLOAT32)
  3571. #define DUK_HEAP_STRING_SET_UINT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT32)
  3572. #define DUK_HTHREAD_STRING_SET_UINT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT32)
  3573. #define DUK_HEAP_STRING_SET_INT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT32)
  3574. #define DUK_HTHREAD_STRING_SET_INT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT32)
  3575. #define DUK_HEAP_STRING_SET_UINT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT16)
  3576. #define DUK_HTHREAD_STRING_SET_UINT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT16)
  3577. #define DUK_HEAP_STRING_SET_INT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT16)
  3578. #define DUK_HTHREAD_STRING_SET_INT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT16)
  3579. #define DUK_HEAP_STRING_SET_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT8)
  3580. #define DUK_HTHREAD_STRING_SET_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT8)
  3581. #define DUK_HEAP_STRING_SET_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT8)
  3582. #define DUK_HTHREAD_STRING_SET_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT8)
  3583. #define DUK_HEAP_STRING_GET_FLOAT64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FLOAT64)
  3584. #define DUK_HTHREAD_STRING_GET_FLOAT64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FLOAT64)
  3585. #define DUK_HEAP_STRING_GET_FLOAT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FLOAT32)
  3586. #define DUK_HTHREAD_STRING_GET_FLOAT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FLOAT32)
  3587. #define DUK_HEAP_STRING_GET_UINT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT32)
  3588. #define DUK_HTHREAD_STRING_GET_UINT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT32)
  3589. #define DUK_HEAP_STRING_GET_INT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT32)
  3590. #define DUK_HTHREAD_STRING_GET_INT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT32)
  3591. #define DUK_HEAP_STRING_GET_UINT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT16)
  3592. #define DUK_HTHREAD_STRING_GET_UINT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT16)
  3593. #define DUK_HEAP_STRING_GET_INT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT16)
  3594. #define DUK_HTHREAD_STRING_GET_INT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT16)
  3595. #define DUK_HEAP_STRING_GET_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT8)
  3596. #define DUK_HTHREAD_STRING_GET_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT8)
  3597. #define DUK_HEAP_STRING_GET_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT8)
  3598. #define DUK_HTHREAD_STRING_GET_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT8)
  3599. #define DUK_HEAP_STRING_SUBARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBARRAY)
  3600. #define DUK_HTHREAD_STRING_SUBARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBARRAY)
  3601. #define DUK_HEAP_STRING_BYTES_PER_ELEMENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTES_PER_ELEMENT)
  3602. #define DUK_HTHREAD_STRING_BYTES_PER_ELEMENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTES_PER_ELEMENT)
  3603. #define DUK_HEAP_STRING_BYTE_OFFSET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTE_OFFSET)
  3604. #define DUK_HTHREAD_STRING_BYTE_OFFSET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTE_OFFSET)
  3605. #define DUK_HEAP_STRING_LC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BUFFER)
  3606. #define DUK_HTHREAD_STRING_LC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BUFFER)
  3607. #define DUK_HEAP_STRING_IS_VIEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_VIEW)
  3608. #define DUK_HTHREAD_STRING_IS_VIEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_VIEW)
  3609. #define DUK_HEAP_STRING_DATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA)
  3610. #define DUK_HTHREAD_STRING_DATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA)
  3611. #define DUK_HEAP_STRING_TYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPE)
  3612. #define DUK_HTHREAD_STRING_TYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPE)
  3613. #define DUK_HEAP_STRING_WRITE_INT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT_BE)
  3614. #define DUK_HTHREAD_STRING_WRITE_INT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT_BE)
  3615. #define DUK_HEAP_STRING_WRITE_INT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT_LE)
  3616. #define DUK_HTHREAD_STRING_WRITE_INT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT_LE)
  3617. #define DUK_HEAP_STRING_WRITE_UINT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT_BE)
  3618. #define DUK_HTHREAD_STRING_WRITE_UINT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT_BE)
  3619. #define DUK_HEAP_STRING_WRITE_UINT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT_LE)
  3620. #define DUK_HTHREAD_STRING_WRITE_UINT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT_LE)
  3621. #define DUK_HEAP_STRING_WRITE_DOUBLE_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_DOUBLE_BE)
  3622. #define DUK_HTHREAD_STRING_WRITE_DOUBLE_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_DOUBLE_BE)
  3623. #define DUK_HEAP_STRING_WRITE_DOUBLE_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_DOUBLE_LE)
  3624. #define DUK_HTHREAD_STRING_WRITE_DOUBLE_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_DOUBLE_LE)
  3625. #define DUK_HEAP_STRING_WRITE_FLOAT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_FLOAT_BE)
  3626. #define DUK_HTHREAD_STRING_WRITE_FLOAT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_FLOAT_BE)
  3627. #define DUK_HEAP_STRING_WRITE_FLOAT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_FLOAT_LE)
  3628. #define DUK_HTHREAD_STRING_WRITE_FLOAT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_FLOAT_LE)
  3629. #define DUK_HEAP_STRING_WRITE_INT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT32_BE)
  3630. #define DUK_HTHREAD_STRING_WRITE_INT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT32_BE)
  3631. #define DUK_HEAP_STRING_WRITE_INT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT32_LE)
  3632. #define DUK_HTHREAD_STRING_WRITE_INT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT32_LE)
  3633. #define DUK_HEAP_STRING_WRITE_UINT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT32_BE)
  3634. #define DUK_HTHREAD_STRING_WRITE_UINT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT32_BE)
  3635. #define DUK_HEAP_STRING_WRITE_UINT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT32_LE)
  3636. #define DUK_HTHREAD_STRING_WRITE_UINT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT32_LE)
  3637. #define DUK_HEAP_STRING_WRITE_INT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT16_BE)
  3638. #define DUK_HTHREAD_STRING_WRITE_INT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT16_BE)
  3639. #define DUK_HEAP_STRING_WRITE_INT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT16_LE)
  3640. #define DUK_HTHREAD_STRING_WRITE_INT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT16_LE)
  3641. #define DUK_HEAP_STRING_WRITE_UINT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT16_BE)
  3642. #define DUK_HTHREAD_STRING_WRITE_UINT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT16_BE)
  3643. #define DUK_HEAP_STRING_WRITE_UINT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT16_LE)
  3644. #define DUK_HTHREAD_STRING_WRITE_UINT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT16_LE)
  3645. #define DUK_HEAP_STRING_WRITE_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT8)
  3646. #define DUK_HTHREAD_STRING_WRITE_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT8)
  3647. #define DUK_HEAP_STRING_WRITE_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT8)
  3648. #define DUK_HTHREAD_STRING_WRITE_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT8)
  3649. #define DUK_HEAP_STRING_READ_INT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT_BE)
  3650. #define DUK_HTHREAD_STRING_READ_INT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT_BE)
  3651. #define DUK_HEAP_STRING_READ_INT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT_LE)
  3652. #define DUK_HTHREAD_STRING_READ_INT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT_LE)
  3653. #define DUK_HEAP_STRING_READ_UINT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT_BE)
  3654. #define DUK_HTHREAD_STRING_READ_UINT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT_BE)
  3655. #define DUK_HEAP_STRING_READ_UINT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT_LE)
  3656. #define DUK_HTHREAD_STRING_READ_UINT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT_LE)
  3657. #define DUK_HEAP_STRING_READ_DOUBLE_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_DOUBLE_BE)
  3658. #define DUK_HTHREAD_STRING_READ_DOUBLE_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_DOUBLE_BE)
  3659. #define DUK_HEAP_STRING_READ_DOUBLE_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_DOUBLE_LE)
  3660. #define DUK_HTHREAD_STRING_READ_DOUBLE_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_DOUBLE_LE)
  3661. #define DUK_HEAP_STRING_READ_FLOAT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_FLOAT_BE)
  3662. #define DUK_HTHREAD_STRING_READ_FLOAT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_FLOAT_BE)
  3663. #define DUK_HEAP_STRING_READ_FLOAT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_FLOAT_LE)
  3664. #define DUK_HTHREAD_STRING_READ_FLOAT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_FLOAT_LE)
  3665. #define DUK_HEAP_STRING_READ_INT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT32_BE)
  3666. #define DUK_HTHREAD_STRING_READ_INT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT32_BE)
  3667. #define DUK_HEAP_STRING_READ_INT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT32_LE)
  3668. #define DUK_HTHREAD_STRING_READ_INT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT32_LE)
  3669. #define DUK_HEAP_STRING_READ_UINT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT32_BE)
  3670. #define DUK_HTHREAD_STRING_READ_UINT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT32_BE)
  3671. #define DUK_HEAP_STRING_READ_UINT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT32_LE)
  3672. #define DUK_HTHREAD_STRING_READ_UINT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT32_LE)
  3673. #define DUK_HEAP_STRING_READ_INT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT16_BE)
  3674. #define DUK_HTHREAD_STRING_READ_INT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT16_BE)
  3675. #define DUK_HEAP_STRING_READ_INT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT16_LE)
  3676. #define DUK_HTHREAD_STRING_READ_INT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT16_LE)
  3677. #define DUK_HEAP_STRING_READ_UINT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT16_BE)
  3678. #define DUK_HTHREAD_STRING_READ_UINT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT16_BE)
  3679. #define DUK_HEAP_STRING_READ_UINT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT16_LE)
  3680. #define DUK_HTHREAD_STRING_READ_UINT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT16_LE)
  3681. #define DUK_HEAP_STRING_READ_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT8)
  3682. #define DUK_HTHREAD_STRING_READ_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT8)
  3683. #define DUK_HEAP_STRING_READ_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT8)
  3684. #define DUK_HTHREAD_STRING_READ_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT8)
  3685. #define DUK_HEAP_STRING_COPY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COPY)
  3686. #define DUK_HTHREAD_STRING_COPY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COPY)
  3687. #define DUK_HEAP_STRING_EQUALS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EQUALS)
  3688. #define DUK_HTHREAD_STRING_EQUALS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EQUALS)
  3689. #define DUK_HEAP_STRING_FILL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILL)
  3690. #define DUK_HTHREAD_STRING_FILL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILL)
  3691. #define DUK_HEAP_STRING_WRITE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE)
  3692. #define DUK_HTHREAD_STRING_WRITE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE)
  3693. #define DUK_HEAP_STRING_COMPARE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPARE)
  3694. #define DUK_HTHREAD_STRING_COMPARE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPARE)
  3695. #define DUK_HEAP_STRING_BYTE_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTE_LENGTH)
  3696. #define DUK_HTHREAD_STRING_BYTE_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTE_LENGTH)
  3697. #define DUK_HEAP_STRING_IS_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_BUFFER)
  3698. #define DUK_HTHREAD_STRING_IS_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_BUFFER)
  3699. #define DUK_HEAP_STRING_IS_ENCODING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_ENCODING)
  3700. #define DUK_HTHREAD_STRING_IS_ENCODING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_ENCODING)
  3701. #define DUK_HEAP_STRING_EXPORTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORTS)
  3702. #define DUK_HTHREAD_STRING_EXPORTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORTS)
  3703. #define DUK_HEAP_STRING_ID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ID)
  3704. #define DUK_HTHREAD_STRING_ID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ID)
  3705. #define DUK_HEAP_STRING_REQUIRE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REQUIRE)
  3706. #define DUK_HTHREAD_STRING_REQUIRE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REQUIRE)
  3707. #define DUK_HEAP_STRING___PROTO__(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX___PROTO__)
  3708. #define DUK_HTHREAD_STRING___PROTO__(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX___PROTO__)
  3709. #define DUK_HEAP_STRING_SET_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_PROTOTYPE_OF)
  3710. #define DUK_HTHREAD_STRING_SET_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_PROTOTYPE_OF)
  3711. #define DUK_HEAP_STRING_OWN_KEYS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OWN_KEYS)
  3712. #define DUK_HTHREAD_STRING_OWN_KEYS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OWN_KEYS)
  3713. #define DUK_HEAP_STRING_ENUMERATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERATE)
  3714. #define DUK_HTHREAD_STRING_ENUMERATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERATE)
  3715. #define DUK_HEAP_STRING_DELETE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE_PROPERTY)
  3716. #define DUK_HTHREAD_STRING_DELETE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE_PROPERTY)
  3717. #define DUK_HEAP_STRING_HAS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HAS)
  3718. #define DUK_HTHREAD_STRING_HAS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HAS)
  3719. #define DUK_HEAP_STRING_PROXY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROXY)
  3720. #define DUK_HTHREAD_STRING_PROXY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROXY)
  3721. #define DUK_HEAP_STRING_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLEE)
  3722. #define DUK_HTHREAD_STRING_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLEE)
  3723. #define DUK_HEAP_STRING_INVALID_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INVALID_DATE)
  3724. #define DUK_HTHREAD_STRING_INVALID_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INVALID_DATE)
  3725. #define DUK_HEAP_STRING_BRACKETED_ELLIPSIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BRACKETED_ELLIPSIS)
  3726. #define DUK_HTHREAD_STRING_BRACKETED_ELLIPSIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BRACKETED_ELLIPSIS)
  3727. #define DUK_HEAP_STRING_NEWLINE_TAB(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEWLINE_TAB)
  3728. #define DUK_HTHREAD_STRING_NEWLINE_TAB(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEWLINE_TAB)
  3729. #define DUK_HEAP_STRING_SPACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPACE)
  3730. #define DUK_HTHREAD_STRING_SPACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPACE)
  3731. #define DUK_HEAP_STRING_COMMA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMMA)
  3732. #define DUK_HTHREAD_STRING_COMMA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMMA)
  3733. #define DUK_HEAP_STRING_MINUS_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_ZERO)
  3734. #define DUK_HTHREAD_STRING_MINUS_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_ZERO)
  3735. #define DUK_HEAP_STRING_PLUS_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PLUS_ZERO)
  3736. #define DUK_HTHREAD_STRING_PLUS_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PLUS_ZERO)
  3737. #define DUK_HEAP_STRING_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ZERO)
  3738. #define DUK_HTHREAD_STRING_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ZERO)
  3739. #define DUK_HEAP_STRING_MINUS_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_INFINITY)
  3740. #define DUK_HTHREAD_STRING_MINUS_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_INFINITY)
  3741. #define DUK_HEAP_STRING_PLUS_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PLUS_INFINITY)
  3742. #define DUK_HTHREAD_STRING_PLUS_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PLUS_INFINITY)
  3743. #define DUK_HEAP_STRING_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INFINITY)
  3744. #define DUK_HTHREAD_STRING_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INFINITY)
  3745. #define DUK_HEAP_STRING_LC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_OBJECT)
  3746. #define DUK_HTHREAD_STRING_LC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_OBJECT)
  3747. #define DUK_HEAP_STRING_LC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_STRING)
  3748. #define DUK_HTHREAD_STRING_LC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_STRING)
  3749. #define DUK_HEAP_STRING_LC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NUMBER)
  3750. #define DUK_HTHREAD_STRING_LC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NUMBER)
  3751. #define DUK_HEAP_STRING_LC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BOOLEAN)
  3752. #define DUK_HTHREAD_STRING_LC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BOOLEAN)
  3753. #define DUK_HEAP_STRING_LC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_UNDEFINED)
  3754. #define DUK_HTHREAD_STRING_LC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_UNDEFINED)
  3755. #define DUK_HEAP_STRING_STRINGIFY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STRINGIFY)
  3756. #define DUK_HTHREAD_STRING_STRINGIFY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STRINGIFY)
  3757. #define DUK_HEAP_STRING_TAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TAN)
  3758. #define DUK_HTHREAD_STRING_TAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TAN)
  3759. #define DUK_HEAP_STRING_SQRT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT)
  3760. #define DUK_HTHREAD_STRING_SQRT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT)
  3761. #define DUK_HEAP_STRING_SIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SIN)
  3762. #define DUK_HTHREAD_STRING_SIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SIN)
  3763. #define DUK_HEAP_STRING_ROUND(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ROUND)
  3764. #define DUK_HTHREAD_STRING_ROUND(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ROUND)
  3765. #define DUK_HEAP_STRING_RANDOM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RANDOM)
  3766. #define DUK_HTHREAD_STRING_RANDOM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RANDOM)
  3767. #define DUK_HEAP_STRING_POW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POW)
  3768. #define DUK_HTHREAD_STRING_POW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POW)
  3769. #define DUK_HEAP_STRING_MIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MIN)
  3770. #define DUK_HTHREAD_STRING_MIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MIN)
  3771. #define DUK_HEAP_STRING_MAX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAX)
  3772. #define DUK_HTHREAD_STRING_MAX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAX)
  3773. #define DUK_HEAP_STRING_LOG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG)
  3774. #define DUK_HTHREAD_STRING_LOG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG)
  3775. #define DUK_HEAP_STRING_FLOOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOOR)
  3776. #define DUK_HTHREAD_STRING_FLOOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOOR)
  3777. #define DUK_HEAP_STRING_EXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXP)
  3778. #define DUK_HTHREAD_STRING_EXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXP)
  3779. #define DUK_HEAP_STRING_COS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COS)
  3780. #define DUK_HTHREAD_STRING_COS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COS)
  3781. #define DUK_HEAP_STRING_CEIL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CEIL)
  3782. #define DUK_HTHREAD_STRING_CEIL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CEIL)
  3783. #define DUK_HEAP_STRING_ATAN2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ATAN2)
  3784. #define DUK_HTHREAD_STRING_ATAN2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ATAN2)
  3785. #define DUK_HEAP_STRING_ATAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ATAN)
  3786. #define DUK_HTHREAD_STRING_ATAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ATAN)
  3787. #define DUK_HEAP_STRING_ASIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ASIN)
  3788. #define DUK_HTHREAD_STRING_ASIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ASIN)
  3789. #define DUK_HEAP_STRING_ACOS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ACOS)
  3790. #define DUK_HTHREAD_STRING_ACOS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ACOS)
  3791. #define DUK_HEAP_STRING_ABS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ABS)
  3792. #define DUK_HTHREAD_STRING_ABS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ABS)
  3793. #define DUK_HEAP_STRING_SQRT2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT2)
  3794. #define DUK_HTHREAD_STRING_SQRT2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT2)
  3795. #define DUK_HEAP_STRING_SQRT1_2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT1_2)
  3796. #define DUK_HTHREAD_STRING_SQRT1_2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT1_2)
  3797. #define DUK_HEAP_STRING_PI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PI)
  3798. #define DUK_HTHREAD_STRING_PI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PI)
  3799. #define DUK_HEAP_STRING_LOG10E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG10E)
  3800. #define DUK_HTHREAD_STRING_LOG10E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG10E)
  3801. #define DUK_HEAP_STRING_LOG2E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG2E)
  3802. #define DUK_HTHREAD_STRING_LOG2E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG2E)
  3803. #define DUK_HEAP_STRING_LN2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LN2)
  3804. #define DUK_HTHREAD_STRING_LN2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LN2)
  3805. #define DUK_HEAP_STRING_LN10(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LN10)
  3806. #define DUK_HTHREAD_STRING_LN10(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LN10)
  3807. #define DUK_HEAP_STRING_E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_E)
  3808. #define DUK_HTHREAD_STRING_E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_E)
  3809. #define DUK_HEAP_STRING_MESSAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MESSAGE)
  3810. #define DUK_HTHREAD_STRING_MESSAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MESSAGE)
  3811. #define DUK_HEAP_STRING_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAME)
  3812. #define DUK_HTHREAD_STRING_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAME)
  3813. #define DUK_HEAP_STRING_INPUT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INPUT)
  3814. #define DUK_HTHREAD_STRING_INPUT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INPUT)
  3815. #define DUK_HEAP_STRING_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INDEX)
  3816. #define DUK_HTHREAD_STRING_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INDEX)
  3817. #define DUK_HEAP_STRING_ESCAPED_EMPTY_REGEXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ESCAPED_EMPTY_REGEXP)
  3818. #define DUK_HTHREAD_STRING_ESCAPED_EMPTY_REGEXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ESCAPED_EMPTY_REGEXP)
  3819. #define DUK_HEAP_STRING_LAST_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LAST_INDEX)
  3820. #define DUK_HTHREAD_STRING_LAST_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LAST_INDEX)
  3821. #define DUK_HEAP_STRING_MULTILINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MULTILINE)
  3822. #define DUK_HTHREAD_STRING_MULTILINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MULTILINE)
  3823. #define DUK_HEAP_STRING_IGNORE_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IGNORE_CASE)
  3824. #define DUK_HTHREAD_STRING_IGNORE_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IGNORE_CASE)
  3825. #define DUK_HEAP_STRING_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SOURCE)
  3826. #define DUK_HTHREAD_STRING_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SOURCE)
  3827. #define DUK_HEAP_STRING_TEST(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TEST)
  3828. #define DUK_HTHREAD_STRING_TEST(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TEST)
  3829. #define DUK_HEAP_STRING_EXEC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXEC)
  3830. #define DUK_HTHREAD_STRING_EXEC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXEC)
  3831. #define DUK_HEAP_STRING_TO_GMT_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_GMT_STRING)
  3832. #define DUK_HTHREAD_STRING_TO_GMT_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_GMT_STRING)
  3833. #define DUK_HEAP_STRING_SET_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_YEAR)
  3834. #define DUK_HTHREAD_STRING_SET_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_YEAR)
  3835. #define DUK_HEAP_STRING_GET_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_YEAR)
  3836. #define DUK_HTHREAD_STRING_GET_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_YEAR)
  3837. #define DUK_HEAP_STRING_TO_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_JSON)
  3838. #define DUK_HTHREAD_STRING_TO_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_JSON)
  3839. #define DUK_HEAP_STRING_TO_ISO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_ISO_STRING)
  3840. #define DUK_HTHREAD_STRING_TO_ISO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_ISO_STRING)
  3841. #define DUK_HEAP_STRING_TO_UTC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_UTC_STRING)
  3842. #define DUK_HTHREAD_STRING_TO_UTC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_UTC_STRING)
  3843. #define DUK_HEAP_STRING_SET_UTC_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_FULL_YEAR)
  3844. #define DUK_HTHREAD_STRING_SET_UTC_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_FULL_YEAR)
  3845. #define DUK_HEAP_STRING_SET_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FULL_YEAR)
  3846. #define DUK_HTHREAD_STRING_SET_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FULL_YEAR)
  3847. #define DUK_HEAP_STRING_SET_UTC_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MONTH)
  3848. #define DUK_HTHREAD_STRING_SET_UTC_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MONTH)
  3849. #define DUK_HEAP_STRING_SET_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MONTH)
  3850. #define DUK_HTHREAD_STRING_SET_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MONTH)
  3851. #define DUK_HEAP_STRING_SET_UTC_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_DATE)
  3852. #define DUK_HTHREAD_STRING_SET_UTC_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_DATE)
  3853. #define DUK_HEAP_STRING_SET_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_DATE)
  3854. #define DUK_HTHREAD_STRING_SET_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_DATE)
  3855. #define DUK_HEAP_STRING_SET_UTC_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_HOURS)
  3856. #define DUK_HTHREAD_STRING_SET_UTC_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_HOURS)
  3857. #define DUK_HEAP_STRING_SET_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_HOURS)
  3858. #define DUK_HTHREAD_STRING_SET_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_HOURS)
  3859. #define DUK_HEAP_STRING_SET_UTC_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MINUTES)
  3860. #define DUK_HTHREAD_STRING_SET_UTC_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MINUTES)
  3861. #define DUK_HEAP_STRING_SET_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MINUTES)
  3862. #define DUK_HTHREAD_STRING_SET_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MINUTES)
  3863. #define DUK_HEAP_STRING_SET_UTC_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_SECONDS)
  3864. #define DUK_HTHREAD_STRING_SET_UTC_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_SECONDS)
  3865. #define DUK_HEAP_STRING_SET_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_SECONDS)
  3866. #define DUK_HTHREAD_STRING_SET_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_SECONDS)
  3867. #define DUK_HEAP_STRING_SET_UTC_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MILLISECONDS)
  3868. #define DUK_HTHREAD_STRING_SET_UTC_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MILLISECONDS)
  3869. #define DUK_HEAP_STRING_SET_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MILLISECONDS)
  3870. #define DUK_HTHREAD_STRING_SET_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MILLISECONDS)
  3871. #define DUK_HEAP_STRING_SET_TIME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_TIME)
  3872. #define DUK_HTHREAD_STRING_SET_TIME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_TIME)
  3873. #define DUK_HEAP_STRING_GET_TIMEZONE_OFFSET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_TIMEZONE_OFFSET)
  3874. #define DUK_HTHREAD_STRING_GET_TIMEZONE_OFFSET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_TIMEZONE_OFFSET)
  3875. #define DUK_HEAP_STRING_GET_UTC_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MILLISECONDS)
  3876. #define DUK_HTHREAD_STRING_GET_UTC_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MILLISECONDS)
  3877. #define DUK_HEAP_STRING_GET_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MILLISECONDS)
  3878. #define DUK_HTHREAD_STRING_GET_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MILLISECONDS)
  3879. #define DUK_HEAP_STRING_GET_UTC_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_SECONDS)
  3880. #define DUK_HTHREAD_STRING_GET_UTC_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_SECONDS)
  3881. #define DUK_HEAP_STRING_GET_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_SECONDS)
  3882. #define DUK_HTHREAD_STRING_GET_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_SECONDS)
  3883. #define DUK_HEAP_STRING_GET_UTC_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MINUTES)
  3884. #define DUK_HTHREAD_STRING_GET_UTC_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MINUTES)
  3885. #define DUK_HEAP_STRING_GET_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MINUTES)
  3886. #define DUK_HTHREAD_STRING_GET_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MINUTES)
  3887. #define DUK_HEAP_STRING_GET_UTC_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_HOURS)
  3888. #define DUK_HTHREAD_STRING_GET_UTC_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_HOURS)
  3889. #define DUK_HEAP_STRING_GET_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_HOURS)
  3890. #define DUK_HTHREAD_STRING_GET_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_HOURS)
  3891. #define DUK_HEAP_STRING_GET_UTC_DAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_DAY)
  3892. #define DUK_HTHREAD_STRING_GET_UTC_DAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_DAY)
  3893. #define DUK_HEAP_STRING_GET_DAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_DAY)
  3894. #define DUK_HTHREAD_STRING_GET_DAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_DAY)
  3895. #define DUK_HEAP_STRING_GET_UTC_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_DATE)
  3896. #define DUK_HTHREAD_STRING_GET_UTC_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_DATE)
  3897. #define DUK_HEAP_STRING_GET_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_DATE)
  3898. #define DUK_HTHREAD_STRING_GET_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_DATE)
  3899. #define DUK_HEAP_STRING_GET_UTC_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MONTH)
  3900. #define DUK_HTHREAD_STRING_GET_UTC_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MONTH)
  3901. #define DUK_HEAP_STRING_GET_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MONTH)
  3902. #define DUK_HTHREAD_STRING_GET_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MONTH)
  3903. #define DUK_HEAP_STRING_GET_UTC_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_FULL_YEAR)
  3904. #define DUK_HTHREAD_STRING_GET_UTC_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_FULL_YEAR)
  3905. #define DUK_HEAP_STRING_GET_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FULL_YEAR)
  3906. #define DUK_HTHREAD_STRING_GET_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FULL_YEAR)
  3907. #define DUK_HEAP_STRING_GET_TIME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_TIME)
  3908. #define DUK_HTHREAD_STRING_GET_TIME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_TIME)
  3909. #define DUK_HEAP_STRING_TO_LOCALE_TIME_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_TIME_STRING)
  3910. #define DUK_HTHREAD_STRING_TO_LOCALE_TIME_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_TIME_STRING)
  3911. #define DUK_HEAP_STRING_TO_LOCALE_DATE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_DATE_STRING)
  3912. #define DUK_HTHREAD_STRING_TO_LOCALE_DATE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_DATE_STRING)
  3913. #define DUK_HEAP_STRING_TO_TIME_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_TIME_STRING)
  3914. #define DUK_HTHREAD_STRING_TO_TIME_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_TIME_STRING)
  3915. #define DUK_HEAP_STRING_TO_DATE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_DATE_STRING)
  3916. #define DUK_HTHREAD_STRING_TO_DATE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_DATE_STRING)
  3917. #define DUK_HEAP_STRING_NOW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NOW)
  3918. #define DUK_HTHREAD_STRING_NOW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NOW)
  3919. #define DUK_HEAP_STRING_UTC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UTC)
  3920. #define DUK_HTHREAD_STRING_UTC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UTC)
  3921. #define DUK_HEAP_STRING_PARSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE)
  3922. #define DUK_HTHREAD_STRING_PARSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE)
  3923. #define DUK_HEAP_STRING_TO_PRECISION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_PRECISION)
  3924. #define DUK_HTHREAD_STRING_TO_PRECISION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_PRECISION)
  3925. #define DUK_HEAP_STRING_TO_EXPONENTIAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_EXPONENTIAL)
  3926. #define DUK_HTHREAD_STRING_TO_EXPONENTIAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_EXPONENTIAL)
  3927. #define DUK_HEAP_STRING_TO_FIXED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_FIXED)
  3928. #define DUK_HTHREAD_STRING_TO_FIXED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_FIXED)
  3929. #define DUK_HEAP_STRING_POSITIVE_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POSITIVE_INFINITY)
  3930. #define DUK_HTHREAD_STRING_POSITIVE_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POSITIVE_INFINITY)
  3931. #define DUK_HEAP_STRING_NEGATIVE_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEGATIVE_INFINITY)
  3932. #define DUK_HTHREAD_STRING_NEGATIVE_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEGATIVE_INFINITY)
  3933. #define DUK_HEAP_STRING_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAN)
  3934. #define DUK_HTHREAD_STRING_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAN)
  3935. #define DUK_HEAP_STRING_MIN_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MIN_VALUE)
  3936. #define DUK_HTHREAD_STRING_MIN_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MIN_VALUE)
  3937. #define DUK_HEAP_STRING_MAX_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAX_VALUE)
  3938. #define DUK_HTHREAD_STRING_MAX_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAX_VALUE)
  3939. #define DUK_HEAP_STRING_SUBSTR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBSTR)
  3940. #define DUK_HTHREAD_STRING_SUBSTR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBSTR)
  3941. #define DUK_HEAP_STRING_TRIM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRIM)
  3942. #define DUK_HTHREAD_STRING_TRIM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRIM)
  3943. #define DUK_HEAP_STRING_TO_LOCALE_UPPER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_UPPER_CASE)
  3944. #define DUK_HTHREAD_STRING_TO_LOCALE_UPPER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_UPPER_CASE)
  3945. #define DUK_HEAP_STRING_TO_UPPER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_UPPER_CASE)
  3946. #define DUK_HTHREAD_STRING_TO_UPPER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_UPPER_CASE)
  3947. #define DUK_HEAP_STRING_TO_LOCALE_LOWER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_LOWER_CASE)
  3948. #define DUK_HTHREAD_STRING_TO_LOCALE_LOWER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_LOWER_CASE)
  3949. #define DUK_HEAP_STRING_TO_LOWER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOWER_CASE)
  3950. #define DUK_HTHREAD_STRING_TO_LOWER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOWER_CASE)
  3951. #define DUK_HEAP_STRING_SUBSTRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBSTRING)
  3952. #define DUK_HTHREAD_STRING_SUBSTRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBSTRING)
  3953. #define DUK_HEAP_STRING_SPLIT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPLIT)
  3954. #define DUK_HTHREAD_STRING_SPLIT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPLIT)
  3955. #define DUK_HEAP_STRING_SEARCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SEARCH)
  3956. #define DUK_HTHREAD_STRING_SEARCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SEARCH)
  3957. #define DUK_HEAP_STRING_REPLACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REPLACE)
  3958. #define DUK_HTHREAD_STRING_REPLACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REPLACE)
  3959. #define DUK_HEAP_STRING_MATCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MATCH)
  3960. #define DUK_HTHREAD_STRING_MATCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MATCH)
  3961. #define DUK_HEAP_STRING_LOCALE_COMPARE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOCALE_COMPARE)
  3962. #define DUK_HTHREAD_STRING_LOCALE_COMPARE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOCALE_COMPARE)
  3963. #define DUK_HEAP_STRING_CHAR_CODE_AT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CHAR_CODE_AT)
  3964. #define DUK_HTHREAD_STRING_CHAR_CODE_AT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CHAR_CODE_AT)
  3965. #define DUK_HEAP_STRING_CHAR_AT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CHAR_AT)
  3966. #define DUK_HTHREAD_STRING_CHAR_AT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CHAR_AT)
  3967. #define DUK_HEAP_STRING_FROM_CHAR_CODE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FROM_CHAR_CODE)
  3968. #define DUK_HTHREAD_STRING_FROM_CHAR_CODE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FROM_CHAR_CODE)
  3969. #define DUK_HEAP_STRING_REDUCE_RIGHT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REDUCE_RIGHT)
  3970. #define DUK_HTHREAD_STRING_REDUCE_RIGHT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REDUCE_RIGHT)
  3971. #define DUK_HEAP_STRING_REDUCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REDUCE)
  3972. #define DUK_HTHREAD_STRING_REDUCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REDUCE)
  3973. #define DUK_HEAP_STRING_FILTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILTER)
  3974. #define DUK_HTHREAD_STRING_FILTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILTER)
  3975. #define DUK_HEAP_STRING_MAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAP)
  3976. #define DUK_HTHREAD_STRING_MAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAP)
  3977. #define DUK_HEAP_STRING_FOR_EACH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FOR_EACH)
  3978. #define DUK_HTHREAD_STRING_FOR_EACH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FOR_EACH)
  3979. #define DUK_HEAP_STRING_SOME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SOME)
  3980. #define DUK_HTHREAD_STRING_SOME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SOME)
  3981. #define DUK_HEAP_STRING_EVERY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVERY)
  3982. #define DUK_HTHREAD_STRING_EVERY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVERY)
  3983. #define DUK_HEAP_STRING_LAST_INDEX_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LAST_INDEX_OF)
  3984. #define DUK_HTHREAD_STRING_LAST_INDEX_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LAST_INDEX_OF)
  3985. #define DUK_HEAP_STRING_INDEX_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INDEX_OF)
  3986. #define DUK_HTHREAD_STRING_INDEX_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INDEX_OF)
  3987. #define DUK_HEAP_STRING_UNSHIFT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UNSHIFT)
  3988. #define DUK_HTHREAD_STRING_UNSHIFT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UNSHIFT)
  3989. #define DUK_HEAP_STRING_SPLICE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPLICE)
  3990. #define DUK_HTHREAD_STRING_SPLICE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPLICE)
  3991. #define DUK_HEAP_STRING_SORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SORT)
  3992. #define DUK_HTHREAD_STRING_SORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SORT)
  3993. #define DUK_HEAP_STRING_SLICE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SLICE)
  3994. #define DUK_HTHREAD_STRING_SLICE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SLICE)
  3995. #define DUK_HEAP_STRING_SHIFT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SHIFT)
  3996. #define DUK_HTHREAD_STRING_SHIFT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SHIFT)
  3997. #define DUK_HEAP_STRING_REVERSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REVERSE)
  3998. #define DUK_HTHREAD_STRING_REVERSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REVERSE)
  3999. #define DUK_HEAP_STRING_PUSH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PUSH)
  4000. #define DUK_HTHREAD_STRING_PUSH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PUSH)
  4001. #define DUK_HEAP_STRING_POP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POP)
  4002. #define DUK_HTHREAD_STRING_POP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POP)
  4003. #define DUK_HEAP_STRING_JOIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JOIN)
  4004. #define DUK_HTHREAD_STRING_JOIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JOIN)
  4005. #define DUK_HEAP_STRING_CONCAT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONCAT)
  4006. #define DUK_HTHREAD_STRING_CONCAT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONCAT)
  4007. #define DUK_HEAP_STRING_IS_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_ARRAY)
  4008. #define DUK_HTHREAD_STRING_IS_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_ARRAY)
  4009. #define DUK_HEAP_STRING_LC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ARGUMENTS)
  4010. #define DUK_HTHREAD_STRING_LC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ARGUMENTS)
  4011. #define DUK_HEAP_STRING_CALLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLER)
  4012. #define DUK_HTHREAD_STRING_CALLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLER)
  4013. #define DUK_HEAP_STRING_BIND(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BIND)
  4014. #define DUK_HTHREAD_STRING_BIND(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BIND)
  4015. #define DUK_HEAP_STRING_CALL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALL)
  4016. #define DUK_HTHREAD_STRING_CALL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALL)
  4017. #define DUK_HEAP_STRING_APPLY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_APPLY)
  4018. #define DUK_HTHREAD_STRING_APPLY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_APPLY)
  4019. #define DUK_HEAP_STRING_PROPERTY_IS_ENUMERABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROPERTY_IS_ENUMERABLE)
  4020. #define DUK_HTHREAD_STRING_PROPERTY_IS_ENUMERABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROPERTY_IS_ENUMERABLE)
  4021. #define DUK_HEAP_STRING_IS_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_PROTOTYPE_OF)
  4022. #define DUK_HTHREAD_STRING_IS_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_PROTOTYPE_OF)
  4023. #define DUK_HEAP_STRING_HAS_OWN_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HAS_OWN_PROPERTY)
  4024. #define DUK_HTHREAD_STRING_HAS_OWN_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HAS_OWN_PROPERTY)
  4025. #define DUK_HEAP_STRING_VALUE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE_OF)
  4026. #define DUK_HTHREAD_STRING_VALUE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE_OF)
  4027. #define DUK_HEAP_STRING_TO_LOCALE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_STRING)
  4028. #define DUK_HTHREAD_STRING_TO_LOCALE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_STRING)
  4029. #define DUK_HEAP_STRING_TO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_STRING)
  4030. #define DUK_HTHREAD_STRING_TO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_STRING)
  4031. #define DUK_HEAP_STRING_CONSTRUCTOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONSTRUCTOR)
  4032. #define DUK_HTHREAD_STRING_CONSTRUCTOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONSTRUCTOR)
  4033. #define DUK_HEAP_STRING_SET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET)
  4034. #define DUK_HTHREAD_STRING_SET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET)
  4035. #define DUK_HEAP_STRING_GET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET)
  4036. #define DUK_HTHREAD_STRING_GET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET)
  4037. #define DUK_HEAP_STRING_ENUMERABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERABLE)
  4038. #define DUK_HTHREAD_STRING_ENUMERABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERABLE)
  4039. #define DUK_HEAP_STRING_CONFIGURABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONFIGURABLE)
  4040. #define DUK_HTHREAD_STRING_CONFIGURABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONFIGURABLE)
  4041. #define DUK_HEAP_STRING_WRITABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITABLE)
  4042. #define DUK_HTHREAD_STRING_WRITABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITABLE)
  4043. #define DUK_HEAP_STRING_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE)
  4044. #define DUK_HTHREAD_STRING_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE)
  4045. #define DUK_HEAP_STRING_KEYS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_KEYS)
  4046. #define DUK_HTHREAD_STRING_KEYS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_KEYS)
  4047. #define DUK_HEAP_STRING_IS_EXTENSIBLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_EXTENSIBLE)
  4048. #define DUK_HTHREAD_STRING_IS_EXTENSIBLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_EXTENSIBLE)
  4049. #define DUK_HEAP_STRING_IS_FROZEN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_FROZEN)
  4050. #define DUK_HTHREAD_STRING_IS_FROZEN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_FROZEN)
  4051. #define DUK_HEAP_STRING_IS_SEALED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_SEALED)
  4052. #define DUK_HTHREAD_STRING_IS_SEALED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_SEALED)
  4053. #define DUK_HEAP_STRING_PREVENT_EXTENSIONS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PREVENT_EXTENSIONS)
  4054. #define DUK_HTHREAD_STRING_PREVENT_EXTENSIONS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PREVENT_EXTENSIONS)
  4055. #define DUK_HEAP_STRING_FREEZE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FREEZE)
  4056. #define DUK_HTHREAD_STRING_FREEZE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FREEZE)
  4057. #define DUK_HEAP_STRING_SEAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SEAL)
  4058. #define DUK_HTHREAD_STRING_SEAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SEAL)
  4059. #define DUK_HEAP_STRING_DEFINE_PROPERTIES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFINE_PROPERTIES)
  4060. #define DUK_HTHREAD_STRING_DEFINE_PROPERTIES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFINE_PROPERTIES)
  4061. #define DUK_HEAP_STRING_DEFINE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFINE_PROPERTY)
  4062. #define DUK_HTHREAD_STRING_DEFINE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFINE_PROPERTY)
  4063. #define DUK_HEAP_STRING_CREATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CREATE)
  4064. #define DUK_HTHREAD_STRING_CREATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CREATE)
  4065. #define DUK_HEAP_STRING_GET_OWN_PROPERTY_NAMES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_OWN_PROPERTY_NAMES)
  4066. #define DUK_HTHREAD_STRING_GET_OWN_PROPERTY_NAMES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_OWN_PROPERTY_NAMES)
  4067. #define DUK_HEAP_STRING_GET_OWN_PROPERTY_DESCRIPTOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR)
  4068. #define DUK_HTHREAD_STRING_GET_OWN_PROPERTY_DESCRIPTOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR)
  4069. #define DUK_HEAP_STRING_GET_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_PROTOTYPE_OF)
  4070. #define DUK_HTHREAD_STRING_GET_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_PROTOTYPE_OF)
  4071. #define DUK_HEAP_STRING_PROTOTYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTOTYPE)
  4072. #define DUK_HTHREAD_STRING_PROTOTYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTOTYPE)
  4073. #define DUK_HEAP_STRING_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LENGTH)
  4074. #define DUK_HTHREAD_STRING_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LENGTH)
  4075. #define DUK_HEAP_STRING_ALERT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ALERT)
  4076. #define DUK_HTHREAD_STRING_ALERT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ALERT)
  4077. #define DUK_HEAP_STRING_PRINT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PRINT)
  4078. #define DUK_HTHREAD_STRING_PRINT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PRINT)
  4079. #define DUK_HEAP_STRING_UNESCAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UNESCAPE)
  4080. #define DUK_HTHREAD_STRING_UNESCAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UNESCAPE)
  4081. #define DUK_HEAP_STRING_ESCAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ESCAPE)
  4082. #define DUK_HTHREAD_STRING_ESCAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ESCAPE)
  4083. #define DUK_HEAP_STRING_ENCODE_URI_COMPONENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENCODE_URI_COMPONENT)
  4084. #define DUK_HTHREAD_STRING_ENCODE_URI_COMPONENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENCODE_URI_COMPONENT)
  4085. #define DUK_HEAP_STRING_ENCODE_URI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENCODE_URI)
  4086. #define DUK_HTHREAD_STRING_ENCODE_URI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENCODE_URI)
  4087. #define DUK_HEAP_STRING_DECODE_URI_COMPONENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DECODE_URI_COMPONENT)
  4088. #define DUK_HTHREAD_STRING_DECODE_URI_COMPONENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DECODE_URI_COMPONENT)
  4089. #define DUK_HEAP_STRING_DECODE_URI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DECODE_URI)
  4090. #define DUK_HTHREAD_STRING_DECODE_URI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DECODE_URI)
  4091. #define DUK_HEAP_STRING_IS_FINITE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_FINITE)
  4092. #define DUK_HTHREAD_STRING_IS_FINITE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_FINITE)
  4093. #define DUK_HEAP_STRING_IS_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_NAN)
  4094. #define DUK_HTHREAD_STRING_IS_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_NAN)
  4095. #define DUK_HEAP_STRING_PARSE_FLOAT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE_FLOAT)
  4096. #define DUK_HTHREAD_STRING_PARSE_FLOAT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE_FLOAT)
  4097. #define DUK_HEAP_STRING_PARSE_INT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE_INT)
  4098. #define DUK_HTHREAD_STRING_PARSE_INT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE_INT)
  4099. #define DUK_HEAP_STRING_EVAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVAL)
  4100. #define DUK_HTHREAD_STRING_EVAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVAL)
  4101. #define DUK_HEAP_STRING_URI_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_URI_ERROR)
  4102. #define DUK_HTHREAD_STRING_URI_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_URI_ERROR)
  4103. #define DUK_HEAP_STRING_TYPE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPE_ERROR)
  4104. #define DUK_HTHREAD_STRING_TYPE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPE_ERROR)
  4105. #define DUK_HEAP_STRING_SYNTAX_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SYNTAX_ERROR)
  4106. #define DUK_HTHREAD_STRING_SYNTAX_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SYNTAX_ERROR)
  4107. #define DUK_HEAP_STRING_REFERENCE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REFERENCE_ERROR)
  4108. #define DUK_HTHREAD_STRING_REFERENCE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REFERENCE_ERROR)
  4109. #define DUK_HEAP_STRING_RANGE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RANGE_ERROR)
  4110. #define DUK_HTHREAD_STRING_RANGE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RANGE_ERROR)
  4111. #define DUK_HEAP_STRING_EVAL_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVAL_ERROR)
  4112. #define DUK_HTHREAD_STRING_EVAL_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVAL_ERROR)
  4113. #define DUK_HEAP_STRING_BREAK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BREAK)
  4114. #define DUK_HTHREAD_STRING_BREAK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BREAK)
  4115. #define DUK_HEAP_STRING_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CASE)
  4116. #define DUK_HTHREAD_STRING_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CASE)
  4117. #define DUK_HEAP_STRING_CATCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CATCH)
  4118. #define DUK_HTHREAD_STRING_CATCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CATCH)
  4119. #define DUK_HEAP_STRING_CONTINUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONTINUE)
  4120. #define DUK_HTHREAD_STRING_CONTINUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONTINUE)
  4121. #define DUK_HEAP_STRING_DEBUGGER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEBUGGER)
  4122. #define DUK_HTHREAD_STRING_DEBUGGER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEBUGGER)
  4123. #define DUK_HEAP_STRING_DEFAULT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFAULT)
  4124. #define DUK_HTHREAD_STRING_DEFAULT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFAULT)
  4125. #define DUK_HEAP_STRING_DELETE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE)
  4126. #define DUK_HTHREAD_STRING_DELETE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE)
  4127. #define DUK_HEAP_STRING_DO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DO)
  4128. #define DUK_HTHREAD_STRING_DO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DO)
  4129. #define DUK_HEAP_STRING_ELSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ELSE)
  4130. #define DUK_HTHREAD_STRING_ELSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ELSE)
  4131. #define DUK_HEAP_STRING_FINALLY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FINALLY)
  4132. #define DUK_HTHREAD_STRING_FINALLY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FINALLY)
  4133. #define DUK_HEAP_STRING_FOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FOR)
  4134. #define DUK_HTHREAD_STRING_FOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FOR)
  4135. #define DUK_HEAP_STRING_LC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FUNCTION)
  4136. #define DUK_HTHREAD_STRING_LC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FUNCTION)
  4137. #define DUK_HEAP_STRING_IF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IF)
  4138. #define DUK_HTHREAD_STRING_IF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IF)
  4139. #define DUK_HEAP_STRING_IN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IN)
  4140. #define DUK_HTHREAD_STRING_IN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IN)
  4141. #define DUK_HEAP_STRING_INSTANCEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INSTANCEOF)
  4142. #define DUK_HTHREAD_STRING_INSTANCEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INSTANCEOF)
  4143. #define DUK_HEAP_STRING_NEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEW)
  4144. #define DUK_HTHREAD_STRING_NEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEW)
  4145. #define DUK_HEAP_STRING_RETURN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RETURN)
  4146. #define DUK_HTHREAD_STRING_RETURN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RETURN)
  4147. #define DUK_HEAP_STRING_SWITCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SWITCH)
  4148. #define DUK_HTHREAD_STRING_SWITCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SWITCH)
  4149. #define DUK_HEAP_STRING_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THIS)
  4150. #define DUK_HTHREAD_STRING_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THIS)
  4151. #define DUK_HEAP_STRING_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THROW)
  4152. #define DUK_HTHREAD_STRING_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THROW)
  4153. #define DUK_HEAP_STRING_TRY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRY)
  4154. #define DUK_HTHREAD_STRING_TRY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRY)
  4155. #define DUK_HEAP_STRING_TYPEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPEOF)
  4156. #define DUK_HTHREAD_STRING_TYPEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPEOF)
  4157. #define DUK_HEAP_STRING_VAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VAR)
  4158. #define DUK_HTHREAD_STRING_VAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VAR)
  4159. #define DUK_HEAP_STRING_CONST(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONST)
  4160. #define DUK_HTHREAD_STRING_CONST(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONST)
  4161. #define DUK_HEAP_STRING_VOID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VOID)
  4162. #define DUK_HTHREAD_STRING_VOID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VOID)
  4163. #define DUK_HEAP_STRING_WHILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WHILE)
  4164. #define DUK_HTHREAD_STRING_WHILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WHILE)
  4165. #define DUK_HEAP_STRING_WITH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WITH)
  4166. #define DUK_HTHREAD_STRING_WITH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WITH)
  4167. #define DUK_HEAP_STRING_CLASS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLASS)
  4168. #define DUK_HTHREAD_STRING_CLASS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLASS)
  4169. #define DUK_HEAP_STRING_ENUM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUM)
  4170. #define DUK_HTHREAD_STRING_ENUM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUM)
  4171. #define DUK_HEAP_STRING_EXPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORT)
  4172. #define DUK_HTHREAD_STRING_EXPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORT)
  4173. #define DUK_HEAP_STRING_EXTENDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXTENDS)
  4174. #define DUK_HTHREAD_STRING_EXTENDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXTENDS)
  4175. #define DUK_HEAP_STRING_IMPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPORT)
  4176. #define DUK_HTHREAD_STRING_IMPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPORT)
  4177. #define DUK_HEAP_STRING_SUPER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUPER)
  4178. #define DUK_HTHREAD_STRING_SUPER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUPER)
  4179. #define DUK_HEAP_STRING_LC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NULL)
  4180. #define DUK_HTHREAD_STRING_LC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NULL)
  4181. #define DUK_HEAP_STRING_TRUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRUE)
  4182. #define DUK_HTHREAD_STRING_TRUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRUE)
  4183. #define DUK_HEAP_STRING_FALSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FALSE)
  4184. #define DUK_HTHREAD_STRING_FALSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FALSE)
  4185. #define DUK_HEAP_STRING_IMPLEMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPLEMENTS)
  4186. #define DUK_HTHREAD_STRING_IMPLEMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPLEMENTS)
  4187. #define DUK_HEAP_STRING_INTERFACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INTERFACE)
  4188. #define DUK_HTHREAD_STRING_INTERFACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INTERFACE)
  4189. #define DUK_HEAP_STRING_LET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LET)
  4190. #define DUK_HTHREAD_STRING_LET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LET)
  4191. #define DUK_HEAP_STRING_PACKAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PACKAGE)
  4192. #define DUK_HTHREAD_STRING_PACKAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PACKAGE)
  4193. #define DUK_HEAP_STRING_PRIVATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PRIVATE)
  4194. #define DUK_HTHREAD_STRING_PRIVATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PRIVATE)
  4195. #define DUK_HEAP_STRING_PROTECTED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTECTED)
  4196. #define DUK_HTHREAD_STRING_PROTECTED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTECTED)
  4197. #define DUK_HEAP_STRING_PUBLIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PUBLIC)
  4198. #define DUK_HTHREAD_STRING_PUBLIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PUBLIC)
  4199. #define DUK_HEAP_STRING_STATIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STATIC)
  4200. #define DUK_HTHREAD_STRING_STATIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STATIC)
  4201. #define DUK_HEAP_STRING_YIELD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_YIELD)
  4202. #define DUK_HTHREAD_STRING_YIELD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_YIELD)
  4203. #define DUK_HEAP_NUM_STRINGS 414
  4204. #define DUK_STRIDX_START_RESERVED 369
  4205. #define DUK_STRIDX_START_STRICT_RESERVED 405
  4206. #define DUK_STRIDX_END_RESERVED 414 /* exclusive endpoint */
  4207. #if !defined(DUK_SINGLE_FILE)
  4208. DUK_INTERNAL_DECL const duk_c_function duk_bi_native_functions[147];
  4209. DUK_INTERNAL_DECL const duk_uint8_t duk_builtins_data[1952];
  4210. #ifdef DUK_USE_BUILTIN_INITJS
  4211. DUK_INTERNAL_DECL const duk_uint8_t duk_initjs_data[1757];
  4212. #endif /* DUK_USE_BUILTIN_INITJS */
  4213. #endif /* !DUK_SINGLE_FILE */
  4214. #define DUK_BUILTINS_DATA_LENGTH 1952
  4215. #ifdef DUK_USE_BUILTIN_INITJS
  4216. #define DUK_BUILTIN_INITJS_DATA_LENGTH 1757
  4217. #endif /* DUK_USE_BUILTIN_INITJS */
  4218. #define DUK_BIDX_GLOBAL 0
  4219. #define DUK_BIDX_GLOBAL_ENV 1
  4220. #define DUK_BIDX_OBJECT_CONSTRUCTOR 2
  4221. #define DUK_BIDX_OBJECT_PROTOTYPE 3
  4222. #define DUK_BIDX_FUNCTION_CONSTRUCTOR 4
  4223. #define DUK_BIDX_FUNCTION_PROTOTYPE 5
  4224. #define DUK_BIDX_ARRAY_CONSTRUCTOR 6
  4225. #define DUK_BIDX_ARRAY_PROTOTYPE 7
  4226. #define DUK_BIDX_STRING_CONSTRUCTOR 8
  4227. #define DUK_BIDX_STRING_PROTOTYPE 9
  4228. #define DUK_BIDX_BOOLEAN_CONSTRUCTOR 10
  4229. #define DUK_BIDX_BOOLEAN_PROTOTYPE 11
  4230. #define DUK_BIDX_NUMBER_CONSTRUCTOR 12
  4231. #define DUK_BIDX_NUMBER_PROTOTYPE 13
  4232. #define DUK_BIDX_DATE_CONSTRUCTOR 14
  4233. #define DUK_BIDX_DATE_PROTOTYPE 15
  4234. #define DUK_BIDX_REGEXP_CONSTRUCTOR 16
  4235. #define DUK_BIDX_REGEXP_PROTOTYPE 17
  4236. #define DUK_BIDX_ERROR_CONSTRUCTOR 18
  4237. #define DUK_BIDX_ERROR_PROTOTYPE 19
  4238. #define DUK_BIDX_EVAL_ERROR_CONSTRUCTOR 20
  4239. #define DUK_BIDX_EVAL_ERROR_PROTOTYPE 21
  4240. #define DUK_BIDX_RANGE_ERROR_CONSTRUCTOR 22
  4241. #define DUK_BIDX_RANGE_ERROR_PROTOTYPE 23
  4242. #define DUK_BIDX_REFERENCE_ERROR_CONSTRUCTOR 24
  4243. #define DUK_BIDX_REFERENCE_ERROR_PROTOTYPE 25
  4244. #define DUK_BIDX_SYNTAX_ERROR_CONSTRUCTOR 26
  4245. #define DUK_BIDX_SYNTAX_ERROR_PROTOTYPE 27
  4246. #define DUK_BIDX_TYPE_ERROR_CONSTRUCTOR 28
  4247. #define DUK_BIDX_TYPE_ERROR_PROTOTYPE 29
  4248. #define DUK_BIDX_URI_ERROR_CONSTRUCTOR 30
  4249. #define DUK_BIDX_URI_ERROR_PROTOTYPE 31
  4250. #define DUK_BIDX_MATH 32
  4251. #define DUK_BIDX_JSON 33
  4252. #define DUK_BIDX_TYPE_ERROR_THROWER 34
  4253. #define DUK_BIDX_PROXY_CONSTRUCTOR 35
  4254. #define DUK_BIDX_DUKTAPE 36
  4255. #define DUK_BIDX_THREAD_CONSTRUCTOR 37
  4256. #define DUK_BIDX_THREAD_PROTOTYPE 38
  4257. #define DUK_BIDX_BUFFER_CONSTRUCTOR 39
  4258. #define DUK_BIDX_BUFFER_PROTOTYPE 40
  4259. #define DUK_BIDX_POINTER_CONSTRUCTOR 41
  4260. #define DUK_BIDX_POINTER_PROTOTYPE 42
  4261. #define DUK_BIDX_LOGGER_CONSTRUCTOR 43
  4262. #define DUK_BIDX_LOGGER_PROTOTYPE 44
  4263. #define DUK_BIDX_DOUBLE_ERROR 45
  4264. #define DUK_BIDX_ARRAYBUFFER_CONSTRUCTOR 46
  4265. #define DUK_BIDX_ARRAYBUFFER_PROTOTYPE 47
  4266. #define DUK_BIDX_DATAVIEW_CONSTRUCTOR 48
  4267. #define DUK_BIDX_DATAVIEW_PROTOTYPE 49
  4268. #define DUK_BIDX_TYPEDARRAY_PROTOTYPE 50
  4269. #define DUK_BIDX_INT8ARRAY_CONSTRUCTOR 51
  4270. #define DUK_BIDX_INT8ARRAY_PROTOTYPE 52
  4271. #define DUK_BIDX_UINT8ARRAY_CONSTRUCTOR 53
  4272. #define DUK_BIDX_UINT8ARRAY_PROTOTYPE 54
  4273. #define DUK_BIDX_UINT8CLAMPEDARRAY_CONSTRUCTOR 55
  4274. #define DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE 56
  4275. #define DUK_BIDX_INT16ARRAY_CONSTRUCTOR 57
  4276. #define DUK_BIDX_INT16ARRAY_PROTOTYPE 58
  4277. #define DUK_BIDX_UINT16ARRAY_CONSTRUCTOR 59
  4278. #define DUK_BIDX_UINT16ARRAY_PROTOTYPE 60
  4279. #define DUK_BIDX_INT32ARRAY_CONSTRUCTOR 61
  4280. #define DUK_BIDX_INT32ARRAY_PROTOTYPE 62
  4281. #define DUK_BIDX_UINT32ARRAY_CONSTRUCTOR 63
  4282. #define DUK_BIDX_UINT32ARRAY_PROTOTYPE 64
  4283. #define DUK_BIDX_FLOAT32ARRAY_CONSTRUCTOR 65
  4284. #define DUK_BIDX_FLOAT32ARRAY_PROTOTYPE 66
  4285. #define DUK_BIDX_FLOAT64ARRAY_CONSTRUCTOR 67
  4286. #define DUK_BIDX_FLOAT64ARRAY_PROTOTYPE 68
  4287. #define DUK_BIDX_NODEJS_BUFFER_CONSTRUCTOR 69
  4288. #define DUK_BIDX_NODEJS_BUFFER_PROTOTYPE 70
  4289. #define DUK_NUM_BUILTINS 71
  4290. #else
  4291. #error invalid endianness defines
  4292. #endif
  4293. #endif /* DUK_BUILTINS_H_INCLUDED */
  4294. #line 50 "duk_internal.h"
  4295. #line 1 "duk_util.h"
  4296. /*
  4297. * Utilities
  4298. */
  4299. #ifndef DUK_UTIL_H_INCLUDED
  4300. #define DUK_UTIL_H_INCLUDED
  4301. #define DUK_UTIL_MIN_HASH_PRIME 17 /* must match genhashsizes.py */
  4302. #define DUK_UTIL_GET_HASH_PROBE_STEP(hash) (duk_util_probe_steps[(hash) & 0x1f])
  4303. /*
  4304. * Endian conversion
  4305. */
  4306. #if defined(DUK_USE_INTEGER_LE)
  4307. #define DUK_HTON32(x) DUK_BSWAP32((x))
  4308. #define DUK_NTOH32(x) DUK_BSWAP32((x))
  4309. #define DUK_HTON16(x) DUK_BSWAP16((x))
  4310. #define DUK_NTOH16(x) DUK_BSWAP16((x))
  4311. #elif defined(DUK_USE_INTEGER_BE)
  4312. #define DUK_HTON32(x) (x)
  4313. #define DUK_NTOH32(x) (x)
  4314. #define DUK_HTON16(x) (x)
  4315. #define DUK_NTOH16(x) (x)
  4316. #else
  4317. #error internal error, endianness defines broken
  4318. #endif
  4319. /*
  4320. * Bitstream decoder
  4321. */
  4322. struct duk_bitdecoder_ctx {
  4323. const duk_uint8_t *data;
  4324. duk_size_t offset;
  4325. duk_size_t length;
  4326. duk_uint32_t currval;
  4327. duk_small_int_t currbits;
  4328. };
  4329. /*
  4330. * Bitstream encoder
  4331. */
  4332. struct duk_bitencoder_ctx {
  4333. duk_uint8_t *data;
  4334. duk_size_t offset;
  4335. duk_size_t length;
  4336. duk_uint32_t currval;
  4337. duk_small_int_t currbits;
  4338. duk_small_int_t truncated;
  4339. };
  4340. /*
  4341. * Raw write/read macros for big endian, unaligned basic values.
  4342. * Caller ensures there's enough space. The macros update the pointer
  4343. * argument automatically on resizes. The idiom seems a bit odd, but
  4344. * leads to compact code.
  4345. */
  4346. #define DUK_RAW_WRITE_U8(ptr,val) do { \
  4347. *(ptr)++ = (duk_uint8_t) (val); \
  4348. } while (0)
  4349. #define DUK_RAW_WRITE_U16_BE(ptr,val) duk_raw_write_u16_be(&(ptr), (duk_uint16_t) (val))
  4350. #define DUK_RAW_WRITE_U32_BE(ptr,val) duk_raw_write_u32_be(&(ptr), (duk_uint32_t) (val))
  4351. #define DUK_RAW_WRITE_DOUBLE_BE(ptr,val) duk_raw_write_double_be(&(ptr), (duk_double_t) (val))
  4352. #define DUK_RAW_WRITE_XUTF8(ptr,val) do { \
  4353. /* 'ptr' is evaluated both as LHS and RHS. */ \
  4354. duk_uint8_t *duk__ptr; \
  4355. duk_small_int_t duk__len; \
  4356. duk__ptr = (duk_uint8_t *) (ptr); \
  4357. duk__len = duk_unicode_encode_xutf8((duk_ucodepoint_t) (val), duk__ptr); \
  4358. duk__ptr += duk__len; \
  4359. (ptr) = duk__ptr; \
  4360. } while (0)
  4361. #define DUK_RAW_WRITE_CESU8(ptr,val) do { \
  4362. /* 'ptr' is evaluated both as LHS and RHS. */ \
  4363. duk_uint8_t *duk__ptr; \
  4364. duk_small_int_t duk__len; \
  4365. duk__ptr = (duk_uint8_t *) (ptr); \
  4366. duk__len = duk_unicode_encode_cesu8((duk_ucodepoint_t) (val), duk__ptr); \
  4367. duk__ptr += duk__len; \
  4368. (ptr) = duk__ptr; \
  4369. } while (0)
  4370. #define DUK_RAW_READ_U8(ptr) ((duk_uint8_t) (*(ptr)++))
  4371. #define DUK_RAW_READ_U16_BE(ptr) duk_raw_read_u16_be(&(ptr));
  4372. #define DUK_RAW_READ_U32_BE(ptr) duk_raw_read_u32_be(&(ptr));
  4373. #define DUK_RAW_READ_DOUBLE_BE(ptr) duk_raw_read_double_be(&(ptr));
  4374. /*
  4375. * Buffer writer (dynamic buffer only)
  4376. *
  4377. * Helper for writing to a dynamic buffer with a concept of a "spare" area
  4378. * to reduce resizes. You can ensure there is enough space beforehand and
  4379. * then write for a while without further checks, relying on a stable data
  4380. * pointer. Spare handling is automatic so call sites only indicate how
  4381. * much data they need right now.
  4382. *
  4383. * There are several ways to write using bufwriter. The best approach
  4384. * depends mainly on how much performance matters over code footprint.
  4385. * The key issues are (1) ensuring there is space and (2) keeping the
  4386. * pointers consistent. Fast code should ensure space for multiple writes
  4387. * with one ensure call. Fastest inner loop code can temporarily borrow
  4388. * the 'p' pointer but must write it back eventually.
  4389. *
  4390. * Be careful to ensure all macro arguments (other than static pointers like
  4391. * 'thr' and 'bw_ctx') are evaluated exactly once, using temporaries if
  4392. * necessary (if that's not possible, there should be a note near the macro).
  4393. * Buffer write arguments often contain arithmetic etc so this is
  4394. * particularly important here.
  4395. */
  4396. /* XXX: Migrate bufwriter and other read/write helpers to its own header? */
  4397. struct duk_bufwriter_ctx {
  4398. duk_uint8_t *p;
  4399. duk_uint8_t *p_base;
  4400. duk_uint8_t *p_limit;
  4401. duk_hbuffer_dynamic *buf;
  4402. };
  4403. #define DUK_BW_SPARE_ADD 64
  4404. #define DUK_BW_SPARE_SHIFT 4 /* 2^4 -> 1/16 = 6.25% spare */
  4405. /* Initialization and finalization (compaction), converting to other types. */
  4406. #define DUK_BW_INIT_PUSHBUF(thr,bw_ctx,sz) do { \
  4407. duk_bw_init_pushbuf((thr), (bw_ctx), (sz)); \
  4408. } while (0)
  4409. #define DUK_BW_INIT_WITHBUF(thr,bw_ctx,buf) do { \
  4410. duk_bw_init((thr), (bw_ctx), (buf)); \
  4411. } while (0)
  4412. #define DUK_BW_COMPACT(thr,bw_ctx) do { \
  4413. /* Make underlying buffer compact to match DUK_BW_GET_SIZE(). */ \
  4414. duk_bw_compact((thr), (bw_ctx)); \
  4415. } while (0)
  4416. #define DUK_BW_PUSH_AS_STRING(thr,bw_ctx) do { \
  4417. duk_push_lstring((duk_context *) (thr), \
  4418. (const char *) (bw_ctx)->p_base, \
  4419. (duk_size_t) ((bw_ctx)->p - (bw_ctx)->p_base)); \
  4420. } while (0)
  4421. /* Pointers may be NULL for a while when 'buf' size is zero and before any
  4422. * ENSURE calls have been made. Once an ENSURE has been made, the pointers
  4423. * are required to be non-NULL so that it's always valid to use memcpy() and
  4424. * memmove(), even for zero size.
  4425. */
  4426. #define DUK_BW_ASSERT_VALID_EXPR(thr,bw_ctx) \
  4427. DUK_ASSERT_EXPR((bw_ctx) != NULL && \
  4428. (bw_ctx)->buf != NULL && \
  4429. ((DUK_HBUFFER_DYNAMIC_GET_SIZE((bw_ctx)->buf) == 0) || \
  4430. ((bw_ctx)->p != NULL && \
  4431. (bw_ctx)->p_base != NULL && \
  4432. (bw_ctx)->p_limit != NULL && \
  4433. (bw_ctx)->p_limit >= (bw_ctx)->p_base && \
  4434. (bw_ctx)->p >= (bw_ctx)->p_base && \
  4435. (bw_ctx)->p <= (bw_ctx)->p_limit)))
  4436. #define DUK_BW_ASSERT_VALID(thr,bw_ctx) do { \
  4437. DUK_BW_ASSERT_VALID_EXPR((thr), (bw_ctx)); \
  4438. } while (0)
  4439. /* Working with the pointer and current size. */
  4440. #define DUK_BW_GET_PTR(thr,bw_ctx) \
  4441. ((bw_ctx)->p)
  4442. #define DUK_BW_SET_PTR(thr,bw_ctx,ptr) do { \
  4443. (bw_ctx)->p = (ptr); \
  4444. } while (0)
  4445. #define DUK_BW_ADD_PTR(thr,bw_ctx,delta) do { \
  4446. (bw_ctx)->p += (delta); \
  4447. } while (0)
  4448. #define DUK_BW_GET_BASEPTR(thr,bw_ctx) \
  4449. ((bw_ctx)->p_base)
  4450. #define DUK_BW_GET_LIMITPTR(thr,bw_ctx) \
  4451. ((bw_ctx)->p_limit)
  4452. #define DUK_BW_GET_SIZE(thr,bw_ctx) \
  4453. ((duk_size_t) ((bw_ctx)->p - (bw_ctx)->p_base))
  4454. #define DUK_BW_SET_SIZE(thr,bw_ctx,sz) do { \
  4455. DUK_ASSERT((duk_size_t) (sz) <= (duk_size_t) ((bw_ctx)->p - (bw_ctx)->p_base)); \
  4456. (bw_ctx)->p = (bw_ctx)->p_base + (sz); \
  4457. } while (0)
  4458. #define DUK_BW_RESET_SIZE(thr,bw_ctx) do { \
  4459. /* Reset to zero size, keep current limit. */ \
  4460. (bw_ctx)->p = (bw_ctx)->p_base; \
  4461. } while (0)
  4462. #define DUK_BW_GET_BUFFER(thr,bw_ctx) \
  4463. ((bw_ctx)->buf)
  4464. /* Ensuring (reserving) space. */
  4465. #define DUK_BW_ENSURE(thr,bw_ctx,sz) do { \
  4466. duk_size_t duk__sz, duk__space; \
  4467. DUK_BW_ASSERT_VALID((thr), (bw_ctx)); \
  4468. duk__sz = (sz); \
  4469. duk__space = (duk_size_t) ((bw_ctx)->p_limit - (bw_ctx)->p); \
  4470. if (duk__space < duk__sz) { \
  4471. (void) duk_bw_resize((thr), (bw_ctx), duk__sz); \
  4472. } \
  4473. } while (0)
  4474. /* NOTE: Multiple evaluation of 'ptr' in this macro. */
  4475. /* XXX: Rework to use an always-inline function? */
  4476. #define DUK_BW_ENSURE_RAW(thr,bw_ctx,sz,ptr) \
  4477. (((duk_size_t) ((bw_ctx)->p_limit - (ptr)) >= (sz)) ? \
  4478. (ptr) : \
  4479. ((bw_ctx)->p = (ptr), duk_bw_resize((thr),(bw_ctx),(sz))))
  4480. #define DUK_BW_ENSURE_GETPTR(thr,bw_ctx,sz) \
  4481. DUK_BW_ENSURE_RAW((thr), (bw_ctx), (sz), (bw_ctx)->p)
  4482. #define DUK_BW_ASSERT_SPACE_EXPR(thr,bw_ctx,sz) \
  4483. (DUK_BW_ASSERT_VALID_EXPR((thr), (bw_ctx)), \
  4484. DUK_ASSERT_EXPR((duk_size_t) ((bw_ctx)->p_limit - (bw_ctx)->p) >= (duk_size_t) (sz)))
  4485. #define DUK_BW_ASSERT_SPACE(thr,bw_ctx,sz) do { \
  4486. DUK_BW_ASSERT_SPACE_EXPR((thr), (bw_ctx), (sz)); \
  4487. } while (0)
  4488. /* Miscellaneous. */
  4489. #define DUK_BW_SETPTR_AND_COMPACT(thr,bw_ctx,ptr) do { \
  4490. (bw_ctx)->p = (ptr); \
  4491. duk_bw_compact((thr), (bw_ctx)); \
  4492. } while (0)
  4493. /* Fast write calls which assume you control the spare beforehand.
  4494. * Multibyte write variants exist and use a temporary write pointer
  4495. * because byte writes alias with anything: with a stored pointer
  4496. * explicit pointer load/stores get generated (e.g. gcc -Os).
  4497. */
  4498. #define DUK_BW_WRITE_RAW_U8(thr,bw_ctx,val) do { \
  4499. DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 1); \
  4500. *(bw_ctx)->p++ = (duk_uint8_t) (val); \
  4501. } while (0)
  4502. #define DUK_BW_WRITE_RAW_U8_2(thr,bw_ctx,val1,val2) do { \
  4503. duk_uint8_t *duk__p; \
  4504. DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 2); \
  4505. duk__p = (bw_ctx)->p; \
  4506. *duk__p++ = (duk_uint8_t) (val1); \
  4507. *duk__p++ = (duk_uint8_t) (val2); \
  4508. (bw_ctx)->p = duk__p; \
  4509. } while (0)
  4510. #define DUK_BW_WRITE_RAW_U8_3(thr,bw_ctx,val1,val2,val3) do { \
  4511. duk_uint8_t *duk__p; \
  4512. DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 3); \
  4513. duk__p = (bw_ctx)->p; \
  4514. *duk__p++ = (duk_uint8_t) (val1); \
  4515. *duk__p++ = (duk_uint8_t) (val2); \
  4516. *duk__p++ = (duk_uint8_t) (val3); \
  4517. (bw_ctx)->p = duk__p; \
  4518. } while (0)
  4519. #define DUK_BW_WRITE_RAW_U8_4(thr,bw_ctx,val1,val2,val3,val4) do { \
  4520. duk_uint8_t *duk__p; \
  4521. DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 4); \
  4522. duk__p = (bw_ctx)->p; \
  4523. *duk__p++ = (duk_uint8_t) (val1); \
  4524. *duk__p++ = (duk_uint8_t) (val2); \
  4525. *duk__p++ = (duk_uint8_t) (val3); \
  4526. *duk__p++ = (duk_uint8_t) (val4); \
  4527. (bw_ctx)->p = duk__p; \
  4528. } while (0)
  4529. #define DUK_BW_WRITE_RAW_U8_5(thr,bw_ctx,val1,val2,val3,val4,val5) do { \
  4530. duk_uint8_t *duk__p; \
  4531. DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 5); \
  4532. duk__p = (bw_ctx)->p; \
  4533. *duk__p++ = (duk_uint8_t) (val1); \
  4534. *duk__p++ = (duk_uint8_t) (val2); \
  4535. *duk__p++ = (duk_uint8_t) (val3); \
  4536. *duk__p++ = (duk_uint8_t) (val4); \
  4537. *duk__p++ = (duk_uint8_t) (val5); \
  4538. (bw_ctx)->p = duk__p; \
  4539. } while (0)
  4540. #define DUK_BW_WRITE_RAW_U8_6(thr,bw_ctx,val1,val2,val3,val4,val5,val6) do { \
  4541. duk_uint8_t *duk__p; \
  4542. DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 6); \
  4543. duk__p = (bw_ctx)->p; \
  4544. *duk__p++ = (duk_uint8_t) (val1); \
  4545. *duk__p++ = (duk_uint8_t) (val2); \
  4546. *duk__p++ = (duk_uint8_t) (val3); \
  4547. *duk__p++ = (duk_uint8_t) (val4); \
  4548. *duk__p++ = (duk_uint8_t) (val5); \
  4549. *duk__p++ = (duk_uint8_t) (val6); \
  4550. (bw_ctx)->p = duk__p; \
  4551. } while (0)
  4552. #define DUK_BW_WRITE_RAW_XUTF8(thr,bw_ctx,cp) do { \
  4553. duk_ucodepoint_t duk__cp; \
  4554. duk_small_int_t duk__enc_len; \
  4555. duk__cp = (cp); \
  4556. DUK_BW_ASSERT_SPACE((thr), (bw_ctx), duk_unicode_get_xutf8_length(duk__cp)); \
  4557. duk__enc_len = duk_unicode_encode_xutf8(duk__cp, (bw_ctx)->p); \
  4558. (bw_ctx)->p += duk__enc_len; \
  4559. } while (0)
  4560. #define DUK_BW_WRITE_RAW_CESU8(thr,bw_ctx,cp) do { \
  4561. duk_ucodepoint_t duk__cp; \
  4562. duk_small_int_t duk__enc_len; \
  4563. duk__cp = (duk_ucodepoint_t) (cp); \
  4564. DUK_BW_ASSERT_SPACE((thr), (bw_ctx), duk_unicode_get_cesu8_length(duk__cp)); \
  4565. duk__enc_len = duk_unicode_encode_cesu8(duk__cp, (bw_ctx)->p); \
  4566. (bw_ctx)->p += duk__enc_len; \
  4567. } while (0)
  4568. /* XXX: add temporary duk__p pointer here too; sharing */
  4569. #define DUK_BW_WRITE_RAW_BYTES(thr,bw_ctx,valptr,valsz) do { \
  4570. const void *duk__valptr; \
  4571. duk_size_t duk__valsz; \
  4572. duk__valptr = (const void *) (valptr); \
  4573. duk__valsz = (duk_size_t) (valsz); \
  4574. DUK_MEMCPY((void *) ((bw_ctx)->p), duk__valptr, duk__valsz); \
  4575. (bw_ctx)->p += duk__valsz; \
  4576. } while (0)
  4577. #define DUK_BW_WRITE_RAW_CSTRING(thr,bw_ctx,val) do { \
  4578. const duk_uint8_t *duk__val; \
  4579. duk_size_t duk__val_len; \
  4580. duk__val = (const duk_uint8_t *) (val); \
  4581. duk__val_len = DUK_STRLEN((const char *) duk__val); \
  4582. DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) duk__val, duk__val_len); \
  4583. (bw_ctx)->p += duk__val_len; \
  4584. } while (0)
  4585. #define DUK_BW_WRITE_RAW_HSTRING(thr,bw_ctx,val) do { \
  4586. duk_size_t duk__val_len; \
  4587. duk__val_len = DUK_HSTRING_GET_BYTELEN((val)); \
  4588. DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HSTRING_GET_DATA((val)), duk__val_len); \
  4589. (bw_ctx)->p += duk__val_len; \
  4590. } while (0)
  4591. #define DUK_BW_WRITE_RAW_HBUFFER(thr,bw_ctx,val) do { \
  4592. duk_size_t duk__val_len; \
  4593. duk__val_len = DUK_HBUFFER_GET_SIZE((val)); \
  4594. DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \
  4595. (bw_ctx)->p += duk__val_len; \
  4596. } while (0)
  4597. #define DUK_BW_WRITE_RAW_HBUFFER_FIXED(thr,bw_ctx,val) do { \
  4598. duk_size_t duk__val_len; \
  4599. duk__val_len = DUK_HBUFFER_FIXED_GET_SIZE((val)); \
  4600. DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_FIXED_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \
  4601. (bw_ctx)->p += duk__val_len; \
  4602. } while (0)
  4603. #define DUK_BW_WRITE_RAW_HBUFFER_DYNAMIC(thr,bw_ctx,val) do { \
  4604. duk_size_t duk__val_len; \
  4605. duk__val_len = DUK_HBUFFER_DYNAMIC_GET_SIZE((val)); \
  4606. DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \
  4607. (bw_ctx)->p += duk__val_len; \
  4608. } while (0)
  4609. /* Append bytes from a slice already in the buffer. */
  4610. #define DUK_BW_WRITE_RAW_SLICE(thr,bw,dst_off,dst_len) \
  4611. duk_bw_write_raw_slice((thr), (bw), (dst_off), (dst_len))
  4612. /* Insert bytes in the middle of the buffer from an external buffer. */
  4613. #define DUK_BW_INSERT_RAW_BYTES(thr,bw,dst_off,buf,len) \
  4614. duk_bw_insert_raw_bytes((thr), (bw), (dst_off), (buf), (len))
  4615. /* Insert bytes in the middle of the buffer from a slice already
  4616. * in the buffer. Source offset is interpreted "before" the operation.
  4617. */
  4618. #define DUK_BW_INSERT_RAW_SLICE(thr,bw,dst_off,src_off,len) \
  4619. duk_bw_insert_raw_slice((thr), (bw), (dst_off), (src_off), (len))
  4620. /* Insert a reserved area somewhere in the buffer; caller fills it.
  4621. * Evaluates to a (duk_uint_t *) pointing to the start of the reserved
  4622. * area for convenience.
  4623. */
  4624. #define DUK_BW_INSERT_RAW_AREA(thr,bw,off,len) \
  4625. duk_bw_insert_raw_area((thr), (bw), (off), (len))
  4626. /* Remove a slice from inside buffer. */
  4627. #define DUK_BW_REMOVE_RAW_SLICE(thr,bw,off,len) \
  4628. duk_bw_remove_raw_slice((thr), (bw), (off), (len))
  4629. /* Safe write calls which will ensure space first. */
  4630. #define DUK_BW_WRITE_ENSURE_U8(thr,bw_ctx,val) do { \
  4631. DUK_BW_ENSURE((thr), (bw_ctx), 1); \
  4632. DUK_BW_WRITE_RAW_U8((thr), (bw_ctx), (val)); \
  4633. } while (0)
  4634. #define DUK_BW_WRITE_ENSURE_U8_2(thr,bw_ctx,val1,val2) do { \
  4635. DUK_BW_ENSURE((thr), (bw_ctx), 2); \
  4636. DUK_BW_WRITE_RAW_U8_2((thr), (bw_ctx), (val1), (val2)); \
  4637. } while (0)
  4638. #define DUK_BW_WRITE_ENSURE_U8_3(thr,bw_ctx,val1,val2,val3) do { \
  4639. DUK_BW_ENSURE((thr), (bw_ctx), 3); \
  4640. DUK_BW_WRITE_RAW_U8_3((thr), (bw_ctx), (val1), (val2), (val3)); \
  4641. } while (0)
  4642. #define DUK_BW_WRITE_ENSURE_U8_4(thr,bw_ctx,val1,val2,val3,val4) do { \
  4643. DUK_BW_ENSURE((thr), (bw_ctx), 4); \
  4644. DUK_BW_WRITE_RAW_U8_4((thr), (bw_ctx), (val1), (val2), (val3), (val4)); \
  4645. } while (0)
  4646. #define DUK_BW_WRITE_ENSURE_U8_5(thr,bw_ctx,val1,val2,val3,val4,val5) do { \
  4647. DUK_BW_ENSURE((thr), (bw_ctx), 5); \
  4648. DUK_BW_WRITE_RAW_U8_5((thr), (bw_ctx), (val1), (val2), (val3), (val4), (val5)); \
  4649. } while (0)
  4650. #define DUK_BW_WRITE_ENSURE_U8_6(thr,bw_ctx,val1,val2,val3,val4,val5,val6) do { \
  4651. DUK_BW_ENSURE((thr), (bw_ctx), 6); \
  4652. DUK_BW_WRITE_RAW_U8_6((thr), (bw_ctx), (val1), (val2), (val3), (val4), (val5), (val6)); \
  4653. } while (0)
  4654. #define DUK_BW_WRITE_ENSURE_XUTF8(thr,bw_ctx,cp) do { \
  4655. DUK_BW_ENSURE((thr), (bw_ctx), DUK_UNICODE_MAX_XUTF8_LENGTH); \
  4656. DUK_BW_WRITE_RAW_XUTF8((thr), (bw_ctx), (cp)); \
  4657. } while (0)
  4658. #define DUK_BW_WRITE_ENSURE_CESU8(thr,bw_ctx,cp) do { \
  4659. DUK_BW_ENSURE((thr), (bw_ctx), DUK_UNICODE_MAX_CESU8_LENGTH); \
  4660. DUK_BW_WRITE_RAW_CESU8((thr), (bw_ctx), (cp)); \
  4661. } while (0)
  4662. /* XXX: add temporary duk__p pointer here too; sharing */
  4663. #define DUK_BW_WRITE_ENSURE_BYTES(thr,bw_ctx,valptr,valsz) do { \
  4664. const void *duk__valptr; \
  4665. duk_size_t duk__valsz; \
  4666. duk__valptr = (const void *) (valptr); \
  4667. duk__valsz = (duk_size_t) (valsz); \
  4668. DUK_BW_ENSURE((thr), (bw_ctx), duk__valsz); \
  4669. DUK_MEMCPY((void *) ((bw_ctx)->p), duk__valptr, duk__valsz); \
  4670. (bw_ctx)->p += duk__valsz; \
  4671. } while (0)
  4672. #define DUK_BW_WRITE_ENSURE_CSTRING(thr,bw_ctx,val) do { \
  4673. const duk_uint8_t *duk__val; \
  4674. duk_size_t duk__val_len; \
  4675. duk__val = (const duk_uint8_t *) (val); \
  4676. duk__val_len = DUK_STRLEN((const char *) duk__val); \
  4677. DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \
  4678. DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) duk__val, duk__val_len); \
  4679. (bw_ctx)->p += duk__val_len; \
  4680. } while (0)
  4681. #define DUK_BW_WRITE_ENSURE_HSTRING(thr,bw_ctx,val) do { \
  4682. duk_size_t duk__val_len; \
  4683. duk__val_len = DUK_HSTRING_GET_BYTELEN((val)); \
  4684. DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \
  4685. DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HSTRING_GET_DATA((val)), duk__val_len); \
  4686. (bw_ctx)->p += duk__val_len; \
  4687. } while (0)
  4688. #define DUK_BW_WRITE_ENSURE_HBUFFER(thr,bw_ctx,val) do { \
  4689. duk_size_t duk__val_len; \
  4690. duk__val_len = DUK_HBUFFER_GET_SIZE((val)); \
  4691. DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \
  4692. DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \
  4693. (bw_ctx)->p += duk__val_len; \
  4694. } while (0)
  4695. #define DUK_BW_WRITE_ENSURE_HBUFFER_FIXED(thr,bw_ctx,val) do { \
  4696. duk_size_t duk__val_len; \
  4697. duk__val_len = DUK_HBUFFER_FIXED_GET_SIZE((val)); \
  4698. DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \
  4699. DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_FIXED_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \
  4700. (bw_ctx)->p += duk__val_len; \
  4701. } while (0)
  4702. #define DUK_BW_WRITE_ENSURE_HBUFFER_DYNAMIC(thr,bw_ctx,val) do { \
  4703. duk_size_t duk__val_len; \
  4704. duk__val_len = DUK_HBUFFER_DYNAMIC_GET_SIZE((val)); \
  4705. DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \
  4706. DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \
  4707. (bw_ctx)->p += duk__val_len; \
  4708. } while (0)
  4709. #define DUK_BW_WRITE_ENSURE_SLICE(thr,bw,dst_off,dst_len) \
  4710. duk_bw_write_ensure_slice((thr), (bw), (dst_off), (dst_len))
  4711. #define DUK_BW_INSERT_ENSURE_BYTES(thr,bw,dst_off,buf,len) \
  4712. duk_bw_insert_ensure_bytes((thr), (bw), (dst_off), (buf), (len))
  4713. #define DUK_BW_INSERT_ENSURE_SLICE(thr,bw,dst_off,src_off,len) \
  4714. duk_bw_insert_ensure_slice((thr), (bw), (dst_off), (src_off), (len))
  4715. #define DUK_BW_INSERT_ENSURE_AREA(thr,bw,off,len) \
  4716. /* Evaluates to (duk_uint8_t *) pointing to start of area. */ \
  4717. duk_bw_insert_ensure_area((thr), (bw), (off), (len))
  4718. #define DUK_BW_REMOVE_ENSURE_SLICE(thr,bw,off,len) \
  4719. /* No difference between raw/ensure because the buffer shrinks. */ \
  4720. DUK_BW_REMOVE_RAW_SLICE((thr), (bw), (off), (len))
  4721. /*
  4722. * Externs and prototypes
  4723. */
  4724. #if !defined(DUK_SINGLE_FILE)
  4725. DUK_INTERNAL_DECL duk_uint8_t duk_lc_digits[36];
  4726. DUK_INTERNAL_DECL duk_uint8_t duk_uc_nybbles[16];
  4727. DUK_INTERNAL_DECL duk_int8_t duk_hex_dectab[256];
  4728. #endif /* !DUK_SINGLE_FILE */
  4729. /* Note: assumes that duk_util_probe_steps size is 32 */
  4730. #if defined(DUK_USE_HOBJECT_HASH_PART) || defined(DUK_USE_STRTAB_PROBE)
  4731. #if !defined(DUK_SINGLE_FILE)
  4732. DUK_INTERNAL_DECL duk_uint8_t duk_util_probe_steps[32];
  4733. #endif /* !DUK_SINGLE_FILE */
  4734. #endif
  4735. DUK_INTERNAL_DECL duk_uint32_t duk_util_hashbytes(const duk_uint8_t *data, duk_size_t len, duk_uint32_t seed);
  4736. #if defined(DUK_USE_HOBJECT_HASH_PART) || defined(DUK_USE_STRTAB_PROBE)
  4737. DUK_INTERNAL_DECL duk_uint32_t duk_util_get_hash_prime(duk_uint32_t size);
  4738. #endif
  4739. DUK_INTERNAL_DECL duk_int32_t duk_bd_decode(duk_bitdecoder_ctx *ctx, duk_small_int_t bits);
  4740. DUK_INTERNAL_DECL duk_small_int_t duk_bd_decode_flag(duk_bitdecoder_ctx *ctx);
  4741. DUK_INTERNAL_DECL duk_int32_t duk_bd_decode_flagged(duk_bitdecoder_ctx *ctx, duk_small_int_t bits, duk_int32_t def_value);
  4742. DUK_INTERNAL_DECL void duk_be_encode(duk_bitencoder_ctx *ctx, duk_uint32_t data, duk_small_int_t bits);
  4743. DUK_INTERNAL_DECL void duk_be_finish(duk_bitencoder_ctx *ctx);
  4744. DUK_INTERNAL_DECL duk_uint32_t duk_util_tinyrandom_get_bits(duk_hthread *thr, duk_small_int_t n);
  4745. DUK_INTERNAL_DECL duk_double_t duk_util_tinyrandom_get_double(duk_hthread *thr);
  4746. DUK_INTERNAL_DECL void duk_bw_init(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_hbuffer_dynamic *h_buf);
  4747. DUK_INTERNAL_DECL void duk_bw_init_pushbuf(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_size_t buf_size);
  4748. DUK_INTERNAL_DECL duk_uint8_t *duk_bw_resize(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_size_t sz);
  4749. DUK_INTERNAL_DECL void duk_bw_compact(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx);
  4750. 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);
  4751. 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);
  4752. 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);
  4753. 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);
  4754. 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);
  4755. 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);
  4756. 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);
  4757. 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);
  4758. DUK_INTERNAL_DECL void duk_bw_remove_raw_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t off, duk_size_t len);
  4759. /* No duk_bw_remove_ensure_slice(), functionality would be identical. */
  4760. DUK_INTERNAL_DECL duk_uint16_t duk_raw_read_u16_be(duk_uint8_t **p);
  4761. DUK_INTERNAL_DECL duk_uint32_t duk_raw_read_u32_be(duk_uint8_t **p);
  4762. DUK_INTERNAL_DECL duk_double_t duk_raw_read_double_be(duk_uint8_t **p);
  4763. DUK_INTERNAL_DECL void duk_raw_write_u16_be(duk_uint8_t **p, duk_uint16_t val);
  4764. DUK_INTERNAL_DECL void duk_raw_write_u32_be(duk_uint8_t **p, duk_uint32_t val);
  4765. DUK_INTERNAL_DECL void duk_raw_write_double_be(duk_uint8_t **p, duk_double_t val);
  4766. #if defined(DUK_USE_DEBUGGER_SUPPORT) /* For now only needed by the debugger. */
  4767. DUK_INTERNAL void duk_byteswap_bytes(duk_uint8_t *p, duk_small_uint_t len);
  4768. #endif
  4769. #endif /* DUK_UTIL_H_INCLUDED */
  4770. #line 1 "duk_strings.h"
  4771. /*
  4772. * Shared error messages: declarations and macros
  4773. *
  4774. * Error messages are accessed through macros with fine-grained, explicit
  4775. * error message distinctions. Concrete error messages are selected by the
  4776. * macros and multiple macros can map to the same concrete string to save
  4777. * on code footprint. This allows flexible footprint/verbosity tuning with
  4778. * minimal code impact. There are a few limitations to this approach:
  4779. * (1) switching between plain messages and format strings doesn't work
  4780. * conveniently, and (2) conditional strings are a bit awkward to handle.
  4781. *
  4782. * Because format strings behave differently in the call site (they need to
  4783. * be followed by format arguments), they have a special prefix (DUK_STR_FMT_
  4784. * and duk_str_fmt_).
  4785. *
  4786. * On some compilers using explicit shared strings is preferable; on others
  4787. * it may be better to use straight literals because the compiler will combine
  4788. * them anyway, and such strings won't end up unnecessarily in a symbol table.
  4789. */
  4790. #ifndef DUK_ERRMSG_H_INCLUDED
  4791. #define DUK_ERRMSG_H_INCLUDED
  4792. #define DUK_STR_INTERNAL_ERROR duk_str_internal_error
  4793. #define DUK_STR_INVALID_COUNT duk_str_invalid_count
  4794. #define DUK_STR_INVALID_CALL_ARGS duk_str_invalid_call_args
  4795. #define DUK_STR_NOT_CONSTRUCTABLE duk_str_not_constructable
  4796. #define DUK_STR_NOT_CALLABLE duk_str_not_callable
  4797. #define DUK_STR_NOT_EXTENSIBLE duk_str_not_extensible
  4798. #define DUK_STR_NOT_WRITABLE duk_str_not_writable
  4799. #define DUK_STR_NOT_CONFIGURABLE duk_str_not_configurable
  4800. #if !defined(DUK_SINGLE_FILE)
  4801. DUK_INTERNAL_DECL const char *duk_str_internal_error;
  4802. DUK_INTERNAL_DECL const char *duk_str_invalid_count;
  4803. DUK_INTERNAL_DECL const char *duk_str_invalid_call_args;
  4804. DUK_INTERNAL_DECL const char *duk_str_not_constructable;
  4805. DUK_INTERNAL_DECL const char *duk_str_not_callable;
  4806. DUK_INTERNAL_DECL const char *duk_str_not_extensible;
  4807. DUK_INTERNAL_DECL const char *duk_str_not_writable;
  4808. DUK_INTERNAL_DECL const char *duk_str_not_configurable;
  4809. #endif /* !DUK_SINGLE_FILE */
  4810. #define DUK_STR_INVALID_CONTEXT duk_str_invalid_context
  4811. #define DUK_STR_INVALID_INDEX duk_str_invalid_index
  4812. #define DUK_STR_PUSH_BEYOND_ALLOC_STACK duk_str_push_beyond_alloc_stack
  4813. #define DUK_STR_NOT_UNDEFINED duk_str_not_undefined
  4814. #define DUK_STR_NOT_NULL duk_str_not_null
  4815. #define DUK_STR_NOT_BOOLEAN duk_str_not_boolean
  4816. #define DUK_STR_NOT_NUMBER duk_str_not_number
  4817. #define DUK_STR_NOT_STRING duk_str_not_string
  4818. #define DUK_STR_NOT_POINTER duk_str_not_pointer
  4819. #define DUK_STR_NOT_BUFFER duk_str_not_buffer
  4820. #define DUK_STR_UNEXPECTED_TYPE duk_str_unexpected_type
  4821. #define DUK_STR_NOT_THREAD duk_str_not_thread
  4822. #define DUK_STR_NOT_COMPILEDFUNCTION duk_str_not_compiledfunction
  4823. #define DUK_STR_NOT_NATIVEFUNCTION duk_str_not_nativefunction
  4824. #define DUK_STR_NOT_C_FUNCTION duk_str_not_c_function
  4825. #define DUK_STR_DEFAULTVALUE_COERCE_FAILED duk_str_defaultvalue_coerce_failed
  4826. #define DUK_STR_NUMBER_OUTSIDE_RANGE duk_str_number_outside_range
  4827. #define DUK_STR_NOT_OBJECT_COERCIBLE duk_str_not_object_coercible
  4828. #define DUK_STR_STRING_TOO_LONG duk_str_string_too_long
  4829. #define DUK_STR_BUFFER_TOO_LONG duk_str_buffer_too_long
  4830. #define DUK_STR_SPRINTF_TOO_LONG duk_str_sprintf_too_long
  4831. #define DUK_STR_ALLOC_FAILED duk_str_alloc_failed
  4832. #define DUK_STR_POP_TOO_MANY duk_str_pop_too_many
  4833. #define DUK_STR_WRONG_BUFFER_TYPE duk_str_wrong_buffer_type
  4834. #define DUK_STR_FAILED_TO_EXTEND_VALSTACK duk_str_failed_to_extend_valstack
  4835. #define DUK_STR_ENCODE_FAILED duk_str_encode_failed
  4836. #define DUK_STR_DECODE_FAILED duk_str_decode_failed
  4837. #define DUK_STR_NO_SOURCECODE duk_str_no_sourcecode
  4838. #define DUK_STR_CONCAT_RESULT_TOO_LONG duk_str_concat_result_too_long
  4839. #define DUK_STR_UNIMPLEMENTED duk_str_unimplemented
  4840. #define DUK_STR_UNSUPPORTED duk_str_unsupported
  4841. #define DUK_STR_ARRAY_LENGTH_OVER_2G duk_str_array_length_over_2g
  4842. #if !defined(DUK_SINGLE_FILE)
  4843. DUK_INTERNAL_DECL const char *duk_str_invalid_context;
  4844. DUK_INTERNAL_DECL const char *duk_str_invalid_index;
  4845. DUK_INTERNAL_DECL const char *duk_str_push_beyond_alloc_stack;
  4846. DUK_INTERNAL_DECL const char *duk_str_not_undefined;
  4847. DUK_INTERNAL_DECL const char *duk_str_not_null;
  4848. DUK_INTERNAL_DECL const char *duk_str_not_boolean;
  4849. DUK_INTERNAL_DECL const char *duk_str_not_number;
  4850. DUK_INTERNAL_DECL const char *duk_str_not_string;
  4851. DUK_INTERNAL_DECL const char *duk_str_not_pointer;
  4852. DUK_INTERNAL_DECL const char *duk_str_not_buffer;
  4853. DUK_INTERNAL_DECL const char *duk_str_unexpected_type;
  4854. DUK_INTERNAL_DECL const char *duk_str_not_thread;
  4855. DUK_INTERNAL_DECL const char *duk_str_not_compiledfunction;
  4856. DUK_INTERNAL_DECL const char *duk_str_not_nativefunction;
  4857. DUK_INTERNAL_DECL const char *duk_str_not_c_function;
  4858. DUK_INTERNAL_DECL const char *duk_str_defaultvalue_coerce_failed;
  4859. DUK_INTERNAL_DECL const char *duk_str_number_outside_range;
  4860. DUK_INTERNAL_DECL const char *duk_str_not_object_coercible;
  4861. DUK_INTERNAL_DECL const char *duk_str_string_too_long;
  4862. DUK_INTERNAL_DECL const char *duk_str_buffer_too_long;
  4863. DUK_INTERNAL_DECL const char *duk_str_sprintf_too_long;
  4864. DUK_INTERNAL_DECL const char *duk_str_alloc_failed;
  4865. DUK_INTERNAL_DECL const char *duk_str_pop_too_many;
  4866. DUK_INTERNAL_DECL const char *duk_str_wrong_buffer_type;
  4867. DUK_INTERNAL_DECL const char *duk_str_failed_to_extend_valstack;
  4868. DUK_INTERNAL_DECL const char *duk_str_encode_failed;
  4869. DUK_INTERNAL_DECL const char *duk_str_decode_failed;
  4870. DUK_INTERNAL_DECL const char *duk_str_no_sourcecode;
  4871. DUK_INTERNAL_DECL const char *duk_str_concat_result_too_long;
  4872. DUK_INTERNAL_DECL const char *duk_str_unimplemented;
  4873. DUK_INTERNAL_DECL const char *duk_str_unsupported;
  4874. DUK_INTERNAL_DECL const char *duk_str_array_length_over_2g;
  4875. #endif /* !DUK_SINGLE_FILE */
  4876. #define DUK_STR_FMT_PTR duk_str_fmt_ptr
  4877. #define DUK_STR_FMT_INVALID_JSON duk_str_fmt_invalid_json
  4878. #define DUK_STR_JSONDEC_RECLIMIT duk_str_jsondec_reclimit
  4879. #define DUK_STR_JSONENC_RECLIMIT duk_str_jsonenc_reclimit
  4880. #define DUK_STR_CYCLIC_INPUT duk_str_cyclic_input
  4881. #if !defined(DUK_SINGLE_FILE)
  4882. DUK_INTERNAL_DECL const char *duk_str_fmt_ptr;
  4883. DUK_INTERNAL_DECL const char *duk_str_fmt_invalid_json;
  4884. DUK_INTERNAL_DECL const char *duk_str_jsondec_reclimit;
  4885. DUK_INTERNAL_DECL const char *duk_str_jsonenc_reclimit;
  4886. DUK_INTERNAL_DECL const char *duk_str_cyclic_input;
  4887. #endif /* !DUK_SINGLE_FILE */
  4888. #define DUK_STR_PROXY_REVOKED duk_str_proxy_revoked
  4889. #define DUK_STR_OBJECT_RESIZE_FAILED duk_str_object_resize_failed
  4890. #define DUK_STR_INVALID_BASE duk_str_invalid_base
  4891. #define DUK_STR_STRICT_CALLER_READ duk_str_strict_caller_read
  4892. #define DUK_STR_PROXY_REJECTED duk_str_proxy_rejected
  4893. #define DUK_STR_INVALID_ARRAY_LENGTH duk_str_invalid_array_length
  4894. #define DUK_STR_ARRAY_LENGTH_WRITE_FAILED duk_str_array_length_write_failed
  4895. #define DUK_STR_ARRAY_LENGTH_NOT_WRITABLE duk_str_array_length_not_writable
  4896. #define DUK_STR_SETTER_UNDEFINED duk_str_setter_undefined
  4897. #define DUK_STR_REDEFINE_VIRT_PROP duk_str_redefine_virt_prop
  4898. #define DUK_STR_INVALID_DESCRIPTOR duk_str_invalid_descriptor
  4899. #define DUK_STR_PROPERTY_IS_VIRTUAL duk_str_property_is_virtual
  4900. #if !defined(DUK_SINGLE_FILE)
  4901. DUK_INTERNAL_DECL const char *duk_str_proxy_revoked;
  4902. DUK_INTERNAL_DECL const char *duk_str_object_resize_failed;
  4903. DUK_INTERNAL_DECL const char *duk_str_invalid_base;
  4904. DUK_INTERNAL_DECL const char *duk_str_strict_caller_read;
  4905. DUK_INTERNAL_DECL const char *duk_str_proxy_rejected;
  4906. DUK_INTERNAL_DECL const char *duk_str_invalid_array_length;
  4907. DUK_INTERNAL_DECL const char *duk_str_array_length_write_failed;
  4908. DUK_INTERNAL_DECL const char *duk_str_array_length_not_writable;
  4909. DUK_INTERNAL_DECL const char *duk_str_setter_undefined;
  4910. DUK_INTERNAL_DECL const char *duk_str_redefine_virt_prop;
  4911. DUK_INTERNAL_DECL const char *duk_str_invalid_descriptor;
  4912. DUK_INTERNAL_DECL const char *duk_str_property_is_virtual;
  4913. #endif /* !DUK_SINGLE_FILE */
  4914. #define DUK_STR_PARSE_ERROR duk_str_parse_error
  4915. #define DUK_STR_DUPLICATE_LABEL duk_str_duplicate_label
  4916. #define DUK_STR_INVALID_LABEL duk_str_invalid_label
  4917. #define DUK_STR_INVALID_ARRAY_LITERAL duk_str_invalid_array_literal
  4918. #define DUK_STR_INVALID_OBJECT_LITERAL duk_str_invalid_object_literal
  4919. #define DUK_STR_INVALID_VAR_DECLARATION duk_str_invalid_var_declaration
  4920. #define DUK_STR_CANNOT_DELETE_IDENTIFIER duk_str_cannot_delete_identifier
  4921. #define DUK_STR_INVALID_EXPRESSION duk_str_invalid_expression
  4922. #define DUK_STR_INVALID_LVALUE duk_str_invalid_lvalue
  4923. #define DUK_STR_EXPECTED_IDENTIFIER duk_str_expected_identifier
  4924. #define DUK_STR_EMPTY_EXPR_NOT_ALLOWED duk_str_empty_expr_not_allowed
  4925. #define DUK_STR_INVALID_FOR duk_str_invalid_for
  4926. #define DUK_STR_INVALID_SWITCH duk_str_invalid_switch
  4927. #define DUK_STR_INVALID_BREAK_CONT_LABEL duk_str_invalid_break_cont_label
  4928. #define DUK_STR_INVALID_RETURN duk_str_invalid_return
  4929. #define DUK_STR_INVALID_TRY duk_str_invalid_try
  4930. #define DUK_STR_INVALID_THROW duk_str_invalid_throw
  4931. #define DUK_STR_WITH_IN_STRICT_MODE duk_str_with_in_strict_mode
  4932. #define DUK_STR_FUNC_STMT_NOT_ALLOWED duk_str_func_stmt_not_allowed
  4933. #define DUK_STR_UNTERMINATED_STMT duk_str_unterminated_stmt
  4934. #define DUK_STR_INVALID_ARG_NAME duk_str_invalid_arg_name
  4935. #define DUK_STR_INVALID_FUNC_NAME duk_str_invalid_func_name
  4936. #define DUK_STR_INVALID_GETSET_NAME duk_str_invalid_getset_name
  4937. #define DUK_STR_FUNC_NAME_REQUIRED duk_str_func_name_required
  4938. #if !defined(DUK_SINGLE_FILE)
  4939. DUK_INTERNAL_DECL const char *duk_str_parse_error;
  4940. DUK_INTERNAL_DECL const char *duk_str_duplicate_label;
  4941. DUK_INTERNAL_DECL const char *duk_str_invalid_label;
  4942. DUK_INTERNAL_DECL const char *duk_str_invalid_array_literal;
  4943. DUK_INTERNAL_DECL const char *duk_str_invalid_object_literal;
  4944. DUK_INTERNAL_DECL const char *duk_str_invalid_var_declaration;
  4945. DUK_INTERNAL_DECL const char *duk_str_cannot_delete_identifier;
  4946. DUK_INTERNAL_DECL const char *duk_str_invalid_expression;
  4947. DUK_INTERNAL_DECL const char *duk_str_invalid_lvalue;
  4948. DUK_INTERNAL_DECL const char *duk_str_expected_identifier;
  4949. DUK_INTERNAL_DECL const char *duk_str_empty_expr_not_allowed;
  4950. DUK_INTERNAL_DECL const char *duk_str_invalid_for;
  4951. DUK_INTERNAL_DECL const char *duk_str_invalid_switch;
  4952. DUK_INTERNAL_DECL const char *duk_str_invalid_break_cont_label;
  4953. DUK_INTERNAL_DECL const char *duk_str_invalid_return;
  4954. DUK_INTERNAL_DECL const char *duk_str_invalid_try;
  4955. DUK_INTERNAL_DECL const char *duk_str_invalid_throw;
  4956. DUK_INTERNAL_DECL const char *duk_str_with_in_strict_mode;
  4957. DUK_INTERNAL_DECL const char *duk_str_func_stmt_not_allowed;
  4958. DUK_INTERNAL_DECL const char *duk_str_unterminated_stmt;
  4959. DUK_INTERNAL_DECL const char *duk_str_invalid_arg_name;
  4960. DUK_INTERNAL_DECL const char *duk_str_invalid_func_name;
  4961. DUK_INTERNAL_DECL const char *duk_str_invalid_getset_name;
  4962. DUK_INTERNAL_DECL const char *duk_str_func_name_required;
  4963. #endif /* !DUK_SINGLE_FILE */
  4964. #define DUK_STR_INTERNAL_ERROR_EXEC_LONGJMP duk_str_internal_error_exec_longjmp
  4965. #if !defined(DUK_SINGLE_FILE)
  4966. DUK_INTERNAL_DECL const char *duk_str_internal_error_exec_longjmp;
  4967. #endif /* !DUK_SINGLE_FILE */
  4968. #define DUK_STR_INVALID_QUANTIFIER_NO_ATOM duk_str_invalid_quantifier_no_atom
  4969. #define DUK_STR_INVALID_QUANTIFIER_VALUES duk_str_invalid_quantifier_values
  4970. #define DUK_STR_QUANTIFIER_TOO_MANY_COPIES duk_str_quantifier_too_many_copies
  4971. #define DUK_STR_UNEXPECTED_CLOSING_PAREN duk_str_unexpected_closing_paren
  4972. #define DUK_STR_UNEXPECTED_END_OF_PATTERN duk_str_unexpected_end_of_pattern
  4973. #define DUK_STR_UNEXPECTED_REGEXP_TOKEN duk_str_unexpected_regexp_token
  4974. #define DUK_STR_INVALID_REGEXP_FLAGS duk_str_invalid_regexp_flags
  4975. #define DUK_STR_INVALID_BACKREFS duk_str_invalid_backrefs
  4976. #define DUK_STR_REGEXP_BACKTRACK_FAILED duk_str_regexp_backtrack_failed
  4977. #define DUK_STR_REGEXP_ADVANCE_FAILED duk_str_regexp_advance_failed
  4978. #define DUK_STR_REGEXP_INTERNAL_ERROR duk_str_regexp_internal_error
  4979. #if !defined(DUK_SINGLE_FILE)
  4980. DUK_INTERNAL_DECL const char *duk_str_invalid_quantifier_no_atom;
  4981. DUK_INTERNAL_DECL const char *duk_str_invalid_quantifier_values;
  4982. DUK_INTERNAL_DECL const char *duk_str_quantifier_too_many_copies;
  4983. DUK_INTERNAL_DECL const char *duk_str_unexpected_closing_paren;
  4984. DUK_INTERNAL_DECL const char *duk_str_unexpected_end_of_pattern;
  4985. DUK_INTERNAL_DECL const char *duk_str_unexpected_regexp_token;
  4986. DUK_INTERNAL_DECL const char *duk_str_invalid_regexp_flags;
  4987. DUK_INTERNAL_DECL const char *duk_str_invalid_backrefs;
  4988. DUK_INTERNAL_DECL const char *duk_str_regexp_backtrack_failed;
  4989. DUK_INTERNAL_DECL const char *duk_str_regexp_advance_failed;
  4990. DUK_INTERNAL_DECL const char *duk_str_regexp_internal_error;
  4991. #endif /* !DUK_SINGLE_FILE */
  4992. #define DUK_STR_VALSTACK_LIMIT duk_str_valstack_limit
  4993. #define DUK_STR_CALLSTACK_LIMIT duk_str_callstack_limit
  4994. #define DUK_STR_CATCHSTACK_LIMIT duk_str_catchstack_limit
  4995. #define DUK_STR_OBJECT_PROPERTY_LIMIT duk_str_object_property_limit
  4996. #define DUK_STR_PROTOTYPE_CHAIN_LIMIT duk_str_prototype_chain_limit
  4997. #define DUK_STR_BOUND_CHAIN_LIMIT duk_str_bound_chain_limit
  4998. #define DUK_STR_C_CALLSTACK_LIMIT duk_str_c_callstack_limit
  4999. #define DUK_STR_COMPILER_RECURSION_LIMIT duk_str_compiler_recursion_limit
  5000. #define DUK_STR_BYTECODE_LIMIT duk_str_bytecode_limit
  5001. #define DUK_STR_REG_LIMIT duk_str_reg_limit
  5002. #define DUK_STR_TEMP_LIMIT duk_str_temp_limit
  5003. #define DUK_STR_CONST_LIMIT duk_str_const_limit
  5004. #define DUK_STR_FUNC_LIMIT duk_str_func_limit
  5005. #define DUK_STR_REGEXP_COMPILER_RECURSION_LIMIT duk_str_regexp_compiler_recursion_limit
  5006. #define DUK_STR_REGEXP_EXECUTOR_RECURSION_LIMIT duk_str_regexp_executor_recursion_limit
  5007. #define DUK_STR_REGEXP_EXECUTOR_STEP_LIMIT duk_str_regexp_executor_step_limit
  5008. #if !defined(DUK_SINGLE_FILE)
  5009. DUK_INTERNAL_DECL const char *duk_str_valstack_limit;
  5010. DUK_INTERNAL_DECL const char *duk_str_callstack_limit;
  5011. DUK_INTERNAL_DECL const char *duk_str_catchstack_limit;
  5012. DUK_INTERNAL_DECL const char *duk_str_object_property_limit;
  5013. DUK_INTERNAL_DECL const char *duk_str_prototype_chain_limit;
  5014. DUK_INTERNAL_DECL const char *duk_str_bound_chain_limit;
  5015. DUK_INTERNAL_DECL const char *duk_str_c_callstack_limit;
  5016. DUK_INTERNAL_DECL const char *duk_str_compiler_recursion_limit;
  5017. DUK_INTERNAL_DECL const char *duk_str_bytecode_limit;
  5018. DUK_INTERNAL_DECL const char *duk_str_reg_limit;
  5019. DUK_INTERNAL_DECL const char *duk_str_temp_limit;
  5020. DUK_INTERNAL_DECL const char *duk_str_const_limit;
  5021. DUK_INTERNAL_DECL const char *duk_str_func_limit;
  5022. DUK_INTERNAL_DECL const char *duk_str_regexp_compiler_recursion_limit;
  5023. DUK_INTERNAL_DECL const char *duk_str_regexp_executor_recursion_limit;
  5024. DUK_INTERNAL_DECL const char *duk_str_regexp_executor_step_limit;
  5025. #endif /* !DUK_SINGLE_FILE */
  5026. #define DUK_STR_ANON duk_str_anon
  5027. #define DUK_STR_REALLOC_FAILED duk_str_realloc_failed
  5028. #if !defined(DUK_SINGLE_FILE)
  5029. DUK_INTERNAL_DECL const char *duk_str_anon;
  5030. DUK_INTERNAL_DECL const char *duk_str_realloc_failed;
  5031. #endif /* !DUK_SINGLE_FILE */
  5032. #endif /* DUK_ERRMSG_H_INCLUDED */
  5033. #line 1 "duk_js_bytecode.h"
  5034. /*
  5035. * Ecmascript bytecode
  5036. */
  5037. #ifndef DUK_JS_BYTECODE_H_INCLUDED
  5038. #define DUK_JS_BYTECODE_H_INCLUDED
  5039. /*
  5040. * Logical instruction layout
  5041. * ==========================
  5042. *
  5043. * !3!3!2!2!2!2!2!2!2!2!2!2!1!1!1!1!1!1!1!1!1!1! ! ! ! ! ! ! ! ! ! !
  5044. * !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!
  5045. * +---------------------------------------------------+-----------+
  5046. * ! C ! B ! A ! OP !
  5047. * +---------------------------------------------------+-----------+
  5048. *
  5049. * OP (6 bits): opcode (DUK_OP_*), access should be fastest
  5050. * A (8 bits): typically a target register number
  5051. * B (9 bits): typically first source register/constant number
  5052. * C (9 bits): typically second source register/constant number
  5053. *
  5054. * Some instructions combine BC or ABC together for larger parameter values.
  5055. * Signed integers (e.g. jump offsets) are encoded as unsigned, with an opcode
  5056. * specific bias. B and C may denote a register or a constant, see
  5057. * DUK_BC_ISREG() and DUK_BC_ISCONST().
  5058. *
  5059. * Note: macro naming is a bit misleading, e.g. "ABC" in macro name but
  5060. * the field layout is logically "CBA".
  5061. */
  5062. typedef duk_uint32_t duk_instr_t;
  5063. #define DUK_DEC_OP(x) ((x) & 0x3fUL)
  5064. #define DUK_DEC_A(x) (((x) >> 6) & 0xffUL)
  5065. #define DUK_DEC_B(x) (((x) >> 14) & 0x1ffUL)
  5066. #define DUK_DEC_C(x) (((x) >> 23) & 0x1ffUL)
  5067. #define DUK_DEC_BC(x) (((x) >> 14) & 0x3ffffUL)
  5068. #define DUK_DEC_ABC(x) (((x) >> 6) & 0x3ffffffUL)
  5069. #define DUK_ENC_OP(op) ((duk_instr_t) (op))
  5070. #define DUK_ENC_OP_ABC(op,abc) ((duk_instr_t) ( \
  5071. (((duk_instr_t) (abc)) << 6) | \
  5072. ((duk_instr_t) (op)) \
  5073. ))
  5074. #define DUK_ENC_OP_A_BC(op,a,bc) ((duk_instr_t) ( \
  5075. (((duk_instr_t) (bc)) << 14) | \
  5076. (((duk_instr_t) (a)) << 6) | \
  5077. ((duk_instr_t) (op)) \
  5078. ))
  5079. #define DUK_ENC_OP_A_B_C(op,a,b,c) ((duk_instr_t) ( \
  5080. (((duk_instr_t) (c)) << 23) | \
  5081. (((duk_instr_t) (b)) << 14) | \
  5082. (((duk_instr_t) (a)) << 6) | \
  5083. ((duk_instr_t) (op)) \
  5084. ))
  5085. #define DUK_ENC_OP_A_B(op,a,b) DUK_ENC_OP_A_B_C(op,a,b,0)
  5086. #define DUK_ENC_OP_A(op,a) DUK_ENC_OP_A_B_C(op,a,0,0)
  5087. /* Constants should be signed so that signed arithmetic involving them
  5088. * won't cause values to be coerced accidentally to unsigned.
  5089. */
  5090. #define DUK_BC_OP_MIN 0
  5091. #define DUK_BC_OP_MAX 0x3fL
  5092. #define DUK_BC_A_MIN 0
  5093. #define DUK_BC_A_MAX 0xffL
  5094. #define DUK_BC_B_MIN 0
  5095. #define DUK_BC_B_MAX 0x1ffL
  5096. #define DUK_BC_C_MIN 0
  5097. #define DUK_BC_C_MAX 0x1ffL
  5098. #define DUK_BC_BC_MIN 0
  5099. #define DUK_BC_BC_MAX 0x3ffffL
  5100. #define DUK_BC_ABC_MIN 0
  5101. #define DUK_BC_ABC_MAX 0x3ffffffL
  5102. #define DUK_BC_EXTRAOP_MIN DUK_BC_A_MIN
  5103. #define DUK_BC_EXTRAOP_MAX DUK_BC_A_MAX
  5104. #define DUK_OP_LDREG 0
  5105. #define DUK_OP_STREG 1
  5106. #define DUK_OP_LDCONST 2
  5107. #define DUK_OP_LDINT 3
  5108. #define DUK_OP_LDINTX 4
  5109. #define DUK_OP_MPUTOBJ 5
  5110. #define DUK_OP_MPUTOBJI 6
  5111. #define DUK_OP_MPUTARR 7
  5112. #define DUK_OP_MPUTARRI 8
  5113. #define DUK_OP_NEW 9
  5114. #define DUK_OP_NEWI 10
  5115. #define DUK_OP_REGEXP 11
  5116. #define DUK_OP_CSREG 12
  5117. #define DUK_OP_CSREGI 13
  5118. #define DUK_OP_GETVAR 14
  5119. #define DUK_OP_PUTVAR 15
  5120. #define DUK_OP_DECLVAR 16
  5121. #define DUK_OP_DELVAR 17
  5122. #define DUK_OP_CSVAR 18
  5123. #define DUK_OP_CSVARI 19
  5124. #define DUK_OP_CLOSURE 20
  5125. #define DUK_OP_GETPROP 21
  5126. #define DUK_OP_PUTPROP 22
  5127. #define DUK_OP_DELPROP 23
  5128. #define DUK_OP_CSPROP 24
  5129. #define DUK_OP_CSPROPI 25
  5130. #define DUK_OP_ADD 26
  5131. #define DUK_OP_SUB 27
  5132. #define DUK_OP_MUL 28
  5133. #define DUK_OP_DIV 29
  5134. #define DUK_OP_MOD 30
  5135. #define DUK_OP_BAND 31
  5136. #define DUK_OP_BOR 32
  5137. #define DUK_OP_BXOR 33
  5138. #define DUK_OP_BASL 34
  5139. #define DUK_OP_BLSR 35
  5140. #define DUK_OP_BASR 36
  5141. #define DUK_OP_EQ 37
  5142. #define DUK_OP_NEQ 38
  5143. #define DUK_OP_SEQ 39
  5144. #define DUK_OP_SNEQ 40
  5145. #define DUK_OP_GT 41
  5146. #define DUK_OP_GE 42
  5147. #define DUK_OP_LT 43
  5148. #define DUK_OP_LE 44
  5149. #define DUK_OP_IF 45
  5150. #define DUK_OP_JUMP 46
  5151. #define DUK_OP_RETURN 47
  5152. #define DUK_OP_CALL 48
  5153. #define DUK_OP_CALLI 49
  5154. #define DUK_OP_TRYCATCH 50
  5155. #define DUK_OP_EXTRA 51
  5156. #define DUK_OP_PREINCR 52 /* pre/post opcode values have constraints, */
  5157. #define DUK_OP_PREDECR 53 /* see duk_js_executor.c */
  5158. #define DUK_OP_POSTINCR 54
  5159. #define DUK_OP_POSTDECR 55
  5160. #define DUK_OP_PREINCV 56
  5161. #define DUK_OP_PREDECV 57
  5162. #define DUK_OP_POSTINCV 58
  5163. #define DUK_OP_POSTDECV 59
  5164. #define DUK_OP_PREINCP 60
  5165. #define DUK_OP_PREDECP 61
  5166. #define DUK_OP_POSTINCP 62
  5167. #define DUK_OP_POSTDECP 63
  5168. #define DUK_OP_NONE 64 /* dummy value used as marker */
  5169. /* DUK_OP_EXTRA, sub-operation in A */
  5170. #define DUK_EXTRAOP_NOP 0
  5171. #define DUK_EXTRAOP_INVALID 1
  5172. #define DUK_EXTRAOP_LDTHIS 2
  5173. #define DUK_EXTRAOP_LDUNDEF 3
  5174. #define DUK_EXTRAOP_LDNULL 4
  5175. #define DUK_EXTRAOP_LDTRUE 5
  5176. #define DUK_EXTRAOP_LDFALSE 6
  5177. #define DUK_EXTRAOP_NEWOBJ 7
  5178. #define DUK_EXTRAOP_NEWARR 8
  5179. #define DUK_EXTRAOP_SETALEN 9
  5180. #define DUK_EXTRAOP_TYPEOF 10
  5181. #define DUK_EXTRAOP_TYPEOFID 11
  5182. #define DUK_EXTRAOP_INITENUM 12
  5183. #define DUK_EXTRAOP_NEXTENUM 13
  5184. #define DUK_EXTRAOP_INITSET 14
  5185. #define DUK_EXTRAOP_INITSETI 15
  5186. #define DUK_EXTRAOP_INITGET 16
  5187. #define DUK_EXTRAOP_INITGETI 17
  5188. #define DUK_EXTRAOP_ENDTRY 18
  5189. #define DUK_EXTRAOP_ENDCATCH 19
  5190. #define DUK_EXTRAOP_ENDFIN 20
  5191. #define DUK_EXTRAOP_THROW 21
  5192. #define DUK_EXTRAOP_INVLHS 22
  5193. #define DUK_EXTRAOP_UNM 23
  5194. #define DUK_EXTRAOP_UNP 24
  5195. #define DUK_EXTRAOP_DEBUGGER 25
  5196. #define DUK_EXTRAOP_BREAK 26
  5197. #define DUK_EXTRAOP_CONTINUE 27
  5198. #define DUK_EXTRAOP_BNOT 28
  5199. #define DUK_EXTRAOP_LNOT 29
  5200. #define DUK_EXTRAOP_INSTOF 30
  5201. #define DUK_EXTRAOP_IN 31
  5202. #define DUK_EXTRAOP_LABEL 32
  5203. #define DUK_EXTRAOP_ENDLABEL 33
  5204. /* DUK_OP_CALL flags in A */
  5205. #define DUK_BC_CALL_FLAG_TAILCALL (1 << 0)
  5206. #define DUK_BC_CALL_FLAG_EVALCALL (1 << 1)
  5207. /* DUK_OP_TRYCATCH flags in A */
  5208. #define DUK_BC_TRYCATCH_FLAG_HAVE_CATCH (1 << 0)
  5209. #define DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY (1 << 1)
  5210. #define DUK_BC_TRYCATCH_FLAG_CATCH_BINDING (1 << 2)
  5211. #define DUK_BC_TRYCATCH_FLAG_WITH_BINDING (1 << 3)
  5212. /* DUK_OP_RETURN flags in A */
  5213. #define DUK_BC_RETURN_FLAG_HAVE_RETVAL (1 << 0)
  5214. /* DUK_OP_DECLVAR flags in A; bottom bits are reserved for propdesc flags (DUK_PROPDESC_FLAG_XXX) */
  5215. #define DUK_BC_DECLVAR_FLAG_UNDEF_VALUE (1 << 4) /* use 'undefined' for value automatically */
  5216. #define DUK_BC_DECLVAR_FLAG_FUNC_DECL (1 << 5) /* function declaration */
  5217. /* misc constants and helper macros */
  5218. #define DUK_BC_REGLIMIT 256 /* if B/C is >= this value, refers to a const */
  5219. #define DUK_BC_ISREG(x) ((x) < DUK_BC_REGLIMIT)
  5220. #define DUK_BC_ISCONST(x) ((x) >= DUK_BC_REGLIMIT)
  5221. #define DUK_BC_LDINT_BIAS (1L << 17)
  5222. #define DUK_BC_LDINTX_SHIFT 18
  5223. #define DUK_BC_JUMP_BIAS (1L << 25)
  5224. #endif /* DUK_JS_BYTECODE_H_INCLUDED */
  5225. #line 1 "duk_lexer.h"
  5226. /*
  5227. * Lexer defines.
  5228. */
  5229. #ifndef DUK_LEXER_H_INCLUDED
  5230. #define DUK_LEXER_H_INCLUDED
  5231. typedef void (*duk_re_range_callback)(void *user, duk_codepoint_t r1, duk_codepoint_t r2, duk_bool_t direct);
  5232. /*
  5233. * A token is interpreted as any possible production of InputElementDiv
  5234. * and InputElementRegExp, see E5 Section 7 in its entirety. Note that
  5235. * the E5 "Token" production does not cover all actual tokens of the
  5236. * language (which is explicitly stated in the specification, Section 7.5).
  5237. * Null and boolean literals are defined as part of both ReservedWord
  5238. * (E5 Section 7.6.1) and Literal (E5 Section 7.8) productions. Here,
  5239. * null and boolean values have literal tokens, and are not reserved
  5240. * words.
  5241. *
  5242. * Decimal literal negative/positive sign is -not- part of DUK_TOK_NUMBER.
  5243. * The number tokens always have a non-negative value. The unary minus
  5244. * operator in "-1.0" is optimized during compilation to yield a single
  5245. * negative constant.
  5246. *
  5247. * Token numbering is free except that reserved words are required to be
  5248. * in a continuous range and in a particular order. See genstrings.py.
  5249. */
  5250. #define DUK_LEXER_INITCTX(ctx) duk_lexer_initctx((ctx))
  5251. #define DUK_LEXER_SETPOINT(ctx,pt) duk_lexer_setpoint((ctx), (pt))
  5252. #define DUK_LEXER_GETPOINT(ctx,pt) do { (pt)->offset = (ctx)->window[0].offset; \
  5253. (pt)->line = (ctx)->window[0].line; } while (0)
  5254. /* currently 6 characters of lookup are actually needed (duk_lexer.c) */
  5255. #define DUK_LEXER_WINDOW_SIZE 6
  5256. #if defined(DUK_USE_LEXER_SLIDING_WINDOW)
  5257. #define DUK_LEXER_BUFFER_SIZE 64
  5258. #endif
  5259. #define DUK_TOK_MINVAL 0
  5260. /* returned after EOF (infinite amount) */
  5261. #define DUK_TOK_EOF 0
  5262. /* identifier names (E5 Section 7.6) */
  5263. #define DUK_TOK_IDENTIFIER 1
  5264. /* reserved words: keywords */
  5265. #define DUK_TOK_START_RESERVED 2
  5266. #define DUK_TOK_BREAK 2
  5267. #define DUK_TOK_CASE 3
  5268. #define DUK_TOK_CATCH 4
  5269. #define DUK_TOK_CONTINUE 5
  5270. #define DUK_TOK_DEBUGGER 6
  5271. #define DUK_TOK_DEFAULT 7
  5272. #define DUK_TOK_DELETE 8
  5273. #define DUK_TOK_DO 9
  5274. #define DUK_TOK_ELSE 10
  5275. #define DUK_TOK_FINALLY 11
  5276. #define DUK_TOK_FOR 12
  5277. #define DUK_TOK_FUNCTION 13
  5278. #define DUK_TOK_IF 14
  5279. #define DUK_TOK_IN 15
  5280. #define DUK_TOK_INSTANCEOF 16
  5281. #define DUK_TOK_NEW 17
  5282. #define DUK_TOK_RETURN 18
  5283. #define DUK_TOK_SWITCH 19
  5284. #define DUK_TOK_THIS 20
  5285. #define DUK_TOK_THROW 21
  5286. #define DUK_TOK_TRY 22
  5287. #define DUK_TOK_TYPEOF 23
  5288. #define DUK_TOK_VAR 24
  5289. #define DUK_TOK_CONST 25
  5290. #define DUK_TOK_VOID 26
  5291. #define DUK_TOK_WHILE 27
  5292. #define DUK_TOK_WITH 28
  5293. /* reserved words: future reserved words */
  5294. #define DUK_TOK_CLASS 29
  5295. #define DUK_TOK_ENUM 30
  5296. #define DUK_TOK_EXPORT 31
  5297. #define DUK_TOK_EXTENDS 32
  5298. #define DUK_TOK_IMPORT 33
  5299. #define DUK_TOK_SUPER 34
  5300. /* "null", "true", and "false" are always reserved words.
  5301. * Note that "get" and "set" are not!
  5302. */
  5303. #define DUK_TOK_NULL 35
  5304. #define DUK_TOK_TRUE 36
  5305. #define DUK_TOK_FALSE 37
  5306. /* reserved words: additional future reserved words in strict mode */
  5307. #define DUK_TOK_START_STRICT_RESERVED 38 /* inclusive */
  5308. #define DUK_TOK_IMPLEMENTS 38
  5309. #define DUK_TOK_INTERFACE 39
  5310. #define DUK_TOK_LET 40
  5311. #define DUK_TOK_PACKAGE 41
  5312. #define DUK_TOK_PRIVATE 42
  5313. #define DUK_TOK_PROTECTED 43
  5314. #define DUK_TOK_PUBLIC 44
  5315. #define DUK_TOK_STATIC 45
  5316. #define DUK_TOK_YIELD 46
  5317. #define DUK_TOK_END_RESERVED 47 /* exclusive */
  5318. /* "get" and "set" are tokens but NOT ReservedWords. They are currently
  5319. * parsed and identifiers and these defines are actually now unused.
  5320. */
  5321. #define DUK_TOK_GET 47
  5322. #define DUK_TOK_SET 48
  5323. /* punctuators (unlike the spec, also includes "/" and "/=") */
  5324. #define DUK_TOK_LCURLY 49
  5325. #define DUK_TOK_RCURLY 50
  5326. #define DUK_TOK_LBRACKET 51
  5327. #define DUK_TOK_RBRACKET 52
  5328. #define DUK_TOK_LPAREN 53
  5329. #define DUK_TOK_RPAREN 54
  5330. #define DUK_TOK_PERIOD 55
  5331. #define DUK_TOK_SEMICOLON 56
  5332. #define DUK_TOK_COMMA 57
  5333. #define DUK_TOK_LT 58
  5334. #define DUK_TOK_GT 59
  5335. #define DUK_TOK_LE 60
  5336. #define DUK_TOK_GE 61
  5337. #define DUK_TOK_EQ 62
  5338. #define DUK_TOK_NEQ 63
  5339. #define DUK_TOK_SEQ 64
  5340. #define DUK_TOK_SNEQ 65
  5341. #define DUK_TOK_ADD 66
  5342. #define DUK_TOK_SUB 67
  5343. #define DUK_TOK_MUL 68
  5344. #define DUK_TOK_DIV 69
  5345. #define DUK_TOK_MOD 70
  5346. #define DUK_TOK_INCREMENT 71
  5347. #define DUK_TOK_DECREMENT 72
  5348. #define DUK_TOK_ALSHIFT 73 /* named "arithmetic" because result is signed */
  5349. #define DUK_TOK_ARSHIFT 74
  5350. #define DUK_TOK_RSHIFT 75
  5351. #define DUK_TOK_BAND 76
  5352. #define DUK_TOK_BOR 77
  5353. #define DUK_TOK_BXOR 78
  5354. #define DUK_TOK_LNOT 79
  5355. #define DUK_TOK_BNOT 80
  5356. #define DUK_TOK_LAND 81
  5357. #define DUK_TOK_LOR 82
  5358. #define DUK_TOK_QUESTION 83
  5359. #define DUK_TOK_COLON 84
  5360. #define DUK_TOK_EQUALSIGN 85
  5361. #define DUK_TOK_ADD_EQ 86
  5362. #define DUK_TOK_SUB_EQ 87
  5363. #define DUK_TOK_MUL_EQ 88
  5364. #define DUK_TOK_DIV_EQ 89
  5365. #define DUK_TOK_MOD_EQ 90
  5366. #define DUK_TOK_ALSHIFT_EQ 91
  5367. #define DUK_TOK_ARSHIFT_EQ 92
  5368. #define DUK_TOK_RSHIFT_EQ 93
  5369. #define DUK_TOK_BAND_EQ 94
  5370. #define DUK_TOK_BOR_EQ 95
  5371. #define DUK_TOK_BXOR_EQ 96
  5372. /* literals (E5 Section 7.8), except null, true, false, which are treated
  5373. * like reserved words (above).
  5374. */
  5375. #define DUK_TOK_NUMBER 97
  5376. #define DUK_TOK_STRING 98
  5377. #define DUK_TOK_REGEXP 99
  5378. #define DUK_TOK_MAXVAL 99 /* inclusive */
  5379. /* Convert heap string index to a token (reserved words) */
  5380. #define DUK_STRIDX_TO_TOK(x) ((x) - DUK_STRIDX_START_RESERVED + DUK_TOK_START_RESERVED)
  5381. /* Sanity check */
  5382. #if (DUK_TOK_MAXVAL > 255)
  5383. #error DUK_TOK_MAXVAL too large, code assumes it fits into 8 bits
  5384. #endif
  5385. /* Sanity checks for string and token defines */
  5386. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_BREAK) != DUK_TOK_BREAK)
  5387. #error mismatch in token defines
  5388. #endif
  5389. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CASE) != DUK_TOK_CASE)
  5390. #error mismatch in token defines
  5391. #endif
  5392. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CATCH) != DUK_TOK_CATCH)
  5393. #error mismatch in token defines
  5394. #endif
  5395. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CONTINUE) != DUK_TOK_CONTINUE)
  5396. #error mismatch in token defines
  5397. #endif
  5398. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DEBUGGER) != DUK_TOK_DEBUGGER)
  5399. #error mismatch in token defines
  5400. #endif
  5401. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DEFAULT) != DUK_TOK_DEFAULT)
  5402. #error mismatch in token defines
  5403. #endif
  5404. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DELETE) != DUK_TOK_DELETE)
  5405. #error mismatch in token defines
  5406. #endif
  5407. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DO) != DUK_TOK_DO)
  5408. #error mismatch in token defines
  5409. #endif
  5410. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_ELSE) != DUK_TOK_ELSE)
  5411. #error mismatch in token defines
  5412. #endif
  5413. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_FINALLY) != DUK_TOK_FINALLY)
  5414. #error mismatch in token defines
  5415. #endif
  5416. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_FOR) != DUK_TOK_FOR)
  5417. #error mismatch in token defines
  5418. #endif
  5419. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_LC_FUNCTION) != DUK_TOK_FUNCTION)
  5420. #error mismatch in token defines
  5421. #endif
  5422. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IF) != DUK_TOK_IF)
  5423. #error mismatch in token defines
  5424. #endif
  5425. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IN) != DUK_TOK_IN)
  5426. #error mismatch in token defines
  5427. #endif
  5428. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_INSTANCEOF) != DUK_TOK_INSTANCEOF)
  5429. #error mismatch in token defines
  5430. #endif
  5431. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_NEW) != DUK_TOK_NEW)
  5432. #error mismatch in token defines
  5433. #endif
  5434. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_RETURN) != DUK_TOK_RETURN)
  5435. #error mismatch in token defines
  5436. #endif
  5437. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_SWITCH) != DUK_TOK_SWITCH)
  5438. #error mismatch in token defines
  5439. #endif
  5440. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_THIS) != DUK_TOK_THIS)
  5441. #error mismatch in token defines
  5442. #endif
  5443. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_THROW) != DUK_TOK_THROW)
  5444. #error mismatch in token defines
  5445. #endif
  5446. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_TRY) != DUK_TOK_TRY)
  5447. #error mismatch in token defines
  5448. #endif
  5449. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_TYPEOF) != DUK_TOK_TYPEOF)
  5450. #error mismatch in token defines
  5451. #endif
  5452. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_VAR) != DUK_TOK_VAR)
  5453. #error mismatch in token defines
  5454. #endif
  5455. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_VOID) != DUK_TOK_VOID)
  5456. #error mismatch in token defines
  5457. #endif
  5458. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_WHILE) != DUK_TOK_WHILE)
  5459. #error mismatch in token defines
  5460. #endif
  5461. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_WITH) != DUK_TOK_WITH)
  5462. #error mismatch in token defines
  5463. #endif
  5464. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CLASS) != DUK_TOK_CLASS)
  5465. #error mismatch in token defines
  5466. #endif
  5467. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CONST) != DUK_TOK_CONST)
  5468. #error mismatch in token defines
  5469. #endif
  5470. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_ENUM) != DUK_TOK_ENUM)
  5471. #error mismatch in token defines
  5472. #endif
  5473. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_EXPORT) != DUK_TOK_EXPORT)
  5474. #error mismatch in token defines
  5475. #endif
  5476. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_EXTENDS) != DUK_TOK_EXTENDS)
  5477. #error mismatch in token defines
  5478. #endif
  5479. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IMPORT) != DUK_TOK_IMPORT)
  5480. #error mismatch in token defines
  5481. #endif
  5482. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_SUPER) != DUK_TOK_SUPER)
  5483. #error mismatch in token defines
  5484. #endif
  5485. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_LC_NULL) != DUK_TOK_NULL)
  5486. #error mismatch in token defines
  5487. #endif
  5488. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_TRUE) != DUK_TOK_TRUE)
  5489. #error mismatch in token defines
  5490. #endif
  5491. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_FALSE) != DUK_TOK_FALSE)
  5492. #error mismatch in token defines
  5493. #endif
  5494. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IMPLEMENTS) != DUK_TOK_IMPLEMENTS)
  5495. #error mismatch in token defines
  5496. #endif
  5497. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_INTERFACE) != DUK_TOK_INTERFACE)
  5498. #error mismatch in token defines
  5499. #endif
  5500. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_LET) != DUK_TOK_LET)
  5501. #error mismatch in token defines
  5502. #endif
  5503. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PACKAGE) != DUK_TOK_PACKAGE)
  5504. #error mismatch in token defines
  5505. #endif
  5506. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PRIVATE) != DUK_TOK_PRIVATE)
  5507. #error mismatch in token defines
  5508. #endif
  5509. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PROTECTED) != DUK_TOK_PROTECTED)
  5510. #error mismatch in token defines
  5511. #endif
  5512. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PUBLIC) != DUK_TOK_PUBLIC)
  5513. #error mismatch in token defines
  5514. #endif
  5515. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_STATIC) != DUK_TOK_STATIC)
  5516. #error mismatch in token defines
  5517. #endif
  5518. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_YIELD) != DUK_TOK_YIELD)
  5519. #error mismatch in token defines
  5520. #endif
  5521. /* Regexp tokens */
  5522. #define DUK_RETOK_EOF 0
  5523. #define DUK_RETOK_DISJUNCTION 1
  5524. #define DUK_RETOK_QUANTIFIER 2
  5525. #define DUK_RETOK_ASSERT_START 3
  5526. #define DUK_RETOK_ASSERT_END 4
  5527. #define DUK_RETOK_ASSERT_WORD_BOUNDARY 5
  5528. #define DUK_RETOK_ASSERT_NOT_WORD_BOUNDARY 6
  5529. #define DUK_RETOK_ASSERT_START_POS_LOOKAHEAD 7
  5530. #define DUK_RETOK_ASSERT_START_NEG_LOOKAHEAD 8
  5531. #define DUK_RETOK_ATOM_PERIOD 9
  5532. #define DUK_RETOK_ATOM_CHAR 10
  5533. #define DUK_RETOK_ATOM_DIGIT 11
  5534. #define DUK_RETOK_ATOM_NOT_DIGIT 12
  5535. #define DUK_RETOK_ATOM_WHITE 13
  5536. #define DUK_RETOK_ATOM_NOT_WHITE 14
  5537. #define DUK_RETOK_ATOM_WORD_CHAR 15
  5538. #define DUK_RETOK_ATOM_NOT_WORD_CHAR 16
  5539. #define DUK_RETOK_ATOM_BACKREFERENCE 17
  5540. #define DUK_RETOK_ATOM_START_CAPTURE_GROUP 18
  5541. #define DUK_RETOK_ATOM_START_NONCAPTURE_GROUP 19
  5542. #define DUK_RETOK_ATOM_START_CHARCLASS 20
  5543. #define DUK_RETOK_ATOM_START_CHARCLASS_INVERTED 21
  5544. #define DUK_RETOK_ATOM_END_GROUP 22
  5545. /* Constants for duk_lexer_ctx.buf. */
  5546. #define DUK_LEXER_TEMP_BUF_LIMIT 256
  5547. /* A token value. Can be memcpy()'d, but note that slot1/slot2 values are on the valstack.
  5548. * Some fields (like num, str1, str2) are only valid for specific token types and may have
  5549. * stale values otherwise.
  5550. */
  5551. struct duk_token {
  5552. duk_small_int_t t; /* token type (with reserved word identification) */
  5553. duk_small_int_t t_nores; /* token type (with reserved words as DUK_TOK_IDENTIFER) */
  5554. duk_double_t num; /* numeric value of token */
  5555. duk_hstring *str1; /* string 1 of token (borrowed, stored to ctx->slot1_idx) */
  5556. duk_hstring *str2; /* string 2 of token (borrowed, stored to ctx->slot2_idx) */
  5557. duk_size_t start_offset; /* start byte offset of token in lexer input */
  5558. duk_int_t start_line; /* start line of token (first char) */
  5559. duk_int_t num_escapes; /* number of escapes and line continuations (for directive prologue) */
  5560. duk_bool_t lineterm; /* token was preceded by a lineterm */
  5561. duk_bool_t allow_auto_semi; /* token allows automatic semicolon insertion (eof or preceded by newline) */
  5562. };
  5563. #define DUK_RE_QUANTIFIER_INFINITE ((duk_uint32_t) 0xffffffffUL)
  5564. /* A regexp token value. */
  5565. struct duk_re_token {
  5566. duk_small_int_t t; /* token type */
  5567. duk_small_int_t greedy;
  5568. duk_uint_fast32_t num; /* numeric value (character, count) */
  5569. duk_uint_fast32_t qmin;
  5570. duk_uint_fast32_t qmax;
  5571. };
  5572. /* A structure for 'snapshotting' a point for rewinding */
  5573. struct duk_lexer_point {
  5574. duk_size_t offset;
  5575. duk_int_t line;
  5576. };
  5577. /* Lexer codepoint with additional info like offset/line number */
  5578. struct duk_lexer_codepoint {
  5579. duk_codepoint_t codepoint;
  5580. duk_size_t offset;
  5581. duk_int_t line;
  5582. };
  5583. /* Lexer context. Same context is used for Ecmascript and Regexp parsing. */
  5584. struct duk_lexer_ctx {
  5585. #if defined(DUK_USE_LEXER_SLIDING_WINDOW)
  5586. duk_lexer_codepoint *window; /* unicode code points, window[0] is always next, points to 'buffer' */
  5587. duk_lexer_codepoint buffer[DUK_LEXER_BUFFER_SIZE];
  5588. #else
  5589. duk_lexer_codepoint window[DUK_LEXER_WINDOW_SIZE]; /* unicode code points, window[0] is always next */
  5590. #endif
  5591. duk_hthread *thr; /* thread; minimizes argument passing */
  5592. const duk_uint8_t *input; /* input string (may be a user pointer) */
  5593. duk_size_t input_length; /* input byte length */
  5594. duk_size_t input_offset; /* input offset for window leading edge (not window[0]) */
  5595. duk_int_t input_line; /* input linenumber at input_offset (not window[0]), init to 1 */
  5596. duk_idx_t slot1_idx; /* valstack slot for 1st token value */
  5597. duk_idx_t slot2_idx; /* valstack slot for 2nd token value */
  5598. duk_idx_t buf_idx; /* valstack slot for temp buffer */
  5599. duk_hbuffer_dynamic *buf; /* temp accumulation buffer */
  5600. duk_bufwriter_ctx bw; /* bufwriter for temp accumulation */
  5601. duk_int_t token_count; /* number of tokens parsed */
  5602. duk_int_t token_limit; /* maximum token count before error (sanity backstop) */
  5603. };
  5604. /*
  5605. * Prototypes
  5606. */
  5607. DUK_INTERNAL_DECL void duk_lexer_initctx(duk_lexer_ctx *lex_ctx);
  5608. DUK_INTERNAL_DECL void duk_lexer_setpoint(duk_lexer_ctx *lex_ctx, duk_lexer_point *pt);
  5609. DUK_INTERNAL_DECL
  5610. void duk_lexer_parse_js_input_element(duk_lexer_ctx *lex_ctx,
  5611. duk_token *out_token,
  5612. duk_bool_t strict_mode,
  5613. duk_bool_t regexp_mode);
  5614. #ifdef DUK_USE_REGEXP_SUPPORT
  5615. DUK_INTERNAL_DECL void duk_lexer_parse_re_token(duk_lexer_ctx *lex_ctx, duk_re_token *out_token);
  5616. DUK_INTERNAL_DECL void duk_lexer_parse_re_ranges(duk_lexer_ctx *lex_ctx, duk_re_range_callback gen_range, void *userdata);
  5617. #endif /* DUK_USE_REGEXP_SUPPORT */
  5618. #endif /* DUK_LEXER_H_INCLUDED */
  5619. #line 1 "duk_js_compiler.h"
  5620. /*
  5621. * Ecmascript compiler.
  5622. */
  5623. #ifndef DUK_JS_COMPILER_H_INCLUDED
  5624. #define DUK_JS_COMPILER_H_INCLUDED
  5625. /* ecmascript compiler limits */
  5626. #define DUK_COMPILER_TOKEN_LIMIT 100000000L /* 1e8: protects against deeply nested inner functions */
  5627. /* maximum loopcount for peephole optimization */
  5628. #define DUK_COMPILER_PEEPHOLE_MAXITER 3
  5629. /* maximum bytecode length in instructions */
  5630. #define DUK_COMPILER_MAX_BYTECODE_LENGTH (256L * 1024L * 1024L) /* 1 GB */
  5631. /*
  5632. * Compiler intermediate values
  5633. *
  5634. * Intermediate values describe either plain values (e.g. strings or
  5635. * numbers) or binary operations which have not yet been coerced into
  5636. * either a left-hand-side or right-hand-side role (e.g. object property).
  5637. */
  5638. #define DUK_IVAL_NONE 0 /* no value */
  5639. #define DUK_IVAL_PLAIN 1 /* register, constant, or value */
  5640. #define DUK_IVAL_ARITH 2 /* binary arithmetic; DUK_OP_ADD, DUK_OP_EQ, other binary ops */
  5641. #define DUK_IVAL_ARITH_EXTRAOP 3 /* binary arithmetic using extraops; DUK_EXTRAOP_INSTOF etc */
  5642. #define DUK_IVAL_PROP 4 /* property access */
  5643. #define DUK_IVAL_VAR 5 /* variable access */
  5644. #define DUK_ISPEC_NONE 0 /* no value */
  5645. #define DUK_ISPEC_VALUE 1 /* value resides in 'valstack_idx' */
  5646. #define DUK_ISPEC_REGCONST 2 /* value resides in a register or constant */
  5647. /* bit mask which indicates that a regconst is a constant instead of a register */
  5648. #define DUK_JS_CONST_MARKER 0x80000000UL
  5649. /* type to represent a reg/const reference during compilation */
  5650. typedef duk_uint32_t duk_regconst_t;
  5651. /* type to represent a straight register reference, with <0 indicating none */
  5652. typedef duk_int32_t duk_reg_t;
  5653. typedef struct {
  5654. duk_small_uint_t t; /* DUK_ISPEC_XXX */
  5655. duk_regconst_t regconst;
  5656. duk_idx_t valstack_idx; /* always set; points to a reserved valstack slot */
  5657. } duk_ispec;
  5658. typedef struct {
  5659. /*
  5660. * PLAIN: x1
  5661. * ARITH: x1 <op> x2
  5662. * PROP: x1.x2
  5663. * VAR: x1 (name)
  5664. */
  5665. /* XXX: can be optimized for smaller footprint esp. on 32-bit environments */
  5666. duk_small_uint_t t; /* DUK_IVAL_XXX */
  5667. duk_small_uint_t op; /* bytecode opcode (or extraop) for binary ops */
  5668. duk_ispec x1;
  5669. duk_ispec x2;
  5670. } duk_ivalue;
  5671. /*
  5672. * Bytecode instruction representation during compilation
  5673. *
  5674. * Contains the actual instruction and (optionally) debug info.
  5675. */
  5676. struct duk_compiler_instr {
  5677. duk_instr_t ins;
  5678. #if defined(DUK_USE_PC2LINE)
  5679. duk_uint32_t line;
  5680. #endif
  5681. };
  5682. /*
  5683. * Compiler state
  5684. */
  5685. #define DUK_LABEL_FLAG_ALLOW_BREAK (1 << 0)
  5686. #define DUK_LABEL_FLAG_ALLOW_CONTINUE (1 << 1)
  5687. #define DUK_DECL_TYPE_VAR 0
  5688. #define DUK_DECL_TYPE_FUNC 1
  5689. /* XXX: optimize to 16 bytes */
  5690. typedef struct {
  5691. duk_small_uint_t flags;
  5692. duk_int_t label_id; /* numeric label_id (-1 reserved as marker) */
  5693. duk_hstring *h_label; /* borrowed label name */
  5694. duk_int_t catch_depth; /* catch depth at point of definition */
  5695. duk_int_t pc_label; /* pc of label statement:
  5696. * pc+1: break jump site
  5697. * pc+2: continue jump site
  5698. */
  5699. /* Fast jumps (which avoid longjmp) jump directly to the jump sites
  5700. * which are always known even while the iteration/switch statement
  5701. * is still being parsed. A final peephole pass "straightens out"
  5702. * the jumps.
  5703. */
  5704. } duk_labelinfo;
  5705. /* Compiling state of one function, eventually converted to duk_hcompiledfunction */
  5706. struct duk_compiler_func {
  5707. /* These pointers are at the start of the struct so that they pack
  5708. * nicely. Mixing pointers and integer values is bad on some
  5709. * platforms (e.g. if int is 32 bits and pointers are 64 bits).
  5710. */
  5711. duk_bufwriter_ctx bw_code; /* bufwriter for code */
  5712. duk_hstring *h_name; /* function name (borrowed reference), ends up in _name */
  5713. /* h_code: held in bw_code */
  5714. duk_hobject *h_consts; /* array */
  5715. duk_hobject *h_funcs; /* array of function templates: [func1, offset1, line1, func2, offset2, line2]
  5716. * offset/line points to closing brace to allow skipping on pass 2
  5717. */
  5718. duk_hobject *h_decls; /* array of declarations: [ name1, val1, name2, val2, ... ]
  5719. * valN = (typeN) | (fnum << 8), where fnum is inner func number (0 for vars)
  5720. * record function and variable declarations in pass 1
  5721. */
  5722. duk_hobject *h_labelnames; /* array of active label names */
  5723. duk_hbuffer_dynamic *h_labelinfos; /* C array of duk_labelinfo */
  5724. duk_hobject *h_argnames; /* array of formal argument names (-> _Formals) */
  5725. duk_hobject *h_varmap; /* variable map for pass 2 (identifier -> register number or null (unmapped)) */
  5726. /* value stack indices for tracking objects */
  5727. /* code_idx: not needed */
  5728. duk_idx_t consts_idx;
  5729. duk_idx_t funcs_idx;
  5730. duk_idx_t decls_idx;
  5731. duk_idx_t labelnames_idx;
  5732. duk_idx_t labelinfos_idx;
  5733. duk_idx_t argnames_idx;
  5734. duk_idx_t varmap_idx;
  5735. /* temp reg handling */
  5736. duk_reg_t temp_first; /* first register that is a temporary (below: variables) */
  5737. duk_reg_t temp_next; /* next temporary register to allocate */
  5738. duk_reg_t temp_max; /* highest value of temp_reg (temp_max - 1 is highest used reg) */
  5739. /* shuffle registers if large number of regs/consts */
  5740. duk_reg_t shuffle1;
  5741. duk_reg_t shuffle2;
  5742. duk_reg_t shuffle3;
  5743. /* stats for current expression being parsed */
  5744. duk_int_t nud_count;
  5745. duk_int_t led_count;
  5746. duk_int_t paren_level; /* parenthesis count, 0 = top level */
  5747. duk_bool_t expr_lhs; /* expression is left-hand-side compatible */
  5748. duk_bool_t allow_in; /* current paren level allows 'in' token */
  5749. /* misc */
  5750. duk_int_t stmt_next; /* statement id allocation (running counter) */
  5751. duk_int_t label_next; /* label id allocation (running counter) */
  5752. duk_int_t catch_depth; /* catch stack depth */
  5753. duk_int_t with_depth; /* with stack depth (affects identifier lookups) */
  5754. duk_int_t fnum_next; /* inner function numbering */
  5755. duk_int_t num_formals; /* number of formal arguments */
  5756. duk_reg_t reg_stmt_value; /* register for writing value of 'non-empty' statements (global or eval code), -1 is marker */
  5757. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  5758. duk_int_t min_line; /* XXX: typing (duk_hcompiledfunction has duk_uint32_t) */
  5759. duk_int_t max_line;
  5760. #endif
  5761. /* status booleans */
  5762. duk_bool_t is_function; /* is an actual function (not global/eval code) */
  5763. duk_bool_t is_eval; /* is eval code */
  5764. duk_bool_t is_global; /* is global code */
  5765. duk_bool_t is_setget; /* is a setter/getter */
  5766. duk_bool_t is_decl; /* is a function declaration (as opposed to function expression) */
  5767. duk_bool_t is_strict; /* function is strict */
  5768. duk_bool_t is_notail; /* function must not be tail called */
  5769. duk_bool_t in_directive_prologue; /* parsing in "directive prologue", recognize directives */
  5770. duk_bool_t in_scanning; /* parsing in "scanning" phase (first pass) */
  5771. duk_bool_t may_direct_eval; /* function may call direct eval */
  5772. duk_bool_t id_access_arguments; /* function refers to 'arguments' identifier */
  5773. duk_bool_t id_access_slow; /* function makes one or more slow path accesses */
  5774. duk_bool_t is_arguments_shadowed; /* argument/function declaration shadows 'arguments' */
  5775. duk_bool_t needs_shuffle; /* function needs shuffle registers */
  5776. duk_bool_t reject_regexp_in_adv; /* reject RegExp literal on next advance() call; needed for handling IdentifierName productions */
  5777. };
  5778. struct duk_compiler_ctx {
  5779. duk_hthread *thr;
  5780. /* filename being compiled (ends up in functions' '_filename' property) */
  5781. duk_hstring *h_filename; /* borrowed reference */
  5782. /* lexing (tokenization) state (contains two valstack slot indices) */
  5783. duk_lexer_ctx lex;
  5784. /* current and previous token for parsing */
  5785. duk_token prev_token;
  5786. duk_token curr_token;
  5787. duk_idx_t tok11_idx; /* curr_token slot1 (matches 'lex' slot1_idx) */
  5788. duk_idx_t tok12_idx; /* curr_token slot2 (matches 'lex' slot2_idx) */
  5789. duk_idx_t tok21_idx; /* prev_token slot1 */
  5790. duk_idx_t tok22_idx; /* prev_token slot2 */
  5791. /* recursion limit */
  5792. duk_int_t recursion_depth;
  5793. duk_int_t recursion_limit;
  5794. /* code emission temporary */
  5795. duk_int_t emit_jumpslot_pc;
  5796. /* current function being compiled (embedded instead of pointer for more compact access) */
  5797. duk_compiler_func curr_func;
  5798. };
  5799. /*
  5800. * Prototypes
  5801. */
  5802. #define DUK_JS_COMPILE_FLAG_EVAL (1 << 0) /* source is eval code (not global) */
  5803. #define DUK_JS_COMPILE_FLAG_STRICT (1 << 1) /* strict outer context */
  5804. #define DUK_JS_COMPILE_FLAG_FUNCEXPR (1 << 2) /* source is a function expression (used for Function constructor) */
  5805. 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);
  5806. #endif /* DUK_JS_COMPILER_H_INCLUDED */
  5807. #line 1 "duk_regexp.h"
  5808. /*
  5809. * Regular expression structs, constants, and bytecode defines.
  5810. */
  5811. #ifndef DUK_REGEXP_H_INCLUDED
  5812. #define DUK_REGEXP_H_INCLUDED
  5813. /* maximum bytecode copies for {n,m} quantifiers */
  5814. #define DUK_RE_MAX_ATOM_COPIES 1000
  5815. /* regexp compilation limits */
  5816. #define DUK_RE_COMPILE_TOKEN_LIMIT 100000000L /* 1e8 */
  5817. /* regexp execution limits */
  5818. #define DUK_RE_EXECUTE_STEPS_LIMIT 1000000000L /* 1e9 */
  5819. /* regexp opcodes */
  5820. #define DUK_REOP_MATCH 1
  5821. #define DUK_REOP_CHAR 2
  5822. #define DUK_REOP_PERIOD 3
  5823. #define DUK_REOP_RANGES 4
  5824. #define DUK_REOP_INVRANGES 5
  5825. #define DUK_REOP_JUMP 6
  5826. #define DUK_REOP_SPLIT1 7
  5827. #define DUK_REOP_SPLIT2 8
  5828. #define DUK_REOP_SQMINIMAL 9
  5829. #define DUK_REOP_SQGREEDY 10
  5830. #define DUK_REOP_SAVE 11
  5831. #define DUK_REOP_WIPERANGE 12
  5832. #define DUK_REOP_LOOKPOS 13
  5833. #define DUK_REOP_LOOKNEG 14
  5834. #define DUK_REOP_BACKREFERENCE 15
  5835. #define DUK_REOP_ASSERT_START 16
  5836. #define DUK_REOP_ASSERT_END 17
  5837. #define DUK_REOP_ASSERT_WORD_BOUNDARY 18
  5838. #define DUK_REOP_ASSERT_NOT_WORD_BOUNDARY 19
  5839. /* flags */
  5840. #define DUK_RE_FLAG_GLOBAL (1 << 0)
  5841. #define DUK_RE_FLAG_IGNORE_CASE (1 << 1)
  5842. #define DUK_RE_FLAG_MULTILINE (1 << 2)
  5843. struct duk_re_matcher_ctx {
  5844. duk_hthread *thr;
  5845. duk_uint32_t re_flags;
  5846. const duk_uint8_t *input;
  5847. const duk_uint8_t *input_end;
  5848. const duk_uint8_t *bytecode;
  5849. const duk_uint8_t *bytecode_end;
  5850. const duk_uint8_t **saved; /* allocated from valstack (fixed buffer) */
  5851. duk_uint32_t nsaved;
  5852. duk_uint32_t recursion_depth;
  5853. duk_uint32_t recursion_limit;
  5854. duk_uint32_t steps_count;
  5855. duk_uint32_t steps_limit;
  5856. };
  5857. struct duk_re_compiler_ctx {
  5858. duk_hthread *thr;
  5859. duk_uint32_t re_flags;
  5860. duk_lexer_ctx lex;
  5861. duk_re_token curr_token;
  5862. duk_bufwriter_ctx bw;
  5863. duk_uint32_t captures; /* highest capture number emitted so far (used as: ++captures) */
  5864. duk_uint32_t highest_backref;
  5865. duk_uint32_t recursion_depth;
  5866. duk_uint32_t recursion_limit;
  5867. duk_uint32_t nranges; /* internal temporary value, used for char classes */
  5868. };
  5869. /*
  5870. * Prototypes
  5871. */
  5872. DUK_INTERNAL_DECL void duk_regexp_compile(duk_hthread *thr);
  5873. DUK_INTERNAL_DECL void duk_regexp_create_instance(duk_hthread *thr);
  5874. DUK_INTERNAL_DECL void duk_regexp_match(duk_hthread *thr);
  5875. DUK_INTERNAL_DECL void duk_regexp_match_force_global(duk_hthread *thr); /* hacky helper for String.prototype.split() */
  5876. #endif /* DUK_REGEXP_H_INCLUDED */
  5877. #line 1 "duk_tval.h"
  5878. /*
  5879. * Tagged type definition (duk_tval) and accessor macros.
  5880. *
  5881. * Access all fields through the accessor macros, as the representation
  5882. * is quite tricky.
  5883. *
  5884. * There are two packed type alternatives: an 8-byte representation
  5885. * based on an IEEE double (preferred for compactness), and a 12-byte
  5886. * representation (portability). The latter is needed also in e.g.
  5887. * 64-bit environments (it usually pads to 16 bytes per value).
  5888. *
  5889. * Selecting the tagged type format involves many trade-offs (memory
  5890. * use, size and performance of generated code, portability, etc),
  5891. * see doc/types.rst for a detailed discussion (especially of how the
  5892. * IEEE double format is used to pack tagged values).
  5893. *
  5894. * NB: because macro arguments are often expressions, macros should
  5895. * avoid evaluating their argument more than once.
  5896. */
  5897. #ifndef DUK_TVAL_H_INCLUDED
  5898. #define DUK_TVAL_H_INCLUDED
  5899. /* sanity */
  5900. #if !defined(DUK_USE_DOUBLE_LE) && !defined(DUK_USE_DOUBLE_ME) && !defined(DUK_USE_DOUBLE_BE)
  5901. #error unsupported: cannot determine byte order variant
  5902. #endif
  5903. #ifdef DUK_USE_PACKED_TVAL
  5904. /* ======================================================================== */
  5905. /*
  5906. * Packed 8-byte representation
  5907. */
  5908. /* sanity */
  5909. #if !defined(DUK_USE_PACKED_TVAL_POSSIBLE)
  5910. #error packed representation not supported
  5911. #endif
  5912. /* use duk_double_union as duk_tval directly */
  5913. typedef union duk_double_union duk_tval;
  5914. /* tags */
  5915. #define DUK_TAG_NORMALIZED_NAN 0x7ff8UL /* the NaN variant we use */
  5916. /* avoid tag 0xfff0, no risk of confusion with negative infinity */
  5917. #if defined(DUK_USE_FASTINT)
  5918. #define DUK_TAG_FASTINT 0xfff1UL /* embed: integer value */
  5919. #endif
  5920. #define DUK_TAG_UNUSED 0xfff2UL /* marker; not actual tagged value */
  5921. #define DUK_TAG_UNDEFINED 0xfff3UL /* embed: nothing */
  5922. #define DUK_TAG_NULL 0xfff4UL /* embed: nothing */
  5923. #define DUK_TAG_BOOLEAN 0xfff5UL /* embed: 0 or 1 (false or true) */
  5924. /* DUK_TAG_NUMBER would logically go here, but it has multiple 'tags' */
  5925. #define DUK_TAG_POINTER 0xfff6UL /* embed: void ptr */
  5926. #define DUK_TAG_LIGHTFUNC 0xfff7UL /* embed: func ptr */
  5927. #define DUK_TAG_STRING 0xfff8UL /* embed: duk_hstring ptr */
  5928. #define DUK_TAG_OBJECT 0xfff9UL /* embed: duk_hobject ptr */
  5929. #define DUK_TAG_BUFFER 0xfffaUL /* embed: duk_hbuffer ptr */
  5930. /* for convenience */
  5931. #define DUK_XTAG_BOOLEAN_FALSE 0xfff50000UL
  5932. #define DUK_XTAG_BOOLEAN_TRUE 0xfff50001UL
  5933. /* two casts to avoid gcc warning: "warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]" */
  5934. #ifdef DUK_USE_64BIT_OPS
  5935. #ifdef DUK_USE_DOUBLE_ME
  5936. #define DUK__TVAL_SET_TAGGEDPOINTER(v,h,tag) do { \
  5937. (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) (tag)) << 16) | (((duk_uint64_t) (duk_uint32_t) (h)) << 32); \
  5938. } while (0)
  5939. #else
  5940. #define DUK__TVAL_SET_TAGGEDPOINTER(v,h,tag) do { \
  5941. (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) (tag)) << 48) | ((duk_uint64_t) (duk_uint32_t) (h)); \
  5942. } while (0)
  5943. #endif
  5944. #else /* DUK_USE_64BIT_OPS */
  5945. #define DUK__TVAL_SET_TAGGEDPOINTER(v,h,tag) do { \
  5946. (v)->ui[DUK_DBL_IDX_UI0] = ((duk_uint32_t) (tag)) << 16; \
  5947. (v)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (h); \
  5948. } while (0)
  5949. #endif /* DUK_USE_64BIT_OPS */
  5950. #ifdef DUK_USE_64BIT_OPS
  5951. /* Double casting for pointer to avoid gcc warning (cast from pointer to integer of different size) */
  5952. #ifdef DUK_USE_DOUBLE_ME
  5953. #define DUK__TVAL_SET_LIGHTFUNC(v,fp,flags) do { \
  5954. (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_LIGHTFUNC) << 16) | \
  5955. ((duk_uint64_t) (flags)) | \
  5956. (((duk_uint64_t) (duk_uint32_t) (fp)) << 32); \
  5957. } while (0)
  5958. #else
  5959. #define DUK__TVAL_SET_LIGHTFUNC(v,fp,flags) do { \
  5960. (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_LIGHTFUNC) << 48) | \
  5961. (((duk_uint64_t) (flags)) << 32) | \
  5962. ((duk_uint64_t) (duk_uint32_t) (fp)); \
  5963. } while (0)
  5964. #endif
  5965. #else /* DUK_USE_64BIT_OPS */
  5966. #define DUK__TVAL_SET_LIGHTFUNC(v,fp,flags) do { \
  5967. (v)->ui[DUK_DBL_IDX_UI0] = (((duk_uint32_t) DUK_TAG_LIGHTFUNC) << 16) | ((duk_uint32_t) (flags)); \
  5968. (v)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (fp); \
  5969. } while (0)
  5970. #endif /* DUK_USE_64BIT_OPS */
  5971. #if defined(DUK_USE_FASTINT)
  5972. /* Note: masking is done for 'i' to deal with negative numbers correctly */
  5973. #ifdef DUK_USE_DOUBLE_ME
  5974. #define DUK__TVAL_SET_FASTINT(v,i) do { \
  5975. (v)->ui[DUK_DBL_IDX_UI0] = ((duk_uint32_t) DUK_TAG_FASTINT) << 16 | (((duk_uint32_t) ((i) >> 32)) & 0x0000ffffUL); \
  5976. (v)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (i); \
  5977. } while (0)
  5978. #define DUK__TVAL_SET_FASTINT_U32(v,i) do { \
  5979. (v)->ui[DUK_DBL_IDX_UI0] = ((duk_uint32_t) DUK_TAG_FASTINT) << 16; \
  5980. (v)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (i); \
  5981. } while (0)
  5982. #else
  5983. #define DUK__TVAL_SET_FASTINT(v,i) do { \
  5984. (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_FASTINT) << 48) | (((duk_uint64_t) (i)) & 0x0000ffffffffffffULL); \
  5985. } while (0)
  5986. #define DUK__TVAL_SET_FASTINT_U32(v,i) do { \
  5987. (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_FASTINT) << 48) | (duk_uint64_t) (i); \
  5988. } while (0)
  5989. #endif
  5990. #define DUK__TVAL_SET_FASTINT_I32(v,i) do { \
  5991. duk_int64_t duk__tmp = (duk_int64_t) (i); \
  5992. DUK_TVAL_SET_FASTINT((v), duk__tmp); \
  5993. } while (0)
  5994. /* XXX: clumsy sign extend and masking of 16 topmost bits */
  5995. #ifdef DUK_USE_DOUBLE_ME
  5996. #define DUK__TVAL_GET_FASTINT(v) (((duk_int64_t) ((((duk_uint64_t) (v)->ui[DUK_DBL_IDX_UI0]) << 32) | ((duk_uint64_t) (v)->ui[DUK_DBL_IDX_UI1]))) << 16 >> 16)
  5997. #else
  5998. #define DUK__TVAL_GET_FASTINT(v) ((((duk_int64_t) (v)->ull[DUK_DBL_IDX_ULL0]) << 16) >> 16)
  5999. #endif
  6000. #define DUK__TVAL_GET_FASTINT_U32(v) ((v)->ui[DUK_DBL_IDX_UI1])
  6001. #define DUK__TVAL_GET_FASTINT_I32(v) ((duk_int32_t) (v)->ui[DUK_DBL_IDX_UI1])
  6002. #endif /* DUK_USE_FASTINT */
  6003. #define DUK_TVAL_SET_UNDEFINED(v) do { \
  6004. (v)->us[DUK_DBL_IDX_US0] = (duk_uint16_t) DUK_TAG_UNDEFINED; \
  6005. } while (0)
  6006. #define DUK_TVAL_SET_UNUSED(v) do { \
  6007. (v)->us[DUK_DBL_IDX_US0] = (duk_uint16_t) DUK_TAG_UNUSED; \
  6008. } while (0)
  6009. #define DUK_TVAL_SET_NULL(v) do { \
  6010. (v)->us[DUK_DBL_IDX_US0] = (duk_uint16_t) DUK_TAG_NULL; \
  6011. } while (0)
  6012. #define DUK_TVAL_SET_BOOLEAN(v,val) DUK_DBLUNION_SET_HIGH32((v), (((duk_uint32_t) DUK_TAG_BOOLEAN) << 16) | ((duk_uint32_t) (val)))
  6013. #define DUK_TVAL_SET_NAN(v) DUK_DBLUNION_SET_NAN_FULL((v))
  6014. /* Assumes that caller has normalized NaNs, otherwise trouble ahead. */
  6015. #if defined(DUK_USE_FASTINT)
  6016. #define DUK_TVAL_SET_DOUBLE(v,d) do { \
  6017. duk_double_t duk__dblval; \
  6018. duk__dblval = (d); \
  6019. DUK_ASSERT_DOUBLE_IS_NORMALIZED(duk__dblval); \
  6020. DUK_DBLUNION_SET_DOUBLE((v), duk__dblval); \
  6021. } while (0)
  6022. #define DUK_TVAL_SET_FASTINT(v,i) DUK__TVAL_SET_FASTINT((v), (i))
  6023. #define DUK_TVAL_SET_FASTINT_I32(v,i) DUK__TVAL_SET_FASTINT_I32((v), (i))
  6024. #define DUK_TVAL_SET_FASTINT_U32(v,i) DUK__TVAL_SET_FASTINT_U32((v), (i))
  6025. #define DUK_TVAL_SET_NUMBER_CHKFAST(v,d) duk_tval_set_number_chkfast((v), (d))
  6026. #define DUK_TVAL_SET_NUMBER(v,d) DUK_TVAL_SET_DOUBLE((v), (d))
  6027. #define DUK_TVAL_CHKFAST_INPLACE(v) do { \
  6028. duk_tval *duk__tv; \
  6029. duk_double_t duk__d; \
  6030. duk__tv = (v); \
  6031. if (DUK_TVAL_IS_DOUBLE(duk__tv)) { \
  6032. duk__d = DUK_TVAL_GET_DOUBLE(duk__tv); \
  6033. DUK_TVAL_SET_NUMBER_CHKFAST(duk__tv, duk__d); \
  6034. } \
  6035. } while (0)
  6036. #else
  6037. #define DUK_TVAL_SET_DOUBLE(v,d) do { \
  6038. duk_double_t duk__dblval; \
  6039. duk__dblval = (d); \
  6040. DUK_ASSERT_DOUBLE_IS_NORMALIZED(duk__dblval); \
  6041. DUK_DBLUNION_SET_DOUBLE((v), duk__dblval); \
  6042. } while (0)
  6043. #define DUK_TVAL_SET_NUMBER_CHKFAST(v,d) DUK_TVAL_SET_DOUBLE((v), (d))
  6044. #define DUK_TVAL_SET_NUMBER(v,d) DUK_TVAL_SET_DOUBLE((v), (d))
  6045. #define DUK_TVAL_CHKFAST_INPLACE(v) do { } while (0)
  6046. #endif
  6047. #define DUK_TVAL_SET_LIGHTFUNC(v,fp,flags) DUK__TVAL_SET_LIGHTFUNC((v), (fp), (flags))
  6048. #define DUK_TVAL_SET_STRING(v,h) DUK__TVAL_SET_TAGGEDPOINTER((v), (h), DUK_TAG_STRING)
  6049. #define DUK_TVAL_SET_OBJECT(v,h) DUK__TVAL_SET_TAGGEDPOINTER((v), (h), DUK_TAG_OBJECT)
  6050. #define DUK_TVAL_SET_BUFFER(v,h) DUK__TVAL_SET_TAGGEDPOINTER((v), (h), DUK_TAG_BUFFER)
  6051. #define DUK_TVAL_SET_POINTER(v,p) DUK__TVAL_SET_TAGGEDPOINTER((v), (p), DUK_TAG_POINTER)
  6052. #define DUK_TVAL_SET_TVAL(v,x) do { *(v) = *(x); } while (0)
  6053. /* getters */
  6054. #define DUK_TVAL_GET_BOOLEAN(v) ((int) (v)->us[DUK_DBL_IDX_US1])
  6055. #if defined(DUK_USE_FASTINT)
  6056. #define DUK_TVAL_GET_DOUBLE(v) ((v)->d)
  6057. #define DUK_TVAL_GET_FASTINT(v) DUK__TVAL_GET_FASTINT((v))
  6058. #define DUK_TVAL_GET_FASTINT_U32(v) DUK__TVAL_GET_FASTINT_U32((v))
  6059. #define DUK_TVAL_GET_FASTINT_I32(v) DUK__TVAL_GET_FASTINT_I32((v))
  6060. #define DUK_TVAL_GET_NUMBER(v) duk_tval_get_number_packed((v))
  6061. #else
  6062. #define DUK_TVAL_GET_NUMBER(v) ((v)->d)
  6063. #define DUK_TVAL_GET_DOUBLE(v) ((v)->d)
  6064. #endif
  6065. #define DUK_TVAL_GET_LIGHTFUNC(v,out_fp,out_flags) do { \
  6066. (out_flags) = (v)->ui[DUK_DBL_IDX_UI0] & 0xffffUL; \
  6067. (out_fp) = (duk_c_function) (v)->ui[DUK_DBL_IDX_UI1]; \
  6068. } while (0)
  6069. #define DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(v) ((duk_c_function) ((v)->ui[DUK_DBL_IDX_UI1]))
  6070. #define DUK_TVAL_GET_LIGHTFUNC_FLAGS(v) (((int) (v)->ui[DUK_DBL_IDX_UI0]) & 0xffffUL)
  6071. #define DUK_TVAL_GET_STRING(v) ((duk_hstring *) (v)->vp[DUK_DBL_IDX_VP1])
  6072. #define DUK_TVAL_GET_OBJECT(v) ((duk_hobject *) (v)->vp[DUK_DBL_IDX_VP1])
  6073. #define DUK_TVAL_GET_BUFFER(v) ((duk_hbuffer *) (v)->vp[DUK_DBL_IDX_VP1])
  6074. #define DUK_TVAL_GET_POINTER(v) ((void *) (v)->vp[DUK_DBL_IDX_VP1])
  6075. #define DUK_TVAL_GET_HEAPHDR(v) ((duk_heaphdr *) (v)->vp[DUK_DBL_IDX_VP1])
  6076. /* decoding */
  6077. #define DUK_TVAL_GET_TAG(v) ((duk_small_uint_t) (v)->us[DUK_DBL_IDX_US0])
  6078. #define DUK_TVAL_IS_UNDEFINED(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_UNDEFINED)
  6079. #define DUK_TVAL_IS_UNUSED(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_UNUSED)
  6080. #define DUK_TVAL_IS_NULL(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_NULL)
  6081. #define DUK_TVAL_IS_BOOLEAN(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_BOOLEAN)
  6082. #define DUK_TVAL_IS_BOOLEAN_TRUE(v) ((v)->ui[DUK_DBL_IDX_UI0] == DUK_XTAG_BOOLEAN_TRUE)
  6083. #define DUK_TVAL_IS_BOOLEAN_FALSE(v) ((v)->ui[DUK_DBL_IDX_UI0] == DUK_XTAG_BOOLEAN_FALSE)
  6084. #define DUK_TVAL_IS_LIGHTFUNC(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_LIGHTFUNC)
  6085. #define DUK_TVAL_IS_STRING(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_STRING)
  6086. #define DUK_TVAL_IS_OBJECT(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_OBJECT)
  6087. #define DUK_TVAL_IS_BUFFER(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_BUFFER)
  6088. #define DUK_TVAL_IS_POINTER(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_POINTER)
  6089. #if defined(DUK_USE_FASTINT)
  6090. /* 0xfff0 is -Infinity */
  6091. #define DUK_TVAL_IS_DOUBLE(v) (DUK_TVAL_GET_TAG((v)) <= 0xfff0UL)
  6092. #define DUK_TVAL_IS_FASTINT(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_FASTINT)
  6093. #define DUK_TVAL_IS_NUMBER(v) (DUK_TVAL_GET_TAG((v)) <= 0xfff1UL)
  6094. #else
  6095. #define DUK_TVAL_IS_NUMBER(v) (DUK_TVAL_GET_TAG((v)) <= 0xfff0UL)
  6096. #define DUK_TVAL_IS_DOUBLE(v) DUK_TVAL_IS_NUMBER((v))
  6097. #endif
  6098. /* This is performance critical because it appears in every DECREF. */
  6099. #define DUK_TVAL_IS_HEAP_ALLOCATED(v) (DUK_TVAL_GET_TAG((v)) >= DUK_TAG_STRING)
  6100. #if defined(DUK_USE_FASTINT)
  6101. DUK_INTERNAL_DECL duk_double_t duk_tval_get_number_packed(duk_tval *tv);
  6102. #endif
  6103. #else /* DUK_USE_PACKED_TVAL */
  6104. /* ======================================================================== */
  6105. /*
  6106. * Portable 12-byte representation
  6107. */
  6108. /* Note: not initializing all bytes is normally not an issue: Duktape won't
  6109. * read or use the uninitialized bytes so valgrind won't issue warnings.
  6110. * In some special cases a harmless valgrind warning may be issued though.
  6111. * For example, the DumpHeap debugger command writes out a compiled function's
  6112. * 'data' area as is, including any uninitialized bytes, which causes a
  6113. * valgrind warning.
  6114. */
  6115. typedef struct duk_tval_struct duk_tval;
  6116. struct duk_tval_struct {
  6117. duk_small_uint_t t;
  6118. duk_small_uint_t v_extra;
  6119. union {
  6120. duk_double_t d;
  6121. duk_small_int_t i;
  6122. #if defined(DUK_USE_FASTINT)
  6123. duk_int64_t fi; /* if present, forces 16-byte duk_tval */
  6124. #endif
  6125. void *voidptr;
  6126. duk_hstring *hstring;
  6127. duk_hobject *hobject;
  6128. duk_hcompiledfunction *hcompiledfunction;
  6129. duk_hnativefunction *hnativefunction;
  6130. duk_hthread *hthread;
  6131. duk_hbuffer *hbuffer;
  6132. duk_heaphdr *heaphdr;
  6133. duk_c_function lightfunc;
  6134. } v;
  6135. };
  6136. #define DUK__TAG_NUMBER 0 /* not exposed */
  6137. #if defined(DUK_USE_FASTINT)
  6138. #define DUK_TAG_FASTINT 1
  6139. #endif
  6140. #define DUK_TAG_UNDEFINED 2
  6141. #define DUK_TAG_NULL 3
  6142. #define DUK_TAG_BOOLEAN 4
  6143. #define DUK_TAG_POINTER 5
  6144. #define DUK_TAG_LIGHTFUNC 6
  6145. #define DUK_TAG_UNUSED 7 /* marker; not actual tagged type */
  6146. #define DUK_TAG_STRING 8 /* first heap allocated, match bit boundary */
  6147. #define DUK_TAG_OBJECT 9
  6148. #define DUK_TAG_BUFFER 10
  6149. /* DUK__TAG_NUMBER is intentionally first, as it is the default clause in code
  6150. * to support the 8-byte representation. Further, it is a non-heap-allocated
  6151. * type so it should come before DUK_TAG_STRING. Finally, it should not break
  6152. * the tag value ranges covered by case-clauses in a switch-case.
  6153. */
  6154. /* setters */
  6155. #define DUK_TVAL_SET_UNDEFINED(tv) do { \
  6156. (tv)->t = DUK_TAG_UNDEFINED; \
  6157. } while (0)
  6158. #define DUK_TVAL_SET_UNUSED(tv) do { \
  6159. (tv)->t = DUK_TAG_UNUSED; \
  6160. } while (0)
  6161. #define DUK_TVAL_SET_NULL(tv) do { \
  6162. (tv)->t = DUK_TAG_NULL; \
  6163. } while (0)
  6164. #define DUK_TVAL_SET_BOOLEAN(tv,val) do { \
  6165. (tv)->t = DUK_TAG_BOOLEAN; \
  6166. (tv)->v.i = (val); \
  6167. } while (0)
  6168. #if defined(DUK_USE_FASTINT)
  6169. #define DUK_TVAL_SET_DOUBLE(tv,val) do { \
  6170. (tv)->t = DUK__TAG_NUMBER; \
  6171. (tv)->v.d = (val); \
  6172. } while (0)
  6173. #define DUK_TVAL_SET_FASTINT(tv,val) do { \
  6174. (tv)->t = DUK_TAG_FASTINT; \
  6175. (tv)->v.fi = (val); \
  6176. } while (0)
  6177. #define DUK_TVAL_SET_FASTINT_U32(tv,val) do { \
  6178. (tv)->t = DUK_TAG_FASTINT; \
  6179. (tv)->v.fi = (duk_int64_t) (val); \
  6180. } while (0)
  6181. #define DUK_TVAL_SET_FASTINT_I32(tv,val) do { \
  6182. (tv)->t = DUK_TAG_FASTINT; \
  6183. (tv)->v.fi = (duk_int64_t) (val); \
  6184. } while (0)
  6185. #define DUK_TVAL_SET_NUMBER_CHKFAST(tv,d) \
  6186. duk_tval_set_number_chkfast((tv), (d))
  6187. #define DUK_TVAL_SET_NUMBER(tv,val) \
  6188. DUK_TVAL_SET_DOUBLE((tv), (val))
  6189. #define DUK_TVAL_CHKFAST_INPLACE(v) do { \
  6190. duk_tval *duk__tv; \
  6191. duk_double_t duk__d; \
  6192. duk__tv = (v); \
  6193. if (DUK_TVAL_IS_DOUBLE(duk__tv)) { \
  6194. duk__d = DUK_TVAL_GET_DOUBLE(duk__tv); \
  6195. DUK_TVAL_SET_NUMBER_CHKFAST(duk__tv, duk__d); \
  6196. } \
  6197. } while (0)
  6198. #else
  6199. #define DUK_TVAL_SET_NUMBER(tv,val) do { \
  6200. (tv)->t = DUK__TAG_NUMBER; \
  6201. (tv)->v.d = (val); \
  6202. } while (0)
  6203. #define DUK_TVAL_SET_NUMBER_CHKFAST(tv,d) \
  6204. DUK_TVAL_SET_NUMBER((tv), (d))
  6205. #define DUK_TVAL_SET_DOUBLE(v,d) \
  6206. DUK_TVAL_SET_NUMBER((tv), (d))
  6207. #define DUK_TVAL_CHKFAST_INPLACE(v) do { } while (0)
  6208. #endif /* DUK_USE_FASTINT */
  6209. #define DUK_TVAL_SET_POINTER(tv,hptr) do { \
  6210. (tv)->t = DUK_TAG_POINTER; \
  6211. (tv)->v.voidptr = (hptr); \
  6212. } while (0)
  6213. #define DUK_TVAL_SET_LIGHTFUNC(tv,fp,flags) do { \
  6214. (tv)->t = DUK_TAG_LIGHTFUNC; \
  6215. (tv)->v_extra = (flags); \
  6216. (tv)->v.lightfunc = (duk_c_function) (fp); \
  6217. } while (0)
  6218. #define DUK_TVAL_SET_STRING(tv,hptr) do { \
  6219. (tv)->t = DUK_TAG_STRING; \
  6220. (tv)->v.hstring = (hptr); \
  6221. } while (0)
  6222. #define DUK_TVAL_SET_OBJECT(tv,hptr) do { \
  6223. (tv)->t = DUK_TAG_OBJECT; \
  6224. (tv)->v.hobject = (hptr); \
  6225. } while (0)
  6226. #define DUK_TVAL_SET_BUFFER(tv,hptr) do { \
  6227. (tv)->t = DUK_TAG_BUFFER; \
  6228. (tv)->v.hbuffer = (hptr); \
  6229. } while (0)
  6230. #define DUK_TVAL_SET_NAN(tv) do { \
  6231. /* in non-packed representation we don't care about which NaN is used */ \
  6232. (tv)->t = DUK__TAG_NUMBER; \
  6233. (tv)->v.d = DUK_DOUBLE_NAN; \
  6234. } while (0)
  6235. #define DUK_TVAL_SET_TVAL(v,x) do { *(v) = *(x); } while (0)
  6236. /* getters */
  6237. #define DUK_TVAL_GET_BOOLEAN(tv) ((tv)->v.i)
  6238. #if defined(DUK_USE_FASTINT)
  6239. #define DUK_TVAL_GET_DOUBLE(tv) ((tv)->v.d)
  6240. #define DUK_TVAL_GET_FASTINT(tv) ((tv)->v.fi)
  6241. #define DUK_TVAL_GET_FASTINT_U32(tv) ((duk_uint32_t) ((tv)->v.fi))
  6242. #define DUK_TVAL_GET_FASTINT_I32(tv) ((duk_int32_t) ((tv)->v.fi))
  6243. #if 0
  6244. #define DUK_TVAL_GET_NUMBER(tv) (DUK_TVAL_IS_FASTINT((tv)) ? \
  6245. (duk_double_t) DUK_TVAL_GET_FASTINT((tv)) : \
  6246. DUK_TVAL_GET_DOUBLE((tv)))
  6247. #define DUK_TVAL_GET_NUMBER(tv) duk_tval_get_number_unpacked((tv))
  6248. #else
  6249. /* This seems reasonable overall. */
  6250. #define DUK_TVAL_GET_NUMBER(tv) (DUK_TVAL_IS_FASTINT((tv)) ? \
  6251. duk_tval_get_number_unpacked_fastint((tv)) : \
  6252. DUK_TVAL_GET_DOUBLE((tv)))
  6253. #endif
  6254. #else
  6255. #define DUK_TVAL_GET_NUMBER(tv) ((tv)->v.d)
  6256. #define DUK_TVAL_GET_DOUBLE(tv) ((tv)->v.d)
  6257. #endif /* DUK_USE_FASTINT */
  6258. #define DUK_TVAL_GET_POINTER(tv) ((tv)->v.voidptr)
  6259. #define DUK_TVAL_GET_LIGHTFUNC(tv,out_fp,out_flags) do { \
  6260. (out_flags) = (duk_uint32_t) (tv)->v_extra; \
  6261. (out_fp) = (tv)->v.lightfunc; \
  6262. } while (0)
  6263. #define DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv) ((tv)->v.lightfunc)
  6264. #define DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv) ((duk_uint32_t) ((tv)->v_extra))
  6265. #define DUK_TVAL_GET_STRING(tv) ((tv)->v.hstring)
  6266. #define DUK_TVAL_GET_OBJECT(tv) ((tv)->v.hobject)
  6267. #define DUK_TVAL_GET_BUFFER(tv) ((tv)->v.hbuffer)
  6268. #define DUK_TVAL_GET_HEAPHDR(tv) ((tv)->v.heaphdr)
  6269. /* decoding */
  6270. #define DUK_TVAL_GET_TAG(tv) ((tv)->t)
  6271. #define DUK_TVAL_IS_UNDEFINED(tv) ((tv)->t == DUK_TAG_UNDEFINED)
  6272. #define DUK_TVAL_IS_UNUSED(tv) ((tv)->t == DUK_TAG_UNUSED)
  6273. #define DUK_TVAL_IS_NULL(tv) ((tv)->t == DUK_TAG_NULL)
  6274. #define DUK_TVAL_IS_BOOLEAN(tv) ((tv)->t == DUK_TAG_BOOLEAN)
  6275. #define DUK_TVAL_IS_BOOLEAN_TRUE(tv) (((tv)->t == DUK_TAG_BOOLEAN) && ((tv)->v.i != 0))
  6276. #define DUK_TVAL_IS_BOOLEAN_FALSE(tv) (((tv)->t == DUK_TAG_BOOLEAN) && ((tv)->v.i == 0))
  6277. #if defined(DUK_USE_FASTINT)
  6278. #define DUK_TVAL_IS_DOUBLE(tv) ((tv)->t == DUK__TAG_NUMBER)
  6279. #define DUK_TVAL_IS_FASTINT(tv) ((tv)->t == DUK_TAG_FASTINT)
  6280. #define DUK_TVAL_IS_NUMBER(tv) ((tv)->t == DUK__TAG_NUMBER || \
  6281. (tv)->t == DUK_TAG_FASTINT)
  6282. #else
  6283. #define DUK_TVAL_IS_NUMBER(tv) ((tv)->t == DUK__TAG_NUMBER)
  6284. #define DUK_TVAL_IS_DOUBLE(v) DUK_TVAL_IS_NUMBER((v))
  6285. #endif /* DUK_USE_FASTINT */
  6286. #define DUK_TVAL_IS_POINTER(tv) ((tv)->t == DUK_TAG_POINTER)
  6287. #define DUK_TVAL_IS_LIGHTFUNC(tv) ((tv)->t == DUK_TAG_LIGHTFUNC)
  6288. #define DUK_TVAL_IS_STRING(tv) ((tv)->t == DUK_TAG_STRING)
  6289. #define DUK_TVAL_IS_OBJECT(tv) ((tv)->t == DUK_TAG_OBJECT)
  6290. #define DUK_TVAL_IS_BUFFER(tv) ((tv)->t == DUK_TAG_BUFFER)
  6291. /* This is performance critical because it's needed for every DECREF.
  6292. * Take advantage of the fact that the first heap allocated tag is 8,
  6293. * so that bit 3 is set for all heap allocated tags (and never set for
  6294. * non-heap-allocated tags).
  6295. */
  6296. #if 0
  6297. #define DUK_TVAL_IS_HEAP_ALLOCATED(tv) ((tv)->t >= DUK_TAG_STRING)
  6298. #endif
  6299. #define DUK_TVAL_IS_HEAP_ALLOCATED(tv) ((tv)->t & 0x08)
  6300. #if defined(DUK_USE_FASTINT)
  6301. #if 0
  6302. DUK_INTERNAL_DECL duk_double_t duk_tval_get_number_unpacked(duk_tval *tv);
  6303. #endif
  6304. DUK_INTERNAL_DECL duk_double_t duk_tval_get_number_unpacked_fastint(duk_tval *tv);
  6305. #endif
  6306. #endif /* DUK_USE_PACKED_TVAL */
  6307. /*
  6308. * Convenience (independent of representation)
  6309. */
  6310. #define DUK_TVAL_SET_BOOLEAN_TRUE(v) DUK_TVAL_SET_BOOLEAN(v, 1)
  6311. #define DUK_TVAL_SET_BOOLEAN_FALSE(v) DUK_TVAL_SET_BOOLEAN(v, 0)
  6312. /* Lightfunc flags packing and unpacking. */
  6313. /* Sign extend: 0x0000##00 -> 0x##000000 -> sign extend to 0xssssss## */
  6314. #define DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags) \
  6315. ((((duk_int32_t) (lf_flags)) << 16) >> 24)
  6316. #define DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags) \
  6317. (((lf_flags) >> 4) & 0x0f)
  6318. #define DUK_LFUNC_FLAGS_GET_NARGS(lf_flags) \
  6319. ((lf_flags) & 0x0f)
  6320. #define DUK_LFUNC_FLAGS_PACK(magic,length,nargs) \
  6321. (((magic) & 0xff) << 8) | ((length) << 4) | (nargs)
  6322. #define DUK_LFUNC_NARGS_VARARGS 0x0f /* varargs marker */
  6323. #define DUK_LFUNC_NARGS_MIN 0x00
  6324. #define DUK_LFUNC_NARGS_MAX 0x0e /* max, excl. varargs marker */
  6325. #define DUK_LFUNC_LENGTH_MIN 0x00
  6326. #define DUK_LFUNC_LENGTH_MAX 0x0f
  6327. #define DUK_LFUNC_MAGIC_MIN (-0x80)
  6328. #define DUK_LFUNC_MAGIC_MAX 0x7f
  6329. /* fastint constants etc */
  6330. #if defined(DUK_USE_FASTINT)
  6331. #define DUK_FASTINT_MIN (-0x800000000000LL)
  6332. #define DUK_FASTINT_MAX 0x7fffffffffffLL
  6333. #define DUK_FASTINT_BITS 48
  6334. DUK_INTERNAL_DECL void duk_tval_set_number_chkfast(duk_tval *tv, duk_double_t x);
  6335. #endif
  6336. #endif /* DUK_TVAL_H_INCLUDED */
  6337. #line 1 "duk_heaphdr.h"
  6338. /*
  6339. * Heap header definition and assorted macros, including ref counting.
  6340. * Access all fields through the accessor macros.
  6341. */
  6342. #ifndef DUK_HEAPHDR_H_INCLUDED
  6343. #define DUK_HEAPHDR_H_INCLUDED
  6344. /*
  6345. * Common heap header
  6346. *
  6347. * All heap objects share the same flags and refcount fields. Objects other
  6348. * than strings also need to have a single or double linked list pointers
  6349. * for insertion into the "heap allocated" list. Strings are held in the
  6350. * heap-wide string table so they don't need link pointers.
  6351. *
  6352. * Technically, 'h_refcount' must be wide enough to guarantee that it cannot
  6353. * wrap (otherwise objects might be freed incorrectly after wrapping). This
  6354. * means essentially that the refcount field must be as wide as data pointers.
  6355. * On 64-bit platforms this means that the refcount needs to be 64 bits even
  6356. * if an 'int' is 32 bits. This is a bit unfortunate, and compromising on
  6357. * this might be reasonable in the future.
  6358. *
  6359. * Heap header size on 32-bit platforms: 8 bytes without reference counting,
  6360. * 16 bytes with reference counting.
  6361. */
  6362. struct duk_heaphdr {
  6363. duk_uint32_t h_flags;
  6364. #if defined(DUK_USE_REFERENCE_COUNTING)
  6365. #if defined(DUK_USE_REFCOUNT16)
  6366. duk_uint16_t h_refcount16;
  6367. #else
  6368. duk_size_t h_refcount;
  6369. #endif
  6370. #endif
  6371. #if defined(DUK_USE_HEAPPTR16)
  6372. duk_uint16_t h_next16;
  6373. #else
  6374. duk_heaphdr *h_next;
  6375. #endif
  6376. #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
  6377. /* refcounting requires direct heap frees, which in turn requires a dual linked heap */
  6378. #if defined(DUK_USE_HEAPPTR16)
  6379. duk_uint16_t h_prev16;
  6380. #else
  6381. duk_heaphdr *h_prev;
  6382. #endif
  6383. #endif
  6384. /* When DUK_USE_HEAPPTR16 (and DUK_USE_REFCOUNT16) is in use, the
  6385. * struct won't align nicely to 4 bytes. This 16-bit extra field
  6386. * is added to make the alignment clean; the field can be used by
  6387. * heap objects when 16-bit packing is used. This field is now
  6388. * conditional to DUK_USE_HEAPPTR16 only, but it is intended to be
  6389. * used with DUK_USE_REFCOUNT16 and DUK_USE_DOUBLE_LINKED_HEAP;
  6390. * this only matter to low memory environments anyway.
  6391. */
  6392. #if defined(DUK_USE_HEAPPTR16)
  6393. duk_uint16_t h_extra16;
  6394. #endif
  6395. };
  6396. struct duk_heaphdr_string {
  6397. /* 16 bits would be enough for shared heaphdr flags and duk_hstring
  6398. * flags. The initial parts of duk_heaphdr_string and duk_heaphdr
  6399. * must match so changing the flags field size here would be quite
  6400. * awkward. However, to minimize struct size, we can pack at least
  6401. * 16 bits of duk_hstring data into the flags field.
  6402. */
  6403. duk_uint32_t h_flags;
  6404. #if defined(DUK_USE_REFERENCE_COUNTING)
  6405. #if defined(DUK_USE_REFCOUNT16)
  6406. duk_uint16_t h_refcount16;
  6407. #else
  6408. duk_size_t h_refcount;
  6409. #endif
  6410. #endif
  6411. };
  6412. #define DUK_HEAPHDR_FLAGS_TYPE_MASK 0x00000003UL
  6413. #define DUK_HEAPHDR_FLAGS_FLAG_MASK (~DUK_HEAPHDR_FLAGS_TYPE_MASK)
  6414. /* 2 bits for heap type */
  6415. #define DUK_HEAPHDR_FLAGS_HEAP_START 2 /* 4 heap flags */
  6416. #define DUK_HEAPHDR_FLAGS_USER_START 6 /* 26 user flags */
  6417. #define DUK_HEAPHDR_HEAP_FLAG_NUMBER(n) (DUK_HEAPHDR_FLAGS_HEAP_START + (n))
  6418. #define DUK_HEAPHDR_USER_FLAG_NUMBER(n) (DUK_HEAPHDR_FLAGS_USER_START + (n))
  6419. #define DUK_HEAPHDR_HEAP_FLAG(n) (1UL << (DUK_HEAPHDR_FLAGS_HEAP_START + (n)))
  6420. #define DUK_HEAPHDR_USER_FLAG(n) (1UL << (DUK_HEAPHDR_FLAGS_USER_START + (n)))
  6421. #define DUK_HEAPHDR_FLAG_REACHABLE DUK_HEAPHDR_HEAP_FLAG(0) /* mark-and-sweep: reachable */
  6422. #define DUK_HEAPHDR_FLAG_TEMPROOT DUK_HEAPHDR_HEAP_FLAG(1) /* mark-and-sweep: children not processed */
  6423. #define DUK_HEAPHDR_FLAG_FINALIZABLE DUK_HEAPHDR_HEAP_FLAG(2) /* mark-and-sweep: finalizable (on current pass) */
  6424. #define DUK_HEAPHDR_FLAG_FINALIZED DUK_HEAPHDR_HEAP_FLAG(3) /* mark-and-sweep: finalized (on previous pass) */
  6425. #define DUK_HTYPE_MIN 1
  6426. #define DUK_HTYPE_STRING 1
  6427. #define DUK_HTYPE_OBJECT 2
  6428. #define DUK_HTYPE_BUFFER 3
  6429. #define DUK_HTYPE_MAX 3
  6430. #if defined(DUK_USE_HEAPPTR16)
  6431. #define DUK_HEAPHDR_GET_NEXT(heap,h) \
  6432. ((duk_heaphdr *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->h_next16))
  6433. #define DUK_HEAPHDR_SET_NEXT(heap,h,val) do { \
  6434. (h)->h_next16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) val); \
  6435. } while (0)
  6436. #else
  6437. #define DUK_HEAPHDR_GET_NEXT(heap,h) ((h)->h_next)
  6438. #define DUK_HEAPHDR_SET_NEXT(heap,h,val) do { \
  6439. (h)->h_next = (val); \
  6440. } while (0)
  6441. #endif
  6442. #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
  6443. #if defined(DUK_USE_HEAPPTR16)
  6444. #define DUK_HEAPHDR_GET_PREV(heap,h) \
  6445. ((duk_heaphdr *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->h_prev16))
  6446. #define DUK_HEAPHDR_SET_PREV(heap,h,val) do { \
  6447. (h)->h_prev16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (val)); \
  6448. } while (0)
  6449. #else
  6450. #define DUK_HEAPHDR_GET_PREV(heap,h) ((h)->h_prev)
  6451. #define DUK_HEAPHDR_SET_PREV(heap,h,val) do { \
  6452. (h)->h_prev = (val); \
  6453. } while (0)
  6454. #endif
  6455. #endif
  6456. #if defined(DUK_USE_REFERENCE_COUNTING)
  6457. #if defined(DUK_USE_REFCOUNT16)
  6458. #define DUK_HEAPHDR_GET_REFCOUNT(h) ((h)->h_refcount16)
  6459. #define DUK_HEAPHDR_SET_REFCOUNT(h,val) do { \
  6460. (h)->h_refcount16 = (val); \
  6461. } while (0)
  6462. #define DUK_HEAPHDR_PREINC_REFCOUNT(h) (++(h)->h_refcount16) /* result: updated refcount */
  6463. #define DUK_HEAPHDR_PREDEC_REFCOUNT(h) (--(h)->h_refcount16) /* result: updated refcount */
  6464. #else
  6465. #define DUK_HEAPHDR_GET_REFCOUNT(h) ((h)->h_refcount)
  6466. #define DUK_HEAPHDR_SET_REFCOUNT(h,val) do { \
  6467. (h)->h_refcount = (val); \
  6468. } while (0)
  6469. #define DUK_HEAPHDR_PREINC_REFCOUNT(h) (++(h)->h_refcount) /* result: updated refcount */
  6470. #define DUK_HEAPHDR_PREDEC_REFCOUNT(h) (--(h)->h_refcount) /* result: updated refcount */
  6471. #endif
  6472. #else
  6473. /* refcount macros not defined without refcounting, caller must #ifdef now */
  6474. #endif /* DUK_USE_REFERENCE_COUNTING */
  6475. /*
  6476. * Note: type is treated as a field separate from flags, so some masking is
  6477. * involved in the macros below.
  6478. */
  6479. #define DUK_HEAPHDR_GET_FLAGS_RAW(h) ((h)->h_flags)
  6480. #define DUK_HEAPHDR_GET_FLAGS(h) ((h)->h_flags & DUK_HEAPHDR_FLAGS_FLAG_MASK)
  6481. #define DUK_HEAPHDR_SET_FLAGS(h,val) do { \
  6482. (h)->h_flags = ((h)->h_flags & ~(DUK_HEAPHDR_FLAGS_FLAG_MASK)) | (val); \
  6483. } while (0)
  6484. #define DUK_HEAPHDR_GET_TYPE(h) ((h)->h_flags & DUK_HEAPHDR_FLAGS_TYPE_MASK)
  6485. #define DUK_HEAPHDR_SET_TYPE(h,val) do { \
  6486. (h)->h_flags = ((h)->h_flags & ~(DUK_HEAPHDR_FLAGS_TYPE_MASK)) | (val); \
  6487. } while (0)
  6488. #define DUK_HEAPHDR_HTYPE_VALID(h) ( \
  6489. DUK_HEAPHDR_GET_TYPE((h)) >= DUK_HTYPE_MIN && \
  6490. DUK_HEAPHDR_GET_TYPE((h)) <= DUK_HTYPE_MAX \
  6491. )
  6492. #define DUK_HEAPHDR_SET_TYPE_AND_FLAGS(h,tval,fval) do { \
  6493. (h)->h_flags = ((tval) & DUK_HEAPHDR_FLAGS_TYPE_MASK) | \
  6494. ((fval) & DUK_HEAPHDR_FLAGS_FLAG_MASK); \
  6495. } while (0)
  6496. #define DUK_HEAPHDR_SET_FLAG_BITS(h,bits) do { \
  6497. DUK_ASSERT(((bits) & ~(DUK_HEAPHDR_FLAGS_FLAG_MASK)) == 0); \
  6498. (h)->h_flags |= (bits); \
  6499. } while (0)
  6500. #define DUK_HEAPHDR_CLEAR_FLAG_BITS(h,bits) do { \
  6501. DUK_ASSERT(((bits) & ~(DUK_HEAPHDR_FLAGS_FLAG_MASK)) == 0); \
  6502. (h)->h_flags &= ~((bits)); \
  6503. } while (0)
  6504. #define DUK_HEAPHDR_CHECK_FLAG_BITS(h,bits) (((h)->h_flags & (bits)) != 0)
  6505. #define DUK_HEAPHDR_SET_REACHABLE(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_REACHABLE)
  6506. #define DUK_HEAPHDR_CLEAR_REACHABLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_REACHABLE)
  6507. #define DUK_HEAPHDR_HAS_REACHABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_REACHABLE)
  6508. #define DUK_HEAPHDR_SET_TEMPROOT(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_TEMPROOT)
  6509. #define DUK_HEAPHDR_CLEAR_TEMPROOT(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_TEMPROOT)
  6510. #define DUK_HEAPHDR_HAS_TEMPROOT(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_TEMPROOT)
  6511. #define DUK_HEAPHDR_SET_FINALIZABLE(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZABLE)
  6512. #define DUK_HEAPHDR_CLEAR_FINALIZABLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZABLE)
  6513. #define DUK_HEAPHDR_HAS_FINALIZABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZABLE)
  6514. #define DUK_HEAPHDR_SET_FINALIZED(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZED)
  6515. #define DUK_HEAPHDR_CLEAR_FINALIZED(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZED)
  6516. #define DUK_HEAPHDR_HAS_FINALIZED(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZED)
  6517. /* get or set a range of flags; m=first bit number, n=number of bits */
  6518. #define DUK_HEAPHDR_GET_FLAG_RANGE(h,m,n) (((h)->h_flags >> (m)) & ((1UL << (n)) - 1UL))
  6519. #define DUK_HEAPHDR_SET_FLAG_RANGE(h,m,n,v) do { \
  6520. (h)->h_flags = \
  6521. ((h)->h_flags & (~(((1 << (n)) - 1) << (m)))) \
  6522. | ((v) << (m)); \
  6523. } while (0)
  6524. /* init pointer fields to null */
  6525. #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
  6526. #define DUK_HEAPHDR_INIT_NULLS(h) do { \
  6527. DUK_HEAPHDR_SET_NEXT((h), (void *) NULL); \
  6528. DUK_HEAPHDR_SET_PREV((h), (void *) NULL); \
  6529. } while (0)
  6530. #else
  6531. #define DUK_HEAPHDR_INIT_NULLS(h) do { \
  6532. DUK_HEAPHDR_SET_NEXT((h), (void *) NULL); \
  6533. } while (0)
  6534. #endif
  6535. #define DUK_HEAPHDR_STRING_INIT_NULLS(h) /* currently nop */
  6536. /*
  6537. * Reference counting helper macros. The macros take a thread argument
  6538. * and must thus always be executed in a specific thread context. The
  6539. * thread argument is needed for features like finalization. Currently
  6540. * it is not required for INCREF, but it is included just in case.
  6541. *
  6542. * Note that 'raw' macros such as DUK_HEAPHDR_GET_REFCOUNT() are not
  6543. * defined without DUK_USE_REFERENCE_COUNTING, so caller must #ifdef
  6544. * around them.
  6545. */
  6546. #if defined(DUK_USE_REFERENCE_COUNTING)
  6547. /* Fast variants, inline refcount operations except for refzero handling.
  6548. * Can be used explicitly when speed is always more important than size.
  6549. * For a good compiler and a single file build, these are basically the
  6550. * same as a forced inline.
  6551. */
  6552. #define DUK_TVAL_INCREF_FAST(thr,tv) do { \
  6553. duk_tval *duk__tv = (tv); \
  6554. DUK_ASSERT(duk__tv != NULL); \
  6555. if (DUK_TVAL_IS_HEAP_ALLOCATED(duk__tv)) { \
  6556. duk_heaphdr *duk__h = DUK_TVAL_GET_HEAPHDR(duk__tv); \
  6557. DUK_ASSERT(duk__h != NULL); \
  6558. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \
  6559. DUK_HEAPHDR_PREINC_REFCOUNT(duk__h); \
  6560. } \
  6561. } while (0)
  6562. #define DUK_TVAL_DECREF_FAST(thr,tv) do { \
  6563. duk_tval *duk__tv = (tv); \
  6564. DUK_ASSERT(duk__tv != NULL); \
  6565. if (DUK_TVAL_IS_HEAP_ALLOCATED(duk__tv)) { \
  6566. duk_heaphdr *duk__h = DUK_TVAL_GET_HEAPHDR(duk__tv); \
  6567. DUK_ASSERT(duk__h != NULL); \
  6568. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \
  6569. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(duk__h) > 0); \
  6570. if (DUK_HEAPHDR_PREDEC_REFCOUNT(duk__h) == 0) { \
  6571. duk_heaphdr_refzero((thr), duk__h); \
  6572. } \
  6573. } \
  6574. } while (0)
  6575. #define DUK_HEAPHDR_INCREF_FAST(thr,h) do { \
  6576. duk_heaphdr *duk__h = (duk_heaphdr *) (h); \
  6577. DUK_ASSERT(duk__h != NULL); \
  6578. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \
  6579. DUK_HEAPHDR_PREINC_REFCOUNT(duk__h); \
  6580. } while (0)
  6581. #define DUK_HEAPHDR_DECREF_FAST(thr,h) do { \
  6582. duk_heaphdr *duk__h = (duk_heaphdr *) (h); \
  6583. DUK_ASSERT(duk__h != NULL); \
  6584. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \
  6585. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(duk__h) > 0); \
  6586. if (DUK_HEAPHDR_PREDEC_REFCOUNT(duk__h) == 0) { \
  6587. duk_heaphdr_refzero((thr), duk__h); \
  6588. } \
  6589. } while (0)
  6590. /* Slow variants, call to a helper to reduce code size.
  6591. * Can be used explicitly when size is always more important than speed.
  6592. */
  6593. #define DUK_TVAL_INCREF_SLOW(thr,tv) do { \
  6594. duk_tval_incref((tv)); \
  6595. } while (0)
  6596. #define DUK_TVAL_DECREF_SLOW(thr,tv) do { \
  6597. duk_tval_decref((thr), (tv)); \
  6598. } while (0)
  6599. #define DUK_HEAPHDR_INCREF_SLOW(thr,h) do { \
  6600. duk_heaphdr_incref((duk_heaphdr *) (h)); \
  6601. } while (0)
  6602. #define DUK_HEAPHDR_DECREF_SLOW(thr,h) do { \
  6603. duk_heaphdr_decref((thr), (duk_heaphdr *) (h)); \
  6604. } while (0)
  6605. /* Default variants. Selection depends on speed/size preference.
  6606. * Concretely: with gcc 4.8.1 -Os x64 the difference in final binary
  6607. * is about +1kB for _FAST variants.
  6608. */
  6609. #if defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  6610. #define DUK_TVAL_INCREF(thr,tv) DUK_TVAL_INCREF_FAST((thr),(tv))
  6611. #define DUK_TVAL_DECREF(thr,tv) DUK_TVAL_DECREF_FAST((thr),(tv))
  6612. #define DUK_HEAPHDR_INCREF(thr,h) DUK_HEAPHDR_INCREF_FAST((thr),(h))
  6613. #define DUK_HEAPHDR_DECREF(thr,h) DUK_HEAPHDR_DECREF_FAST((thr),(h))
  6614. #else
  6615. #define DUK_TVAL_INCREF(thr,tv) DUK_TVAL_INCREF_SLOW((thr),(tv))
  6616. #define DUK_TVAL_DECREF(thr,tv) DUK_TVAL_DECREF_SLOW((thr),(tv))
  6617. #define DUK_HEAPHDR_INCREF(thr,h) DUK_HEAPHDR_INCREF_SLOW((thr),(h))
  6618. #define DUK_HEAPHDR_DECREF(thr,h) DUK_HEAPHDR_DECREF_SLOW((thr),(h))
  6619. #endif
  6620. /* Casting convenience. */
  6621. #define DUK_HSTRING_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h))
  6622. #define DUK_HSTRING_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) (h))
  6623. #define DUK_HOBJECT_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h))
  6624. #define DUK_HOBJECT_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) (h))
  6625. #define DUK_HBUFFER_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h))
  6626. #define DUK_HBUFFER_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) (h))
  6627. #define DUK_HCOMPILEDFUNCTION_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj)
  6628. #define DUK_HCOMPILEDFUNCTION_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) &(h)->obj)
  6629. #define DUK_HNATIVEFUNCTION_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj)
  6630. #define DUK_HNATIVEFUNCTION_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) &(h)->obj)
  6631. #define DUK_HBUFFEROBJECT_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj)
  6632. #define DUK_HBUFFEROBJECT_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) &(h)->obj)
  6633. #define DUK_HTHREAD_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj)
  6634. #define DUK_HTHREAD_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) &(h)->obj)
  6635. /* Convenience for some situations; the above macros don't allow NULLs
  6636. * for performance reasons.
  6637. */
  6638. #define DUK_HOBJECT_INCREF_ALLOWNULL(thr,h) do { \
  6639. if ((h) != NULL) { \
  6640. DUK_HEAPHDR_INCREF((thr), (duk_heaphdr *) (h)); \
  6641. } \
  6642. } while (0)
  6643. #define DUK_HOBJECT_DECREF_ALLOWNULL(thr,h) do { \
  6644. if ((h) != NULL) { \
  6645. DUK_HEAPHDR_DECREF((thr), (duk_heaphdr *) (h)); \
  6646. } \
  6647. } while (0)
  6648. /*
  6649. * Macros to set a duk_tval and update refcount of the target (decref the
  6650. * old value and incref the new value if necessary). This is both performance
  6651. * and footprint critical; any changes made should be measured for size/speed.
  6652. */
  6653. #define DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0(thr,tvptr_dst) do { \
  6654. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  6655. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6656. DUK_TVAL_SET_UNDEFINED(tv__dst); \
  6657. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6658. } while (0)
  6659. #define DUK_TVAL_SET_UNUSED_UPDREF_ALT0(thr,tvptr_dst) do { \
  6660. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  6661. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6662. DUK_TVAL_SET_UNUSED(tv__dst); \
  6663. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6664. } while (0)
  6665. #define DUK_TVAL_SET_NULL_UPDREF_ALT0(thr,tvptr_dst) do { \
  6666. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  6667. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6668. DUK_TVAL_SET_NULL(tv__dst); \
  6669. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6670. } while (0)
  6671. #define DUK_TVAL_SET_BOOLEAN_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6672. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  6673. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6674. DUK_TVAL_SET_BOOLEAN(tv__dst, (newval)); \
  6675. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6676. } while (0)
  6677. #define DUK_TVAL_SET_NUMBER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6678. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  6679. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6680. DUK_TVAL_SET_NUMBER(tv__dst, (newval)); \
  6681. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6682. } while (0)
  6683. #define DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6684. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  6685. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6686. DUK_TVAL_SET_NUMBER_CHKFAST(tv__dst, (newval)); \
  6687. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6688. } while (0)
  6689. #define DUK_TVAL_SET_DOUBLE_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6690. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  6691. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6692. DUK_TVAL_SET_DOUBLE(tv__dst, (newval)); \
  6693. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6694. } while (0)
  6695. #define DUK_TVAL_SET_NAN_UPDREF_ALT0(thr,tvptr_dst) do { \
  6696. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  6697. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6698. DUK_TVAL_SET_NAN(tv__dst); \
  6699. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6700. } while (0)
  6701. #if defined(DUK_USE_FASTINT)
  6702. #define DUK_TVAL_SET_FASTINT_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6703. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  6704. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6705. DUK_TVAL_SET_FASTINT(tv__dst, (newval)); \
  6706. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6707. } while (0)
  6708. #define DUK_TVAL_SET_FASTINT_I32_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6709. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  6710. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6711. DUK_TVAL_SET_FASTINT_I32(tv__dst, (newval)); \
  6712. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6713. } while (0)
  6714. #define DUK_TVAL_SET_FASTINT_U32_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6715. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  6716. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6717. DUK_TVAL_SET_FASTINT_U32(tv__dst, (newval)); \
  6718. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6719. } while (0)
  6720. #endif /* DUK_USE_FASTINT */
  6721. #define DUK_TVAL_SET_LIGHTFUNC_UPDREF_ALT0(thr,tvptr_dst,lf_v,lf_fp,lf_flags) do { \
  6722. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  6723. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6724. DUK_TVAL_SET_LIGHTFUNC(tv__dst, (lf_v), (lf_fp), (lf_flags)); \
  6725. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6726. } while (0)
  6727. #define DUK_TVAL_SET_STRING_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6728. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  6729. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6730. DUK_TVAL_SET_STRING(tv__dst, (newval)); \
  6731. DUK_HSTRING_INCREF((thr), (newval)); \
  6732. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6733. } while (0)
  6734. #define DUK_TVAL_SET_OBJECT_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6735. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  6736. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6737. DUK_TVAL_SET_OBJECT(tv__dst, (newval)); \
  6738. DUK_HOBJECT_INCREF((thr), (newval)); \
  6739. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6740. } while (0)
  6741. #define DUK_TVAL_SET_BUFFER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6742. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  6743. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6744. DUK_TVAL_SET_BUFFER(tv__dst, (newval)); \
  6745. DUK_HBUFFER_INCREF((thr), (newval)); \
  6746. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6747. } while (0)
  6748. #define DUK_TVAL_SET_POINTER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6749. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  6750. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6751. DUK_TVAL_SET_POINTER(tv__dst, (newval)); \
  6752. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6753. } while (0)
  6754. /* DUK_TVAL_SET_TVAL_UPDREF() is used a lot in executor, property lookups,
  6755. * etc, so it's very important for performance.
  6756. *
  6757. * NOTE: the source and destination duk_tval pointers may be the same, and
  6758. * the macros MUST deal with that correctly.
  6759. */
  6760. /* Original idiom used. */
  6761. #define DUK_TVAL_SET_TVAL_UPDREF_ALT0(thr,tvptr_dst,tvptr_src) do { \
  6762. duk_tval *tv__dst, *tv__src; duk_tval tv__tmp; \
  6763. tv__dst = (tvptr_dst); tv__src = (tvptr_src); \
  6764. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6765. DUK_TVAL_SET_TVAL(tv__dst, tv__src); \
  6766. DUK_TVAL_INCREF((thr), tv__src); \
  6767. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  6768. } while (0)
  6769. #if 0 /* XXX: to optimize and measure */
  6770. /* Original idiom but with forced fast refcount macros. */
  6771. #define DUK_TVAL_SET_TVAL_UPDREF_ALT0F(thr,tvptr_dst,tvptr_src) do { \
  6772. duk_tval *tv__dst, *tv__src; duk_tval tv__tmp; \
  6773. tv__dst = (tvptr_dst); tv__src = (tvptr_src); \
  6774. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  6775. DUK_TVAL_SET_TVAL(tv__dst, tv__src); \
  6776. DUK_TVAL_INCREF_FAST((thr), tv__src); \
  6777. DUK_TVAL_DECREF_FAST((thr), &tv__tmp); /* side effects */ \
  6778. } while (0)
  6779. /* Use 'h__obj' temporary to avoid a full duk_tval copy. */
  6780. #define DUK_TVAL_SET_TVAL_UPDREF_ALT1(thr,tvptr_dst,tvptr_src) do { \
  6781. duk_tval *tv__dst, *tv__src; duk_heaphdr *h__obj; \
  6782. tv__dst = (tvptr_dst); tv__src = (tvptr_src); \
  6783. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv__dst)) { \
  6784. h__obj = DUK_TVAL_GET_HEAPHDR(tv__dst); \
  6785. DUK_ASSERT(h__obj != NULL); \
  6786. } else { \
  6787. h__obj = NULL; \
  6788. } \
  6789. DUK_TVAL_SET_TVAL(tv__dst, tv__src); \
  6790. DUK_TVAL_INCREF((thr), tv__src); \
  6791. if (h__obj != NULL) { \
  6792. DUK_HEAPHDR_DECREF_FAST((thr), h__obj); /* side effects */ \
  6793. } \
  6794. } while (0)
  6795. /* Use 'h__obj' temporary to avoid a full duk_tval copy. */
  6796. #define DUK_TVAL_SET_TVAL_UPDREF_ALT1A(thr,tvptr_dst,tvptr_src) do { \
  6797. duk_tval *tv__dst, *tv__src; duk_heaphdr *h__obj; \
  6798. tv__dst = (tvptr_dst); tv__src = (tvptr_src); \
  6799. if (DUK_UNLIKELY(DUK_TVAL_IS_HEAP_ALLOCATED(tv__dst))) { \
  6800. h__obj = DUK_TVAL_GET_HEAPHDR(tv__dst); \
  6801. DUK_ASSERT(h__obj != NULL); \
  6802. } else { \
  6803. h__obj = NULL; \
  6804. } \
  6805. DUK_TVAL_SET_TVAL(tv__dst, tv__src); \
  6806. DUK_TVAL_INCREF_FAST((thr), tv__src); \
  6807. if (DUK_UNLIKELY(h__obj != NULL)) { \
  6808. DUK_HEAPHDR_DECREF_FAST((thr), h__obj); /* side effects */ \
  6809. } \
  6810. } while (0)
  6811. /* Avoid rechecking 'h__obj'. */
  6812. #define DUK_TVAL_SET_TVAL_UPDREF_ALT2(thr,tvptr_dst,tvptr_src) do { \
  6813. duk_tval *tv__dst, *tv__src; duk_heaphdr *h__obj; \
  6814. tv__dst = (tvptr_dst); tv__src = (tvptr_src); \
  6815. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv__dst)) { \
  6816. h__obj = DUK_TVAL_GET_HEAPHDR(tv__dst); \
  6817. DUK_ASSERT(h__obj != NULL); \
  6818. DUK_TVAL_SET_TVAL(tv__dst, tv__src); \
  6819. DUK_TVAL_INCREF((thr), tv__src); \
  6820. DUK_HEAPHDR_DECREF_FAST((thr), h__obj); /* side effects */ \
  6821. } else { \
  6822. DUK_TVAL_SET_TVAL(tv__dst, tv__src); \
  6823. DUK_TVAL_INCREF((thr), tv__src); \
  6824. } \
  6825. } while (0)
  6826. /* Avoid rechecking 'h__obj'. */
  6827. #define DUK_TVAL_SET_TVAL_UPDREF_ALT3(thr,tvptr_dst,tvptr_src) do { \
  6828. duk_tval *tv__dst, *tv__src; duk_heaphdr *h__obj; \
  6829. tv__dst = (tvptr_dst); tv__src = (tvptr_src); \
  6830. if (DUK_UNLIKELY(DUK_TVAL_IS_HEAP_ALLOCATED(tv__dst))) { \
  6831. h__obj = DUK_TVAL_GET_HEAPHDR(tv__dst); \
  6832. DUK_ASSERT(h__obj != NULL); \
  6833. DUK_TVAL_SET_TVAL(tv__dst, tv__src); \
  6834. DUK_TVAL_INCREF_FAST((thr), tv__src); \
  6835. DUK_HEAPHDR_DECREF_FAST((thr), h__obj); /* side effects */ \
  6836. } else { \
  6837. DUK_TVAL_SET_TVAL(tv__dst, tv__src); \
  6838. DUK_TVAL_INCREF_FAST((thr), tv__src); \
  6839. } \
  6840. } while (0)
  6841. #endif
  6842. /* XXX: no optimized variants yet */
  6843. #define DUK_TVAL_SET_UNDEFINED_UPDREF DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0
  6844. #define DUK_TVAL_SET_UNUSED_UPDREF DUK_TVAL_SET_UNUSED_UPDREF_ALT0
  6845. #define DUK_TVAL_SET_NULL_UPDREF DUK_TVAL_SET_NULL_UPDREF_ALT0
  6846. #define DUK_TVAL_SET_BOOLEAN_UPDREF DUK_TVAL_SET_BOOLEAN_UPDREF_ALT0
  6847. #define DUK_TVAL_SET_NUMBER_UPDREF DUK_TVAL_SET_NUMBER_UPDREF_ALT0
  6848. #define DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF_ALT0
  6849. #define DUK_TVAL_SET_DOUBLE_UPDREF DUK_TVAL_SET_DOUBLE_UPDREF_ALT0
  6850. #define DUK_TVAL_SET_NAN_UPDREF DUK_TVAL_SET_NAN_UPDREF_ALT0
  6851. #if defined(DUK_USE_FASTINT)
  6852. #define DUK_TVAL_SET_FASTINT_UPDREF DUK_TVAL_SET_FASTINT_UPDREF_ALT0
  6853. #define DUK_TVAL_SET_FASTINT_I32_UPDREF DUK_TVAL_SET_FASTINT_I32_UPDREF_ALT0
  6854. #define DUK_TVAL_SET_FASTINT_U32_UPDREF DUK_TVAL_SET_FASTINT_U32_UPDREF_ALT0
  6855. #endif /* DUK_USE_FASTINT */
  6856. #define DUK_TVAL_SET_LIGHTFUNC_UPDREF DUK_TVAL_SET_LIGHTFUNC_UPDREF_ALT0
  6857. #define DUK_TVAL_SET_STRING_UPDREF DUK_TVAL_SET_STRING_UPDREF_ALT0
  6858. #define DUK_TVAL_SET_OBJECT_UPDREF DUK_TVAL_SET_OBJECT_UPDREF_ALT0
  6859. #define DUK_TVAL_SET_BUFFER_UPDREF DUK_TVAL_SET_BUFFER_UPDREF_ALT0
  6860. #define DUK_TVAL_SET_POINTER_UPDREF DUK_TVAL_SET_POINTER_UPDREF_ALT0
  6861. #if defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  6862. /* Optimized for speed. */
  6863. #define DUK_TVAL_SET_TVAL_UPDREF DUK_TVAL_SET_TVAL_UPDREF_ALT0
  6864. #define DUK_TVAL_SET_TVAL_UPDREF_FAST DUK_TVAL_SET_TVAL_UPDREF_ALT0
  6865. #define DUK_TVAL_SET_TVAL_UPDREF_SLOW DUK_TVAL_SET_TVAL_UPDREF_ALT0
  6866. #else
  6867. /* Optimized for size. */
  6868. #define DUK_TVAL_SET_TVAL_UPDREF DUK_TVAL_SET_TVAL_UPDREF_ALT0
  6869. #define DUK_TVAL_SET_TVAL_UPDREF_FAST DUK_TVAL_SET_TVAL_UPDREF_ALT0
  6870. #define DUK_TVAL_SET_TVAL_UPDREF_SLOW DUK_TVAL_SET_TVAL_UPDREF_ALT0
  6871. #endif
  6872. #else /* DUK_USE_REFERENCE_COUNTING */
  6873. #define DUK_TVAL_INCREF_FAST(thr,v) do {} while (0) /* nop */
  6874. #define DUK_TVAL_DECREF_FAST(thr,v) do {} while (0) /* nop */
  6875. #define DUK_TVAL_INCREF_SLOW(thr,v) do {} while (0) /* nop */
  6876. #define DUK_TVAL_DECREF_SLOW(thr,v) do {} while (0) /* nop */
  6877. #define DUK_TVAL_INCREF(thr,v) do {} while (0) /* nop */
  6878. #define DUK_TVAL_DECREF(thr,v) do {} while (0) /* nop */
  6879. #define DUK_HEAPHDR_INCREF_FAST(thr,h) do {} while (0) /* nop */
  6880. #define DUK_HEAPHDR_DECREF_FAST(thr,h) do {} while (0) /* nop */
  6881. #define DUK_HEAPHDR_INCREF_SLOW(thr,h) do {} while (0) /* nop */
  6882. #define DUK_HEAPHDR_DECREF_SLOW(thr,h) do {} while (0) /* nop */
  6883. #define DUK_HEAPHDR_INCREF(thr,h) do {} while (0) /* nop */
  6884. #define DUK_HEAPHDR_DECREF(thr,h) do {} while (0) /* nop */
  6885. #define DUK_HSTRING_INCREF(thr,h) do {} while (0) /* nop */
  6886. #define DUK_HSTRING_DECREF(thr,h) do {} while (0) /* nop */
  6887. #define DUK_HOBJECT_INCREF(thr,h) do {} while (0) /* nop */
  6888. #define DUK_HOBJECT_DECREF(thr,h) do {} while (0) /* nop */
  6889. #define DUK_HBUFFER_INCREF(thr,h) do {} while (0) /* nop */
  6890. #define DUK_HBUFFER_DECREF(thr,h) do {} while (0) /* nop */
  6891. #define DUK_HCOMPILEDFUNCTION_INCREF(thr,h) do {} while (0) /* nop */
  6892. #define DUK_HCOMPILEDFUNCTION_DECREF(thr,h) do {} while (0) /* nop */
  6893. #define DUK_HNATIVEFUNCTION_INCREF(thr,h) do {} while (0) /* nop */
  6894. #define DUK_HNATIVEFUNCTION_DECREF(thr,h) do {} while (0) /* nop */
  6895. #define DUK_HBUFFEROBJECT_INCREF(thr,h) do {} while (0) /* nop */
  6896. #define DUK_HBUFFEROBJECT_DECREF(thr,h) do {} while (0) /* nop */
  6897. #define DUK_HTHREAD_INCREF(thr,h) do {} while (0) /* nop */
  6898. #define DUK_HTHREAD_DECREF(thr,h) do {} while (0) /* nop */
  6899. #define DUK_HOBJECT_INCREF_ALLOWNULL(thr,h) do {} while (0) /* nop */
  6900. #define DUK_HOBJECT_DECREF_ALLOWNULL(thr,h) do {} while (0) /* nop */
  6901. #define DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0(thr,tvptr_dst) do { \
  6902. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  6903. DUK_TVAL_SET_UNDEFINED(tv__dst); \
  6904. DUK_UNREF((thr)); \
  6905. } while (0)
  6906. #define DUK_TVAL_SET_UNUSED_UPDREF_ALT0(thr,tvptr_dst) do { \
  6907. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  6908. DUK_TVAL_SET_UNUSED(tv__dst); \
  6909. DUK_UNREF((thr)); \
  6910. } while (0)
  6911. #define DUK_TVAL_SET_NULL_UPDREF_ALT0(thr,tvptr_dst) do { \
  6912. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  6913. DUK_TVAL_SET_NULL(tv__dst); \
  6914. DUK_UNREF((thr)); \
  6915. } while (0)
  6916. #define DUK_TVAL_SET_BOOLEAN_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6917. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  6918. DUK_TVAL_SET_BOOLEAN(tv__dst, (newval)); \
  6919. DUK_UNREF((thr)); \
  6920. } while (0)
  6921. #define DUK_TVAL_SET_NUMBER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6922. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  6923. DUK_TVAL_SET_NUMBER(tv__dst, (newval)); \
  6924. DUK_UNREF((thr)); \
  6925. } while (0)
  6926. #define DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6927. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  6928. DUK_TVAL_SET_NUMBER_CHKFAST(tv__dst, (newval)); \
  6929. DUK_UNREF((thr)); \
  6930. } while (0)
  6931. #define DUK_TVAL_SET_DOUBLE_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6932. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  6933. DUK_TVAL_SET_DOUBLE(tv__dst, (newval)); \
  6934. DUK_UNREF((thr)); \
  6935. } while (0)
  6936. #define DUK_TVAL_SET_NAN_UPDREF_ALT0(thr,tvptr_dst) do { \
  6937. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  6938. DUK_TVAL_SET_NAN(tv__dst); \
  6939. DUK_UNREF((thr)); \
  6940. } while (0)
  6941. #if defined(DUK_USE_FASTINT)
  6942. #define DUK_TVAL_SET_FASTINT_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6943. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  6944. DUK_TVAL_SET_FASTINT(tv__dst, (newval)); \
  6945. DUK_UNREF((thr)); \
  6946. } while (0)
  6947. #define DUK_TVAL_SET_FASTINT_I32_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6948. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  6949. DUK_TVAL_SET_FASTINT_I32(tv__dst, (newval)); \
  6950. DUK_UNREF((thr)); \
  6951. } while (0)
  6952. #define DUK_TVAL_SET_FASTINT_U32_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6953. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  6954. DUK_TVAL_SET_FASTINT_U32(tv__dst, (newval)); \
  6955. DUK_UNREF((thr)); \
  6956. } while (0)
  6957. #endif /* DUK_USE_FASTINT */
  6958. #define DUK_TVAL_SET_LIGHTFUNC_UPDREF_ALT0(thr,tvptr_dst,lf_v,lf_fp,lf_flags) do { \
  6959. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  6960. DUK_TVAL_SET_LIGHTFUNC(tv__dst, (lf_v), (lf_fp), (lf_flags)); \
  6961. DUK_UNREF((thr)); \
  6962. } while (0)
  6963. #define DUK_TVAL_SET_STRING_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6964. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  6965. DUK_TVAL_SET_STRING(tv__dst, (newval)); \
  6966. DUK_UNREF((thr)); \
  6967. } while (0)
  6968. #define DUK_TVAL_SET_OBJECT_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6969. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  6970. DUK_TVAL_SET_OBJECT(tv__dst, (newval)); \
  6971. DUK_UNREF((thr)); \
  6972. } while (0)
  6973. #define DUK_TVAL_SET_BUFFER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6974. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  6975. DUK_TVAL_SET_BUFFER(tv__dst, (newval)); \
  6976. DUK_UNREF((thr)); \
  6977. } while (0)
  6978. #define DUK_TVAL_SET_POINTER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  6979. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  6980. DUK_TVAL_SET_POINTER(tv__dst, (newval)); \
  6981. DUK_UNREF((thr)); \
  6982. } while (0)
  6983. #define DUK_TVAL_SET_TVAL_UPDREF_ALT0(thr,tvptr_dst,tvptr_src) do { \
  6984. duk_tval *tv__dst, *tv__src; \
  6985. tv__dst = (tvptr_dst); tv__src = (tvptr_src); \
  6986. DUK_TVAL_SET_TVAL(tv__dst, tv__src); \
  6987. DUK_UNREF((thr)); \
  6988. } while (0)
  6989. #define DUK_TVAL_SET_UNDEFINED_UPDREF DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0
  6990. #define DUK_TVAL_SET_UNUSED_UPDREF DUK_TVAL_SET_UNUSED_UPDREF_ALT0
  6991. #define DUK_TVAL_SET_NULL_UPDREF DUK_TVAL_SET_NULL_UPDREF_ALT0
  6992. #define DUK_TVAL_SET_BOOLEAN_UPDREF DUK_TVAL_SET_BOOLEAN_UPDREF_ALT0
  6993. #define DUK_TVAL_SET_NUMBER_UPDREF DUK_TVAL_SET_NUMBER_UPDREF_ALT0
  6994. #define DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF_ALT0
  6995. #define DUK_TVAL_SET_DOUBLE_UPDREF DUK_TVAL_SET_DOUBLE_UPDREF_ALT0
  6996. #define DUK_TVAL_SET_NAN_UPDREF DUK_TVAL_SET_NAN_UPDREF_ALT0
  6997. #if defined(DUK_USE_FASTINT)
  6998. #define DUK_TVAL_SET_FASTINT_UPDREF DUK_TVAL_SET_FASTINT_UPDREF_ALT0
  6999. #define DUK_TVAL_SET_FASTINT_I32_UPDREF DUK_TVAL_SET_FASTINT_I32_UPDREF_ALT0
  7000. #define DUK_TVAL_SET_FASTINT_U32_UPDREF DUK_TVAL_SET_FASTINT_U32_UPDREF_ALT0
  7001. #endif /* DUK_USE_FASTINT */
  7002. #define DUK_TVAL_SET_LIGHTFUNC_UPDREF DUK_TVAL_SET_LIGHTFUNC_UPDREF_ALT0
  7003. #define DUK_TVAL_SET_STRING_UPDREF DUK_TVAL_SET_STRING_UPDREF_ALT0
  7004. #define DUK_TVAL_SET_OBJECT_UPDREF DUK_TVAL_SET_OBJECT_UPDREF_ALT0
  7005. #define DUK_TVAL_SET_BUFFER_UPDREF DUK_TVAL_SET_BUFFER_UPDREF_ALT0
  7006. #define DUK_TVAL_SET_POINTER_UPDREF DUK_TVAL_SET_POINTER_UPDREF_ALT0
  7007. #define DUK_TVAL_SET_TVAL_UPDREF DUK_TVAL_SET_TVAL_UPDREF_ALT0
  7008. #define DUK_TVAL_SET_TVAL_UPDREF_FAST DUK_TVAL_SET_TVAL_UPDREF_ALT0
  7009. #define DUK_TVAL_SET_TVAL_UPDREF_SLOW DUK_TVAL_SET_TVAL_UPDREF_ALT0
  7010. #endif /* DUK_USE_REFERENCE_COUNTING */
  7011. #endif /* DUK_HEAPHDR_H_INCLUDED */
  7012. #line 1 "duk_api_internal.h"
  7013. /*
  7014. * Internal API calls which have (stack and other) semantics similar
  7015. * to the public API.
  7016. */
  7017. #ifndef DUK_API_INTERNAL_H_INCLUDED
  7018. #define DUK_API_INTERNAL_H_INCLUDED
  7019. /* duk_push_sprintf constants */
  7020. #define DUK_PUSH_SPRINTF_INITIAL_SIZE 256L
  7021. #define DUK_PUSH_SPRINTF_SANITY_LIMIT (1L * 1024L * 1024L * 1024L)
  7022. /* Flag ORed to err_code to indicate __FILE__ / __LINE__ is not
  7023. * blamed as source of error for error fileName / lineNumber.
  7024. */
  7025. #define DUK_ERRCODE_FLAG_NOBLAME_FILELINE (1L << 24)
  7026. /* Valstack resize flags */
  7027. #define DUK_VSRESIZE_FLAG_SHRINK (1 << 0)
  7028. #define DUK_VSRESIZE_FLAG_COMPACT (1 << 1)
  7029. #define DUK_VSRESIZE_FLAG_THROW (1 << 2)
  7030. /* Current convention is to use duk_size_t for value stack sizes and global indices,
  7031. * and duk_idx_t for local frame indices.
  7032. */
  7033. DUK_INTERNAL_DECL
  7034. duk_bool_t duk_valstack_resize_raw(duk_context *ctx,
  7035. duk_size_t min_new_size,
  7036. duk_small_uint_t flags);
  7037. DUK_INTERNAL_DECL duk_tval *duk_get_tval(duk_context *ctx, duk_idx_t index);
  7038. DUK_INTERNAL_DECL duk_tval *duk_require_tval(duk_context *ctx, duk_idx_t index);
  7039. DUK_INTERNAL_DECL void duk_push_tval(duk_context *ctx, duk_tval *tv);
  7040. /* Push the current 'this' binding; throw TypeError if binding is not object
  7041. * coercible (CheckObjectCoercible).
  7042. */
  7043. DUK_INTERNAL_DECL void duk_push_this_check_object_coercible(duk_context *ctx);
  7044. /* duk_push_this() + CheckObjectCoercible() + duk_to_object() */
  7045. DUK_INTERNAL_DECL duk_hobject *duk_push_this_coercible_to_object(duk_context *ctx);
  7046. /* duk_push_this() + CheckObjectCoercible() + duk_to_string() */
  7047. DUK_INTERNAL_DECL duk_hstring *duk_push_this_coercible_to_string(duk_context *ctx);
  7048. /* Get a borrowed duk_tval pointer to the current 'this' binding. Caller must
  7049. * make sure there's an active callstack entry. Note that the returned pointer
  7050. * is unstable with regards to side effects.
  7051. */
  7052. DUK_INTERNAL_DECL duk_tval *duk_get_borrowed_this_tval(duk_context *ctx);
  7053. /* XXX: add fastint support? */
  7054. #define duk_push_u64(ctx,val) \
  7055. duk_push_number((ctx), (duk_double_t) (val))
  7056. #define duk_push_i64(ctx,val) \
  7057. duk_push_number((ctx), (duk_double_t) (val))
  7058. /* duk_push_(u)int() is guaranteed to support at least (un)signed 32-bit range */
  7059. #define duk_push_u32(ctx,val) \
  7060. duk_push_uint((ctx), (duk_uint_t) (val))
  7061. #define duk_push_i32(ctx,val) \
  7062. duk_push_int((ctx), (duk_int_t) (val))
  7063. /* sometimes stack and array indices need to go on the stack */
  7064. #define duk_push_idx(ctx,val) \
  7065. duk_push_int((ctx), (duk_int_t) (val))
  7066. #define duk_push_uarridx(ctx,val) \
  7067. duk_push_uint((ctx), (duk_uint_t) (val))
  7068. #define duk_push_size_t(ctx,val) \
  7069. duk_push_uint((ctx), (duk_uint_t) (val)) /* XXX: assumed to fit for now */
  7070. /* internal helper for looking up a tagged type */
  7071. #define DUK_GETTAGGED_FLAG_ALLOW_NULL (1L << 24)
  7072. #define DUK_GETTAGGED_FLAG_CHECK_CLASS (1L << 25)
  7073. #define DUK_GETTAGGED_CLASS_SHIFT 16
  7074. DUK_INTERNAL_DECL duk_heaphdr *duk_get_tagged_heaphdr_raw(duk_context *ctx, duk_idx_t index, duk_uint_t flags_and_tag);
  7075. DUK_INTERNAL_DECL duk_hstring *duk_get_hstring(duk_context *ctx, duk_idx_t index);
  7076. DUK_INTERNAL_DECL duk_hobject *duk_get_hobject(duk_context *ctx, duk_idx_t index);
  7077. DUK_INTERNAL_DECL duk_hbuffer *duk_get_hbuffer(duk_context *ctx, duk_idx_t index);
  7078. DUK_INTERNAL_DECL duk_hthread *duk_get_hthread(duk_context *ctx, duk_idx_t index);
  7079. DUK_INTERNAL_DECL duk_hcompiledfunction *duk_get_hcompiledfunction(duk_context *ctx, duk_idx_t index);
  7080. DUK_INTERNAL_DECL duk_hnativefunction *duk_get_hnativefunction(duk_context *ctx, duk_idx_t index);
  7081. #define duk_get_hobject_with_class(ctx,index,classnum) \
  7082. ((duk_hobject *) duk_get_tagged_heaphdr_raw((ctx), (index), \
  7083. DUK_TAG_OBJECT | DUK_GETTAGGED_FLAG_ALLOW_NULL | \
  7084. DUK_GETTAGGED_FLAG_CHECK_CLASS | ((classnum) << DUK_GETTAGGED_CLASS_SHIFT)))
  7085. #if 0 /* This would be pointless: unexpected type and lightfunc would both return NULL */
  7086. DUK_INTERNAL_DECL duk_hobject *duk_get_hobject_or_lfunc(duk_context *ctx, duk_idx_t index);
  7087. #endif
  7088. DUK_INTERNAL_DECL duk_hobject *duk_get_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index);
  7089. #if 0 /*unused*/
  7090. DUK_INTERNAL_DECL void *duk_get_voidptr(duk_context *ctx, duk_idx_t index);
  7091. #endif
  7092. DUK_INTERNAL_DECL duk_hstring *duk_to_hstring(duk_context *ctx, duk_idx_t index);
  7093. #if defined(DUK_USE_DEBUGGER_SUPPORT) /* only needed by debugger for now */
  7094. DUK_INTERNAL_DECL duk_hstring *duk_safe_to_hstring(duk_context *ctx, duk_idx_t index);
  7095. #endif
  7096. DUK_INTERNAL_DECL void duk_to_object_class_string_top(duk_context *ctx);
  7097. DUK_INTERNAL_DECL void duk_push_hobject_class_string(duk_context *ctx, duk_hobject *h);
  7098. DUK_INTERNAL_DECL duk_int_t duk_to_int_clamped_raw(duk_context *ctx, duk_idx_t index, duk_int_t minval, duk_int_t maxval, duk_bool_t *out_clamped); /* out_clamped=NULL, RangeError if outside range */
  7099. DUK_INTERNAL_DECL duk_int_t duk_to_int_clamped(duk_context *ctx, duk_idx_t index, duk_int_t minval, duk_int_t maxval);
  7100. DUK_INTERNAL_DECL duk_int_t duk_to_int_check_range(duk_context *ctx, duk_idx_t index, duk_int_t minval, duk_int_t maxval);
  7101. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  7102. DUK_INTERNAL_DECL duk_uint8_t duk_to_uint8clamped(duk_context *ctx, duk_idx_t index);
  7103. #endif
  7104. DUK_INTERNAL_DECL duk_hstring *duk_require_hstring(duk_context *ctx, duk_idx_t index);
  7105. DUK_INTERNAL_DECL duk_hobject *duk_require_hobject(duk_context *ctx, duk_idx_t index);
  7106. DUK_INTERNAL_DECL duk_hbuffer *duk_require_hbuffer(duk_context *ctx, duk_idx_t index);
  7107. DUK_INTERNAL_DECL duk_hthread *duk_require_hthread(duk_context *ctx, duk_idx_t index);
  7108. DUK_INTERNAL_DECL duk_hcompiledfunction *duk_require_hcompiledfunction(duk_context *ctx, duk_idx_t index);
  7109. DUK_INTERNAL_DECL duk_hnativefunction *duk_require_hnativefunction(duk_context *ctx, duk_idx_t index);
  7110. #define duk_require_hobject_with_class(ctx,index,classnum) \
  7111. ((duk_hobject *) duk_get_tagged_heaphdr_raw((ctx), (index), \
  7112. DUK_TAG_OBJECT | \
  7113. DUK_GETTAGGED_FLAG_CHECK_CLASS | ((classnum) << DUK_GETTAGGED_CLASS_SHIFT)))
  7114. DUK_INTERNAL_DECL duk_hobject *duk_require_hobject_or_lfunc(duk_context *ctx, duk_idx_t index);
  7115. DUK_INTERNAL_DECL duk_hobject *duk_require_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index);
  7116. DUK_INTERNAL_DECL void duk_push_hstring(duk_context *ctx, duk_hstring *h);
  7117. DUK_INTERNAL_DECL void duk_push_hstring_stridx(duk_context *ctx, duk_small_int_t stridx);
  7118. DUK_INTERNAL_DECL void duk_push_hobject(duk_context *ctx, duk_hobject *h);
  7119. DUK_INTERNAL_DECL void duk_push_hbuffer(duk_context *ctx, duk_hbuffer *h);
  7120. #define duk_push_hthread(ctx,h) \
  7121. duk_push_hobject((ctx), (duk_hobject *) (h))
  7122. #define duk_push_hcompiledfunction(ctx,h) \
  7123. duk_push_hobject((ctx), (duk_hobject *) (h))
  7124. #define duk_push_hnativefunction(ctx,h) \
  7125. duk_push_hobject((ctx), (duk_hobject *) (h))
  7126. DUK_INTERNAL_DECL void duk_push_hobject_bidx(duk_context *ctx, duk_small_int_t builtin_idx);
  7127. DUK_INTERNAL_DECL duk_idx_t duk_push_object_helper(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx);
  7128. DUK_INTERNAL_DECL duk_idx_t duk_push_object_helper_proto(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_hobject *proto);
  7129. DUK_INTERNAL_DECL duk_idx_t duk_push_object_internal(duk_context *ctx);
  7130. DUK_INTERNAL_DECL duk_idx_t duk_push_compiledfunction(duk_context *ctx);
  7131. DUK_INTERNAL_DECL void duk_push_c_function_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs);
  7132. DUK_INTERNAL_DECL void duk_push_c_function_noconstruct_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs);
  7133. DUK_INTERNAL_DECL void duk_push_string_funcptr(duk_context *ctx, duk_uint8_t *ptr, duk_size_t sz);
  7134. DUK_INTERNAL_DECL void duk_push_lightfunc_name(duk_context *ctx, duk_tval *tv);
  7135. DUK_INTERNAL_DECL void duk_push_lightfunc_tostring(duk_context *ctx, duk_tval *tv);
  7136. DUK_INTERNAL_DECL duk_hbufferobject *duk_push_bufferobject_raw(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx);
  7137. DUK_INTERNAL_DECL const char *duk_push_string_tval_readable(duk_context *ctx, duk_tval *tv);
  7138. DUK_INTERNAL_DECL duk_bool_t duk_get_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx); /* [] -> [val] */
  7139. DUK_INTERNAL_DECL duk_bool_t duk_put_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx); /* [val] -> [] */
  7140. DUK_INTERNAL_DECL duk_bool_t duk_del_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx); /* [] -> [] */
  7141. DUK_INTERNAL_DECL duk_bool_t duk_has_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx); /* [] -> [] */
  7142. DUK_INTERNAL_DECL duk_bool_t duk_get_prop_stridx_boolean(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_bool_t *out_has_prop); /* [] -> [] */
  7143. DUK_INTERNAL_DECL void duk_xdef_prop(duk_context *ctx, duk_idx_t obj_index, duk_small_uint_t desc_flags); /* [key val] -> [] */
  7144. DUK_INTERNAL_DECL void duk_xdef_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index, duk_small_uint_t desc_flags); /* [val] -> [] */
  7145. DUK_INTERNAL_DECL void duk_xdef_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_uint_t desc_flags); /* [val] -> [] */
  7146. DUK_INTERNAL_DECL void duk_xdef_prop_stridx_builtin(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_int_t builtin_idx, duk_small_uint_t desc_flags); /* [] -> [] */
  7147. DUK_INTERNAL_DECL void duk_xdef_prop_stridx_thrower(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_uint_t desc_flags); /* [] -> [] */
  7148. /* These are macros for now, but could be separate functions to reduce code
  7149. * footprint (check call site count before refactoring).
  7150. */
  7151. #define duk_xdef_prop_wec(ctx,obj_index) \
  7152. duk_xdef_prop((ctx), (obj_index), DUK_PROPDESC_FLAGS_WEC)
  7153. #define duk_xdef_prop_index_wec(ctx,obj_index,arr_index) \
  7154. duk_xdef_prop_index((ctx), (obj_index), (arr_index), DUK_PROPDESC_FLAGS_WEC)
  7155. #define duk_xdef_prop_stridx_wec(ctx,obj_index,stridx) \
  7156. duk_xdef_prop_stridx((ctx), (obj_index), (stridx), DUK_PROPDESC_FLAGS_WEC)
  7157. /* Set object 'length'. */
  7158. DUK_INTERNAL_DECL void duk_set_length(duk_context *ctx, duk_idx_t index, duk_size_t length);
  7159. #endif /* DUK_API_INTERNAL_H_INCLUDED */
  7160. #line 1 "duk_hstring.h"
  7161. /*
  7162. * Heap string representation.
  7163. *
  7164. * Strings are byte sequences ordinarily stored in extended UTF-8 format,
  7165. * allowing values larger than the official UTF-8 range (used internally)
  7166. * and also allowing UTF-8 encoding of surrogate pairs (CESU-8 format).
  7167. * Strings may also be invalid UTF-8 altogether which is the case e.g. with
  7168. * strings used as internal property names and raw buffers converted to
  7169. * strings. In such cases the 'clen' field contains an inaccurate value.
  7170. *
  7171. * Ecmascript requires support for 32-bit long strings. However, since each
  7172. * 16-bit codepoint can take 3 bytes in CESU-8, this representation can only
  7173. * support about 1.4G codepoint long strings in extreme cases. This is not
  7174. * really a practical issue.
  7175. */
  7176. #ifndef DUK_HSTRING_H_INCLUDED
  7177. #define DUK_HSTRING_H_INCLUDED
  7178. /* Impose a maximum string length for now. Restricted artificially to
  7179. * ensure adding a heap header length won't overflow size_t. The limit
  7180. * should be synchronized with DUK_HBUFFER_MAX_BYTELEN.
  7181. *
  7182. * E5.1 makes provisions to support strings longer than 4G characters.
  7183. * This limit should be eliminated on 64-bit platforms (and increased
  7184. * closer to maximum support on 32-bit platforms).
  7185. */
  7186. #if defined(DUK_USE_STRLEN16)
  7187. #define DUK_HSTRING_MAX_BYTELEN (0x0000ffffUL)
  7188. #else
  7189. #define DUK_HSTRING_MAX_BYTELEN (0x7fffffffUL)
  7190. #endif
  7191. /* XXX: could add flags for "is valid CESU-8" (Ecmascript compatible strings),
  7192. * "is valid UTF-8", "is valid extended UTF-8" (internal strings are not,
  7193. * regexp bytecode is), and "contains non-BMP characters". These are not
  7194. * needed right now.
  7195. */
  7196. #define DUK_HSTRING_FLAG_ARRIDX DUK_HEAPHDR_USER_FLAG(0) /* string is a valid array index */
  7197. #define DUK_HSTRING_FLAG_INTERNAL DUK_HEAPHDR_USER_FLAG(1) /* string is internal */
  7198. #define DUK_HSTRING_FLAG_RESERVED_WORD DUK_HEAPHDR_USER_FLAG(2) /* string is a reserved word (non-strict) */
  7199. #define DUK_HSTRING_FLAG_STRICT_RESERVED_WORD DUK_HEAPHDR_USER_FLAG(3) /* string is a reserved word (strict) */
  7200. #define DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS DUK_HEAPHDR_USER_FLAG(4) /* string is 'eval' or 'arguments' */
  7201. #define DUK_HSTRING_FLAG_EXTDATA DUK_HEAPHDR_USER_FLAG(5) /* string data is external (duk_hstring_external) */
  7202. #define DUK_HSTRING_HAS_ARRIDX(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ARRIDX)
  7203. #define DUK_HSTRING_HAS_INTERNAL(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_INTERNAL)
  7204. #define DUK_HSTRING_HAS_RESERVED_WORD(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_RESERVED_WORD)
  7205. #define DUK_HSTRING_HAS_STRICT_RESERVED_WORD(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_STRICT_RESERVED_WORD)
  7206. #define DUK_HSTRING_HAS_EVAL_OR_ARGUMENTS(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS)
  7207. #define DUK_HSTRING_HAS_EXTDATA(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EXTDATA)
  7208. #define DUK_HSTRING_SET_ARRIDX(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ARRIDX)
  7209. #define DUK_HSTRING_SET_INTERNAL(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_INTERNAL)
  7210. #define DUK_HSTRING_SET_RESERVED_WORD(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_RESERVED_WORD)
  7211. #define DUK_HSTRING_SET_STRICT_RESERVED_WORD(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_STRICT_RESERVED_WORD)
  7212. #define DUK_HSTRING_SET_EVAL_OR_ARGUMENTS(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS)
  7213. #define DUK_HSTRING_SET_EXTDATA(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EXTDATA)
  7214. #define DUK_HSTRING_CLEAR_ARRIDX(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ARRIDX)
  7215. #define DUK_HSTRING_CLEAR_INTERNAL(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_INTERNAL)
  7216. #define DUK_HSTRING_CLEAR_RESERVED_WORD(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_RESERVED_WORD)
  7217. #define DUK_HSTRING_CLEAR_STRICT_RESERVED_WORD(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_STRICT_RESERVED_WORD)
  7218. #define DUK_HSTRING_CLEAR_EVAL_OR_ARGUMENTS(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS)
  7219. #define DUK_HSTRING_CLEAR_EXTDATA(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EXTDATA)
  7220. #define DUK_HSTRING_IS_ASCII(x) (DUK_HSTRING_GET_BYTELEN((x)) == DUK_HSTRING_GET_CHARLEN((x)))
  7221. #define DUK_HSTRING_IS_EMPTY(x) (DUK_HSTRING_GET_BYTELEN((x)) == 0)
  7222. #if defined(DUK_USE_STRHASH16)
  7223. #define DUK_HSTRING_GET_HASH(x) ((x)->hdr.h_flags >> 16)
  7224. #define DUK_HSTRING_SET_HASH(x,v) do { \
  7225. (x)->hdr.h_flags = ((x)->hdr.h_flags & 0x0000ffffUL) | ((v) << 16); \
  7226. } while (0)
  7227. #else
  7228. #define DUK_HSTRING_GET_HASH(x) ((x)->hash)
  7229. #define DUK_HSTRING_SET_HASH(x,v) do { \
  7230. (x)->hash = (v); \
  7231. } while (0)
  7232. #endif
  7233. #if defined(DUK_USE_STRLEN16)
  7234. #define DUK_HSTRING_GET_BYTELEN(x) ((x)->blen16)
  7235. #define DUK_HSTRING_SET_BYTELEN(x,v) do { \
  7236. (x)->blen16 = (v); \
  7237. } while (0)
  7238. #define DUK_HSTRING_GET_CHARLEN(x) ((x)->clen16)
  7239. #define DUK_HSTRING_SET_CHARLEN(x,v) do { \
  7240. (x)->clen16 = (v); \
  7241. } while (0)
  7242. #else
  7243. #define DUK_HSTRING_GET_BYTELEN(x) ((x)->blen)
  7244. #define DUK_HSTRING_SET_BYTELEN(x,v) do { \
  7245. (x)->blen = (v); \
  7246. } while (0)
  7247. #define DUK_HSTRING_GET_CHARLEN(x) ((x)->clen)
  7248. #define DUK_HSTRING_SET_CHARLEN(x,v) do { \
  7249. (x)->clen = (v); \
  7250. } while (0)
  7251. #endif
  7252. #if defined(DUK_USE_HSTRING_EXTDATA)
  7253. #define DUK_HSTRING_GET_EXTDATA(x) \
  7254. ((x)->extdata)
  7255. #define DUK_HSTRING_GET_DATA(x) \
  7256. (DUK_HSTRING_HAS_EXTDATA((x)) ? \
  7257. DUK_HSTRING_GET_EXTDATA((duk_hstring_external *) (x)) : ((const duk_uint8_t *) ((x) + 1)))
  7258. #else
  7259. #define DUK_HSTRING_GET_DATA(x) \
  7260. ((const duk_uint8_t *) ((x) + 1))
  7261. #endif
  7262. #define DUK_HSTRING_GET_DATA_END(x) \
  7263. (DUK_HSTRING_GET_DATA((x)) + (x)->blen)
  7264. /* marker value; in E5 2^32-1 is not a valid array index (2^32-2 is highest valid) */
  7265. #define DUK_HSTRING_NO_ARRAY_INDEX (0xffffffffUL)
  7266. /* get array index related to string (or return DUK_HSTRING_NO_ARRAY_INDEX);
  7267. * avoids helper call if string has no array index value.
  7268. */
  7269. #define DUK_HSTRING_GET_ARRIDX_FAST(h) \
  7270. (DUK_HSTRING_HAS_ARRIDX((h)) ? duk_js_to_arrayindex_string_helper((h)) : DUK_HSTRING_NO_ARRAY_INDEX)
  7271. /* slower but more compact variant */
  7272. #define DUK_HSTRING_GET_ARRIDX_SLOW(h) \
  7273. (duk_js_to_arrayindex_string_helper((h)))
  7274. /*
  7275. * Misc
  7276. */
  7277. struct duk_hstring {
  7278. /* Smaller heaphdr than for other objects, because strings are held
  7279. * in string intern table which requires no link pointers. Much of
  7280. * the 32-bit flags field is unused by flags, so we can stuff a 16-bit
  7281. * field in there.
  7282. */
  7283. duk_heaphdr_string hdr;
  7284. /* Note: we could try to stuff a partial hash (e.g. 16 bits) into the
  7285. * shared heap header. Good hashing needs more hash bits though.
  7286. */
  7287. /* string hash */
  7288. #if defined(DUK_USE_STRHASH16)
  7289. /* If 16-bit hash is in use, stuff it into duk_heaphdr_string flags. */
  7290. #else
  7291. duk_uint32_t hash;
  7292. #endif
  7293. /* length in bytes (not counting NUL term) */
  7294. #if defined(DUK_USE_STRLEN16)
  7295. duk_uint16_t blen16;
  7296. #else
  7297. duk_uint32_t blen;
  7298. #endif
  7299. /* length in codepoints (must be E5 compatible) */
  7300. #if defined(DUK_USE_STRLEN16)
  7301. duk_uint16_t clen16;
  7302. #else
  7303. duk_uint32_t clen;
  7304. #endif
  7305. /*
  7306. * String value of 'blen+1' bytes follows (+1 for NUL termination
  7307. * convenience for C API). No alignment needs to be guaranteed
  7308. * for strings, but fields above should guarantee alignment-by-4
  7309. * (but not alignment-by-8).
  7310. */
  7311. };
  7312. /* The external string struct is defined even when the feature is inactive. */
  7313. struct duk_hstring_external {
  7314. duk_hstring str;
  7315. /*
  7316. * For an external string, the NUL-terminated string data is stored
  7317. * externally. The user must guarantee that data behind this pointer
  7318. * doesn't change while it's used.
  7319. */
  7320. const duk_uint8_t *extdata;
  7321. };
  7322. /*
  7323. * Prototypes
  7324. */
  7325. DUK_INTERNAL_DECL duk_ucodepoint_t duk_hstring_char_code_at_raw(duk_hthread *thr, duk_hstring *h, duk_uint_t pos);
  7326. #endif /* DUK_HSTRING_H_INCLUDED */
  7327. #line 1 "duk_hobject.h"
  7328. /*
  7329. * Heap object representation.
  7330. *
  7331. * Heap objects are used for Ecmascript objects, arrays, and functions,
  7332. * but also for internal control like declarative and object environment
  7333. * records. Compiled functions, native functions, and threads are also
  7334. * objects but with an extended C struct.
  7335. *
  7336. * Objects provide the required Ecmascript semantics and exotic behaviors
  7337. * especially for property access.
  7338. *
  7339. * Properties are stored in three conceptual parts:
  7340. *
  7341. * 1. A linear 'entry part' contains ordered key-value-attributes triples
  7342. * and is the main method of string properties.
  7343. *
  7344. * 2. An optional linear 'array part' is used for array objects to store a
  7345. * (dense) range of [0,N[ array indexed entries with default attributes
  7346. * (writable, enumerable, configurable). If the array part would become
  7347. * sparse or non-default attributes are required, the array part is
  7348. * abandoned and moved to the 'entry part'.
  7349. *
  7350. * 3. An optional 'hash part' is used to optimize lookups of the entry
  7351. * part; it is used only for objects with sufficiently many properties
  7352. * and can be abandoned without loss of information.
  7353. *
  7354. * These three conceptual parts are stored in a single memory allocated area.
  7355. * This minimizes memory allocation overhead but also means that all three
  7356. * parts are resized together, and makes property access a bit complicated.
  7357. */
  7358. #ifndef DUK_HOBJECT_H_INCLUDED
  7359. #define DUK_HOBJECT_H_INCLUDED
  7360. /* there are currently 26 flag bits available */
  7361. #define DUK_HOBJECT_FLAG_EXTENSIBLE DUK_HEAPHDR_USER_FLAG(0) /* object is extensible */
  7362. #define DUK_HOBJECT_FLAG_CONSTRUCTABLE DUK_HEAPHDR_USER_FLAG(1) /* object is constructable */
  7363. #define DUK_HOBJECT_FLAG_BOUND DUK_HEAPHDR_USER_FLAG(2) /* object established using Function.prototype.bind() */
  7364. #define DUK_HOBJECT_FLAG_COMPILEDFUNCTION DUK_HEAPHDR_USER_FLAG(4) /* object is a compiled function (duk_hcompiledfunction) */
  7365. #define DUK_HOBJECT_FLAG_NATIVEFUNCTION DUK_HEAPHDR_USER_FLAG(5) /* object is a native function (duk_hnativefunction) */
  7366. #define DUK_HOBJECT_FLAG_BUFFEROBJECT DUK_HEAPHDR_USER_FLAG(6) /* object is a buffer object (duk_hbufferobject) (always exotic) */
  7367. #define DUK_HOBJECT_FLAG_THREAD DUK_HEAPHDR_USER_FLAG(7) /* object is a thread (duk_hthread) */
  7368. #define DUK_HOBJECT_FLAG_ARRAY_PART DUK_HEAPHDR_USER_FLAG(8) /* object has an array part (a_size may still be 0) */
  7369. #define DUK_HOBJECT_FLAG_STRICT DUK_HEAPHDR_USER_FLAG(9) /* function: function object is strict */
  7370. #define DUK_HOBJECT_FLAG_NOTAIL DUK_HEAPHDR_USER_FLAG(10) /* function: function must not be tail called */
  7371. #define DUK_HOBJECT_FLAG_NEWENV DUK_HEAPHDR_USER_FLAG(11) /* function: create new environment when called (see duk_hcompiledfunction) */
  7372. #define DUK_HOBJECT_FLAG_NAMEBINDING DUK_HEAPHDR_USER_FLAG(12) /* function: create binding for func name (function templates only, used for named function expressions) */
  7373. #define DUK_HOBJECT_FLAG_CREATEARGS DUK_HEAPHDR_USER_FLAG(13) /* function: create an arguments object on function call */
  7374. #define DUK_HOBJECT_FLAG_ENVRECCLOSED DUK_HEAPHDR_USER_FLAG(14) /* envrec: (declarative) record is closed */
  7375. #define DUK_HOBJECT_FLAG_EXOTIC_ARRAY DUK_HEAPHDR_USER_FLAG(15) /* 'Array' object, array length and index exotic behavior */
  7376. #define DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ DUK_HEAPHDR_USER_FLAG(16) /* 'String' object, array index exotic behavior */
  7377. #define DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS DUK_HEAPHDR_USER_FLAG(17) /* 'Arguments' object and has arguments exotic behavior (non-strict callee) */
  7378. #define DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC DUK_HEAPHDR_USER_FLAG(18) /* Duktape/C (nativefunction) object, exotic 'length' */
  7379. #define DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ DUK_HEAPHDR_USER_FLAG(19) /* 'Proxy' object */
  7380. #define DUK_HOBJECT_FLAG_CLASS_BASE DUK_HEAPHDR_USER_FLAG_NUMBER(21)
  7381. #define DUK_HOBJECT_FLAG_CLASS_BITS 5
  7382. #define DUK_HOBJECT_GET_CLASS_NUMBER(h) \
  7383. DUK_HEAPHDR_GET_FLAG_RANGE(&(h)->hdr, DUK_HOBJECT_FLAG_CLASS_BASE, DUK_HOBJECT_FLAG_CLASS_BITS)
  7384. #define DUK_HOBJECT_SET_CLASS_NUMBER(h,v) \
  7385. DUK_HEAPHDR_SET_FLAG_RANGE(&(h)->hdr, DUK_HOBJECT_FLAG_CLASS_BASE, DUK_HOBJECT_FLAG_CLASS_BITS, (v))
  7386. #define DUK_HOBJECT_GET_CLASS_MASK(h) \
  7387. (1UL << DUK_HEAPHDR_GET_FLAG_RANGE(&(h)->hdr, DUK_HOBJECT_FLAG_CLASS_BASE, DUK_HOBJECT_FLAG_CLASS_BITS))
  7388. /* Macro for creating flag initializer from a class number.
  7389. * Unsigned type cast is needed to avoid warnings about coercing
  7390. * a signed integer to an unsigned one; the largest class values
  7391. * have the highest bit (bit 31) set which causes this.
  7392. */
  7393. #define DUK_HOBJECT_CLASS_AS_FLAGS(v) (((duk_uint_t) (v)) << DUK_HOBJECT_FLAG_CLASS_BASE)
  7394. /* E5 Section 8.6.2 + custom classes */
  7395. #define DUK_HOBJECT_CLASS_UNUSED 0
  7396. #define DUK_HOBJECT_CLASS_ARGUMENTS 1
  7397. #define DUK_HOBJECT_CLASS_ARRAY 2
  7398. #define DUK_HOBJECT_CLASS_BOOLEAN 3
  7399. #define DUK_HOBJECT_CLASS_DATE 4
  7400. #define DUK_HOBJECT_CLASS_ERROR 5
  7401. #define DUK_HOBJECT_CLASS_FUNCTION 6
  7402. #define DUK_HOBJECT_CLASS_JSON 7
  7403. #define DUK_HOBJECT_CLASS_MATH 8
  7404. #define DUK_HOBJECT_CLASS_NUMBER 9
  7405. #define DUK_HOBJECT_CLASS_OBJECT 10
  7406. #define DUK_HOBJECT_CLASS_REGEXP 11
  7407. #define DUK_HOBJECT_CLASS_STRING 12
  7408. #define DUK_HOBJECT_CLASS_GLOBAL 13
  7409. #define DUK_HOBJECT_CLASS_OBJENV 14 /* custom */
  7410. #define DUK_HOBJECT_CLASS_DECENV 15 /* custom */
  7411. #define DUK_HOBJECT_CLASS_BUFFER 16 /* custom; implies DUK_HOBJECT_IS_BUFFEROBJECT */
  7412. #define DUK_HOBJECT_CLASS_POINTER 17 /* custom */
  7413. #define DUK_HOBJECT_CLASS_THREAD 18 /* custom; implies DUK_HOBJECT_IS_THREAD */
  7414. #define DUK_HOBJECT_CLASS_ARRAYBUFFER 19 /* implies DUK_HOBJECT_IS_BUFFEROBJECT */
  7415. #define DUK_HOBJECT_CLASS_DATAVIEW 20
  7416. #define DUK_HOBJECT_CLASS_INT8ARRAY 21
  7417. #define DUK_HOBJECT_CLASS_UINT8ARRAY 22
  7418. #define DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY 23
  7419. #define DUK_HOBJECT_CLASS_INT16ARRAY 24
  7420. #define DUK_HOBJECT_CLASS_UINT16ARRAY 25
  7421. #define DUK_HOBJECT_CLASS_INT32ARRAY 26
  7422. #define DUK_HOBJECT_CLASS_UINT32ARRAY 27
  7423. #define DUK_HOBJECT_CLASS_FLOAT32ARRAY 28
  7424. #define DUK_HOBJECT_CLASS_FLOAT64ARRAY 29
  7425. #define DUK_HOBJECT_CLASS_MAX 29
  7426. /* class masks */
  7427. #define DUK_HOBJECT_CMASK_ALL ((1UL << (DUK_HOBJECT_CLASS_MAX + 1)) - 1UL)
  7428. #define DUK_HOBJECT_CMASK_UNUSED (1UL << DUK_HOBJECT_CLASS_UNUSED)
  7429. #define DUK_HOBJECT_CMASK_ARGUMENTS (1UL << DUK_HOBJECT_CLASS_ARGUMENTS)
  7430. #define DUK_HOBJECT_CMASK_ARRAY (1UL << DUK_HOBJECT_CLASS_ARRAY)
  7431. #define DUK_HOBJECT_CMASK_BOOLEAN (1UL << DUK_HOBJECT_CLASS_BOOLEAN)
  7432. #define DUK_HOBJECT_CMASK_DATE (1UL << DUK_HOBJECT_CLASS_DATE)
  7433. #define DUK_HOBJECT_CMASK_ERROR (1UL << DUK_HOBJECT_CLASS_ERROR)
  7434. #define DUK_HOBJECT_CMASK_FUNCTION (1UL << DUK_HOBJECT_CLASS_FUNCTION)
  7435. #define DUK_HOBJECT_CMASK_JSON (1UL << DUK_HOBJECT_CLASS_JSON)
  7436. #define DUK_HOBJECT_CMASK_MATH (1UL << DUK_HOBJECT_CLASS_MATH)
  7437. #define DUK_HOBJECT_CMASK_NUMBER (1UL << DUK_HOBJECT_CLASS_NUMBER)
  7438. #define DUK_HOBJECT_CMASK_OBJECT (1UL << DUK_HOBJECT_CLASS_OBJECT)
  7439. #define DUK_HOBJECT_CMASK_REGEXP (1UL << DUK_HOBJECT_CLASS_REGEXP)
  7440. #define DUK_HOBJECT_CMASK_STRING (1UL << DUK_HOBJECT_CLASS_STRING)
  7441. #define DUK_HOBJECT_CMASK_GLOBAL (1UL << DUK_HOBJECT_CLASS_GLOBAL)
  7442. #define DUK_HOBJECT_CMASK_OBJENV (1UL << DUK_HOBJECT_CLASS_OBJENV)
  7443. #define DUK_HOBJECT_CMASK_DECENV (1UL << DUK_HOBJECT_CLASS_DECENV)
  7444. #define DUK_HOBJECT_CMASK_BUFFER (1UL << DUK_HOBJECT_CLASS_BUFFER)
  7445. #define DUK_HOBJECT_CMASK_POINTER (1UL << DUK_HOBJECT_CLASS_POINTER)
  7446. #define DUK_HOBJECT_CMASK_THREAD (1UL << DUK_HOBJECT_CLASS_THREAD)
  7447. #define DUK_HOBJECT_CMASK_ARRAYBUFFER (1UL << DUK_HOBJECT_CLASS_ARRAYBUFFER)
  7448. #define DUK_HOBJECT_CMASK_DATAVIEW (1UL << DUK_HOBJECT_CLASS_DATAVIEW)
  7449. #define DUK_HOBJECT_CMASK_INT8ARRAY (1UL << DUK_HOBJECT_CLASS_INT8ARRAY)
  7450. #define DUK_HOBJECT_CMASK_UINT8ARRAY (1UL << DUK_HOBJECT_CLASS_UINT8ARRAY)
  7451. #define DUK_HOBJECT_CMASK_UINT8CLAMPEDARRAY (1UL << DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY)
  7452. #define DUK_HOBJECT_CMASK_INT16ARRAY (1UL << DUK_HOBJECT_CLASS_INT16ARRAY)
  7453. #define DUK_HOBJECT_CMASK_UINT16ARRAY (1UL << DUK_HOBJECT_CLASS_UINT16ARRAY)
  7454. #define DUK_HOBJECT_CMASK_INT32ARRAY (1UL << DUK_HOBJECT_CLASS_INT32ARRAY)
  7455. #define DUK_HOBJECT_CMASK_UINT32ARRAY (1UL << DUK_HOBJECT_CLASS_UINT32ARRAY)
  7456. #define DUK_HOBJECT_CMASK_FLOAT32ARRAY (1UL << DUK_HOBJECT_CLASS_FLOAT32ARRAY)
  7457. #define DUK_HOBJECT_CMASK_FLOAT64ARRAY (1UL << DUK_HOBJECT_CLASS_FLOAT64ARRAY)
  7458. #define DUK_HOBJECT_CMASK_ALL_BUFFEROBJECTS \
  7459. (DUK_HOBJECT_CMASK_BUFFER | \
  7460. DUK_HOBJECT_CMASK_ARRAYBUFFER | \
  7461. DUK_HOBJECT_CMASK_DATAVIEW | \
  7462. DUK_HOBJECT_CMASK_INT8ARRAY | \
  7463. DUK_HOBJECT_CMASK_UINT8ARRAY | \
  7464. DUK_HOBJECT_CMASK_UINT8CLAMPEDARRAY | \
  7465. DUK_HOBJECT_CMASK_INT16ARRAY | \
  7466. DUK_HOBJECT_CMASK_UINT16ARRAY | \
  7467. DUK_HOBJECT_CMASK_INT32ARRAY | \
  7468. DUK_HOBJECT_CMASK_UINT32ARRAY | \
  7469. DUK_HOBJECT_CMASK_FLOAT32ARRAY | \
  7470. DUK_HOBJECT_CMASK_FLOAT64ARRAY)
  7471. #define DUK_HOBJECT_IS_OBJENV(h) (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_OBJENV)
  7472. #define DUK_HOBJECT_IS_DECENV(h) (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_DECENV)
  7473. #define DUK_HOBJECT_IS_ENV(h) (DUK_HOBJECT_IS_OBJENV((h)) || DUK_HOBJECT_IS_DECENV((h)))
  7474. #define DUK_HOBJECT_IS_ARRAY(h) (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_ARRAY)
  7475. #define DUK_HOBJECT_IS_COMPILEDFUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPILEDFUNCTION)
  7476. #define DUK_HOBJECT_IS_NATIVEFUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATIVEFUNCTION)
  7477. #define DUK_HOBJECT_IS_BUFFEROBJECT(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFFEROBJECT)
  7478. #define DUK_HOBJECT_IS_THREAD(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_THREAD)
  7479. #define DUK_HOBJECT_IS_NONBOUND_FUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, \
  7480. DUK_HOBJECT_FLAG_COMPILEDFUNCTION | \
  7481. DUK_HOBJECT_FLAG_NATIVEFUNCTION)
  7482. #define DUK_HOBJECT_IS_FUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, \
  7483. DUK_HOBJECT_FLAG_BOUND | \
  7484. DUK_HOBJECT_FLAG_COMPILEDFUNCTION | \
  7485. DUK_HOBJECT_FLAG_NATIVEFUNCTION)
  7486. #define DUK_HOBJECT_IS_CALLABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, \
  7487. DUK_HOBJECT_FLAG_BOUND | \
  7488. DUK_HOBJECT_FLAG_COMPILEDFUNCTION | \
  7489. DUK_HOBJECT_FLAG_NATIVEFUNCTION)
  7490. /* object has any exotic behavior(s) */
  7491. #define DUK_HOBJECT_EXOTIC_BEHAVIOR_FLAGS (DUK_HOBJECT_FLAG_EXOTIC_ARRAY | \
  7492. DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS | \
  7493. DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ | \
  7494. DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC | \
  7495. DUK_HOBJECT_FLAG_BUFFEROBJECT | \
  7496. DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ)
  7497. #define DUK_HOBJECT_HAS_EXOTIC_BEHAVIOR(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_EXOTIC_BEHAVIOR_FLAGS)
  7498. #define DUK_HOBJECT_HAS_EXTENSIBLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXTENSIBLE)
  7499. #define DUK_HOBJECT_HAS_CONSTRUCTABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CONSTRUCTABLE)
  7500. #define DUK_HOBJECT_HAS_BOUND(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BOUND)
  7501. #define DUK_HOBJECT_HAS_COMPILEDFUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPILEDFUNCTION)
  7502. #define DUK_HOBJECT_HAS_NATIVEFUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATIVEFUNCTION)
  7503. #define DUK_HOBJECT_HAS_BUFFEROBJECT(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFFEROBJECT)
  7504. #define DUK_HOBJECT_HAS_THREAD(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_THREAD)
  7505. #define DUK_HOBJECT_HAS_ARRAY_PART(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ARRAY_PART)
  7506. #define DUK_HOBJECT_HAS_STRICT(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_STRICT)
  7507. #define DUK_HOBJECT_HAS_NOTAIL(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NOTAIL)
  7508. #define DUK_HOBJECT_HAS_NEWENV(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NEWENV)
  7509. #define DUK_HOBJECT_HAS_NAMEBINDING(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NAMEBINDING)
  7510. #define DUK_HOBJECT_HAS_CREATEARGS(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CREATEARGS)
  7511. #define DUK_HOBJECT_HAS_ENVRECCLOSED(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ENVRECCLOSED)
  7512. #define DUK_HOBJECT_HAS_EXOTIC_ARRAY(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARRAY)
  7513. #define DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ)
  7514. #define DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS)
  7515. #define DUK_HOBJECT_HAS_EXOTIC_DUKFUNC(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC)
  7516. #define DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ)
  7517. #define DUK_HOBJECT_SET_EXTENSIBLE(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXTENSIBLE)
  7518. #define DUK_HOBJECT_SET_CONSTRUCTABLE(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CONSTRUCTABLE)
  7519. #define DUK_HOBJECT_SET_BOUND(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BOUND)
  7520. #define DUK_HOBJECT_SET_COMPILEDFUNCTION(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPILEDFUNCTION)
  7521. #define DUK_HOBJECT_SET_NATIVEFUNCTION(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATIVEFUNCTION)
  7522. #define DUK_HOBJECT_SET_BUFFEROBJECT(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFFEROBJECT)
  7523. #define DUK_HOBJECT_SET_THREAD(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_THREAD)
  7524. #define DUK_HOBJECT_SET_ARRAY_PART(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ARRAY_PART)
  7525. #define DUK_HOBJECT_SET_STRICT(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_STRICT)
  7526. #define DUK_HOBJECT_SET_NOTAIL(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NOTAIL)
  7527. #define DUK_HOBJECT_SET_NEWENV(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NEWENV)
  7528. #define DUK_HOBJECT_SET_NAMEBINDING(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NAMEBINDING)
  7529. #define DUK_HOBJECT_SET_CREATEARGS(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CREATEARGS)
  7530. #define DUK_HOBJECT_SET_ENVRECCLOSED(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ENVRECCLOSED)
  7531. #define DUK_HOBJECT_SET_EXOTIC_ARRAY(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARRAY)
  7532. #define DUK_HOBJECT_SET_EXOTIC_STRINGOBJ(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ)
  7533. #define DUK_HOBJECT_SET_EXOTIC_ARGUMENTS(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS)
  7534. #define DUK_HOBJECT_SET_EXOTIC_DUKFUNC(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC)
  7535. #define DUK_HOBJECT_SET_EXOTIC_PROXYOBJ(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ)
  7536. #define DUK_HOBJECT_CLEAR_EXTENSIBLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXTENSIBLE)
  7537. #define DUK_HOBJECT_CLEAR_CONSTRUCTABLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CONSTRUCTABLE)
  7538. #define DUK_HOBJECT_CLEAR_BOUND(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BOUND)
  7539. #define DUK_HOBJECT_CLEAR_COMPILEDFUNCTION(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPILEDFUNCTION)
  7540. #define DUK_HOBJECT_CLEAR_NATIVEFUNCTION(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATIVEFUNCTION)
  7541. #define DUK_HOBJECT_CLEAR_BUFFEROBJECT(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFFEROBJECT)
  7542. #define DUK_HOBJECT_CLEAR_THREAD(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_THREAD)
  7543. #define DUK_HOBJECT_CLEAR_ARRAY_PART(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ARRAY_PART)
  7544. #define DUK_HOBJECT_CLEAR_STRICT(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_STRICT)
  7545. #define DUK_HOBJECT_CLEAR_NOTAIL(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NOTAIL)
  7546. #define DUK_HOBJECT_CLEAR_NEWENV(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NEWENV)
  7547. #define DUK_HOBJECT_CLEAR_NAMEBINDING(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NAMEBINDING)
  7548. #define DUK_HOBJECT_CLEAR_CREATEARGS(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CREATEARGS)
  7549. #define DUK_HOBJECT_CLEAR_ENVRECCLOSED(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ENVRECCLOSED)
  7550. #define DUK_HOBJECT_CLEAR_EXOTIC_ARRAY(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARRAY)
  7551. #define DUK_HOBJECT_CLEAR_EXOTIC_STRINGOBJ(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ)
  7552. #define DUK_HOBJECT_CLEAR_EXOTIC_ARGUMENTS(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS)
  7553. #define DUK_HOBJECT_CLEAR_EXOTIC_DUKFUNC(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC)
  7554. #define DUK_HOBJECT_CLEAR_EXOTIC_PROXYOBJ(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ)
  7555. /* flags used for property attributes in duk_propdesc and packed flags */
  7556. #define DUK_PROPDESC_FLAG_WRITABLE (1 << 0) /* E5 Section 8.6.1 */
  7557. #define DUK_PROPDESC_FLAG_ENUMERABLE (1 << 1) /* E5 Section 8.6.1 */
  7558. #define DUK_PROPDESC_FLAG_CONFIGURABLE (1 << 2) /* E5 Section 8.6.1 */
  7559. #define DUK_PROPDESC_FLAG_ACCESSOR (1 << 3) /* accessor */
  7560. #define DUK_PROPDESC_FLAG_VIRTUAL (1 << 4) /* property is virtual: used in duk_propdesc, never stored
  7561. * (used by e.g. buffer virtual properties)
  7562. */
  7563. #define DUK_PROPDESC_FLAGS_MASK (DUK_PROPDESC_FLAG_WRITABLE | \
  7564. DUK_PROPDESC_FLAG_ENUMERABLE | \
  7565. DUK_PROPDESC_FLAG_CONFIGURABLE | \
  7566. DUK_PROPDESC_FLAG_ACCESSOR)
  7567. /* additional flags which are passed in the same flags argument as property
  7568. * flags but are not stored in object properties.
  7569. */
  7570. #define DUK_PROPDESC_FLAG_NO_OVERWRITE (1 << 4) /* internal define property: skip write silently if exists */
  7571. /* convenience */
  7572. #define DUK_PROPDESC_FLAGS_NONE 0
  7573. #define DUK_PROPDESC_FLAGS_W (DUK_PROPDESC_FLAG_WRITABLE)
  7574. #define DUK_PROPDESC_FLAGS_E (DUK_PROPDESC_FLAG_ENUMERABLE)
  7575. #define DUK_PROPDESC_FLAGS_C (DUK_PROPDESC_FLAG_CONFIGURABLE)
  7576. #define DUK_PROPDESC_FLAGS_WE (DUK_PROPDESC_FLAG_WRITABLE | DUK_PROPDESC_FLAG_ENUMERABLE)
  7577. #define DUK_PROPDESC_FLAGS_WC (DUK_PROPDESC_FLAG_WRITABLE | DUK_PROPDESC_FLAG_CONFIGURABLE)
  7578. #define DUK_PROPDESC_FLAGS_EC (DUK_PROPDESC_FLAG_ENUMERABLE | DUK_PROPDESC_FLAG_CONFIGURABLE)
  7579. #define DUK_PROPDESC_FLAGS_WEC (DUK_PROPDESC_FLAG_WRITABLE | \
  7580. DUK_PROPDESC_FLAG_ENUMERABLE | \
  7581. DUK_PROPDESC_FLAG_CONFIGURABLE)
  7582. /*
  7583. * Macro for object validity check
  7584. *
  7585. * Assert for currently guaranteed relations between flags, for instance.
  7586. */
  7587. #define DUK_ASSERT_HOBJECT_VALID(h) do { \
  7588. DUK_ASSERT((h) != NULL); \
  7589. DUK_ASSERT(!DUK_HOBJECT_IS_CALLABLE((h)) || \
  7590. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_FUNCTION); \
  7591. DUK_ASSERT(!DUK_HOBJECT_IS_BUFFEROBJECT((h)) || \
  7592. (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_BUFFER || \
  7593. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_ARRAYBUFFER || \
  7594. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_DATAVIEW || \
  7595. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_INT8ARRAY || \
  7596. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_UINT8ARRAY || \
  7597. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY || \
  7598. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_INT16ARRAY || \
  7599. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_UINT16ARRAY || \
  7600. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_INT32ARRAY || \
  7601. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_UINT32ARRAY || \
  7602. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_FLOAT32ARRAY || \
  7603. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_FLOAT64ARRAY)); \
  7604. } while (0)
  7605. /*
  7606. * Macros to access the 'props' allocation.
  7607. */
  7608. #if defined(DUK_USE_HEAPPTR16)
  7609. #define DUK_HOBJECT_GET_PROPS(heap,h) \
  7610. ((duk_uint8_t *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, ((duk_heaphdr *) (h))->h_extra16))
  7611. #define DUK_HOBJECT_SET_PROPS(heap,h,x) do { \
  7612. ((duk_heaphdr *) (h))->h_extra16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (x)); \
  7613. } while (0)
  7614. #else
  7615. #define DUK_HOBJECT_GET_PROPS(heap,h) \
  7616. ((h)->props)
  7617. #define DUK_HOBJECT_SET_PROPS(heap,h,x) do { \
  7618. (h)->props = (duk_uint8_t *) (x); \
  7619. } while (0)
  7620. #endif
  7621. #if defined(DUK_USE_HOBJECT_LAYOUT_1)
  7622. /* LAYOUT 1 */
  7623. #define DUK_HOBJECT_E_GET_KEY_BASE(heap,h) \
  7624. ((duk_hstring **) (void *) ( \
  7625. DUK_HOBJECT_GET_PROPS((heap), (h)) \
  7626. ))
  7627. #define DUK_HOBJECT_E_GET_VALUE_BASE(heap,h) \
  7628. ((duk_propvalue *) (void *) ( \
  7629. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  7630. DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_hstring *) \
  7631. ))
  7632. #define DUK_HOBJECT_E_GET_FLAGS_BASE(heap,h) \
  7633. ((duk_uint8_t *) (void *) ( \
  7634. DUK_HOBJECT_GET_PROPS((heap), (h)) + DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue)) \
  7635. ))
  7636. #define DUK_HOBJECT_A_GET_BASE(heap,h) \
  7637. ((duk_tval *) (void *) ( \
  7638. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  7639. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) \
  7640. ))
  7641. #define DUK_HOBJECT_H_GET_BASE(heap,h) \
  7642. ((duk_uint32_t *) (void *) ( \
  7643. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  7644. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \
  7645. DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \
  7646. ))
  7647. #define DUK_HOBJECT_P_COMPUTE_SIZE(n_ent,n_arr,n_hash) \
  7648. ( \
  7649. (n_ent) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \
  7650. (n_arr) * sizeof(duk_tval) + \
  7651. (n_hash) * sizeof(duk_uint32_t) \
  7652. )
  7653. #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 { \
  7654. (set_e_k) = (duk_hstring **) (void *) (p_base); \
  7655. (set_e_pv) = (duk_propvalue *) (void *) ((set_e_k) + (n_ent)); \
  7656. (set_e_f) = (duk_uint8_t *) (void *) ((set_e_pv) + (n_ent)); \
  7657. (set_a) = (duk_tval *) (void *) ((set_e_f) + (n_ent)); \
  7658. (set_h) = (duk_uint32_t *) (void *) ((set_a) + (n_arr)); \
  7659. } while (0)
  7660. #elif defined(DUK_USE_HOBJECT_LAYOUT_2)
  7661. /* LAYOUT 2 */
  7662. #if (DUK_USE_ALIGN_BY == 4)
  7663. #define DUK_HOBJECT_E_FLAG_PADDING(e_sz) ((4 - (e_sz)) & 0x03)
  7664. #elif (DUK_USE_ALIGN_BY == 8)
  7665. #define DUK_HOBJECT_E_FLAG_PADDING(e_sz) ((8 - (e_sz)) & 0x07)
  7666. #elif (DUK_USE_ALIGN_BY == 1)
  7667. #define DUK_HOBJECT_E_FLAG_PADDING(e_sz) 0
  7668. #else
  7669. #error invalid DUK_USE_ALIGN_BY
  7670. #endif
  7671. #define DUK_HOBJECT_E_GET_KEY_BASE(heap,h) \
  7672. ((duk_hstring **) (void *) ( \
  7673. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  7674. DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_propvalue) \
  7675. ))
  7676. #define DUK_HOBJECT_E_GET_VALUE_BASE(heap,h) \
  7677. ((duk_propvalue *) (void *) ( \
  7678. DUK_HOBJECT_GET_PROPS((heap), (h)) \
  7679. ))
  7680. #define DUK_HOBJECT_E_GET_FLAGS_BASE(heap,h) \
  7681. ((duk_uint8_t *) (void *) ( \
  7682. DUK_HOBJECT_GET_PROPS((heap), (h)) + DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue)) \
  7683. ))
  7684. #define DUK_HOBJECT_A_GET_BASE(heap,h) \
  7685. ((duk_tval *) (void *) ( \
  7686. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  7687. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \
  7688. DUK_HOBJECT_E_FLAG_PADDING(DUK_HOBJECT_GET_ESIZE((h))) \
  7689. ))
  7690. #define DUK_HOBJECT_H_GET_BASE(heap,h) \
  7691. ((duk_uint32_t *) (void *) ( \
  7692. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  7693. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \
  7694. DUK_HOBJECT_E_FLAG_PADDING(DUK_HOBJECT_GET_ESIZE((h))) + \
  7695. DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \
  7696. ))
  7697. #define DUK_HOBJECT_P_COMPUTE_SIZE(n_ent,n_arr,n_hash) \
  7698. ( \
  7699. (n_ent) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \
  7700. DUK_HOBJECT_E_FLAG_PADDING((n_ent)) + \
  7701. (n_arr) * sizeof(duk_tval) + \
  7702. (n_hash) * sizeof(duk_uint32_t) \
  7703. )
  7704. #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 { \
  7705. (set_e_pv) = (duk_propvalue *) (void *) (p_base); \
  7706. (set_e_k) = (duk_hstring **) (void *) ((set_e_pv) + (n_ent)); \
  7707. (set_e_f) = (duk_uint8_t *) (void *) ((set_e_k) + (n_ent)); \
  7708. (set_a) = (duk_tval *) (void *) (((duk_uint8_t *) (set_e_f)) + \
  7709. sizeof(duk_uint8_t) * (n_ent) + \
  7710. DUK_HOBJECT_E_FLAG_PADDING((n_ent))); \
  7711. (set_h) = (duk_uint32_t *) (void *) ((set_a) + (n_arr)); \
  7712. } while (0)
  7713. #elif defined(DUK_USE_HOBJECT_LAYOUT_3)
  7714. /* LAYOUT 3 */
  7715. #define DUK_HOBJECT_E_GET_KEY_BASE(heap,h) \
  7716. ((duk_hstring **) (void *) ( \
  7717. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  7718. DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_propvalue) + \
  7719. DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \
  7720. ))
  7721. #define DUK_HOBJECT_E_GET_VALUE_BASE(heap,h) \
  7722. ((duk_propvalue *) (void *) ( \
  7723. DUK_HOBJECT_GET_PROPS((heap), (h)) \
  7724. ))
  7725. #define DUK_HOBJECT_E_GET_FLAGS_BASE(heap,h) \
  7726. ((duk_uint8_t *) (void *) ( \
  7727. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  7728. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_propvalue) + sizeof(duk_hstring *)) + \
  7729. DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) + \
  7730. DUK_HOBJECT_GET_HSIZE((h)) * sizeof(duk_uint32_t) \
  7731. ))
  7732. #define DUK_HOBJECT_A_GET_BASE(heap,h) \
  7733. ((duk_tval *) (void *) ( \
  7734. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  7735. DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_propvalue) \
  7736. ))
  7737. #define DUK_HOBJECT_H_GET_BASE(heap,h) \
  7738. ((duk_uint32_t *) (void *) ( \
  7739. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  7740. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_propvalue) + sizeof(duk_hstring *)) + \
  7741. DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \
  7742. ))
  7743. #define DUK_HOBJECT_P_COMPUTE_SIZE(n_ent,n_arr,n_hash) \
  7744. ( \
  7745. (n_ent) * (sizeof(duk_propvalue) + sizeof(duk_hstring *) + sizeof(duk_uint8_t)) + \
  7746. (n_arr) * sizeof(duk_tval) + \
  7747. (n_hash) * sizeof(duk_uint32_t) \
  7748. )
  7749. #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 { \
  7750. (set_e_pv) = (duk_propvalue *) (void *) (p_base); \
  7751. (set_a) = (duk_tval *) (void *) ((set_e_pv) + (n_ent)); \
  7752. (set_e_k) = (duk_hstring **) (void *) ((set_a) + (n_arr)); \
  7753. (set_h) = (duk_uint32_t *) (void *) ((set_e_k) + (n_ent)); \
  7754. (set_e_f) = (duk_uint8_t *) (void *) ((set_h) + (n_hash)); \
  7755. } while (0)
  7756. #else
  7757. #error invalid hobject layout defines
  7758. #endif /* hobject property layout */
  7759. #define DUK_HOBJECT_E_ALLOC_SIZE(h) \
  7760. DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE((h)), DUK_HOBJECT_GET_ASIZE((h)), DUK_HOBJECT_GET_HSIZE((h)))
  7761. #define DUK_HOBJECT_E_GET_KEY(heap,h,i) (DUK_HOBJECT_E_GET_KEY_BASE((heap), (h))[(i)])
  7762. #define DUK_HOBJECT_E_GET_KEY_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_KEY_BASE((heap), (h))[(i)])
  7763. #define DUK_HOBJECT_E_GET_VALUE(heap,h,i) (DUK_HOBJECT_E_GET_VALUE_BASE((heap), (h))[(i)])
  7764. #define DUK_HOBJECT_E_GET_VALUE_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE_BASE((heap), (h))[(i)])
  7765. #define DUK_HOBJECT_E_GET_VALUE_TVAL(heap,h,i) (DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).v)
  7766. #define DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).v)
  7767. #define DUK_HOBJECT_E_GET_VALUE_GETTER(heap,h,i) (DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.get)
  7768. #define DUK_HOBJECT_E_GET_VALUE_GETTER_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.get)
  7769. #define DUK_HOBJECT_E_GET_VALUE_SETTER(heap,h,i) (DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.set)
  7770. #define DUK_HOBJECT_E_GET_VALUE_SETTER_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.set)
  7771. #define DUK_HOBJECT_E_GET_FLAGS(heap,h,i) (DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)])
  7772. #define DUK_HOBJECT_E_GET_FLAGS_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)])
  7773. #define DUK_HOBJECT_A_GET_VALUE(heap,h,i) (DUK_HOBJECT_A_GET_BASE((heap), (h))[(i)])
  7774. #define DUK_HOBJECT_A_GET_VALUE_PTR(heap,h,i) (&DUK_HOBJECT_A_GET_BASE((heap), (h))[(i)])
  7775. #define DUK_HOBJECT_H_GET_INDEX(heap,h,i) (DUK_HOBJECT_H_GET_BASE((heap), (h))[(i)])
  7776. #define DUK_HOBJECT_H_GET_INDEX_PTR(heap,h,i) (&DUK_HOBJECT_H_GET_BASE((heap), (h))[(i)])
  7777. #define DUK_HOBJECT_E_SET_KEY(heap,h,i,k) do { \
  7778. DUK_HOBJECT_E_GET_KEY((heap), (h), (i)) = (k); \
  7779. } while (0)
  7780. #define DUK_HOBJECT_E_SET_VALUE(heap,h,i,v) do { \
  7781. DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)) = (v); \
  7782. } while (0)
  7783. #define DUK_HOBJECT_E_SET_VALUE_TVAL(heap,h,i,v) do { \
  7784. DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).v = (v); \
  7785. } while (0)
  7786. #define DUK_HOBJECT_E_SET_VALUE_GETTER(heap,h,i,v) do { \
  7787. DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.get = (v); \
  7788. } while (0)
  7789. #define DUK_HOBJECT_E_SET_VALUE_SETTER(heap,h,i,v) do { \
  7790. DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.set = (v); \
  7791. } while (0)
  7792. #define DUK_HOBJECT_E_SET_FLAGS(heap,h,i,f) do { \
  7793. DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) = (f); \
  7794. } while (0)
  7795. #define DUK_HOBJECT_A_SET_VALUE(heap,h,i,v) do { \
  7796. DUK_HOBJECT_A_GET_VALUE((heap), (h), (i)) = (v); \
  7797. } while (0)
  7798. #define DUK_HOBJECT_A_SET_VALUE_TVAL(heap,h,i,v) \
  7799. DUK_HOBJECT_A_SET_VALUE((heap), (h), (i), (v)) /* alias for above */
  7800. #define DUK_HOBJECT_H_SET_INDEX(heap,h,i,v) do { \
  7801. DUK_HOBJECT_H_GET_INDEX((heap), (h), (i)) = (v); \
  7802. } while (0)
  7803. #define DUK_HOBJECT_E_SET_FLAG_BITS(heap,h,i,mask) do { \
  7804. DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)] |= (mask); \
  7805. } while (0)
  7806. #define DUK_HOBJECT_E_CLEAR_FLAG_BITS(heap,h,i,mask) do { \
  7807. DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)] &= ~(mask); \
  7808. } while (0)
  7809. #define DUK_HOBJECT_E_SLOT_IS_WRITABLE(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_WRITABLE) != 0)
  7810. #define DUK_HOBJECT_E_SLOT_IS_ENUMERABLE(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_ENUMERABLE) != 0)
  7811. #define DUK_HOBJECT_E_SLOT_IS_CONFIGURABLE(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_CONFIGURABLE) != 0)
  7812. #define DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_ACCESSOR) != 0)
  7813. #define DUK_HOBJECT_E_SLOT_SET_WRITABLE(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_WRITABLE)
  7814. #define DUK_HOBJECT_E_SLOT_SET_ENUMERABLE(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ENUMERABLE)
  7815. #define DUK_HOBJECT_E_SLOT_SET_CONFIGURABLE(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_CONFIGURABLE)
  7816. #define DUK_HOBJECT_E_SLOT_SET_ACCESSOR(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ACCESSOR)
  7817. #define DUK_HOBJECT_E_SLOT_CLEAR_WRITABLE(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_WRITABLE)
  7818. #define DUK_HOBJECT_E_SLOT_CLEAR_ENUMERABLE(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ENUMERABLE)
  7819. #define DUK_HOBJECT_E_SLOT_CLEAR_CONFIGURABLE(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_CONFIGURABLE)
  7820. #define DUK_HOBJECT_E_SLOT_CLEAR_ACCESSOR(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ACCESSOR)
  7821. #define DUK_PROPDESC_IS_WRITABLE(p) (((p)->flags & DUK_PROPDESC_FLAG_WRITABLE) != 0)
  7822. #define DUK_PROPDESC_IS_ENUMERABLE(p) (((p)->flags & DUK_PROPDESC_FLAG_ENUMERABLE) != 0)
  7823. #define DUK_PROPDESC_IS_CONFIGURABLE(p) (((p)->flags & DUK_PROPDESC_FLAG_CONFIGURABLE) != 0)
  7824. #define DUK_PROPDESC_IS_ACCESSOR(p) (((p)->flags & DUK_PROPDESC_FLAG_ACCESSOR) != 0)
  7825. #define DUK_HOBJECT_HASHIDX_UNUSED 0xffffffffUL
  7826. #define DUK_HOBJECT_HASHIDX_DELETED 0xfffffffeUL
  7827. /*
  7828. * Macros for accessing size fields
  7829. */
  7830. #if defined(DUK_USE_OBJSIZES16)
  7831. #define DUK_HOBJECT_GET_ESIZE(h) ((h)->e_size16)
  7832. #define DUK_HOBJECT_SET_ESIZE(h,v) do { (h)->e_size16 = (v); } while (0)
  7833. #define DUK_HOBJECT_GET_ENEXT(h) ((h)->e_next16)
  7834. #define DUK_HOBJECT_SET_ENEXT(h,v) do { (h)->e_next16 = (v); } while (0)
  7835. #define DUK_HOBJECT_POSTINC_ENEXT(h) ((h)->e_next16++)
  7836. #define DUK_HOBJECT_GET_ASIZE(h) ((h)->a_size16)
  7837. #define DUK_HOBJECT_SET_ASIZE(h,v) do { (h)->a_size16 = (v); } while (0)
  7838. #if defined(DUK_USE_HOBJECT_HASH_PART)
  7839. #define DUK_HOBJECT_GET_HSIZE(h) ((h)->h_size16)
  7840. #define DUK_HOBJECT_SET_HSIZE(h,v) do { (h)->h_size16 = (v); } while (0)
  7841. #else
  7842. #define DUK_HOBJECT_GET_HSIZE(h) 0
  7843. #define DUK_HOBJECT_SET_HSIZE(h,v) do { DUK_ASSERT((v) == 0); } while (0)
  7844. #endif
  7845. #else
  7846. #define DUK_HOBJECT_GET_ESIZE(h) ((h)->e_size)
  7847. #define DUK_HOBJECT_SET_ESIZE(h,v) do { (h)->e_size = (v); } while (0)
  7848. #define DUK_HOBJECT_GET_ENEXT(h) ((h)->e_next)
  7849. #define DUK_HOBJECT_SET_ENEXT(h,v) do { (h)->e_next = (v); } while (0)
  7850. #define DUK_HOBJECT_POSTINC_ENEXT(h) ((h)->e_next++)
  7851. #define DUK_HOBJECT_GET_ASIZE(h) ((h)->a_size)
  7852. #define DUK_HOBJECT_SET_ASIZE(h,v) do { (h)->a_size = (v); } while (0)
  7853. #if defined(DUK_USE_HOBJECT_HASH_PART)
  7854. #define DUK_HOBJECT_GET_HSIZE(h) ((h)->h_size)
  7855. #define DUK_HOBJECT_SET_HSIZE(h,v) do { (h)->h_size = (v); } while (0)
  7856. #else
  7857. #define DUK_HOBJECT_GET_HSIZE(h) 0
  7858. #define DUK_HOBJECT_SET_HSIZE(h,v) do { DUK_ASSERT((v) == 0); } while (0)
  7859. #endif
  7860. #endif
  7861. /*
  7862. * Misc
  7863. */
  7864. /* Maximum prototype traversal depth. Sanity limit which handles e.g.
  7865. * prototype loops (even complex ones like 1->2->3->4->2->3->4->2->3->4).
  7866. */
  7867. #define DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY 10000L
  7868. /* Maximum traversal depth for "bound function" chains. */
  7869. #define DUK_HOBJECT_BOUND_CHAIN_SANITY 10000L
  7870. /*
  7871. * Ecmascript [[Class]]
  7872. */
  7873. /* range check not necessary because all 4-bit values are mapped */
  7874. #define DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(n) duk_class_number_to_stridx[(n)]
  7875. #define DUK_HOBJECT_GET_CLASS_STRING(heap,h) \
  7876. DUK_HEAP_GET_STRING( \
  7877. (heap), \
  7878. DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(DUK_HOBJECT_GET_CLASS_NUMBER((h))) \
  7879. )
  7880. /*
  7881. * Macros for property handling
  7882. */
  7883. #if defined(DUK_USE_HEAPPTR16)
  7884. #define DUK_HOBJECT_GET_PROTOTYPE(heap,h) \
  7885. ((duk_hobject *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->prototype16))
  7886. #define DUK_HOBJECT_SET_PROTOTYPE(heap,h,x) do { \
  7887. (h)->prototype16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (x)); \
  7888. } while (0)
  7889. #else
  7890. #define DUK_HOBJECT_GET_PROTOTYPE(heap,h) \
  7891. ((h)->prototype)
  7892. #define DUK_HOBJECT_SET_PROTOTYPE(heap,h,x) do { \
  7893. (h)->prototype = (x); \
  7894. } while (0)
  7895. #endif
  7896. /* note: this updates refcounts */
  7897. #define DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr,h,p) duk_hobject_set_prototype_updref((thr), (h), (p))
  7898. /*
  7899. * Resizing and hash behavior
  7900. */
  7901. /* Sanity limit on max number of properties (allocated, not necessarily used).
  7902. * This is somewhat arbitrary, but if we're close to 2**32 properties some
  7903. * algorithms will fail (e.g. hash size selection, next prime selection).
  7904. * Also, we use negative array/entry table indices to indicate 'not found',
  7905. * so anything above 0x80000000 will cause trouble now.
  7906. */
  7907. #if defined(DUK_USE_OBJSIZES16)
  7908. #define DUK_HOBJECT_MAX_PROPERTIES 0x0000ffffUL
  7909. #else
  7910. #define DUK_HOBJECT_MAX_PROPERTIES 0x7fffffffUL /* 2**31-1 ~= 2G properties */
  7911. #endif
  7912. /* higher value conserves memory; also note that linear scan is cache friendly */
  7913. #define DUK_HOBJECT_E_USE_HASH_LIMIT 32
  7914. /* hash size relative to entries size: for value X, approx. hash_prime(e_size + e_size / X) */
  7915. #define DUK_HOBJECT_H_SIZE_DIVISOR 4 /* hash size approx. 1.25 times entries size */
  7916. /* if new_size < L * old_size, resize without abandon check; L = 3-bit fixed point, e.g. 9 -> 9/8 = 112.5% */
  7917. #define DUK_HOBJECT_A_FAST_RESIZE_LIMIT 9 /* 112.5%, i.e. new size less than 12.5% higher -> fast resize */
  7918. /* if density < L, abandon array part, L = 3-bit fixed point, e.g. 2 -> 2/8 = 25% */
  7919. /* limit is quite low: one array entry is 8 bytes, one normal entry is 4+1+8+4 = 17 bytes (with hash entry) */
  7920. #define DUK_HOBJECT_A_ABANDON_LIMIT 2 /* 25%, i.e. less than 25% used -> abandon */
  7921. /* internal align target for props allocation, must be 2*n for some n */
  7922. #if (DUK_USE_ALIGN_BY == 4)
  7923. #define DUK_HOBJECT_ALIGN_TARGET 4
  7924. #elif (DUK_USE_ALIGN_BY == 8)
  7925. #define DUK_HOBJECT_ALIGN_TARGET 8
  7926. #elif (DUK_USE_ALIGN_BY == 1)
  7927. #define DUK_HOBJECT_ALIGN_TARGET 1
  7928. #else
  7929. #error invalid DUK_USE_ALIGN_BY
  7930. #endif
  7931. /* controls for minimum entry part growth */
  7932. #define DUK_HOBJECT_E_MIN_GROW_ADD 16
  7933. #define DUK_HOBJECT_E_MIN_GROW_DIVISOR 8 /* 2^3 -> 1/8 = 12.5% min growth */
  7934. /* controls for minimum array part growth */
  7935. #define DUK_HOBJECT_A_MIN_GROW_ADD 16
  7936. #define DUK_HOBJECT_A_MIN_GROW_DIVISOR 8 /* 2^3 -> 1/8 = 12.5% min growth */
  7937. /* probe sequence */
  7938. #define DUK_HOBJECT_HASH_INITIAL(hash,h_size) ((hash) % (h_size))
  7939. #define DUK_HOBJECT_HASH_PROBE_STEP(hash) DUK_UTIL_GET_HASH_PROBE_STEP((hash))
  7940. /*
  7941. * PC-to-line constants
  7942. */
  7943. #define DUK_PC2LINE_SKIP 64
  7944. /* maximum length for a SKIP-1 diffstream: 35 bits per entry, rounded up to bytes */
  7945. #define DUK_PC2LINE_MAX_DIFF_LENGTH (((DUK_PC2LINE_SKIP - 1) * 35 + 7) / 8)
  7946. /*
  7947. * Struct defs
  7948. */
  7949. struct duk_propaccessor {
  7950. duk_hobject *get;
  7951. duk_hobject *set;
  7952. };
  7953. union duk_propvalue {
  7954. /* The get/set pointers could be 16-bit pointer compressed but it
  7955. * would make no difference on 32-bit platforms because duk_tval is
  7956. * 8 bytes or more anyway.
  7957. */
  7958. duk_tval v;
  7959. duk_propaccessor a;
  7960. };
  7961. struct duk_propdesc {
  7962. /* read-only values 'lifted' for ease of use */
  7963. duk_small_int_t flags;
  7964. duk_hobject *get;
  7965. duk_hobject *set;
  7966. /* for updating (all are set to < 0 for virtual properties) */
  7967. duk_int_t e_idx; /* prop index in 'entry part', < 0 if not there */
  7968. duk_int_t h_idx; /* prop index in 'hash part', < 0 if not there */
  7969. duk_int_t a_idx; /* prop index in 'array part', < 0 if not there */
  7970. };
  7971. struct duk_hobject {
  7972. duk_heaphdr hdr;
  7973. /*
  7974. * 'props' contains {key,value,flags} entries, optional array entries, and
  7975. * an optional hash lookup table for non-array entries in a single 'sliced'
  7976. * allocation. There are several layout options, which differ slightly in
  7977. * generated code size/speed and alignment/padding; duk_features.h selects
  7978. * the layout used.
  7979. *
  7980. * Layout 1 (DUK_USE_HOBJECT_LAYOUT_1):
  7981. *
  7982. * e_size * sizeof(duk_hstring *) bytes of entry keys (e_next gc reachable)
  7983. * e_size * sizeof(duk_propvalue) bytes of entry values (e_next gc reachable)
  7984. * e_size * sizeof(duk_uint8_t) bytes of entry flags (e_next gc reachable)
  7985. * a_size * sizeof(duk_tval) bytes of (opt) array values (plain only) (all gc reachable)
  7986. * h_size * sizeof(duk_uint32_t) bytes of (opt) hash indexes to entries (e_size),
  7987. * 0xffffffffUL = unused, 0xfffffffeUL = deleted
  7988. *
  7989. * Layout 2 (DUK_USE_HOBJECT_LAYOUT_2):
  7990. *
  7991. * e_size * sizeof(duk_propvalue) bytes of entry values (e_next gc reachable)
  7992. * e_size * sizeof(duk_hstring *) bytes of entry keys (e_next gc reachable)
  7993. * e_size * sizeof(duk_uint8_t) + pad bytes of entry flags (e_next gc reachable)
  7994. * a_size * sizeof(duk_tval) bytes of (opt) array values (plain only) (all gc reachable)
  7995. * h_size * sizeof(duk_uint32_t) bytes of (opt) hash indexes to entries (e_size),
  7996. * 0xffffffffUL = unused, 0xfffffffeUL = deleted
  7997. *
  7998. * Layout 3 (DUK_USE_HOBJECT_LAYOUT_3):
  7999. *
  8000. * e_size * sizeof(duk_propvalue) bytes of entry values (e_next gc reachable)
  8001. * a_size * sizeof(duk_tval) bytes of (opt) array values (plain only) (all gc reachable)
  8002. * e_size * sizeof(duk_hstring *) bytes of entry keys (e_next gc reachable)
  8003. * h_size * sizeof(duk_uint32_t) bytes of (opt) hash indexes to entries (e_size),
  8004. * 0xffffffffUL = unused, 0xfffffffeUL = deleted
  8005. * e_size * sizeof(duk_uint8_t) bytes of entry flags (e_next gc reachable)
  8006. *
  8007. * In layout 1, the 'e_next' count is rounded to 4 or 8 on platforms
  8008. * requiring 4 or 8 byte alignment. This ensures proper alignment
  8009. * for the entries, at the cost of memory footprint. However, it's
  8010. * probably preferable to use another layout on such platforms instead.
  8011. *
  8012. * In layout 2, the key and value parts are swapped to avoid padding
  8013. * the key array on platforms requiring alignment by 8. The flags part
  8014. * is padded to get alignment for array entries. The 'e_next' count does
  8015. * not need to be rounded as in layout 1.
  8016. *
  8017. * In layout 3, entry values and array values are always aligned properly,
  8018. * and assuming pointers are at most 8 bytes, so are the entry keys. Hash
  8019. * indices will be properly aligned (assuming pointers are at least 4 bytes).
  8020. * Finally, flags don't need additional alignment. This layout provides
  8021. * compact allocations without padding (even on platforms with alignment
  8022. * requirements) at the cost of a bit slower lookups.
  8023. *
  8024. * Objects with few keys don't have a hash index; keys are looked up linearly,
  8025. * which is cache efficient because the keys are consecutive. Larger objects
  8026. * have a hash index part which contains integer indexes to the entries part.
  8027. *
  8028. * A single allocation reduces memory allocation overhead but requires more
  8029. * work when any part needs to be resized. A sliced allocation for entries
  8030. * makes linear key matching faster on most platforms (more locality) and
  8031. * skimps on flags size (which would be followed by 3 bytes of padding in
  8032. * most architectures if entries were placed in a struct).
  8033. *
  8034. * 'props' also contains internal properties distinguished with a non-BMP
  8035. * prefix. Often used properties should be placed early in 'props' whenever
  8036. * possible to make accessing them as fast a possible.
  8037. */
  8038. #if defined(DUK_USE_HEAPPTR16)
  8039. /* Located in duk_heaphdr h_extra16. Subclasses of duk_hobject (like
  8040. * duk_hcompiledfunction) are not free to use h_extra16 for this reason.
  8041. */
  8042. #else
  8043. duk_uint8_t *props;
  8044. #endif
  8045. /* prototype: the only internal property lifted outside 'e' as it is so central */
  8046. #if defined(DUK_USE_HEAPPTR16)
  8047. duk_uint16_t prototype16;
  8048. #else
  8049. duk_hobject *prototype;
  8050. #endif
  8051. #if defined(DUK_USE_OBJSIZES16)
  8052. duk_uint16_t e_size16;
  8053. duk_uint16_t e_next16;
  8054. duk_uint16_t a_size16;
  8055. #if defined(DUK_USE_HOBJECT_HASH_PART)
  8056. duk_uint16_t h_size16;
  8057. #endif
  8058. #else
  8059. duk_uint32_t e_size; /* entry part size */
  8060. duk_uint32_t e_next; /* index for next new key ([0,e_next[ are gc reachable) */
  8061. duk_uint32_t a_size; /* array part size (entirely gc reachable) */
  8062. #if defined(DUK_USE_HOBJECT_HASH_PART)
  8063. duk_uint32_t h_size; /* hash part size or 0 if unused */
  8064. #endif
  8065. #endif
  8066. };
  8067. /*
  8068. * Exposed data
  8069. */
  8070. #if !defined(DUK_SINGLE_FILE)
  8071. DUK_INTERNAL_DECL duk_uint8_t duk_class_number_to_stridx[32];
  8072. #endif /* !DUK_SINGLE_FILE */
  8073. /*
  8074. * Prototypes
  8075. */
  8076. /* alloc and init */
  8077. DUK_INTERNAL_DECL duk_hobject *duk_hobject_alloc(duk_heap *heap, duk_uint_t hobject_flags);
  8078. #if 0 /* unused */
  8079. DUK_INTERNAL_DECL duk_hobject *duk_hobject_alloc_checked(duk_hthread *thr, duk_uint_t hobject_flags);
  8080. #endif
  8081. DUK_INTERNAL_DECL duk_hcompiledfunction *duk_hcompiledfunction_alloc(duk_heap *heap, duk_uint_t hobject_flags);
  8082. DUK_INTERNAL_DECL duk_hnativefunction *duk_hnativefunction_alloc(duk_heap *heap, duk_uint_t hobject_flags);
  8083. DUK_INTERNAL duk_hbufferobject *duk_hbufferobject_alloc(duk_heap *heap, duk_uint_t hobject_flags);
  8084. DUK_INTERNAL_DECL duk_hthread *duk_hthread_alloc(duk_heap *heap, duk_uint_t hobject_flags);
  8085. /* low-level property functions */
  8086. DUK_INTERNAL_DECL void duk_hobject_find_existing_entry(duk_heap *heap, duk_hobject *obj, duk_hstring *key, duk_int_t *e_idx, duk_int_t *h_idx);
  8087. DUK_INTERNAL_DECL duk_tval *duk_hobject_find_existing_entry_tval_ptr(duk_heap *heap, duk_hobject *obj, duk_hstring *key);
  8088. DUK_INTERNAL_DECL duk_tval *duk_hobject_find_existing_entry_tval_ptr_and_attrs(duk_heap *heap, duk_hobject *obj, duk_hstring *key, duk_int_t *out_attrs);
  8089. DUK_INTERNAL_DECL duk_tval *duk_hobject_find_existing_array_entry_tval_ptr(duk_heap *heap, duk_hobject *obj, duk_uarridx_t i);
  8090. /* XXX: when optimizing for guaranteed property slots, use a guaranteed
  8091. * slot for internal value; this call can then access it directly.
  8092. */
  8093. #define duk_hobject_get_internal_value_tval_ptr(heap,obj) \
  8094. duk_hobject_find_existing_entry_tval_ptr((heap), (obj), DUK_HEAP_STRING_INT_VALUE((heap)))
  8095. /* core property functions */
  8096. DUK_INTERNAL_DECL duk_bool_t duk_hobject_getprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key);
  8097. 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);
  8098. 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);
  8099. DUK_INTERNAL_DECL duk_bool_t duk_hobject_hasprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key);
  8100. /* internal property functions */
  8101. #define DUK_DELPROP_FLAG_THROW (1 << 0)
  8102. #define DUK_DELPROP_FLAG_FORCE (1 << 1)
  8103. DUK_INTERNAL_DECL duk_bool_t duk_hobject_delprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags);
  8104. DUK_INTERNAL_DECL duk_bool_t duk_hobject_hasprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key);
  8105. DUK_INTERNAL_DECL void duk_hobject_define_property_internal(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags);
  8106. 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);
  8107. DUK_INTERNAL_DECL void duk_hobject_define_accessor_internal(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_hobject *getter, duk_hobject *setter, duk_small_uint_t propflags);
  8108. DUK_INTERNAL_DECL void duk_hobject_set_length(duk_hthread *thr, duk_hobject *obj, duk_uint32_t length); /* XXX: duk_uarridx_t? */
  8109. DUK_INTERNAL_DECL void duk_hobject_set_length_zero(duk_hthread *thr, duk_hobject *obj);
  8110. DUK_INTERNAL_DECL duk_uint32_t duk_hobject_get_length(duk_hthread *thr, duk_hobject *obj); /* XXX: duk_uarridx_t? */
  8111. /* helpers for defineProperty() and defineProperties() */
  8112. DUK_INTERNAL_DECL
  8113. void duk_hobject_prepare_property_descriptor(duk_context *ctx,
  8114. duk_idx_t idx_in,
  8115. duk_uint_t *out_defprop_flags,
  8116. duk_idx_t *out_idx_value,
  8117. duk_hobject **out_getter,
  8118. duk_hobject **out_setter);
  8119. DUK_INTERNAL_DECL
  8120. void duk_hobject_define_property_helper(duk_context *ctx,
  8121. duk_uint_t defprop_flags,
  8122. duk_hobject *obj,
  8123. duk_hstring *key,
  8124. duk_idx_t idx_value,
  8125. duk_hobject *get,
  8126. duk_hobject *set);
  8127. /* Object built-in methods */
  8128. DUK_INTERNAL_DECL duk_ret_t duk_hobject_object_get_own_property_descriptor(duk_context *ctx);
  8129. DUK_INTERNAL_DECL void duk_hobject_object_seal_freeze_helper(duk_hthread *thr, duk_hobject *obj, duk_bool_t is_freeze);
  8130. DUK_INTERNAL_DECL duk_bool_t duk_hobject_object_is_sealed_frozen_helper(duk_hthread *thr, duk_hobject *obj, duk_bool_t is_frozen);
  8131. DUK_INTERNAL_DECL duk_bool_t duk_hobject_object_ownprop_helper(duk_context *ctx, duk_small_uint_t required_desc_flags);
  8132. /* internal properties */
  8133. DUK_INTERNAL_DECL duk_bool_t duk_hobject_get_internal_value(duk_heap *heap, duk_hobject *obj, duk_tval *tv);
  8134. DUK_INTERNAL_DECL duk_hstring *duk_hobject_get_internal_value_string(duk_heap *heap, duk_hobject *obj);
  8135. /* hobject management functions */
  8136. DUK_INTERNAL_DECL void duk_hobject_compact_props(duk_hthread *thr, duk_hobject *obj);
  8137. /* ES6 proxy */
  8138. #if defined(DUK_USE_ES6_PROXY)
  8139. DUK_INTERNAL_DECL duk_bool_t duk_hobject_proxy_check(duk_hthread *thr, duk_hobject *obj, duk_hobject **out_target, duk_hobject **out_handler);
  8140. DUK_INTERNAL_DECL duk_hobject *duk_hobject_resolve_proxy_target(duk_hthread *thr, duk_hobject *obj);
  8141. #endif
  8142. /* enumeration */
  8143. DUK_INTERNAL_DECL void duk_hobject_enumerator_create(duk_context *ctx, duk_small_uint_t enum_flags);
  8144. DUK_INTERNAL_DECL duk_ret_t duk_hobject_get_enumerated_keys(duk_context *ctx, duk_small_uint_t enum_flags);
  8145. DUK_INTERNAL_DECL duk_bool_t duk_hobject_enumerator_next(duk_context *ctx, duk_bool_t get_value);
  8146. /* macros */
  8147. DUK_INTERNAL_DECL void duk_hobject_set_prototype_updref(duk_hthread *thr, duk_hobject *h, duk_hobject *p);
  8148. /* finalization */
  8149. DUK_INTERNAL_DECL void duk_hobject_run_finalizer(duk_hthread *thr, duk_hobject *obj);
  8150. /* pc2line */
  8151. #if defined(DUK_USE_PC2LINE)
  8152. DUK_INTERNAL_DECL void duk_hobject_pc2line_pack(duk_hthread *thr, duk_compiler_instr *instrs, duk_uint_fast32_t length);
  8153. DUK_INTERNAL_DECL duk_uint_fast32_t duk_hobject_pc2line_query(duk_context *ctx, duk_idx_t idx_func, duk_uint_fast32_t pc);
  8154. #endif
  8155. /* misc */
  8156. 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);
  8157. #endif /* DUK_HOBJECT_H_INCLUDED */
  8158. #line 1 "duk_hcompiledfunction.h"
  8159. /*
  8160. * Heap compiled function (Ecmascript function) representation.
  8161. *
  8162. * There is a single data buffer containing the Ecmascript function's
  8163. * bytecode, constants, and inner functions.
  8164. */
  8165. #ifndef DUK_HCOMPILEDFUNCTION_H_INCLUDED
  8166. #define DUK_HCOMPILEDFUNCTION_H_INCLUDED
  8167. /*
  8168. * Field accessor macros
  8169. */
  8170. /* XXX: casts could be improved, especially for GET/SET DATA */
  8171. #if defined(DUK_USE_HEAPPTR16)
  8172. #define DUK_HCOMPILEDFUNCTION_GET_DATA(heap,h) \
  8173. ((duk_hbuffer_fixed *) (void *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->data16))
  8174. #define DUK_HCOMPILEDFUNCTION_SET_DATA(heap,h,v) do { \
  8175. (h)->data16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \
  8176. } while (0)
  8177. #define DUK_HCOMPILEDFUNCTION_GET_FUNCS(heap,h) \
  8178. ((duk_hobject **) (void *) (DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->funcs16)))
  8179. #define DUK_HCOMPILEDFUNCTION_SET_FUNCS(heap,h,v) do { \
  8180. (h)->funcs16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \
  8181. } while (0)
  8182. #define DUK_HCOMPILEDFUNCTION_GET_BYTECODE(heap,h) \
  8183. ((duk_instr_t *) (void *) (DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->bytecode16)))
  8184. #define DUK_HCOMPILEDFUNCTION_SET_BYTECODE(heap,h,v) do { \
  8185. (h)->bytecode16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \
  8186. } while (0)
  8187. #else
  8188. #define DUK_HCOMPILEDFUNCTION_GET_DATA(heap,h) \
  8189. ((duk_hbuffer_fixed *) (void *) (h)->data)
  8190. #define DUK_HCOMPILEDFUNCTION_SET_DATA(heap,h,v) do { \
  8191. (h)->data = (duk_hbuffer *) (v); \
  8192. } while (0)
  8193. #define DUK_HCOMPILEDFUNCTION_GET_FUNCS(heap,h) \
  8194. ((h)->funcs)
  8195. #define DUK_HCOMPILEDFUNCTION_SET_FUNCS(heap,h,v) do { \
  8196. (h)->funcs = (v); \
  8197. } while (0)
  8198. #define DUK_HCOMPILEDFUNCTION_GET_BYTECODE(heap,h) \
  8199. ((h)->bytecode)
  8200. #define DUK_HCOMPILEDFUNCTION_SET_BYTECODE(heap,h,v) do { \
  8201. (h)->bytecode = (v); \
  8202. } while (0)
  8203. #endif
  8204. /*
  8205. * Accessor macros for function specific data areas
  8206. */
  8207. /* Note: assumes 'data' is always a fixed buffer */
  8208. #define DUK_HCOMPILEDFUNCTION_GET_BUFFER_BASE(heap,h) \
  8209. DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), DUK_HCOMPILEDFUNCTION_GET_DATA((heap), (h)))
  8210. #define DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(heap,h) \
  8211. ((duk_tval *) (void *) DUK_HCOMPILEDFUNCTION_GET_BUFFER_BASE((heap), (h)))
  8212. #define DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(heap,h) \
  8213. DUK_HCOMPILEDFUNCTION_GET_FUNCS((heap), (h))
  8214. #define DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(heap,h) \
  8215. DUK_HCOMPILEDFUNCTION_GET_BYTECODE((heap), (h))
  8216. #define DUK_HCOMPILEDFUNCTION_GET_CONSTS_END(heap,h) \
  8217. ((duk_tval *) (void *) DUK_HCOMPILEDFUNCTION_GET_FUNCS((heap), (h)))
  8218. #define DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(heap,h) \
  8219. ((duk_hobject **) (void *) DUK_HCOMPILEDFUNCTION_GET_BYTECODE((heap), (h)))
  8220. /* XXX: double evaluation of DUK_HCOMPILEDFUNCTION_GET_DATA() */
  8221. #define DUK_HCOMPILEDFUNCTION_GET_CODE_END(heap,h) \
  8222. ((duk_instr_t *) (void *) (DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), DUK_HCOMPILEDFUNCTION_GET_DATA((heap), (h))) + \
  8223. DUK_HBUFFER_GET_SIZE((duk_hbuffer *) DUK_HCOMPILEDFUNCTION_GET_DATA((heap), h))))
  8224. #define DUK_HCOMPILEDFUNCTION_GET_CONSTS_SIZE(heap,h) \
  8225. ( \
  8226. (duk_size_t) \
  8227. ( \
  8228. ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_END((heap), (h))) - \
  8229. ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE((heap), (h))) \
  8230. ) \
  8231. )
  8232. #define DUK_HCOMPILEDFUNCTION_GET_FUNCS_SIZE(heap,h) \
  8233. ( \
  8234. (duk_size_t) \
  8235. ( \
  8236. ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_END((heap), (h))) - \
  8237. ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE((heap), (h))) \
  8238. ) \
  8239. )
  8240. #define DUK_HCOMPILEDFUNCTION_GET_CODE_SIZE(heap,h) \
  8241. ( \
  8242. (duk_size_t) \
  8243. ( \
  8244. ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_CODE_END((heap),(h))) - \
  8245. ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_CODE_BASE((heap),(h))) \
  8246. ) \
  8247. )
  8248. #define DUK_HCOMPILEDFUNCTION_GET_CONSTS_COUNT(heap,h) \
  8249. ((duk_size_t) (DUK_HCOMPILEDFUNCTION_GET_CONSTS_SIZE((heap), (h)) / sizeof(duk_tval)))
  8250. #define DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(heap,h) \
  8251. ((duk_size_t) (DUK_HCOMPILEDFUNCTION_GET_FUNCS_SIZE((heap), (h)) / sizeof(duk_hobject *)))
  8252. #define DUK_HCOMPILEDFUNCTION_GET_CODE_COUNT(heap,h) \
  8253. ((duk_size_t) (DUK_HCOMPILEDFUNCTION_GET_CODE_SIZE((heap), (h)) / sizeof(duk_instr_t)))
  8254. /*
  8255. * Main struct
  8256. */
  8257. struct duk_hcompiledfunction {
  8258. /* shared object part */
  8259. duk_hobject obj;
  8260. /*
  8261. * Pointers to function data area for faster access. Function
  8262. * data is a buffer shared between all closures of the same
  8263. * "template" function. The data buffer is always fixed (non-
  8264. * dynamic, hence stable), with a layout as follows:
  8265. *
  8266. * constants (duk_tval)
  8267. * inner functions (duk_hobject *)
  8268. * bytecode (duk_instr_t)
  8269. *
  8270. * Note: bytecode end address can be computed from 'data' buffer
  8271. * size. It is not strictly necessary functionally, assuming
  8272. * bytecode never jumps outside its allocated area. However,
  8273. * it's a safety/robustness feature for avoiding the chance of
  8274. * executing random data as bytecode due to a compiler error.
  8275. *
  8276. * Note: values in the data buffer must be incref'd (they will
  8277. * be decref'd on release) for every compiledfunction referring
  8278. * to the 'data' element.
  8279. */
  8280. /* Data area, fixed allocation, stable data ptrs. */
  8281. #if defined(DUK_USE_HEAPPTR16)
  8282. duk_uint16_t data16;
  8283. #else
  8284. duk_hbuffer *data;
  8285. #endif
  8286. /* No need for constants pointer (= same as data).
  8287. *
  8288. * When using 16-bit packing alignment to 4 is nice. 'funcs' will be
  8289. * 4-byte aligned because 'constants' are duk_tvals. For now the
  8290. * inner function pointers are not compressed, so that 'bytecode' will
  8291. * also be 4-byte aligned.
  8292. */
  8293. #if defined(DUK_USE_HEAPPTR16)
  8294. duk_uint16_t funcs16;
  8295. duk_uint16_t bytecode16;
  8296. #else
  8297. duk_hobject **funcs;
  8298. duk_instr_t *bytecode;
  8299. #endif
  8300. /*
  8301. * 'nregs' registers are allocated on function entry, at most 'nargs'
  8302. * are initialized to arguments, and the rest to undefined. Arguments
  8303. * above 'nregs' are not mapped to registers. All registers in the
  8304. * active stack range must be initialized because they are GC reachable.
  8305. * 'nargs' is needed so that if the function is given more than 'nargs'
  8306. * arguments, the additional arguments do not 'clobber' registers
  8307. * beyond 'nregs' which must be consistently initialized to undefined.
  8308. *
  8309. * Usually there is no need to know which registers are mapped to
  8310. * local variables. Registers may be allocated to variable in any
  8311. * way (even including gaps). However, a register-variable mapping
  8312. * must be the same for the duration of the function execution and
  8313. * the register cannot be used for anything else.
  8314. *
  8315. * When looking up variables by name, the '_Varmap' map is used.
  8316. * When an activation closes, registers mapped to arguments are
  8317. * copied into the environment record based on the same map. The
  8318. * reverse map (from register to variable) is not currently needed
  8319. * at run time, except for debugging, so it is not maintained.
  8320. */
  8321. duk_uint16_t nregs; /* regs to allocate */
  8322. duk_uint16_t nargs; /* number of arguments allocated to regs */
  8323. /*
  8324. * Additional control information is placed into the object itself
  8325. * as internal properties to avoid unnecessary fields for the
  8326. * majority of functions. The compiler tries to omit internal
  8327. * control fields when possible.
  8328. *
  8329. * Function templates:
  8330. *
  8331. * {
  8332. * name: "func", // declaration, named function expressions
  8333. * fileName: <debug info for creating nice errors>
  8334. * _Varmap: { "arg1": 0, "arg2": 1, "varname": 2 },
  8335. * _Formals: [ "arg1", "arg2" ],
  8336. * _Source: "function func(arg1, arg2) { ... }",
  8337. * _Pc2line: <debug info for pc-to-line mapping>,
  8338. * }
  8339. *
  8340. * Function instances:
  8341. *
  8342. * {
  8343. * length: 2,
  8344. * prototype: { constructor: <func> },
  8345. * caller: <thrower>,
  8346. * arguments: <thrower>,
  8347. * name: "func", // declaration, named function expressions
  8348. * fileName: <debug info for creating nice errors>
  8349. * _Varmap: { "arg1": 0, "arg2": 1, "varname": 2 },
  8350. * _Formals: [ "arg1", "arg2" ],
  8351. * _Source: "function func(arg1, arg2) { ... }",
  8352. * _Pc2line: <debug info for pc-to-line mapping>,
  8353. * _Varenv: <variable environment of closure>,
  8354. * _Lexenv: <lexical environment of closure (if differs from _Varenv)>
  8355. * }
  8356. *
  8357. * More detailed description of these properties can be found
  8358. * in the documentation.
  8359. */
  8360. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  8361. /* Line number range for function. Needed during debugging to
  8362. * determine active breakpoints.
  8363. */
  8364. duk_uint32_t start_line;
  8365. duk_uint32_t end_line;
  8366. #endif
  8367. };
  8368. #endif /* DUK_HCOMPILEDFUNCTION_H_INCLUDED */
  8369. #line 1 "duk_hnativefunction.h"
  8370. /*
  8371. * Heap native function representation.
  8372. */
  8373. #ifndef DUK_HNATIVEFUNCTION_H_INCLUDED
  8374. #define DUK_HNATIVEFUNCTION_H_INCLUDED
  8375. #define DUK_HNATIVEFUNCTION_NARGS_VARARGS ((duk_int16_t) -1)
  8376. #define DUK_HNATIVEFUNCTION_NARGS_MAX ((duk_int16_t) 0x7fff)
  8377. struct duk_hnativefunction {
  8378. /* shared object part */
  8379. duk_hobject obj;
  8380. duk_c_function func;
  8381. duk_int16_t nargs;
  8382. duk_int16_t magic;
  8383. /* The 'magic' field allows an opaque 16-bit field to be accessed by the
  8384. * Duktape/C function. This allows, for instance, the same native function
  8385. * to be used for a set of very similar functions, with the 'magic' field
  8386. * providing the necessary non-argument flags / values to guide the behavior
  8387. * of the native function. The value is signed on purpose: it is easier to
  8388. * convert a signed value to unsigned (simply AND with 0xffff) than vice
  8389. * versa.
  8390. *
  8391. * Note: cannot place nargs/magic into the heaphdr flags, because
  8392. * duk_hobject takes almost all flags already (and needs the spare).
  8393. */
  8394. };
  8395. #endif /* DUK_HNATIVEFUNCTION_H_INCLUDED */
  8396. #line 1 "duk_hbufferobject.h"
  8397. /*
  8398. * Heap Buffer object representation. Used for all Buffer variants.
  8399. */
  8400. #ifndef DUK_HBUFFEROBJECT_H_INCLUDED
  8401. #define DUK_HBUFFEROBJECT_H_INCLUDED
  8402. /* All element accessors are host endian now (driven by TypedArray spec). */
  8403. #define DUK_HBUFFEROBJECT_ELEM_UINT8 0
  8404. #define DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED 1
  8405. #define DUK_HBUFFEROBJECT_ELEM_INT8 2
  8406. #define DUK_HBUFFEROBJECT_ELEM_UINT16 3
  8407. #define DUK_HBUFFEROBJECT_ELEM_INT16 4
  8408. #define DUK_HBUFFEROBJECT_ELEM_UINT32 5
  8409. #define DUK_HBUFFEROBJECT_ELEM_INT32 6
  8410. #define DUK_HBUFFEROBJECT_ELEM_FLOAT32 7
  8411. #define DUK_HBUFFEROBJECT_ELEM_FLOAT64 8
  8412. #define DUK_HBUFFEROBJECT_ELEM_MAX 8
  8413. #define DUK_ASSERT_HBUFFEROBJECT_VALID(h) do { \
  8414. DUK_ASSERT((h) != NULL); \
  8415. DUK_ASSERT((h)->shift <= 3); \
  8416. DUK_ASSERT((h)->elem_type <= DUK_HBUFFEROBJECT_ELEM_MAX); \
  8417. DUK_ASSERT(((h)->shift == 0 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8) || \
  8418. ((h)->shift == 0 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED) || \
  8419. ((h)->shift == 0 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_INT8) || \
  8420. ((h)->shift == 1 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT16) || \
  8421. ((h)->shift == 1 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_INT16) || \
  8422. ((h)->shift == 2 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT32) || \
  8423. ((h)->shift == 2 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_INT32) || \
  8424. ((h)->shift == 2 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_FLOAT32) || \
  8425. ((h)->shift == 3 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_FLOAT64)); \
  8426. DUK_ASSERT((h)->is_view == 0 || (h)->is_view == 1); \
  8427. DUK_ASSERT(DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) (h))); \
  8428. if ((h)->buf == NULL) { \
  8429. DUK_ASSERT((h)->offset == 0); \
  8430. DUK_ASSERT((h)->length == 0); \
  8431. } else { \
  8432. /* No assertions for offset or length; in particular, \
  8433. * it's OK for length to be longer than underlying \
  8434. * buffer. Just ensure they don't wrap when added. \
  8435. */ \
  8436. DUK_ASSERT((h)->offset + (h)->length >= (h)->offset); \
  8437. } \
  8438. } while (0)
  8439. /* Get the current data pointer (caller must ensure buf != NULL) as a
  8440. * duk_uint8_t ptr.
  8441. */
  8442. #define DUK_HBUFFEROBJECT_GET_SLICE_BASE(heap,h) \
  8443. (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \
  8444. (((duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR((heap), (h)->buf)) + (h)->offset))
  8445. /* True if slice is full, i.e. offset is zero and length covers the entire
  8446. * buffer. This status may change independently of the duk_hbufferobject if
  8447. * the underlying buffer is dynamic and changes without the hbufferobject
  8448. * being changed.
  8449. */
  8450. #define DUK_HBUFFEROBJECT_FULL_SLICE(h) \
  8451. (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \
  8452. ((h)->offset == 0 && (h)->length == DUK_HBUFFER_GET_SIZE((h)->buf)))
  8453. /* Validate that the whole slice [0,length[ is contained in the underlying
  8454. * buffer. Caller must ensure 'buf' != NULL.
  8455. */
  8456. #define DUK_HBUFFEROBJECT_VALID_SLICE(h) \
  8457. (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \
  8458. ((h)->offset + (h)->length <= DUK_HBUFFER_GET_SIZE((h)->buf)))
  8459. /* Validate byte read/write for virtual 'offset', i.e. check that the
  8460. * offset, taking into account h->offset, is within the underlying
  8461. * buffer size. This is a safety check which is needed to ensure
  8462. * that even a misconfigured duk_hbufferobject never causes memory
  8463. * unsafe behavior (e.g. if an underlying dynamic buffer changes
  8464. * after being setup). Caller must ensure 'buf' != NULL.
  8465. */
  8466. #define DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_INCL(h,off) \
  8467. (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \
  8468. ((h)->offset + (off) < DUK_HBUFFER_GET_SIZE((h)->buf)))
  8469. #define DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h,off) \
  8470. (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \
  8471. ((h)->offset + (off) <= DUK_HBUFFER_GET_SIZE((h)->buf)))
  8472. /* Clamp an input byte length (already assumed to be within the nominal
  8473. * duk_hbufferobject 'length') to the current dynamic buffer limits to
  8474. * yield a byte length limit that's safe for memory accesses. This value
  8475. * can be invalidated by any side effect because it may trigger a user
  8476. * callback that resizes the underlying buffer.
  8477. */
  8478. #define DUK_HBUFFEROBJECT_CLAMP_BYTELENGTH(h,len) \
  8479. (DUK_ASSERT_EXPR((h) != NULL), \
  8480. duk_hbufferobject_clamp_bytelength((h), (len)))
  8481. struct duk_hbufferobject {
  8482. /* Shared object part. */
  8483. duk_hobject obj;
  8484. /* Underlying buffer (refcounted), may be NULL. */
  8485. duk_hbuffer *buf;
  8486. /* Slice and accessor information.
  8487. *
  8488. * Because the underlying buffer may be dynamic, these may be
  8489. * invalidated by the buffer being modified so that both offset
  8490. * and length should be validated before every access. Behavior
  8491. * when the underlying buffer has changed doesn't need to be clean:
  8492. * virtual 'length' doesn't need to be affected, reads can return
  8493. * zero/NaN, and writes can be ignored.
  8494. *
  8495. * Note that a data pointer cannot be precomputed because 'buf' may
  8496. * be dynamic and its pointer unstable.
  8497. */
  8498. duk_uint_t offset; /* byte offset to buf */
  8499. duk_uint_t length; /* byte index limit for element access, exclusive */
  8500. duk_uint8_t shift; /* element size shift:
  8501. * 0 = u8/i8
  8502. * 1 = u16/i16
  8503. * 2 = u32/i32/float
  8504. * 3 = double
  8505. */
  8506. duk_uint8_t elem_type; /* element type */
  8507. duk_uint8_t is_view;
  8508. };
  8509. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  8510. DUK_INTERNAL_DECL duk_uint_t duk_hbufferobject_clamp_bytelength(duk_hbufferobject *h_bufobj, duk_uint_t len);
  8511. #endif
  8512. DUK_INTERNAL_DECL void duk_hbufferobject_push_validated_read(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size);
  8513. DUK_INTERNAL_DECL void duk_hbufferobject_validated_write(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size);
  8514. #endif /* DUK_HBUFFEROBJECT_H_INCLUDED */
  8515. #line 1 "duk_hthread.h"
  8516. /*
  8517. * Heap thread object representation.
  8518. *
  8519. * duk_hthread is also the 'context' (duk_context) for exposed APIs
  8520. * which mostly operate on the topmost frame of the value stack.
  8521. */
  8522. #ifndef DUK_HTHREAD_H_INCLUDED
  8523. #define DUK_HTHREAD_H_INCLUDED
  8524. /*
  8525. * Stack constants
  8526. */
  8527. #define DUK_VALSTACK_GROW_STEP 128 /* roughly 1 kiB */
  8528. #define DUK_VALSTACK_SHRINK_THRESHOLD 256 /* roughly 2 kiB */
  8529. #define DUK_VALSTACK_SHRINK_SPARE 64 /* roughly 0.5 kiB */
  8530. #define DUK_VALSTACK_INITIAL_SIZE 128 /* roughly 1.0 kiB -> but rounds up to DUK_VALSTACK_GROW_STEP in practice */
  8531. #define DUK_VALSTACK_INTERNAL_EXTRA 64 /* internal extra elements assumed on function entry,
  8532. * always added to user-defined 'extra' for e.g. the
  8533. * duk_check_stack() call.
  8534. */
  8535. #define DUK_VALSTACK_API_ENTRY_MINIMUM DUK_API_ENTRY_STACK
  8536. /* number of elements guaranteed to be user accessible
  8537. * (in addition to call arguments) on Duktape/C function entry.
  8538. */
  8539. /* Note: DUK_VALSTACK_INITIAL_SIZE must be >= DUK_VALSTACK_API_ENTRY_MINIMUM
  8540. * + DUK_VALSTACK_INTERNAL_EXTRA so that the initial stack conforms to spare
  8541. * requirements.
  8542. */
  8543. #define DUK_VALSTACK_DEFAULT_MAX 1000000L
  8544. #define DUK_CALLSTACK_GROW_STEP 8 /* roughly 256 bytes */
  8545. #define DUK_CALLSTACK_SHRINK_THRESHOLD 16 /* roughly 512 bytes */
  8546. #define DUK_CALLSTACK_SHRINK_SPARE 8 /* roughly 256 bytes */
  8547. #define DUK_CALLSTACK_INITIAL_SIZE 8
  8548. #define DUK_CALLSTACK_DEFAULT_MAX 10000L
  8549. #define DUK_CATCHSTACK_GROW_STEP 4 /* roughly 64 bytes */
  8550. #define DUK_CATCHSTACK_SHRINK_THRESHOLD 8 /* roughly 128 bytes */
  8551. #define DUK_CATCHSTACK_SHRINK_SPARE 4 /* roughly 64 bytes */
  8552. #define DUK_CATCHSTACK_INITIAL_SIZE 4
  8553. #define DUK_CATCHSTACK_DEFAULT_MAX 10000L
  8554. /*
  8555. * Activation defines
  8556. */
  8557. #define DUK_ACT_FLAG_STRICT (1 << 0) /* function executes in strict mode */
  8558. #define DUK_ACT_FLAG_TAILCALLED (1 << 1) /* activation has tail called one or more times */
  8559. #define DUK_ACT_FLAG_CONSTRUCT (1 << 2) /* function executes as a constructor (called via "new") */
  8560. #define DUK_ACT_FLAG_PREVENT_YIELD (1 << 3) /* activation prevents yield (native call or "new") */
  8561. #define DUK_ACT_FLAG_DIRECT_EVAL (1 << 4) /* activation is a direct eval call */
  8562. #define DUK_ACT_FLAG_BREAKPOINT_ACTIVE (1 << 5) /* activation has active breakpoint(s) */
  8563. #define DUK_ACT_GET_FUNC(act) ((act)->func)
  8564. /*
  8565. * Flags for __FILE__ / __LINE__ registered into tracedata
  8566. */
  8567. #define DUK_TB_FLAG_NOBLAME_FILELINE (1 << 0) /* don't report __FILE__ / __LINE__ as fileName/lineNumber */
  8568. /*
  8569. * Catcher defines
  8570. */
  8571. /* flags field: LLLLLLFT, L = label (24 bits), F = flags (4 bits), T = type (4 bits) */
  8572. #define DUK_CAT_TYPE_MASK 0x0000000fUL
  8573. #define DUK_CAT_TYPE_BITS 4
  8574. #define DUK_CAT_LABEL_MASK 0xffffff00UL
  8575. #define DUK_CAT_LABEL_BITS 24
  8576. #define DUK_CAT_LABEL_SHIFT 8
  8577. #define DUK_CAT_FLAG_CATCH_ENABLED (1 << 4) /* catch part will catch */
  8578. #define DUK_CAT_FLAG_FINALLY_ENABLED (1 << 5) /* finally part will catch */
  8579. #define DUK_CAT_FLAG_CATCH_BINDING_ENABLED (1 << 6) /* request to create catch binding */
  8580. #define DUK_CAT_FLAG_LEXENV_ACTIVE (1 << 7) /* catch or with binding is currently active */
  8581. #define DUK_CAT_TYPE_UNKNOWN 0
  8582. #define DUK_CAT_TYPE_TCF 1
  8583. #define DUK_CAT_TYPE_LABEL 2
  8584. #define DUK_CAT_GET_TYPE(c) ((c)->flags & DUK_CAT_TYPE_MASK)
  8585. #define DUK_CAT_GET_LABEL(c) (((c)->flags & DUK_CAT_LABEL_MASK) >> DUK_CAT_LABEL_SHIFT)
  8586. #define DUK_CAT_HAS_CATCH_ENABLED(c) ((c)->flags & DUK_CAT_FLAG_CATCH_ENABLED)
  8587. #define DUK_CAT_HAS_FINALLY_ENABLED(c) ((c)->flags & DUK_CAT_FLAG_FINALLY_ENABLED)
  8588. #define DUK_CAT_HAS_CATCH_BINDING_ENABLED(c) ((c)->flags & DUK_CAT_FLAG_CATCH_BINDING_ENABLED)
  8589. #define DUK_CAT_HAS_LEXENV_ACTIVE(c) ((c)->flags & DUK_CAT_FLAG_LEXENV_ACTIVE)
  8590. #define DUK_CAT_SET_CATCH_ENABLED(c) do { \
  8591. (c)->flags |= DUK_CAT_FLAG_CATCH_ENABLED; \
  8592. } while (0)
  8593. #define DUK_CAT_SET_FINALLY_ENABLED(c) do { \
  8594. (c)->flags |= DUK_CAT_FLAG_FINALLY_ENABLED; \
  8595. } while (0)
  8596. #define DUK_CAT_SET_CATCH_BINDING_ENABLED(c) do { \
  8597. (c)->flags |= DUK_CAT_FLAG_CATCH_BINDING_ENABLED; \
  8598. } while (0)
  8599. #define DUK_CAT_SET_LEXENV_ACTIVE(c) do { \
  8600. (c)->flags |= DUK_CAT_FLAG_LEXENV_ACTIVE; \
  8601. } while (0)
  8602. #define DUK_CAT_CLEAR_CATCH_ENABLED(c) do { \
  8603. (c)->flags &= ~DUK_CAT_FLAG_CATCH_ENABLED; \
  8604. } while (0)
  8605. #define DUK_CAT_CLEAR_FINALLY_ENABLED(c) do { \
  8606. (c)->flags &= ~DUK_CAT_FLAG_FINALLY_ENABLED; \
  8607. } while (0)
  8608. #define DUK_CAT_CLEAR_CATCH_BINDING_ENABLED(c) do { \
  8609. (c)->flags &= ~DUK_CAT_FLAG_CATCH_BINDING_ENABLED; \
  8610. } while (0)
  8611. #define DUK_CAT_CLEAR_LEXENV_ACTIVE(c) do { \
  8612. (c)->flags &= ~DUK_CAT_FLAG_LEXENV_ACTIVE; \
  8613. } while (0)
  8614. /*
  8615. * Thread defines
  8616. */
  8617. #if defined(DUK_USE_HEAPPTR16)
  8618. #define DUK_HTHREAD_GET_STRING(thr,idx) \
  8619. ((duk_hstring *) DUK_USE_HEAPPTR_DEC16((thr)->heap->heap_udata, (thr)->strs16[(idx)]))
  8620. #else
  8621. #define DUK_HTHREAD_GET_STRING(thr,idx) \
  8622. ((thr)->strs[(idx)])
  8623. #endif
  8624. #define DUK_HTHREAD_GET_CURRENT_ACTIVATION(thr) (&(thr)->callstack[(thr)->callstack_top - 1])
  8625. /* values for the state field */
  8626. #define DUK_HTHREAD_STATE_INACTIVE 1 /* thread not currently running */
  8627. #define DUK_HTHREAD_STATE_RUNNING 2 /* thread currently running (only one at a time) */
  8628. #define DUK_HTHREAD_STATE_RESUMED 3 /* thread resumed another thread (active but not running) */
  8629. #define DUK_HTHREAD_STATE_YIELDED 4 /* thread has yielded */
  8630. #define DUK_HTHREAD_STATE_TERMINATED 5 /* thread has terminated */
  8631. /* Executor interrupt default interval when nothing else requires a
  8632. * smaller value. The default interval must be small enough to allow
  8633. * for reasonable execution timeout checking but large enough to keep
  8634. * impact on execution performance low.
  8635. */
  8636. #if defined(DUK_USE_INTERRUPT_COUNTER)
  8637. #define DUK_HTHREAD_INTCTR_DEFAULT (256L * 1024L)
  8638. #endif
  8639. /*
  8640. * Assert context is valid: non-NULL pointer, fields look sane.
  8641. *
  8642. * This is used by public API call entrypoints to catch invalid 'ctx' pointers
  8643. * as early as possible; invalid 'ctx' pointers cause very odd and difficult to
  8644. * diagnose behavior so it's worth checking even when the check is not 100%.
  8645. */
  8646. #if defined(DUK_USE_PREFER_SIZE)
  8647. #define DUK_ASSERT_CTX_VSSIZE(ctx) /*nop*/
  8648. #else
  8649. #define DUK_ASSERT_CTX_VSSIZE(ctx) \
  8650. DUK_ASSERT((duk_size_t) (((duk_hthread *) (ctx))->valstack_end - ((duk_hthread *) (ctx))->valstack) == \
  8651. ((duk_hthread *) (ctx))->valstack_size)
  8652. #endif
  8653. #define DUK_ASSERT_CTX_VALID(ctx) do { \
  8654. DUK_ASSERT((ctx) != NULL); \
  8655. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) (ctx)) == DUK_HTYPE_OBJECT); \
  8656. DUK_ASSERT(DUK_HOBJECT_IS_THREAD((duk_hobject *) (ctx))); \
  8657. DUK_ASSERT(((duk_hthread *) (ctx))->unused1 == 0); \
  8658. DUK_ASSERT(((duk_hthread *) (ctx))->unused2 == 0); \
  8659. DUK_ASSERT(((duk_hthread *) (ctx))->valstack != NULL); \
  8660. DUK_ASSERT(((duk_hthread *) (ctx))->valstack_end >= ((duk_hthread *) (ctx))->valstack); \
  8661. DUK_ASSERT(((duk_hthread *) (ctx))->valstack_top >= ((duk_hthread *) (ctx))->valstack); \
  8662. DUK_ASSERT(((duk_hthread *) (ctx))->valstack_top >= ((duk_hthread *) (ctx))->valstack_bottom); \
  8663. DUK_ASSERT(((duk_hthread *) (ctx))->valstack_end >= ((duk_hthread *) (ctx))->valstack_top); \
  8664. DUK_ASSERT_CTX_VSSIZE((ctx)); \
  8665. } while (0)
  8666. /*
  8667. * Struct defines
  8668. */
  8669. /* XXX: for a memory-code tradeoff, remove 'func' and make it's access either a function
  8670. * or a macro. This would make the activation 32 bytes long on 32-bit platforms again.
  8671. */
  8672. /* Note: it's nice if size is 2^N (at least for 32-bit platforms). */
  8673. struct duk_activation {
  8674. duk_tval tv_func; /* borrowed: full duk_tval for function being executed; for lightfuncs */
  8675. duk_hobject *func; /* borrowed: function being executed; for bound function calls, this is the final, real function, NULL for lightfuncs */
  8676. duk_hobject *var_env; /* current variable environment (may be NULL if delayed) */
  8677. duk_hobject *lex_env; /* current lexical environment (may be NULL if delayed) */
  8678. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  8679. /* Previous value of 'func' caller, restored when unwound. Only in use
  8680. * when 'func' is non-strict.
  8681. */
  8682. duk_hobject *prev_caller;
  8683. #endif
  8684. duk_instr_t *curr_pc; /* next instruction to execute (points to 'func' bytecode, stable pointer), NULL for native calls */
  8685. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  8686. duk_uint32_t prev_line; /* needed for stepping */
  8687. #endif
  8688. duk_small_uint_t flags;
  8689. /* idx_bottom and idx_retval are only used for book-keeping of
  8690. * Ecmascript-initiated calls, to allow returning to an Ecmascript
  8691. * function properly. They are duk_size_t to match the convention
  8692. * that value stack sizes are duk_size_t and local frame indices
  8693. * are duk_idx_t.
  8694. */
  8695. /* Bottom of valstack for this activation, used to reset
  8696. * valstack_bottom on return; index is absolute. Note:
  8697. * idx_top not needed because top is set to 'nregs' always
  8698. * when returning to an Ecmascript activation.
  8699. */
  8700. duk_size_t idx_bottom;
  8701. /* Return value when returning to this activation (points to caller
  8702. * reg, not callee reg); index is absolute (only set if activation is
  8703. * not topmost).
  8704. *
  8705. * Note: idx_bottom is always set, while idx_retval is only applicable
  8706. * for activations below the topmost one. Currently idx_retval for
  8707. * the topmost activation is considered garbage (and it not initialized
  8708. * on entry or cleared on return; may contain previous or garbage
  8709. * values).
  8710. */
  8711. duk_size_t idx_retval;
  8712. /* Current 'this' binding is the value just below idx_bottom.
  8713. * Previously, 'this' binding was handled with an index to the
  8714. * (calling) valstack. This works for everything except tail
  8715. * calls, which must not "cumulate" valstack temps.
  8716. */
  8717. };
  8718. /* Note: it's nice if size is 2^N (not 4x4 = 16 bytes on 32 bit) */
  8719. struct duk_catcher {
  8720. duk_hstring *h_varname; /* borrowed reference to catch variable name (or NULL if none) */
  8721. /* (reference is valid as long activation exists) */
  8722. duk_instr_t *pc_base; /* resume execution from pc_base or pc_base+1 (points to 'func' bytecode, stable pointer) */
  8723. duk_size_t callstack_index; /* callstack index of related activation */
  8724. duk_size_t idx_base; /* idx_base and idx_base+1 get completion value and type */
  8725. duk_uint32_t flags; /* type and control flags, label number */
  8726. };
  8727. struct duk_hthread {
  8728. /* Shared object part */
  8729. duk_hobject obj;
  8730. /* Pointer to bytecode executor's 'curr_pc' variable. Used to copy
  8731. * the current PC back into the topmost activation when activation
  8732. * state is about to change (or "syncing" is otherwise needed). This
  8733. * is rather awkward but important for performance, see execution.rst.
  8734. */
  8735. duk_instr_t **ptr_curr_pc;
  8736. /* Backpointers. */
  8737. duk_heap *heap;
  8738. /* Current strictness flag: affects API calls. */
  8739. duk_uint8_t strict;
  8740. /* Thread state. */
  8741. duk_uint8_t state;
  8742. duk_uint8_t unused1;
  8743. duk_uint8_t unused2;
  8744. /* Sanity limits for stack sizes. */
  8745. duk_size_t valstack_max;
  8746. duk_size_t callstack_max;
  8747. duk_size_t catchstack_max;
  8748. /* XXX: Valstack, callstack, and catchstack are currently assumed
  8749. * to have non-NULL pointers. Relaxing this would not lead to big
  8750. * benefits (except perhaps for terminated threads).
  8751. */
  8752. /* Value stack: these are expressed as pointers for faster stack manipulation.
  8753. * [valstack,valstack_top[ is GC-reachable, [valstack_top,valstack_end[ is
  8754. * not GC-reachable but kept initialized as 'undefined'.
  8755. */
  8756. duk_tval *valstack; /* start of valstack allocation */
  8757. duk_tval *valstack_end; /* end of valstack allocation (exclusive) */
  8758. duk_tval *valstack_bottom; /* bottom of current frame */
  8759. duk_tval *valstack_top; /* top of current frame (exclusive) */
  8760. #if !defined(DUK_USE_PREFER_SIZE)
  8761. duk_size_t valstack_size; /* cached: valstack_end - valstack (in entries, not bytes) */
  8762. #endif
  8763. /* Call stack. [0,callstack_top[ is GC reachable. */
  8764. duk_activation *callstack;
  8765. duk_size_t callstack_size; /* allocation size */
  8766. duk_size_t callstack_top; /* next to use, highest used is top - 1 */
  8767. duk_size_t callstack_preventcount; /* number of activation records in callstack preventing a yield */
  8768. /* Catch stack. [0,catchstack_top[ is GC reachable. */
  8769. duk_catcher *catchstack;
  8770. duk_size_t catchstack_size; /* allocation size */
  8771. duk_size_t catchstack_top; /* next to use, highest used is top - 1 */
  8772. /* Yield/resume book-keeping. */
  8773. duk_hthread *resumer; /* who resumed us (if any) */
  8774. /* Current compiler state (if any), used for augmenting SyntaxErrors. */
  8775. duk_compiler_ctx *compile_ctx;
  8776. #if defined(DUK_USE_INTERRUPT_COUNTER)
  8777. /* Interrupt counter for triggering a slow path check for execution
  8778. * timeout, debugger interaction such as breakpoints, etc. The value
  8779. * is valid for the current running thread, and both the init and
  8780. * counter values are copied whenever a thread switch occurs. It's
  8781. * important for the counter to be conveniently accessible for the
  8782. * bytecode executor inner loop for performance reasons.
  8783. */
  8784. duk_int_t interrupt_counter; /* countdown state */
  8785. duk_int_t interrupt_init; /* start value for current countdown */
  8786. #endif
  8787. /* Builtin-objects; may or may not be shared with other threads,
  8788. * threads existing in different "compartments" will have different
  8789. * built-ins. Must be stored on a per-thread basis because there
  8790. * is no intermediate structure for a thread group / compartment.
  8791. * This takes quite a lot of space, currently 43x4 = 172 bytes on
  8792. * 32-bit platforms.
  8793. */
  8794. duk_hobject *builtins[DUK_NUM_BUILTINS];
  8795. /* Convenience copies from heap/vm for faster access. */
  8796. #if defined(DUK_USE_HEAPPTR16)
  8797. duk_uint16_t *strs16;
  8798. #else
  8799. duk_hstring **strs;
  8800. #endif
  8801. };
  8802. /*
  8803. * Prototypes
  8804. */
  8805. DUK_INTERNAL_DECL void duk_hthread_copy_builtin_objects(duk_hthread *thr_from, duk_hthread *thr_to);
  8806. DUK_INTERNAL_DECL void duk_hthread_create_builtin_objects(duk_hthread *thr);
  8807. DUK_INTERNAL_DECL duk_bool_t duk_hthread_init_stacks(duk_heap *heap, duk_hthread *thr);
  8808. DUK_INTERNAL_DECL void duk_hthread_terminate(duk_hthread *thr);
  8809. DUK_INTERNAL_DECL void duk_hthread_callstack_grow(duk_hthread *thr);
  8810. DUK_INTERNAL_DECL void duk_hthread_callstack_shrink_check(duk_hthread *thr);
  8811. DUK_INTERNAL_DECL void duk_hthread_callstack_unwind(duk_hthread *thr, duk_size_t new_top);
  8812. DUK_INTERNAL_DECL void duk_hthread_catchstack_grow(duk_hthread *thr);
  8813. DUK_INTERNAL_DECL void duk_hthread_catchstack_shrink_check(duk_hthread *thr);
  8814. DUK_INTERNAL_DECL void duk_hthread_catchstack_unwind(duk_hthread *thr, duk_size_t new_top);
  8815. DUK_INTERNAL_DECL duk_activation *duk_hthread_get_current_activation(duk_hthread *thr);
  8816. DUK_INTERNAL_DECL void *duk_hthread_get_valstack_ptr(duk_heap *heap, void *ud); /* indirect allocs */
  8817. DUK_INTERNAL_DECL void *duk_hthread_get_callstack_ptr(duk_heap *heap, void *ud); /* indirect allocs */
  8818. DUK_INTERNAL_DECL void *duk_hthread_get_catchstack_ptr(duk_heap *heap, void *ud); /* indirect allocs */
  8819. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  8820. DUK_INTERNAL_DECL duk_uint_fast32_t duk_hthread_get_act_curr_pc(duk_hthread *thr, duk_activation *act);
  8821. #endif
  8822. DUK_INTERNAL_DECL duk_uint_fast32_t duk_hthread_get_act_prev_pc(duk_hthread *thr, duk_activation *act);
  8823. DUK_INTERNAL_DECL void duk_hthread_sync_currpc(duk_hthread *thr);
  8824. DUK_INTERNAL_DECL void duk_hthread_sync_and_null_currpc(duk_hthread *thr);
  8825. #endif /* DUK_HTHREAD_H_INCLUDED */
  8826. #line 1 "duk_hbuffer.h"
  8827. /*
  8828. * Heap buffer representation.
  8829. *
  8830. * Heap allocated user data buffer which is either:
  8831. *
  8832. * 1. A fixed size buffer (data follows header statically)
  8833. * 2. A dynamic size buffer (data pointer follows header)
  8834. *
  8835. * The data pointer for a variable size buffer of zero size may be NULL.
  8836. */
  8837. #ifndef DUK_HBUFFER_H_INCLUDED
  8838. #define DUK_HBUFFER_H_INCLUDED
  8839. /*
  8840. * Flags
  8841. *
  8842. * Fixed buffer: 0
  8843. * Dynamic buffer: DUK_HBUFFER_FLAG_DYNAMIC
  8844. * External buffer: DUK_HBUFFER_FLAG_DYNAMIC | DUK_HBUFFER_FLAG_EXTERNAL
  8845. */
  8846. #define DUK_HBUFFER_FLAG_DYNAMIC DUK_HEAPHDR_USER_FLAG(0) /* buffer is behind a pointer, dynamic or external */
  8847. #define DUK_HBUFFER_FLAG_EXTERNAL DUK_HEAPHDR_USER_FLAG(1) /* buffer pointer is to an externally allocated buffer */
  8848. #define DUK_HBUFFER_HAS_DYNAMIC(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_DYNAMIC)
  8849. #define DUK_HBUFFER_HAS_EXTERNAL(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_EXTERNAL)
  8850. #define DUK_HBUFFER_SET_DYNAMIC(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_DYNAMIC)
  8851. #define DUK_HBUFFER_SET_EXTERNAL(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_EXTERNAL)
  8852. #define DUK_HBUFFER_CLEAR_DYNAMIC(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_DYNAMIC)
  8853. #define DUK_HBUFFER_CLEAR_EXTERNAL(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_EXTERNAL)
  8854. /*
  8855. * Misc defines
  8856. */
  8857. /* Impose a maximum buffer length for now. Restricted artificially to
  8858. * ensure resize computations or adding a heap header length won't
  8859. * overflow size_t and that a signed duk_int_t can hold a buffer
  8860. * length. The limit should be synchronized with DUK_HSTRING_MAX_BYTELEN.
  8861. */
  8862. #if defined(DUK_USE_BUFLEN16)
  8863. #define DUK_HBUFFER_MAX_BYTELEN (0x0000ffffUL)
  8864. #else
  8865. /* Intentionally not 0x7fffffffUL; at least JSON code expects that
  8866. * 2*len + 2 fits in 32 bits.
  8867. */
  8868. #define DUK_HBUFFER_MAX_BYTELEN (0x7ffffffeUL)
  8869. #endif
  8870. /*
  8871. * Field access
  8872. */
  8873. /* Get/set the current user visible size, without accounting for a dynamic
  8874. * buffer's "spare" (= usable size).
  8875. */
  8876. #if defined(DUK_USE_BUFLEN16)
  8877. /* size stored in duk_heaphdr unused flag bits */
  8878. #define DUK_HBUFFER_GET_SIZE(x) ((x)->hdr.h_flags >> 16)
  8879. #define DUK_HBUFFER_SET_SIZE(x,v) do { \
  8880. (x)->hdr.h_flags = ((x)->hdr.h_flags & 0x0000ffffUL) | ((v) << 16); \
  8881. } while (0)
  8882. #define DUK_HBUFFER_ADD_SIZE(x,dv) do { \
  8883. (x)->hdr.h_flags += ((dv) << 16); \
  8884. } while (0)
  8885. #define DUK_HBUFFER_SUB_SIZE(x,dv) do { \
  8886. (x)->hdr.h_flags -= ((dv) << 16); \
  8887. } while (0)
  8888. #else
  8889. #define DUK_HBUFFER_GET_SIZE(x) (((duk_hbuffer *) (x))->size)
  8890. #define DUK_HBUFFER_SET_SIZE(x,v) do { \
  8891. ((duk_hbuffer *) (x))->size = (v); \
  8892. } while (0)
  8893. #define DUK_HBUFFER_ADD_SIZE(x,dv) do { \
  8894. (x)->size += (dv); \
  8895. } while (0)
  8896. #define DUK_HBUFFER_SUB_SIZE(x,dv) do { \
  8897. (x)->size -= (dv); \
  8898. } while (0)
  8899. #endif
  8900. #define DUK_HBUFFER_FIXED_GET_SIZE(x) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) (x))
  8901. #define DUK_HBUFFER_FIXED_SET_SIZE(x,v) DUK_HBUFFER_SET_SIZE((duk_hbuffer *) (x))
  8902. #define DUK_HBUFFER_DYNAMIC_GET_SIZE(x) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) (x))
  8903. #define DUK_HBUFFER_DYNAMIC_SET_SIZE(x,v) DUK_HBUFFER_SET_SIZE((duk_hbuffer *) (x), (v))
  8904. #define DUK_HBUFFER_DYNAMIC_ADD_SIZE(x,dv) DUK_HBUFFER_ADD_SIZE((duk_hbuffer *) (x), (dv))
  8905. #define DUK_HBUFFER_DYNAMIC_SUB_SIZE(x,dv) DUK_HBUFFER_SUB_SIZE((duk_hbuffer *) (x), (dv))
  8906. #define DUK_HBUFFER_EXTERNAL_GET_SIZE(x) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) (x))
  8907. #define DUK_HBUFFER_EXTERNAL_SET_SIZE(x,v) DUK_HBUFFER_SET_SIZE((duk_hbuffer *) (x), (v))
  8908. #define DUK_HBUFFER_FIXED_GET_DATA_PTR(heap,x) ((duk_uint8_t *) (((duk_hbuffer_fixed *) (x)) + 1))
  8909. #if defined(DUK_USE_HEAPPTR16)
  8910. #define DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap,x) \
  8911. ((void *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, ((duk_heaphdr *) (x))->h_extra16))
  8912. #define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(heap,x,v) do { \
  8913. ((duk_heaphdr *) (x))->h_extra16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \
  8914. } while (0)
  8915. #define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR_NULL(heap,x) do { \
  8916. ((duk_heaphdr *) (x))->h_extra16 = 0; /* assume 0 <=> NULL */ \
  8917. } while (0)
  8918. #else
  8919. #define DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap,x) ((x)->curr_alloc)
  8920. #define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(heap,x,v) do { \
  8921. (x)->curr_alloc = (void *) (v); \
  8922. } while (0)
  8923. #define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR_NULL(heap,x) do { \
  8924. (x)->curr_alloc = (void *) NULL; \
  8925. } while (0)
  8926. #endif
  8927. /* No pointer compression because pointer is potentially outside of
  8928. * Duktape heap.
  8929. */
  8930. #if defined(DUK_USE_HEAPPTR16)
  8931. #define DUK_HBUFFER_EXTERNAL_GET_DATA_PTR(heap,x) \
  8932. ((void *) (x)->curr_alloc)
  8933. #define DUK_HBUFFER_EXTERNAL_SET_DATA_PTR(heap,x,v) do { \
  8934. (x)->curr_alloc = (void *) (v); \
  8935. } while (0)
  8936. #define DUK_HBUFFER_EXTERNAL_SET_DATA_PTR_NULL(heap,x) do { \
  8937. (x)->curr_alloc = (void *) NULL; \
  8938. } while (0)
  8939. #else
  8940. #define DUK_HBUFFER_EXTERNAL_GET_DATA_PTR(heap,x) \
  8941. ((void *) (x)->curr_alloc)
  8942. #define DUK_HBUFFER_EXTERNAL_SET_DATA_PTR(heap,x,v) do { \
  8943. (x)->curr_alloc = (void *) (v); \
  8944. } while (0)
  8945. #define DUK_HBUFFER_EXTERNAL_SET_DATA_PTR_NULL(heap,x) do { \
  8946. (x)->curr_alloc = (void *) NULL; \
  8947. } while (0)
  8948. #endif
  8949. /* Get a pointer to the current buffer contents (matching current allocation
  8950. * size). May be NULL for zero size dynamic/external buffer.
  8951. */
  8952. #if defined(DUK_USE_HEAPPTR16)
  8953. #define DUK_HBUFFER_GET_DATA_PTR(heap,x) ( \
  8954. DUK_HBUFFER_HAS_DYNAMIC((x)) ? \
  8955. ( \
  8956. DUK_HBUFFER_HAS_EXTERNAL((x)) ? \
  8957. DUK_HBUFFER_EXTERNAL_GET_DATA_PTR((heap), (duk_hbuffer_external *) (x)) : \
  8958. DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((heap), (duk_hbuffer_dynamic *) (x)) \
  8959. ) : \
  8960. DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), (duk_hbuffer_fixed *) (x)) \
  8961. )
  8962. #else
  8963. /* Without heap pointer compression duk_hbuffer_dynamic and duk_hbuffer_external
  8964. * have the same layout so checking for fixed vs. dynamic (or external) is enough.
  8965. */
  8966. #define DUK_HBUFFER_GET_DATA_PTR(heap,x) ( \
  8967. DUK_HBUFFER_HAS_DYNAMIC((x)) ? \
  8968. DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((heap), (duk_hbuffer_dynamic *) (x)) : \
  8969. DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), (duk_hbuffer_fixed *) (x)) \
  8970. )
  8971. #endif
  8972. /*
  8973. * Structs
  8974. */
  8975. /* Shared prefix for all buffer types. */
  8976. struct duk_hbuffer {
  8977. duk_heaphdr hdr;
  8978. /* It's not strictly necessary to track the current size, but
  8979. * it is useful for writing robust native code.
  8980. */
  8981. /* Current size (not counting a dynamic buffer's "spare"). */
  8982. #if defined(DUK_USE_BUFLEN16)
  8983. /* Stored in duk_heaphdr unused flags. */
  8984. #else
  8985. duk_size_t size;
  8986. #endif
  8987. /*
  8988. * Data following the header depends on the DUK_HBUFFER_FLAG_DYNAMIC
  8989. * flag.
  8990. *
  8991. * If the flag is clear (the buffer is a fixed size one), the buffer
  8992. * data follows the header directly, consisting of 'size' bytes.
  8993. *
  8994. * If the flag is set, the actual buffer is allocated separately, and
  8995. * a few control fields follow the header. Specifically:
  8996. *
  8997. * - a "void *" pointing to the current allocation
  8998. * - a duk_size_t indicating the full allocated size (always >= 'size')
  8999. *
  9000. * If DUK_HBUFFER_FLAG_EXTERNAL is set, the buffer has been allocated
  9001. * by user code, so that Duktape won't be able to resize it and won't
  9002. * free it. This allows buffers to point to e.g. an externally
  9003. * allocated structure such as a frame buffer.
  9004. *
  9005. * Unlike strings, no terminator byte (NUL) is guaranteed after the
  9006. * data. This would be convenient, but would pad aligned user buffers
  9007. * unnecessarily upwards in size. For instance, if user code requested
  9008. * a 64-byte dynamic buffer, 65 bytes would actually be allocated which
  9009. * would then potentially round upwards to perhaps 68 or 72 bytes.
  9010. */
  9011. };
  9012. /* Fixed buffer; data follows struct, with proper alignment guaranteed by
  9013. * struct size.
  9014. */
  9015. #if (DUK_USE_ALIGN_BY == 8) && defined(DUK_USE_PACK_MSVC_PRAGMA)
  9016. #pragma pack(push, 8)
  9017. #endif
  9018. struct duk_hbuffer_fixed {
  9019. /* A union is used here as a portable struct size / alignment trick:
  9020. * by adding a 32-bit or a 64-bit (unused) union member, the size of
  9021. * the struct is effectively forced to be a multiple of 4 or 8 bytes
  9022. * (respectively) without increasing the size of the struct unless
  9023. * necessary.
  9024. */
  9025. union {
  9026. struct {
  9027. duk_heaphdr hdr;
  9028. #if defined(DUK_USE_BUFLEN16)
  9029. /* Stored in duk_heaphdr unused flags. */
  9030. #else
  9031. duk_size_t size;
  9032. #endif
  9033. } s;
  9034. #if (DUK_USE_ALIGN_BY == 4)
  9035. duk_uint32_t dummy_for_align4;
  9036. #elif (DUK_USE_ALIGN_BY == 8)
  9037. duk_double_t dummy_for_align8;
  9038. #elif (DUK_USE_ALIGN_BY == 1)
  9039. /* no extra padding */
  9040. #else
  9041. #error invalid DUK_USE_ALIGN_BY
  9042. #endif
  9043. } u;
  9044. /*
  9045. * Data follows the struct header. The struct size is padded by the
  9046. * compiler based on the struct members. This guarantees that the
  9047. * buffer data will be aligned-by-4 but not necessarily aligned-by-8.
  9048. *
  9049. * On platforms where alignment does not matter, the struct padding
  9050. * could be removed (if there is any). On platforms where alignment
  9051. * by 8 is required, the struct size must be forced to be a multiple
  9052. * of 8 by some means. Without it, some user code may break, and also
  9053. * Duktape itself breaks (e.g. the compiler stores duk_tvals in a
  9054. * dynamic buffer).
  9055. */
  9056. }
  9057. #if (DUK_USE_ALIGN_BY == 8) && defined(DUK_USE_PACK_GCC_ATTR)
  9058. __attribute__ ((aligned (8)))
  9059. #elif (DUK_USE_ALIGN_BY == 8) && defined(DUK_USE_PACK_CLANG_ATTR)
  9060. __attribute__ ((aligned (8)))
  9061. #endif
  9062. ;
  9063. #if (DUK_USE_ALIGN_BY == 8) && defined(DUK_USE_PACK_MSVC_PRAGMA)
  9064. #pragma pack(pop)
  9065. #endif
  9066. /* Dynamic buffer with 'curr_alloc' pointing to a dynamic area allocated using
  9067. * heap allocation primitives. Also used for external buffers when low memory
  9068. * options are not used.
  9069. */
  9070. struct duk_hbuffer_dynamic {
  9071. duk_heaphdr hdr;
  9072. #if defined(DUK_USE_BUFLEN16)
  9073. /* Stored in duk_heaphdr unused flags. */
  9074. #else
  9075. duk_size_t size;
  9076. #endif
  9077. #if defined(DUK_USE_HEAPPTR16)
  9078. /* Stored in duk_heaphdr h_extra16. */
  9079. #else
  9080. void *curr_alloc; /* may be NULL if alloc_size == 0 */
  9081. #endif
  9082. /*
  9083. * Allocation size for 'curr_alloc' is alloc_size. There is no
  9084. * automatic NUL terminator for buffers (see above for rationale).
  9085. *
  9086. * 'curr_alloc' is explicitly allocated with heap allocation
  9087. * primitives and will thus always have alignment suitable for
  9088. * e.g. duk_tval and an IEEE double.
  9089. */
  9090. };
  9091. /* External buffer with 'curr_alloc' managed by user code and pointing to an
  9092. * arbitrary address. When heap pointer compression is not used, this struct
  9093. * has the same layout as duk_hbuffer_dynamic.
  9094. */
  9095. struct duk_hbuffer_external {
  9096. duk_heaphdr hdr;
  9097. #if defined(DUK_USE_BUFLEN16)
  9098. /* Stored in duk_heaphdr unused flags. */
  9099. #else
  9100. duk_size_t size;
  9101. #endif
  9102. /* Cannot be compressed as a heap pointer because may point to
  9103. * an arbitrary address.
  9104. */
  9105. void *curr_alloc; /* may be NULL if alloc_size == 0 */
  9106. };
  9107. /*
  9108. * Prototypes
  9109. */
  9110. DUK_INTERNAL_DECL duk_hbuffer *duk_hbuffer_alloc(duk_heap *heap, duk_size_t size, duk_small_uint_t flags, void **out_bufdata);
  9111. DUK_INTERNAL_DECL void *duk_hbuffer_get_dynalloc_ptr(duk_heap *heap, void *ud); /* indirect allocs */
  9112. /* dynamic buffer ops */
  9113. DUK_INTERNAL_DECL void duk_hbuffer_resize(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t new_size);
  9114. DUK_INTERNAL_DECL void duk_hbuffer_reset(duk_hthread *thr, duk_hbuffer_dynamic *buf);
  9115. #endif /* DUK_HBUFFER_H_INCLUDED */
  9116. #line 1 "duk_heap.h"
  9117. /*
  9118. * Heap structure.
  9119. *
  9120. * Heap contains allocated heap objects, interned strings, and built-in
  9121. * strings for one or more threads.
  9122. */
  9123. #ifndef DUK_HEAP_H_INCLUDED
  9124. #define DUK_HEAP_H_INCLUDED
  9125. /* alloc function typedefs in duktape.h */
  9126. /*
  9127. * Heap flags
  9128. */
  9129. #define DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING (1 << 0) /* mark-and-sweep is currently running */
  9130. #define DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED (1 << 1) /* mark-and-sweep marking reached a recursion limit and must use multi-pass marking */
  9131. #define DUK_HEAP_FLAG_REFZERO_FREE_RUNNING (1 << 2) /* refcount code is processing refzero list */
  9132. #define DUK_HEAP_FLAG_ERRHANDLER_RUNNING (1 << 3) /* an error handler (user callback to augment/replace error) is running */
  9133. #define DUK_HEAP_FLAG_INTERRUPT_RUNNING (1 << 4) /* executor interrupt running (used to avoid nested interrupts) */
  9134. #define DUK__HEAP_HAS_FLAGS(heap,bits) ((heap)->flags & (bits))
  9135. #define DUK__HEAP_SET_FLAGS(heap,bits) do { \
  9136. (heap)->flags |= (bits); \
  9137. } while (0)
  9138. #define DUK__HEAP_CLEAR_FLAGS(heap,bits) do { \
  9139. (heap)->flags &= ~(bits); \
  9140. } while (0)
  9141. #define DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING)
  9142. #define DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED)
  9143. #define DUK_HEAP_HAS_REFZERO_FREE_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING)
  9144. #define DUK_HEAP_HAS_ERRHANDLER_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING)
  9145. #define DUK_HEAP_HAS_INTERRUPT_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING)
  9146. #define DUK_HEAP_SET_MARKANDSWEEP_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING)
  9147. #define DUK_HEAP_SET_MARKANDSWEEP_RECLIMIT_REACHED(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED)
  9148. #define DUK_HEAP_SET_REFZERO_FREE_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING)
  9149. #define DUK_HEAP_SET_ERRHANDLER_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING)
  9150. #define DUK_HEAP_SET_INTERRUPT_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING)
  9151. #define DUK_HEAP_CLEAR_MARKANDSWEEP_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING)
  9152. #define DUK_HEAP_CLEAR_MARKANDSWEEP_RECLIMIT_REACHED(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED)
  9153. #define DUK_HEAP_CLEAR_REFZERO_FREE_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING)
  9154. #define DUK_HEAP_CLEAR_ERRHANDLER_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING)
  9155. #define DUK_HEAP_CLEAR_INTERRUPT_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING)
  9156. /*
  9157. * Longjmp types, also double as identifying continuation type for a rethrow (in 'finally')
  9158. */
  9159. #define DUK_LJ_TYPE_UNKNOWN 0 /* unused */
  9160. #define DUK_LJ_TYPE_THROW 1 /* value1 -> error object */
  9161. #define DUK_LJ_TYPE_YIELD 2 /* value1 -> yield value, iserror -> error / normal */
  9162. #define DUK_LJ_TYPE_RESUME 3 /* value1 -> resume value, value2 -> resumee thread, iserror -> error/normal */
  9163. #define DUK_LJ_TYPE_BREAK 4 /* value1 -> label number, pseudo-type to indicate a break continuation (for ENDFIN) */
  9164. #define DUK_LJ_TYPE_CONTINUE 5 /* value1 -> label number, pseudo-type to indicate a continue continuation (for ENDFIN) */
  9165. #define DUK_LJ_TYPE_RETURN 6 /* value1 -> return value, pseudo-type to indicate a return continuation (for ENDFIN) */
  9166. #define DUK_LJ_TYPE_NORMAL 7 /* no value, pseudo-type to indicate a normal continuation (for ENDFIN) */
  9167. /*
  9168. * Mark-and-sweep flags
  9169. *
  9170. * These are separate from heap level flags now but could be merged.
  9171. * The heap structure only contains a 'base mark-and-sweep flags'
  9172. * field and the GC caller can impose further flags.
  9173. */
  9174. #define DUK_MS_FLAG_EMERGENCY (1 << 0) /* emergency mode: try extra hard */
  9175. #define DUK_MS_FLAG_NO_STRINGTABLE_RESIZE (1 << 1) /* don't resize stringtable (but may sweep it); needed during stringtable resize */
  9176. #define DUK_MS_FLAG_NO_FINALIZERS (1 << 2) /* don't run finalizers (which may have arbitrary side effects) */
  9177. #define DUK_MS_FLAG_NO_OBJECT_COMPACTION (1 << 3) /* don't compact objects; needed during object property allocation resize */
  9178. /*
  9179. * Thread switching
  9180. *
  9181. * To switch heap->curr_thread, use the macro below so that interrupt counters
  9182. * get updated correctly. The macro allows a NULL target thread because that
  9183. * happens e.g. in call handling.
  9184. */
  9185. #if defined(DUK_USE_INTERRUPT_COUNTER)
  9186. #define DUK_HEAP_SWITCH_THREAD(heap,newthr) duk_heap_switch_thread((heap), (newthr))
  9187. #else
  9188. #define DUK_HEAP_SWITCH_THREAD(heap,newthr) do { \
  9189. (heap)->curr_thread = (newthr); \
  9190. } while (0)
  9191. #endif
  9192. /*
  9193. * Other heap related defines
  9194. */
  9195. /* Mark-and-sweep interval is relative to combined count of objects and
  9196. * strings kept in the heap during the latest mark-and-sweep pass.
  9197. * Fixed point .8 multiplier and .0 adder. Trigger count (interval) is
  9198. * decreased by each (re)allocation attempt (regardless of size), and each
  9199. * refzero processed object.
  9200. *
  9201. * 'SKIP' indicates how many (re)allocations to wait until a retry if
  9202. * GC is skipped because there is no thread do it with yet (happens
  9203. * only during init phases).
  9204. */
  9205. #if defined(DUK_USE_MARK_AND_SWEEP)
  9206. #if defined(DUK_USE_REFERENCE_COUNTING)
  9207. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_MULT 12800L /* 50x heap size */
  9208. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_ADD 1024L
  9209. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_SKIP 256L
  9210. #else
  9211. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_MULT 256L /* 1x heap size */
  9212. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_ADD 1024L
  9213. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_SKIP 256L
  9214. #endif
  9215. #endif
  9216. /* Stringcache is used for speeding up char-offset-to-byte-offset
  9217. * translations for non-ASCII strings.
  9218. */
  9219. #define DUK_HEAP_STRCACHE_SIZE 4
  9220. #define DUK_HEAP_STRINGCACHE_NOCACHE_LIMIT 16 /* strings up to the this length are not cached */
  9221. /* helper to insert a (non-string) heap object into heap allocated list */
  9222. #define DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap,hdr) duk_heap_insert_into_heap_allocated((heap),(hdr))
  9223. /*
  9224. * Stringtable
  9225. */
  9226. /* initial stringtable size, must be prime and higher than DUK_UTIL_MIN_HASH_PRIME */
  9227. #define DUK_STRTAB_INITIAL_SIZE 17
  9228. /* indicates a deleted string; any fixed non-NULL, non-hstring pointer works */
  9229. #define DUK_STRTAB_DELETED_MARKER(heap) ((duk_hstring *) heap)
  9230. /* resizing parameters */
  9231. #define DUK_STRTAB_MIN_FREE_DIVISOR 4 /* load factor max 75% */
  9232. #define DUK_STRTAB_MIN_USED_DIVISOR 4 /* load factor min 25% */
  9233. #define DUK_STRTAB_GROW_ST_SIZE(n) ((n) + (n)) /* used entries + approx 100% -> reset load to 50% */
  9234. #define DUK_STRTAB_U32_MAX_STRLEN 10 /* 4'294'967'295 */
  9235. #define DUK_STRTAB_HIGHEST_32BIT_PRIME 0xfffffffbUL
  9236. /* probe sequence (open addressing) */
  9237. #define DUK_STRTAB_HASH_INITIAL(hash,h_size) ((hash) % (h_size))
  9238. #define DUK_STRTAB_HASH_PROBE_STEP(hash) DUK_UTIL_GET_HASH_PROBE_STEP((hash))
  9239. /* fixed top level hashtable size (separate chaining) */
  9240. #define DUK_STRTAB_CHAIN_SIZE DUK_USE_STRTAB_CHAIN_SIZE
  9241. /*
  9242. * Built-in strings
  9243. */
  9244. /* heap string indices are autogenerated in duk_strings.h */
  9245. #if defined(DUK_USE_HEAPPTR16)
  9246. #define DUK_HEAP_GET_STRING(heap,idx) \
  9247. ((duk_hstring *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (heap)->strs16[(idx)]))
  9248. #else
  9249. #define DUK_HEAP_GET_STRING(heap,idx) \
  9250. ((heap)->strs[(idx)])
  9251. #endif
  9252. /*
  9253. * Raw memory calls: relative to heap, but no GC interaction
  9254. */
  9255. #define DUK_ALLOC_RAW(heap,size) \
  9256. ((heap)->alloc_func((heap)->heap_udata, (size)))
  9257. #define DUK_REALLOC_RAW(heap,ptr,newsize) \
  9258. ((heap)->realloc_func((heap)->heap_udata, (void *) (ptr), (newsize)))
  9259. #define DUK_FREE_RAW(heap,ptr) \
  9260. ((heap)->free_func((heap)->heap_udata, (void *) (ptr)))
  9261. /*
  9262. * Memory calls: relative to heap, GC interaction, but no error throwing.
  9263. *
  9264. * XXX: Currently a mark-and-sweep triggered by memory allocation will run
  9265. * using the heap->heap_thread. This thread is also used for running
  9266. * mark-and-sweep finalization; this is not ideal because it breaks the
  9267. * isolation between multiple global environments.
  9268. *
  9269. * Notes:
  9270. *
  9271. * - DUK_FREE() is required to ignore NULL and any other possible return
  9272. * value of a zero-sized alloc/realloc (same as ANSI C free()).
  9273. *
  9274. * - There is no DUK_REALLOC_ZEROED because we don't assume to know the
  9275. * old size. Caller must zero the reallocated memory.
  9276. *
  9277. * - DUK_REALLOC_INDIRECT() must be used when a mark-and-sweep triggered
  9278. * by an allocation failure might invalidate the original 'ptr', thus
  9279. * causing a realloc retry to use an invalid pointer. Example: we're
  9280. * reallocating the value stack and a finalizer resizes the same value
  9281. * stack during mark-and-sweep. The indirect variant requests for the
  9282. * current location of the pointer being reallocated using a callback
  9283. * right before every realloc attempt; this circuitous approach is used
  9284. * to avoid strict aliasing issues in a more straightforward indirect
  9285. * pointer (void **) approach. Note: the pointer in the storage
  9286. * location is read but is NOT updated; the caller must do that.
  9287. */
  9288. /* callback for indirect reallocs, request for current pointer */
  9289. typedef void *(*duk_mem_getptr)(duk_heap *heap, void *ud);
  9290. #define DUK_ALLOC(heap,size) duk_heap_mem_alloc((heap), (size))
  9291. #define DUK_ALLOC_ZEROED(heap,size) duk_heap_mem_alloc_zeroed((heap), (size))
  9292. #define DUK_REALLOC(heap,ptr,newsize) duk_heap_mem_realloc((heap), (ptr), (newsize))
  9293. #define DUK_REALLOC_INDIRECT(heap,cb,ud,newsize) duk_heap_mem_realloc_indirect((heap), (cb), (ud), (newsize))
  9294. #define DUK_FREE(heap,ptr) duk_heap_mem_free((heap), (ptr))
  9295. /*
  9296. * Memory constants
  9297. */
  9298. #define DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT 5 /* Retry allocation after mark-and-sweep for this
  9299. * many times. A single mark-and-sweep round is
  9300. * not guaranteed to free all unreferenced memory
  9301. * because of finalization (in fact, ANY number of
  9302. * rounds is strictly not enough).
  9303. */
  9304. #define DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT 3 /* Starting from this round, use emergency mode
  9305. * for mark-and-sweep.
  9306. */
  9307. /*
  9308. * Debugger support
  9309. */
  9310. /* Maximum number of breakpoints. Only breakpoints that are set are
  9311. * consulted so increasing this has no performance impact.
  9312. */
  9313. #define DUK_HEAP_MAX_BREAKPOINTS 16
  9314. /* Opcode interval for a Date-based status/peek rate limit check. Only
  9315. * relevant when debugger is attached. Requesting a timestamp may be a
  9316. * slow operation on some platforms so this shouldn't be too low. On the
  9317. * other hand a high value makes Duktape react to a pause request slowly.
  9318. */
  9319. #define DUK_HEAP_DBG_RATELIMIT_OPCODES 4000
  9320. /* Milliseconds between status notify and transport peeks. */
  9321. #define DUK_HEAP_DBG_RATELIMIT_MILLISECS 200
  9322. /* Step types */
  9323. #define DUK_STEP_TYPE_NONE 0
  9324. #define DUK_STEP_TYPE_INTO 1
  9325. #define DUK_STEP_TYPE_OVER 2
  9326. #define DUK_STEP_TYPE_OUT 3
  9327. struct duk_breakpoint {
  9328. duk_hstring *filename;
  9329. duk_uint32_t line;
  9330. };
  9331. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  9332. #define DUK_HEAP_IS_DEBUGGER_ATTACHED(heap) ((heap)->dbg_read_cb != NULL)
  9333. #define DUK_HEAP_CLEAR_STEP_STATE(heap) do { \
  9334. (heap)->dbg_step_type = DUK_STEP_TYPE_NONE; \
  9335. (heap)->dbg_step_thread = NULL; \
  9336. (heap)->dbg_step_csindex = 0; \
  9337. (heap)->dbg_step_startline = 0; \
  9338. } while (0)
  9339. #define DUK_HEAP_SET_PAUSED(heap) do { \
  9340. (heap)->dbg_paused = 1; \
  9341. (heap)->dbg_state_dirty = 1; \
  9342. DUK_HEAP_CLEAR_STEP_STATE((heap)); \
  9343. } while (0)
  9344. #define DUK_HEAP_CLEAR_PAUSED(heap) do { \
  9345. (heap)->dbg_paused = 0; \
  9346. (heap)->dbg_state_dirty = 1; \
  9347. DUK_HEAP_CLEAR_STEP_STATE((heap)); \
  9348. } while (0)
  9349. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  9350. /*
  9351. * String cache should ideally be at duk_hthread level, but that would
  9352. * cause string finalization to slow down relative to the number of
  9353. * threads; string finalization must check the string cache for "weak"
  9354. * references to the string being finalized to avoid dead pointers.
  9355. *
  9356. * Thus, string caches are now at the heap level now.
  9357. */
  9358. struct duk_strcache {
  9359. duk_hstring *h;
  9360. duk_uint32_t bidx;
  9361. duk_uint32_t cidx;
  9362. };
  9363. /*
  9364. * Longjmp state, contains the information needed to perform a longjmp.
  9365. * Longjmp related values are written to value1, value2, and iserror.
  9366. */
  9367. struct duk_ljstate {
  9368. duk_jmpbuf *jmpbuf_ptr; /* current setjmp() catchpoint */
  9369. duk_small_uint_t type; /* longjmp type */
  9370. duk_bool_t iserror; /* isError flag for yield */
  9371. duk_tval value1; /* 1st related value (type specific) */
  9372. duk_tval value2; /* 2nd related value (type specific) */
  9373. };
  9374. /*
  9375. * Stringtable entry for fixed size stringtable
  9376. */
  9377. struct duk_strtab_entry {
  9378. #if defined(DUK_USE_HEAPPTR16)
  9379. /* A 16-bit listlen makes sense with 16-bit heap pointers: there
  9380. * won't be space for 64k strings anyway.
  9381. */
  9382. duk_uint16_t listlen; /* if 0, 'str16' used, if > 0, 'strlist16' used */
  9383. union {
  9384. duk_uint16_t strlist16;
  9385. duk_uint16_t str16;
  9386. } u;
  9387. #else
  9388. duk_size_t listlen; /* if 0, 'str' used, if > 0, 'strlist' used */
  9389. union {
  9390. duk_hstring **strlist;
  9391. duk_hstring *str;
  9392. } u;
  9393. #endif
  9394. };
  9395. /*
  9396. * Main heap structure
  9397. */
  9398. struct duk_heap {
  9399. duk_small_uint_t flags;
  9400. /* Allocator functions. */
  9401. duk_alloc_function alloc_func;
  9402. duk_realloc_function realloc_func;
  9403. duk_free_function free_func;
  9404. /* Heap udata, used for allocator functions but also for other heap
  9405. * level callbacks like pointer compression, etc.
  9406. */
  9407. void *heap_udata;
  9408. /* Precomputed pointers when using 16-bit heap pointer packing. */
  9409. #if defined(DUK_USE_HEAPPTR16)
  9410. duk_uint16_t heapptr_null16;
  9411. duk_uint16_t heapptr_deleted16;
  9412. #endif
  9413. /* Fatal error handling, called e.g. when a longjmp() is needed but
  9414. * lj.jmpbuf_ptr is NULL. fatal_func must never return; it's not
  9415. * declared as "noreturn" because doing that for typedefs is a bit
  9416. * challenging portability-wise.
  9417. */
  9418. duk_fatal_function fatal_func;
  9419. /* allocated heap objects */
  9420. duk_heaphdr *heap_allocated;
  9421. /* work list for objects whose refcounts are zero but which have not been
  9422. * "finalized"; avoids recursive C calls when refcounts go to zero in a
  9423. * chain of objects.
  9424. */
  9425. #if defined(DUK_USE_REFERENCE_COUNTING)
  9426. duk_heaphdr *refzero_list;
  9427. duk_heaphdr *refzero_list_tail;
  9428. #endif
  9429. #if defined(DUK_USE_MARK_AND_SWEEP)
  9430. /* mark-and-sweep control */
  9431. #if defined(DUK_USE_VOLUNTARY_GC)
  9432. duk_int_t mark_and_sweep_trigger_counter;
  9433. #endif
  9434. duk_int_t mark_and_sweep_recursion_depth;
  9435. /* mark-and-sweep flags automatically active (used for critical sections) */
  9436. duk_small_uint_t mark_and_sweep_base_flags;
  9437. /* work list for objects to be finalized (by mark-and-sweep) */
  9438. duk_heaphdr *finalize_list;
  9439. #endif
  9440. /* longjmp state */
  9441. duk_ljstate lj;
  9442. /* marker for detecting internal "double faults", see duk_error_throw.c */
  9443. duk_bool_t handling_error;
  9444. /* heap thread, used internally and for finalization */
  9445. duk_hthread *heap_thread;
  9446. /* current thread */
  9447. duk_hthread *curr_thread; /* currently running thread */
  9448. /* heap level "stash" object (e.g., various reachability roots) */
  9449. duk_hobject *heap_object;
  9450. /* duk_handle_call / duk_handle_safe_call recursion depth limiting */
  9451. duk_int_t call_recursion_depth;
  9452. duk_int_t call_recursion_limit;
  9453. /* mix-in value for computing string hashes; should be reasonably unpredictable */
  9454. duk_uint32_t hash_seed;
  9455. /* rnd_state for duk_util_tinyrandom.c */
  9456. duk_uint32_t rnd_state;
  9457. /* For manual debugging: instruction count based on executor and
  9458. * interrupt counter book-keeping. Inspect debug logs to see how
  9459. * they match up.
  9460. */
  9461. #if defined(DUK_USE_INTERRUPT_COUNTER) && defined(DUK_USE_DEBUG)
  9462. duk_int_t inst_count_exec;
  9463. duk_int_t inst_count_interrupt;
  9464. #endif
  9465. /* debugger */
  9466. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  9467. /* callbacks and udata; dbg_read_cb != NULL is used to indicate attached state */
  9468. duk_debug_read_function dbg_read_cb; /* required, NULL implies detached */
  9469. duk_debug_write_function dbg_write_cb; /* required */
  9470. duk_debug_peek_function dbg_peek_cb;
  9471. duk_debug_read_flush_function dbg_read_flush_cb;
  9472. duk_debug_write_flush_function dbg_write_flush_cb;
  9473. duk_debug_detached_function dbg_detached_cb;
  9474. void *dbg_udata;
  9475. /* debugger state, only relevant when attached */
  9476. duk_bool_t dbg_processing; /* currently processing messages or breakpoints: don't enter message processing recursively (e.g. no breakpoints when processing debugger eval) */
  9477. duk_bool_t dbg_paused; /* currently paused: talk with debug client until step/resume */
  9478. duk_bool_t dbg_state_dirty; /* resend state next time executor is about to run */
  9479. duk_bool_t dbg_force_restart; /* force executor restart to recheck breakpoints; used to handle function returns (see GH-303) */
  9480. duk_small_uint_t dbg_step_type; /* step type: none, step into, step over, step out */
  9481. duk_hthread *dbg_step_thread; /* borrowed; NULL if no step state (NULLed in unwind) */
  9482. duk_size_t dbg_step_csindex; /* callstack index */
  9483. duk_uint32_t dbg_step_startline; /* starting line number */
  9484. duk_breakpoint dbg_breakpoints[DUK_HEAP_MAX_BREAKPOINTS]; /* breakpoints: [0,breakpoint_count[ gc reachable */
  9485. duk_small_uint_t dbg_breakpoint_count;
  9486. duk_breakpoint *dbg_breakpoints_active[DUK_HEAP_MAX_BREAKPOINTS + 1]; /* currently active breakpoints: NULL term, borrowed pointers */
  9487. /* XXX: make active breakpoints actual copies instead of pointers? */
  9488. /* These are for rate limiting Status notifications and transport peeking. */
  9489. duk_uint32_t dbg_exec_counter; /* cumulative opcode execution count (overflows are OK) */
  9490. duk_uint32_t dbg_last_counter; /* value of dbg_exec_counter when we last did a Date-based check */
  9491. duk_double_t dbg_last_time; /* time when status/peek was last done (Date-based rate limit) */
  9492. /* Used to support single-byte stream lookahead. */
  9493. duk_bool_t dbg_have_next_byte;
  9494. duk_uint8_t dbg_next_byte;
  9495. #endif
  9496. /* string intern table (weak refs) */
  9497. #if defined(DUK_USE_STRTAB_PROBE)
  9498. #if defined(DUK_USE_HEAPPTR16)
  9499. duk_uint16_t *strtable16;
  9500. #else
  9501. duk_hstring **strtable;
  9502. #endif
  9503. duk_uint32_t st_size; /* alloc size in elements */
  9504. duk_uint32_t st_used; /* used elements (includes DELETED) */
  9505. #endif
  9506. /* XXX: static alloc is OK until separate chaining stringtable
  9507. * resizing is implemented.
  9508. */
  9509. #if defined(DUK_USE_STRTAB_CHAIN)
  9510. duk_strtab_entry strtable[DUK_STRTAB_CHAIN_SIZE];
  9511. #endif
  9512. /* string access cache (codepoint offset -> byte offset) for fast string
  9513. * character looping; 'weak' reference which needs special handling in GC.
  9514. */
  9515. duk_strcache strcache[DUK_HEAP_STRCACHE_SIZE];
  9516. /* built-in strings */
  9517. #if defined(DUK_USE_HEAPPTR16)
  9518. duk_uint16_t strs16[DUK_HEAP_NUM_STRINGS];
  9519. #else
  9520. duk_hstring *strs[DUK_HEAP_NUM_STRINGS];
  9521. #endif
  9522. };
  9523. /*
  9524. * Prototypes
  9525. */
  9526. DUK_INTERNAL_DECL
  9527. duk_heap *duk_heap_alloc(duk_alloc_function alloc_func,
  9528. duk_realloc_function realloc_func,
  9529. duk_free_function free_func,
  9530. void *heap_udata,
  9531. duk_fatal_function fatal_func);
  9532. DUK_INTERNAL_DECL void duk_heap_free(duk_heap *heap);
  9533. DUK_INTERNAL_DECL void duk_free_hobject_inner(duk_heap *heap, duk_hobject *h);
  9534. DUK_INTERNAL_DECL void duk_free_hbuffer_inner(duk_heap *heap, duk_hbuffer *h);
  9535. DUK_INTERNAL_DECL void duk_free_hstring_inner(duk_heap *heap, duk_hstring *h);
  9536. DUK_INTERNAL_DECL void duk_heap_free_heaphdr_raw(duk_heap *heap, duk_heaphdr *hdr);
  9537. DUK_INTERNAL_DECL void duk_heap_insert_into_heap_allocated(duk_heap *heap, duk_heaphdr *hdr);
  9538. #if defined(DUK_USE_DOUBLE_LINKED_HEAP) && defined(DUK_USE_REFERENCE_COUNTING)
  9539. DUK_INTERNAL_DECL void duk_heap_remove_any_from_heap_allocated(duk_heap *heap, duk_heaphdr *hdr);
  9540. #endif
  9541. #if defined(DUK_USE_INTERRUPT_COUNTER)
  9542. DUK_INTERNAL_DECL void duk_heap_switch_thread(duk_heap *heap, duk_hthread *new_thr);
  9543. #endif
  9544. #if 0 /*unused*/
  9545. DUK_INTERNAL_DECL duk_hstring *duk_heap_string_lookup(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen);
  9546. #endif
  9547. DUK_INTERNAL_DECL duk_hstring *duk_heap_string_intern(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen);
  9548. DUK_INTERNAL_DECL duk_hstring *duk_heap_string_intern_checked(duk_hthread *thr, const duk_uint8_t *str, duk_uint32_t len);
  9549. #if 0 /*unused*/
  9550. DUK_INTERNAL_DECL duk_hstring *duk_heap_string_lookup_u32(duk_heap *heap, duk_uint32_t val);
  9551. #endif
  9552. DUK_INTERNAL_DECL duk_hstring *duk_heap_string_intern_u32(duk_heap *heap, duk_uint32_t val);
  9553. DUK_INTERNAL_DECL duk_hstring *duk_heap_string_intern_u32_checked(duk_hthread *thr, duk_uint32_t val);
  9554. #if defined(DUK_USE_REFERENCE_COUNTING)
  9555. DUK_INTERNAL_DECL void duk_heap_string_remove(duk_heap *heap, duk_hstring *h);
  9556. #endif
  9557. #if defined(DUK_USE_MARK_AND_SWEEP) && defined(DUK_USE_MS_STRINGTABLE_RESIZE)
  9558. DUK_INTERNAL_DECL void duk_heap_force_strtab_resize(duk_heap *heap);
  9559. #endif
  9560. DUK_INTERNAL void duk_heap_free_strtab(duk_heap *heap);
  9561. #if defined(DUK_USE_DEBUG)
  9562. DUK_INTERNAL void duk_heap_dump_strtab(duk_heap *heap);
  9563. #endif
  9564. DUK_INTERNAL_DECL void duk_heap_strcache_string_remove(duk_heap *heap, duk_hstring *h);
  9565. DUK_INTERNAL_DECL duk_uint_fast32_t duk_heap_strcache_offset_char2byte(duk_hthread *thr, duk_hstring *h, duk_uint_fast32_t char_offset);
  9566. #if defined(DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS)
  9567. DUK_INTERNAL_DECL void *duk_default_alloc_function(void *udata, duk_size_t size);
  9568. DUK_INTERNAL_DECL void *duk_default_realloc_function(void *udata, void *ptr, duk_size_t newsize);
  9569. DUK_INTERNAL_DECL void duk_default_free_function(void *udata, void *ptr);
  9570. #endif
  9571. DUK_INTERNAL_DECL void *duk_heap_mem_alloc(duk_heap *heap, duk_size_t size);
  9572. DUK_INTERNAL_DECL void *duk_heap_mem_alloc_zeroed(duk_heap *heap, duk_size_t size);
  9573. DUK_INTERNAL_DECL void *duk_heap_mem_realloc(duk_heap *heap, void *ptr, duk_size_t newsize);
  9574. DUK_INTERNAL_DECL void *duk_heap_mem_realloc_indirect(duk_heap *heap, duk_mem_getptr cb, void *ud, duk_size_t newsize);
  9575. DUK_INTERNAL_DECL void duk_heap_mem_free(duk_heap *heap, void *ptr);
  9576. #ifdef DUK_USE_REFERENCE_COUNTING
  9577. #if !defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  9578. DUK_INTERNAL_DECL void duk_tval_incref(duk_tval *tv);
  9579. #endif
  9580. #if 0 /* unused */
  9581. DUK_INTERNAL_DECL void duk_tval_incref_allownull(duk_tval *tv);
  9582. #endif
  9583. DUK_INTERNAL_DECL void duk_tval_decref(duk_hthread *thr, duk_tval *tv);
  9584. #if 0 /* unused */
  9585. DUK_INTERNAL_DECL void duk_tval_decref_allownull(duk_hthread *thr, duk_tval *tv);
  9586. #endif
  9587. #if !defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  9588. DUK_INTERNAL_DECL void duk_heaphdr_incref(duk_heaphdr *h);
  9589. #endif
  9590. #if 0 /* unused */
  9591. DUK_INTERNAL_DECL void duk_heaphdr_incref_allownull(duk_heaphdr *h);
  9592. #endif
  9593. DUK_INTERNAL_DECL void duk_heaphdr_decref(duk_hthread *thr, duk_heaphdr *h);
  9594. DUK_INTERNAL_DECL void duk_heaphdr_decref_allownull(duk_hthread *thr, duk_heaphdr *h);
  9595. DUK_INTERNAL_DECL void duk_heaphdr_refzero(duk_hthread *thr, duk_heaphdr *h);
  9596. DUK_INTERNAL_DECL void duk_heaphdr_refcount_finalize(duk_hthread *thr, duk_heaphdr *hdr);
  9597. #else
  9598. /* no refcounting */
  9599. #endif
  9600. #if defined(DUK_USE_MARK_AND_SWEEP)
  9601. DUK_INTERNAL_DECL duk_bool_t duk_heap_mark_and_sweep(duk_heap *heap, duk_small_uint_t flags);
  9602. #endif
  9603. DUK_INTERNAL_DECL duk_uint32_t duk_heap_hashstring(duk_heap *heap, const duk_uint8_t *str, duk_size_t len);
  9604. #endif /* DUK_HEAP_H_INCLUDED */
  9605. #line 1 "duk_debugger.h"
  9606. #ifndef DUK_DEBUGGER_H_INCLUDED
  9607. #define DUK_DEBUGGER_H_INCLUDED
  9608. /* Debugger protocol version is defined in the public API header. */
  9609. #define DUK_DBG_MARKER_EOM 0x00
  9610. #define DUK_DBG_MARKER_REQUEST 0x01
  9611. #define DUK_DBG_MARKER_REPLY 0x02
  9612. #define DUK_DBG_MARKER_ERROR 0x03
  9613. #define DUK_DBG_MARKER_NOTIFY 0x04
  9614. #define DUK_DBG_ERR_UNKNOWN 0x00
  9615. #define DUK_DBG_ERR_UNSUPPORTED 0x01
  9616. #define DUK_DBG_ERR_TOOMANY 0x02
  9617. #define DUK_DBG_ERR_NOTFOUND 0x03
  9618. /* Initiated by Duktape */
  9619. #define DUK_DBG_CMD_STATUS 0x01
  9620. #define DUK_DBG_CMD_PRINT 0x02
  9621. #define DUK_DBG_CMD_ALERT 0x03
  9622. #define DUK_DBG_CMD_LOG 0x04
  9623. #define DUK_DBG_CMD_THROW 0x05
  9624. /* Initiated by debug client */
  9625. #define DUK_DBG_CMD_BASICINFO 0x10
  9626. #define DUK_DBG_CMD_TRIGGERSTATUS 0x11
  9627. #define DUK_DBG_CMD_PAUSE 0x12
  9628. #define DUK_DBG_CMD_RESUME 0x13
  9629. #define DUK_DBG_CMD_STEPINTO 0x14
  9630. #define DUK_DBG_CMD_STEPOVER 0x15
  9631. #define DUK_DBG_CMD_STEPOUT 0x16
  9632. #define DUK_DBG_CMD_LISTBREAK 0x17
  9633. #define DUK_DBG_CMD_ADDBREAK 0x18
  9634. #define DUK_DBG_CMD_DELBREAK 0x19
  9635. #define DUK_DBG_CMD_GETVAR 0x1a
  9636. #define DUK_DBG_CMD_PUTVAR 0x1b
  9637. #define DUK_DBG_CMD_GETCALLSTACK 0x1c
  9638. #define DUK_DBG_CMD_GETLOCALS 0x1d
  9639. #define DUK_DBG_CMD_EVAL 0x1e
  9640. #define DUK_DBG_CMD_DETACH 0x1f
  9641. #define DUK_DBG_CMD_DUMPHEAP 0x20
  9642. #define DUK_DBG_CMD_GETBYTECODE 0x21
  9643. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  9644. DUK_INTERNAL_DECL void duk_debug_do_detach(duk_heap *heap);
  9645. DUK_INTERNAL_DECL duk_bool_t duk_debug_read_peek(duk_hthread *thr);
  9646. DUK_INTERNAL_DECL void duk_debug_write_flush(duk_hthread *thr);
  9647. DUK_INTERNAL_DECL void duk_debug_skip_bytes(duk_hthread *thr, duk_size_t length);
  9648. DUK_INTERNAL_DECL void duk_debug_skip_byte(duk_hthread *thr);
  9649. DUK_INTERNAL_DECL void duk_debug_read_bytes(duk_hthread *thr, duk_uint8_t *data, duk_size_t length);
  9650. DUK_INTERNAL_DECL duk_uint8_t duk_debug_read_byte(duk_hthread *thr);
  9651. DUK_INTERNAL_DECL duk_int32_t duk_debug_read_int(duk_hthread *thr);
  9652. DUK_INTERNAL_DECL duk_hstring *duk_debug_read_hstring(duk_hthread *thr);
  9653. /* XXX: exposed duk_debug_read_pointer */
  9654. /* XXX: exposed duk_debug_read_buffer */
  9655. /* XXX: exposed duk_debug_read_hbuffer */
  9656. DUK_INTERNAL_DECL void duk_debug_read_tval(duk_hthread *thr);
  9657. DUK_INTERNAL_DECL void duk_debug_write_bytes(duk_hthread *thr, const duk_uint8_t *data, duk_size_t length);
  9658. DUK_INTERNAL_DECL void duk_debug_write_byte(duk_hthread *thr, duk_uint8_t x);
  9659. DUK_INTERNAL_DECL void duk_debug_write_unused(duk_hthread *thr);
  9660. DUK_INTERNAL_DECL void duk_debug_write_undefined(duk_hthread *thr);
  9661. DUK_INTERNAL_DECL void duk_debug_write_int(duk_hthread *thr, duk_int32_t x);
  9662. DUK_INTERNAL_DECL void duk_debug_write_uint(duk_hthread *thr, duk_uint32_t x);
  9663. DUK_INTERNAL_DECL void duk_debug_write_string(duk_hthread *thr, const char *data, duk_size_t length);
  9664. DUK_INTERNAL_DECL void duk_debug_write_cstring(duk_hthread *thr, const char *data);
  9665. DUK_INTERNAL_DECL void duk_debug_write_hstring(duk_hthread *thr, duk_hstring *h);
  9666. DUK_INTERNAL_DECL void duk_debug_write_buffer(duk_hthread *thr, const char *data, duk_size_t length);
  9667. DUK_INTERNAL_DECL void duk_debug_write_hbuffer(duk_hthread *thr, duk_hbuffer *h);
  9668. DUK_INTERNAL_DECL void duk_debug_write_pointer(duk_hthread *thr, void *ptr);
  9669. #if defined(DUK_USE_DEBUGGER_DUMPHEAP)
  9670. DUK_INTERNAL_DECL void duk_debug_write_heapptr(duk_hthread *thr, duk_heaphdr *h);
  9671. #endif
  9672. DUK_INTERNAL_DECL void duk_debug_write_hobject(duk_hthread *thr, duk_hobject *obj);
  9673. DUK_INTERNAL_DECL void duk_debug_write_tval(duk_hthread *thr, duk_tval *tv);
  9674. #if 0 /* unused */
  9675. DUK_INTERNAL_DECL void duk_debug_write_request(duk_hthread *thr, duk_small_uint_t command);
  9676. #endif
  9677. DUK_INTERNAL_DECL void duk_debug_write_reply(duk_hthread *thr);
  9678. DUK_INTERNAL_DECL void duk_debug_write_error_eom(duk_hthread *thr, duk_small_uint_t err_code, const char *msg);
  9679. DUK_INTERNAL_DECL void duk_debug_write_notify(duk_hthread *thr, duk_small_uint_t command);
  9680. DUK_INTERNAL_DECL void duk_debug_write_eom(duk_hthread *thr);
  9681. DUK_INTERNAL duk_uint_fast32_t duk_debug_curr_line(duk_hthread *thr);
  9682. DUK_INTERNAL void duk_debug_send_status(duk_hthread *thr);
  9683. DUK_INTERNAL_DECL void duk_debug_send_throw(duk_hthread *thr, duk_bool_t fatal);
  9684. DUK_INTERNAL_DECL void duk_debug_halt_execution(duk_hthread *thr, duk_bool_t use_prev_pc);
  9685. DUK_INTERNAL_DECL duk_bool_t duk_debug_process_messages(duk_hthread *thr, duk_bool_t no_block);
  9686. DUK_INTERNAL_DECL duk_small_int_t duk_debug_add_breakpoint(duk_hthread *thr, duk_hstring *filename, duk_uint32_t line);
  9687. DUK_INTERNAL_DECL duk_bool_t duk_debug_remove_breakpoint(duk_hthread *thr, duk_small_uint_t breakpoint_index);
  9688. #endif
  9689. #endif /* DUK_DEBUGGER_H_INCLUDED */
  9690. #line 1 "duk_debug.h"
  9691. /*
  9692. * Debugging macros, DUK_DPRINT() and its variants in particular.
  9693. *
  9694. * DUK_DPRINT() allows formatted debug prints, and supports standard
  9695. * and Duktape specific formatters. See duk_debug_vsnprintf.c for details.
  9696. *
  9697. * DUK_D(x), DUK_DD(x), and DUK_DDD(x) are used together with log macros
  9698. * for technical reasons. They are concretely used to hide 'x' from the
  9699. * compiler when the corresponding log level is disabled. This allows
  9700. * clean builds on non-C99 compilers, at the cost of more verbose code.
  9701. * Examples:
  9702. *
  9703. * DUK_D(DUK_DPRINT("foo"));
  9704. * DUK_DD(DUK_DDPRINT("foo"));
  9705. * DUK_DDD(DUK_DDDPRINT("foo"));
  9706. *
  9707. * This approach is preferable to the old "double parentheses" hack because
  9708. * double parentheses make the C99 solution worse: __FILE__ and __LINE__ can
  9709. * no longer be added transparently without going through globals, which
  9710. * works poorly with threading.
  9711. */
  9712. #ifndef DUK_DEBUG_H_INCLUDED
  9713. #define DUK_DEBUG_H_INCLUDED
  9714. #ifdef DUK_USE_DEBUG
  9715. #if defined(DUK_USE_DPRINT)
  9716. #define DUK_D(x) x
  9717. #else
  9718. #define DUK_D(x) do { } while (0) /* omit */
  9719. #endif
  9720. #if defined(DUK_USE_DDPRINT)
  9721. #define DUK_DD(x) x
  9722. #else
  9723. #define DUK_DD(x) do { } while (0) /* omit */
  9724. #endif
  9725. #if defined(DUK_USE_DDDPRINT)
  9726. #define DUK_DDD(x) x
  9727. #else
  9728. #define DUK_DDD(x) do { } while (0) /* omit */
  9729. #endif
  9730. /*
  9731. * Exposed debug macros: debugging enabled
  9732. */
  9733. #define DUK_LEVEL_DEBUG 1
  9734. #define DUK_LEVEL_DDEBUG 2
  9735. #define DUK_LEVEL_DDDEBUG 3
  9736. #ifdef DUK_USE_VARIADIC_MACROS
  9737. /* Note: combining __FILE__, __LINE__, and __func__ into fmt would be
  9738. * possible compile time, but waste some space with shared function names.
  9739. */
  9740. #define DUK__DEBUG_LOG(lev,...) duk_debug_log((duk_small_int_t) (lev), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, DUK_FUNC_MACRO, __VA_ARGS__);
  9741. #define DUK_DPRINT(...) DUK__DEBUG_LOG(DUK_LEVEL_DEBUG, __VA_ARGS__)
  9742. #ifdef DUK_USE_DDPRINT
  9743. #define DUK_DDPRINT(...) DUK__DEBUG_LOG(DUK_LEVEL_DDEBUG, __VA_ARGS__)
  9744. #else
  9745. #define DUK_DDPRINT(...)
  9746. #endif
  9747. #ifdef DUK_USE_DDDPRINT
  9748. #define DUK_DDDPRINT(...) DUK__DEBUG_LOG(DUK_LEVEL_DDDEBUG, __VA_ARGS__)
  9749. #else
  9750. #define DUK_DDDPRINT(...)
  9751. #endif
  9752. #else /* DUK_USE_VARIADIC_MACROS */
  9753. #define DUK__DEBUG_STASH(lev) \
  9754. (void) DUK_SNPRINTF(duk_debug_file_stash, DUK_DEBUG_STASH_SIZE, "%s", (const char *) DUK_FILE_MACRO), \
  9755. duk_debug_file_stash[DUK_DEBUG_STASH_SIZE - 1] = (char) 0; \
  9756. (void) DUK_SNPRINTF(duk_debug_line_stash, DUK_DEBUG_STASH_SIZE, "%ld", (long) DUK_LINE_MACRO), \
  9757. duk_debug_line_stash[DUK_DEBUG_STASH_SIZE - 1] = (char) 0; \
  9758. (void) DUK_SNPRINTF(duk_debug_func_stash, DUK_DEBUG_STASH_SIZE, "%s", (const char *) DUK_FUNC_MACRO), \
  9759. duk_debug_func_stash[DUK_DEBUG_STASH_SIZE - 1] = (char) 0; \
  9760. (void) (duk_debug_level_stash = (lev))
  9761. /* Without variadic macros resort to comma expression trickery to handle debug
  9762. * prints. This generates a lot of harmless warnings. These hacks are not
  9763. * needed normally because DUK_D() and friends will hide the entire debug log
  9764. * statement from the compiler.
  9765. */
  9766. #ifdef DUK_USE_DPRINT
  9767. #define DUK_DPRINT DUK__DEBUG_STASH(DUK_LEVEL_DEBUG), (void) duk_debug_log /* args go here in parens */
  9768. #else
  9769. #define DUK_DPRINT 0 && /* args go here as a comma expression in parens */
  9770. #endif
  9771. #ifdef DUK_USE_DDPRINT
  9772. #define DUK_DDPRINT DUK__DEBUG_STASH(DUK_LEVEL_DDEBUG), (void) duk_debug_log /* args go here in parens */
  9773. #else
  9774. #define DUK_DDPRINT 0 && /* args */
  9775. #endif
  9776. #ifdef DUK_USE_DDDPRINT
  9777. #define DUK_DDDPRINT DUK__DEBUG_STASH(DUK_LEVEL_DDDEBUG), (void) duk_debug_log /* args go here in parens */
  9778. #else
  9779. #define DUK_DDDPRINT 0 && /* args */
  9780. #endif
  9781. #endif /* DUK_USE_VARIADIC_MACROS */
  9782. #else /* DUK_USE_DEBUG */
  9783. /*
  9784. * Exposed debug macros: debugging disabled
  9785. */
  9786. #define DUK_D(x) do { } while (0) /* omit */
  9787. #define DUK_DD(x) do { } while (0) /* omit */
  9788. #define DUK_DDD(x) do { } while (0) /* omit */
  9789. #ifdef DUK_USE_VARIADIC_MACROS
  9790. #define DUK_DPRINT(...)
  9791. #define DUK_DDPRINT(...)
  9792. #define DUK_DDDPRINT(...)
  9793. #else /* DUK_USE_VARIADIC_MACROS */
  9794. #define DUK_DPRINT 0 && /* args go here as a comma expression in parens */
  9795. #define DUK_DDPRINT 0 && /* args */
  9796. #define DUK_DDDPRINT 0 && /* args */
  9797. #endif /* DUK_USE_VARIADIC_MACROS */
  9798. #endif /* DUK_USE_DEBUG */
  9799. /*
  9800. * Structs
  9801. */
  9802. #ifdef DUK_USE_DEBUG
  9803. struct duk_fixedbuffer {
  9804. duk_uint8_t *buffer;
  9805. duk_size_t length;
  9806. duk_size_t offset;
  9807. duk_bool_t truncated;
  9808. };
  9809. #endif
  9810. /*
  9811. * Prototypes
  9812. */
  9813. #ifdef DUK_USE_DEBUG
  9814. DUK_INTERNAL_DECL duk_int_t duk_debug_vsnprintf(char *str, duk_size_t size, const char *format, va_list ap);
  9815. #if 0 /*unused*/
  9816. DUK_INTERNAL_DECL duk_int_t duk_debug_snprintf(char *str, duk_size_t size, const char *format, ...);
  9817. #endif
  9818. DUK_INTERNAL_DECL void duk_debug_format_funcptr(char *buf, duk_size_t buf_size, duk_uint8_t *fptr, duk_size_t fptr_size);
  9819. #ifdef DUK_USE_VARIADIC_MACROS
  9820. DUK_INTERNAL_DECL void duk_debug_log(duk_small_int_t level, const char *file, duk_int_t line, const char *func, const char *fmt, ...);
  9821. #else /* DUK_USE_VARIADIC_MACROS */
  9822. /* parameter passing, not thread safe */
  9823. #define DUK_DEBUG_STASH_SIZE 128
  9824. #if !defined(DUK_SINGLE_FILE)
  9825. DUK_INTERNAL_DECL char duk_debug_file_stash[DUK_DEBUG_STASH_SIZE];
  9826. DUK_INTERNAL_DECL char duk_debug_line_stash[DUK_DEBUG_STASH_SIZE];
  9827. DUK_INTERNAL_DECL char duk_debug_func_stash[DUK_DEBUG_STASH_SIZE];
  9828. DUK_INTERNAL_DECL duk_small_int_t duk_debug_level_stash;
  9829. #endif
  9830. DUK_INTERNAL_DECL void duk_debug_log(const char *fmt, ...);
  9831. #endif /* DUK_USE_VARIADIC_MACROS */
  9832. DUK_INTERNAL_DECL void duk_fb_put_bytes(duk_fixedbuffer *fb, duk_uint8_t *buffer, duk_size_t length);
  9833. DUK_INTERNAL_DECL void duk_fb_put_byte(duk_fixedbuffer *fb, duk_uint8_t x);
  9834. DUK_INTERNAL_DECL void duk_fb_put_cstring(duk_fixedbuffer *fb, const char *x);
  9835. DUK_INTERNAL_DECL void duk_fb_sprintf(duk_fixedbuffer *fb, const char *fmt, ...);
  9836. DUK_INTERNAL_DECL void duk_fb_put_funcptr(duk_fixedbuffer *fb, duk_uint8_t *fptr, duk_size_t fptr_size);
  9837. DUK_INTERNAL_DECL duk_bool_t duk_fb_is_full(duk_fixedbuffer *fb);
  9838. #endif /* DUK_USE_DEBUG */
  9839. #endif /* DUK_DEBUG_H_INCLUDED */
  9840. #line 1 "duk_error.h"
  9841. /*
  9842. * Error handling macros, assertion macro, error codes.
  9843. *
  9844. * There are three level of 'errors':
  9845. *
  9846. * 1. Ordinary errors, relative to a thread, cause a longjmp, catchable.
  9847. * 2. Fatal errors, relative to a heap, cause fatal handler to be called.
  9848. * 3. Panic errors, unrelated to a heap and cause a process exit.
  9849. *
  9850. * Panics are used by the default fatal error handler and by debug code
  9851. * such as assertions. By providing a proper fatal error handler, user
  9852. * code can avoid panics in non-debug builds.
  9853. */
  9854. #ifndef DUK_ERROR_H_INCLUDED
  9855. #define DUK_ERROR_H_INCLUDED
  9856. /*
  9857. * Error codes: defined in duktape.h
  9858. *
  9859. * Error codes are used as a shorthand to throw exceptions from inside
  9860. * the implementation. The appropriate Ecmascript object is constructed
  9861. * based on the code. Ecmascript code throws objects directly. The error
  9862. * codes are defined in the public API header because they are also used
  9863. * by calling code.
  9864. */
  9865. /*
  9866. * Normal error
  9867. *
  9868. * Normal error is thrown with a longjmp() through the current setjmp()
  9869. * catchpoint record in the duk_heap. The 'curr_thread' of the duk_heap
  9870. * identifies the throwing thread.
  9871. *
  9872. * Error formatting is not always necessary but there are no separate calls
  9873. * (to minimize code size). Error object creation will consume a considerable
  9874. * amount of time, compared to which formatting is probably trivial. Note
  9875. * that special formatting (provided by DUK_DEBUG macros) is NOT available.
  9876. *
  9877. * The _RAW variants allow the caller to specify file and line. This makes
  9878. * it easier to write checked calls which want to use the call site of the
  9879. * checked function, not the error macro call inside the checked function.
  9880. *
  9881. * We prefer the standard variadic macros; if they are not available, we
  9882. * fall back to awkward hacks.
  9883. */
  9884. #ifdef DUK_USE_VERBOSE_ERRORS
  9885. #ifdef DUK_USE_VARIADIC_MACROS
  9886. /* __VA_ARGS__ has comma issues for empty lists, so we mandate at least 1 argument for '...' (format string) */
  9887. #define DUK_ERROR(thr,err,...) duk_err_handle_error(DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (thr), (err), __VA_ARGS__)
  9888. #define DUK_ERROR_RAW(file,line,thr,err,...) duk_err_handle_error((file), (line), (thr), (err), __VA_ARGS__)
  9889. #else /* DUK_USE_VARIADIC_MACROS */
  9890. /* Parameter passing here is not thread safe. We rely on the __FILE__
  9891. * pointer being a constant which can be passed through a global.
  9892. */
  9893. #define DUK_ERROR \
  9894. (void) (duk_err_file_stash = (const char *) DUK_FILE_MACRO, \
  9895. duk_err_line_stash = (duk_int_t) DUK_LINE_MACRO, \
  9896. duk_err_handle_error_stash) /* arguments follow */
  9897. #define DUK_ERROR_RAW duk_err_handle_error
  9898. #endif /* DUK_USE_VARIADIC_MACROS */
  9899. #else /* DUK_USE_VERBOSE_ERRORS */
  9900. #ifdef DUK_USE_VARIADIC_MACROS
  9901. #define DUK_ERROR(thr,err,...) duk_err_handle_error((thr), (err))
  9902. #define DUK_ERROR_RAW(file,line,thr,err,...) duk_err_handle_error((thr), (err))
  9903. #else /* DUK_USE_VARIADIC_MACROS */
  9904. /* This is sub-optimal because arguments will be passed but ignored, and the strings
  9905. * will go into the object file. Can't think of how to do this portably and still
  9906. * relatively conveniently.
  9907. */
  9908. #define DUK_ERROR duk_err_handle_error_nonverbose1
  9909. #define DUK_ERROR_RAW duk_err_handle_error_nonverbose2
  9910. #endif /* DUK_USE_VARIADIC_MACROS */
  9911. #endif /* DUK_USE_VERBOSE_ERRORS */
  9912. /*
  9913. * Fatal error
  9914. *
  9915. * There are no fatal error macros at the moment. There are so few call
  9916. * sites that the fatal error handler is called directly.
  9917. */
  9918. /*
  9919. * Panic error
  9920. *
  9921. * Panic errors are not relative to either a heap or a thread, and cause
  9922. * DUK_PANIC() macro to be invoked. Unless a user provides DUK_USE_PANIC_HANDLER,
  9923. * DUK_PANIC() calls a helper which prints out the error and causes a process
  9924. * exit.
  9925. *
  9926. * The user can override the macro to provide custom handling. A macro is
  9927. * used to allow the user to have inline panic handling if desired (without
  9928. * causing a potentially risky function call).
  9929. *
  9930. * Panics are only used in debug code such as assertions, and by the default
  9931. * fatal error handler.
  9932. */
  9933. #if defined(DUK_USE_PANIC_HANDLER)
  9934. /* already defined, good */
  9935. #define DUK_PANIC(code,msg) DUK_USE_PANIC_HANDLER((code),(msg))
  9936. #else
  9937. #define DUK_PANIC(code,msg) duk_default_panic_handler((code),(msg))
  9938. #endif /* DUK_USE_PANIC_HANDLER */
  9939. /*
  9940. * Assert macro: failure causes panic.
  9941. */
  9942. #ifdef DUK_USE_ASSERTIONS
  9943. /* the message should be a compile time constant without formatting (less risk);
  9944. * we don't care about assertion text size because they're not used in production
  9945. * builds.
  9946. */
  9947. #define DUK_ASSERT(x) do { \
  9948. if (!(x)) { \
  9949. DUK_PANIC(DUK_ERR_ASSERTION_ERROR, \
  9950. "assertion failed: " #x \
  9951. " (" DUK_FILE_MACRO ":" DUK_MACRO_STRINGIFY(DUK_LINE_MACRO) ")"); \
  9952. } \
  9953. } while (0)
  9954. /* Assertion compatible inside a comma expression, evaluates to void.
  9955. * Currently not compatible with DUK_USE_PANIC_HANDLER() which may have
  9956. * a statement block.
  9957. */
  9958. #if defined(DUK_USE_PANIC_HANDLER)
  9959. /* XXX: resolve macro definition issue or call through a helper function? */
  9960. #define DUK_ASSERT_EXPR(x) ((void) 0)
  9961. #else
  9962. #define DUK_ASSERT_EXPR(x) \
  9963. ((void) ((x) ? 0 : (DUK_PANIC(DUK_ERR_ASSERTION_ERROR, \
  9964. "assertion failed: " #x \
  9965. " (" DUK_FILE_MACRO ":" DUK_MACRO_STRINGIFY(DUK_LINE_MACRO) ")"), 0)))
  9966. #endif
  9967. #else /* DUK_USE_ASSERTIONS */
  9968. #define DUK_ASSERT(x) do { /* assertion omitted */ } while (0)
  9969. #define DUK_ASSERT_EXPR(x) ((void) 0)
  9970. #endif /* DUK_USE_ASSERTIONS */
  9971. /* this variant is used when an assert would generate a compile warning by
  9972. * being always true (e.g. >= 0 comparison for an unsigned value
  9973. */
  9974. #define DUK_ASSERT_DISABLE(x) do { /* assertion disabled */ } while (0)
  9975. /*
  9976. * Assertion helpers
  9977. */
  9978. #if defined(DUK_USE_ASSERTIONS) && defined(DUK_USE_REFERENCE_COUNTING)
  9979. #define DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(h) do { \
  9980. DUK_ASSERT((h) == NULL || DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) (h)) > 0); \
  9981. } while (0)
  9982. #define DUK_ASSERT_REFCOUNT_NONZERO_TVAL(tv) do { \
  9983. if ((tv) != NULL && DUK_TVAL_IS_HEAP_ALLOCATED((tv))) { \
  9984. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(DUK_TVAL_GET_HEAPHDR((tv))) > 0); \
  9985. } \
  9986. } while (0)
  9987. #else
  9988. #define DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(h) /* no refcount check */
  9989. #define DUK_ASSERT_REFCOUNT_NONZERO_TVAL(tv) /* no refcount check */
  9990. #endif
  9991. #define DUK_ASSERT_TOP(ctx,n) DUK_ASSERT((duk_idx_t) duk_get_top((ctx)) == (duk_idx_t) (n))
  9992. #if defined(DUK_USE_ASSERTIONS) && defined(DUK_USE_PACKED_TVAL)
  9993. #define DUK_ASSERT_DOUBLE_IS_NORMALIZED(dval) do { \
  9994. duk_double_union duk__assert_tmp_du; \
  9995. duk__assert_tmp_du.d = (dval); \
  9996. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&duk__assert_tmp_du)); \
  9997. } while (0)
  9998. #else
  9999. #define DUK_ASSERT_DOUBLE_IS_NORMALIZED(dval) /* nop */
  10000. #endif
  10001. /*
  10002. * Helper for valstack space
  10003. *
  10004. * Caller of DUK_ASSERT_VALSTACK_SPACE() estimates the number of free stack entries
  10005. * required for its own use, and any child calls which are not (a) Duktape API calls
  10006. * or (b) Duktape calls which involve extending the valstack (e.g. getter call).
  10007. */
  10008. #define DUK_VALSTACK_ASSERT_EXTRA 5 /* this is added to checks to allow for Duktape
  10009. * API calls in addition to function's own use
  10010. */
  10011. #if defined(DUK_USE_ASSERTIONS)
  10012. #define DUK_ASSERT_VALSTACK_SPACE(thr,n) do { \
  10013. DUK_ASSERT((thr) != NULL); \
  10014. DUK_ASSERT((thr)->valstack_end - (thr)->valstack_top >= (n) + DUK_VALSTACK_ASSERT_EXTRA); \
  10015. } while (0)
  10016. #else
  10017. #define DUK_ASSERT_VALSTACK_SPACE(thr,n) /* no valstack space check */
  10018. #endif
  10019. /*
  10020. * Prototypes
  10021. */
  10022. #ifdef DUK_USE_VERBOSE_ERRORS
  10023. #ifdef DUK_USE_VARIADIC_MACROS
  10024. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...));
  10025. #else /* DUK_USE_VARIADIC_MACROS */
  10026. #if !defined(DUK_SINGLE_FILE)
  10027. DUK_INTERNAL_DECL const char *duk_err_file_stash;
  10028. DUK_INTERNAL_DECL duk_int_t duk_err_line_stash;
  10029. #endif /* !DUK_SINGLE_FILE */
  10030. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...));
  10031. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error_stash(duk_hthread *thr, duk_errcode_t code, const char *fmt, ...));
  10032. #endif /* DUK_USE_VARIADIC_MACROS */
  10033. #else /* DUK_USE_VERBOSE_ERRORS */
  10034. #ifdef DUK_USE_VARIADIC_MACROS
  10035. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error(duk_hthread *thr, duk_errcode_t code));
  10036. #else /* DUK_USE_VARIADIC_MACROS */
  10037. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error_nonverbose1(duk_hthread *thr, duk_errcode_t code, const char *fmt, ...));
  10038. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error_nonverbose2(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...));
  10039. #endif /* DUK_USE_VARIADIC_MACROS */
  10040. #endif /* DUK_USE_VERBOSE_ERRORS */
  10041. #ifdef DUK_USE_VERBOSE_ERRORS
  10042. 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));
  10043. #else
  10044. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_create_and_throw(duk_hthread *thr, duk_errcode_t code));
  10045. #endif
  10046. DUK_NORETURN(DUK_INTERNAL_DECL void duk_error_throw_from_negative_rc(duk_hthread *thr, duk_ret_t rc));
  10047. #if defined(DUK_USE_AUGMENT_ERROR_CREATE)
  10048. DUK_INTERNAL_DECL void duk_err_augment_error_create(duk_hthread *thr, duk_hthread *thr_callstack, const char *filename, duk_int_t line, duk_bool_t noblame_fileline);
  10049. #endif
  10050. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  10051. DUK_INTERNAL_DECL void duk_err_augment_error_throw(duk_hthread *thr);
  10052. #endif
  10053. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_longjmp(duk_hthread *thr));
  10054. DUK_NORETURN(DUK_INTERNAL_DECL void duk_default_fatal_handler(duk_context *ctx, duk_errcode_t code, const char *msg));
  10055. #if !defined(DUK_USE_PANIC_HANDLER)
  10056. DUK_NORETURN(DUK_INTERNAL_DECL void duk_default_panic_handler(duk_errcode_t code, const char *msg));
  10057. #endif
  10058. DUK_INTERNAL_DECL void duk_err_setup_heap_ljstate(duk_hthread *thr, duk_small_int_t lj_type);
  10059. DUK_INTERNAL_DECL duk_hobject *duk_error_prototype_from_code(duk_hthread *thr, duk_errcode_t err_code);
  10060. #endif /* DUK_ERROR_H_INCLUDED */
  10061. #line 1 "duk_unicode.h"
  10062. /*
  10063. * Unicode helpers
  10064. */
  10065. #ifndef DUK_UNICODE_H_INCLUDED
  10066. #define DUK_UNICODE_H_INCLUDED
  10067. /*
  10068. * UTF-8 / XUTF-8 / CESU-8 constants
  10069. */
  10070. #define DUK_UNICODE_MAX_XUTF8_LENGTH 7 /* up to 36 bit codepoints */
  10071. #define DUK_UNICODE_MAX_XUTF8_BMP_LENGTH 3 /* all codepoints up to U+FFFF */
  10072. #define DUK_UNICODE_MAX_CESU8_LENGTH 6 /* all codepoints up to U+10FFFF */
  10073. #define DUK_UNICODE_MAX_CESU8_BMP_LENGTH 3 /* all codepoints up to U+FFFF */
  10074. /*
  10075. * Useful Unicode codepoints
  10076. *
  10077. * Integer constants must be signed to avoid unexpected coercions
  10078. * in comparisons.
  10079. */
  10080. #define DUK_UNICODE_CP_ZWNJ 0x200cL /* zero-width non-joiner */
  10081. #define DUK_UNICODE_CP_ZWJ 0x200dL /* zero-width joiner */
  10082. #define DUK_UNICODE_CP_REPLACEMENT_CHARACTER 0xfffdL /* http://en.wikipedia.org/wiki/Replacement_character#Replacement_character */
  10083. /*
  10084. * ASCII character constants
  10085. *
  10086. * C character literals like 'x' have a platform specific value and do
  10087. * not match ASCII (UTF-8) values on e.g. EBCDIC platforms. So, use
  10088. * these (admittedly awkward) constants instead. These constants must
  10089. * also have signed values to avoid unexpected coercions in comparisons.
  10090. *
  10091. * http://en.wikipedia.org/wiki/ASCII
  10092. */
  10093. #define DUK_ASC_NUL 0x00
  10094. #define DUK_ASC_SOH 0x01
  10095. #define DUK_ASC_STX 0x02
  10096. #define DUK_ASC_ETX 0x03
  10097. #define DUK_ASC_EOT 0x04
  10098. #define DUK_ASC_ENQ 0x05
  10099. #define DUK_ASC_ACK 0x06
  10100. #define DUK_ASC_BEL 0x07
  10101. #define DUK_ASC_BS 0x08
  10102. #define DUK_ASC_HT 0x09
  10103. #define DUK_ASC_LF 0x0a
  10104. #define DUK_ASC_VT 0x0b
  10105. #define DUK_ASC_FF 0x0c
  10106. #define DUK_ASC_CR 0x0d
  10107. #define DUK_ASC_SO 0x0e
  10108. #define DUK_ASC_SI 0x0f
  10109. #define DUK_ASC_DLE 0x10
  10110. #define DUK_ASC_DC1 0x11
  10111. #define DUK_ASC_DC2 0x12
  10112. #define DUK_ASC_DC3 0x13
  10113. #define DUK_ASC_DC4 0x14
  10114. #define DUK_ASC_NAK 0x15
  10115. #define DUK_ASC_SYN 0x16
  10116. #define DUK_ASC_ETB 0x17
  10117. #define DUK_ASC_CAN 0x18
  10118. #define DUK_ASC_EM 0x19
  10119. #define DUK_ASC_SUB 0x1a
  10120. #define DUK_ASC_ESC 0x1b
  10121. #define DUK_ASC_FS 0x1c
  10122. #define DUK_ASC_GS 0x1d
  10123. #define DUK_ASC_RS 0x1e
  10124. #define DUK_ASC_US 0x1f
  10125. #define DUK_ASC_SPACE 0x20
  10126. #define DUK_ASC_EXCLAMATION 0x21
  10127. #define DUK_ASC_DOUBLEQUOTE 0x22
  10128. #define DUK_ASC_HASH 0x23
  10129. #define DUK_ASC_DOLLAR 0x24
  10130. #define DUK_ASC_PERCENT 0x25
  10131. #define DUK_ASC_AMP 0x26
  10132. #define DUK_ASC_SINGLEQUOTE 0x27
  10133. #define DUK_ASC_LPAREN 0x28
  10134. #define DUK_ASC_RPAREN 0x29
  10135. #define DUK_ASC_STAR 0x2a
  10136. #define DUK_ASC_PLUS 0x2b
  10137. #define DUK_ASC_COMMA 0x2c
  10138. #define DUK_ASC_MINUS 0x2d
  10139. #define DUK_ASC_PERIOD 0x2e
  10140. #define DUK_ASC_SLASH 0x2f
  10141. #define DUK_ASC_0 0x30
  10142. #define DUK_ASC_1 0x31
  10143. #define DUK_ASC_2 0x32
  10144. #define DUK_ASC_3 0x33
  10145. #define DUK_ASC_4 0x34
  10146. #define DUK_ASC_5 0x35
  10147. #define DUK_ASC_6 0x36
  10148. #define DUK_ASC_7 0x37
  10149. #define DUK_ASC_8 0x38
  10150. #define DUK_ASC_9 0x39
  10151. #define DUK_ASC_COLON 0x3a
  10152. #define DUK_ASC_SEMICOLON 0x3b
  10153. #define DUK_ASC_LANGLE 0x3c
  10154. #define DUK_ASC_EQUALS 0x3d
  10155. #define DUK_ASC_RANGLE 0x3e
  10156. #define DUK_ASC_QUESTION 0x3f
  10157. #define DUK_ASC_ATSIGN 0x40
  10158. #define DUK_ASC_UC_A 0x41
  10159. #define DUK_ASC_UC_B 0x42
  10160. #define DUK_ASC_UC_C 0x43
  10161. #define DUK_ASC_UC_D 0x44
  10162. #define DUK_ASC_UC_E 0x45
  10163. #define DUK_ASC_UC_F 0x46
  10164. #define DUK_ASC_UC_G 0x47
  10165. #define DUK_ASC_UC_H 0x48
  10166. #define DUK_ASC_UC_I 0x49
  10167. #define DUK_ASC_UC_J 0x4a
  10168. #define DUK_ASC_UC_K 0x4b
  10169. #define DUK_ASC_UC_L 0x4c
  10170. #define DUK_ASC_UC_M 0x4d
  10171. #define DUK_ASC_UC_N 0x4e
  10172. #define DUK_ASC_UC_O 0x4f
  10173. #define DUK_ASC_UC_P 0x50
  10174. #define DUK_ASC_UC_Q 0x51
  10175. #define DUK_ASC_UC_R 0x52
  10176. #define DUK_ASC_UC_S 0x53
  10177. #define DUK_ASC_UC_T 0x54
  10178. #define DUK_ASC_UC_U 0x55
  10179. #define DUK_ASC_UC_V 0x56
  10180. #define DUK_ASC_UC_W 0x57
  10181. #define DUK_ASC_UC_X 0x58
  10182. #define DUK_ASC_UC_Y 0x59
  10183. #define DUK_ASC_UC_Z 0x5a
  10184. #define DUK_ASC_LBRACKET 0x5b
  10185. #define DUK_ASC_BACKSLASH 0x5c
  10186. #define DUK_ASC_RBRACKET 0x5d
  10187. #define DUK_ASC_CARET 0x5e
  10188. #define DUK_ASC_UNDERSCORE 0x5f
  10189. #define DUK_ASC_GRAVE 0x60
  10190. #define DUK_ASC_LC_A 0x61
  10191. #define DUK_ASC_LC_B 0x62
  10192. #define DUK_ASC_LC_C 0x63
  10193. #define DUK_ASC_LC_D 0x64
  10194. #define DUK_ASC_LC_E 0x65
  10195. #define DUK_ASC_LC_F 0x66
  10196. #define DUK_ASC_LC_G 0x67
  10197. #define DUK_ASC_LC_H 0x68
  10198. #define DUK_ASC_LC_I 0x69
  10199. #define DUK_ASC_LC_J 0x6a
  10200. #define DUK_ASC_LC_K 0x6b
  10201. #define DUK_ASC_LC_L 0x6c
  10202. #define DUK_ASC_LC_M 0x6d
  10203. #define DUK_ASC_LC_N 0x6e
  10204. #define DUK_ASC_LC_O 0x6f
  10205. #define DUK_ASC_LC_P 0x70
  10206. #define DUK_ASC_LC_Q 0x71
  10207. #define DUK_ASC_LC_R 0x72
  10208. #define DUK_ASC_LC_S 0x73
  10209. #define DUK_ASC_LC_T 0x74
  10210. #define DUK_ASC_LC_U 0x75
  10211. #define DUK_ASC_LC_V 0x76
  10212. #define DUK_ASC_LC_W 0x77
  10213. #define DUK_ASC_LC_X 0x78
  10214. #define DUK_ASC_LC_Y 0x79
  10215. #define DUK_ASC_LC_Z 0x7a
  10216. #define DUK_ASC_LCURLY 0x7b
  10217. #define DUK_ASC_PIPE 0x7c
  10218. #define DUK_ASC_RCURLY 0x7d
  10219. #define DUK_ASC_TILDE 0x7e
  10220. #define DUK_ASC_DEL 0x7f
  10221. /*
  10222. * Unicode tables
  10223. */
  10224. #ifdef DUK_USE_SOURCE_NONBMP
  10225. /*
  10226. * Automatically generated by extract_chars.py, do not edit!
  10227. */
  10228. extern const duk_uint8_t duk_unicode_ids_noa[791];
  10229. #else
  10230. /*
  10231. * Automatically generated by extract_chars.py, do not edit!
  10232. */
  10233. extern const duk_uint8_t duk_unicode_ids_noabmp[611];
  10234. #endif
  10235. #ifdef DUK_USE_SOURCE_NONBMP
  10236. /*
  10237. * Automatically generated by extract_chars.py, do not edit!
  10238. */
  10239. extern const duk_uint8_t duk_unicode_ids_m_let_noa[42];
  10240. #else
  10241. /*
  10242. * Automatically generated by extract_chars.py, do not edit!
  10243. */
  10244. extern const duk_uint8_t duk_unicode_ids_m_let_noabmp[24];
  10245. #endif
  10246. #ifdef DUK_USE_SOURCE_NONBMP
  10247. /*
  10248. * Automatically generated by extract_chars.py, do not edit!
  10249. */
  10250. extern const duk_uint8_t duk_unicode_idp_m_ids_noa[397];
  10251. #else
  10252. /*
  10253. * Automatically generated by extract_chars.py, do not edit!
  10254. */
  10255. extern const duk_uint8_t duk_unicode_idp_m_ids_noabmp[348];
  10256. #endif
  10257. /*
  10258. * Automatically generated by extract_caseconv.py, do not edit!
  10259. */
  10260. extern const duk_uint8_t duk_unicode_caseconv_uc[1288];
  10261. extern const duk_uint8_t duk_unicode_caseconv_lc[616];
  10262. /*
  10263. * Extern
  10264. */
  10265. /* duk_unicode_support.c */
  10266. #if !defined(DUK_SINGLE_FILE)
  10267. DUK_INTERNAL_DECL duk_uint8_t duk_unicode_xutf8_markers[7];
  10268. DUK_INTERNAL_DECL duk_uint16_t duk_unicode_re_ranges_digit[2];
  10269. DUK_INTERNAL_DECL duk_uint16_t duk_unicode_re_ranges_white[22];
  10270. DUK_INTERNAL_DECL duk_uint16_t duk_unicode_re_ranges_wordchar[8];
  10271. DUK_INTERNAL_DECL duk_uint16_t duk_unicode_re_ranges_not_digit[4];
  10272. DUK_INTERNAL_DECL duk_uint16_t duk_unicode_re_ranges_not_white[24];
  10273. DUK_INTERNAL_DECL duk_uint16_t duk_unicode_re_ranges_not_wordchar[10];
  10274. #endif /* !DUK_SINGLE_FILE */
  10275. /*
  10276. * Prototypes
  10277. */
  10278. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_get_xutf8_length(duk_ucodepoint_t cp);
  10279. #if defined(DUK_USE_ASSERTIONS)
  10280. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_get_cesu8_length(duk_ucodepoint_t cp);
  10281. #endif
  10282. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_encode_xutf8(duk_ucodepoint_t cp, duk_uint8_t *out);
  10283. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_encode_cesu8(duk_ucodepoint_t cp, duk_uint8_t *out);
  10284. 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);
  10285. 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);
  10286. DUK_INTERNAL_DECL duk_size_t duk_unicode_unvalidated_utf8_length(const duk_uint8_t *data, duk_size_t blen);
  10287. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_whitespace(duk_codepoint_t cp);
  10288. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_line_terminator(duk_codepoint_t cp);
  10289. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_identifier_start(duk_codepoint_t cp);
  10290. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_identifier_part(duk_codepoint_t cp);
  10291. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_letter(duk_codepoint_t cp);
  10292. DUK_INTERNAL_DECL void duk_unicode_case_convert_string(duk_hthread *thr, duk_bool_t uppercase);
  10293. DUK_INTERNAL_DECL duk_codepoint_t duk_unicode_re_canonicalize_char(duk_hthread *thr, duk_codepoint_t cp);
  10294. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_re_is_wordchar(duk_codepoint_t cp);
  10295. #endif /* DUK_UNICODE_H_INCLUDED */
  10296. #line 1 "duk_json.h"
  10297. /*
  10298. * Defines for JSON, especially duk_bi_json.c.
  10299. */
  10300. #ifndef DUK_JSON_H_INCLUDED
  10301. #define DUK_JSON_H_INCLUDED
  10302. /* Encoding/decoding flags */
  10303. #define DUK_JSON_FLAG_ASCII_ONLY (1 << 0) /* escape any non-ASCII characters */
  10304. #define DUK_JSON_FLAG_AVOID_KEY_QUOTES (1 << 1) /* avoid key quotes when key is an ASCII Identifier */
  10305. #define DUK_JSON_FLAG_EXT_CUSTOM (1 << 2) /* extended types: custom encoding */
  10306. #define DUK_JSON_FLAG_EXT_COMPATIBLE (1 << 3) /* extended types: compatible encoding */
  10307. /* How much stack to require on entry to object/array encode */
  10308. #define DUK_JSON_ENC_REQSTACK 32
  10309. /* How much stack to require on entry to object/array decode */
  10310. #define DUK_JSON_DEC_REQSTACK 32
  10311. /* How large a loop detection stack to use for fast path */
  10312. #if defined(DUK_USE_JSON_STRINGIFY_FASTPATH)
  10313. #define DUK_JSON_ENC_LOOPARRAY 64
  10314. #endif
  10315. /* Encoding state. Heap object references are all borrowed. */
  10316. typedef struct {
  10317. duk_hthread *thr;
  10318. duk_bufwriter_ctx bw; /* output bufwriter */
  10319. duk_hobject *h_replacer; /* replacer function */
  10320. duk_hstring *h_gap; /* gap (if empty string, NULL) */
  10321. duk_hstring *h_indent; /* current indent (if gap is NULL, this is NULL) */
  10322. duk_idx_t idx_proplist; /* explicit PropertyList */
  10323. duk_idx_t idx_loop; /* valstack index of loop detection object */
  10324. duk_small_uint_t flags;
  10325. duk_small_uint_t flag_ascii_only;
  10326. duk_small_uint_t flag_avoid_key_quotes;
  10327. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  10328. duk_small_uint_t flag_ext_custom;
  10329. duk_small_uint_t flag_ext_compatible;
  10330. #endif
  10331. duk_int_t recursion_depth;
  10332. duk_int_t recursion_limit;
  10333. duk_uint_t mask_for_undefined; /* type bit mask: types which certainly produce 'undefined' */
  10334. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  10335. duk_small_uint_t stridx_custom_undefined;
  10336. duk_small_uint_t stridx_custom_nan;
  10337. duk_small_uint_t stridx_custom_neginf;
  10338. duk_small_uint_t stridx_custom_posinf;
  10339. duk_small_uint_t stridx_custom_function;
  10340. #endif
  10341. #if defined(DUK_USE_JSON_STRINGIFY_FASTPATH)
  10342. duk_hobject *visiting[DUK_JSON_ENC_LOOPARRAY]; /* indexed by recursion_depth */
  10343. #endif
  10344. } duk_json_enc_ctx;
  10345. typedef struct {
  10346. duk_hthread *thr;
  10347. const duk_uint8_t *p;
  10348. const duk_uint8_t *p_start;
  10349. const duk_uint8_t *p_end;
  10350. duk_idx_t idx_reviver;
  10351. duk_small_uint_t flags;
  10352. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  10353. duk_small_uint_t flag_ext_custom;
  10354. duk_small_uint_t flag_ext_compatible;
  10355. #endif
  10356. duk_int_t recursion_depth;
  10357. duk_int_t recursion_limit;
  10358. } duk_json_dec_ctx;
  10359. #endif /* DUK_JSON_H_INCLUDED */
  10360. #line 1 "duk_js.h"
  10361. /*
  10362. * Ecmascript execution, support primitives.
  10363. */
  10364. #ifndef DUK_JS_H_INCLUDED
  10365. #define DUK_JS_H_INCLUDED
  10366. /* Flags for call handling. */
  10367. #define DUK_CALL_FLAG_PROTECTED (1 << 0) /* duk_handle_call: call is protected */
  10368. #define DUK_CALL_FLAG_IGNORE_RECLIMIT (1 << 1) /* duk_handle_call: call ignores C recursion limit (for errhandler calls) */
  10369. #define DUK_CALL_FLAG_CONSTRUCTOR_CALL (1 << 2) /* duk_handle_call: constructor call (i.e. called as 'new Foo()') */
  10370. #define DUK_CALL_FLAG_IS_RESUME (1 << 3) /* duk_handle_ecma_call_setup: setup for a resume() */
  10371. #define DUK_CALL_FLAG_IS_TAILCALL (1 << 4) /* duk_handle_ecma_call_setup: setup for a tail call */
  10372. #define DUK_CALL_FLAG_DIRECT_EVAL (1 << 5) /* call is a direct eval call */
  10373. /* Flags for duk_js_equals_helper(). */
  10374. #define DUK_EQUALS_FLAG_SAMEVALUE (1 << 0) /* use SameValue instead of non-strict equality */
  10375. #define DUK_EQUALS_FLAG_STRICT (1 << 1) /* use strict equality instead of non-strict equality */
  10376. /* Flags for duk_js_compare_helper(). */
  10377. #define DUK_COMPARE_FLAG_EVAL_LEFT_FIRST (1 << 0) /* eval left argument first */
  10378. #define DUK_COMPARE_FLAG_NEGATE (1 << 1) /* negate result */
  10379. /* conversions, coercions, comparison, etc */
  10380. DUK_INTERNAL_DECL duk_bool_t duk_js_toboolean(duk_tval *tv);
  10381. DUK_INTERNAL_DECL duk_double_t duk_js_tonumber(duk_hthread *thr, duk_tval *tv);
  10382. DUK_INTERNAL_DECL duk_double_t duk_js_tointeger_number(duk_double_t x);
  10383. DUK_INTERNAL_DECL duk_double_t duk_js_tointeger(duk_hthread *thr, duk_tval *tv);
  10384. DUK_INTERNAL_DECL duk_uint32_t duk_js_touint32(duk_hthread *thr, duk_tval *tv);
  10385. DUK_INTERNAL_DECL duk_int32_t duk_js_toint32(duk_hthread *thr, duk_tval *tv);
  10386. DUK_INTERNAL_DECL duk_uint16_t duk_js_touint16(duk_hthread *thr, duk_tval *tv);
  10387. DUK_INTERNAL_DECL duk_small_int_t duk_js_to_arrayindex_raw_string(const duk_uint8_t *str, duk_uint32_t blen, duk_uarridx_t *out_idx);
  10388. DUK_INTERNAL_DECL duk_uarridx_t duk_js_to_arrayindex_string_helper(duk_hstring *h);
  10389. DUK_INTERNAL_DECL duk_bool_t duk_js_equals_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_int_t flags);
  10390. 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);
  10391. DUK_INTERNAL_DECL duk_small_int_t duk_js_string_compare(duk_hstring *h1, duk_hstring *h2);
  10392. #if 0 /* unused */
  10393. DUK_INTERNAL_DECL duk_small_int_t duk_js_buffer_compare(duk_heap *heap, duk_hbuffer *h1, duk_hbuffer *h2);
  10394. #endif
  10395. DUK_INTERNAL_DECL duk_bool_t duk_js_compare_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_int_t flags);
  10396. DUK_INTERNAL_DECL duk_bool_t duk_js_instanceof(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y);
  10397. DUK_INTERNAL_DECL duk_bool_t duk_js_in(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y);
  10398. DUK_INTERNAL_DECL duk_hstring *duk_js_typeof(duk_hthread *thr, duk_tval *tv_x);
  10399. #define duk_js_equals(thr,tv_x,tv_y) \
  10400. duk_js_equals_helper((thr), (tv_x), (tv_y), 0)
  10401. #define duk_js_strict_equals(tv_x,tv_y) \
  10402. duk_js_equals_helper(NULL, (tv_x), (tv_y), DUK_EQUALS_FLAG_STRICT)
  10403. #define duk_js_samevalue(tv_x,tv_y) \
  10404. duk_js_equals_helper(NULL, (tv_x), (tv_y), DUK_EQUALS_FLAG_SAMEVALUE)
  10405. /* E5 Sections 11.8.1, 11.8.5; x < y */
  10406. #define duk_js_lessthan(thr,tv_x,tv_y) \
  10407. duk_js_compare_helper((thr), (tv_x), (tv_Y), DUK_COMPARE_FLAG_EVAL_LEFT_FIRST)
  10408. /* E5 Sections 11.8.2, 11.8.5; x > y --> y < x */
  10409. #define duk_js_greaterthan(thr,tv_x,tv_y) \
  10410. duk_js_compare_helper((thr), (tv_y), (tv_x), 0)
  10411. /* E5 Sections 11.8.3, 11.8.5; x <= y --> not (x > y) --> not (y < x) */
  10412. #define duk_js_lessthanorequal(thr,tv_x,tv_y) \
  10413. duk_js_compare_helper((thr), (tv_y), (tv_x), DUK_COMPARE_FLAG_NEGATE)
  10414. /* E5 Sections 11.8.4, 11.8.5; x >= y --> not (x < y) */
  10415. #define duk_js_greaterthanorequal(thr,tv_x,tv_y) \
  10416. duk_js_compare_helper((thr), (tv_x), (tv_y), DUK_COMPARE_FLAG_EVAL_LEFT_FIRST | DUK_COMPARE_FLAG_NEGATE)
  10417. /* identifiers and environment handling */
  10418. #if 0 /*unused*/
  10419. DUK_INTERNAL duk_bool_t duk_js_hasvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name);
  10420. #endif
  10421. DUK_INTERNAL_DECL duk_bool_t duk_js_getvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name, duk_bool_t throw_flag);
  10422. DUK_INTERNAL_DECL duk_bool_t duk_js_getvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name, duk_bool_t throw_flag);
  10423. DUK_INTERNAL_DECL void duk_js_putvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name, duk_tval *val, duk_bool_t strict);
  10424. DUK_INTERNAL_DECL void duk_js_putvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name, duk_tval *val, duk_bool_t strict);
  10425. #if 0 /*unused*/
  10426. DUK_INTERNAL_DECL duk_bool_t duk_js_delvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name);
  10427. #endif
  10428. DUK_INTERNAL_DECL duk_bool_t duk_js_delvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name);
  10429. DUK_INTERNAL_DECL duk_bool_t duk_js_declvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name, duk_tval *val, duk_small_int_t prop_flags, duk_bool_t is_func_decl);
  10430. DUK_INTERNAL_DECL void duk_js_init_activation_environment_records_delayed(duk_hthread *thr, duk_activation *act);
  10431. DUK_INTERNAL_DECL void duk_js_close_environment_record(duk_hthread *thr, duk_hobject *env, duk_hobject *func, duk_size_t regbase);
  10432. DUK_INTERNAL_DECL duk_hobject *duk_create_activation_environment_record(duk_hthread *thr, duk_hobject *func, duk_size_t idx_bottom);
  10433. DUK_INTERNAL_DECL
  10434. void duk_js_push_closure(duk_hthread *thr,
  10435. duk_hcompiledfunction *fun_temp,
  10436. duk_hobject *outer_var_env,
  10437. duk_hobject *outer_lex_env);
  10438. /* call handling */
  10439. DUK_INTERNAL_DECL duk_int_t duk_handle_call(duk_hthread *thr, duk_idx_t num_stack_args, duk_small_uint_t call_flags);
  10440. DUK_INTERNAL_DECL duk_int_t duk_handle_safe_call(duk_hthread *thr, duk_safe_call_function func, duk_idx_t num_stack_args, duk_idx_t num_stack_res);
  10441. DUK_INTERNAL_DECL duk_bool_t duk_handle_ecma_call_setup(duk_hthread *thr, duk_idx_t num_stack_args, duk_small_uint_t call_flags);
  10442. /* bytecode execution */
  10443. DUK_INTERNAL_DECL void duk_js_execute_bytecode(duk_hthread *exec_thr);
  10444. #endif /* DUK_JS_H_INCLUDED */
  10445. #line 1 "duk_numconv.h"
  10446. #ifndef DUK_NUMCONV_H_INCLUDED
  10447. #define DUK_NUMCONV_H_INCLUDED
  10448. /*
  10449. * Number-to-string conversion. The semantics of these is very tightly
  10450. * bound with the Ecmascript semantics required for call sites.
  10451. */
  10452. /* Output a specified number of digits instead of using the shortest
  10453. * form. Used for toPrecision() and toFixed().
  10454. */
  10455. #define DUK_N2S_FLAG_FIXED_FORMAT (1 << 0)
  10456. /* Force exponential format. Used for toExponential(). */
  10457. #define DUK_N2S_FLAG_FORCE_EXP (1 << 1)
  10458. /* If number would need zero padding (for whole number part), use
  10459. * exponential format instead. E.g. if input number is 12300, 3
  10460. * digits are generated ("123"), output "1.23e+4" instead of "12300".
  10461. * Used for toPrecision().
  10462. */
  10463. #define DUK_N2S_FLAG_NO_ZERO_PAD (1 << 2)
  10464. /* Digit count indicates number of fractions (i.e. an absolute
  10465. * digit index instead of a relative one). Used together with
  10466. * DUK_N2S_FLAG_FIXED_FORMAT for toFixed().
  10467. */
  10468. #define DUK_N2S_FLAG_FRACTION_DIGITS (1 << 3)
  10469. /*
  10470. * String-to-number conversion
  10471. */
  10472. /* Maximum exponent value when parsing numbers. This is not strictly
  10473. * compliant as there should be no upper limit, but as we parse the
  10474. * exponent without a bigint, impose some limit.
  10475. */
  10476. #define DUK_S2N_MAX_EXPONENT 1000000000
  10477. /* Trim white space (= allow leading and trailing whitespace) */
  10478. #define DUK_S2N_FLAG_TRIM_WHITE (1 << 0)
  10479. /* Allow exponent */
  10480. #define DUK_S2N_FLAG_ALLOW_EXP (1 << 1)
  10481. /* Allow trailing garbage (e.g. treat "123foo" as "123) */
  10482. #define DUK_S2N_FLAG_ALLOW_GARBAGE (1 << 2)
  10483. /* Allow leading plus sign */
  10484. #define DUK_S2N_FLAG_ALLOW_PLUS (1 << 3)
  10485. /* Allow leading minus sign */
  10486. #define DUK_S2N_FLAG_ALLOW_MINUS (1 << 4)
  10487. /* Allow 'Infinity' */
  10488. #define DUK_S2N_FLAG_ALLOW_INF (1 << 5)
  10489. /* Allow fraction part */
  10490. #define DUK_S2N_FLAG_ALLOW_FRAC (1 << 6)
  10491. /* Allow naked fraction (e.g. ".123") */
  10492. #define DUK_S2N_FLAG_ALLOW_NAKED_FRAC (1 << 7)
  10493. /* Allow empty fraction (e.g. "123.") */
  10494. #define DUK_S2N_FLAG_ALLOW_EMPTY_FRAC (1 << 8)
  10495. /* Allow empty string to be interpreted as 0 */
  10496. #define DUK_S2N_FLAG_ALLOW_EMPTY_AS_ZERO (1 << 9)
  10497. /* Allow leading zeroes (e.g. "0123" -> "123") */
  10498. #define DUK_S2N_FLAG_ALLOW_LEADING_ZERO (1 << 10)
  10499. /* Allow automatic detection of hex base ("0x" or "0X" prefix),
  10500. * overrides radix argument and forces integer mode.
  10501. */
  10502. #define DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT (1 << 11)
  10503. /* Allow automatic detection of octal base, overrides radix
  10504. * argument and forces integer mode.
  10505. */
  10506. #define DUK_S2N_FLAG_ALLOW_AUTO_OCT_INT (1 << 12)
  10507. /*
  10508. * Prototypes
  10509. */
  10510. DUK_INTERNAL_DECL void duk_numconv_stringify(duk_context *ctx, duk_small_int_t radix, duk_small_int_t digits, duk_small_uint_t flags);
  10511. DUK_INTERNAL_DECL void duk_numconv_parse(duk_context *ctx, duk_small_int_t radix, duk_small_uint_t flags);
  10512. #endif /* DUK_NUMCONV_H_INCLUDED */
  10513. #line 1 "duk_bi_protos.h"
  10514. /*
  10515. * Prototypes for all built-in functions.
  10516. */
  10517. #ifndef DUK_BUILTIN_PROTOS_H_INCLUDED
  10518. #define DUK_BUILTIN_PROTOS_H_INCLUDED
  10519. /* Buffer size needed for duk_bi_date_format_timeval().
  10520. * Accurate value is 32 + 1 for NUL termination:
  10521. * >>> len('+123456-01-23T12:34:56.123+12:34')
  10522. * 32
  10523. * Include additional space to be safe.
  10524. */
  10525. #define DUK_BI_DATE_ISO8601_BUFSIZE 48
  10526. /* Maximum length of CommonJS module identifier to resolve. Length includes
  10527. * both current module ID, requested (possibly relative) module ID, and a
  10528. * slash in between.
  10529. */
  10530. #define DUK_BI_COMMONJS_MODULE_ID_LIMIT 256
  10531. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_constructor(duk_context *ctx);
  10532. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_constructor_is_array(duk_context *ctx);
  10533. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_to_string(duk_context *ctx);
  10534. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_concat(duk_context *ctx);
  10535. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_join_shared(duk_context *ctx);
  10536. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_pop(duk_context *ctx);
  10537. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_push(duk_context *ctx);
  10538. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_reverse(duk_context *ctx);
  10539. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_shift(duk_context *ctx);
  10540. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_slice(duk_context *ctx);
  10541. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_sort(duk_context *ctx);
  10542. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_splice(duk_context *ctx);
  10543. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_unshift(duk_context *ctx);
  10544. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_indexof_shared(duk_context *ctx);
  10545. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_iter_shared(duk_context *ctx);
  10546. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_reduce_shared(duk_context *ctx);
  10547. DUK_INTERNAL_DECL duk_ret_t duk_bi_boolean_constructor(duk_context *ctx);
  10548. DUK_INTERNAL_DECL duk_ret_t duk_bi_boolean_prototype_tostring_shared(duk_context *ctx);
  10549. /* XXX: naming is inconsistent with other builtins, "prototype" not used as
  10550. * part of function name.
  10551. */
  10552. DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_constructor(duk_context *ctx);
  10553. DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_prototype_tostring_shared(duk_context *ctx);
  10554. DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_readfield(duk_context *ctx);
  10555. DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_writefield(duk_context *ctx);
  10556. DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_compare_shared(duk_context *ctx);
  10557. DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_slice_shared(duk_context *ctx);
  10558. DUK_INTERNAL_DECL duk_ret_t duk_bi_arraybuffer_constructor(duk_context *ctx);
  10559. DUK_INTERNAL_DECL duk_ret_t duk_bi_arraybuffer_isview(duk_context *ctx);
  10560. DUK_INTERNAL_DECL duk_ret_t duk_bi_dataview_constructor(duk_context *ctx);
  10561. DUK_INTERNAL_DECL duk_ret_t duk_bi_typedarray_constructor(duk_context *ctx);
  10562. DUK_INTERNAL_DECL duk_ret_t duk_bi_typedarray_set(duk_context *ctx);
  10563. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_constructor(duk_context *ctx);
  10564. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_concat(duk_context *ctx);
  10565. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_is_encoding(duk_context *ctx);
  10566. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_is_buffer(duk_context *ctx);
  10567. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_byte_length(duk_context *ctx);
  10568. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_context *ctx);
  10569. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_tojson(duk_context *ctx);
  10570. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_fill(duk_context *ctx);
  10571. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_copy(duk_context *ctx);
  10572. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_write(duk_context *ctx);
  10573. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor(duk_context *ctx);
  10574. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor_parse(duk_context *ctx);
  10575. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor_utc(duk_context *ctx);
  10576. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor_now(duk_context *ctx);
  10577. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_tostring_shared(duk_context *ctx);
  10578. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_value_of(duk_context *ctx);
  10579. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_to_json(duk_context *ctx);
  10580. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_get_shared(duk_context *ctx);
  10581. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_get_timezone_offset(duk_context *ctx);
  10582. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_set_shared(duk_context *ctx);
  10583. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_set_time(duk_context *ctx);
  10584. /* Helpers exposed for internal use */
  10585. 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);
  10586. DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_timeval_from_dparts(duk_double_t *dparts, duk_small_uint_t flags);
  10587. DUK_INTERNAL_DECL void duk_bi_date_format_timeval(duk_double_t timeval, duk_uint8_t *out_buf);
  10588. DUK_INTERNAL_DECL duk_bool_t duk_bi_date_is_leap_year(duk_int_t year);
  10589. DUK_INTERNAL_DECL duk_bool_t duk_bi_date_timeval_in_valid_range(duk_double_t x);
  10590. DUK_INTERNAL_DECL duk_bool_t duk_bi_date_year_in_valid_range(duk_double_t year);
  10591. DUK_INTERNAL_DECL duk_bool_t duk_bi_date_timeval_in_leeway_range(duk_double_t x);
  10592. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_info(duk_context *ctx);
  10593. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_act(duk_context *ctx);
  10594. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_gc(duk_context *ctx);
  10595. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_fin(duk_context *ctx);
  10596. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_enc(duk_context *ctx);
  10597. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_dec(duk_context *ctx);
  10598. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_compact(duk_context *ctx);
  10599. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_constructor_shared(duk_context *ctx);
  10600. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_to_string(duk_context *ctx);
  10601. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_stack_getter(duk_context *ctx);
  10602. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_filename_getter(duk_context *ctx);
  10603. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_linenumber_getter(duk_context *ctx);
  10604. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_nop_setter(duk_context *ctx);
  10605. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_constructor(duk_context *ctx);
  10606. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype(duk_context *ctx);
  10607. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_to_string(duk_context *ctx);
  10608. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_apply(duk_context *ctx);
  10609. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_call(duk_context *ctx);
  10610. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_bind(duk_context *ctx);
  10611. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_eval(duk_context *ctx);
  10612. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_parse_int(duk_context *ctx);
  10613. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_parse_float(duk_context *ctx);
  10614. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_is_nan(duk_context *ctx);
  10615. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_is_finite(duk_context *ctx);
  10616. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_decode_uri(duk_context *ctx);
  10617. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_decode_uri_component(duk_context *ctx);
  10618. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_encode_uri(duk_context *ctx);
  10619. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_encode_uri_component(duk_context *ctx);
  10620. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_escape(duk_context *ctx);
  10621. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_unescape(duk_context *ctx);
  10622. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_print_helper(duk_context *ctx);
  10623. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_require(duk_context *ctx);
  10624. DUK_INTERNAL_DECL
  10625. void duk_bi_json_parse_helper(duk_context *ctx,
  10626. duk_idx_t idx_value,
  10627. duk_idx_t idx_reviver,
  10628. duk_small_uint_t flags);
  10629. DUK_INTERNAL_DECL
  10630. void duk_bi_json_stringify_helper(duk_context *ctx,
  10631. duk_idx_t idx_value,
  10632. duk_idx_t idx_replacer,
  10633. duk_idx_t idx_space,
  10634. duk_small_uint_t flags);
  10635. DUK_INTERNAL_DECL duk_ret_t duk_bi_json_object_parse(duk_context *ctx);
  10636. DUK_INTERNAL_DECL duk_ret_t duk_bi_json_object_stringify(duk_context *ctx);
  10637. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_onearg_shared(duk_context *ctx);
  10638. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_twoarg_shared(duk_context *ctx);
  10639. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_max(duk_context *ctx);
  10640. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_min(duk_context *ctx);
  10641. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_random(duk_context *ctx);
  10642. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_constructor(duk_context *ctx);
  10643. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_string(duk_context *ctx);
  10644. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_locale_string(duk_context *ctx);
  10645. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_value_of(duk_context *ctx);
  10646. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_fixed(duk_context *ctx);
  10647. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_exponential(duk_context *ctx);
  10648. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_precision(duk_context *ctx);
  10649. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_getprototype_shared(duk_context *ctx);
  10650. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_setprototype_shared(duk_context *ctx);
  10651. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor(duk_context *ctx);
  10652. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_get_own_property_descriptor(duk_context *ctx);
  10653. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_create(duk_context *ctx);
  10654. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_define_property(duk_context *ctx);
  10655. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_define_properties(duk_context *ctx);
  10656. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_seal_freeze_shared(duk_context *ctx);
  10657. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_prevent_extensions(duk_context *ctx);
  10658. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_is_sealed_frozen_shared(duk_context *ctx);
  10659. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_is_extensible(duk_context *ctx);
  10660. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_keys_shared(duk_context *ctx);
  10661. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_to_string(duk_context *ctx);
  10662. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_to_locale_string(duk_context *ctx);
  10663. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_value_of(duk_context *ctx);
  10664. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_has_own_property(duk_context *ctx);
  10665. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_is_prototype_of(duk_context *ctx);
  10666. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_property_is_enumerable(duk_context *ctx);
  10667. DUK_INTERNAL_DECL duk_ret_t duk_bi_pointer_constructor(duk_context *ctx);
  10668. DUK_INTERNAL_DECL duk_ret_t duk_bi_pointer_prototype_tostring_shared(duk_context *ctx);
  10669. DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_constructor(duk_context *ctx);
  10670. DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_exec(duk_context *ctx);
  10671. DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_test(duk_context *ctx);
  10672. DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_to_string(duk_context *ctx);
  10673. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_constructor(duk_context *ctx);
  10674. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_constructor_from_char_code(duk_context *ctx);
  10675. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_to_string(duk_context *ctx);
  10676. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_char_at(duk_context *ctx);
  10677. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_char_code_at(duk_context *ctx);
  10678. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_concat(duk_context *ctx);
  10679. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_indexof_shared(duk_context *ctx);
  10680. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_locale_compare(duk_context *ctx);
  10681. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_match(duk_context *ctx);
  10682. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_replace(duk_context *ctx);
  10683. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_search(duk_context *ctx);
  10684. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_slice(duk_context *ctx);
  10685. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_split(duk_context *ctx);
  10686. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_substring(duk_context *ctx);
  10687. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_caseconv_shared(duk_context *ctx);
  10688. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_trim(duk_context *ctx);
  10689. /* Note: present even if DUK_USE_SECTION_B undefined given because genbuiltins.py
  10690. * will point to it.
  10691. */
  10692. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_substr(duk_context *ctx);
  10693. DUK_INTERNAL_DECL duk_ret_t duk_bi_proxy_constructor(duk_context *ctx);
  10694. #if 0 /* unimplemented now */
  10695. DUK_INTERNAL_DECL duk_ret_t duk_bi_proxy_constructor_revocable(duk_context *ctx);
  10696. #endif
  10697. DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_constructor(duk_context *ctx);
  10698. DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_resume(duk_context *ctx);
  10699. DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_yield(duk_context *ctx);
  10700. DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_current(duk_context *ctx);
  10701. DUK_INTERNAL_DECL duk_ret_t duk_bi_logger_constructor(duk_context *ctx);
  10702. DUK_INTERNAL_DECL duk_ret_t duk_bi_logger_prototype_fmt(duk_context *ctx);
  10703. DUK_INTERNAL_DECL duk_ret_t duk_bi_logger_prototype_raw(duk_context *ctx);
  10704. DUK_INTERNAL_DECL duk_ret_t duk_bi_logger_prototype_log_shared(duk_context *ctx);
  10705. DUK_INTERNAL_DECL duk_ret_t duk_bi_type_error_thrower(duk_context *ctx);
  10706. #endif /* DUK_BUILTIN_PROTOS_H_INCLUDED */
  10707. #line 1 "duk_selftest.h"
  10708. /*
  10709. * Selftest code
  10710. */
  10711. #ifndef DUK_SELFTEST_H_INCLUDED
  10712. #define DUK_SELFTEST_H_INCLUDED
  10713. #if defined(DUK_USE_SELF_TESTS)
  10714. DUK_INTERNAL_DECL void duk_selftest_run_tests(void);
  10715. #endif
  10716. #endif /* DUK_SELFTEST_H_INCLUDED */
  10717. #line 77 "duk_internal.h"
  10718. #endif /* DUK_INTERNAL_H_INCLUDED */
  10719. #line 1 "duk_strings.c"
  10720. /*
  10721. * Shared error message strings
  10722. *
  10723. * To minimize code footprint, try to share error messages inside Duktape
  10724. * code. Modern compilers will do this automatically anyway, this is mostly
  10725. * for older compilers.
  10726. */
  10727. /* include removed: duk_internal.h */
  10728. /* Mostly API and built-in method related */
  10729. DUK_INTERNAL const char *duk_str_internal_error = "internal error";
  10730. DUK_INTERNAL const char *duk_str_invalid_count = "invalid count";
  10731. DUK_INTERNAL const char *duk_str_invalid_call_args = "invalid call args";
  10732. DUK_INTERNAL const char *duk_str_not_constructable = "not constructable";
  10733. DUK_INTERNAL const char *duk_str_not_callable = "not callable";
  10734. DUK_INTERNAL const char *duk_str_not_extensible = "not extensible";
  10735. DUK_INTERNAL const char *duk_str_not_writable = "not writable";
  10736. DUK_INTERNAL const char *duk_str_not_configurable = "not configurable";
  10737. DUK_INTERNAL const char *duk_str_invalid_context = "invalid context";
  10738. DUK_INTERNAL const char *duk_str_invalid_index = "invalid index";
  10739. DUK_INTERNAL const char *duk_str_push_beyond_alloc_stack = "attempt to push beyond currently allocated stack";
  10740. DUK_INTERNAL const char *duk_str_not_undefined = "not undefined";
  10741. DUK_INTERNAL const char *duk_str_not_null = "not null";
  10742. DUK_INTERNAL const char *duk_str_not_boolean = "not boolean";
  10743. DUK_INTERNAL const char *duk_str_not_number = "not number";
  10744. DUK_INTERNAL const char *duk_str_not_string = "not string";
  10745. DUK_INTERNAL const char *duk_str_not_pointer = "not pointer";
  10746. DUK_INTERNAL const char *duk_str_not_buffer = "not buffer";
  10747. DUK_INTERNAL const char *duk_str_unexpected_type = "unexpected type";
  10748. DUK_INTERNAL const char *duk_str_not_thread = "not thread";
  10749. DUK_INTERNAL const char *duk_str_not_compiledfunction = "not compiledfunction";
  10750. DUK_INTERNAL const char *duk_str_not_nativefunction = "not nativefunction";
  10751. DUK_INTERNAL const char *duk_str_not_c_function = "not c function";
  10752. DUK_INTERNAL const char *duk_str_defaultvalue_coerce_failed = "[[DefaultValue]] coerce failed";
  10753. DUK_INTERNAL const char *duk_str_number_outside_range = "number outside range";
  10754. DUK_INTERNAL const char *duk_str_not_object_coercible = "not object coercible";
  10755. DUK_INTERNAL const char *duk_str_string_too_long = "string too long";
  10756. DUK_INTERNAL const char *duk_str_buffer_too_long = "buffer too long";
  10757. DUK_INTERNAL const char *duk_str_sprintf_too_long = "sprintf message too long";
  10758. DUK_INTERNAL const char *duk_str_alloc_failed = "alloc failed";
  10759. DUK_INTERNAL const char *duk_str_pop_too_many = "attempt to pop too many entries";
  10760. DUK_INTERNAL const char *duk_str_wrong_buffer_type = "wrong buffer type";
  10761. DUK_INTERNAL const char *duk_str_failed_to_extend_valstack = "failed to extend valstack";
  10762. DUK_INTERNAL const char *duk_str_encode_failed = "encode failed";
  10763. DUK_INTERNAL const char *duk_str_decode_failed = "decode failed";
  10764. DUK_INTERNAL const char *duk_str_no_sourcecode = "no sourcecode";
  10765. DUK_INTERNAL const char *duk_str_concat_result_too_long = "concat result too long";
  10766. DUK_INTERNAL const char *duk_str_unimplemented = "unimplemented";
  10767. DUK_INTERNAL const char *duk_str_unsupported = "unsupported";
  10768. DUK_INTERNAL const char *duk_str_array_length_over_2g = "array length over 2G";
  10769. /* JSON */
  10770. DUK_INTERNAL const char *duk_str_fmt_ptr = "%p";
  10771. DUK_INTERNAL const char *duk_str_fmt_invalid_json = "invalid json (at offset %ld)";
  10772. DUK_INTERNAL const char *duk_str_jsondec_reclimit = "json decode recursion limit";
  10773. DUK_INTERNAL const char *duk_str_jsonenc_reclimit = "json encode recursion limit";
  10774. DUK_INTERNAL const char *duk_str_cyclic_input = "cyclic input";
  10775. /* Object property access */
  10776. DUK_INTERNAL const char *duk_str_proxy_revoked = "proxy revoked";
  10777. DUK_INTERNAL const char *duk_str_object_resize_failed = "object resize failed";
  10778. DUK_INTERNAL const char *duk_str_invalid_base = "invalid base value";
  10779. DUK_INTERNAL const char *duk_str_strict_caller_read = "attempt to read strict 'caller'";
  10780. DUK_INTERNAL const char *duk_str_proxy_rejected = "proxy rejected";
  10781. DUK_INTERNAL const char *duk_str_invalid_array_length = "invalid array length";
  10782. DUK_INTERNAL const char *duk_str_array_length_write_failed = "array length write failed";
  10783. DUK_INTERNAL const char *duk_str_array_length_not_writable = "array length non-writable";
  10784. DUK_INTERNAL const char *duk_str_setter_undefined = "setter undefined";
  10785. DUK_INTERNAL const char *duk_str_redefine_virt_prop = "attempt to redefine virtual property";
  10786. DUK_INTERNAL const char *duk_str_invalid_descriptor = "invalid descriptor";
  10787. DUK_INTERNAL const char *duk_str_property_is_virtual = "property is virtual";
  10788. /* Compiler */
  10789. DUK_INTERNAL const char *duk_str_parse_error = "parse error";
  10790. DUK_INTERNAL const char *duk_str_duplicate_label = "duplicate label";
  10791. DUK_INTERNAL const char *duk_str_invalid_label = "invalid label";
  10792. DUK_INTERNAL const char *duk_str_invalid_array_literal = "invalid array literal";
  10793. DUK_INTERNAL const char *duk_str_invalid_object_literal = "invalid object literal";
  10794. DUK_INTERNAL const char *duk_str_invalid_var_declaration = "invalid variable declaration";
  10795. DUK_INTERNAL const char *duk_str_cannot_delete_identifier = "cannot delete identifier";
  10796. DUK_INTERNAL const char *duk_str_invalid_expression = "invalid expression";
  10797. DUK_INTERNAL const char *duk_str_invalid_lvalue = "invalid lvalue";
  10798. DUK_INTERNAL const char *duk_str_expected_identifier = "expected identifier";
  10799. DUK_INTERNAL const char *duk_str_empty_expr_not_allowed = "empty expression not allowed";
  10800. DUK_INTERNAL const char *duk_str_invalid_for = "invalid for statement";
  10801. DUK_INTERNAL const char *duk_str_invalid_switch = "invalid switch statement";
  10802. DUK_INTERNAL const char *duk_str_invalid_break_cont_label = "invalid break/continue label";
  10803. DUK_INTERNAL const char *duk_str_invalid_return = "invalid return";
  10804. DUK_INTERNAL const char *duk_str_invalid_try = "invalid try";
  10805. DUK_INTERNAL const char *duk_str_invalid_throw = "invalid throw";
  10806. DUK_INTERNAL const char *duk_str_with_in_strict_mode = "with in strict mode";
  10807. DUK_INTERNAL const char *duk_str_func_stmt_not_allowed = "function statement not allowed";
  10808. DUK_INTERNAL const char *duk_str_unterminated_stmt = "unterminated statement";
  10809. DUK_INTERNAL const char *duk_str_invalid_arg_name = "invalid argument name";
  10810. DUK_INTERNAL const char *duk_str_invalid_func_name = "invalid function name";
  10811. DUK_INTERNAL const char *duk_str_invalid_getset_name = "invalid getter/setter name";
  10812. DUK_INTERNAL const char *duk_str_func_name_required = "function name required";
  10813. /* Executor */
  10814. DUK_INTERNAL const char *duk_str_internal_error_exec_longjmp = "internal error in bytecode executor longjmp handler";
  10815. /* Regexp */
  10816. DUK_INTERNAL const char *duk_str_invalid_quantifier_no_atom = "quantifier without preceding atom";
  10817. DUK_INTERNAL const char *duk_str_invalid_quantifier_values = "quantifier values invalid (qmin > qmax)";
  10818. DUK_INTERNAL const char *duk_str_quantifier_too_many_copies = "quantifier expansion requires too many atom copies";
  10819. DUK_INTERNAL const char *duk_str_unexpected_closing_paren = "unexpected closing parenthesis";
  10820. DUK_INTERNAL const char *duk_str_unexpected_end_of_pattern = "unexpected end of pattern";
  10821. DUK_INTERNAL const char *duk_str_unexpected_regexp_token = "unexpected token in regexp";
  10822. DUK_INTERNAL const char *duk_str_invalid_regexp_flags = "invalid regexp flags";
  10823. DUK_INTERNAL const char *duk_str_invalid_backrefs = "invalid backreference(s)";
  10824. DUK_INTERNAL const char *duk_str_regexp_backtrack_failed = "regexp backtrack failed";
  10825. DUK_INTERNAL const char *duk_str_regexp_advance_failed = "regexp advance failed";
  10826. DUK_INTERNAL const char *duk_str_regexp_internal_error = "regexp internal error";
  10827. /* Limits */
  10828. DUK_INTERNAL const char *duk_str_valstack_limit = "valstack limit";
  10829. DUK_INTERNAL const char *duk_str_callstack_limit = "callstack limit";
  10830. DUK_INTERNAL const char *duk_str_catchstack_limit = "catchstack limit";
  10831. DUK_INTERNAL const char *duk_str_object_property_limit = "object property limit";
  10832. DUK_INTERNAL const char *duk_str_prototype_chain_limit = "prototype chain limit";
  10833. DUK_INTERNAL const char *duk_str_bound_chain_limit = "function call bound chain limit";
  10834. DUK_INTERNAL const char *duk_str_c_callstack_limit = "C call stack depth limit";
  10835. DUK_INTERNAL const char *duk_str_compiler_recursion_limit = "compiler recursion limit";
  10836. DUK_INTERNAL const char *duk_str_bytecode_limit = "bytecode limit";
  10837. DUK_INTERNAL const char *duk_str_reg_limit = "register limit";
  10838. DUK_INTERNAL const char *duk_str_temp_limit = "temp limit";
  10839. DUK_INTERNAL const char *duk_str_const_limit = "const limit";
  10840. DUK_INTERNAL const char *duk_str_func_limit = "function limit";
  10841. DUK_INTERNAL const char *duk_str_regexp_compiler_recursion_limit = "regexp compiler recursion limit";
  10842. DUK_INTERNAL const char *duk_str_regexp_executor_recursion_limit = "regexp executor recursion limit";
  10843. DUK_INTERNAL const char *duk_str_regexp_executor_step_limit = "regexp step limit";
  10844. /* Misc */
  10845. DUK_INTERNAL const char *duk_str_anon = "anon";
  10846. DUK_INTERNAL const char *duk_str_realloc_failed = "realloc failed";
  10847. #line 1 "duk_debug_macros.c"
  10848. /*
  10849. * Debugging macro calls.
  10850. */
  10851. /* include removed: duk_internal.h */
  10852. #ifdef DUK_USE_DEBUG
  10853. /*
  10854. * Debugging enabled
  10855. */
  10856. #include <stdio.h>
  10857. #include <stdlib.h>
  10858. #include <stdarg.h>
  10859. #define DUK__DEBUG_BUFSIZE DUK_USE_DEBUG_BUFSIZE
  10860. DUK_LOCAL char duk__debug_buf[DUK__DEBUG_BUFSIZE];
  10861. DUK_LOCAL const char *duk__get_level_string(duk_small_int_t level) {
  10862. switch ((int) level) {
  10863. case DUK_LEVEL_DEBUG:
  10864. return "D";
  10865. case DUK_LEVEL_DDEBUG:
  10866. return "DD";
  10867. case DUK_LEVEL_DDDEBUG:
  10868. return "DDD";
  10869. }
  10870. return "???";
  10871. }
  10872. #ifdef DUK_USE_DPRINT_COLORS
  10873. /* http://en.wikipedia.org/wiki/ANSI_escape_code */
  10874. #define DUK__TERM_REVERSE "\x1b[7m"
  10875. #define DUK__TERM_BRIGHT "\x1b[1m"
  10876. #define DUK__TERM_RESET "\x1b[0m"
  10877. #define DUK__TERM_BLUE "\x1b[34m"
  10878. #define DUK__TERM_RED "\x1b[31m"
  10879. DUK_LOCAL const char *duk__get_term_1(duk_small_int_t level) {
  10880. DUK_UNREF(level);
  10881. return (const char *) DUK__TERM_RED;
  10882. }
  10883. DUK_LOCAL const char *duk__get_term_2(duk_small_int_t level) {
  10884. switch ((int) level) {
  10885. case DUK_LEVEL_DEBUG:
  10886. return (const char *) (DUK__TERM_RESET DUK__TERM_BRIGHT);
  10887. case DUK_LEVEL_DDEBUG:
  10888. return (const char *) (DUK__TERM_RESET);
  10889. case DUK_LEVEL_DDDEBUG:
  10890. return (const char *) (DUK__TERM_RESET DUK__TERM_BLUE);
  10891. }
  10892. return (const char *) DUK__TERM_RESET;
  10893. }
  10894. DUK_LOCAL const char *duk__get_term_3(duk_small_int_t level) {
  10895. DUK_UNREF(level);
  10896. return (const char *) DUK__TERM_RESET;
  10897. }
  10898. #else
  10899. DUK_LOCAL const char *duk__get_term_1(duk_small_int_t level) {
  10900. DUK_UNREF(level);
  10901. return (const char *) "";
  10902. }
  10903. DUK_LOCAL const char *duk__get_term_2(duk_small_int_t level) {
  10904. DUK_UNREF(level);
  10905. return (const char *) "";
  10906. }
  10907. DUK_LOCAL const char *duk__get_term_3(duk_small_int_t level) {
  10908. DUK_UNREF(level);
  10909. return (const char *) "";
  10910. }
  10911. #endif /* DUK_USE_DPRINT_COLORS */
  10912. #ifdef DUK_USE_VARIADIC_MACROS
  10913. DUK_INTERNAL void duk_debug_log(duk_small_int_t level, const char *file, duk_int_t line, const char *func, const char *fmt, ...) {
  10914. va_list ap;
  10915. va_start(ap, fmt);
  10916. DUK_MEMZERO((void *) duk__debug_buf, (size_t) DUK__DEBUG_BUFSIZE);
  10917. duk_debug_vsnprintf(duk__debug_buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap);
  10918. #ifdef DUK_USE_DPRINT_RDTSC
  10919. DUK_FPRINTF(DUK_STDERR, "%s[%s] <%llu> %s:%ld (%s):%s %s%s\n",
  10920. (const char *) duk__get_term_1(level),
  10921. (const char *) duk__get_level_string(level),
  10922. (unsigned long long) DUK_USE_RDTSC(), /* match the inline asm in duk_features.h */
  10923. (const char *) file,
  10924. (long) line,
  10925. (const char *) func,
  10926. (const char *) duk__get_term_2(level),
  10927. (const char *) duk__debug_buf,
  10928. (const char *) duk__get_term_3(level));
  10929. #else
  10930. DUK_FPRINTF(DUK_STDERR, "%s[%s] %s:%ld (%s):%s %s%s\n",
  10931. (const char *) duk__get_term_1(level),
  10932. (const char *) duk__get_level_string(level),
  10933. (const char *) file,
  10934. (long) line,
  10935. (const char *) func,
  10936. (const char *) duk__get_term_2(level),
  10937. (const char *) duk__debug_buf,
  10938. (const char *) duk__get_term_3(level));
  10939. #endif
  10940. DUK_FFLUSH(DUK_STDERR);
  10941. va_end(ap);
  10942. }
  10943. #else /* DUK_USE_VARIADIC_MACROS */
  10944. DUK_INTERNAL char duk_debug_file_stash[DUK_DEBUG_STASH_SIZE];
  10945. DUK_INTERNAL char duk_debug_line_stash[DUK_DEBUG_STASH_SIZE];
  10946. DUK_INTERNAL char duk_debug_func_stash[DUK_DEBUG_STASH_SIZE];
  10947. DUK_INTERNAL duk_small_int_t duk_debug_level_stash;
  10948. DUK_INTERNAL void duk_debug_log(const char *fmt, ...) {
  10949. va_list ap;
  10950. duk_small_int_t level = duk_debug_level_stash;
  10951. va_start(ap, fmt);
  10952. DUK_MEMZERO((void *) duk__debug_buf, (size_t) DUK__DEBUG_BUFSIZE);
  10953. duk_debug_vsnprintf(duk__debug_buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap);
  10954. #ifdef DUK_USE_DPRINT_RDTSC
  10955. DUK_FPRINTF(DUK_STDERR, "%s[%s] <%llu> %s:%s (%s):%s %s%s\n",
  10956. (const char *) duk__get_term_1(level),
  10957. (const char *) duk__get_level_string(duk_debug_level_stash),
  10958. (unsigned long long) DUK_USE_RDTSC(), /* match duk_features.h */
  10959. (const char *) duk_debug_file_stash,
  10960. (const char *) duk_debug_line_stash,
  10961. (const char *) duk_debug_func_stash,
  10962. (const char *) duk__get_term_2(level),
  10963. (const char *) duk__debug_buf,
  10964. (const char *) duk__get_term_3(level));
  10965. #else
  10966. DUK_FPRINTF(DUK_STDERR, "%s[%s] %s:%s (%s):%s %s%s\n",
  10967. (const char *) duk__get_term_1(level),
  10968. (const char *) duk__get_level_string(duk_debug_level_stash),
  10969. (const char *) duk_debug_file_stash,
  10970. (const char *) duk_debug_line_stash,
  10971. (const char *) duk_debug_func_stash,
  10972. (const char *) duk__get_term_2(level),
  10973. (const char *) duk__debug_buf,
  10974. (const char *) duk__get_term_3(level));
  10975. #endif
  10976. DUK_FFLUSH(DUK_STDERR);
  10977. va_end(ap);
  10978. }
  10979. #endif /* DUK_USE_VARIADIC_MACROS */
  10980. #else /* DUK_USE_DEBUG */
  10981. /*
  10982. * Debugging disabled
  10983. */
  10984. #endif /* DUK_USE_DEBUG */
  10985. #line 1 "duk_builtins.c"
  10986. /*
  10987. * Automatically generated by genbuiltins.py, do not edit!
  10988. */
  10989. /* include removed: duk_internal.h */
  10990. #if defined(DUK_USE_DOUBLE_LE)
  10991. DUK_INTERNAL const duk_uint8_t duk_strings_data[2624] = {
  10992. 55,86,227,24,145,55,102,120,144,3,63,94,228,54,100,137,186,50,11,164,109,
  10993. 77,215,5,61,35,106,206,149,110,4,254,219,237,58,8,196,24,103,74,183,2,127,
  10994. 103,246,93,4,98,12,47,180,67,103,246,127,101,208,70,32,194,186,134,207,236,
  10995. 254,203,160,140,65,133,246,136,108,254,199,237,186,8,196,24,87,80,217,253,
  10996. 143,219,116,17,136,49,30,209,13,159,220,116,75,3,30,65,244,17,136,48,174,
  10997. 209,13,159,220,116,17,136,48,158,161,179,251,142,130,49,6,17,209,130,96,
  10998. 237,80,75,47,160,140,65,142,134,133,41,34,110,134,133,41,34,3,25,110,8,22,
  10999. 158,130,38,163,8,217,200,158,76,156,210,117,128,153,203,210,70,46,137,187,
  11000. 18,27,164,187,201,209,130,100,55,91,70,4,145,63,66,231,44,128,105,187,41,
  11001. 197,13,49,122,8,196,24,71,75,70,138,104,115,77,215,5,36,20,201,214,209,107,
  11002. 79,104,209,144,168,105,6,207,251,209,104,209,125,212,227,66,127,235,191,
  11003. 239,232,180,90,52,95,69,247,83,141,9,255,174,255,191,162,211,80,210,253,23,
  11004. 221,78,52,39,254,183,254,254,139,72,105,126,139,238,167,26,19,255,91,255,
  11005. 127,69,166,129,191,69,247,83,141,9,255,175,255,191,162,213,26,50,23,232,
  11006. 190,234,113,161,63,245,115,119,86,227,118,83,138,26,98,9,110,48,86,22,148,
  11007. 160,152,22,82,70,46,137,44,8,180,163,32,104,98,206,32,17,7,16,88,101,100,
  11008. 206,42,70,36,108,205,18,74,140,33,196,230,60,2,152,146,33,38,230,8,36,79,
  11009. 182,251,65,156,151,24,200,33,145,162,25,80,209,24,67,0,166,68,52,174,61,73,
  11010. 25,33,205,25,27,84,177,195,234,220,1,144,105,99,135,217,16,17,17,208,72,
  11011. 199,179,60,93,100,146,49,232,162,64,76,135,19,152,244,44,136,223,98,67,4,
  11012. 18,33,247,217,158,36,0,209,190,156,13,26,201,21,111,165,67,64,180,100,145,
  11013. 62,250,32,45,100,33,55,214,1,229,223,65,19,72,187,236,206,137,35,125,120,
  11014. 190,201,104,105,15,190,201,212,136,136,125,246,160,137,27,83,239,171,37,
  11015. 200,218,159,125,168,34,192,61,27,233,93,22,1,114,78,250,28,76,130,112,200,
  11016. 93,245,164,188,207,190,204,17,49,38,109,246,160,93,8,119,185,13,153,34,173,
  11017. 246,113,0,136,48,76,10,90,26,78,182,140,9,34,130,161,100,235,64,194,9,226,
  11018. 44,166,1,41,221,153,226,235,118,120,121,58,72,197,209,63,71,69,76,15,34,
  11019. 164,73,244,171,112,39,246,223,104,169,18,125,42,220,9,253,159,217,38,68,
  11020. 159,104,134,207,236,254,201,18,36,250,134,207,236,254,201,50,36,251,68,54,
  11021. 127,99,246,200,145,39,212,54,127,99,246,200,145,39,218,33,179,251,131,200,
  11022. 147,234,27,63,184,81,137,62,149,110,4,254,219,237,20,98,79,165,91,129,63,
  11023. 179,251,36,152,147,237,16,217,253,159,217,32,196,159,80,217,253,159,217,36,
  11024. 196,159,104,134,207,236,126,217,6,36,250,134,207,236,126,217,6,36,251,68,
  11025. 54,127,112,115,18,125,67,103,247,8,149,2,8,196,24,143,131,137,146,90,121,
  11026. 35,162,44,140,35,102,160,226,100,235,138,89,18,102,13,10,82,68,200,151,106,
  11027. 130,88,131,4,192,73,225,228,85,162,137,147,168,108,252,18,42,209,68,201,
  11028. 212,54,126,89,23,104,162,100,245,17,230,207,193,34,237,20,76,158,162,60,
  11029. 217,249,100,109,162,137,147,163,117,2,178,120,36,109,162,137,147,163,117,2,
  11030. 178,121,100,101,162,137,147,165,91,129,63,4,140,180,81,50,116,171,112,39,
  11031. 229,145,150,138,38,78,161,179,251,63,178,240,72,203,69,19,39,80,217,253,
  11032. 159,217,121,100,109,162,137,147,212,71,155,63,179,251,47,4,141,180,81,50,
  11033. 122,136,243,103,246,127,101,229,145,150,138,38,78,161,179,251,31,182,240,
  11034. 72,203,69,19,39,80,217,253,143,219,121,100,109,162,137,147,212,71,155,63,
  11035. 177,251,111,4,141,180,81,50,122,136,243,103,246,63,109,229,145,54,138,38,
  11036. 78,161,179,251,133,90,40,153,61,68,121,179,251,132,196,128,31,80,217,248,
  11037. 36,76,72,1,245,13,159,150,69,68,128,31,168,143,54,126,9,21,18,0,126,162,60,
  11038. 217,249,100,100,72,1,244,110,160,86,79,4,140,137,0,62,141,212,10,201,229,
  11039. 145,113,32,7,210,173,192,159,130,69,196,128,31,74,183,2,126,89,23,18,0,125,
  11040. 67,103,246,127,101,224,145,113,32,7,212,54,127,103,246,94,89,25,18,0,126,
  11041. 162,60,217,253,159,217,120,36,100,72,1,250,136,243,103,246,127,101,229,145,
  11042. 113,32,7,212,54,127,99,246,222,9,23,18,0,125,67,103,246,63,109,229,145,145,
  11043. 32,7,234,35,205,159,216,253,183,130,70,68,128,31,168,143,54,127,99,246,222,
  11044. 89,17,18,0,125,67,103,247,9,137,0,63,81,30,108,254,224,130,115,240,98,66,
  11045. 128,92,136,84,45,101,180,81,50,28,78,99,193,18,40,56,153,58,178,52,211,58,
  11046. 17,46,134,133,41,34,164,75,164,104,156,52,52,199,37,222,232,206,66,64,207,
  11047. 18,66,136,137,19,173,62,46,155,181,167,72,147,235,226,233,186,120,121,58,
  11048. 226,157,214,111,84,76,73,36,109,24,72,130,100,112,200,178,76,157,124,92,
  11049. 242,70,120,25,193,34,245,241,117,240,97,1,107,33,25,212,54,160,90,7,244,29,
  11050. 24,38,66,254,223,215,125,119,215,126,232,190,43,226,67,244,1,250,193,125,
  11051. 111,216,11,234,254,192,63,96,159,173,234,26,84,53,19,194,126,175,168,105,
  11052. 80,212,79,8,234,26,84,53,19,193,156,20,144,83,52,167,20,52,198,109,24,18,
  11053. 68,225,115,150,64,53,52,104,200,84,52,131,76,167,20,52,200,46,7,48,52,146,
  11054. 132,102,57,33,165,139,168,209,154,32,104,220,193,189,214,27,16,209,176,23,
  11055. 26,220,98,149,110,116,70,75,188,98,116,136,34,33,101,4,192,223,178,32,38,6,
  11056. 144,18,67,72,1,58,67,0,100,95,74,17,159,217,31,210,132,103,246,58,251,33,
  11057. 121,232,55,150,227,125,143,216,16,190,91,141,246,68,31,150,223,178,39,150,
  11058. 223,177,251,0,244,135,97,37,32,24,132,104,24,66,161,175,164,202,134,140,
  11059. 151,39,212,125,255,221,125,74,86,9,79,168,104,201,116,178,139,154,22,134,
  11060. 145,72,51,93,18,116,64,145,13,39,82,34,33,38,73,76,132,185,4,185,187,198,
  11061. 100,229,233,197,13,49,228,73,247,4,4,78,98,79,184,32,34,105,187,201,147,
  11062. 154,185,187,200,147,165,233,197,13,50,230,239,82,98,151,167,20,52,206,145,
  11063. 39,234,76,69,245,22,190,224,128,138,228,73,244,180,90,251,130,2,43,145,39,
  11064. 234,76,76,243,155,51,162,68,159,88,230,204,234,145,39,234,76,67,240,38,67,
  11065. 200,147,232,193,50,46,68,159,169,49,31,206,164,100,137,18,125,59,169,25,54,
  11066. 68,159,169,49,51,200,109,38,73,42,68,159,88,134,210,100,147,100,73,250,147,
  11067. 20,188,65,57,163,146,164,73,246,68,19,154,57,74,68,159,169,49,51,200,90,
  11068. 209,34,9,205,28,159,34,79,172,66,214,137,16,78,104,228,121,18,125,154,24,
  11069. 72,152,147,236,208,194,101,205,39,92,82,200,147,145,137,63,82,98,103,144,
  11070. 181,162,68,19,154,57,60,196,159,88,133,173,18,32,156,209,201,166,36,253,73,
  11071. 138,94,32,156,209,201,70,36,251,34,9,205,28,154,98,79,212,152,153,228,54,
  11072. 147,36,148,98,79,172,67,105,50,73,102,36,253,73,136,254,117,35,36,24,147,
  11073. 233,221,72,201,38,36,253,73,136,126,6,12,98,79,163,6,20,98,79,212,152,135,
  11074. 224,76,135,49,39,209,130,100,89,137,63,82,98,103,156,217,157,6,36,250,199,
  11075. 54,103,113,137,63,82,98,47,168,181,247,4,4,86,98,79,165,162,215,220,16,17,
  11076. 57,137,62,205,12,36,166,238,173,194,2,201,217,161,132,236,167,20,52,210,
  11077. 155,186,183,8,11,39,70,9,147,178,156,80,211,50,110,236,208,194,118,83,138,
  11078. 26,102,77,221,24,38,78,202,113,67,76,54,186,195,245,38,34,188,17,145,23,55,
  11079. 117,241,32,145,36,57,173,155,186,75,189,205,35,102,128,44,243,119,74,139,
  11080. 144,113,243,221,36,77,21,38,144,210,161,168,158,35,230,144,192,154,42,77,
  11081. 33,165,67,81,60,15,173,7,90,159,49,13,213,64,186,17,62,96,47,170,129,116,
  11082. 33,165,64,202,113,36,226,134,70,110,234,220,32,44,157,163,222,72,244,64,
  11083. 145,23,55,118,143,121,35,209,2,68,140,221,213,184,64,89,58,183,88,145,232,
  11084. 129,34,46,110,234,221,98,71,162,4,136,153,80,50,156,80,211,22,79,90,38,105,
  11085. 16,17,17,207,18,61,96,17,10,192,76,71,106,220,32,44,157,19,152,240,68,138,
  11086. 17,193,30,137,195,39,65,51,8,224,143,65,54,22,46,103,68,112,71,162,112,200,
  11087. 184,144,116,17,59,20,24,243,52,72,58,8,134,42,23,50,68,108,3,206,87,71,164,
  11088. 0,142,73,57,132,41,42,72,225,107,4,167,212,52,100,191,92,83,161,163,37,250,
  11089. 226,158,141,145,208,89,154,79,90,4,66,73,209,153,100,180,8,133,145,208,89,
  11090. 158,36,169,35,34,17,244,145,198,247,60,137,114,26,97,57,162,4,206,137,116,
  11091. 17,136,48,144,68,212,97,27,57,24,64,90,201,18,5,13,25,4,5,172,160,123,215,
  11092. 138,62,46,121,35,60,117,18,233,27,70,18,32,10,200,212,75,175,139,166,233,
  11093. 225,228,235,138,227,130,93,117,155,215,197,207,36,103,131,212,11,161,58,
  11094. 226,186,110,234,220,32,44,157,148,226,134,153,19,119,101,56,161,166,88,156,
  11095. 217,78,52,20,221,17,200,147,25,137,53,17,180,97,34,0,172,140,19,154,84,26,
  11096. 145,0,86,68,90,40,152,2,178,22,160,93,8,69,19,18,98,37,210,94,100,108,144,
  11097. 21,145,8,151,75,23,100,141,66,37,217,16,11,32,226,248,146,164,108,250,75,
  11098. 204,141,146,28,217,24,177,33,50,66,72,128,92,6,66,161,164,235,226,231,146,
  11099. 51,65,36,225,144,168,105,58,248,185,228,140,240,97,68,128,153,38,98,79,174,
  11100. 179,122,248,185,228,140,241,214,129,132,150,12,73,245,214,111,95,23,60,145,
  11101. 158,58,50,72,81,67,230,232,184,196,159,95,23,77,211,195,201,215,21,47,139,
  11102. 166,233,225,228,50,200,211,76,229,2,201,25,149,241,67,102,138,52,146,16,30,
  11103. 67,18,66,3,201,34,52,78,25,61,72,160,94,115,30,230,145,179,73,26,39,12,158,
  11104. 164,81,33,144,78,25,61,72,160,94,115,30,230,145,179,72,200,39,12,158,164,
  11105. 80,132,75,165,67,81,50,21,18,235,65,214,169,224,140,137,210,173,192,154,30,
  11106. 8,200,157,67,102,66,84,11,71,169,20,19,209,139,162,158,207,15,39,73,24,186,
  11107. 43,236,176,217,130,253,36,98,232,187,177,33,73,18,52,68,233,35,23,69,93,
  11108. 136,26,98,116,145,139,162,158,146,160,95,73,24,186,37,12,72,5,16,64,145,10,
  11109. 32,76,71,64,156,217,161,180,34,6,64,208,198,36,78,50,20,20,92,204,50,44,
  11110. 147,32,134,226,17,114,33,202,134,129,107,192,202,232,160,180,104,166,135,
  11111. 52,72,40,144,213,33,178,152,26,34,56,163,105,44,104,146,116,139,77,43,34,
  11112. 98,57,38,116,72,179,60,93,97,206,56,52,240,242,56,163,168,34,81,57,178,153,
  11113. 42,228,12,182,58,22,66,89,19,57,68,176,74,68,35,104,195,18,239,116,102,114,
  11114. 94,100,104,228,100,49,238,140,203,42,60,145,35,104,181,146,113,161,10,80,
  11115. 46,68,82,24,245,145,132,108,228,148,54,100,137,64,34,13,100,153,222,1,40,6,
  11116. 33,223,20,84,19,34,95,23,76,130,153,6,103,208,43,64,141,41,130,104,17,112,
  11117. 130,44,96,
  11118. };
  11119. /* to convert a heap stridx to a token number, subtract
  11120. * DUK_STRIDX_START_RESERVED and add DUK_TOK_START_RESERVED.
  11121. */
  11122. /* native functions: 147 */
  11123. DUK_INTERNAL const duk_c_function duk_bi_native_functions[147] = {
  11124. duk_bi_array_constructor,
  11125. duk_bi_array_constructor_is_array,
  11126. duk_bi_array_prototype_concat,
  11127. duk_bi_array_prototype_indexof_shared,
  11128. duk_bi_array_prototype_iter_shared,
  11129. duk_bi_array_prototype_join_shared,
  11130. duk_bi_array_prototype_pop,
  11131. duk_bi_array_prototype_push,
  11132. duk_bi_array_prototype_reduce_shared,
  11133. duk_bi_array_prototype_reverse,
  11134. duk_bi_array_prototype_shift,
  11135. duk_bi_array_prototype_slice,
  11136. duk_bi_array_prototype_sort,
  11137. duk_bi_array_prototype_splice,
  11138. duk_bi_array_prototype_to_string,
  11139. duk_bi_array_prototype_unshift,
  11140. duk_bi_arraybuffer_constructor,
  11141. duk_bi_arraybuffer_isview,
  11142. duk_bi_boolean_constructor,
  11143. duk_bi_boolean_prototype_tostring_shared,
  11144. duk_bi_buffer_compare_shared,
  11145. duk_bi_buffer_constructor,
  11146. duk_bi_buffer_prototype_tostring_shared,
  11147. duk_bi_buffer_readfield,
  11148. duk_bi_buffer_slice_shared,
  11149. duk_bi_buffer_writefield,
  11150. duk_bi_dataview_constructor,
  11151. duk_bi_date_constructor,
  11152. duk_bi_date_constructor_now,
  11153. duk_bi_date_constructor_parse,
  11154. duk_bi_date_constructor_utc,
  11155. duk_bi_date_prototype_get_shared,
  11156. duk_bi_date_prototype_get_timezone_offset,
  11157. duk_bi_date_prototype_set_shared,
  11158. duk_bi_date_prototype_set_time,
  11159. duk_bi_date_prototype_to_json,
  11160. duk_bi_date_prototype_tostring_shared,
  11161. duk_bi_date_prototype_value_of,
  11162. duk_bi_duktape_object_act,
  11163. duk_bi_duktape_object_compact,
  11164. duk_bi_duktape_object_dec,
  11165. duk_bi_duktape_object_enc,
  11166. duk_bi_duktape_object_fin,
  11167. duk_bi_duktape_object_gc,
  11168. duk_bi_duktape_object_info,
  11169. duk_bi_error_constructor_shared,
  11170. duk_bi_error_prototype_filename_getter,
  11171. duk_bi_error_prototype_linenumber_getter,
  11172. duk_bi_error_prototype_nop_setter,
  11173. duk_bi_error_prototype_stack_getter,
  11174. duk_bi_error_prototype_to_string,
  11175. duk_bi_function_constructor,
  11176. duk_bi_function_prototype,
  11177. duk_bi_function_prototype_apply,
  11178. duk_bi_function_prototype_bind,
  11179. duk_bi_function_prototype_call,
  11180. duk_bi_function_prototype_to_string,
  11181. duk_bi_global_object_decode_uri,
  11182. duk_bi_global_object_decode_uri_component,
  11183. duk_bi_global_object_encode_uri,
  11184. duk_bi_global_object_encode_uri_component,
  11185. duk_bi_global_object_escape,
  11186. duk_bi_global_object_eval,
  11187. duk_bi_global_object_is_finite,
  11188. duk_bi_global_object_is_nan,
  11189. duk_bi_global_object_parse_float,
  11190. duk_bi_global_object_parse_int,
  11191. duk_bi_global_object_print_helper,
  11192. duk_bi_global_object_require,
  11193. duk_bi_global_object_unescape,
  11194. duk_bi_json_object_parse,
  11195. duk_bi_json_object_stringify,
  11196. duk_bi_logger_constructor,
  11197. duk_bi_logger_prototype_fmt,
  11198. duk_bi_logger_prototype_log_shared,
  11199. duk_bi_logger_prototype_raw,
  11200. duk_bi_math_object_max,
  11201. duk_bi_math_object_min,
  11202. duk_bi_math_object_onearg_shared,
  11203. duk_bi_math_object_random,
  11204. duk_bi_math_object_twoarg_shared,
  11205. duk_bi_nodejs_buffer_byte_length,
  11206. duk_bi_nodejs_buffer_concat,
  11207. duk_bi_nodejs_buffer_constructor,
  11208. duk_bi_nodejs_buffer_copy,
  11209. duk_bi_nodejs_buffer_fill,
  11210. duk_bi_nodejs_buffer_is_buffer,
  11211. duk_bi_nodejs_buffer_is_encoding,
  11212. duk_bi_nodejs_buffer_tojson,
  11213. duk_bi_nodejs_buffer_tostring,
  11214. duk_bi_nodejs_buffer_write,
  11215. duk_bi_number_constructor,
  11216. duk_bi_number_prototype_to_exponential,
  11217. duk_bi_number_prototype_to_fixed,
  11218. duk_bi_number_prototype_to_locale_string,
  11219. duk_bi_number_prototype_to_precision,
  11220. duk_bi_number_prototype_to_string,
  11221. duk_bi_number_prototype_value_of,
  11222. duk_bi_object_constructor,
  11223. duk_bi_object_constructor_create,
  11224. duk_bi_object_constructor_define_properties,
  11225. duk_bi_object_constructor_define_property,
  11226. duk_bi_object_constructor_get_own_property_descriptor,
  11227. duk_bi_object_constructor_is_extensible,
  11228. duk_bi_object_constructor_is_sealed_frozen_shared,
  11229. duk_bi_object_constructor_keys_shared,
  11230. duk_bi_object_constructor_prevent_extensions,
  11231. duk_bi_object_constructor_seal_freeze_shared,
  11232. duk_bi_object_getprototype_shared,
  11233. duk_bi_object_prototype_has_own_property,
  11234. duk_bi_object_prototype_is_prototype_of,
  11235. duk_bi_object_prototype_property_is_enumerable,
  11236. duk_bi_object_prototype_to_locale_string,
  11237. duk_bi_object_prototype_to_string,
  11238. duk_bi_object_prototype_value_of,
  11239. duk_bi_object_setprototype_shared,
  11240. duk_bi_pointer_constructor,
  11241. duk_bi_pointer_prototype_tostring_shared,
  11242. duk_bi_proxy_constructor,
  11243. duk_bi_regexp_constructor,
  11244. duk_bi_regexp_prototype_exec,
  11245. duk_bi_regexp_prototype_test,
  11246. duk_bi_regexp_prototype_to_string,
  11247. duk_bi_string_constructor,
  11248. duk_bi_string_constructor_from_char_code,
  11249. duk_bi_string_prototype_caseconv_shared,
  11250. duk_bi_string_prototype_char_at,
  11251. duk_bi_string_prototype_char_code_at,
  11252. duk_bi_string_prototype_concat,
  11253. duk_bi_string_prototype_indexof_shared,
  11254. duk_bi_string_prototype_locale_compare,
  11255. duk_bi_string_prototype_match,
  11256. duk_bi_string_prototype_replace,
  11257. duk_bi_string_prototype_search,
  11258. duk_bi_string_prototype_slice,
  11259. duk_bi_string_prototype_split,
  11260. duk_bi_string_prototype_substr,
  11261. duk_bi_string_prototype_substring,
  11262. duk_bi_string_prototype_to_string,
  11263. duk_bi_string_prototype_trim,
  11264. duk_bi_thread_constructor,
  11265. duk_bi_thread_current,
  11266. duk_bi_thread_resume,
  11267. duk_bi_thread_yield,
  11268. duk_bi_type_error_thrower,
  11269. duk_bi_typedarray_constructor,
  11270. duk_bi_typedarray_set,
  11271. };
  11272. DUK_INTERNAL const duk_uint8_t duk_builtins_data[1952] = {
  11273. 105,195,75,16,121,40,105,51,14,252,104,52,8,131,72,0,115,225,65,165,236,55,
  11274. 243,6,145,32,210,24,210,182,25,249,35,120,216,99,226,13,78,225,116,177,164,
  11275. 180,44,192,4,202,52,150,220,24,0,169,70,146,219,123,0,23,40,210,91,110,96,
  11276. 3,37,26,75,109,172,0,108,163,73,109,177,128,14,148,105,45,181,176,1,242,
  11277. 144,56,209,32,94,6,167,101,98,80,211,24,1,250,67,72,168,67,232,13,46,128,
  11278. 47,162,52,164,0,62,80,163,72,128,61,40,107,26,7,37,20,53,200,131,88,0,66,
  11279. 134,185,16,98,80,215,34,11,96,0,138,26,228,65,76,0,69,67,92,136,37,128,6,
  11280. 168,107,145,4,48,1,165,13,114,32,118,0,44,161,174,68,12,192,7,148,53,200,
  11281. 129,88,1,26,134,165,48,130,80,31,255,241,69,224,0,0,0,0,0,0,124,63,174,32,
  11282. 0,0,0,0,0,0,120,63,175,98,7,140,16,116,194,7,12,48,108,196,6,140,80,100,
  11283. 198,6,12,112,92,200,5,140,149,192,202,91,204,181,184,204,91,76,213,176,206,
  11284. 90,204,240,84,208,5,13,9,124,210,43,13,24,64,226,131,205,112,56,216,3,77,
  11285. 152,48,218,130,205,184,40,220,130,77,216,32,222,129,205,248,24,224,129,78,
  11286. 25,214,163,226,90,80,145,104,65,37,157,0,150,99,242,89,78,73,100,58,37,140,
  11287. 236,150,35,194,88,79,73,96,69,37,125,12,122,188,134,62,0,2,165,68,39,255,
  11288. 255,193,43,67,0,0,80,127,192,58,182,216,80,0,21,59,154,64,0,107,76,200,172,
  11289. 180,146,176,198,138,187,43,42,204,136,170,181,146,168,214,80,0,26,155,81,
  11290. 42,77,4,168,180,20,0,6,160,206,74,123,73,64,0,127,255,4,10,153,219,28,198,
  11291. 163,184,130,140,224,10,43,144,40,141,164,161,183,18,132,222,64,161,127,128,
  11292. 0,63,225,1,109,74,8,137,71,56,5,4,213,20,3,115,233,249,177,240,80,255,192,
  11293. 6,120,2,64,127,195,0,173,28,56,20,96,80,128,0,206,192,143,167,64,164,156,
  11294. 131,2,112,14,125,55,9,4,216,40,19,80,180,77,3,9,51,13,94,153,7,159,76,64,
  11295. 207,192,0,102,0,103,255,255,242,240,67,73,112,33,168,0,12,180,16,212,0,10,
  11296. 88,8,106,0,7,43,4,53,0,4,149,4,31,128,0,202,66,15,255,255,194,137,254,0,50,
  11297. 135,195,224,127,196,2,87,132,17,82,143,20,10,44,80,36,239,196,147,63,146,
  11298. 119,0,125,49,129,52,152,64,154,128,0,201,96,137,36,131,36,142,17,18,40,82,
  11299. 77,97,145,33,135,68,130,37,17,247,208,71,159,65,29,125,8,0,12,113,244,32,0,
  11300. 49,184,176,70,162,16,20,95,240,0,7,252,80,37,120,193,81,196,194,0,3,69,19,
  11301. 0,81,191,197,140,192,255,255,255,255,255,255,239,127,140,64,1,0,0,0,0,0,0,
  11302. 0,139,192,0,0,0,0,0,0,248,127,138,192,0,0,0,0,0,0,240,127,139,64,0,0,0,0,0,
  11303. 0,240,255,0,31,241,128,149,224,0,0,0,0,0,0,0,0,13,71,96,37,25,120,148,86,
  11304. 16,69,23,73,19,92,36,73,124,129,71,255,0,56,136,233,34,3,223,208,241,192,3,
  11305. 254,56,18,188,128,0,0,0,0,0,15,135,251,104,228,128,135,18,4,0,6,26,72,16,0,
  11306. 42,49,32,64,0,225,132,129,0,4,133,146,4,0,21,210,72,16,0,103,65,32,64,1,
  11307. 220,228,100,162,146,130,20,74,8,72,248,64,2,33,3,225,0,9,131,143,132,0,42,
  11308. 12,62,16,0,184,40,248,64,3,32,131,225,0,13,129,143,132,0,58,4,62,16,0,248,
  11309. 8,248,64,4,32,3,225,0,17,127,143,132,0,73,252,62,16,1,55,232,248,64,5,31,
  11310. 131,225,0,21,125,143,132,0,89,244,62,16,1,119,201,0,31,4,68,123,144,148,0,
  11311. 97,236,66,80,1,151,169,10,248,0,211,208,133,124,0,109,230,66,254,0,56,242,
  11312. 33,127,0,29,120,144,207,128,15,60,8,103,192,7,221,228,37,0,32,119,16,148,0,
  11313. 133,218,66,190,0,68,236,33,95,0,35,117,144,191,128,18,58,136,95,192,9,92,
  11314. 195,225,0,38,114,144,148,0,156,41,31,224,0,15,249,1,138,144,64,192,2,2,225,
  11315. 132,221,9,70,112,70,111,198,111,72,0,0,0,0,0,0,0,0,13,198,240,71,19,201,40,
  11316. 239,64,10,79,248,0,3,254,72,86,209,5,155,36,17,46,185,137,129,109,203,140,
  11317. 11,78,94,96,13,28,200,1,74,255,0,2,127,202,4,218,43,131,100,130,32,5,47,
  11318. 252,0,9,255,44,19,104,173,237,146,8,128,20,207,240,0,39,252,192,77,162,183,
  11319. 54,72,34,0,83,127,192,0,159,243,65,54,138,218,217,32,136,1,78,255,0,2,127,
  11320. 206,4,218,43,99,100,130,32,5,63,252,0,9,255,60,19,104,173,109,146,8,128,15,
  11321. 255,242,27,16,26,85,197,34,194,175,193,80,26,240,5,149,109,110,236,90,192,
  11322. 144,26,208,59,206,126,191,144,139,185,143,218,176,63,160,138,217,81,197,
  11323. 125,207,218,144,3,185,73,133,94,242,246,207,218,112,6,11,81,21,62,200,66,
  11324. 80,26,80,51,78,223,217,167,168,57,143,218,48,51,78,223,217,167,168,61,143,
  11325. 210,104,39,17,158,156,80,0,22,114,113,64,0,153,169,197,0,3,102,40,33,150,
  11326. 156,80,0,70,82,113,64,1,89,41,197,0,6,100,39,20,0,29,142,156,80,0,134,50,
  11327. 98,243,21,53,121,136,160,144,0,22,26,120,24,73,197,0,9,96,167,20,0,41,128,
  11328. 156,80,0,181,250,113,64,3,1,255,254,0,81,20,100,47,145,216,23,255,240,0,11,
  11329. 255,248,0,3,255,252,81,252,0,0,0,0,8,4,252,68,0,129,167,1,26,144,9,165,0,
  11330. 26,177,199,197,132,30,147,16,120,86,65,217,80,240,232,164,120,114,80,60,52,
  11331. 39,32,84,223,192,15,59,30,129,156,115,6,81,160,7,253,40,0,5,81,252,0,1,255,
  11332. 78,0,84,113,96,128,0,209,69,128,21,87,240,0,7,253,72,1,81,221,66,0,3,69,
  11333. 117,0,85,159,192,0,31,245,97,10,100,0,0,0,0,0,0,0,32,10,164,130,97,221,191,
  11334. 113,3,20,146,12,18,200,47,74,30,23,37,15,128,0,143,146,135,192,0,133,169,
  11335. 67,224,0,98,196,161,240,0,65,90,80,248,0,41,63,255,194,109,65,11,137,191,
  11336. 174,45,153,98,242,229,191,147,102,8,190,94,92,183,242,65,167,114,12,188,
  11337. 185,111,228,131,70,29,217,54,105,221,156,0,171,255,128,9,208,68,128,255,
  11338. 174,0,25,168,194,64,0,130,177,254,0,0,255,176,1,3,120,186,64,12,13,194,233,
  11339. 0,32,54,139,164,0,196,216,46,144,2,19,88,186,64,12,141,66,233,0,34,52,139,
  11340. 164,0,140,208,46,144,2,67,56,203,64,12,12,195,45,0,32,50,140,180,0,196,200,
  11341. 50,208,2,19,24,203,64,12,140,67,45,0,34,48,140,180,0,140,192,50,208,2,64,
  11342. 127,255,128,21,38,73,7,1,132,128,0,133,105,252,19,140,0,0,0,0,0,0,15,3,240,
  11343. 25,127,102,0,1,91,127,4,227,0,0,0,0,0,0,3,192,252,6,95,218,128,0,87,31,193,
  11344. 56,192,0,0,0,0,0,0,240,63,1,151,246,224,0,21,215,240,78,48,0,0,0,0,0,0,0,
  11345. 16,0,101,253,200,0,5,121,252,19,140,0,0,0,0,0,0,0,4,0,25,127,118,0,1,95,
  11346. 127,4,227,0,0,0,0,0,0,0,65,0,6,95,222,128,0,88,31,193,56,192,0,0,0,0,0,0,
  11347. 16,64,1,151,247,224,0,22,23,240,78,48,0,0,0,0,0,0,4,16,0,101,254,8,0,5,137,
  11348. 252,19,140,0,0,0,0,0,0,2,4,0,25,127,134,0,1,99,127,0,89,218,146,20,74,228,
  11349. 80,171,17,64,162,132,248,162,64,0,193,255,138,5,137,161,116,38,69,210,0,32,
  11350. 152,23,72,0,10,92,93,32,1,41,97,116,128,8,165,69,210,0,50,148,23,72,0,18,
  11351. 76,93,32,1,73,33,116,128,9,36,69,210,0,52,144,23,72,0,26,60,93,32,1,104,
  11352. 225,116,128,2,35,69,210,0,24,140,23,104,0,42,44,93,160,1,168,161,118,128,
  11353. 10,162,69,218,0,58,136,25,98,28,101,160,2,8,97,150,128,0,161,70,90,0,18,
  11354. 132,25,104,0,138,12,101,160,3,40,33,150,128,1,32,70,90,0,20,128,25,104,0,
  11355. 145,252,101,160,3,71,225,150,128,1,159,70,90,0,22,124,25,104,0,33,236,101,
  11356. 160,1,135,161,152,128,2,158,70,98,0,26,120,25,136,0,169,220,102,32,3,180,
  11357. 117,150,57,214,0,157,85,98,112,80,137,241,66,128,0,166,213,33,53,24,66,121,
  11358. 106,0,
  11359. };
  11360. #ifdef DUK_USE_BUILTIN_INITJS
  11361. DUK_INTERNAL const duk_uint8_t duk_initjs_data[1757] = {
  11362. 47,42,10,32,42,32,32,73,110,105,116,32,99,111,100,101,32,102,111,114,32,
  11363. 108,101,103,97,99,121,32,99,111,109,112,97,116,105,98,105,108,105,116,121,
  11364. 46,10,32,42,10,32,42,32,32,67,111,109,112,97,116,105,98,105,108,105,116,
  11365. 121,32,112,114,111,112,101,114,116,105,101,115,32,47,32,119,114,97,112,112,
  11366. 101,114,32,102,117,110,99,116,105,111,110,115,32,104,101,114,101,32,97,108,
  11367. 108,111,119,32,68,117,107,116,97,112,101,32,116,111,32,114,101,109,97,105,
  11368. 110,10,32,42,32,32,99,111,109,112,97,116,105,98,108,101,32,102,111,114,32,
  11369. 117,115,101,114,32,99,111,100,101,32,119,104,101,110,32,99,111,114,101,32,
  11370. 102,101,97,116,117,114,101,115,32,97,114,101,32,99,104,97,110,103,101,100,
  11371. 44,32,119,105,116,104,111,117,116,32,98,117,114,100,101,110,105,110,103,10,
  11372. 32,42,32,32,116,104,101,32,109,97,105,110,32,67,32,99,111,100,101,32,119,
  11373. 105,116,104,32,99,111,109,112,97,116,105,98,105,108,105,116,121,32,115,116,
  11374. 117,102,102,46,10,32,42,10,32,42,32,32,84,104,105,115,32,102,105,108,101,
  11375. 32,105,115,32,109,105,110,105,102,105,101,100,32,119,105,116,104,32,85,103,
  11376. 108,105,102,121,74,83,32,111,114,32,116,104,101,32,99,108,111,115,117,114,
  11377. 101,32,99,111,109,112,105,108,101,114,46,32,32,66,111,116,104,32,119,105,
  11378. 108,108,10,32,42,32,32,114,101,110,97,109,101,32,118,97,114,105,97,98,108,
  11379. 101,115,44,32,114,101,109,111,118,101,32,99,111,109,109,101,110,116,115,44,
  11380. 32,97,110,100,32,97,114,101,32,99,108,101,118,101,114,32,101,110,111,117,
  11381. 103,104,32,116,111,32,100,114,111,112,32,97,110,121,10,32,42,32,32,34,105,
  11382. 102,32,40,102,97,108,115,101,41,32,123,32,46,46,46,32,125,34,32,98,108,111,
  11383. 99,107,115,32,97,108,116,111,103,101,116,104,101,114,44,32,115,111,32,116,
  11384. 104,97,116,39,115,32,97,110,32,101,102,102,101,99,116,105,118,101,32,119,
  11385. 97,121,32,116,111,10,32,42,32,32,100,105,115,97,98,108,101,32,99,117,114,
  11386. 114,101,110,116,108,121,32,117,110,110,101,101,100,101,100,32,99,111,100,
  11387. 101,46,10,32,42,47,10,10,40,102,117,110,99,116,105,111,110,40,71,44,32,68,
  11388. 41,32,123,10,32,32,32,32,39,117,115,101,32,115,116,114,105,99,116,39,59,10,
  11389. 10,32,32,32,32,102,117,110,99,116,105,111,110,32,100,101,102,40,111,98,106,
  11390. 101,99,116,44,32,110,97,109,101,44,32,118,97,108,117,101,41,32,123,10,32,
  11391. 32,32,32,32,32,32,32,79,98,106,101,99,116,46,100,101,102,105,110,101,80,
  11392. 114,111,112,101,114,116,121,40,111,98,106,101,99,116,44,32,110,97,109,101,
  11393. 44,32,123,10,32,32,32,32,32,32,32,32,32,32,32,32,118,97,108,117,101,58,32,
  11394. 118,97,108,117,101,44,10,32,32,32,32,32,32,32,32,32,32,32,32,119,114,105,
  11395. 116,97,98,108,101,58,32,116,114,117,101,44,10,32,32,32,32,32,32,32,32,32,
  11396. 32,32,32,101,110,117,109,101,114,97,98,108,101,58,32,102,97,108,115,101,44,
  11397. 10,32,32,32,32,32,32,32,32,32,32,32,32,99,111,110,102,105,103,117,114,97,
  11398. 98,108,101,58,32,116,114,117,101,10,32,32,32,32,32,32,32,32,125,41,59,10,
  11399. 32,32,32,32,125,10,10,32,32,32,32,102,117,110,99,116,105,111,110,32,100,
  11400. 101,102,68,40,110,97,109,101,44,32,118,97,108,117,101,41,32,123,10,32,32,
  11401. 32,32,32,32,32,32,100,101,102,40,68,44,32,110,97,109,101,44,32,118,97,108,
  11402. 117,101,41,59,10,32,32,32,32,125,10,10,32,32,32,32,47,47,32,67,111,109,112,
  11403. 97,116,105,98,105,108,105,116,121,32,102,111,114,32,39,99,111,110,115,111,
  11404. 108,101,46,108,111,103,39,46,10,32,32,32,32,105,102,32,40,102,97,108,115,
  11405. 101,41,32,123,10,32,32,32,32,32,32,32,32,99,111,110,115,111,108,101,32,61,
  11406. 32,123,10,32,32,32,32,32,32,32,32,32,32,32,32,108,111,103,58,32,102,117,
  11407. 110,99,116,105,111,110,40,41,32,123,10,32,32,32,32,32,32,32,32,32,32,32,32,
  11408. 32,32,32,32,112,114,105,110,116,40,65,114,114,97,121,46,112,114,111,116,
  11409. 111,116,121,112,101,46,106,111,105,110,46,99,97,108,108,40,97,114,103,117,
  11410. 109,101,110,116,115,44,32,39,32,39,41,41,59,10,32,32,32,32,32,32,32,32,32,
  11411. 32,32,32,125,10,32,32,32,32,32,32,32,32,125,59,10,32,32,32,32,125,10,10,32,
  11412. 32,32,32,47,47,32,68,117,107,116,97,112,101,46,108,105,110,101,40,41,32,
  11413. 119,97,115,32,114,101,109,111,118,101,100,32,105,110,32,68,117,107,116,97,
  11414. 112,101,32,48,46,49,49,46,48,44,32,104,101,114,101,39,115,32,97,110,32,101,
  11415. 120,97,109,112,108,101,10,32,32,32,32,47,47,32,114,101,112,108,97,99,101,
  11416. 109,101,110,116,32,117,115,101,114,32,99,111,100,101,32,99,111,117,108,100,
  11417. 32,117,115,101,46,10,32,32,32,32,105,102,32,40,102,97,108,115,101,41,32,
  11418. 123,10,32,32,32,32,32,32,32,32,100,101,102,40,68,44,32,39,108,105,110,101,
  11419. 39,44,32,102,117,110,99,116,105,111,110,32,40,41,32,123,10,32,32,32,32,32,
  11420. 32,32,32,32,32,32,32,39,117,115,101,32,100,117,107,32,110,111,116,97,105,
  11421. 108,39,59,10,10,32,32,32,32,32,32,32,32,32,32,32,32,47,42,32,84,97,105,108,
  11422. 32,99,97,108,108,115,32,97,114,101,32,112,114,101,118,101,110,116,101,100,
  11423. 32,116,111,32,101,110,115,117,114,101,32,99,97,108,108,105,110,103,32,97,
  11424. 99,116,105,118,97,116,105,111,110,32,101,120,105,115,116,115,46,10,32,32,
  11425. 32,32,32,32,32,32,32,32,32,32,32,42,32,67,97,108,108,32,115,116,97,99,107,
  11426. 32,105,110,100,105,99,101,115,58,32,45,49,32,61,32,68,117,107,116,97,112,
  11427. 101,46,97,99,116,44,32,45,50,32,61,32,103,101,116,67,117,114,114,101,110,
  11428. 116,76,105,110,101,44,32,45,51,32,61,32,99,97,108,108,101,114,10,32,32,32,
  11429. 32,32,32,32,32,32,32,32,32,32,42,47,10,10,32,32,32,32,32,32,32,32,32,32,32,
  11430. 32,114,101,116,117,114,110,32,40,68,117,107,116,97,112,101,46,97,99,116,40,
  11431. 45,51,41,32,124,124,32,123,125,41,46,108,105,110,101,78,117,109,98,101,114,
  11432. 59,10,32,32,32,32,32,32,32,32,125,41,59,10,32,32,32,32,125,10,10,32,32,32,
  11433. 32,47,47,32,76,111,103,103,101,114,32,111,98,106,101,99,116,32,102,111,114,
  11434. 32,67,32,99,111,100,101,32,112,114,111,118,105,100,101,100,32,98,121,32,
  11435. 105,110,105,116,32,99,111,100,101,32,110,111,119,46,10,32,32,32,32,105,102,
  11436. 32,40,116,114,117,101,41,32,123,10,32,32,32,32,32,32,32,32,100,101,102,40,
  11437. 68,46,76,111,103,103,101,114,44,32,39,99,108,111,103,39,44,32,110,101,119,
  11438. 32,68,46,76,111,103,103,101,114,40,39,67,39,41,41,59,10,32,32,32,32,125,10,
  11439. 10,32,32,32,32,47,47,32,84,114,97,99,107,105,110,103,32,116,97,98,108,101,
  11440. 32,102,111,114,32,67,111,109,109,111,110,74,83,32,109,111,100,117,108,101,
  11441. 32,108,111,97,100,105,110,103,46,10,32,32,32,32,105,102,32,40,116,114,117,
  11442. 101,41,32,123,10,32,32,32,32,32,32,32,32,100,101,102,40,68,44,32,39,109,
  11443. 111,100,76,111,97,100,101,100,39,44,32,123,125,41,59,10,32,32,32,32,125,10,
  11444. 125,41,40,116,104,105,115,44,32,68,117,107,116,97,112,101,41,59,10,0,
  11445. };
  11446. #endif /* DUK_USE_BUILTIN_INITJS */
  11447. #elif defined(DUK_USE_DOUBLE_BE)
  11448. DUK_INTERNAL const duk_uint8_t duk_strings_data[2624] = {
  11449. 55,86,227,24,145,55,102,120,144,3,63,94,228,54,100,137,186,50,11,164,109,
  11450. 77,215,5,61,35,106,206,149,110,4,254,219,237,58,8,196,24,103,74,183,2,127,
  11451. 103,246,93,4,98,12,47,180,67,103,246,127,101,208,70,32,194,186,134,207,236,
  11452. 254,203,160,140,65,133,246,136,108,254,199,237,186,8,196,24,87,80,217,253,
  11453. 143,219,116,17,136,49,30,209,13,159,220,116,75,3,30,65,244,17,136,48,174,
  11454. 209,13,159,220,116,17,136,48,158,161,179,251,142,130,49,6,17,209,130,96,
  11455. 237,80,75,47,160,140,65,142,134,133,41,34,110,134,133,41,34,3,25,110,8,22,
  11456. 158,130,38,163,8,217,200,158,76,156,210,117,128,153,203,210,70,46,137,187,
  11457. 18,27,164,187,201,209,130,100,55,91,70,4,145,63,66,231,44,128,105,187,41,
  11458. 197,13,49,122,8,196,24,71,75,70,138,104,115,77,215,5,36,20,201,214,209,107,
  11459. 79,104,209,144,168,105,6,207,251,209,104,209,125,212,227,66,127,235,191,
  11460. 239,232,180,90,52,95,69,247,83,141,9,255,174,255,191,162,211,80,210,253,23,
  11461. 221,78,52,39,254,183,254,254,139,72,105,126,139,238,167,26,19,255,91,255,
  11462. 127,69,166,129,191,69,247,83,141,9,255,175,255,191,162,213,26,50,23,232,
  11463. 190,234,113,161,63,245,115,119,86,227,118,83,138,26,98,9,110,48,86,22,148,
  11464. 160,152,22,82,70,46,137,44,8,180,163,32,104,98,206,32,17,7,16,88,101,100,
  11465. 206,42,70,36,108,205,18,74,140,33,196,230,60,2,152,146,33,38,230,8,36,79,
  11466. 182,251,65,156,151,24,200,33,145,162,25,80,209,24,67,0,166,68,52,174,61,73,
  11467. 25,33,205,25,27,84,177,195,234,220,1,144,105,99,135,217,16,17,17,208,72,
  11468. 199,179,60,93,100,146,49,232,162,64,76,135,19,152,244,44,136,223,98,67,4,
  11469. 18,33,247,217,158,36,0,209,190,156,13,26,201,21,111,165,67,64,180,100,145,
  11470. 62,250,32,45,100,33,55,214,1,229,223,65,19,72,187,236,206,137,35,125,120,
  11471. 190,201,104,105,15,190,201,212,136,136,125,246,160,137,27,83,239,171,37,
  11472. 200,218,159,125,168,34,192,61,27,233,93,22,1,114,78,250,28,76,130,112,200,
  11473. 93,245,164,188,207,190,204,17,49,38,109,246,160,93,8,119,185,13,153,34,173,
  11474. 246,113,0,136,48,76,10,90,26,78,182,140,9,34,130,161,100,235,64,194,9,226,
  11475. 44,166,1,41,221,153,226,235,118,120,121,58,72,197,209,63,71,69,76,15,34,
  11476. 164,73,244,171,112,39,246,223,104,169,18,125,42,220,9,253,159,217,38,68,
  11477. 159,104,134,207,236,254,201,18,36,250,134,207,236,254,201,50,36,251,68,54,
  11478. 127,99,246,200,145,39,212,54,127,99,246,200,145,39,218,33,179,251,131,200,
  11479. 147,234,27,63,184,81,137,62,149,110,4,254,219,237,20,98,79,165,91,129,63,
  11480. 179,251,36,152,147,237,16,217,253,159,217,32,196,159,80,217,253,159,217,36,
  11481. 196,159,104,134,207,236,126,217,6,36,250,134,207,236,126,217,6,36,251,68,
  11482. 54,127,112,115,18,125,67,103,247,8,149,2,8,196,24,143,131,137,146,90,121,
  11483. 35,162,44,140,35,102,160,226,100,235,138,89,18,102,13,10,82,68,200,151,106,
  11484. 130,88,131,4,192,73,225,228,85,162,137,147,168,108,252,18,42,209,68,201,
  11485. 212,54,126,89,23,104,162,100,245,17,230,207,193,34,237,20,76,158,162,60,
  11486. 217,249,100,109,162,137,147,163,117,2,178,120,36,109,162,137,147,163,117,2,
  11487. 178,121,100,101,162,137,147,165,91,129,63,4,140,180,81,50,116,171,112,39,
  11488. 229,145,150,138,38,78,161,179,251,63,178,240,72,203,69,19,39,80,217,253,
  11489. 159,217,121,100,109,162,137,147,212,71,155,63,179,251,47,4,141,180,81,50,
  11490. 122,136,243,103,246,127,101,229,145,150,138,38,78,161,179,251,31,182,240,
  11491. 72,203,69,19,39,80,217,253,143,219,121,100,109,162,137,147,212,71,155,63,
  11492. 177,251,111,4,141,180,81,50,122,136,243,103,246,63,109,229,145,54,138,38,
  11493. 78,161,179,251,133,90,40,153,61,68,121,179,251,132,196,128,31,80,217,248,
  11494. 36,76,72,1,245,13,159,150,69,68,128,31,168,143,54,126,9,21,18,0,126,162,60,
  11495. 217,249,100,100,72,1,244,110,160,86,79,4,140,137,0,62,141,212,10,201,229,
  11496. 145,113,32,7,210,173,192,159,130,69,196,128,31,74,183,2,126,89,23,18,0,125,
  11497. 67,103,246,127,101,224,145,113,32,7,212,54,127,103,246,94,89,25,18,0,126,
  11498. 162,60,217,253,159,217,120,36,100,72,1,250,136,243,103,246,127,101,229,145,
  11499. 113,32,7,212,54,127,99,246,222,9,23,18,0,125,67,103,246,63,109,229,145,145,
  11500. 32,7,234,35,205,159,216,253,183,130,70,68,128,31,168,143,54,127,99,246,222,
  11501. 89,17,18,0,125,67,103,247,9,137,0,63,81,30,108,254,224,130,115,240,98,66,
  11502. 128,92,136,84,45,101,180,81,50,28,78,99,193,18,40,56,153,58,178,52,211,58,
  11503. 17,46,134,133,41,34,164,75,164,104,156,52,52,199,37,222,232,206,66,64,207,
  11504. 18,66,136,137,19,173,62,46,155,181,167,72,147,235,226,233,186,120,121,58,
  11505. 226,157,214,111,84,76,73,36,109,24,72,130,100,112,200,178,76,157,124,92,
  11506. 242,70,120,25,193,34,245,241,117,240,97,1,107,33,25,212,54,160,90,7,244,29,
  11507. 24,38,66,254,223,215,125,119,215,126,232,190,43,226,67,244,1,250,193,125,
  11508. 111,216,11,234,254,192,63,96,159,173,234,26,84,53,19,194,126,175,168,105,
  11509. 80,212,79,8,234,26,84,53,19,193,156,20,144,83,52,167,20,52,198,109,24,18,
  11510. 68,225,115,150,64,53,52,104,200,84,52,131,76,167,20,52,200,46,7,48,52,146,
  11511. 132,102,57,33,165,139,168,209,154,32,104,220,193,189,214,27,16,209,176,23,
  11512. 26,220,98,149,110,116,70,75,188,98,116,136,34,33,101,4,192,223,178,32,38,6,
  11513. 144,18,67,72,1,58,67,0,100,95,74,17,159,217,31,210,132,103,246,58,251,33,
  11514. 121,232,55,150,227,125,143,216,16,190,91,141,246,68,31,150,223,178,39,150,
  11515. 223,177,251,0,244,135,97,37,32,24,132,104,24,66,161,175,164,202,134,140,
  11516. 151,39,212,125,255,221,125,74,86,9,79,168,104,201,116,178,139,154,22,134,
  11517. 145,72,51,93,18,116,64,145,13,39,82,34,33,38,73,76,132,185,4,185,187,198,
  11518. 100,229,233,197,13,49,228,73,247,4,4,78,98,79,184,32,34,105,187,201,147,
  11519. 154,185,187,200,147,165,233,197,13,50,230,239,82,98,151,167,20,52,206,145,
  11520. 39,234,76,69,245,22,190,224,128,138,228,73,244,180,90,251,130,2,43,145,39,
  11521. 234,76,76,243,155,51,162,68,159,88,230,204,234,145,39,234,76,67,240,38,67,
  11522. 200,147,232,193,50,46,68,159,169,49,31,206,164,100,137,18,125,59,169,25,54,
  11523. 68,159,169,49,51,200,109,38,73,42,68,159,88,134,210,100,147,100,73,250,147,
  11524. 20,188,65,57,163,146,164,73,246,68,19,154,57,74,68,159,169,49,51,200,90,
  11525. 209,34,9,205,28,159,34,79,172,66,214,137,16,78,104,228,121,18,125,154,24,
  11526. 72,152,147,236,208,194,101,205,39,92,82,200,147,145,137,63,82,98,103,144,
  11527. 181,162,68,19,154,57,60,196,159,88,133,173,18,32,156,209,201,166,36,253,73,
  11528. 138,94,32,156,209,201,70,36,251,34,9,205,28,154,98,79,212,152,153,228,54,
  11529. 147,36,148,98,79,172,67,105,50,73,102,36,253,73,136,254,117,35,36,24,147,
  11530. 233,221,72,201,38,36,253,73,136,126,6,12,98,79,163,6,20,98,79,212,152,135,
  11531. 224,76,135,49,39,209,130,100,89,137,63,82,98,103,156,217,157,6,36,250,199,
  11532. 54,103,113,137,63,82,98,47,168,181,247,4,4,86,98,79,165,162,215,220,16,17,
  11533. 57,137,62,205,12,36,166,238,173,194,2,201,217,161,132,236,167,20,52,210,
  11534. 155,186,183,8,11,39,70,9,147,178,156,80,211,50,110,236,208,194,118,83,138,
  11535. 26,102,77,221,24,38,78,202,113,67,76,54,186,195,245,38,34,188,17,145,23,55,
  11536. 117,241,32,145,36,57,173,155,186,75,189,205,35,102,128,44,243,119,74,139,
  11537. 144,113,243,221,36,77,21,38,144,210,161,168,158,35,230,144,192,154,42,77,
  11538. 33,165,67,81,60,15,173,7,90,159,49,13,213,64,186,17,62,96,47,170,129,116,
  11539. 33,165,64,202,113,36,226,134,70,110,234,220,32,44,157,163,222,72,244,64,
  11540. 145,23,55,118,143,121,35,209,2,68,140,221,213,184,64,89,58,183,88,145,232,
  11541. 129,34,46,110,234,221,98,71,162,4,136,153,80,50,156,80,211,22,79,90,38,105,
  11542. 16,17,17,207,18,61,96,17,10,192,76,71,106,220,32,44,157,19,152,240,68,138,
  11543. 17,193,30,137,195,39,65,51,8,224,143,65,54,22,46,103,68,112,71,162,112,200,
  11544. 184,144,116,17,59,20,24,243,52,72,58,8,134,42,23,50,68,108,3,206,87,71,164,
  11545. 0,142,73,57,132,41,42,72,225,107,4,167,212,52,100,191,92,83,161,163,37,250,
  11546. 226,158,141,145,208,89,154,79,90,4,66,73,209,153,100,180,8,133,145,208,89,
  11547. 158,36,169,35,34,17,244,145,198,247,60,137,114,26,97,57,162,4,206,137,116,
  11548. 17,136,48,144,68,212,97,27,57,24,64,90,201,18,5,13,25,4,5,172,160,123,215,
  11549. 138,62,46,121,35,60,117,18,233,27,70,18,32,10,200,212,75,175,139,166,233,
  11550. 225,228,235,138,227,130,93,117,155,215,197,207,36,103,131,212,11,161,58,
  11551. 226,186,110,234,220,32,44,157,148,226,134,153,19,119,101,56,161,166,88,156,
  11552. 217,78,52,20,221,17,200,147,25,137,53,17,180,97,34,0,172,140,19,154,84,26,
  11553. 145,0,86,68,90,40,152,2,178,22,160,93,8,69,19,18,98,37,210,94,100,108,144,
  11554. 21,145,8,151,75,23,100,141,66,37,217,16,11,32,226,248,146,164,108,250,75,
  11555. 204,141,146,28,217,24,177,33,50,66,72,128,92,6,66,161,164,235,226,231,146,
  11556. 51,65,36,225,144,168,105,58,248,185,228,140,240,97,68,128,153,38,98,79,174,
  11557. 179,122,248,185,228,140,241,214,129,132,150,12,73,245,214,111,95,23,60,145,
  11558. 158,58,50,72,81,67,230,232,184,196,159,95,23,77,211,195,201,215,21,47,139,
  11559. 166,233,225,228,50,200,211,76,229,2,201,25,149,241,67,102,138,52,146,16,30,
  11560. 67,18,66,3,201,34,52,78,25,61,72,160,94,115,30,230,145,179,73,26,39,12,158,
  11561. 164,81,33,144,78,25,61,72,160,94,115,30,230,145,179,72,200,39,12,158,164,
  11562. 80,132,75,165,67,81,50,21,18,235,65,214,169,224,140,137,210,173,192,154,30,
  11563. 8,200,157,67,102,66,84,11,71,169,20,19,209,139,162,158,207,15,39,73,24,186,
  11564. 43,236,176,217,130,253,36,98,232,187,177,33,73,18,52,68,233,35,23,69,93,
  11565. 136,26,98,116,145,139,162,158,146,160,95,73,24,186,37,12,72,5,16,64,145,10,
  11566. 32,76,71,64,156,217,161,180,34,6,64,208,198,36,78,50,20,20,92,204,50,44,
  11567. 147,32,134,226,17,114,33,202,134,129,107,192,202,232,160,180,104,166,135,
  11568. 52,72,40,144,213,33,178,152,26,34,56,163,105,44,104,146,116,139,77,43,34,
  11569. 98,57,38,116,72,179,60,93,97,206,56,52,240,242,56,163,168,34,81,57,178,153,
  11570. 42,228,12,182,58,22,66,89,19,57,68,176,74,68,35,104,195,18,239,116,102,114,
  11571. 94,100,104,228,100,49,238,140,203,42,60,145,35,104,181,146,113,161,10,80,
  11572. 46,68,82,24,245,145,132,108,228,148,54,100,137,64,34,13,100,153,222,1,40,6,
  11573. 33,223,20,84,19,34,95,23,76,130,153,6,103,208,43,64,141,41,130,104,17,112,
  11574. 130,44,96,
  11575. };
  11576. /* to convert a heap stridx to a token number, subtract
  11577. * DUK_STRIDX_START_RESERVED and add DUK_TOK_START_RESERVED.
  11578. */
  11579. /* native functions: 147 */
  11580. DUK_INTERNAL const duk_c_function duk_bi_native_functions[147] = {
  11581. duk_bi_array_constructor,
  11582. duk_bi_array_constructor_is_array,
  11583. duk_bi_array_prototype_concat,
  11584. duk_bi_array_prototype_indexof_shared,
  11585. duk_bi_array_prototype_iter_shared,
  11586. duk_bi_array_prototype_join_shared,
  11587. duk_bi_array_prototype_pop,
  11588. duk_bi_array_prototype_push,
  11589. duk_bi_array_prototype_reduce_shared,
  11590. duk_bi_array_prototype_reverse,
  11591. duk_bi_array_prototype_shift,
  11592. duk_bi_array_prototype_slice,
  11593. duk_bi_array_prototype_sort,
  11594. duk_bi_array_prototype_splice,
  11595. duk_bi_array_prototype_to_string,
  11596. duk_bi_array_prototype_unshift,
  11597. duk_bi_arraybuffer_constructor,
  11598. duk_bi_arraybuffer_isview,
  11599. duk_bi_boolean_constructor,
  11600. duk_bi_boolean_prototype_tostring_shared,
  11601. duk_bi_buffer_compare_shared,
  11602. duk_bi_buffer_constructor,
  11603. duk_bi_buffer_prototype_tostring_shared,
  11604. duk_bi_buffer_readfield,
  11605. duk_bi_buffer_slice_shared,
  11606. duk_bi_buffer_writefield,
  11607. duk_bi_dataview_constructor,
  11608. duk_bi_date_constructor,
  11609. duk_bi_date_constructor_now,
  11610. duk_bi_date_constructor_parse,
  11611. duk_bi_date_constructor_utc,
  11612. duk_bi_date_prototype_get_shared,
  11613. duk_bi_date_prototype_get_timezone_offset,
  11614. duk_bi_date_prototype_set_shared,
  11615. duk_bi_date_prototype_set_time,
  11616. duk_bi_date_prototype_to_json,
  11617. duk_bi_date_prototype_tostring_shared,
  11618. duk_bi_date_prototype_value_of,
  11619. duk_bi_duktape_object_act,
  11620. duk_bi_duktape_object_compact,
  11621. duk_bi_duktape_object_dec,
  11622. duk_bi_duktape_object_enc,
  11623. duk_bi_duktape_object_fin,
  11624. duk_bi_duktape_object_gc,
  11625. duk_bi_duktape_object_info,
  11626. duk_bi_error_constructor_shared,
  11627. duk_bi_error_prototype_filename_getter,
  11628. duk_bi_error_prototype_linenumber_getter,
  11629. duk_bi_error_prototype_nop_setter,
  11630. duk_bi_error_prototype_stack_getter,
  11631. duk_bi_error_prototype_to_string,
  11632. duk_bi_function_constructor,
  11633. duk_bi_function_prototype,
  11634. duk_bi_function_prototype_apply,
  11635. duk_bi_function_prototype_bind,
  11636. duk_bi_function_prototype_call,
  11637. duk_bi_function_prototype_to_string,
  11638. duk_bi_global_object_decode_uri,
  11639. duk_bi_global_object_decode_uri_component,
  11640. duk_bi_global_object_encode_uri,
  11641. duk_bi_global_object_encode_uri_component,
  11642. duk_bi_global_object_escape,
  11643. duk_bi_global_object_eval,
  11644. duk_bi_global_object_is_finite,
  11645. duk_bi_global_object_is_nan,
  11646. duk_bi_global_object_parse_float,
  11647. duk_bi_global_object_parse_int,
  11648. duk_bi_global_object_print_helper,
  11649. duk_bi_global_object_require,
  11650. duk_bi_global_object_unescape,
  11651. duk_bi_json_object_parse,
  11652. duk_bi_json_object_stringify,
  11653. duk_bi_logger_constructor,
  11654. duk_bi_logger_prototype_fmt,
  11655. duk_bi_logger_prototype_log_shared,
  11656. duk_bi_logger_prototype_raw,
  11657. duk_bi_math_object_max,
  11658. duk_bi_math_object_min,
  11659. duk_bi_math_object_onearg_shared,
  11660. duk_bi_math_object_random,
  11661. duk_bi_math_object_twoarg_shared,
  11662. duk_bi_nodejs_buffer_byte_length,
  11663. duk_bi_nodejs_buffer_concat,
  11664. duk_bi_nodejs_buffer_constructor,
  11665. duk_bi_nodejs_buffer_copy,
  11666. duk_bi_nodejs_buffer_fill,
  11667. duk_bi_nodejs_buffer_is_buffer,
  11668. duk_bi_nodejs_buffer_is_encoding,
  11669. duk_bi_nodejs_buffer_tojson,
  11670. duk_bi_nodejs_buffer_tostring,
  11671. duk_bi_nodejs_buffer_write,
  11672. duk_bi_number_constructor,
  11673. duk_bi_number_prototype_to_exponential,
  11674. duk_bi_number_prototype_to_fixed,
  11675. duk_bi_number_prototype_to_locale_string,
  11676. duk_bi_number_prototype_to_precision,
  11677. duk_bi_number_prototype_to_string,
  11678. duk_bi_number_prototype_value_of,
  11679. duk_bi_object_constructor,
  11680. duk_bi_object_constructor_create,
  11681. duk_bi_object_constructor_define_properties,
  11682. duk_bi_object_constructor_define_property,
  11683. duk_bi_object_constructor_get_own_property_descriptor,
  11684. duk_bi_object_constructor_is_extensible,
  11685. duk_bi_object_constructor_is_sealed_frozen_shared,
  11686. duk_bi_object_constructor_keys_shared,
  11687. duk_bi_object_constructor_prevent_extensions,
  11688. duk_bi_object_constructor_seal_freeze_shared,
  11689. duk_bi_object_getprototype_shared,
  11690. duk_bi_object_prototype_has_own_property,
  11691. duk_bi_object_prototype_is_prototype_of,
  11692. duk_bi_object_prototype_property_is_enumerable,
  11693. duk_bi_object_prototype_to_locale_string,
  11694. duk_bi_object_prototype_to_string,
  11695. duk_bi_object_prototype_value_of,
  11696. duk_bi_object_setprototype_shared,
  11697. duk_bi_pointer_constructor,
  11698. duk_bi_pointer_prototype_tostring_shared,
  11699. duk_bi_proxy_constructor,
  11700. duk_bi_regexp_constructor,
  11701. duk_bi_regexp_prototype_exec,
  11702. duk_bi_regexp_prototype_test,
  11703. duk_bi_regexp_prototype_to_string,
  11704. duk_bi_string_constructor,
  11705. duk_bi_string_constructor_from_char_code,
  11706. duk_bi_string_prototype_caseconv_shared,
  11707. duk_bi_string_prototype_char_at,
  11708. duk_bi_string_prototype_char_code_at,
  11709. duk_bi_string_prototype_concat,
  11710. duk_bi_string_prototype_indexof_shared,
  11711. duk_bi_string_prototype_locale_compare,
  11712. duk_bi_string_prototype_match,
  11713. duk_bi_string_prototype_replace,
  11714. duk_bi_string_prototype_search,
  11715. duk_bi_string_prototype_slice,
  11716. duk_bi_string_prototype_split,
  11717. duk_bi_string_prototype_substr,
  11718. duk_bi_string_prototype_substring,
  11719. duk_bi_string_prototype_to_string,
  11720. duk_bi_string_prototype_trim,
  11721. duk_bi_thread_constructor,
  11722. duk_bi_thread_current,
  11723. duk_bi_thread_resume,
  11724. duk_bi_thread_yield,
  11725. duk_bi_type_error_thrower,
  11726. duk_bi_typedarray_constructor,
  11727. duk_bi_typedarray_set,
  11728. };
  11729. DUK_INTERNAL const duk_uint8_t duk_builtins_data[1952] = {
  11730. 105,195,75,16,121,40,105,51,14,252,104,52,8,131,72,0,115,225,65,165,236,55,
  11731. 243,6,145,32,210,24,210,182,25,249,35,120,216,99,226,13,78,225,116,177,164,
  11732. 180,44,192,4,202,52,150,220,24,0,169,70,146,219,123,0,23,40,210,91,110,96,
  11733. 3,37,26,75,109,172,0,108,163,73,109,177,128,14,148,105,45,181,176,1,242,
  11734. 144,56,209,32,94,6,167,101,98,80,211,24,1,250,67,72,168,67,232,13,46,128,
  11735. 47,162,52,164,0,62,80,163,72,128,61,40,107,26,7,37,20,53,200,131,88,0,66,
  11736. 134,185,16,98,80,215,34,11,96,0,138,26,228,65,76,0,69,67,92,136,37,128,6,
  11737. 168,107,145,4,48,1,165,13,114,32,118,0,44,161,174,68,12,192,7,148,53,200,
  11738. 129,88,1,26,134,165,48,130,80,31,255,241,69,224,63,252,0,0,0,0,0,0,46,32,
  11739. 63,248,0,0,0,0,0,0,47,98,7,140,16,116,194,7,12,48,108,196,6,140,80,100,198,
  11740. 6,12,112,92,200,5,140,149,192,202,91,204,181,184,204,91,76,213,176,206,90,
  11741. 204,240,84,208,5,13,9,124,210,43,13,24,64,226,131,205,112,56,216,3,77,152,
  11742. 48,218,130,205,184,40,220,130,77,216,32,222,129,205,248,24,224,129,78,25,
  11743. 214,163,226,90,80,145,104,65,37,157,0,150,99,242,89,78,73,100,58,37,140,
  11744. 236,150,35,194,88,79,73,96,69,37,125,12,122,188,134,62,0,2,165,68,39,255,
  11745. 255,193,43,67,0,0,80,127,192,58,182,216,80,0,21,59,154,64,0,107,76,200,172,
  11746. 180,146,176,198,138,187,43,42,204,136,170,181,146,168,214,80,0,26,155,81,
  11747. 42,77,4,168,180,20,0,6,160,206,74,123,73,64,0,127,255,4,10,153,219,28,198,
  11748. 163,184,130,140,224,10,43,144,40,141,164,161,183,18,132,222,64,161,127,128,
  11749. 0,63,225,1,109,74,8,137,71,56,5,4,213,20,3,115,233,249,177,240,80,255,192,
  11750. 6,120,2,64,127,195,0,173,28,56,20,96,80,128,0,206,192,143,167,64,164,156,
  11751. 131,2,112,14,125,55,9,4,216,40,19,80,180,77,3,9,51,13,94,153,7,159,76,64,
  11752. 207,192,0,102,0,103,255,255,242,240,67,73,112,33,168,0,12,180,16,212,0,10,
  11753. 88,8,106,0,7,43,4,53,0,4,149,4,31,128,0,202,66,15,255,255,194,137,254,0,50,
  11754. 135,195,224,127,196,2,87,132,17,82,143,20,10,44,80,36,239,196,147,63,146,
  11755. 119,0,125,49,129,52,152,64,154,128,0,201,96,137,36,131,36,142,17,18,40,82,
  11756. 77,97,145,33,135,68,130,37,17,247,208,71,159,65,29,125,8,0,12,113,244,32,0,
  11757. 49,184,176,70,162,16,20,95,240,0,7,252,80,37,120,193,81,196,194,0,3,69,19,
  11758. 0,81,191,197,140,192,127,239,255,255,255,255,255,255,140,64,0,0,0,0,0,0,0,
  11759. 1,139,192,127,248,0,0,0,0,0,0,138,192,127,240,0,0,0,0,0,0,139,64,255,240,0,
  11760. 0,0,0,0,0,0,31,241,128,149,224,0,0,0,0,0,0,0,0,13,71,96,37,25,120,148,86,
  11761. 16,69,23,73,19,92,36,73,124,129,71,255,0,56,136,233,34,3,223,208,241,192,3,
  11762. 254,56,18,188,135,255,128,0,0,0,0,0,11,104,228,128,135,18,4,0,6,26,72,16,0,
  11763. 42,49,32,64,0,225,132,129,0,4,133,146,4,0,21,210,72,16,0,103,65,32,64,1,
  11764. 220,228,100,162,146,130,20,74,8,72,248,64,2,33,3,225,0,9,131,143,132,0,42,
  11765. 12,62,16,0,184,40,248,64,3,32,131,225,0,13,129,143,132,0,58,4,62,16,0,248,
  11766. 8,248,64,4,32,3,225,0,17,127,143,132,0,73,252,62,16,1,55,232,248,64,5,31,
  11767. 131,225,0,21,125,143,132,0,89,244,62,16,1,119,201,0,31,4,68,123,144,148,0,
  11768. 97,236,66,80,1,151,169,10,248,0,211,208,133,124,0,109,230,66,254,0,56,242,
  11769. 33,127,0,29,120,144,207,128,15,60,8,103,192,7,221,228,37,0,32,119,16,148,0,
  11770. 133,218,66,190,0,68,236,33,95,0,35,117,144,191,128,18,58,136,95,192,9,92,
  11771. 195,225,0,38,114,144,148,0,156,41,31,224,0,15,249,1,138,144,64,192,2,2,225,
  11772. 132,221,9,70,112,70,111,198,111,72,0,0,0,0,0,0,0,0,13,198,240,71,19,201,40,
  11773. 239,64,10,79,248,0,3,254,72,86,209,5,155,36,17,46,185,137,129,109,203,140,
  11774. 11,78,94,96,13,28,200,1,74,255,0,2,127,202,4,218,43,131,100,130,32,5,47,
  11775. 252,0,9,255,44,19,104,173,237,146,8,128,20,207,240,0,39,252,192,77,162,183,
  11776. 54,72,34,0,83,127,192,0,159,243,65,54,138,218,217,32,136,1,78,255,0,2,127,
  11777. 206,4,218,43,99,100,130,32,5,63,252,0,9,255,60,19,104,173,109,146,8,128,15,
  11778. 255,242,27,16,16,1,111,194,162,197,21,218,90,240,16,0,154,236,110,237,85,
  11779. 69,154,208,15,249,139,144,191,190,142,123,218,176,15,253,197,81,217,74,224,
  11780. 191,154,144,15,246,242,222,197,73,185,67,154,112,16,2,72,126,213,17,11,70,
  11781. 26,80,15,249,168,39,153,159,206,243,90,48,15,253,168,39,153,159,206,243,82,
  11782. 104,39,17,158,156,80,0,22,114,113,64,0,153,169,197,0,3,102,40,33,150,156,
  11783. 80,0,70,82,113,64,1,89,41,197,0,6,100,39,20,0,29,142,156,80,0,134,50,98,
  11784. 243,21,53,121,136,160,144,0,22,26,120,24,73,197,0,9,96,167,20,0,41,128,156,
  11785. 80,0,181,250,113,64,3,1,255,254,0,81,20,100,47,145,216,23,255,240,0,11,255,
  11786. 248,0,3,255,252,81,252,4,12,68,248,0,0,0,0,0,129,167,1,26,144,9,165,0,26,
  11787. 177,199,197,132,30,147,16,120,86,65,217,80,240,232,164,120,114,80,60,52,39,
  11788. 32,84,223,192,15,59,30,129,156,115,6,81,160,7,253,40,0,5,81,252,0,1,255,78,
  11789. 0,84,113,96,128,0,209,69,128,21,87,240,0,7,253,72,1,81,221,66,0,3,69,117,0,
  11790. 85,159,192,0,31,245,97,10,100,32,0,0,0,0,0,0,0,10,164,130,97,221,191,113,3,
  11791. 20,146,12,18,200,47,74,30,23,37,15,128,0,143,146,135,192,0,133,169,67,224,
  11792. 0,98,196,161,240,0,65,90,80,248,0,41,63,255,194,109,65,11,137,191,174,45,
  11793. 153,98,242,229,191,147,102,8,190,94,92,183,242,65,167,114,12,188,185,111,
  11794. 228,131,70,29,217,54,105,221,156,0,171,255,128,9,208,68,128,255,174,0,25,
  11795. 168,194,64,0,130,177,254,0,0,255,176,1,3,120,186,64,12,13,194,233,0,32,54,
  11796. 139,164,0,196,216,46,144,2,19,88,186,64,12,141,66,233,0,34,52,139,164,0,
  11797. 140,208,46,144,2,67,56,203,64,12,12,195,45,0,32,50,140,180,0,196,200,50,
  11798. 208,2,19,24,203,64,12,140,67,45,0,34,48,140,180,0,140,192,50,208,2,64,127,
  11799. 255,128,21,38,73,7,1,132,128,0,133,105,252,19,140,3,255,0,0,0,0,0,0,0,25,
  11800. 127,102,0,1,91,127,4,227,0,255,192,0,0,0,0,0,0,6,95,218,128,0,87,31,193,56,
  11801. 192,63,240,0,0,0,0,0,0,1,151,246,224,0,21,215,240,78,48,16,0,0,0,0,0,0,0,0,
  11802. 101,253,200,0,5,121,252,19,140,4,0,0,0,0,0,0,0,0,25,127,118,0,1,95,127,4,
  11803. 227,1,0,64,0,0,0,0,0,0,6,95,222,128,0,88,31,193,56,192,64,16,0,0,0,0,0,0,1,
  11804. 151,247,224,0,22,23,240,78,48,16,4,0,0,0,0,0,0,0,101,254,8,0,5,137,252,19,
  11805. 140,4,2,0,0,0,0,0,0,0,25,127,134,0,1,99,127,0,89,218,146,20,74,228,80,171,
  11806. 17,64,162,132,248,162,64,0,193,255,138,5,137,161,116,38,69,210,0,32,152,23,
  11807. 72,0,10,92,93,32,1,41,97,116,128,8,165,69,210,0,50,148,23,72,0,18,76,93,32,
  11808. 1,73,33,116,128,9,36,69,210,0,52,144,23,72,0,26,60,93,32,1,104,225,116,128,
  11809. 2,35,69,210,0,24,140,23,104,0,42,44,93,160,1,168,161,118,128,10,162,69,218,
  11810. 0,58,136,25,98,28,101,160,2,8,97,150,128,0,161,70,90,0,18,132,25,104,0,138,
  11811. 12,101,160,3,40,33,150,128,1,32,70,90,0,20,128,25,104,0,145,252,101,160,3,
  11812. 71,225,150,128,1,159,70,90,0,22,124,25,104,0,33,236,101,160,1,135,161,152,
  11813. 128,2,158,70,98,0,26,120,25,136,0,169,220,102,32,3,180,117,150,57,214,0,
  11814. 157,85,98,112,80,137,241,66,128,0,166,213,33,53,24,66,121,106,0,
  11815. };
  11816. #ifdef DUK_USE_BUILTIN_INITJS
  11817. DUK_INTERNAL const duk_uint8_t duk_initjs_data[1757] = {
  11818. 47,42,10,32,42,32,32,73,110,105,116,32,99,111,100,101,32,102,111,114,32,
  11819. 108,101,103,97,99,121,32,99,111,109,112,97,116,105,98,105,108,105,116,121,
  11820. 46,10,32,42,10,32,42,32,32,67,111,109,112,97,116,105,98,105,108,105,116,
  11821. 121,32,112,114,111,112,101,114,116,105,101,115,32,47,32,119,114,97,112,112,
  11822. 101,114,32,102,117,110,99,116,105,111,110,115,32,104,101,114,101,32,97,108,
  11823. 108,111,119,32,68,117,107,116,97,112,101,32,116,111,32,114,101,109,97,105,
  11824. 110,10,32,42,32,32,99,111,109,112,97,116,105,98,108,101,32,102,111,114,32,
  11825. 117,115,101,114,32,99,111,100,101,32,119,104,101,110,32,99,111,114,101,32,
  11826. 102,101,97,116,117,114,101,115,32,97,114,101,32,99,104,97,110,103,101,100,
  11827. 44,32,119,105,116,104,111,117,116,32,98,117,114,100,101,110,105,110,103,10,
  11828. 32,42,32,32,116,104,101,32,109,97,105,110,32,67,32,99,111,100,101,32,119,
  11829. 105,116,104,32,99,111,109,112,97,116,105,98,105,108,105,116,121,32,115,116,
  11830. 117,102,102,46,10,32,42,10,32,42,32,32,84,104,105,115,32,102,105,108,101,
  11831. 32,105,115,32,109,105,110,105,102,105,101,100,32,119,105,116,104,32,85,103,
  11832. 108,105,102,121,74,83,32,111,114,32,116,104,101,32,99,108,111,115,117,114,
  11833. 101,32,99,111,109,112,105,108,101,114,46,32,32,66,111,116,104,32,119,105,
  11834. 108,108,10,32,42,32,32,114,101,110,97,109,101,32,118,97,114,105,97,98,108,
  11835. 101,115,44,32,114,101,109,111,118,101,32,99,111,109,109,101,110,116,115,44,
  11836. 32,97,110,100,32,97,114,101,32,99,108,101,118,101,114,32,101,110,111,117,
  11837. 103,104,32,116,111,32,100,114,111,112,32,97,110,121,10,32,42,32,32,34,105,
  11838. 102,32,40,102,97,108,115,101,41,32,123,32,46,46,46,32,125,34,32,98,108,111,
  11839. 99,107,115,32,97,108,116,111,103,101,116,104,101,114,44,32,115,111,32,116,
  11840. 104,97,116,39,115,32,97,110,32,101,102,102,101,99,116,105,118,101,32,119,
  11841. 97,121,32,116,111,10,32,42,32,32,100,105,115,97,98,108,101,32,99,117,114,
  11842. 114,101,110,116,108,121,32,117,110,110,101,101,100,101,100,32,99,111,100,
  11843. 101,46,10,32,42,47,10,10,40,102,117,110,99,116,105,111,110,40,71,44,32,68,
  11844. 41,32,123,10,32,32,32,32,39,117,115,101,32,115,116,114,105,99,116,39,59,10,
  11845. 10,32,32,32,32,102,117,110,99,116,105,111,110,32,100,101,102,40,111,98,106,
  11846. 101,99,116,44,32,110,97,109,101,44,32,118,97,108,117,101,41,32,123,10,32,
  11847. 32,32,32,32,32,32,32,79,98,106,101,99,116,46,100,101,102,105,110,101,80,
  11848. 114,111,112,101,114,116,121,40,111,98,106,101,99,116,44,32,110,97,109,101,
  11849. 44,32,123,10,32,32,32,32,32,32,32,32,32,32,32,32,118,97,108,117,101,58,32,
  11850. 118,97,108,117,101,44,10,32,32,32,32,32,32,32,32,32,32,32,32,119,114,105,
  11851. 116,97,98,108,101,58,32,116,114,117,101,44,10,32,32,32,32,32,32,32,32,32,
  11852. 32,32,32,101,110,117,109,101,114,97,98,108,101,58,32,102,97,108,115,101,44,
  11853. 10,32,32,32,32,32,32,32,32,32,32,32,32,99,111,110,102,105,103,117,114,97,
  11854. 98,108,101,58,32,116,114,117,101,10,32,32,32,32,32,32,32,32,125,41,59,10,
  11855. 32,32,32,32,125,10,10,32,32,32,32,102,117,110,99,116,105,111,110,32,100,
  11856. 101,102,68,40,110,97,109,101,44,32,118,97,108,117,101,41,32,123,10,32,32,
  11857. 32,32,32,32,32,32,100,101,102,40,68,44,32,110,97,109,101,44,32,118,97,108,
  11858. 117,101,41,59,10,32,32,32,32,125,10,10,32,32,32,32,47,47,32,67,111,109,112,
  11859. 97,116,105,98,105,108,105,116,121,32,102,111,114,32,39,99,111,110,115,111,
  11860. 108,101,46,108,111,103,39,46,10,32,32,32,32,105,102,32,40,102,97,108,115,
  11861. 101,41,32,123,10,32,32,32,32,32,32,32,32,99,111,110,115,111,108,101,32,61,
  11862. 32,123,10,32,32,32,32,32,32,32,32,32,32,32,32,108,111,103,58,32,102,117,
  11863. 110,99,116,105,111,110,40,41,32,123,10,32,32,32,32,32,32,32,32,32,32,32,32,
  11864. 32,32,32,32,112,114,105,110,116,40,65,114,114,97,121,46,112,114,111,116,
  11865. 111,116,121,112,101,46,106,111,105,110,46,99,97,108,108,40,97,114,103,117,
  11866. 109,101,110,116,115,44,32,39,32,39,41,41,59,10,32,32,32,32,32,32,32,32,32,
  11867. 32,32,32,125,10,32,32,32,32,32,32,32,32,125,59,10,32,32,32,32,125,10,10,32,
  11868. 32,32,32,47,47,32,68,117,107,116,97,112,101,46,108,105,110,101,40,41,32,
  11869. 119,97,115,32,114,101,109,111,118,101,100,32,105,110,32,68,117,107,116,97,
  11870. 112,101,32,48,46,49,49,46,48,44,32,104,101,114,101,39,115,32,97,110,32,101,
  11871. 120,97,109,112,108,101,10,32,32,32,32,47,47,32,114,101,112,108,97,99,101,
  11872. 109,101,110,116,32,117,115,101,114,32,99,111,100,101,32,99,111,117,108,100,
  11873. 32,117,115,101,46,10,32,32,32,32,105,102,32,40,102,97,108,115,101,41,32,
  11874. 123,10,32,32,32,32,32,32,32,32,100,101,102,40,68,44,32,39,108,105,110,101,
  11875. 39,44,32,102,117,110,99,116,105,111,110,32,40,41,32,123,10,32,32,32,32,32,
  11876. 32,32,32,32,32,32,32,39,117,115,101,32,100,117,107,32,110,111,116,97,105,
  11877. 108,39,59,10,10,32,32,32,32,32,32,32,32,32,32,32,32,47,42,32,84,97,105,108,
  11878. 32,99,97,108,108,115,32,97,114,101,32,112,114,101,118,101,110,116,101,100,
  11879. 32,116,111,32,101,110,115,117,114,101,32,99,97,108,108,105,110,103,32,97,
  11880. 99,116,105,118,97,116,105,111,110,32,101,120,105,115,116,115,46,10,32,32,
  11881. 32,32,32,32,32,32,32,32,32,32,32,42,32,67,97,108,108,32,115,116,97,99,107,
  11882. 32,105,110,100,105,99,101,115,58,32,45,49,32,61,32,68,117,107,116,97,112,
  11883. 101,46,97,99,116,44,32,45,50,32,61,32,103,101,116,67,117,114,114,101,110,
  11884. 116,76,105,110,101,44,32,45,51,32,61,32,99,97,108,108,101,114,10,32,32,32,
  11885. 32,32,32,32,32,32,32,32,32,32,42,47,10,10,32,32,32,32,32,32,32,32,32,32,32,
  11886. 32,114,101,116,117,114,110,32,40,68,117,107,116,97,112,101,46,97,99,116,40,
  11887. 45,51,41,32,124,124,32,123,125,41,46,108,105,110,101,78,117,109,98,101,114,
  11888. 59,10,32,32,32,32,32,32,32,32,125,41,59,10,32,32,32,32,125,10,10,32,32,32,
  11889. 32,47,47,32,76,111,103,103,101,114,32,111,98,106,101,99,116,32,102,111,114,
  11890. 32,67,32,99,111,100,101,32,112,114,111,118,105,100,101,100,32,98,121,32,
  11891. 105,110,105,116,32,99,111,100,101,32,110,111,119,46,10,32,32,32,32,105,102,
  11892. 32,40,116,114,117,101,41,32,123,10,32,32,32,32,32,32,32,32,100,101,102,40,
  11893. 68,46,76,111,103,103,101,114,44,32,39,99,108,111,103,39,44,32,110,101,119,
  11894. 32,68,46,76,111,103,103,101,114,40,39,67,39,41,41,59,10,32,32,32,32,125,10,
  11895. 10,32,32,32,32,47,47,32,84,114,97,99,107,105,110,103,32,116,97,98,108,101,
  11896. 32,102,111,114,32,67,111,109,109,111,110,74,83,32,109,111,100,117,108,101,
  11897. 32,108,111,97,100,105,110,103,46,10,32,32,32,32,105,102,32,40,116,114,117,
  11898. 101,41,32,123,10,32,32,32,32,32,32,32,32,100,101,102,40,68,44,32,39,109,
  11899. 111,100,76,111,97,100,101,100,39,44,32,123,125,41,59,10,32,32,32,32,125,10,
  11900. 125,41,40,116,104,105,115,44,32,68,117,107,116,97,112,101,41,59,10,0,
  11901. };
  11902. #endif /* DUK_USE_BUILTIN_INITJS */
  11903. #elif defined(DUK_USE_DOUBLE_ME)
  11904. DUK_INTERNAL const duk_uint8_t duk_strings_data[2624] = {
  11905. 55,86,227,24,145,55,102,120,144,3,63,94,228,54,100,137,186,50,11,164,109,
  11906. 77,215,5,61,35,106,206,149,110,4,254,219,237,58,8,196,24,103,74,183,2,127,
  11907. 103,246,93,4,98,12,47,180,67,103,246,127,101,208,70,32,194,186,134,207,236,
  11908. 254,203,160,140,65,133,246,136,108,254,199,237,186,8,196,24,87,80,217,253,
  11909. 143,219,116,17,136,49,30,209,13,159,220,116,75,3,30,65,244,17,136,48,174,
  11910. 209,13,159,220,116,17,136,48,158,161,179,251,142,130,49,6,17,209,130,96,
  11911. 237,80,75,47,160,140,65,142,134,133,41,34,110,134,133,41,34,3,25,110,8,22,
  11912. 158,130,38,163,8,217,200,158,76,156,210,117,128,153,203,210,70,46,137,187,
  11913. 18,27,164,187,201,209,130,100,55,91,70,4,145,63,66,231,44,128,105,187,41,
  11914. 197,13,49,122,8,196,24,71,75,70,138,104,115,77,215,5,36,20,201,214,209,107,
  11915. 79,104,209,144,168,105,6,207,251,209,104,209,125,212,227,66,127,235,191,
  11916. 239,232,180,90,52,95,69,247,83,141,9,255,174,255,191,162,211,80,210,253,23,
  11917. 221,78,52,39,254,183,254,254,139,72,105,126,139,238,167,26,19,255,91,255,
  11918. 127,69,166,129,191,69,247,83,141,9,255,175,255,191,162,213,26,50,23,232,
  11919. 190,234,113,161,63,245,115,119,86,227,118,83,138,26,98,9,110,48,86,22,148,
  11920. 160,152,22,82,70,46,137,44,8,180,163,32,104,98,206,32,17,7,16,88,101,100,
  11921. 206,42,70,36,108,205,18,74,140,33,196,230,60,2,152,146,33,38,230,8,36,79,
  11922. 182,251,65,156,151,24,200,33,145,162,25,80,209,24,67,0,166,68,52,174,61,73,
  11923. 25,33,205,25,27,84,177,195,234,220,1,144,105,99,135,217,16,17,17,208,72,
  11924. 199,179,60,93,100,146,49,232,162,64,76,135,19,152,244,44,136,223,98,67,4,
  11925. 18,33,247,217,158,36,0,209,190,156,13,26,201,21,111,165,67,64,180,100,145,
  11926. 62,250,32,45,100,33,55,214,1,229,223,65,19,72,187,236,206,137,35,125,120,
  11927. 190,201,104,105,15,190,201,212,136,136,125,246,160,137,27,83,239,171,37,
  11928. 200,218,159,125,168,34,192,61,27,233,93,22,1,114,78,250,28,76,130,112,200,
  11929. 93,245,164,188,207,190,204,17,49,38,109,246,160,93,8,119,185,13,153,34,173,
  11930. 246,113,0,136,48,76,10,90,26,78,182,140,9,34,130,161,100,235,64,194,9,226,
  11931. 44,166,1,41,221,153,226,235,118,120,121,58,72,197,209,63,71,69,76,15,34,
  11932. 164,73,244,171,112,39,246,223,104,169,18,125,42,220,9,253,159,217,38,68,
  11933. 159,104,134,207,236,254,201,18,36,250,134,207,236,254,201,50,36,251,68,54,
  11934. 127,99,246,200,145,39,212,54,127,99,246,200,145,39,218,33,179,251,131,200,
  11935. 147,234,27,63,184,81,137,62,149,110,4,254,219,237,20,98,79,165,91,129,63,
  11936. 179,251,36,152,147,237,16,217,253,159,217,32,196,159,80,217,253,159,217,36,
  11937. 196,159,104,134,207,236,126,217,6,36,250,134,207,236,126,217,6,36,251,68,
  11938. 54,127,112,115,18,125,67,103,247,8,149,2,8,196,24,143,131,137,146,90,121,
  11939. 35,162,44,140,35,102,160,226,100,235,138,89,18,102,13,10,82,68,200,151,106,
  11940. 130,88,131,4,192,73,225,228,85,162,137,147,168,108,252,18,42,209,68,201,
  11941. 212,54,126,89,23,104,162,100,245,17,230,207,193,34,237,20,76,158,162,60,
  11942. 217,249,100,109,162,137,147,163,117,2,178,120,36,109,162,137,147,163,117,2,
  11943. 178,121,100,101,162,137,147,165,91,129,63,4,140,180,81,50,116,171,112,39,
  11944. 229,145,150,138,38,78,161,179,251,63,178,240,72,203,69,19,39,80,217,253,
  11945. 159,217,121,100,109,162,137,147,212,71,155,63,179,251,47,4,141,180,81,50,
  11946. 122,136,243,103,246,127,101,229,145,150,138,38,78,161,179,251,31,182,240,
  11947. 72,203,69,19,39,80,217,253,143,219,121,100,109,162,137,147,212,71,155,63,
  11948. 177,251,111,4,141,180,81,50,122,136,243,103,246,63,109,229,145,54,138,38,
  11949. 78,161,179,251,133,90,40,153,61,68,121,179,251,132,196,128,31,80,217,248,
  11950. 36,76,72,1,245,13,159,150,69,68,128,31,168,143,54,126,9,21,18,0,126,162,60,
  11951. 217,249,100,100,72,1,244,110,160,86,79,4,140,137,0,62,141,212,10,201,229,
  11952. 145,113,32,7,210,173,192,159,130,69,196,128,31,74,183,2,126,89,23,18,0,125,
  11953. 67,103,246,127,101,224,145,113,32,7,212,54,127,103,246,94,89,25,18,0,126,
  11954. 162,60,217,253,159,217,120,36,100,72,1,250,136,243,103,246,127,101,229,145,
  11955. 113,32,7,212,54,127,99,246,222,9,23,18,0,125,67,103,246,63,109,229,145,145,
  11956. 32,7,234,35,205,159,216,253,183,130,70,68,128,31,168,143,54,127,99,246,222,
  11957. 89,17,18,0,125,67,103,247,9,137,0,63,81,30,108,254,224,130,115,240,98,66,
  11958. 128,92,136,84,45,101,180,81,50,28,78,99,193,18,40,56,153,58,178,52,211,58,
  11959. 17,46,134,133,41,34,164,75,164,104,156,52,52,199,37,222,232,206,66,64,207,
  11960. 18,66,136,137,19,173,62,46,155,181,167,72,147,235,226,233,186,120,121,58,
  11961. 226,157,214,111,84,76,73,36,109,24,72,130,100,112,200,178,76,157,124,92,
  11962. 242,70,120,25,193,34,245,241,117,240,97,1,107,33,25,212,54,160,90,7,244,29,
  11963. 24,38,66,254,223,215,125,119,215,126,232,190,43,226,67,244,1,250,193,125,
  11964. 111,216,11,234,254,192,63,96,159,173,234,26,84,53,19,194,126,175,168,105,
  11965. 80,212,79,8,234,26,84,53,19,193,156,20,144,83,52,167,20,52,198,109,24,18,
  11966. 68,225,115,150,64,53,52,104,200,84,52,131,76,167,20,52,200,46,7,48,52,146,
  11967. 132,102,57,33,165,139,168,209,154,32,104,220,193,189,214,27,16,209,176,23,
  11968. 26,220,98,149,110,116,70,75,188,98,116,136,34,33,101,4,192,223,178,32,38,6,
  11969. 144,18,67,72,1,58,67,0,100,95,74,17,159,217,31,210,132,103,246,58,251,33,
  11970. 121,232,55,150,227,125,143,216,16,190,91,141,246,68,31,150,223,178,39,150,
  11971. 223,177,251,0,244,135,97,37,32,24,132,104,24,66,161,175,164,202,134,140,
  11972. 151,39,212,125,255,221,125,74,86,9,79,168,104,201,116,178,139,154,22,134,
  11973. 145,72,51,93,18,116,64,145,13,39,82,34,33,38,73,76,132,185,4,185,187,198,
  11974. 100,229,233,197,13,49,228,73,247,4,4,78,98,79,184,32,34,105,187,201,147,
  11975. 154,185,187,200,147,165,233,197,13,50,230,239,82,98,151,167,20,52,206,145,
  11976. 39,234,76,69,245,22,190,224,128,138,228,73,244,180,90,251,130,2,43,145,39,
  11977. 234,76,76,243,155,51,162,68,159,88,230,204,234,145,39,234,76,67,240,38,67,
  11978. 200,147,232,193,50,46,68,159,169,49,31,206,164,100,137,18,125,59,169,25,54,
  11979. 68,159,169,49,51,200,109,38,73,42,68,159,88,134,210,100,147,100,73,250,147,
  11980. 20,188,65,57,163,146,164,73,246,68,19,154,57,74,68,159,169,49,51,200,90,
  11981. 209,34,9,205,28,159,34,79,172,66,214,137,16,78,104,228,121,18,125,154,24,
  11982. 72,152,147,236,208,194,101,205,39,92,82,200,147,145,137,63,82,98,103,144,
  11983. 181,162,68,19,154,57,60,196,159,88,133,173,18,32,156,209,201,166,36,253,73,
  11984. 138,94,32,156,209,201,70,36,251,34,9,205,28,154,98,79,212,152,153,228,54,
  11985. 147,36,148,98,79,172,67,105,50,73,102,36,253,73,136,254,117,35,36,24,147,
  11986. 233,221,72,201,38,36,253,73,136,126,6,12,98,79,163,6,20,98,79,212,152,135,
  11987. 224,76,135,49,39,209,130,100,89,137,63,82,98,103,156,217,157,6,36,250,199,
  11988. 54,103,113,137,63,82,98,47,168,181,247,4,4,86,98,79,165,162,215,220,16,17,
  11989. 57,137,62,205,12,36,166,238,173,194,2,201,217,161,132,236,167,20,52,210,
  11990. 155,186,183,8,11,39,70,9,147,178,156,80,211,50,110,236,208,194,118,83,138,
  11991. 26,102,77,221,24,38,78,202,113,67,76,54,186,195,245,38,34,188,17,145,23,55,
  11992. 117,241,32,145,36,57,173,155,186,75,189,205,35,102,128,44,243,119,74,139,
  11993. 144,113,243,221,36,77,21,38,144,210,161,168,158,35,230,144,192,154,42,77,
  11994. 33,165,67,81,60,15,173,7,90,159,49,13,213,64,186,17,62,96,47,170,129,116,
  11995. 33,165,64,202,113,36,226,134,70,110,234,220,32,44,157,163,222,72,244,64,
  11996. 145,23,55,118,143,121,35,209,2,68,140,221,213,184,64,89,58,183,88,145,232,
  11997. 129,34,46,110,234,221,98,71,162,4,136,153,80,50,156,80,211,22,79,90,38,105,
  11998. 16,17,17,207,18,61,96,17,10,192,76,71,106,220,32,44,157,19,152,240,68,138,
  11999. 17,193,30,137,195,39,65,51,8,224,143,65,54,22,46,103,68,112,71,162,112,200,
  12000. 184,144,116,17,59,20,24,243,52,72,58,8,134,42,23,50,68,108,3,206,87,71,164,
  12001. 0,142,73,57,132,41,42,72,225,107,4,167,212,52,100,191,92,83,161,163,37,250,
  12002. 226,158,141,145,208,89,154,79,90,4,66,73,209,153,100,180,8,133,145,208,89,
  12003. 158,36,169,35,34,17,244,145,198,247,60,137,114,26,97,57,162,4,206,137,116,
  12004. 17,136,48,144,68,212,97,27,57,24,64,90,201,18,5,13,25,4,5,172,160,123,215,
  12005. 138,62,46,121,35,60,117,18,233,27,70,18,32,10,200,212,75,175,139,166,233,
  12006. 225,228,235,138,227,130,93,117,155,215,197,207,36,103,131,212,11,161,58,
  12007. 226,186,110,234,220,32,44,157,148,226,134,153,19,119,101,56,161,166,88,156,
  12008. 217,78,52,20,221,17,200,147,25,137,53,17,180,97,34,0,172,140,19,154,84,26,
  12009. 145,0,86,68,90,40,152,2,178,22,160,93,8,69,19,18,98,37,210,94,100,108,144,
  12010. 21,145,8,151,75,23,100,141,66,37,217,16,11,32,226,248,146,164,108,250,75,
  12011. 204,141,146,28,217,24,177,33,50,66,72,128,92,6,66,161,164,235,226,231,146,
  12012. 51,65,36,225,144,168,105,58,248,185,228,140,240,97,68,128,153,38,98,79,174,
  12013. 179,122,248,185,228,140,241,214,129,132,150,12,73,245,214,111,95,23,60,145,
  12014. 158,58,50,72,81,67,230,232,184,196,159,95,23,77,211,195,201,215,21,47,139,
  12015. 166,233,225,228,50,200,211,76,229,2,201,25,149,241,67,102,138,52,146,16,30,
  12016. 67,18,66,3,201,34,52,78,25,61,72,160,94,115,30,230,145,179,73,26,39,12,158,
  12017. 164,81,33,144,78,25,61,72,160,94,115,30,230,145,179,72,200,39,12,158,164,
  12018. 80,132,75,165,67,81,50,21,18,235,65,214,169,224,140,137,210,173,192,154,30,
  12019. 8,200,157,67,102,66,84,11,71,169,20,19,209,139,162,158,207,15,39,73,24,186,
  12020. 43,236,176,217,130,253,36,98,232,187,177,33,73,18,52,68,233,35,23,69,93,
  12021. 136,26,98,116,145,139,162,158,146,160,95,73,24,186,37,12,72,5,16,64,145,10,
  12022. 32,76,71,64,156,217,161,180,34,6,64,208,198,36,78,50,20,20,92,204,50,44,
  12023. 147,32,134,226,17,114,33,202,134,129,107,192,202,232,160,180,104,166,135,
  12024. 52,72,40,144,213,33,178,152,26,34,56,163,105,44,104,146,116,139,77,43,34,
  12025. 98,57,38,116,72,179,60,93,97,206,56,52,240,242,56,163,168,34,81,57,178,153,
  12026. 42,228,12,182,58,22,66,89,19,57,68,176,74,68,35,104,195,18,239,116,102,114,
  12027. 94,100,104,228,100,49,238,140,203,42,60,145,35,104,181,146,113,161,10,80,
  12028. 46,68,82,24,245,145,132,108,228,148,54,100,137,64,34,13,100,153,222,1,40,6,
  12029. 33,223,20,84,19,34,95,23,76,130,153,6,103,208,43,64,141,41,130,104,17,112,
  12030. 130,44,96,
  12031. };
  12032. /* to convert a heap stridx to a token number, subtract
  12033. * DUK_STRIDX_START_RESERVED and add DUK_TOK_START_RESERVED.
  12034. */
  12035. /* native functions: 147 */
  12036. DUK_INTERNAL const duk_c_function duk_bi_native_functions[147] = {
  12037. duk_bi_array_constructor,
  12038. duk_bi_array_constructor_is_array,
  12039. duk_bi_array_prototype_concat,
  12040. duk_bi_array_prototype_indexof_shared,
  12041. duk_bi_array_prototype_iter_shared,
  12042. duk_bi_array_prototype_join_shared,
  12043. duk_bi_array_prototype_pop,
  12044. duk_bi_array_prototype_push,
  12045. duk_bi_array_prototype_reduce_shared,
  12046. duk_bi_array_prototype_reverse,
  12047. duk_bi_array_prototype_shift,
  12048. duk_bi_array_prototype_slice,
  12049. duk_bi_array_prototype_sort,
  12050. duk_bi_array_prototype_splice,
  12051. duk_bi_array_prototype_to_string,
  12052. duk_bi_array_prototype_unshift,
  12053. duk_bi_arraybuffer_constructor,
  12054. duk_bi_arraybuffer_isview,
  12055. duk_bi_boolean_constructor,
  12056. duk_bi_boolean_prototype_tostring_shared,
  12057. duk_bi_buffer_compare_shared,
  12058. duk_bi_buffer_constructor,
  12059. duk_bi_buffer_prototype_tostring_shared,
  12060. duk_bi_buffer_readfield,
  12061. duk_bi_buffer_slice_shared,
  12062. duk_bi_buffer_writefield,
  12063. duk_bi_dataview_constructor,
  12064. duk_bi_date_constructor,
  12065. duk_bi_date_constructor_now,
  12066. duk_bi_date_constructor_parse,
  12067. duk_bi_date_constructor_utc,
  12068. duk_bi_date_prototype_get_shared,
  12069. duk_bi_date_prototype_get_timezone_offset,
  12070. duk_bi_date_prototype_set_shared,
  12071. duk_bi_date_prototype_set_time,
  12072. duk_bi_date_prototype_to_json,
  12073. duk_bi_date_prototype_tostring_shared,
  12074. duk_bi_date_prototype_value_of,
  12075. duk_bi_duktape_object_act,
  12076. duk_bi_duktape_object_compact,
  12077. duk_bi_duktape_object_dec,
  12078. duk_bi_duktape_object_enc,
  12079. duk_bi_duktape_object_fin,
  12080. duk_bi_duktape_object_gc,
  12081. duk_bi_duktape_object_info,
  12082. duk_bi_error_constructor_shared,
  12083. duk_bi_error_prototype_filename_getter,
  12084. duk_bi_error_prototype_linenumber_getter,
  12085. duk_bi_error_prototype_nop_setter,
  12086. duk_bi_error_prototype_stack_getter,
  12087. duk_bi_error_prototype_to_string,
  12088. duk_bi_function_constructor,
  12089. duk_bi_function_prototype,
  12090. duk_bi_function_prototype_apply,
  12091. duk_bi_function_prototype_bind,
  12092. duk_bi_function_prototype_call,
  12093. duk_bi_function_prototype_to_string,
  12094. duk_bi_global_object_decode_uri,
  12095. duk_bi_global_object_decode_uri_component,
  12096. duk_bi_global_object_encode_uri,
  12097. duk_bi_global_object_encode_uri_component,
  12098. duk_bi_global_object_escape,
  12099. duk_bi_global_object_eval,
  12100. duk_bi_global_object_is_finite,
  12101. duk_bi_global_object_is_nan,
  12102. duk_bi_global_object_parse_float,
  12103. duk_bi_global_object_parse_int,
  12104. duk_bi_global_object_print_helper,
  12105. duk_bi_global_object_require,
  12106. duk_bi_global_object_unescape,
  12107. duk_bi_json_object_parse,
  12108. duk_bi_json_object_stringify,
  12109. duk_bi_logger_constructor,
  12110. duk_bi_logger_prototype_fmt,
  12111. duk_bi_logger_prototype_log_shared,
  12112. duk_bi_logger_prototype_raw,
  12113. duk_bi_math_object_max,
  12114. duk_bi_math_object_min,
  12115. duk_bi_math_object_onearg_shared,
  12116. duk_bi_math_object_random,
  12117. duk_bi_math_object_twoarg_shared,
  12118. duk_bi_nodejs_buffer_byte_length,
  12119. duk_bi_nodejs_buffer_concat,
  12120. duk_bi_nodejs_buffer_constructor,
  12121. duk_bi_nodejs_buffer_copy,
  12122. duk_bi_nodejs_buffer_fill,
  12123. duk_bi_nodejs_buffer_is_buffer,
  12124. duk_bi_nodejs_buffer_is_encoding,
  12125. duk_bi_nodejs_buffer_tojson,
  12126. duk_bi_nodejs_buffer_tostring,
  12127. duk_bi_nodejs_buffer_write,
  12128. duk_bi_number_constructor,
  12129. duk_bi_number_prototype_to_exponential,
  12130. duk_bi_number_prototype_to_fixed,
  12131. duk_bi_number_prototype_to_locale_string,
  12132. duk_bi_number_prototype_to_precision,
  12133. duk_bi_number_prototype_to_string,
  12134. duk_bi_number_prototype_value_of,
  12135. duk_bi_object_constructor,
  12136. duk_bi_object_constructor_create,
  12137. duk_bi_object_constructor_define_properties,
  12138. duk_bi_object_constructor_define_property,
  12139. duk_bi_object_constructor_get_own_property_descriptor,
  12140. duk_bi_object_constructor_is_extensible,
  12141. duk_bi_object_constructor_is_sealed_frozen_shared,
  12142. duk_bi_object_constructor_keys_shared,
  12143. duk_bi_object_constructor_prevent_extensions,
  12144. duk_bi_object_constructor_seal_freeze_shared,
  12145. duk_bi_object_getprototype_shared,
  12146. duk_bi_object_prototype_has_own_property,
  12147. duk_bi_object_prototype_is_prototype_of,
  12148. duk_bi_object_prototype_property_is_enumerable,
  12149. duk_bi_object_prototype_to_locale_string,
  12150. duk_bi_object_prototype_to_string,
  12151. duk_bi_object_prototype_value_of,
  12152. duk_bi_object_setprototype_shared,
  12153. duk_bi_pointer_constructor,
  12154. duk_bi_pointer_prototype_tostring_shared,
  12155. duk_bi_proxy_constructor,
  12156. duk_bi_regexp_constructor,
  12157. duk_bi_regexp_prototype_exec,
  12158. duk_bi_regexp_prototype_test,
  12159. duk_bi_regexp_prototype_to_string,
  12160. duk_bi_string_constructor,
  12161. duk_bi_string_constructor_from_char_code,
  12162. duk_bi_string_prototype_caseconv_shared,
  12163. duk_bi_string_prototype_char_at,
  12164. duk_bi_string_prototype_char_code_at,
  12165. duk_bi_string_prototype_concat,
  12166. duk_bi_string_prototype_indexof_shared,
  12167. duk_bi_string_prototype_locale_compare,
  12168. duk_bi_string_prototype_match,
  12169. duk_bi_string_prototype_replace,
  12170. duk_bi_string_prototype_search,
  12171. duk_bi_string_prototype_slice,
  12172. duk_bi_string_prototype_split,
  12173. duk_bi_string_prototype_substr,
  12174. duk_bi_string_prototype_substring,
  12175. duk_bi_string_prototype_to_string,
  12176. duk_bi_string_prototype_trim,
  12177. duk_bi_thread_constructor,
  12178. duk_bi_thread_current,
  12179. duk_bi_thread_resume,
  12180. duk_bi_thread_yield,
  12181. duk_bi_type_error_thrower,
  12182. duk_bi_typedarray_constructor,
  12183. duk_bi_typedarray_set,
  12184. };
  12185. DUK_INTERNAL const duk_uint8_t duk_builtins_data[1952] = {
  12186. 105,195,75,16,121,40,105,51,14,252,104,52,8,131,72,0,115,225,65,165,236,55,
  12187. 243,6,145,32,210,24,210,182,25,249,35,120,216,99,226,13,78,225,116,177,164,
  12188. 180,44,192,4,202,52,150,220,24,0,169,70,146,219,123,0,23,40,210,91,110,96,
  12189. 3,37,26,75,109,172,0,108,163,73,109,177,128,14,148,105,45,181,176,1,242,
  12190. 144,56,209,32,94,6,167,101,98,80,211,24,1,250,67,72,168,67,232,13,46,128,
  12191. 47,162,52,164,0,62,80,163,72,128,61,40,107,26,7,37,20,53,200,131,88,0,66,
  12192. 134,185,16,98,80,215,34,11,96,0,138,26,228,65,76,0,69,67,92,136,37,128,6,
  12193. 168,107,145,4,48,1,165,13,114,32,118,0,44,161,174,68,12,192,7,148,53,200,
  12194. 129,88,1,26,134,165,48,130,80,31,255,241,69,224,0,0,124,63,128,0,0,0,46,32,
  12195. 0,0,120,63,128,0,0,0,47,98,7,140,16,116,194,7,12,48,108,196,6,140,80,100,
  12196. 198,6,12,112,92,200,5,140,149,192,202,91,204,181,184,204,91,76,213,176,206,
  12197. 90,204,240,84,208,5,13,9,124,210,43,13,24,64,226,131,205,112,56,216,3,77,
  12198. 152,48,218,130,205,184,40,220,130,77,216,32,222,129,205,248,24,224,129,78,
  12199. 25,214,163,226,90,80,145,104,65,37,157,0,150,99,242,89,78,73,100,58,37,140,
  12200. 236,150,35,194,88,79,73,96,69,37,125,12,122,188,134,62,0,2,165,68,39,255,
  12201. 255,193,43,67,0,0,80,127,192,58,182,216,80,0,21,59,154,64,0,107,76,200,172,
  12202. 180,146,176,198,138,187,43,42,204,136,170,181,146,168,214,80,0,26,155,81,
  12203. 42,77,4,168,180,20,0,6,160,206,74,123,73,64,0,127,255,4,10,153,219,28,198,
  12204. 163,184,130,140,224,10,43,144,40,141,164,161,183,18,132,222,64,161,127,128,
  12205. 0,63,225,1,109,74,8,137,71,56,5,4,213,20,3,115,233,249,177,240,80,255,192,
  12206. 6,120,2,64,127,195,0,173,28,56,20,96,80,128,0,206,192,143,167,64,164,156,
  12207. 131,2,112,14,125,55,9,4,216,40,19,80,180,77,3,9,51,13,94,153,7,159,76,64,
  12208. 207,192,0,102,0,103,255,255,242,240,67,73,112,33,168,0,12,180,16,212,0,10,
  12209. 88,8,106,0,7,43,4,53,0,4,149,4,31,128,0,202,66,15,255,255,194,137,254,0,50,
  12210. 135,195,224,127,196,2,87,132,17,82,143,20,10,44,80,36,239,196,147,63,146,
  12211. 119,0,125,49,129,52,152,64,154,128,0,201,96,137,36,131,36,142,17,18,40,82,
  12212. 77,97,145,33,135,68,130,37,17,247,208,71,159,65,29,125,8,0,12,113,244,32,0,
  12213. 49,184,176,70,162,16,20,95,240,0,7,252,80,37,120,193,81,196,194,0,3,69,19,
  12214. 0,81,191,197,140,192,255,255,239,127,255,255,255,255,140,64,0,0,0,0,1,0,0,
  12215. 0,139,192,0,0,248,127,0,0,0,0,138,192,0,0,240,127,0,0,0,0,139,64,0,0,240,
  12216. 255,0,0,0,0,0,31,241,128,149,224,0,0,0,0,0,0,0,0,13,71,96,37,25,120,148,86,
  12217. 16,69,23,73,19,92,36,73,124,129,71,255,0,56,136,233,34,3,223,208,241,192,3,
  12218. 254,56,18,188,128,0,15,135,240,0,0,0,11,104,228,128,135,18,4,0,6,26,72,16,
  12219. 0,42,49,32,64,0,225,132,129,0,4,133,146,4,0,21,210,72,16,0,103,65,32,64,1,
  12220. 220,228,100,162,146,130,20,74,8,72,248,64,2,33,3,225,0,9,131,143,132,0,42,
  12221. 12,62,16,0,184,40,248,64,3,32,131,225,0,13,129,143,132,0,58,4,62,16,0,248,
  12222. 8,248,64,4,32,3,225,0,17,127,143,132,0,73,252,62,16,1,55,232,248,64,5,31,
  12223. 131,225,0,21,125,143,132,0,89,244,62,16,1,119,201,0,31,4,68,123,144,148,0,
  12224. 97,236,66,80,1,151,169,10,248,0,211,208,133,124,0,109,230,66,254,0,56,242,
  12225. 33,127,0,29,120,144,207,128,15,60,8,103,192,7,221,228,37,0,32,119,16,148,0,
  12226. 133,218,66,190,0,68,236,33,95,0,35,117,144,191,128,18,58,136,95,192,9,92,
  12227. 195,225,0,38,114,144,148,0,156,41,31,224,0,15,249,1,138,144,64,192,2,2,225,
  12228. 132,221,9,70,112,70,111,198,111,72,0,0,0,0,0,0,0,0,13,198,240,71,19,201,40,
  12229. 239,64,10,79,248,0,3,254,72,86,209,5,155,36,17,46,185,137,129,109,203,140,
  12230. 11,78,94,96,13,28,200,1,74,255,0,2,127,202,4,218,43,131,100,130,32,5,47,
  12231. 252,0,9,255,44,19,104,173,237,146,8,128,20,207,240,0,39,252,192,77,162,183,
  12232. 54,72,34,0,83,127,192,0,159,243,65,54,138,218,217,32,136,1,78,255,0,2,127,
  12233. 206,4,218,43,99,100,130,32,5,63,252,0,9,255,60,19,104,173,109,146,8,128,15,
  12234. 255,242,27,16,2,175,193,80,26,85,197,34,218,240,44,90,192,144,5,149,109,
  12235. 110,218,208,16,139,185,143,251,206,126,191,154,176,17,197,125,207,255,160,
  12236. 138,217,90,144,30,242,246,207,195,185,73,133,90,112,62,200,66,80,6,11,81,
  12237. 21,26,80,39,168,57,143,243,78,223,217,154,48,39,168,61,143,243,78,223,217,
  12238. 146,104,39,17,158,156,80,0,22,114,113,64,0,153,169,197,0,3,102,40,33,150,
  12239. 156,80,0,70,82,113,64,1,89,41,197,0,6,100,39,20,0,29,142,156,80,0,134,50,
  12240. 98,243,21,53,121,136,160,144,0,22,26,120,24,73,197,0,9,96,167,20,0,41,128,
  12241. 156,80,0,181,250,113,64,3,1,255,254,0,81,20,100,47,145,216,23,255,240,0,11,
  12242. 255,248,0,3,255,252,81,252,8,4,252,68,0,0,0,0,0,129,167,1,26,144,9,165,0,
  12243. 26,177,199,197,132,30,147,16,120,86,65,217,80,240,232,164,120,114,80,60,52,
  12244. 39,32,84,223,192,15,59,30,129,156,115,6,81,160,7,253,40,0,5,81,252,0,1,255,
  12245. 78,0,84,113,96,128,0,209,69,128,21,87,240,0,7,253,72,1,81,221,66,0,3,69,
  12246. 117,0,85,159,192,0,31,245,97,10,100,0,0,0,32,0,0,0,0,10,164,130,97,221,191,
  12247. 113,3,20,146,12,18,200,47,74,30,23,37,15,128,0,143,146,135,192,0,133,169,
  12248. 67,224,0,98,196,161,240,0,65,90,80,248,0,41,63,255,194,109,65,11,137,191,
  12249. 174,45,153,98,242,229,191,147,102,8,190,94,92,183,242,65,167,114,12,188,
  12250. 185,111,228,131,70,29,217,54,105,221,156,0,171,255,128,9,208,68,128,255,
  12251. 174,0,25,168,194,64,0,130,177,254,0,0,255,176,1,3,120,186,64,12,13,194,233,
  12252. 0,32,54,139,164,0,196,216,46,144,2,19,88,186,64,12,141,66,233,0,34,52,139,
  12253. 164,0,140,208,46,144,2,67,56,203,64,12,12,195,45,0,32,50,140,180,0,196,200,
  12254. 50,208,2,19,24,203,64,12,140,67,45,0,34,48,140,180,0,140,192,50,208,2,64,
  12255. 127,255,128,21,38,73,7,1,132,128,0,133,105,252,19,140,0,0,15,3,240,0,0,0,0,
  12256. 25,127,102,0,1,91,127,4,227,0,0,3,192,252,0,0,0,0,6,95,218,128,0,87,31,193,
  12257. 56,192,0,0,240,63,0,0,0,0,1,151,246,224,0,21,215,240,78,48,0,0,0,16,0,0,0,
  12258. 0,0,101,253,200,0,5,121,252,19,140,0,0,0,4,0,0,0,0,0,25,127,118,0,1,95,127,
  12259. 4,227,0,0,0,65,0,0,0,0,0,6,95,222,128,0,88,31,193,56,192,0,0,16,64,0,0,0,0,
  12260. 1,151,247,224,0,22,23,240,78,48,0,0,4,16,0,0,0,0,0,101,254,8,0,5,137,252,
  12261. 19,140,0,0,2,4,0,0,0,0,0,25,127,134,0,1,99,127,0,89,218,146,20,74,228,80,
  12262. 171,17,64,162,132,248,162,64,0,193,255,138,5,137,161,116,38,69,210,0,32,
  12263. 152,23,72,0,10,92,93,32,1,41,97,116,128,8,165,69,210,0,50,148,23,72,0,18,
  12264. 76,93,32,1,73,33,116,128,9,36,69,210,0,52,144,23,72,0,26,60,93,32,1,104,
  12265. 225,116,128,2,35,69,210,0,24,140,23,104,0,42,44,93,160,1,168,161,118,128,
  12266. 10,162,69,218,0,58,136,25,98,28,101,160,2,8,97,150,128,0,161,70,90,0,18,
  12267. 132,25,104,0,138,12,101,160,3,40,33,150,128,1,32,70,90,0,20,128,25,104,0,
  12268. 145,252,101,160,3,71,225,150,128,1,159,70,90,0,22,124,25,104,0,33,236,101,
  12269. 160,1,135,161,152,128,2,158,70,98,0,26,120,25,136,0,169,220,102,32,3,180,
  12270. 117,150,57,214,0,157,85,98,112,80,137,241,66,128,0,166,213,33,53,24,66,121,
  12271. 106,0,
  12272. };
  12273. #ifdef DUK_USE_BUILTIN_INITJS
  12274. DUK_INTERNAL const duk_uint8_t duk_initjs_data[1757] = {
  12275. 47,42,10,32,42,32,32,73,110,105,116,32,99,111,100,101,32,102,111,114,32,
  12276. 108,101,103,97,99,121,32,99,111,109,112,97,116,105,98,105,108,105,116,121,
  12277. 46,10,32,42,10,32,42,32,32,67,111,109,112,97,116,105,98,105,108,105,116,
  12278. 121,32,112,114,111,112,101,114,116,105,101,115,32,47,32,119,114,97,112,112,
  12279. 101,114,32,102,117,110,99,116,105,111,110,115,32,104,101,114,101,32,97,108,
  12280. 108,111,119,32,68,117,107,116,97,112,101,32,116,111,32,114,101,109,97,105,
  12281. 110,10,32,42,32,32,99,111,109,112,97,116,105,98,108,101,32,102,111,114,32,
  12282. 117,115,101,114,32,99,111,100,101,32,119,104,101,110,32,99,111,114,101,32,
  12283. 102,101,97,116,117,114,101,115,32,97,114,101,32,99,104,97,110,103,101,100,
  12284. 44,32,119,105,116,104,111,117,116,32,98,117,114,100,101,110,105,110,103,10,
  12285. 32,42,32,32,116,104,101,32,109,97,105,110,32,67,32,99,111,100,101,32,119,
  12286. 105,116,104,32,99,111,109,112,97,116,105,98,105,108,105,116,121,32,115,116,
  12287. 117,102,102,46,10,32,42,10,32,42,32,32,84,104,105,115,32,102,105,108,101,
  12288. 32,105,115,32,109,105,110,105,102,105,101,100,32,119,105,116,104,32,85,103,
  12289. 108,105,102,121,74,83,32,111,114,32,116,104,101,32,99,108,111,115,117,114,
  12290. 101,32,99,111,109,112,105,108,101,114,46,32,32,66,111,116,104,32,119,105,
  12291. 108,108,10,32,42,32,32,114,101,110,97,109,101,32,118,97,114,105,97,98,108,
  12292. 101,115,44,32,114,101,109,111,118,101,32,99,111,109,109,101,110,116,115,44,
  12293. 32,97,110,100,32,97,114,101,32,99,108,101,118,101,114,32,101,110,111,117,
  12294. 103,104,32,116,111,32,100,114,111,112,32,97,110,121,10,32,42,32,32,34,105,
  12295. 102,32,40,102,97,108,115,101,41,32,123,32,46,46,46,32,125,34,32,98,108,111,
  12296. 99,107,115,32,97,108,116,111,103,101,116,104,101,114,44,32,115,111,32,116,
  12297. 104,97,116,39,115,32,97,110,32,101,102,102,101,99,116,105,118,101,32,119,
  12298. 97,121,32,116,111,10,32,42,32,32,100,105,115,97,98,108,101,32,99,117,114,
  12299. 114,101,110,116,108,121,32,117,110,110,101,101,100,101,100,32,99,111,100,
  12300. 101,46,10,32,42,47,10,10,40,102,117,110,99,116,105,111,110,40,71,44,32,68,
  12301. 41,32,123,10,32,32,32,32,39,117,115,101,32,115,116,114,105,99,116,39,59,10,
  12302. 10,32,32,32,32,102,117,110,99,116,105,111,110,32,100,101,102,40,111,98,106,
  12303. 101,99,116,44,32,110,97,109,101,44,32,118,97,108,117,101,41,32,123,10,32,
  12304. 32,32,32,32,32,32,32,79,98,106,101,99,116,46,100,101,102,105,110,101,80,
  12305. 114,111,112,101,114,116,121,40,111,98,106,101,99,116,44,32,110,97,109,101,
  12306. 44,32,123,10,32,32,32,32,32,32,32,32,32,32,32,32,118,97,108,117,101,58,32,
  12307. 118,97,108,117,101,44,10,32,32,32,32,32,32,32,32,32,32,32,32,119,114,105,
  12308. 116,97,98,108,101,58,32,116,114,117,101,44,10,32,32,32,32,32,32,32,32,32,
  12309. 32,32,32,101,110,117,109,101,114,97,98,108,101,58,32,102,97,108,115,101,44,
  12310. 10,32,32,32,32,32,32,32,32,32,32,32,32,99,111,110,102,105,103,117,114,97,
  12311. 98,108,101,58,32,116,114,117,101,10,32,32,32,32,32,32,32,32,125,41,59,10,
  12312. 32,32,32,32,125,10,10,32,32,32,32,102,117,110,99,116,105,111,110,32,100,
  12313. 101,102,68,40,110,97,109,101,44,32,118,97,108,117,101,41,32,123,10,32,32,
  12314. 32,32,32,32,32,32,100,101,102,40,68,44,32,110,97,109,101,44,32,118,97,108,
  12315. 117,101,41,59,10,32,32,32,32,125,10,10,32,32,32,32,47,47,32,67,111,109,112,
  12316. 97,116,105,98,105,108,105,116,121,32,102,111,114,32,39,99,111,110,115,111,
  12317. 108,101,46,108,111,103,39,46,10,32,32,32,32,105,102,32,40,102,97,108,115,
  12318. 101,41,32,123,10,32,32,32,32,32,32,32,32,99,111,110,115,111,108,101,32,61,
  12319. 32,123,10,32,32,32,32,32,32,32,32,32,32,32,32,108,111,103,58,32,102,117,
  12320. 110,99,116,105,111,110,40,41,32,123,10,32,32,32,32,32,32,32,32,32,32,32,32,
  12321. 32,32,32,32,112,114,105,110,116,40,65,114,114,97,121,46,112,114,111,116,
  12322. 111,116,121,112,101,46,106,111,105,110,46,99,97,108,108,40,97,114,103,117,
  12323. 109,101,110,116,115,44,32,39,32,39,41,41,59,10,32,32,32,32,32,32,32,32,32,
  12324. 32,32,32,125,10,32,32,32,32,32,32,32,32,125,59,10,32,32,32,32,125,10,10,32,
  12325. 32,32,32,47,47,32,68,117,107,116,97,112,101,46,108,105,110,101,40,41,32,
  12326. 119,97,115,32,114,101,109,111,118,101,100,32,105,110,32,68,117,107,116,97,
  12327. 112,101,32,48,46,49,49,46,48,44,32,104,101,114,101,39,115,32,97,110,32,101,
  12328. 120,97,109,112,108,101,10,32,32,32,32,47,47,32,114,101,112,108,97,99,101,
  12329. 109,101,110,116,32,117,115,101,114,32,99,111,100,101,32,99,111,117,108,100,
  12330. 32,117,115,101,46,10,32,32,32,32,105,102,32,40,102,97,108,115,101,41,32,
  12331. 123,10,32,32,32,32,32,32,32,32,100,101,102,40,68,44,32,39,108,105,110,101,
  12332. 39,44,32,102,117,110,99,116,105,111,110,32,40,41,32,123,10,32,32,32,32,32,
  12333. 32,32,32,32,32,32,32,39,117,115,101,32,100,117,107,32,110,111,116,97,105,
  12334. 108,39,59,10,10,32,32,32,32,32,32,32,32,32,32,32,32,47,42,32,84,97,105,108,
  12335. 32,99,97,108,108,115,32,97,114,101,32,112,114,101,118,101,110,116,101,100,
  12336. 32,116,111,32,101,110,115,117,114,101,32,99,97,108,108,105,110,103,32,97,
  12337. 99,116,105,118,97,116,105,111,110,32,101,120,105,115,116,115,46,10,32,32,
  12338. 32,32,32,32,32,32,32,32,32,32,32,42,32,67,97,108,108,32,115,116,97,99,107,
  12339. 32,105,110,100,105,99,101,115,58,32,45,49,32,61,32,68,117,107,116,97,112,
  12340. 101,46,97,99,116,44,32,45,50,32,61,32,103,101,116,67,117,114,114,101,110,
  12341. 116,76,105,110,101,44,32,45,51,32,61,32,99,97,108,108,101,114,10,32,32,32,
  12342. 32,32,32,32,32,32,32,32,32,32,42,47,10,10,32,32,32,32,32,32,32,32,32,32,32,
  12343. 32,114,101,116,117,114,110,32,40,68,117,107,116,97,112,101,46,97,99,116,40,
  12344. 45,51,41,32,124,124,32,123,125,41,46,108,105,110,101,78,117,109,98,101,114,
  12345. 59,10,32,32,32,32,32,32,32,32,125,41,59,10,32,32,32,32,125,10,10,32,32,32,
  12346. 32,47,47,32,76,111,103,103,101,114,32,111,98,106,101,99,116,32,102,111,114,
  12347. 32,67,32,99,111,100,101,32,112,114,111,118,105,100,101,100,32,98,121,32,
  12348. 105,110,105,116,32,99,111,100,101,32,110,111,119,46,10,32,32,32,32,105,102,
  12349. 32,40,116,114,117,101,41,32,123,10,32,32,32,32,32,32,32,32,100,101,102,40,
  12350. 68,46,76,111,103,103,101,114,44,32,39,99,108,111,103,39,44,32,110,101,119,
  12351. 32,68,46,76,111,103,103,101,114,40,39,67,39,41,41,59,10,32,32,32,32,125,10,
  12352. 10,32,32,32,32,47,47,32,84,114,97,99,107,105,110,103,32,116,97,98,108,101,
  12353. 32,102,111,114,32,67,111,109,109,111,110,74,83,32,109,111,100,117,108,101,
  12354. 32,108,111,97,100,105,110,103,46,10,32,32,32,32,105,102,32,40,116,114,117,
  12355. 101,41,32,123,10,32,32,32,32,32,32,32,32,100,101,102,40,68,44,32,39,109,
  12356. 111,100,76,111,97,100,101,100,39,44,32,123,125,41,59,10,32,32,32,32,125,10,
  12357. 125,41,40,116,104,105,115,44,32,68,117,107,116,97,112,101,41,59,10,0,
  12358. };
  12359. #endif /* DUK_USE_BUILTIN_INITJS */
  12360. #else
  12361. #error invalid endianness defines
  12362. #endif
  12363. #line 1 "duk_error_macros.c"
  12364. /*
  12365. * Error, fatal, and panic handling.
  12366. */
  12367. /* include removed: duk_internal.h */
  12368. #define DUK__ERRFMT_BUFSIZE 256 /* size for formatting buffers */
  12369. #ifdef DUK_USE_VERBOSE_ERRORS
  12370. #ifdef DUK_USE_VARIADIC_MACROS
  12371. DUK_INTERNAL void duk_err_handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
  12372. va_list ap;
  12373. char msg[DUK__ERRFMT_BUFSIZE];
  12374. va_start(ap, fmt);
  12375. (void) DUK_VSNPRINTF(msg, sizeof(msg), fmt, ap);
  12376. msg[sizeof(msg) - 1] = (char) 0;
  12377. duk_err_create_and_throw(thr, code, msg, filename, line);
  12378. va_end(ap); /* dead code, but ensures portability (see Linux man page notes) */
  12379. }
  12380. #else /* DUK_USE_VARIADIC_MACROS */
  12381. DUK_INTERNAL const char *duk_err_file_stash = NULL;
  12382. DUK_INTERNAL duk_int_t duk_err_line_stash = 0;
  12383. DUK_NORETURN(DUK_LOCAL_DECL void duk__handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, va_list ap));
  12384. DUK_LOCAL void duk__handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, va_list ap) {
  12385. char msg[DUK__ERRFMT_BUFSIZE];
  12386. (void) DUK_VSNPRINTF(msg, sizeof(msg), fmt, ap);
  12387. msg[sizeof(msg) - 1] = (char) 0;
  12388. duk_err_create_and_throw(thr, code, msg, filename, line);
  12389. }
  12390. DUK_INTERNAL void duk_err_handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
  12391. va_list ap;
  12392. va_start(ap, fmt);
  12393. duk__handle_error(filename, line, thr, code, fmt, ap);
  12394. va_end(ap); /* dead code */
  12395. }
  12396. DUK_INTERNAL void duk_err_handle_error_stash(duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
  12397. va_list ap;
  12398. va_start(ap, fmt);
  12399. duk__handle_error(duk_err_file_stash, duk_err_line_stash, thr, code, fmt, ap);
  12400. va_end(ap); /* dead code */
  12401. }
  12402. #endif /* DUK_USE_VARIADIC_MACROS */
  12403. #else /* DUK_USE_VERBOSE_ERRORS */
  12404. #ifdef DUK_USE_VARIADIC_MACROS
  12405. DUK_INTERNAL void duk_err_handle_error(duk_hthread *thr, duk_errcode_t code) {
  12406. duk_err_create_and_throw(thr, code);
  12407. }
  12408. #else /* DUK_USE_VARIADIC_MACROS */
  12409. DUK_INTERNAL void duk_err_handle_error_nonverbose1(duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
  12410. DUK_UNREF(fmt);
  12411. duk_err_create_and_throw(thr, code);
  12412. }
  12413. DUK_INTERNAL void duk_err_handle_error_nonverbose2(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
  12414. DUK_UNREF(filename);
  12415. DUK_UNREF(line);
  12416. DUK_UNREF(fmt);
  12417. duk_err_create_and_throw(thr, code);
  12418. }
  12419. #endif /* DUK_USE_VARIADIC_MACROS */
  12420. #endif /* DUK_USE_VERBOSE_ERRORS */
  12421. /*
  12422. * Default fatal error handler
  12423. */
  12424. DUK_INTERNAL void duk_default_fatal_handler(duk_context *ctx, duk_errcode_t code, const char *msg) {
  12425. DUK_UNREF(ctx);
  12426. #ifdef DUK_USE_FILE_IO
  12427. DUK_FPRINTF(DUK_STDERR, "FATAL %ld: %s\n", (long) code, (const char *) (msg ? msg : "null"));
  12428. DUK_FFLUSH(DUK_STDERR);
  12429. #else
  12430. /* omit print */
  12431. #endif
  12432. DUK_D(DUK_DPRINT("default fatal handler called, code %ld -> calling DUK_PANIC()", (long) code));
  12433. DUK_PANIC(code, msg);
  12434. DUK_UNREACHABLE();
  12435. }
  12436. /*
  12437. * Default panic handler
  12438. */
  12439. #if !defined(DUK_USE_PANIC_HANDLER)
  12440. DUK_INTERNAL void duk_default_panic_handler(duk_errcode_t code, const char *msg) {
  12441. #ifdef DUK_USE_FILE_IO
  12442. DUK_FPRINTF(DUK_STDERR, "PANIC %ld: %s ("
  12443. #if defined(DUK_USE_PANIC_ABORT)
  12444. "calling abort"
  12445. #elif defined(DUK_USE_PANIC_EXIT)
  12446. "calling exit"
  12447. #elif defined(DUK_USE_PANIC_SEGFAULT)
  12448. "segfaulting on purpose"
  12449. #else
  12450. #error no DUK_USE_PANIC_xxx macro defined
  12451. #endif
  12452. ")\n", (long) code, (const char *) (msg ? msg : "null"));
  12453. DUK_FFLUSH(DUK_STDERR);
  12454. #else
  12455. /* omit print */
  12456. DUK_UNREF(code);
  12457. DUK_UNREF(msg);
  12458. #endif
  12459. #if defined(DUK_USE_PANIC_ABORT)
  12460. DUK_ABORT();
  12461. #elif defined(DUK_USE_PANIC_EXIT)
  12462. DUK_EXIT(-1);
  12463. #elif defined(DUK_USE_PANIC_SEGFAULT)
  12464. /* exit() afterwards to satisfy "noreturn" */
  12465. DUK_CAUSE_SEGFAULT(); /* SCANBUILD: "Dereference of null pointer", normal */
  12466. DUK_EXIT(-1);
  12467. #else
  12468. #error no DUK_USE_PANIC_xxx macro defined
  12469. #endif
  12470. DUK_UNREACHABLE();
  12471. }
  12472. #endif /* !DUK_USE_PANIC_HANDLER */
  12473. #undef DUK__ERRFMT_BUFSIZE
  12474. #line 1 "duk_unicode_support.c"
  12475. /*
  12476. * Various Unicode help functions for character classification predicates,
  12477. * case conversion, decoding, etc.
  12478. */
  12479. /* include removed: duk_internal.h */
  12480. /*
  12481. * XUTF-8 and CESU-8 encoding/decoding
  12482. */
  12483. DUK_INTERNAL duk_small_int_t duk_unicode_get_xutf8_length(duk_ucodepoint_t cp) {
  12484. duk_uint_fast32_t x = (duk_uint_fast32_t) cp;
  12485. if (x < 0x80UL) {
  12486. /* 7 bits */
  12487. return 1;
  12488. } else if (x < 0x800UL) {
  12489. /* 11 bits */
  12490. return 2;
  12491. } else if (x < 0x10000UL) {
  12492. /* 16 bits */
  12493. return 3;
  12494. } else if (x < 0x200000UL) {
  12495. /* 21 bits */
  12496. return 4;
  12497. } else if (x < 0x4000000UL) {
  12498. /* 26 bits */
  12499. return 5;
  12500. } else if (x < (duk_ucodepoint_t) 0x80000000UL) {
  12501. /* 31 bits */
  12502. return 6;
  12503. } else {
  12504. /* 36 bits */
  12505. return 7;
  12506. }
  12507. }
  12508. #if defined(DUK_USE_ASSERTIONS)
  12509. DUK_INTERNAL duk_small_int_t duk_unicode_get_cesu8_length(duk_ucodepoint_t cp) {
  12510. duk_uint_fast32_t x = (duk_uint_fast32_t) cp;
  12511. if (x < 0x80UL) {
  12512. /* 7 bits */
  12513. return 1;
  12514. } else if (x < 0x800UL) {
  12515. /* 11 bits */
  12516. return 2;
  12517. } else if (x < 0x10000UL) {
  12518. /* 16 bits */
  12519. return 3;
  12520. } else {
  12521. /* Encoded as surrogate pair, each encoding to 3 bytes for
  12522. * 6 bytes total. Codepoints above U+10FFFF encode as 6 bytes
  12523. * too, see duk_unicode_encode_cesu8().
  12524. */
  12525. return 3 + 3;
  12526. }
  12527. }
  12528. #endif /* DUK_USE_ASSERTIONS */
  12529. DUK_INTERNAL duk_uint8_t duk_unicode_xutf8_markers[7] = {
  12530. 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
  12531. };
  12532. /* Encode to extended UTF-8; 'out' must have space for at least
  12533. * DUK_UNICODE_MAX_XUTF8_LENGTH bytes. Allows encoding of any
  12534. * 32-bit (unsigned) codepoint.
  12535. */
  12536. DUK_INTERNAL duk_small_int_t duk_unicode_encode_xutf8(duk_ucodepoint_t cp, duk_uint8_t *out) {
  12537. duk_uint_fast32_t x = (duk_uint_fast32_t) cp;
  12538. duk_small_int_t len;
  12539. duk_uint8_t marker;
  12540. duk_small_int_t i;
  12541. len = duk_unicode_get_xutf8_length(cp);
  12542. DUK_ASSERT(len > 0);
  12543. marker = duk_unicode_xutf8_markers[len - 1]; /* 64-bit OK because always >= 0 */
  12544. i = len;
  12545. DUK_ASSERT(i > 0);
  12546. do {
  12547. i--;
  12548. if (i > 0) {
  12549. out[i] = (duk_uint8_t) (0x80 + (x & 0x3f));
  12550. x >>= 6;
  12551. } else {
  12552. /* Note: masking of 'x' is not necessary because of
  12553. * range check and shifting -> no bits overlapping
  12554. * the marker should be set.
  12555. */
  12556. out[0] = (duk_uint8_t) (marker + x);
  12557. }
  12558. } while (i > 0);
  12559. return len;
  12560. }
  12561. /* Encode to CESU-8; 'out' must have space for at least
  12562. * DUK_UNICODE_MAX_CESU8_LENGTH bytes; codepoints above U+10FFFF
  12563. * will encode to garbage but won't overwrite the output buffer.
  12564. */
  12565. DUK_INTERNAL duk_small_int_t duk_unicode_encode_cesu8(duk_ucodepoint_t cp, duk_uint8_t *out) {
  12566. duk_uint_fast32_t x = (duk_uint_fast32_t) cp;
  12567. duk_small_int_t len;
  12568. if (x < 0x80UL) {
  12569. out[0] = (duk_uint8_t) x;
  12570. len = 1;
  12571. } else if (x < 0x800UL) {
  12572. out[0] = (duk_uint8_t) (0xc0 + ((x >> 6) & 0x1f));
  12573. out[1] = (duk_uint8_t) (0x80 + (x & 0x3f));
  12574. len = 2;
  12575. } else if (x < 0x10000UL) {
  12576. /* surrogate pairs get encoded here */
  12577. out[0] = (duk_uint8_t) (0xe0 + ((x >> 12) & 0x0f));
  12578. out[1] = (duk_uint8_t) (0x80 + ((x >> 6) & 0x3f));
  12579. out[2] = (duk_uint8_t) (0x80 + (x & 0x3f));
  12580. len = 3;
  12581. } else {
  12582. /*
  12583. * Unicode codepoints above U+FFFF are encoded as surrogate
  12584. * pairs here. This ensures that all CESU-8 codepoints are
  12585. * 16-bit values as expected in Ecmascript. The surrogate
  12586. * pairs always get a 3-byte encoding (each) in CESU-8.
  12587. * See: http://en.wikipedia.org/wiki/Surrogate_pair
  12588. *
  12589. * 20-bit codepoint, 10 bits (A and B) per surrogate pair:
  12590. *
  12591. * x = 0b00000000 0000AAAA AAAAAABB BBBBBBBB
  12592. * sp1 = 0b110110AA AAAAAAAA (0xd800 + ((x >> 10) & 0x3ff))
  12593. * sp2 = 0b110111BB BBBBBBBB (0xdc00 + (x & 0x3ff))
  12594. *
  12595. * Encoded into CESU-8:
  12596. *
  12597. * sp1 -> 0b11101101 (0xe0 + ((sp1 >> 12) & 0x0f))
  12598. * -> 0b1010AAAA (0x80 + ((sp1 >> 6) & 0x3f))
  12599. * -> 0b10AAAAAA (0x80 + (sp1 & 0x3f))
  12600. * sp2 -> 0b11101101 (0xe0 + ((sp2 >> 12) & 0x0f))
  12601. * -> 0b1011BBBB (0x80 + ((sp2 >> 6) & 0x3f))
  12602. * -> 0b10BBBBBB (0x80 + (sp2 & 0x3f))
  12603. *
  12604. * Note that 0x10000 must be subtracted first. The code below
  12605. * avoids the sp1, sp2 temporaries which saves around 20 bytes
  12606. * of code.
  12607. */
  12608. x -= 0x10000UL;
  12609. out[0] = (duk_uint8_t) (0xed);
  12610. out[1] = (duk_uint8_t) (0xa0 + ((x >> 16) & 0x0f));
  12611. out[2] = (duk_uint8_t) (0x80 + ((x >> 10) & 0x3f));
  12612. out[3] = (duk_uint8_t) (0xed);
  12613. out[4] = (duk_uint8_t) (0xb0 + ((x >> 6) & 0x0f));
  12614. out[5] = (duk_uint8_t) (0x80 + (x & 0x3f));
  12615. len = 6;
  12616. }
  12617. return len;
  12618. }
  12619. /* Decode helper. Return zero on error. */
  12620. 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) {
  12621. const duk_uint8_t *p;
  12622. duk_uint32_t res;
  12623. duk_uint_fast8_t ch;
  12624. duk_small_int_t n;
  12625. DUK_UNREF(thr);
  12626. p = *ptr;
  12627. if (p < ptr_start || p >= ptr_end) {
  12628. goto fail;
  12629. }
  12630. /*
  12631. * UTF-8 decoder which accepts longer than standard byte sequences.
  12632. * This allows full 32-bit code points to be used.
  12633. */
  12634. ch = (duk_uint_fast8_t) (*p++);
  12635. if (ch < 0x80) {
  12636. /* 0xxx xxxx [7 bits] */
  12637. res = (duk_uint32_t) (ch & 0x7f);
  12638. n = 0;
  12639. } else if (ch < 0xc0) {
  12640. /* 10xx xxxx -> invalid */
  12641. goto fail;
  12642. } else if (ch < 0xe0) {
  12643. /* 110x xxxx 10xx xxxx [11 bits] */
  12644. res = (duk_uint32_t) (ch & 0x1f);
  12645. n = 1;
  12646. } else if (ch < 0xf0) {
  12647. /* 1110 xxxx 10xx xxxx 10xx xxxx [16 bits] */
  12648. res = (duk_uint32_t) (ch & 0x0f);
  12649. n = 2;
  12650. } else if (ch < 0xf8) {
  12651. /* 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx [21 bits] */
  12652. res = (duk_uint32_t) (ch & 0x07);
  12653. n = 3;
  12654. } else if (ch < 0xfc) {
  12655. /* 1111 10xx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [26 bits] */
  12656. res = (duk_uint32_t) (ch & 0x03);
  12657. n = 4;
  12658. } else if (ch < 0xfe) {
  12659. /* 1111 110x 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [31 bits] */
  12660. res = (duk_uint32_t) (ch & 0x01);
  12661. n = 5;
  12662. } else if (ch < 0xff) {
  12663. /* 1111 1110 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [36 bits] */
  12664. res = (duk_uint32_t) (0);
  12665. n = 6;
  12666. } else {
  12667. /* 8-byte format could be:
  12668. * 1111 1111 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [41 bits]
  12669. *
  12670. * However, this format would not have a zero bit following the
  12671. * leading one bits and would not allow 0xFF to be used as an
  12672. * "invalid xutf-8" marker for internal keys. Further, 8-byte
  12673. * encodings (up to 41 bit code points) are not currently needed.
  12674. */
  12675. goto fail;
  12676. }
  12677. DUK_ASSERT(p >= ptr_start); /* verified at beginning */
  12678. if (p + n > ptr_end) {
  12679. /* check pointer at end */
  12680. goto fail;
  12681. }
  12682. while (n > 0) {
  12683. DUK_ASSERT(p >= ptr_start && p < ptr_end);
  12684. res = res << 6;
  12685. res += (duk_uint32_t) ((*p++) & 0x3f);
  12686. n--;
  12687. }
  12688. *ptr = p;
  12689. *out_cp = res;
  12690. return 1;
  12691. fail:
  12692. return 0;
  12693. }
  12694. /* used by e.g. duk_regexp_executor.c, string built-ins */
  12695. 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) {
  12696. duk_ucodepoint_t cp;
  12697. if (duk_unicode_decode_xutf8(thr, ptr, ptr_start, ptr_end, &cp)) {
  12698. return cp;
  12699. }
  12700. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, "utf-8 decode failed");
  12701. DUK_UNREACHABLE();
  12702. return 0;
  12703. }
  12704. /* Compute (extended) utf-8 length without codepoint encoding validation,
  12705. * used for string interning.
  12706. *
  12707. * NOTE: This algorithm is performance critical, more so than string hashing
  12708. * in some cases. It is needed when interning a string and needs to scan
  12709. * every byte of the string with no skipping. Having an ASCII fast path
  12710. * is useful if possible in the algorithm. The current algorithms were
  12711. * chosen from several variants, based on x64 gcc -O2 testing. See:
  12712. * https://github.com/svaarala/duktape/pull/422
  12713. */
  12714. #if defined(DUK_USE_PREFER_SIZE)
  12715. /* Small variant; roughly 150 bytes smaller than the fast variant. */
  12716. DUK_INTERNAL duk_size_t duk_unicode_unvalidated_utf8_length(const duk_uint8_t *data, duk_size_t blen) {
  12717. const duk_uint8_t *p;
  12718. const duk_uint8_t *p_end;
  12719. duk_size_t ncont;
  12720. duk_size_t clen;
  12721. p = data;
  12722. p_end = data + blen;
  12723. ncont = 0;
  12724. while (p != p_end) {
  12725. duk_uint8_t x;
  12726. x = *p++;
  12727. if (DUK_UNLIKELY(x >= 0x80 && x <= 0xbf)) {
  12728. ncont++;
  12729. }
  12730. }
  12731. DUK_ASSERT(ncont <= blen);
  12732. clen = blen - ncont;
  12733. DUK_ASSERT(clen <= blen);
  12734. return clen;
  12735. }
  12736. #else /* DUK_USE_PREFER_SIZE */
  12737. /* This seems like a good overall approach. Fast path for ASCII in 4 byte
  12738. * blocks.
  12739. */
  12740. DUK_INTERNAL duk_size_t duk_unicode_unvalidated_utf8_length(const duk_uint8_t *data, duk_size_t blen) {
  12741. const duk_uint8_t *p;
  12742. const duk_uint8_t *p_end;
  12743. const duk_uint32_t *p32_end;
  12744. const duk_uint32_t *p32;
  12745. duk_size_t ncont;
  12746. duk_size_t clen;
  12747. ncont = 0; /* number of continuation (non-initial) bytes in [0x80,0xbf] */
  12748. p = data;
  12749. p_end = data + blen;
  12750. if (blen < 16) {
  12751. goto skip_fastpath;
  12752. }
  12753. /* Align 'p' to 4; the input data may have arbitrary alignment.
  12754. * End of string check not needed because blen >= 16.
  12755. */
  12756. while (((duk_uintptr_t) (const void *) p) & 0x03) {
  12757. duk_uint8_t x;
  12758. x = *p++;
  12759. if (DUK_UNLIKELY(x >= 0x80 && x <= 0xbf)) {
  12760. ncont++;
  12761. }
  12762. }
  12763. /* Full, aligned 4-byte reads. */
  12764. p32_end = (const duk_uint32_t *) (const void *) (p + ((duk_size_t) (p_end - p) & (duk_size_t) (~0x03)));
  12765. p32 = (const duk_uint32_t *) (const void *) p;
  12766. while (p32 != (const duk_uint32_t *) p32_end) {
  12767. duk_uint32_t x;
  12768. x = *p32++;
  12769. if (DUK_LIKELY((x & 0x80808080UL) == 0)) {
  12770. ; /* ASCII fast path */
  12771. } else {
  12772. /* Flip highest bit of each byte which changes
  12773. * the bit pattern 10xxxxxx into 00xxxxxx which
  12774. * allows an easy bit mask test.
  12775. */
  12776. x ^= 0x80808080UL;
  12777. if (DUK_UNLIKELY(!(x & 0xc0000000UL))) {
  12778. ncont++;
  12779. }
  12780. if (DUK_UNLIKELY(!(x & 0x00c00000UL))) {
  12781. ncont++;
  12782. }
  12783. if (DUK_UNLIKELY(!(x & 0x0000c000UL))) {
  12784. ncont++;
  12785. }
  12786. if (DUK_UNLIKELY(!(x & 0x000000c0UL))) {
  12787. ncont++;
  12788. }
  12789. }
  12790. }
  12791. p = (const duk_uint8_t *) p32;
  12792. /* Fall through to handle the rest. */
  12793. skip_fastpath:
  12794. while (p != p_end) {
  12795. duk_uint8_t x;
  12796. x = *p++;
  12797. if (DUK_UNLIKELY(x >= 0x80 && x <= 0xbf)) {
  12798. ncont++;
  12799. }
  12800. }
  12801. DUK_ASSERT(ncont <= blen);
  12802. clen = blen - ncont;
  12803. DUK_ASSERT(clen <= blen);
  12804. return clen;
  12805. }
  12806. #endif /* DUK_USE_PREFER_SIZE */
  12807. /*
  12808. * Unicode range matcher
  12809. *
  12810. * Matches a codepoint against a packed bitstream of character ranges.
  12811. * Used for slow path Unicode matching.
  12812. */
  12813. /* Must match src/extract_chars.py, generate_match_table3(). */
  12814. DUK_LOCAL duk_uint32_t duk__uni_decode_value(duk_bitdecoder_ctx *bd_ctx) {
  12815. duk_uint32_t t;
  12816. t = (duk_uint32_t) duk_bd_decode(bd_ctx, 4);
  12817. if (t <= 0x0eU) {
  12818. return t;
  12819. }
  12820. t = (duk_uint32_t) duk_bd_decode(bd_ctx, 8);
  12821. if (t <= 0xfdU) {
  12822. return t + 0x0f;
  12823. }
  12824. if (t == 0xfeU) {
  12825. t = (duk_uint32_t) duk_bd_decode(bd_ctx, 12);
  12826. return t + 0x0fU + 0xfeU;
  12827. } else {
  12828. t = (duk_uint32_t) duk_bd_decode(bd_ctx, 24);
  12829. return t + 0x0fU + 0xfeU + 0x1000UL;
  12830. }
  12831. }
  12832. DUK_LOCAL duk_small_int_t duk__uni_range_match(const duk_uint8_t *unitab, duk_size_t unilen, duk_codepoint_t cp) {
  12833. duk_bitdecoder_ctx bd_ctx;
  12834. duk_codepoint_t prev_re;
  12835. DUK_MEMZERO(&bd_ctx, sizeof(bd_ctx));
  12836. bd_ctx.data = (const duk_uint8_t *) unitab;
  12837. bd_ctx.length = (duk_size_t) unilen;
  12838. prev_re = 0;
  12839. for (;;) {
  12840. duk_codepoint_t r1, r2;
  12841. r1 = (duk_codepoint_t) duk__uni_decode_value(&bd_ctx);
  12842. if (r1 == 0) {
  12843. break;
  12844. }
  12845. r2 = (duk_codepoint_t) duk__uni_decode_value(&bd_ctx);
  12846. r1 = prev_re + r1;
  12847. r2 = r1 + r2;
  12848. prev_re = r2;
  12849. /* [r1,r2] is the range */
  12850. DUK_DDD(DUK_DDDPRINT("duk__uni_range_match: cp=%06lx range=[0x%06lx,0x%06lx]",
  12851. (unsigned long) cp, (unsigned long) r1, (unsigned long) r2));
  12852. if (cp >= r1 && cp <= r2) {
  12853. return 1;
  12854. }
  12855. }
  12856. return 0;
  12857. }
  12858. /*
  12859. * "WhiteSpace" production check.
  12860. */
  12861. DUK_INTERNAL duk_small_int_t duk_unicode_is_whitespace(duk_codepoint_t cp) {
  12862. /*
  12863. * E5 Section 7.2 specifies six characters specifically as
  12864. * white space:
  12865. *
  12866. * 0009;<control>;Cc;0;S;;;;;N;CHARACTER TABULATION;;;;
  12867. * 000B;<control>;Cc;0;S;;;;;N;LINE TABULATION;;;;
  12868. * 000C;<control>;Cc;0;WS;;;;;N;FORM FEED (FF);;;;
  12869. * 0020;SPACE;Zs;0;WS;;;;;N;;;;;
  12870. * 00A0;NO-BREAK SPACE;Zs;0;CS;<noBreak> 0020;;;;N;NON-BREAKING SPACE;;;;
  12871. * FEFF;ZERO WIDTH NO-BREAK SPACE;Cf;0;BN;;;;;N;BYTE ORDER MARK;;;;
  12872. *
  12873. * It also specifies any Unicode category 'Zs' characters as white
  12874. * space. These can be extracted with the "src/extract_chars.py" script.
  12875. * Current result:
  12876. *
  12877. * RAW OUTPUT:
  12878. * ===========
  12879. * 0020;SPACE;Zs;0;WS;;;;;N;;;;;
  12880. * 00A0;NO-BREAK SPACE;Zs;0;CS;<noBreak> 0020;;;;N;NON-BREAKING SPACE;;;;
  12881. * 1680;OGHAM SPACE MARK;Zs;0;WS;;;;;N;;;;;
  12882. * 180E;MONGOLIAN VOWEL SEPARATOR;Zs;0;WS;;;;;N;;;;;
  12883. * 2000;EN QUAD;Zs;0;WS;2002;;;;N;;;;;
  12884. * 2001;EM QUAD;Zs;0;WS;2003;;;;N;;;;;
  12885. * 2002;EN SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  12886. * 2003;EM SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  12887. * 2004;THREE-PER-EM SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  12888. * 2005;FOUR-PER-EM SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  12889. * 2006;SIX-PER-EM SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  12890. * 2007;FIGURE SPACE;Zs;0;WS;<noBreak> 0020;;;;N;;;;;
  12891. * 2008;PUNCTUATION SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  12892. * 2009;THIN SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  12893. * 200A;HAIR SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  12894. * 202F;NARROW NO-BREAK SPACE;Zs;0;CS;<noBreak> 0020;;;;N;;;;;
  12895. * 205F;MEDIUM MATHEMATICAL SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  12896. * 3000;IDEOGRAPHIC SPACE;Zs;0;WS;<wide> 0020;;;;N;;;;;
  12897. *
  12898. * RANGES:
  12899. * =======
  12900. * 0x0020
  12901. * 0x00a0
  12902. * 0x1680
  12903. * 0x180e
  12904. * 0x2000 ... 0x200a
  12905. * 0x202f
  12906. * 0x205f
  12907. * 0x3000
  12908. *
  12909. * A manual decoder (below) is probably most compact for this.
  12910. */
  12911. duk_uint_fast8_t lo;
  12912. duk_uint_fast32_t hi;
  12913. /* cp == -1 (EOF) never matches and causes return value 0 */
  12914. lo = (duk_uint_fast8_t) (cp & 0xff);
  12915. hi = (duk_uint_fast32_t) (cp >> 8); /* does not fit into an uchar */
  12916. if (hi == 0x0000UL) {
  12917. if (lo == 0x09U || lo == 0x0bU || lo == 0x0cU ||
  12918. lo == 0x20U || lo == 0xa0U) {
  12919. return 1;
  12920. }
  12921. } else if (hi == 0x0020UL) {
  12922. if (lo <= 0x0aU || lo == 0x2fU || lo == 0x5fU) {
  12923. return 1;
  12924. }
  12925. } else if (cp == 0x1680L || cp == 0x180eL || cp == 0x3000L ||
  12926. cp == 0xfeffL) {
  12927. return 1;
  12928. }
  12929. return 0;
  12930. }
  12931. /*
  12932. * "LineTerminator" production check.
  12933. */
  12934. DUK_INTERNAL duk_small_int_t duk_unicode_is_line_terminator(duk_codepoint_t cp) {
  12935. /*
  12936. * E5 Section 7.3
  12937. *
  12938. * A LineTerminatorSequence essentially merges <CR> <LF> sequences
  12939. * into a single line terminator. This must be handled by the caller.
  12940. */
  12941. if (cp == 0x000aL || cp == 0x000dL || cp == 0x2028L ||
  12942. cp == 0x2029L) {
  12943. return 1;
  12944. }
  12945. return 0;
  12946. }
  12947. /*
  12948. * "IdentifierStart" production check.
  12949. */
  12950. DUK_INTERNAL duk_small_int_t duk_unicode_is_identifier_start(duk_codepoint_t cp) {
  12951. /*
  12952. * E5 Section 7.6:
  12953. *
  12954. * IdentifierStart:
  12955. * UnicodeLetter
  12956. * $
  12957. * _
  12958. * \ UnicodeEscapeSequence
  12959. *
  12960. * IdentifierStart production has one multi-character production:
  12961. *
  12962. * \ UnicodeEscapeSequence
  12963. *
  12964. * The '\' character is -not- matched by this function. Rather, the caller
  12965. * should decode the escape and then call this function to check whether the
  12966. * decoded character is acceptable (see discussion in E5 Section 7.6).
  12967. *
  12968. * The "UnicodeLetter" alternative of the production allows letters
  12969. * from various Unicode categories. These can be extracted with the
  12970. * "src/extract_chars.py" script.
  12971. *
  12972. * Because the result has hundreds of Unicode codepoint ranges, matching
  12973. * for any values >= 0x80 are done using a very slow range-by-range scan
  12974. * and a packed range format.
  12975. *
  12976. * The ASCII portion (codepoints 0x00 ... 0x7f) is fast-pathed below because
  12977. * it matters the most. The ASCII related ranges of IdentifierStart are:
  12978. *
  12979. * 0x0041 ... 0x005a ['A' ... 'Z']
  12980. * 0x0061 ... 0x007a ['a' ... 'z']
  12981. * 0x0024 ['$']
  12982. * 0x005f ['_']
  12983. */
  12984. /* ASCII (and EOF) fast path -- quick accept and reject */
  12985. if (cp <= 0x7fL) {
  12986. if ((cp >= 'a' && cp <= 'z') ||
  12987. (cp >= 'A' && cp <= 'Z') ||
  12988. cp == '_' || cp == '$') {
  12989. return 1;
  12990. }
  12991. return 0;
  12992. }
  12993. /* Non-ASCII slow path (range-by-range linear comparison), very slow */
  12994. #ifdef DUK_USE_SOURCE_NONBMP
  12995. if (duk__uni_range_match(duk_unicode_ids_noa,
  12996. (duk_size_t) sizeof(duk_unicode_ids_noa),
  12997. (duk_codepoint_t) cp)) {
  12998. return 1;
  12999. }
  13000. return 0;
  13001. #else
  13002. if (cp < 0x10000L) {
  13003. if (duk__uni_range_match(duk_unicode_ids_noabmp,
  13004. sizeof(duk_unicode_ids_noabmp),
  13005. (duk_codepoint_t) cp)) {
  13006. return 1;
  13007. }
  13008. return 0;
  13009. } else {
  13010. /* without explicit non-BMP support, assume non-BMP characters
  13011. * are always accepted as identifier characters.
  13012. */
  13013. return 1;
  13014. }
  13015. #endif
  13016. }
  13017. /*
  13018. * "IdentifierPart" production check.
  13019. */
  13020. DUK_INTERNAL duk_small_int_t duk_unicode_is_identifier_part(duk_codepoint_t cp) {
  13021. /*
  13022. * E5 Section 7.6:
  13023. *
  13024. * IdentifierPart:
  13025. * IdentifierStart
  13026. * UnicodeCombiningMark
  13027. * UnicodeDigit
  13028. * UnicodeConnectorPunctuation
  13029. * <ZWNJ> [U+200C]
  13030. * <ZWJ> [U+200D]
  13031. *
  13032. * IdentifierPart production has one multi-character production
  13033. * as part of its IdentifierStart alternative. The '\' character
  13034. * of an escape sequence is not matched here, see discussion in
  13035. * duk_unicode_is_identifier_start().
  13036. *
  13037. * To match non-ASCII characters (codepoints >= 0x80), a very slow
  13038. * linear range-by-range scan is used. The codepoint is first compared
  13039. * to the IdentifierStart ranges, and if it doesn't match, then to a
  13040. * set consisting of code points in IdentifierPart but not in
  13041. * IdentifierStart. This is done to keep the unicode range data small,
  13042. * at the expense of speed.
  13043. *
  13044. * The ASCII fast path consists of:
  13045. *
  13046. * 0x0030 ... 0x0039 ['0' ... '9', UnicodeDigit]
  13047. * 0x0041 ... 0x005a ['A' ... 'Z', IdentifierStart]
  13048. * 0x0061 ... 0x007a ['a' ... 'z', IdentifierStart]
  13049. * 0x0024 ['$', IdentifierStart]
  13050. * 0x005f ['_', IdentifierStart and
  13051. * UnicodeConnectorPunctuation]
  13052. *
  13053. * UnicodeCombiningMark has no code points <= 0x7f.
  13054. *
  13055. * The matching code reuses the "identifier start" tables, and then
  13056. * consults a separate range set for characters in "identifier part"
  13057. * but not in "identifier start". These can be extracted with the
  13058. * "src/extract_chars.py" script.
  13059. *
  13060. * UnicodeCombiningMark -> categories Mn, Mc
  13061. * UnicodeDigit -> categories Nd
  13062. * UnicodeConnectorPunctuation -> categories Pc
  13063. */
  13064. /* ASCII (and EOF) fast path -- quick accept and reject */
  13065. if (cp <= 0x7fL) {
  13066. if ((cp >= 'a' && cp <= 'z') ||
  13067. (cp >= 'A' && cp <= 'Z') ||
  13068. (cp >= '0' && cp <= '9') ||
  13069. cp == '_' || cp == '$') {
  13070. return 1;
  13071. }
  13072. return 0;
  13073. }
  13074. /* Non-ASCII slow path (range-by-range linear comparison), very slow */
  13075. #ifdef DUK_USE_SOURCE_NONBMP
  13076. if (duk__uni_range_match(duk_unicode_ids_noa,
  13077. sizeof(duk_unicode_ids_noa),
  13078. (duk_codepoint_t) cp) ||
  13079. duk__uni_range_match(duk_unicode_idp_m_ids_noa,
  13080. sizeof(duk_unicode_idp_m_ids_noa),
  13081. (duk_codepoint_t) cp)) {
  13082. return 1;
  13083. }
  13084. return 0;
  13085. #else
  13086. if (cp < 0x10000L) {
  13087. if (duk__uni_range_match(duk_unicode_ids_noabmp,
  13088. sizeof(duk_unicode_ids_noabmp),
  13089. (duk_codepoint_t) cp) ||
  13090. duk__uni_range_match(duk_unicode_idp_m_ids_noabmp,
  13091. sizeof(duk_unicode_idp_m_ids_noabmp),
  13092. (duk_codepoint_t) cp)) {
  13093. return 1;
  13094. }
  13095. return 0;
  13096. } else {
  13097. /* without explicit non-BMP support, assume non-BMP characters
  13098. * are always accepted as identifier characters.
  13099. */
  13100. return 1;
  13101. }
  13102. #endif
  13103. }
  13104. /*
  13105. * Unicode letter check.
  13106. */
  13107. DUK_INTERNAL duk_small_int_t duk_unicode_is_letter(duk_codepoint_t cp) {
  13108. /*
  13109. * Unicode letter is now taken to be the categories:
  13110. *
  13111. * Lu, Ll, Lt, Lm, Lo
  13112. *
  13113. * (Not sure if this is exactly correct.)
  13114. *
  13115. * The ASCII fast path consists of:
  13116. *
  13117. * 0x0041 ... 0x005a ['A' ... 'Z']
  13118. * 0x0061 ... 0x007a ['a' ... 'z']
  13119. */
  13120. /* ASCII (and EOF) fast path -- quick accept and reject */
  13121. if (cp <= 0x7fL) {
  13122. if ((cp >= 'a' && cp <= 'z') ||
  13123. (cp >= 'A' && cp <= 'Z')) {
  13124. return 1;
  13125. }
  13126. return 0;
  13127. }
  13128. /* Non-ASCII slow path (range-by-range linear comparison), very slow */
  13129. #ifdef DUK_USE_SOURCE_NONBMP
  13130. if (duk__uni_range_match(duk_unicode_ids_noa,
  13131. sizeof(duk_unicode_ids_noa),
  13132. (duk_codepoint_t) cp) &&
  13133. !duk__uni_range_match(duk_unicode_ids_m_let_noa,
  13134. sizeof(duk_unicode_ids_m_let_noa),
  13135. (duk_codepoint_t) cp)) {
  13136. return 1;
  13137. }
  13138. return 0;
  13139. #else
  13140. if (cp < 0x10000L) {
  13141. if (duk__uni_range_match(duk_unicode_ids_noabmp,
  13142. sizeof(duk_unicode_ids_noabmp),
  13143. (duk_codepoint_t) cp) &&
  13144. !duk__uni_range_match(duk_unicode_ids_m_let_noabmp,
  13145. sizeof(duk_unicode_ids_m_let_noabmp),
  13146. (duk_codepoint_t) cp)) {
  13147. return 1;
  13148. }
  13149. return 0;
  13150. } else {
  13151. /* without explicit non-BMP support, assume non-BMP characters
  13152. * are always accepted as letters.
  13153. */
  13154. return 1;
  13155. }
  13156. #endif
  13157. }
  13158. /*
  13159. * Complex case conversion helper which decodes a bit-packed conversion
  13160. * control stream generated by unicode/extract_caseconv.py. The conversion
  13161. * is very slow because it runs through the conversion data in a linear
  13162. * fashion to save space (which is why ASCII characters have a special
  13163. * fast path before arriving here).
  13164. *
  13165. * The particular bit counts etc have been determined experimentally to
  13166. * be small but still sufficient, and must match the Python script
  13167. * (src/extract_caseconv.py).
  13168. *
  13169. * The return value is the case converted codepoint or -1 if the conversion
  13170. * results in multiple characters (this is useful for regexp Canonicalization
  13171. * operation). If 'buf' is not NULL, the result codepoint(s) are also
  13172. * appended to the hbuffer.
  13173. *
  13174. * Context and locale specific rules must be checked before consulting
  13175. * this function.
  13176. */
  13177. DUK_LOCAL
  13178. duk_codepoint_t duk__slow_case_conversion(duk_hthread *thr,
  13179. duk_bufwriter_ctx *bw,
  13180. duk_codepoint_t cp,
  13181. duk_bitdecoder_ctx *bd_ctx) {
  13182. duk_small_int_t skip = 0;
  13183. duk_small_int_t n;
  13184. duk_small_int_t t;
  13185. duk_small_int_t count;
  13186. duk_codepoint_t tmp_cp;
  13187. duk_codepoint_t start_i;
  13188. duk_codepoint_t start_o;
  13189. DUK_UNREF(thr);
  13190. DUK_ASSERT(bd_ctx != NULL);
  13191. DUK_DDD(DUK_DDDPRINT("slow case conversion for codepoint: %ld", (long) cp));
  13192. /* range conversion with a "skip" */
  13193. DUK_DDD(DUK_DDDPRINT("checking ranges"));
  13194. for (;;) {
  13195. skip++;
  13196. n = (duk_small_int_t) duk_bd_decode(bd_ctx, 6);
  13197. if (n == 0x3f) {
  13198. /* end marker */
  13199. break;
  13200. }
  13201. DUK_DDD(DUK_DDDPRINT("skip=%ld, n=%ld", (long) skip, (long) n));
  13202. while (n--) {
  13203. start_i = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  13204. start_o = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  13205. count = (duk_small_int_t) duk_bd_decode(bd_ctx, 7);
  13206. DUK_DDD(DUK_DDDPRINT("range: start_i=%ld, start_o=%ld, count=%ld, skip=%ld",
  13207. (long) start_i, (long) start_o, (long) count, (long) skip));
  13208. if (cp >= start_i) {
  13209. tmp_cp = cp - start_i; /* always >= 0 */
  13210. if (tmp_cp < (duk_codepoint_t) count * (duk_codepoint_t) skip &&
  13211. (tmp_cp % (duk_codepoint_t) skip) == 0) {
  13212. DUK_DDD(DUK_DDDPRINT("range matches input codepoint"));
  13213. cp = start_o + tmp_cp;
  13214. goto single;
  13215. }
  13216. }
  13217. }
  13218. }
  13219. /* 1:1 conversion */
  13220. n = (duk_small_int_t) duk_bd_decode(bd_ctx, 6);
  13221. DUK_DDD(DUK_DDDPRINT("checking 1:1 conversions (count %ld)", (long) n));
  13222. while (n--) {
  13223. start_i = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  13224. start_o = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  13225. DUK_DDD(DUK_DDDPRINT("1:1 conversion %ld -> %ld", (long) start_i, (long) start_o));
  13226. if (cp == start_i) {
  13227. DUK_DDD(DUK_DDDPRINT("1:1 matches input codepoint"));
  13228. cp = start_o;
  13229. goto single;
  13230. }
  13231. }
  13232. /* complex, multicharacter conversion */
  13233. n = (duk_small_int_t) duk_bd_decode(bd_ctx, 7);
  13234. DUK_DDD(DUK_DDDPRINT("checking 1:n conversions (count %ld)", (long) n));
  13235. while (n--) {
  13236. start_i = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  13237. t = (duk_small_int_t) duk_bd_decode(bd_ctx, 2);
  13238. DUK_DDD(DUK_DDDPRINT("1:n conversion %ld -> %ld chars", (long) start_i, (long) t));
  13239. if (cp == start_i) {
  13240. DUK_DDD(DUK_DDDPRINT("1:n matches input codepoint"));
  13241. if (bw != NULL) {
  13242. while (t--) {
  13243. tmp_cp = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  13244. DUK_BW_WRITE_RAW_XUTF8(thr, bw, (duk_ucodepoint_t) tmp_cp);
  13245. }
  13246. }
  13247. return -1;
  13248. } else {
  13249. while (t--) {
  13250. (void) duk_bd_decode(bd_ctx, 16);
  13251. }
  13252. }
  13253. }
  13254. /* default: no change */
  13255. DUK_DDD(DUK_DDDPRINT("no rule matches, output is same as input"));
  13256. /* fall through */
  13257. single:
  13258. if (bw != NULL) {
  13259. DUK_BW_WRITE_RAW_XUTF8(thr, bw, (duk_ucodepoint_t) cp);
  13260. }
  13261. return cp;
  13262. }
  13263. /*
  13264. * Case conversion helper, with context/local sensitivity.
  13265. * For proper case conversion, one needs to know the character
  13266. * and the preceding and following characters, as well as
  13267. * locale/language.
  13268. */
  13269. /* XXX: add 'language' argument when locale/language sensitive rule
  13270. * support added.
  13271. */
  13272. DUK_LOCAL
  13273. duk_codepoint_t duk__case_transform_helper(duk_hthread *thr,
  13274. duk_bufwriter_ctx *bw,
  13275. duk_codepoint_t cp,
  13276. duk_codepoint_t prev,
  13277. duk_codepoint_t next,
  13278. duk_bool_t uppercase) {
  13279. duk_bitdecoder_ctx bd_ctx;
  13280. /* fast path for ASCII */
  13281. if (cp < 0x80L) {
  13282. /* XXX: there are language sensitive rules for the ASCII range.
  13283. * If/when language/locale support is implemented, they need to
  13284. * be implemented here for the fast path. There are no context
  13285. * sensitive rules for ASCII range.
  13286. */
  13287. if (uppercase) {
  13288. if (cp >= 'a' && cp <= 'z') {
  13289. cp = cp - 'a' + 'A';
  13290. }
  13291. } else {
  13292. if (cp >= 'A' && cp <= 'Z') {
  13293. cp = cp - 'A' + 'a';
  13294. }
  13295. }
  13296. if (bw != NULL) {
  13297. DUK_BW_WRITE_RAW_U8(thr, bw, (duk_uint8_t) cp);
  13298. }
  13299. return cp;
  13300. }
  13301. /* context and locale specific rules which cannot currently be represented
  13302. * in the caseconv bitstream: hardcoded rules in C
  13303. */
  13304. if (uppercase) {
  13305. /* XXX: turkish / azeri */
  13306. } else {
  13307. /*
  13308. * Final sigma context specific rule. This is a rather tricky
  13309. * rule and this handling is probably not 100% correct now.
  13310. * The rule is not locale/language specific so it is supported.
  13311. */
  13312. if (cp == 0x03a3L && /* U+03A3 = GREEK CAPITAL LETTER SIGMA */
  13313. duk_unicode_is_letter(prev) && /* prev exists and is not a letter */
  13314. !duk_unicode_is_letter(next)) { /* next does not exist or next is not a letter */
  13315. /* Capital sigma occurred at "end of word", lowercase to
  13316. * U+03C2 = GREEK SMALL LETTER FINAL SIGMA. Otherwise
  13317. * fall through and let the normal rules lowercase it to
  13318. * U+03C3 = GREEK SMALL LETTER SIGMA.
  13319. */
  13320. cp = 0x03c2L;
  13321. goto singlechar;
  13322. }
  13323. /* XXX: lithuanian not implemented */
  13324. /* XXX: lithuanian, explicit dot rules */
  13325. /* XXX: turkish / azeri, lowercase rules */
  13326. }
  13327. /* 1:1 or special conversions, but not locale/context specific: script generated rules */
  13328. DUK_MEMZERO(&bd_ctx, sizeof(bd_ctx));
  13329. if (uppercase) {
  13330. bd_ctx.data = (const duk_uint8_t *) duk_unicode_caseconv_uc;
  13331. bd_ctx.length = (duk_size_t) sizeof(duk_unicode_caseconv_uc);
  13332. } else {
  13333. bd_ctx.data = (const duk_uint8_t *) duk_unicode_caseconv_lc;
  13334. bd_ctx.length = (duk_size_t) sizeof(duk_unicode_caseconv_lc);
  13335. }
  13336. return duk__slow_case_conversion(thr, bw, cp, &bd_ctx);
  13337. singlechar:
  13338. if (bw != NULL) {
  13339. DUK_BW_WRITE_RAW_XUTF8(thr, bw, (duk_ucodepoint_t) cp);
  13340. }
  13341. return cp;
  13342. /* unused now, not needed until Turkish/Azeri */
  13343. #if 0
  13344. nochar:
  13345. return -1;
  13346. #endif
  13347. }
  13348. /*
  13349. * Replace valstack top with case converted version.
  13350. */
  13351. DUK_INTERNAL void duk_unicode_case_convert_string(duk_hthread *thr, duk_small_int_t uppercase) {
  13352. duk_context *ctx = (duk_context *) thr;
  13353. duk_hstring *h_input;
  13354. duk_bufwriter_ctx bw_alloc;
  13355. duk_bufwriter_ctx *bw;
  13356. const duk_uint8_t *p, *p_start, *p_end;
  13357. duk_codepoint_t prev, curr, next;
  13358. h_input = duk_require_hstring(ctx, -1);
  13359. DUK_ASSERT(h_input != NULL);
  13360. bw = &bw_alloc;
  13361. DUK_BW_INIT_PUSHBUF(thr, bw, DUK_HSTRING_GET_BYTELEN(h_input));
  13362. /* [ ... input buffer ] */
  13363. p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
  13364. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  13365. p = p_start;
  13366. prev = -1; DUK_UNREF(prev);
  13367. curr = -1;
  13368. next = -1;
  13369. for (;;) {
  13370. prev = curr;
  13371. curr = next;
  13372. next = -1;
  13373. if (p < p_end) {
  13374. next = (int) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end);
  13375. } else {
  13376. /* end of input and last char has been processed */
  13377. if (curr < 0) {
  13378. break;
  13379. }
  13380. }
  13381. /* on first round, skip */
  13382. if (curr >= 0) {
  13383. /* XXX: could add a fast path to process chunks of input codepoints,
  13384. * but relative benefit would be quite small.
  13385. */
  13386. /* Ensure space for maximum multi-character result; estimate is overkill. */
  13387. DUK_BW_ENSURE(thr, bw, 8 * DUK_UNICODE_MAX_XUTF8_LENGTH);
  13388. duk__case_transform_helper(thr,
  13389. bw,
  13390. (duk_codepoint_t) curr,
  13391. prev,
  13392. next,
  13393. uppercase);
  13394. }
  13395. }
  13396. DUK_BW_COMPACT(thr, bw);
  13397. duk_to_string(ctx, -1); /* invalidates h_buf pointer */
  13398. duk_remove(ctx, -2);
  13399. }
  13400. #ifdef DUK_USE_REGEXP_SUPPORT
  13401. /*
  13402. * Canonicalize() abstract operation needed for canonicalization of individual
  13403. * codepoints during regexp compilation and execution, see E5 Section 15.10.2.8.
  13404. * Note that codepoints are canonicalized one character at a time, so no context
  13405. * specific rules can apply. Locale specific rules can apply, though.
  13406. */
  13407. DUK_INTERNAL duk_codepoint_t duk_unicode_re_canonicalize_char(duk_hthread *thr, duk_codepoint_t cp) {
  13408. duk_codepoint_t y;
  13409. y = duk__case_transform_helper(thr,
  13410. NULL, /* NULL is allowed, no output */
  13411. cp, /* curr char */
  13412. -1, /* prev char */
  13413. -1, /* next char */
  13414. 1); /* uppercase */
  13415. if ((y < 0) || (cp >= 0x80 && y < 0x80)) {
  13416. /* multiple codepoint conversion or non-ASCII mapped to ASCII
  13417. * --> leave as is.
  13418. */
  13419. return cp;
  13420. }
  13421. return y;
  13422. }
  13423. /*
  13424. * E5 Section 15.10.2.6 "IsWordChar" abstract operation. Assume
  13425. * x < 0 for characters read outside the string.
  13426. */
  13427. DUK_INTERNAL duk_small_int_t duk_unicode_re_is_wordchar(duk_codepoint_t x) {
  13428. /*
  13429. * Note: the description in E5 Section 15.10.2.6 has a typo, it
  13430. * contains 'A' twice and lacks 'a'; the intent is [0-9a-zA-Z_].
  13431. */
  13432. if ((x >= '0' && x <= '9') ||
  13433. (x >= 'a' && x <= 'z') ||
  13434. (x >= 'A' && x <= 'Z') ||
  13435. (x == '_')) {
  13436. return 1;
  13437. }
  13438. return 0;
  13439. }
  13440. /*
  13441. * Regexp range tables
  13442. */
  13443. /* exposed because lexer needs these too */
  13444. DUK_INTERNAL duk_uint16_t duk_unicode_re_ranges_digit[2] = {
  13445. (duk_uint16_t) 0x0030UL, (duk_uint16_t) 0x0039UL,
  13446. };
  13447. DUK_INTERNAL duk_uint16_t duk_unicode_re_ranges_white[22] = {
  13448. (duk_uint16_t) 0x0009UL, (duk_uint16_t) 0x000DUL,
  13449. (duk_uint16_t) 0x0020UL, (duk_uint16_t) 0x0020UL,
  13450. (duk_uint16_t) 0x00A0UL, (duk_uint16_t) 0x00A0UL,
  13451. (duk_uint16_t) 0x1680UL, (duk_uint16_t) 0x1680UL,
  13452. (duk_uint16_t) 0x180EUL, (duk_uint16_t) 0x180EUL,
  13453. (duk_uint16_t) 0x2000UL, (duk_uint16_t) 0x200AUL,
  13454. (duk_uint16_t) 0x2028UL, (duk_uint16_t) 0x2029UL,
  13455. (duk_uint16_t) 0x202FUL, (duk_uint16_t) 0x202FUL,
  13456. (duk_uint16_t) 0x205FUL, (duk_uint16_t) 0x205FUL,
  13457. (duk_uint16_t) 0x3000UL, (duk_uint16_t) 0x3000UL,
  13458. (duk_uint16_t) 0xFEFFUL, (duk_uint16_t) 0xFEFFUL,
  13459. };
  13460. DUK_INTERNAL duk_uint16_t duk_unicode_re_ranges_wordchar[8] = {
  13461. (duk_uint16_t) 0x0030UL, (duk_uint16_t) 0x0039UL,
  13462. (duk_uint16_t) 0x0041UL, (duk_uint16_t) 0x005AUL,
  13463. (duk_uint16_t) 0x005FUL, (duk_uint16_t) 0x005FUL,
  13464. (duk_uint16_t) 0x0061UL, (duk_uint16_t) 0x007AUL,
  13465. };
  13466. DUK_INTERNAL duk_uint16_t duk_unicode_re_ranges_not_digit[4] = {
  13467. (duk_uint16_t) 0x0000UL, (duk_uint16_t) 0x002FUL,
  13468. (duk_uint16_t) 0x003AUL, (duk_uint16_t) 0xFFFFUL,
  13469. };
  13470. DUK_INTERNAL duk_uint16_t duk_unicode_re_ranges_not_white[24] = {
  13471. (duk_uint16_t) 0x0000UL, (duk_uint16_t) 0x0008UL,
  13472. (duk_uint16_t) 0x000EUL, (duk_uint16_t) 0x001FUL,
  13473. (duk_uint16_t) 0x0021UL, (duk_uint16_t) 0x009FUL,
  13474. (duk_uint16_t) 0x00A1UL, (duk_uint16_t) 0x167FUL,
  13475. (duk_uint16_t) 0x1681UL, (duk_uint16_t) 0x180DUL,
  13476. (duk_uint16_t) 0x180FUL, (duk_uint16_t) 0x1FFFUL,
  13477. (duk_uint16_t) 0x200BUL, (duk_uint16_t) 0x2027UL,
  13478. (duk_uint16_t) 0x202AUL, (duk_uint16_t) 0x202EUL,
  13479. (duk_uint16_t) 0x2030UL, (duk_uint16_t) 0x205EUL,
  13480. (duk_uint16_t) 0x2060UL, (duk_uint16_t) 0x2FFFUL,
  13481. (duk_uint16_t) 0x3001UL, (duk_uint16_t) 0xFEFEUL,
  13482. (duk_uint16_t) 0xFF00UL, (duk_uint16_t) 0xFFFFUL,
  13483. };
  13484. DUK_INTERNAL duk_uint16_t duk_unicode_re_ranges_not_wordchar[10] = {
  13485. (duk_uint16_t) 0x0000UL, (duk_uint16_t) 0x002FUL,
  13486. (duk_uint16_t) 0x003AUL, (duk_uint16_t) 0x0040UL,
  13487. (duk_uint16_t) 0x005BUL, (duk_uint16_t) 0x005EUL,
  13488. (duk_uint16_t) 0x0060UL, (duk_uint16_t) 0x0060UL,
  13489. (duk_uint16_t) 0x007BUL, (duk_uint16_t) 0xFFFFUL,
  13490. };
  13491. #endif /* DUK_USE_REGEXP_SUPPORT */
  13492. #line 1 "duk_util_misc.c"
  13493. /*
  13494. * Misc util stuff
  13495. */
  13496. /* include removed: duk_internal.h */
  13497. /*
  13498. * Lowercase digits for radix values 2 to 36. Also doubles as lowercase
  13499. * hex nybble table.
  13500. */
  13501. DUK_INTERNAL duk_uint8_t duk_lc_digits[36] = {
  13502. '0', '1', '2', '3', '4', '5', '6', '7',
  13503. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
  13504. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  13505. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  13506. 'w', 'x', 'y', 'z'
  13507. };
  13508. DUK_INTERNAL duk_uint8_t duk_uc_nybbles[16] = {
  13509. '0', '1', '2', '3', '4', '5', '6', '7',
  13510. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  13511. };
  13512. /*
  13513. * Table for decoding ASCII hex digits, -1 if invalid.
  13514. */
  13515. DUK_INTERNAL duk_int8_t duk_hex_dectab[256] = {
  13516. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0f */
  13517. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1f */
  13518. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20-0x2f */
  13519. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0x30-0x3f */
  13520. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40-0x4f */
  13521. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5f */
  13522. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x60-0x6f */
  13523. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x70-0x7f */
  13524. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x80-0x8f */
  13525. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x90-0x9f */
  13526. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xa0-0xaf */
  13527. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xb0-0xbf */
  13528. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xc0-0xcf */
  13529. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xd0-0xdf */
  13530. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xe0-0xef */
  13531. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 0xf0-0xff */
  13532. };
  13533. /*
  13534. * Arbitrary byteswap for potentially unaligned values
  13535. *
  13536. * Used to byteswap pointers e.g. in debugger code.
  13537. */
  13538. #if defined(DUK_USE_DEBUGGER_SUPPORT) /* For now only needed by the debugger. */
  13539. DUK_INTERNAL void duk_byteswap_bytes(duk_uint8_t *p, duk_small_uint_t len) {
  13540. duk_uint8_t tmp;
  13541. duk_uint8_t *q = p + len - 1;
  13542. while (p - q < 0) {
  13543. tmp = *p;
  13544. *p = *q;
  13545. *q = tmp;
  13546. p++;
  13547. q--;
  13548. }
  13549. }
  13550. #endif
  13551. #line 1 "duk_util_hashprime.c"
  13552. /*
  13553. * Round a number upwards to a prime (not usually the nearest one).
  13554. *
  13555. * Uses a table of successive 32-bit primes whose ratio is roughly
  13556. * constant. This keeps the relative upwards 'rounding error' bounded
  13557. * and the data size small. A simple 'predict-correct' compression is
  13558. * used to compress primes to one byte per prime. See genhashsizes.py
  13559. * for details.
  13560. *
  13561. * The minimum prime returned here must be coordinated with the possible
  13562. * probe sequence steps in duk_hobject and duk_heap stringtable.
  13563. */
  13564. /* include removed: duk_internal.h */
  13565. /* Awkward inclusion condition: drop out of compilation if not needed by any
  13566. * call site: object hash part or probing stringtable.
  13567. */
  13568. #if defined(DUK_USE_HOBJECT_HASH_PART) || defined(DUK_USE_STRTAB_PROBE)
  13569. /* hash size ratio goal, must match genhashsizes.py */
  13570. #define DUK__HASH_SIZE_RATIO 1177 /* floor(1.15 * (1 << 10)) */
  13571. /* prediction corrections for prime list (see genhashsizes.py) */
  13572. DUK_LOCAL const duk_int8_t duk__hash_size_corrections[] = {
  13573. 17, /* minimum prime */
  13574. 4, 3, 4, 1, 4, 1, 1, 2, 2, 2, 2, 1, 6, 6, 9, 5, 1, 2, 2, 5, 1, 3, 3, 3,
  13575. 5, 4, 4, 2, 4, 8, 3, 4, 23, 2, 4, 7, 8, 11, 2, 12, 15, 10, 1, 1, 5, 1, 5,
  13576. 8, 9, 17, 14, 10, 7, 5, 2, 46, 21, 1, 9, 9, 4, 4, 10, 23, 36, 6, 20, 29,
  13577. 18, 6, 19, 21, 16, 11, 5, 5, 48, 9, 1, 39, 14, 8, 4, 29, 9, 1, 15, 48, 12,
  13578. 22, 6, 15, 27, 4, 2, 17, 28, 8, 9, 4, 5, 8, 3, 3, 8, 37, 11, 15, 8, 30,
  13579. 43, 6, 33, 41, 5, 20, 32, 41, 38, 24, 77, 14, 19, 11, 4, 35, 18, 19, 41,
  13580. 10, 23, 16, 9, 2,
  13581. -1
  13582. };
  13583. /* probe steps (see genhashsizes.py), currently assumed to be 32 entries long
  13584. * (DUK_UTIL_GET_HASH_PROBE_STEP macro).
  13585. */
  13586. DUK_INTERNAL duk_uint8_t duk_util_probe_steps[32] = {
  13587. 2, 3, 5, 7, 11, 13, 19, 31, 41, 47, 59, 67, 73, 79, 89, 101, 103, 107,
  13588. 109, 127, 137, 139, 149, 157, 163, 167, 173, 181, 191, 193, 197, 199
  13589. };
  13590. DUK_INTERNAL duk_uint32_t duk_util_get_hash_prime(duk_uint32_t size) {
  13591. const duk_int8_t *p = duk__hash_size_corrections;
  13592. duk_uint32_t curr;
  13593. curr = (duk_uint32_t) *p++;
  13594. for (;;) {
  13595. duk_small_int_t t = (duk_small_int_t) *p++;
  13596. if (t < 0) {
  13597. /* may happen if size is very close to 2^32-1 */
  13598. break;
  13599. }
  13600. /* prediction: portable variant using doubles if 64-bit values not available */
  13601. #ifdef DUK_USE_64BIT_OPS
  13602. curr = (duk_uint32_t) ((((duk_uint64_t) curr) * ((duk_uint64_t) DUK__HASH_SIZE_RATIO)) >> 10);
  13603. #else
  13604. /* 32-bit x 11-bit = 43-bit, fits accurately into a double */
  13605. curr = (duk_uint32_t) DUK_FLOOR(((double) curr) * ((double) DUK__HASH_SIZE_RATIO) / 1024.0);
  13606. #endif
  13607. /* correction */
  13608. curr += t;
  13609. DUK_DDD(DUK_DDDPRINT("size=%ld, curr=%ld", (long) size, (long) curr));
  13610. if (curr >= size) {
  13611. return curr;
  13612. }
  13613. }
  13614. return 0;
  13615. }
  13616. #endif /* DUK_USE_HOBJECT_HASH_PART || DUK_USE_STRTAB_PROBE */
  13617. #line 1 "duk_hobject_class.c"
  13618. /*
  13619. * Hobject Ecmascript [[Class]].
  13620. */
  13621. /* include removed: duk_internal.h */
  13622. #if (DUK_STRIDX_UC_ARGUMENTS > 255)
  13623. #error constant too large
  13624. #endif
  13625. #if (DUK_STRIDX_ARRAY > 255)
  13626. #error constant too large
  13627. #endif
  13628. #if (DUK_STRIDX_UC_BOOLEAN > 255)
  13629. #error constant too large
  13630. #endif
  13631. #if (DUK_STRIDX_DATE > 255)
  13632. #error constant too large
  13633. #endif
  13634. #if (DUK_STRIDX_UC_ERROR > 255)
  13635. #error constant too large
  13636. #endif
  13637. #if (DUK_STRIDX_UC_FUNCTION > 255)
  13638. #error constant too large
  13639. #endif
  13640. #if (DUK_STRIDX_JSON > 255)
  13641. #error constant too large
  13642. #endif
  13643. #if (DUK_STRIDX_MATH > 255)
  13644. #error constant too large
  13645. #endif
  13646. #if (DUK_STRIDX_UC_NUMBER > 255)
  13647. #error constant too large
  13648. #endif
  13649. #if (DUK_STRIDX_UC_OBJECT > 255)
  13650. #error constant too large
  13651. #endif
  13652. #if (DUK_STRIDX_REG_EXP > 255)
  13653. #error constant too large
  13654. #endif
  13655. #if (DUK_STRIDX_UC_STRING > 255)
  13656. #error constant too large
  13657. #endif
  13658. #if (DUK_STRIDX_GLOBAL > 255)
  13659. #error constant too large
  13660. #endif
  13661. #if (DUK_STRIDX_OBJ_ENV > 255)
  13662. #error constant too large
  13663. #endif
  13664. #if (DUK_STRIDX_DEC_ENV > 255)
  13665. #error constant too large
  13666. #endif
  13667. #if (DUK_STRIDX_UC_BUFFER > 255)
  13668. #error constant too large
  13669. #endif
  13670. #if (DUK_STRIDX_UC_POINTER > 255)
  13671. #error constant too large
  13672. #endif
  13673. #if (DUK_STRIDX_UC_THREAD > 255)
  13674. #error constant too large
  13675. #endif
  13676. #if (DUK_STRIDX_ARRAY_BUFFER > 255)
  13677. #error constant too large
  13678. #endif
  13679. #if (DUK_STRIDX_DATA_VIEW > 255)
  13680. #error constant too large
  13681. #endif
  13682. #if (DUK_STRIDX_INT8_ARRAY > 255)
  13683. #error constant too large
  13684. #endif
  13685. #if (DUK_STRIDX_UINT8_ARRAY > 255)
  13686. #error constant too large
  13687. #endif
  13688. #if (DUK_STRIDX_UINT8_CLAMPED_ARRAY > 255)
  13689. #error constant too large
  13690. #endif
  13691. #if (DUK_STRIDX_INT16_ARRAY > 255)
  13692. #error constant too large
  13693. #endif
  13694. #if (DUK_STRIDX_UINT16_ARRAY > 255)
  13695. #error constant too large
  13696. #endif
  13697. #if (DUK_STRIDX_INT32_ARRAY > 255)
  13698. #error constant too large
  13699. #endif
  13700. #if (DUK_STRIDX_UINT32_ARRAY > 255)
  13701. #error constant too large
  13702. #endif
  13703. #if (DUK_STRIDX_FLOAT32_ARRAY > 255)
  13704. #error constant too large
  13705. #endif
  13706. #if (DUK_STRIDX_FLOAT64_ARRAY > 255)
  13707. #error constant too large
  13708. #endif
  13709. #if (DUK_STRIDX_EMPTY_STRING > 255)
  13710. #error constant too large
  13711. #endif
  13712. /* Note: assumes that these string indexes are 8-bit, genstrings.py must ensure that */
  13713. DUK_INTERNAL duk_uint8_t duk_class_number_to_stridx[32] = {
  13714. DUK_STRIDX_EMPTY_STRING, /* UNUSED, intentionally empty */
  13715. DUK_STRIDX_UC_ARGUMENTS,
  13716. DUK_STRIDX_ARRAY,
  13717. DUK_STRIDX_UC_BOOLEAN,
  13718. DUK_STRIDX_DATE,
  13719. DUK_STRIDX_UC_ERROR,
  13720. DUK_STRIDX_UC_FUNCTION,
  13721. DUK_STRIDX_JSON,
  13722. DUK_STRIDX_MATH,
  13723. DUK_STRIDX_UC_NUMBER,
  13724. DUK_STRIDX_UC_OBJECT,
  13725. DUK_STRIDX_REG_EXP,
  13726. DUK_STRIDX_UC_STRING,
  13727. DUK_STRIDX_GLOBAL,
  13728. DUK_STRIDX_OBJ_ENV,
  13729. DUK_STRIDX_DEC_ENV,
  13730. DUK_STRIDX_UC_BUFFER,
  13731. DUK_STRIDX_UC_POINTER,
  13732. DUK_STRIDX_UC_THREAD,
  13733. DUK_STRIDX_ARRAY_BUFFER,
  13734. DUK_STRIDX_DATA_VIEW,
  13735. DUK_STRIDX_INT8_ARRAY,
  13736. DUK_STRIDX_UINT8_ARRAY,
  13737. DUK_STRIDX_UINT8_CLAMPED_ARRAY,
  13738. DUK_STRIDX_INT16_ARRAY,
  13739. DUK_STRIDX_UINT16_ARRAY,
  13740. DUK_STRIDX_INT32_ARRAY,
  13741. DUK_STRIDX_UINT32_ARRAY,
  13742. DUK_STRIDX_FLOAT32_ARRAY,
  13743. DUK_STRIDX_FLOAT64_ARRAY,
  13744. DUK_STRIDX_EMPTY_STRING, /* UNUSED, intentionally empty */
  13745. DUK_STRIDX_EMPTY_STRING, /* UNUSED, intentionally empty */
  13746. };
  13747. #line 1 "duk_alloc_default.c"
  13748. /*
  13749. * Default allocation functions.
  13750. *
  13751. * Assumes behavior such as malloc allowing zero size, yielding
  13752. * a NULL or a unique pointer which is a no-op for free.
  13753. */
  13754. /* include removed: duk_internal.h */
  13755. DUK_INTERNAL void *duk_default_alloc_function(void *udata, duk_size_t size) {
  13756. void *res;
  13757. DUK_UNREF(udata);
  13758. res = DUK_ANSI_MALLOC(size);
  13759. DUK_DDD(DUK_DDDPRINT("default alloc function: %lu -> %p",
  13760. (unsigned long) size, (void *) res));
  13761. return res;
  13762. }
  13763. DUK_INTERNAL void *duk_default_realloc_function(void *udata, void *ptr, duk_size_t newsize) {
  13764. void *res;
  13765. DUK_UNREF(udata);
  13766. res = DUK_ANSI_REALLOC(ptr, newsize);
  13767. DUK_DDD(DUK_DDDPRINT("default realloc function: %p %lu -> %p",
  13768. (void *) ptr, (unsigned long) newsize, (void *) res));
  13769. return res;
  13770. }
  13771. DUK_INTERNAL void duk_default_free_function(void *udata, void *ptr) {
  13772. DUK_DDD(DUK_DDDPRINT("default free function: %p", (void *) ptr));
  13773. DUK_UNREF(udata);
  13774. DUK_ANSI_FREE(ptr);
  13775. }
  13776. #line 1 "duk_api_buffer.c"
  13777. /*
  13778. * Buffer
  13779. */
  13780. /* include removed: duk_internal.h */
  13781. DUK_EXTERNAL void *duk_resize_buffer(duk_context *ctx, duk_idx_t index, duk_size_t new_size) {
  13782. duk_hthread *thr = (duk_hthread *) ctx;
  13783. duk_hbuffer_dynamic *h;
  13784. DUK_ASSERT_CTX_VALID(ctx);
  13785. h = (duk_hbuffer_dynamic *) duk_require_hbuffer(ctx, index);
  13786. DUK_ASSERT(h != NULL);
  13787. if (!(DUK_HBUFFER_HAS_DYNAMIC(h) && !DUK_HBUFFER_HAS_EXTERNAL(h))) {
  13788. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_WRONG_BUFFER_TYPE);
  13789. }
  13790. /* maximum size check is handled by callee */
  13791. duk_hbuffer_resize(thr, h, new_size);
  13792. return DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h);
  13793. }
  13794. DUK_EXTERNAL void *duk_steal_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) {
  13795. duk_hthread *thr = (duk_hthread *) ctx;
  13796. duk_hbuffer_dynamic *h;
  13797. void *ptr;
  13798. duk_size_t sz;
  13799. DUK_ASSERT(ctx != NULL);
  13800. h = (duk_hbuffer_dynamic *) duk_require_hbuffer(ctx, index);
  13801. DUK_ASSERT(h != NULL);
  13802. if (!(DUK_HBUFFER_HAS_DYNAMIC(h) && !DUK_HBUFFER_HAS_EXTERNAL(h))) {
  13803. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_WRONG_BUFFER_TYPE);
  13804. }
  13805. /* Forget the previous allocation, setting size to 0 and alloc to
  13806. * NULL. Caller is responsible for freeing the previous allocation.
  13807. * Getting the allocation and clearing it is done in the same API
  13808. * call to avoid any chance of a realloc.
  13809. */
  13810. ptr = DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h);
  13811. sz = DUK_HBUFFER_DYNAMIC_GET_SIZE(h);
  13812. if (out_size) {
  13813. *out_size = sz;
  13814. }
  13815. DUK_HBUFFER_DYNAMIC_SET_DATA_PTR_NULL(thr->heap, h);
  13816. DUK_HBUFFER_DYNAMIC_SET_SIZE(h, 0);
  13817. return ptr;
  13818. }
  13819. DUK_EXTERNAL void duk_config_buffer(duk_context *ctx, duk_idx_t index, void *ptr, duk_size_t len) {
  13820. duk_hthread *thr = (duk_hthread *) ctx;
  13821. duk_hbuffer_external *h;
  13822. DUK_ASSERT(ctx != NULL);
  13823. h = (duk_hbuffer_external *) duk_require_hbuffer(ctx, index);
  13824. DUK_ASSERT(h != NULL);
  13825. if (!DUK_HBUFFER_HAS_EXTERNAL(h)) {
  13826. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_WRONG_BUFFER_TYPE);
  13827. }
  13828. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(h));
  13829. DUK_HBUFFER_EXTERNAL_SET_DATA_PTR(thr->heap, h, ptr);
  13830. DUK_HBUFFER_EXTERNAL_SET_SIZE(h, len);
  13831. }
  13832. #line 1 "duk_api_bytecode.c"
  13833. /*
  13834. * Bytecode dump/load
  13835. *
  13836. * The bytecode load primitive is more important performance-wise than the
  13837. * dump primitive.
  13838. *
  13839. * Unlike most Duktape API calls, bytecode dump/load is not guaranteed to be
  13840. * memory safe for invalid arguments - caller beware! There's little point
  13841. * in trying to achieve memory safety unless bytecode instructions are also
  13842. * validated which is not easy to do with indirect register references etc.
  13843. */
  13844. /* include removed: duk_internal.h */
  13845. #if defined(DUK_USE_BYTECODE_DUMP_SUPPORT)
  13846. #define DUK__SER_MARKER 0xff
  13847. #define DUK__SER_VERSION 0x00
  13848. #define DUK__SER_STRING 0x00
  13849. #define DUK__SER_NUMBER 0x01
  13850. #define DUK__BYTECODE_INITIAL_ALLOC 256
  13851. /*
  13852. * Dump/load helpers, xxx_raw() helpers do no buffer checks
  13853. */
  13854. DUK_LOCAL duk_uint8_t *duk__load_string_raw(duk_context *ctx, duk_uint8_t *p) {
  13855. duk_uint32_t len;
  13856. len = DUK_RAW_READ_U32_BE(p);
  13857. duk_push_lstring(ctx, (const char *) p, len);
  13858. p += len;
  13859. return p;
  13860. }
  13861. DUK_LOCAL duk_uint8_t *duk__load_buffer_raw(duk_context *ctx, duk_uint8_t *p) {
  13862. duk_uint32_t len;
  13863. duk_uint8_t *buf;
  13864. len = DUK_RAW_READ_U32_BE(p);
  13865. buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) len);
  13866. DUK_ASSERT(buf != NULL);
  13867. DUK_MEMCPY((void *) buf, (const void *) p, (size_t) len);
  13868. p += len;
  13869. return p;
  13870. }
  13871. DUK_LOCAL duk_uint8_t *duk__dump_hstring_raw(duk_uint8_t *p, duk_hstring *h) {
  13872. duk_size_t len;
  13873. duk_uint32_t tmp32;
  13874. DUK_ASSERT(h != NULL);
  13875. len = DUK_HSTRING_GET_BYTELEN(h);
  13876. DUK_ASSERT(len <= 0xffffffffUL); /* string limits */
  13877. tmp32 = (duk_uint32_t) len;
  13878. DUK_RAW_WRITE_U32_BE(p, tmp32);
  13879. DUK_MEMCPY((void *) p,
  13880. (const void *) DUK_HSTRING_GET_DATA(h),
  13881. len);
  13882. p += len;
  13883. return p;
  13884. }
  13885. DUK_LOCAL duk_uint8_t *duk__dump_hbuffer_raw(duk_hthread *thr, duk_uint8_t *p, duk_hbuffer *h) {
  13886. duk_size_t len;
  13887. duk_uint32_t tmp32;
  13888. DUK_ASSERT(thr != NULL);
  13889. DUK_ASSERT(h != NULL);
  13890. DUK_UNREF(thr);
  13891. len = DUK_HBUFFER_GET_SIZE(h);
  13892. DUK_ASSERT(len <= 0xffffffffUL); /* buffer limits */
  13893. tmp32 = (duk_uint32_t) len;
  13894. DUK_RAW_WRITE_U32_BE(p, tmp32);
  13895. DUK_MEMCPY((void *) p,
  13896. (const void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h),
  13897. len);
  13898. p += len;
  13899. return p;
  13900. }
  13901. 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) {
  13902. duk_hstring *h_str;
  13903. duk_tval *tv;
  13904. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_GET_STRING(thr, stridx));
  13905. if (tv != NULL && DUK_TVAL_IS_STRING(tv)) {
  13906. h_str = DUK_TVAL_GET_STRING(tv);
  13907. DUK_ASSERT(h_str != NULL);
  13908. } else {
  13909. h_str = DUK_HTHREAD_STRING_EMPTY_STRING(thr);
  13910. DUK_ASSERT(h_str != NULL);
  13911. }
  13912. DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */
  13913. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4 + DUK_HSTRING_GET_BYTELEN(h_str), p);
  13914. p = duk__dump_hstring_raw(p, h_str);
  13915. return p;
  13916. }
  13917. 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) {
  13918. duk_tval *tv;
  13919. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_GET_STRING(thr, stridx));
  13920. if (tv != NULL && DUK_TVAL_IS_BUFFER(tv)) {
  13921. duk_hbuffer *h_buf;
  13922. h_buf = DUK_TVAL_GET_BUFFER(tv);
  13923. DUK_ASSERT(h_buf != NULL);
  13924. DUK_ASSERT(DUK_HBUFFER_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */
  13925. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4 + DUK_HBUFFER_GET_SIZE(h_buf), p);
  13926. p = duk__dump_hbuffer_raw(thr, p, h_buf);
  13927. } else {
  13928. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4, p);
  13929. DUK_RAW_WRITE_U32_BE(p, 0);
  13930. }
  13931. return p;
  13932. }
  13933. 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) {
  13934. duk_tval *tv;
  13935. duk_uint32_t val;
  13936. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_GET_STRING(thr, stridx));
  13937. if (tv != NULL && DUK_TVAL_IS_NUMBER(tv)) {
  13938. val = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv);
  13939. } else {
  13940. val = def_value;
  13941. }
  13942. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4, p);
  13943. DUK_RAW_WRITE_U32_BE(p, val);
  13944. return p;
  13945. }
  13946. DUK_LOCAL duk_uint8_t *duk__dump_varmap(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func) {
  13947. duk_tval *tv;
  13948. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_STRING_INT_VARMAP(thr));
  13949. if (tv != NULL && DUK_TVAL_IS_OBJECT(tv)) {
  13950. duk_hobject *h;
  13951. duk_uint_fast32_t i;
  13952. h = DUK_TVAL_GET_OBJECT(tv);
  13953. DUK_ASSERT(h != NULL);
  13954. /* We know _Varmap only has own properties so walk property
  13955. * table directly. We also know _Varmap is dense and all
  13956. * values are numbers; assert for these. GC and finalizers
  13957. * shouldn't affect _Varmap so side effects should be fine.
  13958. */
  13959. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(h); i++) {
  13960. duk_hstring *key;
  13961. duk_tval *tv_val;
  13962. duk_uint32_t val;
  13963. key = DUK_HOBJECT_E_GET_KEY(thr->heap, h, i);
  13964. DUK_ASSERT(key != NULL); /* _Varmap is dense */
  13965. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, h, i));
  13966. tv_val = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, h, i);
  13967. DUK_ASSERT(tv_val != NULL);
  13968. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_val)); /* known to be number; in fact an integer */
  13969. #if defined(DUK_USE_FASTINT)
  13970. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv_val));
  13971. DUK_ASSERT(DUK_TVAL_GET_FASTINT(tv_val) == (duk_int64_t) DUK_TVAL_GET_FASTINT_U32(tv_val)); /* known to be 32-bit */
  13972. val = DUK_TVAL_GET_FASTINT_U32(tv_val);
  13973. #else
  13974. val = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv_val);
  13975. #endif
  13976. DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */
  13977. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4 + DUK_HSTRING_GET_BYTELEN(key) + 4, p);
  13978. p = duk__dump_hstring_raw(p, key);
  13979. DUK_RAW_WRITE_U32_BE(p, val);
  13980. }
  13981. }
  13982. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4, p);
  13983. DUK_RAW_WRITE_U32_BE(p, 0); /* end of _Varmap */
  13984. return p;
  13985. }
  13986. DUK_LOCAL duk_uint8_t *duk__dump_formals(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func) {
  13987. duk_tval *tv;
  13988. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_STRING_INT_FORMALS(thr));
  13989. if (tv != NULL && DUK_TVAL_IS_OBJECT(tv)) {
  13990. duk_hobject *h;
  13991. duk_uint_fast32_t i;
  13992. h = DUK_TVAL_GET_OBJECT(tv);
  13993. DUK_ASSERT(h != NULL);
  13994. /* We know _Formals is dense and all entries will be in the
  13995. * array part. GC and finalizers shouldn't affect _Formals
  13996. * so side effects should be fine.
  13997. */
  13998. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(h); i++) {
  13999. duk_tval *tv_val;
  14000. duk_hstring *varname;
  14001. tv_val = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, h, i);
  14002. DUK_ASSERT(tv_val != NULL);
  14003. if (DUK_TVAL_IS_STRING(tv_val)) {
  14004. /* Array is dense and contains only strings, but ASIZE may
  14005. * be larger than used part and there are UNUSED entries.
  14006. */
  14007. varname = DUK_TVAL_GET_STRING(tv_val);
  14008. DUK_ASSERT(varname != NULL);
  14009. DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */
  14010. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4 + DUK_HSTRING_GET_BYTELEN(varname), p);
  14011. p = duk__dump_hstring_raw(p, varname);
  14012. }
  14013. }
  14014. }
  14015. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4, p);
  14016. DUK_RAW_WRITE_U32_BE(p, 0); /* end of _Formals */
  14017. return p;
  14018. }
  14019. static duk_uint8_t *duk__dump_func(duk_context *ctx, duk_hcompiledfunction *func, duk_bufwriter_ctx *bw_ctx, duk_uint8_t *p) {
  14020. duk_hthread *thr;
  14021. duk_tval *tv, *tv_end;
  14022. duk_instr_t *ins, *ins_end;
  14023. duk_hobject **fn, **fn_end;
  14024. duk_hstring *h_str;
  14025. duk_uint32_t count_instr;
  14026. duk_uint32_t tmp32;
  14027. duk_uint16_t tmp16;
  14028. duk_double_t d;
  14029. thr = (duk_hthread *) ctx;
  14030. DUK_UNREF(ctx);
  14031. DUK_UNREF(thr);
  14032. DUK_DD(DUK_DDPRINT("dumping function %p to %p: "
  14033. "consts=[%p,%p[ (%ld bytes, %ld items), "
  14034. "funcs=[%p,%p[ (%ld bytes, %ld items), "
  14035. "code=[%p,%p[ (%ld bytes, %ld items)",
  14036. (void *) func,
  14037. (void *) p,
  14038. (void *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, func),
  14039. (void *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_END(thr->heap, func),
  14040. (long) DUK_HCOMPILEDFUNCTION_GET_CONSTS_SIZE(thr->heap, func),
  14041. (long) DUK_HCOMPILEDFUNCTION_GET_CONSTS_COUNT(thr->heap, func),
  14042. (void *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, func),
  14043. (void *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(thr->heap, func),
  14044. (long) DUK_HCOMPILEDFUNCTION_GET_FUNCS_SIZE(thr->heap, func),
  14045. (long) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(thr->heap, func),
  14046. (void *) DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, func),
  14047. (void *) DUK_HCOMPILEDFUNCTION_GET_CODE_END(thr->heap, func),
  14048. (long) DUK_HCOMPILEDFUNCTION_GET_CODE_SIZE(thr->heap, func),
  14049. (long) DUK_HCOMPILEDFUNCTION_GET_CODE_COUNT(thr->heap, func)));
  14050. DUK_ASSERT(DUK_USE_ESBC_MAX_BYTES <= 0x7fffffffUL); /* ensures no overflow */
  14051. count_instr = (duk_uint32_t) DUK_HCOMPILEDFUNCTION_GET_CODE_COUNT(thr->heap, func);
  14052. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 3 * 4 + 2 * 2 + 3 * 4 + count_instr * 4, p);
  14053. /* Fixed header info. */
  14054. tmp32 = count_instr;
  14055. DUK_RAW_WRITE_U32_BE(p, tmp32);
  14056. tmp32 = (duk_uint32_t) DUK_HCOMPILEDFUNCTION_GET_CONSTS_COUNT(thr->heap, func);
  14057. DUK_RAW_WRITE_U32_BE(p, tmp32);
  14058. tmp32 = (duk_uint32_t) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(thr->heap, func);
  14059. DUK_RAW_WRITE_U32_BE(p, tmp32);
  14060. tmp16 = func->nregs;
  14061. DUK_RAW_WRITE_U16_BE(p, tmp16);
  14062. tmp16 = func->nargs;
  14063. DUK_RAW_WRITE_U16_BE(p, tmp16);
  14064. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  14065. tmp32 = func->start_line;
  14066. DUK_RAW_WRITE_U32_BE(p, tmp32);
  14067. tmp32 = func->end_line;
  14068. DUK_RAW_WRITE_U32_BE(p, tmp32);
  14069. #else
  14070. DUK_RAW_WRITE_U32_BE(p, 0);
  14071. DUK_RAW_WRITE_U32_BE(p, 0);
  14072. #endif
  14073. tmp32 = ((duk_heaphdr *) func)->h_flags & DUK_HEAPHDR_FLAGS_FLAG_MASK;
  14074. DUK_RAW_WRITE_U32_BE(p, tmp32);
  14075. /* Bytecode instructions: endian conversion needed unless
  14076. * platform is big endian.
  14077. */
  14078. ins = DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, func);
  14079. ins_end = DUK_HCOMPILEDFUNCTION_GET_CODE_END(thr->heap, func);
  14080. DUK_ASSERT((duk_size_t) (ins_end - ins) == (duk_size_t) count_instr);
  14081. #if defined(DUK_USE_INTEGER_BE)
  14082. DUK_MEMCPY((void *) p, (const void *) ins, (size_t) (ins_end - ins));
  14083. p += (size_t) (ins_end - ins);
  14084. #else
  14085. while (ins != ins_end) {
  14086. tmp32 = (duk_uint32_t) (*ins);
  14087. DUK_RAW_WRITE_U32_BE(p, tmp32);
  14088. ins++;
  14089. }
  14090. #endif
  14091. /* Constants: variable size encoding. */
  14092. tv = DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, func);
  14093. tv_end = DUK_HCOMPILEDFUNCTION_GET_CONSTS_END(thr->heap, func);
  14094. while (tv != tv_end) {
  14095. /* constants are strings or numbers now */
  14096. DUK_ASSERT(DUK_TVAL_IS_STRING(tv) ||
  14097. DUK_TVAL_IS_NUMBER(tv));
  14098. if (DUK_TVAL_IS_STRING(tv)) {
  14099. h_str = DUK_TVAL_GET_STRING(tv);
  14100. DUK_ASSERT(h_str != NULL);
  14101. DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */
  14102. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 1 + 4 + DUK_HSTRING_GET_BYTELEN(h_str), p),
  14103. *p++ = DUK__SER_STRING;
  14104. p = duk__dump_hstring_raw(p, h_str);
  14105. } else {
  14106. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  14107. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 1 + 8, p);
  14108. *p++ = DUK__SER_NUMBER;
  14109. d = DUK_TVAL_GET_NUMBER(tv);
  14110. DUK_RAW_WRITE_DOUBLE_BE(p, d);
  14111. }
  14112. tv++;
  14113. }
  14114. /* Inner functions recursively. */
  14115. fn = (duk_hobject **) DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, func);
  14116. fn_end = (duk_hobject **) DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(thr->heap, func);
  14117. while (fn != fn_end) {
  14118. /* XXX: This causes recursion up to inner function depth
  14119. * which is normally not an issue, e.g. mark-and-sweep uses
  14120. * a recursion limiter to avoid C stack issues. Avoiding
  14121. * this would mean some sort of a work list or just refusing
  14122. * to serialize deep functions.
  14123. */
  14124. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(*fn));
  14125. p = duk__dump_func(ctx, (duk_hcompiledfunction *) *fn, bw_ctx, p);
  14126. fn++;
  14127. }
  14128. /* Object extra properties.
  14129. *
  14130. * There are some difference between function templates and functions.
  14131. * For example, function templates don't have .length and nargs is
  14132. * normally used to instantiate the functions.
  14133. */
  14134. p = duk__dump_uint32_prop(thr, p, bw_ctx, (duk_hobject *) func, DUK_STRIDX_LENGTH, (duk_uint32_t) func->nargs);
  14135. p = duk__dump_string_prop(thr, p, bw_ctx, (duk_hobject *) func, DUK_STRIDX_NAME);
  14136. p = duk__dump_string_prop(thr, p, bw_ctx, (duk_hobject *) func, DUK_STRIDX_FILE_NAME);
  14137. p = duk__dump_buffer_prop(thr, p, bw_ctx, (duk_hobject *) func, DUK_STRIDX_INT_PC2LINE);
  14138. p = duk__dump_varmap(thr, p, bw_ctx, (duk_hobject *) func);
  14139. p = duk__dump_formals(thr, p, bw_ctx, (duk_hobject *) func);
  14140. DUK_DD(DUK_DDPRINT("serialized function %p -> final pointer %p", (void *) func, (void *) p));
  14141. return p;
  14142. }
  14143. /* Load a function from bytecode. The function object returned here must
  14144. * match what is created by duk_js_push_closure() with respect to its flags,
  14145. * properties, etc.
  14146. *
  14147. * NOTE: there are intentionally no input buffer length / bound checks.
  14148. * Adding them would be easy but wouldn't ensure memory safety as untrusted
  14149. * or broken bytecode is unsafe during execution unless the opcodes themselves
  14150. * are validated (which is quite complex, especially for indirect opcodes).
  14151. */
  14152. #define DUK__ASSERT_LEFT(n) do { \
  14153. DUK_ASSERT((duk_size_t) (p_end - p) >= (duk_size_t) (n)); \
  14154. } while (0)
  14155. static duk_uint8_t *duk__load_func(duk_context *ctx, duk_uint8_t *p, duk_uint8_t *p_end) {
  14156. duk_hthread *thr;
  14157. duk_hcompiledfunction *h_fun;
  14158. duk_hbuffer *h_data;
  14159. duk_size_t data_size;
  14160. duk_uint32_t count_instr, count_const, count_funcs;
  14161. duk_uint32_t n;
  14162. duk_uint32_t tmp32;
  14163. duk_small_uint_t const_type;
  14164. duk_uint8_t *fun_data;
  14165. duk_uint8_t *q;
  14166. duk_idx_t idx_base;
  14167. duk_tval *tv1;
  14168. duk_uarridx_t arr_idx;
  14169. /* XXX: There's some overlap with duk_js_closure() here, but
  14170. * seems difficult to share code. Ensure that the final function
  14171. * looks the same as created by duk_js_closure().
  14172. */
  14173. DUK_ASSERT(ctx != NULL);
  14174. thr = (duk_hthread *) ctx;
  14175. DUK_DD(DUK_DDPRINT("loading function, p=%p, p_end=%p", (void *) p, (void *) p_end));
  14176. DUK__ASSERT_LEFT(3 * 4);
  14177. count_instr = DUK_RAW_READ_U32_BE(p);
  14178. count_const = DUK_RAW_READ_U32_BE(p);
  14179. count_funcs = DUK_RAW_READ_U32_BE(p);
  14180. data_size = sizeof(duk_tval) * count_const +
  14181. sizeof(duk_hobject *) * count_funcs +
  14182. sizeof(duk_instr_t) * count_instr;
  14183. DUK_DD(DUK_DDPRINT("instr=%ld, const=%ld, funcs=%ld, data_size=%ld",
  14184. (long) count_instr, (long) count_const,
  14185. (long) count_const, (long) data_size));
  14186. /* Value stack is used to ensure reachability of constants and
  14187. * inner functions being loaded. Require enough space to handle
  14188. * large functions correctly.
  14189. */
  14190. duk_require_stack(ctx, 2 + count_const + count_funcs);
  14191. idx_base = duk_get_top(ctx);
  14192. /* Push function object, init flags etc. This must match
  14193. * duk_js_push_closure() quite carefully.
  14194. */
  14195. duk_push_compiledfunction(ctx);
  14196. h_fun = duk_get_hcompiledfunction(ctx, -1);
  14197. DUK_ASSERT(h_fun != NULL);
  14198. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) h_fun));
  14199. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, h_fun) == NULL);
  14200. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_FUNCS(thr->heap, h_fun) == NULL);
  14201. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_BYTECODE(thr->heap, h_fun) == NULL);
  14202. h_fun->nregs = DUK_RAW_READ_U16_BE(p);
  14203. h_fun->nargs = DUK_RAW_READ_U16_BE(p);
  14204. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  14205. h_fun->start_line = DUK_RAW_READ_U32_BE(p);
  14206. h_fun->end_line = DUK_RAW_READ_U32_BE(p);
  14207. #else
  14208. p += 8; /* skip line info */
  14209. #endif
  14210. /* duk_hcompiledfunction flags; quite version specific */
  14211. tmp32 = DUK_RAW_READ_U32_BE(p);
  14212. DUK_HEAPHDR_SET_FLAGS((duk_heaphdr *) h_fun, tmp32);
  14213. /* standard prototype */
  14214. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, &h_fun->obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  14215. /* assert just a few critical flags */
  14216. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) h_fun) == DUK_HTYPE_OBJECT);
  14217. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(&h_fun->obj));
  14218. DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(&h_fun->obj));
  14219. DUK_ASSERT(!DUK_HOBJECT_HAS_NATIVEFUNCTION(&h_fun->obj));
  14220. DUK_ASSERT(!DUK_HOBJECT_HAS_THREAD(&h_fun->obj));
  14221. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARRAY(&h_fun->obj));
  14222. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(&h_fun->obj));
  14223. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(&h_fun->obj));
  14224. /* Create function 'data' buffer but don't attach it yet. */
  14225. fun_data = (duk_uint8_t *) duk_push_fixed_buffer(ctx, data_size);
  14226. DUK_ASSERT(fun_data != NULL);
  14227. /* Load bytecode instructions. */
  14228. DUK_ASSERT(sizeof(duk_instr_t) == 4);
  14229. DUK__ASSERT_LEFT(count_instr * sizeof(duk_instr_t));
  14230. #if defined(DUK_USE_INTEGER_BE)
  14231. q = fun_data + sizeof(duk_tval) * count_const + sizeof(duk_hobject *) * count_funcs;
  14232. DUK_MEMCPY((void *) q,
  14233. (const void *) p,
  14234. sizeof(duk_instr_t) * count_instr);
  14235. p += sizeof(duk_instr_t) * count_instr;
  14236. #else
  14237. q = fun_data + sizeof(duk_tval) * count_const + sizeof(duk_hobject *) * count_funcs;
  14238. for (n = count_instr; n > 0; n--) {
  14239. *((duk_instr_t *) (void *) q) = DUK_RAW_READ_U32_BE(p);
  14240. q += sizeof(duk_instr_t);
  14241. }
  14242. #endif
  14243. /* Load constants onto value stack but don't yet copy to buffer. */
  14244. for (n = count_const; n > 0; n--) {
  14245. DUK__ASSERT_LEFT(1);
  14246. const_type = DUK_RAW_READ_U8(p);
  14247. switch (const_type) {
  14248. case DUK__SER_STRING: {
  14249. p = duk__load_string_raw(ctx, p);
  14250. break;
  14251. }
  14252. case DUK__SER_NUMBER: {
  14253. /* Important to do a fastint check so that constants are
  14254. * properly read back as fastints.
  14255. */
  14256. duk_tval tv_tmp;
  14257. duk_double_t val;
  14258. DUK__ASSERT_LEFT(8);
  14259. val = DUK_RAW_READ_DOUBLE_BE(p);
  14260. DUK_TVAL_SET_NUMBER_CHKFAST(&tv_tmp, val);
  14261. duk_push_tval(ctx, &tv_tmp);
  14262. break;
  14263. }
  14264. default: {
  14265. goto format_error;
  14266. }
  14267. }
  14268. }
  14269. /* Load inner functions to value stack, but don't yet copy to buffer. */
  14270. for (n = count_funcs; n > 0; n--) {
  14271. p = duk__load_func(ctx, p, p_end);
  14272. if (p == NULL) {
  14273. goto format_error;
  14274. }
  14275. }
  14276. /* With constants and inner functions on value stack, we can now
  14277. * atomically finish the function 'data' buffer, bump refcounts,
  14278. * etc.
  14279. *
  14280. * Here we take advantage of the value stack being just a duk_tval
  14281. * array: we can just memcpy() the constants as long as we incref
  14282. * them afterwards.
  14283. */
  14284. h_data = (duk_hbuffer *) duk_get_hbuffer(ctx, idx_base + 1);
  14285. DUK_ASSERT(h_data != NULL);
  14286. DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC(h_data));
  14287. DUK_HCOMPILEDFUNCTION_SET_DATA(thr->heap, h_fun, h_data);
  14288. DUK_HBUFFER_INCREF(thr, h_data);
  14289. tv1 = duk_get_tval(ctx, idx_base + 2); /* may be NULL if no constants or inner funcs */
  14290. DUK_ASSERT((count_const == 0 && count_funcs == 0) || tv1 != NULL);
  14291. q = fun_data;
  14292. if (count_const > 0) {
  14293. /* Explicit zero size check to avoid NULL 'tv1'. */
  14294. DUK_MEMCPY((void *) q, (const void *) tv1, sizeof(duk_tval) * count_const);
  14295. for (n = count_const; n > 0; n--) {
  14296. DUK_TVAL_INCREF_FAST(thr, (duk_tval *) (void *) q); /* no side effects */
  14297. q += sizeof(duk_tval);
  14298. }
  14299. tv1 += count_const;
  14300. }
  14301. DUK_HCOMPILEDFUNCTION_SET_FUNCS(thr->heap, h_fun, (duk_hobject **) (void *) q);
  14302. for (n = count_funcs; n > 0; n--) {
  14303. duk_hobject *h_obj;
  14304. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1));
  14305. h_obj = DUK_TVAL_GET_OBJECT(tv1);
  14306. DUK_ASSERT(h_obj != NULL);
  14307. tv1++;
  14308. DUK_HOBJECT_INCREF(thr, h_obj);
  14309. *((duk_hobject **) (void *) q) = h_obj;
  14310. q += sizeof(duk_hobject *);
  14311. }
  14312. DUK_HCOMPILEDFUNCTION_SET_BYTECODE(thr->heap, h_fun, (duk_instr_t *) (void *) q);
  14313. /* The function object is now reachable and refcounts are fine,
  14314. * so we can pop off all the temporaries.
  14315. */
  14316. DUK_DDD(DUK_DDDPRINT("function is reachable, reset top; func: %!iT", duk_get_tval(ctx, idx_base)));
  14317. duk_set_top(ctx, idx_base + 1);
  14318. /* Setup function properties. */
  14319. tmp32 = DUK_RAW_READ_U32_BE(p);
  14320. duk_push_u32(ctx, tmp32);
  14321. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE);
  14322. p = duk__load_string_raw(ctx, p);
  14323. if (DUK_HOBJECT_HAS_NAMEBINDING((duk_hobject *) h_fun)) {
  14324. /* Original function instance/template had NAMEBINDING.
  14325. * Must create a lexical environment on loading to allow
  14326. * recursive functions like 'function foo() { foo(); }'.
  14327. */
  14328. duk_hobject *proto;
  14329. proto = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  14330. (void) duk_push_object_helper_proto(ctx,
  14331. DUK_HOBJECT_FLAG_EXTENSIBLE |
  14332. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV),
  14333. proto);
  14334. duk_dup(ctx, -2); /* -> [ func funcname env funcname ] */
  14335. duk_dup(ctx, idx_base); /* -> [ func funcname env funcname func ] */
  14336. duk_xdef_prop(ctx, -3, DUK_PROPDESC_FLAGS_NONE); /* -> [ func funcname env ] */
  14337. duk_xdef_prop_stridx(ctx, idx_base, DUK_STRIDX_INT_LEXENV, DUK_PROPDESC_FLAGS_WC);
  14338. /* since closure has NEWENV, never define DUK_STRIDX_INT_VARENV, as it
  14339. * will be ignored anyway
  14340. */
  14341. }
  14342. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE);
  14343. p = duk__load_string_raw(ctx, p);
  14344. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC);
  14345. duk_push_object(ctx);
  14346. duk_dup(ctx, -2);
  14347. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_CONSTRUCTOR, DUK_PROPDESC_FLAGS_WC); /* func.prototype.constructor = func */
  14348. duk_compact(ctx, -1);
  14349. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_PROTOTYPE, DUK_PROPDESC_FLAGS_W);
  14350. p = duk__load_buffer_raw(ctx, p);
  14351. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_PC2LINE, DUK_PROPDESC_FLAGS_WC);
  14352. duk_push_object(ctx); /* _Varmap */
  14353. for (;;) {
  14354. /* XXX: awkward */
  14355. p = duk__load_string_raw(ctx, p);
  14356. if (duk_get_length(ctx, -1) == 0) {
  14357. duk_pop(ctx);
  14358. break;
  14359. }
  14360. tmp32 = DUK_RAW_READ_U32_BE(p);
  14361. duk_push_u32(ctx, tmp32);
  14362. duk_put_prop(ctx, -3);
  14363. }
  14364. duk_compact(ctx, -1);
  14365. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VARMAP, DUK_PROPDESC_FLAGS_NONE);
  14366. duk_push_array(ctx); /* _Formals */
  14367. for (arr_idx = 0; ; arr_idx++) {
  14368. /* XXX: awkward */
  14369. p = duk__load_string_raw(ctx, p);
  14370. if (duk_get_length(ctx, -1) == 0) {
  14371. duk_pop(ctx);
  14372. break;
  14373. }
  14374. duk_put_prop_index(ctx, -2, arr_idx);
  14375. }
  14376. duk_compact(ctx, -1);
  14377. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_FORMALS, DUK_PROPDESC_FLAGS_NONE);
  14378. /* Return with final function pushed on stack top. */
  14379. DUK_DD(DUK_DDPRINT("final loaded function: %!iT", duk_get_tval(ctx, -1)));
  14380. DUK_ASSERT_TOP(ctx, idx_base + 1);
  14381. return p;
  14382. format_error:
  14383. return NULL;
  14384. }
  14385. DUK_EXTERNAL void duk_dump_function(duk_context *ctx) {
  14386. duk_hthread *thr;
  14387. duk_hcompiledfunction *func;
  14388. duk_bufwriter_ctx bw_ctx_alloc;
  14389. duk_bufwriter_ctx *bw_ctx = &bw_ctx_alloc;
  14390. duk_uint8_t *p;
  14391. DUK_ASSERT(ctx != NULL);
  14392. thr = (duk_hthread *) ctx;
  14393. /* Bound functions don't have all properties so we'd either need to
  14394. * lookup the non-bound target function or reject bound functions.
  14395. * For now, bound functions are rejected.
  14396. */
  14397. func = duk_require_hcompiledfunction(ctx, -1);
  14398. DUK_ASSERT(func != NULL);
  14399. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(&func->obj));
  14400. /* Estimating the result size beforehand would be costly, so
  14401. * start with a reasonable size and extend as needed.
  14402. */
  14403. DUK_BW_INIT_PUSHBUF(thr, bw_ctx, DUK__BYTECODE_INITIAL_ALLOC);
  14404. p = DUK_BW_GET_PTR(thr, bw_ctx);
  14405. *p++ = DUK__SER_MARKER;
  14406. *p++ = DUK__SER_VERSION;
  14407. p = duk__dump_func(ctx, func, bw_ctx, p);
  14408. DUK_BW_SET_PTR(thr, bw_ctx, p);
  14409. DUK_BW_COMPACT(thr, bw_ctx);
  14410. DUK_DD(DUK_DDPRINT("serialized result: %!T", duk_get_tval(ctx, -1)));
  14411. duk_remove(ctx, -2); /* [ ... func buf ] -> [ ... buf ] */
  14412. }
  14413. DUK_EXTERNAL void duk_load_function(duk_context *ctx) {
  14414. duk_hthread *thr;
  14415. duk_uint8_t *p_buf, *p, *p_end;
  14416. duk_size_t sz;
  14417. DUK_ASSERT(ctx != NULL);
  14418. thr = (duk_hthread *) ctx;
  14419. DUK_UNREF(ctx);
  14420. p_buf = (duk_uint8_t *) duk_require_buffer(ctx, -1, &sz);
  14421. DUK_ASSERT(p_buf != NULL);
  14422. /* The caller is responsible for being sure that bytecode being loaded
  14423. * is valid and trusted. Invalid bytecode can cause memory unsafe
  14424. * behavior directly during loading or later during bytecode execution
  14425. * (instruction validation would be quite complex to implement).
  14426. *
  14427. * This signature check is the only sanity check for detecting
  14428. * accidental invalid inputs. The initial 0xFF byte ensures no
  14429. * ordinary string will be accepted by accident.
  14430. */
  14431. p = p_buf;
  14432. p_end = p_buf + sz;
  14433. if (sz < 2 || p[0] != DUK__SER_MARKER || p[1] != DUK__SER_VERSION) {
  14434. goto format_error;
  14435. }
  14436. p += 2;
  14437. p = duk__load_func(ctx, p, p_end);
  14438. if (p == NULL) {
  14439. goto format_error;
  14440. }
  14441. duk_remove(ctx, -2); /* [ ... buf func ] -> [ ... func ] */
  14442. return;
  14443. format_error:
  14444. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_DECODE_FAILED);
  14445. }
  14446. #undef DUK__SER_MARKER
  14447. #undef DUK__SER_VERSION
  14448. #undef DUK__SER_STRING
  14449. #undef DUK__SER_NUMBER
  14450. #undef DUK__BYTECODE_INITIAL_ALLOC
  14451. #else /* DUK_USE_BYTECODE_DUMP_SUPPORT */
  14452. DUK_EXTERNAL void duk_dump_function(duk_context *ctx) {
  14453. DUK_ERROR((duk_hthread *) ctx, DUK_ERR_ERROR, DUK_STR_UNSUPPORTED);
  14454. }
  14455. DUK_EXTERNAL void duk_load_function(duk_context *ctx) {
  14456. DUK_ERROR((duk_hthread *) ctx, DUK_ERR_ERROR, DUK_STR_UNSUPPORTED);
  14457. }
  14458. #endif /* DUK_USE_BYTECODE_DUMP_SUPPORT */
  14459. #line 1 "duk_api_call.c"
  14460. /*
  14461. * Calls.
  14462. *
  14463. * Protected variants should avoid ever throwing an error.
  14464. */
  14465. /* include removed: duk_internal.h */
  14466. /* Prepare value stack for a method call through an object property.
  14467. * May currently throw an error e.g. when getting the property.
  14468. */
  14469. DUK_LOCAL void duk__call_prop_prep_stack(duk_context *ctx, duk_idx_t normalized_obj_index, duk_idx_t nargs) {
  14470. DUK_ASSERT_CTX_VALID(ctx);
  14471. DUK_DDD(DUK_DDDPRINT("duk__call_prop_prep_stack, normalized_obj_index=%ld, nargs=%ld, stacktop=%ld",
  14472. (long) normalized_obj_index, (long) nargs, (long) duk_get_top(ctx)));
  14473. /* [... key arg1 ... argN] */
  14474. /* duplicate key */
  14475. duk_dup(ctx, -nargs - 1); /* Note: -nargs alone would fail for nargs == 0, this is OK */
  14476. duk_get_prop(ctx, normalized_obj_index);
  14477. DUK_DDD(DUK_DDDPRINT("func: %!T", (duk_tval *) duk_get_tval(ctx, -1)));
  14478. /* [... key arg1 ... argN func] */
  14479. duk_replace(ctx, -nargs - 2);
  14480. /* [... func arg1 ... argN] */
  14481. duk_dup(ctx, normalized_obj_index);
  14482. duk_insert(ctx, -nargs - 1);
  14483. /* [... func this arg1 ... argN] */
  14484. }
  14485. DUK_EXTERNAL void duk_call(duk_context *ctx, duk_idx_t nargs) {
  14486. duk_hthread *thr = (duk_hthread *) ctx;
  14487. duk_small_uint_t call_flags;
  14488. duk_idx_t idx_func;
  14489. duk_int_t rc;
  14490. DUK_ASSERT_CTX_VALID(ctx);
  14491. DUK_ASSERT(thr != NULL);
  14492. idx_func = duk_get_top(ctx) - nargs - 1;
  14493. if (idx_func < 0 || nargs < 0) {
  14494. /* note that we can't reliably pop anything here */
  14495. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  14496. }
  14497. /* XXX: awkward; we assume there is space for this, overwrite
  14498. * directly instead?
  14499. */
  14500. duk_push_undefined(ctx);
  14501. duk_insert(ctx, idx_func + 1);
  14502. call_flags = 0; /* not protected, respect reclimit, not constructor */
  14503. rc = duk_handle_call(thr, /* thread */
  14504. nargs, /* num_stack_args */
  14505. call_flags); /* call_flags */
  14506. DUK_UNREF(rc);
  14507. }
  14508. DUK_EXTERNAL void duk_call_method(duk_context *ctx, duk_idx_t nargs) {
  14509. duk_hthread *thr = (duk_hthread *) ctx;
  14510. duk_small_uint_t call_flags;
  14511. duk_idx_t idx_func;
  14512. duk_int_t rc;
  14513. DUK_ASSERT_CTX_VALID(ctx);
  14514. DUK_ASSERT(thr != NULL);
  14515. idx_func = duk_get_top(ctx) - nargs - 2; /* must work for nargs <= 0 */
  14516. if (idx_func < 0 || nargs < 0) {
  14517. /* note that we can't reliably pop anything here */
  14518. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  14519. }
  14520. call_flags = 0; /* not protected, respect reclimit, not constructor */
  14521. rc = duk_handle_call(thr, /* thread */
  14522. nargs, /* num_stack_args */
  14523. call_flags); /* call_flags */
  14524. DUK_UNREF(rc);
  14525. }
  14526. DUK_EXTERNAL void duk_call_prop(duk_context *ctx, duk_idx_t obj_index, duk_idx_t nargs) {
  14527. /*
  14528. * XXX: if duk_handle_call() took values through indices, this could be
  14529. * made much more sensible. However, duk_handle_call() needs to fudge
  14530. * the 'this' and 'func' values to handle bound function chains, which
  14531. * is now done "in-place", so this is not a trivial change.
  14532. */
  14533. DUK_ASSERT_CTX_VALID(ctx);
  14534. obj_index = duk_require_normalize_index(ctx, obj_index); /* make absolute */
  14535. duk__call_prop_prep_stack(ctx, obj_index, nargs);
  14536. duk_call_method(ctx, nargs);
  14537. }
  14538. DUK_EXTERNAL duk_int_t duk_pcall(duk_context *ctx, duk_idx_t nargs) {
  14539. duk_hthread *thr = (duk_hthread *) ctx;
  14540. duk_small_uint_t call_flags;
  14541. duk_idx_t idx_func;
  14542. duk_int_t rc;
  14543. DUK_ASSERT_CTX_VALID(ctx);
  14544. DUK_ASSERT(thr != NULL);
  14545. idx_func = duk_get_top(ctx) - nargs - 1; /* must work for nargs <= 0 */
  14546. if (idx_func < 0 || nargs < 0) {
  14547. /* We can't reliably pop anything here because the stack input
  14548. * shape is incorrect. So we throw an error; if the caller has
  14549. * no catch point for this, a fatal error will occur. Another
  14550. * alternative would be to just return an error. But then the
  14551. * stack would be in an unknown state which might cause some
  14552. * very hard to diagnose problems later on. Also note that even
  14553. * if we did not throw an error here, the underlying call handler
  14554. * might STILL throw an out-of-memory error or some other internal
  14555. * fatal error.
  14556. */
  14557. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  14558. return DUK_EXEC_ERROR; /* unreachable */
  14559. }
  14560. /* awkward; we assume there is space for this */
  14561. duk_push_undefined(ctx);
  14562. duk_insert(ctx, idx_func + 1);
  14563. call_flags = DUK_CALL_FLAG_PROTECTED; /* protected, respect reclimit, not constructor */
  14564. rc = duk_handle_call(thr, /* thread */
  14565. nargs, /* num_stack_args */
  14566. call_flags); /* call_flags */
  14567. return rc;
  14568. }
  14569. DUK_EXTERNAL duk_int_t duk_pcall_method(duk_context *ctx, duk_idx_t nargs) {
  14570. duk_hthread *thr = (duk_hthread *) ctx;
  14571. duk_small_uint_t call_flags;
  14572. duk_idx_t idx_func;
  14573. duk_int_t rc;
  14574. DUK_ASSERT_CTX_VALID(ctx);
  14575. DUK_ASSERT(thr != NULL);
  14576. idx_func = duk_get_top(ctx) - nargs - 2; /* must work for nargs <= 0 */
  14577. if (idx_func < 0 || nargs < 0) {
  14578. /* See comments in duk_pcall(). */
  14579. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  14580. return DUK_EXEC_ERROR; /* unreachable */
  14581. }
  14582. call_flags = DUK_CALL_FLAG_PROTECTED; /* protected, respect reclimit, not constructor */
  14583. rc = duk_handle_call(thr, /* thread */
  14584. nargs, /* num_stack_args */
  14585. call_flags); /* call_flags */
  14586. return rc;
  14587. }
  14588. DUK_LOCAL duk_ret_t duk__pcall_prop_raw(duk_context *ctx) {
  14589. duk_idx_t obj_index;
  14590. duk_idx_t nargs;
  14591. /* Get the original arguments. Note that obj_index may be a relative
  14592. * index so the stack must have the same top when we use it.
  14593. */
  14594. DUK_ASSERT_CTX_VALID(ctx);
  14595. obj_index = (duk_idx_t) duk_get_int(ctx, -2);
  14596. nargs = (duk_idx_t) duk_get_int(ctx, -1);
  14597. duk_pop_2(ctx);
  14598. obj_index = duk_require_normalize_index(ctx, obj_index); /* make absolute */
  14599. duk__call_prop_prep_stack(ctx, obj_index, nargs);
  14600. duk_call_method(ctx, nargs);
  14601. return 1;
  14602. }
  14603. DUK_EXTERNAL duk_int_t duk_pcall_prop(duk_context *ctx, duk_idx_t obj_index, duk_idx_t nargs) {
  14604. /*
  14605. * Must be careful to catch errors related to value stack manipulation
  14606. * and property lookup, not just the call itself.
  14607. */
  14608. DUK_ASSERT_CTX_VALID(ctx);
  14609. duk_push_idx(ctx, obj_index);
  14610. duk_push_idx(ctx, nargs);
  14611. /* Inputs: explicit arguments (nargs), +1 for key, +2 for obj_index/nargs passing.
  14612. * If the value stack does not contain enough args, an error is thrown; this matches
  14613. * behavior of the other protected call API functions.
  14614. */
  14615. return duk_safe_call(ctx, duk__pcall_prop_raw, nargs + 1 + 2 /*nargs*/, 1 /*nrets*/);
  14616. }
  14617. DUK_EXTERNAL duk_int_t duk_safe_call(duk_context *ctx, duk_safe_call_function func, duk_idx_t nargs, duk_idx_t nrets) {
  14618. duk_hthread *thr = (duk_hthread *) ctx;
  14619. duk_int_t rc;
  14620. DUK_ASSERT_CTX_VALID(ctx);
  14621. DUK_ASSERT(thr != NULL);
  14622. if (duk_get_top(ctx) < nargs || nrets < 0) {
  14623. /* See comments in duk_pcall(). */
  14624. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  14625. return DUK_EXEC_ERROR; /* unreachable */
  14626. }
  14627. rc = duk_handle_safe_call(thr, /* thread */
  14628. func, /* func */
  14629. nargs, /* num_stack_args */
  14630. nrets); /* num_stack_res */
  14631. return rc;
  14632. }
  14633. DUK_EXTERNAL void duk_new(duk_context *ctx, duk_idx_t nargs) {
  14634. /*
  14635. * There are two [[Construct]] operations in the specification:
  14636. *
  14637. * - E5 Section 13.2.2: for Function objects
  14638. * - E5 Section 15.3.4.5.2: for "bound" Function objects
  14639. *
  14640. * The chain of bound functions is resolved in Section 15.3.4.5.2,
  14641. * with arguments "piling up" until the [[Construct]] internal
  14642. * method is called on the final, actual Function object. Note
  14643. * that the "prototype" property is looked up *only* from the
  14644. * final object, *before* calling the constructor.
  14645. *
  14646. * Currently we follow the bound function chain here to get the
  14647. * "prototype" property value from the final, non-bound function.
  14648. * However, we let duk_handle_call() handle the argument "piling"
  14649. * when the constructor is called. The bound function chain is
  14650. * thus now processed twice.
  14651. *
  14652. * When constructing new Array instances, an unnecessary object is
  14653. * created and discarded now: the standard [[Construct]] creates an
  14654. * object, and calls the Array constructor. The Array constructor
  14655. * returns an Array instance, which is used as the result value for
  14656. * the "new" operation; the object created before the Array constructor
  14657. * call is discarded.
  14658. *
  14659. * This would be easy to fix, e.g. by knowing that the Array constructor
  14660. * will always create a replacement object and skip creating the fallback
  14661. * object in that case.
  14662. *
  14663. * Note: functions called via "new" need to know they are called as a
  14664. * constructor. For instance, built-in constructors behave differently
  14665. * depending on how they are called.
  14666. */
  14667. /* XXX: merge this with duk_js_call.c, as this function implements
  14668. * core semantics (or perhaps merge the two files altogether).
  14669. */
  14670. duk_hthread *thr = (duk_hthread *) ctx;
  14671. duk_hobject *proto;
  14672. duk_hobject *cons;
  14673. duk_hobject *fallback;
  14674. duk_idx_t idx_cons;
  14675. duk_small_uint_t call_flags;
  14676. duk_int_t rc;
  14677. DUK_ASSERT_CTX_VALID(ctx);
  14678. /* [... constructor arg1 ... argN] */
  14679. idx_cons = duk_require_normalize_index(ctx, -nargs - 1);
  14680. DUK_DDD(DUK_DDDPRINT("top=%ld, nargs=%ld, idx_cons=%ld",
  14681. (long) duk_get_top(ctx), (long) nargs, (long) idx_cons));
  14682. /* XXX: code duplication */
  14683. /*
  14684. * Figure out the final, non-bound constructor, to get "prototype"
  14685. * property.
  14686. */
  14687. duk_dup(ctx, idx_cons);
  14688. for (;;) {
  14689. cons = duk_get_hobject(ctx, -1);
  14690. if (cons == NULL || !DUK_HOBJECT_HAS_CONSTRUCTABLE(cons)) {
  14691. /* Checking constructability from anything else than the
  14692. * initial constructor is not strictly necessary, but a
  14693. * nice sanity check.
  14694. */
  14695. goto not_constructable;
  14696. }
  14697. if (!DUK_HOBJECT_HAS_BOUND(cons)) {
  14698. break;
  14699. }
  14700. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_TARGET); /* -> [... cons target] */
  14701. duk_remove(ctx, -2); /* -> [... target] */
  14702. }
  14703. DUK_ASSERT(cons != NULL && !DUK_HOBJECT_HAS_BOUND(cons));
  14704. /* [... constructor arg1 ... argN final_cons] */
  14705. /*
  14706. * Create "fallback" object to be used as the object instance,
  14707. * unless the constructor returns a replacement value.
  14708. * Its internal prototype needs to be set based on "prototype"
  14709. * property of the constructor.
  14710. */
  14711. duk_push_object(ctx); /* class Object, extensible */
  14712. /* [... constructor arg1 ... argN final_cons fallback] */
  14713. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_PROTOTYPE);
  14714. proto = duk_get_hobject(ctx, -1);
  14715. if (!proto) {
  14716. DUK_DDD(DUK_DDDPRINT("constructor has no 'prototype' property, or value not an object "
  14717. "-> leave standard Object prototype as fallback prototype"));
  14718. } else {
  14719. DUK_DDD(DUK_DDDPRINT("constructor has 'prototype' property with object value "
  14720. "-> set fallback prototype to that value: %!iO", (duk_heaphdr *) proto));
  14721. fallback = duk_get_hobject(ctx, -2);
  14722. DUK_ASSERT(fallback != NULL);
  14723. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, fallback, proto);
  14724. }
  14725. duk_pop(ctx);
  14726. /* [... constructor arg1 ... argN final_cons fallback] */
  14727. /*
  14728. * Manipulate callstack for the call.
  14729. */
  14730. duk_dup_top(ctx);
  14731. duk_insert(ctx, idx_cons + 1); /* use fallback as 'this' value */
  14732. duk_insert(ctx, idx_cons); /* also stash it before constructor,
  14733. * in case we need it (as the fallback value)
  14734. */
  14735. duk_pop(ctx); /* pop final_cons */
  14736. /* [... fallback constructor fallback(this) arg1 ... argN];
  14737. * Note: idx_cons points to first 'fallback', not 'constructor'.
  14738. */
  14739. DUK_DDD(DUK_DDDPRINT("before call, idx_cons+1 (constructor) -> %!T, idx_cons+2 (fallback/this) -> %!T, "
  14740. "nargs=%ld, top=%ld",
  14741. (duk_tval *) duk_get_tval(ctx, idx_cons + 1),
  14742. (duk_tval *) duk_get_tval(ctx, idx_cons + 2),
  14743. (long) nargs,
  14744. (long) duk_get_top(ctx)));
  14745. /*
  14746. * Call the constructor function (called in "constructor mode").
  14747. */
  14748. call_flags = DUK_CALL_FLAG_CONSTRUCTOR_CALL; /* not protected, respect reclimit, is a constructor call */
  14749. rc = duk_handle_call(thr, /* thread */
  14750. nargs, /* num_stack_args */
  14751. call_flags); /* call_flags */
  14752. DUK_UNREF(rc);
  14753. /* [... fallback retval] */
  14754. DUK_DDD(DUK_DDDPRINT("constructor call finished, rc=%ld, fallback=%!iT, retval=%!iT",
  14755. (long) rc,
  14756. (duk_tval *) duk_get_tval(ctx, -2),
  14757. (duk_tval *) duk_get_tval(ctx, -1)));
  14758. /*
  14759. * Determine whether to use the constructor return value as the created
  14760. * object instance or not.
  14761. */
  14762. if (duk_is_object(ctx, -1)) {
  14763. duk_remove(ctx, -2);
  14764. } else {
  14765. duk_pop(ctx);
  14766. }
  14767. /*
  14768. * Augment created errors upon creation (not when they are thrown or
  14769. * rethrown). __FILE__ and __LINE__ are not desirable here; the call
  14770. * stack reflects the caller which is correct.
  14771. */
  14772. #ifdef DUK_USE_AUGMENT_ERROR_CREATE
  14773. duk_hthread_sync_currpc(thr);
  14774. duk_err_augment_error_create(thr, thr, NULL, 0, 1 /*noblame_fileline*/);
  14775. #endif
  14776. /* [... retval] */
  14777. return;
  14778. not_constructable:
  14779. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_CONSTRUCTABLE);
  14780. }
  14781. DUK_LOCAL duk_ret_t duk__pnew_helper(duk_context *ctx) {
  14782. duk_uint_t nargs;
  14783. nargs = duk_to_uint(ctx, -1);
  14784. duk_pop(ctx);
  14785. duk_new(ctx, nargs);
  14786. return 1;
  14787. }
  14788. DUK_EXTERNAL duk_int_t duk_pnew(duk_context *ctx, duk_idx_t nargs) {
  14789. duk_int_t rc;
  14790. DUK_ASSERT_CTX_VALID(ctx);
  14791. /* For now, just use duk_safe_call() to wrap duk_new(). We can't
  14792. * simply use a protected duk_handle_call() because there's post
  14793. * processing which might throw. It should be possible to ensure
  14794. * the post processing never throws (except in internal errors and
  14795. * out of memory etc which are always allowed) and then remove this
  14796. * wrapper.
  14797. */
  14798. duk_push_uint(ctx, nargs);
  14799. rc = duk_safe_call(ctx, duk__pnew_helper, nargs + 2 /*nargs*/, 1 /*nrets*/);
  14800. return rc;
  14801. }
  14802. DUK_EXTERNAL duk_bool_t duk_is_constructor_call(duk_context *ctx) {
  14803. duk_hthread *thr = (duk_hthread *) ctx;
  14804. duk_activation *act;
  14805. DUK_ASSERT_CTX_VALID(ctx);
  14806. DUK_ASSERT(thr != NULL);
  14807. DUK_ASSERT_DISABLE(thr->callstack_top >= 0);
  14808. act = duk_hthread_get_current_activation(thr);
  14809. DUK_ASSERT(act != NULL); /* because callstack_top > 0 */
  14810. return ((act->flags & DUK_ACT_FLAG_CONSTRUCT) != 0 ? 1 : 0);
  14811. }
  14812. DUK_EXTERNAL duk_bool_t duk_is_strict_call(duk_context *ctx) {
  14813. duk_hthread *thr = (duk_hthread *) ctx;
  14814. duk_activation *act;
  14815. /* For user code this could just return 1 (strict) always
  14816. * because all Duktape/C functions are considered strict,
  14817. * and strict is also the default when nothing is running.
  14818. * However, Duktape may call this function internally when
  14819. * the current activation is an Ecmascript function, so
  14820. * this cannot be replaced by a 'return 1' without fixing
  14821. * the internal call sites.
  14822. */
  14823. DUK_ASSERT_CTX_VALID(ctx);
  14824. DUK_ASSERT(thr != NULL);
  14825. DUK_ASSERT_DISABLE(thr->callstack_top >= 0);
  14826. act = duk_hthread_get_current_activation(thr);
  14827. if (act == NULL) {
  14828. /* Strict by default. */
  14829. return 1;
  14830. }
  14831. return ((act->flags & DUK_ACT_FLAG_STRICT) != 0 ? 1 : 0);
  14832. }
  14833. /*
  14834. * Duktape/C function magic
  14835. */
  14836. DUK_EXTERNAL duk_int_t duk_get_current_magic(duk_context *ctx) {
  14837. duk_hthread *thr = (duk_hthread *) ctx;
  14838. duk_activation *act;
  14839. duk_hobject *func;
  14840. DUK_ASSERT_CTX_VALID(ctx);
  14841. DUK_ASSERT(thr != NULL);
  14842. DUK_ASSERT_DISABLE(thr->callstack_top >= 0);
  14843. act = duk_hthread_get_current_activation(thr);
  14844. if (act) {
  14845. func = DUK_ACT_GET_FUNC(act);
  14846. if (!func) {
  14847. duk_tval *tv = &act->tv_func;
  14848. duk_small_uint_t lf_flags;
  14849. lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv);
  14850. return (duk_int_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags);
  14851. }
  14852. DUK_ASSERT(func != NULL);
  14853. if (DUK_HOBJECT_IS_NATIVEFUNCTION(func)) {
  14854. duk_hnativefunction *nf = (duk_hnativefunction *) func;
  14855. return (duk_int_t) nf->magic;
  14856. }
  14857. }
  14858. return 0;
  14859. }
  14860. DUK_EXTERNAL duk_int_t duk_get_magic(duk_context *ctx, duk_idx_t index) {
  14861. duk_hthread *thr = (duk_hthread *) ctx;
  14862. duk_tval *tv;
  14863. duk_hobject *h;
  14864. DUK_ASSERT_CTX_VALID(ctx);
  14865. tv = duk_require_tval(ctx, index);
  14866. if (DUK_TVAL_IS_OBJECT(tv)) {
  14867. h = DUK_TVAL_GET_OBJECT(tv);
  14868. DUK_ASSERT(h != NULL);
  14869. if (!DUK_HOBJECT_HAS_NATIVEFUNCTION(h)) {
  14870. goto type_error;
  14871. }
  14872. return (duk_int_t) ((duk_hnativefunction *) h)->magic;
  14873. } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  14874. duk_small_uint_t lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv);
  14875. return (duk_int_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags);
  14876. }
  14877. /* fall through */
  14878. type_error:
  14879. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_UNEXPECTED_TYPE);
  14880. return 0;
  14881. }
  14882. DUK_EXTERNAL void duk_set_magic(duk_context *ctx, duk_idx_t index, duk_int_t magic) {
  14883. duk_hnativefunction *nf;
  14884. DUK_ASSERT_CTX_VALID(ctx);
  14885. nf = duk_require_hnativefunction(ctx, index);
  14886. DUK_ASSERT(nf != NULL);
  14887. nf->magic = (duk_int16_t) magic;
  14888. }
  14889. #line 1 "duk_api_codec.c"
  14890. /*
  14891. * Encoding and decoding basic formats: hex, base64.
  14892. *
  14893. * These are in-place operations which may allow an optimized implementation.
  14894. */
  14895. /* include removed: duk_internal.h */
  14896. /* dst length must be exactly ceil(len/3)*4 */
  14897. DUK_LOCAL void duk__base64_encode_helper(const duk_uint8_t *src, const duk_uint8_t *src_end,
  14898. duk_uint8_t *dst, duk_uint8_t *dst_end) {
  14899. duk_small_uint_t i, snip;
  14900. duk_uint_fast32_t t;
  14901. duk_uint_fast8_t x, y;
  14902. DUK_UNREF(dst_end);
  14903. while (src < src_end) {
  14904. /* read 3 bytes into 't', padded by zero */
  14905. snip = 4;
  14906. t = 0;
  14907. for (i = 0; i < 3; i++) {
  14908. t = t << 8;
  14909. if (src >= src_end) {
  14910. snip--;
  14911. } else {
  14912. t += (duk_uint_fast32_t) (*src++);
  14913. }
  14914. }
  14915. /*
  14916. * Missing bytes snip base64 example
  14917. * 0 4 XXXX
  14918. * 1 3 XXX=
  14919. * 2 2 XX==
  14920. */
  14921. DUK_ASSERT(snip >= 2 && snip <= 4);
  14922. for (i = 0; i < 4; i++) {
  14923. x = (duk_uint_fast8_t) ((t >> 18) & 0x3f);
  14924. t = t << 6;
  14925. /* A straightforward 64-byte lookup would be faster
  14926. * and cleaner, but this is shorter.
  14927. */
  14928. if (i >= snip) {
  14929. y = '=';
  14930. } else if (x <= 25) {
  14931. y = x + 'A';
  14932. } else if (x <= 51) {
  14933. y = x - 26 + 'a';
  14934. } else if (x <= 61) {
  14935. y = x - 52 + '0';
  14936. } else if (x == 62) {
  14937. y = '+';
  14938. } else {
  14939. y = '/';
  14940. }
  14941. DUK_ASSERT(dst < dst_end);
  14942. *dst++ = (duk_uint8_t) y;
  14943. }
  14944. }
  14945. }
  14946. DUK_LOCAL duk_bool_t duk__base64_decode_helper(const duk_uint8_t *src, const duk_uint8_t *src_end,
  14947. duk_uint8_t *dst, duk_uint8_t *dst_end, duk_uint8_t **out_dst_final) {
  14948. duk_uint_fast32_t t;
  14949. duk_uint_fast8_t x, y;
  14950. duk_small_uint_t group_idx;
  14951. DUK_UNREF(dst_end);
  14952. t = 0;
  14953. group_idx = 0;
  14954. while (src < src_end) {
  14955. x = *src++;
  14956. if (x >= 'A' && x <= 'Z') {
  14957. y = x - 'A' + 0;
  14958. } else if (x >= 'a' && x <= 'z') {
  14959. y = x - 'a' + 26;
  14960. } else if (x >= '0' && x <= '9') {
  14961. y = x - '0' + 52;
  14962. } else if (x == '+') {
  14963. y = 62;
  14964. } else if (x == '/') {
  14965. y = 63;
  14966. } else if (x == '=') {
  14967. /* We don't check the zero padding bytes here right now.
  14968. * This seems to be common behavior for base-64 decoders.
  14969. */
  14970. if (group_idx == 2) {
  14971. /* xx== -> 1 byte, t contains 12 bits, 4 on right are zero */
  14972. t = t >> 4;
  14973. DUK_ASSERT(dst < dst_end);
  14974. *dst++ = (duk_uint8_t) t;
  14975. if (src >= src_end) {
  14976. goto error;
  14977. }
  14978. x = *src++;
  14979. if (x != '=') {
  14980. goto error;
  14981. }
  14982. } else if (group_idx == 3) {
  14983. /* xxx= -> 2 bytes, t contains 18 bits, 2 on right are zero */
  14984. t = t >> 2;
  14985. DUK_ASSERT(dst < dst_end);
  14986. *dst++ = (duk_uint8_t) ((t >> 8) & 0xff);
  14987. DUK_ASSERT(dst < dst_end);
  14988. *dst++ = (duk_uint8_t) (t & 0xff);
  14989. } else {
  14990. goto error;
  14991. }
  14992. /* Here we can choose either to end parsing and ignore
  14993. * whatever follows, or to continue parsing in case
  14994. * multiple (possibly padded) base64 strings have been
  14995. * concatenated. Currently, keep on parsing.
  14996. */
  14997. t = 0;
  14998. group_idx = 0;
  14999. continue;
  15000. } else if (x == 0x09 || x == 0x0a || x == 0x0d || x == 0x20) {
  15001. /* allow basic ASCII whitespace */
  15002. continue;
  15003. } else {
  15004. goto error;
  15005. }
  15006. t = (t << 6) + y;
  15007. if (group_idx == 3) {
  15008. /* output 3 bytes from 't' */
  15009. DUK_ASSERT(dst < dst_end);
  15010. *dst++ = (duk_uint8_t) ((t >> 16) & 0xff);
  15011. DUK_ASSERT(dst < dst_end);
  15012. *dst++ = (duk_uint8_t) ((t >> 8) & 0xff);
  15013. DUK_ASSERT(dst < dst_end);
  15014. *dst++ = (duk_uint8_t) (t & 0xff);
  15015. t = 0;
  15016. group_idx = 0;
  15017. } else {
  15018. group_idx++;
  15019. }
  15020. }
  15021. if (group_idx != 0) {
  15022. /* Here we'd have the option of decoding unpadded base64
  15023. * (e.g. "xxxxyy" instead of "xxxxyy==". Currently not
  15024. * accepted.
  15025. */
  15026. goto error;
  15027. }
  15028. *out_dst_final = dst;
  15029. return 1;
  15030. error:
  15031. return 0;
  15032. }
  15033. /* Shared handling for encode/decode argument. Fast path handling for
  15034. * buffer and string values because they're the most common. In particular,
  15035. * avoid creating a temporary string or buffer when possible.
  15036. */
  15037. DUK_LOCAL const duk_uint8_t *duk__prep_codec_arg(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) {
  15038. DUK_ASSERT(duk_is_valid_index(ctx, index)); /* checked by caller */
  15039. if (duk_is_buffer(ctx, index)) {
  15040. return (const duk_uint8_t *) duk_get_buffer(ctx, index, out_len);
  15041. } else {
  15042. return (const duk_uint8_t *) duk_to_lstring(ctx, index, out_len);
  15043. }
  15044. }
  15045. DUK_EXTERNAL const char *duk_base64_encode(duk_context *ctx, duk_idx_t index) {
  15046. duk_hthread *thr = (duk_hthread *) ctx;
  15047. duk_uint8_t *src;
  15048. duk_size_t srclen;
  15049. duk_size_t dstlen;
  15050. duk_uint8_t *dst;
  15051. const char *ret;
  15052. DUK_ASSERT_CTX_VALID(ctx);
  15053. /* XXX: optimize for string inputs: no need to coerce to a buffer
  15054. * which makes a copy of the input.
  15055. */
  15056. index = duk_require_normalize_index(ctx, index);
  15057. src = (duk_uint8_t *) duk_to_buffer(ctx, index, &srclen);
  15058. /* Note: for srclen=0, src may be NULL */
  15059. /* Computation must not wrap; this limit works for 32-bit size_t:
  15060. * >>> srclen = 3221225469
  15061. * >>> '%x' % ((srclen + 2) / 3 * 4)
  15062. * 'fffffffc'
  15063. */
  15064. if (srclen > 3221225469UL) {
  15065. goto type_error;
  15066. }
  15067. dstlen = (srclen + 2) / 3 * 4;
  15068. dst = (duk_uint8_t *) duk_push_fixed_buffer(ctx, dstlen);
  15069. duk__base64_encode_helper((const duk_uint8_t *) src, (const duk_uint8_t *) (src + srclen),
  15070. dst, (dst + dstlen));
  15071. ret = duk_to_string(ctx, -1);
  15072. duk_replace(ctx, index);
  15073. return ret;
  15074. type_error:
  15075. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_ENCODE_FAILED);
  15076. return NULL; /* never here */
  15077. }
  15078. DUK_EXTERNAL void duk_base64_decode(duk_context *ctx, duk_idx_t index) {
  15079. duk_hthread *thr = (duk_hthread *) ctx;
  15080. const duk_uint8_t *src;
  15081. duk_size_t srclen;
  15082. duk_size_t dstlen;
  15083. duk_uint8_t *dst;
  15084. duk_uint8_t *dst_final;
  15085. duk_bool_t retval;
  15086. DUK_ASSERT_CTX_VALID(ctx);
  15087. /* XXX: optimize for buffer inputs: no need to coerce to a string
  15088. * which causes an unnecessary interning.
  15089. */
  15090. index = duk_require_normalize_index(ctx, index);
  15091. src = (const duk_uint8_t *) duk_to_lstring(ctx, index, &srclen);
  15092. /* Computation must not wrap, only srclen + 3 is at risk of
  15093. * wrapping because after that the number gets smaller.
  15094. * This limit works for 32-bit size_t:
  15095. * 0x100000000 - 3 - 1 = 4294967292
  15096. */
  15097. if (srclen > 4294967292UL) {
  15098. goto type_error;
  15099. }
  15100. dstlen = (srclen + 3) / 4 * 3; /* upper limit */
  15101. dst = (duk_uint8_t *) duk_push_dynamic_buffer(ctx, dstlen);
  15102. /* Note: for dstlen=0, dst may be NULL */
  15103. retval = duk__base64_decode_helper((const duk_uint8_t *) src, (const duk_uint8_t *) (src + srclen),
  15104. dst, dst + dstlen, &dst_final);
  15105. if (!retval) {
  15106. goto type_error;
  15107. }
  15108. /* XXX: convert to fixed buffer? */
  15109. (void) duk_resize_buffer(ctx, -1, (duk_size_t) (dst_final - dst));
  15110. duk_replace(ctx, index);
  15111. return;
  15112. type_error:
  15113. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_DECODE_FAILED);
  15114. }
  15115. DUK_EXTERNAL const char *duk_hex_encode(duk_context *ctx, duk_idx_t index) {
  15116. const duk_uint8_t *inp;
  15117. duk_size_t len;
  15118. duk_size_t i;
  15119. duk_small_uint_t t;
  15120. duk_uint8_t *buf;
  15121. const char *ret;
  15122. DUK_ASSERT_CTX_VALID(ctx);
  15123. index = duk_require_normalize_index(ctx, index);
  15124. inp = duk__prep_codec_arg(ctx, index, &len);
  15125. DUK_ASSERT(inp != NULL || len == 0);
  15126. /* Fixed buffer, no zeroing because we'll fill all the data. */
  15127. buf = (duk_uint8_t *) duk_push_buffer_raw(ctx, len * 2, DUK_BUF_FLAG_NOZERO /*flags*/);
  15128. DUK_ASSERT(buf != NULL);
  15129. for (i = 0; i < len; i++) {
  15130. /* XXX: by using two 256-entry tables could avoid shifting and masking. */
  15131. t = (duk_small_uint_t) inp[i];
  15132. buf[i*2 + 0] = duk_lc_digits[t >> 4];
  15133. buf[i*2 + 1] = duk_lc_digits[t & 0x0f];
  15134. }
  15135. /* XXX: Using a string return value forces a string intern which is
  15136. * not always necessary. As a rough performance measure, hex encode
  15137. * time for tests/perf/test-hex-encode.js dropped from ~35s to ~15s
  15138. * without string coercion. Change to returning a buffer and let the
  15139. * caller coerce to string if necessary?
  15140. */
  15141. ret = duk_to_string(ctx, -1);
  15142. duk_replace(ctx, index);
  15143. return ret;
  15144. }
  15145. DUK_EXTERNAL void duk_hex_decode(duk_context *ctx, duk_idx_t index) {
  15146. duk_hthread *thr = (duk_hthread *) ctx;
  15147. const duk_uint8_t *inp;
  15148. duk_size_t len;
  15149. duk_size_t i;
  15150. duk_small_int_t t;
  15151. duk_uint8_t *buf;
  15152. DUK_ASSERT_CTX_VALID(ctx);
  15153. index = duk_require_normalize_index(ctx, index);
  15154. inp = duk__prep_codec_arg(ctx, index, &len);
  15155. DUK_ASSERT(inp != NULL || len == 0);
  15156. if (len & 0x01) {
  15157. goto type_error;
  15158. }
  15159. /* Fixed buffer, no zeroing because we'll fill all the data. */
  15160. buf = (duk_uint8_t *) duk_push_buffer_raw(ctx, len / 2, DUK_BUF_FLAG_NOZERO /*flags*/);
  15161. DUK_ASSERT(buf != NULL);
  15162. for (i = 0; i < len; i += 2) {
  15163. /* For invalid characters the value -1 gets extended to
  15164. * at least 16 bits. If either nybble is invalid, the
  15165. * resulting 't' will be < 0.
  15166. */
  15167. t = (((duk_small_int_t) duk_hex_dectab[inp[i]]) << 4) |
  15168. ((duk_small_int_t) duk_hex_dectab[inp[i + 1]]);
  15169. if (DUK_UNLIKELY(t < 0)) {
  15170. goto type_error;
  15171. }
  15172. buf[i >> 1] = (duk_uint8_t) t;
  15173. }
  15174. duk_replace(ctx, index);
  15175. return;
  15176. type_error:
  15177. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_DECODE_FAILED);
  15178. }
  15179. DUK_EXTERNAL const char *duk_json_encode(duk_context *ctx, duk_idx_t index) {
  15180. #ifdef DUK_USE_ASSERTIONS
  15181. duk_idx_t top_at_entry;
  15182. #endif
  15183. const char *ret;
  15184. DUK_ASSERT_CTX_VALID(ctx);
  15185. #ifdef DUK_USE_ASSERTIONS
  15186. top_at_entry = duk_get_top(ctx);
  15187. #endif
  15188. index = duk_require_normalize_index(ctx, index);
  15189. duk_bi_json_stringify_helper(ctx,
  15190. index /*idx_value*/,
  15191. DUK_INVALID_INDEX /*idx_replacer*/,
  15192. DUK_INVALID_INDEX /*idx_space*/,
  15193. 0 /*flags*/);
  15194. DUK_ASSERT(duk_is_string(ctx, -1));
  15195. duk_replace(ctx, index);
  15196. ret = duk_get_string(ctx, index);
  15197. DUK_ASSERT(duk_get_top(ctx) == top_at_entry);
  15198. return ret;
  15199. }
  15200. DUK_EXTERNAL void duk_json_decode(duk_context *ctx, duk_idx_t index) {
  15201. #ifdef DUK_USE_ASSERTIONS
  15202. duk_idx_t top_at_entry;
  15203. #endif
  15204. DUK_ASSERT_CTX_VALID(ctx);
  15205. #ifdef DUK_USE_ASSERTIONS
  15206. top_at_entry = duk_get_top(ctx);
  15207. #endif
  15208. index = duk_require_normalize_index(ctx, index);
  15209. duk_bi_json_parse_helper(ctx,
  15210. index /*idx_value*/,
  15211. DUK_INVALID_INDEX /*idx_reviver*/,
  15212. 0 /*flags*/);
  15213. duk_replace(ctx, index);
  15214. DUK_ASSERT(duk_get_top(ctx) == top_at_entry);
  15215. }
  15216. #line 1 "duk_api_compile.c"
  15217. /*
  15218. * Compilation and evaluation
  15219. */
  15220. /* include removed: duk_internal.h */
  15221. typedef struct duk__compile_raw_args duk__compile_raw_args;
  15222. struct duk__compile_raw_args {
  15223. duk_size_t src_length; /* should be first on 64-bit platforms */
  15224. const duk_uint8_t *src_buffer;
  15225. duk_uint_t flags;
  15226. };
  15227. /* Eval is just a wrapper now. */
  15228. DUK_EXTERNAL duk_int_t duk_eval_raw(duk_context *ctx, const char *src_buffer, duk_size_t src_length, duk_uint_t flags) {
  15229. duk_uint_t comp_flags;
  15230. duk_int_t rc;
  15231. DUK_ASSERT_CTX_VALID(ctx);
  15232. /* Note: strictness is *not* inherited from the current Duktape/C.
  15233. * This would be confusing because the current strictness state
  15234. * depends on whether we're running inside a Duktape/C activation
  15235. * (= strict mode) or outside of any activation (= non-strict mode).
  15236. * See tests/api/test-eval-strictness.c for more discussion.
  15237. */
  15238. /* [ ... source? filename ] (depends on flags) */
  15239. comp_flags = flags;
  15240. comp_flags |= DUK_COMPILE_EVAL;
  15241. rc = duk_compile_raw(ctx, src_buffer, src_length, comp_flags); /* may be safe, or non-safe depending on flags */
  15242. /* [ ... closure/error ] */
  15243. if (rc != DUK_EXEC_SUCCESS) {
  15244. rc = DUK_EXEC_ERROR;
  15245. goto got_rc;
  15246. }
  15247. duk_push_global_object(ctx); /* explicit 'this' binding, see GH-164 */
  15248. if (flags & DUK_COMPILE_SAFE) {
  15249. rc = duk_pcall_method(ctx, 0);
  15250. } else {
  15251. duk_call_method(ctx, 0);
  15252. rc = DUK_EXEC_SUCCESS;
  15253. }
  15254. /* [ ... result/error ] */
  15255. got_rc:
  15256. if (flags & DUK_COMPILE_NORESULT) {
  15257. duk_pop(ctx);
  15258. }
  15259. return rc;
  15260. }
  15261. /* Helper which can be called both directly and with duk_safe_call(). */
  15262. DUK_LOCAL duk_ret_t duk__do_compile(duk_context *ctx) {
  15263. duk_hthread *thr = (duk_hthread *) ctx;
  15264. duk__compile_raw_args *comp_args;
  15265. duk_uint_t flags;
  15266. duk_small_uint_t comp_flags;
  15267. duk_hcompiledfunction *h_templ;
  15268. DUK_ASSERT_CTX_VALID(ctx);
  15269. /* Note: strictness is not inherited from the current Duktape/C
  15270. * context. Otherwise it would not be possible to compile
  15271. * non-strict code inside a Duktape/C activation (which is
  15272. * always strict now). See tests/api/test-eval-strictness.c
  15273. * for discussion.
  15274. */
  15275. /* [ ... source? filename &comp_args ] (depends on flags) */
  15276. comp_args = (duk__compile_raw_args *) duk_require_pointer(ctx, -1);
  15277. flags = comp_args->flags;
  15278. duk_pop(ctx);
  15279. /* [ ... source? filename ] */
  15280. if (!comp_args->src_buffer) {
  15281. duk_hstring *h_sourcecode;
  15282. h_sourcecode = duk_get_hstring(ctx, -2);
  15283. if ((flags & DUK_COMPILE_NOSOURCE) || /* args incorrect */
  15284. (h_sourcecode == NULL)) { /* e.g. duk_push_string_file_raw() pushed undefined */
  15285. /* XXX: when this error is caused by a nonexistent
  15286. * file given to duk_peval_file() or similar, the
  15287. * error message is not the best possible.
  15288. */
  15289. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_NO_SOURCECODE);
  15290. }
  15291. DUK_ASSERT(h_sourcecode != NULL);
  15292. comp_args->src_buffer = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_sourcecode);
  15293. comp_args->src_length = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h_sourcecode);
  15294. }
  15295. DUK_ASSERT(comp_args->src_buffer != NULL);
  15296. /* XXX: unnecessary translation of flags */
  15297. comp_flags = 0;
  15298. if (flags & DUK_COMPILE_EVAL) {
  15299. comp_flags |= DUK_JS_COMPILE_FLAG_EVAL;
  15300. }
  15301. if (flags & DUK_COMPILE_FUNCTION) {
  15302. comp_flags |= DUK_JS_COMPILE_FLAG_EVAL |
  15303. DUK_JS_COMPILE_FLAG_FUNCEXPR;
  15304. }
  15305. if (flags & DUK_COMPILE_STRICT) {
  15306. comp_flags |= DUK_JS_COMPILE_FLAG_STRICT;
  15307. }
  15308. /* [ ... source? filename ] */
  15309. duk_js_compile(thr, comp_args->src_buffer, comp_args->src_length, comp_flags);
  15310. /* [ ... source? func_template ] */
  15311. if (flags & DUK_COMPILE_NOSOURCE) {
  15312. ;
  15313. } else {
  15314. duk_remove(ctx, -2);
  15315. }
  15316. /* [ ... func_template ] */
  15317. h_templ = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1);
  15318. DUK_ASSERT(h_templ != NULL);
  15319. duk_js_push_closure(thr,
  15320. h_templ,
  15321. thr->builtins[DUK_BIDX_GLOBAL_ENV],
  15322. thr->builtins[DUK_BIDX_GLOBAL_ENV]);
  15323. duk_remove(ctx, -2); /* -> [ ... closure ] */
  15324. /* [ ... closure ] */
  15325. return 1;
  15326. }
  15327. DUK_EXTERNAL duk_int_t duk_compile_raw(duk_context *ctx, const char *src_buffer, duk_size_t src_length, duk_uint_t flags) {
  15328. duk__compile_raw_args comp_args_alloc;
  15329. duk__compile_raw_args *comp_args = &comp_args_alloc;
  15330. DUK_ASSERT_CTX_VALID(ctx);
  15331. if ((flags & DUK_COMPILE_STRLEN) && (src_buffer != NULL)) {
  15332. /* String length is computed here to avoid multiple evaluation
  15333. * of a macro argument in the calling side.
  15334. */
  15335. src_length = DUK_STRLEN(src_buffer);
  15336. }
  15337. comp_args->src_buffer = (const duk_uint8_t *) src_buffer;
  15338. comp_args->src_length = src_length;
  15339. comp_args->flags = flags;
  15340. duk_push_pointer(ctx, (void *) comp_args);
  15341. /* [ ... source? filename &comp_args ] (depends on flags) */
  15342. if (flags & DUK_COMPILE_SAFE) {
  15343. duk_int_t rc;
  15344. duk_int_t nargs;
  15345. duk_int_t nrets = 1;
  15346. /* Arguments are either: [ filename &comp_args ] or [ source filename &comp_args ] */
  15347. nargs = (flags & DUK_COMPILE_NOSOURCE) ? 2 : 3;
  15348. rc = duk_safe_call(ctx, duk__do_compile, nargs, nrets);
  15349. /* [ ... closure ] */
  15350. return rc;
  15351. }
  15352. (void) duk__do_compile(ctx);
  15353. /* [ ... closure ] */
  15354. return DUK_EXEC_SUCCESS;
  15355. }
  15356. #line 1 "duk_api_debug.c"
  15357. /*
  15358. * Debugging related API calls
  15359. */
  15360. /* include removed: duk_internal.h */
  15361. DUK_EXTERNAL void duk_push_context_dump(duk_context *ctx) {
  15362. duk_idx_t idx;
  15363. duk_idx_t top;
  15364. DUK_ASSERT_CTX_VALID(ctx);
  15365. /* We don't duk_require_stack() here now, but rely on the caller having
  15366. * enough space.
  15367. */
  15368. top = duk_get_top(ctx);
  15369. duk_push_array(ctx);
  15370. for (idx = 0; idx < top; idx++) {
  15371. duk_dup(ctx, idx);
  15372. duk_put_prop_index(ctx, -2, idx);
  15373. }
  15374. /* XXX: conversion errors should not propagate outwards.
  15375. * Perhaps values need to be coerced individually?
  15376. */
  15377. duk_bi_json_stringify_helper(ctx,
  15378. duk_get_top_index(ctx), /*idx_value*/
  15379. DUK_INVALID_INDEX, /*idx_replacer*/
  15380. DUK_INVALID_INDEX, /*idx_space*/
  15381. DUK_JSON_FLAG_EXT_CUSTOM |
  15382. DUK_JSON_FLAG_ASCII_ONLY |
  15383. DUK_JSON_FLAG_AVOID_KEY_QUOTES /*flags*/);
  15384. duk_push_sprintf(ctx, "ctx: top=%ld, stack=%s", (long) top, (const char *) duk_safe_to_string(ctx, -1));
  15385. duk_replace(ctx, -3); /* [ ... arr jsonx(arr) res ] -> [ ... res jsonx(arr) ] */
  15386. duk_pop(ctx);
  15387. DUK_ASSERT(duk_is_string(ctx, -1));
  15388. }
  15389. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  15390. DUK_EXTERNAL void duk_debugger_attach(duk_context *ctx,
  15391. duk_debug_read_function read_cb,
  15392. duk_debug_write_function write_cb,
  15393. duk_debug_peek_function peek_cb,
  15394. duk_debug_read_flush_function read_flush_cb,
  15395. duk_debug_write_flush_function write_flush_cb,
  15396. duk_debug_detached_function detached_cb,
  15397. void *udata) {
  15398. duk_hthread *thr = (duk_hthread *) ctx;
  15399. duk_heap *heap;
  15400. const char *str;
  15401. duk_size_t len;
  15402. /* XXX: should there be an error or an automatic detach if
  15403. * already attached?
  15404. */
  15405. DUK_ASSERT_CTX_VALID(ctx);
  15406. DUK_ASSERT(read_cb != NULL);
  15407. DUK_ASSERT(write_cb != NULL);
  15408. /* Other callbacks are optional. */
  15409. heap = thr->heap;
  15410. heap->dbg_read_cb = read_cb;
  15411. heap->dbg_write_cb = write_cb;
  15412. heap->dbg_peek_cb = peek_cb;
  15413. heap->dbg_read_flush_cb = read_flush_cb;
  15414. heap->dbg_write_flush_cb = write_flush_cb;
  15415. heap->dbg_detached_cb = detached_cb;
  15416. heap->dbg_udata = udata;
  15417. heap->dbg_have_next_byte = 0;
  15418. /* Start in paused state. */
  15419. heap->dbg_processing = 0;
  15420. heap->dbg_paused = 1;
  15421. heap->dbg_state_dirty = 1;
  15422. heap->dbg_force_restart = 0;
  15423. heap->dbg_step_type = 0;
  15424. heap->dbg_step_thread = NULL;
  15425. heap->dbg_step_csindex = 0;
  15426. heap->dbg_step_startline = 0;
  15427. heap->dbg_exec_counter = 0;
  15428. heap->dbg_last_counter = 0;
  15429. heap->dbg_last_time = 0.0;
  15430. /* Send version identification and flush right afterwards. Note that
  15431. * we must write raw, unframed bytes here.
  15432. */
  15433. duk_push_sprintf(ctx, "%ld %ld %s %s\n",
  15434. (long) DUK_DEBUG_PROTOCOL_VERSION,
  15435. (long) DUK_VERSION,
  15436. (const char *) DUK_GIT_DESCRIBE,
  15437. (const char *) DUK_USE_TARGET_INFO);
  15438. str = duk_get_lstring(ctx, -1, &len);
  15439. DUK_ASSERT(str != NULL);
  15440. duk_debug_write_bytes(thr, (const duk_uint8_t *) str, len);
  15441. duk_debug_write_flush(thr);
  15442. duk_pop(ctx);
  15443. }
  15444. DUK_EXTERNAL void duk_debugger_detach(duk_context *ctx) {
  15445. duk_hthread *thr;
  15446. DUK_ASSERT_CTX_VALID(ctx);
  15447. thr = (duk_hthread *) ctx;
  15448. DUK_ASSERT(thr != NULL);
  15449. DUK_ASSERT(thr->heap != NULL);
  15450. /* Can be called multiple times with no harm. */
  15451. duk_debug_do_detach(thr->heap);
  15452. }
  15453. DUK_EXTERNAL void duk_debugger_cooperate(duk_context *ctx) {
  15454. duk_hthread *thr;
  15455. duk_bool_t processed_messages;
  15456. DUK_ASSERT_CTX_VALID(ctx);
  15457. thr = (duk_hthread *) ctx;
  15458. DUK_ASSERT(thr != NULL);
  15459. DUK_ASSERT(thr->heap != NULL);
  15460. if (!DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) {
  15461. return;
  15462. }
  15463. if (thr->callstack_top > 0 || thr->heap->dbg_processing) {
  15464. /* Calling duk_debugger_cooperate() while Duktape is being
  15465. * called into is not supported. This is not a 100% check
  15466. * but prevents any damage in most cases.
  15467. */
  15468. return;
  15469. }
  15470. thr->heap->dbg_processing = 1;
  15471. processed_messages = duk_debug_process_messages(thr, 1 /*no_block*/);
  15472. thr->heap->dbg_processing = 0;
  15473. DUK_UNREF(processed_messages);
  15474. }
  15475. #else /* DUK_USE_DEBUGGER_SUPPORT */
  15476. DUK_EXTERNAL void duk_debugger_attach(duk_context *ctx,
  15477. duk_debug_read_function read_cb,
  15478. duk_debug_write_function write_cb,
  15479. duk_debug_peek_function peek_cb,
  15480. duk_debug_read_flush_function read_flush_cb,
  15481. duk_debug_write_flush_function write_flush_cb,
  15482. duk_debug_detached_function detached_cb,
  15483. void *udata) {
  15484. DUK_ASSERT_CTX_VALID(ctx);
  15485. DUK_UNREF(read_cb);
  15486. DUK_UNREF(write_cb);
  15487. DUK_UNREF(peek_cb);
  15488. DUK_UNREF(read_flush_cb);
  15489. DUK_UNREF(write_flush_cb);
  15490. DUK_UNREF(detached_cb);
  15491. DUK_UNREF(udata);
  15492. duk_error(ctx, DUK_ERR_API_ERROR, "no debugger support");
  15493. }
  15494. DUK_EXTERNAL void duk_debugger_detach(duk_context *ctx) {
  15495. DUK_ASSERT_CTX_VALID(ctx);
  15496. duk_error(ctx, DUK_ERR_API_ERROR, "no debugger support");
  15497. }
  15498. DUK_EXTERNAL void duk_debugger_cooperate(duk_context *ctx) {
  15499. /* nop */
  15500. DUK_ASSERT_CTX_VALID(ctx);
  15501. DUK_UNREF(ctx);
  15502. }
  15503. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  15504. #line 1 "duk_api_heap.c"
  15505. /*
  15506. * Heap creation and destruction
  15507. */
  15508. /* include removed: duk_internal.h */
  15509. DUK_EXTERNAL
  15510. duk_context *duk_create_heap(duk_alloc_function alloc_func,
  15511. duk_realloc_function realloc_func,
  15512. duk_free_function free_func,
  15513. void *heap_udata,
  15514. duk_fatal_function fatal_handler) {
  15515. duk_heap *heap = NULL;
  15516. duk_context *ctx;
  15517. /* Assume that either all memory funcs are NULL or non-NULL, mixed
  15518. * cases will now be unsafe.
  15519. */
  15520. /* XXX: just assert non-NULL values here and make caller arguments
  15521. * do the defaulting to the default implementations (smaller code)?
  15522. */
  15523. if (!alloc_func) {
  15524. DUK_ASSERT(realloc_func == NULL);
  15525. DUK_ASSERT(free_func == NULL);
  15526. alloc_func = duk_default_alloc_function;
  15527. realloc_func = duk_default_realloc_function;
  15528. free_func = duk_default_free_function;
  15529. } else {
  15530. DUK_ASSERT(realloc_func != NULL);
  15531. DUK_ASSERT(free_func != NULL);
  15532. }
  15533. if (!fatal_handler) {
  15534. fatal_handler = duk_default_fatal_handler;
  15535. }
  15536. DUK_ASSERT(alloc_func != NULL);
  15537. DUK_ASSERT(realloc_func != NULL);
  15538. DUK_ASSERT(free_func != NULL);
  15539. DUK_ASSERT(fatal_handler != NULL);
  15540. heap = duk_heap_alloc(alloc_func, realloc_func, free_func, heap_udata, fatal_handler);
  15541. if (!heap) {
  15542. return NULL;
  15543. }
  15544. ctx = (duk_context *) heap->heap_thread;
  15545. DUK_ASSERT(ctx != NULL);
  15546. DUK_ASSERT(((duk_hthread *) ctx)->heap != NULL);
  15547. return ctx;
  15548. }
  15549. DUK_EXTERNAL void duk_destroy_heap(duk_context *ctx) {
  15550. duk_hthread *thr = (duk_hthread *) ctx;
  15551. duk_heap *heap;
  15552. if (!ctx) {
  15553. return;
  15554. }
  15555. heap = thr->heap;
  15556. DUK_ASSERT(heap != NULL);
  15557. duk_heap_free(heap);
  15558. }
  15559. /* XXX: better place for this */
  15560. DUK_EXTERNAL void duk_set_global_object(duk_context *ctx) {
  15561. duk_hthread *thr = (duk_hthread *) ctx;
  15562. duk_hobject *h_glob;
  15563. duk_hobject *h_prev_glob;
  15564. duk_hobject *h_env;
  15565. duk_hobject *h_prev_env;
  15566. DUK_D(DUK_DPRINT("replace global object with: %!T", duk_get_tval(ctx, -1)));
  15567. h_glob = duk_require_hobject(ctx, -1);
  15568. DUK_ASSERT(h_glob != NULL);
  15569. /*
  15570. * Replace global object.
  15571. */
  15572. h_prev_glob = thr->builtins[DUK_BIDX_GLOBAL];
  15573. DUK_UNREF(h_prev_glob);
  15574. thr->builtins[DUK_BIDX_GLOBAL] = h_glob;
  15575. DUK_HOBJECT_INCREF(thr, h_glob);
  15576. DUK_HOBJECT_DECREF_ALLOWNULL(thr, h_prev_glob); /* side effects, in theory (referenced by global env) */
  15577. /*
  15578. * Replace lexical environment for global scope
  15579. *
  15580. * Create a new object environment for the global lexical scope.
  15581. * We can't just reset the _Target property of the current one,
  15582. * because the lexical scope is shared by other threads with the
  15583. * same (initial) built-ins.
  15584. */
  15585. (void) duk_push_object_helper(ctx,
  15586. DUK_HOBJECT_FLAG_EXTENSIBLE |
  15587. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJENV),
  15588. -1); /* no prototype, updated below */
  15589. duk_dup(ctx, -2);
  15590. duk_dup(ctx, -3);
  15591. /* [ ... new_glob new_env new_glob new_glob ] */
  15592. duk_xdef_prop_stridx(thr, -3, DUK_STRIDX_INT_TARGET, DUK_PROPDESC_FLAGS_NONE);
  15593. duk_xdef_prop_stridx(thr, -2, DUK_STRIDX_INT_THIS, DUK_PROPDESC_FLAGS_NONE);
  15594. /* [ ... new_glob new_env ] */
  15595. h_env = duk_get_hobject(ctx, -1);
  15596. DUK_ASSERT(h_env != NULL);
  15597. h_prev_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  15598. thr->builtins[DUK_BIDX_GLOBAL_ENV] = h_env;
  15599. DUK_HOBJECT_INCREF(thr, h_env);
  15600. DUK_HOBJECT_DECREF_ALLOWNULL(thr, h_prev_env); /* side effects */
  15601. DUK_UNREF(h_env); /* without refcounts */
  15602. DUK_UNREF(h_prev_env);
  15603. /* [ ... new_glob new_env ] */
  15604. duk_pop_2(ctx);
  15605. /* [ ... ] */
  15606. }
  15607. #line 1 "duk_api_logging.c"
  15608. /*
  15609. * Logging
  15610. *
  15611. * Current logging primitive is a sprintf-style log which is convenient
  15612. * for most C code. Another useful primitive would be to log N arguments
  15613. * from value stack (like the Ecmascript binding does).
  15614. */
  15615. /* include removed: duk_internal.h */
  15616. DUK_EXTERNAL void duk_log_va(duk_context *ctx, duk_int_t level, const char *fmt, va_list ap) {
  15617. /* stridx_logfunc[] must be static to allow initializer with old compilers like BCC */
  15618. static const duk_uint16_t stridx_logfunc[6] = {
  15619. DUK_STRIDX_LC_TRACE, DUK_STRIDX_LC_DEBUG, DUK_STRIDX_LC_INFO,
  15620. DUK_STRIDX_LC_WARN, DUK_STRIDX_LC_ERROR, DUK_STRIDX_LC_FATAL
  15621. };
  15622. DUK_ASSERT_CTX_VALID(ctx);
  15623. if (level < 0) {
  15624. level = 0;
  15625. } else if (level > (int) (sizeof(stridx_logfunc) / sizeof(duk_uint16_t)) - 1) {
  15626. level = (int) (sizeof(stridx_logfunc) / sizeof(duk_uint16_t)) - 1;
  15627. }
  15628. duk_push_hobject_bidx(ctx, DUK_BIDX_LOGGER_CONSTRUCTOR);
  15629. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_CLOG);
  15630. duk_get_prop_stridx(ctx, -1, stridx_logfunc[level]);
  15631. duk_dup(ctx, -2);
  15632. /* [ ... Logger clog logfunc clog ] */
  15633. duk_push_vsprintf(ctx, fmt, ap);
  15634. /* [ ... Logger clog logfunc clog(=this) msg ] */
  15635. duk_call_method(ctx, 1 /*nargs*/);
  15636. /* [ ... Logger clog res ] */
  15637. duk_pop_3(ctx);
  15638. }
  15639. DUK_EXTERNAL void duk_log(duk_context *ctx, duk_int_t level, const char *fmt, ...) {
  15640. va_list ap;
  15641. DUK_ASSERT_CTX_VALID(ctx);
  15642. va_start(ap, fmt);
  15643. duk_log_va(ctx, level, fmt, ap);
  15644. va_end(ap);
  15645. }
  15646. #line 1 "duk_api_memory.c"
  15647. /*
  15648. * Memory calls.
  15649. */
  15650. /* include removed: duk_internal.h */
  15651. DUK_EXTERNAL void *duk_alloc_raw(duk_context *ctx, duk_size_t size) {
  15652. duk_hthread *thr = (duk_hthread *) ctx;
  15653. DUK_ASSERT_CTX_VALID(ctx);
  15654. return DUK_ALLOC_RAW(thr->heap, size);
  15655. }
  15656. DUK_EXTERNAL void duk_free_raw(duk_context *ctx, void *ptr) {
  15657. duk_hthread *thr = (duk_hthread *) ctx;
  15658. DUK_ASSERT_CTX_VALID(ctx);
  15659. DUK_FREE_RAW(thr->heap, ptr);
  15660. }
  15661. DUK_EXTERNAL void *duk_realloc_raw(duk_context *ctx, void *ptr, duk_size_t size) {
  15662. duk_hthread *thr = (duk_hthread *) ctx;
  15663. DUK_ASSERT_CTX_VALID(ctx);
  15664. return DUK_REALLOC_RAW(thr->heap, ptr, size);
  15665. }
  15666. DUK_EXTERNAL void *duk_alloc(duk_context *ctx, duk_size_t size) {
  15667. duk_hthread *thr = (duk_hthread *) ctx;
  15668. DUK_ASSERT_CTX_VALID(ctx);
  15669. return DUK_ALLOC(thr->heap, size);
  15670. }
  15671. DUK_EXTERNAL void duk_free(duk_context *ctx, void *ptr) {
  15672. duk_hthread *thr = (duk_hthread *) ctx;
  15673. DUK_ASSERT_CTX_VALID(ctx);
  15674. DUK_FREE(thr->heap, ptr);
  15675. }
  15676. DUK_EXTERNAL void *duk_realloc(duk_context *ctx, void *ptr, duk_size_t size) {
  15677. duk_hthread *thr = (duk_hthread *) ctx;
  15678. DUK_ASSERT_CTX_VALID(ctx);
  15679. /*
  15680. * Note: since this is an exposed API call, there should be
  15681. * no way a mark-and-sweep could have a side effect on the
  15682. * memory allocation behind 'ptr'; the pointer should never
  15683. * be something that Duktape wants to change.
  15684. *
  15685. * Thus, no need to use DUK_REALLOC_INDIRECT (and we don't
  15686. * have the storage location here anyway).
  15687. */
  15688. return DUK_REALLOC(thr->heap, ptr, size);
  15689. }
  15690. DUK_EXTERNAL void duk_get_memory_functions(duk_context *ctx, duk_memory_functions *out_funcs) {
  15691. duk_hthread *thr = (duk_hthread *) ctx;
  15692. duk_heap *heap;
  15693. DUK_ASSERT_CTX_VALID(ctx);
  15694. DUK_ASSERT(out_funcs != NULL);
  15695. DUK_ASSERT(thr != NULL);
  15696. DUK_ASSERT(thr->heap != NULL);
  15697. heap = thr->heap;
  15698. out_funcs->alloc_func = heap->alloc_func;
  15699. out_funcs->realloc_func = heap->realloc_func;
  15700. out_funcs->free_func = heap->free_func;
  15701. out_funcs->udata = heap->heap_udata;
  15702. }
  15703. DUK_EXTERNAL void duk_gc(duk_context *ctx, duk_uint_t flags) {
  15704. #ifdef DUK_USE_MARK_AND_SWEEP
  15705. duk_hthread *thr = (duk_hthread *) ctx;
  15706. duk_heap *heap;
  15707. DUK_UNREF(flags);
  15708. /* NULL accepted */
  15709. if (!ctx) {
  15710. return;
  15711. }
  15712. DUK_ASSERT_CTX_VALID(ctx);
  15713. heap = thr->heap;
  15714. DUK_ASSERT(heap != NULL);
  15715. DUK_D(DUK_DPRINT("mark-and-sweep requested by application"));
  15716. duk_heap_mark_and_sweep(heap, 0);
  15717. #else
  15718. DUK_D(DUK_DPRINT("mark-and-sweep requested by application but mark-and-sweep not enabled, ignoring"));
  15719. DUK_UNREF(ctx);
  15720. DUK_UNREF(flags);
  15721. #endif
  15722. }
  15723. #line 1 "duk_api_object.c"
  15724. /*
  15725. * Object handling: property access and other support functions.
  15726. */
  15727. /* include removed: duk_internal.h */
  15728. /*
  15729. * Property handling
  15730. *
  15731. * The API exposes only the most common property handling functions.
  15732. * The caller can invoke Ecmascript built-ins for full control (e.g.
  15733. * defineProperty, getOwnPropertyDescriptor).
  15734. */
  15735. DUK_EXTERNAL duk_bool_t duk_get_prop(duk_context *ctx, duk_idx_t obj_index) {
  15736. duk_hthread *thr = (duk_hthread *) ctx;
  15737. duk_tval *tv_obj;
  15738. duk_tval *tv_key;
  15739. duk_bool_t rc;
  15740. DUK_ASSERT_CTX_VALID(ctx);
  15741. /* Note: copying tv_obj and tv_key to locals to shield against a valstack
  15742. * resize is not necessary for a property get right now.
  15743. */
  15744. tv_obj = duk_require_tval(ctx, obj_index);
  15745. tv_key = duk_require_tval(ctx, -1);
  15746. rc = duk_hobject_getprop(thr, tv_obj, tv_key);
  15747. DUK_ASSERT(rc == 0 || rc == 1);
  15748. /* a value is left on stack regardless of rc */
  15749. duk_remove(ctx, -2); /* remove key */
  15750. return rc; /* 1 if property found, 0 otherwise */
  15751. }
  15752. DUK_EXTERNAL duk_bool_t duk_get_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) {
  15753. DUK_ASSERT_CTX_VALID(ctx);
  15754. DUK_ASSERT(key != NULL);
  15755. obj_index = duk_require_normalize_index(ctx, obj_index);
  15756. duk_push_string(ctx, key);
  15757. return duk_get_prop(ctx, obj_index);
  15758. }
  15759. DUK_EXTERNAL duk_bool_t duk_get_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) {
  15760. DUK_ASSERT_CTX_VALID(ctx);
  15761. obj_index = duk_require_normalize_index(ctx, obj_index);
  15762. duk_push_uarridx(ctx, arr_index);
  15763. return duk_get_prop(ctx, obj_index);
  15764. }
  15765. DUK_INTERNAL duk_bool_t duk_get_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx) {
  15766. duk_hthread *thr = (duk_hthread *) ctx;
  15767. DUK_ASSERT_CTX_VALID(ctx);
  15768. DUK_ASSERT_DISABLE(stridx >= 0);
  15769. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  15770. obj_index = duk_require_normalize_index(ctx, obj_index);
  15771. duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx));
  15772. return duk_get_prop(ctx, obj_index);
  15773. }
  15774. DUK_INTERNAL duk_bool_t duk_get_prop_stridx_boolean(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_bool_t *out_has_prop) {
  15775. duk_bool_t rc;
  15776. DUK_ASSERT_CTX_VALID(ctx);
  15777. DUK_ASSERT_DISABLE(stridx >= 0);
  15778. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  15779. rc = duk_get_prop_stridx(ctx, obj_index, stridx);
  15780. if (out_has_prop) {
  15781. *out_has_prop = rc;
  15782. }
  15783. rc = duk_to_boolean(ctx, -1);
  15784. DUK_ASSERT(rc == 0 || rc == 1);
  15785. duk_pop(ctx);
  15786. return rc;
  15787. }
  15788. DUK_EXTERNAL duk_bool_t duk_put_prop(duk_context *ctx, duk_idx_t obj_index) {
  15789. duk_hthread *thr = (duk_hthread *) ctx;
  15790. duk_tval *tv_obj;
  15791. duk_tval *tv_key;
  15792. duk_tval *tv_val;
  15793. duk_small_int_t throw_flag;
  15794. duk_bool_t rc;
  15795. DUK_ASSERT_CTX_VALID(ctx);
  15796. /* Note: copying tv_obj and tv_key to locals to shield against a valstack
  15797. * resize is not necessary for a property put right now (putprop protects
  15798. * against it internally).
  15799. */
  15800. tv_obj = duk_require_tval(ctx, obj_index);
  15801. tv_key = duk_require_tval(ctx, -2);
  15802. tv_val = duk_require_tval(ctx, -1);
  15803. throw_flag = duk_is_strict_call(ctx);
  15804. rc = duk_hobject_putprop(thr, tv_obj, tv_key, tv_val, throw_flag);
  15805. DUK_ASSERT(rc == 0 || rc == 1);
  15806. duk_pop_2(ctx); /* remove key and value */
  15807. return rc; /* 1 if property found, 0 otherwise */
  15808. }
  15809. DUK_EXTERNAL duk_bool_t duk_put_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) {
  15810. DUK_ASSERT_CTX_VALID(ctx);
  15811. DUK_ASSERT(key != NULL);
  15812. obj_index = duk_require_normalize_index(ctx, obj_index);
  15813. duk_push_string(ctx, key);
  15814. duk_swap_top(ctx, -2); /* [val key] -> [key val] */
  15815. return duk_put_prop(ctx, obj_index);
  15816. }
  15817. DUK_EXTERNAL duk_bool_t duk_put_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) {
  15818. DUK_ASSERT_CTX_VALID(ctx);
  15819. obj_index = duk_require_normalize_index(ctx, obj_index);
  15820. duk_push_uarridx(ctx, arr_index);
  15821. duk_swap_top(ctx, -2); /* [val key] -> [key val] */
  15822. return duk_put_prop(ctx, obj_index);
  15823. }
  15824. DUK_INTERNAL duk_bool_t duk_put_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx) {
  15825. duk_hthread *thr = (duk_hthread *) ctx;
  15826. DUK_ASSERT_CTX_VALID(ctx);
  15827. DUK_ASSERT_DISABLE(stridx >= 0);
  15828. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  15829. obj_index = duk_require_normalize_index(ctx, obj_index);
  15830. duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx));
  15831. duk_swap_top(ctx, -2); /* [val key] -> [key val] */
  15832. return duk_put_prop(ctx, obj_index);
  15833. }
  15834. DUK_EXTERNAL duk_bool_t duk_del_prop(duk_context *ctx, duk_idx_t obj_index) {
  15835. duk_hthread *thr = (duk_hthread *) ctx;
  15836. duk_tval *tv_obj;
  15837. duk_tval *tv_key;
  15838. duk_small_int_t throw_flag;
  15839. duk_bool_t rc;
  15840. DUK_ASSERT_CTX_VALID(ctx);
  15841. /* Note: copying tv_obj and tv_key to locals to shield against a valstack
  15842. * resize is not necessary for a property delete right now.
  15843. */
  15844. tv_obj = duk_require_tval(ctx, obj_index);
  15845. tv_key = duk_require_tval(ctx, -1);
  15846. throw_flag = duk_is_strict_call(ctx);
  15847. rc = duk_hobject_delprop(thr, tv_obj, tv_key, throw_flag);
  15848. DUK_ASSERT(rc == 0 || rc == 1);
  15849. duk_pop(ctx); /* remove key */
  15850. return rc;
  15851. }
  15852. DUK_EXTERNAL duk_bool_t duk_del_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) {
  15853. DUK_ASSERT_CTX_VALID(ctx);
  15854. DUK_ASSERT(key != NULL);
  15855. obj_index = duk_require_normalize_index(ctx, obj_index);
  15856. duk_push_string(ctx, key);
  15857. return duk_del_prop(ctx, obj_index);
  15858. }
  15859. DUK_EXTERNAL duk_bool_t duk_del_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) {
  15860. DUK_ASSERT_CTX_VALID(ctx);
  15861. obj_index = duk_require_normalize_index(ctx, obj_index);
  15862. duk_push_uarridx(ctx, arr_index);
  15863. return duk_del_prop(ctx, obj_index);
  15864. }
  15865. DUK_INTERNAL duk_bool_t duk_del_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx) {
  15866. duk_hthread *thr = (duk_hthread *) ctx;
  15867. DUK_ASSERT_CTX_VALID(ctx);
  15868. DUK_ASSERT_DISABLE(stridx >= 0);
  15869. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  15870. obj_index = duk_require_normalize_index(ctx, obj_index);
  15871. duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx));
  15872. return duk_del_prop(ctx, obj_index);
  15873. }
  15874. DUK_EXTERNAL duk_bool_t duk_has_prop(duk_context *ctx, duk_idx_t obj_index) {
  15875. duk_hthread *thr = (duk_hthread *) ctx;
  15876. duk_tval *tv_obj;
  15877. duk_tval *tv_key;
  15878. duk_bool_t rc;
  15879. DUK_ASSERT_CTX_VALID(ctx);
  15880. /* Note: copying tv_obj and tv_key to locals to shield against a valstack
  15881. * resize is not necessary for a property existence check right now.
  15882. */
  15883. tv_obj = duk_require_tval(ctx, obj_index);
  15884. tv_key = duk_require_tval(ctx, -1);
  15885. rc = duk_hobject_hasprop(thr, tv_obj, tv_key);
  15886. DUK_ASSERT(rc == 0 || rc == 1);
  15887. duk_pop(ctx); /* remove key */
  15888. return rc; /* 1 if property found, 0 otherwise */
  15889. }
  15890. DUK_EXTERNAL duk_bool_t duk_has_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) {
  15891. DUK_ASSERT_CTX_VALID(ctx);
  15892. DUK_ASSERT(key != NULL);
  15893. obj_index = duk_require_normalize_index(ctx, obj_index);
  15894. duk_push_string(ctx, key);
  15895. return duk_has_prop(ctx, obj_index);
  15896. }
  15897. DUK_EXTERNAL duk_bool_t duk_has_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) {
  15898. DUK_ASSERT_CTX_VALID(ctx);
  15899. obj_index = duk_require_normalize_index(ctx, obj_index);
  15900. duk_push_uarridx(ctx, arr_index);
  15901. return duk_has_prop(ctx, obj_index);
  15902. }
  15903. DUK_INTERNAL duk_bool_t duk_has_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx) {
  15904. duk_hthread *thr = (duk_hthread *) ctx;
  15905. DUK_ASSERT_CTX_VALID(ctx);
  15906. DUK_ASSERT_DISABLE(stridx >= 0);
  15907. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  15908. obj_index = duk_require_normalize_index(ctx, obj_index);
  15909. duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx));
  15910. return duk_has_prop(ctx, obj_index);
  15911. }
  15912. /* Define own property without inheritance looks and such. This differs from
  15913. * [[DefineOwnProperty]] because special behaviors (like Array 'length') are
  15914. * not invoked by this method. The caller must be careful to invoke any such
  15915. * behaviors if necessary.
  15916. */
  15917. DUK_INTERNAL void duk_xdef_prop(duk_context *ctx, duk_idx_t obj_index, duk_small_uint_t desc_flags) {
  15918. duk_hthread *thr = (duk_hthread *) ctx;
  15919. duk_hobject *obj;
  15920. duk_hstring *key;
  15921. DUK_ASSERT_CTX_VALID(ctx);
  15922. obj = duk_require_hobject(ctx, obj_index);
  15923. DUK_ASSERT(obj != NULL);
  15924. key = duk_to_hstring(ctx, -2);
  15925. DUK_ASSERT(key != NULL);
  15926. DUK_ASSERT(duk_require_tval(ctx, -1) != NULL);
  15927. duk_hobject_define_property_internal(thr, obj, key, desc_flags);
  15928. duk_pop(ctx); /* pop key */
  15929. }
  15930. DUK_INTERNAL void duk_xdef_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index, duk_small_uint_t desc_flags) {
  15931. duk_hthread *thr = (duk_hthread *) ctx;
  15932. duk_hobject *obj;
  15933. DUK_ASSERT_CTX_VALID(ctx);
  15934. obj = duk_require_hobject(ctx, obj_index);
  15935. DUK_ASSERT(obj != NULL);
  15936. duk_hobject_define_property_internal_arridx(thr, obj, arr_index, desc_flags);
  15937. /* value popped by call */
  15938. }
  15939. DUK_INTERNAL void duk_xdef_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_uint_t desc_flags) {
  15940. duk_hthread *thr = (duk_hthread *) ctx;
  15941. duk_hobject *obj;
  15942. duk_hstring *key;
  15943. DUK_ASSERT_CTX_VALID(ctx);
  15944. DUK_ASSERT_DISABLE(stridx >= 0);
  15945. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  15946. obj = duk_require_hobject(ctx, obj_index);
  15947. DUK_ASSERT(obj != NULL);
  15948. key = DUK_HTHREAD_GET_STRING(thr, stridx);
  15949. DUK_ASSERT(key != NULL);
  15950. DUK_ASSERT(duk_require_tval(ctx, -1) != NULL);
  15951. duk_hobject_define_property_internal(thr, obj, key, desc_flags);
  15952. /* value popped by call */
  15953. }
  15954. DUK_INTERNAL void duk_xdef_prop_stridx_builtin(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_int_t builtin_idx, duk_small_uint_t desc_flags) {
  15955. duk_hthread *thr = (duk_hthread *) ctx;
  15956. duk_hobject *obj;
  15957. duk_hstring *key;
  15958. DUK_ASSERT_CTX_VALID(ctx);
  15959. DUK_ASSERT_DISABLE(stridx >= 0);
  15960. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  15961. DUK_ASSERT_DISABLE(builtin_idx >= 0);
  15962. DUK_ASSERT(builtin_idx < DUK_NUM_BUILTINS);
  15963. obj = duk_require_hobject(ctx, obj_index);
  15964. DUK_ASSERT(obj != NULL);
  15965. key = DUK_HTHREAD_GET_STRING(thr, stridx);
  15966. DUK_ASSERT(key != NULL);
  15967. duk_push_hobject(ctx, thr->builtins[builtin_idx]);
  15968. duk_hobject_define_property_internal(thr, obj, key, desc_flags);
  15969. /* value popped by call */
  15970. }
  15971. /* This is a rare property helper; it sets the global thrower (E5 Section 13.2.3)
  15972. * setter/getter into an object property. This is needed by the 'arguments'
  15973. * object creation code, function instance creation code, and Function.prototype.bind().
  15974. */
  15975. DUK_INTERNAL void duk_xdef_prop_stridx_thrower(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_uint_t desc_flags) {
  15976. duk_hthread *thr = (duk_hthread *) ctx;
  15977. duk_hobject *obj = duk_require_hobject(ctx, obj_index);
  15978. duk_hobject *thrower = thr->builtins[DUK_BIDX_TYPE_ERROR_THROWER];
  15979. duk_hobject_define_accessor_internal(thr, obj, DUK_HTHREAD_GET_STRING(thr, stridx), thrower, thrower, desc_flags);
  15980. }
  15981. /* Object.defineProperty() equivalent C binding. */
  15982. DUK_EXTERNAL void duk_def_prop(duk_context *ctx, duk_idx_t obj_index, duk_uint_t flags) {
  15983. duk_hthread *thr = (duk_hthread *) ctx;
  15984. duk_idx_t idx_base;
  15985. duk_hobject *obj;
  15986. duk_hstring *key;
  15987. duk_idx_t idx_value;
  15988. duk_hobject *get;
  15989. duk_hobject *set;
  15990. duk_uint_t is_data_desc;
  15991. duk_uint_t is_acc_desc;
  15992. DUK_ASSERT_CTX_VALID(ctx);
  15993. obj = duk_require_hobject(ctx, obj_index);
  15994. is_data_desc = flags & (DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE);
  15995. is_acc_desc = flags & (DUK_DEFPROP_HAVE_GETTER | DUK_DEFPROP_HAVE_SETTER);
  15996. if (is_data_desc && is_acc_desc) {
  15997. /* "Have" flags must not be conflicting so that they would
  15998. * apply to both a plain property and an accessor at the same
  15999. * time.
  16000. */
  16001. goto fail_invalid_desc;
  16002. }
  16003. idx_base = duk_get_top_index(ctx);
  16004. if (flags & DUK_DEFPROP_HAVE_SETTER) {
  16005. duk_require_type_mask(ctx, idx_base, DUK_TYPE_MASK_UNDEFINED |
  16006. DUK_TYPE_MASK_OBJECT |
  16007. DUK_TYPE_MASK_LIGHTFUNC);
  16008. set = duk_get_hobject_or_lfunc_coerce(ctx, idx_base);
  16009. if (set != NULL && !DUK_HOBJECT_IS_CALLABLE(set)) {
  16010. goto fail_not_callable;
  16011. }
  16012. idx_base--;
  16013. } else {
  16014. set = NULL;
  16015. }
  16016. if (flags & DUK_DEFPROP_HAVE_GETTER) {
  16017. duk_require_type_mask(ctx, idx_base, DUK_TYPE_MASK_UNDEFINED |
  16018. DUK_TYPE_MASK_OBJECT |
  16019. DUK_TYPE_MASK_LIGHTFUNC);
  16020. get = duk_get_hobject_or_lfunc_coerce(ctx, idx_base);
  16021. if (get != NULL && !DUK_HOBJECT_IS_CALLABLE(get)) {
  16022. goto fail_not_callable;
  16023. }
  16024. idx_base--;
  16025. } else {
  16026. get = NULL;
  16027. }
  16028. if (flags & DUK_DEFPROP_HAVE_VALUE) {
  16029. idx_value = idx_base;
  16030. idx_base--;
  16031. } else {
  16032. idx_value = (duk_idx_t) -1;
  16033. }
  16034. key = duk_require_hstring(ctx, idx_base);
  16035. duk_require_valid_index(ctx, idx_base);
  16036. duk_hobject_define_property_helper(ctx,
  16037. flags /*defprop_flags*/,
  16038. obj,
  16039. key,
  16040. idx_value,
  16041. get,
  16042. set);
  16043. /* Clean up stack */
  16044. duk_set_top(ctx, idx_base);
  16045. /* [ ... obj ... ] */
  16046. return;
  16047. fail_invalid_desc:
  16048. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_DESCRIPTOR);
  16049. return;
  16050. fail_not_callable:
  16051. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_CALLABLE);
  16052. return;
  16053. }
  16054. /*
  16055. * Object related
  16056. *
  16057. * Note: seal() and freeze() are accessible through Ecmascript bindings,
  16058. * and are not exposed through the API.
  16059. */
  16060. DUK_EXTERNAL void duk_compact(duk_context *ctx, duk_idx_t obj_index) {
  16061. duk_hthread *thr = (duk_hthread *) ctx;
  16062. duk_hobject *obj;
  16063. DUK_ASSERT_CTX_VALID(ctx);
  16064. obj = duk_get_hobject(ctx, obj_index);
  16065. if (obj) {
  16066. /* Note: this may fail, caller should protect the call if necessary */
  16067. duk_hobject_compact_props(thr, obj);
  16068. }
  16069. }
  16070. /* XXX: the duk_hobject_enum.c stack APIs should be reworked */
  16071. DUK_EXTERNAL void duk_enum(duk_context *ctx, duk_idx_t obj_index, duk_uint_t enum_flags) {
  16072. DUK_ASSERT_CTX_VALID(ctx);
  16073. duk_dup(ctx, obj_index);
  16074. duk_require_hobject_or_lfunc_coerce(ctx, -1);
  16075. duk_hobject_enumerator_create(ctx, enum_flags); /* [target] -> [enum] */
  16076. }
  16077. DUK_EXTERNAL duk_bool_t duk_next(duk_context *ctx, duk_idx_t enum_index, duk_bool_t get_value) {
  16078. DUK_ASSERT_CTX_VALID(ctx);
  16079. duk_require_hobject(ctx, enum_index);
  16080. duk_dup(ctx, enum_index);
  16081. return duk_hobject_enumerator_next(ctx, get_value);
  16082. }
  16083. /*
  16084. * Helpers for writing multiple properties
  16085. */
  16086. DUK_EXTERNAL void duk_put_function_list(duk_context *ctx, duk_idx_t obj_index, const duk_function_list_entry *funcs) {
  16087. const duk_function_list_entry *ent = funcs;
  16088. DUK_ASSERT_CTX_VALID(ctx);
  16089. obj_index = duk_require_normalize_index(ctx, obj_index);
  16090. if (ent != NULL) {
  16091. while (ent->key != NULL) {
  16092. duk_push_c_function(ctx, ent->value, ent->nargs);
  16093. duk_put_prop_string(ctx, obj_index, ent->key);
  16094. ent++;
  16095. }
  16096. }
  16097. }
  16098. DUK_EXTERNAL void duk_put_number_list(duk_context *ctx, duk_idx_t obj_index, const duk_number_list_entry *numbers) {
  16099. const duk_number_list_entry *ent = numbers;
  16100. DUK_ASSERT_CTX_VALID(ctx);
  16101. obj_index = duk_require_normalize_index(ctx, obj_index);
  16102. if (ent != NULL) {
  16103. while (ent->key != NULL) {
  16104. duk_push_number(ctx, ent->value);
  16105. duk_put_prop_string(ctx, obj_index, ent->key);
  16106. ent++;
  16107. }
  16108. }
  16109. }
  16110. /*
  16111. * Shortcut for accessing global object properties
  16112. */
  16113. DUK_EXTERNAL duk_bool_t duk_get_global_string(duk_context *ctx, const char *key) {
  16114. duk_hthread *thr = (duk_hthread *) ctx;
  16115. duk_bool_t ret;
  16116. DUK_ASSERT_CTX_VALID(ctx);
  16117. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
  16118. /* XXX: direct implementation */
  16119. duk_push_hobject(ctx, thr->builtins[DUK_BIDX_GLOBAL]);
  16120. ret = duk_get_prop_string(ctx, -1, key);
  16121. duk_remove(ctx, -2);
  16122. return ret;
  16123. }
  16124. DUK_EXTERNAL duk_bool_t duk_put_global_string(duk_context *ctx, const char *key) {
  16125. duk_hthread *thr = (duk_hthread *) ctx;
  16126. duk_bool_t ret;
  16127. DUK_ASSERT_CTX_VALID(ctx);
  16128. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
  16129. /* XXX: direct implementation */
  16130. duk_push_hobject(ctx, thr->builtins[DUK_BIDX_GLOBAL]);
  16131. duk_insert(ctx, -2);
  16132. ret = duk_put_prop_string(ctx, -2, key); /* [ ... global val ] -> [ ... global ] */
  16133. duk_pop(ctx);
  16134. return ret;
  16135. }
  16136. /*
  16137. * Object prototype
  16138. */
  16139. DUK_EXTERNAL void duk_get_prototype(duk_context *ctx, duk_idx_t index) {
  16140. duk_hthread *thr = (duk_hthread *) ctx;
  16141. duk_hobject *obj;
  16142. duk_hobject *proto;
  16143. DUK_ASSERT_CTX_VALID(ctx);
  16144. DUK_UNREF(thr);
  16145. obj = duk_require_hobject(ctx, index);
  16146. DUK_ASSERT(obj != NULL);
  16147. /* XXX: shared helper for duk_push_hobject_or_undefined()? */
  16148. proto = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, obj);
  16149. if (proto) {
  16150. duk_push_hobject(ctx, proto);
  16151. } else {
  16152. duk_push_undefined(ctx);
  16153. }
  16154. }
  16155. DUK_EXTERNAL void duk_set_prototype(duk_context *ctx, duk_idx_t index) {
  16156. duk_hthread *thr = (duk_hthread *) ctx;
  16157. duk_hobject *obj;
  16158. duk_hobject *proto;
  16159. DUK_ASSERT_CTX_VALID(ctx);
  16160. obj = duk_require_hobject(ctx, index);
  16161. DUK_ASSERT(obj != NULL);
  16162. duk_require_type_mask(ctx, -1, DUK_TYPE_MASK_UNDEFINED |
  16163. DUK_TYPE_MASK_OBJECT);
  16164. proto = duk_get_hobject(ctx, -1);
  16165. /* proto can also be NULL here (allowed explicitly) */
  16166. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, obj, proto);
  16167. duk_pop(ctx);
  16168. }
  16169. /*
  16170. * Object finalizer
  16171. */
  16172. /* XXX: these could be implemented as macros calling an internal function
  16173. * directly.
  16174. * XXX: same issue as with Duktape.fin: there's no way to delete the property
  16175. * now (just set it to undefined).
  16176. */
  16177. DUK_EXTERNAL void duk_get_finalizer(duk_context *ctx, duk_idx_t index) {
  16178. DUK_ASSERT_CTX_VALID(ctx);
  16179. duk_get_prop_stridx(ctx, index, DUK_STRIDX_INT_FINALIZER);
  16180. }
  16181. DUK_EXTERNAL void duk_set_finalizer(duk_context *ctx, duk_idx_t index) {
  16182. DUK_ASSERT_CTX_VALID(ctx);
  16183. duk_put_prop_stridx(ctx, index, DUK_STRIDX_INT_FINALIZER);
  16184. }
  16185. #line 1 "duk_api_stack.c"
  16186. /*
  16187. * API calls related to general value stack manipulation: resizing the value
  16188. * stack, pushing and popping values, type checking and reading values,
  16189. * coercing values, etc.
  16190. *
  16191. * Also contains internal functions (such as duk_get_tval()), defined
  16192. * in duk_api_internal.h, with semantics similar to the public API.
  16193. */
  16194. /* XXX: repetition of stack pre-checks -> helper or macro or inline */
  16195. /* XXX: shared api error strings, and perhaps even throw code for rare cases? */
  16196. /* include removed: duk_internal.h */
  16197. /*
  16198. * Forward declarations
  16199. */
  16200. DUK_LOCAL_DECL duk_idx_t duk__push_c_function_raw(duk_context *ctx, duk_c_function func, duk_idx_t nargs, duk_uint_t flags);
  16201. /*
  16202. * Global state for working around missing variadic macros
  16203. */
  16204. #ifndef DUK_USE_VARIADIC_MACROS
  16205. DUK_EXTERNAL const char *duk_api_global_filename = NULL;
  16206. DUK_EXTERNAL duk_int_t duk_api_global_line = 0;
  16207. #endif
  16208. /*
  16209. * Helpers
  16210. */
  16211. /* Check that there's room to push one value. */
  16212. #if defined(DUK_USE_VALSTACK_UNSAFE)
  16213. /* Faster but value stack overruns are memory unsafe. */
  16214. #define DUK__CHECK_SPACE() do { \
  16215. DUK_ASSERT(!(thr->valstack_top >= thr->valstack_end)); \
  16216. } while (0)
  16217. #else
  16218. #define DUK__CHECK_SPACE() do { \
  16219. if (thr->valstack_top >= thr->valstack_end) { \
  16220. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK); \
  16221. } \
  16222. } while (0)
  16223. #endif
  16224. DUK_LOCAL duk_int_t duk__api_coerce_d2i(duk_context *ctx, duk_idx_t index, duk_bool_t require) {
  16225. duk_hthread *thr;
  16226. duk_tval *tv;
  16227. duk_small_int_t c;
  16228. duk_double_t d;
  16229. thr = (duk_hthread *) ctx;
  16230. tv = duk_get_tval(ctx, index);
  16231. if (tv == NULL) {
  16232. goto error_notnumber;
  16233. }
  16234. /*
  16235. * Special cases like NaN and +/- Infinity are handled explicitly
  16236. * because a plain C coercion from double to int handles these cases
  16237. * in undesirable ways. For instance, NaN may coerce to INT_MIN
  16238. * (not zero), and INT_MAX + 1 may coerce to INT_MIN (not INT_MAX).
  16239. *
  16240. * This double-to-int coercion differs from ToInteger() because it
  16241. * has a finite range (ToInteger() allows e.g. +/- Infinity). It
  16242. * also differs from ToInt32() because the INT_MIN/INT_MAX clamping
  16243. * depends on the size of the int type on the platform. In particular,
  16244. * on platforms with a 64-bit int type, the full range is allowed.
  16245. */
  16246. #if defined(DUK_USE_FASTINT)
  16247. if (DUK_TVAL_IS_FASTINT(tv)) {
  16248. duk_int64_t t = DUK_TVAL_GET_FASTINT(tv);
  16249. #if (DUK_INT_MAX <= 0x7fffffffL)
  16250. /* Clamping only necessary for 32-bit ints. */
  16251. if (t < DUK_INT_MIN) {
  16252. t = DUK_INT_MIN;
  16253. } else if (t > DUK_INT_MAX) {
  16254. t = DUK_INT_MAX;
  16255. }
  16256. #endif
  16257. return (duk_int_t) t;
  16258. }
  16259. #endif
  16260. if (DUK_TVAL_IS_NUMBER(tv)) {
  16261. d = DUK_TVAL_GET_NUMBER(tv);
  16262. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  16263. if (c == DUK_FP_NAN) {
  16264. return 0;
  16265. } else if (d < (duk_double_t) DUK_INT_MIN) {
  16266. /* covers -Infinity */
  16267. return DUK_INT_MIN;
  16268. } else if (d > (duk_double_t) DUK_INT_MAX) {
  16269. /* covers +Infinity */
  16270. return DUK_INT_MAX;
  16271. } else {
  16272. /* coerce towards zero */
  16273. return (duk_int_t) d;
  16274. }
  16275. }
  16276. error_notnumber:
  16277. if (require) {
  16278. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_NUMBER);
  16279. /* not reachable */
  16280. }
  16281. return 0;
  16282. }
  16283. DUK_LOCAL duk_uint_t duk__api_coerce_d2ui(duk_context *ctx, duk_idx_t index, duk_bool_t require) {
  16284. duk_hthread *thr;
  16285. duk_tval *tv;
  16286. duk_small_int_t c;
  16287. duk_double_t d;
  16288. /* Same as above but for unsigned int range. */
  16289. thr = (duk_hthread *) ctx;
  16290. tv = duk_get_tval(ctx, index);
  16291. if (tv == NULL) {
  16292. goto error_notnumber;
  16293. }
  16294. #if defined(DUK_USE_FASTINT)
  16295. if (DUK_TVAL_IS_FASTINT(tv)) {
  16296. duk_int64_t t = DUK_TVAL_GET_FASTINT(tv);
  16297. if (t < 0) {
  16298. t = 0;
  16299. }
  16300. #if (DUK_UINT_MAX <= 0xffffffffUL)
  16301. /* Clamping only necessary for 32-bit ints. */
  16302. else if (t > DUK_UINT_MAX) {
  16303. t = DUK_UINT_MAX;
  16304. }
  16305. #endif
  16306. return (duk_uint_t) t;
  16307. }
  16308. #endif
  16309. if (DUK_TVAL_IS_NUMBER(tv)) {
  16310. d = DUK_TVAL_GET_NUMBER(tv);
  16311. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  16312. if (c == DUK_FP_NAN) {
  16313. return 0;
  16314. } else if (d < 0.0) {
  16315. /* covers -Infinity */
  16316. return (duk_uint_t) 0;
  16317. } else if (d > (duk_double_t) DUK_UINT_MAX) {
  16318. /* covers +Infinity */
  16319. return (duk_uint_t) DUK_UINT_MAX;
  16320. } else {
  16321. /* coerce towards zero */
  16322. return (duk_uint_t) d;
  16323. }
  16324. }
  16325. error_notnumber:
  16326. if (require) {
  16327. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_NUMBER);
  16328. /* not reachable */
  16329. }
  16330. return 0;
  16331. }
  16332. /*
  16333. * Stack index validation/normalization and getting a stack duk_tval ptr.
  16334. *
  16335. * These are called by many API entrypoints so the implementations must be
  16336. * fast and "inlined".
  16337. *
  16338. * There's some repetition because of this; keep the functions in sync.
  16339. */
  16340. DUK_EXTERNAL duk_idx_t duk_normalize_index(duk_context *ctx, duk_idx_t index) {
  16341. duk_hthread *thr = (duk_hthread *) ctx;
  16342. duk_uidx_t vs_size;
  16343. duk_uidx_t uindex;
  16344. DUK_ASSERT_CTX_VALID(ctx);
  16345. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  16346. /* Care must be taken to avoid pointer wrapping in the index
  16347. * validation. For instance, on a 32-bit platform with 8-byte
  16348. * duk_tval the index 0x20000000UL would wrap the memory space
  16349. * once.
  16350. */
  16351. /* Assume value stack sizes (in elements) fits into duk_idx_t. */
  16352. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  16353. vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
  16354. DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
  16355. if (index < 0) {
  16356. uindex = vs_size + (duk_uidx_t) index;
  16357. } else {
  16358. /* since index non-negative */
  16359. DUK_ASSERT(index != DUK_INVALID_INDEX);
  16360. uindex = (duk_uidx_t) index;
  16361. }
  16362. /* DUK_INVALID_INDEX won't be accepted as a valid index. */
  16363. DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
  16364. if (DUK_LIKELY(uindex < vs_size)) {
  16365. return (duk_idx_t) uindex;
  16366. }
  16367. return DUK_INVALID_INDEX;
  16368. }
  16369. DUK_EXTERNAL duk_idx_t duk_require_normalize_index(duk_context *ctx, duk_idx_t index) {
  16370. duk_hthread *thr = (duk_hthread *) ctx;
  16371. duk_uidx_t vs_size;
  16372. duk_uidx_t uindex;
  16373. DUK_ASSERT_CTX_VALID(ctx);
  16374. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  16375. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  16376. vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
  16377. DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
  16378. if (index < 0) {
  16379. uindex = vs_size + (duk_uidx_t) index;
  16380. } else {
  16381. DUK_ASSERT(index != DUK_INVALID_INDEX);
  16382. uindex = (duk_uidx_t) index;
  16383. }
  16384. /* DUK_INVALID_INDEX won't be accepted as a valid index. */
  16385. DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
  16386. if (DUK_LIKELY(uindex < vs_size)) {
  16387. return (duk_idx_t) uindex;
  16388. }
  16389. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_INDEX);
  16390. return 0; /* unreachable */
  16391. }
  16392. DUK_INTERNAL duk_tval *duk_get_tval(duk_context *ctx, duk_idx_t index) {
  16393. duk_hthread *thr = (duk_hthread *) ctx;
  16394. duk_uidx_t vs_size;
  16395. duk_uidx_t uindex;
  16396. DUK_ASSERT_CTX_VALID(ctx);
  16397. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  16398. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  16399. vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
  16400. DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
  16401. if (index < 0) {
  16402. uindex = vs_size + (duk_uidx_t) index;
  16403. } else {
  16404. DUK_ASSERT(index != DUK_INVALID_INDEX);
  16405. uindex = (duk_uidx_t) index;
  16406. }
  16407. /* DUK_INVALID_INDEX won't be accepted as a valid index. */
  16408. DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
  16409. if (DUK_LIKELY(uindex < vs_size)) {
  16410. return thr->valstack_bottom + uindex;
  16411. }
  16412. return NULL;
  16413. }
  16414. DUK_INTERNAL duk_tval *duk_require_tval(duk_context *ctx, duk_idx_t index) {
  16415. duk_hthread *thr = (duk_hthread *) ctx;
  16416. duk_uidx_t vs_size;
  16417. duk_uidx_t uindex;
  16418. DUK_ASSERT_CTX_VALID(ctx);
  16419. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  16420. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  16421. vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
  16422. DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
  16423. /* Use unsigned arithmetic to optimize comparison. */
  16424. if (index < 0) {
  16425. uindex = vs_size + (duk_uidx_t) index;
  16426. } else {
  16427. DUK_ASSERT(index != DUK_INVALID_INDEX);
  16428. uindex = (duk_uidx_t) index;
  16429. }
  16430. /* DUK_INVALID_INDEX won't be accepted as a valid index. */
  16431. DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
  16432. if (DUK_LIKELY(uindex < vs_size)) {
  16433. return thr->valstack_bottom + uindex;
  16434. }
  16435. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_INDEX);
  16436. return NULL;
  16437. }
  16438. /* Non-critical. */
  16439. DUK_EXTERNAL duk_bool_t duk_is_valid_index(duk_context *ctx, duk_idx_t index) {
  16440. DUK_ASSERT_CTX_VALID(ctx);
  16441. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  16442. return (duk_normalize_index(ctx, index) >= 0);
  16443. }
  16444. /* Non-critical. */
  16445. DUK_EXTERNAL void duk_require_valid_index(duk_context *ctx, duk_idx_t index) {
  16446. duk_hthread *thr = (duk_hthread *) ctx;
  16447. DUK_ASSERT_CTX_VALID(ctx);
  16448. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  16449. if (duk_normalize_index(ctx, index) < 0) {
  16450. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_INDEX);
  16451. }
  16452. }
  16453. /*
  16454. * Value stack top handling
  16455. */
  16456. DUK_EXTERNAL duk_idx_t duk_get_top(duk_context *ctx) {
  16457. duk_hthread *thr = (duk_hthread *) ctx;
  16458. DUK_ASSERT_CTX_VALID(ctx);
  16459. return (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  16460. }
  16461. /* Set stack top within currently allocated range, but don't reallocate.
  16462. * This is performance critical especially for call handling, so whenever
  16463. * changing, profile and look at generated code.
  16464. */
  16465. DUK_EXTERNAL void duk_set_top(duk_context *ctx, duk_idx_t index) {
  16466. duk_hthread *thr = (duk_hthread *) ctx;
  16467. duk_uidx_t vs_size;
  16468. duk_uidx_t vs_limit;
  16469. duk_uidx_t uindex;
  16470. duk_tval *tv;
  16471. DUK_ASSERT_CTX_VALID(ctx);
  16472. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  16473. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  16474. DUK_ASSERT(thr->valstack_end >= thr->valstack_bottom);
  16475. vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
  16476. vs_limit = (duk_uidx_t) (thr->valstack_end - thr->valstack_bottom);
  16477. if (index < 0) {
  16478. /* Negative indices are always within allocated stack but
  16479. * must not go below zero index.
  16480. */
  16481. uindex = vs_size + (duk_uidx_t) index;
  16482. } else {
  16483. /* Positive index can be higher than valstack top but must
  16484. * not go above allocated stack (equality is OK).
  16485. */
  16486. uindex = (duk_uidx_t) index;
  16487. }
  16488. /* DUK_INVALID_INDEX won't be accepted as a valid index. */
  16489. DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
  16490. DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_limit);
  16491. #if defined(DUK_USE_VALSTACK_UNSAFE)
  16492. DUK_ASSERT(uindex <= vs_limit);
  16493. #else
  16494. if (DUK_UNLIKELY(uindex > vs_limit)) {
  16495. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_INDEX);
  16496. return;
  16497. }
  16498. #endif
  16499. DUK_ASSERT(uindex <= vs_limit);
  16500. /* Handle change in value stack top. Respect value stack
  16501. * initialization policy: 'undefined' above top. Note that
  16502. * DECREF may cause a side effect that reallocates valstack,
  16503. * so must relookup after DECREF.
  16504. */
  16505. if (uindex >= vs_size) {
  16506. /* Stack size increases or stays the same. */
  16507. #if defined(DUK_USE_ASSERTIONS)
  16508. duk_uidx_t count;
  16509. count = uindex - vs_size;
  16510. while (count != 0) {
  16511. count--;
  16512. tv = thr->valstack_top + count;
  16513. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv));
  16514. }
  16515. #endif
  16516. thr->valstack_top = thr->valstack_bottom + uindex;
  16517. } else {
  16518. /* Stack size decreases. */
  16519. #if defined(DUK_USE_REFERENCE_COUNTING)
  16520. duk_uidx_t count;
  16521. count = vs_size - uindex;
  16522. DUK_ASSERT(count > 0);
  16523. while (count > 0) {
  16524. count--;
  16525. tv = --thr->valstack_top; /* tv -> value just before prev top value; must relookup */
  16526. DUK_ASSERT(tv >= thr->valstack_bottom);
  16527. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
  16528. }
  16529. #else /* DUK_USE_REFERENCE_COUNTING */
  16530. duk_uidx_t count;
  16531. duk_tval *tv_end;
  16532. count = vs_size - uindex;
  16533. tv = thr->valstack_top;
  16534. tv_end = tv - count;
  16535. DUK_ASSERT(tv > tv_end);
  16536. do {
  16537. tv--;
  16538. DUK_TVAL_SET_UNDEFINED(tv);
  16539. } while (tv != tv_end);
  16540. thr->valstack_top = tv_end;
  16541. #endif /* DUK_USE_REFERENCE_COUNTING */
  16542. }
  16543. }
  16544. DUK_EXTERNAL duk_idx_t duk_get_top_index(duk_context *ctx) {
  16545. duk_hthread *thr = (duk_hthread *) ctx;
  16546. duk_idx_t ret;
  16547. DUK_ASSERT_CTX_VALID(ctx);
  16548. ret = ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1;
  16549. if (DUK_UNLIKELY(ret < 0)) {
  16550. /* Return invalid index; if caller uses this without checking
  16551. * in another API call, the index won't map to a valid stack
  16552. * entry.
  16553. */
  16554. return DUK_INVALID_INDEX;
  16555. }
  16556. return ret;
  16557. }
  16558. DUK_EXTERNAL duk_idx_t duk_require_top_index(duk_context *ctx) {
  16559. duk_hthread *thr = (duk_hthread *) ctx;
  16560. duk_idx_t ret;
  16561. DUK_ASSERT_CTX_VALID(ctx);
  16562. ret = ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1;
  16563. if (DUK_UNLIKELY(ret < 0)) {
  16564. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_INDEX);
  16565. }
  16566. return ret;
  16567. }
  16568. /*
  16569. * Value stack resizing.
  16570. *
  16571. * This resizing happens above the current "top": the value stack can be
  16572. * grown or shrunk, but the "top" is not affected. The value stack cannot
  16573. * be resized to a size below the current "top".
  16574. *
  16575. * The low level reallocation primitive must carefully recompute all value
  16576. * stack pointers, and must also work if ALL pointers are NULL. The resize
  16577. * is quite tricky because the valstack realloc may cause a mark-and-sweep,
  16578. * which may run finalizers. Running finalizers may resize the valstack
  16579. * recursively (the same value stack we're working on). So, after realloc
  16580. * returns, we know that the valstack "top" should still be the same (there
  16581. * should not be live values above the "top"), but its underlying size and
  16582. * pointer may have changed.
  16583. */
  16584. /* XXX: perhaps refactor this to allow caller to specify some parameters, or
  16585. * at least a 'compact' flag which skips any spare or round-up .. useful for
  16586. * emergency gc.
  16587. */
  16588. DUK_LOCAL duk_bool_t duk__resize_valstack(duk_context *ctx, duk_size_t new_size) {
  16589. duk_hthread *thr = (duk_hthread *) ctx;
  16590. duk_ptrdiff_t old_bottom_offset;
  16591. duk_ptrdiff_t old_top_offset;
  16592. duk_ptrdiff_t old_end_offset_post;
  16593. #ifdef DUK_USE_DEBUG
  16594. duk_ptrdiff_t old_end_offset_pre;
  16595. duk_tval *old_valstack_pre;
  16596. duk_tval *old_valstack_post;
  16597. #endif
  16598. duk_tval *new_valstack;
  16599. duk_size_t new_alloc_size;
  16600. duk_tval *p;
  16601. DUK_ASSERT_CTX_VALID(ctx);
  16602. DUK_ASSERT(thr != NULL);
  16603. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  16604. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  16605. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  16606. DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack) <= new_size); /* can't resize below 'top' */
  16607. DUK_ASSERT(new_size <= thr->valstack_max); /* valstack limit caller has check, prevents wrapping */
  16608. DUK_ASSERT(new_size <= DUK_SIZE_MAX / sizeof(duk_tval)); /* specific assert for wrapping */
  16609. /* get pointer offsets for tweaking below */
  16610. old_bottom_offset = (((duk_uint8_t *) thr->valstack_bottom) - ((duk_uint8_t *) thr->valstack));
  16611. old_top_offset = (((duk_uint8_t *) thr->valstack_top) - ((duk_uint8_t *) thr->valstack));
  16612. #ifdef DUK_USE_DEBUG
  16613. old_end_offset_pre = (((duk_uint8_t *) thr->valstack_end) - ((duk_uint8_t *) thr->valstack)); /* not very useful, used for debugging */
  16614. old_valstack_pre = thr->valstack;
  16615. #endif
  16616. /* Allocate a new valstack.
  16617. *
  16618. * Note: cannot use a plain DUK_REALLOC() because a mark-and-sweep may
  16619. * invalidate the original thr->valstack base pointer inside the realloc
  16620. * process. See doc/memory-management.rst.
  16621. */
  16622. new_alloc_size = sizeof(duk_tval) * new_size;
  16623. new_valstack = (duk_tval *) DUK_REALLOC_INDIRECT(thr->heap, duk_hthread_get_valstack_ptr, (void *) thr, new_alloc_size);
  16624. if (!new_valstack) {
  16625. /* Because new_size != 0, if condition doesn't need to be
  16626. * (new_valstack != NULL || new_size == 0).
  16627. */
  16628. DUK_ASSERT(new_size != 0);
  16629. DUK_D(DUK_DPRINT("failed to resize valstack to %lu entries (%lu bytes)",
  16630. (unsigned long) new_size, (unsigned long) new_alloc_size));
  16631. return 0;
  16632. }
  16633. /* Note: the realloc may have triggered a mark-and-sweep which may
  16634. * have resized our valstack internally. However, the mark-and-sweep
  16635. * MUST NOT leave the stack bottom/top in a different state. Particular
  16636. * assumptions and facts:
  16637. *
  16638. * - The thr->valstack pointer may be different after realloc,
  16639. * and the offset between thr->valstack_end <-> thr->valstack
  16640. * may have changed.
  16641. * - The offset between thr->valstack_bottom <-> thr->valstack
  16642. * and thr->valstack_top <-> thr->valstack MUST NOT have changed,
  16643. * because mark-and-sweep must adhere to a strict stack policy.
  16644. * In other words, logical bottom and top MUST NOT have changed.
  16645. * - All values above the top are unreachable but are initialized
  16646. * to UNDEFINED, up to the post-realloc valstack_end.
  16647. * - 'old_end_offset' must be computed after realloc to be correct.
  16648. */
  16649. DUK_ASSERT((((duk_uint8_t *) thr->valstack_bottom) - ((duk_uint8_t *) thr->valstack)) == old_bottom_offset);
  16650. DUK_ASSERT((((duk_uint8_t *) thr->valstack_top) - ((duk_uint8_t *) thr->valstack)) == old_top_offset);
  16651. /* success, fixup pointers */
  16652. old_end_offset_post = (((duk_uint8_t *) thr->valstack_end) - ((duk_uint8_t *) thr->valstack)); /* must be computed after realloc */
  16653. #ifdef DUK_USE_DEBUG
  16654. old_valstack_post = thr->valstack;
  16655. #endif
  16656. thr->valstack = new_valstack;
  16657. thr->valstack_end = new_valstack + new_size;
  16658. #if !defined(DUK_USE_PREFER_SIZE)
  16659. thr->valstack_size = new_size;
  16660. #endif
  16661. thr->valstack_bottom = (duk_tval *) (void *) ((duk_uint8_t *) new_valstack + old_bottom_offset);
  16662. thr->valstack_top = (duk_tval *) (void *) ((duk_uint8_t *) new_valstack + old_top_offset);
  16663. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  16664. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  16665. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  16666. /* useful for debugging */
  16667. #ifdef DUK_USE_DEBUG
  16668. if (old_end_offset_pre != old_end_offset_post) {
  16669. DUK_D(DUK_DPRINT("valstack was resized during valstack_resize(), probably by mark-and-sweep; "
  16670. "end offset changed: %lu -> %lu",
  16671. (unsigned long) old_end_offset_pre,
  16672. (unsigned long) old_end_offset_post));
  16673. }
  16674. if (old_valstack_pre != old_valstack_post) {
  16675. DUK_D(DUK_DPRINT("valstack pointer changed during valstack_resize(), probably by mark-and-sweep: %p -> %p",
  16676. (void *) old_valstack_pre,
  16677. (void *) old_valstack_post));
  16678. }
  16679. #endif
  16680. DUK_DD(DUK_DDPRINT("resized valstack to %lu elements (%lu bytes), bottom=%ld, top=%ld, "
  16681. "new pointers: start=%p end=%p bottom=%p top=%p",
  16682. (unsigned long) new_size, (unsigned long) new_alloc_size,
  16683. (long) (thr->valstack_bottom - thr->valstack),
  16684. (long) (thr->valstack_top - thr->valstack),
  16685. (void *) thr->valstack, (void *) thr->valstack_end,
  16686. (void *) thr->valstack_bottom, (void *) thr->valstack_top));
  16687. /* Init newly allocated slots (only). */
  16688. p = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + old_end_offset_post);
  16689. while (p < thr->valstack_end) {
  16690. /* Never executed if new size is smaller. */
  16691. DUK_TVAL_SET_UNDEFINED(p);
  16692. p++;
  16693. }
  16694. /* Assert for value stack initialization policy. */
  16695. #if defined(DUK_USE_ASSERTIONS)
  16696. p = thr->valstack_top;
  16697. while (p < thr->valstack_end) {
  16698. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(p));
  16699. p++;
  16700. }
  16701. #endif
  16702. return 1;
  16703. }
  16704. DUK_INTERNAL
  16705. duk_bool_t duk_valstack_resize_raw(duk_context *ctx,
  16706. duk_size_t min_new_size,
  16707. duk_small_uint_t flags) {
  16708. duk_hthread *thr = (duk_hthread *) ctx;
  16709. duk_size_t old_size;
  16710. duk_size_t new_size;
  16711. duk_bool_t is_shrink = 0;
  16712. duk_small_uint_t shrink_flag = (flags & DUK_VSRESIZE_FLAG_SHRINK);
  16713. duk_small_uint_t compact_flag = (flags & DUK_VSRESIZE_FLAG_COMPACT);
  16714. duk_small_uint_t throw_flag = (flags & DUK_VSRESIZE_FLAG_THROW);
  16715. DUK_DDD(DUK_DDDPRINT("check valstack resize: min_new_size=%lu, curr_size=%ld, curr_top=%ld, "
  16716. "curr_bottom=%ld, shrink=%d, compact=%d, throw=%d",
  16717. (unsigned long) min_new_size,
  16718. (long) (thr->valstack_end - thr->valstack),
  16719. (long) (thr->valstack_top - thr->valstack),
  16720. (long) (thr->valstack_bottom - thr->valstack),
  16721. (int) shrink_flag, (int) compact_flag, (int) throw_flag));
  16722. DUK_ASSERT_CTX_VALID(ctx);
  16723. DUK_ASSERT(thr != NULL);
  16724. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  16725. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  16726. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  16727. #if defined(DUK_USE_PREFER_SIZE)
  16728. old_size = (duk_size_t) (thr->valstack_end - thr->valstack);
  16729. #else
  16730. DUK_ASSERT((duk_size_t) (thr->valstack_end - thr->valstack) == thr->valstack_size);
  16731. old_size = thr->valstack_size;
  16732. #endif
  16733. if (min_new_size <= old_size) {
  16734. is_shrink = 1;
  16735. if (!shrink_flag ||
  16736. old_size - min_new_size < DUK_VALSTACK_SHRINK_THRESHOLD) {
  16737. DUK_DDD(DUK_DDDPRINT("no need to grow or shrink valstack"));
  16738. return 1;
  16739. }
  16740. }
  16741. new_size = min_new_size;
  16742. if (!compact_flag) {
  16743. if (is_shrink) {
  16744. /* shrink case; leave some spare */
  16745. new_size += DUK_VALSTACK_SHRINK_SPARE;
  16746. }
  16747. /* round up roughly to next 'grow step' */
  16748. new_size = (new_size / DUK_VALSTACK_GROW_STEP + 1) * DUK_VALSTACK_GROW_STEP;
  16749. }
  16750. DUK_DD(DUK_DDPRINT("want to %s valstack: %lu -> %lu elements (min_new_size %lu)",
  16751. (const char *) (new_size > old_size ? "grow" : "shrink"),
  16752. (unsigned long) old_size, (unsigned long) new_size,
  16753. (unsigned long) min_new_size));
  16754. if (new_size > thr->valstack_max) {
  16755. /* Note: may be triggered even if minimal new_size would not reach the limit,
  16756. * plan limit accordingly (taking DUK_VALSTACK_GROW_STEP into account).
  16757. */
  16758. if (throw_flag) {
  16759. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_VALSTACK_LIMIT);
  16760. } else {
  16761. return 0;
  16762. }
  16763. }
  16764. /*
  16765. * When resizing the valstack, a mark-and-sweep may be triggered for
  16766. * the allocation of the new valstack. If the mark-and-sweep needs
  16767. * to use our thread for something, it may cause *the same valstack*
  16768. * to be resized recursively. This happens e.g. when mark-and-sweep
  16769. * finalizers are called. This is taken into account carefully in
  16770. * duk__resize_valstack().
  16771. *
  16772. * 'new_size' is known to be <= valstack_max, which ensures that
  16773. * size_t and pointer arithmetic won't wrap in duk__resize_valstack().
  16774. */
  16775. if (!duk__resize_valstack(ctx, new_size)) {
  16776. if (is_shrink) {
  16777. DUK_DD(DUK_DDPRINT("valstack resize failed, but is a shrink, ignore"));
  16778. return 1;
  16779. }
  16780. DUK_DD(DUK_DDPRINT("valstack resize failed"));
  16781. if (throw_flag) {
  16782. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_FAILED_TO_EXTEND_VALSTACK);
  16783. } else {
  16784. return 0;
  16785. }
  16786. }
  16787. DUK_DDD(DUK_DDDPRINT("valstack resize successful"));
  16788. return 1;
  16789. }
  16790. DUK_EXTERNAL duk_bool_t duk_check_stack(duk_context *ctx, duk_idx_t extra) {
  16791. duk_hthread *thr = (duk_hthread *) ctx;
  16792. duk_size_t min_new_size;
  16793. DUK_ASSERT_CTX_VALID(ctx);
  16794. DUK_ASSERT(thr != NULL);
  16795. if (DUK_UNLIKELY(extra < 0)) {
  16796. /* Clamping to zero makes the API more robust to calling code
  16797. * calculation errors.
  16798. */
  16799. extra = 0;
  16800. }
  16801. min_new_size = (thr->valstack_top - thr->valstack) + extra + DUK_VALSTACK_INTERNAL_EXTRA;
  16802. return duk_valstack_resize_raw(ctx,
  16803. min_new_size, /* min_new_size */
  16804. 0 /* no shrink */ | /* flags */
  16805. 0 /* no compact */ |
  16806. 0 /* no throw */);
  16807. }
  16808. DUK_EXTERNAL void duk_require_stack(duk_context *ctx, duk_idx_t extra) {
  16809. duk_hthread *thr = (duk_hthread *) ctx;
  16810. duk_size_t min_new_size;
  16811. DUK_ASSERT_CTX_VALID(ctx);
  16812. DUK_ASSERT(thr != NULL);
  16813. if (DUK_UNLIKELY(extra < 0)) {
  16814. /* Clamping to zero makes the API more robust to calling code
  16815. * calculation errors.
  16816. */
  16817. extra = 0;
  16818. }
  16819. min_new_size = (thr->valstack_top - thr->valstack) + extra + DUK_VALSTACK_INTERNAL_EXTRA;
  16820. (void) duk_valstack_resize_raw(ctx,
  16821. min_new_size, /* min_new_size */
  16822. 0 /* no shrink */ | /* flags */
  16823. 0 /* no compact */ |
  16824. DUK_VSRESIZE_FLAG_THROW);
  16825. }
  16826. DUK_EXTERNAL duk_bool_t duk_check_stack_top(duk_context *ctx, duk_idx_t top) {
  16827. duk_size_t min_new_size;
  16828. DUK_ASSERT_CTX_VALID(ctx);
  16829. if (DUK_UNLIKELY(top < 0)) {
  16830. /* Clamping to zero makes the API more robust to calling code
  16831. * calculation errors.
  16832. */
  16833. top = 0;
  16834. }
  16835. min_new_size = top + DUK_VALSTACK_INTERNAL_EXTRA;
  16836. return duk_valstack_resize_raw(ctx,
  16837. min_new_size, /* min_new_size */
  16838. 0 /* no shrink */ | /* flags */
  16839. 0 /* no compact */ |
  16840. 0 /* no throw */);
  16841. }
  16842. DUK_EXTERNAL void duk_require_stack_top(duk_context *ctx, duk_idx_t top) {
  16843. duk_size_t min_new_size;
  16844. DUK_ASSERT_CTX_VALID(ctx);
  16845. if (DUK_UNLIKELY(top < 0)) {
  16846. /* Clamping to zero makes the API more robust to calling code
  16847. * calculation errors.
  16848. */
  16849. top = 0;
  16850. }
  16851. min_new_size = top + DUK_VALSTACK_INTERNAL_EXTRA;
  16852. (void) duk_valstack_resize_raw(ctx,
  16853. min_new_size, /* min_new_size */
  16854. 0 /* no shrink */ | /* flags */
  16855. 0 /* no compact */ |
  16856. DUK_VSRESIZE_FLAG_THROW);
  16857. }
  16858. /*
  16859. * Basic stack manipulation: swap, dup, insert, replace, etc
  16860. */
  16861. DUK_EXTERNAL void duk_swap(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) {
  16862. duk_tval *tv1;
  16863. duk_tval *tv2;
  16864. duk_tval tv_tmp;
  16865. DUK_ASSERT_CTX_VALID(ctx);
  16866. tv1 = duk_require_tval(ctx, index1);
  16867. DUK_ASSERT(tv1 != NULL);
  16868. tv2 = duk_require_tval(ctx, index2);
  16869. DUK_ASSERT(tv2 != NULL);
  16870. /* If tv1==tv2 this is a NOP, no check is needed */
  16871. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  16872. DUK_TVAL_SET_TVAL(tv1, tv2);
  16873. DUK_TVAL_SET_TVAL(tv2, &tv_tmp);
  16874. }
  16875. DUK_EXTERNAL void duk_swap_top(duk_context *ctx, duk_idx_t index) {
  16876. DUK_ASSERT_CTX_VALID(ctx);
  16877. duk_swap(ctx, index, -1);
  16878. }
  16879. DUK_EXTERNAL void duk_dup(duk_context *ctx, duk_idx_t from_index) {
  16880. duk_hthread *thr;
  16881. duk_tval *tv_from;
  16882. duk_tval *tv_to;
  16883. DUK_ASSERT_CTX_VALID(ctx);
  16884. thr = (duk_hthread *) ctx;
  16885. DUK__CHECK_SPACE();
  16886. tv_from = duk_require_tval(ctx, from_index);
  16887. tv_to = thr->valstack_top++;
  16888. DUK_ASSERT(tv_from != NULL);
  16889. DUK_ASSERT(tv_to != NULL);
  16890. DUK_TVAL_SET_TVAL(tv_to, tv_from);
  16891. DUK_TVAL_INCREF(thr, tv_to); /* no side effects */
  16892. }
  16893. DUK_EXTERNAL void duk_dup_top(duk_context *ctx) {
  16894. duk_hthread *thr;
  16895. duk_tval *tv_from;
  16896. duk_tval *tv_to;
  16897. DUK_ASSERT_CTX_VALID(ctx);
  16898. thr = (duk_hthread *) ctx;
  16899. DUK__CHECK_SPACE();
  16900. if (thr->valstack_top - thr->valstack_bottom <= 0) {
  16901. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_INDEX);
  16902. }
  16903. tv_from = thr->valstack_top - 1;
  16904. tv_to = thr->valstack_top++;
  16905. DUK_ASSERT(tv_from != NULL);
  16906. DUK_ASSERT(tv_to != NULL);
  16907. DUK_TVAL_SET_TVAL(tv_to, tv_from);
  16908. DUK_TVAL_INCREF(thr, tv_to); /* no side effects */
  16909. }
  16910. DUK_EXTERNAL void duk_insert(duk_context *ctx, duk_idx_t to_index) {
  16911. duk_tval *p;
  16912. duk_tval *q;
  16913. duk_tval tv_tmp;
  16914. duk_size_t nbytes;
  16915. DUK_ASSERT_CTX_VALID(ctx);
  16916. p = duk_require_tval(ctx, to_index);
  16917. DUK_ASSERT(p != NULL);
  16918. q = duk_require_tval(ctx, -1);
  16919. DUK_ASSERT(q != NULL);
  16920. DUK_ASSERT(q >= p);
  16921. /* nbytes
  16922. * <--------->
  16923. * [ ... | p | x | x | q ]
  16924. * => [ ... | q | p | x | x ]
  16925. */
  16926. nbytes = (duk_size_t) (((duk_uint8_t *) q) - ((duk_uint8_t *) p)); /* Note: 'q' is top-1 */
  16927. DUK_DDD(DUK_DDDPRINT("duk_insert: to_index=%ld, p=%p, q=%p, nbytes=%lu",
  16928. (long) to_index, (void *) p, (void *) q, (unsigned long) nbytes));
  16929. /* No net refcount changes. */
  16930. if (nbytes > 0) {
  16931. DUK_TVAL_SET_TVAL(&tv_tmp, q);
  16932. DUK_ASSERT(nbytes > 0);
  16933. DUK_MEMMOVE((void *) (p + 1), (const void *) p, (size_t) nbytes);
  16934. DUK_TVAL_SET_TVAL(p, &tv_tmp);
  16935. } else {
  16936. /* nop: insert top to top */
  16937. DUK_ASSERT(nbytes == 0);
  16938. DUK_ASSERT(p == q);
  16939. }
  16940. }
  16941. DUK_EXTERNAL void duk_replace(duk_context *ctx, duk_idx_t to_index) {
  16942. duk_hthread *thr = (duk_hthread *) ctx;
  16943. duk_tval *tv1;
  16944. duk_tval *tv2;
  16945. duk_tval tv_tmp;
  16946. DUK_ASSERT_CTX_VALID(ctx);
  16947. tv1 = duk_require_tval(ctx, -1);
  16948. DUK_ASSERT(tv1 != NULL);
  16949. tv2 = duk_require_tval(ctx, to_index);
  16950. DUK_ASSERT(tv2 != NULL);
  16951. /* For tv1 == tv2, both pointing to stack top, the end result
  16952. * is same as duk_pop(ctx).
  16953. */
  16954. DUK_TVAL_SET_TVAL(&tv_tmp, tv2);
  16955. DUK_TVAL_SET_TVAL(tv2, tv1);
  16956. DUK_TVAL_SET_UNDEFINED(tv1);
  16957. thr->valstack_top--;
  16958. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  16959. }
  16960. DUK_EXTERNAL void duk_copy(duk_context *ctx, duk_idx_t from_index, duk_idx_t to_index) {
  16961. duk_hthread *thr = (duk_hthread *) ctx;
  16962. duk_tval *tv1;
  16963. duk_tval *tv2;
  16964. DUK_ASSERT_CTX_VALID(ctx);
  16965. DUK_UNREF(thr); /* w/o refcounting */
  16966. tv1 = duk_require_tval(ctx, from_index);
  16967. DUK_ASSERT(tv1 != NULL);
  16968. tv2 = duk_require_tval(ctx, to_index);
  16969. DUK_ASSERT(tv2 != NULL);
  16970. /* For tv1 == tv2, this is a no-op (no explicit check needed). */
  16971. DUK_TVAL_SET_TVAL_UPDREF(thr, tv2, tv1); /* side effects */
  16972. }
  16973. DUK_EXTERNAL void duk_remove(duk_context *ctx, duk_idx_t index) {
  16974. duk_hthread *thr = (duk_hthread *) ctx;
  16975. duk_tval *p;
  16976. duk_tval *q;
  16977. #ifdef DUK_USE_REFERENCE_COUNTING
  16978. duk_tval tv_tmp;
  16979. #endif
  16980. duk_size_t nbytes;
  16981. DUK_ASSERT_CTX_VALID(ctx);
  16982. p = duk_require_tval(ctx, index);
  16983. DUK_ASSERT(p != NULL);
  16984. q = duk_require_tval(ctx, -1);
  16985. DUK_ASSERT(q != NULL);
  16986. DUK_ASSERT(q >= p);
  16987. /* nbytes zero size case
  16988. * <--------->
  16989. * [ ... | p | x | x | q ] [ ... | p==q ]
  16990. * => [ ... | x | x | q ] [ ... ]
  16991. */
  16992. #ifdef DUK_USE_REFERENCE_COUNTING
  16993. /* use a temp: decref only when valstack reachable values are correct */
  16994. DUK_TVAL_SET_TVAL(&tv_tmp, p);
  16995. #endif
  16996. nbytes = (duk_size_t) (((duk_uint8_t *) q) - ((duk_uint8_t *) p)); /* Note: 'q' is top-1 */
  16997. DUK_MEMMOVE((void *) p, (const void *) (p + 1), (size_t) nbytes); /* zero size not an issue: pointers are valid */
  16998. DUK_TVAL_SET_UNDEFINED(q);
  16999. thr->valstack_top--;
  17000. #ifdef DUK_USE_REFERENCE_COUNTING
  17001. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  17002. #endif
  17003. }
  17004. /*
  17005. * Stack slice primitives
  17006. */
  17007. DUK_EXTERNAL void duk_xcopymove_raw(duk_context *to_ctx, duk_context *from_ctx, duk_idx_t count, duk_bool_t is_copy) {
  17008. duk_hthread *to_thr = (duk_hthread *) to_ctx;
  17009. duk_hthread *from_thr = (duk_hthread *) from_ctx;
  17010. void *src;
  17011. duk_size_t nbytes;
  17012. duk_tval *p;
  17013. duk_tval *q;
  17014. /* XXX: several pointer comparison issues here */
  17015. DUK_ASSERT_CTX_VALID(to_ctx);
  17016. DUK_ASSERT_CTX_VALID(from_ctx);
  17017. DUK_ASSERT(to_ctx != NULL);
  17018. DUK_ASSERT(from_ctx != NULL);
  17019. if (to_ctx == from_ctx) {
  17020. DUK_ERROR(to_thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CONTEXT);
  17021. return;
  17022. }
  17023. if ((count < 0) ||
  17024. (count > (duk_idx_t) to_thr->valstack_max)) {
  17025. /* Maximum value check ensures 'nbytes' won't wrap below. */
  17026. DUK_ERROR(to_thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_COUNT);
  17027. return;
  17028. }
  17029. nbytes = sizeof(duk_tval) * count;
  17030. if (nbytes == 0) {
  17031. return;
  17032. }
  17033. DUK_ASSERT(to_thr->valstack_top <= to_thr->valstack_end);
  17034. if ((duk_size_t) ((duk_uint8_t *) to_thr->valstack_end - (duk_uint8_t *) to_thr->valstack_top) < nbytes) {
  17035. DUK_ERROR(to_thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  17036. }
  17037. src = (void *) ((duk_uint8_t *) from_thr->valstack_top - nbytes);
  17038. if (src < (void *) from_thr->valstack_bottom) {
  17039. DUK_ERROR(to_thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_COUNT);
  17040. }
  17041. /* copy values (no overlap even if to_ctx == from_ctx; that's not
  17042. * allowed now anyway)
  17043. */
  17044. DUK_ASSERT(nbytes > 0);
  17045. DUK_MEMCPY((void *) to_thr->valstack_top, (const void *) src, (size_t) nbytes);
  17046. p = to_thr->valstack_top;
  17047. to_thr->valstack_top = (duk_tval *) (void *) (((duk_uint8_t *) p) + nbytes);
  17048. if (is_copy) {
  17049. /* Incref copies, keep originals. */
  17050. q = to_thr->valstack_top;
  17051. while (p < q) {
  17052. DUK_TVAL_INCREF(to_thr, p); /* no side effects */
  17053. p++;
  17054. }
  17055. } else {
  17056. /* No net refcount change. */
  17057. p = from_thr->valstack_top;
  17058. q = (duk_tval *) (void *) (((duk_uint8_t *) p) - nbytes);
  17059. from_thr->valstack_top = q;
  17060. while (p > q) {
  17061. p--;
  17062. DUK_TVAL_SET_UNDEFINED(p);
  17063. /* XXX: fast primitive to set a bunch of values to UNDEFINED */
  17064. }
  17065. }
  17066. }
  17067. /*
  17068. * Get/require
  17069. */
  17070. DUK_EXTERNAL void duk_require_undefined(duk_context *ctx, duk_idx_t index) {
  17071. duk_hthread *thr = (duk_hthread *) ctx;
  17072. duk_tval *tv;
  17073. DUK_ASSERT_CTX_VALID(ctx);
  17074. tv = duk_get_tval(ctx, index);
  17075. if (tv && DUK_TVAL_IS_UNDEFINED(tv)) {
  17076. /* Note: accept both 'actual' and 'unused' undefined */
  17077. return;
  17078. }
  17079. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_UNDEFINED);
  17080. }
  17081. DUK_EXTERNAL void duk_require_null(duk_context *ctx, duk_idx_t index) {
  17082. duk_hthread *thr = (duk_hthread *) ctx;
  17083. duk_tval *tv;
  17084. DUK_ASSERT_CTX_VALID(ctx);
  17085. tv = duk_get_tval(ctx, index);
  17086. if (tv && DUK_TVAL_IS_NULL(tv)) {
  17087. return;
  17088. }
  17089. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_NULL);
  17090. return; /* not reachable */
  17091. }
  17092. DUK_EXTERNAL duk_bool_t duk_get_boolean(duk_context *ctx, duk_idx_t index) {
  17093. duk_bool_t ret = 0; /* default: false */
  17094. duk_tval *tv;
  17095. DUK_ASSERT_CTX_VALID(ctx);
  17096. tv = duk_get_tval(ctx, index);
  17097. if (tv && DUK_TVAL_IS_BOOLEAN(tv)) {
  17098. ret = DUK_TVAL_GET_BOOLEAN(tv);
  17099. }
  17100. DUK_ASSERT(ret == 0 || ret == 1);
  17101. return ret;
  17102. }
  17103. DUK_EXTERNAL duk_bool_t duk_require_boolean(duk_context *ctx, duk_idx_t index) {
  17104. duk_hthread *thr = (duk_hthread *) ctx;
  17105. duk_tval *tv;
  17106. DUK_ASSERT_CTX_VALID(ctx);
  17107. tv = duk_get_tval(ctx, index);
  17108. if (tv && DUK_TVAL_IS_BOOLEAN(tv)) {
  17109. duk_bool_t ret = DUK_TVAL_GET_BOOLEAN(tv);
  17110. DUK_ASSERT(ret == 0 || ret == 1);
  17111. return ret;
  17112. }
  17113. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_BOOLEAN);
  17114. return 0; /* not reachable */
  17115. }
  17116. DUK_EXTERNAL duk_double_t duk_get_number(duk_context *ctx, duk_idx_t index) {
  17117. duk_double_union ret;
  17118. duk_tval *tv;
  17119. DUK_ASSERT_CTX_VALID(ctx);
  17120. ret.d = DUK_DOUBLE_NAN; /* default: NaN */
  17121. tv = duk_get_tval(ctx, index);
  17122. if (tv && DUK_TVAL_IS_NUMBER(tv)) {
  17123. ret.d = DUK_TVAL_GET_NUMBER(tv);
  17124. }
  17125. /*
  17126. * Number should already be in NaN-normalized form, but let's
  17127. * normalize anyway.
  17128. */
  17129. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&ret);
  17130. return ret.d;
  17131. }
  17132. DUK_EXTERNAL duk_double_t duk_require_number(duk_context *ctx, duk_idx_t index) {
  17133. duk_hthread *thr = (duk_hthread *) ctx;
  17134. duk_tval *tv;
  17135. DUK_ASSERT_CTX_VALID(ctx);
  17136. tv = duk_get_tval(ctx, index);
  17137. if (tv && DUK_TVAL_IS_NUMBER(tv)) {
  17138. duk_double_union ret;
  17139. ret.d = DUK_TVAL_GET_NUMBER(tv);
  17140. /*
  17141. * Number should already be in NaN-normalized form,
  17142. * but let's normalize anyway.
  17143. */
  17144. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&ret);
  17145. return ret.d;
  17146. }
  17147. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_NUMBER);
  17148. return DUK_DOUBLE_NAN; /* not reachable */
  17149. }
  17150. DUK_EXTERNAL duk_int_t duk_get_int(duk_context *ctx, duk_idx_t index) {
  17151. /* Custom coercion for API */
  17152. DUK_ASSERT_CTX_VALID(ctx);
  17153. return (duk_int_t) duk__api_coerce_d2i(ctx, index, 0 /*require*/);
  17154. }
  17155. DUK_EXTERNAL duk_uint_t duk_get_uint(duk_context *ctx, duk_idx_t index) {
  17156. /* Custom coercion for API */
  17157. DUK_ASSERT_CTX_VALID(ctx);
  17158. return (duk_uint_t) duk__api_coerce_d2ui(ctx, index, 0 /*require*/);
  17159. }
  17160. DUK_EXTERNAL duk_int_t duk_require_int(duk_context *ctx, duk_idx_t index) {
  17161. /* Custom coercion for API */
  17162. DUK_ASSERT_CTX_VALID(ctx);
  17163. return (duk_int_t) duk__api_coerce_d2i(ctx, index, 1 /*require*/);
  17164. }
  17165. DUK_EXTERNAL duk_uint_t duk_require_uint(duk_context *ctx, duk_idx_t index) {
  17166. /* Custom coercion for API */
  17167. DUK_ASSERT_CTX_VALID(ctx);
  17168. return (duk_uint_t) duk__api_coerce_d2ui(ctx, index, 1 /*require*/);
  17169. }
  17170. DUK_EXTERNAL const char *duk_get_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) {
  17171. const char *ret;
  17172. duk_tval *tv;
  17173. DUK_ASSERT_CTX_VALID(ctx);
  17174. /* default: NULL, length 0 */
  17175. ret = NULL;
  17176. if (out_len) {
  17177. *out_len = 0;
  17178. }
  17179. tv = duk_get_tval(ctx, index);
  17180. if (tv && DUK_TVAL_IS_STRING(tv)) {
  17181. /* Here we rely on duk_hstring instances always being zero
  17182. * terminated even if the actual string is not.
  17183. */
  17184. duk_hstring *h = DUK_TVAL_GET_STRING(tv);
  17185. DUK_ASSERT(h != NULL);
  17186. ret = (const char *) DUK_HSTRING_GET_DATA(h);
  17187. if (out_len) {
  17188. *out_len = DUK_HSTRING_GET_BYTELEN(h);
  17189. }
  17190. }
  17191. return ret;
  17192. }
  17193. DUK_EXTERNAL const char *duk_require_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) {
  17194. duk_hthread *thr = (duk_hthread *) ctx;
  17195. const char *ret;
  17196. DUK_ASSERT_CTX_VALID(ctx);
  17197. /* Note: this check relies on the fact that even a zero-size string
  17198. * has a non-NULL pointer.
  17199. */
  17200. ret = duk_get_lstring(ctx, index, out_len);
  17201. if (ret) {
  17202. return ret;
  17203. }
  17204. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_STRING);
  17205. return NULL; /* not reachable */
  17206. }
  17207. DUK_EXTERNAL const char *duk_get_string(duk_context *ctx, duk_idx_t index) {
  17208. DUK_ASSERT_CTX_VALID(ctx);
  17209. return duk_get_lstring(ctx, index, NULL);
  17210. }
  17211. DUK_EXTERNAL const char *duk_require_string(duk_context *ctx, duk_idx_t index) {
  17212. DUK_ASSERT_CTX_VALID(ctx);
  17213. return duk_require_lstring(ctx, index, NULL);
  17214. }
  17215. DUK_EXTERNAL void *duk_get_pointer(duk_context *ctx, duk_idx_t index) {
  17216. duk_tval *tv;
  17217. DUK_ASSERT_CTX_VALID(ctx);
  17218. tv = duk_get_tval(ctx, index);
  17219. if (tv && DUK_TVAL_IS_POINTER(tv)) {
  17220. void *p = DUK_TVAL_GET_POINTER(tv); /* may be NULL */
  17221. return (void *) p;
  17222. }
  17223. return NULL;
  17224. }
  17225. DUK_EXTERNAL void *duk_require_pointer(duk_context *ctx, duk_idx_t index) {
  17226. duk_hthread *thr = (duk_hthread *) ctx;
  17227. duk_tval *tv;
  17228. DUK_ASSERT_CTX_VALID(ctx);
  17229. /* Note: here we must be wary of the fact that a pointer may be
  17230. * valid and be a NULL.
  17231. */
  17232. tv = duk_get_tval(ctx, index);
  17233. if (tv && DUK_TVAL_IS_POINTER(tv)) {
  17234. void *p = DUK_TVAL_GET_POINTER(tv); /* may be NULL */
  17235. return (void *) p;
  17236. }
  17237. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_POINTER);
  17238. return NULL; /* not reachable */
  17239. }
  17240. #if 0 /*unused*/
  17241. DUK_INTERNAL void *duk_get_voidptr(duk_context *ctx, duk_idx_t index) {
  17242. duk_tval *tv;
  17243. DUK_ASSERT_CTX_VALID(ctx);
  17244. tv = duk_get_tval(ctx, index);
  17245. if (tv && DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  17246. duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
  17247. DUK_ASSERT(h != NULL);
  17248. return (void *) h;
  17249. }
  17250. return NULL;
  17251. }
  17252. #endif
  17253. DUK_LOCAL void *duk__get_buffer_helper(duk_context *ctx, duk_idx_t index, duk_size_t *out_size, duk_bool_t throw_flag) {
  17254. duk_hthread *thr = (duk_hthread *) ctx;
  17255. duk_tval *tv;
  17256. DUK_ASSERT_CTX_VALID(ctx);
  17257. DUK_UNREF(thr);
  17258. if (out_size != NULL) {
  17259. *out_size = 0;
  17260. }
  17261. tv = duk_get_tval(ctx, index);
  17262. if (tv && DUK_TVAL_IS_BUFFER(tv)) {
  17263. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  17264. DUK_ASSERT(h != NULL);
  17265. if (out_size) {
  17266. *out_size = DUK_HBUFFER_GET_SIZE(h);
  17267. }
  17268. return (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h); /* may be NULL (but only if size is 0) */
  17269. }
  17270. if (throw_flag) {
  17271. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_BUFFER);
  17272. }
  17273. return NULL;
  17274. }
  17275. DUK_EXTERNAL void *duk_get_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) {
  17276. return duk__get_buffer_helper(ctx, index, out_size, 0 /*throw_flag*/);
  17277. }
  17278. DUK_EXTERNAL void *duk_require_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) {
  17279. return duk__get_buffer_helper(ctx, index, out_size, 1 /*throw_flag*/);
  17280. }
  17281. DUK_LOCAL void *duk__get_buffer_data_helper(duk_context *ctx, duk_idx_t index, duk_size_t *out_size, duk_bool_t throw_flag) {
  17282. duk_hthread *thr = (duk_hthread *) ctx;
  17283. duk_tval *tv;
  17284. DUK_ASSERT_CTX_VALID(ctx);
  17285. DUK_UNREF(thr);
  17286. if (out_size != NULL) {
  17287. *out_size = 0;
  17288. }
  17289. tv = duk_get_tval(ctx, index);
  17290. if (tv == NULL) {
  17291. goto fail;
  17292. }
  17293. if (DUK_TVAL_IS_BUFFER(tv)) {
  17294. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  17295. DUK_ASSERT(h != NULL);
  17296. if (out_size) {
  17297. *out_size = DUK_HBUFFER_GET_SIZE(h);
  17298. }
  17299. return (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h); /* may be NULL (but only if size is 0) */
  17300. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  17301. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  17302. DUK_ASSERT(h != NULL);
  17303. if (DUK_HOBJECT_IS_BUFFEROBJECT(h)) {
  17304. /* XXX: this is probably a useful shared helper: for a
  17305. * duk_hbufferobject, get a validated buffer pointer/length.
  17306. */
  17307. duk_hbufferobject *h_bufobj = (duk_hbufferobject *) h;
  17308. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  17309. if (h_bufobj->buf != NULL &&
  17310. DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj)) {
  17311. duk_uint8_t *p;
  17312. p = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf);
  17313. if (out_size != NULL) {
  17314. *out_size = (duk_size_t) h_bufobj->length;
  17315. }
  17316. return (void *) (p + h_bufobj->offset);
  17317. }
  17318. /* if slice not fully valid, treat as error */
  17319. }
  17320. }
  17321. fail:
  17322. if (throw_flag) {
  17323. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_BUFFER);
  17324. }
  17325. return NULL;
  17326. }
  17327. DUK_EXTERNAL void *duk_get_buffer_data(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) {
  17328. return duk__get_buffer_data_helper(ctx, index, out_size, 0 /*throw_flag*/);
  17329. }
  17330. DUK_EXTERNAL void *duk_require_buffer_data(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) {
  17331. return duk__get_buffer_data_helper(ctx, index, out_size, 1 /*throw_flag*/);
  17332. }
  17333. /* Raw helper for getting a value from the stack, checking its tag, and possible its object class.
  17334. * The tag cannot be a number because numbers don't have an internal tag in the packed representation.
  17335. */
  17336. DUK_INTERNAL duk_heaphdr *duk_get_tagged_heaphdr_raw(duk_context *ctx, duk_idx_t index, duk_uint_t flags_and_tag) {
  17337. duk_hthread *thr = (duk_hthread *) ctx;
  17338. duk_tval *tv;
  17339. duk_small_uint_t tag = flags_and_tag & 0xffffU; /* tags can be up to 16 bits */
  17340. DUK_ASSERT_CTX_VALID(ctx);
  17341. tv = duk_get_tval(ctx, index);
  17342. if (tv && (DUK_TVAL_GET_TAG(tv) == tag)) {
  17343. duk_heaphdr *ret;
  17344. /* Note: tag comparison in general doesn't work for numbers,
  17345. * but it does work for everything else (heap objects here).
  17346. */
  17347. ret = DUK_TVAL_GET_HEAPHDR(tv);
  17348. DUK_ASSERT(ret != NULL); /* tagged null pointers should never occur */
  17349. /* If class check has been requested, tag must also be DUK_TAG_OBJECT.
  17350. * This allows us to just check the class check flag without checking
  17351. * the tag also.
  17352. */
  17353. DUK_ASSERT((flags_and_tag & DUK_GETTAGGED_FLAG_CHECK_CLASS) == 0 ||
  17354. tag == DUK_TAG_OBJECT);
  17355. if ((flags_and_tag & DUK_GETTAGGED_FLAG_CHECK_CLASS) == 0 || /* no class check */
  17356. (duk_int_t) DUK_HOBJECT_GET_CLASS_NUMBER((duk_hobject *) ret) == /* or class check matches */
  17357. (duk_int_t) ((flags_and_tag >> DUK_GETTAGGED_CLASS_SHIFT) & 0xff)) {
  17358. return ret;
  17359. }
  17360. }
  17361. if (flags_and_tag & DUK_GETTAGGED_FLAG_ALLOW_NULL) {
  17362. return (duk_heaphdr *) NULL;
  17363. }
  17364. /* Formatting the tag number here is not very useful: the tag value
  17365. * is Duktape internal (not the same as DUK_TYPE_xxx) and even depends
  17366. * on the duk_tval layout. If anything, add a human readable type here.
  17367. */
  17368. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_UNEXPECTED_TYPE);
  17369. return NULL; /* not reachable */
  17370. }
  17371. DUK_INTERNAL duk_hstring *duk_get_hstring(duk_context *ctx, duk_idx_t index) {
  17372. return (duk_hstring *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_STRING | DUK_GETTAGGED_FLAG_ALLOW_NULL);
  17373. }
  17374. DUK_INTERNAL duk_hstring *duk_require_hstring(duk_context *ctx, duk_idx_t index) {
  17375. return (duk_hstring *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_STRING);
  17376. }
  17377. DUK_INTERNAL duk_hobject *duk_get_hobject(duk_context *ctx, duk_idx_t index) {
  17378. return (duk_hobject *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT | DUK_GETTAGGED_FLAG_ALLOW_NULL);
  17379. }
  17380. DUK_INTERNAL duk_hobject *duk_require_hobject(duk_context *ctx, duk_idx_t index) {
  17381. return (duk_hobject *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT);
  17382. }
  17383. DUK_INTERNAL duk_hbuffer *duk_get_hbuffer(duk_context *ctx, duk_idx_t index) {
  17384. return (duk_hbuffer *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_BUFFER | DUK_GETTAGGED_FLAG_ALLOW_NULL);
  17385. }
  17386. DUK_INTERNAL duk_hbuffer *duk_require_hbuffer(duk_context *ctx, duk_idx_t index) {
  17387. return (duk_hbuffer *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_BUFFER);
  17388. }
  17389. DUK_INTERNAL duk_hthread *duk_get_hthread(duk_context *ctx, duk_idx_t index) {
  17390. duk_hobject *h = (duk_hobject *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT | DUK_GETTAGGED_FLAG_ALLOW_NULL);
  17391. if (h != NULL && !DUK_HOBJECT_IS_THREAD(h)) {
  17392. h = NULL;
  17393. }
  17394. return (duk_hthread *) h;
  17395. }
  17396. DUK_INTERNAL duk_hthread *duk_require_hthread(duk_context *ctx, duk_idx_t index) {
  17397. duk_hthread *thr = (duk_hthread *) ctx;
  17398. duk_hobject *h = (duk_hobject *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT);
  17399. DUK_ASSERT(h != NULL);
  17400. if (!DUK_HOBJECT_IS_THREAD(h)) {
  17401. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_THREAD);
  17402. }
  17403. return (duk_hthread *) h;
  17404. }
  17405. DUK_INTERNAL duk_hcompiledfunction *duk_get_hcompiledfunction(duk_context *ctx, duk_idx_t index) {
  17406. duk_hobject *h = (duk_hobject *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT | DUK_GETTAGGED_FLAG_ALLOW_NULL);
  17407. if (h != NULL && !DUK_HOBJECT_IS_COMPILEDFUNCTION(h)) {
  17408. h = NULL;
  17409. }
  17410. return (duk_hcompiledfunction *) h;
  17411. }
  17412. DUK_INTERNAL duk_hcompiledfunction *duk_require_hcompiledfunction(duk_context *ctx, duk_idx_t index) {
  17413. duk_hthread *thr = (duk_hthread *) ctx;
  17414. duk_hobject *h = (duk_hobject *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT);
  17415. DUK_ASSERT(h != NULL);
  17416. if (!DUK_HOBJECT_IS_COMPILEDFUNCTION(h)) {
  17417. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_COMPILEDFUNCTION);
  17418. }
  17419. return (duk_hcompiledfunction *) h;
  17420. }
  17421. DUK_INTERNAL duk_hnativefunction *duk_get_hnativefunction(duk_context *ctx, duk_idx_t index) {
  17422. duk_hobject *h = (duk_hobject *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT | DUK_GETTAGGED_FLAG_ALLOW_NULL);
  17423. if (h != NULL && !DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
  17424. h = NULL;
  17425. }
  17426. return (duk_hnativefunction *) h;
  17427. }
  17428. DUK_INTERNAL duk_hnativefunction *duk_require_hnativefunction(duk_context *ctx, duk_idx_t index) {
  17429. duk_hthread *thr = (duk_hthread *) ctx;
  17430. duk_hobject *h = (duk_hobject *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT);
  17431. DUK_ASSERT(h != NULL);
  17432. if (!DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
  17433. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_NATIVEFUNCTION);
  17434. }
  17435. return (duk_hnativefunction *) h;
  17436. }
  17437. DUK_EXTERNAL duk_c_function duk_get_c_function(duk_context *ctx, duk_idx_t index) {
  17438. duk_tval *tv;
  17439. duk_hobject *h;
  17440. duk_hnativefunction *f;
  17441. DUK_ASSERT_CTX_VALID(ctx);
  17442. tv = duk_get_tval(ctx, index);
  17443. if (!tv) {
  17444. return NULL;
  17445. }
  17446. if (!DUK_TVAL_IS_OBJECT(tv)) {
  17447. return NULL;
  17448. }
  17449. h = DUK_TVAL_GET_OBJECT(tv);
  17450. DUK_ASSERT(h != NULL);
  17451. if (!DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
  17452. return NULL;
  17453. }
  17454. DUK_ASSERT(DUK_HOBJECT_HAS_NATIVEFUNCTION(h));
  17455. f = (duk_hnativefunction *) h;
  17456. return f->func;
  17457. }
  17458. DUK_EXTERNAL duk_c_function duk_require_c_function(duk_context *ctx, duk_idx_t index) {
  17459. duk_hthread *thr = (duk_hthread *) ctx;
  17460. duk_c_function ret;
  17461. DUK_ASSERT_CTX_VALID(ctx);
  17462. ret = duk_get_c_function(ctx, index);
  17463. if (!ret) {
  17464. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_C_FUNCTION);
  17465. }
  17466. return ret;
  17467. }
  17468. DUK_EXTERNAL duk_context *duk_get_context(duk_context *ctx, duk_idx_t index) {
  17469. DUK_ASSERT_CTX_VALID(ctx);
  17470. return (duk_context *) duk_get_hthread(ctx, index);
  17471. }
  17472. DUK_EXTERNAL duk_context *duk_require_context(duk_context *ctx, duk_idx_t index) {
  17473. DUK_ASSERT_CTX_VALID(ctx);
  17474. return (duk_context *) duk_require_hthread(ctx, index);
  17475. }
  17476. DUK_EXTERNAL void *duk_get_heapptr(duk_context *ctx, duk_idx_t index) {
  17477. duk_tval *tv;
  17478. void *ret;
  17479. DUK_ASSERT_CTX_VALID(ctx);
  17480. tv = duk_get_tval(ctx, index);
  17481. if (tv && DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  17482. ret = (void *) DUK_TVAL_GET_HEAPHDR(tv);
  17483. DUK_ASSERT(ret != NULL);
  17484. return ret;
  17485. }
  17486. return (void *) NULL;
  17487. }
  17488. DUK_EXTERNAL void *duk_require_heapptr(duk_context *ctx, duk_idx_t index) {
  17489. duk_hthread *thr = (duk_hthread *) ctx;
  17490. duk_tval *tv;
  17491. void *ret;
  17492. DUK_ASSERT_CTX_VALID(ctx);
  17493. tv = duk_require_tval(ctx, index);
  17494. DUK_ASSERT(tv != NULL);
  17495. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  17496. ret = (void *) DUK_TVAL_GET_HEAPHDR(tv);
  17497. DUK_ASSERT(ret != NULL);
  17498. return ret;
  17499. }
  17500. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_UNEXPECTED_TYPE);
  17501. return (void *) NULL; /* not reachable */
  17502. }
  17503. #if 0
  17504. /* This would be pointless: we'd return NULL for both lightfuncs and
  17505. * unexpected types.
  17506. */
  17507. duk_hobject *duk_get_hobject_or_lfunc(duk_context *ctx, duk_idx_t index) {
  17508. }
  17509. #endif
  17510. /* Useful for internal call sites where we either expect an object (function)
  17511. * or a lightfunc. Accepts an object (returned as is) or a lightfunc (coerced
  17512. * to an object). Return value is NULL if value is neither an object nor a
  17513. * lightfunc.
  17514. */
  17515. duk_hobject *duk_get_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index) {
  17516. duk_tval *tv;
  17517. DUK_ASSERT_CTX_VALID(ctx);
  17518. tv = duk_require_tval(ctx, index);
  17519. DUK_ASSERT(tv != NULL);
  17520. if (DUK_TVAL_IS_OBJECT(tv)) {
  17521. return DUK_TVAL_GET_OBJECT(tv);
  17522. } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  17523. duk_to_object(ctx, index);
  17524. return duk_require_hobject(ctx, index);
  17525. }
  17526. return NULL;
  17527. }
  17528. /* Useful for internal call sites where we either expect an object (function)
  17529. * or a lightfunc. Returns NULL for a lightfunc.
  17530. */
  17531. DUK_INTERNAL duk_hobject *duk_require_hobject_or_lfunc(duk_context *ctx, duk_idx_t index) {
  17532. duk_hthread *thr = (duk_hthread *) ctx;
  17533. duk_tval *tv;
  17534. DUK_ASSERT_CTX_VALID(ctx);
  17535. tv = duk_require_tval(ctx, index);
  17536. DUK_ASSERT(tv != NULL);
  17537. if (DUK_TVAL_IS_OBJECT(tv)) {
  17538. return DUK_TVAL_GET_OBJECT(tv);
  17539. } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  17540. return NULL;
  17541. }
  17542. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_UNEXPECTED_TYPE);
  17543. return NULL; /* not reachable */
  17544. }
  17545. /* Useful for internal call sites where we either expect an object (function)
  17546. * or a lightfunc. Accepts an object (returned as is) or a lightfunc (coerced
  17547. * to an object). Return value is never NULL.
  17548. */
  17549. DUK_INTERNAL duk_hobject *duk_require_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index) {
  17550. duk_hthread *thr = (duk_hthread *) ctx;
  17551. duk_tval *tv;
  17552. DUK_ASSERT_CTX_VALID(ctx);
  17553. tv = duk_require_tval(ctx, index);
  17554. if (DUK_TVAL_IS_OBJECT(tv)) {
  17555. return DUK_TVAL_GET_OBJECT(tv);
  17556. } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  17557. duk_to_object(ctx, index);
  17558. return duk_require_hobject(ctx, index);
  17559. }
  17560. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_UNEXPECTED_TYPE);
  17561. return NULL; /* not reachable */
  17562. }
  17563. DUK_EXTERNAL duk_size_t duk_get_length(duk_context *ctx, duk_idx_t index) {
  17564. duk_tval *tv;
  17565. DUK_ASSERT_CTX_VALID(ctx);
  17566. tv = duk_get_tval(ctx, index);
  17567. if (!tv) {
  17568. return 0;
  17569. }
  17570. switch (DUK_TVAL_GET_TAG(tv)) {
  17571. case DUK_TAG_UNDEFINED:
  17572. case DUK_TAG_NULL:
  17573. case DUK_TAG_BOOLEAN:
  17574. case DUK_TAG_POINTER:
  17575. return 0;
  17576. case DUK_TAG_STRING: {
  17577. duk_hstring *h = DUK_TVAL_GET_STRING(tv);
  17578. DUK_ASSERT(h != NULL);
  17579. return (duk_size_t) DUK_HSTRING_GET_CHARLEN(h);
  17580. }
  17581. case DUK_TAG_OBJECT: {
  17582. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  17583. DUK_ASSERT(h != NULL);
  17584. return (duk_size_t) duk_hobject_get_length((duk_hthread *) ctx, h);
  17585. }
  17586. case DUK_TAG_BUFFER: {
  17587. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  17588. DUK_ASSERT(h != NULL);
  17589. return (duk_size_t) DUK_HBUFFER_GET_SIZE(h);
  17590. }
  17591. case DUK_TAG_LIGHTFUNC: {
  17592. duk_small_uint_t lf_flags;
  17593. lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv);
  17594. return (duk_size_t) DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags);
  17595. }
  17596. #if defined(DUK_USE_FASTINT)
  17597. case DUK_TAG_FASTINT:
  17598. #endif
  17599. default:
  17600. /* number */
  17601. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  17602. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  17603. return 0;
  17604. }
  17605. DUK_UNREACHABLE();
  17606. }
  17607. DUK_INTERNAL void duk_set_length(duk_context *ctx, duk_idx_t index, duk_size_t length) {
  17608. duk_hthread *thr = (duk_hthread *) ctx;
  17609. duk_hobject *h;
  17610. DUK_ASSERT_CTX_VALID(ctx);
  17611. h = duk_get_hobject(ctx, index);
  17612. if (!h) {
  17613. return;
  17614. }
  17615. duk_hobject_set_length(thr, h, (duk_uint32_t) length); /* XXX: typing */
  17616. }
  17617. /*
  17618. * Conversions and coercions
  17619. *
  17620. * The conversion/coercions are in-place operations on the value stack.
  17621. * Some operations are implemented here directly, while others call a
  17622. * helper in duk_js_ops.c after validating arguments.
  17623. */
  17624. /* E5 Section 8.12.8 */
  17625. DUK_LOCAL duk_bool_t duk__defaultvalue_coerce_attempt(duk_context *ctx, duk_idx_t index, duk_small_int_t func_stridx) {
  17626. if (duk_get_prop_stridx(ctx, index, func_stridx)) {
  17627. /* [ ... func ] */
  17628. if (duk_is_callable(ctx, -1)) {
  17629. duk_dup(ctx, index); /* -> [ ... func this ] */
  17630. duk_call_method(ctx, 0); /* -> [ ... retval ] */
  17631. if (duk_is_primitive(ctx, -1)) {
  17632. duk_replace(ctx, index);
  17633. return 1;
  17634. }
  17635. /* [ ... retval ]; popped below */
  17636. }
  17637. }
  17638. duk_pop(ctx); /* [ ... func/retval ] -> [ ... ] */
  17639. return 0;
  17640. }
  17641. DUK_EXTERNAL void duk_to_defaultvalue(duk_context *ctx, duk_idx_t index, duk_int_t hint) {
  17642. duk_hthread *thr = (duk_hthread *) ctx;
  17643. duk_hobject *obj;
  17644. /* inline initializer for coercers[] is not allowed by old compilers like BCC */
  17645. duk_small_int_t coercers[2];
  17646. DUK_ASSERT_CTX_VALID(ctx);
  17647. DUK_ASSERT(thr != NULL);
  17648. coercers[0] = DUK_STRIDX_VALUE_OF;
  17649. coercers[1] = DUK_STRIDX_TO_STRING;
  17650. index = duk_require_normalize_index(ctx, index);
  17651. obj = duk_require_hobject_or_lfunc(ctx, index);
  17652. if (hint == DUK_HINT_NONE) {
  17653. if (obj != NULL && DUK_HOBJECT_GET_CLASS_NUMBER(obj) == DUK_HOBJECT_CLASS_DATE) {
  17654. hint = DUK_HINT_STRING;
  17655. } else {
  17656. hint = DUK_HINT_NUMBER;
  17657. }
  17658. }
  17659. if (hint == DUK_HINT_STRING) {
  17660. coercers[0] = DUK_STRIDX_TO_STRING;
  17661. coercers[1] = DUK_STRIDX_VALUE_OF;
  17662. }
  17663. if (duk__defaultvalue_coerce_attempt(ctx, index, coercers[0])) {
  17664. return;
  17665. }
  17666. if (duk__defaultvalue_coerce_attempt(ctx, index, coercers[1])) {
  17667. return;
  17668. }
  17669. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_DEFAULTVALUE_COERCE_FAILED);
  17670. }
  17671. DUK_EXTERNAL void duk_to_undefined(duk_context *ctx, duk_idx_t index) {
  17672. duk_hthread *thr = (duk_hthread *) ctx;
  17673. duk_tval *tv;
  17674. DUK_ASSERT_CTX_VALID(ctx);
  17675. DUK_UNREF(thr);
  17676. tv = duk_require_tval(ctx, index);
  17677. DUK_ASSERT(tv != NULL);
  17678. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
  17679. }
  17680. DUK_EXTERNAL void duk_to_null(duk_context *ctx, duk_idx_t index) {
  17681. duk_hthread *thr = (duk_hthread *) ctx;
  17682. duk_tval *tv;
  17683. DUK_ASSERT_CTX_VALID(ctx);
  17684. DUK_UNREF(thr);
  17685. tv = duk_require_tval(ctx, index);
  17686. DUK_ASSERT(tv != NULL);
  17687. DUK_TVAL_SET_NULL_UPDREF(thr, tv); /* side effects */
  17688. }
  17689. /* E5 Section 9.1 */
  17690. DUK_EXTERNAL void duk_to_primitive(duk_context *ctx, duk_idx_t index, duk_int_t hint) {
  17691. DUK_ASSERT_CTX_VALID(ctx);
  17692. DUK_ASSERT(hint == DUK_HINT_NONE || hint == DUK_HINT_NUMBER || hint == DUK_HINT_STRING);
  17693. index = duk_require_normalize_index(ctx, index);
  17694. if (!duk_check_type_mask(ctx, index, DUK_TYPE_MASK_OBJECT |
  17695. DUK_TYPE_MASK_LIGHTFUNC)) {
  17696. /* everything except object stay as is */
  17697. return;
  17698. }
  17699. duk_to_defaultvalue(ctx, index, hint);
  17700. }
  17701. /* E5 Section 9.2 */
  17702. DUK_EXTERNAL duk_bool_t duk_to_boolean(duk_context *ctx, duk_idx_t index) {
  17703. duk_hthread *thr = (duk_hthread *) ctx;
  17704. duk_tval *tv;
  17705. duk_bool_t val;
  17706. DUK_ASSERT_CTX_VALID(ctx);
  17707. DUK_UNREF(thr);
  17708. index = duk_require_normalize_index(ctx, index);
  17709. tv = duk_require_tval(ctx, index);
  17710. DUK_ASSERT(tv != NULL);
  17711. val = duk_js_toboolean(tv);
  17712. DUK_ASSERT(val == 0 || val == 1);
  17713. /* Note: no need to re-lookup tv, conversion is side effect free */
  17714. DUK_ASSERT(tv != NULL);
  17715. DUK_TVAL_SET_BOOLEAN_UPDREF(thr, tv, val); /* side effects */
  17716. return val;
  17717. }
  17718. DUK_EXTERNAL duk_double_t duk_to_number(duk_context *ctx, duk_idx_t index) {
  17719. duk_hthread *thr = (duk_hthread *) ctx;
  17720. duk_tval *tv;
  17721. duk_double_t d;
  17722. DUK_ASSERT_CTX_VALID(ctx);
  17723. tv = duk_require_tval(ctx, index);
  17724. DUK_ASSERT(tv != NULL);
  17725. /* XXX: fastint? */
  17726. d = duk_js_tonumber(thr, tv);
  17727. /* Note: need to re-lookup because ToNumber() may have side effects */
  17728. tv = duk_require_tval(ctx, index);
  17729. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv, d); /* side effects */
  17730. return d;
  17731. }
  17732. /* XXX: combine all the integer conversions: they share everything
  17733. * but the helper function for coercion.
  17734. */
  17735. typedef duk_double_t (*duk__toint_coercer)(duk_hthread *thr, duk_tval *tv);
  17736. DUK_LOCAL duk_double_t duk__to_int_uint_helper(duk_context *ctx, duk_idx_t index, duk__toint_coercer coerce_func) {
  17737. duk_hthread *thr = (duk_hthread *) ctx;
  17738. duk_tval *tv;
  17739. duk_double_t d;
  17740. DUK_ASSERT_CTX_VALID(ctx);
  17741. tv = duk_require_tval(ctx, index);
  17742. DUK_ASSERT(tv != NULL);
  17743. d = coerce_func(thr, tv);
  17744. /* XXX: fastint? */
  17745. /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
  17746. tv = duk_require_tval(ctx, index);
  17747. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv, d); /* side effects */
  17748. return d;
  17749. }
  17750. DUK_EXTERNAL duk_int_t duk_to_int(duk_context *ctx, duk_idx_t index) {
  17751. /* Value coercion (in stack): ToInteger(), E5 Section 9.4
  17752. * API return value coercion: custom
  17753. */
  17754. DUK_ASSERT_CTX_VALID(ctx);
  17755. (void) duk__to_int_uint_helper(ctx, index, duk_js_tointeger);
  17756. return (duk_int_t) duk__api_coerce_d2i(ctx, index, 0 /*require*/);
  17757. }
  17758. DUK_EXTERNAL duk_uint_t duk_to_uint(duk_context *ctx, duk_idx_t index) {
  17759. /* Value coercion (in stack): ToInteger(), E5 Section 9.4
  17760. * API return value coercion: custom
  17761. */
  17762. DUK_ASSERT_CTX_VALID(ctx);
  17763. (void) duk__to_int_uint_helper(ctx, index, duk_js_tointeger);
  17764. return (duk_uint_t) duk__api_coerce_d2ui(ctx, index, 0 /*require*/);
  17765. }
  17766. DUK_EXTERNAL duk_int32_t duk_to_int32(duk_context *ctx, duk_idx_t index) {
  17767. duk_hthread *thr = (duk_hthread *) ctx;
  17768. duk_tval *tv;
  17769. duk_int32_t ret;
  17770. DUK_ASSERT_CTX_VALID(ctx);
  17771. tv = duk_require_tval(ctx, index);
  17772. DUK_ASSERT(tv != NULL);
  17773. ret = duk_js_toint32(thr, tv);
  17774. /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
  17775. tv = duk_require_tval(ctx, index);
  17776. #if defined(DUK_USE_FASTINT)
  17777. DUK_TVAL_SET_FASTINT_I32_UPDREF(thr, tv, ret); /* side effects */
  17778. return ret;
  17779. #else
  17780. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv, (duk_double_t) ret); /* side effects */
  17781. return ret;
  17782. #endif
  17783. }
  17784. DUK_EXTERNAL duk_uint32_t duk_to_uint32(duk_context *ctx, duk_idx_t index) {
  17785. duk_hthread *thr = (duk_hthread *) ctx;
  17786. duk_tval *tv;
  17787. duk_uint32_t ret;
  17788. DUK_ASSERT_CTX_VALID(ctx);
  17789. tv = duk_require_tval(ctx, index);
  17790. DUK_ASSERT(tv != NULL);
  17791. ret = duk_js_touint32(thr, tv);
  17792. /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
  17793. tv = duk_require_tval(ctx, index);
  17794. #if defined(DUK_USE_FASTINT)
  17795. DUK_TVAL_SET_FASTINT_U32_UPDREF(thr, tv, ret); /* side effects */
  17796. return ret;
  17797. #else
  17798. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv, (duk_double_t) ret); /* side effects */
  17799. #endif
  17800. return ret;
  17801. }
  17802. DUK_EXTERNAL duk_uint16_t duk_to_uint16(duk_context *ctx, duk_idx_t index) {
  17803. duk_hthread *thr = (duk_hthread *) ctx;
  17804. duk_tval *tv;
  17805. duk_uint16_t ret;
  17806. DUK_ASSERT_CTX_VALID(ctx);
  17807. tv = duk_require_tval(ctx, index);
  17808. DUK_ASSERT(tv != NULL);
  17809. ret = duk_js_touint16(thr, tv);
  17810. /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
  17811. tv = duk_require_tval(ctx, index);
  17812. #if defined(DUK_USE_FASTINT)
  17813. DUK_TVAL_SET_FASTINT_U32_UPDREF(thr, tv, ret); /* side effects */
  17814. return ret;
  17815. #else
  17816. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv, (duk_double_t) ret); /* side effects */
  17817. #endif
  17818. return ret;
  17819. }
  17820. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  17821. /* Special coercion for Uint8ClampedArray. */
  17822. DUK_INTERNAL duk_uint8_t duk_to_uint8clamped(duk_context *ctx, duk_idx_t index) {
  17823. duk_double_t d;
  17824. duk_double_t t;
  17825. duk_uint8_t ret;
  17826. /* XXX: Simplify this algorithm, should be possible to come up with
  17827. * a shorter and faster algorithm by inspecting IEEE representation
  17828. * directly.
  17829. */
  17830. d = duk_to_number(ctx, index);
  17831. if (d <= 0.0) {
  17832. return 0;
  17833. } else if (d >= 255) {
  17834. return 255;
  17835. } else if (DUK_ISNAN(d)) {
  17836. /* Avoid NaN-to-integer coercion as it is compiler specific. */
  17837. return 0;
  17838. }
  17839. t = d - DUK_FLOOR(d);
  17840. if (t == 0.5) {
  17841. /* Exact halfway, round to even. */
  17842. ret = (duk_uint8_t) d;
  17843. ret = (ret + 1) & 0xfe; /* Example: d=3.5, t=0.5 -> ret = (3 + 1) & 0xfe = 4 & 0xfe = 4
  17844. * Example: d=4.5, t=0.5 -> ret = (4 + 1) & 0xfe = 5 & 0xfe = 4
  17845. */
  17846. } else {
  17847. /* Not halfway, round to nearest. */
  17848. ret = (duk_uint8_t) (d + 0.5);
  17849. }
  17850. return ret;
  17851. }
  17852. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  17853. DUK_EXTERNAL const char *duk_to_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) {
  17854. DUK_ASSERT_CTX_VALID(ctx);
  17855. (void) duk_to_string(ctx, index);
  17856. return duk_require_lstring(ctx, index, out_len);
  17857. }
  17858. DUK_LOCAL duk_ret_t duk__safe_to_string_raw(duk_context *ctx) {
  17859. DUK_ASSERT_CTX_VALID(ctx);
  17860. duk_to_string(ctx, -1);
  17861. return 1;
  17862. }
  17863. DUK_EXTERNAL const char *duk_safe_to_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) {
  17864. DUK_ASSERT_CTX_VALID(ctx);
  17865. index = duk_require_normalize_index(ctx, index);
  17866. /* We intentionally ignore the duk_safe_call() return value and only
  17867. * check the output type. This way we don't also need to check that
  17868. * the returned value is indeed a string in the success case.
  17869. */
  17870. duk_dup(ctx, index);
  17871. (void) duk_safe_call(ctx, duk__safe_to_string_raw, 1 /*nargs*/, 1 /*nrets*/);
  17872. if (!duk_is_string(ctx, -1)) {
  17873. /* Error: try coercing error to string once. */
  17874. (void) duk_safe_call(ctx, duk__safe_to_string_raw, 1 /*nargs*/, 1 /*nrets*/);
  17875. if (!duk_is_string(ctx, -1)) {
  17876. /* Double error */
  17877. duk_pop(ctx);
  17878. duk_push_hstring_stridx(ctx, DUK_STRIDX_UC_ERROR);
  17879. } else {
  17880. ;
  17881. }
  17882. } else {
  17883. ;
  17884. }
  17885. DUK_ASSERT(duk_is_string(ctx, -1));
  17886. DUK_ASSERT(duk_get_string(ctx, -1) != NULL);
  17887. duk_replace(ctx, index);
  17888. return duk_get_lstring(ctx, index, out_len);
  17889. }
  17890. #if defined(DUK_USE_DEBUGGER_SUPPORT) /* only needed by debugger for now */
  17891. DUK_EXTERNAL duk_hstring *duk_safe_to_hstring(duk_context *ctx, duk_idx_t index) {
  17892. (void) duk_safe_to_string(ctx, index);
  17893. DUK_ASSERT(duk_is_string(ctx, index));
  17894. DUK_ASSERT(duk_get_hstring(ctx, index) != NULL);
  17895. return duk_get_hstring(ctx, index);
  17896. }
  17897. #endif
  17898. /* Coerce top into Object.prototype.toString() output. */
  17899. DUK_INTERNAL void duk_to_object_class_string_top(duk_context *ctx) {
  17900. duk_hthread *thr;
  17901. duk_uint_t typemask;
  17902. duk_hstring *h_strclass;
  17903. DUK_ASSERT_CTX_VALID(ctx);
  17904. thr = (duk_hthread *) ctx;
  17905. typemask = duk_get_type_mask(ctx, -1);
  17906. if (typemask & DUK_TYPE_MASK_UNDEFINED) {
  17907. h_strclass = DUK_HTHREAD_STRING_UC_UNDEFINED(thr);
  17908. } else if (typemask & DUK_TYPE_MASK_NULL) {
  17909. h_strclass = DUK_HTHREAD_STRING_UC_NULL(thr);
  17910. } else {
  17911. duk_hobject *h_obj;
  17912. duk_to_object(ctx, -1);
  17913. h_obj = duk_get_hobject(ctx, -1);
  17914. DUK_ASSERT(h_obj != NULL);
  17915. h_strclass = DUK_HOBJECT_GET_CLASS_STRING(thr->heap, h_obj);
  17916. }
  17917. DUK_ASSERT(h_strclass != NULL);
  17918. duk_pop(ctx);
  17919. duk_push_sprintf(ctx, "[object %s]", (const char *) DUK_HSTRING_GET_DATA(h_strclass));
  17920. }
  17921. DUK_INTERNAL void duk_push_hobject_class_string(duk_context *ctx, duk_hobject *h) {
  17922. duk_hthread *thr;
  17923. duk_hstring *h_strclass;
  17924. DUK_ASSERT_CTX_VALID(ctx);
  17925. DUK_ASSERT(h != NULL);
  17926. thr = (duk_hthread *) ctx;
  17927. h_strclass = DUK_HOBJECT_GET_CLASS_STRING(thr->heap, h);
  17928. DUK_ASSERT(h_strclass != NULL);
  17929. duk_push_sprintf(ctx, "[object %s]", (const char *) DUK_HSTRING_GET_DATA(h_strclass));
  17930. }
  17931. /* XXX: other variants like uint, u32 etc */
  17932. DUK_INTERNAL duk_int_t duk_to_int_clamped_raw(duk_context *ctx, duk_idx_t index, duk_int_t minval, duk_int_t maxval, duk_bool_t *out_clamped) {
  17933. duk_hthread *thr = (duk_hthread *) ctx;
  17934. duk_tval *tv;
  17935. duk_tval tv_tmp;
  17936. duk_double_t d, dmin, dmax;
  17937. duk_int_t res;
  17938. duk_bool_t clamped = 0;
  17939. DUK_ASSERT_CTX_VALID(ctx);
  17940. tv = duk_require_tval(ctx, index);
  17941. DUK_ASSERT(tv != NULL);
  17942. d = duk_js_tointeger(thr, tv); /* E5 Section 9.4, ToInteger() */
  17943. dmin = (duk_double_t) minval;
  17944. dmax = (duk_double_t) maxval;
  17945. if (d < dmin) {
  17946. clamped = 1;
  17947. res = minval;
  17948. d = dmin;
  17949. } else if (d > dmax) {
  17950. clamped = 1;
  17951. res = maxval;
  17952. d = dmax;
  17953. } else {
  17954. res = (duk_int_t) d;
  17955. }
  17956. /* 'd' and 'res' agree here */
  17957. /* Relookup in case duk_js_tointeger() ends up e.g. coercing an object. */
  17958. tv = duk_require_tval(ctx, index);
  17959. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  17960. #if defined(DUK_USE_FASTINT)
  17961. #if (DUK_INT_MAX <= 0x7fffffffL)
  17962. DUK_TVAL_SET_FASTINT_I32(tv, res);
  17963. #else
  17964. /* Clamping needed if duk_int_t is 64 bits. */
  17965. if (res >= DUK_FASTINT_MIN && res <= DUK_FASTINT_MAX) {
  17966. DUK_TVAL_SET_FASTINT(tv, res);
  17967. } else {
  17968. DUK_TVAL_SET_NUMBER(tv, d);
  17969. }
  17970. #endif
  17971. #else
  17972. DUK_TVAL_SET_NUMBER(tv, d); /* no need to incref */
  17973. #endif
  17974. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  17975. if (out_clamped) {
  17976. *out_clamped = clamped;
  17977. } else {
  17978. /* coerced value is updated to value stack even when RangeError thrown */
  17979. if (clamped) {
  17980. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_NUMBER_OUTSIDE_RANGE);
  17981. }
  17982. }
  17983. return res;
  17984. }
  17985. DUK_INTERNAL duk_int_t duk_to_int_clamped(duk_context *ctx, duk_idx_t index, duk_idx_t minval, duk_idx_t maxval) {
  17986. duk_bool_t dummy;
  17987. return duk_to_int_clamped_raw(ctx, index, minval, maxval, &dummy);
  17988. }
  17989. DUK_INTERNAL duk_int_t duk_to_int_check_range(duk_context *ctx, duk_idx_t index, duk_int_t minval, duk_int_t maxval) {
  17990. return duk_to_int_clamped_raw(ctx, index, minval, maxval, NULL); /* out_clamped==NULL -> RangeError if outside range */
  17991. }
  17992. DUK_EXTERNAL const char *duk_to_string(duk_context *ctx, duk_idx_t index) {
  17993. duk_hthread *thr = (duk_hthread *) ctx;
  17994. duk_tval *tv;
  17995. DUK_ASSERT_CTX_VALID(ctx);
  17996. DUK_UNREF(thr);
  17997. index = duk_require_normalize_index(ctx, index);
  17998. tv = duk_require_tval(ctx, index);
  17999. DUK_ASSERT(tv != NULL);
  18000. switch (DUK_TVAL_GET_TAG(tv)) {
  18001. case DUK_TAG_UNDEFINED: {
  18002. duk_push_hstring_stridx(ctx, DUK_STRIDX_LC_UNDEFINED);
  18003. break;
  18004. }
  18005. case DUK_TAG_NULL: {
  18006. duk_push_hstring_stridx(ctx, DUK_STRIDX_LC_NULL);
  18007. break;
  18008. }
  18009. case DUK_TAG_BOOLEAN: {
  18010. if (DUK_TVAL_GET_BOOLEAN(tv)) {
  18011. duk_push_hstring_stridx(ctx, DUK_STRIDX_TRUE);
  18012. } else {
  18013. duk_push_hstring_stridx(ctx, DUK_STRIDX_FALSE);
  18014. }
  18015. break;
  18016. }
  18017. case DUK_TAG_STRING: {
  18018. /* nop */
  18019. goto skip_replace;
  18020. }
  18021. case DUK_TAG_OBJECT: {
  18022. duk_to_primitive(ctx, index, DUK_HINT_STRING);
  18023. return duk_to_string(ctx, index); /* Note: recursive call */
  18024. }
  18025. case DUK_TAG_BUFFER: {
  18026. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  18027. /* Note: this allows creation of internal strings. */
  18028. DUK_ASSERT(h != NULL);
  18029. duk_push_lstring(ctx,
  18030. (const char *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h),
  18031. (duk_size_t) DUK_HBUFFER_GET_SIZE(h));
  18032. break;
  18033. }
  18034. case DUK_TAG_POINTER: {
  18035. void *ptr = DUK_TVAL_GET_POINTER(tv);
  18036. if (ptr != NULL) {
  18037. duk_push_sprintf(ctx, DUK_STR_FMT_PTR, (void *) ptr);
  18038. } else {
  18039. /* Represent a null pointer as 'null' to be consistent with
  18040. * the JX format variant. Native '%p' format for a NULL
  18041. * pointer may be e.g. '(nil)'.
  18042. */
  18043. duk_push_hstring_stridx(ctx, DUK_STRIDX_LC_NULL);
  18044. }
  18045. break;
  18046. }
  18047. case DUK_TAG_LIGHTFUNC: {
  18048. /* Should match Function.prototype.toString() */
  18049. duk_push_lightfunc_tostring(ctx, tv);
  18050. break;
  18051. }
  18052. #if defined(DUK_USE_FASTINT)
  18053. case DUK_TAG_FASTINT:
  18054. #endif
  18055. default: {
  18056. /* number */
  18057. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  18058. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  18059. duk_push_tval(ctx, tv);
  18060. duk_numconv_stringify(ctx,
  18061. 10 /*radix*/,
  18062. 0 /*precision:shortest*/,
  18063. 0 /*force_exponential*/);
  18064. break;
  18065. }
  18066. }
  18067. duk_replace(ctx, index);
  18068. skip_replace:
  18069. return duk_require_string(ctx, index);
  18070. }
  18071. DUK_INTERNAL duk_hstring *duk_to_hstring(duk_context *ctx, duk_idx_t index) {
  18072. duk_hstring *ret;
  18073. DUK_ASSERT_CTX_VALID(ctx);
  18074. duk_to_string(ctx, index);
  18075. ret = duk_get_hstring(ctx, index);
  18076. DUK_ASSERT(ret != NULL);
  18077. return ret;
  18078. }
  18079. DUK_EXTERNAL void *duk_to_buffer_raw(duk_context *ctx, duk_idx_t index, duk_size_t *out_size, duk_uint_t mode) {
  18080. duk_hthread *thr = (duk_hthread *) ctx;
  18081. duk_hbuffer *h_buf;
  18082. const duk_uint8_t *src_data;
  18083. duk_size_t src_size;
  18084. duk_uint8_t *dst_data;
  18085. DUK_ASSERT_CTX_VALID(ctx);
  18086. DUK_UNREF(thr);
  18087. index = duk_require_normalize_index(ctx, index);
  18088. h_buf = duk_get_hbuffer(ctx, index);
  18089. if (h_buf != NULL) {
  18090. /* Buffer is kept as is, with the fixed/dynamic nature of the
  18091. * buffer only changed if requested. An external buffer
  18092. * is converted into a non-external dynamic buffer in a
  18093. * duk_to_dynamic_buffer() call.
  18094. */
  18095. duk_uint_t tmp;
  18096. duk_uint8_t *tmp_ptr;
  18097. tmp_ptr = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_buf);
  18098. src_data = (const duk_uint8_t *) tmp_ptr;
  18099. src_size = DUK_HBUFFER_GET_SIZE(h_buf);
  18100. tmp = (DUK_HBUFFER_HAS_DYNAMIC(h_buf) ? DUK_BUF_MODE_DYNAMIC : DUK_BUF_MODE_FIXED);
  18101. if ((tmp == mode && !DUK_HBUFFER_HAS_EXTERNAL(h_buf)) ||
  18102. mode == DUK_BUF_MODE_DONTCARE) {
  18103. /* Note: src_data may be NULL if input is a zero-size
  18104. * dynamic buffer.
  18105. */
  18106. dst_data = tmp_ptr;
  18107. goto skip_copy;
  18108. }
  18109. } else {
  18110. /* Non-buffer value is first ToString() coerced, then converted
  18111. * to a buffer (fixed buffer is used unless a dynamic buffer is
  18112. * explicitly requested).
  18113. */
  18114. src_data = (const duk_uint8_t *) duk_to_lstring(ctx, index, &src_size);
  18115. }
  18116. dst_data = (duk_uint8_t *) duk_push_buffer(ctx, src_size, (mode == DUK_BUF_MODE_DYNAMIC) /*dynamic*/);
  18117. if (DUK_LIKELY(src_size > 0)) {
  18118. /* When src_size == 0, src_data may be NULL (if source
  18119. * buffer is dynamic), and dst_data may be NULL (if
  18120. * target buffer is dynamic). Avoid zero-size memcpy()
  18121. * with an invalid pointer.
  18122. */
  18123. DUK_MEMCPY((void *) dst_data, (const void *) src_data, (size_t) src_size);
  18124. }
  18125. duk_replace(ctx, index);
  18126. skip_copy:
  18127. if (out_size) {
  18128. *out_size = src_size;
  18129. }
  18130. return dst_data;
  18131. }
  18132. DUK_EXTERNAL void *duk_to_pointer(duk_context *ctx, duk_idx_t index) {
  18133. duk_tval *tv;
  18134. void *res;
  18135. DUK_ASSERT_CTX_VALID(ctx);
  18136. index = duk_require_normalize_index(ctx, index);
  18137. tv = duk_require_tval(ctx, index);
  18138. DUK_ASSERT(tv != NULL);
  18139. switch (DUK_TVAL_GET_TAG(tv)) {
  18140. case DUK_TAG_UNDEFINED:
  18141. case DUK_TAG_NULL:
  18142. case DUK_TAG_BOOLEAN:
  18143. res = NULL;
  18144. break;
  18145. case DUK_TAG_POINTER:
  18146. res = DUK_TVAL_GET_POINTER(tv);
  18147. break;
  18148. case DUK_TAG_STRING:
  18149. case DUK_TAG_OBJECT:
  18150. case DUK_TAG_BUFFER:
  18151. /* Heap allocated: return heap pointer which is NOT useful
  18152. * for the caller, except for debugging.
  18153. */
  18154. res = (void *) DUK_TVAL_GET_HEAPHDR(tv);
  18155. break;
  18156. case DUK_TAG_LIGHTFUNC:
  18157. /* Function pointers do not always cast correctly to void *
  18158. * (depends on memory and segmentation model for instance),
  18159. * so they coerce to NULL.
  18160. */
  18161. res = NULL;
  18162. break;
  18163. #if defined(DUK_USE_FASTINT)
  18164. case DUK_TAG_FASTINT:
  18165. #endif
  18166. default:
  18167. /* number */
  18168. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  18169. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  18170. res = NULL;
  18171. break;
  18172. }
  18173. duk_push_pointer(ctx, res);
  18174. duk_replace(ctx, index);
  18175. return res;
  18176. }
  18177. DUK_EXTERNAL void duk_to_object(duk_context *ctx, duk_idx_t index) {
  18178. duk_hthread *thr = (duk_hthread *) ctx;
  18179. duk_tval *tv;
  18180. duk_uint_t flags = 0; /* shared flags for a subset of types */
  18181. duk_small_int_t proto = 0;
  18182. DUK_ASSERT_CTX_VALID(ctx);
  18183. index = duk_require_normalize_index(ctx, index);
  18184. tv = duk_require_tval(ctx, index);
  18185. DUK_ASSERT(tv != NULL);
  18186. switch (DUK_TVAL_GET_TAG(tv)) {
  18187. case DUK_TAG_UNDEFINED:
  18188. case DUK_TAG_NULL: {
  18189. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_OBJECT_COERCIBLE);
  18190. break;
  18191. }
  18192. case DUK_TAG_BOOLEAN: {
  18193. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  18194. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BOOLEAN);
  18195. proto = DUK_BIDX_BOOLEAN_PROTOTYPE;
  18196. goto create_object;
  18197. }
  18198. case DUK_TAG_STRING: {
  18199. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  18200. DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ |
  18201. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_STRING);
  18202. proto = DUK_BIDX_STRING_PROTOTYPE;
  18203. goto create_object;
  18204. }
  18205. case DUK_TAG_OBJECT: {
  18206. /* nop */
  18207. break;
  18208. }
  18209. case DUK_TAG_BUFFER: {
  18210. /* A plain buffer coerces to a Duktape.Buffer because it's the
  18211. * object counterpart of the plain buffer value. But it might
  18212. * still make more sense to produce an ArrayBuffer here?
  18213. */
  18214. duk_hbufferobject *h_bufobj;
  18215. duk_hbuffer *h_val;
  18216. h_val = DUK_TVAL_GET_BUFFER(tv);
  18217. DUK_ASSERT(h_val != NULL);
  18218. h_bufobj = duk_push_bufferobject_raw(ctx,
  18219. DUK_HOBJECT_FLAG_EXTENSIBLE |
  18220. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  18221. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER),
  18222. DUK_BIDX_BUFFER_PROTOTYPE);
  18223. DUK_ASSERT(h_bufobj != NULL);
  18224. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE((duk_hobject *) h_bufobj));
  18225. DUK_ASSERT(DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) h_bufobj));
  18226. h_bufobj->buf = h_val;
  18227. DUK_HBUFFER_INCREF(thr, h_val);
  18228. DUK_ASSERT(h_bufobj->offset == 0);
  18229. h_bufobj->length = (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_val);
  18230. DUK_ASSERT(h_bufobj->shift == 0);
  18231. DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8);
  18232. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  18233. goto replace_value;
  18234. }
  18235. case DUK_TAG_POINTER: {
  18236. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  18237. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_POINTER);
  18238. proto = DUK_BIDX_POINTER_PROTOTYPE;
  18239. goto create_object;
  18240. }
  18241. case DUK_TAG_LIGHTFUNC: {
  18242. /* Lightfunc coerces to a Function instance with concrete
  18243. * properties. Since 'length' is virtual for Duktape/C
  18244. * functions, don't need to define that.
  18245. *
  18246. * The result is made extensible to mimic what happens to
  18247. * strings:
  18248. * > Object.isExtensible(Object('foo'))
  18249. * true
  18250. */
  18251. duk_small_uint_t lf_flags;
  18252. duk_small_uint_t nargs;
  18253. duk_small_uint_t lf_len;
  18254. duk_c_function func;
  18255. duk_hnativefunction *nf;
  18256. DUK_TVAL_GET_LIGHTFUNC(tv, func, lf_flags);
  18257. nargs = DUK_LFUNC_FLAGS_GET_NARGS(lf_flags);
  18258. if (nargs == DUK_LFUNC_NARGS_VARARGS) {
  18259. nargs = DUK_VARARGS;
  18260. }
  18261. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  18262. DUK_HOBJECT_FLAG_CONSTRUCTABLE |
  18263. DUK_HOBJECT_FLAG_NATIVEFUNCTION |
  18264. DUK_HOBJECT_FLAG_NEWENV |
  18265. DUK_HOBJECT_FLAG_STRICT |
  18266. DUK_HOBJECT_FLAG_NOTAIL |
  18267. /* DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC: omitted here intentionally */
  18268. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION);
  18269. (void) duk__push_c_function_raw(ctx, func, (duk_idx_t) nargs, flags);
  18270. lf_len = DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags);
  18271. if (lf_len != nargs) {
  18272. /* Explicit length is only needed if it differs from 'nargs'. */
  18273. duk_push_int(ctx, (duk_int_t) lf_len);
  18274. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE);
  18275. }
  18276. duk_push_lightfunc_name(ctx, tv);
  18277. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE);
  18278. nf = duk_get_hnativefunction(ctx, -1);
  18279. DUK_ASSERT(nf != NULL);
  18280. nf->magic = (duk_int16_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags);
  18281. /* Enable DUKFUNC exotic behavior once properties are set up. */
  18282. DUK_HOBJECT_SET_EXOTIC_DUKFUNC((duk_hobject *) nf);
  18283. goto replace_value;
  18284. }
  18285. #if defined(DUK_USE_FASTINT)
  18286. case DUK_TAG_FASTINT:
  18287. #endif
  18288. default: {
  18289. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  18290. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  18291. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  18292. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_NUMBER);
  18293. proto = DUK_BIDX_NUMBER_PROTOTYPE;
  18294. goto create_object;
  18295. }
  18296. }
  18297. return;
  18298. create_object:
  18299. (void) duk_push_object_helper(ctx, flags, proto);
  18300. /* Note: Boolean prototype's internal value property is not writable,
  18301. * but duk_xdef_prop_stridx() disregards the write protection. Boolean
  18302. * instances are immutable.
  18303. *
  18304. * String and buffer special behaviors are already enabled which is not
  18305. * ideal, but a write to the internal value is not affected by them.
  18306. */
  18307. duk_dup(ctx, index);
  18308. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE);
  18309. replace_value:
  18310. duk_replace(ctx, index);
  18311. }
  18312. /*
  18313. * Type checking
  18314. */
  18315. DUK_LOCAL duk_bool_t duk__tag_check(duk_context *ctx, duk_idx_t index, duk_small_uint_t tag) {
  18316. duk_tval *tv;
  18317. tv = duk_get_tval(ctx, index);
  18318. if (!tv) {
  18319. return 0;
  18320. }
  18321. return (DUK_TVAL_GET_TAG(tv) == tag);
  18322. }
  18323. DUK_LOCAL duk_bool_t duk__obj_flag_any_default_false(duk_context *ctx, duk_idx_t index, duk_uint_t flag_mask) {
  18324. duk_hobject *obj;
  18325. DUK_ASSERT_CTX_VALID(ctx);
  18326. obj = duk_get_hobject(ctx, index);
  18327. if (obj) {
  18328. return (DUK_HEAPHDR_CHECK_FLAG_BITS((duk_heaphdr *) obj, flag_mask) ? 1 : 0);
  18329. }
  18330. return 0;
  18331. }
  18332. DUK_EXTERNAL duk_int_t duk_get_type(duk_context *ctx, duk_idx_t index) {
  18333. duk_tval *tv;
  18334. DUK_ASSERT_CTX_VALID(ctx);
  18335. tv = duk_get_tval(ctx, index);
  18336. if (!tv) {
  18337. return DUK_TYPE_NONE;
  18338. }
  18339. switch (DUK_TVAL_GET_TAG(tv)) {
  18340. case DUK_TAG_UNDEFINED:
  18341. return DUK_TYPE_UNDEFINED;
  18342. case DUK_TAG_NULL:
  18343. return DUK_TYPE_NULL;
  18344. case DUK_TAG_BOOLEAN:
  18345. return DUK_TYPE_BOOLEAN;
  18346. case DUK_TAG_STRING:
  18347. return DUK_TYPE_STRING;
  18348. case DUK_TAG_OBJECT:
  18349. return DUK_TYPE_OBJECT;
  18350. case DUK_TAG_BUFFER:
  18351. return DUK_TYPE_BUFFER;
  18352. case DUK_TAG_POINTER:
  18353. return DUK_TYPE_POINTER;
  18354. case DUK_TAG_LIGHTFUNC:
  18355. return DUK_TYPE_LIGHTFUNC;
  18356. #if defined(DUK_USE_FASTINT)
  18357. case DUK_TAG_FASTINT:
  18358. #endif
  18359. default:
  18360. /* Note: number has no explicit tag (in 8-byte representation) */
  18361. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  18362. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  18363. return DUK_TYPE_NUMBER;
  18364. }
  18365. DUK_UNREACHABLE();
  18366. }
  18367. DUK_EXTERNAL duk_bool_t duk_check_type(duk_context *ctx, duk_idx_t index, duk_int_t type) {
  18368. DUK_ASSERT_CTX_VALID(ctx);
  18369. return (duk_get_type(ctx, index) == type) ? 1 : 0;
  18370. }
  18371. DUK_EXTERNAL duk_uint_t duk_get_type_mask(duk_context *ctx, duk_idx_t index) {
  18372. duk_tval *tv;
  18373. DUK_ASSERT_CTX_VALID(ctx);
  18374. tv = duk_get_tval(ctx, index);
  18375. if (!tv) {
  18376. return DUK_TYPE_MASK_NONE;
  18377. }
  18378. switch (DUK_TVAL_GET_TAG(tv)) {
  18379. case DUK_TAG_UNDEFINED:
  18380. return DUK_TYPE_MASK_UNDEFINED;
  18381. case DUK_TAG_NULL:
  18382. return DUK_TYPE_MASK_NULL;
  18383. case DUK_TAG_BOOLEAN:
  18384. return DUK_TYPE_MASK_BOOLEAN;
  18385. case DUK_TAG_STRING:
  18386. return DUK_TYPE_MASK_STRING;
  18387. case DUK_TAG_OBJECT:
  18388. return DUK_TYPE_MASK_OBJECT;
  18389. case DUK_TAG_BUFFER:
  18390. return DUK_TYPE_MASK_BUFFER;
  18391. case DUK_TAG_POINTER:
  18392. return DUK_TYPE_MASK_POINTER;
  18393. case DUK_TAG_LIGHTFUNC:
  18394. return DUK_TYPE_MASK_LIGHTFUNC;
  18395. #if defined(DUK_USE_FASTINT)
  18396. case DUK_TAG_FASTINT:
  18397. #endif
  18398. default:
  18399. /* Note: number has no explicit tag (in 8-byte representation) */
  18400. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  18401. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  18402. return DUK_TYPE_MASK_NUMBER;
  18403. }
  18404. DUK_UNREACHABLE();
  18405. }
  18406. DUK_EXTERNAL duk_bool_t duk_check_type_mask(duk_context *ctx, duk_idx_t index, duk_uint_t mask) {
  18407. duk_hthread *thr = (duk_hthread *) ctx;
  18408. DUK_ASSERT_CTX_VALID(ctx);
  18409. if (duk_get_type_mask(ctx, index) & mask) {
  18410. return 1;
  18411. }
  18412. if (mask & DUK_TYPE_MASK_THROW) {
  18413. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_UNEXPECTED_TYPE);
  18414. DUK_UNREACHABLE();
  18415. }
  18416. return 0;
  18417. }
  18418. DUK_EXTERNAL duk_bool_t duk_is_undefined(duk_context *ctx, duk_idx_t index) {
  18419. DUK_ASSERT_CTX_VALID(ctx);
  18420. return duk__tag_check(ctx, index, DUK_TAG_UNDEFINED);
  18421. }
  18422. DUK_EXTERNAL duk_bool_t duk_is_null(duk_context *ctx, duk_idx_t index) {
  18423. DUK_ASSERT_CTX_VALID(ctx);
  18424. return duk__tag_check(ctx, index, DUK_TAG_NULL);
  18425. }
  18426. DUK_EXTERNAL duk_bool_t duk_is_null_or_undefined(duk_context *ctx, duk_idx_t index) {
  18427. duk_tval *tv;
  18428. duk_small_uint_t tag;
  18429. DUK_ASSERT_CTX_VALID(ctx);
  18430. tv = duk_get_tval(ctx, index);
  18431. if (!tv) {
  18432. return 0;
  18433. }
  18434. tag = DUK_TVAL_GET_TAG(tv);
  18435. return (tag == DUK_TAG_UNDEFINED) || (tag == DUK_TAG_NULL);
  18436. }
  18437. DUK_EXTERNAL duk_bool_t duk_is_boolean(duk_context *ctx, duk_idx_t index) {
  18438. DUK_ASSERT_CTX_VALID(ctx);
  18439. return duk__tag_check(ctx, index, DUK_TAG_BOOLEAN);
  18440. }
  18441. DUK_EXTERNAL duk_bool_t duk_is_number(duk_context *ctx, duk_idx_t index) {
  18442. duk_tval *tv;
  18443. DUK_ASSERT_CTX_VALID(ctx);
  18444. /*
  18445. * Number is special because it doesn't have a specific
  18446. * tag in the 8-byte representation.
  18447. */
  18448. /* XXX: shorter version for 12-byte representation? */
  18449. tv = duk_get_tval(ctx, index);
  18450. if (!tv) {
  18451. return 0;
  18452. }
  18453. return DUK_TVAL_IS_NUMBER(tv);
  18454. }
  18455. DUK_EXTERNAL duk_bool_t duk_is_nan(duk_context *ctx, duk_idx_t index) {
  18456. /* XXX: This will now return false for non-numbers, even though they would
  18457. * coerce to NaN (as a general rule). In particular, duk_get_number()
  18458. * returns a NaN for non-numbers, so should this function also return
  18459. * true for non-numbers?
  18460. */
  18461. duk_tval *tv;
  18462. DUK_ASSERT_CTX_VALID(ctx);
  18463. tv = duk_get_tval(ctx, index);
  18464. if (!tv || !DUK_TVAL_IS_NUMBER(tv)) {
  18465. return 0;
  18466. }
  18467. return DUK_ISNAN(DUK_TVAL_GET_NUMBER(tv));
  18468. }
  18469. DUK_EXTERNAL duk_bool_t duk_is_string(duk_context *ctx, duk_idx_t index) {
  18470. DUK_ASSERT_CTX_VALID(ctx);
  18471. return duk__tag_check(ctx, index, DUK_TAG_STRING);
  18472. }
  18473. DUK_EXTERNAL duk_bool_t duk_is_object(duk_context *ctx, duk_idx_t index) {
  18474. DUK_ASSERT_CTX_VALID(ctx);
  18475. return duk__tag_check(ctx, index, DUK_TAG_OBJECT);
  18476. }
  18477. DUK_EXTERNAL duk_bool_t duk_is_buffer(duk_context *ctx, duk_idx_t index) {
  18478. DUK_ASSERT_CTX_VALID(ctx);
  18479. return duk__tag_check(ctx, index, DUK_TAG_BUFFER);
  18480. }
  18481. DUK_EXTERNAL duk_bool_t duk_is_pointer(duk_context *ctx, duk_idx_t index) {
  18482. DUK_ASSERT_CTX_VALID(ctx);
  18483. return duk__tag_check(ctx, index, DUK_TAG_POINTER);
  18484. }
  18485. DUK_EXTERNAL duk_bool_t duk_is_lightfunc(duk_context *ctx, duk_idx_t index) {
  18486. DUK_ASSERT_CTX_VALID(ctx);
  18487. return duk__tag_check(ctx, index, DUK_TAG_LIGHTFUNC);
  18488. }
  18489. DUK_EXTERNAL duk_bool_t duk_is_array(duk_context *ctx, duk_idx_t index) {
  18490. duk_hobject *obj;
  18491. DUK_ASSERT_CTX_VALID(ctx);
  18492. obj = duk_get_hobject(ctx, index);
  18493. if (obj) {
  18494. return (DUK_HOBJECT_GET_CLASS_NUMBER(obj) == DUK_HOBJECT_CLASS_ARRAY ? 1 : 0);
  18495. }
  18496. return 0;
  18497. }
  18498. DUK_EXTERNAL duk_bool_t duk_is_function(duk_context *ctx, duk_idx_t index) {
  18499. duk_tval *tv;
  18500. DUK_ASSERT_CTX_VALID(ctx);
  18501. tv = duk_get_tval(ctx, index);
  18502. if (tv && DUK_TVAL_IS_LIGHTFUNC(tv)) {
  18503. return 1;
  18504. }
  18505. return duk__obj_flag_any_default_false(ctx,
  18506. index,
  18507. DUK_HOBJECT_FLAG_COMPILEDFUNCTION |
  18508. DUK_HOBJECT_FLAG_NATIVEFUNCTION |
  18509. DUK_HOBJECT_FLAG_BOUND);
  18510. }
  18511. DUK_EXTERNAL duk_bool_t duk_is_c_function(duk_context *ctx, duk_idx_t index) {
  18512. DUK_ASSERT_CTX_VALID(ctx);
  18513. return duk__obj_flag_any_default_false(ctx,
  18514. index,
  18515. DUK_HOBJECT_FLAG_NATIVEFUNCTION);
  18516. }
  18517. DUK_EXTERNAL duk_bool_t duk_is_ecmascript_function(duk_context *ctx, duk_idx_t index) {
  18518. DUK_ASSERT_CTX_VALID(ctx);
  18519. return duk__obj_flag_any_default_false(ctx,
  18520. index,
  18521. DUK_HOBJECT_FLAG_COMPILEDFUNCTION);
  18522. }
  18523. DUK_EXTERNAL duk_bool_t duk_is_bound_function(duk_context *ctx, duk_idx_t index) {
  18524. DUK_ASSERT_CTX_VALID(ctx);
  18525. return duk__obj_flag_any_default_false(ctx,
  18526. index,
  18527. DUK_HOBJECT_FLAG_BOUND);
  18528. }
  18529. DUK_EXTERNAL duk_bool_t duk_is_thread(duk_context *ctx, duk_idx_t index) {
  18530. DUK_ASSERT_CTX_VALID(ctx);
  18531. return duk__obj_flag_any_default_false(ctx,
  18532. index,
  18533. DUK_HOBJECT_FLAG_THREAD);
  18534. }
  18535. DUK_EXTERNAL duk_bool_t duk_is_callable(duk_context *ctx, duk_idx_t index) {
  18536. /* XXX: currently same as duk_is_function() */
  18537. DUK_ASSERT_CTX_VALID(ctx);
  18538. return duk_is_function(ctx, index);
  18539. }
  18540. DUK_EXTERNAL duk_bool_t duk_is_fixed_buffer(duk_context *ctx, duk_idx_t index) {
  18541. duk_tval *tv;
  18542. DUK_ASSERT_CTX_VALID(ctx);
  18543. tv = duk_get_tval(ctx, index);
  18544. if (tv && DUK_TVAL_IS_BUFFER(tv)) {
  18545. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  18546. DUK_ASSERT(h != NULL);
  18547. return (DUK_HBUFFER_HAS_DYNAMIC(h) ? 0 : 1);
  18548. }
  18549. return 0;
  18550. }
  18551. DUK_EXTERNAL duk_bool_t duk_is_dynamic_buffer(duk_context *ctx, duk_idx_t index) {
  18552. duk_tval *tv;
  18553. DUK_ASSERT_CTX_VALID(ctx);
  18554. tv = duk_get_tval(ctx, index);
  18555. if (tv && DUK_TVAL_IS_BUFFER(tv)) {
  18556. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  18557. DUK_ASSERT(h != NULL);
  18558. return (DUK_HBUFFER_HAS_DYNAMIC(h) && !DUK_HBUFFER_HAS_EXTERNAL(h) ? 1 : 0);
  18559. }
  18560. return 0;
  18561. }
  18562. DUK_EXTERNAL duk_bool_t duk_is_external_buffer(duk_context *ctx, duk_idx_t index) {
  18563. duk_tval *tv;
  18564. DUK_ASSERT_CTX_VALID(ctx);
  18565. tv = duk_get_tval(ctx, index);
  18566. if (tv && DUK_TVAL_IS_BUFFER(tv)) {
  18567. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  18568. DUK_ASSERT(h != NULL);
  18569. return (DUK_HBUFFER_HAS_DYNAMIC(h) && DUK_HBUFFER_HAS_EXTERNAL(h) ? 1 : 0);
  18570. }
  18571. return 0;
  18572. }
  18573. DUK_EXTERNAL duk_errcode_t duk_get_error_code(duk_context *ctx, duk_idx_t index) {
  18574. duk_hthread *thr = (duk_hthread *) ctx;
  18575. duk_hobject *h;
  18576. duk_uint_t sanity;
  18577. DUK_ASSERT_CTX_VALID(ctx);
  18578. h = duk_get_hobject(ctx, index);
  18579. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  18580. do {
  18581. if (!h) {
  18582. return DUK_ERR_NONE;
  18583. }
  18584. if (h == thr->builtins[DUK_BIDX_EVAL_ERROR_PROTOTYPE]) {
  18585. return DUK_ERR_EVAL_ERROR;
  18586. }
  18587. if (h == thr->builtins[DUK_BIDX_RANGE_ERROR_PROTOTYPE]) {
  18588. return DUK_ERR_RANGE_ERROR;
  18589. }
  18590. if (h == thr->builtins[DUK_BIDX_REFERENCE_ERROR_PROTOTYPE]) {
  18591. return DUK_ERR_REFERENCE_ERROR;
  18592. }
  18593. if (h == thr->builtins[DUK_BIDX_SYNTAX_ERROR_PROTOTYPE]) {
  18594. return DUK_ERR_SYNTAX_ERROR;
  18595. }
  18596. if (h == thr->builtins[DUK_BIDX_TYPE_ERROR_PROTOTYPE]) {
  18597. return DUK_ERR_TYPE_ERROR;
  18598. }
  18599. if (h == thr->builtins[DUK_BIDX_URI_ERROR_PROTOTYPE]) {
  18600. return DUK_ERR_URI_ERROR;
  18601. }
  18602. if (h == thr->builtins[DUK_BIDX_ERROR_PROTOTYPE]) {
  18603. return DUK_ERR_ERROR;
  18604. }
  18605. h = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
  18606. } while (--sanity > 0);
  18607. return DUK_ERR_NONE;
  18608. }
  18609. /*
  18610. * Pushers
  18611. */
  18612. DUK_INTERNAL void duk_push_tval(duk_context *ctx, duk_tval *tv) {
  18613. duk_hthread *thr;
  18614. duk_tval *tv_slot;
  18615. DUK_ASSERT_CTX_VALID(ctx);
  18616. DUK_ASSERT(tv != NULL);
  18617. thr = (duk_hthread *) ctx;
  18618. DUK__CHECK_SPACE();
  18619. tv_slot = thr->valstack_top++;
  18620. DUK_TVAL_SET_TVAL(tv_slot, tv);
  18621. DUK_TVAL_INCREF(thr, tv); /* no side effects */
  18622. }
  18623. DUK_EXTERNAL void duk_push_undefined(duk_context *ctx) {
  18624. duk_hthread *thr;
  18625. DUK_ASSERT_CTX_VALID(ctx);
  18626. thr = (duk_hthread *) ctx;
  18627. DUK__CHECK_SPACE();
  18628. /* Because value stack init policy is 'undefined above top',
  18629. * we don't need to write, just assert.
  18630. */
  18631. thr->valstack_top++;
  18632. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top - 1));
  18633. }
  18634. DUK_EXTERNAL void duk_push_null(duk_context *ctx) {
  18635. duk_hthread *thr;
  18636. duk_tval *tv_slot;
  18637. DUK_ASSERT_CTX_VALID(ctx);
  18638. thr = (duk_hthread *) ctx;
  18639. DUK__CHECK_SPACE();
  18640. tv_slot = thr->valstack_top++;
  18641. DUK_TVAL_SET_NULL(tv_slot);
  18642. }
  18643. DUK_EXTERNAL void duk_push_boolean(duk_context *ctx, duk_bool_t val) {
  18644. duk_hthread *thr;
  18645. duk_tval *tv_slot;
  18646. duk_small_int_t b;
  18647. DUK_ASSERT_CTX_VALID(ctx);
  18648. thr = (duk_hthread *) ctx;
  18649. DUK__CHECK_SPACE();
  18650. b = (val ? 1 : 0); /* ensure value is 1 or 0 (not other non-zero) */
  18651. tv_slot = thr->valstack_top++;
  18652. DUK_TVAL_SET_BOOLEAN(tv_slot, b);
  18653. }
  18654. DUK_EXTERNAL void duk_push_true(duk_context *ctx) {
  18655. duk_hthread *thr;
  18656. duk_tval *tv_slot;
  18657. DUK_ASSERT_CTX_VALID(ctx);
  18658. thr = (duk_hthread *) ctx;
  18659. DUK__CHECK_SPACE();
  18660. tv_slot = thr->valstack_top++;
  18661. DUK_TVAL_SET_BOOLEAN_TRUE(tv_slot);
  18662. }
  18663. DUK_EXTERNAL void duk_push_false(duk_context *ctx) {
  18664. duk_hthread *thr;
  18665. duk_tval *tv_slot;
  18666. DUK_ASSERT_CTX_VALID(ctx);
  18667. thr = (duk_hthread *) ctx;
  18668. DUK__CHECK_SPACE();
  18669. tv_slot = thr->valstack_top++;
  18670. DUK_TVAL_SET_BOOLEAN_FALSE(tv_slot);
  18671. }
  18672. /* normalize NaN which may not match our canonical internal NaN */
  18673. DUK_EXTERNAL void duk_push_number(duk_context *ctx, duk_double_t val) {
  18674. duk_hthread *thr;
  18675. duk_tval *tv_slot;
  18676. duk_double_union du;
  18677. DUK_ASSERT_CTX_VALID(ctx);
  18678. thr = (duk_hthread *) ctx;
  18679. DUK__CHECK_SPACE();
  18680. du.d = val;
  18681. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  18682. tv_slot = thr->valstack_top++;
  18683. DUK_TVAL_SET_NUMBER(tv_slot, du.d);
  18684. }
  18685. DUK_EXTERNAL void duk_push_int(duk_context *ctx, duk_int_t val) {
  18686. #if defined(DUK_USE_FASTINT)
  18687. duk_hthread *thr;
  18688. duk_tval *tv_slot;
  18689. DUK_ASSERT_CTX_VALID(ctx);
  18690. thr = (duk_hthread *) ctx;
  18691. DUK__CHECK_SPACE();
  18692. tv_slot = thr->valstack_top++;
  18693. #if DUK_INT_MAX <= 0x7fffffffL
  18694. DUK_TVAL_SET_FASTINT_I32(tv_slot, (duk_int32_t) val);
  18695. #else
  18696. if (val >= DUK_FASTINT_MIN && val <= DUK_FASTINT_MAX) {
  18697. DUK_TVAL_SET_FASTINT(tv_slot, (duk_int64_t) val);
  18698. } else {
  18699. duk_double_t = (duk_double_t) val;
  18700. DUK_TVAL_SET_NUMBER(tv_slot, d);
  18701. }
  18702. #endif
  18703. #else /* DUK_USE_FASTINT */
  18704. duk_hthread *thr;
  18705. duk_tval *tv_slot;
  18706. duk_double_t d;
  18707. DUK_ASSERT_CTX_VALID(ctx);
  18708. thr = (duk_hthread *) ctx;
  18709. DUK__CHECK_SPACE();
  18710. d = (duk_double_t) val;
  18711. tv_slot = thr->valstack_top++;
  18712. DUK_TVAL_SET_NUMBER(tv_slot, d);
  18713. #endif /* DUK_USE_FASTINT */
  18714. }
  18715. DUK_EXTERNAL void duk_push_uint(duk_context *ctx, duk_uint_t val) {
  18716. #if defined(DUK_USE_FASTINT)
  18717. duk_hthread *thr;
  18718. duk_tval *tv_slot;
  18719. DUK_ASSERT_CTX_VALID(ctx);
  18720. thr = (duk_hthread *) ctx;
  18721. DUK__CHECK_SPACE();
  18722. tv_slot = thr->valstack_top++;
  18723. #if DUK_UINT_MAX <= 0xffffffffUL
  18724. DUK_TVAL_SET_FASTINT_U32(tv_slot, (duk_uint32_t) val);
  18725. #else
  18726. if (val <= DUK_FASTINT_MAX) { /* val is unsigned so >= 0 */
  18727. /* XXX: take advantage of val being unsigned, no need to mask */
  18728. DUK_TVAL_SET_FASTINT(tv_slot, (duk_int64_t) val);
  18729. } else {
  18730. duk_double_t = (duk_double_t) val;
  18731. DUK_TVAL_SET_NUMBER(tv_slot, d);
  18732. }
  18733. #endif
  18734. #else /* DUK_USE_FASTINT */
  18735. duk_hthread *thr;
  18736. duk_tval *tv_slot;
  18737. duk_double_t d;
  18738. DUK_ASSERT_CTX_VALID(ctx);
  18739. thr = (duk_hthread *) ctx;
  18740. DUK__CHECK_SPACE();
  18741. d = (duk_double_t) val;
  18742. tv_slot = thr->valstack_top++;
  18743. DUK_TVAL_SET_NUMBER(tv_slot, d);
  18744. #endif /* DUK_USE_FASTINT */
  18745. }
  18746. DUK_EXTERNAL void duk_push_nan(duk_context *ctx) {
  18747. duk_hthread *thr;
  18748. duk_tval *tv_slot;
  18749. duk_double_union du;
  18750. DUK_ASSERT_CTX_VALID(ctx);
  18751. thr = (duk_hthread *) ctx;
  18752. DUK__CHECK_SPACE();
  18753. DUK_DBLUNION_SET_NAN(&du);
  18754. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  18755. tv_slot = thr->valstack_top++;
  18756. DUK_TVAL_SET_NUMBER(tv_slot, du.d);
  18757. }
  18758. DUK_EXTERNAL const char *duk_push_lstring(duk_context *ctx, const char *str, duk_size_t len) {
  18759. duk_hthread *thr = (duk_hthread *) ctx;
  18760. duk_hstring *h;
  18761. duk_tval *tv_slot;
  18762. DUK_ASSERT_CTX_VALID(ctx);
  18763. /* check stack before interning (avoid hanging temp) */
  18764. if (thr->valstack_top >= thr->valstack_end) {
  18765. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  18766. }
  18767. /* NULL with zero length represents an empty string; NULL with higher
  18768. * length is also now trated like an empty string although it is
  18769. * a bit dubious. This is unlike duk_push_string() which pushes a
  18770. * 'null' if the input string is a NULL.
  18771. */
  18772. if (!str) {
  18773. len = 0;
  18774. }
  18775. /* Check for maximum string length */
  18776. if (len > DUK_HSTRING_MAX_BYTELEN) {
  18777. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_STRING_TOO_LONG);
  18778. }
  18779. h = duk_heap_string_intern_checked(thr, (const duk_uint8_t *) str, (duk_uint32_t) len);
  18780. DUK_ASSERT(h != NULL);
  18781. tv_slot = thr->valstack_top++;
  18782. DUK_TVAL_SET_STRING(tv_slot, h);
  18783. DUK_HSTRING_INCREF(thr, h); /* no side effects */
  18784. return (const char *) DUK_HSTRING_GET_DATA(h);
  18785. }
  18786. DUK_EXTERNAL const char *duk_push_string(duk_context *ctx, const char *str) {
  18787. DUK_ASSERT_CTX_VALID(ctx);
  18788. if (str) {
  18789. return duk_push_lstring(ctx, str, DUK_STRLEN(str));
  18790. } else {
  18791. duk_push_null(ctx);
  18792. return NULL;
  18793. }
  18794. }
  18795. #ifdef DUK_USE_FILE_IO
  18796. /* This is a bit clunky because it is ANSI C portable. Should perhaps
  18797. * relocate to another file because this is potentially platform
  18798. * dependent.
  18799. */
  18800. DUK_EXTERNAL const char *duk_push_string_file_raw(duk_context *ctx, const char *path, duk_uint_t flags) {
  18801. duk_hthread *thr = (duk_hthread *) ctx;
  18802. duk_file *f = NULL;
  18803. char *buf;
  18804. long sz; /* ANSI C typing */
  18805. DUK_ASSERT_CTX_VALID(ctx);
  18806. if (!path) {
  18807. goto fail;
  18808. }
  18809. f = DUK_FOPEN(path, "rb");
  18810. if (!f) {
  18811. goto fail;
  18812. }
  18813. if (DUK_FSEEK(f, 0, SEEK_END) < 0) {
  18814. goto fail;
  18815. }
  18816. sz = DUK_FTELL(f);
  18817. if (sz < 0) {
  18818. goto fail;
  18819. }
  18820. if (DUK_FSEEK(f, 0, SEEK_SET) < 0) {
  18821. goto fail;
  18822. }
  18823. buf = (char *) duk_push_fixed_buffer(ctx, (duk_size_t) sz);
  18824. DUK_ASSERT(buf != NULL);
  18825. if ((duk_size_t) DUK_FREAD(buf, 1, (size_t) sz, f) != (duk_size_t) sz) {
  18826. goto fail;
  18827. }
  18828. (void) DUK_FCLOSE(f); /* ignore fclose() error */
  18829. f = NULL;
  18830. return duk_to_string(ctx, -1);
  18831. fail:
  18832. if (f) {
  18833. DUK_FCLOSE(f);
  18834. }
  18835. if (flags != 0) {
  18836. DUK_ASSERT(flags == DUK_STRING_PUSH_SAFE); /* only flag now */
  18837. duk_push_undefined(ctx);
  18838. } else {
  18839. /* XXX: string not shared because it is conditional */
  18840. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "read file error");
  18841. }
  18842. return NULL;
  18843. }
  18844. #else
  18845. DUK_EXTERNAL const char *duk_push_string_file_raw(duk_context *ctx, const char *path, duk_uint_t flags) {
  18846. duk_hthread *thr = (duk_hthread *) ctx;
  18847. DUK_ASSERT_CTX_VALID(ctx);
  18848. DUK_UNREF(path);
  18849. if (flags != 0) {
  18850. DUK_ASSERT(flags == DUK_STRING_PUSH_SAFE); /* only flag now */
  18851. duk_push_undefined(ctx);
  18852. } else {
  18853. /* XXX: string not shared because it is conditional */
  18854. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "file I/O disabled");
  18855. }
  18856. return NULL;
  18857. }
  18858. #endif /* DUK_USE_FILE_IO */
  18859. DUK_EXTERNAL void duk_push_pointer(duk_context *ctx, void *val) {
  18860. duk_hthread *thr;
  18861. duk_tval *tv_slot;
  18862. DUK_ASSERT_CTX_VALID(ctx);
  18863. thr = (duk_hthread *) ctx;
  18864. DUK__CHECK_SPACE();
  18865. tv_slot = thr->valstack_top++;
  18866. DUK_TVAL_SET_POINTER(tv_slot, val);
  18867. }
  18868. DUK_LOCAL void duk__push_this_helper(duk_context *ctx, duk_small_uint_t check_object_coercible) {
  18869. duk_hthread *thr;
  18870. duk_tval *tv_slot;
  18871. DUK_ASSERT_CTX_VALID(ctx);
  18872. DUK_ASSERT_DISABLE(thr->callstack_top >= 0); /* avoid warning (unsigned) */
  18873. thr = (duk_hthread *) ctx;
  18874. DUK_ASSERT(thr->callstack_top <= thr->callstack_size);
  18875. DUK__CHECK_SPACE();
  18876. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top)); /* because of valstack init policy */
  18877. tv_slot = thr->valstack_top++;
  18878. if (DUK_UNLIKELY(thr->callstack_top == 0)) {
  18879. if (check_object_coercible) {
  18880. goto type_error;
  18881. }
  18882. /* 'undefined' already on stack top */
  18883. } else {
  18884. duk_tval *tv;
  18885. /* 'this' binding is just before current activation's bottom */
  18886. DUK_ASSERT(thr->valstack_bottom > thr->valstack);
  18887. tv = thr->valstack_bottom - 1;
  18888. if (check_object_coercible &&
  18889. (DUK_TVAL_IS_UNDEFINED(tv) || DUK_TVAL_IS_NULL(tv))) {
  18890. /* XXX: better macro for DUK_TVAL_IS_UNDEFINED_OR_NULL(tv) */
  18891. goto type_error;
  18892. }
  18893. DUK_TVAL_SET_TVAL(tv_slot, tv);
  18894. DUK_TVAL_INCREF(thr, tv);
  18895. }
  18896. return;
  18897. type_error:
  18898. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_OBJECT_COERCIBLE);
  18899. }
  18900. DUK_EXTERNAL void duk_push_this(duk_context *ctx) {
  18901. DUK_ASSERT_CTX_VALID(ctx);
  18902. duk__push_this_helper(ctx, 0 /*check_object_coercible*/);
  18903. }
  18904. DUK_INTERNAL void duk_push_this_check_object_coercible(duk_context *ctx) {
  18905. DUK_ASSERT_CTX_VALID(ctx);
  18906. duk__push_this_helper(ctx, 1 /*check_object_coercible*/);
  18907. }
  18908. DUK_INTERNAL duk_hobject *duk_push_this_coercible_to_object(duk_context *ctx) {
  18909. duk_hobject *h;
  18910. DUK_ASSERT_CTX_VALID(ctx);
  18911. duk__push_this_helper(ctx, 1 /*check_object_coercible*/);
  18912. duk_to_object(ctx, -1);
  18913. h = duk_get_hobject(ctx, -1);
  18914. DUK_ASSERT(h != NULL);
  18915. return h;
  18916. }
  18917. DUK_INTERNAL duk_hstring *duk_push_this_coercible_to_string(duk_context *ctx) {
  18918. duk_hstring *h;
  18919. DUK_ASSERT_CTX_VALID(ctx);
  18920. duk__push_this_helper(ctx, 1 /*check_object_coercible*/);
  18921. duk_to_string(ctx, -1);
  18922. h = duk_get_hstring(ctx, -1);
  18923. DUK_ASSERT(h != NULL);
  18924. return h;
  18925. }
  18926. DUK_INTERNAL duk_tval *duk_get_borrowed_this_tval(duk_context *ctx) {
  18927. duk_hthread *thr;
  18928. DUK_ASSERT(ctx != NULL);
  18929. thr = (duk_hthread *) ctx;
  18930. DUK_ASSERT(thr->callstack_top > 0); /* caller required to know */
  18931. DUK_ASSERT(thr->valstack_bottom > thr->valstack); /* consequence of above */
  18932. DUK_ASSERT(thr->valstack_bottom - 1 >= thr->valstack); /* 'this' binding exists */
  18933. return thr->valstack_bottom - 1;
  18934. }
  18935. DUK_EXTERNAL void duk_push_current_function(duk_context *ctx) {
  18936. duk_hthread *thr = (duk_hthread *) ctx;
  18937. duk_activation *act;
  18938. DUK_ASSERT_CTX_VALID(ctx);
  18939. DUK_ASSERT(thr != NULL);
  18940. DUK_ASSERT_DISABLE(thr->callstack_top >= 0);
  18941. DUK_ASSERT(thr->callstack_top <= thr->callstack_size);
  18942. act = duk_hthread_get_current_activation(thr);
  18943. if (act) {
  18944. duk_push_tval(ctx, &act->tv_func);
  18945. } else {
  18946. duk_push_undefined(ctx);
  18947. }
  18948. }
  18949. DUK_EXTERNAL void duk_push_current_thread(duk_context *ctx) {
  18950. duk_hthread *thr = (duk_hthread *) ctx;
  18951. DUK_ASSERT_CTX_VALID(ctx);
  18952. DUK_ASSERT(thr != NULL);
  18953. if (thr->heap->curr_thread) {
  18954. duk_push_hobject(ctx, (duk_hobject *) thr->heap->curr_thread);
  18955. } else {
  18956. duk_push_undefined(ctx);
  18957. }
  18958. }
  18959. DUK_EXTERNAL void duk_push_global_object(duk_context *ctx) {
  18960. DUK_ASSERT_CTX_VALID(ctx);
  18961. duk_push_hobject_bidx(ctx, DUK_BIDX_GLOBAL);
  18962. }
  18963. /* XXX: size optimize */
  18964. DUK_LOCAL void duk__push_stash(duk_context *ctx) {
  18965. DUK_ASSERT_CTX_VALID(ctx);
  18966. if (!duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE)) {
  18967. DUK_DDD(DUK_DDDPRINT("creating heap/global/thread stash on first use"));
  18968. duk_pop(ctx);
  18969. duk_push_object_internal(ctx);
  18970. duk_dup_top(ctx);
  18971. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_C); /* [ ... parent stash stash ] -> [ ... parent stash ] */
  18972. }
  18973. duk_remove(ctx, -2);
  18974. }
  18975. DUK_EXTERNAL void duk_push_heap_stash(duk_context *ctx) {
  18976. duk_hthread *thr = (duk_hthread *) ctx;
  18977. duk_heap *heap;
  18978. DUK_ASSERT_CTX_VALID(ctx);
  18979. heap = thr->heap;
  18980. DUK_ASSERT(heap->heap_object != NULL);
  18981. duk_push_hobject(ctx, heap->heap_object);
  18982. duk__push_stash(ctx);
  18983. }
  18984. DUK_EXTERNAL void duk_push_global_stash(duk_context *ctx) {
  18985. DUK_ASSERT_CTX_VALID(ctx);
  18986. duk_push_global_object(ctx);
  18987. duk__push_stash(ctx);
  18988. }
  18989. DUK_EXTERNAL void duk_push_thread_stash(duk_context *ctx, duk_context *target_ctx) {
  18990. duk_hthread *thr = (duk_hthread *) ctx;
  18991. DUK_ASSERT_CTX_VALID(ctx);
  18992. if (!target_ctx) {
  18993. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  18994. return; /* not reached */
  18995. }
  18996. duk_push_hobject(ctx, (duk_hobject *) target_ctx);
  18997. duk__push_stash(ctx);
  18998. }
  18999. /* XXX: duk_ssize_t would be useful here */
  19000. DUK_LOCAL duk_int_t duk__try_push_vsprintf(duk_context *ctx, void *buf, duk_size_t sz, const char *fmt, va_list ap) {
  19001. duk_int_t len;
  19002. DUK_ASSERT_CTX_VALID(ctx);
  19003. DUK_UNREF(ctx);
  19004. /* NUL terminator handling doesn't matter here */
  19005. len = DUK_VSNPRINTF((char *) buf, sz, fmt, ap);
  19006. if (len < (duk_int_t) sz) {
  19007. /* Return value of 'sz' or more indicates output was (potentially)
  19008. * truncated.
  19009. */
  19010. return (duk_int_t) len;
  19011. }
  19012. return -1;
  19013. }
  19014. DUK_EXTERNAL const char *duk_push_vsprintf(duk_context *ctx, const char *fmt, va_list ap) {
  19015. duk_hthread *thr = (duk_hthread *) ctx;
  19016. duk_uint8_t stack_buf[DUK_PUSH_SPRINTF_INITIAL_SIZE];
  19017. duk_size_t sz = DUK_PUSH_SPRINTF_INITIAL_SIZE;
  19018. duk_bool_t pushed_buf = 0;
  19019. void *buf;
  19020. duk_int_t len; /* XXX: duk_ssize_t */
  19021. const char *res;
  19022. DUK_ASSERT_CTX_VALID(ctx);
  19023. /* special handling of fmt==NULL */
  19024. if (!fmt) {
  19025. duk_hstring *h_str;
  19026. duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING);
  19027. h_str = DUK_HTHREAD_STRING_EMPTY_STRING(thr); /* rely on interning, must be this string */
  19028. return (const char *) DUK_HSTRING_GET_DATA(h_str);
  19029. }
  19030. /* initial estimate based on format string */
  19031. sz = DUK_STRLEN(fmt) + 16; /* format plus something to avoid just missing */
  19032. if (sz < DUK_PUSH_SPRINTF_INITIAL_SIZE) {
  19033. sz = DUK_PUSH_SPRINTF_INITIAL_SIZE;
  19034. }
  19035. DUK_ASSERT(sz > 0);
  19036. /* Try to make do with a stack buffer to avoid allocating a temporary buffer.
  19037. * This works 99% of the time which is quite nice.
  19038. */
  19039. for (;;) {
  19040. va_list ap_copy; /* copied so that 'ap' can be reused */
  19041. if (sz <= sizeof(stack_buf)) {
  19042. buf = stack_buf;
  19043. } else if (!pushed_buf) {
  19044. pushed_buf = 1;
  19045. buf = duk_push_dynamic_buffer(ctx, sz);
  19046. } else {
  19047. buf = duk_resize_buffer(ctx, -1, sz);
  19048. }
  19049. DUK_ASSERT(buf != NULL);
  19050. DUK_VA_COPY(ap_copy, ap);
  19051. len = duk__try_push_vsprintf(ctx, buf, sz, fmt, ap_copy);
  19052. va_end(ap_copy);
  19053. if (len >= 0) {
  19054. break;
  19055. }
  19056. /* failed, resize and try again */
  19057. sz = sz * 2;
  19058. if (sz >= DUK_PUSH_SPRINTF_SANITY_LIMIT) {
  19059. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_SPRINTF_TOO_LONG);
  19060. }
  19061. }
  19062. /* Cannot use duk_to_string() on the buffer because it is usually
  19063. * larger than 'len'. Also, 'buf' is usually a stack buffer.
  19064. */
  19065. res = duk_push_lstring(ctx, (const char *) buf, (duk_size_t) len); /* [ buf? res ] */
  19066. if (pushed_buf) {
  19067. duk_remove(ctx, -2);
  19068. }
  19069. return res;
  19070. }
  19071. DUK_EXTERNAL const char *duk_push_sprintf(duk_context *ctx, const char *fmt, ...) {
  19072. va_list ap;
  19073. const char *ret;
  19074. DUK_ASSERT_CTX_VALID(ctx);
  19075. /* allow fmt==NULL */
  19076. va_start(ap, fmt);
  19077. ret = duk_push_vsprintf(ctx, fmt, ap);
  19078. va_end(ap);
  19079. return ret;
  19080. }
  19081. DUK_INTERNAL duk_idx_t duk_push_object_helper(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx) {
  19082. duk_hthread *thr = (duk_hthread *) ctx;
  19083. duk_tval *tv_slot;
  19084. duk_hobject *h;
  19085. duk_idx_t ret;
  19086. DUK_ASSERT_CTX_VALID(ctx);
  19087. DUK_ASSERT(prototype_bidx == -1 ||
  19088. (prototype_bidx >= 0 && prototype_bidx < DUK_NUM_BUILTINS));
  19089. /* check stack first */
  19090. if (thr->valstack_top >= thr->valstack_end) {
  19091. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  19092. }
  19093. h = duk_hobject_alloc(thr->heap, hobject_flags_and_class);
  19094. if (!h) {
  19095. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_ALLOC_FAILED);
  19096. }
  19097. DUK_DDD(DUK_DDDPRINT("created object with flags: 0x%08lx", (unsigned long) h->hdr.h_flags));
  19098. tv_slot = thr->valstack_top;
  19099. DUK_TVAL_SET_OBJECT(tv_slot, h);
  19100. DUK_HOBJECT_INCREF(thr, h); /* no side effects */
  19101. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  19102. thr->valstack_top++;
  19103. /* object is now reachable */
  19104. if (prototype_bidx >= 0) {
  19105. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, thr->builtins[prototype_bidx]);
  19106. } else {
  19107. DUK_ASSERT(prototype_bidx == -1);
  19108. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h) == NULL);
  19109. }
  19110. return ret;
  19111. }
  19112. DUK_INTERNAL duk_idx_t duk_push_object_helper_proto(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_hobject *proto) {
  19113. duk_hthread *thr = (duk_hthread *) ctx;
  19114. duk_idx_t ret;
  19115. duk_hobject *h;
  19116. DUK_ASSERT_CTX_VALID(ctx);
  19117. ret = duk_push_object_helper(ctx, hobject_flags_and_class, -1);
  19118. h = duk_get_hobject(ctx, -1);
  19119. DUK_ASSERT(h != NULL);
  19120. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h) == NULL);
  19121. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, proto);
  19122. return ret;
  19123. }
  19124. DUK_EXTERNAL duk_idx_t duk_push_object(duk_context *ctx) {
  19125. DUK_ASSERT_CTX_VALID(ctx);
  19126. return duk_push_object_helper(ctx,
  19127. DUK_HOBJECT_FLAG_EXTENSIBLE |
  19128. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  19129. DUK_BIDX_OBJECT_PROTOTYPE);
  19130. }
  19131. DUK_EXTERNAL duk_idx_t duk_push_array(duk_context *ctx) {
  19132. duk_hthread *thr = (duk_hthread *) ctx;
  19133. duk_hobject *obj;
  19134. duk_idx_t ret;
  19135. DUK_ASSERT_CTX_VALID(ctx);
  19136. ret = duk_push_object_helper(ctx,
  19137. DUK_HOBJECT_FLAG_EXTENSIBLE |
  19138. DUK_HOBJECT_FLAG_ARRAY_PART |
  19139. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAY),
  19140. DUK_BIDX_ARRAY_PROTOTYPE);
  19141. obj = duk_require_hobject(ctx, ret);
  19142. /*
  19143. * An array must have a 'length' property (E5 Section 15.4.5.2).
  19144. * The special array behavior flag must only be enabled once the
  19145. * length property has been added.
  19146. *
  19147. * The internal property must be a number (and preferably a
  19148. * fastint if fastint support is enabled).
  19149. */
  19150. duk_push_int(ctx, 0);
  19151. #if defined(DUK_USE_FASTINT)
  19152. DUK_ASSERT(DUK_TVAL_IS_FASTINT(duk_require_tval(ctx, -1)));
  19153. #endif
  19154. duk_hobject_define_property_internal(thr,
  19155. obj,
  19156. DUK_HTHREAD_STRING_LENGTH(thr),
  19157. DUK_PROPDESC_FLAGS_W);
  19158. DUK_HOBJECT_SET_EXOTIC_ARRAY(obj);
  19159. return ret;
  19160. }
  19161. DUK_EXTERNAL duk_idx_t duk_push_thread_raw(duk_context *ctx, duk_uint_t flags) {
  19162. duk_hthread *thr = (duk_hthread *) ctx;
  19163. duk_hthread *obj;
  19164. duk_idx_t ret;
  19165. duk_tval *tv_slot;
  19166. DUK_ASSERT_CTX_VALID(ctx);
  19167. /* check stack first */
  19168. if (thr->valstack_top >= thr->valstack_end) {
  19169. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  19170. }
  19171. obj = duk_hthread_alloc(thr->heap,
  19172. DUK_HOBJECT_FLAG_EXTENSIBLE |
  19173. DUK_HOBJECT_FLAG_THREAD |
  19174. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_THREAD));
  19175. if (!obj) {
  19176. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_ALLOC_FAILED);
  19177. }
  19178. obj->state = DUK_HTHREAD_STATE_INACTIVE;
  19179. #if defined(DUK_USE_HEAPPTR16)
  19180. obj->strs16 = thr->strs16;
  19181. #else
  19182. obj->strs = thr->strs;
  19183. #endif
  19184. DUK_DDD(DUK_DDDPRINT("created thread object with flags: 0x%08lx", (unsigned long) obj->obj.hdr.h_flags));
  19185. /* make the new thread reachable */
  19186. tv_slot = thr->valstack_top;
  19187. DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
  19188. DUK_HTHREAD_INCREF(thr, obj);
  19189. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  19190. thr->valstack_top++;
  19191. /* important to do this *after* pushing, to make the thread reachable for gc */
  19192. if (!duk_hthread_init_stacks(thr->heap, obj)) {
  19193. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_ALLOC_FAILED);
  19194. }
  19195. /* initialize built-ins - either by copying or creating new ones */
  19196. if (flags & DUK_THREAD_NEW_GLOBAL_ENV) {
  19197. duk_hthread_create_builtin_objects(obj);
  19198. } else {
  19199. duk_hthread_copy_builtin_objects(thr, obj);
  19200. }
  19201. /* default prototype (Note: 'obj' must be reachable) */
  19202. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, obj->builtins[DUK_BIDX_THREAD_PROTOTYPE]);
  19203. /* Initial stack size satisfies the stack spare constraints so there
  19204. * is no need to require stack here.
  19205. */
  19206. DUK_ASSERT(DUK_VALSTACK_INITIAL_SIZE >=
  19207. DUK_VALSTACK_API_ENTRY_MINIMUM + DUK_VALSTACK_INTERNAL_EXTRA);
  19208. return ret;
  19209. }
  19210. DUK_INTERNAL duk_idx_t duk_push_compiledfunction(duk_context *ctx) {
  19211. duk_hthread *thr = (duk_hthread *) ctx;
  19212. duk_hcompiledfunction *obj;
  19213. duk_idx_t ret;
  19214. duk_tval *tv_slot;
  19215. DUK_ASSERT_CTX_VALID(ctx);
  19216. /* check stack first */
  19217. if (thr->valstack_top >= thr->valstack_end) {
  19218. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  19219. }
  19220. /* Template functions are not strictly constructable (they don't
  19221. * have a "prototype" property for instance), so leave the
  19222. * DUK_HOBJECT_FLAG_CONSRUCTABLE flag cleared here.
  19223. */
  19224. obj = duk_hcompiledfunction_alloc(thr->heap,
  19225. DUK_HOBJECT_FLAG_EXTENSIBLE |
  19226. DUK_HOBJECT_FLAG_COMPILEDFUNCTION |
  19227. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION));
  19228. if (!obj) {
  19229. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_ALLOC_FAILED);
  19230. }
  19231. DUK_DDD(DUK_DDDPRINT("created compiled function object with flags: 0x%08lx", (unsigned long) obj->obj.hdr.h_flags));
  19232. tv_slot = thr->valstack_top;
  19233. DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
  19234. DUK_HOBJECT_INCREF(thr, obj);
  19235. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  19236. thr->valstack_top++;
  19237. /* default prototype (Note: 'obj' must be reachable) */
  19238. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  19239. return ret;
  19240. }
  19241. DUK_LOCAL duk_idx_t duk__push_c_function_raw(duk_context *ctx, duk_c_function func, duk_idx_t nargs, duk_uint_t flags) {
  19242. duk_hthread *thr = (duk_hthread *) ctx;
  19243. duk_hnativefunction *obj;
  19244. duk_idx_t ret;
  19245. duk_tval *tv_slot;
  19246. duk_uint16_t func_nargs;
  19247. DUK_ASSERT_CTX_VALID(ctx);
  19248. /* check stack first */
  19249. if (thr->valstack_top >= thr->valstack_end) {
  19250. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  19251. }
  19252. if (func == NULL) {
  19253. goto api_error;
  19254. }
  19255. if (nargs >= 0 && nargs < DUK_HNATIVEFUNCTION_NARGS_MAX) {
  19256. func_nargs = (duk_uint16_t) nargs;
  19257. } else if (nargs == DUK_VARARGS) {
  19258. func_nargs = DUK_HNATIVEFUNCTION_NARGS_VARARGS;
  19259. } else {
  19260. goto api_error;
  19261. }
  19262. obj = duk_hnativefunction_alloc(thr->heap, flags);
  19263. if (!obj) {
  19264. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_ALLOC_FAILED);
  19265. }
  19266. obj->func = func;
  19267. obj->nargs = func_nargs;
  19268. DUK_DDD(DUK_DDDPRINT("created native function object with flags: 0x%08lx, nargs=%ld",
  19269. (unsigned long) obj->obj.hdr.h_flags, (long) obj->nargs));
  19270. tv_slot = thr->valstack_top;
  19271. DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
  19272. DUK_HOBJECT_INCREF(thr, obj);
  19273. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  19274. thr->valstack_top++;
  19275. /* default prototype (Note: 'obj' must be reachable) */
  19276. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  19277. return ret;
  19278. api_error:
  19279. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  19280. return 0; /* not reached */
  19281. }
  19282. DUK_EXTERNAL duk_idx_t duk_push_c_function(duk_context *ctx, duk_c_function func, duk_int_t nargs) {
  19283. duk_uint_t flags;
  19284. DUK_ASSERT_CTX_VALID(ctx);
  19285. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  19286. DUK_HOBJECT_FLAG_CONSTRUCTABLE |
  19287. DUK_HOBJECT_FLAG_NATIVEFUNCTION |
  19288. DUK_HOBJECT_FLAG_NEWENV |
  19289. DUK_HOBJECT_FLAG_STRICT |
  19290. DUK_HOBJECT_FLAG_NOTAIL |
  19291. DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC |
  19292. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION);
  19293. return duk__push_c_function_raw(ctx, func, nargs, flags);
  19294. }
  19295. DUK_INTERNAL void duk_push_c_function_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs) {
  19296. duk_uint_t flags;
  19297. DUK_ASSERT_CTX_VALID(ctx);
  19298. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  19299. DUK_HOBJECT_FLAG_CONSTRUCTABLE |
  19300. DUK_HOBJECT_FLAG_NATIVEFUNCTION |
  19301. DUK_HOBJECT_FLAG_NEWENV |
  19302. DUK_HOBJECT_FLAG_STRICT |
  19303. DUK_HOBJECT_FLAG_NOTAIL |
  19304. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION);
  19305. (void) duk__push_c_function_raw(ctx, func, nargs, flags);
  19306. }
  19307. DUK_INTERNAL void duk_push_c_function_noconstruct_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs) {
  19308. duk_uint_t flags;
  19309. DUK_ASSERT_CTX_VALID(ctx);
  19310. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  19311. DUK_HOBJECT_FLAG_NATIVEFUNCTION |
  19312. DUK_HOBJECT_FLAG_NEWENV |
  19313. DUK_HOBJECT_FLAG_STRICT |
  19314. DUK_HOBJECT_FLAG_NOTAIL |
  19315. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION);
  19316. (void) duk__push_c_function_raw(ctx, func, nargs, flags);
  19317. }
  19318. DUK_EXTERNAL duk_idx_t duk_push_c_lightfunc(duk_context *ctx, duk_c_function func, duk_idx_t nargs, duk_idx_t length, duk_int_t magic) {
  19319. duk_hthread *thr = (duk_hthread *) ctx;
  19320. duk_tval tv_tmp;
  19321. duk_small_uint_t lf_flags;
  19322. DUK_ASSERT_CTX_VALID(ctx);
  19323. /* check stack first */
  19324. if (thr->valstack_top >= thr->valstack_end) {
  19325. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  19326. }
  19327. if (nargs >= DUK_LFUNC_NARGS_MIN && nargs <= DUK_LFUNC_NARGS_MAX) {
  19328. /* as is */
  19329. } else if (nargs == DUK_VARARGS) {
  19330. nargs = DUK_LFUNC_NARGS_VARARGS;
  19331. } else {
  19332. goto api_error;
  19333. }
  19334. if (!(length >= DUK_LFUNC_LENGTH_MIN && length <= DUK_LFUNC_LENGTH_MAX)) {
  19335. goto api_error;
  19336. }
  19337. if (!(magic >= DUK_LFUNC_MAGIC_MIN && magic <= DUK_LFUNC_MAGIC_MAX)) {
  19338. goto api_error;
  19339. }
  19340. lf_flags = DUK_LFUNC_FLAGS_PACK(magic, length, nargs);
  19341. DUK_TVAL_SET_LIGHTFUNC(&tv_tmp, func, lf_flags);
  19342. duk_push_tval(ctx, &tv_tmp); /* XXX: direct valstack write */
  19343. DUK_ASSERT(thr->valstack_top != thr->valstack_bottom);
  19344. return ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1;
  19345. api_error:
  19346. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  19347. return 0; /* not reached */
  19348. }
  19349. DUK_INTERNAL duk_hbufferobject *duk_push_bufferobject_raw(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx) {
  19350. duk_hthread *thr = (duk_hthread *) ctx;
  19351. duk_hbufferobject *obj;
  19352. duk_tval *tv_slot;
  19353. DUK_ASSERT(ctx != NULL);
  19354. DUK_ASSERT(prototype_bidx >= 0);
  19355. /* check stack first */
  19356. if (thr->valstack_top >= thr->valstack_end) {
  19357. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  19358. }
  19359. obj = duk_hbufferobject_alloc(thr->heap, hobject_flags_and_class);
  19360. if (!obj) {
  19361. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_ALLOC_FAILED);
  19362. }
  19363. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, thr->builtins[prototype_bidx]);
  19364. DUK_ASSERT_HBUFFEROBJECT_VALID(obj);
  19365. tv_slot = thr->valstack_top;
  19366. DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
  19367. DUK_HOBJECT_INCREF(thr, obj);
  19368. thr->valstack_top++;
  19369. return obj;
  19370. }
  19371. /* XXX: There's quite a bit of overlap with buffer creation handling in
  19372. * duk_bi_buffer.c. Look for overlap and refactor.
  19373. */
  19374. #define DUK__PACK_ARGS(classnum,protobidx,elemtype,elemshift,isview) \
  19375. (((classnum) << 24) | ((protobidx) << 16) | ((elemtype) << 8) | ((elemshift) << 4) | (isview))
  19376. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  19377. static const duk_uint32_t duk__bufobj_flags_lookup[] = {
  19378. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_BUFFER, DUK_BIDX_BUFFER_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 0), /* DUK_BUFOBJ_DUKTAPE_BUFFER */
  19379. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_BUFFER, DUK_BIDX_NODEJS_BUFFER_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 0), /* DUK_BUFOBJ_NODEJS_BUFFER */
  19380. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_ARRAYBUFFER, DUK_BIDX_ARRAYBUFFER_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 0), /* DUK_BUFOBJ_ARRAYBUFFER */
  19381. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_DATAVIEW, DUK_BIDX_DATAVIEW_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 1), /* DUK_BUFOBJ_DATAVIEW */
  19382. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_INT8ARRAY, DUK_BIDX_INT8ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_INT8, 0, 1), /* DUK_BUFOBJ_INT8ARRAY */
  19383. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT8ARRAY, DUK_BIDX_UINT8ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 1), /* DUK_BUFOBJ_UINT8ARRAY */
  19384. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY, DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED, 0, 1), /* DUK_BUFOBJ_UINT8CLAMPEDARRAY */
  19385. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_INT16ARRAY, DUK_BIDX_INT16ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_INT16, 1, 1), /* DUK_BUFOBJ_INT16ARRAY */
  19386. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT16ARRAY, DUK_BIDX_UINT16ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT16, 1, 1), /* DUK_BUFOBJ_UINT16ARRAY */
  19387. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_INT32ARRAY, DUK_BIDX_INT32ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_INT32, 2, 1), /* DUK_BUFOBJ_INT32ARRAY */
  19388. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT32ARRAY, DUK_BIDX_UINT32ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT32, 2, 1), /* DUK_BUFOBJ_UINT32ARRAY */
  19389. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_FLOAT32ARRAY, DUK_BIDX_FLOAT32ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_FLOAT32, 2, 1), /* DUK_BUFOBJ_FLOAT32ARRAY */
  19390. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_FLOAT64ARRAY, DUK_BIDX_FLOAT64ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_FLOAT64, 3, 1) /* DUK_BUFOBJ_FLOAT64ARRAY */
  19391. };
  19392. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  19393. /* Only allow Duktape.Buffer when support disabled. */
  19394. static const duk_uint32_t duk__bufobj_flags_lookup[] = {
  19395. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_BUFFER, DUK_BIDX_BUFFER_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 0) /* DUK_BUFOBJ_DUKTAPE_BUFFER */
  19396. };
  19397. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  19398. #undef DUK__PACK_ARGS
  19399. DUK_EXTERNAL void duk_push_buffer_object(duk_context *ctx, duk_idx_t idx_buffer, duk_size_t byte_offset, duk_size_t byte_length, duk_uint_t flags) {
  19400. duk_hthread *thr;
  19401. duk_hbufferobject *h_bufobj;
  19402. duk_hbuffer *h_val;
  19403. duk_uint32_t tmp;
  19404. duk_uint_t classnum;
  19405. duk_uint_t protobidx;
  19406. duk_uint_t lookupidx;
  19407. duk_uint_t uint_offset, uint_length, uint_added;
  19408. DUK_ASSERT_CTX_VALID(ctx);
  19409. thr = (duk_hthread *) ctx;
  19410. DUK_UNREF(thr);
  19411. /* The underlying types for offset/length in duk_hbufferobject is
  19412. * duk_uint_t; make sure argument values fit and that offset + length
  19413. * does not wrap.
  19414. */
  19415. uint_offset = (duk_uint_t) byte_offset;
  19416. uint_length = (duk_uint_t) byte_length;
  19417. if (sizeof(duk_size_t) != sizeof(duk_uint_t)) {
  19418. if ((duk_size_t) uint_offset != byte_offset || (duk_size_t) uint_length != byte_length) {
  19419. goto range_error;
  19420. }
  19421. }
  19422. uint_added = uint_offset + uint_length;
  19423. if (uint_added < uint_offset) {
  19424. goto range_error;
  19425. }
  19426. DUK_ASSERT(uint_added >= uint_offset && uint_added >= uint_length);
  19427. DUK_ASSERT_DISABLE(flags >= 0); /* flags is unsigned */
  19428. lookupidx = flags & 0x0f; /* 4 low bits */
  19429. if (lookupidx >= sizeof(duk__bufobj_flags_lookup) / sizeof(duk_uint32_t)) {
  19430. goto arg_error;
  19431. }
  19432. tmp = duk__bufobj_flags_lookup[lookupidx];
  19433. classnum = tmp >> 24;
  19434. protobidx = (tmp >> 16) & 0xff;
  19435. h_val = duk_require_hbuffer(ctx, idx_buffer);
  19436. DUK_ASSERT(h_val != NULL);
  19437. h_bufobj = duk_push_bufferobject_raw(ctx,
  19438. DUK_HOBJECT_FLAG_EXTENSIBLE |
  19439. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  19440. DUK_HOBJECT_CLASS_AS_FLAGS(classnum),
  19441. protobidx);
  19442. DUK_ASSERT(h_bufobj != NULL);
  19443. h_bufobj->buf = h_val;
  19444. DUK_HBUFFER_INCREF(thr, h_val);
  19445. h_bufobj->offset = uint_offset;
  19446. h_bufobj->length = uint_length;
  19447. h_bufobj->shift = (tmp >> 4) & 0x0f;
  19448. h_bufobj->elem_type = (tmp >> 8) & 0xff;
  19449. h_bufobj->is_view = tmp & 0x0f;
  19450. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  19451. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  19452. /* TypedArray views need an automatic ArrayBuffer which must be
  19453. * provided as .buffer property of the view. Just create a new
  19454. * ArrayBuffer sharing the same underlying buffer.
  19455. */
  19456. if (flags & DUK_BUFOBJ_CREATE_ARRBUF) {
  19457. h_bufobj = duk_push_bufferobject_raw(ctx,
  19458. DUK_HOBJECT_FLAG_EXTENSIBLE |
  19459. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  19460. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAYBUFFER),
  19461. DUK_BIDX_ARRAYBUFFER_PROTOTYPE);
  19462. DUK_ASSERT(h_bufobj != NULL);
  19463. h_bufobj->buf = h_val;
  19464. DUK_HBUFFER_INCREF(thr, h_val);
  19465. h_bufobj->offset = uint_offset;
  19466. h_bufobj->length = uint_length;
  19467. DUK_ASSERT(h_bufobj->shift == 0);
  19468. h_bufobj->elem_type = DUK_HBUFFEROBJECT_ELEM_UINT8;
  19469. DUK_ASSERT(h_bufobj->is_view == 0);
  19470. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  19471. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE);
  19472. duk_compact(ctx, -1);
  19473. }
  19474. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  19475. return;
  19476. range_error:
  19477. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_INVALID_CALL_ARGS);
  19478. return; /* not reached */
  19479. arg_error:
  19480. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_CALL_ARGS);
  19481. return; /* not reached */
  19482. }
  19483. DUK_EXTERNAL duk_idx_t duk_push_error_object_va_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, va_list ap) {
  19484. duk_hthread *thr = (duk_hthread *) ctx;
  19485. duk_idx_t ret;
  19486. duk_hobject *proto;
  19487. #ifdef DUK_USE_AUGMENT_ERROR_CREATE
  19488. duk_bool_t noblame_fileline;
  19489. #endif
  19490. DUK_ASSERT_CTX_VALID(ctx);
  19491. DUK_ASSERT(thr != NULL);
  19492. DUK_UNREF(filename);
  19493. DUK_UNREF(line);
  19494. /* Error code also packs a tracedata related flag. */
  19495. #ifdef DUK_USE_AUGMENT_ERROR_CREATE
  19496. noblame_fileline = err_code & DUK_ERRCODE_FLAG_NOBLAME_FILELINE;
  19497. #endif
  19498. err_code = err_code & (~DUK_ERRCODE_FLAG_NOBLAME_FILELINE);
  19499. /* error gets its 'name' from the prototype */
  19500. proto = duk_error_prototype_from_code(thr, err_code);
  19501. ret = duk_push_object_helper_proto(ctx,
  19502. DUK_HOBJECT_FLAG_EXTENSIBLE |
  19503. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ERROR),
  19504. proto);
  19505. /* ... and its 'message' from an instance property */
  19506. if (fmt) {
  19507. duk_push_vsprintf(ctx, fmt, ap);
  19508. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC);
  19509. } else {
  19510. /* If no explicit message given, put error code into message field
  19511. * (as a number). This is not fully in keeping with the Ecmascript
  19512. * error model because messages are supposed to be strings (Error
  19513. * constructors use ToString() on their argument). However, it's
  19514. * probably more useful than having a separate 'code' property.
  19515. */
  19516. duk_push_int(ctx, err_code);
  19517. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC);
  19518. }
  19519. #if 0
  19520. /* Disabled for now, not sure this is a useful property */
  19521. duk_push_int(ctx, err_code);
  19522. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_CODE, DUK_PROPDESC_FLAGS_WC);
  19523. #endif
  19524. /* Creation time error augmentation */
  19525. #ifdef DUK_USE_AUGMENT_ERROR_CREATE
  19526. /* filename may be NULL in which case file/line is not recorded */
  19527. duk_err_augment_error_create(thr, thr, filename, line, noblame_fileline); /* may throw an error */
  19528. #endif
  19529. return ret;
  19530. }
  19531. DUK_EXTERNAL duk_idx_t duk_push_error_object_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, ...) {
  19532. va_list ap;
  19533. duk_idx_t ret;
  19534. DUK_ASSERT_CTX_VALID(ctx);
  19535. va_start(ap, fmt);
  19536. ret = duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap);
  19537. va_end(ap);
  19538. return ret;
  19539. }
  19540. #if !defined(DUK_USE_VARIADIC_MACROS)
  19541. DUK_EXTERNAL duk_idx_t duk_push_error_object_stash(duk_context *ctx, duk_errcode_t err_code, const char *fmt, ...) {
  19542. const char *filename = duk_api_global_filename;
  19543. duk_int_t line = duk_api_global_line;
  19544. va_list ap;
  19545. duk_idx_t ret;
  19546. DUK_ASSERT_CTX_VALID(ctx);
  19547. duk_api_global_filename = NULL;
  19548. duk_api_global_line = 0;
  19549. va_start(ap, fmt);
  19550. ret = duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap);
  19551. va_end(ap);
  19552. return ret;
  19553. }
  19554. #endif /* DUK_USE_VARIADIC_MACROS */
  19555. DUK_EXTERNAL void *duk_push_buffer_raw(duk_context *ctx, duk_size_t size, duk_small_uint_t flags) {
  19556. duk_hthread *thr = (duk_hthread *) ctx;
  19557. duk_tval *tv_slot;
  19558. duk_hbuffer *h;
  19559. void *buf_data;
  19560. DUK_ASSERT_CTX_VALID(ctx);
  19561. /* check stack first */
  19562. if (thr->valstack_top >= thr->valstack_end) {
  19563. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  19564. }
  19565. /* Check for maximum buffer length. */
  19566. if (size > DUK_HBUFFER_MAX_BYTELEN) {
  19567. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_BUFFER_TOO_LONG);
  19568. }
  19569. h = duk_hbuffer_alloc(thr->heap, size, flags, &buf_data);
  19570. if (!h) {
  19571. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_ALLOC_FAILED);
  19572. }
  19573. tv_slot = thr->valstack_top;
  19574. DUK_TVAL_SET_BUFFER(tv_slot, h);
  19575. DUK_HBUFFER_INCREF(thr, h);
  19576. thr->valstack_top++;
  19577. return (void *) buf_data;
  19578. }
  19579. DUK_EXTERNAL duk_idx_t duk_push_heapptr(duk_context *ctx, void *ptr) {
  19580. duk_hthread *thr = (duk_hthread *) ctx;
  19581. duk_idx_t ret;
  19582. DUK_ASSERT_CTX_VALID(ctx);
  19583. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  19584. if (ptr == NULL) {
  19585. goto push_undefined;
  19586. }
  19587. switch (DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) ptr)) {
  19588. case DUK_HTYPE_STRING:
  19589. duk_push_hstring(ctx, (duk_hstring *) ptr);
  19590. break;
  19591. case DUK_HTYPE_OBJECT:
  19592. duk_push_hobject(ctx, (duk_hobject *) ptr);
  19593. break;
  19594. case DUK_HTYPE_BUFFER:
  19595. duk_push_hbuffer(ctx, (duk_hbuffer *) ptr);
  19596. break;
  19597. default:
  19598. goto push_undefined;
  19599. }
  19600. return ret;
  19601. push_undefined:
  19602. duk_push_undefined(ctx);
  19603. return ret;
  19604. }
  19605. DUK_INTERNAL duk_idx_t duk_push_object_internal(duk_context *ctx) {
  19606. return duk_push_object_helper(ctx,
  19607. DUK_HOBJECT_FLAG_EXTENSIBLE |
  19608. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  19609. -1); /* no prototype */
  19610. }
  19611. DUK_INTERNAL void duk_push_hstring(duk_context *ctx, duk_hstring *h) {
  19612. duk_tval tv;
  19613. DUK_ASSERT_CTX_VALID(ctx);
  19614. DUK_ASSERT(h != NULL);
  19615. DUK_TVAL_SET_STRING(&tv, h);
  19616. duk_push_tval(ctx, &tv);
  19617. }
  19618. DUK_INTERNAL void duk_push_hstring_stridx(duk_context *ctx, duk_small_int_t stridx) {
  19619. duk_hthread *thr = (duk_hthread *) ctx;
  19620. DUK_ASSERT(stridx >= 0 && stridx < DUK_HEAP_NUM_STRINGS);
  19621. duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx));
  19622. }
  19623. DUK_INTERNAL void duk_push_hobject(duk_context *ctx, duk_hobject *h) {
  19624. duk_tval tv;
  19625. DUK_ASSERT_CTX_VALID(ctx);
  19626. DUK_ASSERT(h != NULL);
  19627. DUK_TVAL_SET_OBJECT(&tv, h);
  19628. duk_push_tval(ctx, &tv);
  19629. }
  19630. DUK_INTERNAL void duk_push_hbuffer(duk_context *ctx, duk_hbuffer *h) {
  19631. duk_tval tv;
  19632. DUK_ASSERT_CTX_VALID(ctx);
  19633. DUK_ASSERT(h != NULL);
  19634. DUK_TVAL_SET_BUFFER(&tv, h);
  19635. duk_push_tval(ctx, &tv);
  19636. }
  19637. DUK_INTERNAL void duk_push_hobject_bidx(duk_context *ctx, duk_small_int_t builtin_idx) {
  19638. duk_hthread *thr = (duk_hthread *) ctx;
  19639. DUK_ASSERT_CTX_VALID(ctx);
  19640. DUK_ASSERT(thr != NULL);
  19641. DUK_ASSERT(builtin_idx >= 0 && builtin_idx < DUK_NUM_BUILTINS);
  19642. DUK_ASSERT(thr->builtins[builtin_idx] != NULL);
  19643. duk_push_hobject(ctx, thr->builtins[builtin_idx]);
  19644. }
  19645. /*
  19646. * Poppers
  19647. */
  19648. DUK_EXTERNAL void duk_pop_n(duk_context *ctx, duk_idx_t count) {
  19649. duk_hthread *thr = (duk_hthread *) ctx;
  19650. duk_tval *tv;
  19651. DUK_ASSERT_CTX_VALID(ctx);
  19652. if (DUK_UNLIKELY(count < 0)) {
  19653. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_COUNT);
  19654. return;
  19655. }
  19656. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  19657. if (DUK_UNLIKELY((duk_size_t) (thr->valstack_top - thr->valstack_bottom) < (duk_size_t) count)) {
  19658. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_POP_TOO_MANY);
  19659. }
  19660. /*
  19661. * Must be very careful here, every DECREF may cause reallocation
  19662. * of our valstack.
  19663. */
  19664. /* XXX: inlined DECREF macro would be nice here: no NULL check,
  19665. * refzero queueing but no refzero algorithm run (= no pointer
  19666. * instability), inline code.
  19667. */
  19668. /* XXX: optimize loops */
  19669. #if defined(DUK_USE_REFERENCE_COUNTING)
  19670. while (count > 0) {
  19671. count--;
  19672. tv = --thr->valstack_top; /* tv points to element just below prev top */
  19673. DUK_ASSERT(tv >= thr->valstack_bottom);
  19674. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
  19675. }
  19676. #else
  19677. tv = thr->valstack_top;
  19678. while (count > 0) {
  19679. count--;
  19680. tv--;
  19681. DUK_ASSERT(tv >= thr->valstack_bottom);
  19682. DUK_TVAL_SET_UNDEFINED(tv);
  19683. }
  19684. thr->valstack_top = tv;
  19685. #endif
  19686. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  19687. }
  19688. /* Popping one element is called so often that when footprint is not an issue,
  19689. * compile a specialized function for it.
  19690. */
  19691. #if defined(DUK_USE_PREFER_SIZE)
  19692. DUK_EXTERNAL void duk_pop(duk_context *ctx) {
  19693. DUK_ASSERT_CTX_VALID(ctx);
  19694. duk_pop_n(ctx, 1);
  19695. }
  19696. #else
  19697. DUK_EXTERNAL void duk_pop(duk_context *ctx) {
  19698. duk_hthread *thr = (duk_hthread *) ctx;
  19699. duk_tval *tv;
  19700. DUK_ASSERT_CTX_VALID(ctx);
  19701. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  19702. if (DUK_UNLIKELY(thr->valstack_top == thr->valstack_bottom)) {
  19703. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_POP_TOO_MANY);
  19704. }
  19705. tv = --thr->valstack_top; /* tv points to element just below prev top */
  19706. DUK_ASSERT(tv >= thr->valstack_bottom);
  19707. #ifdef DUK_USE_REFERENCE_COUNTING
  19708. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
  19709. #else
  19710. DUK_TVAL_SET_UNDEFINED(tv);
  19711. #endif
  19712. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  19713. }
  19714. #endif /* !DUK_USE_PREFER_SIZE */
  19715. DUK_EXTERNAL void duk_pop_2(duk_context *ctx) {
  19716. DUK_ASSERT_CTX_VALID(ctx);
  19717. duk_pop_n(ctx, 2);
  19718. }
  19719. DUK_EXTERNAL void duk_pop_3(duk_context *ctx) {
  19720. DUK_ASSERT_CTX_VALID(ctx);
  19721. duk_pop_n(ctx, 3);
  19722. }
  19723. /*
  19724. * Error throwing
  19725. */
  19726. DUK_EXTERNAL void duk_throw(duk_context *ctx) {
  19727. duk_hthread *thr = (duk_hthread *) ctx;
  19728. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  19729. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  19730. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  19731. if (thr->valstack_top == thr->valstack_bottom) {
  19732. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  19733. }
  19734. /* Errors are augmented when they are created, not when they are
  19735. * thrown or re-thrown. The current error handler, however, runs
  19736. * just before an error is thrown.
  19737. */
  19738. /* Sync so that augmentation sees up-to-date activations, NULL
  19739. * thr->ptr_curr_pc so that it's not used if side effects occur
  19740. * in augmentation or longjmp handling.
  19741. */
  19742. duk_hthread_sync_and_null_currpc(thr);
  19743. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  19744. DUK_DDD(DUK_DDDPRINT("THROW ERROR (API): %!dT (before throw augment)", (duk_tval *) duk_get_tval(ctx, -1)));
  19745. duk_err_augment_error_throw(thr);
  19746. #endif
  19747. DUK_DDD(DUK_DDDPRINT("THROW ERROR (API): %!dT (after throw augment)", (duk_tval *) duk_get_tval(ctx, -1)));
  19748. duk_err_setup_heap_ljstate(thr, DUK_LJ_TYPE_THROW);
  19749. /* thr->heap->lj.jmpbuf_ptr is checked by duk_err_longjmp() so we don't
  19750. * need to check that here. If the value is NULL, a panic occurs because
  19751. * we can't return.
  19752. */
  19753. duk_err_longjmp(thr);
  19754. DUK_UNREACHABLE();
  19755. }
  19756. DUK_EXTERNAL void duk_fatal(duk_context *ctx, duk_errcode_t err_code, const char *err_msg) {
  19757. duk_hthread *thr = (duk_hthread *) ctx;
  19758. DUK_ASSERT_CTX_VALID(ctx);
  19759. DUK_ASSERT(thr != NULL);
  19760. DUK_ASSERT(thr->heap != NULL);
  19761. DUK_ASSERT(thr->heap->fatal_func != NULL);
  19762. DUK_D(DUK_DPRINT("fatal error occurred, code %ld, message %s",
  19763. (long) err_code, (const char *) err_msg));
  19764. /* fatal_func should be noreturn, but noreturn declarations on function
  19765. * pointers has a very spotty support apparently so it's not currently
  19766. * done.
  19767. */
  19768. thr->heap->fatal_func(ctx, err_code, err_msg);
  19769. DUK_PANIC(DUK_ERR_API_ERROR, "fatal handler returned");
  19770. }
  19771. DUK_EXTERNAL void duk_error_va_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, va_list ap) {
  19772. DUK_ASSERT_CTX_VALID(ctx);
  19773. duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap);
  19774. duk_throw(ctx);
  19775. }
  19776. DUK_EXTERNAL void duk_error_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, ...) {
  19777. va_list ap;
  19778. DUK_ASSERT_CTX_VALID(ctx);
  19779. va_start(ap, fmt);
  19780. duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap);
  19781. va_end(ap);
  19782. duk_throw(ctx);
  19783. }
  19784. #if !defined(DUK_USE_VARIADIC_MACROS)
  19785. DUK_EXTERNAL void duk_error_stash(duk_context *ctx, duk_errcode_t err_code, const char *fmt, ...) {
  19786. const char *filename;
  19787. duk_int_t line;
  19788. va_list ap;
  19789. DUK_ASSERT_CTX_VALID(ctx);
  19790. filename = duk_api_global_filename;
  19791. line = duk_api_global_line;
  19792. duk_api_global_filename = NULL;
  19793. duk_api_global_line = 0;
  19794. va_start(ap, fmt);
  19795. duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap);
  19796. va_end(ap);
  19797. duk_throw(ctx);
  19798. }
  19799. #endif /* DUK_USE_VARIADIC_MACROS */
  19800. /*
  19801. * Comparison
  19802. */
  19803. DUK_EXTERNAL duk_bool_t duk_equals(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) {
  19804. duk_hthread *thr = (duk_hthread *) ctx;
  19805. duk_tval *tv1, *tv2;
  19806. DUK_ASSERT_CTX_VALID(ctx);
  19807. tv1 = duk_get_tval(ctx, index1);
  19808. tv2 = duk_get_tval(ctx, index2);
  19809. if ((tv1 == NULL) || (tv2 == NULL)) {
  19810. return 0;
  19811. }
  19812. /* Coercion may be needed, the helper handles that by pushing the
  19813. * tagged values to the stack.
  19814. */
  19815. return duk_js_equals(thr, tv1, tv2);
  19816. }
  19817. DUK_EXTERNAL duk_bool_t duk_strict_equals(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) {
  19818. duk_tval *tv1, *tv2;
  19819. DUK_ASSERT_CTX_VALID(ctx);
  19820. tv1 = duk_get_tval(ctx, index1);
  19821. tv2 = duk_get_tval(ctx, index2);
  19822. if ((tv1 == NULL) || (tv2 == NULL)) {
  19823. return 0;
  19824. }
  19825. /* No coercions or other side effects, so safe */
  19826. return duk_js_strict_equals(tv1, tv2);
  19827. }
  19828. /*
  19829. * instanceof
  19830. */
  19831. DUK_EXTERNAL duk_bool_t duk_instanceof(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) {
  19832. duk_tval *tv1, *tv2;
  19833. DUK_ASSERT_CTX_VALID(ctx);
  19834. /* Index validation is strict, which differs from duk_equals().
  19835. * The strict behavior mimics how instanceof itself works, e.g.
  19836. * it is a TypeError if rval is not a -callable- object. It would
  19837. * be somewhat inconsistent if rval would be allowed to be
  19838. * non-existent without a TypeError.
  19839. */
  19840. tv1 = duk_require_tval(ctx, index1);
  19841. DUK_ASSERT(tv1 != NULL);
  19842. tv2 = duk_require_tval(ctx, index2);
  19843. DUK_ASSERT(tv2 != NULL);
  19844. return duk_js_instanceof((duk_hthread *) ctx, tv1, tv2);
  19845. }
  19846. /*
  19847. * Lightfunc
  19848. */
  19849. DUK_INTERNAL void duk_push_lightfunc_name(duk_context *ctx, duk_tval *tv) {
  19850. duk_c_function func;
  19851. DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv));
  19852. /* Lightfunc name, includes Duktape/C native function pointer, which
  19853. * can often be used to locate the function from a symbol table.
  19854. * The name also includes the 16-bit duk_tval flags field because it
  19855. * includes the magic value. Because a single native function often
  19856. * provides different functionality depending on the magic value, it
  19857. * seems reasonably to include it in the name.
  19858. *
  19859. * On the other hand, a complicated name increases string table
  19860. * pressure in low memory environments (but only when function name
  19861. * is accessed).
  19862. */
  19863. func = DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv);
  19864. duk_push_sprintf(ctx, "light_");
  19865. duk_push_string_funcptr(ctx, (duk_uint8_t *) &func, sizeof(func));
  19866. duk_push_sprintf(ctx, "_%04x", (unsigned int) DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv));
  19867. duk_concat(ctx, 3);
  19868. }
  19869. DUK_INTERNAL void duk_push_lightfunc_tostring(duk_context *ctx, duk_tval *tv) {
  19870. DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv));
  19871. duk_push_string(ctx, "function ");
  19872. duk_push_lightfunc_name(ctx, tv);
  19873. duk_push_string(ctx, "() {/* light */}");
  19874. duk_concat(ctx, 3);
  19875. }
  19876. /*
  19877. * Function pointers
  19878. *
  19879. * Printing function pointers is non-portable, so we do that by hex printing
  19880. * bytes from memory.
  19881. */
  19882. DUK_INTERNAL void duk_push_string_funcptr(duk_context *ctx, duk_uint8_t *ptr, duk_size_t sz) {
  19883. duk_uint8_t buf[32 * 2];
  19884. duk_uint8_t *p, *q;
  19885. duk_small_uint_t i;
  19886. duk_small_uint_t t;
  19887. DUK_ASSERT(sz <= 32); /* sanity limit for function pointer size */
  19888. p = buf;
  19889. #if defined(DUK_USE_INTEGER_LE)
  19890. q = ptr + sz;
  19891. #else
  19892. q = ptr;
  19893. #endif
  19894. for (i = 0; i < sz; i++) {
  19895. #if defined(DUK_USE_INTEGER_LE)
  19896. t = *(--q);
  19897. #else
  19898. t = *(q++);
  19899. #endif
  19900. *p++ = duk_lc_digits[t >> 4];
  19901. *p++ = duk_lc_digits[t & 0x0f];
  19902. }
  19903. duk_push_lstring(ctx, (const char *) buf, sz * 2);
  19904. }
  19905. /*
  19906. * Push readable string summarizing duk_tval. The operation is side effect
  19907. * free and will only throw from internal errors (e.g. out of memory).
  19908. * This is used by e.g. property access code to summarize a key/base safely,
  19909. * and is not intended to be fast (but small and safe).
  19910. */
  19911. #define DUK__READABLE_STRING_MAXCHARS 32
  19912. /* String sanitizer which escapes ASCII control characters and a few other
  19913. * ASCII characters, passes Unicode as is, and replaces invalid UTF-8 with
  19914. * question marks. No errors are thrown for any input string, except in out
  19915. * of memory situations.
  19916. */
  19917. DUK_LOCAL void duk__push_hstring_readable_unicode(duk_context *ctx, duk_hstring *h_input) {
  19918. duk_hthread *thr;
  19919. const duk_uint8_t *p, *p_start, *p_end;
  19920. duk_uint8_t buf[DUK_UNICODE_MAX_XUTF8_LENGTH * DUK__READABLE_STRING_MAXCHARS +
  19921. 2 /*quotes*/ + 3 /*periods*/];
  19922. duk_uint8_t *q;
  19923. duk_ucodepoint_t cp;
  19924. duk_small_uint_t nchars;
  19925. DUK_ASSERT_CTX_VALID(ctx);
  19926. DUK_ASSERT(h_input != NULL);
  19927. thr = (duk_hthread *) ctx;
  19928. p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
  19929. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  19930. p = p_start;
  19931. q = buf;
  19932. nchars = 0;
  19933. *q++ = (duk_uint8_t) DUK_ASC_SINGLEQUOTE;
  19934. for (;;) {
  19935. if (p >= p_end) {
  19936. break;
  19937. }
  19938. if (nchars == DUK__READABLE_STRING_MAXCHARS) {
  19939. *q++ = (duk_uint8_t) DUK_ASC_PERIOD;
  19940. *q++ = (duk_uint8_t) DUK_ASC_PERIOD;
  19941. *q++ = (duk_uint8_t) DUK_ASC_PERIOD;
  19942. break;
  19943. }
  19944. if (duk_unicode_decode_xutf8(thr, &p, p_start, p_end, &cp)) {
  19945. if (cp < 0x20 || cp == 0x7f || cp == DUK_ASC_SINGLEQUOTE || cp == DUK_ASC_BACKSLASH) {
  19946. DUK_ASSERT(DUK_UNICODE_MAX_XUTF8_LENGTH >= 4); /* estimate is valid */
  19947. DUK_ASSERT((cp >> 4) <= 0x0f);
  19948. *q++ = (duk_uint8_t) DUK_ASC_BACKSLASH;
  19949. *q++ = (duk_uint8_t) DUK_ASC_LC_X;
  19950. *q++ = (duk_uint8_t) duk_lc_digits[cp >> 4];
  19951. *q++ = (duk_uint8_t) duk_lc_digits[cp & 0x0f];
  19952. } else {
  19953. q += duk_unicode_encode_xutf8(cp, q);
  19954. }
  19955. } else {
  19956. p++; /* advance manually */
  19957. *q++ = (duk_uint8_t) DUK_ASC_QUESTION;
  19958. }
  19959. nchars++;
  19960. }
  19961. *q++ = (duk_uint8_t) DUK_ASC_SINGLEQUOTE;
  19962. duk_push_lstring(ctx, (const char *) buf, (duk_size_t) (q - buf));
  19963. }
  19964. DUK_INTERNAL const char *duk_push_string_tval_readable(duk_context *ctx, duk_tval *tv) {
  19965. duk_hthread *thr;
  19966. DUK_ASSERT_CTX_VALID(ctx);
  19967. thr = (duk_hthread *) ctx;
  19968. DUK_UNREF(thr);
  19969. switch (DUK_TVAL_GET_TAG(tv)) {
  19970. case DUK_TAG_STRING: {
  19971. duk__push_hstring_readable_unicode(ctx, DUK_TVAL_GET_STRING(tv));
  19972. break;
  19973. }
  19974. case DUK_TAG_OBJECT: {
  19975. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  19976. DUK_ASSERT(h != NULL);
  19977. duk_push_hobject_class_string(ctx, h);
  19978. break;
  19979. }
  19980. case DUK_TAG_BUFFER: {
  19981. /* XXX: Hex encoded, length limited buffer summary here? */
  19982. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  19983. DUK_ASSERT(h != NULL);
  19984. duk_push_sprintf(ctx, "[buffer:%ld]", (long) DUK_HBUFFER_GET_SIZE(h));
  19985. break;
  19986. }
  19987. default: {
  19988. duk_push_tval(ctx, tv);
  19989. break;
  19990. }
  19991. }
  19992. return duk_to_string(ctx, -1);
  19993. }
  19994. #undef DUK__CHECK_SPACE
  19995. #undef DUK__READABLE_STRING_MAXCHARS
  19996. #line 1 "duk_api_string.c"
  19997. /*
  19998. * String manipulation
  19999. */
  20000. /* include removed: duk_internal.h */
  20001. DUK_LOCAL void duk__concat_and_join_helper(duk_context *ctx, duk_idx_t count_in, duk_bool_t is_join) {
  20002. duk_hthread *thr = (duk_hthread *) ctx;
  20003. duk_uint_t count;
  20004. duk_uint_t i;
  20005. duk_size_t idx;
  20006. duk_size_t len;
  20007. duk_hstring *h;
  20008. duk_uint8_t *buf;
  20009. DUK_ASSERT_CTX_VALID(ctx);
  20010. if (DUK_UNLIKELY(count_in <= 0)) {
  20011. if (count_in < 0) {
  20012. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_COUNT);
  20013. return;
  20014. }
  20015. DUK_ASSERT(count_in == 0);
  20016. duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING);
  20017. return;
  20018. }
  20019. count = (duk_uint_t) count_in;
  20020. if (is_join) {
  20021. duk_size_t t1, t2, limit;
  20022. h = duk_to_hstring(ctx, -((duk_idx_t) count) - 1);
  20023. DUK_ASSERT(h != NULL);
  20024. /* A bit tricky overflow test, see doc/code-issues.rst. */
  20025. t1 = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h);
  20026. t2 = (duk_size_t) (count - 1);
  20027. limit = (duk_size_t) DUK_HSTRING_MAX_BYTELEN;
  20028. if (DUK_UNLIKELY(t2 != 0 && t1 > limit / t2)) {
  20029. /* Combined size of separators already overflows */
  20030. goto error_overflow;
  20031. }
  20032. len = (duk_size_t) (t1 * t2);
  20033. } else {
  20034. len = (duk_size_t) 0;
  20035. }
  20036. for (i = count; i >= 1; i--) {
  20037. duk_size_t new_len;
  20038. duk_to_string(ctx, -((duk_idx_t) i));
  20039. h = duk_require_hstring(ctx, -((duk_idx_t) i));
  20040. new_len = len + (duk_size_t) DUK_HSTRING_GET_BYTELEN(h);
  20041. /* Impose a string maximum length, need to handle overflow
  20042. * correctly.
  20043. */
  20044. if (new_len < len || /* wrapped */
  20045. new_len > (duk_size_t) DUK_HSTRING_MAX_BYTELEN) {
  20046. goto error_overflow;
  20047. }
  20048. len = new_len;
  20049. }
  20050. DUK_DDD(DUK_DDDPRINT("join/concat %lu strings, total length %lu bytes",
  20051. (unsigned long) count, (unsigned long) len));
  20052. /* use stack allocated buffer to ensure reachability in errors (e.g. intern error) */
  20053. buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, len);
  20054. DUK_ASSERT(buf != NULL);
  20055. /* [... (sep) str1 str2 ... strN buf] */
  20056. idx = 0;
  20057. for (i = count; i >= 1; i--) {
  20058. if (is_join && i != count) {
  20059. h = duk_require_hstring(ctx, -((duk_idx_t) count) - 2); /* extra -1 for buffer */
  20060. DUK_MEMCPY(buf + idx, DUK_HSTRING_GET_DATA(h), DUK_HSTRING_GET_BYTELEN(h));
  20061. idx += DUK_HSTRING_GET_BYTELEN(h);
  20062. }
  20063. h = duk_require_hstring(ctx, -((duk_idx_t) i) - 1); /* extra -1 for buffer */
  20064. DUK_MEMCPY(buf + idx, DUK_HSTRING_GET_DATA(h), DUK_HSTRING_GET_BYTELEN(h));
  20065. idx += DUK_HSTRING_GET_BYTELEN(h);
  20066. }
  20067. DUK_ASSERT(idx == len);
  20068. /* [... (sep) str1 str2 ... strN buf] */
  20069. /* get rid of the strings early to minimize memory use before intern */
  20070. if (is_join) {
  20071. duk_replace(ctx, -((duk_idx_t) count) - 2); /* overwrite sep */
  20072. duk_pop_n(ctx, count);
  20073. } else {
  20074. duk_replace(ctx, -((duk_idx_t) count) - 1); /* overwrite str1 */
  20075. duk_pop_n(ctx, count-1);
  20076. }
  20077. /* [... buf] */
  20078. (void) duk_to_string(ctx, -1);
  20079. /* [... res] */
  20080. return;
  20081. error_overflow:
  20082. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_CONCAT_RESULT_TOO_LONG);
  20083. }
  20084. DUK_EXTERNAL void duk_concat(duk_context *ctx, duk_idx_t count) {
  20085. DUK_ASSERT_CTX_VALID(ctx);
  20086. duk__concat_and_join_helper(ctx, count, 0 /*is_join*/);
  20087. }
  20088. DUK_EXTERNAL void duk_join(duk_context *ctx, duk_idx_t count) {
  20089. DUK_ASSERT_CTX_VALID(ctx);
  20090. duk__concat_and_join_helper(ctx, count, 1 /*is_join*/);
  20091. }
  20092. /* XXX: could map/decode be unified with duk_unicode_support.c code?
  20093. * Case conversion needs also the character surroundings though.
  20094. */
  20095. DUK_EXTERNAL void duk_decode_string(duk_context *ctx, duk_idx_t index, duk_decode_char_function callback, void *udata) {
  20096. duk_hthread *thr = (duk_hthread *) ctx;
  20097. duk_hstring *h_input;
  20098. const duk_uint8_t *p, *p_start, *p_end;
  20099. duk_codepoint_t cp;
  20100. DUK_ASSERT_CTX_VALID(ctx);
  20101. h_input = duk_require_hstring(ctx, index);
  20102. DUK_ASSERT(h_input != NULL);
  20103. p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
  20104. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  20105. p = p_start;
  20106. for (;;) {
  20107. if (p >= p_end) {
  20108. break;
  20109. }
  20110. cp = (int) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end);
  20111. callback(udata, cp);
  20112. }
  20113. }
  20114. DUK_EXTERNAL void duk_map_string(duk_context *ctx, duk_idx_t index, duk_map_char_function callback, void *udata) {
  20115. duk_hthread *thr = (duk_hthread *) ctx;
  20116. duk_hstring *h_input;
  20117. duk_bufwriter_ctx bw_alloc;
  20118. duk_bufwriter_ctx *bw;
  20119. const duk_uint8_t *p, *p_start, *p_end;
  20120. duk_codepoint_t cp;
  20121. DUK_ASSERT_CTX_VALID(ctx);
  20122. index = duk_normalize_index(ctx, index);
  20123. h_input = duk_require_hstring(ctx, index);
  20124. DUK_ASSERT(h_input != NULL);
  20125. bw = &bw_alloc;
  20126. DUK_BW_INIT_PUSHBUF(thr, bw, DUK_HSTRING_GET_BYTELEN(h_input)); /* reasonable output estimate */
  20127. p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
  20128. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  20129. p = p_start;
  20130. for (;;) {
  20131. /* XXX: could write output in chunks with fewer ensure calls,
  20132. * but relative benefit would be small here.
  20133. */
  20134. if (p >= p_end) {
  20135. break;
  20136. }
  20137. cp = (int) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end);
  20138. cp = callback(udata, cp);
  20139. DUK_BW_WRITE_ENSURE_XUTF8(thr, bw, cp);
  20140. }
  20141. DUK_BW_COMPACT(thr, bw);
  20142. duk_to_string(ctx, -1);
  20143. duk_replace(ctx, index);
  20144. }
  20145. DUK_EXTERNAL void duk_substring(duk_context *ctx, duk_idx_t index, duk_size_t start_offset, duk_size_t end_offset) {
  20146. duk_hthread *thr = (duk_hthread *) ctx;
  20147. duk_hstring *h;
  20148. duk_hstring *res;
  20149. duk_size_t start_byte_offset;
  20150. duk_size_t end_byte_offset;
  20151. DUK_ASSERT_CTX_VALID(ctx);
  20152. index = duk_require_normalize_index(ctx, index);
  20153. h = duk_require_hstring(ctx, index);
  20154. DUK_ASSERT(h != NULL);
  20155. if (end_offset >= DUK_HSTRING_GET_CHARLEN(h)) {
  20156. end_offset = DUK_HSTRING_GET_CHARLEN(h);
  20157. }
  20158. if (start_offset > end_offset) {
  20159. start_offset = end_offset;
  20160. }
  20161. DUK_ASSERT_DISABLE(start_offset >= 0);
  20162. DUK_ASSERT(start_offset <= end_offset && start_offset <= DUK_HSTRING_GET_CHARLEN(h));
  20163. DUK_ASSERT_DISABLE(end_offset >= 0);
  20164. DUK_ASSERT(end_offset >= start_offset && end_offset <= DUK_HSTRING_GET_CHARLEN(h));
  20165. /* guaranteed by string limits */
  20166. DUK_ASSERT(start_offset <= DUK_UINT32_MAX);
  20167. DUK_ASSERT(end_offset <= DUK_UINT32_MAX);
  20168. start_byte_offset = (duk_size_t) duk_heap_strcache_offset_char2byte(thr, h, (duk_uint_fast32_t) start_offset);
  20169. end_byte_offset = (duk_size_t) duk_heap_strcache_offset_char2byte(thr, h, (duk_uint_fast32_t) end_offset);
  20170. DUK_ASSERT(end_byte_offset >= start_byte_offset);
  20171. DUK_ASSERT(end_byte_offset - start_byte_offset <= DUK_UINT32_MAX); /* guaranteed by string limits */
  20172. /* no size check is necessary */
  20173. res = duk_heap_string_intern_checked(thr,
  20174. DUK_HSTRING_GET_DATA(h) + start_byte_offset,
  20175. (duk_uint32_t) (end_byte_offset - start_byte_offset));
  20176. duk_push_hstring(ctx, res);
  20177. duk_replace(ctx, index);
  20178. }
  20179. /* XXX: this is quite clunky. Add Unicode helpers to scan backwards and
  20180. * forwards with a callback to process codepoints?
  20181. */
  20182. DUK_EXTERNAL void duk_trim(duk_context *ctx, duk_idx_t index) {
  20183. duk_hthread *thr = (duk_hthread *) ctx;
  20184. duk_hstring *h;
  20185. const duk_uint8_t *p, *p_start, *p_end, *p_tmp1, *p_tmp2; /* pointers for scanning */
  20186. const duk_uint8_t *q_start, *q_end; /* start (incl) and end (excl) of trimmed part */
  20187. duk_codepoint_t cp;
  20188. DUK_ASSERT_CTX_VALID(ctx);
  20189. index = duk_require_normalize_index(ctx, index);
  20190. h = duk_require_hstring(ctx, index);
  20191. DUK_ASSERT(h != NULL);
  20192. p_start = DUK_HSTRING_GET_DATA(h);
  20193. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h);
  20194. p = p_start;
  20195. while (p < p_end) {
  20196. p_tmp1 = p;
  20197. cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &p_tmp1, p_start, p_end);
  20198. if (!(duk_unicode_is_whitespace(cp) || duk_unicode_is_line_terminator(cp))) {
  20199. break;
  20200. }
  20201. p = p_tmp1;
  20202. }
  20203. q_start = p;
  20204. if (p == p_end) {
  20205. /* entire string is whitespace */
  20206. q_end = p;
  20207. goto scan_done;
  20208. }
  20209. p = p_end;
  20210. while (p > p_start) {
  20211. p_tmp1 = p;
  20212. while (p > p_start) {
  20213. p--;
  20214. if (((*p) & 0xc0) != 0x80) {
  20215. break;
  20216. }
  20217. }
  20218. p_tmp2 = p;
  20219. cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &p_tmp2, p_start, p_end);
  20220. if (!(duk_unicode_is_whitespace(cp) || duk_unicode_is_line_terminator(cp))) {
  20221. p = p_tmp1;
  20222. break;
  20223. }
  20224. }
  20225. q_end = p;
  20226. scan_done:
  20227. /* This may happen when forward and backward scanning disagree
  20228. * (possible for non-extended-UTF-8 strings).
  20229. */
  20230. if (q_end < q_start) {
  20231. q_end = q_start;
  20232. }
  20233. DUK_ASSERT(q_start >= p_start && q_start <= p_end);
  20234. DUK_ASSERT(q_end >= p_start && q_end <= p_end);
  20235. DUK_ASSERT(q_end >= q_start);
  20236. DUK_DDD(DUK_DDDPRINT("trim: p_start=%p, p_end=%p, q_start=%p, q_end=%p",
  20237. (void *) p_start, (void *) p_end, (void *) q_start, (void *) q_end));
  20238. if (q_start == p_start && q_end == p_end) {
  20239. DUK_DDD(DUK_DDDPRINT("nothing was trimmed: avoid interning (hashing etc)"));
  20240. return;
  20241. }
  20242. duk_push_lstring(ctx, (const char *) q_start, (duk_size_t) (q_end - q_start));
  20243. duk_replace(ctx, index);
  20244. }
  20245. DUK_EXTERNAL duk_codepoint_t duk_char_code_at(duk_context *ctx, duk_idx_t index, duk_size_t char_offset) {
  20246. duk_hthread *thr = (duk_hthread *) ctx;
  20247. duk_hstring *h;
  20248. duk_ucodepoint_t cp;
  20249. DUK_ASSERT_CTX_VALID(ctx);
  20250. h = duk_require_hstring(ctx, index);
  20251. DUK_ASSERT(h != NULL);
  20252. DUK_ASSERT_DISABLE(char_offset >= 0); /* always true, arg is unsigned */
  20253. if (char_offset >= DUK_HSTRING_GET_CHARLEN(h)) {
  20254. return 0;
  20255. }
  20256. DUK_ASSERT(char_offset <= DUK_UINT_MAX); /* guaranteed by string limits */
  20257. cp = duk_hstring_char_code_at_raw(thr, h, (duk_uint_t) char_offset);
  20258. return (duk_codepoint_t) cp;
  20259. }
  20260. #line 1 "duk_api_var.c"
  20261. /*
  20262. * Variable access
  20263. */
  20264. /* include removed: duk_internal.h */
  20265. DUK_EXTERNAL void duk_get_var(duk_context *ctx) {
  20266. duk_hthread *thr = (duk_hthread *) ctx;
  20267. duk_activation *act;
  20268. duk_hstring *h_varname;
  20269. duk_small_int_t throw_flag = 1; /* always throw ReferenceError for unresolvable */
  20270. DUK_ASSERT_CTX_VALID(ctx);
  20271. h_varname = duk_require_hstring(ctx, -1); /* XXX: tostring? */
  20272. DUK_ASSERT(h_varname != NULL);
  20273. act = duk_hthread_get_current_activation(thr);
  20274. if (act) {
  20275. (void) duk_js_getvar_activation(thr, act, h_varname, throw_flag); /* -> [ ... varname val this ] */
  20276. } else {
  20277. /* Outside any activation -> look up from global. */
  20278. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL_ENV] != NULL);
  20279. (void) duk_js_getvar_envrec(thr, thr->builtins[DUK_BIDX_GLOBAL_ENV], h_varname, throw_flag);
  20280. }
  20281. /* [ ... varname val this ] (because throw_flag == 1, always resolved) */
  20282. duk_pop(ctx);
  20283. duk_remove(ctx, -2);
  20284. /* [ ... val ] */
  20285. /* Return value would be pointless: because throw_flag==1, we always
  20286. * throw if the identifier doesn't resolve.
  20287. */
  20288. return;
  20289. }
  20290. DUK_EXTERNAL void duk_put_var(duk_context *ctx) {
  20291. duk_hthread *thr = (duk_hthread *) ctx;
  20292. duk_activation *act;
  20293. duk_hstring *h_varname;
  20294. duk_tval *tv_val;
  20295. duk_small_int_t throw_flag;
  20296. DUK_ASSERT_CTX_VALID(ctx);
  20297. h_varname = duk_require_hstring(ctx, -2); /* XXX: tostring? */
  20298. DUK_ASSERT(h_varname != NULL);
  20299. tv_val = duk_require_tval(ctx, -1);
  20300. throw_flag = duk_is_strict_call(ctx);
  20301. act = duk_hthread_get_current_activation(thr);
  20302. if (act) {
  20303. duk_js_putvar_activation(thr, act, h_varname, tv_val, throw_flag); /* -> [ ... varname val this ] */
  20304. } else {
  20305. /* Outside any activation -> put to global. */
  20306. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL_ENV] != NULL);
  20307. duk_js_putvar_envrec(thr, thr->builtins[DUK_BIDX_GLOBAL_ENV], h_varname, tv_val, throw_flag);
  20308. }
  20309. /* [ ... varname val ] */
  20310. duk_pop_2(ctx);
  20311. /* [ ... ] */
  20312. return;
  20313. }
  20314. DUK_EXTERNAL duk_bool_t duk_del_var(duk_context *ctx) {
  20315. DUK_ASSERT_CTX_VALID(ctx);
  20316. DUK_ERROR((duk_hthread *) ctx, DUK_ERR_UNIMPLEMENTED_ERROR, DUK_STR_UNIMPLEMENTED);
  20317. return 0;
  20318. }
  20319. DUK_EXTERNAL duk_bool_t duk_has_var(duk_context *ctx) {
  20320. DUK_ASSERT_CTX_VALID(ctx);
  20321. DUK_ERROR((duk_hthread *) ctx, DUK_ERR_UNIMPLEMENTED_ERROR, DUK_STR_UNIMPLEMENTED);
  20322. return 0;
  20323. }
  20324. #line 1 "duk_bi_array.c"
  20325. /*
  20326. * Array built-ins
  20327. *
  20328. * Note that most Array built-ins are intentionally generic and work even
  20329. * when the 'this' binding is not an Array instance. To ensure this,
  20330. * Array algorithms do not assume "magical" Array behavior for the "length"
  20331. * property, for instance.
  20332. *
  20333. * XXX: the "Throw" flag should be set for (almost?) all [[Put]] and
  20334. * [[Delete]] operations, but it's currently false throughout. Go through
  20335. * all put/delete cases and check throw flag use. Need a new API primitive
  20336. * which allows throws flag to be specified.
  20337. *
  20338. * XXX: array lengths above 2G won't work reliably. There are many places
  20339. * where one needs a full signed 32-bit range ([-0xffffffff, 0xffffffff],
  20340. * i.e. -33- bits). Although array 'length' cannot be written to be outside
  20341. * the unsigned 32-bit range (E5.1 Section 15.4.5.1 throws a RangeError if so)
  20342. * some intermediate values may be above 0xffffffff and this may not be always
  20343. * correctly handled now (duk_uint32_t is not enough for all algorithms).
  20344. *
  20345. * For instance, push() can legitimately write entries beyond length 0xffffffff
  20346. * and cause a RangeError only at the end. To do this properly, the current
  20347. * push() implementation tracks the array index using a 'double' instead of a
  20348. * duk_uint32_t (which is somewhat awkward). See test-bi-array-push-maxlen.js.
  20349. *
  20350. * On using "put" vs. "def" prop
  20351. * =============================
  20352. *
  20353. * Code below must be careful to use the appropriate primitive as it matters
  20354. * for compliance. When using "put" there may be inherited properties in
  20355. * Array.prototype which cause side effects when values are written. When
  20356. * using "define" there are no such side effects, and many test262 test cases
  20357. * check for this (for real world code, such side effects are very rare).
  20358. * Both "put" and "define" are used in the E5.1 specification; as a rule,
  20359. * "put" is used when modifying an existing array (or a non-array 'this'
  20360. * binding) and "define" for setting values into a fresh result array.
  20361. *
  20362. * Also note that Array instance 'length' should be writable, but not
  20363. * enumerable and definitely not configurable: even Duktape code internally
  20364. * assumes that an Array instance will always have a 'length' property.
  20365. * Preventing deletion of the property is critical.
  20366. */
  20367. /* include removed: duk_internal.h */
  20368. /* Perform an intermediate join when this many elements have been pushed
  20369. * on the value stack.
  20370. */
  20371. #define DUK__ARRAY_MID_JOIN_LIMIT 4096
  20372. /* Shared entry code for many Array built-ins. Note that length is left
  20373. * on stack (it could be popped, but that's not necessary).
  20374. */
  20375. DUK_LOCAL duk_uint32_t duk__push_this_obj_len_u32(duk_context *ctx) {
  20376. duk_uint32_t len;
  20377. (void) duk_push_this_coercible_to_object(ctx);
  20378. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LENGTH);
  20379. len = duk_to_uint32(ctx, -1);
  20380. /* -> [ ... ToObject(this) ToUint32(length) ] */
  20381. return len;
  20382. }
  20383. DUK_LOCAL duk_uint32_t duk__push_this_obj_len_u32_limited(duk_context *ctx) {
  20384. /* Range limited to [0, 0x7fffffff] range, i.e. range that can be
  20385. * represented with duk_int32_t. Use this when the method doesn't
  20386. * handle the full 32-bit unsigned range correctly.
  20387. */
  20388. duk_uint32_t ret = duk__push_this_obj_len_u32(ctx);
  20389. if (DUK_UNLIKELY(ret >= 0x80000000UL)) {
  20390. DUK_ERROR((duk_hthread *) ctx, DUK_ERR_INTERNAL_ERROR, DUK_STR_ARRAY_LENGTH_OVER_2G);
  20391. }
  20392. return ret;
  20393. }
  20394. /*
  20395. * Constructor
  20396. */
  20397. DUK_INTERNAL duk_ret_t duk_bi_array_constructor(duk_context *ctx) {
  20398. duk_idx_t nargs;
  20399. duk_double_t d;
  20400. duk_uint32_t len;
  20401. duk_idx_t i;
  20402. nargs = duk_get_top(ctx);
  20403. duk_push_array(ctx);
  20404. if (nargs == 1 && duk_is_number(ctx, 0)) {
  20405. /* XXX: expensive check (also shared elsewhere - so add a shared internal API call?) */
  20406. d = duk_get_number(ctx, 0);
  20407. len = duk_to_uint32(ctx, 0);
  20408. if (((duk_double_t) len) != d) {
  20409. return DUK_RET_RANGE_ERROR;
  20410. }
  20411. /* XXX: if 'len' is low, may want to ensure array part is kept:
  20412. * the caller is likely to want a dense array.
  20413. */
  20414. duk_push_u32(ctx, len);
  20415. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W); /* [ ToUint32(len) array ToUint32(len) ] -> [ ToUint32(len) array ] */
  20416. return 1;
  20417. }
  20418. /* XXX: optimize by creating array into correct size directly, and
  20419. * operating on the array part directly; values can be memcpy()'d from
  20420. * value stack directly as long as refcounts are increased.
  20421. */
  20422. for (i = 0; i < nargs; i++) {
  20423. duk_dup(ctx, i);
  20424. duk_xdef_prop_index_wec(ctx, -2, (duk_uarridx_t) i);
  20425. }
  20426. duk_push_u32(ctx, (duk_uint32_t) nargs);
  20427. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W);
  20428. return 1;
  20429. }
  20430. /*
  20431. * isArray()
  20432. */
  20433. DUK_INTERNAL duk_ret_t duk_bi_array_constructor_is_array(duk_context *ctx) {
  20434. duk_hobject *h;
  20435. h = duk_get_hobject_with_class(ctx, 0, DUK_HOBJECT_CLASS_ARRAY);
  20436. duk_push_boolean(ctx, (h != NULL));
  20437. return 1;
  20438. }
  20439. /*
  20440. * toString()
  20441. */
  20442. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_to_string(duk_context *ctx) {
  20443. (void) duk_push_this_coercible_to_object(ctx);
  20444. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_JOIN);
  20445. /* [ ... this func ] */
  20446. if (!duk_is_callable(ctx, -1)) {
  20447. /* Fall back to the initial (original) Object.toString(). We don't
  20448. * currently have pointers to the built-in functions, only the top
  20449. * level global objects (like "Array") so this is now done in a bit
  20450. * of a hacky manner. It would be cleaner to push the (original)
  20451. * function and use duk_call_method().
  20452. */
  20453. /* XXX: 'this' will be ToObject() coerced twice, which is incorrect
  20454. * but should have no visible side effects.
  20455. */
  20456. DUK_DDD(DUK_DDDPRINT("this.join is not callable, fall back to (original) Object.toString"));
  20457. duk_set_top(ctx, 0);
  20458. return duk_bi_object_prototype_to_string(ctx); /* has access to 'this' binding */
  20459. }
  20460. /* [ ... this func ] */
  20461. duk_insert(ctx, -2);
  20462. /* [ ... func this ] */
  20463. DUK_DDD(DUK_DDDPRINT("calling: func=%!iT, this=%!iT",
  20464. (duk_tval *) duk_get_tval(ctx, -2),
  20465. (duk_tval *) duk_get_tval(ctx, -1)));
  20466. duk_call_method(ctx, 0);
  20467. return 1;
  20468. }
  20469. /*
  20470. * concat()
  20471. */
  20472. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_concat(duk_context *ctx) {
  20473. duk_idx_t i, n;
  20474. duk_uarridx_t idx, idx_last;
  20475. duk_uarridx_t j, len;
  20476. duk_hobject *h;
  20477. /* XXX: the insert here is a bit expensive if there are a lot of items.
  20478. * It could also be special cased in the outermost for loop quite easily
  20479. * (as the element is dup()'d anyway).
  20480. */
  20481. (void) duk_push_this_coercible_to_object(ctx);
  20482. duk_insert(ctx, 0);
  20483. n = duk_get_top(ctx);
  20484. duk_push_array(ctx); /* -> [ ToObject(this) item1 ... itemN arr ] */
  20485. /* NOTE: The Array special behaviors are NOT invoked by duk_xdef_prop_index()
  20486. * (which differs from the official algorithm). If no error is thrown, this
  20487. * doesn't matter as the length is updated at the end. However, if an error
  20488. * is thrown, the length will be unset. That shouldn't matter because the
  20489. * caller won't get a reference to the intermediate value.
  20490. */
  20491. idx = 0;
  20492. idx_last = 0;
  20493. for (i = 0; i < n; i++) {
  20494. DUK_ASSERT_TOP(ctx, n + 1);
  20495. /* [ ToObject(this) item1 ... itemN arr ] */
  20496. duk_dup(ctx, i);
  20497. h = duk_get_hobject_with_class(ctx, -1, DUK_HOBJECT_CLASS_ARRAY);
  20498. if (!h) {
  20499. duk_xdef_prop_index_wec(ctx, -2, idx++);
  20500. idx_last = idx;
  20501. continue;
  20502. }
  20503. /* [ ToObject(this) item1 ... itemN arr item(i) ] */
  20504. /* XXX: an array can have length higher than 32 bits; this is not handled
  20505. * correctly now.
  20506. */
  20507. len = (duk_uarridx_t) duk_get_length(ctx, -1);
  20508. for (j = 0; j < len; j++) {
  20509. if (duk_get_prop_index(ctx, -1, j)) {
  20510. /* [ ToObject(this) item1 ... itemN arr item(i) item(i)[j] ] */
  20511. duk_xdef_prop_index_wec(ctx, -3, idx++);
  20512. idx_last = idx;
  20513. } else {
  20514. idx++;
  20515. duk_pop(ctx);
  20516. #if defined(DUK_USE_NONSTD_ARRAY_CONCAT_TRAILER)
  20517. /* According to E5.1 Section 15.4.4.4 nonexistent trailing
  20518. * elements do not affect 'length' of the result. Test262
  20519. * and other engines disagree, so update idx_last here too.
  20520. */
  20521. idx_last = idx;
  20522. #else
  20523. /* Strict standard behavior, ignore trailing elements for
  20524. * result 'length'.
  20525. */
  20526. #endif
  20527. }
  20528. }
  20529. duk_pop(ctx);
  20530. }
  20531. /* The E5.1 Section 15.4.4.4 algorithm doesn't set the length explicitly
  20532. * in the end, but because we're operating with an internal value which
  20533. * is known to be an array, this should be equivalent.
  20534. */
  20535. duk_push_uarridx(ctx, idx_last);
  20536. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W);
  20537. DUK_ASSERT_TOP(ctx, n + 1);
  20538. return 1;
  20539. }
  20540. /*
  20541. * join(), toLocaleString()
  20542. *
  20543. * Note: checking valstack is necessary, but only in the per-element loop.
  20544. *
  20545. * Note: the trivial approach of pushing all the elements on the value stack
  20546. * and then calling duk_join() fails when the array contains a large number
  20547. * of elements. This problem can't be offloaded to duk_join() because the
  20548. * elements to join must be handled here and have special handling. Current
  20549. * approach is to do intermediate joins with very large number of elements.
  20550. * There is no fancy handling; the prefix gets re-joined multiple times.
  20551. */
  20552. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_join_shared(duk_context *ctx) {
  20553. duk_uint32_t len, count;
  20554. duk_uint32_t idx;
  20555. duk_small_int_t to_locale_string = duk_get_current_magic(ctx);
  20556. duk_idx_t valstack_required;
  20557. /* For join(), nargs is 1. For toLocaleString(), nargs is 0 and
  20558. * setting the top essentially pushes an undefined to the stack,
  20559. * thus defaulting to a comma separator.
  20560. */
  20561. duk_set_top(ctx, 1);
  20562. if (duk_is_undefined(ctx, 0)) {
  20563. duk_pop(ctx);
  20564. duk_push_hstring_stridx(ctx, DUK_STRIDX_COMMA);
  20565. } else {
  20566. duk_to_string(ctx, 0);
  20567. }
  20568. len = duk__push_this_obj_len_u32(ctx);
  20569. /* [ sep ToObject(this) len ] */
  20570. DUK_DDD(DUK_DDDPRINT("sep=%!T, this=%!T, len=%lu",
  20571. (duk_tval *) duk_get_tval(ctx, 0),
  20572. (duk_tval *) duk_get_tval(ctx, 1),
  20573. (unsigned long) len));
  20574. /* The extra (+4) is tight. */
  20575. valstack_required = (len >= DUK__ARRAY_MID_JOIN_LIMIT ?
  20576. DUK__ARRAY_MID_JOIN_LIMIT : len) + 4;
  20577. duk_require_stack(ctx, valstack_required);
  20578. duk_dup(ctx, 0);
  20579. /* [ sep ToObject(this) len sep ] */
  20580. count = 0;
  20581. idx = 0;
  20582. for (;;) {
  20583. if (count >= DUK__ARRAY_MID_JOIN_LIMIT || /* intermediate join to avoid valstack overflow */
  20584. idx >= len) { /* end of loop (careful with len==0) */
  20585. /* [ sep ToObject(this) len sep str0 ... str(count-1) ] */
  20586. DUK_DDD(DUK_DDDPRINT("mid/final join, count=%ld, idx=%ld, len=%ld",
  20587. (long) count, (long) idx, (long) len));
  20588. duk_join(ctx, (duk_idx_t) count); /* -> [ sep ToObject(this) len str ] */
  20589. duk_dup(ctx, 0); /* -> [ sep ToObject(this) len str sep ] */
  20590. duk_insert(ctx, -2); /* -> [ sep ToObject(this) len sep str ] */
  20591. count = 1;
  20592. }
  20593. if (idx >= len) {
  20594. /* if true, the stack already contains the final result */
  20595. break;
  20596. }
  20597. duk_get_prop_index(ctx, 1, (duk_uarridx_t) idx);
  20598. if (duk_is_null_or_undefined(ctx, -1)) {
  20599. duk_pop(ctx);
  20600. duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING);
  20601. } else {
  20602. if (to_locale_string) {
  20603. duk_to_object(ctx, -1);
  20604. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_TO_LOCALE_STRING);
  20605. duk_insert(ctx, -2); /* -> [ ... toLocaleString ToObject(val) ] */
  20606. duk_call_method(ctx, 0);
  20607. duk_to_string(ctx, -1);
  20608. } else {
  20609. duk_to_string(ctx, -1);
  20610. }
  20611. }
  20612. count++;
  20613. idx++;
  20614. }
  20615. /* [ sep ToObject(this) len sep result ] */
  20616. return 1;
  20617. }
  20618. /*
  20619. * pop(), push()
  20620. */
  20621. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_pop(duk_context *ctx) {
  20622. duk_uint32_t len;
  20623. duk_uint32_t idx;
  20624. DUK_ASSERT_TOP(ctx, 0);
  20625. len = duk__push_this_obj_len_u32(ctx);
  20626. if (len == 0) {
  20627. duk_push_int(ctx, 0);
  20628. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LENGTH);
  20629. return 0;
  20630. }
  20631. idx = len - 1;
  20632. duk_get_prop_index(ctx, 0, (duk_uarridx_t) idx);
  20633. duk_del_prop_index(ctx, 0, (duk_uarridx_t) idx);
  20634. duk_push_u32(ctx, idx);
  20635. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LENGTH);
  20636. return 1;
  20637. }
  20638. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_push(duk_context *ctx) {
  20639. /* Note: 'this' is not necessarily an Array object. The push()
  20640. * algorithm is supposed to work for other kinds of objects too,
  20641. * so the algorithm has e.g. an explicit update for the 'length'
  20642. * property which is normally "magical" in arrays.
  20643. */
  20644. duk_uint32_t len;
  20645. duk_idx_t i, n;
  20646. n = duk_get_top(ctx);
  20647. len = duk__push_this_obj_len_u32(ctx);
  20648. /* [ arg1 ... argN obj length ] */
  20649. /* Technically Array.prototype.push() can create an Array with length
  20650. * longer than 2^32-1, i.e. outside the 32-bit range. The final length
  20651. * is *not* wrapped to 32 bits in the specification.
  20652. *
  20653. * This implementation tracks length with a uint32 because it's much
  20654. * more practical.
  20655. *
  20656. * See: test-bi-array-push-maxlen.js.
  20657. */
  20658. if (len + (duk_uint32_t) n < len) {
  20659. DUK_D(DUK_DPRINT("Array.prototype.push() would go beyond 32-bit length, throw"));
  20660. return DUK_RET_RANGE_ERROR;
  20661. }
  20662. for (i = 0; i < n; i++) {
  20663. duk_dup(ctx, i);
  20664. duk_put_prop_index(ctx, -3, len + i);
  20665. }
  20666. len += n;
  20667. duk_push_u32(ctx, len);
  20668. duk_dup_top(ctx);
  20669. duk_put_prop_stridx(ctx, -4, DUK_STRIDX_LENGTH);
  20670. /* [ arg1 ... argN obj length new_length ] */
  20671. return 1;
  20672. }
  20673. /*
  20674. * sort()
  20675. *
  20676. * Currently qsort with random pivot. This is now really, really slow,
  20677. * because there is no fast path for array parts.
  20678. *
  20679. * Signed indices are used because qsort() leaves and degenerate cases
  20680. * may use a negative offset.
  20681. */
  20682. DUK_LOCAL duk_small_int_t duk__array_sort_compare(duk_context *ctx, duk_int_t idx1, duk_int_t idx2) {
  20683. duk_bool_t have1, have2;
  20684. duk_bool_t undef1, undef2;
  20685. duk_small_int_t ret;
  20686. duk_idx_t idx_obj = 1; /* fixed offsets in valstack */
  20687. duk_idx_t idx_fn = 0;
  20688. duk_hstring *h1, *h2;
  20689. /* Fast exit if indices are identical. This is valid for a non-existent property,
  20690. * for an undefined value, and almost always for ToString() coerced comparison of
  20691. * arbitrary values (corner cases where this is not the case include e.g. a an
  20692. * object with varying ToString() coercion).
  20693. *
  20694. * The specification does not prohibit "caching" of values read from the array, so
  20695. * assuming equality for comparing an index with itself falls into the category of
  20696. * "caching".
  20697. *
  20698. * Also, compareFn may be inconsistent, so skipping a call to compareFn here may
  20699. * have an effect on the final result. The specification does not require any
  20700. * specific behavior for inconsistent compare functions, so again, this fast path
  20701. * is OK.
  20702. */
  20703. if (idx1 == idx2) {
  20704. DUK_DDD(DUK_DDDPRINT("duk__array_sort_compare: idx1=%ld, idx2=%ld -> indices identical, quick exit",
  20705. (long) idx1, (long) idx2));
  20706. return 0;
  20707. }
  20708. have1 = duk_get_prop_index(ctx, idx_obj, (duk_uarridx_t) idx1);
  20709. have2 = duk_get_prop_index(ctx, idx_obj, (duk_uarridx_t) idx2);
  20710. DUK_DDD(DUK_DDDPRINT("duk__array_sort_compare: idx1=%ld, idx2=%ld, have1=%ld, have2=%ld, val1=%!T, val2=%!T",
  20711. (long) idx1, (long) idx2, (long) have1, (long) have2,
  20712. (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1)));
  20713. if (have1) {
  20714. if (have2) {
  20715. ;
  20716. } else {
  20717. ret = -1;
  20718. goto pop_ret;
  20719. }
  20720. } else {
  20721. if (have2) {
  20722. ret = 1;
  20723. goto pop_ret;
  20724. } else {
  20725. ret = 0;
  20726. goto pop_ret;
  20727. }
  20728. }
  20729. undef1 = duk_is_undefined(ctx, -2);
  20730. undef2 = duk_is_undefined(ctx, -1);
  20731. if (undef1) {
  20732. if (undef2) {
  20733. ret = 0;
  20734. goto pop_ret;
  20735. } else {
  20736. ret = 1;
  20737. goto pop_ret;
  20738. }
  20739. } else {
  20740. if (undef2) {
  20741. ret = -1;
  20742. goto pop_ret;
  20743. } else {
  20744. ;
  20745. }
  20746. }
  20747. if (!duk_is_undefined(ctx, idx_fn)) {
  20748. duk_double_t d;
  20749. /* no need to check callable; duk_call() will do that */
  20750. duk_dup(ctx, idx_fn); /* -> [ ... x y fn ] */
  20751. duk_insert(ctx, -3); /* -> [ ... fn x y ] */
  20752. duk_call(ctx, 2); /* -> [ ... res ] */
  20753. /* The specification is a bit vague what to do if the return
  20754. * value is not a number. Other implementations seem to
  20755. * tolerate non-numbers but e.g. V8 won't apparently do a
  20756. * ToNumber().
  20757. */
  20758. /* XXX: best behavior for real world compatibility? */
  20759. d = duk_to_number(ctx, -1);
  20760. if (d < 0.0) {
  20761. ret = -1;
  20762. } else if (d > 0.0) {
  20763. ret = 1;
  20764. } else {
  20765. ret = 0;
  20766. }
  20767. duk_pop(ctx);
  20768. DUK_DDD(DUK_DDDPRINT("-> result %ld (from comparefn, after coercion)", (long) ret));
  20769. return ret;
  20770. }
  20771. /* string compare is the default (a bit oddly) */
  20772. h1 = duk_to_hstring(ctx, -2);
  20773. h2 = duk_to_hstring(ctx, -1);
  20774. DUK_ASSERT(h1 != NULL);
  20775. DUK_ASSERT(h2 != NULL);
  20776. ret = duk_js_string_compare(h1, h2); /* retval is directly usable */
  20777. goto pop_ret;
  20778. pop_ret:
  20779. duk_pop_2(ctx);
  20780. DUK_DDD(DUK_DDDPRINT("-> result %ld", (long) ret));
  20781. return ret;
  20782. }
  20783. DUK_LOCAL void duk__array_sort_swap(duk_context *ctx, duk_int_t l, duk_int_t r) {
  20784. duk_bool_t have_l, have_r;
  20785. duk_idx_t idx_obj = 1; /* fixed offset in valstack */
  20786. if (l == r) {
  20787. return;
  20788. }
  20789. /* swap elements; deal with non-existent elements correctly */
  20790. have_l = duk_get_prop_index(ctx, idx_obj, (duk_uarridx_t) l);
  20791. have_r = duk_get_prop_index(ctx, idx_obj, (duk_uarridx_t) r);
  20792. if (have_r) {
  20793. /* right exists, [[Put]] regardless whether or not left exists */
  20794. duk_put_prop_index(ctx, idx_obj, (duk_uarridx_t) l);
  20795. } else {
  20796. duk_del_prop_index(ctx, idx_obj, (duk_uarridx_t) l);
  20797. duk_pop(ctx);
  20798. }
  20799. if (have_l) {
  20800. duk_put_prop_index(ctx, idx_obj, (duk_uarridx_t) r);
  20801. } else {
  20802. duk_del_prop_index(ctx, idx_obj, (duk_uarridx_t) r);
  20803. duk_pop(ctx);
  20804. }
  20805. }
  20806. #if defined(DUK_USE_DDDPRINT)
  20807. /* Debug print which visualizes the qsort partitioning process. */
  20808. DUK_LOCAL void duk__debuglog_qsort_state(duk_context *ctx, duk_int_t lo, duk_int_t hi, duk_int_t pivot) {
  20809. char buf[4096];
  20810. char *ptr = buf;
  20811. duk_int_t i, n;
  20812. n = (duk_int_t) duk_get_length(ctx, 1);
  20813. if (n > 4000) {
  20814. n = 4000;
  20815. }
  20816. *ptr++ = '[';
  20817. for (i = 0; i < n; i++) {
  20818. if (i == pivot) {
  20819. *ptr++ = '|';
  20820. } else if (i == lo) {
  20821. *ptr++ = '<';
  20822. } else if (i == hi) {
  20823. *ptr++ = '>';
  20824. } else if (i >= lo && i <= hi) {
  20825. *ptr++ = '-';
  20826. } else {
  20827. *ptr++ = ' ';
  20828. }
  20829. }
  20830. *ptr++ = ']';
  20831. *ptr++ = '\0';
  20832. DUK_DDD(DUK_DDDPRINT("%s (lo=%ld, hi=%ld, pivot=%ld)",
  20833. (const char *) buf, (long) lo, (long) hi, (long) pivot));
  20834. }
  20835. #endif
  20836. DUK_LOCAL void duk__array_qsort(duk_context *ctx, duk_int_t lo, duk_int_t hi) {
  20837. duk_hthread *thr = (duk_hthread *) ctx;
  20838. duk_int_t p, l, r;
  20839. /* The lo/hi indices may be crossed and hi < 0 is possible at entry. */
  20840. DUK_DDD(DUK_DDDPRINT("duk__array_qsort: lo=%ld, hi=%ld, obj=%!T",
  20841. (long) lo, (long) hi, (duk_tval *) duk_get_tval(ctx, 1)));
  20842. DUK_ASSERT_TOP(ctx, 3);
  20843. /* In some cases it may be that lo > hi, or hi < 0; these
  20844. * degenerate cases happen e.g. for empty arrays, and in
  20845. * recursion leaves.
  20846. */
  20847. /* trivial cases */
  20848. if (hi - lo < 1) {
  20849. DUK_DDD(DUK_DDDPRINT("degenerate case, return immediately"));
  20850. return;
  20851. }
  20852. DUK_ASSERT(hi > lo);
  20853. DUK_ASSERT(hi - lo + 1 >= 2);
  20854. /* randomized pivot selection */
  20855. p = lo + (duk_util_tinyrandom_get_bits(thr, 30) % (hi - lo + 1)); /* rnd in [lo,hi] */
  20856. DUK_ASSERT(p >= lo && p <= hi);
  20857. DUK_DDD(DUK_DDDPRINT("lo=%ld, hi=%ld, chose pivot p=%ld",
  20858. (long) lo, (long) hi, (long) p));
  20859. /* move pivot out of the way */
  20860. duk__array_sort_swap(ctx, p, lo);
  20861. p = lo;
  20862. DUK_DDD(DUK_DDDPRINT("pivot moved out of the way: %!T", (duk_tval *) duk_get_tval(ctx, 1)));
  20863. l = lo + 1;
  20864. r = hi;
  20865. for (;;) {
  20866. /* find elements to swap */
  20867. for (;;) {
  20868. DUK_DDD(DUK_DDDPRINT("left scan: l=%ld, r=%ld, p=%ld",
  20869. (long) l, (long) r, (long) p));
  20870. if (l >= hi) {
  20871. break;
  20872. }
  20873. if (duk__array_sort_compare(ctx, l, p) >= 0) { /* !(l < p) */
  20874. break;
  20875. }
  20876. l++;
  20877. }
  20878. for (;;) {
  20879. DUK_DDD(DUK_DDDPRINT("right scan: l=%ld, r=%ld, p=%ld",
  20880. (long) l, (long) r, (long) p));
  20881. if (r <= lo) {
  20882. break;
  20883. }
  20884. if (duk__array_sort_compare(ctx, p, r) >= 0) { /* !(p < r) */
  20885. break;
  20886. }
  20887. r--;
  20888. }
  20889. if (l >= r) {
  20890. goto done;
  20891. }
  20892. DUK_ASSERT(l < r);
  20893. DUK_DDD(DUK_DDDPRINT("swap %ld and %ld", (long) l, (long) r));
  20894. duk__array_sort_swap(ctx, l, r);
  20895. DUK_DDD(DUK_DDDPRINT("after swap: %!T", (duk_tval *) duk_get_tval(ctx, 1)));
  20896. l++;
  20897. r--;
  20898. }
  20899. done:
  20900. /* Note that 'l' and 'r' may cross, i.e. r < l */
  20901. DUK_ASSERT(l >= lo && l <= hi);
  20902. DUK_ASSERT(r >= lo && r <= hi);
  20903. /* XXX: there's no explicit recursion bound here now. For the average
  20904. * qsort recursion depth O(log n) that's not really necessary: e.g. for
  20905. * 2**32 recursion depth would be about 32 which is OK. However, qsort
  20906. * worst case recursion depth is O(n) which may be a problem.
  20907. */
  20908. /* move pivot to its final place */
  20909. DUK_DDD(DUK_DDDPRINT("before final pivot swap: %!T", (duk_tval *) duk_get_tval(ctx, 1)));
  20910. duk__array_sort_swap(ctx, lo, r);
  20911. #if defined(DUK_USE_DDDPRINT)
  20912. duk__debuglog_qsort_state(ctx, lo, hi, r);
  20913. #endif
  20914. DUK_DDD(DUK_DDDPRINT("recurse: pivot=%ld, obj=%!T", (long) r, (duk_tval *) duk_get_tval(ctx, 1)));
  20915. duk__array_qsort(ctx, lo, r - 1);
  20916. duk__array_qsort(ctx, r + 1, hi);
  20917. }
  20918. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_sort(duk_context *ctx) {
  20919. duk_uint32_t len;
  20920. /* XXX: len >= 0x80000000 won't work below because a signed type
  20921. * is needed by qsort.
  20922. */
  20923. len = duk__push_this_obj_len_u32_limited(ctx);
  20924. /* stack[0] = compareFn
  20925. * stack[1] = ToObject(this)
  20926. * stack[2] = ToUint32(length)
  20927. */
  20928. if (len > 0) {
  20929. /* avoid degenerate cases, so that (len - 1) won't underflow */
  20930. duk__array_qsort(ctx, (duk_int_t) 0, (duk_int_t) (len - 1));
  20931. }
  20932. DUK_ASSERT_TOP(ctx, 3);
  20933. duk_pop(ctx);
  20934. return 1; /* return ToObject(this) */
  20935. }
  20936. /*
  20937. * splice()
  20938. */
  20939. /* XXX: this compiles to over 500 bytes now, even without special handling
  20940. * for an array part. Uses signed ints so does not handle full array range correctly.
  20941. */
  20942. /* XXX: can shift() / unshift() use the same helper?
  20943. * shift() is (close to?) <--> splice(0, 1)
  20944. * unshift is (close to?) <--> splice(0, 0, [items])?
  20945. */
  20946. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_splice(duk_context *ctx) {
  20947. duk_idx_t nargs;
  20948. duk_uint32_t len;
  20949. duk_bool_t have_delcount;
  20950. duk_int_t item_count;
  20951. duk_int_t act_start;
  20952. duk_int_t del_count;
  20953. duk_int_t i, n;
  20954. DUK_UNREF(have_delcount);
  20955. nargs = duk_get_top(ctx);
  20956. if (nargs < 2) {
  20957. duk_set_top(ctx, 2);
  20958. nargs = 2;
  20959. have_delcount = 0;
  20960. } else {
  20961. have_delcount = 1;
  20962. }
  20963. /* XXX: len >= 0x80000000 won't work below because we need to be
  20964. * able to represent -len.
  20965. */
  20966. len = duk__push_this_obj_len_u32_limited(ctx);
  20967. act_start = duk_to_int_clamped(ctx, 0, -((duk_int_t) len), (duk_int_t) len);
  20968. if (act_start < 0) {
  20969. act_start = len + act_start;
  20970. }
  20971. DUK_ASSERT(act_start >= 0 && act_start <= (duk_int_t) len);
  20972. #ifdef DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT
  20973. if (have_delcount) {
  20974. #endif
  20975. del_count = duk_to_int_clamped(ctx, 1, 0, len - act_start);
  20976. #ifdef DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT
  20977. } else {
  20978. /* E5.1 standard behavior when deleteCount is not given would be
  20979. * to treat it just like if 'undefined' was given, which coerces
  20980. * ultimately to 0. Real world behavior is to splice to the end
  20981. * of array, see test-bi-array-proto-splice-no-delcount.js.
  20982. */
  20983. del_count = len - act_start;
  20984. }
  20985. #endif
  20986. DUK_ASSERT(nargs >= 2);
  20987. item_count = (duk_int_t) (nargs - 2);
  20988. DUK_ASSERT(del_count >= 0 && del_count <= (duk_int_t) len - act_start);
  20989. DUK_ASSERT(del_count + act_start <= (duk_int_t) len);
  20990. /* For now, restrict result array into 32-bit length range. */
  20991. if (((duk_double_t) len) - ((duk_double_t) del_count) + ((duk_double_t) item_count) > (duk_double_t) DUK_UINT32_MAX) {
  20992. DUK_D(DUK_DPRINT("Array.prototype.splice() would go beyond 32-bit length, throw"));
  20993. return DUK_RET_RANGE_ERROR;
  20994. }
  20995. duk_push_array(ctx);
  20996. /* stack[0] = start
  20997. * stack[1] = deleteCount
  20998. * stack[2...nargs-1] = items
  20999. * stack[nargs] = ToObject(this) -3
  21000. * stack[nargs+1] = ToUint32(length) -2
  21001. * stack[nargs+2] = result array -1
  21002. */
  21003. DUK_ASSERT_TOP(ctx, nargs + 3);
  21004. /* Step 9: copy elements-to-be-deleted into the result array */
  21005. for (i = 0; i < del_count; i++) {
  21006. if (duk_get_prop_index(ctx, -3, (duk_uarridx_t) (act_start + i))) {
  21007. duk_xdef_prop_index_wec(ctx, -2, i); /* throw flag irrelevant (false in std alg) */
  21008. } else {
  21009. duk_pop(ctx);
  21010. }
  21011. }
  21012. duk_push_u32(ctx, (duk_uint32_t) del_count);
  21013. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W);
  21014. /* Steps 12 and 13: reorganize elements to make room for itemCount elements */
  21015. if (item_count < del_count) {
  21016. /* [ A B C D E F G H ] rel_index = 2, del_count 3, item count 1
  21017. * -> [ A B F G H ] (conceptual intermediate step)
  21018. * -> [ A B . F G H ] (placeholder marked)
  21019. * [ A B C F G H ] (actual result at this point, C will be replaced)
  21020. */
  21021. DUK_ASSERT_TOP(ctx, nargs + 3);
  21022. n = len - del_count;
  21023. for (i = act_start; i < n; i++) {
  21024. if (duk_get_prop_index(ctx, -3, (duk_uarridx_t) (i + del_count))) {
  21025. duk_put_prop_index(ctx, -4, (duk_uarridx_t) (i + item_count));
  21026. } else {
  21027. duk_pop(ctx);
  21028. duk_del_prop_index(ctx, -3, (duk_uarridx_t) (i + item_count));
  21029. }
  21030. }
  21031. DUK_ASSERT_TOP(ctx, nargs + 3);
  21032. /* loop iterator init and limit changed from standard algorithm */
  21033. n = len - del_count + item_count;
  21034. for (i = len - 1; i >= n; i--) {
  21035. duk_del_prop_index(ctx, -3, (duk_uarridx_t) i);
  21036. }
  21037. DUK_ASSERT_TOP(ctx, nargs + 3);
  21038. } else if (item_count > del_count) {
  21039. /* [ A B C D E F G H ] rel_index = 2, del_count 3, item count 4
  21040. * -> [ A B F G H ] (conceptual intermediate step)
  21041. * -> [ A B . . . . F G H ] (placeholder marked)
  21042. * [ A B C D E F F G H ] (actual result at this point)
  21043. */
  21044. DUK_ASSERT_TOP(ctx, nargs + 3);
  21045. /* loop iterator init and limit changed from standard algorithm */
  21046. for (i = len - del_count - 1; i >= act_start; i--) {
  21047. if (duk_get_prop_index(ctx, -3, (duk_uarridx_t) (i + del_count))) {
  21048. duk_put_prop_index(ctx, -4, (duk_uarridx_t) (i + item_count));
  21049. } else {
  21050. duk_pop(ctx);
  21051. duk_del_prop_index(ctx, -3, (duk_uarridx_t) (i + item_count));
  21052. }
  21053. }
  21054. DUK_ASSERT_TOP(ctx, nargs + 3);
  21055. } else {
  21056. /* [ A B C D E F G H ] rel_index = 2, del_count 3, item count 3
  21057. * -> [ A B F G H ] (conceptual intermediate step)
  21058. * -> [ A B . . . F G H ] (placeholder marked)
  21059. * [ A B C D E F G H ] (actual result at this point)
  21060. */
  21061. }
  21062. DUK_ASSERT_TOP(ctx, nargs + 3);
  21063. /* Step 15: insert itemCount elements into the hole made above */
  21064. for (i = 0; i < item_count; i++) {
  21065. duk_dup(ctx, i + 2); /* args start at index 2 */
  21066. duk_put_prop_index(ctx, -4, (duk_uarridx_t) (act_start + i));
  21067. }
  21068. /* Step 16: update length; note that the final length may be above 32 bit range
  21069. * (but we checked above that this isn't the case here)
  21070. */
  21071. duk_push_u32(ctx, len - del_count + item_count);
  21072. duk_put_prop_stridx(ctx, -4, DUK_STRIDX_LENGTH);
  21073. /* result array is already at the top of stack */
  21074. DUK_ASSERT_TOP(ctx, nargs + 3);
  21075. return 1;
  21076. }
  21077. /*
  21078. * reverse()
  21079. */
  21080. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_reverse(duk_context *ctx) {
  21081. duk_uint32_t len;
  21082. duk_uint32_t middle;
  21083. duk_uint32_t lower, upper;
  21084. duk_bool_t have_lower, have_upper;
  21085. len = duk__push_this_obj_len_u32(ctx);
  21086. middle = len / 2;
  21087. /* If len <= 1, middle will be 0 and for-loop bails out
  21088. * immediately (0 < 0 -> false).
  21089. */
  21090. for (lower = 0; lower < middle; lower++) {
  21091. DUK_ASSERT(len >= 2);
  21092. DUK_ASSERT_TOP(ctx, 2);
  21093. DUK_ASSERT(len >= lower + 1);
  21094. upper = len - lower - 1;
  21095. have_lower = duk_get_prop_index(ctx, -2, (duk_uarridx_t) lower);
  21096. have_upper = duk_get_prop_index(ctx, -3, (duk_uarridx_t) upper);
  21097. /* [ ToObject(this) ToUint32(length) lowerValue upperValue ] */
  21098. if (have_upper) {
  21099. duk_put_prop_index(ctx, -4, (duk_uarridx_t) lower);
  21100. } else {
  21101. duk_del_prop_index(ctx, -4, (duk_uarridx_t) lower);
  21102. duk_pop(ctx);
  21103. }
  21104. if (have_lower) {
  21105. duk_put_prop_index(ctx, -3, (duk_uarridx_t) upper);
  21106. } else {
  21107. duk_del_prop_index(ctx, -3, (duk_uarridx_t) upper);
  21108. duk_pop(ctx);
  21109. }
  21110. DUK_ASSERT_TOP(ctx, 2);
  21111. }
  21112. DUK_ASSERT_TOP(ctx, 2);
  21113. duk_pop(ctx); /* -> [ ToObject(this) ] */
  21114. return 1;
  21115. }
  21116. /*
  21117. * slice()
  21118. */
  21119. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_slice(duk_context *ctx) {
  21120. duk_uint32_t len;
  21121. duk_int_t start, end;
  21122. duk_int_t i;
  21123. duk_uarridx_t idx;
  21124. duk_uint32_t res_length = 0;
  21125. /* XXX: len >= 0x80000000 won't work below because we need to be
  21126. * able to represent -len.
  21127. */
  21128. len = duk__push_this_obj_len_u32_limited(ctx);
  21129. duk_push_array(ctx);
  21130. /* stack[0] = start
  21131. * stack[1] = end
  21132. * stack[2] = ToObject(this)
  21133. * stack[3] = ToUint32(length)
  21134. * stack[4] = result array
  21135. */
  21136. start = duk_to_int_clamped(ctx, 0, -((duk_int_t) len), (duk_int_t) len);
  21137. if (start < 0) {
  21138. start = len + start;
  21139. }
  21140. /* XXX: could duk_is_undefined() provide defaulting undefined to 'len'
  21141. * (the upper limit)?
  21142. */
  21143. if (duk_is_undefined(ctx, 1)) {
  21144. end = len;
  21145. } else {
  21146. end = duk_to_int_clamped(ctx, 1, -((duk_int_t) len), (duk_int_t) len);
  21147. if (end < 0) {
  21148. end = len + end;
  21149. }
  21150. }
  21151. DUK_ASSERT(start >= 0 && (duk_uint32_t) start <= len);
  21152. DUK_ASSERT(end >= 0 && (duk_uint32_t) end <= len);
  21153. idx = 0;
  21154. for (i = start; i < end; i++) {
  21155. DUK_ASSERT_TOP(ctx, 5);
  21156. if (duk_get_prop_index(ctx, 2, (duk_uarridx_t) i)) {
  21157. duk_xdef_prop_index_wec(ctx, 4, idx);
  21158. res_length = idx + 1;
  21159. } else {
  21160. duk_pop(ctx);
  21161. }
  21162. idx++;
  21163. DUK_ASSERT_TOP(ctx, 5);
  21164. }
  21165. duk_push_u32(ctx, res_length);
  21166. duk_xdef_prop_stridx(ctx, 4, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W);
  21167. DUK_ASSERT_TOP(ctx, 5);
  21168. return 1;
  21169. }
  21170. /*
  21171. * shift()
  21172. */
  21173. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_shift(duk_context *ctx) {
  21174. duk_uint32_t len;
  21175. duk_uint32_t i;
  21176. len = duk__push_this_obj_len_u32(ctx);
  21177. if (len == 0) {
  21178. duk_push_int(ctx, 0);
  21179. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LENGTH);
  21180. return 0;
  21181. }
  21182. duk_get_prop_index(ctx, 0, 0);
  21183. /* stack[0] = object (this)
  21184. * stack[1] = ToUint32(length)
  21185. * stack[2] = elem at index 0 (retval)
  21186. */
  21187. for (i = 1; i < len; i++) {
  21188. DUK_ASSERT_TOP(ctx, 3);
  21189. if (duk_get_prop_index(ctx, 0, (duk_uarridx_t) i)) {
  21190. /* fromPresent = true */
  21191. duk_put_prop_index(ctx, 0, (duk_uarridx_t) (i - 1));
  21192. } else {
  21193. /* fromPresent = false */
  21194. duk_del_prop_index(ctx, 0, (duk_uarridx_t) (i - 1));
  21195. duk_pop(ctx);
  21196. }
  21197. }
  21198. duk_del_prop_index(ctx, 0, (duk_uarridx_t) (len - 1));
  21199. duk_push_u32(ctx, (duk_uint32_t) (len - 1));
  21200. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LENGTH);
  21201. DUK_ASSERT_TOP(ctx, 3);
  21202. return 1;
  21203. }
  21204. /*
  21205. * unshift()
  21206. */
  21207. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_unshift(duk_context *ctx) {
  21208. duk_idx_t nargs;
  21209. duk_uint32_t len;
  21210. duk_uint32_t i;
  21211. nargs = duk_get_top(ctx);
  21212. len = duk__push_this_obj_len_u32(ctx);
  21213. /* stack[0...nargs-1] = unshift args (vararg)
  21214. * stack[nargs] = ToObject(this)
  21215. * stack[nargs+1] = ToUint32(length)
  21216. */
  21217. DUK_ASSERT_TOP(ctx, nargs + 2);
  21218. /* Note: unshift() may operate on indices above unsigned 32-bit range
  21219. * and the final length may be >= 2**32. However, we restrict the
  21220. * final result to 32-bit range for practicality.
  21221. */
  21222. if (len + (duk_uint32_t) nargs < len) {
  21223. DUK_D(DUK_DPRINT("Array.prototype.unshift() would go beyond 32-bit length, throw"));
  21224. return DUK_RET_RANGE_ERROR;
  21225. }
  21226. i = len;
  21227. while (i > 0) {
  21228. DUK_ASSERT_TOP(ctx, nargs + 2);
  21229. i--;
  21230. /* k+argCount-1; note that may be above 32-bit range */
  21231. if (duk_get_prop_index(ctx, -2, (duk_uarridx_t) i)) {
  21232. /* fromPresent = true */
  21233. /* [ ... ToObject(this) ToUint32(length) val ] */
  21234. duk_put_prop_index(ctx, -3, (duk_uarridx_t) (i + nargs)); /* -> [ ... ToObject(this) ToUint32(length) ] */
  21235. } else {
  21236. /* fromPresent = false */
  21237. /* [ ... ToObject(this) ToUint32(length) val ] */
  21238. duk_pop(ctx);
  21239. duk_del_prop_index(ctx, -2, (duk_uarridx_t) (i + nargs)); /* -> [ ... ToObject(this) ToUint32(length) ] */
  21240. }
  21241. DUK_ASSERT_TOP(ctx, nargs + 2);
  21242. }
  21243. for (i = 0; i < (duk_uint32_t) nargs; i++) {
  21244. DUK_ASSERT_TOP(ctx, nargs + 2);
  21245. duk_dup(ctx, i); /* -> [ ... ToObject(this) ToUint32(length) arg[i] ] */
  21246. duk_put_prop_index(ctx, -3, (duk_uarridx_t) i);
  21247. DUK_ASSERT_TOP(ctx, nargs + 2);
  21248. }
  21249. DUK_ASSERT_TOP(ctx, nargs + 2);
  21250. duk_push_u32(ctx, len + nargs);
  21251. duk_dup_top(ctx); /* -> [ ... ToObject(this) ToUint32(length) final_len final_len ] */
  21252. duk_put_prop_stridx(ctx, -4, DUK_STRIDX_LENGTH);
  21253. return 1;
  21254. }
  21255. /*
  21256. * indexOf(), lastIndexOf()
  21257. */
  21258. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_indexof_shared(duk_context *ctx) {
  21259. duk_idx_t nargs;
  21260. duk_int_t i, len;
  21261. duk_int_t from_index;
  21262. duk_small_int_t idx_step = duk_get_current_magic(ctx); /* idx_step is +1 for indexOf, -1 for lastIndexOf */
  21263. /* lastIndexOf() needs to be a vararg function because we must distinguish
  21264. * between an undefined fromIndex and a "not given" fromIndex; indexOf() is
  21265. * made vararg for symmetry although it doesn't strictly need to be.
  21266. */
  21267. nargs = duk_get_top(ctx);
  21268. duk_set_top(ctx, 2);
  21269. /* XXX: must be able to represent -len */
  21270. len = (duk_int_t) duk__push_this_obj_len_u32_limited(ctx);
  21271. if (len == 0) {
  21272. goto not_found;
  21273. }
  21274. /* Index clamping is a bit tricky, we must ensure that we'll only iterate
  21275. * through elements that exist and that the specific requirements from E5.1
  21276. * Sections 15.4.4.14 and 15.4.4.15 are fulfilled; especially:
  21277. *
  21278. * - indexOf: clamp to [-len,len], negative handling -> [0,len],
  21279. * if clamped result is len, for-loop bails out immediately
  21280. *
  21281. * - lastIndexOf: clamp to [-len-1, len-1], negative handling -> [-1, len-1],
  21282. * if clamped result is -1, for-loop bails out immediately
  21283. *
  21284. * If fromIndex is not given, ToInteger(undefined) = 0, which is correct
  21285. * for indexOf() but incorrect for lastIndexOf(). Hence special handling,
  21286. * and why lastIndexOf() needs to be a vararg function.
  21287. */
  21288. if (nargs >= 2) {
  21289. /* indexOf: clamp fromIndex to [-len, len]
  21290. * (if fromIndex == len, for-loop terminates directly)
  21291. *
  21292. * lastIndexOf: clamp fromIndex to [-len - 1, len - 1]
  21293. * (if clamped to -len-1 -> fromIndex becomes -1, terminates for-loop directly)
  21294. */
  21295. from_index = duk_to_int_clamped(ctx,
  21296. 1,
  21297. (idx_step > 0 ? -len : -len - 1),
  21298. (idx_step > 0 ? len : len - 1));
  21299. if (from_index < 0) {
  21300. /* for lastIndexOf, result may be -1 (mark immediate termination) */
  21301. from_index = len + from_index;
  21302. }
  21303. } else {
  21304. /* for indexOf, ToInteger(undefined) would be 0, i.e. correct, but
  21305. * handle both indexOf and lastIndexOf specially here.
  21306. */
  21307. if (idx_step > 0) {
  21308. from_index = 0;
  21309. } else {
  21310. from_index = len - 1;
  21311. }
  21312. }
  21313. /* stack[0] = searchElement
  21314. * stack[1] = fromIndex
  21315. * stack[2] = object
  21316. * stack[3] = length (not needed, but not popped above)
  21317. */
  21318. for (i = from_index; i >= 0 && i < len; i += idx_step) {
  21319. DUK_ASSERT_TOP(ctx, 4);
  21320. if (duk_get_prop_index(ctx, 2, (duk_uarridx_t) i)) {
  21321. DUK_ASSERT_TOP(ctx, 5);
  21322. if (duk_strict_equals(ctx, 0, 4)) {
  21323. duk_push_int(ctx, i);
  21324. return 1;
  21325. }
  21326. }
  21327. duk_pop(ctx);
  21328. }
  21329. not_found:
  21330. duk_push_int(ctx, -1);
  21331. return 1;
  21332. }
  21333. /*
  21334. * every(), some(), forEach(), map(), filter()
  21335. */
  21336. #define DUK__ITER_EVERY 0
  21337. #define DUK__ITER_SOME 1
  21338. #define DUK__ITER_FOREACH 2
  21339. #define DUK__ITER_MAP 3
  21340. #define DUK__ITER_FILTER 4
  21341. /* XXX: This helper is a bit awkward because the handling for the different iteration
  21342. * callers is quite different. This now compiles to a bit less than 500 bytes, so with
  21343. * 5 callers the net result is about 100 bytes / caller.
  21344. */
  21345. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_iter_shared(duk_context *ctx) {
  21346. duk_uint32_t len;
  21347. duk_uint32_t i;
  21348. duk_uarridx_t k;
  21349. duk_bool_t bval;
  21350. duk_small_int_t iter_type = duk_get_current_magic(ctx);
  21351. duk_uint32_t res_length = 0;
  21352. /* each call this helper serves has nargs==2 */
  21353. DUK_ASSERT_TOP(ctx, 2);
  21354. len = duk__push_this_obj_len_u32(ctx);
  21355. if (!duk_is_callable(ctx, 0)) {
  21356. goto type_error;
  21357. }
  21358. /* if thisArg not supplied, behave as if undefined was supplied */
  21359. if (iter_type == DUK__ITER_MAP || iter_type == DUK__ITER_FILTER) {
  21360. duk_push_array(ctx);
  21361. } else {
  21362. duk_push_undefined(ctx);
  21363. }
  21364. /* stack[0] = callback
  21365. * stack[1] = thisArg
  21366. * stack[2] = object
  21367. * stack[3] = ToUint32(length) (unused, but avoid unnecessary pop)
  21368. * stack[4] = result array (or undefined)
  21369. */
  21370. k = 0; /* result index for filter() */
  21371. for (i = 0; i < len; i++) {
  21372. DUK_ASSERT_TOP(ctx, 5);
  21373. if (!duk_get_prop_index(ctx, 2, (duk_uarridx_t) i)) {
  21374. #if defined(DUK_USE_NONSTD_ARRAY_MAP_TRAILER)
  21375. /* Real world behavior for map(): trailing non-existent
  21376. * elements don't invoke the user callback, but are still
  21377. * counted towards result 'length'.
  21378. */
  21379. if (iter_type == DUK__ITER_MAP) {
  21380. res_length = i + 1;
  21381. }
  21382. #else
  21383. /* Standard behavior for map(): trailing non-existent
  21384. * elements don't invoke the user callback and are not
  21385. * counted towards result 'length'.
  21386. */
  21387. #endif
  21388. duk_pop(ctx);
  21389. continue;
  21390. }
  21391. /* The original value needs to be preserved for filter(), hence
  21392. * this funny order. We can't re-get the value because of side
  21393. * effects.
  21394. */
  21395. duk_dup(ctx, 0);
  21396. duk_dup(ctx, 1);
  21397. duk_dup(ctx, -3);
  21398. duk_push_u32(ctx, i);
  21399. duk_dup(ctx, 2); /* [ ... val callback thisArg val i obj ] */
  21400. duk_call_method(ctx, 3); /* -> [ ... val retval ] */
  21401. switch (iter_type) {
  21402. case DUK__ITER_EVERY:
  21403. bval = duk_to_boolean(ctx, -1);
  21404. if (!bval) {
  21405. /* stack top contains 'false' */
  21406. return 1;
  21407. }
  21408. break;
  21409. case DUK__ITER_SOME:
  21410. bval = duk_to_boolean(ctx, -1);
  21411. if (bval) {
  21412. /* stack top contains 'true' */
  21413. return 1;
  21414. }
  21415. break;
  21416. case DUK__ITER_FOREACH:
  21417. /* nop */
  21418. break;
  21419. case DUK__ITER_MAP:
  21420. duk_dup(ctx, -1);
  21421. duk_xdef_prop_index_wec(ctx, 4, (duk_uarridx_t) i); /* retval to result[i] */
  21422. res_length = i + 1;
  21423. break;
  21424. case DUK__ITER_FILTER:
  21425. bval = duk_to_boolean(ctx, -1);
  21426. if (bval) {
  21427. duk_dup(ctx, -2); /* orig value */
  21428. duk_xdef_prop_index_wec(ctx, 4, (duk_uarridx_t) k);
  21429. k++;
  21430. res_length = k;
  21431. }
  21432. break;
  21433. default:
  21434. DUK_UNREACHABLE();
  21435. break;
  21436. }
  21437. duk_pop_2(ctx);
  21438. DUK_ASSERT_TOP(ctx, 5);
  21439. }
  21440. switch (iter_type) {
  21441. case DUK__ITER_EVERY:
  21442. duk_push_true(ctx);
  21443. break;
  21444. case DUK__ITER_SOME:
  21445. duk_push_false(ctx);
  21446. break;
  21447. case DUK__ITER_FOREACH:
  21448. duk_push_undefined(ctx);
  21449. break;
  21450. case DUK__ITER_MAP:
  21451. case DUK__ITER_FILTER:
  21452. DUK_ASSERT_TOP(ctx, 5);
  21453. DUK_ASSERT(duk_is_array(ctx, -1)); /* topmost element is the result array already */
  21454. duk_push_u32(ctx, res_length);
  21455. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W);
  21456. break;
  21457. default:
  21458. DUK_UNREACHABLE();
  21459. break;
  21460. }
  21461. return 1;
  21462. type_error:
  21463. return DUK_RET_TYPE_ERROR;
  21464. }
  21465. /*
  21466. * reduce(), reduceRight()
  21467. */
  21468. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_reduce_shared(duk_context *ctx) {
  21469. duk_idx_t nargs;
  21470. duk_bool_t have_acc;
  21471. duk_uint32_t i, len;
  21472. duk_small_int_t idx_step = duk_get_current_magic(ctx); /* idx_step is +1 for reduce, -1 for reduceRight */
  21473. /* We're a varargs function because we need to detect whether
  21474. * initialValue was given or not.
  21475. */
  21476. nargs = duk_get_top(ctx);
  21477. DUK_DDD(DUK_DDDPRINT("nargs=%ld", (long) nargs));
  21478. duk_set_top(ctx, 2);
  21479. len = duk__push_this_obj_len_u32(ctx);
  21480. if (!duk_is_callable(ctx, 0)) {
  21481. goto type_error;
  21482. }
  21483. /* stack[0] = callback fn
  21484. * stack[1] = initialValue
  21485. * stack[2] = object (coerced this)
  21486. * stack[3] = length (not needed, but not popped above)
  21487. * stack[4] = accumulator
  21488. */
  21489. have_acc = 0;
  21490. if (nargs >= 2) {
  21491. duk_dup(ctx, 1);
  21492. have_acc = 1;
  21493. }
  21494. DUK_DDD(DUK_DDDPRINT("have_acc=%ld, acc=%!T",
  21495. (long) have_acc, (duk_tval *) duk_get_tval(ctx, 3)));
  21496. /* For len == 0, i is initialized to len - 1 which underflows.
  21497. * The condition (i < len) will then exit the for-loop on the
  21498. * first round which is correct. Similarly, loop termination
  21499. * happens by i underflowing.
  21500. */
  21501. for (i = (idx_step >= 0 ? 0 : len - 1);
  21502. i < len; /* i >= 0 would always be true */
  21503. i += idx_step) {
  21504. DUK_DDD(DUK_DDDPRINT("i=%ld, len=%ld, have_acc=%ld, top=%ld, acc=%!T",
  21505. (long) i, (long) len, (long) have_acc,
  21506. (long) duk_get_top(ctx),
  21507. (duk_tval *) duk_get_tval(ctx, 4)));
  21508. DUK_ASSERT((have_acc && duk_get_top(ctx) == 5) ||
  21509. (!have_acc && duk_get_top(ctx) == 4));
  21510. if (!duk_has_prop_index(ctx, 2, (duk_uarridx_t) i)) {
  21511. continue;
  21512. }
  21513. if (!have_acc) {
  21514. DUK_ASSERT_TOP(ctx, 4);
  21515. duk_get_prop_index(ctx, 2, (duk_uarridx_t) i);
  21516. have_acc = 1;
  21517. DUK_ASSERT_TOP(ctx, 5);
  21518. } else {
  21519. DUK_ASSERT_TOP(ctx, 5);
  21520. duk_dup(ctx, 0);
  21521. duk_dup(ctx, 4);
  21522. duk_get_prop_index(ctx, 2, (duk_uarridx_t) i);
  21523. duk_push_u32(ctx, i);
  21524. duk_dup(ctx, 2);
  21525. DUK_DDD(DUK_DDDPRINT("calling reduce function: func=%!T, prev=%!T, curr=%!T, idx=%!T, obj=%!T",
  21526. (duk_tval *) duk_get_tval(ctx, -5), (duk_tval *) duk_get_tval(ctx, -4),
  21527. (duk_tval *) duk_get_tval(ctx, -3), (duk_tval *) duk_get_tval(ctx, -2),
  21528. (duk_tval *) duk_get_tval(ctx, -1)));
  21529. duk_call(ctx, 4);
  21530. DUK_DDD(DUK_DDDPRINT("-> result: %!T", (duk_tval *) duk_get_tval(ctx, -1)));
  21531. duk_replace(ctx, 4);
  21532. DUK_ASSERT_TOP(ctx, 5);
  21533. }
  21534. }
  21535. if (!have_acc) {
  21536. goto type_error;
  21537. }
  21538. DUK_ASSERT_TOP(ctx, 5);
  21539. return 1;
  21540. type_error:
  21541. return DUK_RET_TYPE_ERROR;
  21542. }
  21543. #undef DUK__ARRAY_MID_JOIN_LIMIT
  21544. #undef DUK__ITER_EVERY
  21545. #undef DUK__ITER_SOME
  21546. #undef DUK__ITER_FOREACH
  21547. #undef DUK__ITER_MAP
  21548. #undef DUK__ITER_FILTER
  21549. #line 1 "duk_bi_boolean.c"
  21550. /*
  21551. * Boolean built-ins
  21552. */
  21553. /* include removed: duk_internal.h */
  21554. /* Shared helper to provide toString() and valueOf(). Checks 'this', gets
  21555. * the primitive value to stack top, and optionally coerces with ToString().
  21556. */
  21557. DUK_INTERNAL duk_ret_t duk_bi_boolean_prototype_tostring_shared(duk_context *ctx) {
  21558. duk_tval *tv;
  21559. duk_hobject *h;
  21560. duk_small_int_t coerce_tostring = duk_get_current_magic(ctx);
  21561. /* XXX: there is room to use a shared helper here, many built-ins
  21562. * check the 'this' type, and if it's an object, check its class,
  21563. * then get its internal value, etc.
  21564. */
  21565. duk_push_this(ctx);
  21566. tv = duk_get_tval(ctx, -1);
  21567. DUK_ASSERT(tv != NULL);
  21568. if (DUK_TVAL_IS_BOOLEAN(tv)) {
  21569. goto type_ok;
  21570. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  21571. h = DUK_TVAL_GET_OBJECT(tv);
  21572. DUK_ASSERT(h != NULL);
  21573. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_BOOLEAN) {
  21574. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE);
  21575. DUK_ASSERT(duk_is_boolean(ctx, -1));
  21576. goto type_ok;
  21577. }
  21578. }
  21579. return DUK_RET_TYPE_ERROR;
  21580. type_ok:
  21581. if (coerce_tostring) {
  21582. duk_to_string(ctx, -1);
  21583. }
  21584. return 1;
  21585. }
  21586. DUK_INTERNAL duk_ret_t duk_bi_boolean_constructor(duk_context *ctx) {
  21587. duk_hthread *thr = (duk_hthread *) ctx;
  21588. duk_hobject *h_this;
  21589. DUK_UNREF(thr);
  21590. duk_to_boolean(ctx, 0);
  21591. if (duk_is_constructor_call(ctx)) {
  21592. /* XXX: helper; rely on Boolean.prototype as being non-writable, non-configurable */
  21593. duk_push_this(ctx);
  21594. h_this = duk_get_hobject(ctx, -1);
  21595. DUK_ASSERT(h_this != NULL);
  21596. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_this) == thr->builtins[DUK_BIDX_BOOLEAN_PROTOTYPE]);
  21597. DUK_HOBJECT_SET_CLASS_NUMBER(h_this, DUK_HOBJECT_CLASS_BOOLEAN);
  21598. duk_dup(ctx, 0); /* -> [ val obj val ] */
  21599. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE); /* XXX: proper flags? */
  21600. } /* unbalanced stack */
  21601. return 1;
  21602. }
  21603. #line 1 "duk_bi_buffer.c"
  21604. /*
  21605. * Duktape.Buffer, Node.js Buffer, and Khronos/ES6 TypedArray built-ins
  21606. */
  21607. /* include removed: duk_internal.h */
  21608. /*
  21609. * Misc helpers
  21610. */
  21611. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  21612. /* Map DUK_HBUFFEROBJECT_ELEM_xxx to duk_hobject class number.
  21613. * Sync with duk_hbufferobject.h and duk_hobject.h.
  21614. */
  21615. static const duk_uint8_t duk__buffer_class_from_elemtype[9] = {
  21616. DUK_HOBJECT_CLASS_UINT8ARRAY,
  21617. DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY,
  21618. DUK_HOBJECT_CLASS_INT8ARRAY,
  21619. DUK_HOBJECT_CLASS_UINT16ARRAY,
  21620. DUK_HOBJECT_CLASS_INT16ARRAY,
  21621. DUK_HOBJECT_CLASS_UINT32ARRAY,
  21622. DUK_HOBJECT_CLASS_INT32ARRAY,
  21623. DUK_HOBJECT_CLASS_FLOAT32ARRAY,
  21624. DUK_HOBJECT_CLASS_FLOAT64ARRAY
  21625. };
  21626. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  21627. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  21628. /* Map DUK_HBUFFEROBJECT_ELEM_xxx to prototype object built-in index.
  21629. * Sync with duk_hbufferobject.h.
  21630. */
  21631. static const duk_uint8_t duk__buffer_proto_from_elemtype[9] = {
  21632. DUK_BIDX_UINT8ARRAY_PROTOTYPE,
  21633. DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE,
  21634. DUK_BIDX_INT8ARRAY_PROTOTYPE,
  21635. DUK_BIDX_UINT16ARRAY_PROTOTYPE,
  21636. DUK_BIDX_INT16ARRAY_PROTOTYPE,
  21637. DUK_BIDX_UINT32ARRAY_PROTOTYPE,
  21638. DUK_BIDX_INT32ARRAY_PROTOTYPE,
  21639. DUK_BIDX_FLOAT32ARRAY_PROTOTYPE,
  21640. DUK_BIDX_FLOAT64ARRAY_PROTOTYPE
  21641. };
  21642. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  21643. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  21644. /* Map DUK__FLX_xxx to byte size.
  21645. */
  21646. static const duk_uint8_t duk__buffer_nbytes_from_fldtype[6] = {
  21647. 1, /* DUK__FLD_8BIT */
  21648. 2, /* DUK__FLD_16BIT */
  21649. 4, /* DUK__FLD_32BIT */
  21650. 4, /* DUK__FLD_FLOAT */
  21651. 8, /* DUK__FLD_DOUBLE */
  21652. 0 /* DUK__FLD_VARINT; not relevant here */
  21653. };
  21654. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  21655. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  21656. /* Bitfield for each DUK_HBUFFEROBJECT_ELEM_xxx indicating which element types
  21657. * are compatible with a blind byte copy for the TypedArray set() method (also
  21658. * used for TypedArray constructor). Array index is target buffer elem type,
  21659. * bitfield indicates compatible source types. The types must have same byte
  21660. * size and they must be coercion compatible.
  21661. */
  21662. static duk_uint16_t duk__buffer_elemtype_copy_compatible[9] = {
  21663. /* xxx -> DUK_HBUFFEROBJECT_ELEM_UINT8 */
  21664. (1U << DUK_HBUFFEROBJECT_ELEM_UINT8) |
  21665. (1U << DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED) |
  21666. (1U << DUK_HBUFFEROBJECT_ELEM_INT8),
  21667. /* xxx -> DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED
  21668. * Note: INT8 is -not- copy compatible, e.g. -1 would coerce to 0x00.
  21669. */
  21670. (1U << DUK_HBUFFEROBJECT_ELEM_UINT8) |
  21671. (1U << DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED),
  21672. /* xxx -> DUK_HBUFFEROBJECT_ELEM_INT8 */
  21673. (1U << DUK_HBUFFEROBJECT_ELEM_UINT8) |
  21674. (1U << DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED) |
  21675. (1U << DUK_HBUFFEROBJECT_ELEM_INT8),
  21676. /* xxx -> DUK_HBUFFEROBJECT_ELEM_UINT16 */
  21677. (1U << DUK_HBUFFEROBJECT_ELEM_UINT16) |
  21678. (1U << DUK_HBUFFEROBJECT_ELEM_INT16),
  21679. /* xxx -> DUK_HBUFFEROBJECT_ELEM_INT16 */
  21680. (1U << DUK_HBUFFEROBJECT_ELEM_UINT16) |
  21681. (1U << DUK_HBUFFEROBJECT_ELEM_INT16),
  21682. /* xxx -> DUK_HBUFFEROBJECT_ELEM_UINT32 */
  21683. (1U << DUK_HBUFFEROBJECT_ELEM_UINT32) |
  21684. (1U << DUK_HBUFFEROBJECT_ELEM_INT32),
  21685. /* xxx -> DUK_HBUFFEROBJECT_ELEM_INT32 */
  21686. (1U << DUK_HBUFFEROBJECT_ELEM_UINT32) |
  21687. (1U << DUK_HBUFFEROBJECT_ELEM_INT32),
  21688. /* xxx -> DUK_HBUFFEROBJECT_ELEM_FLOAT32 */
  21689. (1U << DUK_HBUFFEROBJECT_ELEM_FLOAT32),
  21690. /* xxx -> DUK_HBUFFEROBJECT_ELEM_FLOAT64 */
  21691. (1U << DUK_HBUFFEROBJECT_ELEM_FLOAT64)
  21692. };
  21693. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  21694. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  21695. /* Shared helper. */
  21696. DUK_LOCAL duk_hbufferobject *duk__getrequire_bufobj_this(duk_context *ctx, duk_bool_t throw_flag) {
  21697. duk_hthread *thr;
  21698. duk_tval *tv;
  21699. duk_hbufferobject *h_this;
  21700. DUK_ASSERT(ctx != NULL);
  21701. thr = (duk_hthread *) ctx;
  21702. tv = duk_get_borrowed_this_tval(ctx);
  21703. DUK_ASSERT(tv != NULL);
  21704. if (DUK_TVAL_IS_OBJECT(tv)) {
  21705. h_this = (duk_hbufferobject *) DUK_TVAL_GET_OBJECT(tv);
  21706. DUK_ASSERT(h_this != NULL);
  21707. if (DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) h_this)) {
  21708. DUK_ASSERT_HBUFFEROBJECT_VALID(h_this);
  21709. return h_this;
  21710. }
  21711. }
  21712. if (throw_flag) {
  21713. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_BUFFER);
  21714. }
  21715. return NULL;
  21716. }
  21717. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  21718. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  21719. /* Check that 'this' is a duk_hbufferobject and return a pointer to it. */
  21720. DUK_LOCAL duk_hbufferobject *duk__get_bufobj_this(duk_context *ctx) {
  21721. return duk__getrequire_bufobj_this(ctx, 0);
  21722. }
  21723. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  21724. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  21725. /* Check that 'this' is a duk_hbufferobject and return a pointer to it
  21726. * (NULL if not).
  21727. */
  21728. DUK_LOCAL duk_hbufferobject *duk__require_bufobj_this(duk_context *ctx) {
  21729. return duk__getrequire_bufobj_this(ctx, 1);
  21730. }
  21731. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  21732. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  21733. /* Check that value is a duk_hbufferobject and return a pointer to it. */
  21734. DUK_LOCAL duk_hbufferobject *duk__require_bufobj_value(duk_context *ctx, duk_idx_t index) {
  21735. duk_hthread *thr;
  21736. duk_tval *tv;
  21737. duk_hbufferobject *h_obj;
  21738. thr = (duk_hthread *) ctx;
  21739. /* Don't accept relative indices now. */
  21740. DUK_ASSERT(index >= 0);
  21741. tv = duk_require_tval(ctx, index);
  21742. DUK_ASSERT(tv != NULL);
  21743. if (DUK_TVAL_IS_OBJECT(tv)) {
  21744. h_obj = (duk_hbufferobject *) DUK_TVAL_GET_OBJECT(tv);
  21745. DUK_ASSERT(h_obj != NULL);
  21746. if (DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) h_obj)) {
  21747. DUK_ASSERT_HBUFFEROBJECT_VALID(h_obj);
  21748. return h_obj;
  21749. }
  21750. }
  21751. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_BUFFER);
  21752. return NULL; /* not reachable */
  21753. }
  21754. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  21755. DUK_LOCAL void duk__set_bufobj_buffer(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_hbuffer *h_val) {
  21756. duk_hthread *thr;
  21757. thr = (duk_hthread *) ctx;
  21758. DUK_UNREF(thr);
  21759. DUK_ASSERT(ctx != NULL);
  21760. DUK_ASSERT(h_bufobj != NULL);
  21761. DUK_ASSERT(h_bufobj->buf == NULL); /* no need to decref */
  21762. DUK_ASSERT(h_val != NULL);
  21763. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  21764. h_bufobj->buf = h_val;
  21765. DUK_HBUFFER_INCREF(thr, h_val);
  21766. h_bufobj->length = (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_val);
  21767. DUK_ASSERT(h_bufobj->shift == 0);
  21768. DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8);
  21769. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  21770. }
  21771. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  21772. DUK_LOCAL duk_hbufferobject *duk__push_arraybuffer_with_length(duk_context *ctx, duk_uint_t len) {
  21773. duk_hbuffer *h_val;
  21774. duk_hbufferobject *h_bufobj;
  21775. (void) duk_push_fixed_buffer(ctx, (duk_size_t) len);
  21776. h_val = (duk_hbuffer *) duk_get_hbuffer(ctx, -1);
  21777. DUK_ASSERT(h_val != NULL);
  21778. h_bufobj = duk_push_bufferobject_raw(ctx,
  21779. DUK_HOBJECT_FLAG_EXTENSIBLE |
  21780. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  21781. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAYBUFFER),
  21782. DUK_BIDX_ARRAYBUFFER_PROTOTYPE);
  21783. DUK_ASSERT(h_bufobj != NULL);
  21784. duk__set_bufobj_buffer(ctx, h_bufobj, h_val);
  21785. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  21786. return h_bufobj;
  21787. }
  21788. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  21789. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  21790. /* Shared offset/length coercion helper. */
  21791. DUK_LOCAL void duk__resolve_offset_opt_length(duk_context *ctx,
  21792. duk_hbufferobject *h_bufarg,
  21793. duk_idx_t idx_offset,
  21794. duk_idx_t idx_length,
  21795. duk_uint_t *out_offset,
  21796. duk_uint_t *out_length,
  21797. duk_bool_t throw_flag) {
  21798. duk_hthread *thr;
  21799. duk_int_t offset_signed;
  21800. duk_int_t length_signed;
  21801. duk_uint_t offset;
  21802. duk_uint_t length;
  21803. thr = (duk_hthread *) ctx;
  21804. DUK_UNREF(thr);
  21805. offset_signed = duk_to_int(ctx, idx_offset);
  21806. if (offset_signed < 0) {
  21807. goto fail_range;
  21808. }
  21809. offset = (duk_uint_t) offset_signed;
  21810. if (offset > h_bufarg->length) {
  21811. goto fail_range;
  21812. }
  21813. DUK_ASSERT_DISABLE(offset >= 0); /* unsigned */
  21814. DUK_ASSERT(offset <= h_bufarg->length);
  21815. if (duk_is_undefined(ctx, idx_length)) {
  21816. DUK_ASSERT(h_bufarg->length >= offset);
  21817. length = h_bufarg->length - offset; /* >= 0 */
  21818. } else {
  21819. length_signed = duk_to_int(ctx, idx_length);
  21820. if (length_signed < 0) {
  21821. goto fail_range;
  21822. }
  21823. length = (duk_uint_t) length_signed;
  21824. DUK_ASSERT(h_bufarg->length >= offset);
  21825. if (length > h_bufarg->length - offset) {
  21826. /* Unlike for negative arguments, some call sites
  21827. * want length to be clamped if it's positive.
  21828. */
  21829. if (throw_flag) {
  21830. goto fail_range;
  21831. } else {
  21832. length = h_bufarg->length - offset;
  21833. }
  21834. }
  21835. }
  21836. DUK_ASSERT_DISABLE(length >= 0); /* unsigned */
  21837. DUK_ASSERT(offset + length <= h_bufarg->length);
  21838. *out_offset = offset;
  21839. *out_length = length;
  21840. return;
  21841. fail_range:
  21842. duk_error(thr, DUK_ERR_RANGE_ERROR, DUK_STR_INVALID_CALL_ARGS);
  21843. }
  21844. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  21845. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  21846. /* Shared lenient buffer length clamping helper. No negative indices, no
  21847. * element/byte shifting.
  21848. */
  21849. DUK_LOCAL void duk__clamp_startend_nonegidx_noshift(duk_context *ctx,
  21850. duk_hbufferobject *h_bufobj,
  21851. duk_idx_t idx_start,
  21852. duk_idx_t idx_end,
  21853. duk_int_t *out_start_offset,
  21854. duk_int_t *out_end_offset) {
  21855. duk_int_t buffer_length;
  21856. duk_int_t start_offset;
  21857. duk_int_t end_offset;
  21858. DUK_ASSERT(out_start_offset != NULL);
  21859. DUK_ASSERT(out_end_offset != NULL);
  21860. buffer_length = (duk_int_t) h_bufobj->length;
  21861. /* undefined coerces to zero which is correct */
  21862. start_offset = duk_to_int_clamped(ctx, idx_start, 0, buffer_length);
  21863. if (duk_is_undefined(ctx, idx_end)) {
  21864. end_offset = buffer_length;
  21865. } else {
  21866. end_offset = duk_to_int_clamped(ctx, idx_end, start_offset, buffer_length);
  21867. }
  21868. DUK_ASSERT(start_offset >= 0);
  21869. DUK_ASSERT(start_offset <= buffer_length);
  21870. DUK_ASSERT(end_offset >= 0);
  21871. DUK_ASSERT(end_offset <= buffer_length);
  21872. DUK_ASSERT(start_offset <= end_offset);
  21873. *out_start_offset = start_offset;
  21874. *out_end_offset = end_offset;
  21875. }
  21876. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  21877. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  21878. /* Shared lenient buffer length clamping helper. Indices are treated as
  21879. * element indices (though output values are byte offsets) which only
  21880. * really matters for TypedArray views as other buffer object have a zero
  21881. * shift. Negative indices are counted from end of input slice; crossed
  21882. * indices are clamped to zero length; and final indices are clamped
  21883. * against input slice. Used for e.g. ArrayBuffer slice().
  21884. */
  21885. DUK_LOCAL void duk__clamp_startend_negidx_shifted(duk_context *ctx,
  21886. duk_hbufferobject *h_bufobj,
  21887. duk_idx_t idx_start,
  21888. duk_idx_t idx_end,
  21889. duk_int_t *out_start_offset,
  21890. duk_int_t *out_end_offset) {
  21891. duk_int_t buffer_length;
  21892. duk_int_t start_offset;
  21893. duk_int_t end_offset;
  21894. DUK_ASSERT(out_start_offset != NULL);
  21895. DUK_ASSERT(out_end_offset != NULL);
  21896. buffer_length = (duk_int_t) h_bufobj->length;
  21897. buffer_length >>= h_bufobj->shift; /* as elements */
  21898. /* Resolve start/end offset as element indices first; arguments
  21899. * at idx_start/idx_end are element offsets. Working with element
  21900. * indices first also avoids potential for wrapping.
  21901. */
  21902. start_offset = duk_to_int(ctx, idx_start);
  21903. if (start_offset < 0) {
  21904. start_offset = buffer_length + start_offset;
  21905. }
  21906. if (duk_is_undefined(ctx, idx_end)) {
  21907. end_offset = buffer_length;
  21908. } else {
  21909. end_offset = duk_to_int(ctx, idx_end);
  21910. if (end_offset < 0) {
  21911. end_offset = buffer_length + end_offset;
  21912. }
  21913. }
  21914. /* Note: start_offset/end_offset can still be < 0 here. */
  21915. if (start_offset < 0) {
  21916. start_offset = 0;
  21917. } else if (start_offset > buffer_length) {
  21918. start_offset = buffer_length;
  21919. }
  21920. if (end_offset < start_offset) {
  21921. end_offset = start_offset;
  21922. } else if (end_offset > buffer_length) {
  21923. end_offset = buffer_length;
  21924. }
  21925. DUK_ASSERT(start_offset >= 0);
  21926. DUK_ASSERT(start_offset <= buffer_length);
  21927. DUK_ASSERT(end_offset >= 0);
  21928. DUK_ASSERT(end_offset <= buffer_length);
  21929. DUK_ASSERT(start_offset <= end_offset);
  21930. /* Convert indices to byte offsets. */
  21931. start_offset <<= h_bufobj->shift;
  21932. end_offset <<= h_bufobj->shift;
  21933. *out_start_offset = start_offset;
  21934. *out_end_offset = end_offset;
  21935. }
  21936. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  21937. /*
  21938. * Indexed read/write helpers (also used from outside this file)
  21939. */
  21940. DUK_INTERNAL void duk_hbufferobject_push_validated_read(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size) {
  21941. duk_double_union du;
  21942. DUK_MEMCPY((void *) du.uc, (const void *) p, (size_t) elem_size);
  21943. switch (h_bufobj->elem_type) {
  21944. case DUK_HBUFFEROBJECT_ELEM_UINT8:
  21945. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  21946. case DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED:
  21947. #endif
  21948. duk_push_uint(ctx, (duk_uint_t) du.uc[0]);
  21949. break;
  21950. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  21951. /* These are not needed when only Duktape.Buffer is supported. */
  21952. case DUK_HBUFFEROBJECT_ELEM_INT8:
  21953. duk_push_int(ctx, (duk_int_t) (duk_int8_t) du.uc[0]);
  21954. break;
  21955. case DUK_HBUFFEROBJECT_ELEM_UINT16:
  21956. duk_push_uint(ctx, (duk_uint_t) du.us[0]);
  21957. break;
  21958. case DUK_HBUFFEROBJECT_ELEM_INT16:
  21959. duk_push_int(ctx, (duk_int_t) (duk_int16_t) du.us[0]);
  21960. break;
  21961. case DUK_HBUFFEROBJECT_ELEM_UINT32:
  21962. duk_push_uint(ctx, (duk_uint_t) du.ui[0]);
  21963. break;
  21964. case DUK_HBUFFEROBJECT_ELEM_INT32:
  21965. duk_push_int(ctx, (duk_int_t) (duk_int32_t) du.ui[0]);
  21966. break;
  21967. case DUK_HBUFFEROBJECT_ELEM_FLOAT32:
  21968. duk_push_number(ctx, (duk_double_t) du.f[0]);
  21969. break;
  21970. case DUK_HBUFFEROBJECT_ELEM_FLOAT64:
  21971. duk_push_number(ctx, (duk_double_t) du.d);
  21972. break;
  21973. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  21974. default:
  21975. DUK_UNREACHABLE();
  21976. }
  21977. }
  21978. DUK_INTERNAL void duk_hbufferobject_validated_write(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size) {
  21979. duk_double_union du;
  21980. /* NOTE! Caller must ensure that any side effects from the
  21981. * coercions below are safe. If that cannot be guaranteed
  21982. * (which is normally the case), caller must coerce the
  21983. * argument using duk_to_number() before any pointer
  21984. * validations; the result of duk_to_number() always coerces
  21985. * without side effects here.
  21986. */
  21987. switch (h_bufobj->elem_type) {
  21988. case DUK_HBUFFEROBJECT_ELEM_UINT8:
  21989. du.uc[0] = (duk_uint8_t) duk_to_uint32(ctx, -1);
  21990. break;
  21991. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  21992. /* These are not needed when only Duktape.Buffer is supported. */
  21993. case DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED:
  21994. du.uc[0] = (duk_uint8_t) duk_to_uint8clamped(ctx, -1);
  21995. break;
  21996. case DUK_HBUFFEROBJECT_ELEM_INT8:
  21997. du.uc[0] = (duk_uint8_t) duk_to_int32(ctx, -1);
  21998. break;
  21999. case DUK_HBUFFEROBJECT_ELEM_UINT16:
  22000. du.us[0] = (duk_uint16_t) duk_to_uint32(ctx, -1);
  22001. break;
  22002. case DUK_HBUFFEROBJECT_ELEM_INT16:
  22003. du.us[0] = (duk_uint16_t) duk_to_int32(ctx, -1);
  22004. break;
  22005. case DUK_HBUFFEROBJECT_ELEM_UINT32:
  22006. du.ui[0] = (duk_uint32_t) duk_to_uint32(ctx, -1);
  22007. break;
  22008. case DUK_HBUFFEROBJECT_ELEM_INT32:
  22009. du.ui[0] = (duk_uint32_t) duk_to_int32(ctx, -1);
  22010. break;
  22011. case DUK_HBUFFEROBJECT_ELEM_FLOAT32:
  22012. du.f[0] = (duk_float_t) duk_to_number(ctx, -1);
  22013. break;
  22014. case DUK_HBUFFEROBJECT_ELEM_FLOAT64:
  22015. du.d = (duk_double_t) duk_to_number(ctx, -1);
  22016. break;
  22017. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22018. default:
  22019. DUK_UNREACHABLE();
  22020. }
  22021. DUK_MEMCPY((void *) p, (const void *) du.uc, (size_t) elem_size);
  22022. }
  22023. /*
  22024. * Duktape.Buffer: constructor
  22025. */
  22026. DUK_INTERNAL duk_ret_t duk_bi_buffer_constructor(duk_context *ctx) {
  22027. duk_hthread *thr;
  22028. duk_size_t buf_size;
  22029. duk_small_int_t buf_dynamic;
  22030. duk_uint8_t *buf_data;
  22031. const duk_uint8_t *src_data;
  22032. thr = (duk_hthread *) ctx;
  22033. DUK_UNREF(thr);
  22034. /*
  22035. * Constructor arguments are currently somewhat compatible with
  22036. * (keep it that way if possible):
  22037. *
  22038. * http://nodejs.org/api/buffer.html
  22039. *
  22040. * Note that the ToBuffer() coercion (duk_to_buffer()) does NOT match
  22041. * the constructor behavior.
  22042. */
  22043. buf_dynamic = duk_get_boolean(ctx, 1); /* default to false */
  22044. switch (duk_get_type(ctx, 0)) {
  22045. case DUK_TYPE_NUMBER: {
  22046. /* new buffer of specified size */
  22047. buf_size = (duk_size_t) duk_to_int(ctx, 0);
  22048. (void) duk_push_buffer(ctx, buf_size, buf_dynamic);
  22049. break;
  22050. }
  22051. case DUK_TYPE_BUFFER: {
  22052. /* return input buffer, converted to a Duktape.Buffer object
  22053. * if called as a constructor (no change if called as a
  22054. * function).
  22055. */
  22056. duk_set_top(ctx, 1);
  22057. break;
  22058. }
  22059. case DUK_TYPE_STRING: {
  22060. /* new buffer with string contents */
  22061. src_data = (const duk_uint8_t *) duk_get_lstring(ctx, 0, &buf_size);
  22062. DUK_ASSERT(src_data != NULL); /* even for zero-length string */
  22063. buf_data = (duk_uint8_t *) duk_push_buffer(ctx, buf_size, buf_dynamic);
  22064. DUK_MEMCPY((void *) buf_data, (const void *) src_data, (size_t) buf_size);
  22065. break;
  22066. }
  22067. case DUK_TYPE_OBJECT: {
  22068. /* For all duk_hbufferobjects, get the plain buffer inside
  22069. * without making a copy. This is compatible with Duktape 1.2
  22070. * but means that a slice/view information is ignored and the
  22071. * full underlying buffer is returned.
  22072. *
  22073. * If called as a constructor, a new Duktape.Buffer object
  22074. * pointing to the same plain buffer is created below.
  22075. */
  22076. duk_hbufferobject *h_bufobj;
  22077. h_bufobj = (duk_hbufferobject *) duk_get_hobject(ctx, 0);
  22078. DUK_ASSERT(h_bufobj != NULL);
  22079. if (!DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) h_bufobj)) {
  22080. return DUK_RET_TYPE_ERROR;
  22081. }
  22082. if (h_bufobj->buf == NULL) {
  22083. return DUK_RET_TYPE_ERROR;
  22084. }
  22085. duk_push_hbuffer(ctx, h_bufobj->buf);
  22086. break;
  22087. }
  22088. case DUK_TYPE_NONE:
  22089. default: {
  22090. return DUK_RET_TYPE_ERROR;
  22091. }
  22092. }
  22093. DUK_ASSERT(duk_is_buffer(ctx, -1));
  22094. /* stack is unbalanced, but: [ <something> buf ] */
  22095. if (duk_is_constructor_call(ctx)) {
  22096. duk_hbufferobject *h_bufobj;
  22097. duk_hbuffer *h_val;
  22098. h_val = duk_get_hbuffer(ctx, -1);
  22099. DUK_ASSERT(h_val != NULL);
  22100. h_bufobj = duk_push_bufferobject_raw(ctx,
  22101. DUK_HOBJECT_FLAG_EXTENSIBLE |
  22102. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  22103. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER),
  22104. DUK_BIDX_BUFFER_PROTOTYPE);
  22105. DUK_ASSERT(h_bufobj != NULL);
  22106. duk__set_bufobj_buffer(ctx, h_bufobj, h_val);
  22107. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  22108. }
  22109. /* Note: unbalanced stack on purpose */
  22110. return 1;
  22111. }
  22112. /*
  22113. * Node.js Buffer: constructor
  22114. */
  22115. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  22116. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_constructor(duk_context *ctx) {
  22117. /* Internal class is Object: Object.prototype.toString.call(new Buffer(0))
  22118. * prints "[object Object]".
  22119. */
  22120. duk_int_t len;
  22121. duk_int_t i;
  22122. duk_uint8_t *buf;
  22123. duk_hbuffer *h_buf;
  22124. duk_hbufferobject *h_bufobj;
  22125. duk_size_t buf_size;
  22126. switch (duk_get_type(ctx, 0)) {
  22127. case DUK_TYPE_BUFFER: {
  22128. /* Custom behavior: plain buffer is used as internal buffer
  22129. * without making a copy (matches Duktape.Buffer).
  22130. */
  22131. duk_set_top(ctx, 1); /* -> [ buffer ] */
  22132. break;
  22133. }
  22134. case DUK_TYPE_NUMBER: {
  22135. len = duk_to_int_clamped(ctx, 0, 0, DUK_INT_MAX);
  22136. buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) len);
  22137. break;
  22138. }
  22139. case DUK_TYPE_OBJECT: {
  22140. (void) duk_get_prop_string(ctx, 0, "length");
  22141. len = duk_to_int_clamped(ctx, -1, 0, DUK_INT_MAX);
  22142. duk_pop(ctx);
  22143. buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) len);
  22144. for (i = 0; i < len; i++) {
  22145. /* XXX: fast path for array arguments? */
  22146. duk_get_prop_index(ctx, 0, (duk_uarridx_t) i);
  22147. buf[i] = (duk_uint8_t) (duk_to_uint32(ctx, -1) & 0xffU);
  22148. duk_pop(ctx);
  22149. }
  22150. break;
  22151. }
  22152. case DUK_TYPE_STRING: {
  22153. /* ignore encoding for now */
  22154. duk_dup(ctx, 0);
  22155. buf = (duk_uint8_t *) duk_to_buffer(ctx, -1, &buf_size);
  22156. break;
  22157. }
  22158. default:
  22159. return DUK_RET_TYPE_ERROR;
  22160. }
  22161. DUK_ASSERT(duk_is_buffer(ctx, -1));
  22162. h_buf = duk_get_hbuffer(ctx, -1);
  22163. DUK_ASSERT(h_buf != NULL);
  22164. h_bufobj = duk_push_bufferobject_raw(ctx,
  22165. DUK_HOBJECT_FLAG_EXTENSIBLE |
  22166. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  22167. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER),
  22168. DUK_BIDX_NODEJS_BUFFER_PROTOTYPE);
  22169. DUK_ASSERT(h_bufobj != NULL);
  22170. h_bufobj->buf = h_buf;
  22171. DUK_HBUFFER_INCREF(thr, h_buf);
  22172. DUK_ASSERT(h_bufobj->offset == 0);
  22173. h_bufobj->length = (duk_int_t) DUK_HBUFFER_GET_SIZE(h_buf);
  22174. DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8);
  22175. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  22176. return 1;
  22177. }
  22178. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22179. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_constructor(duk_context *ctx) {
  22180. DUK_UNREF(ctx);
  22181. return DUK_RET_UNSUPPORTED_ERROR;
  22182. }
  22183. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22184. /*
  22185. * ArrayBuffer, DataView, and TypedArray constructors
  22186. */
  22187. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  22188. DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_constructor(duk_context *ctx) {
  22189. duk_hbufferobject *h_bufobj;
  22190. duk_hbuffer *h_val;
  22191. /* XXX: function flag to make this automatic? */
  22192. if (!duk_is_constructor_call(ctx)) {
  22193. return DUK_RET_TYPE_ERROR;
  22194. }
  22195. if (duk_is_buffer(ctx, 0)) {
  22196. /* Custom behavior: plain buffer is used as internal buffer
  22197. * without making a copy (matches Duktape.Buffer).
  22198. */
  22199. h_val = duk_get_hbuffer(ctx, 0);
  22200. DUK_ASSERT(h_val != NULL);
  22201. /* XXX: accept any duk_hbufferobject type as an input also? */
  22202. } else {
  22203. duk_int_t len;
  22204. len = duk_to_int(ctx, 0);
  22205. if (len < 0) {
  22206. goto fail_length;
  22207. }
  22208. (void) duk_push_fixed_buffer(ctx, (duk_size_t) len);
  22209. h_val = (duk_hbuffer *) duk_get_hbuffer(ctx, -1);
  22210. DUK_ASSERT(h_val != NULL);
  22211. }
  22212. h_bufobj = duk_push_bufferobject_raw(ctx,
  22213. DUK_HOBJECT_FLAG_EXTENSIBLE |
  22214. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  22215. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAYBUFFER),
  22216. DUK_BIDX_ARRAYBUFFER_PROTOTYPE);
  22217. DUK_ASSERT(h_bufobj != NULL);
  22218. duk__set_bufobj_buffer(ctx, h_bufobj, h_val);
  22219. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  22220. return 1;
  22221. fail_length:
  22222. return DUK_RET_RANGE_ERROR;
  22223. }
  22224. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22225. DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_constructor(duk_context *ctx) {
  22226. DUK_UNREF(ctx);
  22227. return DUK_RET_UNSUPPORTED_ERROR;
  22228. }
  22229. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22230. /* Format of magic, bits:
  22231. * 0...1: elem size shift (0-3)
  22232. * 2...5: elem type (DUK_HBUFFEROBJECT_ELEM_xxx)
  22233. */
  22234. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  22235. DUK_INTERNAL duk_ret_t duk_bi_typedarray_constructor(duk_context *ctx) {
  22236. duk_hthread *thr;
  22237. duk_tval *tv;
  22238. duk_hobject *h_obj;
  22239. duk_hbufferobject *h_bufobj = NULL;
  22240. duk_hbufferobject *h_bufarr = NULL;
  22241. duk_hbufferobject *h_bufarg = NULL;
  22242. duk_hbuffer *h_val;
  22243. duk_small_uint_t magic;
  22244. duk_small_uint_t shift;
  22245. duk_small_uint_t elem_type;
  22246. duk_small_uint_t elem_size;
  22247. duk_small_uint_t class_num;
  22248. duk_small_uint_t proto_bidx;
  22249. duk_uint_t align_mask;
  22250. duk_uint_t elem_length;
  22251. duk_int_t elem_length_signed;
  22252. duk_uint_t byte_length;
  22253. duk_small_uint_t copy_mode;
  22254. thr = (duk_hthread *) ctx;
  22255. DUK_UNREF(thr);
  22256. /* XXX: function flag to make this automatic? */
  22257. if (!duk_is_constructor_call(ctx)) {
  22258. return DUK_RET_TYPE_ERROR;
  22259. }
  22260. /* We could fit built-in index into magic but that'd make the magic
  22261. * number dependent on built-in numbering (genbuiltins.py doesn't
  22262. * handle that yet). So map both class and prototype from the
  22263. * element type.
  22264. */
  22265. magic = duk_get_current_magic(ctx);
  22266. shift = magic & 0x03; /* bits 0...1: shift */
  22267. elem_type = (magic >> 2) & 0x0f; /* bits 2...5: type */
  22268. elem_size = 1 << shift;
  22269. align_mask = elem_size - 1;
  22270. DUK_ASSERT(elem_type < sizeof(duk__buffer_proto_from_elemtype) / sizeof(duk_uint8_t));
  22271. proto_bidx = duk__buffer_proto_from_elemtype[elem_type];
  22272. DUK_ASSERT(proto_bidx < DUK_NUM_BUILTINS);
  22273. DUK_ASSERT(elem_type < sizeof(duk__buffer_class_from_elemtype) / sizeof(duk_uint8_t));
  22274. class_num = duk__buffer_class_from_elemtype[elem_type];
  22275. DUK_DD(DUK_DDPRINT("typedarray constructor, magic=%d, shift=%d, elem_type=%d, "
  22276. "elem_size=%d, proto_bidx=%d, class_num=%d",
  22277. (int) magic, (int) shift, (int) elem_type, (int) elem_size,
  22278. (int) proto_bidx, (int) class_num));
  22279. /* Argument variants. When the argument is an ArrayBuffer a view to
  22280. * the same buffer is created; otherwise a new ArrayBuffer is always
  22281. * created.
  22282. */
  22283. tv = duk_get_tval(ctx, 0);
  22284. DUK_ASSERT(tv != NULL); /* arg count */
  22285. if (DUK_TVAL_IS_OBJECT(tv)) {
  22286. h_obj = DUK_TVAL_GET_OBJECT(tv);
  22287. DUK_ASSERT(h_obj != NULL);
  22288. if (DUK_HOBJECT_GET_CLASS_NUMBER(h_obj) == DUK_HOBJECT_CLASS_ARRAYBUFFER) {
  22289. /* ArrayBuffer: unlike any other argument variant, create
  22290. * a view into the existing buffer.
  22291. */
  22292. duk_int_t byte_offset_signed;
  22293. duk_uint_t byte_offset;
  22294. h_bufarg = (duk_hbufferobject *) h_obj;
  22295. byte_offset_signed = duk_to_int(ctx, 1);
  22296. if (byte_offset_signed < 0) {
  22297. goto fail_arguments;
  22298. }
  22299. byte_offset = (duk_uint_t) byte_offset_signed;
  22300. if (byte_offset > h_bufarg->length ||
  22301. (byte_offset & align_mask) != 0) {
  22302. /* Must be >= 0 and multiple of element size. */
  22303. goto fail_arguments;
  22304. }
  22305. if (duk_is_undefined(ctx, 2)) {
  22306. DUK_ASSERT(h_bufarg->length >= byte_offset);
  22307. byte_length = h_bufarg->length - byte_offset;
  22308. if ((byte_length & align_mask) != 0) {
  22309. /* Must be element size multiple from
  22310. * start offset to end of buffer.
  22311. */
  22312. goto fail_arguments;
  22313. }
  22314. elem_length = (byte_length >> shift);
  22315. } else {
  22316. elem_length_signed = duk_to_int(ctx, 2);
  22317. if (elem_length_signed < 0) {
  22318. goto fail_arguments;
  22319. }
  22320. elem_length = (duk_uint_t) elem_length_signed;
  22321. byte_length = elem_length << shift;
  22322. if ((byte_length >> shift) != elem_length) {
  22323. /* Byte length would overflow. */
  22324. /* XXX: easier check with less code? */
  22325. goto fail_arguments;
  22326. }
  22327. DUK_ASSERT(h_bufarg->length >= byte_offset);
  22328. if (byte_length > h_bufarg->length - byte_offset) {
  22329. /* Not enough data. */
  22330. goto fail_arguments;
  22331. }
  22332. }
  22333. DUK_ASSERT_DISABLE(byte_offset >= 0);
  22334. DUK_ASSERT(byte_offset <= h_bufarg->length);
  22335. DUK_ASSERT_DISABLE(byte_length >= 0);
  22336. DUK_ASSERT(byte_offset + byte_length <= h_bufarg->length);
  22337. DUK_ASSERT((elem_length << shift) == byte_length);
  22338. h_bufobj = duk_push_bufferobject_raw(ctx,
  22339. DUK_HOBJECT_FLAG_EXTENSIBLE |
  22340. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  22341. DUK_HOBJECT_CLASS_AS_FLAGS(class_num),
  22342. proto_bidx);
  22343. h_val = h_bufarg->buf;
  22344. if (h_val == NULL) {
  22345. return DUK_RET_TYPE_ERROR;
  22346. }
  22347. h_bufobj->buf = h_val;
  22348. DUK_HBUFFER_INCREF(thr, h_val);
  22349. h_bufobj->offset = h_bufarg->offset + byte_offset;
  22350. h_bufobj->length = byte_length;
  22351. h_bufobj->shift = shift;
  22352. h_bufobj->elem_type = elem_type;
  22353. h_bufobj->is_view = 1;
  22354. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  22355. /* Set .buffer to the argument ArrayBuffer. */
  22356. duk_dup(ctx, 0);
  22357. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE);
  22358. duk_compact(ctx, -1);
  22359. return 1;
  22360. } else if (DUK_HOBJECT_IS_BUFFEROBJECT(h_obj)) {
  22361. /* TypedArray (or other non-ArrayBuffer duk_hbufferobject).
  22362. * Conceptually same behavior as for an Array-like argument,
  22363. * with a few fast paths.
  22364. */
  22365. h_bufarg = (duk_hbufferobject *) h_obj;
  22366. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufarg);
  22367. elem_length_signed = (duk_int_t) (h_bufarg->length >> h_bufarg->shift);
  22368. if (h_bufarg->buf == NULL) {
  22369. return DUK_RET_TYPE_ERROR;
  22370. }
  22371. /* Select copy mode. Must take into account element
  22372. * compatibility and validity of the underlying source
  22373. * buffer.
  22374. */
  22375. DUK_DDD(DUK_DDDPRINT("selecting copy mode for bufobj arg, "
  22376. "src byte_length=%ld, src shift=%d, "
  22377. "src/dst elem_length=%ld; "
  22378. "dst shift=%d -> dst byte_length=%ld",
  22379. (long) h_bufarg->length, (int) h_bufarg->shift,
  22380. (long) elem_length_signed, (int) shift,
  22381. (long) (elem_length_signed << shift)));
  22382. copy_mode = 2; /* default is explicit index read/write copy */
  22383. DUK_ASSERT(elem_type < sizeof(duk__buffer_elemtype_copy_compatible) / sizeof(duk_uint16_t));
  22384. if (DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg)) {
  22385. if ((duk__buffer_elemtype_copy_compatible[elem_type] & (1 << h_bufarg->elem_type)) != 0) {
  22386. DUK_DDD(DUK_DDDPRINT("source/target are copy compatible, memcpy"));
  22387. DUK_ASSERT(shift == h_bufarg->shift); /* byte sizes will match */
  22388. copy_mode = 0;
  22389. } else {
  22390. DUK_DDD(DUK_DDDPRINT("source/target not copy compatible but valid, fast copy"));
  22391. copy_mode = 1;
  22392. }
  22393. }
  22394. } else {
  22395. /* Array or Array-like */
  22396. elem_length_signed = (duk_int_t) duk_get_length(ctx, 0);
  22397. copy_mode = 2;
  22398. }
  22399. } else {
  22400. /* Non-object argument is simply int coerced, matches
  22401. * V8 behavior (except for "null", which we coerce to
  22402. * 0 but V8 TypeErrors).
  22403. */
  22404. elem_length_signed = duk_to_int(ctx, 0);
  22405. copy_mode = 3;
  22406. }
  22407. if (elem_length_signed < 0) {
  22408. goto fail_arguments;
  22409. }
  22410. elem_length = (duk_uint_t) elem_length_signed;
  22411. byte_length = (duk_uint_t) (elem_length << shift);
  22412. if ((byte_length >> shift) != elem_length) {
  22413. /* Byte length would overflow. */
  22414. /* XXX: easier check with less code? */
  22415. goto fail_arguments;
  22416. }
  22417. DUK_DDD(DUK_DDDPRINT("elem_length=%ld, byte_length=%ld",
  22418. (long) elem_length, (long) byte_length));
  22419. /* ArrayBuffer argument is handled specially above; the rest of the
  22420. * argument variants are handled by shared code below.
  22421. */
  22422. /* Push a new ArrayBuffer (becomes view .buffer) */
  22423. h_bufarr = duk__push_arraybuffer_with_length(ctx, byte_length);
  22424. DUK_ASSERT(h_bufarr != NULL);
  22425. h_val = h_bufarr->buf;
  22426. DUK_ASSERT(h_val != NULL);
  22427. /* Push the resulting view object and attach the ArrayBuffer. */
  22428. h_bufobj = duk_push_bufferobject_raw(ctx,
  22429. DUK_HOBJECT_FLAG_EXTENSIBLE |
  22430. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  22431. DUK_HOBJECT_CLASS_AS_FLAGS(class_num),
  22432. proto_bidx);
  22433. h_bufobj->buf = h_val;
  22434. DUK_HBUFFER_INCREF(thr, h_val);
  22435. DUK_ASSERT(h_bufobj->offset == 0);
  22436. h_bufobj->length = byte_length;
  22437. h_bufobj->shift = shift;
  22438. h_bufobj->elem_type = elem_type;
  22439. h_bufobj->is_view = 1;
  22440. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  22441. /* Set .buffer */
  22442. duk_dup(ctx, -2);
  22443. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE);
  22444. duk_compact(ctx, -1);
  22445. /* Copy values, the copy method depends on the arguments.
  22446. *
  22447. * Copy mode decision may depend on the validity of the underlying
  22448. * buffer of the source argument; there must be no harmful side effects
  22449. * from there to here for copy_mode to still be valid.
  22450. */
  22451. DUK_DDD(DUK_DDDPRINT("copy mode: %d", (int) copy_mode));
  22452. switch (copy_mode) {
  22453. case 0: {
  22454. /* Use byte copy. */
  22455. duk_uint8_t *p_src;
  22456. duk_uint8_t *p_dst;
  22457. DUK_ASSERT(h_bufobj != NULL);
  22458. DUK_ASSERT(h_bufobj->buf != NULL);
  22459. DUK_ASSERT(DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj));
  22460. DUK_ASSERT(h_bufarg != NULL);
  22461. DUK_ASSERT(h_bufarg->buf != NULL);
  22462. DUK_ASSERT(DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg));
  22463. p_dst = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufobj);
  22464. p_src = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufarg);
  22465. DUK_DDD(DUK_DDDPRINT("using memcpy: p_src=%p, p_dst=%p, byte_length=%ld",
  22466. (void *) p_src, (void *) p_dst, (long) byte_length));
  22467. DUK_MEMCPY((void *) p_dst, (const void *) p_src, (size_t) byte_length);
  22468. break;
  22469. }
  22470. case 1: {
  22471. /* Copy values through direct validated reads and writes. */
  22472. duk_small_uint_t src_elem_size;
  22473. duk_small_uint_t dst_elem_size;
  22474. duk_uint8_t *p_src;
  22475. duk_uint8_t *p_src_end;
  22476. duk_uint8_t *p_dst;
  22477. DUK_ASSERT(h_bufobj != NULL);
  22478. DUK_ASSERT(h_bufobj->buf != NULL);
  22479. DUK_ASSERT(DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj));
  22480. DUK_ASSERT(h_bufarg != NULL);
  22481. DUK_ASSERT(h_bufarg->buf != NULL);
  22482. DUK_ASSERT(DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg));
  22483. src_elem_size = 1 << h_bufarg->shift;
  22484. dst_elem_size = elem_size;
  22485. p_src = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufarg);
  22486. p_dst = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufobj);
  22487. p_src_end = p_src + h_bufarg->length;
  22488. DUK_DDD(DUK_DDDPRINT("using fast copy: p_src=%p, p_src_end=%p, p_dst=%p, "
  22489. "src_elem_size=%d, dst_elem_size=%d",
  22490. (void *) p_src, (void *) p_src_end, (void *) p_dst,
  22491. (int) src_elem_size, (int) dst_elem_size));
  22492. while (p_src != p_src_end) {
  22493. DUK_DDD(DUK_DDDPRINT("fast path per element copy loop: "
  22494. "p_src=%p, p_src_end=%p, p_dst=%p",
  22495. (void *) p_src, (void *) p_src_end, (void *) p_dst));
  22496. /* A validated read() is always a number, so it's write coercion
  22497. * is always side effect free an won't invalidate pointers etc.
  22498. */
  22499. duk_hbufferobject_push_validated_read(ctx, h_bufarg, p_src, src_elem_size);
  22500. duk_hbufferobject_validated_write(ctx, h_bufobj, p_dst, dst_elem_size);
  22501. duk_pop(ctx);
  22502. p_src += src_elem_size;
  22503. p_dst += dst_elem_size;
  22504. }
  22505. break;
  22506. }
  22507. case 2: {
  22508. /* Copy values by index reads and writes. Let virtual
  22509. * property handling take care of coercion.
  22510. */
  22511. duk_uint_t i;
  22512. DUK_DDD(DUK_DDDPRINT("using slow copy"));
  22513. for (i = 0; i < elem_length; i++) {
  22514. duk_get_prop_index(ctx, 0, (duk_uarridx_t) i);
  22515. duk_put_prop_index(ctx, -2, (duk_uarridx_t) i);
  22516. }
  22517. break;
  22518. }
  22519. default:
  22520. case 3: {
  22521. /* No copy, leave zero bytes in the buffer. There's no
  22522. * ambiguity with Float32/Float64 because zero bytes also
  22523. * represent 0.0.
  22524. */
  22525. DUK_DDD(DUK_DDDPRINT("using no copy"));
  22526. break;
  22527. }
  22528. }
  22529. return 1;
  22530. fail_arguments:
  22531. return DUK_RET_RANGE_ERROR;
  22532. }
  22533. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22534. DUK_INTERNAL duk_ret_t duk_bi_typedarray_constructor(duk_context *ctx) {
  22535. DUK_UNREF(ctx);
  22536. return DUK_RET_UNSUPPORTED_ERROR;
  22537. }
  22538. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22539. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  22540. DUK_INTERNAL duk_ret_t duk_bi_dataview_constructor(duk_context *ctx) {
  22541. duk_hbufferobject *h_bufarg;
  22542. duk_hbufferobject *h_bufobj;
  22543. duk_hbuffer *h_val;
  22544. duk_uint_t offset;
  22545. duk_uint_t length;
  22546. /* XXX: function flag to make this automatic? */
  22547. if (!duk_is_constructor_call(ctx)) {
  22548. return DUK_RET_TYPE_ERROR;
  22549. }
  22550. h_bufarg = duk__require_bufobj_value(ctx, 0);
  22551. DUK_ASSERT(h_bufarg != NULL);
  22552. duk__resolve_offset_opt_length(ctx, h_bufarg, 1, 2, &offset, &length, 1 /*throw_flag*/);
  22553. DUK_ASSERT(offset <= h_bufarg->length);
  22554. DUK_ASSERT(offset + length <= h_bufarg->length);
  22555. h_bufobj = duk_push_bufferobject_raw(ctx,
  22556. DUK_HOBJECT_FLAG_EXTENSIBLE |
  22557. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  22558. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DATAVIEW),
  22559. DUK_BIDX_DATAVIEW_PROTOTYPE);
  22560. h_val = h_bufarg->buf;
  22561. if (h_val == NULL) {
  22562. return DUK_RET_TYPE_ERROR;
  22563. }
  22564. h_bufobj->buf = h_val;
  22565. DUK_HBUFFER_INCREF(thr, h_val);
  22566. h_bufobj->offset = h_bufarg->offset + offset;
  22567. h_bufobj->length = length;
  22568. DUK_ASSERT(h_bufobj->shift == 0);
  22569. DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8);
  22570. h_bufobj->is_view = 1;
  22571. /* The DataView .buffer property is ordinarily set to the argument
  22572. * which is an ArrayBuffer. We accept any duk_hbufferobject as
  22573. * an argument and .buffer will be set to the argument regardless
  22574. * of what it is. This may be a bit confusing if the argument
  22575. * is e.g. a DataView or another TypedArray view.
  22576. *
  22577. * XXX: Copy .buffer property from a DataView/TypedArray argument?
  22578. * Create a fresh ArrayBuffer for Duktape.Buffer and Node.js Buffer
  22579. * arguments? See: test-bug-dataview-buffer-prop.js.
  22580. */
  22581. duk_dup(ctx, 0);
  22582. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE);
  22583. duk_compact(ctx, -1);
  22584. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  22585. return 1;
  22586. }
  22587. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22588. DUK_INTERNAL duk_ret_t duk_bi_dataview_constructor(duk_context *ctx) {
  22589. DUK_UNREF(ctx);
  22590. return DUK_RET_UNSUPPORTED_ERROR;
  22591. }
  22592. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22593. /*
  22594. * ArrayBuffer.isView()
  22595. */
  22596. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  22597. DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_isview(duk_context *ctx) {
  22598. duk_hobject *h_obj;
  22599. duk_bool_t ret = 0;
  22600. h_obj = duk_get_hobject(ctx, 0);
  22601. if (h_obj != NULL && DUK_HOBJECT_IS_BUFFEROBJECT(h_obj)) {
  22602. ret = ((duk_hbufferobject *) h_obj)->is_view;
  22603. }
  22604. duk_push_boolean(ctx, ret);
  22605. return 1;
  22606. }
  22607. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22608. DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_isview(duk_context *ctx) {
  22609. DUK_UNREF(ctx);
  22610. return DUK_RET_UNSUPPORTED_ERROR;
  22611. }
  22612. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22613. /*
  22614. * Node.js Buffer: toString([encoding], [start], [end])
  22615. */
  22616. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  22617. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_context *ctx) {
  22618. duk_hthread *thr;
  22619. duk_hbufferobject *h_this;
  22620. duk_int_t start_offset, end_offset;
  22621. duk_uint8_t *buf_slice;
  22622. duk_size_t slice_length;
  22623. thr = (duk_hthread *) ctx;
  22624. DUK_UNREF(thr);
  22625. h_this = duk__get_bufobj_this(ctx);
  22626. if (h_this == NULL) {
  22627. /* XXX: happens e.g. when evaluating: String(Buffer.prototype). */
  22628. duk_push_string(ctx, "[object Object]");
  22629. return 1;
  22630. }
  22631. DUK_ASSERT_HBUFFEROBJECT_VALID(h_this);
  22632. /* ignore encoding for now */
  22633. duk__clamp_startend_nonegidx_noshift(ctx, h_this, 1 /*idx_start*/, 2 /*idx_end*/, &start_offset, &end_offset);
  22634. slice_length = (duk_size_t) (end_offset - start_offset);
  22635. buf_slice = (duk_uint8_t *) duk_push_fixed_buffer(ctx, slice_length);
  22636. DUK_ASSERT(buf_slice != NULL);
  22637. if (h_this->buf == NULL) {
  22638. goto type_error;
  22639. }
  22640. if (DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_this, start_offset + slice_length)) {
  22641. DUK_MEMCPY((void *) buf_slice,
  22642. (const void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + start_offset),
  22643. (size_t) slice_length);
  22644. } else {
  22645. /* not covered, return all zeroes */
  22646. ;
  22647. }
  22648. duk_to_string(ctx, -1);
  22649. return 1;
  22650. type_error:
  22651. return DUK_RET_TYPE_ERROR;
  22652. }
  22653. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22654. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_context *ctx) {
  22655. DUK_UNREF(ctx);
  22656. return DUK_RET_UNSUPPORTED_ERROR;
  22657. }
  22658. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22659. /*
  22660. * Duktape.Buffer: toString(), valueOf()
  22661. */
  22662. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  22663. DUK_INTERNAL duk_ret_t duk_bi_buffer_prototype_tostring_shared(duk_context *ctx) {
  22664. duk_hthread *thr;
  22665. duk_tval *tv;
  22666. duk_small_int_t to_string = duk_get_current_magic(ctx);
  22667. thr = (duk_hthread *) ctx;
  22668. DUK_UNREF(thr);
  22669. tv = duk_get_borrowed_this_tval(ctx);
  22670. DUK_ASSERT(tv != NULL);
  22671. if (DUK_TVAL_IS_BUFFER(tv)) {
  22672. duk_hbuffer *h_buf;
  22673. h_buf = DUK_TVAL_GET_BUFFER(tv);
  22674. DUK_ASSERT(h_buf != NULL);
  22675. duk_push_hbuffer(ctx, h_buf);
  22676. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  22677. duk_hobject *h;
  22678. duk_hbufferobject *h_bufobj;
  22679. /* Accept any duk_hbufferobject, though we're only normally
  22680. * called for Duktape.Buffer values.
  22681. */
  22682. h = DUK_TVAL_GET_OBJECT(tv);
  22683. DUK_ASSERT(h != NULL);
  22684. if (!DUK_HOBJECT_IS_BUFFEROBJECT(h)) {
  22685. DUK_DD(DUK_DDPRINT("toString/valueOf() called for a non-bufferobject object"));
  22686. goto type_error;
  22687. }
  22688. h_bufobj = (duk_hbufferobject *) h;
  22689. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  22690. if (h_bufobj->buf == NULL) {
  22691. DUK_DD(DUK_DDPRINT("toString/valueOf() called for a bufferobject with NULL buf"));
  22692. goto type_error;
  22693. }
  22694. duk_push_hbuffer(ctx, h_bufobj->buf);
  22695. } else {
  22696. goto type_error;
  22697. }
  22698. if (to_string) {
  22699. duk_to_string(ctx, -1);
  22700. }
  22701. return 1;
  22702. type_error:
  22703. return DUK_RET_TYPE_ERROR;
  22704. }
  22705. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22706. DUK_INTERNAL duk_ret_t duk_bi_buffer_prototype_tostring_shared(duk_context *ctx) {
  22707. DUK_UNREF(ctx);
  22708. return DUK_RET_UNSUPPORTED_ERROR;
  22709. }
  22710. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22711. /*
  22712. * Node.js Buffer.prototype: toJSON()
  22713. */
  22714. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  22715. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tojson(duk_context *ctx) {
  22716. duk_hthread *thr;
  22717. duk_hbufferobject *h_this;
  22718. duk_uint8_t *buf;
  22719. duk_uint_t i;
  22720. thr = (duk_hthread *) ctx;
  22721. DUK_UNREF(thr);
  22722. h_this = duk__require_bufobj_this(ctx);
  22723. DUK_ASSERT(h_this != NULL);
  22724. if (h_this->buf == NULL || !DUK_HBUFFEROBJECT_VALID_SLICE(h_this)) {
  22725. /* Serialize uncovered backing buffer as a null; doesn't
  22726. * really matter as long we're memory safe.
  22727. */
  22728. duk_push_null(ctx);
  22729. return 1;
  22730. }
  22731. duk_push_object(ctx);
  22732. duk_push_hstring_stridx(ctx, DUK_STRIDX_UC_BUFFER);
  22733. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_TYPE);
  22734. duk_push_array(ctx);
  22735. for (i = 0; i < h_this->length; i++) {
  22736. /* XXX: regetting the pointer may be overkill - we're writing
  22737. * to a side-effect free array here.
  22738. */
  22739. DUK_ASSERT(h_this->buf != NULL);
  22740. buf = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this);
  22741. duk_push_uint(ctx, (duk_uint_t) buf[i]);
  22742. duk_put_prop_index(ctx, -2, (duk_idx_t) i);
  22743. }
  22744. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_DATA);
  22745. return 1;
  22746. }
  22747. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22748. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tojson(duk_context *ctx) {
  22749. DUK_UNREF(ctx);
  22750. return DUK_RET_UNSUPPORTED_ERROR;
  22751. }
  22752. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22753. /*
  22754. * Node.js Buffer.prototype.equals()
  22755. * Node.js Buffer.prototype.compare()
  22756. * Node.js Buffer.compare()
  22757. */
  22758. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  22759. DUK_INTERNAL duk_ret_t duk_bi_buffer_compare_shared(duk_context *ctx) {
  22760. duk_hthread *thr;
  22761. duk_small_uint_t magic;
  22762. duk_hbufferobject *h_bufarg1;
  22763. duk_hbufferobject *h_bufarg2;
  22764. duk_small_int_t comp_res;
  22765. thr = (duk_hthread *) ctx;
  22766. DUK_UNREF(thr);
  22767. magic = duk_get_current_magic(ctx);
  22768. if (magic & 0x02) {
  22769. /* Static call style. */
  22770. h_bufarg1 = duk__require_bufobj_value(ctx, 0);
  22771. h_bufarg2 = duk__require_bufobj_value(ctx, 1);
  22772. } else {
  22773. h_bufarg1 = duk__require_bufobj_this(ctx);
  22774. h_bufarg2 = duk__require_bufobj_value(ctx, 0);
  22775. }
  22776. DUK_ASSERT(h_bufarg1 != NULL);
  22777. DUK_ASSERT(h_bufarg2 != NULL);
  22778. /* We want to compare the slice/view areas of the arguments.
  22779. * If either slice/view is invalid (underlying buffer is shorter)
  22780. * ensure equals() is false, but otherwise the only thing that
  22781. * matters is to be memory safe.
  22782. */
  22783. if (DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg1) &&
  22784. DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg2)) {
  22785. comp_res = duk_js_data_compare((const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufarg1->buf) + h_bufarg1->offset,
  22786. (const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufarg2->buf) + h_bufarg2->offset,
  22787. (duk_size_t) h_bufarg1->length,
  22788. (duk_size_t) h_bufarg2->length);
  22789. } else {
  22790. comp_res = -1; /* either nonzero value is ok */
  22791. }
  22792. if (magic & 0x01) {
  22793. /* compare: similar to string comparison but for buffer data. */
  22794. duk_push_int(ctx, comp_res);
  22795. } else {
  22796. /* equals */
  22797. duk_push_boolean(ctx, (comp_res == 0));
  22798. }
  22799. return 1;
  22800. }
  22801. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22802. DUK_INTERNAL duk_ret_t duk_bi_buffer_compare_shared(duk_context *ctx) {
  22803. DUK_UNREF(ctx);
  22804. return DUK_RET_UNSUPPORTED_ERROR;
  22805. }
  22806. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22807. /*
  22808. * Node.js Buffer.prototype.fill()
  22809. */
  22810. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  22811. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_fill(duk_context *ctx) {
  22812. duk_hthread *thr;
  22813. duk_hbufferobject *h_this;
  22814. const duk_uint8_t *fill_str_ptr;
  22815. duk_size_t fill_str_len;
  22816. duk_uint8_t fill_value;
  22817. duk_int_t fill_offset;
  22818. duk_int_t fill_end;
  22819. duk_size_t fill_length;
  22820. duk_uint8_t *p;
  22821. thr = (duk_hthread *) ctx;
  22822. DUK_UNREF(thr);
  22823. h_this = duk__require_bufobj_this(ctx);
  22824. DUK_ASSERT(h_this != NULL);
  22825. if (h_this->buf == NULL) {
  22826. return DUK_RET_TYPE_ERROR;
  22827. }
  22828. /* [ value offset end ] */
  22829. if (duk_is_string(ctx, 0)) {
  22830. fill_str_ptr = (const duk_uint8_t *) duk_get_lstring(ctx, 0, &fill_str_len);
  22831. DUK_ASSERT(fill_str_ptr != NULL);
  22832. } else {
  22833. fill_value = (duk_uint8_t) duk_to_uint32(ctx, 0);
  22834. fill_str_ptr = (const duk_uint8_t *) &fill_value;
  22835. fill_str_len = 1;
  22836. }
  22837. /* Fill offset handling is more lenient than in Node.js. */
  22838. duk__clamp_startend_nonegidx_noshift(ctx, h_this, 1 /*idx_start*/, 2 /*idx_end*/, &fill_offset, &fill_end);
  22839. DUK_DDD(DUK_DDDPRINT("fill: fill_value=%02x, fill_offset=%ld, fill_end=%ld, view length=%ld",
  22840. (unsigned int) fill_value, (long) fill_offset, (long) fill_end, (long) h_this->length));
  22841. DUK_ASSERT(fill_end - fill_offset >= 0);
  22842. DUK_ASSERT(h_this->buf != NULL);
  22843. p = (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + fill_offset);
  22844. fill_length = (duk_size_t) (fill_end - fill_offset);
  22845. if (fill_str_len == 1) {
  22846. /* Handle single character fills as memset() even when
  22847. * the fill data comes from a one-char argument.
  22848. */
  22849. DUK_MEMSET((void *) p, (int) fill_str_ptr[0], (size_t) fill_length);
  22850. } else if (fill_str_len > 1) {
  22851. duk_size_t i, n, t;
  22852. for (i = 0, n = (fill_end - fill_offset), t = 0; i < n; i++) {
  22853. p[i] = fill_str_ptr[t++];
  22854. if (t >= fill_str_len) {
  22855. t = 0;
  22856. }
  22857. }
  22858. } else {
  22859. DUK_DDD(DUK_DDDPRINT("zero size fill pattern, ignore silently"));
  22860. }
  22861. /* Return the Buffer to allow chaining: b.fill(0x11).fill(0x22, 3, 5).toString() */
  22862. duk_push_this(ctx);
  22863. return 1;
  22864. }
  22865. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22866. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_fill(duk_context *ctx) {
  22867. DUK_UNREF(ctx);
  22868. return DUK_RET_UNSUPPORTED_ERROR;
  22869. }
  22870. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22871. /*
  22872. * Node.js Buffer.prototype.write(string, [offset], [length], [encoding])
  22873. */
  22874. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  22875. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_write(duk_context *ctx) {
  22876. duk_hthread *thr;
  22877. duk_hbufferobject *h_this;
  22878. duk_uint_t offset;
  22879. duk_uint_t length;
  22880. const duk_uint8_t *str_data;
  22881. duk_size_t str_len;
  22882. thr = (duk_hthread *) ctx;
  22883. DUK_UNREF(thr);
  22884. h_this = duk__require_bufobj_this(ctx);
  22885. DUK_ASSERT(h_this != NULL);
  22886. /* Argument must be a string, e.g. a buffer is not allowed. */
  22887. str_data = (const duk_uint8_t *) duk_require_lstring(ctx, 0, &str_len);
  22888. duk__resolve_offset_opt_length(ctx, h_this, 1, 2, &offset, &length, 0 /*throw_flag*/);
  22889. DUK_ASSERT(offset <= h_this->length);
  22890. DUK_ASSERT(offset + length <= h_this->length);
  22891. /* XXX: encoding is ignored now. */
  22892. if (length > str_len) {
  22893. length = (duk_uint_t) str_len;
  22894. }
  22895. if (DUK_HBUFFEROBJECT_VALID_SLICE(h_this)) {
  22896. /* Cannot overlap. */
  22897. DUK_MEMCPY((void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + offset),
  22898. (const void *) str_data,
  22899. (size_t) length);
  22900. } else {
  22901. DUK_DDD(DUK_DDDPRINT("write() target buffer is not covered, silent ignore"));
  22902. }
  22903. duk_push_uint(ctx, length);
  22904. return 1;
  22905. }
  22906. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22907. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_write(duk_context *ctx) {
  22908. DUK_UNREF(ctx);
  22909. return DUK_RET_UNSUPPORTED_ERROR;
  22910. }
  22911. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  22912. /*
  22913. * Node.js Buffer.prototype.copy()
  22914. */
  22915. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  22916. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_copy(duk_context *ctx) {
  22917. duk_hthread *thr;
  22918. duk_hbufferobject *h_this;
  22919. duk_hbufferobject *h_bufarg;
  22920. duk_int_t source_length;
  22921. duk_int_t target_length;
  22922. duk_int_t target_start, source_start, source_end;
  22923. duk_uint_t target_ustart, source_ustart, source_uend;
  22924. duk_uint_t copy_size = 0;
  22925. /* [ targetBuffer targetStart sourceStart sourceEnd ] */
  22926. thr = (duk_hthread *) ctx;
  22927. DUK_UNREF(thr);
  22928. h_this = duk__require_bufobj_this(ctx);
  22929. h_bufarg = duk__require_bufobj_value(ctx, 0);
  22930. DUK_ASSERT(h_this != NULL);
  22931. DUK_ASSERT(h_bufarg != NULL);
  22932. source_length = (duk_int_t) h_this->length;
  22933. target_length = (duk_int_t) h_bufarg->length;
  22934. target_start = duk_to_int(ctx, 1);
  22935. source_start = duk_to_int(ctx, 2);
  22936. if (duk_is_undefined(ctx, 3)) {
  22937. source_end = source_length;
  22938. } else {
  22939. source_end = duk_to_int(ctx, 3);
  22940. }
  22941. DUK_DDD(DUK_DDDPRINT("checking copy args: target_start=%ld, target_length=%ld, "
  22942. "source_start=%ld, source_end=%ld, source_length=%ld",
  22943. (long) target_start, (long) h_bufarg->length,
  22944. (long) source_start, (long) source_end, (long) source_length));
  22945. /* This behavior mostly mimics Node.js now. */
  22946. if (source_start < 0 || source_end < 0 || target_start < 0) {
  22947. /* Negative offsets cause a RangeError. */
  22948. goto fail_bounds;
  22949. }
  22950. source_ustart = (duk_uint_t) source_start;
  22951. source_uend = (duk_uint_t) source_end;
  22952. target_ustart = (duk_uint_t) target_start;
  22953. if (source_ustart >= source_uend || /* crossed offsets or zero size */
  22954. source_ustart >= (duk_uint_t) source_length || /* source out-of-bounds (but positive) */
  22955. target_ustart >= (duk_uint_t) target_length) { /* target out-of-bounds (but positive) */
  22956. goto silent_ignore;
  22957. }
  22958. if (source_uend >= (duk_uint_t) source_length) {
  22959. /* Source end clamped silently to available length. */
  22960. source_uend = source_length;
  22961. }
  22962. copy_size = source_uend - source_ustart;
  22963. if (target_ustart + copy_size > (duk_uint_t) target_length) {
  22964. /* Clamp to target's end if too long.
  22965. *
  22966. * NOTE: there's no overflow possibility in the comparison;
  22967. * both target_ustart and copy_size are >= 0 and based on
  22968. * values in duk_int_t range. Adding them as duk_uint_t
  22969. * values is then guaranteed not to overflow.
  22970. */
  22971. DUK_ASSERT(target_ustart + copy_size >= target_ustart); /* no overflow */
  22972. DUK_ASSERT(target_ustart + copy_size >= copy_size); /* no overflow */
  22973. copy_size = (duk_uint_t) target_length - target_ustart;
  22974. }
  22975. DUK_DDD(DUK_DDDPRINT("making copy: target_ustart=%lu source_ustart=%lu copy_size=%lu",
  22976. (unsigned long) target_ustart, (unsigned long) source_ustart,
  22977. (unsigned long) copy_size));
  22978. DUK_ASSERT(copy_size >= 1);
  22979. DUK_ASSERT(source_ustart <= (duk_uint_t) source_length);
  22980. DUK_ASSERT(source_ustart + copy_size <= (duk_uint_t) source_length);
  22981. DUK_ASSERT(target_ustart <= (duk_uint_t) target_length);
  22982. DUK_ASSERT(target_ustart + copy_size <= (duk_uint_t) target_length);
  22983. /* Ensure copy is covered by underlying buffers. */
  22984. DUK_ASSERT(h_bufarg->buf != NULL); /* length check */
  22985. DUK_ASSERT(h_this->buf != NULL); /* length check */
  22986. if (DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_bufarg, target_ustart + copy_size) &&
  22987. DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_this, source_ustart + copy_size)) {
  22988. /* Must use memmove() because copy area may overlap (source and target
  22989. * buffer may be the same, or from different slices.
  22990. */
  22991. DUK_MEMMOVE((void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufarg) + target_ustart),
  22992. (const void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + source_ustart),
  22993. (size_t) copy_size);
  22994. } else {
  22995. DUK_DDD(DUK_DDDPRINT("buffer copy not covered by underlying buffer(s), ignoring"));
  22996. }
  22997. silent_ignore:
  22998. /* Return value is like write(), number of bytes written.
  22999. * The return value matters because of code like:
  23000. * "off += buf.copy(...)".
  23001. */
  23002. duk_push_uint(ctx, copy_size);
  23003. return 1;
  23004. fail_bounds:
  23005. return DUK_RET_RANGE_ERROR;
  23006. }
  23007. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23008. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_copy(duk_context *ctx) {
  23009. DUK_UNREF(ctx);
  23010. return DUK_RET_UNSUPPORTED_ERROR;
  23011. }
  23012. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23013. /*
  23014. * TypedArray.prototype.set()
  23015. *
  23016. * TypedArray set() is pretty interesting to implement because:
  23017. *
  23018. * - The source argument may be a plain array or a typedarray. If the
  23019. * source is a TypedArray, values are decoded and re-encoded into the
  23020. * target (not as a plain byte copy). This may happen even when the
  23021. * element byte size is the same, e.g. integer values may be re-encoded
  23022. * into floats.
  23023. *
  23024. * - Source and target may refer to the same underlying buffer, so that
  23025. * the set() operation may overlap. The specification requires that this
  23026. * must work as if a copy was made before the operation. Note that this
  23027. * is NOT a simple memmove() situation because the source and target
  23028. * byte sizes may be different -- e.g. a 4-byte source (Int8Array) may
  23029. * expand to a 16-byte target (Uint32Array) so that the target overlaps
  23030. * the source both from beginning and the end (unlike in typical memmove).
  23031. *
  23032. * - Even if 'buf' pointers of the source and target differ, there's no
  23033. * guarantee that their memory areas don't overlap. This may be the
  23034. * case with external buffers.
  23035. *
  23036. * Even so, it is nice to optimize for the common case:
  23037. *
  23038. * - Source and target separate buffers or non-overlapping.
  23039. *
  23040. * - Source and target have a compatible type so that a plain byte copy
  23041. * is possible. Note that while e.g. uint8 and int8 are compatible
  23042. * (coercion one way or another doesn't change the byte representation),
  23043. * e.g. int8 and uint8clamped are NOT compatible when writing int8
  23044. * values into uint8clamped typedarray (-1 would clamp to 0 for instance).
  23045. *
  23046. * See test-bi-typedarray-proto-set.js.
  23047. */
  23048. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  23049. DUK_INTERNAL duk_ret_t duk_bi_typedarray_set(duk_context *ctx) {
  23050. duk_hthread *thr;
  23051. duk_hbufferobject *h_this;
  23052. duk_hobject *h_obj;
  23053. duk_uarridx_t i, n;
  23054. duk_int_t offset_signed;
  23055. duk_uint_t offset_elems;
  23056. duk_uint_t offset_bytes;
  23057. thr = (duk_hthread *) ctx;
  23058. DUK_UNREF(thr);
  23059. h_this = duk__require_bufobj_this(ctx);
  23060. DUK_ASSERT(h_this != NULL);
  23061. DUK_ASSERT_HBUFFEROBJECT_VALID(h_this);
  23062. if (h_this->buf == NULL) {
  23063. DUK_DDD(DUK_DDDPRINT("source neutered, skip copy"));
  23064. return 0;
  23065. }
  23066. h_obj = duk_require_hobject(ctx, 0);
  23067. DUK_ASSERT(h_obj != NULL);
  23068. /* XXX: V8 throws a TypeError for negative values. Would it
  23069. * be more useful to interpret negative offsets here from the
  23070. * end of the buffer too?
  23071. */
  23072. offset_signed = duk_to_int(ctx, 1);
  23073. if (offset_signed < 0) {
  23074. return DUK_RET_TYPE_ERROR;
  23075. }
  23076. offset_elems = (duk_uint_t) offset_signed;
  23077. offset_bytes = offset_elems << h_this->shift;
  23078. if ((offset_bytes >> h_this->shift) != offset_elems) {
  23079. /* Byte length would overflow. */
  23080. /* XXX: easier check with less code? */
  23081. return DUK_RET_RANGE_ERROR;
  23082. }
  23083. if (offset_bytes > h_this->length) {
  23084. /* Equality may be OK but >length not. Checking
  23085. * this explicitly avoids some overflow cases
  23086. * below.
  23087. */
  23088. return DUK_RET_RANGE_ERROR;
  23089. }
  23090. DUK_ASSERT(offset_bytes <= h_this->length);
  23091. /* Fast path: source is a TypedArray (or any bufferobject). */
  23092. if (DUK_HOBJECT_IS_BUFFEROBJECT(h_obj)) {
  23093. duk_hbufferobject *h_bufarg;
  23094. duk_uint16_t comp_mask;
  23095. duk_small_int_t no_overlap = 0;
  23096. duk_uint_t src_length;
  23097. duk_uint_t dst_length;
  23098. duk_uint_t dst_length_elems;
  23099. duk_uint8_t *p_src_base;
  23100. duk_uint8_t *p_src_end;
  23101. duk_uint8_t *p_src;
  23102. duk_uint8_t *p_dst_base;
  23103. duk_uint8_t *p_dst;
  23104. duk_small_uint_t src_elem_size;
  23105. duk_small_uint_t dst_elem_size;
  23106. h_bufarg = (duk_hbufferobject *) h_obj;
  23107. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufarg);
  23108. if (h_bufarg->buf == NULL) {
  23109. DUK_DDD(DUK_DDDPRINT("target neutered, skip copy"));
  23110. return 0;
  23111. }
  23112. /* Nominal size check. */
  23113. src_length = h_bufarg->length; /* bytes in source */
  23114. dst_length_elems = (src_length >> h_bufarg->shift); /* elems in source and dest */
  23115. dst_length = dst_length_elems << h_this->shift; /* bytes in dest */
  23116. if ((dst_length >> h_this->shift) != dst_length_elems) {
  23117. /* Byte length would overflow. */
  23118. /* XXX: easier check with less code? */
  23119. return DUK_RET_RANGE_ERROR;
  23120. }
  23121. DUK_DDD(DUK_DDDPRINT("nominal size check: src_length=%ld, dst_length=%ld",
  23122. (long) src_length, (long) dst_length));
  23123. DUK_ASSERT(offset_bytes <= h_this->length);
  23124. if (dst_length > h_this->length - offset_bytes) {
  23125. /* Overflow not an issue because subtraction is used on the right
  23126. * side and guaranteed to be >= 0.
  23127. */
  23128. DUK_DDD(DUK_DDDPRINT("copy exceeds target buffer nominal length"));
  23129. return DUK_RET_RANGE_ERROR;
  23130. }
  23131. if (!DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_this, offset_bytes + dst_length)) {
  23132. DUK_DDD(DUK_DDDPRINT("copy not covered by underlying target buffer, ignore"));
  23133. return 0;
  23134. }
  23135. p_src_base = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufarg);
  23136. p_dst_base = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + offset_bytes;
  23137. /* Check actual underlying buffers for validity and that they
  23138. * cover the copy. No side effects are allowed after the check
  23139. * so that the validity status doesn't change.
  23140. */
  23141. if (!DUK_HBUFFEROBJECT_VALID_SLICE(h_this) ||
  23142. !DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg)) {
  23143. /* The condition could be more narrow and check for the
  23144. * copy area only, but there's no need for fine grained
  23145. * behavior when the underlying buffer is misconfigured.
  23146. */
  23147. DUK_DDD(DUK_DDDPRINT("source and/or target not covered by underlying buffer, skip copy"));
  23148. return 0;
  23149. }
  23150. /* We want to do a straight memory copy if possible: this is
  23151. * an important operation because .set() is the TypedArray
  23152. * way to copy chunks of memory. However, because set()
  23153. * conceptually works in terms of elements, not all views are
  23154. * compatible with direct byte copying.
  23155. *
  23156. * If we do manage a direct copy, the "overlap issue" handled
  23157. * below can just be solved using memmove() because the source
  23158. * and destination element sizes are necessarily equal.
  23159. */
  23160. DUK_ASSERT(h_this->elem_type < sizeof(duk__buffer_elemtype_copy_compatible) / sizeof(duk_uint16_t));
  23161. comp_mask = duk__buffer_elemtype_copy_compatible[h_this->elem_type];
  23162. if (comp_mask & (1 << h_bufarg->elem_type)) {
  23163. DUK_ASSERT(src_length == dst_length);
  23164. DUK_DDD(DUK_DDDPRINT("fast path: able to use memmove() because views are compatible"));
  23165. DUK_MEMMOVE((void *) p_dst_base, (const void *) p_src_base, (size_t) dst_length);
  23166. return 0;
  23167. }
  23168. DUK_DDD(DUK_DDDPRINT("fast path: views are not compatible with a byte copy, copy by item"));
  23169. /* We want to avoid making a copy to process set() but that's
  23170. * not always possible: the source and the target may overlap
  23171. * and because element sizes are different, the overlap cannot
  23172. * always be handled with a memmove() or choosing the copy
  23173. * direction in a certain way. For example, if source type is
  23174. * uint8 and target type is uint32, the target area may exceed
  23175. * the source area from both ends!
  23176. *
  23177. * Note that because external buffers may point to the same
  23178. * memory areas, we must ultimately make this check using
  23179. * pointers.
  23180. *
  23181. * NOTE: careful with side effects: any side effect may cause
  23182. * a buffer resize (or external buffer pointer/length update)!
  23183. */
  23184. DUK_DDD(DUK_DDDPRINT("overlap check: p_src_base=%p, src_length=%ld, "
  23185. "p_dst_base=%p, dst_length=%ld",
  23186. (void *) p_src_base, (long) src_length,
  23187. (void *) p_dst_base, (long) dst_length));
  23188. if (p_src_base >= p_dst_base + dst_length || /* source starts after dest ends */
  23189. p_src_base + src_length <= p_dst_base) { /* source ends before dest starts */
  23190. no_overlap = 1;
  23191. }
  23192. if (!no_overlap) {
  23193. /* There's overlap: the desired end result is that
  23194. * conceptually a copy is made to avoid "trampling"
  23195. * of source data by destination writes. We make
  23196. * an actual temporary copy to handle this case.
  23197. */
  23198. duk_uint8_t *p_src_copy;
  23199. DUK_DDD(DUK_DDDPRINT("there is overlap, make a copy of the source"));
  23200. p_src_copy = (duk_uint8_t *) duk_push_fixed_buffer(ctx, src_length);
  23201. DUK_ASSERT(p_src_copy != NULL);
  23202. DUK_MEMCPY((void *) p_src_copy, (const void *) p_src_base, (size_t) src_length);
  23203. p_src_base = p_src_copy; /* use p_src_base from now on */
  23204. }
  23205. /* Value stack intentionally mixed size here. */
  23206. DUK_DDD(DUK_DDDPRINT("after overlap check: p_src_base=%p, src_length=%ld, "
  23207. "p_dst_base=%p, dst_length=%ld, valstack top=%ld",
  23208. (void *) p_src_base, (long) src_length,
  23209. (void *) p_dst_base, (long) dst_length,
  23210. (long) duk_get_top(ctx)));
  23211. /* Ready to make the copy. We must proceed element by element
  23212. * and must avoid any side effects that might cause the buffer
  23213. * validity check above to become invalid.
  23214. *
  23215. * Although we work through the value stack here, only plain
  23216. * numbers are handled which should be side effect safe.
  23217. */
  23218. src_elem_size = 1 << h_bufarg->shift;
  23219. dst_elem_size = 1 << h_this->shift;
  23220. p_src = p_src_base;
  23221. p_dst = p_dst_base;
  23222. p_src_end = p_src_base + src_length;
  23223. while (p_src != p_src_end) {
  23224. DUK_DDD(DUK_DDDPRINT("fast path per element copy loop: "
  23225. "p_src=%p, p_src_end=%p, p_dst=%p",
  23226. (void *) p_src, (void *) p_src_end, (void *) p_dst));
  23227. /* A validated read() is always a number, so it's write coercion
  23228. * is always side effect free an won't invalidate pointers etc.
  23229. */
  23230. duk_hbufferobject_push_validated_read(ctx, h_bufarg, p_src, src_elem_size);
  23231. duk_hbufferobject_validated_write(ctx, h_this, p_dst, dst_elem_size);
  23232. duk_pop(ctx);
  23233. p_src += src_elem_size;
  23234. p_dst += dst_elem_size;
  23235. }
  23236. return 0;
  23237. } else {
  23238. /* Slow path: quite slow, but we save space by using the property code
  23239. * to write coerce target values. We don't need to worry about overlap
  23240. * here because the source is not a TypedArray.
  23241. *
  23242. * We could use the bufferobject write coercion helper but since the
  23243. * property read may have arbitrary side effects, full validity checks
  23244. * would be needed for every element anyway.
  23245. */
  23246. n = (duk_uarridx_t) duk_get_length(ctx, 0);
  23247. DUK_ASSERT(offset_bytes <= h_this->length);
  23248. if ((n << h_this->shift) > h_this->length - offset_bytes) {
  23249. /* Overflow not an issue because subtraction is used on the right
  23250. * side and guaranteed to be >= 0.
  23251. */
  23252. DUK_DDD(DUK_DDDPRINT("copy exceeds target buffer nominal length"));
  23253. return DUK_RET_RANGE_ERROR;
  23254. }
  23255. /* There's no need to check for buffer validity status for the
  23256. * target here: the property access code will do that for each
  23257. * element. Moreover, if we did check the validity here, side
  23258. * effects from reading the source argument might invalidate
  23259. * the results anyway.
  23260. */
  23261. DUK_ASSERT_TOP(ctx, 2);
  23262. duk_push_this(ctx);
  23263. for (i = 0; i < n; i++) {
  23264. duk_get_prop_index(ctx, 0, i);
  23265. duk_put_prop_index(ctx, 2, offset_elems + i);
  23266. }
  23267. }
  23268. return 0;
  23269. }
  23270. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23271. DUK_INTERNAL duk_ret_t duk_bi_typedarray_set(duk_context *ctx) {
  23272. DUK_UNREF(ctx);
  23273. return DUK_RET_UNSUPPORTED_ERROR;
  23274. }
  23275. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23276. /*
  23277. * Node.js Buffer.prototype.slice([start], [end])
  23278. * ArrayBuffer.prototype.slice(begin, [end])
  23279. * TypedArray.prototype.slice(begin, [end])
  23280. *
  23281. * The API calls are almost identical; negative indices are counted from end
  23282. * of buffer, and final indices are clamped (allowing crossed indices). Main
  23283. * differences:
  23284. *
  23285. * - Copy/view behavior; Node.js .slice() and TypedArray .subarray() create
  23286. * views, ArrayBuffer .slice() creates a copy
  23287. *
  23288. * - Resulting object has a different class and prototype depending on the
  23289. * call (or 'this' argument)
  23290. *
  23291. * - TypedArray .subarray() arguments are element indices, not byte offsets
  23292. */
  23293. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  23294. DUK_INTERNAL duk_ret_t duk_bi_buffer_slice_shared(duk_context *ctx) {
  23295. duk_hthread *thr;
  23296. duk_small_int_t magic;
  23297. duk_small_uint_t res_class_num;
  23298. duk_hobject *res_proto;
  23299. duk_hbufferobject *h_this;
  23300. duk_hbufferobject *h_bufobj;
  23301. duk_hbuffer *h_val;
  23302. duk_int_t start_offset, end_offset;
  23303. duk_uint_t slice_length;
  23304. thr = (duk_hthread *) ctx;
  23305. DUK_UNREF(thr);
  23306. /* [ start end ] */
  23307. magic = duk_get_current_magic(ctx);
  23308. h_this = duk__require_bufobj_this(ctx);
  23309. /* Slice offsets are element (not byte) offsets, which only matters
  23310. * for TypedArray views, Node.js Buffer and ArrayBuffer have shift
  23311. * zero so byte and element offsets are the same. Negative indices
  23312. * are counted from end of slice, crossed indices are allowed (and
  23313. * result in zero length result), and final values are clamped
  23314. * against the current slice. There's intentionally no check
  23315. * against the underlying buffer here.
  23316. */
  23317. duk__clamp_startend_negidx_shifted(ctx, h_this, 0 /*idx_start*/, 1 /*idx_end*/, &start_offset, &end_offset);
  23318. DUK_ASSERT(end_offset >= start_offset);
  23319. slice_length = (duk_uint_t) (end_offset - start_offset);
  23320. /* The resulting buffer object gets the same class and prototype as
  23321. * the buffer in 'this', e.g. if the input is a Node.js Buffer the
  23322. * result is a Node.js Buffer; if the input is a Float32Array, the
  23323. * result is a Float32Array.
  23324. *
  23325. * For the class number this seems correct. The internal prototype
  23326. * is not so clear: if 'this' is a bufferobject with a non-standard
  23327. * prototype object, that value gets copied over into the result
  23328. * (instead of using the standard prototype for that object type).
  23329. */
  23330. res_class_num = DUK_HOBJECT_GET_CLASS_NUMBER((duk_hobject *) h_this);
  23331. h_bufobj = duk_push_bufferobject_raw(ctx,
  23332. DUK_HOBJECT_FLAG_EXTENSIBLE |
  23333. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  23334. DUK_HOBJECT_CLASS_AS_FLAGS(res_class_num),
  23335. DUK_BIDX_OBJECT_PROTOTYPE); /* replaced */
  23336. DUK_ASSERT(h_bufobj != NULL);
  23337. res_proto = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) h_this); /* may be NULL */
  23338. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) h_bufobj, res_proto);
  23339. h_bufobj->length = slice_length;
  23340. h_bufobj->shift = h_this->shift; /* inherit */
  23341. h_bufobj->elem_type = h_this->elem_type; /* inherit */
  23342. h_bufobj->is_view = magic & 0x01;
  23343. DUK_ASSERT(h_bufobj->is_view == 0 || h_bufobj->is_view == 1);
  23344. h_val = h_this->buf;
  23345. if (h_val == NULL) {
  23346. return DUK_RET_TYPE_ERROR;
  23347. }
  23348. if (magic & 0x02) {
  23349. /* non-zero: make copy */
  23350. duk_uint8_t *p_copy;
  23351. duk_size_t copy_length;
  23352. p_copy = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) slice_length);
  23353. DUK_ASSERT(p_copy != NULL);
  23354. /* Copy slice, respecting underlying buffer limits; remainder
  23355. * is left as zero.
  23356. */
  23357. copy_length = DUK_HBUFFEROBJECT_CLAMP_BYTELENGTH(h_this, slice_length);
  23358. DUK_MEMCPY((void *) p_copy,
  23359. (const void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + start_offset),
  23360. copy_length);
  23361. h_val = duk_get_hbuffer(ctx, -1);
  23362. DUK_ASSERT(h_val != NULL);
  23363. h_bufobj->buf = h_val;
  23364. DUK_HBUFFER_INCREF(thr, h_val);
  23365. DUK_ASSERT(h_bufobj->offset == 0);
  23366. duk_pop(ctx); /* reachable so pop OK */
  23367. } else {
  23368. h_bufobj->buf = h_val;
  23369. DUK_HBUFFER_INCREF(thr, h_val);
  23370. h_bufobj->offset = (duk_uint_t) (h_this->offset + start_offset);
  23371. /* Copy the .buffer property, needed for TypedArray.prototype.subarray().
  23372. *
  23373. * XXX: limit copy only for TypedArray classes specifically?
  23374. */
  23375. duk_push_this(ctx);
  23376. if (duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LC_BUFFER)) {
  23377. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE);
  23378. duk_pop(ctx);
  23379. } else {
  23380. duk_pop_2(ctx);
  23381. }
  23382. }
  23383. /* unbalanced stack on purpose */
  23384. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  23385. return 1;
  23386. }
  23387. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23388. DUK_INTERNAL duk_ret_t duk_bi_buffer_slice_shared(duk_context *ctx) {
  23389. DUK_UNREF(ctx);
  23390. return DUK_RET_UNSUPPORTED_ERROR;
  23391. }
  23392. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23393. /*
  23394. * Node.js Buffer.isEncoding()
  23395. */
  23396. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  23397. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_encoding(duk_context *ctx) {
  23398. const char *encoding;
  23399. /* only accept lowercase 'utf8' now. */
  23400. encoding = duk_to_string(ctx, 0);
  23401. DUK_ASSERT(duk_is_string(ctx, 0)); /* guaranteed by duk_to_string() */
  23402. duk_push_boolean(ctx, DUK_STRCMP(encoding, "utf8") == 0);
  23403. return 1;
  23404. }
  23405. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23406. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_encoding(duk_context *ctx) {
  23407. DUK_UNREF(ctx);
  23408. return DUK_RET_UNSUPPORTED_ERROR;
  23409. }
  23410. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23411. /*
  23412. * Node.js Buffer.isBuffer()
  23413. */
  23414. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  23415. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_buffer(duk_context *ctx) {
  23416. duk_hthread *thr;
  23417. duk_tval *tv;
  23418. duk_hobject *h;
  23419. duk_hobject *h_proto;
  23420. duk_bool_t ret = 0;
  23421. thr = (duk_hthread *) ctx;
  23422. DUK_ASSERT(duk_get_top(ctx) >= 1); /* nargs */
  23423. tv = duk_get_tval(ctx, 0);
  23424. DUK_ASSERT(tv != NULL);
  23425. if (DUK_TVAL_IS_OBJECT(tv)) {
  23426. h = DUK_TVAL_GET_OBJECT(tv);
  23427. DUK_ASSERT(h != NULL);
  23428. h_proto = thr->builtins[DUK_BIDX_NODEJS_BUFFER_PROTOTYPE];
  23429. DUK_ASSERT(h_proto != NULL);
  23430. h = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
  23431. if (h) {
  23432. ret = duk_hobject_prototype_chain_contains(thr, h, h_proto, 0 /*ignore_loop*/);
  23433. }
  23434. }
  23435. duk_push_boolean(ctx, ret);
  23436. return 1;
  23437. }
  23438. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23439. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_buffer(duk_context *ctx) {
  23440. DUK_UNREF(ctx);
  23441. return DUK_RET_UNSUPPORTED_ERROR;
  23442. }
  23443. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23444. /*
  23445. * Node.js Buffer.byteLength()
  23446. */
  23447. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  23448. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_byte_length(duk_context *ctx) {
  23449. const char *str;
  23450. duk_size_t len;
  23451. /* At the moment Buffer(<str>) will just use the string bytes as
  23452. * is (ignoring encoding), so we return the string length here
  23453. * unconditionally.
  23454. */
  23455. str = duk_to_lstring(ctx, 0, &len);
  23456. DUK_UNREF(str);
  23457. duk_push_size_t(ctx, len);
  23458. return 1;
  23459. }
  23460. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23461. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_byte_length(duk_context *ctx) {
  23462. DUK_UNREF(ctx);
  23463. return DUK_RET_UNSUPPORTED_ERROR;
  23464. }
  23465. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23466. /*
  23467. * Node.js Buffer.concat()
  23468. */
  23469. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  23470. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_concat(duk_context *ctx) {
  23471. duk_hthread *thr;
  23472. duk_hobject *h_arg;
  23473. duk_int_t total_length = 0;
  23474. duk_hbufferobject *h_bufobj;
  23475. duk_hbufferobject *h_bufres;
  23476. duk_hbuffer *h_val;
  23477. duk_uint_t i, n;
  23478. duk_uint8_t *p;
  23479. duk_size_t space_left;
  23480. duk_size_t copy_size;
  23481. thr = (duk_hthread *) ctx;
  23482. DUK_UNREF(thr);
  23483. /* Node.js accepts only actual Arrays. */
  23484. h_arg = duk_require_hobject(ctx, 0);
  23485. if (DUK_HOBJECT_GET_CLASS_NUMBER(h_arg) != DUK_HOBJECT_CLASS_ARRAY) {
  23486. return DUK_RET_TYPE_ERROR;
  23487. }
  23488. /* Compute result length and validate argument buffers. */
  23489. n = (duk_uint_t) duk_get_length(ctx, 0);
  23490. for (i = 0; i < n; i++) {
  23491. /* Neutered checks not necessary here: neutered buffers have
  23492. * zero 'length' so we'll effectively skip them.
  23493. */
  23494. DUK_ASSERT_TOP(ctx, 2); /* [ array totalLength ] */
  23495. duk_get_prop_index(ctx, 0, (duk_uarridx_t) i); /* -> [ array totalLength buf ] */
  23496. h_bufobj = duk__require_bufobj_value(ctx, 2);
  23497. DUK_ASSERT(h_bufobj != NULL);
  23498. total_length += h_bufobj->length;
  23499. duk_pop(ctx);
  23500. }
  23501. if (n == 1) {
  23502. /* For the case n==1 Node.js doesn't seem to type check
  23503. * the sole member but we do it before returning it.
  23504. * For this case only the original buffer object is
  23505. * returned (not a copy).
  23506. */
  23507. duk_get_prop_index(ctx, 0, 0);
  23508. return 1;
  23509. }
  23510. /* User totalLength overrides a computed length, but we'll check
  23511. * every copy in the copy loop. Note that duk_to_uint() can
  23512. * technically have arbitrary side effects so we need to recheck
  23513. * the buffers in the copy loop.
  23514. */
  23515. if (!duk_is_undefined(ctx, 1) && n > 0) {
  23516. /* For n == 0, Node.js ignores totalLength argument and
  23517. * returns a zero length buffer.
  23518. */
  23519. total_length = duk_to_int(ctx, 1);
  23520. }
  23521. if (total_length < 0) {
  23522. return DUK_RET_RANGE_ERROR;
  23523. }
  23524. h_bufres = duk_push_bufferobject_raw(ctx,
  23525. DUK_HOBJECT_FLAG_EXTENSIBLE |
  23526. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  23527. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER),
  23528. DUK_BIDX_NODEJS_BUFFER_PROTOTYPE);
  23529. DUK_ASSERT(h_bufres != NULL);
  23530. p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, total_length);
  23531. DUK_ASSERT(p != NULL);
  23532. space_left = total_length;
  23533. for (i = 0; i < n; i++) {
  23534. DUK_ASSERT_TOP(ctx, 4); /* [ array totalLength bufres buf ] */
  23535. duk_get_prop_index(ctx, 0, (duk_uarridx_t) i);
  23536. h_bufobj = duk__require_bufobj_value(ctx, 4);
  23537. DUK_ASSERT(h_bufobj != NULL);
  23538. copy_size = h_bufobj->length;
  23539. if (copy_size > space_left) {
  23540. copy_size = space_left;
  23541. }
  23542. if (h_bufobj->buf != NULL &&
  23543. DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj)) {
  23544. DUK_MEMCPY((void *) p,
  23545. (const void *) DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufobj),
  23546. copy_size);
  23547. } else {
  23548. /* Just skip, leaving zeroes in the result. */
  23549. ;
  23550. }
  23551. p += copy_size;
  23552. space_left -= copy_size;
  23553. duk_pop(ctx);
  23554. }
  23555. h_val = duk_get_hbuffer(ctx, -1);
  23556. DUK_ASSERT(h_val != NULL);
  23557. duk__set_bufobj_buffer(ctx, h_bufres, h_val);
  23558. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufres);
  23559. duk_pop(ctx); /* pop plain buffer, now reachable through h_bufres */
  23560. return 1; /* return h_bufres */
  23561. }
  23562. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23563. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_concat(duk_context *ctx) {
  23564. DUK_UNREF(ctx);
  23565. return DUK_RET_UNSUPPORTED_ERROR;
  23566. }
  23567. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23568. /*
  23569. * Shared readfield and writefield methods
  23570. *
  23571. * The readfield/writefield methods need support for endianness and field
  23572. * types. All offsets are byte based so no offset shifting is needed.
  23573. */
  23574. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  23575. /* Format of magic, bits:
  23576. * 0...1: field type; 0=uint8, 1=uint16, 2=uint32, 3=float, 4=double, 5=unused, 6=unused, 7=unused
  23577. * 3: endianness: 0=little, 1=big
  23578. * 4: signed: 1=yes, 0=no
  23579. * 5: typedarray: 1=yes, 0=no
  23580. */
  23581. #define DUK__FLD_8BIT 0
  23582. #define DUK__FLD_16BIT 1
  23583. #define DUK__FLD_32BIT 2
  23584. #define DUK__FLD_FLOAT 3
  23585. #define DUK__FLD_DOUBLE 4
  23586. #define DUK__FLD_VARINT 5
  23587. #define DUK__FLD_BIGENDIAN (1 << 3)
  23588. #define DUK__FLD_SIGNED (1 << 4)
  23589. #define DUK__FLD_TYPEDARRAY (1 << 5)
  23590. /* XXX: split into separate functions for each field type? */
  23591. DUK_INTERNAL duk_ret_t duk_bi_buffer_readfield(duk_context *ctx) {
  23592. duk_hthread *thr;
  23593. duk_small_int_t magic = (duk_small_int_t) duk_get_current_magic(ctx);
  23594. duk_small_int_t magic_ftype;
  23595. duk_small_int_t magic_bigendian;
  23596. duk_small_int_t magic_signed;
  23597. duk_small_int_t magic_typedarray;
  23598. duk_small_int_t endswap;
  23599. duk_hbufferobject *h_this;
  23600. duk_bool_t no_assert;
  23601. duk_int_t offset_signed;
  23602. duk_uint_t offset;
  23603. duk_uint_t buffer_length;
  23604. duk_uint_t check_length;
  23605. duk_uint8_t *buf;
  23606. duk_double_union du;
  23607. thr = (duk_hthread *) ctx;
  23608. DUK_UNREF(thr);
  23609. magic_ftype = magic & 0x0007;
  23610. magic_bigendian = magic & 0x0008;
  23611. magic_signed = magic & 0x0010;
  23612. magic_typedarray = magic & 0x0020;
  23613. h_this = duk__require_bufobj_this(ctx);
  23614. DUK_ASSERT(h_this != NULL);
  23615. buffer_length = h_this->length;
  23616. /* [ offset noAssert ], when ftype != DUK__FLD_VARINT */
  23617. /* [ offset fieldByteLength noAssert ], when ftype == DUK__FLD_VARINT */
  23618. /* [ offset littleEndian ], when DUK__FLD_TYPEDARRAY (regardless of ftype) */
  23619. /* Handle TypedArray vs. Node.js Buffer arg differences */
  23620. if (magic_typedarray) {
  23621. no_assert = 0;
  23622. #if defined(DUK_USE_INTEGER_LE)
  23623. endswap = !duk_to_boolean(ctx, 1); /* 1=little endian */
  23624. #else
  23625. endswap = duk_to_boolean(ctx, 1); /* 1=little endian */
  23626. #endif
  23627. } else {
  23628. no_assert = duk_to_boolean(ctx, (magic_ftype == DUK__FLD_VARINT) ? 2 : 1);
  23629. #if defined(DUK_USE_INTEGER_LE)
  23630. endswap = magic_bigendian;
  23631. #else
  23632. endswap = !magic_bigendian;
  23633. #endif
  23634. }
  23635. /* Offset is coerced first to signed integer range and then to unsigned.
  23636. * This ensures we can add a small byte length (1-8) to the offset in
  23637. * bound checks and not wrap.
  23638. */
  23639. offset_signed = duk_to_int(ctx, 0);
  23640. offset = (duk_uint_t) offset_signed;
  23641. if (offset_signed < 0) {
  23642. goto fail_bounds;
  23643. }
  23644. DUK_DDD(DUK_DDDPRINT("readfield, buffer_length=%ld, offset=%ld, no_assert=%d, "
  23645. "magic=%04x, magic_fieldtype=%d, magic_bigendian=%d, magic_signed=%d, "
  23646. "endswap=%d",
  23647. (long) buffer_length, (long) offset, (int) no_assert,
  23648. (unsigned int) magic, (int) magic_ftype, (int) (magic_bigendian >> 3),
  23649. (int) (magic_signed >> 4), (int) endswap));
  23650. /* Update 'buffer_length' to be the effective, safe limit which
  23651. * takes into account the underlying buffer. This value will be
  23652. * potentially invalidated by any side effect.
  23653. */
  23654. check_length = DUK_HBUFFEROBJECT_CLAMP_BYTELENGTH(h_this, buffer_length);
  23655. DUK_DDD(DUK_DDDPRINT("buffer_length=%ld, check_length=%ld",
  23656. (long) buffer_length, (long) check_length));
  23657. if (h_this->buf) {
  23658. buf = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this);
  23659. } else {
  23660. /* neutered, value doesn't matter because check_length is 0. */
  23661. DUK_ASSERT(check_length == 0);
  23662. buf = NULL;
  23663. }
  23664. switch (magic_ftype) {
  23665. case DUK__FLD_8BIT: {
  23666. duk_uint8_t tmp;
  23667. if (offset + 1U > check_length) {
  23668. goto fail_bounds;
  23669. }
  23670. tmp = buf[offset];
  23671. if (magic_signed) {
  23672. duk_push_int(ctx, (duk_int_t) ((duk_int8_t) tmp));
  23673. } else {
  23674. duk_push_uint(ctx, (duk_uint_t) tmp);
  23675. }
  23676. break;
  23677. }
  23678. case DUK__FLD_16BIT: {
  23679. duk_uint16_t tmp;
  23680. if (offset + 2U > check_length) {
  23681. goto fail_bounds;
  23682. }
  23683. DUK_MEMCPY((void *) du.uc, (const void *) (buf + offset), 2);
  23684. tmp = du.us[0];
  23685. if (endswap) {
  23686. tmp = DUK_BSWAP16(tmp);
  23687. }
  23688. if (magic_signed) {
  23689. duk_push_int(ctx, (duk_int_t) ((duk_int16_t) tmp));
  23690. } else {
  23691. duk_push_uint(ctx, (duk_uint_t) tmp);
  23692. }
  23693. break;
  23694. }
  23695. case DUK__FLD_32BIT: {
  23696. duk_uint32_t tmp;
  23697. if (offset + 4U > check_length) {
  23698. goto fail_bounds;
  23699. }
  23700. DUK_MEMCPY((void *) du.uc, (const void *) (buf + offset), 4);
  23701. tmp = du.ui[0];
  23702. if (endswap) {
  23703. tmp = DUK_BSWAP32(tmp);
  23704. }
  23705. if (magic_signed) {
  23706. duk_push_int(ctx, (duk_int_t) ((duk_int32_t) tmp));
  23707. } else {
  23708. duk_push_uint(ctx, (duk_uint_t) tmp);
  23709. }
  23710. break;
  23711. }
  23712. case DUK__FLD_FLOAT: {
  23713. duk_uint32_t tmp;
  23714. if (offset + 4U > check_length) {
  23715. goto fail_bounds;
  23716. }
  23717. DUK_MEMCPY((void *) du.uc, (const void *) (buf + offset), 4);
  23718. if (endswap) {
  23719. tmp = du.ui[0];
  23720. tmp = DUK_BSWAP32(tmp);
  23721. du.ui[0] = tmp;
  23722. }
  23723. duk_push_number(ctx, (duk_double_t) du.f[0]);
  23724. break;
  23725. }
  23726. case DUK__FLD_DOUBLE: {
  23727. if (offset + 8U > check_length) {
  23728. goto fail_bounds;
  23729. }
  23730. DUK_MEMCPY((void *) du.uc, (const void *) (buf + offset), 8);
  23731. if (endswap) {
  23732. DUK_DBLUNION_BSWAP64(&du);
  23733. }
  23734. duk_push_number(ctx, (duk_double_t) du.d);
  23735. break;
  23736. }
  23737. case DUK__FLD_VARINT: {
  23738. /* Node.js Buffer variable width integer field. We don't really
  23739. * care about speed here, so aim for shortest algorithm.
  23740. */
  23741. duk_int_t field_bytelen;
  23742. duk_int_t i, i_step, i_end;
  23743. #if defined(DUK_USE_64BIT_OPS)
  23744. duk_int64_t tmp;
  23745. duk_small_uint_t shift_tmp;
  23746. #else
  23747. duk_double_t tmp;
  23748. duk_small_int_t highbyte;
  23749. #endif
  23750. const duk_uint8_t *p;
  23751. field_bytelen = duk_get_int(ctx, 1); /* avoid side effects! */
  23752. if (field_bytelen < 1 || field_bytelen > 6) {
  23753. goto fail_field_length;
  23754. }
  23755. if (offset + (duk_uint_t) field_bytelen > check_length) {
  23756. goto fail_bounds;
  23757. }
  23758. p = (const duk_uint8_t *) (buf + offset);
  23759. /* Slow gathering of value using either 64-bit arithmetic
  23760. * or IEEE doubles if 64-bit types not available. Handling
  23761. * of negative numbers is a bit non-obvious in both cases.
  23762. */
  23763. if (magic_bigendian) {
  23764. /* Gather in big endian */
  23765. i = 0;
  23766. i_step = 1;
  23767. i_end = field_bytelen; /* one i_step over */
  23768. } else {
  23769. /* Gather in little endian */
  23770. i = field_bytelen - 1;
  23771. i_step = -1;
  23772. i_end = -1; /* one i_step over */
  23773. }
  23774. #if defined(DUK_USE_64BIT_OPS)
  23775. tmp = 0;
  23776. do {
  23777. DUK_ASSERT(i >= 0 && i < field_bytelen);
  23778. tmp = (tmp << 8) + (duk_int64_t) p[i];
  23779. i += i_step;
  23780. } while (i != i_end);
  23781. if (magic_signed) {
  23782. /* Shift to sign extend. */
  23783. shift_tmp = 64 - (field_bytelen * 8);
  23784. tmp = (tmp << shift_tmp) >> shift_tmp;
  23785. }
  23786. duk_push_i64(ctx, tmp);
  23787. #else
  23788. highbyte = p[i];
  23789. if (magic_signed && (highbyte & 0x80) != 0) {
  23790. /* 0xff => 255 - 256 = -1; 0x80 => 128 - 256 = -128 */
  23791. tmp = (duk_double_t) (highbyte - 256);
  23792. } else {
  23793. tmp = (duk_double_t) highbyte;
  23794. }
  23795. for (;;) {
  23796. i += i_step;
  23797. if (i == i_end) {
  23798. break;
  23799. }
  23800. DUK_ASSERT(i >= 0 && i < field_bytelen);
  23801. tmp = (tmp * 256.0) + (duk_double_t) p[i];
  23802. }
  23803. duk_push_number(ctx, tmp);
  23804. #endif
  23805. break;
  23806. }
  23807. default: { /* should never happen but default here */
  23808. goto fail_bounds;
  23809. }
  23810. }
  23811. return 1;
  23812. fail_field_length:
  23813. fail_bounds:
  23814. if (no_assert) {
  23815. /* Node.js return value for noAssert out-of-bounds reads is
  23816. * usually (but not always) NaN. Return NaN consistently.
  23817. */
  23818. duk_push_nan(ctx);
  23819. return 1;
  23820. }
  23821. return DUK_RET_RANGE_ERROR;
  23822. }
  23823. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23824. DUK_INTERNAL duk_ret_t duk_bi_buffer_readfield(duk_context *ctx) {
  23825. DUK_UNREF(ctx);
  23826. return DUK_RET_UNSUPPORTED_ERROR;
  23827. }
  23828. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23829. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  23830. /* XXX: split into separate functions for each field type? */
  23831. DUK_INTERNAL duk_ret_t duk_bi_buffer_writefield(duk_context *ctx) {
  23832. duk_hthread *thr;
  23833. duk_small_int_t magic = (duk_small_int_t) duk_get_current_magic(ctx);
  23834. duk_small_int_t magic_ftype;
  23835. duk_small_int_t magic_bigendian;
  23836. duk_small_int_t magic_signed;
  23837. duk_small_int_t magic_typedarray;
  23838. duk_small_int_t endswap;
  23839. duk_hbufferobject *h_this;
  23840. duk_bool_t no_assert;
  23841. duk_int_t offset_signed;
  23842. duk_uint_t offset;
  23843. duk_uint_t buffer_length;
  23844. duk_uint_t check_length;
  23845. duk_uint8_t *buf;
  23846. duk_double_union du;
  23847. duk_int_t nbytes = 0;
  23848. thr = (duk_hthread *) ctx;
  23849. DUK_UNREF(thr);
  23850. magic_ftype = magic & 0x0007;
  23851. magic_bigendian = magic & 0x0008;
  23852. magic_signed = magic & 0x0010;
  23853. magic_typedarray = magic & 0x0020;
  23854. DUK_UNREF(magic_signed);
  23855. h_this = duk__require_bufobj_this(ctx);
  23856. DUK_ASSERT(h_this != NULL);
  23857. buffer_length = h_this->length;
  23858. /* [ value offset noAssert ], when ftype != DUK__FLD_VARINT */
  23859. /* [ value offset fieldByteLength noAssert ], when ftype == DUK__FLD_VARINT */
  23860. /* [ offset value littleEndian ], when DUK__FLD_TYPEDARRAY (regardless of ftype) */
  23861. /* Handle TypedArray vs. Node.js Buffer arg differences */
  23862. if (magic_typedarray) {
  23863. no_assert = 0;
  23864. #if defined(DUK_USE_INTEGER_LE)
  23865. endswap = !duk_to_boolean(ctx, 2); /* 1=little endian */
  23866. #else
  23867. endswap = duk_to_boolean(ctx, 2); /* 1=little endian */
  23868. #endif
  23869. duk_swap(ctx, 0, 1); /* offset/value order different from Node.js */
  23870. } else {
  23871. no_assert = duk_to_boolean(ctx, (magic_ftype == DUK__FLD_VARINT) ? 3 : 2);
  23872. #if defined(DUK_USE_INTEGER_LE)
  23873. endswap = magic_bigendian;
  23874. #else
  23875. endswap = !magic_bigendian;
  23876. #endif
  23877. }
  23878. /* Offset is coerced first to signed integer range and then to unsigned.
  23879. * This ensures we can add a small byte length (1-8) to the offset in
  23880. * bound checks and not wrap.
  23881. */
  23882. offset_signed = duk_to_int(ctx, 1);
  23883. offset = (duk_uint_t) offset_signed;
  23884. /* We need 'nbytes' even for a failed offset; return value must be
  23885. * (offset + nbytes) even when write fails due to invalid offset.
  23886. */
  23887. if (magic_ftype != DUK__FLD_VARINT) {
  23888. DUK_ASSERT(magic_ftype >= 0 && magic_ftype < (duk_small_int_t) (sizeof(duk__buffer_nbytes_from_fldtype) / sizeof(duk_uint8_t)));
  23889. nbytes = duk__buffer_nbytes_from_fldtype[magic_ftype];
  23890. } else {
  23891. nbytes = duk_get_int(ctx, 2);
  23892. if (nbytes < 1 || nbytes > 6) {
  23893. goto fail_field_length;
  23894. }
  23895. }
  23896. DUK_ASSERT(nbytes >= 1 && nbytes <= 8);
  23897. /* Now we can check offset validity. */
  23898. if (offset_signed < 0) {
  23899. goto fail_bounds;
  23900. }
  23901. DUK_DDD(DUK_DDDPRINT("writefield, value=%!T, buffer_length=%ld, offset=%ld, no_assert=%d, "
  23902. "magic=%04x, magic_fieldtype=%d, magic_bigendian=%d, magic_signed=%d, "
  23903. "endswap=%d",
  23904. duk_get_tval(ctx, 0), (long) buffer_length, (long) offset, (int) no_assert,
  23905. (unsigned int) magic, (int) magic_ftype, (int) (magic_bigendian >> 3),
  23906. (int) (magic_signed >> 4), (int) endswap));
  23907. /* Coerce value to a number before computing check_length, so that
  23908. * the field type specific coercion below can't have side effects
  23909. * that would invalidate check_length.
  23910. */
  23911. duk_to_number(ctx, 0);
  23912. /* Update 'buffer_length' to be the effective, safe limit which
  23913. * takes into account the underlying buffer. This value will be
  23914. * potentially invalidated by any side effect.
  23915. */
  23916. check_length = DUK_HBUFFEROBJECT_CLAMP_BYTELENGTH(h_this, buffer_length);
  23917. DUK_DDD(DUK_DDDPRINT("buffer_length=%ld, check_length=%ld",
  23918. (long) buffer_length, (long) check_length));
  23919. if (h_this->buf) {
  23920. buf = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this);
  23921. } else {
  23922. /* neutered, value doesn't matter because check_length is 0. */
  23923. DUK_ASSERT(check_length == 0);
  23924. buf = NULL;
  23925. }
  23926. switch (magic_ftype) {
  23927. case DUK__FLD_8BIT: {
  23928. if (offset + 1U > check_length) {
  23929. goto fail_bounds;
  23930. }
  23931. /* sign doesn't matter when writing */
  23932. buf[offset] = (duk_uint8_t) duk_to_uint32(ctx, 0);
  23933. break;
  23934. }
  23935. case DUK__FLD_16BIT: {
  23936. duk_uint16_t tmp;
  23937. if (offset + 2U > check_length) {
  23938. goto fail_bounds;
  23939. }
  23940. tmp = (duk_uint16_t) duk_to_uint32(ctx, 0);
  23941. if (endswap) {
  23942. tmp = DUK_BSWAP16(tmp);
  23943. }
  23944. du.us[0] = tmp;
  23945. /* sign doesn't matter when writing */
  23946. DUK_MEMCPY((void *) (buf + offset), (const void *) du.uc, 2);
  23947. break;
  23948. }
  23949. case DUK__FLD_32BIT: {
  23950. duk_uint32_t tmp;
  23951. if (offset + 4U > check_length) {
  23952. goto fail_bounds;
  23953. }
  23954. tmp = (duk_uint32_t) duk_to_uint32(ctx, 0);
  23955. if (endswap) {
  23956. tmp = DUK_BSWAP32(tmp);
  23957. }
  23958. du.ui[0] = tmp;
  23959. /* sign doesn't matter when writing */
  23960. DUK_MEMCPY((void *) (buf + offset), (const void *) du.uc, 4);
  23961. break;
  23962. }
  23963. case DUK__FLD_FLOAT: {
  23964. duk_uint32_t tmp;
  23965. if (offset + 4U > check_length) {
  23966. goto fail_bounds;
  23967. }
  23968. du.f[0] = (duk_float_t) duk_to_number(ctx, 0);
  23969. if (endswap) {
  23970. tmp = du.ui[0];
  23971. tmp = DUK_BSWAP32(tmp);
  23972. du.ui[0] = tmp;
  23973. }
  23974. /* sign doesn't matter when writing */
  23975. DUK_MEMCPY((void *) (buf + offset), (const void *) du.uc, 4);
  23976. break;
  23977. }
  23978. case DUK__FLD_DOUBLE: {
  23979. if (offset + 8U > check_length) {
  23980. goto fail_bounds;
  23981. }
  23982. du.d = (duk_double_t) duk_to_number(ctx, 0);
  23983. if (endswap) {
  23984. DUK_DBLUNION_BSWAP64(&du);
  23985. }
  23986. /* sign doesn't matter when writing */
  23987. DUK_MEMCPY((void *) (buf + offset), (const void *) du.uc, 8);
  23988. break;
  23989. }
  23990. case DUK__FLD_VARINT: {
  23991. /* Node.js Buffer variable width integer field. We don't really
  23992. * care about speed here, so aim for shortest algorithm.
  23993. */
  23994. duk_int_t field_bytelen;
  23995. duk_int_t i, i_step, i_end;
  23996. #if defined(DUK_USE_64BIT_OPS)
  23997. duk_int64_t tmp;
  23998. #else
  23999. duk_double_t tmp;
  24000. #endif
  24001. duk_uint8_t *p;
  24002. field_bytelen = (duk_int_t) nbytes;
  24003. if (offset + (duk_uint_t) field_bytelen > check_length) {
  24004. goto fail_bounds;
  24005. }
  24006. /* Slow writing of value using either 64-bit arithmetic
  24007. * or IEEE doubles if 64-bit types not available. There's
  24008. * no special sign handling when writing varints.
  24009. */
  24010. if (magic_bigendian) {
  24011. /* Write in big endian */
  24012. i = field_bytelen; /* one i_step added at top of loop */
  24013. i_step = -1;
  24014. i_end = 0;
  24015. } else {
  24016. /* Write in little endian */
  24017. i = -1; /* one i_step added at top of loop */
  24018. i_step = 1;
  24019. i_end = field_bytelen - 1;
  24020. }
  24021. /* XXX: The duk_to_number() cast followed by integer coercion
  24022. * is platform specific so NaN, +/- Infinity, and out-of-bounds
  24023. * values result in platform specific output now.
  24024. * See: test-bi-nodejs-buffer-proto-varint-special.js
  24025. */
  24026. #if defined(DUK_USE_64BIT_OPS)
  24027. tmp = (duk_int64_t) duk_to_number(ctx, 0);
  24028. p = (duk_uint8_t *) (buf + offset);
  24029. do {
  24030. i += i_step;
  24031. DUK_ASSERT(i >= 0 && i < field_bytelen);
  24032. p[i] = (duk_uint8_t) (tmp & 0xff);
  24033. tmp = tmp >> 8; /* unnecessary shift for last byte */
  24034. } while (i != i_end);
  24035. #else
  24036. tmp = duk_to_number(ctx, 0);
  24037. p = (duk_uint8_t *) (buf + offset);
  24038. do {
  24039. i += i_step;
  24040. tmp = DUK_FLOOR(tmp);
  24041. DUK_ASSERT(i >= 0 && i < field_bytelen);
  24042. p[i] = (duk_uint8_t) (DUK_FMOD(tmp, 256.0));
  24043. tmp = tmp / 256.0; /* unnecessary div for last byte */
  24044. } while (i != i_end);
  24045. #endif
  24046. break;
  24047. }
  24048. default: { /* should never happen but default here */
  24049. goto fail_bounds;
  24050. }
  24051. }
  24052. /* Node.js Buffer: return offset + #bytes written (i.e. next
  24053. * write offset).
  24054. */
  24055. if (magic_typedarray) {
  24056. /* For TypedArrays 'undefined' return value is specified
  24057. * by ES6 (matches V8).
  24058. */
  24059. return 0;
  24060. }
  24061. duk_push_uint(ctx, offset + nbytes);
  24062. return 1;
  24063. fail_field_length:
  24064. fail_bounds:
  24065. if (no_assert) {
  24066. /* Node.js return value for failed writes is offset + #bytes
  24067. * that would have been written.
  24068. */
  24069. /* XXX: for negative input offsets, 'offset' will be a large
  24070. * positive value so the result here is confusing.
  24071. */
  24072. if (magic_typedarray) {
  24073. return 0;
  24074. }
  24075. duk_push_uint(ctx, offset + nbytes);
  24076. return 1;
  24077. }
  24078. return DUK_RET_RANGE_ERROR;
  24079. }
  24080. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24081. DUK_INTERNAL duk_ret_t duk_bi_buffer_writefield(duk_context *ctx) {
  24082. DUK_UNREF(ctx);
  24083. return DUK_RET_UNSUPPORTED_ERROR;
  24084. }
  24085. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24086. #undef DUK__FLD_8BIT
  24087. #undef DUK__FLD_16BIT
  24088. #undef DUK__FLD_32BIT
  24089. #undef DUK__FLD_FLOAT
  24090. #undef DUK__FLD_DOUBLE
  24091. #undef DUK__FLD_VARINT
  24092. #undef DUK__FLD_BIGENDIAN
  24093. #undef DUK__FLD_SIGNED
  24094. #undef DUK__FLD_TYPEDARRAY
  24095. #line 1 "duk_bi_date.c"
  24096. /*
  24097. * Date built-ins
  24098. *
  24099. * Unlike most built-ins, Date has some platform dependencies for getting
  24100. * UTC time, converting between UTC and local time, and parsing and
  24101. * formatting time values. These are all abstracted behind DUK_USE_xxx
  24102. * config options. There are built-in platform specific providers for
  24103. * POSIX and Windows, but external providers can also be used.
  24104. *
  24105. * See doc/datetime.rst.
  24106. *
  24107. */
  24108. /* include removed: duk_internal.h */
  24109. /*
  24110. * Forward declarations
  24111. */
  24112. DUK_LOCAL_DECL duk_double_t duk__push_this_get_timeval_tzoffset(duk_context *ctx, duk_small_uint_t flags, duk_int_t *out_tzoffset);
  24113. DUK_LOCAL_DECL duk_double_t duk__push_this_get_timeval(duk_context *ctx, duk_small_uint_t flags);
  24114. DUK_LOCAL_DECL void duk__twodigit_year_fixup(duk_context *ctx, duk_idx_t idx_val);
  24115. DUK_LOCAL_DECL duk_ret_t duk__set_this_timeval_from_dparts(duk_context *ctx, duk_double_t *dparts, duk_small_uint_t flags);
  24116. /*
  24117. * Other file level defines
  24118. */
  24119. /* Debug macro to print all parts and dparts (used manually because of debug level). */
  24120. #define DUK__DPRINT_PARTS_AND_DPARTS(parts,dparts) do { \
  24121. DUK_D(DUK_DPRINT("parts: %ld %ld %ld %ld %ld %ld %ld %ld, dparts: %lf %lf %lf %lf %lf %lf %lf %lf", \
  24122. (long) (parts)[0], (long) (parts)[1], \
  24123. (long) (parts)[2], (long) (parts)[3], \
  24124. (long) (parts)[4], (long) (parts)[5], \
  24125. (long) (parts)[6], (long) (parts)[7], \
  24126. (double) (dparts)[0], (double) (dparts)[1], \
  24127. (double) (dparts)[2], (double) (dparts)[3], \
  24128. (double) (dparts)[4], (double) (dparts)[5], \
  24129. (double) (dparts)[6], (double) (dparts)[7])); \
  24130. } while (0)
  24131. #define DUK__DPRINT_PARTS(parts) do { \
  24132. DUK_D(DUK_DPRINT("parts: %ld %ld %ld %ld %ld %ld %ld %ld", \
  24133. (long) (parts)[0], (long) (parts)[1], \
  24134. (long) (parts)[2], (long) (parts)[3], \
  24135. (long) (parts)[4], (long) (parts)[5], \
  24136. (long) (parts)[6], (long) (parts)[7])); \
  24137. } while (0)
  24138. #define DUK__DPRINT_DPARTS(dparts) do { \
  24139. DUK_D(DUK_DPRINT("dparts: %lf %lf %lf %lf %lf %lf %lf %lf", \
  24140. (double) (dparts)[0], (double) (dparts)[1], \
  24141. (double) (dparts)[2], (double) (dparts)[3], \
  24142. (double) (dparts)[4], (double) (dparts)[5], \
  24143. (double) (dparts)[6], (double) (dparts)[7])); \
  24144. } while (0)
  24145. /* Equivalent year for DST calculations outside [1970,2038[ range, see
  24146. * E5 Section 15.9.1.8. Equivalent year has the same leap-year-ness and
  24147. * starts with the same weekday on Jan 1.
  24148. * https://bugzilla.mozilla.org/show_bug.cgi?id=351066
  24149. */
  24150. #define DUK__YEAR(x) ((duk_uint8_t) ((x) - 1970))
  24151. DUK_LOCAL duk_uint8_t duk__date_equivyear[14] = {
  24152. #if 1
  24153. /* This is based on V8 EquivalentYear() algorithm (see src/genequivyear.py):
  24154. * http://code.google.com/p/v8/source/browse/trunk/src/date.h#146
  24155. */
  24156. /* non-leap year: sunday, monday, ... */
  24157. DUK__YEAR(2023), DUK__YEAR(2035), DUK__YEAR(2019), DUK__YEAR(2031),
  24158. DUK__YEAR(2015), DUK__YEAR(2027), DUK__YEAR(2011),
  24159. /* leap year: sunday, monday, ... */
  24160. DUK__YEAR(2012), DUK__YEAR(2024), DUK__YEAR(2008), DUK__YEAR(2020),
  24161. DUK__YEAR(2032), DUK__YEAR(2016), DUK__YEAR(2028)
  24162. #endif
  24163. #if 0
  24164. /* This is based on Rhino EquivalentYear() algorithm:
  24165. * https://github.com/mozilla/rhino/blob/f99cc11d616f0cdda2c42bde72b3484df6182947/src/org/mozilla/javascript/NativeDate.java
  24166. */
  24167. /* non-leap year: sunday, monday, ... */
  24168. DUK__YEAR(1978), DUK__YEAR(1973), DUK__YEAR(1985), DUK__YEAR(1986),
  24169. DUK__YEAR(1981), DUK__YEAR(1971), DUK__YEAR(1977),
  24170. /* leap year: sunday, monday, ... */
  24171. DUK__YEAR(1984), DUK__YEAR(1996), DUK__YEAR(1980), DUK__YEAR(1992),
  24172. DUK__YEAR(1976), DUK__YEAR(1988), DUK__YEAR(1972)
  24173. #endif
  24174. };
  24175. #undef DUK__YEAR
  24176. /*
  24177. * ISO 8601 subset parser.
  24178. */
  24179. /* Parser part count. */
  24180. #define DUK__NUM_ISO8601_PARSER_PARTS 9
  24181. /* Parser part indices. */
  24182. #define DUK__PI_YEAR 0
  24183. #define DUK__PI_MONTH 1
  24184. #define DUK__PI_DAY 2
  24185. #define DUK__PI_HOUR 3
  24186. #define DUK__PI_MINUTE 4
  24187. #define DUK__PI_SECOND 5
  24188. #define DUK__PI_MILLISECOND 6
  24189. #define DUK__PI_TZHOUR 7
  24190. #define DUK__PI_TZMINUTE 8
  24191. /* Parser part masks. */
  24192. #define DUK__PM_YEAR (1 << DUK__PI_YEAR)
  24193. #define DUK__PM_MONTH (1 << DUK__PI_MONTH)
  24194. #define DUK__PM_DAY (1 << DUK__PI_DAY)
  24195. #define DUK__PM_HOUR (1 << DUK__PI_HOUR)
  24196. #define DUK__PM_MINUTE (1 << DUK__PI_MINUTE)
  24197. #define DUK__PM_SECOND (1 << DUK__PI_SECOND)
  24198. #define DUK__PM_MILLISECOND (1 << DUK__PI_MILLISECOND)
  24199. #define DUK__PM_TZHOUR (1 << DUK__PI_TZHOUR)
  24200. #define DUK__PM_TZMINUTE (1 << DUK__PI_TZMINUTE)
  24201. /* Parser separator indices. */
  24202. #define DUK__SI_PLUS 0
  24203. #define DUK__SI_MINUS 1
  24204. #define DUK__SI_T 2
  24205. #define DUK__SI_SPACE 3
  24206. #define DUK__SI_COLON 4
  24207. #define DUK__SI_PERIOD 5
  24208. #define DUK__SI_Z 6
  24209. #define DUK__SI_NUL 7
  24210. /* Parser separator masks. */
  24211. #define DUK__SM_PLUS (1 << DUK__SI_PLUS)
  24212. #define DUK__SM_MINUS (1 << DUK__SI_MINUS)
  24213. #define DUK__SM_T (1 << DUK__SI_T)
  24214. #define DUK__SM_SPACE (1 << DUK__SI_SPACE)
  24215. #define DUK__SM_COLON (1 << DUK__SI_COLON)
  24216. #define DUK__SM_PERIOD (1 << DUK__SI_PERIOD)
  24217. #define DUK__SM_Z (1 << DUK__SI_Z)
  24218. #define DUK__SM_NUL (1 << DUK__SI_NUL)
  24219. /* Rule control flags. */
  24220. #define DUK__CF_NEG (1 << 0) /* continue matching, set neg_tzoffset flag */
  24221. #define DUK__CF_ACCEPT (1 << 1) /* accept string */
  24222. #define DUK__CF_ACCEPT_NUL (1 << 2) /* accept string if next char is NUL (otherwise reject) */
  24223. #define DUK__PACK_RULE(partmask,sepmask,nextpart,flags) \
  24224. ((duk_uint32_t) (partmask) + \
  24225. (((duk_uint32_t) (sepmask)) << 9) + \
  24226. (((duk_uint32_t) (nextpart)) << 17) + \
  24227. (((duk_uint32_t) (flags)) << 21))
  24228. #define DUK__UNPACK_RULE(rule,var_nextidx,var_flags) do { \
  24229. (var_nextidx) = (duk_small_uint_t) (((rule) >> 17) & 0x0f); \
  24230. (var_flags) = (duk_small_uint_t) ((rule) >> 21); \
  24231. } while (0)
  24232. #define DUK__RULE_MASK_PART_SEP 0x1ffffUL
  24233. /* Matching separator index is used in the control table */
  24234. DUK_LOCAL const duk_uint8_t duk__parse_iso8601_seps[] = {
  24235. DUK_ASC_PLUS /*0*/, DUK_ASC_MINUS /*1*/, DUK_ASC_UC_T /*2*/, DUK_ASC_SPACE /*3*/,
  24236. DUK_ASC_COLON /*4*/, DUK_ASC_PERIOD /*5*/, DUK_ASC_UC_Z /*6*/, DUK_ASC_NUL /*7*/
  24237. };
  24238. /* Rule table: first matching rule is used to determine what to do next. */
  24239. DUK_LOCAL const duk_uint32_t duk__parse_iso8601_control[] = {
  24240. DUK__PACK_RULE(DUK__PM_YEAR, DUK__SM_MINUS, DUK__PI_MONTH, 0),
  24241. DUK__PACK_RULE(DUK__PM_MONTH, DUK__SM_MINUS, DUK__PI_DAY, 0),
  24242. DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY, DUK__SM_T | DUK__SM_SPACE, DUK__PI_HOUR, 0),
  24243. DUK__PACK_RULE(DUK__PM_HOUR, DUK__SM_COLON, DUK__PI_MINUTE, 0),
  24244. DUK__PACK_RULE(DUK__PM_MINUTE, DUK__SM_COLON, DUK__PI_SECOND, 0),
  24245. DUK__PACK_RULE(DUK__PM_SECOND, DUK__SM_PERIOD, DUK__PI_MILLISECOND, 0),
  24246. DUK__PACK_RULE(DUK__PM_TZHOUR, DUK__SM_COLON, DUK__PI_TZMINUTE, 0),
  24247. 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),
  24248. 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),
  24249. 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),
  24250. 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)
  24251. /* Note1: the specification doesn't require matching a time form with
  24252. * just hours ("HH"), but we accept it here, e.g. "2012-01-02T12Z".
  24253. *
  24254. * Note2: the specification doesn't require matching a timezone offset
  24255. * with just hours ("HH"), but accept it here, e.g. "2012-01-02T03:04:05+02"
  24256. */
  24257. };
  24258. DUK_LOCAL duk_bool_t duk__parse_string_iso8601_subset(duk_context *ctx, const char *str) {
  24259. duk_int_t parts[DUK__NUM_ISO8601_PARSER_PARTS];
  24260. duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS];
  24261. duk_double_t d;
  24262. const duk_uint8_t *p;
  24263. duk_small_uint_t part_idx = 0;
  24264. duk_int_t accum = 0;
  24265. duk_small_uint_t ndigits = 0;
  24266. duk_bool_t neg_year = 0;
  24267. duk_bool_t neg_tzoffset = 0;
  24268. duk_uint_fast8_t ch;
  24269. duk_small_uint_t i;
  24270. /* During parsing, month and day are one-based; set defaults here. */
  24271. DUK_MEMZERO(parts, sizeof(parts));
  24272. DUK_ASSERT(parts[DUK_DATE_IDX_YEAR] == 0); /* don't care value, year is mandatory */
  24273. parts[DUK_DATE_IDX_MONTH] = 1;
  24274. parts[DUK_DATE_IDX_DAY] = 1;
  24275. /* Special handling for year sign. */
  24276. p = (const duk_uint8_t *) str;
  24277. ch = p[0];
  24278. if (ch == DUK_ASC_PLUS) {
  24279. p++;
  24280. } else if (ch == DUK_ASC_MINUS) {
  24281. neg_year = 1;
  24282. p++;
  24283. }
  24284. for (;;) {
  24285. ch = *p++;
  24286. DUK_DDD(DUK_DDDPRINT("parsing, part_idx=%ld, char=%ld ('%c')",
  24287. (long) part_idx, (long) ch,
  24288. (int) ((ch >= 0x20 && ch <= 0x7e) ? ch : DUK_ASC_QUESTION)));
  24289. if (ch >= DUK_ASC_0 && ch <= DUK_ASC_9) {
  24290. if (ndigits >= 9) {
  24291. DUK_DDD(DUK_DDDPRINT("too many digits -> reject"));
  24292. goto reject;
  24293. }
  24294. if (part_idx == DUK__PI_MILLISECOND /*msec*/ && ndigits >= 3) {
  24295. /* ignore millisecond fractions after 3 */
  24296. } else {
  24297. accum = accum * 10 + ((duk_int_t) ch) - ((duk_int_t) DUK_ASC_0) + 0x00;
  24298. ndigits++;
  24299. }
  24300. } else {
  24301. duk_uint_fast32_t match_val;
  24302. duk_small_int_t sep_idx;
  24303. if (ndigits <= 0) {
  24304. goto reject;
  24305. }
  24306. if (part_idx == DUK__PI_MILLISECOND) {
  24307. /* complete the millisecond field */
  24308. while (ndigits < 3) {
  24309. accum *= 10;
  24310. ndigits++;
  24311. }
  24312. }
  24313. parts[part_idx] = accum;
  24314. DUK_DDD(DUK_DDDPRINT("wrote part %ld -> value %ld", (long) part_idx, (long) accum));
  24315. accum = 0;
  24316. ndigits = 0;
  24317. for (i = 0; i < (duk_small_uint_t) (sizeof(duk__parse_iso8601_seps) / sizeof(duk_uint8_t)); i++) {
  24318. if (duk__parse_iso8601_seps[i] == ch) {
  24319. break;
  24320. }
  24321. }
  24322. if (i == (duk_small_uint_t) (sizeof(duk__parse_iso8601_seps) / sizeof(duk_uint8_t))) {
  24323. DUK_DDD(DUK_DDDPRINT("separator character doesn't match -> reject"));
  24324. goto reject;
  24325. }
  24326. sep_idx = i;
  24327. match_val = (1UL << part_idx) + (1UL << (sep_idx + 9)); /* match against rule part/sep bits */
  24328. for (i = 0; i < (duk_small_uint_t) (sizeof(duk__parse_iso8601_control) / sizeof(duk_uint32_t)); i++) {
  24329. duk_uint_fast32_t rule = duk__parse_iso8601_control[i];
  24330. duk_small_uint_t nextpart;
  24331. duk_small_uint_t cflags;
  24332. DUK_DDD(DUK_DDDPRINT("part_idx=%ld, sep_idx=%ld, match_val=0x%08lx, considering rule=0x%08lx",
  24333. (long) part_idx, (long) sep_idx,
  24334. (unsigned long) match_val, (unsigned long) rule));
  24335. if ((rule & match_val) != match_val) {
  24336. continue;
  24337. }
  24338. DUK__UNPACK_RULE(rule, nextpart, cflags);
  24339. DUK_DDD(DUK_DDDPRINT("rule match -> part_idx=%ld, sep_idx=%ld, match_val=0x%08lx, "
  24340. "rule=0x%08lx -> nextpart=%ld, cflags=0x%02lx",
  24341. (long) part_idx, (long) sep_idx,
  24342. (unsigned long) match_val, (unsigned long) rule,
  24343. (long) nextpart, (unsigned long) cflags));
  24344. if (cflags & DUK__CF_NEG) {
  24345. neg_tzoffset = 1;
  24346. }
  24347. if (cflags & DUK__CF_ACCEPT) {
  24348. goto accept;
  24349. }
  24350. if (cflags & DUK__CF_ACCEPT_NUL) {
  24351. DUK_ASSERT(*(p - 1) != (char) 0);
  24352. if (*p == DUK_ASC_NUL) {
  24353. goto accept;
  24354. }
  24355. goto reject;
  24356. }
  24357. part_idx = nextpart;
  24358. break;
  24359. } /* rule match */
  24360. if (i == (duk_small_uint_t) (sizeof(duk__parse_iso8601_control) / sizeof(duk_uint32_t))) {
  24361. DUK_DDD(DUK_DDDPRINT("no rule matches -> reject"));
  24362. goto reject;
  24363. }
  24364. if (ch == 0) {
  24365. /* This shouldn't be necessary, but check just in case
  24366. * to avoid any chance of overruns.
  24367. */
  24368. DUK_DDD(DUK_DDDPRINT("NUL after rule matching (should not happen) -> reject"));
  24369. goto reject;
  24370. }
  24371. } /* if-digit-else-ctrl */
  24372. } /* char loop */
  24373. /* We should never exit the loop above, but if we do, reject
  24374. * by falling through.
  24375. */
  24376. DUK_DDD(DUK_DDDPRINT("fell out of char loop without explicit accept/reject -> reject"));
  24377. reject:
  24378. DUK_DDD(DUK_DDDPRINT("reject"));
  24379. return 0;
  24380. accept:
  24381. DUK_DDD(DUK_DDDPRINT("accept"));
  24382. /* Apply timezone offset to get the main parts in UTC */
  24383. if (neg_year) {
  24384. parts[DUK__PI_YEAR] = -parts[DUK__PI_YEAR];
  24385. }
  24386. if (neg_tzoffset) {
  24387. parts[DUK__PI_HOUR] += parts[DUK__PI_TZHOUR];
  24388. parts[DUK__PI_MINUTE] += parts[DUK__PI_TZMINUTE];
  24389. } else {
  24390. parts[DUK__PI_HOUR] -= parts[DUK__PI_TZHOUR];
  24391. parts[DUK__PI_MINUTE] -= parts[DUK__PI_TZMINUTE];
  24392. }
  24393. parts[DUK__PI_MONTH] -= 1; /* zero-based month */
  24394. parts[DUK__PI_DAY] -= 1; /* zero-based day */
  24395. /* Use double parts, they tolerate unnormalized time.
  24396. *
  24397. * Note: DUK_DATE_IDX_WEEKDAY is initialized with a bogus value (DUK__PI_TZHOUR)
  24398. * on purpose. It won't be actually used by duk_bi_date_get_timeval_from_dparts(),
  24399. * but will make the value initialized just in case, and avoid any
  24400. * potential for Valgrind issues.
  24401. */
  24402. for (i = 0; i < DUK_DATE_IDX_NUM_PARTS; i++) {
  24403. DUK_DDD(DUK_DDDPRINT("part[%ld] = %ld", (long) i, (long) parts[i]));
  24404. dparts[i] = parts[i];
  24405. }
  24406. d = duk_bi_date_get_timeval_from_dparts(dparts, 0 /*flags*/);
  24407. duk_push_number(ctx, d);
  24408. return 1;
  24409. }
  24410. /*
  24411. * Date/time parsing helper.
  24412. *
  24413. * Parse a datetime string into a time value. We must first try to parse
  24414. * the input according to the standard format in E5.1 Section 15.9.1.15.
  24415. * If that fails, we can try to parse using custom parsing, which can
  24416. * either be platform neutral (custom code) or platform specific (using
  24417. * existing platform API calls).
  24418. *
  24419. * Note in particular that we must parse whatever toString(), toUTCString(),
  24420. * and toISOString() can produce; see E5.1 Section 15.9.4.2.
  24421. *
  24422. * Returns 1 to allow tail calling.
  24423. *
  24424. * There is much room for improvement here with respect to supporting
  24425. * alternative datetime formats. For instance, V8 parses '2012-01-01' as
  24426. * UTC and '2012/01/01' as local time.
  24427. */
  24428. DUK_LOCAL duk_ret_t duk__parse_string(duk_context *ctx, const char *str) {
  24429. /* XXX: there is a small risk here: because the ISO 8601 parser is
  24430. * very loose, it may end up parsing some datetime values which
  24431. * would be better parsed with a platform specific parser.
  24432. */
  24433. DUK_ASSERT(str != NULL);
  24434. DUK_DDD(DUK_DDDPRINT("parse datetime from string '%s'", (const char *) str));
  24435. if (duk__parse_string_iso8601_subset(ctx, str) != 0) {
  24436. return 1;
  24437. }
  24438. #if defined(DUK_USE_DATE_PARSE_STRING)
  24439. /* Contract, either:
  24440. * - Push value on stack and return 1
  24441. * - Don't push anything on stack and return 0
  24442. */
  24443. if (DUK_USE_DATE_PARSE_STRING(ctx, str) != 0) {
  24444. return 1;
  24445. }
  24446. #else
  24447. /* No platform-specific parsing, this is not an error. */
  24448. #endif
  24449. duk_push_nan(ctx);
  24450. return 1;
  24451. }
  24452. /*
  24453. * Calendar helpers
  24454. *
  24455. * Some helpers are used for getters and can operate on normalized values
  24456. * which can be represented with 32-bit signed integers. Other helpers are
  24457. * needed by setters and operate on un-normalized double values, must watch
  24458. * out for non-finite numbers etc.
  24459. */
  24460. DUK_LOCAL duk_uint8_t duk__days_in_month[12] = {
  24461. (duk_uint8_t) 31, (duk_uint8_t) 28, (duk_uint8_t) 31, (duk_uint8_t) 30,
  24462. (duk_uint8_t) 31, (duk_uint8_t) 30, (duk_uint8_t) 31, (duk_uint8_t) 31,
  24463. (duk_uint8_t) 30, (duk_uint8_t) 31, (duk_uint8_t) 30, (duk_uint8_t) 31
  24464. };
  24465. /* Maximum iteration count for computing UTC-to-local time offset when
  24466. * creating an Ecmascript time value from local parts.
  24467. */
  24468. #define DUK__LOCAL_TZOFFSET_MAXITER 4
  24469. /* Because 'day since epoch' can be negative and is used to compute weekday
  24470. * using a modulo operation, add this multiple of 7 to avoid negative values
  24471. * when year is below 1970 epoch. Ecmascript time values are restricted to
  24472. * +/- 100 million days from epoch, so this adder fits nicely into 32 bits.
  24473. * Round to a multiple of 7 (= floor(100000000 / 7) * 7) and add margin.
  24474. */
  24475. #define DUK__WEEKDAY_MOD_ADDER (20000000 * 7) /* 0x08583b00 */
  24476. DUK_INTERNAL duk_bool_t duk_bi_date_is_leap_year(duk_int_t year) {
  24477. if ((year % 4) != 0) {
  24478. return 0;
  24479. }
  24480. if ((year % 100) != 0) {
  24481. return 1;
  24482. }
  24483. if ((year % 400) != 0) {
  24484. return 0;
  24485. }
  24486. return 1;
  24487. }
  24488. DUK_INTERNAL duk_bool_t duk_bi_date_timeval_in_valid_range(duk_double_t x) {
  24489. return (x >= -DUK_DATE_MSEC_100M_DAYS && x <= DUK_DATE_MSEC_100M_DAYS);
  24490. }
  24491. DUK_INTERNAL duk_bool_t duk_bi_date_timeval_in_leeway_range(duk_double_t x) {
  24492. return (x >= -DUK_DATE_MSEC_100M_DAYS_LEEWAY && x <= DUK_DATE_MSEC_100M_DAYS_LEEWAY);
  24493. }
  24494. DUK_INTERNAL duk_bool_t duk_bi_date_year_in_valid_range(duk_double_t x) {
  24495. return (x >= DUK_DATE_MIN_ECMA_YEAR && x <= DUK_DATE_MAX_ECMA_YEAR);
  24496. }
  24497. DUK_LOCAL duk_double_t duk__timeclip(duk_double_t x) {
  24498. if (!DUK_ISFINITE(x)) {
  24499. return DUK_DOUBLE_NAN;
  24500. }
  24501. if (!duk_bi_date_timeval_in_valid_range(x)) {
  24502. return DUK_DOUBLE_NAN;
  24503. }
  24504. x = duk_js_tointeger_number(x);
  24505. /* Here we'd have the option to normalize -0 to +0. */
  24506. return x;
  24507. }
  24508. /* Integer division which floors also negative values correctly. */
  24509. DUK_LOCAL duk_int_t duk__div_floor(duk_int_t a, duk_int_t b) {
  24510. DUK_ASSERT(b > 0);
  24511. if (a >= 0) {
  24512. return a / b;
  24513. } else {
  24514. /* e.g. a = -4, b = 5 --> -4 - 5 + 1 / 5 --> -8 / 5 --> -1
  24515. * a = -5, b = 5 --> -5 - 5 + 1 / 5 --> -9 / 5 --> -1
  24516. * a = -6, b = 5 --> -6 - 5 + 1 / 5 --> -10 / 5 --> -2
  24517. */
  24518. return (a - b + 1) / b;
  24519. }
  24520. }
  24521. /* Compute day number of the first day of a given year. */
  24522. DUK_LOCAL duk_int_t duk__day_from_year(duk_int_t year) {
  24523. /* Note: in integer arithmetic, (x / 4) is same as floor(x / 4) for non-negative
  24524. * values, but is incorrect for negative ones.
  24525. */
  24526. return 365 * (year - 1970)
  24527. + duk__div_floor(year - 1969, 4)
  24528. - duk__div_floor(year - 1901, 100)
  24529. + duk__div_floor(year - 1601, 400);
  24530. }
  24531. /* Given a day number, determine year and day-within-year. */
  24532. DUK_LOCAL duk_int_t duk__year_from_day(duk_int_t day, duk_small_int_t *out_day_within_year) {
  24533. duk_int_t year;
  24534. duk_int_t diff_days;
  24535. /* estimate year upwards (towards positive infinity), then back down;
  24536. * two iterations should be enough
  24537. */
  24538. if (day >= 0) {
  24539. year = 1970 + day / 365;
  24540. } else {
  24541. year = 1970 + day / 366;
  24542. }
  24543. for (;;) {
  24544. diff_days = duk__day_from_year(year) - day;
  24545. DUK_DDD(DUK_DDDPRINT("year=%ld day=%ld, diff_days=%ld", (long) year, (long) day, (long) diff_days));
  24546. if (diff_days <= 0) {
  24547. DUK_ASSERT(-diff_days < 366); /* fits into duk_small_int_t */
  24548. *out_day_within_year = -diff_days;
  24549. DUK_DDD(DUK_DDDPRINT("--> year=%ld, day-within-year=%ld",
  24550. (long) year, (long) *out_day_within_year));
  24551. DUK_ASSERT(*out_day_within_year >= 0);
  24552. DUK_ASSERT(*out_day_within_year < (duk_bi_date_is_leap_year(year) ? 366 : 365));
  24553. return year;
  24554. }
  24555. /* Note: this is very tricky; we must never 'overshoot' the
  24556. * correction downwards.
  24557. */
  24558. year -= 1 + (diff_days - 1) / 366; /* conservative */
  24559. }
  24560. }
  24561. /* Given a (year, month, day-within-month) triple, compute day number.
  24562. * The input triple is un-normalized and may contain non-finite values.
  24563. */
  24564. DUK_LOCAL duk_double_t duk__make_day(duk_double_t year, duk_double_t month, duk_double_t day) {
  24565. duk_int_t day_num;
  24566. duk_bool_t is_leap;
  24567. duk_small_int_t i, n;
  24568. /* Assume that year, month, day are all coerced to whole numbers.
  24569. * They may also be NaN or infinity, in which case this function
  24570. * must return NaN or infinity to ensure time value becomes NaN.
  24571. * If 'day' is NaN, the final return will end up returning a NaN,
  24572. * so it doesn't need to be checked here.
  24573. */
  24574. if (!DUK_ISFINITE(year) || !DUK_ISFINITE(month)) {
  24575. return DUK_DOUBLE_NAN;
  24576. }
  24577. year += DUK_FLOOR(month / 12.0);
  24578. month = DUK_FMOD(month, 12.0);
  24579. if (month < 0.0) {
  24580. /* handle negative values */
  24581. month += 12.0;
  24582. }
  24583. /* The algorithm in E5.1 Section 15.9.1.12 normalizes month, but
  24584. * does not normalize the day-of-month (nor check whether or not
  24585. * it is finite) because it's not necessary for finding the day
  24586. * number which matches the (year,month) pair.
  24587. *
  24588. * We assume that duk__day_from_year() is exact here.
  24589. *
  24590. * Without an explicit infinity / NaN check in the beginning,
  24591. * day_num would be a bogus integer here.
  24592. *
  24593. * It's possible for 'year' to be out of integer range here.
  24594. * If so, we need to return NaN without integer overflow.
  24595. * This fixes test-bug-setyear-overflow.js.
  24596. */
  24597. if (!duk_bi_date_year_in_valid_range(year)) {
  24598. DUK_DD(DUK_DDPRINT("year not in ecmascript valid range, avoid integer overflow: %lf", (double) year));
  24599. return DUK_DOUBLE_NAN;
  24600. }
  24601. day_num = duk__day_from_year((duk_int_t) year);
  24602. is_leap = duk_bi_date_is_leap_year((duk_int_t) year);
  24603. n = (duk_small_int_t) month;
  24604. for (i = 0; i < n; i++) {
  24605. day_num += duk__days_in_month[i];
  24606. if (i == 1 && is_leap) {
  24607. day_num++;
  24608. }
  24609. }
  24610. /* If 'day' is NaN, returns NaN. */
  24611. return (duk_double_t) day_num + day;
  24612. }
  24613. /* Split time value into parts. The time value is assumed to be an internal
  24614. * one, i.e. finite, no fractions. Possible local time adjustment has already
  24615. * been applied when reading the time value.
  24616. */
  24617. 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) {
  24618. duk_double_t d1, d2;
  24619. duk_int_t t1, t2;
  24620. duk_int_t day_since_epoch;
  24621. duk_int_t year; /* does not fit into 16 bits */
  24622. duk_small_int_t day_in_year;
  24623. duk_small_int_t month;
  24624. duk_small_int_t day;
  24625. duk_small_int_t dim;
  24626. duk_int_t jan1_since_epoch;
  24627. duk_small_int_t jan1_weekday;
  24628. duk_int_t equiv_year;
  24629. duk_small_uint_t i;
  24630. duk_bool_t is_leap;
  24631. duk_small_int_t arridx;
  24632. DUK_ASSERT(DUK_ISFINITE(d)); /* caller checks */
  24633. DUK_ASSERT(DUK_FLOOR(d) == d); /* no fractions in internal time */
  24634. /* The timevalue must be in valid Ecmascript range, but since a local
  24635. * time offset can be applied, we need to allow a +/- 24h leeway to
  24636. * the value. In other words, although the UTC time is within the
  24637. * Ecmascript range, the local part values can be just outside of it.
  24638. */
  24639. DUK_UNREF(duk_bi_date_timeval_in_leeway_range);
  24640. DUK_ASSERT(duk_bi_date_timeval_in_leeway_range(d));
  24641. /* these computations are guaranteed to be exact for the valid
  24642. * E5 time value range, assuming milliseconds without fractions.
  24643. */
  24644. d1 = (duk_double_t) DUK_FMOD(d, (double) DUK_DATE_MSEC_DAY);
  24645. if (d1 < 0.0) {
  24646. /* deal with negative values */
  24647. d1 += (duk_double_t) DUK_DATE_MSEC_DAY;
  24648. }
  24649. d2 = DUK_FLOOR((double) (d / (duk_double_t) DUK_DATE_MSEC_DAY));
  24650. DUK_ASSERT(d2 * ((duk_double_t) DUK_DATE_MSEC_DAY) + d1 == d);
  24651. /* now expected to fit into a 32-bit integer */
  24652. t1 = (duk_int_t) d1;
  24653. t2 = (duk_int_t) d2;
  24654. day_since_epoch = t2;
  24655. DUK_ASSERT((duk_double_t) t1 == d1);
  24656. DUK_ASSERT((duk_double_t) t2 == d2);
  24657. /* t1 = milliseconds within day (fits 32 bit)
  24658. * t2 = day number from epoch (fits 32 bit, may be negative)
  24659. */
  24660. parts[DUK_DATE_IDX_MILLISECOND] = t1 % 1000; t1 /= 1000;
  24661. parts[DUK_DATE_IDX_SECOND] = t1 % 60; t1 /= 60;
  24662. parts[DUK_DATE_IDX_MINUTE] = t1 % 60; t1 /= 60;
  24663. parts[DUK_DATE_IDX_HOUR] = t1;
  24664. DUK_ASSERT(parts[DUK_DATE_IDX_MILLISECOND] >= 0 && parts[DUK_DATE_IDX_MILLISECOND] <= 999);
  24665. DUK_ASSERT(parts[DUK_DATE_IDX_SECOND] >= 0 && parts[DUK_DATE_IDX_SECOND] <= 59);
  24666. DUK_ASSERT(parts[DUK_DATE_IDX_MINUTE] >= 0 && parts[DUK_DATE_IDX_MINUTE] <= 59);
  24667. DUK_ASSERT(parts[DUK_DATE_IDX_HOUR] >= 0 && parts[DUK_DATE_IDX_HOUR] <= 23);
  24668. DUK_DDD(DUK_DDDPRINT("d=%lf, d1=%lf, d2=%lf, t1=%ld, t2=%ld, parts: hour=%ld min=%ld sec=%ld msec=%ld",
  24669. (double) d, (double) d1, (double) d2, (long) t1, (long) t2,
  24670. (long) parts[DUK_DATE_IDX_HOUR],
  24671. (long) parts[DUK_DATE_IDX_MINUTE],
  24672. (long) parts[DUK_DATE_IDX_SECOND],
  24673. (long) parts[DUK_DATE_IDX_MILLISECOND]));
  24674. /* This assert depends on the input parts representing time inside
  24675. * the Ecmascript range.
  24676. */
  24677. DUK_ASSERT(t2 + DUK__WEEKDAY_MOD_ADDER >= 0);
  24678. parts[DUK_DATE_IDX_WEEKDAY] = (t2 + 4 + DUK__WEEKDAY_MOD_ADDER) % 7; /* E5.1 Section 15.9.1.6 */
  24679. DUK_ASSERT(parts[DUK_DATE_IDX_WEEKDAY] >= 0 && parts[DUK_DATE_IDX_WEEKDAY] <= 6);
  24680. year = duk__year_from_day(t2, &day_in_year);
  24681. day = day_in_year;
  24682. is_leap = duk_bi_date_is_leap_year(year);
  24683. for (month = 0; month < 12; month++) {
  24684. dim = duk__days_in_month[month];
  24685. if (month == 1 && is_leap) {
  24686. dim++;
  24687. }
  24688. DUK_DDD(DUK_DDDPRINT("month=%ld, dim=%ld, day=%ld",
  24689. (long) month, (long) dim, (long) day));
  24690. if (day < dim) {
  24691. break;
  24692. }
  24693. day -= dim;
  24694. }
  24695. DUK_DDD(DUK_DDDPRINT("final month=%ld", (long) month));
  24696. DUK_ASSERT(month >= 0 && month <= 11);
  24697. DUK_ASSERT(day >= 0 && day <= 31);
  24698. /* Equivalent year mapping, used to avoid DST trouble when platform
  24699. * may fail to provide reasonable DST answers for dates outside the
  24700. * ordinary range (e.g. 1970-2038). An equivalent year has the same
  24701. * leap-year-ness as the original year and begins on the same weekday
  24702. * (Jan 1).
  24703. *
  24704. * The year 2038 is avoided because there seem to be problems with it
  24705. * on some platforms. The year 1970 is also avoided as there were
  24706. * practical problems with it; an equivalent year is used for it too,
  24707. * which breaks some DST computations for 1970 right now, see e.g.
  24708. * test-bi-date-tzoffset-brute-fi.js.
  24709. */
  24710. if ((flags & DUK_DATE_FLAG_EQUIVYEAR) && (year < 1971 || year > 2037)) {
  24711. DUK_ASSERT(is_leap == 0 || is_leap == 1);
  24712. jan1_since_epoch = day_since_epoch - day_in_year; /* day number for Jan 1 since epoch */
  24713. DUK_ASSERT(jan1_since_epoch + DUK__WEEKDAY_MOD_ADDER >= 0);
  24714. jan1_weekday = (jan1_since_epoch + 4 + DUK__WEEKDAY_MOD_ADDER) % 7; /* E5.1 Section 15.9.1.6 */
  24715. DUK_ASSERT(jan1_weekday >= 0 && jan1_weekday <= 6);
  24716. arridx = jan1_weekday;
  24717. if (is_leap) {
  24718. arridx += 7;
  24719. }
  24720. DUK_ASSERT(arridx >= 0 && arridx < (duk_small_int_t) (sizeof(duk__date_equivyear) / sizeof(duk_uint8_t)));
  24721. equiv_year = (duk_int_t) duk__date_equivyear[arridx] + 1970;
  24722. year = equiv_year;
  24723. DUK_DDD(DUK_DDDPRINT("equiv year mapping, year=%ld, day_in_year=%ld, day_since_epoch=%ld, "
  24724. "jan1_since_epoch=%ld, jan1_weekday=%ld -> equiv year %ld",
  24725. (long) year, (long) day_in_year, (long) day_since_epoch,
  24726. (long) jan1_since_epoch, (long) jan1_weekday, (long) equiv_year));
  24727. }
  24728. parts[DUK_DATE_IDX_YEAR] = year;
  24729. parts[DUK_DATE_IDX_MONTH] = month;
  24730. parts[DUK_DATE_IDX_DAY] = day;
  24731. if (flags & DUK_DATE_FLAG_ONEBASED) {
  24732. parts[DUK_DATE_IDX_MONTH]++; /* zero-based -> one-based */
  24733. parts[DUK_DATE_IDX_DAY]++; /* -""- */
  24734. }
  24735. if (dparts != NULL) {
  24736. for (i = 0; i < DUK_DATE_IDX_NUM_PARTS; i++) {
  24737. dparts[i] = (duk_double_t) parts[i];
  24738. }
  24739. }
  24740. }
  24741. /* Compute time value from (double) parts. The parts can be either UTC
  24742. * or local time; if local, they need to be (conceptually) converted into
  24743. * UTC time. The parts may represent valid or invalid time, and may be
  24744. * wildly out of range (but may cancel each other and still come out in
  24745. * the valid Date range).
  24746. */
  24747. DUK_INTERNAL duk_double_t duk_bi_date_get_timeval_from_dparts(duk_double_t *dparts, duk_small_uint_t flags) {
  24748. #if defined(DUK_USE_PARANOID_DATE_COMPUTATION)
  24749. /* See comments below on MakeTime why these are volatile. */
  24750. volatile duk_double_t tmp_time;
  24751. volatile duk_double_t tmp_day;
  24752. volatile duk_double_t d;
  24753. #else
  24754. duk_double_t tmp_time;
  24755. duk_double_t tmp_day;
  24756. duk_double_t d;
  24757. #endif
  24758. duk_small_uint_t i;
  24759. duk_int_t tzoff, tzoffprev1, tzoffprev2;
  24760. /* Expects 'this' at top of stack on entry. */
  24761. /* Coerce all finite parts with ToInteger(). ToInteger() must not
  24762. * be called for NaN/Infinity because it will convert e.g. NaN to
  24763. * zero. If ToInteger() has already been called, this has no side
  24764. * effects and is idempotent.
  24765. *
  24766. * Don't read dparts[DUK_DATE_IDX_WEEKDAY]; it will cause Valgrind
  24767. * issues if the value is uninitialized.
  24768. */
  24769. for (i = 0; i <= DUK_DATE_IDX_MILLISECOND; i++) {
  24770. /* SCANBUILD: scan-build complains here about assigned value
  24771. * being garbage or undefined. This is correct but operating
  24772. * on undefined values has no ill effect and is ignored by the
  24773. * caller in the case where this happens.
  24774. */
  24775. d = dparts[i];
  24776. if (DUK_ISFINITE(d)) {
  24777. dparts[i] = duk_js_tointeger_number(d);
  24778. }
  24779. }
  24780. /* Use explicit steps in computation to try to ensure that
  24781. * computation happens with intermediate results coerced to
  24782. * double values (instead of using something more accurate).
  24783. * E.g. E5.1 Section 15.9.1.11 requires use of IEEE 754
  24784. * rules (= Ecmascript '+' and '*' operators).
  24785. *
  24786. * Without 'volatile' even this approach fails on some platform
  24787. * and compiler combinations. For instance, gcc 4.8.1 on Ubuntu
  24788. * 64-bit, with -m32 and without -std=c99, test-bi-date-canceling.js
  24789. * would fail because of some optimizations when computing tmp_time
  24790. * (MakeTime below). Adding 'volatile' to tmp_time solved this
  24791. * particular problem (annoyingly, also adding debug prints or
  24792. * running the executable under valgrind hides it).
  24793. */
  24794. /* MakeTime */
  24795. tmp_time = 0.0;
  24796. tmp_time += dparts[DUK_DATE_IDX_HOUR] * ((duk_double_t) DUK_DATE_MSEC_HOUR);
  24797. tmp_time += dparts[DUK_DATE_IDX_MINUTE] * ((duk_double_t) DUK_DATE_MSEC_MINUTE);
  24798. tmp_time += dparts[DUK_DATE_IDX_SECOND] * ((duk_double_t) DUK_DATE_MSEC_SECOND);
  24799. tmp_time += dparts[DUK_DATE_IDX_MILLISECOND];
  24800. /* MakeDay */
  24801. tmp_day = duk__make_day(dparts[DUK_DATE_IDX_YEAR], dparts[DUK_DATE_IDX_MONTH], dparts[DUK_DATE_IDX_DAY]);
  24802. /* MakeDate */
  24803. d = tmp_day * ((duk_double_t) DUK_DATE_MSEC_DAY) + tmp_time;
  24804. DUK_DDD(DUK_DDDPRINT("time=%lf day=%lf --> timeval=%lf",
  24805. (double) tmp_time, (double) tmp_day, (double) d));
  24806. /* Optional UTC conversion. */
  24807. if (flags & DUK_DATE_FLAG_LOCALTIME) {
  24808. /* DUK_USE_DATE_GET_LOCAL_TZOFFSET() needs to be called with a
  24809. * time value computed from UTC parts. At this point we only
  24810. * have 'd' which is a time value computed from local parts, so
  24811. * it is off by the UTC-to-local time offset which we don't know
  24812. * yet. The current solution for computing the UTC-to-local
  24813. * time offset is to iterate a few times and detect a fixed
  24814. * point or a two-cycle loop (or a sanity iteration limit),
  24815. * see test-bi-date-local-parts.js and test-bi-date-tzoffset-basic-fi.js.
  24816. *
  24817. * E5.1 Section 15.9.1.9:
  24818. * UTC(t) = t - LocalTZA - DaylightSavingTA(t - LocalTZA)
  24819. *
  24820. * For NaN/inf, DUK_USE_DATE_GET_LOCAL_TZOFFSET() returns 0.
  24821. */
  24822. #if 0
  24823. /* Old solution: don't iterate, incorrect */
  24824. tzoff = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d);
  24825. DUK_DDD(DUK_DDDPRINT("tzoffset w/o iteration, tzoff=%ld", (long) tzoff));
  24826. d -= tzoff * 1000L;
  24827. DUK_UNREF(tzoffprev1);
  24828. DUK_UNREF(tzoffprev2);
  24829. #endif
  24830. /* Iteration solution */
  24831. tzoff = 0;
  24832. tzoffprev1 = 999999999L; /* invalid value which never matches */
  24833. for (i = 0; i < DUK__LOCAL_TZOFFSET_MAXITER; i++) {
  24834. tzoffprev2 = tzoffprev1;
  24835. tzoffprev1 = tzoff;
  24836. tzoff = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d - tzoff * 1000L);
  24837. DUK_DDD(DUK_DDDPRINT("tzoffset iteration, i=%d, tzoff=%ld, tzoffprev1=%ld tzoffprev2=%ld",
  24838. (int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2));
  24839. if (tzoff == tzoffprev1) {
  24840. DUK_DDD(DUK_DDDPRINT("tzoffset iteration finished, i=%d, tzoff=%ld, tzoffprev1=%ld, tzoffprev2=%ld",
  24841. (int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2));
  24842. break;
  24843. } else if (tzoff == tzoffprev2) {
  24844. /* Two value cycle, see e.g. test-bi-date-tzoffset-basic-fi.js.
  24845. * In these cases, favor a higher tzoffset to get a consistent
  24846. * result which is independent of iteration count. Not sure if
  24847. * this is a generically correct solution.
  24848. */
  24849. DUK_DDD(DUK_DDDPRINT("tzoffset iteration two-value cycle, i=%d, tzoff=%ld, tzoffprev1=%ld, tzoffprev2=%ld",
  24850. (int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2));
  24851. if (tzoffprev1 > tzoff) {
  24852. tzoff = tzoffprev1;
  24853. }
  24854. break;
  24855. }
  24856. }
  24857. DUK_DDD(DUK_DDDPRINT("tzoffset iteration, tzoff=%ld", (long) tzoff));
  24858. d -= tzoff * 1000L;
  24859. }
  24860. /* TimeClip(), which also handles Infinity -> NaN conversion */
  24861. d = duk__timeclip(d);
  24862. return d;
  24863. }
  24864. /*
  24865. * API oriented helpers
  24866. */
  24867. /* Push 'this' binding, check that it is a Date object; then push the
  24868. * internal time value. At the end, stack is: [ ... this timeval ].
  24869. * Returns the time value. Local time adjustment is done if requested.
  24870. */
  24871. DUK_LOCAL duk_double_t duk__push_this_get_timeval_tzoffset(duk_context *ctx, duk_small_uint_t flags, duk_int_t *out_tzoffset) {
  24872. duk_hthread *thr = (duk_hthread *) ctx;
  24873. duk_hobject *h;
  24874. duk_double_t d;
  24875. duk_int_t tzoffset = 0;
  24876. duk_push_this(ctx);
  24877. h = duk_get_hobject(ctx, -1); /* XXX: getter with class check, useful in built-ins */
  24878. if (h == NULL || DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_DATE) {
  24879. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "expected Date");
  24880. }
  24881. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE);
  24882. d = duk_to_number(ctx, -1);
  24883. duk_pop(ctx);
  24884. if (DUK_ISNAN(d)) {
  24885. if (flags & DUK_DATE_FLAG_NAN_TO_ZERO) {
  24886. d = 0.0;
  24887. }
  24888. if (flags & DUK_DATE_FLAG_NAN_TO_RANGE_ERROR) {
  24889. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, "Invalid Date");
  24890. }
  24891. }
  24892. /* if no NaN handling flag, may still be NaN here, but not Inf */
  24893. DUK_ASSERT(!DUK_ISINF(d));
  24894. if (flags & DUK_DATE_FLAG_LOCALTIME) {
  24895. /* Note: DST adjustment is determined using UTC time.
  24896. * If 'd' is NaN, tzoffset will be 0.
  24897. */
  24898. tzoffset = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d); /* seconds */
  24899. d += tzoffset * 1000L;
  24900. }
  24901. if (out_tzoffset) {
  24902. *out_tzoffset = tzoffset;
  24903. }
  24904. /* [ ... this ] */
  24905. return d;
  24906. }
  24907. DUK_LOCAL duk_double_t duk__push_this_get_timeval(duk_context *ctx, duk_small_uint_t flags) {
  24908. return duk__push_this_get_timeval_tzoffset(ctx, flags, NULL);
  24909. }
  24910. /* Set timeval to 'this' from dparts, push the new time value onto the
  24911. * value stack and return 1 (caller can then tail call us). Expects
  24912. * the value stack to contain 'this' on the stack top.
  24913. */
  24914. DUK_LOCAL duk_ret_t duk__set_this_timeval_from_dparts(duk_context *ctx, duk_double_t *dparts, duk_small_uint_t flags) {
  24915. duk_double_t d;
  24916. /* [ ... this ] */
  24917. d = duk_bi_date_get_timeval_from_dparts(dparts, flags);
  24918. duk_push_number(ctx, d); /* -> [ ... this timeval_new ] */
  24919. duk_dup_top(ctx); /* -> [ ... this timeval_new timeval_new ] */
  24920. duk_put_prop_stridx(ctx, -3, DUK_STRIDX_INT_VALUE);
  24921. /* stack top: new time value, return 1 to allow tail calls */
  24922. return 1;
  24923. }
  24924. /* 'out_buf' must be at least DUK_BI_DATE_ISO8601_BUFSIZE long. */
  24925. 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) {
  24926. char yearstr[8]; /* "-123456\0" */
  24927. char tzstr[8]; /* "+11:22\0" */
  24928. char sep = (flags & DUK_DATE_FLAG_SEP_T) ? DUK_ASC_UC_T : DUK_ASC_SPACE;
  24929. DUK_ASSERT(parts[DUK_DATE_IDX_MONTH] >= 1 && parts[DUK_DATE_IDX_MONTH] <= 12);
  24930. DUK_ASSERT(parts[DUK_DATE_IDX_DAY] >= 1 && parts[DUK_DATE_IDX_DAY] <= 31);
  24931. DUK_ASSERT(parts[DUK_DATE_IDX_YEAR] >= -999999 && parts[DUK_DATE_IDX_YEAR] <= 999999);
  24932. /* Note: %06d for positive value, %07d for negative value to include
  24933. * sign and 6 digits.
  24934. */
  24935. DUK_SNPRINTF(yearstr,
  24936. sizeof(yearstr),
  24937. (parts[DUK_DATE_IDX_YEAR] >= 0 && parts[DUK_DATE_IDX_YEAR] <= 9999) ? "%04ld" :
  24938. ((parts[DUK_DATE_IDX_YEAR] >= 0) ? "+%06ld" : "%07ld"),
  24939. (long) parts[DUK_DATE_IDX_YEAR]);
  24940. yearstr[sizeof(yearstr) - 1] = (char) 0;
  24941. if (flags & DUK_DATE_FLAG_LOCALTIME) {
  24942. /* tzoffset seconds are dropped; 16 bits suffice for
  24943. * time offset in minutes
  24944. */
  24945. if (tzoffset >= 0) {
  24946. duk_small_int_t tmp = tzoffset / 60;
  24947. DUK_SNPRINTF(tzstr, sizeof(tzstr), "+%02d:%02d", (int) (tmp / 60), (int) (tmp % 60));
  24948. } else {
  24949. duk_small_int_t tmp = -tzoffset / 60;
  24950. DUK_SNPRINTF(tzstr, sizeof(tzstr), "-%02d:%02d", (int) (tmp / 60), (int) (tmp % 60));
  24951. }
  24952. tzstr[sizeof(tzstr) - 1] = (char) 0;
  24953. } else {
  24954. tzstr[0] = DUK_ASC_UC_Z;
  24955. tzstr[1] = (char) 0;
  24956. }
  24957. /* Unlike year, the other parts fit into 16 bits so %d format
  24958. * is portable.
  24959. */
  24960. if ((flags & DUK_DATE_FLAG_TOSTRING_DATE) && (flags & DUK_DATE_FLAG_TOSTRING_TIME)) {
  24961. DUK_SPRINTF((char *) out_buf, "%s-%02d-%02d%c%02d:%02d:%02d.%03d%s",
  24962. (const char *) yearstr, (int) parts[DUK_DATE_IDX_MONTH], (int) parts[DUK_DATE_IDX_DAY], (int) sep,
  24963. (int) parts[DUK_DATE_IDX_HOUR], (int) parts[DUK_DATE_IDX_MINUTE],
  24964. (int) parts[DUK_DATE_IDX_SECOND], (int) parts[DUK_DATE_IDX_MILLISECOND], (const char *) tzstr);
  24965. } else if (flags & DUK_DATE_FLAG_TOSTRING_DATE) {
  24966. DUK_SPRINTF((char *) out_buf, "%s-%02d-%02d",
  24967. (const char *) yearstr, (int) parts[DUK_DATE_IDX_MONTH], (int) parts[DUK_DATE_IDX_DAY]);
  24968. } else {
  24969. DUK_ASSERT(flags & DUK_DATE_FLAG_TOSTRING_TIME);
  24970. DUK_SPRINTF((char *) out_buf, "%02d:%02d:%02d.%03d%s",
  24971. (int) parts[DUK_DATE_IDX_HOUR], (int) parts[DUK_DATE_IDX_MINUTE],
  24972. (int) parts[DUK_DATE_IDX_SECOND], (int) parts[DUK_DATE_IDX_MILLISECOND],
  24973. (const char *) tzstr);
  24974. }
  24975. }
  24976. /* Helper for string conversion calls: check 'this' binding, get the
  24977. * internal time value, and format date and/or time in a few formats.
  24978. * Return value allows tail calls.
  24979. */
  24980. DUK_LOCAL duk_ret_t duk__to_string_helper(duk_context *ctx, duk_small_uint_t flags) {
  24981. duk_double_t d;
  24982. duk_int_t parts[DUK_DATE_IDX_NUM_PARTS];
  24983. duk_int_t tzoffset; /* seconds, doesn't fit into 16 bits */
  24984. duk_bool_t rc;
  24985. duk_uint8_t buf[DUK_BI_DATE_ISO8601_BUFSIZE];
  24986. DUK_UNREF(rc); /* unreferenced with some options */
  24987. d = duk__push_this_get_timeval_tzoffset(ctx, flags, &tzoffset);
  24988. if (DUK_ISNAN(d)) {
  24989. duk_push_hstring_stridx(ctx, DUK_STRIDX_INVALID_DATE);
  24990. return 1;
  24991. }
  24992. DUK_ASSERT(DUK_ISFINITE(d));
  24993. /* formatters always get one-based month/day-of-month */
  24994. duk_bi_date_timeval_to_parts(d, parts, NULL, DUK_DATE_FLAG_ONEBASED);
  24995. DUK_ASSERT(parts[DUK_DATE_IDX_MONTH] >= 1 && parts[DUK_DATE_IDX_MONTH] <= 12);
  24996. DUK_ASSERT(parts[DUK_DATE_IDX_DAY] >= 1 && parts[DUK_DATE_IDX_DAY] <= 31);
  24997. if (flags & DUK_DATE_FLAG_TOSTRING_LOCALE) {
  24998. /* try locale specific formatter; if it refuses to format the
  24999. * string, fall back to an ISO 8601 formatted value in local
  25000. * time.
  25001. */
  25002. #if defined(DUK_USE_DATE_FORMAT_STRING)
  25003. /* Contract, either:
  25004. * - Push string to value stack and return 1
  25005. * - Don't push anything and return 0
  25006. */
  25007. rc = DUK_USE_DATE_FORMAT_STRING(ctx, parts, tzoffset, flags);
  25008. if (rc != 0) {
  25009. return 1;
  25010. }
  25011. #else
  25012. /* No locale specific formatter; this is OK, we fall back
  25013. * to ISO 8601.
  25014. */
  25015. #endif
  25016. }
  25017. /* Different calling convention than above used because the helper
  25018. * is shared.
  25019. */
  25020. duk__format_parts_iso8601(parts, tzoffset, flags, buf);
  25021. duk_push_string(ctx, (const char *) buf);
  25022. return 1;
  25023. }
  25024. /* Helper for component getter calls: check 'this' binding, get the
  25025. * internal time value, split it into parts (either as UTC time or
  25026. * local time), push a specified component as a return value to the
  25027. * value stack and return 1 (caller can then tail call us).
  25028. */
  25029. DUK_LOCAL duk_ret_t duk__get_part_helper(duk_context *ctx, duk_small_uint_t flags_and_idx) {
  25030. duk_double_t d;
  25031. duk_int_t parts[DUK_DATE_IDX_NUM_PARTS];
  25032. duk_small_uint_t idx_part = (duk_small_uint_t) (flags_and_idx >> DUK_DATE_FLAG_VALUE_SHIFT); /* unpack args */
  25033. DUK_ASSERT_DISABLE(idx_part >= 0); /* unsigned */
  25034. DUK_ASSERT(idx_part < DUK_DATE_IDX_NUM_PARTS);
  25035. d = duk__push_this_get_timeval(ctx, flags_and_idx);
  25036. if (DUK_ISNAN(d)) {
  25037. duk_push_nan(ctx);
  25038. return 1;
  25039. }
  25040. DUK_ASSERT(DUK_ISFINITE(d));
  25041. duk_bi_date_timeval_to_parts(d, parts, NULL, flags_and_idx); /* no need to mask idx portion */
  25042. /* Setter APIs detect special year numbers (0...99) and apply a +1900
  25043. * only in certain cases. The legacy getYear() getter applies -1900
  25044. * unconditionally.
  25045. */
  25046. duk_push_int(ctx, (flags_and_idx & DUK_DATE_FLAG_SUB1900) ? parts[idx_part] - 1900 : parts[idx_part]);
  25047. return 1;
  25048. }
  25049. /* Helper for component setter calls: check 'this' binding, get the
  25050. * internal time value, split it into parts (either as UTC time or
  25051. * local time), modify one or more components as specified, recompute
  25052. * the time value, set it as the internal value. Finally, push the
  25053. * new time value as a return value to the value stack and return 1
  25054. * (caller can then tail call us).
  25055. */
  25056. DUK_LOCAL duk_ret_t duk__set_part_helper(duk_context *ctx, duk_small_uint_t flags_and_maxnargs) {
  25057. duk_double_t d;
  25058. duk_int_t parts[DUK_DATE_IDX_NUM_PARTS];
  25059. duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS];
  25060. duk_idx_t nargs;
  25061. duk_small_uint_t maxnargs = (duk_small_uint_t) (flags_and_maxnargs >> DUK_DATE_FLAG_VALUE_SHIFT); /* unpack args */
  25062. duk_small_uint_t idx_first, idx;
  25063. duk_small_uint_t i;
  25064. nargs = duk_get_top(ctx);
  25065. d = duk__push_this_get_timeval(ctx, flags_and_maxnargs);
  25066. DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d));
  25067. if (DUK_ISFINITE(d)) {
  25068. duk_bi_date_timeval_to_parts(d, parts, dparts, flags_and_maxnargs);
  25069. } else {
  25070. /* NaN timevalue: we need to coerce the arguments, but
  25071. * the resulting internal timestamp needs to remain NaN.
  25072. * This works but is not pretty: parts and dparts will
  25073. * be partially uninitialized, but we only write to them.
  25074. */
  25075. }
  25076. /*
  25077. * Determining which datetime components to overwrite based on
  25078. * stack arguments is a bit complicated, but important to factor
  25079. * out from setters themselves for compactness.
  25080. *
  25081. * If DUK_DATE_FLAG_TIMESETTER, maxnargs indicates setter type:
  25082. *
  25083. * 1 -> millisecond
  25084. * 2 -> second, [millisecond]
  25085. * 3 -> minute, [second], [millisecond]
  25086. * 4 -> hour, [minute], [second], [millisecond]
  25087. *
  25088. * Else:
  25089. *
  25090. * 1 -> date
  25091. * 2 -> month, [date]
  25092. * 3 -> year, [month], [date]
  25093. *
  25094. * By comparing nargs and maxnargs (and flags) we know which
  25095. * components to override. We rely on part index ordering.
  25096. */
  25097. if (flags_and_maxnargs & DUK_DATE_FLAG_TIMESETTER) {
  25098. DUK_ASSERT(maxnargs >= 1 && maxnargs <= 4);
  25099. idx_first = DUK_DATE_IDX_MILLISECOND - (maxnargs - 1);
  25100. } else {
  25101. DUK_ASSERT(maxnargs >= 1 && maxnargs <= 3);
  25102. idx_first = DUK_DATE_IDX_DAY - (maxnargs - 1);
  25103. }
  25104. DUK_ASSERT_DISABLE(idx_first >= 0); /* unsigned */
  25105. DUK_ASSERT(idx_first < DUK_DATE_IDX_NUM_PARTS);
  25106. for (i = 0; i < maxnargs; i++) {
  25107. if ((duk_idx_t) i >= nargs) {
  25108. /* no argument given -> leave components untouched */
  25109. break;
  25110. }
  25111. idx = idx_first + i;
  25112. DUK_ASSERT_DISABLE(idx >= 0); /* unsigned */
  25113. DUK_ASSERT(idx < DUK_DATE_IDX_NUM_PARTS);
  25114. if (idx == DUK_DATE_IDX_YEAR && (flags_and_maxnargs & DUK_DATE_FLAG_YEAR_FIXUP)) {
  25115. duk__twodigit_year_fixup(ctx, (duk_idx_t) i);
  25116. }
  25117. dparts[idx] = duk_to_number(ctx, i);
  25118. if (idx == DUK_DATE_IDX_DAY) {
  25119. /* Day-of-month is one-based in the API, but zero-based
  25120. * internally, so fix here. Note that month is zero-based
  25121. * both in the API and internally.
  25122. */
  25123. /* SCANBUILD: complains about use of uninitialized values.
  25124. * The complaint is correct, but operating in undefined
  25125. * values here is intentional in some cases and the caller
  25126. * ignores the results.
  25127. */
  25128. dparts[idx] -= 1.0;
  25129. }
  25130. }
  25131. /* Leaves new timevalue on stack top and returns 1, which is correct
  25132. * for part setters.
  25133. */
  25134. if (DUK_ISFINITE(d)) {
  25135. return duk__set_this_timeval_from_dparts(ctx, dparts, flags_and_maxnargs);
  25136. } else {
  25137. /* Internal timevalue is already NaN, so don't touch it. */
  25138. duk_push_nan(ctx);
  25139. return 1;
  25140. }
  25141. }
  25142. /* Apply ToNumber() to specified index; if ToInteger(val) in [0,99], add
  25143. * 1900 and replace value at idx_val.
  25144. */
  25145. DUK_LOCAL void duk__twodigit_year_fixup(duk_context *ctx, duk_idx_t idx_val) {
  25146. duk_double_t d;
  25147. /* XXX: idx_val would fit into 16 bits, but using duk_small_uint_t
  25148. * might not generate better code due to casting.
  25149. */
  25150. /* E5 Sections 15.9.3.1, B.2.4, B.2.5 */
  25151. duk_to_number(ctx, idx_val);
  25152. if (duk_is_nan(ctx, idx_val)) {
  25153. return;
  25154. }
  25155. duk_dup(ctx, idx_val);
  25156. duk_to_int(ctx, -1);
  25157. d = duk_get_number(ctx, -1); /* get as double to handle huge numbers correctly */
  25158. if (d >= 0.0 && d <= 99.0) {
  25159. d += 1900.0;
  25160. duk_push_number(ctx, d);
  25161. duk_replace(ctx, idx_val);
  25162. }
  25163. duk_pop(ctx);
  25164. }
  25165. /* Set datetime parts from stack arguments, defaulting any missing values.
  25166. * Day-of-week is not set; it is not required when setting the time value.
  25167. */
  25168. DUK_LOCAL void duk__set_parts_from_args(duk_context *ctx, duk_double_t *dparts, duk_idx_t nargs) {
  25169. duk_double_t d;
  25170. duk_small_uint_t i;
  25171. duk_small_uint_t idx;
  25172. /* Causes a ToNumber() coercion, but doesn't break coercion order since
  25173. * year is coerced first anyway.
  25174. */
  25175. duk__twodigit_year_fixup(ctx, 0);
  25176. /* There are at most 7 args, but we use 8 here so that also
  25177. * DUK_DATE_IDX_WEEKDAY gets initialized (to zero) to avoid the potential
  25178. * for any Valgrind gripes later.
  25179. */
  25180. for (i = 0; i < 8; i++) {
  25181. /* Note: rely on index ordering */
  25182. idx = DUK_DATE_IDX_YEAR + i;
  25183. if ((duk_idx_t) i < nargs) {
  25184. d = duk_to_number(ctx, (duk_idx_t) i);
  25185. if (idx == DUK_DATE_IDX_DAY) {
  25186. /* Convert day from one-based to zero-based (internal). This may
  25187. * cause the day part to be negative, which is OK.
  25188. */
  25189. d -= 1.0;
  25190. }
  25191. } else {
  25192. /* All components default to 0 except day-of-month which defaults
  25193. * to 1. However, because our internal day-of-month is zero-based,
  25194. * it also defaults to zero here.
  25195. */
  25196. d = 0.0;
  25197. }
  25198. dparts[idx] = d;
  25199. }
  25200. DUK_DDD(DUK_DDDPRINT("parts from args -> %lf %lf %lf %lf %lf %lf %lf %lf",
  25201. (double) dparts[0], (double) dparts[1],
  25202. (double) dparts[2], (double) dparts[3],
  25203. (double) dparts[4], (double) dparts[5],
  25204. (double) dparts[6], (double) dparts[7]));
  25205. }
  25206. /*
  25207. * Helper to format a time value into caller buffer, used by logging.
  25208. * 'out_buf' must be at least DUK_BI_DATE_ISO8601_BUFSIZE long.
  25209. */
  25210. DUK_INTERNAL void duk_bi_date_format_timeval(duk_double_t timeval, duk_uint8_t *out_buf) {
  25211. duk_int_t parts[DUK_DATE_IDX_NUM_PARTS];
  25212. duk_bi_date_timeval_to_parts(timeval,
  25213. parts,
  25214. NULL,
  25215. DUK_DATE_FLAG_ONEBASED);
  25216. duk__format_parts_iso8601(parts,
  25217. 0 /*tzoffset*/,
  25218. DUK_DATE_FLAG_TOSTRING_DATE |
  25219. DUK_DATE_FLAG_TOSTRING_TIME |
  25220. DUK_DATE_FLAG_SEP_T /*flags*/,
  25221. out_buf);
  25222. }
  25223. /*
  25224. * Indirect magic value lookup for Date methods.
  25225. *
  25226. * Date methods don't put their control flags into the function magic value
  25227. * because they wouldn't fit into a LIGHTFUNC's magic field. Instead, the
  25228. * magic value is set to an index pointing to the array of control flags
  25229. * below.
  25230. *
  25231. * This must be kept in strict sync with genbuiltins.py!
  25232. */
  25233. static duk_uint16_t duk__date_magics[] = {
  25234. /* 0: toString */
  25235. DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_LOCALTIME,
  25236. /* 1: toDateString */
  25237. DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_LOCALTIME,
  25238. /* 2: toTimeString */
  25239. DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_LOCALTIME,
  25240. /* 3: toLocaleString */
  25241. DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_TOSTRING_LOCALE + DUK_DATE_FLAG_LOCALTIME,
  25242. /* 4: toLocaleDateString */
  25243. DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_LOCALE + DUK_DATE_FLAG_LOCALTIME,
  25244. /* 5: toLocaleTimeString */
  25245. DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_TOSTRING_LOCALE + DUK_DATE_FLAG_LOCALTIME,
  25246. /* 6: toUTCString */
  25247. DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_TIME,
  25248. /* 7: toISOString */
  25249. DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_NAN_TO_RANGE_ERROR + DUK_DATE_FLAG_SEP_T,
  25250. /* 8: getFullYear */
  25251. DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_YEAR << DUK_DATE_FLAG_VALUE_SHIFT),
  25252. /* 9: getUTCFullYear */
  25253. 0 + (DUK_DATE_IDX_YEAR << DUK_DATE_FLAG_VALUE_SHIFT),
  25254. /* 10: getMonth */
  25255. DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_MONTH << DUK_DATE_FLAG_VALUE_SHIFT),
  25256. /* 11: getUTCMonth */
  25257. 0 + (DUK_DATE_IDX_MONTH << DUK_DATE_FLAG_VALUE_SHIFT),
  25258. /* 12: getDate */
  25259. DUK_DATE_FLAG_ONEBASED + DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_DAY << DUK_DATE_FLAG_VALUE_SHIFT),
  25260. /* 13: getUTCDate */
  25261. DUK_DATE_FLAG_ONEBASED + (DUK_DATE_IDX_DAY << DUK_DATE_FLAG_VALUE_SHIFT),
  25262. /* 14: getDay */
  25263. DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_WEEKDAY << DUK_DATE_FLAG_VALUE_SHIFT),
  25264. /* 15: getUTCDay */
  25265. 0 + (DUK_DATE_IDX_WEEKDAY << DUK_DATE_FLAG_VALUE_SHIFT),
  25266. /* 16: getHours */
  25267. DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_HOUR << DUK_DATE_FLAG_VALUE_SHIFT),
  25268. /* 17: getUTCHours */
  25269. 0 + (DUK_DATE_IDX_HOUR << DUK_DATE_FLAG_VALUE_SHIFT),
  25270. /* 18: getMinutes */
  25271. DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_MINUTE << DUK_DATE_FLAG_VALUE_SHIFT),
  25272. /* 19: getUTCMinutes */
  25273. 0 + (DUK_DATE_IDX_MINUTE << DUK_DATE_FLAG_VALUE_SHIFT),
  25274. /* 20: getSeconds */
  25275. DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_SECOND << DUK_DATE_FLAG_VALUE_SHIFT),
  25276. /* 21: getUTCSeconds */
  25277. 0 + (DUK_DATE_IDX_SECOND << DUK_DATE_FLAG_VALUE_SHIFT),
  25278. /* 22: getMilliseconds */
  25279. DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_MILLISECOND << DUK_DATE_FLAG_VALUE_SHIFT),
  25280. /* 23: getUTCMilliseconds */
  25281. 0 + (DUK_DATE_IDX_MILLISECOND << DUK_DATE_FLAG_VALUE_SHIFT),
  25282. /* 24: setMilliseconds */
  25283. DUK_DATE_FLAG_TIMESETTER + DUK_DATE_FLAG_LOCALTIME + (1 << DUK_DATE_FLAG_VALUE_SHIFT),
  25284. /* 25: setUTCMilliseconds */
  25285. DUK_DATE_FLAG_TIMESETTER + (1 << DUK_DATE_FLAG_VALUE_SHIFT),
  25286. /* 26: setSeconds */
  25287. DUK_DATE_FLAG_TIMESETTER + DUK_DATE_FLAG_LOCALTIME + (2 << DUK_DATE_FLAG_VALUE_SHIFT),
  25288. /* 27: setUTCSeconds */
  25289. DUK_DATE_FLAG_TIMESETTER + (2 << DUK_DATE_FLAG_VALUE_SHIFT),
  25290. /* 28: setMinutes */
  25291. DUK_DATE_FLAG_TIMESETTER + DUK_DATE_FLAG_LOCALTIME + (3 << DUK_DATE_FLAG_VALUE_SHIFT),
  25292. /* 29: setUTCMinutes */
  25293. DUK_DATE_FLAG_TIMESETTER + (3 << DUK_DATE_FLAG_VALUE_SHIFT),
  25294. /* 30: setHours */
  25295. DUK_DATE_FLAG_TIMESETTER + DUK_DATE_FLAG_LOCALTIME + (4 << DUK_DATE_FLAG_VALUE_SHIFT),
  25296. /* 31: setUTCHours */
  25297. DUK_DATE_FLAG_TIMESETTER + (4 << DUK_DATE_FLAG_VALUE_SHIFT),
  25298. /* 32: setDate */
  25299. DUK_DATE_FLAG_LOCALTIME + (1 << DUK_DATE_FLAG_VALUE_SHIFT),
  25300. /* 33: setUTCDate */
  25301. 0 + (1 << DUK_DATE_FLAG_VALUE_SHIFT),
  25302. /* 34: setMonth */
  25303. DUK_DATE_FLAG_LOCALTIME + (2 << DUK_DATE_FLAG_VALUE_SHIFT),
  25304. /* 35: setUTCMonth */
  25305. 0 + (2 << DUK_DATE_FLAG_VALUE_SHIFT),
  25306. /* 36: setFullYear */
  25307. DUK_DATE_FLAG_NAN_TO_ZERO + DUK_DATE_FLAG_LOCALTIME + (3 << DUK_DATE_FLAG_VALUE_SHIFT),
  25308. /* 37: setUTCFullYear */
  25309. DUK_DATE_FLAG_NAN_TO_ZERO + (3 << DUK_DATE_FLAG_VALUE_SHIFT),
  25310. /* 38: getYear */
  25311. DUK_DATE_FLAG_LOCALTIME + DUK_DATE_FLAG_SUB1900 + (DUK_DATE_IDX_YEAR << DUK_DATE_FLAG_VALUE_SHIFT),
  25312. /* 39: setYear */
  25313. DUK_DATE_FLAG_NAN_TO_ZERO + DUK_DATE_FLAG_YEAR_FIXUP + (3 << DUK_DATE_FLAG_VALUE_SHIFT),
  25314. };
  25315. DUK_LOCAL duk_small_uint_t duk__date_get_indirect_magic(duk_context *ctx) {
  25316. duk_small_int_t magicidx = (duk_small_uint_t) duk_get_current_magic(ctx);
  25317. DUK_ASSERT(magicidx >= 0 && magicidx < (duk_small_int_t) (sizeof(duk__date_magics) / sizeof(duk_uint16_t)));
  25318. return (duk_small_uint_t) duk__date_magics[magicidx];
  25319. }
  25320. /*
  25321. * Constructor calls
  25322. */
  25323. DUK_INTERNAL duk_ret_t duk_bi_date_constructor(duk_context *ctx) {
  25324. duk_idx_t nargs = duk_get_top(ctx);
  25325. duk_bool_t is_cons = duk_is_constructor_call(ctx);
  25326. duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS];
  25327. duk_double_t d;
  25328. DUK_DDD(DUK_DDDPRINT("Date constructor, nargs=%ld, is_cons=%ld", (long) nargs, (long) is_cons));
  25329. duk_push_object_helper(ctx,
  25330. DUK_HOBJECT_FLAG_EXTENSIBLE |
  25331. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DATE),
  25332. DUK_BIDX_DATE_PROTOTYPE);
  25333. /* Unlike most built-ins, the internal [[PrimitiveValue]] of a Date
  25334. * is mutable.
  25335. */
  25336. if (nargs == 0 || !is_cons) {
  25337. d = duk__timeclip(DUK_USE_DATE_GET_NOW(ctx));
  25338. duk_push_number(ctx, d);
  25339. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_W);
  25340. if (!is_cons) {
  25341. /* called as a normal function: return new Date().toString() */
  25342. duk_to_string(ctx, -1);
  25343. }
  25344. return 1;
  25345. } else if (nargs == 1) {
  25346. duk_to_primitive(ctx, 0, DUK_HINT_NONE);
  25347. if (duk_is_string(ctx, 0)) {
  25348. duk__parse_string(ctx, duk_to_string(ctx, 0));
  25349. duk_replace(ctx, 0); /* may be NaN */
  25350. }
  25351. d = duk__timeclip(duk_to_number(ctx, 0));
  25352. duk_push_number(ctx, d);
  25353. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_W);
  25354. return 1;
  25355. }
  25356. duk__set_parts_from_args(ctx, dparts, nargs);
  25357. /* Parts are in local time, convert when setting. */
  25358. (void) duk__set_this_timeval_from_dparts(ctx, dparts, DUK_DATE_FLAG_LOCALTIME /*flags*/); /* -> [ ... this timeval ] */
  25359. duk_pop(ctx); /* -> [ ... this ] */
  25360. return 1;
  25361. }
  25362. DUK_INTERNAL duk_ret_t duk_bi_date_constructor_parse(duk_context *ctx) {
  25363. return duk__parse_string(ctx, duk_to_string(ctx, 0));
  25364. }
  25365. DUK_INTERNAL duk_ret_t duk_bi_date_constructor_utc(duk_context *ctx) {
  25366. duk_idx_t nargs = duk_get_top(ctx);
  25367. duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS];
  25368. duk_double_t d;
  25369. /* Behavior for nargs < 2 is implementation dependent: currently we'll
  25370. * set a NaN time value (matching V8 behavior) in this case.
  25371. */
  25372. if (nargs < 2) {
  25373. duk_push_nan(ctx);
  25374. } else {
  25375. duk__set_parts_from_args(ctx, dparts, nargs);
  25376. d = duk_bi_date_get_timeval_from_dparts(dparts, 0 /*flags*/);
  25377. duk_push_number(ctx, d);
  25378. }
  25379. return 1;
  25380. }
  25381. DUK_INTERNAL duk_ret_t duk_bi_date_constructor_now(duk_context *ctx) {
  25382. duk_double_t d;
  25383. d = DUK_USE_DATE_GET_NOW(ctx);
  25384. DUK_ASSERT(duk__timeclip(d) == d); /* TimeClip() should never be necessary */
  25385. duk_push_number(ctx, d);
  25386. return 1;
  25387. }
  25388. /*
  25389. * String/JSON conversions
  25390. *
  25391. * Human readable conversions are now basically ISO 8601 with a space
  25392. * (instead of 'T') as the date/time separator. This is a good baseline
  25393. * and is platform independent.
  25394. *
  25395. * A shared native helper to provide many conversions. Magic value contains
  25396. * a set of flags. The helper provides:
  25397. *
  25398. * toString()
  25399. * toDateString()
  25400. * toTimeString()
  25401. * toLocaleString()
  25402. * toLocaleDateString()
  25403. * toLocaleTimeString()
  25404. * toUTCString()
  25405. * toISOString()
  25406. *
  25407. * Notes:
  25408. *
  25409. * - Date.prototype.toGMTString() and Date.prototype.toUTCString() are
  25410. * required to be the same Ecmascript function object (!), so it is
  25411. * omitted from here.
  25412. *
  25413. * - Date.prototype.toUTCString(): E5.1 specification does not require a
  25414. * specific format, but result should be human readable. The
  25415. * specification suggests using ISO 8601 format with a space (instead
  25416. * of 'T') separator if a more human readable format is not available.
  25417. *
  25418. * - Date.prototype.toISOString(): unlike other conversion functions,
  25419. * toISOString() requires a RangeError for invalid date values.
  25420. */
  25421. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_tostring_shared(duk_context *ctx) {
  25422. duk_small_uint_t flags = duk__date_get_indirect_magic(ctx);
  25423. return duk__to_string_helper(ctx, flags);
  25424. }
  25425. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_value_of(duk_context *ctx) {
  25426. /* This native function is also used for Date.prototype.getTime()
  25427. * as their behavior is identical.
  25428. */
  25429. duk_double_t d = duk__push_this_get_timeval(ctx, 0 /*flags*/); /* -> [ this ] */
  25430. DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d));
  25431. duk_push_number(ctx, d);
  25432. return 1;
  25433. }
  25434. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_to_json(duk_context *ctx) {
  25435. /* Note: toJSON() is a generic function which works even if 'this'
  25436. * is not a Date. The sole argument is ignored.
  25437. */
  25438. duk_push_this(ctx);
  25439. duk_to_object(ctx, -1);
  25440. duk_dup_top(ctx);
  25441. duk_to_primitive(ctx, -1, DUK_HINT_NUMBER);
  25442. if (duk_is_number(ctx, -1)) {
  25443. duk_double_t d = duk_get_number(ctx, -1);
  25444. if (!DUK_ISFINITE(d)) {
  25445. duk_push_null(ctx);
  25446. return 1;
  25447. }
  25448. }
  25449. duk_pop(ctx);
  25450. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_TO_ISO_STRING);
  25451. duk_dup(ctx, -2); /* -> [ O toIsoString O ] */
  25452. duk_call_method(ctx, 0);
  25453. return 1;
  25454. }
  25455. /*
  25456. * Getters.
  25457. *
  25458. * Implementing getters is quite easy. The internal time value is either
  25459. * NaN, or represents milliseconds (without fractions) from Jan 1, 1970.
  25460. * The internal time value can be converted to integer parts, and each
  25461. * part will be normalized and will fit into a 32-bit signed integer.
  25462. *
  25463. * A shared native helper to provide all getters. Magic value contains
  25464. * a set of flags and also packs the date component index argument. The
  25465. * helper provides:
  25466. *
  25467. * getFullYear()
  25468. * getUTCFullYear()
  25469. * getMonth()
  25470. * getUTCMonth()
  25471. * getDate()
  25472. * getUTCDate()
  25473. * getDay()
  25474. * getUTCDay()
  25475. * getHours()
  25476. * getUTCHours()
  25477. * getMinutes()
  25478. * getUTCMinutes()
  25479. * getSeconds()
  25480. * getUTCSeconds()
  25481. * getMilliseconds()
  25482. * getUTCMilliseconds()
  25483. * getYear()
  25484. *
  25485. * Notes:
  25486. *
  25487. * - Date.prototype.getDate(): 'date' means day-of-month, and is
  25488. * zero-based in internal calculations but public API expects it to
  25489. * be one-based.
  25490. *
  25491. * - Date.prototype.getTime() and Date.prototype.valueOf() have identical
  25492. * behavior. They have separate function objects, but share the same C
  25493. * function (duk_bi_date_prototype_value_of).
  25494. */
  25495. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_get_shared(duk_context *ctx) {
  25496. duk_small_uint_t flags_and_idx = duk__date_get_indirect_magic(ctx);
  25497. return duk__get_part_helper(ctx, flags_and_idx);
  25498. }
  25499. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_get_timezone_offset(duk_context *ctx) {
  25500. /*
  25501. * Return (t - LocalTime(t)) in minutes:
  25502. *
  25503. * t - LocalTime(t) = t - (t + LocalTZA + DaylightSavingTA(t))
  25504. * = -(LocalTZA + DaylightSavingTA(t))
  25505. *
  25506. * where DaylightSavingTA() is checked for time 't'.
  25507. *
  25508. * Note that the sign of the result is opposite to common usage,
  25509. * e.g. for EE(S)T which normally is +2h or +3h from UTC, this
  25510. * function returns -120 or -180.
  25511. *
  25512. */
  25513. duk_double_t d;
  25514. duk_int_t tzoffset;
  25515. /* Note: DST adjustment is determined using UTC time. */
  25516. d = duk__push_this_get_timeval(ctx, 0 /*flags*/);
  25517. DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d));
  25518. if (DUK_ISNAN(d)) {
  25519. duk_push_nan(ctx);
  25520. } else {
  25521. DUK_ASSERT(DUK_ISFINITE(d));
  25522. tzoffset = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d);
  25523. duk_push_int(ctx, -tzoffset / 60);
  25524. }
  25525. return 1;
  25526. }
  25527. /*
  25528. * Setters.
  25529. *
  25530. * Setters are a bit more complicated than getters. Component setters
  25531. * break down the current time value into its (normalized) component
  25532. * parts, replace one or more components with -unnormalized- new values,
  25533. * and the components are then converted back into a time value. As an
  25534. * example of using unnormalized values:
  25535. *
  25536. * var d = new Date(1234567890);
  25537. *
  25538. * is equivalent to:
  25539. *
  25540. * var d = new Date(0);
  25541. * d.setUTCMilliseconds(1234567890);
  25542. *
  25543. * A shared native helper to provide almost all setters. Magic value
  25544. * contains a set of flags and also packs the "maxnargs" argument. The
  25545. * helper provides:
  25546. *
  25547. * setMilliseconds()
  25548. * setUTCMilliseconds()
  25549. * setSeconds()
  25550. * setUTCSeconds()
  25551. * setMinutes()
  25552. * setUTCMinutes()
  25553. * setHours()
  25554. * setUTCHours()
  25555. * setDate()
  25556. * setUTCDate()
  25557. * setMonth()
  25558. * setUTCMonth()
  25559. * setFullYear()
  25560. * setUTCFullYear()
  25561. * setYear()
  25562. *
  25563. * Notes:
  25564. *
  25565. * - Date.prototype.setYear() (Section B addition): special year check
  25566. * is omitted. NaN / Infinity will just flow through and ultimately
  25567. * result in a NaN internal time value.
  25568. *
  25569. * - Date.prototype.setYear() does not have optional arguments for
  25570. * setting month and day-in-month (like setFullYear()), but we indicate
  25571. * 'maxnargs' to be 3 to get the year written to the correct component
  25572. * index in duk__set_part_helper(). The function has nargs == 1, so only
  25573. * the year will be set regardless of actual argument count.
  25574. */
  25575. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_set_shared(duk_context *ctx) {
  25576. duk_small_uint_t flags_and_maxnargs = duk__date_get_indirect_magic(ctx);
  25577. return duk__set_part_helper(ctx, flags_and_maxnargs);
  25578. }
  25579. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_set_time(duk_context *ctx) {
  25580. duk_double_t d;
  25581. (void) duk__push_this_get_timeval(ctx, 0 /*flags*/); /* -> [ timeval this ] */
  25582. d = duk__timeclip(duk_to_number(ctx, 0));
  25583. duk_push_number(ctx, d);
  25584. duk_dup_top(ctx);
  25585. duk_put_prop_stridx(ctx, -3, DUK_STRIDX_INT_VALUE); /* -> [ timeval this timeval ] */
  25586. return 1;
  25587. }
  25588. #line 1 "duk_bi_date_unix.c"
  25589. /*
  25590. * Unix-like Date providers
  25591. *
  25592. * Generally useful Unix / POSIX / ANSI Date providers.
  25593. */
  25594. /* include removed: duk_internal.h */
  25595. /* The necessary #includes are in place in duk_config.h. */
  25596. /* Buffer sizes for some UNIX calls. Larger than strictly necessary
  25597. * to avoid Valgrind errors.
  25598. */
  25599. #define DUK__STRPTIME_BUF_SIZE 64
  25600. #define DUK__STRFTIME_BUF_SIZE 64
  25601. #if defined(DUK_USE_DATE_NOW_GETTIMEOFDAY)
  25602. /* Get current Ecmascript time (= UNIX/Posix time, but in milliseconds). */
  25603. DUK_INTERNAL duk_double_t duk_bi_date_get_now_gettimeofday(duk_context *ctx) {
  25604. duk_hthread *thr = (duk_hthread *) ctx;
  25605. struct timeval tv;
  25606. duk_double_t d;
  25607. if (gettimeofday(&tv, NULL) != 0) {
  25608. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, "gettimeofday failed");
  25609. }
  25610. d = ((duk_double_t) tv.tv_sec) * 1000.0 +
  25611. ((duk_double_t) (tv.tv_usec / 1000));
  25612. DUK_ASSERT(DUK_FLOOR(d) == d); /* no fractions */
  25613. return d;
  25614. }
  25615. #endif /* DUK_USE_DATE_NOW_GETTIMEOFDAY */
  25616. #if defined(DUK_USE_DATE_NOW_TIME)
  25617. /* Not a very good provider: only full seconds are available. */
  25618. DUK_INTERNAL duk_double_t duk_bi_date_get_now_time(duk_context *ctx) {
  25619. time_t t = time(NULL);
  25620. return ((duk_double_t) t) * 1000.0;
  25621. }
  25622. #endif /* DUK_USE_DATE_NOW_TIME */
  25623. #if defined(DUK_USE_DATE_TZO_GMTIME) || defined(DUK_USE_DATE_TZO_GMTIME_R)
  25624. /* Get local time offset (in seconds) for a certain (UTC) instant 'd'. */
  25625. DUK_INTERNAL duk_int_t duk_bi_date_get_local_tzoffset_gmtime(duk_double_t d) {
  25626. time_t t, t1, t2;
  25627. duk_int_t parts[DUK_DATE_IDX_NUM_PARTS];
  25628. duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS];
  25629. struct tm tms[2];
  25630. #ifdef DUK_USE_DATE_TZO_GMTIME
  25631. struct tm *tm_ptr;
  25632. #endif
  25633. /* For NaN/inf, the return value doesn't matter. */
  25634. if (!DUK_ISFINITE(d)) {
  25635. return 0;
  25636. }
  25637. /* If not within Ecmascript range, some integer time calculations
  25638. * won't work correctly (and some asserts will fail), so bail out
  25639. * if so. This fixes test-bug-date-insane-setyear.js. There is
  25640. * a +/- 24h leeway in this range check to avoid a test262 corner
  25641. * case documented in test-bug-date-timeval-edges.js.
  25642. */
  25643. if (!duk_bi_date_timeval_in_leeway_range(d)) {
  25644. DUK_DD(DUK_DDPRINT("timeval not within valid range, skip tzoffset computation to avoid integer overflows"));
  25645. return 0;
  25646. }
  25647. /*
  25648. * This is a bit tricky to implement portably. The result depends
  25649. * on the timestamp (specifically, DST depends on the timestamp).
  25650. * If e.g. UNIX APIs are used, they'll have portability issues with
  25651. * very small and very large years.
  25652. *
  25653. * Current approach:
  25654. *
  25655. * - Stay within portable UNIX limits by using equivalent year mapping.
  25656. * Avoid year 1970 and 2038 as some conversions start to fail, at
  25657. * least on some platforms. Avoiding 1970 means that there are
  25658. * currently DST discrepancies for 1970.
  25659. *
  25660. * - Create a UTC and local time breakdowns from 't'. Then create
  25661. * a time_t using gmtime() and localtime() and compute the time
  25662. * difference between the two.
  25663. *
  25664. * Equivalent year mapping (E5 Section 15.9.1.8):
  25665. *
  25666. * If the host environment provides functionality for determining
  25667. * daylight saving time, the implementation of ECMAScript is free
  25668. * to map the year in question to an equivalent year (same
  25669. * leap-year-ness and same starting week day for the year) for which
  25670. * the host environment provides daylight saving time information.
  25671. * The only restriction is that all equivalent years should produce
  25672. * the same result.
  25673. *
  25674. * This approach is quite reasonable but not entirely correct, e.g.
  25675. * the specification also states (E5 Section 15.9.1.8):
  25676. *
  25677. * The implementation of ECMAScript should not try to determine
  25678. * whether the exact time was subject to daylight saving time, but
  25679. * just whether daylight saving time would have been in effect if
  25680. * the _current daylight saving time algorithm_ had been used at the
  25681. * time. This avoids complications such as taking into account the
  25682. * years that the locale observed daylight saving time year round.
  25683. *
  25684. * Since we rely on the platform APIs for conversions between local
  25685. * time and UTC, we can't guarantee the above. Rather, if the platform
  25686. * has historical DST rules they will be applied. This seems to be the
  25687. * general preferred direction in Ecmascript standardization (or at least
  25688. * implementations) anyway, and even the equivalent year mapping should
  25689. * be disabled if the platform is known to handle DST properly for the
  25690. * full Ecmascript range.
  25691. *
  25692. * The following has useful discussion and links:
  25693. *
  25694. * https://bugzilla.mozilla.org/show_bug.cgi?id=351066
  25695. */
  25696. duk_bi_date_timeval_to_parts(d, parts, dparts, DUK_DATE_FLAG_EQUIVYEAR /*flags*/);
  25697. DUK_ASSERT(parts[DUK_DATE_IDX_YEAR] >= 1970 && parts[DUK_DATE_IDX_YEAR] <= 2038);
  25698. d = duk_bi_date_get_timeval_from_dparts(dparts, 0 /*flags*/);
  25699. DUK_ASSERT(d >= 0 && d < 2147483648.0 * 1000.0); /* unsigned 31-bit range */
  25700. t = (time_t) (d / 1000.0);
  25701. DUK_DDD(DUK_DDDPRINT("timeval: %lf -> time_t %ld", (double) d, (long) t));
  25702. t1 = t;
  25703. DUK_MEMZERO((void *) tms, sizeof(struct tm) * 2);
  25704. #if defined(DUK_USE_DATE_TZO_GMTIME_R)
  25705. (void) gmtime_r(&t, &tms[0]);
  25706. (void) localtime_r(&t, &tms[1]);
  25707. #elif defined(DUK_USE_DATE_TZO_GMTIME)
  25708. tm_ptr = gmtime(&t);
  25709. DUK_MEMCPY((void *) &tms[0], tm_ptr, sizeof(struct tm));
  25710. tm_ptr = localtime(&t);
  25711. DUK_MEMCPY((void *) &tms[1], tm_ptr, sizeof(struct tm));
  25712. #else
  25713. #error internal error
  25714. #endif
  25715. DUK_DDD(DUK_DDDPRINT("gmtime result: tm={sec:%ld,min:%ld,hour:%ld,mday:%ld,mon:%ld,year:%ld,"
  25716. "wday:%ld,yday:%ld,isdst:%ld}",
  25717. (long) tms[0].tm_sec, (long) tms[0].tm_min, (long) tms[0].tm_hour,
  25718. (long) tms[0].tm_mday, (long) tms[0].tm_mon, (long) tms[0].tm_year,
  25719. (long) tms[0].tm_wday, (long) tms[0].tm_yday, (long) tms[0].tm_isdst));
  25720. DUK_DDD(DUK_DDDPRINT("localtime result: tm={sec:%ld,min:%ld,hour:%ld,mday:%ld,mon:%ld,year:%ld,"
  25721. "wday:%ld,yday:%ld,isdst:%ld}",
  25722. (long) tms[1].tm_sec, (long) tms[1].tm_min, (long) tms[1].tm_hour,
  25723. (long) tms[1].tm_mday, (long) tms[1].tm_mon, (long) tms[1].tm_year,
  25724. (long) tms[1].tm_wday, (long) tms[1].tm_yday, (long) tms[1].tm_isdst));
  25725. /* tm_isdst is both an input and an output to mktime(), use 0 to
  25726. * avoid DST handling in mktime():
  25727. * - https://github.com/svaarala/duktape/issues/406
  25728. * - http://stackoverflow.com/questions/8558919/mktime-and-tm-isdst
  25729. */
  25730. tms[0].tm_isdst = 0;
  25731. tms[1].tm_isdst = 0;
  25732. t1 = mktime(&tms[0]); /* UTC */
  25733. t2 = mktime(&tms[1]); /* local */
  25734. if (t1 == (time_t) -1 || t2 == (time_t) -1) {
  25735. /* This check used to be for (t < 0) but on some platforms
  25736. * time_t is unsigned and apparently the proper way to detect
  25737. * an mktime() error return is the cast above. See e.g.:
  25738. * http://pubs.opengroup.org/onlinepubs/009695299/functions/mktime.html
  25739. */
  25740. goto error;
  25741. }
  25742. DUK_DDD(DUK_DDDPRINT("t1=%ld (utc), t2=%ld (local)", (long) t1, (long) t2));
  25743. /* Compute final offset in seconds, positive if local time ahead of
  25744. * UTC (returned value is UTC-to-local offset).
  25745. *
  25746. * difftime() returns a double, so coercion to int generates quite
  25747. * a lot of code. Direct subtraction is not portable, however.
  25748. * XXX: allow direct subtraction on known platforms.
  25749. */
  25750. #if 0
  25751. return (duk_int_t) (t2 - t1);
  25752. #endif
  25753. return (duk_int_t) difftime(t2, t1);
  25754. error:
  25755. /* XXX: return something more useful, so that caller can throw? */
  25756. DUK_D(DUK_DPRINT("mktime() failed, d=%lf", (double) d));
  25757. return 0;
  25758. }
  25759. #endif /* DUK_USE_DATE_TZO_GMTIME */
  25760. #if defined(DUK_USE_DATE_PRS_STRPTIME)
  25761. DUK_INTERNAL duk_bool_t duk_bi_date_parse_string_strptime(duk_context *ctx, const char *str) {
  25762. struct tm tm;
  25763. time_t t;
  25764. char buf[DUK__STRPTIME_BUF_SIZE];
  25765. /* copy to buffer with spare to avoid Valgrind gripes from strptime */
  25766. DUK_ASSERT(str != NULL);
  25767. DUK_MEMZERO(buf, sizeof(buf)); /* valgrind whine without this */
  25768. DUK_SNPRINTF(buf, sizeof(buf), "%s", (const char *) str);
  25769. buf[sizeof(buf) - 1] = (char) 0;
  25770. DUK_DDD(DUK_DDDPRINT("parsing: '%s'", (const char *) buf));
  25771. DUK_MEMZERO(&tm, sizeof(tm));
  25772. if (strptime((const char *) buf, "%c", &tm) != NULL) {
  25773. DUK_DDD(DUK_DDDPRINT("before mktime: tm={sec:%ld,min:%ld,hour:%ld,mday:%ld,mon:%ld,year:%ld,"
  25774. "wday:%ld,yday:%ld,isdst:%ld}",
  25775. (long) tm.tm_sec, (long) tm.tm_min, (long) tm.tm_hour,
  25776. (long) tm.tm_mday, (long) tm.tm_mon, (long) tm.tm_year,
  25777. (long) tm.tm_wday, (long) tm.tm_yday, (long) tm.tm_isdst));
  25778. tm.tm_isdst = -1; /* negative: dst info not available */
  25779. t = mktime(&tm);
  25780. DUK_DDD(DUK_DDDPRINT("mktime() -> %ld", (long) t));
  25781. if (t >= 0) {
  25782. duk_push_number(ctx, ((duk_double_t) t) * 1000.0);
  25783. return 1;
  25784. }
  25785. }
  25786. return 0;
  25787. }
  25788. #endif /* DUK_USE_DATE_PRS_STRPTIME */
  25789. #if defined(DUK_USE_DATE_PRS_GETDATE)
  25790. DUK_INTERNAL duk_bool_t duk_bi_date_parse_string_getdate(duk_context *ctx, const char *str) {
  25791. struct tm tm;
  25792. duk_small_int_t rc;
  25793. time_t t;
  25794. /* For this to work, DATEMSK must be set, so this is not very
  25795. * convenient for an embeddable interpreter.
  25796. */
  25797. DUK_MEMZERO(&tm, sizeof(struct tm));
  25798. rc = (duk_small_int_t) getdate_r(str, &tm);
  25799. DUK_DDD(DUK_DDDPRINT("getdate_r() -> %ld", (long) rc));
  25800. if (rc == 0) {
  25801. t = mktime(&tm);
  25802. DUK_DDD(DUK_DDDPRINT("mktime() -> %ld", (long) t));
  25803. if (t >= 0) {
  25804. duk_push_number(ctx, (duk_double_t) t);
  25805. return 1;
  25806. }
  25807. }
  25808. return 0;
  25809. }
  25810. #endif /* DUK_USE_DATE_PRS_GETDATE */
  25811. #if defined(DUK_USE_DATE_FMT_STRFTIME)
  25812. DUK_INTERNAL duk_bool_t duk_bi_date_format_parts_strftime(duk_context *ctx, duk_int_t *parts, duk_int_t tzoffset, duk_small_uint_t flags) {
  25813. char buf[DUK__STRFTIME_BUF_SIZE];
  25814. struct tm tm;
  25815. const char *fmt;
  25816. DUK_UNREF(tzoffset);
  25817. /* If the platform doesn't support the entire Ecmascript range, we need
  25818. * to return 0 so that the caller can fall back to the default formatter.
  25819. *
  25820. * For now, assume that if time_t is 8 bytes or more, the whole Ecmascript
  25821. * range is supported. For smaller time_t values (4 bytes in practice),
  25822. * assumes that the signed 32-bit range is supported.
  25823. *
  25824. * XXX: detect this more correctly per platform. The size of time_t is
  25825. * probably not an accurate guarantee of strftime() supporting or not
  25826. * supporting a large time range (the full Ecmascript range).
  25827. */
  25828. if (sizeof(time_t) < 8 &&
  25829. (parts[DUK_DATE_IDX_YEAR] < 1970 || parts[DUK_DATE_IDX_YEAR] > 2037)) {
  25830. /* be paranoid for 32-bit time values (even avoiding negative ones) */
  25831. return 0;
  25832. }
  25833. DUK_MEMZERO(&tm, sizeof(tm));
  25834. tm.tm_sec = parts[DUK_DATE_IDX_SECOND];
  25835. tm.tm_min = parts[DUK_DATE_IDX_MINUTE];
  25836. tm.tm_hour = parts[DUK_DATE_IDX_HOUR];
  25837. tm.tm_mday = parts[DUK_DATE_IDX_DAY]; /* already one-based */
  25838. tm.tm_mon = parts[DUK_DATE_IDX_MONTH] - 1; /* one-based -> zero-based */
  25839. tm.tm_year = parts[DUK_DATE_IDX_YEAR] - 1900;
  25840. tm.tm_wday = parts[DUK_DATE_IDX_WEEKDAY];
  25841. tm.tm_isdst = 0;
  25842. DUK_MEMZERO(buf, sizeof(buf));
  25843. if ((flags & DUK_DATE_FLAG_TOSTRING_DATE) && (flags & DUK_DATE_FLAG_TOSTRING_TIME)) {
  25844. fmt = "%c";
  25845. } else if (flags & DUK_DATE_FLAG_TOSTRING_DATE) {
  25846. fmt = "%x";
  25847. } else {
  25848. DUK_ASSERT(flags & DUK_DATE_FLAG_TOSTRING_TIME);
  25849. fmt = "%X";
  25850. }
  25851. (void) strftime(buf, sizeof(buf) - 1, fmt, &tm);
  25852. DUK_ASSERT(buf[sizeof(buf) - 1] == 0);
  25853. duk_push_string(ctx, buf);
  25854. return 1;
  25855. }
  25856. #endif /* DUK_USE_DATE_FMT_STRFTIME */
  25857. #undef DUK__STRPTIME_BUF_SIZE
  25858. #undef DUK__STRFTIME_BUF_SIZE
  25859. #line 1 "duk_bi_date_windows.c"
  25860. /*
  25861. * Windows Date providers
  25862. *
  25863. * Platform specific links:
  25864. *
  25865. * - http://msdn.microsoft.com/en-us/library/windows/desktop/ms725473(v=vs.85).aspx
  25866. */
  25867. /* include removed: duk_internal.h */
  25868. /* The necessary #includes are in place in duk_config.h. */
  25869. #if defined(DUK_USE_DATE_NOW_WINDOWS) || defined(DUK_USE_DATE_TZO_WINDOWS)
  25870. /* Shared Windows helpers. */
  25871. DUK_LOCAL void duk__convert_systime_to_ularge(const SYSTEMTIME *st, ULARGE_INTEGER *res) {
  25872. FILETIME ft;
  25873. if (SystemTimeToFileTime(st, &ft) == 0) {
  25874. DUK_D(DUK_DPRINT("SystemTimeToFileTime() failed, returning 0"));
  25875. res->QuadPart = 0;
  25876. } else {
  25877. res->LowPart = ft.dwLowDateTime;
  25878. res->HighPart = ft.dwHighDateTime;
  25879. }
  25880. }
  25881. DUK_LOCAL void duk__set_systime_jan1970(SYSTEMTIME *st) {
  25882. DUK_MEMZERO((void *) st, sizeof(*st));
  25883. st->wYear = 1970;
  25884. st->wMonth = 1;
  25885. st->wDayOfWeek = 4; /* not sure whether or not needed; Thursday */
  25886. st->wDay = 1;
  25887. DUK_ASSERT(st->wHour == 0);
  25888. DUK_ASSERT(st->wMinute == 0);
  25889. DUK_ASSERT(st->wSecond == 0);
  25890. DUK_ASSERT(st->wMilliseconds == 0);
  25891. }
  25892. #endif /* defined(DUK_USE_DATE_NOW_WINDOWS) || defined(DUK_USE_DATE_TZO_WINDOWS) */
  25893. #ifdef DUK_USE_DATE_NOW_WINDOWS
  25894. DUK_INTERNAL duk_double_t duk_bi_date_get_now_windows(duk_context *ctx) {
  25895. /* Suggested step-by-step method from documentation of RtlTimeToSecondsSince1970:
  25896. * http://msdn.microsoft.com/en-us/library/windows/desktop/ms724928(v=vs.85).aspx
  25897. */
  25898. SYSTEMTIME st1, st2;
  25899. ULARGE_INTEGER tmp1, tmp2;
  25900. DUK_UNREF(ctx);
  25901. GetSystemTime(&st1);
  25902. duk__convert_systime_to_ularge((const SYSTEMTIME *) &st1, &tmp1);
  25903. duk__set_systime_jan1970(&st2);
  25904. duk__convert_systime_to_ularge((const SYSTEMTIME *) &st2, &tmp2);
  25905. /* Difference is in 100ns units, convert to milliseconds w/o fractions */
  25906. return (duk_double_t) ((tmp1.QuadPart - tmp2.QuadPart) / 10000LL);
  25907. }
  25908. #endif /* DUK_USE_DATE_NOW_WINDOWS */
  25909. #if defined(DUK_USE_DATE_TZO_WINDOWS)
  25910. DUK_INTERNAL_DECL duk_int_t duk_bi_date_get_local_tzoffset_windows(duk_double_t d) {
  25911. SYSTEMTIME st1;
  25912. SYSTEMTIME st2;
  25913. SYSTEMTIME st3;
  25914. ULARGE_INTEGER tmp1;
  25915. ULARGE_INTEGER tmp2;
  25916. ULARGE_INTEGER tmp3;
  25917. FILETIME ft1;
  25918. /* XXX: handling of timestamps outside Windows supported range.
  25919. * How does Windows deal with dates before 1600? Does windows
  25920. * support all Ecmascript years (like -200000 and +200000)?
  25921. * Should equivalent year mapping be used here too? If so, use
  25922. * a shared helper (currently integrated into timeval-to-parts).
  25923. */
  25924. /* Use the approach described in "Remarks" of FileTimeToLocalFileTime:
  25925. * http://msdn.microsoft.com/en-us/library/windows/desktop/ms724277(v=vs.85).aspx
  25926. */
  25927. duk__set_systime_jan1970(&st1);
  25928. duk__convert_systime_to_ularge((const SYSTEMTIME *) &st1, &tmp1);
  25929. tmp2.QuadPart = (ULONGLONG) (d * 10000.0); /* millisec -> 100ns units since jan 1, 1970 */
  25930. tmp2.QuadPart += tmp1.QuadPart; /* input 'd' in Windows UTC, 100ns units */
  25931. ft1.dwLowDateTime = tmp2.LowPart;
  25932. ft1.dwHighDateTime = tmp2.HighPart;
  25933. FileTimeToSystemTime((const FILETIME *) &ft1, &st2);
  25934. if (SystemTimeToTzSpecificLocalTime((LPTIME_ZONE_INFORMATION) NULL, &st2, &st3) == 0) {
  25935. DUK_D(DUK_DPRINT("SystemTimeToTzSpecificLocalTime() failed, return tzoffset 0"));
  25936. return 0;
  25937. }
  25938. duk__convert_systime_to_ularge((const SYSTEMTIME *) &st3, &tmp3);
  25939. /* Positive if local time ahead of UTC. */
  25940. return (duk_int_t) (((LONGLONG) tmp3.QuadPart - (LONGLONG) tmp2.QuadPart) / 10000000LL); /* seconds */
  25941. }
  25942. #endif /* DUK_USE_DATE_TZO_WINDOWS */
  25943. #line 1 "duk_bi_duktape.c"
  25944. /*
  25945. * Duktape built-ins
  25946. *
  25947. * Size optimization note: it might seem that vararg multipurpose functions
  25948. * like fin(), enc(), and dec() are not very size optimal, but using a single
  25949. * user-visible Ecmascript function saves a lot of run-time footprint; each
  25950. * Function instance takes >100 bytes. Using a shared native helper and a
  25951. * 'magic' value won't save much if there are multiple Function instances
  25952. * anyway.
  25953. */
  25954. /* include removed: duk_internal.h */
  25955. /* Raw helper to extract internal information / statistics about a value.
  25956. * The return values are version specific and must not expose anything
  25957. * that would lead to security issues (e.g. exposing compiled function
  25958. * 'data' buffer might be an issue). Currently only counts and sizes and
  25959. * such are given so there should not be a security impact.
  25960. */
  25961. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_info(duk_context *ctx) {
  25962. duk_hthread *thr = (duk_hthread *) ctx;
  25963. duk_tval *tv;
  25964. duk_heaphdr *h;
  25965. duk_int_t i, n;
  25966. DUK_UNREF(thr);
  25967. /* result array */
  25968. duk_push_array(ctx); /* -> [ val arr ] */
  25969. /* type tag (public) */
  25970. duk_push_int(ctx, duk_get_type(ctx, 0));
  25971. /* address */
  25972. tv = duk_get_tval(ctx, 0);
  25973. DUK_ASSERT(tv != NULL); /* because arg count is 1 */
  25974. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  25975. h = DUK_TVAL_GET_HEAPHDR(tv);
  25976. duk_push_pointer(ctx, (void *) h);
  25977. } else {
  25978. /* internal type tag */
  25979. duk_push_int(ctx, (duk_int_t) DUK_TVAL_GET_TAG(tv));
  25980. goto done;
  25981. }
  25982. DUK_ASSERT(h != NULL);
  25983. /* refcount */
  25984. #ifdef DUK_USE_REFERENCE_COUNTING
  25985. duk_push_size_t(ctx, DUK_HEAPHDR_GET_REFCOUNT(h));
  25986. #else
  25987. duk_push_undefined(ctx);
  25988. #endif
  25989. /* heaphdr size and additional allocation size, followed by
  25990. * type specific stuff (with varying value count)
  25991. */
  25992. switch ((duk_small_int_t) DUK_HEAPHDR_GET_TYPE(h)) {
  25993. case DUK_HTYPE_STRING: {
  25994. duk_hstring *h_str = (duk_hstring *) h;
  25995. duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hstring) + DUK_HSTRING_GET_BYTELEN(h_str) + 1));
  25996. break;
  25997. }
  25998. case DUK_HTYPE_OBJECT: {
  25999. duk_hobject *h_obj = (duk_hobject *) h;
  26000. duk_small_uint_t hdr_size;
  26001. if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h_obj)) {
  26002. hdr_size = (duk_small_uint_t) sizeof(duk_hcompiledfunction);
  26003. } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(h_obj)) {
  26004. hdr_size = (duk_small_uint_t) sizeof(duk_hnativefunction);
  26005. } else if (DUK_HOBJECT_IS_THREAD(h_obj)) {
  26006. hdr_size = (duk_small_uint_t) sizeof(duk_hthread);
  26007. } else {
  26008. hdr_size = (duk_small_uint_t) sizeof(duk_hobject);
  26009. }
  26010. duk_push_uint(ctx, (duk_uint_t) hdr_size);
  26011. duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_E_ALLOC_SIZE(h_obj));
  26012. duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_GET_ESIZE(h_obj));
  26013. /* Note: e_next indicates the number of gc-reachable entries
  26014. * in the entry part, and also indicates the index where the
  26015. * next new property would be inserted. It does *not* indicate
  26016. * the number of non-NULL keys present in the object. That
  26017. * value could be counted separately but requires a pass through
  26018. * the key list.
  26019. */
  26020. duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_GET_ENEXT(h_obj));
  26021. duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_GET_ASIZE(h_obj));
  26022. duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_GET_HSIZE(h_obj));
  26023. if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h_obj)) {
  26024. duk_hbuffer *h_data = (duk_hbuffer *) DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, (duk_hcompiledfunction *) h_obj);
  26025. if (h_data) {
  26026. duk_push_uint(ctx, (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_data));
  26027. } else {
  26028. duk_push_uint(ctx, 0);
  26029. }
  26030. }
  26031. break;
  26032. }
  26033. case DUK_HTYPE_BUFFER: {
  26034. duk_hbuffer *h_buf = (duk_hbuffer *) h;
  26035. if (DUK_HBUFFER_HAS_DYNAMIC(h_buf)) {
  26036. if (DUK_HBUFFER_HAS_EXTERNAL(h_buf)) {
  26037. duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hbuffer_external)));
  26038. } else {
  26039. /* When alloc_size == 0 the second allocation may not
  26040. * actually exist.
  26041. */
  26042. duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hbuffer_dynamic)));
  26043. }
  26044. duk_push_uint(ctx, (duk_uint_t) (DUK_HBUFFER_GET_SIZE(h_buf)));
  26045. } else {
  26046. duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hbuffer_fixed) + DUK_HBUFFER_GET_SIZE(h_buf) + 1));
  26047. }
  26048. break;
  26049. }
  26050. }
  26051. done:
  26052. /* set values into ret array */
  26053. /* XXX: primitive to make array from valstack slice */
  26054. n = duk_get_top(ctx);
  26055. for (i = 2; i < n; i++) {
  26056. duk_dup(ctx, i);
  26057. duk_put_prop_index(ctx, 1, i - 2);
  26058. }
  26059. duk_dup(ctx, 1);
  26060. return 1;
  26061. }
  26062. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_act(duk_context *ctx) {
  26063. duk_hthread *thr = (duk_hthread *) ctx;
  26064. duk_activation *act;
  26065. duk_uint_fast32_t pc;
  26066. duk_uint_fast32_t line;
  26067. duk_int_t level;
  26068. /* -1 = top callstack entry, callstack[callstack_top - 1]
  26069. * -callstack_top = bottom callstack entry, callstack[0]
  26070. */
  26071. level = duk_to_int(ctx, 0);
  26072. if (level >= 0 || -level > (duk_int_t) thr->callstack_top) {
  26073. return 0;
  26074. }
  26075. DUK_ASSERT(level >= -((duk_int_t) thr->callstack_top) && level <= -1);
  26076. act = thr->callstack + thr->callstack_top + level;
  26077. duk_push_object(ctx);
  26078. duk_push_tval(ctx, &act->tv_func);
  26079. /* Relevant PC is just before current one because PC is
  26080. * post-incremented. This should match what error augment
  26081. * code does.
  26082. */
  26083. pc = duk_hthread_get_act_prev_pc(thr, act);
  26084. duk_push_uint(ctx, (duk_uint_t) pc);
  26085. #if defined(DUK_USE_PC2LINE)
  26086. line = duk_hobject_pc2line_query(ctx, -2, pc);
  26087. #else
  26088. line = 0;
  26089. #endif
  26090. duk_push_uint(ctx, (duk_uint_t) line);
  26091. /* Providing access to e.g. act->lex_env would be dangerous: these
  26092. * internal structures must never be accessible to the application.
  26093. * Duktape relies on them having consistent data, and this consistency
  26094. * is only asserted for, not checked for.
  26095. */
  26096. /* [ level obj func pc line ] */
  26097. /* XXX: version specific array format instead? */
  26098. duk_xdef_prop_stridx_wec(ctx, -4, DUK_STRIDX_LINE_NUMBER);
  26099. duk_xdef_prop_stridx_wec(ctx, -3, DUK_STRIDX_PC);
  26100. duk_xdef_prop_stridx_wec(ctx, -2, DUK_STRIDX_LC_FUNCTION);
  26101. return 1;
  26102. }
  26103. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_gc(duk_context *ctx) {
  26104. #ifdef DUK_USE_MARK_AND_SWEEP
  26105. duk_hthread *thr = (duk_hthread *) ctx;
  26106. duk_small_uint_t flags;
  26107. duk_bool_t rc;
  26108. flags = (duk_small_uint_t) duk_get_uint(ctx, 0);
  26109. rc = duk_heap_mark_and_sweep(thr->heap, flags);
  26110. /* XXX: Not sure what the best return value would be in the API.
  26111. * Return a boolean for now. Note that rc == 0 is success (true).
  26112. */
  26113. duk_push_boolean(ctx, !rc);
  26114. return 1;
  26115. #else
  26116. DUK_UNREF(ctx);
  26117. return 0;
  26118. #endif
  26119. }
  26120. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_fin(duk_context *ctx) {
  26121. (void) duk_require_hobject(ctx, 0);
  26122. if (duk_get_top(ctx) >= 2) {
  26123. /* Set: currently a finalizer is disabled by setting it to
  26124. * undefined; this does not remove the property at the moment.
  26125. * The value could be type checked to be either a function
  26126. * or something else; if something else, the property could
  26127. * be deleted.
  26128. */
  26129. duk_set_top(ctx, 2);
  26130. (void) duk_put_prop_stridx(ctx, 0, DUK_STRIDX_INT_FINALIZER);
  26131. return 0;
  26132. } else {
  26133. /* Get. */
  26134. DUK_ASSERT(duk_get_top(ctx) == 1);
  26135. duk_get_prop_stridx(ctx, 0, DUK_STRIDX_INT_FINALIZER);
  26136. return 1;
  26137. }
  26138. }
  26139. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_enc(duk_context *ctx) {
  26140. duk_hthread *thr = (duk_hthread *) ctx;
  26141. duk_hstring *h_str;
  26142. /* Vararg function: must be careful to check/require arguments.
  26143. * The JSON helpers accept invalid indices and treat them like
  26144. * non-existent optional parameters.
  26145. */
  26146. h_str = duk_require_hstring(ctx, 0);
  26147. duk_require_valid_index(ctx, 1);
  26148. if (h_str == DUK_HTHREAD_STRING_HEX(thr)) {
  26149. duk_set_top(ctx, 2);
  26150. duk_hex_encode(ctx, 1);
  26151. DUK_ASSERT_TOP(ctx, 2);
  26152. } else if (h_str == DUK_HTHREAD_STRING_BASE64(thr)) {
  26153. duk_set_top(ctx, 2);
  26154. duk_base64_encode(ctx, 1);
  26155. DUK_ASSERT_TOP(ctx, 2);
  26156. #ifdef DUK_USE_JX
  26157. } else if (h_str == DUK_HTHREAD_STRING_JX(thr)) {
  26158. duk_bi_json_stringify_helper(ctx,
  26159. 1 /*idx_value*/,
  26160. 2 /*idx_replacer*/,
  26161. 3 /*idx_space*/,
  26162. DUK_JSON_FLAG_EXT_CUSTOM |
  26163. DUK_JSON_FLAG_ASCII_ONLY |
  26164. DUK_JSON_FLAG_AVOID_KEY_QUOTES /*flags*/);
  26165. #endif
  26166. #ifdef DUK_USE_JC
  26167. } else if (h_str == DUK_HTHREAD_STRING_JC(thr)) {
  26168. duk_bi_json_stringify_helper(ctx,
  26169. 1 /*idx_value*/,
  26170. 2 /*idx_replacer*/,
  26171. 3 /*idx_space*/,
  26172. DUK_JSON_FLAG_EXT_COMPATIBLE |
  26173. DUK_JSON_FLAG_ASCII_ONLY /*flags*/);
  26174. #endif
  26175. } else {
  26176. return DUK_RET_TYPE_ERROR;
  26177. }
  26178. return 1;
  26179. }
  26180. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_dec(duk_context *ctx) {
  26181. duk_hthread *thr = (duk_hthread *) ctx;
  26182. duk_hstring *h_str;
  26183. /* Vararg function: must be careful to check/require arguments.
  26184. * The JSON helpers accept invalid indices and treat them like
  26185. * non-existent optional parameters.
  26186. */
  26187. h_str = duk_require_hstring(ctx, 0);
  26188. duk_require_valid_index(ctx, 1);
  26189. if (h_str == DUK_HTHREAD_STRING_HEX(thr)) {
  26190. duk_set_top(ctx, 2);
  26191. duk_hex_decode(ctx, 1);
  26192. DUK_ASSERT_TOP(ctx, 2);
  26193. } else if (h_str == DUK_HTHREAD_STRING_BASE64(thr)) {
  26194. duk_set_top(ctx, 2);
  26195. duk_base64_decode(ctx, 1);
  26196. DUK_ASSERT_TOP(ctx, 2);
  26197. #ifdef DUK_USE_JX
  26198. } else if (h_str == DUK_HTHREAD_STRING_JX(thr)) {
  26199. duk_bi_json_parse_helper(ctx,
  26200. 1 /*idx_value*/,
  26201. 2 /*idx_replacer*/,
  26202. DUK_JSON_FLAG_EXT_CUSTOM /*flags*/);
  26203. #endif
  26204. #ifdef DUK_USE_JC
  26205. } else if (h_str == DUK_HTHREAD_STRING_JC(thr)) {
  26206. duk_bi_json_parse_helper(ctx,
  26207. 1 /*idx_value*/,
  26208. 2 /*idx_replacer*/,
  26209. DUK_JSON_FLAG_EXT_COMPATIBLE /*flags*/);
  26210. #endif
  26211. } else {
  26212. return DUK_RET_TYPE_ERROR;
  26213. }
  26214. return 1;
  26215. }
  26216. /*
  26217. * Compact an object
  26218. */
  26219. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_compact(duk_context *ctx) {
  26220. DUK_ASSERT_TOP(ctx, 1);
  26221. duk_compact(ctx, 0);
  26222. return 1; /* return the argument object */
  26223. }
  26224. #line 1 "duk_bi_error.c"
  26225. /*
  26226. * Error built-ins
  26227. */
  26228. /* include removed: duk_internal.h */
  26229. DUK_INTERNAL duk_ret_t duk_bi_error_constructor_shared(duk_context *ctx) {
  26230. /* Behavior for constructor and non-constructor call is
  26231. * the same except for augmenting the created error. When
  26232. * called as a constructor, the caller (duk_new()) will handle
  26233. * augmentation; when called as normal function, we need to do
  26234. * it here.
  26235. */
  26236. duk_hthread *thr = (duk_hthread *) ctx;
  26237. duk_small_int_t bidx_prototype = duk_get_current_magic(ctx);
  26238. /* same for both error and each subclass like TypeError */
  26239. duk_uint_t flags_and_class = DUK_HOBJECT_FLAG_EXTENSIBLE |
  26240. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ERROR);
  26241. DUK_UNREF(thr);
  26242. duk_push_object_helper(ctx, flags_and_class, bidx_prototype);
  26243. /* If message is undefined, the own property 'message' is not set at
  26244. * all to save property space. An empty message is inherited anyway.
  26245. */
  26246. if (!duk_is_undefined(ctx, 0)) {
  26247. duk_to_string(ctx, 0);
  26248. duk_dup(ctx, 0); /* [ message error message ] */
  26249. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC);
  26250. }
  26251. /* Augment the error if called as a normal function. __FILE__ and __LINE__
  26252. * are not desirable in this case.
  26253. */
  26254. #ifdef DUK_USE_AUGMENT_ERROR_CREATE
  26255. if (!duk_is_constructor_call(ctx)) {
  26256. duk_err_augment_error_create(thr, thr, NULL, 0, 1 /*noblame_fileline*/);
  26257. }
  26258. #endif
  26259. return 1;
  26260. }
  26261. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_to_string(duk_context *ctx) {
  26262. /* XXX: optimize with more direct internal access */
  26263. duk_push_this(ctx);
  26264. (void) duk_require_hobject_or_lfunc_coerce(ctx, -1);
  26265. /* [ ... this ] */
  26266. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_NAME);
  26267. if (duk_is_undefined(ctx, -1)) {
  26268. duk_pop(ctx);
  26269. duk_push_string(ctx, "Error");
  26270. } else {
  26271. duk_to_string(ctx, -1);
  26272. }
  26273. /* [ ... this name ] */
  26274. /* XXX: Are steps 6 and 7 in E5 Section 15.11.4.4 duplicated by
  26275. * accident or are they actually needed? The first ToString()
  26276. * could conceivably return 'undefined'.
  26277. */
  26278. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE);
  26279. if (duk_is_undefined(ctx, -1)) {
  26280. duk_pop(ctx);
  26281. duk_push_string(ctx, "");
  26282. } else {
  26283. duk_to_string(ctx, -1);
  26284. }
  26285. /* [ ... this name message ] */
  26286. if (duk_get_length(ctx, -2) == 0) {
  26287. /* name is empty -> return message */
  26288. return 1;
  26289. }
  26290. if (duk_get_length(ctx, -1) == 0) {
  26291. /* message is empty -> return name */
  26292. duk_pop(ctx);
  26293. return 1;
  26294. }
  26295. duk_push_string(ctx, ": ");
  26296. duk_insert(ctx, -2); /* ... name ': ' message */
  26297. duk_concat(ctx, 3);
  26298. return 1;
  26299. }
  26300. #ifdef DUK_USE_TRACEBACKS
  26301. /*
  26302. * Traceback handling
  26303. *
  26304. * The unified helper decodes the traceback and produces various requested
  26305. * outputs. It should be optimized for size, and may leave garbage on stack,
  26306. * only the topmost return value matters. For instance, traceback separator
  26307. * and decoded strings are pushed even when looking for filename only.
  26308. *
  26309. * NOTE: although _Tracedata is an internal property, user code can currently
  26310. * write to the array (or replace it with something other than an array).
  26311. * The code below must tolerate arbitrary _Tracedata. It can throw errors
  26312. * etc, but cannot cause a segfault or memory unsafe behavior.
  26313. */
  26314. /* constants arbitrary, chosen for small loads */
  26315. #define DUK__OUTPUT_TYPE_TRACEBACK (-1)
  26316. #define DUK__OUTPUT_TYPE_FILENAME 0
  26317. #define DUK__OUTPUT_TYPE_LINENUMBER 1
  26318. DUK_LOCAL duk_ret_t duk__traceback_getter_helper(duk_context *ctx, duk_small_int_t output_type) {
  26319. duk_hthread *thr = (duk_hthread *) ctx;
  26320. duk_idx_t idx_td;
  26321. duk_small_int_t i; /* traceback depth fits into 16 bits */
  26322. duk_small_int_t t; /* stack type fits into 16 bits */
  26323. const char *str_tailcalled = " tailcalled";
  26324. const char *str_strict = " strict";
  26325. const char *str_construct = " construct";
  26326. const char *str_prevyield = " preventsyield";
  26327. const char *str_directeval = " directeval";
  26328. const char *str_empty = "";
  26329. DUK_ASSERT_TOP(ctx, 0); /* fixed arg count */
  26330. duk_push_this(ctx);
  26331. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_TRACEDATA);
  26332. idx_td = duk_get_top_index(ctx);
  26333. duk_push_hstring_stridx(ctx, DUK_STRIDX_NEWLINE_TAB);
  26334. duk_push_this(ctx);
  26335. /* [ ... this tracedata sep this ] */
  26336. /* XXX: skip null filename? */
  26337. if (duk_check_type(ctx, idx_td, DUK_TYPE_OBJECT)) {
  26338. /* Current tracedata contains 2 entries per callstack entry. */
  26339. for (i = 0; ; i += 2) {
  26340. duk_int_t pc;
  26341. duk_int_t line;
  26342. duk_int_t flags;
  26343. duk_double_t d;
  26344. const char *funcname;
  26345. const char *filename;
  26346. duk_hobject *h_func;
  26347. duk_hstring *h_name;
  26348. duk_require_stack(ctx, 5);
  26349. duk_get_prop_index(ctx, idx_td, i);
  26350. duk_get_prop_index(ctx, idx_td, i + 1);
  26351. d = duk_to_number(ctx, -1);
  26352. pc = (duk_int_t) DUK_FMOD(d, DUK_DOUBLE_2TO32);
  26353. flags = (duk_int_t) DUK_FLOOR(d / DUK_DOUBLE_2TO32);
  26354. t = (duk_small_int_t) duk_get_type(ctx, -2);
  26355. if (t == DUK_TYPE_OBJECT || t == DUK_TYPE_LIGHTFUNC) {
  26356. /*
  26357. * Ecmascript/native function call or lightfunc call
  26358. */
  26359. /* [ ... v1(func) v2(pc+flags) ] */
  26360. h_func = duk_get_hobject(ctx, -2); /* NULL for lightfunc */
  26361. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_NAME);
  26362. duk_get_prop_stridx(ctx, -3, DUK_STRIDX_FILE_NAME);
  26363. #if defined(DUK_USE_PC2LINE)
  26364. line = duk_hobject_pc2line_query(ctx, -4, (duk_uint_fast32_t) pc);
  26365. #else
  26366. line = 0;
  26367. #endif
  26368. /* [ ... v1 v2 name filename ] */
  26369. if (output_type == DUK__OUTPUT_TYPE_FILENAME) {
  26370. return 1;
  26371. } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) {
  26372. duk_push_int(ctx, line);
  26373. return 1;
  26374. }
  26375. h_name = duk_get_hstring(ctx, -2); /* may be NULL */
  26376. funcname = (h_name == NULL || h_name == DUK_HTHREAD_STRING_EMPTY_STRING(thr)) ?
  26377. "anon" : (const char *) DUK_HSTRING_GET_DATA(h_name);
  26378. filename = duk_get_string(ctx, -1);
  26379. filename = filename ? filename : "";
  26380. DUK_ASSERT(funcname != NULL);
  26381. DUK_ASSERT(filename != NULL);
  26382. if (h_func == NULL) {
  26383. duk_push_sprintf(ctx, "%s light%s%s%s%s%s",
  26384. (const char *) funcname,
  26385. (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty),
  26386. (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcalled : str_empty),
  26387. (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty),
  26388. (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty),
  26389. (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty));
  26390. } else if (DUK_HOBJECT_HAS_NATIVEFUNCTION(h_func)) {
  26391. duk_push_sprintf(ctx, "%s %s native%s%s%s%s%s",
  26392. (const char *) funcname,
  26393. (const char *) filename,
  26394. (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty),
  26395. (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcalled : str_empty),
  26396. (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty),
  26397. (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty),
  26398. (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty));
  26399. } else {
  26400. duk_push_sprintf(ctx, "%s %s:%ld%s%s%s%s%s",
  26401. (const char *) funcname,
  26402. (const char *) filename,
  26403. (long) line,
  26404. (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty),
  26405. (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcalled : str_empty),
  26406. (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty),
  26407. (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty),
  26408. (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty));
  26409. }
  26410. duk_replace(ctx, -5); /* [ ... v1 v2 name filename str ] -> [ ... str v2 name filename ] */
  26411. duk_pop_n(ctx, 3); /* -> [ ... str ] */
  26412. } else if (t == DUK_TYPE_STRING) {
  26413. /*
  26414. * __FILE__ / __LINE__ entry, here 'pc' is line number directly.
  26415. * Sometimes __FILE__ / __LINE__ is reported as the source for
  26416. * the error (fileName, lineNumber), sometimes not.
  26417. */
  26418. /* [ ... v1(filename) v2(line+flags) ] */
  26419. if (!(flags & DUK_TB_FLAG_NOBLAME_FILELINE)) {
  26420. if (output_type == DUK__OUTPUT_TYPE_FILENAME) {
  26421. duk_pop(ctx);
  26422. return 1;
  26423. } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) {
  26424. duk_push_int(ctx, pc);
  26425. return 1;
  26426. }
  26427. }
  26428. duk_push_sprintf(ctx, "%s:%ld",
  26429. (const char *) duk_get_string(ctx, -2), (long) pc);
  26430. duk_replace(ctx, -3); /* [ ... v1 v2 str ] -> [ ... str v2 ] */
  26431. duk_pop(ctx); /* -> [ ... str ] */
  26432. } else {
  26433. /* unknown, ignore */
  26434. duk_pop_2(ctx);
  26435. break;
  26436. }
  26437. }
  26438. if (i >= DUK_USE_TRACEBACK_DEPTH * 2) {
  26439. /* Possibly truncated; there is no explicit truncation
  26440. * marker so this is the best we can do.
  26441. */
  26442. duk_push_hstring_stridx(ctx, DUK_STRIDX_BRACKETED_ELLIPSIS);
  26443. }
  26444. }
  26445. /* [ ... this tracedata sep this str1 ... strN ] */
  26446. if (output_type != DUK__OUTPUT_TYPE_TRACEBACK) {
  26447. return 0;
  26448. } else {
  26449. /* The 'this' after 'sep' will get ToString() coerced by
  26450. * duk_join() automatically. We don't want to do that
  26451. * coercion when providing .fileName or .lineNumber (GH-254).
  26452. */
  26453. duk_join(ctx, duk_get_top(ctx) - (idx_td + 2) /*count, not including sep*/);
  26454. return 1;
  26455. }
  26456. }
  26457. /* XXX: output type could be encoded into native function 'magic' value to
  26458. * save space.
  26459. */
  26460. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_stack_getter(duk_context *ctx) {
  26461. return duk__traceback_getter_helper(ctx, DUK__OUTPUT_TYPE_TRACEBACK);
  26462. }
  26463. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_filename_getter(duk_context *ctx) {
  26464. return duk__traceback_getter_helper(ctx, DUK__OUTPUT_TYPE_FILENAME);
  26465. }
  26466. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_linenumber_getter(duk_context *ctx) {
  26467. return duk__traceback_getter_helper(ctx, DUK__OUTPUT_TYPE_LINENUMBER);
  26468. }
  26469. #undef DUK__OUTPUT_TYPE_TRACEBACK
  26470. #undef DUK__OUTPUT_TYPE_FILENAME
  26471. #undef DUK__OUTPUT_TYPE_LINENUMBER
  26472. #else /* DUK_USE_TRACEBACKS */
  26473. /*
  26474. * Traceback handling when tracebacks disabled.
  26475. *
  26476. * The fileName / lineNumber stubs are now necessary because built-in
  26477. * data will include the accessor properties in Error.prototype. If those
  26478. * are removed for builds without tracebacks, these can also be removed.
  26479. * 'stack' should still be present and produce a ToString() equivalent:
  26480. * this is useful for user code which prints a stacktrace and expects to
  26481. * see something useful. A normal stacktrace also begins with a ToString()
  26482. * of the error so this makes sense.
  26483. */
  26484. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_stack_getter(duk_context *ctx) {
  26485. /* XXX: remove this native function and map 'stack' accessor
  26486. * to the toString() implementation directly.
  26487. */
  26488. return duk_bi_error_prototype_to_string(ctx);
  26489. }
  26490. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_filename_getter(duk_context *ctx) {
  26491. DUK_UNREF(ctx);
  26492. return 0;
  26493. }
  26494. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_linenumber_getter(duk_context *ctx) {
  26495. DUK_UNREF(ctx);
  26496. return 0;
  26497. }
  26498. #endif /* DUK_USE_TRACEBACKS */
  26499. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_nop_setter(duk_context *ctx) {
  26500. /* Attempt to write 'stack', 'fileName', 'lineNumber' is a silent no-op.
  26501. * User can use Object.defineProperty() to override this behavior.
  26502. */
  26503. DUK_ASSERT_TOP(ctx, 1); /* fixed arg count */
  26504. DUK_UNREF(ctx);
  26505. return 0;
  26506. }
  26507. #line 1 "duk_bi_function.c"
  26508. /*
  26509. * Function built-ins
  26510. */
  26511. /* include removed: duk_internal.h */
  26512. DUK_INTERNAL duk_ret_t duk_bi_function_constructor(duk_context *ctx) {
  26513. duk_hthread *thr = (duk_hthread *) ctx;
  26514. duk_hstring *h_sourcecode;
  26515. duk_idx_t nargs;
  26516. duk_idx_t i;
  26517. duk_small_uint_t comp_flags;
  26518. duk_hcompiledfunction *func;
  26519. duk_hobject *outer_lex_env;
  26520. duk_hobject *outer_var_env;
  26521. /* normal and constructor calls have identical semantics */
  26522. nargs = duk_get_top(ctx);
  26523. for (i = 0; i < nargs; i++) {
  26524. duk_to_string(ctx, i);
  26525. }
  26526. if (nargs == 0) {
  26527. duk_push_string(ctx, "");
  26528. duk_push_string(ctx, "");
  26529. } else if (nargs == 1) {
  26530. /* XXX: cover this with the generic >1 case? */
  26531. duk_push_string(ctx, "");
  26532. } else {
  26533. duk_insert(ctx, 0); /* [ arg1 ... argN-1 body] -> [body arg1 ... argN-1] */
  26534. duk_push_string(ctx, ",");
  26535. duk_insert(ctx, 1);
  26536. duk_join(ctx, nargs - 1);
  26537. }
  26538. /* [ body formals ], formals is comma separated list that needs to be parsed */
  26539. DUK_ASSERT_TOP(ctx, 2);
  26540. /* XXX: this placeholder is not always correct, but use for now.
  26541. * It will fail in corner cases; see test-dev-func-cons-args.js.
  26542. */
  26543. duk_push_string(ctx, "function(");
  26544. duk_dup(ctx, 1);
  26545. duk_push_string(ctx, "){");
  26546. duk_dup(ctx, 0);
  26547. duk_push_string(ctx, "}");
  26548. duk_concat(ctx, 5);
  26549. /* [ body formals source ] */
  26550. DUK_ASSERT_TOP(ctx, 3);
  26551. /* strictness is not inherited, intentional */
  26552. comp_flags = DUK_JS_COMPILE_FLAG_FUNCEXPR;
  26553. duk_push_hstring_stridx(ctx, DUK_STRIDX_COMPILE); /* XXX: copy from caller? */ /* XXX: ignored now */
  26554. h_sourcecode = duk_require_hstring(ctx, -2);
  26555. duk_js_compile(thr,
  26556. (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_sourcecode),
  26557. (duk_size_t) DUK_HSTRING_GET_BYTELEN(h_sourcecode),
  26558. comp_flags);
  26559. func = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1);
  26560. DUK_ASSERT(func != NULL);
  26561. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) func));
  26562. /* [ body formals source template ] */
  26563. /* only outer_lex_env matters, as functions always get a new
  26564. * variable declaration environment.
  26565. */
  26566. outer_lex_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  26567. outer_var_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  26568. duk_js_push_closure(thr, func, outer_var_env, outer_lex_env);
  26569. /* [ body formals source template closure ] */
  26570. return 1;
  26571. }
  26572. DUK_INTERNAL duk_ret_t duk_bi_function_prototype(duk_context *ctx) {
  26573. /* ignore arguments, return undefined (E5 Section 15.3.4) */
  26574. DUK_UNREF(ctx);
  26575. return 0;
  26576. }
  26577. DUK_INTERNAL duk_ret_t duk_bi_function_prototype_to_string(duk_context *ctx) {
  26578. duk_tval *tv;
  26579. /*
  26580. * E5 Section 15.3.4.2 places few requirements on the output of
  26581. * this function:
  26582. *
  26583. * - The result is an implementation dependent representation
  26584. * of the function; in particular
  26585. *
  26586. * - The result must follow the syntax of a FunctionDeclaration.
  26587. * In particular, the function must have a name (even in the
  26588. * case of an anonymous function or a function with an empty
  26589. * name).
  26590. *
  26591. * - Note in particular that the output does NOT need to compile
  26592. * into anything useful.
  26593. */
  26594. /* XXX: faster internal way to get this */
  26595. duk_push_this(ctx);
  26596. tv = duk_get_tval(ctx, -1);
  26597. DUK_ASSERT(tv != NULL);
  26598. if (DUK_TVAL_IS_OBJECT(tv)) {
  26599. duk_hobject *obj = DUK_TVAL_GET_OBJECT(tv);
  26600. const char *func_name = DUK_STR_ANON;
  26601. /* XXX: rework, it would be nice to avoid C formatting functions to
  26602. * ensure there are no Unicode issues.
  26603. */
  26604. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_NAME);
  26605. if (!duk_is_undefined(ctx, -1)) {
  26606. func_name = duk_to_string(ctx, -1);
  26607. DUK_ASSERT(func_name != NULL);
  26608. if (func_name[0] == (char) 0) {
  26609. func_name = DUK_STR_ANON;
  26610. }
  26611. }
  26612. if (DUK_HOBJECT_HAS_COMPILEDFUNCTION(obj)) {
  26613. /* XXX: actual source, if available */
  26614. duk_push_sprintf(ctx, "function %s() {/* ecmascript */}", (const char *) func_name);
  26615. } else if (DUK_HOBJECT_HAS_NATIVEFUNCTION(obj)) {
  26616. duk_push_sprintf(ctx, "function %s() {/* native */}", (const char *) func_name);
  26617. } else if (DUK_HOBJECT_HAS_BOUND(obj)) {
  26618. duk_push_sprintf(ctx, "function %s() {/* bound */}", (const char *) func_name);
  26619. } else {
  26620. goto type_error;
  26621. }
  26622. } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  26623. duk_push_lightfunc_tostring(ctx, tv);
  26624. } else {
  26625. goto type_error;
  26626. }
  26627. return 1;
  26628. type_error:
  26629. return DUK_RET_TYPE_ERROR;
  26630. }
  26631. DUK_INTERNAL duk_ret_t duk_bi_function_prototype_apply(duk_context *ctx) {
  26632. duk_idx_t len;
  26633. duk_idx_t i;
  26634. DUK_ASSERT_TOP(ctx, 2); /* not a vararg function */
  26635. duk_push_this(ctx);
  26636. if (!duk_is_callable(ctx, -1)) {
  26637. DUK_DDD(DUK_DDDPRINT("func is not callable"));
  26638. goto type_error;
  26639. }
  26640. duk_insert(ctx, 0);
  26641. DUK_ASSERT_TOP(ctx, 3);
  26642. DUK_DDD(DUK_DDDPRINT("func=%!iT, thisArg=%!iT, argArray=%!iT",
  26643. (duk_tval *) duk_get_tval(ctx, 0),
  26644. (duk_tval *) duk_get_tval(ctx, 1),
  26645. (duk_tval *) duk_get_tval(ctx, 2)));
  26646. /* [ func thisArg argArray ] */
  26647. if (duk_is_null_or_undefined(ctx, 2)) {
  26648. DUK_DDD(DUK_DDDPRINT("argArray is null/undefined, no args"));
  26649. len = 0;
  26650. } else if (!duk_is_object(ctx, 2)) {
  26651. goto type_error;
  26652. } else {
  26653. DUK_DDD(DUK_DDDPRINT("argArray is an object"));
  26654. /* XXX: make this an internal helper */
  26655. duk_get_prop_stridx(ctx, 2, DUK_STRIDX_LENGTH);
  26656. len = (duk_idx_t) duk_to_uint32(ctx, -1); /* ToUint32() coercion required */
  26657. duk_pop(ctx);
  26658. duk_require_stack(ctx, len);
  26659. DUK_DDD(DUK_DDDPRINT("argArray length is %ld", (long) len));
  26660. for (i = 0; i < len; i++) {
  26661. duk_get_prop_index(ctx, 2, i);
  26662. }
  26663. }
  26664. duk_remove(ctx, 2);
  26665. DUK_ASSERT_TOP(ctx, 2 + len);
  26666. /* [ func thisArg arg1 ... argN ] */
  26667. DUK_DDD(DUK_DDDPRINT("apply, func=%!iT, thisArg=%!iT, len=%ld",
  26668. (duk_tval *) duk_get_tval(ctx, 0),
  26669. (duk_tval *) duk_get_tval(ctx, 1),
  26670. (long) len));
  26671. duk_call_method(ctx, len);
  26672. return 1;
  26673. type_error:
  26674. return DUK_RET_TYPE_ERROR;
  26675. }
  26676. DUK_INTERNAL duk_ret_t duk_bi_function_prototype_call(duk_context *ctx) {
  26677. duk_idx_t nargs;
  26678. /* Step 1 is not necessary because duk_call_method() will take
  26679. * care of it.
  26680. */
  26681. /* vararg function, thisArg needs special handling */
  26682. nargs = duk_get_top(ctx); /* = 1 + arg count */
  26683. if (nargs == 0) {
  26684. duk_push_undefined(ctx);
  26685. nargs++;
  26686. }
  26687. DUK_ASSERT(nargs >= 1);
  26688. /* [ thisArg arg1 ... argN ] */
  26689. duk_push_this(ctx); /* 'func' in the algorithm */
  26690. duk_insert(ctx, 0);
  26691. /* [ func thisArg arg1 ... argN ] */
  26692. DUK_DDD(DUK_DDDPRINT("func=%!iT, thisArg=%!iT, argcount=%ld, top=%ld",
  26693. (duk_tval *) duk_get_tval(ctx, 0),
  26694. (duk_tval *) duk_get_tval(ctx, 1),
  26695. (long) (nargs - 1),
  26696. (long) duk_get_top(ctx)));
  26697. duk_call_method(ctx, nargs - 1);
  26698. return 1;
  26699. }
  26700. /* XXX: the implementation now assumes "chained" bound functions,
  26701. * whereas "collapsed" bound functions (where there is ever only
  26702. * one bound function which directly points to a non-bound, final
  26703. * function) would require a "collapsing" implementation which
  26704. * merges argument lists etc here.
  26705. */
  26706. DUK_INTERNAL duk_ret_t duk_bi_function_prototype_bind(duk_context *ctx) {
  26707. duk_hobject *h_bound;
  26708. duk_hobject *h_target;
  26709. duk_idx_t nargs;
  26710. duk_idx_t i;
  26711. /* vararg function, careful arg handling (e.g. thisArg may not be present) */
  26712. nargs = duk_get_top(ctx); /* = 1 + arg count */
  26713. if (nargs == 0) {
  26714. duk_push_undefined(ctx);
  26715. nargs++;
  26716. }
  26717. DUK_ASSERT(nargs >= 1);
  26718. duk_push_this(ctx);
  26719. if (!duk_is_callable(ctx, -1)) {
  26720. DUK_DDD(DUK_DDDPRINT("func is not callable"));
  26721. goto type_error;
  26722. }
  26723. /* [ thisArg arg1 ... argN func ] (thisArg+args == nargs total) */
  26724. DUK_ASSERT_TOP(ctx, nargs + 1);
  26725. /* create bound function object */
  26726. duk_push_object_helper(ctx,
  26727. DUK_HOBJECT_FLAG_EXTENSIBLE |
  26728. DUK_HOBJECT_FLAG_BOUND |
  26729. DUK_HOBJECT_FLAG_CONSTRUCTABLE |
  26730. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION),
  26731. DUK_BIDX_FUNCTION_PROTOTYPE);
  26732. h_bound = duk_get_hobject(ctx, -1);
  26733. DUK_ASSERT(h_bound != NULL);
  26734. /* [ thisArg arg1 ... argN func boundFunc ] */
  26735. duk_dup(ctx, -2); /* func */
  26736. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_TARGET, DUK_PROPDESC_FLAGS_NONE);
  26737. duk_dup(ctx, 0); /* thisArg */
  26738. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_THIS, DUK_PROPDESC_FLAGS_NONE);
  26739. duk_push_array(ctx);
  26740. /* [ thisArg arg1 ... argN func boundFunc argArray ] */
  26741. for (i = 0; i < nargs - 1; i++) {
  26742. duk_dup(ctx, 1 + i);
  26743. duk_put_prop_index(ctx, -2, i);
  26744. }
  26745. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_ARGS, DUK_PROPDESC_FLAGS_NONE);
  26746. /* [ thisArg arg1 ... argN func boundFunc ] */
  26747. /* bound function 'length' property is interesting */
  26748. h_target = duk_get_hobject(ctx, -2);
  26749. if (h_target == NULL || /* lightfunc */
  26750. DUK_HOBJECT_GET_CLASS_NUMBER(h_target) == DUK_HOBJECT_CLASS_FUNCTION) {
  26751. /* For lightfuncs, simply read the virtual property. */
  26752. duk_int_t tmp;
  26753. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH);
  26754. tmp = duk_to_int(ctx, -1) - (nargs - 1); /* step 15.a */
  26755. duk_pop(ctx);
  26756. duk_push_int(ctx, (tmp < 0 ? 0 : tmp));
  26757. } else {
  26758. duk_push_int(ctx, 0);
  26759. }
  26760. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE); /* attrs in E5 Section 15.3.5.1 */
  26761. /* caller and arguments must use the same thrower, [[ThrowTypeError]] */
  26762. duk_xdef_prop_stridx_thrower(ctx, -1, DUK_STRIDX_CALLER, DUK_PROPDESC_FLAGS_NONE);
  26763. duk_xdef_prop_stridx_thrower(ctx, -1, DUK_STRIDX_LC_ARGUMENTS, DUK_PROPDESC_FLAGS_NONE);
  26764. /* these non-standard properties are copied for convenience */
  26765. /* XXX: 'copy properties' API call? */
  26766. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_NAME);
  26767. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_WC);
  26768. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME);
  26769. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC);
  26770. /* The 'strict' flag is copied to get the special [[Get]] of E5.1
  26771. * Section 15.3.5.4 to apply when a 'caller' value is a strict bound
  26772. * function. Not sure if this is correct, because the specification
  26773. * is a bit ambiguous on this point but it would make sense.
  26774. */
  26775. if (h_target == NULL) {
  26776. /* Lightfuncs are always strict. */
  26777. DUK_HOBJECT_SET_STRICT(h_bound);
  26778. } else if (DUK_HOBJECT_HAS_STRICT(h_target)) {
  26779. DUK_HOBJECT_SET_STRICT(h_bound);
  26780. }
  26781. DUK_DDD(DUK_DDDPRINT("created bound function: %!iT", (duk_tval *) duk_get_tval(ctx, -1)));
  26782. return 1;
  26783. type_error:
  26784. return DUK_RET_TYPE_ERROR;
  26785. }
  26786. #line 1 "duk_bi_global.c"
  26787. /*
  26788. * Global object built-ins
  26789. */
  26790. /* include removed: duk_internal.h */
  26791. /*
  26792. * Encoding/decoding helpers
  26793. */
  26794. /* XXX: Could add fast path (for each transform callback) with direct byte
  26795. * lookups (no shifting) and no explicit check for x < 0x80 before table
  26796. * lookup.
  26797. */
  26798. /* Macros for creating and checking bitmasks for character encoding.
  26799. * Bit number is a bit counterintuitive, but minimizes code size.
  26800. */
  26801. #define DUK__MKBITS(a,b,c,d,e,f,g,h) ((duk_uint8_t) ( \
  26802. ((a) << 0) | ((b) << 1) | ((c) << 2) | ((d) << 3) | \
  26803. ((e) << 4) | ((f) << 5) | ((g) << 6) | ((h) << 7) \
  26804. ))
  26805. #define DUK__CHECK_BITMASK(table,cp) ((table)[(cp) >> 3] & (1 << ((cp) & 0x07)))
  26806. /* E5.1 Section 15.1.3.3: uriReserved + uriUnescaped + '#' */
  26807. DUK_LOCAL const duk_uint8_t duk__encode_uriunescaped_table[16] = {
  26808. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */
  26809. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */
  26810. DUK__MKBITS(0, 1, 0, 1, 1, 0, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x20-0x2f */
  26811. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 0, 1, 0, 1), /* 0x30-0x3f */
  26812. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x40-0x4f */
  26813. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 1), /* 0x50-0x5f */
  26814. DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x60-0x6f */
  26815. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 1, 0), /* 0x70-0x7f */
  26816. };
  26817. /* E5.1 Section 15.1.3.4: uriUnescaped */
  26818. DUK_LOCAL const duk_uint8_t duk__encode_uricomponent_unescaped_table[16] = {
  26819. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */
  26820. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */
  26821. DUK__MKBITS(0, 1, 0, 0, 0, 0, 0, 1), DUK__MKBITS(1, 1, 1, 0, 0, 1, 1, 0), /* 0x20-0x2f */
  26822. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 0, 0, 0, 0, 0, 0), /* 0x30-0x3f */
  26823. DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x40-0x4f */
  26824. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 1), /* 0x50-0x5f */
  26825. DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x60-0x6f */
  26826. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 1, 0), /* 0x70-0x7f */
  26827. };
  26828. /* E5.1 Section 15.1.3.1: uriReserved + '#' */
  26829. DUK_LOCAL const duk_uint8_t duk__decode_uri_reserved_table[16] = {
  26830. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */
  26831. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */
  26832. DUK__MKBITS(0, 0, 0, 1, 1, 0, 1, 0), DUK__MKBITS(0, 0, 0, 1, 1, 0, 0, 1), /* 0x20-0x2f */
  26833. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 1, 1, 0, 1, 0, 1), /* 0x30-0x3f */
  26834. DUK__MKBITS(1, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x40-0x4f */
  26835. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x50-0x5f */
  26836. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x60-0x6f */
  26837. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x70-0x7f */
  26838. };
  26839. /* E5.1 Section 15.1.3.2: empty */
  26840. DUK_LOCAL const duk_uint8_t duk__decode_uri_component_reserved_table[16] = {
  26841. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */
  26842. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */
  26843. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x20-0x2f */
  26844. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x30-0x3f */
  26845. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x40-0x4f */
  26846. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x50-0x5f */
  26847. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x60-0x6f */
  26848. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x70-0x7f */
  26849. };
  26850. #ifdef DUK_USE_SECTION_B
  26851. /* E5.1 Section B.2.2, step 7. */
  26852. DUK_LOCAL const duk_uint8_t duk__escape_unescaped_table[16] = {
  26853. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */
  26854. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */
  26855. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 1, 1, 0, 1, 1, 1), /* 0x20-0x2f */
  26856. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 0, 0, 0, 0, 0, 0), /* 0x30-0x3f */
  26857. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x40-0x4f */
  26858. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 1), /* 0x50-0x5f */
  26859. DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x60-0x6f */
  26860. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 0) /* 0x70-0x7f */
  26861. };
  26862. #endif /* DUK_USE_SECTION_B */
  26863. #undef DUK__MKBITS
  26864. typedef struct {
  26865. duk_hthread *thr;
  26866. duk_hstring *h_str;
  26867. duk_bufwriter_ctx bw;
  26868. const duk_uint8_t *p;
  26869. const duk_uint8_t *p_start;
  26870. const duk_uint8_t *p_end;
  26871. } duk__transform_context;
  26872. typedef void (*duk__transform_callback)(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp);
  26873. /* XXX: refactor and share with other code */
  26874. DUK_LOCAL duk_small_int_t duk__decode_hex_escape(const duk_uint8_t *p, duk_small_int_t n) {
  26875. duk_small_int_t ch;
  26876. duk_small_int_t t = 0;
  26877. while (n > 0) {
  26878. t = t * 16;
  26879. ch = (duk_small_int_t) duk_hex_dectab[*p++];
  26880. if (DUK_LIKELY(ch >= 0)) {
  26881. t += ch;
  26882. } else {
  26883. return -1;
  26884. }
  26885. n--;
  26886. }
  26887. return t;
  26888. }
  26889. DUK_LOCAL int duk__transform_helper(duk_context *ctx, duk__transform_callback callback, const void *udata) {
  26890. duk_hthread *thr = (duk_hthread *) ctx;
  26891. duk__transform_context tfm_ctx_alloc;
  26892. duk__transform_context *tfm_ctx = &tfm_ctx_alloc;
  26893. duk_codepoint_t cp;
  26894. tfm_ctx->thr = thr;
  26895. tfm_ctx->h_str = duk_to_hstring(ctx, 0);
  26896. DUK_ASSERT(tfm_ctx->h_str != NULL);
  26897. DUK_BW_INIT_PUSHBUF(thr, &tfm_ctx->bw, DUK_HSTRING_GET_BYTELEN(tfm_ctx->h_str)); /* initial size guess */
  26898. tfm_ctx->p_start = DUK_HSTRING_GET_DATA(tfm_ctx->h_str);
  26899. tfm_ctx->p_end = tfm_ctx->p_start + DUK_HSTRING_GET_BYTELEN(tfm_ctx->h_str);
  26900. tfm_ctx->p = tfm_ctx->p_start;
  26901. while (tfm_ctx->p < tfm_ctx->p_end) {
  26902. cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &tfm_ctx->p, tfm_ctx->p_start, tfm_ctx->p_end);
  26903. callback(tfm_ctx, udata, cp);
  26904. }
  26905. DUK_BW_COMPACT(thr, &tfm_ctx->bw);
  26906. duk_to_string(ctx, -1);
  26907. return 1;
  26908. }
  26909. DUK_LOCAL void duk__transform_callback_encode_uri(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp) {
  26910. duk_uint8_t xutf8_buf[DUK_UNICODE_MAX_XUTF8_LENGTH];
  26911. duk_small_int_t len;
  26912. duk_codepoint_t cp1, cp2;
  26913. duk_small_int_t i, t;
  26914. const duk_uint8_t *unescaped_table = (const duk_uint8_t *) udata;
  26915. /* UTF-8 encoded bytes escaped as %xx%xx%xx... -> 3 * nbytes.
  26916. * Codepoint range is restricted so this is a slightly too large
  26917. * but doesn't matter.
  26918. */
  26919. DUK_BW_ENSURE(tfm_ctx->thr, &tfm_ctx->bw, 3 * DUK_UNICODE_MAX_XUTF8_LENGTH);
  26920. if (cp < 0) {
  26921. goto uri_error;
  26922. } else if ((cp < 0x80L) && DUK__CHECK_BITMASK(unescaped_table, cp)) {
  26923. DUK_BW_WRITE_RAW_U8(tfm_ctx->thr, &tfm_ctx->bw, (duk_uint8_t) cp);
  26924. return;
  26925. } else if (cp >= 0xdc00L && cp <= 0xdfffL) {
  26926. goto uri_error;
  26927. } else if (cp >= 0xd800L && cp <= 0xdbffL) {
  26928. /* Needs lookahead */
  26929. if (duk_unicode_decode_xutf8(tfm_ctx->thr, &tfm_ctx->p, tfm_ctx->p_start, tfm_ctx->p_end, (duk_ucodepoint_t *) &cp2) == 0) {
  26930. goto uri_error;
  26931. }
  26932. if (!(cp2 >= 0xdc00L && cp2 <= 0xdfffL)) {
  26933. goto uri_error;
  26934. }
  26935. cp1 = cp;
  26936. cp = ((cp1 - 0xd800L) << 10) + (cp2 - 0xdc00L) + 0x10000L;
  26937. } else if (cp > 0x10ffffL) {
  26938. /* Although we can allow non-BMP characters (they'll decode
  26939. * back into surrogate pairs), we don't allow extended UTF-8
  26940. * characters; they would encode to URIs which won't decode
  26941. * back because of strict UTF-8 checks in URI decoding.
  26942. * (However, we could just as well allow them here.)
  26943. */
  26944. goto uri_error;
  26945. } else {
  26946. /* Non-BMP characters within valid UTF-8 range: encode as is.
  26947. * They'll decode back into surrogate pairs if the escaped
  26948. * output is decoded.
  26949. */
  26950. ;
  26951. }
  26952. len = duk_unicode_encode_xutf8((duk_ucodepoint_t) cp, xutf8_buf);
  26953. for (i = 0; i < len; i++) {
  26954. t = (int) xutf8_buf[i];
  26955. DUK_BW_WRITE_RAW_U8_3(tfm_ctx->thr,
  26956. &tfm_ctx->bw,
  26957. DUK_ASC_PERCENT,
  26958. (duk_uint8_t) duk_uc_nybbles[t >> 4],
  26959. (duk_uint8_t) duk_uc_nybbles[t & 0x0f]);
  26960. }
  26961. return;
  26962. uri_error:
  26963. DUK_ERROR(tfm_ctx->thr, DUK_ERR_URI_ERROR, "invalid input");
  26964. }
  26965. DUK_LOCAL void duk__transform_callback_decode_uri(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp) {
  26966. const duk_uint8_t *reserved_table = (const duk_uint8_t *) udata;
  26967. duk_small_uint_t utf8_blen;
  26968. duk_codepoint_t min_cp;
  26969. duk_small_int_t t; /* must be signed */
  26970. duk_small_uint_t i;
  26971. /* Maximum write size: XUTF8 path writes max DUK_UNICODE_MAX_XUTF8_LENGTH,
  26972. * percent escape path writes max two times CESU-8 encoded BMP length.
  26973. */
  26974. DUK_BW_ENSURE(tfm_ctx->thr,
  26975. &tfm_ctx->bw,
  26976. (DUK_UNICODE_MAX_XUTF8_LENGTH >= 2 * DUK_UNICODE_MAX_CESU8_BMP_LENGTH ?
  26977. DUK_UNICODE_MAX_XUTF8_LENGTH : DUK_UNICODE_MAX_CESU8_BMP_LENGTH));
  26978. if (cp == (duk_codepoint_t) '%') {
  26979. const duk_uint8_t *p = tfm_ctx->p;
  26980. duk_size_t left = (duk_size_t) (tfm_ctx->p_end - p); /* bytes left */
  26981. DUK_DDD(DUK_DDDPRINT("percent encoding, left=%ld", (long) left));
  26982. if (left < 2) {
  26983. goto uri_error;
  26984. }
  26985. t = duk__decode_hex_escape(p, 2);
  26986. DUK_DDD(DUK_DDDPRINT("first byte: %ld", (long) t));
  26987. if (t < 0) {
  26988. goto uri_error;
  26989. }
  26990. if (t < 0x80) {
  26991. if (DUK__CHECK_BITMASK(reserved_table, t)) {
  26992. /* decode '%xx' to '%xx' if decoded char in reserved set */
  26993. DUK_ASSERT(tfm_ctx->p - 1 >= tfm_ctx->p_start);
  26994. DUK_BW_WRITE_RAW_U8_3(tfm_ctx->thr,
  26995. &tfm_ctx->bw,
  26996. DUK_ASC_PERCENT,
  26997. p[0],
  26998. p[1]);
  26999. } else {
  27000. DUK_BW_WRITE_RAW_U8(tfm_ctx->thr, &tfm_ctx->bw, (duk_uint8_t) t);
  27001. }
  27002. tfm_ctx->p += 2;
  27003. return;
  27004. }
  27005. /* Decode UTF-8 codepoint from a sequence of hex escapes. The
  27006. * first byte of the sequence has been decoded to 't'.
  27007. *
  27008. * Note that UTF-8 validation must be strict according to the
  27009. * specification: E5.1 Section 15.1.3, decode algorithm step
  27010. * 4.d.vii.8. URIError from non-shortest encodings is also
  27011. * specifically noted in the spec.
  27012. */
  27013. DUK_ASSERT(t >= 0x80);
  27014. if (t < 0xc0) {
  27015. /* continuation byte */
  27016. goto uri_error;
  27017. } else if (t < 0xe0) {
  27018. /* 110x xxxx; 2 bytes */
  27019. utf8_blen = 2;
  27020. min_cp = 0x80L;
  27021. cp = t & 0x1f;
  27022. } else if (t < 0xf0) {
  27023. /* 1110 xxxx; 3 bytes */
  27024. utf8_blen = 3;
  27025. min_cp = 0x800L;
  27026. cp = t & 0x0f;
  27027. } else if (t < 0xf8) {
  27028. /* 1111 0xxx; 4 bytes */
  27029. utf8_blen = 4;
  27030. min_cp = 0x10000L;
  27031. cp = t & 0x07;
  27032. } else {
  27033. /* extended utf-8 not allowed for URIs */
  27034. goto uri_error;
  27035. }
  27036. if (left < utf8_blen * 3 - 1) {
  27037. /* '%xx%xx...%xx', p points to char after first '%' */
  27038. goto uri_error;
  27039. }
  27040. p += 3;
  27041. for (i = 1; i < utf8_blen; i++) {
  27042. /* p points to digit part ('%xy', p points to 'x') */
  27043. t = duk__decode_hex_escape(p, 2);
  27044. DUK_DDD(DUK_DDDPRINT("i=%ld utf8_blen=%ld cp=%ld t=0x%02lx",
  27045. (long) i, (long) utf8_blen, (long) cp, (unsigned long) t));
  27046. if (t < 0) {
  27047. goto uri_error;
  27048. }
  27049. if ((t & 0xc0) != 0x80) {
  27050. goto uri_error;
  27051. }
  27052. cp = (cp << 6) + (t & 0x3f);
  27053. p += 3;
  27054. }
  27055. p--; /* p overshoots */
  27056. tfm_ctx->p = p;
  27057. DUK_DDD(DUK_DDDPRINT("final cp=%ld, min_cp=%ld", (long) cp, (long) min_cp));
  27058. if (cp < min_cp || cp > 0x10ffffL || (cp >= 0xd800L && cp <= 0xdfffL)) {
  27059. goto uri_error;
  27060. }
  27061. /* The E5.1 algorithm checks whether or not a decoded codepoint
  27062. * is below 0x80 and perhaps may be in the "reserved" set.
  27063. * This seems pointless because the single byte UTF-8 case is
  27064. * handled separately, and non-shortest encodings are rejected.
  27065. * So, 'cp' cannot be below 0x80 here, and thus cannot be in
  27066. * the reserved set.
  27067. */
  27068. /* utf-8 validation ensures these */
  27069. DUK_ASSERT(cp >= 0x80L && cp <= 0x10ffffL);
  27070. if (cp >= 0x10000L) {
  27071. cp -= 0x10000L;
  27072. DUK_ASSERT(cp < 0x100000L);
  27073. DUK_BW_WRITE_RAW_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, ((cp >> 10) + 0xd800L));
  27074. DUK_BW_WRITE_RAW_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, ((cp & 0x03ffUL) + 0xdc00L));
  27075. } else {
  27076. DUK_BW_WRITE_RAW_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, cp);
  27077. }
  27078. } else {
  27079. DUK_BW_WRITE_RAW_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, cp);
  27080. }
  27081. return;
  27082. uri_error:
  27083. DUK_ERROR(tfm_ctx->thr, DUK_ERR_URI_ERROR, "invalid input");
  27084. }
  27085. #ifdef DUK_USE_SECTION_B
  27086. DUK_LOCAL void duk__transform_callback_escape(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp) {
  27087. DUK_UNREF(udata);
  27088. DUK_BW_ENSURE(tfm_ctx->thr, &tfm_ctx->bw, 6);
  27089. if (cp < 0) {
  27090. goto esc_error;
  27091. } else if ((cp < 0x80L) && DUK__CHECK_BITMASK(duk__escape_unescaped_table, cp)) {
  27092. DUK_BW_WRITE_RAW_U8(tfm_ctx->thr, &tfm_ctx->bw, (duk_uint8_t) cp);
  27093. } else if (cp < 0x100L) {
  27094. DUK_BW_WRITE_RAW_U8_3(tfm_ctx->thr,
  27095. &tfm_ctx->bw,
  27096. (duk_uint8_t) DUK_ASC_PERCENT,
  27097. (duk_uint8_t) duk_uc_nybbles[cp >> 4],
  27098. (duk_uint8_t) duk_uc_nybbles[cp & 0x0f]);
  27099. } else if (cp < 0x10000L) {
  27100. DUK_BW_WRITE_RAW_U8_6(tfm_ctx->thr,
  27101. &tfm_ctx->bw,
  27102. (duk_uint8_t) DUK_ASC_PERCENT,
  27103. (duk_uint8_t) DUK_ASC_LC_U,
  27104. (duk_uint8_t) duk_uc_nybbles[cp >> 12],
  27105. (duk_uint8_t) duk_uc_nybbles[(cp >> 8) & 0x0f],
  27106. (duk_uint8_t) duk_uc_nybbles[(cp >> 4) & 0x0f],
  27107. (duk_uint8_t) duk_uc_nybbles[cp & 0x0f]);
  27108. } else {
  27109. /* Characters outside BMP cannot be escape()'d. We could
  27110. * encode them as surrogate pairs (for codepoints inside
  27111. * valid UTF-8 range, but not extended UTF-8). Because
  27112. * escape() and unescape() are legacy functions, we don't.
  27113. */
  27114. goto esc_error;
  27115. }
  27116. return;
  27117. esc_error:
  27118. DUK_ERROR(tfm_ctx->thr, DUK_ERR_TYPE_ERROR, "invalid input");
  27119. }
  27120. DUK_LOCAL void duk__transform_callback_unescape(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp) {
  27121. duk_small_int_t t;
  27122. DUK_UNREF(udata);
  27123. if (cp == (duk_codepoint_t) '%') {
  27124. const duk_uint8_t *p = tfm_ctx->p;
  27125. duk_size_t left = (duk_size_t) (tfm_ctx->p_end - p); /* bytes left */
  27126. if (left >= 5 && p[0] == 'u' &&
  27127. ((t = duk__decode_hex_escape(p + 1, 4)) >= 0)) {
  27128. cp = (duk_codepoint_t) t;
  27129. tfm_ctx->p += 5;
  27130. } else if (left >= 2 &&
  27131. ((t = duk__decode_hex_escape(p, 2)) >= 0)) {
  27132. cp = (duk_codepoint_t) t;
  27133. tfm_ctx->p += 2;
  27134. }
  27135. }
  27136. DUK_BW_WRITE_ENSURE_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, cp);
  27137. }
  27138. #endif /* DUK_USE_SECTION_B */
  27139. /*
  27140. * Eval
  27141. *
  27142. * Eval needs to handle both a "direct eval" and an "indirect eval".
  27143. * Direct eval handling needs access to the caller's activation so that its
  27144. * lexical environment can be accessed. A direct eval is only possible from
  27145. * Ecmascript code; an indirect eval call is possible also from C code.
  27146. * When an indirect eval call is made from C code, there may not be a
  27147. * calling activation at all which needs careful handling.
  27148. */
  27149. DUK_INTERNAL duk_ret_t duk_bi_global_object_eval(duk_context *ctx) {
  27150. duk_hthread *thr = (duk_hthread *) ctx;
  27151. duk_hstring *h;
  27152. duk_activation *act_caller;
  27153. duk_activation *act_eval;
  27154. duk_activation *act;
  27155. duk_hcompiledfunction *func;
  27156. duk_hobject *outer_lex_env;
  27157. duk_hobject *outer_var_env;
  27158. duk_bool_t this_to_global = 1;
  27159. duk_small_uint_t comp_flags;
  27160. duk_int_t level = -2;
  27161. DUK_ASSERT_TOP(ctx, 1);
  27162. DUK_ASSERT(thr->callstack_top >= 1); /* at least this function exists */
  27163. DUK_ASSERT(((thr->callstack + thr->callstack_top - 1)->flags & DUK_ACT_FLAG_DIRECT_EVAL) == 0 || /* indirect eval */
  27164. (thr->callstack_top >= 2)); /* if direct eval, calling activation must exist */
  27165. /*
  27166. * callstack_top - 1 --> this function
  27167. * callstack_top - 2 --> caller (may not exist)
  27168. *
  27169. * If called directly from C, callstack_top might be 1. If calling
  27170. * activation doesn't exist, call must be indirect.
  27171. */
  27172. h = duk_get_hstring(ctx, 0);
  27173. if (!h) {
  27174. return 1; /* return arg as-is */
  27175. }
  27176. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  27177. /* NOTE: level is used only by the debugger and should never be present
  27178. * for an Ecmascript eval().
  27179. */
  27180. level = -2; /* by default, use caller's environment */
  27181. if (duk_get_top(ctx) >= 2 && duk_is_number(ctx, 1)) {
  27182. level = duk_get_int(ctx, 1);
  27183. }
  27184. DUK_ASSERT(level <= -2);
  27185. #endif
  27186. /* [ source ] */
  27187. comp_flags = DUK_JS_COMPILE_FLAG_EVAL;
  27188. act_eval = thr->callstack + thr->callstack_top - 1; /* this function */
  27189. if (thr->callstack_top >= (duk_size_t) -level) {
  27190. /* Have a calling activation, check for direct eval (otherwise
  27191. * assume indirect eval.
  27192. */
  27193. act_caller = thr->callstack + thr->callstack_top + level; /* caller */
  27194. if ((act_caller->flags & DUK_ACT_FLAG_STRICT) &&
  27195. (act_eval->flags & DUK_ACT_FLAG_DIRECT_EVAL)) {
  27196. /* Only direct eval inherits strictness from calling code
  27197. * (E5.1 Section 10.1.1).
  27198. */
  27199. comp_flags |= DUK_JS_COMPILE_FLAG_STRICT;
  27200. }
  27201. } else {
  27202. DUK_ASSERT((act_eval->flags & DUK_ACT_FLAG_DIRECT_EVAL) == 0);
  27203. }
  27204. act_caller = NULL; /* avoid dereference after potential callstack realloc */
  27205. act_eval = NULL;
  27206. duk_push_hstring_stridx(ctx, DUK_STRIDX_INPUT); /* XXX: copy from caller? */
  27207. duk_js_compile(thr,
  27208. (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h),
  27209. (duk_size_t) DUK_HSTRING_GET_BYTELEN(h),
  27210. comp_flags);
  27211. func = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1);
  27212. DUK_ASSERT(func != NULL);
  27213. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) func));
  27214. /* [ source template ] */
  27215. /* E5 Section 10.4.2 */
  27216. DUK_ASSERT(thr->callstack_top >= 1);
  27217. act = thr->callstack + thr->callstack_top - 1; /* this function */
  27218. if (act->flags & DUK_ACT_FLAG_DIRECT_EVAL) {
  27219. DUK_ASSERT(thr->callstack_top >= 2);
  27220. act = thr->callstack + thr->callstack_top + level; /* caller */
  27221. if (act->lex_env == NULL) {
  27222. DUK_ASSERT(act->var_env == NULL);
  27223. DUK_DDD(DUK_DDDPRINT("delayed environment initialization"));
  27224. /* this may have side effects, so re-lookup act */
  27225. duk_js_init_activation_environment_records_delayed(thr, act);
  27226. act = thr->callstack + thr->callstack_top + level;
  27227. }
  27228. DUK_ASSERT(act->lex_env != NULL);
  27229. DUK_ASSERT(act->var_env != NULL);
  27230. this_to_global = 0;
  27231. if (DUK_HOBJECT_HAS_STRICT((duk_hobject *) func)) {
  27232. duk_hobject *new_env;
  27233. duk_hobject *act_lex_env;
  27234. DUK_DDD(DUK_DDDPRINT("direct eval call to a strict function -> "
  27235. "var_env and lex_env to a fresh env, "
  27236. "this_binding to caller's this_binding"));
  27237. act = thr->callstack + thr->callstack_top + level; /* caller */
  27238. act_lex_env = act->lex_env;
  27239. act = NULL; /* invalidated */
  27240. (void) duk_push_object_helper_proto(ctx,
  27241. DUK_HOBJECT_FLAG_EXTENSIBLE |
  27242. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV),
  27243. act_lex_env);
  27244. new_env = duk_require_hobject(ctx, -1);
  27245. DUK_ASSERT(new_env != NULL);
  27246. DUK_DDD(DUK_DDDPRINT("new_env allocated: %!iO",
  27247. (duk_heaphdr *) new_env));
  27248. outer_lex_env = new_env;
  27249. outer_var_env = new_env;
  27250. duk_insert(ctx, 0); /* stash to bottom of value stack to keep new_env reachable */
  27251. /* compiler's responsibility */
  27252. DUK_ASSERT(DUK_HOBJECT_HAS_NEWENV((duk_hobject *) func));
  27253. } else {
  27254. DUK_DDD(DUK_DDDPRINT("direct eval call to a non-strict function -> "
  27255. "var_env and lex_env to caller's envs, "
  27256. "this_binding to caller's this_binding"));
  27257. outer_lex_env = act->lex_env;
  27258. outer_var_env = act->var_env;
  27259. /* compiler's responsibility */
  27260. DUK_ASSERT(!DUK_HOBJECT_HAS_NEWENV((duk_hobject *) func));
  27261. }
  27262. } else {
  27263. DUK_DDD(DUK_DDDPRINT("indirect eval call -> var_env and lex_env to "
  27264. "global object, this_binding to global object"));
  27265. this_to_global = 1;
  27266. outer_lex_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  27267. outer_var_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  27268. }
  27269. act = NULL;
  27270. duk_js_push_closure(thr, func, outer_var_env, outer_lex_env);
  27271. /* [ source template closure ] */
  27272. if (this_to_global) {
  27273. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
  27274. duk_push_hobject_bidx(ctx, DUK_BIDX_GLOBAL);
  27275. } else {
  27276. duk_tval *tv;
  27277. DUK_ASSERT(thr->callstack_top >= 2);
  27278. act = thr->callstack + thr->callstack_top + level; /* caller */
  27279. tv = thr->valstack + act->idx_bottom - 1; /* this is just beneath bottom */
  27280. DUK_ASSERT(tv >= thr->valstack);
  27281. duk_push_tval(ctx, tv);
  27282. }
  27283. DUK_DDD(DUK_DDDPRINT("eval -> lex_env=%!iO, var_env=%!iO, this_binding=%!T",
  27284. (duk_heaphdr *) outer_lex_env,
  27285. (duk_heaphdr *) outer_var_env,
  27286. (duk_tval *) duk_get_tval(ctx, -1)));
  27287. /* [ source template closure this ] */
  27288. duk_call_method(ctx, 0);
  27289. /* [ source template result ] */
  27290. return 1;
  27291. }
  27292. /*
  27293. * Parsing of ints and floats
  27294. */
  27295. DUK_INTERNAL duk_ret_t duk_bi_global_object_parse_int(duk_context *ctx) {
  27296. duk_int32_t radix;
  27297. duk_small_uint_t s2n_flags;
  27298. DUK_ASSERT_TOP(ctx, 2);
  27299. duk_to_string(ctx, 0);
  27300. radix = duk_to_int32(ctx, 1);
  27301. s2n_flags = DUK_S2N_FLAG_TRIM_WHITE |
  27302. DUK_S2N_FLAG_ALLOW_GARBAGE |
  27303. DUK_S2N_FLAG_ALLOW_PLUS |
  27304. DUK_S2N_FLAG_ALLOW_MINUS |
  27305. DUK_S2N_FLAG_ALLOW_LEADING_ZERO |
  27306. DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT;
  27307. /* Specification stripPrefix maps to DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT.
  27308. *
  27309. * Don't autodetect octals (from leading zeroes), require user code to
  27310. * provide an explicit radix 8 for parsing octal. See write-up from Mozilla:
  27311. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#ECMAScript_5_Removes_Octal_Interpretation
  27312. */
  27313. if (radix != 0) {
  27314. if (radix < 2 || radix > 36) {
  27315. goto ret_nan;
  27316. }
  27317. if (radix != 16) {
  27318. s2n_flags &= ~DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT;
  27319. }
  27320. } else {
  27321. radix = 10;
  27322. }
  27323. duk_dup(ctx, 0);
  27324. duk_numconv_parse(ctx, radix, s2n_flags);
  27325. return 1;
  27326. ret_nan:
  27327. duk_push_nan(ctx);
  27328. return 1;
  27329. }
  27330. DUK_INTERNAL duk_ret_t duk_bi_global_object_parse_float(duk_context *ctx) {
  27331. duk_small_uint_t s2n_flags;
  27332. duk_int32_t radix;
  27333. DUK_ASSERT_TOP(ctx, 1);
  27334. duk_to_string(ctx, 0);
  27335. radix = 10;
  27336. /* XXX: check flags */
  27337. s2n_flags = DUK_S2N_FLAG_TRIM_WHITE |
  27338. DUK_S2N_FLAG_ALLOW_EXP |
  27339. DUK_S2N_FLAG_ALLOW_GARBAGE |
  27340. DUK_S2N_FLAG_ALLOW_PLUS |
  27341. DUK_S2N_FLAG_ALLOW_MINUS |
  27342. DUK_S2N_FLAG_ALLOW_INF |
  27343. DUK_S2N_FLAG_ALLOW_FRAC |
  27344. DUK_S2N_FLAG_ALLOW_NAKED_FRAC |
  27345. DUK_S2N_FLAG_ALLOW_EMPTY_FRAC |
  27346. DUK_S2N_FLAG_ALLOW_LEADING_ZERO;
  27347. duk_numconv_parse(ctx, radix, s2n_flags);
  27348. return 1;
  27349. }
  27350. /*
  27351. * Number checkers
  27352. */
  27353. DUK_INTERNAL duk_ret_t duk_bi_global_object_is_nan(duk_context *ctx) {
  27354. duk_double_t d = duk_to_number(ctx, 0);
  27355. duk_push_boolean(ctx, DUK_ISNAN(d));
  27356. return 1;
  27357. }
  27358. DUK_INTERNAL duk_ret_t duk_bi_global_object_is_finite(duk_context *ctx) {
  27359. duk_double_t d = duk_to_number(ctx, 0);
  27360. duk_push_boolean(ctx, DUK_ISFINITE(d));
  27361. return 1;
  27362. }
  27363. /*
  27364. * URI handling
  27365. */
  27366. DUK_INTERNAL duk_ret_t duk_bi_global_object_decode_uri(duk_context *ctx) {
  27367. return duk__transform_helper(ctx, duk__transform_callback_decode_uri, (const void *) duk__decode_uri_reserved_table);
  27368. }
  27369. DUK_INTERNAL duk_ret_t duk_bi_global_object_decode_uri_component(duk_context *ctx) {
  27370. return duk__transform_helper(ctx, duk__transform_callback_decode_uri, (const void *) duk__decode_uri_component_reserved_table);
  27371. }
  27372. DUK_INTERNAL duk_ret_t duk_bi_global_object_encode_uri(duk_context *ctx) {
  27373. return duk__transform_helper(ctx, duk__transform_callback_encode_uri, (const void *) duk__encode_uriunescaped_table);
  27374. }
  27375. DUK_INTERNAL duk_ret_t duk_bi_global_object_encode_uri_component(duk_context *ctx) {
  27376. return duk__transform_helper(ctx, duk__transform_callback_encode_uri, (const void *) duk__encode_uricomponent_unescaped_table);
  27377. }
  27378. #ifdef DUK_USE_SECTION_B
  27379. DUK_INTERNAL duk_ret_t duk_bi_global_object_escape(duk_context *ctx) {
  27380. return duk__transform_helper(ctx, duk__transform_callback_escape, (const void *) NULL);
  27381. }
  27382. DUK_INTERNAL duk_ret_t duk_bi_global_object_unescape(duk_context *ctx) {
  27383. return duk__transform_helper(ctx, duk__transform_callback_unescape, (const void *) NULL);
  27384. }
  27385. #else /* DUK_USE_SECTION_B */
  27386. DUK_INTERNAL duk_ret_t duk_bi_global_object_escape(duk_context *ctx) {
  27387. DUK_UNREF(ctx);
  27388. return DUK_RET_UNSUPPORTED_ERROR;
  27389. }
  27390. DUK_INTERNAL duk_ret_t duk_bi_global_object_unescape(duk_context *ctx) {
  27391. DUK_UNREF(ctx);
  27392. return DUK_RET_UNSUPPORTED_ERROR;
  27393. }
  27394. #endif /* DUK_USE_SECTION_B */
  27395. #if defined(DUK_USE_BROWSER_LIKE) && (defined(DUK_USE_FILE_IO) || defined(DUK_USE_DEBUGGER_SUPPORT))
  27396. DUK_INTERNAL duk_ret_t duk_bi_global_object_print_helper(duk_context *ctx) {
  27397. duk_hthread *thr = (duk_hthread *) ctx;
  27398. duk_int_t magic;
  27399. duk_idx_t nargs;
  27400. const duk_uint8_t *buf;
  27401. duk_size_t sz_buf;
  27402. const char nl = (const char) DUK_ASC_LF;
  27403. #ifndef DUK_USE_PREFER_SIZE
  27404. duk_uint8_t buf_stack[256];
  27405. #endif
  27406. #ifdef DUK_USE_FILE_IO
  27407. duk_file *f_out;
  27408. #endif
  27409. DUK_UNREF(thr);
  27410. magic = duk_get_current_magic(ctx);
  27411. DUK_UNREF(magic);
  27412. nargs = duk_get_top(ctx);
  27413. /* If argument count is 1 and first argument is a buffer, write the buffer
  27414. * as raw data into the file without a newline; this allows exact control
  27415. * over stdout/stderr without an additional entrypoint (useful for now).
  27416. *
  27417. * Otherwise current print/alert semantics are to ToString() coerce
  27418. * arguments, join them with a single space, and append a newline.
  27419. */
  27420. if (nargs == 1 && duk_is_buffer(ctx, 0)) {
  27421. buf = (const duk_uint8_t *) duk_get_buffer(ctx, 0, &sz_buf);
  27422. DUK_ASSERT(buf != NULL);
  27423. } else if (nargs > 0) {
  27424. #ifdef DUK_USE_PREFER_SIZE
  27425. /* Compact but lots of churn. */
  27426. duk_push_hstring_stridx(thr, DUK_STRIDX_SPACE);
  27427. duk_insert(ctx, 0);
  27428. duk_join(ctx, nargs);
  27429. duk_push_string(thr, "\n");
  27430. duk_concat(ctx, 2);
  27431. buf = (const duk_uint8_t *) duk_get_lstring(ctx, -1, &sz_buf);
  27432. DUK_ASSERT(buf != NULL);
  27433. #else /* DUK_USE_PREFER_SIZE */
  27434. /* Higher footprint, less churn. */
  27435. duk_idx_t i;
  27436. duk_size_t sz_str;
  27437. const duk_uint8_t *p_str;
  27438. duk_uint8_t *p;
  27439. sz_buf = (duk_size_t) nargs; /* spaces (nargs - 1) + newline */
  27440. for (i = 0; i < nargs; i++) {
  27441. (void) duk_to_lstring(ctx, i, &sz_str);
  27442. sz_buf += sz_str;
  27443. }
  27444. if (sz_buf <= sizeof(buf_stack)) {
  27445. p = (duk_uint8_t *) buf_stack;
  27446. } else {
  27447. p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, sz_buf);
  27448. DUK_ASSERT(p != NULL);
  27449. }
  27450. buf = (const duk_uint8_t *) p;
  27451. for (i = 0; i < nargs; i++) {
  27452. p_str = (const duk_uint8_t *) duk_get_lstring(ctx, i, &sz_str);
  27453. DUK_ASSERT(p_str != NULL);
  27454. DUK_MEMCPY((void *) p, (const void *) p_str, sz_str);
  27455. p += sz_str;
  27456. *p++ = (duk_uint8_t) (i == nargs - 1 ? DUK_ASC_LF : DUK_ASC_SPACE);
  27457. }
  27458. DUK_ASSERT((const duk_uint8_t *) p == buf + sz_buf);
  27459. #endif /* DUK_USE_PREFER_SIZE */
  27460. } else {
  27461. buf = (const duk_uint8_t *) &nl;
  27462. sz_buf = 1;
  27463. }
  27464. /* 'buf' contains the string to write, 'sz_buf' contains the length
  27465. * (which may be zero).
  27466. */
  27467. DUK_ASSERT(buf != NULL);
  27468. if (sz_buf == 0) {
  27469. return 0;
  27470. }
  27471. #ifdef DUK_USE_FILE_IO
  27472. f_out = (magic ? DUK_STDERR : DUK_STDOUT);
  27473. DUK_FWRITE((const void *) buf, 1, (size_t) sz_buf, f_out);
  27474. DUK_FFLUSH(f_out);
  27475. #endif
  27476. #if defined(DUK_USE_DEBUGGER_SUPPORT) && defined(DUK_USE_DEBUGGER_FWD_PRINTALERT)
  27477. if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) {
  27478. duk_debug_write_notify(thr, magic ? DUK_DBG_CMD_ALERT : DUK_DBG_CMD_PRINT);
  27479. duk_debug_write_string(thr, (const char *) buf, sz_buf);
  27480. duk_debug_write_eom(thr);
  27481. }
  27482. #endif
  27483. return 0;
  27484. }
  27485. #elif defined(DUK_USE_BROWSER_LIKE) /* print provider */
  27486. DUK_INTERNAL duk_ret_t duk_bi_global_object_print_helper(duk_context *ctx) {
  27487. DUK_UNREF(ctx);
  27488. return 0;
  27489. }
  27490. #else /* print provider */
  27491. DUK_INTERNAL duk_ret_t duk_bi_global_object_print_helper(duk_context *ctx) {
  27492. DUK_UNREF(ctx);
  27493. return DUK_RET_UNSUPPORTED_ERROR;
  27494. }
  27495. #endif /* print provider */
  27496. /*
  27497. * CommonJS require() and modules support
  27498. */
  27499. #if defined(DUK_USE_COMMONJS_MODULES)
  27500. DUK_LOCAL void duk__bi_global_resolve_module_id(duk_context *ctx, const char *req_id, const char *mod_id) {
  27501. duk_hthread *thr = (duk_hthread *) ctx;
  27502. duk_size_t mod_id_len;
  27503. duk_size_t req_id_len;
  27504. duk_uint8_t buf_in[DUK_BI_COMMONJS_MODULE_ID_LIMIT];
  27505. duk_uint8_t buf_out[DUK_BI_COMMONJS_MODULE_ID_LIMIT];
  27506. duk_uint8_t *p;
  27507. duk_uint8_t *q;
  27508. DUK_ASSERT(req_id != NULL);
  27509. /* mod_id may be NULL */
  27510. DUK_ASSERT(sizeof(buf_out) >= sizeof(buf_in)); /* bound checking requires this */
  27511. /*
  27512. * A few notes on the algorithm:
  27513. *
  27514. * - Terms are not allowed to begin with a period unless the term
  27515. * is either '.' or '..'. This simplifies implementation (and
  27516. * is within CommonJS modules specification).
  27517. *
  27518. * - There are few output bound checks here. This is on purpose:
  27519. * we check the input length and rely on the output never being
  27520. * longer than the input, so we cannot run out of output space.
  27521. *
  27522. * - Non-ASCII characters are processed as individual bytes and
  27523. * need no special treatment. However, U+0000 terminates the
  27524. * algorithm; this is not an issue because U+0000 is not a
  27525. * desirable term character anyway.
  27526. */
  27527. /*
  27528. * Set up the resolution input which is the requested ID directly
  27529. * (if absolute or no current module path) or with current module
  27530. * ID prepended (if relative and current module path exists).
  27531. *
  27532. * Suppose current module is 'foo/bar' and relative path is './quux'.
  27533. * The 'bar' component must be replaced so the initial input here is
  27534. * 'foo/bar/.././quux'.
  27535. */
  27536. req_id_len = DUK_STRLEN(req_id);
  27537. if (mod_id != NULL && req_id[0] == '.') {
  27538. mod_id_len = DUK_STRLEN(mod_id);
  27539. if (mod_id_len + 4 + req_id_len + 1 >= sizeof(buf_in)) {
  27540. DUK_DD(DUK_DDPRINT("resolve error: current and requested module ID don't fit into resolve input buffer"));
  27541. goto resolve_error;
  27542. }
  27543. (void) DUK_SNPRINTF((char *) buf_in, sizeof(buf_in), "%s/../%s", (const char *) mod_id, (const char *) req_id);
  27544. } else {
  27545. if (req_id_len + 1 >= sizeof(buf_in)) {
  27546. DUK_DD(DUK_DDPRINT("resolve error: requested module ID doesn't fit into resolve input buffer"));
  27547. goto resolve_error;
  27548. }
  27549. (void) DUK_SNPRINTF((char *) buf_in, sizeof(buf_in), "%s", (const char *) req_id);
  27550. }
  27551. buf_in[sizeof(buf_in) - 1] = (duk_uint8_t) 0;
  27552. DUK_DDD(DUK_DDDPRINT("input module id: '%s'", (const char *) buf_in));
  27553. /*
  27554. * Resolution loop. At the top of the loop we're expecting a valid
  27555. * term: '.', '..', or a non-empty identifier not starting with a period.
  27556. */
  27557. p = buf_in;
  27558. q = buf_out;
  27559. for (;;) {
  27560. duk_uint_fast8_t c;
  27561. /* Here 'p' always points to the start of a term. */
  27562. DUK_DDD(DUK_DDDPRINT("resolve loop top: p -> '%s', q=%p, buf_out=%p",
  27563. (const char *) p, (void *) q, (void *) buf_out));
  27564. c = *p++;
  27565. if (DUK_UNLIKELY(c == 0)) {
  27566. DUK_DD(DUK_DDPRINT("resolve error: requested ID must end with a non-empty term"));
  27567. goto resolve_error;
  27568. } else if (DUK_UNLIKELY(c == '.')) {
  27569. c = *p++;
  27570. if (c == '/') {
  27571. /* Term was '.' and is eaten entirely (including dup slashes). */
  27572. goto eat_dup_slashes;
  27573. }
  27574. if (c == '.' && *p == '/') {
  27575. /* Term was '..', backtrack resolved name by one component.
  27576. * q[-1] = previous slash (or beyond start of buffer)
  27577. * q[-2] = last char of previous component (or beyond start of buffer)
  27578. */
  27579. p++; /* eat (first) input slash */
  27580. DUK_ASSERT(q >= buf_out);
  27581. if (q == buf_out) {
  27582. DUK_DD(DUK_DDPRINT("resolve error: term was '..' but nothing to backtrack"));
  27583. goto resolve_error;
  27584. }
  27585. DUK_ASSERT(*(q - 1) == '/');
  27586. q--; /* backtrack to last output slash */
  27587. for (;;) {
  27588. /* Backtrack to previous slash or start of buffer. */
  27589. DUK_ASSERT(q >= buf_out);
  27590. if (q == buf_out) {
  27591. break;
  27592. }
  27593. if (*(q - 1) == '/') {
  27594. break;
  27595. }
  27596. q--;
  27597. }
  27598. goto eat_dup_slashes;
  27599. }
  27600. DUK_DD(DUK_DDPRINT("resolve error: term begins with '.' but is not '.' or '..' (not allowed now)"));
  27601. goto resolve_error;
  27602. } else if (DUK_UNLIKELY(c == '/')) {
  27603. /* e.g. require('/foo'), empty terms not allowed */
  27604. DUK_DD(DUK_DDPRINT("resolve error: empty term (not allowed now)"));
  27605. goto resolve_error;
  27606. } else {
  27607. for (;;) {
  27608. /* Copy term name until end or '/'. */
  27609. *q++ = c;
  27610. c = *p++;
  27611. if (DUK_UNLIKELY(c == 0)) {
  27612. goto loop_done;
  27613. } else if (DUK_UNLIKELY(c == '/')) {
  27614. *q++ = '/';
  27615. break;
  27616. } else {
  27617. /* write on next loop */
  27618. }
  27619. }
  27620. }
  27621. eat_dup_slashes:
  27622. for (;;) {
  27623. /* eat dup slashes */
  27624. c = *p;
  27625. if (DUK_LIKELY(c != '/')) {
  27626. break;
  27627. }
  27628. p++;
  27629. }
  27630. }
  27631. loop_done:
  27632. duk_push_lstring(ctx, (const char *) buf_out, (size_t) (q - buf_out));
  27633. return;
  27634. resolve_error:
  27635. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "cannot resolve module id: %s", (const char *) req_id);
  27636. }
  27637. #endif /* DUK_USE_COMMONJS_MODULES */
  27638. #if defined(DUK_USE_COMMONJS_MODULES)
  27639. DUK_INTERNAL duk_ret_t duk_bi_global_object_require(duk_context *ctx) {
  27640. const char *str_req_id; /* requested identifier */
  27641. const char *str_mod_id; /* require.id of current module */
  27642. duk_int_t pcall_rc;
  27643. /* NOTE: we try to minimize code size by avoiding unnecessary pops,
  27644. * so the stack looks a bit cluttered in this function. DUK_ASSERT_TOP()
  27645. * assertions are used to ensure stack configuration is correct at each
  27646. * step.
  27647. */
  27648. /*
  27649. * Resolve module identifier into canonical absolute form.
  27650. */
  27651. str_req_id = duk_require_string(ctx, 0);
  27652. duk_push_current_function(ctx);
  27653. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_ID);
  27654. str_mod_id = duk_get_string(ctx, 2); /* ignore non-strings */
  27655. DUK_DDD(DUK_DDDPRINT("resolve module id: requested=%!T, currentmodule=%!T",
  27656. (duk_tval *) duk_get_tval(ctx, 0),
  27657. (duk_tval *) duk_get_tval(ctx, 2)));
  27658. duk__bi_global_resolve_module_id(ctx, str_req_id, str_mod_id);
  27659. str_req_id = NULL;
  27660. str_mod_id = NULL;
  27661. DUK_DDD(DUK_DDDPRINT("resolved module id: requested=%!T, currentmodule=%!T, result=%!T",
  27662. (duk_tval *) duk_get_tval(ctx, 0),
  27663. (duk_tval *) duk_get_tval(ctx, 2),
  27664. (duk_tval *) duk_get_tval(ctx, 3)));
  27665. /* [ requested_id require require.id resolved_id ] */
  27666. DUK_ASSERT_TOP(ctx, 4);
  27667. /*
  27668. * Cached module check.
  27669. *
  27670. * If module has been loaded or its loading has already begun without
  27671. * finishing, return the same cached value ('exports'). The value is
  27672. * registered when module load starts so that circular references can
  27673. * be supported to some extent.
  27674. */
  27675. /* [ requested_id require require.id resolved_id ] */
  27676. DUK_ASSERT_TOP(ctx, 4);
  27677. duk_push_hobject_bidx(ctx, DUK_BIDX_DUKTAPE);
  27678. duk_get_prop_stridx(ctx, 4, DUK_STRIDX_MOD_LOADED); /* Duktape.modLoaded */
  27679. (void) duk_require_hobject(ctx, 5);
  27680. /* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded ] */
  27681. DUK_ASSERT_TOP(ctx, 6);
  27682. duk_dup(ctx, 3);
  27683. if (duk_get_prop(ctx, 5)) {
  27684. /* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded Duktape.modLoaded[id] ] */
  27685. DUK_DD(DUK_DDPRINT("module already loaded: %!T",
  27686. (duk_tval *) duk_get_tval(ctx, 3)));
  27687. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_EXPORTS); /* return module.exports */
  27688. return 1;
  27689. }
  27690. /* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined ] */
  27691. DUK_ASSERT_TOP(ctx, 7);
  27692. /*
  27693. * Module not loaded (and loading not started previously).
  27694. *
  27695. * Create a new require() function with 'id' set to resolved ID
  27696. * of module being loaded. Also create 'exports' and 'module'
  27697. * tables but don't register exports to the loaded table yet.
  27698. * We don't want to do that unless the user module search callbacks
  27699. * succeeds in finding the module.
  27700. */
  27701. DUK_DD(DUK_DDPRINT("module not yet loaded: %!T",
  27702. (duk_tval *) duk_get_tval(ctx, 3)));
  27703. /* Fresh require: require.id is left configurable (but not writable)
  27704. * so that is not easy to accidentally tweak it, but it can still be
  27705. * done with Object.defineProperty().
  27706. *
  27707. * XXX: require.id could also be just made non-configurable, as there
  27708. * is no practical reason to touch it.
  27709. */
  27710. duk_push_c_function(ctx, duk_bi_global_object_require, 1 /*nargs*/);
  27711. duk_dup(ctx, 3);
  27712. duk_xdef_prop_stridx(ctx, 7, DUK_STRIDX_ID, DUK_PROPDESC_FLAGS_C); /* a fresh require() with require.id = resolved target module id */
  27713. /* Module table:
  27714. * - module.exports: initial exports table (may be replaced by user)
  27715. * - module.id is non-writable and non-configurable, as the CommonJS
  27716. * spec suggests this if possible.
  27717. */
  27718. duk_push_object(ctx); /* exports */
  27719. duk_push_object(ctx); /* module */
  27720. duk_dup(ctx, -2);
  27721. duk_xdef_prop_stridx(ctx, 9, DUK_STRIDX_EXPORTS, DUK_PROPDESC_FLAGS_WC); /* module.exports = exports */
  27722. duk_dup(ctx, 3); /* resolved id: require(id) must return this same module */
  27723. duk_xdef_prop_stridx(ctx, 9, DUK_STRIDX_ID, DUK_PROPDESC_FLAGS_NONE); /* module.id = resolved_id */
  27724. duk_compact(ctx, 9); /* module table remains registered to modLoaded, minimize its size */
  27725. /* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined fresh_require exports module ] */
  27726. DUK_ASSERT_TOP(ctx, 10);
  27727. /* Register the module table early to modLoaded[] so that we can
  27728. * support circular references even in modSearch(). If an error
  27729. * is thrown, we'll delete the reference.
  27730. */
  27731. duk_dup(ctx, 3);
  27732. duk_dup(ctx, 9);
  27733. duk_put_prop(ctx, 5); /* Duktape.modLoaded[resolved_id] = module */
  27734. /*
  27735. * Call user provided module search function and build the wrapped
  27736. * module source code (if necessary). The module search function
  27737. * can be used to implement pure Ecmacsript, pure C, and mixed
  27738. * Ecmascript/C modules.
  27739. *
  27740. * The module search function can operate on the exports table directly
  27741. * (e.g. DLL code can register values to it). It can also return a
  27742. * string which is interpreted as module source code (if a non-string
  27743. * is returned the module is assumed to be a pure C one). If a module
  27744. * cannot be found, an error must be thrown by the user callback.
  27745. *
  27746. * Because Duktape.modLoaded[] already contains the module being
  27747. * loaded, circular references for C modules should also work
  27748. * (although expected to be quite rare).
  27749. */
  27750. duk_push_string(ctx, "(function(require,exports,module){");
  27751. /* Duktape.modSearch(resolved_id, fresh_require, exports, module). */
  27752. duk_get_prop_stridx(ctx, 4, DUK_STRIDX_MOD_SEARCH); /* Duktape.modSearch */
  27753. duk_dup(ctx, 3);
  27754. duk_dup(ctx, 7);
  27755. duk_dup(ctx, 8);
  27756. duk_dup(ctx, 9); /* [ ... Duktape.modSearch resolved_id fresh_require exports module ] */
  27757. pcall_rc = duk_pcall(ctx, 4 /*nargs*/); /* -> [ ... source ] */
  27758. DUK_ASSERT_TOP(ctx, 12);
  27759. if (pcall_rc != DUK_EXEC_SUCCESS) {
  27760. /* Delete entry in Duktape.modLoaded[] and rethrow. */
  27761. goto delete_rethrow;
  27762. }
  27763. /* If user callback did not return source code, module loading
  27764. * is finished (user callback initialized exports table directly).
  27765. */
  27766. if (!duk_is_string(ctx, 11)) {
  27767. /* User callback did not return source code, so module loading
  27768. * is finished: just update modLoaded with final module.exports
  27769. * and we're done.
  27770. */
  27771. goto return_exports;
  27772. }
  27773. /* Finish the wrapped module source. Force resolved module ID as the
  27774. * fileName so it gets set for functions defined within a module. This
  27775. * also ensures loggers created within the module get the module ID as
  27776. * their default logger name.
  27777. */
  27778. duk_push_string(ctx, "})");
  27779. duk_concat(ctx, 3);
  27780. duk_dup(ctx, 3); /* resolved module ID for fileName */
  27781. duk_eval_raw(ctx, NULL, 0, DUK_COMPILE_EVAL);
  27782. /* XXX: The module wrapper function is currently anonymous and is shown
  27783. * in stack traces. It would be nice to force it to match the module
  27784. * name (perhaps just the cleaned up last term). At the moment 'name'
  27785. * is write protected so we can't change it directly. Note that we must
  27786. * not introduce an actual name binding into the function scope (which
  27787. * is usually the case with a named function) because it would affect
  27788. * the scope seen by the module and shadow accesses to globals of the
  27789. * same name.
  27790. */
  27791. /*
  27792. * Call the wrapped module function.
  27793. *
  27794. * Use a protected call so that we can update Duktape.modLoaded[resolved_id]
  27795. * even if the module throws an error.
  27796. */
  27797. /* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined fresh_require exports module mod_func ] */
  27798. DUK_ASSERT_TOP(ctx, 11);
  27799. duk_dup(ctx, 8); /* exports (this binding) */
  27800. duk_dup(ctx, 7); /* fresh require (argument) */
  27801. duk_get_prop_stridx(ctx, 9, DUK_STRIDX_EXPORTS); /* relookup exports from module.exports in case it was changed by modSearch */
  27802. duk_dup(ctx, 9); /* module (argument) */
  27803. /* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined fresh_require exports module mod_func exports fresh_require exports module ] */
  27804. DUK_ASSERT_TOP(ctx, 15);
  27805. pcall_rc = duk_pcall_method(ctx, 3 /*nargs*/);
  27806. if (pcall_rc != DUK_EXEC_SUCCESS) {
  27807. /* Module loading failed. Node.js will forget the module
  27808. * registration so that another require() will try to load
  27809. * the module again. Mimic that behavior.
  27810. */
  27811. goto delete_rethrow;
  27812. }
  27813. /* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined fresh_require exports module result(ignored) ] */
  27814. DUK_ASSERT_TOP(ctx, 11);
  27815. /* fall through */
  27816. return_exports:
  27817. duk_get_prop_stridx(ctx, 9, DUK_STRIDX_EXPORTS);
  27818. return 1; /* return module.exports */
  27819. delete_rethrow:
  27820. duk_dup(ctx, 3);
  27821. duk_del_prop(ctx, 5); /* delete Duktape.modLoaded[resolved_id] */
  27822. duk_throw(ctx); /* rethrow original error */
  27823. return 0; /* not reachable */
  27824. }
  27825. #else
  27826. DUK_INTERNAL duk_ret_t duk_bi_global_object_require(duk_context *ctx) {
  27827. DUK_UNREF(ctx);
  27828. return DUK_RET_UNSUPPORTED_ERROR;
  27829. }
  27830. #endif /* DUK_USE_COMMONJS_MODULES */
  27831. #line 1 "duk_bi_json.c"
  27832. /*
  27833. * JSON built-ins.
  27834. *
  27835. * See doc/json.rst.
  27836. *
  27837. * Codepoints are handled as duk_uint_fast32_t to ensure that the full
  27838. * unsigned 32-bit range is supported. This matters to e.g. JX.
  27839. *
  27840. * Input parsing doesn't do an explicit end-of-input check at all. This is
  27841. * safe: input string data is always NUL-terminated (0x00) and valid JSON
  27842. * inputs never contain plain NUL characters, so that as long as syntax checks
  27843. * are correct, we'll never read past the NUL. This approach reduces code size
  27844. * and improves parsing performance, but it's critical that syntax checks are
  27845. * indeed correct!
  27846. */
  27847. /* include removed: duk_internal.h */
  27848. /*
  27849. * Local defines and forward declarations.
  27850. */
  27851. #define DUK__JSON_DECSTR_BUFSIZE 128
  27852. #define DUK__JSON_DECSTR_CHUNKSIZE 64
  27853. #define DUK__JSON_ENCSTR_CHUNKSIZE 64
  27854. #define DUK__JSON_STRINGIFY_BUFSIZE 128
  27855. #define DUK__JSON_MAX_ESC_LEN 10 /* '\Udeadbeef' */
  27856. DUK_LOCAL_DECL void duk__dec_syntax_error(duk_json_dec_ctx *js_ctx);
  27857. DUK_LOCAL_DECL void duk__dec_eat_white(duk_json_dec_ctx *js_ctx);
  27858. DUK_LOCAL_DECL duk_uint8_t duk__dec_peek(duk_json_dec_ctx *js_ctx);
  27859. DUK_LOCAL_DECL duk_uint8_t duk__dec_get(duk_json_dec_ctx *js_ctx);
  27860. DUK_LOCAL_DECL duk_uint8_t duk__dec_get_nonwhite(duk_json_dec_ctx *js_ctx);
  27861. DUK_LOCAL_DECL duk_uint_fast32_t duk__dec_decode_hex_escape(duk_json_dec_ctx *js_ctx, duk_small_uint_t n);
  27862. DUK_LOCAL_DECL void duk__dec_req_stridx(duk_json_dec_ctx *js_ctx, duk_small_uint_t stridx);
  27863. DUK_LOCAL_DECL void duk__dec_string(duk_json_dec_ctx *js_ctx);
  27864. #ifdef DUK_USE_JX
  27865. DUK_LOCAL_DECL void duk__dec_plain_string(duk_json_dec_ctx *js_ctx);
  27866. DUK_LOCAL_DECL void duk__dec_pointer(duk_json_dec_ctx *js_ctx);
  27867. DUK_LOCAL_DECL void duk__dec_buffer(duk_json_dec_ctx *js_ctx);
  27868. #endif
  27869. DUK_LOCAL_DECL void duk__dec_number(duk_json_dec_ctx *js_ctx);
  27870. DUK_LOCAL_DECL void duk__dec_objarr_entry(duk_json_dec_ctx *js_ctx);
  27871. DUK_LOCAL_DECL void duk__dec_objarr_exit(duk_json_dec_ctx *js_ctx);
  27872. DUK_LOCAL_DECL void duk__dec_object(duk_json_dec_ctx *js_ctx);
  27873. DUK_LOCAL_DECL void duk__dec_array(duk_json_dec_ctx *js_ctx);
  27874. DUK_LOCAL_DECL void duk__dec_value(duk_json_dec_ctx *js_ctx);
  27875. DUK_LOCAL_DECL void duk__dec_reviver_walk(duk_json_dec_ctx *js_ctx);
  27876. DUK_LOCAL_DECL void duk__emit_1(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch);
  27877. DUK_LOCAL_DECL void duk__emit_2(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch1, duk_uint_fast8_t ch2);
  27878. #if defined(DUK_USE_JSON_STRINGIFY_FASTPATH)
  27879. DUK_LOCAL_DECL void duk__unemit_1(duk_json_enc_ctx *js_ctx);
  27880. #endif
  27881. DUK_LOCAL_DECL void duk__emit_hstring(duk_json_enc_ctx *js_ctx, duk_hstring *h);
  27882. #if defined(DUK_USE_FASTINT)
  27883. DUK_LOCAL_DECL void duk__emit_cstring(duk_json_enc_ctx *js_ctx, const char *p);
  27884. #endif
  27885. DUK_LOCAL_DECL void duk__emit_stridx(duk_json_enc_ctx *js_ctx, duk_small_uint_t stridx);
  27886. 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);
  27887. DUK_LOCAL_DECL duk_bool_t duk__enc_key_quotes_needed(duk_hstring *h_key);
  27888. DUK_LOCAL_DECL void duk__enc_quote_string(duk_json_enc_ctx *js_ctx, duk_hstring *h_str);
  27889. DUK_LOCAL_DECL void duk__enc_objarr_entry(duk_json_enc_ctx *js_ctx, duk_hstring **h_stepback, duk_hstring **h_indent, duk_idx_t *entry_top);
  27890. DUK_LOCAL_DECL void duk__enc_objarr_exit(duk_json_enc_ctx *js_ctx, duk_hstring **h_stepback, duk_hstring **h_indent, duk_idx_t *entry_top);
  27891. DUK_LOCAL_DECL void duk__enc_object(duk_json_enc_ctx *js_ctx);
  27892. DUK_LOCAL_DECL void duk__enc_array(duk_json_enc_ctx *js_ctx);
  27893. DUK_LOCAL_DECL duk_bool_t duk__enc_value1(duk_json_enc_ctx *js_ctx, duk_idx_t idx_holder);
  27894. DUK_LOCAL_DECL void duk__enc_value2(duk_json_enc_ctx *js_ctx);
  27895. DUK_LOCAL_DECL duk_bool_t duk__enc_allow_into_proplist(duk_tval *tv);
  27896. DUK_LOCAL_DECL void duk__enc_double(duk_json_enc_ctx *js_ctx);
  27897. #if defined(DUK_USE_FASTINT)
  27898. DUK_LOCAL_DECL void duk__enc_fastint_tval(duk_json_enc_ctx *js_ctx, duk_tval *tv);
  27899. #endif
  27900. /*
  27901. * Helper tables
  27902. */
  27903. #if defined(DUK_USE_JSON_QUOTESTRING_FASTPATH)
  27904. DUK_LOCAL const duk_uint8_t duk__json_quotestr_lookup[256] = {
  27905. /* 0x00 ... 0x7f: as is
  27906. * 0x80: escape generically
  27907. * 0x81: slow path
  27908. * 0xa0 ... 0xff: backslash + one char
  27909. */
  27910. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xe2, 0xf4, 0xee, 0x80, 0xe6, 0xf2, 0x80, 0x80,
  27911. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  27912. 0x20, 0x21, 0xa2, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  27913. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  27914. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  27915. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0xdc, 0x5d, 0x5e, 0x5f,
  27916. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  27917. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x81,
  27918. 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
  27919. 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
  27920. 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
  27921. 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
  27922. 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
  27923. 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
  27924. 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
  27925. 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81
  27926. };
  27927. #else /* DUK_USE_JSON_QUOTESTRING_FASTPATH */
  27928. DUK_LOCAL const duk_uint8_t duk__json_quotestr_esc[14] = {
  27929. DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL,
  27930. DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL,
  27931. DUK_ASC_LC_B, DUK_ASC_LC_T, DUK_ASC_LC_N, DUK_ASC_NUL,
  27932. DUK_ASC_LC_F, DUK_ASC_LC_R
  27933. };
  27934. #endif /* DUK_USE_JSON_QUOTESTRING_FASTPATH */
  27935. #if defined(DUK_USE_JSON_DECSTRING_FASTPATH)
  27936. DUK_LOCAL const duk_uint8_t duk__json_decstr_lookup[256] = {
  27937. /* 0x00: slow path
  27938. * other: as is
  27939. */
  27940. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27941. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27942. 0x20, 0x21, 0x00, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  27943. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  27944. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  27945. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x00, 0x5d, 0x5e, 0x5f,
  27946. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  27947. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
  27948. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  27949. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  27950. 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
  27951. 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  27952. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  27953. 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
  27954. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  27955. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
  27956. };
  27957. #endif /* DUK_USE_JSON_DECSTRING_FASTPATH */
  27958. #if defined(DUK_USE_JSON_EATWHITE_FASTPATH)
  27959. DUK_LOCAL const duk_uint8_t duk__json_eatwhite_lookup[256] = {
  27960. /* 0x00: finish (non-white)
  27961. * 0x01: continue
  27962. */
  27963. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00,
  27964. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27965. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27966. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27967. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27968. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27969. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27970. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27971. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27972. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27973. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27974. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27975. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27976. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27977. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27978. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  27979. };
  27980. #endif /* DUK_USE_JSON_EATWHITE_FASTPATH */
  27981. #if defined(DUK_USE_JSON_DECNUMBER_FASTPATH)
  27982. DUK_LOCAL const duk_uint8_t duk__json_decnumber_lookup[256] = {
  27983. /* 0x00: finish (not part of number)
  27984. * 0x01: continue
  27985. */
  27986. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27987. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27988. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00,
  27989. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27990. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27991. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27992. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27993. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27994. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27995. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27996. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27997. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27998. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27999. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  28000. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  28001. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  28002. };
  28003. #endif /* DUK_USE_JSON_DECNUMBER_FASTPATH */
  28004. /*
  28005. * Parsing implementation.
  28006. *
  28007. * JSON lexer is now separate from duk_lexer.c because there are numerous
  28008. * small differences making it difficult to share the lexer.
  28009. *
  28010. * The parser here works with raw bytes directly; this works because all
  28011. * JSON delimiters are ASCII characters. Invalid xUTF-8 encoded values
  28012. * inside strings will be passed on without normalization; this is not a
  28013. * compliance concern because compliant inputs will always be valid
  28014. * CESU-8 encodings.
  28015. */
  28016. DUK_LOCAL void duk__dec_syntax_error(duk_json_dec_ctx *js_ctx) {
  28017. /* Shared handler to minimize parser size. Cause will be
  28018. * hidden, unfortunately, but we'll have an offset which
  28019. * is often quite enough.
  28020. */
  28021. DUK_ERROR(js_ctx->thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_FMT_INVALID_JSON,
  28022. (long) (js_ctx->p - js_ctx->p_start));
  28023. }
  28024. DUK_LOCAL void duk__dec_eat_white(duk_json_dec_ctx *js_ctx) {
  28025. const duk_uint8_t *p;
  28026. duk_uint8_t t;
  28027. p = js_ctx->p;
  28028. for (;;) {
  28029. DUK_ASSERT(p <= js_ctx->p_end);
  28030. t = *p;
  28031. #if defined(DUK_USE_JSON_EATWHITE_FASTPATH)
  28032. /* This fast path is pretty marginal in practice.
  28033. * XXX: candidate for removal.
  28034. */
  28035. DUK_ASSERT(duk__json_eatwhite_lookup[0x00] == 0x00); /* end-of-input breaks */
  28036. if (duk__json_eatwhite_lookup[t] == 0) {
  28037. break;
  28038. }
  28039. #else /* DUK_USE_JSON_EATWHITE_FASTPATH */
  28040. if (!(t == 0x20 || t == 0x0a || t == 0x0d || t == 0x09)) {
  28041. /* NUL also comes here. Comparison order matters, 0x20
  28042. * is most common whitespace.
  28043. */
  28044. break;
  28045. }
  28046. #endif /* DUK_USE_JSON_EATWHITE_FASTPATH */
  28047. p++;
  28048. }
  28049. js_ctx->p = p;
  28050. }
  28051. DUK_LOCAL duk_uint8_t duk__dec_peek(duk_json_dec_ctx *js_ctx) {
  28052. DUK_ASSERT(js_ctx->p <= js_ctx->p_end);
  28053. return *js_ctx->p;
  28054. }
  28055. DUK_LOCAL duk_uint8_t duk__dec_get(duk_json_dec_ctx *js_ctx) {
  28056. DUK_ASSERT(js_ctx->p <= js_ctx->p_end);
  28057. return *js_ctx->p++;
  28058. }
  28059. DUK_LOCAL duk_uint8_t duk__dec_get_nonwhite(duk_json_dec_ctx *js_ctx) {
  28060. duk__dec_eat_white(js_ctx);
  28061. return duk__dec_get(js_ctx);
  28062. }
  28063. /* For JX, expressing the whole unsigned 32-bit range matters. */
  28064. DUK_LOCAL duk_uint_fast32_t duk__dec_decode_hex_escape(duk_json_dec_ctx *js_ctx, duk_small_uint_t n) {
  28065. duk_small_uint_t i;
  28066. duk_uint_fast32_t res = 0;
  28067. duk_uint8_t x;
  28068. duk_small_int_t t;
  28069. for (i = 0; i < n; i++) {
  28070. /* XXX: share helper from lexer; duk_lexer.c / hexval(). */
  28071. x = duk__dec_get(js_ctx);
  28072. DUK_DDD(DUK_DDDPRINT("decode_hex_escape: i=%ld, n=%ld, res=%ld, x=%ld",
  28073. (long) i, (long) n, (long) res, (long) x));
  28074. /* x == 0x00 (EOF) causes syntax_error */
  28075. DUK_ASSERT(duk_hex_dectab[0] == -1);
  28076. t = duk_hex_dectab[x & 0xff];
  28077. if (DUK_LIKELY(t >= 0)) {
  28078. res = (res * 16) + t;
  28079. } else {
  28080. /* catches EOF and invalid digits */
  28081. goto syntax_error;
  28082. }
  28083. }
  28084. DUK_DDD(DUK_DDDPRINT("final hex decoded value: %ld", (long) res));
  28085. return res;
  28086. syntax_error:
  28087. duk__dec_syntax_error(js_ctx);
  28088. DUK_UNREACHABLE();
  28089. return 0;
  28090. }
  28091. DUK_LOCAL void duk__dec_req_stridx(duk_json_dec_ctx *js_ctx, duk_small_uint_t stridx) {
  28092. duk_hstring *h;
  28093. const duk_uint8_t *p;
  28094. duk_uint8_t x, y;
  28095. /* First character has already been eaten and checked by the caller.
  28096. * We can scan until a NUL in stridx string because no built-in strings
  28097. * have internal NULs.
  28098. */
  28099. DUK_ASSERT_DISABLE(stridx >= 0); /* unsigned */
  28100. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  28101. h = DUK_HTHREAD_GET_STRING(js_ctx->thr, stridx);
  28102. DUK_ASSERT(h != NULL);
  28103. p = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h) + 1;
  28104. DUK_ASSERT(*(js_ctx->p - 1) == *(p - 1)); /* first character has been matched */
  28105. for (;;) {
  28106. x = *p;
  28107. if (x == 0) {
  28108. break;
  28109. }
  28110. y = duk__dec_get(js_ctx);
  28111. if (x != y) {
  28112. /* Catches EOF of JSON input. */
  28113. goto syntax_error;
  28114. }
  28115. p++;
  28116. }
  28117. return;
  28118. syntax_error:
  28119. duk__dec_syntax_error(js_ctx);
  28120. DUK_UNREACHABLE();
  28121. }
  28122. DUK_LOCAL duk_small_int_t duk__dec_string_escape(duk_json_dec_ctx *js_ctx, duk_uint8_t **ext_p) {
  28123. duk_uint_fast32_t cp;
  28124. /* EOF (-1) will be cast to an unsigned value first
  28125. * and then re-cast for the switch. In any case, it
  28126. * will match the default case (syntax error).
  28127. */
  28128. cp = (duk_uint_fast32_t) duk__dec_get(js_ctx);
  28129. switch ((int) cp) {
  28130. case DUK_ASC_BACKSLASH: break;
  28131. case DUK_ASC_DOUBLEQUOTE: break;
  28132. case DUK_ASC_SLASH: break;
  28133. case DUK_ASC_LC_T: cp = 0x09; break;
  28134. case DUK_ASC_LC_N: cp = 0x0a; break;
  28135. case DUK_ASC_LC_R: cp = 0x0d; break;
  28136. case DUK_ASC_LC_F: cp = 0x0c; break;
  28137. case DUK_ASC_LC_B: cp = 0x08; break;
  28138. case DUK_ASC_LC_U: {
  28139. cp = duk__dec_decode_hex_escape(js_ctx, 4);
  28140. break;
  28141. }
  28142. #ifdef DUK_USE_JX
  28143. case DUK_ASC_UC_U: {
  28144. if (js_ctx->flag_ext_custom) {
  28145. cp = duk__dec_decode_hex_escape(js_ctx, 8);
  28146. } else {
  28147. return 1; /* syntax error */
  28148. }
  28149. break;
  28150. }
  28151. case DUK_ASC_LC_X: {
  28152. if (js_ctx->flag_ext_custom) {
  28153. cp = duk__dec_decode_hex_escape(js_ctx, 2);
  28154. } else {
  28155. return 1; /* syntax error */
  28156. }
  28157. break;
  28158. }
  28159. #endif /* DUK_USE_JX */
  28160. default:
  28161. /* catches EOF (0x00) */
  28162. return 1; /* syntax error */
  28163. }
  28164. DUK_RAW_WRITE_XUTF8(*ext_p, cp);
  28165. return 0;
  28166. }
  28167. DUK_LOCAL void duk__dec_string(duk_json_dec_ctx *js_ctx) {
  28168. duk_hthread *thr = js_ctx->thr;
  28169. duk_context *ctx = (duk_context *) thr;
  28170. duk_bufwriter_ctx bw_alloc;
  28171. duk_bufwriter_ctx *bw;
  28172. duk_uint8_t *q;
  28173. /* '"' was eaten by caller */
  28174. /* Note that we currently parse -bytes-, not codepoints.
  28175. * All non-ASCII extended UTF-8 will encode to bytes >= 0x80,
  28176. * so they'll simply pass through (valid UTF-8 or not).
  28177. */
  28178. bw = &bw_alloc;
  28179. DUK_BW_INIT_PUSHBUF(js_ctx->thr, bw, DUK__JSON_DECSTR_BUFSIZE);
  28180. q = DUK_BW_GET_PTR(js_ctx->thr, bw);
  28181. #if defined(DUK_USE_JSON_DECSTRING_FASTPATH)
  28182. for (;;) {
  28183. duk_small_uint_t safe;
  28184. duk_uint8_t b, x;
  28185. const duk_uint8_t *p;
  28186. /* Select a safe loop count where no output checks are
  28187. * needed assuming we won't encounter escapes. Input
  28188. * bound checks are not necessary as a NUL (guaranteed)
  28189. * will cause a SyntaxError before we read out of bounds.
  28190. */
  28191. safe = DUK__JSON_DECSTR_CHUNKSIZE;
  28192. /* Ensure space for 1:1 output plus one escape. */
  28193. q = DUK_BW_ENSURE_RAW(js_ctx->thr, bw, safe + DUK_UNICODE_MAX_XUTF8_LENGTH, q);
  28194. p = js_ctx->p; /* temp copy, write back for next loop */
  28195. for (;;) {
  28196. if (safe == 0) {
  28197. js_ctx->p = p;
  28198. break;
  28199. }
  28200. safe--;
  28201. /* End of input (NUL) goes through slow path and causes SyntaxError. */
  28202. DUK_ASSERT(duk__json_decstr_lookup[0] == 0x00);
  28203. b = *p++;
  28204. x = (duk_small_int_t) duk__json_decstr_lookup[b];
  28205. if (DUK_LIKELY(x != 0)) {
  28206. /* Fast path, decode as is. */
  28207. *q++ = b;
  28208. } else if (b == DUK_ASC_DOUBLEQUOTE) {
  28209. js_ctx->p = p;
  28210. goto found_quote;
  28211. } else if (b == DUK_ASC_BACKSLASH) {
  28212. /* We've ensured space for one escaped input; then
  28213. * bail out and recheck (this makes escape handling
  28214. * quite slow but it's uncommon).
  28215. */
  28216. js_ctx->p = p;
  28217. if (duk__dec_string_escape(js_ctx, &q) != 0) {
  28218. goto syntax_error;
  28219. }
  28220. break;
  28221. } else {
  28222. js_ctx->p = p;
  28223. goto syntax_error;
  28224. }
  28225. }
  28226. }
  28227. found_quote:
  28228. #else /* DUK_USE_JSON_DECSTRING_FASTPATH */
  28229. for (;;) {
  28230. duk_uint8_t x;
  28231. q = DUK_BW_ENSURE_RAW(js_ctx->thr, bw, DUK_UNICODE_MAX_XUTF8_LENGTH, q);
  28232. x = duk__dec_get(js_ctx);
  28233. if (x == DUK_ASC_DOUBLEQUOTE) {
  28234. break;
  28235. } else if (x == DUK_ASC_BACKSLASH) {
  28236. if (duk__dec_string_escape(js_ctx, &q) != 0) {
  28237. goto syntax_error;
  28238. }
  28239. } else if (x < 0x20) {
  28240. /* catches EOF (NUL) */
  28241. goto syntax_error;
  28242. } else {
  28243. *q++ = (duk_uint8_t) x;
  28244. }
  28245. }
  28246. #endif /* DUK_USE_JSON_DECSTRING_FASTPATH */
  28247. DUK_BW_SETPTR_AND_COMPACT(js_ctx->thr, bw, q);
  28248. duk_to_string(ctx, -1);
  28249. /* [ ... str ] */
  28250. return;
  28251. syntax_error:
  28252. duk__dec_syntax_error(js_ctx);
  28253. DUK_UNREACHABLE();
  28254. }
  28255. #ifdef DUK_USE_JX
  28256. /* Decode a plain string consisting entirely of identifier characters.
  28257. * Used to parse plain keys (e.g. "foo: 123").
  28258. */
  28259. DUK_LOCAL void duk__dec_plain_string(duk_json_dec_ctx *js_ctx) {
  28260. duk_hthread *thr = js_ctx->thr;
  28261. duk_context *ctx = (duk_context *) thr;
  28262. const duk_uint8_t *p;
  28263. duk_small_int_t x;
  28264. /* Caller has already eaten the first char so backtrack one byte. */
  28265. js_ctx->p--; /* safe */
  28266. p = js_ctx->p;
  28267. /* Here again we parse bytes, and non-ASCII UTF-8 will cause end of
  28268. * parsing (which is correct except if there are non-shortest encodings).
  28269. * There is also no need to check explicitly for end of input buffer as
  28270. * the input is NUL padded and NUL will exit the parsing loop.
  28271. *
  28272. * Because no unescaping takes place, we can just scan to the end of the
  28273. * plain string and intern from the input buffer.
  28274. */
  28275. for (;;) {
  28276. x = *p;
  28277. /* There is no need to check the first character specially here
  28278. * (i.e. reject digits): the caller only accepts valid initial
  28279. * characters and won't call us if the first character is a digit.
  28280. * This also ensures that the plain string won't be empty.
  28281. */
  28282. if (!duk_unicode_is_identifier_part((duk_codepoint_t) x)) {
  28283. break;
  28284. }
  28285. p++;
  28286. }
  28287. duk_push_lstring(ctx, (const char *) js_ctx->p, (duk_size_t) (p - js_ctx->p));
  28288. js_ctx->p = p;
  28289. /* [ ... str ] */
  28290. }
  28291. #endif /* DUK_USE_JX */
  28292. #ifdef DUK_USE_JX
  28293. DUK_LOCAL void duk__dec_pointer(duk_json_dec_ctx *js_ctx) {
  28294. duk_hthread *thr = js_ctx->thr;
  28295. duk_context *ctx = (duk_context *) thr;
  28296. const duk_uint8_t *p;
  28297. duk_small_int_t x;
  28298. void *voidptr;
  28299. /* Caller has already eaten the first character ('(') which we don't need. */
  28300. p = js_ctx->p;
  28301. for (;;) {
  28302. x = *p;
  28303. /* Assume that the native representation never contains a closing
  28304. * parenthesis.
  28305. */
  28306. if (x == DUK_ASC_RPAREN) {
  28307. break;
  28308. } else if (x <= 0) {
  28309. /* NUL term or -1 (EOF), NUL check would suffice */
  28310. goto syntax_error;
  28311. }
  28312. p++;
  28313. }
  28314. /* There is no need to NUL delimit the sscanf() call: trailing garbage is
  28315. * ignored and there is always a NUL terminator which will force an error
  28316. * if no error is encountered before it. It's possible that the scan
  28317. * would scan further than between [js_ctx->p,p[ though and we'd advance
  28318. * by less than the scanned value.
  28319. *
  28320. * Because pointers are platform specific, a failure to scan a pointer
  28321. * results in a null pointer which is a better placeholder than a missing
  28322. * value or an error.
  28323. */
  28324. voidptr = NULL;
  28325. (void) DUK_SSCANF((const char *) js_ctx->p, DUK_STR_FMT_PTR, &voidptr);
  28326. duk_push_pointer(ctx, voidptr);
  28327. js_ctx->p = p + 1; /* skip ')' */
  28328. /* [ ... ptr ] */
  28329. return;
  28330. syntax_error:
  28331. duk__dec_syntax_error(js_ctx);
  28332. DUK_UNREACHABLE();
  28333. }
  28334. #endif /* DUK_USE_JX */
  28335. #ifdef DUK_USE_JX
  28336. DUK_LOCAL void duk__dec_buffer(duk_json_dec_ctx *js_ctx) {
  28337. duk_hthread *thr = js_ctx->thr;
  28338. duk_context *ctx = (duk_context *) thr;
  28339. const duk_uint8_t *p;
  28340. duk_small_int_t x;
  28341. /* Caller has already eaten the first character ('|') which we don't need. */
  28342. p = js_ctx->p;
  28343. for (;;) {
  28344. x = *p;
  28345. /* This loop intentionally does not ensure characters are valid
  28346. * ([0-9a-fA-F]) because the hex decode call below will do that.
  28347. */
  28348. if (x == DUK_ASC_PIPE) {
  28349. break;
  28350. } else if (x <= 0) {
  28351. /* NUL term or -1 (EOF), NUL check would suffice */
  28352. goto syntax_error;
  28353. }
  28354. p++;
  28355. }
  28356. duk_push_lstring(ctx, (const char *) js_ctx->p, (duk_size_t) (p - js_ctx->p));
  28357. duk_hex_decode(ctx, -1);
  28358. js_ctx->p = p + 1; /* skip '|' */
  28359. /* [ ... buf ] */
  28360. return;
  28361. syntax_error:
  28362. duk__dec_syntax_error(js_ctx);
  28363. DUK_UNREACHABLE();
  28364. }
  28365. #endif /* DUK_USE_JX */
  28366. /* Parse a number, other than NaN or +/- Infinity */
  28367. DUK_LOCAL void duk__dec_number(duk_json_dec_ctx *js_ctx) {
  28368. duk_context *ctx = (duk_context *) js_ctx->thr;
  28369. const duk_uint8_t *p_start;
  28370. const duk_uint8_t *p;
  28371. duk_uint8_t x;
  28372. duk_small_uint_t s2n_flags;
  28373. DUK_DDD(DUK_DDDPRINT("parse_number"));
  28374. p_start = js_ctx->p;
  28375. /* First pass parse is very lenient (e.g. allows '1.2.3') and extracts a
  28376. * string for strict number parsing.
  28377. */
  28378. p = js_ctx->p;
  28379. for (;;) {
  28380. x = *p;
  28381. DUK_DDD(DUK_DDDPRINT("parse_number: p_start=%p, p=%p, p_end=%p, x=%ld",
  28382. (void *) p_start, (void *) p,
  28383. (void *) js_ctx->p_end, (long) x));
  28384. #if defined(DUK_USE_JSON_DECNUMBER_FASTPATH)
  28385. /* This fast path is pretty marginal in practice.
  28386. * XXX: candidate for removal.
  28387. */
  28388. DUK_ASSERT(duk__json_decnumber_lookup[0x00] == 0x00); /* end-of-input breaks */
  28389. if (duk__json_decnumber_lookup[x] == 0) {
  28390. break;
  28391. }
  28392. #else /* DUK_USE_JSON_DECNUMBER_FASTPATH */
  28393. if (!((x >= DUK_ASC_0 && x <= DUK_ASC_9) ||
  28394. (x == DUK_ASC_PERIOD || x == DUK_ASC_LC_E ||
  28395. x == DUK_ASC_UC_E || x == DUK_ASC_MINUS || x == DUK_ASC_PLUS))) {
  28396. /* Plus sign must be accepted for positive exponents
  28397. * (e.g. '1.5e+2'). This clause catches NULs.
  28398. */
  28399. break;
  28400. }
  28401. #endif /* DUK_USE_JSON_DECNUMBER_FASTPATH */
  28402. p++; /* safe, because matched (NUL causes a break) */
  28403. }
  28404. js_ctx->p = p;
  28405. DUK_ASSERT(js_ctx->p > p_start);
  28406. duk_push_lstring(ctx, (const char *) p_start, (duk_size_t) (p - p_start));
  28407. s2n_flags = DUK_S2N_FLAG_ALLOW_EXP |
  28408. DUK_S2N_FLAG_ALLOW_MINUS | /* but don't allow leading plus */
  28409. DUK_S2N_FLAG_ALLOW_FRAC;
  28410. DUK_DDD(DUK_DDDPRINT("parse_number: string before parsing: %!T",
  28411. (duk_tval *) duk_get_tval(ctx, -1)));
  28412. duk_numconv_parse(ctx, 10 /*radix*/, s2n_flags);
  28413. if (duk_is_nan(ctx, -1)) {
  28414. duk__dec_syntax_error(js_ctx);
  28415. }
  28416. DUK_ASSERT(duk_is_number(ctx, -1));
  28417. DUK_DDD(DUK_DDDPRINT("parse_number: final number: %!T",
  28418. (duk_tval *) duk_get_tval(ctx, -1)));
  28419. /* [ ... num ] */
  28420. }
  28421. DUK_LOCAL void duk__dec_objarr_entry(duk_json_dec_ctx *js_ctx) {
  28422. duk_context *ctx = (duk_context *) js_ctx->thr;
  28423. duk_require_stack(ctx, DUK_JSON_DEC_REQSTACK);
  28424. /* c recursion check */
  28425. DUK_ASSERT(js_ctx->recursion_depth >= 0);
  28426. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  28427. if (js_ctx->recursion_depth >= js_ctx->recursion_limit) {
  28428. DUK_ERROR((duk_hthread *) ctx, DUK_ERR_RANGE_ERROR, DUK_STR_JSONDEC_RECLIMIT);
  28429. }
  28430. js_ctx->recursion_depth++;
  28431. }
  28432. DUK_LOCAL void duk__dec_objarr_exit(duk_json_dec_ctx *js_ctx) {
  28433. /* c recursion check */
  28434. DUK_ASSERT(js_ctx->recursion_depth > 0);
  28435. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  28436. js_ctx->recursion_depth--;
  28437. }
  28438. DUK_LOCAL void duk__dec_object(duk_json_dec_ctx *js_ctx) {
  28439. duk_context *ctx = (duk_context *) js_ctx->thr;
  28440. duk_int_t key_count; /* XXX: a "first" flag would suffice */
  28441. duk_uint8_t x;
  28442. DUK_DDD(DUK_DDDPRINT("parse_object"));
  28443. duk__dec_objarr_entry(js_ctx);
  28444. duk_push_object(ctx);
  28445. /* Initial '{' has been checked and eaten by caller. */
  28446. key_count = 0;
  28447. for (;;) {
  28448. x = duk__dec_get_nonwhite(js_ctx);
  28449. DUK_DDD(DUK_DDDPRINT("parse_object: obj=%!T, x=%ld, key_count=%ld",
  28450. (duk_tval *) duk_get_tval(ctx, -1),
  28451. (long) x, (long) key_count));
  28452. /* handle comma and closing brace */
  28453. if (x == DUK_ASC_COMMA && key_count > 0) {
  28454. /* accept comma, expect new value */
  28455. x = duk__dec_get_nonwhite(js_ctx);
  28456. } else if (x == DUK_ASC_RCURLY) {
  28457. /* eat closing brace */
  28458. break;
  28459. } else if (key_count == 0) {
  28460. /* accept anything, expect first value (EOF will be
  28461. * caught by key parsing below.
  28462. */
  28463. ;
  28464. } else {
  28465. /* catches EOF (NUL) and initial comma */
  28466. goto syntax_error;
  28467. }
  28468. /* parse key and value */
  28469. if (x == DUK_ASC_DOUBLEQUOTE) {
  28470. duk__dec_string(js_ctx);
  28471. #ifdef DUK_USE_JX
  28472. } else if (js_ctx->flag_ext_custom &&
  28473. duk_unicode_is_identifier_start((duk_codepoint_t) x)) {
  28474. duk__dec_plain_string(js_ctx);
  28475. #endif
  28476. } else {
  28477. goto syntax_error;
  28478. }
  28479. /* [ ... obj key ] */
  28480. x = duk__dec_get_nonwhite(js_ctx);
  28481. if (x != DUK_ASC_COLON) {
  28482. goto syntax_error;
  28483. }
  28484. duk__dec_value(js_ctx);
  28485. /* [ ... obj key val ] */
  28486. duk_xdef_prop_wec(ctx, -3);
  28487. /* [ ... obj ] */
  28488. key_count++;
  28489. }
  28490. /* [ ... obj ] */
  28491. DUK_DDD(DUK_DDDPRINT("parse_object: final object is %!T",
  28492. (duk_tval *) duk_get_tval(ctx, -1)));
  28493. duk__dec_objarr_exit(js_ctx);
  28494. return;
  28495. syntax_error:
  28496. duk__dec_syntax_error(js_ctx);
  28497. DUK_UNREACHABLE();
  28498. }
  28499. DUK_LOCAL void duk__dec_array(duk_json_dec_ctx *js_ctx) {
  28500. duk_context *ctx = (duk_context *) js_ctx->thr;
  28501. duk_uarridx_t arr_idx;
  28502. duk_uint8_t x;
  28503. DUK_DDD(DUK_DDDPRINT("parse_array"));
  28504. duk__dec_objarr_entry(js_ctx);
  28505. duk_push_array(ctx);
  28506. /* Initial '[' has been checked and eaten by caller. */
  28507. arr_idx = 0;
  28508. for (;;) {
  28509. x = duk__dec_get_nonwhite(js_ctx);
  28510. DUK_DDD(DUK_DDDPRINT("parse_array: arr=%!T, x=%ld, arr_idx=%ld",
  28511. (duk_tval *) duk_get_tval(ctx, -1),
  28512. (long) x, (long) arr_idx));
  28513. /* handle comma and closing bracket */
  28514. if ((x == DUK_ASC_COMMA) && (arr_idx != 0)) {
  28515. /* accept comma, expect new value */
  28516. ;
  28517. } else if (x == DUK_ASC_RBRACKET) {
  28518. /* eat closing bracket */
  28519. break;
  28520. } else if (arr_idx == 0) {
  28521. /* accept anything, expect first value (EOF will be
  28522. * caught by duk__dec_value() below.
  28523. */
  28524. js_ctx->p--; /* backtrack (safe) */
  28525. } else {
  28526. /* catches EOF (NUL) and initial comma */
  28527. goto syntax_error;
  28528. }
  28529. /* parse value */
  28530. duk__dec_value(js_ctx);
  28531. /* [ ... arr val ] */
  28532. duk_xdef_prop_index_wec(ctx, -2, arr_idx);
  28533. arr_idx++;
  28534. }
  28535. /* Must set 'length' explicitly when using duk_xdef_prop_xxx() to
  28536. * set the values.
  28537. */
  28538. duk_set_length(ctx, -1, arr_idx);
  28539. /* [ ... arr ] */
  28540. DUK_DDD(DUK_DDDPRINT("parse_array: final array is %!T",
  28541. (duk_tval *) duk_get_tval(ctx, -1)));
  28542. duk__dec_objarr_exit(js_ctx);
  28543. return;
  28544. syntax_error:
  28545. duk__dec_syntax_error(js_ctx);
  28546. DUK_UNREACHABLE();
  28547. }
  28548. DUK_LOCAL void duk__dec_value(duk_json_dec_ctx *js_ctx) {
  28549. duk_context *ctx = (duk_context *) js_ctx->thr;
  28550. duk_uint8_t x;
  28551. x = duk__dec_get_nonwhite(js_ctx);
  28552. DUK_DDD(DUK_DDDPRINT("parse_value: initial x=%ld", (long) x));
  28553. /* Note: duk__dec_req_stridx() backtracks one char */
  28554. if (x == DUK_ASC_DOUBLEQUOTE) {
  28555. duk__dec_string(js_ctx);
  28556. } else if ((x >= DUK_ASC_0 && x <= DUK_ASC_9) || (x == DUK_ASC_MINUS)) {
  28557. #ifdef DUK_USE_JX
  28558. if (js_ctx->flag_ext_custom && x == DUK_ASC_MINUS && duk__dec_peek(js_ctx) == DUK_ASC_UC_I) {
  28559. duk__dec_req_stridx(js_ctx, DUK_STRIDX_MINUS_INFINITY); /* "-Infinity", '-' has been eaten */
  28560. duk_push_number(ctx, -DUK_DOUBLE_INFINITY);
  28561. } else {
  28562. #else
  28563. { /* unconditional block */
  28564. #endif
  28565. /* We already ate 'x', so backup one byte. */
  28566. js_ctx->p--; /* safe */
  28567. duk__dec_number(js_ctx);
  28568. }
  28569. } else if (x == DUK_ASC_LC_T) {
  28570. duk__dec_req_stridx(js_ctx, DUK_STRIDX_TRUE);
  28571. duk_push_true(ctx);
  28572. } else if (x == DUK_ASC_LC_F) {
  28573. duk__dec_req_stridx(js_ctx, DUK_STRIDX_FALSE);
  28574. duk_push_false(ctx);
  28575. } else if (x == DUK_ASC_LC_N) {
  28576. duk__dec_req_stridx(js_ctx, DUK_STRIDX_LC_NULL);
  28577. duk_push_null(ctx);
  28578. #ifdef DUK_USE_JX
  28579. } else if (js_ctx->flag_ext_custom && x == DUK_ASC_LC_U) {
  28580. duk__dec_req_stridx(js_ctx, DUK_STRIDX_LC_UNDEFINED);
  28581. duk_push_undefined(ctx);
  28582. } else if (js_ctx->flag_ext_custom && x == DUK_ASC_UC_N) {
  28583. duk__dec_req_stridx(js_ctx, DUK_STRIDX_NAN);
  28584. duk_push_nan(ctx);
  28585. } else if (js_ctx->flag_ext_custom && x == DUK_ASC_UC_I) {
  28586. duk__dec_req_stridx(js_ctx, DUK_STRIDX_INFINITY);
  28587. duk_push_number(ctx, DUK_DOUBLE_INFINITY);
  28588. } else if (js_ctx->flag_ext_custom && x == DUK_ASC_LPAREN) {
  28589. duk__dec_pointer(js_ctx);
  28590. } else if (js_ctx->flag_ext_custom && x == DUK_ASC_PIPE) {
  28591. duk__dec_buffer(js_ctx);
  28592. #endif
  28593. } else if (x == DUK_ASC_LCURLY) {
  28594. duk__dec_object(js_ctx);
  28595. } else if (x == DUK_ASC_LBRACKET) {
  28596. duk__dec_array(js_ctx);
  28597. } else {
  28598. /* catches EOF (NUL) */
  28599. goto syntax_error;
  28600. }
  28601. duk__dec_eat_white(js_ctx);
  28602. /* [ ... val ] */
  28603. return;
  28604. syntax_error:
  28605. duk__dec_syntax_error(js_ctx);
  28606. DUK_UNREACHABLE();
  28607. }
  28608. /* Recursive value reviver, implements the Walk() algorithm. No C recursion
  28609. * check is done here because the initial parsing step will already ensure
  28610. * there is a reasonable limit on C recursion depth and hence object depth.
  28611. */
  28612. DUK_LOCAL void duk__dec_reviver_walk(duk_json_dec_ctx *js_ctx) {
  28613. duk_context *ctx = (duk_context *) js_ctx->thr;
  28614. duk_hobject *h;
  28615. duk_uarridx_t i, arr_len;
  28616. DUK_DDD(DUK_DDDPRINT("walk: top=%ld, holder=%!T, name=%!T",
  28617. (long) duk_get_top(ctx),
  28618. (duk_tval *) duk_get_tval(ctx, -2),
  28619. (duk_tval *) duk_get_tval(ctx, -1)));
  28620. duk_dup_top(ctx);
  28621. duk_get_prop(ctx, -3); /* -> [ ... holder name val ] */
  28622. h = duk_get_hobject(ctx, -1);
  28623. if (h != NULL) {
  28624. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY) {
  28625. arr_len = (duk_uarridx_t) duk_get_length(ctx, -1);
  28626. for (i = 0; i < arr_len; i++) {
  28627. /* [ ... holder name val ] */
  28628. DUK_DDD(DUK_DDDPRINT("walk: array, top=%ld, i=%ld, arr_len=%ld, holder=%!T, name=%!T, val=%!T",
  28629. (long) duk_get_top(ctx), (long) i, (long) arr_len,
  28630. (duk_tval *) duk_get_tval(ctx, -3), (duk_tval *) duk_get_tval(ctx, -2),
  28631. (duk_tval *) duk_get_tval(ctx, -1)));
  28632. /* XXX: push_uint_string / push_u32_string */
  28633. duk_dup_top(ctx);
  28634. duk_push_uint(ctx, (duk_uint_t) i);
  28635. duk_to_string(ctx, -1); /* -> [ ... holder name val val ToString(i) ] */
  28636. duk__dec_reviver_walk(js_ctx); /* -> [ ... holder name val new_elem ] */
  28637. if (duk_is_undefined(ctx, -1)) {
  28638. duk_pop(ctx);
  28639. duk_del_prop_index(ctx, -1, i);
  28640. } else {
  28641. /* XXX: duk_xdef_prop_index_wec() would be more appropriate
  28642. * here but it currently makes some assumptions that might
  28643. * not hold (e.g. that previous property is not an accessor).
  28644. */
  28645. duk_put_prop_index(ctx, -2, i);
  28646. }
  28647. }
  28648. } else {
  28649. /* [ ... holder name val ] */
  28650. duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY /*flags*/);
  28651. while (duk_next(ctx, -1 /*enum_index*/, 0 /*get_value*/)) {
  28652. DUK_DDD(DUK_DDDPRINT("walk: object, top=%ld, holder=%!T, name=%!T, val=%!T, enum=%!iT, obj_key=%!T",
  28653. (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, -5),
  28654. (duk_tval *) duk_get_tval(ctx, -4), (duk_tval *) duk_get_tval(ctx, -3),
  28655. (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1)));
  28656. /* [ ... holder name val enum obj_key ] */
  28657. duk_dup(ctx, -3);
  28658. duk_dup(ctx, -2);
  28659. /* [ ... holder name val enum obj_key val obj_key ] */
  28660. duk__dec_reviver_walk(js_ctx);
  28661. /* [ ... holder name val enum obj_key new_elem ] */
  28662. if (duk_is_undefined(ctx, -1)) {
  28663. duk_pop(ctx);
  28664. duk_del_prop(ctx, -3);
  28665. } else {
  28666. /* XXX: duk_xdef_prop_index_wec() would be more appropriate
  28667. * here but it currently makes some assumptions that might
  28668. * not hold (e.g. that previous property is not an accessor).
  28669. *
  28670. * Using duk_put_prop() works incorrectly with '__proto__'
  28671. * if the own property with that name has been deleted. This
  28672. * does not happen normally, but a clever reviver can trigger
  28673. * that, see complex reviver case in: test-bug-json-parse-__proto__.js.
  28674. */
  28675. duk_put_prop(ctx, -4);
  28676. }
  28677. }
  28678. duk_pop(ctx); /* pop enum */
  28679. }
  28680. }
  28681. /* [ ... holder name val ] */
  28682. duk_dup(ctx, js_ctx->idx_reviver);
  28683. duk_insert(ctx, -4); /* -> [ ... reviver holder name val ] */
  28684. duk_call_method(ctx, 2); /* -> [ ... res ] */
  28685. DUK_DDD(DUK_DDDPRINT("walk: top=%ld, result=%!T",
  28686. (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, -1)));
  28687. }
  28688. /*
  28689. * Stringify implementation.
  28690. */
  28691. #define DUK__EMIT_1(js_ctx,ch) duk__emit_1((js_ctx), (duk_uint_fast8_t) (ch))
  28692. #define DUK__EMIT_2(js_ctx,ch1,ch2) duk__emit_2((js_ctx), (duk_uint_fast8_t) (ch1), (duk_uint_fast8_t) (ch2))
  28693. #define DUK__EMIT_HSTR(js_ctx,h) duk__emit_hstring((js_ctx), (h))
  28694. #if defined(DUK_USE_FASTINT) || defined(DUK_USE_JX) || defined(DUK_USE_JC)
  28695. #define DUK__EMIT_CSTR(js_ctx,p) duk__emit_cstring((js_ctx), (p))
  28696. #endif
  28697. #define DUK__EMIT_STRIDX(js_ctx,i) duk__emit_stridx((js_ctx), (i))
  28698. #if defined(DUK_USE_JSON_STRINGIFY_FASTPATH)
  28699. #define DUK__UNEMIT_1(js_ctx) duk__unemit_1((js_ctx))
  28700. #endif
  28701. DUK_LOCAL void duk__emit_1(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch) {
  28702. DUK_BW_WRITE_ENSURE_U8(js_ctx->thr, &js_ctx->bw, ch);
  28703. }
  28704. DUK_LOCAL void duk__emit_2(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch1, duk_uint_fast8_t ch2) {
  28705. DUK_BW_WRITE_ENSURE_U8_2(js_ctx->thr, &js_ctx->bw, ch1, ch2);
  28706. }
  28707. DUK_LOCAL void duk__emit_hstring(duk_json_enc_ctx *js_ctx, duk_hstring *h) {
  28708. DUK_BW_WRITE_ENSURE_HSTRING(js_ctx->thr, &js_ctx->bw, h);
  28709. }
  28710. #if defined(DUK_USE_FASTINT) || defined(DUK_USE_JX) || defined(DUK_USE_JC)
  28711. DUK_LOCAL void duk__emit_cstring(duk_json_enc_ctx *js_ctx, const char *str) {
  28712. DUK_BW_WRITE_ENSURE_CSTRING(js_ctx->thr, &js_ctx->bw, str);
  28713. }
  28714. #endif
  28715. DUK_LOCAL void duk__emit_stridx(duk_json_enc_ctx *js_ctx, duk_small_uint_t stridx) {
  28716. duk_hstring *h;
  28717. DUK_ASSERT_DISABLE(stridx >= 0); /* unsigned */
  28718. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  28719. h = DUK_HTHREAD_GET_STRING(js_ctx->thr, stridx);
  28720. DUK_ASSERT(h != NULL);
  28721. DUK_BW_WRITE_ENSURE_HSTRING(js_ctx->thr, &js_ctx->bw, h);
  28722. }
  28723. #if defined(DUK_USE_JSON_STRINGIFY_FASTPATH)
  28724. DUK_LOCAL void duk__unemit_1(duk_json_enc_ctx *js_ctx) {
  28725. DUK_ASSERT(DUK_BW_GET_SIZE(js_ctx->thr, &js_ctx->bw) >= 1);
  28726. DUK_BW_ADD_PTR(js_ctx->thr, &js_ctx->bw, -1);
  28727. }
  28728. #endif /* DUK_USE_JSON_STRINGIFY_FASTPATH */
  28729. #define DUK__MKESC(nybbles,esc1,esc2) \
  28730. (((duk_uint_fast32_t) (nybbles)) << 16) | \
  28731. (((duk_uint_fast32_t) (esc1)) << 8) | \
  28732. ((duk_uint_fast32_t) (esc2))
  28733. 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) {
  28734. duk_uint_fast32_t tmp;
  28735. duk_small_uint_t dig;
  28736. DUK_UNREF(js_ctx);
  28737. /* Caller ensures space for at least DUK__JSON_MAX_ESC_LEN. */
  28738. /* Select appropriate escape format automatically, and set 'tmp' to a
  28739. * value encoding both the escape format character and the nybble count:
  28740. *
  28741. * (nybble_count << 16) | (escape_char1) | (escape_char2)
  28742. */
  28743. #ifdef DUK_USE_JX
  28744. if (DUK_LIKELY(cp < 0x100UL)) {
  28745. if (DUK_UNLIKELY(js_ctx->flag_ext_custom)) {
  28746. tmp = DUK__MKESC(2, DUK_ASC_BACKSLASH, DUK_ASC_LC_X);
  28747. } else {
  28748. tmp = DUK__MKESC(4, DUK_ASC_BACKSLASH, DUK_ASC_LC_U);
  28749. }
  28750. } else
  28751. #endif
  28752. if (DUK_LIKELY(cp < 0x10000UL)) {
  28753. tmp = DUK__MKESC(4, DUK_ASC_BACKSLASH, DUK_ASC_LC_U);
  28754. } else {
  28755. #ifdef DUK_USE_JX
  28756. if (DUK_LIKELY(js_ctx->flag_ext_custom)) {
  28757. tmp = DUK__MKESC(8, DUK_ASC_BACKSLASH, DUK_ASC_UC_U);
  28758. } else
  28759. #endif
  28760. {
  28761. /* In compatible mode and standard JSON mode, output
  28762. * something useful for non-BMP characters. This won't
  28763. * roundtrip but will still be more or less readable and
  28764. * more useful than an error.
  28765. */
  28766. tmp = DUK__MKESC(8, DUK_ASC_UC_U, DUK_ASC_PLUS);
  28767. }
  28768. }
  28769. *q++ = (duk_uint8_t) ((tmp >> 8) & 0xff);
  28770. *q++ = (duk_uint8_t) (tmp & 0xff);
  28771. tmp = tmp >> 16;
  28772. while (tmp > 0) {
  28773. tmp--;
  28774. dig = (duk_small_uint_t) ((cp >> (4 * tmp)) & 0x0f);
  28775. *q++ = duk_lc_digits[dig];
  28776. }
  28777. return q;
  28778. }
  28779. /* Check whether key quotes would be needed (custom encoding). */
  28780. DUK_LOCAL duk_bool_t duk__enc_key_quotes_needed(duk_hstring *h_key) {
  28781. const duk_uint8_t *p, *p_start, *p_end;
  28782. duk_small_uint_t ch;
  28783. DUK_ASSERT(h_key != NULL);
  28784. p_start = DUK_HSTRING_GET_DATA(h_key);
  28785. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_key);
  28786. p = p_start;
  28787. DUK_DDD(DUK_DDDPRINT("duk__enc_key_quotes_needed: h_key=%!O, p_start=%p, p_end=%p, p=%p",
  28788. (duk_heaphdr *) h_key, (void *) p_start, (void *) p_end, (void *) p));
  28789. /* Since we only accept ASCII characters, there is no need for
  28790. * actual decoding. A non-ASCII character will be >= 0x80 which
  28791. * causes a false return value immediately.
  28792. */
  28793. if (p == p_end) {
  28794. /* Zero length string is not accepted without quotes */
  28795. return 1;
  28796. }
  28797. while (p < p_end) {
  28798. ch = (duk_small_uint_t) (*p);
  28799. /* Accept ASCII IdentifierStart and IdentifierPart if not first char.
  28800. * Function selection is a bit uncommon.
  28801. */
  28802. if ((p > p_start ? duk_unicode_is_identifier_part :
  28803. duk_unicode_is_identifier_start) ((duk_codepoint_t) ch)) {
  28804. p++;
  28805. continue;
  28806. }
  28807. /* all non-ASCII characters also come here (first byte >= 0x80) */
  28808. return 1;
  28809. }
  28810. return 0;
  28811. }
  28812. /* The Quote(value) operation: quote a string.
  28813. *
  28814. * Stack policy: [ ] -> [ ].
  28815. */
  28816. DUK_LOCAL void duk__enc_quote_string(duk_json_enc_ctx *js_ctx, duk_hstring *h_str) {
  28817. duk_hthread *thr = js_ctx->thr;
  28818. const duk_uint8_t *p, *p_start, *p_end, *p_now, *p_tmp;
  28819. duk_uint8_t *q;
  28820. duk_ucodepoint_t cp; /* typed for duk_unicode_decode_xutf8() */
  28821. DUK_DDD(DUK_DDDPRINT("duk__enc_quote_string: h_str=%!O", (duk_heaphdr *) h_str));
  28822. DUK_ASSERT(h_str != NULL);
  28823. p_start = DUK_HSTRING_GET_DATA(h_str);
  28824. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_str);
  28825. p = p_start;
  28826. DUK__EMIT_1(js_ctx, DUK_ASC_DOUBLEQUOTE);
  28827. /* Encode string in small chunks, estimating the maximum expansion so that
  28828. * there's no need to ensure space while processing the chunk.
  28829. */
  28830. while (p < p_end) {
  28831. duk_size_t left, now, space;
  28832. left = (duk_size_t) (p_end - p);
  28833. now = (left > DUK__JSON_ENCSTR_CHUNKSIZE ?
  28834. DUK__JSON_ENCSTR_CHUNKSIZE : left);
  28835. /* Maximum expansion per input byte is 6:
  28836. * - invalid UTF-8 byte causes "\uXXXX" to be emitted (6/1 = 6).
  28837. * - 2-byte UTF-8 encodes as "\uXXXX" (6/2 = 3).
  28838. * - 4-byte UTF-8 encodes as "\Uxxxxxxxx" (10/4 = 2.5).
  28839. */
  28840. space = now * 6;
  28841. q = DUK_BW_ENSURE_GETPTR(thr, &js_ctx->bw, space);
  28842. p_now = p + now;
  28843. while (p < p_now) {
  28844. #if defined(DUK_USE_JSON_QUOTESTRING_FASTPATH)
  28845. duk_uint8_t b;
  28846. b = duk__json_quotestr_lookup[*p++];
  28847. if (DUK_LIKELY(b < 0x80)) {
  28848. /* Most input bytes go through here. */
  28849. *q++ = b;
  28850. } else if (b >= 0xa0) {
  28851. *q++ = DUK_ASC_BACKSLASH;
  28852. *q++ = (duk_uint8_t) (b - 0x80);
  28853. } else if (b == 0x80) {
  28854. cp = (duk_ucodepoint_t) (*(p - 1));
  28855. q = duk__emit_esc_auto_fast(js_ctx, cp, q);
  28856. } else if (b == 0x7f && js_ctx->flag_ascii_only) {
  28857. /* 0x7F is special */
  28858. DUK_ASSERT(b == 0x81);
  28859. cp = (duk_ucodepoint_t) 0x7f;
  28860. q = duk__emit_esc_auto_fast(js_ctx, cp, q);
  28861. } else {
  28862. DUK_ASSERT(b == 0x81);
  28863. p--;
  28864. /* slow path is shared */
  28865. #else /* DUK_USE_JSON_QUOTESTRING_FASTPATH */
  28866. cp = *p;
  28867. if (DUK_LIKELY(cp <= 0x7f)) {
  28868. /* ascii fast path: avoid decoding utf-8 */
  28869. p++;
  28870. if (cp == 0x22 || cp == 0x5c) {
  28871. /* double quote or backslash */
  28872. *q++ = DUK_ASC_BACKSLASH;
  28873. *q++ = (duk_uint8_t) cp;
  28874. } else if (cp < 0x20) {
  28875. duk_uint_fast8_t esc_char;
  28876. /* This approach is a bit shorter than a straight
  28877. * if-else-ladder and also a bit faster.
  28878. */
  28879. if (cp < (sizeof(duk__json_quotestr_esc) / sizeof(duk_uint8_t)) &&
  28880. (esc_char = duk__json_quotestr_esc[cp]) != 0) {
  28881. *q++ = DUK_ASC_BACKSLASH;
  28882. *q++ = (duk_uint8_t) esc_char;
  28883. } else {
  28884. q = duk__emit_esc_auto_fast(js_ctx, cp, q);
  28885. }
  28886. } else if (cp == 0x7f && js_ctx->flag_ascii_only) {
  28887. q = duk__emit_esc_auto_fast(js_ctx, cp, q);
  28888. } else {
  28889. /* any other printable -> as is */
  28890. *q++ = (duk_uint8_t) cp;
  28891. }
  28892. } else {
  28893. /* slow path is shared */
  28894. #endif /* DUK_USE_JSON_QUOTESTRING_FASTPATH */
  28895. /* slow path decode */
  28896. /* If XUTF-8 decoding fails, treat the offending byte as a codepoint directly
  28897. * and go forward one byte. This is of course very lossy, but allows some kind
  28898. * of output to be produced even for internal strings which don't conform to
  28899. * XUTF-8. All standard Ecmascript strings are always CESU-8, so this behavior
  28900. * does not violate the Ecmascript specification. The behavior is applied to
  28901. * all modes, including Ecmascript standard JSON. Because the current XUTF-8
  28902. * decoding is not very strict, this behavior only really affects initial bytes
  28903. * and truncated codepoints.
  28904. *
  28905. * Another alternative would be to scan forwards to start of next codepoint
  28906. * (or end of input) and emit just one replacement codepoint.
  28907. */
  28908. p_tmp = p;
  28909. if (!duk_unicode_decode_xutf8(thr, &p, p_start, p_end, &cp)) {
  28910. /* Decode failed. */
  28911. cp = *p_tmp;
  28912. p = p_tmp + 1;
  28913. }
  28914. #ifdef DUK_USE_NONSTD_JSON_ESC_U2028_U2029
  28915. if (js_ctx->flag_ascii_only || cp == 0x2028 || cp == 0x2029) {
  28916. #else
  28917. if (js_ctx->flag_ascii_only) {
  28918. #endif
  28919. q = duk__emit_esc_auto_fast(js_ctx, cp, q);
  28920. } else {
  28921. /* as is */
  28922. DUK_RAW_WRITE_XUTF8(q, cp);
  28923. }
  28924. }
  28925. }
  28926. DUK_BW_SET_PTR(thr, &js_ctx->bw, q);
  28927. }
  28928. DUK__EMIT_1(js_ctx, DUK_ASC_DOUBLEQUOTE);
  28929. }
  28930. /* Encode a double (checked by caller) from stack top. Stack top may be
  28931. * replaced by serialized string but is not popped (caller does that).
  28932. */
  28933. DUK_LOCAL void duk__enc_double(duk_json_enc_ctx *js_ctx) {
  28934. duk_context *ctx;
  28935. duk_tval *tv;
  28936. duk_double_t d;
  28937. duk_small_int_t c;
  28938. duk_small_int_t s;
  28939. duk_small_uint_t stridx;
  28940. duk_small_uint_t n2s_flags;
  28941. duk_hstring *h_str;
  28942. DUK_ASSERT(js_ctx != NULL);
  28943. ctx = (duk_context *) js_ctx->thr;
  28944. DUK_ASSERT(ctx != NULL);
  28945. /* Caller must ensure 'tv' is indeed a double and not a fastint! */
  28946. tv = duk_get_tval(ctx, -1);
  28947. DUK_ASSERT(tv != NULL);
  28948. DUK_ASSERT(DUK_TVAL_IS_DOUBLE(tv));
  28949. d = DUK_TVAL_GET_DOUBLE(tv);
  28950. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  28951. s = (duk_small_int_t) DUK_SIGNBIT(d);
  28952. DUK_UNREF(s);
  28953. if (DUK_LIKELY(!(c == DUK_FP_INFINITE || c == DUK_FP_NAN))) {
  28954. DUK_ASSERT(DUK_ISFINITE(d));
  28955. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  28956. /* Negative zero needs special handling in JX/JC because
  28957. * it would otherwise serialize to '0', not '-0'.
  28958. */
  28959. if (DUK_UNLIKELY(c == DUK_FP_ZERO && s != 0 &&
  28960. (js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible))) {
  28961. duk_push_hstring_stridx(ctx, DUK_STRIDX_MINUS_ZERO); /* '-0' */
  28962. } else
  28963. #endif /* DUK_USE_JX || DUK_USE_JC */
  28964. {
  28965. n2s_flags = 0;
  28966. /* [ ... number ] -> [ ... string ] */
  28967. duk_numconv_stringify(ctx, 10 /*radix*/, 0 /*digits*/, n2s_flags);
  28968. }
  28969. h_str = duk_to_hstring(ctx, -1);
  28970. DUK_ASSERT(h_str != NULL);
  28971. DUK__EMIT_HSTR(js_ctx, h_str);
  28972. return;
  28973. }
  28974. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  28975. if (!(js_ctx->flags & (DUK_JSON_FLAG_EXT_CUSTOM |
  28976. DUK_JSON_FLAG_EXT_COMPATIBLE))) {
  28977. stridx = DUK_STRIDX_LC_NULL;
  28978. } else if (c == DUK_FP_NAN) {
  28979. stridx = js_ctx->stridx_custom_nan;
  28980. } else if (s == 0) {
  28981. stridx = js_ctx->stridx_custom_posinf;
  28982. } else {
  28983. stridx = js_ctx->stridx_custom_neginf;
  28984. }
  28985. #else
  28986. stridx = DUK_STRIDX_LC_NULL;
  28987. #endif
  28988. DUK__EMIT_STRIDX(js_ctx, stridx);
  28989. }
  28990. #if defined(DUK_USE_FASTINT)
  28991. /* Encode a fastint from duk_tval ptr, no value stack effects. */
  28992. DUK_LOCAL void duk__enc_fastint_tval(duk_json_enc_ctx *js_ctx, duk_tval *tv) {
  28993. duk_int64_t v;
  28994. /* Fastint range is signed 48-bit so longest value is -2^47 = -140737488355328
  28995. * (16 chars long), longest signed 64-bit value is -2^63 = -9223372036854775808
  28996. * (20 chars long). Alloc space for 64-bit range to be safe.
  28997. */
  28998. duk_uint8_t buf[20 + 1];
  28999. /* Caller must ensure 'tv' is indeed a fastint! */
  29000. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv));
  29001. v = DUK_TVAL_GET_FASTINT(tv);
  29002. /* XXX: There are no format strings in duk_config.h yet, could add
  29003. * one for formatting duk_int64_t. For now, assumes "%lld" and that
  29004. * "long long" type exists. Could also rely on C99 directly but that
  29005. * won't work for older MSVC.
  29006. */
  29007. DUK_SPRINTF((char *) buf, "%lld", (long long) v);
  29008. DUK__EMIT_CSTR(js_ctx, (const char *) buf);
  29009. }
  29010. #endif
  29011. /* Shared entry handling for object/array serialization: indent/stepback,
  29012. * loop detection.
  29013. */
  29014. DUK_LOCAL void duk__enc_objarr_entry(duk_json_enc_ctx *js_ctx, duk_hstring **h_stepback, duk_hstring **h_indent, duk_idx_t *entry_top) {
  29015. duk_context *ctx = (duk_context *) js_ctx->thr;
  29016. duk_hobject *h_target;
  29017. *entry_top = duk_get_top(ctx);
  29018. duk_require_stack(ctx, DUK_JSON_ENC_REQSTACK);
  29019. /* loop check */
  29020. h_target = duk_get_hobject(ctx, -1); /* object or array */
  29021. DUK_ASSERT(h_target != NULL);
  29022. /* XXX: this check is very expensive, perhaps use a small
  29023. * array to make it faster for at least reasonably shallow
  29024. * objects?
  29025. */
  29026. duk_push_sprintf(ctx, DUK_STR_FMT_PTR, (void *) h_target);
  29027. duk_dup_top(ctx); /* -> [ ... voidp voidp ] */
  29028. if (duk_has_prop(ctx, js_ctx->idx_loop)) {
  29029. DUK_ERROR((duk_hthread *) ctx, DUK_ERR_TYPE_ERROR, DUK_STR_CYCLIC_INPUT);
  29030. }
  29031. duk_push_true(ctx); /* -> [ ... voidp true ] */
  29032. duk_put_prop(ctx, js_ctx->idx_loop); /* -> [ ... ] */
  29033. /* c recursion check */
  29034. DUK_ASSERT(js_ctx->recursion_depth >= 0);
  29035. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  29036. if (js_ctx->recursion_depth >= js_ctx->recursion_limit) {
  29037. DUK_ERROR((duk_hthread *) ctx, DUK_ERR_RANGE_ERROR, DUK_STR_JSONENC_RECLIMIT);
  29038. }
  29039. js_ctx->recursion_depth++;
  29040. /* figure out indent and stepback */
  29041. *h_indent = NULL;
  29042. *h_stepback = NULL;
  29043. if (js_ctx->h_gap != NULL) {
  29044. DUK_ASSERT(js_ctx->h_indent != NULL);
  29045. *h_stepback = js_ctx->h_indent;
  29046. duk_push_hstring(ctx, js_ctx->h_indent);
  29047. duk_push_hstring(ctx, js_ctx->h_gap);
  29048. duk_concat(ctx, 2);
  29049. js_ctx->h_indent = duk_get_hstring(ctx, -1);
  29050. *h_indent = js_ctx->h_indent;
  29051. DUK_ASSERT(js_ctx->h_indent != NULL);
  29052. /* The new indent string is left at value stack top, and will
  29053. * be popped by the shared exit handler.
  29054. */
  29055. } else {
  29056. DUK_ASSERT(js_ctx->h_indent == NULL);
  29057. }
  29058. DUK_DDD(DUK_DDDPRINT("shared entry finished: top=%ld, loop=%!T",
  29059. (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, js_ctx->idx_loop)));
  29060. }
  29061. /* Shared exit handling for object/array serialization. */
  29062. DUK_LOCAL void duk__enc_objarr_exit(duk_json_enc_ctx *js_ctx, duk_hstring **h_stepback, duk_hstring **h_indent, duk_idx_t *entry_top) {
  29063. duk_context *ctx = (duk_context *) js_ctx->thr;
  29064. duk_hobject *h_target;
  29065. DUK_UNREF(h_indent);
  29066. if (js_ctx->h_gap != NULL) {
  29067. DUK_ASSERT(js_ctx->h_indent != NULL);
  29068. DUK_ASSERT(*h_stepback != NULL);
  29069. DUK_ASSERT(*h_indent != NULL);
  29070. js_ctx->h_indent = *h_stepback; /* previous js_ctx->h_indent */
  29071. /* Note: we don't need to pop anything because the duk_set_top()
  29072. * at the end will take care of it.
  29073. */
  29074. } else {
  29075. DUK_ASSERT(js_ctx->h_indent == NULL);
  29076. DUK_ASSERT(*h_stepback == NULL);
  29077. DUK_ASSERT(*h_indent == NULL);
  29078. }
  29079. /* c recursion check */
  29080. DUK_ASSERT(js_ctx->recursion_depth > 0);
  29081. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  29082. js_ctx->recursion_depth--;
  29083. /* loop check */
  29084. h_target = duk_get_hobject(ctx, *entry_top - 1); /* original target at entry_top - 1 */
  29085. DUK_ASSERT(h_target != NULL);
  29086. /* XXX: this check is very expensive */
  29087. duk_push_sprintf(ctx, DUK_STR_FMT_PTR, (void *) h_target);
  29088. duk_del_prop(ctx, js_ctx->idx_loop); /* -> [ ... ] */
  29089. /* restore stack top after unbalanced code paths */
  29090. duk_set_top(ctx, *entry_top);
  29091. DUK_DDD(DUK_DDDPRINT("shared entry finished: top=%ld, loop=%!T",
  29092. (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, js_ctx->idx_loop)));
  29093. }
  29094. /* The JO(value) operation: encode object.
  29095. *
  29096. * Stack policy: [ object ] -> [ object ].
  29097. */
  29098. DUK_LOCAL void duk__enc_object(duk_json_enc_ctx *js_ctx) {
  29099. duk_context *ctx = (duk_context *) js_ctx->thr;
  29100. duk_hstring *h_stepback;
  29101. duk_hstring *h_indent;
  29102. duk_hstring *h_key;
  29103. duk_idx_t entry_top;
  29104. duk_idx_t idx_obj;
  29105. duk_idx_t idx_keys;
  29106. duk_bool_t first;
  29107. duk_bool_t undef;
  29108. duk_uarridx_t arr_len, i;
  29109. DUK_DDD(DUK_DDDPRINT("duk__enc_object: obj=%!T", (duk_tval *) duk_get_tval(ctx, -1)));
  29110. duk__enc_objarr_entry(js_ctx, &h_stepback, &h_indent, &entry_top);
  29111. idx_obj = entry_top - 1;
  29112. if (js_ctx->idx_proplist >= 0) {
  29113. idx_keys = js_ctx->idx_proplist;
  29114. } else {
  29115. /* XXX: would be nice to enumerate an object at specified index */
  29116. duk_dup(ctx, idx_obj);
  29117. (void) duk_hobject_get_enumerated_keys(ctx, DUK_ENUM_OWN_PROPERTIES_ONLY /*flags*/); /* [ ... target ] -> [ ... target keys ] */
  29118. idx_keys = duk_require_normalize_index(ctx, -1);
  29119. /* leave stack unbalanced on purpose */
  29120. }
  29121. DUK_DDD(DUK_DDDPRINT("idx_keys=%ld, h_keys=%!T",
  29122. (long) idx_keys, (duk_tval *) duk_get_tval(ctx, idx_keys)));
  29123. /* Steps 8-10 have been merged to avoid a "partial" variable. */
  29124. DUK__EMIT_1(js_ctx, DUK_ASC_LCURLY);
  29125. /* XXX: keys is an internal object with all keys to be processed
  29126. * in its (gapless) array part. Because nobody can touch the keys
  29127. * object, we could iterate its array part directly (keeping in mind
  29128. * that it can be reallocated).
  29129. */
  29130. arr_len = (duk_uarridx_t) duk_get_length(ctx, idx_keys);
  29131. first = 1;
  29132. for (i = 0; i < arr_len; i++) {
  29133. duk_get_prop_index(ctx, idx_keys, i); /* -> [ ... key ] */
  29134. DUK_DDD(DUK_DDDPRINT("object property loop: holder=%!T, key=%!T",
  29135. (duk_tval *) duk_get_tval(ctx, idx_obj),
  29136. (duk_tval *) duk_get_tval(ctx, -1)));
  29137. undef = duk__enc_value1(js_ctx, idx_obj);
  29138. if (undef) {
  29139. /* Value would yield 'undefined', so skip key altogether.
  29140. * Side effects have already happened.
  29141. */
  29142. continue;
  29143. }
  29144. /* [ ... key val ] */
  29145. if (first) {
  29146. first = 0;
  29147. } else {
  29148. DUK__EMIT_1(js_ctx, DUK_ASC_COMMA);
  29149. }
  29150. if (h_indent != NULL) {
  29151. DUK__EMIT_1(js_ctx, 0x0a);
  29152. DUK__EMIT_HSTR(js_ctx, h_indent);
  29153. }
  29154. h_key = duk_get_hstring(ctx, -2);
  29155. DUK_ASSERT(h_key != NULL);
  29156. if (js_ctx->flag_avoid_key_quotes && !duk__enc_key_quotes_needed(h_key)) {
  29157. /* emit key as is */
  29158. DUK__EMIT_HSTR(js_ctx, h_key);
  29159. } else {
  29160. duk__enc_quote_string(js_ctx, h_key);
  29161. }
  29162. if (h_indent != NULL) {
  29163. DUK__EMIT_2(js_ctx, DUK_ASC_COLON, DUK_ASC_SPACE);
  29164. } else {
  29165. DUK__EMIT_1(js_ctx, DUK_ASC_COLON);
  29166. }
  29167. /* [ ... key val ] */
  29168. duk__enc_value2(js_ctx); /* -> [ ... ] */
  29169. }
  29170. if (!first) {
  29171. if (h_stepback != NULL) {
  29172. DUK_ASSERT(h_indent != NULL);
  29173. DUK__EMIT_1(js_ctx, 0x0a);
  29174. DUK__EMIT_HSTR(js_ctx, h_stepback);
  29175. }
  29176. }
  29177. DUK__EMIT_1(js_ctx, DUK_ASC_RCURLY);
  29178. duk__enc_objarr_exit(js_ctx, &h_stepback, &h_indent, &entry_top);
  29179. DUK_ASSERT_TOP(ctx, entry_top);
  29180. }
  29181. /* The JA(value) operation: encode array.
  29182. *
  29183. * Stack policy: [ array ] -> [ array ].
  29184. */
  29185. DUK_LOCAL void duk__enc_array(duk_json_enc_ctx *js_ctx) {
  29186. duk_context *ctx = (duk_context *) js_ctx->thr;
  29187. duk_hstring *h_stepback;
  29188. duk_hstring *h_indent;
  29189. duk_idx_t entry_top;
  29190. duk_idx_t idx_arr;
  29191. duk_bool_t undef;
  29192. duk_uarridx_t i, arr_len;
  29193. DUK_DDD(DUK_DDDPRINT("duk__enc_array: array=%!T",
  29194. (duk_tval *) duk_get_tval(ctx, -1)));
  29195. duk__enc_objarr_entry(js_ctx, &h_stepback, &h_indent, &entry_top);
  29196. idx_arr = entry_top - 1;
  29197. /* Steps 8-10 have been merged to avoid a "partial" variable. */
  29198. DUK__EMIT_1(js_ctx, DUK_ASC_LBRACKET);
  29199. arr_len = (duk_uarridx_t) duk_get_length(ctx, idx_arr);
  29200. for (i = 0; i < arr_len; i++) {
  29201. DUK_DDD(DUK_DDDPRINT("array entry loop: array=%!T, h_indent=%!O, h_stepback=%!O, index=%ld, arr_len=%ld",
  29202. (duk_tval *) duk_get_tval(ctx, idx_arr), (duk_heaphdr *) h_indent,
  29203. (duk_heaphdr *) h_stepback, (long) i, (long) arr_len));
  29204. if (i > 0) {
  29205. DUK__EMIT_1(js_ctx, DUK_ASC_COMMA);
  29206. }
  29207. if (h_indent != NULL) {
  29208. DUK__EMIT_1(js_ctx, 0x0a);
  29209. DUK__EMIT_HSTR(js_ctx, h_indent);
  29210. }
  29211. /* XXX: duk_push_uint_string() */
  29212. duk_push_uint(ctx, (duk_uint_t) i);
  29213. duk_to_string(ctx, -1); /* -> [ ... key ] */
  29214. undef = duk__enc_value1(js_ctx, idx_arr);
  29215. if (undef) {
  29216. DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL);
  29217. } else {
  29218. /* [ ... key val ] */
  29219. duk__enc_value2(js_ctx);
  29220. }
  29221. }
  29222. if (arr_len > 0) {
  29223. if (h_stepback != NULL) {
  29224. DUK_ASSERT(h_indent != NULL);
  29225. DUK__EMIT_1(js_ctx, 0x0a);
  29226. DUK__EMIT_HSTR(js_ctx, h_stepback);
  29227. }
  29228. }
  29229. DUK__EMIT_1(js_ctx, DUK_ASC_RBRACKET);
  29230. duk__enc_objarr_exit(js_ctx, &h_stepback, &h_indent, &entry_top);
  29231. DUK_ASSERT_TOP(ctx, entry_top);
  29232. }
  29233. /* The Str(key, holder) operation: encode value, steps 1-4.
  29234. *
  29235. * Returns non-zero if the value between steps 4 and 5 would yield an
  29236. * 'undefined' final result. This is useful in JO() because we need to
  29237. * get the side effects out, but need to know whether or not a key will
  29238. * be omitted from the serialization.
  29239. *
  29240. * Stack policy: [ ... key ] -> [ ... key val ] if retval == 0.
  29241. * -> [ ... ] if retval != 0.
  29242. */
  29243. DUK_LOCAL duk_bool_t duk__enc_value1(duk_json_enc_ctx *js_ctx, duk_idx_t idx_holder) {
  29244. duk_context *ctx = (duk_context *) js_ctx->thr;
  29245. duk_hthread *thr = (duk_hthread *) ctx;
  29246. duk_hobject *h;
  29247. duk_tval *tv;
  29248. duk_small_int_t c;
  29249. DUK_DDD(DUK_DDDPRINT("duk__enc_value1: idx_holder=%ld, holder=%!T, key=%!T",
  29250. (long) idx_holder, (duk_tval *) duk_get_tval(ctx, idx_holder),
  29251. (duk_tval *) duk_get_tval(ctx, -1)));
  29252. DUK_UNREF(thr);
  29253. duk_dup_top(ctx); /* -> [ ... key key ] */
  29254. duk_get_prop(ctx, idx_holder); /* -> [ ... key val ] */
  29255. DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(ctx, -1)));
  29256. h = duk_get_hobject_or_lfunc_coerce(ctx, -1);
  29257. if (h != NULL) {
  29258. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_TO_JSON);
  29259. h = duk_get_hobject_or_lfunc_coerce(ctx, -1); /* toJSON() can also be a lightfunc */
  29260. if (h != NULL && DUK_HOBJECT_IS_CALLABLE(h)) {
  29261. DUK_DDD(DUK_DDDPRINT("value is object, has callable toJSON() -> call it"));
  29262. duk_dup(ctx, -2); /* -> [ ... key val toJSON val ] */
  29263. duk_dup(ctx, -4); /* -> [ ... key val toJSON val key ] */
  29264. duk_call_method(ctx, 1); /* -> [ ... key val val' ] */
  29265. duk_remove(ctx, -2); /* -> [ ... key val' ] */
  29266. } else {
  29267. duk_pop(ctx);
  29268. }
  29269. }
  29270. /* [ ... key val ] */
  29271. DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(ctx, -1)));
  29272. if (js_ctx->h_replacer) {
  29273. /* XXX: here a "slice copy" would be useful */
  29274. DUK_DDD(DUK_DDDPRINT("replacer is set, call replacer"));
  29275. duk_push_hobject(ctx, js_ctx->h_replacer); /* -> [ ... key val replacer ] */
  29276. duk_dup(ctx, idx_holder); /* -> [ ... key val replacer holder ] */
  29277. duk_dup(ctx, -4); /* -> [ ... key val replacer holder key ] */
  29278. duk_dup(ctx, -4); /* -> [ ... key val replacer holder key val ] */
  29279. duk_call_method(ctx, 2); /* -> [ ... key val val' ] */
  29280. duk_remove(ctx, -2); /* -> [ ... key val' ] */
  29281. }
  29282. /* [ ... key val ] */
  29283. DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(ctx, -1)));
  29284. tv = duk_get_tval(ctx, -1);
  29285. DUK_ASSERT(tv != NULL);
  29286. if (DUK_TVAL_IS_OBJECT(tv)) {
  29287. h = DUK_TVAL_GET_OBJECT(tv);
  29288. DUK_ASSERT(h != NULL);
  29289. if (DUK_HOBJECT_IS_BUFFEROBJECT(h)) {
  29290. duk_hbufferobject *h_bufobj;
  29291. h_bufobj = (duk_hbufferobject *) h;
  29292. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  29293. if (h_bufobj->buf == NULL || !DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj)) {
  29294. duk_push_null(ctx);
  29295. } else if (DUK_HBUFFEROBJECT_FULL_SLICE(h_bufobj)) {
  29296. duk_push_hbuffer(ctx, h_bufobj->buf);
  29297. } else {
  29298. /* This is not very good because we're making a copy
  29299. * for serialization, but only for proper views.
  29300. * Better support would be to serialize slices
  29301. * directly but since we only push a raw buffer
  29302. * here we can't convey the slice offset/length.
  29303. */
  29304. duk_uint8_t *p_buf;
  29305. p_buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, h_bufobj->length);
  29306. DUK_MEMCPY((void *) p_buf,
  29307. (const void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufobj)),
  29308. h_bufobj->length);
  29309. }
  29310. duk_remove(ctx, -2);
  29311. } else {
  29312. c = (duk_small_int_t) DUK_HOBJECT_GET_CLASS_NUMBER(h);
  29313. switch ((int) c) {
  29314. case DUK_HOBJECT_CLASS_NUMBER: {
  29315. DUK_DDD(DUK_DDDPRINT("value is a Number object -> coerce with ToNumber()"));
  29316. duk_to_number(ctx, -1);
  29317. break;
  29318. }
  29319. case DUK_HOBJECT_CLASS_STRING: {
  29320. DUK_DDD(DUK_DDDPRINT("value is a String object -> coerce with ToString()"));
  29321. duk_to_string(ctx, -1);
  29322. break;
  29323. }
  29324. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  29325. case DUK_HOBJECT_CLASS_POINTER:
  29326. #endif
  29327. case DUK_HOBJECT_CLASS_BOOLEAN: {
  29328. DUK_DDD(DUK_DDDPRINT("value is a Boolean/Buffer/Pointer object -> get internal value"));
  29329. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE);
  29330. duk_remove(ctx, -2);
  29331. break;
  29332. }
  29333. } /* end switch */
  29334. }
  29335. }
  29336. /* [ ... key val ] */
  29337. DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(ctx, -1)));
  29338. if (duk_check_type_mask(ctx, -1, js_ctx->mask_for_undefined)) {
  29339. /* will result in undefined */
  29340. DUK_DDD(DUK_DDDPRINT("-> will result in undefined (type mask check)"));
  29341. goto undef;
  29342. }
  29343. /* functions are detected specially */
  29344. h = duk_get_hobject(ctx, -1);
  29345. if (h != NULL && DUK_HOBJECT_IS_CALLABLE(h)) {
  29346. if (js_ctx->flags & (DUK_JSON_FLAG_EXT_CUSTOM |
  29347. DUK_JSON_FLAG_EXT_COMPATIBLE)) {
  29348. /* function will be serialized to custom format */
  29349. } else {
  29350. /* functions are not serialized, results in undefined */
  29351. DUK_DDD(DUK_DDDPRINT("-> will result in undefined (function)"));
  29352. goto undef;
  29353. }
  29354. }
  29355. DUK_DDD(DUK_DDDPRINT("-> will not result in undefined"));
  29356. return 0;
  29357. undef:
  29358. duk_pop_2(ctx);
  29359. return 1;
  29360. }
  29361. /* The Str(key, holder) operation: encode value, steps 5-10.
  29362. *
  29363. * This must not be called unless duk__enc_value1() returns non-zero.
  29364. * If so, this is guaranteed to produce a non-undefined result.
  29365. * Non-standard encodings (e.g. for undefined) are only used if
  29366. * duk__enc_value1() indicates they are accepted; they're not
  29367. * checked or asserted here again.
  29368. *
  29369. * Stack policy: [ ... key val ] -> [ ... ].
  29370. */
  29371. DUK_LOCAL void duk__enc_value2(duk_json_enc_ctx *js_ctx) {
  29372. duk_context *ctx = (duk_context *) js_ctx->thr;
  29373. duk_hthread *thr = (duk_hthread *) ctx;
  29374. duk_tval *tv;
  29375. DUK_UNREF(thr);
  29376. DUK_DDD(DUK_DDDPRINT("duk__enc_value2: key=%!T, val=%!T",
  29377. (duk_tval *) duk_get_tval(ctx, -2),
  29378. (duk_tval *) duk_get_tval(ctx, -1)));
  29379. /* [ ... key val ] */
  29380. tv = duk_get_tval(ctx, -1);
  29381. DUK_ASSERT(tv != NULL);
  29382. switch (DUK_TVAL_GET_TAG(tv)) {
  29383. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  29384. /* When JX/JC not in use, duk__enc_value1 will block undefined values. */
  29385. case DUK_TAG_UNDEFINED: {
  29386. DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_undefined);
  29387. break;
  29388. }
  29389. #endif
  29390. case DUK_TAG_NULL: {
  29391. DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL);
  29392. break;
  29393. }
  29394. case DUK_TAG_BOOLEAN: {
  29395. DUK__EMIT_STRIDX(js_ctx, DUK_TVAL_GET_BOOLEAN(tv) ?
  29396. DUK_STRIDX_TRUE : DUK_STRIDX_FALSE);
  29397. break;
  29398. }
  29399. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  29400. /* When JX/JC not in use, duk__enc_value1 will block pointer values. */
  29401. case DUK_TAG_POINTER: {
  29402. char buf[64]; /* XXX: how to figure correct size? */
  29403. const char *fmt;
  29404. void *ptr = DUK_TVAL_GET_POINTER(tv);
  29405. DUK_MEMZERO(buf, sizeof(buf));
  29406. /* The #ifdef clutter here needs to handle the three cases:
  29407. * (1) JX+JC, (2) JX only, (3) JC only.
  29408. */
  29409. #if defined(DUK_USE_JX) && defined(DUK_USE_JC)
  29410. if (js_ctx->flag_ext_custom)
  29411. #endif
  29412. #if defined(DUK_USE_JX)
  29413. {
  29414. fmt = ptr ? "(%p)" : "(null)";
  29415. }
  29416. #endif
  29417. #if defined(DUK_USE_JX) && defined(DUK_USE_JC)
  29418. else
  29419. #endif
  29420. #if defined(DUK_USE_JC)
  29421. {
  29422. fmt = ptr ? "{\"_ptr\":\"%p\"}" : "{\"_ptr\":\"null\"}";
  29423. }
  29424. #endif
  29425. /* When ptr == NULL, the format argument is unused. */
  29426. DUK_SNPRINTF(buf, sizeof(buf) - 1, fmt, ptr); /* must not truncate */
  29427. DUK__EMIT_CSTR(js_ctx, buf);
  29428. break;
  29429. }
  29430. #endif /* DUK_USE_JX || DUK_USE_JC */
  29431. case DUK_TAG_STRING: {
  29432. duk_hstring *h = DUK_TVAL_GET_STRING(tv);
  29433. DUK_ASSERT(h != NULL);
  29434. duk__enc_quote_string(js_ctx, h);
  29435. break;
  29436. }
  29437. case DUK_TAG_OBJECT: {
  29438. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  29439. DUK_ASSERT(h != NULL);
  29440. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  29441. if (DUK_HOBJECT_IS_CALLABLE(h)) {
  29442. /* We only get here when doing non-standard JSON encoding */
  29443. DUK_ASSERT(js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible);
  29444. DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_function);
  29445. } else /* continues below */
  29446. #endif
  29447. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY) {
  29448. duk__enc_array(js_ctx);
  29449. } else {
  29450. duk__enc_object(js_ctx);
  29451. }
  29452. break;
  29453. }
  29454. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  29455. /* When JX/JC not in use, duk__enc_value1 will block buffer values. */
  29456. case DUK_TAG_BUFFER: {
  29457. /* Buffer values are encoded in (lowercase) hex to make the
  29458. * binary data readable. Base64 or similar would be more
  29459. * compact but less readable, and the point of JX/JC
  29460. * variants is to be as useful to a programmer as possible.
  29461. */
  29462. /* The #ifdef clutter here needs to handle the three cases:
  29463. * (1) JX+JC, (2) JX only, (3) JC only.
  29464. */
  29465. #if defined(DUK_USE_JX) && defined(DUK_USE_JC)
  29466. if (js_ctx->flag_ext_custom)
  29467. #endif
  29468. #if defined(DUK_USE_JX)
  29469. {
  29470. duk_uint8_t *p, *p_end;
  29471. duk_small_uint_t x;
  29472. duk_hbuffer *h;
  29473. duk_uint8_t *q;
  29474. duk_size_t space;
  29475. h = DUK_TVAL_GET_BUFFER(tv);
  29476. DUK_ASSERT(h != NULL);
  29477. p = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h);
  29478. p_end = p + DUK_HBUFFER_GET_SIZE(h);
  29479. space = 1 + DUK_HBUFFER_GET_SIZE(h) * 2 + 1;
  29480. DUK_ASSERT(DUK_HBUFFER_MAX_BYTELEN <= 0x7ffffffeUL);
  29481. DUK_ASSERT((space - 2) / 2 == DUK_HBUFFER_GET_SIZE(h)); /* overflow not possible, buffer limits */
  29482. q = DUK_BW_ENSURE_GETPTR(thr, &js_ctx->bw, space);
  29483. *q++ = DUK_ASC_PIPE;
  29484. while (p < p_end) {
  29485. x = *p++;
  29486. *q++ = duk_lc_digits[(x >> 4) & 0x0f];
  29487. *q++ = duk_lc_digits[x & 0x0f];
  29488. }
  29489. *q++ = DUK_ASC_PIPE;
  29490. DUK_BW_SET_PTR(thr, &js_ctx->bw, q);
  29491. }
  29492. #endif
  29493. #if defined(DUK_USE_JX) && defined(DUK_USE_JC)
  29494. else
  29495. #endif
  29496. #if defined(DUK_USE_JC)
  29497. {
  29498. DUK_ASSERT(js_ctx->flag_ext_compatible);
  29499. duk_hex_encode(ctx, -1);
  29500. DUK__EMIT_CSTR(js_ctx, "{\"_buf\":");
  29501. duk__enc_quote_string(js_ctx, duk_require_hstring(ctx, -1));
  29502. DUK__EMIT_1(js_ctx, DUK_ASC_RCURLY);
  29503. }
  29504. #endif
  29505. break;
  29506. }
  29507. #endif /* DUK_USE_JX || DUK_USE_JC */
  29508. case DUK_TAG_LIGHTFUNC: {
  29509. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  29510. /* We only get here when doing non-standard JSON encoding */
  29511. DUK_ASSERT(js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible);
  29512. DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_function);
  29513. #else
  29514. /* Standard JSON omits functions */
  29515. DUK_UNREACHABLE();
  29516. #endif
  29517. break;
  29518. }
  29519. #if defined(DUK_USE_FASTINT)
  29520. case DUK_TAG_FASTINT:
  29521. /* Number serialization has a significant impact relative to
  29522. * other fast path code, so careful fast path for fastints.
  29523. */
  29524. duk__enc_fastint_tval(js_ctx, tv);
  29525. break;
  29526. #endif
  29527. default: {
  29528. /* number */
  29529. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  29530. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  29531. /* XXX: A fast path for usual integers would be useful when
  29532. * fastint support is not enabled.
  29533. */
  29534. duk__enc_double(js_ctx);
  29535. break;
  29536. }
  29537. }
  29538. /* [ ... key val ] -> [ ... ] */
  29539. duk_pop_2(ctx);
  29540. }
  29541. /* E5 Section 15.12.3, main algorithm, step 4.b.ii steps 1-4. */
  29542. DUK_LOCAL duk_bool_t duk__enc_allow_into_proplist(duk_tval *tv) {
  29543. duk_hobject *h;
  29544. duk_small_int_t c;
  29545. DUK_ASSERT(tv != NULL);
  29546. if (DUK_TVAL_IS_STRING(tv) || DUK_TVAL_IS_NUMBER(tv)) {
  29547. return 1;
  29548. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  29549. h = DUK_TVAL_GET_OBJECT(tv);
  29550. DUK_ASSERT(h != NULL);
  29551. c = (duk_small_int_t) DUK_HOBJECT_GET_CLASS_NUMBER(h);
  29552. if (c == DUK_HOBJECT_CLASS_STRING || c == DUK_HOBJECT_CLASS_NUMBER) {
  29553. return 1;
  29554. }
  29555. }
  29556. return 0;
  29557. }
  29558. /*
  29559. * JSON.stringify() fast path
  29560. */
  29561. #if defined(DUK_USE_JSON_STRINGIFY_FASTPATH)
  29562. DUK_LOCAL duk_bool_t duk__json_stringify_fast_value(duk_json_enc_ctx *js_ctx, duk_tval *tv) {
  29563. duk_uint_fast32_t i, n;
  29564. DUK_DDD(DUK_DDDPRINT("stringify fast: %!T", tv));
  29565. DUK_ASSERT(js_ctx != NULL);
  29566. DUK_ASSERT(js_ctx->thr != NULL);
  29567. #if defined(DUK_USE_JX)
  29568. DUK_ASSERT(js_ctx->flag_ext_custom == 0);
  29569. #endif
  29570. #if defined(DUK_USE_JC)
  29571. DUK_ASSERT(js_ctx->flag_ext_compatible == 0);
  29572. #endif
  29573. restart_match:
  29574. DUK_ASSERT(tv != NULL);
  29575. switch (DUK_TVAL_GET_TAG(tv)) {
  29576. case DUK_TAG_UNDEFINED: {
  29577. goto emit_undefined;
  29578. }
  29579. case DUK_TAG_NULL: {
  29580. DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL);
  29581. break;
  29582. }
  29583. case DUK_TAG_BOOLEAN: {
  29584. DUK__EMIT_STRIDX(js_ctx, DUK_TVAL_GET_BOOLEAN(tv) ?
  29585. DUK_STRIDX_TRUE : DUK_STRIDX_FALSE);
  29586. break;
  29587. }
  29588. case DUK_TAG_STRING: {
  29589. duk_hstring *h;
  29590. h = DUK_TVAL_GET_STRING(tv);
  29591. DUK_ASSERT(h != NULL);
  29592. duk__enc_quote_string(js_ctx, h);
  29593. break;
  29594. }
  29595. case DUK_TAG_OBJECT: {
  29596. duk_hobject *obj;
  29597. duk_tval *tv_val;
  29598. duk_bool_t emitted = 0;
  29599. duk_uint32_t c_bit, c_all, c_array, c_unbox, c_undef, c_object;
  29600. /* For objects JSON.stringify() only looks for own, enumerable
  29601. * properties which is nice for the fast path here.
  29602. *
  29603. * For arrays JSON.stringify() uses [[Get]] so it will actually
  29604. * inherit properties during serialization! This fast path
  29605. * supports gappy arrays as long as there's no actual inherited
  29606. * property (which might be a getter etc).
  29607. *
  29608. * Since recursion only happens for objects, we can have both
  29609. * recursion and loop checks here. We use a simple, depth-limited
  29610. * loop check in the fast path because the object-based tracking
  29611. * is very slow (when tested, it accounted for 50% of fast path
  29612. * execution time for input data with a lot of small objects!).
  29613. */
  29614. obj = DUK_TVAL_GET_OBJECT(tv);
  29615. DUK_ASSERT(obj != NULL);
  29616. /* We rely on a few object flag / class number relationships here,
  29617. * assert for them.
  29618. */
  29619. DUK_ASSERT_HOBJECT_VALID(obj);
  29620. /* Once recursion depth is increased, exit path must decrease
  29621. * it (though it's OK to abort the fast path).
  29622. */
  29623. DUK_ASSERT(js_ctx->recursion_depth >= 0);
  29624. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  29625. if (js_ctx->recursion_depth >= js_ctx->recursion_limit) {
  29626. DUK_DD(DUK_DDPRINT("fast path recursion limit"));
  29627. DUK_ERROR(js_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_JSONDEC_RECLIMIT);
  29628. }
  29629. for (i = 0, n = (duk_uint_fast32_t) js_ctx->recursion_depth; i < n; i++) {
  29630. if (js_ctx->visiting[i] == obj) {
  29631. DUK_DD(DUK_DDPRINT("fast path loop detect"));
  29632. DUK_ERROR(js_ctx->thr, DUK_ERR_TYPE_ERROR, DUK_STR_CYCLIC_INPUT);
  29633. }
  29634. }
  29635. /* Guaranteed by recursion_limit setup so we don't have to
  29636. * check twice.
  29637. */
  29638. DUK_ASSERT(js_ctx->recursion_depth < DUK_JSON_ENC_LOOPARRAY);
  29639. js_ctx->visiting[js_ctx->recursion_depth] = obj;
  29640. js_ctx->recursion_depth++;
  29641. /* If object has a .toJSON() property, we can't be certain
  29642. * that it wouldn't mutate any value arbitrarily, so bail
  29643. * out of the fast path.
  29644. *
  29645. * If an object is a Proxy we also can't avoid side effects
  29646. * so abandon.
  29647. */
  29648. if (duk_hobject_hasprop_raw(js_ctx->thr, obj, DUK_HTHREAD_STRING_TO_JSON(js_ctx->thr)) ||
  29649. DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(obj)) {
  29650. DUK_DD(DUK_DDPRINT("object has a .toJSON property or object is a Proxy, abort fast path"));
  29651. goto abort_fastpath;
  29652. }
  29653. /* We could use a switch-case for the class number but it turns out
  29654. * a small if-else ladder on class masks is better. The if-ladder
  29655. * should be in order of relevancy.
  29656. */
  29657. DUK_ASSERT(DUK_HOBJECT_CLASS_MAX <= 31);
  29658. c_all = DUK_HOBJECT_CMASK_ALL;
  29659. c_array = DUK_HOBJECT_CMASK_ARRAY;
  29660. c_unbox = DUK_HOBJECT_CMASK_NUMBER |
  29661. DUK_HOBJECT_CMASK_STRING |
  29662. DUK_HOBJECT_CMASK_BOOLEAN;
  29663. c_undef = DUK_HOBJECT_CMASK_FUNCTION |
  29664. DUK_HOBJECT_CMASK_ALL_BUFFEROBJECTS;
  29665. c_object = c_all & ~(c_array | c_unbox | c_undef);
  29666. c_bit = DUK_HOBJECT_GET_CLASS_MASK(obj);
  29667. if (c_bit & c_object) {
  29668. /* All other object types. */
  29669. DUK__EMIT_1(js_ctx, DUK_ASC_LCURLY);
  29670. /* A non-Array object should not have an array part in practice.
  29671. * But since it is supported internally (and perhaps used at some
  29672. * point), check and abandon if that's the case.
  29673. */
  29674. if (DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  29675. DUK_DD(DUK_DDPRINT("non-Array object has array part, abort fast path"));
  29676. goto abort_fastpath;
  29677. }
  29678. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(obj); i++) {
  29679. duk_hstring *k;
  29680. duk_size_t prev_size;
  29681. k = DUK_HOBJECT_E_GET_KEY(js_ctx->thr->heap, obj, i);
  29682. if (!k) {
  29683. continue;
  29684. }
  29685. if (!DUK_HOBJECT_E_SLOT_IS_ENUMERABLE(js_ctx->thr->heap, obj, i)) {
  29686. continue;
  29687. }
  29688. if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(js_ctx->thr->heap, obj, i)) {
  29689. /* Getter might have arbitrary side effects,
  29690. * so bail out.
  29691. */
  29692. DUK_DD(DUK_DDPRINT("property is an accessor, abort fast path"));
  29693. goto abort_fastpath;
  29694. }
  29695. if (DUK_HSTRING_HAS_INTERNAL(k)) {
  29696. continue;
  29697. }
  29698. tv_val = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(js_ctx->thr->heap, obj, i);
  29699. prev_size = DUK_BW_GET_SIZE(js_ctx->thr, &js_ctx->bw);
  29700. duk__enc_quote_string(js_ctx, k);
  29701. DUK__EMIT_1(js_ctx, DUK_ASC_COLON);
  29702. if (duk__json_stringify_fast_value(js_ctx, tv_val) == 0) {
  29703. DUK_DD(DUK_DDPRINT("prop value not supported, rewind key and colon"));
  29704. DUK_BW_SET_SIZE(js_ctx->thr, &js_ctx->bw, prev_size);
  29705. } else {
  29706. DUK__EMIT_1(js_ctx, DUK_ASC_COMMA);
  29707. emitted = 1;
  29708. }
  29709. }
  29710. /* If any non-Array value had enumerable virtual own
  29711. * properties, they should be serialized here. Standard
  29712. * types don't.
  29713. */
  29714. if (emitted) {
  29715. DUK__UNEMIT_1(js_ctx); /* eat trailing comma */
  29716. }
  29717. DUK__EMIT_1(js_ctx, DUK_ASC_RCURLY);
  29718. } else if (c_bit & c_array) {
  29719. duk_uint_fast32_t arr_len;
  29720. duk_uint_fast32_t asize;
  29721. DUK__EMIT_1(js_ctx, DUK_ASC_LBRACKET);
  29722. /* Assume arrays are dense in the fast path. */
  29723. if (!DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  29724. DUK_DD(DUK_DDPRINT("Array object is sparse, abort fast path"));
  29725. goto abort_fastpath;
  29726. }
  29727. arr_len = (duk_uint_fast32_t) duk_hobject_get_length(js_ctx->thr, obj);
  29728. asize = (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(obj);
  29729. if (arr_len > asize) {
  29730. /* Array length is larger than 'asize'. This shouldn't
  29731. * happen in practice. Bail out just in case.
  29732. */
  29733. DUK_DD(DUK_DDPRINT("arr_len > asize, abort fast path"));
  29734. goto abort_fastpath;
  29735. }
  29736. /* Array part may be larger than 'length'; if so, iterate
  29737. * only up to array 'length'.
  29738. */
  29739. for (i = 0; i < arr_len; i++) {
  29740. DUK_ASSERT(i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(obj));
  29741. tv_val = DUK_HOBJECT_A_GET_VALUE_PTR(js_ctx->thr->heap, obj, i);
  29742. if (DUK_UNLIKELY(DUK_TVAL_IS_UNUSED(tv_val))) {
  29743. /* Gap in array; check for inherited property,
  29744. * bail out if one exists. This should be enough
  29745. * to support gappy arrays for all practical code.
  29746. */
  29747. duk_hstring *h_tmp;
  29748. duk_bool_t has_inherited;
  29749. /* XXX: refactor into an internal helper, pretty awkward */
  29750. duk_push_uint((duk_context *) js_ctx->thr, (duk_uint_t) i);
  29751. h_tmp = duk_to_hstring((duk_context *) js_ctx->thr, -1);
  29752. DUK_ASSERT(h_tmp != NULL);
  29753. has_inherited = duk_hobject_hasprop_raw(js_ctx->thr, obj, h_tmp);
  29754. duk_pop((duk_context *) js_ctx->thr);
  29755. if (has_inherited) {
  29756. DUK_D(DUK_DPRINT("gap in array, conflicting inherited property, abort fast path"));
  29757. goto abort_fastpath;
  29758. }
  29759. /* Ordinary gap, undefined encodes to 'null' in
  29760. * standard JSON (and no JX/JC support here now).
  29761. */
  29762. DUK_D(DUK_DPRINT("gap in array, no conflicting inherited property, remain on fast path"));
  29763. DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL);
  29764. } else {
  29765. if (duk__json_stringify_fast_value(js_ctx, tv_val) == 0) {
  29766. DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL);
  29767. }
  29768. }
  29769. DUK__EMIT_1(js_ctx, DUK_ASC_COMMA);
  29770. emitted = 1;
  29771. }
  29772. if (emitted) {
  29773. DUK__UNEMIT_1(js_ctx); /* eat trailing comma */
  29774. }
  29775. DUK__EMIT_1(js_ctx, DUK_ASC_RBRACKET);
  29776. } else if (c_bit & c_unbox) {
  29777. /* These three boxed types are required to go through
  29778. * automatic unboxing. Rely on internal value being
  29779. * sane (to avoid infinite recursion).
  29780. */
  29781. duk_tval *tv_internal;
  29782. DUK_DD(DUK_DDPRINT("auto unboxing in fast path"));
  29783. tv_internal = duk_hobject_get_internal_value_tval_ptr(js_ctx->thr->heap, obj);
  29784. DUK_ASSERT(tv_internal != NULL);
  29785. DUK_ASSERT(DUK_TVAL_IS_STRING(tv_internal) ||
  29786. DUK_TVAL_IS_NUMBER(tv_internal) ||
  29787. DUK_TVAL_IS_BOOLEAN(tv_internal));
  29788. /* XXX: for JX/JC, special handling for Pointer, and Buffer? */
  29789. tv = tv_internal;
  29790. goto restart_match;
  29791. } else {
  29792. DUK_ASSERT((c_bit & c_undef) != 0);
  29793. /* Function objects are treated as "undefined" by JSON.
  29794. *
  29795. * The slow path replaces a buffer object automatically with
  29796. * the binary data which then gets treated like "undefined".
  29797. * Since we don't support buffers here now, treat as "undefined".
  29798. */
  29799. /* Must decrease recursion depth before returning. */
  29800. DUK_ASSERT(js_ctx->recursion_depth > 0);
  29801. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  29802. js_ctx->recursion_depth--;
  29803. goto emit_undefined;
  29804. }
  29805. DUK_ASSERT(js_ctx->recursion_depth > 0);
  29806. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  29807. js_ctx->recursion_depth--;
  29808. break;
  29809. }
  29810. case DUK_TAG_BUFFER: {
  29811. goto emit_undefined;
  29812. }
  29813. case DUK_TAG_POINTER: {
  29814. goto emit_undefined;
  29815. }
  29816. case DUK_TAG_LIGHTFUNC: {
  29817. /* A lightfunc might also inherit a .toJSON() so just bail out. */
  29818. DUK_DD(DUK_DDPRINT("value is a lightfunc, abort fast path"));
  29819. goto abort_fastpath;
  29820. }
  29821. #if defined(DUK_USE_FASTINT)
  29822. case DUK_TAG_FASTINT: {
  29823. /* Number serialization has a significant impact relative to
  29824. * other fast path code, so careful fast path for fastints.
  29825. */
  29826. duk__enc_fastint_tval(js_ctx, tv);
  29827. break;
  29828. }
  29829. #endif
  29830. default: {
  29831. /* XXX: A fast path for usual integers would be useful when
  29832. * fastint support is not enabled.
  29833. */
  29834. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  29835. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  29836. /* XXX: Stack discipline is annoying, could be changed in numconv. */
  29837. duk_push_tval((duk_context *) js_ctx->thr, tv);
  29838. duk__enc_double(js_ctx);
  29839. duk_pop((duk_context *) js_ctx->thr);
  29840. #if 0
  29841. /* Could also rely on native sprintf(), but it will handle
  29842. * values like NaN, Infinity, -0, exponent notation etc in
  29843. * a JSON-incompatible way.
  29844. */
  29845. duk_double_t d;
  29846. char buf[64];
  29847. DUK_ASSERT(DUK_TVAL_IS_DOUBLE(tv));
  29848. d = DUK_TVAL_GET_DOUBLE(tv);
  29849. DUK_SPRINTF(buf, "%lg", d);
  29850. DUK__EMIT_CSTR(js_ctx, buf);
  29851. #endif
  29852. }
  29853. }
  29854. return 1; /* not undefined */
  29855. emit_undefined:
  29856. return 0; /* value was undefined/unsupported */
  29857. abort_fastpath:
  29858. /* Error message doesn't matter: the error is ignored anyway. */
  29859. DUK_DD(DUK_DDPRINT("aborting fast path"));
  29860. DUK_ERROR(js_ctx->thr, DUK_ERR_ERROR, DUK_STR_INTERNAL_ERROR);
  29861. return 0; /* unreachable */
  29862. }
  29863. DUK_LOCAL duk_ret_t duk__json_stringify_fast(duk_context *ctx) {
  29864. duk_json_enc_ctx *js_ctx;
  29865. DUK_ASSERT(ctx != NULL);
  29866. js_ctx = (duk_json_enc_ctx *) duk_get_pointer(ctx, -2);
  29867. DUK_ASSERT(js_ctx != NULL);
  29868. if (duk__json_stringify_fast_value(js_ctx, duk_get_tval((duk_context *) (js_ctx->thr), -1)) == 0) {
  29869. DUK_DD(DUK_DDPRINT("top level value not supported, fail fast path"));
  29870. return DUK_RET_ERROR; /* error message doesn't matter, ignored anyway */
  29871. }
  29872. return 0;
  29873. }
  29874. #endif /* DUK_USE_JSON_STRINGIFY_FASTPATH */
  29875. /*
  29876. * Top level wrappers
  29877. */
  29878. DUK_INTERNAL
  29879. void duk_bi_json_parse_helper(duk_context *ctx,
  29880. duk_idx_t idx_value,
  29881. duk_idx_t idx_reviver,
  29882. duk_small_uint_t flags) {
  29883. duk_hthread *thr = (duk_hthread *) ctx;
  29884. duk_json_dec_ctx js_ctx_alloc;
  29885. duk_json_dec_ctx *js_ctx = &js_ctx_alloc;
  29886. duk_hstring *h_text;
  29887. #ifdef DUK_USE_ASSERTIONS
  29888. duk_idx_t entry_top = duk_get_top(ctx);
  29889. #endif
  29890. /* negative top-relative indices not allowed now */
  29891. DUK_ASSERT(idx_value == DUK_INVALID_INDEX || idx_value >= 0);
  29892. DUK_ASSERT(idx_reviver == DUK_INVALID_INDEX || idx_reviver >= 0);
  29893. DUK_DDD(DUK_DDDPRINT("JSON parse start: text=%!T, reviver=%!T, flags=0x%08lx, stack_top=%ld",
  29894. (duk_tval *) duk_get_tval(ctx, idx_value),
  29895. (duk_tval *) duk_get_tval(ctx, idx_reviver),
  29896. (unsigned long) flags,
  29897. (long) duk_get_top(ctx)));
  29898. DUK_MEMZERO(&js_ctx_alloc, sizeof(js_ctx_alloc));
  29899. js_ctx->thr = thr;
  29900. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  29901. /* nothing now */
  29902. #endif
  29903. js_ctx->recursion_limit = DUK_USE_JSON_DEC_RECLIMIT;
  29904. DUK_ASSERT(js_ctx->recursion_depth == 0);
  29905. /* Flag handling currently assumes that flags are consistent. This is OK
  29906. * because the call sites are now strictly controlled.
  29907. */
  29908. js_ctx->flags = flags;
  29909. #ifdef DUK_USE_JX
  29910. js_ctx->flag_ext_custom = flags & DUK_JSON_FLAG_EXT_CUSTOM;
  29911. #endif
  29912. #ifdef DUK_USE_JC
  29913. js_ctx->flag_ext_compatible = flags & DUK_JSON_FLAG_EXT_COMPATIBLE;
  29914. #endif
  29915. h_text = duk_to_hstring(ctx, idx_value); /* coerce in-place */
  29916. DUK_ASSERT(h_text != NULL);
  29917. /* JSON parsing code is allowed to read [p_start,p_end]: p_end is
  29918. * valid and points to the string NUL terminator (which is always
  29919. * guaranteed for duk_hstrings.
  29920. */
  29921. js_ctx->p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_text);
  29922. js_ctx->p = js_ctx->p_start;
  29923. js_ctx->p_end = ((const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_text)) +
  29924. DUK_HSTRING_GET_BYTELEN(h_text);
  29925. DUK_ASSERT(*(js_ctx->p_end) == 0x00);
  29926. duk__dec_value(js_ctx); /* -> [ ... value ] */
  29927. /* Trailing whitespace has been eaten by duk__dec_value(), so if
  29928. * we're not at end of input here, it's a SyntaxError.
  29929. */
  29930. if (js_ctx->p != js_ctx->p_end) {
  29931. duk__dec_syntax_error(js_ctx);
  29932. }
  29933. if (duk_is_callable(ctx, idx_reviver)) {
  29934. DUK_DDD(DUK_DDDPRINT("applying reviver: %!T",
  29935. (duk_tval *) duk_get_tval(ctx, idx_reviver)));
  29936. js_ctx->idx_reviver = idx_reviver;
  29937. duk_push_object(ctx);
  29938. duk_dup(ctx, -2); /* -> [ ... val root val ] */
  29939. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_EMPTY_STRING); /* default attrs ok */
  29940. duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING); /* -> [ ... val root "" ] */
  29941. DUK_DDD(DUK_DDDPRINT("start reviver walk, root=%!T, name=%!T",
  29942. (duk_tval *) duk_get_tval(ctx, -2),
  29943. (duk_tval *) duk_get_tval(ctx, -1)));
  29944. duk__dec_reviver_walk(js_ctx); /* [ ... val root "" ] -> [ ... val val' ] */
  29945. duk_remove(ctx, -2); /* -> [ ... val' ] */
  29946. } else {
  29947. DUK_DDD(DUK_DDDPRINT("reviver does not exist or is not callable: %!T",
  29948. (duk_tval *) duk_get_tval(ctx, idx_reviver)));
  29949. }
  29950. /* Final result is at stack top. */
  29951. DUK_DDD(DUK_DDDPRINT("JSON parse end: text=%!T, reviver=%!T, flags=0x%08lx, result=%!T, stack_top=%ld",
  29952. (duk_tval *) duk_get_tval(ctx, idx_value),
  29953. (duk_tval *) duk_get_tval(ctx, idx_reviver),
  29954. (unsigned long) flags,
  29955. (duk_tval *) duk_get_tval(ctx, -1),
  29956. (long) duk_get_top(ctx)));
  29957. DUK_ASSERT(duk_get_top(ctx) == entry_top + 1);
  29958. }
  29959. DUK_INTERNAL
  29960. void duk_bi_json_stringify_helper(duk_context *ctx,
  29961. duk_idx_t idx_value,
  29962. duk_idx_t idx_replacer,
  29963. duk_idx_t idx_space,
  29964. duk_small_uint_t flags) {
  29965. duk_hthread *thr = (duk_hthread *) ctx;
  29966. duk_json_enc_ctx js_ctx_alloc;
  29967. duk_json_enc_ctx *js_ctx = &js_ctx_alloc;
  29968. duk_hobject *h;
  29969. duk_bool_t undef;
  29970. duk_idx_t idx_holder;
  29971. duk_idx_t entry_top;
  29972. /* negative top-relative indices not allowed now */
  29973. DUK_ASSERT(idx_value == DUK_INVALID_INDEX || idx_value >= 0);
  29974. DUK_ASSERT(idx_replacer == DUK_INVALID_INDEX || idx_replacer >= 0);
  29975. DUK_ASSERT(idx_space == DUK_INVALID_INDEX || idx_space >= 0);
  29976. DUK_DDD(DUK_DDDPRINT("JSON stringify start: value=%!T, replacer=%!T, space=%!T, flags=0x%08lx, stack_top=%ld",
  29977. (duk_tval *) duk_get_tval(ctx, idx_value),
  29978. (duk_tval *) duk_get_tval(ctx, idx_replacer),
  29979. (duk_tval *) duk_get_tval(ctx, idx_space),
  29980. (unsigned long) flags,
  29981. (long) duk_get_top(ctx)));
  29982. entry_top = duk_get_top(ctx);
  29983. /*
  29984. * Context init
  29985. */
  29986. DUK_MEMZERO(&js_ctx_alloc, sizeof(js_ctx_alloc));
  29987. js_ctx->thr = thr;
  29988. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  29989. js_ctx->h_replacer = NULL;
  29990. js_ctx->h_gap = NULL;
  29991. js_ctx->h_indent = NULL;
  29992. #endif
  29993. js_ctx->idx_proplist = -1;
  29994. /* Flag handling currently assumes that flags are consistent. This is OK
  29995. * because the call sites are now strictly controlled.
  29996. */
  29997. js_ctx->flags = flags;
  29998. js_ctx->flag_ascii_only = flags & DUK_JSON_FLAG_ASCII_ONLY;
  29999. js_ctx->flag_avoid_key_quotes = flags & DUK_JSON_FLAG_AVOID_KEY_QUOTES;
  30000. #ifdef DUK_USE_JX
  30001. js_ctx->flag_ext_custom = flags & DUK_JSON_FLAG_EXT_CUSTOM;
  30002. #endif
  30003. #ifdef DUK_USE_JC
  30004. js_ctx->flag_ext_compatible = flags & DUK_JSON_FLAG_EXT_COMPATIBLE;
  30005. #endif
  30006. /* The #ifdef clutter here handles the JX/JC enable/disable
  30007. * combinations properly.
  30008. */
  30009. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  30010. #if defined(DUK_USE_JX)
  30011. if (flags & DUK_JSON_FLAG_EXT_CUSTOM) {
  30012. js_ctx->stridx_custom_undefined = DUK_STRIDX_LC_UNDEFINED;
  30013. js_ctx->stridx_custom_nan = DUK_STRIDX_NAN;
  30014. js_ctx->stridx_custom_neginf = DUK_STRIDX_MINUS_INFINITY;
  30015. js_ctx->stridx_custom_posinf = DUK_STRIDX_INFINITY;
  30016. js_ctx->stridx_custom_function =
  30017. (flags & DUK_JSON_FLAG_AVOID_KEY_QUOTES) ?
  30018. DUK_STRIDX_JSON_EXT_FUNCTION2 :
  30019. DUK_STRIDX_JSON_EXT_FUNCTION1;
  30020. }
  30021. #endif /* DUK_USE_JX */
  30022. #if defined(DUK_USE_JX) && defined(DUK_USE_JC)
  30023. else
  30024. #endif /* DUK_USE_JX && DUK_USE_JC */
  30025. #if defined(DUK_USE_JC)
  30026. if (js_ctx->flags & DUK_JSON_FLAG_EXT_COMPATIBLE) {
  30027. js_ctx->stridx_custom_undefined = DUK_STRIDX_JSON_EXT_UNDEFINED;
  30028. js_ctx->stridx_custom_nan = DUK_STRIDX_JSON_EXT_NAN;
  30029. js_ctx->stridx_custom_neginf = DUK_STRIDX_JSON_EXT_NEGINF;
  30030. js_ctx->stridx_custom_posinf = DUK_STRIDX_JSON_EXT_POSINF;
  30031. js_ctx->stridx_custom_function = DUK_STRIDX_JSON_EXT_FUNCTION1;
  30032. }
  30033. #endif /* DUK_USE_JC */
  30034. #endif /* DUK_USE_JX || DUK_USE_JC */
  30035. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  30036. if (js_ctx->flags & (DUK_JSON_FLAG_EXT_CUSTOM |
  30037. DUK_JSON_FLAG_EXT_COMPATIBLE)) {
  30038. DUK_ASSERT(js_ctx->mask_for_undefined == 0); /* already zero */
  30039. }
  30040. else
  30041. #endif /* DUK_USE_JX || DUK_USE_JC */
  30042. {
  30043. js_ctx->mask_for_undefined = DUK_TYPE_MASK_UNDEFINED |
  30044. DUK_TYPE_MASK_POINTER |
  30045. DUK_TYPE_MASK_BUFFER |
  30046. DUK_TYPE_MASK_LIGHTFUNC;
  30047. }
  30048. DUK_BW_INIT_PUSHBUF(thr, &js_ctx->bw, DUK__JSON_STRINGIFY_BUFSIZE);
  30049. js_ctx->idx_loop = duk_push_object_internal(ctx);
  30050. DUK_ASSERT(js_ctx->idx_loop >= 0);
  30051. /* [ ... buf loop ] */
  30052. /*
  30053. * Process replacer/proplist (2nd argument to JSON.stringify)
  30054. */
  30055. h = duk_get_hobject(ctx, idx_replacer);
  30056. if (h != NULL) {
  30057. if (DUK_HOBJECT_IS_CALLABLE(h)) {
  30058. js_ctx->h_replacer = h;
  30059. } else if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY) {
  30060. /* Here the specification requires correct array index enumeration
  30061. * which is a bit tricky for sparse arrays (it is handled by the
  30062. * enum setup code). We now enumerate ancestors too, although the
  30063. * specification is not very clear on whether that is required.
  30064. */
  30065. duk_uarridx_t plist_idx = 0;
  30066. duk_small_uint_t enum_flags;
  30067. js_ctx->idx_proplist = duk_push_array(ctx); /* XXX: array internal? */
  30068. enum_flags = DUK_ENUM_ARRAY_INDICES_ONLY |
  30069. DUK_ENUM_SORT_ARRAY_INDICES; /* expensive flag */
  30070. duk_enum(ctx, idx_replacer, enum_flags);
  30071. while (duk_next(ctx, -1 /*enum_index*/, 1 /*get_value*/)) {
  30072. /* [ ... proplist enum_obj key val ] */
  30073. if (duk__enc_allow_into_proplist(duk_get_tval(ctx, -1))) {
  30074. /* XXX: duplicates should be eliminated here */
  30075. DUK_DDD(DUK_DDDPRINT("proplist enum: key=%!T, val=%!T --> accept",
  30076. (duk_tval *) duk_get_tval(ctx, -2),
  30077. (duk_tval *) duk_get_tval(ctx, -1)));
  30078. duk_to_string(ctx, -1); /* extra coercion of strings is OK */
  30079. duk_put_prop_index(ctx, -4, plist_idx); /* -> [ ... proplist enum_obj key ] */
  30080. plist_idx++;
  30081. duk_pop(ctx);
  30082. } else {
  30083. DUK_DDD(DUK_DDDPRINT("proplist enum: key=%!T, val=%!T --> reject",
  30084. (duk_tval *) duk_get_tval(ctx, -2),
  30085. (duk_tval *) duk_get_tval(ctx, -1)));
  30086. duk_pop_2(ctx);
  30087. }
  30088. }
  30089. duk_pop(ctx); /* pop enum */
  30090. /* [ ... proplist ] */
  30091. }
  30092. }
  30093. /* [ ... buf loop (proplist) ] */
  30094. /*
  30095. * Process space (3rd argument to JSON.stringify)
  30096. */
  30097. h = duk_get_hobject(ctx, idx_space);
  30098. if (h != NULL) {
  30099. int c = DUK_HOBJECT_GET_CLASS_NUMBER(h);
  30100. if (c == DUK_HOBJECT_CLASS_NUMBER) {
  30101. duk_to_number(ctx, idx_space);
  30102. } else if (c == DUK_HOBJECT_CLASS_STRING) {
  30103. duk_to_string(ctx, idx_space);
  30104. }
  30105. }
  30106. if (duk_is_number(ctx, idx_space)) {
  30107. duk_small_int_t nspace;
  30108. /* spaces[] must be static to allow initializer with old compilers like BCC */
  30109. static const char spaces[10] = {
  30110. DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE,
  30111. DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE,
  30112. DUK_ASC_SPACE, DUK_ASC_SPACE
  30113. }; /* XXX: helper */
  30114. /* ToInteger() coercion; NaN -> 0, infinities are clamped to 0 and 10 */
  30115. nspace = (duk_small_int_t) duk_to_int_clamped(ctx, idx_space, 0 /*minval*/, 10 /*maxval*/);
  30116. DUK_ASSERT(nspace >= 0 && nspace <= 10);
  30117. duk_push_lstring(ctx, spaces, (duk_size_t) nspace);
  30118. js_ctx->h_gap = duk_get_hstring(ctx, -1);
  30119. DUK_ASSERT(js_ctx->h_gap != NULL);
  30120. } else if (duk_is_string(ctx, idx_space)) {
  30121. /* XXX: substring in-place at idx_place? */
  30122. duk_dup(ctx, idx_space);
  30123. duk_substring(ctx, -1, 0, 10); /* clamp to 10 chars */
  30124. js_ctx->h_gap = duk_get_hstring(ctx, -1);
  30125. DUK_ASSERT(js_ctx->h_gap != NULL);
  30126. } else {
  30127. /* nop */
  30128. }
  30129. if (js_ctx->h_gap != NULL) {
  30130. /* if gap is empty, behave as if not given at all */
  30131. if (DUK_HSTRING_GET_CHARLEN(js_ctx->h_gap) == 0) {
  30132. js_ctx->h_gap = NULL;
  30133. } else {
  30134. /* set 'indent' only if it will actually increase */
  30135. js_ctx->h_indent = DUK_HTHREAD_STRING_EMPTY_STRING(thr);
  30136. }
  30137. }
  30138. DUK_ASSERT((js_ctx->h_gap == NULL && js_ctx->h_indent == NULL) ||
  30139. (js_ctx->h_gap != NULL && js_ctx->h_indent != NULL));
  30140. /* [ ... buf loop (proplist) (gap) ] */
  30141. /*
  30142. * Fast path: assume no mutation, iterate object property tables
  30143. * directly; bail out if that assumption doesn't hold.
  30144. */
  30145. #if defined(DUK_USE_JSON_STRINGIFY_FASTPATH)
  30146. /* For now fast path is limited to plain JSON (no JX/JC). This would
  30147. * be easy to fix but must go through value type handling in the fast
  30148. * path.
  30149. */
  30150. if (flags == 0 &&
  30151. js_ctx->h_replacer == NULL &&
  30152. js_ctx->idx_proplist == -1 &&
  30153. js_ctx->h_gap == NULL &&
  30154. js_ctx->h_indent == NULL) {
  30155. duk_int_t pcall_rc;
  30156. #ifdef DUK_USE_MARK_AND_SWEEP
  30157. duk_small_uint_t prev_mark_and_sweep_base_flags;
  30158. #endif
  30159. DUK_DD(DUK_DDPRINT("try JSON.stringify() fast path"));
  30160. /* Use recursion_limit to ensure we don't overwrite js_ctx->visiting[]
  30161. * array so we don't need two counter checks in the fast path. The
  30162. * slow path has a much larger recursion limit which we'll use if
  30163. * necessary.
  30164. */
  30165. DUK_ASSERT(DUK_USE_JSON_ENC_RECLIMIT >= DUK_JSON_ENC_LOOPARRAY);
  30166. js_ctx->recursion_limit = DUK_JSON_ENC_LOOPARRAY;
  30167. DUK_ASSERT(js_ctx->recursion_depth == 0);
  30168. /* Execute the fast path in a protected call. If any error is thrown,
  30169. * fall back to the slow path. This includes e.g. recursion limit
  30170. * because the fast path has a smaller recursion limit (and simpler,
  30171. * limited loop detection).
  30172. */
  30173. duk_push_pointer(ctx, (void *) js_ctx);
  30174. duk_dup(ctx, idx_value);
  30175. #if defined(DUK_USE_MARK_AND_SWEEP)
  30176. /* Must prevent finalizers which may have arbitrary side effects. */
  30177. prev_mark_and_sweep_base_flags = thr->heap->mark_and_sweep_base_flags;
  30178. thr->heap->mark_and_sweep_base_flags |=
  30179. DUK_MS_FLAG_NO_FINALIZERS | /* avoid attempts to add/remove object keys */
  30180. DUK_MS_FLAG_NO_OBJECT_COMPACTION; /* avoid attempt to compact any objects */
  30181. #endif
  30182. pcall_rc = duk_safe_call(ctx, duk__json_stringify_fast, 2 /*nargs*/, 0 /*nret*/);
  30183. #if defined(DUK_USE_MARK_AND_SWEEP)
  30184. thr->heap->mark_and_sweep_base_flags = prev_mark_and_sweep_base_flags;
  30185. #endif
  30186. if (pcall_rc == DUK_EXEC_SUCCESS) {
  30187. DUK_DD(DUK_DDPRINT("fast path successful"));
  30188. DUK_BW_PUSH_AS_STRING(thr, &js_ctx->bw);
  30189. goto replace_finished;
  30190. }
  30191. /* We come here for actual aborts (like encountering .toJSON())
  30192. * but also for recursion/loop errors. Bufwriter size can be
  30193. * kept because we'll probably need at least as much as we've
  30194. * allocated so far.
  30195. */
  30196. DUK_DD(DUK_DDPRINT("fast path failed, serialize using slow path instead"));
  30197. DUK_BW_RESET_SIZE(thr, &js_ctx->bw);
  30198. js_ctx->recursion_depth = 0;
  30199. }
  30200. #endif
  30201. /*
  30202. * Create wrapper object and serialize
  30203. */
  30204. idx_holder = duk_push_object(ctx);
  30205. duk_dup(ctx, idx_value);
  30206. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_EMPTY_STRING);
  30207. DUK_DDD(DUK_DDDPRINT("before: flags=0x%08lx, loop=%!T, replacer=%!O, "
  30208. "proplist=%!T, gap=%!O, indent=%!O, holder=%!T",
  30209. (unsigned long) js_ctx->flags,
  30210. (duk_tval *) duk_get_tval(ctx, js_ctx->idx_loop),
  30211. (duk_heaphdr *) js_ctx->h_replacer,
  30212. (duk_tval *) (js_ctx->idx_proplist >= 0 ? duk_get_tval(ctx, js_ctx->idx_proplist) : NULL),
  30213. (duk_heaphdr *) js_ctx->h_gap,
  30214. (duk_heaphdr *) js_ctx->h_indent,
  30215. (duk_tval *) duk_get_tval(ctx, -1)));
  30216. /* serialize the wrapper with empty string key */
  30217. duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING);
  30218. /* [ ... buf loop (proplist) (gap) holder "" ] */
  30219. js_ctx->recursion_limit = DUK_USE_JSON_ENC_RECLIMIT;
  30220. DUK_ASSERT(js_ctx->recursion_depth == 0);
  30221. undef = duk__enc_value1(js_ctx, idx_holder); /* [ ... holder key ] -> [ ... holder key val ] */
  30222. DUK_DDD(DUK_DDDPRINT("after: flags=0x%08lx, loop=%!T, replacer=%!O, "
  30223. "proplist=%!T, gap=%!O, indent=%!O, holder=%!T",
  30224. (unsigned long) js_ctx->flags,
  30225. (duk_tval *) duk_get_tval(ctx, js_ctx->idx_loop),
  30226. (duk_heaphdr *) js_ctx->h_replacer,
  30227. (duk_tval *) (js_ctx->idx_proplist >= 0 ? duk_get_tval(ctx, js_ctx->idx_proplist) : NULL),
  30228. (duk_heaphdr *) js_ctx->h_gap,
  30229. (duk_heaphdr *) js_ctx->h_indent,
  30230. (duk_tval *) duk_get_tval(ctx, -3)));
  30231. if (undef) {
  30232. /*
  30233. * Result is undefined
  30234. */
  30235. duk_push_undefined(ctx);
  30236. } else {
  30237. /*
  30238. * Finish and convert buffer to result string
  30239. */
  30240. duk__enc_value2(js_ctx); /* [ ... key val ] -> [ ... ] */
  30241. DUK_BW_PUSH_AS_STRING(thr, &js_ctx->bw);
  30242. }
  30243. /* The stack has a variable shape here, so force it to the
  30244. * desired one explicitly.
  30245. */
  30246. #if defined(DUK_USE_JSON_STRINGIFY_FASTPATH)
  30247. replace_finished:
  30248. #endif
  30249. duk_replace(ctx, entry_top);
  30250. duk_set_top(ctx, entry_top + 1);
  30251. DUK_DDD(DUK_DDDPRINT("JSON stringify end: value=%!T, replacer=%!T, space=%!T, "
  30252. "flags=0x%08lx, result=%!T, stack_top=%ld",
  30253. (duk_tval *) duk_get_tval(ctx, idx_value),
  30254. (duk_tval *) duk_get_tval(ctx, idx_replacer),
  30255. (duk_tval *) duk_get_tval(ctx, idx_space),
  30256. (unsigned long) flags,
  30257. (duk_tval *) duk_get_tval(ctx, -1),
  30258. (long) duk_get_top(ctx)));
  30259. DUK_ASSERT(duk_get_top(ctx) == entry_top + 1);
  30260. }
  30261. /*
  30262. * Entry points
  30263. */
  30264. DUK_INTERNAL duk_ret_t duk_bi_json_object_parse(duk_context *ctx) {
  30265. duk_bi_json_parse_helper(ctx,
  30266. 0 /*idx_value*/,
  30267. 1 /*idx_replacer*/,
  30268. 0 /*flags*/);
  30269. return 1;
  30270. }
  30271. DUK_INTERNAL duk_ret_t duk_bi_json_object_stringify(duk_context *ctx) {
  30272. duk_bi_json_stringify_helper(ctx,
  30273. 0 /*idx_value*/,
  30274. 1 /*idx_replacer*/,
  30275. 2 /*idx_space*/,
  30276. 0 /*flags*/);
  30277. return 1;
  30278. }
  30279. #undef DUK__JSON_DECSTR_BUFSIZE
  30280. #undef DUK__JSON_DECSTR_CHUNKSIZE
  30281. #undef DUK__JSON_ENCSTR_CHUNKSIZE
  30282. #undef DUK__JSON_STRINGIFY_BUFSIZE
  30283. #undef DUK__JSON_MAX_ESC_LEN
  30284. #line 1 "duk_bi_logger.c"
  30285. /*
  30286. * Logging support
  30287. */
  30288. /* include removed: duk_internal.h */
  30289. /* 3-letter log level strings */
  30290. DUK_LOCAL const duk_uint8_t duk__log_level_strings[] = {
  30291. (duk_uint8_t) DUK_ASC_UC_T, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_C,
  30292. (duk_uint8_t) DUK_ASC_UC_D, (duk_uint8_t) DUK_ASC_UC_B, (duk_uint8_t) DUK_ASC_UC_G,
  30293. (duk_uint8_t) DUK_ASC_UC_I, (duk_uint8_t) DUK_ASC_UC_N, (duk_uint8_t) DUK_ASC_UC_F,
  30294. (duk_uint8_t) DUK_ASC_UC_W, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_N,
  30295. (duk_uint8_t) DUK_ASC_UC_E, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_R,
  30296. (duk_uint8_t) DUK_ASC_UC_F, (duk_uint8_t) DUK_ASC_UC_T, (duk_uint8_t) DUK_ASC_UC_L
  30297. };
  30298. /* Constructor */
  30299. DUK_INTERNAL duk_ret_t duk_bi_logger_constructor(duk_context *ctx) {
  30300. duk_hthread *thr = (duk_hthread *) ctx;
  30301. duk_idx_t nargs;
  30302. /* Calling as a non-constructor is not meaningful. */
  30303. if (!duk_is_constructor_call(ctx)) {
  30304. return DUK_RET_TYPE_ERROR;
  30305. }
  30306. nargs = duk_get_top(ctx);
  30307. duk_set_top(ctx, 1);
  30308. duk_push_this(ctx);
  30309. /* [ name this ] */
  30310. if (nargs == 0) {
  30311. /* Automatic defaulting of logger name from caller. This would
  30312. * work poorly with tail calls, but constructor calls are currently
  30313. * never tail calls, so tail calls are not an issue now.
  30314. */
  30315. if (thr->callstack_top >= 2) {
  30316. duk_activation *act_caller = thr->callstack + thr->callstack_top - 2;
  30317. duk_hobject *func_caller;
  30318. func_caller = DUK_ACT_GET_FUNC(act_caller);
  30319. if (func_caller) {
  30320. /* Stripping the filename might be a good idea
  30321. * ("/foo/bar/quux.js" -> logger name "quux"),
  30322. * but now used verbatim.
  30323. */
  30324. duk_push_hobject(ctx, func_caller);
  30325. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_FILE_NAME);
  30326. duk_replace(ctx, 0);
  30327. }
  30328. }
  30329. }
  30330. /* the stack is unbalanced here on purpose; we only rely on the
  30331. * initial two values: [ name this ].
  30332. */
  30333. if (duk_is_string(ctx, 0)) {
  30334. duk_dup(ctx, 0);
  30335. duk_put_prop_stridx(ctx, 1, DUK_STRIDX_LC_N);
  30336. } else {
  30337. /* don't set 'n' at all, inherited value is used as name */
  30338. }
  30339. duk_compact(ctx, 1);
  30340. return 0; /* keep default instance */
  30341. }
  30342. /* Default function to format objects. Tries to use toLogString() but falls
  30343. * back to toString(). Any errors are propagated out without catching.
  30344. */
  30345. DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_fmt(duk_context *ctx) {
  30346. if (duk_get_prop_stridx(ctx, 0, DUK_STRIDX_TO_LOG_STRING)) {
  30347. /* [ arg toLogString ] */
  30348. duk_dup(ctx, 0);
  30349. duk_call_method(ctx, 0);
  30350. /* [ arg result ] */
  30351. return 1;
  30352. }
  30353. /* [ arg undefined ] */
  30354. duk_pop(ctx);
  30355. duk_to_string(ctx, 0);
  30356. return 1;
  30357. }
  30358. /* Default function to write a formatted log line. Writes to stderr,
  30359. * appending a newline to the log line.
  30360. *
  30361. * The argument is a buffer whose visible size contains the log message.
  30362. * This function should avoid coercing the buffer to a string to avoid
  30363. * string table traffic.
  30364. */
  30365. DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_raw(duk_context *ctx) {
  30366. const char *data;
  30367. duk_size_t data_len;
  30368. DUK_UNREF(ctx);
  30369. DUK_UNREF(data);
  30370. DUK_UNREF(data_len);
  30371. #ifdef DUK_USE_FILE_IO
  30372. data = (const char *) duk_require_buffer(ctx, 0, &data_len);
  30373. DUK_FWRITE((const void *) data, 1, data_len, DUK_STDERR);
  30374. DUK_FPUTC((int) '\n', DUK_STDERR);
  30375. DUK_FFLUSH(DUK_STDERR);
  30376. #else
  30377. /* nop */
  30378. #endif
  30379. return 0;
  30380. }
  30381. /* Log frontend shared helper, magic value indicates log level. Provides
  30382. * frontend functions: trace(), debug(), info(), warn(), error(), fatal().
  30383. * This needs to have small footprint, reasonable performance, minimal
  30384. * memory churn, etc.
  30385. */
  30386. DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_log_shared(duk_context *ctx) {
  30387. duk_hthread *thr = (duk_hthread *) ctx;
  30388. duk_double_t now;
  30389. duk_small_int_t entry_lev = duk_get_current_magic(ctx);
  30390. duk_small_int_t logger_lev;
  30391. duk_int_t nargs;
  30392. duk_int_t i;
  30393. duk_size_t tot_len;
  30394. const duk_uint8_t *arg_str;
  30395. duk_size_t arg_len;
  30396. duk_uint8_t *buf, *p;
  30397. const duk_uint8_t *q;
  30398. duk_uint8_t date_buf[DUK_BI_DATE_ISO8601_BUFSIZE];
  30399. duk_size_t date_len;
  30400. duk_small_int_t rc;
  30401. DUK_ASSERT(entry_lev >= 0 && entry_lev <= 5);
  30402. DUK_UNREF(thr);
  30403. /* XXX: sanitize to printable (and maybe ASCII) */
  30404. /* XXX: better multiline */
  30405. /*
  30406. * Logger arguments are:
  30407. *
  30408. * magic: log level (0-5)
  30409. * this: logger
  30410. * stack: plain log args
  30411. *
  30412. * We want to minimize memory churn so a two-pass approach
  30413. * is used: first pass formats arguments and computes final
  30414. * string length, second pass copies strings either into a
  30415. * pre-allocated and reused buffer (short messages) or into a
  30416. * newly allocated fixed buffer. If the backend function plays
  30417. * nice, it won't coerce the buffer to a string (and thus
  30418. * intern it).
  30419. */
  30420. nargs = duk_get_top(ctx);
  30421. /* [ arg1 ... argN this ] */
  30422. /*
  30423. * Log level check
  30424. */
  30425. duk_push_this(ctx);
  30426. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LC_L);
  30427. logger_lev = (duk_small_int_t) duk_get_int(ctx, -1);
  30428. if (entry_lev < logger_lev) {
  30429. return 0;
  30430. }
  30431. /* log level could be popped but that's not necessary */
  30432. now = DUK_USE_DATE_GET_NOW(ctx);
  30433. duk_bi_date_format_timeval(now, date_buf);
  30434. date_len = DUK_STRLEN((const char *) date_buf);
  30435. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_LC_N);
  30436. duk_to_string(ctx, -1);
  30437. DUK_ASSERT(duk_is_string(ctx, -1));
  30438. /* [ arg1 ... argN this loggerLevel loggerName ] */
  30439. /*
  30440. * Pass 1
  30441. */
  30442. /* Line format: <time> <entryLev> <loggerName>: <msg> */
  30443. tot_len = 0;
  30444. tot_len += 3 + /* separators: space, space, colon */
  30445. 3 + /* level string */
  30446. date_len + /* time */
  30447. duk_get_length(ctx, -1); /* loggerName */
  30448. for (i = 0; i < nargs; i++) {
  30449. /* When formatting an argument to a string, errors may happen from multiple
  30450. * causes. In general we want to catch obvious errors like a toLogString()
  30451. * throwing an error, but we don't currently try to catch every possible
  30452. * error. In particular, internal errors (like out of memory or stack) are
  30453. * not caught. Also, we expect Error toString() to not throw an error.
  30454. */
  30455. if (duk_is_object(ctx, i)) {
  30456. /* duk_pcall_prop() may itself throw an error, but we're content
  30457. * in catching the obvious errors (like toLogString() throwing an
  30458. * error).
  30459. */
  30460. duk_push_hstring_stridx(ctx, DUK_STRIDX_FMT);
  30461. duk_dup(ctx, i);
  30462. /* [ arg1 ... argN this loggerLevel loggerName 'fmt' arg ] */
  30463. /* call: this.fmt(arg) */
  30464. rc = duk_pcall_prop(ctx, -5 /*obj_index*/, 1 /*nargs*/);
  30465. if (rc) {
  30466. /* Keep the error as the result (coercing it might fail below,
  30467. * but we don't catch that now).
  30468. */
  30469. ;
  30470. }
  30471. duk_replace(ctx, i);
  30472. }
  30473. (void) duk_to_lstring(ctx, i, &arg_len);
  30474. tot_len++; /* sep (even before first one) */
  30475. tot_len += arg_len;
  30476. }
  30477. /*
  30478. * Pass 2
  30479. */
  30480. /* XXX: There used to be a shared log buffer here, but it was removed
  30481. * when dynamic buffer spare was removed. The problem with using
  30482. * bufwriter is that, without the spare, the buffer gets passed on
  30483. * as an argument to the raw() call so it'd need to be resized
  30484. * (reallocated) anyway. If raw() call convention is changed, this
  30485. * could be made more efficient.
  30486. */
  30487. buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, tot_len);
  30488. DUK_ASSERT(buf != NULL);
  30489. p = buf;
  30490. DUK_MEMCPY((void *) p, (const void *) date_buf, (size_t) date_len);
  30491. p += date_len;
  30492. *p++ = (duk_uint8_t) DUK_ASC_SPACE;
  30493. q = duk__log_level_strings + (entry_lev * 3);
  30494. DUK_MEMCPY((void *) p, (const void *) q, (size_t) 3);
  30495. p += 3;
  30496. *p++ = (duk_uint8_t) DUK_ASC_SPACE;
  30497. arg_str = (const duk_uint8_t *) duk_get_lstring(ctx, -2, &arg_len);
  30498. DUK_MEMCPY((void *) p, (const void *) arg_str, (size_t) arg_len);
  30499. p += arg_len;
  30500. *p++ = (duk_uint8_t) DUK_ASC_COLON;
  30501. for (i = 0; i < nargs; i++) {
  30502. *p++ = (duk_uint8_t) DUK_ASC_SPACE;
  30503. arg_str = (const duk_uint8_t *) duk_get_lstring(ctx, i, &arg_len);
  30504. DUK_ASSERT(arg_str != NULL);
  30505. DUK_MEMCPY((void *) p, (const void *) arg_str, (size_t) arg_len);
  30506. p += arg_len;
  30507. }
  30508. DUK_ASSERT(buf + tot_len == p);
  30509. /* [ arg1 ... argN this loggerLevel loggerName buffer ] */
  30510. #if defined(DUK_USE_DEBUGGER_SUPPORT) && defined(DUK_USE_DEBUGGER_FWD_LOGGING)
  30511. /* Do debugger forwarding before raw() because the raw() function
  30512. * doesn't get the log level right now.
  30513. */
  30514. if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) {
  30515. const char *log_buf;
  30516. duk_size_t sz_buf;
  30517. log_buf = (const char *) duk_get_buffer(ctx, -1, &sz_buf);
  30518. DUK_ASSERT(log_buf != NULL);
  30519. duk_debug_write_notify(thr, DUK_DBG_CMD_LOG);
  30520. duk_debug_write_int(thr, (duk_int32_t) entry_lev);
  30521. duk_debug_write_string(thr, (const char *) log_buf, sz_buf);
  30522. duk_debug_write_eom(thr);
  30523. }
  30524. #endif
  30525. /* Call this.raw(msg); look up through the instance allows user to override
  30526. * the raw() function in the instance or in the prototype for maximum
  30527. * flexibility.
  30528. */
  30529. duk_push_hstring_stridx(ctx, DUK_STRIDX_RAW);
  30530. duk_dup(ctx, -2);
  30531. /* [ arg1 ... argN this loggerLevel loggerName buffer 'raw' buffer ] */
  30532. duk_call_prop(ctx, -6, 1); /* this.raw(buffer) */
  30533. return 0;
  30534. }
  30535. #line 1 "duk_bi_math.c"
  30536. /*
  30537. * Math built-ins
  30538. */
  30539. /* include removed: duk_internal.h */
  30540. #if defined(DUK_USE_MATH_BUILTIN)
  30541. /*
  30542. * Use static helpers which can work with math.h functions matching
  30543. * the following signatures. This is not portable if any of these math
  30544. * functions is actually a macro.
  30545. *
  30546. * Typing here is intentionally 'double' wherever values interact with
  30547. * the standard library APIs.
  30548. */
  30549. typedef double (*duk__one_arg_func)(double);
  30550. typedef double (*duk__two_arg_func)(double, double);
  30551. DUK_LOCAL duk_ret_t duk__math_minmax(duk_context *ctx, duk_double_t initial, duk__two_arg_func min_max) {
  30552. duk_idx_t n = duk_get_top(ctx);
  30553. duk_idx_t i;
  30554. duk_double_t res = initial;
  30555. duk_double_t t;
  30556. /*
  30557. * Note: fmax() does not match the E5 semantics. E5 requires
  30558. * that if -any- input to Math.max() is a NaN, the result is a
  30559. * NaN. fmax() will return a NaN only if -both- inputs are NaN.
  30560. * Same applies to fmin().
  30561. *
  30562. * Note: every input value must be coerced with ToNumber(), even
  30563. * if we know the result will be a NaN anyway: ToNumber() may have
  30564. * side effects for which even order of evaluation matters.
  30565. */
  30566. for (i = 0; i < n; i++) {
  30567. t = duk_to_number(ctx, i);
  30568. if (DUK_FPCLASSIFY(t) == DUK_FP_NAN || DUK_FPCLASSIFY(res) == DUK_FP_NAN) {
  30569. /* Note: not normalized, but duk_push_number() will normalize */
  30570. res = (duk_double_t) DUK_DOUBLE_NAN;
  30571. } else {
  30572. res = (duk_double_t) min_max(res, (double) t);
  30573. }
  30574. }
  30575. duk_push_number(ctx, res);
  30576. return 1;
  30577. }
  30578. DUK_LOCAL double duk__fmin_fixed(double x, double y) {
  30579. /* fmin() with args -0 and +0 is not guaranteed to return
  30580. * -0 as Ecmascript requires.
  30581. */
  30582. if (x == 0 && y == 0) {
  30583. /* XXX: what's the safest way of creating a negative zero? */
  30584. if (DUK_SIGNBIT(x) != 0 || DUK_SIGNBIT(y) != 0) {
  30585. return -0.0;
  30586. } else {
  30587. return +0.0;
  30588. }
  30589. }
  30590. #ifdef DUK_USE_MATH_FMIN
  30591. return DUK_FMIN(x, y);
  30592. #else
  30593. return (x < y ? x : y);
  30594. #endif
  30595. }
  30596. DUK_LOCAL double duk__fmax_fixed(double x, double y) {
  30597. /* fmax() with args -0 and +0 is not guaranteed to return
  30598. * +0 as Ecmascript requires.
  30599. */
  30600. if (x == 0 && y == 0) {
  30601. if (DUK_SIGNBIT(x) == 0 || DUK_SIGNBIT(y) == 0) {
  30602. return +0.0;
  30603. } else {
  30604. return -0.0;
  30605. }
  30606. }
  30607. #ifdef DUK_USE_MATH_FMAX
  30608. return DUK_FMAX(x, y);
  30609. #else
  30610. return (x > y ? x : y);
  30611. #endif
  30612. }
  30613. DUK_LOCAL double duk__round_fixed(double x) {
  30614. /* Numbers half-way between integers must be rounded towards +Infinity,
  30615. * e.g. -3.5 must be rounded to -3 (not -4). When rounded to zero, zero
  30616. * sign must be set appropriately. E5.1 Section 15.8.2.15.
  30617. *
  30618. * Note that ANSI C round() is "round to nearest integer, away from zero",
  30619. * which is incorrect for negative values. Here we make do with floor().
  30620. */
  30621. duk_small_int_t c = (duk_small_int_t) DUK_FPCLASSIFY(x);
  30622. if (c == DUK_FP_NAN || c == DUK_FP_INFINITE || c == DUK_FP_ZERO) {
  30623. return x;
  30624. }
  30625. /*
  30626. * x is finite and non-zero
  30627. *
  30628. * -1.6 -> floor(-1.1) -> -2
  30629. * -1.5 -> floor(-1.0) -> -1 (towards +Inf)
  30630. * -1.4 -> floor(-0.9) -> -1
  30631. * -0.5 -> -0.0 (special case)
  30632. * -0.1 -> -0.0 (special case)
  30633. * +0.1 -> +0.0 (special case)
  30634. * +0.5 -> floor(+1.0) -> 1 (towards +Inf)
  30635. * +1.4 -> floor(+1.9) -> 1
  30636. * +1.5 -> floor(+2.0) -> 2 (towards +Inf)
  30637. * +1.6 -> floor(+2.1) -> 2
  30638. */
  30639. if (x >= -0.5 && x < 0.5) {
  30640. /* +0.5 is handled by floor, this is on purpose */
  30641. if (x < 0.0) {
  30642. return -0.0;
  30643. } else {
  30644. return +0.0;
  30645. }
  30646. }
  30647. return DUK_FLOOR(x + 0.5);
  30648. }
  30649. DUK_LOCAL double duk__pow_fixed(double x, double y) {
  30650. /* The ANSI C pow() semantics differ from Ecmascript.
  30651. *
  30652. * E.g. when x==1 and y is +/- infinite, the Ecmascript required
  30653. * result is NaN, while at least Linux pow() returns 1.
  30654. */
  30655. duk_small_int_t cx, cy, sx;
  30656. DUK_UNREF(cx);
  30657. DUK_UNREF(sx);
  30658. cy = (duk_small_int_t) DUK_FPCLASSIFY(y);
  30659. if (cy == DUK_FP_NAN) {
  30660. goto ret_nan;
  30661. }
  30662. if (DUK_FABS(x) == 1.0 && cy == DUK_FP_INFINITE) {
  30663. goto ret_nan;
  30664. }
  30665. #if defined(DUK_USE_POW_NETBSD_WORKAROUND)
  30666. /* See test-bug-netbsd-math-pow.js: NetBSD 6.0 on x86 (at least) does not
  30667. * correctly handle some cases where x=+/-0. Specific fixes to these
  30668. * here.
  30669. */
  30670. cx = (duk_small_int_t) DUK_FPCLASSIFY(x);
  30671. if (cx == DUK_FP_ZERO && y < 0.0) {
  30672. sx = (duk_small_int_t) DUK_SIGNBIT(x);
  30673. if (sx == 0) {
  30674. /* Math.pow(+0,y) should be Infinity when y<0. NetBSD pow()
  30675. * returns -Infinity instead when y is <0 and finite. The
  30676. * if-clause also catches y == -Infinity (which works even
  30677. * without the fix).
  30678. */
  30679. return DUK_DOUBLE_INFINITY;
  30680. } else {
  30681. /* Math.pow(-0,y) where y<0 should be:
  30682. * - -Infinity if y<0 and an odd integer
  30683. * - Infinity otherwise
  30684. * NetBSD pow() returns -Infinity for all finite y<0. The
  30685. * if-clause also catches y == -Infinity (which works even
  30686. * without the fix).
  30687. */
  30688. /* fmod() return value has same sign as input (negative) so
  30689. * the result here will be in the range ]-2,0], 1 indicates
  30690. * odd. If x is -Infinity, NaN is returned and the odd check
  30691. * always concludes "not odd" which results in desired outcome.
  30692. */
  30693. double tmp = DUK_FMOD(y, 2);
  30694. if (tmp == -1.0) {
  30695. return -DUK_DOUBLE_INFINITY;
  30696. } else {
  30697. /* Not odd, or y == -Infinity */
  30698. return DUK_DOUBLE_INFINITY;
  30699. }
  30700. }
  30701. }
  30702. #endif
  30703. return DUK_POW(x, y);
  30704. ret_nan:
  30705. return DUK_DOUBLE_NAN;
  30706. }
  30707. /* Wrappers for calling standard math library methods. These may be required
  30708. * on platforms where one or more of the math built-ins are defined as macros
  30709. * or inline functions and are thus not suitable to be used as function pointers.
  30710. */
  30711. #if defined(DUK_USE_AVOID_PLATFORM_FUNCPTRS)
  30712. DUK_LOCAL double duk__fabs(double x) {
  30713. return DUK_FABS(x);
  30714. }
  30715. DUK_LOCAL double duk__acos(double x) {
  30716. return DUK_ACOS(x);
  30717. }
  30718. DUK_LOCAL double duk__asin(double x) {
  30719. return DUK_ASIN(x);
  30720. }
  30721. DUK_LOCAL double duk__atan(double x) {
  30722. return DUK_ATAN(x);
  30723. }
  30724. DUK_LOCAL double duk__ceil(double x) {
  30725. return DUK_CEIL(x);
  30726. }
  30727. DUK_LOCAL double duk__cos(double x) {
  30728. return DUK_COS(x);
  30729. }
  30730. DUK_LOCAL double duk__exp(double x) {
  30731. return DUK_EXP(x);
  30732. }
  30733. DUK_LOCAL double duk__floor(double x) {
  30734. return DUK_FLOOR(x);
  30735. }
  30736. DUK_LOCAL double duk__log(double x) {
  30737. return DUK_LOG(x);
  30738. }
  30739. DUK_LOCAL double duk__sin(double x) {
  30740. return DUK_SIN(x);
  30741. }
  30742. DUK_LOCAL double duk__sqrt(double x) {
  30743. return DUK_SQRT(x);
  30744. }
  30745. DUK_LOCAL double duk__tan(double x) {
  30746. return DUK_TAN(x);
  30747. }
  30748. DUK_LOCAL double duk__atan2(double x, double y) {
  30749. return DUK_ATAN2(x, y);
  30750. }
  30751. #endif /* DUK_USE_AVOID_PLATFORM_FUNCPTRS */
  30752. /* order must match constants in genbuiltins.py */
  30753. DUK_LOCAL const duk__one_arg_func duk__one_arg_funcs[] = {
  30754. #if defined(DUK_USE_AVOID_PLATFORM_FUNCPTRS)
  30755. duk__fabs,
  30756. duk__acos,
  30757. duk__asin,
  30758. duk__atan,
  30759. duk__ceil,
  30760. duk__cos,
  30761. duk__exp,
  30762. duk__floor,
  30763. duk__log,
  30764. duk__round_fixed,
  30765. duk__sin,
  30766. duk__sqrt,
  30767. duk__tan
  30768. #else
  30769. DUK_FABS,
  30770. DUK_ACOS,
  30771. DUK_ASIN,
  30772. DUK_ATAN,
  30773. DUK_CEIL,
  30774. DUK_COS,
  30775. DUK_EXP,
  30776. DUK_FLOOR,
  30777. DUK_LOG,
  30778. duk__round_fixed,
  30779. DUK_SIN,
  30780. DUK_SQRT,
  30781. DUK_TAN
  30782. #endif
  30783. };
  30784. /* order must match constants in genbuiltins.py */
  30785. DUK_LOCAL const duk__two_arg_func duk__two_arg_funcs[] = {
  30786. #if defined(DUK_USE_AVOID_PLATFORM_FUNCPTRS)
  30787. duk__atan2,
  30788. duk__pow_fixed
  30789. #else
  30790. DUK_ATAN2,
  30791. duk__pow_fixed
  30792. #endif
  30793. };
  30794. DUK_INTERNAL duk_ret_t duk_bi_math_object_onearg_shared(duk_context *ctx) {
  30795. duk_small_int_t fun_idx = duk_get_current_magic(ctx);
  30796. duk__one_arg_func fun;
  30797. DUK_ASSERT(fun_idx >= 0);
  30798. DUK_ASSERT(fun_idx < (duk_small_int_t) (sizeof(duk__one_arg_funcs) / sizeof(duk__one_arg_func)));
  30799. fun = duk__one_arg_funcs[fun_idx];
  30800. duk_push_number(ctx, (duk_double_t) fun((double) duk_to_number(ctx, 0)));
  30801. return 1;
  30802. }
  30803. DUK_INTERNAL duk_ret_t duk_bi_math_object_twoarg_shared(duk_context *ctx) {
  30804. duk_small_int_t fun_idx = duk_get_current_magic(ctx);
  30805. duk__two_arg_func fun;
  30806. DUK_ASSERT(fun_idx >= 0);
  30807. DUK_ASSERT(fun_idx < (duk_small_int_t) (sizeof(duk__two_arg_funcs) / sizeof(duk__two_arg_func)));
  30808. fun = duk__two_arg_funcs[fun_idx];
  30809. duk_push_number(ctx, (duk_double_t) fun((double) duk_to_number(ctx, 0), (double) duk_to_number(ctx, 1)));
  30810. return 1;
  30811. }
  30812. DUK_INTERNAL duk_ret_t duk_bi_math_object_max(duk_context *ctx) {
  30813. return duk__math_minmax(ctx, -DUK_DOUBLE_INFINITY, duk__fmax_fixed);
  30814. }
  30815. DUK_INTERNAL duk_ret_t duk_bi_math_object_min(duk_context *ctx) {
  30816. return duk__math_minmax(ctx, DUK_DOUBLE_INFINITY, duk__fmin_fixed);
  30817. }
  30818. DUK_INTERNAL duk_ret_t duk_bi_math_object_random(duk_context *ctx) {
  30819. duk_push_number(ctx, (duk_double_t) duk_util_tinyrandom_get_double((duk_hthread *) ctx));
  30820. return 1;
  30821. }
  30822. #else /* DUK_USE_MATH_BUILTIN */
  30823. /* A stubbed built-in is useful for e.g. compilation torture testing with BCC. */
  30824. DUK_INTERNAL duk_ret_t duk_bi_math_object_onearg_shared(duk_context *ctx) {
  30825. DUK_UNREF(ctx);
  30826. return DUK_RET_UNIMPLEMENTED_ERROR;
  30827. }
  30828. DUK_INTERNAL duk_ret_t duk_bi_math_object_twoarg_shared(duk_context *ctx) {
  30829. DUK_UNREF(ctx);
  30830. return DUK_RET_UNIMPLEMENTED_ERROR;
  30831. }
  30832. DUK_INTERNAL duk_ret_t duk_bi_math_object_max(duk_context *ctx) {
  30833. DUK_UNREF(ctx);
  30834. return DUK_RET_UNIMPLEMENTED_ERROR;
  30835. }
  30836. DUK_INTERNAL duk_ret_t duk_bi_math_object_min(duk_context *ctx) {
  30837. DUK_UNREF(ctx);
  30838. return DUK_RET_UNIMPLEMENTED_ERROR;
  30839. }
  30840. DUK_INTERNAL duk_ret_t duk_bi_math_object_random(duk_context *ctx) {
  30841. DUK_UNREF(ctx);
  30842. return DUK_RET_UNIMPLEMENTED_ERROR;
  30843. }
  30844. #endif /* DUK_USE_MATH_BUILTIN */
  30845. #line 1 "duk_bi_number.c"
  30846. /*
  30847. * Number built-ins
  30848. */
  30849. /* include removed: duk_internal.h */
  30850. DUK_LOCAL duk_double_t duk__push_this_number_plain(duk_context *ctx) {
  30851. duk_hobject *h;
  30852. /* Number built-in accepts a plain number or a Number object (whose
  30853. * internal value is operated on). Other types cause TypeError.
  30854. */
  30855. duk_push_this(ctx);
  30856. if (duk_is_number(ctx, -1)) {
  30857. DUK_DDD(DUK_DDDPRINT("plain number value: %!T", (duk_tval *) duk_get_tval(ctx, -1)));
  30858. goto done;
  30859. }
  30860. h = duk_get_hobject(ctx, -1);
  30861. if (!h ||
  30862. (DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_NUMBER)) {
  30863. DUK_DDD(DUK_DDDPRINT("unacceptable this value: %!T", (duk_tval *) duk_get_tval(ctx, -1)));
  30864. DUK_ERROR((duk_hthread *) ctx, DUK_ERR_TYPE_ERROR, "expected a number");
  30865. }
  30866. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE);
  30867. DUK_ASSERT(duk_is_number(ctx, -1));
  30868. DUK_DDD(DUK_DDDPRINT("number object: %!T, internal value: %!T",
  30869. (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1)));
  30870. duk_remove(ctx, -2);
  30871. done:
  30872. return duk_get_number(ctx, -1);
  30873. }
  30874. DUK_INTERNAL duk_ret_t duk_bi_number_constructor(duk_context *ctx) {
  30875. duk_hthread *thr = (duk_hthread *) ctx;
  30876. duk_idx_t nargs;
  30877. duk_hobject *h_this;
  30878. DUK_UNREF(thr);
  30879. /*
  30880. * The Number constructor uses ToNumber(arg) for number coercion
  30881. * (coercing an undefined argument to NaN). However, if the
  30882. * argument is not given at all, +0 must be used instead. To do
  30883. * this, a vararg function is used.
  30884. */
  30885. nargs = duk_get_top(ctx);
  30886. if (nargs == 0) {
  30887. duk_push_int(ctx, 0);
  30888. }
  30889. duk_to_number(ctx, 0);
  30890. duk_set_top(ctx, 1);
  30891. DUK_ASSERT_TOP(ctx, 1);
  30892. if (!duk_is_constructor_call(ctx)) {
  30893. return 1;
  30894. }
  30895. /*
  30896. * E5 Section 15.7.2.1 requires that the constructed object
  30897. * must have the original Number.prototype as its internal
  30898. * prototype. However, since Number.prototype is non-writable
  30899. * and non-configurable, this doesn't have to be enforced here:
  30900. * The default object (bound to 'this') is OK, though we have
  30901. * to change its class.
  30902. *
  30903. * Internal value set to ToNumber(arg) or +0; if no arg given,
  30904. * ToNumber(undefined) = NaN, so special treatment is needed
  30905. * (above). String internal value is immutable.
  30906. */
  30907. /* XXX: helper */
  30908. duk_push_this(ctx);
  30909. h_this = duk_get_hobject(ctx, -1);
  30910. DUK_ASSERT(h_this != NULL);
  30911. DUK_HOBJECT_SET_CLASS_NUMBER(h_this, DUK_HOBJECT_CLASS_NUMBER);
  30912. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_this) == thr->builtins[DUK_BIDX_NUMBER_PROTOTYPE]);
  30913. DUK_ASSERT(DUK_HOBJECT_GET_CLASS_NUMBER(h_this) == DUK_HOBJECT_CLASS_NUMBER);
  30914. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(h_this));
  30915. duk_dup(ctx, 0); /* -> [ val obj val ] */
  30916. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE);
  30917. return 0; /* no return value -> don't replace created value */
  30918. }
  30919. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_value_of(duk_context *ctx) {
  30920. (void) duk__push_this_number_plain(ctx);
  30921. return 1;
  30922. }
  30923. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_string(duk_context *ctx) {
  30924. duk_small_int_t radix;
  30925. duk_small_uint_t n2s_flags;
  30926. (void) duk__push_this_number_plain(ctx);
  30927. if (duk_is_undefined(ctx, 0)) {
  30928. radix = 10;
  30929. } else {
  30930. radix = (duk_small_int_t) duk_to_int_check_range(ctx, 0, 2, 36);
  30931. }
  30932. DUK_DDD(DUK_DDDPRINT("radix=%ld", (long) radix));
  30933. n2s_flags = 0;
  30934. duk_numconv_stringify(ctx,
  30935. radix /*radix*/,
  30936. 0 /*digits*/,
  30937. n2s_flags /*flags*/);
  30938. return 1;
  30939. }
  30940. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_locale_string(duk_context *ctx) {
  30941. /* XXX: just use toString() for now; permitted although not recommended.
  30942. * nargs==1, so radix is passed to toString().
  30943. */
  30944. return duk_bi_number_prototype_to_string(ctx);
  30945. }
  30946. /*
  30947. * toFixed(), toExponential(), toPrecision()
  30948. */
  30949. /* XXX: shared helper for toFixed(), toExponential(), toPrecision()? */
  30950. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_fixed(duk_context *ctx) {
  30951. duk_small_int_t frac_digits;
  30952. duk_double_t d;
  30953. duk_small_int_t c;
  30954. duk_small_uint_t n2s_flags;
  30955. frac_digits = (duk_small_int_t) duk_to_int_check_range(ctx, 0, 0, 20);
  30956. d = duk__push_this_number_plain(ctx);
  30957. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  30958. if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) {
  30959. goto use_to_string;
  30960. }
  30961. if (d >= 1.0e21 || d <= -1.0e21) {
  30962. goto use_to_string;
  30963. }
  30964. n2s_flags = DUK_N2S_FLAG_FIXED_FORMAT |
  30965. DUK_N2S_FLAG_FRACTION_DIGITS;
  30966. duk_numconv_stringify(ctx,
  30967. 10 /*radix*/,
  30968. frac_digits /*digits*/,
  30969. n2s_flags /*flags*/);
  30970. return 1;
  30971. use_to_string:
  30972. DUK_ASSERT_TOP(ctx, 2);
  30973. duk_to_string(ctx, -1);
  30974. return 1;
  30975. }
  30976. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_exponential(duk_context *ctx) {
  30977. duk_bool_t frac_undefined;
  30978. duk_small_int_t frac_digits;
  30979. duk_double_t d;
  30980. duk_small_int_t c;
  30981. duk_small_uint_t n2s_flags;
  30982. d = duk__push_this_number_plain(ctx);
  30983. frac_undefined = duk_is_undefined(ctx, 0);
  30984. duk_to_int(ctx, 0); /* for side effects */
  30985. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  30986. if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) {
  30987. goto use_to_string;
  30988. }
  30989. frac_digits = (duk_small_int_t) duk_to_int_check_range(ctx, 0, 0, 20);
  30990. n2s_flags = DUK_N2S_FLAG_FORCE_EXP |
  30991. (frac_undefined ? 0 : DUK_N2S_FLAG_FIXED_FORMAT);
  30992. duk_numconv_stringify(ctx,
  30993. 10 /*radix*/,
  30994. frac_digits + 1 /*leading digit + fractions*/,
  30995. n2s_flags /*flags*/);
  30996. return 1;
  30997. use_to_string:
  30998. DUK_ASSERT_TOP(ctx, 2);
  30999. duk_to_string(ctx, -1);
  31000. return 1;
  31001. }
  31002. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_precision(duk_context *ctx) {
  31003. /* The specification has quite awkward order of coercion and
  31004. * checks for toPrecision(). The operations below are a bit
  31005. * reordered, within constraints of observable side effects.
  31006. */
  31007. duk_double_t d;
  31008. duk_small_int_t prec;
  31009. duk_small_int_t c;
  31010. duk_small_uint_t n2s_flags;
  31011. DUK_ASSERT_TOP(ctx, 1);
  31012. d = duk__push_this_number_plain(ctx);
  31013. if (duk_is_undefined(ctx, 0)) {
  31014. goto use_to_string;
  31015. }
  31016. DUK_ASSERT_TOP(ctx, 2);
  31017. duk_to_int(ctx, 0); /* for side effects */
  31018. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  31019. if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) {
  31020. goto use_to_string;
  31021. }
  31022. prec = (duk_small_int_t) duk_to_int_check_range(ctx, 0, 1, 21);
  31023. n2s_flags = DUK_N2S_FLAG_FIXED_FORMAT |
  31024. DUK_N2S_FLAG_NO_ZERO_PAD;
  31025. duk_numconv_stringify(ctx,
  31026. 10 /*radix*/,
  31027. prec /*digits*/,
  31028. n2s_flags /*flags*/);
  31029. return 1;
  31030. use_to_string:
  31031. /* Used when precision is undefined; also used for NaN (-> "NaN"),
  31032. * and +/- infinity (-> "Infinity", "-Infinity").
  31033. */
  31034. DUK_ASSERT_TOP(ctx, 2);
  31035. duk_to_string(ctx, -1);
  31036. return 1;
  31037. }
  31038. #line 1 "duk_bi_object.c"
  31039. /*
  31040. * Object built-ins
  31041. */
  31042. /* include removed: duk_internal.h */
  31043. DUK_INTERNAL duk_ret_t duk_bi_object_constructor(duk_context *ctx) {
  31044. if (!duk_is_constructor_call(ctx) &&
  31045. !duk_is_null_or_undefined(ctx, 0)) {
  31046. duk_to_object(ctx, 0);
  31047. return 1;
  31048. }
  31049. if (duk_is_object(ctx, 0)) {
  31050. return 1;
  31051. }
  31052. /* Pointer and buffer primitive values are treated like other
  31053. * primitives values which have a fully fledged object counterpart:
  31054. * promote to an object value. Lightfuncs are coerced with
  31055. * ToObject() even they could also be returned as is.
  31056. */
  31057. if (duk_check_type_mask(ctx, 0, DUK_TYPE_MASK_STRING |
  31058. DUK_TYPE_MASK_BOOLEAN |
  31059. DUK_TYPE_MASK_NUMBER |
  31060. DUK_TYPE_MASK_POINTER |
  31061. DUK_TYPE_MASK_BUFFER |
  31062. DUK_TYPE_MASK_LIGHTFUNC)) {
  31063. duk_to_object(ctx, 0);
  31064. return 1;
  31065. }
  31066. duk_push_object_helper(ctx,
  31067. DUK_HOBJECT_FLAG_EXTENSIBLE |
  31068. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  31069. DUK_BIDX_OBJECT_PROTOTYPE);
  31070. return 1;
  31071. }
  31072. /* Shared helper to implement Object.getPrototypeOf and the ES6
  31073. * Object.prototype.__proto__ getter.
  31074. *
  31075. * http://www.ecma-international.org/ecma-262/6.0/index.html#sec-get-object.prototype.__proto__
  31076. */
  31077. DUK_INTERNAL duk_ret_t duk_bi_object_getprototype_shared(duk_context *ctx) {
  31078. duk_hthread *thr = (duk_hthread *) ctx;
  31079. duk_hobject *h;
  31080. duk_hobject *proto;
  31081. DUK_UNREF(thr);
  31082. /* magic: 0=getter call, 1=Object.getPrototypeOf */
  31083. if (duk_get_current_magic(ctx) == 0) {
  31084. duk_push_this_coercible_to_object(ctx);
  31085. duk_insert(ctx, 0);
  31086. }
  31087. h = duk_require_hobject_or_lfunc(ctx, 0);
  31088. /* h is NULL for lightfunc */
  31089. /* XXX: should the API call handle this directly, i.e. attempt
  31090. * to duk_push_hobject(ctx, null) would push a null instead?
  31091. * (On the other hand 'undefined' would be just as logical, but
  31092. * not wanted here.)
  31093. */
  31094. if (h == NULL) {
  31095. duk_push_hobject_bidx(ctx, DUK_BIDX_FUNCTION_PROTOTYPE);
  31096. } else {
  31097. proto = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
  31098. if (proto) {
  31099. duk_push_hobject(ctx, proto);
  31100. } else {
  31101. duk_push_null(ctx);
  31102. }
  31103. }
  31104. return 1;
  31105. }
  31106. /* Shared helper to implement ES6 Object.setPrototypeOf and
  31107. * Object.prototype.__proto__ setter.
  31108. *
  31109. * http://www.ecma-international.org/ecma-262/6.0/index.html#sec-get-object.prototype.__proto__
  31110. * http://www.ecma-international.org/ecma-262/6.0/index.html#sec-object.setprototypeof
  31111. */
  31112. DUK_INTERNAL duk_ret_t duk_bi_object_setprototype_shared(duk_context *ctx) {
  31113. duk_hthread *thr = (duk_hthread *) ctx;
  31114. duk_hobject *h_obj;
  31115. duk_hobject *h_new_proto;
  31116. duk_hobject *h_curr;
  31117. duk_ret_t ret_success = 1; /* retval for success path */
  31118. /* Preliminaries for __proto__ and setPrototypeOf (E6 19.1.2.18 steps 1-4);
  31119. * magic: 0=setter call, 1=Object.setPrototypeOf
  31120. */
  31121. if (duk_get_current_magic(ctx) == 0) {
  31122. duk_push_this_check_object_coercible(ctx);
  31123. duk_insert(ctx, 0);
  31124. if (!duk_check_type_mask(ctx, 1, DUK_TYPE_MASK_NULL | DUK_TYPE_MASK_OBJECT)) {
  31125. return 0;
  31126. }
  31127. /* __proto__ setter returns 'undefined' on success unlike the
  31128. * setPrototypeOf() call which returns the target object.
  31129. */
  31130. ret_success = 0;
  31131. } else {
  31132. duk_require_object_coercible(ctx, 0);
  31133. duk_require_type_mask(ctx, 1, DUK_TYPE_MASK_NULL | DUK_TYPE_MASK_OBJECT);
  31134. }
  31135. h_new_proto = duk_get_hobject(ctx, 1);
  31136. /* h_new_proto may be NULL */
  31137. if (duk_is_lightfunc(ctx, 0)) {
  31138. if (h_new_proto == thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]) {
  31139. goto skip;
  31140. }
  31141. goto fail_nonextensible;
  31142. }
  31143. h_obj = duk_get_hobject(ctx, 0);
  31144. if (!h_obj) {
  31145. goto skip;
  31146. }
  31147. DUK_ASSERT(h_obj != NULL);
  31148. /* [[SetPrototypeOf]] standard behavior, E6 9.1.2 */
  31149. /* TODO: implement Proxy object support here */
  31150. if (h_new_proto == DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_obj)) {
  31151. goto skip;
  31152. }
  31153. if (!DUK_HOBJECT_HAS_EXTENSIBLE(h_obj)) {
  31154. goto fail_nonextensible;
  31155. }
  31156. for (h_curr = h_new_proto; h_curr != NULL; h_curr = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_curr)) {
  31157. /* Loop prevention */
  31158. if (h_curr == h_obj) {
  31159. goto fail_loop;
  31160. }
  31161. }
  31162. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h_obj, h_new_proto);
  31163. /* fall thru */
  31164. skip:
  31165. duk_set_top(ctx, 1);
  31166. return ret_success;
  31167. fail_nonextensible:
  31168. fail_loop:
  31169. return DUK_RET_TYPE_ERROR;
  31170. }
  31171. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_get_own_property_descriptor(duk_context *ctx) {
  31172. /* XXX: no need for indirect call */
  31173. return duk_hobject_object_get_own_property_descriptor(ctx);
  31174. }
  31175. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_create(duk_context *ctx) {
  31176. duk_tval *tv;
  31177. duk_hobject *proto = NULL;
  31178. DUK_ASSERT_TOP(ctx, 2);
  31179. tv = duk_get_tval(ctx, 0);
  31180. DUK_ASSERT(tv != NULL);
  31181. if (DUK_TVAL_IS_NULL(tv)) {
  31182. ;
  31183. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  31184. proto = DUK_TVAL_GET_OBJECT(tv);
  31185. DUK_ASSERT(proto != NULL);
  31186. } else {
  31187. return DUK_RET_TYPE_ERROR;
  31188. }
  31189. (void) duk_push_object_helper_proto(ctx,
  31190. DUK_HOBJECT_FLAG_EXTENSIBLE |
  31191. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  31192. proto);
  31193. if (!duk_is_undefined(ctx, 1)) {
  31194. /* [ O Properties obj ] */
  31195. duk_replace(ctx, 0);
  31196. /* [ obj Properties ] */
  31197. /* Just call the "original" Object.defineProperties() to
  31198. * finish up.
  31199. */
  31200. return duk_bi_object_constructor_define_properties(ctx);
  31201. }
  31202. /* [ O Properties obj ] */
  31203. return 1;
  31204. }
  31205. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_define_property(duk_context *ctx) {
  31206. duk_hobject *obj;
  31207. duk_hstring *key;
  31208. duk_hobject *get;
  31209. duk_hobject *set;
  31210. duk_idx_t idx_value;
  31211. duk_uint_t defprop_flags;
  31212. DUK_ASSERT(ctx != NULL);
  31213. DUK_DDD(DUK_DDDPRINT("Object.defineProperty(): ctx=%p obj=%!T key=%!T desc=%!T",
  31214. (void *) ctx,
  31215. (duk_tval *) duk_get_tval(ctx, 0),
  31216. (duk_tval *) duk_get_tval(ctx, 1),
  31217. (duk_tval *) duk_get_tval(ctx, 2)));
  31218. /* [ obj key desc ] */
  31219. /* Lightfuncs are currently supported by coercing to a temporary
  31220. * Function object; changes will be allowed (the coerced value is
  31221. * extensible) but will be lost.
  31222. */
  31223. obj = duk_require_hobject_or_lfunc_coerce(ctx, 0);
  31224. (void) duk_to_string(ctx, 1);
  31225. key = duk_require_hstring(ctx, 1);
  31226. (void) duk_require_hobject(ctx, 2);
  31227. DUK_ASSERT(obj != NULL);
  31228. DUK_ASSERT(key != NULL);
  31229. DUK_ASSERT(duk_get_hobject(ctx, 2) != NULL);
  31230. /*
  31231. * Validate and convert argument property descriptor (an Ecmascript
  31232. * object) into a set of defprop_flags and possibly property value,
  31233. * getter, and/or setter values on the value stack.
  31234. *
  31235. * Lightfunc set/get values are coerced to full Functions.
  31236. */
  31237. duk_hobject_prepare_property_descriptor(ctx,
  31238. 2 /*idx_desc*/,
  31239. &defprop_flags,
  31240. &idx_value,
  31241. &get,
  31242. &set);
  31243. /*
  31244. * Use Object.defineProperty() helper for the actual operation.
  31245. */
  31246. duk_hobject_define_property_helper(ctx,
  31247. defprop_flags,
  31248. obj,
  31249. key,
  31250. idx_value,
  31251. get,
  31252. set);
  31253. /* Ignore the normalize/validate helper outputs on the value stack,
  31254. * they're popped automatically.
  31255. */
  31256. /*
  31257. * Return target object.
  31258. */
  31259. duk_push_hobject(ctx, obj);
  31260. return 1;
  31261. }
  31262. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_define_properties(duk_context *ctx) {
  31263. duk_small_uint_t pass;
  31264. duk_uint_t defprop_flags;
  31265. duk_hobject *obj;
  31266. duk_idx_t idx_value;
  31267. duk_hobject *get;
  31268. duk_hobject *set;
  31269. /* Lightfunc handling by ToObject() coercion. */
  31270. obj = duk_require_hobject_or_lfunc_coerce(ctx, 0); /* target */
  31271. DUK_ASSERT(obj != NULL);
  31272. duk_to_object(ctx, 1); /* properties object */
  31273. DUK_DDD(DUK_DDDPRINT("target=%!iT, properties=%!iT",
  31274. (duk_tval *) duk_get_tval(ctx, 0),
  31275. (duk_tval *) duk_get_tval(ctx, 1)));
  31276. /*
  31277. * Two pass approach to processing the property descriptors.
  31278. * On first pass validate and normalize all descriptors before
  31279. * any changes are made to the target object. On second pass
  31280. * make the actual modifications to the target object.
  31281. *
  31282. * Right now we'll just use the same normalize/validate helper
  31283. * on both passes, ignoring its outputs on the first pass.
  31284. */
  31285. for (pass = 0; pass < 2; pass++) {
  31286. duk_set_top(ctx, 2); /* -> [ hobject props ] */
  31287. duk_enum(ctx, 1, DUK_ENUM_OWN_PROPERTIES_ONLY /*enum_flags*/);
  31288. for (;;) {
  31289. duk_hstring *key;
  31290. /* [ hobject props enum(props) ] */
  31291. duk_set_top(ctx, 3);
  31292. if (!duk_next(ctx, 2, 1 /*get_value*/)) {
  31293. break;
  31294. }
  31295. DUK_DDD(DUK_DDDPRINT("-> key=%!iT, desc=%!iT",
  31296. (duk_tval *) duk_get_tval(ctx, -2),
  31297. (duk_tval *) duk_get_tval(ctx, -1)));
  31298. /* [ hobject props enum(props) key desc ] */
  31299. duk_hobject_prepare_property_descriptor(ctx,
  31300. 4 /*idx_desc*/,
  31301. &defprop_flags,
  31302. &idx_value,
  31303. &get,
  31304. &set);
  31305. /* [ hobject props enum(props) key desc value? getter? setter? ] */
  31306. if (pass == 0) {
  31307. continue;
  31308. }
  31309. key = duk_get_hstring(ctx, 3);
  31310. DUK_ASSERT(key != NULL);
  31311. duk_hobject_define_property_helper(ctx,
  31312. defprop_flags,
  31313. obj,
  31314. key,
  31315. idx_value,
  31316. get,
  31317. set);
  31318. }
  31319. }
  31320. /*
  31321. * Return target object
  31322. */
  31323. duk_dup(ctx, 0);
  31324. return 1;
  31325. }
  31326. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_seal_freeze_shared(duk_context *ctx) {
  31327. duk_hthread *thr = (duk_hthread *) ctx;
  31328. duk_hobject *h;
  31329. duk_bool_t is_freeze;
  31330. h = duk_require_hobject_or_lfunc(ctx, 0);
  31331. if (!h) {
  31332. /* Lightfunc, always success. */
  31333. return 1;
  31334. }
  31335. is_freeze = (duk_bool_t) duk_get_current_magic(ctx);
  31336. duk_hobject_object_seal_freeze_helper(thr, h, is_freeze);
  31337. /* Sealed and frozen objects cannot gain any more properties,
  31338. * so this is a good time to compact them.
  31339. */
  31340. duk_hobject_compact_props(thr, h);
  31341. return 1;
  31342. }
  31343. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_prevent_extensions(duk_context *ctx) {
  31344. duk_hthread *thr = (duk_hthread *) ctx;
  31345. duk_hobject *h;
  31346. h = duk_require_hobject_or_lfunc(ctx, 0);
  31347. if (!h) {
  31348. /* Lightfunc, always success. */
  31349. return 1;
  31350. }
  31351. DUK_ASSERT(h != NULL);
  31352. DUK_HOBJECT_CLEAR_EXTENSIBLE(h);
  31353. /* A non-extensible object cannot gain any more properties,
  31354. * so this is a good time to compact.
  31355. */
  31356. duk_hobject_compact_props(thr, h);
  31357. return 1;
  31358. }
  31359. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_is_sealed_frozen_shared(duk_context *ctx) {
  31360. duk_hobject *h;
  31361. duk_bool_t is_frozen;
  31362. duk_bool_t rc;
  31363. h = duk_require_hobject_or_lfunc(ctx, 0);
  31364. if (!h) {
  31365. duk_push_true(ctx); /* frozen and sealed */
  31366. } else {
  31367. is_frozen = duk_get_current_magic(ctx);
  31368. rc = duk_hobject_object_is_sealed_frozen_helper((duk_hthread *) ctx, h, is_frozen /*is_frozen*/);
  31369. duk_push_boolean(ctx, rc);
  31370. }
  31371. return 1;
  31372. }
  31373. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_is_extensible(duk_context *ctx) {
  31374. duk_hobject *h;
  31375. h = duk_require_hobject_or_lfunc(ctx, 0);
  31376. if (!h) {
  31377. duk_push_false(ctx);
  31378. } else {
  31379. duk_push_boolean(ctx, DUK_HOBJECT_HAS_EXTENSIBLE(h));
  31380. }
  31381. return 1;
  31382. }
  31383. /* Shared helper for Object.getOwnPropertyNames() and Object.keys().
  31384. * Magic: 0=getOwnPropertyNames, 1=Object.keys.
  31385. */
  31386. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_keys_shared(duk_context *ctx) {
  31387. duk_hthread *thr = (duk_hthread *) ctx;
  31388. duk_hobject *obj;
  31389. #if defined(DUK_USE_ES6_PROXY)
  31390. duk_hobject *h_proxy_target;
  31391. duk_hobject *h_proxy_handler;
  31392. duk_hobject *h_trap_result;
  31393. duk_uarridx_t i, len, idx;
  31394. #endif
  31395. duk_small_uint_t enum_flags;
  31396. DUK_ASSERT_TOP(ctx, 1);
  31397. DUK_UNREF(thr);
  31398. obj = duk_require_hobject_or_lfunc_coerce(ctx, 0);
  31399. DUK_ASSERT(obj != NULL);
  31400. DUK_UNREF(obj);
  31401. #if defined(DUK_USE_ES6_PROXY)
  31402. if (DUK_LIKELY(!duk_hobject_proxy_check(thr,
  31403. obj,
  31404. &h_proxy_target,
  31405. &h_proxy_handler))) {
  31406. goto skip_proxy;
  31407. }
  31408. duk_push_hobject(ctx, h_proxy_handler);
  31409. if (!duk_get_prop_stridx(ctx, -1, DUK_STRIDX_OWN_KEYS)) {
  31410. /* Careful with reachability here: don't pop 'obj' before pushing
  31411. * proxy target.
  31412. */
  31413. DUK_DDD(DUK_DDDPRINT("no ownKeys trap, get keys of target instead"));
  31414. duk_pop_2(ctx);
  31415. duk_push_hobject(ctx, h_proxy_target);
  31416. duk_replace(ctx, 0);
  31417. DUK_ASSERT_TOP(ctx, 1);
  31418. goto skip_proxy;
  31419. }
  31420. /* [ obj handler trap ] */
  31421. duk_insert(ctx, -2);
  31422. duk_push_hobject(ctx, h_proxy_target); /* -> [ obj trap handler target ] */
  31423. duk_call_method(ctx, 1 /*nargs*/); /* -> [ obj trap_result ] */
  31424. h_trap_result = duk_require_hobject(ctx, -1);
  31425. DUK_UNREF(h_trap_result);
  31426. len = (duk_uarridx_t) duk_get_length(ctx, -1);
  31427. idx = 0;
  31428. duk_push_array(ctx);
  31429. for (i = 0; i < len; i++) {
  31430. /* [ obj trap_result res_arr ] */
  31431. if (duk_get_prop_index(ctx, -2, i) && duk_is_string(ctx, -1)) {
  31432. /* XXX: for Object.keys() we should check enumerability of key */
  31433. /* [ obj trap_result res_arr propname ] */
  31434. duk_put_prop_index(ctx, -2, idx);
  31435. idx++;
  31436. } else {
  31437. duk_pop(ctx);
  31438. }
  31439. }
  31440. /* XXX: for Object.keys() the [[OwnPropertyKeys]] result (trap result)
  31441. * should be filtered so that only enumerable keys remain. Enumerability
  31442. * should be checked with [[GetOwnProperty]] on the original object
  31443. * (i.e., the proxy in this case). If the proxy has a getOwnPropertyDescriptor
  31444. * trap, it should be triggered for every property. If the proxy doesn't have
  31445. * the trap, enumerability should be checked against the target object instead.
  31446. * We don't do any of this now, so Object.keys() and Object.getOwnPropertyNames()
  31447. * return the same result now for proxy traps. We still do clean up the trap
  31448. * result, so that Object.keys() and Object.getOwnPropertyNames() will return a
  31449. * clean array of strings without gaps.
  31450. */
  31451. return 1;
  31452. skip_proxy:
  31453. #endif /* DUK_USE_ES6_PROXY */
  31454. DUK_ASSERT_TOP(ctx, 1);
  31455. if (duk_get_current_magic(ctx)) {
  31456. /* Object.keys */
  31457. enum_flags = DUK_ENUM_OWN_PROPERTIES_ONLY |
  31458. DUK_ENUM_NO_PROXY_BEHAVIOR;
  31459. } else {
  31460. /* Object.getOwnPropertyNames */
  31461. enum_flags = DUK_ENUM_INCLUDE_NONENUMERABLE |
  31462. DUK_ENUM_OWN_PROPERTIES_ONLY |
  31463. DUK_ENUM_NO_PROXY_BEHAVIOR;
  31464. }
  31465. return duk_hobject_get_enumerated_keys(ctx, enum_flags);
  31466. }
  31467. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_to_string(duk_context *ctx) {
  31468. duk_push_this(ctx);
  31469. duk_to_object_class_string_top(ctx);
  31470. return 1;
  31471. }
  31472. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_to_locale_string(duk_context *ctx) {
  31473. DUK_ASSERT_TOP(ctx, 0);
  31474. (void) duk_push_this_coercible_to_object(ctx);
  31475. duk_get_prop_stridx(ctx, 0, DUK_STRIDX_TO_STRING);
  31476. if (!duk_is_callable(ctx, 1)) {
  31477. return DUK_RET_TYPE_ERROR;
  31478. }
  31479. duk_dup(ctx, 0); /* -> [ O toString O ] */
  31480. duk_call_method(ctx, 0); /* XXX: call method tail call? */
  31481. return 1;
  31482. }
  31483. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_value_of(duk_context *ctx) {
  31484. (void) duk_push_this_coercible_to_object(ctx);
  31485. return 1;
  31486. }
  31487. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_is_prototype_of(duk_context *ctx) {
  31488. duk_hthread *thr = (duk_hthread *) ctx;
  31489. duk_hobject *h_v;
  31490. duk_hobject *h_obj;
  31491. DUK_ASSERT_TOP(ctx, 1);
  31492. h_v = duk_get_hobject(ctx, 0);
  31493. if (!h_v) {
  31494. duk_push_false(ctx); /* XXX: tail call: return duk_push_false(ctx) */
  31495. return 1;
  31496. }
  31497. h_obj = duk_push_this_coercible_to_object(ctx);
  31498. DUK_ASSERT(h_obj != NULL);
  31499. /* E5.1 Section 15.2.4.6, step 3.a, lookup proto once before compare.
  31500. * Prototype loops should cause an error to be thrown.
  31501. */
  31502. duk_push_boolean(ctx, duk_hobject_prototype_chain_contains(thr, DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_v), h_obj, 0 /*ignore_loop*/));
  31503. return 1;
  31504. }
  31505. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_has_own_property(duk_context *ctx) {
  31506. return duk_hobject_object_ownprop_helper(ctx, 0 /*required_desc_flags*/);
  31507. }
  31508. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_property_is_enumerable(duk_context *ctx) {
  31509. return duk_hobject_object_ownprop_helper(ctx, DUK_PROPDESC_FLAG_ENUMERABLE /*required_desc_flags*/);
  31510. }
  31511. #line 1 "duk_bi_pointer.c"
  31512. /*
  31513. * Pointer built-ins
  31514. */
  31515. /* include removed: duk_internal.h */
  31516. /*
  31517. * Constructor
  31518. */
  31519. DUK_INTERNAL duk_ret_t duk_bi_pointer_constructor(duk_context *ctx) {
  31520. /* XXX: this behavior is quite useless now; it would be nice to be able
  31521. * to create pointer values from e.g. numbers or strings. Numbers are
  31522. * problematic on 64-bit platforms though. Hex encoded strings?
  31523. */
  31524. if (duk_get_top(ctx) == 0) {
  31525. duk_push_pointer(ctx, NULL);
  31526. } else {
  31527. duk_to_pointer(ctx, 0);
  31528. }
  31529. DUK_ASSERT(duk_is_pointer(ctx, 0));
  31530. duk_set_top(ctx, 1);
  31531. if (duk_is_constructor_call(ctx)) {
  31532. duk_push_object_helper(ctx,
  31533. DUK_HOBJECT_FLAG_EXTENSIBLE |
  31534. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_POINTER),
  31535. DUK_BIDX_POINTER_PROTOTYPE);
  31536. /* Pointer object internal value is immutable */
  31537. duk_dup(ctx, 0);
  31538. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE);
  31539. }
  31540. /* Note: unbalanced stack on purpose */
  31541. return 1;
  31542. }
  31543. /*
  31544. * toString(), valueOf()
  31545. */
  31546. DUK_INTERNAL duk_ret_t duk_bi_pointer_prototype_tostring_shared(duk_context *ctx) {
  31547. duk_tval *tv;
  31548. duk_small_int_t to_string = duk_get_current_magic(ctx);
  31549. duk_push_this(ctx);
  31550. tv = duk_require_tval(ctx, -1);
  31551. DUK_ASSERT(tv != NULL);
  31552. if (DUK_TVAL_IS_POINTER(tv)) {
  31553. /* nop */
  31554. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  31555. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  31556. DUK_ASSERT(h != NULL);
  31557. /* Must be a "pointer object", i.e. class "Pointer" */
  31558. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_POINTER) {
  31559. goto type_error;
  31560. }
  31561. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE);
  31562. } else {
  31563. goto type_error;
  31564. }
  31565. if (to_string) {
  31566. duk_to_string(ctx, -1);
  31567. }
  31568. return 1;
  31569. type_error:
  31570. return DUK_RET_TYPE_ERROR;
  31571. }
  31572. #line 1 "duk_bi_proxy.c"
  31573. /*
  31574. * Proxy built-in (ES6)
  31575. */
  31576. /* include removed: duk_internal.h */
  31577. #if defined(DUK_USE_ES6_PROXY)
  31578. DUK_INTERNAL duk_ret_t duk_bi_proxy_constructor(duk_context *ctx) {
  31579. duk_hobject *h_target;
  31580. duk_hobject *h_handler;
  31581. if (!duk_is_constructor_call(ctx)) {
  31582. return DUK_RET_TYPE_ERROR;
  31583. }
  31584. /* Reject a proxy object as the target because it would need
  31585. * special handler in property lookups. (ES6 has no such restriction)
  31586. */
  31587. h_target = duk_require_hobject_or_lfunc_coerce(ctx, 0);
  31588. DUK_ASSERT(h_target != NULL);
  31589. if (DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(h_target)) {
  31590. return DUK_RET_TYPE_ERROR;
  31591. }
  31592. /* Reject a proxy object as the handler because it would cause
  31593. * potentially unbounded recursion. (ES6 has no such restriction)
  31594. */
  31595. h_handler = duk_require_hobject_or_lfunc_coerce(ctx, 1);
  31596. DUK_ASSERT(h_handler != NULL);
  31597. if (DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(h_handler)) {
  31598. return DUK_RET_TYPE_ERROR;
  31599. }
  31600. /* XXX: the returned value is exotic in ES6, but we use a
  31601. * simple object here with no prototype. Without a prototype,
  31602. * [[DefaultValue]] coercion fails which is abit confusing.
  31603. * No callable check/handling in the current Proxy subset.
  31604. */
  31605. (void) duk_push_object_helper_proto(ctx,
  31606. DUK_HOBJECT_FLAG_EXTENSIBLE |
  31607. DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ |
  31608. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  31609. NULL);
  31610. DUK_ASSERT_TOP(ctx, 3);
  31611. /* Make _Target and _Handler non-configurable and non-writable.
  31612. * They can still be forcibly changed by C code (both user and
  31613. * Duktape internal), but not by Ecmascript code.
  31614. */
  31615. /* Proxy target */
  31616. duk_dup(ctx, 0);
  31617. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_TARGET, DUK_PROPDESC_FLAGS_NONE);
  31618. /* Proxy handler */
  31619. duk_dup(ctx, 1);
  31620. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_HANDLER, DUK_PROPDESC_FLAGS_NONE);
  31621. return 1; /* replacement handler */
  31622. }
  31623. #else /* DUK_USE_ES6_PROXY */
  31624. DUK_INTERNAL duk_ret_t duk_bi_proxy_constructor(duk_context *ctx) {
  31625. DUK_UNREF(ctx);
  31626. return DUK_RET_UNSUPPORTED_ERROR;
  31627. }
  31628. #endif /* DUK_USE_ES6_PROXY */
  31629. #line 1 "duk_bi_regexp.c"
  31630. /*
  31631. * RegExp built-ins
  31632. */
  31633. /* include removed: duk_internal.h */
  31634. #ifdef DUK_USE_REGEXP_SUPPORT
  31635. DUK_LOCAL void duk__get_this_regexp(duk_context *ctx) {
  31636. duk_hobject *h;
  31637. duk_push_this(ctx);
  31638. h = duk_require_hobject_with_class(ctx, -1, DUK_HOBJECT_CLASS_REGEXP);
  31639. DUK_ASSERT(h != NULL);
  31640. DUK_UNREF(h);
  31641. duk_insert(ctx, 0); /* prepend regexp to valstack 0 index */
  31642. }
  31643. /* XXX: much to improve (code size) */
  31644. DUK_INTERNAL duk_ret_t duk_bi_regexp_constructor(duk_context *ctx) {
  31645. duk_hthread *thr = (duk_hthread *) ctx;
  31646. duk_hobject *h_pattern;
  31647. DUK_ASSERT_TOP(ctx, 2);
  31648. h_pattern = duk_get_hobject(ctx, 0);
  31649. if (!duk_is_constructor_call(ctx) &&
  31650. h_pattern != NULL &&
  31651. DUK_HOBJECT_GET_CLASS_NUMBER(h_pattern) == DUK_HOBJECT_CLASS_REGEXP &&
  31652. duk_is_undefined(ctx, 1)) {
  31653. /* Called as a function, pattern has [[Class]] "RegExp" and
  31654. * flags is undefined -> return object as is.
  31655. */
  31656. duk_dup(ctx, 0);
  31657. return 1;
  31658. }
  31659. /* Else functionality is identical for function call and constructor
  31660. * call.
  31661. */
  31662. if (h_pattern != NULL &&
  31663. DUK_HOBJECT_GET_CLASS_NUMBER(h_pattern) == DUK_HOBJECT_CLASS_REGEXP) {
  31664. if (duk_is_undefined(ctx, 1)) {
  31665. duk_bool_t flag_g, flag_i, flag_m;
  31666. duk_get_prop_stridx(ctx, 0, DUK_STRIDX_SOURCE);
  31667. flag_g = duk_get_prop_stridx_boolean(ctx, 0, DUK_STRIDX_GLOBAL, NULL);
  31668. flag_i = duk_get_prop_stridx_boolean(ctx, 0, DUK_STRIDX_IGNORE_CASE, NULL);
  31669. flag_m = duk_get_prop_stridx_boolean(ctx, 0, DUK_STRIDX_MULTILINE, NULL);
  31670. duk_push_sprintf(ctx, "%s%s%s",
  31671. (const char *) (flag_g ? "g" : ""),
  31672. (const char *) (flag_i ? "i" : ""),
  31673. (const char *) (flag_m ? "m" : ""));
  31674. /* [ ... pattern flags ] */
  31675. } else {
  31676. return DUK_RET_TYPE_ERROR;
  31677. }
  31678. } else {
  31679. if (duk_is_undefined(ctx, 0)) {
  31680. duk_push_string(ctx, "");
  31681. } else {
  31682. duk_dup(ctx, 0);
  31683. duk_to_string(ctx, -1);
  31684. }
  31685. if (duk_is_undefined(ctx, 1)) {
  31686. duk_push_string(ctx, "");
  31687. } else {
  31688. duk_dup(ctx, 1);
  31689. duk_to_string(ctx, -1);
  31690. }
  31691. /* [ ... pattern flags ] */
  31692. }
  31693. DUK_DDD(DUK_DDDPRINT("RegExp constructor/function call, pattern=%!T, flags=%!T",
  31694. (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1)));
  31695. /* [ ... pattern flags ] */
  31696. duk_regexp_compile(thr);
  31697. /* [ ... bytecode escaped_source ] */
  31698. duk_regexp_create_instance(thr);
  31699. /* [ ... RegExp ] */
  31700. return 1;
  31701. }
  31702. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_exec(duk_context *ctx) {
  31703. duk__get_this_regexp(ctx);
  31704. /* [ regexp input ] */
  31705. duk_regexp_match((duk_hthread *) ctx);
  31706. /* [ result ] */
  31707. return 1;
  31708. }
  31709. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_test(duk_context *ctx) {
  31710. duk__get_this_regexp(ctx);
  31711. /* [ regexp input ] */
  31712. /* result object is created and discarded; wasteful but saves code space */
  31713. duk_regexp_match((duk_hthread *) ctx);
  31714. /* [ result ] */
  31715. duk_push_boolean(ctx, (duk_is_null(ctx, -1) ? 0 : 1));
  31716. return 1;
  31717. }
  31718. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_to_string(duk_context *ctx) {
  31719. duk_hstring *h_bc;
  31720. duk_small_int_t re_flags;
  31721. #if 0
  31722. /* A little tricky string approach to provide the flags string.
  31723. * This depends on the specific flag values in duk_regexp.h,
  31724. * which needs to be asserted for. In practice this doesn't
  31725. * produce more compact code than the easier approach in use.
  31726. */
  31727. const char *flag_strings = "gim\0gi\0gm\0g\0";
  31728. duk_uint8_t flag_offsets[8] = {
  31729. (duk_uint8_t) 3, /* flags: "" */
  31730. (duk_uint8_t) 10, /* flags: "g" */
  31731. (duk_uint8_t) 5, /* flags: "i" */
  31732. (duk_uint8_t) 4, /* flags: "gi" */
  31733. (duk_uint8_t) 2, /* flags: "m" */
  31734. (duk_uint8_t) 7, /* flags: "gm" */
  31735. (duk_uint8_t) 1, /* flags: "im" */
  31736. (duk_uint8_t) 0, /* flags: "gim" */
  31737. };
  31738. DUK_ASSERT(DUK_RE_FLAG_GLOBAL == 1);
  31739. DUK_ASSERT(DUK_RE_FLAG_IGNORE_CASE == 2);
  31740. DUK_ASSERT(DUK_RE_FLAG_MULTILINE == 4);
  31741. #endif
  31742. duk__get_this_regexp(ctx);
  31743. /* [ regexp ] */
  31744. duk_get_prop_stridx(ctx, 0, DUK_STRIDX_SOURCE);
  31745. duk_get_prop_stridx(ctx, 0, DUK_STRIDX_INT_BYTECODE);
  31746. h_bc = duk_get_hstring(ctx, -1);
  31747. DUK_ASSERT(h_bc != NULL);
  31748. DUK_ASSERT(DUK_HSTRING_GET_BYTELEN(h_bc) >= 1);
  31749. DUK_ASSERT(DUK_HSTRING_GET_CHARLEN(h_bc) >= 1);
  31750. DUK_ASSERT(DUK_HSTRING_GET_DATA(h_bc)[0] < 0x80);
  31751. re_flags = (duk_small_int_t) DUK_HSTRING_GET_DATA(h_bc)[0];
  31752. /* [ regexp source bytecode ] */
  31753. #if 1
  31754. /* This is a cleaner approach and also produces smaller code than
  31755. * the other alternative. Use duk_require_string() for format
  31756. * safety (although the source property should always exist).
  31757. */
  31758. duk_push_sprintf(ctx, "/%s/%s%s%s",
  31759. (const char *) duk_require_string(ctx, -2), /* require to be safe */
  31760. (re_flags & DUK_RE_FLAG_GLOBAL) ? "g" : "",
  31761. (re_flags & DUK_RE_FLAG_IGNORE_CASE) ? "i" : "",
  31762. (re_flags & DUK_RE_FLAG_MULTILINE) ? "m" : "");
  31763. #else
  31764. /* This should not be necessary because no-one should tamper with the
  31765. * regexp bytecode, but is prudent to avoid potential segfaults if that
  31766. * were to happen for some reason.
  31767. */
  31768. re_flags &= 0x07;
  31769. DUK_ASSERT(re_flags >= 0 && re_flags <= 7); /* three flags */
  31770. duk_push_sprintf(ctx, "/%s/%s",
  31771. (const char *) duk_require_string(ctx, -2),
  31772. (const char *) (flag_strings + flag_offsets[re_flags]));
  31773. #endif
  31774. return 1;
  31775. }
  31776. #else /* DUK_USE_REGEXP_SUPPORT */
  31777. DUK_INTERNAL duk_ret_t duk_bi_regexp_constructor(duk_context *ctx) {
  31778. DUK_UNREF(ctx);
  31779. return DUK_RET_UNSUPPORTED_ERROR;
  31780. }
  31781. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_exec(duk_context *ctx) {
  31782. DUK_UNREF(ctx);
  31783. return DUK_RET_UNSUPPORTED_ERROR;
  31784. }
  31785. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_test(duk_context *ctx) {
  31786. DUK_UNREF(ctx);
  31787. return DUK_RET_UNSUPPORTED_ERROR;
  31788. }
  31789. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_to_string(duk_context *ctx) {
  31790. DUK_UNREF(ctx);
  31791. return DUK_RET_UNSUPPORTED_ERROR;
  31792. }
  31793. #endif /* DUK_USE_REGEXP_SUPPORT */
  31794. #line 1 "duk_bi_string.c"
  31795. /*
  31796. * String built-ins
  31797. */
  31798. /* XXX: There are several limitations in the current implementation for
  31799. * strings with >= 0x80000000UL characters. In some cases one would need
  31800. * to be able to represent the range [-0xffffffff,0xffffffff] and so on.
  31801. * Generally character and byte length are assumed to fit into signed 32
  31802. * bits (< 0x80000000UL). Places with issues are not marked explicitly
  31803. * below in all cases, look for signed type usage (duk_int_t etc) for
  31804. * offsets/lengths.
  31805. */
  31806. /* include removed: duk_internal.h */
  31807. /*
  31808. * Constructor
  31809. */
  31810. DUK_INTERNAL duk_ret_t duk_bi_string_constructor(duk_context *ctx) {
  31811. /* String constructor needs to distinguish between an argument not given at all
  31812. * vs. given as 'undefined'. We're a vararg function to handle this properly.
  31813. */
  31814. if (duk_get_top(ctx) == 0) {
  31815. duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING);
  31816. } else {
  31817. duk_to_string(ctx, 0);
  31818. }
  31819. DUK_ASSERT(duk_is_string(ctx, 0));
  31820. duk_set_top(ctx, 1);
  31821. if (duk_is_constructor_call(ctx)) {
  31822. duk_push_object_helper(ctx,
  31823. DUK_HOBJECT_FLAG_EXTENSIBLE |
  31824. DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ |
  31825. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_STRING),
  31826. DUK_BIDX_STRING_PROTOTYPE);
  31827. /* String object internal value is immutable */
  31828. duk_dup(ctx, 0);
  31829. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE);
  31830. }
  31831. /* Note: unbalanced stack on purpose */
  31832. return 1;
  31833. }
  31834. DUK_INTERNAL duk_ret_t duk_bi_string_constructor_from_char_code(duk_context *ctx) {
  31835. duk_hthread *thr = (duk_hthread *) ctx;
  31836. duk_bufwriter_ctx bw_alloc;
  31837. duk_bufwriter_ctx *bw;
  31838. duk_idx_t i, n;
  31839. duk_ucodepoint_t cp;
  31840. /* XXX: It would be nice to build the string directly but ToUint16()
  31841. * coercion is needed so a generic helper would not be very
  31842. * helpful (perhaps coerce the value stack first here and then
  31843. * build a string from a duk_tval number sequence in one go?).
  31844. */
  31845. n = duk_get_top(ctx);
  31846. bw = &bw_alloc;
  31847. DUK_BW_INIT_PUSHBUF(thr, bw, n); /* initial estimate for ASCII only codepoints */
  31848. for (i = 0; i < n; i++) {
  31849. /* XXX: could improve bufwriter handling to write multiple codepoints
  31850. * with one ensure call but the relative benefit would be quite small.
  31851. */
  31852. #if defined(DUK_USE_NONSTD_STRING_FROMCHARCODE_32BIT)
  31853. /* ToUint16() coercion is mandatory in the E5.1 specification, but
  31854. * this non-compliant behavior makes more sense because we support
  31855. * non-BMP codepoints. Don't use CESU-8 because that'd create
  31856. * surrogate pairs.
  31857. */
  31858. cp = (duk_ucodepoint_t) duk_to_uint32(ctx, i);
  31859. DUK_BW_WRITE_ENSURE_XUTF8(thr, bw, cp);
  31860. #else
  31861. cp = (duk_ucodepoint_t) duk_to_uint32(ctx, i);
  31862. DUK_BW_WRITE_ENSURE_CESU8(thr, bw, cp);
  31863. #endif
  31864. }
  31865. DUK_BW_COMPACT(thr, bw);
  31866. duk_to_string(ctx, -1);
  31867. return 1;
  31868. }
  31869. /*
  31870. * toString(), valueOf()
  31871. */
  31872. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_to_string(duk_context *ctx) {
  31873. duk_tval *tv;
  31874. duk_push_this(ctx);
  31875. tv = duk_require_tval(ctx, -1);
  31876. DUK_ASSERT(tv != NULL);
  31877. if (DUK_TVAL_IS_STRING(tv)) {
  31878. /* return as is */
  31879. return 1;
  31880. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  31881. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  31882. DUK_ASSERT(h != NULL);
  31883. /* Must be a "string object", i.e. class "String" */
  31884. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_STRING) {
  31885. goto type_error;
  31886. }
  31887. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE);
  31888. DUK_ASSERT(duk_is_string(ctx, -1));
  31889. return 1;
  31890. } else {
  31891. goto type_error;
  31892. }
  31893. /* never here, but fall through */
  31894. type_error:
  31895. return DUK_RET_TYPE_ERROR;
  31896. }
  31897. /*
  31898. * Character and charcode access
  31899. */
  31900. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_char_at(duk_context *ctx) {
  31901. duk_int_t pos;
  31902. /* XXX: faster implementation */
  31903. (void) duk_push_this_coercible_to_string(ctx);
  31904. pos = duk_to_int(ctx, 0);
  31905. duk_substring(ctx, -1, pos, pos + 1);
  31906. return 1;
  31907. }
  31908. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_char_code_at(duk_context *ctx) {
  31909. duk_hthread *thr = (duk_hthread *) ctx;
  31910. duk_int_t pos;
  31911. duk_hstring *h;
  31912. duk_bool_t clamped;
  31913. /* XXX: faster implementation */
  31914. DUK_DDD(DUK_DDDPRINT("arg=%!T", (duk_tval *) duk_get_tval(ctx, 0)));
  31915. h = duk_push_this_coercible_to_string(ctx);
  31916. DUK_ASSERT(h != NULL);
  31917. pos = duk_to_int_clamped_raw(ctx,
  31918. 0 /*index*/,
  31919. 0 /*min(incl)*/,
  31920. DUK_HSTRING_GET_CHARLEN(h) - 1 /*max(incl)*/,
  31921. &clamped /*out_clamped*/);
  31922. if (clamped) {
  31923. duk_push_number(ctx, DUK_DOUBLE_NAN);
  31924. return 1;
  31925. }
  31926. duk_push_u32(ctx, (duk_uint32_t) duk_hstring_char_code_at_raw(thr, h, pos));
  31927. return 1;
  31928. }
  31929. /*
  31930. * substring(), substr(), slice()
  31931. */
  31932. /* XXX: any chance of merging these three similar but still slightly
  31933. * different algorithms so that footprint would be reduced?
  31934. */
  31935. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_substring(duk_context *ctx) {
  31936. duk_hstring *h;
  31937. duk_int_t start_pos, end_pos;
  31938. duk_int_t len;
  31939. h = duk_push_this_coercible_to_string(ctx);
  31940. DUK_ASSERT(h != NULL);
  31941. len = (duk_int_t) DUK_HSTRING_GET_CHARLEN(h);
  31942. /* [ start end str ] */
  31943. start_pos = duk_to_int_clamped(ctx, 0, 0, len);
  31944. if (duk_is_undefined(ctx, 1)) {
  31945. end_pos = len;
  31946. } else {
  31947. end_pos = duk_to_int_clamped(ctx, 1, 0, len);
  31948. }
  31949. DUK_ASSERT(start_pos >= 0 && start_pos <= len);
  31950. DUK_ASSERT(end_pos >= 0 && end_pos <= len);
  31951. if (start_pos > end_pos) {
  31952. duk_int_t tmp = start_pos;
  31953. start_pos = end_pos;
  31954. end_pos = tmp;
  31955. }
  31956. DUK_ASSERT(end_pos >= start_pos);
  31957. duk_substring(ctx, -1, (duk_size_t) start_pos, (duk_size_t) end_pos);
  31958. return 1;
  31959. }
  31960. #ifdef DUK_USE_SECTION_B
  31961. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_substr(duk_context *ctx) {
  31962. duk_hstring *h;
  31963. duk_int_t start_pos, end_pos;
  31964. duk_int_t len;
  31965. /* Unlike non-obsolete String calls, substr() algorithm in E5.1
  31966. * specification will happily coerce undefined and null to strings
  31967. * ("undefined" and "null").
  31968. */
  31969. duk_push_this(ctx);
  31970. h = duk_to_hstring(ctx, -1);
  31971. DUK_ASSERT(h != NULL);
  31972. len = (duk_int_t) DUK_HSTRING_GET_CHARLEN(h);
  31973. /* [ start length str ] */
  31974. /* The implementation for computing of start_pos and end_pos differs
  31975. * from the standard algorithm, but is intended to result in the exactly
  31976. * same behavior. This is not always obvious.
  31977. */
  31978. /* combines steps 2 and 5; -len ensures max() not needed for step 5 */
  31979. start_pos = duk_to_int_clamped(ctx, 0, -len, len);
  31980. if (start_pos < 0) {
  31981. start_pos = len + start_pos;
  31982. }
  31983. DUK_ASSERT(start_pos >= 0 && start_pos <= len);
  31984. /* combines steps 3, 6; step 7 is not needed */
  31985. if (duk_is_undefined(ctx, 1)) {
  31986. end_pos = len;
  31987. } else {
  31988. DUK_ASSERT(start_pos <= len);
  31989. end_pos = start_pos + duk_to_int_clamped(ctx, 1, 0, len - start_pos);
  31990. }
  31991. DUK_ASSERT(start_pos >= 0 && start_pos <= len);
  31992. DUK_ASSERT(end_pos >= 0 && end_pos <= len);
  31993. DUK_ASSERT(end_pos >= start_pos);
  31994. duk_substring(ctx, -1, (duk_size_t) start_pos, (duk_size_t) end_pos);
  31995. return 1;
  31996. }
  31997. #else /* DUK_USE_SECTION_B */
  31998. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_substr(duk_context *ctx) {
  31999. DUK_UNREF(ctx);
  32000. return DUK_RET_UNSUPPORTED_ERROR;
  32001. }
  32002. #endif /* DUK_USE_SECTION_B */
  32003. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_slice(duk_context *ctx) {
  32004. duk_hstring *h;
  32005. duk_int_t start_pos, end_pos;
  32006. duk_int_t len;
  32007. h = duk_push_this_coercible_to_string(ctx);
  32008. DUK_ASSERT(h != NULL);
  32009. len = (duk_int_t) DUK_HSTRING_GET_CHARLEN(h);
  32010. /* [ start end str ] */
  32011. start_pos = duk_to_int_clamped(ctx, 0, -len, len);
  32012. if (start_pos < 0) {
  32013. start_pos = len + start_pos;
  32014. }
  32015. if (duk_is_undefined(ctx, 1)) {
  32016. end_pos = len;
  32017. } else {
  32018. end_pos = duk_to_int_clamped(ctx, 1, -len, len);
  32019. if (end_pos < 0) {
  32020. end_pos = len + end_pos;
  32021. }
  32022. }
  32023. DUK_ASSERT(start_pos >= 0 && start_pos <= len);
  32024. DUK_ASSERT(end_pos >= 0 && end_pos <= len);
  32025. if (end_pos < start_pos) {
  32026. end_pos = start_pos;
  32027. }
  32028. DUK_ASSERT(end_pos >= start_pos);
  32029. duk_substring(ctx, -1, (duk_size_t) start_pos, (duk_size_t) end_pos);
  32030. return 1;
  32031. }
  32032. /*
  32033. * Case conversion
  32034. */
  32035. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_caseconv_shared(duk_context *ctx) {
  32036. duk_hthread *thr = (duk_hthread *) ctx;
  32037. duk_small_int_t uppercase = duk_get_current_magic(ctx);
  32038. (void) duk_push_this_coercible_to_string(ctx);
  32039. duk_unicode_case_convert_string(thr, (duk_bool_t) uppercase);
  32040. return 1;
  32041. }
  32042. /*
  32043. * indexOf() and lastIndexOf()
  32044. */
  32045. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_indexof_shared(duk_context *ctx) {
  32046. duk_hthread *thr = (duk_hthread *) ctx;
  32047. duk_hstring *h_this;
  32048. duk_hstring *h_search;
  32049. duk_int_t clen_this;
  32050. duk_int_t cpos;
  32051. duk_int_t bpos;
  32052. const duk_uint8_t *p_start, *p_end, *p;
  32053. const duk_uint8_t *q_start;
  32054. duk_int_t q_blen;
  32055. duk_uint8_t firstbyte;
  32056. duk_uint8_t t;
  32057. duk_small_int_t is_lastindexof = duk_get_current_magic(ctx); /* 0=indexOf, 1=lastIndexOf */
  32058. h_this = duk_push_this_coercible_to_string(ctx);
  32059. DUK_ASSERT(h_this != NULL);
  32060. clen_this = (duk_int_t) DUK_HSTRING_GET_CHARLEN(h_this);
  32061. h_search = duk_to_hstring(ctx, 0);
  32062. DUK_ASSERT(h_search != NULL);
  32063. q_start = DUK_HSTRING_GET_DATA(h_search);
  32064. q_blen = (duk_int_t) DUK_HSTRING_GET_BYTELEN(h_search);
  32065. duk_to_number(ctx, 1);
  32066. if (duk_is_nan(ctx, 1) && is_lastindexof) {
  32067. /* indexOf: NaN should cause pos to be zero.
  32068. * lastIndexOf: NaN should cause pos to be +Infinity
  32069. * (and later be clamped to len).
  32070. */
  32071. cpos = clen_this;
  32072. } else {
  32073. cpos = duk_to_int_clamped(ctx, 1, 0, clen_this);
  32074. }
  32075. /* Empty searchstring always matches; cpos must be clamped here.
  32076. * (If q_blen were < 0 due to clamped coercion, it would also be
  32077. * caught here.)
  32078. */
  32079. if (q_blen <= 0) {
  32080. duk_push_int(ctx, cpos);
  32081. return 1;
  32082. }
  32083. DUK_ASSERT(q_blen > 0);
  32084. bpos = (duk_int_t) duk_heap_strcache_offset_char2byte(thr, h_this, (duk_uint32_t) cpos);
  32085. p_start = DUK_HSTRING_GET_DATA(h_this);
  32086. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_this);
  32087. p = p_start + bpos;
  32088. /* This loop is optimized for size. For speed, there should be
  32089. * two separate loops, and we should ensure that memcmp() can be
  32090. * used without an extra "will searchstring fit" check. Doing
  32091. * the preconditioning for 'p' and 'p_end' is easy but cpos
  32092. * must be updated if 'p' is wound back (backward scanning).
  32093. */
  32094. firstbyte = q_start[0]; /* leading byte of match string */
  32095. while (p <= p_end && p >= p_start) {
  32096. t = *p;
  32097. /* For Ecmascript strings, this check can only match for
  32098. * initial UTF-8 bytes (not continuation bytes). For other
  32099. * strings all bets are off.
  32100. */
  32101. if ((t == firstbyte) && ((duk_size_t) (p_end - p) >= (duk_size_t) q_blen)) {
  32102. DUK_ASSERT(q_blen > 0); /* no issues with memcmp() zero size, even if broken */
  32103. if (DUK_MEMCMP((const void *) p, (const void *) q_start, (size_t) q_blen) == 0) {
  32104. duk_push_int(ctx, cpos);
  32105. return 1;
  32106. }
  32107. }
  32108. /* track cpos while scanning */
  32109. if (is_lastindexof) {
  32110. /* when going backwards, we decrement cpos 'early';
  32111. * 'p' may point to a continuation byte of the char
  32112. * at offset 'cpos', but that's OK because we'll
  32113. * backtrack all the way to the initial byte.
  32114. */
  32115. if ((t & 0xc0) != 0x80) {
  32116. cpos--;
  32117. }
  32118. p--;
  32119. } else {
  32120. if ((t & 0xc0) != 0x80) {
  32121. cpos++;
  32122. }
  32123. p++;
  32124. }
  32125. }
  32126. /* Not found. Empty string case is handled specially above. */
  32127. duk_push_int(ctx, -1);
  32128. return 1;
  32129. }
  32130. /*
  32131. * replace()
  32132. */
  32133. /* XXX: the current implementation works but is quite clunky; it compiles
  32134. * to almost 1,4kB of x86 code so it needs to be simplified (better approach,
  32135. * shared helpers, etc). Some ideas for refactoring:
  32136. *
  32137. * - a primitive to convert a string into a regexp matcher (reduces matching
  32138. * code at the cost of making matching much slower)
  32139. * - use replace() as a basic helper for match() and split(), which are both
  32140. * much simpler
  32141. * - API call to get_prop and to_boolean
  32142. */
  32143. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_replace(duk_context *ctx) {
  32144. duk_hthread *thr = (duk_hthread *) ctx;
  32145. duk_hstring *h_input;
  32146. duk_hstring *h_match;
  32147. duk_hstring *h_search;
  32148. duk_hobject *h_re;
  32149. duk_bufwriter_ctx bw_alloc;
  32150. duk_bufwriter_ctx *bw;
  32151. #ifdef DUK_USE_REGEXP_SUPPORT
  32152. duk_bool_t is_regexp;
  32153. duk_bool_t is_global;
  32154. #endif
  32155. duk_bool_t is_repl_func;
  32156. duk_uint32_t match_start_coff, match_start_boff;
  32157. #ifdef DUK_USE_REGEXP_SUPPORT
  32158. duk_int_t match_caps;
  32159. #endif
  32160. duk_uint32_t prev_match_end_boff;
  32161. const duk_uint8_t *r_start, *r_end, *r; /* repl string scan */
  32162. duk_size_t tmp_sz;
  32163. DUK_ASSERT_TOP(ctx, 2);
  32164. h_input = duk_push_this_coercible_to_string(ctx);
  32165. DUK_ASSERT(h_input != NULL);
  32166. bw = &bw_alloc;
  32167. DUK_BW_INIT_PUSHBUF(thr, bw, DUK_HSTRING_GET_BYTELEN(h_input)); /* input size is good output starting point */
  32168. DUK_ASSERT_TOP(ctx, 4);
  32169. /* stack[0] = search value
  32170. * stack[1] = replace value
  32171. * stack[2] = input string
  32172. * stack[3] = result buffer
  32173. */
  32174. h_re = duk_get_hobject_with_class(ctx, 0, DUK_HOBJECT_CLASS_REGEXP);
  32175. if (h_re) {
  32176. #ifdef DUK_USE_REGEXP_SUPPORT
  32177. is_regexp = 1;
  32178. is_global = duk_get_prop_stridx_boolean(ctx, 0, DUK_STRIDX_GLOBAL, NULL);
  32179. if (is_global) {
  32180. /* start match from beginning */
  32181. duk_push_int(ctx, 0);
  32182. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LAST_INDEX);
  32183. }
  32184. #else /* DUK_USE_REGEXP_SUPPORT */
  32185. return DUK_RET_UNSUPPORTED_ERROR;
  32186. #endif /* DUK_USE_REGEXP_SUPPORT */
  32187. } else {
  32188. duk_to_string(ctx, 0);
  32189. #ifdef DUK_USE_REGEXP_SUPPORT
  32190. is_regexp = 0;
  32191. is_global = 0;
  32192. #endif
  32193. }
  32194. if (duk_is_function(ctx, 1)) {
  32195. is_repl_func = 1;
  32196. r_start = NULL;
  32197. r_end = NULL;
  32198. } else {
  32199. duk_hstring *h_repl;
  32200. is_repl_func = 0;
  32201. h_repl = duk_to_hstring(ctx, 1);
  32202. DUK_ASSERT(h_repl != NULL);
  32203. r_start = DUK_HSTRING_GET_DATA(h_repl);
  32204. r_end = r_start + DUK_HSTRING_GET_BYTELEN(h_repl);
  32205. }
  32206. prev_match_end_boff = 0;
  32207. for (;;) {
  32208. /*
  32209. * If matching with a regexp:
  32210. * - non-global RegExp: lastIndex not touched on a match, zeroed
  32211. * on a non-match
  32212. * - global RegExp: on match, lastIndex will be updated by regexp
  32213. * executor to point to next char after the matching part (so that
  32214. * characters in the matching part are not matched again)
  32215. *
  32216. * If matching with a string:
  32217. * - always non-global match, find first occurrence
  32218. *
  32219. * We need:
  32220. * - The character offset of start-of-match for the replacer function
  32221. * - The byte offsets for start-of-match and end-of-match to implement
  32222. * the replacement values $&, $`, and $', and to copy non-matching
  32223. * input string portions (including header and trailer) verbatim.
  32224. *
  32225. * NOTE: the E5.1 specification is a bit vague how the RegExp should
  32226. * behave in the replacement process; e.g. is matching done first for
  32227. * all matches (in the global RegExp case) before any replacer calls
  32228. * are made? See: test-bi-string-proto-replace.js for discussion.
  32229. */
  32230. DUK_ASSERT_TOP(ctx, 4);
  32231. #ifdef DUK_USE_REGEXP_SUPPORT
  32232. if (is_regexp) {
  32233. duk_dup(ctx, 0);
  32234. duk_dup(ctx, 2);
  32235. duk_regexp_match(thr); /* [ ... regexp input ] -> [ res_obj ] */
  32236. if (!duk_is_object(ctx, -1)) {
  32237. duk_pop(ctx);
  32238. break;
  32239. }
  32240. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INDEX);
  32241. DUK_ASSERT(duk_is_number(ctx, -1));
  32242. match_start_coff = duk_get_int(ctx, -1);
  32243. duk_pop(ctx);
  32244. duk_get_prop_index(ctx, -1, 0);
  32245. DUK_ASSERT(duk_is_string(ctx, -1));
  32246. h_match = duk_get_hstring(ctx, -1);
  32247. DUK_ASSERT(h_match != NULL);
  32248. duk_pop(ctx); /* h_match is borrowed, remains reachable through match_obj */
  32249. if (DUK_HSTRING_GET_BYTELEN(h_match) == 0) {
  32250. /* This should be equivalent to match() algorithm step 8.f.iii.2:
  32251. * detect an empty match and allow it, but don't allow it twice.
  32252. */
  32253. duk_uint32_t last_index;
  32254. duk_get_prop_stridx(ctx, 0, DUK_STRIDX_LAST_INDEX);
  32255. last_index = (duk_uint32_t) duk_get_uint(ctx, -1);
  32256. DUK_DDD(DUK_DDDPRINT("empty match, bump lastIndex: %ld -> %ld",
  32257. (long) last_index, (long) (last_index + 1)));
  32258. duk_pop(ctx);
  32259. duk_push_int(ctx, last_index + 1);
  32260. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LAST_INDEX);
  32261. }
  32262. DUK_ASSERT(duk_get_length(ctx, -1) <= DUK_INT_MAX); /* string limits */
  32263. match_caps = (duk_int_t) duk_get_length(ctx, -1);
  32264. } else {
  32265. #else /* DUK_USE_REGEXP_SUPPORT */
  32266. { /* unconditionally */
  32267. #endif /* DUK_USE_REGEXP_SUPPORT */
  32268. const duk_uint8_t *p_start, *p_end, *p; /* input string scan */
  32269. const duk_uint8_t *q_start; /* match string */
  32270. duk_size_t q_blen;
  32271. #ifdef DUK_USE_REGEXP_SUPPORT
  32272. DUK_ASSERT(!is_global); /* single match always */
  32273. #endif
  32274. p_start = DUK_HSTRING_GET_DATA(h_input);
  32275. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  32276. p = p_start;
  32277. h_search = duk_get_hstring(ctx, 0);
  32278. DUK_ASSERT(h_search != NULL);
  32279. q_start = DUK_HSTRING_GET_DATA(h_search);
  32280. q_blen = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h_search);
  32281. p_end -= q_blen; /* ensure full memcmp() fits in while */
  32282. match_start_coff = 0;
  32283. while (p <= p_end) {
  32284. DUK_ASSERT(p + q_blen <= DUK_HSTRING_GET_DATA(h_input) + DUK_HSTRING_GET_BYTELEN(h_input));
  32285. if (DUK_MEMCMP((const void *) p, (const void *) q_start, (size_t) q_blen) == 0) {
  32286. duk_dup(ctx, 0);
  32287. h_match = duk_get_hstring(ctx, -1);
  32288. DUK_ASSERT(h_match != NULL);
  32289. #ifdef DUK_USE_REGEXP_SUPPORT
  32290. match_caps = 0;
  32291. #endif
  32292. goto found;
  32293. }
  32294. /* track utf-8 non-continuation bytes */
  32295. if ((p[0] & 0xc0) != 0x80) {
  32296. match_start_coff++;
  32297. }
  32298. p++;
  32299. }
  32300. /* not found */
  32301. break;
  32302. }
  32303. found:
  32304. /* stack[0] = search value
  32305. * stack[1] = replace value
  32306. * stack[2] = input string
  32307. * stack[3] = result buffer
  32308. * stack[4] = regexp match OR match string
  32309. */
  32310. match_start_boff = duk_heap_strcache_offset_char2byte(thr, h_input, match_start_coff);
  32311. tmp_sz = (duk_size_t) (match_start_boff - prev_match_end_boff);
  32312. DUK_BW_WRITE_ENSURE_BYTES(thr, bw, DUK_HSTRING_GET_DATA(h_input) + prev_match_end_boff, tmp_sz);
  32313. prev_match_end_boff = match_start_boff + DUK_HSTRING_GET_BYTELEN(h_match);
  32314. if (is_repl_func) {
  32315. duk_idx_t idx_args;
  32316. duk_hstring *h_repl;
  32317. /* regexp res_obj is at index 4 */
  32318. duk_dup(ctx, 1);
  32319. idx_args = duk_get_top(ctx);
  32320. #ifdef DUK_USE_REGEXP_SUPPORT
  32321. if (is_regexp) {
  32322. duk_int_t idx;
  32323. duk_require_stack(ctx, match_caps + 2);
  32324. for (idx = 0; idx < match_caps; idx++) {
  32325. /* match followed by capture(s) */
  32326. duk_get_prop_index(ctx, 4, idx);
  32327. }
  32328. } else {
  32329. #else /* DUK_USE_REGEXP_SUPPORT */
  32330. { /* unconditionally */
  32331. #endif /* DUK_USE_REGEXP_SUPPORT */
  32332. /* match == search string, by definition */
  32333. duk_dup(ctx, 0);
  32334. }
  32335. duk_push_int(ctx, match_start_coff);
  32336. duk_dup(ctx, 2);
  32337. /* [ ... replacer match [captures] match_char_offset input ] */
  32338. duk_call(ctx, duk_get_top(ctx) - idx_args);
  32339. h_repl = duk_to_hstring(ctx, -1); /* -> [ ... repl_value ] */
  32340. DUK_ASSERT(h_repl != NULL);
  32341. DUK_BW_WRITE_ENSURE_HSTRING(thr, bw, h_repl);
  32342. duk_pop(ctx); /* repl_value */
  32343. } else {
  32344. r = r_start;
  32345. while (r < r_end) {
  32346. duk_int_t ch1;
  32347. duk_int_t ch2;
  32348. #ifdef DUK_USE_REGEXP_SUPPORT
  32349. duk_int_t ch3;
  32350. #endif
  32351. duk_size_t left;
  32352. ch1 = *r++;
  32353. if (ch1 != DUK_ASC_DOLLAR) {
  32354. goto repl_write;
  32355. }
  32356. left = r_end - r;
  32357. if (left <= 0) {
  32358. goto repl_write;
  32359. }
  32360. ch2 = r[0];
  32361. switch ((int) ch2) {
  32362. case DUK_ASC_DOLLAR: {
  32363. ch1 = (1 << 8) + DUK_ASC_DOLLAR;
  32364. goto repl_write;
  32365. }
  32366. case DUK_ASC_AMP: {
  32367. DUK_BW_WRITE_ENSURE_HSTRING(thr, bw, h_match);
  32368. r++;
  32369. continue;
  32370. }
  32371. case DUK_ASC_GRAVE: {
  32372. tmp_sz = (duk_size_t) match_start_boff;
  32373. DUK_BW_WRITE_ENSURE_BYTES(thr, bw, DUK_HSTRING_GET_DATA(h_input), tmp_sz);
  32374. r++;
  32375. continue;
  32376. }
  32377. case DUK_ASC_SINGLEQUOTE: {
  32378. duk_uint32_t match_end_boff;
  32379. /* Use match charlen instead of bytelen, just in case the input and
  32380. * match codepoint encodings would have different lengths.
  32381. */
  32382. match_end_boff = duk_heap_strcache_offset_char2byte(thr,
  32383. h_input,
  32384. match_start_coff + DUK_HSTRING_GET_CHARLEN(h_match));
  32385. tmp_sz = (duk_size_t) (DUK_HSTRING_GET_BYTELEN(h_input) - match_end_boff);
  32386. DUK_BW_WRITE_ENSURE_BYTES(thr, bw, DUK_HSTRING_GET_DATA(h_input) + match_end_boff, tmp_sz);
  32387. r++;
  32388. continue;
  32389. }
  32390. default: {
  32391. #ifdef DUK_USE_REGEXP_SUPPORT
  32392. duk_int_t capnum, captmp, capadv;
  32393. /* XXX: optional check, match_caps is zero if no regexp,
  32394. * so dollar will be interpreted literally anyway.
  32395. */
  32396. if (!is_regexp) {
  32397. goto repl_write;
  32398. }
  32399. if (!(ch2 >= DUK_ASC_0 && ch2 <= DUK_ASC_9)) {
  32400. goto repl_write;
  32401. }
  32402. capnum = ch2 - DUK_ASC_0;
  32403. capadv = 1;
  32404. if (left >= 2) {
  32405. ch3 = r[1];
  32406. if (ch3 >= DUK_ASC_0 && ch3 <= DUK_ASC_9) {
  32407. captmp = capnum * 10 + (ch3 - DUK_ASC_0);
  32408. if (captmp < match_caps) {
  32409. capnum = captmp;
  32410. capadv = 2;
  32411. }
  32412. }
  32413. }
  32414. if (capnum > 0 && capnum < match_caps) {
  32415. DUK_ASSERT(is_regexp != 0); /* match_caps == 0 without regexps */
  32416. /* regexp res_obj is at offset 4 */
  32417. duk_get_prop_index(ctx, 4, (duk_uarridx_t) capnum);
  32418. if (duk_is_string(ctx, -1)) {
  32419. duk_hstring *h_tmp_str;
  32420. h_tmp_str = duk_get_hstring(ctx, -1);
  32421. DUK_ASSERT(h_tmp_str != NULL);
  32422. DUK_BW_WRITE_ENSURE_HSTRING(thr, bw, h_tmp_str);
  32423. } else {
  32424. /* undefined -> skip (replaced with empty) */
  32425. }
  32426. duk_pop(ctx);
  32427. r += capadv;
  32428. continue;
  32429. } else {
  32430. goto repl_write;
  32431. }
  32432. #else /* DUK_USE_REGEXP_SUPPORT */
  32433. goto repl_write; /* unconditionally */
  32434. #endif /* DUK_USE_REGEXP_SUPPORT */
  32435. } /* default case */
  32436. } /* switch (ch2) */
  32437. repl_write:
  32438. /* ch1 = (r_increment << 8) + byte */
  32439. DUK_BW_WRITE_ENSURE_U8(thr, bw, (duk_uint8_t) (ch1 & 0xff));
  32440. r += ch1 >> 8;
  32441. } /* while repl */
  32442. } /* if (is_repl_func) */
  32443. duk_pop(ctx); /* pop regexp res_obj or match string */
  32444. #ifdef DUK_USE_REGEXP_SUPPORT
  32445. if (!is_global) {
  32446. #else
  32447. { /* unconditionally; is_global==0 */
  32448. #endif
  32449. break;
  32450. }
  32451. }
  32452. /* trailer */
  32453. tmp_sz = (duk_size_t) (DUK_HSTRING_GET_BYTELEN(h_input) - prev_match_end_boff);
  32454. DUK_BW_WRITE_ENSURE_BYTES(thr, bw, DUK_HSTRING_GET_DATA(h_input) + prev_match_end_boff, tmp_sz);
  32455. DUK_ASSERT_TOP(ctx, 4);
  32456. DUK_BW_COMPACT(thr, bw);
  32457. duk_to_string(ctx, -1);
  32458. return 1;
  32459. }
  32460. /*
  32461. * split()
  32462. */
  32463. /* XXX: very messy now, but works; clean up, remove unused variables (nomimally
  32464. * used so compiler doesn't complain).
  32465. */
  32466. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_split(duk_context *ctx) {
  32467. duk_hthread *thr = (duk_hthread *) ctx;
  32468. duk_hstring *h_input;
  32469. duk_hstring *h_sep;
  32470. duk_uint32_t limit;
  32471. duk_uint32_t arr_idx;
  32472. #ifdef DUK_USE_REGEXP_SUPPORT
  32473. duk_bool_t is_regexp;
  32474. #endif
  32475. duk_bool_t matched; /* set to 1 if any match exists (needed for empty input special case) */
  32476. duk_uint32_t prev_match_end_coff, prev_match_end_boff;
  32477. duk_uint32_t match_start_boff, match_start_coff;
  32478. duk_uint32_t match_end_boff, match_end_coff;
  32479. DUK_UNREF(thr);
  32480. h_input = duk_push_this_coercible_to_string(ctx);
  32481. DUK_ASSERT(h_input != NULL);
  32482. duk_push_array(ctx);
  32483. if (duk_is_undefined(ctx, 1)) {
  32484. limit = 0xffffffffUL;
  32485. } else {
  32486. limit = duk_to_uint32(ctx, 1);
  32487. }
  32488. if (limit == 0) {
  32489. return 1;
  32490. }
  32491. /* If the separator is a RegExp, make a "clone" of it. The specification
  32492. * algorithm calls [[Match]] directly for specific indices; we emulate this
  32493. * by tweaking lastIndex and using a "force global" variant of duk_regexp_match()
  32494. * which will use global-style matching even when the RegExp itself is non-global.
  32495. */
  32496. if (duk_is_undefined(ctx, 0)) {
  32497. /* The spec algorithm first does "R = ToString(separator)" before checking
  32498. * whether separator is undefined. Since this is side effect free, we can
  32499. * skip the ToString() here.
  32500. */
  32501. duk_dup(ctx, 2);
  32502. duk_put_prop_index(ctx, 3, 0);
  32503. return 1;
  32504. } else if (duk_get_hobject_with_class(ctx, 0, DUK_HOBJECT_CLASS_REGEXP) != NULL) {
  32505. #ifdef DUK_USE_REGEXP_SUPPORT
  32506. duk_push_hobject_bidx(ctx, DUK_BIDX_REGEXP_CONSTRUCTOR);
  32507. duk_dup(ctx, 0);
  32508. duk_new(ctx, 1); /* [ ... RegExp val ] -> [ ... res ] */
  32509. duk_replace(ctx, 0);
  32510. /* lastIndex is initialized to zero by new RegExp() */
  32511. is_regexp = 1;
  32512. #else
  32513. return DUK_RET_UNSUPPORTED_ERROR;
  32514. #endif
  32515. } else {
  32516. duk_to_string(ctx, 0);
  32517. #ifdef DUK_USE_REGEXP_SUPPORT
  32518. is_regexp = 0;
  32519. #endif
  32520. }
  32521. /* stack[0] = separator (string or regexp)
  32522. * stack[1] = limit
  32523. * stack[2] = input string
  32524. * stack[3] = result array
  32525. */
  32526. prev_match_end_boff = 0;
  32527. prev_match_end_coff = 0;
  32528. arr_idx = 0;
  32529. matched = 0;
  32530. for (;;) {
  32531. /*
  32532. * The specification uses RegExp [[Match]] to attempt match at specific
  32533. * offsets. We don't have such a primitive, so we use an actual RegExp
  32534. * and tweak lastIndex. Since the RegExp may be non-global, we use a
  32535. * special variant which forces global-like behavior for matching.
  32536. */
  32537. DUK_ASSERT_TOP(ctx, 4);
  32538. #ifdef DUK_USE_REGEXP_SUPPORT
  32539. if (is_regexp) {
  32540. duk_dup(ctx, 0);
  32541. duk_dup(ctx, 2);
  32542. duk_regexp_match_force_global(thr); /* [ ... regexp input ] -> [ res_obj ] */
  32543. if (!duk_is_object(ctx, -1)) {
  32544. duk_pop(ctx);
  32545. break;
  32546. }
  32547. matched = 1;
  32548. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INDEX);
  32549. DUK_ASSERT(duk_is_number(ctx, -1));
  32550. match_start_coff = duk_get_int(ctx, -1);
  32551. match_start_boff = duk_heap_strcache_offset_char2byte(thr, h_input, match_start_coff);
  32552. duk_pop(ctx);
  32553. if (match_start_coff == DUK_HSTRING_GET_CHARLEN(h_input)) {
  32554. /* don't allow an empty match at the end of the string */
  32555. duk_pop(ctx);
  32556. break;
  32557. }
  32558. duk_get_prop_stridx(ctx, 0, DUK_STRIDX_LAST_INDEX);
  32559. DUK_ASSERT(duk_is_number(ctx, -1));
  32560. match_end_coff = duk_get_int(ctx, -1);
  32561. match_end_boff = duk_heap_strcache_offset_char2byte(thr, h_input, match_end_coff);
  32562. duk_pop(ctx);
  32563. /* empty match -> bump and continue */
  32564. if (prev_match_end_boff == match_end_boff) {
  32565. duk_push_int(ctx, match_end_coff + 1);
  32566. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LAST_INDEX);
  32567. duk_pop(ctx);
  32568. continue;
  32569. }
  32570. } else {
  32571. #else /* DUK_USE_REGEXP_SUPPORT */
  32572. { /* unconditionally */
  32573. #endif /* DUK_USE_REGEXP_SUPPORT */
  32574. const duk_uint8_t *p_start, *p_end, *p; /* input string scan */
  32575. const duk_uint8_t *q_start; /* match string */
  32576. duk_size_t q_blen, q_clen;
  32577. p_start = DUK_HSTRING_GET_DATA(h_input);
  32578. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  32579. p = p_start + prev_match_end_boff;
  32580. h_sep = duk_get_hstring(ctx, 0);
  32581. DUK_ASSERT(h_sep != NULL);
  32582. q_start = DUK_HSTRING_GET_DATA(h_sep);
  32583. q_blen = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h_sep);
  32584. q_clen = (duk_size_t) DUK_HSTRING_GET_CHARLEN(h_sep);
  32585. p_end -= q_blen; /* ensure full memcmp() fits in while */
  32586. match_start_coff = prev_match_end_coff;
  32587. if (q_blen == 0) {
  32588. /* Handle empty separator case: it will always match, and always
  32589. * triggers the check in step 13.c.iii initially. Note that we
  32590. * must skip to either end of string or start of first codepoint,
  32591. * skipping over any continuation bytes!
  32592. *
  32593. * Don't allow an empty string to match at the end of the input.
  32594. */
  32595. matched = 1; /* empty separator can always match */
  32596. match_start_coff++;
  32597. p++;
  32598. while (p < p_end) {
  32599. if ((p[0] & 0xc0) != 0x80) {
  32600. goto found;
  32601. }
  32602. p++;
  32603. }
  32604. goto not_found;
  32605. }
  32606. DUK_ASSERT(q_blen > 0 && q_clen > 0);
  32607. while (p <= p_end) {
  32608. DUK_ASSERT(p + q_blen <= DUK_HSTRING_GET_DATA(h_input) + DUK_HSTRING_GET_BYTELEN(h_input));
  32609. DUK_ASSERT(q_blen > 0); /* no issues with empty memcmp() */
  32610. if (DUK_MEMCMP((const void *) p, (const void *) q_start, (size_t) q_blen) == 0) {
  32611. /* never an empty match, so step 13.c.iii can't be triggered */
  32612. goto found;
  32613. }
  32614. /* track utf-8 non-continuation bytes */
  32615. if ((p[0] & 0xc0) != 0x80) {
  32616. match_start_coff++;
  32617. }
  32618. p++;
  32619. }
  32620. not_found:
  32621. /* not found */
  32622. break;
  32623. found:
  32624. matched = 1;
  32625. match_start_boff = (duk_uint32_t) (p - p_start);
  32626. match_end_coff = (duk_uint32_t) (match_start_coff + q_clen); /* constrained by string length */
  32627. match_end_boff = (duk_uint32_t) (match_start_boff + q_blen); /* ditto */
  32628. /* empty match (may happen with empty separator) -> bump and continue */
  32629. if (prev_match_end_boff == match_end_boff) {
  32630. prev_match_end_boff++;
  32631. prev_match_end_coff++;
  32632. continue;
  32633. }
  32634. } /* if (is_regexp) */
  32635. /* stack[0] = separator (string or regexp)
  32636. * stack[1] = limit
  32637. * stack[2] = input string
  32638. * stack[3] = result array
  32639. * stack[4] = regexp res_obj (if is_regexp)
  32640. */
  32641. DUK_DDD(DUK_DDDPRINT("split; match_start b=%ld,c=%ld, match_end b=%ld,c=%ld, prev_end b=%ld,c=%ld",
  32642. (long) match_start_boff, (long) match_start_coff,
  32643. (long) match_end_boff, (long) match_end_coff,
  32644. (long) prev_match_end_boff, (long) prev_match_end_coff));
  32645. duk_push_lstring(ctx,
  32646. (const char *) (DUK_HSTRING_GET_DATA(h_input) + prev_match_end_boff),
  32647. (duk_size_t) (match_start_boff - prev_match_end_boff));
  32648. duk_put_prop_index(ctx, 3, arr_idx);
  32649. arr_idx++;
  32650. if (arr_idx >= limit) {
  32651. goto hit_limit;
  32652. }
  32653. #ifdef DUK_USE_REGEXP_SUPPORT
  32654. if (is_regexp) {
  32655. duk_size_t i, len;
  32656. len = duk_get_length(ctx, 4);
  32657. for (i = 1; i < len; i++) {
  32658. DUK_ASSERT(i <= DUK_UARRIDX_MAX); /* cannot have >4G captures */
  32659. duk_get_prop_index(ctx, 4, (duk_uarridx_t) i);
  32660. duk_put_prop_index(ctx, 3, arr_idx);
  32661. arr_idx++;
  32662. if (arr_idx >= limit) {
  32663. goto hit_limit;
  32664. }
  32665. }
  32666. duk_pop(ctx);
  32667. /* lastIndex already set up for next match */
  32668. } else {
  32669. #else /* DUK_USE_REGEXP_SUPPORT */
  32670. { /* unconditionally */
  32671. #endif /* DUK_USE_REGEXP_SUPPORT */
  32672. /* no action */
  32673. }
  32674. prev_match_end_boff = match_end_boff;
  32675. prev_match_end_coff = match_end_coff;
  32676. continue;
  32677. } /* for */
  32678. /* Combined step 11 (empty string special case) and 14-15. */
  32679. DUK_DDD(DUK_DDDPRINT("split trailer; prev_end b=%ld,c=%ld",
  32680. (long) prev_match_end_boff, (long) prev_match_end_coff));
  32681. if (DUK_HSTRING_GET_CHARLEN(h_input) > 0 || !matched) {
  32682. /* Add trailer if:
  32683. * a) non-empty input
  32684. * b) empty input and no (zero size) match found (step 11)
  32685. */
  32686. duk_push_lstring(ctx,
  32687. (const char *) DUK_HSTRING_GET_DATA(h_input) + prev_match_end_boff,
  32688. (duk_size_t) (DUK_HSTRING_GET_BYTELEN(h_input) - prev_match_end_boff));
  32689. duk_put_prop_index(ctx, 3, arr_idx);
  32690. /* No arr_idx update or limit check */
  32691. }
  32692. return 1;
  32693. hit_limit:
  32694. #ifdef DUK_USE_REGEXP_SUPPORT
  32695. if (is_regexp) {
  32696. duk_pop(ctx);
  32697. }
  32698. #endif
  32699. return 1;
  32700. }
  32701. /*
  32702. * Various
  32703. */
  32704. #ifdef DUK_USE_REGEXP_SUPPORT
  32705. DUK_LOCAL void duk__to_regexp_helper(duk_context *ctx, duk_idx_t index, duk_bool_t force_new) {
  32706. duk_hobject *h;
  32707. /* Shared helper for match() steps 3-4, search() steps 3-4. */
  32708. DUK_ASSERT(index >= 0);
  32709. if (force_new) {
  32710. goto do_new;
  32711. }
  32712. h = duk_get_hobject_with_class(ctx, index, DUK_HOBJECT_CLASS_REGEXP);
  32713. if (!h) {
  32714. goto do_new;
  32715. }
  32716. return;
  32717. do_new:
  32718. duk_push_hobject_bidx(ctx, DUK_BIDX_REGEXP_CONSTRUCTOR);
  32719. duk_dup(ctx, index);
  32720. duk_new(ctx, 1); /* [ ... RegExp val ] -> [ ... res ] */
  32721. duk_replace(ctx, index);
  32722. }
  32723. #endif /* DUK_USE_REGEXP_SUPPORT */
  32724. #ifdef DUK_USE_REGEXP_SUPPORT
  32725. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_search(duk_context *ctx) {
  32726. duk_hthread *thr = (duk_hthread *) ctx;
  32727. /* Easiest way to implement the search required by the specification
  32728. * is to do a RegExp test() with lastIndex forced to zero. To avoid
  32729. * side effects on the argument, "clone" the RegExp if a RegExp was
  32730. * given as input.
  32731. *
  32732. * The global flag of the RegExp should be ignored; setting lastIndex
  32733. * to zero (which happens when "cloning" the RegExp) should have an
  32734. * equivalent effect.
  32735. */
  32736. DUK_ASSERT_TOP(ctx, 1);
  32737. (void) duk_push_this_coercible_to_string(ctx); /* at index 1 */
  32738. duk__to_regexp_helper(ctx, 0 /*index*/, 1 /*force_new*/);
  32739. /* stack[0] = regexp
  32740. * stack[1] = string
  32741. */
  32742. /* Avoid using RegExp.prototype methods, as they're writable and
  32743. * configurable and may have been changed.
  32744. */
  32745. duk_dup(ctx, 0);
  32746. duk_dup(ctx, 1); /* [ ... re_obj input ] */
  32747. duk_regexp_match(thr); /* -> [ ... res_obj ] */
  32748. if (!duk_is_object(ctx, -1)) {
  32749. duk_push_int(ctx, -1);
  32750. return 1;
  32751. }
  32752. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INDEX);
  32753. DUK_ASSERT(duk_is_number(ctx, -1));
  32754. return 1;
  32755. }
  32756. #else /* DUK_USE_REGEXP_SUPPORT */
  32757. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_search(duk_context *ctx) {
  32758. DUK_UNREF(ctx);
  32759. return DUK_RET_UNSUPPORTED_ERROR;
  32760. }
  32761. #endif /* DUK_USE_REGEXP_SUPPORT */
  32762. #ifdef DUK_USE_REGEXP_SUPPORT
  32763. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_match(duk_context *ctx) {
  32764. duk_hthread *thr = (duk_hthread *) ctx;
  32765. duk_bool_t global;
  32766. duk_int_t prev_last_index;
  32767. duk_int_t this_index;
  32768. duk_int_t arr_idx;
  32769. DUK_ASSERT_TOP(ctx, 1);
  32770. (void) duk_push_this_coercible_to_string(ctx);
  32771. duk__to_regexp_helper(ctx, 0 /*index*/, 0 /*force_new*/);
  32772. global = duk_get_prop_stridx_boolean(ctx, 0, DUK_STRIDX_GLOBAL, NULL);
  32773. DUK_ASSERT_TOP(ctx, 2);
  32774. /* stack[0] = regexp
  32775. * stack[1] = string
  32776. */
  32777. if (!global) {
  32778. duk_regexp_match(thr); /* -> [ res_obj ] */
  32779. return 1; /* return 'res_obj' */
  32780. }
  32781. /* Global case is more complex. */
  32782. /* [ regexp string ] */
  32783. duk_push_int(ctx, 0);
  32784. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LAST_INDEX);
  32785. duk_push_array(ctx);
  32786. /* [ regexp string res_arr ] */
  32787. prev_last_index = 0;
  32788. arr_idx = 0;
  32789. for (;;) {
  32790. DUK_ASSERT_TOP(ctx, 3);
  32791. duk_dup(ctx, 0);
  32792. duk_dup(ctx, 1);
  32793. duk_regexp_match(thr); /* -> [ ... regexp string ] -> [ ... res_obj ] */
  32794. if (!duk_is_object(ctx, -1)) {
  32795. duk_pop(ctx);
  32796. break;
  32797. }
  32798. duk_get_prop_stridx(ctx, 0, DUK_STRIDX_LAST_INDEX);
  32799. DUK_ASSERT(duk_is_number(ctx, -1));
  32800. this_index = duk_get_int(ctx, -1);
  32801. duk_pop(ctx);
  32802. if (this_index == prev_last_index) {
  32803. this_index++;
  32804. duk_push_int(ctx, this_index);
  32805. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LAST_INDEX);
  32806. }
  32807. prev_last_index = this_index;
  32808. duk_get_prop_index(ctx, -1, 0); /* match string */
  32809. duk_put_prop_index(ctx, 2, arr_idx);
  32810. arr_idx++;
  32811. duk_pop(ctx); /* res_obj */
  32812. }
  32813. if (arr_idx == 0) {
  32814. duk_push_null(ctx);
  32815. }
  32816. return 1; /* return 'res_arr' or 'null' */
  32817. }
  32818. #else /* DUK_USE_REGEXP_SUPPORT */
  32819. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_match(duk_context *ctx) {
  32820. DUK_UNREF(ctx);
  32821. return DUK_RET_UNSUPPORTED_ERROR;
  32822. }
  32823. #endif /* DUK_USE_REGEXP_SUPPORT */
  32824. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_concat(duk_context *ctx) {
  32825. /* duk_concat() coerces arguments with ToString() in correct order */
  32826. (void) duk_push_this_coercible_to_string(ctx);
  32827. duk_insert(ctx, 0); /* this is relatively expensive */
  32828. duk_concat(ctx, duk_get_top(ctx));
  32829. return 1;
  32830. }
  32831. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_trim(duk_context *ctx) {
  32832. DUK_ASSERT_TOP(ctx, 0);
  32833. (void) duk_push_this_coercible_to_string(ctx);
  32834. duk_trim(ctx, 0);
  32835. DUK_ASSERT_TOP(ctx, 1);
  32836. return 1;
  32837. }
  32838. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_locale_compare(duk_context *ctx) {
  32839. duk_hstring *h1;
  32840. duk_hstring *h2;
  32841. duk_size_t h1_len, h2_len, prefix_len;
  32842. duk_small_int_t ret = 0;
  32843. duk_small_int_t rc;
  32844. /* The current implementation of localeCompare() is simply a codepoint
  32845. * by codepoint comparison, implemented with a simple string compare
  32846. * because UTF-8 should preserve codepoint ordering (assuming valid
  32847. * shortest UTF-8 encoding).
  32848. *
  32849. * The specification requires that the return value must be related
  32850. * to the sort order: e.g. negative means that 'this' comes before
  32851. * 'that' in sort order. We assume an ascending sort order.
  32852. */
  32853. /* XXX: could share code with duk_js_ops.c, duk_js_compare_helper */
  32854. h1 = duk_push_this_coercible_to_string(ctx);
  32855. DUK_ASSERT(h1 != NULL);
  32856. h2 = duk_to_hstring(ctx, 0);
  32857. DUK_ASSERT(h2 != NULL);
  32858. h1_len = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h1);
  32859. h2_len = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h2);
  32860. prefix_len = (h1_len <= h2_len ? h1_len : h2_len);
  32861. /* Zero size compare not an issue with DUK_MEMCMP. */
  32862. rc = (duk_small_int_t) DUK_MEMCMP((const void *) DUK_HSTRING_GET_DATA(h1),
  32863. (const void *) DUK_HSTRING_GET_DATA(h2),
  32864. (size_t) prefix_len);
  32865. if (rc < 0) {
  32866. ret = -1;
  32867. goto done;
  32868. } else if (rc > 0) {
  32869. ret = 1;
  32870. goto done;
  32871. }
  32872. /* prefix matches, lengths matter now */
  32873. if (h1_len > h2_len) {
  32874. ret = 1;
  32875. goto done;
  32876. } else if (h1_len == h2_len) {
  32877. DUK_ASSERT(ret == 0);
  32878. goto done;
  32879. }
  32880. ret = -1;
  32881. goto done;
  32882. done:
  32883. duk_push_int(ctx, (duk_int_t) ret);
  32884. return 1;
  32885. }
  32886. #line 1 "duk_bi_thread.c"
  32887. /*
  32888. * Thread builtins
  32889. */
  32890. /* include removed: duk_internal.h */
  32891. /*
  32892. * Constructor
  32893. */
  32894. DUK_INTERNAL duk_ret_t duk_bi_thread_constructor(duk_context *ctx) {
  32895. duk_hthread *new_thr;
  32896. duk_hobject *func;
  32897. /* XXX: need a duk_require_func_or_lfunc_coerce() */
  32898. if (!duk_is_callable(ctx, 0)) {
  32899. return DUK_RET_TYPE_ERROR;
  32900. }
  32901. func = duk_require_hobject_or_lfunc_coerce(ctx, 0);
  32902. DUK_ASSERT(func != NULL);
  32903. duk_push_thread(ctx);
  32904. new_thr = (duk_hthread *) duk_get_hobject(ctx, -1);
  32905. DUK_ASSERT(new_thr != NULL);
  32906. new_thr->state = DUK_HTHREAD_STATE_INACTIVE;
  32907. /* push initial function call to new thread stack; this is
  32908. * picked up by resume().
  32909. */
  32910. duk_push_hobject((duk_context *) new_thr, func);
  32911. return 1; /* return thread */
  32912. }
  32913. /*
  32914. * Resume a thread.
  32915. *
  32916. * The thread must be in resumable state, either (a) new thread which hasn't
  32917. * yet started, or (b) a thread which has previously yielded. This method
  32918. * must be called from an Ecmascript function.
  32919. *
  32920. * Args:
  32921. * - thread
  32922. * - value
  32923. * - isError (defaults to false)
  32924. *
  32925. * Note: yield and resume handling is currently asymmetric.
  32926. */
  32927. DUK_INTERNAL duk_ret_t duk_bi_thread_resume(duk_context *ctx) {
  32928. duk_hthread *thr = (duk_hthread *) ctx;
  32929. duk_hthread *thr_resume;
  32930. duk_tval *tv;
  32931. duk_hobject *func;
  32932. duk_hobject *caller_func;
  32933. duk_small_int_t is_error;
  32934. DUK_DDD(DUK_DDDPRINT("Duktape.Thread.resume(): thread=%!T, value=%!T, is_error=%!T",
  32935. (duk_tval *) duk_get_tval(ctx, 0),
  32936. (duk_tval *) duk_get_tval(ctx, 1),
  32937. (duk_tval *) duk_get_tval(ctx, 2)));
  32938. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING);
  32939. DUK_ASSERT(thr->heap->curr_thread == thr);
  32940. thr_resume = duk_require_hthread(ctx, 0);
  32941. is_error = (duk_small_int_t) duk_to_boolean(ctx, 2);
  32942. duk_set_top(ctx, 2);
  32943. /* [ thread value ] */
  32944. /*
  32945. * Thread state and calling context checks
  32946. */
  32947. if (thr->callstack_top < 2) {
  32948. DUK_DD(DUK_DDPRINT("resume state invalid: callstack should contain at least 2 entries (caller and Duktape.Thread.resume)"));
  32949. goto state_error;
  32950. }
  32951. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1) != NULL); /* us */
  32952. DUK_ASSERT(DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1)));
  32953. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2) != NULL); /* caller */
  32954. caller_func = DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2);
  32955. if (!DUK_HOBJECT_IS_COMPILEDFUNCTION(caller_func)) {
  32956. DUK_DD(DUK_DDPRINT("resume state invalid: caller must be Ecmascript code"));
  32957. goto state_error;
  32958. }
  32959. /* Note: there is no requirement that: 'thr->callstack_preventcount == 1'
  32960. * like for yield.
  32961. */
  32962. if (thr_resume->state != DUK_HTHREAD_STATE_INACTIVE &&
  32963. thr_resume->state != DUK_HTHREAD_STATE_YIELDED) {
  32964. DUK_DD(DUK_DDPRINT("resume state invalid: target thread must be INACTIVE or YIELDED"));
  32965. goto state_error;
  32966. }
  32967. DUK_ASSERT(thr_resume->state == DUK_HTHREAD_STATE_INACTIVE ||
  32968. thr_resume->state == DUK_HTHREAD_STATE_YIELDED);
  32969. /* Further state-dependent pre-checks */
  32970. if (thr_resume->state == DUK_HTHREAD_STATE_YIELDED) {
  32971. /* no pre-checks now, assume a previous yield() has left things in
  32972. * tip-top shape (longjmp handler will assert for these).
  32973. */
  32974. } else {
  32975. DUK_ASSERT(thr_resume->state == DUK_HTHREAD_STATE_INACTIVE);
  32976. if ((thr_resume->callstack_top != 0) ||
  32977. (thr_resume->valstack_top - thr_resume->valstack != 1)) {
  32978. goto state_invalid_initial;
  32979. }
  32980. tv = &thr_resume->valstack_top[-1];
  32981. DUK_ASSERT(tv >= thr_resume->valstack && tv < thr_resume->valstack_top);
  32982. if (!DUK_TVAL_IS_OBJECT(tv)) {
  32983. goto state_invalid_initial;
  32984. }
  32985. func = DUK_TVAL_GET_OBJECT(tv);
  32986. DUK_ASSERT(func != NULL);
  32987. if (!DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) {
  32988. /* Note: cannot be a bound function either right now,
  32989. * this would be easy to relax though.
  32990. */
  32991. goto state_invalid_initial;
  32992. }
  32993. }
  32994. /*
  32995. * The error object has been augmented with a traceback and other
  32996. * info from its creation point -- usually another thread. The
  32997. * error handler is called here right before throwing, but it also
  32998. * runs in the resumer's thread. It might be nice to get a traceback
  32999. * from the resumee but this is not the case now.
  33000. */
  33001. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  33002. if (is_error) {
  33003. DUK_ASSERT_TOP(ctx, 2); /* value (error) is at stack top */
  33004. duk_err_augment_error_throw(thr); /* in resumer's context */
  33005. }
  33006. #endif
  33007. #ifdef DUK_USE_DEBUG
  33008. if (is_error) {
  33009. DUK_DDD(DUK_DDDPRINT("RESUME ERROR: thread=%!T, value=%!T",
  33010. (duk_tval *) duk_get_tval(ctx, 0),
  33011. (duk_tval *) duk_get_tval(ctx, 1)));
  33012. } else if (thr_resume->state == DUK_HTHREAD_STATE_YIELDED) {
  33013. DUK_DDD(DUK_DDDPRINT("RESUME NORMAL: thread=%!T, value=%!T",
  33014. (duk_tval *) duk_get_tval(ctx, 0),
  33015. (duk_tval *) duk_get_tval(ctx, 1)));
  33016. } else {
  33017. DUK_DDD(DUK_DDDPRINT("RESUME INITIAL: thread=%!T, value=%!T",
  33018. (duk_tval *) duk_get_tval(ctx, 0),
  33019. (duk_tval *) duk_get_tval(ctx, 1)));
  33020. }
  33021. #endif
  33022. thr->heap->lj.type = DUK_LJ_TYPE_RESUME;
  33023. /* lj value2: thread */
  33024. DUK_ASSERT(thr->valstack_bottom < thr->valstack_top);
  33025. DUK_TVAL_SET_TVAL_UPDREF(thr, &thr->heap->lj.value2, &thr->valstack_bottom[0]); /* side effects */
  33026. /* lj value1: value */
  33027. DUK_ASSERT(thr->valstack_bottom + 1 < thr->valstack_top);
  33028. DUK_TVAL_SET_TVAL_UPDREF(thr, &thr->heap->lj.value1, &thr->valstack_bottom[1]); /* side effects */
  33029. thr->heap->lj.iserror = is_error;
  33030. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* call is from executor, so we know we have a jmpbuf */
  33031. duk_err_longjmp(thr); /* execution resumes in bytecode executor */
  33032. return 0; /* never here */
  33033. state_invalid_initial:
  33034. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "invalid initial thread state/stack");
  33035. return 0; /* never here */
  33036. state_error:
  33037. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "invalid state for resume");
  33038. return 0; /* never here */
  33039. }
  33040. /*
  33041. * Yield the current thread.
  33042. *
  33043. * The thread must be in yieldable state: it must have a resumer, and there
  33044. * must not be any yield-preventing calls (native calls and constructor calls,
  33045. * currently) in the thread's call stack (otherwise a resume would not be
  33046. * possible later). This method must be called from an Ecmascript function.
  33047. *
  33048. * Args:
  33049. * - value
  33050. * - isError (defaults to false)
  33051. *
  33052. * Note: yield and resume handling is currently asymmetric.
  33053. */
  33054. DUK_INTERNAL duk_ret_t duk_bi_thread_yield(duk_context *ctx) {
  33055. duk_hthread *thr = (duk_hthread *) ctx;
  33056. duk_hobject *caller_func;
  33057. duk_small_int_t is_error;
  33058. DUK_DDD(DUK_DDDPRINT("Duktape.Thread.yield(): value=%!T, is_error=%!T",
  33059. (duk_tval *) duk_get_tval(ctx, 0),
  33060. (duk_tval *) duk_get_tval(ctx, 1)));
  33061. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING);
  33062. DUK_ASSERT(thr->heap->curr_thread == thr);
  33063. is_error = (duk_small_int_t) duk_to_boolean(ctx, 1);
  33064. duk_set_top(ctx, 1);
  33065. /* [ value ] */
  33066. /*
  33067. * Thread state and calling context checks
  33068. */
  33069. if (!thr->resumer) {
  33070. DUK_DD(DUK_DDPRINT("yield state invalid: current thread must have a resumer"));
  33071. goto state_error;
  33072. }
  33073. DUK_ASSERT(thr->resumer->state == DUK_HTHREAD_STATE_RESUMED);
  33074. if (thr->callstack_top < 2) {
  33075. DUK_DD(DUK_DDPRINT("yield state invalid: callstack should contain at least 2 entries (caller and Duktape.Thread.yield)"));
  33076. goto state_error;
  33077. }
  33078. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1) != NULL); /* us */
  33079. DUK_ASSERT(DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1)));
  33080. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2) != NULL); /* caller */
  33081. caller_func = DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2);
  33082. if (!DUK_HOBJECT_IS_COMPILEDFUNCTION(caller_func)) {
  33083. DUK_DD(DUK_DDPRINT("yield state invalid: caller must be Ecmascript code"));
  33084. goto state_error;
  33085. }
  33086. DUK_ASSERT(thr->callstack_preventcount >= 1); /* should never be zero, because we (Duktape.Thread.yield) are on the stack */
  33087. if (thr->callstack_preventcount != 1) {
  33088. /* Note: the only yield-preventing call is Duktape.Thread.yield(), hence check for 1, not 0 */
  33089. DUK_DD(DUK_DDPRINT("yield state invalid: there must be no yield-preventing calls in current thread callstack (preventcount is %ld)",
  33090. (long) thr->callstack_preventcount));
  33091. goto state_error;
  33092. }
  33093. /*
  33094. * The error object has been augmented with a traceback and other
  33095. * info from its creation point -- usually the current thread.
  33096. * The error handler, however, is called right before throwing
  33097. * and runs in the yielder's thread.
  33098. */
  33099. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  33100. if (is_error) {
  33101. DUK_ASSERT_TOP(ctx, 1); /* value (error) is at stack top */
  33102. duk_err_augment_error_throw(thr); /* in yielder's context */
  33103. }
  33104. #endif
  33105. #ifdef DUK_USE_DEBUG
  33106. if (is_error) {
  33107. DUK_DDD(DUK_DDDPRINT("YIELD ERROR: value=%!T",
  33108. (duk_tval *) duk_get_tval(ctx, 0)));
  33109. } else {
  33110. DUK_DDD(DUK_DDDPRINT("YIELD NORMAL: value=%!T",
  33111. (duk_tval *) duk_get_tval(ctx, 0)));
  33112. }
  33113. #endif
  33114. /*
  33115. * Process yield
  33116. *
  33117. * After longjmp(), processing continues in bytecode executor longjmp
  33118. * handler, which will e.g. update thr->resumer to NULL.
  33119. */
  33120. thr->heap->lj.type = DUK_LJ_TYPE_YIELD;
  33121. /* lj value1: value */
  33122. DUK_ASSERT(thr->valstack_bottom < thr->valstack_top);
  33123. DUK_TVAL_SET_TVAL_UPDREF(thr, &thr->heap->lj.value1, &thr->valstack_bottom[0]); /* side effects */
  33124. thr->heap->lj.iserror = is_error;
  33125. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* call is from executor, so we know we have a jmpbuf */
  33126. duk_err_longjmp(thr); /* execution resumes in bytecode executor */
  33127. return 0; /* never here */
  33128. state_error:
  33129. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "invalid state for yield");
  33130. return 0; /* never here */
  33131. }
  33132. DUK_INTERNAL duk_ret_t duk_bi_thread_current(duk_context *ctx) {
  33133. duk_push_current_thread(ctx);
  33134. return 1;
  33135. }
  33136. #line 1 "duk_bi_thrower.c"
  33137. /*
  33138. * Type error thrower, E5 Section 13.2.3.
  33139. */
  33140. /* include removed: duk_internal.h */
  33141. DUK_INTERNAL duk_ret_t duk_bi_type_error_thrower(duk_context *ctx) {
  33142. DUK_UNREF(ctx);
  33143. return DUK_RET_TYPE_ERROR;
  33144. }
  33145. #line 1 "duk_debug_fixedbuffer.c"
  33146. /*
  33147. * Fixed buffer helper useful for debugging, requires no allocation
  33148. * which is critical for debugging.
  33149. */
  33150. /* include removed: duk_internal.h */
  33151. #ifdef DUK_USE_DEBUG
  33152. DUK_INTERNAL void duk_fb_put_bytes(duk_fixedbuffer *fb, duk_uint8_t *buffer, duk_size_t length) {
  33153. duk_size_t avail;
  33154. duk_size_t copylen;
  33155. avail = (fb->offset >= fb->length ? (duk_size_t) 0 : (duk_size_t) (fb->length - fb->offset));
  33156. if (length > avail) {
  33157. copylen = avail;
  33158. fb->truncated = 1;
  33159. } else {
  33160. copylen = length;
  33161. }
  33162. DUK_MEMCPY(fb->buffer + fb->offset, buffer, copylen);
  33163. fb->offset += copylen;
  33164. }
  33165. DUK_INTERNAL void duk_fb_put_byte(duk_fixedbuffer *fb, duk_uint8_t x) {
  33166. duk_fb_put_bytes(fb, &x, 1);
  33167. }
  33168. DUK_INTERNAL void duk_fb_put_cstring(duk_fixedbuffer *fb, const char *x) {
  33169. duk_fb_put_bytes(fb, (duk_uint8_t *) x, (duk_size_t) DUK_STRLEN(x));
  33170. }
  33171. DUK_INTERNAL void duk_fb_sprintf(duk_fixedbuffer *fb, const char *fmt, ...) {
  33172. duk_size_t avail;
  33173. va_list ap;
  33174. va_start(ap, fmt);
  33175. avail = (fb->offset >= fb->length ? (duk_size_t) 0 : (duk_size_t) (fb->length - fb->offset));
  33176. if (avail > 0) {
  33177. duk_int_t res = (duk_int_t) DUK_VSNPRINTF((char *) (fb->buffer + fb->offset), avail, fmt, ap);
  33178. if (res < 0) {
  33179. /* error */
  33180. } else if ((duk_size_t) res >= avail) {
  33181. /* (maybe) truncated */
  33182. fb->offset += avail;
  33183. if ((duk_size_t) res > avail) {
  33184. /* actual chars dropped (not just NUL term) */
  33185. fb->truncated = 1;
  33186. }
  33187. } else {
  33188. /* normal */
  33189. fb->offset += res;
  33190. }
  33191. }
  33192. va_end(ap);
  33193. }
  33194. DUK_INTERNAL void duk_fb_put_funcptr(duk_fixedbuffer *fb, duk_uint8_t *fptr, duk_size_t fptr_size) {
  33195. char buf[64+1];
  33196. duk_debug_format_funcptr(buf, sizeof(buf), fptr, fptr_size);
  33197. buf[sizeof(buf) - 1] = (char) 0;
  33198. duk_fb_put_cstring(fb, buf);
  33199. }
  33200. DUK_INTERNAL duk_bool_t duk_fb_is_full(duk_fixedbuffer *fb) {
  33201. return (fb->offset >= fb->length);
  33202. }
  33203. #endif /* DUK_USE_DEBUG */
  33204. #line 1 "duk_debug_heap.c"
  33205. /*
  33206. * Debug dumping of duk_heap.
  33207. */
  33208. /* include removed: duk_internal.h */
  33209. #ifdef DUK_USE_DEBUG
  33210. #if 0 /*unused*/
  33211. DUK_LOCAL void duk__sanitize_snippet(char *buf, duk_size_t buf_size, duk_hstring *str) {
  33212. duk_size_t i;
  33213. duk_size_t nchars;
  33214. duk_size_t maxchars;
  33215. duk_uint8_t *data;
  33216. DUK_MEMZERO(buf, buf_size);
  33217. maxchars = (duk_size_t) (buf_size - 1);
  33218. data = DUK_HSTRING_GET_DATA(str);
  33219. nchars = ((duk_size_t) str->blen < maxchars ? (duk_size_t) str->blen : maxchars);
  33220. for (i = 0; i < nchars; i++) {
  33221. duk_small_int_t c = (duk_small_int_t) data[i];
  33222. if (c < 0x20 || c > 0x7e) {
  33223. c = '.';
  33224. }
  33225. buf[i] = (char) c;
  33226. }
  33227. }
  33228. #endif
  33229. #if 0
  33230. DUK_LOCAL const char *duk__get_heap_type_string(duk_heaphdr *hdr) {
  33231. switch (DUK_HEAPHDR_GET_TYPE(hdr)) {
  33232. case DUK_HTYPE_STRING:
  33233. return "string";
  33234. case DUK_HTYPE_OBJECT:
  33235. return "object";
  33236. case DUK_HTYPE_BUFFER:
  33237. return "buffer";
  33238. default:
  33239. return "???";
  33240. }
  33241. }
  33242. #endif
  33243. #if 0
  33244. DUK_LOCAL void duk__dump_indented(duk_heaphdr *obj, int index) {
  33245. DUK_UNREF(obj);
  33246. DUK_UNREF(index);
  33247. DUK_UNREF(duk__get_heap_type_string);
  33248. #ifdef DUK_USE_REFERENCE_COUNTING
  33249. DUK_D(DUK_DPRINT(" [%ld]: %p %s (flags: 0x%08lx, ref: %ld) -> %!O",
  33250. (long) index,
  33251. (void *) obj,
  33252. (const char *) duk__get_heap_type_string(obj),
  33253. (unsigned long) DUK_HEAPHDR_GET_FLAGS(obj),
  33254. (long) DUK_HEAPHDR_GET_REFCOUNT(obj),
  33255. (duk_heaphdr *) obj));
  33256. #else
  33257. DUK_D(DUK_DPRINT(" [%ld]: %p %s (flags: 0x%08lx) -> %!O",
  33258. (long) index,
  33259. (void *) obj,
  33260. (const char *) duk__get_heap_type_string(obj),
  33261. (unsigned long) DUK_HEAPHDR_GET_FLAGS(obj),
  33262. (duk_heaphdr *) obj));
  33263. #endif
  33264. }
  33265. #endif
  33266. #if 0 /*unused*/
  33267. DUK_LOCAL void duk__dump_heaphdr_list(duk_heap *heap, duk_heaphdr *root, const char *name) {
  33268. duk_int_t count;
  33269. duk_heaphdr *curr;
  33270. DUK_UNREF(heap);
  33271. DUK_UNREF(name);
  33272. count = 0;
  33273. curr = root;
  33274. while (curr) {
  33275. count++;
  33276. curr = DUK_HEAPHDR_GET_NEXT(curr);
  33277. }
  33278. DUK_D(DUK_DPRINT("%s, %ld objects", (const char *) name, (long) count));
  33279. count = 0;
  33280. curr = root;
  33281. while (curr) {
  33282. count++;
  33283. duk__dump_indented(curr, count);
  33284. curr = DUK_HEAPHDR_GET_NEXT(curr);
  33285. }
  33286. }
  33287. #endif
  33288. #if 0 /*unused*/
  33289. DUK_LOCAL void duk__dump_stringtable(duk_heap *heap) {
  33290. duk_uint_fast32_t i;
  33291. char buf[64+1];
  33292. DUK_D(DUK_DPRINT("stringtable %p, used %ld, size %ld, load %ld%%",
  33293. (void *) heap->strtable,
  33294. (long) heap->st_used,
  33295. (long) heap->st_size,
  33296. (long) (((double) heap->st_used) / ((double) heap->st_size) * 100.0)));
  33297. for (i = 0; i < (duk_uint_fast32_t) heap->st_size; i++) {
  33298. duk_hstring *e = heap->strtable[i];
  33299. if (!e) {
  33300. DUK_D(DUK_DPRINT(" [%ld]: NULL", (long) i));
  33301. } else if (e == DUK_STRTAB_DELETED_MARKER(heap)) {
  33302. DUK_D(DUK_DPRINT(" [%ld]: DELETED", (long) i));
  33303. } else {
  33304. duk__sanitize_snippet(buf, sizeof(buf), e);
  33305. #ifdef DUK_USE_REFERENCE_COUNTING
  33306. DUK_D(DUK_DPRINT(" [%ld]: %p (flags: 0x%08lx, ref: %ld) '%s', strhash=0x%08lx, blen=%ld, clen=%ld, "
  33307. "arridx=%ld, internal=%ld, reserved_word=%ld, strict_reserved_word=%ld, eval_or_arguments=%ld",
  33308. (long) i,
  33309. (void *) e,
  33310. (unsigned long) DUK_HEAPHDR_GET_FLAGS((duk_heaphdr *) e),
  33311. (long) DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) e),
  33312. (const char *) buf,
  33313. (unsigned long) e->hash,
  33314. (long) e->blen,
  33315. (long) e->clen,
  33316. (long) (DUK_HSTRING_HAS_ARRIDX(e) ? 1 : 0),
  33317. (long) (DUK_HSTRING_HAS_INTERNAL(e) ? 1 : 0),
  33318. (long) (DUK_HSTRING_HAS_RESERVED_WORD(e) ? 1 : 0),
  33319. (long) (DUK_HSTRING_HAS_STRICT_RESERVED_WORD(e) ? 1 : 0),
  33320. (long) (DUK_HSTRING_HAS_EVAL_OR_ARGUMENTS(e) ? 1 : 0)));
  33321. #else
  33322. DUK_D(DUK_DPRINT(" [%ld]: %p (flags: 0x%08lx) '%s', strhash=0x%08lx, blen=%ld, clen=%ld, "
  33323. "arridx=%ld, internal=%ld, reserved_word=%ld, strict_reserved_word=%ld, eval_or_arguments=%ld",
  33324. (long) i,
  33325. (void *) e,
  33326. (unsigned long) DUK_HEAPHDR_GET_FLAGS((duk_heaphdr *) e),
  33327. (const char *) buf,
  33328. (long) e->hash,
  33329. (long) e->blen,
  33330. (long) e->clen,
  33331. (long) (DUK_HSTRING_HAS_ARRIDX(e) ? 1 : 0),
  33332. (long) (DUK_HSTRING_HAS_INTERNAL(e) ? 1 : 0),
  33333. (long) (DUK_HSTRING_HAS_RESERVED_WORD(e) ? 1 : 0),
  33334. (long) (DUK_HSTRING_HAS_STRICT_RESERVED_WORD(e) ? 1 : 0),
  33335. (long) (DUK_HSTRING_HAS_EVAL_OR_ARGUMENTS(e) ? 1 : 0)));
  33336. #endif
  33337. }
  33338. }
  33339. }
  33340. #endif
  33341. #if 0 /*unused*/
  33342. DUK_LOCAL void duk__dump_strcache(duk_heap *heap) {
  33343. duk_uint_fast32_t i;
  33344. char buf[64+1];
  33345. DUK_D(DUK_DPRINT("stringcache"));
  33346. for (i = 0; i < (duk_uint_fast32_t) DUK_HEAP_STRCACHE_SIZE; i++) {
  33347. duk_strcache *c = &heap->strcache[i];
  33348. if (!c->h) {
  33349. DUK_D(DUK_DPRINT(" [%ld]: bidx=%ld, cidx=%ld, str=NULL",
  33350. (long) i, (long) c->bidx, (long) c->cidx));
  33351. } else {
  33352. duk__sanitize_snippet(buf, sizeof(buf), c->h);
  33353. DUK_D(DUK_DPRINT(" [%ld]: bidx=%ld cidx=%ld str=%s",
  33354. (long) i, (long) c->bidx, (long) c->cidx, (const char *) buf));
  33355. }
  33356. }
  33357. }
  33358. #endif
  33359. #if 0 /*unused*/
  33360. DUK_INTERNAL void duk_debug_dump_heap(duk_heap *heap) {
  33361. char buf[64+1];
  33362. DUK_D(DUK_DPRINT("=== heap %p ===", (void *) heap));
  33363. DUK_D(DUK_DPRINT(" flags: 0x%08lx", (unsigned long) heap->flags));
  33364. /* Note: there is no standard formatter for function pointers */
  33365. #ifdef DUK_USE_GCC_PRAGMAS
  33366. #pragma GCC diagnostic push
  33367. #pragma GCC diagnostic ignored "-pedantic"
  33368. #endif
  33369. duk_debug_format_funcptr(buf, sizeof(buf), (duk_uint8_t *) &heap->alloc_func, sizeof(heap->alloc_func));
  33370. DUK_D(DUK_DPRINT(" alloc_func: %s", (const char *) buf));
  33371. duk_debug_format_funcptr(buf, sizeof(buf), (duk_uint8_t *) &heap->realloc_func, sizeof(heap->realloc_func));
  33372. DUK_D(DUK_DPRINT(" realloc_func: %s", (const char *) buf));
  33373. duk_debug_format_funcptr(buf, sizeof(buf), (duk_uint8_t *) &heap->free_func, sizeof(heap->free_func));
  33374. DUK_D(DUK_DPRINT(" free_func: %s", (const char *) buf));
  33375. duk_debug_format_funcptr(buf, sizeof(buf), (duk_uint8_t *) &heap->fatal_func, sizeof(heap->fatal_func));
  33376. DUK_D(DUK_DPRINT(" fatal_func: %s", (const char *) buf));
  33377. #ifdef DUK_USE_GCC_PRAGMAS
  33378. #pragma GCC diagnostic pop
  33379. #endif
  33380. DUK_D(DUK_DPRINT(" heap_udata: %p", (void *) heap->heap_udata));
  33381. #ifdef DUK_USE_MARK_AND_SWEEP
  33382. #ifdef DUK_USE_VOLUNTARY_GC
  33383. DUK_D(DUK_DPRINT(" mark-and-sweep trig counter: %ld", (long) heap->mark_and_sweep_trigger_counter));
  33384. #endif
  33385. DUK_D(DUK_DPRINT(" mark-and-sweep rec depth: %ld", (long) heap->mark_and_sweep_recursion_depth));
  33386. DUK_D(DUK_DPRINT(" mark-and-sweep base flags: 0x%08lx", (unsigned long) heap->mark_and_sweep_base_flags));
  33387. #endif
  33388. DUK_D(DUK_DPRINT(" lj.jmpbuf_ptr: %p", (void *) heap->lj.jmpbuf_ptr));
  33389. DUK_D(DUK_DPRINT(" lj.type: %ld", (long) heap->lj.type));
  33390. DUK_D(DUK_DPRINT(" lj.value1: %!T", (duk_tval *) &heap->lj.value1));
  33391. DUK_D(DUK_DPRINT(" lj.value2: %!T", (duk_tval *) &heap->lj.value2));
  33392. DUK_D(DUK_DPRINT(" lj.iserror: %ld", (long) heap->lj.iserror));
  33393. DUK_D(DUK_DPRINT(" handling_error: %ld", (long) heap->handling_error));
  33394. DUK_D(DUK_DPRINT(" heap_thread: %!@O", (duk_heaphdr *) heap->heap_thread));
  33395. DUK_D(DUK_DPRINT(" curr_thread: %!@O", (duk_heaphdr *) heap->curr_thread));
  33396. DUK_D(DUK_DPRINT(" heap_object: %!@O", (duk_heaphdr *) heap->heap_object));
  33397. DUK_D(DUK_DPRINT(" call_recursion_depth: %ld", (long) heap->call_recursion_depth));
  33398. DUK_D(DUK_DPRINT(" call_recursion_limit: %ld", (long) heap->call_recursion_limit));
  33399. DUK_D(DUK_DPRINT(" hash_seed: 0x%08lx", (unsigned long) heap->hash_seed));
  33400. DUK_D(DUK_DPRINT(" rnd_state: 0x%08lx", (unsigned long) heap->rnd_state));
  33401. duk__dump_strcache(heap);
  33402. duk__dump_heaphdr_list(heap, heap->heap_allocated, "heap allocated");
  33403. #ifdef DUK_USE_REFERENCE_COUNTING
  33404. duk__dump_heaphdr_list(heap, heap->refzero_list, "refcounting refzero list");
  33405. #endif
  33406. #ifdef DUK_USE_MARK_AND_SWEEP
  33407. duk__dump_heaphdr_list(heap, heap->finalize_list, "mark-and-sweep finalize list");
  33408. #endif
  33409. duk__dump_stringtable(heap);
  33410. /* heap->strs: not worth dumping */
  33411. }
  33412. #endif
  33413. #endif /* DUK_USE_DEBUG */
  33414. #line 1 "duk_debug_vsnprintf.c"
  33415. /*
  33416. * Custom formatter for debug printing, allowing Duktape specific data
  33417. * structures (such as tagged values and heap objects) to be printed with
  33418. * a nice format string. Because debug printing should not affect execution
  33419. * state, formatting here must be independent of execution (see implications
  33420. * below) and must not allocate memory.
  33421. *
  33422. * Custom format tags begin with a '%!' to safely distinguish them from
  33423. * standard format tags. The following conversions are supported:
  33424. *
  33425. * %!T tagged value (duk_tval *)
  33426. * %!O heap object (duk_heaphdr *)
  33427. * %!I decoded bytecode instruction
  33428. * %!C bytecode instruction opcode name (arg is long)
  33429. *
  33430. * Everything is serialized in a JSON-like manner. The default depth is one
  33431. * level, internal prototype is not followed, and internal properties are not
  33432. * serialized. The following modifiers change this behavior:
  33433. *
  33434. * @ print pointers
  33435. * # print binary representations (where applicable)
  33436. * d deep traversal of own properties (not prototype)
  33437. * p follow prototype chain (useless without 'd')
  33438. * i include internal properties (other than prototype)
  33439. * x hexdump buffers
  33440. * h heavy formatting
  33441. *
  33442. * For instance, the following serializes objects recursively, but does not
  33443. * follow the prototype chain nor print internal properties: "%!dO".
  33444. *
  33445. * Notes:
  33446. *
  33447. * * Standard snprintf return value semantics seem to vary. This
  33448. * implementation returns the number of bytes it actually wrote
  33449. * (excluding the null terminator). If retval == buffer size,
  33450. * output was truncated (except for corner cases).
  33451. *
  33452. * * Output format is intentionally different from Ecmascript
  33453. * formatting requirements, as formatting here serves debugging
  33454. * of internals.
  33455. *
  33456. * * Depth checking (and updating) is done in each type printer
  33457. * separately, to allow them to call each other freely.
  33458. *
  33459. * * Some pathological structures might take ages to print (e.g.
  33460. * self recursion with 100 properties pointing to the object
  33461. * itself). To guard against these, each printer also checks
  33462. * whether the output buffer is full; if so, early exit.
  33463. *
  33464. * * Reference loops are detected using a loop stack.
  33465. */
  33466. /* include removed: duk_internal.h */
  33467. #ifdef DUK_USE_DEBUG
  33468. #include <stdio.h>
  33469. #include <stdarg.h>
  33470. #include <string.h>
  33471. /* list of conversion specifiers that terminate a format tag;
  33472. * this is unfortunately guesswork.
  33473. */
  33474. #define DUK__ALLOWED_STANDARD_SPECIFIERS "diouxXeEfFgGaAcsCSpnm"
  33475. /* maximum length of standard format tag that we support */
  33476. #define DUK__MAX_FORMAT_TAG_LENGTH 32
  33477. /* heapobj recursion depth when deep printing is selected */
  33478. #define DUK__DEEP_DEPTH_LIMIT 8
  33479. /* maximum recursion depth for loop detection stacks */
  33480. #define DUK__LOOP_STACK_DEPTH 256
  33481. /* must match bytecode defines now; build autogenerate? */
  33482. DUK_LOCAL const char *duk__bc_optab[64] = {
  33483. "LDREG", "STREG", "LDCONST", "LDINT", "LDINTX", "MPUTOBJ", "MPUTOBJI", "MPUTARR", "MPUTARRI", "NEW",
  33484. "NEWI", "REGEXP", "CSREG", "CSREGI", "GETVAR", "PUTVAR", "DECLVAR", "DELVAR", "CSVAR", "CSVARI",
  33485. "CLOSURE", "GETPROP", "PUTPROP", "DELPROP", "CSPROP", "CSPROPI", "ADD", "SUB", "MUL", "DIV",
  33486. "MOD", "BAND", "BOR", "BXOR", "BASL", "BLSR", "BASR", "EQ", "NEQ", "SEQ",
  33487. "SNEQ", "GT", "GE", "LT", "LE", "IF", "JUMP", "RETURN", "CALL", "CALLI",
  33488. "TRYCATCH", "EXTRA", "PREINCR", "PREDECR", "POSTINCR", "POSTDECR", "PREINCV", "PREDECV", "POSTINCV", "POSTDECV",
  33489. "PREINCP", "PREDECP", "POSTINCP", "POSTDECP"
  33490. };
  33491. DUK_LOCAL const char *duk__bc_extraoptab[256] = {
  33492. "NOP", "INVALID", "LDTHIS", "LDUNDEF", "LDNULL", "LDTRUE", "LDFALSE", "NEWOBJ", "NEWARR", "SETALEN",
  33493. "TYPEOF", "TYPEOFID", "INITENUM", "NEXTENUM", "INITSET", "INITSETI", "INITGET", "INITGETI", "ENDTRY", "ENDCATCH",
  33494. "ENDFIN", "THROW", "INVLHS", "UNM", "UNP", "DEBUGGER", "BREAK", "CONTINUE", "BNOT", "LNOT",
  33495. "INSTOF", "IN", "LABEL", "ENDLABEL", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33496. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33497. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33498. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33499. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33500. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33501. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33502. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33503. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33504. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33505. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33506. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33507. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33508. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33509. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33510. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33511. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33512. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33513. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33514. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33515. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33516. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  33517. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX"
  33518. };
  33519. typedef struct duk__dprint_state duk__dprint_state;
  33520. struct duk__dprint_state {
  33521. duk_fixedbuffer *fb;
  33522. /* loop_stack_index could be perhaps be replaced by 'depth', but it's nice
  33523. * to not couple these two mechanisms unnecessarily.
  33524. */
  33525. duk_hobject *loop_stack[DUK__LOOP_STACK_DEPTH];
  33526. duk_int_t loop_stack_index;
  33527. duk_int_t loop_stack_limit;
  33528. duk_int_t depth;
  33529. duk_int_t depth_limit;
  33530. duk_bool_t pointer;
  33531. duk_bool_t heavy;
  33532. duk_bool_t binary;
  33533. duk_bool_t follow_proto;
  33534. duk_bool_t internal;
  33535. duk_bool_t hexdump;
  33536. };
  33537. /* helpers */
  33538. DUK_LOCAL_DECL void duk__print_hstring(duk__dprint_state *st, duk_hstring *k, duk_bool_t quotes);
  33539. DUK_LOCAL_DECL void duk__print_hobject(duk__dprint_state *st, duk_hobject *h);
  33540. DUK_LOCAL_DECL void duk__print_hbuffer(duk__dprint_state *st, duk_hbuffer *h);
  33541. DUK_LOCAL_DECL void duk__print_tval(duk__dprint_state *st, duk_tval *tv);
  33542. DUK_LOCAL_DECL void duk__print_instr(duk__dprint_state *st, duk_instr_t ins);
  33543. DUK_LOCAL_DECL void duk__print_heaphdr(duk__dprint_state *st, duk_heaphdr *h);
  33544. DUK_LOCAL_DECL void duk__print_shared_heaphdr(duk__dprint_state *st, duk_heaphdr *h);
  33545. DUK_LOCAL_DECL void duk__print_shared_heaphdr_string(duk__dprint_state *st, duk_heaphdr_string *h);
  33546. DUK_LOCAL void duk__print_shared_heaphdr(duk__dprint_state *st, duk_heaphdr *h) {
  33547. duk_fixedbuffer *fb = st->fb;
  33548. if (st->heavy) {
  33549. duk_fb_sprintf(fb, "(%p)", (void *) h);
  33550. }
  33551. if (!h) {
  33552. return;
  33553. }
  33554. if (st->binary) {
  33555. duk_size_t i;
  33556. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_LBRACKET);
  33557. for (i = 0; i < (duk_size_t) sizeof(*h); i++) {
  33558. duk_fb_sprintf(fb, "%02lx", (unsigned long) ((duk_uint8_t *)h)[i]);
  33559. }
  33560. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_RBRACKET);
  33561. }
  33562. #ifdef DUK_USE_REFERENCE_COUNTING /* currently implicitly also DUK_USE_DOUBLE_LINKED_HEAP */
  33563. if (st->heavy) {
  33564. duk_fb_sprintf(fb, "[h_next=%p,h_prev=%p,h_refcount=%lu,h_flags=%08lx,type=%ld,"
  33565. "reachable=%ld,temproot=%ld,finalizable=%ld,finalized=%ld]",
  33566. (void *) DUK_HEAPHDR_GET_NEXT(NULL, h),
  33567. (void *) DUK_HEAPHDR_GET_PREV(NULL, h),
  33568. (unsigned long) DUK_HEAPHDR_GET_REFCOUNT(h),
  33569. (unsigned long) DUK_HEAPHDR_GET_FLAGS(h),
  33570. (long) DUK_HEAPHDR_GET_TYPE(h),
  33571. (long) (DUK_HEAPHDR_HAS_REACHABLE(h) ? 1 : 0),
  33572. (long) (DUK_HEAPHDR_HAS_TEMPROOT(h) ? 1 : 0),
  33573. (long) (DUK_HEAPHDR_HAS_FINALIZABLE(h) ? 1 : 0),
  33574. (long) (DUK_HEAPHDR_HAS_FINALIZED(h) ? 1 : 0));
  33575. }
  33576. #else
  33577. if (st->heavy) {
  33578. duk_fb_sprintf(fb, "[h_next=%p,h_flags=%08lx,type=%ld,reachable=%ld,temproot=%ld,finalizable=%ld,finalized=%ld]",
  33579. (void *) DUK_HEAPHDR_GET_NEXT(NULL, h),
  33580. (unsigned long) DUK_HEAPHDR_GET_FLAGS(h),
  33581. (long) DUK_HEAPHDR_GET_TYPE(h),
  33582. (long) (DUK_HEAPHDR_HAS_REACHABLE(h) ? 1 : 0),
  33583. (long) (DUK_HEAPHDR_HAS_TEMPROOT(h) ? 1 : 0),
  33584. (long) (DUK_HEAPHDR_HAS_FINALIZABLE(h) ? 1 : 0),
  33585. (long) (DUK_HEAPHDR_HAS_FINALIZED(h) ? 1 : 0));
  33586. }
  33587. #endif
  33588. }
  33589. DUK_LOCAL void duk__print_shared_heaphdr_string(duk__dprint_state *st, duk_heaphdr_string *h) {
  33590. duk_fixedbuffer *fb = st->fb;
  33591. if (st->heavy) {
  33592. duk_fb_sprintf(fb, "(%p)", (void *) h);
  33593. }
  33594. if (!h) {
  33595. return;
  33596. }
  33597. if (st->binary) {
  33598. duk_size_t i;
  33599. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_LBRACKET);
  33600. for (i = 0; i < (duk_size_t) sizeof(*h); i++) {
  33601. duk_fb_sprintf(fb, "%02lx", (unsigned long) ((duk_uint8_t *)h)[i]);
  33602. }
  33603. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_RBRACKET);
  33604. }
  33605. #ifdef DUK_USE_REFERENCE_COUNTING
  33606. if (st->heavy) {
  33607. duk_fb_sprintf(fb, "[h_refcount=%lu,h_flags=%08lx,type=%ld,reachable=%ld,temproot=%ld,finalizable=%ld,finalized=%ld]",
  33608. (unsigned long) DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h),
  33609. (unsigned long) DUK_HEAPHDR_GET_FLAGS((duk_heaphdr *) h),
  33610. (long) DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) h),
  33611. (long) (DUK_HEAPHDR_HAS_REACHABLE((duk_heaphdr *) h) ? 1 : 0),
  33612. (long) (DUK_HEAPHDR_HAS_TEMPROOT((duk_heaphdr *) h) ? 1 : 0),
  33613. (long) (DUK_HEAPHDR_HAS_FINALIZABLE((duk_heaphdr *) h) ? 1 : 0),
  33614. (long) (DUK_HEAPHDR_HAS_FINALIZED((duk_heaphdr *) h) ? 1 : 0));
  33615. }
  33616. #else
  33617. if (st->heavy) {
  33618. duk_fb_sprintf(fb, "[h_flags=%08lx,type=%ld,reachable=%ld,temproot=%ld,finalizable=%ld,finalized=%ld]",
  33619. (unsigned long) DUK_HEAPHDR_GET_FLAGS((duk_heaphdr *) h),
  33620. (long) DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) h),
  33621. (long) (DUK_HEAPHDR_HAS_REACHABLE((duk_heaphdr *) h) ? 1 : 0),
  33622. (long) (DUK_HEAPHDR_HAS_TEMPROOT((duk_heaphdr *) h) ? 1 : 0),
  33623. (long) (DUK_HEAPHDR_HAS_FINALIZABLE((duk_heaphdr *) h) ? 1 : 0),
  33624. (long) (DUK_HEAPHDR_HAS_FINALIZED((duk_heaphdr *) h) ? 1 : 0));
  33625. }
  33626. #endif
  33627. }
  33628. DUK_LOCAL void duk__print_hstring(duk__dprint_state *st, duk_hstring *h, duk_bool_t quotes) {
  33629. duk_fixedbuffer *fb = st->fb;
  33630. const duk_uint8_t *p;
  33631. const duk_uint8_t *p_end;
  33632. /* terminal type: no depth check */
  33633. if (duk_fb_is_full(fb)) {
  33634. return;
  33635. }
  33636. duk__print_shared_heaphdr_string(st, &h->hdr);
  33637. if (!h) {
  33638. duk_fb_put_cstring(fb, "NULL");
  33639. return;
  33640. }
  33641. p = DUK_HSTRING_GET_DATA(h);
  33642. p_end = p + DUK_HSTRING_GET_BYTELEN(h);
  33643. if (p_end > p && p[0] == DUK_ASC_UNDERSCORE) {
  33644. /* if property key begins with underscore, encode it with
  33645. * forced quotes (e.g. "_Foo") to distinguish it from encoded
  33646. * internal properties (e.g. \xffBar -> _Bar).
  33647. */
  33648. quotes = 1;
  33649. }
  33650. if (quotes) {
  33651. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_DOUBLEQUOTE);
  33652. }
  33653. while (p < p_end) {
  33654. duk_uint8_t ch = *p++;
  33655. /* two special escapes: '\' and '"', other printables as is */
  33656. if (ch == '\\') {
  33657. duk_fb_sprintf(fb, "\\\\");
  33658. } else if (ch == '"') {
  33659. duk_fb_sprintf(fb, "\\\"");
  33660. } else if (ch >= 0x20 && ch <= 0x7e) {
  33661. duk_fb_put_byte(fb, ch);
  33662. } else if (ch == 0xff && !quotes) {
  33663. /* encode \xffBar as _Bar if no quotes are applied, this is for
  33664. * readable internal keys.
  33665. */
  33666. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_UNDERSCORE);
  33667. } else {
  33668. duk_fb_sprintf(fb, "\\x%02lx", (unsigned long) ch);
  33669. }
  33670. }
  33671. if (quotes) {
  33672. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_DOUBLEQUOTE);
  33673. }
  33674. #ifdef DUK_USE_REFERENCE_COUNTING
  33675. /* XXX: limit to quoted strings only, to save keys from being cluttered? */
  33676. duk_fb_sprintf(fb, "/%lu", (unsigned long) DUK_HEAPHDR_GET_REFCOUNT(&h->hdr));
  33677. #endif
  33678. }
  33679. #ifdef DUK__COMMA
  33680. #undef DUK__COMMA
  33681. #endif
  33682. #define DUK__COMMA() do { \
  33683. if (first) { \
  33684. first = 0; \
  33685. } else { \
  33686. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_COMMA); \
  33687. } \
  33688. } while (0)
  33689. DUK_LOCAL void duk__print_hobject(duk__dprint_state *st, duk_hobject *h) {
  33690. duk_fixedbuffer *fb = st->fb;
  33691. duk_uint_fast32_t i;
  33692. duk_tval *tv;
  33693. duk_hstring *key;
  33694. duk_bool_t first = 1;
  33695. const char *brace1 = "{";
  33696. const char *brace2 = "}";
  33697. duk_bool_t pushed_loopstack = 0;
  33698. if (duk_fb_is_full(fb)) {
  33699. return;
  33700. }
  33701. duk__print_shared_heaphdr(st, &h->hdr);
  33702. if (h && DUK_HOBJECT_HAS_ARRAY_PART(h)) {
  33703. brace1 = "[";
  33704. brace2 = "]";
  33705. }
  33706. if (!h) {
  33707. duk_fb_put_cstring(fb, "NULL");
  33708. goto finished;
  33709. }
  33710. if (st->depth >= st->depth_limit) {
  33711. if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h)) {
  33712. duk_fb_sprintf(fb, "%sobject/compiledfunction %p%s", (const char *) brace1, (void *) h, (const char *) brace2);
  33713. } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
  33714. duk_fb_sprintf(fb, "%sobject/nativefunction %p%s", (const char *) brace1, (void *) h, (const char *) brace2);
  33715. } else if (DUK_HOBJECT_IS_THREAD(h)) {
  33716. duk_fb_sprintf(fb, "%sobject/thread %p%s", (const char *) brace1, (void *) h, (const char *) brace2);
  33717. } else {
  33718. duk_fb_sprintf(fb, "%sobject %p%s", (const char *) brace1, (void *) h, (const char *) brace2); /* may be NULL */
  33719. }
  33720. return;
  33721. }
  33722. for (i = 0; i < (duk_uint_fast32_t) st->loop_stack_index; i++) {
  33723. if (st->loop_stack[i] == h) {
  33724. duk_fb_sprintf(fb, "%sLOOP:%p%s", (const char *) brace1, (void *) h, (const char *) brace2);
  33725. return;
  33726. }
  33727. }
  33728. /* after this, return paths should 'goto finished' for decrement */
  33729. st->depth++;
  33730. if (st->loop_stack_index >= st->loop_stack_limit) {
  33731. duk_fb_sprintf(fb, "%sOUT-OF-LOOP-STACK%s", (const char *) brace1, (const char *) brace2);
  33732. goto finished;
  33733. }
  33734. st->loop_stack[st->loop_stack_index++] = h;
  33735. pushed_loopstack = 1;
  33736. /*
  33737. * Notation: double underscore used for internal properties which are not
  33738. * stored in the property allocation (e.g. '__valstack').
  33739. */
  33740. duk_fb_put_cstring(fb, brace1);
  33741. if (DUK_HOBJECT_GET_PROPS(NULL, h)) {
  33742. duk_uint32_t a_limit;
  33743. a_limit = DUK_HOBJECT_GET_ASIZE(h);
  33744. if (st->internal) {
  33745. /* dump all allocated entries, unused entries print as 'unused',
  33746. * note that these may extend beyond current 'length' and look
  33747. * a bit funny.
  33748. */
  33749. } else {
  33750. /* leave out trailing 'unused' elements */
  33751. while (a_limit > 0) {
  33752. tv = DUK_HOBJECT_A_GET_VALUE_PTR(NULL, h, a_limit - 1);
  33753. if (!DUK_TVAL_IS_UNUSED(tv)) {
  33754. break;
  33755. }
  33756. a_limit--;
  33757. }
  33758. }
  33759. for (i = 0; i < a_limit; i++) {
  33760. tv = DUK_HOBJECT_A_GET_VALUE_PTR(NULL, h, i);
  33761. DUK__COMMA();
  33762. duk__print_tval(st, tv);
  33763. }
  33764. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(h); i++) {
  33765. key = DUK_HOBJECT_E_GET_KEY(NULL, h, i);
  33766. if (!key) {
  33767. continue;
  33768. }
  33769. if (!st->internal &&
  33770. DUK_HSTRING_GET_BYTELEN(key) > 0 &&
  33771. DUK_HSTRING_GET_DATA(key)[0] == 0xff) {
  33772. /* XXX: use DUK_HSTRING_FLAG_INTERNAL? */
  33773. continue;
  33774. }
  33775. DUK__COMMA();
  33776. duk__print_hstring(st, key, 0);
  33777. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_COLON);
  33778. if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(NULL, h, i)) {
  33779. duk_fb_sprintf(fb, "[get:%p,set:%p]",
  33780. (void *) DUK_HOBJECT_E_GET_VALUE(NULL, h, i).a.get,
  33781. (void *) DUK_HOBJECT_E_GET_VALUE(NULL, h, i).a.set);
  33782. } else {
  33783. tv = &DUK_HOBJECT_E_GET_VALUE(NULL, h, i).v;
  33784. duk__print_tval(st, tv);
  33785. }
  33786. if (st->heavy) {
  33787. duk_fb_sprintf(fb, "<%02lx>", (unsigned long) DUK_HOBJECT_E_GET_FLAGS(NULL, h, i));
  33788. }
  33789. }
  33790. }
  33791. if (st->internal) {
  33792. if (DUK_HOBJECT_HAS_EXTENSIBLE(h)) {
  33793. DUK__COMMA(); duk_fb_sprintf(fb, "__extensible:true");
  33794. } else {
  33795. ;
  33796. }
  33797. if (DUK_HOBJECT_HAS_CONSTRUCTABLE(h)) {
  33798. DUK__COMMA(); duk_fb_sprintf(fb, "__constructable:true");
  33799. } else {
  33800. ;
  33801. }
  33802. if (DUK_HOBJECT_HAS_BOUND(h)) {
  33803. DUK__COMMA(); duk_fb_sprintf(fb, "__bound:true");
  33804. } else {
  33805. ;
  33806. }
  33807. if (DUK_HOBJECT_HAS_COMPILEDFUNCTION(h)) {
  33808. DUK__COMMA(); duk_fb_sprintf(fb, "__compiledfunction:true");
  33809. } else {
  33810. ;
  33811. }
  33812. if (DUK_HOBJECT_HAS_NATIVEFUNCTION(h)) {
  33813. DUK__COMMA(); duk_fb_sprintf(fb, "__nativefunction:true");
  33814. } else {
  33815. ;
  33816. }
  33817. if (DUK_HOBJECT_HAS_THREAD(h)) {
  33818. DUK__COMMA(); duk_fb_sprintf(fb, "__thread:true");
  33819. } else {
  33820. ;
  33821. }
  33822. if (DUK_HOBJECT_HAS_ARRAY_PART(h)) {
  33823. DUK__COMMA(); duk_fb_sprintf(fb, "__array_part:true");
  33824. } else {
  33825. ;
  33826. }
  33827. if (DUK_HOBJECT_HAS_STRICT(h)) {
  33828. DUK__COMMA(); duk_fb_sprintf(fb, "__strict:true");
  33829. } else {
  33830. ;
  33831. }
  33832. if (DUK_HOBJECT_HAS_NEWENV(h)) {
  33833. DUK__COMMA(); duk_fb_sprintf(fb, "__newenv:true");
  33834. } else {
  33835. ;
  33836. }
  33837. if (DUK_HOBJECT_HAS_NAMEBINDING(h)) {
  33838. DUK__COMMA(); duk_fb_sprintf(fb, "__namebinding:true");
  33839. } else {
  33840. ;
  33841. }
  33842. if (DUK_HOBJECT_HAS_CREATEARGS(h)) {
  33843. DUK__COMMA(); duk_fb_sprintf(fb, "__createargs:true");
  33844. } else {
  33845. ;
  33846. }
  33847. if (DUK_HOBJECT_HAS_ENVRECCLOSED(h)) {
  33848. DUK__COMMA(); duk_fb_sprintf(fb, "__envrecclosed:true");
  33849. } else {
  33850. ;
  33851. }
  33852. if (DUK_HOBJECT_HAS_EXOTIC_ARRAY(h)) {
  33853. DUK__COMMA(); duk_fb_sprintf(fb, "__special_array:true");
  33854. } else {
  33855. ;
  33856. }
  33857. if (DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(h)) {
  33858. DUK__COMMA(); duk_fb_sprintf(fb, "__special_stringobj:true");
  33859. } else {
  33860. ;
  33861. }
  33862. if (DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(h)) {
  33863. DUK__COMMA(); duk_fb_sprintf(fb, "__special_arguments:true");
  33864. } else {
  33865. ;
  33866. }
  33867. if (DUK_HOBJECT_HAS_EXOTIC_DUKFUNC(h)) {
  33868. DUK__COMMA(); duk_fb_sprintf(fb, "__special_dukfunc:true");
  33869. } else {
  33870. ;
  33871. }
  33872. if (DUK_HOBJECT_IS_BUFFEROBJECT(h)) {
  33873. DUK__COMMA(); duk_fb_sprintf(fb, "__special_bufferobj:true");
  33874. } else {
  33875. ;
  33876. }
  33877. if (DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(h)) {
  33878. DUK__COMMA(); duk_fb_sprintf(fb, "__special_proxyobj:true");
  33879. } else {
  33880. ;
  33881. }
  33882. }
  33883. if (st->internal && DUK_HOBJECT_IS_COMPILEDFUNCTION(h)) {
  33884. duk_hcompiledfunction *f = (duk_hcompiledfunction *) h;
  33885. DUK__COMMA(); duk_fb_put_cstring(fb, "__data:");
  33886. duk__print_hbuffer(st, (duk_hbuffer *) DUK_HCOMPILEDFUNCTION_GET_DATA(NULL, f));
  33887. DUK__COMMA(); duk_fb_sprintf(fb, "__nregs:%ld", (long) f->nregs);
  33888. DUK__COMMA(); duk_fb_sprintf(fb, "__nargs:%ld", (long) f->nargs);
  33889. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  33890. DUK__COMMA(); duk_fb_sprintf(fb, "__start_line:%ld", (long) f->start_line);
  33891. DUK__COMMA(); duk_fb_sprintf(fb, "__end_line:%ld", (long) f->end_line);
  33892. #endif
  33893. DUK__COMMA(); duk_fb_put_cstring(fb, "__data:");
  33894. duk__print_hbuffer(st, (duk_hbuffer *) DUK_HCOMPILEDFUNCTION_GET_DATA(NULL, f));
  33895. } else if (st->internal && DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
  33896. duk_hnativefunction *f = (duk_hnativefunction *) h;
  33897. DUK__COMMA(); duk_fb_sprintf(fb, "__func:");
  33898. duk_fb_put_funcptr(fb, (duk_uint8_t *) &f->func, sizeof(f->func));
  33899. DUK__COMMA(); duk_fb_sprintf(fb, "__nargs:%ld", (long) f->nargs);
  33900. } else if (st->internal && DUK_HOBJECT_IS_BUFFEROBJECT(h)) {
  33901. duk_hbufferobject *b = (duk_hbufferobject *) h;
  33902. DUK__COMMA(); duk_fb_sprintf(fb, "__buf:");
  33903. duk__print_hbuffer(st, (duk_hbuffer *) b->buf);
  33904. DUK__COMMA(); duk_fb_sprintf(fb, "__offset:%ld", (long) b->offset);
  33905. DUK__COMMA(); duk_fb_sprintf(fb, "__length:%ld", (long) b->length);
  33906. DUK__COMMA(); duk_fb_sprintf(fb, "__shift:%ld", (long) b->shift);
  33907. DUK__COMMA(); duk_fb_sprintf(fb, "__elemtype:%ld", (long) b->elem_type);
  33908. } else if (st->internal && DUK_HOBJECT_IS_THREAD(h)) {
  33909. duk_hthread *t = (duk_hthread *) h;
  33910. DUK__COMMA(); duk_fb_sprintf(fb, "__strict:%ld", (long) t->strict);
  33911. DUK__COMMA(); duk_fb_sprintf(fb, "__state:%ld", (long) t->state);
  33912. DUK__COMMA(); duk_fb_sprintf(fb, "__unused1:%ld", (long) t->unused1);
  33913. DUK__COMMA(); duk_fb_sprintf(fb, "__unused2:%ld", (long) t->unused2);
  33914. DUK__COMMA(); duk_fb_sprintf(fb, "__valstack_max:%ld", (long) t->valstack_max);
  33915. DUK__COMMA(); duk_fb_sprintf(fb, "__callstack_max:%ld", (long) t->callstack_max);
  33916. DUK__COMMA(); duk_fb_sprintf(fb, "__catchstack_max:%ld", (long) t->catchstack_max);
  33917. DUK__COMMA(); duk_fb_sprintf(fb, "__valstack:%p", (void *) t->valstack);
  33918. DUK__COMMA(); duk_fb_sprintf(fb, "__valstack_end:%p/%ld", (void *) t->valstack_end, (long) (t->valstack_end - t->valstack));
  33919. DUK__COMMA(); duk_fb_sprintf(fb, "__valstack_bottom:%p/%ld", (void *) t->valstack_bottom, (long) (t->valstack_bottom - t->valstack));
  33920. DUK__COMMA(); duk_fb_sprintf(fb, "__valstack_top:%p/%ld", (void *) t->valstack_top, (long) (t->valstack_top - t->valstack));
  33921. DUK__COMMA(); duk_fb_sprintf(fb, "__catchstack:%p", (void *) t->catchstack);
  33922. DUK__COMMA(); duk_fb_sprintf(fb, "__catchstack_size:%ld", (long) t->catchstack_size);
  33923. DUK__COMMA(); duk_fb_sprintf(fb, "__catchstack_top:%ld", (long) t->catchstack_top);
  33924. DUK__COMMA(); duk_fb_sprintf(fb, "__resumer:"); duk__print_hobject(st, (duk_hobject *) t->resumer);
  33925. /* XXX: print built-ins array? */
  33926. }
  33927. #ifdef DUK_USE_REFERENCE_COUNTING
  33928. if (st->internal) {
  33929. DUK__COMMA(); duk_fb_sprintf(fb, "__refcount:%lu", (unsigned long) DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h));
  33930. }
  33931. #endif
  33932. if (st->internal) {
  33933. DUK__COMMA(); duk_fb_sprintf(fb, "__class:%ld", (long) DUK_HOBJECT_GET_CLASS_NUMBER(h));
  33934. }
  33935. /* prototype should be last, for readability */
  33936. if (st->follow_proto && DUK_HOBJECT_GET_PROTOTYPE(NULL, h)) {
  33937. DUK__COMMA(); duk_fb_put_cstring(fb, "__prototype:"); duk__print_hobject(st, DUK_HOBJECT_GET_PROTOTYPE(NULL, h));
  33938. }
  33939. duk_fb_put_cstring(fb, brace2);
  33940. #if defined(DUK_USE_HOBJECT_HASH_PART)
  33941. if (st->heavy && DUK_HOBJECT_GET_HSIZE(h) > 0) {
  33942. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_LANGLE);
  33943. for (i = 0; i < DUK_HOBJECT_GET_HSIZE(h); i++) {
  33944. duk_uint_t h_idx = DUK_HOBJECT_H_GET_INDEX(NULL, h, i);
  33945. if (i > 0) {
  33946. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_COMMA);
  33947. }
  33948. if (h_idx == DUK_HOBJECT_HASHIDX_UNUSED) {
  33949. duk_fb_sprintf(fb, "u");
  33950. } else if (h_idx == DUK_HOBJECT_HASHIDX_DELETED) {
  33951. duk_fb_sprintf(fb, "d");
  33952. } else {
  33953. duk_fb_sprintf(fb, "%ld", (long) h_idx);
  33954. }
  33955. }
  33956. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_RANGLE);
  33957. }
  33958. #endif
  33959. finished:
  33960. st->depth--;
  33961. if (pushed_loopstack) {
  33962. st->loop_stack_index--;
  33963. st->loop_stack[st->loop_stack_index] = NULL;
  33964. }
  33965. }
  33966. #undef DUK__COMMA
  33967. DUK_LOCAL void duk__print_hbuffer(duk__dprint_state *st, duk_hbuffer *h) {
  33968. duk_fixedbuffer *fb = st->fb;
  33969. duk_size_t i, n;
  33970. duk_uint8_t *p;
  33971. if (duk_fb_is_full(fb)) {
  33972. return;
  33973. }
  33974. /* terminal type: no depth check */
  33975. if (!h) {
  33976. duk_fb_put_cstring(fb, "NULL");
  33977. return;
  33978. }
  33979. if (DUK_HBUFFER_HAS_DYNAMIC(h)) {
  33980. if (DUK_HBUFFER_HAS_EXTERNAL(h)) {
  33981. duk_hbuffer_external *g = (duk_hbuffer_external *) h;
  33982. duk_fb_sprintf(fb, "buffer:external:%p:%ld",
  33983. (void *) DUK_HBUFFER_EXTERNAL_GET_DATA_PTR(NULL, g),
  33984. (long) DUK_HBUFFER_EXTERNAL_GET_SIZE(g));
  33985. } else {
  33986. duk_hbuffer_dynamic *g = (duk_hbuffer_dynamic *) h;
  33987. duk_fb_sprintf(fb, "buffer:dynamic:%p:%ld",
  33988. (void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(NULL, g),
  33989. (long) DUK_HBUFFER_DYNAMIC_GET_SIZE(g));
  33990. }
  33991. } else {
  33992. duk_fb_sprintf(fb, "buffer:fixed:%ld", (long) DUK_HBUFFER_GET_SIZE(h));
  33993. }
  33994. #ifdef DUK_USE_REFERENCE_COUNTING
  33995. duk_fb_sprintf(fb, "/%lu", (unsigned long) DUK_HEAPHDR_GET_REFCOUNT(&h->hdr));
  33996. #endif
  33997. if (st->hexdump) {
  33998. duk_fb_sprintf(fb, "=[");
  33999. n = DUK_HBUFFER_GET_SIZE(h);
  34000. p = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(NULL, h);
  34001. for (i = 0; i < n; i++) {
  34002. duk_fb_sprintf(fb, "%02lx", (unsigned long) p[i]);
  34003. }
  34004. duk_fb_sprintf(fb, "]");
  34005. }
  34006. }
  34007. DUK_LOCAL void duk__print_heaphdr(duk__dprint_state *st, duk_heaphdr *h) {
  34008. duk_fixedbuffer *fb = st->fb;
  34009. if (duk_fb_is_full(fb)) {
  34010. return;
  34011. }
  34012. if (!h) {
  34013. duk_fb_put_cstring(fb, "NULL");
  34014. return;
  34015. }
  34016. switch (DUK_HEAPHDR_GET_TYPE(h)) {
  34017. case DUK_HTYPE_STRING:
  34018. duk__print_hstring(st, (duk_hstring *) h, 1);
  34019. break;
  34020. case DUK_HTYPE_OBJECT:
  34021. duk__print_hobject(st, (duk_hobject *) h);
  34022. break;
  34023. case DUK_HTYPE_BUFFER:
  34024. duk__print_hbuffer(st, (duk_hbuffer *) h);
  34025. break;
  34026. default:
  34027. duk_fb_sprintf(fb, "[unknown htype %ld]", (long) DUK_HEAPHDR_GET_TYPE(h));
  34028. break;
  34029. }
  34030. }
  34031. DUK_LOCAL void duk__print_tval(duk__dprint_state *st, duk_tval *tv) {
  34032. duk_fixedbuffer *fb = st->fb;
  34033. if (duk_fb_is_full(fb)) {
  34034. return;
  34035. }
  34036. /* depth check is done when printing an actual type */
  34037. if (st->heavy) {
  34038. duk_fb_sprintf(fb, "(%p)", (void *) tv);
  34039. }
  34040. if (!tv) {
  34041. duk_fb_put_cstring(fb, "NULL");
  34042. return;
  34043. }
  34044. if (st->binary) {
  34045. duk_size_t i;
  34046. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_LBRACKET);
  34047. for (i = 0; i < (duk_size_t) sizeof(*tv); i++) {
  34048. duk_fb_sprintf(fb, "%02lx", (unsigned long) ((duk_uint8_t *)tv)[i]);
  34049. }
  34050. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_RBRACKET);
  34051. }
  34052. if (st->heavy) {
  34053. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_LANGLE);
  34054. }
  34055. switch (DUK_TVAL_GET_TAG(tv)) {
  34056. case DUK_TAG_UNDEFINED: {
  34057. duk_fb_put_cstring(fb, "undefined");
  34058. break;
  34059. }
  34060. case DUK_TAG_UNUSED: {
  34061. duk_fb_put_cstring(fb, "unused");
  34062. break;
  34063. }
  34064. case DUK_TAG_NULL: {
  34065. duk_fb_put_cstring(fb, "null");
  34066. break;
  34067. }
  34068. case DUK_TAG_BOOLEAN: {
  34069. duk_fb_put_cstring(fb, DUK_TVAL_GET_BOOLEAN(tv) ? "true" : "false");
  34070. break;
  34071. }
  34072. case DUK_TAG_STRING: {
  34073. /* Note: string is a terminal heap object, so no depth check here */
  34074. duk__print_hstring(st, DUK_TVAL_GET_STRING(tv), 1);
  34075. break;
  34076. }
  34077. case DUK_TAG_OBJECT: {
  34078. duk__print_hobject(st, DUK_TVAL_GET_OBJECT(tv));
  34079. break;
  34080. }
  34081. case DUK_TAG_BUFFER: {
  34082. duk__print_hbuffer(st, DUK_TVAL_GET_BUFFER(tv));
  34083. break;
  34084. }
  34085. case DUK_TAG_POINTER: {
  34086. duk_fb_sprintf(fb, "pointer:%p", (void *) DUK_TVAL_GET_POINTER(tv));
  34087. break;
  34088. }
  34089. case DUK_TAG_LIGHTFUNC: {
  34090. duk_c_function func;
  34091. duk_small_uint_t lf_flags;
  34092. DUK_TVAL_GET_LIGHTFUNC(tv, func, lf_flags);
  34093. duk_fb_sprintf(fb, "lightfunc:");
  34094. duk_fb_put_funcptr(fb, (duk_uint8_t *) &func, sizeof(func));
  34095. duk_fb_sprintf(fb, ":%04lx", (long) lf_flags);
  34096. break;
  34097. }
  34098. #if defined(DUK_USE_FASTINT)
  34099. case DUK_TAG_FASTINT:
  34100. #endif
  34101. default: {
  34102. /* IEEE double is approximately 16 decimal digits; print a couple extra */
  34103. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  34104. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  34105. duk_fb_sprintf(fb, "%.18g", (double) DUK_TVAL_GET_NUMBER(tv));
  34106. break;
  34107. }
  34108. }
  34109. if (st->heavy) {
  34110. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_RANGLE);
  34111. }
  34112. }
  34113. DUK_LOCAL void duk__print_instr(duk__dprint_state *st, duk_instr_t ins) {
  34114. duk_fixedbuffer *fb = st->fb;
  34115. duk_small_int_t op;
  34116. const char *op_name;
  34117. const char *extraop_name;
  34118. op = (duk_small_int_t) DUK_DEC_OP(ins);
  34119. op_name = duk__bc_optab[op];
  34120. /* XXX: option to fix opcode length so it lines up nicely */
  34121. if (op == DUK_OP_EXTRA) {
  34122. extraop_name = duk__bc_extraoptab[DUK_DEC_A(ins)];
  34123. duk_fb_sprintf(fb, "%s %ld, %ld",
  34124. (const char *) extraop_name, (long) DUK_DEC_B(ins), (long) DUK_DEC_C(ins));
  34125. } else if (op == DUK_OP_JUMP) {
  34126. duk_int_t diff1 = DUK_DEC_ABC(ins) - DUK_BC_JUMP_BIAS; /* from next pc */
  34127. duk_int_t diff2 = diff1 + 1; /* from curr pc */
  34128. duk_fb_sprintf(fb, "%s %ld (to pc%c%ld)",
  34129. (const char *) op_name, (long) diff1,
  34130. (int) (diff2 >= 0 ? '+' : '-'), /* char format: use int */
  34131. (long) (diff2 >= 0 ? diff2 : -diff2));
  34132. } else {
  34133. duk_fb_sprintf(fb, "%s %ld, %ld, %ld",
  34134. (const char *) op_name, (long) DUK_DEC_A(ins),
  34135. (long) DUK_DEC_B(ins), (long) DUK_DEC_C(ins));
  34136. }
  34137. }
  34138. DUK_LOCAL void duk__print_opcode(duk__dprint_state *st, duk_small_int_t opcode) {
  34139. duk_fixedbuffer *fb = st->fb;
  34140. if (opcode < DUK_BC_OP_MIN || opcode > DUK_BC_OP_MAX) {
  34141. duk_fb_sprintf(fb, "?(%ld)", (long) opcode);
  34142. } else {
  34143. duk_fb_sprintf(fb, "%s", (const char *) duk__bc_optab[opcode]);
  34144. }
  34145. }
  34146. DUK_INTERNAL duk_int_t duk_debug_vsnprintf(char *str, duk_size_t size, const char *format, va_list ap) {
  34147. duk_fixedbuffer fb;
  34148. const char *p = format;
  34149. const char *p_end = p + DUK_STRLEN(format);
  34150. duk_int_t retval;
  34151. DUK_MEMZERO(&fb, sizeof(fb));
  34152. fb.buffer = (duk_uint8_t *) str;
  34153. fb.length = size;
  34154. fb.offset = 0;
  34155. fb.truncated = 0;
  34156. while (p < p_end) {
  34157. char ch = *p++;
  34158. const char *p_begfmt = NULL;
  34159. duk_bool_t got_exclamation = 0;
  34160. duk_bool_t got_long = 0; /* %lf, %ld etc */
  34161. duk__dprint_state st;
  34162. if (ch != DUK_ASC_PERCENT) {
  34163. duk_fb_put_byte(&fb, (duk_uint8_t) ch);
  34164. continue;
  34165. }
  34166. /*
  34167. * Format tag parsing. Since we don't understand all the
  34168. * possible format tags allowed, we just scan for a terminating
  34169. * specifier and keep track of relevant modifiers that we do
  34170. * understand. See man 3 printf.
  34171. */
  34172. DUK_MEMZERO(&st, sizeof(st));
  34173. st.fb = &fb;
  34174. st.depth = 0;
  34175. st.depth_limit = 1;
  34176. st.loop_stack_index = 0;
  34177. st.loop_stack_limit = DUK__LOOP_STACK_DEPTH;
  34178. p_begfmt = p - 1;
  34179. while (p < p_end) {
  34180. ch = *p++;
  34181. if (ch == DUK_ASC_STAR) {
  34182. /* unsupported: would consume multiple args */
  34183. goto error;
  34184. } else if (ch == DUK_ASC_PERCENT) {
  34185. duk_fb_put_byte(&fb, (duk_uint8_t) DUK_ASC_PERCENT);
  34186. break;
  34187. } else if (ch == DUK_ASC_EXCLAMATION) {
  34188. got_exclamation = 1;
  34189. } else if (!got_exclamation && ch == DUK_ASC_LC_L) {
  34190. got_long = 1;
  34191. } else if (got_exclamation && ch == DUK_ASC_LC_D) {
  34192. st.depth_limit = DUK__DEEP_DEPTH_LIMIT;
  34193. } else if (got_exclamation && ch == DUK_ASC_LC_P) {
  34194. st.follow_proto = 1;
  34195. } else if (got_exclamation && ch == DUK_ASC_LC_I) {
  34196. st.internal = 1;
  34197. } else if (got_exclamation && ch == DUK_ASC_LC_X) {
  34198. st.hexdump = 1;
  34199. } else if (got_exclamation && ch == DUK_ASC_LC_H) {
  34200. st.heavy = 1;
  34201. } else if (got_exclamation && ch == DUK_ASC_ATSIGN) {
  34202. st.pointer = 1;
  34203. } else if (got_exclamation && ch == DUK_ASC_HASH) {
  34204. st.binary = 1;
  34205. } else if (got_exclamation && ch == DUK_ASC_UC_T) {
  34206. duk_tval *t = va_arg(ap, duk_tval *);
  34207. if (st.pointer && !st.heavy) {
  34208. duk_fb_sprintf(&fb, "(%p)", (void *) t);
  34209. }
  34210. duk__print_tval(&st, t);
  34211. break;
  34212. } else if (got_exclamation && ch == DUK_ASC_UC_O) {
  34213. duk_heaphdr *t = va_arg(ap, duk_heaphdr *);
  34214. if (st.pointer && !st.heavy) {
  34215. duk_fb_sprintf(&fb, "(%p)", (void *) t);
  34216. }
  34217. duk__print_heaphdr(&st, t);
  34218. break;
  34219. } else if (got_exclamation && ch == DUK_ASC_UC_I) {
  34220. duk_instr_t t = va_arg(ap, duk_instr_t);
  34221. duk__print_instr(&st, t);
  34222. break;
  34223. } else if (got_exclamation && ch == DUK_ASC_UC_C) {
  34224. long t = va_arg(ap, long);
  34225. duk__print_opcode(&st, (duk_small_int_t) t);
  34226. break;
  34227. } else if (!got_exclamation && strchr(DUK__ALLOWED_STANDARD_SPECIFIERS, (int) ch)) {
  34228. char fmtbuf[DUK__MAX_FORMAT_TAG_LENGTH];
  34229. duk_size_t fmtlen;
  34230. DUK_ASSERT(p >= p_begfmt);
  34231. fmtlen = (duk_size_t) (p - p_begfmt);
  34232. if (fmtlen >= sizeof(fmtbuf)) {
  34233. /* format is too large, abort */
  34234. goto error;
  34235. }
  34236. DUK_MEMZERO(fmtbuf, sizeof(fmtbuf));
  34237. DUK_MEMCPY(fmtbuf, p_begfmt, fmtlen);
  34238. /* assume exactly 1 arg, which is why '*' is forbidden; arg size still
  34239. * depends on type though.
  34240. */
  34241. if (ch == DUK_ASC_LC_F || ch == DUK_ASC_LC_G || ch == DUK_ASC_LC_E) {
  34242. /* %f and %lf both consume a 'long' */
  34243. double arg = va_arg(ap, double);
  34244. duk_fb_sprintf(&fb, fmtbuf, arg);
  34245. } else if (ch == DUK_ASC_LC_D && got_long) {
  34246. /* %ld */
  34247. long arg = va_arg(ap, long);
  34248. duk_fb_sprintf(&fb, fmtbuf, arg);
  34249. } else if (ch == DUK_ASC_LC_D) {
  34250. /* %d; only 16 bits are guaranteed */
  34251. int arg = va_arg(ap, int);
  34252. duk_fb_sprintf(&fb, fmtbuf, arg);
  34253. } else if (ch == DUK_ASC_LC_U && got_long) {
  34254. /* %lu */
  34255. unsigned long arg = va_arg(ap, unsigned long);
  34256. duk_fb_sprintf(&fb, fmtbuf, arg);
  34257. } else if (ch == DUK_ASC_LC_U) {
  34258. /* %u; only 16 bits are guaranteed */
  34259. unsigned int arg = va_arg(ap, unsigned int);
  34260. duk_fb_sprintf(&fb, fmtbuf, arg);
  34261. } else if (ch == DUK_ASC_LC_X && got_long) {
  34262. /* %lx */
  34263. unsigned long arg = va_arg(ap, unsigned long);
  34264. duk_fb_sprintf(&fb, fmtbuf, arg);
  34265. } else if (ch == DUK_ASC_LC_X) {
  34266. /* %x; only 16 bits are guaranteed */
  34267. unsigned int arg = va_arg(ap, unsigned int);
  34268. duk_fb_sprintf(&fb, fmtbuf, arg);
  34269. } else if (ch == DUK_ASC_LC_S) {
  34270. /* %s */
  34271. const char *arg = va_arg(ap, const char *);
  34272. if (arg == NULL) {
  34273. /* '%s' and NULL is not portable, so special case
  34274. * it for debug printing.
  34275. */
  34276. duk_fb_sprintf(&fb, "NULL");
  34277. } else {
  34278. duk_fb_sprintf(&fb, fmtbuf, arg);
  34279. }
  34280. } else if (ch == DUK_ASC_LC_P) {
  34281. /* %p */
  34282. void *arg = va_arg(ap, void *);
  34283. if (arg == NULL) {
  34284. /* '%p' and NULL is portable, but special case it
  34285. * anyway to get a standard NULL marker in logs.
  34286. */
  34287. duk_fb_sprintf(&fb, "NULL");
  34288. } else {
  34289. duk_fb_sprintf(&fb, fmtbuf, arg);
  34290. }
  34291. } else if (ch == DUK_ASC_LC_C) {
  34292. /* '%c', passed concretely as int */
  34293. int arg = va_arg(ap, int);
  34294. duk_fb_sprintf(&fb, fmtbuf, arg);
  34295. } else {
  34296. /* Should not happen. */
  34297. duk_fb_sprintf(&fb, "INVALID-FORMAT(%s)", (const char *) fmtbuf);
  34298. }
  34299. break;
  34300. } else {
  34301. /* ignore */
  34302. }
  34303. }
  34304. }
  34305. goto done;
  34306. error:
  34307. duk_fb_put_cstring(&fb, "FMTERR");
  34308. /* fall through */
  34309. done:
  34310. retval = (duk_int_t) fb.offset;
  34311. duk_fb_put_byte(&fb, (duk_uint8_t) 0);
  34312. /* return total chars written excluding terminator */
  34313. return retval;
  34314. }
  34315. #if 0 /*unused*/
  34316. DUK_INTERNAL duk_int_t duk_debug_snprintf(char *str, duk_size_t size, const char *format, ...) {
  34317. duk_int_t retval;
  34318. va_list ap;
  34319. va_start(ap, format);
  34320. retval = duk_debug_vsnprintf(str, size, format, ap);
  34321. va_end(ap);
  34322. return retval;
  34323. }
  34324. #endif
  34325. /* Formatting function pointers is tricky: there is no standard pointer for
  34326. * function pointers and the size of a function pointer may depend on the
  34327. * specific pointer type. This helper formats a function pointer based on
  34328. * its memory layout to get something useful on most platforms.
  34329. */
  34330. DUK_INTERNAL void duk_debug_format_funcptr(char *buf, duk_size_t buf_size, duk_uint8_t *fptr, duk_size_t fptr_size) {
  34331. duk_size_t i;
  34332. duk_uint8_t *p = (duk_uint8_t *) buf;
  34333. duk_uint8_t *p_end = (duk_uint8_t *) (buf + buf_size - 1);
  34334. DUK_MEMZERO(buf, buf_size);
  34335. for (i = 0; i < fptr_size; i++) {
  34336. duk_int_t left = (duk_int_t) (p_end - p);
  34337. duk_uint8_t ch;
  34338. if (left <= 0) {
  34339. break;
  34340. }
  34341. /* Quite approximate but should be useful for little and big endian. */
  34342. #ifdef DUK_USE_INTEGER_BE
  34343. ch = fptr[i];
  34344. #else
  34345. ch = fptr[fptr_size - 1 - i];
  34346. #endif
  34347. p += DUK_SNPRINTF((char *) p, left, "%02lx", (unsigned long) ch);
  34348. }
  34349. }
  34350. #endif /* DUK_USE_DEBUG */
  34351. #line 1 "duk_debugger.c"
  34352. /*
  34353. * Duktape debugger
  34354. */
  34355. /* include removed: duk_internal.h */
  34356. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  34357. /*
  34358. * Helper structs
  34359. */
  34360. typedef union {
  34361. void *p;
  34362. duk_uint_t b[1];
  34363. /* Use b[] to access the size of the union, which is strictly not
  34364. * correct. Can't use fixed size unless there's feature detection
  34365. * for pointer byte size.
  34366. */
  34367. } duk__ptr_union;
  34368. /*
  34369. * Detach handling
  34370. */
  34371. #define DUK__SET_CONN_BROKEN(thr) do { \
  34372. /* For now shared handler is fine. */ \
  34373. duk__debug_do_detach1((thr)->heap); \
  34374. } while (0)
  34375. DUK_LOCAL void duk__debug_do_detach1(duk_heap *heap) {
  34376. /* Can be called multiple times with no harm. Mark the transport
  34377. * bad (dbg_read_cb == NULL) and clear state except for the detached
  34378. * callback and the udata field. The detached callback is delayed
  34379. * to the message loop so that it can be called between messages;
  34380. * this avoids corner cases related to immediate debugger reattach
  34381. * inside the detached callback.
  34382. */
  34383. DUK_D(DUK_DPRINT("debugger transport detaching, marking transport broken"));
  34384. heap->dbg_read_cb = NULL;
  34385. heap->dbg_write_cb = NULL;
  34386. heap->dbg_peek_cb = NULL;
  34387. heap->dbg_read_flush_cb = NULL;
  34388. heap->dbg_write_flush_cb = NULL;
  34389. /* heap->dbg_detached_cb: keep */
  34390. /* heap->dbg_udata: keep */
  34391. heap->dbg_processing = 0;
  34392. heap->dbg_paused = 0;
  34393. heap->dbg_state_dirty = 0;
  34394. heap->dbg_force_restart = 0;
  34395. heap->dbg_step_type = 0;
  34396. heap->dbg_step_thread = NULL;
  34397. heap->dbg_step_csindex = 0;
  34398. heap->dbg_step_startline = 0;
  34399. heap->dbg_have_next_byte = 0;
  34400. /* Ensure there are no stale active breakpoint pointers.
  34401. * Breakpoint list is currently kept - we could empty it
  34402. * here but we'd need to handle refcounts correctly, and
  34403. * we'd need a 'thr' reference for that.
  34404. *
  34405. * XXX: clear breakpoint on either attach or detach?
  34406. */
  34407. heap->dbg_breakpoints_active[0] = (duk_breakpoint *) NULL;
  34408. }
  34409. DUK_LOCAL void duk__debug_do_detach2(duk_heap *heap) {
  34410. duk_debug_detached_function detached_cb;
  34411. void *detached_udata;
  34412. /* Safe to call multiple times. */
  34413. detached_cb = heap->dbg_detached_cb;
  34414. detached_udata = heap->dbg_udata;
  34415. heap->dbg_detached_cb = NULL;
  34416. heap->dbg_udata = NULL;
  34417. if (detached_cb) {
  34418. /* Careful here: state must be wiped before the call
  34419. * so that we can cleanly handle a re-attach from
  34420. * inside the callback.
  34421. */
  34422. DUK_D(DUK_DPRINT("detached during message loop, delayed call to detached_cb"));
  34423. detached_cb(detached_udata);
  34424. }
  34425. }
  34426. DUK_INTERNAL void duk_debug_do_detach(duk_heap *heap) {
  34427. duk__debug_do_detach1(heap);
  34428. duk__debug_do_detach2(heap);
  34429. }
  34430. /*
  34431. * Debug connection peek and flush primitives
  34432. */
  34433. DUK_INTERNAL duk_bool_t duk_debug_read_peek(duk_hthread *thr) {
  34434. duk_heap *heap;
  34435. DUK_ASSERT(thr != NULL);
  34436. heap = thr->heap;
  34437. DUK_ASSERT(heap != NULL);
  34438. if (heap->dbg_read_cb == NULL) {
  34439. DUK_D(DUK_DPRINT("attempt to peek in detached state, return zero (= no data)"));
  34440. return 0;
  34441. }
  34442. if (heap->dbg_peek_cb == NULL) {
  34443. DUK_DD(DUK_DDPRINT("no peek callback, return zero (= no data)"));
  34444. return 0;
  34445. }
  34446. return (duk_bool_t) (heap->dbg_peek_cb(heap->dbg_udata) > 0);
  34447. }
  34448. DUK_INTERNAL void duk_debug_read_flush(duk_hthread *thr) {
  34449. duk_heap *heap;
  34450. DUK_ASSERT(thr != NULL);
  34451. heap = thr->heap;
  34452. DUK_ASSERT(heap != NULL);
  34453. if (heap->dbg_read_cb == NULL) {
  34454. DUK_D(DUK_DPRINT("attempt to read flush in detached state, ignore"));
  34455. return;
  34456. }
  34457. if (heap->dbg_read_flush_cb == NULL) {
  34458. DUK_DD(DUK_DDPRINT("no read flush callback, ignore"));
  34459. return;
  34460. }
  34461. heap->dbg_read_flush_cb(heap->dbg_udata);
  34462. }
  34463. DUK_INTERNAL void duk_debug_write_flush(duk_hthread *thr) {
  34464. duk_heap *heap;
  34465. DUK_ASSERT(thr != NULL);
  34466. heap = thr->heap;
  34467. DUK_ASSERT(heap != NULL);
  34468. if (heap->dbg_read_cb == NULL) {
  34469. DUK_D(DUK_DPRINT("attempt to write flush in detached state, ignore"));
  34470. return;
  34471. }
  34472. if (heap->dbg_write_flush_cb == NULL) {
  34473. DUK_DD(DUK_DDPRINT("no write flush callback, ignore"));
  34474. return;
  34475. }
  34476. heap->dbg_write_flush_cb(heap->dbg_udata);
  34477. }
  34478. /*
  34479. * Debug connection skip primitives
  34480. */
  34481. /* Skip fully. */
  34482. DUK_INTERNAL void duk_debug_skip_bytes(duk_hthread *thr, duk_size_t length) {
  34483. duk_uint8_t dummy[64];
  34484. duk_size_t now;
  34485. DUK_ASSERT(thr != NULL);
  34486. while (length > 0) {
  34487. now = (length > sizeof(dummy) ? sizeof(dummy) : length);
  34488. duk_debug_read_bytes(thr, dummy, now);
  34489. length -= now;
  34490. }
  34491. }
  34492. DUK_INTERNAL void duk_debug_skip_byte(duk_hthread *thr) {
  34493. DUK_ASSERT(thr != NULL);
  34494. (void) duk_debug_read_byte(thr);
  34495. }
  34496. /*
  34497. * Debug connection read primitives
  34498. */
  34499. /* Peek ahead in the stream one byte. */
  34500. DUK_INTERNAL uint8_t duk_debug_peek_byte(duk_hthread *thr) {
  34501. /* It is important not to call this if the last byte read was an EOM.
  34502. * Reading ahead in this scenario would cause unnecessary blocking if
  34503. * another message is not available.
  34504. */
  34505. duk_uint8_t x;
  34506. x = duk_debug_read_byte(thr);
  34507. thr->heap->dbg_have_next_byte = 1;
  34508. thr->heap->dbg_next_byte = x;
  34509. return x;
  34510. }
  34511. /* Read fully. */
  34512. DUK_INTERNAL void duk_debug_read_bytes(duk_hthread *thr, duk_uint8_t *data, duk_size_t length) {
  34513. duk_heap *heap;
  34514. duk_uint8_t *p;
  34515. duk_size_t left;
  34516. duk_size_t got;
  34517. DUK_ASSERT(thr != NULL);
  34518. heap = thr->heap;
  34519. DUK_ASSERT(heap != NULL);
  34520. if (heap->dbg_read_cb == NULL) {
  34521. DUK_D(DUK_DPRINT("attempt to read %ld bytes in detached state, return zero data", (long) length));
  34522. goto fail;
  34523. }
  34524. /* NOTE: length may be zero */
  34525. p = data;
  34526. if (length >= 1 && heap->dbg_have_next_byte) {
  34527. heap->dbg_have_next_byte = 0;
  34528. *p++ = heap->dbg_next_byte;
  34529. }
  34530. for (;;) {
  34531. left = (duk_size_t) ((data + length) - p);
  34532. if (left == 0) {
  34533. break;
  34534. }
  34535. DUK_ASSERT(heap->dbg_read_cb != NULL);
  34536. DUK_ASSERT(left >= 1);
  34537. #if defined(DUK_USE_DEBUGGER_TRANSPORT_TORTURE)
  34538. left = 1;
  34539. #endif
  34540. got = heap->dbg_read_cb(heap->dbg_udata, (char *) p, left);
  34541. if (got == 0 || got > left) {
  34542. DUK_D(DUK_DPRINT("connection error during read, return zero data"));
  34543. DUK__SET_CONN_BROKEN(thr);
  34544. goto fail;
  34545. }
  34546. p += got;
  34547. }
  34548. return;
  34549. fail:
  34550. DUK_MEMZERO((void *) data, (size_t) length);
  34551. }
  34552. DUK_INTERNAL duk_uint8_t duk_debug_read_byte(duk_hthread *thr) {
  34553. duk_heap *heap;
  34554. duk_size_t got;
  34555. duk_uint8_t x;
  34556. DUK_ASSERT(thr != NULL);
  34557. heap = thr->heap;
  34558. DUK_ASSERT(heap != NULL);
  34559. if (heap->dbg_read_cb == NULL) {
  34560. DUK_D(DUK_DPRINT("attempt to read 1 bytes in detached state, return zero data"));
  34561. return 0;
  34562. }
  34563. if (heap->dbg_have_next_byte) {
  34564. heap->dbg_have_next_byte = 0;
  34565. return heap->dbg_next_byte;
  34566. }
  34567. x = 0; /* just in case callback is broken and won't write 'x' */
  34568. DUK_ASSERT(heap->dbg_read_cb != NULL);
  34569. got = heap->dbg_read_cb(heap->dbg_udata, (char *) (&x), 1);
  34570. if (got != 1) {
  34571. DUK_D(DUK_DPRINT("connection error during read, return zero data"));
  34572. DUK__SET_CONN_BROKEN(thr);
  34573. return 0;
  34574. }
  34575. return x;
  34576. }
  34577. DUK_LOCAL duk_uint32_t duk__debug_read_uint32_raw(duk_hthread *thr) {
  34578. duk_uint8_t buf[4];
  34579. DUK_ASSERT(thr != NULL);
  34580. duk_debug_read_bytes(thr, buf, 4);
  34581. return ((duk_uint32_t) buf[0] << 24) |
  34582. ((duk_uint32_t) buf[1] << 16) |
  34583. ((duk_uint32_t) buf[2] << 8) |
  34584. (duk_uint32_t) buf[3];
  34585. }
  34586. DUK_LOCAL duk_uint32_t duk__debug_read_int32_raw(duk_hthread *thr) {
  34587. return (duk_int32_t) duk__debug_read_uint32_raw(thr);
  34588. }
  34589. DUK_LOCAL duk_uint16_t duk__debug_read_uint16_raw(duk_hthread *thr) {
  34590. duk_uint8_t buf[2];
  34591. DUK_ASSERT(thr != NULL);
  34592. duk_debug_read_bytes(thr, buf, 2);
  34593. return ((duk_uint16_t) buf[0] << 8) |
  34594. (duk_uint16_t) buf[1];
  34595. }
  34596. DUK_INTERNAL duk_int32_t duk_debug_read_int(duk_hthread *thr) {
  34597. duk_small_uint_t x;
  34598. duk_small_uint_t t;
  34599. DUK_ASSERT(thr != NULL);
  34600. x = duk_debug_read_byte(thr);
  34601. if (x >= 0xc0) {
  34602. t = duk_debug_read_byte(thr);
  34603. return (duk_int32_t) (((x - 0xc0) << 8) + t);
  34604. } else if (x >= 0x80) {
  34605. return (duk_int32_t) (x - 0x80);
  34606. } else if (x == 0x10) {
  34607. return (duk_int32_t) duk__debug_read_uint32_raw(thr);
  34608. }
  34609. DUK_D(DUK_DPRINT("debug connection error: failed to decode int"));
  34610. DUK__SET_CONN_BROKEN(thr);
  34611. return 0;
  34612. }
  34613. DUK_LOCAL duk_hstring *duk__debug_read_hstring_raw(duk_hthread *thr, duk_uint32_t len) {
  34614. duk_context *ctx = (duk_context *) thr;
  34615. duk_uint8_t buf[31];
  34616. duk_uint8_t *p;
  34617. if (len <= sizeof(buf)) {
  34618. duk_debug_read_bytes(thr, buf, (duk_size_t) len);
  34619. duk_push_lstring(ctx, (const char *) buf, (duk_size_t) len);
  34620. } else {
  34621. p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) len);
  34622. DUK_ASSERT(p != NULL);
  34623. duk_debug_read_bytes(thr, p, (duk_size_t) len);
  34624. duk_to_string(ctx, -1);
  34625. }
  34626. return duk_require_hstring(ctx, -1);
  34627. }
  34628. DUK_INTERNAL duk_hstring *duk_debug_read_hstring(duk_hthread *thr) {
  34629. duk_context *ctx = (duk_context *) thr;
  34630. duk_small_uint_t x;
  34631. duk_uint32_t len;
  34632. DUK_ASSERT(thr != NULL);
  34633. x = duk_debug_read_byte(thr);
  34634. if (x >= 0x60 && x <= 0x7f) {
  34635. /* For short strings, use a fixed temp buffer. */
  34636. len = (duk_uint32_t) (x - 0x60);
  34637. } else if (x == 0x12) {
  34638. len = (duk_uint32_t) duk__debug_read_uint16_raw(thr);
  34639. } else if (x == 0x11) {
  34640. len = (duk_uint32_t) duk__debug_read_uint32_raw(thr);
  34641. } else {
  34642. goto fail;
  34643. }
  34644. return duk__debug_read_hstring_raw(thr, len);
  34645. fail:
  34646. DUK_D(DUK_DPRINT("debug connection error: failed to decode int"));
  34647. DUK__SET_CONN_BROKEN(thr);
  34648. duk_push_hstring_stridx(thr, DUK_STRIDX_EMPTY_STRING); /* always push some string */
  34649. return duk_require_hstring(ctx, -1);
  34650. }
  34651. DUK_LOCAL duk_hbuffer *duk__debug_read_hbuffer_raw(duk_hthread *thr, duk_uint32_t len) {
  34652. duk_context *ctx = (duk_context *) thr;
  34653. duk_uint8_t *p;
  34654. p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) len);
  34655. DUK_ASSERT(p != NULL);
  34656. duk_debug_read_bytes(thr, p, (duk_size_t) len);
  34657. return duk_require_hbuffer(ctx, -1);
  34658. }
  34659. DUK_LOCAL void *duk__debug_read_pointer_raw(duk_hthread *thr) {
  34660. duk_small_uint_t x;
  34661. duk__ptr_union pu;
  34662. DUK_ASSERT(thr != NULL);
  34663. x = duk_debug_read_byte(thr);
  34664. if (x != sizeof(pu)) {
  34665. goto fail;
  34666. }
  34667. duk_debug_read_bytes(thr, (duk_uint8_t *) &pu.p, sizeof(pu));
  34668. #if defined(DUK_USE_INTEGER_LE)
  34669. duk_byteswap_bytes((duk_uint8_t *) pu.b, sizeof(pu));
  34670. #endif
  34671. return (void *) pu.p;
  34672. fail:
  34673. DUK_D(DUK_DPRINT("debug connection error: failed to decode pointer"));
  34674. DUK__SET_CONN_BROKEN(thr);
  34675. return (void *) NULL;
  34676. }
  34677. DUK_LOCAL duk_double_t duk__debug_read_double_raw(duk_hthread *thr) {
  34678. duk_double_union du;
  34679. DUK_ASSERT(sizeof(du.uc) == 8);
  34680. duk_debug_read_bytes(thr, (duk_uint8_t *) du.uc, sizeof(du.uc));
  34681. DUK_DBLUNION_DOUBLE_NTOH(&du);
  34682. return du.d;
  34683. }
  34684. DUK_INTERNAL void duk_debug_read_tval(duk_hthread *thr) {
  34685. duk_context *ctx = (duk_context *) thr;
  34686. duk_uint8_t x;
  34687. duk_uint_t t;
  34688. duk_uint32_t len;
  34689. DUK_ASSERT(thr != NULL);
  34690. x = duk_debug_read_byte(thr);
  34691. if (x >= 0xc0) {
  34692. t = (duk_uint_t) (x - 0xc0);
  34693. t = (t << 8) + duk_debug_read_byte(thr);
  34694. duk_push_uint(ctx, (duk_uint_t) t);
  34695. return;
  34696. }
  34697. if (x >= 0x80) {
  34698. duk_push_uint(ctx, (duk_uint_t) (x - 0x80));
  34699. return;
  34700. }
  34701. if (x >= 0x60) {
  34702. len = (duk_uint32_t) (x - 0x60);
  34703. duk__debug_read_hstring_raw(thr, len);
  34704. return;
  34705. }
  34706. switch (x) {
  34707. case 0x10: {
  34708. duk_int32_t i = duk__debug_read_int32_raw(thr);
  34709. duk_push_i32(ctx, i);
  34710. break;
  34711. }
  34712. case 0x11:
  34713. len = duk__debug_read_uint32_raw(thr);
  34714. duk__debug_read_hstring_raw(thr, len);
  34715. break;
  34716. case 0x12:
  34717. len = duk__debug_read_uint16_raw(thr);
  34718. duk__debug_read_hstring_raw(thr, len);
  34719. break;
  34720. case 0x13:
  34721. len = duk__debug_read_uint32_raw(thr);
  34722. duk__debug_read_hbuffer_raw(thr, len);
  34723. break;
  34724. case 0x14:
  34725. len = duk__debug_read_uint16_raw(thr);
  34726. duk__debug_read_hbuffer_raw(thr, len);
  34727. break;
  34728. case 0x16:
  34729. duk_push_undefined(ctx);
  34730. break;
  34731. case 0x17:
  34732. duk_push_null(ctx);
  34733. break;
  34734. case 0x18:
  34735. duk_push_true(ctx);
  34736. break;
  34737. case 0x19:
  34738. duk_push_false(ctx);
  34739. break;
  34740. case 0x1a: {
  34741. duk_double_t d;
  34742. d = duk__debug_read_double_raw(thr);
  34743. duk_push_number(ctx, d);
  34744. break;
  34745. }
  34746. case 0x1b:
  34747. /* XXX: not needed for now, so not implemented */
  34748. DUK_D(DUK_DPRINT("reading object values unimplemented"));
  34749. goto fail;
  34750. case 0x1c: {
  34751. void *ptr;
  34752. ptr = duk__debug_read_pointer_raw(thr);
  34753. duk_push_pointer(thr, ptr);
  34754. break;
  34755. }
  34756. case 0x1d:
  34757. /* XXX: not needed for now, so not implemented */
  34758. DUK_D(DUK_DPRINT("reading lightfunc values unimplemented"));
  34759. goto fail;
  34760. case 0x1e: {
  34761. duk_heaphdr *h;
  34762. h = (duk_heaphdr *) duk__debug_read_pointer_raw(thr);
  34763. duk_push_heapptr(thr, (void *) h);
  34764. break;
  34765. }
  34766. case 0x15: /* unused: not accepted in inbound messages */
  34767. default:
  34768. goto fail;
  34769. }
  34770. return;
  34771. fail:
  34772. DUK_D(DUK_DPRINT("debug connection error: failed to decode tval"));
  34773. DUK__SET_CONN_BROKEN(thr);
  34774. }
  34775. /*
  34776. * Debug connection write primitives
  34777. */
  34778. /* Write fully. */
  34779. DUK_INTERNAL void duk_debug_write_bytes(duk_hthread *thr, const duk_uint8_t *data, duk_size_t length) {
  34780. duk_heap *heap;
  34781. const duk_uint8_t *p;
  34782. duk_size_t left;
  34783. duk_size_t got;
  34784. DUK_ASSERT(thr != NULL);
  34785. DUK_ASSERT(length == 0 || data != NULL);
  34786. heap = thr->heap;
  34787. DUK_ASSERT(heap != NULL);
  34788. if (heap->dbg_write_cb == NULL) {
  34789. DUK_D(DUK_DPRINT("attempt to write %ld bytes in detached state, ignore", (long) length));
  34790. return;
  34791. }
  34792. if (length == 0) {
  34793. /* Avoid doing an actual write callback with length == 0,
  34794. * because that's reserved for a write flush.
  34795. */
  34796. return;
  34797. }
  34798. DUK_ASSERT(data != NULL);
  34799. p = data;
  34800. for (;;) {
  34801. left = (duk_size_t) ((data + length) - p);
  34802. if (left == 0) {
  34803. break;
  34804. }
  34805. DUK_ASSERT(heap->dbg_write_cb != NULL);
  34806. DUK_ASSERT(left >= 1);
  34807. #if defined(DUK_USE_DEBUGGER_TRANSPORT_TORTURE)
  34808. left = 1;
  34809. #endif
  34810. got = heap->dbg_write_cb(heap->dbg_udata, (const char *) p, left);
  34811. if (got == 0 || got > left) {
  34812. DUK_D(DUK_DPRINT("connection error during write"));
  34813. DUK__SET_CONN_BROKEN(thr);
  34814. return;
  34815. }
  34816. p += got;
  34817. }
  34818. }
  34819. DUK_INTERNAL void duk_debug_write_byte(duk_hthread *thr, duk_uint8_t x) {
  34820. duk_heap *heap;
  34821. duk_size_t got;
  34822. DUK_ASSERT(thr != NULL);
  34823. heap = thr->heap;
  34824. DUK_ASSERT(heap != NULL);
  34825. if (heap->dbg_write_cb == NULL) {
  34826. DUK_D(DUK_DPRINT("attempt to write 1 bytes in detached state, ignore"));
  34827. return;
  34828. }
  34829. DUK_ASSERT(heap->dbg_write_cb != NULL);
  34830. got = heap->dbg_write_cb(heap->dbg_udata, (const char *) (&x), 1);
  34831. if (got != 1) {
  34832. DUK_D(DUK_DPRINT("connection error during write"));
  34833. DUK__SET_CONN_BROKEN(thr);
  34834. }
  34835. }
  34836. DUK_INTERNAL void duk_debug_write_unused(duk_hthread *thr) {
  34837. duk_debug_write_byte(thr, 0x15);
  34838. }
  34839. DUK_INTERNAL void duk_debug_write_undefined(duk_hthread *thr) {
  34840. duk_debug_write_byte(thr, 0x16);
  34841. }
  34842. /* Write signed 32-bit integer. */
  34843. DUK_INTERNAL void duk_debug_write_int(duk_hthread *thr, duk_int32_t x) {
  34844. duk_uint8_t buf[5];
  34845. duk_size_t len;
  34846. DUK_ASSERT(thr != NULL);
  34847. if (x >= 0 && x <= 0x3fL) {
  34848. buf[0] = (duk_uint8_t) (0x80 + x);
  34849. len = 1;
  34850. } else if (x >= 0 && x <= 0x3fffL) {
  34851. buf[0] = (duk_uint8_t) (0xc0 + (x >> 8));
  34852. buf[1] = (duk_uint8_t) (x & 0xff);
  34853. len = 2;
  34854. } else {
  34855. /* Signed integers always map to 4 bytes now. */
  34856. buf[0] = (duk_uint8_t) 0x10;
  34857. buf[1] = (duk_uint8_t) ((x >> 24) & 0xff);
  34858. buf[2] = (duk_uint8_t) ((x >> 16) & 0xff);
  34859. buf[3] = (duk_uint8_t) ((x >> 8) & 0xff);
  34860. buf[4] = (duk_uint8_t) (x & 0xff);
  34861. len = 5;
  34862. }
  34863. duk_debug_write_bytes(thr, buf, len);
  34864. }
  34865. /* Write unsigned 32-bit integer. */
  34866. DUK_INTERNAL void duk_debug_write_uint(duk_hthread *thr, duk_uint32_t x) {
  34867. /* XXX: there's currently no need to support full 32-bit unsigned
  34868. * integer range in practice. If that becomes necessary, add a new
  34869. * dvalue type or encode as an IEEE double.
  34870. */
  34871. duk_debug_write_int(thr, (duk_int32_t) x);
  34872. }
  34873. DUK_INTERNAL void duk_debug_write_strbuf(duk_hthread *thr, const char *data, duk_size_t length, duk_uint8_t marker_base) {
  34874. duk_uint8_t buf[5];
  34875. duk_size_t buflen;
  34876. DUK_ASSERT(thr != NULL);
  34877. DUK_ASSERT(length == 0 || data != NULL);
  34878. if (length <= 0x1fUL && marker_base == 0x11) {
  34879. /* For strings, special form for short lengths. */
  34880. buf[0] = (duk_uint8_t) (0x60 + length);
  34881. buflen = 1;
  34882. } else if (length <= 0xffffUL) {
  34883. buf[0] = (duk_uint8_t) (marker_base + 1);
  34884. buf[1] = (duk_uint8_t) (length >> 8);
  34885. buf[2] = (duk_uint8_t) (length & 0xff);
  34886. buflen = 3;
  34887. } else {
  34888. buf[0] = (duk_uint8_t) marker_base;
  34889. buf[1] = (duk_uint8_t) (length >> 24);
  34890. buf[2] = (duk_uint8_t) ((length >> 16) & 0xff);
  34891. buf[3] = (duk_uint8_t) ((length >> 8) & 0xff);
  34892. buf[4] = (duk_uint8_t) (length & 0xff);
  34893. buflen = 5;
  34894. }
  34895. duk_debug_write_bytes(thr, (const duk_uint8_t *) buf, buflen);
  34896. duk_debug_write_bytes(thr, (const duk_uint8_t *) data, length);
  34897. }
  34898. DUK_INTERNAL void duk_debug_write_string(duk_hthread *thr, const char *data, duk_size_t length) {
  34899. duk_debug_write_strbuf(thr, data, length, 0x11);
  34900. }
  34901. DUK_INTERNAL void duk_debug_write_cstring(duk_hthread *thr, const char *data) {
  34902. DUK_ASSERT(thr != NULL);
  34903. duk_debug_write_string(thr,
  34904. data,
  34905. data ? DUK_STRLEN(data) : 0);
  34906. }
  34907. DUK_INTERNAL void duk_debug_write_hstring(duk_hthread *thr, duk_hstring *h) {
  34908. DUK_ASSERT(thr != NULL);
  34909. /* XXX: differentiate null pointer from empty string? */
  34910. duk_debug_write_string(thr,
  34911. (h != NULL ? (const char *) DUK_HSTRING_GET_DATA(h) : NULL),
  34912. (h != NULL ? (duk_size_t) DUK_HSTRING_GET_BYTELEN(h) : 0));
  34913. }
  34914. DUK_LOCAL void duk__debug_write_hstring_safe_top(duk_hthread *thr) {
  34915. duk_context *ctx = (duk_context *) thr;
  34916. duk_debug_write_hstring(thr, duk_safe_to_hstring(ctx, -1));
  34917. }
  34918. DUK_INTERNAL void duk_debug_write_buffer(duk_hthread *thr, const char *data, duk_size_t length) {
  34919. duk_debug_write_strbuf(thr, data, length, 0x13);
  34920. }
  34921. DUK_INTERNAL void duk_debug_write_hbuffer(duk_hthread *thr, duk_hbuffer *h) {
  34922. DUK_ASSERT(thr != NULL);
  34923. duk_debug_write_buffer(thr,
  34924. (h != NULL ? (const char *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h) : NULL),
  34925. (h != NULL ? (duk_size_t) DUK_HBUFFER_GET_SIZE(h) : 0));
  34926. }
  34927. DUK_LOCAL void duk__debug_write_pointer_raw(duk_hthread *thr, void *ptr, duk_uint8_t ibyte) {
  34928. duk_uint8_t buf[2];
  34929. duk__ptr_union pu;
  34930. DUK_ASSERT(thr != NULL);
  34931. DUK_ASSERT(sizeof(ptr) >= 1 && sizeof(ptr) <= 16);
  34932. /* ptr may be NULL */
  34933. buf[0] = ibyte;
  34934. buf[1] = sizeof(pu);
  34935. duk_debug_write_bytes(thr, buf, 2);
  34936. pu.p = (void *) ptr;
  34937. #if defined(DUK_USE_INTEGER_LE)
  34938. duk_byteswap_bytes((duk_uint8_t *) pu.b, sizeof(pu));
  34939. #endif
  34940. duk_debug_write_bytes(thr, (const duk_uint8_t *) &pu.p, (duk_size_t) sizeof(pu));
  34941. }
  34942. DUK_INTERNAL void duk_debug_write_pointer(duk_hthread *thr, void *ptr) {
  34943. duk__debug_write_pointer_raw(thr, ptr, 0x1c);
  34944. }
  34945. #if defined(DUK_USE_DEBUGGER_DUMPHEAP)
  34946. DUK_INTERNAL void duk_debug_write_heapptr(duk_hthread *thr, duk_heaphdr *h) {
  34947. duk__debug_write_pointer_raw(thr, (void *) h, 0x1e);
  34948. }
  34949. #endif /* DUK_USE_DEBUGGER_DUMPHEAP */
  34950. DUK_INTERNAL void duk_debug_write_hobject(duk_hthread *thr, duk_hobject *obj) {
  34951. duk_uint8_t buf[3];
  34952. duk__ptr_union pu;
  34953. DUK_ASSERT(thr != NULL);
  34954. DUK_ASSERT(sizeof(obj) >= 1 && sizeof(obj) <= 16);
  34955. DUK_ASSERT(obj != NULL);
  34956. buf[0] = 0x1b;
  34957. buf[1] = (duk_uint8_t) DUK_HOBJECT_GET_CLASS_NUMBER(obj);
  34958. buf[2] = sizeof(pu);
  34959. duk_debug_write_bytes(thr, buf, 3);
  34960. pu.p = (void *) obj;
  34961. #if defined(DUK_USE_INTEGER_LE)
  34962. duk_byteswap_bytes((duk_uint8_t *) pu.b, sizeof(pu));
  34963. #endif
  34964. duk_debug_write_bytes(thr, (const duk_uint8_t *) &pu.p, (duk_size_t) sizeof(pu));
  34965. }
  34966. DUK_INTERNAL void duk_debug_write_tval(duk_hthread *thr, duk_tval *tv) {
  34967. duk_c_function lf_func;
  34968. duk_small_uint_t lf_flags;
  34969. duk_uint8_t buf[4];
  34970. duk_double_union du;
  34971. DUK_ASSERT(thr != NULL);
  34972. DUK_ASSERT(tv != NULL);
  34973. switch (DUK_TVAL_GET_TAG(tv)) {
  34974. case DUK_TAG_UNDEFINED:
  34975. duk_debug_write_byte(thr, 0x16);
  34976. break;
  34977. case DUK_TAG_UNUSED:
  34978. duk_debug_write_byte(thr, 0x15);
  34979. break;
  34980. case DUK_TAG_NULL:
  34981. duk_debug_write_byte(thr, 0x17);
  34982. break;
  34983. case DUK_TAG_BOOLEAN:
  34984. DUK_ASSERT(DUK_TVAL_GET_BOOLEAN(tv) == 0 ||
  34985. DUK_TVAL_GET_BOOLEAN(tv) == 1);
  34986. duk_debug_write_byte(thr, DUK_TVAL_GET_BOOLEAN(tv) ? 0x18 : 0x19);
  34987. break;
  34988. case DUK_TAG_POINTER:
  34989. duk_debug_write_pointer(thr, (void *) DUK_TVAL_GET_POINTER(tv));
  34990. break;
  34991. case DUK_TAG_LIGHTFUNC:
  34992. DUK_TVAL_GET_LIGHTFUNC(tv, lf_func, lf_flags);
  34993. buf[0] = 0x1d;
  34994. buf[1] = (duk_uint8_t) (lf_flags >> 8);
  34995. buf[2] = (duk_uint8_t) (lf_flags & 0xff);
  34996. buf[3] = sizeof(lf_func);
  34997. duk_debug_write_bytes(thr, buf, 4);
  34998. duk_debug_write_bytes(thr, (const duk_uint8_t *) &lf_func, sizeof(lf_func));
  34999. break;
  35000. case DUK_TAG_STRING:
  35001. duk_debug_write_hstring(thr, DUK_TVAL_GET_STRING(tv));
  35002. break;
  35003. case DUK_TAG_OBJECT:
  35004. duk_debug_write_hobject(thr, DUK_TVAL_GET_OBJECT(tv));
  35005. break;
  35006. case DUK_TAG_BUFFER:
  35007. duk_debug_write_hbuffer(thr, DUK_TVAL_GET_BUFFER(tv));
  35008. break;
  35009. #if defined(DUK_USE_FASTINT)
  35010. case DUK_TAG_FASTINT:
  35011. #endif
  35012. default:
  35013. /* Numbers are normalized to big (network) endian. */
  35014. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  35015. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  35016. du.d = DUK_TVAL_GET_NUMBER(tv);
  35017. DUK_DBLUNION_DOUBLE_HTON(&du);
  35018. duk_debug_write_byte(thr, 0x1a);
  35019. duk_debug_write_bytes(thr, (const duk_uint8_t *) du.uc, sizeof(du.uc));
  35020. }
  35021. }
  35022. #if defined(DUK_USE_DEBUGGER_DUMPHEAP)
  35023. /* Variant for writing duk_tvals so that any heap allocated values are
  35024. * written out as tagged heap pointers.
  35025. */
  35026. DUK_LOCAL void duk__debug_write_tval_heapptr(duk_hthread *thr, duk_tval *tv) {
  35027. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  35028. duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
  35029. duk_debug_write_heapptr(thr, h);
  35030. } else {
  35031. duk_debug_write_tval(thr, tv);
  35032. }
  35033. }
  35034. #endif /* DUK_USE_DEBUGGER_DUMPHEAP */
  35035. /*
  35036. * Debug connection message write helpers
  35037. */
  35038. #if 0 /* unused */
  35039. DUK_INTERNAL void duk_debug_write_request(duk_hthread *thr, duk_small_uint_t command) {
  35040. duk_debug_write_byte(thr, DUK_DBG_MARKER_REQUEST);
  35041. duk_debug_write_int(thr, command);
  35042. }
  35043. #endif
  35044. DUK_INTERNAL void duk_debug_write_reply(duk_hthread *thr) {
  35045. duk_debug_write_byte(thr, DUK_DBG_MARKER_REPLY);
  35046. }
  35047. DUK_INTERNAL void duk_debug_write_error_eom(duk_hthread *thr, duk_small_uint_t err_code, const char *msg) {
  35048. /* Allow NULL 'msg' */
  35049. duk_debug_write_byte(thr, DUK_DBG_MARKER_ERROR);
  35050. duk_debug_write_int(thr, (duk_int32_t) err_code);
  35051. duk_debug_write_cstring(thr, msg);
  35052. duk_debug_write_eom(thr);
  35053. }
  35054. DUK_INTERNAL void duk_debug_write_notify(duk_hthread *thr, duk_small_uint_t command) {
  35055. duk_debug_write_byte(thr, DUK_DBG_MARKER_NOTIFY);
  35056. duk_debug_write_int(thr, command);
  35057. }
  35058. DUK_INTERNAL void duk_debug_write_eom(duk_hthread *thr) {
  35059. duk_debug_write_byte(thr, DUK_DBG_MARKER_EOM);
  35060. /* As an initial implementation, write flush after every EOM (and the
  35061. * version identifier). A better implementation would flush only when
  35062. * Duktape is finished processing messages so that a flush only happens
  35063. * after all outbound messages are finished on that occasion.
  35064. */
  35065. duk_debug_write_flush(thr);
  35066. }
  35067. /*
  35068. * Status message and helpers
  35069. */
  35070. DUK_INTERNAL duk_uint_fast32_t duk_debug_curr_line(duk_hthread *thr) {
  35071. duk_context *ctx = (duk_context *) thr;
  35072. duk_activation *act;
  35073. duk_uint_fast32_t line;
  35074. duk_uint_fast32_t pc;
  35075. act = duk_hthread_get_current_activation(thr); /* may be NULL */
  35076. if (act == NULL) {
  35077. return 0;
  35078. }
  35079. /* We're conceptually between two opcodes; act->pc indicates the next
  35080. * instruction to be executed. This is usually the correct pc/line to
  35081. * indicate in Status. (For the 'debugger' statement this now reports
  35082. * the pc/line after the debugger statement because the debugger opcode
  35083. * has already been executed.)
  35084. */
  35085. pc = duk_hthread_get_act_curr_pc(thr, act);
  35086. /* XXX: this should be optimized to be a raw query and avoid valstack
  35087. * operations if possible.
  35088. */
  35089. duk_push_tval(ctx, &act->tv_func);
  35090. line = duk_hobject_pc2line_query(ctx, -1, pc);
  35091. duk_pop(ctx);
  35092. return line;
  35093. }
  35094. DUK_INTERNAL void duk_debug_send_status(duk_hthread *thr) {
  35095. duk_context *ctx = (duk_context *) thr;
  35096. duk_activation *act;
  35097. duk_debug_write_notify(thr, DUK_DBG_CMD_STATUS);
  35098. duk_debug_write_int(thr, thr->heap->dbg_paused);
  35099. DUK_ASSERT_DISABLE(thr->callstack_top >= 0); /* unsigned */
  35100. if (thr->callstack_top == 0) {
  35101. duk_debug_write_undefined(thr);
  35102. duk_debug_write_undefined(thr);
  35103. duk_debug_write_int(thr, 0);
  35104. duk_debug_write_int(thr, 0);
  35105. } else {
  35106. act = thr->callstack + thr->callstack_top - 1;
  35107. duk_push_tval(ctx, &act->tv_func);
  35108. duk_get_prop_string(ctx, -1, "fileName");
  35109. duk__debug_write_hstring_safe_top(thr);
  35110. duk_get_prop_string(ctx, -2, "name");
  35111. duk__debug_write_hstring_safe_top(thr);
  35112. duk_pop_3(ctx);
  35113. /* Report next pc/line to be executed. */
  35114. duk_debug_write_uint(thr, (duk_uint32_t) duk_debug_curr_line(thr));
  35115. duk_debug_write_uint(thr, (duk_uint32_t) duk_hthread_get_act_curr_pc(thr, act));
  35116. }
  35117. duk_debug_write_eom(thr);
  35118. }
  35119. #if defined(DUK_USE_DEBUGGER_THROW_NOTIFY)
  35120. DUK_INTERNAL void duk_debug_send_throw(duk_hthread *thr, duk_bool_t fatal) {
  35121. /*
  35122. * NFY <int: 5> <int: fatal> <str: msg> <str: filename> <int: linenumber> EOM
  35123. */
  35124. duk_context *ctx = (duk_context *) thr;
  35125. duk_activation *act;
  35126. duk_uint32_t pc;
  35127. DUK_ASSERT(thr->valstack_top > thr->valstack); /* At least: ... [err] */
  35128. duk_debug_write_notify(thr, DUK_DBG_CMD_THROW);
  35129. duk_debug_write_int(thr, fatal);
  35130. /* Report thrown value to client coerced to string */
  35131. duk_dup(ctx, -1);
  35132. duk__debug_write_hstring_safe_top(thr);
  35133. duk_pop(ctx);
  35134. if (duk_is_error(ctx, -1)) {
  35135. /* Error instance, use augmented error data directly */
  35136. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_FILE_NAME);
  35137. duk__debug_write_hstring_safe_top(thr);
  35138. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_LINE_NUMBER);
  35139. duk_debug_write_uint(thr, duk_get_uint(ctx, -1));
  35140. } else {
  35141. /* For anything other than an Error instance, we calculate the error
  35142. * location directly from the current activation.
  35143. */
  35144. act = thr->callstack + thr->callstack_top - 1;
  35145. duk_push_tval(ctx, &act->tv_func);
  35146. duk_get_prop_string(ctx, -1, "fileName");
  35147. duk__debug_write_hstring_safe_top(thr);
  35148. pc = duk_hthread_get_act_prev_pc(thr, act);
  35149. duk_debug_write_uint(thr, (duk_uint32_t) duk_hobject_pc2line_query(ctx, -2, pc));
  35150. }
  35151. duk_pop_2(ctx); /* shared pop */
  35152. duk_debug_write_eom(thr);
  35153. }
  35154. #endif /* DUK_USE_DEBUGGER_THROW_NOTIFY */
  35155. /*
  35156. * Debug message processing
  35157. */
  35158. /* Skip dvalue. */
  35159. DUK_LOCAL duk_bool_t duk__debug_skip_dvalue(duk_hthread *thr) {
  35160. duk_uint8_t x;
  35161. duk_uint32_t len;
  35162. x = duk_debug_read_byte(thr);
  35163. if (x >= 0xc0) {
  35164. duk_debug_skip_byte(thr);
  35165. return 0;
  35166. }
  35167. if (x >= 0x80) {
  35168. return 0;
  35169. }
  35170. if (x >= 0x60) {
  35171. duk_debug_skip_bytes(thr, x - 0x60);
  35172. return 0;
  35173. }
  35174. switch(x) {
  35175. case 0x00:
  35176. return 1; /* Return 1: got EOM */
  35177. case 0x01:
  35178. case 0x02:
  35179. case 0x03:
  35180. case 0x04:
  35181. break;
  35182. case 0x10:
  35183. (void) duk__debug_read_uint32_raw(thr);
  35184. break;
  35185. case 0x11:
  35186. case 0x13:
  35187. len = duk__debug_read_uint32_raw(thr);
  35188. duk_debug_skip_bytes(thr, len);
  35189. break;
  35190. case 0x12:
  35191. case 0x14:
  35192. len = duk__debug_read_uint16_raw(thr);
  35193. duk_debug_skip_bytes(thr, len);
  35194. break;
  35195. case 0x15:
  35196. case 0x16:
  35197. case 0x17:
  35198. case 0x18:
  35199. case 0x19:
  35200. break;
  35201. case 0x1a:
  35202. duk_debug_skip_bytes(thr, 8);
  35203. break;
  35204. case 0x1b:
  35205. duk_debug_skip_byte(thr);
  35206. len = duk_debug_read_byte(thr);
  35207. duk_debug_skip_bytes(thr, len);
  35208. break;
  35209. case 0x1c:
  35210. len = duk_debug_read_byte(thr);
  35211. duk_debug_skip_bytes(thr, len);
  35212. break;
  35213. case 0x1d:
  35214. duk_debug_skip_bytes(thr, 2);
  35215. len = duk_debug_read_byte(thr);
  35216. duk_debug_skip_bytes(thr, len);
  35217. break;
  35218. default:
  35219. goto fail;
  35220. }
  35221. return 0;
  35222. fail:
  35223. DUK__SET_CONN_BROKEN(thr);
  35224. return 1; /* Pretend like we got EOM */
  35225. }
  35226. /* Skip dvalues to EOM. */
  35227. DUK_LOCAL void duk__debug_skip_to_eom(duk_hthread *thr) {
  35228. for (;;) {
  35229. if (duk__debug_skip_dvalue(thr)) {
  35230. break;
  35231. }
  35232. }
  35233. }
  35234. /*
  35235. * Process incoming debug requests
  35236. *
  35237. * Individual request handlers can push temporaries on the value stack and
  35238. * rely on duk__debug_process_message() to restore the value stack top
  35239. * automatically.
  35240. */
  35241. DUK_LOCAL void duk__debug_handle_basic_info(duk_hthread *thr, duk_heap *heap) {
  35242. DUK_UNREF(heap);
  35243. DUK_D(DUK_DPRINT("debug command Version"));
  35244. duk_debug_write_reply(thr);
  35245. duk_debug_write_int(thr, DUK_VERSION);
  35246. duk_debug_write_cstring(thr, DUK_GIT_DESCRIBE);
  35247. duk_debug_write_cstring(thr, DUK_USE_TARGET_INFO);
  35248. #if defined(DUK_USE_DOUBLE_LE)
  35249. duk_debug_write_int(thr, 1);
  35250. #elif defined(DUK_USE_DOUBLE_ME)
  35251. duk_debug_write_int(thr, 2);
  35252. #elif defined(DUK_USE_DOUBLE_BE)
  35253. duk_debug_write_int(thr, 3);
  35254. #else
  35255. duk_debug_write_int(thr, 0);
  35256. #endif
  35257. duk_debug_write_eom(thr);
  35258. }
  35259. DUK_LOCAL void duk__debug_handle_trigger_status(duk_hthread *thr, duk_heap *heap) {
  35260. DUK_UNREF(heap);
  35261. DUK_D(DUK_DPRINT("debug command TriggerStatus"));
  35262. duk_debug_write_reply(thr);
  35263. duk_debug_write_eom(thr);
  35264. heap->dbg_state_dirty = 1;
  35265. }
  35266. DUK_LOCAL void duk__debug_handle_pause(duk_hthread *thr, duk_heap *heap) {
  35267. DUK_D(DUK_DPRINT("debug command Pause"));
  35268. DUK_HEAP_SET_PAUSED(heap);
  35269. duk_debug_write_reply(thr);
  35270. duk_debug_write_eom(thr);
  35271. }
  35272. DUK_LOCAL void duk__debug_handle_resume(duk_hthread *thr, duk_heap *heap) {
  35273. DUK_D(DUK_DPRINT("debug command Resume"));
  35274. DUK_HEAP_CLEAR_PAUSED(heap);
  35275. duk_debug_write_reply(thr);
  35276. duk_debug_write_eom(thr);
  35277. }
  35278. DUK_LOCAL void duk__debug_handle_step(duk_hthread *thr, duk_heap *heap, duk_int32_t cmd) {
  35279. duk_small_uint_t step_type;
  35280. duk_uint_fast32_t line;
  35281. if (cmd == DUK_DBG_CMD_STEPINTO) {
  35282. step_type = DUK_STEP_TYPE_INTO;
  35283. } else if (cmd == DUK_DBG_CMD_STEPOVER) {
  35284. step_type = DUK_STEP_TYPE_OVER;
  35285. } else {
  35286. DUK_ASSERT(cmd == DUK_DBG_CMD_STEPOUT);
  35287. step_type = DUK_STEP_TYPE_OUT;
  35288. }
  35289. DUK_D(DUK_DPRINT("debug command StepInto/StepOver/StepOut: %d", (int) cmd));
  35290. line = duk_debug_curr_line(thr);
  35291. if (line > 0) {
  35292. heap->dbg_paused = 0;
  35293. heap->dbg_step_type = step_type;
  35294. heap->dbg_step_thread = thr;
  35295. heap->dbg_step_csindex = thr->callstack_top - 1;
  35296. heap->dbg_step_startline = line;
  35297. heap->dbg_state_dirty = 1;
  35298. } else {
  35299. DUK_D(DUK_DPRINT("cannot determine current line, stepinto/stepover/stepout ignored"));
  35300. }
  35301. duk_debug_write_reply(thr);
  35302. duk_debug_write_eom(thr);
  35303. }
  35304. DUK_LOCAL void duk__debug_handle_list_break(duk_hthread *thr, duk_heap *heap) {
  35305. duk_small_int_t i;
  35306. DUK_D(DUK_DPRINT("debug command ListBreak"));
  35307. duk_debug_write_reply(thr);
  35308. for (i = 0; i < (duk_small_int_t) heap->dbg_breakpoint_count; i++) {
  35309. duk_debug_write_hstring(thr, heap->dbg_breakpoints[i].filename);
  35310. duk_debug_write_uint(thr, (duk_uint32_t) heap->dbg_breakpoints[i].line);
  35311. }
  35312. duk_debug_write_eom(thr);
  35313. }
  35314. DUK_LOCAL void duk__debug_handle_add_break(duk_hthread *thr, duk_heap *heap) {
  35315. duk_hstring *filename;
  35316. duk_uint32_t linenumber;
  35317. duk_small_int_t idx;
  35318. DUK_UNREF(heap);
  35319. filename = duk_debug_read_hstring(thr);
  35320. linenumber = (duk_uint32_t) duk_debug_read_int(thr);
  35321. DUK_D(DUK_DPRINT("debug command AddBreak: %!O:%ld", (duk_hobject *) filename, (long) linenumber));
  35322. idx = duk_debug_add_breakpoint(thr, filename, linenumber);
  35323. if (idx >= 0) {
  35324. duk_debug_write_reply(thr);
  35325. duk_debug_write_int(thr, (duk_int32_t) idx);
  35326. duk_debug_write_eom(thr);
  35327. } else {
  35328. duk_debug_write_error_eom(thr, DUK_DBG_ERR_TOOMANY, "no space for breakpoint");
  35329. }
  35330. }
  35331. DUK_LOCAL void duk__debug_handle_del_break(duk_hthread *thr, duk_heap *heap) {
  35332. duk_small_uint_t idx;
  35333. DUK_UNREF(heap);
  35334. DUK_D(DUK_DPRINT("debug command DelBreak"));
  35335. idx = (duk_small_uint_t) duk_debug_read_int(thr);
  35336. if (duk_debug_remove_breakpoint(thr, idx)) {
  35337. duk_debug_write_reply(thr);
  35338. duk_debug_write_eom(thr);
  35339. } else {
  35340. duk_debug_write_error_eom(thr, DUK_DBG_ERR_NOTFOUND, "invalid breakpoint index");
  35341. }
  35342. }
  35343. DUK_LOCAL void duk__debug_handle_get_var(duk_hthread *thr, duk_heap *heap) {
  35344. duk_context *ctx = (duk_context *) thr;
  35345. duk_hstring *str;
  35346. duk_bool_t rc;
  35347. duk_int32_t level;
  35348. DUK_UNREF(heap);
  35349. DUK_D(DUK_DPRINT("debug command GetVar"));
  35350. str = duk_debug_read_hstring(thr); /* push to stack */
  35351. DUK_ASSERT(str != NULL);
  35352. if (duk_debug_peek_byte(thr) != DUK_DBG_MARKER_EOM) {
  35353. level = duk_debug_read_int(thr); /* optional callstack level */
  35354. if (level >= 0 || -level > (duk_int32_t) thr->callstack_top) {
  35355. DUK_D(DUK_DPRINT("invalid callstack level for GetVar"));
  35356. duk_debug_write_error_eom(thr, DUK_DBG_ERR_NOTFOUND, "invalid callstack level");
  35357. return;
  35358. }
  35359. } else {
  35360. level = -1;
  35361. }
  35362. if (thr->callstack_top > 0) {
  35363. rc = duk_js_getvar_activation(thr,
  35364. thr->callstack + thr->callstack_top + level,
  35365. str,
  35366. 0);
  35367. } else {
  35368. /* No activation, no variable access. Could also pretend
  35369. * we're in the global program context and read stuff off
  35370. * the global object.
  35371. */
  35372. DUK_D(DUK_DPRINT("callstack empty, no activation -> ignore getvar"));
  35373. rc = 0;
  35374. }
  35375. duk_debug_write_reply(thr);
  35376. if (rc) {
  35377. duk_debug_write_int(thr, 1);
  35378. DUK_ASSERT(duk_get_tval(ctx, -2) != NULL);
  35379. duk_debug_write_tval(thr, duk_get_tval(ctx, -2));
  35380. } else {
  35381. duk_debug_write_int(thr, 0);
  35382. duk_debug_write_unused(thr);
  35383. }
  35384. duk_debug_write_eom(thr);
  35385. }
  35386. DUK_LOCAL void duk__debug_handle_put_var(duk_hthread *thr, duk_heap *heap) {
  35387. duk_context *ctx = (duk_context *) thr;
  35388. duk_hstring *str;
  35389. duk_tval *tv;
  35390. duk_int32_t level;
  35391. DUK_UNREF(heap);
  35392. DUK_D(DUK_DPRINT("debug command PutVar"));
  35393. str = duk_debug_read_hstring(thr); /* push to stack */
  35394. DUK_ASSERT(str != NULL);
  35395. duk_debug_read_tval(thr); /* push to stack */
  35396. tv = duk_get_tval(ctx, -1);
  35397. DUK_ASSERT(tv != NULL);
  35398. if (duk_debug_peek_byte(thr) != DUK_DBG_MARKER_EOM) {
  35399. level = duk_debug_read_int(thr); /* optional callstack level */
  35400. if (level >= 0 || -level > (duk_int32_t) thr->callstack_top) {
  35401. DUK_D(DUK_DPRINT("invalid callstack level for PutVar"));
  35402. duk_debug_write_error_eom(thr, DUK_DBG_ERR_NOTFOUND, "invalid callstack level");
  35403. return;
  35404. }
  35405. } else {
  35406. level = -1;
  35407. }
  35408. if (thr->callstack_top > 0) {
  35409. duk_js_putvar_activation(thr,
  35410. thr->callstack + thr->callstack_top + level,
  35411. str,
  35412. tv,
  35413. 0);
  35414. } else {
  35415. DUK_D(DUK_DPRINT("callstack empty, no activation -> ignore putvar"));
  35416. }
  35417. /* XXX: Current putvar implementation doesn't have a success flag,
  35418. * add one and send to debug client?
  35419. */
  35420. duk_debug_write_reply(thr);
  35421. duk_debug_write_eom(thr);
  35422. }
  35423. DUK_LOCAL void duk__debug_handle_get_call_stack(duk_hthread *thr, duk_heap *heap) {
  35424. duk_context *ctx = (duk_context *) thr;
  35425. duk_hthread *curr_thr = thr;
  35426. duk_activation *curr_act;
  35427. duk_uint_fast32_t pc;
  35428. duk_uint_fast32_t line;
  35429. duk_size_t i;
  35430. DUK_UNREF(heap);
  35431. duk_debug_write_reply(thr);
  35432. while (curr_thr != NULL) {
  35433. i = curr_thr->callstack_top;
  35434. while (i > 0) {
  35435. i--;
  35436. curr_act = curr_thr->callstack + i;
  35437. /* PC/line semantics here are:
  35438. * - For callstack top we're conceptually between two
  35439. * opcodes and current PC indicates next line to
  35440. * execute, so report that (matches Status).
  35441. * - For other activations we're conceptually still
  35442. * executing the instruction at PC-1, so report that
  35443. * (matches error stacktrace behavior).
  35444. * - See: https://github.com/svaarala/duktape/issues/281
  35445. */
  35446. /* XXX: optimize to use direct reads, i.e. avoid
  35447. * value stack operations.
  35448. */
  35449. duk_push_tval(ctx, &curr_act->tv_func);
  35450. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_FILE_NAME);
  35451. duk__debug_write_hstring_safe_top(thr);
  35452. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_NAME);
  35453. duk__debug_write_hstring_safe_top(thr);
  35454. pc = duk_hthread_get_act_curr_pc(thr, curr_act);
  35455. if (i != curr_thr->callstack_top - 1 && pc > 0) {
  35456. pc--;
  35457. }
  35458. line = duk_hobject_pc2line_query(ctx, -3, pc);
  35459. duk_debug_write_uint(thr, (duk_uint32_t) line);
  35460. duk_debug_write_uint(thr, (duk_uint32_t) pc);
  35461. duk_pop_3(ctx);
  35462. }
  35463. curr_thr = curr_thr->resumer;
  35464. }
  35465. duk_debug_write_eom(thr);
  35466. }
  35467. DUK_LOCAL void duk__debug_handle_get_locals(duk_hthread *thr, duk_heap *heap) {
  35468. duk_context *ctx = (duk_context *) thr;
  35469. duk_activation *curr_act;
  35470. duk_int32_t level;
  35471. duk_hstring *varname;
  35472. DUK_UNREF(heap);
  35473. if (duk_debug_peek_byte(thr) != DUK_DBG_MARKER_EOM) {
  35474. level = duk_debug_read_int(thr); /* optional callstack level */
  35475. if (level >= 0 || -level > (duk_int32_t) thr->callstack_top) {
  35476. DUK_D(DUK_DPRINT("invalid callstack level for GetLocals"));
  35477. duk_debug_write_error_eom(thr, DUK_DBG_ERR_NOTFOUND, "invalid callstack level");
  35478. return;
  35479. }
  35480. duk_debug_write_reply(thr);
  35481. } else {
  35482. duk_debug_write_reply(thr);
  35483. if (thr->callstack_top == 0) {
  35484. goto callstack_empty;
  35485. }
  35486. level = -1;
  35487. }
  35488. curr_act = thr->callstack + thr->callstack_top + level;
  35489. /* XXX: several nice-to-have improvements here:
  35490. * - Use direct reads avoiding value stack operations
  35491. * - Avoid triggering getters, indicate getter values to debug client
  35492. * - If side effects are possible, add error catching
  35493. */
  35494. duk_push_tval(ctx, &curr_act->tv_func);
  35495. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VARMAP);
  35496. if (duk_is_object(ctx, -1)) {
  35497. duk_enum(ctx, -1, 0 /*enum_flags*/);
  35498. while (duk_next(ctx, -1 /*enum_index*/, 0 /*get_value*/)) {
  35499. varname = duk_get_hstring(ctx, -1);
  35500. DUK_ASSERT(varname != NULL);
  35501. duk_js_getvar_activation(thr, curr_act, varname, 0 /*throw_flag*/);
  35502. /* [ ... func varmap enum key value this ] */
  35503. duk_debug_write_hstring(thr, duk_get_hstring(ctx, -3));
  35504. duk_debug_write_tval(thr, duk_get_tval(ctx, -2));
  35505. duk_pop_3(ctx); /* -> [ ... func varmap enum ] */
  35506. }
  35507. } else {
  35508. DUK_D(DUK_DPRINT("varmap is not an object in GetLocals, ignore"));
  35509. }
  35510. callstack_empty:
  35511. duk_debug_write_eom(thr);
  35512. }
  35513. DUK_LOCAL void duk__debug_handle_eval(duk_hthread *thr, duk_heap *heap) {
  35514. duk_context *ctx = (duk_context *) thr;
  35515. duk_small_uint_t call_flags;
  35516. duk_int_t call_ret;
  35517. duk_small_int_t eval_err;
  35518. duk_int32_t level;
  35519. DUK_UNREF(heap);
  35520. DUK_D(DUK_DPRINT("debug command Eval"));
  35521. /* The eval code is executed within the lexical environment of a specified
  35522. * activation. For now, use global object eval() function, with the eval
  35523. * considered a 'direct call to eval'.
  35524. *
  35525. * Callstack level for debug commands only affects scope--the callstack as
  35526. * seen by, e.g. Duktape.act() will be the same regardless.
  35527. */
  35528. /* nargs == 2 so we can pass a callstack level to eval(). */
  35529. duk_push_c_function(ctx, duk_bi_global_object_eval, 2 /*nargs*/);
  35530. duk_push_undefined(ctx); /* 'this' binding shouldn't matter here */
  35531. (void) duk_debug_read_hstring(thr);
  35532. if (duk_debug_peek_byte(thr) != DUK_DBG_MARKER_EOM) {
  35533. level = duk_debug_read_int(thr); /* optional callstack level */
  35534. if (level >= 0 || -level > (duk_int32_t)thr->callstack_top) {
  35535. DUK_D(DUK_DPRINT("invalid callstack level for Eval"));
  35536. duk_debug_write_error_eom(thr, DUK_DBG_ERR_NOTFOUND, "invalid callstack level");
  35537. return;
  35538. }
  35539. }
  35540. else {
  35541. level = -1;
  35542. }
  35543. DUK_ASSERT(level < 0 && -level <= (duk_int32_t) thr->callstack_top);
  35544. duk_push_int(ctx, level - 1); /* compensate for eval() call */
  35545. /* [ ... eval "eval" eval_input level ] */
  35546. call_flags = DUK_CALL_FLAG_PROTECTED;
  35547. if (thr->callstack_top >= (duk_size_t) -level) {
  35548. duk_activation *act;
  35549. duk_hobject *fun;
  35550. act = thr->callstack + thr->callstack_top + level;
  35551. fun = DUK_ACT_GET_FUNC(act);
  35552. if (fun != NULL && DUK_HOBJECT_IS_COMPILEDFUNCTION(fun)) {
  35553. /* Direct eval requires that there's a current
  35554. * activation and it is an Ecmascript function.
  35555. * When Eval is executed from e.g. cooperate API
  35556. * call we'll need to an indirect eval instead.
  35557. */
  35558. call_flags |= DUK_CALL_FLAG_DIRECT_EVAL;
  35559. }
  35560. }
  35561. call_ret = duk_handle_call(thr, 2 /*num_stack_args*/, call_flags);
  35562. if (call_ret == DUK_EXEC_SUCCESS) {
  35563. eval_err = 0;
  35564. /* Use result value as is. */
  35565. } else {
  35566. /* For errors a string coerced result is most informative
  35567. * right now, as the debug client doesn't have the capability
  35568. * to traverse the error object.
  35569. */
  35570. eval_err = 1;
  35571. duk_safe_to_string(ctx, -1);
  35572. }
  35573. /* [ ... result ] */
  35574. duk_debug_write_reply(thr);
  35575. duk_debug_write_int(thr, (duk_int32_t) eval_err);
  35576. DUK_ASSERT(duk_get_tval(ctx, -1) != NULL);
  35577. duk_debug_write_tval(thr, duk_get_tval(ctx, -1));
  35578. duk_debug_write_eom(thr);
  35579. }
  35580. DUK_LOCAL void duk__debug_handle_detach(duk_hthread *thr, duk_heap *heap) {
  35581. DUK_UNREF(heap);
  35582. DUK_D(DUK_DPRINT("debug command Detach"));
  35583. duk_debug_write_reply(thr);
  35584. duk_debug_write_eom(thr);
  35585. DUK_D(DUK_DPRINT("debug connection detached, mark broken"));
  35586. DUK__SET_CONN_BROKEN(thr);
  35587. }
  35588. #if defined(DUK_USE_DEBUGGER_DUMPHEAP)
  35589. DUK_LOCAL void duk__debug_dump_heaphdr(duk_hthread *thr, duk_heap *heap, duk_heaphdr *hdr) {
  35590. DUK_UNREF(heap);
  35591. duk_debug_write_heapptr(thr, hdr);
  35592. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HEAPHDR_GET_TYPE(hdr));
  35593. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HEAPHDR_GET_FLAGS_RAW(hdr));
  35594. #if defined(DUK_USE_REFERENCE_COUNTING)
  35595. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HEAPHDR_GET_REFCOUNT(hdr));
  35596. #else
  35597. duk_debug_write_int(thr, (duk_int32_t) -1);
  35598. #endif
  35599. switch (DUK_HEAPHDR_GET_TYPE(hdr)) {
  35600. case DUK_HTYPE_STRING: {
  35601. duk_hstring *h = (duk_hstring *) hdr;
  35602. duk_debug_write_uint(thr, (duk_int32_t) DUK_HSTRING_GET_BYTELEN(h));
  35603. duk_debug_write_uint(thr, (duk_int32_t) DUK_HSTRING_GET_CHARLEN(h));
  35604. duk_debug_write_uint(thr, (duk_int32_t) DUK_HSTRING_GET_HASH(h));
  35605. duk_debug_write_hstring(thr, h);
  35606. break;
  35607. }
  35608. case DUK_HTYPE_OBJECT: {
  35609. duk_hobject *h = (duk_hobject *) hdr;
  35610. duk_hstring *k;
  35611. duk_uint_fast32_t i;
  35612. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_GET_CLASS_NUMBER(h));
  35613. duk_debug_write_heapptr(thr, (duk_heaphdr *) DUK_HOBJECT_GET_PROTOTYPE(heap, h));
  35614. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_GET_ESIZE(h));
  35615. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_GET_ENEXT(h));
  35616. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_GET_ASIZE(h));
  35617. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_GET_HSIZE(h));
  35618. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(h); i++) {
  35619. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_E_GET_FLAGS(heap, h, i));
  35620. k = DUK_HOBJECT_E_GET_KEY(heap, h, i);
  35621. duk_debug_write_heapptr(thr, (duk_heaphdr *) k);
  35622. if (k == NULL) {
  35623. duk_debug_write_int(thr, 0); /* isAccessor */
  35624. duk_debug_write_unused(thr);
  35625. continue;
  35626. }
  35627. if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap, h, i)) {
  35628. duk_debug_write_int(thr, 1); /* isAccessor */
  35629. duk_debug_write_heapptr(thr, (duk_heaphdr *) DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->a.get);
  35630. duk_debug_write_heapptr(thr, (duk_heaphdr *) DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->a.set);
  35631. } else {
  35632. duk_debug_write_int(thr, 0); /* isAccessor */
  35633. duk__debug_write_tval_heapptr(thr, &DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->v);
  35634. }
  35635. }
  35636. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(h); i++) {
  35637. /* Note: array dump will include elements beyond
  35638. * 'length'.
  35639. */
  35640. duk__debug_write_tval_heapptr(thr, DUK_HOBJECT_A_GET_VALUE_PTR(heap, h, i));
  35641. }
  35642. break;
  35643. }
  35644. case DUK_HTYPE_BUFFER: {
  35645. duk_hbuffer *h = (duk_hbuffer *) hdr;
  35646. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HBUFFER_GET_SIZE(h));
  35647. duk_debug_write_buffer(thr, (const char *) DUK_HBUFFER_GET_DATA_PTR(heap, h), (duk_size_t) DUK_HBUFFER_GET_SIZE(h));
  35648. break;
  35649. }
  35650. default: {
  35651. DUK_D(DUK_DPRINT("invalid htype: %d", (int) DUK_HEAPHDR_GET_TYPE(hdr)));
  35652. }
  35653. }
  35654. }
  35655. DUK_LOCAL void duk__debug_dump_heap_allocated(duk_hthread *thr, duk_heap *heap) {
  35656. duk_heaphdr *hdr;
  35657. hdr = heap->heap_allocated;
  35658. while (hdr != NULL) {
  35659. duk__debug_dump_heaphdr(thr, heap, hdr);
  35660. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  35661. }
  35662. }
  35663. #if defined(DUK_USE_STRTAB_CHAIN)
  35664. DUK_LOCAL void duk__debug_dump_strtab_chain(duk_hthread *thr, duk_heap *heap) {
  35665. duk_uint_fast32_t i, j;
  35666. duk_strtab_entry *e;
  35667. #if defined(DUK_USE_HEAPPTR16)
  35668. duk_uint16_t *lst;
  35669. #else
  35670. duk_hstring **lst;
  35671. #endif
  35672. duk_hstring *h;
  35673. for (i = 0; i < DUK_STRTAB_CHAIN_SIZE; i++) {
  35674. e = heap->strtable + i;
  35675. if (e->listlen > 0) {
  35676. #if defined(DUK_USE_HEAPPTR16)
  35677. lst = (duk_uint16_t *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.strlist16);
  35678. #else
  35679. lst = e->u.strlist;
  35680. #endif
  35681. DUK_ASSERT(lst != NULL);
  35682. for (j = 0; j < e->listlen; j++) {
  35683. #if defined(DUK_USE_HEAPPTR16)
  35684. h = DUK_USE_HEAPPTR_DEC16(heap->heap_udata, lst[j]);
  35685. #else
  35686. h = lst[j];
  35687. #endif
  35688. if (h != NULL) {
  35689. duk__debug_dump_heaphdr(thr, heap, (duk_heaphdr *) h);
  35690. }
  35691. }
  35692. } else {
  35693. #if defined(DUK_USE_HEAPPTR16)
  35694. h = DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.str16);
  35695. #else
  35696. h = e->u.str;
  35697. #endif
  35698. if (h != NULL) {
  35699. duk__debug_dump_heaphdr(thr, heap, (duk_heaphdr *) h);
  35700. }
  35701. }
  35702. }
  35703. }
  35704. #endif /* DUK_USE_STRTAB_CHAIN */
  35705. #if defined(DUK_USE_STRTAB_PROBE)
  35706. DUK_LOCAL void duk__debug_dump_strtab_probe(duk_hthread *thr, duk_heap *heap) {
  35707. duk_uint32_t i;
  35708. duk_hstring *h;
  35709. for (i = 0; i < heap->st_size; i++) {
  35710. #if defined(DUK_USE_HEAPPTR16)
  35711. h = DUK_USE_HEAPPTR_DEC16(heap->strtable16[i]);
  35712. #else
  35713. h = heap->strtable[i];
  35714. #endif
  35715. if (h == NULL || h == DUK_STRTAB_DELETED_MARKER(heap)) {
  35716. continue;
  35717. }
  35718. duk__debug_dump_heaphdr(thr, heap, (duk_heaphdr *) h);
  35719. }
  35720. }
  35721. #endif /* DUK_USE_STRTAB_PROBE */
  35722. DUK_LOCAL void duk__debug_handle_dump_heap(duk_hthread *thr, duk_heap *heap) {
  35723. DUK_D(DUK_DPRINT("debug command DumpHeap"));
  35724. duk_debug_write_reply(thr);
  35725. duk__debug_dump_heap_allocated(thr, heap);
  35726. #if defined(DUK_USE_STRTAB_CHAIN)
  35727. duk__debug_dump_strtab_chain(thr, heap);
  35728. #endif
  35729. #if defined(DUK_USE_STRTAB_PROBE)
  35730. duk__debug_dump_strtab_probe(thr, heap);
  35731. #endif
  35732. duk_debug_write_eom(thr);
  35733. }
  35734. #endif /* DUK_USE_DEBUGGER_DUMPHEAP */
  35735. DUK_LOCAL void duk__debug_handle_get_bytecode(duk_hthread *thr, duk_heap *heap) {
  35736. duk_activation *act;
  35737. duk_hcompiledfunction *fun;
  35738. duk_size_t i, n;
  35739. duk_tval *tv;
  35740. duk_hobject **fn;
  35741. DUK_UNREF(heap);
  35742. DUK_D(DUK_DPRINT("debug command GetBytecode"));
  35743. duk_debug_write_reply(thr);
  35744. if (thr->callstack_top == 0) {
  35745. fun = NULL;
  35746. } else {
  35747. act = thr->callstack + thr->callstack_top - 1;
  35748. fun = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act);
  35749. if (!DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) fun)) {
  35750. fun = NULL;
  35751. }
  35752. }
  35753. DUK_ASSERT(fun == NULL || DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) fun));
  35754. if (fun != NULL) {
  35755. n = DUK_HCOMPILEDFUNCTION_GET_CONSTS_COUNT(heap, fun);
  35756. duk_debug_write_int(thr, (duk_int32_t) n);
  35757. tv = DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(heap, fun);
  35758. for (i = 0; i < n; i++) {
  35759. duk_debug_write_tval(thr, tv);
  35760. tv++;
  35761. }
  35762. n = DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(heap, fun);
  35763. duk_debug_write_int(thr, (duk_int32_t) n);
  35764. fn = DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(heap, fun);
  35765. for (i = 0; i < n; i++) {
  35766. duk_debug_write_hobject(thr, *fn);
  35767. fn++;
  35768. }
  35769. duk_debug_write_string(thr,
  35770. (const char *) DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(heap, fun),
  35771. (duk_size_t) DUK_HCOMPILEDFUNCTION_GET_CODE_SIZE(heap, fun));
  35772. } else {
  35773. duk_debug_write_int(thr, 0);
  35774. duk_debug_write_int(thr, 0);
  35775. duk_debug_write_cstring(thr, "");
  35776. }
  35777. duk_debug_write_eom(thr);
  35778. }
  35779. /* Process one debug message. Automatically restore value stack top to its
  35780. * entry value, so that individual message handlers don't need exact value
  35781. * stack handling which is convenient.
  35782. */
  35783. DUK_LOCAL void duk__debug_process_message(duk_hthread *thr) {
  35784. duk_context *ctx = (duk_context *) thr;
  35785. duk_heap *heap;
  35786. duk_uint8_t x;
  35787. duk_int32_t cmd;
  35788. duk_idx_t entry_top;
  35789. DUK_ASSERT(thr != NULL);
  35790. heap = thr->heap;
  35791. DUK_ASSERT(heap != NULL);
  35792. DUK_UNREF(ctx);
  35793. entry_top = duk_get_top(ctx);
  35794. x = duk_debug_read_byte(thr);
  35795. switch (x) {
  35796. case DUK_DBG_MARKER_REQUEST: {
  35797. cmd = duk_debug_read_int(thr);
  35798. switch (cmd) {
  35799. case DUK_DBG_CMD_BASICINFO: {
  35800. duk__debug_handle_basic_info(thr, heap);
  35801. break;
  35802. }
  35803. case DUK_DBG_CMD_TRIGGERSTATUS: {
  35804. duk__debug_handle_trigger_status(thr, heap);
  35805. break;
  35806. }
  35807. case DUK_DBG_CMD_PAUSE: {
  35808. duk__debug_handle_pause(thr, heap);
  35809. break;
  35810. }
  35811. case DUK_DBG_CMD_RESUME: {
  35812. duk__debug_handle_resume(thr, heap);
  35813. break;
  35814. }
  35815. case DUK_DBG_CMD_STEPINTO:
  35816. case DUK_DBG_CMD_STEPOVER:
  35817. case DUK_DBG_CMD_STEPOUT: {
  35818. duk__debug_handle_step(thr, heap, cmd);
  35819. break;
  35820. }
  35821. case DUK_DBG_CMD_LISTBREAK: {
  35822. duk__debug_handle_list_break(thr, heap);
  35823. break;
  35824. }
  35825. case DUK_DBG_CMD_ADDBREAK: {
  35826. duk__debug_handle_add_break(thr, heap);
  35827. break;
  35828. }
  35829. case DUK_DBG_CMD_DELBREAK: {
  35830. duk__debug_handle_del_break(thr, heap);
  35831. break;
  35832. }
  35833. case DUK_DBG_CMD_GETVAR: {
  35834. duk__debug_handle_get_var(thr, heap);
  35835. break;
  35836. }
  35837. case DUK_DBG_CMD_PUTVAR: {
  35838. duk__debug_handle_put_var(thr, heap);
  35839. break;
  35840. }
  35841. case DUK_DBG_CMD_GETCALLSTACK: {
  35842. duk__debug_handle_get_call_stack(thr, heap);
  35843. break;
  35844. }
  35845. case DUK_DBG_CMD_GETLOCALS: {
  35846. duk__debug_handle_get_locals(thr, heap);
  35847. break;
  35848. }
  35849. case DUK_DBG_CMD_EVAL: {
  35850. duk__debug_handle_eval(thr, heap);
  35851. break;
  35852. }
  35853. case DUK_DBG_CMD_DETACH: {
  35854. /* The actual detached_cb call is postponed to message loop so
  35855. * we don't need any special precautions here (just skip to EOM
  35856. * on the already closed connection).
  35857. */
  35858. duk__debug_handle_detach(thr, heap);
  35859. break;
  35860. }
  35861. #if defined(DUK_USE_DEBUGGER_DUMPHEAP)
  35862. case DUK_DBG_CMD_DUMPHEAP: {
  35863. duk__debug_handle_dump_heap(thr, heap);
  35864. break;
  35865. }
  35866. #endif /* DUK_USE_DEBUGGER_DUMPHEAP */
  35867. case DUK_DBG_CMD_GETBYTECODE: {
  35868. duk__debug_handle_get_bytecode(thr, heap);
  35869. break;
  35870. }
  35871. default: {
  35872. DUK_D(DUK_DPRINT("debug command unsupported: %d", (int) cmd));
  35873. duk_debug_write_error_eom(thr, DUK_DBG_ERR_UNSUPPORTED, "unsupported command");
  35874. }
  35875. } /* switch cmd */
  35876. break;
  35877. }
  35878. case DUK_DBG_MARKER_REPLY: {
  35879. DUK_D(DUK_DPRINT("debug reply, skipping"));
  35880. break;
  35881. }
  35882. case DUK_DBG_MARKER_ERROR: {
  35883. DUK_D(DUK_DPRINT("debug error, skipping"));
  35884. break;
  35885. }
  35886. case DUK_DBG_MARKER_NOTIFY: {
  35887. DUK_D(DUK_DPRINT("debug notify, skipping"));
  35888. break;
  35889. }
  35890. default: {
  35891. DUK_D(DUK_DPRINT("invalid initial byte, drop connection: %d", (int) x));
  35892. goto fail;
  35893. }
  35894. } /* switch initial byte */
  35895. DUK_ASSERT(duk_get_top(ctx) >= entry_top);
  35896. duk_set_top(ctx, entry_top);
  35897. duk__debug_skip_to_eom(thr);
  35898. return;
  35899. fail:
  35900. DUK_ASSERT(duk_get_top(ctx) >= entry_top);
  35901. duk_set_top(ctx, entry_top);
  35902. DUK__SET_CONN_BROKEN(thr);
  35903. return;
  35904. }
  35905. /* Halt execution and enter a debugger message loop until execution is resumed
  35906. * by the client. PC for the current activation may be temporarily decremented
  35907. * so that the "current" instruction will be shown by the client. This helper
  35908. * is callable from anywhere, also outside bytecode executor.
  35909. */
  35910. DUK_INTERNAL void duk_debug_halt_execution(duk_hthread *thr, duk_bool_t use_prev_pc) {
  35911. duk_activation *act;
  35912. duk_hcompiledfunction *fun;
  35913. duk_instr_t *old_pc = NULL;
  35914. DUK_ASSERT(thr != NULL);
  35915. DUK_ASSERT(thr->heap != NULL);
  35916. DUK_ASSERT(DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap));
  35917. DUK_ASSERT(thr->heap->dbg_processing == 0);
  35918. DUK_HEAP_SET_PAUSED(thr->heap);
  35919. act = duk_hthread_get_current_activation(thr);
  35920. /* NOTE: act may be NULL if an error is thrown outside of any activation,
  35921. * which may happen in the case of, e.g. syntax errors.
  35922. */
  35923. /* Decrement PC if that was requested, this requires a PC sync. */
  35924. if (act != NULL) {
  35925. duk_hthread_sync_currpc(thr);
  35926. old_pc = act->curr_pc;
  35927. fun = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act);
  35928. /* Short circuit if is safe: if act->curr_pc != NULL, 'fun' is
  35929. * guaranteed to be a non-NULL Ecmascript function.
  35930. */
  35931. DUK_ASSERT(act->curr_pc == NULL ||
  35932. (fun != NULL && DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) fun)));
  35933. if (use_prev_pc &&
  35934. act->curr_pc != NULL &&
  35935. act->curr_pc > DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, fun)) {
  35936. act->curr_pc--;
  35937. }
  35938. }
  35939. /* Process debug messages until we are no longer paused. */
  35940. /* NOTE: This is a bit fragile. It's important to ensure that neither
  35941. * duk_debug_send_status() or duk_debug_process_messages() throws an
  35942. * error or act->curr_pc will never be reset.
  35943. */
  35944. thr->heap->dbg_processing = 1;
  35945. duk_debug_send_status(thr);
  35946. while (thr->heap->dbg_paused) {
  35947. DUK_ASSERT(thr->heap->dbg_processing);
  35948. duk_debug_process_messages(thr, 0 /*no_block*/);
  35949. }
  35950. thr->heap->dbg_processing = 0;
  35951. /* XXX: Decrementing and restoring act->curr_pc works now, but if the
  35952. * debugger message loop gains the ability to adjust the current PC
  35953. * (e.g. a forced jump) restoring the PC here will break. Another
  35954. * approach would be to use a state flag for the "decrement 1 from
  35955. * topmost activation's PC" and take it into account whenever dealing
  35956. * with PC values.
  35957. */
  35958. if (act != NULL) {
  35959. act->curr_pc = old_pc; /* restore PC */
  35960. }
  35961. }
  35962. DUK_INTERNAL duk_bool_t duk_debug_process_messages(duk_hthread *thr, duk_bool_t no_block) {
  35963. duk_context *ctx = (duk_context *) thr;
  35964. #if defined(DUK_USE_ASSERTIONS)
  35965. duk_idx_t entry_top;
  35966. #endif
  35967. duk_bool_t retval = 0;
  35968. DUK_ASSERT(thr != NULL);
  35969. DUK_UNREF(ctx);
  35970. DUK_ASSERT(thr->heap != NULL);
  35971. DUK_ASSERT(DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap));
  35972. #if defined(DUK_USE_ASSERTIONS)
  35973. entry_top = duk_get_top(ctx);
  35974. #endif
  35975. DUK_DD(DUK_DDPRINT("top at entry: %ld", (long) duk_get_top(ctx)));
  35976. for (;;) {
  35977. /* Process messages until we're no longer paused or we peek
  35978. * and see there's nothing to read right now.
  35979. */
  35980. DUK_DD(DUK_DDPRINT("top at loop top: %ld", (long) duk_get_top(ctx)));
  35981. if (thr->heap->dbg_read_cb == NULL) {
  35982. DUK_D(DUK_DPRINT("debug connection broken, stop processing messages"));
  35983. break;
  35984. } else if (!thr->heap->dbg_paused || no_block) {
  35985. if (!duk_debug_read_peek(thr)) {
  35986. DUK_D(DUK_DPRINT("processing debug message, peek indicated no data, stop processing"));
  35987. break;
  35988. }
  35989. DUK_D(DUK_DPRINT("processing debug message, peek indicated there is data, handle it"));
  35990. } else {
  35991. DUK_D(DUK_DPRINT("paused, process debug message, blocking if necessary"));
  35992. }
  35993. duk__debug_process_message(thr);
  35994. if (thr->heap->dbg_read_cb == NULL) {
  35995. /* Became detached during message handling (perhaps because
  35996. * of an error or by an explicit Detach). Call detached
  35997. * callback here, between messages, to avoid confusing the
  35998. * broken connection and a possible replacement (which may
  35999. * be provided by an instant reattach inside the detached
  36000. * callback).
  36001. */
  36002. duk__debug_do_detach2(thr->heap);
  36003. }
  36004. if (thr->heap->dbg_state_dirty) {
  36005. /* Executed something that may have affected status,
  36006. * resend.
  36007. */
  36008. duk_debug_send_status(thr);
  36009. thr->heap->dbg_state_dirty = 0;
  36010. }
  36011. retval = 1; /* processed one or more messages */
  36012. }
  36013. /* As an initial implementation, read flush after exiting the message
  36014. * loop.
  36015. */
  36016. duk_debug_read_flush(thr);
  36017. DUK_DD(DUK_DDPRINT("top at exit: %ld", (long) duk_get_top(ctx)));
  36018. #if defined(DUK_USE_ASSERTIONS)
  36019. /* Easy to get wrong, so assert for it. */
  36020. DUK_ASSERT(entry_top == duk_get_top(ctx));
  36021. #endif
  36022. return retval;
  36023. }
  36024. /*
  36025. * Breakpoint management
  36026. */
  36027. DUK_INTERNAL duk_small_int_t duk_debug_add_breakpoint(duk_hthread *thr, duk_hstring *filename, duk_uint32_t line) {
  36028. duk_heap *heap;
  36029. duk_breakpoint *b;
  36030. /* Caller must trigger recomputation of active breakpoint list. To
  36031. * ensure stale values are not used if that doesn't happen, clear the
  36032. * active breakpoint list here.
  36033. */
  36034. DUK_ASSERT(thr != NULL);
  36035. DUK_ASSERT(filename != NULL);
  36036. heap = thr->heap;
  36037. DUK_ASSERT(heap != NULL);
  36038. if (heap->dbg_breakpoint_count >= DUK_HEAP_MAX_BREAKPOINTS) {
  36039. DUK_D(DUK_DPRINT("failed to add breakpoint for %O:%ld, all breakpoint slots used",
  36040. (duk_heaphdr *) filename, (long) line));
  36041. return -1;
  36042. }
  36043. heap->dbg_breakpoints_active[0] = (duk_breakpoint *) NULL;
  36044. b = heap->dbg_breakpoints + (heap->dbg_breakpoint_count++);
  36045. b->filename = filename;
  36046. b->line = line;
  36047. DUK_HSTRING_INCREF(thr, filename);
  36048. return heap->dbg_breakpoint_count - 1; /* index */
  36049. }
  36050. DUK_INTERNAL duk_bool_t duk_debug_remove_breakpoint(duk_hthread *thr, duk_small_uint_t breakpoint_index) {
  36051. duk_heap *heap;
  36052. duk_hstring *h;
  36053. duk_breakpoint *b;
  36054. duk_size_t move_size;
  36055. /* Caller must trigger recomputation of active breakpoint list. To
  36056. * ensure stale values are not used if that doesn't happen, clear the
  36057. * active breakpoint list here.
  36058. */
  36059. DUK_ASSERT(thr != NULL);
  36060. heap = thr->heap;
  36061. DUK_ASSERT(heap != NULL);
  36062. DUK_ASSERT(DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap));
  36063. DUK_ASSERT_DISABLE(breakpoint_index >= 0); /* unsigned */
  36064. if (breakpoint_index >= heap->dbg_breakpoint_count) {
  36065. DUK_D(DUK_DPRINT("invalid breakpoint index: %ld", (long) breakpoint_index));
  36066. return 0;
  36067. }
  36068. b = heap->dbg_breakpoints + breakpoint_index;
  36069. h = b->filename;
  36070. DUK_ASSERT(h != NULL);
  36071. move_size = sizeof(duk_breakpoint) * (heap->dbg_breakpoint_count - breakpoint_index - 1);
  36072. if (move_size > 0) {
  36073. DUK_MEMMOVE((void *) b,
  36074. (const void *) (b + 1),
  36075. (size_t) move_size);
  36076. }
  36077. heap->dbg_breakpoint_count--;
  36078. heap->dbg_breakpoints_active[0] = (duk_breakpoint *) NULL;
  36079. DUK_HSTRING_DECREF(thr, h); /* side effects */
  36080. DUK_UNREF(h); /* w/o refcounting */
  36081. /* Breakpoint entries above the used area are left as garbage. */
  36082. return 1;
  36083. }
  36084. #undef DUK__SET_CONN_BROKEN
  36085. #else /* DUK_USE_DEBUGGER_SUPPORT */
  36086. /* No debugger support. */
  36087. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  36088. #line 1 "duk_error_augment.c"
  36089. /*
  36090. * Augmenting errors at their creation site and their throw site.
  36091. *
  36092. * When errors are created, traceback data is added by built-in code
  36093. * and a user error handler (if defined) can process or replace the
  36094. * error. Similarly, when errors are thrown, a user error handler
  36095. * (if defined) can process or replace the error.
  36096. *
  36097. * Augmentation and other processing at error creation time is nice
  36098. * because an error is only created once, but it may be thrown and
  36099. * rethrown multiple times. User error handler registered for processing
  36100. * an error at its throw site must be careful to handle rethrowing in
  36101. * a useful manner.
  36102. *
  36103. * Error augmentation may throw an internal error (e.g. alloc error).
  36104. *
  36105. * Ecmascript allows throwing any values, so all values cannot be
  36106. * augmented. Currently, the built-in augmentation at error creation
  36107. * only augments error values which are Error instances (= have the
  36108. * built-in Error.prototype in their prototype chain) and are also
  36109. * extensible. User error handlers have no limitations in this respect.
  36110. */
  36111. /* include removed: duk_internal.h */
  36112. /*
  36113. * Helper for calling a user error handler.
  36114. *
  36115. * 'thr' must be the currently active thread; the error handler is called
  36116. * in its context. The valstack of 'thr' must have the error value on
  36117. * top, and will be replaced by another error value based on the return
  36118. * value of the error handler.
  36119. *
  36120. * The helper calls duk_handle_call() recursively in protected mode.
  36121. * Before that call happens, no longjmps should happen; as a consequence,
  36122. * we must assume that the valstack contains enough temporary space for
  36123. * arguments and such.
  36124. *
  36125. * While the error handler runs, any errors thrown will not trigger a
  36126. * recursive error handler call (this is implemented using a heap level
  36127. * flag which will "follow" through any coroutines resumed inside the
  36128. * error handler). If the error handler is not callable or throws an
  36129. * error, the resulting error replaces the original error (for Duktape
  36130. * internal errors, duk_error_throw.c further substitutes this error with
  36131. * a DoubleError which is not ideal). This would be easy to change and
  36132. * even signal to the caller.
  36133. *
  36134. * The user error handler is stored in 'Duktape.errCreate' or
  36135. * 'Duktape.errThrow' depending on whether we're augmenting the error at
  36136. * creation or throw time. There are several alternatives to this approach,
  36137. * see doc/error-objects.rst for discussion.
  36138. *
  36139. * Note: since further longjmp()s may occur while calling the error handler
  36140. * (for many reasons, e.g. a labeled 'break' inside the handler), the
  36141. * caller can make no assumptions on the thr->heap->lj state after the
  36142. * call (this affects especially duk_error_throw.c). This is not an issue
  36143. * as long as the caller writes to the lj state only after the error handler
  36144. * finishes.
  36145. */
  36146. #if defined(DUK_USE_ERRTHROW) || defined(DUK_USE_ERRCREATE)
  36147. DUK_LOCAL void duk__err_augment_user(duk_hthread *thr, duk_small_uint_t stridx_cb) {
  36148. duk_context *ctx = (duk_context *) thr;
  36149. duk_tval *tv_hnd;
  36150. duk_small_uint_t call_flags;
  36151. duk_int_t rc;
  36152. DUK_ASSERT(thr != NULL);
  36153. DUK_ASSERT(thr->heap != NULL);
  36154. DUK_ASSERT_DISABLE(stridx_cb >= 0); /* unsigned */
  36155. DUK_ASSERT(stridx_cb < DUK_HEAP_NUM_STRINGS);
  36156. if (DUK_HEAP_HAS_ERRHANDLER_RUNNING(thr->heap)) {
  36157. DUK_DD(DUK_DDPRINT("recursive call to error handler, ignore"));
  36158. return;
  36159. }
  36160. /*
  36161. * Check whether or not we have an error handler.
  36162. *
  36163. * We must be careful of not triggering an error when looking up the
  36164. * property. For instance, if the property is a getter, we don't want
  36165. * to call it, only plain values are allowed. The value, if it exists,
  36166. * is not checked. If the value is not a function, a TypeError happens
  36167. * when it is called and that error replaces the original one.
  36168. */
  36169. DUK_ASSERT_VALSTACK_SPACE(thr, 4); /* 3 entries actually needed below */
  36170. /* [ ... errval ] */
  36171. if (thr->builtins[DUK_BIDX_DUKTAPE] == NULL) {
  36172. /* When creating built-ins, some of the built-ins may not be set
  36173. * and we want to tolerate that when throwing errors.
  36174. */
  36175. DUK_DD(DUK_DDPRINT("error occurred when DUK_BIDX_DUKTAPE is NULL, ignoring"));
  36176. return;
  36177. }
  36178. tv_hnd = duk_hobject_find_existing_entry_tval_ptr(thr->heap,
  36179. thr->builtins[DUK_BIDX_DUKTAPE],
  36180. DUK_HTHREAD_GET_STRING(thr, stridx_cb));
  36181. if (tv_hnd == NULL) {
  36182. DUK_DD(DUK_DDPRINT("error handler does not exist or is not a plain value: %!T",
  36183. (duk_tval *) tv_hnd));
  36184. return;
  36185. }
  36186. DUK_DDD(DUK_DDDPRINT("error handler dump (callability not checked): %!T",
  36187. (duk_tval *) tv_hnd));
  36188. duk_push_tval(ctx, tv_hnd);
  36189. /* [ ... errval errhandler ] */
  36190. duk_insert(ctx, -2); /* -> [ ... errhandler errval ] */
  36191. duk_push_undefined(ctx);
  36192. duk_insert(ctx, -2); /* -> [ ... errhandler undefined(= this) errval ] */
  36193. /* [ ... errhandler undefined errval ] */
  36194. /*
  36195. * DUK_CALL_FLAG_IGNORE_RECLIMIT causes duk_handle_call() to ignore C
  36196. * recursion depth limit (and won't increase it either). This is
  36197. * dangerous, but useful because it allows the error handler to run
  36198. * even if the original error is caused by C recursion depth limit.
  36199. *
  36200. * The heap level DUK_HEAP_FLAG_ERRHANDLER_RUNNING is set for the
  36201. * duration of the error handler and cleared afterwards. This flag
  36202. * prevents the error handler from running recursively. The flag is
  36203. * heap level so that the flag properly controls even coroutines
  36204. * launched by an error handler. Since the flag is heap level, it is
  36205. * critical to restore it correctly.
  36206. *
  36207. * We ignore errors now: a success return and an error value both
  36208. * replace the original error value. (This would be easy to change.)
  36209. */
  36210. DUK_ASSERT(!DUK_HEAP_HAS_ERRHANDLER_RUNNING(thr->heap)); /* since no recursive error handler calls */
  36211. DUK_HEAP_SET_ERRHANDLER_RUNNING(thr->heap);
  36212. call_flags = DUK_CALL_FLAG_PROTECTED |
  36213. DUK_CALL_FLAG_IGNORE_RECLIMIT; /* protected, ignore reclimit, not constructor */
  36214. rc = duk_handle_call(thr,
  36215. 1, /* num args */
  36216. call_flags); /* call_flags */
  36217. DUK_UNREF(rc); /* no need to check now: both success and error are OK */
  36218. DUK_ASSERT(DUK_HEAP_HAS_ERRHANDLER_RUNNING(thr->heap));
  36219. DUK_HEAP_CLEAR_ERRHANDLER_RUNNING(thr->heap);
  36220. /* [ ... errval ] */
  36221. }
  36222. #endif /* DUK_USE_ERRTHROW || DUK_USE_ERRCREATE */
  36223. /*
  36224. * Add tracedata to an error on the stack top.
  36225. */
  36226. #ifdef DUK_USE_TRACEBACKS
  36227. DUK_LOCAL void duk__add_traceback(duk_hthread *thr, duk_hthread *thr_callstack, const char *c_filename, duk_int_t c_line, duk_bool_t noblame_fileline) {
  36228. duk_context *ctx = (duk_context *) thr;
  36229. duk_small_uint_t depth;
  36230. duk_int_t i, i_min;
  36231. duk_uarridx_t arr_idx;
  36232. duk_double_t d;
  36233. DUK_ASSERT(thr != NULL);
  36234. DUK_ASSERT(thr_callstack != NULL);
  36235. DUK_ASSERT(ctx != NULL);
  36236. /* [ ... error ] */
  36237. /*
  36238. * The traceback format is pretty arcane in an attempt to keep it compact
  36239. * and cheap to create. It may change arbitrarily from version to version.
  36240. * It should be decoded/accessed through version specific accessors only.
  36241. *
  36242. * See doc/error-objects.rst.
  36243. */
  36244. DUK_DDD(DUK_DDDPRINT("adding traceback to object: %!T",
  36245. (duk_tval *) duk_get_tval(ctx, -1)));
  36246. duk_push_array(ctx); /* XXX: specify array size, as we know it */
  36247. arr_idx = 0;
  36248. /* compiler SyntaxErrors (and other errors) come first; blame the source
  36249. * code file/line primarily.
  36250. */
  36251. if (thr->compile_ctx != NULL && thr->compile_ctx->h_filename != NULL) {
  36252. duk_push_hstring(ctx, thr->compile_ctx->h_filename);
  36253. duk_xdef_prop_index_wec(ctx, -2, arr_idx);
  36254. arr_idx++;
  36255. duk_push_uint(ctx, (duk_uint_t) thr->compile_ctx->curr_token.start_line); /* (flags<<32) + (line), flags = 0 */
  36256. duk_xdef_prop_index_wec(ctx, -2, arr_idx);
  36257. arr_idx++;
  36258. }
  36259. /* filename/line from C macros (__FILE__, __LINE__) are added as an
  36260. * entry with a special format: (string, number). The number contains
  36261. * the line and flags.
  36262. */
  36263. /* XXX: optimize: allocate an array part to the necessary size (upwards
  36264. * estimate) and fill in the values directly into the array part; finally
  36265. * update 'length'.
  36266. */
  36267. /* XXX: using duk_put_prop_index() would cause obscure error cases when Array.prototype
  36268. * has write-protected array index named properties. This was seen as DoubleErrors
  36269. * in e.g. some test262 test cases. Using duk_xdef_prop_index() is better but heavier.
  36270. * The best fix is to fill in the tracedata directly into the array part.
  36271. */
  36272. /* [ ... error arr ] */
  36273. if (c_filename) {
  36274. duk_push_string(ctx, c_filename);
  36275. duk_xdef_prop_index_wec(ctx, -2, arr_idx);
  36276. arr_idx++;
  36277. d = (noblame_fileline ? ((duk_double_t) DUK_TB_FLAG_NOBLAME_FILELINE) * DUK_DOUBLE_2TO32 : 0.0) +
  36278. (duk_double_t) c_line;
  36279. duk_push_number(ctx, d);
  36280. duk_xdef_prop_index_wec(ctx, -2, arr_idx);
  36281. arr_idx++;
  36282. }
  36283. /* traceback depth doesn't take into account the filename/line
  36284. * special handling above (intentional)
  36285. */
  36286. depth = DUK_USE_TRACEBACK_DEPTH;
  36287. i_min = (thr_callstack->callstack_top > (duk_size_t) depth ? (duk_int_t) (thr_callstack->callstack_top - depth) : 0);
  36288. DUK_ASSERT(i_min >= 0);
  36289. /* [ ... error arr ] */
  36290. DUK_ASSERT(thr_callstack->callstack_top <= DUK_INT_MAX); /* callstack limits */
  36291. for (i = (duk_int_t) (thr_callstack->callstack_top - 1); i >= i_min; i--) {
  36292. duk_uint32_t pc;
  36293. /*
  36294. * Note: each API operation potentially resizes the callstack,
  36295. * so be careful to re-lookup after every operation. Currently
  36296. * these is no issue because we don't store a temporary 'act'
  36297. * pointer at all. (This would be a non-issue if we operated
  36298. * directly on the array part.)
  36299. */
  36300. /* [... arr] */
  36301. DUK_ASSERT_DISABLE(thr_callstack->callstack[i].pc >= 0); /* unsigned */
  36302. /* Add function object. */
  36303. duk_push_tval(ctx, &(thr_callstack->callstack + i)->tv_func);
  36304. duk_xdef_prop_index_wec(ctx, -2, arr_idx);
  36305. arr_idx++;
  36306. /* Add a number containing: pc, activation flags.
  36307. *
  36308. * PC points to next instruction, find offending PC. Note that
  36309. * PC == 0 for native code.
  36310. */
  36311. pc = duk_hthread_get_act_prev_pc(thr_callstack, thr_callstack->callstack + i);
  36312. DUK_ASSERT_DISABLE(pc >= 0); /* unsigned */
  36313. DUK_ASSERT((duk_double_t) pc < DUK_DOUBLE_2TO32); /* assume PC is at most 32 bits and non-negative */
  36314. d = ((duk_double_t) thr_callstack->callstack[i].flags) * DUK_DOUBLE_2TO32 + (duk_double_t) pc;
  36315. duk_push_number(ctx, d); /* -> [... arr num] */
  36316. duk_xdef_prop_index_wec(ctx, -2, arr_idx);
  36317. arr_idx++;
  36318. }
  36319. /* XXX: set with duk_hobject_set_length() when tracedata is filled directly */
  36320. duk_push_uint(ctx, (duk_uint_t) arr_idx);
  36321. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_WC);
  36322. /* [ ... error arr ] */
  36323. duk_xdef_prop_stridx_wec(ctx, -2, DUK_STRIDX_INT_TRACEDATA); /* -> [ ... error ] */
  36324. }
  36325. #endif /* DUK_USE_TRACEBACKS */
  36326. #if defined(DUK_USE_AUGMENT_ERROR_CREATE)
  36327. DUK_LOCAL void duk__err_augment_builtin_throw(duk_hthread *thr, duk_hthread *thr_callstack, const char *c_filename, duk_int_t c_line, duk_small_int_t noblame_fileline, duk_hobject *obj) {
  36328. duk_context *ctx = (duk_context *) thr;
  36329. #ifdef DUK_USE_ASSERTIONS
  36330. duk_int_t entry_top;
  36331. #endif
  36332. #ifdef DUK_USE_ASSERTIONS
  36333. entry_top = duk_get_top(ctx);
  36334. #endif
  36335. DUK_ASSERT(obj != NULL);
  36336. DUK_UNREF(obj); /* unreferenced w/o tracebacks */
  36337. DUK_UNREF(ctx); /* unreferenced w/ tracebacks */
  36338. #ifdef DUK_USE_TRACEBACKS
  36339. /*
  36340. * If tracebacks are enabled, the '_Tracedata' property is the only
  36341. * thing we need: 'fileName' and 'lineNumber' are virtual properties
  36342. * which use '_Tracedata'.
  36343. */
  36344. if (duk_hobject_hasprop_raw(thr, obj, DUK_HTHREAD_STRING_INT_TRACEDATA(thr))) {
  36345. DUK_DDD(DUK_DDDPRINT("error value already has a '_Tracedata' property, not modifying it"));
  36346. } else {
  36347. duk__add_traceback(thr, thr_callstack, c_filename, c_line, noblame_fileline);
  36348. }
  36349. #else
  36350. /*
  36351. * If tracebacks are disabled, 'fileName' and 'lineNumber' are added
  36352. * as plain own properties. Since Error.prototype has accessors of
  36353. * the same name, we need to define own properties directly (cannot
  36354. * just use e.g. duk_put_prop_stridx). Existing properties are not
  36355. * overwritten in case they already exist.
  36356. */
  36357. if (thr->compile_ctx != NULL && thr->compile_ctx->h_filename != NULL) {
  36358. /* Compiler SyntaxError (or other error) gets the primary blame. */
  36359. duk_push_hstring(ctx, thr->compile_ctx->h_filename);
  36360. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE);
  36361. duk_push_uint(ctx, (duk_uint_t) thr->compile_ctx->curr_token.start_line);
  36362. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LINE_NUMBER, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE);
  36363. } else if (c_filename && !noblame_fileline) {
  36364. /* XXX: file/line is disabled in minimal builds, so disable this too
  36365. * when appropriate.
  36366. */
  36367. duk_push_string(ctx, c_filename);
  36368. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE);
  36369. duk_push_int(ctx, c_line);
  36370. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LINE_NUMBER, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE);
  36371. } else if (thr_callstack->callstack_top > 0) {
  36372. duk_activation *act;
  36373. duk_hobject *func;
  36374. act = thr_callstack->callstack + thr_callstack->callstack_top - 1;
  36375. DUK_ASSERT(act >= thr_callstack->callstack && act < thr_callstack->callstack + thr_callstack->callstack_size);
  36376. func = DUK_ACT_GET_FUNC(act);
  36377. if (func) {
  36378. duk_uint32_t pc;
  36379. /* PC points to next instruction, find offending PC. Note that
  36380. * PC == 0 for native code.
  36381. */
  36382. pc = duk_hthread_get_act_prev_pc(thr, act);
  36383. DUK_ASSERT_DISABLE(pc >= 0); /* unsigned */
  36384. DUK_ASSERT((duk_double_t) pc < DUK_DOUBLE_2TO32); /* assume PC is at most 32 bits and non-negative */
  36385. act = NULL; /* invalidated by pushes, so get out of the way */
  36386. duk_push_hobject(ctx, func);
  36387. /* [ ... error func ] */
  36388. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_FILE_NAME);
  36389. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE);
  36390. #if defined(DUK_USE_PC2LINE)
  36391. if (DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) {
  36392. duk_uint32_t ecma_line;
  36393. #if 0
  36394. duk_push_u32(ctx, pc);
  36395. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_PC, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAGS_NO_OVERWRITE);
  36396. #endif
  36397. ecma_line = duk_hobject_pc2line_query(ctx, -1, (duk_uint_fast32_t) pc);
  36398. if (ecma_line > 0) {
  36399. duk_push_u32(ctx, (duk_uint32_t) ecma_line); /* -> [ ... error func line ] */
  36400. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_LINE_NUMBER, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE);
  36401. }
  36402. } else {
  36403. /* Native function, no relevant lineNumber. */
  36404. }
  36405. #endif /* DUK_USE_PC2LINE */
  36406. duk_pop(ctx);
  36407. }
  36408. }
  36409. #endif /* DUK_USE_TRACEBACKS */
  36410. #ifdef DUK_USE_ASSERTIONS
  36411. DUK_ASSERT(duk_get_top(ctx) == entry_top);
  36412. #endif
  36413. }
  36414. #endif /* DUK_USE_AUGMENT_ERROR_CREATE */
  36415. /*
  36416. * Augment an error at creation time with _Tracedata/fileName/lineNumber
  36417. * and allow a user error handler (if defined) to process/replace the error.
  36418. * The error to be augmented is at the stack top.
  36419. *
  36420. * thr: thread containing the error value
  36421. * thr_callstack: thread which should be used for generating callstack etc.
  36422. * c_filename: C __FILE__ related to the error
  36423. * c_line: C __LINE__ related to the error
  36424. * noblame_fileline: if true, don't fileName/line as error source, otherwise use traceback
  36425. * (needed because user code filename/line are reported but internal ones
  36426. * are not)
  36427. *
  36428. * XXX: rename noblame_fileline to flags field; combine it to some existing
  36429. * field (there are only a few call sites so this may not be worth it).
  36430. */
  36431. #if defined(DUK_USE_AUGMENT_ERROR_CREATE)
  36432. 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_bool_t noblame_fileline) {
  36433. duk_context *ctx = (duk_context *) thr;
  36434. duk_hobject *obj;
  36435. DUK_ASSERT(thr != NULL);
  36436. DUK_ASSERT(thr_callstack != NULL);
  36437. DUK_ASSERT(ctx != NULL);
  36438. /* [ ... error ] */
  36439. /*
  36440. * Criteria for augmenting:
  36441. *
  36442. * - augmentation enabled in build (naturally)
  36443. * - error value internal prototype chain contains the built-in
  36444. * Error prototype object (i.e. 'val instanceof Error')
  36445. *
  36446. * Additional criteria for built-in augmenting:
  36447. *
  36448. * - error value is an extensible object
  36449. */
  36450. obj = duk_get_hobject(ctx, -1);
  36451. if (!obj) {
  36452. DUK_DDD(DUK_DDDPRINT("value is not an object, skip both built-in and user augment"));
  36453. return;
  36454. }
  36455. if (!duk_hobject_prototype_chain_contains(thr, obj, thr->builtins[DUK_BIDX_ERROR_PROTOTYPE], 1 /*ignore_loop*/)) {
  36456. /* If the value has a prototype loop, it's critical not to
  36457. * throw here. Instead, assume the value is not to be
  36458. * augmented.
  36459. */
  36460. DUK_DDD(DUK_DDDPRINT("value is not an error instance, skip both built-in and user augment"));
  36461. return;
  36462. }
  36463. if (DUK_HOBJECT_HAS_EXTENSIBLE(obj)) {
  36464. DUK_DDD(DUK_DDDPRINT("error meets criteria, built-in augment"));
  36465. duk__err_augment_builtin_throw(thr, thr_callstack, c_filename, c_line, noblame_fileline, obj);
  36466. } else {
  36467. DUK_DDD(DUK_DDDPRINT("error does not meet criteria, no built-in augment"));
  36468. }
  36469. /* [ ... error ] */
  36470. #if defined(DUK_USE_ERRCREATE)
  36471. duk__err_augment_user(thr, DUK_STRIDX_ERR_CREATE);
  36472. #endif
  36473. }
  36474. #endif /* DUK_USE_AUGMENT_ERROR_CREATE */
  36475. /*
  36476. * Augment an error at throw time; allow a user error handler (if defined)
  36477. * to process/replace the error. The error to be augmented is at the
  36478. * stack top.
  36479. */
  36480. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  36481. DUK_INTERNAL void duk_err_augment_error_throw(duk_hthread *thr) {
  36482. #if defined(DUK_USE_ERRTHROW)
  36483. duk__err_augment_user(thr, DUK_STRIDX_ERR_THROW);
  36484. #endif /* DUK_USE_ERRTHROW */
  36485. }
  36486. #endif /* DUK_USE_AUGMENT_ERROR_THROW */
  36487. #line 1 "duk_error_longjmp.c"
  36488. /*
  36489. * Do a longjmp call, calling the fatal error handler if no
  36490. * catchpoint exists.
  36491. */
  36492. /* include removed: duk_internal.h */
  36493. DUK_INTERNAL void duk_err_longjmp(duk_hthread *thr) {
  36494. DUK_ASSERT(thr != NULL);
  36495. if (!thr->heap->lj.jmpbuf_ptr) {
  36496. /*
  36497. * If we don't have a jmpbuf_ptr, there is little we can do
  36498. * except panic. The caller's expectation is that we never
  36499. * return.
  36500. */
  36501. DUK_D(DUK_DPRINT("uncaught error: type=%d iserror=%d value1=%!T value2=%!T",
  36502. (int) thr->heap->lj.type, (int) thr->heap->lj.iserror,
  36503. &thr->heap->lj.value1, &thr->heap->lj.value2));
  36504. duk_fatal((duk_context *) thr, DUK_ERR_UNCAUGHT_ERROR, "uncaught error");
  36505. DUK_UNREACHABLE();
  36506. }
  36507. DUK_LONGJMP(thr->heap->lj.jmpbuf_ptr->jb);
  36508. DUK_UNREACHABLE();
  36509. }
  36510. #line 1 "duk_error_misc.c"
  36511. /*
  36512. * Error helpers
  36513. */
  36514. /* include removed: duk_internal.h */
  36515. /*
  36516. * Helper to walk the thread chain and see if there is an active error
  36517. * catcher. Protected calls or finally blocks aren't considered catching.
  36518. */
  36519. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  36520. DUK_LOCAL duk_bool_t duk__have_active_catcher(duk_hthread *thr) {
  36521. /*
  36522. * XXX: As noted above, a protected API call won't be counted as a
  36523. * catcher. This is usually convenient, e.g. in the case of a top-
  36524. * level duk_pcall(), but may not always be desirable. Perhaps add an
  36525. * argument to treat them as catchers?
  36526. */
  36527. duk_size_t i;
  36528. DUK_ASSERT(thr != NULL);
  36529. while (thr != NULL) {
  36530. for (i = 0; i < thr->catchstack_top; i++) {
  36531. duk_catcher *cat = thr->catchstack + i;
  36532. if (DUK_CAT_HAS_CATCH_ENABLED(cat)) {
  36533. return 1; /* all we need to know */
  36534. }
  36535. }
  36536. thr = thr->resumer;
  36537. }
  36538. return 0;
  36539. }
  36540. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  36541. /*
  36542. * Get prototype object for an integer error code.
  36543. */
  36544. DUK_INTERNAL duk_hobject *duk_error_prototype_from_code(duk_hthread *thr, duk_errcode_t code) {
  36545. switch (code) {
  36546. case DUK_ERR_EVAL_ERROR:
  36547. return thr->builtins[DUK_BIDX_EVAL_ERROR_PROTOTYPE];
  36548. case DUK_ERR_RANGE_ERROR:
  36549. return thr->builtins[DUK_BIDX_RANGE_ERROR_PROTOTYPE];
  36550. case DUK_ERR_REFERENCE_ERROR:
  36551. return thr->builtins[DUK_BIDX_REFERENCE_ERROR_PROTOTYPE];
  36552. case DUK_ERR_SYNTAX_ERROR:
  36553. return thr->builtins[DUK_BIDX_SYNTAX_ERROR_PROTOTYPE];
  36554. case DUK_ERR_TYPE_ERROR:
  36555. return thr->builtins[DUK_BIDX_TYPE_ERROR_PROTOTYPE];
  36556. case DUK_ERR_URI_ERROR:
  36557. return thr->builtins[DUK_BIDX_URI_ERROR_PROTOTYPE];
  36558. /* XXX: more specific error classes? */
  36559. case DUK_ERR_UNIMPLEMENTED_ERROR:
  36560. case DUK_ERR_INTERNAL_ERROR:
  36561. case DUK_ERR_ALLOC_ERROR:
  36562. case DUK_ERR_ASSERTION_ERROR:
  36563. case DUK_ERR_API_ERROR:
  36564. case DUK_ERR_ERROR:
  36565. default:
  36566. return thr->builtins[DUK_BIDX_ERROR_PROTOTYPE];
  36567. }
  36568. }
  36569. /*
  36570. * Exposed helper for setting up heap longjmp state.
  36571. */
  36572. DUK_INTERNAL void duk_err_setup_heap_ljstate(duk_hthread *thr, duk_small_int_t lj_type) {
  36573. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  36574. /* If something is thrown with the debugger attached and nobody will
  36575. * catch it, execution is paused before the longjmp, turning over
  36576. * control to the debug client. This allows local state to be examined
  36577. * before the stack is unwound. Errors are not intercepted when debug
  36578. * message loop is active (e.g. for Eval).
  36579. */
  36580. /* XXX: Allow customizing the pause and notify behavior at runtime
  36581. * using debugger runtime flags. For now the behavior is fixed using
  36582. * config options.
  36583. */
  36584. #if defined(DUK_USE_DEBUGGER_THROW_NOTIFY) || defined(DUK_USE_DEBUGGER_PAUSE_UNCAUGHT)
  36585. if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap) &&
  36586. !thr->heap->dbg_processing &&
  36587. lj_type == DUK_LJ_TYPE_THROW) {
  36588. duk_context *ctx = (duk_context *) thr;
  36589. duk_bool_t fatal;
  36590. duk_hobject *h_obj;
  36591. /* Don't intercept a DoubleError, we may have caused the initial double
  36592. * fault and attempting to intercept it will cause us to be called
  36593. * recursively and exhaust the C stack.
  36594. */
  36595. h_obj = duk_get_hobject(ctx, -1);
  36596. if (h_obj == thr->builtins[DUK_BIDX_DOUBLE_ERROR]) {
  36597. DUK_D(DUK_DPRINT("built-in DoubleError instance thrown, not intercepting"));
  36598. goto skip_throw_intercept;
  36599. }
  36600. DUK_D(DUK_DPRINT("throw with debugger attached, report to client"));
  36601. fatal = !duk__have_active_catcher(thr);
  36602. #if defined(DUK_USE_DEBUGGER_THROW_NOTIFY)
  36603. /* Report it to the debug client */
  36604. duk_debug_send_throw(thr, fatal);
  36605. #endif
  36606. #if defined(DUK_USE_DEBUGGER_PAUSE_UNCAUGHT)
  36607. if (fatal) {
  36608. DUK_D(DUK_DPRINT("throw will be fatal, halt before longjmp"));
  36609. duk_debug_halt_execution(thr, 1 /*use_prev_pc*/);
  36610. }
  36611. #endif
  36612. }
  36613. skip_throw_intercept:
  36614. #endif /* DUK_USE_DEBUGGER_THROW_NOTIFY || DUK_USE_DEBUGGER_PAUSE_UNCAUGHT */
  36615. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  36616. thr->heap->lj.type = lj_type;
  36617. DUK_ASSERT(thr->valstack_top > thr->valstack);
  36618. DUK_TVAL_SET_TVAL_UPDREF(thr, &thr->heap->lj.value1, thr->valstack_top - 1); /* side effects */
  36619. duk_pop((duk_context *) thr);
  36620. }
  36621. #line 1 "duk_error_throw.c"
  36622. /*
  36623. * Create and throw an Ecmascript error object based on a code and a message.
  36624. *
  36625. * Used when we throw errors internally. Ecmascript generated error objects
  36626. * are created by Ecmascript code, and the throwing is handled by the bytecode
  36627. * executor.
  36628. */
  36629. /* include removed: duk_internal.h */
  36630. /*
  36631. * Create and throw an error (originating from Duktape internally)
  36632. *
  36633. * Push an error object on top of the stack, possibly throw augmenting
  36634. * the error, and finally longjmp.
  36635. *
  36636. * If an error occurs while we're dealing with the current error, we might
  36637. * enter an infinite recursion loop. This is prevented by detecting a
  36638. * "double fault" through the heap->handling_error flag; the recursion
  36639. * then stops at the second level.
  36640. */
  36641. #ifdef DUK_USE_VERBOSE_ERRORS
  36642. 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) {
  36643. #else
  36644. DUK_INTERNAL void duk_err_create_and_throw(duk_hthread *thr, duk_errcode_t code) {
  36645. #endif
  36646. duk_context *ctx = (duk_context *) thr;
  36647. duk_bool_t double_error = thr->heap->handling_error;
  36648. #ifdef DUK_USE_VERBOSE_ERRORS
  36649. DUK_DD(DUK_DDPRINT("duk_err_create_and_throw(): code=%ld, msg=%s, filename=%s, line=%ld",
  36650. (long) code, (const char *) msg,
  36651. (const char *) filename, (long) line));
  36652. #else
  36653. DUK_DD(DUK_DDPRINT("duk_err_create_and_throw(): code=%ld", (long) code));
  36654. #endif
  36655. DUK_ASSERT(thr != NULL);
  36656. DUK_ASSERT(ctx != NULL);
  36657. thr->heap->handling_error = 1;
  36658. if (!double_error) {
  36659. /* Allow headroom for calls during error handling (see GH-191).
  36660. * We allow space for 10 additional recursions, with one extra
  36661. * for, e.g. a print() call at the deepest level.
  36662. */
  36663. DUK_ASSERT(thr->callstack_max == DUK_CALLSTACK_DEFAULT_MAX);
  36664. thr->callstack_max = DUK_CALLSTACK_DEFAULT_MAX + DUK_CALLSTACK_GROW_STEP + 11;
  36665. }
  36666. DUK_ASSERT(thr->callstack_max == DUK_CALLSTACK_DEFAULT_MAX + DUK_CALLSTACK_GROW_STEP + 11); /* just making sure */
  36667. /* Sync so that augmentation sees up-to-date activations, NULL
  36668. * thr->ptr_curr_pc so that it's not used if side effects occur
  36669. * in augmentation or longjmp handling.
  36670. */
  36671. duk_hthread_sync_and_null_currpc(thr);
  36672. /*
  36673. * Create and push an error object onto the top of stack.
  36674. * If a "double error" occurs, use a fixed error instance
  36675. * to avoid further trouble.
  36676. */
  36677. /* XXX: if attempt to push beyond allocated valstack, this double fault
  36678. * handling fails miserably. We should really write the double error
  36679. * directly to thr->heap->lj.value1 and avoid valstack use entirely.
  36680. */
  36681. if (double_error) {
  36682. if (thr->builtins[DUK_BIDX_DOUBLE_ERROR]) {
  36683. DUK_D(DUK_DPRINT("double fault detected -> push built-in fixed 'double error' instance"));
  36684. duk_push_hobject_bidx(ctx, DUK_BIDX_DOUBLE_ERROR);
  36685. } else {
  36686. DUK_D(DUK_DPRINT("double fault detected; there is no built-in fixed 'double error' instance "
  36687. "-> push the error code as a number"));
  36688. duk_push_int(ctx, (duk_int_t) code);
  36689. }
  36690. } else {
  36691. /* Error object is augmented at its creation here. */
  36692. duk_require_stack(ctx, 1);
  36693. /* XXX: unnecessary '%s' formatting here, but cannot use
  36694. * 'msg' as a format string directly.
  36695. */
  36696. #ifdef DUK_USE_VERBOSE_ERRORS
  36697. duk_push_error_object_raw(ctx,
  36698. code | DUK_ERRCODE_FLAG_NOBLAME_FILELINE,
  36699. filename,
  36700. line,
  36701. "%s",
  36702. (const char *) msg);
  36703. #else
  36704. duk_push_error_object_raw(ctx,
  36705. code | DUK_ERRCODE_FLAG_NOBLAME_FILELINE,
  36706. NULL,
  36707. 0,
  36708. NULL);
  36709. #endif
  36710. }
  36711. /*
  36712. * Augment error (throw time), unless alloc/double error
  36713. */
  36714. if (double_error || code == DUK_ERR_ALLOC_ERROR) {
  36715. DUK_D(DUK_DPRINT("alloc or double error: skip throw augmenting to avoid further trouble"));
  36716. } else {
  36717. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  36718. DUK_DDD(DUK_DDDPRINT("THROW ERROR (INTERNAL): %!iT (before throw augment)",
  36719. (duk_tval *) duk_get_tval(ctx, -1)));
  36720. duk_err_augment_error_throw(thr);
  36721. #endif
  36722. }
  36723. /*
  36724. * Finally, longjmp
  36725. */
  36726. duk_err_setup_heap_ljstate(thr, DUK_LJ_TYPE_THROW);
  36727. thr->callstack_max = DUK_CALLSTACK_DEFAULT_MAX; /* reset callstack limit */
  36728. thr->heap->handling_error = 0;
  36729. DUK_DDD(DUK_DDDPRINT("THROW ERROR (INTERNAL): %!iT, %!iT (after throw augment)",
  36730. (duk_tval *) &thr->heap->lj.value1, (duk_tval *) &thr->heap->lj.value2));
  36731. duk_err_longjmp(thr);
  36732. DUK_UNREACHABLE();
  36733. }
  36734. /*
  36735. * Helper for C function call negative return values.
  36736. */
  36737. DUK_INTERNAL void duk_error_throw_from_negative_rc(duk_hthread *thr, duk_ret_t rc) {
  36738. duk_context *ctx = (duk_context *) thr;
  36739. const char *msg;
  36740. duk_errcode_t code;
  36741. DUK_ASSERT(thr != NULL);
  36742. DUK_ASSERT(rc < 0);
  36743. /* XXX: this generates quite large code - perhaps select the error
  36744. * class based on the code and then just use the error 'name'?
  36745. */
  36746. /* XXX: shared strings */
  36747. code = -rc;
  36748. switch (rc) {
  36749. case DUK_RET_UNIMPLEMENTED_ERROR: msg = "unimplemented"; break;
  36750. case DUK_RET_UNSUPPORTED_ERROR: msg = "unsupported"; break;
  36751. case DUK_RET_INTERNAL_ERROR: msg = "internal"; break;
  36752. case DUK_RET_ALLOC_ERROR: msg = "alloc"; break;
  36753. case DUK_RET_ASSERTION_ERROR: msg = "assertion"; break;
  36754. case DUK_RET_API_ERROR: msg = "api"; break;
  36755. case DUK_RET_UNCAUGHT_ERROR: msg = "uncaught"; break;
  36756. case DUK_RET_ERROR: msg = "error"; break;
  36757. case DUK_RET_EVAL_ERROR: msg = "eval"; break;
  36758. case DUK_RET_RANGE_ERROR: msg = "range"; break;
  36759. case DUK_RET_REFERENCE_ERROR: msg = "reference"; break;
  36760. case DUK_RET_SYNTAX_ERROR: msg = "syntax"; break;
  36761. case DUK_RET_TYPE_ERROR: msg = "type"; break;
  36762. case DUK_RET_URI_ERROR: msg = "uri"; break;
  36763. default: msg = "unknown"; break;
  36764. }
  36765. DUK_ASSERT(msg != NULL);
  36766. /*
  36767. * The __FILE__ and __LINE__ information is intentionally not used in the
  36768. * creation of the error object, as it isn't useful in the tracedata. The
  36769. * tracedata still contains the function which returned the negative return
  36770. * code, and having the file/line of this function isn't very useful.
  36771. */
  36772. duk_error_raw(ctx, code, NULL, 0, "%s error (rc %ld)", (const char *) msg, (long) rc);
  36773. DUK_UNREACHABLE();
  36774. }
  36775. #line 1 "duk_hbuffer_alloc.c"
  36776. /*
  36777. * duk_hbuffer allocation and freeing.
  36778. */
  36779. /* include removed: duk_internal.h */
  36780. /* Allocate a new duk_hbuffer of a certain type and return a pointer to it
  36781. * (NULL on error). Write buffer data pointer to 'out_bufdata' (only if
  36782. * allocation successful).
  36783. */
  36784. DUK_INTERNAL duk_hbuffer *duk_hbuffer_alloc(duk_heap *heap, duk_size_t size, duk_small_uint_t flags, void **out_bufdata) {
  36785. duk_hbuffer *res = NULL;
  36786. duk_size_t header_size;
  36787. duk_size_t alloc_size;
  36788. DUK_ASSERT(heap != NULL);
  36789. DUK_ASSERT(out_bufdata != NULL);
  36790. DUK_DDD(DUK_DDDPRINT("allocate hbuffer"));
  36791. /* Size sanity check. Should not be necessary because caller is
  36792. * required to check this, but we don't want to cause a segfault
  36793. * if the size wraps either in duk_size_t computation or when
  36794. * storing the size in a 16-bit field.
  36795. */
  36796. if (size > DUK_HBUFFER_MAX_BYTELEN) {
  36797. DUK_D(DUK_DPRINT("hbuffer alloc failed: size too large: %ld", (long) size));
  36798. return NULL; /* no need to write 'out_bufdata' */
  36799. }
  36800. if (flags & DUK_BUF_FLAG_EXTERNAL) {
  36801. header_size = sizeof(duk_hbuffer_external);
  36802. alloc_size = sizeof(duk_hbuffer_external);
  36803. } if (flags & DUK_BUF_FLAG_DYNAMIC) {
  36804. header_size = sizeof(duk_hbuffer_dynamic);
  36805. alloc_size = sizeof(duk_hbuffer_dynamic);
  36806. } else {
  36807. header_size = sizeof(duk_hbuffer_fixed);
  36808. alloc_size = sizeof(duk_hbuffer_fixed) + size;
  36809. DUK_ASSERT(alloc_size >= sizeof(duk_hbuffer_fixed)); /* no wrapping */
  36810. }
  36811. res = (duk_hbuffer *) DUK_ALLOC(heap, alloc_size);
  36812. if (!res) {
  36813. goto error;
  36814. }
  36815. /* zero everything unless requested not to do so */
  36816. #if defined(DUK_USE_ZERO_BUFFER_DATA)
  36817. DUK_MEMZERO((void *) res,
  36818. (flags & DUK_BUF_FLAG_NOZERO) ? header_size : alloc_size);
  36819. #else
  36820. DUK_MEMZERO((void *) res, header_size);
  36821. #endif
  36822. if (flags & DUK_BUF_FLAG_EXTERNAL) {
  36823. duk_hbuffer_external *h;
  36824. h = (duk_hbuffer_external *) res;
  36825. DUK_UNREF(h);
  36826. *out_bufdata = NULL;
  36827. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  36828. #if defined(DUK_USE_HEAPPTR16)
  36829. /* the compressed pointer is zeroed which maps to NULL, so nothing to do. */
  36830. #else
  36831. DUK_HBUFFER_EXTERNAL_SET_DATA_PTR(heap, h, NULL);
  36832. #endif
  36833. #endif
  36834. DUK_ASSERT(DUK_HBUFFER_EXTERNAL_GET_DATA_PTR(heap, h) == NULL);
  36835. } else if (flags & DUK_BUF_FLAG_DYNAMIC) {
  36836. duk_hbuffer_dynamic *h = (duk_hbuffer_dynamic *) res;
  36837. void *ptr;
  36838. if (size > 0) {
  36839. DUK_ASSERT(!(flags & DUK_BUF_FLAG_EXTERNAL)); /* alloc external with size zero */
  36840. DUK_DDD(DUK_DDDPRINT("dynamic buffer with nonzero size, alloc actual buffer"));
  36841. #ifdef DUK_USE_ZERO_BUFFER_DATA
  36842. ptr = DUK_ALLOC_ZEROED(heap, size);
  36843. #else
  36844. ptr = DUK_ALLOC(heap, size);
  36845. #endif
  36846. if (!ptr) {
  36847. /* Because size > 0, NULL check is correct */
  36848. goto error;
  36849. }
  36850. *out_bufdata = ptr;
  36851. DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(heap, h, ptr);
  36852. } else {
  36853. *out_bufdata = NULL;
  36854. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  36855. #if defined(DUK_USE_HEAPPTR16)
  36856. /* the compressed pointer is zeroed which maps to NULL, so nothing to do. */
  36857. #else
  36858. DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(heap, h, NULL);
  36859. #endif
  36860. #endif
  36861. DUK_ASSERT(DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap, h) == NULL);
  36862. }
  36863. } else {
  36864. *out_bufdata = (void *) ((duk_hbuffer_fixed *) res + 1);
  36865. }
  36866. DUK_HBUFFER_SET_SIZE(res, size);
  36867. DUK_HEAPHDR_SET_TYPE(&res->hdr, DUK_HTYPE_BUFFER);
  36868. if (flags & DUK_BUF_FLAG_DYNAMIC) {
  36869. DUK_HBUFFER_SET_DYNAMIC(res);
  36870. if (flags & DUK_BUF_FLAG_EXTERNAL) {
  36871. DUK_HBUFFER_SET_EXTERNAL(res);
  36872. }
  36873. } else {
  36874. DUK_ASSERT(!(flags & DUK_BUF_FLAG_EXTERNAL));
  36875. }
  36876. DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap, &res->hdr);
  36877. DUK_DDD(DUK_DDDPRINT("allocated hbuffer: %p", (void *) res));
  36878. return res;
  36879. error:
  36880. DUK_DD(DUK_DDPRINT("hbuffer allocation failed"));
  36881. DUK_FREE(heap, res);
  36882. return NULL; /* no need to write 'out_bufdata' */
  36883. }
  36884. /* For indirect allocs. */
  36885. DUK_INTERNAL void *duk_hbuffer_get_dynalloc_ptr(duk_heap *heap, void *ud) {
  36886. duk_hbuffer_dynamic *buf = (duk_hbuffer_dynamic *) ud;
  36887. DUK_UNREF(heap);
  36888. return (void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap, buf);
  36889. }
  36890. #line 1 "duk_hbuffer_ops.c"
  36891. /*
  36892. * duk_hbuffer operations such as resizing and inserting/appending data to
  36893. * a dynamic buffer.
  36894. *
  36895. * Append operations append to the end of the buffer and they are relatively
  36896. * efficient: the buffer is grown with a "spare" part relative to the buffer
  36897. * size to minimize reallocations. Insert operations need to move existing
  36898. * data forward in the buffer with memmove() and are not very efficient.
  36899. * They are used e.g. by the regexp compiler to "backpatch" regexp bytecode.
  36900. */
  36901. /* include removed: duk_internal.h */
  36902. /*
  36903. * Resizing
  36904. */
  36905. DUK_INTERNAL void duk_hbuffer_resize(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t new_size) {
  36906. void *res;
  36907. duk_size_t prev_size;
  36908. DUK_ASSERT(thr != NULL);
  36909. DUK_ASSERT(buf != NULL);
  36910. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  36911. DUK_ASSERT(!DUK_HBUFFER_HAS_EXTERNAL(buf));
  36912. /*
  36913. * Maximum size check
  36914. */
  36915. if (new_size > DUK_HBUFFER_MAX_BYTELEN) {
  36916. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, "buffer too long");
  36917. }
  36918. /*
  36919. * Note: use indirect realloc variant just in case mark-and-sweep
  36920. * (finalizers) might resize this same buffer during garbage
  36921. * collection.
  36922. */
  36923. res = DUK_REALLOC_INDIRECT(thr->heap, duk_hbuffer_get_dynalloc_ptr, (void *) buf, new_size);
  36924. if (res != NULL || new_size == 0) {
  36925. /* 'res' may be NULL if new allocation size is 0. */
  36926. DUK_DDD(DUK_DDDPRINT("resized dynamic buffer %p:%ld -> %p:%ld",
  36927. (void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, buf),
  36928. (long) DUK_HBUFFER_DYNAMIC_GET_SIZE(buf),
  36929. (void *) res,
  36930. (long) new_size));
  36931. /*
  36932. * The entire allocated buffer area, regardless of actual used
  36933. * size, is kept zeroed in resizes for simplicity. If the buffer
  36934. * is grown, zero the new part.
  36935. */
  36936. prev_size = DUK_HBUFFER_DYNAMIC_GET_SIZE(buf);
  36937. if (new_size > prev_size) {
  36938. DUK_ASSERT(new_size - prev_size > 0);
  36939. #ifdef DUK_USE_ZERO_BUFFER_DATA
  36940. DUK_MEMZERO((void *) ((char *) res + prev_size),
  36941. (duk_size_t) (new_size - prev_size));
  36942. #endif
  36943. }
  36944. DUK_HBUFFER_DYNAMIC_SET_SIZE(buf, new_size);
  36945. DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(thr->heap, buf, res);
  36946. } else {
  36947. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, "buffer resize failed: %ld to %ld",
  36948. (long) DUK_HBUFFER_DYNAMIC_GET_SIZE(buf),
  36949. (long) new_size);
  36950. }
  36951. DUK_ASSERT(res != NULL || new_size == 0);
  36952. }
  36953. DUK_INTERNAL void duk_hbuffer_reset(duk_hthread *thr, duk_hbuffer_dynamic *buf) {
  36954. DUK_ASSERT(thr != NULL);
  36955. DUK_ASSERT(buf != NULL);
  36956. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  36957. DUK_ASSERT(!DUK_HBUFFER_HAS_EXTERNAL(buf));
  36958. duk_hbuffer_resize(thr, buf, 0);
  36959. }
  36960. /* include removed: duk_internal.h */
  36961. #line 2 "duk_hbufferobject_misc.c"
  36962. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  36963. DUK_INTERNAL duk_uint_t duk_hbufferobject_clamp_bytelength(duk_hbufferobject *h_bufobj, duk_uint_t len) {
  36964. duk_uint_t buf_size;
  36965. duk_uint_t buf_avail;
  36966. DUK_ASSERT(h_bufobj != NULL);
  36967. DUK_ASSERT(h_bufobj->buf != NULL);
  36968. buf_size = (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_bufobj->buf);
  36969. if (h_bufobj->offset > buf_size) {
  36970. /* Slice starting point is beyond current length. */
  36971. return 0;
  36972. }
  36973. buf_avail = buf_size - h_bufobj->offset;
  36974. return buf_avail >= len ? len : buf_avail;
  36975. }
  36976. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  36977. #line 1 "duk_heap_alloc.c"
  36978. /*
  36979. * duk_heap allocation and freeing.
  36980. */
  36981. /* include removed: duk_internal.h */
  36982. /* constants for built-in string data depacking */
  36983. #define DUK__BITPACK_LETTER_LIMIT 26
  36984. #define DUK__BITPACK_UNDERSCORE 26
  36985. #define DUK__BITPACK_FF 27
  36986. #define DUK__BITPACK_SWITCH1 29
  36987. #define DUK__BITPACK_SWITCH 30
  36988. #define DUK__BITPACK_SEVENBIT 31
  36989. /*
  36990. * Free a heap object.
  36991. *
  36992. * Free heap object and its internal (non-heap) pointers. Assumes that
  36993. * caller has removed the object from heap allocated list or the string
  36994. * intern table, and any weak references (which strings may have) have
  36995. * been already dealt with.
  36996. */
  36997. DUK_INTERNAL void duk_free_hobject_inner(duk_heap *heap, duk_hobject *h) {
  36998. DUK_ASSERT(heap != NULL);
  36999. DUK_ASSERT(h != NULL);
  37000. DUK_FREE(heap, DUK_HOBJECT_GET_PROPS(heap, h));
  37001. if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h)) {
  37002. duk_hcompiledfunction *f = (duk_hcompiledfunction *) h;
  37003. DUK_UNREF(f);
  37004. /* Currently nothing to free; 'data' is a heap object */
  37005. } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
  37006. duk_hnativefunction *f = (duk_hnativefunction *) h;
  37007. DUK_UNREF(f);
  37008. /* Currently nothing to free */
  37009. } else if (DUK_HOBJECT_IS_THREAD(h)) {
  37010. duk_hthread *t = (duk_hthread *) h;
  37011. DUK_FREE(heap, t->valstack);
  37012. DUK_FREE(heap, t->callstack);
  37013. DUK_FREE(heap, t->catchstack);
  37014. /* Don't free h->resumer because it exists in the heap.
  37015. * Callstack entries also contain function pointers which
  37016. * are not freed for the same reason.
  37017. */
  37018. /* XXX: with 'caller' property the callstack would need
  37019. * to be unwound to update the 'caller' properties of
  37020. * functions in the callstack.
  37021. */
  37022. }
  37023. }
  37024. DUK_INTERNAL void duk_free_hbuffer_inner(duk_heap *heap, duk_hbuffer *h) {
  37025. DUK_ASSERT(heap != NULL);
  37026. DUK_ASSERT(h != NULL);
  37027. if (DUK_HBUFFER_HAS_DYNAMIC(h) && !DUK_HBUFFER_HAS_EXTERNAL(h)) {
  37028. duk_hbuffer_dynamic *g = (duk_hbuffer_dynamic *) h;
  37029. DUK_DDD(DUK_DDDPRINT("free dynamic buffer %p", (void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap, g)));
  37030. DUK_FREE(heap, DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap, g));
  37031. }
  37032. }
  37033. DUK_INTERNAL void duk_free_hstring_inner(duk_heap *heap, duk_hstring *h) {
  37034. DUK_ASSERT(heap != NULL);
  37035. DUK_ASSERT(h != NULL);
  37036. DUK_UNREF(heap);
  37037. DUK_UNREF(h);
  37038. #if defined(DUK_USE_HSTRING_EXTDATA) && defined(DUK_USE_EXTSTR_FREE)
  37039. if (DUK_HSTRING_HAS_EXTDATA(h)) {
  37040. DUK_DDD(DUK_DDDPRINT("free extstr: hstring %!O, extdata: %p",
  37041. h, DUK_HSTRING_GET_EXTDATA((duk_hstring_external *) h)));
  37042. DUK_USE_EXTSTR_FREE(heap->heap_udata, (const void *) DUK_HSTRING_GET_EXTDATA((duk_hstring_external *) h));
  37043. }
  37044. #endif
  37045. }
  37046. DUK_INTERNAL void duk_heap_free_heaphdr_raw(duk_heap *heap, duk_heaphdr *hdr) {
  37047. DUK_ASSERT(heap);
  37048. DUK_ASSERT(hdr);
  37049. DUK_DDD(DUK_DDDPRINT("free heaphdr %p, htype %ld", (void *) hdr, (long) DUK_HEAPHDR_GET_TYPE(hdr)));
  37050. switch ((int) DUK_HEAPHDR_GET_TYPE(hdr)) {
  37051. case DUK_HTYPE_STRING:
  37052. duk_free_hstring_inner(heap, (duk_hstring *) hdr);
  37053. break;
  37054. case DUK_HTYPE_OBJECT:
  37055. duk_free_hobject_inner(heap, (duk_hobject *) hdr);
  37056. break;
  37057. case DUK_HTYPE_BUFFER:
  37058. duk_free_hbuffer_inner(heap, (duk_hbuffer *) hdr);
  37059. break;
  37060. default:
  37061. DUK_UNREACHABLE();
  37062. }
  37063. DUK_FREE(heap, hdr);
  37064. }
  37065. /*
  37066. * Free the heap.
  37067. *
  37068. * Frees heap-related non-heap-tracked allocations such as the
  37069. * string intern table; then frees the heap allocated objects;
  37070. * and finally frees the heap structure itself. Reference counts
  37071. * and GC markers are ignored (and not updated) in this process,
  37072. * and finalizers won't be called.
  37073. *
  37074. * The heap pointer and heap object pointers must not be used
  37075. * after this call.
  37076. */
  37077. DUK_LOCAL void duk__free_allocated(duk_heap *heap) {
  37078. duk_heaphdr *curr;
  37079. duk_heaphdr *next;
  37080. curr = heap->heap_allocated;
  37081. while (curr) {
  37082. /* We don't log or warn about freeing zero refcount objects
  37083. * because they may happen with finalizer processing.
  37084. */
  37085. DUK_DDD(DUK_DDDPRINT("FINALFREE (allocated): %!iO",
  37086. (duk_heaphdr *) curr));
  37087. next = DUK_HEAPHDR_GET_NEXT(heap, curr);
  37088. duk_heap_free_heaphdr_raw(heap, curr);
  37089. curr = next;
  37090. }
  37091. }
  37092. #ifdef DUK_USE_REFERENCE_COUNTING
  37093. DUK_LOCAL void duk__free_refzero_list(duk_heap *heap) {
  37094. duk_heaphdr *curr;
  37095. duk_heaphdr *next;
  37096. curr = heap->refzero_list;
  37097. while (curr) {
  37098. DUK_DDD(DUK_DDDPRINT("FINALFREE (refzero_list): %!iO",
  37099. (duk_heaphdr *) curr));
  37100. next = DUK_HEAPHDR_GET_NEXT(heap, curr);
  37101. duk_heap_free_heaphdr_raw(heap, curr);
  37102. curr = next;
  37103. }
  37104. }
  37105. #endif
  37106. #ifdef DUK_USE_MARK_AND_SWEEP
  37107. DUK_LOCAL void duk__free_markandsweep_finalize_list(duk_heap *heap) {
  37108. duk_heaphdr *curr;
  37109. duk_heaphdr *next;
  37110. curr = heap->finalize_list;
  37111. while (curr) {
  37112. DUK_DDD(DUK_DDDPRINT("FINALFREE (finalize_list): %!iO",
  37113. (duk_heaphdr *) curr));
  37114. next = DUK_HEAPHDR_GET_NEXT(heap, curr);
  37115. duk_heap_free_heaphdr_raw(heap, curr);
  37116. curr = next;
  37117. }
  37118. }
  37119. #endif
  37120. DUK_LOCAL void duk__free_stringtable(duk_heap *heap) {
  37121. /* strings are only tracked by stringtable */
  37122. duk_heap_free_strtab(heap);
  37123. }
  37124. DUK_LOCAL void duk__free_run_finalizers(duk_heap *heap) {
  37125. duk_hthread *thr;
  37126. duk_heaphdr *curr;
  37127. #ifdef DUK_USE_DEBUG
  37128. duk_size_t count_obj = 0;
  37129. #endif
  37130. DUK_ASSERT(heap != NULL);
  37131. DUK_ASSERT(heap->heap_thread != NULL);
  37132. #ifdef DUK_USE_REFERENCE_COUNTING
  37133. DUK_ASSERT(heap->refzero_list == NULL); /* refzero not running -> must be empty */
  37134. #endif
  37135. #ifdef DUK_USE_MARK_AND_SWEEP
  37136. DUK_ASSERT(heap->finalize_list == NULL); /* mark-and-sweep not running -> must be empty */
  37137. #endif
  37138. /* XXX: here again finalizer thread is the heap_thread which needs
  37139. * to be coordinated with finalizer thread fixes.
  37140. */
  37141. thr = heap->heap_thread;
  37142. DUK_ASSERT(thr != NULL);
  37143. curr = heap->heap_allocated;
  37144. while (curr) {
  37145. if (DUK_HEAPHDR_GET_TYPE(curr) == DUK_HTYPE_OBJECT) {
  37146. /* Only objects in heap_allocated may have finalizers. Check that
  37147. * the object itself has a _Finalizer property so that we don't
  37148. * execute finalizers for e.g. Proxy objects.
  37149. */
  37150. DUK_ASSERT(thr != NULL);
  37151. DUK_ASSERT(curr != NULL);
  37152. if (duk_hobject_hasprop_raw(thr, (duk_hobject *) curr, DUK_HTHREAD_STRING_INT_FINALIZER(thr))) {
  37153. duk_hobject_run_finalizer(thr, (duk_hobject *) curr);
  37154. }
  37155. #ifdef DUK_USE_DEBUG
  37156. count_obj++;
  37157. #endif
  37158. }
  37159. curr = DUK_HEAPHDR_GET_NEXT(heap, curr);
  37160. }
  37161. /* Note: count includes all objects, not only those with an actual finalizer. */
  37162. #ifdef DUK_USE_DEBUG
  37163. DUK_D(DUK_DPRINT("checked %ld objects for finalizers before freeing heap", (long) count_obj));
  37164. #endif
  37165. }
  37166. DUK_INTERNAL void duk_heap_free(duk_heap *heap) {
  37167. DUK_D(DUK_DPRINT("free heap: %p", (void *) heap));
  37168. #if defined(DUK_USE_DEBUG)
  37169. duk_heap_dump_strtab(heap);
  37170. #endif
  37171. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  37172. /* Detach a debugger if attached (can be called multiple times)
  37173. * safely.
  37174. */
  37175. duk_debug_do_detach(heap);
  37176. #endif
  37177. /* Execute finalizers before freeing the heap, even for reachable
  37178. * objects, and regardless of whether or not mark-and-sweep is
  37179. * enabled. This gives finalizers the chance to free any native
  37180. * resources like file handles, allocations made outside Duktape,
  37181. * etc.
  37182. *
  37183. * XXX: this perhaps requires an execution time limit.
  37184. */
  37185. DUK_D(DUK_DPRINT("execute finalizers before freeing heap"));
  37186. #ifdef DUK_USE_MARK_AND_SWEEP
  37187. /* run mark-and-sweep a few times just in case (unreachable
  37188. * object finalizers run already here)
  37189. */
  37190. duk_heap_mark_and_sweep(heap, 0);
  37191. duk_heap_mark_and_sweep(heap, 0);
  37192. #endif
  37193. duk__free_run_finalizers(heap);
  37194. /* Note: heap->heap_thread, heap->curr_thread, and heap->heap_object
  37195. * are on the heap allocated list.
  37196. */
  37197. DUK_D(DUK_DPRINT("freeing heap objects of heap: %p", (void *) heap));
  37198. duk__free_allocated(heap);
  37199. #ifdef DUK_USE_REFERENCE_COUNTING
  37200. DUK_D(DUK_DPRINT("freeing refzero list of heap: %p", (void *) heap));
  37201. duk__free_refzero_list(heap);
  37202. #endif
  37203. #ifdef DUK_USE_MARK_AND_SWEEP
  37204. DUK_D(DUK_DPRINT("freeing mark-and-sweep finalize list of heap: %p", (void *) heap));
  37205. duk__free_markandsweep_finalize_list(heap);
  37206. #endif
  37207. DUK_D(DUK_DPRINT("freeing string table of heap: %p", (void *) heap));
  37208. duk__free_stringtable(heap);
  37209. DUK_D(DUK_DPRINT("freeing heap structure: %p", (void *) heap));
  37210. heap->free_func(heap->heap_udata, heap);
  37211. }
  37212. /*
  37213. * Allocate a heap.
  37214. *
  37215. * String table is initialized with built-in strings from genstrings.py.
  37216. */
  37217. /* intern built-in strings from precooked data (genstrings.py) */
  37218. DUK_LOCAL duk_bool_t duk__init_heap_strings(duk_heap *heap) {
  37219. duk_bitdecoder_ctx bd_ctx;
  37220. duk_bitdecoder_ctx *bd = &bd_ctx; /* convenience */
  37221. duk_small_uint_t i, j;
  37222. DUK_MEMZERO(&bd_ctx, sizeof(bd_ctx));
  37223. bd->data = (const duk_uint8_t *) duk_strings_data;
  37224. bd->length = (duk_size_t) DUK_STRDATA_DATA_LENGTH;
  37225. for (i = 0; i < DUK_HEAP_NUM_STRINGS; i++) {
  37226. duk_uint8_t tmp[DUK_STRDATA_MAX_STRLEN];
  37227. duk_hstring *h;
  37228. duk_small_uint_t len;
  37229. duk_small_uint_t mode;
  37230. duk_small_uint_t t;
  37231. len = duk_bd_decode(bd, 5);
  37232. mode = 32; /* 0 = uppercase, 32 = lowercase (= 'a' - 'A') */
  37233. for (j = 0; j < len; j++) {
  37234. t = duk_bd_decode(bd, 5);
  37235. if (t < DUK__BITPACK_LETTER_LIMIT) {
  37236. t = t + DUK_ASC_UC_A + mode;
  37237. } else if (t == DUK__BITPACK_UNDERSCORE) {
  37238. t = DUK_ASC_UNDERSCORE;
  37239. } else if (t == DUK__BITPACK_FF) {
  37240. /* Internal keys are prefixed with 0xFF in the stringtable
  37241. * (which makes them invalid UTF-8 on purpose).
  37242. */
  37243. t = 0xff;
  37244. } else if (t == DUK__BITPACK_SWITCH1) {
  37245. t = duk_bd_decode(bd, 5);
  37246. DUK_ASSERT_DISABLE(t >= 0); /* unsigned */
  37247. DUK_ASSERT(t <= 25);
  37248. t = t + DUK_ASC_UC_A + (mode ^ 32);
  37249. } else if (t == DUK__BITPACK_SWITCH) {
  37250. mode = mode ^ 32;
  37251. t = duk_bd_decode(bd, 5);
  37252. DUK_ASSERT_DISABLE(t >= 0);
  37253. DUK_ASSERT(t <= 25);
  37254. t = t + DUK_ASC_UC_A + mode;
  37255. } else if (t == DUK__BITPACK_SEVENBIT) {
  37256. t = duk_bd_decode(bd, 7);
  37257. }
  37258. tmp[j] = (duk_uint8_t) t;
  37259. }
  37260. /* No need to length check string: it will never exceed even
  37261. * the 16-bit length maximum.
  37262. */
  37263. DUK_ASSERT(len <= 0xffffUL);
  37264. DUK_DDD(DUK_DDDPRINT("intern built-in string %ld", (long) i));
  37265. h = duk_heap_string_intern(heap, tmp, len);
  37266. if (!h) {
  37267. goto error;
  37268. }
  37269. /* Special flags checks. Since these strings are always
  37270. * reachable and a string cannot appear twice in the string
  37271. * table, there's no need to check/set these flags elsewhere.
  37272. * The 'internal' flag is set by string intern code.
  37273. */
  37274. if (i == DUK_STRIDX_EVAL || i == DUK_STRIDX_LC_ARGUMENTS) {
  37275. DUK_HSTRING_SET_EVAL_OR_ARGUMENTS(h);
  37276. }
  37277. if (i >= DUK_STRIDX_START_RESERVED && i < DUK_STRIDX_END_RESERVED) {
  37278. DUK_HSTRING_SET_RESERVED_WORD(h);
  37279. if (i >= DUK_STRIDX_START_STRICT_RESERVED) {
  37280. DUK_HSTRING_SET_STRICT_RESERVED_WORD(h);
  37281. }
  37282. }
  37283. DUK_DDD(DUK_DDDPRINT("interned: %!O", (duk_heaphdr *) h));
  37284. /* XXX: The incref macro takes a thread pointer but doesn't
  37285. * use it right now.
  37286. */
  37287. DUK_HSTRING_INCREF(_never_referenced_, h);
  37288. #if defined(DUK_USE_HEAPPTR16)
  37289. heap->strs16[i] = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) h);
  37290. #else
  37291. heap->strs[i] = h;
  37292. #endif
  37293. }
  37294. return 1;
  37295. error:
  37296. return 0;
  37297. }
  37298. DUK_LOCAL duk_bool_t duk__init_heap_thread(duk_heap *heap) {
  37299. duk_hthread *thr;
  37300. DUK_DD(DUK_DDPRINT("heap init: alloc heap thread"));
  37301. thr = duk_hthread_alloc(heap,
  37302. DUK_HOBJECT_FLAG_EXTENSIBLE |
  37303. DUK_HOBJECT_FLAG_THREAD |
  37304. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_THREAD));
  37305. if (!thr) {
  37306. DUK_D(DUK_DPRINT("failed to alloc heap_thread"));
  37307. return 0;
  37308. }
  37309. thr->state = DUK_HTHREAD_STATE_INACTIVE;
  37310. #if defined(DUK_USE_HEAPPTR16)
  37311. thr->strs16 = heap->strs16;
  37312. #else
  37313. thr->strs = heap->strs;
  37314. #endif
  37315. heap->heap_thread = thr;
  37316. DUK_HTHREAD_INCREF(thr, thr); /* Note: first argument not really used */
  37317. /* 'thr' is now reachable */
  37318. if (!duk_hthread_init_stacks(heap, thr)) {
  37319. return 0;
  37320. }
  37321. /* XXX: this may now fail, and is not handled correctly */
  37322. duk_hthread_create_builtin_objects(thr);
  37323. /* default prototype (Note: 'thr' must be reachable) */
  37324. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) thr, thr->builtins[DUK_BIDX_THREAD_PROTOTYPE]);
  37325. return 1;
  37326. }
  37327. #ifdef DUK_USE_DEBUG
  37328. #define DUK__DUMPSZ(t) do { \
  37329. DUK_D(DUK_DPRINT("" #t "=%ld", (long) sizeof(t))); \
  37330. } while (0)
  37331. /* These is not 100% because format would need to be non-portable "long long".
  37332. * Also print out as doubles to catch cases where the "long" type is not wide
  37333. * enough; the limits will then not be printed accurately but the magnitude
  37334. * will be correct.
  37335. */
  37336. #define DUK__DUMPLM_SIGNED_RAW(t,a,b) do { \
  37337. DUK_D(DUK_DPRINT(t "=[%ld,%ld]=[%lf,%lf]", \
  37338. (long) (a), (long) (b), \
  37339. (double) (a), (double) (b))); \
  37340. } while(0)
  37341. #define DUK__DUMPLM_UNSIGNED_RAW(t,a,b) do { \
  37342. DUK_D(DUK_DPRINT(t "=[%lu,%lu]=[%lf,%lf]", \
  37343. (unsigned long) (a), (unsigned long) (b), \
  37344. (double) (a), (double) (b))); \
  37345. } while(0)
  37346. #define DUK__DUMPLM_SIGNED(t) do { \
  37347. DUK__DUMPLM_SIGNED_RAW("DUK_" #t "_{MIN,MAX}", DUK_##t##_MIN, DUK_##t##_MAX); \
  37348. } while(0)
  37349. #define DUK__DUMPLM_UNSIGNED(t) do { \
  37350. DUK__DUMPLM_UNSIGNED_RAW("DUK_" #t "_{MIN,MAX}", DUK_##t##_MIN, DUK_##t##_MAX); \
  37351. } while(0)
  37352. DUK_LOCAL void duk__dump_type_sizes(void) {
  37353. DUK_D(DUK_DPRINT("sizeof()"));
  37354. /* basic platform types */
  37355. DUK__DUMPSZ(char);
  37356. DUK__DUMPSZ(short);
  37357. DUK__DUMPSZ(int);
  37358. DUK__DUMPSZ(long);
  37359. DUK__DUMPSZ(double);
  37360. DUK__DUMPSZ(void *);
  37361. DUK__DUMPSZ(size_t);
  37362. /* basic types from duk_features.h */
  37363. DUK__DUMPSZ(duk_uint8_t);
  37364. DUK__DUMPSZ(duk_int8_t);
  37365. DUK__DUMPSZ(duk_uint16_t);
  37366. DUK__DUMPSZ(duk_int16_t);
  37367. DUK__DUMPSZ(duk_uint32_t);
  37368. DUK__DUMPSZ(duk_int32_t);
  37369. DUK__DUMPSZ(duk_uint64_t);
  37370. DUK__DUMPSZ(duk_int64_t);
  37371. DUK__DUMPSZ(duk_uint_least8_t);
  37372. DUK__DUMPSZ(duk_int_least8_t);
  37373. DUK__DUMPSZ(duk_uint_least16_t);
  37374. DUK__DUMPSZ(duk_int_least16_t);
  37375. DUK__DUMPSZ(duk_uint_least32_t);
  37376. DUK__DUMPSZ(duk_int_least32_t);
  37377. #if defined(DUK_USE_64BIT_OPS)
  37378. DUK__DUMPSZ(duk_uint_least64_t);
  37379. DUK__DUMPSZ(duk_int_least64_t);
  37380. #endif
  37381. DUK__DUMPSZ(duk_uint_fast8_t);
  37382. DUK__DUMPSZ(duk_int_fast8_t);
  37383. DUK__DUMPSZ(duk_uint_fast16_t);
  37384. DUK__DUMPSZ(duk_int_fast16_t);
  37385. DUK__DUMPSZ(duk_uint_fast32_t);
  37386. DUK__DUMPSZ(duk_int_fast32_t);
  37387. #if defined(DUK_USE_64BIT_OPS)
  37388. DUK__DUMPSZ(duk_uint_fast64_t);
  37389. DUK__DUMPSZ(duk_int_fast64_t);
  37390. #endif
  37391. DUK__DUMPSZ(duk_uintptr_t);
  37392. DUK__DUMPSZ(duk_intptr_t);
  37393. DUK__DUMPSZ(duk_uintmax_t);
  37394. DUK__DUMPSZ(duk_intmax_t);
  37395. DUK__DUMPSZ(duk_double_t);
  37396. /* important chosen base types */
  37397. DUK__DUMPSZ(duk_int_t);
  37398. DUK__DUMPSZ(duk_uint_t);
  37399. DUK__DUMPSZ(duk_int_fast_t);
  37400. DUK__DUMPSZ(duk_uint_fast_t);
  37401. DUK__DUMPSZ(duk_small_int_t);
  37402. DUK__DUMPSZ(duk_small_uint_t);
  37403. DUK__DUMPSZ(duk_small_int_fast_t);
  37404. DUK__DUMPSZ(duk_small_uint_fast_t);
  37405. /* some derived types */
  37406. DUK__DUMPSZ(duk_codepoint_t);
  37407. DUK__DUMPSZ(duk_ucodepoint_t);
  37408. DUK__DUMPSZ(duk_idx_t);
  37409. DUK__DUMPSZ(duk_errcode_t);
  37410. DUK__DUMPSZ(duk_uarridx_t);
  37411. /* tval */
  37412. DUK__DUMPSZ(duk_double_union);
  37413. DUK__DUMPSZ(duk_tval);
  37414. /* structs from duk_forwdecl.h */
  37415. DUK__DUMPSZ(duk_jmpbuf);
  37416. DUK__DUMPSZ(duk_heaphdr);
  37417. DUK__DUMPSZ(duk_heaphdr_string);
  37418. DUK__DUMPSZ(duk_hstring);
  37419. DUK__DUMPSZ(duk_hstring_external);
  37420. DUK__DUMPSZ(duk_hobject);
  37421. DUK__DUMPSZ(duk_hcompiledfunction);
  37422. DUK__DUMPSZ(duk_hnativefunction);
  37423. DUK__DUMPSZ(duk_hthread);
  37424. DUK__DUMPSZ(duk_hbuffer);
  37425. DUK__DUMPSZ(duk_hbuffer_fixed);
  37426. DUK__DUMPSZ(duk_hbuffer_dynamic);
  37427. DUK__DUMPSZ(duk_hbuffer_external);
  37428. DUK__DUMPSZ(duk_propaccessor);
  37429. DUK__DUMPSZ(duk_propvalue);
  37430. DUK__DUMPSZ(duk_propdesc);
  37431. DUK__DUMPSZ(duk_heap);
  37432. #if defined(DUK_USE_STRTAB_CHAIN)
  37433. DUK__DUMPSZ(duk_strtab_entry);
  37434. #endif
  37435. DUK__DUMPSZ(duk_activation);
  37436. DUK__DUMPSZ(duk_catcher);
  37437. DUK__DUMPSZ(duk_strcache);
  37438. DUK__DUMPSZ(duk_ljstate);
  37439. DUK__DUMPSZ(duk_fixedbuffer);
  37440. DUK__DUMPSZ(duk_bitdecoder_ctx);
  37441. DUK__DUMPSZ(duk_bitencoder_ctx);
  37442. DUK__DUMPSZ(duk_token);
  37443. DUK__DUMPSZ(duk_re_token);
  37444. DUK__DUMPSZ(duk_lexer_point);
  37445. DUK__DUMPSZ(duk_lexer_ctx);
  37446. DUK__DUMPSZ(duk_compiler_instr);
  37447. DUK__DUMPSZ(duk_compiler_func);
  37448. DUK__DUMPSZ(duk_compiler_ctx);
  37449. DUK__DUMPSZ(duk_re_matcher_ctx);
  37450. DUK__DUMPSZ(duk_re_compiler_ctx);
  37451. }
  37452. DUK_LOCAL void duk__dump_type_limits(void) {
  37453. DUK_D(DUK_DPRINT("limits"));
  37454. /* basic types */
  37455. DUK__DUMPLM_SIGNED(INT8);
  37456. DUK__DUMPLM_UNSIGNED(UINT8);
  37457. DUK__DUMPLM_SIGNED(INT_FAST8);
  37458. DUK__DUMPLM_UNSIGNED(UINT_FAST8);
  37459. DUK__DUMPLM_SIGNED(INT_LEAST8);
  37460. DUK__DUMPLM_UNSIGNED(UINT_LEAST8);
  37461. DUK__DUMPLM_SIGNED(INT16);
  37462. DUK__DUMPLM_UNSIGNED(UINT16);
  37463. DUK__DUMPLM_SIGNED(INT_FAST16);
  37464. DUK__DUMPLM_UNSIGNED(UINT_FAST16);
  37465. DUK__DUMPLM_SIGNED(INT_LEAST16);
  37466. DUK__DUMPLM_UNSIGNED(UINT_LEAST16);
  37467. DUK__DUMPLM_SIGNED(INT32);
  37468. DUK__DUMPLM_UNSIGNED(UINT32);
  37469. DUK__DUMPLM_SIGNED(INT_FAST32);
  37470. DUK__DUMPLM_UNSIGNED(UINT_FAST32);
  37471. DUK__DUMPLM_SIGNED(INT_LEAST32);
  37472. DUK__DUMPLM_UNSIGNED(UINT_LEAST32);
  37473. #if defined(DUK_USE_64BIT_OPS)
  37474. DUK__DUMPLM_SIGNED(INT64);
  37475. DUK__DUMPLM_UNSIGNED(UINT64);
  37476. DUK__DUMPLM_SIGNED(INT_FAST64);
  37477. DUK__DUMPLM_UNSIGNED(UINT_FAST64);
  37478. DUK__DUMPLM_SIGNED(INT_LEAST64);
  37479. DUK__DUMPLM_UNSIGNED(UINT_LEAST64);
  37480. #endif
  37481. DUK__DUMPLM_SIGNED(INTPTR);
  37482. DUK__DUMPLM_UNSIGNED(UINTPTR);
  37483. DUK__DUMPLM_SIGNED(INTMAX);
  37484. DUK__DUMPLM_UNSIGNED(UINTMAX);
  37485. /* derived types */
  37486. DUK__DUMPLM_SIGNED(INT);
  37487. DUK__DUMPLM_UNSIGNED(UINT);
  37488. DUK__DUMPLM_SIGNED(INT_FAST);
  37489. DUK__DUMPLM_UNSIGNED(UINT_FAST);
  37490. DUK__DUMPLM_SIGNED(SMALL_INT);
  37491. DUK__DUMPLM_UNSIGNED(SMALL_UINT);
  37492. DUK__DUMPLM_SIGNED(SMALL_INT_FAST);
  37493. DUK__DUMPLM_UNSIGNED(SMALL_UINT_FAST);
  37494. }
  37495. #undef DUK__DUMPSZ
  37496. #undef DUK__DUMPLM_SIGNED_RAW
  37497. #undef DUK__DUMPLM_UNSIGNED_RAW
  37498. #undef DUK__DUMPLM_SIGNED
  37499. #undef DUK__DUMPLM_UNSIGNED
  37500. DUK_LOCAL void duk__dump_misc_options(void) {
  37501. DUK_D(DUK_DPRINT("DUK_VERSION: %ld", (long) DUK_VERSION));
  37502. DUK_D(DUK_DPRINT("DUK_GIT_DESCRIBE: %s", DUK_GIT_DESCRIBE));
  37503. #if defined(DUK_USE_PACKED_TVAL)
  37504. DUK_D(DUK_DPRINT("DUK_USE_PACKED_TVAL: yes"));
  37505. #else
  37506. DUK_D(DUK_DPRINT("DUK_USE_PACKED_TVAL: no"));
  37507. #endif
  37508. #if defined(DUK_USE_INTEGER_LE)
  37509. DUK_D(DUK_DPRINT("Integer endianness: little"));
  37510. #elif defined(DUK_USE_INTEGER_ME)
  37511. DUK_D(DUK_DPRINT("Integer endianness: mixed"));
  37512. #elif defined(DUK_USE_INTEGER_BE)
  37513. DUK_D(DUK_DPRINT("Integer endianness: big"));
  37514. #else
  37515. DUK_D(DUK_DPRINT("Integer endianness: ???"));
  37516. #endif
  37517. #if defined(DUK_USE_DOUBLE_LE)
  37518. DUK_D(DUK_DPRINT("IEEE double endianness: little"));
  37519. #elif defined(DUK_USE_DOUBLE_ME)
  37520. DUK_D(DUK_DPRINT("IEEE double endianness: mixed"));
  37521. #elif defined(DUK_USE_DOUBLE_BE)
  37522. DUK_D(DUK_DPRINT("IEEE double endianness: big"));
  37523. #else
  37524. DUK_D(DUK_DPRINT("IEEE double endianness: ???"));
  37525. #endif
  37526. }
  37527. #endif /* DUK_USE_DEBUG */
  37528. DUK_INTERNAL
  37529. duk_heap *duk_heap_alloc(duk_alloc_function alloc_func,
  37530. duk_realloc_function realloc_func,
  37531. duk_free_function free_func,
  37532. void *heap_udata,
  37533. duk_fatal_function fatal_func) {
  37534. duk_heap *res = NULL;
  37535. /* Silence a few global unused warnings here. */
  37536. DUK_UNREF(duk_str_unsupported);
  37537. DUK_D(DUK_DPRINT("allocate heap"));
  37538. /*
  37539. * Debug dump type sizes
  37540. */
  37541. #ifdef DUK_USE_DEBUG
  37542. duk__dump_misc_options();
  37543. duk__dump_type_sizes();
  37544. duk__dump_type_limits();
  37545. #endif
  37546. /*
  37547. * If selftests enabled, run them as early as possible
  37548. */
  37549. #ifdef DUK_USE_SELF_TESTS
  37550. DUK_D(DUK_DPRINT("running self tests"));
  37551. duk_selftest_run_tests();
  37552. DUK_D(DUK_DPRINT("self tests passed"));
  37553. #endif
  37554. /*
  37555. * Computed values (e.g. INFINITY)
  37556. */
  37557. #ifdef DUK_USE_COMPUTED_NAN
  37558. do {
  37559. /* Workaround for some exotic platforms where NAN is missing
  37560. * and the expression (0.0 / 0.0) does NOT result in a NaN.
  37561. * Such platforms use the global 'duk_computed_nan' which must
  37562. * be initialized at runtime. Use 'volatile' to ensure that
  37563. * the compiler will actually do the computation and not try
  37564. * to do constant folding which might result in the original
  37565. * problem.
  37566. */
  37567. volatile double dbl1 = 0.0;
  37568. volatile double dbl2 = 0.0;
  37569. duk_computed_nan = dbl1 / dbl2;
  37570. } while (0);
  37571. #endif
  37572. #ifdef DUK_USE_COMPUTED_INFINITY
  37573. do {
  37574. /* Similar workaround for INFINITY. */
  37575. volatile double dbl1 = 1.0;
  37576. volatile double dbl2 = 0.0;
  37577. duk_computed_infinity = dbl1 / dbl2;
  37578. } while (0);
  37579. #endif
  37580. /*
  37581. * Allocate heap struct
  37582. *
  37583. * Use a raw call, all macros expect the heap to be initialized
  37584. */
  37585. res = (duk_heap *) alloc_func(heap_udata, sizeof(duk_heap));
  37586. if (!res) {
  37587. goto error;
  37588. }
  37589. /*
  37590. * Zero the struct, and start initializing roughly in order
  37591. */
  37592. DUK_MEMZERO(res, sizeof(*res));
  37593. /* explicit NULL inits */
  37594. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  37595. res->heap_udata = NULL;
  37596. res->heap_allocated = NULL;
  37597. #ifdef DUK_USE_REFERENCE_COUNTING
  37598. res->refzero_list = NULL;
  37599. res->refzero_list_tail = NULL;
  37600. #endif
  37601. #ifdef DUK_USE_MARK_AND_SWEEP
  37602. res->finalize_list = NULL;
  37603. #endif
  37604. res->heap_thread = NULL;
  37605. res->curr_thread = NULL;
  37606. res->heap_object = NULL;
  37607. #if defined(DUK_USE_STRTAB_CHAIN)
  37608. /* nothing to NULL */
  37609. #elif defined(DUK_USE_STRTAB_PROBE)
  37610. #if defined(DUK_USE_HEAPPTR16)
  37611. res->strtable16 = (duk_uint16_t *) NULL;
  37612. #else
  37613. res->strtable = (duk_hstring **) NULL;
  37614. #endif
  37615. #endif
  37616. #if defined(DUK_USE_HEAPPTR16)
  37617. /* res->strs16[] is zeroed and zero decodes to NULL, so no NULL inits. */
  37618. #else
  37619. {
  37620. duk_small_uint_t i;
  37621. for (i = 0; i < DUK_HEAP_NUM_STRINGS; i++) {
  37622. res->strs[i] = NULL;
  37623. }
  37624. }
  37625. #endif
  37626. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  37627. res->dbg_read_cb = NULL;
  37628. res->dbg_write_cb = NULL;
  37629. res->dbg_peek_cb = NULL;
  37630. res->dbg_read_flush_cb = NULL;
  37631. res->dbg_write_flush_cb = NULL;
  37632. res->dbg_udata = NULL;
  37633. res->dbg_step_thread = NULL;
  37634. #endif
  37635. #endif /* DUK_USE_EXPLICIT_NULL_INIT */
  37636. res->alloc_func = alloc_func;
  37637. res->realloc_func = realloc_func;
  37638. res->free_func = free_func;
  37639. res->heap_udata = heap_udata;
  37640. res->fatal_func = fatal_func;
  37641. #if defined(DUK_USE_HEAPPTR16)
  37642. /* XXX: zero assumption */
  37643. res->heapptr_null16 = DUK_USE_HEAPPTR_ENC16(res->heap_udata, (void *) NULL);
  37644. res->heapptr_deleted16 = DUK_USE_HEAPPTR_ENC16(res->heap_udata, (void *) DUK_STRTAB_DELETED_MARKER(res));
  37645. #endif
  37646. /* res->mark_and_sweep_trigger_counter == 0 -> now causes immediate GC; which is OK */
  37647. res->call_recursion_depth = 0;
  37648. res->call_recursion_limit = DUK_USE_NATIVE_CALL_RECLIMIT;
  37649. /* XXX: use the pointer as a seed for now: mix in time at least */
  37650. /* The casts through duk_intr_pt is to avoid the following GCC warning:
  37651. *
  37652. * warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  37653. *
  37654. * This still generates a /Wp64 warning on VS2010 when compiling for x86.
  37655. */
  37656. res->hash_seed = (duk_uint32_t) (duk_intptr_t) res;
  37657. res->rnd_state = (duk_uint32_t) (duk_intptr_t) res;
  37658. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  37659. res->lj.jmpbuf_ptr = NULL;
  37660. #endif
  37661. DUK_ASSERT(res->lj.type == DUK_LJ_TYPE_UNKNOWN); /* zero */
  37662. DUK_TVAL_SET_UNDEFINED(&res->lj.value1);
  37663. DUK_TVAL_SET_UNDEFINED(&res->lj.value2);
  37664. #if (DUK_STRTAB_INITIAL_SIZE < DUK_UTIL_MIN_HASH_PRIME)
  37665. #error initial heap stringtable size is defined incorrectly
  37666. #endif
  37667. /*
  37668. * Init stringtable: fixed variant
  37669. */
  37670. #if defined(DUK_USE_STRTAB_CHAIN)
  37671. DUK_MEMZERO(res->strtable, sizeof(duk_strtab_entry) * DUK_STRTAB_CHAIN_SIZE);
  37672. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  37673. {
  37674. duk_small_uint_t i;
  37675. for (i = 0; i < DUK_STRTAB_CHAIN_SIZE; i++) {
  37676. #if defined(DUK_USE_HEAPPTR16)
  37677. res->strtable[i].u.str16 = res->heapptr_null16;
  37678. #else
  37679. res->strtable[i].u.str = NULL;
  37680. #endif
  37681. }
  37682. }
  37683. #endif /* DUK_USE_EXPLICIT_NULL_INIT */
  37684. #endif /* DUK_USE_STRTAB_CHAIN */
  37685. /*
  37686. * Init stringtable: probe variant
  37687. */
  37688. #if defined(DUK_USE_STRTAB_PROBE)
  37689. #if defined(DUK_USE_HEAPPTR16)
  37690. res->strtable16 = (duk_uint16_t *) alloc_func(heap_udata, sizeof(duk_uint16_t) * DUK_STRTAB_INITIAL_SIZE);
  37691. if (!res->strtable16) {
  37692. goto error;
  37693. }
  37694. #else /* DUK_USE_HEAPPTR16 */
  37695. res->strtable = (duk_hstring **) alloc_func(heap_udata, sizeof(duk_hstring *) * DUK_STRTAB_INITIAL_SIZE);
  37696. if (!res->strtable) {
  37697. goto error;
  37698. }
  37699. #endif /* DUK_USE_HEAPPTR16 */
  37700. res->st_size = DUK_STRTAB_INITIAL_SIZE;
  37701. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  37702. {
  37703. duk_small_uint_t i;
  37704. DUK_ASSERT(res->st_size == DUK_STRTAB_INITIAL_SIZE);
  37705. for (i = 0; i < DUK_STRTAB_INITIAL_SIZE; i++) {
  37706. #if defined(DUK_USE_HEAPPTR16)
  37707. res->strtable16[i] = res->heapptr_null16;
  37708. #else
  37709. res->strtable[i] = NULL;
  37710. #endif
  37711. }
  37712. }
  37713. #else /* DUK_USE_EXPLICIT_NULL_INIT */
  37714. #if defined(DUK_USE_HEAPPTR16)
  37715. DUK_MEMZERO(res->strtable16, sizeof(duk_uint16_t) * DUK_STRTAB_INITIAL_SIZE);
  37716. #else
  37717. DUK_MEMZERO(res->strtable, sizeof(duk_hstring *) * DUK_STRTAB_INITIAL_SIZE);
  37718. #endif
  37719. #endif /* DUK_USE_EXPLICIT_NULL_INIT */
  37720. #endif /* DUK_USE_STRTAB_PROBE */
  37721. /*
  37722. * Init stringcache
  37723. */
  37724. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  37725. {
  37726. duk_small_uint_t i;
  37727. for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
  37728. res->strcache[i].h = NULL;
  37729. }
  37730. }
  37731. #endif
  37732. /* XXX: error handling is incomplete. It would be cleanest if
  37733. * there was a setjmp catchpoint, so that all init code could
  37734. * freely throw errors. If that were the case, the return code
  37735. * passing here could be removed.
  37736. */
  37737. /*
  37738. * Init built-in strings
  37739. */
  37740. DUK_DD(DUK_DDPRINT("HEAP: INIT STRINGS"));
  37741. if (!duk__init_heap_strings(res)) {
  37742. goto error;
  37743. }
  37744. /*
  37745. * Init the heap thread
  37746. */
  37747. DUK_DD(DUK_DDPRINT("HEAP: INIT HEAP THREAD"));
  37748. if (!duk__init_heap_thread(res)) {
  37749. goto error;
  37750. }
  37751. /*
  37752. * Init the heap object
  37753. */
  37754. DUK_DD(DUK_DDPRINT("HEAP: INIT HEAP OBJECT"));
  37755. DUK_ASSERT(res->heap_thread != NULL);
  37756. res->heap_object = duk_hobject_alloc(res, DUK_HOBJECT_FLAG_EXTENSIBLE |
  37757. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT));
  37758. if (!res->heap_object) {
  37759. goto error;
  37760. }
  37761. DUK_HOBJECT_INCREF(res->heap_thread, res->heap_object);
  37762. /*
  37763. * All done
  37764. */
  37765. DUK_D(DUK_DPRINT("allocated heap: %p", (void *) res));
  37766. return res;
  37767. error:
  37768. DUK_D(DUK_DPRINT("heap allocation failed"));
  37769. if (res) {
  37770. /* assumes that allocated pointers and alloc funcs are valid
  37771. * if res exists
  37772. */
  37773. DUK_ASSERT(res->alloc_func != NULL);
  37774. DUK_ASSERT(res->realloc_func != NULL);
  37775. DUK_ASSERT(res->free_func != NULL);
  37776. duk_heap_free(res);
  37777. }
  37778. return NULL;
  37779. }
  37780. #line 1 "duk_heap_hashstring.c"
  37781. /*
  37782. * String hash computation (interning).
  37783. */
  37784. /* include removed: duk_internal.h */
  37785. /* constants for duk_hashstring() */
  37786. #define DUK__STRHASH_SHORTSTRING 4096L
  37787. #define DUK__STRHASH_MEDIUMSTRING (256L * 1024L)
  37788. #define DUK__STRHASH_BLOCKSIZE 256L
  37789. DUK_INTERNAL duk_uint32_t duk_heap_hashstring(duk_heap *heap, const duk_uint8_t *str, duk_size_t len) {
  37790. duk_uint32_t hash;
  37791. /*
  37792. * Sampling long strings by byte skipping (like Lua does) is potentially
  37793. * a cache problem. Here we do 'block skipping' instead for long strings:
  37794. * hash an initial part, and then sample the rest of the string with
  37795. * reasonably sized chunks.
  37796. *
  37797. * Skip should depend on length and bound the total time to roughly
  37798. * logarithmic.
  37799. *
  37800. * With current values:
  37801. *
  37802. * 1M string => 256 * 241 = 61696 bytes (0.06M) of hashing
  37803. * 1G string => 256 * 16321 = 4178176 bytes (3.98M) of hashing
  37804. *
  37805. * After an initial part has been hashed, an offset is applied before
  37806. * starting the sampling. The initial offset is computed from the
  37807. * hash of the initial part of the string. The idea is to avoid the
  37808. * case that all long strings have certain offset ranges that are never
  37809. * sampled.
  37810. */
  37811. /* note: mixing len into seed improves hashing when skipping */
  37812. duk_uint32_t str_seed = heap->hash_seed ^ ((duk_uint32_t) len);
  37813. if (len <= DUK__STRHASH_SHORTSTRING) {
  37814. hash = duk_util_hashbytes(str, len, str_seed);
  37815. } else {
  37816. duk_size_t off;
  37817. duk_size_t skip;
  37818. if (len <= DUK__STRHASH_MEDIUMSTRING) {
  37819. skip = (duk_size_t) (16 * DUK__STRHASH_BLOCKSIZE + DUK__STRHASH_BLOCKSIZE);
  37820. } else {
  37821. skip = (duk_size_t) (256 * DUK__STRHASH_BLOCKSIZE + DUK__STRHASH_BLOCKSIZE);
  37822. }
  37823. hash = duk_util_hashbytes(str, (duk_size_t) DUK__STRHASH_SHORTSTRING, str_seed);
  37824. off = DUK__STRHASH_SHORTSTRING + (skip * (hash % 256)) / 256;
  37825. /* XXX: inefficient loop */
  37826. while (off < len) {
  37827. duk_size_t left = len - off;
  37828. duk_size_t now = (duk_size_t) (left > DUK__STRHASH_BLOCKSIZE ? DUK__STRHASH_BLOCKSIZE : left);
  37829. hash ^= duk_util_hashbytes(str + off, now, str_seed);
  37830. off += skip;
  37831. }
  37832. }
  37833. #if defined(DUK_USE_STRHASH16)
  37834. /* Truncate to 16 bits here, so that a computed hash can be compared
  37835. * against a hash stored in a 16-bit field.
  37836. */
  37837. hash &= 0x0000ffffUL;
  37838. #endif
  37839. return hash;
  37840. }
  37841. #line 1 "duk_heap_markandsweep.c"
  37842. /*
  37843. * Mark-and-sweep garbage collection.
  37844. */
  37845. /* include removed: duk_internal.h */
  37846. #ifdef DUK_USE_MARK_AND_SWEEP
  37847. DUK_LOCAL_DECL void duk__mark_heaphdr(duk_heap *heap, duk_heaphdr *h);
  37848. DUK_LOCAL_DECL void duk__mark_tval(duk_heap *heap, duk_tval *tv);
  37849. /*
  37850. * Misc
  37851. */
  37852. /* Select a thread for mark-and-sweep use.
  37853. *
  37854. * XXX: This needs to change later.
  37855. */
  37856. DUK_LOCAL duk_hthread *duk__get_temp_hthread(duk_heap *heap) {
  37857. if (heap->curr_thread) {
  37858. return heap->curr_thread;
  37859. }
  37860. return heap->heap_thread; /* may be NULL, too */
  37861. }
  37862. /*
  37863. * Marking functions for heap types: mark children recursively
  37864. */
  37865. DUK_LOCAL void duk__mark_hstring(duk_heap *heap, duk_hstring *h) {
  37866. DUK_UNREF(heap);
  37867. DUK_UNREF(h);
  37868. DUK_DDD(DUK_DDDPRINT("duk__mark_hstring: %p", (void *) h));
  37869. DUK_ASSERT(h);
  37870. /* nothing to process */
  37871. }
  37872. DUK_LOCAL void duk__mark_hobject(duk_heap *heap, duk_hobject *h) {
  37873. duk_uint_fast32_t i;
  37874. DUK_DDD(DUK_DDDPRINT("duk__mark_hobject: %p", (void *) h));
  37875. DUK_ASSERT(h);
  37876. /* XXX: use advancing pointers instead of index macros -> faster and smaller? */
  37877. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(h); i++) {
  37878. duk_hstring *key = DUK_HOBJECT_E_GET_KEY(heap, h, i);
  37879. if (!key) {
  37880. continue;
  37881. }
  37882. duk__mark_heaphdr(heap, (duk_heaphdr *) key);
  37883. if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap, h, i)) {
  37884. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->a.get);
  37885. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->a.set);
  37886. } else {
  37887. duk__mark_tval(heap, &DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->v);
  37888. }
  37889. }
  37890. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(h); i++) {
  37891. duk__mark_tval(heap, DUK_HOBJECT_A_GET_VALUE_PTR(heap, h, i));
  37892. }
  37893. /* hash part is a 'weak reference' and does not contribute */
  37894. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_HOBJECT_GET_PROTOTYPE(heap, h));
  37895. if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h)) {
  37896. duk_hcompiledfunction *f = (duk_hcompiledfunction *) h;
  37897. duk_tval *tv, *tv_end;
  37898. duk_hobject **fn, **fn_end;
  37899. /* 'data' is reachable through every compiled function which
  37900. * contains a reference.
  37901. */
  37902. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_HCOMPILEDFUNCTION_GET_DATA(heap, f));
  37903. tv = DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(heap, f);
  37904. tv_end = DUK_HCOMPILEDFUNCTION_GET_CONSTS_END(heap, f);
  37905. while (tv < tv_end) {
  37906. duk__mark_tval(heap, tv);
  37907. tv++;
  37908. }
  37909. fn = DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(heap, f);
  37910. fn_end = DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(heap, f);
  37911. while (fn < fn_end) {
  37912. duk__mark_heaphdr(heap, (duk_heaphdr *) *fn);
  37913. fn++;
  37914. }
  37915. } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
  37916. duk_hnativefunction *f = (duk_hnativefunction *) h;
  37917. DUK_UNREF(f);
  37918. /* nothing to mark */
  37919. } else if (DUK_HOBJECT_IS_BUFFEROBJECT(h)) {
  37920. duk_hbufferobject *b = (duk_hbufferobject *) h;
  37921. duk__mark_heaphdr(heap, (duk_heaphdr *) b->buf);
  37922. } else if (DUK_HOBJECT_IS_THREAD(h)) {
  37923. duk_hthread *t = (duk_hthread *) h;
  37924. duk_tval *tv;
  37925. tv = t->valstack;
  37926. while (tv < t->valstack_top) {
  37927. duk__mark_tval(heap, tv);
  37928. tv++;
  37929. }
  37930. for (i = 0; i < (duk_uint_fast32_t) t->callstack_top; i++) {
  37931. duk_activation *act = t->callstack + i;
  37932. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_ACT_GET_FUNC(act));
  37933. duk__mark_heaphdr(heap, (duk_heaphdr *) act->var_env);
  37934. duk__mark_heaphdr(heap, (duk_heaphdr *) act->lex_env);
  37935. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  37936. duk__mark_heaphdr(heap, (duk_heaphdr *) act->prev_caller);
  37937. #endif
  37938. }
  37939. #if 0 /* nothing now */
  37940. for (i = 0; i < (duk_uint_fast32_t) t->catchstack_top; i++) {
  37941. duk_catcher *cat = t->catchstack + i;
  37942. }
  37943. #endif
  37944. duk__mark_heaphdr(heap, (duk_heaphdr *) t->resumer);
  37945. /* XXX: duk_small_uint_t would be enough for this loop */
  37946. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  37947. duk__mark_heaphdr(heap, (duk_heaphdr *) t->builtins[i]);
  37948. }
  37949. }
  37950. }
  37951. /* recursion tracking happens here only */
  37952. DUK_LOCAL void duk__mark_heaphdr(duk_heap *heap, duk_heaphdr *h) {
  37953. DUK_DDD(DUK_DDDPRINT("duk__mark_heaphdr %p, type %ld",
  37954. (void *) h,
  37955. (h != NULL ? (long) DUK_HEAPHDR_GET_TYPE(h) : (long) -1)));
  37956. if (!h) {
  37957. return;
  37958. }
  37959. if (DUK_HEAPHDR_HAS_REACHABLE(h)) {
  37960. DUK_DDD(DUK_DDDPRINT("already marked reachable, skip"));
  37961. return;
  37962. }
  37963. DUK_HEAPHDR_SET_REACHABLE(h);
  37964. if (heap->mark_and_sweep_recursion_depth >= DUK_USE_MARK_AND_SWEEP_RECLIMIT) {
  37965. /* log this with a normal debug level because this should be relatively rare */
  37966. DUK_D(DUK_DPRINT("mark-and-sweep recursion limit reached, marking as temproot: %p", (void *) h));
  37967. DUK_HEAP_SET_MARKANDSWEEP_RECLIMIT_REACHED(heap);
  37968. DUK_HEAPHDR_SET_TEMPROOT(h);
  37969. return;
  37970. }
  37971. heap->mark_and_sweep_recursion_depth++;
  37972. switch ((int) DUK_HEAPHDR_GET_TYPE(h)) {
  37973. case DUK_HTYPE_STRING:
  37974. duk__mark_hstring(heap, (duk_hstring *) h);
  37975. break;
  37976. case DUK_HTYPE_OBJECT:
  37977. duk__mark_hobject(heap, (duk_hobject *) h);
  37978. break;
  37979. case DUK_HTYPE_BUFFER:
  37980. /* nothing to mark */
  37981. break;
  37982. default:
  37983. DUK_D(DUK_DPRINT("attempt to mark heaphdr %p with invalid htype %ld", (void *) h, (long) DUK_HEAPHDR_GET_TYPE(h)));
  37984. DUK_UNREACHABLE();
  37985. }
  37986. heap->mark_and_sweep_recursion_depth--;
  37987. }
  37988. DUK_LOCAL void duk__mark_tval(duk_heap *heap, duk_tval *tv) {
  37989. DUK_DDD(DUK_DDDPRINT("duk__mark_tval %p", (void *) tv));
  37990. if (!tv) {
  37991. return;
  37992. }
  37993. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  37994. duk__mark_heaphdr(heap, DUK_TVAL_GET_HEAPHDR(tv));
  37995. }
  37996. }
  37997. /*
  37998. * Mark the heap.
  37999. */
  38000. DUK_LOCAL void duk__mark_roots_heap(duk_heap *heap) {
  38001. duk_small_uint_t i;
  38002. DUK_DD(DUK_DDPRINT("duk__mark_roots_heap: %p", (void *) heap));
  38003. duk__mark_heaphdr(heap, (duk_heaphdr *) heap->heap_thread);
  38004. duk__mark_heaphdr(heap, (duk_heaphdr *) heap->heap_object);
  38005. for (i = 0; i < DUK_HEAP_NUM_STRINGS; i++) {
  38006. duk_hstring *h = DUK_HEAP_GET_STRING(heap, i);
  38007. duk__mark_heaphdr(heap, (duk_heaphdr *) h);
  38008. }
  38009. duk__mark_tval(heap, &heap->lj.value1);
  38010. duk__mark_tval(heap, &heap->lj.value2);
  38011. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  38012. for (i = 0; i < heap->dbg_breakpoint_count; i++) {
  38013. duk__mark_heaphdr(heap, (duk_heaphdr *) heap->dbg_breakpoints[i].filename);
  38014. }
  38015. #endif
  38016. }
  38017. /*
  38018. * Mark refzero_list objects.
  38019. *
  38020. * Objects on the refzero_list have no inbound references. They might have
  38021. * outbound references to objects that we might free, which would invalidate
  38022. * any references held by the refzero objects. A refzero object might also
  38023. * be rescued by refcount finalization. Refzero objects are treated as
  38024. * reachability roots to ensure they (or anything they point to) are not
  38025. * freed in mark-and-sweep.
  38026. */
  38027. #ifdef DUK_USE_REFERENCE_COUNTING
  38028. DUK_LOCAL void duk__mark_refzero_list(duk_heap *heap) {
  38029. duk_heaphdr *hdr;
  38030. DUK_DD(DUK_DDPRINT("duk__mark_refzero_list: %p", (void *) heap));
  38031. hdr = heap->refzero_list;
  38032. while (hdr) {
  38033. duk__mark_heaphdr(heap, hdr);
  38034. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  38035. }
  38036. }
  38037. #endif
  38038. /*
  38039. * Mark unreachable, finalizable objects.
  38040. *
  38041. * Such objects will be moved aside and their finalizers run later. They have
  38042. * to be treated as reachability roots for their properties etc to remain
  38043. * allocated. This marking is only done for unreachable values which would
  38044. * be swept later (refzero_list is thus excluded).
  38045. *
  38046. * Objects are first marked FINALIZABLE and only then marked as reachability
  38047. * roots; otherwise circular references might be handled inconsistently.
  38048. */
  38049. DUK_LOCAL void duk__mark_finalizable(duk_heap *heap) {
  38050. duk_hthread *thr;
  38051. duk_heaphdr *hdr;
  38052. duk_size_t count_finalizable = 0;
  38053. DUK_DD(DUK_DDPRINT("duk__mark_finalizable: %p", (void *) heap));
  38054. thr = duk__get_temp_hthread(heap);
  38055. DUK_ASSERT(thr != NULL);
  38056. hdr = heap->heap_allocated;
  38057. while (hdr) {
  38058. /* A finalizer is looked up from the object and up its prototype chain
  38059. * (which allows inherited finalizers). A prototype loop must not cause
  38060. * an error to be thrown here; duk_hobject_hasprop_raw() will ignore a
  38061. * prototype loop silently and indicate that the property doesn't exist.
  38062. */
  38063. if (!DUK_HEAPHDR_HAS_REACHABLE(hdr) &&
  38064. DUK_HEAPHDR_GET_TYPE(hdr) == DUK_HTYPE_OBJECT &&
  38065. !DUK_HEAPHDR_HAS_FINALIZED(hdr) &&
  38066. duk_hobject_hasprop_raw(thr, (duk_hobject *) hdr, DUK_HTHREAD_STRING_INT_FINALIZER(thr))) {
  38067. /* heaphdr:
  38068. * - is not reachable
  38069. * - is an object
  38070. * - is not a finalized object
  38071. * - has a finalizer
  38072. */
  38073. DUK_DD(DUK_DDPRINT("unreachable heap object will be "
  38074. "finalized -> mark as finalizable "
  38075. "and treat as a reachability root: %p",
  38076. (void *) hdr));
  38077. DUK_HEAPHDR_SET_FINALIZABLE(hdr);
  38078. count_finalizable ++;
  38079. }
  38080. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  38081. }
  38082. if (count_finalizable == 0) {
  38083. return;
  38084. }
  38085. DUK_DD(DUK_DDPRINT("marked %ld heap objects as finalizable, now mark them reachable",
  38086. (long) count_finalizable));
  38087. hdr = heap->heap_allocated;
  38088. while (hdr) {
  38089. if (DUK_HEAPHDR_HAS_FINALIZABLE(hdr)) {
  38090. duk__mark_heaphdr(heap, hdr);
  38091. }
  38092. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  38093. }
  38094. /* Caller will finish the marking process if we hit a recursion limit. */
  38095. }
  38096. /*
  38097. * Mark objects on finalize_list.
  38098. *
  38099. */
  38100. DUK_LOCAL void duk__mark_finalize_list(duk_heap *heap) {
  38101. duk_heaphdr *hdr;
  38102. #ifdef DUK_USE_DEBUG
  38103. duk_size_t count_finalize_list = 0;
  38104. #endif
  38105. DUK_DD(DUK_DDPRINT("duk__mark_finalize_list: %p", (void *) heap));
  38106. hdr = heap->finalize_list;
  38107. while (hdr) {
  38108. duk__mark_heaphdr(heap, hdr);
  38109. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  38110. #ifdef DUK_USE_DEBUG
  38111. count_finalize_list++;
  38112. #endif
  38113. }
  38114. #ifdef DUK_USE_DEBUG
  38115. if (count_finalize_list > 0) {
  38116. DUK_D(DUK_DPRINT("marked %ld objects on the finalize_list as reachable (previous finalizer run skipped)",
  38117. (long) count_finalize_list));
  38118. }
  38119. #endif
  38120. }
  38121. /*
  38122. * Fallback marking handler if recursion limit is reached.
  38123. *
  38124. * Iterates 'temproots' until recursion limit is no longer hit. Note
  38125. * that temproots may reside either in heap allocated list or the
  38126. * refzero work list. This is a slow scan, but guarantees that we
  38127. * finish with a bounded C stack.
  38128. *
  38129. * Note that nodes may have been marked as temproots before this
  38130. * scan begun, OR they may have been marked during the scan (as
  38131. * we process nodes recursively also during the scan). This is
  38132. * intended behavior.
  38133. */
  38134. #ifdef DUK_USE_DEBUG
  38135. DUK_LOCAL void duk__handle_temproot(duk_heap *heap, duk_heaphdr *hdr, duk_size_t *count) {
  38136. #else
  38137. DUK_LOCAL void duk__handle_temproot(duk_heap *heap, duk_heaphdr *hdr) {
  38138. #endif
  38139. if (!DUK_HEAPHDR_HAS_TEMPROOT(hdr)) {
  38140. DUK_DDD(DUK_DDDPRINT("not a temp root: %p", (void *) hdr));
  38141. return;
  38142. }
  38143. DUK_DDD(DUK_DDDPRINT("found a temp root: %p", (void *) hdr));
  38144. DUK_HEAPHDR_CLEAR_TEMPROOT(hdr);
  38145. DUK_HEAPHDR_CLEAR_REACHABLE(hdr); /* done so that duk__mark_heaphdr() works correctly */
  38146. duk__mark_heaphdr(heap, hdr);
  38147. #ifdef DUK_USE_DEBUG
  38148. (*count)++;
  38149. #endif
  38150. }
  38151. DUK_LOCAL void duk__mark_temproots_by_heap_scan(duk_heap *heap) {
  38152. duk_heaphdr *hdr;
  38153. #ifdef DUK_USE_DEBUG
  38154. duk_size_t count;
  38155. #endif
  38156. DUK_DD(DUK_DDPRINT("duk__mark_temproots_by_heap_scan: %p", (void *) heap));
  38157. while (DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED(heap)) {
  38158. DUK_DD(DUK_DDPRINT("recursion limit reached, doing heap scan to continue from temproots"));
  38159. #ifdef DUK_USE_DEBUG
  38160. count = 0;
  38161. #endif
  38162. DUK_HEAP_CLEAR_MARKANDSWEEP_RECLIMIT_REACHED(heap);
  38163. hdr = heap->heap_allocated;
  38164. while (hdr) {
  38165. #ifdef DUK_USE_DEBUG
  38166. duk__handle_temproot(heap, hdr, &count);
  38167. #else
  38168. duk__handle_temproot(heap, hdr);
  38169. #endif
  38170. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  38171. }
  38172. /* must also check refzero_list */
  38173. #ifdef DUK_USE_REFERENCE_COUNTING
  38174. hdr = heap->refzero_list;
  38175. while (hdr) {
  38176. #ifdef DUK_USE_DEBUG
  38177. duk__handle_temproot(heap, hdr, &count);
  38178. #else
  38179. duk__handle_temproot(heap, hdr);
  38180. #endif
  38181. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  38182. }
  38183. #endif /* DUK_USE_REFERENCE_COUNTING */
  38184. #ifdef DUK_USE_DEBUG
  38185. DUK_DD(DUK_DDPRINT("temproot mark heap scan processed %ld temp roots", (long) count));
  38186. #endif
  38187. }
  38188. }
  38189. /*
  38190. * Finalize refcounts for heap elements just about to be freed.
  38191. * This must be done for all objects before freeing to avoid any
  38192. * stale pointer dereferences.
  38193. *
  38194. * Note that this must deduce the set of objects to be freed
  38195. * identically to duk__sweep_heap().
  38196. */
  38197. #ifdef DUK_USE_REFERENCE_COUNTING
  38198. DUK_LOCAL void duk__finalize_refcounts(duk_heap *heap) {
  38199. duk_hthread *thr;
  38200. duk_heaphdr *hdr;
  38201. thr = duk__get_temp_hthread(heap);
  38202. DUK_ASSERT(thr != NULL);
  38203. DUK_DD(DUK_DDPRINT("duk__finalize_refcounts: heap=%p, hthread=%p",
  38204. (void *) heap, (void *) thr));
  38205. hdr = heap->heap_allocated;
  38206. while (hdr) {
  38207. if (!DUK_HEAPHDR_HAS_REACHABLE(hdr)) {
  38208. /*
  38209. * Unreachable object about to be swept. Finalize target refcounts
  38210. * (objects which the unreachable object points to) without doing
  38211. * refzero processing. Recursive decrefs are also prevented when
  38212. * refzero processing is disabled.
  38213. *
  38214. * Value cannot be a finalizable object, as they have been made
  38215. * temporarily reachable for this round.
  38216. */
  38217. DUK_DDD(DUK_DDDPRINT("unreachable object, refcount finalize before sweeping: %p", (void *) hdr));
  38218. duk_heaphdr_refcount_finalize(thr, hdr);
  38219. }
  38220. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  38221. }
  38222. }
  38223. #endif /* DUK_USE_REFERENCE_COUNTING */
  38224. /*
  38225. * Clear (reachable) flags of refzero work list.
  38226. */
  38227. #ifdef DUK_USE_REFERENCE_COUNTING
  38228. DUK_LOCAL void duk__clear_refzero_list_flags(duk_heap *heap) {
  38229. duk_heaphdr *hdr;
  38230. DUK_DD(DUK_DDPRINT("duk__clear_refzero_list_flags: %p", (void *) heap));
  38231. hdr = heap->refzero_list;
  38232. while (hdr) {
  38233. DUK_HEAPHDR_CLEAR_REACHABLE(hdr);
  38234. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(hdr));
  38235. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(hdr));
  38236. DUK_ASSERT(!DUK_HEAPHDR_HAS_TEMPROOT(hdr));
  38237. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  38238. }
  38239. }
  38240. #endif /* DUK_USE_REFERENCE_COUNTING */
  38241. /*
  38242. * Clear (reachable) flags of finalize_list
  38243. *
  38244. * We could mostly do in the sweep phase when we move objects from the
  38245. * heap into the finalize_list. However, if a finalizer run is skipped
  38246. * during a mark-and-sweep, the objects on the finalize_list will be marked
  38247. * reachable during the next mark-and-sweep. Since they're already on the
  38248. * finalize_list, no-one will be clearing their REACHABLE flag so we do it
  38249. * here. (This now overlaps with the sweep handling in a harmless way.)
  38250. */
  38251. DUK_LOCAL void duk__clear_finalize_list_flags(duk_heap *heap) {
  38252. duk_heaphdr *hdr;
  38253. DUK_DD(DUK_DDPRINT("duk__clear_finalize_list_flags: %p", (void *) heap));
  38254. hdr = heap->finalize_list;
  38255. while (hdr) {
  38256. DUK_HEAPHDR_CLEAR_REACHABLE(hdr);
  38257. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(hdr));
  38258. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(hdr));
  38259. DUK_ASSERT(!DUK_HEAPHDR_HAS_TEMPROOT(hdr));
  38260. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  38261. }
  38262. }
  38263. /*
  38264. * Sweep stringtable
  38265. */
  38266. #if defined(DUK_USE_STRTAB_CHAIN)
  38267. /* XXX: skip count_free w/o debug? */
  38268. #if defined(DUK_USE_HEAPPTR16)
  38269. DUK_LOCAL void duk__sweep_string_chain16(duk_heap *heap, duk_uint16_t *slot, duk_size_t *count_keep, duk_size_t *count_free) {
  38270. duk_uint16_t h16 = *slot;
  38271. duk_hstring *h;
  38272. duk_uint16_t null16 = heap->heapptr_null16;
  38273. if (h16 == null16) {
  38274. /* nop */
  38275. return;
  38276. }
  38277. h = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, h16);
  38278. DUK_ASSERT(h != NULL);
  38279. if (DUK_HEAPHDR_HAS_REACHABLE((duk_heaphdr *) h)) {
  38280. DUK_HEAPHDR_CLEAR_REACHABLE((duk_heaphdr *) h);
  38281. (*count_keep)++;
  38282. } else {
  38283. #if defined(DUK_USE_REFERENCE_COUNTING)
  38284. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h) == 0);
  38285. #endif
  38286. /* deal with weak references first */
  38287. duk_heap_strcache_string_remove(heap, (duk_hstring *) h);
  38288. *slot = null16;
  38289. /* free inner references (these exist e.g. when external
  38290. * strings are enabled)
  38291. */
  38292. duk_free_hstring_inner(heap, h);
  38293. DUK_FREE(heap, h);
  38294. (*count_free)++;
  38295. }
  38296. }
  38297. #else /* DUK_USE_HEAPPTR16 */
  38298. DUK_LOCAL void duk__sweep_string_chain(duk_heap *heap, duk_hstring **slot, duk_size_t *count_keep, duk_size_t *count_free) {
  38299. duk_hstring *h = *slot;
  38300. if (h == NULL) {
  38301. /* nop */
  38302. return;
  38303. }
  38304. if (DUK_HEAPHDR_HAS_REACHABLE((duk_heaphdr *) h)) {
  38305. DUK_HEAPHDR_CLEAR_REACHABLE((duk_heaphdr *) h);
  38306. (*count_keep)++;
  38307. } else {
  38308. #if defined(DUK_USE_REFERENCE_COUNTING)
  38309. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h) == 0);
  38310. #endif
  38311. /* deal with weak references first */
  38312. duk_heap_strcache_string_remove(heap, (duk_hstring *) h);
  38313. *slot = NULL;
  38314. /* free inner references (these exist e.g. when external
  38315. * strings are enabled)
  38316. */
  38317. duk_free_hstring_inner(heap, h);
  38318. DUK_FREE(heap, h);
  38319. (*count_free)++;
  38320. }
  38321. }
  38322. #endif /* DUK_USE_HEAPPTR16 */
  38323. DUK_LOCAL void duk__sweep_stringtable_chain(duk_heap *heap, duk_size_t *out_count_keep) {
  38324. duk_strtab_entry *e;
  38325. duk_uint_fast32_t i;
  38326. duk_size_t count_free = 0;
  38327. duk_size_t count_keep = 0;
  38328. duk_size_t j, n;
  38329. #if defined(DUK_USE_HEAPPTR16)
  38330. duk_uint16_t *lst;
  38331. #else
  38332. duk_hstring **lst;
  38333. #endif
  38334. DUK_DD(DUK_DDPRINT("duk__sweep_stringtable: %p", (void *) heap));
  38335. /* Non-zero refcounts should not happen for unreachable strings,
  38336. * because we refcount finalize all unreachable objects which
  38337. * should have decreased unreachable string refcounts to zero
  38338. * (even for cycles).
  38339. */
  38340. for (i = 0; i < DUK_STRTAB_CHAIN_SIZE; i++) {
  38341. e = heap->strtable + i;
  38342. if (e->listlen == 0) {
  38343. #if defined(DUK_USE_HEAPPTR16)
  38344. duk__sweep_string_chain16(heap, &e->u.str16, &count_keep, &count_free);
  38345. #else
  38346. duk__sweep_string_chain(heap, &e->u.str, &count_keep, &count_free);
  38347. #endif
  38348. } else {
  38349. #if defined(DUK_USE_HEAPPTR16)
  38350. lst = (duk_uint16_t *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.strlist16);
  38351. #else
  38352. lst = e->u.strlist;
  38353. #endif
  38354. for (j = 0, n = e->listlen; j < n; j++) {
  38355. #if defined(DUK_USE_HEAPPTR16)
  38356. duk__sweep_string_chain16(heap, lst + j, &count_keep, &count_free);
  38357. #else
  38358. duk__sweep_string_chain(heap, lst + j, &count_keep, &count_free);
  38359. #endif
  38360. }
  38361. }
  38362. }
  38363. DUK_D(DUK_DPRINT("mark-and-sweep sweep stringtable: %ld freed, %ld kept",
  38364. (long) count_free, (long) count_keep));
  38365. *out_count_keep = count_keep;
  38366. }
  38367. #endif /* DUK_USE_STRTAB_CHAIN */
  38368. #if defined(DUK_USE_STRTAB_PROBE)
  38369. DUK_LOCAL void duk__sweep_stringtable_probe(duk_heap *heap, duk_size_t *out_count_keep) {
  38370. duk_hstring *h;
  38371. duk_uint_fast32_t i;
  38372. #ifdef DUK_USE_DEBUG
  38373. duk_size_t count_free = 0;
  38374. #endif
  38375. duk_size_t count_keep = 0;
  38376. DUK_DD(DUK_DDPRINT("duk__sweep_stringtable: %p", (void *) heap));
  38377. for (i = 0; i < heap->st_size; i++) {
  38378. #if defined(DUK_USE_HEAPPTR16)
  38379. h = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->strtable16[i]);
  38380. #else
  38381. h = heap->strtable[i];
  38382. #endif
  38383. if (h == NULL || h == DUK_STRTAB_DELETED_MARKER(heap)) {
  38384. continue;
  38385. } else if (DUK_HEAPHDR_HAS_REACHABLE((duk_heaphdr *) h)) {
  38386. DUK_HEAPHDR_CLEAR_REACHABLE((duk_heaphdr *) h);
  38387. count_keep++;
  38388. continue;
  38389. }
  38390. #ifdef DUK_USE_DEBUG
  38391. count_free++;
  38392. #endif
  38393. #if defined(DUK_USE_REFERENCE_COUNTING)
  38394. /* Non-zero refcounts should not happen for unreachable strings,
  38395. * because we refcount finalize all unreachable objects which
  38396. * should have decreased unreachable string refcounts to zero
  38397. * (even for cycles).
  38398. */
  38399. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h) == 0);
  38400. #endif
  38401. DUK_DDD(DUK_DDDPRINT("sweep string, not reachable: %p", (void *) h));
  38402. /* deal with weak references first */
  38403. duk_heap_strcache_string_remove(heap, (duk_hstring *) h);
  38404. /* remove the string (mark DELETED), could also call
  38405. * duk_heap_string_remove() but that would be slow and
  38406. * pointless because we already know the slot.
  38407. */
  38408. #if defined(DUK_USE_HEAPPTR16)
  38409. heap->strtable16[i] = heap->heapptr_deleted16;
  38410. #else
  38411. heap->strtable[i] = DUK_STRTAB_DELETED_MARKER(heap);
  38412. #endif
  38413. /* free inner references (these exist e.g. when external
  38414. * strings are enabled)
  38415. */
  38416. duk_free_hstring_inner(heap, (duk_hstring *) h);
  38417. /* finally free the struct itself */
  38418. DUK_FREE(heap, h);
  38419. }
  38420. #ifdef DUK_USE_DEBUG
  38421. DUK_D(DUK_DPRINT("mark-and-sweep sweep stringtable: %ld freed, %ld kept",
  38422. (long) count_free, (long) count_keep));
  38423. #endif
  38424. *out_count_keep = count_keep;
  38425. }
  38426. #endif /* DUK_USE_STRTAB_PROBE */
  38427. /*
  38428. * Sweep heap
  38429. */
  38430. DUK_LOCAL void duk__sweep_heap(duk_heap *heap, duk_int_t flags, duk_size_t *out_count_keep) {
  38431. duk_heaphdr *prev; /* last element that was left in the heap */
  38432. duk_heaphdr *curr;
  38433. duk_heaphdr *next;
  38434. #ifdef DUK_USE_DEBUG
  38435. duk_size_t count_free = 0;
  38436. duk_size_t count_finalize = 0;
  38437. duk_size_t count_rescue = 0;
  38438. #endif
  38439. duk_size_t count_keep = 0;
  38440. DUK_UNREF(flags);
  38441. DUK_DD(DUK_DDPRINT("duk__sweep_heap: %p", (void *) heap));
  38442. prev = NULL;
  38443. curr = heap->heap_allocated;
  38444. heap->heap_allocated = NULL;
  38445. while (curr) {
  38446. /* strings are never placed on the heap allocated list */
  38447. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(curr) != DUK_HTYPE_STRING);
  38448. next = DUK_HEAPHDR_GET_NEXT(heap, curr);
  38449. if (DUK_HEAPHDR_HAS_REACHABLE(curr)) {
  38450. /*
  38451. * Reachable object, keep
  38452. */
  38453. DUK_DDD(DUK_DDDPRINT("sweep, reachable: %p", (void *) curr));
  38454. if (DUK_HEAPHDR_HAS_FINALIZABLE(curr)) {
  38455. /*
  38456. * If object has been marked finalizable, move it to the
  38457. * "to be finalized" work list. It will be collected on
  38458. * the next mark-and-sweep if it is still unreachable
  38459. * after running the finalizer.
  38460. */
  38461. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(curr));
  38462. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(curr) == DUK_HTYPE_OBJECT);
  38463. DUK_DDD(DUK_DDDPRINT("object has finalizer, move to finalization work list: %p", (void *) curr));
  38464. #ifdef DUK_USE_DOUBLE_LINKED_HEAP
  38465. if (heap->finalize_list) {
  38466. DUK_HEAPHDR_SET_PREV(heap, heap->finalize_list, curr);
  38467. }
  38468. DUK_HEAPHDR_SET_PREV(heap, curr, NULL);
  38469. #endif
  38470. DUK_HEAPHDR_SET_NEXT(heap, curr, heap->finalize_list);
  38471. heap->finalize_list = curr;
  38472. #ifdef DUK_USE_DEBUG
  38473. count_finalize++;
  38474. #endif
  38475. } else {
  38476. /*
  38477. * Object will be kept; queue object back to heap_allocated (to tail)
  38478. */
  38479. if (DUK_HEAPHDR_HAS_FINALIZED(curr)) {
  38480. /*
  38481. * Object's finalizer was executed on last round, and
  38482. * object has been happily rescued.
  38483. */
  38484. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(curr));
  38485. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(curr) == DUK_HTYPE_OBJECT);
  38486. DUK_DD(DUK_DDPRINT("object rescued during mark-and-sweep finalization: %p", (void *) curr));
  38487. #ifdef DUK_USE_DEBUG
  38488. count_rescue++;
  38489. #endif
  38490. } else {
  38491. /*
  38492. * Plain, boring reachable object.
  38493. */
  38494. count_keep++;
  38495. }
  38496. if (!heap->heap_allocated) {
  38497. heap->heap_allocated = curr;
  38498. }
  38499. if (prev) {
  38500. DUK_HEAPHDR_SET_NEXT(heap, prev, curr);
  38501. }
  38502. #ifdef DUK_USE_DOUBLE_LINKED_HEAP
  38503. DUK_HEAPHDR_SET_PREV(heap, curr, prev);
  38504. #endif
  38505. prev = curr;
  38506. }
  38507. DUK_HEAPHDR_CLEAR_REACHABLE(curr);
  38508. DUK_HEAPHDR_CLEAR_FINALIZED(curr);
  38509. DUK_HEAPHDR_CLEAR_FINALIZABLE(curr);
  38510. DUK_ASSERT(!DUK_HEAPHDR_HAS_REACHABLE(curr));
  38511. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(curr));
  38512. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(curr));
  38513. curr = next;
  38514. } else {
  38515. /*
  38516. * Unreachable object, free
  38517. */
  38518. DUK_DDD(DUK_DDDPRINT("sweep, not reachable: %p", (void *) curr));
  38519. #if defined(DUK_USE_REFERENCE_COUNTING)
  38520. /* Non-zero refcounts should not happen because we refcount
  38521. * finalize all unreachable objects which should cancel out
  38522. * refcounts (even for cycles).
  38523. */
  38524. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(curr) == 0);
  38525. #endif
  38526. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(curr));
  38527. if (DUK_HEAPHDR_HAS_FINALIZED(curr)) {
  38528. DUK_DDD(DUK_DDDPRINT("finalized object not rescued: %p", (void *) curr));
  38529. }
  38530. /* Note: object cannot be a finalizable unreachable object, as
  38531. * they have been marked temporarily reachable for this round,
  38532. * and are handled above.
  38533. */
  38534. #ifdef DUK_USE_DEBUG
  38535. count_free++;
  38536. #endif
  38537. /* weak refs should be handled here, but no weak refs for
  38538. * any non-string objects exist right now.
  38539. */
  38540. /* free object and all auxiliary (non-heap) allocs */
  38541. duk_heap_free_heaphdr_raw(heap, curr);
  38542. curr = next;
  38543. }
  38544. }
  38545. if (prev) {
  38546. DUK_HEAPHDR_SET_NEXT(heap, prev, NULL);
  38547. }
  38548. #ifdef DUK_USE_DEBUG
  38549. DUK_D(DUK_DPRINT("mark-and-sweep sweep objects (non-string): %ld freed, %ld kept, %ld rescued, %ld queued for finalization",
  38550. (long) count_free, (long) count_keep, (long) count_rescue, (long) count_finalize));
  38551. #endif
  38552. *out_count_keep = count_keep;
  38553. }
  38554. /*
  38555. * Run (object) finalizers in the "to be finalized" work list.
  38556. */
  38557. DUK_LOCAL void duk__run_object_finalizers(duk_heap *heap) {
  38558. duk_heaphdr *curr;
  38559. duk_heaphdr *next;
  38560. #ifdef DUK_USE_DEBUG
  38561. duk_size_t count = 0;
  38562. #endif
  38563. duk_hthread *thr;
  38564. DUK_DD(DUK_DDPRINT("duk__run_object_finalizers: %p", (void *) heap));
  38565. thr = duk__get_temp_hthread(heap);
  38566. DUK_ASSERT(thr != NULL);
  38567. curr = heap->finalize_list;
  38568. while (curr) {
  38569. DUK_DDD(DUK_DDDPRINT("mark-and-sweep finalize: %p", (void *) curr));
  38570. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(curr) == DUK_HTYPE_OBJECT); /* only objects have finalizers */
  38571. DUK_ASSERT(!DUK_HEAPHDR_HAS_REACHABLE(curr)); /* flags have been already cleared */
  38572. DUK_ASSERT(!DUK_HEAPHDR_HAS_TEMPROOT(curr));
  38573. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(curr));
  38574. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(curr));
  38575. /* run the finalizer */
  38576. duk_hobject_run_finalizer(thr, (duk_hobject *) curr); /* must never longjmp */
  38577. /* mark FINALIZED, for next mark-and-sweep (will collect unless has become reachable;
  38578. * prevent running finalizer again if reachable)
  38579. */
  38580. DUK_HEAPHDR_SET_FINALIZED(curr);
  38581. /* queue back to heap_allocated */
  38582. next = DUK_HEAPHDR_GET_NEXT(heap, curr);
  38583. DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap, curr);
  38584. curr = next;
  38585. #ifdef DUK_USE_DEBUG
  38586. count++;
  38587. #endif
  38588. }
  38589. /* finalize_list will always be processed completely */
  38590. heap->finalize_list = NULL;
  38591. #ifdef DUK_USE_DEBUG
  38592. DUK_D(DUK_DPRINT("mark-and-sweep finalize objects: %ld finalizers called", (long) count));
  38593. #endif
  38594. }
  38595. /*
  38596. * Object compaction.
  38597. *
  38598. * Compaction is assumed to never throw an error.
  38599. */
  38600. DUK_LOCAL int duk__protected_compact_object(duk_context *ctx) {
  38601. /* XXX: for threads, compact value stack, call stack, catch stack? */
  38602. duk_hobject *obj = duk_get_hobject(ctx, -1);
  38603. DUK_ASSERT(obj != NULL);
  38604. duk_hobject_compact_props((duk_hthread *) ctx, obj);
  38605. return 0;
  38606. }
  38607. #ifdef DUK_USE_DEBUG
  38608. 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) {
  38609. #else
  38610. DUK_LOCAL void duk__compact_object_list(duk_heap *heap, duk_hthread *thr, duk_heaphdr *start) {
  38611. #endif
  38612. duk_heaphdr *curr;
  38613. #ifdef DUK_USE_DEBUG
  38614. duk_size_t old_size, new_size;
  38615. #endif
  38616. duk_hobject *obj;
  38617. DUK_UNREF(heap);
  38618. curr = start;
  38619. while (curr) {
  38620. DUK_DDD(DUK_DDDPRINT("mark-and-sweep compact: %p", (void *) curr));
  38621. if (DUK_HEAPHDR_GET_TYPE(curr) != DUK_HTYPE_OBJECT) {
  38622. goto next;
  38623. }
  38624. obj = (duk_hobject *) curr;
  38625. #ifdef DUK_USE_DEBUG
  38626. old_size = DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE(obj),
  38627. DUK_HOBJECT_GET_ASIZE(obj),
  38628. DUK_HOBJECT_GET_HSIZE(obj));
  38629. #endif
  38630. DUK_DD(DUK_DDPRINT("compact object: %p", (void *) obj));
  38631. duk_push_hobject((duk_context *) thr, obj);
  38632. /* XXX: disable error handlers for duration of compaction? */
  38633. duk_safe_call((duk_context *) thr, duk__protected_compact_object, 1, 0);
  38634. #ifdef DUK_USE_DEBUG
  38635. new_size = DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE(obj),
  38636. DUK_HOBJECT_GET_ASIZE(obj),
  38637. DUK_HOBJECT_GET_HSIZE(obj));
  38638. #endif
  38639. #ifdef DUK_USE_DEBUG
  38640. (*p_count_compact)++;
  38641. (*p_count_bytes_saved) += (duk_size_t) (old_size - new_size);
  38642. #endif
  38643. next:
  38644. curr = DUK_HEAPHDR_GET_NEXT(heap, curr);
  38645. #ifdef DUK_USE_DEBUG
  38646. (*p_count_check)++;
  38647. #endif
  38648. }
  38649. }
  38650. DUK_LOCAL void duk__compact_objects(duk_heap *heap) {
  38651. /* XXX: which lists should participate? to be finalized? */
  38652. #ifdef DUK_USE_DEBUG
  38653. duk_size_t count_check = 0;
  38654. duk_size_t count_compact = 0;
  38655. duk_size_t count_bytes_saved = 0;
  38656. #endif
  38657. duk_hthread *thr;
  38658. DUK_DD(DUK_DDPRINT("duk__compact_objects: %p", (void *) heap));
  38659. thr = duk__get_temp_hthread(heap);
  38660. DUK_ASSERT(thr != NULL);
  38661. #ifdef DUK_USE_DEBUG
  38662. duk__compact_object_list(heap, thr, heap->heap_allocated, &count_check, &count_compact, &count_bytes_saved);
  38663. duk__compact_object_list(heap, thr, heap->finalize_list, &count_check, &count_compact, &count_bytes_saved);
  38664. #ifdef DUK_USE_REFERENCE_COUNTING
  38665. duk__compact_object_list(heap, thr, heap->refzero_list, &count_check, &count_compact, &count_bytes_saved);
  38666. #endif
  38667. #else
  38668. duk__compact_object_list(heap, thr, heap->heap_allocated);
  38669. duk__compact_object_list(heap, thr, heap->finalize_list);
  38670. #ifdef DUK_USE_REFERENCE_COUNTING
  38671. duk__compact_object_list(heap, thr, heap->refzero_list);
  38672. #endif
  38673. #endif
  38674. #ifdef DUK_USE_DEBUG
  38675. DUK_D(DUK_DPRINT("mark-and-sweep compact objects: %ld checked, %ld compaction attempts, %ld bytes saved by compaction",
  38676. (long) count_check, (long) count_compact, (long) count_bytes_saved));
  38677. #endif
  38678. }
  38679. /*
  38680. * Assertion helpers.
  38681. */
  38682. #ifdef DUK_USE_ASSERTIONS
  38683. DUK_LOCAL void duk__assert_heaphdr_flags(duk_heap *heap) {
  38684. duk_heaphdr *hdr;
  38685. hdr = heap->heap_allocated;
  38686. while (hdr) {
  38687. DUK_ASSERT(!DUK_HEAPHDR_HAS_REACHABLE(hdr));
  38688. DUK_ASSERT(!DUK_HEAPHDR_HAS_TEMPROOT(hdr));
  38689. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(hdr));
  38690. /* may have FINALIZED */
  38691. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  38692. }
  38693. #ifdef DUK_USE_REFERENCE_COUNTING
  38694. hdr = heap->refzero_list;
  38695. while (hdr) {
  38696. DUK_ASSERT(!DUK_HEAPHDR_HAS_REACHABLE(hdr));
  38697. DUK_ASSERT(!DUK_HEAPHDR_HAS_TEMPROOT(hdr));
  38698. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(hdr));
  38699. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(hdr));
  38700. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  38701. }
  38702. #endif /* DUK_USE_REFERENCE_COUNTING */
  38703. }
  38704. #ifdef DUK_USE_REFERENCE_COUNTING
  38705. DUK_LOCAL void duk__assert_valid_refcounts(duk_heap *heap) {
  38706. duk_heaphdr *hdr = heap->heap_allocated;
  38707. while (hdr) {
  38708. if (DUK_HEAPHDR_GET_REFCOUNT(hdr) == 0 &&
  38709. DUK_HEAPHDR_HAS_FINALIZED(hdr)) {
  38710. /* An object may be in heap_allocated list with a zero
  38711. * refcount if it has just been finalized and is waiting
  38712. * to be collected by the next cycle.
  38713. */
  38714. } else if (DUK_HEAPHDR_GET_REFCOUNT(hdr) == 0) {
  38715. /* An object may be in heap_allocated list with a zero
  38716. * refcount also if it is a temporary object created by
  38717. * a finalizer; because finalization now runs inside
  38718. * mark-and-sweep, such objects will not be queued to
  38719. * refzero_list and will thus appear here with refcount
  38720. * zero.
  38721. */
  38722. #if 0 /* this case can no longer occur because refcount is unsigned */
  38723. } else if (DUK_HEAPHDR_GET_REFCOUNT(hdr) < 0) {
  38724. DUK_D(DUK_DPRINT("invalid refcount: %ld, %p -> %!O",
  38725. (hdr != NULL ? (long) DUK_HEAPHDR_GET_REFCOUNT(hdr) : (long) 0),
  38726. (void *) hdr, (duk_heaphdr *) hdr));
  38727. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(hdr) > 0);
  38728. #endif
  38729. }
  38730. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  38731. }
  38732. }
  38733. #endif /* DUK_USE_REFERENCE_COUNTING */
  38734. #endif /* DUK_USE_ASSERTIONS */
  38735. /*
  38736. * Finalizer torture. Do one fake finalizer call which causes side effects
  38737. * similar to one or more finalizers on actual objects.
  38738. */
  38739. #if defined(DUK_USE_MARKANDSWEEP_FINALIZER_TORTURE)
  38740. DUK_LOCAL duk_ret_t duk__markandsweep_fake_finalizer(duk_context *ctx) {
  38741. DUK_D(DUK_DPRINT("fake mark-and-sweep torture finalizer executed"));
  38742. /* Require a lot of stack to force a value stack grow/shrink.
  38743. * Recursive mark-and-sweep is prevented by allocation macros
  38744. * so this won't trigger another mark-and-sweep.
  38745. */
  38746. duk_require_stack(ctx, 100000);
  38747. /* XXX: do something to force a callstack grow/shrink, perhaps
  38748. * just a manual forced resize or a forced relocating realloc?
  38749. */
  38750. return 0;
  38751. }
  38752. DUK_LOCAL void duk__markandsweep_torture_finalizer(duk_hthread *thr) {
  38753. duk_context *ctx;
  38754. duk_int_t rc;
  38755. DUK_ASSERT(thr != NULL);
  38756. ctx = (duk_context *) thr;
  38757. /* Avoid fake finalization when callstack limit has been reached.
  38758. * Otherwise a callstack limit error will be created, then refzero'ed.
  38759. */
  38760. if (thr->heap->call_recursion_depth >= thr->heap->call_recursion_limit ||
  38761. thr->callstack_size + 2 * DUK_CALLSTACK_GROW_STEP >= thr->callstack_max /*approximate*/) {
  38762. DUK_D(DUK_DPRINT("call recursion depth reached, avoid fake mark-and-sweep torture finalizer"));
  38763. return;
  38764. }
  38765. /* Run fake finalizer. Avoid creating unnecessary garbage. */
  38766. duk_push_c_function(ctx, duk__markandsweep_fake_finalizer, 0 /*nargs*/);
  38767. rc = duk_pcall(ctx, 0 /*nargs*/);
  38768. DUK_UNREF(rc); /* ignored */
  38769. duk_pop(ctx);
  38770. }
  38771. #endif /* DUK_USE_MARKANDSWEEP_FINALIZER_TORTURE */
  38772. /*
  38773. * Main mark-and-sweep function.
  38774. *
  38775. * 'flags' represents the features requested by the caller. The current
  38776. * heap->mark_and_sweep_base_flags is ORed automatically into the flags;
  38777. * the base flags mask typically prevents certain mark-and-sweep operations
  38778. * to avoid trouble.
  38779. */
  38780. DUK_INTERNAL duk_bool_t duk_heap_mark_and_sweep(duk_heap *heap, duk_small_uint_t flags) {
  38781. duk_hthread *thr;
  38782. duk_size_t count_keep_obj;
  38783. duk_size_t count_keep_str;
  38784. #ifdef DUK_USE_VOLUNTARY_GC
  38785. duk_size_t tmp;
  38786. #endif
  38787. /* XXX: thread selection for mark-and-sweep is currently a hack.
  38788. * If we don't have a thread, the entire mark-and-sweep is now
  38789. * skipped (although we could just skip finalizations).
  38790. */
  38791. /* XXX: if thr != NULL, the thr may still be in the middle of
  38792. * initialization; improve the thread viability test.
  38793. */
  38794. thr = duk__get_temp_hthread(heap);
  38795. if (thr == NULL) {
  38796. DUK_D(DUK_DPRINT("temporary hack: gc skipped because we don't have a temp thread"));
  38797. /* reset voluntary gc trigger count */
  38798. #ifdef DUK_USE_VOLUNTARY_GC
  38799. heap->mark_and_sweep_trigger_counter = DUK_HEAP_MARK_AND_SWEEP_TRIGGER_SKIP;
  38800. #endif
  38801. return 0; /* OK */
  38802. }
  38803. DUK_D(DUK_DPRINT("garbage collect (mark-and-sweep) starting, requested flags: 0x%08lx, effective flags: 0x%08lx",
  38804. (unsigned long) flags, (unsigned long) (flags | heap->mark_and_sweep_base_flags)));
  38805. flags |= heap->mark_and_sweep_base_flags;
  38806. /*
  38807. * Assertions before
  38808. */
  38809. #ifdef DUK_USE_ASSERTIONS
  38810. DUK_ASSERT(!DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap));
  38811. DUK_ASSERT(!DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED(heap));
  38812. DUK_ASSERT(heap->mark_and_sweep_recursion_depth == 0);
  38813. duk__assert_heaphdr_flags(heap);
  38814. #ifdef DUK_USE_REFERENCE_COUNTING
  38815. /* Note: DUK_HEAP_HAS_REFZERO_FREE_RUNNING(heap) may be true; a refcount
  38816. * finalizer may trigger a mark-and-sweep.
  38817. */
  38818. duk__assert_valid_refcounts(heap);
  38819. #endif /* DUK_USE_REFERENCE_COUNTING */
  38820. #endif /* DUK_USE_ASSERTIONS */
  38821. /*
  38822. * Begin
  38823. */
  38824. DUK_HEAP_SET_MARKANDSWEEP_RUNNING(heap);
  38825. /*
  38826. * Mark roots, hoping that recursion limit is not normally hit.
  38827. * If recursion limit is hit, run additional reachability rounds
  38828. * starting from "temproots" until marking is complete.
  38829. *
  38830. * Marking happens in two phases: first we mark actual reachability
  38831. * roots (and run "temproots" to complete the process). Then we
  38832. * check which objects are unreachable and are finalizable; such
  38833. * objects are marked as FINALIZABLE and marked as reachability
  38834. * (and "temproots" is run again to complete the process).
  38835. *
  38836. * The heap finalize_list must also be marked as a reachability root.
  38837. * There may be objects on the list from a previous round if the
  38838. * previous run had finalizer skip flag.
  38839. */
  38840. duk__mark_roots_heap(heap); /* main reachability roots */
  38841. #ifdef DUK_USE_REFERENCE_COUNTING
  38842. duk__mark_refzero_list(heap); /* refzero_list treated as reachability roots */
  38843. #endif
  38844. duk__mark_temproots_by_heap_scan(heap); /* temproots */
  38845. duk__mark_finalizable(heap); /* mark finalizable as reachability roots */
  38846. duk__mark_finalize_list(heap); /* mark finalizer work list as reachability roots */
  38847. duk__mark_temproots_by_heap_scan(heap); /* temproots */
  38848. /*
  38849. * Sweep garbage and remove marking flags, and move objects with
  38850. * finalizers to the finalizer work list.
  38851. *
  38852. * Objects to be swept need to get their refcounts finalized before
  38853. * they are swept. In other words, their target object refcounts
  38854. * need to be decreased. This has to be done before freeing any
  38855. * objects to avoid decref'ing dangling pointers (which may happen
  38856. * even without bugs, e.g. with reference loops)
  38857. *
  38858. * Because strings don't point to other heap objects, similar
  38859. * finalization is not necessary for strings.
  38860. */
  38861. /* XXX: more emergency behavior, e.g. find smaller hash sizes etc */
  38862. #ifdef DUK_USE_REFERENCE_COUNTING
  38863. duk__finalize_refcounts(heap);
  38864. #endif
  38865. duk__sweep_heap(heap, flags, &count_keep_obj);
  38866. #if defined(DUK_USE_STRTAB_CHAIN)
  38867. duk__sweep_stringtable_chain(heap, &count_keep_str);
  38868. #elif defined(DUK_USE_STRTAB_PROBE)
  38869. duk__sweep_stringtable_probe(heap, &count_keep_str);
  38870. #else
  38871. #error internal error, invalid strtab options
  38872. #endif
  38873. #ifdef DUK_USE_REFERENCE_COUNTING
  38874. duk__clear_refzero_list_flags(heap);
  38875. #endif
  38876. duk__clear_finalize_list_flags(heap);
  38877. /*
  38878. * Object compaction (emergency only).
  38879. *
  38880. * Object compaction is a separate step after sweeping, as there is
  38881. * more free memory for it to work with. Also, currently compaction
  38882. * may insert new objects into the heap allocated list and the string
  38883. * table which we don't want to do during a sweep (the reachability
  38884. * flags of such objects would be incorrect). The objects inserted
  38885. * are currently:
  38886. *
  38887. * - a temporary duk_hbuffer for a new properties allocation
  38888. * - if array part is abandoned, string keys are interned
  38889. *
  38890. * The object insertions go to the front of the list, so they do not
  38891. * cause an infinite loop (they are not compacted).
  38892. */
  38893. if ((flags & DUK_MS_FLAG_EMERGENCY) &&
  38894. !(flags & DUK_MS_FLAG_NO_OBJECT_COMPACTION)) {
  38895. duk__compact_objects(heap);
  38896. }
  38897. /*
  38898. * String table resize check.
  38899. *
  38900. * Note: this may silently (and safely) fail if GC is caused by an
  38901. * allocation call in stringtable resize_hash(). Resize_hash()
  38902. * will prevent a recursive call to itself by setting the
  38903. * DUK_MS_FLAG_NO_STRINGTABLE_RESIZE in heap->mark_and_sweep_base_flags.
  38904. */
  38905. /* XXX: stringtable emergency compaction? */
  38906. #if defined(DUK_USE_MS_STRINGTABLE_RESIZE)
  38907. if (!(flags & DUK_MS_FLAG_NO_STRINGTABLE_RESIZE)) {
  38908. DUK_DD(DUK_DDPRINT("resize stringtable: %p", (void *) heap));
  38909. duk_heap_force_strtab_resize(heap);
  38910. } else {
  38911. DUK_D(DUK_DPRINT("stringtable resize skipped because DUK_MS_FLAG_NO_STRINGTABLE_RESIZE is set"));
  38912. }
  38913. #endif
  38914. /*
  38915. * Finalize objects in the finalization work list. Finalized
  38916. * objects are queued back to heap_allocated with FINALIZED set.
  38917. *
  38918. * Since finalizers may cause arbitrary side effects, they are
  38919. * prevented during string table and object property allocation
  38920. * resizing using the DUK_MS_FLAG_NO_FINALIZERS flag in
  38921. * heap->mark_and_sweep_base_flags. In this case the objects
  38922. * remain in the finalization work list after mark-and-sweep
  38923. * exits and they may be finalized on the next pass.
  38924. *
  38925. * Finalization currently happens inside "MARKANDSWEEP_RUNNING"
  38926. * protection (no mark-and-sweep may be triggered by the
  38927. * finalizers). As a side effect:
  38928. *
  38929. * 1) an out-of-memory error inside a finalizer will not
  38930. * cause a mark-and-sweep and may cause the finalizer
  38931. * to fail unnecessarily
  38932. *
  38933. * 2) any temporary objects whose refcount decreases to zero
  38934. * during finalization will not be put into refzero_list;
  38935. * they can only be collected by another mark-and-sweep
  38936. *
  38937. * This is not optimal, but since the sweep for this phase has
  38938. * already happened, this is probably good enough for now.
  38939. */
  38940. #if defined(DUK_USE_MARKANDSWEEP_FINALIZER_TORTURE)
  38941. /* Cannot simulate individual finalizers because finalize_list only
  38942. * contains objects with actual finalizers. But simulate side effects
  38943. * from finalization by doing a bogus function call and resizing the
  38944. * stacks.
  38945. */
  38946. if (flags & DUK_MS_FLAG_NO_FINALIZERS) {
  38947. DUK_D(DUK_DPRINT("skip mark-and-sweep torture finalizer, DUK_MS_FLAG_NO_FINALIZERS is set"));
  38948. } else if (!(thr->valstack != NULL && thr->callstack != NULL && thr->catchstack != NULL)) {
  38949. DUK_D(DUK_DPRINT("skip mark-and-sweep torture finalizer, thread not yet viable"));
  38950. } else {
  38951. DUK_D(DUK_DPRINT("run mark-and-sweep torture finalizer"));
  38952. duk__markandsweep_torture_finalizer(thr);
  38953. }
  38954. #endif /* DUK_USE_MARKANDSWEEP_FINALIZER_TORTURE */
  38955. if (flags & DUK_MS_FLAG_NO_FINALIZERS) {
  38956. DUK_D(DUK_DPRINT("finalizer run skipped because DUK_MS_FLAG_NO_FINALIZERS is set"));
  38957. } else {
  38958. duk__run_object_finalizers(heap);
  38959. }
  38960. /*
  38961. * Finish
  38962. */
  38963. DUK_HEAP_CLEAR_MARKANDSWEEP_RUNNING(heap);
  38964. /*
  38965. * Assertions after
  38966. */
  38967. #ifdef DUK_USE_ASSERTIONS
  38968. DUK_ASSERT(!DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap));
  38969. DUK_ASSERT(!DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED(heap));
  38970. DUK_ASSERT(heap->mark_and_sweep_recursion_depth == 0);
  38971. duk__assert_heaphdr_flags(heap);
  38972. #ifdef DUK_USE_REFERENCE_COUNTING
  38973. /* Note: DUK_HEAP_HAS_REFZERO_FREE_RUNNING(heap) may be true; a refcount
  38974. * finalizer may trigger a mark-and-sweep.
  38975. */
  38976. duk__assert_valid_refcounts(heap);
  38977. #endif /* DUK_USE_REFERENCE_COUNTING */
  38978. #endif /* DUK_USE_ASSERTIONS */
  38979. /*
  38980. * Reset trigger counter
  38981. */
  38982. #ifdef DUK_USE_VOLUNTARY_GC
  38983. tmp = (count_keep_obj + count_keep_str) / 256;
  38984. heap->mark_and_sweep_trigger_counter = (duk_int_t) (
  38985. (tmp * DUK_HEAP_MARK_AND_SWEEP_TRIGGER_MULT) +
  38986. DUK_HEAP_MARK_AND_SWEEP_TRIGGER_ADD);
  38987. DUK_D(DUK_DPRINT("garbage collect (mark-and-sweep) finished: %ld objects kept, %ld strings kept, trigger reset to %ld",
  38988. (long) count_keep_obj, (long) count_keep_str, (long) heap->mark_and_sweep_trigger_counter));
  38989. #else
  38990. DUK_D(DUK_DPRINT("garbage collect (mark-and-sweep) finished: %ld objects kept, %ld strings kept, no voluntary trigger",
  38991. (long) count_keep_obj, (long) count_keep_str));
  38992. #endif
  38993. return 0; /* OK */
  38994. }
  38995. #else /* DUK_USE_MARK_AND_SWEEP */
  38996. /* no mark-and-sweep gc */
  38997. #endif /* DUK_USE_MARK_AND_SWEEP */
  38998. #line 1 "duk_heap_memory.c"
  38999. /*
  39000. * Memory allocation handling.
  39001. */
  39002. /* include removed: duk_internal.h */
  39003. /*
  39004. * Helpers
  39005. *
  39006. * The fast path checks are done within a macro to ensure "inlining"
  39007. * while the slow path actions use a helper (which won't typically be
  39008. * inlined in size optimized builds).
  39009. */
  39010. #if defined(DUK_USE_MARK_AND_SWEEP) && defined(DUK_USE_VOLUNTARY_GC)
  39011. #define DUK__VOLUNTARY_PERIODIC_GC(heap) do { \
  39012. (heap)->mark_and_sweep_trigger_counter--; \
  39013. if ((heap)->mark_and_sweep_trigger_counter <= 0) { \
  39014. duk__run_voluntary_gc(heap); \
  39015. } \
  39016. } while (0)
  39017. DUK_LOCAL void duk__run_voluntary_gc(duk_heap *heap) {
  39018. if (DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)) {
  39019. DUK_DD(DUK_DDPRINT("mark-and-sweep in progress -> skip voluntary mark-and-sweep now"));
  39020. } else {
  39021. duk_small_uint_t flags;
  39022. duk_bool_t rc;
  39023. DUK_D(DUK_DPRINT("triggering voluntary mark-and-sweep"));
  39024. flags = 0;
  39025. rc = duk_heap_mark_and_sweep(heap, flags);
  39026. DUK_UNREF(rc);
  39027. }
  39028. }
  39029. #else
  39030. #define DUK__VOLUNTARY_PERIODIC_GC(heap) /* no voluntary gc */
  39031. #endif /* DUK_USE_MARK_AND_SWEEP && DUK_USE_VOLUNTARY_GC */
  39032. /*
  39033. * Allocate memory with garbage collection
  39034. */
  39035. #ifdef DUK_USE_MARK_AND_SWEEP
  39036. DUK_INTERNAL void *duk_heap_mem_alloc(duk_heap *heap, duk_size_t size) {
  39037. void *res;
  39038. duk_bool_t rc;
  39039. duk_small_int_t i;
  39040. DUK_ASSERT(heap != NULL);
  39041. DUK_ASSERT_DISABLE(size >= 0);
  39042. /*
  39043. * Voluntary periodic GC (if enabled)
  39044. */
  39045. DUK__VOLUNTARY_PERIODIC_GC(heap);
  39046. /*
  39047. * First attempt
  39048. */
  39049. #ifdef DUK_USE_GC_TORTURE
  39050. /* simulate alloc failure on every alloc (except when mark-and-sweep is running) */
  39051. if (!DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)) {
  39052. DUK_DDD(DUK_DDDPRINT("gc torture enabled, pretend that first alloc attempt fails"));
  39053. res = NULL;
  39054. DUK_UNREF(res);
  39055. goto skip_attempt;
  39056. }
  39057. #endif
  39058. res = heap->alloc_func(heap->heap_udata, size);
  39059. if (res || size == 0) {
  39060. /* for zero size allocations NULL is allowed */
  39061. return res;
  39062. }
  39063. #ifdef DUK_USE_GC_TORTURE
  39064. skip_attempt:
  39065. #endif
  39066. DUK_D(DUK_DPRINT("first alloc attempt failed, attempt to gc and retry"));
  39067. /*
  39068. * Avoid a GC if GC is already running. This can happen at a late
  39069. * stage in a GC when we try to e.g. resize the stringtable
  39070. * or compact objects.
  39071. */
  39072. if (DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)) {
  39073. DUK_D(DUK_DPRINT("duk_heap_mem_alloc() failed, gc in progress (gc skipped), alloc size %ld", (long) size));
  39074. return NULL;
  39075. }
  39076. /*
  39077. * Retry with several GC attempts. Initial attempts are made without
  39078. * emergency mode; later attempts use emergency mode which minimizes
  39079. * memory allocations forcibly.
  39080. */
  39081. for (i = 0; i < DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT; i++) {
  39082. duk_small_uint_t flags;
  39083. flags = 0;
  39084. if (i >= DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT - 1) {
  39085. flags |= DUK_MS_FLAG_EMERGENCY;
  39086. }
  39087. rc = duk_heap_mark_and_sweep(heap, flags);
  39088. DUK_UNREF(rc);
  39089. res = heap->alloc_func(heap->heap_udata, size);
  39090. if (res) {
  39091. DUK_D(DUK_DPRINT("duk_heap_mem_alloc() succeeded after gc (pass %ld), alloc size %ld",
  39092. (long) (i + 1), (long) size));
  39093. return res;
  39094. }
  39095. }
  39096. DUK_D(DUK_DPRINT("duk_heap_mem_alloc() failed even after gc, alloc size %ld", (long) size));
  39097. return NULL;
  39098. }
  39099. #else /* DUK_USE_MARK_AND_SWEEP */
  39100. /*
  39101. * Compared to a direct macro expansion this wrapper saves a few
  39102. * instructions because no heap dereferencing is required.
  39103. */
  39104. DUK_INTERNAL void *duk_heap_mem_alloc(duk_heap *heap, duk_size_t size) {
  39105. DUK_ASSERT(heap != NULL);
  39106. DUK_ASSERT_DISABLE(size >= 0);
  39107. return heap->alloc_func(heap->heap_udata, size);
  39108. }
  39109. #endif /* DUK_USE_MARK_AND_SWEEP */
  39110. DUK_INTERNAL void *duk_heap_mem_alloc_zeroed(duk_heap *heap, duk_size_t size) {
  39111. void *res;
  39112. DUK_ASSERT(heap != NULL);
  39113. DUK_ASSERT_DISABLE(size >= 0);
  39114. res = DUK_ALLOC(heap, size);
  39115. if (res) {
  39116. /* assume memset with zero size is OK */
  39117. DUK_MEMZERO(res, size);
  39118. }
  39119. return res;
  39120. }
  39121. /*
  39122. * Reallocate memory with garbage collection
  39123. */
  39124. #ifdef DUK_USE_MARK_AND_SWEEP
  39125. DUK_INTERNAL void *duk_heap_mem_realloc(duk_heap *heap, void *ptr, duk_size_t newsize) {
  39126. void *res;
  39127. duk_bool_t rc;
  39128. duk_small_int_t i;
  39129. DUK_ASSERT(heap != NULL);
  39130. /* ptr may be NULL */
  39131. DUK_ASSERT_DISABLE(newsize >= 0);
  39132. /*
  39133. * Voluntary periodic GC (if enabled)
  39134. */
  39135. DUK__VOLUNTARY_PERIODIC_GC(heap);
  39136. /*
  39137. * First attempt
  39138. */
  39139. #ifdef DUK_USE_GC_TORTURE
  39140. /* simulate alloc failure on every realloc (except when mark-and-sweep is running) */
  39141. if (!DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)) {
  39142. DUK_DDD(DUK_DDDPRINT("gc torture enabled, pretend that first realloc attempt fails"));
  39143. res = NULL;
  39144. DUK_UNREF(res);
  39145. goto skip_attempt;
  39146. }
  39147. #endif
  39148. res = heap->realloc_func(heap->heap_udata, ptr, newsize);
  39149. if (res || newsize == 0) {
  39150. /* for zero size allocations NULL is allowed */
  39151. return res;
  39152. }
  39153. #ifdef DUK_USE_GC_TORTURE
  39154. skip_attempt:
  39155. #endif
  39156. DUK_D(DUK_DPRINT("first realloc attempt failed, attempt to gc and retry"));
  39157. /*
  39158. * Avoid a GC if GC is already running. See duk_heap_mem_alloc().
  39159. */
  39160. if (DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)) {
  39161. DUK_D(DUK_DPRINT("duk_heap_mem_realloc() failed, gc in progress (gc skipped), alloc size %ld", (long) newsize));
  39162. return NULL;
  39163. }
  39164. /*
  39165. * Retry with several GC attempts. Initial attempts are made without
  39166. * emergency mode; later attempts use emergency mode which minimizes
  39167. * memory allocations forcibly.
  39168. */
  39169. for (i = 0; i < DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT; i++) {
  39170. duk_small_uint_t flags;
  39171. flags = 0;
  39172. if (i >= DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT - 1) {
  39173. flags |= DUK_MS_FLAG_EMERGENCY;
  39174. }
  39175. rc = duk_heap_mark_and_sweep(heap, flags);
  39176. DUK_UNREF(rc);
  39177. res = heap->realloc_func(heap->heap_udata, ptr, newsize);
  39178. if (res || newsize == 0) {
  39179. DUK_D(DUK_DPRINT("duk_heap_mem_realloc() succeeded after gc (pass %ld), alloc size %ld",
  39180. (long) (i + 1), (long) newsize));
  39181. return res;
  39182. }
  39183. }
  39184. DUK_D(DUK_DPRINT("duk_heap_mem_realloc() failed even after gc, alloc size %ld", (long) newsize));
  39185. return NULL;
  39186. }
  39187. #else /* DUK_USE_MARK_AND_SWEEP */
  39188. /* saves a few instructions to have this wrapper (see comment on duk_heap_mem_alloc) */
  39189. DUK_INTERNAL void *duk_heap_mem_realloc(duk_heap *heap, void *ptr, duk_size_t newsize) {
  39190. DUK_ASSERT(heap != NULL);
  39191. /* ptr may be NULL */
  39192. DUK_ASSERT_DISABLE(newsize >= 0);
  39193. return heap->realloc_func(heap->heap_udata, ptr, newsize);
  39194. }
  39195. #endif /* DUK_USE_MARK_AND_SWEEP */
  39196. /*
  39197. * Reallocate memory with garbage collection, using a callback to provide
  39198. * the current allocated pointer. This variant is used when a mark-and-sweep
  39199. * (e.g. finalizers) might change the original pointer.
  39200. */
  39201. #ifdef DUK_USE_MARK_AND_SWEEP
  39202. DUK_INTERNAL void *duk_heap_mem_realloc_indirect(duk_heap *heap, duk_mem_getptr cb, void *ud, duk_size_t newsize) {
  39203. void *res;
  39204. duk_bool_t rc;
  39205. duk_small_int_t i;
  39206. DUK_ASSERT(heap != NULL);
  39207. DUK_ASSERT_DISABLE(newsize >= 0);
  39208. /*
  39209. * Voluntary periodic GC (if enabled)
  39210. */
  39211. DUK__VOLUNTARY_PERIODIC_GC(heap);
  39212. /*
  39213. * First attempt
  39214. */
  39215. #ifdef DUK_USE_GC_TORTURE
  39216. /* simulate alloc failure on every realloc (except when mark-and-sweep is running) */
  39217. if (!DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)) {
  39218. DUK_DDD(DUK_DDDPRINT("gc torture enabled, pretend that first indirect realloc attempt fails"));
  39219. res = NULL;
  39220. DUK_UNREF(res);
  39221. goto skip_attempt;
  39222. }
  39223. #endif
  39224. res = heap->realloc_func(heap->heap_udata, cb(heap, ud), newsize);
  39225. if (res || newsize == 0) {
  39226. /* for zero size allocations NULL is allowed */
  39227. return res;
  39228. }
  39229. #ifdef DUK_USE_GC_TORTURE
  39230. skip_attempt:
  39231. #endif
  39232. DUK_D(DUK_DPRINT("first indirect realloc attempt failed, attempt to gc and retry"));
  39233. /*
  39234. * Avoid a GC if GC is already running. See duk_heap_mem_alloc().
  39235. */
  39236. if (DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)) {
  39237. DUK_D(DUK_DPRINT("duk_heap_mem_realloc_indirect() failed, gc in progress (gc skipped), alloc size %ld", (long) newsize));
  39238. return NULL;
  39239. }
  39240. /*
  39241. * Retry with several GC attempts. Initial attempts are made without
  39242. * emergency mode; later attempts use emergency mode which minimizes
  39243. * memory allocations forcibly.
  39244. */
  39245. for (i = 0; i < DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT; i++) {
  39246. duk_small_uint_t flags;
  39247. #ifdef DUK_USE_ASSERTIONS
  39248. void *ptr_pre; /* ptr before mark-and-sweep */
  39249. void *ptr_post;
  39250. #endif
  39251. #ifdef DUK_USE_ASSERTIONS
  39252. ptr_pre = cb(heap, ud);
  39253. #endif
  39254. flags = 0;
  39255. if (i >= DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT - 1) {
  39256. flags |= DUK_MS_FLAG_EMERGENCY;
  39257. }
  39258. rc = duk_heap_mark_and_sweep(heap, flags);
  39259. DUK_UNREF(rc);
  39260. #ifdef DUK_USE_ASSERTIONS
  39261. ptr_post = cb(heap, ud);
  39262. if (ptr_pre != ptr_post) {
  39263. /* useful for debugging */
  39264. DUK_DD(DUK_DDPRINT("note: base pointer changed by mark-and-sweep: %p -> %p",
  39265. (void *) ptr_pre, (void *) ptr_post));
  39266. }
  39267. #endif
  39268. /* Note: key issue here is to re-lookup the base pointer on every attempt.
  39269. * The pointer being reallocated may change after every mark-and-sweep.
  39270. */
  39271. res = heap->realloc_func(heap->heap_udata, cb(heap, ud), newsize);
  39272. if (res || newsize == 0) {
  39273. DUK_D(DUK_DPRINT("duk_heap_mem_realloc_indirect() succeeded after gc (pass %ld), alloc size %ld",
  39274. (long) (i + 1), (long) newsize));
  39275. return res;
  39276. }
  39277. }
  39278. DUK_D(DUK_DPRINT("duk_heap_mem_realloc_indirect() failed even after gc, alloc size %ld", (long) newsize));
  39279. return NULL;
  39280. }
  39281. #else /* DUK_USE_MARK_AND_SWEEP */
  39282. /* saves a few instructions to have this wrapper (see comment on duk_heap_mem_alloc) */
  39283. DUK_INTERNAL void *duk_heap_mem_realloc_indirect(duk_heap *heap, duk_mem_getptr cb, void *ud, duk_size_t newsize) {
  39284. return heap->realloc_func(heap->heap_udata, cb(heap, ud), newsize);
  39285. }
  39286. #endif /* DUK_USE_MARK_AND_SWEEP */
  39287. /*
  39288. * Free memory
  39289. */
  39290. #ifdef DUK_USE_MARK_AND_SWEEP
  39291. DUK_INTERNAL void duk_heap_mem_free(duk_heap *heap, void *ptr) {
  39292. DUK_ASSERT(heap != NULL);
  39293. /* ptr may be NULL */
  39294. /* Must behave like a no-op with NULL and any pointer returned from
  39295. * malloc/realloc with zero size.
  39296. */
  39297. heap->free_func(heap->heap_udata, ptr);
  39298. /* Count free operations toward triggering a GC but never actually trigger
  39299. * a GC from a free. Otherwise code which frees internal structures would
  39300. * need to put in NULLs at every turn to ensure the object is always in
  39301. * consistent state for a mark-and-sweep.
  39302. */
  39303. #ifdef DUK_USE_VOLUNTARY_GC
  39304. heap->mark_and_sweep_trigger_counter--;
  39305. #endif
  39306. }
  39307. #else
  39308. /* saves a few instructions to have this wrapper (see comment on duk_heap_mem_alloc) */
  39309. DUK_INTERNAL void duk_heap_mem_free(duk_heap *heap, void *ptr) {
  39310. DUK_ASSERT(heap != NULL);
  39311. /* ptr may be NULL */
  39312. /* Note: must behave like a no-op with NULL and any pointer
  39313. * returned from malloc/realloc with zero size.
  39314. */
  39315. heap->free_func(heap->heap_udata, ptr);
  39316. }
  39317. #endif
  39318. #line 1 "duk_heap_misc.c"
  39319. /*
  39320. * Support functions for duk_heap.
  39321. */
  39322. /* include removed: duk_internal.h */
  39323. #if defined(DUK_USE_DOUBLE_LINKED_HEAP) && defined(DUK_USE_REFERENCE_COUNTING)
  39324. /* arbitrary remove only works with double linked heap, and is only required by
  39325. * reference counting so far.
  39326. */
  39327. DUK_INTERNAL void duk_heap_remove_any_from_heap_allocated(duk_heap *heap, duk_heaphdr *hdr) {
  39328. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(hdr) != DUK_HTYPE_STRING);
  39329. if (DUK_HEAPHDR_GET_PREV(heap, hdr)) {
  39330. DUK_HEAPHDR_SET_NEXT(heap, DUK_HEAPHDR_GET_PREV(heap, hdr), DUK_HEAPHDR_GET_NEXT(heap, hdr));
  39331. } else {
  39332. heap->heap_allocated = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  39333. }
  39334. if (DUK_HEAPHDR_GET_NEXT(heap, hdr)) {
  39335. DUK_HEAPHDR_SET_PREV(heap, DUK_HEAPHDR_GET_NEXT(heap, hdr), DUK_HEAPHDR_GET_PREV(heap, hdr));
  39336. } else {
  39337. ;
  39338. }
  39339. }
  39340. #endif
  39341. DUK_INTERNAL void duk_heap_insert_into_heap_allocated(duk_heap *heap, duk_heaphdr *hdr) {
  39342. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(hdr) != DUK_HTYPE_STRING);
  39343. #ifdef DUK_USE_DOUBLE_LINKED_HEAP
  39344. if (heap->heap_allocated) {
  39345. DUK_ASSERT(DUK_HEAPHDR_GET_PREV(heap, heap->heap_allocated) == NULL);
  39346. DUK_HEAPHDR_SET_PREV(heap, heap->heap_allocated, hdr);
  39347. }
  39348. DUK_HEAPHDR_SET_PREV(heap, hdr, NULL);
  39349. #endif
  39350. DUK_HEAPHDR_SET_NEXT(heap, hdr, heap->heap_allocated);
  39351. heap->heap_allocated = hdr;
  39352. }
  39353. #ifdef DUK_USE_INTERRUPT_COUNTER
  39354. DUK_INTERNAL void duk_heap_switch_thread(duk_heap *heap, duk_hthread *new_thr) {
  39355. duk_hthread *curr_thr;
  39356. DUK_ASSERT(heap != NULL);
  39357. if (new_thr != NULL) {
  39358. curr_thr = heap->curr_thread;
  39359. if (curr_thr == NULL) {
  39360. /* For initial entry use default value; zero forces an
  39361. * interrupt before executing the first insturction.
  39362. */
  39363. DUK_DD(DUK_DDPRINT("switch thread, initial entry, init default interrupt counter"));
  39364. new_thr->interrupt_counter = 0;
  39365. new_thr->interrupt_init = 0;
  39366. } else {
  39367. /* Copy interrupt counter/init value state to new thread (if any).
  39368. * It's OK for new_thr to be the same as curr_thr.
  39369. */
  39370. #if defined(DUK_USE_DEBUG)
  39371. if (new_thr != curr_thr) {
  39372. DUK_DD(DUK_DDPRINT("switch thread, not initial entry, copy interrupt counter"));
  39373. }
  39374. #endif
  39375. new_thr->interrupt_counter = curr_thr->interrupt_counter;
  39376. new_thr->interrupt_init = curr_thr->interrupt_init;
  39377. }
  39378. } else {
  39379. DUK_DD(DUK_DDPRINT("switch thread, new thread is NULL, no interrupt counter changes"));
  39380. }
  39381. heap->curr_thread = new_thr; /* may be NULL */
  39382. }
  39383. #endif /* DUK_USE_INTERRUPT_COUNTER */
  39384. #line 1 "duk_heap_refcount.c"
  39385. /*
  39386. * Reference counting implementation.
  39387. */
  39388. /* include removed: duk_internal.h */
  39389. #ifdef DUK_USE_REFERENCE_COUNTING
  39390. #ifndef DUK_USE_DOUBLE_LINKED_HEAP
  39391. #error internal error, reference counting requires a double linked heap
  39392. #endif
  39393. /*
  39394. * Misc
  39395. */
  39396. DUK_LOCAL void duk__queue_refzero(duk_heap *heap, duk_heaphdr *hdr) {
  39397. /* tail insert: don't disturb head in case refzero is running */
  39398. if (heap->refzero_list != NULL) {
  39399. duk_heaphdr *hdr_prev;
  39400. hdr_prev = heap->refzero_list_tail;
  39401. DUK_ASSERT(hdr_prev != NULL);
  39402. DUK_ASSERT(DUK_HEAPHDR_GET_NEXT(heap, hdr_prev) == NULL);
  39403. DUK_HEAPHDR_SET_NEXT(heap, hdr, NULL);
  39404. DUK_HEAPHDR_SET_PREV(heap, hdr, hdr_prev);
  39405. DUK_HEAPHDR_SET_NEXT(heap, hdr_prev, hdr);
  39406. heap->refzero_list_tail = hdr;
  39407. } else {
  39408. DUK_ASSERT(heap->refzero_list_tail == NULL);
  39409. DUK_HEAPHDR_SET_NEXT(heap, hdr, NULL);
  39410. DUK_HEAPHDR_SET_PREV(heap, hdr, NULL);
  39411. heap->refzero_list = hdr;
  39412. heap->refzero_list_tail = hdr;
  39413. }
  39414. }
  39415. /*
  39416. * Heap object refcount finalization.
  39417. *
  39418. * When an object is about to be freed, all other objects it refers to must
  39419. * be decref'd. Refcount finalization does NOT free the object or its inner
  39420. * allocations (mark-and-sweep shares these helpers), it just manipulates
  39421. * the refcounts.
  39422. *
  39423. * Note that any of the decref's may cause a refcount to drop to zero, BUT
  39424. * it will not be processed inline; instead, because refzero is already
  39425. * running, the objects will just be queued to refzero list and processed
  39426. * later. This eliminates C recursion.
  39427. */
  39428. DUK_LOCAL void duk__refcount_finalize_hobject(duk_hthread *thr, duk_hobject *h) {
  39429. duk_uint_fast32_t i;
  39430. DUK_ASSERT(h);
  39431. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) h) == DUK_HTYPE_OBJECT);
  39432. /* XXX: better to get base and walk forwards? */
  39433. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(h); i++) {
  39434. duk_hstring *key = DUK_HOBJECT_E_GET_KEY(thr->heap, h, i);
  39435. if (!key) {
  39436. continue;
  39437. }
  39438. duk_heaphdr_decref(thr, (duk_heaphdr *) key);
  39439. if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, h, i)) {
  39440. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, h, i));
  39441. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, h, i));
  39442. } else {
  39443. duk_tval_decref(thr, DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, h, i));
  39444. }
  39445. }
  39446. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(h); i++) {
  39447. duk_tval_decref(thr, DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, h, i));
  39448. }
  39449. /* hash part is a 'weak reference' and does not contribute */
  39450. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h));
  39451. if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h)) {
  39452. duk_hcompiledfunction *f = (duk_hcompiledfunction *) h;
  39453. duk_tval *tv, *tv_end;
  39454. duk_hobject **funcs, **funcs_end;
  39455. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, f) != NULL); /* compiled functions must be created 'atomically' */
  39456. tv = DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, f);
  39457. tv_end = DUK_HCOMPILEDFUNCTION_GET_CONSTS_END(thr->heap, f);
  39458. while (tv < tv_end) {
  39459. duk_tval_decref(thr, tv);
  39460. tv++;
  39461. }
  39462. funcs = DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, f);
  39463. funcs_end = DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(thr->heap, f);
  39464. while (funcs < funcs_end) {
  39465. duk_heaphdr_decref(thr, (duk_heaphdr *) *funcs);
  39466. funcs++;
  39467. }
  39468. duk_heaphdr_decref(thr, (duk_heaphdr *) DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, f));
  39469. } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
  39470. duk_hnativefunction *f = (duk_hnativefunction *) h;
  39471. DUK_UNREF(f);
  39472. /* nothing to finalize */
  39473. } else if (DUK_HOBJECT_IS_BUFFEROBJECT(h)) {
  39474. duk_hbufferobject *b = (duk_hbufferobject *) h;
  39475. if (b->buf) {
  39476. duk_heaphdr_decref(thr, (duk_heaphdr *) b->buf);
  39477. }
  39478. } else if (DUK_HOBJECT_IS_THREAD(h)) {
  39479. duk_hthread *t = (duk_hthread *) h;
  39480. duk_tval *tv;
  39481. tv = t->valstack;
  39482. while (tv < t->valstack_top) {
  39483. duk_tval_decref(thr, tv);
  39484. tv++;
  39485. }
  39486. for (i = 0; i < (duk_uint_fast32_t) t->callstack_top; i++) {
  39487. duk_activation *act = t->callstack + i;
  39488. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) DUK_ACT_GET_FUNC(act));
  39489. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) act->var_env);
  39490. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) act->lex_env);
  39491. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  39492. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) act->prev_caller);
  39493. #endif
  39494. }
  39495. #if 0 /* nothing now */
  39496. for (i = 0; i < (duk_uint_fast32_t) t->catchstack_top; i++) {
  39497. duk_catcher *cat = t->catchstack + i;
  39498. }
  39499. #endif
  39500. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  39501. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) t->builtins[i]);
  39502. }
  39503. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) t->resumer);
  39504. }
  39505. }
  39506. DUK_INTERNAL void duk_heaphdr_refcount_finalize(duk_hthread *thr, duk_heaphdr *hdr) {
  39507. DUK_ASSERT(hdr);
  39508. switch ((int) DUK_HEAPHDR_GET_TYPE(hdr)) {
  39509. case DUK_HTYPE_OBJECT:
  39510. duk__refcount_finalize_hobject(thr, (duk_hobject *) hdr);
  39511. break;
  39512. case DUK_HTYPE_BUFFER:
  39513. /* nothing to finalize */
  39514. break;
  39515. case DUK_HTYPE_STRING:
  39516. /* cannot happen: strings are not put into refzero list (they don't even have the next/prev pointers) */
  39517. default:
  39518. DUK_UNREACHABLE();
  39519. }
  39520. }
  39521. #if defined(DUK_USE_REFZERO_FINALIZER_TORTURE)
  39522. DUK_LOCAL duk_ret_t duk__refcount_fake_finalizer(duk_context *ctx) {
  39523. DUK_UNREF(ctx);
  39524. DUK_D(DUK_DPRINT("fake refcount torture finalizer executed"));
  39525. #if 0
  39526. DUK_DD(DUK_DDPRINT("fake torture finalizer for: %!T", duk_get_tval(ctx, 0)));
  39527. #endif
  39528. /* Require a lot of stack to force a value stack grow/shrink. */
  39529. duk_require_stack(ctx, 100000);
  39530. /* XXX: do something to force a callstack grow/shrink, perhaps
  39531. * just a manual forced resize?
  39532. */
  39533. return 0;
  39534. }
  39535. DUK_LOCAL void duk__refcount_run_torture_finalizer(duk_hthread *thr, duk_hobject *obj) {
  39536. duk_context *ctx;
  39537. duk_int_t rc;
  39538. DUK_ASSERT(thr != NULL);
  39539. DUK_ASSERT(obj != NULL);
  39540. ctx = (duk_context *) thr;
  39541. /* Avoid fake finalization for the duk__refcount_fake_finalizer function
  39542. * itself, otherwise we're in infinite recursion.
  39543. */
  39544. if (DUK_HOBJECT_HAS_NATIVEFUNCTION(obj)) {
  39545. if (((duk_hnativefunction *) obj)->func == duk__refcount_fake_finalizer) {
  39546. DUK_DD(DUK_DDPRINT("avoid fake torture finalizer for duk__refcount_fake_finalizer itself"));
  39547. return;
  39548. }
  39549. }
  39550. /* Avoid fake finalization when callstack limit has been reached.
  39551. * Otherwise a callstack limit error will be created, then refzero'ed,
  39552. * and we're in an infinite loop.
  39553. */
  39554. if (thr->heap->call_recursion_depth >= thr->heap->call_recursion_limit ||
  39555. thr->callstack_size + 2 * DUK_CALLSTACK_GROW_STEP >= thr->callstack_max /*approximate*/) {
  39556. DUK_D(DUK_DPRINT("call recursion depth reached, avoid fake torture finalizer"));
  39557. return;
  39558. }
  39559. /* Run fake finalizer. Avoid creating new refzero queue entries
  39560. * so that we are not forced into a forever loop.
  39561. */
  39562. duk_push_c_function(ctx, duk__refcount_fake_finalizer, 1 /*nargs*/);
  39563. duk_push_hobject(ctx, obj);
  39564. rc = duk_pcall(ctx, 1);
  39565. DUK_UNREF(rc); /* ignored */
  39566. duk_pop(ctx);
  39567. }
  39568. #endif /* DUK_USE_REFZERO_FINALIZER_TORTURE */
  39569. /*
  39570. * Refcount memory freeing loop.
  39571. *
  39572. * Frees objects in the refzero_pending list until the list becomes
  39573. * empty. When an object is freed, its references get decref'd and
  39574. * may cause further objects to be queued for freeing.
  39575. *
  39576. * This could be expanded to allow incremental freeing: just bail out
  39577. * early and resume at a future alloc/decref/refzero.
  39578. */
  39579. DUK_LOCAL void duk__refzero_free_pending(duk_hthread *thr) {
  39580. duk_heaphdr *h1, *h2;
  39581. duk_heap *heap;
  39582. duk_int_t count = 0;
  39583. DUK_ASSERT(thr != NULL);
  39584. DUK_ASSERT(thr->heap != NULL);
  39585. heap = thr->heap;
  39586. DUK_ASSERT(heap != NULL);
  39587. /*
  39588. * Detect recursive invocation
  39589. */
  39590. if (DUK_HEAP_HAS_REFZERO_FREE_RUNNING(heap)) {
  39591. DUK_DDD(DUK_DDDPRINT("refzero free running, skip run"));
  39592. return;
  39593. }
  39594. /*
  39595. * Churn refzero_list until empty
  39596. */
  39597. DUK_HEAP_SET_REFZERO_FREE_RUNNING(heap);
  39598. while (heap->refzero_list) {
  39599. duk_hobject *obj;
  39600. duk_bool_t rescued = 0;
  39601. /*
  39602. * Pick an object from the head (don't remove yet).
  39603. */
  39604. h1 = heap->refzero_list;
  39605. obj = (duk_hobject *) h1;
  39606. DUK_DD(DUK_DDPRINT("refzero processing %p: %!O", (void *) h1, (duk_heaphdr *) h1));
  39607. DUK_ASSERT(DUK_HEAPHDR_GET_PREV(heap, h1) == NULL);
  39608. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(h1) == DUK_HTYPE_OBJECT); /* currently, always the case */
  39609. #if defined(DUK_USE_REFZERO_FINALIZER_TORTURE)
  39610. /* Torture option to shake out finalizer side effect issues:
  39611. * make a bogus function call for every finalizable object,
  39612. * essentially simulating the case where everything has a
  39613. * finalizer.
  39614. */
  39615. DUK_DD(DUK_DDPRINT("refzero torture enabled, fake finalizer"));
  39616. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(h1) == 0);
  39617. DUK_HEAPHDR_PREINC_REFCOUNT(h1); /* bump refcount to prevent refzero during finalizer processing */
  39618. duk__refcount_run_torture_finalizer(thr, obj); /* must never longjmp */
  39619. DUK_HEAPHDR_PREDEC_REFCOUNT(h1); /* remove artificial bump */
  39620. DUK_ASSERT_DISABLE(h1->h_refcount >= 0); /* refcount is unsigned, so always true */
  39621. #endif
  39622. /*
  39623. * Finalizer check.
  39624. *
  39625. * Note: running a finalizer may have arbitrary side effects, e.g.
  39626. * queue more objects on refzero_list (tail), or even trigger a
  39627. * mark-and-sweep.
  39628. *
  39629. * Note: quick reject check should match vast majority of
  39630. * objects and must be safe (not throw any errors, ever).
  39631. */
  39632. /* XXX: If object has FINALIZED, it was finalized by mark-and-sweep on
  39633. * its previous run. Any point in running finalizer again here? If
  39634. * finalization semantics is changed so that finalizer is only run once,
  39635. * checking for FINALIZED would happen here.
  39636. */
  39637. /* A finalizer is looked up from the object and up its prototype chain
  39638. * (which allows inherited finalizers).
  39639. */
  39640. if (duk_hobject_hasprop_raw(thr, obj, DUK_HTHREAD_STRING_INT_FINALIZER(thr))) {
  39641. DUK_DDD(DUK_DDDPRINT("object has a finalizer, run it"));
  39642. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(h1) == 0);
  39643. DUK_HEAPHDR_PREINC_REFCOUNT(h1); /* bump refcount to prevent refzero during finalizer processing */
  39644. duk_hobject_run_finalizer(thr, obj); /* must never longjmp */
  39645. DUK_HEAPHDR_PREDEC_REFCOUNT(h1); /* remove artificial bump */
  39646. DUK_ASSERT_DISABLE(h1->h_refcount >= 0); /* refcount is unsigned, so always true */
  39647. if (DUK_HEAPHDR_GET_REFCOUNT(h1) != 0) {
  39648. DUK_DDD(DUK_DDDPRINT("-> object refcount after finalization non-zero, object will be rescued"));
  39649. rescued = 1;
  39650. } else {
  39651. DUK_DDD(DUK_DDDPRINT("-> object refcount still zero after finalization, object will be freed"));
  39652. }
  39653. }
  39654. /* Refzero head is still the same. This is the case even if finalizer
  39655. * inserted more refzero objects; they are inserted to the tail.
  39656. */
  39657. DUK_ASSERT(h1 == heap->refzero_list);
  39658. /*
  39659. * Remove the object from the refzero list. This cannot be done
  39660. * before a possible finalizer has been executed; the finalizer
  39661. * may trigger a mark-and-sweep, and mark-and-sweep must be able
  39662. * to traverse a complete refzero_list.
  39663. */
  39664. h2 = DUK_HEAPHDR_GET_NEXT(heap, h1);
  39665. if (h2) {
  39666. DUK_HEAPHDR_SET_PREV(heap, h2, NULL); /* not strictly necessary */
  39667. heap->refzero_list = h2;
  39668. } else {
  39669. heap->refzero_list = NULL;
  39670. heap->refzero_list_tail = NULL;
  39671. }
  39672. /*
  39673. * Rescue or free.
  39674. */
  39675. if (rescued) {
  39676. /* yes -> move back to heap allocated */
  39677. DUK_DD(DUK_DDPRINT("object rescued during refcount finalization: %p", (void *) h1));
  39678. DUK_HEAPHDR_SET_PREV(heap, h1, NULL);
  39679. DUK_HEAPHDR_SET_NEXT(heap, h1, heap->heap_allocated);
  39680. heap->heap_allocated = h1;
  39681. } else {
  39682. /* no -> decref members, then free */
  39683. duk__refcount_finalize_hobject(thr, obj);
  39684. duk_heap_free_heaphdr_raw(heap, h1);
  39685. }
  39686. count++;
  39687. }
  39688. DUK_HEAP_CLEAR_REFZERO_FREE_RUNNING(heap);
  39689. DUK_DDD(DUK_DDDPRINT("refzero processed %ld objects", (long) count));
  39690. /*
  39691. * Once the whole refzero cascade has been freed, check for
  39692. * a voluntary mark-and-sweep.
  39693. */
  39694. #if defined(DUK_USE_MARK_AND_SWEEP) && defined(DUK_USE_VOLUNTARY_GC)
  39695. /* 'count' is more or less comparable to normal trigger counter update
  39696. * which happens in memory block (re)allocation.
  39697. */
  39698. heap->mark_and_sweep_trigger_counter -= count;
  39699. if (heap->mark_and_sweep_trigger_counter <= 0) {
  39700. duk_bool_t rc;
  39701. duk_small_uint_t flags = 0; /* not emergency */
  39702. DUK_D(DUK_DPRINT("refcount triggering mark-and-sweep"));
  39703. rc = duk_heap_mark_and_sweep(heap, flags);
  39704. DUK_UNREF(rc);
  39705. DUK_D(DUK_DPRINT("refcount triggered mark-and-sweep => rc %ld", (long) rc));
  39706. }
  39707. #endif /* DUK_USE_MARK_AND_SWEEP && DUK_USE_VOLUNTARY_GC */
  39708. }
  39709. /*
  39710. * Incref and decref functions.
  39711. *
  39712. * Decref may trigger immediate refzero handling, which may free and finalize
  39713. * an arbitrary number of objects.
  39714. *
  39715. */
  39716. DUK_INTERNAL void duk_heaphdr_refzero(duk_hthread *thr, duk_heaphdr *h) {
  39717. duk_heap *heap;
  39718. DUK_ASSERT(thr != NULL);
  39719. DUK_ASSERT(h != NULL);
  39720. heap = thr->heap;
  39721. DUK_DDD(DUK_DDDPRINT("refzero %p: %!O", (void *) h, (duk_heaphdr *) h));
  39722. #ifdef DUK_USE_MARK_AND_SWEEP
  39723. /*
  39724. * If mark-and-sweep is running, don't process 'refzero' situations at all.
  39725. * They may happen because mark-and-sweep needs to finalize refcounts for
  39726. * each object it sweeps. Otherwise the target objects of swept objects
  39727. * would have incorrect refcounts.
  39728. *
  39729. * Note: mark-and-sweep could use a separate decref handler to avoid coming
  39730. * here at all. However, mark-and-sweep may also call finalizers, which
  39731. * can do arbitrary operations and would use this decref variant anyway.
  39732. */
  39733. if (DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)) {
  39734. DUK_DDD(DUK_DDDPRINT("refzero handling suppressed when mark-and-sweep running, object: %p", (void *) h));
  39735. return;
  39736. }
  39737. #endif
  39738. switch ((duk_small_int_t) DUK_HEAPHDR_GET_TYPE(h)) {
  39739. case DUK_HTYPE_STRING:
  39740. /*
  39741. * Strings have no internal references but do have "weak"
  39742. * references in the string cache. Also note that strings
  39743. * are not on the heap_allocated list like other heap
  39744. * elements.
  39745. */
  39746. duk_heap_strcache_string_remove(heap, (duk_hstring *) h);
  39747. duk_heap_string_remove(heap, (duk_hstring *) h);
  39748. duk_heap_free_heaphdr_raw(heap, h);
  39749. break;
  39750. case DUK_HTYPE_OBJECT:
  39751. /*
  39752. * Objects have internal references. Must finalize through
  39753. * the "refzero" work list.
  39754. */
  39755. duk_heap_remove_any_from_heap_allocated(heap, h);
  39756. duk__queue_refzero(heap, h);
  39757. duk__refzero_free_pending(thr);
  39758. break;
  39759. case DUK_HTYPE_BUFFER:
  39760. /*
  39761. * Buffers have no internal references. However, a dynamic
  39762. * buffer has a separate allocation for the buffer. This is
  39763. * freed by duk_heap_free_heaphdr_raw().
  39764. */
  39765. duk_heap_remove_any_from_heap_allocated(heap, h);
  39766. duk_heap_free_heaphdr_raw(heap, h);
  39767. break;
  39768. default:
  39769. DUK_D(DUK_DPRINT("invalid heap type in decref: %ld", (long) DUK_HEAPHDR_GET_TYPE(h)));
  39770. DUK_UNREACHABLE();
  39771. }
  39772. }
  39773. #if !defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  39774. DUK_INTERNAL void duk_tval_incref(duk_tval *tv) {
  39775. DUK_ASSERT(tv != NULL);
  39776. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  39777. duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
  39778. DUK_ASSERT(h != NULL);
  39779. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  39780. DUK_ASSERT_DISABLE(h->h_refcount >= 0);
  39781. DUK_HEAPHDR_PREINC_REFCOUNT(h);
  39782. }
  39783. }
  39784. #endif
  39785. #if 0 /* unused */
  39786. DUK_INTERNAL void duk_tval_incref_allownull(duk_tval *tv) {
  39787. if (tv == NULL) {
  39788. return;
  39789. }
  39790. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  39791. duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
  39792. DUK_ASSERT(h != NULL);
  39793. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  39794. DUK_ASSERT_DISABLE(h->h_refcount >= 0);
  39795. DUK_HEAPHDR_PREINC_REFCOUNT(h);
  39796. }
  39797. }
  39798. #endif
  39799. DUK_INTERNAL void duk_tval_decref(duk_hthread *thr, duk_tval *tv) {
  39800. DUK_ASSERT(thr != NULL);
  39801. DUK_ASSERT(tv != NULL);
  39802. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  39803. duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
  39804. DUK_ASSERT(h != NULL);
  39805. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  39806. duk_heaphdr_decref(thr, h);
  39807. }
  39808. }
  39809. #if 0 /* unused */
  39810. DUK_INTERNAL void duk_tval_decref_allownull(duk_hthread *thr, duk_tval *tv) {
  39811. DUK_ASSERT(thr != NULL);
  39812. if (tv == NULL) {
  39813. return;
  39814. }
  39815. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  39816. duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
  39817. DUK_ASSERT(h != NULL);
  39818. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  39819. duk_heaphdr_decref(thr, h);
  39820. }
  39821. }
  39822. #endif
  39823. #if !defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  39824. DUK_INTERNAL void duk_heaphdr_incref(duk_heaphdr *h) {
  39825. DUK_ASSERT(h != NULL);
  39826. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  39827. DUK_ASSERT_DISABLE(DUK_HEAPHDR_GET_REFCOUNT(h) >= 0);
  39828. DUK_HEAPHDR_PREINC_REFCOUNT(h);
  39829. }
  39830. #endif
  39831. #if 0 /* unused */
  39832. DUK_INTERNAL void duk_heaphdr_incref_allownull(duk_heaphdr *h) {
  39833. if (h == NULL) {
  39834. return;
  39835. }
  39836. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  39837. DUK_ASSERT_DISABLE(DUK_HEAPHDR_GET_REFCOUNT(h) >= 0);
  39838. DUK_HEAPHDR_PREINC_REFCOUNT(h);
  39839. }
  39840. #endif
  39841. DUK_INTERNAL void duk_heaphdr_decref(duk_hthread *thr, duk_heaphdr *h) {
  39842. DUK_ASSERT(thr != NULL);
  39843. DUK_ASSERT(thr->heap != NULL);
  39844. DUK_ASSERT(h != NULL);
  39845. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  39846. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(h) >= 1);
  39847. if (DUK_HEAPHDR_PREDEC_REFCOUNT(h) != 0) {
  39848. return;
  39849. }
  39850. duk_heaphdr_refzero(thr, h);
  39851. }
  39852. DUK_INTERNAL void duk_heaphdr_decref_allownull(duk_hthread *thr, duk_heaphdr *h) {
  39853. DUK_ASSERT(thr != NULL);
  39854. DUK_ASSERT(thr->heap != NULL);
  39855. if (h == NULL) {
  39856. return;
  39857. }
  39858. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  39859. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(h) >= 1);
  39860. if (DUK_HEAPHDR_PREDEC_REFCOUNT(h) != 0) {
  39861. return;
  39862. }
  39863. duk_heaphdr_refzero(thr, h);
  39864. }
  39865. #else
  39866. /* no refcounting */
  39867. #endif /* DUK_USE_REFERENCE_COUNTING */
  39868. #line 1 "duk_heap_stringcache.c"
  39869. /*
  39870. * String cache.
  39871. *
  39872. * Provides a cache to optimize indexed string lookups. The cache keeps
  39873. * track of (byte offset, char offset) states for a fixed number of strings.
  39874. * Otherwise we'd need to scan from either end of the string, as we store
  39875. * strings in (extended) UTF-8.
  39876. */
  39877. /* include removed: duk_internal.h */
  39878. /*
  39879. * Delete references to given hstring from the heap string cache.
  39880. *
  39881. * String cache references are 'weak': they are not counted towards
  39882. * reference counts, nor serve as roots for mark-and-sweep. When an
  39883. * object is about to be freed, such references need to be removed.
  39884. */
  39885. DUK_INTERNAL void duk_heap_strcache_string_remove(duk_heap *heap, duk_hstring *h) {
  39886. duk_small_int_t i;
  39887. for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
  39888. duk_strcache *c = heap->strcache + i;
  39889. if (c->h == h) {
  39890. DUK_DD(DUK_DDPRINT("deleting weak strcache reference to hstring %p from heap %p",
  39891. (void *) h, (void *) heap));
  39892. c->h = NULL;
  39893. /* XXX: the string shouldn't appear twice, but we now loop to the
  39894. * end anyway; if fixed, add a looping assertion to ensure there
  39895. * is no duplicate.
  39896. */
  39897. }
  39898. }
  39899. }
  39900. /*
  39901. * String scanning helpers
  39902. *
  39903. * All bytes other than UTF-8 continuation bytes ([0x80,0xbf]) are
  39904. * considered to contribute a character. This must match how string
  39905. * character length is computed.
  39906. */
  39907. DUK_LOCAL const duk_uint8_t *duk__scan_forwards(const duk_uint8_t *p, const duk_uint8_t *q, duk_uint_fast32_t n) {
  39908. while (n > 0) {
  39909. for (;;) {
  39910. p++;
  39911. if (p >= q) {
  39912. return NULL;
  39913. }
  39914. if ((*p & 0xc0) != 0x80) {
  39915. break;
  39916. }
  39917. }
  39918. n--;
  39919. }
  39920. return p;
  39921. }
  39922. DUK_LOCAL const duk_uint8_t *duk__scan_backwards(const duk_uint8_t *p, const duk_uint8_t *q, duk_uint_fast32_t n) {
  39923. while (n > 0) {
  39924. for (;;) {
  39925. p--;
  39926. if (p < q) {
  39927. return NULL;
  39928. }
  39929. if ((*p & 0xc0) != 0x80) {
  39930. break;
  39931. }
  39932. }
  39933. n--;
  39934. }
  39935. return p;
  39936. }
  39937. /*
  39938. * Convert char offset to byte offset
  39939. *
  39940. * Avoid using the string cache if possible: for ASCII strings byte and
  39941. * char offsets are equal and for short strings direct scanning may be
  39942. * better than using the string cache (which may evict a more important
  39943. * entry).
  39944. *
  39945. * Typing now assumes 32-bit string byte/char offsets (duk_uint_fast32_t).
  39946. * Better typing might be to use duk_size_t.
  39947. */
  39948. DUK_INTERNAL duk_uint_fast32_t duk_heap_strcache_offset_char2byte(duk_hthread *thr, duk_hstring *h, duk_uint_fast32_t char_offset) {
  39949. duk_heap *heap;
  39950. duk_strcache *sce;
  39951. duk_uint_fast32_t byte_offset;
  39952. duk_small_int_t i;
  39953. duk_bool_t use_cache;
  39954. duk_uint_fast32_t dist_start, dist_end, dist_sce;
  39955. const duk_uint8_t *p_start;
  39956. const duk_uint8_t *p_end;
  39957. const duk_uint8_t *p_found;
  39958. if (char_offset > DUK_HSTRING_GET_CHARLEN(h)) {
  39959. goto error;
  39960. }
  39961. /*
  39962. * For ASCII strings, the answer is simple.
  39963. */
  39964. if (DUK_HSTRING_IS_ASCII(h)) {
  39965. /* clen == blen -> pure ascii */
  39966. return char_offset;
  39967. }
  39968. /*
  39969. * For non-ASCII strings, we need to scan forwards or backwards
  39970. * from some starting point. The starting point may be the start
  39971. * or end of the string, or some cached midpoint in the string
  39972. * cache.
  39973. *
  39974. * For "short" strings we simply scan without checking or updating
  39975. * the cache. For longer strings we check and update the cache as
  39976. * necessary, inserting a new cache entry if none exists.
  39977. */
  39978. DUK_DDD(DUK_DDDPRINT("non-ascii string %p, char_offset=%ld, clen=%ld, blen=%ld",
  39979. (void *) h, (long) char_offset,
  39980. (long) DUK_HSTRING_GET_CHARLEN(h),
  39981. (long) DUK_HSTRING_GET_BYTELEN(h)));
  39982. heap = thr->heap;
  39983. sce = NULL;
  39984. use_cache = (DUK_HSTRING_GET_CHARLEN(h) > DUK_HEAP_STRINGCACHE_NOCACHE_LIMIT);
  39985. if (use_cache) {
  39986. #ifdef DUK_USE_DDDPRINT
  39987. DUK_DDD(DUK_DDDPRINT("stringcache before char2byte (using cache):"));
  39988. for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
  39989. duk_strcache *c = heap->strcache + i;
  39990. DUK_DDD(DUK_DDDPRINT(" [%ld] -> h=%p, cidx=%ld, bidx=%ld",
  39991. (long) i, (void *) c->h, (long) c->cidx, (long) c->bidx));
  39992. }
  39993. #endif
  39994. for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
  39995. duk_strcache *c = heap->strcache + i;
  39996. if (c->h == h) {
  39997. sce = c;
  39998. break;
  39999. }
  40000. }
  40001. }
  40002. /*
  40003. * Scan from shortest distance:
  40004. * - start of string
  40005. * - end of string
  40006. * - cache entry (if exists)
  40007. */
  40008. DUK_ASSERT(DUK_HSTRING_GET_CHARLEN(h) >= char_offset);
  40009. dist_start = char_offset;
  40010. dist_end = DUK_HSTRING_GET_CHARLEN(h) - char_offset;
  40011. dist_sce = 0; DUK_UNREF(dist_sce); /* initialize for debug prints, needed if sce==NULL */
  40012. p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h);
  40013. p_end = (const duk_uint8_t *) (p_start + DUK_HSTRING_GET_BYTELEN(h));
  40014. p_found = NULL;
  40015. if (sce) {
  40016. if (char_offset >= sce->cidx) {
  40017. dist_sce = char_offset - sce->cidx;
  40018. if ((dist_sce <= dist_start) && (dist_sce <= dist_end)) {
  40019. DUK_DDD(DUK_DDDPRINT("non-ascii string, use_cache=%ld, sce=%p:%ld:%ld, "
  40020. "dist_start=%ld, dist_end=%ld, dist_sce=%ld => "
  40021. "scan forwards from sce",
  40022. (long) use_cache, (void *) (sce ? sce->h : NULL),
  40023. (sce ? (long) sce->cidx : (long) -1),
  40024. (sce ? (long) sce->bidx : (long) -1),
  40025. (long) dist_start, (long) dist_end, (long) dist_sce));
  40026. p_found = duk__scan_forwards(p_start + sce->bidx,
  40027. p_end,
  40028. dist_sce);
  40029. goto scan_done;
  40030. }
  40031. } else {
  40032. dist_sce = sce->cidx - char_offset;
  40033. if ((dist_sce <= dist_start) && (dist_sce <= dist_end)) {
  40034. DUK_DDD(DUK_DDDPRINT("non-ascii string, use_cache=%ld, sce=%p:%ld:%ld, "
  40035. "dist_start=%ld, dist_end=%ld, dist_sce=%ld => "
  40036. "scan backwards from sce",
  40037. (long) use_cache, (void *) (sce ? sce->h : NULL),
  40038. (sce ? (long) sce->cidx : (long) -1),
  40039. (sce ? (long) sce->bidx : (long) -1),
  40040. (long) dist_start, (long) dist_end, (long) dist_sce));
  40041. p_found = duk__scan_backwards(p_start + sce->bidx,
  40042. p_start,
  40043. dist_sce);
  40044. goto scan_done;
  40045. }
  40046. }
  40047. }
  40048. /* no sce, or sce scan not best */
  40049. if (dist_start <= dist_end) {
  40050. DUK_DDD(DUK_DDDPRINT("non-ascii string, use_cache=%ld, sce=%p:%ld:%ld, "
  40051. "dist_start=%ld, dist_end=%ld, dist_sce=%ld => "
  40052. "scan forwards from string start",
  40053. (long) use_cache, (void *) (sce ? sce->h : NULL),
  40054. (sce ? (long) sce->cidx : (long) -1),
  40055. (sce ? (long) sce->bidx : (long) -1),
  40056. (long) dist_start, (long) dist_end, (long) dist_sce));
  40057. p_found = duk__scan_forwards(p_start,
  40058. p_end,
  40059. dist_start);
  40060. } else {
  40061. DUK_DDD(DUK_DDDPRINT("non-ascii string, use_cache=%ld, sce=%p:%ld:%ld, "
  40062. "dist_start=%ld, dist_end=%ld, dist_sce=%ld => "
  40063. "scan backwards from string end",
  40064. (long) use_cache, (void *) (sce ? sce->h : NULL),
  40065. (sce ? (long) sce->cidx : (long) -1),
  40066. (sce ? (long) sce->bidx : (long) -1),
  40067. (long) dist_start, (long) dist_end, (long) dist_sce));
  40068. p_found = duk__scan_backwards(p_end,
  40069. p_start,
  40070. dist_end);
  40071. }
  40072. scan_done:
  40073. if (!p_found) {
  40074. /* Scan error: this shouldn't normally happen; it could happen if
  40075. * string is not valid UTF-8 data, and clen/blen are not consistent
  40076. * with the scanning algorithm.
  40077. */
  40078. goto error;
  40079. }
  40080. DUK_ASSERT(p_found >= p_start);
  40081. DUK_ASSERT(p_found <= p_end); /* may be equal */
  40082. byte_offset = (duk_uint32_t) (p_found - p_start);
  40083. DUK_DDD(DUK_DDDPRINT("-> string %p, cidx %ld -> bidx %ld",
  40084. (void *) h, (long) char_offset, (long) byte_offset));
  40085. /*
  40086. * Update cache entry (allocating if necessary), and move the
  40087. * cache entry to the first place (in an "LRU" policy).
  40088. */
  40089. if (use_cache) {
  40090. /* update entry, allocating if necessary */
  40091. if (!sce) {
  40092. sce = heap->strcache + DUK_HEAP_STRCACHE_SIZE - 1; /* take last entry */
  40093. sce->h = h;
  40094. }
  40095. DUK_ASSERT(sce != NULL);
  40096. sce->bidx = (duk_uint32_t) (p_found - p_start);
  40097. sce->cidx = (duk_uint32_t) char_offset;
  40098. /* LRU: move our entry to first */
  40099. if (sce > &heap->strcache[0]) {
  40100. /*
  40101. * A C
  40102. * B A
  40103. * C <- sce ==> B
  40104. * D D
  40105. */
  40106. duk_strcache tmp;
  40107. tmp = *sce;
  40108. DUK_MEMMOVE((void *) (&heap->strcache[1]),
  40109. (const void *) (&heap->strcache[0]),
  40110. (size_t) (((char *) sce) - ((char *) &heap->strcache[0])));
  40111. heap->strcache[0] = tmp;
  40112. /* 'sce' points to the wrong entry here, but is no longer used */
  40113. }
  40114. #ifdef DUK_USE_DDDPRINT
  40115. DUK_DDD(DUK_DDDPRINT("stringcache after char2byte (using cache):"));
  40116. for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
  40117. duk_strcache *c = heap->strcache + i;
  40118. DUK_DDD(DUK_DDDPRINT(" [%ld] -> h=%p, cidx=%ld, bidx=%ld",
  40119. (long) i, (void *) c->h, (long) c->cidx, (long) c->bidx));
  40120. }
  40121. #endif
  40122. }
  40123. return byte_offset;
  40124. error:
  40125. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, "string scan error");
  40126. return 0;
  40127. }
  40128. #line 1 "duk_heap_stringtable.c"
  40129. /*
  40130. * Heap stringtable handling, string interning.
  40131. */
  40132. /* include removed: duk_internal.h */
  40133. #if defined(DUK_USE_STRTAB_PROBE)
  40134. #define DUK__HASH_INITIAL(hash,h_size) DUK_STRTAB_HASH_INITIAL((hash),(h_size))
  40135. #define DUK__HASH_PROBE_STEP(hash) DUK_STRTAB_HASH_PROBE_STEP((hash))
  40136. #define DUK__DELETED_MARKER(heap) DUK_STRTAB_DELETED_MARKER((heap))
  40137. #endif
  40138. /*
  40139. * Create a hstring and insert into the heap. The created object
  40140. * is directly garbage collectable with reference count zero.
  40141. *
  40142. * The caller must place the interned string into the stringtable
  40143. * immediately (without chance of a longjmp); otherwise the string
  40144. * is lost.
  40145. */
  40146. DUK_LOCAL
  40147. duk_hstring *duk__alloc_init_hstring(duk_heap *heap,
  40148. const duk_uint8_t *str,
  40149. duk_uint32_t blen,
  40150. duk_uint32_t strhash,
  40151. const duk_uint8_t *extdata) {
  40152. duk_hstring *res = NULL;
  40153. duk_uint8_t *data;
  40154. duk_size_t alloc_size;
  40155. duk_uarridx_t dummy;
  40156. duk_uint32_t clen;
  40157. #if defined(DUK_USE_STRLEN16)
  40158. /* If blen <= 0xffffUL, clen is also guaranteed to be <= 0xffffUL. */
  40159. if (blen > 0xffffUL) {
  40160. DUK_D(DUK_DPRINT("16-bit string blen/clen active and blen over 16 bits, reject intern"));
  40161. return NULL;
  40162. }
  40163. #endif
  40164. if (extdata) {
  40165. alloc_size = (duk_size_t) sizeof(duk_hstring_external);
  40166. res = (duk_hstring *) DUK_ALLOC(heap, alloc_size);
  40167. if (!res) {
  40168. goto alloc_error;
  40169. }
  40170. DUK_MEMZERO(res, sizeof(duk_hstring_external));
  40171. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  40172. DUK_HEAPHDR_STRING_INIT_NULLS(&res->hdr);
  40173. #endif
  40174. DUK_HEAPHDR_SET_TYPE_AND_FLAGS(&res->hdr, DUK_HTYPE_STRING, DUK_HSTRING_FLAG_EXTDATA);
  40175. ((duk_hstring_external *) res)->extdata = extdata;
  40176. } else {
  40177. /* NUL terminate for convenient C access */
  40178. alloc_size = (duk_size_t) (sizeof(duk_hstring) + blen + 1);
  40179. res = (duk_hstring *) DUK_ALLOC(heap, alloc_size);
  40180. if (!res) {
  40181. goto alloc_error;
  40182. }
  40183. DUK_MEMZERO(res, sizeof(duk_hstring));
  40184. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  40185. DUK_HEAPHDR_STRING_INIT_NULLS(&res->hdr);
  40186. #endif
  40187. DUK_HEAPHDR_SET_TYPE_AND_FLAGS(&res->hdr, DUK_HTYPE_STRING, 0);
  40188. data = (duk_uint8_t *) (res + 1);
  40189. DUK_MEMCPY(data, str, blen);
  40190. data[blen] = (duk_uint8_t) 0;
  40191. }
  40192. if (duk_js_to_arrayindex_raw_string(str, blen, &dummy)) {
  40193. DUK_HSTRING_SET_ARRIDX(res);
  40194. }
  40195. /* All strings beginning with 0xff are treated as "internal",
  40196. * even strings interned by the user. This allows user code to
  40197. * create internal properties too, and makes behavior consistent
  40198. * in case user code happens to use a string also used by Duktape
  40199. * (such as string has already been interned and has the 'internal'
  40200. * flag set).
  40201. */
  40202. if (blen > 0 && str[0] == (duk_uint8_t) 0xff) {
  40203. DUK_HSTRING_SET_INTERNAL(res);
  40204. }
  40205. DUK_HSTRING_SET_HASH(res, strhash);
  40206. DUK_HSTRING_SET_BYTELEN(res, blen);
  40207. clen = (duk_uint32_t) duk_unicode_unvalidated_utf8_length(str, (duk_size_t) blen);
  40208. DUK_ASSERT(clen <= blen);
  40209. DUK_HSTRING_SET_CHARLEN(res, clen);
  40210. DUK_DDD(DUK_DDDPRINT("interned string, hash=0x%08lx, blen=%ld, clen=%ld, has_arridx=%ld, has_extdata=%ld",
  40211. (unsigned long) DUK_HSTRING_GET_HASH(res),
  40212. (long) DUK_HSTRING_GET_BYTELEN(res),
  40213. (long) DUK_HSTRING_GET_CHARLEN(res),
  40214. (long) DUK_HSTRING_HAS_ARRIDX(res) ? 1 : 0,
  40215. (long) DUK_HSTRING_HAS_EXTDATA(res) ? 1 : 0));
  40216. return res;
  40217. alloc_error:
  40218. DUK_FREE(heap, res);
  40219. return NULL;
  40220. }
  40221. /*
  40222. * String table algorithm: fixed size string table with array chaining
  40223. *
  40224. * The top level string table has a fixed size, with each slot holding
  40225. * either NULL, string pointer, or pointer to a separately allocated
  40226. * string pointer list.
  40227. *
  40228. * This is good for low memory environments using a pool allocator: the
  40229. * top level allocation has a fixed size and the pointer lists have quite
  40230. * small allocation size, which further matches the typical pool sizes
  40231. * needed by objects, strings, property tables, etc.
  40232. */
  40233. #if defined(DUK_USE_STRTAB_CHAIN)
  40234. #if defined(DUK_USE_HEAPPTR16)
  40235. DUK_LOCAL duk_bool_t duk__insert_hstring_chain(duk_heap *heap, duk_hstring *h) {
  40236. duk_small_uint_t slotidx;
  40237. duk_strtab_entry *e;
  40238. duk_uint16_t *lst;
  40239. duk_uint16_t *new_lst;
  40240. duk_size_t i, n;
  40241. duk_uint16_t null16 = heap->heapptr_null16;
  40242. duk_uint16_t h16 = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) h);
  40243. DUK_ASSERT(heap != NULL);
  40244. DUK_ASSERT(h != NULL);
  40245. slotidx = DUK_HSTRING_GET_HASH(h) % DUK_STRTAB_CHAIN_SIZE;
  40246. DUK_ASSERT(slotidx < DUK_STRTAB_CHAIN_SIZE);
  40247. e = heap->strtable + slotidx;
  40248. if (e->listlen == 0) {
  40249. if (e->u.str16 == null16) {
  40250. e->u.str16 = h16;
  40251. } else {
  40252. /* Now two entries in the same slot, alloc list */
  40253. lst = (duk_uint16_t *) DUK_ALLOC(heap, sizeof(duk_uint16_t) * 2);
  40254. if (lst == NULL) {
  40255. return 1; /* fail */
  40256. }
  40257. lst[0] = e->u.str16;
  40258. lst[1] = h16;
  40259. e->u.strlist16 = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) lst);
  40260. e->listlen = 2;
  40261. }
  40262. } else {
  40263. DUK_ASSERT(e->u.strlist16 != null16);
  40264. lst = (duk_uint16_t *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.strlist16);
  40265. DUK_ASSERT(lst != NULL);
  40266. for (i = 0, n = e->listlen; i < n; i++) {
  40267. if (lst[i] == null16) {
  40268. lst[i] = h16;
  40269. return 0;
  40270. }
  40271. }
  40272. if (e->listlen + 1 == 0) {
  40273. /* Overflow, relevant mainly when listlen is 16 bits. */
  40274. return 1; /* fail */
  40275. }
  40276. new_lst = (duk_uint16_t *) DUK_REALLOC(heap, lst, sizeof(duk_uint16_t) * (e->listlen + 1));
  40277. if (new_lst == NULL) {
  40278. return 1; /* fail */
  40279. }
  40280. new_lst[e->listlen++] = h16;
  40281. e->u.strlist16 = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) new_lst);
  40282. }
  40283. return 0;
  40284. }
  40285. #else /* DUK_USE_HEAPPTR16 */
  40286. DUK_LOCAL duk_bool_t duk__insert_hstring_chain(duk_heap *heap, duk_hstring *h) {
  40287. duk_small_uint_t slotidx;
  40288. duk_strtab_entry *e;
  40289. duk_hstring **lst;
  40290. duk_hstring **new_lst;
  40291. duk_size_t i, n;
  40292. DUK_ASSERT(heap != NULL);
  40293. DUK_ASSERT(h != NULL);
  40294. slotidx = DUK_HSTRING_GET_HASH(h) % DUK_STRTAB_CHAIN_SIZE;
  40295. DUK_ASSERT(slotidx < DUK_STRTAB_CHAIN_SIZE);
  40296. e = heap->strtable + slotidx;
  40297. if (e->listlen == 0) {
  40298. if (e->u.str == NULL) {
  40299. e->u.str = h;
  40300. } else {
  40301. /* Now two entries in the same slot, alloc list */
  40302. lst = (duk_hstring **) DUK_ALLOC(heap, sizeof(duk_hstring *) * 2);
  40303. if (lst == NULL) {
  40304. return 1; /* fail */
  40305. }
  40306. lst[0] = e->u.str;
  40307. lst[1] = h;
  40308. e->u.strlist = lst;
  40309. e->listlen = 2;
  40310. }
  40311. } else {
  40312. DUK_ASSERT(e->u.strlist != NULL);
  40313. lst = e->u.strlist;
  40314. for (i = 0, n = e->listlen; i < n; i++) {
  40315. if (lst[i] == NULL) {
  40316. lst[i] = h;
  40317. return 0;
  40318. }
  40319. }
  40320. if (e->listlen + 1 == 0) {
  40321. /* Overflow, relevant mainly when listlen is 16 bits. */
  40322. return 1; /* fail */
  40323. }
  40324. new_lst = (duk_hstring **) DUK_REALLOC(heap, e->u.strlist, sizeof(duk_hstring *) * (e->listlen + 1));
  40325. if (new_lst == NULL) {
  40326. return 1; /* fail */
  40327. }
  40328. new_lst[e->listlen++] = h;
  40329. e->u.strlist = new_lst;
  40330. }
  40331. return 0;
  40332. }
  40333. #endif /* DUK_USE_HEAPPTR16 */
  40334. #if defined(DUK_USE_HEAPPTR16)
  40335. DUK_LOCAL duk_hstring *duk__find_matching_string_chain(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen, duk_uint32_t strhash) {
  40336. duk_small_uint_t slotidx;
  40337. duk_strtab_entry *e;
  40338. duk_uint16_t *lst;
  40339. duk_size_t i, n;
  40340. duk_uint16_t null16 = heap->heapptr_null16;
  40341. DUK_ASSERT(heap != NULL);
  40342. slotidx = strhash % DUK_STRTAB_CHAIN_SIZE;
  40343. DUK_ASSERT(slotidx < DUK_STRTAB_CHAIN_SIZE);
  40344. e = heap->strtable + slotidx;
  40345. if (e->listlen == 0) {
  40346. if (e->u.str16 != null16) {
  40347. duk_hstring *h = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.str16);
  40348. DUK_ASSERT(h != NULL);
  40349. if (DUK_HSTRING_GET_BYTELEN(h) == blen &&
  40350. DUK_MEMCMP((const void *) str, (const void *) DUK_HSTRING_GET_DATA(h), (size_t) blen) == 0) {
  40351. return h;
  40352. }
  40353. }
  40354. } else {
  40355. DUK_ASSERT(e->u.strlist16 != null16);
  40356. lst = (duk_uint16_t *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.strlist16);
  40357. DUK_ASSERT(lst != NULL);
  40358. for (i = 0, n = e->listlen; i < n; i++) {
  40359. if (lst[i] != null16) {
  40360. duk_hstring *h = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, lst[i]);
  40361. DUK_ASSERT(h != NULL);
  40362. if (DUK_HSTRING_GET_BYTELEN(h) == blen &&
  40363. DUK_MEMCMP((const void *) str, (const void *) DUK_HSTRING_GET_DATA(h), (size_t) blen) == 0) {
  40364. return h;
  40365. }
  40366. }
  40367. }
  40368. }
  40369. return NULL;
  40370. }
  40371. #else /* DUK_USE_HEAPPTR16 */
  40372. DUK_LOCAL duk_hstring *duk__find_matching_string_chain(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen, duk_uint32_t strhash) {
  40373. duk_small_uint_t slotidx;
  40374. duk_strtab_entry *e;
  40375. duk_hstring **lst;
  40376. duk_size_t i, n;
  40377. DUK_ASSERT(heap != NULL);
  40378. slotidx = strhash % DUK_STRTAB_CHAIN_SIZE;
  40379. DUK_ASSERT(slotidx < DUK_STRTAB_CHAIN_SIZE);
  40380. e = heap->strtable + slotidx;
  40381. if (e->listlen == 0) {
  40382. if (e->u.str != NULL &&
  40383. DUK_HSTRING_GET_BYTELEN(e->u.str) == blen &&
  40384. DUK_MEMCMP((const void *) str, (const void *) DUK_HSTRING_GET_DATA(e->u.str), (size_t) blen) == 0) {
  40385. return e->u.str;
  40386. }
  40387. } else {
  40388. DUK_ASSERT(e->u.strlist != NULL);
  40389. lst = e->u.strlist;
  40390. for (i = 0, n = e->listlen; i < n; i++) {
  40391. if (lst[i] != NULL &&
  40392. DUK_HSTRING_GET_BYTELEN(lst[i]) == blen &&
  40393. DUK_MEMCMP((const void *) str, (const void *) DUK_HSTRING_GET_DATA(lst[i]), (size_t) blen) == 0) {
  40394. return lst[i];
  40395. }
  40396. }
  40397. }
  40398. return NULL;
  40399. }
  40400. #endif /* DUK_USE_HEAPPTR16 */
  40401. #if defined(DUK_USE_HEAPPTR16)
  40402. DUK_LOCAL void duk__remove_matching_hstring_chain(duk_heap *heap, duk_hstring *h) {
  40403. duk_small_uint_t slotidx;
  40404. duk_strtab_entry *e;
  40405. duk_uint16_t *lst;
  40406. duk_size_t i, n;
  40407. duk_uint16_t h16;
  40408. duk_uint16_t null16 = heap->heapptr_null16;
  40409. DUK_ASSERT(heap != NULL);
  40410. DUK_ASSERT(h != NULL);
  40411. slotidx = DUK_HSTRING_GET_HASH(h) % DUK_STRTAB_CHAIN_SIZE;
  40412. DUK_ASSERT(slotidx < DUK_STRTAB_CHAIN_SIZE);
  40413. DUK_ASSERT(h != NULL);
  40414. h16 = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) h);
  40415. e = heap->strtable + slotidx;
  40416. if (e->listlen == 0) {
  40417. if (e->u.str16 == h16) {
  40418. e->u.str16 = null16;
  40419. return;
  40420. }
  40421. } else {
  40422. DUK_ASSERT(e->u.strlist16 != null16);
  40423. lst = (duk_uint16_t *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.strlist16);
  40424. DUK_ASSERT(lst != NULL);
  40425. for (i = 0, n = e->listlen; i < n; i++) {
  40426. if (lst[i] == h16) {
  40427. lst[i] = null16;
  40428. return;
  40429. }
  40430. }
  40431. }
  40432. DUK_D(DUK_DPRINT("failed to find string that should be in stringtable"));
  40433. DUK_UNREACHABLE();
  40434. return;
  40435. }
  40436. #else /* DUK_USE_HEAPPTR16 */
  40437. DUK_LOCAL void duk__remove_matching_hstring_chain(duk_heap *heap, duk_hstring *h) {
  40438. duk_small_uint_t slotidx;
  40439. duk_strtab_entry *e;
  40440. duk_hstring **lst;
  40441. duk_size_t i, n;
  40442. DUK_ASSERT(heap != NULL);
  40443. DUK_ASSERT(h != NULL);
  40444. slotidx = DUK_HSTRING_GET_HASH(h) % DUK_STRTAB_CHAIN_SIZE;
  40445. DUK_ASSERT(slotidx < DUK_STRTAB_CHAIN_SIZE);
  40446. e = heap->strtable + slotidx;
  40447. if (e->listlen == 0) {
  40448. DUK_ASSERT(h != NULL);
  40449. if (e->u.str == h) {
  40450. e->u.str = NULL;
  40451. return;
  40452. }
  40453. } else {
  40454. DUK_ASSERT(e->u.strlist != NULL);
  40455. lst = e->u.strlist;
  40456. for (i = 0, n = e->listlen; i < n; i++) {
  40457. DUK_ASSERT(h != NULL);
  40458. if (lst[i] == h) {
  40459. lst[i] = NULL;
  40460. return;
  40461. }
  40462. }
  40463. }
  40464. DUK_D(DUK_DPRINT("failed to find string that should be in stringtable"));
  40465. DUK_UNREACHABLE();
  40466. return;
  40467. }
  40468. #endif /* DUK_USE_HEAPPTR16 */
  40469. #if defined(DUK_USE_DEBUG)
  40470. DUK_INTERNAL void duk_heap_dump_strtab(duk_heap *heap) {
  40471. duk_strtab_entry *e;
  40472. duk_small_uint_t i;
  40473. duk_size_t j, n, used;
  40474. #if defined(DUK_USE_HEAPPTR16)
  40475. duk_uint16_t *lst;
  40476. duk_uint16_t null16 = heap->heapptr_null16;
  40477. #else
  40478. duk_hstring **lst;
  40479. #endif
  40480. DUK_ASSERT(heap != NULL);
  40481. for (i = 0; i < DUK_STRTAB_CHAIN_SIZE; i++) {
  40482. e = heap->strtable + i;
  40483. if (e->listlen == 0) {
  40484. #if defined(DUK_USE_HEAPPTR16)
  40485. DUK_DD(DUK_DDPRINT("[%03d] -> plain %d", (int) i, (int) (e->u.str16 != null16 ? 1 : 0)));
  40486. #else
  40487. DUK_DD(DUK_DDPRINT("[%03d] -> plain %d", (int) i, (int) (e->u.str ? 1 : 0)));
  40488. #endif
  40489. } else {
  40490. used = 0;
  40491. #if defined(DUK_USE_HEAPPTR16)
  40492. lst = (duk_uint16_t *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.strlist16);
  40493. #else
  40494. lst = e->u.strlist;
  40495. #endif
  40496. DUK_ASSERT(lst != NULL);
  40497. for (j = 0, n = e->listlen; j < n; j++) {
  40498. #if defined(DUK_USE_HEAPPTR16)
  40499. if (lst[j] != null16) {
  40500. #else
  40501. if (lst[j] != NULL) {
  40502. #endif
  40503. used++;
  40504. }
  40505. }
  40506. DUK_DD(DUK_DDPRINT("[%03d] -> array %d/%d", (int) i, (int) used, (int) e->listlen));
  40507. }
  40508. }
  40509. }
  40510. #endif /* DUK_USE_DEBUG */
  40511. #endif /* DUK_USE_STRTAB_CHAIN */
  40512. /*
  40513. * String table algorithm: closed hashing with a probe sequence
  40514. *
  40515. * This is the default algorithm and works fine for environments with
  40516. * minimal memory constraints.
  40517. */
  40518. #if defined(DUK_USE_STRTAB_PROBE)
  40519. /* Count actually used (non-NULL, non-DELETED) entries. */
  40520. DUK_LOCAL duk_int_t duk__count_used_probe(duk_heap *heap) {
  40521. duk_int_t res = 0;
  40522. duk_uint_fast32_t i, n;
  40523. #if defined(DUK_USE_HEAPPTR16)
  40524. duk_uint16_t null16 = heap->heapptr_null16;
  40525. duk_uint16_t deleted16 = heap->heapptr_deleted16;
  40526. #endif
  40527. n = (duk_uint_fast32_t) heap->st_size;
  40528. for (i = 0; i < n; i++) {
  40529. #if defined(DUK_USE_HEAPPTR16)
  40530. if (heap->strtable16[i] != null16 && heap->strtable16[i] != deleted16) {
  40531. #else
  40532. if (heap->strtable[i] != NULL && heap->strtable[i] != DUK__DELETED_MARKER(heap)) {
  40533. #endif
  40534. res++;
  40535. }
  40536. }
  40537. return res;
  40538. }
  40539. #if defined(DUK_USE_HEAPPTR16)
  40540. DUK_LOCAL void duk__insert_hstring_probe(duk_heap *heap, duk_uint16_t *entries16, duk_uint32_t size, duk_uint32_t *p_used, duk_hstring *h) {
  40541. #else
  40542. DUK_LOCAL void duk__insert_hstring_probe(duk_heap *heap, duk_hstring **entries, duk_uint32_t size, duk_uint32_t *p_used, duk_hstring *h) {
  40543. #endif
  40544. duk_uint32_t i;
  40545. duk_uint32_t step;
  40546. #if defined(DUK_USE_HEAPPTR16)
  40547. duk_uint16_t null16 = heap->heapptr_null16;
  40548. duk_uint16_t deleted16 = heap->heapptr_deleted16;
  40549. #endif
  40550. DUK_ASSERT(size > 0);
  40551. i = DUK__HASH_INITIAL(DUK_HSTRING_GET_HASH(h), size);
  40552. step = DUK__HASH_PROBE_STEP(DUK_HSTRING_GET_HASH(h));
  40553. for (;;) {
  40554. #if defined(DUK_USE_HEAPPTR16)
  40555. duk_uint16_t e16 = entries16[i];
  40556. #else
  40557. duk_hstring *e = entries[i];
  40558. #endif
  40559. #if defined(DUK_USE_HEAPPTR16)
  40560. /* XXX: could check for e16 == 0 because NULL is guaranteed to
  40561. * encode to zero.
  40562. */
  40563. if (e16 == null16) {
  40564. #else
  40565. if (e == NULL) {
  40566. #endif
  40567. DUK_DDD(DUK_DDDPRINT("insert hit (null): %ld", (long) i));
  40568. #if defined(DUK_USE_HEAPPTR16)
  40569. entries16[i] = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) h);
  40570. #else
  40571. entries[i] = h;
  40572. #endif
  40573. (*p_used)++;
  40574. break;
  40575. #if defined(DUK_USE_HEAPPTR16)
  40576. } else if (e16 == deleted16) {
  40577. #else
  40578. } else if (e == DUK__DELETED_MARKER(heap)) {
  40579. #endif
  40580. /* st_used remains the same, DELETED is counted as used */
  40581. DUK_DDD(DUK_DDDPRINT("insert hit (deleted): %ld", (long) i));
  40582. #if defined(DUK_USE_HEAPPTR16)
  40583. entries16[i] = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) h);
  40584. #else
  40585. entries[i] = h;
  40586. #endif
  40587. break;
  40588. }
  40589. DUK_DDD(DUK_DDDPRINT("insert miss: %ld", (long) i));
  40590. i = (i + step) % size;
  40591. /* looping should never happen */
  40592. DUK_ASSERT(i != DUK__HASH_INITIAL(DUK_HSTRING_GET_HASH(h), size));
  40593. }
  40594. }
  40595. #if defined(DUK_USE_HEAPPTR16)
  40596. DUK_LOCAL duk_hstring *duk__find_matching_string_probe(duk_heap *heap, duk_uint16_t *entries16, duk_uint32_t size, const duk_uint8_t *str, duk_uint32_t blen, duk_uint32_t strhash) {
  40597. #else
  40598. DUK_LOCAL duk_hstring *duk__find_matching_string_probe(duk_heap *heap, duk_hstring **entries, duk_uint32_t size, const duk_uint8_t *str, duk_uint32_t blen, duk_uint32_t strhash) {
  40599. #endif
  40600. duk_uint32_t i;
  40601. duk_uint32_t step;
  40602. DUK_ASSERT(size > 0);
  40603. i = DUK__HASH_INITIAL(strhash, size);
  40604. step = DUK__HASH_PROBE_STEP(strhash);
  40605. for (;;) {
  40606. duk_hstring *e;
  40607. #if defined(DUK_USE_HEAPPTR16)
  40608. e = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, entries16[i]);
  40609. #else
  40610. e = entries[i];
  40611. #endif
  40612. if (!e) {
  40613. return NULL;
  40614. }
  40615. if (e != DUK__DELETED_MARKER(heap) && DUK_HSTRING_GET_BYTELEN(e) == blen) {
  40616. if (DUK_MEMCMP((const void *) str, (const void *) DUK_HSTRING_GET_DATA(e), (size_t) blen) == 0) {
  40617. DUK_DDD(DUK_DDDPRINT("find matching hit: %ld (step %ld, size %ld)",
  40618. (long) i, (long) step, (long) size));
  40619. return e;
  40620. }
  40621. }
  40622. DUK_DDD(DUK_DDDPRINT("find matching miss: %ld (step %ld, size %ld)",
  40623. (long) i, (long) step, (long) size));
  40624. i = (i + step) % size;
  40625. /* looping should never happen */
  40626. DUK_ASSERT(i != DUK__HASH_INITIAL(strhash, size));
  40627. }
  40628. DUK_UNREACHABLE();
  40629. }
  40630. #if defined(DUK_USE_HEAPPTR16)
  40631. DUK_LOCAL void duk__remove_matching_hstring_probe(duk_heap *heap, duk_uint16_t *entries16, duk_uint32_t size, duk_hstring *h) {
  40632. #else
  40633. DUK_LOCAL void duk__remove_matching_hstring_probe(duk_heap *heap, duk_hstring **entries, duk_uint32_t size, duk_hstring *h) {
  40634. #endif
  40635. duk_uint32_t i;
  40636. duk_uint32_t step;
  40637. duk_uint32_t hash;
  40638. #if defined(DUK_USE_HEAPPTR16)
  40639. duk_uint16_t null16 = heap->heapptr_null16;
  40640. duk_uint16_t h16 = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) h);
  40641. #endif
  40642. DUK_ASSERT(size > 0);
  40643. hash = DUK_HSTRING_GET_HASH(h);
  40644. i = DUK__HASH_INITIAL(hash, size);
  40645. step = DUK__HASH_PROBE_STEP(hash);
  40646. for (;;) {
  40647. #if defined(DUK_USE_HEAPPTR16)
  40648. duk_uint16_t e16 = entries16[i];
  40649. #else
  40650. duk_hstring *e = entries[i];
  40651. #endif
  40652. #if defined(DUK_USE_HEAPPTR16)
  40653. if (e16 == null16) {
  40654. #else
  40655. if (!e) {
  40656. #endif
  40657. DUK_UNREACHABLE();
  40658. break;
  40659. }
  40660. #if defined(DUK_USE_HEAPPTR16)
  40661. if (e16 == h16) {
  40662. #else
  40663. if (e == h) {
  40664. #endif
  40665. /* st_used remains the same, DELETED is counted as used */
  40666. DUK_DDD(DUK_DDDPRINT("free matching hit: %ld", (long) i));
  40667. #if defined(DUK_USE_HEAPPTR16)
  40668. entries16[i] = heap->heapptr_deleted16;
  40669. #else
  40670. entries[i] = DUK__DELETED_MARKER(heap);
  40671. #endif
  40672. break;
  40673. }
  40674. DUK_DDD(DUK_DDDPRINT("free matching miss: %ld", (long) i));
  40675. i = (i + step) % size;
  40676. /* looping should never happen */
  40677. DUK_ASSERT(i != DUK__HASH_INITIAL(hash, size));
  40678. }
  40679. }
  40680. DUK_LOCAL duk_bool_t duk__resize_strtab_raw_probe(duk_heap *heap, duk_uint32_t new_size) {
  40681. #ifdef DUK_USE_MARK_AND_SWEEP
  40682. duk_small_uint_t prev_mark_and_sweep_base_flags;
  40683. #endif
  40684. #ifdef DUK_USE_DEBUG
  40685. duk_uint32_t old_used = heap->st_used;
  40686. #endif
  40687. duk_uint32_t old_size = heap->st_size;
  40688. #if defined(DUK_USE_HEAPPTR16)
  40689. duk_uint16_t *old_entries = heap->strtable16;
  40690. duk_uint16_t *new_entries = NULL;
  40691. #else
  40692. duk_hstring **old_entries = heap->strtable;
  40693. duk_hstring **new_entries = NULL;
  40694. #endif
  40695. duk_uint32_t new_used = 0;
  40696. duk_uint32_t i;
  40697. #ifdef DUK_USE_DEBUG
  40698. DUK_UNREF(old_used); /* unused with some debug level combinations */
  40699. #endif
  40700. #ifdef DUK_USE_DDDPRINT
  40701. DUK_DDD(DUK_DDDPRINT("attempt to resize stringtable: %ld entries, %ld bytes, %ld used, %ld%% load -> %ld entries, %ld bytes, %ld used, %ld%% load",
  40702. (long) old_size, (long) (sizeof(duk_hstring *) * old_size), (long) old_used,
  40703. (long) (((double) old_used) / ((double) old_size) * 100.0),
  40704. (long) new_size, (long) (sizeof(duk_hstring *) * new_size), (long) duk__count_used_probe(heap),
  40705. (long) (((double) duk__count_used_probe(heap)) / ((double) new_size) * 100.0)));
  40706. #endif
  40707. DUK_ASSERT(new_size > (duk_uint32_t) duk__count_used_probe(heap)); /* required for rehash to succeed, equality not that useful */
  40708. DUK_ASSERT(old_entries);
  40709. #ifdef DUK_USE_MARK_AND_SWEEP
  40710. DUK_ASSERT((heap->mark_and_sweep_base_flags & DUK_MS_FLAG_NO_STRINGTABLE_RESIZE) == 0);
  40711. #endif
  40712. /*
  40713. * The attempt to allocate may cause a GC. Such a GC must not attempt to resize
  40714. * the stringtable (though it can be swept); finalizer execution and object
  40715. * compaction must also be postponed to avoid the pressure to add strings to the
  40716. * string table.
  40717. */
  40718. #ifdef DUK_USE_MARK_AND_SWEEP
  40719. prev_mark_and_sweep_base_flags = heap->mark_and_sweep_base_flags;
  40720. heap->mark_and_sweep_base_flags |= \
  40721. DUK_MS_FLAG_NO_STRINGTABLE_RESIZE | /* avoid recursive call here */
  40722. DUK_MS_FLAG_NO_FINALIZERS | /* avoid pressure to add/remove strings */
  40723. DUK_MS_FLAG_NO_OBJECT_COMPACTION; /* avoid array abandoning which interns strings */
  40724. #endif
  40725. #if defined(DUK_USE_HEAPPTR16)
  40726. new_entries = (duk_uint16_t *) DUK_ALLOC(heap, sizeof(duk_uint16_t) * new_size);
  40727. #else
  40728. new_entries = (duk_hstring **) DUK_ALLOC(heap, sizeof(duk_hstring *) * new_size);
  40729. #endif
  40730. #ifdef DUK_USE_MARK_AND_SWEEP
  40731. heap->mark_and_sweep_base_flags = prev_mark_and_sweep_base_flags;
  40732. #endif
  40733. if (!new_entries) {
  40734. goto resize_error;
  40735. }
  40736. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  40737. for (i = 0; i < new_size; i++) {
  40738. #if defined(DUK_USE_HEAPPTR16)
  40739. new_entries[i] = heap->heapptr_null16;
  40740. #else
  40741. new_entries[i] = NULL;
  40742. #endif
  40743. }
  40744. #else
  40745. #if defined(DUK_USE_HEAPPTR16)
  40746. /* Relies on NULL encoding to zero. */
  40747. DUK_MEMZERO(new_entries, sizeof(duk_uint16_t) * new_size);
  40748. #else
  40749. DUK_MEMZERO(new_entries, sizeof(duk_hstring *) * new_size);
  40750. #endif
  40751. #endif
  40752. /* Because new_size > duk__count_used_probe(heap), guaranteed to work */
  40753. for (i = 0; i < old_size; i++) {
  40754. duk_hstring *e;
  40755. #if defined(DUK_USE_HEAPPTR16)
  40756. e = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, old_entries[i]);
  40757. #else
  40758. e = old_entries[i];
  40759. #endif
  40760. if (e == NULL || e == DUK__DELETED_MARKER(heap)) {
  40761. continue;
  40762. }
  40763. /* checking for DUK__DELETED_MARKER is not necessary here, but helper does it now */
  40764. duk__insert_hstring_probe(heap, new_entries, new_size, &new_used, e);
  40765. }
  40766. #ifdef DUK_USE_DDPRINT
  40767. DUK_DD(DUK_DDPRINT("resized stringtable: %ld entries, %ld bytes, %ld used, %ld%% load -> %ld entries, %ld bytes, %ld used, %ld%% load",
  40768. (long) old_size, (long) (sizeof(duk_hstring *) * old_size), (long) old_used,
  40769. (long) (((double) old_used) / ((double) old_size) * 100.0),
  40770. (long) new_size, (long) (sizeof(duk_hstring *) * new_size), (long) new_used,
  40771. (long) (((double) new_used) / ((double) new_size) * 100.0)));
  40772. #endif
  40773. #if defined(DUK_USE_HEAPPTR16)
  40774. DUK_FREE(heap, heap->strtable16);
  40775. heap->strtable16 = new_entries;
  40776. #else
  40777. DUK_FREE(heap, heap->strtable);
  40778. heap->strtable = new_entries;
  40779. #endif
  40780. heap->st_size = new_size;
  40781. heap->st_used = new_used; /* may be less, since DELETED entries are NULLed by rehash */
  40782. return 0; /* OK */
  40783. resize_error:
  40784. DUK_FREE(heap, new_entries);
  40785. return 1; /* FAIL */
  40786. }
  40787. DUK_LOCAL duk_bool_t duk__resize_strtab_probe(duk_heap *heap) {
  40788. duk_uint32_t new_size;
  40789. duk_bool_t ret;
  40790. new_size = (duk_uint32_t) duk__count_used_probe(heap);
  40791. if (new_size >= 0x80000000UL) {
  40792. new_size = DUK_STRTAB_HIGHEST_32BIT_PRIME;
  40793. } else {
  40794. new_size = duk_util_get_hash_prime(DUK_STRTAB_GROW_ST_SIZE(new_size));
  40795. new_size = duk_util_get_hash_prime(new_size);
  40796. }
  40797. DUK_ASSERT(new_size > 0);
  40798. /* rehash even if old and new sizes are the same to get rid of
  40799. * DELETED entries.
  40800. */
  40801. ret = duk__resize_strtab_raw_probe(heap, new_size);
  40802. return ret;
  40803. }
  40804. DUK_LOCAL duk_bool_t duk__recheck_strtab_size_probe(duk_heap *heap, duk_uint32_t new_used) {
  40805. duk_uint32_t new_free;
  40806. duk_uint32_t tmp1;
  40807. duk_uint32_t tmp2;
  40808. DUK_ASSERT(new_used <= heap->st_size); /* grow by at most one */
  40809. new_free = heap->st_size - new_used; /* unsigned intentionally */
  40810. /* new_free / size <= 1 / DIV <=> new_free <= size / DIV */
  40811. /* new_used / size <= 1 / DIV <=> new_used <= size / DIV */
  40812. tmp1 = heap->st_size / DUK_STRTAB_MIN_FREE_DIVISOR;
  40813. tmp2 = heap->st_size / DUK_STRTAB_MIN_USED_DIVISOR;
  40814. if (new_free <= tmp1 || new_used <= tmp2) {
  40815. /* load factor too low or high, count actually used entries and resize */
  40816. return duk__resize_strtab_probe(heap);
  40817. } else {
  40818. return 0; /* OK */
  40819. }
  40820. }
  40821. #if defined(DUK_USE_DEBUG)
  40822. DUK_INTERNAL void duk_heap_dump_strtab(duk_heap *heap) {
  40823. duk_uint32_t i;
  40824. duk_hstring *h;
  40825. DUK_ASSERT(heap != NULL);
  40826. #if defined(DUK_USE_HEAPPTR16)
  40827. DUK_ASSERT(heap->strtable16 != NULL);
  40828. #else
  40829. DUK_ASSERT(heap->strtable != NULL);
  40830. #endif
  40831. DUK_UNREF(h);
  40832. for (i = 0; i < heap->st_size; i++) {
  40833. #if defined(DUK_USE_HEAPPTR16)
  40834. h = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->strtable16[i]);
  40835. #else
  40836. h = heap->strtable[i];
  40837. #endif
  40838. DUK_DD(DUK_DDPRINT("[%03d] -> %p", (int) i, (void *) h));
  40839. }
  40840. }
  40841. #endif /* DUK_USE_DEBUG */
  40842. #endif /* DUK_USE_STRTAB_PROBE */
  40843. /*
  40844. * Raw intern and lookup
  40845. */
  40846. DUK_LOCAL duk_hstring *duk__do_intern(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen, duk_uint32_t strhash) {
  40847. duk_hstring *res;
  40848. const duk_uint8_t *extdata;
  40849. #if defined(DUK_USE_STRTAB_PROBE)
  40850. if (duk__recheck_strtab_size_probe(heap, heap->st_used + 1)) {
  40851. return NULL;
  40852. }
  40853. #endif
  40854. /* For manual testing only. */
  40855. #if 0
  40856. {
  40857. duk_size_t i;
  40858. DUK_PRINTF("INTERN: \"");
  40859. for (i = 0; i < blen; i++) {
  40860. duk_uint8_t x = str[i];
  40861. if (x >= 0x20 && x <= 0x7e && x != '"' && x != '\\') {
  40862. DUK_PRINTF("%c", (int) x); /* char: use int cast */
  40863. } else {
  40864. DUK_PRINTF("\\x%02lx", (long) x);
  40865. }
  40866. }
  40867. DUK_PRINTF("\"\n");
  40868. }
  40869. #endif
  40870. #if defined(DUK_USE_HSTRING_EXTDATA) && defined(DUK_USE_EXTSTR_INTERN_CHECK)
  40871. extdata = (const duk_uint8_t *) DUK_USE_EXTSTR_INTERN_CHECK(heap->heap_udata, (void *) str, (duk_size_t) blen);
  40872. #else
  40873. extdata = (const duk_uint8_t *) NULL;
  40874. #endif
  40875. res = duk__alloc_init_hstring(heap, str, blen, strhash, extdata);
  40876. if (!res) {
  40877. return NULL;
  40878. }
  40879. #if defined(DUK_USE_STRTAB_CHAIN)
  40880. if (duk__insert_hstring_chain(heap, res)) {
  40881. /* failed */
  40882. DUK_FREE(heap, res);
  40883. return NULL;
  40884. }
  40885. #elif defined(DUK_USE_STRTAB_PROBE)
  40886. /* guaranteed to succeed */
  40887. duk__insert_hstring_probe(heap,
  40888. #if defined(DUK_USE_HEAPPTR16)
  40889. heap->strtable16,
  40890. #else
  40891. heap->strtable,
  40892. #endif
  40893. heap->st_size,
  40894. &heap->st_used,
  40895. res);
  40896. #else
  40897. #error internal error, invalid strtab options
  40898. #endif
  40899. /* Note: hstring is in heap but has refcount zero and is not strongly reachable.
  40900. * Caller should increase refcount and make the hstring reachable before any
  40901. * operations which require allocation (and possible gc).
  40902. */
  40903. return res;
  40904. }
  40905. DUK_LOCAL duk_hstring *duk__do_lookup(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen, duk_uint32_t *out_strhash) {
  40906. duk_hstring *res;
  40907. DUK_ASSERT(out_strhash);
  40908. *out_strhash = duk_heap_hashstring(heap, str, (duk_size_t) blen);
  40909. #if defined(DUK_USE_STRTAB_CHAIN)
  40910. res = duk__find_matching_string_chain(heap, str, blen, *out_strhash);
  40911. #elif defined(DUK_USE_STRTAB_PROBE)
  40912. res = duk__find_matching_string_probe(heap,
  40913. #if defined(DUK_USE_HEAPPTR16)
  40914. heap->strtable16,
  40915. #else
  40916. heap->strtable,
  40917. #endif
  40918. heap->st_size,
  40919. str,
  40920. blen,
  40921. *out_strhash);
  40922. #else
  40923. #error internal error, invalid strtab options
  40924. #endif
  40925. return res;
  40926. }
  40927. /*
  40928. * Exposed calls
  40929. */
  40930. #if 0 /*unused*/
  40931. DUK_INTERNAL duk_hstring *duk_heap_string_lookup(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen) {
  40932. duk_uint32_t strhash; /* dummy */
  40933. return duk__do_lookup(heap, str, blen, &strhash);
  40934. }
  40935. #endif
  40936. DUK_INTERNAL duk_hstring *duk_heap_string_intern(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen) {
  40937. duk_hstring *res;
  40938. duk_uint32_t strhash;
  40939. /* caller is responsible for ensuring this */
  40940. DUK_ASSERT(blen <= DUK_HSTRING_MAX_BYTELEN);
  40941. res = duk__do_lookup(heap, str, blen, &strhash);
  40942. if (res) {
  40943. return res;
  40944. }
  40945. res = duk__do_intern(heap, str, blen, strhash);
  40946. return res; /* may be NULL */
  40947. }
  40948. DUK_INTERNAL duk_hstring *duk_heap_string_intern_checked(duk_hthread *thr, const duk_uint8_t *str, duk_uint32_t blen) {
  40949. duk_hstring *res = duk_heap_string_intern(thr->heap, str, blen);
  40950. if (!res) {
  40951. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, "failed to intern string");
  40952. }
  40953. return res;
  40954. }
  40955. #if 0 /*unused*/
  40956. DUK_INTERNAL duk_hstring *duk_heap_string_lookup_u32(duk_heap *heap, duk_uint32_t val) {
  40957. char buf[DUK_STRTAB_U32_MAX_STRLEN+1];
  40958. DUK_SNPRINTF(buf, sizeof(buf), "%lu", (unsigned long) val);
  40959. buf[sizeof(buf) - 1] = (char) 0;
  40960. DUK_ASSERT(DUK_STRLEN(buf) <= DUK_UINT32_MAX); /* formatted result limited */
  40961. return duk_heap_string_lookup(heap, (const duk_uint8_t *) buf, (duk_uint32_t) DUK_STRLEN(buf));
  40962. }
  40963. #endif
  40964. DUK_INTERNAL duk_hstring *duk_heap_string_intern_u32(duk_heap *heap, duk_uint32_t val) {
  40965. char buf[DUK_STRTAB_U32_MAX_STRLEN+1];
  40966. DUK_SNPRINTF(buf, sizeof(buf), "%lu", (unsigned long) val);
  40967. buf[sizeof(buf) - 1] = (char) 0;
  40968. DUK_ASSERT(DUK_STRLEN(buf) <= DUK_UINT32_MAX); /* formatted result limited */
  40969. return duk_heap_string_intern(heap, (const duk_uint8_t *) buf, (duk_uint32_t) DUK_STRLEN(buf));
  40970. }
  40971. DUK_INTERNAL duk_hstring *duk_heap_string_intern_u32_checked(duk_hthread *thr, duk_uint32_t val) {
  40972. duk_hstring *res = duk_heap_string_intern_u32(thr->heap, val);
  40973. if (!res) {
  40974. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, "failed to intern string");
  40975. }
  40976. return res;
  40977. }
  40978. /* find and remove string from stringtable; caller must free the string itself */
  40979. #if defined(DUK_USE_REFERENCE_COUNTING)
  40980. DUK_INTERNAL void duk_heap_string_remove(duk_heap *heap, duk_hstring *h) {
  40981. DUK_DDD(DUK_DDDPRINT("remove string from stringtable: %!O", (duk_heaphdr *) h));
  40982. #if defined(DUK_USE_STRTAB_CHAIN)
  40983. duk__remove_matching_hstring_chain(heap, h);
  40984. #elif defined(DUK_USE_STRTAB_PROBE)
  40985. duk__remove_matching_hstring_probe(heap,
  40986. #if defined(DUK_USE_HEAPPTR16)
  40987. heap->strtable16,
  40988. #else
  40989. heap->strtable,
  40990. #endif
  40991. heap->st_size,
  40992. h);
  40993. #else
  40994. #error internal error, invalid strtab options
  40995. #endif
  40996. }
  40997. #endif
  40998. #if defined(DUK_USE_MARK_AND_SWEEP) && defined(DUK_USE_MS_STRINGTABLE_RESIZE)
  40999. DUK_INTERNAL void duk_heap_force_strtab_resize(duk_heap *heap) {
  41000. /* Force a resize so that DELETED entries are eliminated.
  41001. * Another option would be duk__recheck_strtab_size_probe();
  41002. * but since that happens on every intern anyway, this whole
  41003. * check can now be disabled.
  41004. */
  41005. #if defined(DUK_USE_STRTAB_CHAIN)
  41006. DUK_UNREF(heap);
  41007. #elif defined(DUK_USE_STRTAB_PROBE)
  41008. duk__resize_strtab_probe(heap);
  41009. #endif
  41010. }
  41011. #endif
  41012. #if defined(DUK_USE_STRTAB_CHAIN)
  41013. DUK_INTERNAL void duk_heap_free_strtab(duk_heap *heap) {
  41014. /* Free strings in the stringtable and any allocations needed
  41015. * by the stringtable itself.
  41016. */
  41017. duk_uint_fast32_t i, j;
  41018. duk_strtab_entry *e;
  41019. #if defined(DUK_USE_HEAPPTR16)
  41020. duk_uint16_t *lst;
  41021. duk_uint16_t null16 = heap->heapptr_null16;
  41022. #else
  41023. duk_hstring **lst;
  41024. #endif
  41025. duk_hstring *h;
  41026. for (i = 0; i < DUK_STRTAB_CHAIN_SIZE; i++) {
  41027. e = heap->strtable + i;
  41028. if (e->listlen > 0) {
  41029. #if defined(DUK_USE_HEAPPTR16)
  41030. lst = (duk_uint16_t *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.strlist16);
  41031. #else
  41032. lst = e->u.strlist;
  41033. #endif
  41034. DUK_ASSERT(lst != NULL);
  41035. for (j = 0; j < e->listlen; j++) {
  41036. #if defined(DUK_USE_HEAPPTR16)
  41037. h = DUK_USE_HEAPPTR_DEC16(heap->heap_udata, lst[j]);
  41038. lst[j] = null16;
  41039. #else
  41040. h = lst[j];
  41041. lst[j] = NULL;
  41042. #endif
  41043. /* strings may have inner refs (extdata) in some cases */
  41044. if (h != NULL) {
  41045. duk_free_hstring_inner(heap, h);
  41046. DUK_FREE(heap, h);
  41047. }
  41048. }
  41049. #if defined(DUK_USE_HEAPPTR16)
  41050. e->u.strlist16 = null16;
  41051. #else
  41052. e->u.strlist = NULL;
  41053. #endif
  41054. DUK_FREE(heap, lst);
  41055. } else {
  41056. #if defined(DUK_USE_HEAPPTR16)
  41057. h = DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.str16);
  41058. e->u.str16 = null16;
  41059. #else
  41060. h = e->u.str;
  41061. e->u.str = NULL;
  41062. #endif
  41063. if (h != NULL) {
  41064. duk_free_hstring_inner(heap, h);
  41065. DUK_FREE(heap, h);
  41066. }
  41067. }
  41068. e->listlen = 0;
  41069. }
  41070. }
  41071. #endif /* DUK_USE_STRTAB_CHAIN */
  41072. #if defined(DUK_USE_STRTAB_PROBE)
  41073. DUK_INTERNAL void duk_heap_free_strtab(duk_heap *heap) {
  41074. duk_uint_fast32_t i;
  41075. duk_hstring *h;
  41076. #if defined(DUK_USE_HEAPPTR16)
  41077. if (heap->strtable16) {
  41078. #else
  41079. if (heap->strtable) {
  41080. #endif
  41081. for (i = 0; i < (duk_uint_fast32_t) heap->st_size; i++) {
  41082. #if defined(DUK_USE_HEAPPTR16)
  41083. h = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, heap->strtable16[i]);
  41084. #else
  41085. h = heap->strtable[i];
  41086. #endif
  41087. if (h == NULL || h == DUK_STRTAB_DELETED_MARKER(heap)) {
  41088. continue;
  41089. }
  41090. DUK_ASSERT(h != NULL);
  41091. /* strings may have inner refs (extdata) in some cases */
  41092. duk_free_hstring_inner(heap, h);
  41093. DUK_FREE(heap, h);
  41094. #if 0 /* not strictly necessary */
  41095. heap->strtable[i] = NULL;
  41096. #endif
  41097. }
  41098. #if defined(DUK_USE_HEAPPTR16)
  41099. DUK_FREE(heap, heap->strtable16);
  41100. #else
  41101. DUK_FREE(heap, heap->strtable);
  41102. #endif
  41103. #if 0 /* not strictly necessary */
  41104. heap->strtable = NULL;
  41105. #endif
  41106. }
  41107. }
  41108. #endif /* DUK_USE_STRTAB_PROBE */
  41109. /* Undefine local defines */
  41110. #undef DUK__HASH_INITIAL
  41111. #undef DUK__HASH_PROBE_STEP
  41112. #undef DUK__DELETED_MARKER
  41113. #line 1 "duk_hobject_alloc.c"
  41114. /*
  41115. * Hobject allocation.
  41116. *
  41117. * Provides primitive allocation functions for all object types (plain object,
  41118. * compiled function, native function, thread). The object return is not yet
  41119. * in "heap allocated" list and has a refcount of zero, so caller must careful.
  41120. */
  41121. /* include removed: duk_internal.h */
  41122. DUK_LOCAL void duk__init_object_parts(duk_heap *heap, duk_hobject *obj, duk_uint_t hobject_flags) {
  41123. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  41124. DUK_HOBJECT_SET_PROPS(heap, obj, NULL);
  41125. #endif
  41126. /* XXX: macro? sets both heaphdr and object flags */
  41127. obj->hdr.h_flags = hobject_flags;
  41128. DUK_HEAPHDR_SET_TYPE(&obj->hdr, DUK_HTYPE_OBJECT); /* also goes into flags */
  41129. #if defined(DUK_USE_HEAPPTR16)
  41130. /* Zero encoded pointer is required to match NULL */
  41131. DUK_HEAPHDR_SET_NEXT(heap, &obj->hdr, NULL);
  41132. #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
  41133. DUK_HEAPHDR_SET_PREV(heap, &obj->hdr, NULL);
  41134. #endif
  41135. #endif
  41136. DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap, &obj->hdr);
  41137. /*
  41138. * obj->props is intentionally left as NULL, and duk_hobject_props.c must deal
  41139. * with this properly. This is intentional: empty objects consume a minimum
  41140. * amount of memory. Further, an initial allocation might fail and cause
  41141. * 'obj' to "leak" (require a mark-and-sweep) since it is not reachable yet.
  41142. */
  41143. }
  41144. /*
  41145. * Allocate an duk_hobject.
  41146. *
  41147. * The allocated object has no allocation for properties; the caller may
  41148. * want to force a resize if a desired size is known.
  41149. *
  41150. * The allocated object has zero reference count and is not reachable.
  41151. * The caller MUST make the object reachable and increase its reference
  41152. * count before invoking any operation that might require memory allocation.
  41153. */
  41154. DUK_INTERNAL duk_hobject *duk_hobject_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
  41155. duk_hobject *res;
  41156. DUK_ASSERT(heap != NULL);
  41157. /* different memory layout, alloc size, and init */
  41158. DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_COMPILEDFUNCTION) == 0);
  41159. DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_NATIVEFUNCTION) == 0);
  41160. DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_THREAD) == 0);
  41161. res = (duk_hobject *) DUK_ALLOC(heap, sizeof(duk_hobject));
  41162. if (!res) {
  41163. return NULL;
  41164. }
  41165. DUK_MEMZERO(res, sizeof(duk_hobject));
  41166. duk__init_object_parts(heap, res, hobject_flags);
  41167. return res;
  41168. }
  41169. DUK_INTERNAL duk_hcompiledfunction *duk_hcompiledfunction_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
  41170. duk_hcompiledfunction *res;
  41171. res = (duk_hcompiledfunction *) DUK_ALLOC(heap, sizeof(duk_hcompiledfunction));
  41172. if (!res) {
  41173. return NULL;
  41174. }
  41175. DUK_MEMZERO(res, sizeof(duk_hcompiledfunction));
  41176. duk__init_object_parts(heap, &res->obj, hobject_flags);
  41177. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  41178. #ifdef DUK_USE_HEAPPTR16
  41179. /* NULL pointer is required to encode to zero, so memset is enough. */
  41180. #else
  41181. res->data = NULL;
  41182. res->funcs = NULL;
  41183. res->bytecode = NULL;
  41184. #endif
  41185. #endif
  41186. return res;
  41187. }
  41188. DUK_INTERNAL duk_hnativefunction *duk_hnativefunction_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
  41189. duk_hnativefunction *res;
  41190. res = (duk_hnativefunction *) DUK_ALLOC(heap, sizeof(duk_hnativefunction));
  41191. if (!res) {
  41192. return NULL;
  41193. }
  41194. DUK_MEMZERO(res, sizeof(duk_hnativefunction));
  41195. duk__init_object_parts(heap, &res->obj, hobject_flags);
  41196. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  41197. res->func = NULL;
  41198. #endif
  41199. return res;
  41200. }
  41201. DUK_INTERNAL duk_hbufferobject *duk_hbufferobject_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
  41202. duk_hbufferobject *res;
  41203. res = (duk_hbufferobject *) DUK_ALLOC(heap, sizeof(duk_hbufferobject));
  41204. if (!res) {
  41205. return NULL;
  41206. }
  41207. DUK_MEMZERO(res, sizeof(duk_hbufferobject));
  41208. duk__init_object_parts(heap, &res->obj, hobject_flags);
  41209. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  41210. res->buf = NULL;
  41211. #endif
  41212. DUK_ASSERT_HBUFFEROBJECT_VALID(res);
  41213. return res;
  41214. }
  41215. /*
  41216. * Allocate a new thread.
  41217. *
  41218. * Leaves the built-ins array uninitialized. The caller must either
  41219. * initialize a new global context or share existing built-ins from
  41220. * another thread.
  41221. */
  41222. DUK_INTERNAL duk_hthread *duk_hthread_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
  41223. duk_hthread *res;
  41224. res = (duk_hthread *) DUK_ALLOC(heap, sizeof(duk_hthread));
  41225. if (!res) {
  41226. return NULL;
  41227. }
  41228. DUK_MEMZERO(res, sizeof(duk_hthread));
  41229. duk__init_object_parts(heap, &res->obj, hobject_flags);
  41230. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  41231. res->ptr_curr_pc = NULL;
  41232. res->heap = NULL;
  41233. res->valstack = NULL;
  41234. res->valstack_end = NULL;
  41235. res->valstack_bottom = NULL;
  41236. res->valstack_top = NULL;
  41237. res->callstack = NULL;
  41238. res->catchstack = NULL;
  41239. res->resumer = NULL;
  41240. res->compile_ctx = NULL,
  41241. #ifdef DUK_USE_HEAPPTR16
  41242. res->strs16 = NULL;
  41243. #else
  41244. res->strs = NULL;
  41245. #endif
  41246. {
  41247. int i;
  41248. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  41249. res->builtins[i] = NULL;
  41250. }
  41251. }
  41252. #endif
  41253. /* when nothing is running, API calls are in non-strict mode */
  41254. DUK_ASSERT(res->strict == 0);
  41255. res->heap = heap;
  41256. res->valstack_max = DUK_VALSTACK_DEFAULT_MAX;
  41257. res->callstack_max = DUK_CALLSTACK_DEFAULT_MAX;
  41258. res->catchstack_max = DUK_CATCHSTACK_DEFAULT_MAX;
  41259. return res;
  41260. }
  41261. #if 0 /* unused now */
  41262. DUK_INTERNAL duk_hobject *duk_hobject_alloc_checked(duk_hthread *thr, duk_uint_t hobject_flags) {
  41263. duk_hobject *res = duk_hobject_alloc(thr->heap, hobject_flags);
  41264. if (!res) {
  41265. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, "failed to allocate an object");
  41266. }
  41267. return res;
  41268. }
  41269. #endif
  41270. #line 1 "duk_hobject_enum.c"
  41271. /*
  41272. * Hobject enumeration support.
  41273. *
  41274. * Creates an internal enumeration state object to be used e.g. with for-in
  41275. * enumeration. The state object contains a snapshot of target object keys
  41276. * and internal control state for enumeration. Enumerator flags allow caller
  41277. * to e.g. request internal/non-enumerable properties, and to enumerate only
  41278. * "own" properties.
  41279. *
  41280. * Also creates the result value for e.g. Object.keys() based on the same
  41281. * internal structure.
  41282. *
  41283. * This snapshot-based enumeration approach is used to simplify enumeration:
  41284. * non-snapshot-based approaches are difficult to reconcile with mutating
  41285. * the enumeration target, running multiple long-lived enumerators at the
  41286. * same time, garbage collection details, etc. The downside is that the
  41287. * enumerator object is memory inefficient especially for iterating arrays.
  41288. */
  41289. /* include removed: duk_internal.h */
  41290. /* XXX: identify enumeration target with an object index (not top of stack) */
  41291. /* must match exactly the number of internal properties inserted to enumerator */
  41292. #define DUK__ENUM_START_INDEX 2
  41293. DUK_LOCAL const duk_uint16_t duk__bufferobject_virtual_props[] = {
  41294. DUK_STRIDX_LENGTH,
  41295. DUK_STRIDX_BYTE_LENGTH,
  41296. DUK_STRIDX_BYTE_OFFSET,
  41297. DUK_STRIDX_BYTES_PER_ELEMENT
  41298. };
  41299. /*
  41300. * Helper to sort array index keys. The keys are in the enumeration object
  41301. * entry part, starting from DUK__ENUM_START_INDEX, and the entry part is dense.
  41302. *
  41303. * We use insertion sort because it is simple (leading to compact code,)
  41304. * works nicely in-place, and minimizes operations if data is already sorted
  41305. * or nearly sorted (which is a very common case here). It also minimizes
  41306. * the use of element comparisons in general. This is nice because element
  41307. * comparisons here involve re-parsing the string keys into numbers each
  41308. * time, which is naturally very expensive.
  41309. *
  41310. * Note that the entry part values are all "true", e.g.
  41311. *
  41312. * "1" -> true, "3" -> true, "2" -> true
  41313. *
  41314. * so it suffices to only work in the key part without exchanging any keys,
  41315. * simplifying the sort.
  41316. *
  41317. * http://en.wikipedia.org/wiki/Insertion_sort
  41318. *
  41319. * (Compiles to about 160 bytes now as a stand-alone function.)
  41320. */
  41321. DUK_LOCAL void duk__sort_array_indices(duk_hthread *thr, duk_hobject *h_obj) {
  41322. duk_hstring **keys;
  41323. duk_hstring **p_curr, **p_insert, **p_end;
  41324. duk_hstring *h_curr;
  41325. duk_uarridx_t val_highest, val_curr, val_insert;
  41326. DUK_ASSERT(h_obj != NULL);
  41327. DUK_ASSERT(DUK_HOBJECT_GET_ENEXT(h_obj) >= 2); /* control props */
  41328. DUK_UNREF(thr);
  41329. if (DUK_HOBJECT_GET_ENEXT(h_obj) <= 1 + DUK__ENUM_START_INDEX) {
  41330. return;
  41331. }
  41332. keys = DUK_HOBJECT_E_GET_KEY_BASE(thr->heap, h_obj);
  41333. p_end = keys + DUK_HOBJECT_GET_ENEXT(h_obj);
  41334. keys += DUK__ENUM_START_INDEX;
  41335. DUK_DDD(DUK_DDDPRINT("keys=%p, p_end=%p (after skipping enum props)",
  41336. (void *) keys, (void *) p_end));
  41337. #ifdef DUK_USE_DDDPRINT
  41338. {
  41339. duk_uint_fast32_t i;
  41340. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(h_obj); i++) {
  41341. DUK_DDD(DUK_DDDPRINT("initial: %ld %p -> %!O",
  41342. (long) i,
  41343. (void *) DUK_HOBJECT_E_GET_KEY_PTR(thr->heap, h_obj, i),
  41344. (duk_heaphdr *) DUK_HOBJECT_E_GET_KEY(thr->heap, h_obj, i)));
  41345. }
  41346. }
  41347. #endif
  41348. val_highest = DUK_HSTRING_GET_ARRIDX_SLOW(keys[0]);
  41349. for (p_curr = keys + 1; p_curr < p_end; p_curr++) {
  41350. DUK_ASSERT(*p_curr != NULL);
  41351. val_curr = DUK_HSTRING_GET_ARRIDX_SLOW(*p_curr);
  41352. if (val_curr >= val_highest) {
  41353. DUK_DDD(DUK_DDDPRINT("p_curr=%p, p_end=%p, val_highest=%ld, val_curr=%ld -> "
  41354. "already in correct order, next",
  41355. (void *) p_curr, (void *) p_end, (long) val_highest, (long) val_curr));
  41356. val_highest = val_curr;
  41357. continue;
  41358. }
  41359. DUK_DDD(DUK_DDDPRINT("p_curr=%p, p_end=%p, val_highest=%ld, val_curr=%ld -> "
  41360. "needs to be inserted",
  41361. (void *) p_curr, (void *) p_end, (long) val_highest, (long) val_curr));
  41362. /* Needs to be inserted; scan backwards, since we optimize
  41363. * for the case where elements are nearly in order.
  41364. */
  41365. p_insert = p_curr - 1;
  41366. for (;;) {
  41367. val_insert = DUK_HSTRING_GET_ARRIDX_SLOW(*p_insert);
  41368. if (val_insert < val_curr) {
  41369. DUK_DDD(DUK_DDDPRINT("p_insert=%p, val_insert=%ld, val_curr=%ld -> insert after this",
  41370. (void *) p_insert, (long) val_insert, (long) val_curr));
  41371. p_insert++;
  41372. break;
  41373. }
  41374. if (p_insert == keys) {
  41375. DUK_DDD(DUK_DDDPRINT("p_insert=%p -> out of keys, insert to beginning", (void *) p_insert));
  41376. break;
  41377. }
  41378. DUK_DDD(DUK_DDDPRINT("p_insert=%p, val_insert=%ld, val_curr=%ld -> search backwards",
  41379. (void *) p_insert, (long) val_insert, (long) val_curr));
  41380. p_insert--;
  41381. }
  41382. DUK_DDD(DUK_DDDPRINT("final p_insert=%p", (void *) p_insert));
  41383. /* .-- p_insert .-- p_curr
  41384. * v v
  41385. * | ... | insert | ... | curr
  41386. */
  41387. h_curr = *p_curr;
  41388. DUK_DDD(DUK_DDDPRINT("memmove: dest=%p, src=%p, size=%ld, h_curr=%p",
  41389. (void *) (p_insert + 1), (void *) p_insert,
  41390. (long) (p_curr - p_insert), (void *) h_curr));
  41391. DUK_MEMMOVE((void *) (p_insert + 1),
  41392. (const void *) p_insert,
  41393. (size_t) ((p_curr - p_insert) * sizeof(duk_hstring *)));
  41394. *p_insert = h_curr;
  41395. /* keep val_highest */
  41396. }
  41397. #ifdef DUK_USE_DDDPRINT
  41398. {
  41399. duk_uint_fast32_t i;
  41400. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(h_obj); i++) {
  41401. DUK_DDD(DUK_DDDPRINT("final: %ld %p -> %!O",
  41402. (long) i,
  41403. (void *) DUK_HOBJECT_E_GET_KEY_PTR(thr->heap, h_obj, i),
  41404. (duk_heaphdr *) DUK_HOBJECT_E_GET_KEY(thr->heap, h_obj, i)));
  41405. }
  41406. }
  41407. #endif
  41408. }
  41409. /*
  41410. * Create an internal enumerator object E, which has its keys ordered
  41411. * to match desired enumeration ordering. Also initialize internal control
  41412. * properties for enumeration.
  41413. *
  41414. * Note: if an array was used to hold enumeration keys instead, an array
  41415. * scan would be needed to eliminate duplicates found in the prototype chain.
  41416. */
  41417. DUK_INTERNAL void duk_hobject_enumerator_create(duk_context *ctx, duk_small_uint_t enum_flags) {
  41418. duk_hthread *thr = (duk_hthread *) ctx;
  41419. duk_hobject *enum_target;
  41420. duk_hobject *curr;
  41421. duk_hobject *res;
  41422. #if defined(DUK_USE_ES6_PROXY)
  41423. duk_hobject *h_proxy_target;
  41424. duk_hobject *h_proxy_handler;
  41425. duk_hobject *h_trap_result;
  41426. #endif
  41427. duk_uint_fast32_t i, len; /* used for array, stack, and entry indices */
  41428. DUK_ASSERT(ctx != NULL);
  41429. DUK_DDD(DUK_DDDPRINT("create enumerator, stack top: %ld", (long) duk_get_top(ctx)));
  41430. enum_target = duk_require_hobject(ctx, -1);
  41431. DUK_ASSERT(enum_target != NULL);
  41432. duk_push_object_internal(ctx);
  41433. res = duk_require_hobject(ctx, -1);
  41434. DUK_DDD(DUK_DDDPRINT("created internal object"));
  41435. /* [enum_target res] */
  41436. /* Target must be stored so that we can recheck whether or not
  41437. * keys still exist when we enumerate. This is not done if the
  41438. * enumeration result comes from a proxy trap as there is no
  41439. * real object to check against.
  41440. */
  41441. duk_push_hobject(ctx, enum_target);
  41442. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_INT_TARGET);
  41443. /* Initialize index so that we skip internal control keys. */
  41444. duk_push_int(ctx, DUK__ENUM_START_INDEX);
  41445. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_INT_NEXT);
  41446. /*
  41447. * Proxy object handling
  41448. */
  41449. #if defined(DUK_USE_ES6_PROXY)
  41450. if (DUK_LIKELY((enum_flags & DUK_ENUM_NO_PROXY_BEHAVIOR) != 0)) {
  41451. goto skip_proxy;
  41452. }
  41453. if (DUK_LIKELY(!duk_hobject_proxy_check(thr,
  41454. enum_target,
  41455. &h_proxy_target,
  41456. &h_proxy_handler))) {
  41457. goto skip_proxy;
  41458. }
  41459. DUK_DDD(DUK_DDDPRINT("proxy enumeration"));
  41460. duk_push_hobject(ctx, h_proxy_handler);
  41461. if (!duk_get_prop_stridx(ctx, -1, DUK_STRIDX_ENUMERATE)) {
  41462. /* No need to replace the 'enum_target' value in stack, only the
  41463. * enum_target reference. This also ensures that the original
  41464. * enum target is reachable, which keeps the proxy and the proxy
  41465. * target reachable. We do need to replace the internal _Target.
  41466. */
  41467. DUK_DDD(DUK_DDDPRINT("no enumerate trap, enumerate proxy target instead"));
  41468. DUK_DDD(DUK_DDDPRINT("h_proxy_target=%!O", (duk_heaphdr *) h_proxy_target));
  41469. enum_target = h_proxy_target;
  41470. duk_push_hobject(ctx, enum_target); /* -> [ ... enum_target res handler undefined target ] */
  41471. duk_put_prop_stridx(ctx, -4, DUK_STRIDX_INT_TARGET);
  41472. duk_pop_2(ctx); /* -> [ ... enum_target res ] */
  41473. goto skip_proxy;
  41474. }
  41475. /* [ ... enum_target res handler trap ] */
  41476. duk_insert(ctx, -2);
  41477. duk_push_hobject(ctx, h_proxy_target); /* -> [ ... enum_target res trap handler target ] */
  41478. duk_call_method(ctx, 1 /*nargs*/); /* -> [ ... enum_target res trap_result ] */
  41479. h_trap_result = duk_require_hobject(ctx, -1);
  41480. DUK_UNREF(h_trap_result);
  41481. /* Copy trap result keys into the enumerator object. */
  41482. len = (duk_uint_fast32_t) duk_get_length(ctx, -1);
  41483. for (i = 0; i < len; i++) {
  41484. /* XXX: not sure what the correct semantic details are here,
  41485. * e.g. handling of missing values (gaps), handling of non-array
  41486. * trap results, etc.
  41487. *
  41488. * For keys, we simply skip non-string keys which seems to be
  41489. * consistent with how e.g. Object.keys() will process proxy trap
  41490. * results (ES6, Section 19.1.2.14).
  41491. */
  41492. if (duk_get_prop_index(ctx, -1, i) && duk_is_string(ctx, -1)) {
  41493. /* [ ... enum_target res trap_result val ] */
  41494. duk_push_true(ctx);
  41495. /* [ ... enum_target res trap_result val true ] */
  41496. duk_put_prop(ctx, -4);
  41497. } else {
  41498. duk_pop(ctx);
  41499. }
  41500. }
  41501. /* [ ... enum_target res trap_result ] */
  41502. duk_pop(ctx);
  41503. duk_remove(ctx, -2);
  41504. /* [ ... res ] */
  41505. /* The internal _Target property is kept pointing to the original
  41506. * enumeration target (the proxy object), so that the enumerator
  41507. * 'next' operation can read property values if so requested. The
  41508. * fact that the _Target is a proxy disables key existence check
  41509. * during enumeration.
  41510. */
  41511. DUK_DDD(DUK_DDDPRINT("proxy enumeration, final res: %!O", (duk_heaphdr *) res));
  41512. goto compact_and_return;
  41513. skip_proxy:
  41514. #endif /* DUK_USE_ES6_PROXY */
  41515. curr = enum_target;
  41516. while (curr) {
  41517. /*
  41518. * Virtual properties.
  41519. *
  41520. * String and buffer indices are virtual and always enumerable,
  41521. * 'length' is virtual and non-enumerable. Array and arguments
  41522. * object props have special behavior but are concrete.
  41523. */
  41524. if (DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(curr) ||
  41525. DUK_HOBJECT_IS_BUFFEROBJECT(curr)) {
  41526. /* String and buffer enumeration behavior is identical now,
  41527. * so use shared handler.
  41528. */
  41529. if (DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(curr)) {
  41530. duk_hstring *h_val;
  41531. h_val = duk_hobject_get_internal_value_string(thr->heap, curr);
  41532. DUK_ASSERT(h_val != NULL); /* string objects must not created without internal value */
  41533. len = (duk_uint_fast32_t) DUK_HSTRING_GET_CHARLEN(h_val);
  41534. } else {
  41535. duk_hbufferobject *h_bufobj;
  41536. DUK_ASSERT(DUK_HOBJECT_IS_BUFFEROBJECT(curr));
  41537. h_bufobj = (duk_hbufferobject *) curr;
  41538. if (h_bufobj == NULL) {
  41539. /* Neutered buffer, zero length seems
  41540. * like good behavior here.
  41541. */
  41542. len = 0;
  41543. } else {
  41544. /* There's intentionally no check for
  41545. * current underlying buffer length.
  41546. */
  41547. len = (duk_uint_fast32_t) (h_bufobj->length >> h_bufobj->shift);
  41548. }
  41549. }
  41550. for (i = 0; i < len; i++) {
  41551. duk_hstring *k;
  41552. k = duk_heap_string_intern_u32_checked(thr, i);
  41553. DUK_ASSERT(k);
  41554. duk_push_hstring(ctx, k);
  41555. duk_push_true(ctx);
  41556. /* [enum_target res key true] */
  41557. duk_put_prop(ctx, -3);
  41558. /* [enum_target res] */
  41559. }
  41560. /* 'length' and other virtual properties are not
  41561. * enumerable, but are included if non-enumerable
  41562. * properties are requested.
  41563. */
  41564. if (enum_flags & DUK_ENUM_INCLUDE_NONENUMERABLE) {
  41565. duk_uint_fast32_t n;
  41566. if (DUK_HOBJECT_IS_BUFFEROBJECT(curr)) {
  41567. n = sizeof(duk__bufferobject_virtual_props) / sizeof(duk_uint16_t);
  41568. } else {
  41569. DUK_ASSERT(DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(curr));
  41570. DUK_ASSERT(duk__bufferobject_virtual_props[0] == DUK_STRIDX_LENGTH);
  41571. n = 1; /* only 'length' */
  41572. }
  41573. for (i = 0; i < n; i++) {
  41574. duk_push_hstring_stridx(ctx, duk__bufferobject_virtual_props[i]);
  41575. duk_push_true(ctx);
  41576. duk_put_prop(ctx, -3);
  41577. }
  41578. }
  41579. } else if (DUK_HOBJECT_HAS_EXOTIC_DUKFUNC(curr)) {
  41580. if (enum_flags & DUK_ENUM_INCLUDE_NONENUMERABLE) {
  41581. duk_push_hstring_stridx(ctx, DUK_STRIDX_LENGTH);
  41582. duk_push_true(ctx);
  41583. duk_put_prop(ctx, -3);
  41584. }
  41585. }
  41586. /*
  41587. * Array part
  41588. *
  41589. * Note: ordering between array and entry part must match 'abandon array'
  41590. * behavior in duk_hobject_props.c: key order after an array is abandoned
  41591. * must be the same.
  41592. */
  41593. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(curr); i++) {
  41594. duk_hstring *k;
  41595. duk_tval *tv;
  41596. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, curr, i);
  41597. if (DUK_TVAL_IS_UNUSED(tv)) {
  41598. continue;
  41599. }
  41600. k = duk_heap_string_intern_u32_checked(thr, i);
  41601. DUK_ASSERT(k);
  41602. duk_push_hstring(ctx, k);
  41603. duk_push_true(ctx);
  41604. /* [enum_target res key true] */
  41605. duk_put_prop(ctx, -3);
  41606. /* [enum_target res] */
  41607. }
  41608. /*
  41609. * Entries part
  41610. */
  41611. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(curr); i++) {
  41612. duk_hstring *k;
  41613. k = DUK_HOBJECT_E_GET_KEY(thr->heap, curr, i);
  41614. if (!k) {
  41615. continue;
  41616. }
  41617. if (!DUK_HOBJECT_E_SLOT_IS_ENUMERABLE(thr->heap, curr, i) &&
  41618. !(enum_flags & DUK_ENUM_INCLUDE_NONENUMERABLE)) {
  41619. continue;
  41620. }
  41621. if (DUK_HSTRING_HAS_INTERNAL(k) &&
  41622. !(enum_flags & DUK_ENUM_INCLUDE_INTERNAL)) {
  41623. continue;
  41624. }
  41625. if ((enum_flags & DUK_ENUM_ARRAY_INDICES_ONLY) &&
  41626. (DUK_HSTRING_GET_ARRIDX_SLOW(k) == DUK_HSTRING_NO_ARRAY_INDEX)) {
  41627. continue;
  41628. }
  41629. DUK_ASSERT(DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, curr, i) ||
  41630. !DUK_TVAL_IS_UNUSED(&DUK_HOBJECT_E_GET_VALUE_PTR(thr->heap, curr, i)->v));
  41631. duk_push_hstring(ctx, k);
  41632. duk_push_true(ctx);
  41633. /* [enum_target res key true] */
  41634. duk_put_prop(ctx, -3);
  41635. /* [enum_target res] */
  41636. }
  41637. if (enum_flags & DUK_ENUM_OWN_PROPERTIES_ONLY) {
  41638. break;
  41639. }
  41640. curr = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, curr);
  41641. }
  41642. /* [enum_target res] */
  41643. duk_remove(ctx, -2);
  41644. /* [res] */
  41645. if ((enum_flags & (DUK_ENUM_ARRAY_INDICES_ONLY | DUK_ENUM_SORT_ARRAY_INDICES)) ==
  41646. (DUK_ENUM_ARRAY_INDICES_ONLY | DUK_ENUM_SORT_ARRAY_INDICES)) {
  41647. /*
  41648. * Some E5/E5.1 algorithms require that array indices are iterated
  41649. * in a strictly ascending order. This is the case for e.g.
  41650. * Array.prototype.forEach() and JSON.stringify() PropertyList
  41651. * handling.
  41652. *
  41653. * To ensure this property for arrays with an array part (and
  41654. * arbitrary objects too, since e.g. forEach() can be applied
  41655. * to an array), the caller can request that we sort the keys
  41656. * here.
  41657. */
  41658. /* XXX: avoid this at least when enum_target is an Array, it has an
  41659. * array part, and no ancestor properties were included? Not worth
  41660. * it for JSON, but maybe worth it for forEach().
  41661. */
  41662. /* XXX: may need a 'length' filter for forEach()
  41663. */
  41664. DUK_DDD(DUK_DDDPRINT("sort array indices by caller request"));
  41665. duk__sort_array_indices(thr, res);
  41666. }
  41667. #if defined(DUK_USE_ES6_PROXY)
  41668. compact_and_return:
  41669. #endif
  41670. /* compact; no need to seal because object is internal */
  41671. duk_hobject_compact_props(thr, res);
  41672. DUK_DDD(DUK_DDDPRINT("created enumerator object: %!iT", (duk_tval *) duk_get_tval(ctx, -1)));
  41673. }
  41674. /*
  41675. * Returns non-zero if a key and/or value was enumerated, and:
  41676. *
  41677. * [enum] -> [key] (get_value == 0)
  41678. * [enum] -> [key value] (get_value == 1)
  41679. *
  41680. * Returns zero without pushing anything on the stack otherwise.
  41681. */
  41682. DUK_INTERNAL duk_bool_t duk_hobject_enumerator_next(duk_context *ctx, duk_bool_t get_value) {
  41683. duk_hthread *thr = (duk_hthread *) ctx;
  41684. duk_hobject *e;
  41685. duk_hobject *enum_target;
  41686. duk_hstring *res = NULL;
  41687. duk_uint_fast32_t idx;
  41688. duk_bool_t check_existence;
  41689. DUK_ASSERT(ctx != NULL);
  41690. /* [... enum] */
  41691. e = duk_require_hobject(ctx, -1);
  41692. /* XXX use get tval ptr, more efficient */
  41693. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_NEXT);
  41694. idx = (duk_uint_fast32_t) duk_require_uint(ctx, -1);
  41695. duk_pop(ctx);
  41696. DUK_DDD(DUK_DDDPRINT("enumeration: index is: %ld", (long) idx));
  41697. /* Enumeration keys are checked against the enumeration target (to see
  41698. * that they still exist). In the proxy enumeration case _Target will
  41699. * be the proxy, and checking key existence against the proxy is not
  41700. * required (or sensible, as the keys may be fully virtual).
  41701. */
  41702. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_TARGET);
  41703. enum_target = duk_require_hobject(ctx, -1);
  41704. DUK_ASSERT(enum_target != NULL);
  41705. #if defined(DUK_USE_ES6_PROXY)
  41706. check_existence = (!DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(enum_target));
  41707. #else
  41708. check_existence = 1;
  41709. #endif
  41710. duk_pop(ctx); /* still reachable */
  41711. DUK_DDD(DUK_DDDPRINT("getting next enum value, enum_target=%!iO, enumerator=%!iT",
  41712. (duk_heaphdr *) enum_target, (duk_tval *) duk_get_tval(ctx, -1)));
  41713. /* no array part */
  41714. for (;;) {
  41715. duk_hstring *k;
  41716. if (idx >= DUK_HOBJECT_GET_ENEXT(e)) {
  41717. DUK_DDD(DUK_DDDPRINT("enumeration: ran out of elements"));
  41718. break;
  41719. }
  41720. /* we know these because enum objects are internally created */
  41721. k = DUK_HOBJECT_E_GET_KEY(thr->heap, e, idx);
  41722. DUK_ASSERT(k != NULL);
  41723. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, e, idx));
  41724. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(&DUK_HOBJECT_E_GET_VALUE(thr->heap, e, idx).v));
  41725. idx++;
  41726. /* recheck that the property still exists */
  41727. if (check_existence && !duk_hobject_hasprop_raw(thr, enum_target, k)) {
  41728. DUK_DDD(DUK_DDDPRINT("property deleted during enumeration, skip"));
  41729. continue;
  41730. }
  41731. DUK_DDD(DUK_DDDPRINT("enumeration: found element, key: %!O", (duk_heaphdr *) k));
  41732. res = k;
  41733. break;
  41734. }
  41735. DUK_DDD(DUK_DDDPRINT("enumeration: updating next index to %ld", (long) idx));
  41736. duk_push_u32(ctx, (duk_uint32_t) idx);
  41737. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_INT_NEXT);
  41738. /* [... enum] */
  41739. if (res) {
  41740. duk_push_hstring(ctx, res);
  41741. if (get_value) {
  41742. duk_push_hobject(ctx, enum_target);
  41743. duk_dup(ctx, -2); /* -> [... enum key enum_target key] */
  41744. duk_get_prop(ctx, -2); /* -> [... enum key enum_target val] */
  41745. duk_remove(ctx, -2); /* -> [... enum key val] */
  41746. duk_remove(ctx, -3); /* -> [... key val] */
  41747. } else {
  41748. duk_remove(ctx, -2); /* -> [... key] */
  41749. }
  41750. return 1;
  41751. } else {
  41752. duk_pop(ctx); /* -> [...] */
  41753. return 0;
  41754. }
  41755. }
  41756. /*
  41757. * Get enumerated keys in an Ecmascript array. Matches Object.keys() behavior
  41758. * described in E5 Section 15.2.3.14.
  41759. */
  41760. DUK_INTERNAL duk_ret_t duk_hobject_get_enumerated_keys(duk_context *ctx, duk_small_uint_t enum_flags) {
  41761. duk_hthread *thr = (duk_hthread *) ctx;
  41762. duk_hobject *e;
  41763. duk_uint_fast32_t i;
  41764. duk_uint_fast32_t idx;
  41765. DUK_ASSERT(ctx != NULL);
  41766. DUK_ASSERT(duk_get_hobject(ctx, -1) != NULL);
  41767. DUK_UNREF(thr);
  41768. /* Create a temporary enumerator to get the (non-duplicated) key list;
  41769. * the enumerator state is initialized without being needed, but that
  41770. * has little impact.
  41771. */
  41772. duk_hobject_enumerator_create(ctx, enum_flags);
  41773. duk_push_array(ctx);
  41774. /* [enum_target enum res] */
  41775. e = duk_require_hobject(ctx, -2);
  41776. DUK_ASSERT(e != NULL);
  41777. idx = 0;
  41778. for (i = DUK__ENUM_START_INDEX; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(e); i++) {
  41779. duk_hstring *k;
  41780. k = DUK_HOBJECT_E_GET_KEY(thr->heap, e, i);
  41781. DUK_ASSERT(k); /* enumerator must have no keys deleted */
  41782. /* [enum_target enum res] */
  41783. duk_push_hstring(ctx, k);
  41784. duk_put_prop_index(ctx, -2, idx);
  41785. idx++;
  41786. }
  41787. /* [enum_target enum res] */
  41788. duk_remove(ctx, -2);
  41789. /* [enum_target res] */
  41790. return 1; /* return 1 to allow callers to tail call */
  41791. }
  41792. #line 1 "duk_hobject_finalizer.c"
  41793. /*
  41794. * Run an duk_hobject finalizer. Used for both reference counting
  41795. * and mark-and-sweep algorithms. Must never throw an error.
  41796. *
  41797. * There is no return value. Any return value or error thrown by
  41798. * the finalizer is ignored (although errors are debug logged).
  41799. *
  41800. * Notes:
  41801. *
  41802. * - The thread used for calling the finalizer is the same as the
  41803. * 'thr' argument. This may need to change later.
  41804. *
  41805. * - The finalizer thread 'top' assertions are there because it is
  41806. * critical that strict stack policy is observed (i.e. no cruft
  41807. * left on the finalizer stack).
  41808. */
  41809. /* include removed: duk_internal.h */
  41810. DUK_LOCAL duk_ret_t duk__finalize_helper(duk_context *ctx) {
  41811. DUK_ASSERT(ctx != NULL);
  41812. DUK_DDD(DUK_DDDPRINT("protected finalization helper running"));
  41813. /* [... obj] */
  41814. /* XXX: Finalizer lookup should traverse the prototype chain (to allow
  41815. * inherited finalizers) but should not invoke accessors or proxy object
  41816. * behavior. At the moment this lookup will invoke proxy behavior, so
  41817. * caller must ensure that this function is not called if the target is
  41818. * a Proxy.
  41819. */
  41820. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_FINALIZER); /* -> [... obj finalizer] */
  41821. if (!duk_is_callable(ctx, -1)) {
  41822. DUK_DDD(DUK_DDDPRINT("-> no finalizer or finalizer not callable"));
  41823. return 0;
  41824. }
  41825. duk_dup(ctx, -2); /* -> [... obj finalizer obj] */
  41826. DUK_DDD(DUK_DDDPRINT("-> finalizer found, calling finalizer"));
  41827. duk_call(ctx, 1); /* -> [... obj retval] */
  41828. DUK_DDD(DUK_DDDPRINT("finalizer finished successfully"));
  41829. return 0;
  41830. /* Note: we rely on duk_safe_call() to fix up the stack for the caller,
  41831. * so we don't need to pop stuff here. There is no return value;
  41832. * caller determines rescued status based on object refcount.
  41833. */
  41834. }
  41835. DUK_INTERNAL void duk_hobject_run_finalizer(duk_hthread *thr, duk_hobject *obj) {
  41836. duk_context *ctx = (duk_context *) thr;
  41837. duk_ret_t rc;
  41838. #ifdef DUK_USE_ASSERTIONS
  41839. duk_idx_t entry_top;
  41840. #endif
  41841. DUK_DDD(DUK_DDDPRINT("running object finalizer for object: %p", (void *) obj));
  41842. DUK_ASSERT(thr != NULL);
  41843. DUK_ASSERT(ctx != NULL);
  41844. DUK_ASSERT(obj != NULL);
  41845. DUK_ASSERT_VALSTACK_SPACE(thr, 1);
  41846. #ifdef DUK_USE_ASSERTIONS
  41847. entry_top = duk_get_top(ctx);
  41848. #endif
  41849. /*
  41850. * Get and call the finalizer. All of this must be wrapped
  41851. * in a protected call, because even getting the finalizer
  41852. * may trigger an error (getter may throw one, for instance).
  41853. */
  41854. /* XXX: use a NULL error handler for the finalizer call? */
  41855. DUK_DDD(DUK_DDDPRINT("-> finalizer found, calling wrapped finalize helper"));
  41856. duk_push_hobject(ctx, obj); /* this also increases refcount by one */
  41857. rc = duk_safe_call(ctx, duk__finalize_helper, 0 /*nargs*/, 1 /*nrets*/); /* -> [... obj retval/error] */
  41858. DUK_ASSERT_TOP(ctx, entry_top + 2); /* duk_safe_call discipline */
  41859. if (rc != DUK_EXEC_SUCCESS) {
  41860. /* Note: we ask for one return value from duk_safe_call to get this
  41861. * error debugging here.
  41862. */
  41863. DUK_D(DUK_DPRINT("wrapped finalizer call failed for object %p (ignored); error: %!T",
  41864. (void *) obj, (duk_tval *) duk_get_tval(ctx, -1)));
  41865. }
  41866. duk_pop_2(ctx); /* -> [...] */
  41867. DUK_ASSERT_TOP(ctx, entry_top);
  41868. }
  41869. #line 1 "duk_hobject_misc.c"
  41870. /*
  41871. * Misc support functions
  41872. */
  41873. /* include removed: duk_internal.h */
  41874. DUK_INTERNAL duk_bool_t duk_hobject_prototype_chain_contains(duk_hthread *thr, duk_hobject *h, duk_hobject *p, duk_bool_t ignore_loop) {
  41875. duk_uint_t sanity;
  41876. DUK_ASSERT(thr != NULL);
  41877. DUK_ASSERT(h != NULL);
  41878. /* allow 'p' to be NULL; then the result is always false */
  41879. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  41880. do {
  41881. if (h == p) {
  41882. return 1;
  41883. }
  41884. if (sanity-- == 0) {
  41885. if (ignore_loop) {
  41886. break;
  41887. } else {
  41888. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  41889. }
  41890. }
  41891. h = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
  41892. } while (h);
  41893. return 0;
  41894. }
  41895. DUK_INTERNAL void duk_hobject_set_prototype_updref(duk_hthread *thr, duk_hobject *h, duk_hobject *p) {
  41896. #ifdef DUK_USE_REFERENCE_COUNTING
  41897. duk_hobject *tmp;
  41898. DUK_ASSERT(h);
  41899. tmp = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
  41900. DUK_HOBJECT_SET_PROTOTYPE(thr->heap, h, p);
  41901. DUK_HOBJECT_INCREF_ALLOWNULL(thr, p); /* avoid problems if p == h->prototype */
  41902. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  41903. #else
  41904. DUK_ASSERT(h);
  41905. DUK_UNREF(thr);
  41906. DUK_HOBJECT_SET_PROTOTYPE(thr->heap, h, p);
  41907. #endif
  41908. }
  41909. #line 1 "duk_hobject_pc2line.c"
  41910. /*
  41911. * Helpers for creating and querying pc2line debug data, which
  41912. * converts a bytecode program counter to a source line number.
  41913. *
  41914. * The run-time pc2line data is bit-packed, and documented in:
  41915. *
  41916. * doc/function-objects.rst
  41917. */
  41918. /* include removed: duk_internal.h */
  41919. #if defined(DUK_USE_PC2LINE)
  41920. /* Generate pc2line data for an instruction sequence, leaving a buffer on stack top. */
  41921. DUK_INTERNAL void duk_hobject_pc2line_pack(duk_hthread *thr, duk_compiler_instr *instrs, duk_uint_fast32_t length) {
  41922. duk_context *ctx = (duk_context *) thr;
  41923. duk_hbuffer_dynamic *h_buf;
  41924. duk_bitencoder_ctx be_ctx_alloc;
  41925. duk_bitencoder_ctx *be_ctx = &be_ctx_alloc;
  41926. duk_uint32_t *hdr;
  41927. duk_size_t new_size;
  41928. duk_uint_fast32_t num_header_entries;
  41929. duk_uint_fast32_t curr_offset;
  41930. duk_int_fast32_t curr_line, next_line, diff_line;
  41931. duk_uint_fast32_t curr_pc;
  41932. duk_uint_fast32_t hdr_index;
  41933. DUK_ASSERT(length <= DUK_COMPILER_MAX_BYTECODE_LENGTH);
  41934. /* XXX: add proper spare handling to dynamic buffer, to minimize
  41935. * reallocs; currently there is no spare at all.
  41936. */
  41937. num_header_entries = (length + DUK_PC2LINE_SKIP - 1) / DUK_PC2LINE_SKIP;
  41938. curr_offset = (duk_uint_fast32_t) (sizeof(duk_uint32_t) + num_header_entries * sizeof(duk_uint32_t) * 2);
  41939. duk_push_dynamic_buffer(ctx, (duk_size_t) curr_offset);
  41940. h_buf = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, -1);
  41941. DUK_ASSERT(h_buf != NULL);
  41942. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(h_buf) && !DUK_HBUFFER_HAS_EXTERNAL(h_buf));
  41943. hdr = (duk_uint32_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h_buf);
  41944. DUK_ASSERT(hdr != NULL);
  41945. hdr[0] = (duk_uint32_t) length; /* valid pc range is [0, length[ */
  41946. curr_pc = 0U;
  41947. while (curr_pc < length) {
  41948. new_size = (duk_size_t) (curr_offset + DUK_PC2LINE_MAX_DIFF_LENGTH);
  41949. duk_hbuffer_resize(thr, h_buf, new_size);
  41950. hdr = (duk_uint32_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h_buf);
  41951. DUK_ASSERT(hdr != NULL);
  41952. DUK_ASSERT(curr_pc < length);
  41953. hdr_index = 1 + (curr_pc / DUK_PC2LINE_SKIP) * 2;
  41954. curr_line = (duk_int_fast32_t) instrs[curr_pc].line;
  41955. hdr[hdr_index + 0] = (duk_uint32_t) curr_line;
  41956. hdr[hdr_index + 1] = (duk_uint32_t) curr_offset;
  41957. #if 0
  41958. DUK_DDD(DUK_DDDPRINT("hdr[%ld]: pc=%ld line=%ld offset=%ld",
  41959. (long) (curr_pc / DUK_PC2LINE_SKIP),
  41960. (long) curr_pc,
  41961. (long) hdr[hdr_index + 0],
  41962. (long) hdr[hdr_index + 1]));
  41963. #endif
  41964. DUK_MEMZERO(be_ctx, sizeof(*be_ctx));
  41965. be_ctx->data = ((duk_uint8_t *) hdr) + curr_offset;
  41966. be_ctx->length = (duk_size_t) DUK_PC2LINE_MAX_DIFF_LENGTH;
  41967. for (;;) {
  41968. curr_pc++;
  41969. if ( ((curr_pc % DUK_PC2LINE_SKIP) == 0) || /* end of diff run */
  41970. (curr_pc >= length) ) { /* end of bytecode */
  41971. break;
  41972. }
  41973. DUK_ASSERT(curr_pc < length);
  41974. next_line = (duk_int32_t) instrs[curr_pc].line;
  41975. diff_line = next_line - curr_line;
  41976. #if 0
  41977. DUK_DDD(DUK_DDDPRINT("curr_line=%ld, next_line=%ld -> diff_line=%ld",
  41978. (long) curr_line, (long) next_line, (long) diff_line));
  41979. #endif
  41980. if (diff_line == 0) {
  41981. /* 0 */
  41982. duk_be_encode(be_ctx, 0, 1);
  41983. } else if (diff_line >= 1 && diff_line <= 4) {
  41984. /* 1 0 <2 bits> */
  41985. duk_be_encode(be_ctx, (0x02 << 2) + (diff_line - 1), 4);
  41986. } else if (diff_line >= -0x80 && diff_line <= 0x7f) {
  41987. /* 1 1 0 <8 bits> */
  41988. DUK_ASSERT(diff_line + 0x80 >= 0 && diff_line + 0x80 <= 0xff);
  41989. duk_be_encode(be_ctx, (0x06 << 8) + (diff_line + 0x80), 11);
  41990. } else {
  41991. /* 1 1 1 <32 bits>
  41992. * Encode in two parts to avoid bitencode 24-bit limitation
  41993. */
  41994. duk_be_encode(be_ctx, (0x07 << 16) + ((next_line >> 16) & 0xffffU), 19);
  41995. duk_be_encode(be_ctx, next_line & 0xffffU, 16);
  41996. }
  41997. curr_line = next_line;
  41998. }
  41999. duk_be_finish(be_ctx);
  42000. DUK_ASSERT(!be_ctx->truncated);
  42001. /* be_ctx->offset == length of encoded bitstream */
  42002. curr_offset += (duk_uint_fast32_t) be_ctx->offset;
  42003. }
  42004. /* compact */
  42005. new_size = (duk_size_t) curr_offset;
  42006. duk_hbuffer_resize(thr, h_buf, new_size);
  42007. (void) duk_to_fixed_buffer(ctx, -1, NULL);
  42008. DUK_DDD(DUK_DDDPRINT("final pc2line data: pc_limit=%ld, length=%ld, %lf bits/opcode --> %!ixT",
  42009. (long) length, (long) new_size, (double) new_size * 8.0 / (double) length,
  42010. (duk_tval *) duk_get_tval(ctx, -1)));
  42011. }
  42012. /* PC is unsigned. If caller does PC arithmetic and gets a negative result,
  42013. * it will map to a large PC which is out of bounds and causes a zero to be
  42014. * returned.
  42015. */
  42016. DUK_LOCAL duk_uint_fast32_t duk__hobject_pc2line_query_raw(duk_hthread *thr, duk_hbuffer_fixed *buf, duk_uint_fast32_t pc) {
  42017. duk_bitdecoder_ctx bd_ctx_alloc;
  42018. duk_bitdecoder_ctx *bd_ctx = &bd_ctx_alloc;
  42019. duk_uint32_t *hdr;
  42020. duk_uint_fast32_t start_offset;
  42021. duk_uint_fast32_t pc_limit;
  42022. duk_uint_fast32_t hdr_index;
  42023. duk_uint_fast32_t pc_base;
  42024. duk_uint_fast32_t n;
  42025. duk_uint_fast32_t curr_line;
  42026. DUK_ASSERT(buf != NULL);
  42027. DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) buf) && !DUK_HBUFFER_HAS_EXTERNAL((duk_hbuffer *) buf));
  42028. DUK_UNREF(thr);
  42029. /*
  42030. * Use the index in the header to find the right starting point
  42031. */
  42032. hdr_index = pc / DUK_PC2LINE_SKIP;
  42033. pc_base = hdr_index * DUK_PC2LINE_SKIP;
  42034. n = pc - pc_base;
  42035. if (DUK_HBUFFER_FIXED_GET_SIZE(buf) <= sizeof(duk_uint32_t)) {
  42036. DUK_DD(DUK_DDPRINT("pc2line lookup failed: buffer is smaller than minimal header"));
  42037. goto error;
  42038. }
  42039. hdr = (duk_uint32_t *) (void *) DUK_HBUFFER_FIXED_GET_DATA_PTR(thr->heap, buf);
  42040. pc_limit = hdr[0];
  42041. if (pc >= pc_limit) {
  42042. /* Note: pc is unsigned and cannot be negative */
  42043. DUK_DD(DUK_DDPRINT("pc2line lookup failed: pc out of bounds (pc=%ld, limit=%ld)",
  42044. (long) pc, (long) pc_limit));
  42045. goto error;
  42046. }
  42047. curr_line = hdr[1 + hdr_index * 2];
  42048. start_offset = hdr[1 + hdr_index * 2 + 1];
  42049. if ((duk_size_t) start_offset > DUK_HBUFFER_FIXED_GET_SIZE(buf)) {
  42050. DUK_DD(DUK_DDPRINT("pc2line lookup failed: start_offset out of bounds (start_offset=%ld, buffer_size=%ld)",
  42051. (long) start_offset, (long) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) buf)));
  42052. goto error;
  42053. }
  42054. /*
  42055. * Iterate the bitstream (line diffs) until PC is reached
  42056. */
  42057. DUK_MEMZERO(bd_ctx, sizeof(*bd_ctx));
  42058. bd_ctx->data = ((duk_uint8_t *) hdr) + start_offset;
  42059. bd_ctx->length = (duk_size_t) (DUK_HBUFFER_FIXED_GET_SIZE(buf) - start_offset);
  42060. #if 0
  42061. DUK_DDD(DUK_DDDPRINT("pc2line lookup: pc=%ld -> hdr_index=%ld, pc_base=%ld, n=%ld, start_offset=%ld",
  42062. (long) pc, (long) hdr_index, (long) pc_base, (long) n, (long) start_offset));
  42063. #endif
  42064. while (n > 0) {
  42065. #if 0
  42066. DUK_DDD(DUK_DDDPRINT("lookup: n=%ld, curr_line=%ld", (long) n, (long) curr_line));
  42067. #endif
  42068. if (duk_bd_decode_flag(bd_ctx)) {
  42069. if (duk_bd_decode_flag(bd_ctx)) {
  42070. if (duk_bd_decode_flag(bd_ctx)) {
  42071. /* 1 1 1 <32 bits> */
  42072. duk_uint_fast32_t t;
  42073. t = duk_bd_decode(bd_ctx, 16); /* workaround: max nbits = 24 now */
  42074. t = (t << 16) + duk_bd_decode(bd_ctx, 16);
  42075. curr_line = t;
  42076. } else {
  42077. /* 1 1 0 <8 bits> */
  42078. duk_uint_fast32_t t;
  42079. t = duk_bd_decode(bd_ctx, 8);
  42080. curr_line = curr_line + t - 0x80;
  42081. }
  42082. } else {
  42083. /* 1 0 <2 bits> */
  42084. duk_uint_fast32_t t;
  42085. t = duk_bd_decode(bd_ctx, 2);
  42086. curr_line = curr_line + t + 1;
  42087. }
  42088. } else {
  42089. /* 0: no change */
  42090. }
  42091. n--;
  42092. }
  42093. DUK_DDD(DUK_DDDPRINT("pc2line lookup result: pc %ld -> line %ld", (long) pc, (long) curr_line));
  42094. return curr_line;
  42095. error:
  42096. DUK_D(DUK_DPRINT("pc2line conversion failed for pc=%ld", (long) pc));
  42097. return 0;
  42098. }
  42099. DUK_INTERNAL duk_uint_fast32_t duk_hobject_pc2line_query(duk_context *ctx, duk_idx_t idx_func, duk_uint_fast32_t pc) {
  42100. duk_hbuffer_fixed *pc2line;
  42101. duk_uint_fast32_t line;
  42102. /* XXX: now that pc2line is used by the debugger quite heavily in
  42103. * checked execution, this should be optimized to avoid value stack
  42104. * and perhaps also implement some form of pc2line caching (see
  42105. * future work in debugger.rst).
  42106. */
  42107. duk_get_prop_stridx(ctx, idx_func, DUK_STRIDX_INT_PC2LINE);
  42108. pc2line = (duk_hbuffer_fixed *) duk_get_hbuffer(ctx, -1);
  42109. if (pc2line != NULL) {
  42110. DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) pc2line) && !DUK_HBUFFER_HAS_EXTERNAL((duk_hbuffer *) pc2line));
  42111. line = duk__hobject_pc2line_query_raw((duk_hthread *) ctx, pc2line, (duk_uint_fast32_t) pc);
  42112. } else {
  42113. line = 0;
  42114. }
  42115. duk_pop(ctx);
  42116. return line;
  42117. }
  42118. #endif /* DUK_USE_PC2LINE */
  42119. #line 1 "duk_hobject_props.c"
  42120. /*
  42121. * Hobject property set/get functionality.
  42122. *
  42123. * This is very central functionality for size, performance, and compliance.
  42124. * It is also rather intricate; see hobject-algorithms.rst for discussion on
  42125. * the algorithms and memory-management.rst for discussion on refcounts and
  42126. * side effect issues.
  42127. *
  42128. * Notes:
  42129. *
  42130. * - It might be tempting to assert "refcount nonzero" for objects
  42131. * being operated on, but that's not always correct: objects with
  42132. * a zero refcount may be operated on by the refcount implementation
  42133. * (finalization) for instance. Hence, no refcount assertions are made.
  42134. *
  42135. * - Many operations (memory allocation, identifier operations, etc)
  42136. * may cause arbitrary side effects (e.g. through GC and finalization).
  42137. * These side effects may invalidate duk_tval pointers which point to
  42138. * areas subject to reallocation (like value stack). Heap objects
  42139. * themselves have stable pointers. Holding heap object pointers or
  42140. * duk_tval copies is not problematic with respect to side effects;
  42141. * care must be taken when holding and using argument duk_tval pointers.
  42142. *
  42143. * - If a finalizer is executed, it may operate on the the same object
  42144. * we're currently dealing with. For instance, the finalizer might
  42145. * delete a certain property which has already been looked up and
  42146. * confirmed to exist. Ideally finalizers would be disabled if GC
  42147. * happens during property access. At the moment property table realloc
  42148. * disables finalizers, and all DECREFs may cause arbitrary changes so
  42149. * handle DECREF carefully.
  42150. *
  42151. * - The order of operations for a DECREF matters. When DECREF is executed,
  42152. * the entire object graph must be consistent; note that a refzero may
  42153. * lead to a mark-and-sweep through a refcount finalizer.
  42154. */
  42155. /*
  42156. * XXX: array indices are mostly typed as duk_uint32_t here; duk_uarridx_t
  42157. * might be more appropriate.
  42158. */
  42159. /*
  42160. * XXX: duk_uint_fast32_t should probably be used in many places here.
  42161. */
  42162. /* include removed: duk_internal.h */
  42163. /*
  42164. * Local defines
  42165. */
  42166. #define DUK__NO_ARRAY_INDEX DUK_HSTRING_NO_ARRAY_INDEX
  42167. /* hash probe sequence */
  42168. #define DUK__HASH_INITIAL(hash,h_size) DUK_HOBJECT_HASH_INITIAL((hash),(h_size))
  42169. #define DUK__HASH_PROBE_STEP(hash) DUK_HOBJECT_HASH_PROBE_STEP((hash))
  42170. /* marker values for hash part */
  42171. #define DUK__HASH_UNUSED DUK_HOBJECT_HASHIDX_UNUSED
  42172. #define DUK__HASH_DELETED DUK_HOBJECT_HASHIDX_DELETED
  42173. /* valstack space that suffices for all local calls, including recursion
  42174. * of other than Duktape calls (getters etc)
  42175. */
  42176. #define DUK__VALSTACK_SPACE 10
  42177. /* valstack space allocated especially for proxy lookup which does a
  42178. * recursive property lookup
  42179. */
  42180. #define DUK__VALSTACK_PROXY_LOOKUP 20
  42181. /*
  42182. * Local prototypes
  42183. */
  42184. #define DUK__DESC_FLAG_PUSH_VALUE (1 << 0) /* push value to stack */
  42185. #define DUK__DESC_FLAG_IGNORE_PROTOLOOP (1 << 1) /* don't throw for prototype loop */
  42186. 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);
  42187. 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);
  42188. DUK_LOCAL_DECL void duk__check_arguments_map_for_delete(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *temp_desc);
  42189. 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);
  42190. DUK_LOCAL_DECL duk_bool_t duk__handle_put_array_length(duk_hthread *thr, duk_hobject *obj);
  42191. DUK_LOCAL_DECL duk_bool_t duk__get_property_desc(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *out_desc, duk_small_uint_t flags);
  42192. DUK_LOCAL_DECL duk_bool_t duk__get_own_property_desc_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_uint32_t arr_idx, duk_propdesc *out_desc, duk_small_uint_t flags);
  42193. DUK_LOCAL_DECL duk_bool_t duk__get_own_property_desc(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *out_desc, duk_small_uint_t flags);
  42194. DUK_LOCAL duk_uint32_t duk__get_old_array_length(duk_hthread *thr, duk_hobject *obj, duk_propdesc *temp_desc);
  42195. /*
  42196. * Misc helpers
  42197. */
  42198. /* Convert a duk_tval number (caller checks) to a 32-bit index. Returns
  42199. * DUK__NO_ARRAY_INDEX if the number is not whole or not a valid array
  42200. * index.
  42201. */
  42202. /* XXX: for fastints, could use a variant which assumes a double duk_tval
  42203. * (and doesn't need to check for fastint again).
  42204. */
  42205. DUK_LOCAL duk_uint32_t duk__tval_number_to_arr_idx(duk_tval *tv) {
  42206. duk_double_t dbl;
  42207. duk_uint32_t idx;
  42208. DUK_ASSERT(tv != NULL);
  42209. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  42210. dbl = DUK_TVAL_GET_NUMBER(tv);
  42211. idx = (duk_uint32_t) dbl;
  42212. if ((duk_double_t) idx == dbl) {
  42213. /* Is whole and within 32 bit range. If the value happens to be 0xFFFFFFFF,
  42214. * it's not a valid array index but will then match DUK__NO_ARRAY_INDEX.
  42215. */
  42216. return idx;
  42217. }
  42218. return DUK__NO_ARRAY_INDEX;
  42219. }
  42220. #if defined(DUK_USE_FASTINT)
  42221. /* Convert a duk_tval fastint (caller checks) to a 32-bit index. */
  42222. DUK_LOCAL duk_uint32_t duk__tval_fastint_to_arr_idx(duk_tval *tv) {
  42223. duk_int64_t t;
  42224. DUK_ASSERT(tv != NULL);
  42225. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv));
  42226. t = DUK_TVAL_GET_FASTINT(tv);
  42227. if ((t & ~0xffffffffULL) != 0) {
  42228. /* Catches >0x100000000 and negative values. */
  42229. return DUK__NO_ARRAY_INDEX;
  42230. }
  42231. /* If the value happens to be 0xFFFFFFFF, it's not a valid array index
  42232. * but will then match DUK__NO_ARRAY_INDEX.
  42233. */
  42234. return (duk_uint32_t) t;
  42235. }
  42236. #endif /* DUK_USE_FASTINT */
  42237. /* Push an arbitrary duk_tval to the stack, coerce it to string, and return
  42238. * both a duk_hstring pointer and an array index (or DUK__NO_ARRAY_INDEX).
  42239. */
  42240. DUK_LOCAL duk_uint32_t duk__push_tval_to_hstring_arr_idx(duk_context *ctx, duk_tval *tv, duk_hstring **out_h) {
  42241. duk_uint32_t arr_idx;
  42242. duk_hstring *h;
  42243. DUK_ASSERT(ctx != NULL);
  42244. DUK_ASSERT(tv != NULL);
  42245. DUK_ASSERT(out_h != NULL);
  42246. duk_push_tval(ctx, tv);
  42247. duk_to_string(ctx, -1);
  42248. h = duk_get_hstring(ctx, -1);
  42249. DUK_ASSERT(h != NULL);
  42250. *out_h = h;
  42251. arr_idx = DUK_HSTRING_GET_ARRIDX_FAST(h);
  42252. return arr_idx;
  42253. }
  42254. /* String is an own (virtual) property of a lightfunc. */
  42255. DUK_LOCAL duk_bool_t duk__key_is_lightfunc_ownprop(duk_hthread *thr, duk_hstring *key) {
  42256. return (key == DUK_HTHREAD_STRING_LENGTH(thr) ||
  42257. key == DUK_HTHREAD_STRING_NAME(thr));
  42258. }
  42259. /*
  42260. * Helpers for managing property storage size
  42261. */
  42262. /* Get default hash part size for a certain entry part size. */
  42263. #if defined(DUK_USE_HOBJECT_HASH_PART)
  42264. DUK_LOCAL duk_uint32_t duk__get_default_h_size(duk_uint32_t e_size) {
  42265. DUK_ASSERT(e_size <= DUK_HOBJECT_MAX_PROPERTIES);
  42266. if (e_size >= DUK_HOBJECT_E_USE_HASH_LIMIT) {
  42267. duk_uint32_t res;
  42268. /* result: hash_prime(floor(1.2 * e_size)) */
  42269. res = duk_util_get_hash_prime(e_size + e_size / DUK_HOBJECT_H_SIZE_DIVISOR);
  42270. /* if fails, e_size will be zero = not an issue, except performance-wise */
  42271. DUK_ASSERT(res == 0 || res > e_size);
  42272. return res;
  42273. } else {
  42274. return 0;
  42275. }
  42276. }
  42277. #endif /* USE_PROP_HASH_PART */
  42278. /* Get minimum entry part growth for a certain size. */
  42279. DUK_LOCAL duk_uint32_t duk__get_min_grow_e(duk_uint32_t e_size) {
  42280. duk_uint32_t res;
  42281. DUK_ASSERT(e_size <= DUK_HOBJECT_MAX_PROPERTIES);
  42282. res = (e_size + DUK_HOBJECT_E_MIN_GROW_ADD) / DUK_HOBJECT_E_MIN_GROW_DIVISOR;
  42283. DUK_ASSERT(res >= 1); /* important for callers */
  42284. return res;
  42285. }
  42286. /* Get minimum array part growth for a certain size. */
  42287. DUK_LOCAL duk_uint32_t duk__get_min_grow_a(duk_uint32_t a_size) {
  42288. duk_uint32_t res;
  42289. DUK_ASSERT((duk_size_t) a_size <= DUK_HOBJECT_MAX_PROPERTIES);
  42290. res = (a_size + DUK_HOBJECT_A_MIN_GROW_ADD) / DUK_HOBJECT_A_MIN_GROW_DIVISOR;
  42291. DUK_ASSERT(res >= 1); /* important for callers */
  42292. return res;
  42293. }
  42294. /* Count actually used entry part entries (non-NULL keys). */
  42295. DUK_LOCAL duk_uint32_t duk__count_used_e_keys(duk_hthread *thr, duk_hobject *obj) {
  42296. duk_uint_fast32_t i;
  42297. duk_uint_fast32_t n = 0;
  42298. duk_hstring **e;
  42299. DUK_ASSERT(obj != NULL);
  42300. DUK_UNREF(thr);
  42301. e = DUK_HOBJECT_E_GET_KEY_BASE(thr->heap, obj);
  42302. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  42303. if (*e++) {
  42304. n++;
  42305. }
  42306. }
  42307. return (duk_uint32_t) n;
  42308. }
  42309. /* Count actually used array part entries and array minimum size.
  42310. * NOTE: 'out_min_size' can be computed much faster by starting from the
  42311. * end and breaking out early when finding first used entry, but this is
  42312. * not needed now.
  42313. */
  42314. DUK_LOCAL void duk__compute_a_stats(duk_hthread *thr, duk_hobject *obj, duk_uint32_t *out_used, duk_uint32_t *out_min_size) {
  42315. duk_uint_fast32_t i;
  42316. duk_uint_fast32_t used = 0;
  42317. duk_uint_fast32_t highest_idx = (duk_uint_fast32_t) -1; /* see below */
  42318. duk_tval *a;
  42319. DUK_ASSERT(obj != NULL);
  42320. DUK_ASSERT(out_used != NULL);
  42321. DUK_ASSERT(out_min_size != NULL);
  42322. DUK_UNREF(thr);
  42323. a = DUK_HOBJECT_A_GET_BASE(thr->heap, obj);
  42324. for (i = 0; i < DUK_HOBJECT_GET_ASIZE(obj); i++) {
  42325. duk_tval *tv = a++;
  42326. if (!DUK_TVAL_IS_UNUSED(tv)) {
  42327. used++;
  42328. highest_idx = i;
  42329. }
  42330. }
  42331. /* Initial value for highest_idx is -1 coerced to unsigned. This
  42332. * is a bit odd, but (highest_idx + 1) will then wrap to 0 below
  42333. * for out_min_size as intended.
  42334. */
  42335. *out_used = used;
  42336. *out_min_size = highest_idx + 1; /* 0 if no used entries */
  42337. }
  42338. /* Check array density and indicate whether or not the array part should be abandoned. */
  42339. DUK_LOCAL duk_bool_t duk__abandon_array_density_check(duk_uint32_t a_used, duk_uint32_t a_size) {
  42340. /*
  42341. * Array abandon check; abandon if:
  42342. *
  42343. * new_used / new_size < limit
  42344. * new_used < limit * new_size || limit is 3 bits fixed point
  42345. * new_used < limit' / 8 * new_size || *8
  42346. * 8*new_used < limit' * new_size || :8
  42347. * new_used < limit' * (new_size / 8)
  42348. *
  42349. * Here, new_used = a_used, new_size = a_size.
  42350. *
  42351. * Note: some callers use approximate values for a_used and/or a_size
  42352. * (e.g. dropping a '+1' term). This doesn't affect the usefulness
  42353. * of the check, but may confuse debugging.
  42354. */
  42355. return (a_used < DUK_HOBJECT_A_ABANDON_LIMIT * (a_size >> 3));
  42356. }
  42357. /* Fast check for extending array: check whether or not a slow density check is required. */
  42358. DUK_LOCAL duk_bool_t duk__abandon_array_slow_check_required(duk_uint32_t arr_idx, duk_uint32_t old_size) {
  42359. /*
  42360. * In a fast check we assume old_size equals old_used (i.e., existing
  42361. * array is fully dense).
  42362. *
  42363. * Slow check if:
  42364. *
  42365. * (new_size - old_size) / old_size > limit
  42366. * new_size - old_size > limit * old_size
  42367. * new_size > (1 + limit) * old_size || limit' is 3 bits fixed point
  42368. * new_size > (1 + (limit' / 8)) * old_size || * 8
  42369. * 8 * new_size > (8 + limit') * old_size || : 8
  42370. * new_size > (8 + limit') * (old_size / 8)
  42371. * new_size > limit'' * (old_size / 8) || limit'' = 9 -> max 25% increase
  42372. * arr_idx + 1 > limit'' * (old_size / 8)
  42373. *
  42374. * This check doesn't work well for small values, so old_size is rounded
  42375. * up for the check (and the '+ 1' of arr_idx can be ignored in practice):
  42376. *
  42377. * arr_idx > limit'' * ((old_size + 7) / 8)
  42378. */
  42379. return (arr_idx > DUK_HOBJECT_A_FAST_RESIZE_LIMIT * ((old_size + 7) >> 3));
  42380. }
  42381. /*
  42382. * Proxy helpers
  42383. */
  42384. #if defined(DUK_USE_ES6_PROXY)
  42385. DUK_INTERNAL duk_bool_t duk_hobject_proxy_check(duk_hthread *thr, duk_hobject *obj, duk_hobject **out_target, duk_hobject **out_handler) {
  42386. duk_tval *tv_target;
  42387. duk_tval *tv_handler;
  42388. duk_hobject *h_target;
  42389. duk_hobject *h_handler;
  42390. DUK_ASSERT(thr != NULL);
  42391. DUK_ASSERT(obj != NULL);
  42392. DUK_ASSERT(out_target != NULL);
  42393. DUK_ASSERT(out_handler != NULL);
  42394. /* Caller doesn't need to check exotic proxy behavior (but does so for
  42395. * some fast paths).
  42396. */
  42397. if (DUK_LIKELY(!DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(obj))) {
  42398. return 0;
  42399. }
  42400. tv_handler = duk_hobject_find_existing_entry_tval_ptr(thr->heap, obj, DUK_HTHREAD_STRING_INT_HANDLER(thr));
  42401. if (!tv_handler) {
  42402. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROXY_REVOKED);
  42403. return 0;
  42404. }
  42405. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv_handler));
  42406. h_handler = DUK_TVAL_GET_OBJECT(tv_handler);
  42407. DUK_ASSERT(h_handler != NULL);
  42408. *out_handler = h_handler;
  42409. tv_handler = NULL; /* avoid issues with relocation */
  42410. tv_target = duk_hobject_find_existing_entry_tval_ptr(thr->heap, obj, DUK_HTHREAD_STRING_INT_TARGET(thr));
  42411. if (!tv_target) {
  42412. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROXY_REVOKED);
  42413. return 0;
  42414. }
  42415. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv_target));
  42416. h_target = DUK_TVAL_GET_OBJECT(tv_target);
  42417. DUK_ASSERT(h_target != NULL);
  42418. *out_target = h_target;
  42419. tv_target = NULL; /* avoid issues with relocation */
  42420. return 1;
  42421. }
  42422. #endif /* DUK_USE_ES6_PROXY */
  42423. /* Get Proxy target object. If the argument is not a Proxy, return it as is.
  42424. * If a Proxy is revoked, an error is thrown.
  42425. */
  42426. #if defined(DUK_USE_ES6_PROXY)
  42427. DUK_INTERNAL duk_hobject *duk_hobject_resolve_proxy_target(duk_hthread *thr, duk_hobject *obj) {
  42428. duk_hobject *h_target;
  42429. duk_hobject *h_handler;
  42430. DUK_ASSERT(thr != NULL);
  42431. DUK_ASSERT(obj != NULL);
  42432. /* Resolve Proxy targets until Proxy chain ends. No explicit check for
  42433. * a Proxy loop: user code cannot create such a loop without tweaking
  42434. * internal properties directly.
  42435. */
  42436. while (DUK_UNLIKELY(DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(obj))) {
  42437. if (duk_hobject_proxy_check(thr, obj, &h_target, &h_handler)) {
  42438. DUK_ASSERT(h_target != NULL);
  42439. obj = h_target;
  42440. } else {
  42441. break;
  42442. }
  42443. }
  42444. DUK_ASSERT(obj != NULL);
  42445. return obj;
  42446. }
  42447. #endif /* DUK_USE_ES6_PROXY */
  42448. #if defined(DUK_USE_ES6_PROXY)
  42449. 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) {
  42450. duk_context *ctx = (duk_context *) thr;
  42451. duk_hobject *h_handler;
  42452. DUK_ASSERT(thr != NULL);
  42453. DUK_ASSERT(obj != NULL);
  42454. DUK_ASSERT(tv_key != NULL);
  42455. DUK_ASSERT(out_target != NULL);
  42456. if (!duk_hobject_proxy_check(thr, obj, out_target, &h_handler)) {
  42457. return 0;
  42458. }
  42459. DUK_ASSERT(*out_target != NULL);
  42460. DUK_ASSERT(h_handler != NULL);
  42461. /* XXX: At the moment Duktape accesses internal keys like _Finalizer using a
  42462. * normal property set/get which would allow a proxy handler to interfere with
  42463. * such behavior and to get access to internal key strings. This is not a problem
  42464. * as such because internal key strings can be created in other ways too (e.g.
  42465. * through buffers). The best fix is to change Duktape internal lookups to
  42466. * skip proxy behavior. Until that, internal property accesses bypass the
  42467. * proxy and are applied to the target (as if the handler did not exist).
  42468. * This has some side effects, see test-bi-proxy-internal-keys.js.
  42469. */
  42470. if (DUK_TVAL_IS_STRING(tv_key)) {
  42471. duk_hstring *h_key = (duk_hstring *) DUK_TVAL_GET_STRING(tv_key);
  42472. DUK_ASSERT(h_key != NULL);
  42473. if (DUK_HSTRING_HAS_INTERNAL(h_key)) {
  42474. DUK_DDD(DUK_DDDPRINT("internal key, skip proxy handler and apply to target"));
  42475. return 0;
  42476. }
  42477. }
  42478. /* The handler is looked up with a normal property lookup; it may be an
  42479. * accessor or the handler object itself may be a proxy object. If the
  42480. * handler is a proxy, we need to extend the valstack as we make a
  42481. * recursive proxy check without a function call in between (in fact
  42482. * there is no limit to the potential recursion here).
  42483. *
  42484. * (For sanity, proxy creation rejects another proxy object as either
  42485. * the handler or the target at the moment so recursive proxy cases
  42486. * are not realized now.)
  42487. */
  42488. /* XXX: C recursion limit if proxies are allowed as handler/target values */
  42489. duk_require_stack(ctx, DUK__VALSTACK_PROXY_LOOKUP);
  42490. duk_push_hobject(ctx, h_handler);
  42491. if (duk_get_prop_stridx(ctx, -1, stridx_trap)) {
  42492. /* -> [ ... handler trap ] */
  42493. duk_insert(ctx, -2); /* -> [ ... trap handler ] */
  42494. /* stack prepped for func call: [ ... trap handler ] */
  42495. return 1;
  42496. } else {
  42497. duk_pop_2(ctx);
  42498. return 0;
  42499. }
  42500. }
  42501. #endif /* DUK_USE_ES6_PROXY */
  42502. /*
  42503. * Reallocate property allocation, moving properties to the new allocation.
  42504. *
  42505. * Includes key compaction, rehashing, and can also optionally abandoning
  42506. * the array part, 'migrating' array entries into the beginning of the
  42507. * new entry part. Arguments are not validated here, so e.g. new_h_size
  42508. * MUST be a valid prime.
  42509. *
  42510. * There is no support for in-place reallocation or just compacting keys
  42511. * without resizing the property allocation. This is intentional to keep
  42512. * code size minimal.
  42513. *
  42514. * The implementation is relatively straightforward, except for the array
  42515. * abandonment process. Array abandonment requires that new string keys
  42516. * are interned, which may trigger GC. All keys interned so far must be
  42517. * reachable for GC at all times; valstack is used for that now.
  42518. *
  42519. * Also, a GC triggered during this reallocation process must not interfere
  42520. * with the object being resized. This is currently controlled by using
  42521. * heap->mark_and_sweep_base_flags to indicate that no finalizers will be
  42522. * executed (as they can affect ANY object) and no objects are compacted
  42523. * (it would suffice to protect this particular object only, though).
  42524. *
  42525. * Note: a non-checked variant would be nice but is a bit tricky to
  42526. * implement for the array abandonment process. It's easy for
  42527. * everything else.
  42528. *
  42529. * Note: because we need to potentially resize the valstack (as part
  42530. * of abandoning the array part), any tval pointers to the valstack
  42531. * will become invalid after this call.
  42532. */
  42533. DUK_LOCAL
  42534. void duk__realloc_props(duk_hthread *thr,
  42535. duk_hobject *obj,
  42536. duk_uint32_t new_e_size,
  42537. duk_uint32_t new_a_size,
  42538. duk_uint32_t new_h_size,
  42539. duk_bool_t abandon_array) {
  42540. duk_context *ctx = (duk_context *) thr;
  42541. #ifdef DUK_USE_MARK_AND_SWEEP
  42542. duk_small_uint_t prev_mark_and_sweep_base_flags;
  42543. #endif
  42544. duk_uint32_t new_alloc_size;
  42545. duk_uint32_t new_e_size_adjusted;
  42546. duk_uint8_t *new_p;
  42547. duk_hstring **new_e_k;
  42548. duk_propvalue *new_e_pv;
  42549. duk_uint8_t *new_e_f;
  42550. duk_tval *new_a;
  42551. duk_uint32_t *new_h;
  42552. duk_uint32_t new_e_next;
  42553. duk_uint_fast32_t i;
  42554. DUK_ASSERT(thr != NULL);
  42555. DUK_ASSERT(ctx != NULL);
  42556. DUK_ASSERT(obj != NULL);
  42557. DUK_ASSERT(!abandon_array || new_a_size == 0); /* if abandon_array, new_a_size must be 0 */
  42558. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, obj) != NULL || (DUK_HOBJECT_GET_ESIZE(obj) == 0 && DUK_HOBJECT_GET_ASIZE(obj) == 0));
  42559. DUK_ASSERT(new_h_size == 0 || new_h_size >= new_e_size); /* required to guarantee success of rehashing,
  42560. * intentionally use unadjusted new_e_size
  42561. */
  42562. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  42563. /*
  42564. * Pre resize assertions.
  42565. */
  42566. #ifdef DUK_USE_ASSERTIONS
  42567. /* XXX: pre-checks (such as no duplicate keys) */
  42568. #endif
  42569. /*
  42570. * For property layout 1, tweak e_size to ensure that the whole entry
  42571. * part (key + val + flags) is a suitable multiple for alignment
  42572. * (platform specific).
  42573. *
  42574. * Property layout 2 does not require this tweaking and is preferred
  42575. * on low RAM platforms requiring alignment.
  42576. */
  42577. #if defined(DUK_USE_HOBJECT_LAYOUT_2) || defined(DUK_USE_HOBJECT_LAYOUT_3)
  42578. DUK_DDD(DUK_DDDPRINT("using layout 2 or 3, no need to pad e_size: %ld", (long) new_e_size));
  42579. new_e_size_adjusted = new_e_size;
  42580. #elif defined(DUK_USE_HOBJECT_LAYOUT_1) && (DUK_HOBJECT_ALIGN_TARGET == 1)
  42581. DUK_DDD(DUK_DDDPRINT("using layout 1, but no need to pad e_size: %ld", (long) new_e_size));
  42582. new_e_size_adjusted = new_e_size;
  42583. #elif defined(DUK_USE_HOBJECT_LAYOUT_1) && ((DUK_HOBJECT_ALIGN_TARGET == 4) || (DUK_HOBJECT_ALIGN_TARGET == 8))
  42584. new_e_size_adjusted = (new_e_size + DUK_HOBJECT_ALIGN_TARGET - 1) & (~(DUK_HOBJECT_ALIGN_TARGET - 1));
  42585. DUK_DDD(DUK_DDDPRINT("using layout 1, and alignment target is %ld, adjusted e_size: %ld -> %ld",
  42586. (long) DUK_HOBJECT_ALIGN_TARGET, (long) new_e_size, (long) new_e_size_adjusted));
  42587. DUK_ASSERT(new_e_size_adjusted >= new_e_size);
  42588. #else
  42589. #error invalid hobject layout defines
  42590. #endif
  42591. /*
  42592. * Debug logging after adjustment.
  42593. */
  42594. 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 "
  42595. "{e_size=%ld,a_size=%ld,h_size=%ld}, abandon_array=%ld, unadjusted new_e_size=%ld",
  42596. (void *) obj,
  42597. (long) DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE(obj),
  42598. DUK_HOBJECT_GET_ASIZE(obj),
  42599. DUK_HOBJECT_GET_HSIZE(obj)),
  42600. (long) DUK_HOBJECT_P_COMPUTE_SIZE(new_e_size_adjusted, new_a_size, new_h_size),
  42601. (void *) DUK_HOBJECT_GET_PROPS(thr->heap, obj),
  42602. (long) DUK_HOBJECT_GET_ESIZE(obj),
  42603. (long) DUK_HOBJECT_GET_ENEXT(obj),
  42604. (long) DUK_HOBJECT_GET_ASIZE(obj),
  42605. (long) DUK_HOBJECT_GET_HSIZE(obj),
  42606. (long) new_e_size_adjusted,
  42607. (long) new_a_size,
  42608. (long) new_h_size,
  42609. (long) abandon_array,
  42610. (long) new_e_size));
  42611. /*
  42612. * Property count check. This is the only point where we ensure that
  42613. * we don't get more (allocated) property space that we can handle.
  42614. * There aren't hard limits as such, but some algorithms fail (e.g.
  42615. * finding next higher prime, selecting hash part size) if we get too
  42616. * close to the 4G property limit.
  42617. *
  42618. * Since this works based on allocation size (not actually used size),
  42619. * the limit is a bit approximate but good enough in practice.
  42620. */
  42621. if (new_e_size_adjusted + new_a_size > DUK_HOBJECT_MAX_PROPERTIES) {
  42622. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_OBJECT_PROPERTY_LIMIT);
  42623. }
  42624. /*
  42625. * Compute new alloc size and alloc new area.
  42626. *
  42627. * The new area is allocated as a dynamic buffer and placed into the
  42628. * valstack for reachability. The actual buffer is then detached at
  42629. * the end.
  42630. *
  42631. * Note: heap_mark_and_sweep_base_flags are altered here to ensure
  42632. * no-one touches this object while we're resizing and rehashing it.
  42633. * The flags must be reset on every exit path after it. Finalizers
  42634. * and compaction is prevented currently for all objects while it
  42635. * would be enough to restrict it only for the current object.
  42636. */
  42637. #ifdef DUK_USE_MARK_AND_SWEEP
  42638. prev_mark_and_sweep_base_flags = thr->heap->mark_and_sweep_base_flags;
  42639. thr->heap->mark_and_sweep_base_flags |=
  42640. DUK_MS_FLAG_NO_FINALIZERS | /* avoid attempts to add/remove object keys */
  42641. DUK_MS_FLAG_NO_OBJECT_COMPACTION; /* avoid attempt to compact the current object */
  42642. #endif
  42643. new_alloc_size = DUK_HOBJECT_P_COMPUTE_SIZE(new_e_size_adjusted, new_a_size, new_h_size);
  42644. DUK_DDD(DUK_DDDPRINT("new hobject allocation size is %ld", (long) new_alloc_size));
  42645. if (new_alloc_size == 0) {
  42646. /* for zero size, don't push anything on valstack */
  42647. DUK_ASSERT(new_e_size_adjusted == 0);
  42648. DUK_ASSERT(new_a_size == 0);
  42649. DUK_ASSERT(new_h_size == 0);
  42650. new_p = NULL;
  42651. } else {
  42652. /* This may trigger mark-and-sweep with arbitrary side effects,
  42653. * including an attempted resize of the object we're resizing,
  42654. * executing a finalizer which may add or remove properties of
  42655. * the object we're resizing etc.
  42656. */
  42657. /* Note: buffer is dynamic so that we can 'steal' the actual
  42658. * allocation later.
  42659. */
  42660. new_p = (duk_uint8_t *) duk_push_dynamic_buffer(ctx, new_alloc_size); /* errors out if out of memory */
  42661. DUK_ASSERT(new_p != NULL); /* since new_alloc_size > 0 */
  42662. }
  42663. /* Set up pointers to the new property area: this is hidden behind a macro
  42664. * because it is memory layout specific.
  42665. */
  42666. DUK_HOBJECT_P_SET_REALLOC_PTRS(new_p, new_e_k, new_e_pv, new_e_f, new_a, new_h,
  42667. new_e_size_adjusted, new_a_size, new_h_size);
  42668. DUK_UNREF(new_h); /* happens when hash part dropped */
  42669. new_e_next = 0;
  42670. /* if new_p == NULL, all of these pointers are NULL */
  42671. DUK_ASSERT((new_p != NULL) ||
  42672. (new_e_k == NULL && new_e_pv == NULL && new_e_f == NULL &&
  42673. new_a == NULL && new_h == NULL));
  42674. 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",
  42675. (long) new_alloc_size, (void *) new_e_k, (void *) new_e_pv, (void *) new_e_f,
  42676. (void *) new_a, (void *) new_h));
  42677. /*
  42678. * Migrate array to start of entries if requested.
  42679. *
  42680. * Note: from an enumeration perspective the order of entry keys matters.
  42681. * Array keys should appear wherever they appeared before the array abandon
  42682. * operation.
  42683. */
  42684. if (abandon_array) {
  42685. /*
  42686. * Note: assuming new_a_size == 0, and that entry part contains
  42687. * no conflicting keys, refcounts do not need to be adjusted for
  42688. * the values, as they remain exactly the same.
  42689. *
  42690. * The keys, however, need to be interned, incref'd, and be
  42691. * reachable for GC. Any intern attempt may trigger a GC and
  42692. * claim any non-reachable strings, so every key must be reachable
  42693. * at all times.
  42694. *
  42695. * A longjmp must not occur here, as the new_p allocation would
  42696. * be freed without these keys being decref'd, hence the messy
  42697. * decref handling if intern fails.
  42698. */
  42699. DUK_ASSERT(new_a_size == 0);
  42700. for (i = 0; i < DUK_HOBJECT_GET_ASIZE(obj); i++) {
  42701. duk_tval *tv1;
  42702. duk_tval *tv2;
  42703. duk_hstring *key;
  42704. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, obj) != NULL);
  42705. tv1 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, i);
  42706. if (DUK_TVAL_IS_UNUSED(tv1)) {
  42707. continue;
  42708. }
  42709. DUK_ASSERT(new_p != NULL && new_e_k != NULL &&
  42710. new_e_pv != NULL && new_e_f != NULL);
  42711. /*
  42712. * Intern key via the valstack to ensure reachability behaves
  42713. * properly. We must avoid longjmp's here so use non-checked
  42714. * primitives.
  42715. *
  42716. * Note: duk_check_stack() potentially reallocs the valstack,
  42717. * invalidating any duk_tval pointers to valstack. Callers
  42718. * must be careful.
  42719. */
  42720. /* never shrinks; auto-adds DUK_VALSTACK_INTERNAL_EXTRA, which is generous */
  42721. if (!duk_check_stack(ctx, 1)) {
  42722. goto abandon_error;
  42723. }
  42724. DUK_ASSERT_VALSTACK_SPACE(thr, 1);
  42725. key = duk_heap_string_intern_u32(thr->heap, i);
  42726. if (!key) {
  42727. goto abandon_error;
  42728. }
  42729. duk_push_hstring(ctx, key); /* keep key reachable for GC etc; guaranteed not to fail */
  42730. /* key is now reachable in the valstack */
  42731. DUK_HSTRING_INCREF(thr, key); /* second incref for the entry reference */
  42732. new_e_k[new_e_next] = key;
  42733. tv2 = &new_e_pv[new_e_next].v; /* array entries are all plain values */
  42734. DUK_TVAL_SET_TVAL(tv2, tv1);
  42735. new_e_f[new_e_next] = DUK_PROPDESC_FLAG_WRITABLE |
  42736. DUK_PROPDESC_FLAG_ENUMERABLE |
  42737. DUK_PROPDESC_FLAG_CONFIGURABLE;
  42738. new_e_next++;
  42739. /* Note: new_e_next matches pushed temp key count, and nothing can
  42740. * fail above between the push and this point.
  42741. */
  42742. }
  42743. DUK_DDD(DUK_DDDPRINT("abandon array: pop %ld key temps from valstack", (long) new_e_next));
  42744. duk_pop_n(ctx, new_e_next);
  42745. }
  42746. /*
  42747. * Copy keys and values in the entry part (compacting them at the same time).
  42748. */
  42749. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  42750. duk_hstring *key;
  42751. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, obj) != NULL);
  42752. key = DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i);
  42753. if (!key) {
  42754. continue;
  42755. }
  42756. DUK_ASSERT(new_p != NULL && new_e_k != NULL &&
  42757. new_e_pv != NULL && new_e_f != NULL);
  42758. new_e_k[new_e_next] = key;
  42759. new_e_pv[new_e_next] = DUK_HOBJECT_E_GET_VALUE(thr->heap, obj, i);
  42760. new_e_f[new_e_next] = DUK_HOBJECT_E_GET_FLAGS(thr->heap, obj, i);
  42761. new_e_next++;
  42762. }
  42763. /* the entries [new_e_next, new_e_size_adjusted[ are left uninitialized on purpose (ok, not gc reachable) */
  42764. /*
  42765. * Copy array elements to new array part.
  42766. */
  42767. if (new_a_size > DUK_HOBJECT_GET_ASIZE(obj)) {
  42768. /* copy existing entries as is */
  42769. DUK_ASSERT(new_p != NULL && new_a != NULL);
  42770. if (DUK_HOBJECT_GET_ASIZE(obj) > 0) {
  42771. /* Avoid zero copy with an invalid pointer. If obj->p is NULL,
  42772. * the 'new_a' pointer will be invalid which is not allowed even
  42773. * when copy size is zero.
  42774. */
  42775. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, obj) != NULL);
  42776. DUK_ASSERT(DUK_HOBJECT_GET_ASIZE(obj) > 0);
  42777. DUK_MEMCPY((void *) new_a, (void *) DUK_HOBJECT_A_GET_BASE(thr->heap, obj), sizeof(duk_tval) * DUK_HOBJECT_GET_ASIZE(obj));
  42778. }
  42779. /* fill new entries with -unused- (required, gc reachable) */
  42780. for (i = DUK_HOBJECT_GET_ASIZE(obj); i < new_a_size; i++) {
  42781. duk_tval *tv = &new_a[i];
  42782. DUK_TVAL_SET_UNUSED(tv);
  42783. }
  42784. } else {
  42785. #ifdef DUK_USE_ASSERTIONS
  42786. /* caller must have decref'd values above new_a_size (if that is necessary) */
  42787. if (!abandon_array) {
  42788. for (i = new_a_size; i < DUK_HOBJECT_GET_ASIZE(obj); i++) {
  42789. duk_tval *tv;
  42790. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, i);
  42791. /* current assertion is quite strong: decref's and set to unused */
  42792. DUK_ASSERT(DUK_TVAL_IS_UNUSED(tv));
  42793. }
  42794. }
  42795. #endif
  42796. if (new_a_size > 0) {
  42797. /* Avoid zero copy with an invalid pointer. If obj->p is NULL,
  42798. * the 'new_a' pointer will be invalid which is not allowed even
  42799. * when copy size is zero.
  42800. */
  42801. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, obj) != NULL);
  42802. DUK_ASSERT(new_a_size > 0);
  42803. DUK_MEMCPY((void *) new_a, (void *) DUK_HOBJECT_A_GET_BASE(thr->heap, obj), sizeof(duk_tval) * new_a_size);
  42804. }
  42805. }
  42806. /*
  42807. * Rebuild the hash part always from scratch (guaranteed to finish).
  42808. *
  42809. * Any resize of hash part requires rehashing. In addition, by rehashing
  42810. * get rid of any elements marked deleted (DUK__HASH_DELETED) which is critical
  42811. * to ensuring the hash part never fills up.
  42812. */
  42813. #if defined(DUK_USE_HOBJECT_HASH_PART)
  42814. if (DUK_UNLIKELY(new_h_size > 0)) {
  42815. DUK_ASSERT(new_h != NULL);
  42816. /* fill new_h with u32 0xff = UNUSED */
  42817. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, obj) != NULL);
  42818. DUK_ASSERT(new_h_size > 0);
  42819. DUK_MEMSET(new_h, 0xff, sizeof(duk_uint32_t) * new_h_size);
  42820. DUK_ASSERT(new_e_next <= new_h_size); /* equality not actually possible */
  42821. for (i = 0; i < new_e_next; i++) {
  42822. duk_hstring *key = new_e_k[i];
  42823. duk_uint32_t j, step;
  42824. DUK_ASSERT(key != NULL);
  42825. j = DUK__HASH_INITIAL(DUK_HSTRING_GET_HASH(key), new_h_size);
  42826. step = DUK__HASH_PROBE_STEP(DUK_HSTRING_GET_HASH(key));
  42827. for (;;) {
  42828. DUK_ASSERT(new_h[j] != DUK__HASH_DELETED); /* should never happen */
  42829. if (new_h[j] == DUK__HASH_UNUSED) {
  42830. DUK_DDD(DUK_DDDPRINT("rebuild hit %ld -> %ld", (long) j, (long) i));
  42831. new_h[j] = i;
  42832. break;
  42833. }
  42834. DUK_DDD(DUK_DDDPRINT("rebuild miss %ld, step %ld", (long) j, (long) step));
  42835. j = (j + step) % new_h_size;
  42836. /* guaranteed to finish */
  42837. DUK_ASSERT(j != (duk_uint32_t) DUK__HASH_INITIAL(DUK_HSTRING_GET_HASH(key), new_h_size));
  42838. }
  42839. }
  42840. } else {
  42841. DUK_DDD(DUK_DDDPRINT("no hash part, no rehash"));
  42842. }
  42843. #endif /* DUK_USE_HOBJECT_HASH_PART */
  42844. /*
  42845. * Nice debug log.
  42846. */
  42847. 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 "
  42848. "{p=%p,e_size=%ld,e_next=%ld,a_size=%ld,h_size=%ld}, abandon_array=%ld, unadjusted new_e_size=%ld",
  42849. (void *) obj,
  42850. (long) DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE(obj),
  42851. DUK_HOBJECT_GET_ASIZE(obj),
  42852. DUK_HOBJECT_GET_HSIZE(obj)),
  42853. (long) new_alloc_size,
  42854. (void *) DUK_HOBJECT_GET_PROPS(thr->heap, obj),
  42855. (long) DUK_HOBJECT_GET_ESIZE(obj),
  42856. (long) DUK_HOBJECT_GET_ENEXT(obj),
  42857. (long) DUK_HOBJECT_GET_ASIZE(obj),
  42858. (long) DUK_HOBJECT_GET_HSIZE(obj),
  42859. (void *) new_p,
  42860. (long) new_e_size_adjusted,
  42861. (long) new_e_next,
  42862. (long) new_a_size,
  42863. (long) new_h_size,
  42864. (long) abandon_array,
  42865. (long) new_e_size));
  42866. /*
  42867. * All done, switch properties ('p') allocation to new one.
  42868. */
  42869. DUK_FREE(thr->heap, DUK_HOBJECT_GET_PROPS(thr->heap, obj)); /* NULL obj->p is OK */
  42870. DUK_HOBJECT_SET_PROPS(thr->heap, obj, new_p);
  42871. DUK_HOBJECT_SET_ESIZE(obj, new_e_size_adjusted);
  42872. DUK_HOBJECT_SET_ENEXT(obj, new_e_next);
  42873. DUK_HOBJECT_SET_ASIZE(obj, new_a_size);
  42874. DUK_HOBJECT_SET_HSIZE(obj, new_h_size);
  42875. if (new_p) {
  42876. /*
  42877. * Detach actual buffer from dynamic buffer in valstack, and
  42878. * pop it from the stack.
  42879. *
  42880. * XXX: the buffer object is certainly not reachable at this point,
  42881. * so it would be nice to free it forcibly even with only
  42882. * mark-and-sweep enabled. Not a big issue though.
  42883. */
  42884. (void) duk_steal_buffer(ctx, -1, NULL);
  42885. duk_pop(ctx);
  42886. } else {
  42887. DUK_ASSERT(new_alloc_size == 0);
  42888. /* no need to pop, nothing was pushed */
  42889. }
  42890. /* clear array part flag only after switching */
  42891. if (abandon_array) {
  42892. DUK_HOBJECT_CLEAR_ARRAY_PART(obj);
  42893. }
  42894. DUK_DDD(DUK_DDDPRINT("resize result: %!O", (duk_heaphdr *) obj));
  42895. #ifdef DUK_USE_MARK_AND_SWEEP
  42896. thr->heap->mark_and_sweep_base_flags = prev_mark_and_sweep_base_flags;
  42897. #endif
  42898. /*
  42899. * Post resize assertions.
  42900. */
  42901. #ifdef DUK_USE_ASSERTIONS
  42902. /* XXX: post-checks (such as no duplicate keys) */
  42903. #endif
  42904. return;
  42905. /*
  42906. * Abandon array failed, need to decref keys already inserted
  42907. * into the beginning of new_e_k before unwinding valstack.
  42908. */
  42909. abandon_error:
  42910. DUK_D(DUK_DPRINT("hobject resize failed during abandon array, decref keys"));
  42911. i = new_e_next;
  42912. while (i > 0) {
  42913. i--;
  42914. DUK_ASSERT(new_e_k != NULL);
  42915. DUK_ASSERT(new_e_k[i] != NULL);
  42916. DUK_HSTRING_DECREF(thr, new_e_k[i]); /* side effects */
  42917. }
  42918. #ifdef DUK_USE_MARK_AND_SWEEP
  42919. thr->heap->mark_and_sweep_base_flags = prev_mark_and_sweep_base_flags;
  42920. #endif
  42921. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_OBJECT_RESIZE_FAILED);
  42922. }
  42923. /*
  42924. * Helpers to resize properties allocation on specific needs.
  42925. */
  42926. /* Grow entry part allocation for one additional entry. */
  42927. DUK_LOCAL void duk__grow_props_for_new_entry_item(duk_hthread *thr, duk_hobject *obj) {
  42928. duk_uint32_t old_e_used; /* actually used, non-NULL entries */
  42929. duk_uint32_t new_e_size;
  42930. duk_uint32_t new_a_size;
  42931. duk_uint32_t new_h_size;
  42932. DUK_ASSERT(thr != NULL);
  42933. DUK_ASSERT(obj != NULL);
  42934. /* Duktape 0.11.0 and prior tried to optimize the resize by not
  42935. * counting the number of actually used keys prior to the resize.
  42936. * This worked mostly well but also caused weird leak-like behavior
  42937. * as in: test-bug-object-prop-alloc-unbounded.js. So, now we count
  42938. * the keys explicitly to compute the new entry part size.
  42939. */
  42940. old_e_used = duk__count_used_e_keys(thr, obj);
  42941. new_e_size = old_e_used + duk__get_min_grow_e(old_e_used);
  42942. #if defined(DUK_USE_HOBJECT_HASH_PART)
  42943. new_h_size = duk__get_default_h_size(new_e_size);
  42944. #else
  42945. new_h_size = 0;
  42946. #endif
  42947. new_a_size = DUK_HOBJECT_GET_ASIZE(obj);
  42948. DUK_ASSERT(new_e_size >= old_e_used + 1); /* duk__get_min_grow_e() is always >= 1 */
  42949. duk__realloc_props(thr, obj, new_e_size, new_a_size, new_h_size, 0);
  42950. }
  42951. /* Grow array part for a new highest array index. */
  42952. DUK_LOCAL void duk__grow_props_for_array_item(duk_hthread *thr, duk_hobject *obj, duk_uint32_t highest_arr_idx) {
  42953. duk_uint32_t new_e_size;
  42954. duk_uint32_t new_a_size;
  42955. duk_uint32_t new_h_size;
  42956. DUK_ASSERT(thr != NULL);
  42957. DUK_ASSERT(obj != NULL);
  42958. DUK_ASSERT(highest_arr_idx >= DUK_HOBJECT_GET_ASIZE(obj));
  42959. /* minimum new length is highest_arr_idx + 1 */
  42960. new_e_size = DUK_HOBJECT_GET_ESIZE(obj);
  42961. new_h_size = DUK_HOBJECT_GET_HSIZE(obj);
  42962. new_a_size = highest_arr_idx + duk__get_min_grow_a(highest_arr_idx);
  42963. DUK_ASSERT(new_a_size >= highest_arr_idx + 1); /* duk__get_min_grow_a() is always >= 1 */
  42964. duk__realloc_props(thr, obj, new_e_size, new_a_size, new_h_size, 0);
  42965. }
  42966. /* Abandon array part, moving array entries into entries part.
  42967. * This requires a props resize, which is a heavy operation.
  42968. * We also compact the entries part while we're at it, although
  42969. * this is not strictly required.
  42970. */
  42971. DUK_LOCAL void duk__abandon_array_checked(duk_hthread *thr, duk_hobject *obj) {
  42972. duk_uint32_t new_e_size;
  42973. duk_uint32_t new_a_size;
  42974. duk_uint32_t new_h_size;
  42975. duk_uint32_t e_used; /* actually used, non-NULL keys */
  42976. duk_uint32_t a_used;
  42977. duk_uint32_t a_size;
  42978. DUK_ASSERT(thr != NULL);
  42979. DUK_ASSERT(obj != NULL);
  42980. e_used = duk__count_used_e_keys(thr, obj);
  42981. duk__compute_a_stats(thr, obj, &a_used, &a_size);
  42982. /*
  42983. * Must guarantee all actually used array entries will fit into
  42984. * new entry part. Add one growth step to ensure we don't run out
  42985. * of space right away.
  42986. */
  42987. new_e_size = e_used + a_used;
  42988. new_e_size = new_e_size + duk__get_min_grow_e(new_e_size);
  42989. new_a_size = 0;
  42990. #if defined(DUK_USE_HOBJECT_HASH_PART)
  42991. new_h_size = duk__get_default_h_size(new_e_size);
  42992. #else
  42993. new_h_size = 0;
  42994. #endif
  42995. DUK_DD(DUK_DDPRINT("abandon array part for hobject %p, "
  42996. "array stats before: e_used=%ld, a_used=%ld, a_size=%ld; "
  42997. "resize to e_size=%ld, a_size=%ld, h_size=%ld",
  42998. (void *) obj, (long) e_used, (long) a_used, (long) a_size,
  42999. (long) new_e_size, (long) new_a_size, (long) new_h_size));
  43000. duk__realloc_props(thr, obj, new_e_size, new_a_size, new_h_size, 1);
  43001. }
  43002. /*
  43003. * Compact an object. Minimizes allocation size for objects which are
  43004. * not likely to be extended. This is useful for internal and non-
  43005. * extensible objects, but can also be called for non-extensible objects.
  43006. * May abandon the array part if it is computed to be too sparse.
  43007. *
  43008. * This call is relatively expensive, as it needs to scan both the
  43009. * entries and the array part.
  43010. *
  43011. * The call may fail due to allocation error.
  43012. */
  43013. DUK_INTERNAL void duk_hobject_compact_props(duk_hthread *thr, duk_hobject *obj) {
  43014. duk_uint32_t e_size; /* currently used -> new size */
  43015. duk_uint32_t a_size; /* currently required */
  43016. duk_uint32_t a_used; /* actually used */
  43017. duk_uint32_t h_size;
  43018. duk_bool_t abandon_array;
  43019. DUK_ASSERT(thr != NULL);
  43020. DUK_ASSERT(obj != NULL);
  43021. e_size = duk__count_used_e_keys(thr, obj);
  43022. duk__compute_a_stats(thr, obj, &a_used, &a_size);
  43023. DUK_DD(DUK_DDPRINT("compacting hobject, used e keys %ld, used a keys %ld, min a size %ld, "
  43024. "resized array density would be: %ld/%ld = %lf",
  43025. (long) e_size, (long) a_used, (long) a_size,
  43026. (long) a_used, (long) a_size,
  43027. (double) a_used / (double) a_size));
  43028. if (duk__abandon_array_density_check(a_used, a_size)) {
  43029. DUK_DD(DUK_DDPRINT("decided to abandon array during compaction, a_used=%ld, a_size=%ld",
  43030. (long) a_used, (long) a_size));
  43031. abandon_array = 1;
  43032. e_size += a_used;
  43033. a_size = 0;
  43034. } else {
  43035. DUK_DD(DUK_DDPRINT("decided to keep array during compaction"));
  43036. abandon_array = 0;
  43037. }
  43038. #if defined(DUK_USE_HOBJECT_HASH_PART)
  43039. if (e_size >= DUK_HOBJECT_E_USE_HASH_LIMIT) {
  43040. h_size = duk__get_default_h_size(e_size);
  43041. } else {
  43042. h_size = 0;
  43043. }
  43044. #else
  43045. h_size = 0;
  43046. #endif
  43047. DUK_DD(DUK_DDPRINT("compacting hobject -> new e_size %ld, new a_size=%ld, new h_size=%ld, abandon_array=%ld",
  43048. (long) e_size, (long) a_size, (long) h_size, (long) abandon_array));
  43049. duk__realloc_props(thr, obj, e_size, a_size, h_size, abandon_array);
  43050. }
  43051. /*
  43052. * Find an existing key from entry part either by linear scan or by
  43053. * using the hash index (if it exists).
  43054. *
  43055. * Sets entry index (and possibly the hash index) to output variables,
  43056. * which allows the caller to update the entry and hash entries in-place.
  43057. * If entry is not found, both values are set to -1. If entry is found
  43058. * but there is no hash part, h_idx is set to -1.
  43059. */
  43060. DUK_INTERNAL void duk_hobject_find_existing_entry(duk_heap *heap, duk_hobject *obj, duk_hstring *key, duk_int_t *e_idx, duk_int_t *h_idx) {
  43061. DUK_ASSERT(obj != NULL);
  43062. DUK_ASSERT(key != NULL);
  43063. DUK_ASSERT(e_idx != NULL);
  43064. DUK_ASSERT(h_idx != NULL);
  43065. DUK_UNREF(heap);
  43066. if (DUK_LIKELY(DUK_HOBJECT_GET_HSIZE(obj) == 0))
  43067. {
  43068. /* Linear scan: more likely because most objects are small.
  43069. * This is an important fast path.
  43070. *
  43071. * XXX: this might be worth inlining for property lookups.
  43072. */
  43073. duk_uint_fast32_t i;
  43074. duk_uint_fast32_t n;
  43075. duk_hstring **h_keys_base;
  43076. DUK_DDD(DUK_DDDPRINT("duk_hobject_find_existing_entry() using linear scan for lookup"));
  43077. h_keys_base = DUK_HOBJECT_E_GET_KEY_BASE(heap, obj);
  43078. n = DUK_HOBJECT_GET_ENEXT(obj);
  43079. for (i = 0; i < n; i++) {
  43080. if (h_keys_base[i] == key) {
  43081. *e_idx = i;
  43082. *h_idx = -1;
  43083. return;
  43084. }
  43085. }
  43086. }
  43087. #if defined(DUK_USE_HOBJECT_HASH_PART)
  43088. else
  43089. {
  43090. /* hash lookup */
  43091. duk_uint32_t n;
  43092. duk_uint32_t i, step;
  43093. duk_uint32_t *h_base;
  43094. DUK_DDD(DUK_DDDPRINT("duk_hobject_find_existing_entry() using hash part for lookup"));
  43095. h_base = DUK_HOBJECT_H_GET_BASE(heap, obj);
  43096. n = DUK_HOBJECT_GET_HSIZE(obj);
  43097. i = DUK__HASH_INITIAL(DUK_HSTRING_GET_HASH(key), n);
  43098. step = DUK__HASH_PROBE_STEP(DUK_HSTRING_GET_HASH(key));
  43099. for (;;) {
  43100. duk_uint32_t t;
  43101. DUK_ASSERT_DISABLE(i >= 0); /* unsigned */
  43102. DUK_ASSERT(i < DUK_HOBJECT_GET_HSIZE(obj));
  43103. t = h_base[i];
  43104. DUK_ASSERT(t == DUK__HASH_UNUSED || t == DUK__HASH_DELETED ||
  43105. (t < DUK_HOBJECT_GET_ESIZE(obj))); /* t >= 0 always true, unsigned */
  43106. if (t == DUK__HASH_UNUSED) {
  43107. break;
  43108. } else if (t == DUK__HASH_DELETED) {
  43109. DUK_DDD(DUK_DDDPRINT("lookup miss (deleted) i=%ld, t=%ld",
  43110. (long) i, (long) t));
  43111. } else {
  43112. DUK_ASSERT(t < DUK_HOBJECT_GET_ESIZE(obj));
  43113. if (DUK_HOBJECT_E_GET_KEY(heap, obj, t) == key) {
  43114. DUK_DDD(DUK_DDDPRINT("lookup hit i=%ld, t=%ld -> key %p",
  43115. (long) i, (long) t, (void *) key));
  43116. *e_idx = t;
  43117. *h_idx = i;
  43118. return;
  43119. }
  43120. DUK_DDD(DUK_DDDPRINT("lookup miss i=%ld, t=%ld",
  43121. (long) i, (long) t));
  43122. }
  43123. i = (i + step) % n;
  43124. /* guaranteed to finish, as hash is never full */
  43125. DUK_ASSERT(i != (duk_uint32_t) DUK__HASH_INITIAL(DUK_HSTRING_GET_HASH(key), n));
  43126. }
  43127. }
  43128. #endif /* DUK_USE_HOBJECT_HASH_PART */
  43129. /* not found */
  43130. *e_idx = -1;
  43131. *h_idx = -1;
  43132. }
  43133. /* For internal use: get non-accessor entry value */
  43134. DUK_INTERNAL duk_tval *duk_hobject_find_existing_entry_tval_ptr(duk_heap *heap, duk_hobject *obj, duk_hstring *key) {
  43135. duk_int_t e_idx;
  43136. duk_int_t h_idx;
  43137. DUK_ASSERT(obj != NULL);
  43138. DUK_ASSERT(key != NULL);
  43139. DUK_UNREF(heap);
  43140. duk_hobject_find_existing_entry(heap, obj, key, &e_idx, &h_idx);
  43141. if (e_idx >= 0 && !DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap, obj, e_idx)) {
  43142. return DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(heap, obj, e_idx);
  43143. } else {
  43144. return NULL;
  43145. }
  43146. }
  43147. /* For internal use: get non-accessor entry value and attributes */
  43148. DUK_INTERNAL duk_tval *duk_hobject_find_existing_entry_tval_ptr_and_attrs(duk_heap *heap, duk_hobject *obj, duk_hstring *key, duk_int_t *out_attrs) {
  43149. duk_int_t e_idx;
  43150. duk_int_t h_idx;
  43151. DUK_ASSERT(obj != NULL);
  43152. DUK_ASSERT(key != NULL);
  43153. DUK_ASSERT(out_attrs != NULL);
  43154. DUK_UNREF(heap);
  43155. duk_hobject_find_existing_entry(heap, obj, key, &e_idx, &h_idx);
  43156. if (e_idx >= 0 && !DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap, obj, e_idx)) {
  43157. *out_attrs = DUK_HOBJECT_E_GET_FLAGS(heap, obj, e_idx);
  43158. return DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(heap, obj, e_idx);
  43159. } else {
  43160. *out_attrs = 0;
  43161. return NULL;
  43162. }
  43163. }
  43164. /* For internal use: get array part value */
  43165. DUK_INTERNAL duk_tval *duk_hobject_find_existing_array_entry_tval_ptr(duk_heap *heap, duk_hobject *obj, duk_uarridx_t i) {
  43166. duk_tval *tv;
  43167. DUK_ASSERT(obj != NULL);
  43168. DUK_UNREF(heap);
  43169. if (!DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  43170. return NULL;
  43171. }
  43172. if (i >= DUK_HOBJECT_GET_ASIZE(obj)) {
  43173. return NULL;
  43174. }
  43175. tv = DUK_HOBJECT_A_GET_VALUE_PTR(heap, obj, i);
  43176. return tv;
  43177. }
  43178. /*
  43179. * Allocate and initialize a new entry, resizing the properties allocation
  43180. * if necessary. Returns entry index (e_idx) or throws an error if alloc fails.
  43181. *
  43182. * Sets the key of the entry (increasing the key's refcount), and updates
  43183. * the hash part if it exists. Caller must set value and flags, and update
  43184. * the entry value refcount. A decref for the previous value is not necessary.
  43185. */
  43186. DUK_LOCAL duk_bool_t duk__alloc_entry_checked(duk_hthread *thr, duk_hobject *obj, duk_hstring *key) {
  43187. duk_uint32_t idx;
  43188. DUK_ASSERT(thr != NULL);
  43189. DUK_ASSERT(obj != NULL);
  43190. DUK_ASSERT(key != NULL);
  43191. DUK_ASSERT(DUK_HOBJECT_GET_ENEXT(obj) <= DUK_HOBJECT_GET_ESIZE(obj));
  43192. #ifdef DUK_USE_ASSERTIONS
  43193. /* key must not already exist in entry part */
  43194. {
  43195. duk_uint_fast32_t i;
  43196. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  43197. DUK_ASSERT(DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i) != key);
  43198. }
  43199. }
  43200. #endif
  43201. if (DUK_HOBJECT_GET_ENEXT(obj) >= DUK_HOBJECT_GET_ESIZE(obj)) {
  43202. /* only need to guarantee 1 more slot, but allocation growth is in chunks */
  43203. DUK_DDD(DUK_DDDPRINT("entry part full, allocate space for one more entry"));
  43204. duk__grow_props_for_new_entry_item(thr, obj);
  43205. }
  43206. DUK_ASSERT(DUK_HOBJECT_GET_ENEXT(obj) < DUK_HOBJECT_GET_ESIZE(obj));
  43207. idx = DUK_HOBJECT_POSTINC_ENEXT(obj);
  43208. /* previous value is assumed to be garbage, so don't touch it */
  43209. DUK_HOBJECT_E_SET_KEY(thr->heap, obj, idx, key);
  43210. DUK_HSTRING_INCREF(thr, key);
  43211. #if defined(DUK_USE_HOBJECT_HASH_PART)
  43212. if (DUK_UNLIKELY(DUK_HOBJECT_GET_HSIZE(obj) > 0)) {
  43213. duk_uint32_t n;
  43214. duk_uint32_t i, step;
  43215. duk_uint32_t *h_base = DUK_HOBJECT_H_GET_BASE(thr->heap, obj);
  43216. n = DUK_HOBJECT_GET_HSIZE(obj);
  43217. i = DUK__HASH_INITIAL(DUK_HSTRING_GET_HASH(key), n);
  43218. step = DUK__HASH_PROBE_STEP(DUK_HSTRING_GET_HASH(key));
  43219. for (;;) {
  43220. duk_uint32_t t = h_base[i];
  43221. if (t == DUK__HASH_UNUSED || t == DUK__HASH_DELETED) {
  43222. DUK_DDD(DUK_DDDPRINT("duk__alloc_entry_checked() inserted key into hash part, %ld -> %ld",
  43223. (long) i, (long) idx));
  43224. DUK_ASSERT_DISABLE(i >= 0); /* unsigned */
  43225. DUK_ASSERT(i < DUK_HOBJECT_GET_HSIZE(obj));
  43226. DUK_ASSERT_DISABLE(idx >= 0);
  43227. DUK_ASSERT(idx < DUK_HOBJECT_GET_ESIZE(obj));
  43228. h_base[i] = idx;
  43229. break;
  43230. }
  43231. DUK_DDD(DUK_DDDPRINT("duk__alloc_entry_checked() miss %ld", (long) i));
  43232. i = (i + step) % n;
  43233. /* guaranteed to find an empty slot */
  43234. DUK_ASSERT(i != (duk_uint32_t) DUK__HASH_INITIAL(DUK_HSTRING_GET_HASH(key), DUK_HOBJECT_GET_HSIZE(obj)));
  43235. }
  43236. }
  43237. #endif /* DUK_USE_HOBJECT_HASH_PART */
  43238. /* Note: we could return the hash index here too, but it's not
  43239. * needed right now.
  43240. */
  43241. DUK_ASSERT_DISABLE(idx >= 0);
  43242. DUK_ASSERT(idx < DUK_HOBJECT_GET_ESIZE(obj));
  43243. DUK_ASSERT(idx < DUK_HOBJECT_GET_ENEXT(obj));
  43244. return idx;
  43245. }
  43246. /*
  43247. * Object internal value
  43248. *
  43249. * Returned value is guaranteed to be reachable / incref'd, caller does not need
  43250. * to incref OR decref. No proxies or accessors are invoked, no prototype walk.
  43251. */
  43252. DUK_INTERNAL duk_bool_t duk_hobject_get_internal_value(duk_heap *heap, duk_hobject *obj, duk_tval *tv_out) {
  43253. duk_int_t e_idx;
  43254. duk_int_t h_idx;
  43255. DUK_ASSERT(heap != NULL);
  43256. DUK_ASSERT(obj != NULL);
  43257. DUK_ASSERT(tv_out != NULL);
  43258. /* always in entry part, no need to look up parents etc */
  43259. duk_hobject_find_existing_entry(heap, obj, DUK_HEAP_STRING_INT_VALUE(heap), &e_idx, &h_idx);
  43260. if (e_idx >= 0) {
  43261. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap, obj, e_idx));
  43262. DUK_TVAL_SET_TVAL(tv_out, DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(heap, obj, e_idx));
  43263. return 1;
  43264. }
  43265. DUK_TVAL_SET_UNDEFINED(tv_out);
  43266. return 0;
  43267. }
  43268. DUK_INTERNAL duk_hstring *duk_hobject_get_internal_value_string(duk_heap *heap, duk_hobject *obj) {
  43269. duk_tval tv;
  43270. DUK_ASSERT(heap != NULL);
  43271. DUK_ASSERT(obj != NULL);
  43272. if (duk_hobject_get_internal_value(heap, obj, &tv)) {
  43273. duk_hstring *h;
  43274. DUK_ASSERT(DUK_TVAL_IS_STRING(&tv));
  43275. h = DUK_TVAL_GET_STRING(&tv);
  43276. return h;
  43277. }
  43278. return NULL;
  43279. }
  43280. /*
  43281. * Arguments handling helpers (argument map mainly).
  43282. *
  43283. * An arguments object has exotic behavior for some numeric indices.
  43284. * Accesses may translate to identifier operations which may have
  43285. * arbitrary side effects (potentially invalidating any duk_tval
  43286. * pointers).
  43287. */
  43288. /* Lookup 'key' from arguments internal 'map', perform a variable lookup
  43289. * if mapped, and leave the result on top of stack (and return non-zero).
  43290. * Used in E5 Section 10.6 algorithms [[Get]] and [[GetOwnProperty]].
  43291. */
  43292. DUK_LOCAL
  43293. duk_bool_t duk__lookup_arguments_map(duk_hthread *thr,
  43294. duk_hobject *obj,
  43295. duk_hstring *key,
  43296. duk_propdesc *temp_desc,
  43297. duk_hobject **out_map,
  43298. duk_hobject **out_varenv) {
  43299. duk_context *ctx = (duk_context *) thr;
  43300. duk_hobject *map;
  43301. duk_hobject *varenv;
  43302. duk_bool_t rc;
  43303. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  43304. DUK_DDD(DUK_DDDPRINT("arguments map lookup: thr=%p, obj=%p, key=%p, temp_desc=%p "
  43305. "(obj -> %!O, key -> %!O)",
  43306. (void *) thr, (void *) obj, (void *) key, (void *) temp_desc,
  43307. (duk_heaphdr *) obj, (duk_heaphdr *) key));
  43308. if (!duk__get_own_property_desc(thr, obj, DUK_HTHREAD_STRING_INT_MAP(thr), temp_desc, DUK__DESC_FLAG_PUSH_VALUE)) {
  43309. DUK_DDD(DUK_DDDPRINT("-> no 'map'"));
  43310. return 0;
  43311. }
  43312. map = duk_require_hobject(ctx, -1);
  43313. DUK_ASSERT(map != NULL);
  43314. duk_pop(ctx); /* map is reachable through obj */
  43315. if (!duk__get_own_property_desc(thr, map, key, temp_desc, DUK__DESC_FLAG_PUSH_VALUE)) {
  43316. DUK_DDD(DUK_DDDPRINT("-> 'map' exists, but key not in map"));
  43317. return 0;
  43318. }
  43319. /* [... varname] */
  43320. DUK_DDD(DUK_DDDPRINT("-> 'map' exists, and contains key, key is mapped to argument/variable binding %!T",
  43321. (duk_tval *) duk_get_tval(ctx, -1)));
  43322. DUK_ASSERT(duk_is_string(ctx, -1)); /* guaranteed when building arguments */
  43323. /* get varenv for varname (callee's declarative lexical environment) */
  43324. rc = duk__get_own_property_desc(thr, obj, DUK_HTHREAD_STRING_INT_VARENV(thr), temp_desc, DUK__DESC_FLAG_PUSH_VALUE);
  43325. DUK_UNREF(rc);
  43326. DUK_ASSERT(rc != 0); /* arguments MUST have an initialized lexical environment reference */
  43327. varenv = duk_require_hobject(ctx, -1);
  43328. DUK_ASSERT(varenv != NULL);
  43329. duk_pop(ctx); /* varenv remains reachable through 'obj' */
  43330. DUK_DDD(DUK_DDDPRINT("arguments varenv is: %!dO", (duk_heaphdr *) varenv));
  43331. /* success: leave varname in stack */
  43332. *out_map = map;
  43333. *out_varenv = varenv;
  43334. return 1; /* [... varname] */
  43335. }
  43336. /* Lookup 'key' from arguments internal 'map', and leave replacement value
  43337. * on stack top if mapped (and return non-zero).
  43338. * Used in E5 Section 10.6 algorithm for [[GetOwnProperty]] (used by [[Get]]).
  43339. */
  43340. DUK_LOCAL duk_bool_t duk__check_arguments_map_for_get(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *temp_desc) {
  43341. duk_context *ctx = (duk_context *) thr;
  43342. duk_hobject *map;
  43343. duk_hobject *varenv;
  43344. duk_hstring *varname;
  43345. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  43346. if (!duk__lookup_arguments_map(thr, obj, key, temp_desc, &map, &varenv)) {
  43347. DUK_DDD(DUK_DDDPRINT("arguments: key not mapped, no exotic get behavior"));
  43348. return 0;
  43349. }
  43350. /* [... varname] */
  43351. varname = duk_require_hstring(ctx, -1);
  43352. DUK_ASSERT(varname != NULL);
  43353. duk_pop(ctx); /* varname is still reachable */
  43354. DUK_DDD(DUK_DDDPRINT("arguments object automatic getvar for a bound variable; "
  43355. "key=%!O, varname=%!O",
  43356. (duk_heaphdr *) key,
  43357. (duk_heaphdr *) varname));
  43358. (void) duk_js_getvar_envrec(thr, varenv, varname, 1 /*throw*/);
  43359. /* [... value this_binding] */
  43360. duk_pop(ctx);
  43361. /* leave result on stack top */
  43362. return 1;
  43363. }
  43364. /* Lookup 'key' from arguments internal 'map', perform a variable write if mapped.
  43365. * Used in E5 Section 10.6 algorithm for [[DefineOwnProperty]] (used by [[Put]]).
  43366. * Assumes stack top contains 'put' value (which is NOT popped).
  43367. */
  43368. 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) {
  43369. duk_context *ctx = (duk_context *) thr;
  43370. duk_hobject *map;
  43371. duk_hobject *varenv;
  43372. duk_hstring *varname;
  43373. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  43374. if (!duk__lookup_arguments_map(thr, obj, key, temp_desc, &map, &varenv)) {
  43375. DUK_DDD(DUK_DDDPRINT("arguments: key not mapped, no exotic put behavior"));
  43376. return;
  43377. }
  43378. /* [... put_value varname] */
  43379. varname = duk_require_hstring(ctx, -1);
  43380. DUK_ASSERT(varname != NULL);
  43381. duk_pop(ctx); /* varname is still reachable */
  43382. DUK_DDD(DUK_DDDPRINT("arguments object automatic putvar for a bound variable; "
  43383. "key=%!O, varname=%!O, value=%!T",
  43384. (duk_heaphdr *) key,
  43385. (duk_heaphdr *) varname,
  43386. (duk_tval *) duk_require_tval(ctx, -1)));
  43387. /* [... put_value] */
  43388. /*
  43389. * Note: although arguments object variable mappings are only established
  43390. * for non-strict functions (and a call to a non-strict function created
  43391. * the arguments object in question), an inner strict function may be doing
  43392. * the actual property write. Hence the throw_flag applied here comes from
  43393. * the property write call.
  43394. */
  43395. duk_js_putvar_envrec(thr, varenv, varname, duk_require_tval(ctx, -1), throw_flag);
  43396. /* [... put_value] */
  43397. }
  43398. /* Lookup 'key' from arguments internal 'map', delete mapping if found.
  43399. * Used in E5 Section 10.6 algorithm for [[Delete]]. Note that the
  43400. * variable/argument itself (where the map points) is not deleted.
  43401. */
  43402. DUK_LOCAL void duk__check_arguments_map_for_delete(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *temp_desc) {
  43403. duk_context *ctx = (duk_context *) thr;
  43404. duk_hobject *map;
  43405. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  43406. if (!duk__get_own_property_desc(thr, obj, DUK_HTHREAD_STRING_INT_MAP(thr), temp_desc, DUK__DESC_FLAG_PUSH_VALUE)) {
  43407. DUK_DDD(DUK_DDDPRINT("arguments: key not mapped, no exotic delete behavior"));
  43408. return;
  43409. }
  43410. map = duk_require_hobject(ctx, -1);
  43411. DUK_ASSERT(map != NULL);
  43412. duk_pop(ctx); /* map is reachable through obj */
  43413. DUK_DDD(DUK_DDDPRINT("-> have 'map', delete key %!O from map (if exists)); ignore result",
  43414. (duk_heaphdr *) key));
  43415. /* Note: no recursion issue, we can trust 'map' to behave */
  43416. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_BEHAVIOR(map));
  43417. DUK_DDD(DUK_DDDPRINT("map before deletion: %!O", (duk_heaphdr *) map));
  43418. (void) duk_hobject_delprop_raw(thr, map, key, 0); /* ignore result */
  43419. DUK_DDD(DUK_DDDPRINT("map after deletion: %!O", (duk_heaphdr *) map));
  43420. }
  43421. /*
  43422. * Ecmascript compliant [[GetOwnProperty]](P), for internal use only.
  43423. *
  43424. * If property is found:
  43425. * - Fills descriptor fields to 'out_desc'
  43426. * - If DUK__DESC_FLAG_PUSH_VALUE is set, pushes a value related to the
  43427. * property onto the stack ('undefined' for accessor properties).
  43428. * - Returns non-zero
  43429. *
  43430. * If property is not found:
  43431. * - 'out_desc' is left in untouched state (possibly garbage)
  43432. * - Nothing is pushed onto the stack (not even with DUK__DESC_FLAG_PUSH_VALUE
  43433. * set)
  43434. * - Returns zero
  43435. *
  43436. * Notes:
  43437. *
  43438. * - Getting a property descriptor may cause an allocation (and hence
  43439. * GC) to take place, hence reachability and refcount of all related
  43440. * values matter. Reallocation of value stack, properties, etc may
  43441. * invalidate many duk_tval pointers (concretely, those which reside
  43442. * in memory areas subject to reallocation). However, heap object
  43443. * pointers are never affected (heap objects have stable pointers).
  43444. *
  43445. * - The value of a plain property is always reachable and has a non-zero
  43446. * reference count.
  43447. *
  43448. * - The value of a virtual property is not necessarily reachable from
  43449. * elsewhere and may have a refcount of zero. Hence we push it onto
  43450. * the valstack for the caller, which ensures it remains reachable
  43451. * while it is needed.
  43452. *
  43453. * - There are no virtual accessor properties. Hence, all getters and
  43454. * setters are always related to concretely stored properties, which
  43455. * ensures that the get/set functions in the resulting descriptor are
  43456. * reachable and have non-zero refcounts. Should there be virtual
  43457. * accessor properties later, this would need to change.
  43458. */
  43459. DUK_LOCAL duk_bool_t duk__get_own_property_desc_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_uint32_t arr_idx, duk_propdesc *out_desc, duk_small_uint_t flags) {
  43460. duk_context *ctx = (duk_context *) thr;
  43461. duk_tval *tv;
  43462. DUK_DDD(DUK_DDDPRINT("duk__get_own_property_desc: thr=%p, obj=%p, key=%p, out_desc=%p, flags=%lx, "
  43463. "arr_idx=%ld (obj -> %!O, key -> %!O)",
  43464. (void *) thr, (void *) obj, (void *) key, (void *) out_desc,
  43465. (long) flags, (long) arr_idx,
  43466. (duk_heaphdr *) obj, (duk_heaphdr *) key));
  43467. DUK_ASSERT(ctx != NULL);
  43468. DUK_ASSERT(thr != NULL);
  43469. DUK_ASSERT(thr->heap != NULL);
  43470. DUK_ASSERT(obj != NULL);
  43471. DUK_ASSERT(key != NULL);
  43472. DUK_ASSERT(out_desc != NULL);
  43473. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  43474. /* XXX: optimize this filling behavior later */
  43475. out_desc->flags = 0;
  43476. out_desc->get = NULL;
  43477. out_desc->set = NULL;
  43478. out_desc->e_idx = -1;
  43479. out_desc->h_idx = -1;
  43480. out_desc->a_idx = -1;
  43481. /*
  43482. * Array part
  43483. */
  43484. if (DUK_HOBJECT_HAS_ARRAY_PART(obj) && arr_idx != DUK__NO_ARRAY_INDEX) {
  43485. if (arr_idx < DUK_HOBJECT_GET_ASIZE(obj)) {
  43486. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, arr_idx);
  43487. if (!DUK_TVAL_IS_UNUSED(tv)) {
  43488. DUK_DDD(DUK_DDDPRINT("-> found in array part"));
  43489. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  43490. duk_push_tval(ctx, tv);
  43491. }
  43492. /* implicit attributes */
  43493. out_desc->flags = DUK_PROPDESC_FLAG_WRITABLE |
  43494. DUK_PROPDESC_FLAG_CONFIGURABLE |
  43495. DUK_PROPDESC_FLAG_ENUMERABLE;
  43496. out_desc->a_idx = arr_idx;
  43497. goto prop_found;
  43498. }
  43499. }
  43500. /* assume array part is comprehensive (contains all array indexed elements
  43501. * or none of them); hence no need to check the entries part here.
  43502. */
  43503. DUK_DDD(DUK_DDDPRINT("-> not found as a concrete property (has array part, "
  43504. "should be there if present)"));
  43505. goto prop_not_found_concrete;
  43506. }
  43507. /*
  43508. * Entries part
  43509. */
  43510. duk_hobject_find_existing_entry(thr->heap, obj, key, &out_desc->e_idx, &out_desc->h_idx);
  43511. if (out_desc->e_idx >= 0) {
  43512. duk_int_t e_idx = out_desc->e_idx;
  43513. out_desc->flags = DUK_HOBJECT_E_GET_FLAGS(thr->heap, obj, e_idx);
  43514. if (out_desc->flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  43515. DUK_DDD(DUK_DDDPRINT("-> found accessor property in entry part"));
  43516. out_desc->get = DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, obj, e_idx);
  43517. out_desc->set = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, obj, e_idx);
  43518. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  43519. /* a dummy undefined value is pushed to make valstack
  43520. * behavior uniform for caller
  43521. */
  43522. duk_push_undefined(ctx);
  43523. }
  43524. } else {
  43525. DUK_DDD(DUK_DDDPRINT("-> found plain property in entry part"));
  43526. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, e_idx);
  43527. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  43528. duk_push_tval(ctx, tv);
  43529. }
  43530. }
  43531. goto prop_found;
  43532. }
  43533. /*
  43534. * Not found as a concrete property, check whether a String object
  43535. * virtual property matches.
  43536. */
  43537. prop_not_found_concrete:
  43538. if (DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(obj)) {
  43539. DUK_DDD(DUK_DDDPRINT("string object exotic property get for key: %!O, arr_idx: %ld",
  43540. (duk_heaphdr *) key, (long) arr_idx));
  43541. if (arr_idx != DUK__NO_ARRAY_INDEX) {
  43542. duk_hstring *h_val;
  43543. DUK_DDD(DUK_DDDPRINT("array index exists"));
  43544. h_val = duk_hobject_get_internal_value_string(thr->heap, obj);
  43545. DUK_ASSERT(h_val);
  43546. if (arr_idx < DUK_HSTRING_GET_CHARLEN(h_val)) {
  43547. DUK_DDD(DUK_DDDPRINT("-> found, array index inside string"));
  43548. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  43549. duk_push_hstring(ctx, h_val);
  43550. duk_substring(ctx, -1, arr_idx, arr_idx + 1); /* [str] -> [substr] */
  43551. }
  43552. out_desc->flags = DUK_PROPDESC_FLAG_ENUMERABLE | /* E5 Section 15.5.5.2 */
  43553. DUK_PROPDESC_FLAG_VIRTUAL;
  43554. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj));
  43555. return 1; /* cannot be e.g. arguments exotic, since exotic 'traits' are mutually exclusive */
  43556. } else {
  43557. /* index is above internal string length -> property is fully normal */
  43558. DUK_DDD(DUK_DDDPRINT("array index outside string -> normal property"));
  43559. }
  43560. } else if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  43561. duk_hstring *h_val;
  43562. DUK_DDD(DUK_DDDPRINT("-> found, key is 'length', length exotic behavior"));
  43563. h_val = duk_hobject_get_internal_value_string(thr->heap, obj);
  43564. DUK_ASSERT(h_val != NULL);
  43565. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  43566. duk_push_uint(ctx, (duk_uint_t) DUK_HSTRING_GET_CHARLEN(h_val));
  43567. }
  43568. out_desc->flags = DUK_PROPDESC_FLAG_VIRTUAL; /* E5 Section 15.5.5.1 */
  43569. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj));
  43570. return 1; /* cannot be arguments exotic */
  43571. }
  43572. } else if (DUK_HOBJECT_IS_BUFFEROBJECT(obj)) {
  43573. duk_hbufferobject *h_bufobj;
  43574. duk_uint_t byte_off;
  43575. duk_small_uint_t elem_size;
  43576. h_bufobj = (duk_hbufferobject *) obj;
  43577. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  43578. DUK_DDD(DUK_DDDPRINT("bufferobject property get for key: %!O, arr_idx: %ld",
  43579. (duk_heaphdr *) key, (long) arr_idx));
  43580. if (arr_idx != DUK__NO_ARRAY_INDEX) {
  43581. DUK_DDD(DUK_DDDPRINT("array index exists"));
  43582. /* Careful with wrapping: arr_idx upshift may easily wrap, whereas
  43583. * length downshift won't.
  43584. */
  43585. if (arr_idx < (h_bufobj->length >> h_bufobj->shift)) {
  43586. byte_off = arr_idx << h_bufobj->shift; /* no wrap assuming h_bufobj->length is valid */
  43587. elem_size = 1 << h_bufobj->shift;
  43588. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  43589. duk_uint8_t *data;
  43590. if (h_bufobj->buf != NULL && DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_bufobj, byte_off + elem_size)) {
  43591. data = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf) + h_bufobj->offset + byte_off;
  43592. duk_hbufferobject_push_validated_read(ctx, h_bufobj, data, elem_size);
  43593. } else {
  43594. DUK_D(DUK_DPRINT("bufferobject access out of underlying buffer, ignoring (read zero)"));
  43595. duk_push_uint(ctx, 0);
  43596. }
  43597. }
  43598. out_desc->flags = DUK_PROPDESC_FLAG_WRITABLE |
  43599. DUK_PROPDESC_FLAG_ENUMERABLE |
  43600. DUK_PROPDESC_FLAG_VIRTUAL;
  43601. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj));
  43602. return 1; /* cannot be e.g. arguments exotic, since exotic 'traits' are mutually exclusive */
  43603. } else {
  43604. /* index is above internal buffer length -> property is fully normal */
  43605. DUK_DDD(DUK_DDDPRINT("array index outside buffer -> normal property"));
  43606. }
  43607. } else if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  43608. DUK_DDD(DUK_DDDPRINT("-> found, key is 'length', length exotic behavior"));
  43609. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  43610. /* Length in elements: take into account shift, but
  43611. * intentionally don't check the underlying buffer here.
  43612. */
  43613. duk_push_uint(ctx, h_bufobj->length >> h_bufobj->shift);
  43614. }
  43615. out_desc->flags = DUK_PROPDESC_FLAG_VIRTUAL;
  43616. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj));
  43617. return 1; /* cannot be arguments exotic */
  43618. } else if (key == DUK_HTHREAD_STRING_BYTE_LENGTH(thr)) {
  43619. /* If neutered must return 0; length is zeroed during
  43620. * neutering.
  43621. */
  43622. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  43623. duk_push_uint(ctx, h_bufobj->length);
  43624. }
  43625. out_desc->flags = DUK_PROPDESC_FLAG_VIRTUAL;
  43626. return 1; /* cannot be arguments exotic */
  43627. } else if (key == DUK_HTHREAD_STRING_BYTE_OFFSET(thr)) {
  43628. /* If neutered must return 0; offset is zeroed during
  43629. * neutering.
  43630. */
  43631. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  43632. duk_push_uint(ctx, h_bufobj->offset);
  43633. }
  43634. out_desc->flags = DUK_PROPDESC_FLAG_VIRTUAL;
  43635. return 1; /* cannot be arguments exotic */
  43636. } else if (key == DUK_HTHREAD_STRING_BYTES_PER_ELEMENT(thr)) {
  43637. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  43638. duk_push_uint(ctx, 1 << h_bufobj->shift);
  43639. }
  43640. out_desc->flags = DUK_PROPDESC_FLAG_VIRTUAL;
  43641. return 1; /* cannot be arguments exotic */
  43642. }
  43643. } else if (DUK_HOBJECT_HAS_EXOTIC_DUKFUNC(obj)) {
  43644. DUK_DDD(DUK_DDDPRINT("duktape/c object exotic property get for key: %!O, arr_idx: %ld",
  43645. (duk_heaphdr *) key, (long) arr_idx));
  43646. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  43647. DUK_DDD(DUK_DDDPRINT("-> found, key is 'length', length exotic behavior"));
  43648. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  43649. duk_int16_t func_nargs = ((duk_hnativefunction *) obj)->nargs;
  43650. duk_push_int(ctx, func_nargs == DUK_HNATIVEFUNCTION_NARGS_VARARGS ? 0 : func_nargs);
  43651. }
  43652. out_desc->flags = DUK_PROPDESC_FLAG_VIRTUAL; /* not enumerable */
  43653. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj));
  43654. return 1; /* cannot be arguments exotic */
  43655. }
  43656. }
  43657. /* Array properties have exotic behavior but they are concrete,
  43658. * so no special handling here.
  43659. *
  43660. * Arguments exotic behavior (E5 Section 10.6, [[GetOwnProperty]]
  43661. * is only relevant as a post-check implemented below; hence no
  43662. * check here.
  43663. */
  43664. /*
  43665. * Not found as concrete or virtual
  43666. */
  43667. DUK_DDD(DUK_DDDPRINT("-> not found (virtual, entry part, or array part)"));
  43668. return 0;
  43669. /*
  43670. * Found
  43671. *
  43672. * Arguments object has exotic post-processing, see E5 Section 10.6,
  43673. * description of [[GetOwnProperty]] variant for arguments.
  43674. */
  43675. prop_found:
  43676. DUK_DDD(DUK_DDDPRINT("-> property found, checking for arguments exotic post-behavior"));
  43677. /* Notes:
  43678. * - only numbered indices are relevant, so arr_idx fast reject is good
  43679. * (this is valid unless there are more than 4**32-1 arguments).
  43680. * - since variable lookup has no side effects, this can be skipped if
  43681. * DUK__DESC_FLAG_PUSH_VALUE is not set.
  43682. */
  43683. if (DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj) &&
  43684. arr_idx != DUK__NO_ARRAY_INDEX &&
  43685. (flags & DUK__DESC_FLAG_PUSH_VALUE)) {
  43686. duk_propdesc temp_desc;
  43687. /* Magically bound variable cannot be an accessor. However,
  43688. * there may be an accessor property (or a plain property) in
  43689. * place with magic behavior removed. This happens e.g. when
  43690. * a magic property is redefined with defineProperty().
  43691. * Cannot assert for "not accessor" here.
  43692. */
  43693. /* replaces top of stack with new value if necessary */
  43694. DUK_ASSERT((flags & DUK__DESC_FLAG_PUSH_VALUE) != 0);
  43695. if (duk__check_arguments_map_for_get(thr, obj, key, &temp_desc)) {
  43696. DUK_DDD(DUK_DDDPRINT("-> arguments exotic behavior overrides result: %!T -> %!T",
  43697. (duk_tval *) duk_get_tval(ctx, -2),
  43698. (duk_tval *) duk_get_tval(ctx, -1)));
  43699. /* [... old_result result] -> [... result] */
  43700. duk_remove(ctx, -2);
  43701. }
  43702. }
  43703. return 1;
  43704. }
  43705. DUK_LOCAL duk_bool_t duk__get_own_property_desc(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *out_desc, duk_small_uint_t flags) {
  43706. DUK_ASSERT(thr != NULL);
  43707. DUK_ASSERT(obj != NULL);
  43708. DUK_ASSERT(key != NULL);
  43709. DUK_ASSERT(out_desc != NULL);
  43710. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  43711. return duk__get_own_property_desc_raw(thr, obj, key, DUK_HSTRING_GET_ARRIDX_SLOW(key), out_desc, flags);
  43712. }
  43713. /*
  43714. * Ecmascript compliant [[GetProperty]](P), for internal use only.
  43715. *
  43716. * If property is found:
  43717. * - Fills descriptor fields to 'out_desc'
  43718. * - If DUK__DESC_FLAG_PUSH_VALUE is set, pushes a value related to the
  43719. * property onto the stack ('undefined' for accessor properties).
  43720. * - Returns non-zero
  43721. *
  43722. * If property is not found:
  43723. * - 'out_desc' is left in untouched state (possibly garbage)
  43724. * - Nothing is pushed onto the stack (not even with DUK__DESC_FLAG_PUSH_VALUE
  43725. * set)
  43726. * - Returns zero
  43727. *
  43728. * May cause arbitrary side effects and invalidate (most) duk_tval
  43729. * pointers.
  43730. */
  43731. DUK_LOCAL duk_bool_t duk__get_property_desc(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *out_desc, duk_small_uint_t flags) {
  43732. duk_hobject *curr;
  43733. duk_uint32_t arr_idx;
  43734. duk_uint_t sanity;
  43735. DUK_ASSERT(thr != NULL);
  43736. DUK_ASSERT(thr->heap != NULL);
  43737. DUK_ASSERT(obj != NULL);
  43738. DUK_ASSERT(key != NULL);
  43739. DUK_ASSERT(out_desc != NULL);
  43740. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  43741. arr_idx = DUK_HSTRING_GET_ARRIDX_FAST(key);
  43742. DUK_DDD(DUK_DDDPRINT("duk__get_property_desc: thr=%p, obj=%p, key=%p, out_desc=%p, flags=%lx, "
  43743. "arr_idx=%ld (obj -> %!O, key -> %!O)",
  43744. (void *) thr, (void *) obj, (void *) key, (void *) out_desc,
  43745. (long) flags, (long) arr_idx,
  43746. (duk_heaphdr *) obj, (duk_heaphdr *) key));
  43747. curr = obj;
  43748. DUK_ASSERT(curr != NULL);
  43749. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  43750. do {
  43751. if (duk__get_own_property_desc_raw(thr, curr, key, arr_idx, out_desc, flags)) {
  43752. /* stack contains value (if requested), 'out_desc' is set */
  43753. return 1;
  43754. }
  43755. /* not found in 'curr', next in prototype chain; impose max depth */
  43756. if (sanity-- == 0) {
  43757. if (flags & DUK__DESC_FLAG_IGNORE_PROTOLOOP) {
  43758. /* treat like property not found */
  43759. break;
  43760. } else {
  43761. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  43762. }
  43763. }
  43764. curr = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, curr);
  43765. } while (curr);
  43766. /* out_desc is left untouched (possibly garbage), caller must use return
  43767. * value to determine whether out_desc can be looked up
  43768. */
  43769. return 0;
  43770. }
  43771. /*
  43772. * Shallow fast path checks for accessing array elements with numeric
  43773. * indices. The goal is to try to avoid coercing an array index to an
  43774. * (interned) string for the most common lookups, in particular, for
  43775. * standard Array objects.
  43776. *
  43777. * Interning is avoided but only for a very narrow set of cases:
  43778. * - Object has array part, index is within array allocation, and
  43779. * value is not unused (= key exists)
  43780. * - Object has no interfering exotic behavior (e.g. arguments or
  43781. * string object exotic behaviors interfere, array exotic
  43782. * behavior does not).
  43783. *
  43784. * Current shortcoming: if key does not exist (even if it is within
  43785. * the array allocation range) a slow path lookup with interning is
  43786. * always required. This can probably be fixed so that there is a
  43787. * quick fast path for non-existent elements as well, at least for
  43788. * standard Array objects.
  43789. */
  43790. DUK_LOCAL duk_tval *duk__getprop_shallow_fastpath_array_tval(duk_hthread *thr, duk_hobject *obj, duk_tval *tv_key) {
  43791. duk_tval *tv;
  43792. duk_uint32_t idx;
  43793. DUK_UNREF(thr);
  43794. if (!(DUK_HOBJECT_HAS_ARRAY_PART(obj) &&
  43795. !DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj) &&
  43796. !DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(obj) &&
  43797. !DUK_HOBJECT_IS_BUFFEROBJECT(obj) &&
  43798. !DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(obj))) {
  43799. /* Must have array part and no conflicting exotic behaviors.
  43800. * Doesn't need to have array special behavior, e.g. Arguments
  43801. * object has array part.
  43802. */
  43803. return NULL;
  43804. }
  43805. /* Arrays never have other exotic behaviors. */
  43806. DUK_DDD(DUK_DDDPRINT("fast path attempt (no exotic string/arguments/buffer "
  43807. "behavior, object has array part)"));
  43808. #if defined(DUK_USE_FASTINT)
  43809. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  43810. idx = duk__tval_fastint_to_arr_idx(tv_key);
  43811. } else
  43812. #endif
  43813. if (DUK_TVAL_IS_DOUBLE(tv_key)) {
  43814. idx = duk__tval_number_to_arr_idx(tv_key);
  43815. } else {
  43816. DUK_DDD(DUK_DDDPRINT("key is not a number"));
  43817. return NULL;
  43818. }
  43819. /* If index is not valid, idx will be DUK__NO_ARRAY_INDEX which
  43820. * is 0xffffffffUL. We don't need to check for that explicitly
  43821. * because 0xffffffffUL will never be inside object 'a_size'.
  43822. */
  43823. if (idx >= DUK_HOBJECT_GET_ASIZE(obj)) {
  43824. DUK_DDD(DUK_DDDPRINT("key is not an array index or outside array part"));
  43825. return NULL;
  43826. }
  43827. DUK_ASSERT(idx != 0xffffffffUL);
  43828. DUK_ASSERT(idx != DUK__NO_ARRAY_INDEX);
  43829. /* XXX: for array instances we could take a shortcut here and assume
  43830. * Array.prototype doesn't contain an array index property.
  43831. */
  43832. DUK_DDD(DUK_DDDPRINT("key is a valid array index and inside array part"));
  43833. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, idx);
  43834. if (!DUK_TVAL_IS_UNUSED(tv)) {
  43835. DUK_DDD(DUK_DDDPRINT("-> fast path successful"));
  43836. return tv;
  43837. }
  43838. DUK_DDD(DUK_DDDPRINT("fast path attempt failed, fall back to slow path"));
  43839. return NULL;
  43840. }
  43841. 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, duk_propdesc *temp_desc) {
  43842. duk_tval *tv;
  43843. duk_uint32_t idx;
  43844. duk_uint32_t old_len, new_len;
  43845. if (!(DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj) &&
  43846. DUK_HOBJECT_HAS_ARRAY_PART(obj) &&
  43847. DUK_HOBJECT_HAS_EXTENSIBLE(obj))) {
  43848. return 0;
  43849. }
  43850. #if defined(DUK_USE_FASTINT)
  43851. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  43852. idx = duk__tval_fastint_to_arr_idx(tv_key);
  43853. } else
  43854. #endif
  43855. if (DUK_TVAL_IS_DOUBLE(tv_key)) {
  43856. idx = duk__tval_number_to_arr_idx(tv_key);
  43857. } else {
  43858. DUK_DDD(DUK_DDDPRINT("key is not a number"));
  43859. return 0;
  43860. }
  43861. /* If index is not valid, idx will be DUK__NO_ARRAY_INDEX which
  43862. * is 0xffffffffUL. We don't need to check for that explicitly
  43863. * because 0xffffffffUL will never be inside object 'a_size'.
  43864. */
  43865. if (idx >= DUK_HOBJECT_GET_ASIZE(obj)) { /* for resizing of array part, use slow path */
  43866. return 0;
  43867. }
  43868. DUK_ASSERT(idx != 0xffffffffUL);
  43869. DUK_ASSERT(idx != DUK__NO_ARRAY_INDEX);
  43870. old_len = duk__get_old_array_length(thr, obj, temp_desc);
  43871. if (idx >= old_len) {
  43872. DUK_DDD(DUK_DDDPRINT("write new array entry requires length update "
  43873. "(arr_idx=%ld, old_len=%ld)",
  43874. (long) idx, (long) old_len));
  43875. if (!(temp_desc->flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  43876. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_WRITABLE);
  43877. return 0; /* not reachable */
  43878. }
  43879. new_len = idx + 1;
  43880. /* No resize has occurred so temp_desc->e_idx is still OK */
  43881. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, temp_desc->e_idx);
  43882. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  43883. #if defined(DUK_USE_FASTINT)
  43884. DUK_TVAL_SET_FASTINT_U32(tv, new_len); /* no need for decref/incref because value is a number */
  43885. #else
  43886. DUK_TVAL_SET_NUMBER(tv, (duk_double_t) new_len); /* no need for decref/incref because value is a number */
  43887. #endif
  43888. } else {
  43889. ;
  43890. }
  43891. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, idx);
  43892. DUK_TVAL_SET_TVAL_UPDREF(thr, tv, tv_val); /* side effects */
  43893. DUK_DDD(DUK_DDDPRINT("array fast path success for index %ld", (long) idx));
  43894. return 1;
  43895. }
  43896. /*
  43897. * Fast path for bufferobject getprop/putprop
  43898. */
  43899. DUK_LOCAL duk_bool_t duk__getprop_fastpath_bufobj_tval(duk_hthread *thr, duk_hobject *obj, duk_tval *tv_key) {
  43900. duk_context *ctx;
  43901. duk_uint32_t idx;
  43902. duk_hbufferobject *h_bufobj;
  43903. duk_uint_t byte_off;
  43904. duk_small_uint_t elem_size;
  43905. duk_uint8_t *data;
  43906. ctx = (duk_context *) thr;
  43907. if (!DUK_HOBJECT_IS_BUFFEROBJECT(obj)) {
  43908. return 0;
  43909. }
  43910. h_bufobj = (duk_hbufferobject *) obj;
  43911. #if defined(DUK_USE_FASTINT)
  43912. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  43913. idx = duk__tval_fastint_to_arr_idx(tv_key);
  43914. } else
  43915. #endif
  43916. if (DUK_TVAL_IS_DOUBLE(tv_key)) {
  43917. idx = duk__tval_number_to_arr_idx(tv_key);
  43918. } else {
  43919. return 0;
  43920. }
  43921. /* If index is not valid, idx will be DUK__NO_ARRAY_INDEX which
  43922. * is 0xffffffffUL. We don't need to check for that explicitly
  43923. * because 0xffffffffUL will never be inside bufferobject length.
  43924. */
  43925. /* Careful with wrapping (left shifting idx would be unsafe). */
  43926. if (idx >= (h_bufobj->length >> h_bufobj->shift)) {
  43927. return 0;
  43928. }
  43929. DUK_ASSERT(idx != DUK__NO_ARRAY_INDEX);
  43930. byte_off = idx << h_bufobj->shift; /* no wrap assuming h_bufobj->length is valid */
  43931. elem_size = 1 << h_bufobj->shift;
  43932. if (h_bufobj->buf != NULL && DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_bufobj, byte_off + elem_size)) {
  43933. data = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf) + h_bufobj->offset + byte_off;
  43934. duk_hbufferobject_push_validated_read(ctx, h_bufobj, data, elem_size);
  43935. } else {
  43936. DUK_D(DUK_DPRINT("bufferobject access out of underlying buffer, ignoring (read zero)"));
  43937. duk_push_uint(ctx, 0);
  43938. }
  43939. return 1;
  43940. }
  43941. DUK_LOCAL duk_bool_t duk__putprop_fastpath_bufobj_tval(duk_hthread *thr, duk_hobject *obj, duk_tval *tv_key, duk_tval *tv_val) {
  43942. duk_context *ctx;
  43943. duk_uint32_t idx;
  43944. duk_hbufferobject *h_bufobj;
  43945. duk_uint_t byte_off;
  43946. duk_small_uint_t elem_size;
  43947. duk_uint8_t *data;
  43948. ctx = (duk_context *) thr;
  43949. if (!(DUK_HOBJECT_IS_BUFFEROBJECT(obj) &&
  43950. DUK_TVAL_IS_NUMBER(tv_val))) {
  43951. return 0;
  43952. }
  43953. h_bufobj = (duk_hbufferobject *) obj;
  43954. #if defined(DUK_USE_FASTINT)
  43955. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  43956. idx = duk__tval_fastint_to_arr_idx(tv_key);
  43957. } else
  43958. #endif
  43959. if (DUK_TVAL_IS_DOUBLE(tv_key)) {
  43960. idx = duk__tval_number_to_arr_idx(tv_key);
  43961. } else {
  43962. return 0;
  43963. }
  43964. /* If index is not valid, idx will be DUK__NO_ARRAY_INDEX which
  43965. * is 0xffffffffUL. We don't need to check for that explicitly
  43966. * because 0xffffffffUL will never be inside bufferobject length.
  43967. */
  43968. /* Careful with wrapping (left shifting idx would be unsafe). */
  43969. if (idx >= (h_bufobj->length >> h_bufobj->shift)) {
  43970. return 0;
  43971. }
  43972. DUK_ASSERT(idx != DUK__NO_ARRAY_INDEX);
  43973. byte_off = idx << h_bufobj->shift; /* no wrap assuming h_bufobj->length is valid */
  43974. elem_size = 1 << h_bufobj->shift;
  43975. /* Value is required to be a number in the fast path so there
  43976. * are no side effects in write coercion.
  43977. */
  43978. duk_push_tval(ctx, tv_val);
  43979. DUK_ASSERT(duk_is_number(ctx, -1));
  43980. if (h_bufobj->buf != NULL && DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_bufobj, byte_off + elem_size)) {
  43981. data = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf) + h_bufobj->offset + byte_off;
  43982. duk_hbufferobject_validated_write(ctx, h_bufobj, data, elem_size);
  43983. } else {
  43984. DUK_D(DUK_DPRINT("bufferobject access out of underlying buffer, ignoring (write skipped)"));
  43985. }
  43986. duk_pop(ctx);
  43987. return 1;
  43988. }
  43989. /*
  43990. * GETPROP: Ecmascript property read.
  43991. */
  43992. DUK_INTERNAL duk_bool_t duk_hobject_getprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key) {
  43993. duk_context *ctx = (duk_context *) thr;
  43994. duk_tval tv_obj_copy;
  43995. duk_tval tv_key_copy;
  43996. duk_hobject *curr = NULL;
  43997. duk_hstring *key = NULL;
  43998. duk_uint32_t arr_idx = DUK__NO_ARRAY_INDEX;
  43999. duk_propdesc desc;
  44000. duk_uint_t sanity;
  44001. DUK_DDD(DUK_DDDPRINT("getprop: thr=%p, obj=%p, key=%p (obj -> %!T, key -> %!T)",
  44002. (void *) thr, (void *) tv_obj, (void *) tv_key,
  44003. (duk_tval *) tv_obj, (duk_tval *) tv_key));
  44004. DUK_ASSERT(ctx != NULL);
  44005. DUK_ASSERT(thr != NULL);
  44006. DUK_ASSERT(thr->heap != NULL);
  44007. DUK_ASSERT(tv_obj != NULL);
  44008. DUK_ASSERT(tv_key != NULL);
  44009. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  44010. /*
  44011. * Make a copy of tv_obj, tv_key, and tv_val to avoid any issues of
  44012. * them being invalidated by a valstack resize.
  44013. *
  44014. * XXX: this is now an overkill for many fast paths. Rework this
  44015. * to be faster (although switching to a valstack discipline might
  44016. * be a better solution overall).
  44017. */
  44018. DUK_TVAL_SET_TVAL(&tv_obj_copy, tv_obj);
  44019. DUK_TVAL_SET_TVAL(&tv_key_copy, tv_key);
  44020. tv_obj = &tv_obj_copy;
  44021. tv_key = &tv_key_copy;
  44022. /*
  44023. * Coercion and fast path processing
  44024. */
  44025. switch (DUK_TVAL_GET_TAG(tv_obj)) {
  44026. case DUK_TAG_UNDEFINED:
  44027. case DUK_TAG_NULL: {
  44028. /* Note: unconditional throw */
  44029. DUK_DDD(DUK_DDDPRINT("base object is undefined or null -> reject"));
  44030. #if defined(DUK_USE_VERBOSE_PROP_ERRORS)
  44031. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "cannot read property %s of %s",
  44032. duk_push_string_tval_readable(ctx, tv_key), duk_push_string_tval_readable(ctx, tv_obj));
  44033. #else
  44034. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_BASE);
  44035. #endif
  44036. return 0;
  44037. }
  44038. case DUK_TAG_BOOLEAN: {
  44039. DUK_DDD(DUK_DDDPRINT("base object is a boolean, start lookup from boolean prototype"));
  44040. curr = thr->builtins[DUK_BIDX_BOOLEAN_PROTOTYPE];
  44041. break;
  44042. }
  44043. case DUK_TAG_STRING: {
  44044. duk_hstring *h = DUK_TVAL_GET_STRING(tv_obj);
  44045. duk_int_t pop_count;
  44046. #if defined(DUK_USE_FASTINT)
  44047. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  44048. arr_idx = duk__tval_fastint_to_arr_idx(tv_key);
  44049. DUK_DDD(DUK_DDDPRINT("base object string, key is a fast-path fastint; arr_idx %ld", (long) arr_idx));
  44050. pop_count = 0;
  44051. } else
  44052. #endif
  44053. if (DUK_TVAL_IS_NUMBER(tv_key)) {
  44054. arr_idx = duk__tval_number_to_arr_idx(tv_key);
  44055. DUK_DDD(DUK_DDDPRINT("base object string, key is a fast-path number; arr_idx %ld", (long) arr_idx));
  44056. pop_count = 0;
  44057. } else {
  44058. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  44059. DUK_ASSERT(key != NULL);
  44060. DUK_DDD(DUK_DDDPRINT("base object string, key is a non-fast-path number; after "
  44061. "coercion key is %!T, arr_idx %ld",
  44062. (duk_tval *) duk_get_tval(ctx, -1), (long) arr_idx));
  44063. pop_count = 1;
  44064. }
  44065. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  44066. arr_idx < DUK_HSTRING_GET_CHARLEN(h)) {
  44067. duk_pop_n(ctx, pop_count);
  44068. duk_push_hstring(ctx, h);
  44069. duk_substring(ctx, -1, arr_idx, arr_idx + 1); /* [str] -> [substr] */
  44070. DUK_DDD(DUK_DDDPRINT("-> %!T (base is string, key is an index inside string length "
  44071. "after coercion -> return char)",
  44072. (duk_tval *) duk_get_tval(ctx, -1)));
  44073. return 1;
  44074. }
  44075. if (pop_count == 0) {
  44076. /* This is a pretty awkward control flow, but we need to recheck the
  44077. * key coercion here.
  44078. */
  44079. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  44080. DUK_ASSERT(key != NULL);
  44081. DUK_DDD(DUK_DDDPRINT("base object string, key is a non-fast-path number; after "
  44082. "coercion key is %!T, arr_idx %ld",
  44083. (duk_tval *) duk_get_tval(ctx, -1), (long) arr_idx));
  44084. }
  44085. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  44086. duk_pop(ctx); /* [key] -> [] */
  44087. duk_push_uint(ctx, (duk_uint_t) DUK_HSTRING_GET_CHARLEN(h)); /* [] -> [res] */
  44088. DUK_DDD(DUK_DDDPRINT("-> %!T (base is string, key is 'length' after coercion -> "
  44089. "return string length)",
  44090. (duk_tval *) duk_get_tval(ctx, -1)));
  44091. return 1;
  44092. }
  44093. DUK_DDD(DUK_DDDPRINT("base object is a string, start lookup from string prototype"));
  44094. curr = thr->builtins[DUK_BIDX_STRING_PROTOTYPE];
  44095. goto lookup; /* avoid double coercion */
  44096. }
  44097. case DUK_TAG_OBJECT: {
  44098. duk_tval *tmp;
  44099. curr = DUK_TVAL_GET_OBJECT(tv_obj);
  44100. DUK_ASSERT(curr != NULL);
  44101. tmp = duk__getprop_shallow_fastpath_array_tval(thr, curr, tv_key);
  44102. if (tmp) {
  44103. duk_push_tval(ctx, tmp);
  44104. DUK_DDD(DUK_DDDPRINT("-> %!T (base is object, key is a number, array part "
  44105. "fast path)",
  44106. (duk_tval *) duk_get_tval(ctx, -1)));
  44107. return 1;
  44108. }
  44109. if (duk__getprop_fastpath_bufobj_tval(thr, curr, tv_key) != 0) {
  44110. /* Read value pushed on stack. */
  44111. DUK_DDD(DUK_DDDPRINT("-> %!T (base is bufobj, key is a number, bufferobject "
  44112. "fast path)",
  44113. (duk_tval *) duk_get_tval(ctx, -1)));
  44114. return 1;
  44115. }
  44116. #if defined(DUK_USE_ES6_PROXY)
  44117. if (DUK_UNLIKELY(DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(curr))) {
  44118. duk_hobject *h_target;
  44119. if (duk__proxy_check_prop(thr, curr, DUK_STRIDX_GET, tv_key, &h_target)) {
  44120. /* -> [ ... trap handler ] */
  44121. DUK_DDD(DUK_DDDPRINT("-> proxy object 'get' for key %!T", (duk_tval *) tv_key));
  44122. duk_push_hobject(ctx, h_target); /* target */
  44123. duk_push_tval(ctx, tv_key); /* P */
  44124. duk_push_tval(ctx, tv_obj); /* Receiver: Proxy object */
  44125. duk_call_method(ctx, 3 /*nargs*/);
  44126. /* Target object must be checked for a conflicting
  44127. * non-configurable property.
  44128. */
  44129. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  44130. DUK_ASSERT(key != NULL);
  44131. if (duk__get_own_property_desc_raw(thr, h_target, key, arr_idx, &desc, DUK__DESC_FLAG_PUSH_VALUE)) {
  44132. duk_tval *tv_hook = duk_require_tval(ctx, -3); /* value from hook */
  44133. duk_tval *tv_targ = duk_require_tval(ctx, -1); /* value from target */
  44134. duk_bool_t datadesc_reject;
  44135. duk_bool_t accdesc_reject;
  44136. DUK_DDD(DUK_DDDPRINT("proxy 'get': target has matching property %!O, check for "
  44137. "conflicting property; tv_hook=%!T, tv_targ=%!T, desc.flags=0x%08lx, "
  44138. "desc.get=%p, desc.set=%p",
  44139. (duk_heaphdr *) key, (duk_tval *) tv_hook, (duk_tval *) tv_targ,
  44140. (unsigned long) desc.flags,
  44141. (void *) desc.get, (void *) desc.set));
  44142. datadesc_reject = !(desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) &&
  44143. !(desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) &&
  44144. !(desc.flags & DUK_PROPDESC_FLAG_WRITABLE) &&
  44145. !duk_js_samevalue(tv_hook, tv_targ);
  44146. accdesc_reject = (desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) &&
  44147. !(desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) &&
  44148. (desc.get == NULL) &&
  44149. !DUK_TVAL_IS_UNDEFINED(tv_hook);
  44150. if (datadesc_reject || accdesc_reject) {
  44151. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROXY_REJECTED);
  44152. }
  44153. duk_pop_2(ctx);
  44154. } else {
  44155. duk_pop(ctx);
  44156. }
  44157. return 1; /* return value */
  44158. }
  44159. curr = h_target; /* resume lookup from target */
  44160. DUK_TVAL_SET_OBJECT(tv_obj, curr);
  44161. }
  44162. #endif /* DUK_USE_ES6_PROXY */
  44163. if (DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(curr)) {
  44164. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  44165. DUK_ASSERT(key != NULL);
  44166. if (duk__check_arguments_map_for_get(thr, curr, key, &desc)) {
  44167. DUK_DDD(DUK_DDDPRINT("-> %!T (base is object with arguments exotic behavior, "
  44168. "key matches magically bound property -> skip standard "
  44169. "Get with replacement value)",
  44170. (duk_tval *) duk_get_tval(ctx, -1)));
  44171. /* no need for 'caller' post-check, because 'key' must be an array index */
  44172. duk_remove(ctx, -2); /* [key result] -> [result] */
  44173. return 1;
  44174. }
  44175. goto lookup; /* avoid double coercion */
  44176. }
  44177. break;
  44178. }
  44179. /* Buffer has virtual properties similar to string, but indexed values
  44180. * are numbers, not 1-byte buffers/strings which would perform badly.
  44181. */
  44182. case DUK_TAG_BUFFER: {
  44183. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv_obj);
  44184. duk_int_t pop_count;
  44185. /*
  44186. * Because buffer values are often looped over, a number fast path
  44187. * is important.
  44188. */
  44189. #if defined(DUK_USE_FASTINT)
  44190. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  44191. arr_idx = duk__tval_fastint_to_arr_idx(tv_key);
  44192. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a fast-path fastint; arr_idx %ld", (long) arr_idx));
  44193. pop_count = 0;
  44194. }
  44195. else
  44196. #endif
  44197. if (DUK_TVAL_IS_NUMBER(tv_key)) {
  44198. arr_idx = duk__tval_number_to_arr_idx(tv_key);
  44199. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a fast-path number; arr_idx %ld", (long) arr_idx));
  44200. pop_count = 0;
  44201. } else {
  44202. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  44203. DUK_ASSERT(key != NULL);
  44204. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a non-fast-path number; after "
  44205. "coercion key is %!T, arr_idx %ld",
  44206. (duk_tval *) duk_get_tval(ctx, -1), (long) arr_idx));
  44207. pop_count = 1;
  44208. }
  44209. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  44210. arr_idx < DUK_HBUFFER_GET_SIZE(h)) {
  44211. duk_pop_n(ctx, pop_count);
  44212. duk_push_uint(ctx, ((duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h))[arr_idx]);
  44213. DUK_DDD(DUK_DDDPRINT("-> %!T (base is buffer, key is an index inside buffer length "
  44214. "after coercion -> return byte as number)",
  44215. (duk_tval *) duk_get_tval(ctx, -1)));
  44216. return 1;
  44217. }
  44218. if (pop_count == 0) {
  44219. /* This is a pretty awkward control flow, but we need to recheck the
  44220. * key coercion here.
  44221. */
  44222. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  44223. DUK_ASSERT(key != NULL);
  44224. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a non-fast-path number; after "
  44225. "coercion key is %!T, arr_idx %ld",
  44226. (duk_tval *) duk_get_tval(ctx, -1), (long) arr_idx));
  44227. }
  44228. if (key == DUK_HTHREAD_STRING_LENGTH(thr) ||
  44229. key == DUK_HTHREAD_STRING_BYTE_LENGTH(thr)) {
  44230. duk_pop(ctx); /* [key] -> [] */
  44231. duk_push_uint(ctx, (duk_uint_t) DUK_HBUFFER_GET_SIZE(h)); /* [] -> [res] */
  44232. DUK_DDD(DUK_DDDPRINT("-> %!T (base is buffer, key is 'length' or 'byteLength' "
  44233. "after coercion -> return buffer length)",
  44234. (duk_tval *) duk_get_tval(ctx, -1)));
  44235. return 1;
  44236. } else if (key == DUK_HTHREAD_STRING_BYTE_OFFSET(thr)) {
  44237. duk_pop(ctx); /* [key] -> [] */
  44238. duk_push_uint(ctx, 0); /* [] -> [res] */
  44239. DUK_DDD(DUK_DDDPRINT("-> %!T (base is buffer, key is 'byteOffset' after coercion -> "
  44240. "return 0 for consistency with Buffer objects)",
  44241. (duk_tval *) duk_get_tval(ctx, -1)));
  44242. return 1;
  44243. } else if (key == DUK_HTHREAD_STRING_BYTES_PER_ELEMENT(thr)) {
  44244. duk_pop(ctx); /* [key] -> [] */
  44245. duk_push_uint(ctx, 1); /* [] -> [res] */
  44246. DUK_DDD(DUK_DDDPRINT("-> %!T (base is buffer, key is 'BYTES_PER_ELEMENT' after coercion -> "
  44247. "return 1 for consistency with Buffer objects)",
  44248. (duk_tval *) duk_get_tval(ctx, -1)));
  44249. return 1;
  44250. }
  44251. DUK_DDD(DUK_DDDPRINT("base object is a buffer, start lookup from buffer prototype"));
  44252. curr = thr->builtins[DUK_BIDX_BUFFER_PROTOTYPE];
  44253. goto lookup; /* avoid double coercion */
  44254. }
  44255. case DUK_TAG_POINTER: {
  44256. DUK_DDD(DUK_DDDPRINT("base object is a pointer, start lookup from pointer prototype"));
  44257. curr = thr->builtins[DUK_BIDX_POINTER_PROTOTYPE];
  44258. break;
  44259. }
  44260. case DUK_TAG_LIGHTFUNC: {
  44261. duk_int_t lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv_obj);
  44262. /* Must coerce key: if key is an object, it may coerce to e.g. 'length'. */
  44263. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  44264. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  44265. duk_int_t lf_len = DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags);
  44266. duk_pop(ctx);
  44267. duk_push_int(ctx, lf_len);
  44268. return 1;
  44269. } else if (key == DUK_HTHREAD_STRING_NAME(thr)) {
  44270. duk_pop(ctx);
  44271. duk_push_lightfunc_name(ctx, tv_obj);
  44272. return 1;
  44273. }
  44274. DUK_DDD(DUK_DDDPRINT("base object is a lightfunc, start lookup from function prototype"));
  44275. curr = thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE];
  44276. goto lookup; /* avoid double coercion */
  44277. }
  44278. #if defined(DUK_USE_FASTINT)
  44279. case DUK_TAG_FASTINT:
  44280. #endif
  44281. default: {
  44282. /* number */
  44283. DUK_DDD(DUK_DDDPRINT("base object is a number, start lookup from number prototype"));
  44284. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv_obj));
  44285. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_obj));
  44286. curr = thr->builtins[DUK_BIDX_NUMBER_PROTOTYPE];
  44287. break;
  44288. }
  44289. }
  44290. /* key coercion (unless already coerced above) */
  44291. DUK_ASSERT(key == NULL);
  44292. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  44293. DUK_ASSERT(key != NULL);
  44294. /*
  44295. * Property lookup
  44296. */
  44297. lookup:
  44298. /* [key] (coerced) */
  44299. DUK_ASSERT(curr != NULL);
  44300. DUK_ASSERT(key != NULL);
  44301. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  44302. do {
  44303. if (!duk__get_own_property_desc_raw(thr, curr, key, arr_idx, &desc, DUK__DESC_FLAG_PUSH_VALUE)) {
  44304. goto next_in_chain;
  44305. }
  44306. if (desc.get != NULL) {
  44307. /* accessor with defined getter */
  44308. DUK_ASSERT((desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) != 0);
  44309. duk_pop(ctx); /* [key undefined] -> [key] */
  44310. duk_push_hobject(ctx, desc.get);
  44311. duk_push_tval(ctx, tv_obj); /* note: original, uncoerced base */
  44312. #ifdef DUK_USE_NONSTD_GETTER_KEY_ARGUMENT
  44313. duk_dup(ctx, -3);
  44314. duk_call_method(ctx, 1); /* [key getter this key] -> [key retval] */
  44315. #else
  44316. duk_call_method(ctx, 0); /* [key getter this] -> [key retval] */
  44317. #endif
  44318. } else {
  44319. /* [key value] or [key undefined] */
  44320. /* data property or accessor without getter */
  44321. DUK_ASSERT(((desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) == 0) ||
  44322. (desc.get == NULL));
  44323. /* if accessor without getter, return value is undefined */
  44324. DUK_ASSERT(((desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) == 0) ||
  44325. duk_is_undefined(ctx, -1));
  44326. /* Note: for an accessor without getter, falling through to
  44327. * check for "caller" exotic behavior is unnecessary as
  44328. * "undefined" will never activate the behavior. But it does
  44329. * no harm, so we'll do it anyway.
  44330. */
  44331. }
  44332. goto found; /* [key result] */
  44333. next_in_chain:
  44334. /* XXX: option to pretend property doesn't exist if sanity limit is
  44335. * hit might be useful.
  44336. */
  44337. if (sanity-- == 0) {
  44338. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  44339. }
  44340. curr = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, curr);
  44341. } while (curr);
  44342. /*
  44343. * Not found
  44344. */
  44345. duk_to_undefined(ctx, -1); /* [key] -> [undefined] (default value) */
  44346. DUK_DDD(DUK_DDDPRINT("-> %!T (not found)", (duk_tval *) duk_get_tval(ctx, -1)));
  44347. return 0;
  44348. /*
  44349. * Found; post-processing (Function and arguments objects)
  44350. */
  44351. found:
  44352. /* [key result] */
  44353. #if !defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY)
  44354. /* Special behavior for 'caller' property of (non-bound) function objects
  44355. * and non-strict Arguments objects: if 'caller' -value- (!) is a strict
  44356. * mode function, throw a TypeError (E5 Sections 15.3.5.4, 10.6).
  44357. * Quite interestingly, a non-strict function with no formal arguments
  44358. * will get an arguments object -without- special 'caller' behavior!
  44359. *
  44360. * The E5.1 spec is a bit ambiguous if this special behavior applies when
  44361. * a bound function is the base value (not the 'caller' value): Section
  44362. * 15.3.4.5 (describing bind()) states that [[Get]] for bound functions
  44363. * matches that of Section 15.3.5.4 ([[Get]] for Function instances).
  44364. * However, Section 13.3.5.4 has "NOTE: Function objects created using
  44365. * Function.prototype.bind use the default [[Get]] internal method."
  44366. * The current implementation assumes this means that bound functions
  44367. * should not have the special [[Get]] behavior.
  44368. *
  44369. * The E5.1 spec is also a bit unclear if the TypeError throwing is
  44370. * applied if the 'caller' value is a strict bound function. The
  44371. * current implementation will throw even for both strict non-bound
  44372. * and strict bound functions.
  44373. *
  44374. * See test-dev-strict-func-as-caller-prop-value.js for quite extensive
  44375. * tests.
  44376. *
  44377. * This exotic behavior is disabled when the non-standard 'caller' property
  44378. * is enabled, as it conflicts with the free use of 'caller'.
  44379. */
  44380. if (key == DUK_HTHREAD_STRING_CALLER(thr) &&
  44381. DUK_TVAL_IS_OBJECT(tv_obj)) {
  44382. duk_hobject *orig = DUK_TVAL_GET_OBJECT(tv_obj);
  44383. DUK_ASSERT(orig != NULL);
  44384. if (DUK_HOBJECT_IS_NONBOUND_FUNCTION(orig) ||
  44385. DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(orig)) {
  44386. duk_hobject *h;
  44387. /* XXX: The TypeError is currently not applied to bound
  44388. * functions because the 'strict' flag is not copied by
  44389. * bind(). This may or may not be correct, the specification
  44390. * only refers to the value being a "strict mode Function
  44391. * object" which is ambiguous.
  44392. */
  44393. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(orig));
  44394. h = duk_get_hobject(ctx, -1); /* NULL if not an object */
  44395. if (h &&
  44396. DUK_HOBJECT_IS_FUNCTION(h) &&
  44397. DUK_HOBJECT_HAS_STRICT(h)) {
  44398. /* XXX: sufficient to check 'strict', assert for 'is function' */
  44399. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_STRICT_CALLER_READ);
  44400. }
  44401. }
  44402. }
  44403. #endif /* !DUK_USE_NONSTD_FUNC_CALLER_PROPERTY */
  44404. duk_remove(ctx, -2); /* [key result] -> [result] */
  44405. DUK_DDD(DUK_DDDPRINT("-> %!T (found)", (duk_tval *) duk_get_tval(ctx, -1)));
  44406. return 1;
  44407. }
  44408. /*
  44409. * HASPROP: Ecmascript property existence check ("in" operator).
  44410. *
  44411. * Interestingly, the 'in' operator does not do any coercion of
  44412. * the target object.
  44413. */
  44414. DUK_INTERNAL duk_bool_t duk_hobject_hasprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key) {
  44415. duk_context *ctx = (duk_context *) thr;
  44416. duk_tval tv_key_copy;
  44417. duk_hobject *obj;
  44418. duk_hstring *key;
  44419. duk_uint32_t arr_idx;
  44420. duk_bool_t rc;
  44421. duk_propdesc desc;
  44422. DUK_DDD(DUK_DDDPRINT("hasprop: thr=%p, obj=%p, key=%p (obj -> %!T, key -> %!T)",
  44423. (void *) thr, (void *) tv_obj, (void *) tv_key,
  44424. (duk_tval *) tv_obj, (duk_tval *) tv_key));
  44425. DUK_ASSERT(thr != NULL);
  44426. DUK_ASSERT(thr->heap != NULL);
  44427. DUK_ASSERT(tv_obj != NULL);
  44428. DUK_ASSERT(tv_key != NULL);
  44429. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  44430. DUK_TVAL_SET_TVAL(&tv_key_copy, tv_key);
  44431. tv_key = &tv_key_copy;
  44432. /*
  44433. * The 'in' operator requires an object as its right hand side,
  44434. * throwing a TypeError unconditionally if this is not the case.
  44435. *
  44436. * However, lightfuncs need to behave like fully fledged objects
  44437. * here to be maximally transparent, so we need to handle them
  44438. * here.
  44439. */
  44440. /* XXX: Refactor key coercion so that it's only called once. It can't
  44441. * be trivially lifted here because the object must be type checked
  44442. * first.
  44443. */
  44444. if (DUK_TVAL_IS_OBJECT(tv_obj)) {
  44445. obj = DUK_TVAL_GET_OBJECT(tv_obj);
  44446. DUK_ASSERT(obj != NULL);
  44447. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  44448. } else if (DUK_TVAL_IS_LIGHTFUNC(tv_obj)) {
  44449. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  44450. if (duk__key_is_lightfunc_ownprop(thr, key)) {
  44451. /* FOUND */
  44452. rc = 1;
  44453. goto pop_and_return;
  44454. }
  44455. /* If not found, resume existence check from Function.prototype.
  44456. * We can just substitute the value in this case; nothing will
  44457. * need the original base value (as would be the case with e.g.
  44458. * setters/getters.
  44459. */
  44460. obj = thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE];
  44461. } else {
  44462. /* Note: unconditional throw */
  44463. DUK_DDD(DUK_DDDPRINT("base object is not an object -> reject"));
  44464. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_BASE);
  44465. }
  44466. /* XXX: fast path for arrays? */
  44467. DUK_ASSERT(key != NULL);
  44468. DUK_ASSERT(obj != NULL);
  44469. DUK_UNREF(arr_idx);
  44470. #if defined(DUK_USE_ES6_PROXY)
  44471. if (DUK_UNLIKELY(DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(obj))) {
  44472. duk_hobject *h_target;
  44473. duk_bool_t tmp_bool;
  44474. /* XXX: the key in 'key in obj' is string coerced before we're called
  44475. * (which is the required behavior in E5/E5.1/E6) so the key is a string
  44476. * here already.
  44477. */
  44478. if (duk__proxy_check_prop(thr, obj, DUK_STRIDX_HAS, tv_key, &h_target)) {
  44479. /* [ ... key trap handler ] */
  44480. DUK_DDD(DUK_DDDPRINT("-> proxy object 'has' for key %!T", (duk_tval *) tv_key));
  44481. duk_push_hobject(ctx, h_target); /* target */
  44482. duk_push_tval(ctx, tv_key); /* P */
  44483. duk_call_method(ctx, 2 /*nargs*/);
  44484. tmp_bool = duk_to_boolean(ctx, -1);
  44485. if (!tmp_bool) {
  44486. /* Target object must be checked for a conflicting
  44487. * non-configurable property.
  44488. */
  44489. if (duk__get_own_property_desc_raw(thr, h_target, key, arr_idx, &desc, 0 /*flags*/)) { /* don't push value */
  44490. DUK_DDD(DUK_DDDPRINT("proxy 'has': target has matching property %!O, check for "
  44491. "conflicting property; desc.flags=0x%08lx, "
  44492. "desc.get=%p, desc.set=%p",
  44493. (duk_heaphdr *) key, (unsigned long) desc.flags,
  44494. (void *) desc.get, (void *) desc.set));
  44495. /* XXX: Extensibility check for target uses IsExtensible(). If we
  44496. * implemented the isExtensible trap and didn't reject proxies as
  44497. * proxy targets, it should be respected here.
  44498. */
  44499. if (!((desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && /* property is configurable and */
  44500. DUK_HOBJECT_HAS_EXTENSIBLE(h_target))) { /* ... target is extensible */
  44501. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROXY_REJECTED);
  44502. }
  44503. }
  44504. }
  44505. duk_pop_2(ctx); /* [ key trap_result ] -> [] */
  44506. return tmp_bool;
  44507. }
  44508. obj = h_target; /* resume check from proxy target */
  44509. }
  44510. #endif /* DUK_USE_ES6_PROXY */
  44511. /* XXX: inline into a prototype walking loop? */
  44512. rc = duk__get_property_desc(thr, obj, key, &desc, 0 /*flags*/); /* don't push value */
  44513. /* fall through */
  44514. pop_and_return:
  44515. duk_pop(ctx); /* [ key ] -> [] */
  44516. return rc;
  44517. }
  44518. /*
  44519. * HASPROP variant used internally.
  44520. *
  44521. * This primitive must never throw an error, callers rely on this.
  44522. * In particular, don't throw an error for prototype loops; instead,
  44523. * pretend like the property doesn't exist if a prototype sanity limit
  44524. * is reached.
  44525. *
  44526. * Does not implement proxy behavior: if applied to a proxy object,
  44527. * returns key existence on the proxy object itself.
  44528. */
  44529. DUK_INTERNAL duk_bool_t duk_hobject_hasprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key) {
  44530. duk_propdesc dummy;
  44531. DUK_ASSERT(thr != NULL);
  44532. DUK_ASSERT(thr->heap != NULL);
  44533. DUK_ASSERT(obj != NULL);
  44534. DUK_ASSERT(key != NULL);
  44535. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  44536. return duk__get_property_desc(thr, obj, key, &dummy, DUK__DESC_FLAG_IGNORE_PROTOLOOP); /* don't push value */
  44537. }
  44538. /*
  44539. * Helper: handle Array object 'length' write which automatically
  44540. * deletes properties, see E5 Section 15.4.5.1, step 3. This is
  44541. * quite tricky to get right.
  44542. *
  44543. * Used by duk_hobject_putprop().
  44544. */
  44545. DUK_LOCAL duk_uint32_t duk__get_old_array_length(duk_hthread *thr, duk_hobject *obj, duk_propdesc *temp_desc) {
  44546. duk_bool_t rc;
  44547. duk_tval *tv;
  44548. duk_uint32_t res;
  44549. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  44550. /* This function is only called for objects with array exotic behavior.
  44551. * The [[DefineOwnProperty]] algorithm for arrays requires that
  44552. * 'length' can never have a value outside the unsigned 32-bit range,
  44553. * attempt to write such a value is a RangeError. Here we can thus
  44554. * assert for this. When Duktape internals go around the official
  44555. * property write interface (doesn't happen often) this assumption is
  44556. * easy to accidentally break, so such code must be written carefully.
  44557. * See test-bi-array-push-maxlen.js.
  44558. */
  44559. rc = duk__get_own_property_desc_raw(thr, obj, DUK_HTHREAD_STRING_LENGTH(thr), DUK__NO_ARRAY_INDEX, temp_desc, 0 /*flags*/); /* don't push value */
  44560. DUK_UNREF(rc);
  44561. DUK_ASSERT(rc != 0); /* arrays MUST have a 'length' property */
  44562. DUK_ASSERT(temp_desc->e_idx >= 0);
  44563. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, temp_desc->e_idx);
  44564. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); /* array 'length' is always a number, as we coerce it */
  44565. DUK_ASSERT(DUK_TVAL_GET_NUMBER(tv) >= 0.0);
  44566. DUK_ASSERT(DUK_TVAL_GET_NUMBER(tv) <= (double) 0xffffffffUL);
  44567. DUK_ASSERT((duk_double_t) (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv) == DUK_TVAL_GET_NUMBER(tv));
  44568. #if defined(DUK_USE_FASTINT)
  44569. /* Downgrade checks are not made everywhere, so 'length' is not always
  44570. * a fastint (it is a number though). This can be removed once length
  44571. * is always guaranteed to be a fastint.
  44572. */
  44573. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv) || DUK_TVAL_IS_DOUBLE(tv));
  44574. if (DUK_TVAL_IS_FASTINT(tv)) {
  44575. res = (duk_uint32_t) DUK_TVAL_GET_FASTINT_U32(tv);
  44576. } else {
  44577. res = (duk_uint32_t) DUK_TVAL_GET_DOUBLE(tv);
  44578. }
  44579. #else
  44580. res = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv);
  44581. #endif /* DUK_USE_FASTINT */
  44582. return res;
  44583. }
  44584. DUK_LOCAL duk_uint32_t duk__to_new_array_length_checked(duk_hthread *thr) {
  44585. duk_context *ctx = (duk_context *) thr;
  44586. duk_uint32_t res;
  44587. duk_double_t d;
  44588. /* Input value should be on stack top and will be coerced and
  44589. * popped. Refuse to update an Array's 'length' to a value
  44590. * outside the 32-bit range. Negative zero is accepted as zero.
  44591. */
  44592. /* XXX: fastint */
  44593. d = duk_to_number(ctx, -1);
  44594. res = (duk_uint32_t) d;
  44595. if ((duk_double_t) res != d) {
  44596. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_INVALID_ARRAY_LENGTH);
  44597. }
  44598. duk_pop(ctx);
  44599. return res;
  44600. }
  44601. /* Delete elements required by a smaller length, taking into account
  44602. * potentially non-configurable elements. Returns non-zero if all
  44603. * elements could be deleted, and zero if all or some elements could
  44604. * not be deleted. Also writes final "target length" to 'out_result_len'.
  44605. * This is the length value that should go into the 'length' property
  44606. * (must be set by the caller). Never throws an error.
  44607. */
  44608. DUK_LOCAL
  44609. duk_bool_t duk__handle_put_array_length_smaller(duk_hthread *thr,
  44610. duk_hobject *obj,
  44611. duk_uint32_t old_len,
  44612. duk_uint32_t new_len,
  44613. duk_bool_t force_flag,
  44614. duk_uint32_t *out_result_len) {
  44615. duk_uint32_t target_len;
  44616. duk_uint_fast32_t i;
  44617. duk_uint32_t arr_idx;
  44618. duk_hstring *key;
  44619. duk_tval *tv;
  44620. duk_bool_t rc;
  44621. DUK_DDD(DUK_DDDPRINT("new array length smaller than old (%ld -> %ld), "
  44622. "probably need to remove elements",
  44623. (long) old_len, (long) new_len));
  44624. /*
  44625. * New length is smaller than old length, need to delete properties above
  44626. * the new length.
  44627. *
  44628. * If array part exists, this is straightforward: array entries cannot
  44629. * be non-configurable so this is guaranteed to work.
  44630. *
  44631. * If array part does not exist, array-indexed values are scattered
  44632. * in the entry part, and some may not be configurable (preventing length
  44633. * from becoming lower than their index + 1). To handle the algorithm
  44634. * in E5 Section 15.4.5.1, step l correctly, we scan the entire property
  44635. * set twice.
  44636. */
  44637. DUK_ASSERT(thr != NULL);
  44638. DUK_ASSERT(obj != NULL);
  44639. DUK_ASSERT(new_len < old_len);
  44640. DUK_ASSERT(out_result_len != NULL);
  44641. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  44642. if (DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  44643. /*
  44644. * All defined array-indexed properties are in the array part
  44645. * (we assume the array part is comprehensive), and all array
  44646. * entries are writable, configurable, and enumerable. Thus,
  44647. * nothing can prevent array entries from being deleted.
  44648. */
  44649. DUK_DDD(DUK_DDDPRINT("have array part, easy case"));
  44650. if (old_len < DUK_HOBJECT_GET_ASIZE(obj)) {
  44651. /* XXX: assertion that entries >= old_len are already unused */
  44652. i = old_len;
  44653. } else {
  44654. i = DUK_HOBJECT_GET_ASIZE(obj);
  44655. }
  44656. DUK_ASSERT(i <= DUK_HOBJECT_GET_ASIZE(obj));
  44657. while (i > new_len) {
  44658. i--;
  44659. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, i);
  44660. DUK_TVAL_SET_UNUSED_UPDREF(thr, tv); /* side effects */
  44661. }
  44662. *out_result_len = new_len;
  44663. return 1;
  44664. } else {
  44665. /*
  44666. * Entries part is a bit more complex
  44667. */
  44668. /* Stage 1: find highest preventing non-configurable entry (if any).
  44669. * When forcing, ignore non-configurability.
  44670. */
  44671. DUK_DDD(DUK_DDDPRINT("no array part, slow case"));
  44672. DUK_DDD(DUK_DDDPRINT("array length write, no array part, stage 1: find target_len "
  44673. "(highest preventing non-configurable entry (if any))"));
  44674. target_len = new_len;
  44675. if (force_flag) {
  44676. DUK_DDD(DUK_DDDPRINT("array length write, no array part; force flag -> skip stage 1"));
  44677. goto skip_stage1;
  44678. }
  44679. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  44680. key = DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i);
  44681. if (!key) {
  44682. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: null key", (long) i));
  44683. continue;
  44684. }
  44685. if (!DUK_HSTRING_HAS_ARRIDX(key)) {
  44686. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: key not an array index", (long) i));
  44687. continue;
  44688. }
  44689. DUK_ASSERT(DUK_HSTRING_HAS_ARRIDX(key)); /* XXX: macro checks for array index flag, which is unnecessary here */
  44690. arr_idx = DUK_HSTRING_GET_ARRIDX_SLOW(key);
  44691. DUK_ASSERT(arr_idx != DUK__NO_ARRAY_INDEX);
  44692. DUK_ASSERT(arr_idx < old_len); /* consistency requires this */
  44693. if (arr_idx < new_len) {
  44694. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: key is array index %ld, below new_len",
  44695. (long) i, (long) arr_idx));
  44696. continue;
  44697. }
  44698. if (DUK_HOBJECT_E_SLOT_IS_CONFIGURABLE(thr->heap, obj, i)) {
  44699. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: key is a relevant array index %ld, but configurable",
  44700. (long) i, (long) arr_idx));
  44701. continue;
  44702. }
  44703. /* relevant array index is non-configurable, blocks write */
  44704. if (arr_idx >= target_len) {
  44705. DUK_DDD(DUK_DDDPRINT("entry at index %ld has arr_idx %ld, is not configurable, "
  44706. "update target_len %ld -> %ld",
  44707. (long) i, (long) arr_idx, (long) target_len,
  44708. (long) (arr_idx + 1)));
  44709. target_len = arr_idx + 1;
  44710. }
  44711. }
  44712. skip_stage1:
  44713. /* stage 2: delete configurable entries above target length */
  44714. DUK_DDD(DUK_DDDPRINT("old_len=%ld, new_len=%ld, target_len=%ld",
  44715. (long) old_len, (long) new_len, (long) target_len));
  44716. DUK_DDD(DUK_DDDPRINT("array length write, no array part, stage 2: remove "
  44717. "entries >= target_len"));
  44718. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  44719. key = DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i);
  44720. if (!key) {
  44721. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: null key", (long) i));
  44722. continue;
  44723. }
  44724. if (!DUK_HSTRING_HAS_ARRIDX(key)) {
  44725. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: key not an array index", (long) i));
  44726. continue;
  44727. }
  44728. DUK_ASSERT(DUK_HSTRING_HAS_ARRIDX(key)); /* XXX: macro checks for array index flag, which is unnecessary here */
  44729. arr_idx = DUK_HSTRING_GET_ARRIDX_SLOW(key);
  44730. DUK_ASSERT(arr_idx != DUK__NO_ARRAY_INDEX);
  44731. DUK_ASSERT(arr_idx < old_len); /* consistency requires this */
  44732. if (arr_idx < target_len) {
  44733. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: key is array index %ld, below target_len",
  44734. (long) i, (long) arr_idx));
  44735. continue;
  44736. }
  44737. DUK_ASSERT(force_flag || DUK_HOBJECT_E_SLOT_IS_CONFIGURABLE(thr->heap, obj, i)); /* stage 1 guarantees */
  44738. DUK_DDD(DUK_DDDPRINT("delete entry index %ld: key is array index %ld",
  44739. (long) i, (long) arr_idx));
  44740. /*
  44741. * Slow delete, but we don't care as we're already in a very slow path.
  44742. * The delete always succeeds: key has no exotic behavior, property
  44743. * is configurable, and no resize occurs.
  44744. */
  44745. rc = duk_hobject_delprop_raw(thr, obj, key, force_flag ? DUK_DELPROP_FLAG_FORCE : 0);
  44746. DUK_UNREF(rc);
  44747. DUK_ASSERT(rc != 0);
  44748. }
  44749. /* stage 3: update length (done by caller), decide return code */
  44750. DUK_DDD(DUK_DDDPRINT("array length write, no array part, stage 3: update length (done by caller)"));
  44751. *out_result_len = target_len;
  44752. if (target_len == new_len) {
  44753. DUK_DDD(DUK_DDDPRINT("target_len matches new_len, return success"));
  44754. return 1;
  44755. }
  44756. DUK_DDD(DUK_DDDPRINT("target_len does not match new_len (some entry prevented "
  44757. "full length adjustment), return error"));
  44758. return 0;
  44759. }
  44760. DUK_UNREACHABLE();
  44761. }
  44762. /* XXX: is valstack top best place for argument? */
  44763. DUK_LOCAL duk_bool_t duk__handle_put_array_length(duk_hthread *thr, duk_hobject *obj) {
  44764. duk_context *ctx = (duk_context *) thr;
  44765. duk_propdesc desc;
  44766. duk_uint32_t old_len;
  44767. duk_uint32_t new_len;
  44768. duk_uint32_t result_len;
  44769. duk_tval *tv;
  44770. duk_bool_t rc;
  44771. DUK_DDD(DUK_DDDPRINT("handling a put operation to array 'length' exotic property, "
  44772. "new val: %!T",
  44773. (duk_tval *) duk_get_tval(ctx, -1)));
  44774. DUK_ASSERT(thr != NULL);
  44775. DUK_ASSERT(ctx != NULL);
  44776. DUK_ASSERT(obj != NULL);
  44777. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  44778. DUK_ASSERT(duk_is_valid_index(ctx, -1));
  44779. /*
  44780. * Get old and new length
  44781. */
  44782. old_len = duk__get_old_array_length(thr, obj, &desc);
  44783. duk_dup(ctx, -1); /* [in_val in_val] */
  44784. new_len = duk__to_new_array_length_checked(thr); /* -> [in_val] */
  44785. DUK_DDD(DUK_DDDPRINT("old_len=%ld, new_len=%ld", (long) old_len, (long) new_len));
  44786. /*
  44787. * Writability check
  44788. */
  44789. if (!(desc.flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  44790. DUK_DDD(DUK_DDDPRINT("length is not writable, fail"));
  44791. return 0;
  44792. }
  44793. /*
  44794. * New length not lower than old length => no changes needed
  44795. * (not even array allocation).
  44796. */
  44797. if (new_len >= old_len) {
  44798. DUK_DDD(DUK_DDDPRINT("new length is higher than old length, just update length, no deletions"));
  44799. DUK_ASSERT(desc.e_idx >= 0);
  44800. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, desc.e_idx));
  44801. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, desc.e_idx);
  44802. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  44803. /* no decref needed for a number */
  44804. #if defined(DUK_USE_FASTINT)
  44805. DUK_TVAL_SET_FASTINT_U32(tv, new_len);
  44806. #else
  44807. DUK_TVAL_SET_NUMBER(tv, (duk_double_t) new_len);
  44808. #endif
  44809. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  44810. return 1;
  44811. }
  44812. DUK_DDD(DUK_DDDPRINT("new length is lower than old length, probably must delete entries"));
  44813. /*
  44814. * New length lower than old length => delete elements, then
  44815. * update length.
  44816. *
  44817. * Note: even though a bunch of elements have been deleted, the 'desc' is
  44818. * still valid as properties haven't been resized (and entries compacted).
  44819. */
  44820. rc = duk__handle_put_array_length_smaller(thr, obj, old_len, new_len, 0 /*force_flag*/, &result_len);
  44821. DUK_ASSERT(result_len >= new_len && result_len <= old_len);
  44822. DUK_ASSERT(desc.e_idx >= 0);
  44823. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, desc.e_idx));
  44824. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, desc.e_idx);
  44825. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  44826. /* no decref needed for a number */
  44827. #if defined(DUK_USE_FASTINT)
  44828. DUK_TVAL_SET_FASTINT_U32(tv, result_len);
  44829. #else
  44830. DUK_TVAL_SET_NUMBER(tv, (duk_double_t) result_len);
  44831. #endif
  44832. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  44833. /* XXX: shrink array allocation or entries compaction here? */
  44834. return rc;
  44835. }
  44836. /*
  44837. * PUTPROP: Ecmascript property write.
  44838. *
  44839. * Unlike Ecmascript primitive which returns nothing, returns 1 to indicate
  44840. * success and 0 to indicate failure (assuming throw is not set).
  44841. *
  44842. * This is an extremely tricky function. Some examples:
  44843. *
  44844. * * Currently a decref may trigger a GC, which may compact an object's
  44845. * property allocation. Consequently, any entry indices (e_idx) will
  44846. * be potentially invalidated by a decref.
  44847. *
  44848. * * Exotic behaviors (strings, arrays, arguments object) require,
  44849. * among other things:
  44850. *
  44851. * - Preprocessing before and postprocessing after an actual property
  44852. * write. For example, array index write requires pre-checking the
  44853. * array 'length' property for access control, and may require an
  44854. * array 'length' update after the actual write has succeeded (but
  44855. * not if it fails).
  44856. *
  44857. * - Deletion of multiple entries, as a result of array 'length' write.
  44858. *
  44859. * * Input values are taken as pointers which may point to the valstack.
  44860. * If valstack is resized because of the put (this may happen at least
  44861. * when the array part is abandoned), the pointers can be invalidated.
  44862. * (We currently make a copy of all of the input values to avoid issues.)
  44863. */
  44864. 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) {
  44865. duk_context *ctx = (duk_context *) thr;
  44866. duk_tval tv_obj_copy;
  44867. duk_tval tv_key_copy;
  44868. duk_tval tv_val_copy;
  44869. duk_hobject *orig = NULL; /* NULL if tv_obj is primitive */
  44870. duk_hobject *curr;
  44871. duk_hstring *key = NULL;
  44872. duk_propdesc desc;
  44873. duk_tval *tv;
  44874. duk_uint32_t arr_idx;
  44875. duk_bool_t rc;
  44876. duk_int_t e_idx;
  44877. duk_uint_t sanity;
  44878. duk_uint32_t new_array_length = 0; /* 0 = no update */
  44879. DUK_DDD(DUK_DDDPRINT("putprop: thr=%p, obj=%p, key=%p, val=%p, throw=%ld "
  44880. "(obj -> %!T, key -> %!T, val -> %!T)",
  44881. (void *) thr, (void *) tv_obj, (void *) tv_key, (void *) tv_val,
  44882. (long) throw_flag, (duk_tval *) tv_obj, (duk_tval *) tv_key, (duk_tval *) tv_val));
  44883. DUK_ASSERT(thr != NULL);
  44884. DUK_ASSERT(thr->heap != NULL);
  44885. DUK_ASSERT(ctx != NULL);
  44886. DUK_ASSERT(tv_obj != NULL);
  44887. DUK_ASSERT(tv_key != NULL);
  44888. DUK_ASSERT(tv_val != NULL);
  44889. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  44890. /*
  44891. * Make a copy of tv_obj, tv_key, and tv_val to avoid any issues of
  44892. * them being invalidated by a valstack resize.
  44893. *
  44894. * XXX: this is an overkill for some paths, so optimize this later
  44895. * (or maybe switch to a stack arguments model entirely).
  44896. */
  44897. DUK_TVAL_SET_TVAL(&tv_obj_copy, tv_obj);
  44898. DUK_TVAL_SET_TVAL(&tv_key_copy, tv_key);
  44899. DUK_TVAL_SET_TVAL(&tv_val_copy, tv_val);
  44900. tv_obj = &tv_obj_copy;
  44901. tv_key = &tv_key_copy;
  44902. tv_val = &tv_val_copy;
  44903. /*
  44904. * Coercion and fast path processing.
  44905. */
  44906. switch (DUK_TVAL_GET_TAG(tv_obj)) {
  44907. case DUK_TAG_UNDEFINED:
  44908. case DUK_TAG_NULL: {
  44909. /* Note: unconditional throw */
  44910. DUK_DDD(DUK_DDDPRINT("base object is undefined or null -> reject (object=%!iT)",
  44911. (duk_tval *) tv_obj));
  44912. #if defined(DUK_USE_VERBOSE_PROP_ERRORS)
  44913. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "cannot write property %s of %s",
  44914. duk_push_string_tval_readable(ctx, tv_key), duk_push_string_tval_readable(ctx, tv_obj));
  44915. #else
  44916. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_BASE);
  44917. #endif
  44918. return 0;
  44919. }
  44920. case DUK_TAG_BOOLEAN: {
  44921. DUK_DDD(DUK_DDDPRINT("base object is a boolean, start lookup from boolean prototype"));
  44922. curr = thr->builtins[DUK_BIDX_BOOLEAN_PROTOTYPE];
  44923. break;
  44924. }
  44925. case DUK_TAG_STRING: {
  44926. duk_hstring *h = DUK_TVAL_GET_STRING(tv_obj);
  44927. /*
  44928. * Note: currently no fast path for array index writes.
  44929. * They won't be possible anyway as strings are immutable.
  44930. */
  44931. DUK_ASSERT(key == NULL);
  44932. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  44933. DUK_ASSERT(key != NULL);
  44934. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  44935. goto fail_not_writable;
  44936. }
  44937. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  44938. arr_idx < DUK_HSTRING_GET_CHARLEN(h)) {
  44939. goto fail_not_writable;
  44940. }
  44941. DUK_DDD(DUK_DDDPRINT("base object is a string, start lookup from string prototype"));
  44942. curr = thr->builtins[DUK_BIDX_STRING_PROTOTYPE];
  44943. goto lookup; /* avoid double coercion */
  44944. }
  44945. case DUK_TAG_OBJECT: {
  44946. orig = DUK_TVAL_GET_OBJECT(tv_obj);
  44947. DUK_ASSERT(orig != NULL);
  44948. /* The fast path for array property put is not fully compliant:
  44949. * If one places conflicting number-indexed properties into
  44950. * Array.prototype (for example, a non-writable Array.prototype[7])
  44951. * the fast path will incorrectly ignore them.
  44952. *
  44953. * This fast path could be made compliant by falling through
  44954. * to the slow path if the previous value was UNUSED. This would
  44955. * also remove the need to check for extensibility. Right now a
  44956. * non-extensible array is slower than an extensible one as far
  44957. * as writes are concerned.
  44958. *
  44959. * The fast path behavior is documented in more detail here:
  44960. * tests/ecmascript/test-misc-array-fast-write.js
  44961. */
  44962. if (duk__putprop_shallow_fastpath_array_tval(thr, orig, tv_key, tv_val, &desc) != 0) {
  44963. DUK_DDD(DUK_DDDPRINT("array fast path success"));
  44964. return 1;
  44965. }
  44966. if (duk__putprop_fastpath_bufobj_tval(thr, orig, tv_key, tv_val) != 0) {
  44967. DUK_DDD(DUK_DDDPRINT("base is bufobj, key is a number, bufferobject fast path"));
  44968. return 1;
  44969. }
  44970. #if defined(DUK_USE_ES6_PROXY)
  44971. if (DUK_UNLIKELY(DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(orig))) {
  44972. duk_hobject *h_target;
  44973. duk_bool_t tmp_bool;
  44974. if (duk__proxy_check_prop(thr, orig, DUK_STRIDX_SET, tv_key, &h_target)) {
  44975. /* -> [ ... trap handler ] */
  44976. DUK_DDD(DUK_DDDPRINT("-> proxy object 'set' for key %!T", (duk_tval *) tv_key));
  44977. duk_push_hobject(ctx, h_target); /* target */
  44978. duk_push_tval(ctx, tv_key); /* P */
  44979. duk_push_tval(ctx, tv_val); /* V */
  44980. duk_push_tval(ctx, tv_obj); /* Receiver: Proxy object */
  44981. duk_call_method(ctx, 4 /*nargs*/);
  44982. tmp_bool = duk_to_boolean(ctx, -1);
  44983. duk_pop(ctx);
  44984. if (!tmp_bool) {
  44985. goto fail_proxy_rejected;
  44986. }
  44987. /* Target object must be checked for a conflicting
  44988. * non-configurable property.
  44989. */
  44990. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  44991. DUK_ASSERT(key != NULL);
  44992. if (duk__get_own_property_desc_raw(thr, h_target, key, arr_idx, &desc, DUK__DESC_FLAG_PUSH_VALUE)) {
  44993. duk_tval *tv_targ = duk_require_tval(ctx, -1);
  44994. duk_bool_t datadesc_reject;
  44995. duk_bool_t accdesc_reject;
  44996. DUK_DDD(DUK_DDDPRINT("proxy 'set': target has matching property %!O, check for "
  44997. "conflicting property; tv_val=%!T, tv_targ=%!T, desc.flags=0x%08lx, "
  44998. "desc.get=%p, desc.set=%p",
  44999. (duk_heaphdr *) key, (duk_tval *) tv_val, (duk_tval *) tv_targ,
  45000. (unsigned long) desc.flags,
  45001. (void *) desc.get, (void *) desc.set));
  45002. datadesc_reject = !(desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) &&
  45003. !(desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) &&
  45004. !(desc.flags & DUK_PROPDESC_FLAG_WRITABLE) &&
  45005. !duk_js_samevalue(tv_val, tv_targ);
  45006. accdesc_reject = (desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) &&
  45007. !(desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) &&
  45008. (desc.set == NULL);
  45009. if (datadesc_reject || accdesc_reject) {
  45010. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROXY_REJECTED);
  45011. }
  45012. duk_pop_2(ctx);
  45013. } else {
  45014. duk_pop(ctx);
  45015. }
  45016. return 1; /* success */
  45017. }
  45018. orig = h_target; /* resume write to target */
  45019. DUK_TVAL_SET_OBJECT(tv_obj, orig);
  45020. }
  45021. #endif /* DUK_USE_ES6_PROXY */
  45022. curr = orig;
  45023. break;
  45024. }
  45025. case DUK_TAG_BUFFER: {
  45026. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv_obj);
  45027. duk_int_t pop_count = 0;
  45028. /*
  45029. * Because buffer values may be looped over and read/written
  45030. * from, an array index fast path is important.
  45031. */
  45032. #if defined(DUK_USE_FASTINT)
  45033. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  45034. arr_idx = duk__tval_fastint_to_arr_idx(tv_key);
  45035. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a fast-path fastint; arr_idx %ld", (long) arr_idx));
  45036. pop_count = 0;
  45037. } else
  45038. #endif
  45039. if (DUK_TVAL_IS_NUMBER(tv_key)) {
  45040. arr_idx = duk__tval_number_to_arr_idx(tv_key);
  45041. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a fast-path number; arr_idx %ld", (long) arr_idx));
  45042. pop_count = 0;
  45043. } else {
  45044. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  45045. DUK_ASSERT(key != NULL);
  45046. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a non-fast-path number; after "
  45047. "coercion key is %!T, arr_idx %ld",
  45048. (duk_tval *) duk_get_tval(ctx, -1), (long) arr_idx));
  45049. pop_count = 1;
  45050. }
  45051. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  45052. arr_idx < DUK_HBUFFER_GET_SIZE(h)) {
  45053. duk_uint8_t *data;
  45054. DUK_DDD(DUK_DDDPRINT("writing to buffer data at index %ld", (long) arr_idx));
  45055. data = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h);
  45056. /* XXX: duk_to_int() ensures we'll get 8 lowest bits as
  45057. * as input is within duk_int_t range (capped outside it).
  45058. */
  45059. #if defined(DUK_USE_FASTINT)
  45060. /* Buffer writes are often integers. */
  45061. if (DUK_TVAL_IS_FASTINT(tv_val)) {
  45062. data[arr_idx] = (duk_uint8_t) DUK_TVAL_GET_FASTINT_U32(tv_val);
  45063. }
  45064. else
  45065. #endif
  45066. {
  45067. duk_push_tval(ctx, tv_val);
  45068. data[arr_idx] = (duk_uint8_t) duk_to_uint32(ctx, -1);
  45069. pop_count++;
  45070. }
  45071. duk_pop_n(ctx, pop_count);
  45072. DUK_DDD(DUK_DDDPRINT("result: success (buffer data write)"));
  45073. return 1;
  45074. }
  45075. if (pop_count == 0) {
  45076. /* This is a pretty awkward control flow, but we need to recheck the
  45077. * key coercion here.
  45078. */
  45079. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  45080. DUK_ASSERT(key != NULL);
  45081. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a non-fast-path number; after "
  45082. "coercion key is %!T, arr_idx %ld",
  45083. (duk_tval *) duk_get_tval(ctx, -1), (long) arr_idx));
  45084. }
  45085. if (key == DUK_HTHREAD_STRING_LENGTH(thr) ||
  45086. key == DUK_HTHREAD_STRING_BYTE_LENGTH(thr) ||
  45087. key == DUK_HTHREAD_STRING_BYTE_OFFSET(thr) ||
  45088. key == DUK_HTHREAD_STRING_BYTES_PER_ELEMENT(thr)) {
  45089. goto fail_not_writable;
  45090. }
  45091. DUK_DDD(DUK_DDDPRINT("base object is a buffer, start lookup from buffer prototype"));
  45092. curr = thr->builtins[DUK_BIDX_BUFFER_PROTOTYPE];
  45093. goto lookup; /* avoid double coercion */
  45094. }
  45095. case DUK_TAG_POINTER: {
  45096. DUK_DDD(DUK_DDDPRINT("base object is a pointer, start lookup from pointer prototype"));
  45097. curr = thr->builtins[DUK_BIDX_POINTER_PROTOTYPE];
  45098. break;
  45099. }
  45100. case DUK_TAG_LIGHTFUNC: {
  45101. /* All lightfunc own properties are non-writable and the lightfunc
  45102. * is considered non-extensible. However, the write may be captured
  45103. * by an inherited setter which means we can't stop the lookup here.
  45104. */
  45105. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  45106. if (duk__key_is_lightfunc_ownprop(thr, key)) {
  45107. goto fail_not_writable;
  45108. }
  45109. DUK_DDD(DUK_DDDPRINT("base object is a lightfunc, start lookup from function prototype"));
  45110. curr = thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE];
  45111. goto lookup; /* avoid double coercion */
  45112. }
  45113. #if defined(DUK_USE_FASTINT)
  45114. case DUK_TAG_FASTINT:
  45115. #endif
  45116. default: {
  45117. /* number */
  45118. DUK_DDD(DUK_DDDPRINT("base object is a number, start lookup from number prototype"));
  45119. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_obj));
  45120. curr = thr->builtins[DUK_BIDX_NUMBER_PROTOTYPE];
  45121. break;
  45122. }
  45123. }
  45124. DUK_ASSERT(key == NULL);
  45125. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  45126. DUK_ASSERT(key != NULL);
  45127. lookup:
  45128. /*
  45129. * Check whether the property already exists in the prototype chain.
  45130. * Note that the actual write goes into the original base object
  45131. * (except if an accessor property captures the write).
  45132. */
  45133. /* [key] */
  45134. DUK_ASSERT(curr != NULL);
  45135. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  45136. do {
  45137. if (!duk__get_own_property_desc_raw(thr, curr, key, arr_idx, &desc, 0 /*flags*/)) { /* don't push value */
  45138. goto next_in_chain;
  45139. }
  45140. if (desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  45141. /*
  45142. * Found existing accessor property (own or inherited).
  45143. * Call setter with 'this' set to orig, and value as the only argument.
  45144. *
  45145. * Note: no exotic arguments object behavior, because [[Put]] never
  45146. * calls [[DefineOwnProperty]] (E5 Section 8.12.5, step 5.b).
  45147. */
  45148. duk_hobject *setter;
  45149. DUK_DD(DUK_DDPRINT("put to an own or inherited accessor, calling setter"));
  45150. setter = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, curr, desc.e_idx);
  45151. if (!setter) {
  45152. goto fail_no_setter;
  45153. }
  45154. duk_push_hobject(ctx, setter);
  45155. duk_push_tval(ctx, tv_obj); /* note: original, uncoerced base */
  45156. duk_push_tval(ctx, tv_val); /* [key setter this val] */
  45157. #ifdef DUK_USE_NONSTD_SETTER_KEY_ARGUMENT
  45158. duk_dup(ctx, -4);
  45159. duk_call_method(ctx, 2); /* [key setter this val key] -> [key retval] */
  45160. #else
  45161. duk_call_method(ctx, 1); /* [key setter this val] -> [key retval] */
  45162. #endif
  45163. duk_pop(ctx); /* ignore retval -> [key] */
  45164. goto success_no_arguments_exotic;
  45165. }
  45166. if (orig == NULL) {
  45167. /*
  45168. * Found existing own or inherited plain property, but original
  45169. * base is a primitive value.
  45170. */
  45171. DUK_DD(DUK_DDPRINT("attempt to create a new property in a primitive base object"));
  45172. goto fail_base_primitive;
  45173. }
  45174. if (curr != orig) {
  45175. /*
  45176. * Found existing inherited plain property.
  45177. * Do an access control check, and if OK, write
  45178. * new property to 'orig'.
  45179. */
  45180. if (!DUK_HOBJECT_HAS_EXTENSIBLE(orig)) {
  45181. DUK_DD(DUK_DDPRINT("found existing inherited plain property, but original object is not extensible"));
  45182. goto fail_not_extensible;
  45183. }
  45184. if (!(desc.flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  45185. DUK_DD(DUK_DDPRINT("found existing inherited plain property, original object is extensible, but inherited property is not writable"));
  45186. goto fail_not_writable;
  45187. }
  45188. DUK_DD(DUK_DDPRINT("put to new property, object extensible, inherited property found and is writable"));
  45189. goto create_new;
  45190. } else {
  45191. /*
  45192. * Found existing own (non-inherited) plain property.
  45193. * Do an access control check and update in place.
  45194. */
  45195. if (!(desc.flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  45196. DUK_DD(DUK_DDPRINT("found existing own (non-inherited) plain property, but property is not writable"));
  45197. goto fail_not_writable;
  45198. }
  45199. if (desc.flags & DUK_PROPDESC_FLAG_VIRTUAL) {
  45200. DUK_DD(DUK_DDPRINT("found existing own (non-inherited) virtual property, property is writable"));
  45201. if (DUK_HOBJECT_IS_BUFFEROBJECT(curr)) {
  45202. duk_hbufferobject *h_bufobj;
  45203. duk_uint_t byte_off;
  45204. duk_small_uint_t elem_size;
  45205. h_bufobj = (duk_hbufferobject *) curr;
  45206. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  45207. DUK_DD(DUK_DDPRINT("writable virtual property is in buffer object"));
  45208. /* Careful with wrapping: arr_idx upshift may easily wrap, whereas
  45209. * length downshift won't.
  45210. */
  45211. if (arr_idx < (h_bufobj->length >> h_bufobj->shift)) {
  45212. duk_uint8_t *data;
  45213. DUK_DDD(DUK_DDDPRINT("writing to buffer data at index %ld", (long) arr_idx));
  45214. DUK_ASSERT(arr_idx != DUK__NO_ARRAY_INDEX); /* index/length check guarantees */
  45215. byte_off = arr_idx << h_bufobj->shift; /* no wrap assuming h_bufobj->length is valid */
  45216. elem_size = 1 << h_bufobj->shift;
  45217. /* Coerce to number before validating pointers etc so that the
  45218. * number coercions in duk_hbufferobject_validated_write() are
  45219. * guaranteed to be side effect free and not invalidate the
  45220. * pointer checks we do here.
  45221. */
  45222. duk_push_tval(ctx, tv_val);
  45223. duk_to_number(ctx, -1);
  45224. if (h_bufobj->buf != NULL && DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_bufobj, byte_off + elem_size)) {
  45225. data = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf) + h_bufobj->offset + byte_off;
  45226. duk_hbufferobject_validated_write(ctx, h_bufobj, data, elem_size);
  45227. } else {
  45228. DUK_D(DUK_DPRINT("bufferobject access out of underlying buffer, ignoring (write skipped)"));
  45229. }
  45230. duk_pop(ctx);
  45231. goto success_no_arguments_exotic;
  45232. }
  45233. }
  45234. goto fail_internal; /* should not happen */
  45235. }
  45236. DUK_DD(DUK_DDPRINT("put to existing own plain property, property is writable"));
  45237. goto update_old;
  45238. }
  45239. DUK_UNREACHABLE();
  45240. next_in_chain:
  45241. /* XXX: option to pretend property doesn't exist if sanity limit is
  45242. * hit might be useful.
  45243. */
  45244. if (sanity-- == 0) {
  45245. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  45246. }
  45247. curr = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, curr);
  45248. } while (curr);
  45249. /*
  45250. * Property not found in prototype chain.
  45251. */
  45252. DUK_DDD(DUK_DDDPRINT("property not found in prototype chain"));
  45253. if (orig == NULL) {
  45254. DUK_DD(DUK_DDPRINT("attempt to create a new property in a primitive base object"));
  45255. goto fail_base_primitive;
  45256. }
  45257. if (!DUK_HOBJECT_HAS_EXTENSIBLE(orig)) {
  45258. DUK_DD(DUK_DDPRINT("put to a new property (not found in prototype chain), but original object not extensible"));
  45259. goto fail_not_extensible;
  45260. }
  45261. goto create_new;
  45262. update_old:
  45263. /*
  45264. * Update an existing property of the base object.
  45265. */
  45266. /* [key] */
  45267. DUK_DDD(DUK_DDDPRINT("update an existing property of the original object"));
  45268. DUK_ASSERT(orig != NULL);
  45269. /* Although there are writable virtual properties (e.g. plain buffer
  45270. * and buffer object number indices), they are handled before we come
  45271. * here.
  45272. */
  45273. DUK_ASSERT((desc.flags & DUK_PROPDESC_FLAG_VIRTUAL) == 0);
  45274. DUK_ASSERT(desc.a_idx >= 0 || desc.e_idx >= 0);
  45275. if (DUK_HOBJECT_HAS_EXOTIC_ARRAY(orig) &&
  45276. key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  45277. /*
  45278. * Write to 'length' of an array is a very complex case
  45279. * handled in a helper which updates both the array elements
  45280. * and writes the new 'length'. The write may result in an
  45281. * unconditional RangeError or a partial write (indicated
  45282. * by a return code).
  45283. *
  45284. * Note: the helper has an unnecessary writability check
  45285. * for 'length', we already know it is writable.
  45286. */
  45287. DUK_DDD(DUK_DDDPRINT("writing existing 'length' property to array exotic, invoke complex helper"));
  45288. /* XXX: the helper currently assumes stack top contains new
  45289. * 'length' value and the whole calling convention is not very
  45290. * compatible with what we need.
  45291. */
  45292. duk_push_tval(ctx, tv_val); /* [key val] */
  45293. rc = duk__handle_put_array_length(thr, orig);
  45294. duk_pop(ctx); /* [key val] -> [key] */
  45295. if (!rc) {
  45296. goto fail_array_length_partial;
  45297. }
  45298. /* key is 'length', cannot match argument exotic behavior */
  45299. goto success_no_arguments_exotic;
  45300. }
  45301. if (desc.e_idx >= 0) {
  45302. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, orig, desc.e_idx);
  45303. DUK_DDD(DUK_DDDPRINT("previous entry value: %!iT", (duk_tval *) tv));
  45304. DUK_TVAL_SET_TVAL_UPDREF(thr, tv, tv_val); /* side effects */
  45305. /* don't touch property attributes or hash part */
  45306. DUK_DD(DUK_DDPRINT("put to an existing entry at index %ld -> new value %!iT",
  45307. (long) desc.e_idx, (duk_tval *) tv));
  45308. } else {
  45309. /* Note: array entries are always writable, so the writability check
  45310. * above is pointless for them. The check could be avoided with some
  45311. * refactoring but is probably not worth it.
  45312. */
  45313. DUK_ASSERT(desc.a_idx >= 0);
  45314. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, orig, desc.a_idx);
  45315. DUK_DDD(DUK_DDDPRINT("previous array value: %!iT", (duk_tval *) tv));
  45316. DUK_TVAL_SET_TVAL_UPDREF(thr, tv, tv_val); /* side effects */
  45317. DUK_DD(DUK_DDPRINT("put to an existing array entry at index %ld -> new value %!iT",
  45318. (long) desc.a_idx, (duk_tval *) tv));
  45319. }
  45320. /* Regardless of whether property is found in entry or array part,
  45321. * it may have arguments exotic behavior (array indices may reside
  45322. * in entry part for abandoned / non-existent array parts).
  45323. */
  45324. goto success_with_arguments_exotic;
  45325. create_new:
  45326. /*
  45327. * Create a new property in the original object.
  45328. *
  45329. * Exotic properties need to be reconsidered here from a write
  45330. * perspective (not just property attributes perspective).
  45331. * However, the property does not exist in the object already,
  45332. * so this limits the kind of exotic properties that apply.
  45333. */
  45334. /* [key] */
  45335. DUK_DDD(DUK_DDDPRINT("create new property to original object"));
  45336. DUK_ASSERT(orig != NULL);
  45337. /* Not possible because array object 'length' is present
  45338. * from its creation and cannot be deleted, and is thus
  45339. * caught as an existing property above.
  45340. */
  45341. DUK_ASSERT(!(DUK_HOBJECT_HAS_EXOTIC_ARRAY(orig) &&
  45342. key == DUK_HTHREAD_STRING_LENGTH(thr)));
  45343. if (DUK_HOBJECT_HAS_EXOTIC_ARRAY(orig) &&
  45344. arr_idx != DUK__NO_ARRAY_INDEX) {
  45345. /* automatic length update */
  45346. duk_uint32_t old_len;
  45347. old_len = duk__get_old_array_length(thr, orig, &desc);
  45348. if (arr_idx >= old_len) {
  45349. DUK_DDD(DUK_DDDPRINT("write new array entry requires length update "
  45350. "(arr_idx=%ld, old_len=%ld)",
  45351. (long) arr_idx, (long) old_len));
  45352. if (!(desc.flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  45353. DUK_DD(DUK_DDPRINT("attempt to extend array, but array 'length' is not writable"));
  45354. goto fail_not_writable;
  45355. }
  45356. /* Note: actual update happens once write has been completed
  45357. * without error below. The write should always succeed
  45358. * from a specification viewpoint, but we may e.g. run out
  45359. * of memory. It's safer in this order.
  45360. */
  45361. DUK_ASSERT(arr_idx != 0xffffffffUL);
  45362. new_array_length = arr_idx + 1; /* flag for later write */
  45363. } else {
  45364. DUK_DDD(DUK_DDDPRINT("write new array entry does not require length update "
  45365. "(arr_idx=%ld, old_len=%ld)",
  45366. (long) arr_idx, (long) old_len));
  45367. }
  45368. }
  45369. /* write_to_array_part: */
  45370. /*
  45371. * Write to array part?
  45372. *
  45373. * Note: array abandonding requires a property resize which uses
  45374. * 'rechecks' valstack for temporaries and may cause any existing
  45375. * valstack pointers to be invalidated. To protect against this,
  45376. * tv_obj, tv_key, and tv_val are copies of the original inputs.
  45377. */
  45378. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  45379. DUK_HOBJECT_HAS_ARRAY_PART(orig)) {
  45380. if (arr_idx < DUK_HOBJECT_GET_ASIZE(orig)) {
  45381. goto no_array_growth;
  45382. }
  45383. /*
  45384. * Array needs to grow, but we don't want it becoming too sparse.
  45385. * If it were to become sparse, abandon array part, moving all
  45386. * array entries into the entries part (for good).
  45387. *
  45388. * Since we don't keep track of actual density (used vs. size) of
  45389. * the array part, we need to estimate somehow. The check is made
  45390. * in two parts:
  45391. *
  45392. * - Check whether the resize need is small compared to the
  45393. * current size (relatively); if so, resize without further
  45394. * checking (essentially we assume that the original part is
  45395. * "dense" so that the result would be dense enough).
  45396. *
  45397. * - Otherwise, compute the resize using an actual density
  45398. * measurement based on counting the used array entries.
  45399. */
  45400. DUK_DDD(DUK_DDDPRINT("write to new array requires array resize, decide whether to do a "
  45401. "fast resize without abandon check (arr_idx=%ld, old_size=%ld)",
  45402. (long) arr_idx, (long) DUK_HOBJECT_GET_ASIZE(orig)));
  45403. if (duk__abandon_array_slow_check_required(arr_idx, DUK_HOBJECT_GET_ASIZE(orig))) {
  45404. duk_uint32_t old_used;
  45405. duk_uint32_t old_size;
  45406. DUK_DDD(DUK_DDDPRINT("=> fast check is NOT OK, do slow check for array abandon"));
  45407. duk__compute_a_stats(thr, orig, &old_used, &old_size);
  45408. DUK_DDD(DUK_DDDPRINT("abandon check, array stats: old_used=%ld, old_size=%ld, arr_idx=%ld",
  45409. (long) old_used, (long) old_size, (long) arr_idx));
  45410. /* Note: intentionally use approximations to shave a few instructions:
  45411. * a_used = old_used (accurate: old_used + 1)
  45412. * a_size = arr_idx (accurate: arr_idx + 1)
  45413. */
  45414. if (duk__abandon_array_density_check(old_used, arr_idx)) {
  45415. DUK_DD(DUK_DDPRINT("write to new array entry beyond current length, "
  45416. "decided to abandon array part (would become too sparse)"));
  45417. /* abandoning requires a props allocation resize and
  45418. * 'rechecks' the valstack, invalidating any existing
  45419. * valstack value pointers!
  45420. */
  45421. duk__abandon_array_checked(thr, orig);
  45422. DUK_ASSERT(!DUK_HOBJECT_HAS_ARRAY_PART(orig));
  45423. goto write_to_entry_part;
  45424. }
  45425. DUK_DDD(DUK_DDDPRINT("=> decided to keep array part"));
  45426. } else {
  45427. DUK_DDD(DUK_DDDPRINT("=> fast resize is OK"));
  45428. }
  45429. DUK_DD(DUK_DDPRINT("write to new array entry beyond current length, "
  45430. "decided to extend current allocation"));
  45431. duk__grow_props_for_array_item(thr, orig, arr_idx);
  45432. no_array_growth:
  45433. /* Note: assume array part is comprehensive, so that either
  45434. * the write goes to the array part, or we've abandoned the
  45435. * array above (and will not come here).
  45436. */
  45437. DUK_ASSERT(DUK_HOBJECT_HAS_ARRAY_PART(orig));
  45438. DUK_ASSERT(arr_idx < DUK_HOBJECT_GET_ASIZE(orig));
  45439. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, orig, arr_idx);
  45440. /* prev value must be unused, no decref */
  45441. DUK_ASSERT(DUK_TVAL_IS_UNUSED(tv));
  45442. DUK_TVAL_SET_TVAL(tv, tv_val);
  45443. DUK_TVAL_INCREF(thr, tv);
  45444. DUK_DD(DUK_DDPRINT("put to new array entry: %ld -> %!T",
  45445. (long) arr_idx, (duk_tval *) tv));
  45446. /* Note: array part values are [[Writable]], [[Enumerable]],
  45447. * and [[Configurable]] which matches the required attributes
  45448. * here.
  45449. */
  45450. goto entry_updated;
  45451. }
  45452. write_to_entry_part:
  45453. /*
  45454. * Write to entry part
  45455. */
  45456. /* entry allocation updates hash part and increases the key
  45457. * refcount; may need a props allocation resize but doesn't
  45458. * 'recheck' the valstack.
  45459. */
  45460. e_idx = duk__alloc_entry_checked(thr, orig, key);
  45461. DUK_ASSERT(e_idx >= 0);
  45462. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, orig, e_idx);
  45463. /* prev value can be garbage, no decref */
  45464. DUK_TVAL_SET_TVAL(tv, tv_val);
  45465. DUK_TVAL_INCREF(thr, tv);
  45466. DUK_HOBJECT_E_SET_FLAGS(thr->heap, orig, e_idx, DUK_PROPDESC_FLAGS_WEC);
  45467. goto entry_updated;
  45468. entry_updated:
  45469. /*
  45470. * Possible pending array length update, which must only be done
  45471. * if the actual entry write succeeded.
  45472. */
  45473. if (new_array_length > 0) {
  45474. /*
  45475. * Note: zero works as a "no update" marker because the new length
  45476. * can never be zero after a new property is written.
  45477. *
  45478. * Note: must re-lookup because calls above (e.g. duk__alloc_entry_checked())
  45479. * may realloc and compact properties and hence change e_idx.
  45480. */
  45481. DUK_DDD(DUK_DDDPRINT("write successful, pending array length update to: %ld",
  45482. (long) new_array_length));
  45483. rc = duk__get_own_property_desc_raw(thr, orig, DUK_HTHREAD_STRING_LENGTH(thr), DUK__NO_ARRAY_INDEX, &desc, 0 /*flags*/); /* don't push value */
  45484. DUK_UNREF(rc);
  45485. DUK_ASSERT(rc != 0);
  45486. DUK_ASSERT(desc.e_idx >= 0);
  45487. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, orig, desc.e_idx);
  45488. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  45489. /* no need for decref/incref because value is a number */
  45490. #if defined(DUK_USE_FASTINT)
  45491. DUK_TVAL_SET_FASTINT_U32(tv, new_array_length);
  45492. #else
  45493. DUK_TVAL_SET_NUMBER(tv, (duk_double_t) new_array_length);
  45494. #endif
  45495. }
  45496. /*
  45497. * Arguments exotic behavior not possible for new properties: all
  45498. * magically bound properties are initially present in the arguments
  45499. * object, and if they are deleted, the binding is also removed from
  45500. * parameter map.
  45501. */
  45502. goto success_no_arguments_exotic;
  45503. success_with_arguments_exotic:
  45504. /*
  45505. * Arguments objects have exotic [[DefineOwnProperty]] which updates
  45506. * the internal 'map' of arguments for writes to currently mapped
  45507. * arguments. More conretely, writes to mapped arguments generate
  45508. * a write to a bound variable.
  45509. *
  45510. * The [[Put]] algorithm invokes [[DefineOwnProperty]] for existing
  45511. * data properties and new properties, but not for existing accessors.
  45512. * Hence, in E5 Section 10.6 ([[DefinedOwnProperty]] algorithm), we
  45513. * have a Desc with 'Value' (and possibly other properties too), and
  45514. * we end up in step 5.b.i.
  45515. */
  45516. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  45517. DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(orig)) {
  45518. /* Note: only numbered indices are relevant, so arr_idx fast reject
  45519. * is good (this is valid unless there are more than 4**32-1 arguments).
  45520. */
  45521. DUK_DDD(DUK_DDDPRINT("putprop successful, arguments exotic behavior needed"));
  45522. /* Note: we can reuse 'desc' here */
  45523. /* XXX: top of stack must contain value, which helper doesn't touch,
  45524. * rework to use tv_val directly?
  45525. */
  45526. duk_push_tval(ctx, tv_val);
  45527. (void) duk__check_arguments_map_for_put(thr, orig, key, &desc, throw_flag);
  45528. duk_pop(ctx);
  45529. }
  45530. /* fall thru */
  45531. success_no_arguments_exotic:
  45532. /* shared exit path now */
  45533. DUK_DDD(DUK_DDDPRINT("result: success"));
  45534. duk_pop(ctx); /* remove key */
  45535. return 1;
  45536. fail_proxy_rejected:
  45537. DUK_DDD(DUK_DDDPRINT("result: error, proxy rejects"));
  45538. if (throw_flag) {
  45539. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROXY_REJECTED);
  45540. }
  45541. /* Note: no key on stack */
  45542. return 0;
  45543. fail_base_primitive:
  45544. DUK_DDD(DUK_DDDPRINT("result: error, base primitive"));
  45545. if (throw_flag) {
  45546. #if defined(DUK_USE_VERBOSE_PROP_ERRORS)
  45547. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "cannot write property %s of %s",
  45548. duk_push_string_tval_readable(ctx, tv_key), duk_push_string_tval_readable(ctx, tv_obj));
  45549. #else
  45550. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_BASE);
  45551. #endif
  45552. }
  45553. duk_pop(ctx); /* remove key */
  45554. return 0;
  45555. fail_not_extensible:
  45556. DUK_DDD(DUK_DDDPRINT("result: error, not extensible"));
  45557. if (throw_flag) {
  45558. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_EXTENSIBLE);
  45559. }
  45560. duk_pop(ctx); /* remove key */
  45561. return 0;
  45562. fail_not_writable:
  45563. DUK_DDD(DUK_DDDPRINT("result: error, not writable"));
  45564. if (throw_flag) {
  45565. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_WRITABLE);
  45566. }
  45567. duk_pop(ctx); /* remove key */
  45568. return 0;
  45569. fail_array_length_partial:
  45570. DUK_DDD(DUK_DDDPRINT("result: error, array length write only partially successful"));
  45571. if (throw_flag) {
  45572. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_ARRAY_LENGTH_WRITE_FAILED);
  45573. }
  45574. duk_pop(ctx); /* remove key */
  45575. return 0;
  45576. fail_no_setter:
  45577. DUK_DDD(DUK_DDDPRINT("result: error, accessor property without setter"));
  45578. if (throw_flag) {
  45579. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_SETTER_UNDEFINED);
  45580. }
  45581. duk_pop(ctx); /* remove key */
  45582. return 0;
  45583. fail_internal:
  45584. DUK_DDD(DUK_DDDPRINT("result: error, internal"));
  45585. if (throw_flag) {
  45586. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INTERNAL_ERROR);
  45587. }
  45588. duk_pop(ctx); /* remove key */
  45589. return 0;
  45590. }
  45591. /*
  45592. * Ecmascript compliant [[Delete]](P, Throw).
  45593. */
  45594. DUK_INTERNAL duk_bool_t duk_hobject_delprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags) {
  45595. duk_propdesc desc;
  45596. duk_tval *tv;
  45597. duk_uint32_t arr_idx;
  45598. duk_bool_t throw_flag;
  45599. duk_bool_t force_flag;
  45600. throw_flag = (flags & DUK_DELPROP_FLAG_THROW);
  45601. force_flag = (flags & DUK_DELPROP_FLAG_FORCE);
  45602. DUK_DDD(DUK_DDDPRINT("delprop_raw: thr=%p, obj=%p, key=%p, throw=%ld, force=%ld (obj -> %!O, key -> %!O)",
  45603. (void *) thr, (void *) obj, (void *) key, (long) throw_flag, (long) force_flag,
  45604. (duk_heaphdr *) obj, (duk_heaphdr *) key));
  45605. DUK_ASSERT(thr != NULL);
  45606. DUK_ASSERT(thr->heap != NULL);
  45607. DUK_ASSERT(obj != NULL);
  45608. DUK_ASSERT(key != NULL);
  45609. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  45610. arr_idx = DUK_HSTRING_GET_ARRIDX_FAST(key);
  45611. /* 0 = don't push current value */
  45612. if (!duk__get_own_property_desc_raw(thr, obj, key, arr_idx, &desc, 0 /*flags*/)) { /* don't push value */
  45613. DUK_DDD(DUK_DDDPRINT("property not found, succeed always"));
  45614. goto success;
  45615. }
  45616. if ((desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) == 0 && !force_flag) {
  45617. goto fail_not_configurable;
  45618. }
  45619. if (desc.a_idx < 0 && desc.e_idx < 0) {
  45620. /* Currently there are no deletable virtual properties, but
  45621. * with force_flag we might attempt to delete one.
  45622. */
  45623. goto fail_virtual;
  45624. }
  45625. if (desc.a_idx >= 0) {
  45626. DUK_ASSERT(desc.e_idx < 0);
  45627. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, desc.a_idx);
  45628. DUK_TVAL_SET_UNUSED_UPDREF(thr, tv); /* side effects */
  45629. goto success;
  45630. } else {
  45631. DUK_ASSERT(desc.a_idx < 0);
  45632. /* remove hash entry (no decref) */
  45633. #if defined(DUK_USE_HOBJECT_HASH_PART)
  45634. if (desc.h_idx >= 0) {
  45635. duk_uint32_t *h_base = DUK_HOBJECT_H_GET_BASE(thr->heap, obj);
  45636. DUK_DDD(DUK_DDDPRINT("removing hash entry at h_idx %ld", (long) desc.h_idx));
  45637. DUK_ASSERT(DUK_HOBJECT_GET_HSIZE(obj) > 0);
  45638. DUK_ASSERT((duk_uint32_t) desc.h_idx < DUK_HOBJECT_GET_HSIZE(obj));
  45639. h_base[desc.h_idx] = DUK__HASH_DELETED;
  45640. } else {
  45641. DUK_ASSERT(DUK_HOBJECT_GET_HSIZE(obj) == 0);
  45642. }
  45643. #else
  45644. DUK_ASSERT(DUK_HOBJECT_GET_HSIZE(obj) == 0);
  45645. #endif
  45646. /* remove value */
  45647. DUK_DDD(DUK_DDDPRINT("before removing value, e_idx %ld, key %p, key at slot %p",
  45648. (long) desc.e_idx, (void *) key, (void *) DUK_HOBJECT_E_GET_KEY(thr->heap, obj, desc.e_idx)));
  45649. DUK_DDD(DUK_DDDPRINT("removing value at e_idx %ld", (long) desc.e_idx));
  45650. if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, desc.e_idx)) {
  45651. duk_hobject *tmp;
  45652. tmp = DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, obj, desc.e_idx);
  45653. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, desc.e_idx, NULL);
  45654. DUK_UNREF(tmp);
  45655. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp); /* side effects */
  45656. tmp = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, obj, desc.e_idx);
  45657. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, desc.e_idx, NULL);
  45658. DUK_UNREF(tmp);
  45659. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp); /* side effects */
  45660. } else {
  45661. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, desc.e_idx);
  45662. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
  45663. }
  45664. #if 0
  45665. /* Not strictly necessary because if key == NULL, flag MUST be ignored. */
  45666. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, desc.e_idx, 0);
  45667. #endif
  45668. /* remove key */
  45669. DUK_DDD(DUK_DDDPRINT("before removing key, e_idx %ld, key %p, key at slot %p",
  45670. (long) desc.e_idx, (void *) key, (void *) DUK_HOBJECT_E_GET_KEY(thr->heap, obj, desc.e_idx)));
  45671. DUK_DDD(DUK_DDDPRINT("removing key at e_idx %ld", (long) desc.e_idx));
  45672. DUK_ASSERT(key == DUK_HOBJECT_E_GET_KEY(thr->heap, obj, desc.e_idx));
  45673. DUK_HOBJECT_E_SET_KEY(thr->heap, obj, desc.e_idx, NULL);
  45674. DUK_HSTRING_DECREF(thr, key); /* side effects */
  45675. goto success;
  45676. }
  45677. DUK_UNREACHABLE();
  45678. success:
  45679. /*
  45680. * Argument exotic [[Delete]] behavior (E5 Section 10.6) is
  45681. * a post-check, keeping arguments internal 'map' in sync with
  45682. * any successful deletes (note that property does not need to
  45683. * exist for delete to 'succeed').
  45684. *
  45685. * Delete key from 'map'. Since 'map' only contains array index
  45686. * keys, we can use arr_idx for a fast skip.
  45687. */
  45688. DUK_DDD(DUK_DDDPRINT("delete successful, check for arguments exotic behavior"));
  45689. if (arr_idx != DUK__NO_ARRAY_INDEX && DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj)) {
  45690. /* Note: only numbered indices are relevant, so arr_idx fast reject
  45691. * is good (this is valid unless there are more than 4**32-1 arguments).
  45692. */
  45693. DUK_DDD(DUK_DDDPRINT("delete successful, arguments exotic behavior needed"));
  45694. /* Note: we can reuse 'desc' here */
  45695. (void) duk__check_arguments_map_for_delete(thr, obj, key, &desc);
  45696. }
  45697. DUK_DDD(DUK_DDDPRINT("delete successful"));
  45698. return 1;
  45699. fail_virtual:
  45700. DUK_DDD(DUK_DDDPRINT("delete failed: property found, force flag, but virtual"));
  45701. if (throw_flag) {
  45702. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROPERTY_IS_VIRTUAL);
  45703. }
  45704. return 0;
  45705. fail_not_configurable:
  45706. DUK_DDD(DUK_DDDPRINT("delete failed: property found, not configurable"));
  45707. if (throw_flag) {
  45708. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_CONFIGURABLE);
  45709. }
  45710. return 0;
  45711. }
  45712. /*
  45713. * DELPROP: Ecmascript property deletion.
  45714. */
  45715. DUK_INTERNAL duk_bool_t duk_hobject_delprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key, duk_bool_t throw_flag) {
  45716. duk_context *ctx = (duk_context *) thr;
  45717. duk_hstring *key = NULL;
  45718. #if defined(DUK_USE_ES6_PROXY)
  45719. duk_propdesc desc;
  45720. #endif
  45721. duk_int_t entry_top;
  45722. duk_uint32_t arr_idx = DUK__NO_ARRAY_INDEX;
  45723. duk_bool_t rc;
  45724. DUK_DDD(DUK_DDDPRINT("delprop: thr=%p, obj=%p, key=%p (obj -> %!T, key -> %!T)",
  45725. (void *) thr, (void *) tv_obj, (void *) tv_key,
  45726. (duk_tval *) tv_obj, (duk_tval *) tv_key));
  45727. DUK_ASSERT(ctx != NULL);
  45728. DUK_ASSERT(thr != NULL);
  45729. DUK_ASSERT(thr->heap != NULL);
  45730. DUK_ASSERT(tv_obj != NULL);
  45731. DUK_ASSERT(tv_key != NULL);
  45732. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  45733. /* Storing the entry top is cheaper here to ensure stack is correct at exit,
  45734. * as there are several paths out.
  45735. */
  45736. entry_top = duk_get_top(ctx);
  45737. if (DUK_TVAL_IS_UNDEFINED(tv_obj) ||
  45738. DUK_TVAL_IS_NULL(tv_obj)) {
  45739. DUK_DDD(DUK_DDDPRINT("base object is undefined or null -> reject"));
  45740. goto fail_invalid_base_uncond;
  45741. }
  45742. duk_push_tval(ctx, tv_obj);
  45743. duk_push_tval(ctx, tv_key);
  45744. tv_obj = duk_get_tval(ctx, -2);
  45745. if (DUK_TVAL_IS_OBJECT(tv_obj)) {
  45746. duk_hobject *obj = DUK_TVAL_GET_OBJECT(tv_obj);
  45747. DUK_ASSERT(obj != NULL);
  45748. #if defined(DUK_USE_ES6_PROXY)
  45749. if (DUK_UNLIKELY(DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(obj))) {
  45750. duk_hobject *h_target;
  45751. duk_bool_t tmp_bool;
  45752. /* Note: proxy handling must happen before key is string coerced. */
  45753. if (duk__proxy_check_prop(thr, obj, DUK_STRIDX_DELETE_PROPERTY, tv_key, &h_target)) {
  45754. /* -> [ ... trap handler ] */
  45755. DUK_DDD(DUK_DDDPRINT("-> proxy object 'deleteProperty' for key %!T", (duk_tval *) tv_key));
  45756. duk_push_hobject(ctx, h_target); /* target */
  45757. duk_push_tval(ctx, tv_key); /* P */
  45758. duk_call_method(ctx, 2 /*nargs*/);
  45759. tmp_bool = duk_to_boolean(ctx, -1);
  45760. duk_pop(ctx);
  45761. if (!tmp_bool) {
  45762. goto fail_proxy_rejected; /* retval indicates delete failed */
  45763. }
  45764. /* Target object must be checked for a conflicting
  45765. * non-configurable property.
  45766. */
  45767. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  45768. DUK_ASSERT(key != NULL);
  45769. if (duk__get_own_property_desc_raw(thr, h_target, key, arr_idx, &desc, 0 /*flags*/)) { /* don't push value */
  45770. int desc_reject;
  45771. DUK_DDD(DUK_DDDPRINT("proxy 'deleteProperty': target has matching property %!O, check for "
  45772. "conflicting property; desc.flags=0x%08lx, "
  45773. "desc.get=%p, desc.set=%p",
  45774. (duk_heaphdr *) key, (unsigned long) desc.flags,
  45775. (void *) desc.get, (void *) desc.set));
  45776. desc_reject = !(desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE);
  45777. if (desc_reject) {
  45778. /* unconditional */
  45779. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROXY_REJECTED);
  45780. }
  45781. }
  45782. rc = 1; /* success */
  45783. goto done_rc;
  45784. }
  45785. obj = h_target; /* resume delete to target */
  45786. }
  45787. #endif /* DUK_USE_ES6_PROXY */
  45788. duk_to_string(ctx, -1);
  45789. key = duk_get_hstring(ctx, -1);
  45790. DUK_ASSERT(key != NULL);
  45791. rc = duk_hobject_delprop_raw(thr, obj, key, throw_flag ? DUK_DELPROP_FLAG_THROW : 0);
  45792. goto done_rc;
  45793. } else if (DUK_TVAL_IS_STRING(tv_obj)) {
  45794. /* XXX: unnecessary string coercion for array indices,
  45795. * intentional to keep small.
  45796. */
  45797. duk_hstring *h = DUK_TVAL_GET_STRING(tv_obj);
  45798. DUK_ASSERT(h != NULL);
  45799. duk_to_string(ctx, -1);
  45800. key = duk_get_hstring(ctx, -1);
  45801. DUK_ASSERT(key != NULL);
  45802. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  45803. goto fail_not_configurable;
  45804. }
  45805. arr_idx = DUK_HSTRING_GET_ARRIDX_FAST(key);
  45806. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  45807. arr_idx < DUK_HSTRING_GET_CHARLEN(h)) {
  45808. goto fail_not_configurable;
  45809. }
  45810. } else if (DUK_TVAL_IS_BUFFER(tv_obj)) {
  45811. /* XXX: unnecessary string coercion for array indices,
  45812. * intentional to keep small; some overlap with string
  45813. * handling.
  45814. */
  45815. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv_obj);
  45816. DUK_ASSERT(h != NULL);
  45817. duk_to_string(ctx, -1);
  45818. key = duk_get_hstring(ctx, -1);
  45819. DUK_ASSERT(key != NULL);
  45820. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  45821. goto fail_not_configurable;
  45822. }
  45823. arr_idx = DUK_HSTRING_GET_ARRIDX_FAST(key);
  45824. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  45825. arr_idx < DUK_HBUFFER_GET_SIZE(h)) {
  45826. goto fail_not_configurable;
  45827. }
  45828. } else if (DUK_TVAL_IS_LIGHTFUNC(tv_obj)) {
  45829. /* Lightfunc virtual properties are non-configurable, so
  45830. * reject if match any of them.
  45831. */
  45832. duk_to_string(ctx, -1);
  45833. key = duk_get_hstring(ctx, -1);
  45834. DUK_ASSERT(key != NULL);
  45835. if (duk__key_is_lightfunc_ownprop(thr, key)) {
  45836. goto fail_not_configurable;
  45837. }
  45838. }
  45839. /* non-object base, no offending virtual property */
  45840. rc = 1;
  45841. goto done_rc;
  45842. done_rc:
  45843. duk_set_top(ctx, entry_top);
  45844. return rc;
  45845. fail_invalid_base_uncond:
  45846. /* Note: unconditional throw */
  45847. DUK_ASSERT(duk_get_top(ctx) == entry_top);
  45848. #if defined(DUK_USE_VERBOSE_PROP_ERRORS)
  45849. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "cannot delete property %s of %s",
  45850. duk_push_string_tval_readable(ctx, tv_key), duk_push_string_tval_readable(ctx, tv_obj));
  45851. #else
  45852. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_BASE);
  45853. #endif
  45854. return 0;
  45855. fail_proxy_rejected:
  45856. if (throw_flag) {
  45857. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROXY_REJECTED);
  45858. }
  45859. duk_set_top(ctx, entry_top);
  45860. return 0;
  45861. fail_not_configurable:
  45862. if (throw_flag) {
  45863. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_CONFIGURABLE);
  45864. }
  45865. duk_set_top(ctx, entry_top);
  45866. return 0;
  45867. }
  45868. /*
  45869. * Internal helper to define a property with specific flags, ignoring
  45870. * normal semantics such as extensibility, write protection etc.
  45871. * Overwrites any existing value and attributes unless caller requests
  45872. * that value only be updated if it doesn't already exists.
  45873. *
  45874. * Does not support:
  45875. * - virtual properties (error if write attempted)
  45876. * - getter/setter properties (error if write attempted)
  45877. * - non-default (!= WEC) attributes for array entries (error if attempted)
  45878. * - array abandoning: if array part exists, it is always extended
  45879. * - array 'length' updating
  45880. *
  45881. * Stack: [... in_val] -> []
  45882. *
  45883. * Used for e.g. built-in initialization and environment record
  45884. * operations.
  45885. */
  45886. DUK_INTERNAL void duk_hobject_define_property_internal(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags) {
  45887. duk_context *ctx = (duk_context *) thr;
  45888. duk_propdesc desc;
  45889. duk_uint32_t arr_idx;
  45890. duk_int_t e_idx;
  45891. duk_tval *tv1 = NULL;
  45892. duk_tval *tv2 = NULL;
  45893. duk_small_uint_t propflags = flags & DUK_PROPDESC_FLAGS_MASK; /* mask out flags not actually stored */
  45894. DUK_DDD(DUK_DDDPRINT("define new property (internal): thr=%p, obj=%!O, key=%!O, flags=0x%02lx, val=%!T",
  45895. (void *) thr, (duk_heaphdr *) obj, (duk_heaphdr *) key,
  45896. (unsigned long) flags, (duk_tval *) duk_get_tval(ctx, -1)));
  45897. DUK_ASSERT(thr != NULL);
  45898. DUK_ASSERT(thr->heap != NULL);
  45899. DUK_ASSERT(obj != NULL);
  45900. DUK_ASSERT(key != NULL);
  45901. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  45902. DUK_ASSERT(duk_is_valid_index(ctx, -1)); /* contains value */
  45903. arr_idx = DUK_HSTRING_GET_ARRIDX_SLOW(key);
  45904. if (duk__get_own_property_desc_raw(thr, obj, key, arr_idx, &desc, 0 /*flags*/)) { /* don't push value */
  45905. if (desc.e_idx >= 0) {
  45906. if (flags & DUK_PROPDESC_FLAG_NO_OVERWRITE) {
  45907. DUK_DDD(DUK_DDDPRINT("property already exists in the entry part -> skip as requested"));
  45908. goto pop_exit;
  45909. }
  45910. DUK_DDD(DUK_DDDPRINT("property already exists in the entry part -> update value and attributes"));
  45911. if (DUK_UNLIKELY(DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, desc.e_idx))) {
  45912. DUK_D(DUK_DPRINT("existing property is an accessor, not supported"));
  45913. goto error_internal;
  45914. }
  45915. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, desc.e_idx, propflags);
  45916. tv1 = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, desc.e_idx);
  45917. } else if (desc.a_idx >= 0) {
  45918. if (flags & DUK_PROPDESC_FLAG_NO_OVERWRITE) {
  45919. DUK_DDD(DUK_DDDPRINT("property already exists in the array part -> skip as requested"));
  45920. goto pop_exit;
  45921. }
  45922. DUK_DDD(DUK_DDDPRINT("property already exists in the array part -> update value (assert attributes)"));
  45923. if (propflags != DUK_PROPDESC_FLAGS_WEC) {
  45924. DUK_D(DUK_DPRINT("existing property in array part, but propflags not WEC (0x%02lx)",
  45925. (unsigned long) propflags));
  45926. goto error_internal;
  45927. }
  45928. tv1 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, desc.a_idx);
  45929. } else {
  45930. if (flags & DUK_PROPDESC_FLAG_NO_OVERWRITE) {
  45931. DUK_DDD(DUK_DDDPRINT("property already exists but is virtual -> skip as requested"));
  45932. goto pop_exit;
  45933. }
  45934. DUK_DDD(DUK_DDDPRINT("property already exists but is virtual -> failure"));
  45935. goto error_virtual;
  45936. }
  45937. goto write_value;
  45938. }
  45939. if (DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  45940. if (arr_idx != DUK__NO_ARRAY_INDEX) {
  45941. DUK_DDD(DUK_DDDPRINT("property does not exist, object has array part -> possibly extend array part and write value (assert attributes)"));
  45942. DUK_ASSERT(propflags == DUK_PROPDESC_FLAGS_WEC);
  45943. /* always grow the array, no sparse / abandon support here */
  45944. if (arr_idx >= DUK_HOBJECT_GET_ASIZE(obj)) {
  45945. duk__grow_props_for_array_item(thr, obj, arr_idx);
  45946. }
  45947. DUK_ASSERT(arr_idx < DUK_HOBJECT_GET_ASIZE(obj));
  45948. tv1 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, arr_idx);
  45949. goto write_value;
  45950. }
  45951. }
  45952. DUK_DDD(DUK_DDDPRINT("property does not exist, object belongs in entry part -> allocate new entry and write value and attributes"));
  45953. e_idx = duk__alloc_entry_checked(thr, obj, key); /* increases key refcount */
  45954. DUK_ASSERT(e_idx >= 0);
  45955. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, e_idx, propflags);
  45956. tv1 = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, e_idx);
  45957. /* new entry: previous value is garbage; set to undefined to share write_value */
  45958. DUK_TVAL_SET_UNDEFINED(tv1);
  45959. goto write_value;
  45960. write_value:
  45961. /* tv1 points to value storage */
  45962. tv2 = duk_require_tval(ctx, -1); /* late lookup, avoid side effects */
  45963. DUK_DDD(DUK_DDDPRINT("writing/updating value: %!T -> %!T",
  45964. (duk_tval *) tv1, (duk_tval *) tv2));
  45965. DUK_TVAL_SET_TVAL_UPDREF(thr, tv1, tv2); /* side effects */
  45966. goto pop_exit;
  45967. pop_exit:
  45968. duk_pop(ctx); /* remove in_val */
  45969. return;
  45970. error_internal:
  45971. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR);
  45972. return;
  45973. error_virtual:
  45974. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_REDEFINE_VIRT_PROP);
  45975. return;
  45976. }
  45977. /*
  45978. * Fast path for defining array indexed values without interning the key.
  45979. * This is used by e.g. code for Array prototype and traceback creation so
  45980. * must avoid interning.
  45981. */
  45982. 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) {
  45983. duk_context *ctx = (duk_context *) thr;
  45984. duk_hstring *key;
  45985. duk_tval *tv1, *tv2;
  45986. DUK_DDD(DUK_DDDPRINT("define new property (internal) arr_idx fast path: thr=%p, obj=%!O, "
  45987. "arr_idx=%ld, flags=0x%02lx, val=%!T",
  45988. (void *) thr, obj, (long) arr_idx, (unsigned long) flags,
  45989. (duk_tval *) duk_get_tval(ctx, -1)));
  45990. DUK_ASSERT(thr != NULL);
  45991. DUK_ASSERT(thr->heap != NULL);
  45992. DUK_ASSERT(obj != NULL);
  45993. if (DUK_HOBJECT_HAS_ARRAY_PART(obj) &&
  45994. arr_idx != DUK__NO_ARRAY_INDEX &&
  45995. flags == DUK_PROPDESC_FLAGS_WEC) {
  45996. DUK_ASSERT((flags & DUK_PROPDESC_FLAG_NO_OVERWRITE) == 0); /* covered by comparison */
  45997. DUK_DDD(DUK_DDDPRINT("define property to array part (property may or may not exist yet)"));
  45998. /* always grow the array, no sparse / abandon support here */
  45999. if (arr_idx >= DUK_HOBJECT_GET_ASIZE(obj)) {
  46000. duk__grow_props_for_array_item(thr, obj, arr_idx);
  46001. }
  46002. DUK_ASSERT(arr_idx < DUK_HOBJECT_GET_ASIZE(obj));
  46003. tv1 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, arr_idx);
  46004. tv2 = duk_require_tval(ctx, -1);
  46005. DUK_TVAL_SET_TVAL_UPDREF(thr, tv1, tv2); /* side effects */
  46006. duk_pop(ctx); /* [ ...val ] -> [ ... ] */
  46007. return;
  46008. }
  46009. DUK_DDD(DUK_DDDPRINT("define property fast path didn't work, use slow path"));
  46010. duk_push_uint(ctx, (duk_uint_t) arr_idx);
  46011. key = duk_to_hstring(ctx, -1);
  46012. DUK_ASSERT(key != NULL);
  46013. duk_insert(ctx, -2); /* [ ... val key ] -> [ ... key val ] */
  46014. duk_hobject_define_property_internal(thr, obj, key, flags);
  46015. duk_pop(ctx); /* [ ... key ] -> [ ... ] */
  46016. }
  46017. /*
  46018. * Internal helper for defining an accessor property, ignoring
  46019. * normal semantics such as extensibility, write protection etc.
  46020. * Overwrites any existing value and attributes. This is called
  46021. * very rarely, so the implementation first sets a value to undefined
  46022. * and then changes the entry to an accessor (this is to save code space).
  46023. */
  46024. DUK_INTERNAL void duk_hobject_define_accessor_internal(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_hobject *getter, duk_hobject *setter, duk_small_uint_t propflags) {
  46025. duk_context *ctx = (duk_context *) thr;
  46026. duk_int_t e_idx;
  46027. duk_int_t h_idx;
  46028. DUK_DDD(DUK_DDDPRINT("define new accessor (internal): thr=%p, obj=%!O, key=%!O, "
  46029. "getter=%!O, setter=%!O, flags=0x%02lx",
  46030. (void *) thr, (duk_heaphdr *) obj, (duk_heaphdr *) key,
  46031. (duk_heaphdr *) getter, (duk_heaphdr *) setter,
  46032. (unsigned long) propflags));
  46033. DUK_ASSERT(thr != NULL);
  46034. DUK_ASSERT(thr->heap != NULL);
  46035. DUK_ASSERT(obj != NULL);
  46036. DUK_ASSERT(key != NULL);
  46037. DUK_ASSERT((propflags & ~DUK_PROPDESC_FLAGS_MASK) == 0);
  46038. /* setter and/or getter may be NULL */
  46039. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  46040. /* force the property to 'undefined' to create a slot for it */
  46041. duk_push_undefined(ctx);
  46042. duk_hobject_define_property_internal(thr, obj, key, propflags);
  46043. duk_hobject_find_existing_entry(thr->heap, obj, key, &e_idx, &h_idx);
  46044. DUK_DDD(DUK_DDDPRINT("accessor slot: e_idx=%ld, h_idx=%ld", (long) e_idx, (long) h_idx));
  46045. DUK_ASSERT(e_idx >= 0);
  46046. DUK_ASSERT((duk_uint32_t) e_idx < DUK_HOBJECT_GET_ENEXT(obj));
  46047. /* no need to decref, as previous value is 'undefined' */
  46048. DUK_HOBJECT_E_SLOT_SET_ACCESSOR(thr->heap, obj, e_idx);
  46049. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, e_idx, getter);
  46050. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, e_idx, setter);
  46051. DUK_HOBJECT_INCREF_ALLOWNULL(thr, getter);
  46052. DUK_HOBJECT_INCREF_ALLOWNULL(thr, setter);
  46053. }
  46054. /*
  46055. * Internal helpers for managing object 'length'
  46056. */
  46057. /* XXX: awkward helpers */
  46058. DUK_INTERNAL void duk_hobject_set_length(duk_hthread *thr, duk_hobject *obj, duk_uint32_t length) {
  46059. duk_context *ctx = (duk_context *) thr;
  46060. duk_push_hobject(ctx, obj);
  46061. duk_push_hstring_stridx(ctx, DUK_STRIDX_LENGTH);
  46062. duk_push_u32(ctx, length);
  46063. (void) duk_hobject_putprop(thr, duk_get_tval(ctx, -3), duk_get_tval(ctx, -2), duk_get_tval(ctx, -1), 0);
  46064. duk_pop_n(ctx, 3);
  46065. }
  46066. DUK_INTERNAL void duk_hobject_set_length_zero(duk_hthread *thr, duk_hobject *obj) {
  46067. duk_hobject_set_length(thr, obj, 0);
  46068. }
  46069. DUK_INTERNAL duk_uint32_t duk_hobject_get_length(duk_hthread *thr, duk_hobject *obj) {
  46070. duk_context *ctx = (duk_context *) thr;
  46071. duk_double_t val;
  46072. duk_push_hobject(ctx, obj);
  46073. duk_push_hstring_stridx(ctx, DUK_STRIDX_LENGTH);
  46074. (void) duk_hobject_getprop(thr, duk_get_tval(ctx, -2), duk_get_tval(ctx, -1));
  46075. val = duk_to_number(ctx, -1);
  46076. duk_pop_n(ctx, 3);
  46077. if (val >= 0.0 && val < DUK_DOUBLE_2TO32) {
  46078. return (duk_uint32_t) val;
  46079. }
  46080. return 0;
  46081. }
  46082. /*
  46083. * Object.getOwnPropertyDescriptor() (E5 Sections 15.2.3.3, 8.10.4)
  46084. *
  46085. * This is an actual function call.
  46086. */
  46087. DUK_INTERNAL duk_ret_t duk_hobject_object_get_own_property_descriptor(duk_context *ctx) {
  46088. duk_hthread *thr = (duk_hthread *) ctx;
  46089. duk_hobject *obj;
  46090. duk_hstring *key;
  46091. duk_propdesc pd;
  46092. duk_bool_t rc;
  46093. DUK_ASSERT(ctx != NULL);
  46094. DUK_ASSERT(thr != NULL);
  46095. DUK_ASSERT(thr->heap != NULL);
  46096. obj = duk_require_hobject_or_lfunc_coerce(ctx, 0);
  46097. (void) duk_to_string(ctx, 1);
  46098. key = duk_require_hstring(ctx, 1);
  46099. DUK_ASSERT(obj != NULL);
  46100. DUK_ASSERT(key != NULL);
  46101. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  46102. rc = duk__get_own_property_desc(thr, obj, key, &pd, DUK__DESC_FLAG_PUSH_VALUE);
  46103. if (!rc) {
  46104. duk_push_undefined(ctx);
  46105. /* [obj key undefined] */
  46106. return 1;
  46107. }
  46108. duk_push_object(ctx);
  46109. /* [obj key value desc] */
  46110. if (DUK_PROPDESC_IS_ACCESSOR(&pd)) {
  46111. /* If a setter/getter is missing (undefined), the descriptor must
  46112. * still have the property present with the value 'undefined'.
  46113. */
  46114. if (pd.get) {
  46115. duk_push_hobject(ctx, pd.get);
  46116. } else {
  46117. duk_push_undefined(ctx);
  46118. }
  46119. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_GET);
  46120. if (pd.set) {
  46121. duk_push_hobject(ctx, pd.set);
  46122. } else {
  46123. duk_push_undefined(ctx);
  46124. }
  46125. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_SET);
  46126. } else {
  46127. duk_dup(ctx, -2); /* [obj key value desc value] */
  46128. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_VALUE);
  46129. duk_push_boolean(ctx, DUK_PROPDESC_IS_WRITABLE(&pd));
  46130. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_WRITABLE);
  46131. /* [obj key value desc] */
  46132. }
  46133. duk_push_boolean(ctx, DUK_PROPDESC_IS_ENUMERABLE(&pd));
  46134. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_ENUMERABLE);
  46135. duk_push_boolean(ctx, DUK_PROPDESC_IS_CONFIGURABLE(&pd));
  46136. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_CONFIGURABLE);
  46137. /* [obj key value desc] */
  46138. return 1;
  46139. }
  46140. /*
  46141. * NormalizePropertyDescriptor() related helper.
  46142. *
  46143. * Internal helper which validates and normalizes a property descriptor
  46144. * represented as an Ecmascript object (e.g. argument to defineProperty()).
  46145. * The output of this conversion is a set of defprop_flags and possibly
  46146. * some values pushed on the value stack; some subset of: property value,
  46147. * getter, setter. Caller must manage stack top carefully because the
  46148. * number of values pushed depends on the input property descriptor.
  46149. *
  46150. * The original descriptor object must not be altered in the process.
  46151. */
  46152. /* XXX: very basic optimization -> duk_get_prop_stridx_top */
  46153. DUK_INTERNAL
  46154. void duk_hobject_prepare_property_descriptor(duk_context *ctx,
  46155. duk_idx_t idx_in,
  46156. duk_uint_t *out_defprop_flags,
  46157. duk_idx_t *out_idx_value,
  46158. duk_hobject **out_getter,
  46159. duk_hobject **out_setter) {
  46160. duk_hthread *thr = (duk_hthread *) ctx;
  46161. duk_idx_t idx_value = -1;
  46162. duk_hobject *getter = NULL;
  46163. duk_hobject *setter = NULL;
  46164. duk_bool_t is_data_desc = 0;
  46165. duk_bool_t is_acc_desc = 0;
  46166. duk_uint_t defprop_flags = 0;
  46167. DUK_ASSERT(ctx != NULL);
  46168. DUK_ASSERT(out_defprop_flags != NULL);
  46169. DUK_ASSERT(out_idx_value != NULL);
  46170. DUK_ASSERT(out_getter != NULL);
  46171. DUK_ASSERT(out_setter != NULL);
  46172. /* Must be an object, otherwise TypeError (E5.1 Section 8.10.5, step 1). */
  46173. idx_in = duk_require_normalize_index(ctx, idx_in);
  46174. (void) duk_require_hobject(ctx, idx_in);
  46175. /* The coercion order must match the ToPropertyDescriptor() algorithm
  46176. * so that side effects in coercion happen in the correct order.
  46177. * (This order also happens to be compatible with duk_def_prop(),
  46178. * although it doesn't matter in practice.)
  46179. */
  46180. if (duk_get_prop_stridx(ctx, idx_in, DUK_STRIDX_VALUE)) {
  46181. is_data_desc = 1;
  46182. defprop_flags |= DUK_DEFPROP_HAVE_VALUE;
  46183. idx_value = duk_get_top_index(ctx);
  46184. /* Leave 'value' on stack */
  46185. } else {
  46186. duk_pop(ctx);
  46187. }
  46188. if (duk_get_prop_stridx(ctx, idx_in, DUK_STRIDX_WRITABLE)) {
  46189. is_data_desc = 1;
  46190. if (duk_to_boolean(ctx, -1)) {
  46191. defprop_flags |= DUK_DEFPROP_HAVE_WRITABLE | DUK_DEFPROP_WRITABLE;
  46192. } else {
  46193. defprop_flags |= DUK_DEFPROP_HAVE_WRITABLE;
  46194. }
  46195. }
  46196. duk_pop(ctx);
  46197. if (duk_get_prop_stridx(ctx, idx_in, DUK_STRIDX_GET)) {
  46198. duk_tval *tv = duk_require_tval(ctx, -1);
  46199. duk_hobject *h_get;
  46200. if (DUK_TVAL_IS_UNDEFINED(tv)) {
  46201. /* undefined is accepted */
  46202. DUK_ASSERT(getter == NULL);
  46203. } else {
  46204. /* NOTE: lightfuncs are coerced to full functions because
  46205. * lightfuncs don't fit into a property value slot. This
  46206. * has some side effects, see test-dev-lightfunc-accessor.js.
  46207. */
  46208. h_get = duk_get_hobject_or_lfunc_coerce(ctx, -1);
  46209. if (h_get == NULL || !DUK_HOBJECT_IS_CALLABLE(h_get)) {
  46210. goto type_error;
  46211. }
  46212. getter = h_get;
  46213. }
  46214. is_acc_desc = 1;
  46215. defprop_flags |= DUK_DEFPROP_HAVE_GETTER;
  46216. /* Leave 'getter' on stack */
  46217. } else {
  46218. duk_pop(ctx);
  46219. }
  46220. if (duk_get_prop_stridx(ctx, idx_in, DUK_STRIDX_SET)) {
  46221. duk_tval *tv = duk_require_tval(ctx, -1);
  46222. duk_hobject *h_set;
  46223. is_acc_desc = 1;
  46224. if (DUK_TVAL_IS_UNDEFINED(tv)) {
  46225. /* undefined is accepted */
  46226. DUK_ASSERT(setter == NULL);
  46227. } else {
  46228. /* NOTE: lightfuncs are coerced to full functions because
  46229. * lightfuncs don't fit into a property value slot. This
  46230. * has some side effects, see test-dev-lightfunc-accessor.js.
  46231. */
  46232. h_set = duk_get_hobject_or_lfunc_coerce(ctx, -1);
  46233. if (h_set == NULL || !DUK_HOBJECT_IS_CALLABLE(h_set)) {
  46234. goto type_error;
  46235. }
  46236. setter = h_set;
  46237. }
  46238. is_acc_desc = 1;
  46239. defprop_flags |= DUK_DEFPROP_HAVE_SETTER;
  46240. /* Leave 'setter' on stack */
  46241. } else {
  46242. duk_pop(ctx);
  46243. }
  46244. if (duk_get_prop_stridx(ctx, idx_in, DUK_STRIDX_ENUMERABLE)) {
  46245. if (duk_to_boolean(ctx, -1)) {
  46246. defprop_flags |= DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE;
  46247. } else {
  46248. defprop_flags |= DUK_DEFPROP_HAVE_ENUMERABLE;
  46249. }
  46250. }
  46251. duk_pop(ctx);
  46252. if (duk_get_prop_stridx(ctx, idx_in, DUK_STRIDX_CONFIGURABLE)) {
  46253. if (duk_to_boolean(ctx, -1)) {
  46254. defprop_flags |= DUK_DEFPROP_HAVE_CONFIGURABLE | DUK_DEFPROP_CONFIGURABLE;
  46255. } else {
  46256. defprop_flags |= DUK_DEFPROP_HAVE_CONFIGURABLE;
  46257. }
  46258. }
  46259. duk_pop(ctx);
  46260. if (is_data_desc && is_acc_desc) {
  46261. goto type_error;
  46262. }
  46263. *out_defprop_flags = defprop_flags;
  46264. *out_idx_value = idx_value;
  46265. *out_getter = getter;
  46266. *out_setter = setter;
  46267. /* [ ... value? getter? setter? ] */
  46268. return;
  46269. type_error:
  46270. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_DESCRIPTOR);
  46271. }
  46272. /*
  46273. * Object.defineProperty() related helper (E5 Section 15.2.3.6)
  46274. *
  46275. * Inlines all [[DefineOwnProperty]] exotic behaviors.
  46276. *
  46277. * Note: Ecmascript compliant [[DefineOwnProperty]](P, Desc, Throw) is not
  46278. * implemented directly, but Object.defineProperty() serves its purpose.
  46279. * We don't need the [[DefineOwnProperty]] internally and we don't have a
  46280. * property descriptor with 'missing values' so it's easier to avoid it
  46281. * entirely.
  46282. *
  46283. * Note: this is only called for actual objects, not primitive values.
  46284. * This must support virtual properties for full objects (e.g. Strings)
  46285. * but not for plain values (e.g. strings). Lightfuncs, even though
  46286. * primitive in a sense, are treated like objects and accepted as target
  46287. * values.
  46288. */
  46289. /* XXX: this is a major target for size optimization */
  46290. DUK_INTERNAL
  46291. void duk_hobject_define_property_helper(duk_context *ctx,
  46292. duk_uint_t defprop_flags,
  46293. duk_hobject *obj,
  46294. duk_hstring *key,
  46295. duk_idx_t idx_value,
  46296. duk_hobject *get,
  46297. duk_hobject *set) {
  46298. duk_hthread *thr = (duk_hthread *) ctx;
  46299. duk_uint32_t arr_idx;
  46300. duk_tval tv;
  46301. duk_bool_t has_enumerable;
  46302. duk_bool_t has_configurable;
  46303. duk_bool_t has_writable;
  46304. duk_bool_t has_value;
  46305. duk_bool_t has_get;
  46306. duk_bool_t has_set;
  46307. duk_bool_t is_enumerable;
  46308. duk_bool_t is_configurable;
  46309. duk_bool_t is_writable;
  46310. duk_bool_t throw_flag;
  46311. duk_bool_t force_flag;
  46312. duk_small_uint_t new_flags;
  46313. duk_propdesc curr;
  46314. duk_uint32_t arridx_new_array_length; /* != 0 => post-update for array 'length' (used when key is an array index) */
  46315. duk_uint32_t arrlen_old_len;
  46316. duk_uint32_t arrlen_new_len;
  46317. duk_bool_t pending_write_protect;
  46318. DUK_ASSERT(thr != NULL);
  46319. DUK_ASSERT(thr->heap != NULL);
  46320. DUK_ASSERT(ctx != NULL);
  46321. DUK_ASSERT(obj != NULL);
  46322. DUK_ASSERT(key != NULL);
  46323. /* idx_value may be < 0 (no value), set and get may be NULL */
  46324. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  46325. /* All the flags fit in 16 bits, so will fit into duk_bool_t. */
  46326. has_writable = (defprop_flags & DUK_DEFPROP_HAVE_WRITABLE);
  46327. has_enumerable = (defprop_flags & DUK_DEFPROP_HAVE_ENUMERABLE);
  46328. has_configurable = (defprop_flags & DUK_DEFPROP_HAVE_CONFIGURABLE);
  46329. has_value = (defprop_flags & DUK_DEFPROP_HAVE_VALUE);
  46330. has_get = (defprop_flags & DUK_DEFPROP_HAVE_GETTER);
  46331. has_set = (defprop_flags & DUK_DEFPROP_HAVE_SETTER);
  46332. is_writable = (defprop_flags & DUK_DEFPROP_WRITABLE);
  46333. is_enumerable = (defprop_flags & DUK_DEFPROP_ENUMERABLE);
  46334. is_configurable = (defprop_flags & DUK_DEFPROP_CONFIGURABLE);
  46335. throw_flag = 1; /* Object.defineProperty() calls [[DefineOwnProperty]] with Throw=true */
  46336. force_flag = (defprop_flags & DUK_DEFPROP_FORCE);
  46337. arr_idx = DUK_HSTRING_GET_ARRIDX_SLOW(key);
  46338. arridx_new_array_length = 0;
  46339. pending_write_protect = 0;
  46340. arrlen_old_len = 0;
  46341. arrlen_new_len = 0;
  46342. DUK_DDD(DUK_DDDPRINT("has_enumerable=%ld is_enumerable=%ld "
  46343. "has_configurable=%ld is_configurable=%ld "
  46344. "has_writable=%ld is_writable=%ld "
  46345. "has_value=%ld value=%!T "
  46346. "has_get=%ld get=%p=%!O "
  46347. "has_set=%ld set=%p=%!O "
  46348. "arr_idx=%ld",
  46349. (long) has_enumerable, (long) is_enumerable,
  46350. (long) has_configurable, (long) is_configurable,
  46351. (long) has_writable, (long) is_writable,
  46352. (long) has_value, (duk_tval *) (idx_value >= 0 ? duk_get_tval(ctx, idx_value) : NULL),
  46353. (long) has_get, (void *) get, (duk_heaphdr *) get,
  46354. (long) has_set, (void *) set, (duk_heaphdr *) set,
  46355. (long) arr_idx));
  46356. /*
  46357. * Array exotic behaviors can be implemented at this point. The local variables
  46358. * are essentially a 'value copy' of the input descriptor (Desc), which is modified
  46359. * by the Array [[DefineOwnProperty]] (E5 Section 15.4.5.1).
  46360. */
  46361. if (!DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj)) {
  46362. goto skip_array_exotic;
  46363. }
  46364. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  46365. /* E5 Section 15.4.5.1, step 3, steps a - i are implemented here, j - n at the end */
  46366. if (!has_value) {
  46367. DUK_DDD(DUK_DDDPRINT("exotic array behavior for 'length', but no value in descriptor -> normal behavior"));
  46368. goto skip_array_exotic;
  46369. }
  46370. DUK_DDD(DUK_DDDPRINT("exotic array behavior for 'length', value present in descriptor -> exotic behavior"));
  46371. /*
  46372. * Get old and new length
  46373. */
  46374. /* Note: reuse 'curr' as a temp propdesc */
  46375. arrlen_old_len = duk__get_old_array_length(thr, obj, &curr);
  46376. duk_dup(ctx, idx_value);
  46377. arrlen_new_len = duk__to_new_array_length_checked(thr);
  46378. duk_push_u32(ctx, arrlen_new_len);
  46379. duk_replace(ctx, idx_value); /* step 3.e: replace 'Desc.[[Value]]' */
  46380. DUK_DDD(DUK_DDDPRINT("old_len=%ld, new_len=%ld", (long) arrlen_old_len, (long) arrlen_new_len));
  46381. if (arrlen_new_len >= arrlen_old_len) {
  46382. /* standard behavior, step 3.f.i */
  46383. DUK_DDD(DUK_DDDPRINT("new length is same or higher as previous => standard behavior"));
  46384. goto skip_array_exotic;
  46385. }
  46386. DUK_DDD(DUK_DDDPRINT("new length is smaller than previous => exotic post behavior"));
  46387. /* XXX: consolidated algorithm step 15.f -> redundant? */
  46388. if (!(curr.flags & DUK_PROPDESC_FLAG_WRITABLE) && !force_flag) {
  46389. /* Note: 'curr' refers to 'length' propdesc */
  46390. goto fail_not_writable_array_length;
  46391. }
  46392. /* steps 3.h and 3.i */
  46393. if (has_writable && !is_writable) {
  46394. DUK_DDD(DUK_DDDPRINT("desc writable is false, force it back to true, and flag pending write protect"));
  46395. is_writable = 1;
  46396. pending_write_protect = 1;
  46397. }
  46398. /* remaining actual steps are carried out if standard DefineOwnProperty succeeds */
  46399. } else if (arr_idx != DUK__NO_ARRAY_INDEX) {
  46400. /* XXX: any chance of unifying this with the 'length' key handling? */
  46401. /* E5 Section 15.4.5.1, step 4 */
  46402. duk_uint32_t old_len;
  46403. /* Note: use 'curr' as a temp propdesc */
  46404. old_len = duk__get_old_array_length(thr, obj, &curr);
  46405. if (arr_idx >= old_len) {
  46406. DUK_DDD(DUK_DDDPRINT("defineProperty requires array length update "
  46407. "(arr_idx=%ld, old_len=%ld)",
  46408. (long) arr_idx, (long) old_len));
  46409. if (!(curr.flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  46410. /* Note: 'curr' refers to 'length' propdesc */
  46411. goto fail_not_writable_array_length;
  46412. }
  46413. /* actual update happens once write has been completed without
  46414. * error below.
  46415. */
  46416. DUK_ASSERT(arr_idx != 0xffffffffUL);
  46417. arridx_new_array_length = arr_idx + 1;
  46418. } else {
  46419. DUK_DDD(DUK_DDDPRINT("defineProperty does not require length update "
  46420. "(arr_idx=%ld, old_len=%ld) -> standard behavior",
  46421. (long) arr_idx, (long) old_len));
  46422. }
  46423. }
  46424. skip_array_exotic:
  46425. /* XXX: There is currently no support for writing buffer object
  46426. * indexed elements here. Attempt to do so will succeed and
  46427. * write a concrete property into the buffer object. This should
  46428. * be fixed at some point but because buffers are a custom feature
  46429. * anyway, this is relatively unimportant.
  46430. */
  46431. /*
  46432. * Actual Object.defineProperty() default algorithm.
  46433. */
  46434. /*
  46435. * First check whether property exists; if not, simple case. This covers
  46436. * steps 1-4.
  46437. */
  46438. if (!duk__get_own_property_desc_raw(thr, obj, key, arr_idx, &curr, DUK__DESC_FLAG_PUSH_VALUE)) {
  46439. DUK_DDD(DUK_DDDPRINT("property does not exist"));
  46440. if (!DUK_HOBJECT_HAS_EXTENSIBLE(obj) && !force_flag) {
  46441. goto fail_not_extensible;
  46442. }
  46443. /* XXX: share final setting code for value and flags? difficult because
  46444. * refcount code is different. Share entry allocation? But can't allocate
  46445. * until array index checked.
  46446. */
  46447. /* steps 4.a and 4.b are tricky */
  46448. if (has_set || has_get) {
  46449. duk_int_t e_idx;
  46450. DUK_DDD(DUK_DDDPRINT("create new accessor property"));
  46451. DUK_ASSERT(has_set || set == NULL);
  46452. DUK_ASSERT(has_get || get == NULL);
  46453. DUK_ASSERT(!has_value);
  46454. DUK_ASSERT(!has_writable);
  46455. new_flags = DUK_PROPDESC_FLAG_ACCESSOR; /* defaults, E5 Section 8.6.1, Table 7 */
  46456. if (has_enumerable && is_enumerable) {
  46457. new_flags |= DUK_PROPDESC_FLAG_ENUMERABLE;
  46458. }
  46459. if (has_configurable && is_configurable) {
  46460. new_flags |= DUK_PROPDESC_FLAG_CONFIGURABLE;
  46461. }
  46462. if (arr_idx != DUK__NO_ARRAY_INDEX && DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  46463. DUK_DDD(DUK_DDDPRINT("accessor cannot go to array part, abandon array"));
  46464. duk__abandon_array_checked(thr, obj);
  46465. }
  46466. /* write to entry part */
  46467. e_idx = duk__alloc_entry_checked(thr, obj, key);
  46468. DUK_ASSERT(e_idx >= 0);
  46469. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, e_idx, get);
  46470. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, e_idx, set);
  46471. DUK_HOBJECT_INCREF_ALLOWNULL(thr, get);
  46472. DUK_HOBJECT_INCREF_ALLOWNULL(thr, set);
  46473. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, e_idx, new_flags);
  46474. goto success_exotics;
  46475. } else {
  46476. duk_int_t e_idx;
  46477. duk_tval *tv2;
  46478. DUK_DDD(DUK_DDDPRINT("create new data property"));
  46479. DUK_ASSERT(!has_set);
  46480. DUK_ASSERT(!has_get);
  46481. new_flags = 0; /* defaults, E5 Section 8.6.1, Table 7 */
  46482. if (has_writable && is_writable) {
  46483. new_flags |= DUK_PROPDESC_FLAG_WRITABLE;
  46484. }
  46485. if (has_enumerable && is_enumerable) {
  46486. new_flags |= DUK_PROPDESC_FLAG_ENUMERABLE;
  46487. }
  46488. if (has_configurable && is_configurable) {
  46489. new_flags |= DUK_PROPDESC_FLAG_CONFIGURABLE;
  46490. }
  46491. if (has_value) {
  46492. duk_tval *tv_tmp = duk_require_tval(ctx, idx_value);
  46493. DUK_TVAL_SET_TVAL(&tv, tv_tmp);
  46494. } else {
  46495. DUK_TVAL_SET_UNDEFINED(&tv); /* default value */
  46496. }
  46497. if (arr_idx != DUK__NO_ARRAY_INDEX && DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  46498. if (new_flags == DUK_PROPDESC_FLAGS_WEC) {
  46499. #if 0
  46500. DUK_DDD(DUK_DDDPRINT("new data property attributes match array defaults, attempt to write to array part"));
  46501. /* may become sparse...*/
  46502. #endif
  46503. /* XXX: handling for array part missing now; this doesn't affect
  46504. * compliance but causes array entry writes using defineProperty()
  46505. * to always abandon array part.
  46506. */
  46507. }
  46508. DUK_DDD(DUK_DDDPRINT("new data property cannot go to array part, abandon array"));
  46509. duk__abandon_array_checked(thr, obj);
  46510. /* fall through */
  46511. }
  46512. /* write to entry part */
  46513. e_idx = duk__alloc_entry_checked(thr, obj, key);
  46514. DUK_ASSERT(e_idx >= 0);
  46515. tv2 = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, e_idx);
  46516. DUK_TVAL_SET_TVAL(tv2, &tv);
  46517. DUK_TVAL_INCREF(thr, tv2);
  46518. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, e_idx, new_flags);
  46519. goto success_exotics;
  46520. }
  46521. DUK_UNREACHABLE();
  46522. }
  46523. /* we currently assume virtual properties are not configurable (as none of them are) */
  46524. DUK_ASSERT((curr.e_idx >= 0 || curr.a_idx >= 0) || !(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE));
  46525. /* [obj key desc value get set curr_value] */
  46526. /*
  46527. * Property already exists. Steps 5-6 detect whether any changes need
  46528. * to be made.
  46529. */
  46530. if (has_enumerable) {
  46531. if (is_enumerable) {
  46532. if (!(curr.flags & DUK_PROPDESC_FLAG_ENUMERABLE)) {
  46533. goto need_check;
  46534. }
  46535. } else {
  46536. if (curr.flags & DUK_PROPDESC_FLAG_ENUMERABLE) {
  46537. goto need_check;
  46538. }
  46539. }
  46540. }
  46541. if (has_configurable) {
  46542. if (is_configurable) {
  46543. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE)) {
  46544. goto need_check;
  46545. }
  46546. } else {
  46547. if (curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) {
  46548. goto need_check;
  46549. }
  46550. }
  46551. }
  46552. if (has_value) {
  46553. duk_tval *tmp1;
  46554. duk_tval *tmp2;
  46555. /* attempt to change from accessor to data property */
  46556. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  46557. goto need_check;
  46558. }
  46559. tmp1 = duk_require_tval(ctx, -1); /* curr value */
  46560. tmp2 = duk_require_tval(ctx, idx_value); /* new value */
  46561. if (!duk_js_samevalue(tmp1, tmp2)) {
  46562. goto need_check;
  46563. }
  46564. }
  46565. if (has_writable) {
  46566. /* attempt to change from accessor to data property */
  46567. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  46568. goto need_check;
  46569. }
  46570. if (is_writable) {
  46571. if (!(curr.flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  46572. goto need_check;
  46573. }
  46574. } else {
  46575. if (curr.flags & DUK_PROPDESC_FLAG_WRITABLE) {
  46576. goto need_check;
  46577. }
  46578. }
  46579. }
  46580. if (has_set) {
  46581. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  46582. if (set != curr.set) {
  46583. goto need_check;
  46584. }
  46585. } else {
  46586. goto need_check;
  46587. }
  46588. }
  46589. if (has_get) {
  46590. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  46591. if (get != curr.get) {
  46592. goto need_check;
  46593. }
  46594. } else {
  46595. goto need_check;
  46596. }
  46597. }
  46598. /* property exists, either 'desc' is empty, or all values
  46599. * match (SameValue)
  46600. */
  46601. goto success_no_exotics;
  46602. need_check:
  46603. /*
  46604. * Some change(s) need to be made. Steps 7-11.
  46605. */
  46606. /* shared checks for all descriptor types */
  46607. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && !force_flag) {
  46608. if (has_configurable && is_configurable) {
  46609. goto fail_not_configurable;
  46610. }
  46611. if (has_enumerable) {
  46612. if (curr.flags & DUK_PROPDESC_FLAG_ENUMERABLE) {
  46613. if (!is_enumerable) {
  46614. goto fail_not_configurable;
  46615. }
  46616. } else {
  46617. if (is_enumerable) {
  46618. goto fail_not_configurable;
  46619. }
  46620. }
  46621. }
  46622. }
  46623. /* Reject attempt to change virtual properties: not part of the
  46624. * standard algorithm, applies currently to e.g. virtual index
  46625. * properties of buffer objects (which are virtual but writable).
  46626. * (Cannot "force" modification of a virtual property.)
  46627. */
  46628. if (curr.flags & DUK_PROPDESC_FLAG_VIRTUAL) {
  46629. goto fail_virtual;
  46630. }
  46631. /* descriptor type specific checks */
  46632. if (has_set || has_get) {
  46633. /* IsAccessorDescriptor(desc) == true */
  46634. DUK_ASSERT(!has_writable);
  46635. DUK_ASSERT(!has_value);
  46636. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  46637. /* curr and desc are accessors */
  46638. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && !force_flag) {
  46639. if (has_set && set != curr.set) {
  46640. goto fail_not_configurable;
  46641. }
  46642. if (has_get && get != curr.get) {
  46643. goto fail_not_configurable;
  46644. }
  46645. }
  46646. } else {
  46647. duk_bool_t rc;
  46648. duk_tval *tv1;
  46649. /* curr is data, desc is accessor */
  46650. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && !force_flag) {
  46651. goto fail_not_configurable;
  46652. }
  46653. DUK_DDD(DUK_DDDPRINT("convert property to accessor property"));
  46654. if (curr.a_idx >= 0) {
  46655. DUK_DDD(DUK_DDDPRINT("property to convert is stored in an array entry, abandon array and re-lookup"));
  46656. duk__abandon_array_checked(thr, obj);
  46657. duk_pop(ctx); /* remove old value */
  46658. rc = duk__get_own_property_desc_raw(thr, obj, key, arr_idx, &curr, DUK__DESC_FLAG_PUSH_VALUE);
  46659. DUK_UNREF(rc);
  46660. DUK_ASSERT(rc != 0);
  46661. DUK_ASSERT(curr.e_idx >= 0 && curr.a_idx < 0);
  46662. }
  46663. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  46664. tv1 = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, curr.e_idx);
  46665. /* XXX: just decref */
  46666. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv1); /* side effects */
  46667. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, curr.e_idx, NULL);
  46668. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, curr.e_idx, NULL);
  46669. DUK_HOBJECT_E_SLOT_CLEAR_WRITABLE(thr->heap, obj, curr.e_idx);
  46670. DUK_HOBJECT_E_SLOT_SET_ACCESSOR(thr->heap, obj, curr.e_idx);
  46671. DUK_DDD(DUK_DDDPRINT("flags after data->accessor conversion: 0x%02lx",
  46672. (unsigned long) DUK_HOBJECT_E_GET_FLAGS(thr->heap, obj, curr.e_idx)));
  46673. /* re-lookup to update curr.flags
  46674. * XXX: would be faster to update directly
  46675. */
  46676. duk_pop(ctx); /* remove old value */
  46677. rc = duk__get_own_property_desc_raw(thr, obj, key, arr_idx, &curr, DUK__DESC_FLAG_PUSH_VALUE);
  46678. DUK_UNREF(rc);
  46679. DUK_ASSERT(rc != 0);
  46680. }
  46681. } else if (has_value || has_writable) {
  46682. /* IsDataDescriptor(desc) == true */
  46683. DUK_ASSERT(!has_set);
  46684. DUK_ASSERT(!has_get);
  46685. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  46686. duk_bool_t rc;
  46687. duk_hobject *tmp;
  46688. /* curr is accessor, desc is data */
  46689. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && !force_flag) {
  46690. goto fail_not_configurable;
  46691. }
  46692. /* curr is accessor -> cannot be in array part */
  46693. DUK_ASSERT(curr.e_idx >= 0 && curr.a_idx < 0);
  46694. DUK_DDD(DUK_DDDPRINT("convert property to data property"));
  46695. DUK_ASSERT(DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  46696. tmp = DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, obj, curr.e_idx);
  46697. DUK_UNREF(tmp);
  46698. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, curr.e_idx, NULL);
  46699. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp); /* side effects */
  46700. tmp = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, obj, curr.e_idx);
  46701. DUK_UNREF(tmp);
  46702. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, curr.e_idx, NULL);
  46703. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp); /* side effects */
  46704. DUK_TVAL_SET_UNDEFINED(DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, curr.e_idx));
  46705. DUK_HOBJECT_E_SLOT_CLEAR_WRITABLE(thr->heap, obj, curr.e_idx);
  46706. DUK_HOBJECT_E_SLOT_CLEAR_ACCESSOR(thr->heap, obj, curr.e_idx);
  46707. DUK_DDD(DUK_DDDPRINT("flags after accessor->data conversion: 0x%02lx",
  46708. (unsigned long) DUK_HOBJECT_E_GET_FLAGS(thr->heap, obj, curr.e_idx)));
  46709. /* re-lookup to update curr.flags
  46710. * XXX: would be faster to update directly
  46711. */
  46712. duk_pop(ctx); /* remove old value */
  46713. rc = duk__get_own_property_desc_raw(thr, obj, key, arr_idx, &curr, DUK__DESC_FLAG_PUSH_VALUE);
  46714. DUK_UNREF(rc);
  46715. DUK_ASSERT(rc != 0);
  46716. } else {
  46717. /* curr and desc are data */
  46718. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && !force_flag) {
  46719. if (!(curr.flags & DUK_PROPDESC_FLAG_WRITABLE) && has_writable && is_writable) {
  46720. goto fail_not_configurable;
  46721. }
  46722. /* Note: changing from writable to non-writable is OK */
  46723. if (!(curr.flags & DUK_PROPDESC_FLAG_WRITABLE) && has_value) {
  46724. duk_tval *tmp1 = duk_require_tval(ctx, -1); /* curr value */
  46725. duk_tval *tmp2 = duk_require_tval(ctx, idx_value); /* new value */
  46726. if (!duk_js_samevalue(tmp1, tmp2)) {
  46727. goto fail_not_configurable;
  46728. }
  46729. }
  46730. }
  46731. }
  46732. } else {
  46733. /* IsGenericDescriptor(desc) == true; this means in practice that 'desc'
  46734. * only has [[Enumerable]] or [[Configurable]] flag updates, which are
  46735. * allowed at this point.
  46736. */
  46737. DUK_ASSERT(!has_value && !has_writable && !has_get && !has_set);
  46738. }
  46739. /*
  46740. * Start doing property attributes updates. Steps 12-13.
  46741. *
  46742. * Start by computing new attribute flags without writing yet.
  46743. * Property type conversion is done above if necessary.
  46744. */
  46745. new_flags = curr.flags;
  46746. if (has_enumerable) {
  46747. if (is_enumerable) {
  46748. new_flags |= DUK_PROPDESC_FLAG_ENUMERABLE;
  46749. } else {
  46750. new_flags &= ~DUK_PROPDESC_FLAG_ENUMERABLE;
  46751. }
  46752. }
  46753. if (has_configurable) {
  46754. if (is_configurable) {
  46755. new_flags |= DUK_PROPDESC_FLAG_CONFIGURABLE;
  46756. } else {
  46757. new_flags &= ~DUK_PROPDESC_FLAG_CONFIGURABLE;
  46758. }
  46759. }
  46760. if (has_writable) {
  46761. if (is_writable) {
  46762. new_flags |= DUK_PROPDESC_FLAG_WRITABLE;
  46763. } else {
  46764. new_flags &= ~DUK_PROPDESC_FLAG_WRITABLE;
  46765. }
  46766. }
  46767. /* XXX: write protect after flag? -> any chance of handling it here? */
  46768. DUK_DDD(DUK_DDDPRINT("new flags that we want to write: 0x%02lx",
  46769. (unsigned long) new_flags));
  46770. /*
  46771. * Check whether we need to abandon an array part (if it exists)
  46772. */
  46773. if (curr.a_idx >= 0) {
  46774. duk_bool_t rc;
  46775. DUK_ASSERT(curr.e_idx < 0);
  46776. if (new_flags == DUK_PROPDESC_FLAGS_WEC) {
  46777. duk_tval *tv1, *tv2;
  46778. DUK_DDD(DUK_DDDPRINT("array index, new property attributes match array defaults, update in-place"));
  46779. DUK_ASSERT(curr.flags == DUK_PROPDESC_FLAGS_WEC); /* must have been, since in array part */
  46780. DUK_ASSERT(!has_set);
  46781. DUK_ASSERT(!has_get);
  46782. tv2 = duk_require_tval(ctx, idx_value);
  46783. tv1 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, curr.a_idx);
  46784. DUK_TVAL_SET_TVAL_UPDREF(thr, tv1, tv2); /* side effects */
  46785. goto success_exotics;
  46786. }
  46787. DUK_DDD(DUK_DDDPRINT("array index, new property attributes do not match array defaults, abandon array and re-lookup"));
  46788. duk__abandon_array_checked(thr, obj);
  46789. duk_pop(ctx); /* remove old value */
  46790. rc = duk__get_own_property_desc_raw(thr, obj, key, arr_idx, &curr, DUK__DESC_FLAG_PUSH_VALUE);
  46791. DUK_UNREF(rc);
  46792. DUK_ASSERT(rc != 0);
  46793. DUK_ASSERT(curr.e_idx >= 0 && curr.a_idx < 0);
  46794. }
  46795. DUK_DDD(DUK_DDDPRINT("updating existing property in entry part"));
  46796. /* array case is handled comprehensively above */
  46797. DUK_ASSERT(curr.e_idx >= 0 && curr.a_idx < 0);
  46798. DUK_DDD(DUK_DDDPRINT("update existing property attributes"));
  46799. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, curr.e_idx, new_flags);
  46800. if (has_set) {
  46801. duk_hobject *tmp;
  46802. DUK_DDD(DUK_DDDPRINT("update existing property setter"));
  46803. DUK_ASSERT(DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  46804. tmp = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, obj, curr.e_idx);
  46805. DUK_UNREF(tmp);
  46806. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, curr.e_idx, set);
  46807. DUK_HOBJECT_INCREF_ALLOWNULL(thr, set);
  46808. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp); /* side effects */
  46809. }
  46810. if (has_get) {
  46811. duk_hobject *tmp;
  46812. DUK_DDD(DUK_DDDPRINT("update existing property getter"));
  46813. DUK_ASSERT(DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  46814. tmp = DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, obj, curr.e_idx);
  46815. DUK_UNREF(tmp);
  46816. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, curr.e_idx, get);
  46817. DUK_HOBJECT_INCREF_ALLOWNULL(thr, get);
  46818. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp); /* side effects */
  46819. }
  46820. if (has_value) {
  46821. duk_tval *tv1, *tv2;
  46822. DUK_DDD(DUK_DDDPRINT("update existing property value"));
  46823. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  46824. tv2 = duk_require_tval(ctx, idx_value);
  46825. tv1 = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, curr.e_idx);
  46826. DUK_TVAL_SET_TVAL_UPDREF(thr, tv1, tv2); /* side effects */
  46827. }
  46828. /*
  46829. * Standard algorithm succeeded without errors, check for exotic post-behaviors.
  46830. *
  46831. * Arguments exotic behavior in E5 Section 10.6 occurs after the standard
  46832. * [[DefineOwnProperty]] has completed successfully.
  46833. *
  46834. * Array exotic behavior in E5 Section 15.4.5.1 is implemented partly
  46835. * prior to the default [[DefineOwnProperty]], but:
  46836. * - for an array index key (e.g. "10") the final 'length' update occurs here
  46837. * - for 'length' key the element deletion and 'length' update occurs here
  46838. */
  46839. success_exotics:
  46840. /* [obj key desc value get set curr_value] */
  46841. if (DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj)) {
  46842. if (arridx_new_array_length > 0) {
  46843. duk_tval *tmp;
  46844. duk_bool_t rc;
  46845. /*
  46846. * Note: zero works as a "no update" marker because the new length
  46847. * can never be zero after a new property is written.
  46848. */
  46849. /* E5 Section 15.4.5.1, steps 4.e.i - 4.e.ii */
  46850. DUK_DDD(DUK_DDDPRINT("defineProperty successful, pending array length update to: %ld",
  46851. (long) arridx_new_array_length));
  46852. /* Note: reuse 'curr' */
  46853. rc = duk__get_own_property_desc_raw(thr, obj, DUK_HTHREAD_STRING_LENGTH(thr), DUK__NO_ARRAY_INDEX, &curr, 0 /*flags*/); /* don't push value */
  46854. DUK_UNREF(rc);
  46855. DUK_ASSERT(rc != 0);
  46856. DUK_ASSERT(curr.e_idx >= 0);
  46857. tmp = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, curr.e_idx);
  46858. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tmp));
  46859. /* no need for decref/incref because value is a number */
  46860. #if defined(DUK_USE_FASTINT)
  46861. DUK_TVAL_SET_FASTINT_U32(tmp, arridx_new_array_length);
  46862. #else
  46863. DUK_TVAL_SET_NUMBER(tmp, (duk_double_t) arridx_new_array_length);
  46864. #endif
  46865. }
  46866. if (key == DUK_HTHREAD_STRING_LENGTH(thr) && arrlen_new_len < arrlen_old_len) {
  46867. /*
  46868. * E5 Section 15.4.5.1, steps 3.k - 3.n. The order at the end combines
  46869. * the error case 3.l.iii and the success case 3.m-3.n.
  46870. *
  46871. * Note: 'length' is always in entries part, so no array abandon issues for
  46872. * 'writable' update.
  46873. */
  46874. /* XXX: investigate whether write protect can be handled above, if we
  46875. * just update length here while ignoring its protected status
  46876. */
  46877. duk_tval *tmp;
  46878. duk_uint32_t result_len;
  46879. duk_bool_t rc;
  46880. DUK_DDD(DUK_DDDPRINT("defineProperty successful, key is 'length', exotic array behavior, "
  46881. "doing array element deletion and length update"));
  46882. rc = duk__handle_put_array_length_smaller(thr, obj, arrlen_old_len, arrlen_new_len, force_flag, &result_len);
  46883. /* update length (curr points to length, and we assume it's still valid) */
  46884. DUK_ASSERT(result_len >= arrlen_new_len && result_len <= arrlen_old_len);
  46885. DUK_ASSERT(curr.e_idx >= 0);
  46886. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  46887. tmp = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, curr.e_idx);
  46888. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tmp));
  46889. /* no decref needed for a number */
  46890. #if defined(DUK_USE_FASTINT)
  46891. DUK_TVAL_SET_FASTINT_U32(tmp, result_len);
  46892. #else
  46893. DUK_TVAL_SET_NUMBER(tmp, (duk_double_t) result_len);
  46894. #endif
  46895. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tmp));
  46896. if (pending_write_protect) {
  46897. DUK_DDD(DUK_DDDPRINT("setting array length non-writable (pending writability update)"));
  46898. DUK_HOBJECT_E_SLOT_CLEAR_WRITABLE(thr->heap, obj, curr.e_idx);
  46899. }
  46900. /*
  46901. * XXX: shrink array allocation or entries compaction here?
  46902. */
  46903. if (!rc) {
  46904. goto fail_array_length_partial;
  46905. }
  46906. }
  46907. } else if (arr_idx != DUK__NO_ARRAY_INDEX && DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj)) {
  46908. duk_hobject *map;
  46909. duk_hobject *varenv;
  46910. DUK_ASSERT(arridx_new_array_length == 0);
  46911. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj)); /* traits are separate; in particular, arguments not an array */
  46912. map = NULL;
  46913. varenv = NULL;
  46914. if (!duk__lookup_arguments_map(thr, obj, key, &curr, &map, &varenv)) {
  46915. goto success_no_exotics;
  46916. }
  46917. DUK_ASSERT(map != NULL);
  46918. DUK_ASSERT(varenv != NULL);
  46919. /* [obj key desc value get set curr_value varname] */
  46920. if (has_set || has_get) {
  46921. /* = IsAccessorDescriptor(Desc) */
  46922. DUK_DDD(DUK_DDDPRINT("defineProperty successful, key mapped to arguments 'map' "
  46923. "changed to an accessor, delete arguments binding"));
  46924. (void) duk_hobject_delprop_raw(thr, map, key, 0); /* ignore result */
  46925. } else {
  46926. /* Note: this order matters (final value before deleting map entry must be done) */
  46927. DUK_DDD(DUK_DDDPRINT("defineProperty successful, key mapped to arguments 'map', "
  46928. "check for value update / binding deletion"));
  46929. if (has_value) {
  46930. duk_hstring *varname;
  46931. DUK_DDD(DUK_DDDPRINT("defineProperty successful, key mapped to arguments 'map', "
  46932. "update bound value (variable/argument)"));
  46933. varname = duk_require_hstring(ctx, -1);
  46934. DUK_ASSERT(varname != NULL);
  46935. DUK_DDD(DUK_DDDPRINT("arguments object automatic putvar for a bound variable; "
  46936. "key=%!O, varname=%!O, value=%!T",
  46937. (duk_heaphdr *) key,
  46938. (duk_heaphdr *) varname,
  46939. (duk_tval *) duk_require_tval(ctx, idx_value)));
  46940. /* strict flag for putvar comes from our caller (currently: fixed) */
  46941. duk_js_putvar_envrec(thr, varenv, varname, duk_require_tval(ctx, idx_value), throw_flag);
  46942. }
  46943. if (has_writable && !is_writable) {
  46944. DUK_DDD(DUK_DDDPRINT("defineProperty successful, key mapped to arguments 'map', "
  46945. "changed to non-writable, delete arguments binding"));
  46946. (void) duk_hobject_delprop_raw(thr, map, key, 0); /* ignore result */
  46947. }
  46948. }
  46949. /* 'varname' is in stack in this else branch, leaving an unbalanced stack below,
  46950. * but this doesn't matter now.
  46951. */
  46952. }
  46953. success_no_exotics:
  46954. return;
  46955. fail_virtual:
  46956. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROPERTY_IS_VIRTUAL);
  46957. return;
  46958. fail_not_writable_array_length:
  46959. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_ARRAY_LENGTH_NOT_WRITABLE);
  46960. return;
  46961. fail_not_extensible:
  46962. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_EXTENSIBLE);
  46963. return;
  46964. fail_not_configurable:
  46965. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_CONFIGURABLE);
  46966. return;
  46967. fail_array_length_partial:
  46968. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_ARRAY_LENGTH_WRITE_FAILED);
  46969. return;
  46970. }
  46971. /*
  46972. * Object.prototype.hasOwnProperty() and Object.prototype.propertyIsEnumerable().
  46973. */
  46974. DUK_INTERNAL duk_bool_t duk_hobject_object_ownprop_helper(duk_context *ctx, duk_small_uint_t required_desc_flags) {
  46975. duk_hthread *thr = (duk_hthread *) ctx;
  46976. duk_hstring *h_v;
  46977. duk_hobject *h_obj;
  46978. duk_propdesc desc;
  46979. duk_bool_t ret;
  46980. /* coercion order matters */
  46981. h_v = duk_to_hstring(ctx, 0);
  46982. DUK_ASSERT(h_v != NULL);
  46983. h_obj = duk_push_this_coercible_to_object(ctx);
  46984. DUK_ASSERT(h_obj != NULL);
  46985. ret = duk__get_own_property_desc(thr, h_obj, h_v, &desc, 0 /*flags*/); /* don't push value */
  46986. duk_push_boolean(ctx, ret && ((desc.flags & required_desc_flags) == required_desc_flags));
  46987. return 1;
  46988. }
  46989. /*
  46990. * Object.seal() and Object.freeze() (E5 Sections 15.2.3.8 and 15.2.3.9)
  46991. *
  46992. * Since the algorithms are similar, a helper provides both functions.
  46993. * Freezing is essentially sealing + making plain properties non-writable.
  46994. *
  46995. * Note: virtual (non-concrete) properties which are non-configurable but
  46996. * writable would pose some problems, but such properties do not currently
  46997. * exist (all virtual properties are non-configurable and non-writable).
  46998. * If they did exist, the non-configurability does NOT prevent them from
  46999. * becoming non-writable. However, this change should be recorded somehow
  47000. * so that it would turn up (e.g. when getting the property descriptor),
  47001. * requiring some additional flags in the object.
  47002. */
  47003. DUK_INTERNAL void duk_hobject_object_seal_freeze_helper(duk_hthread *thr, duk_hobject *obj, duk_bool_t is_freeze) {
  47004. duk_uint_fast32_t i;
  47005. DUK_ASSERT(thr != NULL);
  47006. DUK_ASSERT(thr->heap != NULL);
  47007. DUK_ASSERT(obj != NULL);
  47008. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  47009. /*
  47010. * Abandon array part because all properties must become non-configurable.
  47011. * Note that this is now done regardless of whether this is always the case
  47012. * (skips check, but performance problem if caller would do this many times
  47013. * for the same object; not likely).
  47014. */
  47015. duk__abandon_array_checked(thr, obj);
  47016. DUK_ASSERT(DUK_HOBJECT_GET_ASIZE(obj) == 0);
  47017. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  47018. duk_uint8_t *fp;
  47019. /* since duk__abandon_array_checked() causes a resize, there should be no gaps in keys */
  47020. DUK_ASSERT(DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i) != NULL);
  47021. /* avoid multiple computations of flags address; bypasses macros */
  47022. fp = DUK_HOBJECT_E_GET_FLAGS_PTR(thr->heap, obj, i);
  47023. if (is_freeze && !((*fp) & DUK_PROPDESC_FLAG_ACCESSOR)) {
  47024. *fp &= ~(DUK_PROPDESC_FLAG_WRITABLE | DUK_PROPDESC_FLAG_CONFIGURABLE);
  47025. } else {
  47026. *fp &= ~DUK_PROPDESC_FLAG_CONFIGURABLE;
  47027. }
  47028. }
  47029. DUK_HOBJECT_CLEAR_EXTENSIBLE(obj);
  47030. /* no need to compact since we already did that in duk__abandon_array_checked()
  47031. * (regardless of whether an array part existed or not.
  47032. */
  47033. return;
  47034. }
  47035. /*
  47036. * Object.isSealed() and Object.isFrozen() (E5 Sections 15.2.3.11, 15.2.3.13)
  47037. *
  47038. * Since the algorithms are similar, a helper provides both functions.
  47039. * Freezing is essentially sealing + making plain properties non-writable.
  47040. *
  47041. * Note: all virtual (non-concrete) properties are currently non-configurable
  47042. * and non-writable (and there are no accessor virtual properties), so they don't
  47043. * need to be considered here now.
  47044. */
  47045. DUK_INTERNAL duk_bool_t duk_hobject_object_is_sealed_frozen_helper(duk_hthread *thr, duk_hobject *obj, duk_bool_t is_frozen) {
  47046. duk_uint_fast32_t i;
  47047. DUK_ASSERT(obj != NULL);
  47048. DUK_UNREF(thr);
  47049. /* Note: no allocation pressure, no need to check refcounts etc */
  47050. /* must not be extensible */
  47051. if (DUK_HOBJECT_HAS_EXTENSIBLE(obj)) {
  47052. return 0;
  47053. }
  47054. /* all virtual properties are non-configurable and non-writable */
  47055. /* entry part must not contain any configurable properties, or
  47056. * writable properties (if is_frozen).
  47057. */
  47058. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  47059. duk_small_uint_t flags;
  47060. if (!DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i)) {
  47061. continue;
  47062. }
  47063. /* avoid multiple computations of flags address; bypasses macros */
  47064. flags = (duk_small_uint_t) DUK_HOBJECT_E_GET_FLAGS(thr->heap, obj, i);
  47065. if (flags & DUK_PROPDESC_FLAG_CONFIGURABLE) {
  47066. return 0;
  47067. }
  47068. if (is_frozen &&
  47069. !(flags & DUK_PROPDESC_FLAG_ACCESSOR) &&
  47070. (flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  47071. return 0;
  47072. }
  47073. }
  47074. /* array part must not contain any non-unused properties, as they would
  47075. * be configurable and writable.
  47076. */
  47077. for (i = 0; i < DUK_HOBJECT_GET_ASIZE(obj); i++) {
  47078. duk_tval *tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, i);
  47079. if (!DUK_TVAL_IS_UNUSED(tv)) {
  47080. return 0;
  47081. }
  47082. }
  47083. return 1;
  47084. }
  47085. /*
  47086. * Object.preventExtensions() and Object.isExtensible() (E5 Sections 15.2.3.10, 15.2.3.13)
  47087. *
  47088. * Not needed, implemented by macros DUK_HOBJECT_{HAS,CLEAR,SET}_EXTENSIBLE
  47089. * and the Object built-in bindings.
  47090. */
  47091. /* Undefine local defines */
  47092. #undef DUK__NO_ARRAY_INDEX
  47093. #undef DUK__HASH_INITIAL
  47094. #undef DUK__HASH_PROBE_STEP
  47095. #undef DUK__HASH_UNUSED
  47096. #undef DUK__HASH_DELETED
  47097. #undef DUK__VALSTACK_SPACE
  47098. #line 1 "duk_hstring_misc.c"
  47099. /*
  47100. * Misc support functions
  47101. */
  47102. /* include removed: duk_internal.h */
  47103. DUK_INTERNAL duk_ucodepoint_t duk_hstring_char_code_at_raw(duk_hthread *thr, duk_hstring *h, duk_uint_t pos) {
  47104. duk_uint32_t boff;
  47105. const duk_uint8_t *p, *p_start, *p_end;
  47106. duk_ucodepoint_t cp;
  47107. /* Caller must check character offset to be inside the string. */
  47108. DUK_ASSERT(thr != NULL);
  47109. DUK_ASSERT(h != NULL);
  47110. DUK_ASSERT_DISABLE(pos >= 0); /* unsigned */
  47111. DUK_ASSERT(pos < (duk_uint_t) DUK_HSTRING_GET_CHARLEN(h));
  47112. boff = duk_heap_strcache_offset_char2byte(thr, h, (duk_uint32_t) pos);
  47113. DUK_DDD(DUK_DDDPRINT("charCodeAt: pos=%ld -> boff=%ld, str=%!O",
  47114. (long) pos, (long) boff, (duk_heaphdr *) h));
  47115. DUK_ASSERT_DISABLE(boff >= 0);
  47116. DUK_ASSERT(boff < DUK_HSTRING_GET_BYTELEN(h));
  47117. p_start = DUK_HSTRING_GET_DATA(h);
  47118. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h);
  47119. p = p_start + boff;
  47120. DUK_DDD(DUK_DDDPRINT("p_start=%p, p_end=%p, p=%p",
  47121. (void *) p_start, (void *) p_end, (void *) p));
  47122. /* This may throw an error though not for valid E5 strings. */
  47123. cp = duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end);
  47124. return cp;
  47125. }
  47126. #line 1 "duk_hthread_alloc.c"
  47127. /*
  47128. * duk_hthread allocation and freeing.
  47129. */
  47130. /* include removed: duk_internal.h */
  47131. /*
  47132. * Allocate initial stacks for a thread. Note that 'thr' must be reachable
  47133. * as a garbage collection may be triggered by the allocation attempts.
  47134. * Returns zero (without leaking memory) if init fails.
  47135. */
  47136. DUK_INTERNAL duk_bool_t duk_hthread_init_stacks(duk_heap *heap, duk_hthread *thr) {
  47137. duk_size_t alloc_size;
  47138. duk_size_t i;
  47139. DUK_ASSERT(heap != NULL);
  47140. DUK_ASSERT(thr != NULL);
  47141. DUK_ASSERT(thr->valstack == NULL);
  47142. DUK_ASSERT(thr->valstack_end == NULL);
  47143. DUK_ASSERT(thr->valstack_bottom == NULL);
  47144. DUK_ASSERT(thr->valstack_top == NULL);
  47145. DUK_ASSERT(thr->callstack == NULL);
  47146. DUK_ASSERT(thr->catchstack == NULL);
  47147. /* valstack */
  47148. alloc_size = sizeof(duk_tval) * DUK_VALSTACK_INITIAL_SIZE;
  47149. thr->valstack = (duk_tval *) DUK_ALLOC(heap, alloc_size);
  47150. if (!thr->valstack) {
  47151. goto fail;
  47152. }
  47153. DUK_MEMZERO(thr->valstack, alloc_size);
  47154. thr->valstack_end = thr->valstack + DUK_VALSTACK_INITIAL_SIZE;
  47155. #if !defined(DUK_USE_PREFER_SIZE)
  47156. thr->valstack_size = DUK_VALSTACK_INITIAL_SIZE;
  47157. #endif
  47158. thr->valstack_bottom = thr->valstack;
  47159. thr->valstack_top = thr->valstack;
  47160. for (i = 0; i < DUK_VALSTACK_INITIAL_SIZE; i++) {
  47161. DUK_TVAL_SET_UNDEFINED(&thr->valstack[i]);
  47162. }
  47163. /* callstack */
  47164. alloc_size = sizeof(duk_activation) * DUK_CALLSTACK_INITIAL_SIZE;
  47165. thr->callstack = (duk_activation *) DUK_ALLOC(heap, alloc_size);
  47166. if (!thr->callstack) {
  47167. goto fail;
  47168. }
  47169. DUK_MEMZERO(thr->callstack, alloc_size);
  47170. thr->callstack_size = DUK_CALLSTACK_INITIAL_SIZE;
  47171. DUK_ASSERT(thr->callstack_top == 0);
  47172. /* catchstack */
  47173. alloc_size = sizeof(duk_catcher) * DUK_CATCHSTACK_INITIAL_SIZE;
  47174. thr->catchstack = (duk_catcher *) DUK_ALLOC(heap, alloc_size);
  47175. if (!thr->catchstack) {
  47176. goto fail;
  47177. }
  47178. DUK_MEMZERO(thr->catchstack, alloc_size);
  47179. thr->catchstack_size = DUK_CATCHSTACK_INITIAL_SIZE;
  47180. DUK_ASSERT(thr->catchstack_top == 0);
  47181. return 1;
  47182. fail:
  47183. DUK_FREE(heap, thr->valstack);
  47184. DUK_FREE(heap, thr->callstack);
  47185. DUK_FREE(heap, thr->catchstack);
  47186. thr->valstack = NULL;
  47187. thr->callstack = NULL;
  47188. thr->catchstack = NULL;
  47189. return 0;
  47190. }
  47191. /* For indirect allocs. */
  47192. DUK_INTERNAL void *duk_hthread_get_valstack_ptr(duk_heap *heap, void *ud) {
  47193. duk_hthread *thr = (duk_hthread *) ud;
  47194. DUK_UNREF(heap);
  47195. return (void *) thr->valstack;
  47196. }
  47197. DUK_INTERNAL void *duk_hthread_get_callstack_ptr(duk_heap *heap, void *ud) {
  47198. duk_hthread *thr = (duk_hthread *) ud;
  47199. DUK_UNREF(heap);
  47200. return (void *) thr->callstack;
  47201. }
  47202. DUK_INTERNAL void *duk_hthread_get_catchstack_ptr(duk_heap *heap, void *ud) {
  47203. duk_hthread *thr = (duk_hthread *) ud;
  47204. DUK_UNREF(heap);
  47205. return (void *) thr->catchstack;
  47206. }
  47207. #line 1 "duk_hthread_builtins.c"
  47208. /*
  47209. * Initialize built-in objects. Current thread must have a valstack
  47210. * and initialization errors may longjmp, so a setjmp() catch point
  47211. * must exist.
  47212. */
  47213. /* include removed: duk_internal.h */
  47214. /*
  47215. * Encoding constants, must match genbuiltins.py
  47216. */
  47217. #define DUK__CLASS_BITS 5
  47218. #define DUK__BIDX_BITS 7
  47219. #define DUK__STRIDX_BITS 9 /* XXX: try to optimize to 8 */
  47220. #define DUK__NATIDX_BITS 8
  47221. #define DUK__NUM_NORMAL_PROPS_BITS 6
  47222. #define DUK__NUM_FUNC_PROPS_BITS 6
  47223. #define DUK__PROP_FLAGS_BITS 3
  47224. #define DUK__STRING_LENGTH_BITS 8
  47225. #define DUK__STRING_CHAR_BITS 7
  47226. #define DUK__LENGTH_PROP_BITS 3
  47227. #define DUK__NARGS_BITS 3
  47228. #define DUK__PROP_TYPE_BITS 3
  47229. #define DUK__MAGIC_BITS 16
  47230. #define DUK__NARGS_VARARGS_MARKER 0x07
  47231. #define DUK__NO_CLASS_MARKER 0x00 /* 0 = DUK_HOBJECT_CLASS_UNUSED */
  47232. #define DUK__NO_BIDX_MARKER 0x7f
  47233. #define DUK__NO_STRIDX_MARKER 0xff
  47234. #define DUK__PROP_TYPE_DOUBLE 0
  47235. #define DUK__PROP_TYPE_STRING 1
  47236. #define DUK__PROP_TYPE_STRIDX 2
  47237. #define DUK__PROP_TYPE_BUILTIN 3
  47238. #define DUK__PROP_TYPE_UNDEFINED 4
  47239. #define DUK__PROP_TYPE_BOOLEAN_TRUE 5
  47240. #define DUK__PROP_TYPE_BOOLEAN_FALSE 6
  47241. #define DUK__PROP_TYPE_ACCESSOR 7
  47242. /*
  47243. * Create built-in objects by parsing an init bitstream generated
  47244. * by genbuiltins.py.
  47245. */
  47246. DUK_INTERNAL void duk_hthread_create_builtin_objects(duk_hthread *thr) {
  47247. duk_context *ctx = (duk_context *) thr;
  47248. duk_bitdecoder_ctx bd_ctx;
  47249. duk_bitdecoder_ctx *bd = &bd_ctx; /* convenience */
  47250. duk_hobject *h;
  47251. duk_small_uint_t i, j;
  47252. DUK_D(DUK_DPRINT("INITBUILTINS BEGIN"));
  47253. DUK_MEMZERO(&bd_ctx, sizeof(bd_ctx));
  47254. bd->data = (const duk_uint8_t *) duk_builtins_data;
  47255. bd->length = (duk_size_t) DUK_BUILTINS_DATA_LENGTH;
  47256. /*
  47257. * First create all built-in bare objects on the empty valstack.
  47258. * During init, their indices will correspond to built-in indices.
  47259. *
  47260. * Built-ins will be reachable from both valstack and thr->builtins.
  47261. */
  47262. /* XXX: there is no need to resize valstack because builtin count
  47263. * is much less than the default space; assert for it.
  47264. */
  47265. DUK_DD(DUK_DDPRINT("create empty built-ins"));
  47266. DUK_ASSERT_TOP(ctx, 0);
  47267. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  47268. duk_small_uint_t class_num;
  47269. duk_small_int_t len = -1; /* must be signed */
  47270. class_num = (duk_small_uint_t) duk_bd_decode(bd, DUK__CLASS_BITS);
  47271. len = (duk_small_int_t) duk_bd_decode_flagged(bd, DUK__LENGTH_PROP_BITS, (duk_int32_t) -1 /*def_value*/);
  47272. if (class_num == DUK_HOBJECT_CLASS_FUNCTION) {
  47273. duk_small_uint_t natidx;
  47274. duk_small_uint_t stridx;
  47275. duk_int_t c_nargs; /* must hold DUK_VARARGS */
  47276. duk_c_function c_func;
  47277. duk_int16_t magic;
  47278. DUK_DDD(DUK_DDDPRINT("len=%ld", (long) len));
  47279. DUK_ASSERT(len >= 0);
  47280. natidx = (duk_small_uint_t) duk_bd_decode(bd, DUK__NATIDX_BITS);
  47281. stridx = (duk_small_uint_t) duk_bd_decode(bd, DUK__STRIDX_BITS);
  47282. c_func = duk_bi_native_functions[natidx];
  47283. c_nargs = (duk_small_uint_t) duk_bd_decode_flagged(bd, DUK__NARGS_BITS, len /*def_value*/);
  47284. if (c_nargs == DUK__NARGS_VARARGS_MARKER) {
  47285. c_nargs = DUK_VARARGS;
  47286. }
  47287. /* XXX: set magic directly here? (it could share the c_nargs arg) */
  47288. duk_push_c_function_noexotic(ctx, c_func, c_nargs);
  47289. h = duk_require_hobject(ctx, -1);
  47290. DUK_ASSERT(h != NULL);
  47291. /* Currently all built-in native functions are strict.
  47292. * duk_push_c_function() now sets strict flag, so
  47293. * assert for it.
  47294. */
  47295. DUK_ASSERT(DUK_HOBJECT_HAS_STRICT(h));
  47296. /* XXX: function properties */
  47297. duk_push_hstring_stridx(ctx, stridx);
  47298. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE);
  47299. /* Almost all global level Function objects are constructable
  47300. * but not all: Function.prototype is a non-constructable,
  47301. * callable Function.
  47302. */
  47303. if (duk_bd_decode_flag(bd)) {
  47304. DUK_ASSERT(DUK_HOBJECT_HAS_CONSTRUCTABLE(h));
  47305. } else {
  47306. DUK_HOBJECT_CLEAR_CONSTRUCTABLE(h);
  47307. }
  47308. /* Cast converts magic to 16-bit signed value */
  47309. magic = (duk_int16_t) duk_bd_decode_flagged(bd, DUK__MAGIC_BITS, 0 /*def_value*/);
  47310. ((duk_hnativefunction *) h)->magic = magic;
  47311. } else {
  47312. /* XXX: ARRAY_PART for Array prototype? */
  47313. duk_push_object_helper(ctx,
  47314. DUK_HOBJECT_FLAG_EXTENSIBLE,
  47315. -1); /* no prototype or class yet */
  47316. h = duk_require_hobject(ctx, -1);
  47317. DUK_ASSERT(h != NULL);
  47318. }
  47319. DUK_HOBJECT_SET_CLASS_NUMBER(h, class_num);
  47320. thr->builtins[i] = h;
  47321. DUK_HOBJECT_INCREF(thr, &h->hdr);
  47322. if (len >= 0) {
  47323. /*
  47324. * For top-level objects, 'length' property has the following
  47325. * default attributes: non-writable, non-enumerable, non-configurable
  47326. * (E5 Section 15).
  47327. *
  47328. * However, 'length' property for Array.prototype has attributes
  47329. * expected of an Array instance which are different: writable,
  47330. * non-enumerable, non-configurable (E5 Section 15.4.5.2).
  47331. *
  47332. * This is currently determined implicitly based on class; there are
  47333. * no attribute flags in the init data.
  47334. */
  47335. duk_push_int(ctx, len);
  47336. duk_xdef_prop_stridx(ctx,
  47337. -2,
  47338. DUK_STRIDX_LENGTH,
  47339. (class_num == DUK_HOBJECT_CLASS_ARRAY ? /* only Array.prototype matches */
  47340. DUK_PROPDESC_FLAGS_W : DUK_PROPDESC_FLAGS_NONE));
  47341. }
  47342. /* enable exotic behaviors last */
  47343. if (class_num == DUK_HOBJECT_CLASS_ARRAY) {
  47344. DUK_HOBJECT_SET_EXOTIC_ARRAY(h);
  47345. }
  47346. if (class_num == DUK_HOBJECT_CLASS_STRING) {
  47347. DUK_HOBJECT_SET_EXOTIC_STRINGOBJ(h);
  47348. }
  47349. /* some assertions */
  47350. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(h));
  47351. /* DUK_HOBJECT_FLAG_CONSTRUCTABLE varies */
  47352. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(h));
  47353. DUK_ASSERT(!DUK_HOBJECT_HAS_COMPILEDFUNCTION(h));
  47354. /* DUK_HOBJECT_FLAG_NATIVEFUNCTION varies */
  47355. DUK_ASSERT(!DUK_HOBJECT_HAS_THREAD(h));
  47356. DUK_ASSERT(!DUK_HOBJECT_HAS_ARRAY_PART(h)); /* currently, even for Array.prototype */
  47357. /* DUK_HOBJECT_FLAG_STRICT varies */
  47358. DUK_ASSERT(!DUK_HOBJECT_HAS_NATIVEFUNCTION(h) || /* all native functions have NEWENV */
  47359. DUK_HOBJECT_HAS_NEWENV(h));
  47360. DUK_ASSERT(!DUK_HOBJECT_HAS_NAMEBINDING(h));
  47361. DUK_ASSERT(!DUK_HOBJECT_HAS_CREATEARGS(h));
  47362. DUK_ASSERT(!DUK_HOBJECT_HAS_ENVRECCLOSED(h));
  47363. /* DUK_HOBJECT_FLAG_EXOTIC_ARRAY varies */
  47364. /* DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ varies */
  47365. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(h));
  47366. DUK_DDD(DUK_DDDPRINT("created built-in %ld, class=%ld, length=%ld", (long) i, (long) class_num, (long) len));
  47367. }
  47368. /*
  47369. * Then decode the builtins init data (see genbuiltins.py) to
  47370. * init objects
  47371. */
  47372. DUK_DD(DUK_DDPRINT("initialize built-in object properties"));
  47373. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  47374. duk_small_uint_t t;
  47375. duk_small_uint_t num;
  47376. DUK_DDD(DUK_DDDPRINT("initializing built-in object at index %ld", (long) i));
  47377. h = thr->builtins[i];
  47378. t = (duk_small_uint_t) duk_bd_decode(bd, DUK__BIDX_BITS);
  47379. if (t != DUK__NO_BIDX_MARKER) {
  47380. DUK_DDD(DUK_DDDPRINT("set internal prototype: built-in %ld", (long) t));
  47381. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, thr->builtins[t]);
  47382. }
  47383. t = (duk_small_uint_t) duk_bd_decode(bd, DUK__BIDX_BITS);
  47384. if (t != DUK__NO_BIDX_MARKER) {
  47385. /* 'prototype' property for all built-in objects (which have it) has attributes:
  47386. * [[Writable]] = false,
  47387. * [[Enumerable]] = false,
  47388. * [[Configurable]] = false
  47389. */
  47390. DUK_DDD(DUK_DDDPRINT("set external prototype: built-in %ld", (long) t));
  47391. duk_xdef_prop_stridx_builtin(ctx, i, DUK_STRIDX_PROTOTYPE, t, DUK_PROPDESC_FLAGS_NONE);
  47392. }
  47393. t = (duk_small_uint_t) duk_bd_decode(bd, DUK__BIDX_BITS);
  47394. if (t != DUK__NO_BIDX_MARKER) {
  47395. /* 'constructor' property for all built-in objects (which have it) has attributes:
  47396. * [[Writable]] = true,
  47397. * [[Enumerable]] = false,
  47398. * [[Configurable]] = true
  47399. */
  47400. DUK_DDD(DUK_DDDPRINT("set external constructor: built-in %ld", (long) t));
  47401. duk_xdef_prop_stridx_builtin(ctx, i, DUK_STRIDX_CONSTRUCTOR, t, DUK_PROPDESC_FLAGS_WC);
  47402. }
  47403. /* normal valued properties */
  47404. num = (duk_small_uint_t) duk_bd_decode(bd, DUK__NUM_NORMAL_PROPS_BITS);
  47405. DUK_DDD(DUK_DDDPRINT("built-in object %ld, %ld normal valued properties", (long) i, (long) num));
  47406. for (j = 0; j < num; j++) {
  47407. duk_small_uint_t stridx;
  47408. duk_small_uint_t prop_flags;
  47409. stridx = (duk_small_uint_t) duk_bd_decode(bd, DUK__STRIDX_BITS);
  47410. /*
  47411. * Property attribute defaults are defined in E5 Section 15 (first
  47412. * few pages); there is a default for all properties and a special
  47413. * default for 'length' properties. Variation from the defaults is
  47414. * signaled using a single flag bit in the bitstream.
  47415. */
  47416. if (duk_bd_decode_flag(bd)) {
  47417. prop_flags = (duk_small_uint_t) duk_bd_decode(bd, DUK__PROP_FLAGS_BITS);
  47418. } else {
  47419. if (stridx == DUK_STRIDX_LENGTH) {
  47420. prop_flags = DUK_PROPDESC_FLAGS_NONE;
  47421. } else {
  47422. prop_flags = DUK_PROPDESC_FLAGS_WC;
  47423. }
  47424. }
  47425. t = (duk_small_uint_t) duk_bd_decode(bd, DUK__PROP_TYPE_BITS);
  47426. DUK_DDD(DUK_DDDPRINT("built-in %ld, normal-valued property %ld, stridx %ld, flags 0x%02lx, type %ld",
  47427. (long) i, (long) j, (long) stridx, (unsigned long) prop_flags, (long) t));
  47428. switch (t) {
  47429. case DUK__PROP_TYPE_DOUBLE: {
  47430. duk_double_union du;
  47431. duk_small_uint_t k;
  47432. for (k = 0; k < 8; k++) {
  47433. /* Encoding endianness must match target memory layout,
  47434. * build scripts and genbuiltins.py must ensure this.
  47435. */
  47436. du.uc[k] = (duk_uint8_t) duk_bd_decode(bd, 8);
  47437. }
  47438. duk_push_number(ctx, du.d); /* push operation normalizes NaNs */
  47439. break;
  47440. }
  47441. case DUK__PROP_TYPE_STRING: {
  47442. duk_small_uint_t n;
  47443. duk_small_uint_t k;
  47444. duk_uint8_t *p;
  47445. n = (duk_small_uint_t) duk_bd_decode(bd, DUK__STRING_LENGTH_BITS);
  47446. p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, n);
  47447. for (k = 0; k < n; k++) {
  47448. *p++ = (duk_uint8_t) duk_bd_decode(bd, DUK__STRING_CHAR_BITS);
  47449. }
  47450. duk_to_string(ctx, -1);
  47451. break;
  47452. }
  47453. case DUK__PROP_TYPE_STRIDX: {
  47454. duk_small_uint_t n;
  47455. n = (duk_small_uint_t) duk_bd_decode(bd, DUK__STRIDX_BITS);
  47456. DUK_ASSERT_DISABLE(n >= 0); /* unsigned */
  47457. DUK_ASSERT(n < DUK_HEAP_NUM_STRINGS);
  47458. duk_push_hstring_stridx(ctx, n);
  47459. break;
  47460. }
  47461. case DUK__PROP_TYPE_BUILTIN: {
  47462. duk_small_uint_t bidx;
  47463. bidx = (duk_small_uint_t) duk_bd_decode(bd, DUK__BIDX_BITS);
  47464. DUK_ASSERT(bidx != DUK__NO_BIDX_MARKER);
  47465. duk_dup(ctx, (duk_idx_t) bidx);
  47466. break;
  47467. }
  47468. case DUK__PROP_TYPE_UNDEFINED: {
  47469. duk_push_undefined(ctx);
  47470. break;
  47471. }
  47472. case DUK__PROP_TYPE_BOOLEAN_TRUE: {
  47473. duk_push_true(ctx);
  47474. break;
  47475. }
  47476. case DUK__PROP_TYPE_BOOLEAN_FALSE: {
  47477. duk_push_false(ctx);
  47478. break;
  47479. }
  47480. case DUK__PROP_TYPE_ACCESSOR: {
  47481. duk_small_uint_t natidx_getter = (duk_small_uint_t) duk_bd_decode(bd, DUK__NATIDX_BITS);
  47482. duk_small_uint_t natidx_setter = (duk_small_uint_t) duk_bd_decode(bd, DUK__NATIDX_BITS);
  47483. duk_c_function c_func_getter;
  47484. duk_c_function c_func_setter;
  47485. /* XXX: this is a bit awkward because there is no exposed helper
  47486. * in the API style, only this internal helper.
  47487. */
  47488. DUK_DDD(DUK_DDDPRINT("built-in accessor property: objidx=%ld, stridx=%ld, getteridx=%ld, setteridx=%ld, flags=0x%04lx",
  47489. (long) i, (long) stridx, (long) natidx_getter, (long) natidx_setter, (unsigned long) prop_flags));
  47490. c_func_getter = duk_bi_native_functions[natidx_getter];
  47491. c_func_setter = duk_bi_native_functions[natidx_setter];
  47492. duk_push_c_function_noconstruct_noexotic(ctx, c_func_getter, 0); /* always 0 args */
  47493. duk_push_c_function_noconstruct_noexotic(ctx, c_func_setter, 1); /* always 1 arg */
  47494. /* XXX: magic for getter/setter? */
  47495. prop_flags |= DUK_PROPDESC_FLAG_ACCESSOR; /* accessor flag not encoded explicitly */
  47496. duk_hobject_define_accessor_internal(thr,
  47497. duk_require_hobject(ctx, i),
  47498. DUK_HTHREAD_GET_STRING(thr, stridx),
  47499. duk_require_hobject(ctx, -2),
  47500. duk_require_hobject(ctx, -1),
  47501. prop_flags);
  47502. duk_pop_2(ctx); /* getter and setter, now reachable through object */
  47503. goto skip_value;
  47504. }
  47505. default: {
  47506. /* exhaustive */
  47507. DUK_UNREACHABLE();
  47508. }
  47509. }
  47510. DUK_ASSERT((prop_flags & DUK_PROPDESC_FLAG_ACCESSOR) == 0);
  47511. duk_xdef_prop_stridx(ctx, i, stridx, prop_flags);
  47512. skip_value:
  47513. continue; /* avoid empty label at the end of a compound statement */
  47514. }
  47515. /* native function properties */
  47516. num = (duk_small_uint_t) duk_bd_decode(bd, DUK__NUM_FUNC_PROPS_BITS);
  47517. DUK_DDD(DUK_DDDPRINT("built-in object %ld, %ld function valued properties", (long) i, (long) num));
  47518. for (j = 0; j < num; j++) {
  47519. duk_small_uint_t stridx;
  47520. duk_small_uint_t natidx;
  47521. duk_int_t c_nargs; /* must hold DUK_VARARGS */
  47522. duk_small_uint_t c_length;
  47523. duk_int16_t magic;
  47524. duk_c_function c_func;
  47525. duk_hnativefunction *h_func;
  47526. #if defined(DUK_USE_LIGHTFUNC_BUILTINS)
  47527. duk_small_int_t lightfunc_eligible;
  47528. #endif
  47529. stridx = (duk_small_uint_t) duk_bd_decode(bd, DUK__STRIDX_BITS);
  47530. natidx = (duk_small_uint_t) duk_bd_decode(bd, DUK__NATIDX_BITS);
  47531. c_length = (duk_small_uint_t) duk_bd_decode(bd, DUK__LENGTH_PROP_BITS);
  47532. c_nargs = (duk_int_t) duk_bd_decode_flagged(bd, DUK__NARGS_BITS, (duk_int32_t) c_length /*def_value*/);
  47533. if (c_nargs == DUK__NARGS_VARARGS_MARKER) {
  47534. c_nargs = DUK_VARARGS;
  47535. }
  47536. c_func = duk_bi_native_functions[natidx];
  47537. DUK_DDD(DUK_DDDPRINT("built-in %ld, function-valued property %ld, stridx %ld, natidx %ld, length %ld, nargs %ld",
  47538. (long) i, (long) j, (long) stridx, (long) natidx, (long) c_length,
  47539. (c_nargs == DUK_VARARGS ? (long) -1 : (long) c_nargs)));
  47540. /* Cast converts magic to 16-bit signed value */
  47541. magic = (duk_int16_t) duk_bd_decode_flagged(bd, DUK__MAGIC_BITS, 0);
  47542. #if defined(DUK_USE_LIGHTFUNC_BUILTINS)
  47543. lightfunc_eligible =
  47544. ((c_nargs >= DUK_LFUNC_NARGS_MIN && c_nargs <= DUK_LFUNC_NARGS_MAX) || (c_nargs == DUK_VARARGS)) &&
  47545. (c_length <= DUK_LFUNC_LENGTH_MAX) &&
  47546. (magic >= DUK_LFUNC_MAGIC_MIN && magic <= DUK_LFUNC_MAGIC_MAX);
  47547. if (stridx == DUK_STRIDX_EVAL ||
  47548. stridx == DUK_STRIDX_YIELD ||
  47549. stridx == DUK_STRIDX_RESUME ||
  47550. stridx == DUK_STRIDX_REQUIRE) {
  47551. /* These functions have trouble working as lightfuncs.
  47552. * Some of them have specific asserts and some may have
  47553. * additional properties (e.g. 'require.id' may be written).
  47554. */
  47555. DUK_D(DUK_DPRINT("reject as lightfunc: stridx=%d, i=%d, j=%d", (int) stridx, (int) i, (int) j));
  47556. lightfunc_eligible = 0;
  47557. }
  47558. if (lightfunc_eligible) {
  47559. duk_tval tv_lfunc;
  47560. duk_small_uint_t lf_nargs = (c_nargs == DUK_VARARGS ? DUK_LFUNC_NARGS_VARARGS : c_nargs);
  47561. duk_small_uint_t lf_flags = DUK_LFUNC_FLAGS_PACK(magic, c_length, lf_nargs);
  47562. DUK_TVAL_SET_LIGHTFUNC(&tv_lfunc, c_func, lf_flags);
  47563. duk_push_tval(ctx, &tv_lfunc);
  47564. 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(ctx, -1)));
  47565. goto lightfunc_skip;
  47566. }
  47567. 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));
  47568. #endif /* DUK_USE_LIGHTFUNC_BUILTINS */
  47569. /* [ (builtin objects) ] */
  47570. duk_push_c_function_noconstruct_noexotic(ctx, c_func, c_nargs);
  47571. h_func = duk_require_hnativefunction(ctx, -1);
  47572. DUK_UNREF(h_func);
  47573. /* Currently all built-in native functions are strict.
  47574. * This doesn't matter for many functions, but e.g.
  47575. * String.prototype.charAt (and other string functions)
  47576. * rely on being strict so that their 'this' binding is
  47577. * not automatically coerced.
  47578. */
  47579. DUK_HOBJECT_SET_STRICT((duk_hobject *) h_func);
  47580. /* No built-in functions are constructable except the top
  47581. * level ones (Number, etc).
  47582. */
  47583. DUK_ASSERT(!DUK_HOBJECT_HAS_CONSTRUCTABLE((duk_hobject *) h_func));
  47584. /* XXX: any way to avoid decoding magic bit; there are quite
  47585. * many function properties and relatively few with magic values.
  47586. */
  47587. h_func->magic = magic;
  47588. /* [ (builtin objects) func ] */
  47589. duk_push_int(ctx, c_length);
  47590. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE);
  47591. duk_push_hstring_stridx(ctx, stridx);
  47592. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE);
  47593. /* XXX: other properties of function instances; 'arguments', 'caller'. */
  47594. DUK_DD(DUK_DDPRINT("built-in object %ld, function property %ld -> %!T",
  47595. (long) i, (long) j, (duk_tval *) duk_get_tval(ctx, -1)));
  47596. /* [ (builtin objects) func ] */
  47597. /*
  47598. * The default property attributes are correct for all
  47599. * function valued properties of built-in objects now.
  47600. */
  47601. #if defined(DUK_USE_LIGHTFUNC_BUILTINS)
  47602. lightfunc_skip:
  47603. #endif
  47604. duk_xdef_prop_stridx(ctx, i, stridx, DUK_PROPDESC_FLAGS_WC);
  47605. /* [ (builtin objects) ] */
  47606. }
  47607. }
  47608. /*
  47609. * Special post-tweaks, for cases not covered by the init data format.
  47610. *
  47611. * - Set Date.prototype.toGMTString to Date.prototype.toUTCString.
  47612. * toGMTString is required to have the same Function object as
  47613. * toUTCString in E5 Section B.2.6. Note that while Smjs respects
  47614. * this, V8 does not (the Function objects are distinct).
  47615. *
  47616. * - Make DoubleError non-extensible.
  47617. *
  47618. * - Add info about most important effective compile options to Duktape.
  47619. *
  47620. * - Possibly remove some properties (values or methods) which are not
  47621. * desirable with current feature options but are not currently
  47622. * conditional in init data.
  47623. */
  47624. duk_get_prop_stridx(ctx, DUK_BIDX_DATE_PROTOTYPE, DUK_STRIDX_TO_UTC_STRING);
  47625. duk_xdef_prop_stridx(ctx, DUK_BIDX_DATE_PROTOTYPE, DUK_STRIDX_TO_GMT_STRING, DUK_PROPDESC_FLAGS_WC);
  47626. h = duk_require_hobject(ctx, DUK_BIDX_DOUBLE_ERROR);
  47627. DUK_ASSERT(h != NULL);
  47628. DUK_HOBJECT_CLEAR_EXTENSIBLE(h);
  47629. #if !defined(DUK_USE_ES6_OBJECT_PROTO_PROPERTY)
  47630. DUK_DD(DUK_DDPRINT("delete Object.prototype.__proto__ built-in which is not enabled in features"));
  47631. (void) duk_hobject_delprop_raw(thr, thr->builtins[DUK_BIDX_OBJECT_PROTOTYPE], DUK_HTHREAD_STRING___PROTO__(thr), DUK_DELPROP_FLAG_THROW);
  47632. #endif
  47633. #if !defined(DUK_USE_ES6_OBJECT_SETPROTOTYPEOF)
  47634. DUK_DD(DUK_DDPRINT("delete Object.setPrototypeOf built-in which is not enabled in features"));
  47635. (void) duk_hobject_delprop_raw(thr, thr->builtins[DUK_BIDX_OBJECT_CONSTRUCTOR], DUK_HTHREAD_STRING_SET_PROTOTYPE_OF(thr), DUK_DELPROP_FLAG_THROW);
  47636. #endif
  47637. duk_push_string(ctx,
  47638. /* Endianness indicator */
  47639. #if defined(DUK_USE_INTEGER_LE)
  47640. "l"
  47641. #elif defined(DUK_USE_INTEGER_BE)
  47642. "b"
  47643. #elif defined(DUK_USE_INTEGER_ME) /* integer mixed endian not really used now */
  47644. "m"
  47645. #else
  47646. "?"
  47647. #endif
  47648. #if defined(DUK_USE_DOUBLE_LE)
  47649. "l"
  47650. #elif defined(DUK_USE_DOUBLE_BE)
  47651. "b"
  47652. #elif defined(DUK_USE_DOUBLE_ME)
  47653. "m"
  47654. #else
  47655. "?"
  47656. #endif
  47657. #if defined(DUK_USE_BYTEORDER_FORCED)
  47658. "f"
  47659. #endif
  47660. " "
  47661. /* Packed or unpacked tval */
  47662. #if defined(DUK_USE_PACKED_TVAL)
  47663. "p"
  47664. #else
  47665. "u"
  47666. #endif
  47667. #if defined(DUK_USE_FASTINT)
  47668. "f"
  47669. #endif
  47670. " "
  47671. /* Low memory options */
  47672. #if defined(DUK_USE_STRTAB_CHAIN)
  47673. "c" /* chain */
  47674. #elif defined(DUK_USE_STRTAB_PROBE)
  47675. "p" /* probe */
  47676. #else
  47677. "?"
  47678. #endif
  47679. #if !defined(DUK_USE_HEAPPTR16) && !defined(DUK_DATAPTR16) && !defined(DUK_FUNCPTR16)
  47680. "n"
  47681. #endif
  47682. #if defined(DUK_USE_HEAPPTR16)
  47683. "h"
  47684. #endif
  47685. #if defined(DUK_USE_DATAPTR16)
  47686. "d"
  47687. #endif
  47688. #if defined(DUK_USE_FUNCPTR16)
  47689. "f"
  47690. #endif
  47691. #if defined(DUK_USE_REFCOUNT16)
  47692. "R"
  47693. #endif
  47694. #if defined(DUK_USE_STRHASH16)
  47695. "H"
  47696. #endif
  47697. #if defined(DUK_USE_STRLEN16)
  47698. "S"
  47699. #endif
  47700. #if defined(DUK_USE_BUFLEN16)
  47701. "B"
  47702. #endif
  47703. #if defined(DUK_USE_OBJSIZES16)
  47704. "O"
  47705. #endif
  47706. #if defined(DUK_USE_LIGHTFUNC_BUILTINS)
  47707. "L"
  47708. #endif
  47709. " "
  47710. /* Object property allocation layout */
  47711. #if defined(DUK_USE_HOBJECT_LAYOUT_1)
  47712. "p1"
  47713. #elif defined(DUK_USE_HOBJECT_LAYOUT_2)
  47714. "p2"
  47715. #elif defined(DUK_USE_HOBJECT_LAYOUT_3)
  47716. "p3"
  47717. #else
  47718. "p?"
  47719. #endif
  47720. " "
  47721. /* Alignment guarantee */
  47722. #if (DUK_USE_ALIGN_BY == 4)
  47723. "a4"
  47724. #elif (DUK_USE_ALIGN_BY == 8)
  47725. "a8"
  47726. #elif (DUK_USE_ALIGN_BY == 1)
  47727. "a1"
  47728. #else
  47729. #error invalid DUK_USE_ALIGN_BY
  47730. #endif
  47731. " "
  47732. /* Architecture, OS, and compiler strings */
  47733. DUK_USE_ARCH_STRING
  47734. " "
  47735. DUK_USE_OS_STRING
  47736. " "
  47737. DUK_USE_COMPILER_STRING);
  47738. duk_xdef_prop_stridx(ctx, DUK_BIDX_DUKTAPE, DUK_STRIDX_ENV, DUK_PROPDESC_FLAGS_WC);
  47739. /*
  47740. * InitJS code - Ecmascript code evaluated from a built-in source
  47741. * which provides e.g. backward compatibility. User can also provide
  47742. * JS code to be evaluated at startup.
  47743. */
  47744. #ifdef DUK_USE_BUILTIN_INITJS
  47745. /* XXX: compression */
  47746. DUK_DD(DUK_DDPRINT("running built-in initjs"));
  47747. duk_eval_string(ctx, (const char *) duk_initjs_data); /* initjs data is NUL terminated */
  47748. duk_pop(ctx);
  47749. #endif /* DUK_USE_BUILTIN_INITJS */
  47750. #ifdef DUK_USE_USER_INITJS
  47751. /* XXX: compression (as an option) */
  47752. DUK_DD(DUK_DDPRINT("running user initjs"));
  47753. duk_eval_string_noresult(ctx, (const char *) DUK_USE_USER_INITJS);
  47754. #endif /* DUK_USE_USER_INITJS */
  47755. /*
  47756. * Since built-ins are not often extended, compact them.
  47757. */
  47758. DUK_DD(DUK_DDPRINT("compact built-ins"));
  47759. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  47760. duk_hobject_compact_props(thr, thr->builtins[i]);
  47761. }
  47762. DUK_D(DUK_DPRINT("INITBUILTINS END"));
  47763. #ifdef DUK_USE_DDPRINT
  47764. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  47765. DUK_DD(DUK_DDPRINT("built-in object %ld after initialization and compacting: %!@iO",
  47766. (long) i, (duk_heaphdr *) thr->builtins[i]));
  47767. }
  47768. #endif
  47769. /*
  47770. * Pop built-ins from stack: they are now INCREF'd and
  47771. * reachable from the builtins[] array.
  47772. */
  47773. duk_pop_n(ctx, DUK_NUM_BUILTINS);
  47774. DUK_ASSERT_TOP(ctx, 0);
  47775. }
  47776. DUK_INTERNAL void duk_hthread_copy_builtin_objects(duk_hthread *thr_from, duk_hthread *thr_to) {
  47777. duk_small_uint_t i;
  47778. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  47779. thr_to->builtins[i] = thr_from->builtins[i];
  47780. DUK_HOBJECT_INCREF_ALLOWNULL(thr_to, thr_to->builtins[i]); /* side effect free */
  47781. }
  47782. }
  47783. #line 1 "duk_hthread_misc.c"
  47784. /*
  47785. * Thread support.
  47786. */
  47787. /* include removed: duk_internal.h */
  47788. DUK_INTERNAL void duk_hthread_terminate(duk_hthread *thr) {
  47789. DUK_ASSERT(thr != NULL);
  47790. /* Order of unwinding is important */
  47791. duk_hthread_catchstack_unwind(thr, 0);
  47792. duk_hthread_callstack_unwind(thr, 0); /* side effects, possibly errors */
  47793. thr->valstack_bottom = thr->valstack;
  47794. duk_set_top((duk_context *) thr, 0); /* unwinds valstack, updating refcounts */
  47795. thr->state = DUK_HTHREAD_STATE_TERMINATED;
  47796. /* Here we could remove references to built-ins, but it may not be
  47797. * worth the effort because built-ins are quite likely to be shared
  47798. * with another (unterminated) thread, and terminated threads are also
  47799. * usually garbage collected quite quickly. Also, doing DECREFs
  47800. * could trigger finalization, which would run on the current thread
  47801. * and have access to only some of the built-ins. Garbage collection
  47802. * deals with this correctly already.
  47803. */
  47804. /* XXX: Shrink the stacks to minimize memory usage? May not
  47805. * be worth the effort because terminated threads are usually
  47806. * garbage collected quite soon.
  47807. */
  47808. }
  47809. DUK_INTERNAL duk_activation *duk_hthread_get_current_activation(duk_hthread *thr) {
  47810. DUK_ASSERT(thr != NULL);
  47811. if (thr->callstack_top > 0) {
  47812. return thr->callstack + thr->callstack_top - 1;
  47813. } else {
  47814. return NULL;
  47815. }
  47816. }
  47817. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  47818. DUK_INTERNAL duk_uint_fast32_t duk_hthread_get_act_curr_pc(duk_hthread *thr, duk_activation *act) {
  47819. duk_instr_t *bcode;
  47820. DUK_ASSERT(thr != NULL);
  47821. DUK_ASSERT(act != NULL);
  47822. DUK_UNREF(thr);
  47823. /* XXX: store 'bcode' pointer to activation for faster lookup? */
  47824. if (act->func && DUK_HOBJECT_IS_COMPILEDFUNCTION(act->func)) {
  47825. bcode = DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, (duk_hcompiledfunction *) (act->func));
  47826. return (duk_uint_fast32_t) (act->curr_pc - bcode);
  47827. }
  47828. return 0;
  47829. }
  47830. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  47831. DUK_INTERNAL duk_uint_fast32_t duk_hthread_get_act_prev_pc(duk_hthread *thr, duk_activation *act) {
  47832. duk_instr_t *bcode;
  47833. duk_uint_fast32_t ret;
  47834. DUK_ASSERT(thr != NULL);
  47835. DUK_ASSERT(act != NULL);
  47836. DUK_UNREF(thr);
  47837. if (act->func && DUK_HOBJECT_IS_COMPILEDFUNCTION(act->func)) {
  47838. bcode = DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, (duk_hcompiledfunction *) (act->func));
  47839. ret = (duk_uint_fast32_t) (act->curr_pc - bcode);
  47840. if (ret > 0) {
  47841. ret--;
  47842. }
  47843. return ret;
  47844. }
  47845. return 0;
  47846. }
  47847. /* Write bytecode executor's curr_pc back to topmost activation (if any). */
  47848. DUK_INTERNAL void duk_hthread_sync_currpc(duk_hthread *thr) {
  47849. duk_activation *act;
  47850. DUK_ASSERT(thr != NULL);
  47851. if (thr->ptr_curr_pc != NULL) {
  47852. /* ptr_curr_pc != NULL only when bytecode executor is active. */
  47853. DUK_ASSERT(thr->callstack_top > 0);
  47854. act = thr->callstack + thr->callstack_top - 1;
  47855. act->curr_pc = *thr->ptr_curr_pc;
  47856. }
  47857. }
  47858. DUK_INTERNAL void duk_hthread_sync_and_null_currpc(duk_hthread *thr) {
  47859. duk_activation *act;
  47860. DUK_ASSERT(thr != NULL);
  47861. if (thr->ptr_curr_pc != NULL) {
  47862. /* ptr_curr_pc != NULL only when bytecode executor is active. */
  47863. DUK_ASSERT(thr->callstack_top > 0);
  47864. act = thr->callstack + thr->callstack_top - 1;
  47865. act->curr_pc = *thr->ptr_curr_pc;
  47866. thr->ptr_curr_pc = NULL;
  47867. }
  47868. }
  47869. #line 1 "duk_hthread_stacks.c"
  47870. /*
  47871. * Manipulation of thread stacks (valstack, callstack, catchstack).
  47872. *
  47873. * Ideally unwinding of stacks should have no side effects, which would
  47874. * then favor separate unwinding and shrink check primitives for each
  47875. * stack type. A shrink check may realloc and thus have side effects.
  47876. *
  47877. * However, currently callstack unwinding itself has side effects, as it
  47878. * needs to DECREF multiple objects, close environment records, etc.
  47879. * Stacks must thus be unwound in the correct order by the caller.
  47880. *
  47881. * (XXX: This should be probably reworked so that there is a shared
  47882. * unwind primitive which handles all stacks as requested, and knows
  47883. * the proper order for unwinding.)
  47884. *
  47885. * Valstack entries above 'top' are always kept initialized to
  47886. * "undefined unused". Callstack and catchstack entries above 'top'
  47887. * are not zeroed and are left as garbage.
  47888. *
  47889. * Value stack handling is mostly a part of the API implementation.
  47890. */
  47891. /* include removed: duk_internal.h */
  47892. /* check that there is space for at least one new entry */
  47893. DUK_INTERNAL void duk_hthread_callstack_grow(duk_hthread *thr) {
  47894. duk_activation *new_ptr;
  47895. duk_size_t old_size;
  47896. duk_size_t new_size;
  47897. DUK_ASSERT(thr != NULL);
  47898. DUK_ASSERT_DISABLE(thr->callstack_top >= 0); /* avoid warning (unsigned) */
  47899. DUK_ASSERT(thr->callstack_size >= thr->callstack_top);
  47900. if (thr->callstack_top < thr->callstack_size) {
  47901. return;
  47902. }
  47903. old_size = thr->callstack_size;
  47904. new_size = old_size + DUK_CALLSTACK_GROW_STEP;
  47905. /* this is a bit approximate (errors out before max is reached); this is OK */
  47906. if (new_size >= thr->callstack_max) {
  47907. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_CALLSTACK_LIMIT);
  47908. }
  47909. DUK_DD(DUK_DDPRINT("growing callstack %ld -> %ld", (long) old_size, (long) new_size));
  47910. /*
  47911. * Note: must use indirect variant of DUK_REALLOC() because underlying
  47912. * pointer may be changed by mark-and-sweep.
  47913. */
  47914. DUK_ASSERT(new_size > 0);
  47915. new_ptr = (duk_activation *) DUK_REALLOC_INDIRECT(thr->heap, duk_hthread_get_callstack_ptr, (void *) thr, sizeof(duk_activation) * new_size);
  47916. if (!new_ptr) {
  47917. /* No need for a NULL/zero-size check because new_size > 0) */
  47918. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_REALLOC_FAILED);
  47919. }
  47920. thr->callstack = new_ptr;
  47921. thr->callstack_size = new_size;
  47922. /* note: any entries above the callstack top are garbage and not zeroed */
  47923. }
  47924. DUK_INTERNAL void duk_hthread_callstack_shrink_check(duk_hthread *thr) {
  47925. duk_size_t new_size;
  47926. duk_activation *p;
  47927. DUK_ASSERT(thr != NULL);
  47928. DUK_ASSERT_DISABLE(thr->callstack_top >= 0); /* avoid warning (unsigned) */
  47929. DUK_ASSERT(thr->callstack_size >= thr->callstack_top);
  47930. if (thr->callstack_size - thr->callstack_top < DUK_CALLSTACK_SHRINK_THRESHOLD) {
  47931. return;
  47932. }
  47933. new_size = thr->callstack_top + DUK_CALLSTACK_SHRINK_SPARE;
  47934. DUK_ASSERT(new_size >= thr->callstack_top);
  47935. DUK_DD(DUK_DDPRINT("shrinking callstack %ld -> %ld", (long) thr->callstack_size, (long) new_size));
  47936. /*
  47937. * Note: must use indirect variant of DUK_REALLOC() because underlying
  47938. * pointer may be changed by mark-and-sweep.
  47939. */
  47940. /* shrink failure is not fatal */
  47941. p = (duk_activation *) DUK_REALLOC_INDIRECT(thr->heap, duk_hthread_get_callstack_ptr, (void *) thr, sizeof(duk_activation) * new_size);
  47942. if (p) {
  47943. thr->callstack = p;
  47944. thr->callstack_size = new_size;
  47945. } else {
  47946. /* Because new_size != 0, if condition doesn't need to be
  47947. * (p != NULL || new_size == 0).
  47948. */
  47949. DUK_ASSERT(new_size != 0);
  47950. DUK_D(DUK_DPRINT("callstack shrink failed, ignoring"));
  47951. }
  47952. /* note: any entries above the callstack top are garbage and not zeroed */
  47953. }
  47954. DUK_INTERNAL void duk_hthread_callstack_unwind(duk_hthread *thr, duk_size_t new_top) {
  47955. duk_size_t idx;
  47956. DUK_DDD(DUK_DDDPRINT("unwind callstack top of thread %p from %ld to %ld",
  47957. (void *) thr,
  47958. (thr != NULL ? (long) thr->callstack_top : (long) -1),
  47959. (long) new_top));
  47960. DUK_ASSERT(thr);
  47961. DUK_ASSERT(thr->heap);
  47962. DUK_ASSERT_DISABLE(new_top >= 0); /* unsigned */
  47963. DUK_ASSERT((duk_size_t) new_top <= thr->callstack_top); /* cannot grow */
  47964. /*
  47965. * The loop below must avoid issues with potential callstack
  47966. * reallocations. A resize (and other side effects) may happen
  47967. * e.g. due to finalizer/errhandler calls caused by a refzero or
  47968. * mark-and-sweep. Arbitrary finalizers may run, because when
  47969. * an environment record is refzero'd, it may refer to arbitrary
  47970. * values which also become refzero'd.
  47971. *
  47972. * So, the pointer 'p' is re-looked-up below whenever a side effect
  47973. * might have changed it.
  47974. */
  47975. idx = thr->callstack_top;
  47976. while (idx > new_top) {
  47977. duk_activation *act;
  47978. duk_hobject *func;
  47979. #ifdef DUK_USE_REFERENCE_COUNTING
  47980. duk_hobject *tmp;
  47981. #endif
  47982. #ifdef DUK_USE_DEBUGGER_SUPPORT
  47983. duk_heap *heap;
  47984. #endif
  47985. idx--;
  47986. DUK_ASSERT_DISABLE(idx >= 0); /* unsigned */
  47987. DUK_ASSERT((duk_size_t) idx < thr->callstack_size); /* true, despite side effect resizes */
  47988. act = thr->callstack + idx;
  47989. /* With lightfuncs, act 'func' may be NULL */
  47990. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  47991. /*
  47992. * Restore 'caller' property for non-strict callee functions.
  47993. */
  47994. func = DUK_ACT_GET_FUNC(act);
  47995. if (func != NULL && !DUK_HOBJECT_HAS_STRICT(func)) {
  47996. duk_tval *tv_caller;
  47997. duk_tval tv_tmp;
  47998. duk_hobject *h_tmp;
  47999. tv_caller = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_CALLER(thr));
  48000. /* The act->prev_caller should only be set if the entry for 'caller'
  48001. * exists (as it is only set in that case, and the property is not
  48002. * configurable), but handle all the cases anyway.
  48003. */
  48004. if (tv_caller) {
  48005. DUK_TVAL_SET_TVAL(&tv_tmp, tv_caller);
  48006. if (act->prev_caller) {
  48007. /* Just transfer the refcount from act->prev_caller to tv_caller,
  48008. * so no need for a refcount update. This is the expected case.
  48009. */
  48010. DUK_TVAL_SET_OBJECT(tv_caller, act->prev_caller);
  48011. act->prev_caller = NULL;
  48012. } else {
  48013. DUK_TVAL_SET_NULL(tv_caller); /* no incref needed */
  48014. DUK_ASSERT(act->prev_caller == NULL);
  48015. }
  48016. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  48017. } else {
  48018. h_tmp = act->prev_caller;
  48019. if (h_tmp) {
  48020. act->prev_caller = NULL;
  48021. DUK_HOBJECT_DECREF(thr, h_tmp); /* side effects */
  48022. }
  48023. }
  48024. act = thr->callstack + idx; /* avoid side effects */
  48025. DUK_ASSERT(act->prev_caller == NULL);
  48026. }
  48027. #endif
  48028. /*
  48029. * Unwind debugger state. If we unwind while stepping
  48030. * (either step over or step into), pause execution.
  48031. */
  48032. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  48033. heap = thr->heap;
  48034. if (heap->dbg_step_thread == thr &&
  48035. heap->dbg_step_csindex == idx) {
  48036. /* Pause for all step types: step into, step over, step out.
  48037. * This is the only place explicitly handling a step out.
  48038. */
  48039. DUK_HEAP_SET_PAUSED(heap);
  48040. DUK_ASSERT(heap->dbg_step_thread == NULL);
  48041. }
  48042. #endif
  48043. /*
  48044. * Close environment record(s) if they exist.
  48045. *
  48046. * Only variable environments are closed. If lex_env != var_env, it
  48047. * cannot currently contain any register bound declarations.
  48048. *
  48049. * Only environments created for a NEWENV function are closed. If an
  48050. * environment is created for e.g. an eval call, it must not be closed.
  48051. */
  48052. func = DUK_ACT_GET_FUNC(act);
  48053. if (func != NULL && !DUK_HOBJECT_HAS_NEWENV(func)) {
  48054. DUK_DDD(DUK_DDDPRINT("skip closing environments, envs not owned by this activation"));
  48055. goto skip_env_close;
  48056. }
  48057. /* func is NULL for lightfunc */
  48058. DUK_ASSERT(act->lex_env == act->var_env);
  48059. if (act->var_env != NULL) {
  48060. DUK_DDD(DUK_DDDPRINT("closing var_env record %p -> %!O",
  48061. (void *) act->var_env, (duk_heaphdr *) act->var_env));
  48062. duk_js_close_environment_record(thr, act->var_env, func, act->idx_bottom);
  48063. act = thr->callstack + idx; /* avoid side effect issues */
  48064. }
  48065. #if 0
  48066. if (act->lex_env != NULL) {
  48067. if (act->lex_env == act->var_env) {
  48068. /* common case, already closed, so skip */
  48069. DUK_DD(DUK_DDPRINT("lex_env and var_env are the same and lex_env "
  48070. "already closed -> skip closing lex_env"));
  48071. ;
  48072. } else {
  48073. DUK_DD(DUK_DDPRINT("closing lex_env record %p -> %!O",
  48074. (void *) act->lex_env, (duk_heaphdr *) act->lex_env));
  48075. duk_js_close_environment_record(thr, act->lex_env, DUK_ACT_GET_FUNC(act), act->idx_bottom);
  48076. act = thr->callstack + idx; /* avoid side effect issues */
  48077. }
  48078. }
  48079. #endif
  48080. DUK_ASSERT((act->lex_env == NULL) ||
  48081. ((duk_hobject_find_existing_entry_tval_ptr(thr->heap, act->lex_env, DUK_HTHREAD_STRING_INT_CALLEE(thr)) == NULL) &&
  48082. (duk_hobject_find_existing_entry_tval_ptr(thr->heap, act->lex_env, DUK_HTHREAD_STRING_INT_VARMAP(thr)) == NULL) &&
  48083. (duk_hobject_find_existing_entry_tval_ptr(thr->heap, act->lex_env, DUK_HTHREAD_STRING_INT_THREAD(thr)) == NULL) &&
  48084. (duk_hobject_find_existing_entry_tval_ptr(thr->heap, act->lex_env, DUK_HTHREAD_STRING_INT_REGBASE(thr)) == NULL)));
  48085. DUK_ASSERT((act->var_env == NULL) ||
  48086. ((duk_hobject_find_existing_entry_tval_ptr(thr->heap, act->var_env, DUK_HTHREAD_STRING_INT_CALLEE(thr)) == NULL) &&
  48087. (duk_hobject_find_existing_entry_tval_ptr(thr->heap, act->var_env, DUK_HTHREAD_STRING_INT_VARMAP(thr)) == NULL) &&
  48088. (duk_hobject_find_existing_entry_tval_ptr(thr->heap, act->var_env, DUK_HTHREAD_STRING_INT_THREAD(thr)) == NULL) &&
  48089. (duk_hobject_find_existing_entry_tval_ptr(thr->heap, act->var_env, DUK_HTHREAD_STRING_INT_REGBASE(thr)) == NULL)));
  48090. skip_env_close:
  48091. /*
  48092. * Update preventcount
  48093. */
  48094. if (act->flags & DUK_ACT_FLAG_PREVENT_YIELD) {
  48095. DUK_ASSERT(thr->callstack_preventcount >= 1);
  48096. thr->callstack_preventcount--;
  48097. }
  48098. /*
  48099. * Reference count updates
  48100. *
  48101. * Note: careful manipulation of refcounts. The top is
  48102. * not updated yet, so all the activations are reachable
  48103. * for mark-and-sweep (which may be triggered by decref).
  48104. * However, the pointers are NULL so this is not an issue.
  48105. */
  48106. #ifdef DUK_USE_REFERENCE_COUNTING
  48107. tmp = act->var_env;
  48108. #endif
  48109. act->var_env = NULL;
  48110. #ifdef DUK_USE_REFERENCE_COUNTING
  48111. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  48112. act = thr->callstack + idx; /* avoid side effect issues */
  48113. #endif
  48114. #ifdef DUK_USE_REFERENCE_COUNTING
  48115. tmp = act->lex_env;
  48116. #endif
  48117. act->lex_env = NULL;
  48118. #ifdef DUK_USE_REFERENCE_COUNTING
  48119. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  48120. act = thr->callstack + idx; /* avoid side effect issues */
  48121. #endif
  48122. /* Note: this may cause a corner case situation where a finalizer
  48123. * may see a currently reachable activation whose 'func' is NULL.
  48124. */
  48125. #ifdef DUK_USE_REFERENCE_COUNTING
  48126. tmp = DUK_ACT_GET_FUNC(act);
  48127. #endif
  48128. act->func = NULL;
  48129. #ifdef DUK_USE_REFERENCE_COUNTING
  48130. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  48131. act = thr->callstack + idx; /* avoid side effect issues */
  48132. DUK_UNREF(act);
  48133. #endif
  48134. }
  48135. thr->callstack_top = new_top;
  48136. /*
  48137. * We could clear the book-keeping variables for the topmost activation,
  48138. * but don't do so now.
  48139. */
  48140. #if 0
  48141. if (thr->callstack_top > 0) {
  48142. duk_activation *act = thr->callstack + thr->callstack_top - 1;
  48143. act->idx_retval = 0;
  48144. }
  48145. #endif
  48146. /* Note: any entries above the callstack top are garbage and not zeroed.
  48147. * Also topmost activation idx_retval is garbage (not zeroed), and must
  48148. * be ignored.
  48149. */
  48150. }
  48151. DUK_INTERNAL void duk_hthread_catchstack_grow(duk_hthread *thr) {
  48152. duk_catcher *new_ptr;
  48153. duk_size_t old_size;
  48154. duk_size_t new_size;
  48155. DUK_ASSERT(thr != NULL);
  48156. DUK_ASSERT_DISABLE(thr->catchstack_top); /* avoid warning (unsigned) */
  48157. DUK_ASSERT(thr->catchstack_size >= thr->catchstack_top);
  48158. if (thr->catchstack_top < thr->catchstack_size) {
  48159. return;
  48160. }
  48161. old_size = thr->catchstack_size;
  48162. new_size = old_size + DUK_CATCHSTACK_GROW_STEP;
  48163. /* this is a bit approximate (errors out before max is reached); this is OK */
  48164. if (new_size >= thr->catchstack_max) {
  48165. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_CATCHSTACK_LIMIT);
  48166. }
  48167. DUK_DD(DUK_DDPRINT("growing catchstack %ld -> %ld", (long) old_size, (long) new_size));
  48168. /*
  48169. * Note: must use indirect variant of DUK_REALLOC() because underlying
  48170. * pointer may be changed by mark-and-sweep.
  48171. */
  48172. DUK_ASSERT(new_size > 0);
  48173. new_ptr = (duk_catcher *) DUK_REALLOC_INDIRECT(thr->heap, duk_hthread_get_catchstack_ptr, (void *) thr, sizeof(duk_catcher) * new_size);
  48174. if (!new_ptr) {
  48175. /* No need for a NULL/zero-size check because new_size > 0) */
  48176. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_REALLOC_FAILED);
  48177. }
  48178. thr->catchstack = new_ptr;
  48179. thr->catchstack_size = new_size;
  48180. /* note: any entries above the catchstack top are garbage and not zeroed */
  48181. }
  48182. DUK_INTERNAL void duk_hthread_catchstack_shrink_check(duk_hthread *thr) {
  48183. duk_size_t new_size;
  48184. duk_catcher *p;
  48185. DUK_ASSERT(thr != NULL);
  48186. DUK_ASSERT_DISABLE(thr->catchstack_top >= 0); /* avoid warning (unsigned) */
  48187. DUK_ASSERT(thr->catchstack_size >= thr->catchstack_top);
  48188. if (thr->catchstack_size - thr->catchstack_top < DUK_CATCHSTACK_SHRINK_THRESHOLD) {
  48189. return;
  48190. }
  48191. new_size = thr->catchstack_top + DUK_CATCHSTACK_SHRINK_SPARE;
  48192. DUK_ASSERT(new_size >= thr->catchstack_top);
  48193. DUK_DD(DUK_DDPRINT("shrinking catchstack %ld -> %ld", (long) thr->catchstack_size, (long) new_size));
  48194. /*
  48195. * Note: must use indirect variant of DUK_REALLOC() because underlying
  48196. * pointer may be changed by mark-and-sweep.
  48197. */
  48198. /* shrink failure is not fatal */
  48199. p = (duk_catcher *) DUK_REALLOC_INDIRECT(thr->heap, duk_hthread_get_catchstack_ptr, (void *) thr, sizeof(duk_catcher) * new_size);
  48200. if (p) {
  48201. thr->catchstack = p;
  48202. thr->catchstack_size = new_size;
  48203. } else {
  48204. /* Because new_size != 0, if condition doesn't need to be
  48205. * (p != NULL || new_size == 0).
  48206. */
  48207. DUK_ASSERT(new_size != 0);
  48208. DUK_D(DUK_DPRINT("catchstack shrink failed, ignoring"));
  48209. }
  48210. /* note: any entries above the catchstack top are garbage and not zeroed */
  48211. }
  48212. DUK_INTERNAL void duk_hthread_catchstack_unwind(duk_hthread *thr, duk_size_t new_top) {
  48213. duk_size_t idx;
  48214. DUK_DDD(DUK_DDDPRINT("unwind catchstack top of thread %p from %ld to %ld",
  48215. (void *) thr,
  48216. (thr != NULL ? (long) thr->catchstack_top : (long) -1),
  48217. (long) new_top));
  48218. DUK_ASSERT(thr);
  48219. DUK_ASSERT(thr->heap);
  48220. DUK_ASSERT_DISABLE(new_top >= 0); /* unsigned */
  48221. DUK_ASSERT((duk_size_t) new_top <= thr->catchstack_top); /* cannot grow */
  48222. /*
  48223. * Since there are no references in the catcher structure,
  48224. * unwinding is quite simple. The only thing we need to
  48225. * look out for is popping a possible lexical environment
  48226. * established for an active catch clause.
  48227. */
  48228. idx = thr->catchstack_top;
  48229. while (idx > new_top) {
  48230. duk_catcher *p;
  48231. duk_activation *act;
  48232. duk_hobject *env;
  48233. idx--;
  48234. DUK_ASSERT_DISABLE(idx >= 0); /* unsigned */
  48235. DUK_ASSERT((duk_size_t) idx < thr->catchstack_size);
  48236. p = thr->catchstack + idx;
  48237. if (DUK_CAT_HAS_LEXENV_ACTIVE(p)) {
  48238. DUK_DDD(DUK_DDDPRINT("unwinding catchstack idx %ld, callstack idx %ld, callstack top %ld: lexical environment active",
  48239. (long) idx, (long) p->callstack_index, (long) thr->callstack_top));
  48240. /* XXX: Here we have a nasty dependency: the need to manipulate
  48241. * the callstack means that catchstack must always be unwound by
  48242. * the caller before unwinding the callstack. This should be fixed
  48243. * later.
  48244. */
  48245. /* Note that multiple catchstack entries may refer to the same
  48246. * callstack entry.
  48247. */
  48248. act = thr->callstack + p->callstack_index;
  48249. DUK_ASSERT(act >= thr->callstack);
  48250. DUK_ASSERT(act < thr->callstack + thr->callstack_top);
  48251. DUK_DDD(DUK_DDDPRINT("catchstack_index=%ld, callstack_index=%ld, lex_env=%!iO",
  48252. (long) idx, (long) p->callstack_index,
  48253. (duk_heaphdr *) act->lex_env));
  48254. env = act->lex_env; /* current lex_env of the activation (created for catcher) */
  48255. DUK_ASSERT(env != NULL); /* must be, since env was created when catcher was created */
  48256. act->lex_env = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, env); /* prototype is lex_env before catcher created */
  48257. DUK_HOBJECT_DECREF(thr, env);
  48258. /* There is no need to decref anything else than 'env': if 'env'
  48259. * becomes unreachable, refzero will handle decref'ing its prototype.
  48260. */
  48261. }
  48262. }
  48263. thr->catchstack_top = new_top;
  48264. /* note: any entries above the catchstack top are garbage and not zeroed */
  48265. }
  48266. #line 1 "duk_js_call.c"
  48267. /*
  48268. * Call handling.
  48269. *
  48270. * The main work horse functions are:
  48271. * - duk_handle_call(): call to a C/Ecmascript functions
  48272. * - duk_handle_safe_call(): make a protected C call within current activation
  48273. * - duk_handle_ecma_call_setup(): Ecmascript-to-Ecmascript calls, including
  48274. * tail calls and coroutine resume
  48275. */
  48276. /* include removed: duk_internal.h */
  48277. /*
  48278. * Misc
  48279. */
  48280. #if defined(DUK_USE_INTERRUPT_COUNTER) && defined(DUK_USE_DEBUG)
  48281. DUK_LOCAL void duk__interrupt_fixup(duk_hthread *thr, duk_hthread *entry_curr_thread) {
  48282. /* XXX: Currently the bytecode executor and executor interrupt
  48283. * instruction counts are off because we don't execute the
  48284. * interrupt handler when we're about to exit from the initial
  48285. * user call into Duktape.
  48286. *
  48287. * If we were to execute the interrupt handler here, the counts
  48288. * would match. You can enable this block manually to check
  48289. * that this is the case.
  48290. */
  48291. DUK_ASSERT(thr != NULL);
  48292. DUK_ASSERT(thr->heap != NULL);
  48293. #if defined(DUK_USE_INTERRUPT_DEBUG_FIXUP)
  48294. if (entry_curr_thread == NULL) {
  48295. thr->interrupt_init = thr->interrupt_init - thr->interrupt_counter;
  48296. thr->heap->inst_count_interrupt += thr->interrupt_init;
  48297. DUK_DD(DUK_DDPRINT("debug test: updated interrupt count on exit to "
  48298. "user code, instruction counts: executor=%ld, interrupt=%ld",
  48299. (long) thr->heap->inst_count_exec, (long) thr->heap->inst_count_interrupt));
  48300. DUK_ASSERT(thr->heap->inst_count_exec == thr->heap->inst_count_interrupt);
  48301. }
  48302. #else
  48303. DUK_UNREF(thr);
  48304. DUK_UNREF(entry_curr_thread);
  48305. #endif
  48306. }
  48307. #endif
  48308. /*
  48309. * Arguments object creation.
  48310. *
  48311. * Creating arguments objects is a bit finicky, see E5 Section 10.6 for the
  48312. * specific requirements. Much of the arguments object exotic behavior is
  48313. * implemented in duk_hobject_props.c, and is enabled by the object flag
  48314. * DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS.
  48315. */
  48316. DUK_LOCAL
  48317. void duk__create_arguments_object(duk_hthread *thr,
  48318. duk_hobject *func,
  48319. duk_hobject *varenv,
  48320. duk_idx_t idx_argbase, /* idx of first argument on stack */
  48321. duk_idx_t num_stack_args) { /* num args starting from idx_argbase */
  48322. duk_context *ctx = (duk_context *) thr;
  48323. duk_hobject *arg; /* 'arguments' */
  48324. duk_hobject *formals; /* formals for 'func' (may be NULL if func is a C function) */
  48325. duk_idx_t i_arg;
  48326. duk_idx_t i_map;
  48327. duk_idx_t i_mappednames;
  48328. duk_idx_t i_formals;
  48329. duk_idx_t i_argbase;
  48330. duk_idx_t n_formals;
  48331. duk_idx_t idx;
  48332. duk_bool_t need_map;
  48333. DUK_DDD(DUK_DDDPRINT("creating arguments object for func=%!iO, varenv=%!iO, "
  48334. "idx_argbase=%ld, num_stack_args=%ld",
  48335. (duk_heaphdr *) func, (duk_heaphdr *) varenv,
  48336. (long) idx_argbase, (long) num_stack_args));
  48337. DUK_ASSERT(thr != NULL);
  48338. DUK_ASSERT(func != NULL);
  48339. DUK_ASSERT(DUK_HOBJECT_IS_NONBOUND_FUNCTION(func));
  48340. DUK_ASSERT(varenv != NULL);
  48341. DUK_ASSERT(idx_argbase >= 0); /* assumed to bottom relative */
  48342. DUK_ASSERT(num_stack_args >= 0);
  48343. need_map = 0;
  48344. i_argbase = idx_argbase;
  48345. DUK_ASSERT(i_argbase >= 0);
  48346. duk_push_hobject(ctx, func);
  48347. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_FORMALS);
  48348. formals = duk_get_hobject(ctx, -1);
  48349. n_formals = 0;
  48350. if (formals) {
  48351. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LENGTH);
  48352. n_formals = (duk_idx_t) duk_require_int(ctx, -1);
  48353. duk_pop(ctx);
  48354. }
  48355. duk_remove(ctx, -2); /* leave formals on stack for later use */
  48356. i_formals = duk_require_top_index(ctx);
  48357. DUK_ASSERT(n_formals >= 0);
  48358. DUK_ASSERT(formals != NULL || n_formals == 0);
  48359. DUK_DDD(DUK_DDDPRINT("func=%!O, formals=%!O, n_formals=%ld",
  48360. (duk_heaphdr *) func, (duk_heaphdr *) formals,
  48361. (long) n_formals));
  48362. /* [ ... formals ] */
  48363. /*
  48364. * Create required objects:
  48365. * - 'arguments' object: array-like, but not an array
  48366. * - 'map' object: internal object, tied to 'arguments'
  48367. * - 'mappedNames' object: temporary value used during construction
  48368. */
  48369. i_arg = duk_push_object_helper(ctx,
  48370. DUK_HOBJECT_FLAG_EXTENSIBLE |
  48371. DUK_HOBJECT_FLAG_ARRAY_PART |
  48372. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARGUMENTS),
  48373. DUK_BIDX_OBJECT_PROTOTYPE);
  48374. DUK_ASSERT(i_arg >= 0);
  48375. arg = duk_require_hobject(ctx, -1);
  48376. DUK_ASSERT(arg != NULL);
  48377. i_map = duk_push_object_helper(ctx,
  48378. DUK_HOBJECT_FLAG_EXTENSIBLE |
  48379. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  48380. -1); /* no prototype */
  48381. DUK_ASSERT(i_map >= 0);
  48382. i_mappednames = duk_push_object_helper(ctx,
  48383. DUK_HOBJECT_FLAG_EXTENSIBLE |
  48384. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  48385. -1); /* no prototype */
  48386. DUK_ASSERT(i_mappednames >= 0);
  48387. /* [... formals arguments map mappedNames] */
  48388. DUK_DDD(DUK_DDDPRINT("created arguments related objects: "
  48389. "arguments at index %ld -> %!O "
  48390. "map at index %ld -> %!O "
  48391. "mappednames at index %ld -> %!O",
  48392. (long) i_arg, (duk_heaphdr *) duk_get_hobject(ctx, i_arg),
  48393. (long) i_map, (duk_heaphdr *) duk_get_hobject(ctx, i_map),
  48394. (long) i_mappednames, (duk_heaphdr *) duk_get_hobject(ctx, i_mappednames)));
  48395. /*
  48396. * Init arguments properties, map, etc.
  48397. */
  48398. duk_push_int(ctx, num_stack_args);
  48399. duk_xdef_prop_stridx(ctx, i_arg, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_WC);
  48400. /*
  48401. * Init argument related properties
  48402. */
  48403. /* step 11 */
  48404. idx = num_stack_args - 1;
  48405. while (idx >= 0) {
  48406. DUK_DDD(DUK_DDDPRINT("arg idx %ld, argbase=%ld, argidx=%ld",
  48407. (long) idx, (long) i_argbase, (long) (i_argbase + idx)));
  48408. DUK_DDD(DUK_DDDPRINT("define arguments[%ld]=arg", (long) idx));
  48409. duk_dup(ctx, i_argbase + idx);
  48410. duk_xdef_prop_index_wec(ctx, i_arg, (duk_uarridx_t) idx);
  48411. DUK_DDD(DUK_DDDPRINT("defined arguments[%ld]=arg", (long) idx));
  48412. /* step 11.c is relevant only if non-strict (checked in 11.c.ii) */
  48413. if (!DUK_HOBJECT_HAS_STRICT(func) && idx < n_formals) {
  48414. DUK_ASSERT(formals != NULL);
  48415. DUK_DDD(DUK_DDDPRINT("strict function, index within formals (%ld < %ld)",
  48416. (long) idx, (long) n_formals));
  48417. duk_get_prop_index(ctx, i_formals, idx);
  48418. DUK_ASSERT(duk_is_string(ctx, -1));
  48419. duk_dup(ctx, -1); /* [... name name] */
  48420. if (!duk_has_prop(ctx, i_mappednames)) {
  48421. /* steps 11.c.ii.1 - 11.c.ii.4, but our internal book-keeping
  48422. * differs from the reference model
  48423. */
  48424. /* [... name] */
  48425. need_map = 1;
  48426. DUK_DDD(DUK_DDDPRINT("set mappednames[%s]=%ld",
  48427. (const char *) duk_get_string(ctx, -1),
  48428. (long) idx));
  48429. duk_dup(ctx, -1); /* name */
  48430. duk_push_uint(ctx, (duk_uint_t) idx); /* index */
  48431. duk_to_string(ctx, -1);
  48432. duk_xdef_prop_wec(ctx, i_mappednames); /* out of spec, must be configurable */
  48433. DUK_DDD(DUK_DDDPRINT("set map[%ld]=%s",
  48434. (long) idx,
  48435. duk_get_string(ctx, -1)));
  48436. duk_dup(ctx, -1); /* name */
  48437. duk_xdef_prop_index_wec(ctx, i_map, (duk_uarridx_t) idx); /* out of spec, must be configurable */
  48438. } else {
  48439. /* duk_has_prop() popped the second 'name' */
  48440. }
  48441. /* [... name] */
  48442. duk_pop(ctx); /* pop 'name' */
  48443. }
  48444. idx--;
  48445. }
  48446. DUK_DDD(DUK_DDDPRINT("actual arguments processed"));
  48447. /* step 12 */
  48448. if (need_map) {
  48449. DUK_DDD(DUK_DDDPRINT("adding 'map' and 'varenv' to arguments object"));
  48450. /* should never happen for a strict callee */
  48451. DUK_ASSERT(!DUK_HOBJECT_HAS_STRICT(func));
  48452. duk_dup(ctx, i_map);
  48453. duk_xdef_prop_stridx(ctx, i_arg, DUK_STRIDX_INT_MAP, DUK_PROPDESC_FLAGS_NONE); /* out of spec, don't care */
  48454. /* The variable environment for magic variable bindings needs to be
  48455. * given by the caller and recorded in the arguments object.
  48456. *
  48457. * See E5 Section 10.6, the creation of setters/getters.
  48458. *
  48459. * The variable environment also provides access to the callee, so
  48460. * an explicit (internal) callee property is not needed.
  48461. */
  48462. duk_push_hobject(ctx, varenv);
  48463. duk_xdef_prop_stridx(ctx, i_arg, DUK_STRIDX_INT_VARENV, DUK_PROPDESC_FLAGS_NONE); /* out of spec, don't care */
  48464. }
  48465. /* steps 13-14 */
  48466. if (DUK_HOBJECT_HAS_STRICT(func)) {
  48467. /*
  48468. * Note: callee/caller are throwers and are not deletable etc.
  48469. * They could be implemented as virtual properties, but currently
  48470. * there is no support for virtual properties which are accessors
  48471. * (only plain virtual properties). This would not be difficult
  48472. * to change in duk_hobject_props, but we can make the throwers
  48473. * normal, concrete properties just as easily.
  48474. *
  48475. * Note that the specification requires that the *same* thrower
  48476. * built-in object is used here! See E5 Section 10.6 main
  48477. * algoritm, step 14, and Section 13.2.3 which describes the
  48478. * thrower. See test case test-arguments-throwers.js.
  48479. */
  48480. DUK_DDD(DUK_DDDPRINT("strict function, setting caller/callee to throwers"));
  48481. duk_xdef_prop_stridx_thrower(ctx, i_arg, DUK_STRIDX_CALLER, DUK_PROPDESC_FLAGS_NONE);
  48482. duk_xdef_prop_stridx_thrower(ctx, i_arg, DUK_STRIDX_CALLEE, DUK_PROPDESC_FLAGS_NONE);
  48483. } else {
  48484. DUK_DDD(DUK_DDDPRINT("non-strict function, setting callee to actual value"));
  48485. duk_push_hobject(ctx, func);
  48486. duk_xdef_prop_stridx(ctx, i_arg, DUK_STRIDX_CALLEE, DUK_PROPDESC_FLAGS_WC);
  48487. }
  48488. /* set exotic behavior only after we're done */
  48489. if (need_map) {
  48490. /*
  48491. * Note: exotic behaviors are only enabled for arguments
  48492. * objects which have a parameter map (see E5 Section 10.6
  48493. * main algorithm, step 12).
  48494. *
  48495. * In particular, a non-strict arguments object with no
  48496. * mapped formals does *NOT* get exotic behavior, even
  48497. * for e.g. "caller" property. This seems counterintuitive
  48498. * but seems to be the case.
  48499. */
  48500. /* cannot be strict (never mapped variables) */
  48501. DUK_ASSERT(!DUK_HOBJECT_HAS_STRICT(func));
  48502. DUK_DDD(DUK_DDDPRINT("enabling exotic behavior for arguments object"));
  48503. DUK_HOBJECT_SET_EXOTIC_ARGUMENTS(arg);
  48504. } else {
  48505. DUK_DDD(DUK_DDDPRINT("not enabling exotic behavior for arguments object"));
  48506. }
  48507. /* nice log */
  48508. DUK_DDD(DUK_DDDPRINT("final arguments related objects: "
  48509. "arguments at index %ld -> %!O "
  48510. "map at index %ld -> %!O "
  48511. "mappednames at index %ld -> %!O",
  48512. (long) i_arg, (duk_heaphdr *) duk_get_hobject(ctx, i_arg),
  48513. (long) i_map, (duk_heaphdr *) duk_get_hobject(ctx, i_map),
  48514. (long) i_mappednames, (duk_heaphdr *) duk_get_hobject(ctx, i_mappednames)));
  48515. /* [args(n) [crud] formals arguments map mappednames] -> [args [crud] arguments] */
  48516. duk_pop_2(ctx);
  48517. duk_remove(ctx, -2);
  48518. }
  48519. /* Helper for creating the arguments object and adding it to the env record
  48520. * on top of the value stack. This helper has a very strict dependency on
  48521. * the shape of the input stack.
  48522. */
  48523. DUK_LOCAL
  48524. void duk__handle_createargs_for_call(duk_hthread *thr,
  48525. duk_hobject *func,
  48526. duk_hobject *env,
  48527. duk_idx_t num_stack_args) {
  48528. duk_context *ctx = (duk_context *) thr;
  48529. DUK_DDD(DUK_DDDPRINT("creating arguments object for function call"));
  48530. DUK_ASSERT(thr != NULL);
  48531. DUK_ASSERT(func != NULL);
  48532. DUK_ASSERT(env != NULL);
  48533. DUK_ASSERT(DUK_HOBJECT_HAS_CREATEARGS(func));
  48534. DUK_ASSERT(duk_get_top(ctx) >= num_stack_args + 1);
  48535. /* [... arg1 ... argN envobj] */
  48536. duk__create_arguments_object(thr,
  48537. func,
  48538. env,
  48539. duk_get_top(ctx) - num_stack_args - 1, /* idx_argbase */
  48540. num_stack_args);
  48541. /* [... arg1 ... argN envobj argobj] */
  48542. duk_xdef_prop_stridx(ctx,
  48543. -2,
  48544. DUK_STRIDX_LC_ARGUMENTS,
  48545. DUK_HOBJECT_HAS_STRICT(func) ? DUK_PROPDESC_FLAGS_E : /* strict: non-deletable, non-writable */
  48546. DUK_PROPDESC_FLAGS_WE); /* non-strict: non-deletable, writable */
  48547. /* [... arg1 ... argN envobj] */
  48548. }
  48549. /*
  48550. * Helper for handling a "bound function" chain when a call is being made.
  48551. *
  48552. * Follows the bound function chain until a non-bound function is found.
  48553. * Prepends the bound arguments to the value stack (at idx_func + 2),
  48554. * updating 'num_stack_args' in the process. The 'this' binding is also
  48555. * updated if necessary (at idx_func + 1). Note that for constructor calls
  48556. * the 'this' binding is never updated by [[BoundThis]].
  48557. *
  48558. * XXX: bound function chains could be collapsed at bound function creation
  48559. * time so that each bound function would point directly to a non-bound
  48560. * function. This would make call time handling much easier.
  48561. */
  48562. DUK_LOCAL
  48563. void duk__handle_bound_chain_for_call(duk_hthread *thr,
  48564. duk_idx_t idx_func,
  48565. duk_idx_t *p_num_stack_args, /* may be changed by call */
  48566. duk_bool_t is_constructor_call) {
  48567. duk_context *ctx = (duk_context *) thr;
  48568. duk_idx_t num_stack_args;
  48569. duk_tval *tv_func;
  48570. duk_hobject *func;
  48571. duk_uint_t sanity;
  48572. DUK_ASSERT(thr != NULL);
  48573. DUK_ASSERT(p_num_stack_args != NULL);
  48574. /* On entry, item at idx_func is a bound, non-lightweight function,
  48575. * but we don't rely on that below.
  48576. */
  48577. num_stack_args = *p_num_stack_args;
  48578. sanity = DUK_HOBJECT_BOUND_CHAIN_SANITY;
  48579. do {
  48580. duk_idx_t i, len;
  48581. tv_func = duk_require_tval(ctx, idx_func);
  48582. DUK_ASSERT(tv_func != NULL);
  48583. if (DUK_TVAL_IS_LIGHTFUNC(tv_func)) {
  48584. /* Lightweight function: never bound, so terminate. */
  48585. break;
  48586. } else if (DUK_TVAL_IS_OBJECT(tv_func)) {
  48587. func = DUK_TVAL_GET_OBJECT(tv_func);
  48588. if (!DUK_HOBJECT_HAS_BOUND(func)) {
  48589. /* Normal non-bound function. */
  48590. break;
  48591. }
  48592. } else {
  48593. /* Function.prototype.bind() should never let this happen,
  48594. * ugly error message is enough.
  48595. */
  48596. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR);
  48597. }
  48598. DUK_ASSERT(DUK_TVAL_GET_OBJECT(tv_func) != NULL);
  48599. /* XXX: this could be more compact by accessing the internal properties
  48600. * directly as own properties (they cannot be inherited, and are not
  48601. * externally visible).
  48602. */
  48603. DUK_DDD(DUK_DDDPRINT("bound function encountered, ptr=%p, num_stack_args=%ld: %!T",
  48604. (void *) DUK_TVAL_GET_OBJECT(tv_func), (long) num_stack_args, tv_func));
  48605. /* [ ... func this arg1 ... argN ] */
  48606. if (is_constructor_call) {
  48607. /* See: tests/ecmascript/test-spec-bound-constructor.js */
  48608. DUK_DDD(DUK_DDDPRINT("constructor call: don't update this binding"));
  48609. } else {
  48610. duk_get_prop_stridx(ctx, idx_func, DUK_STRIDX_INT_THIS);
  48611. duk_replace(ctx, idx_func + 1); /* idx_this = idx_func + 1 */
  48612. }
  48613. /* [ ... func this arg1 ... argN ] */
  48614. /* XXX: duk_get_length? */
  48615. duk_get_prop_stridx(ctx, idx_func, DUK_STRIDX_INT_ARGS); /* -> [ ... func this arg1 ... argN _Args ] */
  48616. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LENGTH); /* -> [ ... func this arg1 ... argN _Args length ] */
  48617. len = (duk_idx_t) duk_require_int(ctx, -1);
  48618. duk_pop(ctx);
  48619. for (i = 0; i < len; i++) {
  48620. /* XXX: very slow - better to bulk allocate a gap, and copy
  48621. * from args_array directly (we know it has a compact array
  48622. * part, etc).
  48623. */
  48624. /* [ ... func this <some bound args> arg1 ... argN _Args ] */
  48625. duk_get_prop_index(ctx, -1, i);
  48626. duk_insert(ctx, idx_func + 2 + i); /* idx_args = idx_func + 2 */
  48627. }
  48628. num_stack_args += len; /* must be updated to work properly (e.g. creation of 'arguments') */
  48629. duk_pop(ctx);
  48630. /* [ ... func this <bound args> arg1 ... argN ] */
  48631. duk_get_prop_stridx(ctx, idx_func, DUK_STRIDX_INT_TARGET);
  48632. duk_replace(ctx, idx_func); /* replace in stack */
  48633. DUK_DDD(DUK_DDDPRINT("bound function handled, num_stack_args=%ld, idx_func=%ld, curr func=%!T",
  48634. (long) num_stack_args, (long) idx_func, duk_get_tval(ctx, idx_func)));
  48635. } while (--sanity > 0);
  48636. if (sanity == 0) {
  48637. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_BOUND_CHAIN_LIMIT);
  48638. }
  48639. DUK_DDD(DUK_DDDPRINT("final non-bound function is: %!T", duk_get_tval(ctx, idx_func)));
  48640. #ifdef DUK_USE_ASSERTIONS
  48641. tv_func = duk_require_tval(ctx, idx_func);
  48642. DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv_func) || DUK_TVAL_IS_OBJECT(tv_func));
  48643. if (DUK_TVAL_IS_OBJECT(tv_func)) {
  48644. func = DUK_TVAL_GET_OBJECT(tv_func);
  48645. DUK_ASSERT(func != NULL);
  48646. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func));
  48647. DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(func) ||
  48648. DUK_HOBJECT_HAS_NATIVEFUNCTION(func));
  48649. }
  48650. #endif
  48651. /* write back */
  48652. *p_num_stack_args = num_stack_args;
  48653. }
  48654. /*
  48655. * Helper for setting up var_env and lex_env of an activation,
  48656. * assuming it does NOT have the DUK_HOBJECT_FLAG_NEWENV flag.
  48657. */
  48658. DUK_LOCAL
  48659. void duk__handle_oldenv_for_call(duk_hthread *thr,
  48660. duk_hobject *func,
  48661. duk_activation *act) {
  48662. duk_tval *tv;
  48663. DUK_ASSERT(thr != NULL);
  48664. DUK_ASSERT(func != NULL);
  48665. DUK_ASSERT(act != NULL);
  48666. DUK_ASSERT(!DUK_HOBJECT_HAS_NEWENV(func));
  48667. DUK_ASSERT(!DUK_HOBJECT_HAS_CREATEARGS(func));
  48668. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_INT_LEXENV(thr));
  48669. if (tv) {
  48670. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  48671. DUK_ASSERT(DUK_HOBJECT_IS_ENV(DUK_TVAL_GET_OBJECT(tv)));
  48672. act->lex_env = DUK_TVAL_GET_OBJECT(tv);
  48673. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_INT_VARENV(thr));
  48674. if (tv) {
  48675. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  48676. DUK_ASSERT(DUK_HOBJECT_IS_ENV(DUK_TVAL_GET_OBJECT(tv)));
  48677. act->var_env = DUK_TVAL_GET_OBJECT(tv);
  48678. } else {
  48679. act->var_env = act->lex_env;
  48680. }
  48681. } else {
  48682. act->lex_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  48683. act->var_env = act->lex_env;
  48684. }
  48685. DUK_HOBJECT_INCREF_ALLOWNULL(thr, act->lex_env);
  48686. DUK_HOBJECT_INCREF_ALLOWNULL(thr, act->var_env);
  48687. }
  48688. /*
  48689. * Helper for updating callee 'caller' property.
  48690. */
  48691. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  48692. DUK_LOCAL void duk__update_func_caller_prop(duk_hthread *thr, duk_hobject *func) {
  48693. duk_tval *tv_caller;
  48694. duk_hobject *h_tmp;
  48695. duk_activation *act_callee;
  48696. duk_activation *act_caller;
  48697. DUK_ASSERT(thr != NULL);
  48698. DUK_ASSERT(func != NULL);
  48699. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func)); /* bound chain resolved */
  48700. DUK_ASSERT(thr->callstack_top >= 1);
  48701. if (DUK_HOBJECT_HAS_STRICT(func)) {
  48702. /* Strict functions don't get their 'caller' updated. */
  48703. return;
  48704. }
  48705. act_callee = thr->callstack + thr->callstack_top - 1;
  48706. act_caller = (thr->callstack_top >= 2 ? act_callee - 1 : NULL);
  48707. /* Backup 'caller' property and update its value. */
  48708. tv_caller = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_CALLER(thr));
  48709. if (tv_caller) {
  48710. /* If caller is global/eval code, 'caller' should be set to
  48711. * 'null'.
  48712. *
  48713. * XXX: there is no exotic flag to infer this correctly now.
  48714. * The NEWENV flag is used now which works as intended for
  48715. * everything (global code, non-strict eval code, and functions)
  48716. * except strict eval code. Bound functions are never an issue
  48717. * because 'func' has been resolved to a non-bound function.
  48718. */
  48719. if (act_caller) {
  48720. /* act_caller->func may be NULL in some finalization cases,
  48721. * just treat like we don't know the caller.
  48722. */
  48723. if (act_caller->func && !DUK_HOBJECT_HAS_NEWENV(act_caller->func)) {
  48724. /* Setting to NULL causes 'caller' to be set to
  48725. * 'null' as desired.
  48726. */
  48727. act_caller = NULL;
  48728. }
  48729. }
  48730. if (DUK_TVAL_IS_OBJECT(tv_caller)) {
  48731. h_tmp = DUK_TVAL_GET_OBJECT(tv_caller);
  48732. DUK_ASSERT(h_tmp != NULL);
  48733. act_callee->prev_caller = h_tmp;
  48734. /* Previous value doesn't need refcount changes because its ownership
  48735. * is transferred to prev_caller.
  48736. */
  48737. if (act_caller) {
  48738. DUK_ASSERT(act_caller->func != NULL);
  48739. DUK_TVAL_SET_OBJECT(tv_caller, act_caller->func);
  48740. DUK_TVAL_INCREF(thr, tv_caller);
  48741. } else {
  48742. DUK_TVAL_SET_NULL(tv_caller); /* no incref */
  48743. }
  48744. } else {
  48745. /* 'caller' must only take on 'null' or function value */
  48746. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_caller));
  48747. DUK_ASSERT(act_callee->prev_caller == NULL);
  48748. if (act_caller && act_caller->func) {
  48749. /* Tolerate act_caller->func == NULL which happens in
  48750. * some finalization cases; treat like unknown caller.
  48751. */
  48752. DUK_TVAL_SET_OBJECT(tv_caller, act_caller->func);
  48753. DUK_TVAL_INCREF(thr, tv_caller);
  48754. } else {
  48755. DUK_TVAL_SET_NULL(tv_caller); /* no incref */
  48756. }
  48757. }
  48758. }
  48759. }
  48760. #endif /* DUK_USE_NONSTD_FUNC_CALLER_PROPERTY */
  48761. /*
  48762. * Determine the effective 'this' binding and coerce the current value
  48763. * on the valstack to the effective one (in-place, at idx_this).
  48764. *
  48765. * The current this value in the valstack (at idx_this) represents either:
  48766. * - the caller's requested 'this' binding; or
  48767. * - a 'this' binding accumulated from the bound function chain
  48768. *
  48769. * The final 'this' binding for the target function may still be
  48770. * different, and is determined as described in E5 Section 10.4.3.
  48771. *
  48772. * For global and eval code (E5 Sections 10.4.1 and 10.4.2), we assume
  48773. * that the caller has provided the correct 'this' binding explicitly
  48774. * when calling, i.e.:
  48775. *
  48776. * - global code: this=global object
  48777. * - direct eval: this=copy from eval() caller's this binding
  48778. * - other eval: this=global object
  48779. *
  48780. * Note: this function may cause a recursive function call with arbitrary
  48781. * side effects, because ToObject() may be called.
  48782. */
  48783. DUK_LOCAL
  48784. void duk__coerce_effective_this_binding(duk_hthread *thr,
  48785. duk_hobject *func,
  48786. duk_idx_t idx_this) {
  48787. duk_context *ctx = (duk_context *) thr;
  48788. duk_tval *tv_this;
  48789. duk_hobject *obj_global;
  48790. if (func == NULL || DUK_HOBJECT_HAS_STRICT(func)) {
  48791. /* Lightfuncs are always considered strict. */
  48792. DUK_DDD(DUK_DDDPRINT("this binding: strict -> use directly"));
  48793. return;
  48794. }
  48795. /* XXX: byte offset */
  48796. tv_this = thr->valstack_bottom + idx_this;
  48797. switch (DUK_TVAL_GET_TAG(tv_this)) {
  48798. case DUK_TAG_OBJECT:
  48799. case DUK_TAG_LIGHTFUNC: /* lightfuncs are treated like objects and not coerced */
  48800. DUK_DDD(DUK_DDDPRINT("this binding: non-strict, object -> use directly"));
  48801. break;
  48802. case DUK_TAG_UNDEFINED:
  48803. case DUK_TAG_NULL:
  48804. DUK_DDD(DUK_DDDPRINT("this binding: non-strict, undefined/null -> use global object"));
  48805. obj_global = thr->builtins[DUK_BIDX_GLOBAL];
  48806. /* XXX: avoid this check somehow */
  48807. if (DUK_LIKELY(obj_global != NULL)) {
  48808. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_this)); /* no need to decref previous value */
  48809. DUK_TVAL_SET_OBJECT(tv_this, obj_global);
  48810. DUK_HOBJECT_INCREF(thr, obj_global);
  48811. } else {
  48812. /* This may only happen if built-ins are being "torn down".
  48813. * This behavior is out of specification scope.
  48814. */
  48815. DUK_D(DUK_DPRINT("this binding: wanted to use global object, but it is NULL -> using undefined instead"));
  48816. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_this)); /* no need to decref previous value */
  48817. DUK_TVAL_SET_UNDEFINED(tv_this); /* nothing to incref */
  48818. }
  48819. break;
  48820. default:
  48821. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv_this));
  48822. DUK_DDD(DUK_DDDPRINT("this binding: non-strict, not object/undefined/null -> use ToObject(value)"));
  48823. duk_to_object(ctx, idx_this); /* may have side effects */
  48824. break;
  48825. }
  48826. }
  48827. /*
  48828. * Shared helper for non-bound func lookup.
  48829. *
  48830. * Returns duk_hobject * to the final non-bound function (NULL for lightfunc).
  48831. */
  48832. DUK_LOCAL
  48833. duk_hobject *duk__nonbound_func_lookup(duk_context *ctx,
  48834. duk_idx_t idx_func,
  48835. duk_idx_t *out_num_stack_args,
  48836. duk_tval **out_tv_func,
  48837. duk_small_uint_t call_flags) {
  48838. duk_hthread *thr = (duk_hthread *) ctx;
  48839. duk_tval *tv_func;
  48840. duk_hobject *func;
  48841. for (;;) {
  48842. /* Use loop to minimize code size of relookup after bound function case */
  48843. tv_func = duk_get_tval(ctx, idx_func);
  48844. DUK_ASSERT(tv_func != NULL);
  48845. if (DUK_TVAL_IS_OBJECT(tv_func)) {
  48846. func = DUK_TVAL_GET_OBJECT(tv_func);
  48847. if (!DUK_HOBJECT_IS_CALLABLE(func)) {
  48848. goto not_callable_error;
  48849. }
  48850. if (DUK_HOBJECT_HAS_BOUND(func)) {
  48851. duk__handle_bound_chain_for_call(thr, idx_func, out_num_stack_args, call_flags & DUK_CALL_FLAG_CONSTRUCTOR_CALL);
  48852. /* The final object may be a normal function or a lightfunc.
  48853. * We need to re-lookup tv_func because it may have changed
  48854. * (also value stack may have been resized). Loop again to
  48855. * do that; we're guaranteed not to come here again.
  48856. */
  48857. DUK_ASSERT(DUK_TVAL_IS_OBJECT(duk_require_tval(ctx, idx_func)) ||
  48858. DUK_TVAL_IS_LIGHTFUNC(duk_require_tval(ctx, idx_func)));
  48859. continue;
  48860. }
  48861. } else if (DUK_TVAL_IS_LIGHTFUNC(tv_func)) {
  48862. func = NULL;
  48863. } else {
  48864. goto not_callable_error;
  48865. }
  48866. break;
  48867. }
  48868. DUK_ASSERT((DUK_TVAL_IS_OBJECT(tv_func) && DUK_HOBJECT_IS_CALLABLE(DUK_TVAL_GET_OBJECT(tv_func))) ||
  48869. DUK_TVAL_IS_LIGHTFUNC(tv_func));
  48870. DUK_ASSERT(func == NULL || !DUK_HOBJECT_HAS_BOUND(func));
  48871. DUK_ASSERT(func == NULL || (DUK_HOBJECT_IS_COMPILEDFUNCTION(func) ||
  48872. DUK_HOBJECT_IS_NATIVEFUNCTION(func)));
  48873. *out_tv_func = tv_func;
  48874. return func;
  48875. not_callable_error:
  48876. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_CALLABLE);
  48877. DUK_UNREACHABLE();
  48878. return NULL; /* never executed */
  48879. }
  48880. /*
  48881. * Value stack resize and stack top adjustment helper
  48882. *
  48883. * XXX: This should all be merged to duk_valstack_resize_raw().
  48884. */
  48885. DUK_LOCAL
  48886. void duk__adjust_valstack_and_top(duk_hthread *thr, duk_idx_t num_stack_args, duk_idx_t idx_args, duk_idx_t nregs, duk_idx_t nargs, duk_hobject *func) {
  48887. duk_context *ctx = (duk_context *) thr;
  48888. duk_size_t vs_min_size;
  48889. duk_bool_t adjusted_top = 0;
  48890. vs_min_size = (thr->valstack_bottom - thr->valstack) + /* bottom of current func */
  48891. idx_args; /* bottom of new func */
  48892. if (nregs >= 0) {
  48893. DUK_ASSERT(nargs >= 0);
  48894. DUK_ASSERT(nregs >= nargs);
  48895. vs_min_size += nregs;
  48896. } else {
  48897. /* 'func' wants stack "as is" */
  48898. vs_min_size += num_stack_args; /* num entries of new func at entry */
  48899. }
  48900. if (func == NULL || DUK_HOBJECT_IS_NATIVEFUNCTION(func)) {
  48901. vs_min_size += DUK_VALSTACK_API_ENTRY_MINIMUM; /* Duktape/C API guaranteed entries (on top of args) */
  48902. }
  48903. vs_min_size += DUK_VALSTACK_INTERNAL_EXTRA; /* + spare */
  48904. /* XXX: Awkward fix for GH-107: we can't resize the value stack to
  48905. * a size smaller than the current top, so the order of the resize
  48906. * and adjusting the stack top depends on the current vs. final size
  48907. * of the value stack. Ideally duk_valstack_resize_raw() would have
  48908. * a combined algorithm to avoid this.
  48909. */
  48910. if (vs_min_size < (duk_size_t) (thr->valstack_top - thr->valstack)) {
  48911. DUK_DDD(DUK_DDDPRINT(("final size smaller, set top before resize")));
  48912. DUK_ASSERT(nregs >= 0); /* can't happen when keeping current stack size */
  48913. duk_set_top(ctx, idx_args + nargs); /* clamp anything above nargs */
  48914. duk_set_top(ctx, idx_args + nregs); /* extend with undefined */
  48915. adjusted_top = 1;
  48916. }
  48917. (void) duk_valstack_resize_raw((duk_context *) thr,
  48918. vs_min_size,
  48919. DUK_VSRESIZE_FLAG_SHRINK | /* flags */
  48920. 0 /* no compact */ |
  48921. DUK_VSRESIZE_FLAG_THROW);
  48922. if (!adjusted_top) {
  48923. if (nregs >= 0) {
  48924. DUK_ASSERT(nregs >= nargs);
  48925. duk_set_top(ctx, idx_args + nargs); /* clamp anything above nargs */
  48926. duk_set_top(ctx, idx_args + nregs); /* extend with undefined */
  48927. }
  48928. }
  48929. }
  48930. /*
  48931. * Helper for making various kinds of calls.
  48932. *
  48933. * Call flags:
  48934. *
  48935. * DUK_CALL_FLAG_PROTECTED <--> protected call
  48936. * DUK_CALL_FLAG_IGNORE_RECLIMIT <--> ignore C recursion limit,
  48937. * for errhandler calls
  48938. * DUK_CALL_FLAG_CONSTRUCTOR_CALL <--> for 'new Foo()' calls
  48939. *
  48940. * Input stack (thr):
  48941. *
  48942. * [ func this arg1 ... argN ]
  48943. *
  48944. * Output stack (thr):
  48945. *
  48946. * [ retval ] (DUK_EXEC_SUCCESS)
  48947. * [ errobj ] (DUK_EXEC_ERROR (normal error), protected call)
  48948. *
  48949. * Even when executing a protected call an error may be thrown in rare cases.
  48950. * For instance, if we run out of memory when setting up the return stack
  48951. * after a caught error, the out of memory is propagated to the caller.
  48952. * Similarly, API errors (such as invalid input stack shape and invalid
  48953. * indices) cause an error to propagate out of this function. If there is
  48954. * no catchpoint for this error, the fatal error handler is called.
  48955. *
  48956. * See 'execution.rst'.
  48957. *
  48958. * The allowed thread states for making a call are:
  48959. * - thr matches heap->curr_thread, and thr is already RUNNING
  48960. * - thr does not match heap->curr_thread (may be NULL or other),
  48961. * and thr is INACTIVE (in this case, a setjmp() catchpoint is
  48962. * always used for thread book-keeping to work properly)
  48963. *
  48964. * Like elsewhere, gotos are used to keep indent level minimal and
  48965. * avoiding a dozen helpers with awkward plumbing.
  48966. *
  48967. * Note: setjmp() and local variables have a nasty interaction,
  48968. * see execution.rst; non-volatile locals modified after setjmp()
  48969. * call are not guaranteed to keep their value.
  48970. */
  48971. DUK_INTERNAL
  48972. duk_int_t duk_handle_call(duk_hthread *thr,
  48973. duk_idx_t num_stack_args,
  48974. duk_small_uint_t call_flags) {
  48975. duk_context *ctx = (duk_context *) thr;
  48976. duk_size_t entry_valstack_bottom_index;
  48977. duk_size_t entry_valstack_end;
  48978. duk_size_t entry_callstack_top;
  48979. duk_size_t entry_catchstack_top;
  48980. duk_int_t entry_call_recursion_depth;
  48981. duk_hthread *entry_curr_thread;
  48982. duk_uint_fast8_t entry_thread_state;
  48983. duk_instr_t **entry_ptr_curr_pc;
  48984. volatile duk_bool_t need_setjmp;
  48985. duk_jmpbuf * volatile old_jmpbuf_ptr = NULL; /* ptr is volatile (not the target) */
  48986. duk_idx_t idx_func; /* valstack index of 'func' and retval (relative to entry valstack_bottom) */
  48987. duk_idx_t idx_args; /* valstack index of start of args (arg1) (relative to entry valstack_bottom) */
  48988. duk_idx_t nargs; /* # argument registers target function wants (< 0 => "as is") */
  48989. duk_idx_t nregs; /* # total registers target function wants on entry (< 0 => "as is") */
  48990. duk_hobject *func; /* 'func' on stack (borrowed reference) */
  48991. duk_tval *tv_func; /* duk_tval ptr for 'func' on stack (borrowed reference) or tv_func_copy */
  48992. duk_tval tv_func_copy; /* to avoid relookups */
  48993. duk_activation *act;
  48994. duk_hobject *env;
  48995. duk_jmpbuf our_jmpbuf;
  48996. duk_int_t retval = DUK_EXEC_ERROR;
  48997. duk_ret_t rc;
  48998. DUK_ASSERT(thr != NULL);
  48999. DUK_ASSERT(ctx != NULL);
  49000. DUK_ASSERT(num_stack_args >= 0);
  49001. /* XXX: currently NULL allocations are not supported; remove if later allowed */
  49002. DUK_ASSERT(thr->valstack != NULL);
  49003. DUK_ASSERT(thr->callstack != NULL);
  49004. DUK_ASSERT(thr->catchstack != NULL);
  49005. /*
  49006. * Preliminaries, required by setjmp() handler.
  49007. *
  49008. * Must be careful not to throw an unintended error here.
  49009. *
  49010. * Note: careful with indices like '-x'; if 'x' is zero, it
  49011. * refers to valstack_bottom.
  49012. */
  49013. entry_valstack_bottom_index = (duk_size_t) (thr->valstack_bottom - thr->valstack);
  49014. #if defined(DUK_USE_PREFER_SIZE)
  49015. entry_valstack_end = (duk_size_t) (thr->valstack_end - thr->valstack);
  49016. #else
  49017. DUK_ASSERT((duk_size_t) (thr->valstack_end - thr->valstack) == thr->valstack_size);
  49018. entry_valstack_end = thr->valstack_size;
  49019. #endif
  49020. entry_callstack_top = thr->callstack_top;
  49021. entry_catchstack_top = thr->catchstack_top;
  49022. entry_call_recursion_depth = thr->heap->call_recursion_depth;
  49023. entry_curr_thread = thr->heap->curr_thread; /* Note: may be NULL if first call */
  49024. entry_thread_state = thr->state;
  49025. entry_ptr_curr_pc = thr->ptr_curr_pc; /* may be NULL */
  49026. idx_func = duk_normalize_index(ctx, -num_stack_args - 2); /* idx_func must be valid, note: non-throwing! */
  49027. idx_args = idx_func + 2; /* idx_args is not necessarily valid if num_stack_args == 0 (idx_args then equals top) */
  49028. /* Need a setjmp() catchpoint if a protected call OR if we need to
  49029. * do mandatory cleanup.
  49030. */
  49031. need_setjmp = ((call_flags & DUK_CALL_FLAG_PROTECTED) != 0) || (thr->heap->curr_thread != thr);
  49032. DUK_DD(DUK_DDPRINT("duk_handle_call: thr=%p, num_stack_args=%ld, "
  49033. "call_flags=0x%08lx (protected=%ld, ignorerec=%ld, constructor=%ld), need_setjmp=%ld, "
  49034. "valstack_top=%ld, idx_func=%ld, idx_args=%ld, rec_depth=%ld/%ld, "
  49035. "entry_valstack_bottom_index=%ld, entry_callstack_top=%ld, entry_catchstack_top=%ld, "
  49036. "entry_call_recursion_depth=%ld, entry_curr_thread=%p, entry_thread_state=%ld",
  49037. (void *) thr,
  49038. (long) num_stack_args,
  49039. (unsigned long) call_flags,
  49040. (long) ((call_flags & DUK_CALL_FLAG_PROTECTED) != 0 ? 1 : 0),
  49041. (long) ((call_flags & DUK_CALL_FLAG_IGNORE_RECLIMIT) != 0 ? 1 : 0),
  49042. (long) ((call_flags & DUK_CALL_FLAG_CONSTRUCTOR_CALL) != 0 ? 1 : 0),
  49043. (long) need_setjmp,
  49044. (long) duk_get_top(ctx),
  49045. (long) idx_func,
  49046. (long) idx_args,
  49047. (long) thr->heap->call_recursion_depth,
  49048. (long) thr->heap->call_recursion_limit,
  49049. (long) entry_valstack_bottom_index,
  49050. (long) entry_callstack_top,
  49051. (long) entry_catchstack_top,
  49052. (long) entry_call_recursion_depth,
  49053. (void *) entry_curr_thread,
  49054. (long) entry_thread_state));
  49055. /* If thr->ptr_curr_pc is set, sync curr_pc to act->pc. Then NULL
  49056. * thr->ptr_curr_pc so that it's not accidentally used with an incorrect
  49057. * activation when side effects occur.
  49058. */
  49059. duk_hthread_sync_and_null_currpc(thr);
  49060. /* XXX: Multiple tv_func lookups are now avoided by making a local
  49061. * copy of tv_func. Another approach would be to compute an offset
  49062. * for tv_func from valstack bottom and recomputing the tv_func
  49063. * pointer quickly as valstack + offset instead of calling duk_get_tval().
  49064. */
  49065. if (idx_func < 0 || idx_args < 0) {
  49066. /*
  49067. * Since stack indices are not reliable, we can't do anything useful
  49068. * here. Invoke the existing setjmp catcher, or if it doesn't exist,
  49069. * call the fatal error handler.
  49070. */
  49071. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  49072. }
  49073. /*
  49074. * Setup a setjmp() catchpoint first because even the call setup
  49075. * may fail.
  49076. */
  49077. if (!need_setjmp) {
  49078. DUK_DDD(DUK_DDDPRINT("don't need a setjmp catchpoint"));
  49079. goto handle_call;
  49080. }
  49081. old_jmpbuf_ptr = thr->heap->lj.jmpbuf_ptr;
  49082. thr->heap->lj.jmpbuf_ptr = &our_jmpbuf;
  49083. if (DUK_SETJMP(thr->heap->lj.jmpbuf_ptr->jb) == 0) {
  49084. DUK_DDD(DUK_DDDPRINT("setjmp catchpoint setup complete"));
  49085. goto handle_call;
  49086. }
  49087. /*
  49088. * Error during setup, call, or postprocessing of the call.
  49089. * The error value is in heap->lj.value1.
  49090. *
  49091. * Note: any local variables accessed here must have their value
  49092. * assigned *before* the setjmp() call, OR they must be declared
  49093. * volatile. Otherwise their value is not guaranteed to be correct.
  49094. *
  49095. * The following are such variables:
  49096. * - duk_handle_call() parameters
  49097. * - entry_*
  49098. * - idx_func
  49099. * - idx_args
  49100. *
  49101. * The very first thing we do is restore the previous setjmp catcher.
  49102. * This means that any error in error handling will propagate outwards
  49103. * instead of causing a setjmp() re-entry above. The *only* actual
  49104. * errors that should happen here are allocation errors.
  49105. */
  49106. DUK_DDD(DUK_DDDPRINT("error caught during protected duk_handle_call(): %!T",
  49107. (duk_tval *) &thr->heap->lj.value1));
  49108. DUK_ASSERT(thr->heap->lj.type == DUK_LJ_TYPE_THROW);
  49109. DUK_ASSERT(thr->callstack_top >= entry_callstack_top);
  49110. DUK_ASSERT(thr->catchstack_top >= entry_catchstack_top);
  49111. /* We don't need to sync back thr->curr_pc here because the
  49112. * bytecode executor always has a setjmp catchpoint which
  49113. * does that before errors propagate to here.
  49114. */
  49115. /*
  49116. * Restore previous setjmp catchpoint
  49117. */
  49118. /* Note: either pointer may be NULL (at entry), so don't assert */
  49119. DUK_DDD(DUK_DDDPRINT("restore jmpbuf_ptr: %p -> %p",
  49120. (void *) (thr && thr->heap ? thr->heap->lj.jmpbuf_ptr : NULL),
  49121. (void *) old_jmpbuf_ptr));
  49122. thr->heap->lj.jmpbuf_ptr = old_jmpbuf_ptr;
  49123. if (!(call_flags & DUK_CALL_FLAG_PROTECTED)) {
  49124. /*
  49125. * Caller did not request a protected call but a setjmp
  49126. * catchpoint was set up to allow cleanup. So, clean up
  49127. * and rethrow.
  49128. *
  49129. * We must restore curr_thread here to ensure that its
  49130. * current value doesn't end up pointing to a thread object
  49131. * which has been freed. This is now a problem because some
  49132. * call sites (namely duk_safe_call()) *first* unwind stacks
  49133. * and only then deal with curr_thread. If those call sites
  49134. * were fixed, this wouldn't matter here.
  49135. *
  49136. * Note: this case happens e.g. when heap->curr_thread is
  49137. * NULL on entry.
  49138. */
  49139. DUK_DDD(DUK_DDDPRINT("call is not protected -> clean up and rethrow"));
  49140. /* Restore entry thread executor curr_pc stack frame pointer. */
  49141. thr->ptr_curr_pc = entry_ptr_curr_pc;
  49142. DUK_HEAP_SWITCH_THREAD(thr->heap, entry_curr_thread); /* may be NULL */
  49143. thr->state = entry_thread_state;
  49144. DUK_ASSERT((thr->state == DUK_HTHREAD_STATE_INACTIVE && thr->heap->curr_thread == NULL) || /* first call */
  49145. (thr->state == DUK_HTHREAD_STATE_INACTIVE && thr->heap->curr_thread != NULL) || /* other call */
  49146. (thr->state == DUK_HTHREAD_STATE_RUNNING && thr->heap->curr_thread == thr)); /* current thread */
  49147. /* XXX: should setjmp catcher be responsible for this instead? */
  49148. thr->heap->call_recursion_depth = entry_call_recursion_depth;
  49149. duk_err_longjmp(thr);
  49150. DUK_UNREACHABLE();
  49151. }
  49152. duk_hthread_catchstack_unwind(thr, entry_catchstack_top);
  49153. duk_hthread_callstack_unwind(thr, entry_callstack_top);
  49154. thr->valstack_bottom = thr->valstack + entry_valstack_bottom_index;
  49155. /* [ ... func this (crud) errobj ] */
  49156. /* XXX: is there space? better implementation: write directly over
  49157. * 'func' slot to avoid valstack grow issues.
  49158. */
  49159. duk_push_tval(ctx, &thr->heap->lj.value1);
  49160. /* [ ... func this (crud) errobj ] */
  49161. duk_replace(ctx, idx_func);
  49162. duk_set_top(ctx, idx_func + 1);
  49163. /* [ ... errobj ] */
  49164. /* Ensure there is internal valstack spare before we exit; this may
  49165. * throw an alloc error. The same guaranteed size must be available
  49166. * as before the call. This is not optimal now: we store the valstack
  49167. * allocated size during entry; this value may be higher than the
  49168. * minimal guarantee for an application.
  49169. */
  49170. (void) duk_valstack_resize_raw((duk_context *) thr,
  49171. entry_valstack_end, /* same as during entry */
  49172. DUK_VSRESIZE_FLAG_SHRINK | /* flags */
  49173. DUK_VSRESIZE_FLAG_COMPACT |
  49174. DUK_VSRESIZE_FLAG_THROW);
  49175. /* Note: currently a second setjmp restoration is done at the target;
  49176. * this is OK, but could be refactored away.
  49177. */
  49178. retval = DUK_EXEC_ERROR;
  49179. goto shrink_and_finished;
  49180. handle_call:
  49181. /*
  49182. * Thread state check and book-keeping.
  49183. */
  49184. if (thr == thr->heap->curr_thread) {
  49185. /* same thread */
  49186. if (thr->state != DUK_HTHREAD_STATE_RUNNING) {
  49187. /* should actually never happen, but check anyway */
  49188. goto thread_state_error;
  49189. }
  49190. } else {
  49191. /* different thread */
  49192. DUK_ASSERT(thr->heap->curr_thread == NULL ||
  49193. thr->heap->curr_thread->state == DUK_HTHREAD_STATE_RUNNING);
  49194. if (thr->state != DUK_HTHREAD_STATE_INACTIVE) {
  49195. goto thread_state_error;
  49196. }
  49197. DUK_HEAP_SWITCH_THREAD(thr->heap, thr);
  49198. thr->state = DUK_HTHREAD_STATE_RUNNING;
  49199. /* Note: multiple threads may be simultaneously in the RUNNING
  49200. * state, but not in the same "resume chain".
  49201. */
  49202. }
  49203. DUK_ASSERT(thr->heap->curr_thread == thr);
  49204. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING);
  49205. /*
  49206. * C call recursion depth check, which provides a reasonable upper
  49207. * bound on maximum C stack size (arbitrary C stack growth is only
  49208. * possible by recursive handle_call / handle_safe_call calls).
  49209. */
  49210. DUK_ASSERT(thr->heap->call_recursion_depth >= 0);
  49211. DUK_ASSERT(thr->heap->call_recursion_depth <= thr->heap->call_recursion_limit);
  49212. if (call_flags & DUK_CALL_FLAG_IGNORE_RECLIMIT) {
  49213. DUK_DD(DUK_DDPRINT("ignoring reclimit for this call (probably an errhandler call)"));
  49214. } else {
  49215. if (thr->heap->call_recursion_depth >= thr->heap->call_recursion_limit) {
  49216. /* XXX: error message is a bit misleading: we reached a recursion
  49217. * limit which is also essentially the same as a C callstack limit
  49218. * (except perhaps with some relaxed threading assumptions).
  49219. */
  49220. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_C_CALLSTACK_LIMIT);
  49221. }
  49222. thr->heap->call_recursion_depth++;
  49223. }
  49224. /*
  49225. * Check the function type, handle bound function chains, and prepare
  49226. * parameters for the rest of the call handling. Also figure out the
  49227. * effective 'this' binding, which replaces the current value at
  49228. * idx_func + 1.
  49229. *
  49230. * If the target function is a 'bound' one, follow the chain of 'bound'
  49231. * functions until a non-bound function is found. During this process,
  49232. * bound arguments are 'prepended' to existing ones, and the "this"
  49233. * binding is overridden. See E5 Section 15.3.4.5.1.
  49234. *
  49235. * Lightfunc detection happens here too. Note that lightweight functions
  49236. * can be wrapped by (non-lightweight) bound functions so we must resolve
  49237. * the bound function chain first.
  49238. */
  49239. func = duk__nonbound_func_lookup(ctx, idx_func, &num_stack_args, &tv_func, call_flags);
  49240. DUK_TVAL_SET_TVAL(&tv_func_copy, tv_func);
  49241. tv_func = &tv_func_copy; /* local copy to avoid relookups */
  49242. DUK_ASSERT(func == NULL || !DUK_HOBJECT_HAS_BOUND(func));
  49243. DUK_ASSERT(func == NULL || (DUK_HOBJECT_IS_COMPILEDFUNCTION(func) ||
  49244. DUK_HOBJECT_IS_NATIVEFUNCTION(func)));
  49245. duk__coerce_effective_this_binding(thr, func, idx_func + 1);
  49246. DUK_DDD(DUK_DDDPRINT("effective 'this' binding is: %!T",
  49247. (duk_tval *) duk_get_tval(ctx, idx_func + 1)));
  49248. /* These base values are never used, but if the compiler doesn't know
  49249. * that DUK_ERROR() won't return, these are needed to silence warnings.
  49250. * On the other hand, scan-build will warn about the values not being
  49251. * used, so add a DUK_UNREF.
  49252. */
  49253. nargs = 0; DUK_UNREF(nargs);
  49254. nregs = 0; DUK_UNREF(nregs);
  49255. if (func == NULL) {
  49256. duk_small_uint_t lf_flags;
  49257. DUK_DDD(DUK_DDDPRINT("lightfunc call handling"));
  49258. DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv_func));
  49259. lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv_func);
  49260. nargs = DUK_LFUNC_FLAGS_GET_NARGS(lf_flags);
  49261. if (nargs == DUK_LFUNC_NARGS_VARARGS) {
  49262. nargs = -1; /* vararg */
  49263. }
  49264. nregs = nargs;
  49265. } else if (DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) {
  49266. nargs = ((duk_hcompiledfunction *) func)->nargs;
  49267. nregs = ((duk_hcompiledfunction *) func)->nregs;
  49268. DUK_ASSERT(nregs >= nargs);
  49269. } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(func)) {
  49270. /* Note: nargs (and nregs) may be negative for a native,
  49271. * function, which indicates that the function wants the
  49272. * input stack "as is" (i.e. handles "vararg" arguments).
  49273. */
  49274. nargs = ((duk_hnativefunction *) func)->nargs;
  49275. nregs = nargs;
  49276. } else {
  49277. /* XXX: this should be an assert */
  49278. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_CALLABLE);
  49279. }
  49280. /* [ ... func this arg1 ... argN ] */
  49281. /*
  49282. * Setup a preliminary activation.
  49283. *
  49284. * Don't touch valstack_bottom or valstack_top yet so that Duktape API
  49285. * calls work normally.
  49286. */
  49287. duk_hthread_callstack_grow(thr);
  49288. if (thr->callstack_top > 0) {
  49289. /*
  49290. * Update idx_retval of current activation.
  49291. *
  49292. * Although it might seem this is not necessary (bytecode executor
  49293. * does this for Ecmascript-to-Ecmascript calls; other calls are
  49294. * handled here), this turns out to be necessary for handling yield
  49295. * and resume. For them, an Ecmascript-to-native call happens, and
  49296. * the Ecmascript call's idx_retval must be set for things to work.
  49297. */
  49298. (thr->callstack + thr->callstack_top - 1)->idx_retval = entry_valstack_bottom_index + idx_func;
  49299. }
  49300. DUK_ASSERT(thr->callstack_top < thr->callstack_size);
  49301. act = thr->callstack + thr->callstack_top;
  49302. thr->callstack_top++;
  49303. DUK_ASSERT(thr->callstack_top <= thr->callstack_size);
  49304. DUK_ASSERT(thr->valstack_top > thr->valstack_bottom); /* at least effective 'this' */
  49305. DUK_ASSERT(func == NULL || !DUK_HOBJECT_HAS_BOUND(func));
  49306. act->flags = 0;
  49307. if (func == NULL || DUK_HOBJECT_HAS_STRICT(func)) {
  49308. act->flags |= DUK_ACT_FLAG_STRICT;
  49309. }
  49310. if (call_flags & DUK_CALL_FLAG_CONSTRUCTOR_CALL) {
  49311. act->flags |= DUK_ACT_FLAG_CONSTRUCT;
  49312. /*act->flags |= DUK_ACT_FLAG_PREVENT_YIELD;*/
  49313. }
  49314. if (func == NULL || DUK_HOBJECT_IS_NATIVEFUNCTION(func)) {
  49315. /*act->flags |= DUK_ACT_FLAG_PREVENT_YIELD;*/
  49316. }
  49317. if (call_flags & DUK_CALL_FLAG_DIRECT_EVAL) {
  49318. act->flags |= DUK_ACT_FLAG_DIRECT_EVAL;
  49319. }
  49320. /* As a first approximation, all calls except Ecmascript-to-Ecmascript
  49321. * calls prevent a yield.
  49322. */
  49323. act->flags |= DUK_ACT_FLAG_PREVENT_YIELD;
  49324. act->func = func; /* NULL for lightfunc */
  49325. act->var_env = NULL;
  49326. act->lex_env = NULL;
  49327. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  49328. act->prev_caller = NULL;
  49329. #endif
  49330. act->curr_pc = NULL;
  49331. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  49332. act->prev_line = 0;
  49333. #endif
  49334. act->idx_bottom = entry_valstack_bottom_index + idx_args;
  49335. #if 0 /* topmost activation idx_retval is considered garbage, no need to init */
  49336. act->idx_retval = 0;
  49337. #endif
  49338. DUK_TVAL_SET_TVAL(&act->tv_func, tv_func); /* borrowed, no refcount */
  49339. if (act->flags & DUK_ACT_FLAG_PREVENT_YIELD) {
  49340. /* duk_hthread_callstack_unwind() will decrease this on unwind */
  49341. thr->callstack_preventcount++;
  49342. }
  49343. /* XXX: Is this INCREF necessary? 'func' is always a borrowed
  49344. * reference reachable through the value stack? If changed, stack
  49345. * unwind code also needs to be fixed to match.
  49346. */
  49347. DUK_HOBJECT_INCREF_ALLOWNULL(thr, func); /* act->func */
  49348. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  49349. if (func) {
  49350. duk__update_func_caller_prop(thr, func);
  49351. }
  49352. act = thr->callstack + thr->callstack_top - 1;
  49353. #endif
  49354. /* [... func this arg1 ... argN] */
  49355. /*
  49356. * Environment record creation and 'arguments' object creation.
  49357. * Named function expression name binding is handled by the
  49358. * compiler; the compiled function's parent env will contain
  49359. * the (immutable) binding already.
  49360. *
  49361. * This handling is now identical for C and Ecmascript functions.
  49362. * C functions always have the 'NEWENV' flag set, so their
  49363. * environment record initialization is delayed (which is good).
  49364. *
  49365. * Delayed creation (on demand) is handled in duk_js_var.c.
  49366. */
  49367. DUK_ASSERT(func == NULL || !DUK_HOBJECT_HAS_BOUND(func)); /* bound function chain has already been resolved */
  49368. if (func != NULL && !DUK_HOBJECT_HAS_NEWENV(func)) {
  49369. /* use existing env (e.g. for non-strict eval); cannot have
  49370. * an own 'arguments' object (but can refer to the existing one)
  49371. */
  49372. DUK_ASSERT(!DUK_HOBJECT_HAS_CREATEARGS(func));
  49373. duk__handle_oldenv_for_call(thr, func, act);
  49374. DUK_ASSERT(act->lex_env != NULL);
  49375. DUK_ASSERT(act->var_env != NULL);
  49376. goto env_done;
  49377. }
  49378. DUK_ASSERT(func == NULL || DUK_HOBJECT_HAS_NEWENV(func));
  49379. if (func == NULL || !DUK_HOBJECT_HAS_CREATEARGS(func)) {
  49380. /* no need to create environment record now; leave as NULL */
  49381. DUK_ASSERT(act->lex_env == NULL);
  49382. DUK_ASSERT(act->var_env == NULL);
  49383. goto env_done;
  49384. }
  49385. /* third arg: absolute index (to entire valstack) of idx_bottom of new activation */
  49386. env = duk_create_activation_environment_record(thr, func, act->idx_bottom);
  49387. DUK_ASSERT(env != NULL);
  49388. /* [... func this arg1 ... argN envobj] */
  49389. DUK_ASSERT(DUK_HOBJECT_HAS_CREATEARGS(func));
  49390. duk__handle_createargs_for_call(thr, func, env, num_stack_args);
  49391. /* [... func this arg1 ... argN envobj] */
  49392. act = thr->callstack + thr->callstack_top - 1;
  49393. act->lex_env = env;
  49394. act->var_env = env;
  49395. DUK_HOBJECT_INCREF(thr, env);
  49396. DUK_HOBJECT_INCREF(thr, env); /* XXX: incref by count (2) directly */
  49397. duk_pop(ctx);
  49398. env_done:
  49399. /* [... func this arg1 ... argN] */
  49400. /*
  49401. * Setup value stack: clamp to 'nargs', fill up to 'nregs'
  49402. *
  49403. * Value stack may either grow or shrink, depending on the
  49404. * number of func registers and the number of actual arguments.
  49405. * If nregs >= 0, func wants args clamped to 'nargs'; else it
  49406. * wants all args (= 'num_stack_args').
  49407. */
  49408. duk__adjust_valstack_and_top(thr,
  49409. num_stack_args,
  49410. idx_args,
  49411. nregs,
  49412. nargs,
  49413. func);
  49414. /*
  49415. * Determine call type; then setup activation and call
  49416. */
  49417. if (func != NULL && DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) {
  49418. goto ecmascript_call;
  49419. } else {
  49420. goto native_call;
  49421. }
  49422. DUK_UNREACHABLE();
  49423. /*
  49424. * Native (C) call
  49425. */
  49426. native_call:
  49427. /*
  49428. * Shift to new valstack_bottom.
  49429. */
  49430. thr->valstack_bottom = thr->valstack_bottom + idx_args;
  49431. /* keep current valstack_top */
  49432. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  49433. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  49434. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  49435. DUK_ASSERT(func == NULL || ((duk_hnativefunction *) func)->func != NULL);
  49436. /* [... func this | arg1 ... argN] ('this' must precede new bottom) */
  49437. /*
  49438. * Actual function call and return value check.
  49439. *
  49440. * Return values:
  49441. * 0 success, no return value (default to 'undefined')
  49442. * 1 success, one return value on top of stack
  49443. * < 0 error, throw a "magic" error
  49444. * other invalid
  49445. */
  49446. /* For native calls must be NULL so we don't sync back */
  49447. DUK_ASSERT(thr->ptr_curr_pc == NULL);
  49448. if (func) {
  49449. rc = ((duk_hnativefunction *) func)->func((duk_context *) thr);
  49450. } else {
  49451. duk_c_function funcptr = DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv_func);
  49452. rc = funcptr((duk_context *) thr);
  49453. }
  49454. if (rc < 0) {
  49455. duk_error_throw_from_negative_rc(thr, rc);
  49456. DUK_UNREACHABLE();
  49457. } else if (rc > 1) {
  49458. DUK_ERROR(thr, DUK_ERR_API_ERROR, "c function returned invalid rc");
  49459. }
  49460. DUK_ASSERT(rc == 0 || rc == 1);
  49461. /*
  49462. * Unwind stack(s) and shift back to old valstack_bottom.
  49463. */
  49464. DUK_ASSERT(thr->catchstack_top == entry_catchstack_top);
  49465. DUK_ASSERT(thr->callstack_top == entry_callstack_top + 1);
  49466. #if 0 /* should be no need to unwind */
  49467. duk_hthread_catchstack_unwind(thr, entry_catchstack_top);
  49468. #endif
  49469. duk_hthread_callstack_unwind(thr, entry_callstack_top);
  49470. thr->valstack_bottom = thr->valstack + entry_valstack_bottom_index;
  49471. /* keep current valstack_top */
  49472. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  49473. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  49474. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  49475. DUK_ASSERT(thr->valstack_top - thr->valstack_bottom >= idx_func + 1);
  49476. /*
  49477. * Manipulate value stack so that return value is on top
  49478. * (pushing an 'undefined' if necessary).
  49479. */
  49480. /* XXX: should this happen in the callee's activation or after unwinding? */
  49481. if (rc == 0) {
  49482. duk_require_stack(ctx, 1);
  49483. duk_push_undefined(ctx);
  49484. }
  49485. /* [... func this (crud) retval] */
  49486. DUK_DDD(DUK_DDDPRINT("native call retval -> %!T (rc=%ld)",
  49487. (duk_tval *) duk_get_tval(ctx, -1), (long) rc));
  49488. duk_replace(ctx, idx_func);
  49489. duk_set_top(ctx, idx_func + 1);
  49490. /* [... retval] */
  49491. /* Ensure there is internal valstack spare before we exit; this may
  49492. * throw an alloc error. The same guaranteed size must be available
  49493. * as before the call. This is not optimal now: we store the valstack
  49494. * allocated size during entry; this value may be higher than the
  49495. * minimal guarantee for an application.
  49496. */
  49497. (void) duk_valstack_resize_raw((duk_context *) thr,
  49498. entry_valstack_end, /* same as during entry */
  49499. DUK_VSRESIZE_FLAG_SHRINK | /* flags */
  49500. DUK_VSRESIZE_FLAG_COMPACT |
  49501. DUK_VSRESIZE_FLAG_THROW);
  49502. /*
  49503. * Shrink checks and return with success.
  49504. */
  49505. retval = DUK_EXEC_SUCCESS;
  49506. goto shrink_and_finished;
  49507. /*
  49508. * Ecmascript call
  49509. */
  49510. ecmascript_call:
  49511. /*
  49512. * Shift to new valstack_bottom.
  49513. */
  49514. DUK_ASSERT(func != NULL);
  49515. DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(func));
  49516. act->curr_pc = DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, (duk_hcompiledfunction *) func);
  49517. thr->valstack_bottom = thr->valstack_bottom + idx_args;
  49518. /* keep current valstack_top */
  49519. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  49520. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  49521. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  49522. /* [... func this | arg1 ... argN] ('this' must precede new bottom) */
  49523. /*
  49524. * Bytecode executor call.
  49525. *
  49526. * Execute bytecode, handling any recursive function calls and
  49527. * thread resumptions. Returns when execution would return from
  49528. * the entry level activation. When the executor returns, a
  49529. * single return value is left on the stack top.
  49530. *
  49531. * The only possible longjmp() is an error (DUK_LJ_TYPE_THROW),
  49532. * other types are handled internally by the executor.
  49533. *
  49534. */
  49535. /* thr->ptr_curr_pc is set by bytecode executor early on entry */
  49536. DUK_ASSERT(thr->ptr_curr_pc == NULL);
  49537. DUK_DDD(DUK_DDDPRINT("entering bytecode execution"));
  49538. duk_js_execute_bytecode(thr);
  49539. DUK_DDD(DUK_DDDPRINT("returned from bytecode execution"));
  49540. /*
  49541. * Unwind stack(s) and shift back to old valstack_bottom.
  49542. */
  49543. DUK_ASSERT(thr->callstack_top == entry_callstack_top + 1);
  49544. duk_hthread_catchstack_unwind(thr, entry_catchstack_top);
  49545. duk_hthread_callstack_unwind(thr, entry_callstack_top);
  49546. thr->valstack_bottom = thr->valstack + entry_valstack_bottom_index;
  49547. /* keep current valstack_top */
  49548. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  49549. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  49550. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  49551. DUK_ASSERT(thr->valstack_top - thr->valstack_bottom >= idx_func + 1);
  49552. /*
  49553. * Manipulate value stack so that return value is on top.
  49554. */
  49555. /* [... func this (crud) retval] */
  49556. duk_replace(ctx, idx_func);
  49557. duk_set_top(ctx, idx_func + 1);
  49558. /* [... retval] */
  49559. /* Ensure there is internal valstack spare before we exit; this may
  49560. * throw an alloc error. The same guaranteed size must be available
  49561. * as before the call. This is not optimal now: we store the valstack
  49562. * allocated size during entry; this value may be higher than the
  49563. * minimal guarantee for an application.
  49564. */
  49565. (void) duk_valstack_resize_raw((duk_context *) thr,
  49566. entry_valstack_end, /* same as during entry */
  49567. DUK_VSRESIZE_FLAG_SHRINK | /* flags */
  49568. DUK_VSRESIZE_FLAG_COMPACT |
  49569. DUK_VSRESIZE_FLAG_THROW);
  49570. /*
  49571. * Shrink checks and return with success.
  49572. */
  49573. retval = DUK_EXEC_SUCCESS;
  49574. goto shrink_and_finished;
  49575. shrink_and_finished:
  49576. #if defined(DUK_USE_FASTINT)
  49577. /* Explicit check for fastint downgrade. */
  49578. {
  49579. duk_tval *tv_fi;
  49580. tv_fi = duk_get_tval(ctx, -1);
  49581. DUK_ASSERT(tv_fi != NULL);
  49582. DUK_TVAL_CHKFAST_INPLACE(tv_fi);
  49583. }
  49584. #endif
  49585. /* these are "soft" shrink checks, whose failures are ignored */
  49586. /* XXX: would be nice if fast path was inlined */
  49587. duk_hthread_catchstack_shrink_check(thr);
  49588. duk_hthread_callstack_shrink_check(thr);
  49589. goto finished;
  49590. finished:
  49591. if (need_setjmp) {
  49592. /* Note: either pointer may be NULL (at entry), so don't assert;
  49593. * this is now done potentially twice, which is OK
  49594. */
  49595. DUK_DDD(DUK_DDDPRINT("restore jmpbuf_ptr: %p -> %p (possibly already done)",
  49596. (void *) (thr && thr->heap ? thr->heap->lj.jmpbuf_ptr : NULL),
  49597. (void *) old_jmpbuf_ptr));
  49598. thr->heap->lj.jmpbuf_ptr = old_jmpbuf_ptr;
  49599. /* These are just convenience "wiping" of state */
  49600. thr->heap->lj.type = DUK_LJ_TYPE_UNKNOWN;
  49601. thr->heap->lj.iserror = 0;
  49602. /* Side effects should not be an issue here: tv_tmp is local and
  49603. * thr->heap (and thr->heap->lj) have a stable pointer. Finalizer
  49604. * runs etc capture even out-of-memory errors so nothing should
  49605. * throw here.
  49606. */
  49607. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, &thr->heap->lj.value1); /* side effects */
  49608. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, &thr->heap->lj.value2); /* side effects */
  49609. DUK_DDD(DUK_DDDPRINT("setjmp catchpoint torn down"));
  49610. }
  49611. /* Restore entry thread executor curr_pc stack frame pointer. */
  49612. thr->ptr_curr_pc = entry_ptr_curr_pc;
  49613. DUK_HEAP_SWITCH_THREAD(thr->heap, entry_curr_thread); /* may be NULL */
  49614. thr->state = (duk_uint8_t) entry_thread_state;
  49615. DUK_ASSERT((thr->state == DUK_HTHREAD_STATE_INACTIVE && thr->heap->curr_thread == NULL) || /* first call */
  49616. (thr->state == DUK_HTHREAD_STATE_INACTIVE && thr->heap->curr_thread != NULL) || /* other call */
  49617. (thr->state == DUK_HTHREAD_STATE_RUNNING && thr->heap->curr_thread == thr)); /* current thread */
  49618. thr->heap->call_recursion_depth = entry_call_recursion_depth;
  49619. /* If the debugger is active we need to force an interrupt so that
  49620. * debugger breakpoints are rechecked. This is important for function
  49621. * calls caused by side effects (e.g. when doing a DUK_OP_GETPROP), see
  49622. * GH-303. Only needed for success path, error path always causes a
  49623. * breakpoint recheck in the executor. It would be enough to set this
  49624. * only when returning to an Ecmascript activation, but setting the flag
  49625. * on every return should have no ill effect.
  49626. */
  49627. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  49628. if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) {
  49629. DUK_DD(DUK_DDPRINT("returning to ecmascript activation with debugger enabled, force interrupt"));
  49630. DUK_ASSERT(thr->interrupt_counter <= thr->interrupt_init);
  49631. thr->interrupt_init -= thr->interrupt_counter;
  49632. thr->interrupt_counter = 0;
  49633. thr->heap->dbg_force_restart = 1;
  49634. }
  49635. #endif
  49636. #if defined(DUK_USE_INTERRUPT_COUNTER) && defined(DUK_USE_DEBUG)
  49637. duk__interrupt_fixup(thr, entry_curr_thread);
  49638. #endif
  49639. return retval;
  49640. thread_state_error:
  49641. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "invalid thread state for call (%ld)", (long) thr->state);
  49642. DUK_UNREACHABLE();
  49643. return DUK_EXEC_ERROR; /* never executed */
  49644. }
  49645. /*
  49646. * Manipulate value stack so that exactly 'num_stack_rets' return
  49647. * values are at 'idx_retbase' in every case, assuming there are
  49648. * 'rc' return values on top of stack.
  49649. *
  49650. * This is a bit tricky, because the called C function operates in
  49651. * the same activation record and may have e.g. popped the stack
  49652. * empty (below idx_retbase).
  49653. */
  49654. 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) {
  49655. duk_context *ctx = (duk_context *) thr;
  49656. duk_idx_t idx_rcbase;
  49657. DUK_ASSERT(thr != NULL);
  49658. DUK_ASSERT(idx_retbase >= 0);
  49659. DUK_ASSERT(num_stack_rets >= 0);
  49660. DUK_ASSERT(num_actual_rets >= 0);
  49661. idx_rcbase = duk_get_top(ctx) - num_actual_rets; /* base of known return values */
  49662. DUK_DDD(DUK_DDDPRINT("adjust valstack after func call: "
  49663. "num_stack_rets=%ld, num_actual_rets=%ld, stack_top=%ld, idx_retbase=%ld, idx_rcbase=%ld",
  49664. (long) num_stack_rets, (long) num_actual_rets, (long) duk_get_top(ctx),
  49665. (long) idx_retbase, (long) idx_rcbase));
  49666. DUK_ASSERT(idx_rcbase >= 0); /* caller must check */
  49667. /* ensure space for final configuration (idx_retbase + num_stack_rets) and
  49668. * intermediate configurations
  49669. */
  49670. duk_require_stack_top(ctx,
  49671. (idx_rcbase > idx_retbase ? idx_rcbase : idx_retbase) +
  49672. num_stack_rets);
  49673. /* chop extra retvals away / extend with undefined */
  49674. duk_set_top(ctx, idx_rcbase + num_stack_rets);
  49675. if (idx_rcbase >= idx_retbase) {
  49676. duk_idx_t count = idx_rcbase - idx_retbase;
  49677. duk_idx_t i;
  49678. DUK_DDD(DUK_DDDPRINT("elements at/after idx_retbase have enough to cover func retvals "
  49679. "(idx_retbase=%ld, idx_rcbase=%ld)", (long) idx_retbase, (long) idx_rcbase));
  49680. /* nuke values at idx_retbase to get the first retval (initially
  49681. * at idx_rcbase) to idx_retbase
  49682. */
  49683. DUK_ASSERT(count >= 0);
  49684. for (i = 0; i < count; i++) {
  49685. /* XXX: inefficient; block remove primitive */
  49686. duk_remove(ctx, idx_retbase);
  49687. }
  49688. } else {
  49689. duk_idx_t count = idx_retbase - idx_rcbase;
  49690. duk_idx_t i;
  49691. DUK_DDD(DUK_DDDPRINT("not enough elements at/after idx_retbase to cover func retvals "
  49692. "(idx_retbase=%ld, idx_rcbase=%ld)", (long) idx_retbase, (long) idx_rcbase));
  49693. /* insert 'undefined' values at idx_rcbase to get the
  49694. * return values to idx_retbase
  49695. */
  49696. DUK_ASSERT(count > 0);
  49697. for (i = 0; i < count; i++) {
  49698. /* XXX: inefficient; block insert primitive */
  49699. duk_push_undefined(ctx);
  49700. duk_insert(ctx, idx_rcbase);
  49701. }
  49702. }
  49703. }
  49704. /*
  49705. * Make a "C protected call" within the current activation.
  49706. *
  49707. * The allowed thread states for making a call are the same as for
  49708. * duk_handle_call().
  49709. *
  49710. * Note that like duk_handle_call(), even if this call is protected,
  49711. * there are a few situations where the current (pre-entry) setjmp
  49712. * catcher (or a fatal error handler if no such catcher exists) is
  49713. * invoked:
  49714. *
  49715. * - Blatant API argument errors (e.g. num_stack_args is invalid,
  49716. * so we can't form a reasonable return stack)
  49717. *
  49718. * - Errors during error handling, e.g. failure to reallocate
  49719. * space in the value stack due to an alloc error
  49720. *
  49721. * Such errors propagate outwards, ultimately to the fatal error
  49722. * handler if nothing else.
  49723. */
  49724. /* XXX: bump preventcount by one for the duration of this call? */
  49725. DUK_INTERNAL
  49726. duk_int_t duk_handle_safe_call(duk_hthread *thr,
  49727. duk_safe_call_function func,
  49728. duk_idx_t num_stack_args,
  49729. duk_idx_t num_stack_rets) {
  49730. duk_context *ctx = (duk_context *) thr;
  49731. duk_size_t entry_valstack_bottom_index;
  49732. duk_size_t entry_callstack_top;
  49733. duk_size_t entry_catchstack_top;
  49734. duk_int_t entry_call_recursion_depth;
  49735. duk_hthread *entry_curr_thread;
  49736. duk_uint_fast8_t entry_thread_state;
  49737. duk_instr_t **entry_ptr_curr_pc;
  49738. duk_jmpbuf *old_jmpbuf_ptr = NULL;
  49739. duk_jmpbuf our_jmpbuf;
  49740. duk_idx_t idx_retbase;
  49741. duk_int_t retval;
  49742. duk_ret_t rc;
  49743. DUK_ASSERT(thr != NULL);
  49744. DUK_ASSERT(ctx != NULL);
  49745. /* Note: careful with indices like '-x'; if 'x' is zero, it refers to bottom */
  49746. entry_valstack_bottom_index = (duk_size_t) (thr->valstack_bottom - thr->valstack);
  49747. entry_callstack_top = thr->callstack_top;
  49748. entry_catchstack_top = thr->catchstack_top;
  49749. entry_call_recursion_depth = thr->heap->call_recursion_depth;
  49750. entry_curr_thread = thr->heap->curr_thread; /* Note: may be NULL if first call */
  49751. entry_thread_state = thr->state;
  49752. entry_ptr_curr_pc = thr->ptr_curr_pc; /* may be NULL */
  49753. idx_retbase = duk_get_top(ctx) - num_stack_args; /* Note: not a valid stack index if num_stack_args == 0 */
  49754. /* Note: cannot portably debug print a function pointer, hence 'func' not printed! */
  49755. DUK_DD(DUK_DDPRINT("duk_handle_safe_call: thr=%p, num_stack_args=%ld, num_stack_rets=%ld, "
  49756. "valstack_top=%ld, idx_retbase=%ld, rec_depth=%ld/%ld, "
  49757. "entry_valstack_bottom_index=%ld, entry_callstack_top=%ld, entry_catchstack_top=%ld, "
  49758. "entry_call_recursion_depth=%ld, entry_curr_thread=%p, entry_thread_state=%ld",
  49759. (void *) thr,
  49760. (long) num_stack_args,
  49761. (long) num_stack_rets,
  49762. (long) duk_get_top(ctx),
  49763. (long) idx_retbase,
  49764. (long) thr->heap->call_recursion_depth,
  49765. (long) thr->heap->call_recursion_limit,
  49766. (long) entry_valstack_bottom_index,
  49767. (long) entry_callstack_top,
  49768. (long) entry_catchstack_top,
  49769. (long) entry_call_recursion_depth,
  49770. (void *) entry_curr_thread,
  49771. (long) entry_thread_state));
  49772. if (idx_retbase < 0) {
  49773. /*
  49774. * Since stack indices are not reliable, we can't do anything useful
  49775. * here. Invoke the existing setjmp catcher, or if it doesn't exist,
  49776. * call the fatal error handler.
  49777. */
  49778. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  49779. }
  49780. /* setjmp catchpoint setup */
  49781. old_jmpbuf_ptr = thr->heap->lj.jmpbuf_ptr;
  49782. thr->heap->lj.jmpbuf_ptr = &our_jmpbuf;
  49783. if (DUK_SETJMP(thr->heap->lj.jmpbuf_ptr->jb) == 0) {
  49784. goto handle_call;
  49785. }
  49786. /*
  49787. * Error during call. The error value is at heap->lj.value1.
  49788. *
  49789. * Careful with variable accesses here; must be assigned to before
  49790. * setjmp() or be declared volatile. See duk_handle_call().
  49791. *
  49792. * The following are such variables:
  49793. * - duk_handle_safe_call() parameters
  49794. * - entry_*
  49795. * - idx_retbase
  49796. *
  49797. * The very first thing we do is restore the previous setjmp catcher.
  49798. * This means that any error in error handling will propagate outwards
  49799. * instead of causing a setjmp() re-entry above. The *only* actual
  49800. * errors that should happen here are allocation errors.
  49801. */
  49802. DUK_DDD(DUK_DDDPRINT("error caught during protected duk_handle_safe_call()"));
  49803. DUK_ASSERT(thr->heap->lj.type == DUK_LJ_TYPE_THROW);
  49804. DUK_ASSERT(thr->callstack_top >= entry_callstack_top);
  49805. DUK_ASSERT(thr->catchstack_top >= entry_catchstack_top);
  49806. /* Note: either pointer may be NULL (at entry), so don't assert;
  49807. * these are now restored twice which is OK.
  49808. */
  49809. thr->heap->lj.jmpbuf_ptr = old_jmpbuf_ptr;
  49810. duk_hthread_catchstack_unwind(thr, entry_catchstack_top);
  49811. duk_hthread_callstack_unwind(thr, entry_callstack_top);
  49812. thr->valstack_bottom = thr->valstack + entry_valstack_bottom_index;
  49813. /* [ ... | (crud) ] */
  49814. /* XXX: space in valstack? see discussion in duk_handle_call. */
  49815. duk_push_tval(ctx, &thr->heap->lj.value1);
  49816. /* [ ... | (crud) errobj ] */
  49817. DUK_ASSERT(duk_get_top(ctx) >= 1); /* at least errobj must be on stack */
  49818. /* check that the valstack has space for the final amount and any
  49819. * intermediate space needed; this is unoptimal but should be safe
  49820. */
  49821. duk_require_stack_top(ctx, idx_retbase + num_stack_rets); /* final configuration */
  49822. duk_require_stack(ctx, num_stack_rets);
  49823. duk__safe_call_adjust_valstack(thr, idx_retbase, num_stack_rets, 1); /* 1 = num actual 'return values' */
  49824. /* [ ... | ] or [ ... | errobj (M * undefined)] where M = num_stack_rets - 1 */
  49825. retval = DUK_EXEC_ERROR;
  49826. goto shrink_and_finished;
  49827. /*
  49828. * Handle call (inside setjmp)
  49829. */
  49830. handle_call:
  49831. DUK_DDD(DUK_DDDPRINT("safe_call setjmp catchpoint setup complete"));
  49832. /*
  49833. * Thread state check and book-keeping.
  49834. */
  49835. if (thr == thr->heap->curr_thread) {
  49836. /* same thread */
  49837. if (thr->state != DUK_HTHREAD_STATE_RUNNING) {
  49838. /* should actually never happen, but check anyway */
  49839. goto thread_state_error;
  49840. }
  49841. } else {
  49842. /* different thread */
  49843. DUK_ASSERT(thr->heap->curr_thread == NULL ||
  49844. thr->heap->curr_thread->state == DUK_HTHREAD_STATE_RUNNING);
  49845. if (thr->state != DUK_HTHREAD_STATE_INACTIVE) {
  49846. goto thread_state_error;
  49847. }
  49848. DUK_HEAP_SWITCH_THREAD(thr->heap, thr);
  49849. thr->state = DUK_HTHREAD_STATE_RUNNING;
  49850. /* Note: multiple threads may be simultaneously in the RUNNING
  49851. * state, but not in the same "resume chain".
  49852. */
  49853. }
  49854. DUK_ASSERT(thr->heap->curr_thread == thr);
  49855. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING);
  49856. /*
  49857. * Recursion limit check.
  49858. *
  49859. * Note: there is no need for an "ignore recursion limit" flag
  49860. * for duk_handle_safe_call now.
  49861. */
  49862. DUK_ASSERT(thr->heap->call_recursion_depth >= 0);
  49863. DUK_ASSERT(thr->heap->call_recursion_depth <= thr->heap->call_recursion_limit);
  49864. if (thr->heap->call_recursion_depth >= thr->heap->call_recursion_limit) {
  49865. /* XXX: error message is a bit misleading: we reached a recursion
  49866. * limit which is also essentially the same as a C callstack limit
  49867. * (except perhaps with some relaxed threading assumptions).
  49868. */
  49869. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_C_CALLSTACK_LIMIT);
  49870. }
  49871. thr->heap->call_recursion_depth++;
  49872. /*
  49873. * Valstack spare check
  49874. */
  49875. duk_require_stack(ctx, 0); /* internal spare */
  49876. /*
  49877. * Make the C call
  49878. */
  49879. rc = func(ctx);
  49880. DUK_DDD(DUK_DDDPRINT("safe_call, func rc=%ld", (long) rc));
  49881. /*
  49882. * Valstack manipulation for results
  49883. */
  49884. /* we're running inside the caller's activation, so no change in call/catch stack or valstack bottom */
  49885. DUK_ASSERT(thr->callstack_top == entry_callstack_top);
  49886. DUK_ASSERT(thr->catchstack_top == entry_catchstack_top);
  49887. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  49888. DUK_ASSERT((duk_size_t) (thr->valstack_bottom - thr->valstack) == entry_valstack_bottom_index);
  49889. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  49890. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  49891. if (rc < 0) {
  49892. duk_error_throw_from_negative_rc(thr, rc);
  49893. }
  49894. DUK_ASSERT(rc >= 0);
  49895. if (duk_get_top(ctx) < rc) {
  49896. DUK_ERROR(thr, DUK_ERR_API_ERROR, "not enough stack values for safe_call rc");
  49897. }
  49898. duk__safe_call_adjust_valstack(thr, idx_retbase, num_stack_rets, rc);
  49899. /* Note: no need from callstack / catchstack shrink check */
  49900. retval = DUK_EXEC_SUCCESS;
  49901. goto finished;
  49902. shrink_and_finished:
  49903. /* these are "soft" shrink checks, whose failures are ignored */
  49904. /* XXX: would be nice if fast path was inlined */
  49905. duk_hthread_catchstack_shrink_check(thr);
  49906. duk_hthread_callstack_shrink_check(thr);
  49907. goto finished;
  49908. finished:
  49909. /* Note: either pointer may be NULL (at entry), so don't assert */
  49910. thr->heap->lj.jmpbuf_ptr = old_jmpbuf_ptr;
  49911. /* These are just convenience "wiping" of state */
  49912. thr->heap->lj.type = DUK_LJ_TYPE_UNKNOWN;
  49913. thr->heap->lj.iserror = 0;
  49914. /* Side effects should not be an issue here: tv_tmp is local and
  49915. * thr->heap (and thr->heap->lj) have a stable pointer. Finalizer
  49916. * runs etc capture even out-of-memory errors so nothing should
  49917. * throw here.
  49918. */
  49919. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, &thr->heap->lj.value1); /* side effects */
  49920. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, &thr->heap->lj.value2); /* side effects */
  49921. DUK_DDD(DUK_DDDPRINT("setjmp catchpoint torn down"));
  49922. /* Restore entry thread executor curr_pc stack frame pointer. */
  49923. thr->ptr_curr_pc = entry_ptr_curr_pc;
  49924. /* XXX: because we unwind stacks above, thr->heap->curr_thread is at
  49925. * risk of pointing to an already freed thread. This was indeed the
  49926. * case in test-bug-multithread-valgrind.c, until duk_handle_call()
  49927. * was fixed to restore thr->heap->curr_thread before rethrowing an
  49928. * uncaught error.
  49929. */
  49930. DUK_HEAP_SWITCH_THREAD(thr->heap, entry_curr_thread); /* may be NULL */
  49931. thr->state = (duk_uint8_t) entry_thread_state;
  49932. DUK_ASSERT((thr->state == DUK_HTHREAD_STATE_INACTIVE && thr->heap->curr_thread == NULL) || /* first call */
  49933. (thr->state == DUK_HTHREAD_STATE_INACTIVE && thr->heap->curr_thread != NULL) || /* other call */
  49934. (thr->state == DUK_HTHREAD_STATE_RUNNING && thr->heap->curr_thread == thr)); /* current thread */
  49935. thr->heap->call_recursion_depth = entry_call_recursion_depth;
  49936. /* stack discipline consistency check */
  49937. DUK_ASSERT(duk_get_top(ctx) == idx_retbase + num_stack_rets);
  49938. /* A debugger forced interrupt check is not needed here, as
  49939. * problematic safe calls are not caused by side effects.
  49940. */
  49941. #if defined(DUK_USE_INTERRUPT_COUNTER) && defined(DUK_USE_DEBUG)
  49942. duk__interrupt_fixup(thr, entry_curr_thread);
  49943. #endif
  49944. return retval;
  49945. thread_state_error:
  49946. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "invalid thread state for safe_call (%ld)", (long) thr->state);
  49947. DUK_UNREACHABLE();
  49948. return DUK_EXEC_ERROR; /* never executed */
  49949. }
  49950. /*
  49951. * Helper for handling an Ecmascript-to-Ecmascript call or an Ecmascript
  49952. * function (initial) Duktape.Thread.resume().
  49953. *
  49954. * Compared to normal calls handled by duk_handle_call(), there are a
  49955. * bunch of differences:
  49956. *
  49957. * - the call is never protected
  49958. * - there is no C recursion depth increase (hence an "ignore recursion
  49959. * limit" flag is not applicable)
  49960. * - instead of making the call, this helper just performs the thread
  49961. * setup and returns; the bytecode executor then restarts execution
  49962. * internally
  49963. * - ecmascript functions are never 'vararg' functions (they access
  49964. * varargs through the 'arguments' object)
  49965. *
  49966. * The callstack of the target contains an earlier Ecmascript call in case
  49967. * of an Ecmascript-to-Ecmascript call (whose idx_retval is updated), or
  49968. * is empty in case of an initial Duktape.Thread.resume().
  49969. *
  49970. * The first thing to do here is to figure out whether an ecma-to-ecma
  49971. * call is actually possible. It's not always the case if the target is
  49972. * a bound function; the final function may be native. In that case,
  49973. * return an error so caller can fall back to a normal call path.
  49974. */
  49975. DUK_INTERNAL
  49976. duk_bool_t duk_handle_ecma_call_setup(duk_hthread *thr,
  49977. duk_idx_t num_stack_args,
  49978. duk_small_uint_t call_flags) {
  49979. duk_context *ctx = (duk_context *) thr;
  49980. duk_size_t entry_valstack_bottom_index;
  49981. duk_idx_t idx_func; /* valstack index of 'func' and retval (relative to entry valstack_bottom) */
  49982. duk_idx_t idx_args; /* valstack index of start of args (arg1) (relative to entry valstack_bottom) */
  49983. duk_idx_t nargs; /* # argument registers target function wants (< 0 => never for ecma calls) */
  49984. duk_idx_t nregs; /* # total registers target function wants on entry (< 0 => never for ecma calls) */
  49985. duk_hobject *func; /* 'func' on stack (borrowed reference) */
  49986. duk_tval *tv_func; /* duk_tval ptr for 'func' on stack (borrowed reference) */
  49987. duk_activation *act;
  49988. duk_hobject *env;
  49989. duk_bool_t use_tailcall;
  49990. duk_instr_t **entry_ptr_curr_pc;
  49991. DUK_ASSERT(thr != NULL);
  49992. DUK_ASSERT(ctx != NULL);
  49993. DUK_ASSERT(!((call_flags & DUK_CALL_FLAG_IS_RESUME) != 0 && (call_flags & DUK_CALL_FLAG_IS_TAILCALL) != 0));
  49994. /* XXX: assume these? */
  49995. DUK_ASSERT(thr->valstack != NULL);
  49996. DUK_ASSERT(thr->callstack != NULL);
  49997. DUK_ASSERT(thr->catchstack != NULL);
  49998. /* no need to handle thread state book-keeping here */
  49999. DUK_ASSERT((call_flags & DUK_CALL_FLAG_IS_RESUME) != 0 ||
  50000. (thr->state == DUK_HTHREAD_STATE_RUNNING &&
  50001. thr->heap->curr_thread == thr));
  50002. /* If thr->ptr_curr_pc is set, sync curr_pc to act->pc. Then NULL
  50003. * thr->ptr_curr_pc so that it's not accidentally used with an incorrect
  50004. * activation when side effects occur. If we end up not making the
  50005. * call we must restore the value.
  50006. */
  50007. entry_ptr_curr_pc = thr->ptr_curr_pc;
  50008. duk_hthread_sync_and_null_currpc(thr);
  50009. /* if a tail call:
  50010. * - an Ecmascript activation must be on top of the callstack
  50011. * - there cannot be any active catchstack entries
  50012. */
  50013. #ifdef DUK_USE_ASSERTIONS
  50014. if (call_flags & DUK_CALL_FLAG_IS_TAILCALL) {
  50015. duk_size_t our_callstack_index;
  50016. duk_size_t i;
  50017. DUK_ASSERT(thr->callstack_top >= 1);
  50018. our_callstack_index = thr->callstack_top - 1;
  50019. DUK_ASSERT_DISABLE(our_callstack_index >= 0);
  50020. DUK_ASSERT(our_callstack_index < thr->callstack_size);
  50021. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + our_callstack_index) != NULL);
  50022. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + our_callstack_index)));
  50023. /* No entry in the catchstack which would actually catch a
  50024. * throw can refer to the callstack entry being reused.
  50025. * There *can* be catchstack entries referring to the current
  50026. * callstack entry as long as they don't catch (e.g. label sites).
  50027. */
  50028. for (i = 0; i < thr->catchstack_top; i++) {
  50029. DUK_ASSERT(thr->catchstack[i].callstack_index < our_callstack_index || /* refer to callstack entries below current */
  50030. DUK_CAT_GET_TYPE(thr->catchstack + i) == DUK_CAT_TYPE_LABEL); /* or a non-catching entry */
  50031. }
  50032. }
  50033. #endif /* DUK_USE_ASSERTIONS */
  50034. entry_valstack_bottom_index = (duk_size_t) (thr->valstack_bottom - thr->valstack);
  50035. idx_func = duk_normalize_index(thr, -num_stack_args - 2);
  50036. idx_args = idx_func + 2;
  50037. DUK_DD(DUK_DDPRINT("handle_ecma_call_setup: thr=%p, "
  50038. "num_stack_args=%ld, call_flags=0x%08lx (resume=%ld, tailcall=%ld), "
  50039. "idx_func=%ld, idx_args=%ld, entry_valstack_bottom_index=%ld",
  50040. (void *) thr,
  50041. (long) num_stack_args,
  50042. (unsigned long) call_flags,
  50043. (long) ((call_flags & DUK_CALL_FLAG_IS_RESUME) != 0 ? 1 : 0),
  50044. (long) ((call_flags & DUK_CALL_FLAG_IS_TAILCALL) != 0 ? 1 : 0),
  50045. (long) idx_func,
  50046. (long) idx_args,
  50047. (long) entry_valstack_bottom_index));
  50048. if (idx_func < 0 || idx_args < 0) {
  50049. /* XXX: assert? compiler is responsible for this never happening */
  50050. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  50051. }
  50052. /*
  50053. * Check the function type, handle bound function chains, and prepare
  50054. * parameters for the rest of the call handling. Also figure out the
  50055. * effective 'this' binding, which replaces the current value at
  50056. * idx_func + 1.
  50057. *
  50058. * If the target function is a 'bound' one, follow the chain of 'bound'
  50059. * functions until a non-bound function is found. During this process,
  50060. * bound arguments are 'prepended' to existing ones, and the "this"
  50061. * binding is overridden. See E5 Section 15.3.4.5.1.
  50062. *
  50063. * If the final target function cannot be handled by an ecma-to-ecma
  50064. * call, return to the caller with a return value indicating this case.
  50065. * The bound chain is resolved and the caller can resume with a plain
  50066. * function call.
  50067. */
  50068. func = duk__nonbound_func_lookup(ctx, idx_func, &num_stack_args, &tv_func, call_flags);
  50069. if (func == NULL || !DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) {
  50070. DUK_DDD(DUK_DDDPRINT("final target is a lightfunc/nativefunc, cannot do ecma-to-ecma call"));
  50071. thr->ptr_curr_pc = entry_ptr_curr_pc;
  50072. return 0;
  50073. }
  50074. /* XXX: tv_func is not actually needed */
  50075. DUK_ASSERT(func != NULL);
  50076. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func));
  50077. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(func));
  50078. duk__coerce_effective_this_binding(thr, func, idx_func + 1);
  50079. DUK_DDD(DUK_DDDPRINT("effective 'this' binding is: %!T",
  50080. duk_get_tval(ctx, idx_func + 1)));
  50081. nargs = ((duk_hcompiledfunction *) func)->nargs;
  50082. nregs = ((duk_hcompiledfunction *) func)->nregs;
  50083. DUK_ASSERT(nregs >= nargs);
  50084. /* [ ... func this arg1 ... argN ] */
  50085. /*
  50086. * Preliminary activation record and valstack manipulation.
  50087. * The concrete actions depend on whether the we're dealing
  50088. * with a tail call (reuse an existing activation), a resume,
  50089. * or a normal call.
  50090. *
  50091. * The basic actions, in varying order, are:
  50092. *
  50093. * - Check stack size for call handling
  50094. * - Grow call stack if necessary (non-tail-calls)
  50095. * - Update current activation (idx_retval) if necessary
  50096. * (non-tail, non-resume calls)
  50097. * - Move start of args (idx_args) to valstack bottom
  50098. * (tail calls)
  50099. *
  50100. * Don't touch valstack_bottom or valstack_top yet so that Duktape API
  50101. * calls work normally.
  50102. */
  50103. /* XXX: some overlapping code; cleanup */
  50104. use_tailcall = call_flags & DUK_CALL_FLAG_IS_TAILCALL;
  50105. #if !defined(DUK_USE_TAILCALL)
  50106. DUK_ASSERT(use_tailcall == 0); /* compiler ensures this */
  50107. #endif
  50108. if (use_tailcall) {
  50109. /* tailcall cannot be flagged to resume calls, and a
  50110. * previous frame must exist
  50111. */
  50112. DUK_ASSERT(thr->callstack_top >= 1);
  50113. DUK_ASSERT((call_flags & DUK_CALL_FLAG_IS_RESUME) == 0);
  50114. act = thr->callstack + thr->callstack_top - 1;
  50115. if (act->flags & DUK_ACT_FLAG_PREVENT_YIELD) {
  50116. /* See: test-bug-tailcall-preventyield-assert.c. */
  50117. DUK_DDD(DUK_DDDPRINT("tail call prevented by current activation having DUK_ACT_FLAG_PREVENTYIELD"));
  50118. use_tailcall = 0;
  50119. } else if (DUK_HOBJECT_HAS_NOTAIL(func)) {
  50120. DUK_D(DUK_DPRINT("tail call prevented by function having a notail flag"));
  50121. use_tailcall = 0;
  50122. }
  50123. }
  50124. if (use_tailcall) {
  50125. duk_tval *tv1, *tv2;
  50126. duk_size_t cs_index;
  50127. duk_int_t i_stk; /* must be signed for loop structure */
  50128. duk_idx_t i_arg;
  50129. /*
  50130. * Tailcall handling
  50131. *
  50132. * Although the callstack entry is reused, we need to explicitly unwind
  50133. * the current activation (or simulate an unwind). In particular, the
  50134. * current activation must be closed, otherwise something like
  50135. * test-bug-reduce-judofyr.js results. Also catchstack needs be unwound
  50136. * because there may be non-error-catching label entries in valid tail calls.
  50137. */
  50138. DUK_DDD(DUK_DDDPRINT("is tail call, reusing activation at callstack top, at index %ld",
  50139. (long) (thr->callstack_top - 1)));
  50140. /* 'act' already set above */
  50141. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func));
  50142. DUK_ASSERT(!DUK_HOBJECT_HAS_NATIVEFUNCTION(func));
  50143. DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(func));
  50144. DUK_ASSERT((act->flags & DUK_ACT_FLAG_PREVENT_YIELD) == 0);
  50145. /* Unwind catchstack entries referring to the callstack entry we're reusing */
  50146. cs_index = thr->callstack_top - 1;
  50147. DUK_ASSERT(thr->catchstack_top <= DUK_INT_MAX); /* catchstack limits */
  50148. for (i_stk = (duk_int_t) (thr->catchstack_top - 1); i_stk >= 0; i_stk--) {
  50149. duk_catcher *cat = thr->catchstack + i_stk;
  50150. if (cat->callstack_index != cs_index) {
  50151. /* 'i' is the first entry we'll keep */
  50152. break;
  50153. }
  50154. }
  50155. duk_hthread_catchstack_unwind(thr, i_stk + 1);
  50156. /* Unwind the topmost callstack entry before reusing it */
  50157. DUK_ASSERT(thr->callstack_top > 0);
  50158. duk_hthread_callstack_unwind(thr, thr->callstack_top - 1);
  50159. /* Then reuse the unwound activation; callstack was not shrunk so there is always space */
  50160. thr->callstack_top++;
  50161. DUK_ASSERT(thr->callstack_top <= thr->callstack_size);
  50162. act = thr->callstack + thr->callstack_top - 1;
  50163. /* Start filling in the activation */
  50164. act->func = func; /* don't want an intermediate exposed state with func == NULL */
  50165. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  50166. act->prev_caller = NULL;
  50167. #endif
  50168. DUK_ASSERT(func != NULL);
  50169. DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(func));
  50170. /* don't want an intermediate exposed state with invalid pc */
  50171. act->curr_pc = DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, (duk_hcompiledfunction *) func);
  50172. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  50173. act->prev_line = 0;
  50174. #endif
  50175. DUK_TVAL_SET_OBJECT(&act->tv_func, func); /* borrowed, no refcount */
  50176. #ifdef DUK_USE_REFERENCE_COUNTING
  50177. DUK_HOBJECT_INCREF(thr, func);
  50178. act = thr->callstack + thr->callstack_top - 1; /* side effects (currently none though) */
  50179. #endif
  50180. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  50181. #ifdef DUK_USE_TAILCALL
  50182. #error incorrect options: tail calls enabled with function caller property
  50183. #endif
  50184. /* XXX: this doesn't actually work properly for tail calls, so
  50185. * tail calls are disabled when DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  50186. * is in use.
  50187. */
  50188. duk__update_func_caller_prop(thr, func);
  50189. act = thr->callstack + thr->callstack_top - 1;
  50190. #endif
  50191. act->flags = (DUK_HOBJECT_HAS_STRICT(func) ?
  50192. DUK_ACT_FLAG_STRICT | DUK_ACT_FLAG_TAILCALLED :
  50193. DUK_ACT_FLAG_TAILCALLED);
  50194. DUK_ASSERT(DUK_ACT_GET_FUNC(act) == func); /* already updated */
  50195. DUK_ASSERT(act->var_env == NULL); /* already NULLed (by unwind) */
  50196. DUK_ASSERT(act->lex_env == NULL); /* already NULLed (by unwind) */
  50197. act->idx_bottom = entry_valstack_bottom_index; /* tail call -> reuse current "frame" */
  50198. DUK_ASSERT(nregs >= 0);
  50199. #if 0 /* topmost activation idx_retval is considered garbage, no need to init */
  50200. act->idx_retval = 0;
  50201. #endif
  50202. /*
  50203. * Manipulate valstack so that args are on the current bottom and the
  50204. * previous caller's 'this' binding (which is the value preceding the
  50205. * current bottom) is replaced with the new 'this' binding:
  50206. *
  50207. * [ ... this_old | (crud) func this_new arg1 ... argN ]
  50208. * --> [ ... this_new | arg1 ... argN ]
  50209. *
  50210. * For tail calling to work properly, the valstack bottom must not grow
  50211. * here; otherwise crud would accumulate on the valstack.
  50212. */
  50213. tv1 = thr->valstack_bottom - 1;
  50214. tv2 = thr->valstack_bottom + idx_func + 1;
  50215. DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top); /* tv1 is -below- valstack_bottom */
  50216. DUK_ASSERT(tv2 >= thr->valstack_bottom && tv2 < thr->valstack_top);
  50217. DUK_TVAL_SET_TVAL_UPDREF(thr, tv1, tv2); /* side effects */
  50218. for (i_arg = 0; i_arg < idx_args; i_arg++) {
  50219. /* XXX: block removal API primitive */
  50220. /* Note: 'func' is popped from valstack here, but it is
  50221. * already reachable from the activation.
  50222. */
  50223. duk_remove(ctx, 0);
  50224. }
  50225. idx_func = 0; DUK_UNREF(idx_func); /* really 'not applicable' anymore, should not be referenced after this */
  50226. idx_args = 0;
  50227. /* [ ... this_new | arg1 ... argN ] */
  50228. } else {
  50229. DUK_DDD(DUK_DDDPRINT("not a tail call, pushing a new activation to callstack, to index %ld",
  50230. (long) (thr->callstack_top)));
  50231. duk_hthread_callstack_grow(thr);
  50232. if (call_flags & DUK_CALL_FLAG_IS_RESUME) {
  50233. DUK_DDD(DUK_DDDPRINT("is resume -> no update to current activation (may not even exist)"));
  50234. } else {
  50235. DUK_DDD(DUK_DDDPRINT("update to current activation idx_retval"));
  50236. DUK_ASSERT(thr->callstack_top < thr->callstack_size);
  50237. DUK_ASSERT(thr->callstack_top >= 1);
  50238. act = thr->callstack + thr->callstack_top - 1;
  50239. DUK_ASSERT(DUK_ACT_GET_FUNC(act) != NULL);
  50240. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(act)));
  50241. act->idx_retval = entry_valstack_bottom_index + idx_func;
  50242. }
  50243. DUK_ASSERT(thr->callstack_top < thr->callstack_size);
  50244. act = thr->callstack + thr->callstack_top;
  50245. thr->callstack_top++;
  50246. DUK_ASSERT(thr->callstack_top <= thr->callstack_size);
  50247. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func));
  50248. DUK_ASSERT(!DUK_HOBJECT_HAS_NATIVEFUNCTION(func));
  50249. DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(func));
  50250. act->flags = (DUK_HOBJECT_HAS_STRICT(func) ?
  50251. DUK_ACT_FLAG_STRICT :
  50252. 0);
  50253. act->func = func;
  50254. act->var_env = NULL;
  50255. act->lex_env = NULL;
  50256. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  50257. act->prev_caller = NULL;
  50258. #endif
  50259. DUK_ASSERT(func != NULL);
  50260. DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(func));
  50261. act->curr_pc = DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, (duk_hcompiledfunction *) func);
  50262. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  50263. act->prev_line = 0;
  50264. #endif
  50265. act->idx_bottom = entry_valstack_bottom_index + idx_args;
  50266. DUK_ASSERT(nregs >= 0);
  50267. #if 0 /* topmost activation idx_retval is considered garbage, no need to init */
  50268. act->idx_retval = 0;
  50269. #endif
  50270. DUK_TVAL_SET_OBJECT(&act->tv_func, func); /* borrowed, no refcount */
  50271. DUK_HOBJECT_INCREF(thr, func); /* act->func */
  50272. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  50273. duk__update_func_caller_prop(thr, func);
  50274. act = thr->callstack + thr->callstack_top - 1;
  50275. #endif
  50276. }
  50277. /* [... func this arg1 ... argN] (not tail call)
  50278. * [this | arg1 ... argN] (tail call)
  50279. *
  50280. * idx_args updated to match
  50281. */
  50282. /*
  50283. * Environment record creation and 'arguments' object creation.
  50284. * Named function expression name binding is handled by the
  50285. * compiler; the compiled function's parent env will contain
  50286. * the (immutable) binding already.
  50287. *
  50288. * Delayed creation (on demand) is handled in duk_js_var.c.
  50289. */
  50290. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func)); /* bound function chain has already been resolved */
  50291. if (!DUK_HOBJECT_HAS_NEWENV(func)) {
  50292. /* use existing env (e.g. for non-strict eval); cannot have
  50293. * an own 'arguments' object (but can refer to the existing one)
  50294. */
  50295. duk__handle_oldenv_for_call(thr, func, act);
  50296. DUK_ASSERT(act->lex_env != NULL);
  50297. DUK_ASSERT(act->var_env != NULL);
  50298. goto env_done;
  50299. }
  50300. DUK_ASSERT(DUK_HOBJECT_HAS_NEWENV(func));
  50301. if (!DUK_HOBJECT_HAS_CREATEARGS(func)) {
  50302. /* no need to create environment record now; leave as NULL */
  50303. DUK_ASSERT(act->lex_env == NULL);
  50304. DUK_ASSERT(act->var_env == NULL);
  50305. goto env_done;
  50306. }
  50307. /* third arg: absolute index (to entire valstack) of idx_bottom of new activation */
  50308. env = duk_create_activation_environment_record(thr, func, act->idx_bottom);
  50309. DUK_ASSERT(env != NULL);
  50310. /* [... arg1 ... argN envobj] */
  50311. /* original input stack before nargs/nregs handling must be
  50312. * intact for 'arguments' object
  50313. */
  50314. DUK_ASSERT(DUK_HOBJECT_HAS_CREATEARGS(func));
  50315. duk__handle_createargs_for_call(thr, func, env, num_stack_args);
  50316. /* [... arg1 ... argN envobj] */
  50317. act = thr->callstack + thr->callstack_top - 1;
  50318. act->lex_env = env;
  50319. act->var_env = env;
  50320. DUK_HOBJECT_INCREF(thr, act->lex_env);
  50321. DUK_HOBJECT_INCREF(thr, act->var_env);
  50322. duk_pop(ctx);
  50323. env_done:
  50324. /* [... arg1 ... argN] */
  50325. /*
  50326. * Setup value stack: clamp to 'nargs', fill up to 'nregs'
  50327. */
  50328. duk__adjust_valstack_and_top(thr,
  50329. num_stack_args,
  50330. idx_args,
  50331. nregs,
  50332. nargs,
  50333. func);
  50334. /*
  50335. * Shift to new valstack_bottom.
  50336. */
  50337. thr->valstack_bottom = thr->valstack_bottom + idx_args;
  50338. /* keep current valstack_top */
  50339. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  50340. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  50341. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  50342. /*
  50343. * Return to bytecode executor, which will resume execution from
  50344. * the topmost activation.
  50345. */
  50346. return 1;
  50347. }
  50348. #line 1 "duk_js_compiler.c"
  50349. /*
  50350. * Ecmascript compiler.
  50351. *
  50352. * Parses an input string and generates a function template result.
  50353. * Compilation may happen in multiple contexts (global code, eval
  50354. * code, function code).
  50355. *
  50356. * The parser uses a traditional top-down recursive parsing for the
  50357. * statement level, and an operator precedence based top-down approach
  50358. * for the expression level. The attempt is to minimize the C stack
  50359. * depth. Bytecode is generated directly without an intermediate
  50360. * representation (tree), at the cost of needing two passes over each
  50361. * function.
  50362. *
  50363. * The top-down recursive parser functions are named "duk__parse_XXX".
  50364. *
  50365. * Recursion limits are in key functions to prevent arbitrary C recursion:
  50366. * function body parsing, statement parsing, and expression parsing.
  50367. *
  50368. * See doc/compiler.rst for discussion on the design.
  50369. *
  50370. * A few typing notes:
  50371. *
  50372. * - duk_regconst_t: unsigned, no marker value for "none"
  50373. * - duk_reg_t: signed, < 0 = none
  50374. * - PC values: duk_int_t, negative values used as markers
  50375. */
  50376. /* include removed: duk_internal.h */
  50377. /* if highest bit of a register number is set, it refers to a constant instead */
  50378. #define DUK__CONST_MARKER DUK_JS_CONST_MARKER
  50379. /* for array and object literals */
  50380. #define DUK__MAX_ARRAY_INIT_VALUES 20
  50381. #define DUK__MAX_OBJECT_INIT_PAIRS 10
  50382. /* XXX: hack, remove when const lookup is not O(n) */
  50383. #define DUK__GETCONST_MAX_CONSTS_CHECK 256
  50384. /* These limits are based on bytecode limits. Max temps is limited
  50385. * by duk_hcompiledfunction nargs/nregs fields being 16 bits.
  50386. */
  50387. #define DUK__MAX_CONSTS DUK_BC_BC_MAX
  50388. #define DUK__MAX_FUNCS DUK_BC_BC_MAX
  50389. #define DUK__MAX_TEMPS 0xffffL
  50390. /* Initial bytecode size allocation. */
  50391. #define DUK__BC_INITIAL_INSTS 256
  50392. #define DUK__RECURSION_INCREASE(comp_ctx,thr) do { \
  50393. DUK_DDD(DUK_DDDPRINT("RECURSION INCREASE: %s:%ld", (const char *) DUK_FILE_MACRO, (long) DUK_LINE_MACRO)); \
  50394. duk__recursion_increase((comp_ctx)); \
  50395. } while (0)
  50396. #define DUK__RECURSION_DECREASE(comp_ctx,thr) do { \
  50397. DUK_DDD(DUK_DDDPRINT("RECURSION DECREASE: %s:%ld", (const char *) DUK_FILE_MACRO, (long) DUK_LINE_MACRO)); \
  50398. duk__recursion_decrease((comp_ctx)); \
  50399. } while (0)
  50400. /* Value stack slot limits: these are quite approximate right now, and
  50401. * because they overlap in control flow, some could be eliminated.
  50402. */
  50403. #define DUK__COMPILE_ENTRY_SLOTS 8
  50404. #define DUK__FUNCTION_INIT_REQUIRE_SLOTS 16
  50405. #define DUK__FUNCTION_BODY_REQUIRE_SLOTS 16
  50406. #define DUK__PARSE_STATEMENTS_SLOTS 16
  50407. #define DUK__PARSE_EXPR_SLOTS 16
  50408. /* Temporary structure used to pass a stack allocated region through
  50409. * duk_safe_call().
  50410. */
  50411. typedef struct {
  50412. duk_small_uint_t flags;
  50413. duk_compiler_ctx comp_ctx_alloc;
  50414. duk_lexer_point lex_pt_alloc;
  50415. } duk__compiler_stkstate;
  50416. /*
  50417. * Prototypes
  50418. */
  50419. /* lexing */
  50420. DUK_LOCAL_DECL void duk__advance_helper(duk_compiler_ctx *comp_ctx, duk_small_int_t expect);
  50421. DUK_LOCAL_DECL void duk__advance_expect(duk_compiler_ctx *comp_ctx, duk_small_int_t expect);
  50422. DUK_LOCAL_DECL void duk__advance(duk_compiler_ctx *ctx);
  50423. /* function helpers */
  50424. DUK_LOCAL_DECL void duk__init_func_valstack_slots(duk_compiler_ctx *comp_ctx);
  50425. DUK_LOCAL_DECL void duk__reset_func_for_pass2(duk_compiler_ctx *comp_ctx);
  50426. DUK_LOCAL_DECL void duk__init_varmap_and_prologue_for_pass2(duk_compiler_ctx *comp_ctx, duk_reg_t *out_stmt_value_reg);
  50427. DUK_LOCAL_DECL void duk__convert_to_func_template(duk_compiler_ctx *comp_ctx, duk_bool_t force_no_namebind);
  50428. DUK_LOCAL_DECL duk_int_t duk__cleanup_varmap(duk_compiler_ctx *comp_ctx);
  50429. /* code emission */
  50430. DUK_LOCAL_DECL duk_int_t duk__get_current_pc(duk_compiler_ctx *comp_ctx);
  50431. DUK_LOCAL_DECL duk_compiler_instr *duk__get_instr_ptr(duk_compiler_ctx *comp_ctx, duk_int_t pc);
  50432. DUK_LOCAL_DECL void duk__emit(duk_compiler_ctx *comp_ctx, duk_instr_t ins);
  50433. #if 0 /* unused */
  50434. DUK_LOCAL_DECL void duk__emit_op_only(duk_compiler_ctx *comp_ctx, duk_small_uint_t op);
  50435. #endif
  50436. 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);
  50437. 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);
  50438. #if 0 /* unused */
  50439. DUK_LOCAL_DECL void duk__emit_a(duk_compiler_ctx *comp_ctx, duk_small_uint_t op_flags, duk_regconst_t a);
  50440. #endif
  50441. 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);
  50442. DUK_LOCAL_DECL void duk__emit_abc(duk_compiler_ctx *comp_ctx, duk_small_uint_t op, duk_regconst_t abc);
  50443. DUK_LOCAL_DECL void duk__emit_extraop_b_c(duk_compiler_ctx *comp_ctx, duk_small_uint_t extraop_flags, duk_regconst_t b, duk_regconst_t c);
  50444. DUK_LOCAL_DECL void duk__emit_extraop_b(duk_compiler_ctx *comp_ctx, duk_small_uint_t extraop_flags, duk_regconst_t b);
  50445. DUK_LOCAL_DECL void duk__emit_extraop_bc(duk_compiler_ctx *comp_ctx, duk_small_uint_t extraop, duk_regconst_t bc);
  50446. DUK_LOCAL_DECL void duk__emit_extraop_only(duk_compiler_ctx *comp_ctx, duk_small_uint_t extraop_flags);
  50447. DUK_LOCAL_DECL void duk__emit_load_int32(duk_compiler_ctx *comp_ctx, duk_reg_t reg, duk_int32_t val);
  50448. DUK_LOCAL_DECL void duk__emit_load_int32_noshuffle(duk_compiler_ctx *comp_ctx, duk_reg_t reg, duk_int32_t val);
  50449. DUK_LOCAL_DECL void duk__emit_jump(duk_compiler_ctx *comp_ctx, duk_int_t target_pc);
  50450. DUK_LOCAL_DECL duk_int_t duk__emit_jump_empty(duk_compiler_ctx *comp_ctx);
  50451. DUK_LOCAL_DECL void duk__insert_jump_entry(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc);
  50452. DUK_LOCAL_DECL void duk__patch_jump(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc, duk_int_t target_pc);
  50453. DUK_LOCAL_DECL void duk__patch_jump_here(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc);
  50454. 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);
  50455. DUK_LOCAL_DECL void duk__emit_if_false_skip(duk_compiler_ctx *comp_ctx, duk_regconst_t regconst);
  50456. DUK_LOCAL_DECL void duk__emit_if_true_skip(duk_compiler_ctx *comp_ctx, duk_regconst_t regconst);
  50457. DUK_LOCAL_DECL void duk__emit_invalid(duk_compiler_ctx *comp_ctx);
  50458. /* ivalue/ispec helpers */
  50459. DUK_LOCAL_DECL void duk__copy_ispec(duk_compiler_ctx *comp_ctx, duk_ispec *src, duk_ispec *dst);
  50460. DUK_LOCAL_DECL void duk__copy_ivalue(duk_compiler_ctx *comp_ctx, duk_ivalue *src, duk_ivalue *dst);
  50461. DUK_LOCAL_DECL duk_bool_t duk__is_whole_get_int32(duk_double_t x, duk_int32_t *ival);
  50462. DUK_LOCAL_DECL duk_reg_t duk__alloctemps(duk_compiler_ctx *comp_ctx, duk_small_int_t num);
  50463. DUK_LOCAL_DECL duk_reg_t duk__alloctemp(duk_compiler_ctx *comp_ctx);
  50464. DUK_LOCAL_DECL void duk__settemp_checkmax(duk_compiler_ctx *comp_ctx, duk_reg_t temp_next);
  50465. DUK_LOCAL_DECL duk_regconst_t duk__getconst(duk_compiler_ctx *comp_ctx);
  50466. DUK_LOCAL_DECL
  50467. duk_regconst_t duk__ispec_toregconst_raw(duk_compiler_ctx *comp_ctx,
  50468. duk_ispec *x,
  50469. duk_reg_t forced_reg,
  50470. duk_small_uint_t flags);
  50471. DUK_LOCAL_DECL void duk__ispec_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ispec *x, duk_reg_t forced_reg);
  50472. DUK_LOCAL_DECL void duk__ivalue_toplain_raw(duk_compiler_ctx *comp_ctx, duk_ivalue *x, duk_reg_t forced_reg);
  50473. DUK_LOCAL_DECL void duk__ivalue_toplain(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  50474. DUK_LOCAL_DECL void duk__ivalue_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  50475. DUK_LOCAL_DECL
  50476. duk_regconst_t duk__ivalue_toregconst_raw(duk_compiler_ctx *comp_ctx,
  50477. duk_ivalue *x,
  50478. duk_reg_t forced_reg,
  50479. duk_small_uint_t flags);
  50480. DUK_LOCAL_DECL duk_reg_t duk__ivalue_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  50481. #if 0 /* unused */
  50482. DUK_LOCAL_DECL duk_reg_t duk__ivalue_totemp(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  50483. #endif
  50484. DUK_LOCAL_DECL void duk__ivalue_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ivalue *x, duk_int_t forced_reg);
  50485. DUK_LOCAL_DECL duk_regconst_t duk__ivalue_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  50486. DUK_LOCAL_DECL duk_regconst_t duk__ivalue_totempconst(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  50487. /* identifier handling */
  50488. DUK_LOCAL_DECL duk_reg_t duk__lookup_active_register_binding(duk_compiler_ctx *comp_ctx);
  50489. DUK_LOCAL_DECL duk_bool_t duk__lookup_lhs(duk_compiler_ctx *ctx, duk_reg_t *out_reg_varbind, duk_regconst_t *out_rc_varname);
  50490. /* label handling */
  50491. 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);
  50492. DUK_LOCAL_DECL void duk__update_label_flags(duk_compiler_ctx *comp_ctx, duk_int_t label_id, duk_small_uint_t flags);
  50493. 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);
  50494. DUK_LOCAL_DECL void duk__reset_labels_to_length(duk_compiler_ctx *comp_ctx, duk_int_t len);
  50495. /* top-down expression parser */
  50496. DUK_LOCAL_DECL void duk__expr_nud(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  50497. DUK_LOCAL_DECL void duk__expr_led(duk_compiler_ctx *comp_ctx, duk_ivalue *left, duk_ivalue *res);
  50498. DUK_LOCAL_DECL duk_small_uint_t duk__expr_lbp(duk_compiler_ctx *comp_ctx);
  50499. DUK_LOCAL_DECL duk_bool_t duk__expr_is_empty(duk_compiler_ctx *comp_ctx);
  50500. /* exprtop is the top level variant which resets nud/led counts */
  50501. DUK_LOCAL_DECL void duk__expr(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  50502. DUK_LOCAL_DECL void duk__exprtop(duk_compiler_ctx *ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  50503. /* convenience helpers */
  50504. #if 0 /* unused */
  50505. DUK_LOCAL_DECL duk_reg_t duk__expr_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  50506. #endif
  50507. #if 0 /* unused */
  50508. DUK_LOCAL_DECL duk_reg_t duk__expr_totemp(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  50509. #endif
  50510. DUK_LOCAL_DECL void duk__expr_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags, duk_reg_t forced_reg);
  50511. DUK_LOCAL_DECL duk_regconst_t duk__expr_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  50512. #if 0 /* unused */
  50513. DUK_LOCAL_DECL duk_regconst_t duk__expr_totempconst(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  50514. #endif
  50515. DUK_LOCAL_DECL void duk__expr_toplain(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  50516. DUK_LOCAL_DECL void duk__expr_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  50517. DUK_LOCAL_DECL duk_reg_t duk__exprtop_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  50518. #if 0 /* unused */
  50519. DUK_LOCAL_DECL duk_reg_t duk__exprtop_totemp(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  50520. #endif
  50521. DUK_LOCAL_DECL void duk__exprtop_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags, duk_reg_t forced_reg);
  50522. DUK_LOCAL_DECL duk_regconst_t duk__exprtop_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  50523. #if 0 /* unused */
  50524. DUK_LOCAL_DECL void duk__exprtop_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  50525. #endif
  50526. /* expression parsing helpers */
  50527. DUK_LOCAL_DECL duk_int_t duk__parse_arguments(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  50528. DUK_LOCAL_DECL void duk__nud_array_literal(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  50529. DUK_LOCAL_DECL void duk__nud_object_literal(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  50530. DUK_LOCAL_DECL duk_bool_t duk__nud_object_literal_key_check(duk_compiler_ctx *comp_ctx, duk_small_uint_t new_key_flags);
  50531. /* statement parsing */
  50532. DUK_LOCAL_DECL void duk__parse_var_decl(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t expr_flags, duk_reg_t *out_reg_varbind, duk_regconst_t *out_rc_varname);
  50533. DUK_LOCAL_DECL void duk__parse_var_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t expr_flags);
  50534. DUK_LOCAL_DECL void duk__parse_for_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site);
  50535. DUK_LOCAL_DECL void duk__parse_switch_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site);
  50536. DUK_LOCAL_DECL void duk__parse_if_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  50537. DUK_LOCAL_DECL void duk__parse_do_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site);
  50538. DUK_LOCAL_DECL void duk__parse_while_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site);
  50539. DUK_LOCAL_DECL void duk__parse_break_or_continue_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  50540. DUK_LOCAL_DECL void duk__parse_return_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  50541. DUK_LOCAL_DECL void duk__parse_throw_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  50542. DUK_LOCAL_DECL void duk__parse_try_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  50543. DUK_LOCAL_DECL void duk__parse_with_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  50544. DUK_LOCAL_DECL void duk__parse_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_bool_t allow_source_elem);
  50545. DUK_LOCAL_DECL duk_int_t duk__stmt_label_site(duk_compiler_ctx *comp_ctx, duk_int_t label_id);
  50546. DUK_LOCAL_DECL void duk__parse_stmts(duk_compiler_ctx *comp_ctx, duk_bool_t allow_source_elem, duk_bool_t expect_eof);
  50547. 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_small_int_t expect_token);
  50548. DUK_LOCAL_DECL void duk__parse_func_formals(duk_compiler_ctx *comp_ctx);
  50549. DUK_LOCAL_DECL void duk__parse_func_like_raw(duk_compiler_ctx *comp_ctx, duk_bool_t is_decl, duk_bool_t is_setget);
  50550. DUK_LOCAL_DECL duk_int_t duk__parse_func_like_fnum(duk_compiler_ctx *comp_ctx, duk_bool_t is_decl, duk_bool_t is_setget);
  50551. /*
  50552. * Parser control values for tokens. The token table is ordered by the
  50553. * DUK_TOK_XXX defines.
  50554. *
  50555. * The binding powers are for lbp() use (i.e. for use in led() context).
  50556. * Binding powers are positive for typing convenience, and bits at the
  50557. * top should be reserved for flags. Binding power step must be higher
  50558. * than 1 so that binding power "lbp - 1" can be used for right associative
  50559. * operators. Currently a step of 2 is used (which frees one more bit for
  50560. * flags).
  50561. */
  50562. /* XXX: actually single step levels would work just fine, clean up */
  50563. /* binding power "levels" (see doc/compiler.rst) */
  50564. #define DUK__BP_INVALID 0 /* always terminates led() */
  50565. #define DUK__BP_EOF 2
  50566. #define DUK__BP_CLOSING 4 /* token closes expression, e.g. ')', ']' */
  50567. #define DUK__BP_FOR_EXPR DUK__BP_CLOSING /* bp to use when parsing a top level Expression */
  50568. #define DUK__BP_COMMA 6
  50569. #define DUK__BP_ASSIGNMENT 8
  50570. #define DUK__BP_CONDITIONAL 10
  50571. #define DUK__BP_LOR 12
  50572. #define DUK__BP_LAND 14
  50573. #define DUK__BP_BOR 16
  50574. #define DUK__BP_BXOR 18
  50575. #define DUK__BP_BAND 20
  50576. #define DUK__BP_EQUALITY 22
  50577. #define DUK__BP_RELATIONAL 24
  50578. #define DUK__BP_SHIFT 26
  50579. #define DUK__BP_ADDITIVE 28
  50580. #define DUK__BP_MULTIPLICATIVE 30
  50581. #define DUK__BP_POSTFIX 32
  50582. #define DUK__BP_CALL 34
  50583. #define DUK__BP_MEMBER 36
  50584. #define DUK__TOKEN_LBP_BP_MASK 0x1f
  50585. #define DUK__TOKEN_LBP_FLAG_NO_REGEXP (1 << 5) /* regexp literal must not follow this token */
  50586. #define DUK__TOKEN_LBP_FLAG_TERMINATES (1 << 6) /* terminates expression; e.g. post-increment/-decrement */
  50587. #define DUK__TOKEN_LBP_FLAG_UNUSED (1 << 7) /* spare */
  50588. #define DUK__TOKEN_LBP_GET_BP(x) ((duk_small_uint_t) (((x) & DUK__TOKEN_LBP_BP_MASK) * 2))
  50589. #define DUK__MK_LBP(bp) ((bp) >> 1) /* bp is assumed to be even */
  50590. #define DUK__MK_LBP_FLAGS(bp,flags) (((bp) >> 1) | (flags))
  50591. DUK_LOCAL const duk_uint8_t duk__token_lbp[] = {
  50592. DUK__MK_LBP(DUK__BP_EOF), /* DUK_TOK_EOF */
  50593. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_IDENTIFIER */
  50594. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_BREAK */
  50595. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_CASE */
  50596. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_CATCH */
  50597. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_CONTINUE */
  50598. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_DEBUGGER */
  50599. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_DEFAULT */
  50600. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_DELETE */
  50601. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_DO */
  50602. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_ELSE */
  50603. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_FINALLY */
  50604. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_FOR */
  50605. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_FUNCTION */
  50606. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_IF */
  50607. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_IN */
  50608. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_INSTANCEOF */
  50609. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_NEW */
  50610. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_RETURN */
  50611. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_SWITCH */
  50612. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_THIS */
  50613. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_THROW */
  50614. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_TRY */
  50615. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_TYPEOF */
  50616. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_VAR */
  50617. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_CONST */
  50618. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_VOID */
  50619. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_WHILE */
  50620. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_WITH */
  50621. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_CLASS */
  50622. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_ENUM */
  50623. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_EXPORT */
  50624. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_EXTENDS */
  50625. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_IMPORT */
  50626. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_SUPER */
  50627. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_NULL */
  50628. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_TRUE */
  50629. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_FALSE */
  50630. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_GET */
  50631. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_SET */
  50632. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_IMPLEMENTS */
  50633. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_INTERFACE */
  50634. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_LET */
  50635. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_PACKAGE */
  50636. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_PRIVATE */
  50637. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_PROTECTED */
  50638. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_PUBLIC */
  50639. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_STATIC */
  50640. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_YIELD */
  50641. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_LCURLY */
  50642. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_RCURLY */
  50643. DUK__MK_LBP(DUK__BP_MEMBER), /* DUK_TOK_LBRACKET */
  50644. DUK__MK_LBP_FLAGS(DUK__BP_CLOSING, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_RBRACKET */
  50645. DUK__MK_LBP(DUK__BP_CALL), /* DUK_TOK_LPAREN */
  50646. DUK__MK_LBP_FLAGS(DUK__BP_CLOSING, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_RPAREN */
  50647. DUK__MK_LBP(DUK__BP_MEMBER), /* DUK_TOK_PERIOD */
  50648. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_SEMICOLON */
  50649. DUK__MK_LBP(DUK__BP_COMMA), /* DUK_TOK_COMMA */
  50650. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_LT */
  50651. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_GT */
  50652. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_LE */
  50653. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_GE */
  50654. DUK__MK_LBP(DUK__BP_EQUALITY), /* DUK_TOK_EQ */
  50655. DUK__MK_LBP(DUK__BP_EQUALITY), /* DUK_TOK_NEQ */
  50656. DUK__MK_LBP(DUK__BP_EQUALITY), /* DUK_TOK_SEQ */
  50657. DUK__MK_LBP(DUK__BP_EQUALITY), /* DUK_TOK_SNEQ */
  50658. DUK__MK_LBP(DUK__BP_ADDITIVE), /* DUK_TOK_ADD */
  50659. DUK__MK_LBP(DUK__BP_ADDITIVE), /* DUK_TOK_SUB */
  50660. DUK__MK_LBP(DUK__BP_MULTIPLICATIVE), /* DUK_TOK_MUL */
  50661. DUK__MK_LBP(DUK__BP_MULTIPLICATIVE), /* DUK_TOK_DIV */
  50662. DUK__MK_LBP(DUK__BP_MULTIPLICATIVE), /* DUK_TOK_MOD */
  50663. DUK__MK_LBP(DUK__BP_POSTFIX), /* DUK_TOK_INCREMENT */
  50664. DUK__MK_LBP(DUK__BP_POSTFIX), /* DUK_TOK_DECREMENT */
  50665. DUK__MK_LBP(DUK__BP_SHIFT), /* DUK_TOK_ALSHIFT */
  50666. DUK__MK_LBP(DUK__BP_SHIFT), /* DUK_TOK_ARSHIFT */
  50667. DUK__MK_LBP(DUK__BP_SHIFT), /* DUK_TOK_RSHIFT */
  50668. DUK__MK_LBP(DUK__BP_BAND), /* DUK_TOK_BAND */
  50669. DUK__MK_LBP(DUK__BP_BOR), /* DUK_TOK_BOR */
  50670. DUK__MK_LBP(DUK__BP_BXOR), /* DUK_TOK_BXOR */
  50671. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_LNOT */
  50672. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_BNOT */
  50673. DUK__MK_LBP(DUK__BP_LAND), /* DUK_TOK_LAND */
  50674. DUK__MK_LBP(DUK__BP_LOR), /* DUK_TOK_LOR */
  50675. DUK__MK_LBP(DUK__BP_CONDITIONAL), /* DUK_TOK_QUESTION */
  50676. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_COLON */
  50677. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_EQUALSIGN */
  50678. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_ADD_EQ */
  50679. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_SUB_EQ */
  50680. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_MUL_EQ */
  50681. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_DIV_EQ */
  50682. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_MOD_EQ */
  50683. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_ALSHIFT_EQ */
  50684. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_ARSHIFT_EQ */
  50685. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_RSHIFT_EQ */
  50686. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_BAND_EQ */
  50687. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_BOR_EQ */
  50688. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_BXOR_EQ */
  50689. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_NUMBER */
  50690. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_STRING */
  50691. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_REGEXP */
  50692. };
  50693. /*
  50694. * Misc helpers
  50695. */
  50696. DUK_LOCAL void duk__recursion_increase(duk_compiler_ctx *comp_ctx) {
  50697. DUK_ASSERT(comp_ctx != NULL);
  50698. DUK_ASSERT(comp_ctx->recursion_depth >= 0);
  50699. if (comp_ctx->recursion_depth >= comp_ctx->recursion_limit) {
  50700. DUK_ERROR(comp_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_COMPILER_RECURSION_LIMIT);
  50701. }
  50702. comp_ctx->recursion_depth++;
  50703. }
  50704. DUK_LOCAL void duk__recursion_decrease(duk_compiler_ctx *comp_ctx) {
  50705. DUK_ASSERT(comp_ctx != NULL);
  50706. DUK_ASSERT(comp_ctx->recursion_depth > 0);
  50707. comp_ctx->recursion_depth--;
  50708. }
  50709. DUK_LOCAL duk_bool_t duk__hstring_is_eval_or_arguments(duk_compiler_ctx *comp_ctx, duk_hstring *h) {
  50710. DUK_UNREF(comp_ctx);
  50711. DUK_ASSERT(h != NULL);
  50712. return DUK_HSTRING_HAS_EVAL_OR_ARGUMENTS(h);
  50713. }
  50714. DUK_LOCAL duk_bool_t duk__hstring_is_eval_or_arguments_in_strict_mode(duk_compiler_ctx *comp_ctx, duk_hstring *h) {
  50715. DUK_ASSERT(h != NULL);
  50716. return (comp_ctx->curr_func.is_strict &&
  50717. DUK_HSTRING_HAS_EVAL_OR_ARGUMENTS(h));
  50718. }
  50719. /*
  50720. * Parser duk__advance() token eating functions
  50721. */
  50722. /* XXX: valstack handling is awkward. Add a valstack helper which
  50723. * avoids dup():ing; valstack_copy(src, dst)?
  50724. */
  50725. DUK_LOCAL void duk__advance_helper(duk_compiler_ctx *comp_ctx, duk_small_int_t expect) {
  50726. duk_hthread *thr = comp_ctx->thr;
  50727. duk_context *ctx = (duk_context *) thr;
  50728. duk_bool_t regexp;
  50729. DUK_ASSERT(comp_ctx->curr_token.t >= 0 && comp_ctx->curr_token.t <= DUK_TOK_MAXVAL); /* MAXVAL is inclusive */
  50730. /*
  50731. * Use current token to decide whether a RegExp can follow.
  50732. *
  50733. * We can use either 't' or 't_nores'; the latter would not
  50734. * recognize keywords. Some keywords can be followed by a
  50735. * RegExp (e.g. "return"), so using 't' is better. This is
  50736. * not trivial, see doc/compiler.rst.
  50737. */
  50738. regexp = 1;
  50739. if (duk__token_lbp[comp_ctx->curr_token.t] & DUK__TOKEN_LBP_FLAG_NO_REGEXP) {
  50740. regexp = 0;
  50741. }
  50742. if (comp_ctx->curr_func.reject_regexp_in_adv) {
  50743. comp_ctx->curr_func.reject_regexp_in_adv = 0;
  50744. regexp = 0;
  50745. }
  50746. if (expect >= 0 && comp_ctx->curr_token.t != expect) {
  50747. DUK_D(DUK_DPRINT("parse error: expect=%ld, got=%ld",
  50748. (long) expect, (long) comp_ctx->curr_token.t));
  50749. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_PARSE_ERROR);
  50750. }
  50751. /* make current token the previous; need to fiddle with valstack "backing store" */
  50752. DUK_MEMCPY(&comp_ctx->prev_token, &comp_ctx->curr_token, sizeof(duk_token));
  50753. duk_copy(ctx, comp_ctx->tok11_idx, comp_ctx->tok21_idx);
  50754. duk_copy(ctx, comp_ctx->tok12_idx, comp_ctx->tok22_idx);
  50755. /* parse new token */
  50756. duk_lexer_parse_js_input_element(&comp_ctx->lex,
  50757. &comp_ctx->curr_token,
  50758. comp_ctx->curr_func.is_strict,
  50759. regexp);
  50760. DUK_DDD(DUK_DDDPRINT("advance: curr: tok=%ld/%ld,%ld,term=%ld,%!T,%!T "
  50761. "prev: tok=%ld/%ld,%ld,term=%ld,%!T,%!T",
  50762. (long) comp_ctx->curr_token.t,
  50763. (long) comp_ctx->curr_token.t_nores,
  50764. (long) comp_ctx->curr_token.start_line,
  50765. (long) comp_ctx->curr_token.lineterm,
  50766. (duk_tval *) duk_get_tval(ctx, comp_ctx->tok11_idx),
  50767. (duk_tval *) duk_get_tval(ctx, comp_ctx->tok12_idx),
  50768. (long) comp_ctx->prev_token.t,
  50769. (long) comp_ctx->prev_token.t_nores,
  50770. (long) comp_ctx->prev_token.start_line,
  50771. (long) comp_ctx->prev_token.lineterm,
  50772. (duk_tval *) duk_get_tval(ctx, comp_ctx->tok21_idx),
  50773. (duk_tval *) duk_get_tval(ctx, comp_ctx->tok22_idx)));
  50774. }
  50775. /* advance, expecting current token to be a specific token; parse next token in regexp context */
  50776. DUK_LOCAL void duk__advance_expect(duk_compiler_ctx *comp_ctx, duk_small_int_t expect) {
  50777. duk__advance_helper(comp_ctx, expect);
  50778. }
  50779. /* advance, whatever the current token is; parse next token in regexp context */
  50780. DUK_LOCAL void duk__advance(duk_compiler_ctx *comp_ctx) {
  50781. duk__advance_helper(comp_ctx, -1);
  50782. }
  50783. /*
  50784. * Helpers for duk_compiler_func.
  50785. */
  50786. /* init function state: inits valstack allocations */
  50787. DUK_LOCAL void duk__init_func_valstack_slots(duk_compiler_ctx *comp_ctx) {
  50788. duk_compiler_func *func = &comp_ctx->curr_func;
  50789. duk_hthread *thr = comp_ctx->thr;
  50790. duk_context *ctx = (duk_context *) thr;
  50791. duk_idx_t entry_top;
  50792. entry_top = duk_get_top(ctx);
  50793. DUK_MEMZERO(func, sizeof(*func)); /* intentional overlap with earlier memzero */
  50794. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  50795. func->h_name = NULL;
  50796. func->h_consts = NULL;
  50797. func->h_funcs = NULL;
  50798. func->h_decls = NULL;
  50799. func->h_labelnames = NULL;
  50800. func->h_labelinfos = NULL;
  50801. func->h_argnames = NULL;
  50802. func->h_varmap = NULL;
  50803. #endif
  50804. duk_require_stack(ctx, DUK__FUNCTION_INIT_REQUIRE_SLOTS);
  50805. DUK_BW_INIT_PUSHBUF(thr, &func->bw_code, DUK__BC_INITIAL_INSTS * sizeof(duk_compiler_instr));
  50806. /* code_idx = entry_top + 0 */
  50807. duk_push_array(ctx);
  50808. func->consts_idx = entry_top + 1;
  50809. func->h_consts = duk_get_hobject(ctx, entry_top + 1);
  50810. DUK_ASSERT(func->h_consts != NULL);
  50811. duk_push_array(ctx);
  50812. func->funcs_idx = entry_top + 2;
  50813. func->h_funcs = duk_get_hobject(ctx, entry_top + 2);
  50814. DUK_ASSERT(func->h_funcs != NULL);
  50815. DUK_ASSERT(func->fnum_next == 0);
  50816. duk_push_array(ctx);
  50817. func->decls_idx = entry_top + 3;
  50818. func->h_decls = duk_get_hobject(ctx, entry_top + 3);
  50819. DUK_ASSERT(func->h_decls != NULL);
  50820. duk_push_array(ctx);
  50821. func->labelnames_idx = entry_top + 4;
  50822. func->h_labelnames = duk_get_hobject(ctx, entry_top + 4);
  50823. DUK_ASSERT(func->h_labelnames != NULL);
  50824. duk_push_dynamic_buffer(ctx, 0);
  50825. func->labelinfos_idx = entry_top + 5;
  50826. func->h_labelinfos = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, entry_top + 5);
  50827. DUK_ASSERT(func->h_labelinfos != NULL);
  50828. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(func->h_labelinfos) && !DUK_HBUFFER_HAS_EXTERNAL(func->h_labelinfos));
  50829. duk_push_array(ctx);
  50830. func->argnames_idx = entry_top + 6;
  50831. func->h_argnames = duk_get_hobject(ctx, entry_top + 6);
  50832. DUK_ASSERT(func->h_argnames != NULL);
  50833. duk_push_object_internal(ctx);
  50834. func->varmap_idx = entry_top + 7;
  50835. func->h_varmap = duk_get_hobject(ctx, entry_top + 7);
  50836. DUK_ASSERT(func->h_varmap != NULL);
  50837. }
  50838. /* reset function state (prepare for pass 2) */
  50839. DUK_LOCAL void duk__reset_func_for_pass2(duk_compiler_ctx *comp_ctx) {
  50840. duk_compiler_func *func = &comp_ctx->curr_func;
  50841. duk_hthread *thr = comp_ctx->thr;
  50842. duk_context *ctx = (duk_context *) thr;
  50843. /* reset bytecode buffer but keep current size; pass 2 will
  50844. * require same amount or more.
  50845. */
  50846. DUK_BW_RESET_SIZE(thr, &func->bw_code);
  50847. duk_hobject_set_length_zero(thr, func->h_consts);
  50848. /* keep func->h_funcs; inner functions are not reparsed to avoid O(depth^2) parsing */
  50849. func->fnum_next = 0;
  50850. /* duk_hobject_set_length_zero(thr, func->h_funcs); */
  50851. duk_hobject_set_length_zero(thr, func->h_labelnames);
  50852. duk_hbuffer_reset(thr, func->h_labelinfos);
  50853. /* keep func->h_argnames; it is fixed for all passes */
  50854. /* truncated in case pass 3 needed */
  50855. duk_push_object_internal(ctx);
  50856. duk_replace(ctx, func->varmap_idx);
  50857. func->h_varmap = duk_get_hobject(ctx, func->varmap_idx);
  50858. DUK_ASSERT(func->h_varmap != NULL);
  50859. }
  50860. /* cleanup varmap from any null entries, compact it, etc; returns number
  50861. * of final entries after cleanup.
  50862. */
  50863. DUK_LOCAL duk_int_t duk__cleanup_varmap(duk_compiler_ctx *comp_ctx) {
  50864. duk_hthread *thr = comp_ctx->thr;
  50865. duk_context *ctx = (duk_context *) thr;
  50866. duk_hobject *h_varmap;
  50867. duk_hstring *h_key;
  50868. duk_tval *tv;
  50869. duk_uint32_t i, e_next;
  50870. duk_int_t ret;
  50871. /* [ ... varmap ] */
  50872. h_varmap = duk_get_hobject(ctx, -1);
  50873. DUK_ASSERT(h_varmap != NULL);
  50874. ret = 0;
  50875. e_next = DUK_HOBJECT_GET_ENEXT(h_varmap);
  50876. for (i = 0; i < e_next; i++) {
  50877. h_key = DUK_HOBJECT_E_GET_KEY(thr->heap, h_varmap, i);
  50878. if (!h_key) {
  50879. continue;
  50880. }
  50881. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, h_varmap, i));
  50882. /* The entries can either be register numbers or 'null' values.
  50883. * Thus, no need to DECREF them and get side effects. DECREF'ing
  50884. * the keys (strings) can cause memory to be freed but no side
  50885. * effects as strings don't have finalizers. This is why we can
  50886. * rely on the object properties not changing from underneath us.
  50887. */
  50888. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, h_varmap, i);
  50889. if (!DUK_TVAL_IS_NUMBER(tv)) {
  50890. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv));
  50891. DUK_HOBJECT_E_SET_KEY(thr->heap, h_varmap, i, NULL);
  50892. DUK_HSTRING_DECREF(thr, h_key);
  50893. /* when key is NULL, value is garbage so no need to set */
  50894. } else {
  50895. ret++;
  50896. }
  50897. }
  50898. duk_compact(ctx, -1);
  50899. return ret;
  50900. }
  50901. /* convert duk_compiler_func into a function template, leaving the result
  50902. * on top of stack.
  50903. */
  50904. /* XXX: awkward and bloated asm -- use faster internal accesses */
  50905. DUK_LOCAL void duk__convert_to_func_template(duk_compiler_ctx *comp_ctx, duk_bool_t force_no_namebind) {
  50906. duk_compiler_func *func = &comp_ctx->curr_func;
  50907. duk_hthread *thr = comp_ctx->thr;
  50908. duk_context *ctx = (duk_context *) thr;
  50909. duk_hcompiledfunction *h_res;
  50910. duk_hbuffer_fixed *h_data;
  50911. duk_size_t consts_count;
  50912. duk_size_t funcs_count;
  50913. duk_size_t code_count;
  50914. duk_size_t code_size;
  50915. duk_size_t data_size;
  50916. duk_size_t i;
  50917. duk_tval *p_const;
  50918. duk_hobject **p_func;
  50919. duk_instr_t *p_instr;
  50920. duk_compiler_instr *q_instr;
  50921. duk_tval *tv;
  50922. DUK_DDD(DUK_DDDPRINT("converting duk_compiler_func to function/template"));
  50923. /*
  50924. * Push result object and init its flags
  50925. */
  50926. /* Valstack should suffice here, required on function valstack init */
  50927. (void) duk_push_compiledfunction(ctx);
  50928. h_res = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1); /* XXX: specific getter */
  50929. DUK_ASSERT(h_res != NULL);
  50930. if (func->is_function) {
  50931. DUK_DDD(DUK_DDDPRINT("function -> set NEWENV"));
  50932. DUK_HOBJECT_SET_NEWENV((duk_hobject *) h_res);
  50933. if (!func->is_arguments_shadowed) {
  50934. /* arguments object would be accessible; note that shadowing
  50935. * bindings are arguments or function declarations, neither
  50936. * of which are deletable, so this is safe.
  50937. */
  50938. if (func->id_access_arguments || func->may_direct_eval) {
  50939. DUK_DDD(DUK_DDDPRINT("function may access 'arguments' object directly or "
  50940. "indirectly -> set CREATEARGS"));
  50941. DUK_HOBJECT_SET_CREATEARGS((duk_hobject *) h_res);
  50942. }
  50943. }
  50944. } else if (func->is_eval && func->is_strict) {
  50945. DUK_DDD(DUK_DDDPRINT("strict eval code -> set NEWENV"));
  50946. DUK_HOBJECT_SET_NEWENV((duk_hobject *) h_res);
  50947. } else {
  50948. /* non-strict eval: env is caller's env or global env (direct vs. indirect call)
  50949. * global code: env is is global env
  50950. */
  50951. DUK_DDD(DUK_DDDPRINT("non-strict eval code or global code -> no NEWENV"));
  50952. DUK_ASSERT(!DUK_HOBJECT_HAS_NEWENV((duk_hobject *) h_res));
  50953. }
  50954. if (func->is_function && !func->is_decl && func->h_name != NULL && !force_no_namebind) {
  50955. /* Object literal set/get functions have a name (property
  50956. * name) but must not have a lexical name binding, see
  50957. * test-bug-getset-func-name.js.
  50958. */
  50959. DUK_DDD(DUK_DDDPRINT("function expression with a name -> set NAMEBINDING"));
  50960. DUK_HOBJECT_SET_NAMEBINDING((duk_hobject *) h_res);
  50961. }
  50962. if (func->is_strict) {
  50963. DUK_DDD(DUK_DDDPRINT("function is strict -> set STRICT"));
  50964. DUK_HOBJECT_SET_STRICT((duk_hobject *) h_res);
  50965. }
  50966. if (func->is_notail) {
  50967. DUK_DDD(DUK_DDDPRINT("function is notail -> set NOTAIL"));
  50968. DUK_HOBJECT_SET_NOTAIL((duk_hobject *) h_res);
  50969. }
  50970. /*
  50971. * Build function fixed size 'data' buffer, which contains bytecode,
  50972. * constants, and inner function references.
  50973. *
  50974. * During the building phase 'data' is reachable but incomplete.
  50975. * Only incref's occur during building (no refzero or GC happens),
  50976. * so the building process is atomic.
  50977. */
  50978. consts_count = duk_hobject_get_length(thr, func->h_consts);
  50979. funcs_count = duk_hobject_get_length(thr, func->h_funcs) / 3;
  50980. code_count = DUK_BW_GET_SIZE(thr, &func->bw_code) / sizeof(duk_compiler_instr);
  50981. code_size = code_count * sizeof(duk_instr_t);
  50982. data_size = consts_count * sizeof(duk_tval) +
  50983. funcs_count * sizeof(duk_hobject *) +
  50984. code_size;
  50985. DUK_DDD(DUK_DDDPRINT("consts_count=%ld, funcs_count=%ld, code_size=%ld -> "
  50986. "data_size=%ld*%ld + %ld*%ld + %ld = %ld",
  50987. (long) consts_count, (long) funcs_count, (long) code_size,
  50988. (long) consts_count, (long) sizeof(duk_tval),
  50989. (long) funcs_count, (long) sizeof(duk_hobject *),
  50990. (long) code_size, (long) data_size));
  50991. duk_push_fixed_buffer(ctx, data_size);
  50992. h_data = (duk_hbuffer_fixed *) duk_get_hbuffer(ctx, -1);
  50993. DUK_ASSERT(h_data != NULL);
  50994. DUK_HCOMPILEDFUNCTION_SET_DATA(thr->heap, h_res, (duk_hbuffer *) h_data);
  50995. DUK_HEAPHDR_INCREF(thr, h_data);
  50996. p_const = (duk_tval *) (void *) DUK_HBUFFER_FIXED_GET_DATA_PTR(thr->heap, h_data);
  50997. for (i = 0; i < consts_count; i++) {
  50998. DUK_ASSERT(i <= DUK_UARRIDX_MAX); /* const limits */
  50999. tv = duk_hobject_find_existing_array_entry_tval_ptr(thr->heap, func->h_consts, (duk_uarridx_t) i);
  51000. DUK_ASSERT(tv != NULL);
  51001. DUK_TVAL_SET_TVAL(p_const, tv);
  51002. p_const++;
  51003. DUK_TVAL_INCREF(thr, tv); /* may be a string constant */
  51004. DUK_DDD(DUK_DDDPRINT("constant: %!T", (duk_tval *) tv));
  51005. }
  51006. p_func = (duk_hobject **) p_const;
  51007. DUK_HCOMPILEDFUNCTION_SET_FUNCS(thr->heap, h_res, p_func);
  51008. for (i = 0; i < funcs_count; i++) {
  51009. duk_hobject *h;
  51010. DUK_ASSERT(i * 3 <= DUK_UARRIDX_MAX); /* func limits */
  51011. tv = duk_hobject_find_existing_array_entry_tval_ptr(thr->heap, func->h_funcs, (duk_uarridx_t) (i * 3));
  51012. DUK_ASSERT(tv != NULL);
  51013. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  51014. h = DUK_TVAL_GET_OBJECT(tv);
  51015. DUK_ASSERT(h != NULL);
  51016. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(h));
  51017. *p_func++ = h;
  51018. DUK_HOBJECT_INCREF(thr, h);
  51019. DUK_DDD(DUK_DDDPRINT("inner function: %p -> %!iO",
  51020. (void *) h, (duk_heaphdr *) h));
  51021. }
  51022. p_instr = (duk_instr_t *) p_func;
  51023. DUK_HCOMPILEDFUNCTION_SET_BYTECODE(thr->heap, h_res, p_instr);
  51024. /* copy bytecode instructions one at a time */
  51025. q_instr = (duk_compiler_instr *) (void *) DUK_BW_GET_BASEPTR(thr, &func->bw_code);
  51026. for (i = 0; i < code_count; i++) {
  51027. p_instr[i] = q_instr[i].ins;
  51028. }
  51029. /* Note: 'q_instr' is still used below */
  51030. DUK_ASSERT((duk_uint8_t *) (p_instr + code_count) == DUK_HBUFFER_FIXED_GET_DATA_PTR(thr->heap, h_data) + data_size);
  51031. duk_pop(ctx); /* 'data' (and everything in it) is reachable through h_res now */
  51032. /*
  51033. * Init object properties
  51034. *
  51035. * Properties should be added in decreasing order of access frequency.
  51036. * (Not very critical for function templates.)
  51037. */
  51038. DUK_DDD(DUK_DDDPRINT("init function properties"));
  51039. /* [ ... res ] */
  51040. /* _Varmap: omitted if function is guaranteed not to do slow path identifier
  51041. * accesses or if it would turn out to be empty of actual register mappings
  51042. * after a cleanup. When debugging is enabled, we always need the varmap to
  51043. * be able to lookup variables at any point.
  51044. */
  51045. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  51046. if (1) {
  51047. #else
  51048. if (func->id_access_slow || /* directly uses slow accesses */
  51049. func->may_direct_eval || /* may indirectly slow access through a direct eval */
  51050. funcs_count > 0) { /* has inner functions which may slow access (XXX: this can be optimized by looking at the inner functions) */
  51051. #endif
  51052. duk_int_t num_used;
  51053. duk_dup(ctx, func->varmap_idx);
  51054. num_used = duk__cleanup_varmap(comp_ctx);
  51055. DUK_DDD(DUK_DDDPRINT("cleaned up varmap: %!T (num_used=%ld)",
  51056. (duk_tval *) duk_get_tval(ctx, -1), (long) num_used));
  51057. if (num_used > 0) {
  51058. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VARMAP, DUK_PROPDESC_FLAGS_NONE);
  51059. } else {
  51060. DUK_DDD(DUK_DDDPRINT("varmap is empty after cleanup -> no need to add"));
  51061. duk_pop(ctx);
  51062. }
  51063. }
  51064. /* _Formals: omitted if function is guaranteed not to need a (non-strict) arguments object */
  51065. if (1) {
  51066. /* XXX: Add a proper condition. If formals list is omitted, recheck
  51067. * handling for 'length' in duk_js_push_closure(); it currently relies
  51068. * on _Formals being set. Removal may need to be conditional to debugging
  51069. * being enabled/disabled too.
  51070. */
  51071. duk_dup(ctx, func->argnames_idx);
  51072. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_FORMALS, DUK_PROPDESC_FLAGS_NONE);
  51073. }
  51074. /* name */
  51075. if (func->h_name) {
  51076. duk_push_hstring(ctx, func->h_name);
  51077. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE);
  51078. }
  51079. /* _Source */
  51080. #if defined(DUK_USE_NONSTD_FUNC_SOURCE_PROPERTY)
  51081. if (0) {
  51082. /* XXX: Currently function source code is not stored, as it is not
  51083. * required by the standard. Source code should not be stored by
  51084. * default (user should enable it explicitly), and the source should
  51085. * probably be compressed with a trivial text compressor; average
  51086. * compression of 20-30% is quite easy to achieve even with a trivial
  51087. * compressor (RLE + backwards lookup).
  51088. *
  51089. * Debugging needs source code to be useful: sometimes input code is
  51090. * not found in files as it may be generated and then eval()'d, given
  51091. * by dynamic C code, etc.
  51092. *
  51093. * Other issues:
  51094. *
  51095. * - Need tokenizer indices for start and end to substring
  51096. * - Always normalize function declaration part?
  51097. * - If we keep _Formals, only need to store body
  51098. */
  51099. /*
  51100. * For global or eval code this is straightforward. For functions
  51101. * created with the Function constructor we only get the source for
  51102. * the body and must manufacture the "function ..." part.
  51103. *
  51104. * For instance, for constructed functions (v8):
  51105. *
  51106. * > a = new Function("foo", "bar", "print(foo)");
  51107. * [Function]
  51108. * > a.toString()
  51109. * 'function anonymous(foo,bar) {\nprint(foo)\n}'
  51110. *
  51111. * Similarly for e.g. getters (v8):
  51112. *
  51113. * > x = { get a(foo,bar) { print(foo); } }
  51114. * { a: [Getter] }
  51115. * > Object.getOwnPropertyDescriptor(x, 'a').get.toString()
  51116. * 'function a(foo,bar) { print(foo); }'
  51117. */
  51118. #if 0
  51119. duk_push_string(ctx, "XXX");
  51120. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_SOURCE, DUK_PROPDESC_FLAGS_NONE);
  51121. #endif
  51122. }
  51123. #endif /* DUK_USE_NONSTD_FUNC_SOURCE_PROPERTY */
  51124. /* _Pc2line */
  51125. #if defined(DUK_USE_PC2LINE)
  51126. if (1) {
  51127. /*
  51128. * Size-optimized pc->line mapping.
  51129. */
  51130. DUK_ASSERT(code_count <= DUK_COMPILER_MAX_BYTECODE_LENGTH);
  51131. duk_hobject_pc2line_pack(thr, q_instr, (duk_uint_fast32_t) code_count); /* -> pushes fixed buffer */
  51132. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_PC2LINE, DUK_PROPDESC_FLAGS_NONE);
  51133. /* XXX: if assertions enabled, walk through all valid PCs
  51134. * and check line mapping.
  51135. */
  51136. }
  51137. #endif /* DUK_USE_PC2LINE */
  51138. /* fileName */
  51139. if (comp_ctx->h_filename) {
  51140. /*
  51141. * Source filename (or equivalent), for identifying thrown errors.
  51142. */
  51143. duk_push_hstring(ctx, comp_ctx->h_filename);
  51144. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_NONE);
  51145. }
  51146. /*
  51147. * Init remaining result fields
  51148. *
  51149. * 'nregs' controls how large a register frame is allocated.
  51150. *
  51151. * 'nargs' controls how many formal arguments are written to registers:
  51152. * r0, ... r(nargs-1). The remaining registers are initialized to
  51153. * undefined.
  51154. */
  51155. DUK_ASSERT(func->temp_max >= 0);
  51156. h_res->nregs = func->temp_max;
  51157. h_res->nargs = duk_hobject_get_length(thr, func->h_argnames);
  51158. DUK_ASSERT(h_res->nregs >= h_res->nargs); /* pass2 allocation handles this */
  51159. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  51160. h_res->start_line = (duk_uint32_t) func->min_line;
  51161. h_res->end_line = (duk_uint32_t) func->max_line;
  51162. #endif
  51163. DUK_DD(DUK_DDPRINT("converted function: %!ixT",
  51164. (duk_tval *) duk_get_tval(ctx, -1)));
  51165. /*
  51166. * Compact the function template.
  51167. */
  51168. duk_compact(ctx, -1);
  51169. /*
  51170. * Debug dumping
  51171. */
  51172. #ifdef DUK_USE_DDDPRINT
  51173. {
  51174. duk_hcompiledfunction *h;
  51175. duk_instr_t *p, *p_start, *p_end;
  51176. h = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1);
  51177. p_start = (duk_instr_t *) DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, h);
  51178. p_end = (duk_instr_t *) DUK_HCOMPILEDFUNCTION_GET_CODE_END(thr->heap, h);
  51179. p = p_start;
  51180. while (p < p_end) {
  51181. DUK_DDD(DUK_DDDPRINT("BC %04ld: %!I ; 0x%08lx op=%ld (%!C) a=%ld b=%ld c=%ld",
  51182. (long) (p - p_start),
  51183. (duk_instr_t) (*p),
  51184. (unsigned long) (*p),
  51185. (long) DUK_DEC_OP(*p),
  51186. (long) DUK_DEC_OP(*p),
  51187. (long) DUK_DEC_A(*p),
  51188. (long) DUK_DEC_B(*p),
  51189. (long) DUK_DEC_C(*p)));
  51190. p++;
  51191. }
  51192. }
  51193. #endif
  51194. }
  51195. /*
  51196. * Code emission helpers
  51197. *
  51198. * Some emission helpers understand the range of target and source reg/const
  51199. * values and automatically emit shuffling code if necessary. This is the
  51200. * case when the slot in question (A, B, C) is used in the standard way and
  51201. * for opcodes the emission helpers explicitly understand (like DUK_OP_CALL).
  51202. *
  51203. * The standard way is that:
  51204. * - slot A is a target register
  51205. * - slot B is a source register/constant
  51206. * - slot C is a source register/constant
  51207. *
  51208. * If a slot is used in a non-standard way the caller must indicate this
  51209. * somehow. If a slot is used as a target instead of a source (or vice
  51210. * versa), this can be indicated with a flag to trigger proper shuffling
  51211. * (e.g. DUK__EMIT_FLAG_B_IS_TARGET). If the value in the slot is not
  51212. * register/const related at all, the caller must ensure that the raw value
  51213. * fits into the corresponding slot so as to not trigger shuffling. The
  51214. * caller must set a "no shuffle" flag to ensure compilation fails if
  51215. * shuffling were to be triggered because of an internal error.
  51216. *
  51217. * For slots B and C the raw slot size is 9 bits but one bit is reserved for
  51218. * the reg/const indicator. To use the full 9-bit range for a raw value,
  51219. * shuffling must be disabled with the DUK__EMIT_FLAG_NO_SHUFFLE_{B,C} flag.
  51220. * Shuffling is only done for A, B, and C slots, not the larger BC or ABC slots.
  51221. *
  51222. * There is call handling specific understanding in the A-B-C emitter to
  51223. * convert call setup and call instructions into indirect ones if necessary.
  51224. */
  51225. /* Code emission flags, passed in the 'opcode' field. Opcode + flags
  51226. * fit into 16 bits for now, so use duk_small_uint.t.
  51227. */
  51228. #define DUK__EMIT_FLAG_NO_SHUFFLE_A (1 << 8)
  51229. #define DUK__EMIT_FLAG_NO_SHUFFLE_B (1 << 9)
  51230. #define DUK__EMIT_FLAG_NO_SHUFFLE_C (1 << 10)
  51231. #define DUK__EMIT_FLAG_A_IS_SOURCE (1 << 11) /* slot A is a source (default: target) */
  51232. #define DUK__EMIT_FLAG_B_IS_TARGET (1 << 12) /* slot B is a target (default: source) */
  51233. #define DUK__EMIT_FLAG_C_IS_TARGET (1 << 13) /* slot C is a target (default: source) */
  51234. #define DUK__EMIT_FLAG_B_IS_TARGETSOURCE (1 << 14) /* slot B is both a target and a source (used by extraops like DUK_EXTRAOP_INSTOF */
  51235. #define DUK__EMIT_FLAG_RESERVE_JUMPSLOT (1 << 15) /* reserve a jumpslot after instr before target spilling, used for NEXTENUM */
  51236. /* XXX: clarify on when and where DUK__CONST_MARKER is allowed */
  51237. /* XXX: opcode specific assertions on when consts are allowed */
  51238. /* XXX: macro smaller than call? */
  51239. DUK_LOCAL duk_int_t duk__get_current_pc(duk_compiler_ctx *comp_ctx) {
  51240. duk_compiler_func *func;
  51241. func = &comp_ctx->curr_func;
  51242. return (duk_int_t) (DUK_BW_GET_SIZE(comp_ctx->thr, &func->bw_code) / sizeof(duk_compiler_instr));
  51243. }
  51244. DUK_LOCAL duk_compiler_instr *duk__get_instr_ptr(duk_compiler_ctx *comp_ctx, duk_int_t pc) {
  51245. DUK_ASSERT(pc >= 0);
  51246. 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)));
  51247. return ((duk_compiler_instr *) (void *) DUK_BW_GET_BASEPTR(comp_ctx->thr, &comp_ctx->curr_func.bw_code)) + pc;
  51248. }
  51249. /* emit instruction; could return PC but that's not needed in the majority
  51250. * of cases.
  51251. */
  51252. DUK_LOCAL void duk__emit(duk_compiler_ctx *comp_ctx, duk_instr_t ins) {
  51253. #if defined(DUK_USE_PC2LINE)
  51254. duk_int_t line;
  51255. #endif
  51256. duk_compiler_instr *instr;
  51257. DUK_DDD(DUK_DDDPRINT("duk__emit: 0x%08lx curr_token.start_line=%ld prev_token.start_line=%ld pc=%ld --> %!I",
  51258. (unsigned long) ins,
  51259. (long) comp_ctx->curr_token.start_line,
  51260. (long) comp_ctx->prev_token.start_line,
  51261. (long) duk__get_current_pc(comp_ctx),
  51262. (duk_instr_t) ins));
  51263. instr = (duk_compiler_instr *) (void *) DUK_BW_ENSURE_GETPTR(comp_ctx->thr, &comp_ctx->curr_func.bw_code, sizeof(duk_compiler_instr));
  51264. DUK_BW_ADD_PTR(comp_ctx->thr, &comp_ctx->curr_func.bw_code, sizeof(duk_compiler_instr));
  51265. #if defined(DUK_USE_PC2LINE)
  51266. /* The line number tracking is a bit inconsistent right now, which
  51267. * affects debugger accuracy. Mostly call sites emit opcodes when
  51268. * they have parsed a token (say a terminating semicolon) and called
  51269. * duk__advance(). In this case the line number of the previous
  51270. * token is the most accurate one (except in prologue where
  51271. * prev_token.start_line is 0). This is probably not 100% correct
  51272. * right now.
  51273. */
  51274. /* approximation, close enough */
  51275. line = comp_ctx->prev_token.start_line;
  51276. if (line == 0) {
  51277. line = comp_ctx->curr_token.start_line;
  51278. }
  51279. #endif
  51280. instr->ins = ins;
  51281. #if defined(DUK_USE_PC2LINE)
  51282. instr->line = line;
  51283. #endif
  51284. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  51285. if (line < comp_ctx->curr_func.min_line) {
  51286. comp_ctx->curr_func.min_line = line;
  51287. }
  51288. if (line > comp_ctx->curr_func.max_line) {
  51289. comp_ctx->curr_func.max_line = line;
  51290. }
  51291. #endif
  51292. /* Limit checks for bytecode byte size and line number. */
  51293. if (DUK_UNLIKELY(DUK_BW_GET_SIZE(comp_ctx->thr, &comp_ctx->curr_func.bw_code) > DUK_USE_ESBC_MAX_BYTES)) {
  51294. goto fail_bc_limit;
  51295. }
  51296. #if defined(DUK_USE_PC2LINE) && defined(DUK_USE_ESBC_LIMITS)
  51297. #if defined(DUK_USE_BUFLEN16)
  51298. /* Buffer length is bounded to 0xffff automatically, avoid compile warning. */
  51299. if (DUK_UNLIKELY(line > DUK_USE_ESBC_MAX_LINENUMBER)) {
  51300. goto fail_bc_limit;
  51301. }
  51302. #else
  51303. if (DUK_UNLIKELY(line > DUK_USE_ESBC_MAX_LINENUMBER)) {
  51304. goto fail_bc_limit;
  51305. }
  51306. #endif
  51307. #endif
  51308. return;
  51309. fail_bc_limit:
  51310. DUK_ERROR(comp_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_BYTECODE_LIMIT);
  51311. }
  51312. /* Update function min/max line from current token. Needed to improve
  51313. * function line range information for debugging, so that e.g. opening
  51314. * curly brace is covered by line range even when no opcodes are emitted
  51315. * for the line containing the brace.
  51316. */
  51317. DUK_LOCAL void duk__update_lineinfo_currtoken(duk_compiler_ctx *comp_ctx) {
  51318. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  51319. duk_int_t line;
  51320. line = comp_ctx->curr_token.start_line;
  51321. if (line == 0) {
  51322. return;
  51323. }
  51324. if (line < comp_ctx->curr_func.min_line) {
  51325. comp_ctx->curr_func.min_line = line;
  51326. }
  51327. if (line > comp_ctx->curr_func.max_line) {
  51328. comp_ctx->curr_func.max_line = line;
  51329. }
  51330. #else
  51331. DUK_UNREF(comp_ctx);
  51332. #endif
  51333. }
  51334. #if 0 /* unused */
  51335. DUK_LOCAL void duk__emit_op_only(duk_compiler_ctx *comp_ctx, duk_small_uint_t op) {
  51336. duk__emit(comp_ctx, DUK_ENC_OP_ABC(op, 0));
  51337. }
  51338. #endif
  51339. /* Important main primitive. */
  51340. 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) {
  51341. duk_instr_t ins = 0;
  51342. duk_int_t a_out = -1;
  51343. duk_int_t b_out = -1;
  51344. duk_int_t c_out = -1;
  51345. duk_int_t tmp;
  51346. DUK_DDD(DUK_DDDPRINT("emit: op_flags=%04lx, a=%ld, b=%ld, c=%ld",
  51347. (unsigned long) op_flags, (long) a, (long) b, (long) c));
  51348. /* We could rely on max temp/const checks: if they don't exceed BC
  51349. * limit, nothing here can either (just asserts would be enough).
  51350. * Currently we check for the limits, which provides additional
  51351. * protection against creating invalid bytecode due to compiler
  51352. * bugs.
  51353. */
  51354. DUK_ASSERT_DISABLE((op_flags & 0xff) >= DUK_BC_OP_MIN); /* unsigned */
  51355. DUK_ASSERT((op_flags & 0xff) <= DUK_BC_OP_MAX);
  51356. /* Input shuffling happens before the actual operation, while output
  51357. * shuffling happens afterwards. Output shuffling decisions are still
  51358. * made at the same time to reduce branch clutter; output shuffle decisions
  51359. * are recorded into X_out variables.
  51360. */
  51361. /* Slot A */
  51362. #if defined(DUK_USE_SHUFFLE_TORTURE)
  51363. if (a <= DUK_BC_A_MAX && (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_A)) {
  51364. #else
  51365. if (a <= DUK_BC_A_MAX) {
  51366. #endif
  51367. ;
  51368. } else if (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_A) {
  51369. DUK_D(DUK_DPRINT("out of regs: 'a' (reg) needs shuffling but shuffle prohibited, a: %ld", (long) a));
  51370. goto error_outofregs;
  51371. } else if (a <= DUK_BC_BC_MAX) {
  51372. comp_ctx->curr_func.needs_shuffle = 1;
  51373. tmp = comp_ctx->curr_func.shuffle1;
  51374. if (op_flags & DUK__EMIT_FLAG_A_IS_SOURCE) {
  51375. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDREG, tmp, a));
  51376. } else {
  51377. duk_small_int_t op = op_flags & 0xff;
  51378. if (op == DUK_OP_CSVAR || op == DUK_OP_CSREG || op == DUK_OP_CSPROP) {
  51379. /* Special handling for call setup instructions. The target
  51380. * is expressed indirectly, but there is no output shuffling.
  51381. */
  51382. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_A_IS_SOURCE) == 0);
  51383. duk__emit_load_int32_noshuffle(comp_ctx, tmp, a);
  51384. DUK_ASSERT(DUK_OP_CSVARI == DUK_OP_CSVAR + 1);
  51385. DUK_ASSERT(DUK_OP_CSREGI == DUK_OP_CSREG + 1);
  51386. DUK_ASSERT(DUK_OP_CSPROPI == DUK_OP_CSPROP + 1);
  51387. op_flags++; /* indirect opcode follows direct */
  51388. } else {
  51389. /* Output shuffle needed after main operation */
  51390. a_out = a;
  51391. }
  51392. }
  51393. a = tmp;
  51394. } else {
  51395. DUK_D(DUK_DPRINT("out of regs: 'a' (reg) needs shuffling but does not fit into BC, a: %ld", (long) a));
  51396. goto error_outofregs;
  51397. }
  51398. /* Slot B */
  51399. if (b & DUK__CONST_MARKER) {
  51400. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_B) == 0);
  51401. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_B_IS_TARGET) == 0);
  51402. DUK_ASSERT((op_flags & 0xff) != DUK_OP_CALL);
  51403. DUK_ASSERT((op_flags & 0xff) != DUK_OP_NEW);
  51404. b = b & ~DUK__CONST_MARKER;
  51405. #if defined(DUK_USE_SHUFFLE_TORTURE)
  51406. if (0) {
  51407. #else
  51408. if (b <= 0xff) {
  51409. #endif
  51410. ins |= DUK_ENC_OP_A_B_C(0, 0, 0x100, 0); /* const flag for B */
  51411. } else if (b <= DUK_BC_BC_MAX) {
  51412. comp_ctx->curr_func.needs_shuffle = 1;
  51413. tmp = comp_ctx->curr_func.shuffle2;
  51414. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDCONST, tmp, b));
  51415. b = tmp;
  51416. } else {
  51417. DUK_D(DUK_DPRINT("out of regs: 'b' (const) needs shuffling but does not fit into BC, b: %ld", (long) b));
  51418. goto error_outofregs;
  51419. }
  51420. } else {
  51421. #if defined(DUK_USE_SHUFFLE_TORTURE)
  51422. if (b <= 0xff && (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_B)) {
  51423. #else
  51424. if (b <= 0xff) {
  51425. #endif
  51426. ;
  51427. } else if (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_B) {
  51428. if (b > DUK_BC_B_MAX) {
  51429. /* Note: 0xff != DUK_BC_B_MAX */
  51430. DUK_D(DUK_DPRINT("out of regs: 'b' (reg) needs shuffling but shuffle prohibited, b: %ld", (long) b));
  51431. goto error_outofregs;
  51432. }
  51433. } else if (b <= DUK_BC_BC_MAX) {
  51434. comp_ctx->curr_func.needs_shuffle = 1;
  51435. tmp = comp_ctx->curr_func.shuffle2;
  51436. if (op_flags & DUK__EMIT_FLAG_B_IS_TARGET) {
  51437. /* Output shuffle needed after main operation */
  51438. b_out = b;
  51439. }
  51440. if (!(op_flags & DUK__EMIT_FLAG_B_IS_TARGET) || (op_flags & DUK__EMIT_FLAG_B_IS_TARGETSOURCE)) {
  51441. duk_small_int_t op = op_flags & 0xff;
  51442. if (op == DUK_OP_CALL || op == DUK_OP_NEW ||
  51443. op == DUK_OP_MPUTOBJ || op == DUK_OP_MPUTARR) {
  51444. /* Special handling for CALL/NEW/MPUTOBJ/MPUTARR shuffling.
  51445. * For each, slot B identifies the first register of a range
  51446. * of registers, so normal shuffling won't work. Instead,
  51447. * an indirect version of the opcode is used.
  51448. */
  51449. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_B_IS_TARGET) == 0);
  51450. duk__emit_load_int32_noshuffle(comp_ctx, tmp, b);
  51451. DUK_ASSERT(DUK_OP_CALLI == DUK_OP_CALL + 1);
  51452. DUK_ASSERT(DUK_OP_NEWI == DUK_OP_NEW + 1);
  51453. DUK_ASSERT(DUK_OP_MPUTOBJI == DUK_OP_MPUTOBJ + 1);
  51454. DUK_ASSERT(DUK_OP_MPUTARRI == DUK_OP_MPUTARR + 1);
  51455. op_flags++; /* indirect opcode follows direct */
  51456. } else {
  51457. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDREG, tmp, b));
  51458. }
  51459. }
  51460. b = tmp;
  51461. } else {
  51462. DUK_D(DUK_DPRINT("out of regs: 'b' (reg) needs shuffling but does not fit into BC, b: %ld", (long) b));
  51463. goto error_outofregs;
  51464. }
  51465. }
  51466. /* Slot C */
  51467. if (c & DUK__CONST_MARKER) {
  51468. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_C) == 0);
  51469. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_C_IS_TARGET) == 0);
  51470. c = c & ~DUK__CONST_MARKER;
  51471. #if defined(DUK_USE_SHUFFLE_TORTURE)
  51472. if (0) {
  51473. #else
  51474. if (c <= 0xff) {
  51475. #endif
  51476. ins |= DUK_ENC_OP_A_B_C(0, 0, 0, 0x100); /* const flag for C */
  51477. } else if (c <= DUK_BC_BC_MAX) {
  51478. comp_ctx->curr_func.needs_shuffle = 1;
  51479. tmp = comp_ctx->curr_func.shuffle3;
  51480. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDCONST, tmp, c));
  51481. c = tmp;
  51482. } else {
  51483. DUK_D(DUK_DPRINT("out of regs: 'c' (const) needs shuffling but does not fit into BC, c: %ld", (long) c));
  51484. goto error_outofregs;
  51485. }
  51486. } else {
  51487. #if defined(DUK_USE_SHUFFLE_TORTURE)
  51488. if (c <= 0xff && (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_C)) {
  51489. #else
  51490. if (c <= 0xff) {
  51491. #endif
  51492. ;
  51493. } else if (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_C) {
  51494. if (c > DUK_BC_C_MAX) {
  51495. /* Note: 0xff != DUK_BC_C_MAX */
  51496. DUK_D(DUK_DPRINT("out of regs: 'c' (reg) needs shuffling but shuffle prohibited, c: %ld", (long) c));
  51497. goto error_outofregs;
  51498. }
  51499. } else if (c <= DUK_BC_BC_MAX) {
  51500. comp_ctx->curr_func.needs_shuffle = 1;
  51501. tmp = comp_ctx->curr_func.shuffle3;
  51502. if (op_flags & DUK__EMIT_FLAG_C_IS_TARGET) {
  51503. /* Output shuffle needed after main operation */
  51504. c_out = c;
  51505. } else {
  51506. duk_small_int_t op = op_flags & 0xff;
  51507. if (op == DUK_OP_EXTRA &&
  51508. (a == DUK_EXTRAOP_INITGET || a == DUK_EXTRAOP_INITSET)) {
  51509. /* Special shuffling for INITGET/INITSET, where slot C
  51510. * identifies a register pair and cannot be shuffled
  51511. * normally. Use an indirect variant instead.
  51512. */
  51513. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_C_IS_TARGET) == 0);
  51514. duk__emit_load_int32_noshuffle(comp_ctx, tmp, c);
  51515. DUK_ASSERT(DUK_EXTRAOP_INITGETI == DUK_EXTRAOP_INITGET + 1);
  51516. DUK_ASSERT(DUK_EXTRAOP_INITSETI == DUK_EXTRAOP_INITSET + 1);
  51517. a++; /* indirect opcode follows direct */
  51518. } else {
  51519. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDREG, tmp, c));
  51520. }
  51521. }
  51522. c = tmp;
  51523. } else {
  51524. DUK_D(DUK_DPRINT("out of regs: 'c' (reg) needs shuffling but does not fit into BC, c: %ld", (long) c));
  51525. goto error_outofregs;
  51526. }
  51527. }
  51528. /* Main operation */
  51529. DUK_ASSERT_DISABLE(a >= DUK_BC_A_MIN); /* unsigned */
  51530. DUK_ASSERT(a <= DUK_BC_A_MAX);
  51531. DUK_ASSERT_DISABLE(b >= DUK_BC_B_MIN); /* unsigned */
  51532. DUK_ASSERT(b <= DUK_BC_B_MAX);
  51533. DUK_ASSERT_DISABLE(c >= DUK_BC_C_MIN); /* unsigned */
  51534. DUK_ASSERT(c <= DUK_BC_C_MAX);
  51535. ins |= DUK_ENC_OP_A_B_C(op_flags & 0xff, a, b, c);
  51536. duk__emit(comp_ctx, ins);
  51537. /* NEXTENUM needs a jump slot right after the main instruction.
  51538. * When the JUMP is taken, output spilling is not needed so this
  51539. * workaround is possible. The jump slot PC is exceptionally
  51540. * plumbed through comp_ctx to minimize call sites.
  51541. */
  51542. if (op_flags & DUK__EMIT_FLAG_RESERVE_JUMPSLOT) {
  51543. comp_ctx->emit_jumpslot_pc = duk__get_current_pc(comp_ctx);
  51544. duk__emit_abc(comp_ctx, DUK_OP_JUMP, 0);
  51545. }
  51546. /* Output shuffling: only one output register is realistically possible.
  51547. *
  51548. * (Zero would normally be an OK marker value: if the target register
  51549. * was zero, it would never be shuffled. But with DUK_USE_SHUFFLE_TORTURE
  51550. * this is no longer true, so use -1 as a marker instead.)
  51551. */
  51552. if (a_out >= 0) {
  51553. DUK_ASSERT(b_out < 0);
  51554. DUK_ASSERT(c_out < 0);
  51555. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_STREG, a, a_out));
  51556. } else if (b_out >= 0) {
  51557. DUK_ASSERT(a_out < 0);
  51558. DUK_ASSERT(c_out < 0);
  51559. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_STREG, b, b_out));
  51560. } else if (c_out >= 0) {
  51561. DUK_ASSERT(b_out < 0);
  51562. DUK_ASSERT(c_out < 0);
  51563. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_STREG, c, c_out));
  51564. }
  51565. return;
  51566. error_outofregs:
  51567. DUK_ERROR(comp_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_REG_LIMIT);
  51568. }
  51569. 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) {
  51570. duk__emit_a_b_c(comp_ctx, op_flags | DUK__EMIT_FLAG_NO_SHUFFLE_C, a, b, 0);
  51571. }
  51572. #if 0 /* unused */
  51573. DUK_LOCAL void duk__emit_a(duk_compiler_ctx *comp_ctx, int op_flags, int a) {
  51574. duk__emit_a_b_c(comp_ctx, op_flags | DUK__EMIT_FLAG_NO_SHUFFLE_B | DUK__EMIT_FLAG_NO_SHUFFLE_C, a, 0, 0);
  51575. }
  51576. #endif
  51577. 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) {
  51578. duk_instr_t ins;
  51579. duk_int_t tmp;
  51580. /* allow caller to give a const number with the DUK__CONST_MARKER */
  51581. bc = bc & (~DUK__CONST_MARKER);
  51582. DUK_ASSERT_DISABLE((op_flags & 0xff) >= DUK_BC_OP_MIN); /* unsigned */
  51583. DUK_ASSERT((op_flags & 0xff) <= DUK_BC_OP_MAX);
  51584. DUK_ASSERT_DISABLE(bc >= DUK_BC_BC_MIN); /* unsigned */
  51585. DUK_ASSERT(bc <= DUK_BC_BC_MAX);
  51586. DUK_ASSERT((bc & DUK__CONST_MARKER) == 0);
  51587. if (bc <= DUK_BC_BC_MAX) {
  51588. ;
  51589. } else {
  51590. /* No BC shuffling now. */
  51591. goto error_outofregs;
  51592. }
  51593. #if defined(DUK_USE_SHUFFLE_TORTURE)
  51594. if (a <= DUK_BC_A_MAX && (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_A)) {
  51595. #else
  51596. if (a <= DUK_BC_A_MAX) {
  51597. #endif
  51598. ins = DUK_ENC_OP_A_BC(op_flags & 0xff, a, bc);
  51599. duk__emit(comp_ctx, ins);
  51600. } else if (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_A) {
  51601. goto error_outofregs;
  51602. } else if (a <= DUK_BC_BC_MAX) {
  51603. comp_ctx->curr_func.needs_shuffle = 1;
  51604. tmp = comp_ctx->curr_func.shuffle1;
  51605. ins = DUK_ENC_OP_A_BC(op_flags & 0xff, tmp, bc);
  51606. if (op_flags & DUK__EMIT_FLAG_A_IS_SOURCE) {
  51607. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDREG, tmp, a));
  51608. duk__emit(comp_ctx, ins);
  51609. } else {
  51610. duk__emit(comp_ctx, ins);
  51611. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_STREG, tmp, a));
  51612. }
  51613. } else {
  51614. goto error_outofregs;
  51615. }
  51616. return;
  51617. error_outofregs:
  51618. DUK_ERROR(comp_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_REG_LIMIT);
  51619. }
  51620. DUK_LOCAL void duk__emit_abc(duk_compiler_ctx *comp_ctx, duk_small_uint_t op, duk_regconst_t abc) {
  51621. duk_instr_t ins;
  51622. DUK_ASSERT_DISABLE(op >= DUK_BC_OP_MIN); /* unsigned */
  51623. DUK_ASSERT(op <= DUK_BC_OP_MAX);
  51624. DUK_ASSERT_DISABLE(abc >= DUK_BC_ABC_MIN); /* unsigned */
  51625. DUK_ASSERT(abc <= DUK_BC_ABC_MAX);
  51626. DUK_ASSERT((abc & DUK__CONST_MARKER) == 0);
  51627. if (abc <= DUK_BC_ABC_MAX) {
  51628. ;
  51629. } else {
  51630. goto error_outofregs;
  51631. }
  51632. ins = DUK_ENC_OP_ABC(op, abc);
  51633. DUK_DDD(DUK_DDDPRINT("duk__emit_abc: 0x%08lx line=%ld pc=%ld op=%ld (%!C) abc=%ld (%!I)",
  51634. (unsigned long) ins, (long) comp_ctx->curr_token.start_line,
  51635. (long) duk__get_current_pc(comp_ctx), (long) op, (long) op,
  51636. (long) abc, (duk_instr_t) ins));
  51637. duk__emit(comp_ctx, ins);
  51638. return;
  51639. error_outofregs:
  51640. DUK_ERROR(comp_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_REG_LIMIT);
  51641. }
  51642. DUK_LOCAL void duk__emit_extraop_b_c(duk_compiler_ctx *comp_ctx, duk_small_uint_t extraop_flags, duk_regconst_t b, duk_regconst_t c) {
  51643. DUK_ASSERT_DISABLE((extraop_flags & 0xff) >= DUK_BC_EXTRAOP_MIN); /* unsigned */
  51644. DUK_ASSERT((extraop_flags & 0xff) <= DUK_BC_EXTRAOP_MAX);
  51645. /* Setting "no shuffle A" is covered by the assert, but it's needed
  51646. * with DUK_USE_SHUFFLE_TORTURE.
  51647. */
  51648. duk__emit_a_b_c(comp_ctx,
  51649. DUK_OP_EXTRA | DUK__EMIT_FLAG_NO_SHUFFLE_A | (extraop_flags & ~0xff), /* transfer flags */
  51650. extraop_flags & 0xff,
  51651. b,
  51652. c);
  51653. }
  51654. DUK_LOCAL void duk__emit_extraop_b(duk_compiler_ctx *comp_ctx, duk_small_uint_t extraop_flags, duk_regconst_t b) {
  51655. DUK_ASSERT_DISABLE((extraop_flags & 0xff) >= DUK_BC_EXTRAOP_MIN); /* unsigned */
  51656. DUK_ASSERT((extraop_flags & 0xff) <= DUK_BC_EXTRAOP_MAX);
  51657. /* Setting "no shuffle A" is covered by the assert, but it's needed
  51658. * with DUK_USE_SHUFFLE_TORTURE.
  51659. */
  51660. duk__emit_a_b_c(comp_ctx,
  51661. DUK_OP_EXTRA | DUK__EMIT_FLAG_NO_SHUFFLE_A | (extraop_flags & ~0xff), /* transfer flags */
  51662. extraop_flags & 0xff,
  51663. b,
  51664. 0);
  51665. }
  51666. DUK_LOCAL void duk__emit_extraop_bc(duk_compiler_ctx *comp_ctx, duk_small_uint_t extraop, duk_regconst_t bc) {
  51667. DUK_ASSERT_DISABLE(extraop >= DUK_BC_EXTRAOP_MIN); /* unsigned */
  51668. DUK_ASSERT(extraop <= DUK_BC_EXTRAOP_MAX);
  51669. /* Setting "no shuffle A" is covered by the assert, but it's needed
  51670. * with DUK_USE_SHUFFLE_TORTURE.
  51671. */
  51672. duk__emit_a_bc(comp_ctx,
  51673. DUK_OP_EXTRA | DUK__EMIT_FLAG_NO_SHUFFLE_A,
  51674. extraop,
  51675. bc);
  51676. }
  51677. DUK_LOCAL void duk__emit_extraop_only(duk_compiler_ctx *comp_ctx, duk_small_uint_t extraop_flags) {
  51678. DUK_ASSERT_DISABLE((extraop_flags & 0xff) >= DUK_BC_EXTRAOP_MIN); /* unsigned */
  51679. DUK_ASSERT((extraop_flags & 0xff) <= DUK_BC_EXTRAOP_MAX);
  51680. /* Setting "no shuffle A" is covered by the assert, but it's needed
  51681. * with DUK_USE_SHUFFLE_TORTURE.
  51682. */
  51683. duk__emit_a_b_c(comp_ctx,
  51684. DUK_OP_EXTRA | DUK__EMIT_FLAG_NO_SHUFFLE_A | DUK__EMIT_FLAG_NO_SHUFFLE_B |
  51685. DUK__EMIT_FLAG_NO_SHUFFLE_C | (extraop_flags & ~0xff), /* transfer flags */
  51686. extraop_flags & 0xff,
  51687. 0,
  51688. 0);
  51689. }
  51690. DUK_LOCAL void duk__emit_load_int32_raw(duk_compiler_ctx *comp_ctx, duk_reg_t reg, duk_int32_t val, duk_small_uint_t op_flags) {
  51691. /* XXX: Shuffling support could be implemented here so that LDINT+LDINTX
  51692. * would only shuffle once (instead of twice). The current code works
  51693. * though, and has a smaller compiler footprint.
  51694. */
  51695. if ((val >= (duk_int32_t) DUK_BC_BC_MIN - (duk_int32_t) DUK_BC_LDINT_BIAS) &&
  51696. (val <= (duk_int32_t) DUK_BC_BC_MAX - (duk_int32_t) DUK_BC_LDINT_BIAS)) {
  51697. DUK_DDD(DUK_DDDPRINT("emit LDINT to reg %ld for %ld", (long) reg, (long) val));
  51698. duk__emit_a_bc(comp_ctx, DUK_OP_LDINT | op_flags, reg, (duk_regconst_t) (val + (duk_int32_t) DUK_BC_LDINT_BIAS));
  51699. } else {
  51700. duk_int32_t hi = val >> DUK_BC_LDINTX_SHIFT;
  51701. duk_int32_t lo = val & ((((duk_int32_t) 1) << DUK_BC_LDINTX_SHIFT) - 1);
  51702. DUK_ASSERT(lo >= 0);
  51703. DUK_DDD(DUK_DDDPRINT("emit LDINT+LDINTX to reg %ld for %ld -> hi %ld, lo %ld",
  51704. (long) reg, (long) val, (long) hi, (long) lo));
  51705. duk__emit_a_bc(comp_ctx, DUK_OP_LDINT | op_flags, reg, (duk_regconst_t) (hi + (duk_int32_t) DUK_BC_LDINT_BIAS));
  51706. duk__emit_a_bc(comp_ctx, DUK_OP_LDINTX | op_flags, reg, (duk_regconst_t) lo);
  51707. }
  51708. }
  51709. DUK_LOCAL void duk__emit_load_int32(duk_compiler_ctx *comp_ctx, duk_reg_t reg, duk_int32_t val) {
  51710. duk__emit_load_int32_raw(comp_ctx, reg, val, 0 /*op_flags*/);
  51711. }
  51712. #if defined(DUK_USE_SHUFFLE_TORTURE)
  51713. /* Used by duk__emit*() calls so that we don't shuffle the loadints that
  51714. * are needed to handle indirect opcodes.
  51715. */
  51716. DUK_LOCAL void duk__emit_load_int32_noshuffle(duk_compiler_ctx *comp_ctx, duk_reg_t reg, duk_int32_t val) {
  51717. duk__emit_load_int32_raw(comp_ctx, reg, val, DUK__EMIT_FLAG_NO_SHUFFLE_A /*op_flags*/);
  51718. }
  51719. #else
  51720. DUK_LOCAL void duk__emit_load_int32_noshuffle(duk_compiler_ctx *comp_ctx, duk_reg_t reg, duk_int32_t val) {
  51721. /* When torture not enabled, can just use the same helper because
  51722. * 'reg' won't get spilled.
  51723. */
  51724. DUK_ASSERT(reg <= DUK_BC_A_MAX);
  51725. duk__emit_load_int32(comp_ctx, reg, val);
  51726. }
  51727. #endif
  51728. DUK_LOCAL void duk__emit_jump(duk_compiler_ctx *comp_ctx, duk_int_t target_pc) {
  51729. duk_int_t curr_pc;
  51730. duk_int_t offset;
  51731. curr_pc = (duk_int_t) (DUK_BW_GET_SIZE(comp_ctx->thr, &comp_ctx->curr_func.bw_code) / sizeof(duk_compiler_instr));
  51732. offset = (duk_int_t) target_pc - (duk_int_t) curr_pc - 1;
  51733. DUK_ASSERT(offset + DUK_BC_JUMP_BIAS >= DUK_BC_ABC_MIN);
  51734. DUK_ASSERT(offset + DUK_BC_JUMP_BIAS <= DUK_BC_ABC_MAX);
  51735. duk__emit_abc(comp_ctx, DUK_OP_JUMP, (duk_regconst_t) (offset + DUK_BC_JUMP_BIAS));
  51736. }
  51737. DUK_LOCAL duk_int_t duk__emit_jump_empty(duk_compiler_ctx *comp_ctx) {
  51738. duk_int_t ret;
  51739. ret = duk__get_current_pc(comp_ctx); /* useful for patching jumps later */
  51740. duk__emit_abc(comp_ctx, DUK_OP_JUMP, 0);
  51741. return ret;
  51742. }
  51743. /* Insert an empty jump in the middle of code emitted earlier. This is
  51744. * currently needed for compiling for-in.
  51745. */
  51746. DUK_LOCAL void duk__insert_jump_entry(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc) {
  51747. #if defined(DUK_USE_PC2LINE)
  51748. duk_int_t line;
  51749. #endif
  51750. duk_compiler_instr *instr;
  51751. duk_size_t offset;
  51752. offset = jump_pc * sizeof(duk_compiler_instr),
  51753. instr = (duk_compiler_instr *) (void *)
  51754. DUK_BW_INSERT_ENSURE_AREA(comp_ctx->thr,
  51755. &comp_ctx->curr_func.bw_code,
  51756. offset,
  51757. sizeof(duk_compiler_instr));
  51758. #if defined(DUK_USE_PC2LINE)
  51759. line = comp_ctx->curr_token.start_line; /* approximation, close enough */
  51760. #endif
  51761. instr->ins = DUK_ENC_OP_ABC(DUK_OP_JUMP, 0);
  51762. #if defined(DUK_USE_PC2LINE)
  51763. instr->line = line;
  51764. #endif
  51765. DUK_BW_ADD_PTR(comp_ctx->thr, &comp_ctx->curr_func.bw_code, sizeof(duk_compiler_instr));
  51766. if (DUK_UNLIKELY(DUK_BW_GET_SIZE(comp_ctx->thr, &comp_ctx->curr_func.bw_code) > DUK_USE_ESBC_MAX_BYTES)) {
  51767. goto fail_bc_limit;
  51768. }
  51769. return;
  51770. fail_bc_limit:
  51771. DUK_ERROR(comp_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_BYTECODE_LIMIT);
  51772. }
  51773. /* Does not assume that jump_pc contains a DUK_OP_JUMP previously; this is intentional
  51774. * to allow e.g. an INVALID opcode be overwritten with a JUMP (label management uses this).
  51775. */
  51776. DUK_LOCAL void duk__patch_jump(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc, duk_int_t target_pc) {
  51777. duk_compiler_instr *instr;
  51778. duk_int_t offset;
  51779. /* allow negative PCs, behave as a no-op */
  51780. if (jump_pc < 0) {
  51781. DUK_DDD(DUK_DDDPRINT("duk__patch_jump(): nop call, jump_pc=%ld (<0), target_pc=%ld",
  51782. (long) jump_pc, (long) target_pc));
  51783. return;
  51784. }
  51785. DUK_ASSERT(jump_pc >= 0);
  51786. /* XXX: range assert */
  51787. instr = duk__get_instr_ptr(comp_ctx, jump_pc);
  51788. DUK_ASSERT(instr != NULL);
  51789. /* XXX: range assert */
  51790. offset = target_pc - jump_pc - 1;
  51791. instr->ins = DUK_ENC_OP_ABC(DUK_OP_JUMP, offset + DUK_BC_JUMP_BIAS);
  51792. DUK_DDD(DUK_DDDPRINT("duk__patch_jump(): jump_pc=%ld, target_pc=%ld, offset=%ld",
  51793. (long) jump_pc, (long) target_pc, (long) offset));
  51794. }
  51795. DUK_LOCAL void duk__patch_jump_here(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc) {
  51796. duk__patch_jump(comp_ctx, jump_pc, duk__get_current_pc(comp_ctx));
  51797. }
  51798. 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) {
  51799. duk_compiler_instr *instr;
  51800. DUK_ASSERT((reg_catch & DUK__CONST_MARKER) == 0);
  51801. instr = duk__get_instr_ptr(comp_ctx, ldconst_pc);
  51802. DUK_ASSERT(DUK_DEC_OP(instr->ins) == DUK_OP_LDCONST);
  51803. DUK_ASSERT(instr != NULL);
  51804. if (const_varname & DUK__CONST_MARKER) {
  51805. /* Have a catch variable. */
  51806. const_varname = const_varname & (~DUK__CONST_MARKER);
  51807. if (reg_catch > DUK_BC_BC_MAX || const_varname > DUK_BC_BC_MAX) {
  51808. /* Catch attempts to use out-of-range reg/const. Without this
  51809. * check Duktape 0.12.0 could generate invalid code which caused
  51810. * an assert failure on execution. This error is triggered e.g.
  51811. * for functions with a lot of constants and a try-catch statement.
  51812. * Shuffling or opcode semantics change is needed to fix the issue.
  51813. * See: test-bug-trycatch-many-constants.js.
  51814. */
  51815. DUK_D(DUK_DPRINT("failed to patch trycatch: flags=%ld, reg_catch=%ld, const_varname=%ld (0x%08lx)",
  51816. (long) flags, (long) reg_catch, (long) const_varname, (long) const_varname));
  51817. DUK_ERROR(comp_ctx->thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_REG_LIMIT);
  51818. }
  51819. instr->ins |= DUK_ENC_OP_A_BC(0, 0, const_varname);
  51820. } else {
  51821. /* No catch variable, e.g. a try-finally; replace LDCONST with
  51822. * NOP to avoid a bogus LDCONST.
  51823. */
  51824. instr->ins = DUK_ENC_OP_A(DUK_OP_EXTRA, DUK_EXTRAOP_NOP);
  51825. }
  51826. instr = duk__get_instr_ptr(comp_ctx, trycatch_pc);
  51827. DUK_ASSERT(instr != NULL);
  51828. DUK_ASSERT_DISABLE(flags >= DUK_BC_A_MIN);
  51829. DUK_ASSERT(flags <= DUK_BC_A_MAX);
  51830. instr->ins = DUK_ENC_OP_A_BC(DUK_OP_TRYCATCH, flags, reg_catch);
  51831. }
  51832. DUK_LOCAL void duk__emit_if_false_skip(duk_compiler_ctx *comp_ctx, duk_regconst_t regconst) {
  51833. duk__emit_a_b_c(comp_ctx,
  51834. DUK_OP_IF | DUK__EMIT_FLAG_NO_SHUFFLE_A | DUK__EMIT_FLAG_NO_SHUFFLE_C,
  51835. 0 /*false*/,
  51836. regconst,
  51837. 0 /*unused*/);
  51838. }
  51839. DUK_LOCAL void duk__emit_if_true_skip(duk_compiler_ctx *comp_ctx, duk_regconst_t regconst) {
  51840. duk__emit_a_b_c(comp_ctx,
  51841. DUK_OP_IF | DUK__EMIT_FLAG_NO_SHUFFLE_A | DUK__EMIT_FLAG_NO_SHUFFLE_C,
  51842. 1 /*true*/,
  51843. regconst,
  51844. 0 /*unused*/);
  51845. }
  51846. DUK_LOCAL void duk__emit_invalid(duk_compiler_ctx *comp_ctx) {
  51847. duk__emit_extraop_bc(comp_ctx, DUK_EXTRAOP_INVALID, 0);
  51848. }
  51849. /*
  51850. * Peephole optimizer for finished bytecode.
  51851. *
  51852. * Does not remove opcodes; currently only straightens out unconditional
  51853. * jump chains which are generated by several control structures.
  51854. */
  51855. DUK_LOCAL void duk__peephole_optimize_bytecode(duk_compiler_ctx *comp_ctx) {
  51856. duk_compiler_instr *bc;
  51857. duk_small_uint_t iter;
  51858. duk_int_t i, n;
  51859. duk_int_t count_opt;
  51860. bc = (duk_compiler_instr *) (void *) DUK_BW_GET_BASEPTR(comp_ctx->thr, &comp_ctx->curr_func.bw_code);
  51861. #if defined(DUK_USE_BUFLEN16)
  51862. /* No need to assert, buffer size maximum is 0xffff. */
  51863. #else
  51864. 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 */
  51865. #endif
  51866. n = (duk_int_t) (DUK_BW_GET_SIZE(comp_ctx->thr, &comp_ctx->curr_func.bw_code) / sizeof(duk_compiler_instr));
  51867. for (iter = 0; iter < DUK_COMPILER_PEEPHOLE_MAXITER; iter++) {
  51868. count_opt = 0;
  51869. for (i = 0; i < n; i++) {
  51870. duk_instr_t ins;
  51871. duk_int_t target_pc1;
  51872. duk_int_t target_pc2;
  51873. ins = bc[i].ins;
  51874. if (DUK_DEC_OP(ins) != DUK_OP_JUMP) {
  51875. continue;
  51876. }
  51877. target_pc1 = i + 1 + DUK_DEC_ABC(ins) - DUK_BC_JUMP_BIAS;
  51878. DUK_DDD(DUK_DDDPRINT("consider jump at pc %ld; target_pc=%ld", (long) i, (long) target_pc1));
  51879. DUK_ASSERT(target_pc1 >= 0);
  51880. DUK_ASSERT(target_pc1 < n);
  51881. /* Note: if target_pc1 == i, we'll optimize a jump to itself.
  51882. * This does not need to be checked for explicitly; the case
  51883. * is rare and max iter breaks us out.
  51884. */
  51885. ins = bc[target_pc1].ins;
  51886. if (DUK_DEC_OP(ins) != DUK_OP_JUMP) {
  51887. continue;
  51888. }
  51889. target_pc2 = target_pc1 + 1 + DUK_DEC_ABC(ins) - DUK_BC_JUMP_BIAS;
  51890. DUK_DDD(DUK_DDDPRINT("optimizing jump at pc %ld; old target is %ld -> new target is %ld",
  51891. (long) i, (long) target_pc1, (long) target_pc2));
  51892. bc[i].ins = DUK_ENC_OP_ABC(DUK_OP_JUMP, target_pc2 - (i + 1) + DUK_BC_JUMP_BIAS);
  51893. count_opt++;
  51894. }
  51895. DUK_DD(DUK_DDPRINT("optimized %ld jumps on peephole round %ld", (long) count_opt, (long) (iter + 1)));
  51896. if (count_opt == 0) {
  51897. break;
  51898. }
  51899. }
  51900. }
  51901. /*
  51902. * Intermediate value helpers
  51903. */
  51904. #define DUK__ISREG(comp_ctx,x) (((x) & DUK__CONST_MARKER) == 0)
  51905. #define DUK__ISCONST(comp_ctx,x) (((x) & DUK__CONST_MARKER) != 0)
  51906. #define DUK__ISTEMP(comp_ctx,x) (DUK__ISREG((comp_ctx), (x)) && (duk_regconst_t) (x) >= (duk_regconst_t) ((comp_ctx)->curr_func.temp_first))
  51907. #define DUK__GETTEMP(comp_ctx) ((comp_ctx)->curr_func.temp_next)
  51908. #define DUK__SETTEMP(comp_ctx,x) ((comp_ctx)->curr_func.temp_next = (x)) /* dangerous: must only lower (temp_max not updated) */
  51909. #define DUK__SETTEMP_CHECKMAX(comp_ctx,x) duk__settemp_checkmax((comp_ctx),(x))
  51910. #define DUK__ALLOCTEMP(comp_ctx) duk__alloctemp((comp_ctx))
  51911. #define DUK__ALLOCTEMPS(comp_ctx,count) duk__alloctemps((comp_ctx),(count))
  51912. /* Flags for intermediate value coercions. A flag for using a forced reg
  51913. * is not needed, the forced_reg argument suffices and generates better
  51914. * code (it is checked as it is used).
  51915. */
  51916. #define DUK__IVAL_FLAG_ALLOW_CONST (1 << 0) /* allow a constant to be returned */
  51917. #define DUK__IVAL_FLAG_REQUIRE_TEMP (1 << 1) /* require a (mutable) temporary as a result (or a const if allowed) */
  51918. #define DUK__IVAL_FLAG_REQUIRE_SHORT (1 << 2) /* require a short (8-bit) reg/const which fits into bytecode B/C slot */
  51919. /* XXX: some code might benefit from DUK__SETTEMP_IFTEMP(ctx,x) */
  51920. #if 0 /* enable manually for dumping */
  51921. #define DUK__DUMP_ISPEC(compctx,ispec) do { duk__dump_ispec((compctx), (ispec)); } while (0)
  51922. #define DUK__DUMP_IVALUE(compctx,ivalue) do { duk__dump_ivalue((compctx), (ivalue)); } while (0)
  51923. DUK_LOCAL void duk__dump_ispec(duk_compiler_ctx *comp_ctx, duk_ispec *x) {
  51924. DUK_D(DUK_DPRINT("ispec dump: t=%ld regconst=0x%08lx, valstack_idx=%ld, value=%!T",
  51925. (long) x->t, (unsigned long) x->regconst, (long) x->valstack_idx,
  51926. duk_get_tval((duk_context *) comp_ctx->thr, x->valstack_idx)));
  51927. }
  51928. DUK_LOCAL void duk__dump_ivalue(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  51929. DUK_D(DUK_DPRINT("ivalue dump: t=%ld op=%ld "
  51930. "x1={t=%ld regconst=0x%08lx valstack_idx=%ld value=%!T} "
  51931. "x2={t=%ld regconst=0x%08lx valstack_idx=%ld value=%!T}",
  51932. (long) x->t, (long) x->op,
  51933. (long) x->x1.t, (unsigned long) x->x1.regconst, (long) x->x1.valstack_idx,
  51934. duk_get_tval((duk_context *) comp_ctx->thr, x->x1.valstack_idx),
  51935. (long) x->x2.t, (unsigned long) x->x2.regconst, (long) x->x2.valstack_idx,
  51936. duk_get_tval((duk_context *) comp_ctx->thr, x->x2.valstack_idx)));
  51937. }
  51938. #else
  51939. #define DUK__DUMP_ISPEC(comp_ctx,x) do {} while (0)
  51940. #define DUK__DUMP_IVALUE(comp_ctx,x) do {} while (0)
  51941. #endif
  51942. DUK_LOCAL void duk__copy_ispec(duk_compiler_ctx *comp_ctx, duk_ispec *src, duk_ispec *dst) {
  51943. duk_context *ctx = (duk_context *) comp_ctx->thr;
  51944. dst->t = src->t;
  51945. dst->regconst = src->regconst;
  51946. duk_copy(ctx, src->valstack_idx, dst->valstack_idx);
  51947. }
  51948. DUK_LOCAL void duk__copy_ivalue(duk_compiler_ctx *comp_ctx, duk_ivalue *src, duk_ivalue *dst) {
  51949. duk_context *ctx = (duk_context *) comp_ctx->thr;
  51950. dst->t = src->t;
  51951. dst->op = src->op;
  51952. dst->x1.t = src->x1.t;
  51953. dst->x1.regconst = src->x1.regconst;
  51954. dst->x2.t = src->x2.t;
  51955. dst->x2.regconst = src->x2.regconst;
  51956. duk_copy(ctx, src->x1.valstack_idx, dst->x1.valstack_idx);
  51957. duk_copy(ctx, src->x2.valstack_idx, dst->x2.valstack_idx);
  51958. }
  51959. /* XXX: to util */
  51960. DUK_LOCAL duk_bool_t duk__is_whole_get_int32(duk_double_t x, duk_int32_t *ival) {
  51961. duk_small_int_t c;
  51962. duk_int32_t t;
  51963. c = DUK_FPCLASSIFY(x);
  51964. if (c == DUK_FP_NORMAL || (c == DUK_FP_ZERO && !DUK_SIGNBIT(x))) {
  51965. /* Don't allow negative zero as it will cause trouble with
  51966. * LDINT+LDINTX. But positive zero is OK.
  51967. */
  51968. t = (duk_int32_t) x;
  51969. if ((duk_double_t) t == x) {
  51970. *ival = t;
  51971. return 1;
  51972. }
  51973. }
  51974. return 0;
  51975. }
  51976. DUK_LOCAL duk_reg_t duk__alloctemps(duk_compiler_ctx *comp_ctx, duk_small_int_t num) {
  51977. duk_reg_t res;
  51978. res = comp_ctx->curr_func.temp_next;
  51979. comp_ctx->curr_func.temp_next += num;
  51980. if (comp_ctx->curr_func.temp_next > DUK__MAX_TEMPS) { /* == DUK__MAX_TEMPS is OK */
  51981. DUK_ERROR(comp_ctx->thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_TEMP_LIMIT);
  51982. }
  51983. /* maintain highest 'used' temporary, needed to figure out nregs of function */
  51984. if (comp_ctx->curr_func.temp_next > comp_ctx->curr_func.temp_max) {
  51985. comp_ctx->curr_func.temp_max = comp_ctx->curr_func.temp_next;
  51986. }
  51987. return res;
  51988. }
  51989. DUK_LOCAL duk_reg_t duk__alloctemp(duk_compiler_ctx *comp_ctx) {
  51990. return duk__alloctemps(comp_ctx, 1);
  51991. }
  51992. DUK_LOCAL void duk__settemp_checkmax(duk_compiler_ctx *comp_ctx, duk_reg_t temp_next) {
  51993. comp_ctx->curr_func.temp_next = temp_next;
  51994. if (temp_next > comp_ctx->curr_func.temp_max) {
  51995. comp_ctx->curr_func.temp_max = temp_next;
  51996. }
  51997. }
  51998. /* get const for value at valstack top */
  51999. DUK_LOCAL duk_regconst_t duk__getconst(duk_compiler_ctx *comp_ctx) {
  52000. duk_hthread *thr = comp_ctx->thr;
  52001. duk_context *ctx = (duk_context *) thr;
  52002. duk_compiler_func *f = &comp_ctx->curr_func;
  52003. duk_tval *tv1;
  52004. duk_int_t i, n, n_check;
  52005. n = (duk_int_t) duk_get_length(ctx, f->consts_idx);
  52006. tv1 = duk_get_tval(ctx, -1);
  52007. DUK_ASSERT(tv1 != NULL);
  52008. #if defined(DUK_USE_FASTINT)
  52009. /* Explicit check for fastint downgrade. */
  52010. DUK_TVAL_CHKFAST_INPLACE(tv1);
  52011. #endif
  52012. /* Sanity workaround for handling functions with a large number of
  52013. * constants at least somewhat reasonably. Otherwise checking whether
  52014. * we already have the constant would grow very slow (as it is O(N^2)).
  52015. */
  52016. n_check = (n > DUK__GETCONST_MAX_CONSTS_CHECK ? DUK__GETCONST_MAX_CONSTS_CHECK : n);
  52017. for (i = 0; i < n_check; i++) {
  52018. duk_tval *tv2 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, f->h_consts, i);
  52019. /* Strict equality is NOT enough, because we cannot use the same
  52020. * constant for e.g. +0 and -0.
  52021. */
  52022. if (duk_js_samevalue(tv1, tv2)) {
  52023. DUK_DDD(DUK_DDDPRINT("reused existing constant for %!T -> const index %ld",
  52024. (duk_tval *) tv1, (long) i));
  52025. duk_pop(ctx);
  52026. return (duk_regconst_t) (i | DUK__CONST_MARKER);
  52027. }
  52028. }
  52029. if (n > DUK__MAX_CONSTS) {
  52030. DUK_ERROR(comp_ctx->thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_CONST_LIMIT);
  52031. }
  52032. DUK_DDD(DUK_DDDPRINT("allocating new constant for %!T -> const index %ld",
  52033. (duk_tval *) tv1, (long) n));
  52034. (void) duk_put_prop_index(ctx, f->consts_idx, n); /* invalidates tv1, tv2 */
  52035. return (duk_regconst_t) (n | DUK__CONST_MARKER);
  52036. }
  52037. /* Get the value represented by an duk_ispec to a register or constant.
  52038. * The caller can control the result by indicating whether or not:
  52039. *
  52040. * (1) a constant is allowed (sometimes the caller needs the result to
  52041. * be in a register)
  52042. *
  52043. * (2) a temporary register is required (usually when caller requires
  52044. * the register to be safely mutable; normally either a bound
  52045. * register or a temporary register are both OK)
  52046. *
  52047. * (3) a forced register target needs to be used
  52048. *
  52049. * Bytecode may be emitted to generate the necessary value. The return
  52050. * value is either a register or a constant.
  52051. */
  52052. DUK_LOCAL
  52053. duk_regconst_t duk__ispec_toregconst_raw(duk_compiler_ctx *comp_ctx,
  52054. duk_ispec *x,
  52055. duk_reg_t forced_reg,
  52056. duk_small_uint_t flags) {
  52057. duk_hthread *thr = comp_ctx->thr;
  52058. duk_context *ctx = (duk_context *) thr;
  52059. DUK_DDD(DUK_DDDPRINT("duk__ispec_toregconst_raw(): x={%ld:%ld:%!T}, "
  52060. "forced_reg=%ld, flags 0x%08lx: allow_const=%ld require_temp=%ld require_short=%ld",
  52061. (long) x->t,
  52062. (long) x->regconst,
  52063. (duk_tval *) duk_get_tval(ctx, x->valstack_idx),
  52064. (long) forced_reg,
  52065. (unsigned long) flags,
  52066. (long) ((flags & DUK__IVAL_FLAG_ALLOW_CONST) ? 1 : 0),
  52067. (long) ((flags & DUK__IVAL_FLAG_REQUIRE_TEMP) ? 1 : 0),
  52068. (long) ((flags & DUK__IVAL_FLAG_REQUIRE_SHORT) ? 1 : 0)));
  52069. switch (x->t) {
  52070. case DUK_ISPEC_VALUE: {
  52071. duk_tval *tv;
  52072. tv = duk_get_tval(ctx, x->valstack_idx);
  52073. DUK_ASSERT(tv != NULL);
  52074. switch (DUK_TVAL_GET_TAG(tv)) {
  52075. case DUK_TAG_UNDEFINED: {
  52076. /* Note: although there is no 'undefined' literal, undefined
  52077. * values can occur during compilation as a result of e.g.
  52078. * the 'void' operator.
  52079. */
  52080. duk_reg_t dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  52081. duk__emit_extraop_bc(comp_ctx, DUK_EXTRAOP_LDUNDEF, (duk_regconst_t) dest);
  52082. return (duk_regconst_t) dest;
  52083. }
  52084. case DUK_TAG_NULL: {
  52085. duk_reg_t dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  52086. duk__emit_extraop_bc(comp_ctx, DUK_EXTRAOP_LDNULL, (duk_regconst_t) dest);
  52087. return (duk_regconst_t) dest;
  52088. }
  52089. case DUK_TAG_BOOLEAN: {
  52090. duk_reg_t dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  52091. duk__emit_extraop_bc(comp_ctx,
  52092. (DUK_TVAL_GET_BOOLEAN(tv) ? DUK_EXTRAOP_LDTRUE : DUK_EXTRAOP_LDFALSE),
  52093. (duk_regconst_t) dest);
  52094. return (duk_regconst_t) dest;
  52095. }
  52096. case DUK_TAG_POINTER: {
  52097. DUK_UNREACHABLE();
  52098. break;
  52099. }
  52100. case DUK_TAG_STRING: {
  52101. duk_hstring *h;
  52102. duk_reg_t dest;
  52103. duk_regconst_t constidx;
  52104. h = DUK_TVAL_GET_STRING(tv);
  52105. DUK_UNREF(h);
  52106. DUK_ASSERT(h != NULL);
  52107. #if 0 /* XXX: to be implemented? */
  52108. /* Use special opcodes to load short strings */
  52109. if (DUK_HSTRING_GET_BYTELEN(h) <= 2) {
  52110. /* Encode into a single opcode (18 bits can encode 1-2 bytes + length indicator) */
  52111. } else if (DUK_HSTRING_GET_BYTELEN(h) <= 6) {
  52112. /* Encode into a double constant (53 bits can encode 6*8 = 48 bits + 3-bit length */
  52113. }
  52114. #endif
  52115. duk_dup(ctx, x->valstack_idx);
  52116. constidx = duk__getconst(comp_ctx);
  52117. if (flags & DUK__IVAL_FLAG_ALLOW_CONST) {
  52118. return constidx;
  52119. }
  52120. dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  52121. duk__emit_a_bc(comp_ctx, DUK_OP_LDCONST, (duk_regconst_t) dest, constidx);
  52122. return (duk_regconst_t) dest;
  52123. }
  52124. case DUK_TAG_OBJECT: {
  52125. DUK_UNREACHABLE();
  52126. break;
  52127. }
  52128. case DUK_TAG_BUFFER: {
  52129. DUK_UNREACHABLE();
  52130. break;
  52131. }
  52132. case DUK_TAG_LIGHTFUNC: {
  52133. DUK_UNREACHABLE();
  52134. break;
  52135. }
  52136. #if defined(DUK_USE_FASTINT)
  52137. case DUK_TAG_FASTINT:
  52138. #endif
  52139. default: {
  52140. /* number */
  52141. duk_reg_t dest;
  52142. duk_regconst_t constidx;
  52143. duk_double_t dval;
  52144. duk_int32_t ival;
  52145. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  52146. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  52147. dval = DUK_TVAL_GET_NUMBER(tv);
  52148. if (!(flags & DUK__IVAL_FLAG_ALLOW_CONST)) {
  52149. /* A number can be loaded either through a constant, using
  52150. * LDINT, or using LDINT+LDINTX. LDINT is always a size win,
  52151. * LDINT+LDINTX is not if the constant is used multiple times.
  52152. * Currently always prefer LDINT+LDINTX over a double constant.
  52153. */
  52154. if (duk__is_whole_get_int32(dval, &ival)) {
  52155. dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  52156. duk__emit_load_int32(comp_ctx, dest, ival);
  52157. return (duk_regconst_t) dest;
  52158. }
  52159. }
  52160. duk_dup(ctx, x->valstack_idx);
  52161. constidx = duk__getconst(comp_ctx);
  52162. if (flags & DUK__IVAL_FLAG_ALLOW_CONST) {
  52163. return constidx;
  52164. } else {
  52165. dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  52166. duk__emit_a_bc(comp_ctx, DUK_OP_LDCONST, (duk_regconst_t) dest, constidx);
  52167. return (duk_regconst_t) dest;
  52168. }
  52169. }
  52170. } /* end switch */
  52171. }
  52172. case DUK_ISPEC_REGCONST: {
  52173. if (forced_reg >= 0) {
  52174. if (x->regconst & DUK__CONST_MARKER) {
  52175. duk__emit_a_bc(comp_ctx, DUK_OP_LDCONST, forced_reg, x->regconst);
  52176. } else if (x->regconst != (duk_regconst_t) forced_reg) {
  52177. duk__emit_a_bc(comp_ctx, DUK_OP_LDREG, forced_reg, x->regconst);
  52178. } else {
  52179. ; /* already in correct reg */
  52180. }
  52181. return (duk_regconst_t) forced_reg;
  52182. }
  52183. DUK_ASSERT(forced_reg < 0);
  52184. if (x->regconst & DUK__CONST_MARKER) {
  52185. if (!(flags & DUK__IVAL_FLAG_ALLOW_CONST)) {
  52186. duk_reg_t dest = DUK__ALLOCTEMP(comp_ctx);
  52187. duk__emit_a_bc(comp_ctx, DUK_OP_LDCONST, (duk_regconst_t) dest, x->regconst);
  52188. return (duk_regconst_t) dest;
  52189. }
  52190. return x->regconst;
  52191. }
  52192. DUK_ASSERT(forced_reg < 0 && !(x->regconst & DUK__CONST_MARKER));
  52193. if ((flags & DUK__IVAL_FLAG_REQUIRE_TEMP) && !DUK__ISTEMP(comp_ctx, x->regconst)) {
  52194. duk_reg_t dest = DUK__ALLOCTEMP(comp_ctx);
  52195. duk__emit_a_bc(comp_ctx, DUK_OP_LDREG, (duk_regconst_t) dest, x->regconst);
  52196. return (duk_regconst_t) dest;
  52197. }
  52198. return x->regconst;
  52199. }
  52200. default: {
  52201. break;
  52202. }
  52203. }
  52204. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR);
  52205. return 0;
  52206. }
  52207. DUK_LOCAL void duk__ispec_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ispec *x, duk_reg_t forced_reg) {
  52208. DUK_ASSERT(forced_reg >= 0);
  52209. (void) duk__ispec_toregconst_raw(comp_ctx, x, forced_reg, 0 /*flags*/);
  52210. }
  52211. /* Coerce an duk_ivalue to a 'plain' value by generating the necessary
  52212. * arithmetic operations, property access, or variable access bytecode.
  52213. * The duk_ivalue argument ('x') is converted into a plain value as a
  52214. * side effect.
  52215. */
  52216. DUK_LOCAL void duk__ivalue_toplain_raw(duk_compiler_ctx *comp_ctx, duk_ivalue *x, duk_reg_t forced_reg) {
  52217. duk_hthread *thr = comp_ctx->thr;
  52218. duk_context *ctx = (duk_context *) thr;
  52219. DUK_DDD(DUK_DDDPRINT("duk__ivalue_toplain_raw(): x={t=%ld,op=%ld,x1={%ld:%ld:%!T},x2={%ld:%ld:%!T}}, "
  52220. "forced_reg=%ld",
  52221. (long) x->t, (long) x->op,
  52222. (long) x->x1.t, (long) x->x1.regconst,
  52223. (duk_tval *) duk_get_tval(ctx, x->x1.valstack_idx),
  52224. (long) x->x2.t, (long) x->x2.regconst,
  52225. (duk_tval *) duk_get_tval(ctx, x->x2.valstack_idx),
  52226. (long) forced_reg));
  52227. switch (x->t) {
  52228. case DUK_IVAL_PLAIN: {
  52229. return;
  52230. }
  52231. /* XXX: support unary arithmetic ivalues (useful?) */
  52232. case DUK_IVAL_ARITH:
  52233. case DUK_IVAL_ARITH_EXTRAOP: {
  52234. duk_regconst_t arg1;
  52235. duk_regconst_t arg2;
  52236. duk_reg_t dest;
  52237. duk_tval *tv1;
  52238. duk_tval *tv2;
  52239. DUK_DDD(DUK_DDDPRINT("arith to plain conversion"));
  52240. /* inline arithmetic check for constant values */
  52241. /* XXX: use the exactly same arithmetic function here as in executor */
  52242. if (x->x1.t == DUK_ISPEC_VALUE && x->x2.t == DUK_ISPEC_VALUE && x->t == DUK_IVAL_ARITH) {
  52243. tv1 = duk_get_tval(ctx, x->x1.valstack_idx);
  52244. tv2 = duk_get_tval(ctx, x->x2.valstack_idx);
  52245. DUK_ASSERT(tv1 != NULL);
  52246. DUK_ASSERT(tv2 != NULL);
  52247. DUK_DDD(DUK_DDDPRINT("arith: tv1=%!T, tv2=%!T",
  52248. (duk_tval *) tv1,
  52249. (duk_tval *) tv2));
  52250. if (DUK_TVAL_IS_NUMBER(tv1) && DUK_TVAL_IS_NUMBER(tv2)) {
  52251. duk_double_t d1 = DUK_TVAL_GET_NUMBER(tv1);
  52252. duk_double_t d2 = DUK_TVAL_GET_NUMBER(tv2);
  52253. duk_double_t d3;
  52254. duk_bool_t accept = 1;
  52255. DUK_DDD(DUK_DDDPRINT("arith inline check: d1=%lf, d2=%lf, op=%ld",
  52256. (double) d1, (double) d2, (long) x->op));
  52257. switch (x->op) {
  52258. case DUK_OP_ADD: d3 = d1 + d2; break;
  52259. case DUK_OP_SUB: d3 = d1 - d2; break;
  52260. case DUK_OP_MUL: d3 = d1 * d2; break;
  52261. case DUK_OP_DIV: d3 = d1 / d2; break;
  52262. default: accept = 0; break;
  52263. }
  52264. if (accept) {
  52265. duk_double_union du;
  52266. du.d = d3;
  52267. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  52268. d3 = du.d;
  52269. x->t = DUK_IVAL_PLAIN;
  52270. DUK_ASSERT(x->x1.t == DUK_ISPEC_VALUE);
  52271. DUK_TVAL_SET_NUMBER(tv1, d3); /* old value is number: no refcount */
  52272. return;
  52273. }
  52274. } else if (x->op == DUK_OP_ADD && DUK_TVAL_IS_STRING(tv1) && DUK_TVAL_IS_STRING(tv2)) {
  52275. /* inline string concatenation */
  52276. duk_dup(ctx, x->x1.valstack_idx);
  52277. duk_dup(ctx, x->x2.valstack_idx);
  52278. duk_concat(ctx, 2);
  52279. duk_replace(ctx, x->x1.valstack_idx);
  52280. x->t = DUK_IVAL_PLAIN;
  52281. DUK_ASSERT(x->x1.t == DUK_ISPEC_VALUE);
  52282. return;
  52283. }
  52284. }
  52285. arg1 = duk__ispec_toregconst_raw(comp_ctx, &x->x1, -1, DUK__IVAL_FLAG_ALLOW_CONST | DUK__IVAL_FLAG_REQUIRE_SHORT /*flags*/);
  52286. arg2 = duk__ispec_toregconst_raw(comp_ctx, &x->x2, -1, DUK__IVAL_FLAG_ALLOW_CONST | DUK__IVAL_FLAG_REQUIRE_SHORT /*flags*/);
  52287. /* If forced reg, use it as destination. Otherwise try to
  52288. * use either coerced ispec if it is a temporary.
  52289. *
  52290. * When using extraops, avoid reusing arg2 as dest because that
  52291. * would lead to an LDREG shuffle below. We still can't guarantee
  52292. * dest != arg2 because we may have a forced_reg.
  52293. */
  52294. if (forced_reg >= 0) {
  52295. dest = forced_reg;
  52296. } else if (DUK__ISTEMP(comp_ctx, arg1)) {
  52297. dest = (duk_reg_t) arg1;
  52298. } else if (DUK__ISTEMP(comp_ctx, arg2) && x->t != DUK_IVAL_ARITH_EXTRAOP) {
  52299. dest = (duk_reg_t) arg2;
  52300. } else {
  52301. dest = DUK__ALLOCTEMP(comp_ctx);
  52302. }
  52303. /* Extraop arithmetic opcodes must have destination same as
  52304. * first source. If second source matches destination we need
  52305. * a temporary register to avoid clobbering the second source.
  52306. *
  52307. * XXX: change calling code to avoid this situation in most cases.
  52308. */
  52309. if (x->t == DUK_IVAL_ARITH_EXTRAOP) {
  52310. if (!(DUK__ISREG(comp_ctx, arg1) && (duk_reg_t) arg1 == dest)) {
  52311. if (DUK__ISREG(comp_ctx, arg2) && (duk_reg_t) arg2 == dest) {
  52312. /* arg2 would be clobbered so reassign it to a temp. */
  52313. duk_reg_t tempreg;
  52314. tempreg = DUK__ALLOCTEMP(comp_ctx);
  52315. duk__emit_a_bc(comp_ctx, DUK_OP_LDREG, tempreg, arg2);
  52316. arg2 = tempreg;
  52317. }
  52318. if (DUK__ISREG(comp_ctx, arg1)) {
  52319. duk__emit_a_bc(comp_ctx, DUK_OP_LDREG, dest, arg1);
  52320. } else {
  52321. DUK_ASSERT(DUK__ISCONST(comp_ctx, arg1));
  52322. duk__emit_a_bc(comp_ctx, DUK_OP_LDCONST, dest, arg1);
  52323. }
  52324. }
  52325. /* Note: special DUK__EMIT_FLAG_B_IS_TARGETSOURCE
  52326. * used to indicate that B is both a source and a
  52327. * target register. When shuffled, it needs to be
  52328. * both input and output shuffled.
  52329. */
  52330. DUK_ASSERT(DUK__ISREG(comp_ctx, dest));
  52331. duk__emit_extraop_b_c(comp_ctx,
  52332. x->op | DUK__EMIT_FLAG_B_IS_TARGET |
  52333. DUK__EMIT_FLAG_B_IS_TARGETSOURCE,
  52334. (duk_regconst_t) dest,
  52335. (duk_regconst_t) arg2);
  52336. } else {
  52337. DUK_ASSERT(DUK__ISREG(comp_ctx, dest));
  52338. duk__emit_a_b_c(comp_ctx, x->op, (duk_regconst_t) dest, arg1, arg2);
  52339. }
  52340. x->t = DUK_IVAL_PLAIN;
  52341. x->x1.t = DUK_ISPEC_REGCONST;
  52342. x->x1.regconst = (duk_regconst_t) dest;
  52343. return;
  52344. }
  52345. case DUK_IVAL_PROP: {
  52346. /* XXX: very similar to DUK_IVAL_ARITH - merge? */
  52347. duk_regconst_t arg1;
  52348. duk_regconst_t arg2;
  52349. duk_reg_t dest;
  52350. /* Need a short reg/const, does not have to be a mutable temp. */
  52351. arg1 = duk__ispec_toregconst_raw(comp_ctx, &x->x1, -1, DUK__IVAL_FLAG_ALLOW_CONST | DUK__IVAL_FLAG_REQUIRE_SHORT /*flags*/);
  52352. arg2 = duk__ispec_toregconst_raw(comp_ctx, &x->x2, -1, DUK__IVAL_FLAG_ALLOW_CONST | DUK__IVAL_FLAG_REQUIRE_SHORT /*flags*/);
  52353. /* Pick a destination register. If either base value or key
  52354. * happens to be a temp value, reuse it as the destination.
  52355. *
  52356. * XXX: The temp must be a "mutable" one, i.e. such that no
  52357. * other expression is using it anymore. Here this should be
  52358. * the case because the value of a property access expression
  52359. * is neither the base nor the key, but the lookup result.
  52360. */
  52361. if (forced_reg >= 0) {
  52362. dest = forced_reg;
  52363. } else if (DUK__ISTEMP(comp_ctx, arg1)) {
  52364. dest = (duk_reg_t) arg1;
  52365. } else if (DUK__ISTEMP(comp_ctx, arg2)) {
  52366. dest = (duk_reg_t) arg2;
  52367. } else {
  52368. dest = DUK__ALLOCTEMP(comp_ctx);
  52369. }
  52370. duk__emit_a_b_c(comp_ctx, DUK_OP_GETPROP, (duk_regconst_t) dest, arg1, arg2);
  52371. x->t = DUK_IVAL_PLAIN;
  52372. x->x1.t = DUK_ISPEC_REGCONST;
  52373. x->x1.regconst = (duk_regconst_t) dest;
  52374. return;
  52375. }
  52376. case DUK_IVAL_VAR: {
  52377. /* x1 must be a string */
  52378. duk_reg_t dest;
  52379. duk_reg_t reg_varbind;
  52380. duk_regconst_t rc_varname;
  52381. DUK_ASSERT(x->x1.t == DUK_ISPEC_VALUE);
  52382. duk_dup(ctx, x->x1.valstack_idx);
  52383. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  52384. x->t = DUK_IVAL_PLAIN;
  52385. x->x1.t = DUK_ISPEC_REGCONST;
  52386. x->x1.regconst = (duk_regconst_t) reg_varbind;
  52387. } else {
  52388. dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  52389. duk__emit_a_bc(comp_ctx, DUK_OP_GETVAR, (duk_regconst_t) dest, rc_varname);
  52390. x->t = DUK_IVAL_PLAIN;
  52391. x->x1.t = DUK_ISPEC_REGCONST;
  52392. x->x1.regconst = (duk_regconst_t) dest;
  52393. }
  52394. return;
  52395. }
  52396. case DUK_IVAL_NONE:
  52397. default: {
  52398. break;
  52399. }
  52400. }
  52401. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR);
  52402. return;
  52403. }
  52404. /* evaluate to plain value, no forced register (temp/bound reg both ok) */
  52405. DUK_LOCAL void duk__ivalue_toplain(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  52406. duk__ivalue_toplain_raw(comp_ctx, x, -1 /*forced_reg*/);
  52407. }
  52408. /* evaluate to final form (e.g. coerce GETPROP to code), throw away temp */
  52409. DUK_LOCAL void duk__ivalue_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  52410. duk_reg_t temp;
  52411. /* If duk__ivalue_toplain_raw() allocates a temp, forget it and
  52412. * restore next temp state.
  52413. */
  52414. temp = DUK__GETTEMP(comp_ctx);
  52415. duk__ivalue_toplain_raw(comp_ctx, x, -1 /*forced_reg*/);
  52416. DUK__SETTEMP(comp_ctx, temp);
  52417. }
  52418. /* Coerce an duk_ivalue to a register or constant; result register may
  52419. * be a temp or a bound register.
  52420. *
  52421. * The duk_ivalue argument ('x') is converted into a regconst as a
  52422. * side effect.
  52423. */
  52424. DUK_LOCAL
  52425. duk_regconst_t duk__ivalue_toregconst_raw(duk_compiler_ctx *comp_ctx,
  52426. duk_ivalue *x,
  52427. duk_reg_t forced_reg,
  52428. duk_small_uint_t flags) {
  52429. duk_hthread *thr = comp_ctx->thr;
  52430. duk_context *ctx = (duk_context *) thr;
  52431. duk_regconst_t reg;
  52432. DUK_UNREF(thr);
  52433. DUK_UNREF(ctx);
  52434. DUK_DDD(DUK_DDDPRINT("duk__ivalue_toregconst_raw(): x={t=%ld,op=%ld,x1={%ld:%ld:%!T},x2={%ld:%ld:%!T}}, "
  52435. "forced_reg=%ld, flags 0x%08lx: allow_const=%ld require_temp=%ld require_short=%ld",
  52436. (long) x->t, (long) x->op,
  52437. (long) x->x1.t, (long) x->x1.regconst,
  52438. (duk_tval *) duk_get_tval(ctx, x->x1.valstack_idx),
  52439. (long) x->x2.t, (long) x->x2.regconst,
  52440. (duk_tval *) duk_get_tval(ctx, x->x2.valstack_idx),
  52441. (long) forced_reg,
  52442. (unsigned long) flags,
  52443. (long) ((flags & DUK__IVAL_FLAG_ALLOW_CONST) ? 1 : 0),
  52444. (long) ((flags & DUK__IVAL_FLAG_REQUIRE_TEMP) ? 1 : 0),
  52445. (long) ((flags & DUK__IVAL_FLAG_REQUIRE_SHORT) ? 1 : 0)));
  52446. /* first coerce to a plain value */
  52447. duk__ivalue_toplain_raw(comp_ctx, x, forced_reg);
  52448. DUK_ASSERT(x->t == DUK_IVAL_PLAIN);
  52449. /* then to a register */
  52450. reg = duk__ispec_toregconst_raw(comp_ctx, &x->x1, forced_reg, flags);
  52451. x->x1.t = DUK_ISPEC_REGCONST;
  52452. x->x1.regconst = reg;
  52453. return reg;
  52454. }
  52455. DUK_LOCAL duk_reg_t duk__ivalue_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  52456. return duk__ivalue_toregconst_raw(comp_ctx, x, -1, 0 /*flags*/);
  52457. }
  52458. #if 0 /* unused */
  52459. DUK_LOCAL duk_reg_t duk__ivalue_totemp(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  52460. return duk__ivalue_toregconst_raw(comp_ctx, x, -1, DUK__IVAL_FLAG_REQUIRE_TEMP /*flags*/);
  52461. }
  52462. #endif
  52463. DUK_LOCAL void duk__ivalue_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ivalue *x, duk_int_t forced_reg) {
  52464. DUK_ASSERT(forced_reg >= 0);
  52465. (void) duk__ivalue_toregconst_raw(comp_ctx, x, forced_reg, 0 /*flags*/);
  52466. }
  52467. DUK_LOCAL duk_regconst_t duk__ivalue_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  52468. return duk__ivalue_toregconst_raw(comp_ctx, x, -1, DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  52469. }
  52470. DUK_LOCAL duk_regconst_t duk__ivalue_totempconst(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  52471. return duk__ivalue_toregconst_raw(comp_ctx, x, -1, DUK__IVAL_FLAG_ALLOW_CONST | DUK__IVAL_FLAG_REQUIRE_TEMP /*flags*/);
  52472. }
  52473. /* The issues below can be solved with better flags */
  52474. /* XXX: many operations actually want toforcedtemp() -- brand new temp? */
  52475. /* XXX: need a toplain_ignore() which will only coerce a value to a temp
  52476. * register if it might have a side effect. Side-effect free values do not
  52477. * need to be coerced.
  52478. */
  52479. /*
  52480. * Identifier handling
  52481. */
  52482. DUK_LOCAL duk_reg_t duk__lookup_active_register_binding(duk_compiler_ctx *comp_ctx) {
  52483. duk_hthread *thr = comp_ctx->thr;
  52484. duk_context *ctx = (duk_context *) thr;
  52485. duk_hstring *h_varname;
  52486. duk_reg_t ret;
  52487. DUK_DDD(DUK_DDDPRINT("resolving identifier reference to '%!T'",
  52488. (duk_tval *) duk_get_tval(ctx, -1)));
  52489. /*
  52490. * Special name handling
  52491. */
  52492. h_varname = duk_get_hstring(ctx, -1);
  52493. DUK_ASSERT(h_varname != NULL);
  52494. if (h_varname == DUK_HTHREAD_STRING_LC_ARGUMENTS(thr)) {
  52495. DUK_DDD(DUK_DDDPRINT("flagging function as accessing 'arguments'"));
  52496. comp_ctx->curr_func.id_access_arguments = 1;
  52497. }
  52498. /*
  52499. * Inside one or more 'with' statements fall back to slow path always.
  52500. * (See e.g. test-stmt-with.js.)
  52501. */
  52502. if (comp_ctx->curr_func.with_depth > 0) {
  52503. DUK_DDD(DUK_DDDPRINT("identifier lookup inside a 'with' -> fall back to slow path"));
  52504. goto slow_path;
  52505. }
  52506. /*
  52507. * Any catch bindings ("catch (e)") also affect identifier binding.
  52508. *
  52509. * Currently, the varmap is modified for the duration of the catch
  52510. * clause to ensure any identifier accesses with the catch variable
  52511. * name will use slow path.
  52512. */
  52513. duk_get_prop(ctx, comp_ctx->curr_func.varmap_idx);
  52514. if (duk_is_number(ctx, -1)) {
  52515. ret = duk_to_int(ctx, -1);
  52516. duk_pop(ctx);
  52517. } else {
  52518. duk_pop(ctx);
  52519. goto slow_path;
  52520. }
  52521. DUK_DDD(DUK_DDDPRINT("identifier lookup -> reg %ld", (long) ret));
  52522. return ret;
  52523. slow_path:
  52524. DUK_DDD(DUK_DDDPRINT("identifier lookup -> slow path"));
  52525. comp_ctx->curr_func.id_access_slow = 1;
  52526. return (duk_reg_t) -1;
  52527. }
  52528. /* Lookup an identifier name in the current varmap, indicating whether the
  52529. * identifier is register-bound and if not, allocating a constant for the
  52530. * identifier name. Returns 1 if register-bound, 0 otherwise. Caller can
  52531. * also check (out_reg_varbind >= 0) to check whether or not identifier is
  52532. * register bound. The caller must NOT use out_rc_varname at all unless
  52533. * return code is 0 or out_reg_varbind is < 0; this is becuase out_rc_varname
  52534. * is unsigned and doesn't have a "unused" / none value.
  52535. */
  52536. DUK_LOCAL duk_bool_t duk__lookup_lhs(duk_compiler_ctx *comp_ctx, duk_reg_t *out_reg_varbind, duk_regconst_t *out_rc_varname) {
  52537. duk_hthread *thr = comp_ctx->thr;
  52538. duk_context *ctx = (duk_context *) thr;
  52539. duk_reg_t reg_varbind;
  52540. duk_regconst_t rc_varname;
  52541. /* [ ... varname ] */
  52542. duk_dup_top(ctx);
  52543. reg_varbind = duk__lookup_active_register_binding(comp_ctx);
  52544. if (reg_varbind >= 0) {
  52545. *out_reg_varbind = reg_varbind;
  52546. *out_rc_varname = 0; /* duk_regconst_t is unsigned, so use 0 as dummy value (ignored by caller) */
  52547. duk_pop(ctx);
  52548. return 1;
  52549. } else {
  52550. rc_varname = duk__getconst(comp_ctx);
  52551. *out_reg_varbind = -1;
  52552. *out_rc_varname = rc_varname;
  52553. return 0;
  52554. }
  52555. }
  52556. /*
  52557. * Label handling
  52558. *
  52559. * Labels are initially added with flags prohibiting both break and continue.
  52560. * When the statement type is finally uncovered (after potentially multiple
  52561. * labels), all the labels are updated to allow/prohibit break and continue.
  52562. */
  52563. 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) {
  52564. duk_hthread *thr = comp_ctx->thr;
  52565. duk_context *ctx = (duk_context *) thr;
  52566. duk_size_t n;
  52567. duk_size_t new_size;
  52568. duk_uint8_t *p;
  52569. duk_labelinfo *li_start, *li;
  52570. /* Duplicate (shadowing) labels are not allowed, except for the empty
  52571. * labels (which are used as default labels for switch and iteration
  52572. * statements).
  52573. *
  52574. * We could also allow shadowing of non-empty pending labels without any
  52575. * other issues than breaking the required label shadowing requirements
  52576. * of the E5 specification, see Section 12.12.
  52577. */
  52578. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, comp_ctx->curr_func.h_labelinfos);
  52579. li_start = (duk_labelinfo *) (void *) p;
  52580. li = (duk_labelinfo *) (void *) (p + DUK_HBUFFER_GET_SIZE(comp_ctx->curr_func.h_labelinfos));
  52581. n = (duk_size_t) (li - li_start);
  52582. while (li > li_start) {
  52583. li--;
  52584. if (li->h_label == h_label && h_label != DUK_HTHREAD_STRING_EMPTY_STRING(thr)) {
  52585. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_DUPLICATE_LABEL);
  52586. }
  52587. }
  52588. duk_push_hstring(ctx, h_label);
  52589. DUK_ASSERT(n <= DUK_UARRIDX_MAX); /* label limits */
  52590. (void) duk_put_prop_index(ctx, comp_ctx->curr_func.labelnames_idx, (duk_uarridx_t) n);
  52591. new_size = (n + 1) * sizeof(duk_labelinfo);
  52592. duk_hbuffer_resize(thr, comp_ctx->curr_func.h_labelinfos, new_size);
  52593. /* XXX: spare handling, slow now */
  52594. /* relookup after possible realloc */
  52595. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, comp_ctx->curr_func.h_labelinfos);
  52596. li_start = (duk_labelinfo *) (void *) p;
  52597. DUK_UNREF(li_start); /* silence scan-build warning */
  52598. li = (duk_labelinfo *) (void *) (p + DUK_HBUFFER_GET_SIZE(comp_ctx->curr_func.h_labelinfos));
  52599. li--;
  52600. /* Labels can be used for iteration statements but also for other statements,
  52601. * in particular a label can be used for a block statement. All cases of a
  52602. * named label accept a 'break' so that flag is set here. Iteration statements
  52603. * also allow 'continue', so that flag is updated when we figure out the
  52604. * statement type.
  52605. */
  52606. li->flags = DUK_LABEL_FLAG_ALLOW_BREAK;
  52607. li->label_id = label_id;
  52608. li->h_label = h_label;
  52609. li->catch_depth = comp_ctx->curr_func.catch_depth; /* catch depth from current func */
  52610. li->pc_label = pc_label;
  52611. DUK_DDD(DUK_DDDPRINT("registered label: flags=0x%08lx, id=%ld, name=%!O, catch_depth=%ld, pc_label=%ld",
  52612. (unsigned long) li->flags, (long) li->label_id, (duk_heaphdr *) li->h_label,
  52613. (long) li->catch_depth, (long) li->pc_label));
  52614. }
  52615. /* Update all labels with matching label_id. */
  52616. DUK_LOCAL void duk__update_label_flags(duk_compiler_ctx *comp_ctx, duk_int_t label_id, duk_small_uint_t flags) {
  52617. duk_uint8_t *p;
  52618. duk_labelinfo *li_start, *li;
  52619. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(comp_ctx->thr->heap, comp_ctx->curr_func.h_labelinfos);
  52620. li_start = (duk_labelinfo *) (void *) p;
  52621. li = (duk_labelinfo *) (void *) (p + DUK_HBUFFER_GET_SIZE(comp_ctx->curr_func.h_labelinfos));
  52622. /* Match labels starting from latest; once label_id no longer matches, we can
  52623. * safely exit without checking the rest of the labels (only the topmost labels
  52624. * are ever updated).
  52625. */
  52626. while (li > li_start) {
  52627. li--;
  52628. if (li->label_id != label_id) {
  52629. break;
  52630. }
  52631. DUK_DDD(DUK_DDDPRINT("updating (overwriting) label flags for li=%p, label_id=%ld, flags=%ld",
  52632. (void *) li, (long) label_id, (long) flags));
  52633. li->flags = flags;
  52634. }
  52635. }
  52636. /* Lookup active label information. Break/continue distinction is necessary to handle switch
  52637. * statement related labels correctly: a switch will only catch a 'break', not a 'continue'.
  52638. *
  52639. * An explicit label cannot appear multiple times in the active set, but empty labels (unlabelled
  52640. * iteration and switch statements) can. A break will match the closest unlabelled or labelled
  52641. * statement. A continue will match the closest unlabelled or labelled iteration statement. It is
  52642. * a syntax error if a continue matches a labelled switch statement; because an explicit label cannot
  52643. * be duplicated, the continue cannot match any valid label outside the switch.
  52644. *
  52645. * A side effect of these rules is that a LABEL statement related to a switch should never actually
  52646. * catch a continue abrupt completion at run-time. Hence an INVALID opcode can be placed in the
  52647. * continue slot of the switch's LABEL statement.
  52648. */
  52649. /* XXX: awkward, especially the bunch of separate output values -> output struct? */
  52650. 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) {
  52651. duk_hthread *thr = comp_ctx->thr;
  52652. duk_context *ctx = (duk_context *) thr;
  52653. duk_uint8_t *p;
  52654. duk_labelinfo *li_start, *li_end, *li;
  52655. duk_bool_t match = 0;
  52656. DUK_DDD(DUK_DDDPRINT("looking up active label: label='%!O', is_break=%ld",
  52657. (duk_heaphdr *) h_label, (long) is_break));
  52658. DUK_UNREF(ctx);
  52659. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, comp_ctx->curr_func.h_labelinfos);
  52660. li_start = (duk_labelinfo *) (void *) p;
  52661. li_end = (duk_labelinfo *) (void *) (p + DUK_HBUFFER_GET_SIZE(comp_ctx->curr_func.h_labelinfos));
  52662. li = li_end;
  52663. /* Match labels starting from latest label because there can be duplicate empty
  52664. * labels in the label set.
  52665. */
  52666. while (li > li_start) {
  52667. li--;
  52668. if (li->h_label != h_label) {
  52669. DUK_DDD(DUK_DDDPRINT("labelinfo[%ld] ->'%!O' != %!O",
  52670. (long) (li - li_start),
  52671. (duk_heaphdr *) li->h_label,
  52672. (duk_heaphdr *) h_label));
  52673. continue;
  52674. }
  52675. DUK_DDD(DUK_DDDPRINT("labelinfo[%ld] -> '%!O' label name matches (still need to check type)",
  52676. (long) (li - li_start), (duk_heaphdr *) h_label));
  52677. /* currently all labels accept a break, so no explicit check for it now */
  52678. DUK_ASSERT(li->flags & DUK_LABEL_FLAG_ALLOW_BREAK);
  52679. if (is_break) {
  52680. /* break matches always */
  52681. match = 1;
  52682. break;
  52683. } else if (li->flags & DUK_LABEL_FLAG_ALLOW_CONTINUE) {
  52684. /* iteration statements allow continue */
  52685. match = 1;
  52686. break;
  52687. } else {
  52688. /* continue matched this label -- we can only continue if this is the empty
  52689. * label, for which duplication is allowed, and thus there is hope of
  52690. * finding a match deeper in the label stack.
  52691. */
  52692. if (h_label != DUK_HTHREAD_STRING_EMPTY_STRING(thr)) {
  52693. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_LABEL);
  52694. } else {
  52695. DUK_DDD(DUK_DDDPRINT("continue matched an empty label which does not "
  52696. "allow a continue -> continue lookup deeper in label stack"));
  52697. }
  52698. }
  52699. }
  52700. /* XXX: match flag is awkward, rework */
  52701. if (!match) {
  52702. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_LABEL);
  52703. }
  52704. DUK_DDD(DUK_DDDPRINT("label match: %!O -> label_id %ld, catch_depth=%ld, pc_label=%ld",
  52705. (duk_heaphdr *) h_label, (long) li->label_id,
  52706. (long) li->catch_depth, (long) li->pc_label));
  52707. *out_label_id = li->label_id;
  52708. *out_label_catch_depth = li->catch_depth;
  52709. *out_label_pc = li->pc_label;
  52710. *out_is_closest = (li == li_end - 1);
  52711. }
  52712. DUK_LOCAL void duk__reset_labels_to_length(duk_compiler_ctx *comp_ctx, duk_int_t len) {
  52713. duk_hthread *thr = comp_ctx->thr;
  52714. duk_context *ctx = (duk_context *) thr;
  52715. duk_size_t new_size;
  52716. /* XXX: duk_set_length */
  52717. new_size = sizeof(duk_labelinfo) * (duk_size_t) len;
  52718. duk_push_int(ctx, len);
  52719. duk_put_prop_stridx(ctx, comp_ctx->curr_func.labelnames_idx, DUK_STRIDX_LENGTH);
  52720. duk_hbuffer_resize(thr, comp_ctx->curr_func.h_labelinfos, new_size);
  52721. }
  52722. /*
  52723. * Expression parsing: duk__expr_nud(), duk__expr_led(), duk__expr_lbp(), and helpers.
  52724. *
  52725. * - duk__expr_nud(): ("null denotation"): process prev_token as a "start" of an expression (e.g. literal)
  52726. * - duk__expr_led(): ("left denotation"): process prev_token in the "middle" of an expression (e.g. operator)
  52727. * - duk__expr_lbp(): ("left-binding power"): return left-binding power of curr_token
  52728. */
  52729. /* object literal key tracking flags */
  52730. #define DUK__OBJ_LIT_KEY_PLAIN (1 << 0) /* key encountered as a plain property */
  52731. #define DUK__OBJ_LIT_KEY_GET (1 << 1) /* key encountered as a getter */
  52732. #define DUK__OBJ_LIT_KEY_SET (1 << 2) /* key encountered as a setter */
  52733. DUK_LOCAL void duk__nud_array_literal(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  52734. duk_hthread *thr = comp_ctx->thr;
  52735. duk_reg_t reg_obj; /* result reg */
  52736. duk_reg_t reg_temp; /* temp reg */
  52737. duk_reg_t temp_start; /* temp reg value for start of loop */
  52738. duk_small_uint_t max_init_values; /* max # of values initialized in one MPUTARR set */
  52739. duk_small_uint_t num_values; /* number of values in current MPUTARR set */
  52740. duk_uarridx_t curr_idx; /* current (next) array index */
  52741. duk_uarridx_t start_idx; /* start array index of current MPUTARR set */
  52742. duk_uarridx_t init_idx; /* last array index explicitly initialized, +1 */
  52743. duk_bool_t require_comma; /* next loop requires a comma */
  52744. /* DUK_TOK_LBRACKET already eaten, current token is right after that */
  52745. DUK_ASSERT(comp_ctx->prev_token.t == DUK_TOK_LBRACKET);
  52746. max_init_values = DUK__MAX_ARRAY_INIT_VALUES; /* XXX: depend on available temps? */
  52747. reg_obj = DUK__ALLOCTEMP(comp_ctx);
  52748. duk__emit_extraop_b_c(comp_ctx,
  52749. DUK_EXTRAOP_NEWARR | DUK__EMIT_FLAG_B_IS_TARGET,
  52750. reg_obj,
  52751. 0); /* XXX: patch initial size afterwards? */
  52752. temp_start = DUK__GETTEMP(comp_ctx);
  52753. /*
  52754. * Emit initializers in sets of maximum max_init_values.
  52755. * Corner cases such as single value initializers do not have
  52756. * special handling now.
  52757. *
  52758. * Elided elements must not be emitted as 'undefined' values,
  52759. * because such values would be enumerable (which is incorrect).
  52760. * Also note that trailing elisions must be reflected in the
  52761. * length of the final array but cause no elements to be actually
  52762. * inserted.
  52763. */
  52764. curr_idx = 0;
  52765. init_idx = 0; /* tracks maximum initialized index + 1 */
  52766. start_idx = 0;
  52767. require_comma = 0;
  52768. for (;;) {
  52769. num_values = 0;
  52770. DUK__SETTEMP(comp_ctx, temp_start);
  52771. if (comp_ctx->curr_token.t == DUK_TOK_RBRACKET) {
  52772. break;
  52773. }
  52774. for (;;) {
  52775. if (comp_ctx->curr_token.t == DUK_TOK_RBRACKET) {
  52776. /* the outer loop will recheck and exit */
  52777. break;
  52778. }
  52779. /* comma check */
  52780. if (require_comma) {
  52781. if (comp_ctx->curr_token.t == DUK_TOK_COMMA) {
  52782. /* comma after a value, expected */
  52783. duk__advance(comp_ctx);
  52784. require_comma = 0;
  52785. continue;
  52786. } else {
  52787. goto syntax_error;
  52788. }
  52789. } else {
  52790. if (comp_ctx->curr_token.t == DUK_TOK_COMMA) {
  52791. /* elision - flush */
  52792. curr_idx++;
  52793. duk__advance(comp_ctx);
  52794. /* if num_values > 0, MPUTARR emitted by outer loop after break */
  52795. break;
  52796. }
  52797. }
  52798. /* else an array initializer element */
  52799. /* initial index */
  52800. if (num_values == 0) {
  52801. start_idx = curr_idx;
  52802. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  52803. duk__emit_load_int32(comp_ctx, reg_temp, (duk_int32_t) start_idx);
  52804. }
  52805. reg_temp = DUK__ALLOCTEMP(comp_ctx); /* alloc temp just in case, to update max temp */
  52806. DUK__SETTEMP(comp_ctx, reg_temp);
  52807. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_COMMA /*rbp_flags*/, reg_temp /*forced_reg*/);
  52808. DUK__SETTEMP(comp_ctx, reg_temp + 1);
  52809. num_values++;
  52810. curr_idx++;
  52811. require_comma = 1;
  52812. if (num_values >= max_init_values) {
  52813. /* MPUTARR emitted by outer loop */
  52814. break;
  52815. }
  52816. }
  52817. if (num_values > 0) {
  52818. /* - A is a source register (it's not a write target, but used
  52819. * to identify the target object) but can be shuffled.
  52820. * - B cannot be shuffled normally because it identifies a range
  52821. * of registers, the emitter has special handling for this
  52822. * (the "no shuffle" flag must not be set).
  52823. * - C is a non-register number and cannot be shuffled, but
  52824. * never needs to be.
  52825. */
  52826. duk__emit_a_b_c(comp_ctx,
  52827. DUK_OP_MPUTARR |
  52828. DUK__EMIT_FLAG_NO_SHUFFLE_C |
  52829. DUK__EMIT_FLAG_A_IS_SOURCE,
  52830. (duk_regconst_t) reg_obj,
  52831. (duk_regconst_t) temp_start,
  52832. (duk_regconst_t) num_values);
  52833. init_idx = start_idx + num_values;
  52834. /* num_values and temp_start reset at top of outer loop */
  52835. }
  52836. }
  52837. DUK_ASSERT(comp_ctx->curr_token.t == DUK_TOK_RBRACKET);
  52838. duk__advance(comp_ctx);
  52839. DUK_DDD(DUK_DDDPRINT("array literal done, curridx=%ld, initidx=%ld",
  52840. (long) curr_idx, (long) init_idx));
  52841. /* trailing elisions? */
  52842. if (curr_idx > init_idx) {
  52843. /* yes, must set array length explicitly */
  52844. DUK_DDD(DUK_DDDPRINT("array literal has trailing elisions which affect its length"));
  52845. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  52846. duk__emit_load_int32(comp_ctx, reg_temp, (duk_int_t) curr_idx);
  52847. duk__emit_extraop_b_c(comp_ctx,
  52848. DUK_EXTRAOP_SETALEN,
  52849. (duk_regconst_t) reg_obj,
  52850. (duk_regconst_t) reg_temp);
  52851. }
  52852. DUK__SETTEMP(comp_ctx, temp_start);
  52853. res->t = DUK_IVAL_PLAIN;
  52854. res->x1.t = DUK_ISPEC_REGCONST;
  52855. res->x1.regconst = (duk_regconst_t) reg_obj;
  52856. return;
  52857. syntax_error:
  52858. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_ARRAY_LITERAL);
  52859. }
  52860. /* duplicate/invalid key checks; returns 1 if syntax error */
  52861. DUK_LOCAL duk_bool_t duk__nud_object_literal_key_check(duk_compiler_ctx *comp_ctx, duk_small_uint_t new_key_flags) {
  52862. duk_hthread *thr = comp_ctx->thr;
  52863. duk_context *ctx = (duk_context *) thr;
  52864. duk_small_uint_t key_flags;
  52865. /* [ ... key_obj key ] */
  52866. DUK_ASSERT(duk_is_string(ctx, -1));
  52867. /*
  52868. * 'key_obj' tracks keys encountered so far by associating an
  52869. * integer with flags with already encountered keys. The checks
  52870. * below implement E5 Section 11.1.5, step 4 for production:
  52871. *
  52872. * PropertyNameAndValueList: PropertyNameAndValueList , PropertyAssignment
  52873. */
  52874. duk_dup(ctx, -1); /* [ ... key_obj key key ] */
  52875. duk_get_prop(ctx, -3); /* [ ... key_obj key val ] */
  52876. key_flags = duk_to_int(ctx, -1);
  52877. duk_pop(ctx); /* [ ... key_obj key ] */
  52878. if (new_key_flags & DUK__OBJ_LIT_KEY_PLAIN) {
  52879. if ((key_flags & DUK__OBJ_LIT_KEY_PLAIN) && comp_ctx->curr_func.is_strict) {
  52880. /* step 4.a */
  52881. DUK_DDD(DUK_DDDPRINT("duplicate key: plain key appears twice in strict mode"));
  52882. return 1;
  52883. }
  52884. if (key_flags & (DUK__OBJ_LIT_KEY_GET | DUK__OBJ_LIT_KEY_SET)) {
  52885. /* step 4.c */
  52886. DUK_DDD(DUK_DDDPRINT("duplicate key: plain key encountered after setter/getter"));
  52887. return 1;
  52888. }
  52889. } else {
  52890. if (key_flags & DUK__OBJ_LIT_KEY_PLAIN) {
  52891. /* step 4.b */
  52892. DUK_DDD(DUK_DDDPRINT("duplicate key: getter/setter encountered after plain key"));
  52893. return 1;
  52894. }
  52895. if (key_flags & new_key_flags) {
  52896. /* step 4.d */
  52897. DUK_DDD(DUK_DDDPRINT("duplicate key: getter/setter encountered twice"));
  52898. return 1;
  52899. }
  52900. }
  52901. new_key_flags |= key_flags;
  52902. DUK_DDD(DUK_DDDPRINT("setting/updating key %!T flags: 0x%08lx -> 0x%08lx",
  52903. (duk_tval *) duk_get_tval(ctx, -1),
  52904. (unsigned long) key_flags,
  52905. (unsigned long) new_key_flags));
  52906. duk_dup(ctx, -1);
  52907. duk_push_int(ctx, new_key_flags); /* [ ... key_obj key key flags ] */
  52908. duk_put_prop(ctx, -4); /* [ ... key_obj key ] */
  52909. return 0;
  52910. }
  52911. DUK_LOCAL void duk__nud_object_literal(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  52912. duk_hthread *thr = comp_ctx->thr;
  52913. duk_context *ctx = (duk_context *) thr;
  52914. duk_reg_t reg_obj; /* result reg */
  52915. duk_reg_t reg_key; /* temp reg for key literal */
  52916. duk_reg_t reg_temp; /* temp reg */
  52917. duk_reg_t temp_start; /* temp reg value for start of loop */
  52918. duk_small_uint_t max_init_pairs; /* max # of key-value pairs initialized in one MPUTOBJ set */
  52919. duk_small_uint_t num_pairs; /* number of pairs in current MPUTOBJ set */
  52920. duk_bool_t first; /* first value: comma must not precede the value */
  52921. duk_bool_t is_set, is_get; /* temps */
  52922. DUK_ASSERT(comp_ctx->prev_token.t == DUK_TOK_LCURLY);
  52923. max_init_pairs = DUK__MAX_OBJECT_INIT_PAIRS; /* XXX: depend on available temps? */
  52924. reg_obj = DUK__ALLOCTEMP(comp_ctx);
  52925. duk__emit_extraop_b_c(comp_ctx,
  52926. DUK_EXTRAOP_NEWOBJ | DUK__EMIT_FLAG_B_IS_TARGET,
  52927. reg_obj,
  52928. 0); /* XXX: patch initial size afterwards? */
  52929. temp_start = DUK__GETTEMP(comp_ctx);
  52930. /* temp object for tracking / detecting duplicate keys */
  52931. duk_push_object(ctx);
  52932. /*
  52933. * Emit initializers in sets of maximum max_init_pairs keys.
  52934. * Setter/getter is handled separately and terminates the
  52935. * current set of initializer values. Corner cases such as
  52936. * single value initializers do not have special handling now.
  52937. */
  52938. first = 1;
  52939. for (;;) {
  52940. num_pairs = 0;
  52941. DUK__SETTEMP(comp_ctx, temp_start);
  52942. if (comp_ctx->curr_token.t == DUK_TOK_RCURLY) {
  52943. break;
  52944. }
  52945. for (;;) {
  52946. /*
  52947. * Three possible element formats:
  52948. * 1) PropertyName : AssignmentExpression
  52949. * 2) get PropertyName () { FunctionBody }
  52950. * 3) set PropertyName ( PropertySetParameterList ) { FunctionBody }
  52951. *
  52952. * PropertyName can be IdentifierName (includes reserved words), a string
  52953. * literal, or a number literal. Note that IdentifierName allows 'get' and
  52954. * 'set' too, so we need to look ahead to the next token to distinguish:
  52955. *
  52956. * { get : 1 }
  52957. *
  52958. * and
  52959. *
  52960. * { get foo() { return 1 } }
  52961. * { get get() { return 1 } } // 'get' as getter propertyname
  52962. *
  52963. * Finally, a trailing comma is allowed.
  52964. *
  52965. * Key name is coerced to string at compile time (and ends up as a
  52966. * a string constant) even for numeric keys (e.g. "{1:'foo'}").
  52967. * These could be emitted using e.g. LDINT, but that seems hardly
  52968. * worth the effort and would increase code size.
  52969. */
  52970. DUK_DDD(DUK_DDDPRINT("object literal inner loop, curr_token->t = %ld",
  52971. (long) comp_ctx->curr_token.t));
  52972. if (comp_ctx->curr_token.t == DUK_TOK_RCURLY) {
  52973. /* the outer loop will recheck and exit */
  52974. break;
  52975. }
  52976. if (num_pairs >= max_init_pairs) {
  52977. /* MPUTOBJ emitted by outer loop */
  52978. break;
  52979. }
  52980. if (first) {
  52981. first = 0;
  52982. } else {
  52983. if (comp_ctx->curr_token.t != DUK_TOK_COMMA) {
  52984. goto syntax_error;
  52985. }
  52986. duk__advance(comp_ctx);
  52987. if (comp_ctx->curr_token.t == DUK_TOK_RCURLY) {
  52988. /* trailing comma followed by rcurly */
  52989. break;
  52990. }
  52991. }
  52992. /* advance to get one step of lookup */
  52993. duk__advance(comp_ctx);
  52994. /* NOTE: "get" and "set" are not officially ReservedWords and the lexer
  52995. * currently treats them always like ordinary identifiers (DUK_TOK_GET
  52996. * and DUK_TOK_SET are unused). They need to be detected based on the
  52997. * identifier string content.
  52998. */
  52999. is_get = (comp_ctx->prev_token.t == DUK_TOK_IDENTIFIER &&
  53000. comp_ctx->prev_token.str1 == DUK_HTHREAD_STRING_GET(thr));
  53001. is_set = (comp_ctx->prev_token.t == DUK_TOK_IDENTIFIER &&
  53002. comp_ctx->prev_token.str1 == DUK_HTHREAD_STRING_SET(thr));
  53003. if ((is_get || is_set) && comp_ctx->curr_token.t != DUK_TOK_COLON) {
  53004. /* getter/setter */
  53005. duk_int_t fnum;
  53006. if (comp_ctx->curr_token.t_nores == DUK_TOK_IDENTIFIER ||
  53007. comp_ctx->curr_token.t_nores == DUK_TOK_STRING) {
  53008. /* same handling for identifiers and strings */
  53009. DUK_ASSERT(comp_ctx->curr_token.str1 != NULL);
  53010. duk_push_hstring(ctx, comp_ctx->curr_token.str1);
  53011. } else if (comp_ctx->curr_token.t == DUK_TOK_NUMBER) {
  53012. duk_push_number(ctx, comp_ctx->curr_token.num);
  53013. duk_to_string(ctx, -1);
  53014. } else {
  53015. goto syntax_error;
  53016. }
  53017. DUK_ASSERT(duk_is_string(ctx, -1));
  53018. if (duk__nud_object_literal_key_check(comp_ctx,
  53019. (is_get ? DUK__OBJ_LIT_KEY_GET : DUK__OBJ_LIT_KEY_SET))) {
  53020. goto syntax_error;
  53021. }
  53022. reg_key = duk__getconst(comp_ctx);
  53023. if (num_pairs > 0) {
  53024. /* - A is a source register (it's not a write target, but used
  53025. * to identify the target object) but can be shuffled.
  53026. * - B cannot be shuffled normally because it identifies a range
  53027. * of registers, the emitter has special handling for this
  53028. * (the "no shuffle" flag must not be set).
  53029. * - C is a non-register number and cannot be shuffled, but
  53030. * never needs to be.
  53031. */
  53032. duk__emit_a_b_c(comp_ctx,
  53033. DUK_OP_MPUTOBJ |
  53034. DUK__EMIT_FLAG_NO_SHUFFLE_C |
  53035. DUK__EMIT_FLAG_A_IS_SOURCE,
  53036. reg_obj,
  53037. temp_start,
  53038. num_pairs);
  53039. num_pairs = 0;
  53040. DUK__SETTEMP(comp_ctx, temp_start);
  53041. }
  53042. /* curr_token = get/set name */
  53043. fnum = duk__parse_func_like_fnum(comp_ctx, 0 /*is_decl*/, 1 /*is_setget*/);
  53044. DUK_ASSERT(DUK__GETTEMP(comp_ctx) == temp_start);
  53045. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  53046. duk__emit_a_bc(comp_ctx,
  53047. DUK_OP_LDCONST,
  53048. (duk_regconst_t) reg_temp,
  53049. (duk_regconst_t) reg_key);
  53050. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  53051. duk__emit_a_bc(comp_ctx,
  53052. DUK_OP_CLOSURE,
  53053. (duk_regconst_t) reg_temp,
  53054. (duk_regconst_t) fnum);
  53055. /* Slot C is used in a non-standard fashion (range of regs),
  53056. * emitter code has special handling for it (must not set the
  53057. * "no shuffle" flag).
  53058. */
  53059. duk__emit_extraop_b_c(comp_ctx,
  53060. (is_get ? DUK_EXTRAOP_INITGET : DUK_EXTRAOP_INITSET),
  53061. reg_obj,
  53062. temp_start); /* temp_start+0 = key, temp_start+1 = closure */
  53063. DUK__SETTEMP(comp_ctx, temp_start);
  53064. } else {
  53065. /* normal key/value */
  53066. if (comp_ctx->prev_token.t_nores == DUK_TOK_IDENTIFIER ||
  53067. comp_ctx->prev_token.t_nores == DUK_TOK_STRING) {
  53068. /* same handling for identifiers and strings */
  53069. DUK_ASSERT(comp_ctx->prev_token.str1 != NULL);
  53070. duk_push_hstring(ctx, comp_ctx->prev_token.str1);
  53071. } else if (comp_ctx->prev_token.t == DUK_TOK_NUMBER) {
  53072. duk_push_number(ctx, comp_ctx->prev_token.num);
  53073. duk_to_string(ctx, -1);
  53074. } else {
  53075. goto syntax_error;
  53076. }
  53077. DUK_ASSERT(duk_is_string(ctx, -1));
  53078. if (duk__nud_object_literal_key_check(comp_ctx, DUK__OBJ_LIT_KEY_PLAIN)) {
  53079. goto syntax_error;
  53080. }
  53081. reg_key = duk__getconst(comp_ctx);
  53082. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  53083. duk__emit_a_bc(comp_ctx,
  53084. DUK_OP_LDCONST,
  53085. (duk_regconst_t) reg_temp,
  53086. (duk_regconst_t) reg_key);
  53087. duk__advance_expect(comp_ctx, DUK_TOK_COLON);
  53088. reg_temp = DUK__ALLOCTEMP(comp_ctx); /* alloc temp just in case, to update max temp */
  53089. DUK__SETTEMP(comp_ctx, reg_temp);
  53090. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_COMMA /*rbp_flags*/, reg_temp /*forced_reg*/);
  53091. DUK__SETTEMP(comp_ctx, reg_temp + 1);
  53092. num_pairs++;
  53093. }
  53094. }
  53095. if (num_pairs > 0) {
  53096. /* See MPUTOBJ comments above. */
  53097. duk__emit_a_b_c(comp_ctx,
  53098. DUK_OP_MPUTOBJ |
  53099. DUK__EMIT_FLAG_NO_SHUFFLE_C |
  53100. DUK__EMIT_FLAG_A_IS_SOURCE,
  53101. reg_obj,
  53102. temp_start,
  53103. num_pairs);
  53104. /* num_pairs and temp_start reset at top of outer loop */
  53105. }
  53106. }
  53107. DUK_ASSERT(comp_ctx->curr_token.t == DUK_TOK_RCURLY);
  53108. duk__advance(comp_ctx);
  53109. DUK__SETTEMP(comp_ctx, temp_start);
  53110. res->t = DUK_IVAL_PLAIN;
  53111. res->x1.t = DUK_ISPEC_REGCONST;
  53112. res->x1.regconst = (duk_regconst_t) reg_obj;
  53113. DUK_DDD(DUK_DDDPRINT("final tracking object: %!T",
  53114. (duk_tval *) duk_get_tval(ctx, -1)));
  53115. duk_pop(ctx);
  53116. return;
  53117. syntax_error:
  53118. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_OBJECT_LITERAL);
  53119. }
  53120. /* Parse argument list. Arguments are written to temps starting from
  53121. * "next temp". Returns number of arguments parsed. Expects left paren
  53122. * to be already eaten, and eats the right paren before returning.
  53123. */
  53124. DUK_LOCAL duk_int_t duk__parse_arguments(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  53125. duk_int_t nargs = 0;
  53126. duk_reg_t reg_temp;
  53127. /* Note: expect that caller has already eaten the left paren */
  53128. DUK_DDD(DUK_DDDPRINT("start parsing arguments, prev_token.t=%ld, curr_token.t=%ld",
  53129. (long) comp_ctx->prev_token.t, (long) comp_ctx->curr_token.t));
  53130. for (;;) {
  53131. if (comp_ctx->curr_token.t == DUK_TOK_RPAREN) {
  53132. break;
  53133. }
  53134. if (nargs > 0) {
  53135. duk__advance_expect(comp_ctx, DUK_TOK_COMMA);
  53136. }
  53137. /* We want the argument expression value to go to "next temp"
  53138. * without additional moves. That should almost always be the
  53139. * case, but we double check after expression parsing.
  53140. *
  53141. * This is not the cleanest possible approach.
  53142. */
  53143. reg_temp = DUK__ALLOCTEMP(comp_ctx); /* bump up "allocated" reg count, just in case */
  53144. DUK__SETTEMP(comp_ctx, reg_temp);
  53145. /* binding power must be high enough to NOT allow comma expressions directly */
  53146. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_COMMA /*rbp_flags*/, reg_temp); /* always allow 'in', coerce to 'tr' just in case */
  53147. DUK__SETTEMP(comp_ctx, reg_temp + 1);
  53148. nargs++;
  53149. DUK_DDD(DUK_DDDPRINT("argument #%ld written into reg %ld", (long) nargs, (long) reg_temp));
  53150. }
  53151. /* eat the right paren */
  53152. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  53153. DUK_DDD(DUK_DDDPRINT("end parsing arguments"));
  53154. return nargs;
  53155. }
  53156. DUK_LOCAL duk_bool_t duk__expr_is_empty(duk_compiler_ctx *comp_ctx) {
  53157. /* empty expressions can be detected conveniently with nud/led counts */
  53158. return (comp_ctx->curr_func.nud_count == 0) &&
  53159. (comp_ctx->curr_func.led_count == 0);
  53160. }
  53161. DUK_LOCAL void duk__expr_nud(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  53162. duk_hthread *thr = comp_ctx->thr;
  53163. duk_context *ctx = (duk_context *) thr;
  53164. duk_token *tk;
  53165. duk_reg_t temp_at_entry;
  53166. duk_small_int_t tok;
  53167. duk_uint32_t args; /* temp variable to pass constants and flags to shared code */
  53168. /*
  53169. * ctx->prev_token token to process with duk__expr_nud()
  53170. * ctx->curr_token updated by caller
  53171. *
  53172. * Note: the token in the switch below has already been eaten.
  53173. */
  53174. temp_at_entry = DUK__GETTEMP(comp_ctx);
  53175. comp_ctx->curr_func.nud_count++;
  53176. tk = &comp_ctx->prev_token;
  53177. tok = tk->t;
  53178. res->t = DUK_IVAL_NONE;
  53179. DUK_DDD(DUK_DDDPRINT("duk__expr_nud(), prev_token.t=%ld, allow_in=%ld, paren_level=%ld",
  53180. (long) tk->t, (long) comp_ctx->curr_func.allow_in, (long) comp_ctx->curr_func.paren_level));
  53181. switch (tok) {
  53182. /* PRIMARY EXPRESSIONS */
  53183. case DUK_TOK_THIS: {
  53184. duk_reg_t reg_temp;
  53185. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  53186. duk__emit_extraop_bc(comp_ctx,
  53187. DUK_EXTRAOP_LDTHIS,
  53188. (duk_regconst_t) reg_temp);
  53189. res->t = DUK_IVAL_PLAIN;
  53190. res->x1.t = DUK_ISPEC_REGCONST;
  53191. res->x1.regconst = (duk_regconst_t) reg_temp;
  53192. return;
  53193. }
  53194. case DUK_TOK_IDENTIFIER: {
  53195. res->t = DUK_IVAL_VAR;
  53196. res->x1.t = DUK_ISPEC_VALUE;
  53197. duk_push_hstring(ctx, tk->str1);
  53198. duk_replace(ctx, res->x1.valstack_idx);
  53199. return;
  53200. }
  53201. case DUK_TOK_NULL: {
  53202. duk_push_null(ctx);
  53203. goto plain_value;
  53204. }
  53205. case DUK_TOK_TRUE: {
  53206. duk_push_true(ctx);
  53207. goto plain_value;
  53208. }
  53209. case DUK_TOK_FALSE: {
  53210. duk_push_false(ctx);
  53211. goto plain_value;
  53212. }
  53213. case DUK_TOK_NUMBER: {
  53214. duk_push_number(ctx, tk->num);
  53215. goto plain_value;
  53216. }
  53217. case DUK_TOK_STRING: {
  53218. DUK_ASSERT(tk->str1 != NULL);
  53219. duk_push_hstring(ctx, tk->str1);
  53220. goto plain_value;
  53221. }
  53222. case DUK_TOK_REGEXP: {
  53223. #ifdef DUK_USE_REGEXP_SUPPORT
  53224. duk_reg_t reg_temp;
  53225. duk_regconst_t rc_re_bytecode; /* const */
  53226. duk_regconst_t rc_re_source; /* const */
  53227. DUK_ASSERT(tk->str1 != NULL);
  53228. DUK_ASSERT(tk->str2 != NULL);
  53229. DUK_DDD(DUK_DDDPRINT("emitting regexp op, str1=%!O, str2=%!O",
  53230. (duk_heaphdr *) tk->str1,
  53231. (duk_heaphdr *) tk->str2));
  53232. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  53233. duk_push_hstring(ctx, tk->str1);
  53234. duk_push_hstring(ctx, tk->str2);
  53235. /* [ ... pattern flags ] */
  53236. duk_regexp_compile(thr);
  53237. /* [ ... escaped_source bytecode ] */
  53238. rc_re_bytecode = duk__getconst(comp_ctx);
  53239. rc_re_source = duk__getconst(comp_ctx);
  53240. duk__emit_a_b_c(comp_ctx,
  53241. DUK_OP_REGEXP,
  53242. (duk_regconst_t) reg_temp /*a*/,
  53243. rc_re_bytecode /*b*/,
  53244. rc_re_source /*c*/);
  53245. res->t = DUK_IVAL_PLAIN;
  53246. res->x1.t = DUK_ISPEC_REGCONST;
  53247. res->x1.regconst = (duk_regconst_t) reg_temp;
  53248. return;
  53249. #else /* DUK_USE_REGEXP_SUPPORT */
  53250. goto syntax_error;
  53251. #endif /* DUK_USE_REGEXP_SUPPORT */
  53252. }
  53253. case DUK_TOK_LBRACKET: {
  53254. DUK_DDD(DUK_DDDPRINT("parsing array literal"));
  53255. duk__nud_array_literal(comp_ctx, res);
  53256. return;
  53257. }
  53258. case DUK_TOK_LCURLY: {
  53259. DUK_DDD(DUK_DDDPRINT("parsing object literal"));
  53260. duk__nud_object_literal(comp_ctx, res);
  53261. return;
  53262. }
  53263. case DUK_TOK_LPAREN: {
  53264. duk_bool_t prev_allow_in;
  53265. comp_ctx->curr_func.paren_level++;
  53266. prev_allow_in = comp_ctx->curr_func.allow_in;
  53267. comp_ctx->curr_func.allow_in = 1; /* reset 'allow_in' for parenthesized expression */
  53268. duk__expr(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/); /* Expression, terminates at a ')' */
  53269. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  53270. comp_ctx->curr_func.allow_in = prev_allow_in;
  53271. comp_ctx->curr_func.paren_level--;
  53272. return;
  53273. }
  53274. /* MEMBER/NEW/CALL EXPRESSIONS */
  53275. case DUK_TOK_NEW: {
  53276. /*
  53277. * Parsing an expression starting with 'new' is tricky because
  53278. * there are multiple possible productions deriving from
  53279. * LeftHandSideExpression which begin with 'new'.
  53280. *
  53281. * We currently resort to one-token lookahead to distinguish the
  53282. * cases. Hopefully this is correct. The binding power must be
  53283. * such that parsing ends at an LPAREN (CallExpression) but not at
  53284. * a PERIOD or LBRACKET (MemberExpression).
  53285. *
  53286. * See doc/compiler.rst for discussion on the parsing approach,
  53287. * and testcases/test-dev-new.js for a bunch of documented tests.
  53288. */
  53289. duk_reg_t reg_target;
  53290. duk_int_t nargs;
  53291. DUK_DDD(DUK_DDDPRINT("begin parsing new expression"));
  53292. reg_target = DUK__ALLOCTEMP(comp_ctx);
  53293. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_CALL /*rbp_flags*/, reg_target /*forced_reg*/);
  53294. DUK__SETTEMP(comp_ctx, reg_target + 1);
  53295. if (comp_ctx->curr_token.t == DUK_TOK_LPAREN) {
  53296. /* 'new' MemberExpression Arguments */
  53297. DUK_DDD(DUK_DDDPRINT("new expression has argument list"));
  53298. duk__advance(comp_ctx);
  53299. nargs = duk__parse_arguments(comp_ctx, res); /* parse args starting from "next temp", reg_target + 1 */
  53300. /* right paren eaten */
  53301. } else {
  53302. /* 'new' MemberExpression */
  53303. DUK_DDD(DUK_DDDPRINT("new expression has no argument list"));
  53304. nargs = 0;
  53305. }
  53306. /* Opcode slot C is used in a non-standard way, so shuffling
  53307. * is not allowed.
  53308. */
  53309. duk__emit_a_b_c(comp_ctx,
  53310. DUK_OP_NEW | DUK__EMIT_FLAG_NO_SHUFFLE_A | DUK__EMIT_FLAG_NO_SHUFFLE_C,
  53311. 0 /*unused*/,
  53312. reg_target /*target*/,
  53313. nargs /*num_args*/);
  53314. DUK_DDD(DUK_DDDPRINT("end parsing new expression"));
  53315. res->t = DUK_IVAL_PLAIN;
  53316. res->x1.t = DUK_ISPEC_REGCONST;
  53317. res->x1.regconst = (duk_regconst_t) reg_target;
  53318. return;
  53319. }
  53320. /* FUNCTION EXPRESSIONS */
  53321. case DUK_TOK_FUNCTION: {
  53322. /* Function expression. Note that any statement beginning with 'function'
  53323. * is handled by the statement parser as a function declaration, or a
  53324. * non-standard function expression/statement (or a SyntaxError). We only
  53325. * handle actual function expressions (occurring inside an expression) here.
  53326. *
  53327. * O(depth^2) parse count for inner functions is handled by recording a
  53328. * lexer offset on the first compilation pass, so that the function can
  53329. * be efficiently skipped on the second pass. This is encapsulated into
  53330. * duk__parse_func_like_fnum().
  53331. */
  53332. duk_reg_t reg_temp;
  53333. duk_int_t fnum;
  53334. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  53335. /* curr_token follows 'function' */
  53336. fnum = duk__parse_func_like_fnum(comp_ctx, 0 /*is_decl*/, 0 /*is_setget*/);
  53337. DUK_DDD(DUK_DDDPRINT("parsed inner function -> fnum %ld", (long) fnum));
  53338. duk__emit_a_bc(comp_ctx,
  53339. DUK_OP_CLOSURE,
  53340. (duk_regconst_t) reg_temp /*a*/,
  53341. (duk_regconst_t) fnum /*bc*/);
  53342. res->t = DUK_IVAL_PLAIN;
  53343. res->x1.t = DUK_ISPEC_REGCONST;
  53344. res->x1.regconst = (duk_regconst_t) reg_temp;
  53345. return;
  53346. }
  53347. /* UNARY EXPRESSIONS */
  53348. case DUK_TOK_DELETE: {
  53349. /* Delete semantics are a bit tricky. The description in E5 specification
  53350. * is kind of confusing, because it distinguishes between resolvability of
  53351. * a reference (which is only known at runtime) seemingly at compile time
  53352. * (= SyntaxError throwing).
  53353. */
  53354. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  53355. if (res->t == DUK_IVAL_VAR) {
  53356. /* not allowed in strict mode, regardless of whether resolves;
  53357. * in non-strict mode DELVAR handles both non-resolving and
  53358. * resolving cases (the specification description is a bit confusing).
  53359. */
  53360. duk_reg_t reg_temp;
  53361. duk_reg_t reg_varbind;
  53362. duk_regconst_t rc_varname;
  53363. if (comp_ctx->curr_func.is_strict) {
  53364. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_CANNOT_DELETE_IDENTIFIER);
  53365. }
  53366. DUK__SETTEMP(comp_ctx, temp_at_entry);
  53367. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  53368. duk_dup(ctx, res->x1.valstack_idx);
  53369. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  53370. /* register bound variables are non-configurable -> always false */
  53371. duk__emit_extraop_bc(comp_ctx,
  53372. DUK_EXTRAOP_LDFALSE,
  53373. (duk_regconst_t) reg_temp);
  53374. } else {
  53375. duk_dup(ctx, res->x1.valstack_idx);
  53376. rc_varname = duk__getconst(comp_ctx);
  53377. duk__emit_a_b(comp_ctx,
  53378. DUK_OP_DELVAR,
  53379. (duk_regconst_t) reg_temp,
  53380. (duk_regconst_t) rc_varname);
  53381. }
  53382. res->t = DUK_IVAL_PLAIN;
  53383. res->x1.t = DUK_ISPEC_REGCONST;
  53384. res->x1.regconst = (duk_regconst_t) reg_temp;
  53385. } else if (res->t == DUK_IVAL_PROP) {
  53386. duk_reg_t reg_temp;
  53387. duk_reg_t reg_obj;
  53388. duk_regconst_t rc_key;
  53389. DUK__SETTEMP(comp_ctx, temp_at_entry);
  53390. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  53391. reg_obj = duk__ispec_toregconst_raw(comp_ctx, &res->x1, -1 /*forced_reg*/, 0 /*flags*/); /* don't allow const */
  53392. rc_key = duk__ispec_toregconst_raw(comp_ctx, &res->x2, -1 /*forced_reg*/, DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  53393. duk__emit_a_b_c(comp_ctx,
  53394. DUK_OP_DELPROP,
  53395. (duk_regconst_t) reg_temp,
  53396. (duk_regconst_t) reg_obj,
  53397. rc_key);
  53398. res->t = DUK_IVAL_PLAIN;
  53399. res->x1.t = DUK_ISPEC_REGCONST;
  53400. res->x1.regconst = (duk_regconst_t) reg_temp;
  53401. } else {
  53402. /* non-Reference deletion is always 'true', even in strict mode */
  53403. duk_push_true(ctx);
  53404. goto plain_value;
  53405. }
  53406. return;
  53407. }
  53408. case DUK_TOK_VOID: {
  53409. duk__expr_toplain_ignore(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  53410. duk_push_undefined(ctx);
  53411. goto plain_value;
  53412. }
  53413. case DUK_TOK_TYPEOF: {
  53414. /* 'typeof' must handle unresolvable references without throwing
  53415. * a ReferenceError (E5 Section 11.4.3). Register mapped values
  53416. * will never be unresolvable so special handling is only required
  53417. * when an identifier is a "slow path" one.
  53418. */
  53419. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  53420. if (res->t == DUK_IVAL_VAR) {
  53421. duk_reg_t reg_varbind;
  53422. duk_regconst_t rc_varname;
  53423. duk_reg_t reg_temp;
  53424. duk_dup(ctx, res->x1.valstack_idx);
  53425. if (!duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  53426. DUK_DDD(DUK_DDDPRINT("typeof for an identifier name which could not be resolved "
  53427. "at compile time, need to use special run-time handling"));
  53428. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  53429. duk__emit_extraop_b_c(comp_ctx,
  53430. DUK_EXTRAOP_TYPEOFID | DUK__EMIT_FLAG_B_IS_TARGET,
  53431. reg_temp,
  53432. rc_varname);
  53433. res->t = DUK_IVAL_PLAIN;
  53434. res->x1.t = DUK_ISPEC_REGCONST;
  53435. res->x1.regconst = (duk_regconst_t) reg_temp;
  53436. return;
  53437. }
  53438. }
  53439. args = (DUK_EXTRAOP_TYPEOF << 8) + 0;
  53440. goto unary_extraop;
  53441. }
  53442. case DUK_TOK_INCREMENT: {
  53443. args = (DUK_OP_PREINCR << 8) + 0;
  53444. goto preincdec;
  53445. }
  53446. case DUK_TOK_DECREMENT: {
  53447. args = (DUK_OP_PREDECR << 8) + 0;
  53448. goto preincdec;
  53449. }
  53450. case DUK_TOK_ADD: {
  53451. /* unary plus */
  53452. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  53453. if (res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_VALUE &&
  53454. duk_is_number(ctx, res->x1.valstack_idx)) {
  53455. /* unary plus of a number is identity */
  53456. ;
  53457. return;
  53458. }
  53459. args = (DUK_EXTRAOP_UNP << 8) + 0;
  53460. goto unary_extraop;
  53461. }
  53462. case DUK_TOK_SUB: {
  53463. /* unary minus */
  53464. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  53465. if (res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_VALUE &&
  53466. duk_is_number(ctx, res->x1.valstack_idx)) {
  53467. /* this optimization is important to handle negative literals (which are not directly
  53468. * provided by the lexical grammar
  53469. */
  53470. duk_tval *tv_num = duk_get_tval(ctx, res->x1.valstack_idx);
  53471. duk_double_union du;
  53472. DUK_ASSERT(tv_num != NULL);
  53473. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_num));
  53474. du.d = DUK_TVAL_GET_NUMBER(tv_num);
  53475. du.d = -du.d;
  53476. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  53477. DUK_TVAL_SET_NUMBER(tv_num, du.d);
  53478. return;
  53479. }
  53480. args = (DUK_EXTRAOP_UNM << 8) + 0;
  53481. goto unary_extraop;
  53482. }
  53483. case DUK_TOK_BNOT: {
  53484. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  53485. args = (DUK_EXTRAOP_BNOT << 8) + 0;
  53486. goto unary_extraop;
  53487. }
  53488. case DUK_TOK_LNOT: {
  53489. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  53490. if (res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_VALUE) {
  53491. /* Very minimal inlining to handle common idioms '!0' and '!1',
  53492. * and also boolean arguments like '!false' and '!true'.
  53493. */
  53494. duk_tval *tv_val = duk_get_tval(ctx, res->x1.valstack_idx);
  53495. DUK_ASSERT(tv_val != NULL);
  53496. if (DUK_TVAL_IS_NUMBER(tv_val)) {
  53497. duk_double_t d;
  53498. d = DUK_TVAL_GET_NUMBER(tv_val);
  53499. if (d == 0.0) {
  53500. /* Matches both +0 and -0 on purpose. */
  53501. DUK_DDD(DUK_DDDPRINT("inlined lnot: !0 -> true"));
  53502. DUK_TVAL_SET_BOOLEAN_TRUE(tv_val);
  53503. return;
  53504. } else if (d == 1.0) {
  53505. DUK_DDD(DUK_DDDPRINT("inlined lnot: !1 -> false"));
  53506. DUK_TVAL_SET_BOOLEAN_FALSE(tv_val);
  53507. return;
  53508. }
  53509. } else if (DUK_TVAL_IS_BOOLEAN(tv_val)) {
  53510. duk_small_int_t v;
  53511. v = DUK_TVAL_GET_BOOLEAN(tv_val);
  53512. DUK_DDD(DUK_DDDPRINT("inlined lnot boolean: %ld", (long) v));
  53513. DUK_ASSERT(v == 0 || v == 1);
  53514. DUK_TVAL_SET_BOOLEAN(tv_val, v ^ 0x01);
  53515. return;
  53516. }
  53517. }
  53518. args = (DUK_EXTRAOP_LNOT << 8) + 0;
  53519. goto unary_extraop;
  53520. }
  53521. } /* end switch */
  53522. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_PARSE_ERROR);
  53523. return;
  53524. unary_extraop:
  53525. {
  53526. /* Note: must coerce to a (writable) temp register, so that e.g. "!x" where x
  53527. * is a reg-mapped variable works correctly (does not mutate the variable register).
  53528. */
  53529. duk_reg_t reg_temp;
  53530. reg_temp = duk__ivalue_toregconst_raw(comp_ctx, res, -1 /*forced_reg*/, DUK__IVAL_FLAG_REQUIRE_TEMP /*flags*/);
  53531. duk__emit_extraop_bc(comp_ctx,
  53532. (args >> 8),
  53533. (duk_regconst_t) reg_temp);
  53534. res->t = DUK_IVAL_PLAIN;
  53535. res->x1.t = DUK_ISPEC_REGCONST;
  53536. res->x1.regconst = (duk_regconst_t) reg_temp;
  53537. return;
  53538. }
  53539. preincdec:
  53540. {
  53541. /* preincrement and predecrement */
  53542. duk_reg_t reg_res;
  53543. duk_small_uint_t args_op = args >> 8;
  53544. /* Specific assumptions for opcode numbering. */
  53545. DUK_ASSERT(DUK_OP_PREINCR + 4 == DUK_OP_PREINCV);
  53546. DUK_ASSERT(DUK_OP_PREDECR + 4 == DUK_OP_PREDECV);
  53547. DUK_ASSERT(DUK_OP_PREINCR + 8 == DUK_OP_PREINCP);
  53548. DUK_ASSERT(DUK_OP_PREDECR + 8 == DUK_OP_PREDECP);
  53549. reg_res = DUK__ALLOCTEMP(comp_ctx);
  53550. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  53551. if (res->t == DUK_IVAL_VAR) {
  53552. duk_hstring *h_varname;
  53553. duk_reg_t reg_varbind;
  53554. duk_regconst_t rc_varname;
  53555. h_varname = duk_get_hstring(ctx, res->x1.valstack_idx);
  53556. DUK_ASSERT(h_varname != NULL);
  53557. if (duk__hstring_is_eval_or_arguments_in_strict_mode(comp_ctx, h_varname)) {
  53558. goto syntax_error;
  53559. }
  53560. duk_dup(ctx, res->x1.valstack_idx);
  53561. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  53562. duk__emit_a_bc(comp_ctx,
  53563. args_op, /* e.g. DUK_OP_PREINCR */
  53564. (duk_regconst_t) reg_res,
  53565. (duk_regconst_t) reg_varbind);
  53566. } else {
  53567. duk__emit_a_bc(comp_ctx,
  53568. args_op + 4, /* e.g. DUK_OP_PREINCV */
  53569. (duk_regconst_t) reg_res,
  53570. rc_varname);
  53571. }
  53572. DUK_DDD(DUK_DDDPRINT("preincdec to '%!O' -> reg_varbind=%ld, rc_varname=%ld",
  53573. (duk_heaphdr *) h_varname, (long) reg_varbind, (long) rc_varname));
  53574. } else if (res->t == DUK_IVAL_PROP) {
  53575. duk_reg_t reg_obj; /* allocate to reg only (not const) */
  53576. duk_regconst_t rc_key;
  53577. reg_obj = duk__ispec_toregconst_raw(comp_ctx, &res->x1, -1 /*forced_reg*/, 0 /*flags*/); /* don't allow const */
  53578. rc_key = duk__ispec_toregconst_raw(comp_ctx, &res->x2, -1 /*forced_reg*/, DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  53579. duk__emit_a_b_c(comp_ctx,
  53580. args_op + 8, /* e.g. DUK_OP_PREINCP */
  53581. (duk_regconst_t) reg_res,
  53582. (duk_regconst_t) reg_obj,
  53583. rc_key);
  53584. } else {
  53585. /* Technically return value is not needed because INVLHS will
  53586. * unconditially throw a ReferenceError. Coercion is necessary
  53587. * for proper semantics (consider ToNumber() called for an object).
  53588. * Use DUK_EXTRAOP_UNP with a dummy register to get ToNumber().
  53589. */
  53590. duk__ivalue_toforcedreg(comp_ctx, res, reg_res);
  53591. duk__emit_extraop_bc(comp_ctx,
  53592. DUK_EXTRAOP_UNP,
  53593. reg_res); /* for side effects, result ignored */
  53594. duk__emit_extraop_only(comp_ctx,
  53595. DUK_EXTRAOP_INVLHS);
  53596. }
  53597. res->t = DUK_IVAL_PLAIN;
  53598. res->x1.t = DUK_ISPEC_REGCONST;
  53599. res->x1.regconst = (duk_regconst_t) reg_res;
  53600. DUK__SETTEMP(comp_ctx, reg_res + 1);
  53601. return;
  53602. }
  53603. plain_value:
  53604. {
  53605. /* Stack top contains plain value */
  53606. res->t = DUK_IVAL_PLAIN;
  53607. res->x1.t = DUK_ISPEC_VALUE;
  53608. duk_replace(ctx, res->x1.valstack_idx);
  53609. return;
  53610. }
  53611. syntax_error:
  53612. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_EXPRESSION);
  53613. }
  53614. /* XXX: add flag to indicate whether caller cares about return value; this
  53615. * affects e.g. handling of assignment expressions. This change needs API
  53616. * changes elsewhere too.
  53617. */
  53618. DUK_LOCAL void duk__expr_led(duk_compiler_ctx *comp_ctx, duk_ivalue *left, duk_ivalue *res) {
  53619. duk_hthread *thr = comp_ctx->thr;
  53620. duk_context *ctx = (duk_context *) thr;
  53621. duk_token *tk;
  53622. duk_small_int_t tok;
  53623. duk_uint32_t args; /* temp variable to pass constants and flags to shared code */
  53624. /*
  53625. * ctx->prev_token token to process with duk__expr_led()
  53626. * ctx->curr_token updated by caller
  53627. */
  53628. comp_ctx->curr_func.led_count++;
  53629. /* The token in the switch has already been eaten here */
  53630. tk = &comp_ctx->prev_token;
  53631. tok = tk->t;
  53632. DUK_DDD(DUK_DDDPRINT("duk__expr_led(), prev_token.t=%ld, allow_in=%ld, paren_level=%ld",
  53633. (long) tk->t, (long) comp_ctx->curr_func.allow_in, (long) comp_ctx->curr_func.paren_level));
  53634. /* XXX: default priority for infix operators is duk__expr_lbp(tok) -> get it here? */
  53635. switch (tok) {
  53636. /* PRIMARY EXPRESSIONS */
  53637. case DUK_TOK_PERIOD: {
  53638. /* Property access expressions are critical for correct LHS ordering,
  53639. * see comments in duk__expr()!
  53640. */
  53641. /* XXX: this now coerces an identifier into a GETVAR to a temp, which
  53642. * causes an extra LDREG in call setup. It's sufficient to coerce to a
  53643. * unary ivalue?
  53644. */
  53645. duk__ivalue_toplain(comp_ctx, left);
  53646. /* NB: must accept reserved words as property name */
  53647. if (comp_ctx->curr_token.t_nores != DUK_TOK_IDENTIFIER) {
  53648. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_EXPECTED_IDENTIFIER);
  53649. }
  53650. res->t = DUK_IVAL_PROP;
  53651. duk__copy_ispec(comp_ctx, &left->x1, &res->x1); /* left.x1 -> res.x1 */
  53652. DUK_ASSERT(comp_ctx->curr_token.str1 != NULL);
  53653. duk_push_hstring(ctx, comp_ctx->curr_token.str1);
  53654. duk_replace(ctx, res->x2.valstack_idx);
  53655. res->x2.t = DUK_ISPEC_VALUE;
  53656. /* special RegExp literal handling after IdentifierName */
  53657. comp_ctx->curr_func.reject_regexp_in_adv = 1;
  53658. duk__advance(comp_ctx);
  53659. return;
  53660. }
  53661. case DUK_TOK_LBRACKET: {
  53662. /* Property access expressions are critical for correct LHS ordering,
  53663. * see comments in duk__expr()!
  53664. */
  53665. /* XXX: optimize temp reg use */
  53666. /* XXX: similar coercion issue as in DUK_TOK_PERIOD */
  53667. /* XXX: coerce to regs? it might be better for enumeration use, where the
  53668. * same PROP ivalue is used multiple times. Or perhaps coerce PROP further
  53669. * there?
  53670. */
  53671. duk__ivalue_toplain(comp_ctx, left);
  53672. duk__expr_toplain(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/); /* Expression, ']' terminates */
  53673. duk__advance_expect(comp_ctx, DUK_TOK_RBRACKET);
  53674. res->t = DUK_IVAL_PROP;
  53675. duk__copy_ispec(comp_ctx, &res->x1, &res->x2); /* res.x1 -> res.x2 */
  53676. duk__copy_ispec(comp_ctx, &left->x1, &res->x1); /* left.x1 -> res.x1 */
  53677. return;
  53678. }
  53679. case DUK_TOK_LPAREN: {
  53680. /* function call */
  53681. duk_reg_t reg_cs = DUK__ALLOCTEMPS(comp_ctx, 2);
  53682. duk_int_t nargs;
  53683. duk_small_uint_t call_flags = 0;
  53684. /*
  53685. * XXX: attempt to get the call result to "next temp" whenever
  53686. * possible to avoid unnecessary register shuffles.
  53687. *
  53688. * XXX: CSPROP (and CSREG) can overwrite the call target register, and save one temp,
  53689. * if the call target is a temporary register and at the top of the temp reg "stack".
  53690. */
  53691. /*
  53692. * Setup call: target and 'this' binding. Three cases:
  53693. *
  53694. * 1. Identifier base (e.g. "foo()")
  53695. * 2. Property base (e.g. "foo.bar()")
  53696. * 3. Register base (e.g. "foo()()"; i.e. when a return value is a function)
  53697. */
  53698. if (left->t == DUK_IVAL_VAR) {
  53699. duk_hstring *h_varname;
  53700. duk_reg_t reg_varbind;
  53701. duk_regconst_t rc_varname;
  53702. DUK_DDD(DUK_DDDPRINT("function call with identifier base"));
  53703. h_varname = duk_get_hstring(ctx, left->x1.valstack_idx);
  53704. DUK_ASSERT(h_varname != NULL);
  53705. if (h_varname == DUK_HTHREAD_STRING_EVAL(thr)) {
  53706. /* Potential direct eval call detected, flag the CALL
  53707. * so that a run-time "direct eval" check is made and
  53708. * special behavior may be triggered. Note that this
  53709. * does not prevent 'eval' from being register bound.
  53710. */
  53711. DUK_DDD(DUK_DDDPRINT("function call with identifier 'eval' "
  53712. "-> enabling EVALCALL flag, marking function "
  53713. "as may_direct_eval"));
  53714. call_flags |= DUK_BC_CALL_FLAG_EVALCALL;
  53715. comp_ctx->curr_func.may_direct_eval = 1;
  53716. }
  53717. duk_dup(ctx, left->x1.valstack_idx);
  53718. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  53719. duk__emit_a_b(comp_ctx,
  53720. DUK_OP_CSREG,
  53721. (duk_regconst_t) (reg_cs + 0),
  53722. (duk_regconst_t) reg_varbind);
  53723. } else {
  53724. duk__emit_a_b(comp_ctx,
  53725. DUK_OP_CSVAR,
  53726. (duk_regconst_t) (reg_cs + 0),
  53727. rc_varname);
  53728. }
  53729. } else if (left->t == DUK_IVAL_PROP) {
  53730. DUK_DDD(DUK_DDDPRINT("function call with property base"));
  53731. duk__ispec_toforcedreg(comp_ctx, &left->x1, reg_cs + 0); /* base */
  53732. duk__ispec_toforcedreg(comp_ctx, &left->x2, reg_cs + 1); /* key */
  53733. duk__emit_a_b_c(comp_ctx,
  53734. DUK_OP_CSPROP,
  53735. (duk_regconst_t) (reg_cs + 0),
  53736. (duk_regconst_t) (reg_cs + 0),
  53737. (duk_regconst_t) (reg_cs + 1)); /* in-place setup */
  53738. } else {
  53739. DUK_DDD(DUK_DDDPRINT("function call with register base"));
  53740. duk__ivalue_toforcedreg(comp_ctx, left, reg_cs + 0);
  53741. duk__emit_a_b(comp_ctx,
  53742. DUK_OP_CSREG,
  53743. (duk_regconst_t) (reg_cs + 0),
  53744. (duk_regconst_t) (reg_cs + 0)); /* in-place setup */
  53745. }
  53746. DUK__SETTEMP(comp_ctx, reg_cs + 2);
  53747. nargs = duk__parse_arguments(comp_ctx, res); /* parse args starting from "next temp" */
  53748. /* Tailcalls are handled by back-patching the TAILCALL flag to the
  53749. * already emitted instruction later (in return statement parser).
  53750. * Since A and C have a special meaning here, they cannot be "shuffled".
  53751. */
  53752. duk__emit_a_b_c(comp_ctx,
  53753. DUK_OP_CALL | DUK__EMIT_FLAG_NO_SHUFFLE_A | DUK__EMIT_FLAG_NO_SHUFFLE_C,
  53754. (duk_regconst_t) call_flags /*flags*/,
  53755. (duk_regconst_t) reg_cs /*basereg*/,
  53756. (duk_regconst_t) nargs /*numargs*/);
  53757. DUK__SETTEMP(comp_ctx, reg_cs + 1); /* result in csreg */
  53758. res->t = DUK_IVAL_PLAIN;
  53759. res->x1.t = DUK_ISPEC_REGCONST;
  53760. res->x1.regconst = (duk_regconst_t) reg_cs;
  53761. return;
  53762. }
  53763. /* POSTFIX EXPRESSION */
  53764. case DUK_TOK_INCREMENT: {
  53765. args = (DUK_OP_POSTINCR << 8) + 0;
  53766. goto postincdec;
  53767. }
  53768. case DUK_TOK_DECREMENT: {
  53769. args = (DUK_OP_POSTDECR << 8) + 0;
  53770. goto postincdec;
  53771. }
  53772. /* MULTIPLICATIVE EXPRESSION */
  53773. case DUK_TOK_MUL: {
  53774. args = (DUK_OP_MUL << 8) + DUK__BP_MULTIPLICATIVE; /* UnaryExpression */
  53775. goto binary;
  53776. }
  53777. case DUK_TOK_DIV: {
  53778. args = (DUK_OP_DIV << 8) + DUK__BP_MULTIPLICATIVE; /* UnaryExpression */
  53779. goto binary;
  53780. }
  53781. case DUK_TOK_MOD: {
  53782. args = (DUK_OP_MOD << 8) + DUK__BP_MULTIPLICATIVE; /* UnaryExpression */
  53783. goto binary;
  53784. }
  53785. /* ADDITIVE EXPRESSION */
  53786. case DUK_TOK_ADD: {
  53787. args = (DUK_OP_ADD << 8) + DUK__BP_ADDITIVE; /* MultiplicativeExpression */
  53788. goto binary;
  53789. }
  53790. case DUK_TOK_SUB: {
  53791. args = (DUK_OP_SUB << 8) + DUK__BP_ADDITIVE; /* MultiplicativeExpression */
  53792. goto binary;
  53793. }
  53794. /* SHIFT EXPRESSION */
  53795. case DUK_TOK_ALSHIFT: {
  53796. /* << */
  53797. args = (DUK_OP_BASL << 8) + DUK__BP_SHIFT;
  53798. goto binary;
  53799. }
  53800. case DUK_TOK_ARSHIFT: {
  53801. /* >> */
  53802. args = (DUK_OP_BASR << 8) + DUK__BP_SHIFT;
  53803. goto binary;
  53804. }
  53805. case DUK_TOK_RSHIFT: {
  53806. /* >>> */
  53807. args = (DUK_OP_BLSR << 8) + DUK__BP_SHIFT;
  53808. goto binary;
  53809. }
  53810. /* RELATIONAL EXPRESSION */
  53811. case DUK_TOK_LT: {
  53812. /* < */
  53813. args = (DUK_OP_LT << 8) + DUK__BP_RELATIONAL;
  53814. goto binary;
  53815. }
  53816. case DUK_TOK_GT: {
  53817. args = (DUK_OP_GT << 8) + DUK__BP_RELATIONAL;
  53818. goto binary;
  53819. }
  53820. case DUK_TOK_LE: {
  53821. args = (DUK_OP_LE << 8) + DUK__BP_RELATIONAL;
  53822. goto binary;
  53823. }
  53824. case DUK_TOK_GE: {
  53825. args = (DUK_OP_GE << 8) + DUK__BP_RELATIONAL;
  53826. goto binary;
  53827. }
  53828. case DUK_TOK_INSTANCEOF: {
  53829. args = (1 << 16 /*is_extra*/) + (DUK_EXTRAOP_INSTOF << 8) + DUK__BP_RELATIONAL;
  53830. goto binary;
  53831. }
  53832. case DUK_TOK_IN: {
  53833. args = (1 << 16 /*is_extra*/) + (DUK_EXTRAOP_IN << 8) + DUK__BP_RELATIONAL;
  53834. goto binary;
  53835. }
  53836. /* EQUALITY EXPRESSION */
  53837. case DUK_TOK_EQ: {
  53838. args = (DUK_OP_EQ << 8) + DUK__BP_EQUALITY;
  53839. goto binary;
  53840. }
  53841. case DUK_TOK_NEQ: {
  53842. args = (DUK_OP_NEQ << 8) + DUK__BP_EQUALITY;
  53843. goto binary;
  53844. }
  53845. case DUK_TOK_SEQ: {
  53846. args = (DUK_OP_SEQ << 8) + DUK__BP_EQUALITY;
  53847. goto binary;
  53848. }
  53849. case DUK_TOK_SNEQ: {
  53850. args = (DUK_OP_SNEQ << 8) + DUK__BP_EQUALITY;
  53851. goto binary;
  53852. }
  53853. /* BITWISE EXPRESSIONS */
  53854. case DUK_TOK_BAND: {
  53855. args = (DUK_OP_BAND << 8) + DUK__BP_BAND;
  53856. goto binary;
  53857. }
  53858. case DUK_TOK_BXOR: {
  53859. args = (DUK_OP_BXOR << 8) + DUK__BP_BXOR;
  53860. goto binary;
  53861. }
  53862. case DUK_TOK_BOR: {
  53863. args = (DUK_OP_BOR << 8) + DUK__BP_BOR;
  53864. goto binary;
  53865. }
  53866. /* LOGICAL EXPRESSIONS */
  53867. case DUK_TOK_LAND: {
  53868. /* syntactically left-associative but parsed as right-associative */
  53869. args = (1 << 8) + DUK__BP_LAND - 1;
  53870. goto binary_logical;
  53871. }
  53872. case DUK_TOK_LOR: {
  53873. /* syntactically left-associative but parsed as right-associative */
  53874. args = (0 << 8) + DUK__BP_LOR - 1;
  53875. goto binary_logical;
  53876. }
  53877. /* CONDITIONAL EXPRESSION */
  53878. case DUK_TOK_QUESTION: {
  53879. /* XXX: common reg allocation need is to reuse a sub-expression's temp reg,
  53880. * but only if it really is a temp. Nothing fancy here now.
  53881. */
  53882. duk_reg_t reg_temp;
  53883. duk_int_t pc_jump1;
  53884. duk_int_t pc_jump2;
  53885. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  53886. duk__ivalue_toforcedreg(comp_ctx, left, reg_temp);
  53887. duk__emit_if_true_skip(comp_ctx, reg_temp);
  53888. pc_jump1 = duk__emit_jump_empty(comp_ctx); /* jump to false */
  53889. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_COMMA /*rbp_flags*/, reg_temp /*forced_reg*/); /* AssignmentExpression */
  53890. duk__advance_expect(comp_ctx, DUK_TOK_COLON);
  53891. pc_jump2 = duk__emit_jump_empty(comp_ctx); /* jump to end */
  53892. duk__patch_jump_here(comp_ctx, pc_jump1);
  53893. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_COMMA /*rbp_flags*/, reg_temp /*forced_reg*/); /* AssignmentExpression */
  53894. duk__patch_jump_here(comp_ctx, pc_jump2);
  53895. DUK__SETTEMP(comp_ctx, reg_temp + 1);
  53896. res->t = DUK_IVAL_PLAIN;
  53897. res->x1.t = DUK_ISPEC_REGCONST;
  53898. res->x1.regconst = (duk_regconst_t) reg_temp;
  53899. return;
  53900. }
  53901. /* ASSIGNMENT EXPRESSION */
  53902. case DUK_TOK_EQUALSIGN: {
  53903. /*
  53904. * Assignments are right associative, allows e.g.
  53905. * a = 5;
  53906. * a += b = 9; // same as a += (b = 9)
  53907. * -> expression value 14, a = 14, b = 9
  53908. *
  53909. * Right associativiness is reflected in the BP for recursion,
  53910. * "-1" ensures assignment operations are allowed.
  53911. *
  53912. * XXX: just use DUK__BP_COMMA (i.e. no need for 2-step bp levels)?
  53913. */
  53914. args = (DUK_OP_NONE << 8) + DUK__BP_ASSIGNMENT - 1; /* DUK_OP_NONE marks a 'plain' assignment */
  53915. goto assign;
  53916. }
  53917. case DUK_TOK_ADD_EQ: {
  53918. /* right associative */
  53919. args = (DUK_OP_ADD << 8) + DUK__BP_ASSIGNMENT - 1;
  53920. goto assign;
  53921. }
  53922. case DUK_TOK_SUB_EQ: {
  53923. /* right associative */
  53924. args = (DUK_OP_SUB << 8) + DUK__BP_ASSIGNMENT - 1;
  53925. goto assign;
  53926. }
  53927. case DUK_TOK_MUL_EQ: {
  53928. /* right associative */
  53929. args = (DUK_OP_MUL << 8) + DUK__BP_ASSIGNMENT - 1;
  53930. goto assign;
  53931. }
  53932. case DUK_TOK_DIV_EQ: {
  53933. /* right associative */
  53934. args = (DUK_OP_DIV << 8) + DUK__BP_ASSIGNMENT - 1;
  53935. goto assign;
  53936. }
  53937. case DUK_TOK_MOD_EQ: {
  53938. /* right associative */
  53939. args = (DUK_OP_MOD << 8) + DUK__BP_ASSIGNMENT - 1;
  53940. goto assign;
  53941. }
  53942. case DUK_TOK_ALSHIFT_EQ: {
  53943. /* right associative */
  53944. args = (DUK_OP_BASL << 8) + DUK__BP_ASSIGNMENT - 1;
  53945. goto assign;
  53946. }
  53947. case DUK_TOK_ARSHIFT_EQ: {
  53948. /* right associative */
  53949. args = (DUK_OP_BASR << 8) + DUK__BP_ASSIGNMENT - 1;
  53950. goto assign;
  53951. }
  53952. case DUK_TOK_RSHIFT_EQ: {
  53953. /* right associative */
  53954. args = (DUK_OP_BLSR << 8) + DUK__BP_ASSIGNMENT - 1;
  53955. goto assign;
  53956. }
  53957. case DUK_TOK_BAND_EQ: {
  53958. /* right associative */
  53959. args = (DUK_OP_BAND << 8) + DUK__BP_ASSIGNMENT - 1;
  53960. goto assign;
  53961. }
  53962. case DUK_TOK_BOR_EQ: {
  53963. /* right associative */
  53964. args = (DUK_OP_BOR << 8) + DUK__BP_ASSIGNMENT - 1;
  53965. goto assign;
  53966. }
  53967. case DUK_TOK_BXOR_EQ: {
  53968. /* right associative */
  53969. args = (DUK_OP_BXOR << 8) + DUK__BP_ASSIGNMENT - 1;
  53970. goto assign;
  53971. }
  53972. /* COMMA */
  53973. case DUK_TOK_COMMA: {
  53974. /* right associative */
  53975. duk__ivalue_toplain_ignore(comp_ctx, left); /* need side effects, not value */
  53976. duk__expr_toplain(comp_ctx, res, DUK__BP_COMMA - 1 /*rbp_flags*/);
  53977. /* return 'res' (of right part) as our result */
  53978. return;
  53979. }
  53980. default: {
  53981. break;
  53982. }
  53983. }
  53984. DUK_D(DUK_DPRINT("parse error: unexpected token: %ld", (long) tok));
  53985. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_PARSE_ERROR);
  53986. return;
  53987. #if 0
  53988. /* XXX: shared handling for 'duk__expr_lhs'? */
  53989. if (comp_ctx->curr_func.paren_level == 0 && XXX) {
  53990. comp_ctx->curr_func.duk__expr_lhs = 0;
  53991. }
  53992. #endif
  53993. binary:
  53994. /*
  53995. * Shared handling of binary operations
  53996. *
  53997. * args = (is_extraop << 16) + (opcode << 8) + rbp
  53998. */
  53999. {
  54000. duk__ivalue_toplain(comp_ctx, left);
  54001. duk__expr_toplain(comp_ctx, res, args & 0xff /*rbp_flags*/);
  54002. /* combine left->x1 and res->x1 (right->x1, really) -> (left->x1 OP res->x1) */
  54003. DUK_ASSERT(left->t == DUK_IVAL_PLAIN);
  54004. DUK_ASSERT(res->t == DUK_IVAL_PLAIN);
  54005. res->t = (args >> 16) ? DUK_IVAL_ARITH_EXTRAOP : DUK_IVAL_ARITH;
  54006. res->op = (args >> 8) & 0xff;
  54007. res->x2.t = res->x1.t;
  54008. res->x2.regconst = res->x1.regconst;
  54009. duk_copy(ctx, res->x1.valstack_idx, res->x2.valstack_idx);
  54010. res->x1.t = left->x1.t;
  54011. res->x1.regconst = left->x1.regconst;
  54012. duk_copy(ctx, left->x1.valstack_idx, res->x1.valstack_idx);
  54013. DUK_DDD(DUK_DDDPRINT("binary op, res: t=%ld, x1.t=%ld, x1.regconst=0x%08lx, x2.t=%ld, x2.regconst=0x%08lx",
  54014. (long) res->t, (long) res->x1.t, (unsigned long) res->x1.regconst, (long) res->x2.t, (unsigned long) res->x2.regconst));
  54015. return;
  54016. }
  54017. binary_logical:
  54018. /*
  54019. * Shared handling for logical AND and logical OR.
  54020. *
  54021. * args = (truthval << 8) + rbp
  54022. *
  54023. * Truthval determines when to skip right-hand-side.
  54024. * For logical AND truthval=1, for logical OR truthval=0.
  54025. *
  54026. * See doc/compiler.rst for discussion on compiling logical
  54027. * AND and OR expressions. The approach here is very simplistic,
  54028. * generating extra jumps and multiple evaluations of truth values,
  54029. * but generates code on-the-fly with only local back-patching.
  54030. *
  54031. * Both logical AND and OR are syntactically left-associated.
  54032. * However, logical ANDs are compiled as right associative
  54033. * expressions, i.e. "A && B && C" as "A && (B && C)", to allow
  54034. * skip jumps to skip over the entire tail. Similarly for logical OR.
  54035. */
  54036. {
  54037. duk_reg_t reg_temp;
  54038. duk_int_t pc_jump;
  54039. duk_small_uint_t args_truthval = args >> 8;
  54040. duk_small_uint_t args_rbp = args & 0xff;
  54041. /* XXX: unoptimal use of temps, resetting */
  54042. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  54043. duk__ivalue_toforcedreg(comp_ctx, left, reg_temp);
  54044. duk__emit_a_b(comp_ctx,
  54045. DUK_OP_IF | DUK__EMIT_FLAG_NO_SHUFFLE_A,
  54046. (duk_regconst_t) args_truthval,
  54047. (duk_regconst_t) reg_temp); /* skip jump conditionally */
  54048. pc_jump = duk__emit_jump_empty(comp_ctx);
  54049. duk__expr_toforcedreg(comp_ctx, res, args_rbp /*rbp_flags*/, reg_temp /*forced_reg*/);
  54050. duk__patch_jump_here(comp_ctx, pc_jump);
  54051. res->t = DUK_IVAL_PLAIN;
  54052. res->x1.t = DUK_ISPEC_REGCONST;
  54053. res->x1.regconst = (duk_regconst_t) reg_temp;
  54054. return;
  54055. }
  54056. assign:
  54057. /*
  54058. * Shared assignment expression handling
  54059. *
  54060. * args = (opcode << 8) + rbp
  54061. *
  54062. * If 'opcode' is DUK_OP_NONE, plain assignment without arithmetic.
  54063. * Syntactically valid left-hand-side forms which are not accepted as
  54064. * left-hand-side values (e.g. as in "f() = 1") must NOT cause a
  54065. * SyntaxError, but rather a run-time ReferenceError.
  54066. *
  54067. * Assignment expression value is conceptually the LHS/RHS value
  54068. * copied into a fresh temporary so that it won't change even if
  54069. * LHS/RHS values change (e.g. when they're identifiers). Doing this
  54070. * concretely produces inefficient bytecode, so we try to avoid the
  54071. * extra temporary for some known-to-be-safe cases. Currently the
  54072. * only safe case we detect is a "top level assignment", for example
  54073. * "x = y + z;", where the assignment expression value is ignored.
  54074. * See: test-dev-assign-expr.js and test-bug-assign-mutate-gh381.js.
  54075. */
  54076. {
  54077. duk_small_uint_t args_op = args >> 8;
  54078. duk_small_uint_t args_rbp = args & 0xff;
  54079. duk_bool_t toplevel_assign;
  54080. /* XXX: here we need to know if 'left' is left-hand-side compatible.
  54081. * That information is no longer available from current expr parsing
  54082. * state; it would need to be carried into the 'left' ivalue or by
  54083. * some other means.
  54084. */
  54085. /* A top-level assignment is e.g. "x = y;". For these it's safe
  54086. * to use the RHS as-is as the expression value, even if the RHS
  54087. * is a reg-bound identifier. The RHS ('res') is right associative
  54088. * so it has consumed all other assignment level operations; the
  54089. * only relevant lower binding power construct is comma operator
  54090. * which will ignore the expression value provided here.
  54091. */
  54092. toplevel_assign = (comp_ctx->curr_func.nud_count == 1 && /* one token before */
  54093. comp_ctx->curr_func.led_count == 1); /* one operator (= assign) */
  54094. DUK_DDD(DUK_DDDPRINT("assignment: nud_count=%ld, led_count=%ld, toplevel_assign=%ld",
  54095. (long) comp_ctx->curr_func.nud_count,
  54096. (long) comp_ctx->curr_func.led_count,
  54097. (long) toplevel_assign));
  54098. if (left->t == DUK_IVAL_VAR) {
  54099. duk_hstring *h_varname;
  54100. duk_reg_t reg_varbind;
  54101. duk_regconst_t rc_varname;
  54102. DUK_ASSERT(left->x1.t == DUK_ISPEC_VALUE); /* LHS is already side effect free */
  54103. /* Keep the RHS as an unresolved ivalue for now, so it
  54104. * can be a plain value or a unary/binary operation here.
  54105. * We resolve it before finishing but doing it later allows
  54106. * better bytecode in some cases.
  54107. */
  54108. duk__expr(comp_ctx, res, args_rbp /*rbp_flags*/);
  54109. h_varname = duk_get_hstring(ctx, left->x1.valstack_idx);
  54110. DUK_ASSERT(h_varname != NULL);
  54111. if (duk__hstring_is_eval_or_arguments_in_strict_mode(comp_ctx, h_varname)) {
  54112. /* E5 Section 11.13.1 (and others for other assignments), step 4 */
  54113. goto syntax_error_lvalue;
  54114. }
  54115. duk_dup(ctx, left->x1.valstack_idx);
  54116. (void) duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname);
  54117. if (args_op == DUK_OP_NONE) {
  54118. if (toplevel_assign) {
  54119. /* Any 'res' will do. */
  54120. DUK_DDD(DUK_DDDPRINT("plain assignment, toplevel assign, use as is"));
  54121. } else {
  54122. /* 'res' must be a plain ivalue, and not register-bound variable. */
  54123. DUK_DDD(DUK_DDDPRINT("plain assignment, not toplevel assign, ensure not a reg-bound identifier"));
  54124. if (res->t != DUK_IVAL_PLAIN || (res->x1.t == DUK_ISPEC_REGCONST &&
  54125. (res->x1.regconst & DUK__CONST_MARKER) == 0 &&
  54126. !DUK__ISTEMP(comp_ctx, res->x1.regconst))) {
  54127. duk__ivalue_totempconst(comp_ctx, res);
  54128. }
  54129. }
  54130. } else {
  54131. duk__ivalue_toregconst(comp_ctx, res);
  54132. DUK_ASSERT(res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_REGCONST);
  54133. if (reg_varbind >= 0) {
  54134. duk_reg_t reg_res;
  54135. if (toplevel_assign) {
  54136. /* 'reg_varbind' is the operation result and can also
  54137. * become the expression value for top level assignments
  54138. * such as: "var x; x += y;".
  54139. */
  54140. reg_res = reg_varbind;
  54141. } else {
  54142. /* Not safe to use 'reg_varbind' as assignment expression
  54143. * value, so go through a temp.
  54144. */
  54145. reg_res = DUK__ALLOCTEMP(comp_ctx);
  54146. }
  54147. duk__emit_a_b_c(comp_ctx,
  54148. args_op,
  54149. (duk_regconst_t) reg_res,
  54150. (duk_regconst_t) reg_varbind,
  54151. res->x1.regconst);
  54152. res->x1.regconst = (duk_regconst_t) reg_res;
  54153. } else {
  54154. /* When LHS is not register bound, always go through a
  54155. * temporary. No optimization for top level assignment.
  54156. */
  54157. duk_reg_t reg_temp;
  54158. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  54159. duk__emit_a_bc(comp_ctx,
  54160. DUK_OP_GETVAR,
  54161. (duk_regconst_t) reg_temp,
  54162. rc_varname);
  54163. duk__emit_a_b_c(comp_ctx,
  54164. args_op,
  54165. (duk_regconst_t) reg_temp,
  54166. (duk_regconst_t) reg_temp,
  54167. res->x1.regconst);
  54168. res->x1.regconst = (duk_regconst_t) reg_temp;
  54169. }
  54170. DUK_ASSERT(res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_REGCONST);
  54171. }
  54172. /* At this point 'res' holds the potential expression value.
  54173. * It can be basically any ivalue here, including a reg-bound
  54174. * identifier (if code above deems it safe) or a unary/binary
  54175. * operation. Operations must be resolved to a side effect free
  54176. * plain value, and the side effects must happen exactly once.
  54177. */
  54178. if (reg_varbind >= 0) {
  54179. if (res->t != DUK_IVAL_PLAIN) {
  54180. /* Resolve 'res' directly into the LHS binding, and use
  54181. * that as the expression value if safe. If not safe,
  54182. * resolve to a temp/const and copy to LHS.
  54183. */
  54184. if (toplevel_assign) {
  54185. duk__ivalue_toforcedreg(comp_ctx, res, (duk_int_t) reg_varbind);
  54186. } else {
  54187. duk__ivalue_totempconst(comp_ctx, res);
  54188. duk__copy_ivalue(comp_ctx, res, left); /* use 'left' as a temp */
  54189. duk__ivalue_toforcedreg(comp_ctx, left, (duk_int_t) reg_varbind);
  54190. }
  54191. } else {
  54192. /* Use 'res' as the expression value (it's side effect
  54193. * free and may be a plain value, a register, or a
  54194. * constant) and write it to the LHS binding too.
  54195. */
  54196. duk__copy_ivalue(comp_ctx, res, left); /* use 'left' as a temp */
  54197. duk__ivalue_toforcedreg(comp_ctx, left, (duk_int_t) reg_varbind);
  54198. }
  54199. } else {
  54200. /* Only a reg fits into 'A' so coerce 'res' into a register
  54201. * for PUTVAR.
  54202. *
  54203. * XXX: here the current A/B/C split is suboptimal: we could
  54204. * just use 9 bits for reg_res (and support constants) and 17
  54205. * instead of 18 bits for the varname const index.
  54206. */
  54207. duk__ivalue_toreg(comp_ctx, res);
  54208. duk__emit_a_bc(comp_ctx,
  54209. DUK_OP_PUTVAR | DUK__EMIT_FLAG_A_IS_SOURCE,
  54210. res->x1.regconst,
  54211. rc_varname);
  54212. }
  54213. /* 'res' contains expression value */
  54214. } else if (left->t == DUK_IVAL_PROP) {
  54215. /* E5 Section 11.13.1 (and others) step 4 never matches for prop writes -> no check */
  54216. duk_reg_t reg_obj;
  54217. duk_regconst_t rc_key;
  54218. duk_regconst_t rc_res;
  54219. duk_reg_t reg_temp;
  54220. /* Property access expressions ('a[b]') are critical to correct
  54221. * LHS evaluation ordering, see test-dev-assign-eval-order*.js.
  54222. * We must make sure that the LHS target slot (base object and
  54223. * key) don't change during RHS evaluation. The only concrete
  54224. * problem is a register reference to a variable-bound register
  54225. * (i.e., non-temp). Require temp regs for both key and base.
  54226. *
  54227. * Don't allow a constant for the object (even for a number
  54228. * etc), as it goes into the 'A' field of the opcode.
  54229. */
  54230. reg_obj = duk__ispec_toregconst_raw(comp_ctx,
  54231. &left->x1,
  54232. -1 /*forced_reg*/,
  54233. DUK__IVAL_FLAG_REQUIRE_TEMP /*flags*/);
  54234. rc_key = duk__ispec_toregconst_raw(comp_ctx,
  54235. &left->x2,
  54236. -1 /*forced_reg*/,
  54237. DUK__IVAL_FLAG_REQUIRE_TEMP | DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  54238. /* Evaluate RHS only when LHS is safe. */
  54239. duk__expr_toregconst(comp_ctx, res, args_rbp /*rbp_flags*/);
  54240. DUK_ASSERT(res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_REGCONST);
  54241. if (args_op == DUK_OP_NONE) {
  54242. rc_res = res->x1.regconst;
  54243. } else {
  54244. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  54245. duk__emit_a_b_c(comp_ctx,
  54246. DUK_OP_GETPROP,
  54247. (duk_regconst_t) reg_temp,
  54248. (duk_regconst_t) reg_obj,
  54249. rc_key);
  54250. duk__emit_a_b_c(comp_ctx,
  54251. args_op,
  54252. (duk_regconst_t) reg_temp,
  54253. (duk_regconst_t) reg_temp,
  54254. res->x1.regconst);
  54255. rc_res = (duk_regconst_t) reg_temp;
  54256. }
  54257. duk__emit_a_b_c(comp_ctx,
  54258. DUK_OP_PUTPROP | DUK__EMIT_FLAG_A_IS_SOURCE,
  54259. (duk_regconst_t) reg_obj,
  54260. rc_key,
  54261. rc_res);
  54262. res->t = DUK_IVAL_PLAIN;
  54263. res->x1.t = DUK_ISPEC_REGCONST;
  54264. res->x1.regconst = rc_res;
  54265. } else {
  54266. /* No support for lvalues returned from new or function call expressions.
  54267. * However, these must NOT cause compile-time SyntaxErrors, but run-time
  54268. * ReferenceErrors. Both left and right sides of the assignment must be
  54269. * evaluated before throwing a ReferenceError. For instance:
  54270. *
  54271. * f() = g();
  54272. *
  54273. * must result in f() being evaluated, then g() being evaluated, and
  54274. * finally, a ReferenceError being thrown. See E5 Section 11.13.1.
  54275. */
  54276. duk_regconst_t rc_res;
  54277. /* first evaluate LHS fully to ensure all side effects are out */
  54278. duk__ivalue_toplain_ignore(comp_ctx, left);
  54279. /* then evaluate RHS fully (its value becomes the expression value too) */
  54280. rc_res = duk__expr_toregconst(comp_ctx, res, args_rbp /*rbp_flags*/);
  54281. duk__emit_extraop_only(comp_ctx,
  54282. DUK_EXTRAOP_INVLHS);
  54283. /* XXX: this value is irrelevant because of INVLHS? */
  54284. res->t = DUK_IVAL_PLAIN;
  54285. res->x1.t = DUK_ISPEC_REGCONST;
  54286. res->x1.regconst = rc_res;
  54287. }
  54288. return;
  54289. }
  54290. postincdec:
  54291. {
  54292. /*
  54293. * Post-increment/decrement will return the original value as its
  54294. * result value. However, even that value will be coerced using
  54295. * ToNumber() which is quite awkward. Specific bytecode opcodes
  54296. * are used to handle these semantics.
  54297. *
  54298. * Note that post increment/decrement has a "no LineTerminator here"
  54299. * restriction. This is handled by duk__expr_lbp(), which forcibly terminates
  54300. * the previous expression if a LineTerminator occurs before '++'/'--'.
  54301. */
  54302. duk_reg_t reg_res;
  54303. duk_small_uint_t args_op = args >> 8;
  54304. /* Specific assumptions for opcode numbering. */
  54305. DUK_ASSERT(DUK_OP_POSTINCR + 4 == DUK_OP_POSTINCV);
  54306. DUK_ASSERT(DUK_OP_POSTDECR + 4 == DUK_OP_POSTDECV);
  54307. DUK_ASSERT(DUK_OP_POSTINCR + 8 == DUK_OP_POSTINCP);
  54308. DUK_ASSERT(DUK_OP_POSTDECR + 8 == DUK_OP_POSTDECP);
  54309. reg_res = DUK__ALLOCTEMP(comp_ctx);
  54310. if (left->t == DUK_IVAL_VAR) {
  54311. duk_hstring *h_varname;
  54312. duk_reg_t reg_varbind;
  54313. duk_regconst_t rc_varname;
  54314. h_varname = duk_get_hstring(ctx, left->x1.valstack_idx);
  54315. DUK_ASSERT(h_varname != NULL);
  54316. if (duk__hstring_is_eval_or_arguments_in_strict_mode(comp_ctx, h_varname)) {
  54317. goto syntax_error;
  54318. }
  54319. duk_dup(ctx, left->x1.valstack_idx);
  54320. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  54321. duk__emit_a_bc(comp_ctx,
  54322. args_op, /* e.g. DUK_OP_POSTINCR */
  54323. (duk_regconst_t) reg_res,
  54324. (duk_regconst_t) reg_varbind);
  54325. } else {
  54326. duk__emit_a_bc(comp_ctx,
  54327. args_op + 4, /* e.g. DUK_OP_POSTINCV */
  54328. (duk_regconst_t) reg_res,
  54329. rc_varname);
  54330. }
  54331. DUK_DDD(DUK_DDDPRINT("postincdec to '%!O' -> reg_varbind=%ld, rc_varname=%ld",
  54332. (duk_heaphdr *) h_varname, (long) reg_varbind, (long) rc_varname));
  54333. } else if (left->t == DUK_IVAL_PROP) {
  54334. duk_reg_t reg_obj; /* allocate to reg only (not const) */
  54335. duk_regconst_t rc_key;
  54336. reg_obj = duk__ispec_toregconst_raw(comp_ctx, &left->x1, -1 /*forced_reg*/, 0 /*flags*/); /* don't allow const */
  54337. rc_key = duk__ispec_toregconst_raw(comp_ctx, &left->x2, -1 /*forced_reg*/, DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  54338. duk__emit_a_b_c(comp_ctx,
  54339. args_op + 8, /* e.g. DUK_OP_POSTINCP */
  54340. (duk_regconst_t) reg_res,
  54341. (duk_regconst_t) reg_obj,
  54342. rc_key);
  54343. } else {
  54344. /* Technically return value is not needed because INVLHS will
  54345. * unconditially throw a ReferenceError. Coercion is necessary
  54346. * for proper semantics (consider ToNumber() called for an object).
  54347. * Use DUK_EXTRAOP_UNP with a dummy register to get ToNumber().
  54348. */
  54349. duk__ivalue_toforcedreg(comp_ctx, left, reg_res);
  54350. duk__emit_extraop_bc(comp_ctx,
  54351. DUK_EXTRAOP_UNP,
  54352. reg_res); /* for side effects, result ignored */
  54353. duk__emit_extraop_only(comp_ctx,
  54354. DUK_EXTRAOP_INVLHS);
  54355. }
  54356. res->t = DUK_IVAL_PLAIN;
  54357. res->x1.t = DUK_ISPEC_REGCONST;
  54358. res->x1.regconst = (duk_regconst_t) reg_res;
  54359. DUK__SETTEMP(comp_ctx, reg_res + 1);
  54360. return;
  54361. }
  54362. syntax_error:
  54363. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_EXPRESSION);
  54364. return;
  54365. syntax_error_lvalue:
  54366. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_LVALUE);
  54367. return;
  54368. }
  54369. DUK_LOCAL duk_small_uint_t duk__expr_lbp(duk_compiler_ctx *comp_ctx) {
  54370. duk_small_int_t tok = comp_ctx->curr_token.t;
  54371. DUK_ASSERT(tok >= DUK_TOK_MINVAL && tok <= DUK_TOK_MAXVAL);
  54372. DUK_ASSERT(sizeof(duk__token_lbp) == DUK_TOK_MAXVAL + 1);
  54373. /* XXX: integrate support for this into led() instead?
  54374. * Similar issue as post-increment/post-decrement.
  54375. */
  54376. /* prevent duk__expr_led() by using a binding power less than anything valid */
  54377. if (tok == DUK_TOK_IN && !comp_ctx->curr_func.allow_in) {
  54378. return 0;
  54379. }
  54380. if ((tok == DUK_TOK_DECREMENT || tok == DUK_TOK_INCREMENT) &&
  54381. (comp_ctx->curr_token.lineterm)) {
  54382. /* '++' or '--' in a post-increment/decrement position,
  54383. * and a LineTerminator occurs between the operator and
  54384. * the preceding expression. Force the previous expr
  54385. * to terminate, in effect treating e.g. "a,b\n++" as
  54386. * "a,b;++" (= SyntaxError).
  54387. */
  54388. return 0;
  54389. }
  54390. return DUK__TOKEN_LBP_GET_BP(duk__token_lbp[tok]); /* format is bit packed */
  54391. }
  54392. /*
  54393. * Expression parsing.
  54394. *
  54395. * Upon entry to 'expr' and its variants, 'curr_tok' is assumed to be the
  54396. * first token of the expression. Upon exit, 'curr_tok' will be the first
  54397. * token not part of the expression (e.g. semicolon terminating an expression
  54398. * statement).
  54399. */
  54400. #define DUK__EXPR_RBP_MASK 0xff
  54401. #define DUK__EXPR_FLAG_REJECT_IN (1 << 8) /* reject 'in' token (used for for-in) */
  54402. #define DUK__EXPR_FLAG_ALLOW_EMPTY (1 << 9) /* allow empty expression */
  54403. #define DUK__EXPR_FLAG_REQUIRE_INIT (1 << 10) /* require initializer for var/const */
  54404. /* main expression parser function */
  54405. DUK_LOCAL void duk__expr(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  54406. duk_hthread *thr = comp_ctx->thr;
  54407. duk_context *ctx = (duk_context *) thr;
  54408. duk_ivalue tmp_alloc; /* 'res' is used for "left", and 'tmp' for "right" */
  54409. duk_ivalue *tmp = &tmp_alloc;
  54410. duk_small_uint_t rbp;
  54411. DUK__RECURSION_INCREASE(comp_ctx, thr);
  54412. duk_require_stack(ctx, DUK__PARSE_EXPR_SLOTS);
  54413. /* filter out flags from exprtop rbp_flags here to save space */
  54414. rbp = rbp_flags & DUK__EXPR_RBP_MASK;
  54415. DUK_DDD(DUK_DDDPRINT("duk__expr(), rbp_flags=%ld, rbp=%ld, allow_in=%ld, paren_level=%ld",
  54416. (long) rbp_flags, (long) rbp, (long) comp_ctx->curr_func.allow_in,
  54417. (long) comp_ctx->curr_func.paren_level));
  54418. DUK_MEMZERO(&tmp_alloc, sizeof(tmp_alloc));
  54419. tmp->x1.valstack_idx = duk_get_top(ctx);
  54420. tmp->x2.valstack_idx = tmp->x1.valstack_idx + 1;
  54421. duk_push_undefined(ctx);
  54422. duk_push_undefined(ctx);
  54423. /* XXX: where to release temp regs in intermediate expressions?
  54424. * e.g. 1+2+3 -> don't inflate temp register count when parsing this.
  54425. * that particular expression temp regs can be forced here.
  54426. */
  54427. /* XXX: increase ctx->expr_tokens here for every consumed token
  54428. * (this would be a nice statistic)?
  54429. */
  54430. if (comp_ctx->curr_token.t == DUK_TOK_SEMICOLON || comp_ctx->curr_token.t == DUK_TOK_RPAREN) {
  54431. /* XXX: possibly incorrect handling of empty expression */
  54432. DUK_DDD(DUK_DDDPRINT("empty expression"));
  54433. if (!(rbp_flags & DUK__EXPR_FLAG_ALLOW_EMPTY)) {
  54434. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_EMPTY_EXPR_NOT_ALLOWED);
  54435. }
  54436. res->t = DUK_IVAL_PLAIN;
  54437. res->x1.t = DUK_ISPEC_VALUE;
  54438. duk_push_undefined(ctx);
  54439. duk_replace(ctx, res->x1.valstack_idx);
  54440. goto cleanup;
  54441. }
  54442. duk__advance(comp_ctx);
  54443. duk__expr_nud(comp_ctx, res); /* reuse 'res' as 'left' */
  54444. while (rbp < duk__expr_lbp(comp_ctx)) {
  54445. duk__advance(comp_ctx);
  54446. duk__expr_led(comp_ctx, res, tmp);
  54447. duk__copy_ivalue(comp_ctx, tmp, res); /* tmp -> res */
  54448. }
  54449. cleanup:
  54450. /* final result is already in 'res' */
  54451. duk_pop_2(ctx);
  54452. DUK__RECURSION_DECREASE(comp_ctx, thr);
  54453. }
  54454. DUK_LOCAL void duk__exprtop(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  54455. duk_hthread *thr = comp_ctx->thr;
  54456. /* Note: these variables must reside in 'curr_func' instead of the global
  54457. * context: when parsing function expressions, expression parsing is nested.
  54458. */
  54459. comp_ctx->curr_func.nud_count = 0;
  54460. comp_ctx->curr_func.led_count = 0;
  54461. comp_ctx->curr_func.paren_level = 0;
  54462. comp_ctx->curr_func.expr_lhs = 1;
  54463. comp_ctx->curr_func.allow_in = (rbp_flags & DUK__EXPR_FLAG_REJECT_IN ? 0 : 1);
  54464. duk__expr(comp_ctx, res, rbp_flags);
  54465. if (!(rbp_flags & DUK__EXPR_FLAG_ALLOW_EMPTY) && duk__expr_is_empty(comp_ctx)) {
  54466. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_EMPTY_EXPR_NOT_ALLOWED);
  54467. }
  54468. }
  54469. /* A bunch of helpers (for size optimization) that combine duk__expr()/duk__exprtop()
  54470. * and result conversions.
  54471. *
  54472. * Each helper needs at least 2-3 calls to make it worth while to wrap.
  54473. */
  54474. #if 0 /* unused */
  54475. DUK_LOCAL duk_reg_t duk__expr_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  54476. duk__expr(comp_ctx, res, rbp_flags);
  54477. return duk__ivalue_toreg(comp_ctx, res);
  54478. }
  54479. #endif
  54480. #if 0 /* unused */
  54481. DUK_LOCAL duk_reg_t duk__expr_totemp(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  54482. duk__expr(comp_ctx, res, rbp_flags);
  54483. return duk__ivalue_totemp(comp_ctx, res);
  54484. }
  54485. #endif
  54486. DUK_LOCAL void duk__expr_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags, duk_reg_t forced_reg) {
  54487. DUK_ASSERT(forced_reg >= 0);
  54488. duk__expr(comp_ctx, res, rbp_flags);
  54489. duk__ivalue_toforcedreg(comp_ctx, res, forced_reg);
  54490. }
  54491. DUK_LOCAL duk_regconst_t duk__expr_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  54492. duk__expr(comp_ctx, res, rbp_flags);
  54493. return duk__ivalue_toregconst(comp_ctx, res);
  54494. }
  54495. #if 0 /* unused */
  54496. DUK_LOCAL duk_regconst_t duk__expr_totempconst(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  54497. duk__expr(comp_ctx, res, rbp_flags);
  54498. return duk__ivalue_totempconst(comp_ctx, res);
  54499. }
  54500. #endif
  54501. DUK_LOCAL void duk__expr_toplain(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  54502. duk__expr(comp_ctx, res, rbp_flags);
  54503. duk__ivalue_toplain(comp_ctx, res);
  54504. }
  54505. DUK_LOCAL void duk__expr_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  54506. duk__expr(comp_ctx, res, rbp_flags);
  54507. duk__ivalue_toplain_ignore(comp_ctx, res);
  54508. }
  54509. DUK_LOCAL duk_reg_t duk__exprtop_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  54510. duk__exprtop(comp_ctx, res, rbp_flags);
  54511. return duk__ivalue_toreg(comp_ctx, res);
  54512. }
  54513. #if 0 /* unused */
  54514. DUK_LOCAL duk_reg_t duk__exprtop_totemp(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  54515. duk__exprtop(comp_ctx, res, rbp_flags);
  54516. return duk__ivalue_totemp(comp_ctx, res);
  54517. }
  54518. #endif
  54519. DUK_LOCAL void duk__exprtop_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags, duk_reg_t forced_reg) {
  54520. DUK_ASSERT(forced_reg >= 0);
  54521. duk__exprtop(comp_ctx, res, rbp_flags);
  54522. duk__ivalue_toforcedreg(comp_ctx, res, forced_reg);
  54523. }
  54524. DUK_LOCAL duk_regconst_t duk__exprtop_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  54525. duk__exprtop(comp_ctx, res, rbp_flags);
  54526. return duk__ivalue_toregconst(comp_ctx, res);
  54527. }
  54528. #if 0 /* unused */
  54529. DUK_LOCAL void duk__exprtop_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *res, int rbp_flags) {
  54530. duk__exprtop(comp_ctx, res, rbp_flags);
  54531. duk__ivalue_toplain_ignore(comp_ctx, res);
  54532. }
  54533. #endif
  54534. /*
  54535. * Parse an individual source element (top level statement) or a statement.
  54536. *
  54537. * Handles labeled statements automatically (peeling away labels before
  54538. * parsing an expression that follows the label(s)).
  54539. *
  54540. * Upon entry, 'curr_tok' contains the first token of the statement (parsed
  54541. * in "allow regexp literal" mode). Upon exit, 'curr_tok' contains the first
  54542. * token following the statement (if the statement has a terminator, this is
  54543. * the token after the terminator).
  54544. */
  54545. #ifdef DUK__HAS_VAL
  54546. #undef DUK__HAS_VAL
  54547. #endif
  54548. #ifdef DUK__HAS_TERM
  54549. #undef DUK__HAS_TERM
  54550. #endif
  54551. #ifdef DUK__ALLOW_AUTO_SEMI_ALWAYS
  54552. #undef DUK__ALLOW_AUTO_SEMI_ALWAYS
  54553. #endif
  54554. #ifdef DUK__STILL_PROLOGUE
  54555. #undef DUK__STILL_PROLOGUE
  54556. #endif
  54557. #ifdef DUK__IS_TERMINAL
  54558. #undef DUK__IS_TERMINAL
  54559. #endif
  54560. #define DUK__HAS_VAL (1 << 0) /* stmt has non-empty value */
  54561. #define DUK__HAS_TERM (1 << 1) /* stmt has explicit/implicit semicolon terminator */
  54562. #define DUK__ALLOW_AUTO_SEMI_ALWAYS (1 << 2) /* allow automatic semicolon even without lineterm (compatibility) */
  54563. #define DUK__STILL_PROLOGUE (1 << 3) /* statement does not terminate directive prologue */
  54564. #define DUK__IS_TERMINAL (1 << 4) /* statement is guaranteed to be terminal (control doesn't flow to next statement) */
  54565. /* Parse a single variable declaration (e.g. "i" or "i=10"). A leading 'var'
  54566. * has already been eaten. These is no return value in 'res', it is used only
  54567. * as a temporary.
  54568. *
  54569. * When called from 'for-in' statement parser, the initializer expression must
  54570. * not allow the 'in' token. The caller supply additional expression parsing
  54571. * flags (like DUK__EXPR_FLAG_REJECT_IN) in 'expr_flags'.
  54572. *
  54573. * Finally, out_rc_varname and out_reg_varbind are updated to reflect where
  54574. * the identifier is bound:
  54575. *
  54576. * If register bound: out_reg_varbind >= 0, out_rc_varname == 0 (ignore)
  54577. * If not register bound: out_reg_varbind < 0, out_rc_varname >= 0
  54578. *
  54579. * These allow the caller to use the variable for further assignment, e.g.
  54580. * as is done in 'for-in' parsing.
  54581. */
  54582. DUK_LOCAL void duk__parse_var_decl(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t expr_flags, duk_reg_t *out_reg_varbind, duk_regconst_t *out_rc_varname) {
  54583. duk_hthread *thr = comp_ctx->thr;
  54584. duk_context *ctx = (duk_context *) thr;
  54585. duk_hstring *h_varname;
  54586. duk_reg_t reg_varbind;
  54587. duk_regconst_t rc_varname;
  54588. /* assume 'var' has been eaten */
  54589. /* Note: Identifier rejects reserved words */
  54590. if (comp_ctx->curr_token.t != DUK_TOK_IDENTIFIER) {
  54591. goto syntax_error;
  54592. }
  54593. h_varname = comp_ctx->curr_token.str1;
  54594. DUK_ASSERT(h_varname != NULL);
  54595. /* strict mode restrictions (E5 Section 12.2.1) */
  54596. if (duk__hstring_is_eval_or_arguments_in_strict_mode(comp_ctx, h_varname)) {
  54597. goto syntax_error;
  54598. }
  54599. /* register declarations in first pass */
  54600. if (comp_ctx->curr_func.in_scanning) {
  54601. duk_uarridx_t n;
  54602. DUK_DDD(DUK_DDDPRINT("register variable declaration %!O in pass 1",
  54603. (duk_heaphdr *) h_varname));
  54604. n = (duk_uarridx_t) duk_get_length(ctx, comp_ctx->curr_func.decls_idx);
  54605. duk_push_hstring(ctx, h_varname);
  54606. duk_put_prop_index(ctx, comp_ctx->curr_func.decls_idx, n);
  54607. duk_push_int(ctx, DUK_DECL_TYPE_VAR + (0 << 8));
  54608. duk_put_prop_index(ctx, comp_ctx->curr_func.decls_idx, n + 1);
  54609. }
  54610. duk_push_hstring(ctx, h_varname); /* push before advancing to keep reachable */
  54611. /* register binding lookup is based on varmap (even in first pass) */
  54612. duk_dup_top(ctx);
  54613. (void) duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname);
  54614. duk__advance(comp_ctx); /* eat identifier */
  54615. if (comp_ctx->curr_token.t == DUK_TOK_EQUALSIGN) {
  54616. duk__advance(comp_ctx);
  54617. DUK_DDD(DUK_DDDPRINT("vardecl, assign to '%!O' -> reg_varbind=%ld, rc_varname=%ld",
  54618. (duk_heaphdr *) h_varname, (long) reg_varbind, (long) rc_varname));
  54619. duk__exprtop(comp_ctx, res, DUK__BP_COMMA | expr_flags /*rbp_flags*/); /* AssignmentExpression */
  54620. if (reg_varbind >= 0) {
  54621. duk__ivalue_toforcedreg(comp_ctx, res, reg_varbind);
  54622. } else {
  54623. duk_reg_t reg_val;
  54624. reg_val = duk__ivalue_toreg(comp_ctx, res);
  54625. duk__emit_a_bc(comp_ctx,
  54626. DUK_OP_PUTVAR | DUK__EMIT_FLAG_A_IS_SOURCE,
  54627. (duk_regconst_t) reg_val,
  54628. rc_varname);
  54629. }
  54630. } else {
  54631. if (expr_flags & DUK__EXPR_FLAG_REQUIRE_INIT) {
  54632. /* Used for minimal 'const': initializer required. */
  54633. goto syntax_error;
  54634. }
  54635. }
  54636. duk_pop(ctx); /* pop varname */
  54637. *out_rc_varname = rc_varname;
  54638. *out_reg_varbind = reg_varbind;
  54639. return;
  54640. syntax_error:
  54641. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_VAR_DECLARATION);
  54642. }
  54643. DUK_LOCAL void duk__parse_var_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t expr_flags) {
  54644. duk_reg_t reg_varbind;
  54645. duk_regconst_t rc_varname;
  54646. duk__advance(comp_ctx); /* eat 'var' */
  54647. for (;;) {
  54648. /* rc_varname and reg_varbind are ignored here */
  54649. duk__parse_var_decl(comp_ctx, res, 0 | expr_flags, &reg_varbind, &rc_varname);
  54650. if (comp_ctx->curr_token.t != DUK_TOK_COMMA) {
  54651. break;
  54652. }
  54653. duk__advance(comp_ctx);
  54654. }
  54655. }
  54656. DUK_LOCAL void duk__parse_for_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site) {
  54657. duk_hthread *thr = comp_ctx->thr;
  54658. duk_context *ctx = (duk_context *) thr;
  54659. duk_int_t pc_v34_lhs; /* start variant 3/4 left-hand-side code (L1 in doc/compiler.rst example) */
  54660. duk_reg_t temp_reset; /* knock back "next temp" to this whenever possible */
  54661. duk_reg_t reg_temps; /* preallocated temporaries (2) for variants 3 and 4 */
  54662. DUK_DDD(DUK_DDDPRINT("start parsing a for/for-in statement"));
  54663. /* Two temporaries are preallocated here for variants 3 and 4 which need
  54664. * registers which are never clobbered by expressions in the loop
  54665. * (concretely: for the enumerator object and the next enumerated value).
  54666. * Variants 1 and 2 "release" these temps.
  54667. */
  54668. reg_temps = DUK__ALLOCTEMPS(comp_ctx, 2);
  54669. temp_reset = DUK__GETTEMP(comp_ctx);
  54670. /*
  54671. * For/for-in main variants are:
  54672. *
  54673. * 1. for (ExpressionNoIn_opt; Expression_opt; Expression_opt) Statement
  54674. * 2. for (var VariableDeclarationNoIn; Expression_opt; Expression_opt) Statement
  54675. * 3. for (LeftHandSideExpression in Expression) Statement
  54676. * 4. for (var VariableDeclarationNoIn in Expression) Statement
  54677. *
  54678. * Parsing these without arbitrary lookahead or backtracking is relatively
  54679. * tricky but we manage to do so for now.
  54680. *
  54681. * See doc/compiler.rst for a detailed discussion of control flow
  54682. * issues, evaluation order issues, etc.
  54683. */
  54684. duk__advance(comp_ctx); /* eat 'for' */
  54685. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  54686. DUK_DDD(DUK_DDDPRINT("detecting for/for-in loop variant, pc=%ld", (long) duk__get_current_pc(comp_ctx)));
  54687. /* a label site has been emitted by duk__parse_stmt() automatically
  54688. * (it will also emit the ENDLABEL).
  54689. */
  54690. if (comp_ctx->curr_token.t == DUK_TOK_VAR) {
  54691. /*
  54692. * Variant 2 or 4
  54693. */
  54694. duk_reg_t reg_varbind; /* variable binding register if register-bound (otherwise < 0) */
  54695. duk_regconst_t rc_varname; /* variable name reg/const, if variable not register-bound */
  54696. duk__advance(comp_ctx); /* eat 'var' */
  54697. duk__parse_var_decl(comp_ctx, res, DUK__EXPR_FLAG_REJECT_IN, &reg_varbind, &rc_varname);
  54698. DUK__SETTEMP(comp_ctx, temp_reset);
  54699. if (comp_ctx->curr_token.t == DUK_TOK_IN) {
  54700. /*
  54701. * Variant 4
  54702. */
  54703. DUK_DDD(DUK_DDDPRINT("detected for variant 4: for (var VariableDeclarationNoIn in Expression) Statement"));
  54704. pc_v34_lhs = duk__get_current_pc(comp_ctx); /* jump is inserted here */
  54705. if (reg_varbind >= 0) {
  54706. duk__emit_a_bc(comp_ctx,
  54707. DUK_OP_LDREG,
  54708. (duk_regconst_t) reg_varbind,
  54709. (duk_regconst_t) (reg_temps + 0));
  54710. } else {
  54711. duk__emit_a_bc(comp_ctx,
  54712. DUK_OP_PUTVAR | DUK__EMIT_FLAG_A_IS_SOURCE,
  54713. (duk_regconst_t) (reg_temps + 0),
  54714. rc_varname);
  54715. }
  54716. goto parse_3_or_4;
  54717. } else {
  54718. /*
  54719. * Variant 2
  54720. */
  54721. DUK_DDD(DUK_DDDPRINT("detected for variant 2: for (var VariableDeclarationNoIn; Expression_opt; Expression_opt) Statement"));
  54722. for (;;) {
  54723. /* more initializers */
  54724. if (comp_ctx->curr_token.t != DUK_TOK_COMMA) {
  54725. break;
  54726. }
  54727. DUK_DDD(DUK_DDDPRINT("variant 2 has another variable initializer"));
  54728. duk__advance(comp_ctx); /* eat comma */
  54729. duk__parse_var_decl(comp_ctx, res, DUK__EXPR_FLAG_REJECT_IN, &reg_varbind, &rc_varname);
  54730. }
  54731. goto parse_1_or_2;
  54732. }
  54733. } else {
  54734. /*
  54735. * Variant 1 or 3
  54736. */
  54737. pc_v34_lhs = duk__get_current_pc(comp_ctx); /* jump is inserted here (variant 3) */
  54738. /* Note that duk__exprtop() here can clobber any reg above current temp_next,
  54739. * so any loop variables (e.g. enumerator) must be "preallocated".
  54740. */
  54741. /* don't coerce yet to a plain value (variant 3 needs special handling) */
  54742. duk__exprtop(comp_ctx, res, DUK__BP_FOR_EXPR | DUK__EXPR_FLAG_REJECT_IN | DUK__EXPR_FLAG_ALLOW_EMPTY /*rbp_flags*/); /* Expression */
  54743. if (comp_ctx->curr_token.t == DUK_TOK_IN) {
  54744. /*
  54745. * Variant 3
  54746. */
  54747. /* XXX: need to determine LHS type, and check that it is LHS compatible */
  54748. DUK_DDD(DUK_DDDPRINT("detected for variant 3: for (LeftHandSideExpression in Expression) Statement"));
  54749. if (duk__expr_is_empty(comp_ctx)) {
  54750. goto syntax_error; /* LeftHandSideExpression does not allow empty expression */
  54751. }
  54752. if (res->t == DUK_IVAL_VAR) {
  54753. duk_reg_t reg_varbind;
  54754. duk_regconst_t rc_varname;
  54755. duk_dup(ctx, res->x1.valstack_idx);
  54756. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  54757. duk__emit_a_bc(comp_ctx,
  54758. DUK_OP_LDREG,
  54759. (duk_regconst_t) reg_varbind,
  54760. (duk_regconst_t) (reg_temps + 0));
  54761. } else {
  54762. duk__emit_a_bc(comp_ctx,
  54763. DUK_OP_PUTVAR | DUK__EMIT_FLAG_A_IS_SOURCE,
  54764. (duk_regconst_t) (reg_temps + 0),
  54765. rc_varname);
  54766. }
  54767. } else if (res->t == DUK_IVAL_PROP) {
  54768. /* Don't allow a constant for the object (even for a number etc), as
  54769. * it goes into the 'A' field of the opcode.
  54770. */
  54771. duk_reg_t reg_obj;
  54772. duk_regconst_t rc_key;
  54773. reg_obj = duk__ispec_toregconst_raw(comp_ctx, &res->x1, -1 /*forced_reg*/, 0 /*flags*/); /* don't allow const */
  54774. rc_key = duk__ispec_toregconst_raw(comp_ctx, &res->x2, -1 /*forced_reg*/, DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  54775. duk__emit_a_b_c(comp_ctx,
  54776. DUK_OP_PUTPROP | DUK__EMIT_FLAG_A_IS_SOURCE,
  54777. (duk_regconst_t) reg_obj,
  54778. rc_key,
  54779. (duk_regconst_t) (reg_temps + 0));
  54780. } else {
  54781. duk__ivalue_toplain_ignore(comp_ctx, res); /* just in case */
  54782. duk__emit_extraop_only(comp_ctx,
  54783. DUK_EXTRAOP_INVLHS);
  54784. }
  54785. goto parse_3_or_4;
  54786. } else {
  54787. /*
  54788. * Variant 1
  54789. */
  54790. DUK_DDD(DUK_DDDPRINT("detected for variant 1: for (ExpressionNoIn_opt; Expression_opt; Expression_opt) Statement"));
  54791. duk__ivalue_toplain_ignore(comp_ctx, res);
  54792. goto parse_1_or_2;
  54793. }
  54794. }
  54795. parse_1_or_2:
  54796. /*
  54797. * Parse variant 1 or 2. The first part expression (which differs
  54798. * in the variants) has already been parsed and its code emitted.
  54799. *
  54800. * reg_temps + 0: unused
  54801. * reg_temps + 1: unused
  54802. */
  54803. {
  54804. duk_regconst_t rc_cond;
  54805. duk_int_t pc_l1, pc_l2, pc_l3, pc_l4;
  54806. duk_int_t pc_jumpto_l3, pc_jumpto_l4;
  54807. duk_bool_t expr_c_empty;
  54808. DUK_DDD(DUK_DDDPRINT("shared code for parsing variants 1 and 2"));
  54809. /* "release" preallocated temps since we won't need them */
  54810. temp_reset = reg_temps + 0;
  54811. DUK__SETTEMP(comp_ctx, temp_reset);
  54812. duk__advance_expect(comp_ctx, DUK_TOK_SEMICOLON);
  54813. pc_l1 = duk__get_current_pc(comp_ctx);
  54814. duk__exprtop(comp_ctx, res, DUK__BP_FOR_EXPR | DUK__EXPR_FLAG_ALLOW_EMPTY /*rbp_flags*/); /* Expression_opt */
  54815. if (duk__expr_is_empty(comp_ctx)) {
  54816. /* no need to coerce */
  54817. pc_jumpto_l3 = duk__emit_jump_empty(comp_ctx); /* to body */
  54818. pc_jumpto_l4 = -1; /* omitted */
  54819. } else {
  54820. rc_cond = duk__ivalue_toregconst(comp_ctx, res);
  54821. duk__emit_if_false_skip(comp_ctx, rc_cond);
  54822. pc_jumpto_l3 = duk__emit_jump_empty(comp_ctx); /* to body */
  54823. pc_jumpto_l4 = duk__emit_jump_empty(comp_ctx); /* to exit */
  54824. }
  54825. DUK__SETTEMP(comp_ctx, temp_reset);
  54826. duk__advance_expect(comp_ctx, DUK_TOK_SEMICOLON);
  54827. pc_l2 = duk__get_current_pc(comp_ctx);
  54828. duk__exprtop(comp_ctx, res, DUK__BP_FOR_EXPR | DUK__EXPR_FLAG_ALLOW_EMPTY /*rbp_flags*/); /* Expression_opt */
  54829. if (duk__expr_is_empty(comp_ctx)) {
  54830. /* no need to coerce */
  54831. expr_c_empty = 1;
  54832. /* JUMP L1 omitted */
  54833. } else {
  54834. duk__ivalue_toplain_ignore(comp_ctx, res);
  54835. expr_c_empty = 0;
  54836. duk__emit_jump(comp_ctx, pc_l1);
  54837. }
  54838. DUK__SETTEMP(comp_ctx, temp_reset);
  54839. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  54840. pc_l3 = duk__get_current_pc(comp_ctx);
  54841. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  54842. if (expr_c_empty) {
  54843. duk__emit_jump(comp_ctx, pc_l1);
  54844. } else {
  54845. duk__emit_jump(comp_ctx, pc_l2);
  54846. }
  54847. /* temp reset is not necessary after duk__parse_stmt(), which already does it */
  54848. pc_l4 = duk__get_current_pc(comp_ctx);
  54849. DUK_DDD(DUK_DDDPRINT("patching jumps: jumpto_l3: %ld->%ld, jumpto_l4: %ld->%ld, "
  54850. "break: %ld->%ld, continue: %ld->%ld",
  54851. (long) pc_jumpto_l3, (long) pc_l3, (long) pc_jumpto_l4, (long) pc_l4,
  54852. (long) (pc_label_site + 1), (long) pc_l4, (long) (pc_label_site + 2), (long) pc_l2));
  54853. duk__patch_jump(comp_ctx, pc_jumpto_l3, pc_l3);
  54854. duk__patch_jump(comp_ctx, pc_jumpto_l4, pc_l4);
  54855. duk__patch_jump(comp_ctx,
  54856. pc_label_site + 1,
  54857. pc_l4); /* break jump */
  54858. duk__patch_jump(comp_ctx,
  54859. pc_label_site + 2,
  54860. expr_c_empty ? pc_l1 : pc_l2); /* continue jump */
  54861. }
  54862. goto finished;
  54863. parse_3_or_4:
  54864. /*
  54865. * Parse variant 3 or 4.
  54866. *
  54867. * For variant 3 (e.g. "for (A in C) D;") the code for A (except the
  54868. * final property/variable write) has already been emitted. The first
  54869. * instruction of that code is at pc_v34_lhs; a JUMP needs to be inserted
  54870. * there to satisfy control flow needs.
  54871. *
  54872. * For variant 4, if the variable declaration had an initializer
  54873. * (e.g. "for (var A = B in C) D;") the code for the assignment
  54874. * (B) has already been emitted.
  54875. *
  54876. * Variables set before entering here:
  54877. *
  54878. * pc_v34_lhs: insert a "JUMP L2" here (see doc/compiler.rst example).
  54879. * reg_temps + 0: iteration target value (written to LHS)
  54880. * reg_temps + 1: enumerator object
  54881. */
  54882. {
  54883. duk_int_t pc_l1, pc_l2, pc_l3, pc_l4, pc_l5;
  54884. duk_int_t pc_jumpto_l2, pc_jumpto_l3, pc_jumpto_l4, pc_jumpto_l5;
  54885. duk_reg_t reg_target;
  54886. DUK_DDD(DUK_DDDPRINT("shared code for parsing variants 3 and 4, pc_v34_lhs=%ld", (long) pc_v34_lhs));
  54887. DUK__SETTEMP(comp_ctx, temp_reset);
  54888. /* First we need to insert a jump in the middle of previously
  54889. * emitted code to get the control flow right. No jumps can
  54890. * cross the position where the jump is inserted. See doc/compiler.rst
  54891. * for discussion on the intricacies of control flow and side effects
  54892. * for variants 3 and 4.
  54893. */
  54894. duk__insert_jump_entry(comp_ctx, pc_v34_lhs);
  54895. pc_jumpto_l2 = pc_v34_lhs; /* inserted jump */
  54896. pc_l1 = pc_v34_lhs + 1; /* +1, right after inserted jump */
  54897. /* The code for writing reg_temps + 0 to the left hand side has already
  54898. * been emitted.
  54899. */
  54900. pc_jumpto_l3 = duk__emit_jump_empty(comp_ctx); /* -> loop body */
  54901. duk__advance(comp_ctx); /* eat 'in' */
  54902. /* Parse enumeration target and initialize enumerator. For 'null' and 'undefined',
  54903. * INITENUM will creates a 'null' enumerator which works like an empty enumerator
  54904. * (E5 Section 12.6.4, step 3). Note that INITENUM requires the value to be in a
  54905. * register (constant not allowed).
  54906. */
  54907. pc_l2 = duk__get_current_pc(comp_ctx);
  54908. reg_target = duk__exprtop_toreg(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/); /* Expression */
  54909. duk__emit_extraop_b_c(comp_ctx,
  54910. DUK_EXTRAOP_INITENUM | DUK__EMIT_FLAG_B_IS_TARGET,
  54911. (duk_regconst_t) (reg_temps + 1),
  54912. (duk_regconst_t) reg_target);
  54913. pc_jumpto_l4 = duk__emit_jump_empty(comp_ctx);
  54914. DUK__SETTEMP(comp_ctx, temp_reset);
  54915. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  54916. pc_l3 = duk__get_current_pc(comp_ctx);
  54917. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  54918. /* temp reset is not necessary after duk__parse_stmt(), which already does it */
  54919. /* NEXTENUM needs a jump slot right after the main opcode.
  54920. * We need the code emitter to reserve the slot: if there's
  54921. * target shuffling, the target shuffle opcodes must happen
  54922. * after the jump slot (for NEXTENUM the shuffle opcodes are
  54923. * not needed if the enum is finished).
  54924. */
  54925. pc_l4 = duk__get_current_pc(comp_ctx);
  54926. duk__emit_extraop_b_c(comp_ctx,
  54927. DUK_EXTRAOP_NEXTENUM | DUK__EMIT_FLAG_B_IS_TARGET | DUK__EMIT_FLAG_RESERVE_JUMPSLOT,
  54928. (duk_regconst_t) (reg_temps + 0),
  54929. (duk_regconst_t) (reg_temps + 1));
  54930. pc_jumpto_l5 = comp_ctx->emit_jumpslot_pc; /* NEXTENUM jump slot: executed when enum finished */
  54931. duk__emit_jump(comp_ctx, pc_l1); /* jump to next loop, using reg_v34_iter as iterated value */
  54932. pc_l5 = duk__get_current_pc(comp_ctx);
  54933. /* XXX: since the enumerator may be a memory expensive object,
  54934. * perhaps clear it explicitly here? If so, break jump must
  54935. * go through this clearing operation.
  54936. */
  54937. DUK_DDD(DUK_DDDPRINT("patching jumps: jumpto_l2: %ld->%ld, jumpto_l3: %ld->%ld, "
  54938. "jumpto_l4: %ld->%ld, jumpto_l5: %ld->%ld, "
  54939. "break: %ld->%ld, continue: %ld->%ld",
  54940. (long) pc_jumpto_l2, (long) pc_l2, (long) pc_jumpto_l3, (long) pc_l3,
  54941. (long) pc_jumpto_l4, (long) pc_l4, (long) pc_jumpto_l5, (long) pc_l5,
  54942. (long) (pc_label_site + 1), (long) pc_l5, (long) (pc_label_site + 2), (long) pc_l4));
  54943. duk__patch_jump(comp_ctx, pc_jumpto_l2, pc_l2);
  54944. duk__patch_jump(comp_ctx, pc_jumpto_l3, pc_l3);
  54945. duk__patch_jump(comp_ctx, pc_jumpto_l4, pc_l4);
  54946. duk__patch_jump(comp_ctx, pc_jumpto_l5, pc_l5);
  54947. duk__patch_jump(comp_ctx, pc_label_site + 1, pc_l5); /* break jump */
  54948. duk__patch_jump(comp_ctx, pc_label_site + 2, pc_l4); /* continue jump */
  54949. }
  54950. goto finished;
  54951. finished:
  54952. DUK_DDD(DUK_DDDPRINT("end parsing a for/for-in statement"));
  54953. return;
  54954. syntax_error:
  54955. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_FOR);
  54956. }
  54957. DUK_LOCAL void duk__parse_switch_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site) {
  54958. duk_hthread *thr = comp_ctx->thr;
  54959. duk_reg_t temp_at_loop;
  54960. duk_regconst_t rc_switch; /* reg/const for switch value */
  54961. duk_regconst_t rc_case; /* reg/const for case value */
  54962. duk_reg_t reg_temp; /* general temp register */
  54963. duk_int_t pc_prevcase = -1;
  54964. duk_int_t pc_prevstmt = -1;
  54965. duk_int_t pc_default = -1; /* -1 == not set, -2 == pending (next statement list) */
  54966. /* Note: negative pc values are ignored when patching jumps, so no explicit checks needed */
  54967. /*
  54968. * Switch is pretty complicated because of several conflicting concerns:
  54969. *
  54970. * - Want to generate code without an intermediate representation,
  54971. * i.e., in one go
  54972. *
  54973. * - Case selectors are expressions, not values, and may thus e.g. throw
  54974. * exceptions (which causes evaluation order concerns)
  54975. *
  54976. * - Evaluation semantics of case selectors and default clause need to be
  54977. * carefully implemented to provide correct behavior even with case value
  54978. * side effects
  54979. *
  54980. * - Fall through case and default clauses; avoiding dead JUMPs if case
  54981. * ends with an unconditional jump (a break or a continue)
  54982. *
  54983. * - The same case value may occur multiple times, but evaluation rules
  54984. * only process the first match before switching to a "propagation" mode
  54985. * where case values are no longer evaluated
  54986. *
  54987. * See E5 Section 12.11. Also see doc/compiler.rst for compilation
  54988. * discussion.
  54989. */
  54990. duk__advance(comp_ctx);
  54991. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  54992. rc_switch = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  54993. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  54994. duk__advance_expect(comp_ctx, DUK_TOK_LCURLY);
  54995. DUK_DDD(DUK_DDDPRINT("switch value in register %ld", (long) rc_switch));
  54996. temp_at_loop = DUK__GETTEMP(comp_ctx);
  54997. for (;;) {
  54998. duk_int_t num_stmts;
  54999. duk_small_int_t tok;
  55000. /* sufficient for keeping temp reg numbers in check */
  55001. DUK__SETTEMP(comp_ctx, temp_at_loop);
  55002. if (comp_ctx->curr_token.t == DUK_TOK_RCURLY) {
  55003. break;
  55004. }
  55005. /*
  55006. * Parse a case or default clause.
  55007. */
  55008. if (comp_ctx->curr_token.t == DUK_TOK_CASE) {
  55009. /*
  55010. * Case clause.
  55011. *
  55012. * Note: cannot use reg_case as a temp register (for SEQ target)
  55013. * because it may be a constant.
  55014. */
  55015. duk__patch_jump_here(comp_ctx, pc_prevcase); /* chain jumps for case
  55016. * evaluation and checking
  55017. */
  55018. duk__advance(comp_ctx);
  55019. rc_case = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  55020. duk__advance_expect(comp_ctx, DUK_TOK_COLON);
  55021. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  55022. duk__emit_a_b_c(comp_ctx,
  55023. DUK_OP_SEQ,
  55024. (duk_regconst_t) reg_temp,
  55025. rc_switch,
  55026. rc_case);
  55027. duk__emit_if_true_skip(comp_ctx, (duk_regconst_t) reg_temp);
  55028. /* jump to next case clause */
  55029. pc_prevcase = duk__emit_jump_empty(comp_ctx); /* no match, next case */
  55030. /* statements go here (if any) on next loop */
  55031. } else if (comp_ctx->curr_token.t == DUK_TOK_DEFAULT) {
  55032. /*
  55033. * Default clause.
  55034. */
  55035. if (pc_default >= 0) {
  55036. goto syntax_error;
  55037. }
  55038. duk__advance(comp_ctx);
  55039. duk__advance_expect(comp_ctx, DUK_TOK_COLON);
  55040. /* Fix for https://github.com/svaarala/duktape/issues/155:
  55041. * If 'default' is first clause (detected by pc_prevcase < 0)
  55042. * we need to ensure we stay in the matching chain.
  55043. */
  55044. if (pc_prevcase < 0) {
  55045. DUK_DD(DUK_DDPRINT("default clause is first, emit prevcase jump"));
  55046. pc_prevcase = duk__emit_jump_empty(comp_ctx);
  55047. }
  55048. /* default clause matches next statement list (if any) */
  55049. pc_default = -2;
  55050. } else {
  55051. /* Code is not accepted before the first case/default clause */
  55052. goto syntax_error;
  55053. }
  55054. /*
  55055. * Parse code after the clause. Possible terminators are
  55056. * 'case', 'default', and '}'.
  55057. *
  55058. * Note that there may be no code at all, not even an empty statement,
  55059. * between case clauses. This must be handled just like an empty statement
  55060. * (omitting seemingly pointless JUMPs), to avoid situations like
  55061. * test-bug-case-fallthrough.js.
  55062. */
  55063. num_stmts = 0;
  55064. if (pc_default == -2) {
  55065. pc_default = duk__get_current_pc(comp_ctx);
  55066. }
  55067. /* Note: this is correct even for default clause statements:
  55068. * they participate in 'fall-through' behavior even if the
  55069. * default clause is in the middle.
  55070. */
  55071. duk__patch_jump_here(comp_ctx, pc_prevstmt); /* chain jumps for 'fall-through'
  55072. * after a case matches.
  55073. */
  55074. for (;;) {
  55075. tok = comp_ctx->curr_token.t;
  55076. if (tok == DUK_TOK_CASE || tok == DUK_TOK_DEFAULT ||
  55077. tok == DUK_TOK_RCURLY) {
  55078. break;
  55079. }
  55080. num_stmts++;
  55081. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  55082. }
  55083. /* fall-through jump to next code of next case (backpatched) */
  55084. pc_prevstmt = duk__emit_jump_empty(comp_ctx);
  55085. /* XXX: would be nice to omit this jump when the jump is not
  55086. * reachable, at least in the obvious cases (such as the case
  55087. * ending with a 'break'.
  55088. *
  55089. * Perhaps duk__parse_stmt() could provide some info on whether
  55090. * the statement is a "dead end"?
  55091. *
  55092. * If implemented, just set pc_prevstmt to -1 when not needed.
  55093. */
  55094. }
  55095. DUK_ASSERT(comp_ctx->curr_token.t == DUK_TOK_RCURLY);
  55096. duk__advance(comp_ctx);
  55097. /* default case control flow patchup; note that if pc_prevcase < 0
  55098. * (i.e. no case clauses), control enters default case automatically.
  55099. */
  55100. if (pc_default >= 0) {
  55101. /* default case exists: go there if no case matches */
  55102. duk__patch_jump(comp_ctx, pc_prevcase, pc_default);
  55103. } else {
  55104. /* default case does not exist, or no statements present
  55105. * after default case: finish case evaluation
  55106. */
  55107. duk__patch_jump_here(comp_ctx, pc_prevcase);
  55108. }
  55109. /* fall-through control flow patchup; note that pc_prevstmt may be
  55110. * < 0 (i.e. no case clauses), in which case this is a no-op.
  55111. */
  55112. duk__patch_jump_here(comp_ctx, pc_prevstmt);
  55113. /* continue jump not patched, an INVALID opcode remains there */
  55114. duk__patch_jump_here(comp_ctx, pc_label_site + 1); /* break jump */
  55115. /* Note: 'fast' breaks will jump to pc_label_site + 1, which will
  55116. * then jump here. The double jump will be eliminated by a
  55117. * peephole pass, resulting in an optimal jump here. The label
  55118. * site jumps will remain in bytecode and will waste code size.
  55119. */
  55120. return;
  55121. syntax_error:
  55122. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_SWITCH);
  55123. }
  55124. DUK_LOCAL void duk__parse_if_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  55125. duk_reg_t temp_reset;
  55126. duk_regconst_t rc_cond;
  55127. duk_int_t pc_jump_false;
  55128. DUK_DDD(DUK_DDDPRINT("begin parsing if statement"));
  55129. temp_reset = DUK__GETTEMP(comp_ctx);
  55130. duk__advance(comp_ctx); /* eat 'if' */
  55131. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  55132. rc_cond = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  55133. duk__emit_if_true_skip(comp_ctx, rc_cond);
  55134. pc_jump_false = duk__emit_jump_empty(comp_ctx); /* jump to end or else part */
  55135. DUK__SETTEMP(comp_ctx, temp_reset);
  55136. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  55137. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  55138. /* The 'else' ambiguity is resolved by 'else' binding to the innermost
  55139. * construct, so greedy matching is correct here.
  55140. */
  55141. if (comp_ctx->curr_token.t == DUK_TOK_ELSE) {
  55142. duk_int_t pc_jump_end;
  55143. DUK_DDD(DUK_DDDPRINT("if has else part"));
  55144. duk__advance(comp_ctx);
  55145. pc_jump_end = duk__emit_jump_empty(comp_ctx); /* jump from true part to end */
  55146. duk__patch_jump_here(comp_ctx, pc_jump_false);
  55147. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  55148. duk__patch_jump_here(comp_ctx, pc_jump_end);
  55149. } else {
  55150. DUK_DDD(DUK_DDDPRINT("if does not have else part"));
  55151. duk__patch_jump_here(comp_ctx, pc_jump_false);
  55152. }
  55153. DUK_DDD(DUK_DDDPRINT("end parsing if statement"));
  55154. }
  55155. DUK_LOCAL void duk__parse_do_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site) {
  55156. duk_regconst_t rc_cond;
  55157. duk_int_t pc_start;
  55158. DUK_DDD(DUK_DDDPRINT("begin parsing do statement"));
  55159. duk__advance(comp_ctx); /* eat 'do' */
  55160. pc_start = duk__get_current_pc(comp_ctx);
  55161. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  55162. duk__patch_jump_here(comp_ctx, pc_label_site + 2); /* continue jump */
  55163. duk__advance_expect(comp_ctx, DUK_TOK_WHILE);
  55164. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  55165. rc_cond = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  55166. duk__emit_if_false_skip(comp_ctx, rc_cond);
  55167. duk__emit_jump(comp_ctx, pc_start);
  55168. /* no need to reset temps, as we're finished emitting code */
  55169. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  55170. duk__patch_jump_here(comp_ctx, pc_label_site + 1); /* break jump */
  55171. DUK_DDD(DUK_DDDPRINT("end parsing do statement"));
  55172. }
  55173. DUK_LOCAL void duk__parse_while_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site) {
  55174. duk_reg_t temp_reset;
  55175. duk_regconst_t rc_cond;
  55176. duk_int_t pc_start;
  55177. duk_int_t pc_jump_false;
  55178. DUK_DDD(DUK_DDDPRINT("begin parsing while statement"));
  55179. temp_reset = DUK__GETTEMP(comp_ctx);
  55180. duk__advance(comp_ctx); /* eat 'while' */
  55181. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  55182. pc_start = duk__get_current_pc(comp_ctx);
  55183. duk__patch_jump_here(comp_ctx, pc_label_site + 2); /* continue jump */
  55184. rc_cond = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  55185. duk__emit_if_true_skip(comp_ctx, rc_cond);
  55186. pc_jump_false = duk__emit_jump_empty(comp_ctx);
  55187. DUK__SETTEMP(comp_ctx, temp_reset);
  55188. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  55189. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  55190. duk__emit_jump(comp_ctx, pc_start);
  55191. duk__patch_jump_here(comp_ctx, pc_jump_false);
  55192. duk__patch_jump_here(comp_ctx, pc_label_site + 1); /* break jump */
  55193. DUK_DDD(DUK_DDDPRINT("end parsing while statement"));
  55194. }
  55195. DUK_LOCAL void duk__parse_break_or_continue_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  55196. duk_hthread *thr = comp_ctx->thr;
  55197. duk_bool_t is_break = (comp_ctx->curr_token.t == DUK_TOK_BREAK);
  55198. duk_int_t label_id;
  55199. duk_int_t label_catch_depth;
  55200. duk_int_t label_pc; /* points to LABEL; pc+1 = jump site for break; pc+2 = jump site for continue */
  55201. duk_bool_t label_is_closest;
  55202. DUK_UNREF(res);
  55203. duk__advance(comp_ctx); /* eat 'break' or 'continue' */
  55204. if (comp_ctx->curr_token.t == DUK_TOK_SEMICOLON || /* explicit semi follows */
  55205. comp_ctx->curr_token.lineterm || /* automatic semi will be inserted */
  55206. comp_ctx->curr_token.allow_auto_semi) { /* automatic semi will be inserted */
  55207. /* break/continue without label */
  55208. duk__lookup_active_label(comp_ctx, DUK_HTHREAD_STRING_EMPTY_STRING(thr), is_break, &label_id, &label_catch_depth, &label_pc, &label_is_closest);
  55209. } else if (comp_ctx->curr_token.t == DUK_TOK_IDENTIFIER) {
  55210. /* break/continue with label (label cannot be a reserved word, production is 'Identifier' */
  55211. DUK_ASSERT(comp_ctx->curr_token.str1 != NULL);
  55212. duk__lookup_active_label(comp_ctx, comp_ctx->curr_token.str1, is_break, &label_id, &label_catch_depth, &label_pc, &label_is_closest);
  55213. duk__advance(comp_ctx);
  55214. } else {
  55215. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_BREAK_CONT_LABEL);
  55216. }
  55217. /* Use a fast break/continue when possible. A fast break/continue is
  55218. * just a jump to the LABEL break/continue jump slot, which then jumps
  55219. * to an appropriate place (for break, going through ENDLABEL correctly).
  55220. * The peephole optimizer will optimize the jump to a direct one.
  55221. */
  55222. if (label_catch_depth == comp_ctx->curr_func.catch_depth &&
  55223. label_is_closest) {
  55224. DUK_DDD(DUK_DDDPRINT("break/continue: is_break=%ld, label_id=%ld, label_is_closest=%ld, "
  55225. "label_catch_depth=%ld, catch_depth=%ld "
  55226. "-> use fast variant (direct jump)",
  55227. (long) is_break, (long) label_id, (long) label_is_closest,
  55228. (long) label_catch_depth, (long) comp_ctx->curr_func.catch_depth));
  55229. duk__emit_jump(comp_ctx, label_pc + (is_break ? 1 : 2));
  55230. } else {
  55231. DUK_DDD(DUK_DDDPRINT("break/continue: is_break=%ld, label_id=%ld, label_is_closest=%ld, "
  55232. "label_catch_depth=%ld, catch_depth=%ld "
  55233. "-> use slow variant (longjmp)",
  55234. (long) is_break, (long) label_id, (long) label_is_closest,
  55235. (long) label_catch_depth, (long) comp_ctx->curr_func.catch_depth));
  55236. duk__emit_extraop_bc(comp_ctx,
  55237. is_break ? DUK_EXTRAOP_BREAK : DUK_EXTRAOP_CONTINUE,
  55238. (duk_regconst_t) label_id);
  55239. }
  55240. }
  55241. DUK_LOCAL void duk__parse_return_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  55242. duk_hthread *thr = comp_ctx->thr;
  55243. duk_regconst_t rc_val;
  55244. duk_small_uint_t ret_flags;
  55245. duk__advance(comp_ctx); /* eat 'return' */
  55246. /* A 'return' statement is only allowed inside an actual function body,
  55247. * not as part of eval or global code.
  55248. */
  55249. if (!comp_ctx->curr_func.is_function) {
  55250. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_RETURN);
  55251. }
  55252. ret_flags = 0;
  55253. if (comp_ctx->curr_token.t == DUK_TOK_SEMICOLON || /* explicit semi follows */
  55254. comp_ctx->curr_token.lineterm || /* automatic semi will be inserted */
  55255. comp_ctx->curr_token.allow_auto_semi) { /* automatic semi will be inserted */
  55256. DUK_DDD(DUK_DDDPRINT("empty return value -> undefined"));
  55257. rc_val = 0;
  55258. } else {
  55259. duk_int_t pc_before_expr;
  55260. duk_int_t pc_after_expr;
  55261. DUK_DDD(DUK_DDDPRINT("return with a value"));
  55262. DUK_UNREF(pc_before_expr);
  55263. DUK_UNREF(pc_after_expr);
  55264. pc_before_expr = duk__get_current_pc(comp_ctx);
  55265. rc_val = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  55266. pc_after_expr = duk__get_current_pc(comp_ctx);
  55267. /* Tail call check: if last opcode emitted was CALL(I), and
  55268. * the context allows it, change the CALL(I) to a tail call.
  55269. * This doesn't guarantee that a tail call will be allowed at
  55270. * runtime, so the RETURN must still be emitted. (Duktape
  55271. * 0.10.0 avoided this and simulated a RETURN if a tail call
  55272. * couldn't be used at runtime; but this didn't work
  55273. * correctly with a thread yield/resume, see
  55274. * test-bug-tailcall-thread-yield-resume.js for discussion.)
  55275. *
  55276. * In addition to the last opcode being CALL, we also need to
  55277. * be sure that 'rc_val' is the result register of the CALL(I).
  55278. * For instance, for the expression 'return 0, (function ()
  55279. * { return 1; }), 2' the last opcode emitted is CALL (no
  55280. * bytecode is emitted for '2') but 'rc_val' indicates
  55281. * constant '2'. Similarly if '2' is replaced by a register
  55282. * bound variable, no opcodes are emitted but tail call would
  55283. * be incorrect.
  55284. *
  55285. * This is tricky and easy to get wrong. It would be best to
  55286. * track enough expression metadata to check that 'rc_val' came
  55287. * from that last CALL instruction. We don't have that metadata
  55288. * now, so we check that 'rc_val' is a temporary register result
  55289. * (not a constant or a register bound variable). There should
  55290. * be no way currently for 'rc_val' to be a temporary for an
  55291. * expression following the CALL instruction without emitting
  55292. * some opcodes following the CALL. This proxy check is used
  55293. * below.
  55294. *
  55295. * See: test-bug-comma-expr-gh131.js.
  55296. *
  55297. * The non-standard 'caller' property disables tail calls
  55298. * because they pose some special cases which haven't been
  55299. * fixed yet.
  55300. */
  55301. #if defined(DUK_USE_TAILCALL)
  55302. if (comp_ctx->curr_func.catch_depth == 0 && /* no catchers */
  55303. pc_after_expr > pc_before_expr) { /* at least one opcode emitted */
  55304. duk_compiler_instr *instr;
  55305. duk_small_uint_t op;
  55306. instr = duk__get_instr_ptr(comp_ctx, pc_after_expr - 1);
  55307. DUK_ASSERT(instr != NULL);
  55308. op = (duk_small_uint_t) DUK_DEC_OP(instr->ins);
  55309. if ((op == DUK_OP_CALL || op == DUK_OP_CALLI) &&
  55310. DUK__ISTEMP(comp_ctx, rc_val) /* see above */) {
  55311. DUK_DDD(DUK_DDDPRINT("return statement detected a tail call opportunity: "
  55312. "catch depth is 0, duk__exprtop() emitted >= 1 instructions, "
  55313. "and last instruction is a CALL "
  55314. "-> set TAILCALL flag"));
  55315. /* Just flip the single bit. */
  55316. instr->ins |= DUK_ENC_OP_A_B_C(0, DUK_BC_CALL_FLAG_TAILCALL, 0, 0);
  55317. }
  55318. }
  55319. #endif /* DUK_USE_TAILCALL */
  55320. ret_flags = DUK_BC_RETURN_FLAG_HAVE_RETVAL;
  55321. }
  55322. duk__emit_a_b(comp_ctx,
  55323. DUK_OP_RETURN | DUK__EMIT_FLAG_NO_SHUFFLE_A,
  55324. (duk_regconst_t) ret_flags /*flags*/,
  55325. rc_val /*reg*/);
  55326. }
  55327. DUK_LOCAL void duk__parse_throw_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  55328. duk_reg_t reg_val;
  55329. duk__advance(comp_ctx); /* eat 'throw' */
  55330. /* Unlike break/continue, throw statement does not allow an empty value. */
  55331. if (comp_ctx->curr_token.lineterm) {
  55332. DUK_ERROR(comp_ctx->thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_THROW);
  55333. }
  55334. reg_val = duk__exprtop_toreg(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  55335. duk__emit_extraop_bc(comp_ctx,
  55336. DUK_EXTRAOP_THROW,
  55337. (duk_regconst_t) reg_val);
  55338. }
  55339. DUK_LOCAL void duk__parse_try_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  55340. duk_hthread *thr = comp_ctx->thr;
  55341. duk_context *ctx = (duk_context *) thr;
  55342. duk_reg_t reg_catch; /* reg_catch+0 and reg_catch+1 are reserved for TRYCATCH */
  55343. duk_regconst_t rc_varname = 0;
  55344. duk_small_uint_t trycatch_flags = 0;
  55345. duk_int_t pc_ldconst = -1;
  55346. duk_int_t pc_trycatch = -1;
  55347. duk_int_t pc_catch = -1;
  55348. duk_int_t pc_finally = -1;
  55349. DUK_UNREF(res);
  55350. /*
  55351. * See the following documentation for discussion:
  55352. *
  55353. * doc/execution.rst: control flow details
  55354. *
  55355. * Try, catch, and finally "parts" are Blocks, not Statements, so
  55356. * they must always be delimited by curly braces. This is unlike e.g.
  55357. * the if statement, which accepts any Statement. This eliminates any
  55358. * questions of matching parts of nested try statements. The Block
  55359. * parsing is implemented inline here (instead of calling out).
  55360. *
  55361. * Finally part has a 'let scoped' variable, which requires a few kinks
  55362. * here.
  55363. */
  55364. comp_ctx->curr_func.catch_depth++;
  55365. duk__advance(comp_ctx); /* eat 'try' */
  55366. reg_catch = DUK__ALLOCTEMPS(comp_ctx, 2);
  55367. /* The target for this LDCONST may need output shuffling, but we assume
  55368. * that 'pc_ldconst' will be the LDCONST that we can patch later. This
  55369. * should be the case because there's no input shuffling. (If there's
  55370. * no catch clause, this LDCONST will be replaced with a NOP.)
  55371. */
  55372. pc_ldconst = duk__get_current_pc(comp_ctx);
  55373. duk__emit_a_bc(comp_ctx, DUK_OP_LDCONST, reg_catch, 0 /*patched later*/);
  55374. pc_trycatch = duk__get_current_pc(comp_ctx);
  55375. duk__emit_invalid(comp_ctx); /* TRYCATCH, cannot emit now (not enough info) */
  55376. duk__emit_invalid(comp_ctx); /* jump for 'catch' case */
  55377. duk__emit_invalid(comp_ctx); /* jump for 'finally' case or end (if no finally) */
  55378. /* try part */
  55379. duk__advance_expect(comp_ctx, DUK_TOK_LCURLY);
  55380. duk__parse_stmts(comp_ctx, 0 /*allow_source_elem*/, 0 /*expect_eof*/);
  55381. /* the DUK_TOK_RCURLY is eaten by duk__parse_stmts() */
  55382. duk__emit_extraop_only(comp_ctx,
  55383. DUK_EXTRAOP_ENDTRY);
  55384. if (comp_ctx->curr_token.t == DUK_TOK_CATCH) {
  55385. /*
  55386. * The catch variable must be updated to reflect the new allocated
  55387. * register for the duration of the catch clause. We need to store
  55388. * and restore the original value for the varmap entry (if any).
  55389. */
  55390. /*
  55391. * Note: currently register bindings must be fixed for the entire
  55392. * function. So, even though the catch variable is in a register
  55393. * we know, we must use an explicit environment record and slow path
  55394. * accesses to read/write the catch binding to make closures created
  55395. * within the catch clause work correctly. This restriction should
  55396. * be fixable (at least in common cases) later.
  55397. *
  55398. * See: test-bug-catch-binding-2.js.
  55399. *
  55400. * XXX: improve to get fast path access to most catch clauses.
  55401. */
  55402. duk_hstring *h_var;
  55403. duk_int_t varmap_value; /* for storing/restoring the varmap binding for catch variable */
  55404. DUK_DDD(DUK_DDDPRINT("stack top at start of catch clause: %ld", (long) duk_get_top(ctx)));
  55405. trycatch_flags |= DUK_BC_TRYCATCH_FLAG_HAVE_CATCH;
  55406. pc_catch = duk__get_current_pc(comp_ctx);
  55407. duk__advance(comp_ctx);
  55408. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  55409. if (comp_ctx->curr_token.t != DUK_TOK_IDENTIFIER) {
  55410. /* Identifier, i.e. don't allow reserved words */
  55411. goto syntax_error;
  55412. }
  55413. h_var = comp_ctx->curr_token.str1;
  55414. DUK_ASSERT(h_var != NULL);
  55415. duk_push_hstring(ctx, h_var); /* keep in on valstack, use borrowed ref below */
  55416. if (comp_ctx->curr_func.is_strict &&
  55417. ((h_var == DUK_HTHREAD_STRING_EVAL(thr)) ||
  55418. (h_var == DUK_HTHREAD_STRING_LC_ARGUMENTS(thr)))) {
  55419. DUK_DDD(DUK_DDDPRINT("catch identifier 'eval' or 'arguments' in strict mode -> SyntaxError"));
  55420. goto syntax_error;
  55421. }
  55422. duk_dup_top(ctx);
  55423. rc_varname = duk__getconst(comp_ctx);
  55424. DUK_DDD(DUK_DDDPRINT("catch clause, rc_varname=0x%08lx (%ld)",
  55425. (unsigned long) rc_varname, (long) rc_varname));
  55426. duk__advance(comp_ctx);
  55427. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  55428. duk__advance_expect(comp_ctx, DUK_TOK_LCURLY);
  55429. DUK_DDD(DUK_DDDPRINT("varmap before modifying for catch clause: %!iT",
  55430. (duk_tval *) duk_get_tval(ctx, comp_ctx->curr_func.varmap_idx)));
  55431. duk_dup_top(ctx);
  55432. duk_get_prop(ctx, comp_ctx->curr_func.varmap_idx);
  55433. if (duk_is_undefined(ctx, -1)) {
  55434. varmap_value = -2;
  55435. } else if (duk_is_null(ctx, -1)) {
  55436. varmap_value = -1;
  55437. } else {
  55438. DUK_ASSERT(duk_is_number(ctx, -1));
  55439. varmap_value = duk_get_int(ctx, -1);
  55440. DUK_ASSERT(varmap_value >= 0);
  55441. }
  55442. duk_pop(ctx);
  55443. #if 0
  55444. /* It'd be nice to do something like this - but it doesn't
  55445. * work for closures created inside the catch clause.
  55446. */
  55447. duk_dup_top(ctx);
  55448. duk_push_int(ctx, (duk_int_t) (reg_catch + 0));
  55449. duk_put_prop(ctx, comp_ctx->curr_func.varmap_idx);
  55450. #endif
  55451. duk_dup_top(ctx);
  55452. duk_push_null(ctx);
  55453. duk_put_prop(ctx, comp_ctx->curr_func.varmap_idx);
  55454. duk__emit_a_bc(comp_ctx,
  55455. DUK_OP_PUTVAR | DUK__EMIT_FLAG_A_IS_SOURCE,
  55456. (duk_regconst_t) (reg_catch + 0) /*value*/,
  55457. rc_varname /*varname*/);
  55458. DUK_DDD(DUK_DDDPRINT("varmap before parsing catch clause: %!iT",
  55459. (duk_tval *) duk_get_tval(ctx, comp_ctx->curr_func.varmap_idx)));
  55460. duk__parse_stmts(comp_ctx, 0 /*allow_source_elem*/, 0 /*expect_eof*/);
  55461. /* the DUK_TOK_RCURLY is eaten by duk__parse_stmts() */
  55462. if (varmap_value == -2) {
  55463. /* not present */
  55464. duk_del_prop(ctx, comp_ctx->curr_func.varmap_idx);
  55465. } else {
  55466. if (varmap_value == -1) {
  55467. duk_push_null(ctx);
  55468. } else {
  55469. DUK_ASSERT(varmap_value >= 0);
  55470. duk_push_int(ctx, varmap_value);
  55471. }
  55472. duk_put_prop(ctx, comp_ctx->curr_func.varmap_idx);
  55473. }
  55474. /* varname is popped by above code */
  55475. DUK_DDD(DUK_DDDPRINT("varmap after restore catch clause: %!iT",
  55476. (duk_tval *) duk_get_tval(ctx, comp_ctx->curr_func.varmap_idx)));
  55477. duk__emit_extraop_only(comp_ctx,
  55478. DUK_EXTRAOP_ENDCATCH);
  55479. /*
  55480. * XXX: for now, indicate that an expensive catch binding
  55481. * declarative environment is always needed. If we don't
  55482. * need it, we don't need the const_varname either.
  55483. */
  55484. trycatch_flags |= DUK_BC_TRYCATCH_FLAG_CATCH_BINDING;
  55485. DUK_DDD(DUK_DDDPRINT("stack top at end of catch clause: %ld", (long) duk_get_top(ctx)));
  55486. }
  55487. if (comp_ctx->curr_token.t == DUK_TOK_FINALLY) {
  55488. trycatch_flags |= DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY;
  55489. pc_finally = duk__get_current_pc(comp_ctx);
  55490. duk__advance(comp_ctx);
  55491. duk__advance_expect(comp_ctx, DUK_TOK_LCURLY);
  55492. duk__parse_stmts(comp_ctx, 0 /*allow_source_elem*/, 0 /*expect_eof*/);
  55493. /* the DUK_TOK_RCURLY is eaten by duk__parse_stmts() */
  55494. duk__emit_extraop_b(comp_ctx,
  55495. DUK_EXTRAOP_ENDFIN,
  55496. reg_catch); /* rethrow */
  55497. }
  55498. if (!(trycatch_flags & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH) &&
  55499. !(trycatch_flags & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY)) {
  55500. /* must have catch and/or finally */
  55501. goto syntax_error;
  55502. }
  55503. /* If there's no catch block, rc_varname will be 0 and duk__patch_trycatch()
  55504. * will replace the LDCONST with a NOP. For any actual constant (including
  55505. * constant 0) the DUK__CONST_MARKER flag will be set in rc_varname.
  55506. */
  55507. duk__patch_trycatch(comp_ctx,
  55508. pc_ldconst,
  55509. pc_trycatch,
  55510. reg_catch,
  55511. rc_varname,
  55512. trycatch_flags);
  55513. if (trycatch_flags & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH) {
  55514. DUK_ASSERT(pc_catch >= 0);
  55515. duk__patch_jump(comp_ctx, pc_trycatch + 1, pc_catch);
  55516. }
  55517. if (trycatch_flags & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY) {
  55518. DUK_ASSERT(pc_finally >= 0);
  55519. duk__patch_jump(comp_ctx, pc_trycatch + 2, pc_finally);
  55520. } else {
  55521. /* without finally, the second jump slot is used to jump to end of stmt */
  55522. duk__patch_jump_here(comp_ctx, pc_trycatch + 2);
  55523. }
  55524. comp_ctx->curr_func.catch_depth--;
  55525. return;
  55526. syntax_error:
  55527. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_TRY);
  55528. }
  55529. DUK_LOCAL void duk__parse_with_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  55530. duk_int_t pc_trycatch;
  55531. duk_int_t pc_finished;
  55532. duk_reg_t reg_catch;
  55533. duk_small_uint_t trycatch_flags;
  55534. if (comp_ctx->curr_func.is_strict) {
  55535. DUK_ERROR(comp_ctx->thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_WITH_IN_STRICT_MODE);
  55536. }
  55537. comp_ctx->curr_func.catch_depth++;
  55538. duk__advance(comp_ctx); /* eat 'with' */
  55539. reg_catch = DUK__ALLOCTEMPS(comp_ctx, 2);
  55540. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  55541. duk__exprtop_toforcedreg(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/, reg_catch);
  55542. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  55543. pc_trycatch = duk__get_current_pc(comp_ctx);
  55544. trycatch_flags = DUK_BC_TRYCATCH_FLAG_WITH_BINDING;
  55545. duk__emit_a_bc(comp_ctx,
  55546. DUK_OP_TRYCATCH | DUK__EMIT_FLAG_NO_SHUFFLE_A,
  55547. (duk_regconst_t) trycatch_flags /*a*/,
  55548. (duk_regconst_t) reg_catch /*bc*/);
  55549. duk__emit_invalid(comp_ctx); /* catch jump */
  55550. duk__emit_invalid(comp_ctx); /* finished jump */
  55551. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  55552. duk__emit_extraop_only(comp_ctx,
  55553. DUK_EXTRAOP_ENDTRY);
  55554. pc_finished = duk__get_current_pc(comp_ctx);
  55555. duk__patch_jump(comp_ctx, pc_trycatch + 2, pc_finished);
  55556. comp_ctx->curr_func.catch_depth--;
  55557. }
  55558. DUK_LOCAL duk_int_t duk__stmt_label_site(duk_compiler_ctx *comp_ctx, duk_int_t label_id) {
  55559. /* if a site already exists, nop: max one label site per statement */
  55560. if (label_id >= 0) {
  55561. return label_id;
  55562. }
  55563. label_id = comp_ctx->curr_func.label_next++;
  55564. DUK_DDD(DUK_DDDPRINT("allocated new label id for label site: %ld", (long) label_id));
  55565. duk__emit_extraop_bc(comp_ctx,
  55566. DUK_EXTRAOP_LABEL,
  55567. (duk_regconst_t) label_id);
  55568. duk__emit_invalid(comp_ctx);
  55569. duk__emit_invalid(comp_ctx);
  55570. return label_id;
  55571. }
  55572. /* Parse a single statement.
  55573. *
  55574. * Creates a label site (with an empty label) automatically for iteration
  55575. * statements. Also "peels off" any label statements for explicit labels.
  55576. */
  55577. DUK_LOCAL void duk__parse_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_bool_t allow_source_elem) {
  55578. duk_hthread *thr = comp_ctx->thr;
  55579. duk_context *ctx = (duk_context *) thr;
  55580. duk_bool_t dir_prol_at_entry; /* directive prologue status at entry */
  55581. duk_reg_t temp_at_entry;
  55582. duk_uarridx_t labels_len_at_entry;
  55583. duk_int_t pc_at_entry; /* assumed to also be PC of "LABEL" */
  55584. duk_int_t stmt_id;
  55585. duk_small_uint_t stmt_flags = 0;
  55586. duk_int_t label_id = -1;
  55587. duk_small_uint_t tok;
  55588. DUK__RECURSION_INCREASE(comp_ctx, thr);
  55589. temp_at_entry = DUK__GETTEMP(comp_ctx);
  55590. pc_at_entry = duk__get_current_pc(comp_ctx);
  55591. labels_len_at_entry = (duk_uarridx_t) duk_get_length(ctx, comp_ctx->curr_func.labelnames_idx);
  55592. stmt_id = comp_ctx->curr_func.stmt_next++;
  55593. dir_prol_at_entry = comp_ctx->curr_func.in_directive_prologue;
  55594. DUK_UNREF(stmt_id);
  55595. DUK_DDD(DUK_DDDPRINT("parsing a statement, stmt_id=%ld, temp_at_entry=%ld, labels_len_at_entry=%ld, "
  55596. "is_strict=%ld, in_directive_prologue=%ld, catch_depth=%ld",
  55597. (long) stmt_id, (long) temp_at_entry, (long) labels_len_at_entry,
  55598. (long) comp_ctx->curr_func.is_strict, (long) comp_ctx->curr_func.in_directive_prologue,
  55599. (long) comp_ctx->curr_func.catch_depth));
  55600. /* The directive prologue flag is cleared by default so that it is
  55601. * unset for any recursive statement parsing. It is only "revived"
  55602. * if a directive is detected. (We could also make directives only
  55603. * allowed if 'allow_source_elem' was true.)
  55604. */
  55605. comp_ctx->curr_func.in_directive_prologue = 0;
  55606. retry_parse:
  55607. DUK_DDD(DUK_DDDPRINT("try stmt parse, stmt_id=%ld, label_id=%ld, allow_source_elem=%ld, catch_depth=%ld",
  55608. (long) stmt_id, (long) label_id, (long) allow_source_elem,
  55609. (long) comp_ctx->curr_func.catch_depth));
  55610. /*
  55611. * Detect iteration statements; if encountered, establish an
  55612. * empty label.
  55613. */
  55614. tok = comp_ctx->curr_token.t;
  55615. if (tok == DUK_TOK_FOR || tok == DUK_TOK_DO || tok == DUK_TOK_WHILE ||
  55616. tok == DUK_TOK_SWITCH) {
  55617. DUK_DDD(DUK_DDDPRINT("iteration/switch statement -> add empty label"));
  55618. label_id = duk__stmt_label_site(comp_ctx, label_id);
  55619. duk__add_label(comp_ctx,
  55620. DUK_HTHREAD_STRING_EMPTY_STRING(thr),
  55621. pc_at_entry /*pc_label*/,
  55622. label_id);
  55623. }
  55624. /*
  55625. * Main switch for statement / source element type.
  55626. */
  55627. switch (comp_ctx->curr_token.t) {
  55628. case DUK_TOK_FUNCTION: {
  55629. /*
  55630. * Function declaration, function expression, or (non-standard)
  55631. * function statement.
  55632. *
  55633. * The E5 specification only allows function declarations at
  55634. * the top level (in "source elements"). An ExpressionStatement
  55635. * is explicitly not allowed to begin with a "function" keyword
  55636. * (E5 Section 12.4). Hence any non-error semantics for such
  55637. * non-top-level statements are non-standard. Duktape semantics
  55638. * for function statements are modelled after V8, see
  55639. * test-dev-func-decl-outside-top.js.
  55640. */
  55641. #if defined(DUK_USE_NONSTD_FUNC_STMT)
  55642. /* Lenient: allow function declarations outside top level in
  55643. * non-strict mode but reject them in strict mode.
  55644. */
  55645. if (allow_source_elem || !comp_ctx->curr_func.is_strict)
  55646. #else /* DUK_USE_NONSTD_FUNC_STMT */
  55647. /* Strict: never allow function declarations outside top level. */
  55648. if (allow_source_elem)
  55649. #endif /* DUK_USE_NONSTD_FUNC_STMT */
  55650. {
  55651. /* FunctionDeclaration: not strictly a statement but handled as such.
  55652. *
  55653. * O(depth^2) parse count for inner functions is handled by recording a
  55654. * lexer offset on the first compilation pass, so that the function can
  55655. * be efficiently skipped on the second pass. This is encapsulated into
  55656. * duk__parse_func_like_fnum().
  55657. */
  55658. duk_int_t fnum;
  55659. DUK_DDD(DUK_DDDPRINT("function declaration statement"));
  55660. duk__advance(comp_ctx); /* eat 'function' */
  55661. fnum = duk__parse_func_like_fnum(comp_ctx, 1 /*is_decl*/, 0 /*is_setget*/);
  55662. if (comp_ctx->curr_func.in_scanning) {
  55663. duk_uarridx_t n;
  55664. duk_hstring *h_funcname;
  55665. duk_get_prop_index(ctx, comp_ctx->curr_func.funcs_idx, fnum * 3);
  55666. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_NAME); /* -> [ ... func name ] */
  55667. h_funcname = duk_get_hstring(ctx, -1);
  55668. DUK_ASSERT(h_funcname != NULL);
  55669. DUK_DDD(DUK_DDDPRINT("register function declaration %!O in pass 1, fnum %ld",
  55670. (duk_heaphdr *) h_funcname, (long) fnum));
  55671. n = (duk_uarridx_t) duk_get_length(ctx, comp_ctx->curr_func.decls_idx);
  55672. duk_push_hstring(ctx, h_funcname);
  55673. duk_put_prop_index(ctx, comp_ctx->curr_func.decls_idx, n);
  55674. duk_push_int(ctx, (duk_int_t) (DUK_DECL_TYPE_FUNC + (fnum << 8)));
  55675. duk_put_prop_index(ctx, comp_ctx->curr_func.decls_idx, n + 1);
  55676. duk_pop_n(ctx, 2);
  55677. }
  55678. /* no statement value (unlike function expression) */
  55679. stmt_flags = 0;
  55680. break;
  55681. } else {
  55682. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_FUNC_STMT_NOT_ALLOWED);
  55683. }
  55684. break;
  55685. }
  55686. case DUK_TOK_LCURLY: {
  55687. DUK_DDD(DUK_DDDPRINT("block statement"));
  55688. duk__advance(comp_ctx);
  55689. duk__parse_stmts(comp_ctx, 0 /*allow_source_elem*/, 0 /*expect_eof*/);
  55690. /* the DUK_TOK_RCURLY is eaten by duk__parse_stmts() */
  55691. if (label_id >= 0) {
  55692. duk__patch_jump_here(comp_ctx, pc_at_entry + 1); /* break jump */
  55693. }
  55694. stmt_flags = 0;
  55695. break;
  55696. }
  55697. case DUK_TOK_CONST: {
  55698. DUK_DDD(DUK_DDDPRINT("constant declaration statement"));
  55699. duk__parse_var_stmt(comp_ctx, res, DUK__EXPR_FLAG_REQUIRE_INIT /*expr_flags*/);
  55700. stmt_flags = DUK__HAS_TERM;
  55701. break;
  55702. }
  55703. case DUK_TOK_VAR: {
  55704. DUK_DDD(DUK_DDDPRINT("variable declaration statement"));
  55705. duk__parse_var_stmt(comp_ctx, res, 0 /*expr_flags*/);
  55706. stmt_flags = DUK__HAS_TERM;
  55707. break;
  55708. }
  55709. case DUK_TOK_SEMICOLON: {
  55710. /* empty statement with an explicit semicolon */
  55711. DUK_DDD(DUK_DDDPRINT("empty statement"));
  55712. stmt_flags = DUK__HAS_TERM;
  55713. break;
  55714. }
  55715. case DUK_TOK_IF: {
  55716. DUK_DDD(DUK_DDDPRINT("if statement"));
  55717. duk__parse_if_stmt(comp_ctx, res);
  55718. if (label_id >= 0) {
  55719. duk__patch_jump_here(comp_ctx, pc_at_entry + 1); /* break jump */
  55720. }
  55721. stmt_flags = 0;
  55722. break;
  55723. }
  55724. case DUK_TOK_DO: {
  55725. /*
  55726. * Do-while statement is mostly trivial, but there is special
  55727. * handling for automatic semicolon handling (triggered by the
  55728. * DUK__ALLOW_AUTO_SEMI_ALWAYS) flag related to a bug filed at:
  55729. *
  55730. * https://bugs.ecmascript.org/show_bug.cgi?id=8
  55731. *
  55732. * See doc/compiler.rst for details.
  55733. */
  55734. DUK_DDD(DUK_DDDPRINT("do statement"));
  55735. DUK_ASSERT(label_id >= 0);
  55736. duk__update_label_flags(comp_ctx,
  55737. label_id,
  55738. DUK_LABEL_FLAG_ALLOW_BREAK | DUK_LABEL_FLAG_ALLOW_CONTINUE);
  55739. duk__parse_do_stmt(comp_ctx, res, pc_at_entry);
  55740. stmt_flags = DUK__HAS_TERM | DUK__ALLOW_AUTO_SEMI_ALWAYS; /* DUK__ALLOW_AUTO_SEMI_ALWAYS workaround */
  55741. break;
  55742. }
  55743. case DUK_TOK_WHILE: {
  55744. DUK_DDD(DUK_DDDPRINT("while statement"));
  55745. DUK_ASSERT(label_id >= 0);
  55746. duk__update_label_flags(comp_ctx,
  55747. label_id,
  55748. DUK_LABEL_FLAG_ALLOW_BREAK | DUK_LABEL_FLAG_ALLOW_CONTINUE);
  55749. duk__parse_while_stmt(comp_ctx, res, pc_at_entry);
  55750. stmt_flags = 0;
  55751. break;
  55752. }
  55753. case DUK_TOK_FOR: {
  55754. /*
  55755. * For/for-in statement is complicated to parse because
  55756. * determining the statement type (three-part for vs. a
  55757. * for-in) requires potential backtracking.
  55758. *
  55759. * See the helper for the messy stuff.
  55760. */
  55761. DUK_DDD(DUK_DDDPRINT("for/for-in statement"));
  55762. DUK_ASSERT(label_id >= 0);
  55763. duk__update_label_flags(comp_ctx,
  55764. label_id,
  55765. DUK_LABEL_FLAG_ALLOW_BREAK | DUK_LABEL_FLAG_ALLOW_CONTINUE);
  55766. duk__parse_for_stmt(comp_ctx, res, pc_at_entry);
  55767. stmt_flags = 0;
  55768. break;
  55769. }
  55770. case DUK_TOK_CONTINUE:
  55771. case DUK_TOK_BREAK: {
  55772. DUK_DDD(DUK_DDDPRINT("break/continue statement"));
  55773. duk__parse_break_or_continue_stmt(comp_ctx, res);
  55774. stmt_flags = DUK__HAS_TERM | DUK__IS_TERMINAL;
  55775. break;
  55776. }
  55777. case DUK_TOK_RETURN: {
  55778. DUK_DDD(DUK_DDDPRINT("return statement"));
  55779. duk__parse_return_stmt(comp_ctx, res);
  55780. stmt_flags = DUK__HAS_TERM | DUK__IS_TERMINAL;
  55781. break;
  55782. }
  55783. case DUK_TOK_WITH: {
  55784. DUK_DDD(DUK_DDDPRINT("with statement"));
  55785. comp_ctx->curr_func.with_depth++;
  55786. duk__parse_with_stmt(comp_ctx, res);
  55787. if (label_id >= 0) {
  55788. duk__patch_jump_here(comp_ctx, pc_at_entry + 1); /* break jump */
  55789. }
  55790. comp_ctx->curr_func.with_depth--;
  55791. stmt_flags = 0;
  55792. break;
  55793. }
  55794. case DUK_TOK_SWITCH: {
  55795. /*
  55796. * The switch statement is pretty messy to compile.
  55797. * See the helper for details.
  55798. */
  55799. DUK_DDD(DUK_DDDPRINT("switch statement"));
  55800. DUK_ASSERT(label_id >= 0);
  55801. duk__update_label_flags(comp_ctx,
  55802. label_id,
  55803. DUK_LABEL_FLAG_ALLOW_BREAK); /* don't allow continue */
  55804. duk__parse_switch_stmt(comp_ctx, res, pc_at_entry);
  55805. stmt_flags = 0;
  55806. break;
  55807. }
  55808. case DUK_TOK_THROW: {
  55809. DUK_DDD(DUK_DDDPRINT("throw statement"));
  55810. duk__parse_throw_stmt(comp_ctx, res);
  55811. stmt_flags = DUK__HAS_TERM | DUK__IS_TERMINAL;
  55812. break;
  55813. }
  55814. case DUK_TOK_TRY: {
  55815. DUK_DDD(DUK_DDDPRINT("try statement"));
  55816. duk__parse_try_stmt(comp_ctx, res);
  55817. stmt_flags = 0;
  55818. break;
  55819. }
  55820. case DUK_TOK_DEBUGGER: {
  55821. duk__advance(comp_ctx);
  55822. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  55823. DUK_DDD(DUK_DDDPRINT("debugger statement: debugging enabled, emit debugger opcode"));
  55824. duk__emit_extraop_only(comp_ctx, DUK_EXTRAOP_DEBUGGER);
  55825. #else
  55826. DUK_DDD(DUK_DDDPRINT("debugger statement: ignored"));
  55827. #endif
  55828. stmt_flags = DUK__HAS_TERM;
  55829. break;
  55830. }
  55831. default: {
  55832. /*
  55833. * Else, must be one of:
  55834. * - ExpressionStatement, possibly a directive (String)
  55835. * - LabelledStatement (Identifier followed by ':')
  55836. *
  55837. * Expressions beginning with 'function' keyword are covered by a case
  55838. * above (such expressions are not allowed in standard E5 anyway).
  55839. * Also expressions starting with '{' are interpreted as block
  55840. * statements. See E5 Section 12.4.
  55841. *
  55842. * Directive detection is tricky; see E5 Section 14.1 on directive
  55843. * prologue. A directive is an expression statement with a single
  55844. * string literal and an explicit or automatic semicolon. Escape
  55845. * characters are significant and no parens etc are allowed:
  55846. *
  55847. * 'use strict'; // valid 'use strict' directive
  55848. * 'use\u0020strict'; // valid directive, not a 'use strict' directive
  55849. * ('use strict'); // not a valid directive
  55850. *
  55851. * The expression is determined to consist of a single string literal
  55852. * based on duk__expr_nud() and duk__expr_led() call counts. The string literal
  55853. * of a 'use strict' directive is determined to lack any escapes based
  55854. * num_escapes count from the lexer. Note that other directives may be
  55855. * allowed to contain escapes, so a directive with escapes does not
  55856. * terminate a directive prologue.
  55857. *
  55858. * We rely on the fact that the expression parser will not emit any
  55859. * code for a single token expression. However, it will generate an
  55860. * intermediate value which we will then successfully ignore.
  55861. *
  55862. * A similar approach is used for labels.
  55863. */
  55864. duk_bool_t single_token;
  55865. DUK_DDD(DUK_DDDPRINT("expression statement"));
  55866. duk__exprtop(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  55867. single_token = (comp_ctx->curr_func.nud_count == 1 && /* one token */
  55868. comp_ctx->curr_func.led_count == 0); /* no operators */
  55869. if (single_token &&
  55870. comp_ctx->prev_token.t == DUK_TOK_IDENTIFIER &&
  55871. comp_ctx->curr_token.t == DUK_TOK_COLON) {
  55872. /*
  55873. * Detected label
  55874. */
  55875. duk_hstring *h_lab;
  55876. /* expected ival */
  55877. DUK_ASSERT(res->t == DUK_IVAL_VAR);
  55878. DUK_ASSERT(res->x1.t == DUK_ISPEC_VALUE);
  55879. DUK_ASSERT(DUK_TVAL_IS_STRING(duk_get_tval(ctx, res->x1.valstack_idx)));
  55880. h_lab = comp_ctx->prev_token.str1;
  55881. DUK_ASSERT(h_lab != NULL);
  55882. DUK_DDD(DUK_DDDPRINT("explicit label site for label '%!O'",
  55883. (duk_heaphdr *) h_lab));
  55884. duk__advance(comp_ctx); /* eat colon */
  55885. label_id = duk__stmt_label_site(comp_ctx, label_id);
  55886. duk__add_label(comp_ctx,
  55887. h_lab,
  55888. pc_at_entry /*pc_label*/,
  55889. label_id);
  55890. /* a statement following a label cannot be a source element
  55891. * (a function declaration).
  55892. */
  55893. allow_source_elem = 0;
  55894. DUK_DDD(DUK_DDDPRINT("label handled, retry statement parsing"));
  55895. goto retry_parse;
  55896. }
  55897. stmt_flags = 0;
  55898. if (dir_prol_at_entry && /* still in prologue */
  55899. single_token && /* single string token */
  55900. comp_ctx->prev_token.t == DUK_TOK_STRING) {
  55901. /*
  55902. * Detected a directive
  55903. */
  55904. duk_hstring *h_dir;
  55905. /* expected ival */
  55906. DUK_ASSERT(res->t == DUK_IVAL_PLAIN);
  55907. DUK_ASSERT(res->x1.t == DUK_ISPEC_VALUE);
  55908. DUK_ASSERT(DUK_TVAL_IS_STRING(duk_get_tval(ctx, res->x1.valstack_idx)));
  55909. h_dir = comp_ctx->prev_token.str1;
  55910. DUK_ASSERT(h_dir != NULL);
  55911. DUK_DDD(DUK_DDDPRINT("potential directive: %!O", h_dir));
  55912. stmt_flags |= DUK__STILL_PROLOGUE;
  55913. /* Note: escaped characters differentiate directives */
  55914. if (comp_ctx->prev_token.num_escapes > 0) {
  55915. DUK_DDD(DUK_DDDPRINT("directive contains escapes: valid directive "
  55916. "but we ignore such directives"));
  55917. } else {
  55918. /*
  55919. * The length comparisons are present to handle
  55920. * strings like "use strict\u0000foo" as required.
  55921. */
  55922. if (DUK_HSTRING_GET_BYTELEN(h_dir) == 10 &&
  55923. DUK_STRNCMP((const char *) DUK_HSTRING_GET_DATA(h_dir), "use strict", 10) == 0) {
  55924. #if defined(DUK_USE_STRICT_DECL)
  55925. DUK_DDD(DUK_DDDPRINT("use strict directive detected: strict flag %ld -> %ld",
  55926. (long) comp_ctx->curr_func.is_strict, (long) 1));
  55927. comp_ctx->curr_func.is_strict = 1;
  55928. #else
  55929. DUK_DDD(DUK_DDDPRINT("use strict detected but strict declarations disabled, ignoring"));
  55930. #endif
  55931. } else if (DUK_HSTRING_GET_BYTELEN(h_dir) == 14 &&
  55932. DUK_STRNCMP((const char *) DUK_HSTRING_GET_DATA(h_dir), "use duk notail", 14) == 0) {
  55933. DUK_DDD(DUK_DDDPRINT("use duk notail directive detected: notail flag %ld -> %ld",
  55934. (long) comp_ctx->curr_func.is_notail, (long) 1));
  55935. comp_ctx->curr_func.is_notail = 1;
  55936. } else {
  55937. DUK_DD(DUK_DDPRINT("unknown directive: '%!O', ignoring but not terminating "
  55938. "directive prologue", (duk_hobject *) h_dir));
  55939. }
  55940. }
  55941. } else {
  55942. DUK_DDD(DUK_DDDPRINT("non-directive expression statement or no longer in prologue; "
  55943. "prologue terminated if still active"));
  55944. }
  55945. stmt_flags |= DUK__HAS_VAL | DUK__HAS_TERM;
  55946. }
  55947. } /* end switch (tok) */
  55948. /*
  55949. * Statement value handling.
  55950. *
  55951. * Global code and eval code has an implicit return value
  55952. * which comes from the last statement with a value
  55953. * (technically a non-"empty" continuation, which is
  55954. * different from an empty statement).
  55955. *
  55956. * Since we don't know whether a later statement will
  55957. * override the value of the current statement, we need
  55958. * to coerce the statement value to a register allocated
  55959. * for implicit return values. In other cases we need
  55960. * to coerce the statement value to a plain value to get
  55961. * any side effects out (consider e.g. "foo.bar;").
  55962. */
  55963. /* XXX: what about statements which leave a half-cooked value in 'res'
  55964. * but have no stmt value? Any such statements?
  55965. */
  55966. if (stmt_flags & DUK__HAS_VAL) {
  55967. duk_reg_t reg_stmt_value = comp_ctx->curr_func.reg_stmt_value;
  55968. if (reg_stmt_value >= 0) {
  55969. duk__ivalue_toforcedreg(comp_ctx, res, reg_stmt_value);
  55970. } else {
  55971. duk__ivalue_toplain_ignore(comp_ctx, res);
  55972. }
  55973. } else {
  55974. ;
  55975. }
  55976. /*
  55977. * Statement terminator check, including automatic semicolon
  55978. * handling. After this step, 'curr_tok' should be the first
  55979. * token after a possible statement terminator.
  55980. */
  55981. if (stmt_flags & DUK__HAS_TERM) {
  55982. if (comp_ctx->curr_token.t == DUK_TOK_SEMICOLON) {
  55983. DUK_DDD(DUK_DDDPRINT("explicit semicolon terminates statement"));
  55984. duk__advance(comp_ctx);
  55985. } else {
  55986. if (comp_ctx->curr_token.allow_auto_semi) {
  55987. DUK_DDD(DUK_DDDPRINT("automatic semicolon terminates statement"));
  55988. } else if (stmt_flags & DUK__ALLOW_AUTO_SEMI_ALWAYS) {
  55989. /* XXX: make this lenience dependent on flags or strictness? */
  55990. DUK_DDD(DUK_DDDPRINT("automatic semicolon terminates statement (allowed for compatibility "
  55991. "even though no lineterm present before next token)"));
  55992. } else {
  55993. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_UNTERMINATED_STMT);
  55994. }
  55995. }
  55996. } else {
  55997. DUK_DDD(DUK_DDDPRINT("statement has no terminator"));
  55998. }
  55999. /*
  56000. * Directive prologue tracking.
  56001. */
  56002. if (stmt_flags & DUK__STILL_PROLOGUE) {
  56003. DUK_DDD(DUK_DDDPRINT("setting in_directive_prologue"));
  56004. comp_ctx->curr_func.in_directive_prologue = 1;
  56005. }
  56006. /*
  56007. * Cleanups (all statement parsing flows through here).
  56008. *
  56009. * Pop label site and reset labels. Reset 'next temp' to value at
  56010. * entry to reuse temps.
  56011. */
  56012. if (label_id >= 0) {
  56013. duk__emit_extraop_bc(comp_ctx,
  56014. DUK_EXTRAOP_ENDLABEL,
  56015. (duk_regconst_t) label_id);
  56016. }
  56017. DUK__SETTEMP(comp_ctx, temp_at_entry);
  56018. duk__reset_labels_to_length(comp_ctx, labels_len_at_entry);
  56019. /* XXX: return indication of "terminalness" (e.g. a 'throw' is terminal) */
  56020. DUK__RECURSION_DECREASE(comp_ctx, thr);
  56021. }
  56022. #undef DUK__HAS_VAL
  56023. #undef DUK__HAS_TERM
  56024. #undef DUK__ALLOW_AUTO_SEMI_ALWAYS
  56025. /*
  56026. * Parse a statement list.
  56027. *
  56028. * Handles automatic semicolon insertion and implicit return value.
  56029. *
  56030. * Upon entry, 'curr_tok' should contain the first token of the first
  56031. * statement (parsed in the "allow regexp literal" mode). Upon exit,
  56032. * 'curr_tok' contains the token following the statement list terminator
  56033. * (EOF or closing brace).
  56034. */
  56035. DUK_LOCAL void duk__parse_stmts(duk_compiler_ctx *comp_ctx, duk_bool_t allow_source_elem, duk_bool_t expect_eof) {
  56036. duk_hthread *thr = comp_ctx->thr;
  56037. duk_context *ctx = (duk_context *) thr;
  56038. duk_ivalue res_alloc;
  56039. duk_ivalue *res = &res_alloc;
  56040. /* Setup state. Initial ivalue is 'undefined'. */
  56041. duk_require_stack(ctx, DUK__PARSE_STATEMENTS_SLOTS);
  56042. /* XXX: 'res' setup can be moved to function body level; in fact, two 'res'
  56043. * intermediate values suffice for parsing of each function. Nesting is needed
  56044. * for nested functions (which may occur inside expressions).
  56045. */
  56046. DUK_MEMZERO(&res_alloc, sizeof(res_alloc));
  56047. res->t = DUK_IVAL_PLAIN;
  56048. res->x1.t = DUK_ISPEC_VALUE;
  56049. res->x1.valstack_idx = duk_get_top(ctx);
  56050. res->x2.valstack_idx = res->x1.valstack_idx + 1;
  56051. duk_push_undefined(ctx);
  56052. duk_push_undefined(ctx);
  56053. /* Parse statements until a closing token (EOF or '}') is found. */
  56054. for (;;) {
  56055. /* Check whether statement list ends. */
  56056. if (expect_eof) {
  56057. if (comp_ctx->curr_token.t == DUK_TOK_EOF) {
  56058. break;
  56059. }
  56060. } else {
  56061. if (comp_ctx->curr_token.t == DUK_TOK_RCURLY) {
  56062. break;
  56063. }
  56064. }
  56065. /* Check statement type based on the first token type.
  56066. *
  56067. * Note: expression parsing helpers expect 'curr_tok' to
  56068. * contain the first token of the expression upon entry.
  56069. */
  56070. DUK_DDD(DUK_DDDPRINT("TOKEN %ld (non-whitespace, non-comment)", (long) comp_ctx->curr_token.t));
  56071. duk__parse_stmt(comp_ctx, res, allow_source_elem);
  56072. }
  56073. duk__advance(comp_ctx);
  56074. /* Tear down state. */
  56075. duk_pop_2(ctx);
  56076. }
  56077. /*
  56078. * Declaration binding instantiation conceptually happens when calling a
  56079. * function; for us it essentially means that function prologue. The
  56080. * conceptual process is described in E5 Section 10.5.
  56081. *
  56082. * We need to keep track of all encountered identifiers to (1) create an
  56083. * identifier-to-register map ("varmap"); and (2) detect duplicate
  56084. * declarations. Identifiers which are not bound to registers still need
  56085. * to be tracked for detecting duplicates. Currently such identifiers
  56086. * are put into the varmap with a 'null' value, which is later cleaned up.
  56087. *
  56088. * To support functions with a large number of variable and function
  56089. * declarations, registers are not allocated beyond a certain limit;
  56090. * after that limit, variables and functions need slow path access.
  56091. * Arguments are currently always register bound, which imposes a hard
  56092. * (and relatively small) argument count limit.
  56093. *
  56094. * Some bindings in E5 are not configurable (= deletable) and almost all
  56095. * are mutable (writable). Exceptions are:
  56096. *
  56097. * - The 'arguments' binding, established only if no shadowing argument
  56098. * or function declaration exists. We handle 'arguments' creation
  56099. * and binding through an explicit slow path environment record.
  56100. *
  56101. * - The "name" binding for a named function expression. This is also
  56102. * handled through an explicit slow path environment record.
  56103. */
  56104. /* XXX: add support for variables to not be register bound always, to
  56105. * handle cases with a very large number of variables?
  56106. */
  56107. DUK_LOCAL void duk__init_varmap_and_prologue_for_pass2(duk_compiler_ctx *comp_ctx, duk_reg_t *out_stmt_value_reg) {
  56108. duk_hthread *thr = comp_ctx->thr;
  56109. duk_context *ctx = (duk_context *) thr;
  56110. duk_hstring *h_name;
  56111. duk_bool_t configurable_bindings;
  56112. duk_uarridx_t num_args;
  56113. duk_uarridx_t num_decls;
  56114. duk_regconst_t rc_name;
  56115. duk_small_uint_t declvar_flags;
  56116. duk_uarridx_t i;
  56117. #ifdef DUK_USE_ASSERTIONS
  56118. duk_idx_t entry_top;
  56119. #endif
  56120. #ifdef DUK_USE_ASSERTIONS
  56121. entry_top = duk_get_top(ctx);
  56122. #endif
  56123. /*
  56124. * Preliminaries
  56125. */
  56126. configurable_bindings = comp_ctx->curr_func.is_eval;
  56127. DUK_DDD(DUK_DDDPRINT("configurable_bindings=%ld", (long) configurable_bindings));
  56128. /* varmap is already in comp_ctx->curr_func.varmap_idx */
  56129. /*
  56130. * Function formal arguments, always bound to registers
  56131. * (there's no support for shuffling them now).
  56132. */
  56133. num_args = (duk_uarridx_t) duk_get_length(ctx, comp_ctx->curr_func.argnames_idx);
  56134. DUK_DDD(DUK_DDDPRINT("num_args=%ld", (long) num_args));
  56135. /* XXX: check num_args */
  56136. for (i = 0; i < num_args; i++) {
  56137. duk_get_prop_index(ctx, comp_ctx->curr_func.argnames_idx, i);
  56138. h_name = duk_get_hstring(ctx, -1);
  56139. DUK_ASSERT(h_name != NULL);
  56140. if (comp_ctx->curr_func.is_strict) {
  56141. if (duk__hstring_is_eval_or_arguments(comp_ctx, h_name)) {
  56142. DUK_DDD(DUK_DDDPRINT("arg named 'eval' or 'arguments' in strict mode -> SyntaxError"));
  56143. goto error_argname;
  56144. }
  56145. duk_dup_top(ctx);
  56146. if (duk_has_prop(ctx, comp_ctx->curr_func.varmap_idx)) {
  56147. DUK_DDD(DUK_DDDPRINT("duplicate arg name in strict mode -> SyntaxError"));
  56148. goto error_argname;
  56149. }
  56150. /* Ensure argument name is not a reserved word in current
  56151. * (final) strictness. Formal argument parsing may not
  56152. * catch reserved names if strictness changes during
  56153. * parsing.
  56154. *
  56155. * We only need to do this in strict mode because non-strict
  56156. * keyword are always detected in formal argument parsing.
  56157. */
  56158. if (DUK_HSTRING_HAS_STRICT_RESERVED_WORD(h_name)) {
  56159. goto error_argname;
  56160. }
  56161. }
  56162. /* overwrite any previous binding of the same name; the effect is
  56163. * that last argument of a certain name wins.
  56164. */
  56165. /* only functions can have arguments */
  56166. DUK_ASSERT(comp_ctx->curr_func.is_function);
  56167. duk_push_uarridx(ctx, i); /* -> [ ... name index ] */
  56168. duk_put_prop(ctx, comp_ctx->curr_func.varmap_idx); /* -> [ ... ] */
  56169. /* no code needs to be emitted, the regs already have values */
  56170. }
  56171. /* use temp_next for tracking register allocations */
  56172. DUK__SETTEMP_CHECKMAX(comp_ctx, (duk_reg_t) num_args);
  56173. /*
  56174. * After arguments, allocate special registers (like shuffling temps)
  56175. */
  56176. if (out_stmt_value_reg) {
  56177. *out_stmt_value_reg = DUK__ALLOCTEMP(comp_ctx);
  56178. }
  56179. if (comp_ctx->curr_func.needs_shuffle) {
  56180. duk_reg_t shuffle_base = DUK__ALLOCTEMPS(comp_ctx, 3);
  56181. comp_ctx->curr_func.shuffle1 = shuffle_base;
  56182. comp_ctx->curr_func.shuffle2 = shuffle_base + 1;
  56183. comp_ctx->curr_func.shuffle3 = shuffle_base + 2;
  56184. DUK_D(DUK_DPRINT("shuffle registers needed by function, allocated: %ld %ld %ld",
  56185. (long) comp_ctx->curr_func.shuffle1,
  56186. (long) comp_ctx->curr_func.shuffle2,
  56187. (long) comp_ctx->curr_func.shuffle3));
  56188. }
  56189. if (comp_ctx->curr_func.temp_next > 0x100) {
  56190. DUK_D(DUK_DPRINT("not enough 8-bit regs: temp_next=%ld", (long) comp_ctx->curr_func.temp_next));
  56191. goto error_outofregs;
  56192. }
  56193. /*
  56194. * Function declarations
  56195. */
  56196. num_decls = (duk_uarridx_t) duk_get_length(ctx, comp_ctx->curr_func.decls_idx);
  56197. DUK_DDD(DUK_DDDPRINT("num_decls=%ld -> %!T",
  56198. (long) num_decls,
  56199. (duk_tval *) duk_get_tval(ctx, comp_ctx->curr_func.decls_idx)));
  56200. for (i = 0; i < num_decls; i += 2) {
  56201. duk_int_t decl_type;
  56202. duk_int_t fnum;
  56203. duk_get_prop_index(ctx, comp_ctx->curr_func.decls_idx, i + 1); /* decl type */
  56204. decl_type = duk_to_int(ctx, -1);
  56205. fnum = decl_type >> 8; /* XXX: macros */
  56206. decl_type = decl_type & 0xff;
  56207. duk_pop(ctx);
  56208. if (decl_type != DUK_DECL_TYPE_FUNC) {
  56209. continue;
  56210. }
  56211. duk_get_prop_index(ctx, comp_ctx->curr_func.decls_idx, i); /* decl name */
  56212. /* XXX: spilling */
  56213. if (comp_ctx->curr_func.is_function) {
  56214. duk_reg_t reg_bind;
  56215. duk_dup_top(ctx);
  56216. if (duk_has_prop(ctx, comp_ctx->curr_func.varmap_idx)) {
  56217. /* shadowed; update value */
  56218. duk_dup_top(ctx);
  56219. duk_get_prop(ctx, comp_ctx->curr_func.varmap_idx);
  56220. reg_bind = duk_to_int(ctx, -1); /* [ ... name reg_bind ] */
  56221. duk__emit_a_bc(comp_ctx,
  56222. DUK_OP_CLOSURE,
  56223. (duk_regconst_t) reg_bind,
  56224. (duk_regconst_t) fnum);
  56225. } else {
  56226. /* function: always register bound */
  56227. reg_bind = DUK__ALLOCTEMP(comp_ctx);
  56228. duk__emit_a_bc(comp_ctx,
  56229. DUK_OP_CLOSURE,
  56230. (duk_regconst_t) reg_bind,
  56231. (duk_regconst_t) fnum);
  56232. duk_push_int(ctx, (duk_int_t) reg_bind);
  56233. }
  56234. } else {
  56235. /* Function declaration for global/eval code is emitted even
  56236. * for duplicates, because of E5 Section 10.5, step 5.e of
  56237. * E5.1 (special behavior for variable bound to global object).
  56238. *
  56239. * DECLVAR will not re-declare a variable as such, but will
  56240. * update the binding value.
  56241. */
  56242. duk_reg_t reg_temp = DUK__ALLOCTEMP(comp_ctx);
  56243. duk_dup_top(ctx);
  56244. rc_name = duk__getconst(comp_ctx);
  56245. duk_push_null(ctx);
  56246. duk__emit_a_bc(comp_ctx,
  56247. DUK_OP_CLOSURE,
  56248. (duk_regconst_t) reg_temp,
  56249. (duk_regconst_t) fnum);
  56250. declvar_flags = DUK_PROPDESC_FLAG_WRITABLE |
  56251. DUK_PROPDESC_FLAG_ENUMERABLE |
  56252. DUK_BC_DECLVAR_FLAG_FUNC_DECL;
  56253. if (configurable_bindings) {
  56254. declvar_flags |= DUK_PROPDESC_FLAG_CONFIGURABLE;
  56255. }
  56256. duk__emit_a_b_c(comp_ctx,
  56257. DUK_OP_DECLVAR | DUK__EMIT_FLAG_NO_SHUFFLE_A,
  56258. (duk_regconst_t) declvar_flags /*flags*/,
  56259. rc_name /*name*/,
  56260. (duk_regconst_t) reg_temp /*value*/);
  56261. DUK__SETTEMP(comp_ctx, reg_temp); /* forget temp */
  56262. }
  56263. DUK_DDD(DUK_DDDPRINT("function declaration to varmap: %!T -> %!T",
  56264. (duk_tval *) duk_get_tval(ctx, -2),
  56265. (duk_tval *) duk_get_tval(ctx, -1)));
  56266. duk_put_prop(ctx, comp_ctx->curr_func.varmap_idx); /* [ ... name reg/null ] -> [ ... ] */
  56267. }
  56268. /*
  56269. * 'arguments' binding is special; if a shadowing argument or
  56270. * function declaration exists, an arguments object will
  56271. * definitely not be needed, regardless of whether the identifier
  56272. * 'arguments' is referenced inside the function body.
  56273. */
  56274. if (duk_has_prop_stridx(ctx, comp_ctx->curr_func.varmap_idx, DUK_STRIDX_LC_ARGUMENTS)) {
  56275. DUK_DDD(DUK_DDDPRINT("'arguments' is shadowed by argument or function declaration "
  56276. "-> arguments object creation can be skipped"));
  56277. comp_ctx->curr_func.is_arguments_shadowed = 1;
  56278. }
  56279. /*
  56280. * Variable declarations.
  56281. *
  56282. * Unlike function declarations, variable declaration values don't get
  56283. * assigned on entry. If a binding of the same name already exists, just
  56284. * ignore it silently.
  56285. */
  56286. for (i = 0; i < num_decls; i += 2) {
  56287. duk_int_t decl_type;
  56288. duk_get_prop_index(ctx, comp_ctx->curr_func.decls_idx, i + 1); /* decl type */
  56289. decl_type = duk_to_int(ctx, -1);
  56290. decl_type = decl_type & 0xff;
  56291. duk_pop(ctx);
  56292. if (decl_type != DUK_DECL_TYPE_VAR) {
  56293. continue;
  56294. }
  56295. duk_get_prop_index(ctx, comp_ctx->curr_func.decls_idx, i); /* decl name */
  56296. if (duk_has_prop(ctx, comp_ctx->curr_func.varmap_idx)) {
  56297. /* shadowed, ignore */
  56298. } else {
  56299. duk_get_prop_index(ctx, comp_ctx->curr_func.decls_idx, i); /* decl name */
  56300. h_name = duk_get_hstring(ctx, -1);
  56301. DUK_ASSERT(h_name != NULL);
  56302. if (h_name == DUK_HTHREAD_STRING_LC_ARGUMENTS(thr) &&
  56303. !comp_ctx->curr_func.is_arguments_shadowed) {
  56304. /* E5 Section steps 7-8 */
  56305. DUK_DDD(DUK_DDDPRINT("'arguments' not shadowed by a function declaration, "
  56306. "but appears as a variable declaration -> treat as "
  56307. "a no-op for variable declaration purposes"));
  56308. duk_pop(ctx);
  56309. continue;
  56310. }
  56311. /* XXX: spilling */
  56312. if (comp_ctx->curr_func.is_function) {
  56313. duk_reg_t reg_bind = DUK__ALLOCTEMP(comp_ctx);
  56314. /* no need to init reg, it will be undefined on entry */
  56315. duk_push_int(ctx, (duk_int_t) reg_bind);
  56316. } else {
  56317. duk_dup_top(ctx);
  56318. rc_name = duk__getconst(comp_ctx);
  56319. duk_push_null(ctx);
  56320. declvar_flags = DUK_PROPDESC_FLAG_WRITABLE |
  56321. DUK_PROPDESC_FLAG_ENUMERABLE |
  56322. DUK_BC_DECLVAR_FLAG_UNDEF_VALUE;
  56323. if (configurable_bindings) {
  56324. declvar_flags |= DUK_PROPDESC_FLAG_CONFIGURABLE;
  56325. }
  56326. duk__emit_a_b_c(comp_ctx,
  56327. DUK_OP_DECLVAR | DUK__EMIT_FLAG_NO_SHUFFLE_A,
  56328. (duk_regconst_t) declvar_flags /*flags*/,
  56329. rc_name /*name*/,
  56330. (duk_regconst_t) 0 /*value*/);
  56331. }
  56332. duk_put_prop(ctx, comp_ctx->curr_func.varmap_idx); /* [ ... name reg/null ] -> [ ... ] */
  56333. }
  56334. }
  56335. /*
  56336. * Wrap up
  56337. */
  56338. DUK_DDD(DUK_DDDPRINT("varmap: %!T, is_arguments_shadowed=%ld",
  56339. (duk_tval *) duk_get_tval(ctx, comp_ctx->curr_func.varmap_idx),
  56340. (long) comp_ctx->curr_func.is_arguments_shadowed));
  56341. DUK_ASSERT_TOP(ctx, entry_top);
  56342. return;
  56343. error_outofregs:
  56344. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_REG_LIMIT);
  56345. DUK_UNREACHABLE();
  56346. return;
  56347. error_argname:
  56348. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_ARG_NAME);
  56349. DUK_UNREACHABLE();
  56350. return;
  56351. }
  56352. /*
  56353. * Parse a function-body-like expression (FunctionBody or Program
  56354. * in E5 grammar) using a two-pass parse. The productions appear
  56355. * in the following contexts:
  56356. *
  56357. * - function expression
  56358. * - function statement
  56359. * - function declaration
  56360. * - getter in object literal
  56361. * - setter in object literal
  56362. * - global code
  56363. * - eval code
  56364. * - Function constructor body
  56365. *
  56366. * This function only parses the statement list of the body; the argument
  56367. * list and possible function name must be initialized by the caller.
  56368. * For instance, for Function constructor, the argument names are originally
  56369. * on the value stack. The parsing of statements ends either at an EOF or
  56370. * a closing brace; this is controlled by an input flag.
  56371. *
  56372. * Note that there are many differences affecting parsing and even code
  56373. * generation:
  56374. *
  56375. * - Global and eval code have an implicit return value generated
  56376. * by the last statement; function code does not
  56377. *
  56378. * - Global code, eval code, and Function constructor body end in
  56379. * an EOF, other bodies in a closing brace ('}')
  56380. *
  56381. * Upon entry, 'curr_tok' is ignored and the function will pull in the
  56382. * first token on its own. Upon exit, 'curr_tok' is the terminating
  56383. * token (EOF or closing brace).
  56384. */
  56385. DUK_LOCAL void duk__parse_func_body(duk_compiler_ctx *comp_ctx, duk_bool_t expect_eof, duk_bool_t implicit_return_value, duk_small_int_t expect_token) {
  56386. duk_compiler_func *func;
  56387. duk_hthread *thr;
  56388. duk_context *ctx;
  56389. duk_reg_t reg_stmt_value = -1;
  56390. duk_lexer_point lex_pt;
  56391. duk_reg_t temp_first;
  56392. duk_small_int_t compile_round = 1;
  56393. DUK_ASSERT(comp_ctx != NULL);
  56394. thr = comp_ctx->thr;
  56395. ctx = (duk_context *) thr;
  56396. DUK_ASSERT(thr != NULL);
  56397. func = &comp_ctx->curr_func;
  56398. DUK_ASSERT(func != NULL);
  56399. DUK__RECURSION_INCREASE(comp_ctx, thr);
  56400. duk_require_stack(ctx, DUK__FUNCTION_BODY_REQUIRE_SLOTS);
  56401. /*
  56402. * Store lexer position for a later rewind
  56403. */
  56404. DUK_LEXER_GETPOINT(&comp_ctx->lex, &lex_pt);
  56405. /*
  56406. * Program code (global and eval code) has an implicit return value
  56407. * from the last statement value (e.g. eval("1; 2+3;") returns 3).
  56408. * This is not the case with functions. If implicit statement return
  56409. * value is requested, all statements are coerced to a register
  56410. * allocated here, and used in the implicit return statement below.
  56411. */
  56412. /* XXX: this is pointless here because pass 1 is throw-away */
  56413. if (implicit_return_value) {
  56414. reg_stmt_value = DUK__ALLOCTEMP(comp_ctx);
  56415. /* If an implicit return value is needed by caller, it must be
  56416. * initialized to 'undefined' because we don't know whether any
  56417. * non-empty (where "empty" is a continuation type, and different
  56418. * from an empty statement) statements will be executed.
  56419. *
  56420. * However, since 1st pass is a throwaway one, no need to emit
  56421. * it here.
  56422. */
  56423. #if 0
  56424. duk__emit_extraop_bc(comp_ctx,
  56425. DUK_EXTRAOP_LDUNDEF,
  56426. 0);
  56427. #endif
  56428. }
  56429. /*
  56430. * First pass.
  56431. *
  56432. * Gather variable/function declarations needed for second pass.
  56433. * Code generated is dummy and discarded.
  56434. */
  56435. func->in_directive_prologue = 1;
  56436. func->in_scanning = 1;
  56437. func->may_direct_eval = 0;
  56438. func->id_access_arguments = 0;
  56439. func->id_access_slow = 0;
  56440. func->reg_stmt_value = reg_stmt_value;
  56441. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  56442. func->min_line = DUK_INT_MAX;
  56443. func->max_line = 0;
  56444. #endif
  56445. /* duk__parse_stmts() expects curr_tok to be set; parse in "allow regexp literal" mode with current strictness */
  56446. if (expect_token >= 0) {
  56447. /* Eating a left curly; regexp mode is allowed by left curly
  56448. * based on duk__token_lbp[] automatically.
  56449. */
  56450. DUK_ASSERT(expect_token == DUK_TOK_LCURLY);
  56451. duk__update_lineinfo_currtoken(comp_ctx);
  56452. duk__advance_expect(comp_ctx, expect_token);
  56453. } else {
  56454. /* Need to set curr_token.t because lexing regexp mode depends on current
  56455. * token type. Zero value causes "allow regexp" mode.
  56456. */
  56457. comp_ctx->curr_token.t = 0;
  56458. duk__advance(comp_ctx);
  56459. }
  56460. DUK_DDD(DUK_DDDPRINT("begin 1st pass"));
  56461. duk__parse_stmts(comp_ctx,
  56462. 1, /* allow source elements */
  56463. expect_eof); /* expect EOF instead of } */
  56464. DUK_DDD(DUK_DDDPRINT("end 1st pass"));
  56465. /*
  56466. * Second (and possibly third) pass.
  56467. *
  56468. * Generate actual code. In most cases the need for shuffle
  56469. * registers is detected during pass 1, but in some corner cases
  56470. * we'll only detect it during pass 2 and a third pass is then
  56471. * needed (see GH-115).
  56472. */
  56473. for (;;) {
  56474. duk_bool_t needs_shuffle_before = comp_ctx->curr_func.needs_shuffle;
  56475. compile_round++;
  56476. /*
  56477. * Rewind lexer.
  56478. *
  56479. * duk__parse_stmts() expects curr_tok to be set; parse in "allow regexp
  56480. * literal" mode with current strictness.
  56481. *
  56482. * curr_token line number info should be initialized for pass 2 before
  56483. * generating prologue, to ensure prologue bytecode gets nice line numbers.
  56484. */
  56485. DUK_DDD(DUK_DDDPRINT("rewind lexer"));
  56486. DUK_LEXER_SETPOINT(&comp_ctx->lex, &lex_pt);
  56487. comp_ctx->curr_token.t = 0; /* this is needed for regexp mode */
  56488. comp_ctx->curr_token.start_line = 0; /* needed for line number tracking (becomes prev_token.start_line) */
  56489. duk__advance(comp_ctx);
  56490. /*
  56491. * Reset function state and perform register allocation, which creates
  56492. * 'varmap' for second pass. Function prologue for variable declarations,
  56493. * binding value initializations etc is emitted as a by-product.
  56494. *
  56495. * Strict mode restrictions for duplicate and invalid argument
  56496. * names are checked here now that we know whether the function
  56497. * is actually strict. See: test-dev-strict-mode-boundary.js.
  56498. *
  56499. * Inner functions are compiled during pass 1 and are not reset.
  56500. */
  56501. duk__reset_func_for_pass2(comp_ctx);
  56502. func->in_directive_prologue = 1;
  56503. func->in_scanning = 0;
  56504. /* must be able to emit code, alloc consts, etc. */
  56505. duk__init_varmap_and_prologue_for_pass2(comp_ctx,
  56506. (implicit_return_value ? &reg_stmt_value : NULL));
  56507. func->reg_stmt_value = reg_stmt_value;
  56508. temp_first = DUK__GETTEMP(comp_ctx);
  56509. func->temp_first = temp_first;
  56510. func->temp_next = temp_first;
  56511. func->stmt_next = 0;
  56512. func->label_next = 0;
  56513. /* XXX: init or assert catch depth etc -- all values */
  56514. func->id_access_arguments = 0;
  56515. func->id_access_slow = 0;
  56516. /*
  56517. * Check function name validity now that we know strictness.
  56518. * This only applies to function declarations and expressions,
  56519. * not setter/getter name.
  56520. *
  56521. * See: test-dev-strict-mode-boundary.js
  56522. */
  56523. if (func->is_function && !func->is_setget && func->h_name != NULL) {
  56524. if (func->is_strict) {
  56525. if (duk__hstring_is_eval_or_arguments(comp_ctx, func->h_name)) {
  56526. DUK_DDD(DUK_DDDPRINT("func name is 'eval' or 'arguments' in strict mode"));
  56527. goto error_funcname;
  56528. }
  56529. if (DUK_HSTRING_HAS_STRICT_RESERVED_WORD(func->h_name)) {
  56530. DUK_DDD(DUK_DDDPRINT("func name is a reserved word in strict mode"));
  56531. goto error_funcname;
  56532. }
  56533. } else {
  56534. if (DUK_HSTRING_HAS_RESERVED_WORD(func->h_name) &&
  56535. !DUK_HSTRING_HAS_STRICT_RESERVED_WORD(func->h_name)) {
  56536. DUK_DDD(DUK_DDDPRINT("func name is a reserved word in non-strict mode"));
  56537. goto error_funcname;
  56538. }
  56539. }
  56540. }
  56541. /*
  56542. * Second pass parsing.
  56543. */
  56544. if (implicit_return_value) {
  56545. /* Default implicit return value. */
  56546. duk__emit_extraop_bc(comp_ctx,
  56547. DUK_EXTRAOP_LDUNDEF,
  56548. 0);
  56549. }
  56550. DUK_DDD(DUK_DDDPRINT("begin 2nd pass"));
  56551. duk__parse_stmts(comp_ctx,
  56552. 1, /* allow source elements */
  56553. expect_eof); /* expect EOF instead of } */
  56554. DUK_DDD(DUK_DDDPRINT("end 2nd pass"));
  56555. duk__update_lineinfo_currtoken(comp_ctx);
  56556. if (needs_shuffle_before == comp_ctx->curr_func.needs_shuffle) {
  56557. /* Shuffle decision not changed. */
  56558. break;
  56559. }
  56560. if (compile_round >= 3) {
  56561. /* Should never happen but avoid infinite loop just in case. */
  56562. DUK_D(DUK_DPRINT("more than 3 compile passes needed, should never happen"));
  56563. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR);
  56564. }
  56565. DUK_D(DUK_DPRINT("need additional round to compile function, round now %d", (int) compile_round));
  56566. }
  56567. /*
  56568. * Emit a final RETURN.
  56569. *
  56570. * It would be nice to avoid emitting an unnecessary "return" opcode
  56571. * if the current PC is not reachable. However, this cannot be reliably
  56572. * detected; even if the previous instruction is an unconditional jump,
  56573. * there may be a previous jump which jumps to current PC (which is the
  56574. * case for iteration and conditional statements, for instance).
  56575. */
  56576. /* XXX: request a "last statement is terminal" from duk__parse_stmt() and duk__parse_stmts();
  56577. * we could avoid the last RETURN if we could ensure there is no way to get here
  56578. * (directly or via a jump)
  56579. */
  56580. DUK_ASSERT(comp_ctx->curr_func.catch_depth == 0);
  56581. if (reg_stmt_value >= 0) {
  56582. duk__emit_a_b(comp_ctx,
  56583. DUK_OP_RETURN | DUK__EMIT_FLAG_NO_SHUFFLE_A,
  56584. (duk_regconst_t) DUK_BC_RETURN_FLAG_HAVE_RETVAL /*flags*/,
  56585. (duk_regconst_t) reg_stmt_value /*reg*/);
  56586. } else {
  56587. duk__emit_a_b(comp_ctx,
  56588. DUK_OP_RETURN | DUK__EMIT_FLAG_NO_SHUFFLE_A,
  56589. (duk_regconst_t) 0 /*flags*/,
  56590. (duk_regconst_t) 0 /*reg(ignored)*/);
  56591. }
  56592. /*
  56593. * Peephole optimize JUMP chains.
  56594. */
  56595. duk__peephole_optimize_bytecode(comp_ctx);
  56596. /*
  56597. * comp_ctx->curr_func is now ready to be converted into an actual
  56598. * function template.
  56599. */
  56600. DUK__RECURSION_DECREASE(comp_ctx, thr);
  56601. return;
  56602. error_funcname:
  56603. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_FUNC_NAME);
  56604. }
  56605. /*
  56606. * Parse a function-like expression:
  56607. *
  56608. * - function expression
  56609. * - function declaration
  56610. * - function statement (non-standard)
  56611. * - setter/getter
  56612. *
  56613. * Adds the function to comp_ctx->curr_func function table and returns the
  56614. * function number.
  56615. *
  56616. * On entry, curr_token points to:
  56617. *
  56618. * - the token after 'function' for function expression/declaration/statement
  56619. * - the token after 'set' or 'get' for setter/getter
  56620. */
  56621. /* Parse formals. */
  56622. DUK_LOCAL void duk__parse_func_formals(duk_compiler_ctx *comp_ctx) {
  56623. duk_hthread *thr = comp_ctx->thr;
  56624. duk_context *ctx = (duk_context *) thr;
  56625. duk_bool_t first = 1;
  56626. duk_uarridx_t n;
  56627. for (;;) {
  56628. if (comp_ctx->curr_token.t == DUK_TOK_RPAREN) {
  56629. break;
  56630. }
  56631. if (first) {
  56632. /* no comma */
  56633. first = 0;
  56634. } else {
  56635. duk__advance_expect(comp_ctx, DUK_TOK_COMMA);
  56636. }
  56637. /* Note: when parsing a formal list in non-strict context, e.g.
  56638. * "implements" is parsed as an identifier. When the function is
  56639. * later detected to be strict, the argument list must be rechecked
  56640. * against a larger set of reserved words (that of strict mode).
  56641. * This is handled by duk__parse_func_body(). Here we recognize
  56642. * whatever tokens are considered reserved in current strictness
  56643. * (which is not always enough).
  56644. */
  56645. if (comp_ctx->curr_token.t != DUK_TOK_IDENTIFIER) {
  56646. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, "expected identifier");
  56647. }
  56648. DUK_ASSERT(comp_ctx->curr_token.t == DUK_TOK_IDENTIFIER);
  56649. DUK_ASSERT(comp_ctx->curr_token.str1 != NULL);
  56650. DUK_DDD(DUK_DDDPRINT("formal argument: %!O",
  56651. (duk_heaphdr *) comp_ctx->curr_token.str1));
  56652. /* XXX: append primitive */
  56653. duk_push_hstring(ctx, comp_ctx->curr_token.str1);
  56654. n = (duk_uarridx_t) duk_get_length(ctx, comp_ctx->curr_func.argnames_idx);
  56655. duk_put_prop_index(ctx, comp_ctx->curr_func.argnames_idx, n);
  56656. duk__advance(comp_ctx); /* eat identifier */
  56657. }
  56658. }
  56659. /* Parse a function-like expression, assuming that 'comp_ctx->curr_func' is
  56660. * correctly set up. Assumes that curr_token is just after 'function' (or
  56661. * 'set'/'get' etc).
  56662. */
  56663. DUK_LOCAL void duk__parse_func_like_raw(duk_compiler_ctx *comp_ctx, duk_bool_t is_decl, duk_bool_t is_setget) {
  56664. duk_hthread *thr = comp_ctx->thr;
  56665. duk_context *ctx = (duk_context *) thr;
  56666. DUK_ASSERT(comp_ctx->curr_func.num_formals == 0);
  56667. DUK_ASSERT(comp_ctx->curr_func.is_function == 1);
  56668. DUK_ASSERT(comp_ctx->curr_func.is_eval == 0);
  56669. DUK_ASSERT(comp_ctx->curr_func.is_global == 0);
  56670. DUK_ASSERT(comp_ctx->curr_func.is_setget == is_setget);
  56671. DUK_ASSERT(comp_ctx->curr_func.is_decl == is_decl);
  56672. duk__update_lineinfo_currtoken(comp_ctx);
  56673. /*
  56674. * Function name (if any)
  56675. *
  56676. * We don't check for prohibited names here, because we don't
  56677. * yet know whether the function will be strict. Function body
  56678. * parsing handles this retroactively.
  56679. *
  56680. * For function expressions and declarations function name must
  56681. * be an Identifer (excludes reserved words). For setter/getter
  56682. * it is a PropertyName which allows reserved words and also
  56683. * strings and numbers (e.g. "{ get 1() { ... } }").
  56684. */
  56685. if (is_setget) {
  56686. /* PropertyName -> IdentifierName | StringLiteral | NumericLiteral */
  56687. if (comp_ctx->curr_token.t_nores == DUK_TOK_IDENTIFIER ||
  56688. comp_ctx->curr_token.t == DUK_TOK_STRING) {
  56689. duk_push_hstring(ctx, comp_ctx->curr_token.str1); /* keep in valstack */
  56690. } else if (comp_ctx->curr_token.t == DUK_TOK_NUMBER) {
  56691. duk_push_number(ctx, comp_ctx->curr_token.num);
  56692. duk_to_string(ctx, -1);
  56693. } else {
  56694. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_GETSET_NAME);
  56695. }
  56696. comp_ctx->curr_func.h_name = duk_get_hstring(ctx, -1); /* borrowed reference */
  56697. DUK_ASSERT(comp_ctx->curr_func.h_name != NULL);
  56698. duk__advance(comp_ctx);
  56699. } else {
  56700. /* Function name is an Identifier (not IdentifierName), but we get
  56701. * the raw name (not recognizing keywords) here and perform the name
  56702. * checks only after pass 1.
  56703. */
  56704. if (comp_ctx->curr_token.t_nores == DUK_TOK_IDENTIFIER) {
  56705. duk_push_hstring(ctx, comp_ctx->curr_token.str1); /* keep in valstack */
  56706. comp_ctx->curr_func.h_name = duk_get_hstring(ctx, -1); /* borrowed reference */
  56707. DUK_ASSERT(comp_ctx->curr_func.h_name != NULL);
  56708. duk__advance(comp_ctx);
  56709. } else {
  56710. /* valstack will be unbalanced, which is OK */
  56711. DUK_ASSERT(!is_setget);
  56712. if (is_decl) {
  56713. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_FUNC_NAME_REQUIRED);
  56714. }
  56715. }
  56716. }
  56717. DUK_DDD(DUK_DDDPRINT("function name: %!O",
  56718. (duk_heaphdr *) comp_ctx->curr_func.h_name));
  56719. /*
  56720. * Formal argument list
  56721. *
  56722. * We don't check for prohibited names or for duplicate argument
  56723. * names here, becase we don't yet know whether the function will
  56724. * be strict. Function body parsing handles this retroactively.
  56725. */
  56726. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  56727. duk__parse_func_formals(comp_ctx);
  56728. DUK_ASSERT(comp_ctx->curr_token.t == DUK_TOK_RPAREN);
  56729. duk__advance(comp_ctx);
  56730. /*
  56731. * Parse function body
  56732. */
  56733. duk__parse_func_body(comp_ctx,
  56734. 0, /* expect_eof */
  56735. 0, /* implicit_return_value */
  56736. DUK_TOK_LCURLY); /* expect_token */
  56737. /*
  56738. * Convert duk_compiler_func to a function template and add it
  56739. * to the parent function table.
  56740. */
  56741. duk__convert_to_func_template(comp_ctx, is_setget /*force_no_namebind*/); /* -> [ ... func ] */
  56742. }
  56743. /* Parse an inner function, adding the function template to the current function's
  56744. * function table. Return a function number to be used by the outer function.
  56745. *
  56746. * Avoiding O(depth^2) inner function parsing is handled here. On the first pass,
  56747. * compile and register the function normally into the 'funcs' array, also recording
  56748. * a lexer point (offset/line) to the closing brace of the function. On the second
  56749. * pass, skip the function and return the same 'fnum' as on the first pass by using
  56750. * a running counter.
  56751. *
  56752. * An unfortunate side effect of this is that when parsing the inner function, almost
  56753. * nothing is known of the outer function, i.e. the inner function's scope. We don't
  56754. * need that information at the moment, but it would allow some optimizations if it
  56755. * were used.
  56756. */
  56757. DUK_LOCAL duk_int_t duk__parse_func_like_fnum(duk_compiler_ctx *comp_ctx, duk_bool_t is_decl, duk_bool_t is_setget) {
  56758. duk_hthread *thr = comp_ctx->thr;
  56759. duk_context *ctx = (duk_context *) thr;
  56760. duk_compiler_func old_func;
  56761. duk_idx_t entry_top;
  56762. duk_int_t fnum;
  56763. /*
  56764. * On second pass, skip the function.
  56765. */
  56766. if (!comp_ctx->curr_func.in_scanning) {
  56767. duk_lexer_point lex_pt;
  56768. fnum = comp_ctx->curr_func.fnum_next++;
  56769. duk_get_prop_index(ctx, comp_ctx->curr_func.funcs_idx, (duk_uarridx_t) (fnum * 3 + 1));
  56770. lex_pt.offset = duk_to_int(ctx, -1);
  56771. duk_pop(ctx);
  56772. duk_get_prop_index(ctx, comp_ctx->curr_func.funcs_idx, (duk_uarridx_t) (fnum * 3 + 2));
  56773. lex_pt.line = duk_to_int(ctx, -1);
  56774. duk_pop(ctx);
  56775. DUK_DDD(DUK_DDDPRINT("second pass of an inner func, skip the function, reparse closing brace; lex offset=%ld, line=%ld",
  56776. (long) lex_pt.offset, (long) lex_pt.line));
  56777. DUK_LEXER_SETPOINT(&comp_ctx->lex, &lex_pt);
  56778. comp_ctx->curr_token.t = 0; /* this is needed for regexp mode */
  56779. comp_ctx->curr_token.start_line = 0; /* needed for line number tracking (becomes prev_token.start_line) */
  56780. duk__advance(comp_ctx);
  56781. duk__advance_expect(comp_ctx, DUK_TOK_RCURLY);
  56782. return fnum;
  56783. }
  56784. /*
  56785. * On first pass, perform actual parsing. Remember valstack top on entry
  56786. * to restore it later, and switch to using a new function in comp_ctx.
  56787. */
  56788. entry_top = duk_get_top(ctx);
  56789. DUK_DDD(DUK_DDDPRINT("before func: entry_top=%ld, curr_tok.start_offset=%ld",
  56790. (long) entry_top, (long) comp_ctx->curr_token.start_offset));
  56791. DUK_MEMCPY(&old_func, &comp_ctx->curr_func, sizeof(duk_compiler_func));
  56792. DUK_MEMZERO(&comp_ctx->curr_func, sizeof(duk_compiler_func));
  56793. duk__init_func_valstack_slots(comp_ctx);
  56794. DUK_ASSERT(comp_ctx->curr_func.num_formals == 0);
  56795. /* inherit initial strictness from parent */
  56796. comp_ctx->curr_func.is_strict = old_func.is_strict;
  56797. DUK_ASSERT(comp_ctx->curr_func.is_notail == 0);
  56798. comp_ctx->curr_func.is_function = 1;
  56799. DUK_ASSERT(comp_ctx->curr_func.is_eval == 0);
  56800. DUK_ASSERT(comp_ctx->curr_func.is_global == 0);
  56801. comp_ctx->curr_func.is_setget = is_setget;
  56802. comp_ctx->curr_func.is_decl = is_decl;
  56803. /*
  56804. * Parse inner function
  56805. */
  56806. duk__parse_func_like_raw(comp_ctx, is_decl, is_setget); /* pushes function template */
  56807. /* prev_token.start_offset points to the closing brace here; when skipping
  56808. * we're going to reparse the closing brace to ensure semicolon insertion
  56809. * etc work as expected.
  56810. */
  56811. DUK_DDD(DUK_DDDPRINT("after func: prev_tok.start_offset=%ld, curr_tok.start_offset=%ld",
  56812. (long) comp_ctx->prev_token.start_offset, (long) comp_ctx->curr_token.start_offset));
  56813. DUK_ASSERT(comp_ctx->lex.input[comp_ctx->prev_token.start_offset] == (duk_uint8_t) DUK_ASC_RCURLY);
  56814. /* XXX: append primitive */
  56815. DUK_ASSERT(duk_get_length(ctx, old_func.funcs_idx) == (duk_size_t) (old_func.fnum_next * 3));
  56816. fnum = old_func.fnum_next++;
  56817. if (fnum > DUK__MAX_FUNCS) {
  56818. DUK_ERROR(comp_ctx->thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_FUNC_LIMIT);
  56819. }
  56820. /* array writes autoincrement length */
  56821. (void) duk_put_prop_index(ctx, old_func.funcs_idx, (duk_uarridx_t) (fnum * 3));
  56822. duk_push_size_t(ctx, comp_ctx->prev_token.start_offset);
  56823. (void) duk_put_prop_index(ctx, old_func.funcs_idx, (duk_uarridx_t) (fnum * 3 + 1));
  56824. duk_push_int(ctx, comp_ctx->prev_token.start_line);
  56825. (void) duk_put_prop_index(ctx, old_func.funcs_idx, (duk_uarridx_t) (fnum * 3 + 2));
  56826. /*
  56827. * Cleanup: restore original function, restore valstack state.
  56828. */
  56829. DUK_MEMCPY((void *) &comp_ctx->curr_func, (void *) &old_func, sizeof(duk_compiler_func));
  56830. duk_set_top(ctx, entry_top);
  56831. DUK_ASSERT_TOP(ctx, entry_top);
  56832. return fnum;
  56833. }
  56834. /*
  56835. * Compile input string into an executable function template without
  56836. * arguments.
  56837. *
  56838. * The string is parsed as the "Program" production of Ecmascript E5.
  56839. * Compilation context can be either global code or eval code (see E5
  56840. * Sections 14 and 15.1.2.1).
  56841. *
  56842. * Input stack: [ ... filename ]
  56843. * Output stack: [ ... func_template ]
  56844. */
  56845. /* XXX: source code property */
  56846. DUK_LOCAL duk_ret_t duk__js_compile_raw(duk_context *ctx) {
  56847. duk_hthread *thr = (duk_hthread *) ctx;
  56848. duk_hstring *h_filename;
  56849. duk__compiler_stkstate *comp_stk;
  56850. duk_compiler_ctx *comp_ctx;
  56851. duk_lexer_point *lex_pt;
  56852. duk_compiler_func *func;
  56853. duk_idx_t entry_top;
  56854. duk_bool_t is_strict;
  56855. duk_bool_t is_eval;
  56856. duk_bool_t is_funcexpr;
  56857. duk_small_uint_t flags;
  56858. DUK_ASSERT(thr != NULL);
  56859. /*
  56860. * Arguments check
  56861. */
  56862. entry_top = duk_get_top(ctx);
  56863. DUK_ASSERT(entry_top >= 2);
  56864. comp_stk = (duk__compiler_stkstate *) duk_require_pointer(ctx, -1);
  56865. comp_ctx = &comp_stk->comp_ctx_alloc;
  56866. lex_pt = &comp_stk->lex_pt_alloc;
  56867. DUK_ASSERT(comp_ctx != NULL);
  56868. DUK_ASSERT(lex_pt != NULL);
  56869. flags = comp_stk->flags;
  56870. is_eval = (flags & DUK_JS_COMPILE_FLAG_EVAL ? 1 : 0);
  56871. is_strict = (flags & DUK_JS_COMPILE_FLAG_STRICT ? 1 : 0);
  56872. is_funcexpr = (flags & DUK_JS_COMPILE_FLAG_FUNCEXPR ? 1 : 0);
  56873. h_filename = duk_get_hstring(ctx, -2); /* may be undefined */
  56874. /*
  56875. * Init compiler and lexer contexts
  56876. */
  56877. func = &comp_ctx->curr_func;
  56878. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  56879. comp_ctx->thr = NULL;
  56880. comp_ctx->h_filename = NULL;
  56881. comp_ctx->prev_token.str1 = NULL;
  56882. comp_ctx->prev_token.str2 = NULL;
  56883. comp_ctx->curr_token.str1 = NULL;
  56884. comp_ctx->curr_token.str2 = NULL;
  56885. #endif
  56886. duk_require_stack(ctx, DUK__COMPILE_ENTRY_SLOTS);
  56887. duk_push_dynamic_buffer(ctx, 0); /* entry_top + 0 */
  56888. duk_push_undefined(ctx); /* entry_top + 1 */
  56889. duk_push_undefined(ctx); /* entry_top + 2 */
  56890. duk_push_undefined(ctx); /* entry_top + 3 */
  56891. duk_push_undefined(ctx); /* entry_top + 4 */
  56892. comp_ctx->thr = thr;
  56893. comp_ctx->h_filename = h_filename;
  56894. comp_ctx->tok11_idx = entry_top + 1;
  56895. comp_ctx->tok12_idx = entry_top + 2;
  56896. comp_ctx->tok21_idx = entry_top + 3;
  56897. comp_ctx->tok22_idx = entry_top + 4;
  56898. comp_ctx->recursion_limit = DUK_USE_COMPILER_RECLIMIT;
  56899. /* comp_ctx->lex has been pre-initialized by caller: it has been
  56900. * zeroed and input/input_length has been set.
  56901. */
  56902. comp_ctx->lex.thr = thr;
  56903. /* comp_ctx->lex.input and comp_ctx->lex.input_length filled by caller */
  56904. comp_ctx->lex.slot1_idx = comp_ctx->tok11_idx;
  56905. comp_ctx->lex.slot2_idx = comp_ctx->tok12_idx;
  56906. comp_ctx->lex.buf_idx = entry_top + 0;
  56907. comp_ctx->lex.buf = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, entry_top + 0);
  56908. DUK_ASSERT(comp_ctx->lex.buf != NULL);
  56909. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(comp_ctx->lex.buf) && !DUK_HBUFFER_HAS_EXTERNAL(comp_ctx->lex.buf));
  56910. comp_ctx->lex.token_limit = DUK_COMPILER_TOKEN_LIMIT;
  56911. lex_pt->offset = 0;
  56912. lex_pt->line = 1;
  56913. DUK_LEXER_SETPOINT(&comp_ctx->lex, lex_pt); /* fills window */
  56914. comp_ctx->curr_token.start_line = 0; /* needed for line number tracking (becomes prev_token.start_line) */
  56915. /*
  56916. * Initialize function state for a zero-argument function
  56917. */
  56918. duk__init_func_valstack_slots(comp_ctx);
  56919. DUK_ASSERT(func->num_formals == 0);
  56920. if (is_funcexpr) {
  56921. /* Name will be filled from function expression, not by caller.
  56922. * This case is used by Function constructor and duk_compile()
  56923. * API with the DUK_COMPILE_FUNCTION option.
  56924. */
  56925. DUK_ASSERT(func->h_name == NULL);
  56926. } else {
  56927. duk_push_hstring_stridx(ctx, (is_eval ? DUK_STRIDX_EVAL :
  56928. DUK_STRIDX_GLOBAL));
  56929. func->h_name = duk_get_hstring(ctx, -1);
  56930. }
  56931. /*
  56932. * Parse a function body or a function-like expression, depending
  56933. * on flags.
  56934. */
  56935. func->is_strict = is_strict;
  56936. func->is_setget = 0;
  56937. func->is_decl = 0;
  56938. if (is_funcexpr) {
  56939. func->is_function = 1;
  56940. func->is_eval = 0;
  56941. func->is_global = 0;
  56942. duk__advance(comp_ctx); /* init 'curr_token' */
  56943. duk__advance_expect(comp_ctx, DUK_TOK_FUNCTION);
  56944. (void) duk__parse_func_like_raw(comp_ctx,
  56945. 0, /* is_decl */
  56946. 0); /* is_setget */
  56947. } else {
  56948. func->is_function = 0;
  56949. func->is_eval = is_eval;
  56950. func->is_global = !is_eval;
  56951. duk__parse_func_body(comp_ctx,
  56952. 1, /* expect_eof */
  56953. 1, /* implicit_return_value */
  56954. -1); /* expect_token */
  56955. }
  56956. /*
  56957. * Convert duk_compiler_func to a function template
  56958. */
  56959. duk__convert_to_func_template(comp_ctx, 0 /*force_no_namebind*/);
  56960. /*
  56961. * Wrapping duk_safe_call() will mangle the stack, just return stack top
  56962. */
  56963. /* [ ... filename (temps) func ] */
  56964. return 1;
  56965. }
  56966. 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) {
  56967. duk_context *ctx = (duk_context *) thr;
  56968. duk__compiler_stkstate comp_stk;
  56969. duk_compiler_ctx *prev_ctx;
  56970. duk_ret_t safe_rc;
  56971. /* XXX: this illustrates that a C catchpoint implemented using duk_safe_call()
  56972. * is a bit heavy at the moment. The wrapper compiles to ~180 bytes on x64.
  56973. * Alternatives would be nice.
  56974. */
  56975. DUK_ASSERT(thr != NULL);
  56976. DUK_ASSERT(src_buffer != NULL);
  56977. /* preinitialize lexer state partially */
  56978. DUK_MEMZERO(&comp_stk, sizeof(comp_stk));
  56979. comp_stk.flags = flags;
  56980. DUK_LEXER_INITCTX(&comp_stk.comp_ctx_alloc.lex);
  56981. comp_stk.comp_ctx_alloc.lex.input = src_buffer;
  56982. comp_stk.comp_ctx_alloc.lex.input_length = src_length;
  56983. duk_push_pointer(ctx, (void *) &comp_stk);
  56984. /* [ ... filename &comp_stk ] */
  56985. prev_ctx = thr->compile_ctx;
  56986. thr->compile_ctx = &comp_stk.comp_ctx_alloc; /* for duk_error_augment.c */
  56987. safe_rc = duk_safe_call(ctx, duk__js_compile_raw, 2 /*nargs*/, 1 /*nret*/);
  56988. thr->compile_ctx = prev_ctx;
  56989. if (safe_rc != DUK_EXEC_SUCCESS) {
  56990. /* Append a "(line NNN)" to the "message" property of any
  56991. * error thrown during compilation. Usually compilation
  56992. * errors are SyntaxErrors but they can also be out-of-memory
  56993. * errors and the like.
  56994. *
  56995. * Source file/line are added to tracedata directly by
  56996. * duk_error_augment.c based on thr->compile_ctx.
  56997. */
  56998. /* [ ... error ] */
  56999. DUK_DDD(DUK_DDDPRINT("compile error, before adding line info: %!T",
  57000. (duk_tval *) duk_get_tval(ctx, -1)));
  57001. if (duk_is_object(ctx, -1)) {
  57002. /* XXX: Now that fileName and lineNumber are set, this is
  57003. * unnecessary. Remove in Duktape 1.3.0?
  57004. */
  57005. if (duk_get_prop_stridx(ctx, -1, DUK_STRIDX_MESSAGE)) {
  57006. duk_push_sprintf(ctx, " (line %ld)", (long) comp_stk.comp_ctx_alloc.curr_token.start_line);
  57007. duk_concat(ctx, 2);
  57008. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE);
  57009. } else {
  57010. duk_pop(ctx);
  57011. }
  57012. }
  57013. DUK_DDD(DUK_DDDPRINT("compile error, after adding line info: %!T",
  57014. (duk_tval *) duk_get_tval(ctx, -1)));
  57015. duk_throw(ctx);
  57016. }
  57017. /* [ ... template ] */
  57018. }
  57019. #line 1 "duk_js_executor.c"
  57020. /*
  57021. * Ecmascript bytecode executor.
  57022. */
  57023. /* include removed: duk_internal.h */
  57024. /*
  57025. * Local declarations.
  57026. */
  57027. DUK_LOCAL_DECL void duk__js_execute_bytecode_inner(duk_hthread *entry_thread, duk_size_t entry_callstack_top);
  57028. /*
  57029. * Arithmetic, binary, and logical helpers.
  57030. *
  57031. * Note: there is no opcode for logical AND or logical OR; this is on
  57032. * purpose, because the evalution order semantics for them make such
  57033. * opcodes pretty pointless: short circuiting means they are most
  57034. * comfortably implemented as jumps. However, a logical NOT opcode
  57035. * is useful.
  57036. *
  57037. * Note: careful with duk_tval pointers here: they are potentially
  57038. * invalidated by any DECREF and almost any API call. It's still
  57039. * preferable to work without making a copy but that's not always
  57040. * possible.
  57041. */
  57042. DUK_LOCAL duk_double_t duk__compute_mod(duk_double_t d1, duk_double_t d2) {
  57043. /*
  57044. * Ecmascript modulus ('%') does not match IEEE 754 "remainder"
  57045. * operation (implemented by remainder() in C99) but does seem
  57046. * to match ANSI C fmod().
  57047. *
  57048. * Compare E5 Section 11.5.3 and "man fmod".
  57049. */
  57050. return (duk_double_t) DUK_FMOD((double) d1, (double) d2);
  57051. }
  57052. DUK_LOCAL void duk__vm_arith_add(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_uint_fast_t idx_z) {
  57053. /*
  57054. * Addition operator is different from other arithmetic
  57055. * operations in that it also provides string concatenation.
  57056. * Hence it is implemented separately.
  57057. *
  57058. * There is a fast path for number addition. Other cases go
  57059. * through potentially multiple coercions as described in the
  57060. * E5 specification. It may be possible to reduce the number
  57061. * of coercions, but this must be done carefully to preserve
  57062. * the exact semantics.
  57063. *
  57064. * E5 Section 11.6.1.
  57065. *
  57066. * Custom types also have special behavior implemented here.
  57067. */
  57068. duk_context *ctx = (duk_context *) thr;
  57069. duk_double_union du;
  57070. DUK_ASSERT(thr != NULL);
  57071. DUK_ASSERT(ctx != NULL);
  57072. DUK_ASSERT(tv_x != NULL); /* may be reg or const */
  57073. DUK_ASSERT(tv_y != NULL); /* may be reg or const */
  57074. DUK_ASSERT_DISABLE(idx_z >= 0); /* unsigned */
  57075. DUK_ASSERT((duk_uint_t) idx_z < (duk_uint_t) duk_get_top(ctx));
  57076. /*
  57077. * Fast paths
  57078. */
  57079. #if defined(DUK_USE_FASTINT)
  57080. if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) {
  57081. duk_int64_t v1, v2, v3;
  57082. duk_int32_t v3_hi;
  57083. duk_tval *tv_z;
  57084. /* Input values are signed 48-bit so we can detect overflow
  57085. * reliably from high bits or just a comparison.
  57086. */
  57087. v1 = DUK_TVAL_GET_FASTINT(tv_x);
  57088. v2 = DUK_TVAL_GET_FASTINT(tv_y);
  57089. v3 = v1 + v2;
  57090. v3_hi = (duk_int32_t) (v3 >> 32);
  57091. if (DUK_LIKELY(v3_hi >= -0x8000LL && v3_hi <= 0x7fffLL)) {
  57092. tv_z = thr->valstack_bottom + idx_z;
  57093. DUK_TVAL_SET_FASTINT_UPDREF(thr, tv_z, v3); /* side effects */
  57094. return;
  57095. } else {
  57096. /* overflow, fall through */
  57097. ;
  57098. }
  57099. }
  57100. #endif /* DUK_USE_FASTINT */
  57101. if (DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y)) {
  57102. duk_tval *tv_z;
  57103. du.d = DUK_TVAL_GET_NUMBER(tv_x) + DUK_TVAL_GET_NUMBER(tv_y);
  57104. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  57105. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  57106. tv_z = thr->valstack_bottom + idx_z;
  57107. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv_z, du.d); /* side effects */
  57108. return;
  57109. }
  57110. /*
  57111. * Slow path: potentially requires function calls for coercion
  57112. */
  57113. duk_push_tval(ctx, tv_x);
  57114. duk_push_tval(ctx, tv_y);
  57115. duk_to_primitive(ctx, -2, DUK_HINT_NONE); /* side effects -> don't use tv_x, tv_y after */
  57116. duk_to_primitive(ctx, -1, DUK_HINT_NONE);
  57117. /* As a first approximation, buffer values are coerced to strings
  57118. * for addition. This means that adding two buffers currently
  57119. * results in a string.
  57120. */
  57121. if (duk_check_type_mask(ctx, -2, DUK_TYPE_MASK_STRING | DUK_TYPE_MASK_BUFFER) ||
  57122. duk_check_type_mask(ctx, -1, DUK_TYPE_MASK_STRING | DUK_TYPE_MASK_BUFFER)) {
  57123. duk_to_string(ctx, -2);
  57124. duk_to_string(ctx, -1);
  57125. duk_concat(ctx, 2); /* [... s1 s2] -> [... s1+s2] */
  57126. duk_replace(ctx, (duk_idx_t) idx_z); /* side effects */
  57127. } else {
  57128. duk_double_t d1, d2;
  57129. d1 = duk_to_number(ctx, -2);
  57130. d2 = duk_to_number(ctx, -1);
  57131. DUK_ASSERT(duk_is_number(ctx, -2));
  57132. DUK_ASSERT(duk_is_number(ctx, -1));
  57133. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d1);
  57134. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2);
  57135. du.d = d1 + d2;
  57136. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  57137. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  57138. duk_pop_2(ctx);
  57139. duk_push_number(ctx, du.d);
  57140. duk_replace(ctx, (duk_idx_t) idx_z); /* side effects */
  57141. }
  57142. }
  57143. DUK_LOCAL void duk__vm_arith_binary_op(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_idx_t idx_z, duk_small_uint_fast_t opcode) {
  57144. /*
  57145. * Arithmetic operations other than '+' have number-only semantics
  57146. * and are implemented here. The separate switch-case here means a
  57147. * "double dispatch" of the arithmetic opcode, but saves code space.
  57148. *
  57149. * E5 Sections 11.5, 11.5.1, 11.5.2, 11.5.3, 11.6, 11.6.1, 11.6.2, 11.6.3.
  57150. */
  57151. duk_context *ctx = (duk_context *) thr;
  57152. duk_tval *tv_z;
  57153. duk_double_t d1, d2;
  57154. duk_double_union du;
  57155. DUK_ASSERT(thr != NULL);
  57156. DUK_ASSERT(ctx != NULL);
  57157. DUK_ASSERT(tv_x != NULL); /* may be reg or const */
  57158. DUK_ASSERT(tv_y != NULL); /* may be reg or const */
  57159. DUK_ASSERT_DISABLE(idx_z >= 0); /* unsigned */
  57160. DUK_ASSERT((duk_uint_t) idx_z < (duk_uint_t) duk_get_top(ctx));
  57161. #if defined(DUK_USE_FASTINT)
  57162. if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) {
  57163. duk_int64_t v1, v2, v3;
  57164. duk_int32_t v3_hi;
  57165. v1 = DUK_TVAL_GET_FASTINT(tv_x);
  57166. v2 = DUK_TVAL_GET_FASTINT(tv_y);
  57167. switch (opcode) {
  57168. case DUK_OP_SUB: {
  57169. v3 = v1 - v2;
  57170. break;
  57171. }
  57172. case DUK_OP_MUL: {
  57173. /* Must ensure result is 64-bit (no overflow); a
  57174. * simple and sufficient fast path is to allow only
  57175. * 32-bit inputs. Avoid zero inputs to avoid
  57176. * negative zero issues (-1 * 0 = -0, for instance).
  57177. */
  57178. if (v1 >= -0x80000000LL && v1 <= 0x7fffffffLL && v1 != 0 &&
  57179. v2 >= -0x80000000LL && v2 <= 0x7fffffffLL && v2 != 0) {
  57180. v3 = v1 * v2;
  57181. } else {
  57182. goto skip_fastint;
  57183. }
  57184. break;
  57185. }
  57186. case DUK_OP_DIV: {
  57187. /* Don't allow a zero divisor. Fast path check by
  57188. * "verifying" with multiplication. Also avoid zero
  57189. * dividend to avoid negative zero issues (0 / -1 = -0
  57190. * for instance).
  57191. */
  57192. if (v1 == 0 || v2 == 0) {
  57193. goto skip_fastint;
  57194. }
  57195. v3 = v1 / v2;
  57196. if (v3 * v2 != v1) {
  57197. goto skip_fastint;
  57198. }
  57199. break;
  57200. }
  57201. case DUK_OP_MOD: {
  57202. /* Don't allow a zero divisor. Restrict both v1 and
  57203. * v2 to positive values to avoid compiler specific
  57204. * behavior.
  57205. */
  57206. if (v1 < 1 || v2 < 1) {
  57207. goto skip_fastint;
  57208. }
  57209. v3 = v1 % v2;
  57210. DUK_ASSERT(v3 >= 0);
  57211. DUK_ASSERT(v3 < v2);
  57212. DUK_ASSERT(v1 - (v1 / v2) * v2 == v3);
  57213. break;
  57214. }
  57215. default: {
  57216. DUK_UNREACHABLE();
  57217. goto skip_fastint;
  57218. }
  57219. }
  57220. v3_hi = (duk_int32_t) (v3 >> 32);
  57221. if (DUK_LIKELY(v3_hi >= -0x8000LL && v3_hi <= 0x7fffLL)) {
  57222. tv_z = thr->valstack_bottom + idx_z;
  57223. DUK_TVAL_SET_FASTINT_UPDREF(thr, tv_z, v3); /* side effects */
  57224. return;
  57225. }
  57226. /* fall through if overflow etc */
  57227. }
  57228. skip_fastint:
  57229. #endif /* DUK_USE_FASTINT */
  57230. if (DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y)) {
  57231. /* fast path */
  57232. d1 = DUK_TVAL_GET_NUMBER(tv_x);
  57233. d2 = DUK_TVAL_GET_NUMBER(tv_y);
  57234. } else {
  57235. duk_push_tval(ctx, tv_x);
  57236. duk_push_tval(ctx, tv_y);
  57237. d1 = duk_to_number(ctx, -2); /* side effects */
  57238. d2 = duk_to_number(ctx, -1);
  57239. DUK_ASSERT(duk_is_number(ctx, -2));
  57240. DUK_ASSERT(duk_is_number(ctx, -1));
  57241. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d1);
  57242. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2);
  57243. duk_pop_2(ctx);
  57244. }
  57245. switch (opcode) {
  57246. case DUK_OP_SUB: {
  57247. du.d = d1 - d2;
  57248. break;
  57249. }
  57250. case DUK_OP_MUL: {
  57251. du.d = d1 * d2;
  57252. break;
  57253. }
  57254. case DUK_OP_DIV: {
  57255. du.d = d1 / d2;
  57256. break;
  57257. }
  57258. case DUK_OP_MOD: {
  57259. du.d = duk__compute_mod(d1, d2);
  57260. break;
  57261. }
  57262. default: {
  57263. DUK_UNREACHABLE();
  57264. du.d = DUK_DOUBLE_NAN; /* should not happen */
  57265. break;
  57266. }
  57267. }
  57268. /* important to use normalized NaN with 8-byte tagged types */
  57269. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  57270. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  57271. tv_z = thr->valstack_bottom + idx_z;
  57272. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv_z, du.d); /* side effects */
  57273. }
  57274. DUK_LOCAL 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) {
  57275. /*
  57276. * Binary bitwise operations use different coercions (ToInt32, ToUint32)
  57277. * depending on the operation. We coerce the arguments first using
  57278. * ToInt32(), and then cast to an 32-bit value if necessary. Note that
  57279. * such casts must be correct even if there is no native 32-bit type
  57280. * (e.g., duk_int32_t and duk_uint32_t are 64-bit).
  57281. *
  57282. * E5 Sections 11.10, 11.7.1, 11.7.2, 11.7.3
  57283. */
  57284. duk_context *ctx = (duk_context *) thr;
  57285. duk_tval *tv_z;
  57286. duk_int32_t i1, i2, i3;
  57287. duk_uint32_t u1, u2, u3;
  57288. #if defined(DUK_USE_FASTINT)
  57289. duk_int64_t fi3;
  57290. #else
  57291. duk_double_t d3;
  57292. #endif
  57293. DUK_ASSERT(thr != NULL);
  57294. DUK_ASSERT(ctx != NULL);
  57295. DUK_ASSERT(tv_x != NULL); /* may be reg or const */
  57296. DUK_ASSERT(tv_y != NULL); /* may be reg or const */
  57297. DUK_ASSERT_DISABLE(idx_z >= 0); /* unsigned */
  57298. DUK_ASSERT((duk_uint_t) idx_z < (duk_uint_t) duk_get_top(ctx));
  57299. #if defined(DUK_USE_FASTINT)
  57300. if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) {
  57301. i1 = (duk_int32_t) DUK_TVAL_GET_FASTINT_I32(tv_x);
  57302. i2 = (duk_int32_t) DUK_TVAL_GET_FASTINT_I32(tv_y);
  57303. }
  57304. else
  57305. #endif /* DUK_USE_FASTINT */
  57306. {
  57307. duk_push_tval(ctx, tv_x);
  57308. duk_push_tval(ctx, tv_y);
  57309. i1 = duk_to_int32(ctx, -2);
  57310. i2 = duk_to_int32(ctx, -1);
  57311. duk_pop_2(ctx);
  57312. }
  57313. switch (opcode) {
  57314. case DUK_OP_BAND: {
  57315. i3 = i1 & i2;
  57316. break;
  57317. }
  57318. case DUK_OP_BOR: {
  57319. i3 = i1 | i2;
  57320. break;
  57321. }
  57322. case DUK_OP_BXOR: {
  57323. i3 = i1 ^ i2;
  57324. break;
  57325. }
  57326. case DUK_OP_BASL: {
  57327. /* Signed shift, named "arithmetic" (asl) because the result
  57328. * is signed, e.g. 4294967295 << 1 -> -2. Note that result
  57329. * must be masked.
  57330. */
  57331. u2 = ((duk_uint32_t) i2) & 0xffffffffUL;
  57332. i3 = i1 << (u2 & 0x1f); /* E5 Section 11.7.1, steps 7 and 8 */
  57333. i3 = i3 & ((duk_int32_t) 0xffffffffUL); /* Note: left shift, should mask */
  57334. break;
  57335. }
  57336. case DUK_OP_BASR: {
  57337. /* signed shift */
  57338. u2 = ((duk_uint32_t) i2) & 0xffffffffUL;
  57339. i3 = i1 >> (u2 & 0x1f); /* E5 Section 11.7.2, steps 7 and 8 */
  57340. break;
  57341. }
  57342. case DUK_OP_BLSR: {
  57343. /* unsigned shift */
  57344. u1 = ((duk_uint32_t) i1) & 0xffffffffUL;
  57345. u2 = ((duk_uint32_t) i2) & 0xffffffffUL;
  57346. /* special result value handling */
  57347. u3 = u1 >> (u2 & 0x1f); /* E5 Section 11.7.2, steps 7 and 8 */
  57348. #if defined(DUK_USE_FASTINT)
  57349. fi3 = (duk_int64_t) u3;
  57350. goto fastint_result_set;
  57351. #else
  57352. d3 = (duk_double_t) u3;
  57353. goto result_set;
  57354. #endif
  57355. }
  57356. default: {
  57357. DUK_UNREACHABLE();
  57358. i3 = 0; /* should not happen */
  57359. break;
  57360. }
  57361. }
  57362. #if defined(DUK_USE_FASTINT)
  57363. /* Result is always fastint compatible. */
  57364. /* XXX: Set 32-bit result (but must then handle signed and
  57365. * unsigned results separately).
  57366. */
  57367. fi3 = (duk_int64_t) i3;
  57368. fastint_result_set:
  57369. tv_z = thr->valstack_bottom + idx_z;
  57370. DUK_TVAL_SET_FASTINT_UPDREF(thr, tv_z, fi3); /* side effects */
  57371. #else
  57372. d3 = (duk_double_t) i3;
  57373. result_set:
  57374. DUK_ASSERT(!DUK_ISNAN(d3)); /* 'd3' is never NaN, so no need to normalize */
  57375. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d3); /* always normalized */
  57376. tv_z = thr->valstack_bottom + idx_z;
  57377. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv_z, d3); /* side effects */
  57378. #endif
  57379. }
  57380. /* In-place unary operation. */
  57381. DUK_LOCAL void duk__vm_arith_unary_op(duk_hthread *thr, duk_tval *tv_x, duk_idx_t idx_x, duk_small_uint_fast_t opcode) {
  57382. /*
  57383. * Arithmetic operations other than '+' have number-only semantics
  57384. * and are implemented here. The separate switch-case here means a
  57385. * "double dispatch" of the arithmetic opcode, but saves code space.
  57386. *
  57387. * E5 Sections 11.5, 11.5.1, 11.5.2, 11.5.3, 11.6, 11.6.1, 11.6.2, 11.6.3.
  57388. */
  57389. duk_context *ctx = (duk_context *) thr;
  57390. duk_double_t d1;
  57391. duk_double_union du;
  57392. DUK_ASSERT(thr != NULL);
  57393. DUK_ASSERT(ctx != NULL);
  57394. DUK_ASSERT(opcode == DUK_EXTRAOP_UNM || opcode == DUK_EXTRAOP_UNP);
  57395. #if defined(DUK_USE_FASTINT)
  57396. if (DUK_TVAL_IS_FASTINT(tv_x)) {
  57397. duk_int64_t v1, v2;
  57398. v1 = DUK_TVAL_GET_FASTINT(tv_x);
  57399. if (opcode == DUK_EXTRAOP_UNM) {
  57400. /* The smallest fastint is no longer 48-bit when
  57401. * negated. Positive zero becames negative zero
  57402. * (cannot be represented) when negated.
  57403. */
  57404. if (DUK_LIKELY(v1 != DUK_FASTINT_MIN && v1 != 0)) {
  57405. v2 = -v1;
  57406. DUK_TVAL_SET_FASTINT(tv_x, v2); /* no refcount changes */
  57407. return;
  57408. }
  57409. } else {
  57410. /* ToNumber() for a fastint is a no-op. */
  57411. DUK_ASSERT(opcode == DUK_EXTRAOP_UNP);
  57412. return;
  57413. }
  57414. /* fall through if overflow etc */
  57415. }
  57416. #endif /* DUK_USE_FASTINT */
  57417. if (!DUK_TVAL_IS_NUMBER(tv_x)) {
  57418. duk_to_number(ctx, idx_x); /* side effects, perform in-place */
  57419. tv_x = duk_get_tval(ctx, idx_x);
  57420. DUK_ASSERT(tv_x != NULL);
  57421. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_x));
  57422. }
  57423. d1 = DUK_TVAL_GET_NUMBER(tv_x);
  57424. if (opcode == DUK_EXTRAOP_UNM) {
  57425. du.d = -d1;
  57426. } else {
  57427. /* ToNumber() for a double is a no-op. */
  57428. DUK_ASSERT(opcode == DUK_EXTRAOP_UNP);
  57429. du.d = d1;
  57430. }
  57431. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du); /* mandatory if du.d is a NaN */
  57432. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  57433. #if defined(DUK_USE_FASTINT)
  57434. /* Unary plus is used to force a fastint check, so must include
  57435. * downgrade check.
  57436. */
  57437. DUK_TVAL_SET_NUMBER_CHKFAST(tv_x, du.d); /* no refcount changes */
  57438. #else
  57439. DUK_TVAL_SET_NUMBER(tv_x, du.d); /* no refcount changes */
  57440. #endif
  57441. }
  57442. DUK_LOCAL void duk__vm_bitwise_not(duk_hthread *thr, duk_tval *tv_x, duk_small_uint_fast_t idx_z) {
  57443. /*
  57444. * E5 Section 11.4.8
  57445. */
  57446. duk_context *ctx = (duk_context *) thr;
  57447. duk_tval *tv_z;
  57448. duk_int32_t i1, i2;
  57449. #if !defined(DUK_USE_FASTINT)
  57450. duk_double_t d2;
  57451. #endif
  57452. DUK_ASSERT(thr != NULL);
  57453. DUK_ASSERT(ctx != NULL);
  57454. DUK_ASSERT(tv_x != NULL); /* may be reg or const */
  57455. DUK_ASSERT_DISABLE(idx_z >= 0);
  57456. DUK_ASSERT((duk_uint_t) idx_z < (duk_uint_t) duk_get_top(ctx));
  57457. #if defined(DUK_USE_FASTINT)
  57458. if (DUK_TVAL_IS_FASTINT(tv_x)) {
  57459. i1 = (duk_int32_t) DUK_TVAL_GET_FASTINT_I32(tv_x);
  57460. }
  57461. else
  57462. #endif /* DUK_USE_FASTINT */
  57463. {
  57464. duk_push_tval(ctx, tv_x);
  57465. i1 = duk_to_int32(ctx, -1);
  57466. duk_pop(ctx);
  57467. }
  57468. i2 = ~i1;
  57469. #if defined(DUK_USE_FASTINT)
  57470. /* Result is always fastint compatible. */
  57471. tv_z = thr->valstack_bottom + idx_z;
  57472. DUK_TVAL_SET_FASTINT_I32_UPDREF(thr, tv_z, i2); /* side effects */
  57473. #else
  57474. d2 = (duk_double_t) i2;
  57475. DUK_ASSERT(!DUK_ISNAN(d2)); /* 'val' is never NaN, so no need to normalize */
  57476. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2); /* always normalized */
  57477. tv_z = thr->valstack_bottom + idx_z;
  57478. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv_z, d2); /* side effects */
  57479. #endif
  57480. }
  57481. DUK_LOCAL void duk__vm_logical_not(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_z) {
  57482. /*
  57483. * E5 Section 11.4.9
  57484. */
  57485. duk_bool_t res;
  57486. DUK_ASSERT(thr != NULL);
  57487. DUK_ASSERT(tv_x != NULL); /* may be reg or const */
  57488. DUK_ASSERT(tv_z != NULL); /* reg */
  57489. DUK_UNREF(thr); /* w/o refcounts */
  57490. /* ToBoolean() does not require any operations with side effects so
  57491. * we can do it efficiently. For footprint it would be better to use
  57492. * duk_js_toboolean() and then push+replace to the result slot.
  57493. */
  57494. res = duk_js_toboolean(tv_x); /* does not modify tv_x */
  57495. DUK_ASSERT(res == 0 || res == 1);
  57496. res ^= 1;
  57497. DUK_TVAL_SET_BOOLEAN_UPDREF(thr, tv_z, res); /* side effects */
  57498. }
  57499. /*
  57500. * Longjmp and other control flow transfer for the bytecode executor.
  57501. *
  57502. * The longjmp handler can handle all longjmp types: error, yield, and
  57503. * resume (pseudotypes are never actually thrown).
  57504. *
  57505. * Error policy for longjmp: should not ordinarily throw errors; if errors
  57506. * occur (e.g. due to out-of-memory) they bubble outwards rather than being
  57507. * handled recursively.
  57508. */
  57509. #define DUK__LONGJMP_RESTART 0 /* state updated, restart bytecode execution */
  57510. #define DUK__LONGJMP_RETHROW 1 /* exit bytecode executor by rethrowing an error to caller */
  57511. #define DUK__RETHAND_RESTART 0 /* state updated, restart bytecode execution */
  57512. #define DUK__RETHAND_FINISHED 1 /* exit bytecode execution with return value */
  57513. /* XXX: optimize reconfig valstack operations so that resize, clamp, and setting
  57514. * top are combined into one pass.
  57515. */
  57516. /* Reconfigure value stack for return to an Ecmascript function at 'act_idx'. */
  57517. DUK_LOCAL void duk__reconfig_valstack_ecma_return(duk_hthread *thr, duk_size_t act_idx) {
  57518. duk_activation *act;
  57519. duk_hcompiledfunction *h_func;
  57520. duk_idx_t clamp_top;
  57521. DUK_ASSERT(thr != NULL);
  57522. DUK_ASSERT_DISABLE(act_idx >= 0); /* unsigned */
  57523. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + act_idx) != NULL);
  57524. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + act_idx)));
  57525. DUK_ASSERT_DISABLE(thr->callstack[act_idx].idx_retval >= 0); /* unsigned */
  57526. /* Clamp so that values at 'clamp_top' and above are wiped and won't
  57527. * retain reachable garbage. Then extend to 'nregs' because we're
  57528. * returning to an Ecmascript function.
  57529. */
  57530. act = thr->callstack + act_idx;
  57531. h_func = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act);
  57532. thr->valstack_bottom = thr->valstack + act->idx_bottom;
  57533. DUK_ASSERT(act->idx_retval >= act->idx_bottom);
  57534. clamp_top = (duk_idx_t) (act->idx_retval - act->idx_bottom + 1); /* +1 = one retval */
  57535. duk_set_top((duk_context *) thr, clamp_top);
  57536. act = NULL;
  57537. (void) duk_valstack_resize_raw((duk_context *) thr,
  57538. (thr->valstack_bottom - thr->valstack) + /* bottom of current func */
  57539. h_func->nregs + /* reg count */
  57540. DUK_VALSTACK_INTERNAL_EXTRA, /* + spare */
  57541. DUK_VSRESIZE_FLAG_SHRINK | /* flags */
  57542. 0 /* no compact */ |
  57543. DUK_VSRESIZE_FLAG_THROW);
  57544. duk_set_top((duk_context *) thr, h_func->nregs);
  57545. }
  57546. DUK_LOCAL void duk__reconfig_valstack_ecma_catcher(duk_hthread *thr, duk_size_t act_idx, duk_size_t cat_idx) {
  57547. duk_activation *act;
  57548. duk_catcher *cat;
  57549. duk_hcompiledfunction *h_func;
  57550. duk_idx_t clamp_top;
  57551. DUK_ASSERT(thr != NULL);
  57552. DUK_ASSERT_DISABLE(act_idx >= 0); /* unsigned */
  57553. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + act_idx) != NULL);
  57554. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + act_idx)));
  57555. DUK_ASSERT_DISABLE(thr->callstack[act_idx].idx_retval >= 0); /* unsigned */
  57556. act = thr->callstack + act_idx;
  57557. cat = thr->catchstack + cat_idx;
  57558. h_func = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act);
  57559. thr->valstack_bottom = thr->valstack + act->idx_bottom;
  57560. DUK_ASSERT(cat->idx_base >= act->idx_bottom);
  57561. clamp_top = (duk_idx_t) (cat->idx_base - act->idx_bottom + 2); /* +2 = catcher value, catcher lj_type */
  57562. duk_set_top((duk_context *) thr, clamp_top);
  57563. act = NULL;
  57564. cat = NULL;
  57565. (void) duk_valstack_resize_raw((duk_context *) thr,
  57566. (thr->valstack_bottom - thr->valstack) + /* bottom of current func */
  57567. h_func->nregs + /* reg count */
  57568. DUK_VALSTACK_INTERNAL_EXTRA, /* + spare */
  57569. DUK_VSRESIZE_FLAG_SHRINK | /* flags */
  57570. 0 /* no compact */ |
  57571. DUK_VSRESIZE_FLAG_THROW);
  57572. duk_set_top((duk_context *) thr, h_func->nregs);
  57573. }
  57574. /* Set catcher regs: idx_base+0 = value, idx_base+1 = lj_type. */
  57575. DUK_LOCAL void duk__set_catcher_regs(duk_hthread *thr, duk_size_t cat_idx, duk_tval *tv_val_unstable, duk_small_uint_t lj_type) {
  57576. duk_tval *tv1;
  57577. DUK_ASSERT(thr != NULL);
  57578. DUK_ASSERT(tv_val_unstable != NULL);
  57579. tv1 = thr->valstack + thr->catchstack[cat_idx].idx_base;
  57580. DUK_ASSERT(tv1 < thr->valstack_top);
  57581. DUK_TVAL_SET_TVAL_UPDREF(thr, tv1, tv_val_unstable); /* side effects */
  57582. tv1 = thr->valstack + thr->catchstack[cat_idx].idx_base + 1;
  57583. DUK_ASSERT(tv1 < thr->valstack_top);
  57584. #if defined(DUK_USE_FASTINT)
  57585. DUK_TVAL_SET_FASTINT_U32_UPDREF(thr, tv1, (duk_uint32_t) lj_type); /* side effects */
  57586. #else
  57587. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv1, (duk_double_t) lj_type); /* side effects */
  57588. #endif
  57589. }
  57590. DUK_LOCAL void duk__handle_catch(duk_hthread *thr, duk_size_t cat_idx, duk_tval *tv_val_unstable, duk_small_uint_t lj_type) {
  57591. duk_context *ctx;
  57592. duk_activation *act;
  57593. DUK_ASSERT(thr != NULL);
  57594. DUK_ASSERT(tv_val_unstable != NULL);
  57595. ctx = (duk_context *) thr;
  57596. duk__set_catcher_regs(thr, cat_idx, tv_val_unstable, lj_type);
  57597. duk_hthread_catchstack_unwind(thr, cat_idx + 1);
  57598. duk_hthread_callstack_unwind(thr, thr->catchstack[cat_idx].callstack_index + 1);
  57599. DUK_ASSERT(thr->callstack_top >= 1);
  57600. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1) != NULL);
  57601. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1)));
  57602. duk__reconfig_valstack_ecma_catcher(thr, thr->callstack_top - 1, cat_idx);
  57603. DUK_ASSERT(thr->callstack_top >= 1);
  57604. act = thr->callstack + thr->callstack_top - 1;
  57605. act->curr_pc = thr->catchstack[cat_idx].pc_base + 0; /* +0 = catch */
  57606. act = NULL;
  57607. /*
  57608. * If entering a 'catch' block which requires an automatic
  57609. * catch variable binding, create the lexical environment.
  57610. *
  57611. * The binding is mutable (= writable) but not deletable.
  57612. * Step 4 for the catch production in E5 Section 12.14;
  57613. * no value is given for CreateMutableBinding 'D' argument,
  57614. * which implies the binding is not deletable.
  57615. */
  57616. if (DUK_CAT_HAS_CATCH_BINDING_ENABLED(&thr->catchstack[cat_idx])) {
  57617. duk_hobject *new_env;
  57618. duk_hobject *act_lex_env;
  57619. DUK_DDD(DUK_DDDPRINT("catcher has an automatic catch binding"));
  57620. /* Note: 'act' is dangerous here because it may get invalidate at many
  57621. * points, so we re-lookup it multiple times.
  57622. */
  57623. DUK_ASSERT(thr->callstack_top >= 1);
  57624. act = thr->callstack + thr->callstack_top - 1;
  57625. if (act->lex_env == NULL) {
  57626. DUK_ASSERT(act->var_env == NULL);
  57627. DUK_DDD(DUK_DDDPRINT("delayed environment initialization"));
  57628. /* this may have side effects, so re-lookup act */
  57629. duk_js_init_activation_environment_records_delayed(thr, act);
  57630. act = thr->callstack + thr->callstack_top - 1;
  57631. }
  57632. DUK_ASSERT(act->lex_env != NULL);
  57633. DUK_ASSERT(act->var_env != NULL);
  57634. DUK_ASSERT(DUK_ACT_GET_FUNC(act) != NULL);
  57635. DUK_UNREF(act); /* unreferenced without assertions */
  57636. act = thr->callstack + thr->callstack_top - 1;
  57637. act_lex_env = act->lex_env;
  57638. act = NULL; /* invalidated */
  57639. (void) duk_push_object_helper_proto(ctx,
  57640. DUK_HOBJECT_FLAG_EXTENSIBLE |
  57641. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV),
  57642. act_lex_env);
  57643. new_env = duk_get_hobject(ctx, -1);
  57644. DUK_ASSERT(new_env != NULL);
  57645. DUK_DDD(DUK_DDDPRINT("new_env allocated: %!iO", (duk_heaphdr *) new_env));
  57646. /* Note: currently the catch binding is handled without a register
  57647. * binding because we don't support dynamic register bindings (they
  57648. * must be fixed for an entire function). So, there is no need to
  57649. * record regbases etc.
  57650. */
  57651. DUK_ASSERT(thr->catchstack[cat_idx].h_varname != NULL);
  57652. duk_push_hstring(ctx, thr->catchstack[cat_idx].h_varname);
  57653. duk_push_tval(ctx, thr->valstack + thr->catchstack[cat_idx].idx_base);
  57654. duk_xdef_prop(ctx, -3, DUK_PROPDESC_FLAGS_W); /* writable, not configurable */
  57655. act = thr->callstack + thr->callstack_top - 1;
  57656. act->lex_env = new_env;
  57657. DUK_HOBJECT_INCREF(thr, new_env); /* reachable through activation */
  57658. DUK_CAT_SET_LEXENV_ACTIVE(&thr->catchstack[cat_idx]);
  57659. duk_pop(ctx);
  57660. DUK_DDD(DUK_DDDPRINT("new_env finished: %!iO", (duk_heaphdr *) new_env));
  57661. }
  57662. DUK_CAT_CLEAR_CATCH_ENABLED(&thr->catchstack[cat_idx]);
  57663. }
  57664. DUK_LOCAL void duk__handle_finally(duk_hthread *thr, duk_size_t cat_idx, duk_tval *tv_val_unstable, duk_small_uint_t lj_type) {
  57665. duk_activation *act;
  57666. DUK_ASSERT(thr != NULL);
  57667. DUK_ASSERT(tv_val_unstable != NULL);
  57668. duk__set_catcher_regs(thr, cat_idx, tv_val_unstable, lj_type);
  57669. duk_hthread_catchstack_unwind(thr, cat_idx + 1); /* cat_idx catcher is kept, even for finally */
  57670. duk_hthread_callstack_unwind(thr, thr->catchstack[cat_idx].callstack_index + 1);
  57671. DUK_ASSERT(thr->callstack_top >= 1);
  57672. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1) != NULL);
  57673. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1)));
  57674. duk__reconfig_valstack_ecma_catcher(thr, thr->callstack_top - 1, cat_idx);
  57675. DUK_ASSERT(thr->callstack_top >= 1);
  57676. act = thr->callstack + thr->callstack_top - 1;
  57677. act->curr_pc = thr->catchstack[cat_idx].pc_base + 1; /* +1 = finally */
  57678. act = NULL;
  57679. DUK_CAT_CLEAR_FINALLY_ENABLED(&thr->catchstack[cat_idx]);
  57680. }
  57681. DUK_LOCAL void duk__handle_label(duk_hthread *thr, duk_size_t cat_idx, duk_small_uint_t lj_type) {
  57682. duk_activation *act;
  57683. DUK_ASSERT(thr != NULL);
  57684. DUK_ASSERT(thr->callstack_top >= 1);
  57685. act = thr->callstack + thr->callstack_top - 1;
  57686. DUK_ASSERT(DUK_ACT_GET_FUNC(act) != NULL);
  57687. DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(act)));
  57688. /* +0 = break, +1 = continue */
  57689. act->curr_pc = thr->catchstack[cat_idx].pc_base + (lj_type == DUK_LJ_TYPE_CONTINUE ? 1 : 0);
  57690. act = NULL; /* invalidated */
  57691. duk_hthread_catchstack_unwind(thr, cat_idx + 1); /* keep label catcher */
  57692. /* no need to unwind callstack */
  57693. /* valstack should not need changes */
  57694. #if defined(DUK_USE_ASSERTIONS)
  57695. DUK_ASSERT(thr->callstack_top >= 1);
  57696. act = thr->callstack + thr->callstack_top - 1;
  57697. DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) ==
  57698. (duk_size_t) ((duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act))->nregs);
  57699. #endif
  57700. }
  57701. /* Called for handling both a longjmp() with type DUK_LJ_TYPE_YIELD and
  57702. * when a RETURN opcode terminates a thread and yields to the resumer.
  57703. */
  57704. DUK_LOCAL void duk__handle_yield(duk_hthread *thr, duk_hthread *resumer, duk_size_t act_idx, duk_tval *tv_val_unstable) {
  57705. duk_tval *tv1;
  57706. DUK_ASSERT(thr != NULL);
  57707. DUK_ASSERT(resumer != NULL);
  57708. DUK_ASSERT(tv_val_unstable != NULL);
  57709. DUK_ASSERT(DUK_ACT_GET_FUNC(resumer->callstack + act_idx) != NULL);
  57710. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(resumer->callstack + act_idx))); /* resume caller must be an ecmascript func */
  57711. tv1 = resumer->valstack + resumer->callstack[act_idx].idx_retval; /* return value from Duktape.Thread.resume() */
  57712. DUK_TVAL_SET_TVAL_UPDREF(thr, tv1, tv_val_unstable); /* side effects */
  57713. duk_hthread_callstack_unwind(resumer, act_idx + 1); /* unwind to 'resume' caller */
  57714. /* no need to unwind catchstack */
  57715. duk__reconfig_valstack_ecma_return(resumer, act_idx);
  57716. /* caller must change active thread, and set thr->resumer to NULL */
  57717. }
  57718. DUK_LOCAL
  57719. duk_small_uint_t duk__handle_longjmp(duk_hthread *thr,
  57720. duk_hthread *entry_thread,
  57721. duk_size_t entry_callstack_top) {
  57722. duk_size_t entry_callstack_index;
  57723. duk_small_uint_t retval = DUK__LONGJMP_RESTART;
  57724. DUK_ASSERT(thr != NULL);
  57725. DUK_ASSERT(entry_thread != NULL);
  57726. DUK_ASSERT(entry_callstack_top > 0); /* guarantees entry_callstack_top - 1 >= 0 */
  57727. entry_callstack_index = entry_callstack_top - 1;
  57728. /* 'thr' is the current thread, as no-one resumes except us and we
  57729. * switch 'thr' in that case.
  57730. */
  57731. DUK_ASSERT(thr == thr->heap->curr_thread);
  57732. /*
  57733. * (Re)try handling the longjmp.
  57734. *
  57735. * A longjmp handler may convert the longjmp to a different type and
  57736. * "virtually" rethrow by goto'ing to 'check_longjmp'. Before the goto,
  57737. * the following must be updated:
  57738. * - the heap 'lj' state
  57739. * - 'thr' must reflect the "throwing" thread
  57740. */
  57741. check_longjmp:
  57742. DUK_DD(DUK_DDPRINT("handling longjmp: type=%ld, value1=%!T, value2=%!T, iserror=%ld",
  57743. (long) thr->heap->lj.type,
  57744. (duk_tval *) &thr->heap->lj.value1,
  57745. (duk_tval *) &thr->heap->lj.value2,
  57746. (long) thr->heap->lj.iserror));
  57747. switch (thr->heap->lj.type) {
  57748. case DUK_LJ_TYPE_RESUME: {
  57749. /*
  57750. * Note: lj.value1 is 'value', lj.value2 is 'resumee'.
  57751. * This differs from YIELD.
  57752. */
  57753. duk_tval *tv;
  57754. duk_tval *tv2;
  57755. duk_size_t act_idx;
  57756. duk_hthread *resumee;
  57757. /* duk_bi_duk_object_yield() and duk_bi_duk_object_resume() ensure all of these are met */
  57758. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING); /* unchanged by Duktape.Thread.resume() */
  57759. DUK_ASSERT(thr->callstack_top >= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */
  57760. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1) != NULL &&
  57761. DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1)) &&
  57762. ((duk_hnativefunction *) DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1))->func == duk_bi_thread_resume);
  57763. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2) != NULL &&
  57764. DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2))); /* an Ecmascript function */
  57765. DUK_ASSERT_DISABLE((thr->callstack + thr->callstack_top - 2)->idx_retval >= 0); /* unsigned */
  57766. tv = &thr->heap->lj.value2; /* resumee */
  57767. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  57768. DUK_ASSERT(DUK_TVAL_GET_OBJECT(tv) != NULL);
  57769. DUK_ASSERT(DUK_HOBJECT_IS_THREAD(DUK_TVAL_GET_OBJECT(tv)));
  57770. resumee = (duk_hthread *) DUK_TVAL_GET_OBJECT(tv);
  57771. DUK_ASSERT(resumee != NULL);
  57772. DUK_ASSERT(resumee->resumer == NULL);
  57773. DUK_ASSERT(resumee->state == DUK_HTHREAD_STATE_INACTIVE ||
  57774. resumee->state == DUK_HTHREAD_STATE_YIELDED); /* checked by Duktape.Thread.resume() */
  57775. DUK_ASSERT(resumee->state != DUK_HTHREAD_STATE_YIELDED ||
  57776. resumee->callstack_top >= 2); /* YIELDED: Ecmascript activation + Duktape.Thread.yield() activation */
  57777. DUK_ASSERT(resumee->state != DUK_HTHREAD_STATE_YIELDED ||
  57778. (DUK_ACT_GET_FUNC(resumee->callstack + resumee->callstack_top - 1) != NULL &&
  57779. DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(resumee->callstack + resumee->callstack_top - 1)) &&
  57780. ((duk_hnativefunction *) DUK_ACT_GET_FUNC(resumee->callstack + resumee->callstack_top - 1))->func == duk_bi_thread_yield));
  57781. DUK_ASSERT(resumee->state != DUK_HTHREAD_STATE_YIELDED ||
  57782. (DUK_ACT_GET_FUNC(resumee->callstack + resumee->callstack_top - 2) != NULL &&
  57783. DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(resumee->callstack + resumee->callstack_top - 2)))); /* an Ecmascript function */
  57784. DUK_ASSERT_DISABLE(resumee->state != DUK_HTHREAD_STATE_YIELDED ||
  57785. (resumee->callstack + resumee->callstack_top - 2)->idx_retval >= 0); /* idx_retval unsigned */
  57786. DUK_ASSERT(resumee->state != DUK_HTHREAD_STATE_INACTIVE ||
  57787. resumee->callstack_top == 0); /* INACTIVE: no activation, single function value on valstack */
  57788. DUK_ASSERT(resumee->state != DUK_HTHREAD_STATE_INACTIVE ||
  57789. (resumee->valstack_top == resumee->valstack + 1 &&
  57790. DUK_TVAL_IS_OBJECT(resumee->valstack_top - 1) &&
  57791. DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_TVAL_GET_OBJECT(resumee->valstack_top - 1))));
  57792. if (thr->heap->lj.iserror) {
  57793. /*
  57794. * Throw the error in the resumed thread's context; the
  57795. * error value is pushed onto the resumee valstack.
  57796. *
  57797. * Note: the callstack of the target may empty in this case
  57798. * too (i.e. the target thread has never been resumed). The
  57799. * value stack will contain the initial function in that case,
  57800. * which we simply ignore.
  57801. */
  57802. resumee->resumer = thr;
  57803. resumee->state = DUK_HTHREAD_STATE_RUNNING;
  57804. thr->state = DUK_HTHREAD_STATE_RESUMED;
  57805. DUK_HEAP_SWITCH_THREAD(thr->heap, resumee);
  57806. thr = resumee;
  57807. thr->heap->lj.type = DUK_LJ_TYPE_THROW;
  57808. /* thr->heap->lj.value1 is already the value to throw */
  57809. /* thr->heap->lj.value2 is 'thread', will be wiped out at the end */
  57810. DUK_ASSERT(thr->heap->lj.iserror); /* already set */
  57811. DUK_DD(DUK_DDPRINT("-> resume with an error, converted to a throw in the resumee, propagate"));
  57812. goto check_longjmp;
  57813. } else if (resumee->state == DUK_HTHREAD_STATE_YIELDED) {
  57814. act_idx = resumee->callstack_top - 2; /* Ecmascript function */
  57815. DUK_ASSERT_DISABLE(resumee->callstack[act_idx].idx_retval >= 0); /* unsigned */
  57816. tv = resumee->valstack + resumee->callstack[act_idx].idx_retval; /* return value from Duktape.Thread.yield() */
  57817. DUK_ASSERT(tv >= resumee->valstack && tv < resumee->valstack_top);
  57818. tv2 = &thr->heap->lj.value1;
  57819. DUK_TVAL_SET_TVAL_UPDREF(thr, tv, tv2); /* side effects */
  57820. duk_hthread_callstack_unwind(resumee, act_idx + 1); /* unwind to 'yield' caller */
  57821. /* no need to unwind catchstack */
  57822. duk__reconfig_valstack_ecma_return(resumee, act_idx);
  57823. resumee->resumer = thr;
  57824. resumee->state = DUK_HTHREAD_STATE_RUNNING;
  57825. thr->state = DUK_HTHREAD_STATE_RESUMED;
  57826. DUK_HEAP_SWITCH_THREAD(thr->heap, resumee);
  57827. #if 0
  57828. thr = resumee; /* not needed, as we exit right away */
  57829. #endif
  57830. DUK_DD(DUK_DDPRINT("-> resume with a value, restart execution in resumee"));
  57831. retval = DUK__LONGJMP_RESTART;
  57832. goto wipe_and_return;
  57833. } else {
  57834. duk_small_uint_t call_flags;
  57835. duk_bool_t setup_rc;
  57836. /* resumee: [... initial_func] (currently actually: [initial_func]) */
  57837. duk_push_undefined((duk_context *) resumee);
  57838. tv = &thr->heap->lj.value1;
  57839. duk_push_tval((duk_context *) resumee, tv);
  57840. /* resumee: [... initial_func undefined(= this) resume_value ] */
  57841. call_flags = DUK_CALL_FLAG_IS_RESUME; /* is resume, not a tail call */
  57842. setup_rc = duk_handle_ecma_call_setup(resumee,
  57843. 1, /* num_stack_args */
  57844. call_flags); /* call_flags */
  57845. if (setup_rc == 0) {
  57846. /* Shouldn't happen but check anyway. */
  57847. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR);
  57848. }
  57849. resumee->resumer = thr;
  57850. resumee->state = DUK_HTHREAD_STATE_RUNNING;
  57851. thr->state = DUK_HTHREAD_STATE_RESUMED;
  57852. DUK_HEAP_SWITCH_THREAD(thr->heap, resumee);
  57853. #if 0
  57854. thr = resumee; /* not needed, as we exit right away */
  57855. #endif
  57856. DUK_DD(DUK_DDPRINT("-> resume with a value, restart execution in resumee"));
  57857. retval = DUK__LONGJMP_RESTART;
  57858. goto wipe_and_return;
  57859. }
  57860. DUK_UNREACHABLE();
  57861. break; /* never here */
  57862. }
  57863. case DUK_LJ_TYPE_YIELD: {
  57864. /*
  57865. * Currently only allowed only if yielding thread has only
  57866. * Ecmascript activations (except for the Duktape.Thread.yield()
  57867. * call at the callstack top) and none of them constructor
  57868. * calls.
  57869. *
  57870. * This excludes the 'entry' thread which will always have
  57871. * a preventcount > 0.
  57872. */
  57873. duk_hthread *resumer;
  57874. /* duk_bi_duk_object_yield() and duk_bi_duk_object_resume() ensure all of these are met */
  57875. DUK_ASSERT(thr != entry_thread); /* Duktape.Thread.yield() should prevent */
  57876. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING); /* unchanged from Duktape.Thread.yield() */
  57877. DUK_ASSERT(thr->callstack_top >= 2); /* Ecmascript activation + Duktape.Thread.yield() activation */
  57878. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1) != NULL &&
  57879. DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1)) &&
  57880. ((duk_hnativefunction *) DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1))->func == duk_bi_thread_yield);
  57881. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2) != NULL &&
  57882. DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2))); /* an Ecmascript function */
  57883. DUK_ASSERT_DISABLE((thr->callstack + thr->callstack_top - 2)->idx_retval >= 0); /* unsigned */
  57884. resumer = thr->resumer;
  57885. DUK_ASSERT(resumer != NULL);
  57886. DUK_ASSERT(resumer->state == DUK_HTHREAD_STATE_RESUMED); /* written by a previous RESUME handling */
  57887. DUK_ASSERT(resumer->callstack_top >= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */
  57888. DUK_ASSERT(DUK_ACT_GET_FUNC(resumer->callstack + resumer->callstack_top - 1) != NULL &&
  57889. DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(resumer->callstack + resumer->callstack_top - 1)) &&
  57890. ((duk_hnativefunction *) DUK_ACT_GET_FUNC(resumer->callstack + resumer->callstack_top - 1))->func == duk_bi_thread_resume);
  57891. DUK_ASSERT(DUK_ACT_GET_FUNC(resumer->callstack + resumer->callstack_top - 2) != NULL &&
  57892. DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(resumer->callstack + resumer->callstack_top - 2))); /* an Ecmascript function */
  57893. DUK_ASSERT_DISABLE((resumer->callstack + resumer->callstack_top - 2)->idx_retval >= 0); /* unsigned */
  57894. if (thr->heap->lj.iserror) {
  57895. thr->state = DUK_HTHREAD_STATE_YIELDED;
  57896. thr->resumer = NULL;
  57897. resumer->state = DUK_HTHREAD_STATE_RUNNING;
  57898. DUK_HEAP_SWITCH_THREAD(thr->heap, resumer);
  57899. thr = resumer;
  57900. thr->heap->lj.type = DUK_LJ_TYPE_THROW;
  57901. /* lj.value1 is already set */
  57902. DUK_ASSERT(thr->heap->lj.iserror); /* already set */
  57903. DUK_DD(DUK_DDPRINT("-> yield an error, converted to a throw in the resumer, propagate"));
  57904. goto check_longjmp;
  57905. } else {
  57906. duk__handle_yield(thr, resumer, resumer->callstack_top - 2, &thr->heap->lj.value1);
  57907. thr->state = DUK_HTHREAD_STATE_YIELDED;
  57908. thr->resumer = NULL;
  57909. resumer->state = DUK_HTHREAD_STATE_RUNNING;
  57910. DUK_HEAP_SWITCH_THREAD(thr->heap, resumer);
  57911. #if 0
  57912. thr = resumer; /* not needed, as we exit right away */
  57913. #endif
  57914. DUK_DD(DUK_DDPRINT("-> yield a value, restart execution in resumer"));
  57915. retval = DUK__LONGJMP_RESTART;
  57916. goto wipe_and_return;
  57917. }
  57918. DUK_UNREACHABLE();
  57919. break; /* never here */
  57920. }
  57921. case DUK_LJ_TYPE_THROW: {
  57922. /*
  57923. * Three possible outcomes:
  57924. * * A try or finally catcher is found => resume there.
  57925. * (or)
  57926. * * The error propagates to the bytecode executor entry
  57927. * level (and we're in the entry thread) => rethrow
  57928. * with a new longjmp(), after restoring the previous
  57929. * catchpoint.
  57930. * * The error is not caught in the current thread, so
  57931. * the thread finishes with an error. This works like
  57932. * a yielded error, except that the thread is finished
  57933. * and can no longer be resumed. (There is always a
  57934. * resumer in this case.)
  57935. *
  57936. * Note: until we hit the entry level, there can only be
  57937. * Ecmascript activations.
  57938. */
  57939. duk_catcher *cat;
  57940. duk_hthread *resumer;
  57941. cat = thr->catchstack + thr->catchstack_top - 1;
  57942. while (cat >= thr->catchstack) {
  57943. if (thr == entry_thread &&
  57944. cat->callstack_index < entry_callstack_index) {
  57945. /* entry level reached */
  57946. break;
  57947. }
  57948. if (DUK_CAT_HAS_CATCH_ENABLED(cat)) {
  57949. DUK_ASSERT(DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF);
  57950. duk__handle_catch(thr,
  57951. cat - thr->catchstack,
  57952. &thr->heap->lj.value1,
  57953. DUK_LJ_TYPE_THROW);
  57954. DUK_DD(DUK_DDPRINT("-> throw caught by a 'catch' clause, restart execution"));
  57955. retval = DUK__LONGJMP_RESTART;
  57956. goto wipe_and_return;
  57957. }
  57958. if (DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
  57959. DUK_ASSERT(DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF);
  57960. DUK_ASSERT(!DUK_CAT_HAS_CATCH_ENABLED(cat));
  57961. duk__handle_finally(thr,
  57962. cat - thr->catchstack,
  57963. &thr->heap->lj.value1,
  57964. DUK_LJ_TYPE_THROW);
  57965. DUK_DD(DUK_DDPRINT("-> throw caught by a 'finally' clause, restart execution"));
  57966. retval = DUK__LONGJMP_RESTART;
  57967. goto wipe_and_return;
  57968. }
  57969. cat--;
  57970. }
  57971. if (thr == entry_thread) {
  57972. /* not caught by anything before entry level; rethrow and let the
  57973. * final catcher unwind everything
  57974. */
  57975. #if 0
  57976. duk_hthread_catchstack_unwind(thr, (cat - thr->catchstack) + 1); /* leave 'cat' as top catcher (also works if catchstack exhausted) */
  57977. duk_hthread_callstack_unwind(thr, entry_callstack_index + 1);
  57978. #endif
  57979. DUK_D(DUK_DPRINT("-> throw propagated up to entry level, rethrow and exit bytecode executor"));
  57980. retval = DUK__LONGJMP_RETHROW;
  57981. goto just_return;
  57982. /* Note: MUST NOT wipe_and_return here, as heap->lj must remain intact */
  57983. }
  57984. DUK_DD(DUK_DDPRINT("-> throw not caught by current thread, yield error to resumer and recheck longjmp"));
  57985. /* not caught by current thread, thread terminates (yield error to resumer);
  57986. * note that this may cause a cascade if the resumer terminates with an uncaught
  57987. * exception etc (this is OK, but needs careful testing)
  57988. */
  57989. DUK_ASSERT(thr->resumer != NULL);
  57990. DUK_ASSERT(thr->resumer->callstack_top >= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */
  57991. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1) != NULL &&
  57992. DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1)) &&
  57993. ((duk_hnativefunction *) DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1))->func == duk_bi_thread_resume); /* Duktape.Thread.resume() */
  57994. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 2) != NULL &&
  57995. DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 2))); /* an Ecmascript function */
  57996. resumer = thr->resumer;
  57997. /* reset longjmp */
  57998. DUK_ASSERT(thr->heap->lj.type == DUK_LJ_TYPE_THROW); /* already set */
  57999. /* lj.value1 already set */
  58000. duk_hthread_terminate(thr); /* updates thread state, minimizes its allocations */
  58001. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_TERMINATED);
  58002. thr->resumer = NULL;
  58003. resumer->state = DUK_HTHREAD_STATE_RUNNING;
  58004. DUK_HEAP_SWITCH_THREAD(thr->heap, resumer);
  58005. thr = resumer;
  58006. goto check_longjmp;
  58007. }
  58008. case DUK_LJ_TYPE_BREAK: /* pseudotypes, not used in actual longjmps */
  58009. case DUK_LJ_TYPE_CONTINUE:
  58010. case DUK_LJ_TYPE_RETURN:
  58011. case DUK_LJ_TYPE_NORMAL:
  58012. default: {
  58013. /* should never happen, but be robust */
  58014. DUK_D(DUK_DPRINT("caught unknown longjmp type %ld, treat as internal error", (long) thr->heap->lj.type));
  58015. goto convert_to_internal_error;
  58016. }
  58017. } /* end switch */
  58018. DUK_UNREACHABLE();
  58019. wipe_and_return:
  58020. /* this is not strictly necessary, but helps debugging */
  58021. thr->heap->lj.type = DUK_LJ_TYPE_UNKNOWN;
  58022. thr->heap->lj.iserror = 0;
  58023. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, &thr->heap->lj.value1); /* side effects */
  58024. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, &thr->heap->lj.value2); /* side effects */
  58025. just_return:
  58026. return retval;
  58027. convert_to_internal_error:
  58028. /* This could also be thrown internally (set the error, goto check_longjmp),
  58029. * but it's better for internal errors to bubble outwards so that we won't
  58030. * infinite loop in this catchpoint.
  58031. */
  58032. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR_EXEC_LONGJMP);
  58033. DUK_UNREACHABLE();
  58034. return retval;
  58035. }
  58036. /* Handle a BREAK/CONTINUE opcode. Avoid using longjmp() for BREAK/CONTINUE
  58037. * handling because it has a measurable performance impact in ordinary
  58038. * environments and an extreme impact in Emscripten (GH-342).
  58039. */
  58040. DUK_LOCAL void duk__handle_break_or_continue(duk_hthread *thr,
  58041. duk_uint_t label_id,
  58042. duk_small_uint_t lj_type) {
  58043. duk_catcher *cat;
  58044. duk_size_t orig_callstack_index;
  58045. DUK_ASSERT(thr != NULL);
  58046. /*
  58047. * Find a matching label catcher or 'finally' catcher in
  58048. * the same function.
  58049. *
  58050. * A label catcher must always exist and will match unless
  58051. * a 'finally' captures the break/continue first. It is the
  58052. * compiler's responsibility to ensure that labels are used
  58053. * correctly.
  58054. */
  58055. /* Note: thr->catchstack_top may be 0, so that cat < thr->catchstack
  58056. * initially. This is OK and intended.
  58057. */
  58058. cat = thr->catchstack + thr->catchstack_top - 1;
  58059. DUK_ASSERT(thr->callstack_top > 0);
  58060. orig_callstack_index = thr->callstack_top - 1;
  58061. DUK_DDD(DUK_DDDPRINT("handling break/continue with label=%ld, callstack index=%ld",
  58062. (long) label_id, (long) cat->callstack_index));
  58063. while (cat >= thr->catchstack) {
  58064. if (cat->callstack_index != orig_callstack_index) {
  58065. break;
  58066. }
  58067. DUK_DDD(DUK_DDDPRINT("considering catcher %ld: type=%ld label=%ld",
  58068. (long) (cat - thr->catchstack),
  58069. (long) DUK_CAT_GET_TYPE(cat),
  58070. (long) DUK_CAT_GET_LABEL(cat)));
  58071. if (DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF &&
  58072. DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
  58073. duk_size_t cat_idx;
  58074. duk_tval tv_tmp;
  58075. cat_idx = (duk_size_t) (cat - thr->catchstack); /* get before side effects */
  58076. #if defined(DUK_USE_FASTINT)
  58077. DUK_TVAL_SET_FASTINT_U32(&tv_tmp, (duk_uint32_t) label_id);
  58078. #else
  58079. DUK_TVAL_SET_NUMBER(&tv_tmp, (duk_double_t) label_id);
  58080. #endif
  58081. duk__handle_finally(thr, cat_idx, &tv_tmp, lj_type);
  58082. DUK_DD(DUK_DDPRINT("-> break/continue caught by 'finally', restart execution"));
  58083. return;
  58084. }
  58085. if (DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_LABEL &&
  58086. (duk_uint_t) DUK_CAT_GET_LABEL(cat) == label_id) {
  58087. duk_size_t cat_idx;
  58088. cat_idx = (duk_size_t) (cat - thr->catchstack);
  58089. duk__handle_label(thr, cat_idx, lj_type);
  58090. DUK_DD(DUK_DDPRINT("-> break/continue caught by a label catcher (in the same function), restart execution"));
  58091. return;
  58092. }
  58093. cat--;
  58094. }
  58095. /* should never happen, but be robust */
  58096. DUK_D(DUK_DPRINT("-> break/continue not caught by anything in the current function (should never happen), throw internal error"));
  58097. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR);
  58098. return;
  58099. }
  58100. /* Handle a RETURN opcode. Avoid using longjmp() for return handling because
  58101. * it has a measurable performance impact in ordinary environments and an extreme
  58102. * impact in Emscripten (GH-342). Return value is on value stack top.
  58103. */
  58104. DUK_LOCAL duk_small_uint_t duk__handle_return(duk_hthread *thr,
  58105. duk_hthread *entry_thread,
  58106. duk_size_t entry_callstack_top) {
  58107. duk_tval *tv1;
  58108. duk_tval *tv2;
  58109. duk_hthread *resumer;
  58110. duk_catcher *cat;
  58111. duk_size_t new_cat_top;
  58112. duk_size_t orig_callstack_index;
  58113. /* We can directly access value stack here. */
  58114. DUK_ASSERT(thr != NULL);
  58115. DUK_ASSERT(entry_thread != NULL);
  58116. DUK_ASSERT(thr->valstack_top - 1 >= thr->valstack_bottom);
  58117. tv1 = thr->valstack_top - 1;
  58118. DUK_TVAL_CHKFAST_INPLACE(tv1); /* fastint downgrade check for return values */
  58119. /*
  58120. * Four possible outcomes:
  58121. *
  58122. * 1. A 'finally' in the same function catches the 'return'.
  58123. * It may continue to propagate when 'finally' is finished,
  58124. * or it may be neutralized by 'finally' (both handled by
  58125. * ENDFIN).
  58126. *
  58127. * 2. The return happens at the entry level of the bytecode
  58128. * executor, so return from the executor (in C stack).
  58129. *
  58130. * 3. There is a calling (Ecmascript) activation in the call
  58131. * stack => return to it, in the same executor instance.
  58132. *
  58133. * 4. There is no calling activation, and the thread is
  58134. * terminated. There is always a resumer in this case,
  58135. * which gets the return value similarly to a 'yield'
  58136. * (except that the current thread can no longer be
  58137. * resumed).
  58138. */
  58139. DUK_ASSERT(thr != NULL);
  58140. DUK_ASSERT(thr->callstack_top >= 1);
  58141. DUK_ASSERT(thr->catchstack != NULL);
  58142. /* XXX: does not work if thr->catchstack is NULL */
  58143. /* XXX: does not work if thr->catchstack is allocated but lowest pointer */
  58144. cat = thr->catchstack + thr->catchstack_top - 1; /* may be < thr->catchstack initially */
  58145. DUK_ASSERT(thr->callstack_top > 0); /* ensures callstack_top - 1 >= 0 */
  58146. orig_callstack_index = thr->callstack_top - 1;
  58147. while (cat >= thr->catchstack) {
  58148. if (cat->callstack_index != orig_callstack_index) {
  58149. break;
  58150. }
  58151. if (DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF &&
  58152. DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
  58153. duk_size_t cat_idx;
  58154. cat_idx = (duk_size_t) (cat - thr->catchstack); /* get before side effects */
  58155. DUK_ASSERT(thr->valstack_top - 1 >= thr->valstack_bottom);
  58156. duk__handle_finally(thr, cat_idx, thr->valstack_top - 1, DUK_LJ_TYPE_RETURN);
  58157. DUK_DD(DUK_DDPRINT("-> return caught by 'finally', restart execution"));
  58158. return DUK__RETHAND_RESTART;
  58159. }
  58160. cat--;
  58161. }
  58162. /* If out of catchstack, cat = thr->catchstack - 1;
  58163. * new_cat_top will be 0 in that case.
  58164. */
  58165. new_cat_top = (duk_size_t) ((cat + 1) - thr->catchstack);
  58166. cat = NULL; /* avoid referencing, invalidated */
  58167. DUK_DDD(DUK_DDDPRINT("no catcher in catch stack, return to calling activation / yield"));
  58168. if (thr == entry_thread &&
  58169. thr->callstack_top == entry_callstack_top) {
  58170. /* Return to the bytecode executor caller which will unwind stacks.
  58171. * Return value is already on the stack top: [ ... retval ].
  58172. */
  58173. DUK_DDD(DUK_DDDPRINT("-> return propagated up to entry level, exit bytecode executor"));
  58174. return DUK__RETHAND_FINISHED;
  58175. }
  58176. if (thr->callstack_top >= 2) {
  58177. /* There is a caller; it MUST be an Ecmascript caller (otherwise it would
  58178. * match entry level check)
  58179. */
  58180. DUK_DDD(DUK_DDDPRINT("return to Ecmascript caller, idx_retval=%ld, lj_value1=%!T",
  58181. (long) (thr->callstack + thr->callstack_top - 2)->idx_retval,
  58182. (duk_tval *) &thr->heap->lj.value1));
  58183. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2))); /* must be ecmascript */
  58184. tv1 = thr->valstack + (thr->callstack + thr->callstack_top - 2)->idx_retval;
  58185. DUK_ASSERT(thr->valstack_top - 1 >= thr->valstack_bottom);
  58186. tv2 = thr->valstack_top - 1;
  58187. DUK_TVAL_SET_TVAL_UPDREF(thr, tv1, tv2); /* side effects */
  58188. DUK_DDD(DUK_DDDPRINT("return value at idx_retval=%ld is %!T",
  58189. (long) (thr->callstack + thr->callstack_top - 2)->idx_retval,
  58190. (duk_tval *) (thr->valstack + (thr->callstack + thr->callstack_top - 2)->idx_retval)));
  58191. duk_hthread_catchstack_unwind(thr, new_cat_top); /* leave 'cat' as top catcher (also works if catchstack exhausted) */
  58192. duk_hthread_callstack_unwind(thr, thr->callstack_top - 1);
  58193. duk__reconfig_valstack_ecma_return(thr, thr->callstack_top - 1);
  58194. DUK_DD(DUK_DDPRINT("-> return not intercepted, restart execution in caller"));
  58195. return DUK__RETHAND_RESTART;
  58196. }
  58197. DUK_DD(DUK_DDPRINT("no calling activation, thread finishes (similar to yield)"));
  58198. DUK_ASSERT(thr->resumer != NULL);
  58199. DUK_ASSERT(thr->resumer->callstack_top >= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */
  58200. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1) != NULL &&
  58201. DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1)) &&
  58202. ((duk_hnativefunction *) DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1))->func == duk_bi_thread_resume); /* Duktape.Thread.resume() */
  58203. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 2) != NULL &&
  58204. DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 2))); /* an Ecmascript function */
  58205. DUK_ASSERT_DISABLE((thr->resumer->callstack + thr->resumer->callstack_top - 2)->idx_retval >= 0); /* unsigned */
  58206. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING);
  58207. DUK_ASSERT(thr->resumer->state == DUK_HTHREAD_STATE_RESUMED);
  58208. resumer = thr->resumer;
  58209. /* Share yield longjmp handler. */
  58210. DUK_ASSERT(thr->valstack_top - 1 >= thr->valstack_bottom);
  58211. duk__handle_yield(thr, resumer, resumer->callstack_top - 2, thr->valstack_top - 1);
  58212. duk_hthread_terminate(thr); /* updates thread state, minimizes its allocations */
  58213. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_TERMINATED);
  58214. thr->resumer = NULL;
  58215. resumer->state = DUK_HTHREAD_STATE_RUNNING;
  58216. DUK_HEAP_SWITCH_THREAD(thr->heap, resumer);
  58217. #if 0
  58218. thr = resumer; /* not needed */
  58219. #endif
  58220. DUK_DD(DUK_DDPRINT("-> return not caught, thread terminated; handle like yield, restart execution in resumer"));
  58221. return DUK__RETHAND_RESTART;
  58222. }
  58223. /*
  58224. * Executor interrupt handling
  58225. *
  58226. * The handler is called whenever the interrupt countdown reaches zero
  58227. * (or below). The handler must perform whatever checks are activated,
  58228. * e.g. check for cumulative step count to impose an execution step
  58229. * limit or check for breakpoints or other debugger interaction.
  58230. *
  58231. * When the actions are done, the handler must reinit the interrupt
  58232. * init and counter values. The 'init' value must indicate how many
  58233. * bytecode instructions are executed before the next interrupt. The
  58234. * counter must interface with the bytecode executor loop. Concretely,
  58235. * the new init value is normally one higher than the new counter value.
  58236. * For instance, to execute exactly one bytecode instruction the init
  58237. * value is set to 1 and the counter to 0. If an error is thrown by the
  58238. * interrupt handler, the counters are set to the same value (e.g. both
  58239. * to 0 to cause an interrupt when the next bytecode instruction is about
  58240. * to be executed after error handling).
  58241. *
  58242. * Maintaining the init/counter value properly is important for accurate
  58243. * behavior. For instance, executor step limit needs a cumulative step
  58244. * count which is simply computed as a sum of 'init' values. This must
  58245. * work accurately even when single stepping.
  58246. */
  58247. #if defined(DUK_USE_INTERRUPT_COUNTER)
  58248. #define DUK__INT_NOACTION 0 /* no specific action, resume normal execution */
  58249. #define DUK__INT_RESTART 1 /* must "goto restart_execution", e.g. breakpoints changed */
  58250. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  58251. DUK_LOCAL void duk__interrupt_handle_debugger(duk_hthread *thr, duk_bool_t *out_immediate, duk_small_uint_t *out_interrupt_retval) {
  58252. duk_context *ctx;
  58253. duk_activation *act;
  58254. duk_breakpoint *bp;
  58255. duk_breakpoint **bp_active;
  58256. duk_uint_fast32_t line = 0;
  58257. duk_bool_t send_status;
  58258. duk_bool_t process_messages;
  58259. duk_bool_t processed_messages = 0;
  58260. ctx = (duk_context *) thr;
  58261. act = thr->callstack + thr->callstack_top - 1;
  58262. /* It might seem that replacing 'thr->heap' with just 'heap' below
  58263. * might be a good idea, but it increases code size slightly
  58264. * (probably due to unnecessary spilling) at least on x64.
  58265. */
  58266. /*
  58267. * Breakpoint and step state checks
  58268. */
  58269. if (act->flags & DUK_ACT_FLAG_BREAKPOINT_ACTIVE ||
  58270. (thr->heap->dbg_step_thread == thr &&
  58271. thr->heap->dbg_step_csindex == thr->callstack_top - 1)) {
  58272. line = duk_debug_curr_line(thr);
  58273. if (act->prev_line != line) {
  58274. /* Stepped? Step out is handled by callstack unwind. */
  58275. if ((thr->heap->dbg_step_type == DUK_STEP_TYPE_INTO ||
  58276. thr->heap->dbg_step_type == DUK_STEP_TYPE_OVER) &&
  58277. (thr->heap->dbg_step_thread == thr) &&
  58278. (thr->heap->dbg_step_csindex == thr->callstack_top - 1) &&
  58279. (line != thr->heap->dbg_step_startline)) {
  58280. DUK_D(DUK_DPRINT("STEP STATE TRIGGERED PAUSE at line %ld",
  58281. (long) line));
  58282. DUK_HEAP_SET_PAUSED(thr->heap);
  58283. }
  58284. /* Check for breakpoints only on line transition.
  58285. * Breakpoint is triggered when we enter the target
  58286. * line from a different line, and the previous line
  58287. * was within the same function.
  58288. *
  58289. * This condition is tricky: the condition used to be
  58290. * that transition to -or across- the breakpoint line
  58291. * triggered the breakpoint. This seems intuitively
  58292. * better because it handles breakpoints on lines with
  58293. * no emitted opcodes; but this leads to the issue
  58294. * described in: https://github.com/svaarala/duktape/issues/263.
  58295. */
  58296. bp_active = thr->heap->dbg_breakpoints_active;
  58297. for (;;) {
  58298. bp = *bp_active++;
  58299. if (bp == NULL) {
  58300. break;
  58301. }
  58302. DUK_ASSERT(bp->filename != NULL);
  58303. if (act->prev_line != bp->line && line == bp->line) {
  58304. DUK_D(DUK_DPRINT("BREAKPOINT TRIGGERED at %!O:%ld",
  58305. (duk_heaphdr *) bp->filename, (long) bp->line));
  58306. DUK_HEAP_SET_PAUSED(thr->heap);
  58307. }
  58308. }
  58309. } else {
  58310. ;
  58311. }
  58312. act->prev_line = line;
  58313. }
  58314. /*
  58315. * Rate limit check for sending status update or peeking into
  58316. * the debug transport. Both can be expensive operations that
  58317. * we don't want to do on every opcode.
  58318. *
  58319. * Making sure the interval remains reasonable on a wide variety
  58320. * of targets and bytecode is difficult without a timestamp, so
  58321. * we use a Date-provided timestamp for the rate limit check.
  58322. * But since it's also expensive to get a timestamp, a bytecode
  58323. * counter is used to rate limit getting timestamps.
  58324. */
  58325. if (thr->heap->dbg_state_dirty || thr->heap->dbg_paused) {
  58326. send_status = 1;
  58327. } else {
  58328. send_status = 0;
  58329. }
  58330. if (thr->heap->dbg_paused) {
  58331. process_messages = 1;
  58332. } else {
  58333. process_messages = 0;
  58334. }
  58335. /* XXX: remove heap->dbg_exec_counter, use heap->inst_count_interrupt instead? */
  58336. thr->heap->dbg_exec_counter += thr->interrupt_init;
  58337. if (thr->heap->dbg_exec_counter - thr->heap->dbg_last_counter >= DUK_HEAP_DBG_RATELIMIT_OPCODES) {
  58338. /* Overflow of the execution counter is fine and doesn't break
  58339. * anything here.
  58340. */
  58341. duk_double_t now, diff_last;
  58342. thr->heap->dbg_last_counter = thr->heap->dbg_exec_counter;
  58343. now = DUK_USE_DATE_GET_NOW(ctx);
  58344. diff_last = now - thr->heap->dbg_last_time;
  58345. if (diff_last < 0.0 || diff_last >= (duk_double_t) DUK_HEAP_DBG_RATELIMIT_MILLISECS) {
  58346. /* Negative value checked so that a "time jump" works
  58347. * reasonably.
  58348. *
  58349. * Same interval is now used for status sending and
  58350. * peeking.
  58351. */
  58352. thr->heap->dbg_last_time = now;
  58353. send_status = 1;
  58354. process_messages = 1;
  58355. }
  58356. }
  58357. /*
  58358. * Send status
  58359. */
  58360. act = NULL; /* may be changed */
  58361. if (send_status) {
  58362. duk_debug_send_status(thr);
  58363. thr->heap->dbg_state_dirty = 0;
  58364. }
  58365. /*
  58366. * Process messages. If we're paused, we'll block for new messages.
  58367. * if we're not paused, we'll process anything we can peek but won't
  58368. * block for more.
  58369. */
  58370. if (process_messages) {
  58371. processed_messages = duk_debug_process_messages(thr, 0 /*no_block*/);
  58372. }
  58373. /* XXX: any case here where we need to re-send status? */
  58374. /* Continue checked execution if there are breakpoints or we're stepping.
  58375. * Also use checked execution if paused flag is active - it shouldn't be
  58376. * because the debug message loop shouldn't terminate if it was. Step out
  58377. * is handled by callstack unwind and doesn't need checked execution.
  58378. * Note that debugger may have detached due to error or explicit request
  58379. * above, so we must recheck attach status.
  58380. */
  58381. if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) {
  58382. act = thr->callstack + thr->callstack_top - 1; /* relookup, may have changed */
  58383. if (act->flags & DUK_ACT_FLAG_BREAKPOINT_ACTIVE ||
  58384. ((thr->heap->dbg_step_type == DUK_STEP_TYPE_INTO ||
  58385. thr->heap->dbg_step_type == DUK_STEP_TYPE_OVER) &&
  58386. thr->heap->dbg_step_thread == thr &&
  58387. thr->heap->dbg_step_csindex == thr->callstack_top - 1) ||
  58388. thr->heap->dbg_paused) {
  58389. *out_immediate = 1;
  58390. }
  58391. /* If we processed any debug messages breakpoints may have
  58392. * changed; restart execution to re-check active breakpoints.
  58393. */
  58394. if (processed_messages) {
  58395. DUK_D(DUK_DPRINT("processed debug messages, restart execution to recheck possibly changed breakpoints"));
  58396. *out_interrupt_retval = DUK__INT_RESTART;
  58397. }
  58398. } else {
  58399. DUK_D(DUK_DPRINT("debugger became detached, resume normal execution"));
  58400. }
  58401. }
  58402. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  58403. DUK_LOCAL duk_small_uint_t duk__executor_interrupt(duk_hthread *thr) {
  58404. duk_int_t ctr;
  58405. duk_activation *act;
  58406. duk_hcompiledfunction *fun;
  58407. duk_bool_t immediate = 0;
  58408. duk_small_uint_t retval;
  58409. DUK_ASSERT(thr != NULL);
  58410. DUK_ASSERT(thr->heap != NULL);
  58411. DUK_ASSERT(thr->callstack != NULL);
  58412. DUK_ASSERT(thr->callstack_top > 0);
  58413. #if defined(DUK_USE_DEBUG)
  58414. thr->heap->inst_count_interrupt += thr->interrupt_init;
  58415. DUK_DD(DUK_DDPRINT("execution interrupt, counter=%ld, init=%ld, "
  58416. "instruction counts: executor=%ld, interrupt=%ld",
  58417. (long) thr->interrupt_counter, (long) thr->interrupt_init,
  58418. (long) thr->heap->inst_count_exec, (long) thr->heap->inst_count_interrupt));
  58419. #endif
  58420. retval = DUK__INT_NOACTION;
  58421. ctr = DUK_HTHREAD_INTCTR_DEFAULT;
  58422. /*
  58423. * Avoid nested calls. Concretely this happens during debugging, e.g.
  58424. * when we eval() an expression.
  58425. *
  58426. * Also don't interrupt if we're currently doing debug processing
  58427. * (which can be initiated outside the bytecode executor) as this
  58428. * may cause the debugger to be called recursively. Check required
  58429. * for correct operation of throw intercept and other "exotic" halting
  58430. * scenarios.
  58431. */
  58432. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  58433. if (DUK_HEAP_HAS_INTERRUPT_RUNNING(thr->heap) || thr->heap->dbg_processing) {
  58434. #else
  58435. if (DUK_HEAP_HAS_INTERRUPT_RUNNING(thr->heap)) {
  58436. #endif
  58437. DUK_DD(DUK_DDPRINT("nested executor interrupt, ignoring"));
  58438. /* Set a high interrupt counter; the original executor
  58439. * interrupt invocation will rewrite before exiting.
  58440. */
  58441. thr->interrupt_init = ctr;
  58442. thr->interrupt_counter = ctr - 1;
  58443. return DUK__INT_NOACTION;
  58444. }
  58445. DUK_HEAP_SET_INTERRUPT_RUNNING(thr->heap);
  58446. act = thr->callstack + thr->callstack_top - 1;
  58447. fun = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act);
  58448. DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION((duk_hobject *) fun));
  58449. DUK_UNREF(fun);
  58450. #if defined(DUK_USE_EXEC_TIMEOUT_CHECK)
  58451. /*
  58452. * Execution timeout check
  58453. */
  58454. if (DUK_USE_EXEC_TIMEOUT_CHECK(thr->heap->heap_udata)) {
  58455. /* Keep throwing an error whenever we get here. The unusual values
  58456. * are set this way because no instruction is ever executed, we just
  58457. * throw an error until all try/catch/finally and other catchpoints
  58458. * have been exhausted. Duktape/C code gets control at each protected
  58459. * call but whenever it enters back into Duktape the RangeError gets
  58460. * raised. User exec timeout check must consistently indicate a timeout
  58461. * until we've fully bubbled out of Duktape.
  58462. */
  58463. DUK_D(DUK_DPRINT("execution timeout, throwing a RangeError"));
  58464. thr->interrupt_init = 0;
  58465. thr->interrupt_counter = 0;
  58466. DUK_HEAP_CLEAR_INTERRUPT_RUNNING(thr->heap);
  58467. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, "execution timeout");
  58468. }
  58469. #endif /* DUK_USE_EXEC_TIMEOUT_CHECK */
  58470. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  58471. if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) {
  58472. duk__interrupt_handle_debugger(thr, &immediate, &retval);
  58473. act = thr->callstack + thr->callstack_top - 1; /* relookup if changed */
  58474. }
  58475. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  58476. /*
  58477. * Update the interrupt counter
  58478. */
  58479. if (immediate) {
  58480. /* Cause an interrupt after executing one instruction. */
  58481. ctr = 1;
  58482. }
  58483. /* The counter value is one less than the init value: init value should
  58484. * indicate how many instructions are executed before interrupt. To
  58485. * execute 1 instruction (after interrupt handler return), counter must
  58486. * be 0.
  58487. */
  58488. DUK_ASSERT(ctr >= 1);
  58489. thr->interrupt_init = ctr;
  58490. thr->interrupt_counter = ctr - 1;
  58491. DUK_HEAP_CLEAR_INTERRUPT_RUNNING(thr->heap);
  58492. return retval;
  58493. }
  58494. #endif /* DUK_USE_INTERRUPT_COUNTER */
  58495. /*
  58496. * Debugger handling for executor restart
  58497. *
  58498. * Check for breakpoints, stepping, etc, and figure out if we should execute
  58499. * in checked or normal mode. Note that we can't do this when an activation
  58500. * is created, because breakpoint status (and stepping status) may change
  58501. * later, so we must recheck every time we're executing an activation.
  58502. */
  58503. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  58504. DUK_LOCAL void duk__executor_recheck_debugger(duk_hthread *thr, duk_activation *act, duk_hcompiledfunction *fun) {
  58505. duk_heap *heap;
  58506. duk_tval *tv_tmp;
  58507. duk_hstring *filename;
  58508. duk_small_uint_t bp_idx;
  58509. duk_breakpoint **bp_active;
  58510. DUK_ASSERT(thr != NULL);
  58511. DUK_ASSERT(act != NULL);
  58512. DUK_ASSERT(fun != NULL);
  58513. heap = thr->heap;
  58514. bp_active = heap->dbg_breakpoints_active;
  58515. act->flags &= ~DUK_ACT_FLAG_BREAKPOINT_ACTIVE;
  58516. tv_tmp = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) fun, DUK_HTHREAD_STRING_FILE_NAME(thr));
  58517. if (tv_tmp && DUK_TVAL_IS_STRING(tv_tmp)) {
  58518. filename = DUK_TVAL_GET_STRING(tv_tmp);
  58519. /* Figure out all active breakpoints. A breakpoint is
  58520. * considered active if the current function's fileName
  58521. * matches the breakpoint's fileName, AND there is no
  58522. * inner function that has matching line numbers
  58523. * (otherwise a breakpoint would be triggered both
  58524. * inside and outside of the inner function which would
  58525. * be confusing). Example:
  58526. *
  58527. * function foo() {
  58528. * print('foo');
  58529. * function bar() { <-. breakpoints in these
  58530. * print('bar'); | lines should not affect
  58531. * } <-' foo() execution
  58532. * bar();
  58533. * }
  58534. *
  58535. * We need a few things that are only available when
  58536. * debugger support is enabled: (1) a line range for
  58537. * each function, and (2) access to the function
  58538. * template to access the inner functions (and their
  58539. * line ranges).
  58540. *
  58541. * It's important to have a narrow match for active
  58542. * breakpoints so that we don't enter checked execution
  58543. * when that's not necessary. For instance, if we're
  58544. * running inside a certain function and there's
  58545. * breakpoint outside in (after the call site), we
  58546. * don't want to slow down execution of the function.
  58547. */
  58548. for (bp_idx = 0; bp_idx < heap->dbg_breakpoint_count; bp_idx++) {
  58549. duk_breakpoint *bp = heap->dbg_breakpoints + bp_idx;
  58550. duk_hobject **funcs, **funcs_end;
  58551. duk_hcompiledfunction *inner_fun;
  58552. duk_bool_t bp_match;
  58553. if (bp->filename == filename &&
  58554. bp->line >= fun->start_line && bp->line <= fun->end_line) {
  58555. bp_match = 1;
  58556. DUK_DD(DUK_DDPRINT("breakpoint filename and line match: "
  58557. "%s:%ld vs. %s (line %ld vs. %ld-%ld)",
  58558. DUK_HSTRING_GET_DATA(bp->filename),
  58559. (long) bp->line,
  58560. DUK_HSTRING_GET_DATA(filename),
  58561. (long) bp->line,
  58562. (long) fun->start_line,
  58563. (long) fun->end_line));
  58564. funcs = DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, fun);
  58565. funcs_end = DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(thr->heap, fun);
  58566. while (funcs != funcs_end) {
  58567. inner_fun = (duk_hcompiledfunction *) *funcs;
  58568. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) inner_fun));
  58569. if (bp->line >= inner_fun->start_line && bp->line <= inner_fun->end_line) {
  58570. DUK_DD(DUK_DDPRINT("inner function masks ('captures') breakpoint"));
  58571. bp_match = 0;
  58572. break;
  58573. }
  58574. funcs++;
  58575. }
  58576. if (bp_match) {
  58577. /* No need to check for size of bp_active list,
  58578. * it's always larger than maximum number of
  58579. * breakpoints.
  58580. */
  58581. act->flags |= DUK_ACT_FLAG_BREAKPOINT_ACTIVE;
  58582. *bp_active = heap->dbg_breakpoints + bp_idx;
  58583. bp_active++;
  58584. }
  58585. }
  58586. }
  58587. }
  58588. *bp_active = NULL; /* terminate */
  58589. DUK_DD(DUK_DDPRINT("ACTIVE BREAKPOINTS: %ld", (long) (bp_active - thr->heap->dbg_breakpoints_active)));
  58590. /* Force pause if we were doing "step into" in another activation. */
  58591. if (thr->heap->dbg_step_thread != NULL &&
  58592. thr->heap->dbg_step_type == DUK_STEP_TYPE_INTO &&
  58593. (thr->heap->dbg_step_thread != thr ||
  58594. thr->heap->dbg_step_csindex != thr->callstack_top - 1)) {
  58595. DUK_D(DUK_DPRINT("STEP INTO ACTIVE, FORCE PAUSED"));
  58596. DUK_HEAP_SET_PAUSED(thr->heap);
  58597. }
  58598. /* Force interrupt right away if we're paused or in "checked mode".
  58599. * Step out is handled by callstack unwind.
  58600. */
  58601. if (act->flags & (DUK_ACT_FLAG_BREAKPOINT_ACTIVE) ||
  58602. thr->heap->dbg_paused ||
  58603. (thr->heap->dbg_step_type != DUK_STEP_TYPE_OUT &&
  58604. thr->heap->dbg_step_csindex == thr->callstack_top - 1)) {
  58605. /* We'll need to interrupt early so recompute the init
  58606. * counter to reflect the number of bytecode instructions
  58607. * executed so that step counts for e.g. debugger rate
  58608. * limiting are accurate.
  58609. */
  58610. DUK_ASSERT(thr->interrupt_counter <= thr->interrupt_init);
  58611. thr->interrupt_init = thr->interrupt_init - thr->interrupt_counter;
  58612. thr->interrupt_counter = 0;
  58613. }
  58614. }
  58615. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  58616. /*
  58617. * Ecmascript bytecode executor.
  58618. *
  58619. * Resume execution for the current thread from its current activation.
  58620. * Returns when execution would return from the entry level activation,
  58621. * leaving a single return value on top of the stack. Function calls
  58622. * and thread resumptions are handled internally. If an error occurs,
  58623. * a longjmp() with type DUK_LJ_TYPE_THROW is called on the entry level
  58624. * setjmp() jmpbuf.
  58625. *
  58626. * Ecmascript function calls and coroutine resumptions are handled
  58627. * internally (by the outer executor function) without recursive C calls.
  58628. * Other function calls are handled using duk_handle_call(), increasing
  58629. * C recursion depth.
  58630. *
  58631. * Abrupt completions (= long control tranfers) are handled either
  58632. * directly by reconfiguring relevant stacks and restarting execution,
  58633. * or via a longjmp. Longjmp-free handling is preferable for performance
  58634. * (especially Emscripten performance), and is used for: break, continue,
  58635. * and return.
  58636. *
  58637. * For more detailed notes, see doc/execution.rst.
  58638. *
  58639. * Also see doc/code-issues.rst for discussion of setjmp(), longjmp(),
  58640. * and volatile.
  58641. */
  58642. /* Presence of 'fun' is config based, there's a marginal performance
  58643. * difference and the best option is architecture dependent.
  58644. */
  58645. #if defined(DUK_USE_EXEC_FUN_LOCAL)
  58646. #define DUK__FUN() fun
  58647. #else
  58648. #define DUK__FUN() ((duk_hcompiledfunction *) DUK_ACT_GET_FUNC((thr)->callstack + (thr)->callstack_top - 1))
  58649. #endif
  58650. #define DUK__STRICT() (DUK_HOBJECT_HAS_STRICT((duk_hobject *) DUK__FUN()))
  58651. #define DUK__REG(x) (*(thr->valstack_bottom + (x)))
  58652. #define DUK__REGP(x) (thr->valstack_bottom + (x))
  58653. #define DUK__CONST(x) (*(consts + (x)))
  58654. #define DUK__CONSTP(x) (consts + (x))
  58655. #define DUK__REGCONST(x) ((x) < DUK_BC_REGLIMIT ? DUK__REG((x)) : DUK__CONST((x) - DUK_BC_REGLIMIT))
  58656. #define DUK__REGCONSTP(x) ((x) < DUK_BC_REGLIMIT ? DUK__REGP((x)) : DUK__CONSTP((x) - DUK_BC_REGLIMIT))
  58657. #ifdef DUK_USE_VERBOSE_EXECUTOR_ERRORS
  58658. #define DUK__INTERNAL_ERROR(msg) do { \
  58659. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, (msg)); \
  58660. } while (0)
  58661. #else
  58662. #define DUK__INTERNAL_ERROR(msg) do { \
  58663. goto internal_error; \
  58664. } while (0)
  58665. #endif
  58666. #define DUK__SYNC_CURR_PC() do { \
  58667. duk_activation *act; \
  58668. act = thr->callstack + thr->callstack_top - 1; \
  58669. act->curr_pc = curr_pc; \
  58670. } while (0)
  58671. #define DUK__SYNC_AND_NULL_CURR_PC() do { \
  58672. duk_activation *act; \
  58673. act = thr->callstack + thr->callstack_top - 1; \
  58674. act->curr_pc = curr_pc; \
  58675. thr->ptr_curr_pc = NULL; \
  58676. } while (0)
  58677. /* Outer executor with setjmp/longjmp handling. */
  58678. DUK_INTERNAL void duk_js_execute_bytecode(duk_hthread *exec_thr) {
  58679. /* Entry level info. */
  58680. duk_hthread *entry_thread;
  58681. duk_size_t entry_callstack_top;
  58682. duk_int_t entry_call_recursion_depth;
  58683. duk_jmpbuf *entry_jmpbuf_ptr;
  58684. duk_heap *heap;
  58685. duk_jmpbuf jmpbuf;
  58686. DUK_ASSERT(exec_thr != NULL);
  58687. DUK_ASSERT(exec_thr->heap != NULL);
  58688. DUK_ASSERT(exec_thr->heap->curr_thread != NULL);
  58689. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR((duk_heaphdr *) exec_thr);
  58690. DUK_ASSERT(exec_thr->callstack_top >= 1); /* at least one activation, ours */
  58691. DUK_ASSERT(DUK_ACT_GET_FUNC(exec_thr->callstack + exec_thr->callstack_top - 1) != NULL);
  58692. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(exec_thr->callstack + exec_thr->callstack_top - 1)));
  58693. entry_thread = exec_thr;
  58694. heap = entry_thread->heap;
  58695. entry_callstack_top = entry_thread->callstack_top;
  58696. entry_call_recursion_depth = entry_thread->heap->call_recursion_depth;
  58697. entry_jmpbuf_ptr = entry_thread->heap->lj.jmpbuf_ptr;
  58698. /*
  58699. * Note: we currently assume that the setjmp() catchpoint is
  58700. * not re-entrant (longjmp() cannot be called more than once
  58701. * for a single setjmp()).
  58702. *
  58703. * See doc/code-issues.rst for notes on variable assignment
  58704. * before and after setjmp().
  58705. */
  58706. for (;;) {
  58707. heap->lj.jmpbuf_ptr = &jmpbuf;
  58708. DUK_ASSERT(heap->lj.jmpbuf_ptr != NULL);
  58709. if (DUK_SETJMP(heap->lj.jmpbuf_ptr->jb) != 0) {
  58710. duk_small_uint_t lj_ret;
  58711. DUK_DDD(DUK_DDDPRINT("longjmp caught by bytecode executor"));
  58712. /* Longjmp callers are required to sync-and-null thr->ptr_curr_pc
  58713. * before longjmp.
  58714. */
  58715. DUK_ASSERT(heap->curr_thread != NULL);
  58716. DUK_ASSERT(heap->curr_thread->ptr_curr_pc == NULL);
  58717. /* XXX: signalling the need to shrink check (only if unwound) */
  58718. /* Must be restored here to handle e.g. yields properly. */
  58719. heap->call_recursion_depth = entry_call_recursion_depth;
  58720. /* Switch to caller's setjmp() catcher so that if an error occurs
  58721. * during error handling, it is always propagated outwards instead
  58722. * of causing an infinite loop in our own handler.
  58723. */
  58724. heap->lj.jmpbuf_ptr = (duk_jmpbuf *) entry_jmpbuf_ptr;
  58725. lj_ret = duk__handle_longjmp(heap->curr_thread, entry_thread, entry_callstack_top);
  58726. if (lj_ret == DUK__LONGJMP_RESTART) {
  58727. /* Restart bytecode execution, possibly with a changed thread. */
  58728. ;
  58729. } else {
  58730. /* Rethrow error to calling state. */
  58731. DUK_ASSERT(lj_ret == DUK__LONGJMP_RETHROW);
  58732. /* Longjmp handling has restored jmpbuf_ptr. */
  58733. DUK_ASSERT(heap->lj.jmpbuf_ptr == entry_jmpbuf_ptr);
  58734. /* Thread may have changed, e.g. YIELD converted to THROW. */
  58735. duk_err_longjmp(heap->curr_thread);
  58736. DUK_UNREACHABLE();
  58737. }
  58738. } else {
  58739. /* Execute bytecode until returned or longjmp(). */
  58740. duk__js_execute_bytecode_inner(entry_thread, entry_callstack_top);
  58741. /* Successful return: restore jmpbuf and return to caller. */
  58742. heap->lj.jmpbuf_ptr = (duk_jmpbuf *) entry_jmpbuf_ptr;
  58743. return;
  58744. }
  58745. }
  58746. DUK_UNREACHABLE();
  58747. }
  58748. /* Inner executor, performance critical. */
  58749. DUK_LOCAL DUK_NOINLINE void duk__js_execute_bytecode_inner(duk_hthread *entry_thread, duk_size_t entry_callstack_top) {
  58750. /* Current PC, accessed by other functions through thr->ptr_to_curr_pc.
  58751. * Critical for performance. It would be safest to make this volatile,
  58752. * but that eliminates performance benefits; aliasing guarantees
  58753. * should be enough though.
  58754. */
  58755. duk_instr_t *curr_pc; /* bytecode has a stable pointer */
  58756. /* Hot variables for interpretation. Critical for performance,
  58757. * but must add sparingly to minimize register shuffling.
  58758. */
  58759. duk_hthread *thr; /* stable */
  58760. duk_tval *consts; /* stable */
  58761. duk_uint_fast32_t ins;
  58762. /* 'funcs' is quite rarely used, so no local for it */
  58763. #if defined(DUK_USE_EXEC_FUN_LOCAL)
  58764. duk_hcompiledfunction *fun;
  58765. #else
  58766. /* 'fun' is quite rarely used, so no local for it */
  58767. #endif
  58768. #ifdef DUK_USE_INTERRUPT_COUNTER
  58769. duk_int_t int_ctr;
  58770. #endif
  58771. #ifdef DUK_USE_ASSERTIONS
  58772. duk_size_t valstack_top_base; /* valstack top, should match before interpreting each op (no leftovers) */
  58773. #endif
  58774. /*
  58775. * Restart execution by reloading thread state.
  58776. *
  58777. * Note that 'thr' and any thread configuration may have changed,
  58778. * so all local variables are suspect and we need to reinitialize.
  58779. *
  58780. * The number of local variables should be kept to a minimum: if
  58781. * the variables are spilled, they will need to be loaded from
  58782. * memory anyway.
  58783. *
  58784. * Any 'goto restart_execution;' code path in opcode dispatch must
  58785. * ensure 'curr_pc' is synced back to act->curr_pc before the goto
  58786. * takes place.
  58787. *
  58788. * The interpreter must be very careful with memory pointers, as
  58789. * many pointers are not guaranteed to be 'stable' and may be
  58790. * reallocated and relocated on-the-fly quite easily (e.g. by a
  58791. * memory allocation or a property access).
  58792. *
  58793. * The following are assumed to have stable pointers:
  58794. * - the current thread
  58795. * - the current function
  58796. * - the bytecode, constant table, inner function table of the
  58797. * current function (as they are a part of the function allocation)
  58798. *
  58799. * The following are assumed to have semi-stable pointers:
  58800. * - the current activation entry: stable as long as callstack
  58801. * is not changed (reallocated by growing or shrinking), or
  58802. * by any garbage collection invocation (through finalizers)
  58803. * - Note in particular that ANY DECREF can invalidate the
  58804. * activation pointer, so for the most part a fresh lookup
  58805. * is required
  58806. *
  58807. * The following are not assumed to have stable pointers at all:
  58808. * - the value stack (registers) of the current thread
  58809. * - the catch stack of the current thread
  58810. *
  58811. * See execution.rst for discussion.
  58812. */
  58813. restart_execution:
  58814. /* Lookup current thread; use the stable 'entry_thread' for this to
  58815. * avoid clobber warnings. Any valid, reachable 'thr' value would be
  58816. * fine for this, so using 'entry_thread' is just to silence warnings.
  58817. */
  58818. thr = entry_thread->heap->curr_thread;
  58819. DUK_ASSERT(thr != NULL);
  58820. DUK_ASSERT(thr->callstack_top >= 1);
  58821. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1) != NULL);
  58822. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1)));
  58823. thr->ptr_curr_pc = &curr_pc;
  58824. /* Relookup and initialize dispatch loop variables. Debugger check. */
  58825. {
  58826. duk_activation *act;
  58827. #if !defined(DUK_USE_EXEC_FUN_LOCAL)
  58828. duk_hcompiledfunction *fun;
  58829. #endif
  58830. /* Assume interrupt init/counter are properly initialized here. */
  58831. /* Assume that thr->valstack_bottom has been set-up before getting here. */
  58832. act = thr->callstack + thr->callstack_top - 1;
  58833. fun = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act);
  58834. DUK_ASSERT(fun != NULL);
  58835. DUK_ASSERT(thr->valstack_top - thr->valstack_bottom == fun->nregs);
  58836. consts = DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, fun);
  58837. DUK_ASSERT(consts != NULL);
  58838. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  58839. if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap) && !thr->heap->dbg_processing) {
  58840. thr->heap->dbg_processing = 1;
  58841. duk__executor_recheck_debugger(thr, act, fun);
  58842. act = thr->callstack + thr->callstack_top - 1; /* relookup after side effects */
  58843. thr->heap->dbg_processing = 0;
  58844. }
  58845. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  58846. #ifdef DUK_USE_ASSERTIONS
  58847. valstack_top_base = (duk_size_t) (thr->valstack_top - thr->valstack);
  58848. #endif
  58849. /* Set up curr_pc for opcode dispatch. */
  58850. curr_pc = act->curr_pc;
  58851. }
  58852. DUK_DD(DUK_DDPRINT("restarting execution, thr %p, act idx %ld, fun %p,"
  58853. "consts %p, funcs %p, lev %ld, regbot %ld, regtop %ld, catchstack_top=%ld, "
  58854. "preventcount=%ld",
  58855. (void *) thr,
  58856. (long) (thr->callstack_top - 1),
  58857. (void *) DUK__FUN(),
  58858. (void *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, DUK__FUN()),
  58859. (void *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, DUK__FUN()),
  58860. (long) (thr->callstack_top - 1),
  58861. (long) (thr->valstack_bottom - thr->valstack),
  58862. (long) (thr->valstack_top - thr->valstack),
  58863. (long) thr->catchstack_top,
  58864. (long) thr->callstack_preventcount));
  58865. /* Dispatch loop. */
  58866. for (;;) {
  58867. DUK_ASSERT(thr->callstack_top >= 1);
  58868. DUK_ASSERT(thr->valstack_top - thr->valstack_bottom == DUK__FUN()->nregs);
  58869. DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack) == valstack_top_base);
  58870. /* Executor interrupt counter check, used to implement breakpoints,
  58871. * debugging interface, execution timeouts, etc. The counter is heap
  58872. * specific but is maintained in the current thread to make the check
  58873. * as fast as possible. The counter is copied back to the heap struct
  58874. * whenever a thread switch occurs by the DUK_HEAP_SWITCH_THREAD() macro.
  58875. */
  58876. #if defined(DUK_USE_INTERRUPT_COUNTER)
  58877. int_ctr = thr->interrupt_counter;
  58878. if (DUK_LIKELY(int_ctr > 0)) {
  58879. thr->interrupt_counter = int_ctr - 1;
  58880. } else {
  58881. /* Trigger at zero or below */
  58882. duk_small_uint_t exec_int_ret;
  58883. /* Write curr_pc back for the debugger. */
  58884. DUK_ASSERT(thr->callstack_top > 0);
  58885. {
  58886. duk_activation *act;
  58887. act = thr->callstack + thr->callstack_top - 1;
  58888. act->curr_pc = (duk_instr_t *) curr_pc;
  58889. }
  58890. /* Force restart caused by a function return; must recheck
  58891. * debugger breakpoints before checking line transitions,
  58892. * see GH-303. Restart and then handle interrupt_counter
  58893. * zero again.
  58894. */
  58895. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  58896. if (thr->heap->dbg_force_restart) {
  58897. DUK_DD(DUK_DDPRINT("dbg_force_restart flag forced restart execution")); /* GH-303 */
  58898. thr->heap->dbg_force_restart = 0;
  58899. goto restart_execution;
  58900. }
  58901. #endif
  58902. exec_int_ret = duk__executor_interrupt(thr);
  58903. if (exec_int_ret == DUK__INT_RESTART) {
  58904. /* curr_pc synced back above */
  58905. goto restart_execution;
  58906. }
  58907. }
  58908. #endif /* DUK_USE_INTERRUPT_COUNTER */
  58909. #if defined(DUK_USE_INTERRUPT_COUNTER) && defined(DUK_USE_DEBUG)
  58910. /* For cross-checking during development: ensure dispatch count
  58911. * matches cumulative interrupt counter init value sums.
  58912. */
  58913. thr->heap->inst_count_exec++;
  58914. #endif
  58915. #if defined(DUK_USE_ASSERTIONS) || defined(DUK_USE_DEBUG)
  58916. {
  58917. duk_activation *act;
  58918. act = thr->callstack + thr->callstack_top - 1;
  58919. DUK_ASSERT(curr_pc >= DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, DUK__FUN()));
  58920. DUK_ASSERT(curr_pc < DUK_HCOMPILEDFUNCTION_GET_CODE_END(thr->heap, DUK__FUN()));
  58921. DUK_UNREF(act); /* if debugging disabled */
  58922. DUK_DDD(DUK_DDDPRINT("executing bytecode: pc=%ld, ins=0x%08lx, op=%ld, valstack_top=%ld/%ld, nregs=%ld --> %!I",
  58923. (long) (curr_pc - DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, DUK__FUN())),
  58924. (unsigned long) *curr_pc,
  58925. (long) DUK_DEC_OP(*curr_pc),
  58926. (long) (thr->valstack_top - thr->valstack),
  58927. (long) (thr->valstack_end - thr->valstack),
  58928. (long) (DUK__FUN() ? DUK__FUN()->nregs : -1),
  58929. (duk_instr_t) *curr_pc));
  58930. }
  58931. #endif
  58932. #if defined(DUK_USE_ASSERTIONS)
  58933. /* Quite heavy assert: check valstack policy. Improper
  58934. * shuffle instructions can write beyond valstack_top/end
  58935. * so this check catches them in the act.
  58936. */
  58937. {
  58938. duk_tval *tv;
  58939. tv = thr->valstack_top;
  58940. while (tv != thr->valstack_end) {
  58941. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv));
  58942. tv++;
  58943. }
  58944. }
  58945. #endif
  58946. ins = *curr_pc++;
  58947. /* Typing: use duk_small_(u)int_fast_t when decoding small
  58948. * opcode fields (op, A, B, C) and duk_(u)int_fast_t when
  58949. * decoding larger fields (e.g. BC which is 18 bits). Use
  58950. * unsigned variant by default, signed when the value is used
  58951. * in signed arithmetic. Using variable names such as 'a', 'b',
  58952. * 'c', 'bc', etc makes it easier to spot typing mismatches.
  58953. */
  58954. /* XXX: the best typing needs to be validated by perf measurement:
  58955. * e.g. using a small type which is the cast to a larger duk_idx_t
  58956. * may be slower than declaring the variable as a duk_idx_t in the
  58957. * first place.
  58958. */
  58959. /* XXX: use macros for the repetitive tval/refcount handling. */
  58960. switch ((int) DUK_DEC_OP(ins)) {
  58961. /* XXX: switch cast? */
  58962. case DUK_OP_LDREG: {
  58963. duk_small_uint_fast_t a;
  58964. duk_uint_fast_t bc;
  58965. duk_tval *tv1, *tv2;
  58966. a = DUK_DEC_A(ins); tv1 = DUK__REGP(a);
  58967. bc = DUK_DEC_BC(ins); tv2 = DUK__REGP(bc);
  58968. DUK_TVAL_SET_TVAL_UPDREF_FAST(thr, tv1, tv2); /* side effects */
  58969. break;
  58970. }
  58971. case DUK_OP_STREG: {
  58972. duk_small_uint_fast_t a;
  58973. duk_uint_fast_t bc;
  58974. duk_tval *tv1, *tv2;
  58975. a = DUK_DEC_A(ins); tv1 = DUK__REGP(a);
  58976. bc = DUK_DEC_BC(ins); tv2 = DUK__REGP(bc);
  58977. DUK_TVAL_SET_TVAL_UPDREF_FAST(thr, tv2, tv1); /* side effects */
  58978. break;
  58979. }
  58980. case DUK_OP_LDCONST: {
  58981. duk_small_uint_fast_t a;
  58982. duk_uint_fast_t bc;
  58983. duk_tval *tv1, *tv2;
  58984. a = DUK_DEC_A(ins); tv1 = DUK__REGP(a);
  58985. bc = DUK_DEC_BC(ins); tv2 = DUK__CONSTP(bc);
  58986. DUK_TVAL_SET_TVAL_UPDREF_FAST(thr, tv1, tv2); /* side effects */
  58987. break;
  58988. }
  58989. case DUK_OP_LDINT: {
  58990. duk_small_uint_fast_t a;
  58991. duk_int_fast_t bc;
  58992. duk_tval *tv1;
  58993. #if defined(DUK_USE_FASTINT)
  58994. duk_int32_t val;
  58995. #else
  58996. duk_double_t val;
  58997. #endif
  58998. #if defined(DUK_USE_FASTINT)
  58999. a = DUK_DEC_A(ins); tv1 = DUK__REGP(a);
  59000. bc = DUK_DEC_BC(ins); val = (duk_int32_t) (bc - DUK_BC_LDINT_BIAS);
  59001. DUK_TVAL_SET_FASTINT_I32_UPDREF(thr, tv1, val); /* side effects */
  59002. #else
  59003. a = DUK_DEC_A(ins); tv1 = DUK__REGP(a);
  59004. bc = DUK_DEC_BC(ins); val = (duk_double_t) (bc - DUK_BC_LDINT_BIAS);
  59005. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv1, val); /* side effects */
  59006. #endif
  59007. break;
  59008. }
  59009. case DUK_OP_LDINTX: {
  59010. duk_small_uint_fast_t a;
  59011. duk_tval *tv1;
  59012. duk_double_t val;
  59013. /* LDINTX is not necessarily in FASTINT range, so
  59014. * no fast path for now.
  59015. *
  59016. * XXX: perhaps restrict LDINTX to fastint range, wider
  59017. * range very rarely needed.
  59018. */
  59019. a = DUK_DEC_A(ins); tv1 = DUK__REGP(a);
  59020. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1));
  59021. val = DUK_TVAL_GET_NUMBER(tv1) * ((duk_double_t) (1L << DUK_BC_LDINTX_SHIFT)) +
  59022. (duk_double_t) DUK_DEC_BC(ins);
  59023. #if defined(DUK_USE_FASTINT)
  59024. DUK_TVAL_SET_NUMBER_CHKFAST(tv1, val);
  59025. #else
  59026. DUK_TVAL_SET_NUMBER(tv1, val);
  59027. #endif
  59028. break;
  59029. }
  59030. case DUK_OP_MPUTOBJ:
  59031. case DUK_OP_MPUTOBJI: {
  59032. duk_context *ctx = (duk_context *) thr;
  59033. duk_small_uint_fast_t a;
  59034. duk_tval *tv1;
  59035. duk_hobject *obj;
  59036. duk_uint_fast_t idx;
  59037. duk_small_uint_fast_t count;
  59038. /* A -> register of target object
  59039. * B -> first register of key/value pair list
  59040. * C -> number of key/value pairs
  59041. */
  59042. a = DUK_DEC_A(ins); tv1 = DUK__REGP(a);
  59043. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1));
  59044. obj = DUK_TVAL_GET_OBJECT(tv1);
  59045. idx = (duk_uint_fast_t) DUK_DEC_B(ins);
  59046. if (DUK_DEC_OP(ins) == DUK_OP_MPUTOBJI) {
  59047. duk_tval *tv_ind = DUK__REGP(idx);
  59048. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind));
  59049. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind);
  59050. }
  59051. count = (duk_small_uint_fast_t) DUK_DEC_C(ins);
  59052. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  59053. if (DUK_UNLIKELY(idx + count * 2 > (duk_uint_fast_t) duk_get_top(ctx))) {
  59054. /* XXX: use duk_is_valid_index() instead? */
  59055. /* XXX: improve check; check against nregs, not against top */
  59056. DUK__INTERNAL_ERROR("MPUTOBJ out of bounds");
  59057. }
  59058. #endif
  59059. duk_push_hobject(ctx, obj);
  59060. while (count > 0) {
  59061. /* XXX: faster initialization (direct access or better primitives) */
  59062. duk_push_tval(ctx, DUK__REGP(idx));
  59063. DUK_ASSERT(duk_is_string(ctx, -1));
  59064. duk_push_tval(ctx, DUK__REGP(idx + 1)); /* -> [... obj key value] */
  59065. duk_xdef_prop_wec(ctx, -3); /* -> [... obj] */
  59066. count--;
  59067. idx += 2;
  59068. }
  59069. duk_pop(ctx); /* [... obj] -> [...] */
  59070. break;
  59071. }
  59072. case DUK_OP_MPUTARR:
  59073. case DUK_OP_MPUTARRI: {
  59074. duk_context *ctx = (duk_context *) thr;
  59075. duk_small_uint_fast_t a;
  59076. duk_tval *tv1;
  59077. duk_hobject *obj;
  59078. duk_uint_fast_t idx;
  59079. duk_small_uint_fast_t count;
  59080. duk_uint32_t arr_idx;
  59081. /* A -> register of target object
  59082. * B -> first register of value data (start_index, value1, value2, ..., valueN)
  59083. * C -> number of key/value pairs (N)
  59084. */
  59085. a = DUK_DEC_A(ins); tv1 = DUK__REGP(a);
  59086. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1));
  59087. obj = DUK_TVAL_GET_OBJECT(tv1);
  59088. DUK_ASSERT(obj != NULL);
  59089. idx = (duk_uint_fast_t) DUK_DEC_B(ins);
  59090. if (DUK_DEC_OP(ins) == DUK_OP_MPUTARRI) {
  59091. duk_tval *tv_ind = DUK__REGP(idx);
  59092. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind));
  59093. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind);
  59094. }
  59095. count = (duk_small_uint_fast_t) DUK_DEC_C(ins);
  59096. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  59097. if (idx + count + 1 > (duk_uint_fast_t) duk_get_top(ctx)) {
  59098. /* XXX: use duk_is_valid_index() instead? */
  59099. /* XXX: improve check; check against nregs, not against top */
  59100. DUK__INTERNAL_ERROR("MPUTARR out of bounds");
  59101. }
  59102. #endif
  59103. tv1 = DUK__REGP(idx);
  59104. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1));
  59105. arr_idx = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv1);
  59106. idx++;
  59107. duk_push_hobject(ctx, obj);
  59108. while (count > 0) {
  59109. /* duk_xdef_prop() will define an own property without any array
  59110. * special behaviors. We'll need to set the array length explicitly
  59111. * in the end. For arrays with elisions, the compiler will emit an
  59112. * explicit SETALEN which will update the length.
  59113. */
  59114. /* XXX: because we're dealing with 'own' properties of a fresh array,
  59115. * the array initializer should just ensure that the array has a large
  59116. * enough array part and write the values directly into array part,
  59117. * and finally set 'length' manually in the end (as already happens now).
  59118. */
  59119. duk_push_tval(ctx, DUK__REGP(idx)); /* -> [... obj value] */
  59120. duk_xdef_prop_index_wec(ctx, -2, arr_idx); /* -> [... obj] */
  59121. /* XXX: could use at least one fewer loop counters */
  59122. count--;
  59123. idx++;
  59124. arr_idx++;
  59125. }
  59126. /* XXX: E5.1 Section 11.1.4 coerces the final length through
  59127. * ToUint32() which is odd but happens now as a side effect of
  59128. * 'arr_idx' type.
  59129. */
  59130. duk_hobject_set_length(thr, obj, (duk_uint32_t) arr_idx);
  59131. duk_pop(ctx); /* [... obj] -> [...] */
  59132. break;
  59133. }
  59134. case DUK_OP_NEW:
  59135. case DUK_OP_NEWI: {
  59136. duk_context *ctx = (duk_context *) thr;
  59137. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  59138. duk_uint_fast_t idx;
  59139. duk_small_uint_fast_t i;
  59140. /* A -> unused (reserved for flags, for consistency with DUK_OP_CALL)
  59141. * B -> target register and start reg: constructor, arg1, ..., argN
  59142. * (for DUK_OP_NEWI, 'b' is indirect)
  59143. * C -> num args (N)
  59144. */
  59145. /* duk_new() will call the constuctor using duk_handle_call().
  59146. * A constructor call prevents a yield from inside the constructor,
  59147. * even if the constructor is an Ecmascript function.
  59148. */
  59149. /* Don't need to sync curr_pc here; duk_new() will do that
  59150. * when it augments the created error.
  59151. */
  59152. /* XXX: unnecessary copying of values? Just set 'top' to
  59153. * b + c, and let the return handling fix up the stack frame?
  59154. */
  59155. idx = (duk_uint_fast_t) DUK_DEC_B(ins);
  59156. if (DUK_DEC_OP(ins) == DUK_OP_NEWI) {
  59157. duk_tval *tv_ind = DUK__REGP(idx);
  59158. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind));
  59159. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind);
  59160. }
  59161. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  59162. if (idx + c + 1 > (duk_uint_fast_t) duk_get_top(ctx)) {
  59163. /* XXX: use duk_is_valid_index() instead? */
  59164. /* XXX: improve check; check against nregs, not against top */
  59165. DUK__INTERNAL_ERROR("NEW out of bounds");
  59166. }
  59167. #endif
  59168. duk_require_stack(ctx, (duk_idx_t) c);
  59169. duk_push_tval(ctx, DUK__REGP(idx));
  59170. for (i = 0; i < c; i++) {
  59171. duk_push_tval(ctx, DUK__REGP(idx + i + 1));
  59172. }
  59173. duk_new(ctx, (duk_idx_t) c); /* [... constructor arg1 ... argN] -> [retval] */
  59174. DUK_DDD(DUK_DDDPRINT("NEW -> %!iT", (duk_tval *) duk_get_tval(ctx, -1)));
  59175. duk_replace(ctx, (duk_idx_t) idx);
  59176. /* When debugger is enabled, we need to recheck the activation
  59177. * status after returning. This is now handled by call handling
  59178. * and heap->dbg_force_restart.
  59179. */
  59180. break;
  59181. }
  59182. case DUK_OP_REGEXP: {
  59183. #ifdef DUK_USE_REGEXP_SUPPORT
  59184. duk_context *ctx = (duk_context *) thr;
  59185. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59186. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59187. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  59188. /* A -> target register
  59189. * B -> bytecode (also contains flags)
  59190. * C -> escaped source
  59191. */
  59192. duk_push_tval(ctx, DUK__REGCONSTP(c));
  59193. duk_push_tval(ctx, DUK__REGCONSTP(b)); /* -> [ ... escaped_source bytecode ] */
  59194. duk_regexp_create_instance(thr); /* -> [ ... regexp_instance ] */
  59195. DUK_DDD(DUK_DDDPRINT("regexp instance: %!iT", (duk_tval *) duk_get_tval(ctx, -1)));
  59196. duk_replace(ctx, (duk_idx_t) a);
  59197. #else
  59198. /* The compiler should never emit DUK_OP_REGEXP if there is no
  59199. * regexp support.
  59200. */
  59201. DUK__INTERNAL_ERROR("no regexp support");
  59202. #endif
  59203. break;
  59204. }
  59205. case DUK_OP_CSREG:
  59206. case DUK_OP_CSREGI: {
  59207. /*
  59208. * Assuming a register binds to a variable declared within this
  59209. * function (a declarative binding), the 'this' for the call
  59210. * setup is always 'undefined'. E5 Section 10.2.1.1.6.
  59211. */
  59212. duk_context *ctx = (duk_context *) thr;
  59213. duk_small_uint_fast_t b = DUK_DEC_B(ins); /* restricted to regs */
  59214. duk_uint_fast_t idx;
  59215. /* A -> target register (A, A+1) for call setup
  59216. * (for DUK_OP_CSREGI, 'a' is indirect)
  59217. * B -> register containing target function (not type checked here)
  59218. */
  59219. /* XXX: direct manipulation, or duk_replace_tval() */
  59220. /* Note: target registers a and a+1 may overlap with DUK__REGP(b).
  59221. * Careful here.
  59222. */
  59223. idx = (duk_uint_fast_t) DUK_DEC_A(ins);
  59224. if (DUK_DEC_OP(ins) == DUK_OP_CSREGI) {
  59225. duk_tval *tv_ind = DUK__REGP(idx);
  59226. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind));
  59227. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind);
  59228. }
  59229. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  59230. if (idx + 2 > (duk_uint_fast_t) duk_get_top(ctx)) {
  59231. /* XXX: use duk_is_valid_index() instead? */
  59232. /* XXX: improve check; check against nregs, not against top */
  59233. DUK__INTERNAL_ERROR("CSREG out of bounds");
  59234. }
  59235. #endif
  59236. duk_push_tval(ctx, DUK__REGP(b));
  59237. duk_replace(ctx, (duk_idx_t) idx);
  59238. duk_push_undefined(ctx);
  59239. duk_replace(ctx, (duk_idx_t) (idx + 1));
  59240. break;
  59241. }
  59242. case DUK_OP_GETVAR: {
  59243. duk_context *ctx = (duk_context *) thr;
  59244. duk_activation *act;
  59245. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59246. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  59247. duk_tval *tv1;
  59248. duk_hstring *name;
  59249. tv1 = DUK__CONSTP(bc);
  59250. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  59251. name = DUK_TVAL_GET_STRING(tv1);
  59252. DUK_ASSERT(name != NULL);
  59253. DUK_DDD(DUK_DDDPRINT("GETVAR: '%!O'", (duk_heaphdr *) name));
  59254. act = thr->callstack + thr->callstack_top - 1;
  59255. (void) duk_js_getvar_activation(thr, act, name, 1 /*throw*/); /* -> [... val this] */
  59256. duk_pop(ctx); /* 'this' binding is not needed here */
  59257. duk_replace(ctx, (duk_idx_t) a);
  59258. break;
  59259. }
  59260. case DUK_OP_PUTVAR: {
  59261. duk_activation *act;
  59262. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59263. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  59264. duk_tval *tv1;
  59265. duk_hstring *name;
  59266. tv1 = DUK__CONSTP(bc);
  59267. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  59268. name = DUK_TVAL_GET_STRING(tv1);
  59269. DUK_ASSERT(name != NULL);
  59270. /* XXX: putvar takes a duk_tval pointer, which is awkward and
  59271. * should be reworked.
  59272. */
  59273. tv1 = DUK__REGP(a); /* val */
  59274. act = thr->callstack + thr->callstack_top - 1;
  59275. duk_js_putvar_activation(thr, act, name, tv1, DUK__STRICT());
  59276. break;
  59277. }
  59278. case DUK_OP_DECLVAR: {
  59279. duk_activation *act;
  59280. duk_context *ctx = (duk_context *) thr;
  59281. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59282. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59283. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  59284. duk_tval *tv1;
  59285. duk_hstring *name;
  59286. duk_small_uint_t prop_flags;
  59287. duk_bool_t is_func_decl;
  59288. duk_bool_t is_undef_value;
  59289. tv1 = DUK__REGCONSTP(b);
  59290. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  59291. name = DUK_TVAL_GET_STRING(tv1);
  59292. DUK_ASSERT(name != NULL);
  59293. is_undef_value = ((a & DUK_BC_DECLVAR_FLAG_UNDEF_VALUE) != 0);
  59294. is_func_decl = ((a & DUK_BC_DECLVAR_FLAG_FUNC_DECL) != 0);
  59295. /* XXX: declvar takes an duk_tval pointer, which is awkward and
  59296. * should be reworked.
  59297. */
  59298. /* Compiler is responsible for selecting property flags (configurability,
  59299. * writability, etc).
  59300. */
  59301. prop_flags = a & DUK_PROPDESC_FLAGS_MASK;
  59302. if (is_undef_value) {
  59303. duk_push_undefined(ctx);
  59304. } else {
  59305. duk_push_tval(ctx, DUK__REGCONSTP(c));
  59306. }
  59307. tv1 = duk_get_tval(ctx, -1);
  59308. act = thr->callstack + thr->callstack_top - 1;
  59309. if (duk_js_declvar_activation(thr, act, name, tv1, prop_flags, is_func_decl)) {
  59310. /* already declared, must update binding value */
  59311. tv1 = duk_get_tval(ctx, -1);
  59312. duk_js_putvar_activation(thr, act, name, tv1, DUK__STRICT());
  59313. }
  59314. duk_pop(ctx);
  59315. break;
  59316. }
  59317. case DUK_OP_DELVAR: {
  59318. duk_activation *act;
  59319. duk_context *ctx = (duk_context *) thr;
  59320. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59321. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59322. duk_tval *tv1;
  59323. duk_hstring *name;
  59324. duk_bool_t rc;
  59325. tv1 = DUK__REGCONSTP(b);
  59326. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  59327. name = DUK_TVAL_GET_STRING(tv1);
  59328. DUK_ASSERT(name != NULL);
  59329. DUK_DDD(DUK_DDDPRINT("DELVAR '%!O'", (duk_heaphdr *) name));
  59330. act = thr->callstack + thr->callstack_top - 1;
  59331. rc = duk_js_delvar_activation(thr, act, name);
  59332. duk_push_boolean(ctx, rc);
  59333. duk_replace(ctx, (duk_idx_t) a);
  59334. break;
  59335. }
  59336. case DUK_OP_CSVAR:
  59337. case DUK_OP_CSVARI: {
  59338. /* 'this' value:
  59339. * E5 Section 6.b.i
  59340. *
  59341. * The only (standard) case where the 'this' binding is non-null is when
  59342. * (1) the variable is found in an object environment record, and
  59343. * (2) that object environment record is a 'with' block.
  59344. *
  59345. */
  59346. duk_context *ctx = (duk_context *) thr;
  59347. duk_activation *act;
  59348. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59349. duk_uint_fast_t idx;
  59350. duk_tval *tv1;
  59351. duk_hstring *name;
  59352. tv1 = DUK__REGCONSTP(b);
  59353. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  59354. name = DUK_TVAL_GET_STRING(tv1);
  59355. DUK_ASSERT(name != NULL);
  59356. act = thr->callstack + thr->callstack_top - 1;
  59357. (void) duk_js_getvar_activation(thr, act, name, 1 /*throw*/); /* -> [... val this] */
  59358. /* Note: target registers a and a+1 may overlap with DUK__REGCONSTP(b)
  59359. * and DUK__REGCONSTP(c). Careful here.
  59360. */
  59361. idx = (duk_uint_fast_t) DUK_DEC_A(ins);
  59362. if (DUK_DEC_OP(ins) == DUK_OP_CSVARI) {
  59363. duk_tval *tv_ind = DUK__REGP(idx);
  59364. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind));
  59365. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind);
  59366. }
  59367. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  59368. if (idx + 2 > (duk_uint_fast_t) duk_get_top(ctx)) {
  59369. /* XXX: use duk_is_valid_index() instead? */
  59370. /* XXX: improve check; check against nregs, not against top */
  59371. DUK__INTERNAL_ERROR("CSVAR out of bounds");
  59372. }
  59373. #endif
  59374. duk_replace(ctx, (duk_idx_t) (idx + 1)); /* 'this' binding */
  59375. duk_replace(ctx, (duk_idx_t) idx); /* variable value (function, we hope, not checked here) */
  59376. break;
  59377. }
  59378. case DUK_OP_CLOSURE: {
  59379. duk_context *ctx = (duk_context *) thr;
  59380. duk_activation *act;
  59381. duk_hcompiledfunction *fun;
  59382. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59383. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  59384. duk_hobject *fun_temp;
  59385. /* A -> target reg
  59386. * BC -> inner function index
  59387. */
  59388. DUK_DDD(DUK_DDDPRINT("CLOSURE to target register %ld, fnum %ld (count %ld)",
  59389. (long) a, (long) bc, (long) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(thr->heap, DUK__FUN())));
  59390. DUK_ASSERT_DISABLE(bc >= 0); /* unsigned */
  59391. DUK_ASSERT((duk_uint_t) bc < (duk_uint_t) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(thr->heap, DUK__FUN()));
  59392. act = thr->callstack + thr->callstack_top - 1;
  59393. fun = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act);
  59394. fun_temp = DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, fun)[bc];
  59395. DUK_ASSERT(fun_temp != NULL);
  59396. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(fun_temp));
  59397. DUK_DDD(DUK_DDDPRINT("CLOSURE: function template is: %p -> %!O",
  59398. (void *) fun_temp, (duk_heaphdr *) fun_temp));
  59399. if (act->lex_env == NULL) {
  59400. DUK_ASSERT(act->var_env == NULL);
  59401. duk_js_init_activation_environment_records_delayed(thr, act);
  59402. }
  59403. DUK_ASSERT(act->lex_env != NULL);
  59404. DUK_ASSERT(act->var_env != NULL);
  59405. /* functions always have a NEWENV flag, i.e. they get a
  59406. * new variable declaration environment, so only lex_env
  59407. * matters here.
  59408. */
  59409. duk_js_push_closure(thr,
  59410. (duk_hcompiledfunction *) fun_temp,
  59411. act->var_env,
  59412. act->lex_env);
  59413. duk_replace(ctx, (duk_idx_t) a);
  59414. break;
  59415. }
  59416. case DUK_OP_GETPROP: {
  59417. duk_context *ctx = (duk_context *) thr;
  59418. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59419. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59420. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  59421. duk_tval *tv_obj;
  59422. duk_tval *tv_key;
  59423. duk_bool_t rc;
  59424. /* A -> target reg
  59425. * B -> object reg/const (may be const e.g. in "'foo'[1]")
  59426. * C -> key reg/const
  59427. */
  59428. tv_obj = DUK__REGCONSTP(b);
  59429. tv_key = DUK__REGCONSTP(c);
  59430. DUK_DDD(DUK_DDDPRINT("GETPROP: a=%ld obj=%!T, key=%!T",
  59431. (long) a,
  59432. (duk_tval *) DUK__REGCONSTP(b),
  59433. (duk_tval *) DUK__REGCONSTP(c)));
  59434. rc = duk_hobject_getprop(thr, tv_obj, tv_key); /* -> [val] */
  59435. DUK_UNREF(rc); /* ignore */
  59436. DUK_DDD(DUK_DDDPRINT("GETPROP --> %!T",
  59437. (duk_tval *) duk_get_tval(ctx, -1)));
  59438. tv_obj = NULL; /* invalidated */
  59439. tv_key = NULL; /* invalidated */
  59440. duk_replace(ctx, (duk_idx_t) a); /* val */
  59441. break;
  59442. }
  59443. case DUK_OP_PUTPROP: {
  59444. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59445. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59446. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  59447. duk_tval *tv_obj;
  59448. duk_tval *tv_key;
  59449. duk_tval *tv_val;
  59450. duk_bool_t rc;
  59451. /* A -> object reg
  59452. * B -> key reg/const
  59453. * C -> value reg/const
  59454. *
  59455. * Note: intentional difference to register arrangement
  59456. * of e.g. GETPROP; 'A' must contain a register-only value.
  59457. */
  59458. tv_obj = DUK__REGP(a);
  59459. tv_key = DUK__REGCONSTP(b);
  59460. tv_val = DUK__REGCONSTP(c);
  59461. DUK_DDD(DUK_DDDPRINT("PUTPROP: obj=%!T, key=%!T, val=%!T",
  59462. (duk_tval *) DUK__REGP(a),
  59463. (duk_tval *) DUK__REGCONSTP(b),
  59464. (duk_tval *) DUK__REGCONSTP(c)));
  59465. rc = duk_hobject_putprop(thr, tv_obj, tv_key, tv_val, DUK__STRICT());
  59466. DUK_UNREF(rc); /* ignore */
  59467. DUK_DDD(DUK_DDDPRINT("PUTPROP --> obj=%!T, key=%!T, val=%!T",
  59468. (duk_tval *) DUK__REGP(a),
  59469. (duk_tval *) DUK__REGCONSTP(b),
  59470. (duk_tval *) DUK__REGCONSTP(c)));
  59471. tv_obj = NULL; /* invalidated */
  59472. tv_key = NULL; /* invalidated */
  59473. tv_val = NULL; /* invalidated */
  59474. break;
  59475. }
  59476. case DUK_OP_DELPROP: {
  59477. duk_context *ctx = (duk_context *) thr;
  59478. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59479. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59480. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  59481. duk_tval *tv_obj;
  59482. duk_tval *tv_key;
  59483. duk_bool_t rc;
  59484. /* A -> result reg
  59485. * B -> object reg
  59486. * C -> key reg/const
  59487. */
  59488. tv_obj = DUK__REGP(b);
  59489. tv_key = DUK__REGCONSTP(c);
  59490. rc = duk_hobject_delprop(thr, tv_obj, tv_key, DUK__STRICT());
  59491. tv_obj = NULL; /* invalidated */
  59492. tv_key = NULL; /* invalidated */
  59493. duk_push_boolean(ctx, rc);
  59494. duk_replace(ctx, (duk_idx_t) a); /* result */
  59495. break;
  59496. }
  59497. case DUK_OP_CSPROP:
  59498. case DUK_OP_CSPROPI: {
  59499. duk_context *ctx = (duk_context *) thr;
  59500. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59501. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  59502. duk_uint_fast_t idx;
  59503. duk_tval *tv_obj;
  59504. duk_tval *tv_key;
  59505. duk_bool_t rc;
  59506. /* E5 Section 11.2.3, step 6.a.i */
  59507. /* E5 Section 10.4.3 */
  59508. /* XXX: allow object to be a const, e.g. in 'foo'.toString()?
  59509. * On the other hand, DUK_REGCONSTP() is slower and generates
  59510. * more code.
  59511. */
  59512. tv_obj = DUK__REGP(b);
  59513. tv_key = DUK__REGCONSTP(c);
  59514. rc = duk_hobject_getprop(thr, tv_obj, tv_key); /* -> [val] */
  59515. DUK_UNREF(rc); /* unused */
  59516. tv_obj = NULL; /* invalidated */
  59517. tv_key = NULL; /* invalidated */
  59518. /* Note: target registers a and a+1 may overlap with DUK__REGP(b)
  59519. * and DUK__REGCONSTP(c). Careful here.
  59520. */
  59521. idx = (duk_uint_fast_t) DUK_DEC_A(ins);
  59522. if (DUK_DEC_OP(ins) == DUK_OP_CSPROPI) {
  59523. duk_tval *tv_ind = DUK__REGP(idx);
  59524. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind));
  59525. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind);
  59526. }
  59527. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  59528. if (idx + 2 > (duk_uint_fast_t) duk_get_top(ctx)) {
  59529. /* XXX: use duk_is_valid_index() instead? */
  59530. /* XXX: improve check; check against nregs, not against top */
  59531. DUK__INTERNAL_ERROR("CSPROP out of bounds");
  59532. }
  59533. #endif
  59534. duk_push_tval(ctx, DUK__REGP(b)); /* [ ... val obj ] */
  59535. duk_replace(ctx, (duk_idx_t) (idx + 1)); /* 'this' binding */
  59536. duk_replace(ctx, (duk_idx_t) idx); /* val */
  59537. break;
  59538. }
  59539. case DUK_OP_ADD:
  59540. case DUK_OP_SUB:
  59541. case DUK_OP_MUL:
  59542. case DUK_OP_DIV:
  59543. case DUK_OP_MOD: {
  59544. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59545. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59546. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  59547. duk_small_uint_fast_t op = DUK_DEC_OP(ins);
  59548. if (op == DUK_OP_ADD) {
  59549. /*
  59550. * Handling DUK_OP_ADD this way is more compact (experimentally)
  59551. * than a separate case with separate argument decoding.
  59552. */
  59553. duk__vm_arith_add(thr, DUK__REGCONSTP(b), DUK__REGCONSTP(c), a);
  59554. } else {
  59555. duk__vm_arith_binary_op(thr, DUK__REGCONSTP(b), DUK__REGCONSTP(c), a, op);
  59556. }
  59557. break;
  59558. }
  59559. case DUK_OP_BAND:
  59560. case DUK_OP_BOR:
  59561. case DUK_OP_BXOR:
  59562. case DUK_OP_BASL:
  59563. case DUK_OP_BLSR:
  59564. case DUK_OP_BASR: {
  59565. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59566. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59567. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  59568. duk_small_uint_fast_t op = DUK_DEC_OP(ins);
  59569. duk__vm_bitwise_binary_op(thr, DUK__REGCONSTP(b), DUK__REGCONSTP(c), a, op);
  59570. break;
  59571. }
  59572. case DUK_OP_EQ:
  59573. case DUK_OP_NEQ: {
  59574. duk_context *ctx = (duk_context *) thr;
  59575. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59576. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59577. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  59578. duk_bool_t tmp;
  59579. /* E5 Sections 11.9.1, 11.9.3 */
  59580. tmp = duk_js_equals(thr, DUK__REGCONSTP(b), DUK__REGCONSTP(c));
  59581. if (DUK_DEC_OP(ins) == DUK_OP_NEQ) {
  59582. tmp = !tmp;
  59583. }
  59584. duk_push_boolean(ctx, tmp);
  59585. duk_replace(ctx, (duk_idx_t) a);
  59586. break;
  59587. }
  59588. case DUK_OP_SEQ:
  59589. case DUK_OP_SNEQ: {
  59590. duk_context *ctx = (duk_context *) thr;
  59591. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59592. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59593. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  59594. duk_bool_t tmp;
  59595. /* E5 Sections 11.9.1, 11.9.3 */
  59596. tmp = duk_js_strict_equals(DUK__REGCONSTP(b), DUK__REGCONSTP(c));
  59597. if (DUK_DEC_OP(ins) == DUK_OP_SNEQ) {
  59598. tmp = !tmp;
  59599. }
  59600. duk_push_boolean(ctx, tmp);
  59601. duk_replace(ctx, (duk_idx_t) a);
  59602. break;
  59603. }
  59604. /* Note: combining comparison ops must be done carefully because
  59605. * of uncomparable values (NaN): it's not necessarily true that
  59606. * (x >= y) === !(x < y). Also, evaluation order matters, and
  59607. * although it would only seem to affect the compiler this is
  59608. * actually not the case, because there are also run-time coercions
  59609. * of the arguments (with potential side effects).
  59610. *
  59611. * XXX: can be combined; check code size.
  59612. */
  59613. case DUK_OP_GT: {
  59614. duk_context *ctx = (duk_context *) thr;
  59615. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59616. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59617. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  59618. duk_bool_t tmp;
  59619. /* x > y --> y < x */
  59620. tmp = duk_js_compare_helper(thr,
  59621. DUK__REGCONSTP(c), /* y */
  59622. DUK__REGCONSTP(b), /* x */
  59623. 0); /* flags */
  59624. duk_push_boolean(ctx, tmp);
  59625. duk_replace(ctx, (duk_idx_t) a);
  59626. break;
  59627. }
  59628. case DUK_OP_GE: {
  59629. duk_context *ctx = (duk_context *) thr;
  59630. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59631. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59632. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  59633. duk_bool_t tmp;
  59634. /* x >= y --> not (x < y) */
  59635. tmp = duk_js_compare_helper(thr,
  59636. DUK__REGCONSTP(b), /* x */
  59637. DUK__REGCONSTP(c), /* y */
  59638. DUK_COMPARE_FLAG_EVAL_LEFT_FIRST |
  59639. DUK_COMPARE_FLAG_NEGATE); /* flags */
  59640. duk_push_boolean(ctx, tmp);
  59641. duk_replace(ctx, (duk_idx_t) a);
  59642. break;
  59643. }
  59644. case DUK_OP_LT: {
  59645. duk_context *ctx = (duk_context *) thr;
  59646. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59647. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59648. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  59649. duk_bool_t tmp;
  59650. /* x < y */
  59651. tmp = duk_js_compare_helper(thr,
  59652. DUK__REGCONSTP(b), /* x */
  59653. DUK__REGCONSTP(c), /* y */
  59654. DUK_COMPARE_FLAG_EVAL_LEFT_FIRST); /* flags */
  59655. duk_push_boolean(ctx, tmp);
  59656. duk_replace(ctx, (duk_idx_t) a);
  59657. break;
  59658. }
  59659. case DUK_OP_LE: {
  59660. duk_context *ctx = (duk_context *) thr;
  59661. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59662. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59663. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  59664. duk_bool_t tmp;
  59665. /* x <= y --> not (x > y) --> not (y < x) */
  59666. tmp = duk_js_compare_helper(thr,
  59667. DUK__REGCONSTP(c), /* y */
  59668. DUK__REGCONSTP(b), /* x */
  59669. DUK_COMPARE_FLAG_NEGATE); /* flags */
  59670. duk_push_boolean(ctx, tmp);
  59671. duk_replace(ctx, (duk_idx_t) a);
  59672. break;
  59673. }
  59674. case DUK_OP_IF: {
  59675. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59676. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59677. duk_bool_t tmp;
  59678. tmp = duk_js_toboolean(DUK__REGCONSTP(b));
  59679. if (tmp == (duk_bool_t) a) {
  59680. /* if boolean matches A, skip next inst */
  59681. curr_pc++;
  59682. } else {
  59683. ;
  59684. }
  59685. break;
  59686. }
  59687. case DUK_OP_JUMP: {
  59688. duk_int_fast_t abc = DUK_DEC_ABC(ins);
  59689. curr_pc += abc - DUK_BC_JUMP_BIAS;
  59690. break;
  59691. }
  59692. case DUK_OP_RETURN: {
  59693. duk_context *ctx = (duk_context *) thr;
  59694. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59695. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  59696. /* duk_small_uint_fast_t c = DUK_DEC_C(ins); */
  59697. duk_small_uint_t ret_result;
  59698. /* A -> flags
  59699. * B -> return value reg/const
  59700. * C -> currently unused
  59701. */
  59702. DUK__SYNC_AND_NULL_CURR_PC();
  59703. /* duk__handle_return() is guaranteed never to throw, except
  59704. * for potential out-of-memory situations which will then
  59705. * propagate out of the executor longjmp handler.
  59706. */
  59707. if (a & DUK_BC_RETURN_FLAG_HAVE_RETVAL) {
  59708. duk_push_tval(ctx, DUK__REGCONSTP(b));
  59709. } else {
  59710. duk_push_undefined(ctx);
  59711. }
  59712. ret_result = duk__handle_return(thr,
  59713. entry_thread,
  59714. entry_callstack_top);
  59715. if (ret_result == DUK__RETHAND_RESTART) {
  59716. goto restart_execution;
  59717. }
  59718. DUK_ASSERT(ret_result == DUK__RETHAND_FINISHED);
  59719. DUK_DDD(DUK_DDDPRINT("exiting executor after RETURN handling"));
  59720. return;
  59721. }
  59722. case DUK_OP_CALL:
  59723. case DUK_OP_CALLI: {
  59724. duk_context *ctx = (duk_context *) thr;
  59725. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  59726. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  59727. duk_uint_fast_t idx;
  59728. duk_small_uint_t call_flags;
  59729. duk_small_uint_t flag_tailcall;
  59730. duk_small_uint_t flag_evalcall;
  59731. duk_tval *tv_func;
  59732. duk_hobject *obj_func;
  59733. duk_bool_t setup_rc;
  59734. duk_idx_t num_stack_args;
  59735. #if !defined(DUK_USE_EXEC_FUN_LOCAL)
  59736. duk_hcompiledfunction *fun;
  59737. #endif
  59738. /* A -> flags
  59739. * B -> base register for call (base -> func, base+1 -> this, base+2 -> arg1 ... base+2+N-1 -> argN)
  59740. * (for DUK_OP_CALLI, 'b' is indirect)
  59741. * C -> nargs
  59742. */
  59743. /* these are not necessarily 0 or 1 (may be other non-zero), that's ok */
  59744. flag_tailcall = (a & DUK_BC_CALL_FLAG_TAILCALL);
  59745. flag_evalcall = (a & DUK_BC_CALL_FLAG_EVALCALL);
  59746. idx = (duk_uint_fast_t) DUK_DEC_B(ins);
  59747. if (DUK_DEC_OP(ins) == DUK_OP_CALLI) {
  59748. duk_tval *tv_ind = DUK__REGP(idx);
  59749. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind));
  59750. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind);
  59751. }
  59752. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  59753. if (!duk_is_valid_index(ctx, (duk_idx_t) idx)) {
  59754. /* XXX: improve check; check against nregs, not against top */
  59755. DUK__INTERNAL_ERROR("CALL out of bounds");
  59756. }
  59757. #endif
  59758. /*
  59759. * To determine whether to use an optimized Ecmascript-to-Ecmascript
  59760. * call, we need to know whether the final, non-bound function is an
  59761. * Ecmascript function.
  59762. *
  59763. * This is now implemented so that we start to do an ecma-to-ecma call
  59764. * setup which will resolve the bound chain as the first thing. If the
  59765. * final function is not eligible, the return value indicates that the
  59766. * ecma-to-ecma call is not possible. The setup will overwrite the call
  59767. * target at DUK__REGP(idx) with the final, non-bound function (which
  59768. * may be a lightfunc), and fudge arguments if necessary.
  59769. *
  59770. * XXX: If an ecma-to-ecma call is not possible, this initial call
  59771. * setup will do bound function chain resolution but won't do the
  59772. * "effective this binding" resolution which is quite confusing.
  59773. * Perhaps add a helper for doing bound function and effective this
  59774. * binding resolution - and call that explicitly? Ecma-to-ecma call
  59775. * setup and normal function handling can then assume this prestep has
  59776. * been done by the caller.
  59777. */
  59778. duk_set_top(ctx, (duk_idx_t) (idx + c + 2)); /* [ ... func this arg1 ... argN ] */
  59779. call_flags = 0;
  59780. if (flag_tailcall) {
  59781. /* We request a tail call, but in some corner cases
  59782. * call handling can decide that a tail call is
  59783. * actually not possible.
  59784. * See: test-bug-tailcall-preventyield-assert.c.
  59785. */
  59786. call_flags |= DUK_CALL_FLAG_IS_TAILCALL;
  59787. }
  59788. /* Compared to duk_handle_call():
  59789. * - protected call: never
  59790. * - ignore recursion limit: never
  59791. */
  59792. num_stack_args = c;
  59793. setup_rc = duk_handle_ecma_call_setup(thr,
  59794. num_stack_args,
  59795. call_flags);
  59796. if (setup_rc) {
  59797. /* Ecma-to-ecma call possible, may or may not be a tail call.
  59798. * Avoid C recursion by being clever.
  59799. */
  59800. DUK_DDD(DUK_DDDPRINT("ecma-to-ecma call setup possible, restart execution"));
  59801. /* curr_pc synced by duk_handle_ecma_call_setup() */
  59802. goto restart_execution;
  59803. }
  59804. DUK_ASSERT(thr->ptr_curr_pc != NULL); /* restored if ecma-to-ecma setup fails */
  59805. DUK_DDD(DUK_DDDPRINT("ecma-to-ecma call not possible, target is native (may be lightfunc)"));
  59806. /* Recompute argument count: bound function handling may have shifted. */
  59807. num_stack_args = duk_get_top(ctx) - (idx + 2);
  59808. DUK_DDD(DUK_DDDPRINT("recomputed arg count: %ld\n", (long) num_stack_args));
  59809. tv_func = DUK__REGP(idx); /* Relookup if relocated */
  59810. if (DUK_TVAL_IS_LIGHTFUNC(tv_func)) {
  59811. call_flags = 0; /* not protected, respect reclimit, not constructor */
  59812. /* There is no eval() special handling here: eval() is never
  59813. * automatically converted to a lightfunc.
  59814. */
  59815. DUK_ASSERT(DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv_func) != duk_bi_global_object_eval);
  59816. duk_handle_call(thr,
  59817. num_stack_args,
  59818. call_flags);
  59819. /* duk_js_call.c is required to restore the stack reserve
  59820. * so we only need to reset the top.
  59821. */
  59822. #if !defined(DUK_USE_EXEC_FUN_LOCAL)
  59823. fun = DUK__FUN();
  59824. #endif
  59825. duk_set_top(ctx, (duk_idx_t) fun->nregs);
  59826. /* No need to reinit setjmp() catchpoint, as call handling
  59827. * will store and restore our state.
  59828. */
  59829. } else {
  59830. /* Call setup checks callability. */
  59831. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv_func));
  59832. obj_func = DUK_TVAL_GET_OBJECT(tv_func);
  59833. DUK_ASSERT(obj_func != NULL);
  59834. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(obj_func));
  59835. /*
  59836. * Other cases, use C recursion.
  59837. *
  59838. * If a tail call was requested we ignore it and execute a normal call.
  59839. * Since Duktape 0.11.0 the compiler emits a RETURN opcode even after
  59840. * a tail call to avoid test-bug-tailcall-thread-yield-resume.js.
  59841. *
  59842. * Direct eval call: (1) call target (before following bound function
  59843. * chain) is the built-in eval() function, and (2) call was made with
  59844. * the identifier 'eval'.
  59845. */
  59846. call_flags = 0; /* not protected, respect reclimit, not constructor */
  59847. if (DUK_HOBJECT_IS_NATIVEFUNCTION(obj_func) &&
  59848. ((duk_hnativefunction *) obj_func)->func == duk_bi_global_object_eval) {
  59849. if (flag_evalcall) {
  59850. DUK_DDD(DUK_DDDPRINT("call target is eval, call identifier was 'eval' -> direct eval"));
  59851. call_flags |= DUK_CALL_FLAG_DIRECT_EVAL;
  59852. } else {
  59853. DUK_DDD(DUK_DDDPRINT("call target is eval, call identifier was not 'eval' -> indirect eval"));
  59854. }
  59855. }
  59856. duk_handle_call(thr,
  59857. num_stack_args,
  59858. call_flags);
  59859. /* duk_js_call.c is required to restore the stack reserve
  59860. * so we only need to reset the top.
  59861. */
  59862. #if !defined(DUK_USE_EXEC_FUN_LOCAL)
  59863. fun = DUK__FUN();
  59864. #endif
  59865. duk_set_top(ctx, (duk_idx_t) fun->nregs);
  59866. /* No need to reinit setjmp() catchpoint, as call handling
  59867. * will store and restore our state.
  59868. */
  59869. }
  59870. /* When debugger is enabled, we need to recheck the activation
  59871. * status after returning. This is now handled by call handling
  59872. * and heap->dbg_force_restart.
  59873. */
  59874. break;
  59875. }
  59876. case DUK_OP_TRYCATCH: {
  59877. duk_context *ctx = (duk_context *) thr;
  59878. duk_activation *act;
  59879. duk_catcher *cat;
  59880. duk_tval *tv1;
  59881. duk_small_uint_fast_t a;
  59882. duk_uint_fast_t bc;
  59883. /* A -> flags
  59884. * BC -> reg_catch; base register for two registers used both during
  59885. * trycatch setup and when catch is triggered
  59886. *
  59887. * If DUK_BC_TRYCATCH_FLAG_CATCH_BINDING set:
  59888. * reg_catch + 0: catch binding variable name (string).
  59889. * Automatic declarative environment is established for
  59890. * the duration of the 'catch' clause.
  59891. *
  59892. * If DUK_BC_TRYCATCH_FLAG_WITH_BINDING set:
  59893. * reg_catch + 0: with 'target value', which is coerced to
  59894. * an object and then used as a bindind object for an
  59895. * environment record. The binding is initialized here, for
  59896. * the 'try' clause.
  59897. *
  59898. * Note that a TRYCATCH generated for a 'with' statement has no
  59899. * catch or finally parts.
  59900. */
  59901. /* XXX: TRYCATCH handling should be reworked to avoid creating
  59902. * an explicit scope unless it is actually needed (e.g. function
  59903. * instances or eval is executed inside the catch block). This
  59904. * rework is not trivial because the compiler doesn't have an
  59905. * intermediate representation. When the rework is done, the
  59906. * opcode format can also be made more straightforward.
  59907. */
  59908. /* XXX: side effect handling is quite awkward here */
  59909. DUK_DDD(DUK_DDDPRINT("TRYCATCH: reg_catch=%ld, have_catch=%ld, "
  59910. "have_finally=%ld, catch_binding=%ld, with_binding=%ld (flags=0x%02lx)",
  59911. (long) DUK_DEC_BC(ins),
  59912. (long) (DUK_DEC_A(ins) & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH ? 1 : 0),
  59913. (long) (DUK_DEC_A(ins) & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY ? 1 : 0),
  59914. (long) (DUK_DEC_A(ins) & DUK_BC_TRYCATCH_FLAG_CATCH_BINDING ? 1 : 0),
  59915. (long) (DUK_DEC_A(ins) & DUK_BC_TRYCATCH_FLAG_WITH_BINDING ? 1 : 0),
  59916. (unsigned long) DUK_DEC_A(ins)));
  59917. a = DUK_DEC_A(ins);
  59918. bc = DUK_DEC_BC(ins);
  59919. act = thr->callstack + thr->callstack_top - 1;
  59920. DUK_ASSERT(thr->callstack_top >= 1);
  59921. /* 'with' target must be created first, in case we run out of memory */
  59922. /* XXX: refactor out? */
  59923. if (a & DUK_BC_TRYCATCH_FLAG_WITH_BINDING) {
  59924. DUK_DDD(DUK_DDDPRINT("need to initialize a with binding object"));
  59925. if (act->lex_env == NULL) {
  59926. DUK_ASSERT(act->var_env == NULL);
  59927. DUK_DDD(DUK_DDDPRINT("delayed environment initialization"));
  59928. /* must relookup act in case of side effects */
  59929. duk_js_init_activation_environment_records_delayed(thr, act);
  59930. act = thr->callstack + thr->callstack_top - 1;
  59931. }
  59932. DUK_ASSERT(act->lex_env != NULL);
  59933. DUK_ASSERT(act->var_env != NULL);
  59934. (void) duk_push_object_helper(ctx,
  59935. DUK_HOBJECT_FLAG_EXTENSIBLE |
  59936. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJENV),
  59937. -1); /* no prototype, updated below */
  59938. duk_push_tval(ctx, DUK__REGP(bc));
  59939. duk_to_object(ctx, -1);
  59940. duk_dup(ctx, -1);
  59941. /* [ ... env target ] */
  59942. /* [ ... env target target ] */
  59943. duk_xdef_prop_stridx(thr, -3, DUK_STRIDX_INT_TARGET, DUK_PROPDESC_FLAGS_NONE);
  59944. duk_xdef_prop_stridx(thr, -2, DUK_STRIDX_INT_THIS, DUK_PROPDESC_FLAGS_NONE); /* always provideThis=true */
  59945. /* [ ... env ] */
  59946. DUK_DDD(DUK_DDDPRINT("environment for with binding: %!iT",
  59947. (duk_tval *) duk_get_tval(ctx, -1)));
  59948. }
  59949. /* allocate catcher and populate it (should be atomic) */
  59950. duk_hthread_catchstack_grow(thr);
  59951. cat = thr->catchstack + thr->catchstack_top;
  59952. DUK_ASSERT(thr->catchstack_top + 1 <= thr->catchstack_size);
  59953. thr->catchstack_top++;
  59954. cat->flags = DUK_CAT_TYPE_TCF;
  59955. cat->h_varname = NULL;
  59956. if (a & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH) {
  59957. cat->flags |= DUK_CAT_FLAG_CATCH_ENABLED;
  59958. }
  59959. if (a & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY) {
  59960. cat->flags |= DUK_CAT_FLAG_FINALLY_ENABLED;
  59961. }
  59962. if (a & DUK_BC_TRYCATCH_FLAG_CATCH_BINDING) {
  59963. DUK_DDD(DUK_DDDPRINT("catch binding flag set to catcher"));
  59964. cat->flags |= DUK_CAT_FLAG_CATCH_BINDING_ENABLED;
  59965. tv1 = DUK__REGP(bc);
  59966. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  59967. /* borrowed reference; although 'tv1' comes from a register,
  59968. * its value was loaded using LDCONST so the constant will
  59969. * also exist and be reachable.
  59970. */
  59971. cat->h_varname = DUK_TVAL_GET_STRING(tv1);
  59972. } else if (a & DUK_BC_TRYCATCH_FLAG_WITH_BINDING) {
  59973. /* env created above to stack top */
  59974. duk_hobject *new_env;
  59975. DUK_DDD(DUK_DDDPRINT("lexenv active flag set to catcher"));
  59976. cat->flags |= DUK_CAT_FLAG_LEXENV_ACTIVE;
  59977. DUK_DDD(DUK_DDDPRINT("activating object env: %!iT",
  59978. (duk_tval *) duk_get_tval(ctx, -1)));
  59979. DUK_ASSERT(act->lex_env != NULL);
  59980. new_env = duk_get_hobject(ctx, -1);
  59981. DUK_ASSERT(new_env != NULL);
  59982. act = thr->callstack + thr->callstack_top - 1; /* relookup (side effects) */
  59983. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, new_env, act->lex_env); /* side effects */
  59984. act = thr->callstack + thr->callstack_top - 1; /* relookup (side effects) */
  59985. act->lex_env = new_env;
  59986. DUK_HOBJECT_INCREF(thr, new_env);
  59987. duk_pop(ctx);
  59988. } else {
  59989. ;
  59990. }
  59991. /* Registers 'bc' and 'bc + 1' are written in longjmp handling
  59992. * and if their previous values (which are temporaries) become
  59993. * unreachable -and- have a finalizer, there'll be a function
  59994. * call during error handling which is not supported now (GH-287).
  59995. * Ensure that both 'bc' and 'bc + 1' have primitive values to
  59996. * guarantee no finalizer calls in error handling. Scrubbing also
  59997. * ensures finalizers for the previous values run here rather than
  59998. * later. Error handling related values are also written to 'bc'
  59999. * and 'bc + 1' but those values never become unreachable during
  60000. * error handling, so there's no side effect problem even if the
  60001. * error value has a finalizer.
  60002. */
  60003. duk_to_undefined(ctx, bc);
  60004. duk_to_undefined(ctx, bc + 1);
  60005. cat = thr->catchstack + thr->catchstack_top - 1; /* relookup (side effects) */
  60006. cat->callstack_index = thr->callstack_top - 1;
  60007. cat->pc_base = (duk_instr_t *) curr_pc; /* pre-incremented, points to first jump slot */
  60008. cat->idx_base = (duk_size_t) (thr->valstack_bottom - thr->valstack) + bc;
  60009. DUK_DDD(DUK_DDDPRINT("TRYCATCH catcher: flags=0x%08lx, callstack_index=%ld, pc_base=%ld, "
  60010. "idx_base=%ld, h_varname=%!O",
  60011. (unsigned long) cat->flags, (long) cat->callstack_index,
  60012. (long) cat->pc_base, (long) cat->idx_base, (duk_heaphdr *) cat->h_varname));
  60013. curr_pc += 2; /* skip jump slots */
  60014. break;
  60015. }
  60016. /* Pre/post inc/dec for register variables, important for loops. */
  60017. case DUK_OP_PREINCR:
  60018. case DUK_OP_PREDECR:
  60019. case DUK_OP_POSTINCR:
  60020. case DUK_OP_POSTDECR: {
  60021. duk_context *ctx = (duk_context *) thr;
  60022. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  60023. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  60024. duk_tval *tv1, *tv2;
  60025. duk_double_t x, y, z;
  60026. /* Two lowest bits of opcode are used to distinguish
  60027. * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
  60028. */
  60029. DUK_ASSERT((DUK_OP_PREINCR & 0x03) == 0x00);
  60030. DUK_ASSERT((DUK_OP_PREDECR & 0x03) == 0x01);
  60031. DUK_ASSERT((DUK_OP_POSTINCR & 0x03) == 0x02);
  60032. DUK_ASSERT((DUK_OP_POSTDECR & 0x03) == 0x03);
  60033. tv1 = DUK__REGP(bc);
  60034. #if defined(DUK_USE_FASTINT)
  60035. if (DUK_TVAL_IS_FASTINT(tv1)) {
  60036. duk_int64_t x_fi, y_fi, z_fi;
  60037. x_fi = DUK_TVAL_GET_FASTINT(tv1);
  60038. if (ins & DUK_ENC_OP(0x01)) {
  60039. if (x_fi == DUK_FASTINT_MIN) {
  60040. goto skip_fastint;
  60041. }
  60042. y_fi = x_fi - 1;
  60043. } else {
  60044. if (x_fi == DUK_FASTINT_MAX) {
  60045. goto skip_fastint;
  60046. }
  60047. y_fi = x_fi + 1;
  60048. }
  60049. DUK_TVAL_SET_FASTINT(tv1, y_fi); /* no need for refcount update */
  60050. tv2 = DUK__REGP(a);
  60051. z_fi = (ins & DUK_ENC_OP(0x02)) ? x_fi : y_fi;
  60052. DUK_TVAL_SET_FASTINT_UPDREF(thr, tv2, z_fi); /* side effects */
  60053. break;
  60054. }
  60055. skip_fastint:
  60056. #endif
  60057. if (DUK_TVAL_IS_NUMBER(tv1)) {
  60058. /* Fast path for the case where the register
  60059. * is a number (e.g. loop counter).
  60060. */
  60061. x = DUK_TVAL_GET_NUMBER(tv1);
  60062. if (ins & DUK_ENC_OP(0x01)) {
  60063. y = x - 1.0;
  60064. } else {
  60065. y = x + 1.0;
  60066. }
  60067. DUK_TVAL_SET_NUMBER(tv1, y); /* no need for refcount update */
  60068. } else {
  60069. x = duk_to_number(ctx, bc);
  60070. if (ins & DUK_ENC_OP(0x01)) {
  60071. y = x - 1.0;
  60072. } else {
  60073. y = x + 1.0;
  60074. }
  60075. duk_push_number(ctx, y);
  60076. duk_replace(ctx, bc);
  60077. }
  60078. tv2 = DUK__REGP(a);
  60079. z = (ins & DUK_ENC_OP(0x02)) ? x : y;
  60080. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv2, z); /* side effects */
  60081. break;
  60082. }
  60083. /* Preinc/predec for var-by-name, slow path. */
  60084. case DUK_OP_PREINCV:
  60085. case DUK_OP_PREDECV:
  60086. case DUK_OP_POSTINCV:
  60087. case DUK_OP_POSTDECV: {
  60088. duk_context *ctx = (duk_context *) thr;
  60089. duk_activation *act;
  60090. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  60091. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  60092. duk_double_t x, y;
  60093. duk_tval *tv1;
  60094. duk_hstring *name;
  60095. /* Two lowest bits of opcode are used to distinguish
  60096. * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
  60097. */
  60098. DUK_ASSERT((DUK_OP_PREINCV & 0x03) == 0x00);
  60099. DUK_ASSERT((DUK_OP_PREDECV & 0x03) == 0x01);
  60100. DUK_ASSERT((DUK_OP_POSTINCV & 0x03) == 0x02);
  60101. DUK_ASSERT((DUK_OP_POSTDECV & 0x03) == 0x03);
  60102. tv1 = DUK__CONSTP(bc);
  60103. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  60104. name = DUK_TVAL_GET_STRING(tv1);
  60105. DUK_ASSERT(name != NULL);
  60106. act = thr->callstack + thr->callstack_top - 1;
  60107. (void) duk_js_getvar_activation(thr, act, name, 1 /*throw*/); /* -> [... val this] */
  60108. /* XXX: fastint fast path would be very useful here */
  60109. x = duk_to_number(ctx, -2);
  60110. duk_pop_2(ctx);
  60111. if (ins & DUK_ENC_OP(0x01)) {
  60112. y = x - 1.0;
  60113. } else {
  60114. y = x + 1.0;
  60115. }
  60116. duk_push_number(ctx, y);
  60117. tv1 = duk_get_tval(ctx, -1);
  60118. DUK_ASSERT(tv1 != NULL);
  60119. duk_js_putvar_activation(thr, act, name, tv1, DUK__STRICT());
  60120. duk_pop(ctx);
  60121. duk_push_number(ctx, (ins & DUK_ENC_OP(0x02)) ? x : y);
  60122. duk_replace(ctx, (duk_idx_t) a);
  60123. break;
  60124. }
  60125. /* Preinc/predec for object properties. */
  60126. case DUK_OP_PREINCP:
  60127. case DUK_OP_PREDECP:
  60128. case DUK_OP_POSTINCP:
  60129. case DUK_OP_POSTDECP: {
  60130. duk_context *ctx = (duk_context *) thr;
  60131. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  60132. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  60133. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  60134. duk_tval *tv_obj;
  60135. duk_tval *tv_key;
  60136. duk_tval *tv_val;
  60137. duk_bool_t rc;
  60138. duk_double_t x, y;
  60139. /* A -> target reg
  60140. * B -> object reg/const (may be const e.g. in "'foo'[1]")
  60141. * C -> key reg/const
  60142. */
  60143. /* Two lowest bits of opcode are used to distinguish
  60144. * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
  60145. */
  60146. DUK_ASSERT((DUK_OP_PREINCP & 0x03) == 0x00);
  60147. DUK_ASSERT((DUK_OP_PREDECP & 0x03) == 0x01);
  60148. DUK_ASSERT((DUK_OP_POSTINCP & 0x03) == 0x02);
  60149. DUK_ASSERT((DUK_OP_POSTDECP & 0x03) == 0x03);
  60150. tv_obj = DUK__REGCONSTP(b);
  60151. tv_key = DUK__REGCONSTP(c);
  60152. rc = duk_hobject_getprop(thr, tv_obj, tv_key); /* -> [val] */
  60153. DUK_UNREF(rc); /* ignore */
  60154. tv_obj = NULL; /* invalidated */
  60155. tv_key = NULL; /* invalidated */
  60156. x = duk_to_number(ctx, -1);
  60157. duk_pop(ctx);
  60158. if (ins & DUK_ENC_OP(0x01)) {
  60159. y = x - 1.0;
  60160. } else {
  60161. y = x + 1.0;
  60162. }
  60163. duk_push_number(ctx, y);
  60164. tv_val = duk_get_tval(ctx, -1);
  60165. DUK_ASSERT(tv_val != NULL);
  60166. tv_obj = DUK__REGCONSTP(b);
  60167. tv_key = DUK__REGCONSTP(c);
  60168. rc = duk_hobject_putprop(thr, tv_obj, tv_key, tv_val, DUK__STRICT());
  60169. DUK_UNREF(rc); /* ignore */
  60170. tv_obj = NULL; /* invalidated */
  60171. tv_key = NULL; /* invalidated */
  60172. duk_pop(ctx);
  60173. duk_push_number(ctx, (ins & DUK_ENC_OP(0x02)) ? x : y);
  60174. duk_replace(ctx, (duk_idx_t) a);
  60175. break;
  60176. }
  60177. case DUK_OP_EXTRA: {
  60178. /* XXX: shared decoding of 'b' and 'c'? */
  60179. duk_small_uint_fast_t extraop = DUK_DEC_A(ins);
  60180. switch ((int) extraop) {
  60181. /* XXX: switch cast? */
  60182. case DUK_EXTRAOP_NOP: {
  60183. /* nop */
  60184. break;
  60185. }
  60186. case DUK_EXTRAOP_INVALID: {
  60187. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, "INVALID opcode (%ld)", (long) DUK_DEC_BC(ins));
  60188. break;
  60189. }
  60190. case DUK_EXTRAOP_LDTHIS: {
  60191. /* Note: 'this' may be bound to any value, not just an object */
  60192. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  60193. duk_tval *tv1, *tv2;
  60194. tv1 = DUK__REGP(bc);
  60195. tv2 = thr->valstack_bottom - 1; /* 'this binding' is just under bottom */
  60196. DUK_ASSERT(tv2 >= thr->valstack);
  60197. DUK_DDD(DUK_DDDPRINT("LDTHIS: %!T to r%ld", (duk_tval *) tv2, (long) bc));
  60198. DUK_TVAL_SET_TVAL_UPDREF_FAST(thr, tv1, tv2); /* side effects */
  60199. break;
  60200. }
  60201. case DUK_EXTRAOP_LDUNDEF: {
  60202. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  60203. duk_tval *tv1;
  60204. tv1 = DUK__REGP(bc);
  60205. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv1); /* side effects */
  60206. break;
  60207. }
  60208. case DUK_EXTRAOP_LDNULL: {
  60209. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  60210. duk_tval *tv1;
  60211. tv1 = DUK__REGP(bc);
  60212. DUK_TVAL_SET_NULL_UPDREF(thr, tv1); /* side effects */
  60213. break;
  60214. }
  60215. case DUK_EXTRAOP_LDTRUE:
  60216. case DUK_EXTRAOP_LDFALSE: {
  60217. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  60218. duk_tval *tv1;
  60219. duk_small_uint_fast_t bval = (extraop == DUK_EXTRAOP_LDTRUE ? 1 : 0);
  60220. tv1 = DUK__REGP(bc);
  60221. DUK_TVAL_SET_BOOLEAN_UPDREF(thr, tv1, bval); /* side effects */
  60222. break;
  60223. }
  60224. case DUK_EXTRAOP_NEWOBJ: {
  60225. duk_context *ctx = (duk_context *) thr;
  60226. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  60227. duk_push_object(ctx);
  60228. duk_replace(ctx, (duk_idx_t) b);
  60229. break;
  60230. }
  60231. case DUK_EXTRAOP_NEWARR: {
  60232. duk_context *ctx = (duk_context *) thr;
  60233. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  60234. duk_push_array(ctx);
  60235. duk_replace(ctx, (duk_idx_t) b);
  60236. break;
  60237. }
  60238. case DUK_EXTRAOP_SETALEN: {
  60239. duk_small_uint_fast_t b;
  60240. duk_small_uint_fast_t c;
  60241. duk_tval *tv1;
  60242. duk_hobject *h;
  60243. duk_uint32_t len;
  60244. b = DUK_DEC_B(ins); tv1 = DUK__REGP(b);
  60245. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1));
  60246. h = DUK_TVAL_GET_OBJECT(tv1);
  60247. c = DUK_DEC_C(ins); tv1 = DUK__REGP(c);
  60248. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1));
  60249. len = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv1);
  60250. duk_hobject_set_length(thr, h, len);
  60251. break;
  60252. }
  60253. case DUK_EXTRAOP_TYPEOF: {
  60254. duk_context *ctx = (duk_context *) thr;
  60255. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  60256. duk_push_hstring(ctx, duk_js_typeof(thr, DUK__REGP(bc)));
  60257. duk_replace(ctx, (duk_idx_t) bc);
  60258. break;
  60259. }
  60260. case DUK_EXTRAOP_TYPEOFID: {
  60261. duk_context *ctx = (duk_context *) thr;
  60262. duk_activation *act;
  60263. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  60264. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  60265. duk_hstring *name;
  60266. duk_tval *tv;
  60267. /* B -> target register
  60268. * C -> constant index of identifier name
  60269. */
  60270. tv = DUK__REGCONSTP(c); /* XXX: this could be a DUK__CONSTP instead */
  60271. DUK_ASSERT(DUK_TVAL_IS_STRING(tv));
  60272. name = DUK_TVAL_GET_STRING(tv);
  60273. act = thr->callstack + thr->callstack_top - 1;
  60274. if (duk_js_getvar_activation(thr, act, name, 0 /*throw*/)) {
  60275. /* -> [... val this] */
  60276. tv = duk_get_tval(ctx, -2);
  60277. duk_push_hstring(ctx, duk_js_typeof(thr, tv));
  60278. duk_replace(ctx, (duk_idx_t) b);
  60279. duk_pop_2(ctx);
  60280. } else {
  60281. /* unresolvable, no stack changes */
  60282. duk_push_hstring_stridx(ctx, DUK_STRIDX_LC_UNDEFINED);
  60283. duk_replace(ctx, (duk_idx_t) b);
  60284. }
  60285. break;
  60286. }
  60287. case DUK_EXTRAOP_INITENUM: {
  60288. duk_context *ctx = (duk_context *) thr;
  60289. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  60290. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  60291. /*
  60292. * Enumeration semantics come from for-in statement, E5 Section 12.6.4.
  60293. * If called with 'null' or 'undefined', this opcode returns 'null' as
  60294. * the enumerator, which is special cased in NEXTENUM. This simplifies
  60295. * the compiler part
  60296. */
  60297. /* B -> register for writing enumerator object
  60298. * C -> value to be enumerated (register)
  60299. */
  60300. if (duk_is_null_or_undefined(ctx, (duk_idx_t) c)) {
  60301. duk_push_null(ctx);
  60302. duk_replace(ctx, (duk_idx_t) b);
  60303. } else {
  60304. duk_dup(ctx, (duk_idx_t) c);
  60305. duk_to_object(ctx, -1);
  60306. duk_hobject_enumerator_create(ctx, 0 /*enum_flags*/); /* [ ... val ] --> [ ... enum ] */
  60307. duk_replace(ctx, (duk_idx_t) b);
  60308. }
  60309. break;
  60310. }
  60311. case DUK_EXTRAOP_NEXTENUM: {
  60312. duk_context *ctx = (duk_context *) thr;
  60313. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  60314. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  60315. /*
  60316. * NEXTENUM checks whether the enumerator still has unenumerated
  60317. * keys. If so, the next key is loaded to the target register
  60318. * and the next instruction is skipped. Otherwise the next instruction
  60319. * will be executed, jumping out of the enumeration loop.
  60320. */
  60321. /* B -> target register for next key
  60322. * C -> enum register
  60323. */
  60324. DUK_DDD(DUK_DDDPRINT("NEXTENUM: b->%!T, c->%!T",
  60325. (duk_tval *) duk_get_tval(ctx, (duk_idx_t) b),
  60326. (duk_tval *) duk_get_tval(ctx, (duk_idx_t) c)));
  60327. if (duk_is_object(ctx, (duk_idx_t) c)) {
  60328. /* XXX: assert 'c' is an enumerator */
  60329. duk_dup(ctx, (duk_idx_t) c);
  60330. if (duk_hobject_enumerator_next(ctx, 0 /*get_value*/)) {
  60331. /* [ ... enum ] -> [ ... next_key ] */
  60332. DUK_DDD(DUK_DDDPRINT("enum active, next key is %!T, skip jump slot ",
  60333. (duk_tval *) duk_get_tval(ctx, -1)));
  60334. curr_pc++;
  60335. } else {
  60336. /* [ ... enum ] -> [ ... ] */
  60337. DUK_DDD(DUK_DDDPRINT("enum finished, execute jump slot"));
  60338. duk_push_undefined(ctx);
  60339. }
  60340. duk_replace(ctx, (duk_idx_t) b);
  60341. } else {
  60342. /* 'null' enumerator case -> behave as with an empty enumerator */
  60343. DUK_ASSERT(duk_is_null(ctx, (duk_idx_t) c));
  60344. DUK_DDD(DUK_DDDPRINT("enum is null, execute jump slot"));
  60345. }
  60346. break;
  60347. }
  60348. case DUK_EXTRAOP_INITSET:
  60349. case DUK_EXTRAOP_INITSETI:
  60350. case DUK_EXTRAOP_INITGET:
  60351. case DUK_EXTRAOP_INITGETI: {
  60352. duk_context *ctx = (duk_context *) thr;
  60353. duk_bool_t is_set = (extraop == DUK_EXTRAOP_INITSET || extraop == DUK_EXTRAOP_INITSETI);
  60354. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  60355. duk_uint_fast_t idx;
  60356. /* B -> object register
  60357. * C -> C+0 contains key, C+1 closure (value)
  60358. */
  60359. /*
  60360. * INITSET/INITGET are only used to initialize object literal keys.
  60361. * The compiler ensures that there cannot be a previous data property
  60362. * of the same name. It also ensures that setter and getter can only
  60363. * be initialized once (or not at all).
  60364. */
  60365. idx = (duk_uint_fast_t) DUK_DEC_C(ins);
  60366. if (extraop == DUK_EXTRAOP_INITSETI || extraop == DUK_EXTRAOP_INITGETI) {
  60367. duk_tval *tv_ind = DUK__REGP(idx);
  60368. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind));
  60369. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind);
  60370. }
  60371. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  60372. if (idx + 2 > (duk_uint_fast_t) duk_get_top(ctx)) {
  60373. /* XXX: use duk_is_valid_index() instead? */
  60374. /* XXX: improve check; check against nregs, not against top */
  60375. DUK__INTERNAL_ERROR("INITSET/INITGET out of bounds");
  60376. }
  60377. #endif
  60378. /* XXX: this is now a very unoptimal implementation -- this can be
  60379. * made very simple by direct manipulation of the object internals,
  60380. * given the guarantees above.
  60381. */
  60382. duk_push_hobject_bidx(ctx, DUK_BIDX_OBJECT_CONSTRUCTOR);
  60383. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_DEFINE_PROPERTY);
  60384. duk_push_undefined(ctx);
  60385. duk_dup(ctx, (duk_idx_t) b);
  60386. duk_dup(ctx, (duk_idx_t) (idx + 0));
  60387. duk_push_object(ctx); /* -> [ Object defineProperty undefined obj key desc ] */
  60388. duk_push_true(ctx);
  60389. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_ENUMERABLE);
  60390. duk_push_true(ctx);
  60391. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_CONFIGURABLE);
  60392. duk_dup(ctx, (duk_idx_t) (idx + 1));
  60393. duk_put_prop_stridx(ctx, -2, (is_set ? DUK_STRIDX_SET : DUK_STRIDX_GET));
  60394. DUK_DDD(DUK_DDDPRINT("INITGET/INITSET: obj=%!T, key=%!T, desc=%!T",
  60395. (duk_tval *) duk_get_tval(ctx, -3),
  60396. (duk_tval *) duk_get_tval(ctx, -2),
  60397. (duk_tval *) duk_get_tval(ctx, -1)));
  60398. duk_call_method(ctx, 3); /* -> [ Object res ] */
  60399. duk_pop_2(ctx);
  60400. DUK_DDD(DUK_DDDPRINT("INITGET/INITSET AFTER: obj=%!T",
  60401. (duk_tval *) duk_get_tval(ctx, (duk_idx_t) b)));
  60402. break;
  60403. }
  60404. case DUK_EXTRAOP_ENDTRY: {
  60405. duk_catcher *cat;
  60406. duk_tval *tv1;
  60407. DUK_ASSERT(thr->catchstack_top >= 1);
  60408. DUK_ASSERT(thr->callstack_top >= 1);
  60409. DUK_ASSERT(thr->catchstack[thr->catchstack_top - 1].callstack_index == thr->callstack_top - 1);
  60410. cat = thr->catchstack + thr->catchstack_top - 1;
  60411. DUK_DDD(DUK_DDDPRINT("ENDTRY: clearing catch active flag (regardless of whether it was set or not)"));
  60412. DUK_CAT_CLEAR_CATCH_ENABLED(cat);
  60413. if (DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
  60414. DUK_DDD(DUK_DDDPRINT("ENDTRY: finally part is active, jump through 2nd jump slot with 'normal continuation'"));
  60415. tv1 = thr->valstack + cat->idx_base;
  60416. DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top);
  60417. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv1); /* side effects */
  60418. tv1 = NULL;
  60419. tv1 = thr->valstack + cat->idx_base + 1;
  60420. DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top);
  60421. #if defined(DUK_USE_FASTINT)
  60422. DUK_TVAL_SET_FASTINT_U32_UPDREF(thr, tv1, (duk_uint32_t) DUK_LJ_TYPE_NORMAL); /* side effects */
  60423. #else
  60424. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv1, (duk_double_t) DUK_LJ_TYPE_NORMAL); /* side effects */
  60425. #endif
  60426. tv1 = NULL;
  60427. DUK_CAT_CLEAR_FINALLY_ENABLED(cat);
  60428. } else {
  60429. DUK_DDD(DUK_DDDPRINT("ENDTRY: no finally part, dismantle catcher, jump through 2nd jump slot (to end of statement)"));
  60430. duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1);
  60431. /* no need to unwind callstack */
  60432. }
  60433. curr_pc = cat->pc_base + 1;
  60434. break;
  60435. }
  60436. case DUK_EXTRAOP_ENDCATCH: {
  60437. duk_activation *act;
  60438. duk_catcher *cat;
  60439. duk_tval *tv1;
  60440. DUK_ASSERT(thr->catchstack_top >= 1);
  60441. DUK_ASSERT(thr->callstack_top >= 1);
  60442. DUK_ASSERT(thr->catchstack[thr->catchstack_top - 1].callstack_index == thr->callstack_top - 1);
  60443. cat = thr->catchstack + thr->catchstack_top - 1;
  60444. DUK_ASSERT(!DUK_CAT_HAS_CATCH_ENABLED(cat)); /* cleared before entering catch part */
  60445. act = thr->callstack + thr->callstack_top - 1;
  60446. if (DUK_CAT_HAS_LEXENV_ACTIVE(cat)) {
  60447. duk_hobject *prev_env;
  60448. /* 'with' binding has no catch clause, so can't be here unless a normal try-catch */
  60449. DUK_ASSERT(DUK_CAT_HAS_CATCH_BINDING_ENABLED(cat));
  60450. DUK_ASSERT(act->lex_env != NULL);
  60451. DUK_DDD(DUK_DDDPRINT("ENDCATCH: popping catcher part lexical environment"));
  60452. prev_env = act->lex_env;
  60453. DUK_ASSERT(prev_env != NULL);
  60454. act->lex_env = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, prev_env);
  60455. DUK_CAT_CLEAR_LEXENV_ACTIVE(cat);
  60456. DUK_HOBJECT_DECREF(thr, prev_env); /* side effects */
  60457. }
  60458. if (DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
  60459. DUK_DDD(DUK_DDDPRINT("ENDCATCH: finally part is active, jump through 2nd jump slot with 'normal continuation'"));
  60460. tv1 = thr->valstack + cat->idx_base;
  60461. DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top);
  60462. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv1); /* side effects */
  60463. tv1 = NULL;
  60464. tv1 = thr->valstack + cat->idx_base + 1;
  60465. DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top);
  60466. #if defined(DUK_USE_FASTINT)
  60467. DUK_TVAL_SET_FASTINT_U32_UPDREF(thr, tv1, (duk_uint32_t) DUK_LJ_TYPE_NORMAL); /* side effects */
  60468. #else
  60469. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv1, (duk_double_t) DUK_LJ_TYPE_NORMAL); /* side effects */
  60470. #endif
  60471. tv1 = NULL;
  60472. DUK_CAT_CLEAR_FINALLY_ENABLED(cat);
  60473. } else {
  60474. DUK_DDD(DUK_DDDPRINT("ENDCATCH: no finally part, dismantle catcher, jump through 2nd jump slot (to end of statement)"));
  60475. duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1);
  60476. /* no need to unwind callstack */
  60477. }
  60478. curr_pc = cat->pc_base + 1;
  60479. break;
  60480. }
  60481. case DUK_EXTRAOP_ENDFIN: {
  60482. duk_context *ctx = (duk_context *) thr;
  60483. duk_catcher *cat;
  60484. duk_tval *tv1;
  60485. duk_small_uint_t cont_type;
  60486. duk_small_uint_t ret_result;
  60487. /* Sync and NULL early. */
  60488. DUK__SYNC_AND_NULL_CURR_PC();
  60489. DUK_ASSERT(thr->catchstack_top >= 1);
  60490. DUK_ASSERT(thr->callstack_top >= 1);
  60491. DUK_ASSERT(thr->catchstack[thr->catchstack_top - 1].callstack_index == thr->callstack_top - 1);
  60492. cat = thr->catchstack + thr->catchstack_top - 1;
  60493. /* CATCH flag may be enabled or disabled here; it may be enabled if
  60494. * the statement has a catch block but the try block does not throw
  60495. * an error.
  60496. */
  60497. DUK_ASSERT(!DUK_CAT_HAS_FINALLY_ENABLED(cat)); /* cleared before entering finally */
  60498. /* XXX: assert idx_base */
  60499. DUK_DDD(DUK_DDDPRINT("ENDFIN: completion value=%!T, type=%!T",
  60500. (duk_tval *) (thr->valstack + cat->idx_base + 0),
  60501. (duk_tval *) (thr->valstack + cat->idx_base + 1)));
  60502. tv1 = thr->valstack + cat->idx_base + 1; /* type */
  60503. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1));
  60504. cont_type = (duk_small_uint_t) DUK_TVAL_GET_NUMBER(tv1);
  60505. switch (cont_type) {
  60506. case DUK_LJ_TYPE_NORMAL: {
  60507. DUK_DDD(DUK_DDDPRINT("ENDFIN: finally part finishing with 'normal' (non-abrupt) completion -> "
  60508. "dismantle catcher, resume execution after ENDFIN"));
  60509. duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1);
  60510. /* no need to unwind callstack */
  60511. goto restart_execution;
  60512. }
  60513. case DUK_LJ_TYPE_RETURN: {
  60514. DUK_DDD(DUK_DDDPRINT("ENDFIN: finally part finishing with 'return' complation -> dismantle "
  60515. "catcher, handle return, lj.value1=%!T", thr->valstack + cat->idx_base));
  60516. /* Not necessary to unwind catchstack: return handling will
  60517. * do it. The finally flag of 'cat' is no longer set. The
  60518. * catch flag may be set, but it's not checked by return handling.
  60519. */
  60520. DUK_ASSERT(!DUK_CAT_HAS_FINALLY_ENABLED(cat)); /* cleared before entering finally */
  60521. #if 0
  60522. duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1);
  60523. #endif
  60524. duk_push_tval(ctx, thr->valstack + cat->idx_base);
  60525. ret_result = duk__handle_return(thr,
  60526. entry_thread,
  60527. entry_callstack_top);
  60528. if (ret_result == DUK__RETHAND_RESTART) {
  60529. goto restart_execution;
  60530. }
  60531. DUK_ASSERT(ret_result == DUK__RETHAND_FINISHED);
  60532. DUK_DDD(DUK_DDDPRINT("exiting executor after ENDFIN and RETURN (pseudo) longjmp type"));
  60533. return;
  60534. }
  60535. case DUK_LJ_TYPE_BREAK:
  60536. case DUK_LJ_TYPE_CONTINUE: {
  60537. duk_uint_t label_id;
  60538. duk_small_uint_t lj_type;
  60539. /* Not necessary to unwind catchstack: break/continue
  60540. * handling will do it. The finally flag of 'cat' is
  60541. * no longer set. The catch flag may be set, but it's
  60542. * not checked by break/continue handling.
  60543. */
  60544. #if 0
  60545. duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1);
  60546. #endif
  60547. tv1 = thr->valstack + cat->idx_base;
  60548. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1));
  60549. #if defined(DUK_USE_FASTINT)
  60550. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv1));
  60551. label_id = (duk_small_uint_t) DUK_TVAL_GET_FASTINT_U32(tv1);
  60552. #else
  60553. label_id = (duk_small_uint_t) DUK_TVAL_GET_NUMBER(tv1);
  60554. #endif
  60555. lj_type = cont_type;
  60556. duk__handle_break_or_continue(thr, label_id, lj_type);
  60557. goto restart_execution;
  60558. }
  60559. default: {
  60560. DUK_DDD(DUK_DDDPRINT("ENDFIN: finally part finishing with abrupt completion, lj_type=%ld -> "
  60561. "dismantle catcher, re-throw error",
  60562. (long) cont_type));
  60563. duk_push_tval(ctx, thr->valstack + cat->idx_base);
  60564. duk_err_setup_heap_ljstate(thr, (duk_small_int_t) cont_type);
  60565. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* always in executor */
  60566. duk_err_longjmp(thr);
  60567. DUK_UNREACHABLE();
  60568. }
  60569. }
  60570. /* Must restart in all cases because we NULLed thr->ptr_curr_pc. */
  60571. DUK_UNREACHABLE();
  60572. break;
  60573. }
  60574. case DUK_EXTRAOP_THROW: {
  60575. duk_context *ctx = (duk_context *) thr;
  60576. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  60577. /* Note: errors are augmented when they are created, not
  60578. * when they are thrown. So, don't augment here, it would
  60579. * break re-throwing for instance.
  60580. */
  60581. /* Sync so that augmentation sees up-to-date activations, NULL
  60582. * thr->ptr_curr_pc so that it's not used if side effects occur
  60583. * in augmentation or longjmp handling.
  60584. */
  60585. DUK__SYNC_AND_NULL_CURR_PC();
  60586. duk_dup(ctx, (duk_idx_t) bc);
  60587. DUK_DDD(DUK_DDDPRINT("THROW ERROR (BYTECODE): %!dT (before throw augment)",
  60588. (duk_tval *) duk_get_tval(ctx, -1)));
  60589. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  60590. duk_err_augment_error_throw(thr);
  60591. DUK_DDD(DUK_DDDPRINT("THROW ERROR (BYTECODE): %!dT (after throw augment)",
  60592. (duk_tval *) duk_get_tval(ctx, -1)));
  60593. #endif
  60594. duk_err_setup_heap_ljstate(thr, DUK_LJ_TYPE_THROW);
  60595. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* always in executor */
  60596. duk_err_longjmp(thr);
  60597. DUK_UNREACHABLE();
  60598. break;
  60599. }
  60600. case DUK_EXTRAOP_INVLHS: {
  60601. DUK_ERROR(thr, DUK_ERR_REFERENCE_ERROR, "invalid lvalue");
  60602. DUK_UNREACHABLE();
  60603. break;
  60604. }
  60605. case DUK_EXTRAOP_UNM:
  60606. case DUK_EXTRAOP_UNP: {
  60607. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  60608. duk__vm_arith_unary_op(thr, DUK__REGP(bc), bc, extraop);
  60609. break;
  60610. }
  60611. case DUK_EXTRAOP_DEBUGGER: {
  60612. /* Opcode only emitted by compiler when debugger
  60613. * support is enabled. Ignore it silently without
  60614. * debugger support, in case it has been loaded
  60615. * from precompiled bytecode.
  60616. */
  60617. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  60618. if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) {
  60619. DUK_D(DUK_DPRINT("DEBUGGER statement encountered, halt execution"));
  60620. DUK__SYNC_AND_NULL_CURR_PC();
  60621. duk_debug_halt_execution(thr, 1 /*use_prev_pc*/);
  60622. DUK_D(DUK_DPRINT("DEBUGGER statement finished, resume execution"));
  60623. goto restart_execution;
  60624. } else {
  60625. DUK_D(DUK_DPRINT("DEBUGGER statement ignored, debugger not attached"));
  60626. }
  60627. #else
  60628. DUK_D(DUK_DPRINT("DEBUGGER statement ignored, no debugger support"));
  60629. #endif
  60630. break;
  60631. }
  60632. case DUK_EXTRAOP_BREAK: {
  60633. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  60634. DUK_DDD(DUK_DDDPRINT("BREAK: %ld", (long) bc));
  60635. DUK__SYNC_AND_NULL_CURR_PC();
  60636. duk__handle_break_or_continue(thr, (duk_uint_t) bc, DUK_LJ_TYPE_BREAK);
  60637. goto restart_execution;
  60638. }
  60639. case DUK_EXTRAOP_CONTINUE: {
  60640. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  60641. DUK_DDD(DUK_DDDPRINT("CONTINUE: %ld", (long) bc));
  60642. DUK__SYNC_AND_NULL_CURR_PC();
  60643. duk__handle_break_or_continue(thr, (duk_uint_t) bc, DUK_LJ_TYPE_CONTINUE);
  60644. goto restart_execution;
  60645. }
  60646. case DUK_EXTRAOP_BNOT: {
  60647. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  60648. duk__vm_bitwise_not(thr, DUK__REGP(bc), bc);
  60649. break;
  60650. }
  60651. case DUK_EXTRAOP_LNOT: {
  60652. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  60653. duk_tval *tv1;
  60654. tv1 = DUK__REGP(bc);
  60655. duk__vm_logical_not(thr, tv1, tv1);
  60656. break;
  60657. }
  60658. case DUK_EXTRAOP_INSTOF: {
  60659. duk_context *ctx = (duk_context *) thr;
  60660. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  60661. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  60662. duk_bool_t tmp;
  60663. tmp = duk_js_instanceof(thr, DUK__REGP(b), DUK__REGCONSTP(c));
  60664. duk_push_boolean(ctx, tmp);
  60665. duk_replace(ctx, (duk_idx_t) b);
  60666. break;
  60667. }
  60668. case DUK_EXTRAOP_IN: {
  60669. duk_context *ctx = (duk_context *) thr;
  60670. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  60671. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  60672. duk_bool_t tmp;
  60673. tmp = duk_js_in(thr, DUK__REGP(b), DUK__REGCONSTP(c));
  60674. duk_push_boolean(ctx, tmp);
  60675. duk_replace(ctx, (duk_idx_t) b);
  60676. break;
  60677. }
  60678. case DUK_EXTRAOP_LABEL: {
  60679. duk_catcher *cat;
  60680. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  60681. /* allocate catcher and populate it (should be atomic) */
  60682. duk_hthread_catchstack_grow(thr);
  60683. cat = thr->catchstack + thr->catchstack_top;
  60684. thr->catchstack_top++;
  60685. cat->flags = DUK_CAT_TYPE_LABEL | (bc << DUK_CAT_LABEL_SHIFT);
  60686. cat->callstack_index = thr->callstack_top - 1;
  60687. cat->pc_base = (duk_instr_t *) curr_pc; /* pre-incremented, points to first jump slot */
  60688. cat->idx_base = 0; /* unused for label */
  60689. cat->h_varname = NULL;
  60690. DUK_DDD(DUK_DDDPRINT("LABEL catcher: flags=0x%08lx, callstack_index=%ld, pc_base=%ld, "
  60691. "idx_base=%ld, h_varname=%!O, label_id=%ld",
  60692. (long) cat->flags, (long) cat->callstack_index, (long) cat->pc_base,
  60693. (long) cat->idx_base, (duk_heaphdr *) cat->h_varname, (long) DUK_CAT_GET_LABEL(cat)));
  60694. curr_pc += 2; /* skip jump slots */
  60695. break;
  60696. }
  60697. case DUK_EXTRAOP_ENDLABEL: {
  60698. duk_catcher *cat;
  60699. #if defined(DUK_USE_DDDPRINT) || defined(DUK_USE_ASSERTIONS)
  60700. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  60701. #endif
  60702. #if defined(DUK_USE_DDDPRINT)
  60703. DUK_DDD(DUK_DDDPRINT("ENDLABEL %ld", (long) bc));
  60704. #endif
  60705. DUK_ASSERT(thr->catchstack_top >= 1);
  60706. cat = thr->catchstack + thr->catchstack_top - 1;
  60707. DUK_UNREF(cat);
  60708. DUK_ASSERT(DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_LABEL);
  60709. DUK_ASSERT((duk_uint_fast_t) DUK_CAT_GET_LABEL(cat) == bc);
  60710. duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1);
  60711. /* no need to unwind callstack */
  60712. break;
  60713. }
  60714. default: {
  60715. DUK__INTERNAL_ERROR("invalid extra opcode");
  60716. }
  60717. } /* end switch */
  60718. break;
  60719. }
  60720. default: {
  60721. /* this should never be possible, because the switch-case is
  60722. * comprehensive
  60723. */
  60724. DUK__INTERNAL_ERROR("invalid opcode");
  60725. break;
  60726. }
  60727. } /* end switch */
  60728. }
  60729. DUK_UNREACHABLE();
  60730. #ifndef DUK_USE_VERBOSE_EXECUTOR_ERRORS
  60731. internal_error:
  60732. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, "internal error in bytecode executor");
  60733. #endif
  60734. }
  60735. #undef DUK__LONGJMP_RESTART
  60736. #undef DUK__LONGJMP_FINISHED
  60737. #undef DUK__LONGJMP_RETHROW
  60738. #undef DUK__RETHAND_RESTART
  60739. #undef DUK__RETHAND_FINISHED
  60740. #undef DUK__INTERNAL_ERROR
  60741. #undef DUK__SYNC_CURR_PC
  60742. #undef DUK__SYNC_AND_NULL_CURR_PC
  60743. #line 1 "duk_js_ops.c"
  60744. /*
  60745. * Ecmascript specification algorithm and conversion helpers.
  60746. *
  60747. * These helpers encapsulate the primitive Ecmascript operation
  60748. * semantics, and are used by the bytecode executor and the API
  60749. * (among other places). Note that some primitives are only
  60750. * implemented as part of the API and have no "internal" helper.
  60751. * (This is the case when an internal helper would not really be
  60752. * useful; e.g. the operation is rare, uses value stack heavily,
  60753. * etc.)
  60754. *
  60755. * The operation arguments depend on what is required to implement
  60756. * the operation:
  60757. *
  60758. * - If an operation is simple and stateless, and has no side
  60759. * effects, it won't take an duk_hthread argument and its
  60760. * arguments may be duk_tval pointers (which are safe as long
  60761. * as no side effects take place).
  60762. *
  60763. * - If complex coercions are required (e.g. a "ToNumber" coercion)
  60764. * or errors may be thrown, the operation takes an duk_hthread
  60765. * argument. This also implies that the operation may have
  60766. * arbitrary side effects, invalidating any duk_tval pointers.
  60767. *
  60768. * - For operations with potential side effects, arguments can be
  60769. * taken in several ways:
  60770. *
  60771. * a) as duk_tval pointers, which makes sense if the "common case"
  60772. * can be resolved without side effects (e.g. coercion); the
  60773. * arguments are pushed to the valstack for coercion if
  60774. * necessary
  60775. *
  60776. * b) as duk_tval values
  60777. *
  60778. * c) implicitly on value stack top
  60779. *
  60780. * d) as indices to the value stack
  60781. *
  60782. * Future work:
  60783. *
  60784. * - Argument styles may not be the most sensible in every case now.
  60785. *
  60786. * - In-place coercions might be useful for several operations, if
  60787. * in-place coercion is OK for the bytecode executor and the API.
  60788. */
  60789. /* include removed: duk_internal.h */
  60790. /*
  60791. * [[DefaultValue]] (E5 Section 8.12.8)
  60792. *
  60793. * ==> implemented in the API.
  60794. */
  60795. /*
  60796. * ToPrimitive() (E5 Section 9.1)
  60797. *
  60798. * ==> implemented in the API.
  60799. */
  60800. /*
  60801. * ToBoolean() (E5 Section 9.2)
  60802. */
  60803. DUK_INTERNAL duk_bool_t duk_js_toboolean(duk_tval *tv) {
  60804. switch (DUK_TVAL_GET_TAG(tv)) {
  60805. case DUK_TAG_UNDEFINED:
  60806. case DUK_TAG_NULL:
  60807. return 0;
  60808. case DUK_TAG_BOOLEAN:
  60809. return DUK_TVAL_GET_BOOLEAN(tv);
  60810. case DUK_TAG_STRING: {
  60811. duk_hstring *h = DUK_TVAL_GET_STRING(tv);
  60812. DUK_ASSERT(h != NULL);
  60813. return (DUK_HSTRING_GET_BYTELEN(h) > 0 ? 1 : 0);
  60814. }
  60815. case DUK_TAG_OBJECT: {
  60816. return 1;
  60817. }
  60818. case DUK_TAG_BUFFER: {
  60819. /* mimic semantics for strings */
  60820. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  60821. DUK_ASSERT(h != NULL);
  60822. return (DUK_HBUFFER_GET_SIZE(h) > 0 ? 1 : 0);
  60823. }
  60824. case DUK_TAG_POINTER: {
  60825. void *p = DUK_TVAL_GET_POINTER(tv);
  60826. return (p != NULL ? 1 : 0);
  60827. }
  60828. case DUK_TAG_LIGHTFUNC: {
  60829. return 1;
  60830. }
  60831. #if defined(DUK_USE_FASTINT)
  60832. case DUK_TAG_FASTINT:
  60833. if (DUK_TVAL_GET_FASTINT(tv) != 0) {
  60834. return 1;
  60835. } else {
  60836. return 0;
  60837. }
  60838. #endif
  60839. default: {
  60840. /* number */
  60841. duk_double_t d;
  60842. int c;
  60843. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  60844. DUK_ASSERT(DUK_TVAL_IS_DOUBLE(tv));
  60845. d = DUK_TVAL_GET_DOUBLE(tv);
  60846. c = DUK_FPCLASSIFY((double) d);
  60847. if (c == DUK_FP_ZERO || c == DUK_FP_NAN) {
  60848. return 0;
  60849. } else {
  60850. return 1;
  60851. }
  60852. }
  60853. }
  60854. DUK_UNREACHABLE();
  60855. }
  60856. /*
  60857. * ToNumber() (E5 Section 9.3)
  60858. *
  60859. * Value to convert must be on stack top, and is popped before exit.
  60860. *
  60861. * See: http://www.cs.indiana.edu/~burger/FP-Printing-PLDI96.pdf
  60862. * http://www.cs.indiana.edu/~burger/fp/index.html
  60863. *
  60864. * Notes on the conversion:
  60865. *
  60866. * - There are specific requirements on the accuracy of the conversion
  60867. * through a "Mathematical Value" (MV), so this conversion is not
  60868. * trivial.
  60869. *
  60870. * - Quick rejects (e.g. based on first char) are difficult because
  60871. * the grammar allows leading and trailing white space.
  60872. *
  60873. * - Quick reject based on string length is difficult even after
  60874. * accounting for white space; there may be arbitrarily many
  60875. * decimal digits.
  60876. *
  60877. * - Standard grammar allows decimal values ("123"), hex values
  60878. * ("0x123") and infinities
  60879. *
  60880. * - Unlike source code literals, ToNumber() coerces empty strings
  60881. * and strings with only whitespace to zero (not NaN).
  60882. */
  60883. /* E5 Section 9.3.1 */
  60884. DUK_LOCAL duk_double_t duk__tonumber_string_raw(duk_hthread *thr) {
  60885. duk_context *ctx = (duk_context *) thr;
  60886. duk_small_uint_t s2n_flags;
  60887. duk_double_t d;
  60888. /* Quite lenient, e.g. allow empty as zero, but don't allow trailing
  60889. * garbage.
  60890. */
  60891. s2n_flags = DUK_S2N_FLAG_TRIM_WHITE |
  60892. DUK_S2N_FLAG_ALLOW_EXP |
  60893. DUK_S2N_FLAG_ALLOW_PLUS |
  60894. DUK_S2N_FLAG_ALLOW_MINUS |
  60895. DUK_S2N_FLAG_ALLOW_INF |
  60896. DUK_S2N_FLAG_ALLOW_FRAC |
  60897. DUK_S2N_FLAG_ALLOW_NAKED_FRAC |
  60898. DUK_S2N_FLAG_ALLOW_EMPTY_FRAC |
  60899. DUK_S2N_FLAG_ALLOW_EMPTY_AS_ZERO |
  60900. DUK_S2N_FLAG_ALLOW_LEADING_ZERO |
  60901. DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT;
  60902. duk_numconv_parse(ctx, 10 /*radix*/, s2n_flags);
  60903. d = duk_get_number(ctx, -1);
  60904. duk_pop(ctx);
  60905. return d;
  60906. }
  60907. DUK_INTERNAL duk_double_t duk_js_tonumber(duk_hthread *thr, duk_tval *tv) {
  60908. duk_context *ctx = (duk_hthread *) thr;
  60909. DUK_ASSERT(thr != NULL);
  60910. DUK_ASSERT(tv != NULL);
  60911. switch (DUK_TVAL_GET_TAG(tv)) {
  60912. case DUK_TAG_UNDEFINED: {
  60913. /* return a specific NaN (although not strictly necessary) */
  60914. duk_double_union du;
  60915. DUK_DBLUNION_SET_NAN(&du);
  60916. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  60917. return du.d;
  60918. }
  60919. case DUK_TAG_NULL: {
  60920. /* +0.0 */
  60921. return 0.0;
  60922. }
  60923. case DUK_TAG_BOOLEAN: {
  60924. if (DUK_TVAL_IS_BOOLEAN_TRUE(tv)) {
  60925. return 1.0;
  60926. }
  60927. return 0.0;
  60928. }
  60929. case DUK_TAG_STRING: {
  60930. duk_hstring *h = DUK_TVAL_GET_STRING(tv);
  60931. duk_push_hstring(ctx, h);
  60932. return duk__tonumber_string_raw(thr);
  60933. }
  60934. case DUK_TAG_OBJECT: {
  60935. /* Note: ToPrimitive(object,hint) == [[DefaultValue]](object,hint),
  60936. * so use [[DefaultValue]] directly.
  60937. */
  60938. duk_double_t d;
  60939. duk_push_tval(ctx, tv);
  60940. duk_to_defaultvalue(ctx, -1, DUK_HINT_NUMBER); /* 'tv' becomes invalid */
  60941. /* recursive call for a primitive value (guaranteed not to cause second
  60942. * recursion).
  60943. */
  60944. d = duk_js_tonumber(thr, duk_require_tval(ctx, -1));
  60945. duk_pop(ctx);
  60946. return d;
  60947. }
  60948. case DUK_TAG_BUFFER: {
  60949. /* Coerce like a string. This makes sense because addition also treats
  60950. * buffers like strings.
  60951. */
  60952. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  60953. duk_push_hbuffer(ctx, h);
  60954. duk_to_string(ctx, -1); /* XXX: expensive, but numconv now expects to see a string */
  60955. return duk__tonumber_string_raw(thr);
  60956. }
  60957. case DUK_TAG_POINTER: {
  60958. /* Coerce like boolean */
  60959. void *p = DUK_TVAL_GET_POINTER(tv);
  60960. return (p != NULL ? 1.0 : 0.0);
  60961. }
  60962. case DUK_TAG_LIGHTFUNC: {
  60963. /* +(function(){}) -> NaN */
  60964. return DUK_DOUBLE_NAN;
  60965. }
  60966. #if defined(DUK_USE_FASTINT)
  60967. case DUK_TAG_FASTINT:
  60968. return (duk_double_t) DUK_TVAL_GET_FASTINT(tv);
  60969. #endif
  60970. default: {
  60971. /* number */
  60972. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  60973. DUK_ASSERT(DUK_TVAL_IS_DOUBLE(tv));
  60974. return DUK_TVAL_GET_DOUBLE(tv);
  60975. }
  60976. }
  60977. DUK_UNREACHABLE();
  60978. }
  60979. /*
  60980. * ToInteger() (E5 Section 9.4)
  60981. */
  60982. /* exposed, used by e.g. duk_bi_date.c */
  60983. DUK_INTERNAL duk_double_t duk_js_tointeger_number(duk_double_t x) {
  60984. duk_small_int_t c = (duk_small_int_t) DUK_FPCLASSIFY(x);
  60985. if (c == DUK_FP_NAN) {
  60986. return 0.0;
  60987. } else if (c == DUK_FP_ZERO || c == DUK_FP_INFINITE) {
  60988. /* XXX: FP_ZERO check can be removed, the else clause handles it
  60989. * correctly (preserving sign).
  60990. */
  60991. return x;
  60992. } else {
  60993. duk_small_int_t s = (duk_small_int_t) DUK_SIGNBIT(x);
  60994. x = DUK_FLOOR(DUK_FABS(x)); /* truncate towards zero */
  60995. if (s) {
  60996. x = -x;
  60997. }
  60998. return x;
  60999. }
  61000. }
  61001. DUK_INTERNAL duk_double_t duk_js_tointeger(duk_hthread *thr, duk_tval *tv) {
  61002. /* XXX: fastint */
  61003. duk_double_t d = duk_js_tonumber(thr, tv); /* invalidates tv */
  61004. return duk_js_tointeger_number(d);
  61005. }
  61006. /*
  61007. * ToInt32(), ToUint32(), ToUint16() (E5 Sections 9.5, 9.6, 9.7)
  61008. */
  61009. /* combined algorithm matching E5 Sections 9.5 and 9.6 */
  61010. DUK_LOCAL duk_double_t duk__toint32_touint32_helper(duk_double_t x, duk_bool_t is_toint32) {
  61011. duk_small_int_t c = (duk_small_int_t) DUK_FPCLASSIFY(x);
  61012. duk_small_int_t s;
  61013. if (c == DUK_FP_NAN || c == DUK_FP_ZERO || c == DUK_FP_INFINITE) {
  61014. return 0.0;
  61015. }
  61016. /* x = sign(x) * floor(abs(x)), i.e. truncate towards zero, keep sign */
  61017. s = (duk_small_int_t) DUK_SIGNBIT(x);
  61018. x = DUK_FLOOR(DUK_FABS(x));
  61019. if (s) {
  61020. x = -x;
  61021. }
  61022. /* NOTE: fmod(x) result sign is same as sign of x, which
  61023. * differs from what Javascript wants (see Section 9.6).
  61024. */
  61025. x = DUK_FMOD(x, DUK_DOUBLE_2TO32); /* -> x in ]-2**32, 2**32[ */
  61026. if (x < 0.0) {
  61027. x += DUK_DOUBLE_2TO32;
  61028. }
  61029. /* -> x in [0, 2**32[ */
  61030. if (is_toint32) {
  61031. if (x >= DUK_DOUBLE_2TO31) {
  61032. /* x in [2**31, 2**32[ */
  61033. x -= DUK_DOUBLE_2TO32; /* -> x in [-2**31,2**31[ */
  61034. }
  61035. }
  61036. return x;
  61037. }
  61038. DUK_INTERNAL duk_int32_t duk_js_toint32(duk_hthread *thr, duk_tval *tv) {
  61039. duk_double_t d;
  61040. #if defined(DUK_USE_FASTINT)
  61041. if (DUK_TVAL_IS_FASTINT(tv)) {
  61042. return DUK_TVAL_GET_FASTINT_I32(tv);
  61043. }
  61044. #endif
  61045. d = duk_js_tonumber(thr, tv); /* invalidates tv */
  61046. d = duk__toint32_touint32_helper(d, 1);
  61047. DUK_ASSERT(DUK_FPCLASSIFY(d) == DUK_FP_ZERO || DUK_FPCLASSIFY(d) == DUK_FP_NORMAL);
  61048. DUK_ASSERT(d >= -2147483648.0 && d <= 2147483647.0); /* [-0x80000000,0x7fffffff] */
  61049. DUK_ASSERT(d == ((duk_double_t) ((duk_int32_t) d))); /* whole, won't clip */
  61050. return (duk_int32_t) d;
  61051. }
  61052. DUK_INTERNAL duk_uint32_t duk_js_touint32(duk_hthread *thr, duk_tval *tv) {
  61053. duk_double_t d;
  61054. #if defined(DUK_USE_FASTINT)
  61055. if (DUK_TVAL_IS_FASTINT(tv)) {
  61056. return DUK_TVAL_GET_FASTINT_U32(tv);
  61057. }
  61058. #endif
  61059. d = duk_js_tonumber(thr, tv); /* invalidates tv */
  61060. d = duk__toint32_touint32_helper(d, 0);
  61061. DUK_ASSERT(DUK_FPCLASSIFY(d) == DUK_FP_ZERO || DUK_FPCLASSIFY(d) == DUK_FP_NORMAL);
  61062. DUK_ASSERT(d >= 0.0 && d <= 4294967295.0); /* [0x00000000, 0xffffffff] */
  61063. DUK_ASSERT(d == ((duk_double_t) ((duk_uint32_t) d))); /* whole, won't clip */
  61064. return (duk_uint32_t) d;
  61065. }
  61066. DUK_INTERNAL duk_uint16_t duk_js_touint16(duk_hthread *thr, duk_tval *tv) {
  61067. /* should be a safe way to compute this */
  61068. return (duk_uint16_t) (duk_js_touint32(thr, tv) & 0x0000ffffU);
  61069. }
  61070. /*
  61071. * ToString() (E5 Section 9.8)
  61072. *
  61073. * ==> implemented in the API.
  61074. */
  61075. /*
  61076. * ToObject() (E5 Section 9.9)
  61077. *
  61078. * ==> implemented in the API.
  61079. */
  61080. /*
  61081. * CheckObjectCoercible() (E5 Section 9.10)
  61082. *
  61083. * Note: no API equivalent now.
  61084. */
  61085. #if 0 /* unused */
  61086. DUK_INTERNAL void duk_js_checkobjectcoercible(duk_hthread *thr, duk_tval *tv_x) {
  61087. duk_small_uint_t tag = DUK_TVAL_GET_TAG(tv_x);
  61088. /* Note: this must match ToObject() behavior */
  61089. if (tag == DUK_TAG_UNDEFINED ||
  61090. tag == DUK_TAG_NULL ||
  61091. tag == DUK_TAG_POINTER ||
  61092. tag == DUK_TAG_BUFFER) {
  61093. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "not object coercible");
  61094. }
  61095. }
  61096. #endif
  61097. /*
  61098. * IsCallable() (E5 Section 9.11)
  61099. *
  61100. * XXX: API equivalent is a separate implementation now, and this has
  61101. * currently no callers.
  61102. */
  61103. #if 0 /* unused */
  61104. DUK_INTERNAL duk_bool_t duk_js_iscallable(duk_tval *tv_x) {
  61105. duk_hobject *obj;
  61106. if (!DUK_TVAL_IS_OBJECT(tv_x)) {
  61107. return 0;
  61108. }
  61109. obj = DUK_TVAL_GET_OBJECT(tv_x);
  61110. DUK_ASSERT(obj != NULL);
  61111. return DUK_HOBJECT_IS_CALLABLE(obj);
  61112. }
  61113. #endif
  61114. /*
  61115. * Loose equality, strict equality, and SameValue (E5 Sections 11.9.1, 11.9.4,
  61116. * 9.12). These have much in common so they can share some helpers.
  61117. *
  61118. * Future work notes:
  61119. *
  61120. * - Current implementation (and spec definition) has recursion; this should
  61121. * be fixed if possible.
  61122. *
  61123. * - String-to-number coercion should be possible without going through the
  61124. * value stack (and be more compact) if a shared helper is invoked.
  61125. */
  61126. /* Note that this is the same operation for strict and loose equality:
  61127. * - E5 Section 11.9.3, step 1.c (loose)
  61128. * - E5 Section 11.9.6, step 4 (strict)
  61129. */
  61130. DUK_LOCAL duk_bool_t duk__js_equals_number(duk_double_t x, duk_double_t y) {
  61131. #if defined(DUK_USE_PARANOID_MATH)
  61132. /* Straightforward algorithm, makes fewer compiler assumptions. */
  61133. duk_small_int_t cx = (duk_small_int_t) DUK_FPCLASSIFY(x);
  61134. duk_small_int_t cy = (duk_small_int_t) DUK_FPCLASSIFY(y);
  61135. if (cx == DUK_FP_NAN || cy == DUK_FP_NAN) {
  61136. return 0;
  61137. }
  61138. if (cx == DUK_FP_ZERO && cy == DUK_FP_ZERO) {
  61139. return 1;
  61140. }
  61141. if (x == y) {
  61142. return 1;
  61143. }
  61144. return 0;
  61145. #else /* DUK_USE_PARANOID_MATH */
  61146. /* Better equivalent algorithm. If the compiler is compliant, C and
  61147. * Ecmascript semantics are identical for this particular comparison.
  61148. * In particular, NaNs must never compare equal and zeroes must compare
  61149. * equal regardless of sign. Could also use a macro, but this inlines
  61150. * already nicely (no difference on gcc, for instance).
  61151. */
  61152. if (x == y) {
  61153. /* IEEE requires that NaNs compare false */
  61154. DUK_ASSERT(DUK_FPCLASSIFY(x) != DUK_FP_NAN);
  61155. DUK_ASSERT(DUK_FPCLASSIFY(y) != DUK_FP_NAN);
  61156. return 1;
  61157. } else {
  61158. /* IEEE requires that zeros compare the same regardless
  61159. * of their signed, so if both x and y are zeroes, they
  61160. * are caught above.
  61161. */
  61162. DUK_ASSERT(!(DUK_FPCLASSIFY(x) == DUK_FP_ZERO && DUK_FPCLASSIFY(y) == DUK_FP_ZERO));
  61163. return 0;
  61164. }
  61165. #endif /* DUK_USE_PARANOID_MATH */
  61166. }
  61167. DUK_LOCAL duk_bool_t duk__js_samevalue_number(duk_double_t x, duk_double_t y) {
  61168. #if defined(DUK_USE_PARANOID_MATH)
  61169. duk_small_int_t cx = (duk_small_int_t) DUK_FPCLASSIFY(x);
  61170. duk_small_int_t cy = (duk_small_int_t) DUK_FPCLASSIFY(y);
  61171. if (cx == DUK_FP_NAN && cy == DUK_FP_NAN) {
  61172. /* SameValue(NaN, NaN) = true, regardless of NaN sign or extra bits */
  61173. return 1;
  61174. }
  61175. if (cx == DUK_FP_ZERO && cy == DUK_FP_ZERO) {
  61176. /* Note: cannot assume that a non-zero return value of signbit() would
  61177. * always be the same -- hence cannot (portably) use something like:
  61178. *
  61179. * signbit(x) == signbit(y)
  61180. */
  61181. duk_small_int_t sx = (DUK_SIGNBIT(x) ? 1 : 0);
  61182. duk_small_int_t sy = (DUK_SIGNBIT(y) ? 1 : 0);
  61183. return (sx == sy);
  61184. }
  61185. /* normal comparison; known:
  61186. * - both x and y are not NaNs (but one of them can be)
  61187. * - both x and y are not zero (but one of them can be)
  61188. * - x and y may be denormal or infinite
  61189. */
  61190. return (x == y);
  61191. #else /* DUK_USE_PARANOID_MATH */
  61192. duk_small_int_t cx = (duk_small_int_t) DUK_FPCLASSIFY(x);
  61193. duk_small_int_t cy = (duk_small_int_t) DUK_FPCLASSIFY(y);
  61194. if (x == y) {
  61195. /* IEEE requires that NaNs compare false */
  61196. DUK_ASSERT(DUK_FPCLASSIFY(x) != DUK_FP_NAN);
  61197. DUK_ASSERT(DUK_FPCLASSIFY(y) != DUK_FP_NAN);
  61198. /* Using classification has smaller footprint than direct comparison. */
  61199. if (DUK_UNLIKELY(cx == DUK_FP_ZERO && cy == DUK_FP_ZERO)) {
  61200. /* Note: cannot assume that a non-zero return value of signbit() would
  61201. * always be the same -- hence cannot (portably) use something like:
  61202. *
  61203. * signbit(x) == signbit(y)
  61204. */
  61205. duk_small_int_t sx = (DUK_SIGNBIT(x) ? 1 : 0);
  61206. duk_small_int_t sy = (DUK_SIGNBIT(y) ? 1 : 0);
  61207. return (sx == sy);
  61208. }
  61209. return 1;
  61210. } else {
  61211. /* IEEE requires that zeros compare the same regardless
  61212. * of their signed, so if both x and y are zeroes, they
  61213. * are caught above.
  61214. */
  61215. DUK_ASSERT(!(DUK_FPCLASSIFY(x) == DUK_FP_ZERO && DUK_FPCLASSIFY(y) == DUK_FP_ZERO));
  61216. /* Difference to non-strict/strict comparison is that NaNs compare
  61217. * equal and signed zero signs matter.
  61218. */
  61219. if (DUK_UNLIKELY(cx == DUK_FP_NAN && cy == DUK_FP_NAN)) {
  61220. /* SameValue(NaN, NaN) = true, regardless of NaN sign or extra bits */
  61221. return 1;
  61222. }
  61223. return 0;
  61224. }
  61225. #endif /* DUK_USE_PARANOID_MATH */
  61226. }
  61227. DUK_INTERNAL duk_bool_t duk_js_equals_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_int_t flags) {
  61228. duk_context *ctx = (duk_context *) thr;
  61229. duk_tval *tv_tmp;
  61230. /* If flags != 0 (strict or SameValue), thr can be NULL. For loose
  61231. * equals comparison it must be != NULL.
  61232. */
  61233. DUK_ASSERT(flags != 0 || thr != NULL);
  61234. /*
  61235. * Same type?
  61236. *
  61237. * Note: since number values have no explicit tag in the 8-byte
  61238. * representation, need the awkward if + switch.
  61239. */
  61240. #if defined(DUK_USE_FASTINT)
  61241. if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) {
  61242. if (DUK_TVAL_GET_FASTINT(tv_x) == DUK_TVAL_GET_FASTINT(tv_y)) {
  61243. return 1;
  61244. } else {
  61245. return 0;
  61246. }
  61247. }
  61248. else
  61249. #endif
  61250. if (DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y)) {
  61251. /* Catches both doubles and cases where only one argument is a fastint */
  61252. if (DUK_UNLIKELY((flags & DUK_EQUALS_FLAG_SAMEVALUE) != 0)) {
  61253. /* SameValue */
  61254. return duk__js_samevalue_number(DUK_TVAL_GET_NUMBER(tv_x),
  61255. DUK_TVAL_GET_NUMBER(tv_y));
  61256. } else {
  61257. /* equals and strict equals */
  61258. return duk__js_equals_number(DUK_TVAL_GET_NUMBER(tv_x),
  61259. DUK_TVAL_GET_NUMBER(tv_y));
  61260. }
  61261. } else if (DUK_TVAL_GET_TAG(tv_x) == DUK_TVAL_GET_TAG(tv_y)) {
  61262. switch (DUK_TVAL_GET_TAG(tv_x)) {
  61263. case DUK_TAG_UNDEFINED:
  61264. case DUK_TAG_NULL: {
  61265. return 1;
  61266. }
  61267. case DUK_TAG_BOOLEAN: {
  61268. return DUK_TVAL_GET_BOOLEAN(tv_x) == DUK_TVAL_GET_BOOLEAN(tv_y);
  61269. }
  61270. case DUK_TAG_POINTER: {
  61271. return DUK_TVAL_GET_POINTER(tv_x) == DUK_TVAL_GET_POINTER(tv_y);
  61272. }
  61273. case DUK_TAG_STRING:
  61274. case DUK_TAG_OBJECT: {
  61275. /* heap pointer comparison suffices */
  61276. return DUK_TVAL_GET_HEAPHDR(tv_x) == DUK_TVAL_GET_HEAPHDR(tv_y);
  61277. }
  61278. case DUK_TAG_BUFFER: {
  61279. if ((flags & (DUK_EQUALS_FLAG_STRICT | DUK_EQUALS_FLAG_SAMEVALUE)) != 0) {
  61280. /* heap pointer comparison suffices */
  61281. return DUK_TVAL_GET_HEAPHDR(tv_x) == DUK_TVAL_GET_HEAPHDR(tv_y);
  61282. } else {
  61283. /* non-strict equality for buffers compares contents */
  61284. duk_hbuffer *h_x = DUK_TVAL_GET_BUFFER(tv_x);
  61285. duk_hbuffer *h_y = DUK_TVAL_GET_BUFFER(tv_y);
  61286. duk_size_t len_x = DUK_HBUFFER_GET_SIZE(h_x);
  61287. duk_size_t len_y = DUK_HBUFFER_GET_SIZE(h_y);
  61288. void *buf_x;
  61289. void *buf_y;
  61290. if (len_x != len_y) {
  61291. return 0;
  61292. }
  61293. buf_x = (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_x);
  61294. buf_y = (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_y);
  61295. /* if len_x == len_y == 0, buf_x and/or buf_y may
  61296. * be NULL, but that's OK.
  61297. */
  61298. DUK_ASSERT(len_x == len_y);
  61299. DUK_ASSERT(len_x == 0 || buf_x != NULL);
  61300. DUK_ASSERT(len_y == 0 || buf_y != NULL);
  61301. return (DUK_MEMCMP((const void *) buf_x, (const void *) buf_y, (size_t) len_x) == 0) ? 1 : 0;
  61302. }
  61303. }
  61304. case DUK_TAG_LIGHTFUNC: {
  61305. /* At least 'magic' has a significant impact on function
  61306. * identity.
  61307. */
  61308. duk_small_uint_t lf_flags_x;
  61309. duk_small_uint_t lf_flags_y;
  61310. duk_c_function func_x;
  61311. duk_c_function func_y;
  61312. DUK_TVAL_GET_LIGHTFUNC(tv_x, func_x, lf_flags_x);
  61313. DUK_TVAL_GET_LIGHTFUNC(tv_y, func_y, lf_flags_y);
  61314. return ((func_x == func_y) && (lf_flags_x == lf_flags_y)) ? 1 : 0;
  61315. }
  61316. #if defined(DUK_USE_FASTINT)
  61317. case DUK_TAG_FASTINT:
  61318. #endif
  61319. default: {
  61320. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv_x));
  61321. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv_y));
  61322. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_x));
  61323. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_y));
  61324. DUK_UNREACHABLE();
  61325. return 0;
  61326. }
  61327. }
  61328. }
  61329. if ((flags & (DUK_EQUALS_FLAG_STRICT | DUK_EQUALS_FLAG_SAMEVALUE)) != 0) {
  61330. return 0;
  61331. }
  61332. DUK_ASSERT(flags == 0); /* non-strict equality from here on */
  61333. /*
  61334. * Types are different; various cases for non-strict comparison
  61335. *
  61336. * Since comparison is symmetric, we use a "swap trick" to reduce
  61337. * code size.
  61338. */
  61339. /* Undefined/null are considered equal (e.g. "null == undefined" -> true). */
  61340. if ((DUK_TVAL_IS_UNDEFINED(tv_x) && DUK_TVAL_IS_NULL(tv_y)) ||
  61341. (DUK_TVAL_IS_NULL(tv_x) && DUK_TVAL_IS_UNDEFINED(tv_y))) {
  61342. return 1;
  61343. }
  61344. /* Number/string-or-buffer -> coerce string to number (e.g. "'1.5' == 1.5" -> true). */
  61345. if (DUK_TVAL_IS_NUMBER(tv_x) && (DUK_TVAL_IS_STRING(tv_y) || DUK_TVAL_IS_BUFFER(tv_y))) {
  61346. /* the next 'if' is guaranteed to match after swap */
  61347. tv_tmp = tv_x;
  61348. tv_x = tv_y;
  61349. tv_y = tv_tmp;
  61350. }
  61351. if ((DUK_TVAL_IS_STRING(tv_x) || DUK_TVAL_IS_BUFFER(tv_x)) && DUK_TVAL_IS_NUMBER(tv_y)) {
  61352. /* XXX: this is possible without resorting to the value stack */
  61353. duk_double_t d1, d2;
  61354. d2 = DUK_TVAL_GET_NUMBER(tv_y);
  61355. duk_push_tval(ctx, tv_x);
  61356. duk_to_string(ctx, -1); /* buffer values are coerced first to string here */
  61357. duk_to_number(ctx, -1);
  61358. d1 = duk_require_number(ctx, -1);
  61359. duk_pop(ctx);
  61360. return duk__js_equals_number(d1, d2);
  61361. }
  61362. /* Buffer/string -> compare contents. */
  61363. if (DUK_TVAL_IS_BUFFER(tv_x) && DUK_TVAL_IS_STRING(tv_y)) {
  61364. tv_tmp = tv_x;
  61365. tv_x = tv_y;
  61366. tv_y = tv_tmp;
  61367. }
  61368. if (DUK_TVAL_IS_STRING(tv_x) && DUK_TVAL_IS_BUFFER(tv_y)) {
  61369. duk_hstring *h_x = DUK_TVAL_GET_STRING(tv_x);
  61370. duk_hbuffer *h_y = DUK_TVAL_GET_BUFFER(tv_y);
  61371. duk_size_t len_x = DUK_HSTRING_GET_BYTELEN(h_x);
  61372. duk_size_t len_y = DUK_HBUFFER_GET_SIZE(h_y);
  61373. const void *buf_x;
  61374. const void *buf_y;
  61375. if (len_x != len_y) {
  61376. return 0;
  61377. }
  61378. buf_x = (const void *) DUK_HSTRING_GET_DATA(h_x);
  61379. buf_y = (const void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_y);
  61380. /* if len_x == len_y == 0, buf_x and/or buf_y may
  61381. * be NULL, but that's OK.
  61382. */
  61383. DUK_ASSERT(len_x == len_y);
  61384. DUK_ASSERT(len_x == 0 || buf_x != NULL);
  61385. DUK_ASSERT(len_y == 0 || buf_y != NULL);
  61386. return (DUK_MEMCMP((const void *) buf_x, (const void *) buf_y, (size_t) len_x) == 0) ? 1 : 0;
  61387. }
  61388. /* Boolean/any -> coerce boolean to number and try again. If boolean is
  61389. * compared to a pointer, the final comparison after coercion now always
  61390. * yields false (as pointer vs. number compares to false), but this is
  61391. * not special cased.
  61392. */
  61393. if (DUK_TVAL_IS_BOOLEAN(tv_x)) {
  61394. tv_tmp = tv_x;
  61395. tv_x = tv_y;
  61396. tv_y = tv_tmp;
  61397. }
  61398. if (DUK_TVAL_IS_BOOLEAN(tv_y)) {
  61399. /* ToNumber(bool) is +1.0 or 0.0. Tagged boolean value is always 0 or 1. */
  61400. duk_bool_t rc;
  61401. DUK_ASSERT(DUK_TVAL_GET_BOOLEAN(tv_y) == 0 || DUK_TVAL_GET_BOOLEAN(tv_y) == 1);
  61402. duk_push_tval(ctx, tv_x);
  61403. duk_push_int(ctx, DUK_TVAL_GET_BOOLEAN(tv_y));
  61404. rc = duk_js_equals_helper(thr, duk_get_tval(ctx, -2), duk_get_tval(ctx, -1), 0 /*flags:nonstrict*/);
  61405. duk_pop_2(ctx);
  61406. return rc;
  61407. }
  61408. /* String-number-buffer/object -> coerce object to primitive (apparently without hint), then try again. */
  61409. if ((DUK_TVAL_IS_STRING(tv_x) || DUK_TVAL_IS_NUMBER(tv_x) || DUK_TVAL_IS_BUFFER(tv_x)) &&
  61410. DUK_TVAL_IS_OBJECT(tv_y)) {
  61411. tv_tmp = tv_x;
  61412. tv_x = tv_y;
  61413. tv_y = tv_tmp;
  61414. }
  61415. if (DUK_TVAL_IS_OBJECT(tv_x) &&
  61416. (DUK_TVAL_IS_STRING(tv_y) || DUK_TVAL_IS_NUMBER(tv_y) || DUK_TVAL_IS_BUFFER(tv_y))) {
  61417. duk_bool_t rc;
  61418. duk_push_tval(ctx, tv_x);
  61419. duk_push_tval(ctx, tv_y);
  61420. duk_to_primitive(ctx, -2, DUK_HINT_NONE); /* apparently no hint? */
  61421. rc = duk_js_equals_helper(thr, duk_get_tval(ctx, -2), duk_get_tval(ctx, -1), 0 /*flags:nonstrict*/);
  61422. duk_pop_2(ctx);
  61423. return rc;
  61424. }
  61425. /* Nothing worked -> not equal. */
  61426. return 0;
  61427. }
  61428. /*
  61429. * Comparisons (x >= y, x > y, x <= y, x < y)
  61430. *
  61431. * E5 Section 11.8.5: implement 'x < y' and then use negate and eval_left_first
  61432. * flags to get the rest.
  61433. */
  61434. /* XXX: this should probably just operate on the stack top, because it
  61435. * needs to push stuff on the stack anyway...
  61436. */
  61437. 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) {
  61438. duk_size_t prefix_len;
  61439. duk_small_int_t rc;
  61440. prefix_len = (len1 <= len2 ? len1 : len2);
  61441. /* DUK_MEMCMP() is guaranteed to return zero (equal) for zero length
  61442. * inputs so no zero length check is needed.
  61443. */
  61444. rc = DUK_MEMCMP((const void *) buf1,
  61445. (const void *) buf2,
  61446. (size_t) prefix_len);
  61447. if (rc < 0) {
  61448. return -1;
  61449. } else if (rc > 0) {
  61450. return 1;
  61451. }
  61452. /* prefix matches, lengths matter now */
  61453. if (len1 < len2) {
  61454. /* e.g. "x" < "xx" */
  61455. return -1;
  61456. } else if (len1 > len2) {
  61457. return 1;
  61458. }
  61459. return 0;
  61460. }
  61461. DUK_INTERNAL duk_small_int_t duk_js_string_compare(duk_hstring *h1, duk_hstring *h2) {
  61462. /*
  61463. * String comparison (E5 Section 11.8.5, step 4), which
  61464. * needs to compare codepoint by codepoint.
  61465. *
  61466. * However, UTF-8 allows us to use strcmp directly: the shared
  61467. * prefix will be encoded identically (UTF-8 has unique encoding)
  61468. * and the first differing character can be compared with a simple
  61469. * unsigned byte comparison (which strcmp does).
  61470. *
  61471. * This will not work properly for non-xutf-8 strings, but this
  61472. * is not an issue for compliance.
  61473. */
  61474. DUK_ASSERT(h1 != NULL);
  61475. DUK_ASSERT(h2 != NULL);
  61476. return duk_js_data_compare((const duk_uint8_t *) DUK_HSTRING_GET_DATA(h1),
  61477. (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h2),
  61478. (duk_size_t) DUK_HSTRING_GET_BYTELEN(h1),
  61479. (duk_size_t) DUK_HSTRING_GET_BYTELEN(h2));
  61480. }
  61481. #if 0 /* unused */
  61482. DUK_INTERNAL duk_small_int_t duk_js_buffer_compare(duk_heap *heap, duk_hbuffer *h1, duk_hbuffer *h2) {
  61483. /* Similar to String comparison. */
  61484. DUK_ASSERT(h1 != NULL);
  61485. DUK_ASSERT(h2 != NULL);
  61486. DUK_UNREF(heap);
  61487. return duk_js_data_compare((const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(heap, h1),
  61488. (const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(heap, h2),
  61489. (duk_size_t) DUK_HBUFFER_GET_SIZE(h1),
  61490. (duk_size_t) DUK_HBUFFER_GET_SIZE(h2));
  61491. }
  61492. #endif
  61493. DUK_INTERNAL duk_bool_t duk_js_compare_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_int_t flags) {
  61494. duk_context *ctx = (duk_context *) thr;
  61495. duk_double_t d1, d2;
  61496. duk_small_int_t c1, c2;
  61497. duk_small_int_t s1, s2;
  61498. duk_small_int_t rc;
  61499. duk_bool_t retval;
  61500. /* Fast path for fastints */
  61501. #if defined(DUK_USE_FASTINT)
  61502. if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) {
  61503. duk_int64_t v1 = DUK_TVAL_GET_FASTINT(tv_x);
  61504. duk_int64_t v2 = DUK_TVAL_GET_FASTINT(tv_y);
  61505. if (v1 < v2) {
  61506. /* 'lt is true' */
  61507. retval = 1;
  61508. } else {
  61509. retval = 0;
  61510. }
  61511. if (flags & DUK_COMPARE_FLAG_NEGATE) {
  61512. retval ^= 1;
  61513. }
  61514. return retval;
  61515. }
  61516. #endif /* DUK_USE_FASTINT */
  61517. /* Fast path for numbers (one of which may be a fastint) */
  61518. #if 1 /* XXX: make fast paths optional for size minimization? */
  61519. if (DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y)) {
  61520. d1 = DUK_TVAL_GET_NUMBER(tv_x);
  61521. d2 = DUK_TVAL_GET_NUMBER(tv_y);
  61522. c1 = DUK_FPCLASSIFY(d1);
  61523. c2 = DUK_FPCLASSIFY(d2);
  61524. if (c1 == DUK_FP_NORMAL && c2 == DUK_FP_NORMAL) {
  61525. /* XXX: this is a very narrow check, and doesn't cover
  61526. * zeroes, subnormals, infinities, which compare normally.
  61527. */
  61528. if (d1 < d2) {
  61529. /* 'lt is true' */
  61530. retval = 1;
  61531. } else {
  61532. retval = 0;
  61533. }
  61534. if (flags & DUK_COMPARE_FLAG_NEGATE) {
  61535. retval ^= 1;
  61536. }
  61537. return retval;
  61538. }
  61539. }
  61540. #endif
  61541. /* Slow path */
  61542. duk_push_tval(ctx, tv_x);
  61543. duk_push_tval(ctx, tv_y);
  61544. if (flags & DUK_COMPARE_FLAG_EVAL_LEFT_FIRST) {
  61545. duk_to_primitive(ctx, -2, DUK_HINT_NUMBER);
  61546. duk_to_primitive(ctx, -1, DUK_HINT_NUMBER);
  61547. } else {
  61548. duk_to_primitive(ctx, -1, DUK_HINT_NUMBER);
  61549. duk_to_primitive(ctx, -2, DUK_HINT_NUMBER);
  61550. }
  61551. /* Note: reuse variables */
  61552. tv_x = duk_get_tval(ctx, -2);
  61553. tv_y = duk_get_tval(ctx, -1);
  61554. if (DUK_TVAL_IS_STRING(tv_x) && DUK_TVAL_IS_STRING(tv_y)) {
  61555. duk_hstring *h1 = DUK_TVAL_GET_STRING(tv_x);
  61556. duk_hstring *h2 = DUK_TVAL_GET_STRING(tv_y);
  61557. DUK_ASSERT(h1 != NULL);
  61558. DUK_ASSERT(h2 != NULL);
  61559. rc = duk_js_string_compare(h1, h2);
  61560. if (rc < 0) {
  61561. goto lt_true;
  61562. } else {
  61563. goto lt_false;
  61564. }
  61565. } else {
  61566. /* Ordering should not matter (E5 Section 11.8.5, step 3.a) but
  61567. * preserve it just in case.
  61568. */
  61569. if (flags & DUK_COMPARE_FLAG_EVAL_LEFT_FIRST) {
  61570. d1 = duk_to_number(ctx, -2);
  61571. d2 = duk_to_number(ctx, -1);
  61572. } else {
  61573. d2 = duk_to_number(ctx, -1);
  61574. d1 = duk_to_number(ctx, -2);
  61575. }
  61576. c1 = (duk_small_int_t) DUK_FPCLASSIFY(d1);
  61577. s1 = (duk_small_int_t) DUK_SIGNBIT(d1);
  61578. c2 = (duk_small_int_t) DUK_FPCLASSIFY(d2);
  61579. s2 = (duk_small_int_t) DUK_SIGNBIT(d2);
  61580. if (c1 == DUK_FP_NAN || c2 == DUK_FP_NAN) {
  61581. goto lt_undefined;
  61582. }
  61583. if (c1 == DUK_FP_ZERO && c2 == DUK_FP_ZERO) {
  61584. /* For all combinations: +0 < +0, +0 < -0, -0 < +0, -0 < -0,
  61585. * steps e, f, and g.
  61586. */
  61587. goto lt_false;
  61588. }
  61589. if (d1 == d2) {
  61590. goto lt_false;
  61591. }
  61592. if (c1 == DUK_FP_INFINITE && s1 == 0) {
  61593. /* x == +Infinity */
  61594. goto lt_false;
  61595. }
  61596. if (c2 == DUK_FP_INFINITE && s2 == 0) {
  61597. /* y == +Infinity */
  61598. goto lt_true;
  61599. }
  61600. if (c2 == DUK_FP_INFINITE && s2 != 0) {
  61601. /* y == -Infinity */
  61602. goto lt_false;
  61603. }
  61604. if (c1 == DUK_FP_INFINITE && s1 != 0) {
  61605. /* x == -Infinity */
  61606. goto lt_true;
  61607. }
  61608. if (d1 < d2) {
  61609. goto lt_true;
  61610. }
  61611. goto lt_false;
  61612. }
  61613. lt_undefined:
  61614. /* Note: undefined from Section 11.8.5 always results in false
  61615. * return (see e.g. Section 11.8.3) - hence special treatment here.
  61616. */
  61617. retval = 0;
  61618. goto cleanup;
  61619. lt_true:
  61620. if (flags & DUK_COMPARE_FLAG_NEGATE) {
  61621. retval = 0;
  61622. goto cleanup;
  61623. } else {
  61624. retval = 1;
  61625. goto cleanup;
  61626. }
  61627. /* never here */
  61628. lt_false:
  61629. if (flags & DUK_COMPARE_FLAG_NEGATE) {
  61630. retval = 1;
  61631. goto cleanup;
  61632. } else {
  61633. retval = 0;
  61634. goto cleanup;
  61635. }
  61636. /* never here */
  61637. cleanup:
  61638. duk_pop_2(ctx);
  61639. return retval;
  61640. }
  61641. /*
  61642. * instanceof
  61643. */
  61644. /*
  61645. * E5 Section 11.8.6 describes the main algorithm, which uses
  61646. * [[HasInstance]]. [[HasInstance]] is defined for only
  61647. * function objects:
  61648. *
  61649. * - Normal functions:
  61650. * E5 Section 15.3.5.3
  61651. * - Functions established with Function.prototype.bind():
  61652. * E5 Section 15.3.4.5.3
  61653. *
  61654. * For other objects, a TypeError is thrown.
  61655. *
  61656. * Limited Proxy support: don't support 'getPrototypeOf' trap but
  61657. * continue lookup in Proxy target if the value is a Proxy.
  61658. */
  61659. DUK_INTERNAL duk_bool_t duk_js_instanceof(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y) {
  61660. duk_context *ctx = (duk_context *) thr;
  61661. duk_hobject *func;
  61662. duk_hobject *val;
  61663. duk_hobject *proto;
  61664. duk_uint_t sanity;
  61665. /*
  61666. * Get the values onto the stack first. It would be possible to cover
  61667. * some normal cases without resorting to the value stack.
  61668. *
  61669. * The right hand side could be a light function (as they generally
  61670. * behave like objects). Light functions never have a 'prototype'
  61671. * property so E5.1 Section 15.3.5.3 step 3 always throws a TypeError.
  61672. * Using duk_require_hobject() is thus correct (except for error msg).
  61673. */
  61674. duk_push_tval(ctx, tv_x);
  61675. duk_push_tval(ctx, tv_y);
  61676. func = duk_require_hobject(ctx, -1);
  61677. /*
  61678. * For bound objects, [[HasInstance]] just calls the target function
  61679. * [[HasInstance]]. If that is again a bound object, repeat until
  61680. * we find a non-bound Function object.
  61681. */
  61682. /* XXX: this bound function resolution also happens elsewhere,
  61683. * move into a shared helper.
  61684. */
  61685. sanity = DUK_HOBJECT_BOUND_CHAIN_SANITY;
  61686. do {
  61687. /* check func supports [[HasInstance]] (this is checked for every function
  61688. * in the bound chain, including the final one)
  61689. */
  61690. if (!DUK_HOBJECT_IS_CALLABLE(func)) {
  61691. /*
  61692. * Note: of native Ecmascript objects, only Function instances
  61693. * have a [[HasInstance]] internal property. Custom objects might
  61694. * also have it, but not in current implementation.
  61695. *
  61696. * XXX: add a separate flag, DUK_HOBJECT_FLAG_ALLOW_INSTANCEOF?
  61697. */
  61698. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "invalid instanceof rval");
  61699. }
  61700. if (!DUK_HOBJECT_HAS_BOUND(func)) {
  61701. break;
  61702. }
  61703. /* [ ... lval rval ] */
  61704. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_TARGET); /* -> [ ... lval rval new_rval ] */
  61705. duk_replace(ctx, -1); /* -> [ ... lval new_rval ] */
  61706. func = duk_require_hobject(ctx, -1);
  61707. /* func support for [[HasInstance]] checked in the beginning of the loop */
  61708. } while (--sanity > 0);
  61709. if (sanity == 0) {
  61710. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_BOUND_CHAIN_LIMIT);
  61711. }
  61712. /*
  61713. * 'func' is now a non-bound object which supports [[HasInstance]]
  61714. * (which here just means DUK_HOBJECT_FLAG_CALLABLE). Move on
  61715. * to execute E5 Section 15.3.5.3.
  61716. */
  61717. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func));
  61718. DUK_ASSERT(DUK_HOBJECT_IS_CALLABLE(func));
  61719. /* [ ... lval rval(func) ] */
  61720. /* Handle lightfuncs through object coercion for now. */
  61721. /* XXX: direct implementation */
  61722. val = duk_get_hobject_or_lfunc_coerce(ctx, -2);
  61723. if (!val) {
  61724. goto pop_and_false;
  61725. }
  61726. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_PROTOTYPE); /* -> [ ... lval rval rval.prototype ] */
  61727. proto = duk_require_hobject(ctx, -1);
  61728. duk_pop(ctx); /* -> [ ... lval rval ] */
  61729. DUK_ASSERT(val != NULL);
  61730. #if defined(DUK_USE_ES6_PROXY)
  61731. val = duk_hobject_resolve_proxy_target(thr, val);
  61732. DUK_ASSERT(val != NULL);
  61733. #endif
  61734. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  61735. do {
  61736. /*
  61737. * Note: prototype chain is followed BEFORE first comparison. This
  61738. * means that the instanceof lval is never itself compared to the
  61739. * rval.prototype property. This is apparently intentional, see E5
  61740. * Section 15.3.5.3, step 4.a.
  61741. *
  61742. * Also note:
  61743. *
  61744. * js> (function() {}) instanceof Function
  61745. * true
  61746. * js> Function instanceof Function
  61747. * true
  61748. *
  61749. * For the latter, h_proto will be Function.prototype, which is the
  61750. * built-in Function prototype. Because Function.[[Prototype]] is
  61751. * also the built-in Function prototype, the result is true.
  61752. */
  61753. DUK_ASSERT(val != NULL);
  61754. val = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, val);
  61755. if (!val) {
  61756. goto pop_and_false;
  61757. }
  61758. DUK_ASSERT(val != NULL);
  61759. #if defined(DUK_USE_ES6_PROXY)
  61760. val = duk_hobject_resolve_proxy_target(thr, val);
  61761. #endif
  61762. if (val == proto) {
  61763. goto pop_and_true;
  61764. }
  61765. /* follow prototype chain */
  61766. } while (--sanity > 0);
  61767. if (sanity == 0) {
  61768. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  61769. }
  61770. DUK_UNREACHABLE();
  61771. pop_and_false:
  61772. duk_pop_2(ctx);
  61773. return 0;
  61774. pop_and_true:
  61775. duk_pop_2(ctx);
  61776. return 1;
  61777. }
  61778. /*
  61779. * in
  61780. */
  61781. /*
  61782. * E5 Sections 11.8.7, 8.12.6.
  61783. *
  61784. * Basically just a property existence check using [[HasProperty]].
  61785. */
  61786. DUK_INTERNAL duk_bool_t duk_js_in(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y) {
  61787. duk_context *ctx = (duk_context *) thr;
  61788. duk_bool_t retval;
  61789. /*
  61790. * Get the values onto the stack first. It would be possible to cover
  61791. * some normal cases without resorting to the value stack (e.g. if
  61792. * lval is already a string).
  61793. */
  61794. /* XXX: The ES5/5.1/6 specifications require that the key in 'key in obj'
  61795. * must be string coerced before the internal HasProperty() algorithm is
  61796. * invoked. A fast path skipping coercion could be safely implemented for
  61797. * numbers (as number-to-string coercion has no side effects). For ES6
  61798. * proxy behavior, the trap 'key' argument must be in a string coerced
  61799. * form (which is a shame).
  61800. */
  61801. /* TypeError if rval is not an object (or lightfunc which should behave
  61802. * like a Function instance).
  61803. */
  61804. duk_push_tval(ctx, tv_x);
  61805. duk_push_tval(ctx, tv_y);
  61806. duk_require_type_mask(ctx, -1, DUK_TYPE_MASK_OBJECT | DUK_TYPE_MASK_LIGHTFUNC);
  61807. duk_to_string(ctx, -2); /* coerce lval with ToString() */
  61808. retval = duk_hobject_hasprop(thr, duk_get_tval(ctx, -1), duk_get_tval(ctx, -2));
  61809. duk_pop_2(ctx);
  61810. return retval;
  61811. }
  61812. /*
  61813. * typeof
  61814. *
  61815. * E5 Section 11.4.3.
  61816. *
  61817. * Very straightforward. The only question is what to return for our
  61818. * non-standard tag / object types.
  61819. *
  61820. * There is an unfortunate string constant define naming problem with
  61821. * typeof return values for e.g. "Object" and "object"; careful with
  61822. * the built-in string defines. The LC_XXX defines are used for the
  61823. * lowercase variants now.
  61824. */
  61825. DUK_INTERNAL duk_hstring *duk_js_typeof(duk_hthread *thr, duk_tval *tv_x) {
  61826. duk_small_int_t stridx = 0;
  61827. switch (DUK_TVAL_GET_TAG(tv_x)) {
  61828. case DUK_TAG_UNDEFINED: {
  61829. stridx = DUK_STRIDX_LC_UNDEFINED;
  61830. break;
  61831. }
  61832. case DUK_TAG_NULL: {
  61833. /* Note: not a typo, "object" is returned for a null value */
  61834. stridx = DUK_STRIDX_LC_OBJECT;
  61835. break;
  61836. }
  61837. case DUK_TAG_BOOLEAN: {
  61838. stridx = DUK_STRIDX_LC_BOOLEAN;
  61839. break;
  61840. }
  61841. case DUK_TAG_POINTER: {
  61842. /* implementation specific */
  61843. stridx = DUK_STRIDX_LC_POINTER;
  61844. break;
  61845. }
  61846. case DUK_TAG_STRING: {
  61847. stridx = DUK_STRIDX_LC_STRING;
  61848. break;
  61849. }
  61850. case DUK_TAG_OBJECT: {
  61851. duk_hobject *obj = DUK_TVAL_GET_OBJECT(tv_x);
  61852. DUK_ASSERT(obj != NULL);
  61853. if (DUK_HOBJECT_IS_CALLABLE(obj)) {
  61854. stridx = DUK_STRIDX_LC_FUNCTION;
  61855. } else {
  61856. stridx = DUK_STRIDX_LC_OBJECT;
  61857. }
  61858. break;
  61859. }
  61860. case DUK_TAG_BUFFER: {
  61861. /* implementation specific */
  61862. stridx = DUK_STRIDX_LC_BUFFER;
  61863. break;
  61864. }
  61865. case DUK_TAG_LIGHTFUNC: {
  61866. stridx = DUK_STRIDX_LC_FUNCTION;
  61867. break;
  61868. }
  61869. #if defined(DUK_USE_FASTINT)
  61870. case DUK_TAG_FASTINT:
  61871. #endif
  61872. default: {
  61873. /* number */
  61874. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv_x));
  61875. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_x));
  61876. stridx = DUK_STRIDX_LC_NUMBER;
  61877. break;
  61878. }
  61879. }
  61880. DUK_ASSERT(stridx >= 0 && stridx < DUK_HEAP_NUM_STRINGS);
  61881. return DUK_HTHREAD_GET_STRING(thr, stridx);
  61882. }
  61883. /*
  61884. * Array index and length
  61885. *
  61886. * Array index: E5 Section 15.4
  61887. * Array length: E5 Section 15.4.5.1 steps 3.c - 3.d (array length write)
  61888. *
  61889. * The DUK_HSTRING_GET_ARRIDX_SLOW() and DUK_HSTRING_GET_ARRIDX_FAST() macros
  61890. * call duk_js_to_arrayindex_string_helper().
  61891. */
  61892. DUK_INTERNAL duk_small_int_t duk_js_to_arrayindex_raw_string(const duk_uint8_t *str, duk_uint32_t blen, duk_uarridx_t *out_idx) {
  61893. duk_uarridx_t res, new_res;
  61894. if (blen == 0 || blen > 10) {
  61895. goto parse_fail;
  61896. }
  61897. if (str[0] == (duk_uint8_t) '0' && blen > 1) {
  61898. goto parse_fail;
  61899. }
  61900. /* Accept 32-bit decimal integers, no leading zeroes, signs, etc.
  61901. * Leading zeroes are not accepted (zero index "0" is an exception
  61902. * handled above).
  61903. */
  61904. res = 0;
  61905. while (blen-- > 0) {
  61906. duk_uint8_t c = *str++;
  61907. if (c >= (duk_uint8_t) '0' && c <= (duk_uint8_t) '9') {
  61908. new_res = res * 10 + (duk_uint32_t) (c - (duk_uint8_t) '0');
  61909. if (new_res < res) {
  61910. /* overflow, more than 32 bits -> not an array index */
  61911. goto parse_fail;
  61912. }
  61913. res = new_res;
  61914. } else {
  61915. goto parse_fail;
  61916. }
  61917. }
  61918. *out_idx = res;
  61919. return 1;
  61920. parse_fail:
  61921. *out_idx = DUK_HSTRING_NO_ARRAY_INDEX;
  61922. return 0;
  61923. }
  61924. /* Called by duk_hstring.h macros */
  61925. DUK_INTERNAL duk_uarridx_t duk_js_to_arrayindex_string_helper(duk_hstring *h) {
  61926. duk_uarridx_t res;
  61927. duk_small_int_t rc;
  61928. if (!DUK_HSTRING_HAS_ARRIDX(h)) {
  61929. return DUK_HSTRING_NO_ARRAY_INDEX;
  61930. }
  61931. rc = duk_js_to_arrayindex_raw_string(DUK_HSTRING_GET_DATA(h),
  61932. DUK_HSTRING_GET_BYTELEN(h),
  61933. &res);
  61934. DUK_UNREF(rc);
  61935. DUK_ASSERT(rc != 0);
  61936. return res;
  61937. }
  61938. #line 1 "duk_js_var.c"
  61939. /*
  61940. * Identifier access and function closure handling.
  61941. *
  61942. * Provides the primitives for slow path identifier accesses: GETVAR,
  61943. * PUTVAR, DELVAR, etc. The fast path, direct register accesses, should
  61944. * be used for most identifier accesses. Consequently, these slow path
  61945. * primitives should be optimized for maximum compactness.
  61946. *
  61947. * Ecmascript environment records (declarative and object) are represented
  61948. * as internal objects with control keys. Environment records have a
  61949. * parent record ("outer environment reference") which is represented by
  61950. * the implicit prototype for technical reasons (in other words, it is a
  61951. * convenient field). The prototype chain is not followed in the ordinary
  61952. * sense for variable lookups.
  61953. *
  61954. * See identifier-handling.rst for more details on the identifier algorithms
  61955. * and the internal representation. See function-objects.rst for details on
  61956. * what function templates and instances are expected to look like.
  61957. *
  61958. * Care must be taken to avoid duk_tval pointer invalidation caused by
  61959. * e.g. value stack or object resizing.
  61960. *
  61961. * TODO: properties for function instances could be initialized much more
  61962. * efficiently by creating a property allocation for a certain size and
  61963. * filling in keys and values directly (and INCREFing both with "bulk incref"
  61964. * primitives.
  61965. *
  61966. * XXX: duk_hobject_getprop() and duk_hobject_putprop() calls are a bit
  61967. * awkward (especially because they follow the prototype chain); rework
  61968. * if "raw" own property helpers are added.
  61969. */
  61970. /* include removed: duk_internal.h */
  61971. /*
  61972. * Local result type for duk__get_identifier_reference() lookup.
  61973. */
  61974. typedef struct {
  61975. duk_hobject *holder; /* for object-bound identifiers */
  61976. duk_tval *value; /* for register-bound and declarative env identifiers */
  61977. duk_int_t attrs; /* property attributes for identifier (relevant if value != NULL) */
  61978. duk_tval *this_binding;
  61979. duk_hobject *env;
  61980. } duk__id_lookup_result;
  61981. /*
  61982. * Create a new function object based on a "template function" which contains
  61983. * compiled bytecode, constants, etc, but lacks a lexical environment.
  61984. *
  61985. * Ecmascript requires that each created closure is a separate object, with
  61986. * its own set of editable properties. However, structured property values
  61987. * (such as the formal arguments list and the variable map) are shared.
  61988. * Also the bytecode, constants, and inner functions are shared.
  61989. *
  61990. * See E5 Section 13.2 for detailed requirements on the function objects;
  61991. * there are no similar requirements for function "templates" which are an
  61992. * implementation dependent internal feature. Also see function-objects.rst
  61993. * for a discussion on the function instance properties provided by this
  61994. * implementation.
  61995. *
  61996. * Notes:
  61997. *
  61998. * * Order of internal properties should match frequency of use, since the
  61999. * properties will be linearly scanned on lookup (functions usually don't
  62000. * have enough properties to warrant a hash part).
  62001. *
  62002. * * The created closure is independent of its template; they do share the
  62003. * same 'data' buffer object, but the template object itself can be freed
  62004. * even if the closure object remains reachable.
  62005. */
  62006. DUK_LOCAL void duk__inc_data_inner_refcounts(duk_hthread *thr, duk_hcompiledfunction *f) {
  62007. duk_tval *tv, *tv_end;
  62008. duk_hobject **funcs, **funcs_end;
  62009. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, f) != NULL); /* compiled functions must be created 'atomically' */
  62010. DUK_UNREF(thr);
  62011. tv = DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, f);
  62012. tv_end = DUK_HCOMPILEDFUNCTION_GET_CONSTS_END(thr->heap, f);
  62013. while (tv < tv_end) {
  62014. DUK_TVAL_INCREF(thr, tv);
  62015. tv++;
  62016. }
  62017. funcs = DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, f);
  62018. funcs_end = DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(thr->heap, f);
  62019. while (funcs < funcs_end) {
  62020. DUK_HEAPHDR_INCREF(thr, (duk_heaphdr *) *funcs);
  62021. funcs++;
  62022. }
  62023. }
  62024. /* Push a new closure on the stack.
  62025. *
  62026. * Note: if fun_temp has NEWENV, i.e. a new lexical and variable declaration
  62027. * is created when the function is called, only outer_lex_env matters
  62028. * (outer_var_env is ignored and may or may not be same as outer_lex_env).
  62029. */
  62030. DUK_LOCAL const duk_uint16_t duk__closure_copy_proplist[] = {
  62031. /* order: most frequent to least frequent */
  62032. DUK_STRIDX_INT_VARMAP,
  62033. DUK_STRIDX_INT_FORMALS,
  62034. DUK_STRIDX_NAME,
  62035. DUK_STRIDX_INT_PC2LINE,
  62036. DUK_STRIDX_FILE_NAME,
  62037. DUK_STRIDX_INT_SOURCE
  62038. };
  62039. DUK_INTERNAL
  62040. void duk_js_push_closure(duk_hthread *thr,
  62041. duk_hcompiledfunction *fun_temp,
  62042. duk_hobject *outer_var_env,
  62043. duk_hobject *outer_lex_env) {
  62044. duk_context *ctx = (duk_context *) thr;
  62045. duk_hcompiledfunction *fun_clos;
  62046. duk_small_uint_t i;
  62047. duk_uint_t len_value;
  62048. DUK_ASSERT(fun_temp != NULL);
  62049. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, fun_temp) != NULL);
  62050. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_FUNCS(thr->heap, fun_temp) != NULL);
  62051. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_BYTECODE(thr->heap, fun_temp) != NULL);
  62052. DUK_ASSERT(outer_var_env != NULL);
  62053. DUK_ASSERT(outer_lex_env != NULL);
  62054. duk_push_compiledfunction(ctx);
  62055. duk_push_hobject(ctx, &fun_temp->obj); /* -> [ ... closure template ] */
  62056. fun_clos = (duk_hcompiledfunction *) duk_get_hcompiledfunction(ctx, -2);
  62057. DUK_ASSERT(fun_clos != NULL);
  62058. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) fun_clos));
  62059. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, fun_clos) == NULL);
  62060. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_FUNCS(thr->heap, fun_clos) == NULL);
  62061. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_BYTECODE(thr->heap, fun_clos) == NULL);
  62062. DUK_HCOMPILEDFUNCTION_SET_DATA(thr->heap, fun_clos, DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, fun_temp));
  62063. DUK_HCOMPILEDFUNCTION_SET_FUNCS(thr->heap, fun_clos, DUK_HCOMPILEDFUNCTION_GET_FUNCS(thr->heap, fun_temp));
  62064. DUK_HCOMPILEDFUNCTION_SET_BYTECODE(thr->heap, fun_clos, DUK_HCOMPILEDFUNCTION_GET_BYTECODE(thr->heap, fun_temp));
  62065. /* Note: all references inside 'data' need to get their refcounts
  62066. * upped too. This is the case because refcounts are decreased
  62067. * through every function referencing 'data' independently.
  62068. */
  62069. DUK_HBUFFER_INCREF(thr, DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, fun_clos));
  62070. duk__inc_data_inner_refcounts(thr, fun_temp);
  62071. fun_clos->nregs = fun_temp->nregs;
  62072. fun_clos->nargs = fun_temp->nargs;
  62073. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  62074. fun_clos->start_line = fun_temp->start_line;
  62075. fun_clos->end_line = fun_temp->end_line;
  62076. #endif
  62077. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, fun_clos) != NULL);
  62078. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_FUNCS(thr->heap, fun_clos) != NULL);
  62079. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_BYTECODE(thr->heap, fun_clos) != NULL);
  62080. /* XXX: could also copy from template, but there's no way to have any
  62081. * other value here now (used code has no access to the template).
  62082. */
  62083. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, &fun_clos->obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  62084. /*
  62085. * Init/assert flags, copying them where appropriate. Some flags
  62086. * (like NEWENV) are processed separately below.
  62087. */
  62088. /* XXX: copy flags using a mask */
  62089. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(&fun_clos->obj));
  62090. DUK_HOBJECT_SET_CONSTRUCTABLE(&fun_clos->obj); /* Note: not set in template (has no "prototype") */
  62091. DUK_ASSERT(DUK_HOBJECT_HAS_CONSTRUCTABLE(&fun_clos->obj));
  62092. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(&fun_clos->obj));
  62093. DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(&fun_clos->obj));
  62094. DUK_ASSERT(!DUK_HOBJECT_HAS_NATIVEFUNCTION(&fun_clos->obj));
  62095. DUK_ASSERT(!DUK_HOBJECT_HAS_THREAD(&fun_clos->obj));
  62096. /* DUK_HOBJECT_FLAG_ARRAY_PART: don't care */
  62097. if (DUK_HOBJECT_HAS_STRICT(&fun_temp->obj)) {
  62098. DUK_HOBJECT_SET_STRICT(&fun_clos->obj);
  62099. }
  62100. if (DUK_HOBJECT_HAS_NOTAIL(&fun_temp->obj)) {
  62101. DUK_HOBJECT_SET_NOTAIL(&fun_clos->obj);
  62102. }
  62103. /* DUK_HOBJECT_FLAG_NEWENV: handled below */
  62104. if (DUK_HOBJECT_HAS_NAMEBINDING(&fun_temp->obj)) {
  62105. /* Although NAMEBINDING is not directly needed for using
  62106. * function instances, it's needed by bytecode dump/load
  62107. * so copy it too.
  62108. */
  62109. DUK_HOBJECT_SET_NAMEBINDING(&fun_clos->obj);
  62110. }
  62111. if (DUK_HOBJECT_HAS_CREATEARGS(&fun_temp->obj)) {
  62112. DUK_HOBJECT_SET_CREATEARGS(&fun_clos->obj);
  62113. }
  62114. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARRAY(&fun_clos->obj));
  62115. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(&fun_clos->obj));
  62116. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(&fun_clos->obj));
  62117. /*
  62118. * Setup environment record properties based on the template and
  62119. * its flags.
  62120. *
  62121. * If DUK_HOBJECT_HAS_NEWENV(fun_temp) is true, the environment
  62122. * records represent identifiers "outside" the function; the
  62123. * "inner" environment records are created on demand. Otherwise,
  62124. * the environment records are those that will be directly used
  62125. * (e.g. for declarations).
  62126. *
  62127. * _Lexenv is always set; _Varenv defaults to _Lexenv if missing,
  62128. * so _Varenv is only set if _Lexenv != _Varenv.
  62129. *
  62130. * This is relatively complex, see doc/identifier-handling.rst.
  62131. */
  62132. if (DUK_HOBJECT_HAS_NEWENV(&fun_temp->obj)) {
  62133. DUK_HOBJECT_SET_NEWENV(&fun_clos->obj);
  62134. if (DUK_HOBJECT_HAS_NAMEBINDING(&fun_temp->obj)) {
  62135. duk_hobject *proto;
  62136. /*
  62137. * Named function expression, name needs to be bound
  62138. * in an intermediate environment record. The "outer"
  62139. * lexical/variable environment will thus be:
  62140. *
  62141. * a) { funcname: <func>, __prototype: outer_lex_env }
  62142. * b) { funcname: <func>, __prototype: <globalenv> } (if outer_lex_env missing)
  62143. */
  62144. DUK_ASSERT(duk_has_prop_stridx(ctx, -1, DUK_STRIDX_NAME)); /* required if NAMEBINDING set */
  62145. if (outer_lex_env) {
  62146. proto = outer_lex_env;
  62147. } else {
  62148. proto = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  62149. }
  62150. /* -> [ ... closure template env ] */
  62151. (void) duk_push_object_helper_proto(ctx,
  62152. DUK_HOBJECT_FLAG_EXTENSIBLE |
  62153. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV),
  62154. proto);
  62155. /* It's important that duk_xdef_prop() is a 'raw define' so that any
  62156. * properties in an ancestor are never an issue (they should never be
  62157. * e.g. non-writable, but just in case).
  62158. */
  62159. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_NAME); /* -> [ ... closure template env funcname ] */
  62160. duk_dup(ctx, -4); /* -> [ ... closure template env funcname closure ] */
  62161. duk_xdef_prop(ctx, -3, DUK_PROPDESC_FLAGS_NONE); /* -> [ ... closure template env ] */
  62162. /* env[funcname] = closure */
  62163. /* [ ... closure template env ] */
  62164. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_INT_LEXENV, DUK_PROPDESC_FLAGS_WC);
  62165. /* since closure has NEWENV, never define DUK_STRIDX_INT_VARENV, as it
  62166. * will be ignored anyway
  62167. */
  62168. /* [ ... closure template ] */
  62169. } else {
  62170. /*
  62171. * Other cases (function declaration, anonymous function expression,
  62172. * strict direct eval code). The "outer" environment will be whatever
  62173. * the caller gave us.
  62174. */
  62175. duk_push_hobject(ctx, outer_lex_env); /* -> [ ... closure template env ] */
  62176. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_INT_LEXENV, DUK_PROPDESC_FLAGS_WC);
  62177. /* since closure has NEWENV, never define DUK_STRIDX_INT_VARENV, as it
  62178. * will be ignored anyway
  62179. */
  62180. /* [ ... closure template ] */
  62181. }
  62182. } else {
  62183. /*
  62184. * Function gets no new environment when called. This is the
  62185. * case for global code, indirect eval code, and non-strict
  62186. * direct eval code. There is no direct correspondence to the
  62187. * E5 specification, as global/eval code is not exposed as a
  62188. * function.
  62189. */
  62190. DUK_ASSERT(!DUK_HOBJECT_HAS_NAMEBINDING(&fun_temp->obj));
  62191. duk_push_hobject(ctx, outer_lex_env); /* -> [ ... closure template env ] */
  62192. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_INT_LEXENV, DUK_PROPDESC_FLAGS_WC);
  62193. if (outer_var_env != outer_lex_env) {
  62194. duk_push_hobject(ctx, outer_var_env); /* -> [ ... closure template env ] */
  62195. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_INT_VARENV, DUK_PROPDESC_FLAGS_WC);
  62196. }
  62197. }
  62198. #ifdef DUK_USE_DDDPRINT
  62199. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_INT_VARENV);
  62200. duk_get_prop_stridx(ctx, -3, DUK_STRIDX_INT_LEXENV);
  62201. DUK_DDD(DUK_DDDPRINT("closure varenv -> %!ipT, lexenv -> %!ipT",
  62202. (duk_tval *) duk_get_tval(ctx, -2),
  62203. (duk_tval *) duk_get_tval(ctx, -1)));
  62204. duk_pop_2(ctx);
  62205. #endif
  62206. /*
  62207. * Copy some internal properties directly
  62208. *
  62209. * The properties will be writable and configurable, but not enumerable.
  62210. */
  62211. /* [ ... closure template ] */
  62212. DUK_DDD(DUK_DDDPRINT("copying properties: closure=%!iT, template=%!iT",
  62213. (duk_tval *) duk_get_tval(ctx, -2),
  62214. (duk_tval *) duk_get_tval(ctx, -1)));
  62215. for (i = 0; i < (duk_small_uint_t) (sizeof(duk__closure_copy_proplist) / sizeof(duk_uint16_t)); i++) {
  62216. duk_small_int_t stridx = (duk_small_int_t) duk__closure_copy_proplist[i];
  62217. if (duk_get_prop_stridx(ctx, -1, stridx)) {
  62218. /* [ ... closure template val ] */
  62219. DUK_DDD(DUK_DDDPRINT("copying property, stridx=%ld -> found", (long) stridx));
  62220. duk_xdef_prop_stridx(ctx, -3, stridx, DUK_PROPDESC_FLAGS_WC);
  62221. } else {
  62222. DUK_DDD(DUK_DDDPRINT("copying property, stridx=%ld -> not found", (long) stridx));
  62223. duk_pop(ctx);
  62224. }
  62225. }
  62226. /*
  62227. * "length" maps to number of formals (E5 Section 13.2) for function
  62228. * declarations/expressions (non-bound functions). Note that 'nargs'
  62229. * is NOT necessarily equal to the number of arguments.
  62230. */
  62231. /* [ ... closure template ] */
  62232. len_value = 0;
  62233. /* XXX: use helper for size optimization */
  62234. if (duk_get_prop_stridx(ctx, -2, DUK_STRIDX_INT_FORMALS)) {
  62235. /* [ ... closure template formals ] */
  62236. DUK_ASSERT(duk_has_prop_stridx(ctx, -1, DUK_STRIDX_LENGTH));
  62237. DUK_ASSERT(duk_get_length(ctx, -1) <= DUK_UINT_MAX); /* formal arg limits */
  62238. len_value = (duk_uint_t) duk_get_length(ctx, -1);
  62239. } else {
  62240. /* XXX: what to do if _Formals is not empty but compiler has
  62241. * optimized it away -- read length from an explicit property
  62242. * then?
  62243. */
  62244. }
  62245. duk_pop(ctx);
  62246. duk_push_uint(ctx, len_value); /* [ ... closure template len_value ] */
  62247. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE);
  62248. /*
  62249. * "prototype" is, by default, a fresh object with the "constructor"
  62250. * property.
  62251. *
  62252. * Note that this creates a circular reference for every function
  62253. * instance (closure) which prevents refcount-based collection of
  62254. * function instances.
  62255. *
  62256. * XXX: Try to avoid creating the default prototype object, because
  62257. * many functions are not used as constructors and the default
  62258. * prototype is unnecessary. Perhaps it could be created on-demand
  62259. * when it is first accessed?
  62260. */
  62261. /* [ ... closure template ] */
  62262. duk_push_object(ctx); /* -> [ ... closure template newobj ] */
  62263. duk_dup(ctx, -3); /* -> [ ... closure template newobj closure ] */
  62264. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_CONSTRUCTOR, DUK_PROPDESC_FLAGS_WC); /* -> [ ... closure template newobj ] */
  62265. duk_compact(ctx, -1); /* compact the prototype */
  62266. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_PROTOTYPE, DUK_PROPDESC_FLAGS_W); /* -> [ ... closure template ] */
  62267. /*
  62268. * "arguments" and "caller" must be mapped to throwers for strict
  62269. * mode and bound functions (E5 Section 15.3.5).
  62270. *
  62271. * XXX: This is expensive to have for every strict function instance.
  62272. * Try to implement as virtual properties or on-demand created properties.
  62273. */
  62274. /* [ ... closure template ] */
  62275. if (DUK_HOBJECT_HAS_STRICT(&fun_clos->obj)) {
  62276. duk_xdef_prop_stridx_thrower(ctx, -2, DUK_STRIDX_CALLER, DUK_PROPDESC_FLAGS_NONE);
  62277. duk_xdef_prop_stridx_thrower(ctx, -2, DUK_STRIDX_LC_ARGUMENTS, DUK_PROPDESC_FLAGS_NONE);
  62278. } else {
  62279. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  62280. DUK_DDD(DUK_DDDPRINT("function is non-strict and non-standard 'caller' property in use, add initial 'null' value"));
  62281. duk_push_null(ctx);
  62282. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_CALLER, DUK_PROPDESC_FLAGS_NONE);
  62283. #else
  62284. DUK_DDD(DUK_DDDPRINT("function is non-strict and non-standard 'caller' property not used"));
  62285. #endif
  62286. }
  62287. /*
  62288. * "name" is a non-standard property found in at least V8, Rhino, smjs.
  62289. * For Rhino and smjs it is non-writable, non-enumerable, and non-configurable;
  62290. * for V8 it is writable, non-enumerable, non-configurable. It is also defined
  62291. * for an anonymous function expression in which case the value is an empty string.
  62292. * We could also leave name 'undefined' for anonymous functions but that would
  62293. * differ from behavior of other engines, so use an empty string.
  62294. *
  62295. * XXX: make optional? costs something per function.
  62296. */
  62297. /* [ ... closure template ] */
  62298. if (duk_get_prop_stridx(ctx, -1, DUK_STRIDX_NAME)) {
  62299. /* [ ... closure template name ] */
  62300. DUK_ASSERT(duk_is_string(ctx, -1));
  62301. } else {
  62302. /* [ ... closure template undefined ] */
  62303. duk_pop(ctx);
  62304. duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING);
  62305. }
  62306. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE); /* -> [ ... closure template ] */
  62307. /*
  62308. * Compact the closure, in most cases no properties will be added later.
  62309. * Also, without this the closures end up having unused property slots
  62310. * (e.g. in Duktape 0.9.0, 8 slots would be allocated and only 7 used).
  62311. * A better future solution would be to allocate the closure directly
  62312. * to correct size (and setup the properties directly without going
  62313. * through the API).
  62314. */
  62315. duk_compact(ctx, -2);
  62316. /*
  62317. * Some assertions (E5 Section 13.2).
  62318. */
  62319. DUK_ASSERT(DUK_HOBJECT_GET_CLASS_NUMBER(&fun_clos->obj) == DUK_HOBJECT_CLASS_FUNCTION);
  62320. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, &fun_clos->obj) == thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  62321. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(&fun_clos->obj));
  62322. DUK_ASSERT(duk_has_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH) != 0);
  62323. DUK_ASSERT(duk_has_prop_stridx(ctx, -2, DUK_STRIDX_PROTOTYPE) != 0);
  62324. DUK_ASSERT(duk_has_prop_stridx(ctx, -2, DUK_STRIDX_NAME) != 0); /* non-standard */
  62325. DUK_ASSERT(!DUK_HOBJECT_HAS_STRICT(&fun_clos->obj) ||
  62326. duk_has_prop_stridx(ctx, -2, DUK_STRIDX_CALLER) != 0);
  62327. DUK_ASSERT(!DUK_HOBJECT_HAS_STRICT(&fun_clos->obj) ||
  62328. duk_has_prop_stridx(ctx, -2, DUK_STRIDX_LC_ARGUMENTS) != 0);
  62329. /*
  62330. * Finish
  62331. */
  62332. /* [ ... closure template ] */
  62333. DUK_DDD(DUK_DDDPRINT("created function instance: template=%!iT -> closure=%!iT",
  62334. (duk_tval *) duk_get_tval(ctx, -1),
  62335. (duk_tval *) duk_get_tval(ctx, -2)));
  62336. duk_pop(ctx);
  62337. /* [ ... closure ] */
  62338. }
  62339. /*
  62340. * Delayed activation environment record initialization (for functions
  62341. * with NEWENV).
  62342. *
  62343. * The non-delayed initialization is handled by duk_handle_call().
  62344. */
  62345. /* shared helper */
  62346. DUK_INTERNAL
  62347. duk_hobject *duk_create_activation_environment_record(duk_hthread *thr,
  62348. duk_hobject *func,
  62349. duk_size_t idx_bottom) {
  62350. duk_context *ctx = (duk_context *) thr;
  62351. duk_hobject *env;
  62352. duk_hobject *parent;
  62353. duk_tval *tv;
  62354. DUK_ASSERT(thr != NULL);
  62355. DUK_ASSERT(func != NULL);
  62356. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_INT_LEXENV(thr));
  62357. if (tv) {
  62358. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  62359. DUK_ASSERT(DUK_HOBJECT_IS_ENV(DUK_TVAL_GET_OBJECT(tv)));
  62360. parent = DUK_TVAL_GET_OBJECT(tv);
  62361. } else {
  62362. parent = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  62363. }
  62364. (void) duk_push_object_helper(ctx,
  62365. DUK_HOBJECT_FLAG_EXTENSIBLE |
  62366. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV),
  62367. -1); /* no prototype, updated below */
  62368. env = duk_require_hobject(ctx, -1);
  62369. DUK_ASSERT(env != NULL);
  62370. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, env, parent); /* parent env is the prototype */
  62371. /* open scope information, for compiled functions only */
  62372. if (DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) {
  62373. duk_push_hthread(ctx, thr);
  62374. duk_xdef_prop_stridx_wec(ctx, -2, DUK_STRIDX_INT_THREAD);
  62375. duk_push_hobject(ctx, func);
  62376. duk_xdef_prop_stridx_wec(ctx, -2, DUK_STRIDX_INT_CALLEE);
  62377. duk_push_size_t(ctx, idx_bottom);
  62378. duk_xdef_prop_stridx_wec(ctx, -2, DUK_STRIDX_INT_REGBASE);
  62379. }
  62380. return env;
  62381. }
  62382. DUK_INTERNAL
  62383. void duk_js_init_activation_environment_records_delayed(duk_hthread *thr,
  62384. duk_activation *act) {
  62385. duk_context *ctx = (duk_context *) thr;
  62386. duk_hobject *func;
  62387. duk_hobject *env;
  62388. func = DUK_ACT_GET_FUNC(act);
  62389. DUK_ASSERT(func != NULL);
  62390. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func)); /* bound functions are never in act 'func' */
  62391. /*
  62392. * Delayed initialization only occurs for 'NEWENV' functions.
  62393. */
  62394. DUK_ASSERT(DUK_HOBJECT_HAS_NEWENV(func));
  62395. DUK_ASSERT(act->lex_env == NULL);
  62396. DUK_ASSERT(act->var_env == NULL);
  62397. env = duk_create_activation_environment_record(thr, func, act->idx_bottom);
  62398. DUK_ASSERT(env != NULL);
  62399. DUK_DDD(DUK_DDDPRINT("created delayed fresh env: %!ipO", (duk_heaphdr *) env));
  62400. #ifdef DUK_USE_DDDPRINT
  62401. {
  62402. duk_hobject *p = env;
  62403. while (p) {
  62404. DUK_DDD(DUK_DDDPRINT(" -> %!ipO", (duk_heaphdr *) p));
  62405. p = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, p);
  62406. }
  62407. }
  62408. #endif
  62409. act->lex_env = env;
  62410. act->var_env = env;
  62411. DUK_HOBJECT_INCREF(thr, env); /* XXX: incref by count (here 2 times) */
  62412. DUK_HOBJECT_INCREF(thr, env);
  62413. duk_pop(ctx);
  62414. }
  62415. /*
  62416. * Closing environment records.
  62417. *
  62418. * The environment record MUST be closed with the thread where its activation
  62419. * is. In other words (if 'env' is open):
  62420. *
  62421. * - 'thr' must match _env.thread
  62422. * - 'func' must match _env.callee
  62423. * - 'regbase' must match _env.regbase
  62424. *
  62425. * These are not looked up from the env to minimize code size.
  62426. *
  62427. * XXX: should access the own properties directly instead of using the API
  62428. */
  62429. DUK_INTERNAL void duk_js_close_environment_record(duk_hthread *thr, duk_hobject *env, duk_hobject *func, duk_size_t regbase) {
  62430. duk_context *ctx = (duk_context *) thr;
  62431. duk_uint_fast32_t i;
  62432. DUK_ASSERT(thr != NULL);
  62433. DUK_ASSERT(env != NULL);
  62434. /* func is NULL for lightfuncs */
  62435. if (!DUK_HOBJECT_IS_DECENV(env) || DUK_HOBJECT_HAS_ENVRECCLOSED(env)) {
  62436. DUK_DDD(DUK_DDDPRINT("environment record not a declarative record, "
  62437. "or already closed: %!iO",
  62438. (duk_heaphdr *) env));
  62439. return;
  62440. }
  62441. DUK_DDD(DUK_DDDPRINT("closing environment record: %!iO, func: %!iO, regbase: %ld",
  62442. (duk_heaphdr *) env, (duk_heaphdr *) func, (long) regbase));
  62443. duk_push_hobject(ctx, env);
  62444. /* assertions: env must be closed in the same thread as where it runs */
  62445. #ifdef DUK_USE_ASSERTIONS
  62446. {
  62447. /* [... env] */
  62448. if (duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_CALLEE)) {
  62449. DUK_ASSERT(duk_is_object(ctx, -1));
  62450. DUK_ASSERT(duk_get_hobject(ctx, -1) == (duk_hobject *) func);
  62451. }
  62452. duk_pop(ctx);
  62453. if (duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_THREAD)) {
  62454. DUK_ASSERT(duk_is_object(ctx, -1));
  62455. DUK_ASSERT(duk_get_hobject(ctx, -1) == (duk_hobject *) thr);
  62456. }
  62457. duk_pop(ctx);
  62458. if (duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_REGBASE)) {
  62459. DUK_ASSERT(duk_is_number(ctx, -1));
  62460. DUK_ASSERT(duk_get_number(ctx, -1) == (double) regbase);
  62461. }
  62462. duk_pop(ctx);
  62463. /* [... env] */
  62464. }
  62465. #endif
  62466. if (func != NULL && DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) {
  62467. duk_hobject *varmap;
  62468. duk_hstring *key;
  62469. duk_tval *tv;
  62470. duk_uint_t regnum;
  62471. /* XXX: additional conditions when to close variables? we don't want to do it
  62472. * unless the environment may have "escaped" (referenced in a function closure).
  62473. * With delayed environments, the existence is probably good enough of a check.
  62474. */
  62475. /* XXX: any way to detect faster whether something needs to be closed?
  62476. * We now look up _Callee and then skip the rest.
  62477. */
  62478. /* Note: we rely on the _Varmap having a bunch of nice properties, like:
  62479. * - being compacted and unmodified during this process
  62480. * - not containing an array part
  62481. * - having correct value types
  62482. */
  62483. /* [... env] */
  62484. if (!duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_CALLEE)) {
  62485. DUK_DDD(DUK_DDDPRINT("env has no callee property, nothing to close; re-delete the control properties just in case"));
  62486. duk_pop(ctx);
  62487. goto skip_varmap;
  62488. }
  62489. /* [... env callee] */
  62490. if (!duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VARMAP)) {
  62491. DUK_DDD(DUK_DDDPRINT("callee has no varmap property, nothing to close; delete the control properties"));
  62492. duk_pop_2(ctx);
  62493. goto skip_varmap;
  62494. }
  62495. varmap = duk_require_hobject(ctx, -1);
  62496. DUK_ASSERT(varmap != NULL);
  62497. DUK_DDD(DUK_DDDPRINT("varmap: %!O", (duk_heaphdr *) varmap));
  62498. /* [... env callee varmap] */
  62499. DUK_DDD(DUK_DDDPRINT("copying bound register values, %ld bound regs", (long) DUK_HOBJECT_GET_ENEXT(varmap)));
  62500. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(varmap); i++) {
  62501. key = DUK_HOBJECT_E_GET_KEY(thr->heap, varmap, i);
  62502. DUK_ASSERT(key != NULL); /* assume keys are compacted */
  62503. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, varmap, i)); /* assume plain values */
  62504. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, varmap, i);
  62505. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); /* assume value is a number */
  62506. regnum = (duk_uint_t) DUK_TVAL_GET_NUMBER(tv);
  62507. DUK_ASSERT_DISABLE(regnum >= 0); /* unsigned */
  62508. DUK_ASSERT(regnum < ((duk_hcompiledfunction *) func)->nregs); /* regnum is sane */
  62509. DUK_ASSERT(thr->valstack + regbase + regnum >= thr->valstack);
  62510. DUK_ASSERT(thr->valstack + regbase + regnum < thr->valstack_top);
  62511. /* XXX: slightly awkward */
  62512. duk_push_hstring(ctx, key);
  62513. duk_push_tval(ctx, thr->valstack + regbase + regnum);
  62514. DUK_DDD(DUK_DDDPRINT("closing identifier '%s' -> reg %ld, value %!T",
  62515. (const char *) duk_require_string(ctx, -2),
  62516. (long) regnum,
  62517. (duk_tval *) duk_get_tval(ctx, -1)));
  62518. /* [... env callee varmap key val] */
  62519. /* if property already exists, overwrites silently */
  62520. duk_xdef_prop(ctx, -5, DUK_PROPDESC_FLAGS_WE); /* writable but not deletable */
  62521. }
  62522. duk_pop_2(ctx);
  62523. /* [... env] */
  62524. }
  62525. skip_varmap:
  62526. /* [... env] */
  62527. duk_del_prop_stridx(ctx, -1, DUK_STRIDX_INT_CALLEE);
  62528. duk_del_prop_stridx(ctx, -1, DUK_STRIDX_INT_THREAD);
  62529. duk_del_prop_stridx(ctx, -1, DUK_STRIDX_INT_REGBASE);
  62530. duk_pop(ctx);
  62531. DUK_HOBJECT_SET_ENVRECCLOSED(env);
  62532. DUK_DDD(DUK_DDDPRINT("environment record after being closed: %!O",
  62533. (duk_heaphdr *) env));
  62534. }
  62535. /*
  62536. * GETIDREF: a GetIdentifierReference-like helper.
  62537. *
  62538. * Provides a parent traversing lookup and a single level lookup
  62539. * (for HasBinding).
  62540. *
  62541. * Instead of returning the value, returns a bunch of values allowing
  62542. * the caller to read, write, or delete the binding. Value pointers
  62543. * are duk_tval pointers which can be mutated directly as long as
  62544. * refcounts are properly updated. Note that any operation which may
  62545. * reallocate valstacks or compact objects may invalidate the returned
  62546. * duk_tval (but not object) pointers, so caller must be very careful.
  62547. *
  62548. * If starting environment record 'env' is given, 'act' is ignored.
  62549. * However, if 'env' is NULL, the caller may identify, in 'act', an
  62550. * activation which hasn't had its declarative environment initialized
  62551. * yet. The activation registers are then looked up, and its parent
  62552. * traversed normally.
  62553. *
  62554. * The 'out' structure values are only valid if the function returns
  62555. * success (non-zero).
  62556. */
  62557. /* lookup name from an open declarative record's registers */
  62558. DUK_LOCAL
  62559. duk_bool_t duk__getid_open_decl_env_regs(duk_hthread *thr,
  62560. duk_hstring *name,
  62561. duk_hobject *env,
  62562. duk__id_lookup_result *out) {
  62563. duk_hthread *env_thr;
  62564. duk_hobject *env_func;
  62565. duk_size_t env_regbase;
  62566. duk_hobject *varmap;
  62567. duk_tval *tv;
  62568. duk_size_t reg_rel;
  62569. duk_size_t idx;
  62570. DUK_ASSERT(thr != NULL);
  62571. DUK_ASSERT(name != NULL);
  62572. DUK_ASSERT(env != NULL);
  62573. DUK_ASSERT(out != NULL);
  62574. DUK_ASSERT(DUK_HOBJECT_IS_DECENV(env));
  62575. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_CALLEE(thr));
  62576. if (!tv) {
  62577. /* env is closed, should be missing _Callee, _Thread, _Regbase */
  62578. DUK_ASSERT(duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_CALLEE(thr)) == NULL);
  62579. DUK_ASSERT(duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_THREAD(thr)) == NULL);
  62580. DUK_ASSERT(duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_REGBASE(thr)) == NULL);
  62581. return 0;
  62582. }
  62583. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  62584. DUK_ASSERT(DUK_TVAL_GET_OBJECT(tv) != NULL);
  62585. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_TVAL_GET_OBJECT(tv)));
  62586. env_func = DUK_TVAL_GET_OBJECT(tv);
  62587. DUK_ASSERT(env_func != NULL);
  62588. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, env_func, DUK_HTHREAD_STRING_INT_VARMAP(thr));
  62589. if (!tv) {
  62590. return 0;
  62591. }
  62592. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  62593. varmap = DUK_TVAL_GET_OBJECT(tv);
  62594. DUK_ASSERT(varmap != NULL);
  62595. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, varmap, name);
  62596. if (!tv) {
  62597. return 0;
  62598. }
  62599. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  62600. reg_rel = (duk_size_t) DUK_TVAL_GET_NUMBER(tv);
  62601. DUK_ASSERT_DISABLE(reg_rel >= 0); /* unsigned */
  62602. DUK_ASSERT(reg_rel < ((duk_hcompiledfunction *) env_func)->nregs);
  62603. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_THREAD(thr));
  62604. DUK_ASSERT(tv != NULL);
  62605. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  62606. DUK_ASSERT(DUK_TVAL_GET_OBJECT(tv) != NULL);
  62607. DUK_ASSERT(DUK_HOBJECT_IS_THREAD(DUK_TVAL_GET_OBJECT(tv)));
  62608. env_thr = (duk_hthread *) DUK_TVAL_GET_OBJECT(tv);
  62609. DUK_ASSERT(env_thr != NULL);
  62610. /* Note: env_thr != thr is quite possible and normal, so careful
  62611. * with what thread is used for valstack lookup.
  62612. */
  62613. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_REGBASE(thr));
  62614. DUK_ASSERT(tv != NULL);
  62615. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  62616. env_regbase = (duk_size_t) DUK_TVAL_GET_NUMBER(tv);
  62617. idx = env_regbase + reg_rel;
  62618. tv = env_thr->valstack + idx;
  62619. DUK_ASSERT(tv >= env_thr->valstack && tv < env_thr->valstack_end); /* XXX: more accurate? */
  62620. out->value = tv;
  62621. out->attrs = DUK_PROPDESC_FLAGS_W; /* registers are mutable, non-deletable */
  62622. out->this_binding = NULL; /* implicit this value always undefined for
  62623. * declarative environment records.
  62624. */
  62625. out->env = env;
  62626. out->holder = NULL;
  62627. return 1;
  62628. }
  62629. /* lookup name from current activation record's functions' registers */
  62630. DUK_LOCAL
  62631. duk_bool_t duk__getid_activation_regs(duk_hthread *thr,
  62632. duk_hstring *name,
  62633. duk_activation *act,
  62634. duk__id_lookup_result *out) {
  62635. duk_tval *tv;
  62636. duk_hobject *func;
  62637. duk_hobject *varmap;
  62638. duk_size_t reg_rel;
  62639. duk_size_t idx;
  62640. DUK_ASSERT(thr != NULL);
  62641. DUK_ASSERT(name != NULL);
  62642. DUK_ASSERT(act != NULL);
  62643. DUK_ASSERT(out != NULL);
  62644. func = DUK_ACT_GET_FUNC(act);
  62645. DUK_ASSERT(func != NULL);
  62646. DUK_ASSERT(DUK_HOBJECT_HAS_NEWENV(func));
  62647. if (!DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) {
  62648. return 0;
  62649. }
  62650. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_INT_VARMAP(thr));
  62651. if (!tv) {
  62652. return 0;
  62653. }
  62654. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  62655. varmap = DUK_TVAL_GET_OBJECT(tv);
  62656. DUK_ASSERT(varmap != NULL);
  62657. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, varmap, name);
  62658. if (!tv) {
  62659. return 0;
  62660. }
  62661. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  62662. reg_rel = (duk_size_t) DUK_TVAL_GET_NUMBER(tv);
  62663. DUK_ASSERT_DISABLE(reg_rel >= 0);
  62664. DUK_ASSERT(reg_rel < ((duk_hcompiledfunction *) func)->nregs);
  62665. idx = act->idx_bottom + reg_rel;
  62666. DUK_ASSERT(idx >= act->idx_bottom);
  62667. tv = thr->valstack + idx;
  62668. out->value = tv;
  62669. out->attrs = DUK_PROPDESC_FLAGS_W; /* registers are mutable, non-deletable */
  62670. out->this_binding = NULL; /* implicit this value always undefined for
  62671. * declarative environment records.
  62672. */
  62673. out->env = NULL;
  62674. out->holder = NULL;
  62675. return 1;
  62676. }
  62677. DUK_LOCAL
  62678. duk_bool_t duk__get_identifier_reference(duk_hthread *thr,
  62679. duk_hobject *env,
  62680. duk_hstring *name,
  62681. duk_activation *act,
  62682. duk_bool_t parents,
  62683. duk__id_lookup_result *out) {
  62684. duk_tval *tv;
  62685. duk_tval *tv_target;
  62686. duk_tval tv_name;
  62687. duk_uint_t sanity;
  62688. DUK_ASSERT(thr != NULL);
  62689. DUK_ASSERT(env != NULL || act != NULL);
  62690. DUK_ASSERT(name != NULL);
  62691. DUK_ASSERT(out != NULL);
  62692. DUK_ASSERT(!env || DUK_HOBJECT_IS_ENV(env));
  62693. DUK_ASSERT(!env || !DUK_HOBJECT_HAS_ARRAY_PART(env));
  62694. /*
  62695. * Conceptually, we look for the identifier binding by starting from
  62696. * 'env' and following to chain of environment records (represented
  62697. * by the prototype chain).
  62698. *
  62699. * If 'env' is NULL, the current activation does not yet have an
  62700. * allocated declarative environment record; this should be treated
  62701. * exactly as if the environment record existed but had no bindings
  62702. * other than register bindings.
  62703. *
  62704. * Note: we assume that with the DUK_HOBJECT_FLAG_NEWENV cleared
  62705. * the environment will always be initialized immediately; hence
  62706. * a NULL 'env' should only happen with the flag set. This is the
  62707. * case for: (1) function calls, and (2) strict, direct eval calls.
  62708. */
  62709. if (env == NULL && act != NULL) {
  62710. duk_hobject *func;
  62711. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference: env is NULL, activation is non-NULL -> "
  62712. "delayed env case, look up activation regs first"));
  62713. /*
  62714. * Try registers
  62715. */
  62716. if (duk__getid_activation_regs(thr, name, act, out)) {
  62717. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference successful: "
  62718. "name=%!O -> value=%!T, attrs=%ld, this=%!T, env=%!O, holder=%!O "
  62719. "(found from register bindings when env=NULL)",
  62720. (duk_heaphdr *) name, (duk_tval *) out->value,
  62721. (long) out->attrs, (duk_tval *) out->this_binding,
  62722. (duk_heaphdr *) out->env, (duk_heaphdr *) out->holder));
  62723. return 1;
  62724. }
  62725. DUK_DDD(DUK_DDDPRINT("not found in current activation regs"));
  62726. /*
  62727. * Not found in registers, proceed to the parent record.
  62728. * Here we need to determine what the parent would be,
  62729. * if 'env' was not NULL (i.e. same logic as when initializing
  62730. * the record).
  62731. *
  62732. * Note that environment initialization is only deferred when
  62733. * DUK_HOBJECT_HAS_NEWENV is set, and this only happens for:
  62734. * - Function code
  62735. * - Strict eval code
  62736. *
  62737. * We only need to check _Lexenv here; _Varenv exists only if it
  62738. * differs from _Lexenv (and thus _Lexenv will also be present).
  62739. */
  62740. if (!parents) {
  62741. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference failed, no parent traversal "
  62742. "(not found from register bindings when env=NULL)"));
  62743. goto fail_not_found;
  62744. }
  62745. func = DUK_ACT_GET_FUNC(act);
  62746. DUK_ASSERT(func != NULL);
  62747. DUK_ASSERT(DUK_HOBJECT_HAS_NEWENV(func));
  62748. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_INT_LEXENV(thr));
  62749. if (tv) {
  62750. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  62751. env = DUK_TVAL_GET_OBJECT(tv);
  62752. } else {
  62753. DUK_ASSERT(duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_INT_VARENV(thr)) == NULL);
  62754. env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  62755. }
  62756. DUK_DDD(DUK_DDDPRINT("continue lookup from env: %!iO",
  62757. (duk_heaphdr *) env));
  62758. }
  62759. /*
  62760. * Prototype walking starting from 'env'.
  62761. *
  62762. * ('act' is not needed anywhere here.)
  62763. */
  62764. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  62765. while (env != NULL) {
  62766. duk_small_int_t cl;
  62767. duk_int_t attrs;
  62768. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference, name=%!O, considering env=%p -> %!iO",
  62769. (duk_heaphdr *) name,
  62770. (void *) env,
  62771. (duk_heaphdr *) env));
  62772. DUK_ASSERT(env != NULL);
  62773. DUK_ASSERT(DUK_HOBJECT_IS_ENV(env));
  62774. DUK_ASSERT(!DUK_HOBJECT_HAS_ARRAY_PART(env));
  62775. cl = DUK_HOBJECT_GET_CLASS_NUMBER(env);
  62776. DUK_ASSERT(cl == DUK_HOBJECT_CLASS_OBJENV || cl == DUK_HOBJECT_CLASS_DECENV);
  62777. if (cl == DUK_HOBJECT_CLASS_DECENV) {
  62778. /*
  62779. * Declarative environment record.
  62780. *
  62781. * Identifiers can never be stored in ancestors and are
  62782. * always plain values, so we can use an internal helper
  62783. * and access the value directly with an duk_tval ptr.
  62784. *
  62785. * A closed environment is only indicated by it missing
  62786. * the "book-keeping" properties required for accessing
  62787. * register-bound variables.
  62788. */
  62789. if (DUK_HOBJECT_HAS_ENVRECCLOSED(env)) {
  62790. /* already closed */
  62791. goto skip_regs;
  62792. }
  62793. if (duk__getid_open_decl_env_regs(thr, name, env, out)) {
  62794. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference successful: "
  62795. "name=%!O -> value=%!T, attrs=%ld, this=%!T, env=%!O, holder=%!O "
  62796. "(declarative environment record, scope open, found in regs)",
  62797. (duk_heaphdr *) name, (duk_tval *) out->value,
  62798. (long) out->attrs, (duk_tval *) out->this_binding,
  62799. (duk_heaphdr *) out->env, (duk_heaphdr *) out->holder));
  62800. return 1;
  62801. }
  62802. skip_regs:
  62803. tv = duk_hobject_find_existing_entry_tval_ptr_and_attrs(thr->heap, env, name, &attrs);
  62804. if (tv) {
  62805. out->value = tv;
  62806. out->attrs = attrs;
  62807. out->this_binding = NULL; /* implicit this value always undefined for
  62808. * declarative environment records.
  62809. */
  62810. out->env = env;
  62811. out->holder = env;
  62812. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference successful: "
  62813. "name=%!O -> value=%!T, attrs=%ld, this=%!T, env=%!O, holder=%!O "
  62814. "(declarative environment record, found in properties)",
  62815. (duk_heaphdr *) name, (duk_tval *) out->value,
  62816. (long) out->attrs, (duk_tval *) out->this_binding,
  62817. (duk_heaphdr *) out->env, (duk_heaphdr *) out->holder));
  62818. return 1;
  62819. }
  62820. } else {
  62821. /*
  62822. * Object environment record.
  62823. *
  62824. * Binding (target) object is an external, uncontrolled object.
  62825. * Identifier may be bound in an ancestor property, and may be
  62826. * an accessor. Target can also be a Proxy which we must support
  62827. * here.
  62828. */
  62829. /* XXX: we could save space by using _Target OR _This. If _Target, assume
  62830. * this binding is undefined. If _This, assumes this binding is _This, and
  62831. * target is also _This. One property would then be enough.
  62832. */
  62833. duk_hobject *target;
  62834. duk_bool_t found;
  62835. DUK_ASSERT(cl == DUK_HOBJECT_CLASS_OBJENV);
  62836. tv_target = duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_TARGET(thr));
  62837. DUK_ASSERT(tv_target != NULL);
  62838. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv_target));
  62839. target = DUK_TVAL_GET_OBJECT(tv_target);
  62840. DUK_ASSERT(target != NULL);
  62841. /* Target may be a Proxy or property may be an accessor, so we must
  62842. * use an actual, Proxy-aware hasprop check here.
  62843. *
  62844. * out->holder is NOT set to the actual duk_hobject where the
  62845. * property is found, but rather the object binding target object.
  62846. */
  62847. if (DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(target)) {
  62848. DUK_ASSERT(name != NULL);
  62849. DUK_TVAL_SET_STRING(&tv_name, name);
  62850. found = duk_hobject_hasprop(thr, tv_target, &tv_name);
  62851. } else {
  62852. /* XXX: duk_hobject_hasprop() would be correct for
  62853. * non-Proxy objects too, but it is about ~20-25%
  62854. * slower at present so separate code paths for
  62855. * Proxy and non-Proxy now.
  62856. */
  62857. found = duk_hobject_hasprop_raw(thr, target, name);
  62858. }
  62859. if (found) {
  62860. out->value = NULL; /* can't get value, may be accessor */
  62861. out->attrs = 0; /* irrelevant when out->value == NULL */
  62862. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_THIS(thr));
  62863. out->this_binding = tv; /* may be NULL */
  62864. out->env = env;
  62865. out->holder = target;
  62866. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference successful: "
  62867. "name=%!O -> value=%!T, attrs=%ld, this=%!T, env=%!O, holder=%!O "
  62868. "(object environment record)",
  62869. (duk_heaphdr *) name, (duk_tval *) out->value,
  62870. (long) out->attrs, (duk_tval *) out->this_binding,
  62871. (duk_heaphdr *) out->env, (duk_heaphdr *) out->holder));
  62872. return 1;
  62873. }
  62874. }
  62875. if (!parents) {
  62876. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference failed, no parent traversal "
  62877. "(not found from first traversed env)"));
  62878. goto fail_not_found;
  62879. }
  62880. if (sanity-- == 0) {
  62881. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  62882. }
  62883. env = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, env);
  62884. };
  62885. /*
  62886. * Not found (even in global object)
  62887. */
  62888. fail_not_found:
  62889. return 0;
  62890. }
  62891. /*
  62892. * HASVAR: check identifier binding from a given environment record
  62893. * without traversing its parents.
  62894. *
  62895. * This primitive is not exposed to user code as such, but is used
  62896. * internally for e.g. declaration binding instantiation.
  62897. *
  62898. * See E5 Sections:
  62899. * 10.2.1.1.1 HasBinding(N)
  62900. * 10.2.1.2.1 HasBinding(N)
  62901. *
  62902. * Note: strictness has no bearing on this check. Hence we don't take
  62903. * a 'strict' parameter.
  62904. */
  62905. #if 0 /*unused*/
  62906. DUK_INTERNAL
  62907. duk_bool_t duk_js_hasvar_envrec(duk_hthread *thr,
  62908. duk_hobject *env,
  62909. duk_hstring *name) {
  62910. duk__id_lookup_result ref;
  62911. duk_bool_t parents;
  62912. DUK_DDD(DUK_DDDPRINT("hasvar: thr=%p, env=%p, name=%!O "
  62913. "(env -> %!dO)",
  62914. (void *) thr, (void *) env, (duk_heaphdr *) name,
  62915. (duk_heaphdr *) env));
  62916. DUK_ASSERT(thr != NULL);
  62917. DUK_ASSERT(env != NULL);
  62918. DUK_ASSERT(name != NULL);
  62919. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(env);
  62920. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(name);
  62921. DUK_ASSERT(DUK_HOBJECT_IS_ENV(env));
  62922. DUK_ASSERT(!DUK_HOBJECT_HAS_ARRAY_PART(env));
  62923. /* lookup results is ignored */
  62924. parents = 0;
  62925. return duk__get_identifier_reference(thr, env, name, NULL, parents, &ref);
  62926. }
  62927. #endif
  62928. /*
  62929. * GETVAR
  62930. *
  62931. * See E5 Sections:
  62932. * 11.1.2 Identifier Reference
  62933. * 10.3.1 Identifier Resolution
  62934. * 11.13.1 Simple Assignment [example of where the Reference is GetValue'd]
  62935. * 8.7.1 GetValue (V)
  62936. * 8.12.1 [[GetOwnProperty]] (P)
  62937. * 8.12.2 [[GetProperty]] (P)
  62938. * 8.12.3 [[Get]] (P)
  62939. *
  62940. * If 'throw' is true, always leaves two values on top of stack: [val this].
  62941. *
  62942. * If 'throw' is false, returns 0 if identifier cannot be resolved, and the
  62943. * stack will be unaffected in this case. If identifier is resolved, returns
  62944. * 1 and leaves [val this] on top of stack.
  62945. *
  62946. * Note: the 'strict' flag of a reference returned by GetIdentifierReference
  62947. * is ignored by GetValue. Hence we don't take a 'strict' parameter.
  62948. *
  62949. * The 'throw' flag is needed for implementing 'typeof' for an unreferenced
  62950. * identifier. An unreference identifier in other contexts generates a
  62951. * ReferenceError.
  62952. */
  62953. DUK_LOCAL
  62954. duk_bool_t duk__getvar_helper(duk_hthread *thr,
  62955. duk_hobject *env,
  62956. duk_activation *act,
  62957. duk_hstring *name,
  62958. duk_bool_t throw_flag) {
  62959. duk_context *ctx = (duk_context *) thr;
  62960. duk__id_lookup_result ref;
  62961. duk_tval tv_tmp_obj;
  62962. duk_tval tv_tmp_key;
  62963. duk_bool_t parents;
  62964. DUK_DDD(DUK_DDDPRINT("getvar: thr=%p, env=%p, act=%p, name=%!O "
  62965. "(env -> %!dO)",
  62966. (void *) thr, (void *) env, (void *) act,
  62967. (duk_heaphdr *) name, (duk_heaphdr *) env));
  62968. DUK_ASSERT(thr != NULL);
  62969. DUK_ASSERT(name != NULL);
  62970. /* env and act may be NULL */
  62971. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(env);
  62972. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(name);
  62973. parents = 1; /* follow parent chain */
  62974. if (duk__get_identifier_reference(thr, env, name, act, parents, &ref)) {
  62975. if (ref.value) {
  62976. DUK_ASSERT(ref.this_binding == NULL); /* always for register bindings */
  62977. duk_push_tval(ctx, ref.value);
  62978. duk_push_undefined(ctx);
  62979. } else {
  62980. DUK_ASSERT(ref.holder != NULL);
  62981. /* Note: getprop may invoke any getter and invalidate any
  62982. * duk_tval pointers, so this must be done first.
  62983. */
  62984. if (ref.this_binding) {
  62985. duk_push_tval(ctx, ref.this_binding);
  62986. } else {
  62987. duk_push_undefined(ctx);
  62988. }
  62989. DUK_TVAL_SET_OBJECT(&tv_tmp_obj, ref.holder);
  62990. DUK_TVAL_SET_STRING(&tv_tmp_key, name);
  62991. (void) duk_hobject_getprop(thr, &tv_tmp_obj, &tv_tmp_key); /* [this value] */
  62992. /* ref.value, ref.this.binding invalidated here by getprop call */
  62993. duk_insert(ctx, -2); /* [this value] -> [value this] */
  62994. }
  62995. return 1;
  62996. } else {
  62997. if (throw_flag) {
  62998. DUK_ERROR(thr, DUK_ERR_REFERENCE_ERROR,
  62999. "identifier '%s' undefined",
  63000. (const char *) DUK_HSTRING_GET_DATA(name));
  63001. }
  63002. return 0;
  63003. }
  63004. }
  63005. DUK_INTERNAL
  63006. duk_bool_t duk_js_getvar_envrec(duk_hthread *thr,
  63007. duk_hobject *env,
  63008. duk_hstring *name,
  63009. duk_bool_t throw_flag) {
  63010. return duk__getvar_helper(thr, env, NULL, name, throw_flag);
  63011. }
  63012. DUK_INTERNAL
  63013. duk_bool_t duk_js_getvar_activation(duk_hthread *thr,
  63014. duk_activation *act,
  63015. duk_hstring *name,
  63016. duk_bool_t throw_flag) {
  63017. DUK_ASSERT(act != NULL);
  63018. return duk__getvar_helper(thr, act->lex_env, act, name, throw_flag);
  63019. }
  63020. /*
  63021. * PUTVAR
  63022. *
  63023. * See E5 Sections:
  63024. * 11.1.2 Identifier Reference
  63025. * 10.3.1 Identifier Resolution
  63026. * 11.13.1 Simple Assignment [example of where the Reference is PutValue'd]
  63027. * 8.7.2 PutValue (V,W) [see especially step 3.b, undefined -> automatic global in non-strict mode]
  63028. * 8.12.4 [[CanPut]] (P)
  63029. * 8.12.5 [[Put]] (P)
  63030. *
  63031. * Note: may invalidate any valstack (or object) duk_tval pointers because
  63032. * putting a value may reallocate any object or any valstack. Caller beware.
  63033. */
  63034. DUK_LOCAL
  63035. void duk__putvar_helper(duk_hthread *thr,
  63036. duk_hobject *env,
  63037. duk_activation *act,
  63038. duk_hstring *name,
  63039. duk_tval *val,
  63040. duk_bool_t strict) {
  63041. duk__id_lookup_result ref;
  63042. duk_tval tv_tmp_obj;
  63043. duk_tval tv_tmp_key;
  63044. duk_bool_t parents;
  63045. DUK_DDD(DUK_DDDPRINT("putvar: thr=%p, env=%p, act=%p, name=%!O, val=%p, strict=%ld "
  63046. "(env -> %!dO, val -> %!T)",
  63047. (void *) thr, (void *) env, (void *) act,
  63048. (duk_heaphdr *) name, (void *) val, (long) strict,
  63049. (duk_heaphdr *) env, (duk_tval *) val));
  63050. DUK_ASSERT(thr != NULL);
  63051. DUK_ASSERT(name != NULL);
  63052. DUK_ASSERT(val != NULL);
  63053. /* env and act may be NULL */
  63054. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(env);
  63055. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(name);
  63056. DUK_ASSERT_REFCOUNT_NONZERO_TVAL(val);
  63057. /*
  63058. * In strict mode E5 protects 'eval' and 'arguments' from being
  63059. * assigned to (or even declared anywhere). Attempt to do so
  63060. * should result in a compile time SyntaxError. See the internal
  63061. * design documentation for details.
  63062. *
  63063. * Thus, we should never come here, run-time, for strict code,
  63064. * and name 'eval' or 'arguments'.
  63065. */
  63066. DUK_ASSERT(!strict ||
  63067. (name != DUK_HTHREAD_STRING_EVAL(thr) &&
  63068. name != DUK_HTHREAD_STRING_LC_ARGUMENTS(thr)));
  63069. /*
  63070. * Lookup variable and update in-place if found.
  63071. */
  63072. parents = 1; /* follow parent chain */
  63073. if (duk__get_identifier_reference(thr, env, name, act, parents, &ref)) {
  63074. if (ref.value && (ref.attrs & DUK_PROPDESC_FLAG_WRITABLE)) {
  63075. /* Update duk_tval in-place if pointer provided and the
  63076. * property is writable. If the property is not writable
  63077. * (immutable binding), use duk_hobject_putprop() which
  63078. * will respect mutability.
  63079. */
  63080. duk_tval *tv_val;
  63081. DUK_ASSERT(ref.this_binding == NULL); /* always for register bindings */
  63082. tv_val = ref.value;
  63083. DUK_ASSERT(tv_val != NULL);
  63084. DUK_TVAL_SET_TVAL_UPDREF(thr, tv_val, val); /* side effects */
  63085. /* ref.value and ref.this_binding invalidated here */
  63086. } else {
  63087. DUK_ASSERT(ref.holder != NULL);
  63088. DUK_TVAL_SET_OBJECT(&tv_tmp_obj, ref.holder);
  63089. DUK_TVAL_SET_STRING(&tv_tmp_key, name);
  63090. (void) duk_hobject_putprop(thr, &tv_tmp_obj, &tv_tmp_key, val, strict);
  63091. /* ref.value and ref.this_binding invalidated here */
  63092. }
  63093. return;
  63094. }
  63095. /*
  63096. * Not found: write to global object (non-strict) or ReferenceError
  63097. * (strict); see E5 Section 8.7.2, step 3.
  63098. */
  63099. if (strict) {
  63100. DUK_DDD(DUK_DDDPRINT("identifier binding not found, strict => reference error"));
  63101. DUK_ERROR(thr, DUK_ERR_REFERENCE_ERROR, "identifier not defined");
  63102. }
  63103. DUK_DDD(DUK_DDDPRINT("identifier binding not found, not strict => set to global"));
  63104. DUK_TVAL_SET_OBJECT(&tv_tmp_obj, thr->builtins[DUK_BIDX_GLOBAL]);
  63105. DUK_TVAL_SET_STRING(&tv_tmp_key, name);
  63106. (void) duk_hobject_putprop(thr, &tv_tmp_obj, &tv_tmp_key, val, 0); /* 0 = no throw */
  63107. /* NB: 'val' may be invalidated here because put_value may realloc valstack,
  63108. * caller beware.
  63109. */
  63110. }
  63111. DUK_INTERNAL
  63112. void duk_js_putvar_envrec(duk_hthread *thr,
  63113. duk_hobject *env,
  63114. duk_hstring *name,
  63115. duk_tval *val,
  63116. duk_bool_t strict) {
  63117. duk__putvar_helper(thr, env, NULL, name, val, strict);
  63118. }
  63119. DUK_INTERNAL
  63120. void duk_js_putvar_activation(duk_hthread *thr,
  63121. duk_activation *act,
  63122. duk_hstring *name,
  63123. duk_tval *val,
  63124. duk_bool_t strict) {
  63125. DUK_ASSERT(act != NULL);
  63126. duk__putvar_helper(thr, act->lex_env, act, name, val, strict);
  63127. }
  63128. /*
  63129. * DELVAR
  63130. *
  63131. * See E5 Sections:
  63132. * 11.4.1 The delete operator
  63133. * 10.2.1.1.5 DeleteBinding (N) [declarative environment record]
  63134. * 10.2.1.2.5 DeleteBinding (N) [object environment record]
  63135. *
  63136. * Variable bindings established inside eval() are deletable (configurable),
  63137. * other bindings are not, including variables declared in global level.
  63138. * Registers are always non-deletable, and the deletion of other bindings
  63139. * is controlled by the configurable flag.
  63140. *
  63141. * For strict mode code, the 'delete' operator should fail with a compile
  63142. * time SyntaxError if applied to identifiers. Hence, no strict mode
  63143. * run-time deletion of identifiers should ever happen. This function
  63144. * should never be called from strict mode code!
  63145. */
  63146. DUK_LOCAL
  63147. duk_bool_t duk__delvar_helper(duk_hthread *thr,
  63148. duk_hobject *env,
  63149. duk_activation *act,
  63150. duk_hstring *name) {
  63151. duk__id_lookup_result ref;
  63152. duk_bool_t parents;
  63153. DUK_DDD(DUK_DDDPRINT("delvar: thr=%p, env=%p, act=%p, name=%!O "
  63154. "(env -> %!dO)",
  63155. (void *) thr, (void *) env, (void *) act,
  63156. (duk_heaphdr *) name, (duk_heaphdr *) env));
  63157. DUK_ASSERT(thr != NULL);
  63158. DUK_ASSERT(name != NULL);
  63159. /* env and act may be NULL */
  63160. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(name);
  63161. parents = 1; /* follow parent chain */
  63162. if (duk__get_identifier_reference(thr, env, name, act, parents, &ref)) {
  63163. if (ref.value && !(ref.attrs & DUK_PROPDESC_FLAG_CONFIGURABLE)) {
  63164. /* Identifier found in registers (always non-deletable)
  63165. * or declarative environment record and non-configurable.
  63166. */
  63167. return 0;
  63168. }
  63169. DUK_ASSERT(ref.holder != NULL);
  63170. return duk_hobject_delprop_raw(thr, ref.holder, name, 0);
  63171. }
  63172. /*
  63173. * Not found (even in global object).
  63174. *
  63175. * In non-strict mode this is a silent SUCCESS (!), see E5 Section 11.4.1,
  63176. * step 3.b. In strict mode this case is a compile time SyntaxError so
  63177. * we should not come here.
  63178. */
  63179. DUK_DDD(DUK_DDDPRINT("identifier to be deleted not found: name=%!O "
  63180. "(treated as silent success)",
  63181. (duk_heaphdr *) name));
  63182. return 1;
  63183. }
  63184. #if 0 /*unused*/
  63185. DUK_INTERNAL
  63186. duk_bool_t duk_js_delvar_envrec(duk_hthread *thr,
  63187. duk_hobject *env,
  63188. duk_hstring *name) {
  63189. return duk__delvar_helper(thr, env, NULL, name);
  63190. }
  63191. #endif
  63192. DUK_INTERNAL
  63193. duk_bool_t duk_js_delvar_activation(duk_hthread *thr,
  63194. duk_activation *act,
  63195. duk_hstring *name) {
  63196. DUK_ASSERT(act != NULL);
  63197. return duk__delvar_helper(thr, act->lex_env, act, name);
  63198. }
  63199. /*
  63200. * DECLVAR
  63201. *
  63202. * See E5 Sections:
  63203. * 10.4.3 Entering Function Code
  63204. * 10.5 Declaration Binding Instantion
  63205. * 12.2 Variable Statement
  63206. * 11.1.2 Identifier Reference
  63207. * 10.3.1 Identifier Resolution
  63208. *
  63209. * Variable declaration behavior is mainly discussed in Section 10.5,
  63210. * and is not discussed in the execution semantics (Sections 11-13).
  63211. *
  63212. * Conceptually declarations happen when code (global, eval, function)
  63213. * is entered, before any user code is executed. In practice, register-
  63214. * bound identifiers are 'declared' automatically (by virtue of being
  63215. * allocated to registers with the initial value 'undefined'). Other
  63216. * identifiers are declared in the function prologue with this primitive.
  63217. *
  63218. * Since non-register bindings eventually back to an internal object's
  63219. * properties, the 'prop_flags' argument is used to specify binding
  63220. * type:
  63221. *
  63222. * - Immutable binding: set DUK_PROPDESC_FLAG_WRITABLE to false
  63223. * - Non-deletable binding: set DUK_PROPDESC_FLAG_CONFIGURABLE to false
  63224. * - The flag DUK_PROPDESC_FLAG_ENUMERABLE should be set, although it
  63225. * doesn't really matter for internal objects
  63226. *
  63227. * All bindings are non-deletable mutable bindings except:
  63228. *
  63229. * - Declarations in eval code (mutable, deletable)
  63230. * - 'arguments' binding in strict function code (immutable)
  63231. * - Function name binding of a function expression (immutable)
  63232. *
  63233. * Declarations may go to declarative environment records (always
  63234. * so for functions), but may also go to object environment records
  63235. * (e.g. global code). The global object environment has special
  63236. * behavior when re-declaring a function (but not a variable); see
  63237. * E5.1 specification, Section 10.5, step 5.e.
  63238. *
  63239. * Declarations always go to the 'top-most' environment record, i.e.
  63240. * we never check the record chain. It's not an error even if a
  63241. * property (even an immutable or non-deletable one) of the same name
  63242. * already exists.
  63243. *
  63244. * If a declared variable already exists, its value needs to be updated
  63245. * (if possible). Returns 1 if a PUTVAR needs to be done by the caller;
  63246. * otherwise returns 0.
  63247. */
  63248. DUK_LOCAL
  63249. duk_bool_t duk__declvar_helper(duk_hthread *thr,
  63250. duk_hobject *env,
  63251. duk_hstring *name,
  63252. duk_tval *val,
  63253. duk_small_int_t prop_flags,
  63254. duk_bool_t is_func_decl) {
  63255. duk_context *ctx = (duk_context *) thr;
  63256. duk_hobject *holder;
  63257. duk_bool_t parents;
  63258. duk__id_lookup_result ref;
  63259. duk_tval *tv;
  63260. DUK_DDD(DUK_DDDPRINT("declvar: thr=%p, env=%p, name=%!O, val=%!T, prop_flags=0x%08lx, is_func_decl=%ld "
  63261. "(env -> %!iO)",
  63262. (void *) thr, (void *) env, (duk_heaphdr *) name,
  63263. (duk_tval *) val, (unsigned long) prop_flags,
  63264. (unsigned int) is_func_decl, (duk_heaphdr *) env));
  63265. DUK_ASSERT(thr != NULL);
  63266. DUK_ASSERT(env != NULL);
  63267. DUK_ASSERT(name != NULL);
  63268. DUK_ASSERT(val != NULL);
  63269. /* Note: in strict mode the compiler should reject explicit
  63270. * declaration of 'eval' or 'arguments'. However, internal
  63271. * bytecode may declare 'arguments' in the function prologue.
  63272. * We don't bother checking (or asserting) for these now.
  63273. */
  63274. /* Note: val is a stable duk_tval pointer. The caller makes
  63275. * a value copy into its stack frame, so 'tv_val' is not subject
  63276. * to side effects here.
  63277. */
  63278. /*
  63279. * Check whether already declared.
  63280. *
  63281. * We need to check whether the binding exists in the environment
  63282. * without walking its parents. However, we still need to check
  63283. * register-bound identifiers and the prototype chain of an object
  63284. * environment target object.
  63285. */
  63286. parents = 0; /* just check 'env' */
  63287. if (duk__get_identifier_reference(thr, env, name, NULL, parents, &ref)) {
  63288. duk_int_t e_idx;
  63289. duk_int_t h_idx;
  63290. duk_small_int_t flags;
  63291. /*
  63292. * Variable already declared, ignore re-declaration.
  63293. * The only exception is the updated behavior of E5.1 for
  63294. * global function declarations, E5.1 Section 10.5, step 5.e.
  63295. * This behavior does not apply to global variable declarations.
  63296. */
  63297. if (!(is_func_decl && env == thr->builtins[DUK_BIDX_GLOBAL_ENV])) {
  63298. DUK_DDD(DUK_DDDPRINT("re-declare a binding, ignoring"));
  63299. return 1; /* 1 -> needs a PUTVAR */
  63300. }
  63301. /*
  63302. * Special behavior in E5.1.
  63303. *
  63304. * Note that even though parents == 0, the conflicting property
  63305. * may be an inherited property (currently our global object's
  63306. * prototype is Object.prototype). Step 5.e first operates on
  63307. * the existing property (which is potentially in an ancestor)
  63308. * and then defines a new property in the global object (and
  63309. * never modifies the ancestor).
  63310. *
  63311. * Also note that this logic would become even more complicated
  63312. * if the conflicting property might be a virtual one. Object
  63313. * prototype has no virtual properties, though.
  63314. *
  63315. * XXX: this is now very awkward, rework.
  63316. */
  63317. DUK_DDD(DUK_DDDPRINT("re-declare a function binding in global object, "
  63318. "updated E5.1 processing"));
  63319. DUK_ASSERT(ref.holder != NULL);
  63320. holder = ref.holder;
  63321. /* holder will be set to the target object, not the actual object
  63322. * where the property was found (see duk__get_identifier_reference()).
  63323. */
  63324. DUK_ASSERT(DUK_HOBJECT_GET_CLASS_NUMBER(holder) == DUK_HOBJECT_CLASS_GLOBAL);
  63325. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARRAY(holder)); /* global object doesn't have array part */
  63326. /* XXX: use a helper for prototype traversal; no loop check here */
  63327. /* must be found: was found earlier, and cannot be inherited */
  63328. for (;;) {
  63329. DUK_ASSERT(holder != NULL);
  63330. duk_hobject_find_existing_entry(thr->heap, holder, name, &e_idx, &h_idx);
  63331. if (e_idx >= 0) {
  63332. break;
  63333. }
  63334. /* SCANBUILD: NULL pointer dereference, doesn't actually trigger,
  63335. * asserted above.
  63336. */
  63337. holder = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, holder);
  63338. }
  63339. DUK_ASSERT(holder != NULL);
  63340. DUK_ASSERT(e_idx >= 0);
  63341. /* SCANBUILD: scan-build produces a NULL pointer dereference warning
  63342. * below; it never actually triggers because holder is actually never
  63343. * NULL.
  63344. */
  63345. /* ref.holder is global object, holder is the object with the
  63346. * conflicting property.
  63347. */
  63348. flags = DUK_HOBJECT_E_GET_FLAGS(thr->heap, holder, e_idx);
  63349. if (!(flags & DUK_PROPDESC_FLAG_CONFIGURABLE)) {
  63350. if (flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  63351. DUK_DDD(DUK_DDDPRINT("existing property is a non-configurable "
  63352. "accessor -> reject"));
  63353. goto fail_existing_attributes;
  63354. }
  63355. if (!((flags & DUK_PROPDESC_FLAG_WRITABLE) &&
  63356. (flags & DUK_PROPDESC_FLAG_ENUMERABLE))) {
  63357. DUK_DDD(DUK_DDDPRINT("existing property is a non-configurable "
  63358. "plain property which is not writable and "
  63359. "enumerable -> reject"));
  63360. goto fail_existing_attributes;
  63361. }
  63362. DUK_DDD(DUK_DDDPRINT("existing property is not configurable but "
  63363. "is plain, enumerable, and writable -> "
  63364. "allow redeclaration"));
  63365. }
  63366. if (holder == ref.holder) {
  63367. /* XXX: if duk_hobject_define_property_internal() was updated
  63368. * to handle a pre-existing accessor property, this would be
  63369. * a simple call (like for the ancestor case).
  63370. */
  63371. DUK_DDD(DUK_DDDPRINT("redefine, offending property in global object itself"));
  63372. if (flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  63373. duk_hobject *tmp;
  63374. tmp = DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, holder, e_idx);
  63375. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, holder, e_idx, NULL);
  63376. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  63377. DUK_UNREF(tmp);
  63378. tmp = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, holder, e_idx);
  63379. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, holder, e_idx, NULL);
  63380. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  63381. DUK_UNREF(tmp);
  63382. } else {
  63383. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, holder, e_idx);
  63384. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv);
  63385. }
  63386. /* Here val would be potentially invalid if we didn't make
  63387. * a value copy at the caller.
  63388. */
  63389. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, holder, e_idx);
  63390. DUK_TVAL_SET_TVAL(tv, val);
  63391. DUK_TVAL_INCREF(thr, tv);
  63392. DUK_HOBJECT_E_SET_FLAGS(thr->heap, holder, e_idx, prop_flags);
  63393. DUK_DDD(DUK_DDDPRINT("updated global binding, final result: "
  63394. "value -> %!T, prop_flags=0x%08lx",
  63395. (duk_tval *) DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, holder, e_idx),
  63396. (unsigned long) prop_flags));
  63397. } else {
  63398. DUK_DDD(DUK_DDDPRINT("redefine, offending property in ancestor"));
  63399. DUK_ASSERT(ref.holder == thr->builtins[DUK_BIDX_GLOBAL]);
  63400. duk_push_tval(ctx, val);
  63401. duk_hobject_define_property_internal(thr, ref.holder, name, prop_flags);
  63402. }
  63403. return 0;
  63404. }
  63405. /*
  63406. * Not found (in registers or record objects). Declare
  63407. * to current variable environment.
  63408. */
  63409. /*
  63410. * Get holder object
  63411. */
  63412. if (DUK_HOBJECT_IS_DECENV(env)) {
  63413. holder = env;
  63414. } else {
  63415. DUK_ASSERT(DUK_HOBJECT_IS_OBJENV(env));
  63416. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_TARGET(thr));
  63417. DUK_ASSERT(tv != NULL);
  63418. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  63419. holder = DUK_TVAL_GET_OBJECT(tv);
  63420. DUK_ASSERT(holder != NULL);
  63421. }
  63422. /*
  63423. * Define new property
  63424. *
  63425. * Note: this may fail if the holder is not extensible.
  63426. */
  63427. /* XXX: this is awkward as we use an internal method which doesn't handle
  63428. * extensibility etc correctly. Basically we'd want to do a [[DefineOwnProperty]]
  63429. * or Object.defineProperty() here.
  63430. */
  63431. if (!DUK_HOBJECT_HAS_EXTENSIBLE(holder)) {
  63432. goto fail_not_extensible;
  63433. }
  63434. duk_push_hobject(ctx, holder);
  63435. duk_push_hstring(ctx, name);
  63436. duk_push_tval(ctx, val);
  63437. duk_xdef_prop(ctx, -3, prop_flags); /* [holder name val] -> [holder] */
  63438. duk_pop(ctx);
  63439. return 0;
  63440. fail_existing_attributes:
  63441. fail_not_extensible:
  63442. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "declaration failed");
  63443. return 0;
  63444. }
  63445. DUK_INTERNAL
  63446. duk_bool_t duk_js_declvar_activation(duk_hthread *thr,
  63447. duk_activation *act,
  63448. duk_hstring *name,
  63449. duk_tval *val,
  63450. duk_small_int_t prop_flags,
  63451. duk_bool_t is_func_decl) {
  63452. duk_hobject *env;
  63453. duk_tval tv_val_copy;
  63454. /*
  63455. * Make a value copy of the input val. This ensures that
  63456. * side effects cannot invalidate the pointer.
  63457. */
  63458. DUK_TVAL_SET_TVAL(&tv_val_copy, val);
  63459. val = &tv_val_copy;
  63460. /*
  63461. * Delayed env creation check
  63462. */
  63463. if (!act->var_env) {
  63464. DUK_ASSERT(act->lex_env == NULL);
  63465. duk_js_init_activation_environment_records_delayed(thr, act);
  63466. }
  63467. DUK_ASSERT(act->lex_env != NULL);
  63468. DUK_ASSERT(act->var_env != NULL);
  63469. env = act->var_env;
  63470. DUK_ASSERT(env != NULL);
  63471. DUK_ASSERT(DUK_HOBJECT_IS_ENV(env));
  63472. return duk__declvar_helper(thr, env, name, val, prop_flags, is_func_decl);
  63473. }
  63474. #line 1 "duk_lexer.c"
  63475. /*
  63476. * Lexer for source files, ToNumber() string conversions, RegExp expressions,
  63477. * and JSON.
  63478. *
  63479. * Provides a stream of Ecmascript tokens from an UTF-8/CESU-8 buffer. The
  63480. * caller can also rewind the token stream into a certain position which is
  63481. * needed by the compiler part for multi-pass scanning. Tokens are
  63482. * represented as duk_token structures, and contain line number information.
  63483. * Token types are identified with DUK_TOK_* defines.
  63484. *
  63485. * Characters are decoded into a fixed size lookup window consisting of
  63486. * decoded Unicode code points, with window positions past the end of the
  63487. * input filled with an invalid codepoint (-1). The tokenizer can thus
  63488. * perform multiple character lookups efficiently and with few sanity
  63489. * checks (such as access outside the end of the input), which keeps the
  63490. * tokenization code small at the cost of performance.
  63491. *
  63492. * Character data in tokens, such as identifier names and string literals,
  63493. * is encoded into CESU-8 format on-the-fly while parsing the token in
  63494. * question. The string data is made reachable to garbage collection by
  63495. * placing the token-related values in value stack entries allocated for
  63496. * this purpose by the caller. The characters exist in Unicode code point
  63497. * form only in the fixed size lookup window, which keeps character data
  63498. * expansion (of especially ASCII data) low.
  63499. *
  63500. * Token parsing supports the full range of Unicode characters as described
  63501. * in the E5 specification. Parsing has been optimized for ASCII characters
  63502. * because ordinary Ecmascript code consists almost entirely of ASCII
  63503. * characters. Matching of complex Unicode codepoint sets (such as in the
  63504. * IdentifierStart and IdentifierPart productions) is optimized for size,
  63505. * and is done using a linear scan of a bit-packed list of ranges. This is
  63506. * very slow, but should never be entered unless the source code actually
  63507. * contains Unicode characters.
  63508. *
  63509. * Ecmascript tokenization is partially context sensitive. First,
  63510. * additional future reserved words are recognized in strict mode (see E5
  63511. * Section 7.6.1.2). Second, a forward slash character ('/') can be
  63512. * recognized either as starting a RegExp literal or as a division operator,
  63513. * depending on context. The caller must provide necessary context flags
  63514. * when requesting a new token.
  63515. *
  63516. * Future work:
  63517. *
  63518. * * Make line number tracking optional, as it consumes space.
  63519. *
  63520. * * Add a feature flag for disabling UTF-8 decoding of input, as most
  63521. * source code is ASCII. Because of Unicode escapes written in ASCII,
  63522. * this does not allow Unicode support to be removed from e.g.
  63523. * duk_unicode_is_identifier_start() nor does it allow removal of CESU-8
  63524. * encoding of e.g. string literals.
  63525. *
  63526. * * Add a feature flag for disabling Unicode compliance of e.g. identifier
  63527. * names. This allows for a build more than a kilobyte smaller, because
  63528. * Unicode ranges needed by duk_unicode_is_identifier_start() and
  63529. * duk_unicode_is_identifier_part() can be dropped. String literals
  63530. * should still be allowed to contain escaped Unicode, so this still does
  63531. * not allow removal of CESU-8 encoding of e.g. string literals.
  63532. *
  63533. * * Character lookup tables for codepoints above BMP could be stripped.
  63534. *
  63535. * * Strictly speaking, E5 specification requires that source code consists
  63536. * of 16-bit code units, and if not, must be conceptually converted to
  63537. * that format first. The current lexer processes Unicode code points
  63538. * and allows characters outside the BMP. These should be converted to
  63539. * surrogate pairs while reading the source characters into the window,
  63540. * not after tokens have been formed (as is done now). However, the fix
  63541. * is not trivial because two characters are decoded from one codepoint.
  63542. *
  63543. * * Optimize for speed as well as size. Large if-else ladders are (at
  63544. * least potentially) slow.
  63545. */
  63546. /* include removed: duk_internal.h */
  63547. /*
  63548. * Various defines and file specific helper macros
  63549. */
  63550. #define DUK__MAX_RE_DECESC_DIGITS 9
  63551. #define DUK__MAX_RE_QUANT_DIGITS 9 /* Does not allow e.g. 2**31-1, but one more would allow overflows of u32. */
  63552. /* whether to use macros or helper function depends on call count */
  63553. #define DUK__ISDIGIT(x) ((x) >= DUK_ASC_0 && (x) <= DUK_ASC_9)
  63554. #define DUK__ISHEXDIGIT(x) duk__is_hex_digit((x))
  63555. #define DUK__ISOCTDIGIT(x) ((x) >= DUK_ASC_0 && (x) <= DUK_ASC_7)
  63556. #define DUK__ISDIGIT03(x) ((x) >= DUK_ASC_0 && (x) <= DUK_ASC_3)
  63557. #define DUK__ISDIGIT47(x) ((x) >= DUK_ASC_4 && (x) <= DUK_ASC_7)
  63558. /* lexer character window helpers */
  63559. #define DUK__LOOKUP(lex_ctx,index) ((lex_ctx)->window[(index)].codepoint)
  63560. #define DUK__ADVANCECHARS(lex_ctx,count) duk__advance_bytes((lex_ctx), (count) * sizeof(duk_lexer_codepoint))
  63561. #define DUK__ADVANCEBYTES(lex_ctx,count) duk__advance_bytes((lex_ctx), (count))
  63562. #define DUK__INITBUFFER(lex_ctx) duk__initbuffer((lex_ctx))
  63563. #define DUK__APPENDBUFFER(lex_ctx,x) duk__appendbuffer((lex_ctx), (duk_codepoint_t) (x))
  63564. /* lookup shorthands (note: assume context variable is named 'lex_ctx') */
  63565. #define DUK__L0() DUK__LOOKUP(lex_ctx, 0)
  63566. #define DUK__L1() DUK__LOOKUP(lex_ctx, 1)
  63567. #define DUK__L2() DUK__LOOKUP(lex_ctx, 2)
  63568. #define DUK__L3() DUK__LOOKUP(lex_ctx, 3)
  63569. #define DUK__L4() DUK__LOOKUP(lex_ctx, 4)
  63570. #define DUK__L5() DUK__LOOKUP(lex_ctx, 5)
  63571. /* packed advance/token number macro used by multiple functions */
  63572. #define DUK__ADVTOK(advbytes,tok) ((((advbytes) * sizeof(duk_lexer_codepoint)) << 8) + (tok))
  63573. /*
  63574. * Advance lookup window by N characters, filling in new characters as
  63575. * necessary. After returning caller is guaranteed a character window of
  63576. * at least DUK_LEXER_WINDOW_SIZE characters.
  63577. *
  63578. * The main function duk__advance_bytes() is called at least once per every
  63579. * token so it has a major lexer/compiler performance impact. There are two
  63580. * variants for the main duk__advance_bytes() algorithm: a sliding window
  63581. * approach which is slightly faster at the cost of larger code footprint,
  63582. * and a simple copying one.
  63583. *
  63584. * Decoding directly from the source string would be another lexing option.
  63585. * But the lookup window based approach has the advantage of hiding the
  63586. * source string and its encoding effectively which gives more flexibility
  63587. * going forward to e.g. support chunked streaming of source from flash.
  63588. *
  63589. * Decodes UTF-8/CESU-8 leniently with support for code points from U+0000 to
  63590. * U+10FFFF, causing an error if the input is unparseable. Leniency means:
  63591. *
  63592. * * Unicode code point validation is intentionally not performed,
  63593. * except to check that the codepoint does not exceed 0x10ffff.
  63594. *
  63595. * * In particular, surrogate pairs are allowed and not combined, which
  63596. * allows source files to represent all SourceCharacters with CESU-8.
  63597. * Broken surrogate pairs are allowed, as Ecmascript does not mandate
  63598. * their validation.
  63599. *
  63600. * * Allow non-shortest UTF-8 encodings.
  63601. *
  63602. * Leniency here causes few security concerns because all character data is
  63603. * decoded into Unicode codepoints before lexer processing, and is then
  63604. * re-encoded into CESU-8. The source can be parsed as strict UTF-8 with
  63605. * a compiler option. However, Ecmascript source characters include -all-
  63606. * 16-bit unsigned integer codepoints, so leniency seems to be appropriate.
  63607. *
  63608. * Note that codepoints above the BMP are not strictly SourceCharacters,
  63609. * but the lexer still accepts them as such. Before ending up in a string
  63610. * or an identifier name, codepoints above BMP are converted into surrogate
  63611. * pairs and then CESU-8 encoded, resulting in 16-bit Unicode data as
  63612. * expected by Ecmascript.
  63613. *
  63614. * An alternative approach to dealing with invalid or partial sequences
  63615. * would be to skip them and replace them with e.g. the Unicode replacement
  63616. * character U+FFFD. This has limited utility because a replacement character
  63617. * will most likely cause a parse error, unless it occurs inside a string.
  63618. * Further, Ecmascript source is typically pure ASCII.
  63619. *
  63620. * See:
  63621. *
  63622. * http://en.wikipedia.org/wiki/UTF-8
  63623. * http://en.wikipedia.org/wiki/CESU-8
  63624. * http://tools.ietf.org/html/rfc3629
  63625. * http://en.wikipedia.org/wiki/UTF-8#Invalid_byte_sequences
  63626. *
  63627. * Future work:
  63628. *
  63629. * * Reject other invalid Unicode sequences (see Wikipedia entry for examples)
  63630. * in strict UTF-8 mode.
  63631. *
  63632. * * Size optimize. An attempt to use a 16-byte lookup table for the first
  63633. * byte resulted in a code increase though.
  63634. *
  63635. * * Is checking against maximum 0x10ffff really useful? 4-byte encoding
  63636. * imposes a certain limit anyway.
  63637. *
  63638. * * Support chunked streaming of source code. Can be implemented either
  63639. * by streaming chunks of bytes or chunks of codepoints.
  63640. */
  63641. #if defined(DUK_USE_LEXER_SLIDING_WINDOW)
  63642. DUK_LOCAL void duk__fill_lexer_buffer(duk_lexer_ctx *lex_ctx, duk_small_uint_t start_offset_bytes) {
  63643. duk_lexer_codepoint *cp, *cp_end;
  63644. duk_ucodepoint_t x;
  63645. duk_small_uint_t contlen;
  63646. const duk_uint8_t *p, *p_end;
  63647. #ifdef DUK_USE_STRICT_UTF8_SOURCE
  63648. duk_ucodepoint_t mincp;
  63649. #endif
  63650. duk_int_t input_line;
  63651. /* Use temporaries and update lex_ctx only when finished. */
  63652. input_line = lex_ctx->input_line;
  63653. p = lex_ctx->input + lex_ctx->input_offset;
  63654. p_end = lex_ctx->input + lex_ctx->input_length;
  63655. cp = (duk_lexer_codepoint *) (void *) ((duk_uint8_t *) lex_ctx->buffer + start_offset_bytes);
  63656. cp_end = lex_ctx->buffer + DUK_LEXER_BUFFER_SIZE;
  63657. for (; cp != cp_end; cp++) {
  63658. cp->offset = (duk_size_t) (p - lex_ctx->input);
  63659. cp->line = input_line;
  63660. /* XXX: potential issue with signed pointers, p_end < p. */
  63661. if (DUK_UNLIKELY(p >= p_end)) {
  63662. /* If input_offset were assigned a negative value, it would
  63663. * result in a large positive value. Most likely it would be
  63664. * larger than input_length and be caught here. In any case
  63665. * no memory unsafe behavior would happen.
  63666. */
  63667. cp->codepoint = -1;
  63668. continue;
  63669. }
  63670. x = (duk_ucodepoint_t) (*p++);
  63671. /* Fast path. */
  63672. if (DUK_LIKELY(x < 0x80UL)) {
  63673. DUK_ASSERT(x != 0x2028UL && x != 0x2029UL); /* not LS/PS */
  63674. if (DUK_UNLIKELY(x <= 0x000dUL)) {
  63675. if ((x == 0x000aUL) ||
  63676. ((x == 0x000dUL) && (p >= p_end || *p != 0x000aUL))) {
  63677. /* lookup for 0x000a above assumes shortest encoding now */
  63678. /* E5 Section 7.3, treat the following as newlines:
  63679. * LF
  63680. * CR [not followed by LF]
  63681. * LS
  63682. * PS
  63683. *
  63684. * For CR LF, CR is ignored if it is followed by LF, and the LF will bump
  63685. * the line number.
  63686. */
  63687. input_line++;
  63688. }
  63689. }
  63690. cp->codepoint = (duk_codepoint_t) x;
  63691. continue;
  63692. }
  63693. /* Slow path. */
  63694. if (x < 0xc0UL) {
  63695. /* 10xx xxxx -> invalid */
  63696. goto error_encoding;
  63697. } else if (x < 0xe0UL) {
  63698. /* 110x xxxx 10xx xxxx */
  63699. contlen = 1;
  63700. #ifdef DUK_USE_STRICT_UTF8_SOURCE
  63701. mincp = 0x80UL;
  63702. #endif
  63703. x = x & 0x1fUL;
  63704. } else if (x < 0xf0UL) {
  63705. /* 1110 xxxx 10xx xxxx 10xx xxxx */
  63706. contlen = 2;
  63707. #ifdef DUK_USE_STRICT_UTF8_SOURCE
  63708. mincp = 0x800UL;
  63709. #endif
  63710. x = x & 0x0fUL;
  63711. } else if (x < 0xf8UL) {
  63712. /* 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx */
  63713. contlen = 3;
  63714. #ifdef DUK_USE_STRICT_UTF8_SOURCE
  63715. mincp = 0x10000UL;
  63716. #endif
  63717. x = x & 0x07UL;
  63718. } else {
  63719. /* no point in supporting encodings of 5 or more bytes */
  63720. goto error_encoding;
  63721. }
  63722. DUK_ASSERT(p_end >= p);
  63723. if ((duk_size_t) contlen > (duk_size_t) (p_end - p)) {
  63724. goto error_clipped;
  63725. }
  63726. while (contlen > 0) {
  63727. duk_small_uint_t y;
  63728. y = *p++;
  63729. if ((y & 0xc0U) != 0x80U) {
  63730. /* check that byte has the form 10xx xxxx */
  63731. goto error_encoding;
  63732. }
  63733. x = x << 6;
  63734. x += y & 0x3fUL;
  63735. contlen--;
  63736. }
  63737. /* check final character validity */
  63738. if (x > 0x10ffffUL) {
  63739. goto error_encoding;
  63740. }
  63741. #ifdef DUK_USE_STRICT_UTF8_SOURCE
  63742. if (x < mincp || (x >= 0xd800UL && x <= 0xdfffUL) || x == 0xfffeUL) {
  63743. goto error_encoding;
  63744. }
  63745. #endif
  63746. DUK_ASSERT(x != 0x000aUL && x != 0x000dUL);
  63747. if ((x == 0x2028UL) || (x == 0x2029UL)) {
  63748. input_line++;
  63749. }
  63750. cp->codepoint = (duk_codepoint_t) x;
  63751. }
  63752. lex_ctx->input_offset = (duk_size_t) (p - lex_ctx->input);
  63753. lex_ctx->input_line = input_line;
  63754. return;
  63755. error_clipped: /* clipped codepoint */
  63756. error_encoding: /* invalid codepoint encoding or codepoint */
  63757. lex_ctx->input_offset = (duk_size_t) (p - lex_ctx->input);
  63758. lex_ctx->input_line = input_line;
  63759. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR, "char decode failed");
  63760. }
  63761. DUK_LOCAL void duk__advance_bytes(duk_lexer_ctx *lex_ctx, duk_small_uint_t count_bytes) {
  63762. duk_small_uint_t used_bytes, avail_bytes;
  63763. DUK_ASSERT_DISABLE(count_bytes >= 0); /* unsigned */
  63764. DUK_ASSERT(count_bytes <= (duk_small_uint_t) (DUK_LEXER_WINDOW_SIZE * sizeof(duk_lexer_codepoint)));
  63765. DUK_ASSERT(lex_ctx->window >= lex_ctx->buffer);
  63766. DUK_ASSERT(lex_ctx->window < lex_ctx->buffer + DUK_LEXER_BUFFER_SIZE);
  63767. DUK_ASSERT((duk_uint8_t *) lex_ctx->window + count_bytes <= (duk_uint8_t *) lex_ctx->buffer + DUK_LEXER_BUFFER_SIZE * sizeof(duk_lexer_codepoint));
  63768. /* Zero 'count' is also allowed to make call sites easier.
  63769. * Arithmetic in bytes generates better code in GCC.
  63770. */
  63771. lex_ctx->window = (duk_lexer_codepoint *) (void *) ((duk_uint8_t *) lex_ctx->window + count_bytes); /* avoid multiply */
  63772. used_bytes = (duk_small_uint_t) ((duk_uint8_t *) lex_ctx->window - (duk_uint8_t *) lex_ctx->buffer);
  63773. avail_bytes = DUK_LEXER_BUFFER_SIZE * sizeof(duk_lexer_codepoint) - used_bytes;
  63774. if (avail_bytes < (duk_small_uint_t) (DUK_LEXER_WINDOW_SIZE * sizeof(duk_lexer_codepoint))) {
  63775. /* Not enough data to provide a full window, so "scroll" window to
  63776. * start of buffer and fill up the rest.
  63777. */
  63778. DUK_MEMMOVE((void *) lex_ctx->buffer,
  63779. (const void *) lex_ctx->window,
  63780. (size_t) avail_bytes);
  63781. lex_ctx->window = lex_ctx->buffer;
  63782. duk__fill_lexer_buffer(lex_ctx, avail_bytes);
  63783. }
  63784. }
  63785. DUK_LOCAL void duk__init_lexer_window(duk_lexer_ctx *lex_ctx) {
  63786. lex_ctx->window = lex_ctx->buffer;
  63787. duk__fill_lexer_buffer(lex_ctx, 0);
  63788. }
  63789. #else /* DUK_USE_LEXER_SLIDING_WINDOW */
  63790. DUK_LOCAL duk_codepoint_t duk__read_char(duk_lexer_ctx *lex_ctx) {
  63791. duk_ucodepoint_t x;
  63792. duk_small_uint_t len;
  63793. duk_small_uint_t i;
  63794. const duk_uint8_t *p;
  63795. #ifdef DUK_USE_STRICT_UTF8_SOURCE
  63796. duk_ucodepoint_t mincp;
  63797. #endif
  63798. duk_size_t input_offset;
  63799. input_offset = lex_ctx->input_offset;
  63800. if (DUK_UNLIKELY(input_offset >= lex_ctx->input_length)) {
  63801. /* If input_offset were assigned a negative value, it would
  63802. * result in a large positive value. Most likely it would be
  63803. * larger than input_length and be caught here. In any case
  63804. * no memory unsafe behavior would happen.
  63805. */
  63806. return -1;
  63807. }
  63808. p = lex_ctx->input + input_offset;
  63809. x = (duk_ucodepoint_t) (*p);
  63810. if (DUK_LIKELY(x < 0x80UL)) {
  63811. /* 0xxx xxxx -> fast path */
  63812. /* input offset tracking */
  63813. lex_ctx->input_offset++;
  63814. DUK_ASSERT(x != 0x2028UL && x != 0x2029UL); /* not LS/PS */
  63815. if (DUK_UNLIKELY(x <= 0x000dUL)) {
  63816. if ((x == 0x000aUL) ||
  63817. ((x == 0x000dUL) && (lex_ctx->input_offset >= lex_ctx->input_length ||
  63818. lex_ctx->input[lex_ctx->input_offset] != 0x000aUL))) {
  63819. /* lookup for 0x000a above assumes shortest encoding now */
  63820. /* E5 Section 7.3, treat the following as newlines:
  63821. * LF
  63822. * CR [not followed by LF]
  63823. * LS
  63824. * PS
  63825. *
  63826. * For CR LF, CR is ignored if it is followed by LF, and the LF will bump
  63827. * the line number.
  63828. */
  63829. lex_ctx->input_line++;
  63830. }
  63831. }
  63832. return (duk_codepoint_t) x;
  63833. }
  63834. /* Slow path. */
  63835. if (x < 0xc0UL) {
  63836. /* 10xx xxxx -> invalid */
  63837. goto error_encoding;
  63838. } else if (x < 0xe0UL) {
  63839. /* 110x xxxx 10xx xxxx */
  63840. len = 2;
  63841. #ifdef DUK_USE_STRICT_UTF8_SOURCE
  63842. mincp = 0x80UL;
  63843. #endif
  63844. x = x & 0x1fUL;
  63845. } else if (x < 0xf0UL) {
  63846. /* 1110 xxxx 10xx xxxx 10xx xxxx */
  63847. len = 3;
  63848. #ifdef DUK_USE_STRICT_UTF8_SOURCE
  63849. mincp = 0x800UL;
  63850. #endif
  63851. x = x & 0x0fUL;
  63852. } else if (x < 0xf8UL) {
  63853. /* 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx */
  63854. len = 4;
  63855. #ifdef DUK_USE_STRICT_UTF8_SOURCE
  63856. mincp = 0x10000UL;
  63857. #endif
  63858. x = x & 0x07UL;
  63859. } else {
  63860. /* no point in supporting encodings of 5 or more bytes */
  63861. goto error_encoding;
  63862. }
  63863. DUK_ASSERT(lex_ctx->input_length >= lex_ctx->input_offset);
  63864. if ((duk_size_t) len > (duk_size_t) (lex_ctx->input_length - lex_ctx->input_offset)) {
  63865. goto error_clipped;
  63866. }
  63867. p++;
  63868. for (i = 1; i < len; i++) {
  63869. duk_small_uint_t y;
  63870. y = *p++;
  63871. if ((y & 0xc0U) != 0x80U) {
  63872. /* check that byte has the form 10xx xxxx */
  63873. goto error_encoding;
  63874. }
  63875. x = x << 6;
  63876. x += y & 0x3fUL;
  63877. }
  63878. /* check final character validity */
  63879. if (x > 0x10ffffUL) {
  63880. goto error_encoding;
  63881. }
  63882. #ifdef DUK_USE_STRICT_UTF8_SOURCE
  63883. if (x < mincp || (x >= 0xd800UL && x <= 0xdfffUL) || x == 0xfffeUL) {
  63884. goto error_encoding;
  63885. }
  63886. #endif
  63887. /* input offset tracking */
  63888. lex_ctx->input_offset += len;
  63889. /* line tracking */
  63890. DUK_ASSERT(x != 0x000aUL && x != 0x000dUL);
  63891. if ((x == 0x2028UL) || (x == 0x2029UL)) {
  63892. lex_ctx->input_line++;
  63893. }
  63894. return (duk_codepoint_t) x;
  63895. error_clipped: /* clipped codepoint */
  63896. error_encoding: /* invalid codepoint encoding or codepoint */
  63897. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR, "char decode failed");
  63898. return 0;
  63899. }
  63900. DUK_LOCAL void duk__advance_bytes(duk_lexer_ctx *lex_ctx, duk_small_uint_t count_bytes) {
  63901. duk_small_uint_t keep_bytes;
  63902. duk_lexer_codepoint *cp, *cp_end;
  63903. DUK_ASSERT_DISABLE(count_bytes >= 0); /* unsigned */
  63904. DUK_ASSERT(count_bytes <= (duk_small_uint_t) (DUK_LEXER_WINDOW_SIZE * sizeof(duk_lexer_codepoint)));
  63905. /* Zero 'count' is also allowed to make call sites easier. */
  63906. keep_bytes = DUK_LEXER_WINDOW_SIZE * sizeof(duk_lexer_codepoint) - count_bytes;
  63907. DUK_MEMMOVE((void *) lex_ctx->window,
  63908. (const void *) ((duk_uint8_t *) lex_ctx->window + count_bytes),
  63909. (size_t) keep_bytes);
  63910. cp = (duk_lexer_codepoint *) ((duk_uint8_t *) lex_ctx->window + keep_bytes);
  63911. cp_end = lex_ctx->window + DUK_LEXER_WINDOW_SIZE;
  63912. for (; cp != cp_end; cp++) {
  63913. cp->offset = lex_ctx->input_offset;
  63914. cp->line = lex_ctx->input_line;
  63915. cp->codepoint = duk__read_char(lex_ctx);
  63916. }
  63917. }
  63918. DUK_LOCAL void duk__init_lexer_window(duk_lexer_ctx *lex_ctx) {
  63919. /* Call with count == DUK_LEXER_WINDOW_SIZE to fill buffer initially. */
  63920. duk__advance_bytes(lex_ctx, DUK_LEXER_WINDOW_SIZE * sizeof(duk_lexer_codepoint)); /* fill window */
  63921. }
  63922. #endif /* DUK_USE_LEXER_SLIDING_WINDOW */
  63923. /*
  63924. * (Re)initialize the temporary byte buffer. May be called extra times
  63925. * with little impact.
  63926. */
  63927. DUK_LOCAL void duk__initbuffer(duk_lexer_ctx *lex_ctx) {
  63928. /* Reuse buffer as is unless buffer has grown large. */
  63929. if (DUK_HBUFFER_DYNAMIC_GET_SIZE(lex_ctx->buf) < DUK_LEXER_TEMP_BUF_LIMIT) {
  63930. /* Keep current size */
  63931. } else {
  63932. duk_hbuffer_resize(lex_ctx->thr, lex_ctx->buf, DUK_LEXER_TEMP_BUF_LIMIT);
  63933. }
  63934. DUK_BW_INIT_WITHBUF(lex_ctx->thr, &lex_ctx->bw, lex_ctx->buf);
  63935. }
  63936. /*
  63937. * Append a Unicode codepoint to the temporary byte buffer. Performs
  63938. * CESU-8 surrogate pair encoding for codepoints above the BMP.
  63939. * Existing surrogate pairs are allowed and also encoded into CESU-8.
  63940. */
  63941. DUK_LOCAL void duk__appendbuffer(duk_lexer_ctx *lex_ctx, duk_codepoint_t x) {
  63942. /*
  63943. * Since character data is only generated by decoding the source or by
  63944. * the compiler itself, we rely on the input codepoints being correct
  63945. * and avoid a check here.
  63946. *
  63947. * Character data can also come here through decoding of Unicode
  63948. * escapes ("\udead\ubeef") so all 16-but unsigned values can be
  63949. * present, even when the source file itself is strict UTF-8.
  63950. */
  63951. DUK_ASSERT(x >= 0 && x <= 0x10ffff);
  63952. DUK_BW_WRITE_ENSURE_CESU8(lex_ctx->thr, &lex_ctx->bw, (duk_ucodepoint_t) x);
  63953. }
  63954. /*
  63955. * Intern the temporary byte buffer into a valstack slot
  63956. * (in practice, slot1 or slot2).
  63957. */
  63958. DUK_LOCAL void duk__internbuffer(duk_lexer_ctx *lex_ctx, duk_idx_t valstack_idx) {
  63959. duk_context *ctx = (duk_context *) lex_ctx->thr;
  63960. DUK_ASSERT(valstack_idx == lex_ctx->slot1_idx || valstack_idx == lex_ctx->slot2_idx);
  63961. DUK_BW_PUSH_AS_STRING(lex_ctx->thr, &lex_ctx->bw);
  63962. duk_replace(ctx, valstack_idx);
  63963. }
  63964. /*
  63965. * Init lexer context
  63966. */
  63967. DUK_INTERNAL void duk_lexer_initctx(duk_lexer_ctx *lex_ctx) {
  63968. DUK_ASSERT(lex_ctx != NULL);
  63969. DUK_MEMZERO(lex_ctx, sizeof(*lex_ctx));
  63970. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  63971. #if defined(DUK_USE_LEXER_SLIDING_WINDOW)
  63972. lex_ctx->window = NULL;
  63973. #endif
  63974. lex_ctx->thr = NULL;
  63975. lex_ctx->input = NULL;
  63976. lex_ctx->buf = NULL;
  63977. #endif
  63978. }
  63979. /*
  63980. * Set lexer input position and reinitialize lookup window.
  63981. */
  63982. /* NB: duk_lexer_getpoint() is a macro only */
  63983. DUK_INTERNAL void duk_lexer_setpoint(duk_lexer_ctx *lex_ctx, duk_lexer_point *pt) {
  63984. DUK_ASSERT_DISABLE(pt->offset >= 0); /* unsigned */
  63985. DUK_ASSERT(pt->line >= 1);
  63986. lex_ctx->input_offset = pt->offset;
  63987. lex_ctx->input_line = pt->line;
  63988. duk__init_lexer_window(lex_ctx);
  63989. }
  63990. /*
  63991. * Lexing helpers
  63992. */
  63993. /* numeric value of a hex digit (also covers octal and decimal digits) */
  63994. DUK_LOCAL duk_codepoint_t duk__hexval(duk_lexer_ctx *lex_ctx, duk_codepoint_t x) {
  63995. duk_small_int_t t;
  63996. /* Here 'x' is a Unicode codepoint */
  63997. if (DUK_LIKELY(x >= 0 && x <= 0xff)) {
  63998. t = duk_hex_dectab[x];
  63999. if (DUK_LIKELY(t >= 0)) {
  64000. return t;
  64001. }
  64002. }
  64003. /* Throwing an error this deep makes the error rather vague, but
  64004. * saves hundreds of bytes of code.
  64005. */
  64006. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR, "decode error");
  64007. return 0;
  64008. }
  64009. /* having this as a separate function provided a size benefit */
  64010. DUK_LOCAL duk_bool_t duk__is_hex_digit(duk_codepoint_t x) {
  64011. if (DUK_LIKELY(x >= 0 && x <= 0xff)) {
  64012. return (duk_hex_dectab[x] >= 0);
  64013. }
  64014. return 0;
  64015. }
  64016. DUK_LOCAL duk_codepoint_t duk__decode_hexesc_from_window(duk_lexer_ctx *lex_ctx, duk_small_int_t lookup_offset) {
  64017. /* validation performed by duk__hexval */
  64018. return (duk__hexval(lex_ctx, lex_ctx->window[lookup_offset].codepoint) << 4) |
  64019. (duk__hexval(lex_ctx, lex_ctx->window[lookup_offset + 1].codepoint));
  64020. }
  64021. DUK_LOCAL duk_codepoint_t duk__decode_uniesc_from_window(duk_lexer_ctx *lex_ctx, duk_small_int_t lookup_offset) {
  64022. /* validation performed by duk__hexval */
  64023. return (duk__hexval(lex_ctx, lex_ctx->window[lookup_offset].codepoint) << 12) |
  64024. (duk__hexval(lex_ctx, lex_ctx->window[lookup_offset + 1].codepoint) << 8) |
  64025. (duk__hexval(lex_ctx, lex_ctx->window[lookup_offset + 2].codepoint) << 4) |
  64026. (duk__hexval(lex_ctx, lex_ctx->window[lookup_offset + 3].codepoint));
  64027. }
  64028. /*
  64029. * Parse Ecmascript source InputElementDiv or InputElementRegExp
  64030. * (E5 Section 7), skipping whitespace, comments, and line terminators.
  64031. *
  64032. * Possible results are:
  64033. * (1) a token
  64034. * (2) a line terminator (skipped)
  64035. * (3) a comment (skipped)
  64036. * (4) EOF
  64037. *
  64038. * White space is automatically skipped from the current position (but
  64039. * not after the input element). If input has already ended, returns
  64040. * DUK_TOK_EOF indefinitely. If a parse error occurs, uses an DUK_ERROR()
  64041. * macro call (and hence a longjmp through current heap longjmp context).
  64042. * Comments and line terminator tokens are automatically skipped.
  64043. *
  64044. * The input element being matched is determined by regexp_mode; if set,
  64045. * parses a InputElementRegExp, otherwise a InputElementDiv. The
  64046. * difference between these are handling of productions starting with a
  64047. * forward slash.
  64048. *
  64049. * If strict_mode is set, recognizes additional future reserved words
  64050. * specific to strict mode, and refuses to parse octal literals.
  64051. *
  64052. * The matching strategy below is to (currently) use a six character
  64053. * lookup window to quickly determine which production is the -longest-
  64054. * matching one, and then parse that. The top-level if-else clauses
  64055. * match the first character, and the code blocks for each clause
  64056. * handle -all- alternatives for that first character. Ecmascript
  64057. * specification uses the "longest match wins" semantics, so the order
  64058. * of the if-clauses matters.
  64059. *
  64060. * Misc notes:
  64061. *
  64062. * * Ecmascript numeric literals do not accept a sign character.
  64063. * Consequently e.g. "-1.0" is parsed as two tokens: a negative
  64064. * sign and a positive numeric literal. The compiler performs
  64065. * the negation during compilation, so this has no adverse impact.
  64066. *
  64067. * * There is no token for "undefined": it is just a value available
  64068. * from the global object (or simply established by doing a reference
  64069. * to an undefined value).
  64070. *
  64071. * * Some contexts want Identifier tokens, which are IdentifierNames
  64072. * excluding reserved words, while some contexts want IdentifierNames
  64073. * directly. In the latter case e.g. "while" is interpreted as an
  64074. * identifier name, not a DUK_TOK_WHILE token. The solution here is
  64075. * to provide both token types: DUK_TOK_WHILE goes to 't' while
  64076. * DUK_TOK_IDENTIFIER goes to 't_nores', and 'slot1' always contains
  64077. * the identifier / keyword name.
  64078. *
  64079. * * Directive prologue needs to identify string literals such as
  64080. * "use strict" and 'use strict', which are sensitive to line
  64081. * continuations and escape sequences. For instance, "use\u0020strict"
  64082. * is a valid directive but is distinct from "use strict". The solution
  64083. * here is to decode escapes while tokenizing, but to keep track of the
  64084. * number of escapes. Directive detection can then check that the
  64085. * number of escapes is zero.
  64086. *
  64087. * * Multi-line comments with one or more internal LineTerminator are
  64088. * treated like a line terminator to comply with automatic semicolon
  64089. * insertion.
  64090. */
  64091. DUK_INTERNAL
  64092. void duk_lexer_parse_js_input_element(duk_lexer_ctx *lex_ctx,
  64093. duk_token *out_token,
  64094. duk_bool_t strict_mode,
  64095. duk_bool_t regexp_mode) {
  64096. duk_codepoint_t x; /* temporary, must be signed and 32-bit to hold Unicode code points */
  64097. duk_small_uint_t advtok = 0; /* (advance << 8) + token_type, updated at function end,
  64098. * init is unnecessary but suppresses "may be used uninitialized" warnings.
  64099. */
  64100. duk_bool_t got_lineterm = 0; /* got lineterm preceding non-whitespace, non-lineterm token */
  64101. if (++lex_ctx->token_count >= lex_ctx->token_limit) {
  64102. DUK_ERROR(lex_ctx->thr, DUK_ERR_RANGE_ERROR, "token limit");
  64103. return; /* unreachable */
  64104. }
  64105. out_token->t = DUK_TOK_EOF;
  64106. out_token->t_nores = -1; /* marker: copy t if not changed */
  64107. #if 0 /* not necessary to init, disabled for faster parsing */
  64108. out_token->num = DUK_DOUBLE_NAN;
  64109. out_token->str1 = NULL;
  64110. out_token->str2 = NULL;
  64111. #endif
  64112. out_token->num_escapes = 0;
  64113. /* out_token->lineterm set by caller */
  64114. /* This would be nice, but parsing is faster without resetting the
  64115. * value slots. The only side effect is that references to temporary
  64116. * string values may linger until lexing is finished; they're then
  64117. * freed normally.
  64118. */
  64119. #if 0
  64120. duk_to_undefined((duk_context *) lex_ctx->thr, lex_ctx->slot1_idx);
  64121. duk_to_undefined((duk_context *) lex_ctx->thr, lex_ctx->slot2_idx);
  64122. #endif
  64123. /* 'advtok' indicates how much to advance and which token id to assign
  64124. * at the end. This shared functionality minimizes code size. All
  64125. * code paths are required to set 'advtok' to some value, so no default
  64126. * init value is used. Code paths calling DUK_ERROR() never return so
  64127. * they don't need to set advtok.
  64128. */
  64129. /*
  64130. * Matching order:
  64131. *
  64132. * Punctuator first chars, also covers comments, regexps
  64133. * LineTerminator
  64134. * Identifier or reserved word, also covers null/true/false literals
  64135. * NumericLiteral
  64136. * StringLiteral
  64137. * EOF
  64138. *
  64139. * The order does not matter as long as the longest match is
  64140. * always correctly identified. There are order dependencies
  64141. * in the clauses, so it's not trivial to convert to a switch.
  64142. */
  64143. restart_lineupdate:
  64144. out_token->start_line = lex_ctx->window[0].line;
  64145. restart:
  64146. out_token->start_offset = lex_ctx->window[0].offset;
  64147. x = DUK__L0();
  64148. switch (x) {
  64149. case DUK_ASC_SPACE:
  64150. case DUK_ASC_HT: /* fast paths for space and tab */
  64151. DUK__ADVANCECHARS(lex_ctx, 1);
  64152. goto restart;
  64153. case DUK_ASC_LF: /* LF line terminator; CR LF and Unicode lineterms are handled in slow path */
  64154. DUK__ADVANCECHARS(lex_ctx, 1);
  64155. got_lineterm = 1;
  64156. goto restart_lineupdate;
  64157. case DUK_ASC_SLASH: /* '/' */
  64158. if (DUK__L1() == '/') {
  64159. /*
  64160. * E5 Section 7.4, allow SourceCharacter (which is any 16-bit
  64161. * code point).
  64162. */
  64163. /* DUK__ADVANCECHARS(lex_ctx, 2) would be correct here, but it unnecessary */
  64164. for (;;) {
  64165. x = DUK__L0();
  64166. if (x < 0 || duk_unicode_is_line_terminator(x)) {
  64167. break;
  64168. }
  64169. DUK__ADVANCECHARS(lex_ctx, 1);
  64170. }
  64171. goto restart; /* line terminator will be handled on next round */
  64172. } else if (DUK__L1() == '*') {
  64173. /*
  64174. * E5 Section 7.4. If the multi-line comment contains a newline,
  64175. * it is treated like a single line terminator for automatic
  64176. * semicolon insertion.
  64177. */
  64178. duk_bool_t last_asterisk = 0;
  64179. DUK__ADVANCECHARS(lex_ctx, 2);
  64180. for (;;) {
  64181. x = DUK__L0();
  64182. if (x < 0) {
  64183. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64184. "eof while parsing multiline comment");
  64185. }
  64186. DUK__ADVANCECHARS(lex_ctx, 1);
  64187. if (last_asterisk && x == '/') {
  64188. break;
  64189. }
  64190. if (duk_unicode_is_line_terminator(x)) {
  64191. got_lineterm = 1;
  64192. }
  64193. last_asterisk = (x == '*');
  64194. }
  64195. goto restart_lineupdate;
  64196. } else if (regexp_mode) {
  64197. #ifdef DUK_USE_REGEXP_SUPPORT
  64198. /*
  64199. * "/" followed by something in regexp mode. See E5 Section 7.8.5.
  64200. *
  64201. * RegExp parsing is a bit complex. First, the regexp body is delimited
  64202. * by forward slashes, but the body may also contain forward slashes as
  64203. * part of an escape sequence or inside a character class (delimited by
  64204. * square brackets). A mini state machine is used to implement these.
  64205. *
  64206. * Further, an early (parse time) error must be thrown if the regexp
  64207. * would cause a run-time error when used in the expression new RegExp(...).
  64208. * Parsing here simply extracts the (candidate) regexp, and also accepts
  64209. * invalid regular expressions (which are delimited properly). The caller
  64210. * (compiler) must perform final validation and regexp compilation.
  64211. *
  64212. * RegExp first char may not be '/' (single line comment) or '*' (multi-
  64213. * line comment). These have already been checked above, so there is no
  64214. * need below for special handling of the first regexp character as in
  64215. * the E5 productions.
  64216. *
  64217. * About unicode escapes within regexp literals:
  64218. *
  64219. * E5 Section 7.8.5 grammar does NOT accept \uHHHH escapes.
  64220. * However, Section 6 states that regexps accept the escapes,
  64221. * see paragraph starting with "In string literals...".
  64222. * The regexp grammar, which sees the decoded regexp literal
  64223. * (after lexical parsing) DOES have a \uHHHH unicode escape.
  64224. * So, for instance:
  64225. *
  64226. * /\u1234/
  64227. *
  64228. * should first be parsed by the lexical grammar as:
  64229. *
  64230. * '\' 'u' RegularExpressionBackslashSequence
  64231. * '1' RegularExpressionNonTerminator
  64232. * '2' RegularExpressionNonTerminator
  64233. * '3' RegularExpressionNonTerminator
  64234. * '4' RegularExpressionNonTerminator
  64235. *
  64236. * and the escape itself is then parsed by the regexp engine.
  64237. * This is the current implementation.
  64238. *
  64239. * Minor spec inconsistency:
  64240. *
  64241. * E5 Section 7.8.5 RegularExpressionBackslashSequence is:
  64242. *
  64243. * \ RegularExpressionNonTerminator
  64244. *
  64245. * while Section A.1 RegularExpressionBackslashSequence is:
  64246. *
  64247. * \ NonTerminator
  64248. *
  64249. * The latter is not normative and a typo.
  64250. *
  64251. */
  64252. /* first, parse regexp body roughly */
  64253. duk_small_int_t state = 0; /* 0=base, 1=esc, 2=class, 3=class+esc */
  64254. DUK__INITBUFFER(lex_ctx);
  64255. for (;;) {
  64256. DUK__ADVANCECHARS(lex_ctx, 1); /* skip opening slash on first loop */
  64257. x = DUK__L0();
  64258. if (x < 0 || duk_unicode_is_line_terminator(x)) {
  64259. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64260. "eof or line terminator while parsing regexp");
  64261. }
  64262. x = DUK__L0(); /* re-read to avoid spill / fetch */
  64263. if (state == 0) {
  64264. if (x == '/') {
  64265. DUK__ADVANCECHARS(lex_ctx, 1); /* eat closing slash */
  64266. break;
  64267. } else if (x == '\\') {
  64268. state = 1;
  64269. } else if (x == '[') {
  64270. state = 2;
  64271. }
  64272. } else if (state == 1) {
  64273. state = 0;
  64274. } else if (state == 2) {
  64275. if (x == ']') {
  64276. state = 0;
  64277. } else if (x == '\\') {
  64278. state = 3;
  64279. }
  64280. } else { /* state == 3 */
  64281. state = 2;
  64282. }
  64283. DUK__APPENDBUFFER(lex_ctx, x);
  64284. }
  64285. duk__internbuffer(lex_ctx, lex_ctx->slot1_idx);
  64286. out_token->str1 = duk_get_hstring((duk_context *) lex_ctx->thr, lex_ctx->slot1_idx);
  64287. /* second, parse flags */
  64288. DUK__INITBUFFER(lex_ctx);
  64289. for (;;) {
  64290. x = DUK__L0();
  64291. if (!duk_unicode_is_identifier_part(x)) {
  64292. break;
  64293. }
  64294. x = DUK__L0(); /* re-read to avoid spill / fetch */
  64295. DUK__APPENDBUFFER(lex_ctx, x);
  64296. DUK__ADVANCECHARS(lex_ctx, 1);
  64297. }
  64298. duk__internbuffer(lex_ctx, lex_ctx->slot2_idx);
  64299. out_token->str2 = duk_get_hstring((duk_context *) lex_ctx->thr, lex_ctx->slot2_idx);
  64300. DUK__INITBUFFER(lex_ctx); /* free some memory */
  64301. /* validation of the regexp is caller's responsibility */
  64302. advtok = DUK__ADVTOK(0, DUK_TOK_REGEXP);
  64303. #else
  64304. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR, "regexp support disabled");
  64305. #endif
  64306. } else if (DUK__L1() == '=') {
  64307. /* "/=" and not in regexp mode */
  64308. advtok = DUK__ADVTOK(2, DUK_TOK_DIV_EQ);
  64309. } else {
  64310. /* "/" and not in regexp mode */
  64311. advtok = DUK__ADVTOK(1, DUK_TOK_DIV);
  64312. }
  64313. break;
  64314. case DUK_ASC_LCURLY: /* '{' */
  64315. advtok = DUK__ADVTOK(1, DUK_TOK_LCURLY);
  64316. break;
  64317. case DUK_ASC_RCURLY: /* '}' */
  64318. advtok = DUK__ADVTOK(1, DUK_TOK_RCURLY);
  64319. break;
  64320. case DUK_ASC_LPAREN: /* '(' */
  64321. advtok = DUK__ADVTOK(1, DUK_TOK_LPAREN);
  64322. break;
  64323. case DUK_ASC_RPAREN: /* ')' */
  64324. advtok = DUK__ADVTOK(1, DUK_TOK_RPAREN);
  64325. break;
  64326. case DUK_ASC_LBRACKET: /* '[' */
  64327. advtok = DUK__ADVTOK(1, DUK_TOK_LBRACKET);
  64328. break;
  64329. case DUK_ASC_RBRACKET: /* ']' */
  64330. advtok = DUK__ADVTOK(1, DUK_TOK_RBRACKET);
  64331. break;
  64332. case DUK_ASC_PERIOD: /* '.' */
  64333. if (DUK__ISDIGIT(DUK__L1())) {
  64334. /* Period followed by a digit can only start DecimalLiteral
  64335. * (handled in slow path). We could jump straight into the
  64336. * DecimalLiteral handling but should avoid goto to inside
  64337. * a block.
  64338. */
  64339. goto slow_path;
  64340. }
  64341. advtok = DUK__ADVTOK(1, DUK_TOK_PERIOD);
  64342. break;
  64343. case DUK_ASC_SEMICOLON: /* ';' */
  64344. advtok = DUK__ADVTOK(1, DUK_TOK_SEMICOLON);
  64345. break;
  64346. case DUK_ASC_COMMA: /* ',' */
  64347. advtok = DUK__ADVTOK(1, DUK_TOK_COMMA);
  64348. break;
  64349. case DUK_ASC_LANGLE: /* '<' */
  64350. if (DUK__L1() == '<' && DUK__L2() == '=') {
  64351. advtok = DUK__ADVTOK(3, DUK_TOK_ALSHIFT_EQ);
  64352. } else if (DUK__L1() == '=') {
  64353. advtok = DUK__ADVTOK(2, DUK_TOK_LE);
  64354. } else if (DUK__L1() == '<') {
  64355. advtok = DUK__ADVTOK(2, DUK_TOK_ALSHIFT);
  64356. } else {
  64357. advtok = DUK__ADVTOK(1, DUK_TOK_LT);
  64358. }
  64359. break;
  64360. case DUK_ASC_RANGLE: /* '>' */
  64361. if (DUK__L1() == '>' && DUK__L2() == '>' && DUK__L3() == '=') {
  64362. advtok = DUK__ADVTOK(4, DUK_TOK_RSHIFT_EQ);
  64363. } else if (DUK__L1() == '>' && DUK__L2() == '>') {
  64364. advtok = DUK__ADVTOK(3, DUK_TOK_RSHIFT);
  64365. } else if (DUK__L1() == '>' && DUK__L2() == '=') {
  64366. advtok = DUK__ADVTOK(3, DUK_TOK_ARSHIFT_EQ);
  64367. } else if (DUK__L1() == '=') {
  64368. advtok = DUK__ADVTOK(2, DUK_TOK_GE);
  64369. } else if (DUK__L1() == '>') {
  64370. advtok = DUK__ADVTOK(2, DUK_TOK_ARSHIFT);
  64371. } else {
  64372. advtok = DUK__ADVTOK(1, DUK_TOK_GT);
  64373. }
  64374. break;
  64375. case DUK_ASC_EQUALS: /* '=' */
  64376. if (DUK__L1() == '=' && DUK__L2() == '=') {
  64377. advtok = DUK__ADVTOK(3, DUK_TOK_SEQ);
  64378. } else if (DUK__L1() == '=') {
  64379. advtok = DUK__ADVTOK(2, DUK_TOK_EQ);
  64380. } else {
  64381. advtok = DUK__ADVTOK(1, DUK_TOK_EQUALSIGN);
  64382. }
  64383. break;
  64384. case DUK_ASC_EXCLAMATION: /* '!' */
  64385. if (DUK__L1() == '=' && DUK__L2() == '=') {
  64386. advtok = DUK__ADVTOK(3, DUK_TOK_SNEQ);
  64387. } else if (DUK__L1() == '=') {
  64388. advtok = DUK__ADVTOK(2, DUK_TOK_NEQ);
  64389. } else {
  64390. advtok = DUK__ADVTOK(1, DUK_TOK_LNOT);
  64391. }
  64392. break;
  64393. case DUK_ASC_PLUS: /* '+' */
  64394. if (DUK__L1() == '+') {
  64395. advtok = DUK__ADVTOK(2, DUK_TOK_INCREMENT);
  64396. } else if (DUK__L1() == '=') {
  64397. advtok = DUK__ADVTOK(2, DUK_TOK_ADD_EQ);
  64398. } else {
  64399. advtok = DUK__ADVTOK(1, DUK_TOK_ADD);
  64400. }
  64401. break;
  64402. case DUK_ASC_MINUS: /* '-' */
  64403. if (DUK__L1() == '-') {
  64404. advtok = DUK__ADVTOK(2, DUK_TOK_DECREMENT);
  64405. } else if (DUK__L1() == '=') {
  64406. advtok = DUK__ADVTOK(2, DUK_TOK_SUB_EQ);
  64407. } else {
  64408. advtok = DUK__ADVTOK(1, DUK_TOK_SUB);
  64409. }
  64410. break;
  64411. case DUK_ASC_STAR: /* '*' */
  64412. if (DUK__L1() == '=') {
  64413. advtok = DUK__ADVTOK(2, DUK_TOK_MUL_EQ);
  64414. } else {
  64415. advtok = DUK__ADVTOK(1, DUK_TOK_MUL);
  64416. }
  64417. break;
  64418. case DUK_ASC_PERCENT: /* '%' */
  64419. if (DUK__L1() == '=') {
  64420. advtok = DUK__ADVTOK(2, DUK_TOK_MOD_EQ);
  64421. } else {
  64422. advtok = DUK__ADVTOK(1, DUK_TOK_MOD);
  64423. }
  64424. break;
  64425. case DUK_ASC_AMP: /* '&' */
  64426. if (DUK__L1() == '&') {
  64427. advtok = DUK__ADVTOK(2, DUK_TOK_LAND);
  64428. } else if (DUK__L1() == '=') {
  64429. advtok = DUK__ADVTOK(2, DUK_TOK_BAND_EQ);
  64430. } else {
  64431. advtok = DUK__ADVTOK(1, DUK_TOK_BAND);
  64432. }
  64433. break;
  64434. case DUK_ASC_PIPE: /* '|' */
  64435. if (DUK__L1() == '|') {
  64436. advtok = DUK__ADVTOK(2, DUK_TOK_LOR);
  64437. } else if (DUK__L1() == '=') {
  64438. advtok = DUK__ADVTOK(2, DUK_TOK_BOR_EQ);
  64439. } else {
  64440. advtok = DUK__ADVTOK(1, DUK_TOK_BOR);
  64441. }
  64442. break;
  64443. case DUK_ASC_CARET: /* '^' */
  64444. if (DUK__L1() == '=') {
  64445. advtok = DUK__ADVTOK(2, DUK_TOK_BXOR_EQ);
  64446. } else {
  64447. advtok = DUK__ADVTOK(1, DUK_TOK_BXOR);
  64448. }
  64449. break;
  64450. case DUK_ASC_TILDE: /* '~' */
  64451. advtok = DUK__ADVTOK(1, DUK_TOK_BNOT);
  64452. break;
  64453. case DUK_ASC_QUESTION: /* '?' */
  64454. advtok = DUK__ADVTOK(1, DUK_TOK_QUESTION);
  64455. break;
  64456. case DUK_ASC_COLON: /* ':' */
  64457. advtok = DUK__ADVTOK(1, DUK_TOK_COLON);
  64458. break;
  64459. case DUK_ASC_DOUBLEQUOTE: /* '"' */
  64460. case DUK_ASC_SINGLEQUOTE: { /* '\'' */
  64461. duk_small_int_t quote = x; /* Note: duk_uint8_t type yields larger code */
  64462. duk_small_int_t adv;
  64463. DUK__INITBUFFER(lex_ctx);
  64464. for (;;) {
  64465. DUK__ADVANCECHARS(lex_ctx, 1); /* eat opening quote on first loop */
  64466. x = DUK__L0();
  64467. if (x < 0 || duk_unicode_is_line_terminator(x)) {
  64468. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64469. "eof or line terminator while parsing string literal");
  64470. }
  64471. if (x == quote) {
  64472. DUK__ADVANCECHARS(lex_ctx, 1); /* eat closing quote */
  64473. break;
  64474. }
  64475. if (x == '\\') {
  64476. /* DUK__L0 -> '\' char
  64477. * DUK__L1 ... DUK__L5 -> more lookup
  64478. */
  64479. x = DUK__L1();
  64480. /* How much to advance before next loop; note that next loop
  64481. * will advance by 1 anyway, so -1 from the total escape
  64482. * length (e.g. len('\uXXXX') - 1 = 6 - 1). As a default,
  64483. * 1 is good.
  64484. */
  64485. adv = 2 - 1; /* note: long live range */
  64486. if (x < 0) {
  64487. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64488. "eof while parsing string literal");
  64489. }
  64490. if (duk_unicode_is_line_terminator(x)) {
  64491. /* line continuation */
  64492. if (x == 0x000d && DUK__L2() == 0x000a) {
  64493. /* CR LF again a special case */
  64494. adv = 3 - 1;
  64495. }
  64496. } else if (x == '\'') {
  64497. DUK__APPENDBUFFER(lex_ctx, 0x0027);
  64498. } else if (x == '"') {
  64499. DUK__APPENDBUFFER(lex_ctx, 0x0022);
  64500. } else if (x == '\\') {
  64501. DUK__APPENDBUFFER(lex_ctx, 0x005c);
  64502. } else if (x == 'b') {
  64503. DUK__APPENDBUFFER(lex_ctx, 0x0008);
  64504. } else if (x == 'f') {
  64505. DUK__APPENDBUFFER(lex_ctx, 0x000c);
  64506. } else if (x == 'n') {
  64507. DUK__APPENDBUFFER(lex_ctx, 0x000a);
  64508. } else if (x == 'r') {
  64509. DUK__APPENDBUFFER(lex_ctx, 0x000d);
  64510. } else if (x == 't') {
  64511. DUK__APPENDBUFFER(lex_ctx, 0x0009);
  64512. } else if (x == 'v') {
  64513. DUK__APPENDBUFFER(lex_ctx, 0x000b);
  64514. } else if (x == 'x') {
  64515. adv = 4 - 1;
  64516. DUK__APPENDBUFFER(lex_ctx, duk__decode_hexesc_from_window(lex_ctx, 2));
  64517. } else if (x == 'u') {
  64518. adv = 6 - 1;
  64519. DUK__APPENDBUFFER(lex_ctx, duk__decode_uniesc_from_window(lex_ctx, 2));
  64520. } else if (DUK__ISDIGIT(x)) {
  64521. duk_codepoint_t ch = 0; /* initialized to avoid warnings of unused var */
  64522. /*
  64523. * Octal escape or zero escape:
  64524. * \0 (lookahead not DecimalDigit)
  64525. * \1 ... \7 (lookahead not DecimalDigit)
  64526. * \ZeroToThree OctalDigit (lookahead not DecimalDigit)
  64527. * \FourToSeven OctalDigit (no lookahead restrictions)
  64528. * \ZeroToThree OctalDigit OctalDigit (no lookahead restrictions)
  64529. *
  64530. * Zero escape is part of the standard syntax. Octal escapes are
  64531. * defined in E5 Section B.1.2, and are only allowed in non-strict mode.
  64532. * Any other productions starting with a decimal digit are invalid.
  64533. */
  64534. if (x == '0' && !DUK__ISDIGIT(DUK__L2())) {
  64535. /* Zero escape (also allowed in non-strict mode) */
  64536. ch = 0;
  64537. /* adv = 2 - 1 default OK */
  64538. #ifdef DUK_USE_OCTAL_SUPPORT
  64539. } else if (strict_mode) {
  64540. /* No other escape beginning with a digit in strict mode */
  64541. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64542. "invalid escape while parsing string literal");
  64543. } else if (DUK__ISDIGIT03(x) && DUK__ISOCTDIGIT(DUK__L2()) && DUK__ISOCTDIGIT(DUK__L3())) {
  64544. /* Three digit octal escape, digits validated. */
  64545. adv = 4 - 1;
  64546. ch = (duk__hexval(lex_ctx, x) << 6) +
  64547. (duk__hexval(lex_ctx, DUK__L2()) << 3) +
  64548. duk__hexval(lex_ctx, DUK__L3());
  64549. } else if (((DUK__ISDIGIT03(x) && !DUK__ISDIGIT(DUK__L3())) || DUK__ISDIGIT47(x)) &&
  64550. DUK__ISOCTDIGIT(DUK__L2())) {
  64551. /* Two digit octal escape, digits validated.
  64552. *
  64553. * The if-condition is a bit tricky. We could catch e.g.
  64554. * '\039' in the three-digit escape and fail it there (by
  64555. * validating the digits), but we want to avoid extra
  64556. * additional validation code.
  64557. */
  64558. adv = 3 - 1;
  64559. ch = (duk__hexval(lex_ctx, x) << 3) +
  64560. duk__hexval(lex_ctx, DUK__L2());
  64561. } else if (DUK__ISDIGIT(x) && !DUK__ISDIGIT(DUK__L2())) {
  64562. /* One digit octal escape, digit validated. */
  64563. /* adv = 2 default OK */
  64564. ch = duk__hexval(lex_ctx, x);
  64565. #else
  64566. /* fall through to error */
  64567. #endif
  64568. } else {
  64569. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64570. "invalid escape while parsing string literal");
  64571. }
  64572. DUK__APPENDBUFFER(lex_ctx, ch);
  64573. } else {
  64574. /* escaped NonEscapeCharacter */
  64575. DUK__APPENDBUFFER(lex_ctx, x);
  64576. }
  64577. DUK__ADVANCECHARS(lex_ctx, adv);
  64578. /* Track number of escapes; count not really needed but directive
  64579. * prologues need to detect whether there were any escapes or line
  64580. * continuations or not.
  64581. */
  64582. out_token->num_escapes++;
  64583. } else {
  64584. /* part of string */
  64585. DUK__APPENDBUFFER(lex_ctx, x);
  64586. }
  64587. }
  64588. duk__internbuffer(lex_ctx, lex_ctx->slot1_idx);
  64589. out_token->str1 = duk_get_hstring((duk_context *) lex_ctx->thr, lex_ctx->slot1_idx);
  64590. DUK__INITBUFFER(lex_ctx); /* free some memory */
  64591. advtok = DUK__ADVTOK(0, DUK_TOK_STRING);
  64592. break;
  64593. }
  64594. default:
  64595. goto slow_path;
  64596. } /* switch */
  64597. goto skip_slow_path;
  64598. slow_path:
  64599. if (duk_unicode_is_line_terminator(x)) {
  64600. if (x == 0x000d && DUK__L1() == 0x000a) {
  64601. /*
  64602. * E5 Section 7.3: CR LF is detected as a single line terminator for
  64603. * line numbers. Here we also detect it as a single line terminator
  64604. * token.
  64605. */
  64606. DUK__ADVANCECHARS(lex_ctx, 2);
  64607. } else {
  64608. DUK__ADVANCECHARS(lex_ctx, 1);
  64609. }
  64610. got_lineterm = 1;
  64611. goto restart_lineupdate;
  64612. } else if (duk_unicode_is_identifier_start(x) || x == '\\') {
  64613. /*
  64614. * Parse an identifier and then check whether it is:
  64615. * - reserved word (keyword or other reserved word)
  64616. * - "null" (NullLiteral)
  64617. * - "true" (BooleanLiteral)
  64618. * - "false" (BooleanLiteral)
  64619. * - anything else => identifier
  64620. *
  64621. * This does not follow the E5 productions cleanly, but is
  64622. * useful and compact.
  64623. *
  64624. * Note that identifiers may contain Unicode escapes,
  64625. * see E5 Sections 6 and 7.6. They must be decoded first,
  64626. * and the result checked against allowed characters.
  64627. * The above if-clause accepts an identifier start and an
  64628. * '\' character -- no other token can begin with a '\'.
  64629. *
  64630. * Note that "get" and "set" are not reserved words in E5
  64631. * specification so they are recognized as plain identifiers
  64632. * (the tokens DUK_TOK_GET and DUK_TOK_SET are actually not
  64633. * used now). The compiler needs to work around this.
  64634. *
  64635. * Strictly speaking, following Ecmascript longest match
  64636. * specification, an invalid escape for the first character
  64637. * should cause a syntax error. However, an invalid escape
  64638. * for IdentifierParts should just terminate the identifier
  64639. * early (longest match), and let the next tokenization
  64640. * fail. For instance Rhino croaks with 'foo\z' when
  64641. * parsing the identifier. This has little practical impact.
  64642. */
  64643. duk_small_int_t i, i_end;
  64644. duk_bool_t first = 1;
  64645. duk_hstring *str;
  64646. DUK__INITBUFFER(lex_ctx);
  64647. for (;;) {
  64648. /* re-lookup first char on first loop */
  64649. if (DUK__L0() == '\\') {
  64650. duk_codepoint_t ch;
  64651. if (DUK__L1() != 'u') {
  64652. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64653. "invalid unicode escape while parsing identifier");
  64654. }
  64655. ch = duk__decode_uniesc_from_window(lex_ctx, 2);
  64656. /* IdentifierStart is stricter than IdentifierPart, so if the first
  64657. * character is escaped, must have a stricter check here.
  64658. */
  64659. if (!(first ? duk_unicode_is_identifier_start(ch) : duk_unicode_is_identifier_part(ch))) {
  64660. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64661. "invalid unicode escaped character while parsing identifier");
  64662. }
  64663. DUK__APPENDBUFFER(lex_ctx, ch);
  64664. DUK__ADVANCECHARS(lex_ctx, 6);
  64665. /* Track number of escapes: necessary for proper keyword
  64666. * detection.
  64667. */
  64668. out_token->num_escapes++;
  64669. } else {
  64670. /* Note: first character is checked against this. But because
  64671. * IdentifierPart includes all IdentifierStart characters, and
  64672. * the first character (if unescaped) has already been checked
  64673. * in the if condition, this is OK.
  64674. */
  64675. if (!duk_unicode_is_identifier_part(DUK__L0())) {
  64676. break;
  64677. }
  64678. DUK__APPENDBUFFER(lex_ctx, DUK__L0());
  64679. DUK__ADVANCECHARS(lex_ctx, 1);
  64680. }
  64681. first = 0;
  64682. }
  64683. duk__internbuffer(lex_ctx, lex_ctx->slot1_idx);
  64684. out_token->str1 = duk_get_hstring((duk_context *) lex_ctx->thr, lex_ctx->slot1_idx);
  64685. str = out_token->str1;
  64686. DUK_ASSERT(str != NULL);
  64687. out_token->t_nores = DUK_TOK_IDENTIFIER;
  64688. DUK__INITBUFFER(lex_ctx); /* free some memory */
  64689. /*
  64690. * Interned identifier is compared against reserved words, which are
  64691. * currently interned into the heap context. See genstrings.py.
  64692. *
  64693. * Note that an escape in the identifier disables recognition of
  64694. * keywords; e.g. "\u0069f = 1;" is a valid statement (assigns to
  64695. * identifier named "if"). This is not necessarily compliant,
  64696. * see test-dec-escaped-char-in-keyword.js.
  64697. *
  64698. * Note: "get" and "set" are awkward. They are not officially
  64699. * ReservedWords (and indeed e.g. "var set = 1;" is valid), and
  64700. * must come out as DUK_TOK_IDENTIFIER. The compiler needs to
  64701. * work around this a bit.
  64702. */
  64703. /* XXX: optimize by adding the token numbers directly into the
  64704. * always interned duk_hstring objects (there should be enough
  64705. * flag bits free for that)?
  64706. */
  64707. i_end = (strict_mode ? DUK_STRIDX_END_RESERVED : DUK_STRIDX_START_STRICT_RESERVED);
  64708. advtok = DUK__ADVTOK(0, DUK_TOK_IDENTIFIER);
  64709. if (out_token->num_escapes == 0) {
  64710. for (i = DUK_STRIDX_START_RESERVED; i < i_end; i++) {
  64711. DUK_ASSERT(i >= 0 && i < DUK_HEAP_NUM_STRINGS);
  64712. if (DUK_HTHREAD_GET_STRING(lex_ctx->thr, i) == str) {
  64713. advtok = DUK__ADVTOK(0, DUK_STRIDX_TO_TOK(i));
  64714. break;
  64715. }
  64716. }
  64717. }
  64718. } else if (DUK__ISDIGIT(x) || (x == '.')) {
  64719. /* Note: decimal number may start with a period, but must be followed by a digit */
  64720. /*
  64721. * DecimalLiteral, HexIntegerLiteral, OctalIntegerLiteral
  64722. * "pre-parsing", followed by an actual, accurate parser step.
  64723. *
  64724. * Note: the leading sign character ('+' or '-') is -not- part of
  64725. * the production in E5 grammar, and that the a DecimalLiteral
  64726. * starting with a '0' must be followed by a non-digit. Leading
  64727. * zeroes are syntax errors and must be checked for.
  64728. *
  64729. * XXX: the two step parsing process is quite awkward, it would
  64730. * be more straightforward to allow numconv to parse the longest
  64731. * valid prefix (it already does that, it only needs to indicate
  64732. * where the input ended). However, the lexer decodes characters
  64733. * using a lookup window, so this is not a trivial change.
  64734. */
  64735. /* XXX: because of the final check below (that the literal is not
  64736. * followed by a digit), this could maybe be simplified, if we bail
  64737. * out early from a leading zero (and if there are no periods etc).
  64738. * Maybe too complex.
  64739. */
  64740. duk_double_t val;
  64741. duk_bool_t int_only = 0;
  64742. duk_bool_t allow_hex = 0;
  64743. duk_small_int_t state; /* 0=before period/exp,
  64744. * 1=after period, before exp
  64745. * 2=after exp, allow '+' or '-'
  64746. * 3=after exp and exp sign
  64747. */
  64748. duk_small_uint_t s2n_flags;
  64749. duk_codepoint_t y;
  64750. DUK__INITBUFFER(lex_ctx);
  64751. y = DUK__L1();
  64752. if (x == '0' && (y == 'x' || y == 'X')) {
  64753. DUK__APPENDBUFFER(lex_ctx, x);
  64754. DUK__APPENDBUFFER(lex_ctx, y);
  64755. DUK__ADVANCECHARS(lex_ctx, 2);
  64756. int_only = 1;
  64757. allow_hex = 1;
  64758. #ifdef DUK_USE_OCTAL_SUPPORT
  64759. } else if (!strict_mode && x == '0' && DUK__ISDIGIT(y)) {
  64760. /* Note: if DecimalLiteral starts with a '0', it can only be
  64761. * followed by a period or an exponent indicator which starts
  64762. * with 'e' or 'E'. Hence the if-check above ensures that
  64763. * OctalIntegerLiteral is the only valid NumericLiteral
  64764. * alternative at this point (even if y is, say, '9').
  64765. */
  64766. DUK__APPENDBUFFER(lex_ctx, x);
  64767. DUK__ADVANCECHARS(lex_ctx, 1);
  64768. int_only = 1;
  64769. #endif
  64770. }
  64771. state = 0;
  64772. for (;;) {
  64773. x = DUK__L0(); /* re-lookup curr char on first round */
  64774. if (DUK__ISDIGIT(x)) {
  64775. /* Note: intentionally allow leading zeroes here, as the
  64776. * actual parser will check for them.
  64777. */
  64778. if (state == 2) {
  64779. state = 3;
  64780. }
  64781. } else if (allow_hex && DUK__ISHEXDIGIT(x)) {
  64782. /* Note: 'e' and 'E' are also accepted here. */
  64783. ;
  64784. } else if (x == '.') {
  64785. if (state >= 1 || int_only) {
  64786. break;
  64787. } else {
  64788. state = 1;
  64789. }
  64790. } else if (x == 'e' || x == 'E') {
  64791. if (state >= 2 || int_only) {
  64792. break;
  64793. } else {
  64794. state = 2;
  64795. }
  64796. } else if (x == '-' || x == '+') {
  64797. if (state != 2) {
  64798. break;
  64799. } else {
  64800. state = 3;
  64801. }
  64802. } else {
  64803. break;
  64804. }
  64805. DUK__APPENDBUFFER(lex_ctx, x);
  64806. DUK__ADVANCECHARS(lex_ctx, 1);
  64807. }
  64808. /* XXX: better coercion */
  64809. duk__internbuffer(lex_ctx, lex_ctx->slot1_idx);
  64810. s2n_flags = DUK_S2N_FLAG_ALLOW_EXP |
  64811. DUK_S2N_FLAG_ALLOW_FRAC |
  64812. DUK_S2N_FLAG_ALLOW_NAKED_FRAC |
  64813. DUK_S2N_FLAG_ALLOW_EMPTY_FRAC |
  64814. #ifdef DUK_USE_OCTAL_SUPPORT
  64815. (strict_mode ? 0 : DUK_S2N_FLAG_ALLOW_AUTO_OCT_INT) |
  64816. #endif
  64817. DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT;
  64818. duk_dup((duk_context *) lex_ctx->thr, lex_ctx->slot1_idx);
  64819. duk_numconv_parse((duk_context *) lex_ctx->thr, 10 /*radix*/, s2n_flags);
  64820. val = duk_to_number((duk_context *) lex_ctx->thr, -1);
  64821. if (DUK_ISNAN(val)) {
  64822. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR, "invalid numeric literal");
  64823. }
  64824. duk_replace((duk_context *) lex_ctx->thr, lex_ctx->slot1_idx); /* could also just pop? */
  64825. DUK__INITBUFFER(lex_ctx); /* free some memory */
  64826. /* Section 7.8.3 (note): NumericLiteral must be followed by something other than
  64827. * IdentifierStart or DecimalDigit.
  64828. */
  64829. if (DUK__ISDIGIT(DUK__L0()) || duk_unicode_is_identifier_start(DUK__L0())) {
  64830. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR, "invalid numeric literal");
  64831. }
  64832. out_token->num = val;
  64833. advtok = DUK__ADVTOK(0, DUK_TOK_NUMBER);
  64834. } else if (duk_unicode_is_whitespace(DUK__LOOKUP(lex_ctx, 0))) {
  64835. DUK__ADVANCECHARS(lex_ctx, 1);
  64836. goto restart;
  64837. } else if (x < 0) {
  64838. advtok = DUK__ADVTOK(0, DUK_TOK_EOF);
  64839. } else {
  64840. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR, "error parsing token");
  64841. }
  64842. skip_slow_path:
  64843. /*
  64844. * Shared exit path
  64845. */
  64846. DUK__ADVANCEBYTES(lex_ctx, advtok >> 8);
  64847. out_token->t = advtok & 0xff;
  64848. if (out_token->t_nores < 0) {
  64849. out_token->t_nores = out_token->t;
  64850. }
  64851. out_token->lineterm = got_lineterm;
  64852. /* Automatic semicolon insertion is allowed if a token is preceded
  64853. * by line terminator(s), or terminates a statement list (right curly
  64854. * or EOF).
  64855. */
  64856. if (got_lineterm || out_token->t == DUK_TOK_RCURLY || out_token->t == DUK_TOK_EOF) {
  64857. out_token->allow_auto_semi = 1;
  64858. } else {
  64859. out_token->allow_auto_semi = 0;
  64860. }
  64861. }
  64862. #ifdef DUK_USE_REGEXP_SUPPORT
  64863. /*
  64864. * Parse a RegExp token. The grammar is described in E5 Section 15.10.
  64865. * Terminal constructions (such as quantifiers) are parsed directly here.
  64866. *
  64867. * 0xffffffffU is used as a marker for "infinity" in quantifiers. Further,
  64868. * DUK__MAX_RE_QUANT_DIGITS limits the maximum number of digits that
  64869. * will be accepted for a quantifier.
  64870. */
  64871. DUK_INTERNAL void duk_lexer_parse_re_token(duk_lexer_ctx *lex_ctx, duk_re_token *out_token) {
  64872. duk_small_int_t advtok = 0; /* init is unnecessary but suppresses "may be used uninitialized" warnings */
  64873. duk_codepoint_t x, y;
  64874. if (++lex_ctx->token_count >= lex_ctx->token_limit) {
  64875. DUK_ERROR(lex_ctx->thr, DUK_ERR_RANGE_ERROR, "token limit");
  64876. return; /* unreachable */
  64877. }
  64878. DUK_MEMZERO(out_token, sizeof(*out_token));
  64879. x = DUK__L0();
  64880. y = DUK__L1();
  64881. DUK_DDD(DUK_DDDPRINT("parsing regexp token, L0=%ld, L1=%ld", (long) x, (long) y));
  64882. switch (x) {
  64883. case '|': {
  64884. advtok = DUK__ADVTOK(1, DUK_RETOK_DISJUNCTION);
  64885. break;
  64886. }
  64887. case '^': {
  64888. advtok = DUK__ADVTOK(1, DUK_RETOK_ASSERT_START);
  64889. break;
  64890. }
  64891. case '$': {
  64892. advtok = DUK__ADVTOK(1, DUK_RETOK_ASSERT_END);
  64893. break;
  64894. }
  64895. case '?': {
  64896. out_token->qmin = 0;
  64897. out_token->qmax = 1;
  64898. if (y == '?') {
  64899. advtok = DUK__ADVTOK(2, DUK_RETOK_QUANTIFIER);
  64900. out_token->greedy = 0;
  64901. } else {
  64902. advtok = DUK__ADVTOK(1, DUK_RETOK_QUANTIFIER);
  64903. out_token->greedy = 1;
  64904. }
  64905. break;
  64906. }
  64907. case '*': {
  64908. out_token->qmin = 0;
  64909. out_token->qmax = DUK_RE_QUANTIFIER_INFINITE;
  64910. if (y == '?') {
  64911. advtok = DUK__ADVTOK(2, DUK_RETOK_QUANTIFIER);
  64912. out_token->greedy = 0;
  64913. } else {
  64914. advtok = DUK__ADVTOK(1, DUK_RETOK_QUANTIFIER);
  64915. out_token->greedy = 1;
  64916. }
  64917. break;
  64918. }
  64919. case '+': {
  64920. out_token->qmin = 1;
  64921. out_token->qmax = DUK_RE_QUANTIFIER_INFINITE;
  64922. if (y == '?') {
  64923. advtok = DUK__ADVTOK(2, DUK_RETOK_QUANTIFIER);
  64924. out_token->greedy = 0;
  64925. } else {
  64926. advtok = DUK__ADVTOK(1, DUK_RETOK_QUANTIFIER);
  64927. out_token->greedy = 1;
  64928. }
  64929. break;
  64930. }
  64931. case '{': {
  64932. /* Production allows 'DecimalDigits', including leading zeroes */
  64933. duk_uint_fast32_t val1 = 0;
  64934. duk_uint_fast32_t val2 = DUK_RE_QUANTIFIER_INFINITE;
  64935. duk_small_int_t digits = 0;
  64936. for (;;) {
  64937. DUK__ADVANCECHARS(lex_ctx, 1); /* eat '{' on entry */
  64938. x = DUK__L0();
  64939. if (DUK__ISDIGIT(x)) {
  64940. if (digits >= DUK__MAX_RE_QUANT_DIGITS) {
  64941. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64942. "invalid regexp quantifier (too many digits)");
  64943. }
  64944. digits++;
  64945. val1 = val1 * 10 + (duk_uint_fast32_t) duk__hexval(lex_ctx, x);
  64946. } else if (x == ',') {
  64947. if (val2 != DUK_RE_QUANTIFIER_INFINITE) {
  64948. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64949. "invalid regexp quantifier (double comma)");
  64950. }
  64951. if (DUK__L1() == '}') {
  64952. /* form: { DecimalDigits , }, val1 = min count */
  64953. if (digits == 0) {
  64954. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64955. "invalid regexp quantifier (missing digits)");
  64956. }
  64957. out_token->qmin = val1;
  64958. out_token->qmax = DUK_RE_QUANTIFIER_INFINITE;
  64959. DUK__ADVANCECHARS(lex_ctx, 2);
  64960. break;
  64961. }
  64962. val2 = val1;
  64963. val1 = 0;
  64964. digits = 0; /* not strictly necessary because of lookahead '}' above */
  64965. } else if (x == '}') {
  64966. if (digits == 0) {
  64967. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64968. "invalid regexp quantifier (missing digits)");
  64969. }
  64970. if (val2 != DUK_RE_QUANTIFIER_INFINITE) {
  64971. /* val2 = min count, val1 = max count */
  64972. out_token->qmin = val2;
  64973. out_token->qmax = val1;
  64974. } else {
  64975. /* val1 = count */
  64976. out_token->qmin = val1;
  64977. out_token->qmax = val1;
  64978. }
  64979. DUK__ADVANCECHARS(lex_ctx, 1);
  64980. break;
  64981. } else {
  64982. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64983. "invalid regexp quantifier (unknown char)");
  64984. }
  64985. }
  64986. if (DUK__L0() == '?') {
  64987. out_token->greedy = 0;
  64988. DUK__ADVANCECHARS(lex_ctx, 1);
  64989. } else {
  64990. out_token->greedy = 1;
  64991. }
  64992. advtok = DUK__ADVTOK(0, DUK_RETOK_QUANTIFIER);
  64993. break;
  64994. }
  64995. case '.': {
  64996. advtok = DUK__ADVTOK(1, DUK_RETOK_ATOM_PERIOD);
  64997. break;
  64998. }
  64999. case '\\': {
  65000. /* The E5.1 specification does not seem to allow IdentifierPart characters
  65001. * to be used as identity escapes. Unfortunately this includes '$', which
  65002. * cannot be escaped as '\$'; it needs to be escaped e.g. as '\u0024'.
  65003. * Many other implementations (including V8 and Rhino, for instance) do
  65004. * accept '\$' as a valid identity escape, which is quite pragmatic.
  65005. * See: test-regexp-identity-escape-dollar.js.
  65006. */
  65007. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_CHAR); /* default: char escape (two chars) */
  65008. if (y == 'b') {
  65009. advtok = DUK__ADVTOK(2, DUK_RETOK_ASSERT_WORD_BOUNDARY);
  65010. } else if (y == 'B') {
  65011. advtok = DUK__ADVTOK(2, DUK_RETOK_ASSERT_NOT_WORD_BOUNDARY);
  65012. } else if (y == 'f') {
  65013. out_token->num = 0x000c;
  65014. } else if (y == 'n') {
  65015. out_token->num = 0x000a;
  65016. } else if (y == 't') {
  65017. out_token->num = 0x0009;
  65018. } else if (y == 'r') {
  65019. out_token->num = 0x000d;
  65020. } else if (y == 'v') {
  65021. out_token->num = 0x000b;
  65022. } else if (y == 'c') {
  65023. x = DUK__L2();
  65024. if ((x >= 'a' && x <= 'z') ||
  65025. (x >= 'A' && x <= 'Z')) {
  65026. out_token->num = (x % 32);
  65027. advtok = DUK__ADVTOK(3, DUK_RETOK_ATOM_CHAR);
  65028. } else {
  65029. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  65030. "invalid regexp control escape");
  65031. }
  65032. } else if (y == 'x') {
  65033. out_token->num = duk__decode_hexesc_from_window(lex_ctx, 2);
  65034. advtok = DUK__ADVTOK(4, DUK_RETOK_ATOM_CHAR);
  65035. } else if (y == 'u') {
  65036. out_token->num = duk__decode_uniesc_from_window(lex_ctx, 2);
  65037. advtok = DUK__ADVTOK(6, DUK_RETOK_ATOM_CHAR);
  65038. } else if (y == 'd') {
  65039. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_DIGIT);
  65040. } else if (y == 'D') {
  65041. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_NOT_DIGIT);
  65042. } else if (y == 's') {
  65043. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_WHITE);
  65044. } else if (y == 'S') {
  65045. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_NOT_WHITE);
  65046. } else if (y == 'w') {
  65047. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_WORD_CHAR);
  65048. } else if (y == 'W') {
  65049. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_NOT_WORD_CHAR);
  65050. } else if (DUK__ISDIGIT(y)) {
  65051. /* E5 Section 15.10.2.11 */
  65052. if (y == '0') {
  65053. if (DUK__ISDIGIT(DUK__L2())) {
  65054. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  65055. "invalid regexp escape");
  65056. }
  65057. out_token->num = 0x0000;
  65058. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_CHAR);
  65059. } else {
  65060. /* XXX: shared parsing? */
  65061. duk_uint_fast32_t val = 0;
  65062. duk_small_int_t i;
  65063. for (i = 0; ; i++) {
  65064. if (i >= DUK__MAX_RE_DECESC_DIGITS) {
  65065. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  65066. "invalid regexp escape (decimal escape too long)");
  65067. }
  65068. DUK__ADVANCECHARS(lex_ctx, 1); /* eat backslash on entry */
  65069. x = DUK__L0();
  65070. if (!DUK__ISDIGIT(x)) {
  65071. break;
  65072. }
  65073. val = val * 10 + (duk_uint_fast32_t) duk__hexval(lex_ctx, x);
  65074. }
  65075. /* DUK__L0() cannot be a digit, because the loop doesn't terminate if it is */
  65076. advtok = DUK__ADVTOK(0, DUK_RETOK_ATOM_BACKREFERENCE);
  65077. out_token->num = val;
  65078. }
  65079. } else if ((y >= 0 && !duk_unicode_is_identifier_part(y)) ||
  65080. #if defined(DUK_USE_NONSTD_REGEXP_DOLLAR_ESCAPE)
  65081. y == '$' ||
  65082. #endif
  65083. y == DUK_UNICODE_CP_ZWNJ ||
  65084. y == DUK_UNICODE_CP_ZWJ) {
  65085. /* IdentityEscape, with dollar added as a valid additional
  65086. * non-standard escape (see test-regexp-identity-escape-dollar.js).
  65087. * Careful not to match end-of-buffer (<0) here.
  65088. */
  65089. out_token->num = y;
  65090. } else {
  65091. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  65092. "invalid regexp escape");
  65093. }
  65094. break;
  65095. }
  65096. case '(': {
  65097. /* XXX: naming is inconsistent: ATOM_END_GROUP ends an ASSERT_START_LOOKAHEAD */
  65098. if (y == '?') {
  65099. if (DUK__L2() == '=') {
  65100. /* (?= */
  65101. advtok = DUK__ADVTOK(3, DUK_RETOK_ASSERT_START_POS_LOOKAHEAD);
  65102. } else if (DUK__L2() == '!') {
  65103. /* (?! */
  65104. advtok = DUK__ADVTOK(3, DUK_RETOK_ASSERT_START_NEG_LOOKAHEAD);
  65105. } else if (DUK__L2() == ':') {
  65106. /* (?: */
  65107. advtok = DUK__ADVTOK(3, DUK_RETOK_ATOM_START_NONCAPTURE_GROUP);
  65108. }
  65109. } else {
  65110. /* ( */
  65111. advtok = DUK__ADVTOK(1, DUK_RETOK_ATOM_START_CAPTURE_GROUP);
  65112. }
  65113. break;
  65114. }
  65115. case ')': {
  65116. advtok = DUK__ADVTOK(1, DUK_RETOK_ATOM_END_GROUP);
  65117. break;
  65118. }
  65119. case '[': {
  65120. /*
  65121. * To avoid creating a heavy intermediate value for the list of ranges,
  65122. * only the start token ('[' or '[^') is parsed here. The regexp
  65123. * compiler parses the ranges itself.
  65124. */
  65125. advtok = DUK__ADVTOK(1, DUK_RETOK_ATOM_START_CHARCLASS);
  65126. if (y == '^') {
  65127. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_START_CHARCLASS_INVERTED);
  65128. }
  65129. break;
  65130. }
  65131. case ']':
  65132. case '}': {
  65133. /* Although these could be parsed as PatternCharacters unambiguously (here),
  65134. * E5 Section 15.10.1 grammar explicitly forbids these as PatternCharacters.
  65135. */
  65136. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  65137. "invalid regexp character");
  65138. break;
  65139. }
  65140. case -1: {
  65141. /* EOF */
  65142. advtok = DUK__ADVTOK(0, DUK_TOK_EOF);
  65143. break;
  65144. }
  65145. default: {
  65146. /* PatternCharacter, all excluded characters are matched by cases above */
  65147. advtok = DUK__ADVTOK(1, DUK_RETOK_ATOM_CHAR);
  65148. out_token->num = x;
  65149. break;
  65150. }
  65151. }
  65152. /*
  65153. * Shared exit path
  65154. */
  65155. DUK__ADVANCEBYTES(lex_ctx, advtok >> 8);
  65156. out_token->t = advtok & 0xff;
  65157. }
  65158. /*
  65159. * Special parser for character classes; calls callback for every
  65160. * range parsed and returns the number of ranges present.
  65161. */
  65162. /* XXX: this duplicates functionality in duk_regexp.c where a similar loop is
  65163. * required anyway. We could use that BUT we need to update the regexp compiler
  65164. * 'nranges' too. Work this out a bit more cleanly to save space.
  65165. */
  65166. /* XXX: the handling of character range detection is a bit convoluted.
  65167. * Try to simplify and make smaller.
  65168. */
  65169. /* XXX: logic for handling character ranges is now incorrect, it will accept
  65170. * e.g. [\d-z] whereas it should croak from it? SMJS accepts this too, though.
  65171. *
  65172. * Needs a read through and a lot of additional tests.
  65173. */
  65174. DUK_LOCAL
  65175. void duk__emit_u16_direct_ranges(duk_lexer_ctx *lex_ctx,
  65176. duk_re_range_callback gen_range,
  65177. void *userdata,
  65178. duk_uint16_t *ranges,
  65179. duk_small_int_t num) {
  65180. duk_uint16_t *ranges_end;
  65181. DUK_UNREF(lex_ctx);
  65182. ranges_end = ranges + num;
  65183. while (ranges < ranges_end) {
  65184. /* mark range 'direct', bypass canonicalization (see Wiki) */
  65185. gen_range(userdata, (duk_codepoint_t) ranges[0], (duk_codepoint_t) ranges[1], 1);
  65186. ranges += 2;
  65187. }
  65188. }
  65189. DUK_INTERNAL void duk_lexer_parse_re_ranges(duk_lexer_ctx *lex_ctx, duk_re_range_callback gen_range, void *userdata) {
  65190. duk_codepoint_t start = -1;
  65191. duk_codepoint_t ch;
  65192. duk_codepoint_t x;
  65193. duk_bool_t dash = 0;
  65194. DUK_DD(DUK_DDPRINT("parsing regexp ranges"));
  65195. for (;;) {
  65196. x = DUK__L0();
  65197. DUK__ADVANCECHARS(lex_ctx, 1);
  65198. ch = -1; /* not strictly necessary, but avoids "uninitialized variable" warnings */
  65199. DUK_UNREF(ch);
  65200. if (x < 0) {
  65201. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  65202. "eof while parsing character class");
  65203. } else if (x == ']') {
  65204. DUK_ASSERT(!dash); /* lookup should prevent this */
  65205. if (start >= 0) {
  65206. gen_range(userdata, start, start, 0);
  65207. }
  65208. break;
  65209. } else if (x == '-') {
  65210. if (start >= 0 && !dash && DUK__L0() != ']') {
  65211. /* '-' as a range indicator */
  65212. dash = 1;
  65213. continue;
  65214. } else {
  65215. /* '-' verbatim */
  65216. ch = x;
  65217. }
  65218. } else if (x == '\\') {
  65219. /*
  65220. * The escapes are same as outside a character class, except that \b has a
  65221. * different meaning, and \B and backreferences are prohibited (see E5
  65222. * Section 15.10.2.19). However, it's difficult to share code because we
  65223. * handle e.g. "\n" very differently: here we generate a single character
  65224. * range for it.
  65225. */
  65226. x = DUK__L0();
  65227. DUK__ADVANCECHARS(lex_ctx, 1);
  65228. if (x == 'b') {
  65229. /* Note: '\b' in char class is different than outside (assertion),
  65230. * '\B' is not allowed and is caught by the duk_unicode_is_identifier_part()
  65231. * check below.
  65232. */
  65233. ch = 0x0008;
  65234. } else if (x == 'f') {
  65235. ch = 0x000c;
  65236. } else if (x == 'n') {
  65237. ch = 0x000a;
  65238. } else if (x == 't') {
  65239. ch = 0x0009;
  65240. } else if (x == 'r') {
  65241. ch = 0x000d;
  65242. } else if (x == 'v') {
  65243. ch = 0x000b;
  65244. } else if (x == 'c') {
  65245. x = DUK__L0();
  65246. DUK__ADVANCECHARS(lex_ctx, 1);
  65247. if ((x >= 'a' && x <= 'z') ||
  65248. (x >= 'A' && x <= 'Z')) {
  65249. ch = (x % 32);
  65250. } else {
  65251. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  65252. "invalid regexp control escape");
  65253. return; /* never reached, but avoids warnings of
  65254. * potentially unused variables.
  65255. */
  65256. }
  65257. } else if (x == 'x') {
  65258. ch = duk__decode_hexesc_from_window(lex_ctx, 0);
  65259. DUK__ADVANCECHARS(lex_ctx, 2);
  65260. } else if (x == 'u') {
  65261. ch = duk__decode_uniesc_from_window(lex_ctx, 0);
  65262. DUK__ADVANCECHARS(lex_ctx, 4);
  65263. } else if (x == 'd') {
  65264. duk__emit_u16_direct_ranges(lex_ctx,
  65265. gen_range,
  65266. userdata,
  65267. duk_unicode_re_ranges_digit,
  65268. sizeof(duk_unicode_re_ranges_digit) / sizeof(duk_uint16_t));
  65269. ch = -1;
  65270. } else if (x == 'D') {
  65271. duk__emit_u16_direct_ranges(lex_ctx,
  65272. gen_range,
  65273. userdata,
  65274. duk_unicode_re_ranges_not_digit,
  65275. sizeof(duk_unicode_re_ranges_not_digit) / sizeof(duk_uint16_t));
  65276. ch = -1;
  65277. } else if (x == 's') {
  65278. duk__emit_u16_direct_ranges(lex_ctx,
  65279. gen_range,
  65280. userdata,
  65281. duk_unicode_re_ranges_white,
  65282. sizeof(duk_unicode_re_ranges_white) / sizeof(duk_uint16_t));
  65283. ch = -1;
  65284. } else if (x == 'S') {
  65285. duk__emit_u16_direct_ranges(lex_ctx,
  65286. gen_range,
  65287. userdata,
  65288. duk_unicode_re_ranges_not_white,
  65289. sizeof(duk_unicode_re_ranges_not_white) / sizeof(duk_uint16_t));
  65290. ch = -1;
  65291. } else if (x == 'w') {
  65292. duk__emit_u16_direct_ranges(lex_ctx,
  65293. gen_range,
  65294. userdata,
  65295. duk_unicode_re_ranges_wordchar,
  65296. sizeof(duk_unicode_re_ranges_wordchar) / sizeof(duk_uint16_t));
  65297. ch = -1;
  65298. } else if (x == 'W') {
  65299. duk__emit_u16_direct_ranges(lex_ctx,
  65300. gen_range,
  65301. userdata,
  65302. duk_unicode_re_ranges_not_wordchar,
  65303. sizeof(duk_unicode_re_ranges_not_wordchar) / sizeof(duk_uint16_t));
  65304. ch = -1;
  65305. } else if (DUK__ISDIGIT(x)) {
  65306. /* DecimalEscape, only \0 is allowed, no leading zeroes are allowed */
  65307. if (x == '0' && !DUK__ISDIGIT(DUK__L0())) {
  65308. ch = 0x0000;
  65309. } else {
  65310. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  65311. "invalid decimal escape");
  65312. }
  65313. } else if (!duk_unicode_is_identifier_part(x)
  65314. #if defined(DUK_USE_NONSTD_REGEXP_DOLLAR_ESCAPE)
  65315. || x == '$'
  65316. #endif
  65317. ) {
  65318. /* IdentityEscape */
  65319. ch = x;
  65320. } else {
  65321. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  65322. "invalid regexp escape");
  65323. }
  65324. } else {
  65325. /* character represents itself */
  65326. ch = x;
  65327. }
  65328. /* ch is a literal character here or -1 if parsed entity was
  65329. * an escape such as "\s".
  65330. */
  65331. if (ch < 0) {
  65332. /* multi-character sets not allowed as part of ranges, see
  65333. * E5 Section 15.10.2.15, abstract operation CharacterRange.
  65334. */
  65335. if (start >= 0) {
  65336. if (dash) {
  65337. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  65338. "invalid range");
  65339. } else {
  65340. gen_range(userdata, start, start, 0);
  65341. start = -1;
  65342. /* dash is already 0 */
  65343. }
  65344. }
  65345. } else {
  65346. if (start >= 0) {
  65347. if (dash) {
  65348. if (start > ch) {
  65349. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  65350. "invalid range");
  65351. }
  65352. gen_range(userdata, start, ch, 0);
  65353. start = -1;
  65354. dash = 0;
  65355. } else {
  65356. gen_range(userdata, start, start, 0);
  65357. start = ch;
  65358. /* dash is already 0 */
  65359. }
  65360. } else {
  65361. start = ch;
  65362. }
  65363. }
  65364. }
  65365. return;
  65366. }
  65367. #endif /* DUK_USE_REGEXP_SUPPORT */
  65368. #line 1 "duk_numconv.c"
  65369. /*
  65370. * Number-to-string and string-to-number conversions.
  65371. *
  65372. * Slow path number-to-string and string-to-number conversion is based on
  65373. * a Dragon4 variant, with fast paths for small integers. Big integer
  65374. * arithmetic is needed for guaranteeing that the conversion is correct
  65375. * and uses a minimum number of digits. The big number arithmetic has a
  65376. * fixed maximum size and does not require dynamic allocations.
  65377. *
  65378. * See: doc/number-conversion.rst.
  65379. */
  65380. /* include removed: duk_internal.h */
  65381. #define DUK__IEEE_DOUBLE_EXP_BIAS 1023
  65382. #define DUK__IEEE_DOUBLE_EXP_MIN (-1022) /* biased exp == 0 -> denormal, exp -1022 */
  65383. #define DUK__DIGITCHAR(x) duk_lc_digits[(x)]
  65384. /*
  65385. * Tables generated with src/gennumdigits.py.
  65386. *
  65387. * duk__str2num_digits_for_radix indicates, for each radix, how many input
  65388. * digits should be considered significant for string-to-number conversion.
  65389. * The input is also padded to this many digits to give the Dragon4
  65390. * conversion enough (apparent) precision to work with.
  65391. *
  65392. * duk__str2num_exp_limits indicates, for each radix, the radix-specific
  65393. * minimum/maximum exponent values (for a Dragon4 integer mantissa)
  65394. * below and above which the number is guaranteed to underflow to zero
  65395. * or overflow to Infinity. This allows parsing to keep bigint values
  65396. * bounded.
  65397. */
  65398. DUK_LOCAL const duk_uint8_t duk__str2num_digits_for_radix[] = {
  65399. 69, 44, 35, 30, 27, 25, 23, 22, 20, 20, /* 2 to 11 */
  65400. 20, 19, 19, 18, 18, 17, 17, 17, 16, 16, /* 12 to 21 */
  65401. 16, 16, 16, 15, 15, 15, 15, 15, 15, 14, /* 22 to 31 */
  65402. 14, 14, 14, 14, 14 /* 31 to 36 */
  65403. };
  65404. typedef struct {
  65405. duk_int16_t upper;
  65406. duk_int16_t lower;
  65407. } duk__exp_limits;
  65408. DUK_LOCAL const duk__exp_limits duk__str2num_exp_limits[] = {
  65409. { 957, -1147 }, { 605, -725 }, { 479, -575 }, { 414, -496 },
  65410. { 372, -446 }, { 342, -411 }, { 321, -384 }, { 304, -364 },
  65411. { 291, -346 }, { 279, -334 }, { 268, -323 }, { 260, -312 },
  65412. { 252, -304 }, { 247, -296 }, { 240, -289 }, { 236, -283 },
  65413. { 231, -278 }, { 227, -273 }, { 223, -267 }, { 220, -263 },
  65414. { 216, -260 }, { 213, -256 }, { 210, -253 }, { 208, -249 },
  65415. { 205, -246 }, { 203, -244 }, { 201, -241 }, { 198, -239 },
  65416. { 196, -237 }, { 195, -234 }, { 193, -232 }, { 191, -230 },
  65417. { 190, -228 }, { 188, -226 }, { 187, -225 },
  65418. };
  65419. /*
  65420. * Limited functionality bigint implementation.
  65421. *
  65422. * Restricted to non-negative numbers with less than 32 * DUK__BI_MAX_PARTS bits,
  65423. * with the caller responsible for ensuring this is never exceeded. No memory
  65424. * allocation (except stack) is needed for bigint computation. Operations
  65425. * have been tailored for number conversion needs.
  65426. *
  65427. * Argument order is "assignment order", i.e. target first, then arguments:
  65428. * x <- y * z --> duk__bi_mul(x, y, z);
  65429. */
  65430. /* This upper value has been experimentally determined; debug build will check
  65431. * bigint size with assertions.
  65432. */
  65433. #define DUK__BI_MAX_PARTS 37 /* 37x32 = 1184 bits */
  65434. #ifdef DUK_USE_DDDPRINT
  65435. #define DUK__BI_PRINT(name,x) duk__bi_print((name),(x))
  65436. #else
  65437. #define DUK__BI_PRINT(name,x)
  65438. #endif
  65439. /* Current size is about 152 bytes. */
  65440. typedef struct {
  65441. duk_small_int_t n;
  65442. duk_uint32_t v[DUK__BI_MAX_PARTS]; /* low to high */
  65443. } duk__bigint;
  65444. #ifdef DUK_USE_DDDPRINT
  65445. DUK_LOCAL void duk__bi_print(const char *name, duk__bigint *x) {
  65446. /* Overestimate required size; debug code so not critical to be tight. */
  65447. char buf[DUK__BI_MAX_PARTS * 9 + 64];
  65448. char *p = buf;
  65449. duk_small_int_t i;
  65450. /* No NUL term checks in this debug code. */
  65451. p += DUK_SPRINTF(p, "%p n=%ld", (void *) x, (long) x->n);
  65452. if (x->n == 0) {
  65453. p += DUK_SPRINTF(p, " 0");
  65454. }
  65455. for (i = x->n - 1; i >= 0; i--) {
  65456. p += DUK_SPRINTF(p, " %08lx", (unsigned long) x->v[i]);
  65457. }
  65458. DUK_DDD(DUK_DDDPRINT("%s: %s", (const char *) name, (const char *) buf));
  65459. }
  65460. #endif
  65461. #ifdef DUK_USE_ASSERTIONS
  65462. DUK_LOCAL duk_small_int_t duk__bi_is_valid(duk__bigint *x) {
  65463. return (duk_small_int_t)
  65464. ( ((x->n >= 0) && (x->n <= DUK__BI_MAX_PARTS)) /* is valid size */ &&
  65465. ((x->n == 0) || (x->v[x->n - 1] != 0)) /* is normalized */ );
  65466. }
  65467. #endif
  65468. DUK_LOCAL void duk__bi_normalize(duk__bigint *x) {
  65469. duk_small_int_t i;
  65470. for (i = x->n - 1; i >= 0; i--) {
  65471. if (x->v[i] != 0) {
  65472. break;
  65473. }
  65474. }
  65475. /* Note: if 'x' is zero, x->n becomes 0 here */
  65476. x->n = i + 1;
  65477. DUK_ASSERT(duk__bi_is_valid(x));
  65478. }
  65479. /* x <- y */
  65480. DUK_LOCAL void duk__bi_copy(duk__bigint *x, duk__bigint *y) {
  65481. duk_small_int_t n;
  65482. n = y->n;
  65483. x->n = n;
  65484. if (n == 0) {
  65485. return;
  65486. }
  65487. DUK_MEMCPY((void *) x->v, (const void *) y->v, (size_t) (sizeof(duk_uint32_t) * n));
  65488. }
  65489. DUK_LOCAL void duk__bi_set_small(duk__bigint *x, duk_uint32_t v) {
  65490. if (v == 0U) {
  65491. x->n = 0;
  65492. } else {
  65493. x->n = 1;
  65494. x->v[0] = v;
  65495. }
  65496. DUK_ASSERT(duk__bi_is_valid(x));
  65497. }
  65498. /* Return value: <0 <=> x < y
  65499. * 0 <=> x == y
  65500. * >0 <=> x > y
  65501. */
  65502. DUK_LOCAL int duk__bi_compare(duk__bigint *x, duk__bigint *y) {
  65503. duk_small_int_t i, nx, ny;
  65504. duk_uint32_t tx, ty;
  65505. DUK_ASSERT(duk__bi_is_valid(x));
  65506. DUK_ASSERT(duk__bi_is_valid(y));
  65507. nx = x->n;
  65508. ny = y->n;
  65509. if (nx > ny) {
  65510. goto ret_gt;
  65511. }
  65512. if (nx < ny) {
  65513. goto ret_lt;
  65514. }
  65515. for (i = nx - 1; i >= 0; i--) {
  65516. tx = x->v[i];
  65517. ty = y->v[i];
  65518. if (tx > ty) {
  65519. goto ret_gt;
  65520. }
  65521. if (tx < ty) {
  65522. goto ret_lt;
  65523. }
  65524. }
  65525. return 0;
  65526. ret_gt:
  65527. return 1;
  65528. ret_lt:
  65529. return -1;
  65530. }
  65531. /* x <- y + z */
  65532. #ifdef DUK_USE_64BIT_OPS
  65533. DUK_LOCAL void duk__bi_add(duk__bigint *x, duk__bigint *y, duk__bigint *z) {
  65534. duk_uint64_t tmp;
  65535. duk_small_int_t i, ny, nz;
  65536. DUK_ASSERT(duk__bi_is_valid(y));
  65537. DUK_ASSERT(duk__bi_is_valid(z));
  65538. if (z->n > y->n) {
  65539. duk__bigint *t;
  65540. t = y; y = z; z = t;
  65541. }
  65542. DUK_ASSERT(y->n >= z->n);
  65543. ny = y->n; nz = z->n;
  65544. tmp = 0U;
  65545. for (i = 0; i < ny; i++) {
  65546. DUK_ASSERT(i < DUK__BI_MAX_PARTS);
  65547. tmp += y->v[i];
  65548. if (i < nz) {
  65549. tmp += z->v[i];
  65550. }
  65551. x->v[i] = (duk_uint32_t) (tmp & 0xffffffffUL);
  65552. tmp = tmp >> 32;
  65553. }
  65554. if (tmp != 0U) {
  65555. DUK_ASSERT(i < DUK__BI_MAX_PARTS);
  65556. x->v[i++] = (duk_uint32_t) tmp;
  65557. }
  65558. x->n = i;
  65559. DUK_ASSERT(x->n <= DUK__BI_MAX_PARTS);
  65560. /* no need to normalize */
  65561. DUK_ASSERT(duk__bi_is_valid(x));
  65562. }
  65563. #else /* DUK_USE_64BIT_OPS */
  65564. DUK_LOCAL void duk__bi_add(duk__bigint *x, duk__bigint *y, duk__bigint *z) {
  65565. duk_uint32_t carry, tmp1, tmp2;
  65566. duk_small_int_t i, ny, nz;
  65567. DUK_ASSERT(duk__bi_is_valid(y));
  65568. DUK_ASSERT(duk__bi_is_valid(z));
  65569. if (z->n > y->n) {
  65570. duk__bigint *t;
  65571. t = y; y = z; z = t;
  65572. }
  65573. DUK_ASSERT(y->n >= z->n);
  65574. ny = y->n; nz = z->n;
  65575. carry = 0U;
  65576. for (i = 0; i < ny; i++) {
  65577. /* Carry is detected based on wrapping which relies on exact 32-bit
  65578. * types.
  65579. */
  65580. DUK_ASSERT(i < DUK__BI_MAX_PARTS);
  65581. tmp1 = y->v[i];
  65582. tmp2 = tmp1;
  65583. if (i < nz) {
  65584. tmp2 += z->v[i];
  65585. }
  65586. /* Careful with carry condition:
  65587. * - If carry not added: 0x12345678 + 0 + 0xffffffff = 0x12345677 (< 0x12345678)
  65588. * - If carry added: 0x12345678 + 1 + 0xffffffff = 0x12345678 (== 0x12345678)
  65589. */
  65590. if (carry) {
  65591. tmp2++;
  65592. carry = (tmp2 <= tmp1 ? 1U : 0U);
  65593. } else {
  65594. carry = (tmp2 < tmp1 ? 1U : 0U);
  65595. }
  65596. x->v[i] = tmp2;
  65597. }
  65598. if (carry) {
  65599. DUK_ASSERT(i < DUK__BI_MAX_PARTS);
  65600. DUK_ASSERT(carry == 1U);
  65601. x->v[i++] = carry;
  65602. }
  65603. x->n = i;
  65604. DUK_ASSERT(x->n <= DUK__BI_MAX_PARTS);
  65605. /* no need to normalize */
  65606. DUK_ASSERT(duk__bi_is_valid(x));
  65607. }
  65608. #endif /* DUK_USE_64BIT_OPS */
  65609. /* x <- y + z */
  65610. DUK_LOCAL void duk__bi_add_small(duk__bigint *x, duk__bigint *y, duk_uint32_t z) {
  65611. duk__bigint tmp;
  65612. DUK_ASSERT(duk__bi_is_valid(y));
  65613. /* XXX: this could be optimized; there is only one call site now though */
  65614. duk__bi_set_small(&tmp, z);
  65615. duk__bi_add(x, y, &tmp);
  65616. DUK_ASSERT(duk__bi_is_valid(x));
  65617. }
  65618. #if 0 /* unused */
  65619. /* x <- x + y, use t as temp */
  65620. DUK_LOCAL void duk__bi_add_copy(duk__bigint *x, duk__bigint *y, duk__bigint *t) {
  65621. duk__bi_add(t, x, y);
  65622. duk__bi_copy(x, t);
  65623. }
  65624. #endif
  65625. /* x <- y - z, require x >= y => z >= 0, i.e. y >= z */
  65626. #ifdef DUK_USE_64BIT_OPS
  65627. DUK_LOCAL void duk__bi_sub(duk__bigint *x, duk__bigint *y, duk__bigint *z) {
  65628. duk_small_int_t i, ny, nz;
  65629. duk_uint32_t ty, tz;
  65630. duk_int64_t tmp;
  65631. DUK_ASSERT(duk__bi_is_valid(y));
  65632. DUK_ASSERT(duk__bi_is_valid(z));
  65633. DUK_ASSERT(duk__bi_compare(y, z) >= 0);
  65634. DUK_ASSERT(y->n >= z->n);
  65635. ny = y->n; nz = z->n;
  65636. tmp = 0;
  65637. for (i = 0; i < ny; i++) {
  65638. ty = y->v[i];
  65639. if (i < nz) {
  65640. tz = z->v[i];
  65641. } else {
  65642. tz = 0;
  65643. }
  65644. tmp = (duk_int64_t) ty - (duk_int64_t) tz + tmp;
  65645. x->v[i] = (duk_uint32_t) (tmp & 0xffffffffUL);
  65646. tmp = tmp >> 32; /* 0 or -1 */
  65647. }
  65648. DUK_ASSERT(tmp == 0);
  65649. x->n = i;
  65650. duk__bi_normalize(x); /* need to normalize, may even cancel to 0 */
  65651. DUK_ASSERT(duk__bi_is_valid(x));
  65652. }
  65653. #else
  65654. DUK_LOCAL void duk__bi_sub(duk__bigint *x, duk__bigint *y, duk__bigint *z) {
  65655. duk_small_int_t i, ny, nz;
  65656. duk_uint32_t tmp1, tmp2, borrow;
  65657. DUK_ASSERT(duk__bi_is_valid(y));
  65658. DUK_ASSERT(duk__bi_is_valid(z));
  65659. DUK_ASSERT(duk__bi_compare(y, z) >= 0);
  65660. DUK_ASSERT(y->n >= z->n);
  65661. ny = y->n; nz = z->n;
  65662. borrow = 0U;
  65663. for (i = 0; i < ny; i++) {
  65664. /* Borrow is detected based on wrapping which relies on exact 32-bit
  65665. * types.
  65666. */
  65667. tmp1 = y->v[i];
  65668. tmp2 = tmp1;
  65669. if (i < nz) {
  65670. tmp2 -= z->v[i];
  65671. }
  65672. /* Careful with borrow condition:
  65673. * - If borrow not subtracted: 0x12345678 - 0 - 0xffffffff = 0x12345679 (> 0x12345678)
  65674. * - If borrow subtracted: 0x12345678 - 1 - 0xffffffff = 0x12345678 (== 0x12345678)
  65675. */
  65676. if (borrow) {
  65677. tmp2--;
  65678. borrow = (tmp2 >= tmp1 ? 1U : 0U);
  65679. } else {
  65680. borrow = (tmp2 > tmp1 ? 1U : 0U);
  65681. }
  65682. x->v[i] = tmp2;
  65683. }
  65684. DUK_ASSERT(borrow == 0U);
  65685. x->n = i;
  65686. duk__bi_normalize(x); /* need to normalize, may even cancel to 0 */
  65687. DUK_ASSERT(duk__bi_is_valid(x));
  65688. }
  65689. #endif
  65690. #if 0 /* unused */
  65691. /* x <- y - z */
  65692. DUK_LOCAL void duk__bi_sub_small(duk__bigint *x, duk__bigint *y, duk_uint32_t z) {
  65693. duk__bigint tmp;
  65694. DUK_ASSERT(duk__bi_is_valid(y));
  65695. /* XXX: this could be optimized */
  65696. duk__bi_set_small(&tmp, z);
  65697. duk__bi_sub(x, y, &tmp);
  65698. DUK_ASSERT(duk__bi_is_valid(x));
  65699. }
  65700. #endif
  65701. /* x <- x - y, use t as temp */
  65702. DUK_LOCAL void duk__bi_sub_copy(duk__bigint *x, duk__bigint *y, duk__bigint *t) {
  65703. duk__bi_sub(t, x, y);
  65704. duk__bi_copy(x, t);
  65705. }
  65706. /* x <- y * z */
  65707. DUK_LOCAL void duk__bi_mul(duk__bigint *x, duk__bigint *y, duk__bigint *z) {
  65708. duk_small_int_t i, j, nx, nz;
  65709. DUK_ASSERT(duk__bi_is_valid(y));
  65710. DUK_ASSERT(duk__bi_is_valid(z));
  65711. nx = y->n + z->n; /* max possible */
  65712. DUK_ASSERT(nx <= DUK__BI_MAX_PARTS);
  65713. if (nx == 0) {
  65714. /* Both inputs are zero; cases where only one is zero can go
  65715. * through main algorithm.
  65716. */
  65717. x->n = 0;
  65718. return;
  65719. }
  65720. DUK_MEMZERO((void *) x->v, (size_t) (sizeof(duk_uint32_t) * nx));
  65721. x->n = nx;
  65722. nz = z->n;
  65723. for (i = 0; i < y->n; i++) {
  65724. #ifdef DUK_USE_64BIT_OPS
  65725. duk_uint64_t tmp = 0U;
  65726. for (j = 0; j < nz; j++) {
  65727. tmp += (duk_uint64_t) y->v[i] * (duk_uint64_t) z->v[j] + x->v[i+j];
  65728. x->v[i+j] = (duk_uint32_t) (tmp & 0xffffffffUL);
  65729. tmp = tmp >> 32;
  65730. }
  65731. if (tmp > 0) {
  65732. DUK_ASSERT(i + j < nx);
  65733. DUK_ASSERT(i + j < DUK__BI_MAX_PARTS);
  65734. DUK_ASSERT(x->v[i+j] == 0U);
  65735. x->v[i+j] = (duk_uint32_t) tmp;
  65736. }
  65737. #else
  65738. /*
  65739. * Multiply + add + carry for 32-bit components using only 16x16->32
  65740. * multiplies and carry detection based on unsigned overflow.
  65741. *
  65742. * 1st mult, 32-bit: (A*2^16 + B)
  65743. * 2nd mult, 32-bit: (C*2^16 + D)
  65744. * 3rd add, 32-bit: E
  65745. * 4th add, 32-bit: F
  65746. *
  65747. * (AC*2^16 + B) * (C*2^16 + D) + E + F
  65748. * = AC*2^32 + AD*2^16 + BC*2^16 + BD + E + F
  65749. * = AC*2^32 + (AD + BC)*2^16 + (BD + E + F)
  65750. * = AC*2^32 + AD*2^16 + BC*2^16 + (BD + E + F)
  65751. */
  65752. duk_uint32_t a, b, c, d, e, f;
  65753. duk_uint32_t r, s, t;
  65754. a = y->v[i]; b = a & 0xffffUL; a = a >> 16;
  65755. f = 0;
  65756. for (j = 0; j < nz; j++) {
  65757. c = z->v[j]; d = c & 0xffffUL; c = c >> 16;
  65758. e = x->v[i+j];
  65759. /* build result as: (r << 32) + s: start with (BD + E + F) */
  65760. r = 0;
  65761. s = b * d;
  65762. /* add E */
  65763. t = s + e;
  65764. if (t < s) { r++; } /* carry */
  65765. s = t;
  65766. /* add F */
  65767. t = s + f;
  65768. if (t < s) { r++; } /* carry */
  65769. s = t;
  65770. /* add BC*2^16 */
  65771. t = b * c;
  65772. r += (t >> 16);
  65773. t = s + ((t & 0xffffUL) << 16);
  65774. if (t < s) { r++; } /* carry */
  65775. s = t;
  65776. /* add AD*2^16 */
  65777. t = a * d;
  65778. r += (t >> 16);
  65779. t = s + ((t & 0xffffUL) << 16);
  65780. if (t < s) { r++; } /* carry */
  65781. s = t;
  65782. /* add AC*2^32 */
  65783. t = a * c;
  65784. r += t;
  65785. DUK_DDD(DUK_DDDPRINT("ab=%08lx cd=%08lx ef=%08lx -> rs=%08lx %08lx",
  65786. (unsigned long) y->v[i], (unsigned long) z->v[j],
  65787. (unsigned long) x->v[i+j], (unsigned long) r,
  65788. (unsigned long) s));
  65789. x->v[i+j] = s;
  65790. f = r;
  65791. }
  65792. if (f > 0U) {
  65793. DUK_ASSERT(i + j < nx);
  65794. DUK_ASSERT(i + j < DUK__BI_MAX_PARTS);
  65795. DUK_ASSERT(x->v[i+j] == 0U);
  65796. x->v[i+j] = (duk_uint32_t) f;
  65797. }
  65798. #endif /* DUK_USE_64BIT_OPS */
  65799. }
  65800. duk__bi_normalize(x);
  65801. DUK_ASSERT(duk__bi_is_valid(x));
  65802. }
  65803. /* x <- y * z */
  65804. DUK_LOCAL void duk__bi_mul_small(duk__bigint *x, duk__bigint *y, duk_uint32_t z) {
  65805. duk__bigint tmp;
  65806. DUK_ASSERT(duk__bi_is_valid(y));
  65807. /* XXX: this could be optimized */
  65808. duk__bi_set_small(&tmp, z);
  65809. duk__bi_mul(x, y, &tmp);
  65810. DUK_ASSERT(duk__bi_is_valid(x));
  65811. }
  65812. /* x <- x * y, use t as temp */
  65813. DUK_LOCAL void duk__bi_mul_copy(duk__bigint *x, duk__bigint *y, duk__bigint *t) {
  65814. duk__bi_mul(t, x, y);
  65815. duk__bi_copy(x, t);
  65816. }
  65817. /* x <- x * y, use t as temp */
  65818. DUK_LOCAL void duk__bi_mul_small_copy(duk__bigint *x, duk_uint32_t y, duk__bigint *t) {
  65819. duk__bi_mul_small(t, x, y);
  65820. duk__bi_copy(x, t);
  65821. }
  65822. DUK_LOCAL int duk__bi_is_even(duk__bigint *x) {
  65823. DUK_ASSERT(duk__bi_is_valid(x));
  65824. return (x->n == 0) || ((x->v[0] & 0x01) == 0);
  65825. }
  65826. DUK_LOCAL int duk__bi_is_zero(duk__bigint *x) {
  65827. DUK_ASSERT(duk__bi_is_valid(x));
  65828. return (x->n == 0); /* this is the case for normalized numbers */
  65829. }
  65830. /* Bigint is 2^52. Used to detect normalized IEEE double mantissa values
  65831. * which are at the lowest edge (next floating point value downwards has
  65832. * a different exponent). The lowest mantissa has the form:
  65833. *
  65834. * 1000........000 (52 zeroes; only "hidden bit" is set)
  65835. */
  65836. DUK_LOCAL duk_small_int_t duk__bi_is_2to52(duk__bigint *x) {
  65837. DUK_ASSERT(duk__bi_is_valid(x));
  65838. return (duk_small_int_t)
  65839. (x->n == 2) && (x->v[0] == 0U) && (x->v[1] == (1U << (52-32)));
  65840. }
  65841. /* x <- (1<<y) */
  65842. DUK_LOCAL void duk__bi_twoexp(duk__bigint *x, duk_small_int_t y) {
  65843. duk_small_int_t n, r;
  65844. n = (y / 32) + 1;
  65845. DUK_ASSERT(n > 0);
  65846. r = y % 32;
  65847. DUK_MEMZERO((void *) x->v, sizeof(duk_uint32_t) * n);
  65848. x->n = n;
  65849. x->v[n - 1] = (((duk_uint32_t) 1) << r);
  65850. }
  65851. /* x <- b^y; use t1 and t2 as temps */
  65852. 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) {
  65853. /* Fast path the binary case */
  65854. DUK_ASSERT(x != t1 && x != t2 && t1 != t2); /* distinct bignums, easy mistake to make */
  65855. DUK_ASSERT(b >= 0);
  65856. DUK_ASSERT(y >= 0);
  65857. if (b == 2) {
  65858. duk__bi_twoexp(x, y);
  65859. return;
  65860. }
  65861. /* http://en.wikipedia.org/wiki/Exponentiation_by_squaring */
  65862. DUK_DDD(DUK_DDDPRINT("exp_small: b=%ld, y=%ld", (long) b, (long) y));
  65863. duk__bi_set_small(x, 1);
  65864. duk__bi_set_small(t1, b);
  65865. for (;;) {
  65866. /* Loop structure ensures that we don't compute t1^2 unnecessarily
  65867. * on the final round, as that might create a bignum exceeding the
  65868. * current DUK__BI_MAX_PARTS limit.
  65869. */
  65870. if (y & 0x01) {
  65871. duk__bi_mul_copy(x, t1, t2);
  65872. }
  65873. y = y >> 1;
  65874. if (y == 0) {
  65875. break;
  65876. }
  65877. duk__bi_mul_copy(t1, t1, t2);
  65878. }
  65879. DUK__BI_PRINT("exp_small result", x);
  65880. }
  65881. /*
  65882. * A Dragon4 number-to-string variant, based on:
  65883. *
  65884. * Guy L. Steele Jr., Jon L. White: "How to Print Floating-Point Numbers
  65885. * Accurately"
  65886. *
  65887. * Robert G. Burger, R. Kent Dybvig: "Printing Floating-Point Numbers
  65888. * Quickly and Accurately"
  65889. *
  65890. * The current algorithm is based on Figure 1 of the Burger-Dybvig paper,
  65891. * i.e. the base implementation without logarithm estimation speedups
  65892. * (these would increase code footprint considerably). Fixed-format output
  65893. * does not follow the suggestions in the paper; instead, we generate an
  65894. * extra digit and round-with-carry.
  65895. *
  65896. * The same algorithm is used for number parsing (with b=10 and B=2)
  65897. * by generating one extra digit and doing rounding manually.
  65898. *
  65899. * See doc/number-conversion.rst for limitations.
  65900. */
  65901. /* Maximum number of digits generated. */
  65902. #define DUK__MAX_OUTPUT_DIGITS 1040 /* (Number.MAX_VALUE).toString(2).length == 1024, + spare */
  65903. /* Maximum number of characters in formatted value. */
  65904. #define DUK__MAX_FORMATTED_LENGTH 1040 /* (-Number.MAX_VALUE).toString(2).length == 1025, + spare */
  65905. /* Number and (minimum) size of bigints in the nc_ctx structure. */
  65906. #define DUK__NUMCONV_CTX_NUM_BIGINTS 7
  65907. #define DUK__NUMCONV_CTX_BIGINTS_SIZE (sizeof(duk__bigint) * DUK__NUMCONV_CTX_NUM_BIGINTS)
  65908. typedef struct {
  65909. /* Currently about 7*152 = 1064 bytes. The space for these
  65910. * duk__bigints is used also as a temporary buffer for generating
  65911. * the final string. This is a bit awkard; a union would be
  65912. * more correct.
  65913. */
  65914. duk__bigint f, r, s, mp, mm, t1, t2;
  65915. duk_small_int_t is_s2n; /* if 1, doing a string-to-number; else doing a number-to-string */
  65916. duk_small_int_t is_fixed; /* if 1, doing a fixed format output (not free format) */
  65917. duk_small_int_t req_digits; /* requested number of output digits; 0 = free-format */
  65918. duk_small_int_t abs_pos; /* digit position is absolute, not relative */
  65919. duk_small_int_t e; /* exponent for 'f' */
  65920. duk_small_int_t b; /* input radix */
  65921. duk_small_int_t B; /* output radix */
  65922. duk_small_int_t k; /* see algorithm */
  65923. duk_small_int_t low_ok; /* see algorithm */
  65924. duk_small_int_t high_ok; /* see algorithm */
  65925. duk_small_int_t unequal_gaps; /* m+ != m- (very rarely) */
  65926. /* Buffer used for generated digits, values are in the range [0,B-1]. */
  65927. duk_uint8_t digits[DUK__MAX_OUTPUT_DIGITS];
  65928. duk_small_int_t count; /* digit count */
  65929. } duk__numconv_stringify_ctx;
  65930. /* Note: computes with 'idx' in assertions, so caller beware.
  65931. * 'idx' is preincremented, i.e. '1' on first call, because it
  65932. * is more convenient for the caller.
  65933. */
  65934. #define DUK__DRAGON4_OUTPUT_PREINC(nc_ctx,preinc_idx,x) do { \
  65935. DUK_ASSERT((preinc_idx) - 1 >= 0); \
  65936. DUK_ASSERT((preinc_idx) - 1 < DUK__MAX_OUTPUT_DIGITS); \
  65937. ((nc_ctx)->digits[(preinc_idx) - 1]) = (duk_uint8_t) (x); \
  65938. } while (0)
  65939. DUK_LOCAL duk_size_t duk__dragon4_format_uint32(duk_uint8_t *buf, duk_uint32_t x, duk_small_int_t radix) {
  65940. duk_uint8_t *p;
  65941. duk_size_t len;
  65942. duk_small_int_t dig;
  65943. duk_small_int_t t;
  65944. DUK_ASSERT(radix >= 2 && radix <= 36);
  65945. /* A 32-bit unsigned integer formats to at most 32 digits (the
  65946. * worst case happens with radix == 2). Output the digits backwards,
  65947. * and use a memmove() to get them in the right place.
  65948. */
  65949. p = buf + 32;
  65950. for (;;) {
  65951. t = x / radix;
  65952. dig = x - t * radix;
  65953. x = t;
  65954. DUK_ASSERT(dig >= 0 && dig < 36);
  65955. *(--p) = DUK__DIGITCHAR(dig);
  65956. if (x == 0) {
  65957. break;
  65958. }
  65959. }
  65960. len = (duk_size_t) ((buf + 32) - p);
  65961. DUK_MEMMOVE((void *) buf, (const void *) p, (size_t) len);
  65962. return len;
  65963. }
  65964. DUK_LOCAL void duk__dragon4_prepare(duk__numconv_stringify_ctx *nc_ctx) {
  65965. duk_small_int_t lowest_mantissa;
  65966. #if 1
  65967. /* Assume IEEE round-to-even, so that shorter encoding can be used
  65968. * when round-to-even would produce correct result. By removing
  65969. * this check (and having low_ok == high_ok == 0) the results would
  65970. * still be accurate but in some cases longer than necessary.
  65971. */
  65972. if (duk__bi_is_even(&nc_ctx->f)) {
  65973. DUK_DDD(DUK_DDDPRINT("f is even"));
  65974. nc_ctx->low_ok = 1;
  65975. nc_ctx->high_ok = 1;
  65976. } else {
  65977. DUK_DDD(DUK_DDDPRINT("f is odd"));
  65978. nc_ctx->low_ok = 0;
  65979. nc_ctx->high_ok = 0;
  65980. }
  65981. #else
  65982. /* Note: not honoring round-to-even should work but now generates incorrect
  65983. * results. For instance, 1e23 serializes to "a000...", i.e. the first digit
  65984. * equals the radix (10). Scaling stops one step too early in this case.
  65985. * Don't know why this is the case, but since this code path is unused, it
  65986. * doesn't matter.
  65987. */
  65988. nc_ctx->low_ok = 0;
  65989. nc_ctx->high_ok = 0;
  65990. #endif
  65991. /* For string-to-number, pretend we never have the lowest mantissa as there
  65992. * is no natural "precision" for inputs. Having lowest_mantissa == 0, we'll
  65993. * fall into the base cases for both e >= 0 and e < 0.
  65994. */
  65995. if (nc_ctx->is_s2n) {
  65996. lowest_mantissa = 0;
  65997. } else {
  65998. lowest_mantissa = duk__bi_is_2to52(&nc_ctx->f);
  65999. }
  66000. nc_ctx->unequal_gaps = 0;
  66001. if (nc_ctx->e >= 0) {
  66002. /* exponent non-negative (and thus not minimum exponent) */
  66003. if (lowest_mantissa) {
  66004. /* (>= e 0) AND (= f (expt b (- p 1)))
  66005. *
  66006. * be <- (expt b e) == b^e
  66007. * be1 <- (* be b) == (expt b (+ e 1)) == b^(e+1)
  66008. * r <- (* f be1 2) == 2 * f * b^(e+1) [if b==2 -> f * b^(e+2)]
  66009. * s <- (* b 2) [if b==2 -> 4]
  66010. * m+ <- be1 == b^(e+1)
  66011. * m- <- be == b^e
  66012. * k <- 0
  66013. * B <- B
  66014. * low_ok <- round
  66015. * high_ok <- round
  66016. */
  66017. DUK_DDD(DUK_DDDPRINT("non-negative exponent (not smallest exponent); "
  66018. "lowest mantissa value for this exponent -> "
  66019. "unequal gaps"));
  66020. duk__bi_exp_small(&nc_ctx->mm, nc_ctx->b, nc_ctx->e, &nc_ctx->t1, &nc_ctx->t2); /* mm <- b^e */
  66021. duk__bi_mul_small(&nc_ctx->mp, &nc_ctx->mm, nc_ctx->b); /* mp <- b^(e+1) */
  66022. duk__bi_mul_small(&nc_ctx->t1, &nc_ctx->f, 2);
  66023. duk__bi_mul(&nc_ctx->r, &nc_ctx->t1, &nc_ctx->mp); /* r <- (2 * f) * b^(e+1) */
  66024. duk__bi_set_small(&nc_ctx->s, nc_ctx->b * 2); /* s <- 2 * b */
  66025. nc_ctx->unequal_gaps = 1;
  66026. } else {
  66027. /* (>= e 0) AND (not (= f (expt b (- p 1))))
  66028. *
  66029. * be <- (expt b e) == b^e
  66030. * r <- (* f be 2) == 2 * f * b^e [if b==2 -> f * b^(e+1)]
  66031. * s <- 2
  66032. * m+ <- be == b^e
  66033. * m- <- be == b^e
  66034. * k <- 0
  66035. * B <- B
  66036. * low_ok <- round
  66037. * high_ok <- round
  66038. */
  66039. DUK_DDD(DUK_DDDPRINT("non-negative exponent (not smallest exponent); "
  66040. "not lowest mantissa for this exponent -> "
  66041. "equal gaps"));
  66042. duk__bi_exp_small(&nc_ctx->mm, nc_ctx->b, nc_ctx->e, &nc_ctx->t1, &nc_ctx->t2); /* mm <- b^e */
  66043. duk__bi_copy(&nc_ctx->mp, &nc_ctx->mm); /* mp <- b^e */
  66044. duk__bi_mul_small(&nc_ctx->t1, &nc_ctx->f, 2);
  66045. duk__bi_mul(&nc_ctx->r, &nc_ctx->t1, &nc_ctx->mp); /* r <- (2 * f) * b^e */
  66046. duk__bi_set_small(&nc_ctx->s, 2); /* s <- 2 */
  66047. }
  66048. } else {
  66049. /* When doing string-to-number, lowest_mantissa is always 0 so
  66050. * the exponent check, while incorrect, won't matter.
  66051. */
  66052. if (nc_ctx->e > DUK__IEEE_DOUBLE_EXP_MIN /*not minimum exponent*/ &&
  66053. lowest_mantissa /* lowest mantissa for this exponent*/) {
  66054. /* r <- (* f b 2) [if b==2 -> (* f 4)]
  66055. * s <- (* (expt b (- 1 e)) 2) == b^(1-e) * 2 [if b==2 -> b^(2-e)]
  66056. * m+ <- b == 2
  66057. * m- <- 1
  66058. * k <- 0
  66059. * B <- B
  66060. * low_ok <- round
  66061. * high_ok <- round
  66062. */
  66063. DUK_DDD(DUK_DDDPRINT("negative exponent; not minimum exponent and "
  66064. "lowest mantissa for this exponent -> "
  66065. "unequal gaps"));
  66066. duk__bi_mul_small(&nc_ctx->r, &nc_ctx->f, nc_ctx->b * 2); /* r <- (2 * b) * f */
  66067. 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 */
  66068. duk__bi_mul_small(&nc_ctx->s, &nc_ctx->t1, 2); /* s <- b^(1-e) * 2 */
  66069. duk__bi_set_small(&nc_ctx->mp, 2);
  66070. duk__bi_set_small(&nc_ctx->mm, 1);
  66071. nc_ctx->unequal_gaps = 1;
  66072. } else {
  66073. /* r <- (* f 2)
  66074. * s <- (* (expt b (- e)) 2) == b^(-e) * 2 [if b==2 -> b^(1-e)]
  66075. * m+ <- 1
  66076. * m- <- 1
  66077. * k <- 0
  66078. * B <- B
  66079. * low_ok <- round
  66080. * high_ok <- round
  66081. */
  66082. DUK_DDD(DUK_DDDPRINT("negative exponent; minimum exponent or not "
  66083. "lowest mantissa for this exponent -> "
  66084. "equal gaps"));
  66085. duk__bi_mul_small(&nc_ctx->r, &nc_ctx->f, 2); /* r <- 2 * f */
  66086. 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 */
  66087. duk__bi_mul_small(&nc_ctx->s, &nc_ctx->t1, 2); /* s <- b^(-e) * 2 */
  66088. duk__bi_set_small(&nc_ctx->mp, 1);
  66089. duk__bi_set_small(&nc_ctx->mm, 1);
  66090. }
  66091. }
  66092. }
  66093. DUK_LOCAL void duk__dragon4_scale(duk__numconv_stringify_ctx *nc_ctx) {
  66094. duk_small_int_t k = 0;
  66095. /* This is essentially the 'scale' algorithm, with recursion removed.
  66096. * Note that 'k' is either correct immediately, or will move in one
  66097. * direction in the loop. There's no need to do the low/high checks
  66098. * on every round (like the Scheme algorithm does).
  66099. *
  66100. * The scheme algorithm finds 'k' and updates 's' simultaneously,
  66101. * while the logical algorithm finds 'k' with 's' having its initial
  66102. * value, after which 's' is updated separately (see the Burger-Dybvig
  66103. * paper, Section 3.1, steps 2 and 3).
  66104. *
  66105. * The case where m+ == m- (almost always) is optimized for, because
  66106. * it reduces the bigint operations considerably and almost always
  66107. * applies. The scale loop only needs to work with m+, so this works.
  66108. */
  66109. /* XXX: this algorithm could be optimized quite a lot by using e.g.
  66110. * a logarithm based estimator for 'k' and performing B^n multiplication
  66111. * using a lookup table or using some bit-representation based exp
  66112. * algorithm. Currently we just loop, with significant performance
  66113. * impact for very large and very small numbers.
  66114. */
  66115. DUK_DDD(DUK_DDDPRINT("scale: B=%ld, low_ok=%ld, high_ok=%ld",
  66116. (long) nc_ctx->B, (long) nc_ctx->low_ok, (long) nc_ctx->high_ok));
  66117. DUK__BI_PRINT("r(init)", &nc_ctx->r);
  66118. DUK__BI_PRINT("s(init)", &nc_ctx->s);
  66119. DUK__BI_PRINT("mp(init)", &nc_ctx->mp);
  66120. DUK__BI_PRINT("mm(init)", &nc_ctx->mm);
  66121. for (;;) {
  66122. DUK_DDD(DUK_DDDPRINT("scale loop (inc k), k=%ld", (long) k));
  66123. DUK__BI_PRINT("r", &nc_ctx->r);
  66124. DUK__BI_PRINT("s", &nc_ctx->s);
  66125. DUK__BI_PRINT("m+", &nc_ctx->mp);
  66126. DUK__BI_PRINT("m-", &nc_ctx->mm);
  66127. duk__bi_add(&nc_ctx->t1, &nc_ctx->r, &nc_ctx->mp); /* t1 = (+ r m+) */
  66128. if (duk__bi_compare(&nc_ctx->t1, &nc_ctx->s) >= (nc_ctx->high_ok ? 0 : 1)) {
  66129. DUK_DDD(DUK_DDDPRINT("k is too low"));
  66130. /* r <- r
  66131. * s <- (* s B)
  66132. * m+ <- m+
  66133. * m- <- m-
  66134. * k <- (+ k 1)
  66135. */
  66136. duk__bi_mul_small_copy(&nc_ctx->s, nc_ctx->B, &nc_ctx->t1);
  66137. k++;
  66138. } else {
  66139. break;
  66140. }
  66141. }
  66142. /* k > 0 -> k was too low, and cannot be too high */
  66143. if (k > 0) {
  66144. goto skip_dec_k;
  66145. }
  66146. for (;;) {
  66147. DUK_DDD(DUK_DDDPRINT("scale loop (dec k), k=%ld", (long) k));
  66148. DUK__BI_PRINT("r", &nc_ctx->r);
  66149. DUK__BI_PRINT("s", &nc_ctx->s);
  66150. DUK__BI_PRINT("m+", &nc_ctx->mp);
  66151. DUK__BI_PRINT("m-", &nc_ctx->mm);
  66152. duk__bi_add(&nc_ctx->t1, &nc_ctx->r, &nc_ctx->mp); /* t1 = (+ r m+) */
  66153. duk__bi_mul_small(&nc_ctx->t2, &nc_ctx->t1, nc_ctx->B); /* t2 = (* (+ r m+) B) */
  66154. if (duk__bi_compare(&nc_ctx->t2, &nc_ctx->s) <= (nc_ctx->high_ok ? -1 : 0)) {
  66155. DUK_DDD(DUK_DDDPRINT("k is too high"));
  66156. /* r <- (* r B)
  66157. * s <- s
  66158. * m+ <- (* m+ B)
  66159. * m- <- (* m- B)
  66160. * k <- (- k 1)
  66161. */
  66162. duk__bi_mul_small_copy(&nc_ctx->r, nc_ctx->B, &nc_ctx->t1);
  66163. duk__bi_mul_small_copy(&nc_ctx->mp, nc_ctx->B, &nc_ctx->t1);
  66164. if (nc_ctx->unequal_gaps) {
  66165. DUK_DDD(DUK_DDDPRINT("m+ != m- -> need to update m- too"));
  66166. duk__bi_mul_small_copy(&nc_ctx->mm, nc_ctx->B, &nc_ctx->t1);
  66167. }
  66168. k--;
  66169. } else {
  66170. break;
  66171. }
  66172. }
  66173. skip_dec_k:
  66174. if (!nc_ctx->unequal_gaps) {
  66175. DUK_DDD(DUK_DDDPRINT("equal gaps, copy m- from m+"));
  66176. duk__bi_copy(&nc_ctx->mm, &nc_ctx->mp); /* mm <- mp */
  66177. }
  66178. nc_ctx->k = k;
  66179. DUK_DDD(DUK_DDDPRINT("final k: %ld", (long) k));
  66180. DUK__BI_PRINT("r(final)", &nc_ctx->r);
  66181. DUK__BI_PRINT("s(final)", &nc_ctx->s);
  66182. DUK__BI_PRINT("mp(final)", &nc_ctx->mp);
  66183. DUK__BI_PRINT("mm(final)", &nc_ctx->mm);
  66184. }
  66185. DUK_LOCAL void duk__dragon4_generate(duk__numconv_stringify_ctx *nc_ctx) {
  66186. duk_small_int_t tc1, tc2; /* terminating conditions */
  66187. duk_small_int_t d; /* current digit */
  66188. duk_small_int_t count = 0; /* digit count */
  66189. /*
  66190. * Digit generation loop.
  66191. *
  66192. * Different termination conditions:
  66193. *
  66194. * 1. Free format output. Terminate when shortest accurate
  66195. * representation found.
  66196. *
  66197. * 2. Fixed format output, with specific number of digits.
  66198. * Ignore termination conditions, terminate when digits
  66199. * generated. Caller requests an extra digit and rounds.
  66200. *
  66201. * 3. Fixed format output, with a specific absolute cut-off
  66202. * position (e.g. 10 digits after decimal point). Note
  66203. * that we always generate at least one digit, even if
  66204. * the digit is below the cut-off point already.
  66205. */
  66206. for (;;) {
  66207. DUK_DDD(DUK_DDDPRINT("generate loop, count=%ld, k=%ld, B=%ld, low_ok=%ld, high_ok=%ld",
  66208. (long) count, (long) nc_ctx->k, (long) nc_ctx->B,
  66209. (long) nc_ctx->low_ok, (long) nc_ctx->high_ok));
  66210. DUK__BI_PRINT("r", &nc_ctx->r);
  66211. DUK__BI_PRINT("s", &nc_ctx->s);
  66212. DUK__BI_PRINT("m+", &nc_ctx->mp);
  66213. DUK__BI_PRINT("m-", &nc_ctx->mm);
  66214. /* (quotient-remainder (* r B) s) using a dummy subtraction loop */
  66215. duk__bi_mul_small(&nc_ctx->t1, &nc_ctx->r, nc_ctx->B); /* t1 <- (* r B) */
  66216. d = 0;
  66217. for (;;) {
  66218. if (duk__bi_compare(&nc_ctx->t1, &nc_ctx->s) < 0) {
  66219. break;
  66220. }
  66221. duk__bi_sub_copy(&nc_ctx->t1, &nc_ctx->s, &nc_ctx->t2); /* t1 <- t1 - s */
  66222. d++;
  66223. }
  66224. duk__bi_copy(&nc_ctx->r, &nc_ctx->t1); /* r <- (remainder (* r B) s) */
  66225. /* d <- (quotient (* r B) s) (in range 0...B-1) */
  66226. DUK_DDD(DUK_DDDPRINT("-> d(quot)=%ld", (long) d));
  66227. DUK__BI_PRINT("r(rem)", &nc_ctx->r);
  66228. duk__bi_mul_small_copy(&nc_ctx->mp, nc_ctx->B, &nc_ctx->t2); /* m+ <- (* m+ B) */
  66229. duk__bi_mul_small_copy(&nc_ctx->mm, nc_ctx->B, &nc_ctx->t2); /* m- <- (* m- B) */
  66230. DUK__BI_PRINT("mp(upd)", &nc_ctx->mp);
  66231. DUK__BI_PRINT("mm(upd)", &nc_ctx->mm);
  66232. /* Terminating conditions. For fixed width output, we just ignore the
  66233. * terminating conditions (and pretend that tc1 == tc2 == false). The
  66234. * the current shortcut for fixed-format output is to generate a few
  66235. * extra digits and use rounding (with carry) to finish the output.
  66236. */
  66237. if (nc_ctx->is_fixed == 0) {
  66238. /* free-form */
  66239. tc1 = (duk__bi_compare(&nc_ctx->r, &nc_ctx->mm) <= (nc_ctx->low_ok ? 0 : -1));
  66240. duk__bi_add(&nc_ctx->t1, &nc_ctx->r, &nc_ctx->mp); /* t1 <- (+ r m+) */
  66241. tc2 = (duk__bi_compare(&nc_ctx->t1, &nc_ctx->s) >= (nc_ctx->high_ok ? 0 : 1));
  66242. DUK_DDD(DUK_DDDPRINT("tc1=%ld, tc2=%ld", (long) tc1, (long) tc2));
  66243. } else {
  66244. /* fixed-format */
  66245. tc1 = 0;
  66246. tc2 = 0;
  66247. }
  66248. /* Count is incremented before DUK__DRAGON4_OUTPUT_PREINC() call
  66249. * on purpose, which is taken into account by the macro.
  66250. */
  66251. count++;
  66252. if (tc1) {
  66253. if (tc2) {
  66254. /* tc1 = true, tc2 = true */
  66255. duk__bi_mul_small(&nc_ctx->t1, &nc_ctx->r, 2);
  66256. if (duk__bi_compare(&nc_ctx->t1, &nc_ctx->s) < 0) { /* (< (* r 2) s) */
  66257. DUK_DDD(DUK_DDDPRINT("tc1=true, tc2=true, 2r > s: output d --> %ld (k=%ld)",
  66258. (long) d, (long) nc_ctx->k));
  66259. DUK__DRAGON4_OUTPUT_PREINC(nc_ctx, count, d);
  66260. } else {
  66261. DUK_DDD(DUK_DDDPRINT("tc1=true, tc2=true, 2r <= s: output d+1 --> %ld (k=%ld)",
  66262. (long) (d + 1), (long) nc_ctx->k));
  66263. DUK__DRAGON4_OUTPUT_PREINC(nc_ctx, count, d + 1);
  66264. }
  66265. break;
  66266. } else {
  66267. /* tc1 = true, tc2 = false */
  66268. DUK_DDD(DUK_DDDPRINT("tc1=true, tc2=false: output d --> %ld (k=%ld)",
  66269. (long) d, (long) nc_ctx->k));
  66270. DUK__DRAGON4_OUTPUT_PREINC(nc_ctx, count, d);
  66271. break;
  66272. }
  66273. } else {
  66274. if (tc2) {
  66275. /* tc1 = false, tc2 = true */
  66276. DUK_DDD(DUK_DDDPRINT("tc1=false, tc2=true: output d+1 --> %ld (k=%ld)",
  66277. (long) (d + 1), (long) nc_ctx->k));
  66278. DUK__DRAGON4_OUTPUT_PREINC(nc_ctx, count, d + 1);
  66279. break;
  66280. } else {
  66281. /* tc1 = false, tc2 = false */
  66282. DUK_DDD(DUK_DDDPRINT("tc1=false, tc2=false: output d --> %ld (k=%ld)",
  66283. (long) d, (long) nc_ctx->k));
  66284. DUK__DRAGON4_OUTPUT_PREINC(nc_ctx, count, d);
  66285. /* r <- r (updated above: r <- (remainder (* r B) s)
  66286. * s <- s
  66287. * m+ <- m+ (updated above: m+ <- (* m+ B)
  66288. * m- <- m- (updated above: m- <- (* m- B)
  66289. * B, low_ok, high_ok are fixed
  66290. */
  66291. /* fall through and continue for-loop */
  66292. }
  66293. }
  66294. /* fixed-format termination conditions */
  66295. if (nc_ctx->is_fixed) {
  66296. if (nc_ctx->abs_pos) {
  66297. int pos = nc_ctx->k - count + 1; /* count is already incremented, take into account */
  66298. DUK_DDD(DUK_DDDPRINT("fixed format, absolute: abs pos=%ld, k=%ld, count=%ld, req=%ld",
  66299. (long) pos, (long) nc_ctx->k, (long) count, (long) nc_ctx->req_digits));
  66300. if (pos <= nc_ctx->req_digits) {
  66301. DUK_DDD(DUK_DDDPRINT("digit position reached req_digits, end generate loop"));
  66302. break;
  66303. }
  66304. } else {
  66305. DUK_DDD(DUK_DDDPRINT("fixed format, relative: k=%ld, count=%ld, req=%ld",
  66306. (long) nc_ctx->k, (long) count, (long) nc_ctx->req_digits));
  66307. if (count >= nc_ctx->req_digits) {
  66308. DUK_DDD(DUK_DDDPRINT("digit count reached req_digits, end generate loop"));
  66309. break;
  66310. }
  66311. }
  66312. }
  66313. } /* for */
  66314. nc_ctx->count = count;
  66315. DUK_DDD(DUK_DDDPRINT("generate finished"));
  66316. #ifdef DUK_USE_DDDPRINT
  66317. {
  66318. duk_uint8_t buf[2048];
  66319. duk_small_int_t i, t;
  66320. DUK_MEMZERO(buf, sizeof(buf));
  66321. for (i = 0; i < nc_ctx->count; i++) {
  66322. t = nc_ctx->digits[i];
  66323. if (t < 0 || t > 36) {
  66324. buf[i] = (duk_uint8_t) '?';
  66325. } else {
  66326. buf[i] = (duk_uint8_t) DUK__DIGITCHAR(t);
  66327. }
  66328. }
  66329. DUK_DDD(DUK_DDDPRINT("-> generated digits; k=%ld, digits='%s'",
  66330. (long) nc_ctx->k, (const char *) buf));
  66331. }
  66332. #endif
  66333. }
  66334. /* Round up digits to a given position. If position is out-of-bounds,
  66335. * does nothing. If carry propagates over the first digit, a '1' is
  66336. * prepended to digits and 'k' will be updated. Return value indicates
  66337. * whether carry propagated over the first digit.
  66338. *
  66339. * Note that nc_ctx->count is NOT updated based on the rounding position
  66340. * (it is updated only if carry overflows over the first digit and an
  66341. * extra digit is prepended).
  66342. */
  66343. DUK_LOCAL duk_small_int_t duk__dragon4_fixed_format_round(duk__numconv_stringify_ctx *nc_ctx, duk_small_int_t round_idx) {
  66344. duk_small_int_t t;
  66345. duk_uint8_t *p;
  66346. duk_uint8_t roundup_limit;
  66347. duk_small_int_t ret = 0;
  66348. /*
  66349. * round_idx points to the digit which is considered for rounding; the
  66350. * digit to its left is the final digit of the rounded value. If round_idx
  66351. * is zero, rounding will be performed; the result will either be an empty
  66352. * rounded value or if carry happens a '1' digit is generated.
  66353. */
  66354. if (round_idx >= nc_ctx->count) {
  66355. DUK_DDD(DUK_DDDPRINT("round_idx out of bounds (%ld >= %ld (count)) -> no rounding",
  66356. (long) round_idx, (long) nc_ctx->count));
  66357. return 0;
  66358. } else if (round_idx < 0) {
  66359. DUK_DDD(DUK_DDDPRINT("round_idx out of bounds (%ld < 0) -> no rounding",
  66360. (long) round_idx));
  66361. return 0;
  66362. }
  66363. /*
  66364. * Round-up limit.
  66365. *
  66366. * For even values, divides evenly, e.g. 10 -> roundup_limit=5.
  66367. *
  66368. * For odd values, rounds up, e.g. 3 -> roundup_limit=2.
  66369. * If radix is 3, 0/3 -> down, 1/3 -> down, 2/3 -> up.
  66370. */
  66371. roundup_limit = (duk_uint8_t) ((nc_ctx->B + 1) / 2);
  66372. p = &nc_ctx->digits[round_idx];
  66373. if (*p >= roundup_limit) {
  66374. DUK_DDD(DUK_DDDPRINT("fixed-format rounding carry required"));
  66375. /* carry */
  66376. for (;;) {
  66377. *p = 0;
  66378. if (p == &nc_ctx->digits[0]) {
  66379. DUK_DDD(DUK_DDDPRINT("carry propagated to first digit -> special case handling"));
  66380. DUK_MEMMOVE((void *) (&nc_ctx->digits[1]),
  66381. (const void *) (&nc_ctx->digits[0]),
  66382. (size_t) (sizeof(char) * nc_ctx->count));
  66383. nc_ctx->digits[0] = 1; /* don't increase 'count' */
  66384. nc_ctx->k++; /* position of highest digit changed */
  66385. nc_ctx->count++; /* number of digits changed */
  66386. ret = 1;
  66387. break;
  66388. }
  66389. DUK_DDD(DUK_DDDPRINT("fixed-format rounding carry: B=%ld, roundup_limit=%ld, p=%p, digits=%p",
  66390. (long) nc_ctx->B, (long) roundup_limit, (void *) p, (void *) nc_ctx->digits));
  66391. p--;
  66392. t = *p;
  66393. DUK_DDD(DUK_DDDPRINT("digit before carry: %ld", (long) t));
  66394. if (++t < nc_ctx->B) {
  66395. DUK_DDD(DUK_DDDPRINT("rounding carry terminated"));
  66396. *p = t;
  66397. break;
  66398. }
  66399. DUK_DDD(DUK_DDDPRINT("wraps, carry to next digit"));
  66400. }
  66401. }
  66402. return ret;
  66403. }
  66404. #define DUK__NO_EXP (65536) /* arbitrary marker, outside valid exp range */
  66405. DUK_LOCAL void duk__dragon4_convert_and_push(duk__numconv_stringify_ctx *nc_ctx,
  66406. duk_context *ctx,
  66407. duk_small_int_t radix,
  66408. duk_small_int_t digits,
  66409. duk_small_uint_t flags,
  66410. duk_small_int_t neg) {
  66411. duk_small_int_t k;
  66412. duk_small_int_t pos, pos_end;
  66413. duk_small_int_t expt;
  66414. duk_small_int_t dig;
  66415. duk_uint8_t *q;
  66416. duk_uint8_t *buf;
  66417. /*
  66418. * The string conversion here incorporates all the necessary Ecmascript
  66419. * semantics without attempting to be generic. nc_ctx->digits contains
  66420. * nc_ctx->count digits (>= 1), with the topmost digit's 'position'
  66421. * indicated by nc_ctx->k as follows:
  66422. *
  66423. * digits="123" count=3 k=0 --> 0.123
  66424. * digits="123" count=3 k=1 --> 1.23
  66425. * digits="123" count=3 k=5 --> 12300
  66426. * digits="123" count=3 k=-1 --> 0.0123
  66427. *
  66428. * Note that the identifier names used for format selection are different
  66429. * in Burger-Dybvig paper and Ecmascript specification (quite confusingly
  66430. * so, because e.g. 'k' has a totally different meaning in each). See
  66431. * documentation for discussion.
  66432. *
  66433. * Ecmascript doesn't specify any specific behavior for format selection
  66434. * (e.g. when to use exponent notation) for non-base-10 numbers.
  66435. *
  66436. * The bigint space in the context is reused for string output, as there
  66437. * is more than enough space for that (>1kB at the moment), and we avoid
  66438. * allocating even more stack.
  66439. */
  66440. DUK_ASSERT(DUK__NUMCONV_CTX_BIGINTS_SIZE >= DUK__MAX_FORMATTED_LENGTH);
  66441. DUK_ASSERT(nc_ctx->count >= 1);
  66442. k = nc_ctx->k;
  66443. buf = (duk_uint8_t *) &nc_ctx->f; /* XXX: union would be more correct */
  66444. q = buf;
  66445. /* Exponent handling: if exponent format is used, record exponent value and
  66446. * fake k such that one leading digit is generated (e.g. digits=123 -> "1.23").
  66447. *
  66448. * toFixed() prevents exponent use; otherwise apply a set of criteria to
  66449. * match the other API calls (toString(), toPrecision, etc).
  66450. */
  66451. expt = DUK__NO_EXP;
  66452. if (!nc_ctx->abs_pos /* toFixed() */) {
  66453. if ((flags & DUK_N2S_FLAG_FORCE_EXP) || /* exponential notation forced */
  66454. ((flags & DUK_N2S_FLAG_NO_ZERO_PAD) && /* fixed precision and zero padding would be required */
  66455. (k - digits >= 1)) || /* (e.g. k=3, digits=2 -> "12X") */
  66456. ((k > 21 || k <= -6) && (radix == 10))) { /* toString() conditions */
  66457. DUK_DDD(DUK_DDDPRINT("use exponential notation: k=%ld -> expt=%ld",
  66458. (long) k, (long) (k - 1)));
  66459. expt = k - 1; /* e.g. 12.3 -> digits="123" k=2 -> 1.23e1 */
  66460. k = 1; /* generate mantissa with a single leading whole number digit */
  66461. }
  66462. }
  66463. if (neg) {
  66464. *q++ = '-';
  66465. }
  66466. /* Start position (inclusive) and end position (exclusive) */
  66467. pos = (k >= 1 ? k : 1);
  66468. if (nc_ctx->is_fixed) {
  66469. if (nc_ctx->abs_pos) {
  66470. /* toFixed() */
  66471. pos_end = -digits;
  66472. } else {
  66473. pos_end = k - digits;
  66474. }
  66475. } else {
  66476. pos_end = k - nc_ctx->count;
  66477. }
  66478. if (pos_end > 0) {
  66479. pos_end = 0;
  66480. }
  66481. DUK_DDD(DUK_DDDPRINT("expt=%ld, k=%ld, count=%ld, pos=%ld, pos_end=%ld, is_fixed=%ld, "
  66482. "digits=%ld, abs_pos=%ld",
  66483. (long) expt, (long) k, (long) nc_ctx->count, (long) pos, (long) pos_end,
  66484. (long) nc_ctx->is_fixed, (long) digits, (long) nc_ctx->abs_pos));
  66485. /* Digit generation */
  66486. while (pos > pos_end) {
  66487. DUK_DDD(DUK_DDDPRINT("digit generation: pos=%ld, pos_end=%ld",
  66488. (long) pos, (long) pos_end));
  66489. if (pos == 0) {
  66490. *q++ = (duk_uint8_t) '.';
  66491. }
  66492. if (pos > k) {
  66493. *q++ = (duk_uint8_t) '0';
  66494. } else if (pos <= k - nc_ctx->count) {
  66495. *q++ = (duk_uint8_t) '0';
  66496. } else {
  66497. dig = nc_ctx->digits[k - pos];
  66498. DUK_ASSERT(dig >= 0 && dig < nc_ctx->B);
  66499. *q++ = (duk_uint8_t) DUK__DIGITCHAR(dig);
  66500. }
  66501. pos--;
  66502. }
  66503. DUK_ASSERT(pos <= 1);
  66504. /* Exponent */
  66505. if (expt != DUK__NO_EXP) {
  66506. /*
  66507. * Exponent notation for non-base-10 numbers isn't specified in Ecmascript
  66508. * specification, as it never explicitly turns up: non-decimal numbers can
  66509. * only be formatted with Number.prototype.toString([radix]) and for that,
  66510. * behavior is not explicitly specified.
  66511. *
  66512. * Logical choices include formatting the exponent as decimal (e.g. binary
  66513. * 100000 as 1e+5) or in current radix (e.g. binary 100000 as 1e+101).
  66514. * The Dragon4 algorithm (in the original paper) prints the exponent value
  66515. * in the target radix B. However, for radix values 15 and above, the
  66516. * exponent separator 'e' is no longer easily parseable. Consider, for
  66517. * instance, the number "1.faecee+1c".
  66518. */
  66519. duk_size_t len;
  66520. char expt_sign;
  66521. *q++ = 'e';
  66522. if (expt >= 0) {
  66523. expt_sign = '+';
  66524. } else {
  66525. expt_sign = '-';
  66526. expt = -expt;
  66527. }
  66528. *q++ = (duk_uint8_t) expt_sign;
  66529. len = duk__dragon4_format_uint32(q, (duk_uint32_t) expt, radix);
  66530. q += len;
  66531. }
  66532. duk_push_lstring(ctx, (const char *) buf, (size_t) (q - buf));
  66533. }
  66534. /*
  66535. * Conversion helpers
  66536. */
  66537. DUK_LOCAL void duk__dragon4_double_to_ctx(duk__numconv_stringify_ctx *nc_ctx, duk_double_t x) {
  66538. duk_double_union u;
  66539. duk_uint32_t tmp;
  66540. duk_small_int_t expt;
  66541. /*
  66542. * seeeeeee eeeeffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
  66543. * A B C D E F G H
  66544. *
  66545. * s sign bit
  66546. * eee... exponent field
  66547. * fff... fraction
  66548. *
  66549. * ieee value = 1.ffff... * 2^(e - 1023) (normal)
  66550. * = 0.ffff... * 2^(-1022) (denormal)
  66551. *
  66552. * algorithm v = f * b^e
  66553. */
  66554. DUK_DBLUNION_SET_DOUBLE(&u, x);
  66555. nc_ctx->f.n = 2;
  66556. tmp = DUK_DBLUNION_GET_LOW32(&u);
  66557. nc_ctx->f.v[0] = tmp;
  66558. tmp = DUK_DBLUNION_GET_HIGH32(&u);
  66559. nc_ctx->f.v[1] = tmp & 0x000fffffUL;
  66560. expt = (duk_small_int_t) ((tmp >> 20) & 0x07ffUL);
  66561. if (expt == 0) {
  66562. /* denormal */
  66563. expt = DUK__IEEE_DOUBLE_EXP_MIN - 52;
  66564. duk__bi_normalize(&nc_ctx->f);
  66565. } else {
  66566. /* normal: implicit leading 1-bit */
  66567. nc_ctx->f.v[1] |= 0x00100000UL;
  66568. expt = expt - DUK__IEEE_DOUBLE_EXP_BIAS - 52;
  66569. DUK_ASSERT(duk__bi_is_valid(&nc_ctx->f)); /* true, because v[1] has at least one bit set */
  66570. }
  66571. DUK_ASSERT(duk__bi_is_valid(&nc_ctx->f));
  66572. nc_ctx->e = expt;
  66573. }
  66574. DUK_LOCAL void duk__dragon4_ctx_to_double(duk__numconv_stringify_ctx *nc_ctx, duk_double_t *x) {
  66575. duk_double_union u;
  66576. duk_small_int_t expt;
  66577. duk_small_int_t i;
  66578. duk_small_int_t bitstart;
  66579. duk_small_int_t bitround;
  66580. duk_small_int_t bitidx;
  66581. duk_small_int_t skip_round;
  66582. duk_uint32_t t, v;
  66583. DUK_ASSERT(nc_ctx->count == 53 + 1);
  66584. /* Sometimes this assert is not true right now; it will be true after
  66585. * rounding. See: test-bug-numconv-mantissa-assert.js.
  66586. */
  66587. DUK_ASSERT_DISABLE(nc_ctx->digits[0] == 1); /* zero handled by caller */
  66588. /* Should not be required because the code below always sets both high
  66589. * and low parts, but at least gcc-4.4.5 fails to deduce this correctly
  66590. * (perhaps because the low part is set (seemingly) conditionally in a
  66591. * loop), so this is here to avoid the bogus warning.
  66592. */
  66593. DUK_MEMZERO((void *) &u, sizeof(u));
  66594. /*
  66595. * Figure out how generated digits match up with the mantissa,
  66596. * and then perform rounding. If mantissa overflows, need to
  66597. * recompute the exponent (it is bumped and may overflow to
  66598. * infinity).
  66599. *
  66600. * For normal numbers the leading '1' is hidden and ignored,
  66601. * and the last bit is used for rounding:
  66602. *
  66603. * rounding pt
  66604. * <--------52------->|
  66605. * 1 x x x x ... x x x x|y ==> x x x x ... x x x x
  66606. *
  66607. * For denormals, the leading '1' is included in the number,
  66608. * and the rounding point is different:
  66609. *
  66610. * rounding pt
  66611. * <--52 or less--->|
  66612. * 1 x x x x ... x x|x x y ==> 0 0 ... 1 x x ... x x
  66613. *
  66614. * The largest denormals will have a mantissa beginning with
  66615. * a '1' (the explicit leading bit); smaller denormals will
  66616. * have leading zero bits.
  66617. *
  66618. * If the exponent would become too high, the result becomes
  66619. * Infinity. If the exponent is so small that the entire
  66620. * mantissa becomes zero, the result becomes zero.
  66621. *
  66622. * Note: the Dragon4 'k' is off-by-one with respect to the IEEE
  66623. * exponent. For instance, k==0 indicates that the leading '1'
  66624. * digit is at the first binary fraction position (0.1xxx...);
  66625. * the corresponding IEEE exponent would be -1.
  66626. */
  66627. skip_round = 0;
  66628. recheck_exp:
  66629. expt = nc_ctx->k - 1; /* IEEE exp without bias */
  66630. if (expt > 1023) {
  66631. /* Infinity */
  66632. bitstart = -255; /* needed for inf: causes mantissa to become zero,
  66633. * and rounding to be skipped.
  66634. */
  66635. expt = 2047;
  66636. } else if (expt >= -1022) {
  66637. /* normal */
  66638. bitstart = 1; /* skip leading digit */
  66639. expt += DUK__IEEE_DOUBLE_EXP_BIAS;
  66640. DUK_ASSERT(expt >= 1 && expt <= 2046);
  66641. } else {
  66642. /* denormal or zero */
  66643. bitstart = 1023 + expt; /* expt==-1023 -> bitstart=0 (leading 1);
  66644. * expt==-1024 -> bitstart=-1 (one left of leading 1), etc
  66645. */
  66646. expt = 0;
  66647. }
  66648. bitround = bitstart + 52;
  66649. DUK_DDD(DUK_DDDPRINT("ieee expt=%ld, bitstart=%ld, bitround=%ld",
  66650. (long) expt, (long) bitstart, (long) bitround));
  66651. if (!skip_round) {
  66652. if (duk__dragon4_fixed_format_round(nc_ctx, bitround)) {
  66653. /* Corner case: see test-numconv-parse-mant-carry.js. We could
  66654. * just bump the exponent and update bitstart, but it's more robust
  66655. * to recompute (but avoid rounding twice).
  66656. */
  66657. DUK_DDD(DUK_DDDPRINT("rounding caused exponent to be bumped, recheck exponent"));
  66658. skip_round = 1;
  66659. goto recheck_exp;
  66660. }
  66661. }
  66662. /*
  66663. * Create mantissa
  66664. */
  66665. t = 0;
  66666. for (i = 0; i < 52; i++) {
  66667. bitidx = bitstart + 52 - 1 - i;
  66668. if (bitidx >= nc_ctx->count) {
  66669. v = 0;
  66670. } else if (bitidx < 0) {
  66671. v = 0;
  66672. } else {
  66673. v = nc_ctx->digits[bitidx];
  66674. }
  66675. DUK_ASSERT(v == 0 || v == 1);
  66676. t += v << (i % 32);
  66677. if (i == 31) {
  66678. /* low 32 bits is complete */
  66679. DUK_DBLUNION_SET_LOW32(&u, t);
  66680. t = 0;
  66681. }
  66682. }
  66683. /* t has high mantissa */
  66684. DUK_DDD(DUK_DDDPRINT("mantissa is complete: %08lx %08lx",
  66685. (unsigned long) t,
  66686. (unsigned long) DUK_DBLUNION_GET_LOW32(&u)));
  66687. DUK_ASSERT(expt >= 0 && expt <= 0x7ffL);
  66688. t += expt << 20;
  66689. #if 0 /* caller handles sign change */
  66690. if (negative) {
  66691. t |= 0x80000000U;
  66692. }
  66693. #endif
  66694. DUK_DBLUNION_SET_HIGH32(&u, t);
  66695. DUK_DDD(DUK_DDDPRINT("number is complete: %08lx %08lx",
  66696. (unsigned long) DUK_DBLUNION_GET_HIGH32(&u),
  66697. (unsigned long) DUK_DBLUNION_GET_LOW32(&u)));
  66698. *x = DUK_DBLUNION_GET_DOUBLE(&u);
  66699. }
  66700. /*
  66701. * Exposed number-to-string API
  66702. *
  66703. * Input: [ number ]
  66704. * Output: [ string ]
  66705. */
  66706. DUK_INTERNAL void duk_numconv_stringify(duk_context *ctx, duk_small_int_t radix, duk_small_int_t digits, duk_small_uint_t flags) {
  66707. duk_double_t x;
  66708. duk_small_int_t c;
  66709. duk_small_int_t neg;
  66710. duk_uint32_t uval;
  66711. duk__numconv_stringify_ctx nc_ctx_alloc; /* large context; around 2kB now */
  66712. duk__numconv_stringify_ctx *nc_ctx = &nc_ctx_alloc;
  66713. x = (duk_double_t) duk_require_number(ctx, -1);
  66714. duk_pop(ctx);
  66715. /*
  66716. * Handle special cases (NaN, infinity, zero).
  66717. */
  66718. c = (duk_small_int_t) DUK_FPCLASSIFY(x);
  66719. if (DUK_SIGNBIT((double) x)) {
  66720. x = -x;
  66721. neg = 1;
  66722. } else {
  66723. neg = 0;
  66724. }
  66725. /* NaN sign bit is platform specific with unpacked, un-normalized NaNs */
  66726. DUK_ASSERT(c == DUK_FP_NAN || DUK_SIGNBIT((double) x) == 0);
  66727. if (c == DUK_FP_NAN) {
  66728. duk_push_hstring_stridx(ctx, DUK_STRIDX_NAN);
  66729. return;
  66730. } else if (c == DUK_FP_INFINITE) {
  66731. if (neg) {
  66732. /* -Infinity */
  66733. duk_push_hstring_stridx(ctx, DUK_STRIDX_MINUS_INFINITY);
  66734. } else {
  66735. /* Infinity */
  66736. duk_push_hstring_stridx(ctx, DUK_STRIDX_INFINITY);
  66737. }
  66738. return;
  66739. } else if (c == DUK_FP_ZERO) {
  66740. /* We can't shortcut zero here if it goes through special formatting
  66741. * (such as forced exponential notation).
  66742. */
  66743. ;
  66744. }
  66745. /*
  66746. * Handle integers in 32-bit range (that is, [-(2**32-1),2**32-1])
  66747. * specially, as they're very likely for embedded programs. This
  66748. * is now done for all radix values. We must be careful not to use
  66749. * the fast path when special formatting (e.g. forced exponential)
  66750. * is in force.
  66751. *
  66752. * XXX: could save space by supporting radix 10 only and using
  66753. * sprintf "%lu" for the fast path and for exponent formatting.
  66754. */
  66755. uval = (unsigned int) x;
  66756. if (((double) uval) == x && /* integer number in range */
  66757. flags == 0) { /* no special formatting */
  66758. /* use bigint area as a temp */
  66759. duk_uint8_t *buf = (duk_uint8_t *) (&nc_ctx->f);
  66760. duk_uint8_t *p = buf;
  66761. DUK_ASSERT(DUK__NUMCONV_CTX_BIGINTS_SIZE >= 32 + 1); /* max size: radix=2 + sign */
  66762. if (neg && uval != 0) {
  66763. /* no negative sign for zero */
  66764. *p++ = (duk_uint8_t) '-';
  66765. }
  66766. p += duk__dragon4_format_uint32(p, uval, radix);
  66767. duk_push_lstring(ctx, (const char *) buf, (duk_size_t) (p - buf));
  66768. return;
  66769. }
  66770. /*
  66771. * Dragon4 setup.
  66772. *
  66773. * Convert double from IEEE representation for conversion;
  66774. * normal finite values have an implicit leading 1-bit. The
  66775. * slow path algorithm doesn't handle zero, so zero is special
  66776. * cased here but still creates a valid nc_ctx, and goes
  66777. * through normal formatting in case special formatting has
  66778. * been requested (e.g. forced exponential format: 0 -> "0e+0").
  66779. */
  66780. /* Would be nice to bulk clear the allocation, but the context
  66781. * is 1-2 kilobytes and nothing should rely on it being zeroed.
  66782. */
  66783. #if 0
  66784. DUK_MEMZERO((void *) nc_ctx, sizeof(*nc_ctx)); /* slow init, do only for slow path cases */
  66785. #endif
  66786. nc_ctx->is_s2n = 0;
  66787. nc_ctx->b = 2;
  66788. nc_ctx->B = radix;
  66789. nc_ctx->abs_pos = 0;
  66790. if (flags & DUK_N2S_FLAG_FIXED_FORMAT) {
  66791. nc_ctx->is_fixed = 1;
  66792. if (flags & DUK_N2S_FLAG_FRACTION_DIGITS) {
  66793. /* absolute req_digits; e.g. digits = 1 -> last digit is 0,
  66794. * but add an extra digit for rounding.
  66795. */
  66796. nc_ctx->abs_pos = 1;
  66797. nc_ctx->req_digits = (-digits + 1) - 1;
  66798. } else {
  66799. nc_ctx->req_digits = digits + 1;
  66800. }
  66801. } else {
  66802. nc_ctx->is_fixed = 0;
  66803. nc_ctx->req_digits = 0;
  66804. }
  66805. if (c == DUK_FP_ZERO) {
  66806. /* Zero special case: fake requested number of zero digits; ensure
  66807. * no sign bit is printed. Relative and absolute fixed format
  66808. * require separate handling.
  66809. */
  66810. duk_small_int_t count;
  66811. if (nc_ctx->is_fixed) {
  66812. if (nc_ctx->abs_pos) {
  66813. count = digits + 2; /* lead zero + 'digits' fractions + 1 for rounding */
  66814. } else {
  66815. count = digits + 1; /* + 1 for rounding */
  66816. }
  66817. } else {
  66818. count = 1;
  66819. }
  66820. DUK_DDD(DUK_DDDPRINT("count=%ld", (long) count));
  66821. DUK_ASSERT(count >= 1);
  66822. DUK_MEMZERO((void *) nc_ctx->digits, count);
  66823. nc_ctx->count = count;
  66824. nc_ctx->k = 1; /* 0.000... */
  66825. neg = 0;
  66826. goto zero_skip;
  66827. }
  66828. duk__dragon4_double_to_ctx(nc_ctx, x); /* -> sets 'f' and 'e' */
  66829. DUK__BI_PRINT("f", &nc_ctx->f);
  66830. DUK_DDD(DUK_DDDPRINT("e=%ld", (long) nc_ctx->e));
  66831. /*
  66832. * Dragon4 slow path digit generation.
  66833. */
  66834. duk__dragon4_prepare(nc_ctx); /* setup many variables in nc_ctx */
  66835. DUK_DDD(DUK_DDDPRINT("after prepare:"));
  66836. DUK__BI_PRINT("r", &nc_ctx->r);
  66837. DUK__BI_PRINT("s", &nc_ctx->s);
  66838. DUK__BI_PRINT("mp", &nc_ctx->mp);
  66839. DUK__BI_PRINT("mm", &nc_ctx->mm);
  66840. duk__dragon4_scale(nc_ctx);
  66841. DUK_DDD(DUK_DDDPRINT("after scale; k=%ld", (long) nc_ctx->k));
  66842. DUK__BI_PRINT("r", &nc_ctx->r);
  66843. DUK__BI_PRINT("s", &nc_ctx->s);
  66844. DUK__BI_PRINT("mp", &nc_ctx->mp);
  66845. DUK__BI_PRINT("mm", &nc_ctx->mm);
  66846. duk__dragon4_generate(nc_ctx);
  66847. /*
  66848. * Convert and push final string.
  66849. */
  66850. zero_skip:
  66851. if (flags & DUK_N2S_FLAG_FIXED_FORMAT) {
  66852. /* Perform fixed-format rounding. */
  66853. duk_small_int_t roundpos;
  66854. if (flags & DUK_N2S_FLAG_FRACTION_DIGITS) {
  66855. /* 'roundpos' is relative to nc_ctx->k and increases to the right
  66856. * (opposite of how 'k' changes).
  66857. */
  66858. roundpos = -digits; /* absolute position for digit considered for rounding */
  66859. roundpos = nc_ctx->k - roundpos;
  66860. } else {
  66861. roundpos = digits;
  66862. }
  66863. DUK_DDD(DUK_DDDPRINT("rounding: k=%ld, count=%ld, digits=%ld, roundpos=%ld",
  66864. (long) nc_ctx->k, (long) nc_ctx->count, (long) digits, (long) roundpos));
  66865. (void) duk__dragon4_fixed_format_round(nc_ctx, roundpos);
  66866. /* Note: 'count' is currently not adjusted by rounding (i.e. the
  66867. * digits are not "chopped off". That shouldn't matter because
  66868. * the digit position (absolute or relative) is passed on to the
  66869. * convert-and-push function.
  66870. */
  66871. }
  66872. duk__dragon4_convert_and_push(nc_ctx, ctx, radix, digits, flags, neg);
  66873. }
  66874. /*
  66875. * Exposed string-to-number API
  66876. *
  66877. * Input: [ string ]
  66878. * Output: [ number ]
  66879. *
  66880. * If number parsing fails, a NaN is pushed as the result. If number parsing
  66881. * fails due to an internal error, an InternalError is thrown.
  66882. */
  66883. DUK_INTERNAL void duk_numconv_parse(duk_context *ctx, duk_small_int_t radix, duk_small_uint_t flags) {
  66884. duk_hthread *thr = (duk_hthread *) ctx;
  66885. duk__numconv_stringify_ctx nc_ctx_alloc; /* large context; around 2kB now */
  66886. duk__numconv_stringify_ctx *nc_ctx = &nc_ctx_alloc;
  66887. duk_double_t res;
  66888. duk_hstring *h_str;
  66889. duk_small_int_t expt;
  66890. duk_small_int_t expt_neg;
  66891. duk_small_int_t expt_adj;
  66892. duk_small_int_t neg;
  66893. duk_small_int_t dig;
  66894. duk_small_int_t dig_whole;
  66895. duk_small_int_t dig_lzero;
  66896. duk_small_int_t dig_frac;
  66897. duk_small_int_t dig_expt;
  66898. duk_small_int_t dig_prec;
  66899. const duk__exp_limits *explim;
  66900. const duk_uint8_t *p;
  66901. duk_small_int_t ch;
  66902. /* This seems to waste a lot of stack frame entries, but good compilers
  66903. * will compute these as needed below. Some of these initial flags are
  66904. * also modified in the code below, so they can't all be removed.
  66905. */
  66906. duk_small_int_t trim_white = (flags & DUK_S2N_FLAG_TRIM_WHITE);
  66907. duk_small_int_t allow_expt = (flags & DUK_S2N_FLAG_ALLOW_EXP);
  66908. duk_small_int_t allow_garbage = (flags & DUK_S2N_FLAG_ALLOW_GARBAGE);
  66909. duk_small_int_t allow_plus = (flags & DUK_S2N_FLAG_ALLOW_PLUS);
  66910. duk_small_int_t allow_minus = (flags & DUK_S2N_FLAG_ALLOW_MINUS);
  66911. duk_small_int_t allow_infinity = (flags & DUK_S2N_FLAG_ALLOW_INF);
  66912. duk_small_int_t allow_frac = (flags & DUK_S2N_FLAG_ALLOW_FRAC);
  66913. duk_small_int_t allow_naked_frac = (flags & DUK_S2N_FLAG_ALLOW_NAKED_FRAC);
  66914. duk_small_int_t allow_empty_frac = (flags & DUK_S2N_FLAG_ALLOW_EMPTY_FRAC);
  66915. duk_small_int_t allow_empty = (flags & DUK_S2N_FLAG_ALLOW_EMPTY_AS_ZERO);
  66916. duk_small_int_t allow_leading_zero = (flags & DUK_S2N_FLAG_ALLOW_LEADING_ZERO);
  66917. duk_small_int_t allow_auto_hex_int = (flags & DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT);
  66918. duk_small_int_t allow_auto_oct_int = (flags & DUK_S2N_FLAG_ALLOW_AUTO_OCT_INT);
  66919. DUK_DDD(DUK_DDDPRINT("parse number: %!T, radix=%ld, flags=0x%08lx",
  66920. (duk_tval *) duk_get_tval(ctx, -1),
  66921. (long) radix, (unsigned long) flags));
  66922. DUK_ASSERT(radix >= 2 && radix <= 36);
  66923. DUK_ASSERT(radix - 2 < (duk_small_int_t) sizeof(duk__str2num_digits_for_radix));
  66924. /*
  66925. * Preliminaries: trim, sign, Infinity check
  66926. *
  66927. * We rely on the interned string having a NUL terminator, which will
  66928. * cause a parse failure wherever it is encountered. As a result, we
  66929. * don't need separate pointer checks.
  66930. *
  66931. * There is no special parsing for 'NaN' in the specification although
  66932. * 'Infinity' (with an optional sign) is allowed in some contexts.
  66933. * Some contexts allow plus/minus sign, while others only allow the
  66934. * minus sign (like JSON.parse()).
  66935. *
  66936. * Automatic hex number detection (leading '0x' or '0X') and octal
  66937. * number detection (leading '0' followed by at least one octal digit)
  66938. * is done here too.
  66939. */
  66940. if (trim_white) {
  66941. /* Leading / trailing whitespace is sometimes accepted and
  66942. * sometimes not. After white space trimming, all valid input
  66943. * characters are pure ASCII.
  66944. */
  66945. duk_trim(ctx, -1);
  66946. }
  66947. h_str = duk_require_hstring(ctx, -1);
  66948. DUK_ASSERT(h_str != NULL);
  66949. p = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_str);
  66950. neg = 0;
  66951. ch = *p;
  66952. if (ch == (duk_small_int_t) '+') {
  66953. if (!allow_plus) {
  66954. DUK_DDD(DUK_DDDPRINT("parse failed: leading plus sign not allowed"));
  66955. goto parse_fail;
  66956. }
  66957. p++;
  66958. } else if (ch == (duk_small_int_t) '-') {
  66959. if (!allow_minus) {
  66960. DUK_DDD(DUK_DDDPRINT("parse failed: leading minus sign not allowed"));
  66961. goto parse_fail;
  66962. }
  66963. p++;
  66964. neg = 1;
  66965. }
  66966. ch = *p;
  66967. if (allow_infinity && ch == (duk_small_int_t) 'I') {
  66968. /* Don't check for Infinity unless the context allows it.
  66969. * 'Infinity' is a valid integer literal in e.g. base-36:
  66970. *
  66971. * parseInt('Infinity', 36)
  66972. * 1461559270678
  66973. */
  66974. const duk_uint8_t *q;
  66975. /* borrow literal Infinity from builtin string */
  66976. q = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(DUK_HTHREAD_STRING_INFINITY(thr));
  66977. if (DUK_STRNCMP((const char *) p, (const char *) q, 8) == 0) {
  66978. if (!allow_garbage && (p[8] != (duk_uint8_t) 0)) {
  66979. DUK_DDD(DUK_DDDPRINT("parse failed: trailing garbage after matching 'Infinity' not allowed"));
  66980. goto parse_fail;
  66981. } else {
  66982. res = DUK_DOUBLE_INFINITY;
  66983. goto negcheck_and_ret;
  66984. }
  66985. }
  66986. }
  66987. if (ch == (duk_small_int_t) '0') {
  66988. duk_small_int_t detect_radix = 0;
  66989. ch = p[1];
  66990. if (allow_auto_hex_int && (ch == (duk_small_int_t) 'x' || ch == (duk_small_int_t) 'X')) {
  66991. DUK_DDD(DUK_DDDPRINT("detected 0x/0X hex prefix, changing radix and preventing fractions and exponent"));
  66992. detect_radix = 16;
  66993. allow_empty = 0; /* interpret e.g. '0x' and '0xg' as a NaN (= parse error) */
  66994. p += 2;
  66995. } else if (allow_auto_oct_int && (ch >= (duk_small_int_t) '0' && ch <= (duk_small_int_t) '9')) {
  66996. DUK_DDD(DUK_DDDPRINT("detected 0n oct prefix, changing radix and preventing fractions and exponent"));
  66997. detect_radix = 8;
  66998. allow_empty = 1; /* interpret e.g. '09' as '0', not NaN */
  66999. p += 1;
  67000. }
  67001. if (detect_radix > 0) {
  67002. radix = detect_radix;
  67003. allow_expt = 0;
  67004. allow_frac = 0;
  67005. allow_naked_frac = 0;
  67006. allow_empty_frac = 0;
  67007. allow_leading_zero = 1; /* allow e.g. '0x0009' and '00077' */
  67008. }
  67009. }
  67010. /*
  67011. * Scan number and setup for Dragon4.
  67012. *
  67013. * The fast path case is detected during setup: an integer which
  67014. * can be converted without rounding, no net exponent. The fast
  67015. * path could be implemented as a separate scan, but may not really
  67016. * be worth it: the multiplications for building 'f' are not
  67017. * expensive when 'f' is small.
  67018. *
  67019. * The significand ('f') must contain enough bits of (apparent)
  67020. * accuracy, so that Dragon4 will generate enough binary output digits.
  67021. * For decimal numbers, this means generating a 20-digit significand,
  67022. * which should yield enough practical accuracy to parse IEEE doubles.
  67023. * In fact, the Ecmascript specification explicitly allows an
  67024. * implementation to treat digits beyond 20 as zeroes (and even
  67025. * to round the 20th digit upwards). For non-decimal numbers, the
  67026. * appropriate number of digits has been precomputed for comparable
  67027. * accuracy.
  67028. *
  67029. * Digit counts:
  67030. *
  67031. * [ dig_lzero ]
  67032. * |
  67033. * .+-..---[ dig_prec ]----.
  67034. * | || |
  67035. * 0000123.456789012345678901234567890e+123456
  67036. * | | | | | |
  67037. * `--+--' `------[ dig_frac ]-------' `-+--'
  67038. * | |
  67039. * [ dig_whole ] [ dig_expt ]
  67040. *
  67041. * dig_frac and dig_expt are -1 if not present
  67042. * dig_lzero is only computed for whole number part
  67043. *
  67044. * Parsing state
  67045. *
  67046. * Parsing whole part dig_frac < 0 AND dig_expt < 0
  67047. * Parsing fraction part dig_frac >= 0 AND dig_expt < 0
  67048. * Parsing exponent part dig_expt >= 0 (dig_frac may be < 0 or >= 0)
  67049. *
  67050. * Note: in case we hit an implementation limit (like exponent range),
  67051. * we should throw an error, NOT return NaN or Infinity. Even with
  67052. * very large exponent (or significand) values the final result may be
  67053. * finite, so NaN/Infinity would be incorrect.
  67054. */
  67055. duk__bi_set_small(&nc_ctx->f, 0);
  67056. dig_prec = 0;
  67057. dig_lzero = 0;
  67058. dig_whole = 0;
  67059. dig_frac = -1;
  67060. dig_expt = -1;
  67061. expt = 0;
  67062. expt_adj = 0; /* essentially tracks digit position of lowest 'f' digit */
  67063. expt_neg = 0;
  67064. for (;;) {
  67065. ch = *p++;
  67066. DUK_DDD(DUK_DDDPRINT("parse digits: p=%p, ch='%c' (%ld), expt=%ld, expt_adj=%ld, "
  67067. "dig_whole=%ld, dig_frac=%ld, dig_expt=%ld, dig_lzero=%ld, dig_prec=%ld",
  67068. (void *) p, (int) ((ch >= 0x20 && ch <= 0x7e) ? ch : '?'), (long) ch,
  67069. (long) expt, (long) expt_adj, (long) dig_whole, (long) dig_frac,
  67070. (long) dig_expt, (long) dig_lzero, (long) dig_prec));
  67071. DUK__BI_PRINT("f", &nc_ctx->f);
  67072. /* Most common cases first. */
  67073. if (ch >= (duk_small_int_t) '0' && ch <= (duk_small_int_t) '9') {
  67074. dig = (int) ch - '0' + 0;
  67075. } else if (ch == (duk_small_int_t) '.') {
  67076. /* A leading digit is not required in some cases, e.g. accept ".123".
  67077. * In other cases (JSON.parse()) a leading digit is required. This
  67078. * is checked for after the loop.
  67079. */
  67080. if (dig_frac >= 0 || dig_expt >= 0) {
  67081. if (allow_garbage) {
  67082. DUK_DDD(DUK_DDDPRINT("garbage termination (invalid period)"));
  67083. break;
  67084. } else {
  67085. DUK_DDD(DUK_DDDPRINT("parse failed: period not allowed"));
  67086. goto parse_fail;
  67087. }
  67088. }
  67089. if (!allow_frac) {
  67090. /* Some contexts don't allow fractions at all; this can't be a
  67091. * post-check because the state ('f' and expt) would be incorrect.
  67092. */
  67093. if (allow_garbage) {
  67094. DUK_DDD(DUK_DDDPRINT("garbage termination (invalid first period)"));
  67095. break;
  67096. } else {
  67097. DUK_DDD(DUK_DDDPRINT("parse failed: fraction part not allowed"));
  67098. }
  67099. }
  67100. DUK_DDD(DUK_DDDPRINT("start fraction part"));
  67101. dig_frac = 0;
  67102. continue;
  67103. } else if (ch == (duk_small_int_t) 0) {
  67104. DUK_DDD(DUK_DDDPRINT("NUL termination"));
  67105. break;
  67106. } else if (allow_expt && dig_expt < 0 && (ch == (duk_small_int_t) 'e' || ch == (duk_small_int_t) 'E')) {
  67107. /* Note: we don't parse back exponent notation for anything else
  67108. * than radix 10, so this is not an ambiguous check (e.g. hex
  67109. * exponent values may have 'e' either as a significand digit
  67110. * or as an exponent separator).
  67111. *
  67112. * If the exponent separator occurs twice, 'e' will be interpreted
  67113. * as a digit (= 14) and will be rejected as an invalid decimal
  67114. * digit.
  67115. */
  67116. DUK_DDD(DUK_DDDPRINT("start exponent part"));
  67117. /* Exponent without a sign or with a +/- sign is accepted
  67118. * by all call sites (even JSON.parse()).
  67119. */
  67120. ch = *p;
  67121. if (ch == (duk_small_int_t) '-') {
  67122. expt_neg = 1;
  67123. p++;
  67124. } else if (ch == (duk_small_int_t) '+') {
  67125. p++;
  67126. }
  67127. dig_expt = 0;
  67128. continue;
  67129. } else if (ch >= (duk_small_int_t) 'a' && ch <= (duk_small_int_t) 'z') {
  67130. dig = (duk_small_int_t) (ch - (duk_small_int_t) 'a' + 0x0a);
  67131. } else if (ch >= (duk_small_int_t) 'A' && ch <= (duk_small_int_t) 'Z') {
  67132. dig = (duk_small_int_t) (ch - (duk_small_int_t) 'A' + 0x0a);
  67133. } else {
  67134. dig = 255; /* triggers garbage digit check below */
  67135. }
  67136. DUK_ASSERT((dig >= 0 && dig <= 35) || dig == 255);
  67137. if (dig >= radix) {
  67138. if (allow_garbage) {
  67139. DUK_DDD(DUK_DDDPRINT("garbage termination"));
  67140. break;
  67141. } else {
  67142. DUK_DDD(DUK_DDDPRINT("parse failed: trailing garbage or invalid digit"));
  67143. goto parse_fail;
  67144. }
  67145. }
  67146. if (dig_expt < 0) {
  67147. /* whole or fraction digit */
  67148. if (dig_prec < duk__str2num_digits_for_radix[radix - 2]) {
  67149. /* significant from precision perspective */
  67150. duk_small_int_t f_zero = duk__bi_is_zero(&nc_ctx->f);
  67151. if (f_zero && dig == 0) {
  67152. /* Leading zero is not counted towards precision digits; not
  67153. * in the integer part, nor in the fraction part.
  67154. */
  67155. if (dig_frac < 0) {
  67156. dig_lzero++;
  67157. }
  67158. } else {
  67159. /* XXX: join these ops (multiply-accumulate), but only if
  67160. * code footprint decreases.
  67161. */
  67162. duk__bi_mul_small(&nc_ctx->t1, &nc_ctx->f, radix);
  67163. duk__bi_add_small(&nc_ctx->f, &nc_ctx->t1, dig);
  67164. dig_prec++;
  67165. }
  67166. } else {
  67167. /* Ignore digits beyond a radix-specific limit, but note them
  67168. * in expt_adj.
  67169. */
  67170. expt_adj++;
  67171. }
  67172. if (dig_frac >= 0) {
  67173. dig_frac++;
  67174. expt_adj--;
  67175. } else {
  67176. dig_whole++;
  67177. }
  67178. } else {
  67179. /* exponent digit */
  67180. expt = expt * radix + dig;
  67181. if (expt > DUK_S2N_MAX_EXPONENT) {
  67182. /* impose a reasonable exponent limit, so that exp
  67183. * doesn't need to get tracked using a bigint.
  67184. */
  67185. DUK_DDD(DUK_DDDPRINT("parse failed: exponent too large"));
  67186. goto parse_int_error;
  67187. }
  67188. dig_expt++;
  67189. }
  67190. }
  67191. /* Leading zero. */
  67192. if (dig_lzero > 0 && dig_whole > 1) {
  67193. if (!allow_leading_zero) {
  67194. DUK_DDD(DUK_DDDPRINT("parse failed: leading zeroes not allowed in integer part"));
  67195. goto parse_fail;
  67196. }
  67197. }
  67198. /* Validity checks for various fraction formats ("0.1", ".1", "1.", "."). */
  67199. if (dig_whole == 0) {
  67200. if (dig_frac == 0) {
  67201. /* "." is not accepted in any format */
  67202. DUK_DDD(DUK_DDDPRINT("parse failed: plain period without leading or trailing digits"));
  67203. goto parse_fail;
  67204. } else if (dig_frac > 0) {
  67205. /* ".123" */
  67206. if (!allow_naked_frac) {
  67207. DUK_DDD(DUK_DDDPRINT("parse failed: fraction part not allowed without "
  67208. "leading integer digit(s)"));
  67209. goto parse_fail;
  67210. }
  67211. } else {
  67212. /* empty ("") is allowed in some formats (e.g. Number(''), as zero */
  67213. if (!allow_empty) {
  67214. DUK_DDD(DUK_DDDPRINT("parse failed: empty string not allowed (as zero)"));
  67215. goto parse_fail;
  67216. }
  67217. }
  67218. } else {
  67219. if (dig_frac == 0) {
  67220. /* "123." is allowed in some formats */
  67221. if (!allow_empty_frac) {
  67222. DUK_DDD(DUK_DDDPRINT("parse failed: empty fractions"));
  67223. goto parse_fail;
  67224. }
  67225. } else if (dig_frac > 0) {
  67226. /* "123.456" */
  67227. ;
  67228. } else {
  67229. /* "123" */
  67230. ;
  67231. }
  67232. }
  67233. /* Exponent without digits (e.g. "1e" or "1e+"). If trailing garbage is
  67234. * allowed, ignore exponent part as garbage (= parse as "1", i.e. exp 0).
  67235. */
  67236. if (dig_expt == 0) {
  67237. if (!allow_garbage) {
  67238. DUK_DDD(DUK_DDDPRINT("parse failed: empty exponent"));
  67239. goto parse_fail;
  67240. }
  67241. DUK_ASSERT(expt == 0);
  67242. }
  67243. if (expt_neg) {
  67244. expt = -expt;
  67245. }
  67246. DUK_DDD(DUK_DDDPRINT("expt=%ld, expt_adj=%ld, net exponent -> %ld",
  67247. (long) expt, (long) expt_adj, (long) (expt + expt_adj)));
  67248. expt += expt_adj;
  67249. /* Fast path check. */
  67250. if (nc_ctx->f.n <= 1 && /* 32-bit value */
  67251. expt == 0 /* no net exponent */) {
  67252. /* Fast path is triggered for no exponent and also for balanced exponent
  67253. * and fraction parts, e.g. for "1.23e2" == "123". Remember to respect
  67254. * zero sign.
  67255. */
  67256. /* XXX: could accept numbers larger than 32 bits, e.g. up to 53 bits? */
  67257. DUK_DDD(DUK_DDDPRINT("fast path number parse"));
  67258. if (nc_ctx->f.n == 1) {
  67259. res = (double) nc_ctx->f.v[0];
  67260. } else {
  67261. res = 0.0;
  67262. }
  67263. goto negcheck_and_ret;
  67264. }
  67265. /* Significand ('f') padding. */
  67266. while (dig_prec < duk__str2num_digits_for_radix[radix - 2]) {
  67267. /* Pad significand with "virtual" zero digits so that Dragon4 will
  67268. * have enough (apparent) precision to work with.
  67269. */
  67270. DUK_DDD(DUK_DDDPRINT("dig_prec=%ld, pad significand with zero", (long) dig_prec));
  67271. duk__bi_mul_small_copy(&nc_ctx->f, radix, &nc_ctx->t1);
  67272. DUK__BI_PRINT("f", &nc_ctx->f);
  67273. expt--;
  67274. dig_prec++;
  67275. }
  67276. DUK_DDD(DUK_DDDPRINT("final exponent: %ld", (long) expt));
  67277. /* Detect zero special case. */
  67278. if (nc_ctx->f.n == 0) {
  67279. /* This may happen even after the fast path check, if exponent is
  67280. * not balanced (e.g. "0e1"). Remember to respect zero sign.
  67281. */
  67282. DUK_DDD(DUK_DDDPRINT("significand is zero"));
  67283. res = 0.0;
  67284. goto negcheck_and_ret;
  67285. }
  67286. /* Quick reject of too large or too small exponents. This check
  67287. * would be incorrect for zero (e.g. "0e1000" is zero, not Infinity)
  67288. * so zero check must be above.
  67289. */
  67290. explim = &duk__str2num_exp_limits[radix - 2];
  67291. if (expt > explim->upper) {
  67292. DUK_DDD(DUK_DDDPRINT("exponent too large -> infinite"));
  67293. res = (duk_double_t) DUK_DOUBLE_INFINITY;
  67294. goto negcheck_and_ret;
  67295. } else if (expt < explim->lower) {
  67296. DUK_DDD(DUK_DDDPRINT("exponent too small -> zero"));
  67297. res = (duk_double_t) 0.0;
  67298. goto negcheck_and_ret;
  67299. }
  67300. nc_ctx->is_s2n = 1;
  67301. nc_ctx->e = expt;
  67302. nc_ctx->b = radix;
  67303. nc_ctx->B = 2;
  67304. nc_ctx->is_fixed = 1;
  67305. nc_ctx->abs_pos = 0;
  67306. nc_ctx->req_digits = 53 + 1;
  67307. DUK__BI_PRINT("f", &nc_ctx->f);
  67308. DUK_DDD(DUK_DDDPRINT("e=%ld", (long) nc_ctx->e));
  67309. /*
  67310. * Dragon4 slow path (binary) digit generation.
  67311. * An extra digit is generated for rounding.
  67312. */
  67313. duk__dragon4_prepare(nc_ctx); /* setup many variables in nc_ctx */
  67314. DUK_DDD(DUK_DDDPRINT("after prepare:"));
  67315. DUK__BI_PRINT("r", &nc_ctx->r);
  67316. DUK__BI_PRINT("s", &nc_ctx->s);
  67317. DUK__BI_PRINT("mp", &nc_ctx->mp);
  67318. DUK__BI_PRINT("mm", &nc_ctx->mm);
  67319. duk__dragon4_scale(nc_ctx);
  67320. DUK_DDD(DUK_DDDPRINT("after scale; k=%ld", (long) nc_ctx->k));
  67321. DUK__BI_PRINT("r", &nc_ctx->r);
  67322. DUK__BI_PRINT("s", &nc_ctx->s);
  67323. DUK__BI_PRINT("mp", &nc_ctx->mp);
  67324. DUK__BI_PRINT("mm", &nc_ctx->mm);
  67325. duk__dragon4_generate(nc_ctx);
  67326. DUK_ASSERT(nc_ctx->count == 53 + 1);
  67327. /*
  67328. * Convert binary digits into an IEEE double. Need to handle
  67329. * denormals and rounding correctly.
  67330. */
  67331. duk__dragon4_ctx_to_double(nc_ctx, &res);
  67332. goto negcheck_and_ret;
  67333. negcheck_and_ret:
  67334. if (neg) {
  67335. res = -res;
  67336. }
  67337. duk_pop(ctx);
  67338. duk_push_number(ctx, (double) res);
  67339. DUK_DDD(DUK_DDDPRINT("result: %!T", (duk_tval *) duk_get_tval(ctx, -1)));
  67340. return;
  67341. parse_fail:
  67342. DUK_DDD(DUK_DDDPRINT("parse failed"));
  67343. duk_pop(ctx);
  67344. duk_push_nan(ctx);
  67345. return;
  67346. parse_int_error:
  67347. DUK_DDD(DUK_DDDPRINT("parse failed, internal error, can't return a value"));
  67348. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, "number parse error");
  67349. return;
  67350. }
  67351. #line 1 "duk_regexp_compiler.c"
  67352. /*
  67353. * Regexp compilation.
  67354. *
  67355. * See doc/regexp.rst for a discussion of the compilation approach and
  67356. * current limitations.
  67357. *
  67358. * Regexp bytecode assumes jumps can be expressed with signed 32-bit
  67359. * integers. Consequently the bytecode size must not exceed 0x7fffffffL.
  67360. * The implementation casts duk_size_t (buffer size) to duk_(u)int32_t
  67361. * in many places. Although this could be changed, the bytecode format
  67362. * limit would still prevent regexps exceeding the signed 32-bit limit
  67363. * from working.
  67364. *
  67365. * XXX: The implementation does not prevent bytecode from exceeding the
  67366. * maximum supported size. This could be done by limiting the maximum
  67367. * input string size (assuming an upper bound can be computed for number
  67368. * of bytecode bytes emitted per input byte) or checking buffer maximum
  67369. * size when emitting bytecode (slower).
  67370. */
  67371. /* include removed: duk_internal.h */
  67372. #ifdef DUK_USE_REGEXP_SUPPORT
  67373. /*
  67374. * Helper macros
  67375. */
  67376. #define DUK__RE_INITIAL_BUFSIZE 64
  67377. #undef DUK__RE_BUFLEN
  67378. #define DUK__RE_BUFLEN(re_ctx) \
  67379. DUK_BW_GET_SIZE(re_ctx->thr, &re_ctx->bw)
  67380. /*
  67381. * Disjunction struct: result of parsing a disjunction
  67382. */
  67383. typedef struct {
  67384. /* Number of characters that the atom matches (e.g. 3 for 'abc'),
  67385. * -1 if atom is complex and number of matched characters either
  67386. * varies or is not known.
  67387. */
  67388. duk_int32_t charlen;
  67389. #if 0
  67390. /* These are not needed to implement quantifier capture handling,
  67391. * but might be needed at some point.
  67392. */
  67393. /* re_ctx->captures at start and end of atom parsing.
  67394. * Since 'captures' indicates highest capture number emitted
  67395. * so far in a DUK_REOP_SAVE, the captures numbers saved by
  67396. * the atom are: ]start_captures,end_captures].
  67397. */
  67398. duk_uint32_t start_captures;
  67399. duk_uint32_t end_captures;
  67400. #endif
  67401. } duk__re_disjunction_info;
  67402. /*
  67403. * Encoding helpers
  67404. *
  67405. * Some of the typing is bytecode based, e.g. slice sizes are unsigned 32-bit
  67406. * even though the buffer operations will use duk_size_t.
  67407. */
  67408. /* XXX: the insert helpers should ensure that the bytecode result is not
  67409. * larger than expected (or at least assert for it). Many things in the
  67410. * bytecode, like skip offsets, won't work correctly if the bytecode is
  67411. * larger than say 2G.
  67412. */
  67413. DUK_LOCAL duk_uint32_t duk__encode_i32(duk_int32_t x) {
  67414. if (x < 0) {
  67415. return ((duk_uint32_t) (-x)) * 2 + 1;
  67416. } else {
  67417. return ((duk_uint32_t) x) * 2;
  67418. }
  67419. }
  67420. /* XXX: return type should probably be duk_size_t, or explicit checks are needed for
  67421. * maximum size.
  67422. */
  67423. DUK_LOCAL duk_uint32_t duk__insert_u32(duk_re_compiler_ctx *re_ctx, duk_uint32_t offset, duk_uint32_t x) {
  67424. duk_uint8_t buf[DUK_UNICODE_MAX_XUTF8_LENGTH];
  67425. duk_small_int_t len;
  67426. len = duk_unicode_encode_xutf8((duk_ucodepoint_t) x, buf);
  67427. DUK_BW_INSERT_ENSURE_BYTES(re_ctx->thr, &re_ctx->bw, offset, buf, len);
  67428. return (duk_uint32_t) len;
  67429. }
  67430. DUK_LOCAL duk_uint32_t duk__append_u32(duk_re_compiler_ctx *re_ctx, duk_uint32_t x) {
  67431. duk_uint8_t buf[DUK_UNICODE_MAX_XUTF8_LENGTH];
  67432. duk_small_int_t len;
  67433. len = duk_unicode_encode_xutf8((duk_ucodepoint_t) x, buf);
  67434. DUK_BW_WRITE_ENSURE_BYTES(re_ctx->thr, &re_ctx->bw, buf, len);
  67435. return (duk_uint32_t) len;
  67436. }
  67437. DUK_LOCAL duk_uint32_t duk__insert_i32(duk_re_compiler_ctx *re_ctx, duk_uint32_t offset, duk_int32_t x) {
  67438. return duk__insert_u32(re_ctx, offset, duk__encode_i32(x));
  67439. }
  67440. #if 0 /* unused */
  67441. DUK_LOCAL duk_uint32_t duk__append_i32(duk_re_compiler_ctx *re_ctx, duk_int32_t x) {
  67442. return duk__append_u32(re_ctx, duk__encode_i32(x));
  67443. }
  67444. #endif
  67445. /* special helper for emitting u16 lists (used for character ranges for built-in char classes) */
  67446. DUK_LOCAL void duk__append_u16_list(duk_re_compiler_ctx *re_ctx, duk_uint16_t *values, duk_uint32_t count) {
  67447. /* Call sites don't need the result length so it's not accumulated. */
  67448. while (count > 0) {
  67449. (void) duk__append_u32(re_ctx, (duk_uint32_t) (*values++));
  67450. count--;
  67451. }
  67452. }
  67453. 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) {
  67454. DUK_BW_INSERT_ENSURE_SLICE(re_ctx->thr, &re_ctx->bw, offset, data_offset, data_length);
  67455. }
  67456. DUK_LOCAL void duk__append_slice(duk_re_compiler_ctx *re_ctx, duk_uint32_t data_offset, duk_uint32_t data_length) {
  67457. DUK_BW_WRITE_ENSURE_SLICE(re_ctx->thr, &re_ctx->bw, data_offset, data_length);
  67458. }
  67459. DUK_LOCAL void duk__remove_slice(duk_re_compiler_ctx *re_ctx, duk_uint32_t data_offset, duk_uint32_t data_length) {
  67460. DUK_BW_REMOVE_ENSURE_SLICE(re_ctx->thr, &re_ctx->bw, data_offset, data_length);
  67461. }
  67462. /*
  67463. * Insert a jump offset at 'offset' to complete an instruction
  67464. * (the jump offset is always the last component of an instruction).
  67465. * The 'skip' argument must be computed relative to 'offset',
  67466. * -without- taking into account the skip field being inserted.
  67467. *
  67468. * ... A B C ins X Y Z ... (ins may be a JUMP, SPLIT1/SPLIT2, etc)
  67469. * => ... A B C ins SKIP X Y Z
  67470. *
  67471. * Computing the final (adjusted) skip value, which is relative to the
  67472. * first byte of the next instruction, is a bit tricky because of the
  67473. * variable length UTF-8 encoding. See doc/regexp.rst for discussion.
  67474. */
  67475. DUK_LOCAL duk_uint32_t duk__insert_jump_offset(duk_re_compiler_ctx *re_ctx, duk_uint32_t offset, duk_int32_t skip) {
  67476. duk_small_int_t len;
  67477. /* XXX: solve into closed form (smaller code) */
  67478. if (skip < 0) {
  67479. /* two encoding attempts suffices */
  67480. len = duk_unicode_get_xutf8_length((duk_codepoint_t) duk__encode_i32(skip));
  67481. len = duk_unicode_get_xutf8_length((duk_codepoint_t) duk__encode_i32(skip - (duk_int32_t) len));
  67482. DUK_ASSERT(duk_unicode_get_xutf8_length(duk__encode_i32(skip - (duk_int32_t) len)) == len); /* no change */
  67483. skip -= (duk_int32_t) len;
  67484. }
  67485. return duk__insert_i32(re_ctx, offset, skip);
  67486. }
  67487. DUK_LOCAL duk_uint32_t duk__append_jump_offset(duk_re_compiler_ctx *re_ctx, duk_int32_t skip) {
  67488. return (duk_uint32_t) duk__insert_jump_offset(re_ctx, (duk_uint32_t) DUK__RE_BUFLEN(re_ctx), skip);
  67489. }
  67490. /*
  67491. * duk_re_range_callback for generating character class ranges.
  67492. *
  67493. * When ignoreCase is false, the range is simply emitted as is.
  67494. * We don't, for instance, eliminate duplicates or overlapping
  67495. * ranges in a character class.
  67496. *
  67497. * When ignoreCase is true, the range needs to be normalized through
  67498. * canonicalization. Unfortunately a canonicalized version of a
  67499. * continuous range is not necessarily continuous (e.g. [x-{] is
  67500. * continuous but [X-{] is not). The current algorithm creates the
  67501. * canonicalized range(s) space efficiently at the cost of compile
  67502. * time execution time (see doc/regexp.rst for discussion).
  67503. *
  67504. * Note that the ctx->nranges is a context-wide temporary value
  67505. * (this is OK because there cannot be multiple character classes
  67506. * being parsed simultaneously).
  67507. */
  67508. DUK_LOCAL void duk__generate_ranges(void *userdata, duk_codepoint_t r1, duk_codepoint_t r2, duk_bool_t direct) {
  67509. duk_re_compiler_ctx *re_ctx = (duk_re_compiler_ctx *) userdata;
  67510. DUK_DD(DUK_DDPRINT("duk__generate_ranges(): re_ctx=%p, range=[%ld,%ld] direct=%ld",
  67511. (void *) re_ctx, (long) r1, (long) r2, (long) direct));
  67512. if (!direct && (re_ctx->re_flags & DUK_RE_FLAG_IGNORE_CASE)) {
  67513. /*
  67514. * Canonicalize a range, generating result ranges as necessary.
  67515. * Needs to exhaustively scan the entire range (at most 65536
  67516. * code points). If 'direct' is set, caller (lexer) has ensured
  67517. * that the range is already canonicalization compatible (this
  67518. * is used to avoid unnecessary canonicalization of built-in
  67519. * ranges like \W, which are not affected by canonicalization).
  67520. *
  67521. * NOTE: here is one place where we don't want to support chars
  67522. * outside the BMP, because the exhaustive search would be
  67523. * massively larger.
  67524. */
  67525. duk_codepoint_t i;
  67526. duk_codepoint_t t;
  67527. duk_codepoint_t r_start, r_end;
  67528. r_start = duk_unicode_re_canonicalize_char(re_ctx->thr, r1);
  67529. r_end = r_start;
  67530. for (i = r1 + 1; i <= r2; i++) {
  67531. t = duk_unicode_re_canonicalize_char(re_ctx->thr, i);
  67532. if (t == r_end + 1) {
  67533. r_end = t;
  67534. } else {
  67535. DUK_DD(DUK_DDPRINT("canonicalized, emit range: [%ld,%ld]", (long) r_start, (long) r_end));
  67536. duk__append_u32(re_ctx, (duk_uint32_t) r_start);
  67537. duk__append_u32(re_ctx, (duk_uint32_t) r_end);
  67538. re_ctx->nranges++;
  67539. r_start = t;
  67540. r_end = t;
  67541. }
  67542. }
  67543. DUK_DD(DUK_DDPRINT("canonicalized, emit range: [%ld,%ld]", (long) r_start, (long) r_end));
  67544. duk__append_u32(re_ctx, (duk_uint32_t) r_start);
  67545. duk__append_u32(re_ctx, (duk_uint32_t) r_end);
  67546. re_ctx->nranges++;
  67547. } else {
  67548. DUK_DD(DUK_DDPRINT("direct, emit range: [%ld,%ld]", (long) r1, (long) r2));
  67549. duk__append_u32(re_ctx, (duk_uint32_t) r1);
  67550. duk__append_u32(re_ctx, (duk_uint32_t) r2);
  67551. re_ctx->nranges++;
  67552. }
  67553. }
  67554. /*
  67555. * Parse regexp Disjunction. Most of regexp compilation happens here.
  67556. *
  67557. * Handles Disjunction, Alternative, and Term productions directly without
  67558. * recursion. The only constructs requiring recursion are positive/negative
  67559. * lookaheads, capturing parentheses, and non-capturing parentheses.
  67560. *
  67561. * The function determines whether the entire disjunction is a 'simple atom'
  67562. * (see doc/regexp.rst discussion on 'simple quantifiers') and if so,
  67563. * returns the atom character length which is needed by the caller to keep
  67564. * track of its own atom character length. A disjunction with more than one
  67565. * alternative is never considered a simple atom (although in some cases
  67566. * that might be the case).
  67567. *
  67568. * Return value: simple atom character length or < 0 if not a simple atom.
  67569. * Appends the bytecode for the disjunction matcher to the end of the temp
  67570. * buffer.
  67571. *
  67572. * Regexp top level structure is:
  67573. *
  67574. * Disjunction = Term*
  67575. * | Term* | Disjunction
  67576. *
  67577. * Term = Assertion
  67578. * | Atom
  67579. * | Atom Quantifier
  67580. *
  67581. * An empty Term sequence is a valid disjunction alternative (e.g. /|||c||/).
  67582. *
  67583. * Notes:
  67584. *
  67585. * * Tracking of the 'simple-ness' of the current atom vs. the entire
  67586. * disjunction are separate matters. For instance, the disjunction
  67587. * may be complex, but individual atoms may be simple. Furthermore,
  67588. * simple quantifiers are used whenever possible, even if the
  67589. * disjunction as a whole is complex.
  67590. *
  67591. * * The estimate of whether an atom is simple is conservative now,
  67592. * and it would be possible to expand it. For instance, captures
  67593. * cause the disjunction to be marked complex, even though captures
  67594. * -can- be handled by simple quantifiers with some minor modifications.
  67595. *
  67596. * * Disjunction 'tainting' as 'complex' is handled at the end of the
  67597. * main for loop collectively for atoms. Assertions, quantifiers,
  67598. * and '|' tokens need to taint the result manually if necessary.
  67599. * Assertions cannot add to result char length, only atoms (and
  67600. * quantifiers) can; currently quantifiers will taint the result
  67601. * as complex though.
  67602. */
  67603. DUK_LOCAL void duk__parse_disjunction(duk_re_compiler_ctx *re_ctx, duk_bool_t expect_eof, duk__re_disjunction_info *out_atom_info) {
  67604. duk_int32_t atom_start_offset = -1; /* negative -> no atom matched on previous round */
  67605. duk_int32_t atom_char_length = 0; /* negative -> complex atom */
  67606. duk_uint32_t atom_start_captures = re_ctx->captures; /* value of re_ctx->captures at start of atom */
  67607. duk_int32_t unpatched_disjunction_split = -1;
  67608. duk_int32_t unpatched_disjunction_jump = -1;
  67609. duk_uint32_t entry_offset = (duk_uint32_t) DUK__RE_BUFLEN(re_ctx);
  67610. duk_int32_t res_charlen = 0; /* -1 if disjunction is complex, char length if simple */
  67611. duk__re_disjunction_info tmp_disj;
  67612. DUK_ASSERT(out_atom_info != NULL);
  67613. if (re_ctx->recursion_depth >= re_ctx->recursion_limit) {
  67614. DUK_ERROR(re_ctx->thr, DUK_ERR_RANGE_ERROR,
  67615. DUK_STR_REGEXP_COMPILER_RECURSION_LIMIT);
  67616. }
  67617. re_ctx->recursion_depth++;
  67618. #if 0
  67619. out_atom_info->start_captures = re_ctx->captures;
  67620. #endif
  67621. for (;;) {
  67622. /* atom_char_length, atom_start_offset, atom_start_offset reflect the
  67623. * atom matched on the previous loop. If a quantifier is encountered
  67624. * on this loop, these are needed to handle the quantifier correctly.
  67625. * new_atom_char_length etc are for the atom parsed on this round;
  67626. * they're written to atom_char_length etc at the end of the round.
  67627. */
  67628. duk_int32_t new_atom_char_length; /* char length of the atom parsed in this loop */
  67629. duk_int32_t new_atom_start_offset; /* bytecode start offset of the atom parsed in this loop
  67630. * (allows quantifiers to copy the atom bytecode)
  67631. */
  67632. duk_uint32_t new_atom_start_captures; /* re_ctx->captures at the start of the atom parsed in this loop */
  67633. duk_lexer_parse_re_token(&re_ctx->lex, &re_ctx->curr_token);
  67634. DUK_DD(DUK_DDPRINT("re token: %ld (num=%ld, char=%c)",
  67635. (long) re_ctx->curr_token.t,
  67636. (long) re_ctx->curr_token.num,
  67637. (re_ctx->curr_token.num >= 0x20 && re_ctx->curr_token.num <= 0x7e) ?
  67638. (int) re_ctx->curr_token.num : (int) '?'));
  67639. /* set by atom case clauses */
  67640. new_atom_start_offset = -1;
  67641. new_atom_char_length = -1;
  67642. new_atom_start_captures = re_ctx->captures;
  67643. switch (re_ctx->curr_token.t) {
  67644. case DUK_RETOK_DISJUNCTION: {
  67645. /*
  67646. * The handling here is a bit tricky. If a previous '|' has been processed,
  67647. * we have a pending split1 and a pending jump (for a previous match). These
  67648. * need to be back-patched carefully. See docs for a detailed example.
  67649. */
  67650. /* patch pending jump and split */
  67651. if (unpatched_disjunction_jump >= 0) {
  67652. duk_uint32_t offset;
  67653. DUK_ASSERT(unpatched_disjunction_split >= 0);
  67654. offset = unpatched_disjunction_jump;
  67655. offset += duk__insert_jump_offset(re_ctx,
  67656. offset,
  67657. (duk_int32_t) (DUK__RE_BUFLEN(re_ctx) - offset));
  67658. /* offset is now target of the pending split (right after jump) */
  67659. duk__insert_jump_offset(re_ctx,
  67660. unpatched_disjunction_split,
  67661. offset - unpatched_disjunction_split);
  67662. }
  67663. /* add a new pending split to the beginning of the entire disjunction */
  67664. (void) duk__insert_u32(re_ctx,
  67665. entry_offset,
  67666. DUK_REOP_SPLIT1); /* prefer direct execution */
  67667. unpatched_disjunction_split = entry_offset + 1; /* +1 for opcode */
  67668. /* add a new pending match jump for latest finished alternative */
  67669. duk__append_u32(re_ctx, DUK_REOP_JUMP);
  67670. unpatched_disjunction_jump = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  67671. /* 'taint' result as complex */
  67672. res_charlen = -1;
  67673. break;
  67674. }
  67675. case DUK_RETOK_QUANTIFIER: {
  67676. if (atom_start_offset < 0) {
  67677. DUK_ERROR(re_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  67678. DUK_STR_INVALID_QUANTIFIER_NO_ATOM);
  67679. }
  67680. if (re_ctx->curr_token.qmin > re_ctx->curr_token.qmax) {
  67681. DUK_ERROR(re_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  67682. DUK_STR_INVALID_QUANTIFIER_VALUES);
  67683. }
  67684. if (atom_char_length >= 0) {
  67685. /*
  67686. * Simple atom
  67687. *
  67688. * If atom_char_length is zero, we'll have unbounded execution time for e.g.
  67689. * /()*x/.exec('x'). We can't just skip the match because it might have some
  67690. * side effects (for instance, if we allowed captures in simple atoms, the
  67691. * capture needs to happen). The simple solution below is to force the
  67692. * quantifier to match at most once, since the additional matches have no effect.
  67693. *
  67694. * With a simple atom there can be no capture groups, so no captures need
  67695. * to be reset.
  67696. */
  67697. duk_int32_t atom_code_length;
  67698. duk_uint32_t offset;
  67699. duk_uint32_t qmin, qmax;
  67700. qmin = re_ctx->curr_token.qmin;
  67701. qmax = re_ctx->curr_token.qmax;
  67702. if (atom_char_length == 0) {
  67703. /* qmin and qmax will be 0 or 1 */
  67704. if (qmin > 1) {
  67705. qmin = 1;
  67706. }
  67707. if (qmax > 1) {
  67708. qmax = 1;
  67709. }
  67710. }
  67711. duk__append_u32(re_ctx, DUK_REOP_MATCH); /* complete 'sub atom' */
  67712. atom_code_length = (duk_int32_t) (DUK__RE_BUFLEN(re_ctx) - atom_start_offset);
  67713. offset = atom_start_offset;
  67714. if (re_ctx->curr_token.greedy) {
  67715. offset += duk__insert_u32(re_ctx, offset, DUK_REOP_SQGREEDY);
  67716. offset += duk__insert_u32(re_ctx, offset, qmin);
  67717. offset += duk__insert_u32(re_ctx, offset, qmax);
  67718. offset += duk__insert_u32(re_ctx, offset, atom_char_length);
  67719. offset += duk__insert_jump_offset(re_ctx, offset, atom_code_length);
  67720. } else {
  67721. offset += duk__insert_u32(re_ctx, offset, DUK_REOP_SQMINIMAL);
  67722. offset += duk__insert_u32(re_ctx, offset, qmin);
  67723. offset += duk__insert_u32(re_ctx, offset, qmax);
  67724. offset += duk__insert_jump_offset(re_ctx, offset, atom_code_length);
  67725. }
  67726. DUK_UNREF(offset); /* silence scan-build warning */
  67727. } else {
  67728. /*
  67729. * Complex atom
  67730. *
  67731. * The original code is used as a template, and removed at the end
  67732. * (this differs from the handling of simple quantifiers).
  67733. *
  67734. * NOTE: there is no current solution for empty atoms in complex
  67735. * quantifiers. This would need some sort of a 'progress' instruction.
  67736. *
  67737. * XXX: impose limit on maximum result size, i.e. atom_code_len * atom_copies?
  67738. */
  67739. duk_int32_t atom_code_length;
  67740. duk_uint32_t atom_copies;
  67741. duk_uint32_t tmp_qmin, tmp_qmax;
  67742. /* pre-check how many atom copies we're willing to make (atom_copies not needed below) */
  67743. atom_copies = (re_ctx->curr_token.qmax == DUK_RE_QUANTIFIER_INFINITE) ?
  67744. re_ctx->curr_token.qmin : re_ctx->curr_token.qmax;
  67745. if (atom_copies > DUK_RE_MAX_ATOM_COPIES) {
  67746. DUK_ERROR(re_ctx->thr, DUK_ERR_RANGE_ERROR,
  67747. DUK_STR_QUANTIFIER_TOO_MANY_COPIES);
  67748. }
  67749. /* wipe the capture range made by the atom (if any) */
  67750. DUK_ASSERT(atom_start_captures <= re_ctx->captures);
  67751. if (atom_start_captures != re_ctx->captures) {
  67752. DUK_ASSERT(atom_start_captures < re_ctx->captures);
  67753. DUK_DDD(DUK_DDDPRINT("must wipe ]atom_start_captures,re_ctx->captures]: ]%ld,%ld]",
  67754. (long) atom_start_captures, (long) re_ctx->captures));
  67755. /* insert (DUK_REOP_WIPERANGE, start, count) in reverse order so the order ends up right */
  67756. duk__insert_u32(re_ctx, atom_start_offset, (re_ctx->captures - atom_start_captures) * 2);
  67757. duk__insert_u32(re_ctx, atom_start_offset, (atom_start_captures + 1) * 2);
  67758. duk__insert_u32(re_ctx, atom_start_offset, DUK_REOP_WIPERANGE);
  67759. } else {
  67760. DUK_DDD(DUK_DDDPRINT("no need to wipe captures: atom_start_captures == re_ctx->captures == %ld",
  67761. (long) atom_start_captures));
  67762. }
  67763. atom_code_length = (duk_int32_t) DUK__RE_BUFLEN(re_ctx) - atom_start_offset;
  67764. /* insert the required matches (qmin) by copying the atom */
  67765. tmp_qmin = re_ctx->curr_token.qmin;
  67766. tmp_qmax = re_ctx->curr_token.qmax;
  67767. while (tmp_qmin > 0) {
  67768. duk__append_slice(re_ctx, atom_start_offset, atom_code_length);
  67769. tmp_qmin--;
  67770. if (tmp_qmax != DUK_RE_QUANTIFIER_INFINITE) {
  67771. tmp_qmax--;
  67772. }
  67773. }
  67774. DUK_ASSERT(tmp_qmin == 0);
  67775. /* insert code for matching the remainder - infinite or finite */
  67776. if (tmp_qmax == DUK_RE_QUANTIFIER_INFINITE) {
  67777. /* reuse last emitted atom for remaining 'infinite' quantifier */
  67778. if (re_ctx->curr_token.qmin == 0) {
  67779. /* Special case: original qmin was zero so there is nothing
  67780. * to repeat. Emit an atom copy but jump over it here.
  67781. */
  67782. duk__append_u32(re_ctx, DUK_REOP_JUMP);
  67783. duk__append_jump_offset(re_ctx, atom_code_length);
  67784. duk__append_slice(re_ctx, atom_start_offset, atom_code_length);
  67785. }
  67786. if (re_ctx->curr_token.greedy) {
  67787. duk__append_u32(re_ctx, DUK_REOP_SPLIT2); /* prefer jump */
  67788. } else {
  67789. duk__append_u32(re_ctx, DUK_REOP_SPLIT1); /* prefer direct */
  67790. }
  67791. duk__append_jump_offset(re_ctx, -atom_code_length - 1); /* -1 for opcode */
  67792. } else {
  67793. /*
  67794. * The remaining matches are emitted as sequence of SPLITs and atom
  67795. * copies; the SPLITs skip the remaining copies and match the sequel.
  67796. * This sequence needs to be emitted starting from the last copy
  67797. * because the SPLITs are variable length due to the variable length
  67798. * skip offset. This causes a lot of memory copying now.
  67799. *
  67800. * Example structure (greedy, match maximum # atoms):
  67801. *
  67802. * SPLIT1 LSEQ
  67803. * (atom)
  67804. * SPLIT1 LSEQ ; <- the byte length of this instruction is needed
  67805. * (atom) ; to encode the above SPLIT1 correctly
  67806. * ...
  67807. * LSEQ:
  67808. */
  67809. duk_uint32_t offset = (duk_uint32_t) DUK__RE_BUFLEN(re_ctx);
  67810. while (tmp_qmax > 0) {
  67811. duk__insert_slice(re_ctx, offset, atom_start_offset, atom_code_length);
  67812. if (re_ctx->curr_token.greedy) {
  67813. duk__insert_u32(re_ctx, offset, DUK_REOP_SPLIT1); /* prefer direct */
  67814. } else {
  67815. duk__insert_u32(re_ctx, offset, DUK_REOP_SPLIT2); /* prefer jump */
  67816. }
  67817. duk__insert_jump_offset(re_ctx,
  67818. offset + 1, /* +1 for opcode */
  67819. (duk_int32_t) (DUK__RE_BUFLEN(re_ctx) - (offset + 1)));
  67820. tmp_qmax--;
  67821. }
  67822. }
  67823. /* remove the original 'template' atom */
  67824. duk__remove_slice(re_ctx, atom_start_offset, atom_code_length);
  67825. }
  67826. /* 'taint' result as complex */
  67827. res_charlen = -1;
  67828. break;
  67829. }
  67830. case DUK_RETOK_ASSERT_START: {
  67831. duk__append_u32(re_ctx, DUK_REOP_ASSERT_START);
  67832. break;
  67833. }
  67834. case DUK_RETOK_ASSERT_END: {
  67835. duk__append_u32(re_ctx, DUK_REOP_ASSERT_END);
  67836. break;
  67837. }
  67838. case DUK_RETOK_ASSERT_WORD_BOUNDARY: {
  67839. duk__append_u32(re_ctx, DUK_REOP_ASSERT_WORD_BOUNDARY);
  67840. break;
  67841. }
  67842. case DUK_RETOK_ASSERT_NOT_WORD_BOUNDARY: {
  67843. duk__append_u32(re_ctx, DUK_REOP_ASSERT_NOT_WORD_BOUNDARY);
  67844. break;
  67845. }
  67846. case DUK_RETOK_ASSERT_START_POS_LOOKAHEAD:
  67847. case DUK_RETOK_ASSERT_START_NEG_LOOKAHEAD: {
  67848. duk_uint32_t offset;
  67849. duk_uint32_t opcode = (re_ctx->curr_token.t == DUK_RETOK_ASSERT_START_POS_LOOKAHEAD) ?
  67850. DUK_REOP_LOOKPOS : DUK_REOP_LOOKNEG;
  67851. offset = (duk_uint32_t) DUK__RE_BUFLEN(re_ctx);
  67852. duk__parse_disjunction(re_ctx, 0, &tmp_disj);
  67853. duk__append_u32(re_ctx, DUK_REOP_MATCH);
  67854. (void) duk__insert_u32(re_ctx, offset, opcode);
  67855. (void) duk__insert_jump_offset(re_ctx,
  67856. offset + 1, /* +1 for opcode */
  67857. (duk_int32_t) (DUK__RE_BUFLEN(re_ctx) - (offset + 1)));
  67858. /* 'taint' result as complex -- this is conservative,
  67859. * as lookaheads do not backtrack.
  67860. */
  67861. res_charlen = -1;
  67862. break;
  67863. }
  67864. case DUK_RETOK_ATOM_PERIOD: {
  67865. new_atom_char_length = 1;
  67866. new_atom_start_offset = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  67867. duk__append_u32(re_ctx, DUK_REOP_PERIOD);
  67868. break;
  67869. }
  67870. case DUK_RETOK_ATOM_CHAR: {
  67871. /* Note: successive characters could be joined into string matches
  67872. * but this is not trivial (consider e.g. '/xyz+/); see docs for
  67873. * more discussion.
  67874. */
  67875. duk_uint32_t ch;
  67876. new_atom_char_length = 1;
  67877. new_atom_start_offset = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  67878. duk__append_u32(re_ctx, DUK_REOP_CHAR);
  67879. ch = re_ctx->curr_token.num;
  67880. if (re_ctx->re_flags & DUK_RE_FLAG_IGNORE_CASE) {
  67881. ch = duk_unicode_re_canonicalize_char(re_ctx->thr, ch);
  67882. }
  67883. duk__append_u32(re_ctx, ch);
  67884. break;
  67885. }
  67886. case DUK_RETOK_ATOM_DIGIT:
  67887. case DUK_RETOK_ATOM_NOT_DIGIT: {
  67888. new_atom_char_length = 1;
  67889. new_atom_start_offset = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  67890. duk__append_u32(re_ctx,
  67891. (re_ctx->curr_token.t == DUK_RETOK_ATOM_DIGIT) ?
  67892. DUK_REOP_RANGES : DUK_REOP_INVRANGES);
  67893. duk__append_u32(re_ctx, sizeof(duk_unicode_re_ranges_digit) / (2 * sizeof(duk_uint16_t)));
  67894. duk__append_u16_list(re_ctx, duk_unicode_re_ranges_digit, sizeof(duk_unicode_re_ranges_digit) / sizeof(duk_uint16_t));
  67895. break;
  67896. }
  67897. case DUK_RETOK_ATOM_WHITE:
  67898. case DUK_RETOK_ATOM_NOT_WHITE: {
  67899. new_atom_char_length = 1;
  67900. new_atom_start_offset = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  67901. duk__append_u32(re_ctx,
  67902. (re_ctx->curr_token.t == DUK_RETOK_ATOM_WHITE) ?
  67903. DUK_REOP_RANGES : DUK_REOP_INVRANGES);
  67904. duk__append_u32(re_ctx, sizeof(duk_unicode_re_ranges_white) / (2 * sizeof(duk_uint16_t)));
  67905. duk__append_u16_list(re_ctx, duk_unicode_re_ranges_white, sizeof(duk_unicode_re_ranges_white) / sizeof(duk_uint16_t));
  67906. break;
  67907. }
  67908. case DUK_RETOK_ATOM_WORD_CHAR:
  67909. case DUK_RETOK_ATOM_NOT_WORD_CHAR: {
  67910. new_atom_char_length = 1;
  67911. new_atom_start_offset = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  67912. duk__append_u32(re_ctx,
  67913. (re_ctx->curr_token.t == DUK_RETOK_ATOM_WORD_CHAR) ?
  67914. DUK_REOP_RANGES : DUK_REOP_INVRANGES);
  67915. duk__append_u32(re_ctx, sizeof(duk_unicode_re_ranges_wordchar) / (2 * sizeof(duk_uint16_t)));
  67916. duk__append_u16_list(re_ctx, duk_unicode_re_ranges_wordchar, sizeof(duk_unicode_re_ranges_wordchar) / sizeof(duk_uint16_t));
  67917. break;
  67918. }
  67919. case DUK_RETOK_ATOM_BACKREFERENCE: {
  67920. duk_uint32_t backref = (duk_uint32_t) re_ctx->curr_token.num;
  67921. if (backref > re_ctx->highest_backref) {
  67922. re_ctx->highest_backref = backref;
  67923. }
  67924. new_atom_char_length = -1; /* mark as complex */
  67925. new_atom_start_offset = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  67926. duk__append_u32(re_ctx, DUK_REOP_BACKREFERENCE);
  67927. duk__append_u32(re_ctx, backref);
  67928. break;
  67929. }
  67930. case DUK_RETOK_ATOM_START_CAPTURE_GROUP: {
  67931. duk_uint32_t cap;
  67932. new_atom_char_length = -1; /* mark as complex (capture handling) */
  67933. new_atom_start_offset = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  67934. cap = ++re_ctx->captures;
  67935. duk__append_u32(re_ctx, DUK_REOP_SAVE);
  67936. duk__append_u32(re_ctx, cap * 2);
  67937. duk__parse_disjunction(re_ctx, 0, &tmp_disj); /* retval (sub-atom char length) unused, tainted as complex above */
  67938. duk__append_u32(re_ctx, DUK_REOP_SAVE);
  67939. duk__append_u32(re_ctx, cap * 2 + 1);
  67940. break;
  67941. }
  67942. case DUK_RETOK_ATOM_START_NONCAPTURE_GROUP: {
  67943. new_atom_start_offset = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  67944. duk__parse_disjunction(re_ctx, 0, &tmp_disj);
  67945. new_atom_char_length = tmp_disj.charlen;
  67946. break;
  67947. }
  67948. case DUK_RETOK_ATOM_START_CHARCLASS:
  67949. case DUK_RETOK_ATOM_START_CHARCLASS_INVERTED: {
  67950. /*
  67951. * Range parsing is done with a special lexer function which calls
  67952. * us for every range parsed. This is different from how rest of
  67953. * the parsing works, but avoids a heavy, arbitrary size intermediate
  67954. * value type to hold the ranges.
  67955. *
  67956. * Another complication is the handling of character ranges when
  67957. * case insensitive matching is used (see docs for discussion).
  67958. * The range handler callback given to the lexer takes care of this
  67959. * as well.
  67960. *
  67961. * Note that duplicate ranges are not eliminated when parsing character
  67962. * classes, so that canonicalization of
  67963. *
  67964. * [0-9a-fA-Fx-{]
  67965. *
  67966. * creates the result (note the duplicate ranges):
  67967. *
  67968. * [0-9A-FA-FX-Z{-{]
  67969. *
  67970. * where [x-{] is split as a result of canonicalization. The duplicate
  67971. * ranges are not a semantics issue: they work correctly.
  67972. */
  67973. duk_uint32_t offset;
  67974. DUK_DD(DUK_DDPRINT("character class"));
  67975. /* insert ranges instruction, range count patched in later */
  67976. new_atom_char_length = 1;
  67977. new_atom_start_offset = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  67978. duk__append_u32(re_ctx,
  67979. (re_ctx->curr_token.t == DUK_RETOK_ATOM_START_CHARCLASS) ?
  67980. DUK_REOP_RANGES : DUK_REOP_INVRANGES);
  67981. offset = (duk_uint32_t) DUK__RE_BUFLEN(re_ctx); /* patch in range count later */
  67982. /* parse ranges until character class ends */
  67983. re_ctx->nranges = 0; /* note: ctx-wide temporary */
  67984. duk_lexer_parse_re_ranges(&re_ctx->lex, duk__generate_ranges, (void *) re_ctx);
  67985. /* insert range count */
  67986. duk__insert_u32(re_ctx, offset, re_ctx->nranges);
  67987. break;
  67988. }
  67989. case DUK_RETOK_ATOM_END_GROUP: {
  67990. if (expect_eof) {
  67991. DUK_ERROR(re_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  67992. DUK_STR_UNEXPECTED_CLOSING_PAREN);
  67993. }
  67994. goto done;
  67995. }
  67996. case DUK_RETOK_EOF: {
  67997. if (!expect_eof) {
  67998. DUK_ERROR(re_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  67999. DUK_STR_UNEXPECTED_END_OF_PATTERN);
  68000. }
  68001. goto done;
  68002. }
  68003. default: {
  68004. DUK_ERROR(re_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  68005. DUK_STR_UNEXPECTED_REGEXP_TOKEN);
  68006. }
  68007. }
  68008. /* a complex (new) atom taints the result */
  68009. if (new_atom_start_offset >= 0) {
  68010. if (new_atom_char_length < 0) {
  68011. res_charlen = -1;
  68012. } else if (res_charlen >= 0) {
  68013. /* only advance if not tainted */
  68014. res_charlen += new_atom_char_length;
  68015. }
  68016. }
  68017. /* record previous atom info in case next token is a quantifier */
  68018. atom_start_offset = new_atom_start_offset;
  68019. atom_char_length = new_atom_char_length;
  68020. atom_start_captures = new_atom_start_captures;
  68021. }
  68022. done:
  68023. /* finish up pending jump and split for last alternative */
  68024. if (unpatched_disjunction_jump >= 0) {
  68025. duk_uint32_t offset;
  68026. DUK_ASSERT(unpatched_disjunction_split >= 0);
  68027. offset = unpatched_disjunction_jump;
  68028. offset += duk__insert_jump_offset(re_ctx,
  68029. offset,
  68030. (duk_int32_t) (DUK__RE_BUFLEN(re_ctx) - offset));
  68031. /* offset is now target of the pending split (right after jump) */
  68032. duk__insert_jump_offset(re_ctx,
  68033. unpatched_disjunction_split,
  68034. offset - unpatched_disjunction_split);
  68035. }
  68036. #if 0
  68037. out_atom_info->end_captures = re_ctx->captures;
  68038. #endif
  68039. out_atom_info->charlen = res_charlen;
  68040. DUK_DDD(DUK_DDDPRINT("parse disjunction finished: charlen=%ld",
  68041. (long) out_atom_info->charlen));
  68042. re_ctx->recursion_depth--;
  68043. }
  68044. /*
  68045. * Flags parsing (see E5 Section 15.10.4.1).
  68046. */
  68047. DUK_LOCAL duk_uint32_t duk__parse_regexp_flags(duk_hthread *thr, duk_hstring *h) {
  68048. const duk_uint8_t *p;
  68049. const duk_uint8_t *p_end;
  68050. duk_uint32_t flags = 0;
  68051. p = DUK_HSTRING_GET_DATA(h);
  68052. p_end = p + DUK_HSTRING_GET_BYTELEN(h);
  68053. /* Note: can be safely scanned as bytes (undecoded) */
  68054. while (p < p_end) {
  68055. duk_uint8_t c = *p++;
  68056. switch ((int) c) {
  68057. case (int) 'g': {
  68058. if (flags & DUK_RE_FLAG_GLOBAL) {
  68059. goto error;
  68060. }
  68061. flags |= DUK_RE_FLAG_GLOBAL;
  68062. break;
  68063. }
  68064. case (int) 'i': {
  68065. if (flags & DUK_RE_FLAG_IGNORE_CASE) {
  68066. goto error;
  68067. }
  68068. flags |= DUK_RE_FLAG_IGNORE_CASE;
  68069. break;
  68070. }
  68071. case (int) 'm': {
  68072. if (flags & DUK_RE_FLAG_MULTILINE) {
  68073. goto error;
  68074. }
  68075. flags |= DUK_RE_FLAG_MULTILINE;
  68076. break;
  68077. }
  68078. default: {
  68079. goto error;
  68080. }
  68081. }
  68082. }
  68083. return flags;
  68084. error:
  68085. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_REGEXP_FLAGS);
  68086. return 0; /* never here */
  68087. }
  68088. /*
  68089. * Create escaped RegExp source (E5 Section 15.10.3).
  68090. *
  68091. * The current approach is to special case the empty RegExp
  68092. * ('' -> '(?:)') and otherwise replace unescaped '/' characters
  68093. * with '\/' regardless of where they occur in the regexp.
  68094. *
  68095. * Note that normalization does not seem to be necessary for
  68096. * RegExp literals (e.g. '/foo/') because to be acceptable as
  68097. * a RegExp literal, the text between forward slashes must
  68098. * already match the escaping requirements (e.g. must not contain
  68099. * unescaped forward slashes or be empty). Escaping IS needed
  68100. * for expressions like 'new Regexp("...", "")' however.
  68101. * Currently, we re-escape in either case.
  68102. *
  68103. * Also note that we process the source here in UTF-8 encoded
  68104. * form. This is correct, because any non-ASCII characters are
  68105. * passed through without change.
  68106. */
  68107. DUK_LOCAL void duk__create_escaped_source(duk_hthread *thr, int idx_pattern) {
  68108. duk_context *ctx = (duk_context *) thr;
  68109. duk_hstring *h;
  68110. const duk_uint8_t *p;
  68111. duk_bufwriter_ctx bw_alloc;
  68112. duk_bufwriter_ctx *bw;
  68113. duk_uint8_t *q;
  68114. duk_size_t i, n;
  68115. duk_uint_fast8_t c_prev, c;
  68116. h = duk_get_hstring(ctx, idx_pattern);
  68117. DUK_ASSERT(h != NULL);
  68118. p = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h);
  68119. n = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h);
  68120. if (n == 0) {
  68121. /* return '(?:)' */
  68122. duk_push_hstring_stridx(ctx, DUK_STRIDX_ESCAPED_EMPTY_REGEXP);
  68123. return;
  68124. }
  68125. bw = &bw_alloc;
  68126. DUK_BW_INIT_PUSHBUF(thr, bw, n);
  68127. q = DUK_BW_GET_PTR(thr, bw);
  68128. c_prev = (duk_uint_fast8_t) 0;
  68129. for (i = 0; i < n; i++) {
  68130. c = p[i];
  68131. q = DUK_BW_ENSURE_RAW(thr, bw, 2, q);
  68132. if (c == (duk_uint_fast8_t) '/' && c_prev != (duk_uint_fast8_t) '\\') {
  68133. /* Unescaped '/' ANYWHERE in the regexp (in disjunction,
  68134. * inside a character class, ...) => same escape works.
  68135. */
  68136. *q++ = DUK_ASC_BACKSLASH;
  68137. }
  68138. *q++ = (duk_uint8_t) c;
  68139. c_prev = c;
  68140. }
  68141. DUK_BW_SETPTR_AND_COMPACT(thr, bw, q);
  68142. duk_to_string(ctx, -1); /* -> [ ... escaped_source ] */
  68143. }
  68144. /*
  68145. * Exposed regexp compilation primitive.
  68146. *
  68147. * Sets up a regexp compilation context, and calls duk__parse_disjunction() to do the
  68148. * actual parsing. Handles generation of the compiled regexp header and the
  68149. * "boilerplate" capture of the matching substring (save 0 and 1). Also does some
  68150. * global level regexp checks after recursive compilation has finished.
  68151. *
  68152. * An escaped version of the regexp source, suitable for use as a RegExp instance
  68153. * 'source' property (see E5 Section 15.10.3), is also left on the stack.
  68154. *
  68155. * Input stack: [ pattern flags ]
  68156. * Output stack: [ bytecode escaped_source ] (both as strings)
  68157. */
  68158. DUK_INTERNAL void duk_regexp_compile(duk_hthread *thr) {
  68159. duk_context *ctx = (duk_context *) thr;
  68160. duk_re_compiler_ctx re_ctx;
  68161. duk_lexer_point lex_point;
  68162. duk_hstring *h_pattern;
  68163. duk_hstring *h_flags;
  68164. duk__re_disjunction_info ign_disj;
  68165. DUK_ASSERT(thr != NULL);
  68166. DUK_ASSERT(ctx != NULL);
  68167. /*
  68168. * Args validation
  68169. */
  68170. /* TypeError if fails */
  68171. h_pattern = duk_require_hstring(ctx, -2);
  68172. h_flags = duk_require_hstring(ctx, -1);
  68173. /*
  68174. * Create normalized 'source' property (E5 Section 15.10.3).
  68175. */
  68176. /* [ ... pattern flags ] */
  68177. duk__create_escaped_source(thr, -2);
  68178. /* [ ... pattern flags escaped_source ] */
  68179. /*
  68180. * Init compilation context
  68181. */
  68182. /* [ ... pattern flags escaped_source buffer ] */
  68183. DUK_MEMZERO(&re_ctx, sizeof(re_ctx));
  68184. DUK_LEXER_INITCTX(&re_ctx.lex); /* duplicate zeroing, expect for (possible) NULL inits */
  68185. re_ctx.thr = thr;
  68186. re_ctx.lex.thr = thr;
  68187. re_ctx.lex.input = DUK_HSTRING_GET_DATA(h_pattern);
  68188. re_ctx.lex.input_length = DUK_HSTRING_GET_BYTELEN(h_pattern);
  68189. re_ctx.lex.token_limit = DUK_RE_COMPILE_TOKEN_LIMIT;
  68190. re_ctx.recursion_limit = DUK_USE_REGEXP_COMPILER_RECLIMIT;
  68191. re_ctx.re_flags = duk__parse_regexp_flags(thr, h_flags);
  68192. DUK_BW_INIT_PUSHBUF(thr, &re_ctx.bw, DUK__RE_INITIAL_BUFSIZE);
  68193. DUK_DD(DUK_DDPRINT("regexp compiler ctx initialized, flags=0x%08lx, recursion_limit=%ld",
  68194. (unsigned long) re_ctx.re_flags, (long) re_ctx.recursion_limit));
  68195. /*
  68196. * Init lexer
  68197. */
  68198. lex_point.offset = 0; /* expensive init, just want to fill window */
  68199. lex_point.line = 1;
  68200. DUK_LEXER_SETPOINT(&re_ctx.lex, &lex_point);
  68201. /*
  68202. * Compilation
  68203. */
  68204. DUK_D(DUK_DPRINT("starting regexp compilation"));
  68205. duk__append_u32(&re_ctx, DUK_REOP_SAVE);
  68206. duk__append_u32(&re_ctx, 0);
  68207. duk__parse_disjunction(&re_ctx, 1 /*expect_eof*/, &ign_disj);
  68208. duk__append_u32(&re_ctx, DUK_REOP_SAVE);
  68209. duk__append_u32(&re_ctx, 1);
  68210. duk__append_u32(&re_ctx, DUK_REOP_MATCH);
  68211. /*
  68212. * Check for invalid backreferences; note that it is NOT an error
  68213. * to back-reference a capture group which has not yet been introduced
  68214. * in the pattern (as in /\1(foo)/); in fact, the backreference will
  68215. * always match! It IS an error to back-reference a capture group
  68216. * which will never be introduced in the pattern. Thus, we can check
  68217. * for such references only after parsing is complete.
  68218. */
  68219. if (re_ctx.highest_backref > re_ctx.captures) {
  68220. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_BACKREFS);
  68221. }
  68222. /*
  68223. * Emit compiled regexp header: flags, ncaptures
  68224. * (insertion order inverted on purpose)
  68225. */
  68226. duk__insert_u32(&re_ctx, 0, (re_ctx.captures + 1) * 2);
  68227. duk__insert_u32(&re_ctx, 0, re_ctx.re_flags);
  68228. /* [ ... pattern flags escaped_source buffer ] */
  68229. DUK_BW_COMPACT(thr, &re_ctx.bw);
  68230. duk_to_string(ctx, -1); /* coerce to string */
  68231. /* [ ... pattern flags escaped_source bytecode ] */
  68232. /*
  68233. * Finalize stack
  68234. */
  68235. duk_remove(ctx, -4); /* -> [ ... flags escaped_source bytecode ] */
  68236. duk_remove(ctx, -3); /* -> [ ... escaped_source bytecode ] */
  68237. DUK_D(DUK_DPRINT("regexp compilation successful, bytecode: %!T, escaped source: %!T",
  68238. (duk_tval *) duk_get_tval(ctx, -1), (duk_tval *) duk_get_tval(ctx, -2)));
  68239. }
  68240. /*
  68241. * Create a RegExp instance (E5 Section 15.10.7).
  68242. *
  68243. * Note: the output stack left by duk_regexp_compile() is directly compatible
  68244. * with the input here.
  68245. *
  68246. * Input stack: [ escaped_source bytecode ] (both as strings)
  68247. * Output stack: [ RegExp ]
  68248. */
  68249. DUK_INTERNAL void duk_regexp_create_instance(duk_hthread *thr) {
  68250. duk_context *ctx = (duk_context *) thr;
  68251. duk_hobject *h;
  68252. duk_hstring *h_bc;
  68253. duk_small_int_t re_flags;
  68254. /* [ ... escape_source bytecode ] */
  68255. h_bc = duk_get_hstring(ctx, -1);
  68256. DUK_ASSERT(h_bc != NULL);
  68257. DUK_ASSERT(DUK_HSTRING_GET_BYTELEN(h_bc) >= 1); /* always at least the header */
  68258. DUK_ASSERT(DUK_HSTRING_GET_CHARLEN(h_bc) >= 1);
  68259. DUK_ASSERT((duk_small_int_t) DUK_HSTRING_GET_DATA(h_bc)[0] < 0x80); /* flags always encodes to 1 byte */
  68260. re_flags = (duk_small_int_t) DUK_HSTRING_GET_DATA(h_bc)[0];
  68261. /* [ ... escaped_source bytecode ] */
  68262. duk_push_object(ctx);
  68263. h = duk_get_hobject(ctx, -1);
  68264. DUK_ASSERT(h != NULL);
  68265. duk_insert(ctx, -3);
  68266. /* [ ... regexp_object escaped_source bytecode ] */
  68267. DUK_HOBJECT_SET_CLASS_NUMBER(h, DUK_HOBJECT_CLASS_REGEXP);
  68268. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, thr->builtins[DUK_BIDX_REGEXP_PROTOTYPE]);
  68269. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_INT_BYTECODE, DUK_PROPDESC_FLAGS_NONE);
  68270. /* [ ... regexp_object escaped_source ] */
  68271. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_SOURCE, DUK_PROPDESC_FLAGS_NONE);
  68272. /* [ ... regexp_object ] */
  68273. duk_push_boolean(ctx, (re_flags & DUK_RE_FLAG_GLOBAL));
  68274. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_GLOBAL, DUK_PROPDESC_FLAGS_NONE);
  68275. duk_push_boolean(ctx, (re_flags & DUK_RE_FLAG_IGNORE_CASE));
  68276. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_IGNORE_CASE, DUK_PROPDESC_FLAGS_NONE);
  68277. duk_push_boolean(ctx, (re_flags & DUK_RE_FLAG_MULTILINE));
  68278. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_MULTILINE, DUK_PROPDESC_FLAGS_NONE);
  68279. duk_push_int(ctx, 0);
  68280. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LAST_INDEX, DUK_PROPDESC_FLAGS_W);
  68281. /* [ ... regexp_object ] */
  68282. }
  68283. #undef DUK__RE_BUFLEN
  68284. #else /* DUK_USE_REGEXP_SUPPORT */
  68285. /* regexp support disabled */
  68286. #endif /* DUK_USE_REGEXP_SUPPORT */
  68287. #line 1 "duk_regexp_executor.c"
  68288. /*
  68289. * Regexp executor.
  68290. *
  68291. * Safety: the Ecmascript executor should prevent user from reading and
  68292. * replacing regexp bytecode. Even so, the executor must validate all
  68293. * memory accesses etc. When an invalid access is detected (e.g. a 'save'
  68294. * opcode to invalid, unallocated index) it should fail with an internal
  68295. * error but not cause a segmentation fault.
  68296. *
  68297. * Notes:
  68298. *
  68299. * - Backtrack counts are limited to unsigned 32 bits but should
  68300. * technically be duk_size_t for strings longer than 4G chars.
  68301. * This also requires a regexp bytecode change.
  68302. */
  68303. /* include removed: duk_internal.h */
  68304. #ifdef DUK_USE_REGEXP_SUPPORT
  68305. /*
  68306. * Helpers for UTF-8 handling
  68307. *
  68308. * For bytecode readers the duk_uint32_t and duk_int32_t types are correct
  68309. * because they're used for more than just codepoints.
  68310. */
  68311. DUK_LOCAL duk_uint32_t duk__bc_get_u32(duk_re_matcher_ctx *re_ctx, const duk_uint8_t **pc) {
  68312. return (duk_uint32_t) duk_unicode_decode_xutf8_checked(re_ctx->thr, pc, re_ctx->bytecode, re_ctx->bytecode_end);
  68313. }
  68314. DUK_LOCAL duk_int32_t duk__bc_get_i32(duk_re_matcher_ctx *re_ctx, const duk_uint8_t **pc) {
  68315. duk_uint32_t t;
  68316. /* signed integer encoding needed to work with UTF-8 */
  68317. t = (duk_uint32_t) duk_unicode_decode_xutf8_checked(re_ctx->thr, pc, re_ctx->bytecode, re_ctx->bytecode_end);
  68318. if (t & 1) {
  68319. return -((duk_int32_t) (t >> 1));
  68320. } else {
  68321. return (duk_int32_t) (t >> 1);
  68322. }
  68323. }
  68324. 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) {
  68325. const duk_uint8_t *p;
  68326. /* Note: allow backtracking from p == ptr_end */
  68327. p = *ptr;
  68328. if (p < ptr_start || p > ptr_end) {
  68329. goto fail;
  68330. }
  68331. while (count > 0) {
  68332. for (;;) {
  68333. p--;
  68334. if (p < ptr_start) {
  68335. goto fail;
  68336. }
  68337. if ((*p & 0xc0) != 0x80) {
  68338. /* utf-8 continuation bytes have the form 10xx xxxx */
  68339. break;
  68340. }
  68341. }
  68342. count--;
  68343. }
  68344. *ptr = p;
  68345. return p;
  68346. fail:
  68347. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_REGEXP_BACKTRACK_FAILED);
  68348. return NULL; /* never here */
  68349. }
  68350. 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) {
  68351. const duk_uint8_t *p;
  68352. p = *ptr;
  68353. if (p < ptr_start || p >= ptr_end) {
  68354. goto fail;
  68355. }
  68356. while (count > 0) {
  68357. for (;;) {
  68358. p++;
  68359. /* Note: if encoding ends by hitting end of input, we don't check that
  68360. * the encoding is valid, we just assume it is.
  68361. */
  68362. if (p >= ptr_end || ((*p & 0xc0) != 0x80)) {
  68363. /* utf-8 continuation bytes have the form 10xx xxxx */
  68364. break;
  68365. }
  68366. }
  68367. count--;
  68368. }
  68369. *ptr = p;
  68370. return p;
  68371. fail:
  68372. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_REGEXP_ADVANCE_FAILED);
  68373. return NULL; /* never here */
  68374. }
  68375. /*
  68376. * Helpers for dealing with the input string
  68377. */
  68378. /* Get a (possibly canonicalized) input character from current sp. The input
  68379. * itself is never modified, and captures always record non-canonicalized
  68380. * characters even in case-insensitive matching.
  68381. */
  68382. DUK_LOCAL duk_codepoint_t duk__inp_get_cp(duk_re_matcher_ctx *re_ctx, const duk_uint8_t **sp) {
  68383. duk_codepoint_t res = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(re_ctx->thr, sp, re_ctx->input, re_ctx->input_end);
  68384. if (re_ctx->re_flags & DUK_RE_FLAG_IGNORE_CASE) {
  68385. res = duk_unicode_re_canonicalize_char(re_ctx->thr, res);
  68386. }
  68387. return res;
  68388. }
  68389. 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) {
  68390. return duk__utf8_backtrack(re_ctx->thr, sp, re_ctx->input, re_ctx->input_end, count);
  68391. }
  68392. /* Backtrack utf-8 input and return a (possibly canonicalized) input character. */
  68393. DUK_LOCAL duk_codepoint_t duk__inp_get_prev_cp(duk_re_matcher_ctx *re_ctx, const duk_uint8_t *sp) {
  68394. /* note: caller 'sp' is intentionally not updated here */
  68395. (void) duk__inp_backtrack(re_ctx, &sp, (duk_uint_fast32_t) 1);
  68396. return duk__inp_get_cp(re_ctx, &sp);
  68397. }
  68398. /*
  68399. * Regexp recursive matching function.
  68400. *
  68401. * Returns 'sp' on successful match (points to character after last matched one),
  68402. * NULL otherwise.
  68403. *
  68404. * The C recursion depth limit check is only performed in this function, this
  68405. * suffices because the function is present in all true recursion required by
  68406. * regexp execution.
  68407. */
  68408. 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) {
  68409. if (re_ctx->recursion_depth >= re_ctx->recursion_limit) {
  68410. DUK_ERROR(re_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_REGEXP_EXECUTOR_RECURSION_LIMIT);
  68411. }
  68412. re_ctx->recursion_depth++;
  68413. for (;;) {
  68414. duk_small_int_t op;
  68415. if (re_ctx->steps_count >= re_ctx->steps_limit) {
  68416. DUK_ERROR(re_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_REGEXP_EXECUTOR_STEP_LIMIT);
  68417. }
  68418. re_ctx->steps_count++;
  68419. op = (duk_small_int_t) duk__bc_get_u32(re_ctx, &pc);
  68420. DUK_DDD(DUK_DDDPRINT("match: rec=%ld, steps=%ld, pc (after op)=%ld, sp=%ld, op=%ld",
  68421. (long) re_ctx->recursion_depth,
  68422. (long) re_ctx->steps_count,
  68423. (long) (pc - re_ctx->bytecode),
  68424. (long) (sp - re_ctx->input),
  68425. (long) op));
  68426. switch (op) {
  68427. case DUK_REOP_MATCH: {
  68428. goto match;
  68429. }
  68430. case DUK_REOP_CHAR: {
  68431. /*
  68432. * Byte-based matching would be possible for case-sensitive
  68433. * matching but not for case-insensitive matching. So, we
  68434. * match by decoding the input and bytecode character normally.
  68435. *
  68436. * Bytecode characters are assumed to be already canonicalized.
  68437. * Input characters are canonicalized automatically by
  68438. * duk__inp_get_cp() if necessary.
  68439. *
  68440. * There is no opcode for matching multiple characters. The
  68441. * regexp compiler has trouble joining strings efficiently
  68442. * during compilation. See doc/regexp.rst for more discussion.
  68443. */
  68444. duk_codepoint_t c1, c2;
  68445. c1 = (duk_codepoint_t) duk__bc_get_u32(re_ctx, &pc);
  68446. DUK_ASSERT(!(re_ctx->re_flags & DUK_RE_FLAG_IGNORE_CASE) ||
  68447. c1 == duk_unicode_re_canonicalize_char(re_ctx->thr, c1)); /* canonicalized by compiler */
  68448. if (sp >= re_ctx->input_end) {
  68449. goto fail;
  68450. }
  68451. c2 = duk__inp_get_cp(re_ctx, &sp);
  68452. DUK_DDD(DUK_DDDPRINT("char match, c1=%ld, c2=%ld", (long) c1, (long) c2));
  68453. if (c1 != c2) {
  68454. goto fail;
  68455. }
  68456. break;
  68457. }
  68458. case DUK_REOP_PERIOD: {
  68459. duk_codepoint_t c;
  68460. if (sp >= re_ctx->input_end) {
  68461. goto fail;
  68462. }
  68463. c = duk__inp_get_cp(re_ctx, &sp);
  68464. if (duk_unicode_is_line_terminator(c)) {
  68465. /* E5 Sections 15.10.2.8, 7.3 */
  68466. goto fail;
  68467. }
  68468. break;
  68469. }
  68470. case DUK_REOP_RANGES:
  68471. case DUK_REOP_INVRANGES: {
  68472. duk_uint32_t n;
  68473. duk_codepoint_t c;
  68474. duk_small_int_t match;
  68475. n = duk__bc_get_u32(re_ctx, &pc);
  68476. if (sp >= re_ctx->input_end) {
  68477. goto fail;
  68478. }
  68479. c = duk__inp_get_cp(re_ctx, &sp);
  68480. match = 0;
  68481. while (n) {
  68482. duk_codepoint_t r1, r2;
  68483. r1 = (duk_codepoint_t) duk__bc_get_u32(re_ctx, &pc);
  68484. r2 = (duk_codepoint_t) duk__bc_get_u32(re_ctx, &pc);
  68485. DUK_DDD(DUK_DDDPRINT("matching ranges/invranges, n=%ld, r1=%ld, r2=%ld, c=%ld",
  68486. (long) n, (long) r1, (long) r2, (long) c));
  68487. if (c >= r1 && c <= r2) {
  68488. /* Note: don't bail out early, we must read all the ranges from
  68489. * bytecode. Another option is to skip them efficiently after
  68490. * breaking out of here. Prefer smallest code.
  68491. */
  68492. match = 1;
  68493. }
  68494. n--;
  68495. }
  68496. if (op == DUK_REOP_RANGES) {
  68497. if (!match) {
  68498. goto fail;
  68499. }
  68500. } else {
  68501. DUK_ASSERT(op == DUK_REOP_INVRANGES);
  68502. if (match) {
  68503. goto fail;
  68504. }
  68505. }
  68506. break;
  68507. }
  68508. case DUK_REOP_ASSERT_START: {
  68509. duk_codepoint_t c;
  68510. if (sp <= re_ctx->input) {
  68511. break;
  68512. }
  68513. if (!(re_ctx->re_flags & DUK_RE_FLAG_MULTILINE)) {
  68514. goto fail;
  68515. }
  68516. c = duk__inp_get_prev_cp(re_ctx, sp);
  68517. if (duk_unicode_is_line_terminator(c)) {
  68518. /* E5 Sections 15.10.2.8, 7.3 */
  68519. break;
  68520. }
  68521. goto fail;
  68522. }
  68523. case DUK_REOP_ASSERT_END: {
  68524. duk_codepoint_t c;
  68525. const duk_uint8_t *tmp_sp;
  68526. if (sp >= re_ctx->input_end) {
  68527. break;
  68528. }
  68529. if (!(re_ctx->re_flags & DUK_RE_FLAG_MULTILINE)) {
  68530. goto fail;
  68531. }
  68532. tmp_sp = sp;
  68533. c = duk__inp_get_cp(re_ctx, &tmp_sp);
  68534. if (duk_unicode_is_line_terminator(c)) {
  68535. /* E5 Sections 15.10.2.8, 7.3 */
  68536. break;
  68537. }
  68538. goto fail;
  68539. }
  68540. case DUK_REOP_ASSERT_WORD_BOUNDARY:
  68541. case DUK_REOP_ASSERT_NOT_WORD_BOUNDARY: {
  68542. /*
  68543. * E5 Section 15.10.2.6. The previous and current character
  68544. * should -not- be canonicalized as they are now. However,
  68545. * canonicalization does not affect the result of IsWordChar()
  68546. * (which depends on Unicode characters never canonicalizing
  68547. * into ASCII characters) so this does not matter.
  68548. */
  68549. duk_small_int_t w1, w2;
  68550. if (sp <= re_ctx->input) {
  68551. w1 = 0; /* not a wordchar */
  68552. } else {
  68553. duk_codepoint_t c;
  68554. c = duk__inp_get_prev_cp(re_ctx, sp);
  68555. w1 = duk_unicode_re_is_wordchar(c);
  68556. }
  68557. if (sp >= re_ctx->input_end) {
  68558. w2 = 0; /* not a wordchar */
  68559. } else {
  68560. const duk_uint8_t *tmp_sp = sp; /* dummy so sp won't get updated */
  68561. duk_codepoint_t c;
  68562. c = duk__inp_get_cp(re_ctx, &tmp_sp);
  68563. w2 = duk_unicode_re_is_wordchar(c);
  68564. }
  68565. if (op == DUK_REOP_ASSERT_WORD_BOUNDARY) {
  68566. if (w1 == w2) {
  68567. goto fail;
  68568. }
  68569. } else {
  68570. DUK_ASSERT(op == DUK_REOP_ASSERT_NOT_WORD_BOUNDARY);
  68571. if (w1 != w2) {
  68572. goto fail;
  68573. }
  68574. }
  68575. break;
  68576. }
  68577. case DUK_REOP_JUMP: {
  68578. duk_int32_t skip;
  68579. skip = duk__bc_get_i32(re_ctx, &pc);
  68580. pc += skip;
  68581. break;
  68582. }
  68583. case DUK_REOP_SPLIT1: {
  68584. /* split1: prefer direct execution (no jump) */
  68585. const duk_uint8_t *sub_sp;
  68586. duk_int32_t skip;
  68587. skip = duk__bc_get_i32(re_ctx, &pc);
  68588. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  68589. if (sub_sp) {
  68590. sp = sub_sp;
  68591. goto match;
  68592. }
  68593. pc += skip;
  68594. break;
  68595. }
  68596. case DUK_REOP_SPLIT2: {
  68597. /* split2: prefer jump execution (not direct) */
  68598. const duk_uint8_t *sub_sp;
  68599. duk_int32_t skip;
  68600. skip = duk__bc_get_i32(re_ctx, &pc);
  68601. sub_sp = duk__match_regexp(re_ctx, pc + skip, sp);
  68602. if (sub_sp) {
  68603. sp = sub_sp;
  68604. goto match;
  68605. }
  68606. break;
  68607. }
  68608. case DUK_REOP_SQMINIMAL: {
  68609. duk_uint32_t q, qmin, qmax;
  68610. duk_int32_t skip;
  68611. const duk_uint8_t *sub_sp;
  68612. qmin = duk__bc_get_u32(re_ctx, &pc);
  68613. qmax = duk__bc_get_u32(re_ctx, &pc);
  68614. skip = duk__bc_get_i32(re_ctx, &pc);
  68615. DUK_DDD(DUK_DDDPRINT("minimal quantifier, qmin=%lu, qmax=%lu, skip=%ld",
  68616. (unsigned long) qmin, (unsigned long) qmax, (long) skip));
  68617. q = 0;
  68618. while (q <= qmax) {
  68619. if (q >= qmin) {
  68620. sub_sp = duk__match_regexp(re_ctx, pc + skip, sp);
  68621. if (sub_sp) {
  68622. sp = sub_sp;
  68623. goto match;
  68624. }
  68625. }
  68626. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  68627. if (!sub_sp) {
  68628. break;
  68629. }
  68630. sp = sub_sp;
  68631. q++;
  68632. }
  68633. goto fail;
  68634. }
  68635. case DUK_REOP_SQGREEDY: {
  68636. duk_uint32_t q, qmin, qmax, atomlen;
  68637. duk_int32_t skip;
  68638. const duk_uint8_t *sub_sp;
  68639. qmin = duk__bc_get_u32(re_ctx, &pc);
  68640. qmax = duk__bc_get_u32(re_ctx, &pc);
  68641. atomlen = duk__bc_get_u32(re_ctx, &pc);
  68642. skip = duk__bc_get_i32(re_ctx, &pc);
  68643. DUK_DDD(DUK_DDDPRINT("greedy quantifier, qmin=%lu, qmax=%lu, atomlen=%lu, skip=%ld",
  68644. (unsigned long) qmin, (unsigned long) qmax, (unsigned long) atomlen, (long) skip));
  68645. q = 0;
  68646. while (q < qmax) {
  68647. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  68648. if (!sub_sp) {
  68649. break;
  68650. }
  68651. sp = sub_sp;
  68652. q++;
  68653. }
  68654. while (q >= qmin) {
  68655. sub_sp = duk__match_regexp(re_ctx, pc + skip, sp);
  68656. if (sub_sp) {
  68657. sp = sub_sp;
  68658. goto match;
  68659. }
  68660. if (q == qmin) {
  68661. break;
  68662. }
  68663. /* Note: if atom were to contain e.g. captures, we would need to
  68664. * re-match the atom to get correct captures. Simply quantifiers
  68665. * do not allow captures in their atom now, so this is not an issue.
  68666. */
  68667. DUK_DDD(DUK_DDDPRINT("greedy quantifier, backtrack %ld characters (atomlen)",
  68668. (long) atomlen));
  68669. sp = duk__inp_backtrack(re_ctx, &sp, (duk_uint_fast32_t) atomlen);
  68670. q--;
  68671. }
  68672. goto fail;
  68673. }
  68674. case DUK_REOP_SAVE: {
  68675. duk_uint32_t idx;
  68676. const duk_uint8_t *old;
  68677. const duk_uint8_t *sub_sp;
  68678. idx = duk__bc_get_u32(re_ctx, &pc);
  68679. if (idx >= re_ctx->nsaved) {
  68680. /* idx is unsigned, < 0 check is not necessary */
  68681. DUK_D(DUK_DPRINT("internal error, regexp save index insane: idx=%ld", (long) idx));
  68682. goto internal_error;
  68683. }
  68684. old = re_ctx->saved[idx];
  68685. re_ctx->saved[idx] = sp;
  68686. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  68687. if (sub_sp) {
  68688. sp = sub_sp;
  68689. goto match;
  68690. }
  68691. re_ctx->saved[idx] = old;
  68692. goto fail;
  68693. }
  68694. case DUK_REOP_WIPERANGE: {
  68695. /* Wipe capture range and save old values for backtracking.
  68696. *
  68697. * XXX: this typically happens with a relatively small idx_count.
  68698. * It might be useful to handle cases where the count is small
  68699. * (say <= 8) by saving the values in stack instead. This would
  68700. * reduce memory churn and improve performance, at the cost of a
  68701. * slightly higher code footprint.
  68702. */
  68703. duk_uint32_t idx_start, idx_count;
  68704. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  68705. duk_uint32_t idx_end, idx;
  68706. #endif
  68707. duk_uint8_t **range_save;
  68708. const duk_uint8_t *sub_sp;
  68709. idx_start = duk__bc_get_u32(re_ctx, &pc);
  68710. idx_count = duk__bc_get_u32(re_ctx, &pc);
  68711. DUK_DDD(DUK_DDDPRINT("wipe saved range: start=%ld, count=%ld -> [%ld,%ld] (captures [%ld,%ld])",
  68712. (long) idx_start, (long) idx_count,
  68713. (long) idx_start, (long) (idx_start + idx_count - 1),
  68714. (long) (idx_start / 2), (long) ((idx_start + idx_count - 1) / 2)));
  68715. if (idx_start + idx_count > re_ctx->nsaved || idx_count == 0) {
  68716. /* idx is unsigned, < 0 check is not necessary */
  68717. DUK_D(DUK_DPRINT("internal error, regexp wipe indices insane: idx_start=%ld, idx_count=%ld",
  68718. (long) idx_start, (long) idx_count));
  68719. goto internal_error;
  68720. }
  68721. DUK_ASSERT(idx_count > 0);
  68722. duk_require_stack((duk_context *) re_ctx->thr, 1);
  68723. range_save = (duk_uint8_t **) duk_push_fixed_buffer((duk_context *) re_ctx->thr,
  68724. sizeof(duk_uint8_t *) * idx_count);
  68725. DUK_ASSERT(range_save != NULL);
  68726. DUK_MEMCPY(range_save, re_ctx->saved + idx_start, sizeof(duk_uint8_t *) * idx_count);
  68727. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  68728. idx_end = idx_start + idx_count;
  68729. for (idx = idx_start; idx < idx_end; idx++) {
  68730. re_ctx->saved[idx] = NULL;
  68731. }
  68732. #else
  68733. DUK_MEMZERO((void *) (re_ctx->saved + idx_start), sizeof(duk_uint8_t *) * idx_count);
  68734. #endif
  68735. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  68736. if (sub_sp) {
  68737. /* match: keep wiped/resaved values */
  68738. DUK_DDD(DUK_DDDPRINT("match: keep wiped/resaved values [%ld,%ld] (captures [%ld,%ld])",
  68739. (long) idx_start, (long) (idx_start + idx_count - 1),
  68740. (long) (idx_start / 2), (long) ((idx_start + idx_count - 1) / 2)));
  68741. duk_pop((duk_context *) re_ctx->thr);
  68742. sp = sub_sp;
  68743. goto match;
  68744. }
  68745. /* fail: restore saves */
  68746. DUK_DDD(DUK_DDDPRINT("fail: restore wiped/resaved values [%ld,%ld] (captures [%ld,%ld])",
  68747. (long) idx_start, (long) (idx_start + idx_count - 1),
  68748. (long) (idx_start / 2), (long) ((idx_start + idx_count - 1) / 2)));
  68749. DUK_MEMCPY((void *) (re_ctx->saved + idx_start),
  68750. (const void *) range_save,
  68751. sizeof(duk_uint8_t *) * idx_count);
  68752. duk_pop((duk_context *) re_ctx->thr);
  68753. goto fail;
  68754. }
  68755. case DUK_REOP_LOOKPOS:
  68756. case DUK_REOP_LOOKNEG: {
  68757. /*
  68758. * Needs a save of multiple saved[] entries depending on what range
  68759. * may be overwritten. Because the regexp parser does no such analysis,
  68760. * we currently save the entire saved array here. Lookaheads are thus
  68761. * a bit expensive. Note that the saved array is not needed for just
  68762. * the lookahead sub-match, but for the matching of the entire sequel.
  68763. *
  68764. * The temporary save buffer is pushed on to the valstack to handle
  68765. * errors correctly. Each lookahead causes a C recursion and pushes
  68766. * more stuff on the value stack. If the C recursion limit is less
  68767. * than the value stack spare, there is no need to check the stack.
  68768. * We do so regardless, just in case.
  68769. */
  68770. duk_int32_t skip;
  68771. duk_uint8_t **full_save;
  68772. const duk_uint8_t *sub_sp;
  68773. DUK_ASSERT(re_ctx->nsaved > 0);
  68774. duk_require_stack((duk_context *) re_ctx->thr, 1);
  68775. full_save = (duk_uint8_t **) duk_push_fixed_buffer((duk_context *) re_ctx->thr,
  68776. sizeof(duk_uint8_t *) * re_ctx->nsaved);
  68777. DUK_ASSERT(full_save != NULL);
  68778. DUK_MEMCPY(full_save, re_ctx->saved, sizeof(duk_uint8_t *) * re_ctx->nsaved);
  68779. skip = duk__bc_get_i32(re_ctx, &pc);
  68780. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  68781. if (op == DUK_REOP_LOOKPOS) {
  68782. if (!sub_sp) {
  68783. goto lookahead_fail;
  68784. }
  68785. } else {
  68786. if (sub_sp) {
  68787. goto lookahead_fail;
  68788. }
  68789. }
  68790. sub_sp = duk__match_regexp(re_ctx, pc + skip, sp);
  68791. if (sub_sp) {
  68792. /* match: keep saves */
  68793. duk_pop((duk_context *) re_ctx->thr);
  68794. sp = sub_sp;
  68795. goto match;
  68796. }
  68797. /* fall through */
  68798. lookahead_fail:
  68799. /* fail: restore saves */
  68800. DUK_MEMCPY((void *) re_ctx->saved,
  68801. (const void *) full_save,
  68802. sizeof(duk_uint8_t *) * re_ctx->nsaved);
  68803. duk_pop((duk_context *) re_ctx->thr);
  68804. goto fail;
  68805. }
  68806. case DUK_REOP_BACKREFERENCE: {
  68807. /*
  68808. * Byte matching for back-references would be OK in case-
  68809. * sensitive matching. In case-insensitive matching we need
  68810. * to canonicalize characters, so back-reference matching needs
  68811. * to be done with codepoints instead. So, we just decode
  68812. * everything normally here, too.
  68813. *
  68814. * Note: back-reference index which is 0 or higher than
  68815. * NCapturingParens (= number of capturing parens in the
  68816. * -entire- regexp) is a compile time error. However, a
  68817. * backreference referring to a valid capture which has
  68818. * not matched anything always succeeds! See E5 Section
  68819. * 15.10.2.9, step 5, sub-step 3.
  68820. */
  68821. duk_uint32_t idx;
  68822. const duk_uint8_t *p;
  68823. idx = duk__bc_get_u32(re_ctx, &pc);
  68824. idx = idx << 1; /* backref n -> saved indices [n*2, n*2+1] */
  68825. if (idx < 2 || idx + 1 >= re_ctx->nsaved) {
  68826. /* regexp compiler should catch these */
  68827. DUK_D(DUK_DPRINT("internal error, backreference index insane"));
  68828. goto internal_error;
  68829. }
  68830. if (!re_ctx->saved[idx] || !re_ctx->saved[idx+1]) {
  68831. /* capture is 'undefined', always matches! */
  68832. DUK_DDD(DUK_DDDPRINT("backreference: saved[%ld,%ld] not complete, always match",
  68833. (long) idx, (long) (idx + 1)));
  68834. break;
  68835. }
  68836. DUK_DDD(DUK_DDDPRINT("backreference: match saved[%ld,%ld]", (long) idx, (long) (idx + 1)));
  68837. p = re_ctx->saved[idx];
  68838. while (p < re_ctx->saved[idx+1]) {
  68839. duk_codepoint_t c1, c2;
  68840. /* Note: not necessary to check p against re_ctx->input_end:
  68841. * the memory access is checked by duk__inp_get_cp(), while
  68842. * valid compiled regexps cannot write a saved[] entry
  68843. * which points to outside the string.
  68844. */
  68845. if (sp >= re_ctx->input_end) {
  68846. goto fail;
  68847. }
  68848. c1 = duk__inp_get_cp(re_ctx, &p);
  68849. c2 = duk__inp_get_cp(re_ctx, &sp);
  68850. if (c1 != c2) {
  68851. goto fail;
  68852. }
  68853. }
  68854. break;
  68855. }
  68856. default: {
  68857. DUK_D(DUK_DPRINT("internal error, regexp opcode error: %ld", (long) op));
  68858. goto internal_error;
  68859. }
  68860. }
  68861. }
  68862. match:
  68863. re_ctx->recursion_depth--;
  68864. return sp;
  68865. fail:
  68866. re_ctx->recursion_depth--;
  68867. return NULL;
  68868. internal_error:
  68869. DUK_ERROR(re_ctx->thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_REGEXP_INTERNAL_ERROR);
  68870. return NULL; /* never here */
  68871. }
  68872. /*
  68873. * Exposed matcher function which provides the semantics of RegExp.prototype.exec().
  68874. *
  68875. * RegExp.prototype.test() has the same semantics as exec() but does not return the
  68876. * result object (which contains the matching string and capture groups). Currently
  68877. * there is no separate test() helper, so a temporary result object is created and
  68878. * discarded if test() is needed. This is intentional, to save code space.
  68879. *
  68880. * Input stack: [ ... re_obj input ]
  68881. * Output stack: [ ... result ]
  68882. */
  68883. DUK_LOCAL void duk__regexp_match_helper(duk_hthread *thr, duk_small_int_t force_global) {
  68884. duk_context *ctx = (duk_context *) thr;
  68885. duk_re_matcher_ctx re_ctx;
  68886. duk_hobject *h_regexp;
  68887. duk_hstring *h_bytecode;
  68888. duk_hstring *h_input;
  68889. const duk_uint8_t *pc;
  68890. const duk_uint8_t *sp;
  68891. duk_small_int_t match = 0;
  68892. duk_small_int_t global;
  68893. duk_uint_fast32_t i;
  68894. double d;
  68895. duk_uint32_t char_offset;
  68896. DUK_ASSERT(thr != NULL);
  68897. DUK_ASSERT(ctx != NULL);
  68898. DUK_DD(DUK_DDPRINT("regexp match: regexp=%!T, input=%!T",
  68899. (duk_tval *) duk_get_tval(ctx, -2),
  68900. (duk_tval *) duk_get_tval(ctx, -1)));
  68901. /*
  68902. * Regexp instance check, bytecode check, input coercion.
  68903. *
  68904. * See E5 Section 15.10.6.
  68905. */
  68906. /* TypeError if wrong; class check, see E5 Section 15.10.6 */
  68907. h_regexp = duk_require_hobject_with_class(ctx, -2, DUK_HOBJECT_CLASS_REGEXP);
  68908. DUK_ASSERT(h_regexp != NULL);
  68909. DUK_ASSERT(DUK_HOBJECT_GET_CLASS_NUMBER(h_regexp) == DUK_HOBJECT_CLASS_REGEXP);
  68910. DUK_UNREF(h_regexp);
  68911. duk_to_string(ctx, -1);
  68912. h_input = duk_get_hstring(ctx, -1);
  68913. DUK_ASSERT(h_input != NULL);
  68914. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_INT_BYTECODE); /* [ ... re_obj input ] -> [ ... re_obj input bc ] */
  68915. h_bytecode = duk_require_hstring(ctx, -1); /* no regexp instance should exist without a non-configurable bytecode property */
  68916. DUK_ASSERT(h_bytecode != NULL);
  68917. /*
  68918. * Basic context initialization.
  68919. *
  68920. * Some init values are read from the bytecode header
  68921. * whose format is (UTF-8 codepoints):
  68922. *
  68923. * uint flags
  68924. * uint nsaved (even, 2n+2 where n = num captures)
  68925. */
  68926. /* [ ... re_obj input bc ] */
  68927. DUK_MEMZERO(&re_ctx, sizeof(re_ctx));
  68928. re_ctx.thr = thr;
  68929. re_ctx.input = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
  68930. re_ctx.input_end = re_ctx.input + DUK_HSTRING_GET_BYTELEN(h_input);
  68931. re_ctx.bytecode = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_bytecode);
  68932. re_ctx.bytecode_end = re_ctx.bytecode + DUK_HSTRING_GET_BYTELEN(h_bytecode);
  68933. re_ctx.saved = NULL;
  68934. re_ctx.recursion_limit = DUK_USE_REGEXP_EXECUTOR_RECLIMIT;
  68935. re_ctx.steps_limit = DUK_RE_EXECUTE_STEPS_LIMIT;
  68936. /* read header */
  68937. pc = re_ctx.bytecode;
  68938. re_ctx.re_flags = duk__bc_get_u32(&re_ctx, &pc);
  68939. re_ctx.nsaved = duk__bc_get_u32(&re_ctx, &pc);
  68940. re_ctx.bytecode = pc;
  68941. DUK_ASSERT(DUK_RE_FLAG_GLOBAL < 0x10000UL); /* must fit into duk_small_int_t */
  68942. global = (duk_small_int_t) (force_global | (re_ctx.re_flags & DUK_RE_FLAG_GLOBAL));
  68943. DUK_ASSERT(re_ctx.nsaved >= 2);
  68944. DUK_ASSERT((re_ctx.nsaved % 2) == 0);
  68945. duk_push_fixed_buffer(ctx, sizeof(duk_uint8_t *) * re_ctx.nsaved);
  68946. re_ctx.saved = (const duk_uint8_t **) duk_get_buffer(ctx, -1, NULL);
  68947. DUK_ASSERT(re_ctx.saved != NULL);
  68948. /* [ ... re_obj input bc saved_buf ] */
  68949. /* buffer is automatically zeroed */
  68950. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  68951. for (i = 0; i < re_ctx.nsaved; i++) {
  68952. re_ctx.saved[i] = (duk_uint8_t *) NULL;
  68953. }
  68954. #endif
  68955. DUK_DDD(DUK_DDDPRINT("regexp ctx initialized, flags=0x%08lx, nsaved=%ld, recursion_limit=%ld, steps_limit=%ld",
  68956. (unsigned long) re_ctx.re_flags, (long) re_ctx.nsaved, (long) re_ctx.recursion_limit,
  68957. (long) re_ctx.steps_limit));
  68958. /*
  68959. * Get starting character offset for match, and initialize 'sp' based on it.
  68960. *
  68961. * Note: lastIndex is non-configurable so it must be present (we check the
  68962. * internal class of the object above, so we know it is). User code can set
  68963. * its value to an arbitrary (garbage) value though; E5 requires that lastIndex
  68964. * be coerced to a number before using. The code below works even if the
  68965. * property is missing: the value will then be coerced to zero.
  68966. *
  68967. * Note: lastIndex may be outside Uint32 range even after ToInteger() coercion.
  68968. * For instance, ToInteger(+Infinity) = +Infinity. We track the match offset
  68969. * as an integer, but pre-check it to be inside the 32-bit range before the loop.
  68970. * If not, the check in E5 Section 15.10.6.2, step 9.a applies.
  68971. */
  68972. /* XXX: lastIndex handling produces a lot of asm */
  68973. /* [ ... re_obj input bc saved_buf ] */
  68974. duk_get_prop_stridx(ctx, -4, DUK_STRIDX_LAST_INDEX); /* -> [ ... re_obj input bc saved_buf lastIndex ] */
  68975. (void) duk_to_int(ctx, -1); /* ToInteger(lastIndex) */
  68976. d = duk_get_number(ctx, -1); /* integer, but may be +/- Infinite, +/- zero (not NaN, though) */
  68977. duk_pop(ctx);
  68978. if (global) {
  68979. if (d < 0.0 || d > (double) DUK_HSTRING_GET_CHARLEN(h_input)) {
  68980. /* match fail */
  68981. char_offset = 0; /* not really necessary */
  68982. DUK_ASSERT(match == 0);
  68983. goto match_over;
  68984. }
  68985. char_offset = (duk_uint32_t) d;
  68986. } else {
  68987. /* lastIndex must be ignored for non-global regexps, but get the
  68988. * value for (theoretical) side effects. No side effects can
  68989. * really occur, because lastIndex is a normal property and is
  68990. * always non-configurable for RegExp instances.
  68991. */
  68992. char_offset = (duk_uint32_t) 0;
  68993. }
  68994. sp = re_ctx.input + duk_heap_strcache_offset_char2byte(thr, h_input, char_offset);
  68995. /*
  68996. * Match loop.
  68997. *
  68998. * Try matching at different offsets until match found or input exhausted.
  68999. */
  69000. /* [ ... re_obj input bc saved_buf ] */
  69001. DUK_ASSERT(match == 0);
  69002. for (;;) {
  69003. /* char offset in [0, h_input->clen] (both ends inclusive), checked before entry */
  69004. DUK_ASSERT_DISABLE(char_offset >= 0);
  69005. DUK_ASSERT(char_offset <= DUK_HSTRING_GET_CHARLEN(h_input));
  69006. /* Note: ctx.steps is intentionally not reset, it applies to the entire unanchored match */
  69007. DUK_ASSERT(re_ctx.recursion_depth == 0);
  69008. DUK_DDD(DUK_DDDPRINT("attempt match at char offset %ld; %p [%p,%p]",
  69009. (long) char_offset, (void *) sp, (void *) re_ctx.input,
  69010. (void *) re_ctx.input_end));
  69011. /*
  69012. * Note:
  69013. *
  69014. * - duk__match_regexp() is required not to longjmp() in ordinary "non-match"
  69015. * conditions; a longjmp() will terminate the entire matching process.
  69016. *
  69017. * - Clearing saved[] is not necessary because backtracking does it
  69018. *
  69019. * - Backtracking also rewinds ctx.recursion back to zero, unless an
  69020. * internal/limit error occurs (which causes a longjmp())
  69021. *
  69022. * - If we supported anchored matches, we would break out here
  69023. * unconditionally; however, Ecmascript regexps don't have anchored
  69024. * matches. It might make sense to implement a fast bail-out if
  69025. * the regexp begins with '^' and sp is not 0: currently we'll just
  69026. * run through the entire input string, trivially failing the match
  69027. * at every non-zero offset.
  69028. */
  69029. if (duk__match_regexp(&re_ctx, re_ctx.bytecode, sp) != NULL) {
  69030. DUK_DDD(DUK_DDDPRINT("match at offset %ld", (long) char_offset));
  69031. match = 1;
  69032. break;
  69033. }
  69034. /* advance by one character (code point) and one char_offset */
  69035. char_offset++;
  69036. if (char_offset > DUK_HSTRING_GET_CHARLEN(h_input)) {
  69037. /*
  69038. * Note:
  69039. *
  69040. * - Intentionally attempt (empty) match at char_offset == k_input->clen
  69041. *
  69042. * - Negative char_offsets have been eliminated and char_offset is duk_uint32_t
  69043. * -> no need or use for a negative check
  69044. */
  69045. DUK_DDD(DUK_DDDPRINT("no match after trying all sp offsets"));
  69046. break;
  69047. }
  69048. /* avoid calling at end of input, will DUK_ERROR (above check suffices to avoid this) */
  69049. (void) duk__utf8_advance(thr, &sp, re_ctx.input, re_ctx.input_end, (duk_uint_fast32_t) 1);
  69050. }
  69051. match_over:
  69052. /*
  69053. * Matching complete, create result array or return a 'null'. Update lastIndex
  69054. * if necessary. See E5 Section 15.10.6.2.
  69055. *
  69056. * Because lastIndex is a character (not byte) offset, we need the character
  69057. * length of the match which we conveniently get as a side effect of interning
  69058. * the matching substring (0th index of result array).
  69059. *
  69060. * saved[0] start pointer (~ byte offset) of current match
  69061. * saved[1] end pointer (~ byte offset) of current match (exclusive)
  69062. * char_offset start character offset of current match (-> .index of result)
  69063. * char_end_offset end character offset (computed below)
  69064. */
  69065. /* [ ... re_obj input bc saved_buf ] */
  69066. if (match) {
  69067. #ifdef DUK_USE_ASSERTIONS
  69068. duk_hobject *h_res;
  69069. #endif
  69070. duk_uint32_t char_end_offset = 0;
  69071. DUK_DDD(DUK_DDDPRINT("regexp matches at char_offset %ld", (long) char_offset));
  69072. DUK_ASSERT(re_ctx.nsaved >= 2); /* must have start and end */
  69073. DUK_ASSERT((re_ctx.nsaved % 2) == 0); /* and even number */
  69074. /* XXX: Array size is known before and (2 * re_ctx.nsaved) but not taken
  69075. * advantage of now. The array is not compacted either, as regexp match
  69076. * objects are usually short lived.
  69077. */
  69078. duk_push_array(ctx);
  69079. #ifdef DUK_USE_ASSERTIONS
  69080. h_res = duk_require_hobject(ctx, -1);
  69081. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(h_res));
  69082. DUK_ASSERT(DUK_HOBJECT_HAS_EXOTIC_ARRAY(h_res));
  69083. DUK_ASSERT(DUK_HOBJECT_GET_CLASS_NUMBER(h_res) == DUK_HOBJECT_CLASS_ARRAY);
  69084. #endif
  69085. /* [ ... re_obj input bc saved_buf res_obj ] */
  69086. duk_push_u32(ctx, char_offset);
  69087. duk_xdef_prop_stridx_wec(ctx, -2, DUK_STRIDX_INDEX);
  69088. duk_dup(ctx, -4);
  69089. duk_xdef_prop_stridx_wec(ctx, -2, DUK_STRIDX_INPUT);
  69090. for (i = 0; i < re_ctx.nsaved; i += 2) {
  69091. /* Captures which are undefined have NULL pointers and are returned
  69092. * as 'undefined'. The same is done when saved[] pointers are insane
  69093. * (this should, of course, never happen in practice).
  69094. */
  69095. if (re_ctx.saved[i] && re_ctx.saved[i+1] && re_ctx.saved[i+1] >= re_ctx.saved[i]) {
  69096. duk_hstring *h_saved;
  69097. duk_push_lstring(ctx,
  69098. (const char *) re_ctx.saved[i],
  69099. (duk_size_t) (re_ctx.saved[i+1] - re_ctx.saved[i]));
  69100. h_saved = duk_get_hstring(ctx, -1);
  69101. DUK_ASSERT(h_saved != NULL);
  69102. if (i == 0) {
  69103. /* Assumes that saved[0] and saved[1] are always
  69104. * set by regexp bytecode (if not, char_end_offset
  69105. * will be zero). Also assumes clen reflects the
  69106. * correct char length.
  69107. */
  69108. char_end_offset = char_offset + DUK_HSTRING_GET_CHARLEN(h_saved);
  69109. }
  69110. } else {
  69111. duk_push_undefined(ctx);
  69112. }
  69113. /* [ ... re_obj input bc saved_buf res_obj val ] */
  69114. duk_put_prop_index(ctx, -2, i / 2);
  69115. }
  69116. /* [ ... re_obj input bc saved_buf res_obj ] */
  69117. /* NB: 'length' property is automatically updated by the array setup loop */
  69118. if (global) {
  69119. /* global regexp: lastIndex updated on match */
  69120. duk_push_u32(ctx, char_end_offset);
  69121. duk_put_prop_stridx(ctx, -6, DUK_STRIDX_LAST_INDEX);
  69122. } else {
  69123. /* non-global regexp: lastIndex never updated on match */
  69124. ;
  69125. }
  69126. } else {
  69127. /*
  69128. * No match, E5 Section 15.10.6.2, step 9.a.i - 9.a.ii apply, regardless
  69129. * of 'global' flag of the RegExp. In particular, if lastIndex is invalid
  69130. * initially, it is reset to zero.
  69131. */
  69132. DUK_DDD(DUK_DDDPRINT("regexp does not match"));
  69133. duk_push_null(ctx);
  69134. /* [ ... re_obj input bc saved_buf res_obj ] */
  69135. duk_push_int(ctx, 0);
  69136. duk_put_prop_stridx(ctx, -6, DUK_STRIDX_LAST_INDEX);
  69137. }
  69138. /* [ ... re_obj input bc saved_buf res_obj ] */
  69139. duk_insert(ctx, -5);
  69140. /* [ ... res_obj re_obj input bc saved_buf ] */
  69141. duk_pop_n(ctx, 4);
  69142. /* [ ... res_obj ] */
  69143. /* XXX: these last tricks are unnecessary if the function is made
  69144. * a genuine native function.
  69145. */
  69146. }
  69147. DUK_INTERNAL void duk_regexp_match(duk_hthread *thr) {
  69148. duk__regexp_match_helper(thr, 0 /*force_global*/);
  69149. }
  69150. /* This variant is needed by String.prototype.split(); it needs to perform
  69151. * global-style matching on a cloned RegExp which is potentially non-global.
  69152. */
  69153. DUK_INTERNAL void duk_regexp_match_force_global(duk_hthread *thr) {
  69154. duk__regexp_match_helper(thr, 1 /*force_global*/);
  69155. }
  69156. #else /* DUK_USE_REGEXP_SUPPORT */
  69157. /* regexp support disabled */
  69158. #endif /* DUK_USE_REGEXP_SUPPORT */
  69159. #line 1 "duk_replacements.c"
  69160. /*
  69161. * Replacements for missing platform functions.
  69162. *
  69163. * Unlike the originals, fpclassify() and signbit() replacements don't
  69164. * work on any floating point types, only doubles. The C typing here
  69165. * mimics the standard prototypes.
  69166. */
  69167. /* include removed: duk_internal.h */
  69168. #ifdef DUK_USE_COMPUTED_NAN
  69169. DUK_INTERNAL double duk_computed_nan;
  69170. #endif
  69171. #ifdef DUK_USE_COMPUTED_INFINITY
  69172. DUK_INTERNAL double duk_computed_infinity;
  69173. #endif
  69174. #ifdef DUK_USE_REPL_FPCLASSIFY
  69175. DUK_INTERNAL int duk_repl_fpclassify(double x) {
  69176. duk_double_union u;
  69177. duk_uint_fast16_t expt;
  69178. duk_small_int_t mzero;
  69179. u.d = x;
  69180. expt = (duk_uint_fast16_t) (u.us[DUK_DBL_IDX_US0] & 0x7ff0UL);
  69181. if (expt > 0x0000UL && expt < 0x7ff0UL) {
  69182. /* expt values [0x001,0x7fe] = normal */
  69183. return DUK_FP_NORMAL;
  69184. }
  69185. mzero = (u.ui[DUK_DBL_IDX_UI1] == 0 && (u.ui[DUK_DBL_IDX_UI0] & 0x000fffffUL) == 0);
  69186. if (expt == 0x0000UL) {
  69187. /* expt 0x000 is zero/subnormal */
  69188. if (mzero) {
  69189. return DUK_FP_ZERO;
  69190. } else {
  69191. return DUK_FP_SUBNORMAL;
  69192. }
  69193. } else {
  69194. /* expt 0xfff is infinite/nan */
  69195. if (mzero) {
  69196. return DUK_FP_INFINITE;
  69197. } else {
  69198. return DUK_FP_NAN;
  69199. }
  69200. }
  69201. }
  69202. #endif
  69203. #ifdef DUK_USE_REPL_SIGNBIT
  69204. DUK_INTERNAL int duk_repl_signbit(double x) {
  69205. duk_double_union u;
  69206. u.d = x;
  69207. return (int) (u.uc[DUK_DBL_IDX_UC0] & 0x80UL);
  69208. }
  69209. #endif
  69210. #ifdef DUK_USE_REPL_ISFINITE
  69211. DUK_INTERNAL int duk_repl_isfinite(double x) {
  69212. int c = DUK_FPCLASSIFY(x);
  69213. if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) {
  69214. return 0;
  69215. } else {
  69216. return 1;
  69217. }
  69218. }
  69219. #endif
  69220. #ifdef DUK_USE_REPL_ISNAN
  69221. DUK_INTERNAL int duk_repl_isnan(double x) {
  69222. int c = DUK_FPCLASSIFY(x);
  69223. return (c == DUK_FP_NAN);
  69224. }
  69225. #endif
  69226. #ifdef DUK_USE_REPL_ISINF
  69227. DUK_INTERNAL int duk_repl_isinf(double x) {
  69228. int c = DUK_FPCLASSIFY(x);
  69229. return (c == DUK_FP_INFINITE);
  69230. }
  69231. #endif
  69232. #line 1 "duk_selftest.c"
  69233. /*
  69234. * Self tests to ensure execution environment is sane. Intended to catch
  69235. * compiler/platform problems which cannot be detected at compile time.
  69236. */
  69237. /* include removed: duk_internal.h */
  69238. #if defined(DUK_USE_SELF_TESTS)
  69239. /*
  69240. * Unions and structs for self tests
  69241. */
  69242. typedef union {
  69243. double d;
  69244. duk_uint8_t c[8];
  69245. } duk__test_double_union;
  69246. #define DUK__DBLUNION_CMP_TRUE(a,b) do { \
  69247. if (DUK_MEMCMP((const void *) (a), (const void *) (b), sizeof(duk__test_double_union)) != 0) { \
  69248. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: double union compares false (expected true)"); \
  69249. } \
  69250. } while (0)
  69251. #define DUK__DBLUNION_CMP_FALSE(a,b) do { \
  69252. if (DUK_MEMCMP((const void *) (a), (const void *) (b), sizeof(duk__test_double_union)) == 0) { \
  69253. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: double union compares true (expected false)"); \
  69254. } \
  69255. } while (0)
  69256. typedef union {
  69257. duk_uint32_t i;
  69258. duk_uint8_t c[8];
  69259. } duk__test_u32_union;
  69260. /*
  69261. * Various sanity checks for typing
  69262. */
  69263. DUK_LOCAL void duk__selftest_types(void) {
  69264. if (!(sizeof(duk_int8_t) == 1 &&
  69265. sizeof(duk_uint8_t) == 1 &&
  69266. sizeof(duk_int16_t) == 2 &&
  69267. sizeof(duk_uint16_t) == 2 &&
  69268. sizeof(duk_int32_t) == 4 &&
  69269. sizeof(duk_uint32_t) == 4)) {
  69270. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: duk_(u)int{8,16,32}_t size");
  69271. }
  69272. #if defined(DUK_USE_64BIT_OPS)
  69273. if (!(sizeof(duk_int64_t) == 8 &&
  69274. sizeof(duk_uint64_t) == 8)) {
  69275. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: duk_(u)int64_t size");
  69276. }
  69277. #endif
  69278. if (!(sizeof(duk_size_t) >= sizeof(duk_uint_t))) {
  69279. /* Some internal code now assumes that all duk_uint_t values
  69280. * can be expressed with a duk_size_t.
  69281. */
  69282. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: duk_size_t is smaller than duk_uint_t");
  69283. }
  69284. if (!(sizeof(duk_int_t) >= 4)) {
  69285. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: duk_int_t is not 32 bits");
  69286. }
  69287. }
  69288. /*
  69289. * Packed tval sanity
  69290. */
  69291. DUK_LOCAL void duk__selftest_packed_tval(void) {
  69292. #if defined(DUK_USE_PACKED_TVAL)
  69293. if (sizeof(void *) > 4) {
  69294. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: packed duk_tval in use but sizeof(void *) > 4");
  69295. }
  69296. #endif
  69297. }
  69298. /*
  69299. * Two's complement arithmetic.
  69300. */
  69301. DUK_LOCAL void duk__selftest_twos_complement(void) {
  69302. volatile int test;
  69303. test = -1;
  69304. /* Note that byte order doesn't affect this test: all bytes in
  69305. * 'test' will be 0xFF for two's complement.
  69306. */
  69307. if (((volatile duk_uint8_t *) &test)[0] != (duk_uint8_t) 0xff) {
  69308. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: two's complement arithmetic");
  69309. }
  69310. }
  69311. /*
  69312. * Byte order. Important to self check, because on some exotic platforms
  69313. * there is no actual detection but rather assumption based on platform
  69314. * defines.
  69315. */
  69316. DUK_LOCAL void duk__selftest_byte_order(void) {
  69317. duk__test_u32_union u1;
  69318. duk__test_double_union u2;
  69319. /*
  69320. * >>> struct.pack('>d', 102030405060).encode('hex')
  69321. * '4237c17c6dc40000'
  69322. */
  69323. #if defined(DUK_USE_INTEGER_LE)
  69324. u1.c[0] = 0xef; u1.c[1] = 0xbe; u1.c[2] = 0xad; u1.c[3] = 0xde;
  69325. #elif defined(DUK_USE_INTEGER_ME)
  69326. #error integer mixed endian not supported now
  69327. #elif defined(DUK_USE_INTEGER_BE)
  69328. u1.c[0] = 0xde; u1.c[1] = 0xad; u1.c[2] = 0xbe; u1.c[3] = 0xef;
  69329. #else
  69330. #error unknown integer endianness
  69331. #endif
  69332. #if defined(DUK_USE_DOUBLE_LE)
  69333. u2.c[0] = 0x00; u2.c[1] = 0x00; u2.c[2] = 0xc4; u2.c[3] = 0x6d;
  69334. u2.c[4] = 0x7c; u2.c[5] = 0xc1; u2.c[6] = 0x37; u2.c[7] = 0x42;
  69335. #elif defined(DUK_USE_DOUBLE_ME)
  69336. u2.c[0] = 0x7c; u2.c[1] = 0xc1; u2.c[2] = 0x37; u2.c[3] = 0x42;
  69337. u2.c[4] = 0x00; u2.c[5] = 0x00; u2.c[6] = 0xc4; u2.c[7] = 0x6d;
  69338. #elif defined(DUK_USE_DOUBLE_BE)
  69339. u2.c[0] = 0x42; u2.c[1] = 0x37; u2.c[2] = 0xc1; u2.c[3] = 0x7c;
  69340. u2.c[4] = 0x6d; u2.c[5] = 0xc4; u2.c[6] = 0x00; u2.c[7] = 0x00;
  69341. #else
  69342. #error unknown double endianness
  69343. #endif
  69344. if (u1.i != (duk_uint32_t) 0xdeadbeefUL) {
  69345. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: duk_uint32_t byte order");
  69346. }
  69347. if (u2.d != (double) 102030405060.0) {
  69348. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: double byte order");
  69349. }
  69350. }
  69351. /*
  69352. * DUK_BSWAP macros
  69353. */
  69354. DUK_LOCAL void duk__selftest_bswap_macros(void) {
  69355. duk_uint32_t x32;
  69356. duk_uint16_t x16;
  69357. duk_double_union du;
  69358. duk_double_t du_diff;
  69359. x16 = 0xbeefUL;
  69360. x16 = DUK_BSWAP16(x16);
  69361. if (x16 != (duk_uint16_t) 0xefbeUL) {
  69362. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: DUK_BSWAP16");
  69363. }
  69364. x32 = 0xdeadbeefUL;
  69365. x32 = DUK_BSWAP32(x32);
  69366. if (x32 != (duk_uint32_t) 0xefbeaddeUL) {
  69367. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: DUK_BSWAP32");
  69368. }
  69369. /* >>> struct.unpack('>d', '4000112233445566'.decode('hex'))
  69370. * (2.008366013071895,)
  69371. */
  69372. du.uc[0] = 0x40; du.uc[1] = 0x00; du.uc[2] = 0x11; du.uc[3] = 0x22;
  69373. du.uc[4] = 0x33; du.uc[5] = 0x44; du.uc[6] = 0x55; du.uc[7] = 0x66;
  69374. DUK_DBLUNION_DOUBLE_NTOH(&du);
  69375. du_diff = du.d - 2.008366013071895;
  69376. #if 0
  69377. DUK_FPRINTF(DUK_STDERR, "du_diff: %lg\n", (double) du_diff);
  69378. #endif
  69379. if (du_diff > 1e-15) {
  69380. /* Allow very small lenience because some compilers won't parse
  69381. * exact IEEE double constants (happened in matrix testing with
  69382. * Linux gcc-4.8 -m32 at least).
  69383. */
  69384. #if 0
  69385. DUK_FPRINTF(DUK_STDERR, "Result of DUK_DBLUNION_DOUBLE_NTOH: %02x %02x %02x %02x %02x %02x %02x %02x\n",
  69386. (unsigned int) du.uc[0], (unsigned int) du.uc[1],
  69387. (unsigned int) du.uc[2], (unsigned int) du.uc[3],
  69388. (unsigned int) du.uc[4], (unsigned int) du.uc[5],
  69389. (unsigned int) du.uc[6], (unsigned int) du.uc[7]);
  69390. #endif
  69391. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: DUK_DBLUNION_DOUBLE_NTOH");
  69392. }
  69393. }
  69394. /*
  69395. * Basic double / byte union memory layout.
  69396. */
  69397. DUK_LOCAL void duk__selftest_double_union_size(void) {
  69398. if (sizeof(duk__test_double_union) != 8) {
  69399. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: invalid union size");
  69400. }
  69401. }
  69402. /*
  69403. * Union aliasing, see misc/clang_aliasing.c.
  69404. */
  69405. DUK_LOCAL void duk__selftest_double_aliasing(void) {
  69406. duk__test_double_union a, b;
  69407. /* This testcase fails when Emscripten-generated code runs on Firefox.
  69408. * It's not an issue because the failure should only affect packed
  69409. * duk_tval representation, which is not used with Emscripten.
  69410. */
  69411. #if defined(DUK_USE_NO_DOUBLE_ALIASING_SELFTEST)
  69412. #if defined(DUK_USE_PACKED_TVAL)
  69413. #error inconsistent defines: skipping double aliasing selftest when using packed duk_tval
  69414. #endif
  69415. return;
  69416. #endif
  69417. /* Test signaling NaN and alias assignment in all
  69418. * endianness combinations.
  69419. */
  69420. /* little endian */
  69421. a.c[0] = 0x11; a.c[1] = 0x22; a.c[2] = 0x33; a.c[3] = 0x44;
  69422. a.c[4] = 0x00; a.c[5] = 0x00; a.c[6] = 0xf1; a.c[7] = 0xff;
  69423. b = a;
  69424. DUK__DBLUNION_CMP_TRUE(&a, &b);
  69425. /* big endian */
  69426. a.c[0] = 0xff; a.c[1] = 0xf1; a.c[2] = 0x00; a.c[3] = 0x00;
  69427. a.c[4] = 0x44; a.c[5] = 0x33; a.c[6] = 0x22; a.c[7] = 0x11;
  69428. b = a;
  69429. DUK__DBLUNION_CMP_TRUE(&a, &b);
  69430. /* mixed endian */
  69431. a.c[0] = 0x00; a.c[1] = 0x00; a.c[2] = 0xf1; a.c[3] = 0xff;
  69432. a.c[4] = 0x11; a.c[5] = 0x22; a.c[6] = 0x33; a.c[7] = 0x44;
  69433. b = a;
  69434. DUK__DBLUNION_CMP_TRUE(&a, &b);
  69435. }
  69436. /*
  69437. * Zero sign, see misc/tcc_zerosign2.c.
  69438. */
  69439. DUK_LOCAL void duk__selftest_double_zero_sign(void) {
  69440. duk__test_double_union a, b;
  69441. a.d = 0.0;
  69442. b.d = -a.d;
  69443. DUK__DBLUNION_CMP_FALSE(&a, &b);
  69444. }
  69445. /*
  69446. * Struct size/alignment if platform requires it
  69447. *
  69448. * There are some compiler specific struct padding pragmas etc in use, this
  69449. * selftest ensures they're correctly detected and used.
  69450. */
  69451. DUK_LOCAL void duk__selftest_struct_align(void) {
  69452. #if (DUK_USE_ALIGN_BY == 4)
  69453. if ((sizeof(duk_hbuffer_fixed) % 4) != 0) {
  69454. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: sizeof(duk_hbuffer_fixed) not aligned to 4");
  69455. }
  69456. #elif (DUK_USE_ALIGN_BY == 8)
  69457. if ((sizeof(duk_hbuffer_fixed) % 8) != 0) {
  69458. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: sizeof(duk_hbuffer_fixed) not aligned to 8");
  69459. }
  69460. #elif (DUK_USE_ALIGN_BY == 1)
  69461. /* no check */
  69462. #else
  69463. #error invalid DUK_USE_ALIGN_BY
  69464. #endif
  69465. }
  69466. /*
  69467. * 64-bit arithmetic
  69468. *
  69469. * There are some platforms/compilers where 64-bit types are available
  69470. * but don't work correctly. Test for known cases.
  69471. */
  69472. DUK_LOCAL void duk__selftest_64bit_arithmetic(void) {
  69473. #if defined(DUK_USE_64BIT_OPS)
  69474. volatile duk_int64_t i;
  69475. volatile duk_double_t d;
  69476. /* Catch a double-to-int64 cast issue encountered in practice. */
  69477. d = 2147483648.0;
  69478. i = (duk_int64_t) d;
  69479. if (i != 0x80000000LL) {
  69480. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: casting 2147483648.0 to duk_int64_t failed");
  69481. }
  69482. #else
  69483. /* nop */
  69484. #endif
  69485. }
  69486. /*
  69487. * Casting
  69488. */
  69489. DUK_LOCAL void duk__selftest_cast_double_to_small_uint(void) {
  69490. /*
  69491. * https://github.com/svaarala/duktape/issues/127#issuecomment-77863473
  69492. */
  69493. duk_double_t d1, d2;
  69494. duk_small_uint_t u;
  69495. duk_double_t d1v, d2v;
  69496. duk_small_uint_t uv;
  69497. /* Test without volatiles */
  69498. d1 = 1.0;
  69499. u = (duk_small_uint_t) d1;
  69500. d2 = (duk_double_t) u;
  69501. if (!(d1 == 1.0 && u == 1 && d2 == 1.0 && d1 == d2)) {
  69502. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: double to duk_small_uint_t cast failed");
  69503. }
  69504. /* Same test with volatiles */
  69505. d1v = 1.0;
  69506. uv = (duk_small_uint_t) d1v;
  69507. d2v = (duk_double_t) uv;
  69508. if (!(d1v == 1.0 && uv == 1 && d2v == 1.0 && d1v == d2v)) {
  69509. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: double to duk_small_uint_t cast failed");
  69510. }
  69511. }
  69512. DUK_LOCAL void duk__selftest_cast_double_to_uint32(void) {
  69513. /*
  69514. * This test fails on an exotic ARM target; double-to-uint
  69515. * cast is incorrectly clamped to -signed- int highest value.
  69516. *
  69517. * https://github.com/svaarala/duktape/issues/336
  69518. */
  69519. duk_double_t dv;
  69520. duk_uint32_t uv;
  69521. dv = 3735928559.0; /* 0xdeadbeef in decimal */
  69522. uv = (duk_uint32_t) dv;
  69523. if (uv != 0xdeadbeefUL) {
  69524. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: double to duk_uint32_t cast failed");
  69525. }
  69526. }
  69527. /*
  69528. * Self test main
  69529. */
  69530. DUK_INTERNAL void duk_selftest_run_tests(void) {
  69531. duk__selftest_types();
  69532. duk__selftest_packed_tval();
  69533. duk__selftest_twos_complement();
  69534. duk__selftest_byte_order();
  69535. duk__selftest_bswap_macros();
  69536. duk__selftest_double_union_size();
  69537. duk__selftest_double_aliasing();
  69538. duk__selftest_double_zero_sign();
  69539. duk__selftest_struct_align();
  69540. duk__selftest_64bit_arithmetic();
  69541. duk__selftest_cast_double_to_small_uint();
  69542. duk__selftest_cast_double_to_uint32();
  69543. }
  69544. #undef DUK__DBLUNION_CMP_TRUE
  69545. #undef DUK__DBLUNION_CMP_FALSE
  69546. #endif /* DUK_USE_SELF_TESTS */
  69547. /* include removed: duk_internal.h */
  69548. #line 2 "duk_tval.c"
  69549. #if defined(DUK_USE_FASTINT)
  69550. /*
  69551. * Manually optimized double-to-fastint downgrade check.
  69552. *
  69553. * This check has a large impact on performance, especially for fastint
  69554. * slow paths, so must be changed carefully. The code should probably be
  69555. * optimized for the case where the result does not fit into a fastint,
  69556. * to minimize the penalty for "slow path code" dealing with fractions etc.
  69557. *
  69558. * At least on one tested soft float ARM platform double-to-int64 coercion
  69559. * is very slow (and sometimes produces incorrect results, see self tests).
  69560. * This algorithm combines a fastint compatibility check and extracting the
  69561. * integer value from an IEEE double for setting the tagged fastint. For
  69562. * other platforms a more naive approach might be better.
  69563. *
  69564. * See doc/fastint.rst for details.
  69565. */
  69566. DUK_INTERNAL DUK_ALWAYS_INLINE void duk_tval_set_number_chkfast(duk_tval *tv, duk_double_t x) {
  69567. duk_double_union du;
  69568. duk_int64_t i;
  69569. duk_small_int_t expt;
  69570. duk_small_int_t shift;
  69571. /* XXX: optimize for packed duk_tval directly? */
  69572. du.d = x;
  69573. i = (duk_int64_t) DUK_DBLUNION_GET_INT64(&du);
  69574. expt = (duk_small_int_t) ((i >> 52) & 0x07ff);
  69575. shift = expt - 1023;
  69576. if (shift >= 0 && shift <= 46) { /* exponents 1023 to 1069 */
  69577. duk_int64_t t;
  69578. if (((0x000fffffffffffffLL >> shift) & i) == 0) {
  69579. t = i | 0x0010000000000000LL; /* implicit leading one */
  69580. t = t & 0x001fffffffffffffLL;
  69581. t = t >> (52 - shift);
  69582. if (i < 0) {
  69583. t = -t;
  69584. }
  69585. DUK_TVAL_SET_FASTINT(tv, t);
  69586. return;
  69587. }
  69588. } else if (shift == -1023) { /* exponent 0 */
  69589. if (i >= 0 && (i & 0x000fffffffffffffLL) == 0) {
  69590. /* Note: reject negative zero. */
  69591. DUK_TVAL_SET_FASTINT(tv, (duk_int64_t) 0);
  69592. return;
  69593. }
  69594. } else if (shift == 47) { /* exponent 1070 */
  69595. if (i < 0 && (i & 0x000fffffffffffffLL) == 0) {
  69596. DUK_TVAL_SET_FASTINT(tv, (duk_int64_t) DUK_FASTINT_MIN);
  69597. return;
  69598. }
  69599. }
  69600. DUK_TVAL_SET_DOUBLE(tv, x);
  69601. return;
  69602. }
  69603. /*
  69604. * Manually optimized number-to-double conversion
  69605. */
  69606. #if defined(DUK_USE_FASTINT) && defined(DUK_USE_PACKED_TVAL)
  69607. DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_packed(duk_tval *tv) {
  69608. duk_double_union du;
  69609. duk_uint64_t t;
  69610. t = (duk_uint64_t) DUK_DBLUNION_GET_UINT64(tv);
  69611. if ((t >> 48) != DUK_TAG_FASTINT) {
  69612. return tv->d;
  69613. } else if (t & 0x0000800000000000ULL) {
  69614. t = (duk_uint64_t) (-((duk_int64_t) t)); /* avoid unary minus on unsigned */
  69615. t = t & 0x0000ffffffffffffULL; /* negative */
  69616. t |= 0xc330000000000000ULL;
  69617. DUK_DBLUNION_SET_UINT64(&du, t);
  69618. return du.d + 4503599627370496.0; /* 1 << 52 */
  69619. } else if (t != 0) {
  69620. t &= 0x0000ffffffffffffULL; /* positive */
  69621. t |= 0x4330000000000000ULL;
  69622. DUK_DBLUNION_SET_UINT64(&du, t);
  69623. return du.d - 4503599627370496.0; /* 1 << 52 */
  69624. } else {
  69625. return 0.0; /* zero */
  69626. }
  69627. }
  69628. #endif /* DUK_USE_FASTINT && DUK_USE_PACKED_TVAL */
  69629. #if 0 /* unused */
  69630. #if defined(DUK_USE_FASTINT) && !defined(DUK_USE_PACKED_TVAL)
  69631. DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_unpacked(duk_tval *tv) {
  69632. duk_double_union du;
  69633. duk_uint64_t t;
  69634. DUK_ASSERT(tv->t == DUK__TAG_NUMBER || tv->t == DUK_TAG_FASTINT);
  69635. if (tv->t == DUK_TAG_FASTINT) {
  69636. if (tv->v.fi >= 0) {
  69637. t = 0x4330000000000000ULL | (duk_uint64_t) tv->v.fi;
  69638. DUK_DBLUNION_SET_UINT64(&du, t);
  69639. return du.d - 4503599627370496.0; /* 1 << 52 */
  69640. } else {
  69641. t = 0xc330000000000000ULL | (duk_uint64_t) (-tv->v.fi);
  69642. DUK_DBLUNION_SET_UINT64(&du, t);
  69643. return du.d + 4503599627370496.0; /* 1 << 52 */
  69644. }
  69645. } else {
  69646. return tv->v.d;
  69647. }
  69648. }
  69649. #endif /* DUK_USE_FASTINT && DUK_USE_PACKED_TVAL */
  69650. #endif /* 0 */
  69651. #if defined(DUK_USE_FASTINT) && !defined(DUK_USE_PACKED_TVAL)
  69652. DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_unpacked_fastint(duk_tval *tv) {
  69653. duk_double_union du;
  69654. duk_uint64_t t;
  69655. DUK_ASSERT(tv->t == DUK_TAG_FASTINT);
  69656. if (tv->v.fi >= 0) {
  69657. t = 0x4330000000000000ULL | (duk_uint64_t) tv->v.fi;
  69658. DUK_DBLUNION_SET_UINT64(&du, t);
  69659. return du.d - 4503599627370496.0; /* 1 << 52 */
  69660. } else {
  69661. t = 0xc330000000000000ULL | (duk_uint64_t) (-tv->v.fi);
  69662. DUK_DBLUNION_SET_UINT64(&du, t);
  69663. return du.d + 4503599627370496.0; /* 1 << 52 */
  69664. }
  69665. }
  69666. #endif /* DUK_USE_FASTINT && DUK_USE_PACKED_TVAL */
  69667. #endif /* DUK_USE_FASTINT */
  69668. #line 1 "duk_unicode_tables.c"
  69669. /*
  69670. * Unicode support tables automatically generated during build.
  69671. */
  69672. /* include removed: duk_internal.h */
  69673. /*
  69674. * Unicode tables containing ranges of Unicode characters in a
  69675. * packed format. These tables are used to match non-ASCII
  69676. * characters of complex productions by resorting to a linear
  69677. * range-by-range comparison. This is very slow, but is expected
  69678. * to be very rare in practical Ecmascript source code, and thus
  69679. * compactness is most important.
  69680. *
  69681. * The tables are matched using uni_range_match() and the format
  69682. * is described in src/extract_chars.py.
  69683. */
  69684. #ifdef DUK_USE_SOURCE_NONBMP
  69685. /* IdentifierStart production with ASCII excluded */
  69686. /* duk_unicode_ids_noa[] */
  69687. /*
  69688. * Automatically generated by extract_chars.py, do not edit!
  69689. */
  69690. const duk_uint8_t duk_unicode_ids_noa[791] = {
  69691. 249,176,176,80,111,7,47,15,47,254,11,197,191,0,72,2,15,115,66,19,57,2,34,2,
  69692. 240,66,244,50,247,185,248,234,241,99,8,241,127,58,240,182,47,31,241,191,21,
  69693. 18,245,50,15,1,24,27,35,15,2,2,240,239,15,244,156,15,10,241,26,21,6,240,
  69694. 101,10,4,15,9,240,159,157,242,100,15,4,8,159,1,98,102,115,19,240,98,98,4,
  69695. 52,15,2,14,18,47,0,31,5,85,19,240,98,98,18,18,31,17,50,15,5,47,2,130,34,
  69696. 240,98,98,18,68,15,4,15,1,31,21,115,19,240,98,98,18,68,15,16,18,47,1,15,3,
  69697. 2,84,34,52,18,2,20,20,36,191,8,15,38,114,34,240,114,146,68,15,12,23,31,21,
  69698. 114,34,240,114,146,68,15,18,2,31,1,31,4,114,34,241,147,15,2,15,3,31,10,86,
  69699. 240,36,240,130,130,3,111,44,242,2,29,111,44,18,3,18,3,7,50,98,34,2,3,18,50,
  69700. 26,3,66,15,7,31,20,15,49,114,241,79,13,79,101,241,191,6,15,2,85,52,4,24,37,
  69701. 205,15,3,241,107,241,178,4,255,224,59,35,54,32,35,63,25,35,63,17,35,54,32,
  69702. 35,62,47,41,35,63,51,241,127,0,240,47,69,223,254,21,227,240,18,240,166,243,
  69703. 180,47,1,194,63,0,240,47,0,240,47,0,194,47,1,242,79,21,5,15,53,244,137,241,
  69704. 146,6,243,107,240,223,37,240,227,76,241,207,7,111,42,240,122,242,95,68,15,
  69705. 79,241,255,3,111,41,240,238,31,2,241,111,12,241,79,27,43,241,79,93,50,63,0,
  69706. 251,15,50,255,224,8,53,63,22,53,55,32,32,32,47,15,63,37,38,32,66,38,67,53,
  69707. 92,98,38,246,96,224,240,44,245,112,80,57,32,68,112,32,32,35,42,51,100,80,
  69708. 240,63,25,255,233,107,241,242,241,242,247,87,63,3,241,107,242,106,15,2,240,
  69709. 122,98,98,98,98,98,98,98,111,66,15,254,12,146,240,184,132,52,95,70,114,47,
  69710. 74,35,111,25,79,78,240,63,11,242,127,0,255,224,244,255,240,0,138,143,60,
  69711. 255,240,4,11,239,38,255,227,127,243,95,30,63,253,79,0,177,240,111,31,240,
  69712. 47,9,159,64,241,152,63,87,51,33,240,9,244,39,34,35,47,7,240,255,36,240,15,
  69713. 34,243,5,64,240,15,12,191,7,240,191,13,143,31,240,224,242,47,25,240,146,39,
  69714. 240,111,7,64,111,32,32,65,52,48,32,240,162,241,85,53,53,166,38,248,63,19,
  69715. 240,255,255,0,26,150,223,7,95,33,255,240,0,255,143,254,2,3,242,227,245,175,
  69716. 24,109,70,2,146,194,66,2,18,18,245,207,19,255,224,93,240,79,48,63,38,241,
  69717. 171,246,100,47,119,241,111,10,127,10,207,73,69,53,53,50,241,91,47,10,47,3,
  69718. 33,46,61,241,79,107,243,127,37,255,223,13,79,33,242,31,15,240,63,11,242,
  69719. 127,14,63,20,87,36,241,207,142,255,226,86,83,2,241,194,20,3,240,127,156,
  69720. 240,107,240,175,184,15,1,50,34,240,191,30,240,223,117,242,107,240,107,240,
  69721. 63,127,243,159,254,42,239,37,243,223,29,255,238,68,255,226,97,248,63,83,
  69722. 255,234,145,255,227,33,255,240,2,44,95,254,18,191,255,0,52,187,31,255,0,18,
  69723. 242,244,82,243,114,19,3,19,50,178,2,98,243,18,51,114,98,240,194,50,66,4,98,
  69724. 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,
  69725. 240,1,114,143,255,0,149,201,241,191,254,242,124,252,239,255,0,46,214,255,
  69726. 225,16,0,
  69727. };
  69728. #else
  69729. /* IdentifierStart production with ASCII and non-BMP excluded */
  69730. /* duk_unicode_ids_noabmp[] */
  69731. /*
  69732. * Automatically generated by extract_chars.py, do not edit!
  69733. */
  69734. const duk_uint8_t duk_unicode_ids_noabmp[611] = {
  69735. 249,176,176,80,111,7,47,15,47,254,11,197,191,0,72,2,15,115,66,19,57,2,34,2,
  69736. 240,66,244,50,247,185,248,234,241,99,8,241,127,58,240,182,47,31,241,191,21,
  69737. 18,245,50,15,1,24,27,35,15,2,2,240,239,15,244,156,15,10,241,26,21,6,240,
  69738. 101,10,4,15,9,240,159,157,242,100,15,4,8,159,1,98,102,115,19,240,98,98,4,
  69739. 52,15,2,14,18,47,0,31,5,85,19,240,98,98,18,18,31,17,50,15,5,47,2,130,34,
  69740. 240,98,98,18,68,15,4,15,1,31,21,115,19,240,98,98,18,68,15,16,18,47,1,15,3,
  69741. 2,84,34,52,18,2,20,20,36,191,8,15,38,114,34,240,114,146,68,15,12,23,31,21,
  69742. 114,34,240,114,146,68,15,18,2,31,1,31,4,114,34,241,147,15,2,15,3,31,10,86,
  69743. 240,36,240,130,130,3,111,44,242,2,29,111,44,18,3,18,3,7,50,98,34,2,3,18,50,
  69744. 26,3,66,15,7,31,20,15,49,114,241,79,13,79,101,241,191,6,15,2,85,52,4,24,37,
  69745. 205,15,3,241,107,241,178,4,255,224,59,35,54,32,35,63,25,35,63,17,35,54,32,
  69746. 35,62,47,41,35,63,51,241,127,0,240,47,69,223,254,21,227,240,18,240,166,243,
  69747. 180,47,1,194,63,0,240,47,0,240,47,0,194,47,1,242,79,21,5,15,53,244,137,241,
  69748. 146,6,243,107,240,223,37,240,227,76,241,207,7,111,42,240,122,242,95,68,15,
  69749. 79,241,255,3,111,41,240,238,31,2,241,111,12,241,79,27,43,241,79,93,50,63,0,
  69750. 251,15,50,255,224,8,53,63,22,53,55,32,32,32,47,15,63,37,38,32,66,38,67,53,
  69751. 92,98,38,246,96,224,240,44,245,112,80,57,32,68,112,32,32,35,42,51,100,80,
  69752. 240,63,25,255,233,107,241,242,241,242,247,87,63,3,241,107,242,106,15,2,240,
  69753. 122,98,98,98,98,98,98,98,111,66,15,254,12,146,240,184,132,52,95,70,114,47,
  69754. 74,35,111,25,79,78,240,63,11,242,127,0,255,224,244,255,240,0,138,143,60,
  69755. 255,240,4,11,239,38,255,227,127,243,95,30,63,253,79,0,177,240,111,31,240,
  69756. 47,9,159,64,241,152,63,87,51,33,240,9,244,39,34,35,47,7,240,255,36,240,15,
  69757. 34,243,5,64,240,15,12,191,7,240,191,13,143,31,240,224,242,47,25,240,146,39,
  69758. 240,111,7,64,111,32,32,65,52,48,32,240,162,241,85,53,53,166,38,248,63,19,
  69759. 240,255,255,0,26,150,223,7,95,33,255,240,0,255,143,254,2,3,242,227,245,175,
  69760. 24,109,70,2,146,194,66,2,18,18,245,207,19,255,224,93,240,79,48,63,38,241,
  69761. 171,246,100,47,119,241,111,10,127,10,207,73,69,53,53,50,0,
  69762. };
  69763. #endif
  69764. #ifdef DUK_USE_SOURCE_NONBMP
  69765. /* IdentifierStart production with Letter and ASCII excluded */
  69766. /* duk_unicode_ids_m_let_noa[] */
  69767. /*
  69768. * Automatically generated by extract_chars.py, do not edit!
  69769. */
  69770. const duk_uint8_t duk_unicode_ids_m_let_noa[42] = {
  69771. 255,240,0,94,18,255,233,99,241,51,63,254,215,32,240,184,240,2,255,240,6,89,
  69772. 249,255,240,4,148,79,37,255,224,192,9,15,120,79,255,0,15,30,245,48,
  69773. };
  69774. #else
  69775. /* IdentifierStart production with Letter, ASCII, and non-BMP excluded */
  69776. /* duk_unicode_ids_m_let_noabmp[] */
  69777. /*
  69778. * Automatically generated by extract_chars.py, do not edit!
  69779. */
  69780. const duk_uint8_t duk_unicode_ids_m_let_noabmp[24] = {
  69781. 255,240,0,94,18,255,233,99,241,51,63,254,215,32,240,184,240,2,255,240,6,89,
  69782. 249,0,
  69783. };
  69784. #endif
  69785. #ifdef DUK_USE_SOURCE_NONBMP
  69786. /* IdentifierPart production with IdentifierStart and ASCII excluded */
  69787. /* duk_unicode_idp_m_ids_noa[] */
  69788. /*
  69789. * Automatically generated by extract_chars.py, do not edit!
  69790. */
  69791. const duk_uint8_t duk_unicode_idp_m_ids_noa[397] = {
  69792. 255,225,243,246,15,254,0,116,255,191,29,32,33,33,32,243,170,242,47,15,112,
  69793. 245,118,53,49,35,57,240,144,241,15,11,244,218,240,25,241,56,241,67,40,34,
  69794. 36,241,210,249,99,242,130,47,2,38,177,57,240,50,242,160,38,49,50,160,177,
  69795. 57,240,50,242,160,36,81,50,64,240,107,64,194,242,160,39,34,34,240,97,57,
  69796. 240,50,242,160,38,49,50,145,177,57,240,64,242,212,66,35,160,240,9,240,50,
  69797. 242,198,34,35,129,193,57,240,65,242,160,38,34,35,129,193,57,240,65,242,198,
  69798. 34,35,160,177,57,240,65,243,128,85,32,39,240,65,242,240,54,215,41,244,144,
  69799. 53,33,197,57,243,1,121,192,32,32,81,242,63,4,33,106,47,20,160,245,111,4,41,
  69800. 211,82,34,54,67,235,46,255,225,179,47,254,42,98,240,242,240,241,241,1,243,
  69801. 79,14,160,57,241,50,57,248,16,246,139,91,185,245,47,1,129,121,242,244,242,
  69802. 185,47,13,58,121,245,132,242,31,1,201,240,56,210,241,9,105,241,237,242,47,
  69803. 4,153,121,246,130,47,5,80,80,251,255,23,240,115,255,225,0,31,35,31,5,15,
  69804. 109,197,4,191,254,175,34,247,240,245,47,16,255,225,30,95,91,31,255,0,100,
  69805. 121,159,55,13,31,100,31,254,0,64,64,80,240,148,244,161,242,79,1,201,127,2,
  69806. 240,9,240,231,240,188,241,227,242,29,240,25,244,29,208,145,57,241,48,242,
  69807. 96,34,49,97,32,255,224,21,114,19,159,255,0,62,24,15,254,29,95,0,240,38,209,
  69808. 240,162,251,41,241,112,255,225,177,15,254,25,105,255,228,75,34,22,63,26,37,
  69809. 15,254,75,66,242,126,241,25,240,34,241,250,255,240,10,249,228,69,151,54,
  69810. 241,3,248,98,255,228,125,242,47,255,12,23,244,254,0,
  69811. };
  69812. #else
  69813. /* IdentifierPart production with IdentifierStart, ASCII, and non-BMP excluded */
  69814. /* duk_unicode_idp_m_ids_noabmp[] */
  69815. /*
  69816. * Automatically generated by extract_chars.py, do not edit!
  69817. */
  69818. const duk_uint8_t duk_unicode_idp_m_ids_noabmp[348] = {
  69819. 255,225,243,246,15,254,0,116,255,191,29,32,33,33,32,243,170,242,47,15,112,
  69820. 245,118,53,49,35,57,240,144,241,15,11,244,218,240,25,241,56,241,67,40,34,
  69821. 36,241,210,249,99,242,130,47,2,38,177,57,240,50,242,160,38,49,50,160,177,
  69822. 57,240,50,242,160,36,81,50,64,240,107,64,194,242,160,39,34,34,240,97,57,
  69823. 240,50,242,160,38,49,50,145,177,57,240,64,242,212,66,35,160,240,9,240,50,
  69824. 242,198,34,35,129,193,57,240,65,242,160,38,34,35,129,193,57,240,65,242,198,
  69825. 34,35,160,177,57,240,65,243,128,85,32,39,240,65,242,240,54,215,41,244,144,
  69826. 53,33,197,57,243,1,121,192,32,32,81,242,63,4,33,106,47,20,160,245,111,4,41,
  69827. 211,82,34,54,67,235,46,255,225,179,47,254,42,98,240,242,240,241,241,1,243,
  69828. 79,14,160,57,241,50,57,248,16,246,139,91,185,245,47,1,129,121,242,244,242,
  69829. 185,47,13,58,121,245,132,242,31,1,201,240,56,210,241,9,105,241,237,242,47,
  69830. 4,153,121,246,130,47,5,80,80,251,255,23,240,115,255,225,0,31,35,31,5,15,
  69831. 109,197,4,191,254,175,34,247,240,245,47,16,255,225,30,95,91,31,255,0,100,
  69832. 121,159,55,13,31,100,31,254,0,64,64,80,240,148,244,161,242,79,1,201,127,2,
  69833. 240,9,240,231,240,188,241,227,242,29,240,25,244,29,208,145,57,241,48,242,
  69834. 96,34,49,97,32,255,224,21,114,19,159,255,0,62,24,15,254,29,95,0,240,38,209,
  69835. 240,162,251,41,241,112,0,
  69836. };
  69837. #endif
  69838. /*
  69839. * Case conversion tables generated using src/extract_caseconv.py.
  69840. */
  69841. /* duk_unicode_caseconv_uc[] */
  69842. /* duk_unicode_caseconv_lc[] */
  69843. /*
  69844. * Automatically generated by extract_caseconv.py, do not edit!
  69845. */
  69846. const duk_uint8_t duk_unicode_caseconv_uc[1288] = {
  69847. 132,3,128,3,0,184,7,192,6,192,112,35,242,199,224,64,74,192,49,32,128,162,
  69848. 128,108,65,1,189,129,254,131,3,173,3,136,6,7,98,7,34,68,15,12,14,140,72,30,
  69849. 104,28,112,32,67,0,65,4,0,138,0,128,4,1,88,65,76,83,15,128,15,132,8,31,16,
  69850. 31,24,12,62,64,62,80,32,124,192,124,224,64,250,0,250,64,97,246,1,246,129,3,
  69851. 238,3,247,64,135,220,135,242,2,15,187,15,237,2,31,120,31,248,4,62,244,63,
  69852. 212,8,125,240,127,232,16,253,128,253,192,33,253,1,253,128,67,252,3,253,0,
  69853. 136,92,8,88,8,18,104,18,91,26,44,48,44,0,94,90,0,33,64,155,253,7,252,132,
  69854. 212,0,32,32,32,6,0,76,192,76,129,128,157,0,156,136,1,75,1,74,46,2,244,2,
  69855. 242,12,6,12,6,8,16,13,8,13,0,48,27,64,27,48,64,57,192,57,162,0,119,192,119,
  69856. 132,128,252,128,252,20,2,35,2,34,18,4,142,4,140,20,13,196,13,192,16,30,200,
  69857. 30,192,192,70,16,70,2,32,145,96,145,70,193,48,129,48,67,130,104,130,104,44,
  69858. 30,1,30,0,150,61,66,61,64,192,125,68,125,100,33,99,65,99,56,50,200,18,200,
  69859. 6,69,157,133,157,96,169,144,105,144,11,211,64,211,64,12,167,35,167,34,15,
  69860. 78,103,78,100,126,157,234,157,228,21,59,253,59,240,90,122,26,122,0,163,128,
  69861. 214,128,214,2,1,197,1,196,6,3,140,3,136,12,7,200,7,196,16,20,0,13,48,32,63,
  69862. 128,63,112,69,142,101,142,64,130,1,136,1,135,4,3,114,3,112,8,26,120,202,
  69863. 120,176,65,1,30,1,29,130,2,105,1,150,5,255,96,22,160,115,128,31,224,47,0,
  69864. 38,32,9,32,47,224,10,96,48,0,72,96,50,64,50,32,50,160,62,192,51,32,51,0,51,
  69865. 64,71,160,51,192,68,0,53,0,52,224,55,224,62,224,59,160,49,192,62,96,62,32,
  69866. 74,5,141,224,74,37,141,160,74,69,142,0,74,96,48,32,74,128,48,192,75,32,49,
  69867. 224,75,96,50,0,76,0,50,96,76,96,50,128,76,180,241,160,77,0,50,224,77,101,
  69868. 140,64,78,37,141,192,78,64,51,160,78,160,51,224,79,165,140,128,81,0,53,192,
  69869. 81,32,72,128,81,128,72,160,82,64,54,224,104,160,115,32,110,224,110,192,117,
  69870. 128,112,192,120,64,116,96,121,128,113,128,122,0,114,64,122,32,115,0,122,
  69871. 160,116,192,122,192,116,0,122,224,121,224,126,0,115,64,126,32,116,32,126,
  69872. 64,127,32,126,160,114,160,153,224,152,3,175,52,239,163,175,165,140,99,211,
  69873. 99,204,3,247,192,115,35,252,163,253,132,41,196,38,68,48,132,48,101,140,37,
  69874. 140,5,140,160,71,69,140,192,71,217,128,55,224,5,48,5,48,20,152,10,240,1,56,
  69875. 7,194,0,74,3,12,3,144,192,230,64,194,0,192,64,236,48,58,80,48,128,48,16,88,
  69876. 120,20,212,21,72,122,90,0,72,3,49,30,151,128,21,0,194,7,166,32,5,112,48,
  69877. 161,233,152,1,100,12,40,122,106,0,65,2,190,31,80,128,233,64,196,199,212,
  69878. 176,58,80,49,48,48,1,245,76,14,148,12,76,12,4,125,91,3,165,3,19,3,66,31,
  69879. 128,135,194,0,230,71,224,97,240,144,57,145,248,40,124,40,14,100,126,14,31,
  69880. 11,3,153,31,132,135,195,0,230,71,225,97,240,208,57,145,248,104,124,56,14,
  69881. 100,126,30,31,15,3,153,31,136,135,194,0,230,71,226,97,240,144,57,145,248,
  69882. 168,124,40,14,100,126,46,31,11,3,153,31,140,135,195,0,230,71,227,97,240,
  69883. 208,57,145,248,232,124,56,14,100,126,62,31,15,3,153,31,144,135,202,0,230,
  69884. 71,228,97,242,144,57,145,249,40,124,168,14,100,126,78,31,43,3,153,31,148,
  69885. 135,203,0,230,71,229,97,242,208,57,145,249,104,124,184,14,100,126,94,31,47,
  69886. 3,153,31,152,135,202,0,230,71,230,97,242,144,57,145,249,168,124,168,14,100,
  69887. 126,110,31,43,3,153,31,156,135,203,0,230,71,231,97,242,208,57,145,249,232,
  69888. 124,184,14,100,126,126,31,47,3,153,31,160,135,218,0,230,71,232,97,246,144,
  69889. 57,145,250,40,125,168,14,100,126,142,31,107,3,153,31,164,135,219,0,230,71,
  69890. 233,97,246,208,57,145,250,104,125,184,14,100,126,158,31,111,3,153,31,168,
  69891. 135,218,0,230,71,234,97,246,144,57,145,250,168,125,168,14,100,126,174,31,
  69892. 107,3,153,31,172,135,219,0,230,71,235,97,246,208,57,145,250,232,125,184,14,
  69893. 100,126,190,31,111,3,153,31,178,135,238,128,230,71,236,224,57,16,57,145,
  69894. 251,72,14,24,14,100,126,218,3,145,3,66,31,183,192,228,64,208,128,230,71,
  69895. 239,32,57,16,57,145,252,40,127,40,14,100,127,14,3,151,3,153,31,196,128,226,
  69896. 64,230,71,241,160,57,112,52,33,252,124,14,92,13,8,14,100,127,50,3,151,3,
  69897. 153,31,210,192,230,64,194,0,192,7,244,240,57,144,48,128,48,17,253,104,14,
  69898. 100,13,8,127,95,3,153,3,8,3,66,31,226,192,233,64,194,0,192,7,248,240,58,80,
  69899. 48,128,48,17,254,72,14,132,12,76,127,154,3,165,3,66,31,231,192,233,64,194,
  69900. 0,208,135,252,161,255,160,57,145,255,56,14,164,14,100,127,210,3,143,3,153,
  69901. 31,246,128,234,64,208,135,253,240,58,144,52,32,57,145,255,200,14,164,14,
  69902. 103,236,2,0,70,0,70,251,1,128,17,128,18,126,192,160,4,96,4,207,176,60,1,24,
  69903. 1,24,1,39,236,19,0,70,0,70,0,76,251,5,128,20,192,21,62,193,160,5,48,5,79,
  69904. 177,56,21,16,21,27,236,82,5,68,5,53,251,21,129,81,1,78,254,197,160,84,224,
  69905. 84,111,177,120,21,16,20,244,
  69906. };
  69907. const duk_uint8_t duk_unicode_caseconv_lc[616] = {
  69908. 144,3,0,3,128,184,6,192,7,192,112,24,144,37,96,64,54,32,81,64,128,226,0,
  69909. 235,65,129,199,1,230,130,3,145,3,177,34,7,70,7,134,36,15,244,13,236,24,32,
  69910. 0,34,129,0,65,0,67,4,0,166,32,172,41,132,40,11,64,19,15,132,15,128,8,31,24,
  69911. 31,16,12,62,80,62,64,32,124,224,124,192,64,250,64,250,0,97,246,129,246,1,3,
  69912. 241,3,240,2,7,230,7,228,4,15,212,15,208,8,31,184,31,176,4,63,116,62,224,8,
  69913. 127,32,125,200,32,254,192,254,128,33,253,161,247,96,67,253,3,252,0,135,250,
  69914. 135,222,129,15,252,15,188,2,31,250,31,124,4,66,192,66,224,64,146,216,147,
  69915. 64,209,96,1,97,130,242,199,224,35,240,95,228,63,232,38,161,1,0,1,1,48,2,
  69916. 100,2,102,12,4,228,4,232,64,10,80,10,89,112,23,144,23,160,96,48,64,48,96,
  69917. 128,104,0,104,65,128,217,128,218,2,1,203,1,204,18,3,188,3,190,36,7,200,7,
  69918. 204,16,15,192,15,201,64,34,32,34,49,32,72,192,72,225,64,220,0,220,65,1,236,
  69919. 1,236,140,4,96,4,97,34,9,20,9,22,108,19,4,19,8,56,38,128,38,138,193,224,1,
  69920. 224,25,99,212,3,212,44,7,214,71,212,66,22,51,150,52,3,44,128,44,129,100,89,
  69921. 214,89,216,10,153,2,153,4,189,52,5,52,8,202,114,42,114,48,244,230,84,230,
  69922. 103,233,222,105,222,129,83,191,83,191,133,167,160,167,161,10,48,13,48,20,0,
  69923. 32,26,192,26,208,64,56,128,56,192,192,113,64,113,129,1,251,129,252,2,44,
  69924. 114,44,115,4,16,12,56,12,64,32,27,128,27,144,64,211,197,211,198,2,8,6,88,9,
  69925. 164,16,17,216,17,224,47,245,1,120,0,255,1,129,2,83,1,134,2,84,1,142,1,221,
  69926. 1,143,2,89,1,144,2,91,1,145,1,146,1,147,2,96,1,148,2,99,1,151,2,104,1,152,
  69927. 1,153,1,157,2,114,1,159,2,117,1,167,1,168,1,174,2,136,1,183,2,146,1,241,1,
  69928. 243,1,246,1,149,1,247,1,191,2,32,1,158,2,58,44,101,2,61,1,154,2,62,44,102,
  69929. 2,67,1,128,2,68,2,137,2,69,2,140,3,118,3,119,3,134,3,172,3,140,3,204,3,207,
  69930. 3,215,3,244,3,184,3,249,3,242,4,192,4,207,30,158,0,223,31,188,31,179,31,
  69931. 204,31,195,31,236,31,229,31,252,31,243,33,38,3,201,33,42,0,107,33,43,0,229,
  69932. 33,50,33,78,33,131,33,132,44,96,44,97,44,98,2,107,44,99,29,125,44,100,2,
  69933. 125,44,109,2,81,44,110,2,113,44,111,2,80,44,112,2,82,167,125,29,121,167,
  69934. 141,2,101,2,2,97,0,52,129,131,128,
  69935. };
  69936. #line 1 "duk_util_bitdecoder.c"
  69937. /*
  69938. * Bitstream decoder.
  69939. */
  69940. /* include removed: duk_internal.h */
  69941. /* Decode 'bits' bits from the input stream (bits must be 1...24).
  69942. * When reading past bitstream end, zeroes are shifted in. The result
  69943. * is signed to match duk_bd_decode_flagged.
  69944. */
  69945. DUK_INTERNAL duk_int32_t duk_bd_decode(duk_bitdecoder_ctx *ctx, duk_small_int_t bits) {
  69946. duk_small_int_t shift;
  69947. duk_uint32_t mask;
  69948. duk_uint32_t tmp;
  69949. /* Note: cannot read more than 24 bits without possibly shifting top bits out.
  69950. * Fixable, but adds complexity.
  69951. */
  69952. DUK_ASSERT(bits >= 1 && bits <= 24);
  69953. while (ctx->currbits < bits) {
  69954. #if 0
  69955. DUK_DDD(DUK_DDDPRINT("decode_bits: shift more data (bits=%ld, currbits=%ld)",
  69956. (long) bits, (long) ctx->currbits));
  69957. #endif
  69958. ctx->currval <<= 8;
  69959. if (ctx->offset < ctx->length) {
  69960. /* If ctx->offset >= ctx->length, we "shift zeroes in"
  69961. * instead of croaking.
  69962. */
  69963. ctx->currval |= ctx->data[ctx->offset++];
  69964. }
  69965. ctx->currbits += 8;
  69966. }
  69967. #if 0
  69968. DUK_DDD(DUK_DDDPRINT("decode_bits: bits=%ld, currbits=%ld, currval=0x%08lx",
  69969. (long) bits, (long) ctx->currbits, (unsigned long) ctx->currval));
  69970. #endif
  69971. /* Extract 'top' bits of currval; note that the extracted bits do not need
  69972. * to be cleared, we just ignore them on next round.
  69973. */
  69974. shift = ctx->currbits - bits;
  69975. mask = (1 << bits) - 1;
  69976. tmp = (ctx->currval >> shift) & mask;
  69977. ctx->currbits = shift; /* remaining */
  69978. #if 0
  69979. DUK_DDD(DUK_DDDPRINT("decode_bits: %ld bits -> 0x%08lx (%ld), currbits=%ld, currval=0x%08lx",
  69980. (long) bits, (unsigned long) tmp, (long) tmp, (long) ctx->currbits, (unsigned long) ctx->currval));
  69981. #endif
  69982. return tmp;
  69983. }
  69984. DUK_INTERNAL duk_small_int_t duk_bd_decode_flag(duk_bitdecoder_ctx *ctx) {
  69985. return (duk_small_int_t) duk_bd_decode(ctx, 1);
  69986. }
  69987. /* Decode a one-bit flag, and if set, decode a value of 'bits', otherwise return
  69988. * default value. Return value is signed so that negative marker value can be
  69989. * used by caller as a "not present" value.
  69990. */
  69991. DUK_INTERNAL duk_int32_t duk_bd_decode_flagged(duk_bitdecoder_ctx *ctx, duk_small_int_t bits, duk_int32_t def_value) {
  69992. if (duk_bd_decode_flag(ctx)) {
  69993. return (duk_int32_t) duk_bd_decode(ctx, bits);
  69994. } else {
  69995. return def_value;
  69996. }
  69997. }
  69998. #line 1 "duk_util_bitencoder.c"
  69999. /*
  70000. * Bitstream encoder.
  70001. */
  70002. /* include removed: duk_internal.h */
  70003. DUK_INTERNAL void duk_be_encode(duk_bitencoder_ctx *ctx, duk_uint32_t data, duk_small_int_t bits) {
  70004. duk_uint8_t tmp;
  70005. DUK_ASSERT(ctx != NULL);
  70006. DUK_ASSERT(ctx->currbits < 8);
  70007. /* This limitation would be fixable but adds unnecessary complexity. */
  70008. DUK_ASSERT(bits >= 1 && bits <= 24);
  70009. ctx->currval = (ctx->currval << bits) | data;
  70010. ctx->currbits += bits;
  70011. while (ctx->currbits >= 8) {
  70012. if (ctx->offset < ctx->length) {
  70013. tmp = (duk_uint8_t) ((ctx->currval >> (ctx->currbits - 8)) & 0xff);
  70014. ctx->data[ctx->offset++] = tmp;
  70015. } else {
  70016. /* If buffer has been exhausted, truncate bitstream */
  70017. ctx->truncated = 1;
  70018. }
  70019. ctx->currbits -= 8;
  70020. }
  70021. }
  70022. DUK_INTERNAL void duk_be_finish(duk_bitencoder_ctx *ctx) {
  70023. duk_small_int_t npad;
  70024. DUK_ASSERT(ctx != NULL);
  70025. DUK_ASSERT(ctx->currbits < 8);
  70026. npad = (duk_small_int_t) (8 - ctx->currbits);
  70027. if (npad > 0) {
  70028. duk_be_encode(ctx, 0, npad);
  70029. }
  70030. DUK_ASSERT(ctx->currbits == 0);
  70031. }
  70032. #line 1 "duk_util_bufwriter.c"
  70033. /*
  70034. * Fast buffer writer with spare management.
  70035. */
  70036. /* include removed: duk_internal.h */
  70037. /*
  70038. * Macro support functions (use only macros in calling code)
  70039. */
  70040. 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) {
  70041. duk_uint8_t *p;
  70042. DUK_ASSERT(thr != NULL);
  70043. DUK_ASSERT(bw_ctx != NULL);
  70044. DUK_UNREF(thr);
  70045. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, bw_ctx->buf);
  70046. DUK_ASSERT(p != NULL || (DUK_HBUFFER_DYNAMIC_GET_SIZE(bw_ctx->buf) == 0 && curr_offset == 0 && new_length == 0));
  70047. bw_ctx->p = p + curr_offset;
  70048. bw_ctx->p_base = p;
  70049. bw_ctx->p_limit = p + new_length;
  70050. }
  70051. DUK_INTERNAL void duk_bw_init(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_hbuffer_dynamic *h_buf) {
  70052. DUK_ASSERT(thr != NULL);
  70053. DUK_ASSERT(bw_ctx != NULL);
  70054. DUK_ASSERT(h_buf != NULL);
  70055. DUK_UNREF(thr);
  70056. bw_ctx->buf = h_buf;
  70057. duk__bw_update_ptrs(thr, bw_ctx, 0, DUK_HBUFFER_DYNAMIC_GET_SIZE(h_buf));
  70058. }
  70059. DUK_INTERNAL void duk_bw_init_pushbuf(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_size_t buf_size) {
  70060. duk_context *ctx;
  70061. DUK_ASSERT(thr != NULL);
  70062. DUK_ASSERT(bw_ctx != NULL);
  70063. ctx = (duk_context *) thr;
  70064. (void) duk_push_dynamic_buffer(ctx, buf_size);
  70065. bw_ctx->buf = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, -1);
  70066. duk__bw_update_ptrs(thr, bw_ctx, 0, buf_size);
  70067. }
  70068. /* Resize target buffer for requested size. Called by the macro only when the
  70069. * fast path test (= there is space) fails.
  70070. */
  70071. DUK_INTERNAL duk_uint8_t *duk_bw_resize(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_size_t sz) {
  70072. duk_size_t curr_off;
  70073. duk_size_t add_sz;
  70074. duk_size_t new_sz;
  70075. DUK_ASSERT(thr != NULL);
  70076. DUK_ASSERT(bw_ctx != NULL);
  70077. /* We could do this operation without caller updating bw_ctx->ptr,
  70078. * but by writing it back here we can share code better.
  70079. */
  70080. curr_off = (duk_size_t) (bw_ctx->p - bw_ctx->p_base);
  70081. add_sz = (curr_off >> DUK_BW_SPARE_SHIFT) + DUK_BW_SPARE_ADD;
  70082. new_sz = curr_off + sz + add_sz;
  70083. if (new_sz < curr_off) {
  70084. /* overflow */
  70085. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_BUFFER_TOO_LONG);
  70086. return NULL; /* not reachable */
  70087. }
  70088. #if 0 /* for manual torture testing: tight allocation, useful with valgrind */
  70089. new_sz = curr_off + sz;
  70090. #endif
  70091. /* This is important to ensure dynamic buffer data pointer is not
  70092. * NULL (which is possible if buffer size is zero), which in turn
  70093. * causes portability issues with e.g. memmove() and memcpy().
  70094. */
  70095. DUK_ASSERT(new_sz >= 1);
  70096. DUK_DD(DUK_DDPRINT("resize bufferwriter from %ld to %ld (add_sz=%ld)", (long) curr_off, (long) new_sz, (long) add_sz));
  70097. duk_hbuffer_resize(thr, bw_ctx->buf, new_sz);
  70098. duk__bw_update_ptrs(thr, bw_ctx, curr_off, new_sz);
  70099. return bw_ctx->p;
  70100. }
  70101. /* Make buffer compact, matching current written size. */
  70102. DUK_INTERNAL void duk_bw_compact(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx) {
  70103. duk_size_t len;
  70104. DUK_ASSERT(thr != NULL);
  70105. DUK_ASSERT(bw_ctx != NULL);
  70106. DUK_UNREF(thr);
  70107. len = (duk_size_t) (bw_ctx->p - bw_ctx->p_base);
  70108. duk_hbuffer_resize(thr, bw_ctx->buf, len);
  70109. duk__bw_update_ptrs(thr, bw_ctx, len, len);
  70110. }
  70111. DUK_INTERNAL void duk_bw_write_raw_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t src_off, duk_size_t len) {
  70112. duk_uint8_t *p_base;
  70113. DUK_ASSERT(thr != NULL);
  70114. DUK_ASSERT(bw != NULL);
  70115. DUK_ASSERT(src_off <= DUK_BW_GET_SIZE(thr, bw));
  70116. DUK_ASSERT(len <= DUK_BW_GET_SIZE(thr, bw));
  70117. DUK_ASSERT(src_off + len <= DUK_BW_GET_SIZE(thr, bw));
  70118. DUK_UNREF(thr);
  70119. p_base = bw->p_base;
  70120. DUK_MEMCPY((void *) bw->p,
  70121. (const void *) (p_base + src_off),
  70122. (size_t) len);
  70123. bw->p += len;
  70124. }
  70125. DUK_INTERNAL void duk_bw_write_ensure_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t src_off, duk_size_t len) {
  70126. DUK_ASSERT(thr != NULL);
  70127. DUK_ASSERT(bw != NULL);
  70128. DUK_ASSERT(src_off <= DUK_BW_GET_SIZE(thr, bw));
  70129. DUK_ASSERT(len <= DUK_BW_GET_SIZE(thr, bw));
  70130. DUK_ASSERT(src_off + len <= DUK_BW_GET_SIZE(thr, bw));
  70131. DUK_UNREF(thr);
  70132. DUK_BW_ENSURE(thr, bw, len);
  70133. duk_bw_write_raw_slice(thr, bw, src_off, len);
  70134. }
  70135. 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) {
  70136. duk_uint8_t *p_base;
  70137. duk_size_t buf_sz, move_sz;
  70138. DUK_ASSERT(thr != NULL);
  70139. DUK_ASSERT(bw != NULL);
  70140. DUK_ASSERT(dst_off <= DUK_BW_GET_SIZE(thr, bw));
  70141. DUK_ASSERT(buf != NULL);
  70142. DUK_UNREF(thr);
  70143. p_base = bw->p_base;
  70144. buf_sz = bw->p - p_base;
  70145. move_sz = buf_sz - dst_off;
  70146. DUK_ASSERT(p_base != NULL); /* buffer size is >= 1 */
  70147. DUK_MEMMOVE((void *) (p_base + dst_off + len),
  70148. (const void *) (p_base + dst_off),
  70149. (size_t) move_sz);
  70150. DUK_MEMCPY((void *) (p_base + dst_off),
  70151. (const void *) buf,
  70152. (size_t) len);
  70153. bw->p += len;
  70154. }
  70155. 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) {
  70156. DUK_ASSERT(thr != NULL);
  70157. DUK_ASSERT(bw != NULL);
  70158. DUK_ASSERT(dst_off <= DUK_BW_GET_SIZE(thr, bw));
  70159. DUK_ASSERT(buf != NULL);
  70160. DUK_UNREF(thr);
  70161. DUK_BW_ENSURE(thr, bw, len);
  70162. duk_bw_insert_raw_bytes(thr, bw, dst_off, buf, len);
  70163. }
  70164. 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) {
  70165. duk_uint8_t *p_base;
  70166. duk_size_t buf_sz, move_sz;
  70167. DUK_ASSERT(thr != NULL);
  70168. DUK_ASSERT(bw != NULL);
  70169. DUK_ASSERT(dst_off <= DUK_BW_GET_SIZE(thr, bw));
  70170. DUK_ASSERT(src_off <= DUK_BW_GET_SIZE(thr, bw));
  70171. DUK_ASSERT(len <= DUK_BW_GET_SIZE(thr, bw));
  70172. DUK_ASSERT(src_off + len <= DUK_BW_GET_SIZE(thr, bw));
  70173. DUK_UNREF(thr);
  70174. p_base = bw->p_base;
  70175. /* Don't support "straddled" source now. */
  70176. DUK_ASSERT(dst_off <= src_off || dst_off >= src_off + len);
  70177. if (dst_off <= src_off) {
  70178. /* Target is before source. Source offset is expressed as
  70179. * a "before change" offset. Account for the memmove.
  70180. */
  70181. src_off += len;
  70182. }
  70183. buf_sz = bw->p - p_base;
  70184. move_sz = buf_sz - dst_off;
  70185. DUK_ASSERT(p_base != NULL); /* buffer size is >= 1 */
  70186. DUK_MEMMOVE((void *) (p_base + dst_off + len),
  70187. (const void *) (p_base + dst_off),
  70188. (size_t) move_sz);
  70189. DUK_MEMCPY((void *) (p_base + dst_off),
  70190. (const void *) (p_base + src_off),
  70191. (size_t) len);
  70192. bw->p += len;
  70193. }
  70194. 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) {
  70195. DUK_ASSERT(thr != NULL);
  70196. DUK_ASSERT(bw != NULL);
  70197. DUK_ASSERT(dst_off <= DUK_BW_GET_SIZE(thr, bw));
  70198. DUK_ASSERT(src_off <= DUK_BW_GET_SIZE(thr, bw));
  70199. DUK_ASSERT(len <= DUK_BW_GET_SIZE(thr, bw));
  70200. DUK_ASSERT(src_off + len <= DUK_BW_GET_SIZE(thr, bw));
  70201. DUK_UNREF(thr);
  70202. /* Don't support "straddled" source now. */
  70203. DUK_ASSERT(dst_off <= src_off || dst_off >= src_off + len);
  70204. DUK_BW_ENSURE(thr, bw, len);
  70205. duk_bw_insert_raw_slice(thr, bw, dst_off, src_off, len);
  70206. }
  70207. 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) {
  70208. duk_uint8_t *p_base, *p_dst, *p_src;
  70209. duk_size_t buf_sz, move_sz;
  70210. DUK_ASSERT(thr != NULL);
  70211. DUK_ASSERT(bw != NULL);
  70212. DUK_ASSERT(off <= DUK_BW_GET_SIZE(thr, bw));
  70213. DUK_UNREF(thr);
  70214. p_base = bw->p_base;
  70215. buf_sz = bw->p - p_base;
  70216. move_sz = buf_sz - off;
  70217. p_dst = p_base + off + len;
  70218. p_src = p_base + off;
  70219. DUK_MEMMOVE((void *) p_dst, (const void *) p_src, (size_t) move_sz);
  70220. return p_src; /* point to start of 'reserved area' */
  70221. }
  70222. 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) {
  70223. DUK_ASSERT(thr != NULL);
  70224. DUK_ASSERT(bw != NULL);
  70225. DUK_ASSERT(off <= DUK_BW_GET_SIZE(thr, bw));
  70226. DUK_UNREF(thr);
  70227. DUK_BW_ENSURE(thr, bw, len);
  70228. return duk_bw_insert_raw_area(thr, bw, off, len);
  70229. }
  70230. DUK_INTERNAL void duk_bw_remove_raw_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t off, duk_size_t len) {
  70231. duk_size_t move_sz;
  70232. duk_uint8_t *p_base;
  70233. duk_uint8_t *p_src;
  70234. duk_uint8_t *p_dst;
  70235. DUK_ASSERT(thr != NULL);
  70236. DUK_ASSERT(bw != NULL);
  70237. DUK_ASSERT(off <= DUK_BW_GET_SIZE(thr, bw));
  70238. DUK_ASSERT(len <= DUK_BW_GET_SIZE(thr, bw));
  70239. DUK_ASSERT(off + len <= DUK_BW_GET_SIZE(thr, bw));
  70240. DUK_UNREF(thr);
  70241. p_base = bw->p_base;
  70242. p_dst = p_base + off;
  70243. p_src = p_dst + len;
  70244. move_sz = (duk_size_t) (bw->p - p_src);
  70245. DUK_MEMMOVE((void *) p_dst,
  70246. (const void *) p_src,
  70247. (size_t) move_sz);
  70248. bw->p -= len;
  70249. }
  70250. /*
  70251. * Macro support functions for reading/writing raw data.
  70252. *
  70253. * These are done using mempcy to ensure they're valid even for unaligned
  70254. * reads/writes on platforms where alignment counts. On x86 at least gcc
  70255. * is able to compile these into a bswap+mov. "Always inline" is used to
  70256. * ensure these macros compile to minimal code.
  70257. *
  70258. * Not really bufwriter related, but currently used together.
  70259. */
  70260. DUK_INTERNAL DUK_ALWAYS_INLINE duk_uint16_t duk_raw_read_u16_be(duk_uint8_t **p) {
  70261. union {
  70262. duk_uint8_t b[2];
  70263. duk_uint16_t x;
  70264. } u;
  70265. DUK_MEMCPY((void *) u.b, (const void *) (*p), (size_t) 2);
  70266. u.x = DUK_NTOH16(u.x);
  70267. *p += 2;
  70268. return u.x;
  70269. }
  70270. DUK_INTERNAL DUK_ALWAYS_INLINE duk_uint32_t duk_raw_read_u32_be(duk_uint8_t **p) {
  70271. union {
  70272. duk_uint8_t b[4];
  70273. duk_uint32_t x;
  70274. } u;
  70275. DUK_MEMCPY((void *) u.b, (const void *) (*p), (size_t) 4);
  70276. u.x = DUK_NTOH32(u.x);
  70277. *p += 4;
  70278. return u.x;
  70279. }
  70280. DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_raw_read_double_be(duk_uint8_t **p) {
  70281. duk_double_union du;
  70282. union {
  70283. duk_uint8_t b[4];
  70284. duk_uint32_t x;
  70285. } u;
  70286. DUK_MEMCPY((void *) u.b, (const void *) (*p), (size_t) 4);
  70287. u.x = DUK_NTOH32(u.x);
  70288. du.ui[DUK_DBL_IDX_UI0] = u.x;
  70289. DUK_MEMCPY((void *) u.b, (const void *) (*p + 4), (size_t) 4);
  70290. u.x = DUK_NTOH32(u.x);
  70291. du.ui[DUK_DBL_IDX_UI1] = u.x;
  70292. *p += 8;
  70293. return du.d;
  70294. }
  70295. DUK_INTERNAL DUK_ALWAYS_INLINE void duk_raw_write_u16_be(duk_uint8_t **p, duk_uint16_t val) {
  70296. union {
  70297. duk_uint8_t b[2];
  70298. duk_uint16_t x;
  70299. } u;
  70300. u.x = DUK_HTON16(val);
  70301. DUK_MEMCPY((void *) (*p), (const void *) u.b, (size_t) 2);
  70302. *p += 2;
  70303. }
  70304. DUK_INTERNAL DUK_ALWAYS_INLINE void duk_raw_write_u32_be(duk_uint8_t **p, duk_uint32_t val) {
  70305. union {
  70306. duk_uint8_t b[4];
  70307. duk_uint32_t x;
  70308. } u;
  70309. u.x = DUK_HTON32(val);
  70310. DUK_MEMCPY((void *) (*p), (const void *) u.b, (size_t) 4);
  70311. *p += 4;
  70312. }
  70313. DUK_INTERNAL DUK_ALWAYS_INLINE void duk_raw_write_double_be(duk_uint8_t **p, duk_double_t val) {
  70314. duk_double_union du;
  70315. union {
  70316. duk_uint8_t b[4];
  70317. duk_uint32_t x;
  70318. } u;
  70319. du.d = val;
  70320. u.x = du.ui[DUK_DBL_IDX_UI0];
  70321. u.x = DUK_HTON32(u.x);
  70322. DUK_MEMCPY((void *) (*p), (const void *) u.b, (size_t) 4);
  70323. u.x = du.ui[DUK_DBL_IDX_UI1];
  70324. u.x = DUK_HTON32(u.x);
  70325. DUK_MEMCPY((void *) (*p + 4), (const void *) u.b, (size_t) 4);
  70326. *p += 8;
  70327. }
  70328. #line 1 "duk_util_hashbytes.c"
  70329. /*
  70330. * Hash function duk_util_hashbytes().
  70331. *
  70332. * Currently, 32-bit MurmurHash2.
  70333. *
  70334. * Don't rely on specific hash values; hash function may be endianness
  70335. * dependent, for instance.
  70336. */
  70337. /* include removed: duk_internal.h */
  70338. /* 'magic' constants for Murmurhash2 */
  70339. #define DUK__MAGIC_M ((duk_uint32_t) 0x5bd1e995UL)
  70340. #define DUK__MAGIC_R 24
  70341. DUK_INTERNAL duk_uint32_t duk_util_hashbytes(const duk_uint8_t *data, duk_size_t len, duk_uint32_t seed) {
  70342. duk_uint32_t h = seed ^ ((duk_uint32_t) len);
  70343. while (len >= 4) {
  70344. /* Portability workaround is required for platforms without
  70345. * unaligned access. The replacement code emulates little
  70346. * endian access even on big endian architectures, which is
  70347. * OK as long as it is consistent for a build.
  70348. */
  70349. #ifdef DUK_USE_HASHBYTES_UNALIGNED_U32_ACCESS
  70350. duk_uint32_t k = *((const duk_uint32_t *) (const void *) data);
  70351. #else
  70352. duk_uint32_t k = ((duk_uint32_t) data[0]) |
  70353. (((duk_uint32_t) data[1]) << 8) |
  70354. (((duk_uint32_t) data[2]) << 16) |
  70355. (((duk_uint32_t) data[3]) << 24);
  70356. #endif
  70357. k *= DUK__MAGIC_M;
  70358. k ^= k >> DUK__MAGIC_R;
  70359. k *= DUK__MAGIC_M;
  70360. h *= DUK__MAGIC_M;
  70361. h ^= k;
  70362. data += 4;
  70363. len -= 4;
  70364. }
  70365. switch (len) {
  70366. case 3: h ^= data[2] << 16;
  70367. case 2: h ^= data[1] << 8;
  70368. case 1: h ^= data[0];
  70369. h *= DUK__MAGIC_M;
  70370. }
  70371. h ^= h >> 13;
  70372. h *= DUK__MAGIC_M;
  70373. h ^= h >> 15;
  70374. return h;
  70375. }
  70376. #line 1 "duk_util_tinyrandom.c"
  70377. /*
  70378. * A tiny random number generator.
  70379. *
  70380. * Currently used for Math.random().
  70381. *
  70382. * http://www.woodmann.com/forum/archive/index.php/t-3100.html
  70383. */
  70384. /* include removed: duk_internal.h */
  70385. #define DUK__UPDATE_RND(rnd) do { \
  70386. (rnd) += ((rnd) * (rnd)) | 0x05; \
  70387. (rnd) = ((rnd) & 0xffffffffU); /* if duk_uint32_t is exactly 32 bits, this is a NOP */ \
  70388. } while (0)
  70389. #define DUK__RND_BIT(rnd) ((rnd) >> 31) /* only use the highest bit */
  70390. DUK_INTERNAL duk_uint32_t duk_util_tinyrandom_get_bits(duk_hthread *thr, duk_small_int_t n) {
  70391. duk_small_int_t i;
  70392. duk_uint32_t res = 0;
  70393. duk_uint32_t rnd;
  70394. rnd = thr->heap->rnd_state;
  70395. for (i = 0; i < n; i++) {
  70396. DUK__UPDATE_RND(rnd);
  70397. res <<= 1;
  70398. res += DUK__RND_BIT(rnd);
  70399. }
  70400. thr->heap->rnd_state = rnd;
  70401. return res;
  70402. }
  70403. DUK_INTERNAL duk_double_t duk_util_tinyrandom_get_double(duk_hthread *thr) {
  70404. duk_double_t t;
  70405. duk_small_int_t n;
  70406. duk_uint32_t rnd;
  70407. /*
  70408. * XXX: could make this a lot faster if we create the double memory
  70409. * representation directly. Feasible easily (must be uniform random).
  70410. */
  70411. rnd = thr->heap->rnd_state;
  70412. n = 53; /* enough to cover the whole mantissa */
  70413. t = 0.0;
  70414. do {
  70415. DUK__UPDATE_RND(rnd);
  70416. t += DUK__RND_BIT(rnd);
  70417. t /= 2.0;
  70418. } while (--n);
  70419. thr->heap->rnd_state = rnd;
  70420. DUK_ASSERT(t >= (duk_double_t) 0.0);
  70421. DUK_ASSERT(t < (duk_double_t) 1.0);
  70422. return t;
  70423. }