duktape.c 2.8 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105561065610756108561095611056111561125611356114561155611656117561185611956120561215612256123561245612556126561275612856129561305613156132561335613456135561365613756138561395614056141561425614356144561455614656147561485614956150561515615256153561545615556156561575615856159561605616156162561635616456165561665616756168561695617056171561725617356174561755617656177561785617956180561815618256183561845618556186561875618856189561905619156192561935619456195561965619756198561995620056201562025620356204562055620656207562085620956210562115621256213562145621556216562175621856219562205622156222562235622456225562265622756228562295623056231562325623356234562355623656237562385623956240562415624256243562445624556246562475624856249562505625156252562535625456255562565625756258562595626056261562625626356264562655626656267562685626956270562715627256273562745627556276562775627856279562805628156282562835628456285562865628756288562895629056291562925629356294562955629656297562985629956300563015630256303563045630556306563075630856309563105631156312563135631456315563165631756318563195632056321563225632356324563255632656327563285632956330563315633256333563345633556336563375633856339563405634156342563435634456345563465634756348563495635056351563525635356354563555635656357563585635956360563615636256363563645636556366563675636856369563705637156372563735637456375563765637756378563795638056381563825638356384563855638656387563885638956390563915639256393563945639556396563975639856399564005640156402564035640456405564065640756408564095641056411564125641356414564155641656417564185641956420564215642256423564245642556426564275642856429564305643156432564335643456435564365643756438564395644056441564425644356444564455644656447564485644956450564515645256453564545645556456564575645856459564605646156462564635646456465564665646756468564695647056471564725647356474564755647656477564785647956480564815648256483564845648556486564875648856489564905649156492564935649456495564965649756498564995650056501565025650356504565055650656507565085650956510565115651256513565145651556516565175651856519565205652156522565235652456525565265652756528565295653056531565325653356534565355653656537565385653956540565415654256543565445654556546565475654856549565505655156552565535655456555565565655756558565595656056561565625656356564565655656656567565685656956570565715657256573565745657556576565775657856579565805658156582565835658456585565865658756588565895659056591565925659356594565955659656597565985659956600566015660256603566045660556606566075660856609566105661156612566135661456615566165661756618566195662056621566225662356624566255662656627566285662956630566315663256633566345663556636566375663856639566405664156642566435664456645566465664756648566495665056651566525665356654566555665656657566585665956660566615666256663566645666556666566675666856669566705667156672566735667456675566765667756678566795668056681566825668356684566855668656687566885668956690566915669256693566945669556696566975669856699567005670156702567035670456705567065670756708567095671056711567125671356714567155671656717567185671956720567215672256723567245672556726567275672856729567305673156732567335673456735567365673756738567395674056741567425674356744567455674656747567485674956750567515675256753567545675556756567575675856759567605676156762567635676456765567665676756768567695677056771567725677356774567755677656777567785677956780567815678256783567845678556786567875678856789567905679156792567935679456795567965679756798567995680056801568025680356804568055680656807568085680956810568115681256813568145681556816568175681856819568205682156822568235682456825568265682756828568295683056831568325683356834568355683656837568385683956840568415684256843568445684556846568475684856849568505685156852568535685456855568565685756858568595686056861568625686356864568655686656867568685686956870568715687256873568745687556876568775687856879568805688156882568835688456885568865688756888568895689056891568925689356894568955689656897568985689956900569015690256903569045690556906569075690856909569105691156912569135691456915569165691756918569195692056921569225692356924569255692656927569285692956930569315693256933569345693556936569375693856939569405694156942569435694456945569465694756948569495695056951569525695356954569555695656957569585695956960569615696256963569645696556966569675696856969569705697156972569735697456975569765697756978569795698056981569825698356984569855698656987569885698956990569915699256993569945699556996569975699856999570005700157002570035700457005570065700757008570095701057011570125701357014570155701657017570185701957020570215702257023570245702557026570275702857029570305703157032570335703457035570365703757038570395704057041570425704357044570455704657047570485704957050570515705257053570545705557056570575705857059570605706157062570635706457065570665706757068570695707057071570725707357074570755707657077570785707957080570815708257083570845708557086570875708857089570905709157092570935709457095570965709757098570995710057101571025710357104571055710657107571085710957110571115711257113571145711557116571175711857119571205712157122571235712457125571265712757128571295713057131571325713357134571355713657137571385713957140571415714257143571445714557146571475714857149571505715157152571535715457155571565715757158571595716057161571625716357164571655716657167571685716957170571715717257173571745717557176571775717857179571805718157182571835718457185571865718757188571895719057191571925719357194571955719657197571985719957200572015720257203572045720557206572075720857209572105721157212572135721457215572165721757218572195722057221572225722357224572255722657227572285722957230572315723257233572345723557236572375723857239572405724157242572435724457245572465724757248572495725057251572525725357254572555725657257572585725957260572615726257263572645726557266572675726857269572705727157272572735727457275572765727757278572795728057281572825728357284572855728657287572885728957290572915729257293572945729557296572975729857299573005730157302573035730457305573065730757308573095731057311573125731357314573155731657317573185731957320573215732257323573245732557326573275732857329573305733157332573335733457335573365733757338573395734057341573425734357344573455734657347573485734957350573515735257353573545735557356573575735857359573605736157362573635736457365573665736757368573695737057371573725737357374573755737657377573785737957380573815738257383573845738557386573875738857389573905739157392573935739457395573965739757398573995740057401574025740357404574055740657407574085740957410574115741257413574145741557416574175741857419574205742157422574235742457425574265742757428574295743057431574325743357434574355743657437574385743957440574415744257443574445744557446574475744857449574505745157452574535745457455574565745757458574595746057461574625746357464574655746657467574685746957470574715747257473574745747557476574775747857479574805748157482574835748457485574865748757488574895749057491574925749357494574955749657497574985749957500575015750257503575045750557506575075750857509575105751157512575135751457515575165751757518575195752057521575225752357524575255752657527575285752957530575315753257533575345753557536575375753857539575405754157542575435754457545575465754757548575495755057551575525755357554575555755657557575585755957560575615756257563575645756557566575675756857569575705757157572575735757457575575765757757578575795758057581575825758357584575855758657587575885758957590575915759257593575945759557596575975759857599576005760157602576035760457605576065760757608576095761057611576125761357614576155761657617576185761957620576215762257623576245762557626576275762857629576305763157632576335763457635576365763757638576395764057641576425764357644576455764657647576485764957650576515765257653576545765557656576575765857659576605766157662576635766457665576665766757668576695767057671576725767357674576755767657677576785767957680576815768257683576845768557686576875768857689576905769157692576935769457695576965769757698576995770057701577025770357704577055770657707577085770957710577115771257713577145771557716577175771857719577205772157722577235772457725577265772757728577295773057731577325773357734577355773657737577385773957740577415774257743577445774557746577475774857749577505775157752577535775457755577565775757758577595776057761577625776357764577655776657767577685776957770577715777257773577745777557776577775777857779577805778157782577835778457785577865778757788577895779057791577925779357794577955779657797577985779957800578015780257803578045780557806578075780857809578105781157812578135781457815578165781757818578195782057821578225782357824578255782657827578285782957830578315783257833578345783557836578375783857839578405784157842578435784457845578465784757848578495785057851578525785357854578555785657857578585785957860578615786257863578645786557866578675786857869578705787157872578735787457875578765787757878578795788057881578825788357884578855788657887578885788957890578915789257893578945789557896578975789857899579005790157902579035790457905579065790757908579095791057911579125791357914579155791657917579185791957920579215792257923579245792557926579275792857929579305793157932579335793457935579365793757938579395794057941579425794357944579455794657947579485794957950579515795257953579545795557956579575795857959579605796157962579635796457965579665796757968579695797057971579725797357974579755797657977579785797957980579815798257983579845798557986579875798857989579905799157992579935799457995579965799757998579995800058001580025800358004580055800658007580085800958010580115801258013580145801558016580175801858019580205802158022580235802458025580265802758028580295803058031580325803358034580355803658037580385803958040580415804258043580445804558046580475804858049580505805158052580535805458055580565805758058580595806058061580625806358064580655806658067580685806958070580715807258073580745807558076580775807858079580805808158082580835808458085580865808758088580895809058091580925809358094580955809658097580985809958100581015810258103581045810558106581075810858109581105811158112581135811458115581165811758118581195812058121581225812358124581255812658127581285812958130581315813258133581345813558136581375813858139581405814158142581435814458145581465814758148581495815058151581525815358154581555815658157581585815958160581615816258163581645816558166581675816858169581705817158172581735817458175581765817758178581795818058181581825818358184581855818658187581885818958190581915819258193581945819558196581975819858199582005820158202582035820458205582065820758208582095821058211582125821358214582155821658217582185821958220582215822258223582245822558226582275822858229582305823158232582335823458235582365823758238582395824058241582425824358244582455824658247582485824958250582515825258253582545825558256582575825858259582605826158262582635826458265582665826758268582695827058271582725827358274582755827658277582785827958280582815828258283582845828558286582875828858289582905829158292582935829458295582965829758298582995830058301583025830358304583055830658307583085830958310583115831258313583145831558316583175831858319583205832158322583235832458325583265832758328583295833058331583325833358334583355833658337583385833958340583415834258343583445834558346583475834858349583505835158352583535835458355583565835758358583595836058361583625836358364583655836658367583685836958370583715837258373583745837558376583775837858379583805838158382583835838458385583865838758388583895839058391583925839358394583955839658397583985839958400584015840258403584045840558406584075840858409584105841158412584135841458415584165841758418584195842058421584225842358424584255842658427584285842958430584315843258433584345843558436584375843858439584405844158442584435844458445584465844758448584495845058451584525845358454584555845658457584585845958460584615846258463584645846558466584675846858469584705847158472584735847458475584765847758478584795848058481584825848358484584855848658487584885848958490584915849258493584945849558496584975849858499585005850158502585035850458505585065850758508585095851058511585125851358514585155851658517585185851958520585215852258523585245852558526585275852858529585305853158532585335853458535585365853758538585395854058541585425854358544585455854658547585485854958550585515855258553585545855558556585575855858559585605856158562585635856458565585665856758568585695857058571585725857358574585755857658577585785857958580585815858258583585845858558586585875858858589585905859158592585935859458595585965859758598585995860058601586025860358604586055860658607586085860958610586115861258613586145861558616586175861858619586205862158622586235862458625586265862758628586295863058631586325863358634586355863658637586385863958640586415864258643586445864558646586475864858649586505865158652586535865458655586565865758658586595866058661586625866358664586655866658667586685866958670586715867258673586745867558676586775867858679586805868158682586835868458685586865868758688586895869058691586925869358694586955869658697586985869958700587015870258703587045870558706587075870858709587105871158712587135871458715587165871758718587195872058721587225872358724587255872658727587285872958730587315873258733587345873558736587375873858739587405874158742587435874458745587465874758748587495875058751587525875358754587555875658757587585875958760587615876258763587645876558766587675876858769587705877158772587735877458775587765877758778587795878058781587825878358784587855878658787587885878958790587915879258793587945879558796587975879858799588005880158802588035880458805588065880758808588095881058811588125881358814588155881658817588185881958820588215882258823588245882558826588275882858829588305883158832588335883458835588365883758838588395884058841588425884358844588455884658847588485884958850588515885258853588545885558856588575885858859588605886158862588635886458865588665886758868588695887058871588725887358874588755887658877588785887958880588815888258883588845888558886588875888858889588905889158892588935889458895588965889758898588995890058901589025890358904589055890658907589085890958910589115891258913589145891558916589175891858919589205892158922589235892458925589265892758928589295893058931589325893358934589355893658937589385893958940589415894258943589445894558946589475894858949589505895158952589535895458955589565895758958589595896058961589625896358964589655896658967589685896958970589715897258973589745897558976589775897858979589805898158982589835898458985589865898758988589895899058991589925899358994589955899658997589985899959000590015900259003590045900559006590075900859009590105901159012590135901459015590165901759018590195902059021590225902359024590255902659027590285902959030590315903259033590345903559036590375903859039590405904159042590435904459045590465904759048590495905059051590525905359054590555905659057590585905959060590615906259063590645906559066590675906859069590705907159072590735907459075590765907759078590795908059081590825908359084590855908659087590885908959090590915909259093590945909559096590975909859099591005910159102591035910459105591065910759108591095911059111591125911359114591155911659117591185911959120591215912259123591245912559126591275912859129591305913159132591335913459135591365913759138591395914059141591425914359144591455914659147591485914959150591515915259153591545915559156591575915859159591605916159162591635916459165591665916759168591695917059171591725917359174591755917659177591785917959180591815918259183591845918559186591875918859189591905919159192591935919459195591965919759198591995920059201592025920359204592055920659207592085920959210592115921259213592145921559216592175921859219592205922159222592235922459225592265922759228592295923059231592325923359234592355923659237592385923959240592415924259243592445924559246592475924859249592505925159252592535925459255592565925759258592595926059261592625926359264592655926659267592685926959270592715927259273592745927559276592775927859279592805928159282592835928459285592865928759288592895929059291592925929359294592955929659297592985929959300593015930259303593045930559306593075930859309593105931159312593135931459315593165931759318593195932059321593225932359324593255932659327593285932959330593315933259333593345933559336593375933859339593405934159342593435934459345593465934759348593495935059351593525935359354593555935659357593585935959360593615936259363593645936559366593675936859369593705937159372593735937459375593765937759378593795938059381593825938359384593855938659387593885938959390593915939259393593945939559396593975939859399594005940159402594035940459405594065940759408594095941059411594125941359414594155941659417594185941959420594215942259423594245942559426594275942859429594305943159432594335943459435594365943759438594395944059441594425944359444594455944659447594485944959450594515945259453594545945559456594575945859459594605946159462594635946459465594665946759468594695947059471594725947359474594755947659477594785947959480594815948259483594845948559486594875948859489594905949159492594935949459495594965949759498594995950059501595025950359504595055950659507595085950959510595115951259513595145951559516595175951859519595205952159522595235952459525595265952759528595295953059531595325953359534595355953659537595385953959540595415954259543595445954559546595475954859549595505955159552595535955459555595565955759558595595956059561595625956359564595655956659567595685956959570595715957259573595745957559576595775957859579595805958159582595835958459585595865958759588595895959059591595925959359594595955959659597595985959959600596015960259603596045960559606596075960859609596105961159612596135961459615596165961759618596195962059621596225962359624596255962659627596285962959630596315963259633596345963559636596375963859639596405964159642596435964459645596465964759648596495965059651596525965359654596555965659657596585965959660596615966259663596645966559666596675966859669596705967159672596735967459675596765967759678596795968059681596825968359684596855968659687596885968959690596915969259693596945969559696596975969859699597005970159702597035970459705597065970759708597095971059711597125971359714597155971659717597185971959720597215972259723597245972559726597275972859729597305973159732597335973459735597365973759738597395974059741597425974359744597455974659747597485974959750597515975259753597545975559756597575975859759597605976159762597635976459765597665976759768597695977059771597725977359774597755977659777597785977959780597815978259783597845978559786597875978859789597905979159792597935979459795597965979759798597995980059801598025980359804598055980659807598085980959810598115981259813598145981559816598175981859819598205982159822598235982459825598265982759828598295983059831598325983359834598355983659837598385983959840598415984259843598445984559846598475984859849598505985159852598535985459855598565985759858598595986059861598625986359864598655986659867598685986959870598715987259873598745987559876598775987859879598805988159882598835988459885598865988759888598895989059891598925989359894598955989659897598985989959900599015990259903599045990559906599075990859909599105991159912599135991459915599165991759918599195992059921599225992359924599255992659927599285992959930599315993259933599345993559936599375993859939599405994159942599435994459945599465994759948599495995059951599525995359954599555995659957599585995959960599615996259963599645996559966599675996859969599705997159972599735997459975599765997759978599795998059981599825998359984599855998659987599885998959990599915999259993599945999559996599975999859999600006000160002600036000460005600066000760008600096001060011600126001360014600156001660017600186001960020600216002260023600246002560026600276002860029600306003160032600336003460035600366003760038600396004060041600426004360044600456004660047600486004960050600516005260053600546005560056600576005860059600606006160062600636006460065600666006760068600696007060071600726007360074600756007660077600786007960080600816008260083600846008560086600876008860089600906009160092600936009460095600966009760098600996010060101601026010360104601056010660107601086010960110601116011260113601146011560116601176011860119601206012160122601236012460125601266012760128601296013060131601326013360134601356013660137601386013960140601416014260143601446014560146601476014860149601506015160152601536015460155601566015760158601596016060161601626016360164601656016660167601686016960170601716017260173601746017560176601776017860179601806018160182601836018460185601866018760188601896019060191601926019360194601956019660197601986019960200602016020260203602046020560206602076020860209602106021160212602136021460215602166021760218602196022060221602226022360224602256022660227602286022960230602316023260233602346023560236602376023860239602406024160242602436024460245602466024760248602496025060251602526025360254602556025660257602586025960260602616026260263602646026560266602676026860269602706027160272602736027460275602766027760278602796028060281602826028360284602856028660287602886028960290602916029260293602946029560296602976029860299603006030160302603036030460305603066030760308603096031060311603126031360314603156031660317603186031960320603216032260323603246032560326603276032860329603306033160332603336033460335603366033760338603396034060341603426034360344603456034660347603486034960350603516035260353603546035560356603576035860359603606036160362603636036460365603666036760368603696037060371603726037360374603756037660377603786037960380603816038260383603846038560386603876038860389603906039160392603936039460395603966039760398603996040060401604026040360404604056040660407604086040960410604116041260413604146041560416604176041860419604206042160422604236042460425604266042760428604296043060431604326043360434604356043660437604386043960440604416044260443604446044560446604476044860449604506045160452604536045460455604566045760458604596046060461604626046360464604656046660467604686046960470604716047260473604746047560476604776047860479604806048160482604836048460485604866048760488604896049060491604926049360494604956049660497604986049960500605016050260503605046050560506605076050860509605106051160512605136051460515605166051760518605196052060521605226052360524605256052660527605286052960530605316053260533605346053560536605376053860539605406054160542605436054460545605466054760548605496055060551605526055360554605556055660557605586055960560605616056260563605646056560566605676056860569605706057160572605736057460575605766057760578605796058060581605826058360584605856058660587605886058960590605916059260593605946059560596605976059860599606006060160602606036060460605606066060760608606096061060611606126061360614606156061660617606186061960620606216062260623606246062560626606276062860629606306063160632606336063460635606366063760638606396064060641606426064360644606456064660647606486064960650606516065260653606546065560656606576065860659606606066160662606636066460665606666066760668606696067060671606726067360674606756067660677606786067960680606816068260683606846068560686606876068860689606906069160692606936069460695606966069760698606996070060701607026070360704607056070660707607086070960710607116071260713607146071560716607176071860719607206072160722607236072460725607266072760728607296073060731607326073360734607356073660737607386073960740607416074260743607446074560746607476074860749607506075160752607536075460755607566075760758607596076060761607626076360764607656076660767607686076960770607716077260773607746077560776607776077860779607806078160782607836078460785607866078760788607896079060791607926079360794607956079660797607986079960800608016080260803608046080560806608076080860809608106081160812608136081460815608166081760818608196082060821608226082360824608256082660827608286082960830608316083260833608346083560836608376083860839608406084160842608436084460845608466084760848608496085060851608526085360854608556085660857608586085960860608616086260863608646086560866608676086860869608706087160872608736087460875608766087760878608796088060881608826088360884608856088660887608886088960890608916089260893608946089560896608976089860899609006090160902609036090460905609066090760908609096091060911609126091360914609156091660917609186091960920609216092260923609246092560926609276092860929609306093160932609336093460935609366093760938609396094060941609426094360944609456094660947609486094960950609516095260953609546095560956609576095860959609606096160962609636096460965609666096760968609696097060971609726097360974609756097660977609786097960980609816098260983609846098560986609876098860989609906099160992609936099460995609966099760998609996100061001610026100361004610056100661007610086100961010610116101261013610146101561016610176101861019610206102161022610236102461025610266102761028610296103061031610326103361034610356103661037610386103961040610416104261043610446104561046610476104861049610506105161052610536105461055610566105761058610596106061061610626106361064610656106661067610686106961070610716107261073610746107561076610776107861079610806108161082610836108461085610866108761088610896109061091610926109361094610956109661097610986109961100611016110261103611046110561106611076110861109611106111161112611136111461115611166111761118611196112061121611226112361124611256112661127611286112961130611316113261133611346113561136611376113861139611406114161142611436114461145611466114761148611496115061151611526115361154611556115661157611586115961160611616116261163611646116561166611676116861169611706117161172611736117461175611766117761178611796118061181611826118361184611856118661187611886118961190611916119261193611946119561196611976119861199612006120161202612036120461205612066120761208612096121061211612126121361214612156121661217612186121961220612216122261223612246122561226612276122861229612306123161232612336123461235612366123761238612396124061241612426124361244612456124661247612486124961250612516125261253612546125561256612576125861259612606126161262612636126461265612666126761268612696127061271612726127361274612756127661277612786127961280612816128261283612846128561286612876128861289612906129161292612936129461295612966129761298612996130061301613026130361304613056130661307613086130961310613116131261313613146131561316613176131861319613206132161322613236132461325613266132761328613296133061331613326133361334613356133661337613386133961340613416134261343613446134561346613476134861349613506135161352613536135461355613566135761358613596136061361613626136361364613656136661367613686136961370613716137261373613746137561376613776137861379613806138161382613836138461385613866138761388613896139061391613926139361394613956139661397613986139961400614016140261403614046140561406614076140861409614106141161412614136141461415614166141761418614196142061421614226142361424614256142661427614286142961430614316143261433614346143561436614376143861439614406144161442614436144461445614466144761448614496145061451614526145361454614556145661457614586145961460614616146261463614646146561466614676146861469614706147161472614736147461475614766147761478614796148061481614826148361484614856148661487614886148961490614916149261493614946149561496614976149861499615006150161502615036150461505615066150761508615096151061511615126151361514615156151661517615186151961520615216152261523615246152561526615276152861529615306153161532615336153461535615366153761538615396154061541615426154361544615456154661547615486154961550615516155261553615546155561556615576155861559615606156161562615636156461565615666156761568615696157061571615726157361574615756157661577615786157961580615816158261583615846158561586615876158861589615906159161592615936159461595615966159761598615996160061601616026160361604616056160661607616086160961610616116161261613616146161561616616176161861619616206162161622616236162461625616266162761628616296163061631616326163361634616356163661637616386163961640616416164261643616446164561646616476164861649616506165161652616536165461655616566165761658616596166061661616626166361664616656166661667616686166961670616716167261673616746167561676616776167861679616806168161682616836168461685616866168761688616896169061691616926169361694616956169661697616986169961700617016170261703617046170561706617076170861709617106171161712617136171461715617166171761718617196172061721617226172361724617256172661727617286172961730617316173261733617346173561736617376173861739617406174161742617436174461745617466174761748617496175061751617526175361754617556175661757617586175961760617616176261763617646176561766617676176861769617706177161772617736177461775617766177761778617796178061781617826178361784617856178661787617886178961790617916179261793617946179561796617976179861799618006180161802618036180461805618066180761808618096181061811618126181361814618156181661817618186181961820618216182261823618246182561826618276182861829618306183161832618336183461835618366183761838618396184061841618426184361844618456184661847618486184961850618516185261853618546185561856618576185861859618606186161862618636186461865618666186761868618696187061871618726187361874618756187661877618786187961880618816188261883618846188561886618876188861889618906189161892618936189461895618966189761898618996190061901619026190361904619056190661907619086190961910619116191261913619146191561916619176191861919619206192161922619236192461925619266192761928619296193061931619326193361934619356193661937619386193961940619416194261943619446194561946619476194861949619506195161952619536195461955619566195761958619596196061961619626196361964619656196661967619686196961970619716197261973619746197561976619776197861979619806198161982619836198461985619866198761988619896199061991619926199361994619956199661997619986199962000620016200262003620046200562006620076200862009620106201162012620136201462015620166201762018620196202062021620226202362024620256202662027620286202962030620316203262033620346203562036620376203862039620406204162042620436204462045620466204762048620496205062051620526205362054620556205662057620586205962060620616206262063620646206562066620676206862069620706207162072620736207462075620766207762078620796208062081620826208362084620856208662087620886208962090620916209262093620946209562096620976209862099621006210162102621036210462105621066210762108621096211062111621126211362114621156211662117621186211962120621216212262123621246212562126621276212862129621306213162132621336213462135621366213762138621396214062141621426214362144621456214662147621486214962150621516215262153621546215562156621576215862159621606216162162621636216462165621666216762168621696217062171621726217362174621756217662177621786217962180621816218262183621846218562186621876218862189621906219162192621936219462195621966219762198621996220062201622026220362204622056220662207622086220962210622116221262213622146221562216622176221862219622206222162222622236222462225622266222762228622296223062231622326223362234622356223662237622386223962240622416224262243622446224562246622476224862249622506225162252622536225462255622566225762258622596226062261622626226362264622656226662267622686226962270622716227262273622746227562276622776227862279622806228162282622836228462285622866228762288622896229062291622926229362294622956229662297622986229962300623016230262303623046230562306623076230862309623106231162312623136231462315623166231762318623196232062321623226232362324623256232662327623286232962330623316233262333623346233562336623376233862339623406234162342623436234462345623466234762348623496235062351623526235362354623556235662357623586235962360623616236262363623646236562366623676236862369623706237162372623736237462375623766237762378623796238062381623826238362384623856238662387623886238962390623916239262393623946239562396623976239862399624006240162402624036240462405624066240762408624096241062411624126241362414624156241662417624186241962420624216242262423624246242562426624276242862429624306243162432624336243462435624366243762438624396244062441624426244362444624456244662447624486244962450624516245262453624546245562456624576245862459624606246162462624636246462465624666246762468624696247062471624726247362474624756247662477624786247962480624816248262483624846248562486624876248862489624906249162492624936249462495624966249762498624996250062501625026250362504625056250662507625086250962510625116251262513625146251562516625176251862519625206252162522625236252462525625266252762528625296253062531625326253362534625356253662537625386253962540625416254262543625446254562546625476254862549625506255162552625536255462555625566255762558625596256062561625626256362564625656256662567625686256962570625716257262573625746257562576625776257862579625806258162582625836258462585625866258762588625896259062591625926259362594625956259662597625986259962600626016260262603626046260562606626076260862609626106261162612626136261462615626166261762618626196262062621626226262362624626256262662627626286262962630626316263262633626346263562636626376263862639626406264162642626436264462645626466264762648626496265062651626526265362654626556265662657626586265962660626616266262663626646266562666626676266862669626706267162672626736267462675626766267762678626796268062681626826268362684626856268662687626886268962690626916269262693626946269562696626976269862699627006270162702627036270462705627066270762708627096271062711627126271362714627156271662717627186271962720627216272262723627246272562726627276272862729627306273162732627336273462735627366273762738627396274062741627426274362744627456274662747627486274962750627516275262753627546275562756627576275862759627606276162762627636276462765627666276762768627696277062771627726277362774627756277662777627786277962780627816278262783627846278562786627876278862789627906279162792627936279462795627966279762798627996280062801628026280362804628056280662807628086280962810628116281262813628146281562816628176281862819628206282162822628236282462825628266282762828628296283062831628326283362834628356283662837628386283962840628416284262843628446284562846628476284862849628506285162852628536285462855628566285762858628596286062861628626286362864628656286662867628686286962870628716287262873628746287562876628776287862879628806288162882628836288462885628866288762888628896289062891628926289362894628956289662897628986289962900629016290262903629046290562906629076290862909629106291162912629136291462915629166291762918629196292062921629226292362924629256292662927629286292962930629316293262933629346293562936629376293862939629406294162942629436294462945629466294762948629496295062951629526295362954629556295662957629586295962960629616296262963629646296562966629676296862969629706297162972629736297462975629766297762978629796298062981629826298362984629856298662987629886298962990629916299262993629946299562996629976299862999630006300163002630036300463005630066300763008630096301063011630126301363014630156301663017630186301963020630216302263023630246302563026630276302863029630306303163032630336303463035630366303763038630396304063041630426304363044630456304663047630486304963050630516305263053630546305563056630576305863059630606306163062630636306463065630666306763068630696307063071630726307363074630756307663077630786307963080630816308263083630846308563086630876308863089630906309163092630936309463095630966309763098630996310063101631026310363104631056310663107631086310963110631116311263113631146311563116631176311863119631206312163122631236312463125631266312763128631296313063131631326313363134631356313663137631386313963140631416314263143631446314563146631476314863149631506315163152631536315463155631566315763158631596316063161631626316363164631656316663167631686316963170631716317263173631746317563176631776317863179631806318163182631836318463185631866318763188631896319063191631926319363194631956319663197631986319963200632016320263203632046320563206632076320863209632106321163212632136321463215632166321763218632196322063221632226322363224632256322663227632286322963230632316323263233632346323563236632376323863239632406324163242632436324463245632466324763248632496325063251632526325363254632556325663257632586325963260632616326263263632646326563266632676326863269632706327163272632736327463275632766327763278632796328063281632826328363284632856328663287632886328963290632916329263293632946329563296632976329863299633006330163302633036330463305633066330763308633096331063311633126331363314633156331663317633186331963320633216332263323633246332563326633276332863329633306333163332633336333463335633366333763338633396334063341633426334363344633456334663347633486334963350633516335263353633546335563356633576335863359633606336163362633636336463365633666336763368633696337063371633726337363374633756337663377633786337963380633816338263383633846338563386633876338863389633906339163392633936339463395633966339763398633996340063401634026340363404634056340663407634086340963410634116341263413634146341563416634176341863419634206342163422634236342463425634266342763428634296343063431634326343363434634356343663437634386343963440634416344263443634446344563446634476344863449634506345163452634536345463455634566345763458634596346063461634626346363464634656346663467634686346963470634716347263473634746347563476634776347863479634806348163482634836348463485634866348763488634896349063491634926349363494634956349663497634986349963500635016350263503635046350563506635076350863509635106351163512635136351463515635166351763518635196352063521635226352363524635256352663527635286352963530635316353263533635346353563536635376353863539635406354163542635436354463545635466354763548635496355063551635526355363554635556355663557635586355963560635616356263563635646356563566635676356863569635706357163572635736357463575635766357763578635796358063581635826358363584635856358663587635886358963590635916359263593635946359563596635976359863599636006360163602636036360463605636066360763608636096361063611636126361363614636156361663617636186361963620636216362263623636246362563626636276362863629636306363163632636336363463635636366363763638636396364063641636426364363644636456364663647636486364963650636516365263653636546365563656636576365863659636606366163662636636366463665636666366763668636696367063671636726367363674636756367663677636786367963680636816368263683636846368563686636876368863689636906369163692636936369463695636966369763698636996370063701637026370363704637056370663707637086370963710637116371263713637146371563716637176371863719637206372163722637236372463725637266372763728637296373063731637326373363734637356373663737637386373963740637416374263743637446374563746637476374863749637506375163752637536375463755637566375763758637596376063761637626376363764637656376663767637686376963770637716377263773637746377563776637776377863779637806378163782637836378463785637866378763788637896379063791637926379363794637956379663797637986379963800638016380263803638046380563806638076380863809638106381163812638136381463815638166381763818638196382063821638226382363824638256382663827638286382963830638316383263833638346383563836638376383863839638406384163842638436384463845638466384763848638496385063851638526385363854638556385663857638586385963860638616386263863638646386563866638676386863869638706387163872638736387463875638766387763878638796388063881638826388363884638856388663887638886388963890638916389263893638946389563896638976389863899639006390163902639036390463905639066390763908639096391063911639126391363914639156391663917639186391963920639216392263923639246392563926639276392863929639306393163932639336393463935639366393763938639396394063941639426394363944639456394663947639486394963950639516395263953639546395563956639576395863959639606396163962639636396463965639666396763968639696397063971639726397363974639756397663977639786397963980639816398263983639846398563986639876398863989639906399163992639936399463995639966399763998639996400064001640026400364004640056400664007640086400964010640116401264013640146401564016640176401864019640206402164022640236402464025640266402764028640296403064031640326403364034640356403664037640386403964040640416404264043640446404564046640476404864049640506405164052640536405464055640566405764058640596406064061640626406364064640656406664067640686406964070640716407264073640746407564076640776407864079640806408164082640836408464085640866408764088640896409064091640926409364094640956409664097640986409964100641016410264103641046410564106641076410864109641106411164112641136411464115641166411764118641196412064121641226412364124641256412664127641286412964130641316413264133641346413564136641376413864139641406414164142641436414464145641466414764148641496415064151641526415364154641556415664157641586415964160641616416264163641646416564166641676416864169641706417164172641736417464175641766417764178641796418064181641826418364184641856418664187641886418964190641916419264193641946419564196641976419864199642006420164202642036420464205642066420764208642096421064211642126421364214642156421664217642186421964220642216422264223642246422564226642276422864229642306423164232642336423464235642366423764238642396424064241642426424364244642456424664247642486424964250642516425264253642546425564256642576425864259642606426164262642636426464265642666426764268642696427064271642726427364274642756427664277642786427964280642816428264283642846428564286642876428864289642906429164292642936429464295642966429764298642996430064301643026430364304643056430664307643086430964310643116431264313643146431564316643176431864319643206432164322643236432464325643266432764328643296433064331643326433364334643356433664337643386433964340643416434264343643446434564346643476434864349643506435164352643536435464355643566435764358643596436064361643626436364364643656436664367643686436964370643716437264373643746437564376643776437864379643806438164382643836438464385643866438764388643896439064391643926439364394643956439664397643986439964400644016440264403644046440564406644076440864409644106441164412644136441464415644166441764418644196442064421644226442364424644256442664427644286442964430644316443264433644346443564436644376443864439644406444164442644436444464445644466444764448644496445064451644526445364454644556445664457644586445964460644616446264463644646446564466644676446864469644706447164472644736447464475644766447764478644796448064481644826448364484644856448664487644886448964490644916449264493644946449564496644976449864499645006450164502645036450464505645066450764508645096451064511645126451364514645156451664517645186451964520645216452264523645246452564526645276452864529645306453164532645336453464535645366453764538645396454064541645426454364544645456454664547645486454964550645516455264553645546455564556645576455864559645606456164562645636456464565645666456764568645696457064571645726457364574645756457664577645786457964580645816458264583645846458564586645876458864589645906459164592645936459464595645966459764598645996460064601646026460364604646056460664607646086460964610646116461264613646146461564616646176461864619646206462164622646236462464625646266462764628646296463064631646326463364634646356463664637646386463964640646416464264643646446464564646646476464864649646506465164652646536465464655646566465764658646596466064661646626466364664646656466664667646686466964670646716467264673646746467564676646776467864679646806468164682646836468464685646866468764688646896469064691646926469364694646956469664697646986469964700647016470264703647046470564706647076470864709647106471164712647136471464715647166471764718647196472064721647226472364724647256472664727647286472964730647316473264733647346473564736647376473864739647406474164742647436474464745647466474764748647496475064751647526475364754647556475664757647586475964760647616476264763647646476564766647676476864769647706477164772647736477464775647766477764778647796478064781647826478364784647856478664787647886478964790647916479264793647946479564796647976479864799648006480164802648036480464805648066480764808648096481064811648126481364814648156481664817648186481964820648216482264823648246482564826648276482864829648306483164832648336483464835648366483764838648396484064841648426484364844648456484664847648486484964850648516485264853648546485564856648576485864859648606486164862648636486464865648666486764868648696487064871648726487364874648756487664877648786487964880648816488264883648846488564886648876488864889648906489164892648936489464895648966489764898648996490064901649026490364904649056490664907649086490964910649116491264913649146491564916649176491864919649206492164922649236492464925649266492764928649296493064931649326493364934649356493664937649386493964940649416494264943649446494564946649476494864949649506495164952649536495464955649566495764958649596496064961649626496364964649656496664967649686496964970649716497264973649746497564976649776497864979649806498164982649836498464985649866498764988649896499064991649926499364994649956499664997649986499965000650016500265003650046500565006650076500865009650106501165012650136501465015650166501765018650196502065021650226502365024650256502665027650286502965030650316503265033650346503565036650376503865039650406504165042650436504465045650466504765048650496505065051650526505365054650556505665057650586505965060650616506265063650646506565066650676506865069650706507165072650736507465075650766507765078650796508065081650826508365084650856508665087650886508965090650916509265093650946509565096650976509865099651006510165102651036510465105651066510765108651096511065111651126511365114651156511665117651186511965120651216512265123651246512565126651276512865129651306513165132651336513465135651366513765138651396514065141651426514365144651456514665147651486514965150651516515265153651546515565156651576515865159651606516165162651636516465165651666516765168651696517065171651726517365174651756517665177651786517965180651816518265183651846518565186651876518865189651906519165192651936519465195651966519765198651996520065201652026520365204652056520665207652086520965210652116521265213652146521565216652176521865219652206522165222652236522465225652266522765228652296523065231652326523365234652356523665237652386523965240652416524265243652446524565246652476524865249652506525165252652536525465255652566525765258652596526065261652626526365264652656526665267652686526965270652716527265273652746527565276652776527865279652806528165282652836528465285652866528765288652896529065291652926529365294652956529665297652986529965300653016530265303653046530565306653076530865309653106531165312653136531465315653166531765318653196532065321653226532365324653256532665327653286532965330653316533265333653346533565336653376533865339653406534165342653436534465345653466534765348653496535065351653526535365354653556535665357653586535965360653616536265363653646536565366653676536865369653706537165372653736537465375653766537765378653796538065381653826538365384653856538665387653886538965390653916539265393653946539565396653976539865399654006540165402654036540465405654066540765408654096541065411654126541365414654156541665417654186541965420654216542265423654246542565426654276542865429654306543165432654336543465435654366543765438654396544065441654426544365444654456544665447654486544965450654516545265453654546545565456654576545865459654606546165462654636546465465654666546765468654696547065471654726547365474654756547665477654786547965480654816548265483654846548565486654876548865489654906549165492654936549465495654966549765498654996550065501655026550365504655056550665507655086550965510655116551265513655146551565516655176551865519655206552165522655236552465525655266552765528655296553065531655326553365534655356553665537655386553965540655416554265543655446554565546655476554865549655506555165552655536555465555655566555765558655596556065561655626556365564655656556665567655686556965570655716557265573655746557565576655776557865579655806558165582655836558465585655866558765588655896559065591655926559365594655956559665597655986559965600656016560265603656046560565606656076560865609656106561165612656136561465615656166561765618656196562065621656226562365624656256562665627656286562965630656316563265633656346563565636656376563865639656406564165642656436564465645656466564765648656496565065651656526565365654656556565665657656586565965660656616566265663656646566565666656676566865669656706567165672656736567465675656766567765678656796568065681656826568365684656856568665687656886568965690656916569265693656946569565696656976569865699657006570165702657036570465705657066570765708657096571065711657126571365714657156571665717657186571965720657216572265723657246572565726657276572865729657306573165732657336573465735657366573765738657396574065741657426574365744657456574665747657486574965750657516575265753657546575565756657576575865759657606576165762657636576465765657666576765768657696577065771657726577365774657756577665777657786577965780657816578265783657846578565786657876578865789657906579165792657936579465795657966579765798657996580065801658026580365804658056580665807658086580965810658116581265813658146581565816658176581865819658206582165822658236582465825658266582765828658296583065831658326583365834658356583665837658386583965840658416584265843658446584565846658476584865849658506585165852658536585465855658566585765858658596586065861658626586365864658656586665867658686586965870658716587265873658746587565876658776587865879658806588165882658836588465885658866588765888658896589065891658926589365894658956589665897658986589965900659016590265903659046590565906659076590865909659106591165912659136591465915659166591765918659196592065921659226592365924659256592665927659286592965930659316593265933659346593565936659376593865939659406594165942659436594465945659466594765948659496595065951659526595365954659556595665957659586595965960659616596265963659646596565966659676596865969659706597165972659736597465975659766597765978659796598065981659826598365984659856598665987659886598965990659916599265993659946599565996659976599865999660006600166002660036600466005660066600766008660096601066011660126601366014660156601666017660186601966020660216602266023660246602566026660276602866029660306603166032660336603466035660366603766038660396604066041660426604366044660456604666047660486604966050660516605266053660546605566056660576605866059660606606166062660636606466065660666606766068660696607066071660726607366074660756607666077660786607966080660816608266083660846608566086660876608866089660906609166092660936609466095660966609766098660996610066101661026610366104661056610666107661086610966110661116611266113661146611566116661176611866119661206612166122661236612466125661266612766128661296613066131661326613366134661356613666137661386613966140661416614266143661446614566146661476614866149661506615166152661536615466155661566615766158661596616066161661626616366164661656616666167661686616966170661716617266173661746617566176661776617866179661806618166182661836618466185661866618766188661896619066191661926619366194661956619666197661986619966200662016620266203662046620566206662076620866209662106621166212662136621466215662166621766218662196622066221662226622366224662256622666227662286622966230662316623266233662346623566236662376623866239662406624166242662436624466245662466624766248662496625066251662526625366254662556625666257662586625966260662616626266263662646626566266662676626866269662706627166272662736627466275662766627766278662796628066281662826628366284662856628666287662886628966290662916629266293662946629566296662976629866299663006630166302663036630466305663066630766308663096631066311663126631366314663156631666317663186631966320663216632266323663246632566326663276632866329663306633166332663336633466335663366633766338663396634066341663426634366344663456634666347663486634966350663516635266353663546635566356663576635866359663606636166362663636636466365663666636766368663696637066371663726637366374663756637666377663786637966380663816638266383663846638566386663876638866389663906639166392663936639466395663966639766398663996640066401664026640366404664056640666407664086640966410664116641266413664146641566416664176641866419664206642166422664236642466425664266642766428664296643066431664326643366434664356643666437664386643966440664416644266443664446644566446664476644866449664506645166452664536645466455664566645766458664596646066461664626646366464664656646666467664686646966470664716647266473664746647566476664776647866479664806648166482664836648466485664866648766488664896649066491664926649366494664956649666497664986649966500665016650266503665046650566506665076650866509665106651166512665136651466515665166651766518665196652066521665226652366524665256652666527665286652966530665316653266533665346653566536665376653866539665406654166542665436654466545665466654766548665496655066551665526655366554665556655666557665586655966560665616656266563665646656566566665676656866569665706657166572665736657466575665766657766578665796658066581665826658366584665856658666587665886658966590665916659266593665946659566596665976659866599666006660166602666036660466605666066660766608666096661066611666126661366614666156661666617666186661966620666216662266623666246662566626666276662866629666306663166632666336663466635666366663766638666396664066641666426664366644666456664666647666486664966650666516665266653666546665566656666576665866659666606666166662666636666466665666666666766668666696667066671666726667366674666756667666677666786667966680666816668266683666846668566686666876668866689666906669166692666936669466695666966669766698666996670066701667026670366704667056670666707667086670966710667116671266713667146671566716667176671866719667206672166722667236672466725667266672766728667296673066731667326673366734667356673666737667386673966740667416674266743667446674566746667476674866749667506675166752667536675466755667566675766758667596676066761667626676366764667656676666767667686676966770667716677266773667746677566776667776677866779667806678166782667836678466785667866678766788667896679066791667926679366794667956679666797667986679966800668016680266803668046680566806668076680866809668106681166812668136681466815668166681766818668196682066821668226682366824668256682666827668286682966830668316683266833668346683566836668376683866839668406684166842668436684466845668466684766848668496685066851668526685366854668556685666857668586685966860668616686266863668646686566866668676686866869668706687166872668736687466875668766687766878668796688066881668826688366884668856688666887668886688966890668916689266893668946689566896668976689866899669006690166902669036690466905669066690766908669096691066911669126691366914669156691666917669186691966920669216692266923669246692566926669276692866929669306693166932669336693466935669366693766938669396694066941669426694366944669456694666947669486694966950669516695266953669546695566956669576695866959669606696166962669636696466965669666696766968669696697066971669726697366974669756697666977669786697966980669816698266983669846698566986669876698866989669906699166992669936699466995669966699766998669996700067001670026700367004670056700667007670086700967010670116701267013670146701567016670176701867019670206702167022670236702467025670266702767028670296703067031670326703367034670356703667037670386703967040670416704267043670446704567046670476704867049670506705167052670536705467055670566705767058670596706067061670626706367064670656706667067670686706967070670716707267073670746707567076670776707867079670806708167082670836708467085670866708767088670896709067091670926709367094670956709667097670986709967100671016710267103671046710567106671076710867109671106711167112671136711467115671166711767118671196712067121671226712367124671256712667127671286712967130671316713267133671346713567136671376713867139671406714167142671436714467145671466714767148671496715067151671526715367154671556715667157671586715967160671616716267163671646716567166671676716867169671706717167172671736717467175671766717767178671796718067181671826718367184671856718667187671886718967190671916719267193671946719567196671976719867199672006720167202672036720467205672066720767208672096721067211672126721367214672156721667217672186721967220672216722267223672246722567226672276722867229672306723167232672336723467235672366723767238672396724067241672426724367244672456724667247672486724967250672516725267253672546725567256672576725867259672606726167262672636726467265672666726767268672696727067271672726727367274672756727667277672786727967280672816728267283672846728567286672876728867289672906729167292672936729467295672966729767298672996730067301673026730367304673056730667307673086730967310673116731267313673146731567316673176731867319673206732167322673236732467325673266732767328673296733067331673326733367334673356733667337673386733967340673416734267343673446734567346673476734867349673506735167352673536735467355673566735767358673596736067361673626736367364673656736667367673686736967370673716737267373673746737567376673776737867379673806738167382673836738467385673866738767388673896739067391673926739367394673956739667397673986739967400674016740267403674046740567406674076740867409674106741167412674136741467415674166741767418674196742067421674226742367424674256742667427674286742967430674316743267433674346743567436674376743867439674406744167442674436744467445674466744767448674496745067451674526745367454674556745667457674586745967460674616746267463674646746567466674676746867469674706747167472674736747467475674766747767478674796748067481674826748367484674856748667487674886748967490674916749267493674946749567496674976749867499675006750167502675036750467505675066750767508675096751067511675126751367514675156751667517675186751967520675216752267523675246752567526675276752867529675306753167532675336753467535675366753767538675396754067541675426754367544675456754667547675486754967550675516755267553675546755567556675576755867559675606756167562675636756467565675666756767568675696757067571675726757367574675756757667577675786757967580675816758267583675846758567586675876758867589675906759167592675936759467595675966759767598675996760067601676026760367604676056760667607676086760967610676116761267613676146761567616676176761867619676206762167622676236762467625676266762767628676296763067631676326763367634676356763667637676386763967640676416764267643676446764567646676476764867649676506765167652676536765467655676566765767658676596766067661676626766367664676656766667667676686766967670676716767267673676746767567676676776767867679676806768167682676836768467685676866768767688676896769067691676926769367694676956769667697676986769967700677016770267703677046770567706677076770867709677106771167712677136771467715677166771767718677196772067721677226772367724677256772667727677286772967730677316773267733677346773567736677376773867739677406774167742677436774467745677466774767748677496775067751677526775367754677556775667757677586775967760677616776267763677646776567766677676776867769677706777167772677736777467775677766777767778677796778067781677826778367784677856778667787677886778967790677916779267793677946779567796677976779867799678006780167802678036780467805678066780767808678096781067811678126781367814678156781667817678186781967820678216782267823678246782567826678276782867829678306783167832678336783467835678366783767838678396784067841678426784367844678456784667847678486784967850678516785267853678546785567856678576785867859678606786167862678636786467865678666786767868678696787067871678726787367874678756787667877678786787967880678816788267883678846788567886678876788867889678906789167892678936789467895678966789767898678996790067901679026790367904679056790667907679086790967910679116791267913679146791567916679176791867919679206792167922679236792467925679266792767928679296793067931679326793367934679356793667937679386793967940679416794267943679446794567946679476794867949679506795167952679536795467955679566795767958679596796067961679626796367964679656796667967679686796967970679716797267973679746797567976679776797867979679806798167982679836798467985679866798767988679896799067991679926799367994679956799667997679986799968000680016800268003680046800568006680076800868009680106801168012680136801468015680166801768018680196802068021680226802368024680256802668027680286802968030680316803268033680346803568036680376803868039680406804168042680436804468045680466804768048680496805068051680526805368054680556805668057680586805968060680616806268063680646806568066680676806868069680706807168072680736807468075680766807768078680796808068081680826808368084680856808668087680886808968090680916809268093680946809568096680976809868099681006810168102681036810468105681066810768108681096811068111681126811368114681156811668117681186811968120681216812268123681246812568126681276812868129681306813168132681336813468135681366813768138681396814068141681426814368144681456814668147681486814968150681516815268153681546815568156681576815868159681606816168162681636816468165681666816768168681696817068171681726817368174681756817668177681786817968180681816818268183681846818568186681876818868189681906819168192681936819468195681966819768198681996820068201682026820368204682056820668207682086820968210682116821268213682146821568216682176821868219682206822168222682236822468225682266822768228682296823068231682326823368234682356823668237682386823968240682416824268243682446824568246682476824868249682506825168252682536825468255682566825768258682596826068261682626826368264682656826668267682686826968270682716827268273682746827568276682776827868279682806828168282682836828468285682866828768288682896829068291682926829368294682956829668297682986829968300683016830268303683046830568306683076830868309683106831168312683136831468315683166831768318683196832068321683226832368324683256832668327683286832968330683316833268333683346833568336683376833868339683406834168342683436834468345683466834768348683496835068351683526835368354683556835668357683586835968360683616836268363683646836568366683676836868369683706837168372683736837468375683766837768378683796838068381683826838368384683856838668387683886838968390683916839268393683946839568396683976839868399684006840168402684036840468405684066840768408684096841068411684126841368414684156841668417684186841968420684216842268423684246842568426684276842868429684306843168432684336843468435684366843768438684396844068441684426844368444684456844668447684486844968450684516845268453684546845568456684576845868459684606846168462684636846468465684666846768468684696847068471684726847368474684756847668477684786847968480684816848268483684846848568486684876848868489684906849168492684936849468495684966849768498684996850068501685026850368504685056850668507685086850968510685116851268513685146851568516685176851868519685206852168522685236852468525685266852768528685296853068531685326853368534685356853668537685386853968540685416854268543685446854568546685476854868549685506855168552685536855468555685566855768558685596856068561685626856368564685656856668567685686856968570685716857268573685746857568576685776857868579685806858168582685836858468585685866858768588685896859068591685926859368594685956859668597685986859968600686016860268603686046860568606686076860868609686106861168612686136861468615686166861768618686196862068621686226862368624686256862668627686286862968630686316863268633686346863568636686376863868639686406864168642686436864468645686466864768648686496865068651686526865368654686556865668657686586865968660686616866268663686646866568666686676866868669686706867168672686736867468675686766867768678686796868068681686826868368684686856868668687686886868968690686916869268693686946869568696686976869868699687006870168702687036870468705687066870768708687096871068711687126871368714687156871668717687186871968720687216872268723687246872568726687276872868729687306873168732687336873468735687366873768738687396874068741687426874368744687456874668747687486874968750687516875268753687546875568756687576875868759687606876168762687636876468765687666876768768687696877068771687726877368774687756877668777687786877968780687816878268783687846878568786687876878868789687906879168792687936879468795687966879768798687996880068801688026880368804688056880668807688086880968810688116881268813688146881568816688176881868819688206882168822688236882468825688266882768828688296883068831688326883368834688356883668837688386883968840688416884268843688446884568846688476884868849688506885168852688536885468855688566885768858688596886068861688626886368864688656886668867688686886968870688716887268873688746887568876688776887868879688806888168882688836888468885688866888768888688896889068891688926889368894688956889668897688986889968900689016890268903689046890568906689076890868909689106891168912689136891468915689166891768918689196892068921689226892368924689256892668927689286892968930689316893268933689346893568936689376893868939689406894168942689436894468945689466894768948689496895068951689526895368954689556895668957689586895968960689616896268963689646896568966689676896868969689706897168972689736897468975689766897768978689796898068981689826898368984689856898668987689886898968990689916899268993689946899568996689976899868999690006900169002690036900469005690066900769008690096901069011690126901369014690156901669017690186901969020690216902269023690246902569026690276902869029690306903169032690336903469035690366903769038690396904069041690426904369044690456904669047690486904969050690516905269053690546905569056690576905869059690606906169062690636906469065690666906769068690696907069071690726907369074690756907669077690786907969080690816908269083690846908569086690876908869089690906909169092690936909469095690966909769098690996910069101691026910369104691056910669107691086910969110691116911269113691146911569116691176911869119691206912169122691236912469125691266912769128691296913069131691326913369134691356913669137691386913969140691416914269143691446914569146691476914869149691506915169152691536915469155691566915769158691596916069161691626916369164691656916669167691686916969170691716917269173691746917569176691776917869179691806918169182691836918469185691866918769188691896919069191691926919369194691956919669197691986919969200692016920269203692046920569206692076920869209692106921169212692136921469215692166921769218692196922069221692226922369224692256922669227692286922969230692316923269233692346923569236692376923869239692406924169242692436924469245692466924769248692496925069251692526925369254692556925669257692586925969260692616926269263692646926569266692676926869269692706927169272692736927469275692766927769278692796928069281692826928369284692856928669287692886928969290692916929269293692946929569296692976929869299693006930169302693036930469305693066930769308693096931069311693126931369314693156931669317693186931969320693216932269323693246932569326693276932869329693306933169332693336933469335693366933769338693396934069341693426934369344693456934669347693486934969350693516935269353693546935569356693576935869359693606936169362693636936469365693666936769368693696937069371693726937369374693756937669377693786937969380693816938269383693846938569386693876938869389693906939169392693936939469395693966939769398693996940069401694026940369404694056940669407694086940969410694116941269413694146941569416694176941869419694206942169422694236942469425694266942769428694296943069431694326943369434694356943669437694386943969440694416944269443694446944569446694476944869449694506945169452694536945469455694566945769458694596946069461694626946369464694656946669467694686946969470694716947269473694746947569476694776947869479694806948169482694836948469485694866948769488694896949069491694926949369494694956949669497694986949969500695016950269503695046950569506695076950869509695106951169512695136951469515695166951769518695196952069521695226952369524695256952669527695286952969530695316953269533695346953569536695376953869539695406954169542695436954469545695466954769548695496955069551695526955369554695556955669557695586955969560695616956269563695646956569566695676956869569695706957169572695736957469575695766957769578695796958069581695826958369584695856958669587695886958969590695916959269593695946959569596695976959869599696006960169602696036960469605696066960769608696096961069611696126961369614696156961669617696186961969620696216962269623696246962569626696276962869629696306963169632696336963469635696366963769638696396964069641696426964369644696456964669647696486964969650696516965269653696546965569656696576965869659696606966169662696636966469665696666966769668696696967069671696726967369674696756967669677696786967969680696816968269683696846968569686696876968869689696906969169692696936969469695696966969769698696996970069701697026970369704697056970669707697086970969710697116971269713697146971569716697176971869719697206972169722697236972469725697266972769728697296973069731697326973369734697356973669737697386973969740697416974269743697446974569746697476974869749697506975169752697536975469755697566975769758697596976069761697626976369764697656976669767697686976969770697716977269773697746977569776697776977869779697806978169782697836978469785697866978769788697896979069791697926979369794697956979669797697986979969800698016980269803698046980569806698076980869809698106981169812698136981469815698166981769818698196982069821698226982369824698256982669827698286982969830698316983269833698346983569836698376983869839698406984169842698436984469845698466984769848698496985069851698526985369854698556985669857698586985969860698616986269863698646986569866698676986869869698706987169872698736987469875698766987769878698796988069881698826988369884698856988669887698886988969890698916989269893698946989569896698976989869899699006990169902699036990469905699066990769908699096991069911699126991369914699156991669917699186991969920699216992269923699246992569926699276992869929699306993169932699336993469935699366993769938699396994069941699426994369944699456994669947699486994969950699516995269953699546995569956699576995869959699606996169962699636996469965699666996769968699696997069971699726997369974699756997669977699786997969980699816998269983699846998569986699876998869989699906999169992699936999469995699966999769998699997000070001700027000370004700057000670007700087000970010700117001270013700147001570016700177001870019700207002170022700237002470025700267002770028700297003070031700327003370034700357003670037700387003970040700417004270043700447004570046700477004870049700507005170052700537005470055700567005770058700597006070061700627006370064700657006670067700687006970070700717007270073700747007570076700777007870079700807008170082700837008470085700867008770088700897009070091700927009370094700957009670097700987009970100701017010270103701047010570106701077010870109701107011170112701137011470115701167011770118701197012070121701227012370124701257012670127701287012970130701317013270133701347013570136701377013870139701407014170142701437014470145701467014770148701497015070151701527015370154701557015670157701587015970160701617016270163701647016570166701677016870169701707017170172701737017470175701767017770178701797018070181701827018370184701857018670187701887018970190701917019270193701947019570196701977019870199702007020170202702037020470205702067020770208702097021070211702127021370214702157021670217702187021970220702217022270223702247022570226702277022870229702307023170232702337023470235702367023770238702397024070241702427024370244702457024670247702487024970250702517025270253702547025570256702577025870259702607026170262702637026470265702667026770268702697027070271702727027370274702757027670277702787027970280702817028270283702847028570286702877028870289702907029170292702937029470295702967029770298702997030070301703027030370304703057030670307703087030970310703117031270313703147031570316703177031870319703207032170322703237032470325703267032770328703297033070331703327033370334703357033670337703387033970340703417034270343703447034570346703477034870349703507035170352703537035470355703567035770358703597036070361703627036370364703657036670367703687036970370703717037270373703747037570376703777037870379703807038170382703837038470385703867038770388703897039070391703927039370394703957039670397703987039970400704017040270403704047040570406704077040870409704107041170412704137041470415704167041770418704197042070421704227042370424704257042670427704287042970430704317043270433704347043570436704377043870439704407044170442704437044470445704467044770448704497045070451704527045370454704557045670457704587045970460704617046270463704647046570466704677046870469704707047170472704737047470475704767047770478704797048070481704827048370484704857048670487704887048970490704917049270493704947049570496704977049870499705007050170502705037050470505705067050770508705097051070511705127051370514705157051670517705187051970520705217052270523705247052570526705277052870529705307053170532705337053470535705367053770538705397054070541705427054370544705457054670547705487054970550705517055270553705547055570556705577055870559705607056170562705637056470565705667056770568705697057070571705727057370574705757057670577705787057970580705817058270583705847058570586705877058870589705907059170592705937059470595705967059770598705997060070601706027060370604706057060670607706087060970610706117061270613706147061570616706177061870619706207062170622706237062470625706267062770628706297063070631706327063370634706357063670637706387063970640706417064270643706447064570646706477064870649706507065170652706537065470655706567065770658706597066070661706627066370664706657066670667706687066970670706717067270673706747067570676706777067870679706807068170682706837068470685706867068770688706897069070691706927069370694706957069670697706987069970700707017070270703707047070570706707077070870709707107071170712707137071470715707167071770718707197072070721707227072370724707257072670727707287072970730707317073270733707347073570736707377073870739707407074170742707437074470745707467074770748707497075070751707527075370754707557075670757707587075970760707617076270763707647076570766707677076870769707707077170772707737077470775707767077770778707797078070781707827078370784707857078670787707887078970790707917079270793707947079570796707977079870799708007080170802708037080470805708067080770808708097081070811708127081370814708157081670817708187081970820708217082270823708247082570826708277082870829708307083170832708337083470835708367083770838708397084070841708427084370844708457084670847708487084970850708517085270853708547085570856708577085870859708607086170862708637086470865708667086770868708697087070871708727087370874708757087670877708787087970880708817088270883708847088570886708877088870889708907089170892708937089470895708967089770898708997090070901709027090370904709057090670907709087090970910709117091270913709147091570916709177091870919709207092170922709237092470925709267092770928709297093070931709327093370934709357093670937709387093970940709417094270943709447094570946709477094870949709507095170952709537095470955709567095770958709597096070961709627096370964709657096670967709687096970970709717097270973709747097570976709777097870979709807098170982709837098470985709867098770988709897099070991709927099370994709957099670997709987099971000710017100271003710047100571006710077100871009710107101171012710137101471015710167101771018710197102071021710227102371024710257102671027710287102971030710317103271033710347103571036710377103871039710407104171042710437104471045710467104771048710497105071051710527105371054710557105671057710587105971060710617106271063710647106571066710677106871069710707107171072710737107471075710767107771078710797108071081710827108371084710857108671087710887108971090710917109271093710947109571096710977109871099711007110171102711037110471105711067110771108711097111071111711127111371114711157111671117711187111971120711217112271123711247112571126711277112871129711307113171132711337113471135711367113771138711397114071141711427114371144711457114671147711487114971150711517115271153711547115571156711577115871159711607116171162711637116471165711667116771168711697117071171711727117371174711757117671177711787117971180711817118271183711847118571186711877118871189711907119171192711937119471195711967119771198711997120071201712027120371204712057120671207712087120971210712117121271213712147121571216712177121871219712207122171222712237122471225712267122771228712297123071231712327123371234712357123671237712387123971240712417124271243712447124571246712477124871249712507125171252712537125471255712567125771258712597126071261712627126371264712657126671267712687126971270712717127271273712747127571276712777127871279712807128171282712837128471285712867128771288712897129071291712927129371294712957129671297712987129971300713017130271303713047130571306713077130871309713107131171312713137131471315713167131771318713197132071321713227132371324713257132671327713287132971330713317133271333713347133571336713377133871339713407134171342713437134471345713467134771348713497135071351713527135371354713557135671357713587135971360713617136271363713647136571366713677136871369713707137171372713737137471375713767137771378713797138071381713827138371384713857138671387713887138971390713917139271393713947139571396713977139871399714007140171402714037140471405714067140771408714097141071411714127141371414714157141671417714187141971420714217142271423714247142571426714277142871429714307143171432714337143471435714367143771438714397144071441714427144371444714457144671447714487144971450714517145271453714547145571456714577145871459714607146171462714637146471465714667146771468714697147071471714727147371474714757147671477714787147971480714817148271483714847148571486714877148871489714907149171492714937149471495714967149771498714997150071501715027150371504715057150671507715087150971510715117151271513715147151571516715177151871519715207152171522715237152471525715267152771528715297153071531715327153371534715357153671537715387153971540715417154271543715447154571546715477154871549715507155171552715537155471555715567155771558715597156071561715627156371564715657156671567715687156971570715717157271573715747157571576715777157871579715807158171582715837158471585715867158771588715897159071591715927159371594715957159671597715987159971600716017160271603716047160571606716077160871609716107161171612716137161471615716167161771618716197162071621716227162371624716257162671627716287162971630716317163271633716347163571636716377163871639716407164171642716437164471645716467164771648716497165071651716527165371654716557165671657716587165971660716617166271663716647166571666716677166871669716707167171672716737167471675716767167771678716797168071681716827168371684716857168671687716887168971690716917169271693716947169571696716977169871699717007170171702717037170471705717067170771708717097171071711717127171371714717157171671717717187171971720717217172271723717247172571726717277172871729717307173171732717337173471735717367173771738717397174071741717427174371744717457174671747717487174971750717517175271753717547175571756717577175871759717607176171762717637176471765717667176771768717697177071771717727177371774717757177671777717787177971780717817178271783717847178571786717877178871789717907179171792717937179471795717967179771798717997180071801718027180371804718057180671807718087180971810718117181271813718147181571816718177181871819718207182171822718237182471825718267182771828718297183071831718327183371834718357183671837718387183971840718417184271843718447184571846718477184871849718507185171852718537185471855718567185771858718597186071861718627186371864718657186671867718687186971870718717187271873718747187571876718777187871879718807188171882718837188471885718867188771888718897189071891718927189371894718957189671897718987189971900719017190271903719047190571906719077190871909719107191171912719137191471915719167191771918719197192071921719227192371924719257192671927719287192971930719317193271933719347193571936719377193871939719407194171942719437194471945719467194771948719497195071951719527195371954719557195671957719587195971960719617196271963719647196571966719677196871969719707197171972719737197471975719767197771978719797198071981719827198371984719857198671987719887198971990719917199271993719947199571996719977199871999720007200172002720037200472005720067200772008720097201072011720127201372014720157201672017720187201972020720217202272023720247202572026720277202872029720307203172032720337203472035720367203772038720397204072041720427204372044720457204672047720487204972050720517205272053720547205572056720577205872059720607206172062720637206472065720667206772068720697207072071720727207372074720757207672077720787207972080720817208272083720847208572086720877208872089720907209172092720937209472095720967209772098720997210072101721027210372104721057210672107721087210972110721117211272113721147211572116721177211872119721207212172122721237212472125721267212772128721297213072131721327213372134721357213672137721387213972140721417214272143721447214572146721477214872149721507215172152721537215472155721567215772158721597216072161721627216372164721657216672167721687216972170721717217272173721747217572176721777217872179721807218172182721837218472185721867218772188721897219072191721927219372194721957219672197721987219972200722017220272203722047220572206722077220872209722107221172212722137221472215722167221772218722197222072221722227222372224722257222672227722287222972230722317223272233722347223572236722377223872239722407224172242722437224472245722467224772248722497225072251722527225372254722557225672257722587225972260722617226272263722647226572266722677226872269722707227172272722737227472275722767227772278722797228072281722827228372284722857228672287722887228972290722917229272293722947229572296722977229872299723007230172302723037230472305723067230772308723097231072311723127231372314723157231672317723187231972320723217232272323723247232572326723277232872329723307233172332723337233472335723367233772338723397234072341723427234372344723457234672347723487234972350723517235272353723547235572356723577235872359723607236172362723637236472365723667236772368723697237072371723727237372374723757237672377723787237972380723817238272383723847238572386723877238872389723907239172392723937239472395723967239772398723997240072401724027240372404724057240672407724087240972410724117241272413724147241572416724177241872419724207242172422724237242472425724267242772428724297243072431724327243372434724357243672437724387243972440724417244272443724447244572446724477244872449724507245172452724537245472455724567245772458724597246072461724627246372464724657246672467724687246972470724717247272473724747247572476724777247872479724807248172482724837248472485724867248772488724897249072491724927249372494724957249672497724987249972500725017250272503725047250572506725077250872509725107251172512725137251472515725167251772518725197252072521725227252372524725257252672527725287252972530725317253272533725347253572536725377253872539725407254172542725437254472545725467254772548725497255072551725527255372554725557255672557725587255972560725617256272563725647256572566725677256872569725707257172572725737257472575725767257772578725797258072581725827258372584725857258672587725887258972590725917259272593725947259572596725977259872599726007260172602726037260472605726067260772608726097261072611726127261372614726157261672617726187261972620726217262272623726247262572626726277262872629726307263172632726337263472635726367263772638726397264072641726427264372644726457264672647726487264972650726517265272653726547265572656726577265872659726607266172662726637266472665726667266772668726697267072671726727267372674726757267672677726787267972680726817268272683726847268572686726877268872689726907269172692726937269472695726967269772698726997270072701727027270372704727057270672707727087270972710727117271272713727147271572716727177271872719727207272172722727237272472725727267272772728727297273072731727327273372734727357273672737727387273972740727417274272743727447274572746727477274872749727507275172752727537275472755727567275772758727597276072761727627276372764727657276672767727687276972770727717277272773727747277572776727777277872779727807278172782727837278472785727867278772788727897279072791727927279372794727957279672797727987279972800728017280272803728047280572806728077280872809728107281172812728137281472815728167281772818728197282072821728227282372824728257282672827728287282972830728317283272833728347283572836728377283872839728407284172842728437284472845728467284772848728497285072851728527285372854728557285672857728587285972860728617286272863728647286572866728677286872869728707287172872728737287472875728767287772878728797288072881728827288372884728857288672887728887288972890728917289272893728947289572896728977289872899729007290172902729037290472905729067290772908729097291072911729127291372914729157291672917729187291972920729217292272923729247292572926729277292872929729307293172932729337293472935729367293772938729397294072941729427294372944729457294672947729487294972950729517295272953729547295572956729577295872959729607296172962729637296472965729667296772968729697297072971729727297372974729757297672977729787297972980729817298272983729847298572986729877298872989729907299172992729937299472995729967299772998729997300073001730027300373004730057300673007730087300973010730117301273013730147301573016730177301873019730207302173022730237302473025730267302773028730297303073031730327303373034730357303673037730387303973040730417304273043730447304573046730477304873049730507305173052730537305473055730567305773058730597306073061730627306373064730657306673067730687306973070730717307273073730747307573076730777307873079730807308173082730837308473085730867308773088730897309073091730927309373094730957309673097730987309973100731017310273103731047310573106731077310873109731107311173112731137311473115731167311773118731197312073121731227312373124731257312673127731287312973130731317313273133731347313573136731377313873139731407314173142731437314473145731467314773148731497315073151731527315373154731557315673157731587315973160731617316273163731647316573166731677316873169731707317173172731737317473175731767317773178731797318073181731827318373184731857318673187731887318973190731917319273193731947319573196731977319873199732007320173202732037320473205732067320773208732097321073211732127321373214732157321673217732187321973220732217322273223732247322573226732277322873229732307323173232732337323473235732367323773238732397324073241732427324373244732457324673247732487324973250732517325273253732547325573256732577325873259732607326173262732637326473265732667326773268732697327073271732727327373274732757327673277732787327973280732817328273283732847328573286732877328873289732907329173292732937329473295732967329773298732997330073301733027330373304733057330673307733087330973310733117331273313733147331573316733177331873319733207332173322733237332473325733267332773328733297333073331733327333373334733357333673337733387333973340733417334273343733447334573346733477334873349733507335173352733537335473355733567335773358733597336073361733627336373364733657336673367733687336973370733717337273373733747337573376733777337873379733807338173382733837338473385733867338773388733897339073391733927339373394733957339673397733987339973400734017340273403734047340573406734077340873409734107341173412734137341473415734167341773418734197342073421734227342373424734257342673427734287342973430734317343273433734347343573436734377343873439734407344173442734437344473445734467344773448734497345073451734527345373454734557345673457734587345973460734617346273463734647346573466734677346873469734707347173472734737347473475734767347773478734797348073481734827348373484734857348673487734887348973490734917349273493734947349573496734977349873499735007350173502735037350473505735067350773508735097351073511735127351373514735157351673517735187351973520735217352273523735247352573526735277352873529735307353173532735337353473535735367353773538735397354073541735427354373544735457354673547735487354973550735517355273553735547355573556735577355873559735607356173562735637356473565735667356773568735697357073571735727357373574735757357673577735787357973580735817358273583735847358573586735877358873589735907359173592735937359473595735967359773598735997360073601736027360373604736057360673607736087360973610736117361273613736147361573616736177361873619736207362173622736237362473625736267362773628736297363073631736327363373634736357363673637736387363973640736417364273643736447364573646736477364873649736507365173652736537365473655736567365773658736597366073661736627366373664736657366673667736687366973670736717367273673736747367573676736777367873679736807368173682736837368473685736867368773688736897369073691736927369373694736957369673697736987369973700737017370273703737047370573706737077370873709737107371173712737137371473715737167371773718737197372073721737227372373724737257372673727737287372973730737317373273733737347373573736737377373873739737407374173742737437374473745737467374773748737497375073751737527375373754737557375673757737587375973760737617376273763737647376573766737677376873769737707377173772737737377473775737767377773778737797378073781737827378373784737857378673787737887378973790737917379273793737947379573796737977379873799738007380173802738037380473805738067380773808738097381073811738127381373814738157381673817738187381973820738217382273823738247382573826738277382873829738307383173832738337383473835738367383773838738397384073841738427384373844738457384673847738487384973850738517385273853738547385573856738577385873859738607386173862738637386473865738667386773868738697387073871738727387373874738757387673877738787387973880738817388273883738847388573886738877388873889738907389173892738937389473895738967389773898738997390073901739027390373904739057390673907739087390973910739117391273913739147391573916739177391873919739207392173922739237392473925739267392773928739297393073931739327393373934739357393673937739387393973940739417394273943739447394573946739477394873949739507395173952739537395473955739567395773958739597396073961739627396373964739657396673967739687396973970739717397273973739747397573976739777397873979739807398173982739837398473985739867398773988739897399073991739927399373994739957399673997739987399974000740017400274003740047400574006740077400874009740107401174012740137401474015740167401774018740197402074021740227402374024740257402674027740287402974030740317403274033740347403574036740377403874039740407404174042740437404474045740467404774048740497405074051740527405374054740557405674057740587405974060740617406274063740647406574066740677406874069740707407174072740737407474075740767407774078740797408074081740827408374084740857408674087740887408974090740917409274093740947409574096740977409874099741007410174102741037410474105741067410774108741097411074111741127411374114741157411674117741187411974120741217412274123741247412574126741277412874129741307413174132741337413474135741367413774138741397414074141741427414374144741457414674147741487414974150741517415274153741547415574156741577415874159741607416174162741637416474165741667416774168741697417074171741727417374174741757417674177741787417974180741817418274183741847418574186741877418874189741907419174192741937419474195741967419774198741997420074201742027420374204742057420674207742087420974210742117421274213742147421574216742177421874219742207422174222742237422474225742267422774228742297423074231742327423374234742357423674237742387423974240742417424274243742447424574246742477424874249742507425174252742537425474255742567425774258742597426074261742627426374264742657426674267742687426974270742717427274273742747427574276742777427874279742807428174282742837428474285742867428774288742897429074291742927429374294742957429674297742987429974300743017430274303743047430574306743077430874309743107431174312743137431474315743167431774318743197432074321743227432374324743257432674327743287432974330743317433274333743347433574336743377433874339743407434174342743437434474345743467434774348743497435074351743527435374354743557435674357743587435974360743617436274363743647436574366743677436874369743707437174372743737437474375743767437774378743797438074381743827438374384743857438674387743887438974390743917439274393743947439574396743977439874399744007440174402744037440474405744067440774408744097441074411744127441374414744157441674417744187441974420744217442274423744247442574426744277442874429744307443174432744337443474435744367443774438744397444074441744427444374444744457444674447744487444974450744517445274453744547445574456744577445874459744607446174462744637446474465744667446774468744697447074471744727447374474744757447674477744787447974480744817448274483744847448574486744877448874489744907449174492744937449474495744967449774498744997450074501745027450374504745057450674507745087450974510745117451274513745147451574516745177451874519745207452174522745237452474525745267452774528745297453074531745327453374534745357453674537745387453974540745417454274543745447454574546745477454874549745507455174552745537455474555745567455774558745597456074561745627456374564745657456674567745687456974570745717457274573745747457574576745777457874579745807458174582745837458474585745867458774588745897459074591745927459374594745957459674597745987459974600746017460274603746047460574606746077460874609746107461174612746137461474615746167461774618746197462074621746227462374624746257462674627746287462974630746317463274633746347463574636746377463874639746407464174642746437464474645746467464774648746497465074651746527465374654746557465674657746587465974660746617466274663746647466574666746677466874669746707467174672746737467474675746767467774678746797468074681746827468374684746857468674687746887468974690746917469274693746947469574696746977469874699747007470174702747037470474705747067470774708747097471074711747127471374714747157471674717747187471974720747217472274723747247472574726747277472874729747307473174732747337473474735747367473774738747397474074741747427474374744747457474674747747487474974750747517475274753747547475574756747577475874759747607476174762747637476474765747667476774768747697477074771747727477374774747757477674777747787477974780747817478274783747847478574786747877478874789747907479174792747937479474795747967479774798747997480074801748027480374804748057480674807748087480974810748117481274813748147481574816748177481874819748207482174822748237482474825748267482774828748297483074831748327483374834748357483674837748387483974840748417484274843748447484574846748477484874849748507485174852748537485474855748567485774858748597486074861748627486374864748657486674867748687486974870748717487274873748747487574876748777487874879748807488174882748837488474885748867488774888748897489074891748927489374894748957489674897748987489974900749017490274903749047490574906749077490874909749107491174912749137491474915749167491774918749197492074921749227492374924749257492674927749287492974930749317493274933749347493574936749377493874939749407494174942749437494474945749467494774948749497495074951749527495374954749557495674957749587495974960749617496274963749647496574966749677496874969749707497174972749737497474975749767497774978749797498074981749827498374984749857498674987749887498974990749917499274993749947499574996749977499874999750007500175002750037500475005750067500775008750097501075011750127501375014750157501675017750187501975020750217502275023750247502575026750277502875029750307503175032750337503475035750367503775038750397504075041750427504375044750457504675047750487504975050750517505275053750547505575056750577505875059750607506175062750637506475065750667506775068750697507075071750727507375074750757507675077750787507975080750817508275083750847508575086750877508875089750907509175092750937509475095750967509775098750997510075101751027510375104751057510675107751087510975110751117511275113751147511575116751177511875119751207512175122751237512475125751267512775128751297513075131751327513375134751357513675137751387513975140751417514275143751447514575146751477514875149751507515175152751537515475155751567515775158751597516075161751627516375164751657516675167751687516975170751717517275173751747517575176751777517875179751807518175182751837518475185751867518775188751897519075191751927519375194751957519675197751987519975200752017520275203752047520575206752077520875209752107521175212752137521475215752167521775218752197522075221752227522375224752257522675227752287522975230752317523275233752347523575236752377523875239752407524175242752437524475245752467524775248752497525075251752527525375254752557525675257752587525975260752617526275263752647526575266752677526875269752707527175272752737527475275752767527775278752797528075281752827528375284752857528675287752887528975290752917529275293752947529575296752977529875299753007530175302753037530475305753067530775308753097531075311753127531375314753157531675317753187531975320753217532275323753247532575326753277532875329753307533175332753337533475335753367533775338753397534075341753427534375344753457534675347753487534975350753517535275353753547535575356753577535875359753607536175362753637536475365753667536775368753697537075371753727537375374753757537675377753787537975380753817538275383753847538575386753877538875389753907539175392753937539475395753967539775398753997540075401754027540375404754057540675407754087540975410754117541275413754147541575416754177541875419754207542175422754237542475425754267542775428754297543075431754327543375434754357543675437754387543975440754417544275443754447544575446754477544875449754507545175452754537545475455754567545775458754597546075461754627546375464754657546675467754687546975470754717547275473754747547575476754777547875479754807548175482754837548475485754867548775488754897549075491754927549375494754957549675497754987549975500755017550275503755047550575506755077550875509755107551175512755137551475515755167551775518755197552075521755227552375524755257552675527755287552975530755317553275533755347553575536755377553875539755407554175542755437554475545755467554775548755497555075551755527555375554755557555675557755587555975560755617556275563755647556575566755677556875569755707557175572755737557475575755767557775578755797558075581755827558375584755857558675587755887558975590755917559275593755947559575596755977559875599756007560175602756037560475605756067560775608756097561075611756127561375614756157561675617756187561975620756217562275623756247562575626756277562875629756307563175632756337563475635756367563775638756397564075641756427564375644756457564675647756487564975650756517565275653756547565575656756577565875659756607566175662756637566475665756667566775668756697567075671756727567375674756757567675677756787567975680756817568275683756847568575686756877568875689756907569175692756937569475695756967569775698756997570075701757027570375704757057570675707757087570975710757117571275713757147571575716757177571875719757207572175722757237572475725757267572775728757297573075731757327573375734757357573675737757387573975740757417574275743757447574575746757477574875749757507575175752757537575475755757567575775758757597576075761757627576375764757657576675767757687576975770757717577275773757747577575776757777577875779757807578175782757837578475785757867578775788757897579075791757927579375794757957579675797757987579975800758017580275803758047580575806758077580875809758107581175812758137581475815758167581775818758197582075821758227582375824758257582675827758287582975830758317583275833758347583575836758377583875839758407584175842758437584475845758467584775848758497585075851758527585375854758557585675857758587585975860758617586275863758647586575866758677586875869758707587175872758737587475875758767587775878758797588075881758827588375884758857588675887758887588975890758917589275893758947589575896758977589875899759007590175902759037590475905759067590775908759097591075911759127591375914759157591675917759187591975920759217592275923759247592575926759277592875929759307593175932759337593475935759367593775938759397594075941759427594375944759457594675947759487594975950759517595275953759547595575956759577595875959759607596175962759637596475965759667596775968759697597075971759727597375974759757597675977759787597975980759817598275983759847598575986759877598875989759907599175992759937599475995759967599775998759997600076001760027600376004760057600676007760087600976010760117601276013760147601576016760177601876019760207602176022760237602476025760267602776028760297603076031760327603376034760357603676037760387603976040760417604276043760447604576046760477604876049760507605176052760537605476055760567605776058760597606076061760627606376064760657606676067760687606976070760717607276073760747607576076760777607876079760807608176082760837608476085760867608776088760897609076091760927609376094760957609676097760987609976100761017610276103761047610576106761077610876109761107611176112761137611476115761167611776118761197612076121761227612376124761257612676127761287612976130761317613276133761347613576136761377613876139761407614176142761437614476145761467614776148761497615076151761527615376154761557615676157761587615976160761617616276163761647616576166761677616876169761707617176172761737617476175761767617776178761797618076181761827618376184761857618676187761887618976190761917619276193761947619576196761977619876199762007620176202762037620476205762067620776208762097621076211762127621376214762157621676217762187621976220762217622276223762247622576226762277622876229762307623176232762337623476235762367623776238762397624076241762427624376244762457624676247762487624976250762517625276253762547625576256762577625876259762607626176262762637626476265762667626776268762697627076271762727627376274762757627676277762787627976280762817628276283762847628576286762877628876289762907629176292762937629476295762967629776298762997630076301763027630376304763057630676307763087630976310763117631276313763147631576316763177631876319763207632176322763237632476325763267632776328763297633076331763327633376334763357633676337763387633976340763417634276343763447634576346763477634876349763507635176352763537635476355763567635776358763597636076361763627636376364763657636676367763687636976370763717637276373763747637576376763777637876379763807638176382763837638476385763867638776388763897639076391763927639376394763957639676397763987639976400764017640276403764047640576406764077640876409764107641176412764137641476415764167641776418764197642076421764227642376424764257642676427764287642976430764317643276433764347643576436764377643876439764407644176442764437644476445764467644776448764497645076451764527645376454764557645676457764587645976460764617646276463764647646576466764677646876469764707647176472764737647476475764767647776478764797648076481764827648376484764857648676487764887648976490764917649276493764947649576496764977649876499765007650176502765037650476505765067650776508765097651076511765127651376514765157651676517765187651976520765217652276523765247652576526765277652876529765307653176532765337653476535765367653776538765397654076541765427654376544765457654676547765487654976550765517655276553765547655576556765577655876559765607656176562765637656476565765667656776568765697657076571765727657376574765757657676577765787657976580765817658276583765847658576586765877658876589765907659176592765937659476595765967659776598765997660076601766027660376604766057660676607766087660976610766117661276613766147661576616766177661876619766207662176622766237662476625766267662776628766297663076631766327663376634766357663676637766387663976640766417664276643766447664576646766477664876649766507665176652766537665476655766567665776658766597666076661766627666376664766657666676667766687666976670766717667276673766747667576676766777667876679766807668176682766837668476685766867668776688766897669076691766927669376694766957669676697766987669976700767017670276703767047670576706767077670876709767107671176712767137671476715767167671776718767197672076721767227672376724767257672676727767287672976730767317673276733767347673576736767377673876739767407674176742767437674476745767467674776748767497675076751767527675376754767557675676757767587675976760767617676276763767647676576766767677676876769767707677176772767737677476775767767677776778767797678076781767827678376784767857678676787767887678976790767917679276793767947679576796767977679876799768007680176802768037680476805768067680776808768097681076811768127681376814768157681676817768187681976820768217682276823768247682576826768277682876829768307683176832768337683476835768367683776838768397684076841768427684376844768457684676847768487684976850768517685276853768547685576856768577685876859768607686176862768637686476865768667686776868768697687076871768727687376874768757687676877768787687976880768817688276883768847688576886768877688876889768907689176892768937689476895768967689776898768997690076901769027690376904769057690676907769087690976910769117691276913769147691576916769177691876919769207692176922769237692476925769267692776928769297693076931769327693376934769357693676937769387693976940769417694276943769447694576946769477694876949769507695176952769537695476955769567695776958769597696076961769627696376964769657696676967769687696976970769717697276973769747697576976769777697876979769807698176982769837698476985769867698776988769897699076991769927699376994769957699676997769987699977000770017700277003770047700577006770077700877009770107701177012770137701477015770167701777018770197702077021770227702377024770257702677027770287702977030770317703277033770347703577036770377703877039770407704177042770437704477045770467704777048770497705077051770527705377054770557705677057770587705977060770617706277063770647706577066770677706877069770707707177072770737707477075770767707777078
  1. /*
  2. * Single file autogenerated distributable for Duktape 1.2.99.
  3. * Git commit e9bba790e9988e98790034f5fbf3305c8a623f42 (v1.2.0-86-ge9bba79).
  4. *
  5. * See Duktape AUTHORS.rst and LICENSE.txt for copyright and
  6. * licensing information.
  7. */
  8. /* LICENSE.txt */
  9. /*
  10. * ===============
  11. * Duktape license
  12. * ===============
  13. *
  14. * (http://opensource.org/licenses/MIT)
  15. *
  16. * Copyright (c) 2013-2015 by Duktape authors (see AUTHORS.rst)
  17. *
  18. * Permission is hereby granted, free of charge, to any person obtaining a copy
  19. * of this software and associated documentation files (the "Software"), to deal
  20. * in the Software without restriction, including without limitation the rights
  21. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  22. * copies of the Software, and to permit persons to whom the Software is
  23. * furnished to do so, subject to the following conditions:
  24. *
  25. * The above copyright notice and this permission notice shall be included in
  26. * all copies or substantial portions of the Software.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  29. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  30. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  31. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  32. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  33. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  34. * THE SOFTWARE.
  35. *
  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 and agreed to irrevocably license
  57. * their contributions under the Duktape ``LICENSE.txt`` (in order of appearance):
  58. *
  59. * * Sami Vaarala <[email protected]>
  60. * * Niki Dobrev
  61. * * Andreas \u00d6man <[email protected]>
  62. * * L\u00e1szl\u00f3 Lang\u00f3 <[email protected]>
  63. * * Legimet <[email protected]>
  64. *
  65. * Other contributions
  66. * ===================
  67. *
  68. * The following people have contributed something other than code (e.g. reported
  69. * bugs, provided ideas, etc; roughly in order of appearance):
  70. *
  71. * * Greg Burns
  72. * * Anthony Rabine
  73. * * Carlos Costa
  74. * * Aur\u00e9lien Bouilland
  75. * * Preet Desai (Pris Matic)
  76. * * judofyr (http://www.reddit.com/user/judofyr)
  77. * * Jason Woofenden
  78. * * Micha\u0142 Przyby\u015b
  79. * * Anthony Howe
  80. * * Conrad Pankoff
  81. * * Jim Schimpf
  82. * * Rajaran Gaunker (https://github.com/zimbabao)
  83. * * Andreas \u00d6man
  84. * * Doug Sanden
  85. * * Josh Engebretson (https://github.com/JoshEngebretson)
  86. * * Remo Eichenberger (https://github.com/remoe)
  87. * * Mamod Mehyar (https://github.com/mamod)
  88. * * David Demelier (https://github.com/markand)
  89. * * Tim Caswell (https://github.com/creationix)
  90. * * Mitchell Blank Jr (https://github.com/mitchblank)
  91. * * https://github.com/yushli
  92. * * Seo Sanghyeon (https://github.com/sanxiyn)
  93. * * Han ChoongWoo (https://github.com/tunz)
  94. * * Joshua Peek (https://github.com/josh)
  95. * * Bruce E. Pascoe (https://github.com/fatcerberus)
  96. * * https://github.com/Kelledin
  97. *
  98. * If you are accidentally missing from this list, send me an e-mail
  99. * (``[email protected]``) and I'll fix the omission.
  100. */
  101. #line 1 "duk_internal.h"
  102. /*
  103. * Top-level include file to be used for all (internal) source files.
  104. *
  105. * Source files should not include individual header files, as they
  106. * have not been designed to be individually included.
  107. */
  108. #ifndef DUK_INTERNAL_H_INCLUDED
  109. #define DUK_INTERNAL_H_INCLUDED
  110. /*
  111. * The 'duktape.h' header provides the public API, but also handles all
  112. * compiler and platform specific feature detection, Duktape feature
  113. * resolution, inclusion of system headers, etc. These have been merged
  114. * because the public API is also dependent on e.g. detecting appropriate
  115. * C types which is quite platform/compiler specific especially for a non-C99
  116. * build. The public API is also dependent on the resolved feature set.
  117. *
  118. * Some actions taken by the merged header (such as including system headers)
  119. * are not appropriate for building a user application. The define
  120. * DUK_COMPILING_DUKTAPE allows the merged header to skip/include some
  121. * sections depending on what is being built.
  122. */
  123. #define DUK_COMPILING_DUKTAPE
  124. #include "duktape.h"
  125. /*
  126. * User declarations, e.g. prototypes for user functions used by Duktape
  127. * macros. Concretely, if DUK_OPT_PANIC_HANDLER is used and the macro
  128. * value calls a user function, it needs to be declared for Duktape
  129. * compilation to avoid warnings.
  130. */
  131. DUK_USE_USER_DECLARE()
  132. /*
  133. * Duktape includes (other than duk_features.h)
  134. *
  135. * The header files expect to be included in an order which satisfies header
  136. * dependencies correctly (the headers themselves don't include any other
  137. * includes). Forward declarations are used to break circular struct/typedef
  138. * dependencies.
  139. */
  140. #line 1 "duk_replacements.h"
  141. #ifndef DUK_REPLACEMENTS_H_INCLUDED
  142. #define DUK_REPLACEMENTS_H_INCLUDED
  143. #ifdef DUK_USE_REPL_FPCLASSIFY
  144. DUK_INTERNAL_DECL int duk_repl_fpclassify(double x);
  145. #endif
  146. #ifdef DUK_USE_REPL_SIGNBIT
  147. DUK_INTERNAL_DECL int duk_repl_signbit(double x);
  148. #endif
  149. #ifdef DUK_USE_REPL_ISFINITE
  150. DUK_INTERNAL_DECL int duk_repl_isfinite(double x);
  151. #endif
  152. #ifdef DUK_USE_REPL_ISNAN
  153. DUK_INTERNAL_DECL int duk_repl_isnan(double x);
  154. #endif
  155. #ifdef DUK_USE_REPL_ISINF
  156. DUK_INTERNAL_DECL int duk_repl_isinf(double x);
  157. #endif
  158. #endif /* DUK_REPLACEMENTS_H_INCLUDED */
  159. #line 1 "duk_jmpbuf.h"
  160. /*
  161. * Wrapper for jmp_buf.
  162. *
  163. * This is used because jmp_buf is an array type for backward compatibility.
  164. * Wrapping jmp_buf in a struct makes pointer references, sizeof, etc,
  165. * behave more intuitively.
  166. *
  167. * http://en.wikipedia.org/wiki/Setjmp.h#Member_types
  168. */
  169. #ifndef DUK_JMPBUF_H_INCLUDED
  170. #define DUK_JMPBUF_H_INCLUDED
  171. struct duk_jmpbuf {
  172. #if defined(DUK_USE_SETJMP) || defined(DUK_USE_UNDERSCORE_SETJMP)
  173. jmp_buf jb;
  174. #elif defined(DUK_USE_SIGSETJMP)
  175. sigjmp_buf jb;
  176. #else
  177. #error internal error, no long control transfer provider
  178. #endif
  179. };
  180. #endif /* DUK_JMPBUF_H_INCLUDED */
  181. #line 1 "duk_forwdecl.h"
  182. /*
  183. * Forward declarations for all Duktape structures.
  184. */
  185. #ifndef DUK_FORWDECL_H_INCLUDED
  186. #define DUK_FORWDECL_H_INCLUDED
  187. /*
  188. * Forward declarations
  189. */
  190. struct duk_jmpbuf;
  191. /* duk_tval intentionally skipped */
  192. struct duk_heaphdr;
  193. struct duk_heaphdr_string;
  194. struct duk_hstring;
  195. struct duk_hstring_external;
  196. struct duk_hobject;
  197. struct duk_hcompiledfunction;
  198. struct duk_hnativefunction;
  199. struct duk_hthread;
  200. struct duk_hbufferobject;
  201. struct duk_hbuffer;
  202. struct duk_hbuffer_fixed;
  203. struct duk_hbuffer_dynamic;
  204. struct duk_propaccessor;
  205. union duk_propvalue;
  206. struct duk_propdesc;
  207. struct duk_heap;
  208. struct duk_breakpoint;
  209. struct duk_activation;
  210. struct duk_catcher;
  211. struct duk_strcache;
  212. struct duk_ljstate;
  213. struct duk_strtab_entry;
  214. #ifdef DUK_USE_DEBUG
  215. struct duk_fixedbuffer;
  216. #endif
  217. struct duk_bitdecoder_ctx;
  218. struct duk_bitencoder_ctx;
  219. struct duk_token;
  220. struct duk_re_token;
  221. struct duk_lexer_point;
  222. struct duk_lexer_ctx;
  223. struct duk_compiler_instr;
  224. struct duk_compiler_func;
  225. struct duk_compiler_ctx;
  226. struct duk_re_matcher_ctx;
  227. struct duk_re_compiler_ctx;
  228. typedef struct duk_jmpbuf duk_jmpbuf;
  229. /* duk_tval intentionally skipped */
  230. typedef struct duk_heaphdr duk_heaphdr;
  231. typedef struct duk_heaphdr_string duk_heaphdr_string;
  232. typedef struct duk_hstring duk_hstring;
  233. typedef struct duk_hstring_external duk_hstring_external;
  234. typedef struct duk_hobject duk_hobject;
  235. typedef struct duk_hcompiledfunction duk_hcompiledfunction;
  236. typedef struct duk_hnativefunction duk_hnativefunction;
  237. typedef struct duk_hbufferobject duk_hbufferobject;
  238. typedef struct duk_hthread duk_hthread;
  239. typedef struct duk_hbuffer duk_hbuffer;
  240. typedef struct duk_hbuffer_fixed duk_hbuffer_fixed;
  241. typedef struct duk_hbuffer_dynamic duk_hbuffer_dynamic;
  242. typedef struct duk_propaccessor duk_propaccessor;
  243. typedef union duk_propvalue duk_propvalue;
  244. typedef struct duk_propdesc duk_propdesc;
  245. typedef struct duk_heap duk_heap;
  246. typedef struct duk_breakpoint duk_breakpoint;
  247. typedef struct duk_activation duk_activation;
  248. typedef struct duk_catcher duk_catcher;
  249. typedef struct duk_strcache duk_strcache;
  250. typedef struct duk_ljstate duk_ljstate;
  251. typedef struct duk_strtab_entry duk_strtab_entry;
  252. #ifdef DUK_USE_DEBUG
  253. typedef struct duk_fixedbuffer duk_fixedbuffer;
  254. #endif
  255. typedef struct duk_bitdecoder_ctx duk_bitdecoder_ctx;
  256. typedef struct duk_bitencoder_ctx duk_bitencoder_ctx;
  257. typedef struct duk_token duk_token;
  258. typedef struct duk_re_token duk_re_token;
  259. typedef struct duk_lexer_point duk_lexer_point;
  260. typedef struct duk_lexer_ctx duk_lexer_ctx;
  261. typedef struct duk_compiler_instr duk_compiler_instr;
  262. typedef struct duk_compiler_func duk_compiler_func;
  263. typedef struct duk_compiler_ctx duk_compiler_ctx;
  264. typedef struct duk_re_matcher_ctx duk_re_matcher_ctx;
  265. typedef struct duk_re_compiler_ctx duk_re_compiler_ctx;
  266. #endif /* DUK_FORWDECL_H_INCLUDED */
  267. #line 1 "duk_builtins.h"
  268. /*
  269. * Automatically generated by genbuiltins.py, do not edit!
  270. */
  271. #ifndef DUK_BUILTINS_H_INCLUDED
  272. #define DUK_BUILTINS_H_INCLUDED
  273. #if defined(DUK_USE_DOUBLE_LE)
  274. #if !defined(DUK_SINGLE_FILE)
  275. DUK_INTERNAL_DECL const duk_uint8_t duk_strings_data[2624];
  276. #endif /* !DUK_SINGLE_FILE */
  277. #define DUK_STRDATA_DATA_LENGTH 2624
  278. #define DUK_STRDATA_MAX_STRLEN 24
  279. #define DUK_STRIDX_UC_LOGGER 0 /* 'Logger' */
  280. #define DUK_STRIDX_UC_THREAD 1 /* 'Thread' */
  281. #define DUK_STRIDX_UC_POINTER 2 /* 'Pointer' */
  282. #define DUK_STRIDX_DEC_ENV 3 /* 'DecEnv' */
  283. #define DUK_STRIDX_OBJ_ENV 4 /* 'ObjEnv' */
  284. #define DUK_STRIDX_FLOAT64_ARRAY 5 /* 'Float64Array' */
  285. #define DUK_STRIDX_FLOAT32_ARRAY 6 /* 'Float32Array' */
  286. #define DUK_STRIDX_UINT32_ARRAY 7 /* 'Uint32Array' */
  287. #define DUK_STRIDX_INT32_ARRAY 8 /* 'Int32Array' */
  288. #define DUK_STRIDX_UINT16_ARRAY 9 /* 'Uint16Array' */
  289. #define DUK_STRIDX_INT16_ARRAY 10 /* 'Int16Array' */
  290. #define DUK_STRIDX_UINT8_CLAMPED_ARRAY 11 /* 'Uint8ClampedArray' */
  291. #define DUK_STRIDX_UINT8_ARRAY 12 /* 'Uint8Array' */
  292. #define DUK_STRIDX_INT8_ARRAY 13 /* 'Int8Array' */
  293. #define DUK_STRIDX_DATA_VIEW 14 /* 'DataView' */
  294. #define DUK_STRIDX_ARRAY_BUFFER 15 /* 'ArrayBuffer' */
  295. #define DUK_STRIDX_UC_BUFFER 16 /* 'Buffer' */
  296. #define DUK_STRIDX_EMPTY_STRING 17 /* '' */
  297. #define DUK_STRIDX_GLOBAL 18 /* 'global' */
  298. #define DUK_STRIDX_UC_ARGUMENTS 19 /* 'Arguments' */
  299. #define DUK_STRIDX_JSON 20 /* 'JSON' */
  300. #define DUK_STRIDX_MATH 21 /* 'Math' */
  301. #define DUK_STRIDX_UC_ERROR 22 /* 'Error' */
  302. #define DUK_STRIDX_REG_EXP 23 /* 'RegExp' */
  303. #define DUK_STRIDX_DATE 24 /* 'Date' */
  304. #define DUK_STRIDX_UC_NUMBER 25 /* 'Number' */
  305. #define DUK_STRIDX_UC_BOOLEAN 26 /* 'Boolean' */
  306. #define DUK_STRIDX_UC_STRING 27 /* 'String' */
  307. #define DUK_STRIDX_ARRAY 28 /* 'Array' */
  308. #define DUK_STRIDX_UC_FUNCTION 29 /* 'Function' */
  309. #define DUK_STRIDX_UC_OBJECT 30 /* 'Object' */
  310. #define DUK_STRIDX_UC_NULL 31 /* 'Null' */
  311. #define DUK_STRIDX_UC_UNDEFINED 32 /* 'Undefined' */
  312. #define DUK_STRIDX_JSON_EXT_FUNCTION2 33 /* '{_func:true}' */
  313. #define DUK_STRIDX_JSON_EXT_FUNCTION1 34 /* '{"_func":true}' */
  314. #define DUK_STRIDX_JSON_EXT_NEGINF 35 /* '{"_ninf":true}' */
  315. #define DUK_STRIDX_JSON_EXT_POSINF 36 /* '{"_inf":true}' */
  316. #define DUK_STRIDX_JSON_EXT_NAN 37 /* '{"_nan":true}' */
  317. #define DUK_STRIDX_JSON_EXT_UNDEFINED 38 /* '{"_undef":true}' */
  318. #define DUK_STRIDX_TO_LOG_STRING 39 /* 'toLogString' */
  319. #define DUK_STRIDX_CLOG 40 /* 'clog' */
  320. #define DUK_STRIDX_LC_L 41 /* 'l' */
  321. #define DUK_STRIDX_LC_N 42 /* 'n' */
  322. #define DUK_STRIDX_LC_FATAL 43 /* 'fatal' */
  323. #define DUK_STRIDX_LC_ERROR 44 /* 'error' */
  324. #define DUK_STRIDX_LC_WARN 45 /* 'warn' */
  325. #define DUK_STRIDX_LC_DEBUG 46 /* 'debug' */
  326. #define DUK_STRIDX_LC_TRACE 47 /* 'trace' */
  327. #define DUK_STRIDX_RAW 48 /* 'raw' */
  328. #define DUK_STRIDX_FMT 49 /* 'fmt' */
  329. #define DUK_STRIDX_CURRENT 50 /* 'current' */
  330. #define DUK_STRIDX_RESUME 51 /* 'resume' */
  331. #define DUK_STRIDX_COMPACT 52 /* 'compact' */
  332. #define DUK_STRIDX_JC 53 /* 'jc' */
  333. #define DUK_STRIDX_JX 54 /* 'jx' */
  334. #define DUK_STRIDX_BASE64 55 /* 'base64' */
  335. #define DUK_STRIDX_HEX 56 /* 'hex' */
  336. #define DUK_STRIDX_DEC 57 /* 'dec' */
  337. #define DUK_STRIDX_ENC 58 /* 'enc' */
  338. #define DUK_STRIDX_FIN 59 /* 'fin' */
  339. #define DUK_STRIDX_GC 60 /* 'gc' */
  340. #define DUK_STRIDX_ACT 61 /* 'act' */
  341. #define DUK_STRIDX_LC_INFO 62 /* 'info' */
  342. #define DUK_STRIDX_VERSION 63 /* 'version' */
  343. #define DUK_STRIDX_ENV 64 /* 'env' */
  344. #define DUK_STRIDX_MOD_LOADED 65 /* 'modLoaded' */
  345. #define DUK_STRIDX_MOD_SEARCH 66 /* 'modSearch' */
  346. #define DUK_STRIDX_ERR_THROW 67 /* 'errThrow' */
  347. #define DUK_STRIDX_ERR_CREATE 68 /* 'errCreate' */
  348. #define DUK_STRIDX_COMPILE 69 /* 'compile' */
  349. #define DUK_STRIDX_INT_REGBASE 70 /* '\x00Regbase' */
  350. #define DUK_STRIDX_INT_THREAD 71 /* '\x00Thread' */
  351. #define DUK_STRIDX_INT_HANDLER 72 /* '\x00Handler' */
  352. #define DUK_STRIDX_INT_FINALIZER 73 /* '\x00Finalizer' */
  353. #define DUK_STRIDX_INT_CALLEE 74 /* '\x00Callee' */
  354. #define DUK_STRIDX_INT_MAP 75 /* '\x00Map' */
  355. #define DUK_STRIDX_INT_ARGS 76 /* '\x00Args' */
  356. #define DUK_STRIDX_INT_THIS 77 /* '\x00This' */
  357. #define DUK_STRIDX_INT_PC2LINE 78 /* '\x00Pc2line' */
  358. #define DUK_STRIDX_INT_SOURCE 79 /* '\x00Source' */
  359. #define DUK_STRIDX_INT_VARENV 80 /* '\x00Varenv' */
  360. #define DUK_STRIDX_INT_LEXENV 81 /* '\x00Lexenv' */
  361. #define DUK_STRIDX_INT_VARMAP 82 /* '\x00Varmap' */
  362. #define DUK_STRIDX_INT_FORMALS 83 /* '\x00Formals' */
  363. #define DUK_STRIDX_INT_BYTECODE 84 /* '\x00Bytecode' */
  364. #define DUK_STRIDX_INT_NEXT 85 /* '\x00Next' */
  365. #define DUK_STRIDX_INT_TARGET 86 /* '\x00Target' */
  366. #define DUK_STRIDX_INT_VALUE 87 /* '\x00Value' */
  367. #define DUK_STRIDX_LC_POINTER 88 /* 'pointer' */
  368. #define DUK_STRIDX_INT_TRACEDATA 89 /* '\x00Tracedata' */
  369. #define DUK_STRIDX_LINE_NUMBER 90 /* 'lineNumber' */
  370. #define DUK_STRIDX_FILE_NAME 91 /* 'fileName' */
  371. #define DUK_STRIDX_PC 92 /* 'pc' */
  372. #define DUK_STRIDX_STACK 93 /* 'stack' */
  373. #define DUK_STRIDX_THROW_TYPE_ERROR 94 /* 'ThrowTypeError' */
  374. #define DUK_STRIDX_DUKTAPE 95 /* 'Duktape' */
  375. #define DUK_STRIDX_SET_FLOAT64 96 /* 'setFloat64' */
  376. #define DUK_STRIDX_SET_FLOAT32 97 /* 'setFloat32' */
  377. #define DUK_STRIDX_SET_UINT32 98 /* 'setUint32' */
  378. #define DUK_STRIDX_SET_INT32 99 /* 'setInt32' */
  379. #define DUK_STRIDX_SET_UINT16 100 /* 'setUint16' */
  380. #define DUK_STRIDX_SET_INT16 101 /* 'setInt16' */
  381. #define DUK_STRIDX_SET_UINT8 102 /* 'setUint8' */
  382. #define DUK_STRIDX_SET_INT8 103 /* 'setInt8' */
  383. #define DUK_STRIDX_GET_FLOAT64 104 /* 'getFloat64' */
  384. #define DUK_STRIDX_GET_FLOAT32 105 /* 'getFloat32' */
  385. #define DUK_STRIDX_GET_UINT32 106 /* 'getUint32' */
  386. #define DUK_STRIDX_GET_INT32 107 /* 'getInt32' */
  387. #define DUK_STRIDX_GET_UINT16 108 /* 'getUint16' */
  388. #define DUK_STRIDX_GET_INT16 109 /* 'getInt16' */
  389. #define DUK_STRIDX_GET_UINT8 110 /* 'getUint8' */
  390. #define DUK_STRIDX_GET_INT8 111 /* 'getInt8' */
  391. #define DUK_STRIDX_SUBARRAY 112 /* 'subarray' */
  392. #define DUK_STRIDX_BYTES_PER_ELEMENT 113 /* 'BYTES_PER_ELEMENT' */
  393. #define DUK_STRIDX_BYTE_OFFSET 114 /* 'byteOffset' */
  394. #define DUK_STRIDX_LC_BUFFER 115 /* 'buffer' */
  395. #define DUK_STRIDX_IS_VIEW 116 /* 'isView' */
  396. #define DUK_STRIDX_DATA 117 /* 'data' */
  397. #define DUK_STRIDX_TYPE 118 /* 'type' */
  398. #define DUK_STRIDX_WRITE_INT_BE 119 /* 'writeIntBE' */
  399. #define DUK_STRIDX_WRITE_INT_LE 120 /* 'writeIntLE' */
  400. #define DUK_STRIDX_WRITE_UINT_BE 121 /* 'writeUIntBE' */
  401. #define DUK_STRIDX_WRITE_UINT_LE 122 /* 'writeUIntLE' */
  402. #define DUK_STRIDX_WRITE_DOUBLE_BE 123 /* 'writeDoubleBE' */
  403. #define DUK_STRIDX_WRITE_DOUBLE_LE 124 /* 'writeDoubleLE' */
  404. #define DUK_STRIDX_WRITE_FLOAT_BE 125 /* 'writeFloatBE' */
  405. #define DUK_STRIDX_WRITE_FLOAT_LE 126 /* 'writeFloatLE' */
  406. #define DUK_STRIDX_WRITE_INT32_BE 127 /* 'writeInt32BE' */
  407. #define DUK_STRIDX_WRITE_INT32_LE 128 /* 'writeInt32LE' */
  408. #define DUK_STRIDX_WRITE_UINT32_BE 129 /* 'writeUInt32BE' */
  409. #define DUK_STRIDX_WRITE_UINT32_LE 130 /* 'writeUInt32LE' */
  410. #define DUK_STRIDX_WRITE_INT16_BE 131 /* 'writeInt16BE' */
  411. #define DUK_STRIDX_WRITE_INT16_LE 132 /* 'writeInt16LE' */
  412. #define DUK_STRIDX_WRITE_UINT16_BE 133 /* 'writeUInt16BE' */
  413. #define DUK_STRIDX_WRITE_UINT16_LE 134 /* 'writeUInt16LE' */
  414. #define DUK_STRIDX_WRITE_INT8 135 /* 'writeInt8' */
  415. #define DUK_STRIDX_WRITE_UINT8 136 /* 'writeUInt8' */
  416. #define DUK_STRIDX_READ_INT_BE 137 /* 'readIntBE' */
  417. #define DUK_STRIDX_READ_INT_LE 138 /* 'readIntLE' */
  418. #define DUK_STRIDX_READ_UINT_BE 139 /* 'readUIntBE' */
  419. #define DUK_STRIDX_READ_UINT_LE 140 /* 'readUIntLE' */
  420. #define DUK_STRIDX_READ_DOUBLE_BE 141 /* 'readDoubleBE' */
  421. #define DUK_STRIDX_READ_DOUBLE_LE 142 /* 'readDoubleLE' */
  422. #define DUK_STRIDX_READ_FLOAT_BE 143 /* 'readFloatBE' */
  423. #define DUK_STRIDX_READ_FLOAT_LE 144 /* 'readFloatLE' */
  424. #define DUK_STRIDX_READ_INT32_BE 145 /* 'readInt32BE' */
  425. #define DUK_STRIDX_READ_INT32_LE 146 /* 'readInt32LE' */
  426. #define DUK_STRIDX_READ_UINT32_BE 147 /* 'readUInt32BE' */
  427. #define DUK_STRIDX_READ_UINT32_LE 148 /* 'readUInt32LE' */
  428. #define DUK_STRIDX_READ_INT16_BE 149 /* 'readInt16BE' */
  429. #define DUK_STRIDX_READ_INT16_LE 150 /* 'readInt16LE' */
  430. #define DUK_STRIDX_READ_UINT16_BE 151 /* 'readUInt16BE' */
  431. #define DUK_STRIDX_READ_UINT16_LE 152 /* 'readUInt16LE' */
  432. #define DUK_STRIDX_READ_INT8 153 /* 'readInt8' */
  433. #define DUK_STRIDX_READ_UINT8 154 /* 'readUInt8' */
  434. #define DUK_STRIDX_COPY 155 /* 'copy' */
  435. #define DUK_STRIDX_EQUALS 156 /* 'equals' */
  436. #define DUK_STRIDX_FILL 157 /* 'fill' */
  437. #define DUK_STRIDX_WRITE 158 /* 'write' */
  438. #define DUK_STRIDX_COMPARE 159 /* 'compare' */
  439. #define DUK_STRIDX_BYTE_LENGTH 160 /* 'byteLength' */
  440. #define DUK_STRIDX_IS_BUFFER 161 /* 'isBuffer' */
  441. #define DUK_STRIDX_IS_ENCODING 162 /* 'isEncoding' */
  442. #define DUK_STRIDX_EXPORTS 163 /* 'exports' */
  443. #define DUK_STRIDX_ID 164 /* 'id' */
  444. #define DUK_STRIDX_REQUIRE 165 /* 'require' */
  445. #define DUK_STRIDX___PROTO__ 166 /* '__proto__' */
  446. #define DUK_STRIDX_SET_PROTOTYPE_OF 167 /* 'setPrototypeOf' */
  447. #define DUK_STRIDX_OWN_KEYS 168 /* 'ownKeys' */
  448. #define DUK_STRIDX_ENUMERATE 169 /* 'enumerate' */
  449. #define DUK_STRIDX_DELETE_PROPERTY 170 /* 'deleteProperty' */
  450. #define DUK_STRIDX_HAS 171 /* 'has' */
  451. #define DUK_STRIDX_PROXY 172 /* 'Proxy' */
  452. #define DUK_STRIDX_CALLEE 173 /* 'callee' */
  453. #define DUK_STRIDX_INVALID_DATE 174 /* 'Invalid Date' */
  454. #define DUK_STRIDX_BRACKETED_ELLIPSIS 175 /* '[...]' */
  455. #define DUK_STRIDX_NEWLINE_TAB 176 /* '\n\t' */
  456. #define DUK_STRIDX_SPACE 177 /* ' ' */
  457. #define DUK_STRIDX_COMMA 178 /* ',' */
  458. #define DUK_STRIDX_MINUS_ZERO 179 /* '-0' */
  459. #define DUK_STRIDX_PLUS_ZERO 180 /* '+0' */
  460. #define DUK_STRIDX_ZERO 181 /* '0' */
  461. #define DUK_STRIDX_MINUS_INFINITY 182 /* '-Infinity' */
  462. #define DUK_STRIDX_PLUS_INFINITY 183 /* '+Infinity' */
  463. #define DUK_STRIDX_INFINITY 184 /* 'Infinity' */
  464. #define DUK_STRIDX_LC_OBJECT 185 /* 'object' */
  465. #define DUK_STRIDX_LC_STRING 186 /* 'string' */
  466. #define DUK_STRIDX_LC_NUMBER 187 /* 'number' */
  467. #define DUK_STRIDX_LC_BOOLEAN 188 /* 'boolean' */
  468. #define DUK_STRIDX_LC_UNDEFINED 189 /* 'undefined' */
  469. #define DUK_STRIDX_STRINGIFY 190 /* 'stringify' */
  470. #define DUK_STRIDX_TAN 191 /* 'tan' */
  471. #define DUK_STRIDX_SQRT 192 /* 'sqrt' */
  472. #define DUK_STRIDX_SIN 193 /* 'sin' */
  473. #define DUK_STRIDX_ROUND 194 /* 'round' */
  474. #define DUK_STRIDX_RANDOM 195 /* 'random' */
  475. #define DUK_STRIDX_POW 196 /* 'pow' */
  476. #define DUK_STRIDX_MIN 197 /* 'min' */
  477. #define DUK_STRIDX_MAX 198 /* 'max' */
  478. #define DUK_STRIDX_LOG 199 /* 'log' */
  479. #define DUK_STRIDX_FLOOR 200 /* 'floor' */
  480. #define DUK_STRIDX_EXP 201 /* 'exp' */
  481. #define DUK_STRIDX_COS 202 /* 'cos' */
  482. #define DUK_STRIDX_CEIL 203 /* 'ceil' */
  483. #define DUK_STRIDX_ATAN2 204 /* 'atan2' */
  484. #define DUK_STRIDX_ATAN 205 /* 'atan' */
  485. #define DUK_STRIDX_ASIN 206 /* 'asin' */
  486. #define DUK_STRIDX_ACOS 207 /* 'acos' */
  487. #define DUK_STRIDX_ABS 208 /* 'abs' */
  488. #define DUK_STRIDX_SQRT2 209 /* 'SQRT2' */
  489. #define DUK_STRIDX_SQRT1_2 210 /* 'SQRT1_2' */
  490. #define DUK_STRIDX_PI 211 /* 'PI' */
  491. #define DUK_STRIDX_LOG10E 212 /* 'LOG10E' */
  492. #define DUK_STRIDX_LOG2E 213 /* 'LOG2E' */
  493. #define DUK_STRIDX_LN2 214 /* 'LN2' */
  494. #define DUK_STRIDX_LN10 215 /* 'LN10' */
  495. #define DUK_STRIDX_E 216 /* 'E' */
  496. #define DUK_STRIDX_MESSAGE 217 /* 'message' */
  497. #define DUK_STRIDX_NAME 218 /* 'name' */
  498. #define DUK_STRIDX_INPUT 219 /* 'input' */
  499. #define DUK_STRIDX_INDEX 220 /* 'index' */
  500. #define DUK_STRIDX_ESCAPED_EMPTY_REGEXP 221 /* '(?:)' */
  501. #define DUK_STRIDX_LAST_INDEX 222 /* 'lastIndex' */
  502. #define DUK_STRIDX_MULTILINE 223 /* 'multiline' */
  503. #define DUK_STRIDX_IGNORE_CASE 224 /* 'ignoreCase' */
  504. #define DUK_STRIDX_SOURCE 225 /* 'source' */
  505. #define DUK_STRIDX_TEST 226 /* 'test' */
  506. #define DUK_STRIDX_EXEC 227 /* 'exec' */
  507. #define DUK_STRIDX_TO_GMT_STRING 228 /* 'toGMTString' */
  508. #define DUK_STRIDX_SET_YEAR 229 /* 'setYear' */
  509. #define DUK_STRIDX_GET_YEAR 230 /* 'getYear' */
  510. #define DUK_STRIDX_TO_JSON 231 /* 'toJSON' */
  511. #define DUK_STRIDX_TO_ISO_STRING 232 /* 'toISOString' */
  512. #define DUK_STRIDX_TO_UTC_STRING 233 /* 'toUTCString' */
  513. #define DUK_STRIDX_SET_UTC_FULL_YEAR 234 /* 'setUTCFullYear' */
  514. #define DUK_STRIDX_SET_FULL_YEAR 235 /* 'setFullYear' */
  515. #define DUK_STRIDX_SET_UTC_MONTH 236 /* 'setUTCMonth' */
  516. #define DUK_STRIDX_SET_MONTH 237 /* 'setMonth' */
  517. #define DUK_STRIDX_SET_UTC_DATE 238 /* 'setUTCDate' */
  518. #define DUK_STRIDX_SET_DATE 239 /* 'setDate' */
  519. #define DUK_STRIDX_SET_UTC_HOURS 240 /* 'setUTCHours' */
  520. #define DUK_STRIDX_SET_HOURS 241 /* 'setHours' */
  521. #define DUK_STRIDX_SET_UTC_MINUTES 242 /* 'setUTCMinutes' */
  522. #define DUK_STRIDX_SET_MINUTES 243 /* 'setMinutes' */
  523. #define DUK_STRIDX_SET_UTC_SECONDS 244 /* 'setUTCSeconds' */
  524. #define DUK_STRIDX_SET_SECONDS 245 /* 'setSeconds' */
  525. #define DUK_STRIDX_SET_UTC_MILLISECONDS 246 /* 'setUTCMilliseconds' */
  526. #define DUK_STRIDX_SET_MILLISECONDS 247 /* 'setMilliseconds' */
  527. #define DUK_STRIDX_SET_TIME 248 /* 'setTime' */
  528. #define DUK_STRIDX_GET_TIMEZONE_OFFSET 249 /* 'getTimezoneOffset' */
  529. #define DUK_STRIDX_GET_UTC_MILLISECONDS 250 /* 'getUTCMilliseconds' */
  530. #define DUK_STRIDX_GET_MILLISECONDS 251 /* 'getMilliseconds' */
  531. #define DUK_STRIDX_GET_UTC_SECONDS 252 /* 'getUTCSeconds' */
  532. #define DUK_STRIDX_GET_SECONDS 253 /* 'getSeconds' */
  533. #define DUK_STRIDX_GET_UTC_MINUTES 254 /* 'getUTCMinutes' */
  534. #define DUK_STRIDX_GET_MINUTES 255 /* 'getMinutes' */
  535. #define DUK_STRIDX_GET_UTC_HOURS 256 /* 'getUTCHours' */
  536. #define DUK_STRIDX_GET_HOURS 257 /* 'getHours' */
  537. #define DUK_STRIDX_GET_UTC_DAY 258 /* 'getUTCDay' */
  538. #define DUK_STRIDX_GET_DAY 259 /* 'getDay' */
  539. #define DUK_STRIDX_GET_UTC_DATE 260 /* 'getUTCDate' */
  540. #define DUK_STRIDX_GET_DATE 261 /* 'getDate' */
  541. #define DUK_STRIDX_GET_UTC_MONTH 262 /* 'getUTCMonth' */
  542. #define DUK_STRIDX_GET_MONTH 263 /* 'getMonth' */
  543. #define DUK_STRIDX_GET_UTC_FULL_YEAR 264 /* 'getUTCFullYear' */
  544. #define DUK_STRIDX_GET_FULL_YEAR 265 /* 'getFullYear' */
  545. #define DUK_STRIDX_GET_TIME 266 /* 'getTime' */
  546. #define DUK_STRIDX_TO_LOCALE_TIME_STRING 267 /* 'toLocaleTimeString' */
  547. #define DUK_STRIDX_TO_LOCALE_DATE_STRING 268 /* 'toLocaleDateString' */
  548. #define DUK_STRIDX_TO_TIME_STRING 269 /* 'toTimeString' */
  549. #define DUK_STRIDX_TO_DATE_STRING 270 /* 'toDateString' */
  550. #define DUK_STRIDX_NOW 271 /* 'now' */
  551. #define DUK_STRIDX_UTC 272 /* 'UTC' */
  552. #define DUK_STRIDX_PARSE 273 /* 'parse' */
  553. #define DUK_STRIDX_TO_PRECISION 274 /* 'toPrecision' */
  554. #define DUK_STRIDX_TO_EXPONENTIAL 275 /* 'toExponential' */
  555. #define DUK_STRIDX_TO_FIXED 276 /* 'toFixed' */
  556. #define DUK_STRIDX_POSITIVE_INFINITY 277 /* 'POSITIVE_INFINITY' */
  557. #define DUK_STRIDX_NEGATIVE_INFINITY 278 /* 'NEGATIVE_INFINITY' */
  558. #define DUK_STRIDX_NAN 279 /* 'NaN' */
  559. #define DUK_STRIDX_MIN_VALUE 280 /* 'MIN_VALUE' */
  560. #define DUK_STRIDX_MAX_VALUE 281 /* 'MAX_VALUE' */
  561. #define DUK_STRIDX_SUBSTR 282 /* 'substr' */
  562. #define DUK_STRIDX_TRIM 283 /* 'trim' */
  563. #define DUK_STRIDX_TO_LOCALE_UPPER_CASE 284 /* 'toLocaleUpperCase' */
  564. #define DUK_STRIDX_TO_UPPER_CASE 285 /* 'toUpperCase' */
  565. #define DUK_STRIDX_TO_LOCALE_LOWER_CASE 286 /* 'toLocaleLowerCase' */
  566. #define DUK_STRIDX_TO_LOWER_CASE 287 /* 'toLowerCase' */
  567. #define DUK_STRIDX_SUBSTRING 288 /* 'substring' */
  568. #define DUK_STRIDX_SPLIT 289 /* 'split' */
  569. #define DUK_STRIDX_SEARCH 290 /* 'search' */
  570. #define DUK_STRIDX_REPLACE 291 /* 'replace' */
  571. #define DUK_STRIDX_MATCH 292 /* 'match' */
  572. #define DUK_STRIDX_LOCALE_COMPARE 293 /* 'localeCompare' */
  573. #define DUK_STRIDX_CHAR_CODE_AT 294 /* 'charCodeAt' */
  574. #define DUK_STRIDX_CHAR_AT 295 /* 'charAt' */
  575. #define DUK_STRIDX_FROM_CHAR_CODE 296 /* 'fromCharCode' */
  576. #define DUK_STRIDX_REDUCE_RIGHT 297 /* 'reduceRight' */
  577. #define DUK_STRIDX_REDUCE 298 /* 'reduce' */
  578. #define DUK_STRIDX_FILTER 299 /* 'filter' */
  579. #define DUK_STRIDX_MAP 300 /* 'map' */
  580. #define DUK_STRIDX_FOR_EACH 301 /* 'forEach' */
  581. #define DUK_STRIDX_SOME 302 /* 'some' */
  582. #define DUK_STRIDX_EVERY 303 /* 'every' */
  583. #define DUK_STRIDX_LAST_INDEX_OF 304 /* 'lastIndexOf' */
  584. #define DUK_STRIDX_INDEX_OF 305 /* 'indexOf' */
  585. #define DUK_STRIDX_UNSHIFT 306 /* 'unshift' */
  586. #define DUK_STRIDX_SPLICE 307 /* 'splice' */
  587. #define DUK_STRIDX_SORT 308 /* 'sort' */
  588. #define DUK_STRIDX_SLICE 309 /* 'slice' */
  589. #define DUK_STRIDX_SHIFT 310 /* 'shift' */
  590. #define DUK_STRIDX_REVERSE 311 /* 'reverse' */
  591. #define DUK_STRIDX_PUSH 312 /* 'push' */
  592. #define DUK_STRIDX_POP 313 /* 'pop' */
  593. #define DUK_STRIDX_JOIN 314 /* 'join' */
  594. #define DUK_STRIDX_CONCAT 315 /* 'concat' */
  595. #define DUK_STRIDX_IS_ARRAY 316 /* 'isArray' */
  596. #define DUK_STRIDX_LC_ARGUMENTS 317 /* 'arguments' */
  597. #define DUK_STRIDX_CALLER 318 /* 'caller' */
  598. #define DUK_STRIDX_BIND 319 /* 'bind' */
  599. #define DUK_STRIDX_CALL 320 /* 'call' */
  600. #define DUK_STRIDX_APPLY 321 /* 'apply' */
  601. #define DUK_STRIDX_PROPERTY_IS_ENUMERABLE 322 /* 'propertyIsEnumerable' */
  602. #define DUK_STRIDX_IS_PROTOTYPE_OF 323 /* 'isPrototypeOf' */
  603. #define DUK_STRIDX_HAS_OWN_PROPERTY 324 /* 'hasOwnProperty' */
  604. #define DUK_STRIDX_VALUE_OF 325 /* 'valueOf' */
  605. #define DUK_STRIDX_TO_LOCALE_STRING 326 /* 'toLocaleString' */
  606. #define DUK_STRIDX_TO_STRING 327 /* 'toString' */
  607. #define DUK_STRIDX_CONSTRUCTOR 328 /* 'constructor' */
  608. #define DUK_STRIDX_SET 329 /* 'set' */
  609. #define DUK_STRIDX_GET 330 /* 'get' */
  610. #define DUK_STRIDX_ENUMERABLE 331 /* 'enumerable' */
  611. #define DUK_STRIDX_CONFIGURABLE 332 /* 'configurable' */
  612. #define DUK_STRIDX_WRITABLE 333 /* 'writable' */
  613. #define DUK_STRIDX_VALUE 334 /* 'value' */
  614. #define DUK_STRIDX_KEYS 335 /* 'keys' */
  615. #define DUK_STRIDX_IS_EXTENSIBLE 336 /* 'isExtensible' */
  616. #define DUK_STRIDX_IS_FROZEN 337 /* 'isFrozen' */
  617. #define DUK_STRIDX_IS_SEALED 338 /* 'isSealed' */
  618. #define DUK_STRIDX_PREVENT_EXTENSIONS 339 /* 'preventExtensions' */
  619. #define DUK_STRIDX_FREEZE 340 /* 'freeze' */
  620. #define DUK_STRIDX_SEAL 341 /* 'seal' */
  621. #define DUK_STRIDX_DEFINE_PROPERTIES 342 /* 'defineProperties' */
  622. #define DUK_STRIDX_DEFINE_PROPERTY 343 /* 'defineProperty' */
  623. #define DUK_STRIDX_CREATE 344 /* 'create' */
  624. #define DUK_STRIDX_GET_OWN_PROPERTY_NAMES 345 /* 'getOwnPropertyNames' */
  625. #define DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR 346 /* 'getOwnPropertyDescriptor' */
  626. #define DUK_STRIDX_GET_PROTOTYPE_OF 347 /* 'getPrototypeOf' */
  627. #define DUK_STRIDX_PROTOTYPE 348 /* 'prototype' */
  628. #define DUK_STRIDX_LENGTH 349 /* 'length' */
  629. #define DUK_STRIDX_ALERT 350 /* 'alert' */
  630. #define DUK_STRIDX_PRINT 351 /* 'print' */
  631. #define DUK_STRIDX_UNESCAPE 352 /* 'unescape' */
  632. #define DUK_STRIDX_ESCAPE 353 /* 'escape' */
  633. #define DUK_STRIDX_ENCODE_URI_COMPONENT 354 /* 'encodeURIComponent' */
  634. #define DUK_STRIDX_ENCODE_URI 355 /* 'encodeURI' */
  635. #define DUK_STRIDX_DECODE_URI_COMPONENT 356 /* 'decodeURIComponent' */
  636. #define DUK_STRIDX_DECODE_URI 357 /* 'decodeURI' */
  637. #define DUK_STRIDX_IS_FINITE 358 /* 'isFinite' */
  638. #define DUK_STRIDX_IS_NAN 359 /* 'isNaN' */
  639. #define DUK_STRIDX_PARSE_FLOAT 360 /* 'parseFloat' */
  640. #define DUK_STRIDX_PARSE_INT 361 /* 'parseInt' */
  641. #define DUK_STRIDX_EVAL 362 /* 'eval' */
  642. #define DUK_STRIDX_URI_ERROR 363 /* 'URIError' */
  643. #define DUK_STRIDX_TYPE_ERROR 364 /* 'TypeError' */
  644. #define DUK_STRIDX_SYNTAX_ERROR 365 /* 'SyntaxError' */
  645. #define DUK_STRIDX_REFERENCE_ERROR 366 /* 'ReferenceError' */
  646. #define DUK_STRIDX_RANGE_ERROR 367 /* 'RangeError' */
  647. #define DUK_STRIDX_EVAL_ERROR 368 /* 'EvalError' */
  648. #define DUK_STRIDX_BREAK 369 /* 'break' */
  649. #define DUK_STRIDX_CASE 370 /* 'case' */
  650. #define DUK_STRIDX_CATCH 371 /* 'catch' */
  651. #define DUK_STRIDX_CONTINUE 372 /* 'continue' */
  652. #define DUK_STRIDX_DEBUGGER 373 /* 'debugger' */
  653. #define DUK_STRIDX_DEFAULT 374 /* 'default' */
  654. #define DUK_STRIDX_DELETE 375 /* 'delete' */
  655. #define DUK_STRIDX_DO 376 /* 'do' */
  656. #define DUK_STRIDX_ELSE 377 /* 'else' */
  657. #define DUK_STRIDX_FINALLY 378 /* 'finally' */
  658. #define DUK_STRIDX_FOR 379 /* 'for' */
  659. #define DUK_STRIDX_LC_FUNCTION 380 /* 'function' */
  660. #define DUK_STRIDX_IF 381 /* 'if' */
  661. #define DUK_STRIDX_IN 382 /* 'in' */
  662. #define DUK_STRIDX_INSTANCEOF 383 /* 'instanceof' */
  663. #define DUK_STRIDX_NEW 384 /* 'new' */
  664. #define DUK_STRIDX_RETURN 385 /* 'return' */
  665. #define DUK_STRIDX_SWITCH 386 /* 'switch' */
  666. #define DUK_STRIDX_THIS 387 /* 'this' */
  667. #define DUK_STRIDX_THROW 388 /* 'throw' */
  668. #define DUK_STRIDX_TRY 389 /* 'try' */
  669. #define DUK_STRIDX_TYPEOF 390 /* 'typeof' */
  670. #define DUK_STRIDX_VAR 391 /* 'var' */
  671. #define DUK_STRIDX_VOID 392 /* 'void' */
  672. #define DUK_STRIDX_WHILE 393 /* 'while' */
  673. #define DUK_STRIDX_WITH 394 /* 'with' */
  674. #define DUK_STRIDX_CLASS 395 /* 'class' */
  675. #define DUK_STRIDX_CONST 396 /* 'const' */
  676. #define DUK_STRIDX_ENUM 397 /* 'enum' */
  677. #define DUK_STRIDX_EXPORT 398 /* 'export' */
  678. #define DUK_STRIDX_EXTENDS 399 /* 'extends' */
  679. #define DUK_STRIDX_IMPORT 400 /* 'import' */
  680. #define DUK_STRIDX_SUPER 401 /* 'super' */
  681. #define DUK_STRIDX_LC_NULL 402 /* 'null' */
  682. #define DUK_STRIDX_TRUE 403 /* 'true' */
  683. #define DUK_STRIDX_FALSE 404 /* 'false' */
  684. #define DUK_STRIDX_IMPLEMENTS 405 /* 'implements' */
  685. #define DUK_STRIDX_INTERFACE 406 /* 'interface' */
  686. #define DUK_STRIDX_LET 407 /* 'let' */
  687. #define DUK_STRIDX_PACKAGE 408 /* 'package' */
  688. #define DUK_STRIDX_PRIVATE 409 /* 'private' */
  689. #define DUK_STRIDX_PROTECTED 410 /* 'protected' */
  690. #define DUK_STRIDX_PUBLIC 411 /* 'public' */
  691. #define DUK_STRIDX_STATIC 412 /* 'static' */
  692. #define DUK_STRIDX_YIELD 413 /* 'yield' */
  693. #define DUK_HEAP_STRING_UC_LOGGER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_LOGGER)
  694. #define DUK_HTHREAD_STRING_UC_LOGGER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_LOGGER)
  695. #define DUK_HEAP_STRING_UC_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_THREAD)
  696. #define DUK_HTHREAD_STRING_UC_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_THREAD)
  697. #define DUK_HEAP_STRING_UC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_POINTER)
  698. #define DUK_HTHREAD_STRING_UC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_POINTER)
  699. #define DUK_HEAP_STRING_DEC_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEC_ENV)
  700. #define DUK_HTHREAD_STRING_DEC_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEC_ENV)
  701. #define DUK_HEAP_STRING_OBJ_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OBJ_ENV)
  702. #define DUK_HTHREAD_STRING_OBJ_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OBJ_ENV)
  703. #define DUK_HEAP_STRING_FLOAT64_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT64_ARRAY)
  704. #define DUK_HTHREAD_STRING_FLOAT64_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT64_ARRAY)
  705. #define DUK_HEAP_STRING_FLOAT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT32_ARRAY)
  706. #define DUK_HTHREAD_STRING_FLOAT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT32_ARRAY)
  707. #define DUK_HEAP_STRING_UINT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT32_ARRAY)
  708. #define DUK_HTHREAD_STRING_UINT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT32_ARRAY)
  709. #define DUK_HEAP_STRING_INT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT32_ARRAY)
  710. #define DUK_HTHREAD_STRING_INT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT32_ARRAY)
  711. #define DUK_HEAP_STRING_UINT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT16_ARRAY)
  712. #define DUK_HTHREAD_STRING_UINT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT16_ARRAY)
  713. #define DUK_HEAP_STRING_INT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT16_ARRAY)
  714. #define DUK_HTHREAD_STRING_INT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT16_ARRAY)
  715. #define DUK_HEAP_STRING_UINT8_CLAMPED_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_CLAMPED_ARRAY)
  716. #define DUK_HTHREAD_STRING_UINT8_CLAMPED_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_CLAMPED_ARRAY)
  717. #define DUK_HEAP_STRING_UINT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_ARRAY)
  718. #define DUK_HTHREAD_STRING_UINT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_ARRAY)
  719. #define DUK_HEAP_STRING_INT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT8_ARRAY)
  720. #define DUK_HTHREAD_STRING_INT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT8_ARRAY)
  721. #define DUK_HEAP_STRING_DATA_VIEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA_VIEW)
  722. #define DUK_HTHREAD_STRING_DATA_VIEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA_VIEW)
  723. #define DUK_HEAP_STRING_ARRAY_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY_BUFFER)
  724. #define DUK_HTHREAD_STRING_ARRAY_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY_BUFFER)
  725. #define DUK_HEAP_STRING_UC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BUFFER)
  726. #define DUK_HTHREAD_STRING_UC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BUFFER)
  727. #define DUK_HEAP_STRING_EMPTY_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EMPTY_STRING)
  728. #define DUK_HTHREAD_STRING_EMPTY_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EMPTY_STRING)
  729. #define DUK_HEAP_STRING_GLOBAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GLOBAL)
  730. #define DUK_HTHREAD_STRING_GLOBAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GLOBAL)
  731. #define DUK_HEAP_STRING_UC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ARGUMENTS)
  732. #define DUK_HTHREAD_STRING_UC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ARGUMENTS)
  733. #define DUK_HEAP_STRING_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON)
  734. #define DUK_HTHREAD_STRING_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON)
  735. #define DUK_HEAP_STRING_MATH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MATH)
  736. #define DUK_HTHREAD_STRING_MATH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MATH)
  737. #define DUK_HEAP_STRING_UC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ERROR)
  738. #define DUK_HTHREAD_STRING_UC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ERROR)
  739. #define DUK_HEAP_STRING_REG_EXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REG_EXP)
  740. #define DUK_HTHREAD_STRING_REG_EXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REG_EXP)
  741. #define DUK_HEAP_STRING_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATE)
  742. #define DUK_HTHREAD_STRING_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATE)
  743. #define DUK_HEAP_STRING_UC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NUMBER)
  744. #define DUK_HTHREAD_STRING_UC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NUMBER)
  745. #define DUK_HEAP_STRING_UC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BOOLEAN)
  746. #define DUK_HTHREAD_STRING_UC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BOOLEAN)
  747. #define DUK_HEAP_STRING_UC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_STRING)
  748. #define DUK_HTHREAD_STRING_UC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_STRING)
  749. #define DUK_HEAP_STRING_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY)
  750. #define DUK_HTHREAD_STRING_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY)
  751. #define DUK_HEAP_STRING_UC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_FUNCTION)
  752. #define DUK_HTHREAD_STRING_UC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_FUNCTION)
  753. #define DUK_HEAP_STRING_UC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_OBJECT)
  754. #define DUK_HTHREAD_STRING_UC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_OBJECT)
  755. #define DUK_HEAP_STRING_UC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NULL)
  756. #define DUK_HTHREAD_STRING_UC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NULL)
  757. #define DUK_HEAP_STRING_UC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_UNDEFINED)
  758. #define DUK_HTHREAD_STRING_UC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_UNDEFINED)
  759. #define DUK_HEAP_STRING_JSON_EXT_FUNCTION2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION2)
  760. #define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION2)
  761. #define DUK_HEAP_STRING_JSON_EXT_FUNCTION1(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION1)
  762. #define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION1(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION1)
  763. #define DUK_HEAP_STRING_JSON_EXT_NEGINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NEGINF)
  764. #define DUK_HTHREAD_STRING_JSON_EXT_NEGINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NEGINF)
  765. #define DUK_HEAP_STRING_JSON_EXT_POSINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_POSINF)
  766. #define DUK_HTHREAD_STRING_JSON_EXT_POSINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_POSINF)
  767. #define DUK_HEAP_STRING_JSON_EXT_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NAN)
  768. #define DUK_HTHREAD_STRING_JSON_EXT_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NAN)
  769. #define DUK_HEAP_STRING_JSON_EXT_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_UNDEFINED)
  770. #define DUK_HTHREAD_STRING_JSON_EXT_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_UNDEFINED)
  771. #define DUK_HEAP_STRING_TO_LOG_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOG_STRING)
  772. #define DUK_HTHREAD_STRING_TO_LOG_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOG_STRING)
  773. #define DUK_HEAP_STRING_CLOG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLOG)
  774. #define DUK_HTHREAD_STRING_CLOG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLOG)
  775. #define DUK_HEAP_STRING_LC_L(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_L)
  776. #define DUK_HTHREAD_STRING_LC_L(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_L)
  777. #define DUK_HEAP_STRING_LC_N(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_N)
  778. #define DUK_HTHREAD_STRING_LC_N(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_N)
  779. #define DUK_HEAP_STRING_LC_FATAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FATAL)
  780. #define DUK_HTHREAD_STRING_LC_FATAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FATAL)
  781. #define DUK_HEAP_STRING_LC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ERROR)
  782. #define DUK_HTHREAD_STRING_LC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ERROR)
  783. #define DUK_HEAP_STRING_LC_WARN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_WARN)
  784. #define DUK_HTHREAD_STRING_LC_WARN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_WARN)
  785. #define DUK_HEAP_STRING_LC_DEBUG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_DEBUG)
  786. #define DUK_HTHREAD_STRING_LC_DEBUG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_DEBUG)
  787. #define DUK_HEAP_STRING_LC_TRACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_TRACE)
  788. #define DUK_HTHREAD_STRING_LC_TRACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_TRACE)
  789. #define DUK_HEAP_STRING_RAW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RAW)
  790. #define DUK_HTHREAD_STRING_RAW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RAW)
  791. #define DUK_HEAP_STRING_FMT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FMT)
  792. #define DUK_HTHREAD_STRING_FMT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FMT)
  793. #define DUK_HEAP_STRING_CURRENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CURRENT)
  794. #define DUK_HTHREAD_STRING_CURRENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CURRENT)
  795. #define DUK_HEAP_STRING_RESUME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RESUME)
  796. #define DUK_HTHREAD_STRING_RESUME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RESUME)
  797. #define DUK_HEAP_STRING_COMPACT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPACT)
  798. #define DUK_HTHREAD_STRING_COMPACT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPACT)
  799. #define DUK_HEAP_STRING_JC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JC)
  800. #define DUK_HTHREAD_STRING_JC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JC)
  801. #define DUK_HEAP_STRING_JX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JX)
  802. #define DUK_HTHREAD_STRING_JX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JX)
  803. #define DUK_HEAP_STRING_BASE64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BASE64)
  804. #define DUK_HTHREAD_STRING_BASE64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BASE64)
  805. #define DUK_HEAP_STRING_HEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HEX)
  806. #define DUK_HTHREAD_STRING_HEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HEX)
  807. #define DUK_HEAP_STRING_DEC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEC)
  808. #define DUK_HTHREAD_STRING_DEC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEC)
  809. #define DUK_HEAP_STRING_ENC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENC)
  810. #define DUK_HTHREAD_STRING_ENC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENC)
  811. #define DUK_HEAP_STRING_FIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FIN)
  812. #define DUK_HTHREAD_STRING_FIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FIN)
  813. #define DUK_HEAP_STRING_GC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GC)
  814. #define DUK_HTHREAD_STRING_GC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GC)
  815. #define DUK_HEAP_STRING_ACT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ACT)
  816. #define DUK_HTHREAD_STRING_ACT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ACT)
  817. #define DUK_HEAP_STRING_LC_INFO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_INFO)
  818. #define DUK_HTHREAD_STRING_LC_INFO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_INFO)
  819. #define DUK_HEAP_STRING_VERSION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VERSION)
  820. #define DUK_HTHREAD_STRING_VERSION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VERSION)
  821. #define DUK_HEAP_STRING_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENV)
  822. #define DUK_HTHREAD_STRING_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENV)
  823. #define DUK_HEAP_STRING_MOD_LOADED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MOD_LOADED)
  824. #define DUK_HTHREAD_STRING_MOD_LOADED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MOD_LOADED)
  825. #define DUK_HEAP_STRING_MOD_SEARCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MOD_SEARCH)
  826. #define DUK_HTHREAD_STRING_MOD_SEARCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MOD_SEARCH)
  827. #define DUK_HEAP_STRING_ERR_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_THROW)
  828. #define DUK_HTHREAD_STRING_ERR_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_THROW)
  829. #define DUK_HEAP_STRING_ERR_CREATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_CREATE)
  830. #define DUK_HTHREAD_STRING_ERR_CREATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_CREATE)
  831. #define DUK_HEAP_STRING_COMPILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPILE)
  832. #define DUK_HTHREAD_STRING_COMPILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPILE)
  833. #define DUK_HEAP_STRING_INT_REGBASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_REGBASE)
  834. #define DUK_HTHREAD_STRING_INT_REGBASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_REGBASE)
  835. #define DUK_HEAP_STRING_INT_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_THREAD)
  836. #define DUK_HTHREAD_STRING_INT_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_THREAD)
  837. #define DUK_HEAP_STRING_INT_HANDLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_HANDLER)
  838. #define DUK_HTHREAD_STRING_INT_HANDLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_HANDLER)
  839. #define DUK_HEAP_STRING_INT_FINALIZER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FINALIZER)
  840. #define DUK_HTHREAD_STRING_INT_FINALIZER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FINALIZER)
  841. #define DUK_HEAP_STRING_INT_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_CALLEE)
  842. #define DUK_HTHREAD_STRING_INT_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_CALLEE)
  843. #define DUK_HEAP_STRING_INT_MAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_MAP)
  844. #define DUK_HTHREAD_STRING_INT_MAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_MAP)
  845. #define DUK_HEAP_STRING_INT_ARGS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_ARGS)
  846. #define DUK_HTHREAD_STRING_INT_ARGS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_ARGS)
  847. #define DUK_HEAP_STRING_INT_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_THIS)
  848. #define DUK_HTHREAD_STRING_INT_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_THIS)
  849. #define DUK_HEAP_STRING_INT_PC2LINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_PC2LINE)
  850. #define DUK_HTHREAD_STRING_INT_PC2LINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_PC2LINE)
  851. #define DUK_HEAP_STRING_INT_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_SOURCE)
  852. #define DUK_HTHREAD_STRING_INT_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_SOURCE)
  853. #define DUK_HEAP_STRING_INT_VARENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARENV)
  854. #define DUK_HTHREAD_STRING_INT_VARENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARENV)
  855. #define DUK_HEAP_STRING_INT_LEXENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_LEXENV)
  856. #define DUK_HTHREAD_STRING_INT_LEXENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_LEXENV)
  857. #define DUK_HEAP_STRING_INT_VARMAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARMAP)
  858. #define DUK_HTHREAD_STRING_INT_VARMAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARMAP)
  859. #define DUK_HEAP_STRING_INT_FORMALS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FORMALS)
  860. #define DUK_HTHREAD_STRING_INT_FORMALS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FORMALS)
  861. #define DUK_HEAP_STRING_INT_BYTECODE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_BYTECODE)
  862. #define DUK_HTHREAD_STRING_INT_BYTECODE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_BYTECODE)
  863. #define DUK_HEAP_STRING_INT_NEXT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_NEXT)
  864. #define DUK_HTHREAD_STRING_INT_NEXT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_NEXT)
  865. #define DUK_HEAP_STRING_INT_TARGET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TARGET)
  866. #define DUK_HTHREAD_STRING_INT_TARGET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TARGET)
  867. #define DUK_HEAP_STRING_INT_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VALUE)
  868. #define DUK_HTHREAD_STRING_INT_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VALUE)
  869. #define DUK_HEAP_STRING_LC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_POINTER)
  870. #define DUK_HTHREAD_STRING_LC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_POINTER)
  871. #define DUK_HEAP_STRING_INT_TRACEDATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TRACEDATA)
  872. #define DUK_HTHREAD_STRING_INT_TRACEDATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TRACEDATA)
  873. #define DUK_HEAP_STRING_LINE_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LINE_NUMBER)
  874. #define DUK_HTHREAD_STRING_LINE_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LINE_NUMBER)
  875. #define DUK_HEAP_STRING_FILE_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILE_NAME)
  876. #define DUK_HTHREAD_STRING_FILE_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILE_NAME)
  877. #define DUK_HEAP_STRING_PC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PC)
  878. #define DUK_HTHREAD_STRING_PC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PC)
  879. #define DUK_HEAP_STRING_STACK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STACK)
  880. #define DUK_HTHREAD_STRING_STACK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STACK)
  881. #define DUK_HEAP_STRING_THROW_TYPE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THROW_TYPE_ERROR)
  882. #define DUK_HTHREAD_STRING_THROW_TYPE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THROW_TYPE_ERROR)
  883. #define DUK_HEAP_STRING_DUKTAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DUKTAPE)
  884. #define DUK_HTHREAD_STRING_DUKTAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DUKTAPE)
  885. #define DUK_HEAP_STRING_SET_FLOAT64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FLOAT64)
  886. #define DUK_HTHREAD_STRING_SET_FLOAT64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FLOAT64)
  887. #define DUK_HEAP_STRING_SET_FLOAT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FLOAT32)
  888. #define DUK_HTHREAD_STRING_SET_FLOAT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FLOAT32)
  889. #define DUK_HEAP_STRING_SET_UINT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT32)
  890. #define DUK_HTHREAD_STRING_SET_UINT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT32)
  891. #define DUK_HEAP_STRING_SET_INT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT32)
  892. #define DUK_HTHREAD_STRING_SET_INT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT32)
  893. #define DUK_HEAP_STRING_SET_UINT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT16)
  894. #define DUK_HTHREAD_STRING_SET_UINT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT16)
  895. #define DUK_HEAP_STRING_SET_INT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT16)
  896. #define DUK_HTHREAD_STRING_SET_INT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT16)
  897. #define DUK_HEAP_STRING_SET_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT8)
  898. #define DUK_HTHREAD_STRING_SET_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT8)
  899. #define DUK_HEAP_STRING_SET_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT8)
  900. #define DUK_HTHREAD_STRING_SET_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT8)
  901. #define DUK_HEAP_STRING_GET_FLOAT64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FLOAT64)
  902. #define DUK_HTHREAD_STRING_GET_FLOAT64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FLOAT64)
  903. #define DUK_HEAP_STRING_GET_FLOAT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FLOAT32)
  904. #define DUK_HTHREAD_STRING_GET_FLOAT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FLOAT32)
  905. #define DUK_HEAP_STRING_GET_UINT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT32)
  906. #define DUK_HTHREAD_STRING_GET_UINT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT32)
  907. #define DUK_HEAP_STRING_GET_INT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT32)
  908. #define DUK_HTHREAD_STRING_GET_INT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT32)
  909. #define DUK_HEAP_STRING_GET_UINT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT16)
  910. #define DUK_HTHREAD_STRING_GET_UINT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT16)
  911. #define DUK_HEAP_STRING_GET_INT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT16)
  912. #define DUK_HTHREAD_STRING_GET_INT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT16)
  913. #define DUK_HEAP_STRING_GET_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT8)
  914. #define DUK_HTHREAD_STRING_GET_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT8)
  915. #define DUK_HEAP_STRING_GET_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT8)
  916. #define DUK_HTHREAD_STRING_GET_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT8)
  917. #define DUK_HEAP_STRING_SUBARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBARRAY)
  918. #define DUK_HTHREAD_STRING_SUBARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBARRAY)
  919. #define DUK_HEAP_STRING_BYTES_PER_ELEMENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTES_PER_ELEMENT)
  920. #define DUK_HTHREAD_STRING_BYTES_PER_ELEMENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTES_PER_ELEMENT)
  921. #define DUK_HEAP_STRING_BYTE_OFFSET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTE_OFFSET)
  922. #define DUK_HTHREAD_STRING_BYTE_OFFSET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTE_OFFSET)
  923. #define DUK_HEAP_STRING_LC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BUFFER)
  924. #define DUK_HTHREAD_STRING_LC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BUFFER)
  925. #define DUK_HEAP_STRING_IS_VIEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_VIEW)
  926. #define DUK_HTHREAD_STRING_IS_VIEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_VIEW)
  927. #define DUK_HEAP_STRING_DATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA)
  928. #define DUK_HTHREAD_STRING_DATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA)
  929. #define DUK_HEAP_STRING_TYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPE)
  930. #define DUK_HTHREAD_STRING_TYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPE)
  931. #define DUK_HEAP_STRING_WRITE_INT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT_BE)
  932. #define DUK_HTHREAD_STRING_WRITE_INT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT_BE)
  933. #define DUK_HEAP_STRING_WRITE_INT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT_LE)
  934. #define DUK_HTHREAD_STRING_WRITE_INT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT_LE)
  935. #define DUK_HEAP_STRING_WRITE_UINT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT_BE)
  936. #define DUK_HTHREAD_STRING_WRITE_UINT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT_BE)
  937. #define DUK_HEAP_STRING_WRITE_UINT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT_LE)
  938. #define DUK_HTHREAD_STRING_WRITE_UINT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT_LE)
  939. #define DUK_HEAP_STRING_WRITE_DOUBLE_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_DOUBLE_BE)
  940. #define DUK_HTHREAD_STRING_WRITE_DOUBLE_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_DOUBLE_BE)
  941. #define DUK_HEAP_STRING_WRITE_DOUBLE_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_DOUBLE_LE)
  942. #define DUK_HTHREAD_STRING_WRITE_DOUBLE_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_DOUBLE_LE)
  943. #define DUK_HEAP_STRING_WRITE_FLOAT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_FLOAT_BE)
  944. #define DUK_HTHREAD_STRING_WRITE_FLOAT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_FLOAT_BE)
  945. #define DUK_HEAP_STRING_WRITE_FLOAT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_FLOAT_LE)
  946. #define DUK_HTHREAD_STRING_WRITE_FLOAT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_FLOAT_LE)
  947. #define DUK_HEAP_STRING_WRITE_INT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT32_BE)
  948. #define DUK_HTHREAD_STRING_WRITE_INT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT32_BE)
  949. #define DUK_HEAP_STRING_WRITE_INT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT32_LE)
  950. #define DUK_HTHREAD_STRING_WRITE_INT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT32_LE)
  951. #define DUK_HEAP_STRING_WRITE_UINT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT32_BE)
  952. #define DUK_HTHREAD_STRING_WRITE_UINT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT32_BE)
  953. #define DUK_HEAP_STRING_WRITE_UINT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT32_LE)
  954. #define DUK_HTHREAD_STRING_WRITE_UINT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT32_LE)
  955. #define DUK_HEAP_STRING_WRITE_INT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT16_BE)
  956. #define DUK_HTHREAD_STRING_WRITE_INT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT16_BE)
  957. #define DUK_HEAP_STRING_WRITE_INT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT16_LE)
  958. #define DUK_HTHREAD_STRING_WRITE_INT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT16_LE)
  959. #define DUK_HEAP_STRING_WRITE_UINT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT16_BE)
  960. #define DUK_HTHREAD_STRING_WRITE_UINT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT16_BE)
  961. #define DUK_HEAP_STRING_WRITE_UINT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT16_LE)
  962. #define DUK_HTHREAD_STRING_WRITE_UINT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT16_LE)
  963. #define DUK_HEAP_STRING_WRITE_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT8)
  964. #define DUK_HTHREAD_STRING_WRITE_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT8)
  965. #define DUK_HEAP_STRING_WRITE_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT8)
  966. #define DUK_HTHREAD_STRING_WRITE_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT8)
  967. #define DUK_HEAP_STRING_READ_INT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT_BE)
  968. #define DUK_HTHREAD_STRING_READ_INT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT_BE)
  969. #define DUK_HEAP_STRING_READ_INT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT_LE)
  970. #define DUK_HTHREAD_STRING_READ_INT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT_LE)
  971. #define DUK_HEAP_STRING_READ_UINT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT_BE)
  972. #define DUK_HTHREAD_STRING_READ_UINT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT_BE)
  973. #define DUK_HEAP_STRING_READ_UINT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT_LE)
  974. #define DUK_HTHREAD_STRING_READ_UINT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT_LE)
  975. #define DUK_HEAP_STRING_READ_DOUBLE_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_DOUBLE_BE)
  976. #define DUK_HTHREAD_STRING_READ_DOUBLE_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_DOUBLE_BE)
  977. #define DUK_HEAP_STRING_READ_DOUBLE_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_DOUBLE_LE)
  978. #define DUK_HTHREAD_STRING_READ_DOUBLE_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_DOUBLE_LE)
  979. #define DUK_HEAP_STRING_READ_FLOAT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_FLOAT_BE)
  980. #define DUK_HTHREAD_STRING_READ_FLOAT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_FLOAT_BE)
  981. #define DUK_HEAP_STRING_READ_FLOAT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_FLOAT_LE)
  982. #define DUK_HTHREAD_STRING_READ_FLOAT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_FLOAT_LE)
  983. #define DUK_HEAP_STRING_READ_INT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT32_BE)
  984. #define DUK_HTHREAD_STRING_READ_INT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT32_BE)
  985. #define DUK_HEAP_STRING_READ_INT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT32_LE)
  986. #define DUK_HTHREAD_STRING_READ_INT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT32_LE)
  987. #define DUK_HEAP_STRING_READ_UINT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT32_BE)
  988. #define DUK_HTHREAD_STRING_READ_UINT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT32_BE)
  989. #define DUK_HEAP_STRING_READ_UINT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT32_LE)
  990. #define DUK_HTHREAD_STRING_READ_UINT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT32_LE)
  991. #define DUK_HEAP_STRING_READ_INT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT16_BE)
  992. #define DUK_HTHREAD_STRING_READ_INT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT16_BE)
  993. #define DUK_HEAP_STRING_READ_INT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT16_LE)
  994. #define DUK_HTHREAD_STRING_READ_INT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT16_LE)
  995. #define DUK_HEAP_STRING_READ_UINT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT16_BE)
  996. #define DUK_HTHREAD_STRING_READ_UINT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT16_BE)
  997. #define DUK_HEAP_STRING_READ_UINT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT16_LE)
  998. #define DUK_HTHREAD_STRING_READ_UINT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT16_LE)
  999. #define DUK_HEAP_STRING_READ_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT8)
  1000. #define DUK_HTHREAD_STRING_READ_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT8)
  1001. #define DUK_HEAP_STRING_READ_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT8)
  1002. #define DUK_HTHREAD_STRING_READ_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT8)
  1003. #define DUK_HEAP_STRING_COPY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COPY)
  1004. #define DUK_HTHREAD_STRING_COPY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COPY)
  1005. #define DUK_HEAP_STRING_EQUALS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EQUALS)
  1006. #define DUK_HTHREAD_STRING_EQUALS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EQUALS)
  1007. #define DUK_HEAP_STRING_FILL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILL)
  1008. #define DUK_HTHREAD_STRING_FILL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILL)
  1009. #define DUK_HEAP_STRING_WRITE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE)
  1010. #define DUK_HTHREAD_STRING_WRITE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE)
  1011. #define DUK_HEAP_STRING_COMPARE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPARE)
  1012. #define DUK_HTHREAD_STRING_COMPARE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPARE)
  1013. #define DUK_HEAP_STRING_BYTE_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTE_LENGTH)
  1014. #define DUK_HTHREAD_STRING_BYTE_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTE_LENGTH)
  1015. #define DUK_HEAP_STRING_IS_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_BUFFER)
  1016. #define DUK_HTHREAD_STRING_IS_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_BUFFER)
  1017. #define DUK_HEAP_STRING_IS_ENCODING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_ENCODING)
  1018. #define DUK_HTHREAD_STRING_IS_ENCODING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_ENCODING)
  1019. #define DUK_HEAP_STRING_EXPORTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORTS)
  1020. #define DUK_HTHREAD_STRING_EXPORTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORTS)
  1021. #define DUK_HEAP_STRING_ID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ID)
  1022. #define DUK_HTHREAD_STRING_ID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ID)
  1023. #define DUK_HEAP_STRING_REQUIRE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REQUIRE)
  1024. #define DUK_HTHREAD_STRING_REQUIRE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REQUIRE)
  1025. #define DUK_HEAP_STRING___PROTO__(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX___PROTO__)
  1026. #define DUK_HTHREAD_STRING___PROTO__(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX___PROTO__)
  1027. #define DUK_HEAP_STRING_SET_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_PROTOTYPE_OF)
  1028. #define DUK_HTHREAD_STRING_SET_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_PROTOTYPE_OF)
  1029. #define DUK_HEAP_STRING_OWN_KEYS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OWN_KEYS)
  1030. #define DUK_HTHREAD_STRING_OWN_KEYS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OWN_KEYS)
  1031. #define DUK_HEAP_STRING_ENUMERATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERATE)
  1032. #define DUK_HTHREAD_STRING_ENUMERATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERATE)
  1033. #define DUK_HEAP_STRING_DELETE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE_PROPERTY)
  1034. #define DUK_HTHREAD_STRING_DELETE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE_PROPERTY)
  1035. #define DUK_HEAP_STRING_HAS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HAS)
  1036. #define DUK_HTHREAD_STRING_HAS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HAS)
  1037. #define DUK_HEAP_STRING_PROXY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROXY)
  1038. #define DUK_HTHREAD_STRING_PROXY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROXY)
  1039. #define DUK_HEAP_STRING_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLEE)
  1040. #define DUK_HTHREAD_STRING_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLEE)
  1041. #define DUK_HEAP_STRING_INVALID_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INVALID_DATE)
  1042. #define DUK_HTHREAD_STRING_INVALID_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INVALID_DATE)
  1043. #define DUK_HEAP_STRING_BRACKETED_ELLIPSIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BRACKETED_ELLIPSIS)
  1044. #define DUK_HTHREAD_STRING_BRACKETED_ELLIPSIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BRACKETED_ELLIPSIS)
  1045. #define DUK_HEAP_STRING_NEWLINE_TAB(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEWLINE_TAB)
  1046. #define DUK_HTHREAD_STRING_NEWLINE_TAB(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEWLINE_TAB)
  1047. #define DUK_HEAP_STRING_SPACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPACE)
  1048. #define DUK_HTHREAD_STRING_SPACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPACE)
  1049. #define DUK_HEAP_STRING_COMMA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMMA)
  1050. #define DUK_HTHREAD_STRING_COMMA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMMA)
  1051. #define DUK_HEAP_STRING_MINUS_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_ZERO)
  1052. #define DUK_HTHREAD_STRING_MINUS_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_ZERO)
  1053. #define DUK_HEAP_STRING_PLUS_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PLUS_ZERO)
  1054. #define DUK_HTHREAD_STRING_PLUS_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PLUS_ZERO)
  1055. #define DUK_HEAP_STRING_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ZERO)
  1056. #define DUK_HTHREAD_STRING_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ZERO)
  1057. #define DUK_HEAP_STRING_MINUS_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_INFINITY)
  1058. #define DUK_HTHREAD_STRING_MINUS_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_INFINITY)
  1059. #define DUK_HEAP_STRING_PLUS_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PLUS_INFINITY)
  1060. #define DUK_HTHREAD_STRING_PLUS_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PLUS_INFINITY)
  1061. #define DUK_HEAP_STRING_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INFINITY)
  1062. #define DUK_HTHREAD_STRING_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INFINITY)
  1063. #define DUK_HEAP_STRING_LC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_OBJECT)
  1064. #define DUK_HTHREAD_STRING_LC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_OBJECT)
  1065. #define DUK_HEAP_STRING_LC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_STRING)
  1066. #define DUK_HTHREAD_STRING_LC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_STRING)
  1067. #define DUK_HEAP_STRING_LC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NUMBER)
  1068. #define DUK_HTHREAD_STRING_LC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NUMBER)
  1069. #define DUK_HEAP_STRING_LC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BOOLEAN)
  1070. #define DUK_HTHREAD_STRING_LC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BOOLEAN)
  1071. #define DUK_HEAP_STRING_LC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_UNDEFINED)
  1072. #define DUK_HTHREAD_STRING_LC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_UNDEFINED)
  1073. #define DUK_HEAP_STRING_STRINGIFY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STRINGIFY)
  1074. #define DUK_HTHREAD_STRING_STRINGIFY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STRINGIFY)
  1075. #define DUK_HEAP_STRING_TAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TAN)
  1076. #define DUK_HTHREAD_STRING_TAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TAN)
  1077. #define DUK_HEAP_STRING_SQRT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT)
  1078. #define DUK_HTHREAD_STRING_SQRT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT)
  1079. #define DUK_HEAP_STRING_SIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SIN)
  1080. #define DUK_HTHREAD_STRING_SIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SIN)
  1081. #define DUK_HEAP_STRING_ROUND(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ROUND)
  1082. #define DUK_HTHREAD_STRING_ROUND(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ROUND)
  1083. #define DUK_HEAP_STRING_RANDOM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RANDOM)
  1084. #define DUK_HTHREAD_STRING_RANDOM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RANDOM)
  1085. #define DUK_HEAP_STRING_POW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POW)
  1086. #define DUK_HTHREAD_STRING_POW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POW)
  1087. #define DUK_HEAP_STRING_MIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MIN)
  1088. #define DUK_HTHREAD_STRING_MIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MIN)
  1089. #define DUK_HEAP_STRING_MAX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAX)
  1090. #define DUK_HTHREAD_STRING_MAX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAX)
  1091. #define DUK_HEAP_STRING_LOG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG)
  1092. #define DUK_HTHREAD_STRING_LOG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG)
  1093. #define DUK_HEAP_STRING_FLOOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOOR)
  1094. #define DUK_HTHREAD_STRING_FLOOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOOR)
  1095. #define DUK_HEAP_STRING_EXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXP)
  1096. #define DUK_HTHREAD_STRING_EXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXP)
  1097. #define DUK_HEAP_STRING_COS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COS)
  1098. #define DUK_HTHREAD_STRING_COS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COS)
  1099. #define DUK_HEAP_STRING_CEIL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CEIL)
  1100. #define DUK_HTHREAD_STRING_CEIL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CEIL)
  1101. #define DUK_HEAP_STRING_ATAN2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ATAN2)
  1102. #define DUK_HTHREAD_STRING_ATAN2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ATAN2)
  1103. #define DUK_HEAP_STRING_ATAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ATAN)
  1104. #define DUK_HTHREAD_STRING_ATAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ATAN)
  1105. #define DUK_HEAP_STRING_ASIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ASIN)
  1106. #define DUK_HTHREAD_STRING_ASIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ASIN)
  1107. #define DUK_HEAP_STRING_ACOS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ACOS)
  1108. #define DUK_HTHREAD_STRING_ACOS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ACOS)
  1109. #define DUK_HEAP_STRING_ABS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ABS)
  1110. #define DUK_HTHREAD_STRING_ABS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ABS)
  1111. #define DUK_HEAP_STRING_SQRT2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT2)
  1112. #define DUK_HTHREAD_STRING_SQRT2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT2)
  1113. #define DUK_HEAP_STRING_SQRT1_2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT1_2)
  1114. #define DUK_HTHREAD_STRING_SQRT1_2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT1_2)
  1115. #define DUK_HEAP_STRING_PI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PI)
  1116. #define DUK_HTHREAD_STRING_PI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PI)
  1117. #define DUK_HEAP_STRING_LOG10E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG10E)
  1118. #define DUK_HTHREAD_STRING_LOG10E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG10E)
  1119. #define DUK_HEAP_STRING_LOG2E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG2E)
  1120. #define DUK_HTHREAD_STRING_LOG2E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG2E)
  1121. #define DUK_HEAP_STRING_LN2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LN2)
  1122. #define DUK_HTHREAD_STRING_LN2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LN2)
  1123. #define DUK_HEAP_STRING_LN10(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LN10)
  1124. #define DUK_HTHREAD_STRING_LN10(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LN10)
  1125. #define DUK_HEAP_STRING_E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_E)
  1126. #define DUK_HTHREAD_STRING_E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_E)
  1127. #define DUK_HEAP_STRING_MESSAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MESSAGE)
  1128. #define DUK_HTHREAD_STRING_MESSAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MESSAGE)
  1129. #define DUK_HEAP_STRING_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAME)
  1130. #define DUK_HTHREAD_STRING_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAME)
  1131. #define DUK_HEAP_STRING_INPUT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INPUT)
  1132. #define DUK_HTHREAD_STRING_INPUT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INPUT)
  1133. #define DUK_HEAP_STRING_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INDEX)
  1134. #define DUK_HTHREAD_STRING_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INDEX)
  1135. #define DUK_HEAP_STRING_ESCAPED_EMPTY_REGEXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ESCAPED_EMPTY_REGEXP)
  1136. #define DUK_HTHREAD_STRING_ESCAPED_EMPTY_REGEXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ESCAPED_EMPTY_REGEXP)
  1137. #define DUK_HEAP_STRING_LAST_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LAST_INDEX)
  1138. #define DUK_HTHREAD_STRING_LAST_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LAST_INDEX)
  1139. #define DUK_HEAP_STRING_MULTILINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MULTILINE)
  1140. #define DUK_HTHREAD_STRING_MULTILINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MULTILINE)
  1141. #define DUK_HEAP_STRING_IGNORE_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IGNORE_CASE)
  1142. #define DUK_HTHREAD_STRING_IGNORE_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IGNORE_CASE)
  1143. #define DUK_HEAP_STRING_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SOURCE)
  1144. #define DUK_HTHREAD_STRING_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SOURCE)
  1145. #define DUK_HEAP_STRING_TEST(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TEST)
  1146. #define DUK_HTHREAD_STRING_TEST(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TEST)
  1147. #define DUK_HEAP_STRING_EXEC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXEC)
  1148. #define DUK_HTHREAD_STRING_EXEC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXEC)
  1149. #define DUK_HEAP_STRING_TO_GMT_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_GMT_STRING)
  1150. #define DUK_HTHREAD_STRING_TO_GMT_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_GMT_STRING)
  1151. #define DUK_HEAP_STRING_SET_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_YEAR)
  1152. #define DUK_HTHREAD_STRING_SET_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_YEAR)
  1153. #define DUK_HEAP_STRING_GET_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_YEAR)
  1154. #define DUK_HTHREAD_STRING_GET_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_YEAR)
  1155. #define DUK_HEAP_STRING_TO_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_JSON)
  1156. #define DUK_HTHREAD_STRING_TO_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_JSON)
  1157. #define DUK_HEAP_STRING_TO_ISO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_ISO_STRING)
  1158. #define DUK_HTHREAD_STRING_TO_ISO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_ISO_STRING)
  1159. #define DUK_HEAP_STRING_TO_UTC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_UTC_STRING)
  1160. #define DUK_HTHREAD_STRING_TO_UTC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_UTC_STRING)
  1161. #define DUK_HEAP_STRING_SET_UTC_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_FULL_YEAR)
  1162. #define DUK_HTHREAD_STRING_SET_UTC_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_FULL_YEAR)
  1163. #define DUK_HEAP_STRING_SET_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FULL_YEAR)
  1164. #define DUK_HTHREAD_STRING_SET_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FULL_YEAR)
  1165. #define DUK_HEAP_STRING_SET_UTC_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MONTH)
  1166. #define DUK_HTHREAD_STRING_SET_UTC_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MONTH)
  1167. #define DUK_HEAP_STRING_SET_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MONTH)
  1168. #define DUK_HTHREAD_STRING_SET_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MONTH)
  1169. #define DUK_HEAP_STRING_SET_UTC_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_DATE)
  1170. #define DUK_HTHREAD_STRING_SET_UTC_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_DATE)
  1171. #define DUK_HEAP_STRING_SET_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_DATE)
  1172. #define DUK_HTHREAD_STRING_SET_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_DATE)
  1173. #define DUK_HEAP_STRING_SET_UTC_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_HOURS)
  1174. #define DUK_HTHREAD_STRING_SET_UTC_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_HOURS)
  1175. #define DUK_HEAP_STRING_SET_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_HOURS)
  1176. #define DUK_HTHREAD_STRING_SET_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_HOURS)
  1177. #define DUK_HEAP_STRING_SET_UTC_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MINUTES)
  1178. #define DUK_HTHREAD_STRING_SET_UTC_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MINUTES)
  1179. #define DUK_HEAP_STRING_SET_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MINUTES)
  1180. #define DUK_HTHREAD_STRING_SET_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MINUTES)
  1181. #define DUK_HEAP_STRING_SET_UTC_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_SECONDS)
  1182. #define DUK_HTHREAD_STRING_SET_UTC_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_SECONDS)
  1183. #define DUK_HEAP_STRING_SET_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_SECONDS)
  1184. #define DUK_HTHREAD_STRING_SET_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_SECONDS)
  1185. #define DUK_HEAP_STRING_SET_UTC_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MILLISECONDS)
  1186. #define DUK_HTHREAD_STRING_SET_UTC_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MILLISECONDS)
  1187. #define DUK_HEAP_STRING_SET_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MILLISECONDS)
  1188. #define DUK_HTHREAD_STRING_SET_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MILLISECONDS)
  1189. #define DUK_HEAP_STRING_SET_TIME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_TIME)
  1190. #define DUK_HTHREAD_STRING_SET_TIME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_TIME)
  1191. #define DUK_HEAP_STRING_GET_TIMEZONE_OFFSET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_TIMEZONE_OFFSET)
  1192. #define DUK_HTHREAD_STRING_GET_TIMEZONE_OFFSET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_TIMEZONE_OFFSET)
  1193. #define DUK_HEAP_STRING_GET_UTC_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MILLISECONDS)
  1194. #define DUK_HTHREAD_STRING_GET_UTC_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MILLISECONDS)
  1195. #define DUK_HEAP_STRING_GET_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MILLISECONDS)
  1196. #define DUK_HTHREAD_STRING_GET_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MILLISECONDS)
  1197. #define DUK_HEAP_STRING_GET_UTC_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_SECONDS)
  1198. #define DUK_HTHREAD_STRING_GET_UTC_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_SECONDS)
  1199. #define DUK_HEAP_STRING_GET_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_SECONDS)
  1200. #define DUK_HTHREAD_STRING_GET_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_SECONDS)
  1201. #define DUK_HEAP_STRING_GET_UTC_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MINUTES)
  1202. #define DUK_HTHREAD_STRING_GET_UTC_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MINUTES)
  1203. #define DUK_HEAP_STRING_GET_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MINUTES)
  1204. #define DUK_HTHREAD_STRING_GET_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MINUTES)
  1205. #define DUK_HEAP_STRING_GET_UTC_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_HOURS)
  1206. #define DUK_HTHREAD_STRING_GET_UTC_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_HOURS)
  1207. #define DUK_HEAP_STRING_GET_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_HOURS)
  1208. #define DUK_HTHREAD_STRING_GET_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_HOURS)
  1209. #define DUK_HEAP_STRING_GET_UTC_DAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_DAY)
  1210. #define DUK_HTHREAD_STRING_GET_UTC_DAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_DAY)
  1211. #define DUK_HEAP_STRING_GET_DAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_DAY)
  1212. #define DUK_HTHREAD_STRING_GET_DAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_DAY)
  1213. #define DUK_HEAP_STRING_GET_UTC_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_DATE)
  1214. #define DUK_HTHREAD_STRING_GET_UTC_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_DATE)
  1215. #define DUK_HEAP_STRING_GET_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_DATE)
  1216. #define DUK_HTHREAD_STRING_GET_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_DATE)
  1217. #define DUK_HEAP_STRING_GET_UTC_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MONTH)
  1218. #define DUK_HTHREAD_STRING_GET_UTC_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MONTH)
  1219. #define DUK_HEAP_STRING_GET_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MONTH)
  1220. #define DUK_HTHREAD_STRING_GET_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MONTH)
  1221. #define DUK_HEAP_STRING_GET_UTC_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_FULL_YEAR)
  1222. #define DUK_HTHREAD_STRING_GET_UTC_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_FULL_YEAR)
  1223. #define DUK_HEAP_STRING_GET_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FULL_YEAR)
  1224. #define DUK_HTHREAD_STRING_GET_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FULL_YEAR)
  1225. #define DUK_HEAP_STRING_GET_TIME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_TIME)
  1226. #define DUK_HTHREAD_STRING_GET_TIME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_TIME)
  1227. #define DUK_HEAP_STRING_TO_LOCALE_TIME_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_TIME_STRING)
  1228. #define DUK_HTHREAD_STRING_TO_LOCALE_TIME_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_TIME_STRING)
  1229. #define DUK_HEAP_STRING_TO_LOCALE_DATE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_DATE_STRING)
  1230. #define DUK_HTHREAD_STRING_TO_LOCALE_DATE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_DATE_STRING)
  1231. #define DUK_HEAP_STRING_TO_TIME_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_TIME_STRING)
  1232. #define DUK_HTHREAD_STRING_TO_TIME_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_TIME_STRING)
  1233. #define DUK_HEAP_STRING_TO_DATE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_DATE_STRING)
  1234. #define DUK_HTHREAD_STRING_TO_DATE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_DATE_STRING)
  1235. #define DUK_HEAP_STRING_NOW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NOW)
  1236. #define DUK_HTHREAD_STRING_NOW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NOW)
  1237. #define DUK_HEAP_STRING_UTC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UTC)
  1238. #define DUK_HTHREAD_STRING_UTC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UTC)
  1239. #define DUK_HEAP_STRING_PARSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE)
  1240. #define DUK_HTHREAD_STRING_PARSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE)
  1241. #define DUK_HEAP_STRING_TO_PRECISION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_PRECISION)
  1242. #define DUK_HTHREAD_STRING_TO_PRECISION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_PRECISION)
  1243. #define DUK_HEAP_STRING_TO_EXPONENTIAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_EXPONENTIAL)
  1244. #define DUK_HTHREAD_STRING_TO_EXPONENTIAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_EXPONENTIAL)
  1245. #define DUK_HEAP_STRING_TO_FIXED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_FIXED)
  1246. #define DUK_HTHREAD_STRING_TO_FIXED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_FIXED)
  1247. #define DUK_HEAP_STRING_POSITIVE_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POSITIVE_INFINITY)
  1248. #define DUK_HTHREAD_STRING_POSITIVE_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POSITIVE_INFINITY)
  1249. #define DUK_HEAP_STRING_NEGATIVE_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEGATIVE_INFINITY)
  1250. #define DUK_HTHREAD_STRING_NEGATIVE_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEGATIVE_INFINITY)
  1251. #define DUK_HEAP_STRING_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAN)
  1252. #define DUK_HTHREAD_STRING_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAN)
  1253. #define DUK_HEAP_STRING_MIN_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MIN_VALUE)
  1254. #define DUK_HTHREAD_STRING_MIN_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MIN_VALUE)
  1255. #define DUK_HEAP_STRING_MAX_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAX_VALUE)
  1256. #define DUK_HTHREAD_STRING_MAX_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAX_VALUE)
  1257. #define DUK_HEAP_STRING_SUBSTR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBSTR)
  1258. #define DUK_HTHREAD_STRING_SUBSTR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBSTR)
  1259. #define DUK_HEAP_STRING_TRIM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRIM)
  1260. #define DUK_HTHREAD_STRING_TRIM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRIM)
  1261. #define DUK_HEAP_STRING_TO_LOCALE_UPPER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_UPPER_CASE)
  1262. #define DUK_HTHREAD_STRING_TO_LOCALE_UPPER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_UPPER_CASE)
  1263. #define DUK_HEAP_STRING_TO_UPPER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_UPPER_CASE)
  1264. #define DUK_HTHREAD_STRING_TO_UPPER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_UPPER_CASE)
  1265. #define DUK_HEAP_STRING_TO_LOCALE_LOWER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_LOWER_CASE)
  1266. #define DUK_HTHREAD_STRING_TO_LOCALE_LOWER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_LOWER_CASE)
  1267. #define DUK_HEAP_STRING_TO_LOWER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOWER_CASE)
  1268. #define DUK_HTHREAD_STRING_TO_LOWER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOWER_CASE)
  1269. #define DUK_HEAP_STRING_SUBSTRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBSTRING)
  1270. #define DUK_HTHREAD_STRING_SUBSTRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBSTRING)
  1271. #define DUK_HEAP_STRING_SPLIT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPLIT)
  1272. #define DUK_HTHREAD_STRING_SPLIT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPLIT)
  1273. #define DUK_HEAP_STRING_SEARCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SEARCH)
  1274. #define DUK_HTHREAD_STRING_SEARCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SEARCH)
  1275. #define DUK_HEAP_STRING_REPLACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REPLACE)
  1276. #define DUK_HTHREAD_STRING_REPLACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REPLACE)
  1277. #define DUK_HEAP_STRING_MATCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MATCH)
  1278. #define DUK_HTHREAD_STRING_MATCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MATCH)
  1279. #define DUK_HEAP_STRING_LOCALE_COMPARE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOCALE_COMPARE)
  1280. #define DUK_HTHREAD_STRING_LOCALE_COMPARE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOCALE_COMPARE)
  1281. #define DUK_HEAP_STRING_CHAR_CODE_AT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CHAR_CODE_AT)
  1282. #define DUK_HTHREAD_STRING_CHAR_CODE_AT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CHAR_CODE_AT)
  1283. #define DUK_HEAP_STRING_CHAR_AT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CHAR_AT)
  1284. #define DUK_HTHREAD_STRING_CHAR_AT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CHAR_AT)
  1285. #define DUK_HEAP_STRING_FROM_CHAR_CODE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FROM_CHAR_CODE)
  1286. #define DUK_HTHREAD_STRING_FROM_CHAR_CODE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FROM_CHAR_CODE)
  1287. #define DUK_HEAP_STRING_REDUCE_RIGHT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REDUCE_RIGHT)
  1288. #define DUK_HTHREAD_STRING_REDUCE_RIGHT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REDUCE_RIGHT)
  1289. #define DUK_HEAP_STRING_REDUCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REDUCE)
  1290. #define DUK_HTHREAD_STRING_REDUCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REDUCE)
  1291. #define DUK_HEAP_STRING_FILTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILTER)
  1292. #define DUK_HTHREAD_STRING_FILTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILTER)
  1293. #define DUK_HEAP_STRING_MAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAP)
  1294. #define DUK_HTHREAD_STRING_MAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAP)
  1295. #define DUK_HEAP_STRING_FOR_EACH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FOR_EACH)
  1296. #define DUK_HTHREAD_STRING_FOR_EACH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FOR_EACH)
  1297. #define DUK_HEAP_STRING_SOME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SOME)
  1298. #define DUK_HTHREAD_STRING_SOME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SOME)
  1299. #define DUK_HEAP_STRING_EVERY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVERY)
  1300. #define DUK_HTHREAD_STRING_EVERY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVERY)
  1301. #define DUK_HEAP_STRING_LAST_INDEX_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LAST_INDEX_OF)
  1302. #define DUK_HTHREAD_STRING_LAST_INDEX_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LAST_INDEX_OF)
  1303. #define DUK_HEAP_STRING_INDEX_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INDEX_OF)
  1304. #define DUK_HTHREAD_STRING_INDEX_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INDEX_OF)
  1305. #define DUK_HEAP_STRING_UNSHIFT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UNSHIFT)
  1306. #define DUK_HTHREAD_STRING_UNSHIFT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UNSHIFT)
  1307. #define DUK_HEAP_STRING_SPLICE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPLICE)
  1308. #define DUK_HTHREAD_STRING_SPLICE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPLICE)
  1309. #define DUK_HEAP_STRING_SORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SORT)
  1310. #define DUK_HTHREAD_STRING_SORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SORT)
  1311. #define DUK_HEAP_STRING_SLICE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SLICE)
  1312. #define DUK_HTHREAD_STRING_SLICE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SLICE)
  1313. #define DUK_HEAP_STRING_SHIFT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SHIFT)
  1314. #define DUK_HTHREAD_STRING_SHIFT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SHIFT)
  1315. #define DUK_HEAP_STRING_REVERSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REVERSE)
  1316. #define DUK_HTHREAD_STRING_REVERSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REVERSE)
  1317. #define DUK_HEAP_STRING_PUSH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PUSH)
  1318. #define DUK_HTHREAD_STRING_PUSH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PUSH)
  1319. #define DUK_HEAP_STRING_POP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POP)
  1320. #define DUK_HTHREAD_STRING_POP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POP)
  1321. #define DUK_HEAP_STRING_JOIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JOIN)
  1322. #define DUK_HTHREAD_STRING_JOIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JOIN)
  1323. #define DUK_HEAP_STRING_CONCAT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONCAT)
  1324. #define DUK_HTHREAD_STRING_CONCAT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONCAT)
  1325. #define DUK_HEAP_STRING_IS_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_ARRAY)
  1326. #define DUK_HTHREAD_STRING_IS_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_ARRAY)
  1327. #define DUK_HEAP_STRING_LC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ARGUMENTS)
  1328. #define DUK_HTHREAD_STRING_LC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ARGUMENTS)
  1329. #define DUK_HEAP_STRING_CALLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLER)
  1330. #define DUK_HTHREAD_STRING_CALLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLER)
  1331. #define DUK_HEAP_STRING_BIND(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BIND)
  1332. #define DUK_HTHREAD_STRING_BIND(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BIND)
  1333. #define DUK_HEAP_STRING_CALL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALL)
  1334. #define DUK_HTHREAD_STRING_CALL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALL)
  1335. #define DUK_HEAP_STRING_APPLY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_APPLY)
  1336. #define DUK_HTHREAD_STRING_APPLY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_APPLY)
  1337. #define DUK_HEAP_STRING_PROPERTY_IS_ENUMERABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROPERTY_IS_ENUMERABLE)
  1338. #define DUK_HTHREAD_STRING_PROPERTY_IS_ENUMERABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROPERTY_IS_ENUMERABLE)
  1339. #define DUK_HEAP_STRING_IS_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_PROTOTYPE_OF)
  1340. #define DUK_HTHREAD_STRING_IS_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_PROTOTYPE_OF)
  1341. #define DUK_HEAP_STRING_HAS_OWN_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HAS_OWN_PROPERTY)
  1342. #define DUK_HTHREAD_STRING_HAS_OWN_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HAS_OWN_PROPERTY)
  1343. #define DUK_HEAP_STRING_VALUE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE_OF)
  1344. #define DUK_HTHREAD_STRING_VALUE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE_OF)
  1345. #define DUK_HEAP_STRING_TO_LOCALE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_STRING)
  1346. #define DUK_HTHREAD_STRING_TO_LOCALE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_STRING)
  1347. #define DUK_HEAP_STRING_TO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_STRING)
  1348. #define DUK_HTHREAD_STRING_TO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_STRING)
  1349. #define DUK_HEAP_STRING_CONSTRUCTOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONSTRUCTOR)
  1350. #define DUK_HTHREAD_STRING_CONSTRUCTOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONSTRUCTOR)
  1351. #define DUK_HEAP_STRING_SET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET)
  1352. #define DUK_HTHREAD_STRING_SET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET)
  1353. #define DUK_HEAP_STRING_GET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET)
  1354. #define DUK_HTHREAD_STRING_GET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET)
  1355. #define DUK_HEAP_STRING_ENUMERABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERABLE)
  1356. #define DUK_HTHREAD_STRING_ENUMERABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERABLE)
  1357. #define DUK_HEAP_STRING_CONFIGURABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONFIGURABLE)
  1358. #define DUK_HTHREAD_STRING_CONFIGURABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONFIGURABLE)
  1359. #define DUK_HEAP_STRING_WRITABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITABLE)
  1360. #define DUK_HTHREAD_STRING_WRITABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITABLE)
  1361. #define DUK_HEAP_STRING_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE)
  1362. #define DUK_HTHREAD_STRING_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE)
  1363. #define DUK_HEAP_STRING_KEYS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_KEYS)
  1364. #define DUK_HTHREAD_STRING_KEYS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_KEYS)
  1365. #define DUK_HEAP_STRING_IS_EXTENSIBLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_EXTENSIBLE)
  1366. #define DUK_HTHREAD_STRING_IS_EXTENSIBLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_EXTENSIBLE)
  1367. #define DUK_HEAP_STRING_IS_FROZEN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_FROZEN)
  1368. #define DUK_HTHREAD_STRING_IS_FROZEN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_FROZEN)
  1369. #define DUK_HEAP_STRING_IS_SEALED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_SEALED)
  1370. #define DUK_HTHREAD_STRING_IS_SEALED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_SEALED)
  1371. #define DUK_HEAP_STRING_PREVENT_EXTENSIONS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PREVENT_EXTENSIONS)
  1372. #define DUK_HTHREAD_STRING_PREVENT_EXTENSIONS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PREVENT_EXTENSIONS)
  1373. #define DUK_HEAP_STRING_FREEZE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FREEZE)
  1374. #define DUK_HTHREAD_STRING_FREEZE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FREEZE)
  1375. #define DUK_HEAP_STRING_SEAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SEAL)
  1376. #define DUK_HTHREAD_STRING_SEAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SEAL)
  1377. #define DUK_HEAP_STRING_DEFINE_PROPERTIES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFINE_PROPERTIES)
  1378. #define DUK_HTHREAD_STRING_DEFINE_PROPERTIES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFINE_PROPERTIES)
  1379. #define DUK_HEAP_STRING_DEFINE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFINE_PROPERTY)
  1380. #define DUK_HTHREAD_STRING_DEFINE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFINE_PROPERTY)
  1381. #define DUK_HEAP_STRING_CREATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CREATE)
  1382. #define DUK_HTHREAD_STRING_CREATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CREATE)
  1383. #define DUK_HEAP_STRING_GET_OWN_PROPERTY_NAMES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_OWN_PROPERTY_NAMES)
  1384. #define DUK_HTHREAD_STRING_GET_OWN_PROPERTY_NAMES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_OWN_PROPERTY_NAMES)
  1385. #define DUK_HEAP_STRING_GET_OWN_PROPERTY_DESCRIPTOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR)
  1386. #define DUK_HTHREAD_STRING_GET_OWN_PROPERTY_DESCRIPTOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR)
  1387. #define DUK_HEAP_STRING_GET_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_PROTOTYPE_OF)
  1388. #define DUK_HTHREAD_STRING_GET_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_PROTOTYPE_OF)
  1389. #define DUK_HEAP_STRING_PROTOTYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTOTYPE)
  1390. #define DUK_HTHREAD_STRING_PROTOTYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTOTYPE)
  1391. #define DUK_HEAP_STRING_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LENGTH)
  1392. #define DUK_HTHREAD_STRING_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LENGTH)
  1393. #define DUK_HEAP_STRING_ALERT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ALERT)
  1394. #define DUK_HTHREAD_STRING_ALERT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ALERT)
  1395. #define DUK_HEAP_STRING_PRINT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PRINT)
  1396. #define DUK_HTHREAD_STRING_PRINT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PRINT)
  1397. #define DUK_HEAP_STRING_UNESCAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UNESCAPE)
  1398. #define DUK_HTHREAD_STRING_UNESCAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UNESCAPE)
  1399. #define DUK_HEAP_STRING_ESCAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ESCAPE)
  1400. #define DUK_HTHREAD_STRING_ESCAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ESCAPE)
  1401. #define DUK_HEAP_STRING_ENCODE_URI_COMPONENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENCODE_URI_COMPONENT)
  1402. #define DUK_HTHREAD_STRING_ENCODE_URI_COMPONENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENCODE_URI_COMPONENT)
  1403. #define DUK_HEAP_STRING_ENCODE_URI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENCODE_URI)
  1404. #define DUK_HTHREAD_STRING_ENCODE_URI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENCODE_URI)
  1405. #define DUK_HEAP_STRING_DECODE_URI_COMPONENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DECODE_URI_COMPONENT)
  1406. #define DUK_HTHREAD_STRING_DECODE_URI_COMPONENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DECODE_URI_COMPONENT)
  1407. #define DUK_HEAP_STRING_DECODE_URI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DECODE_URI)
  1408. #define DUK_HTHREAD_STRING_DECODE_URI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DECODE_URI)
  1409. #define DUK_HEAP_STRING_IS_FINITE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_FINITE)
  1410. #define DUK_HTHREAD_STRING_IS_FINITE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_FINITE)
  1411. #define DUK_HEAP_STRING_IS_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_NAN)
  1412. #define DUK_HTHREAD_STRING_IS_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_NAN)
  1413. #define DUK_HEAP_STRING_PARSE_FLOAT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE_FLOAT)
  1414. #define DUK_HTHREAD_STRING_PARSE_FLOAT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE_FLOAT)
  1415. #define DUK_HEAP_STRING_PARSE_INT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE_INT)
  1416. #define DUK_HTHREAD_STRING_PARSE_INT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE_INT)
  1417. #define DUK_HEAP_STRING_EVAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVAL)
  1418. #define DUK_HTHREAD_STRING_EVAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVAL)
  1419. #define DUK_HEAP_STRING_URI_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_URI_ERROR)
  1420. #define DUK_HTHREAD_STRING_URI_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_URI_ERROR)
  1421. #define DUK_HEAP_STRING_TYPE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPE_ERROR)
  1422. #define DUK_HTHREAD_STRING_TYPE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPE_ERROR)
  1423. #define DUK_HEAP_STRING_SYNTAX_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SYNTAX_ERROR)
  1424. #define DUK_HTHREAD_STRING_SYNTAX_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SYNTAX_ERROR)
  1425. #define DUK_HEAP_STRING_REFERENCE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REFERENCE_ERROR)
  1426. #define DUK_HTHREAD_STRING_REFERENCE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REFERENCE_ERROR)
  1427. #define DUK_HEAP_STRING_RANGE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RANGE_ERROR)
  1428. #define DUK_HTHREAD_STRING_RANGE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RANGE_ERROR)
  1429. #define DUK_HEAP_STRING_EVAL_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVAL_ERROR)
  1430. #define DUK_HTHREAD_STRING_EVAL_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVAL_ERROR)
  1431. #define DUK_HEAP_STRING_BREAK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BREAK)
  1432. #define DUK_HTHREAD_STRING_BREAK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BREAK)
  1433. #define DUK_HEAP_STRING_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CASE)
  1434. #define DUK_HTHREAD_STRING_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CASE)
  1435. #define DUK_HEAP_STRING_CATCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CATCH)
  1436. #define DUK_HTHREAD_STRING_CATCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CATCH)
  1437. #define DUK_HEAP_STRING_CONTINUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONTINUE)
  1438. #define DUK_HTHREAD_STRING_CONTINUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONTINUE)
  1439. #define DUK_HEAP_STRING_DEBUGGER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEBUGGER)
  1440. #define DUK_HTHREAD_STRING_DEBUGGER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEBUGGER)
  1441. #define DUK_HEAP_STRING_DEFAULT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFAULT)
  1442. #define DUK_HTHREAD_STRING_DEFAULT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFAULT)
  1443. #define DUK_HEAP_STRING_DELETE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE)
  1444. #define DUK_HTHREAD_STRING_DELETE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE)
  1445. #define DUK_HEAP_STRING_DO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DO)
  1446. #define DUK_HTHREAD_STRING_DO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DO)
  1447. #define DUK_HEAP_STRING_ELSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ELSE)
  1448. #define DUK_HTHREAD_STRING_ELSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ELSE)
  1449. #define DUK_HEAP_STRING_FINALLY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FINALLY)
  1450. #define DUK_HTHREAD_STRING_FINALLY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FINALLY)
  1451. #define DUK_HEAP_STRING_FOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FOR)
  1452. #define DUK_HTHREAD_STRING_FOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FOR)
  1453. #define DUK_HEAP_STRING_LC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FUNCTION)
  1454. #define DUK_HTHREAD_STRING_LC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FUNCTION)
  1455. #define DUK_HEAP_STRING_IF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IF)
  1456. #define DUK_HTHREAD_STRING_IF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IF)
  1457. #define DUK_HEAP_STRING_IN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IN)
  1458. #define DUK_HTHREAD_STRING_IN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IN)
  1459. #define DUK_HEAP_STRING_INSTANCEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INSTANCEOF)
  1460. #define DUK_HTHREAD_STRING_INSTANCEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INSTANCEOF)
  1461. #define DUK_HEAP_STRING_NEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEW)
  1462. #define DUK_HTHREAD_STRING_NEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEW)
  1463. #define DUK_HEAP_STRING_RETURN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RETURN)
  1464. #define DUK_HTHREAD_STRING_RETURN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RETURN)
  1465. #define DUK_HEAP_STRING_SWITCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SWITCH)
  1466. #define DUK_HTHREAD_STRING_SWITCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SWITCH)
  1467. #define DUK_HEAP_STRING_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THIS)
  1468. #define DUK_HTHREAD_STRING_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THIS)
  1469. #define DUK_HEAP_STRING_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THROW)
  1470. #define DUK_HTHREAD_STRING_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THROW)
  1471. #define DUK_HEAP_STRING_TRY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRY)
  1472. #define DUK_HTHREAD_STRING_TRY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRY)
  1473. #define DUK_HEAP_STRING_TYPEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPEOF)
  1474. #define DUK_HTHREAD_STRING_TYPEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPEOF)
  1475. #define DUK_HEAP_STRING_VAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VAR)
  1476. #define DUK_HTHREAD_STRING_VAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VAR)
  1477. #define DUK_HEAP_STRING_VOID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VOID)
  1478. #define DUK_HTHREAD_STRING_VOID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VOID)
  1479. #define DUK_HEAP_STRING_WHILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WHILE)
  1480. #define DUK_HTHREAD_STRING_WHILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WHILE)
  1481. #define DUK_HEAP_STRING_WITH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WITH)
  1482. #define DUK_HTHREAD_STRING_WITH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WITH)
  1483. #define DUK_HEAP_STRING_CLASS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLASS)
  1484. #define DUK_HTHREAD_STRING_CLASS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLASS)
  1485. #define DUK_HEAP_STRING_CONST(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONST)
  1486. #define DUK_HTHREAD_STRING_CONST(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONST)
  1487. #define DUK_HEAP_STRING_ENUM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUM)
  1488. #define DUK_HTHREAD_STRING_ENUM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUM)
  1489. #define DUK_HEAP_STRING_EXPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORT)
  1490. #define DUK_HTHREAD_STRING_EXPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORT)
  1491. #define DUK_HEAP_STRING_EXTENDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXTENDS)
  1492. #define DUK_HTHREAD_STRING_EXTENDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXTENDS)
  1493. #define DUK_HEAP_STRING_IMPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPORT)
  1494. #define DUK_HTHREAD_STRING_IMPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPORT)
  1495. #define DUK_HEAP_STRING_SUPER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUPER)
  1496. #define DUK_HTHREAD_STRING_SUPER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUPER)
  1497. #define DUK_HEAP_STRING_LC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NULL)
  1498. #define DUK_HTHREAD_STRING_LC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NULL)
  1499. #define DUK_HEAP_STRING_TRUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRUE)
  1500. #define DUK_HTHREAD_STRING_TRUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRUE)
  1501. #define DUK_HEAP_STRING_FALSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FALSE)
  1502. #define DUK_HTHREAD_STRING_FALSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FALSE)
  1503. #define DUK_HEAP_STRING_IMPLEMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPLEMENTS)
  1504. #define DUK_HTHREAD_STRING_IMPLEMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPLEMENTS)
  1505. #define DUK_HEAP_STRING_INTERFACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INTERFACE)
  1506. #define DUK_HTHREAD_STRING_INTERFACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INTERFACE)
  1507. #define DUK_HEAP_STRING_LET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LET)
  1508. #define DUK_HTHREAD_STRING_LET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LET)
  1509. #define DUK_HEAP_STRING_PACKAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PACKAGE)
  1510. #define DUK_HTHREAD_STRING_PACKAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PACKAGE)
  1511. #define DUK_HEAP_STRING_PRIVATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PRIVATE)
  1512. #define DUK_HTHREAD_STRING_PRIVATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PRIVATE)
  1513. #define DUK_HEAP_STRING_PROTECTED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTECTED)
  1514. #define DUK_HTHREAD_STRING_PROTECTED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTECTED)
  1515. #define DUK_HEAP_STRING_PUBLIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PUBLIC)
  1516. #define DUK_HTHREAD_STRING_PUBLIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PUBLIC)
  1517. #define DUK_HEAP_STRING_STATIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STATIC)
  1518. #define DUK_HTHREAD_STRING_STATIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STATIC)
  1519. #define DUK_HEAP_STRING_YIELD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_YIELD)
  1520. #define DUK_HTHREAD_STRING_YIELD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_YIELD)
  1521. #define DUK_HEAP_NUM_STRINGS 414
  1522. #define DUK_STRIDX_START_RESERVED 369
  1523. #define DUK_STRIDX_START_STRICT_RESERVED 405
  1524. #define DUK_STRIDX_END_RESERVED 414 /* exclusive endpoint */
  1525. #if !defined(DUK_SINGLE_FILE)
  1526. DUK_INTERNAL_DECL const duk_c_function duk_bi_native_functions[147];
  1527. DUK_INTERNAL_DECL const duk_uint8_t duk_builtins_data[1952];
  1528. #ifdef DUK_USE_BUILTIN_INITJS
  1529. DUK_INTERNAL_DECL const duk_uint8_t duk_initjs_data[187];
  1530. #endif /* DUK_USE_BUILTIN_INITJS */
  1531. #endif /* !DUK_SINGLE_FILE */
  1532. #define DUK_BUILTINS_DATA_LENGTH 1952
  1533. #ifdef DUK_USE_BUILTIN_INITJS
  1534. #define DUK_BUILTIN_INITJS_DATA_LENGTH 187
  1535. #endif /* DUK_USE_BUILTIN_INITJS */
  1536. #define DUK_BIDX_GLOBAL 0
  1537. #define DUK_BIDX_GLOBAL_ENV 1
  1538. #define DUK_BIDX_OBJECT_CONSTRUCTOR 2
  1539. #define DUK_BIDX_OBJECT_PROTOTYPE 3
  1540. #define DUK_BIDX_FUNCTION_CONSTRUCTOR 4
  1541. #define DUK_BIDX_FUNCTION_PROTOTYPE 5
  1542. #define DUK_BIDX_ARRAY_CONSTRUCTOR 6
  1543. #define DUK_BIDX_ARRAY_PROTOTYPE 7
  1544. #define DUK_BIDX_STRING_CONSTRUCTOR 8
  1545. #define DUK_BIDX_STRING_PROTOTYPE 9
  1546. #define DUK_BIDX_BOOLEAN_CONSTRUCTOR 10
  1547. #define DUK_BIDX_BOOLEAN_PROTOTYPE 11
  1548. #define DUK_BIDX_NUMBER_CONSTRUCTOR 12
  1549. #define DUK_BIDX_NUMBER_PROTOTYPE 13
  1550. #define DUK_BIDX_DATE_CONSTRUCTOR 14
  1551. #define DUK_BIDX_DATE_PROTOTYPE 15
  1552. #define DUK_BIDX_REGEXP_CONSTRUCTOR 16
  1553. #define DUK_BIDX_REGEXP_PROTOTYPE 17
  1554. #define DUK_BIDX_ERROR_CONSTRUCTOR 18
  1555. #define DUK_BIDX_ERROR_PROTOTYPE 19
  1556. #define DUK_BIDX_EVAL_ERROR_CONSTRUCTOR 20
  1557. #define DUK_BIDX_EVAL_ERROR_PROTOTYPE 21
  1558. #define DUK_BIDX_RANGE_ERROR_CONSTRUCTOR 22
  1559. #define DUK_BIDX_RANGE_ERROR_PROTOTYPE 23
  1560. #define DUK_BIDX_REFERENCE_ERROR_CONSTRUCTOR 24
  1561. #define DUK_BIDX_REFERENCE_ERROR_PROTOTYPE 25
  1562. #define DUK_BIDX_SYNTAX_ERROR_CONSTRUCTOR 26
  1563. #define DUK_BIDX_SYNTAX_ERROR_PROTOTYPE 27
  1564. #define DUK_BIDX_TYPE_ERROR_CONSTRUCTOR 28
  1565. #define DUK_BIDX_TYPE_ERROR_PROTOTYPE 29
  1566. #define DUK_BIDX_URI_ERROR_CONSTRUCTOR 30
  1567. #define DUK_BIDX_URI_ERROR_PROTOTYPE 31
  1568. #define DUK_BIDX_MATH 32
  1569. #define DUK_BIDX_JSON 33
  1570. #define DUK_BIDX_TYPE_ERROR_THROWER 34
  1571. #define DUK_BIDX_PROXY_CONSTRUCTOR 35
  1572. #define DUK_BIDX_DUKTAPE 36
  1573. #define DUK_BIDX_THREAD_CONSTRUCTOR 37
  1574. #define DUK_BIDX_THREAD_PROTOTYPE 38
  1575. #define DUK_BIDX_BUFFER_CONSTRUCTOR 39
  1576. #define DUK_BIDX_BUFFER_PROTOTYPE 40
  1577. #define DUK_BIDX_POINTER_CONSTRUCTOR 41
  1578. #define DUK_BIDX_POINTER_PROTOTYPE 42
  1579. #define DUK_BIDX_LOGGER_CONSTRUCTOR 43
  1580. #define DUK_BIDX_LOGGER_PROTOTYPE 44
  1581. #define DUK_BIDX_DOUBLE_ERROR 45
  1582. #define DUK_BIDX_ARRAYBUFFER_CONSTRUCTOR 46
  1583. #define DUK_BIDX_ARRAYBUFFER_PROTOTYPE 47
  1584. #define DUK_BIDX_DATAVIEW_CONSTRUCTOR 48
  1585. #define DUK_BIDX_DATAVIEW_PROTOTYPE 49
  1586. #define DUK_BIDX_TYPEDARRAY_PROTOTYPE 50
  1587. #define DUK_BIDX_INT8ARRAY_CONSTRUCTOR 51
  1588. #define DUK_BIDX_INT8ARRAY_PROTOTYPE 52
  1589. #define DUK_BIDX_UINT8ARRAY_CONSTRUCTOR 53
  1590. #define DUK_BIDX_UINT8ARRAY_PROTOTYPE 54
  1591. #define DUK_BIDX_UINT8CLAMPEDARRAY_CONSTRUCTOR 55
  1592. #define DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE 56
  1593. #define DUK_BIDX_INT16ARRAY_CONSTRUCTOR 57
  1594. #define DUK_BIDX_INT16ARRAY_PROTOTYPE 58
  1595. #define DUK_BIDX_UINT16ARRAY_CONSTRUCTOR 59
  1596. #define DUK_BIDX_UINT16ARRAY_PROTOTYPE 60
  1597. #define DUK_BIDX_INT32ARRAY_CONSTRUCTOR 61
  1598. #define DUK_BIDX_INT32ARRAY_PROTOTYPE 62
  1599. #define DUK_BIDX_UINT32ARRAY_CONSTRUCTOR 63
  1600. #define DUK_BIDX_UINT32ARRAY_PROTOTYPE 64
  1601. #define DUK_BIDX_FLOAT32ARRAY_CONSTRUCTOR 65
  1602. #define DUK_BIDX_FLOAT32ARRAY_PROTOTYPE 66
  1603. #define DUK_BIDX_FLOAT64ARRAY_CONSTRUCTOR 67
  1604. #define DUK_BIDX_FLOAT64ARRAY_PROTOTYPE 68
  1605. #define DUK_BIDX_NODEJS_BUFFER_CONSTRUCTOR 69
  1606. #define DUK_BIDX_NODEJS_BUFFER_PROTOTYPE 70
  1607. #define DUK_NUM_BUILTINS 71
  1608. #elif defined(DUK_USE_DOUBLE_BE)
  1609. #if !defined(DUK_SINGLE_FILE)
  1610. DUK_INTERNAL_DECL const duk_uint8_t duk_strings_data[2624];
  1611. #endif /* !DUK_SINGLE_FILE */
  1612. #define DUK_STRDATA_DATA_LENGTH 2624
  1613. #define DUK_STRDATA_MAX_STRLEN 24
  1614. #define DUK_STRIDX_UC_LOGGER 0 /* 'Logger' */
  1615. #define DUK_STRIDX_UC_THREAD 1 /* 'Thread' */
  1616. #define DUK_STRIDX_UC_POINTER 2 /* 'Pointer' */
  1617. #define DUK_STRIDX_DEC_ENV 3 /* 'DecEnv' */
  1618. #define DUK_STRIDX_OBJ_ENV 4 /* 'ObjEnv' */
  1619. #define DUK_STRIDX_FLOAT64_ARRAY 5 /* 'Float64Array' */
  1620. #define DUK_STRIDX_FLOAT32_ARRAY 6 /* 'Float32Array' */
  1621. #define DUK_STRIDX_UINT32_ARRAY 7 /* 'Uint32Array' */
  1622. #define DUK_STRIDX_INT32_ARRAY 8 /* 'Int32Array' */
  1623. #define DUK_STRIDX_UINT16_ARRAY 9 /* 'Uint16Array' */
  1624. #define DUK_STRIDX_INT16_ARRAY 10 /* 'Int16Array' */
  1625. #define DUK_STRIDX_UINT8_CLAMPED_ARRAY 11 /* 'Uint8ClampedArray' */
  1626. #define DUK_STRIDX_UINT8_ARRAY 12 /* 'Uint8Array' */
  1627. #define DUK_STRIDX_INT8_ARRAY 13 /* 'Int8Array' */
  1628. #define DUK_STRIDX_DATA_VIEW 14 /* 'DataView' */
  1629. #define DUK_STRIDX_ARRAY_BUFFER 15 /* 'ArrayBuffer' */
  1630. #define DUK_STRIDX_UC_BUFFER 16 /* 'Buffer' */
  1631. #define DUK_STRIDX_EMPTY_STRING 17 /* '' */
  1632. #define DUK_STRIDX_GLOBAL 18 /* 'global' */
  1633. #define DUK_STRIDX_UC_ARGUMENTS 19 /* 'Arguments' */
  1634. #define DUK_STRIDX_JSON 20 /* 'JSON' */
  1635. #define DUK_STRIDX_MATH 21 /* 'Math' */
  1636. #define DUK_STRIDX_UC_ERROR 22 /* 'Error' */
  1637. #define DUK_STRIDX_REG_EXP 23 /* 'RegExp' */
  1638. #define DUK_STRIDX_DATE 24 /* 'Date' */
  1639. #define DUK_STRIDX_UC_NUMBER 25 /* 'Number' */
  1640. #define DUK_STRIDX_UC_BOOLEAN 26 /* 'Boolean' */
  1641. #define DUK_STRIDX_UC_STRING 27 /* 'String' */
  1642. #define DUK_STRIDX_ARRAY 28 /* 'Array' */
  1643. #define DUK_STRIDX_UC_FUNCTION 29 /* 'Function' */
  1644. #define DUK_STRIDX_UC_OBJECT 30 /* 'Object' */
  1645. #define DUK_STRIDX_UC_NULL 31 /* 'Null' */
  1646. #define DUK_STRIDX_UC_UNDEFINED 32 /* 'Undefined' */
  1647. #define DUK_STRIDX_JSON_EXT_FUNCTION2 33 /* '{_func:true}' */
  1648. #define DUK_STRIDX_JSON_EXT_FUNCTION1 34 /* '{"_func":true}' */
  1649. #define DUK_STRIDX_JSON_EXT_NEGINF 35 /* '{"_ninf":true}' */
  1650. #define DUK_STRIDX_JSON_EXT_POSINF 36 /* '{"_inf":true}' */
  1651. #define DUK_STRIDX_JSON_EXT_NAN 37 /* '{"_nan":true}' */
  1652. #define DUK_STRIDX_JSON_EXT_UNDEFINED 38 /* '{"_undef":true}' */
  1653. #define DUK_STRIDX_TO_LOG_STRING 39 /* 'toLogString' */
  1654. #define DUK_STRIDX_CLOG 40 /* 'clog' */
  1655. #define DUK_STRIDX_LC_L 41 /* 'l' */
  1656. #define DUK_STRIDX_LC_N 42 /* 'n' */
  1657. #define DUK_STRIDX_LC_FATAL 43 /* 'fatal' */
  1658. #define DUK_STRIDX_LC_ERROR 44 /* 'error' */
  1659. #define DUK_STRIDX_LC_WARN 45 /* 'warn' */
  1660. #define DUK_STRIDX_LC_DEBUG 46 /* 'debug' */
  1661. #define DUK_STRIDX_LC_TRACE 47 /* 'trace' */
  1662. #define DUK_STRIDX_RAW 48 /* 'raw' */
  1663. #define DUK_STRIDX_FMT 49 /* 'fmt' */
  1664. #define DUK_STRIDX_CURRENT 50 /* 'current' */
  1665. #define DUK_STRIDX_RESUME 51 /* 'resume' */
  1666. #define DUK_STRIDX_COMPACT 52 /* 'compact' */
  1667. #define DUK_STRIDX_JC 53 /* 'jc' */
  1668. #define DUK_STRIDX_JX 54 /* 'jx' */
  1669. #define DUK_STRIDX_BASE64 55 /* 'base64' */
  1670. #define DUK_STRIDX_HEX 56 /* 'hex' */
  1671. #define DUK_STRIDX_DEC 57 /* 'dec' */
  1672. #define DUK_STRIDX_ENC 58 /* 'enc' */
  1673. #define DUK_STRIDX_FIN 59 /* 'fin' */
  1674. #define DUK_STRIDX_GC 60 /* 'gc' */
  1675. #define DUK_STRIDX_ACT 61 /* 'act' */
  1676. #define DUK_STRIDX_LC_INFO 62 /* 'info' */
  1677. #define DUK_STRIDX_VERSION 63 /* 'version' */
  1678. #define DUK_STRIDX_ENV 64 /* 'env' */
  1679. #define DUK_STRIDX_MOD_LOADED 65 /* 'modLoaded' */
  1680. #define DUK_STRIDX_MOD_SEARCH 66 /* 'modSearch' */
  1681. #define DUK_STRIDX_ERR_THROW 67 /* 'errThrow' */
  1682. #define DUK_STRIDX_ERR_CREATE 68 /* 'errCreate' */
  1683. #define DUK_STRIDX_COMPILE 69 /* 'compile' */
  1684. #define DUK_STRIDX_INT_REGBASE 70 /* '\x00Regbase' */
  1685. #define DUK_STRIDX_INT_THREAD 71 /* '\x00Thread' */
  1686. #define DUK_STRIDX_INT_HANDLER 72 /* '\x00Handler' */
  1687. #define DUK_STRIDX_INT_FINALIZER 73 /* '\x00Finalizer' */
  1688. #define DUK_STRIDX_INT_CALLEE 74 /* '\x00Callee' */
  1689. #define DUK_STRIDX_INT_MAP 75 /* '\x00Map' */
  1690. #define DUK_STRIDX_INT_ARGS 76 /* '\x00Args' */
  1691. #define DUK_STRIDX_INT_THIS 77 /* '\x00This' */
  1692. #define DUK_STRIDX_INT_PC2LINE 78 /* '\x00Pc2line' */
  1693. #define DUK_STRIDX_INT_SOURCE 79 /* '\x00Source' */
  1694. #define DUK_STRIDX_INT_VARENV 80 /* '\x00Varenv' */
  1695. #define DUK_STRIDX_INT_LEXENV 81 /* '\x00Lexenv' */
  1696. #define DUK_STRIDX_INT_VARMAP 82 /* '\x00Varmap' */
  1697. #define DUK_STRIDX_INT_FORMALS 83 /* '\x00Formals' */
  1698. #define DUK_STRIDX_INT_BYTECODE 84 /* '\x00Bytecode' */
  1699. #define DUK_STRIDX_INT_NEXT 85 /* '\x00Next' */
  1700. #define DUK_STRIDX_INT_TARGET 86 /* '\x00Target' */
  1701. #define DUK_STRIDX_INT_VALUE 87 /* '\x00Value' */
  1702. #define DUK_STRIDX_LC_POINTER 88 /* 'pointer' */
  1703. #define DUK_STRIDX_INT_TRACEDATA 89 /* '\x00Tracedata' */
  1704. #define DUK_STRIDX_LINE_NUMBER 90 /* 'lineNumber' */
  1705. #define DUK_STRIDX_FILE_NAME 91 /* 'fileName' */
  1706. #define DUK_STRIDX_PC 92 /* 'pc' */
  1707. #define DUK_STRIDX_STACK 93 /* 'stack' */
  1708. #define DUK_STRIDX_THROW_TYPE_ERROR 94 /* 'ThrowTypeError' */
  1709. #define DUK_STRIDX_DUKTAPE 95 /* 'Duktape' */
  1710. #define DUK_STRIDX_SET_FLOAT64 96 /* 'setFloat64' */
  1711. #define DUK_STRIDX_SET_FLOAT32 97 /* 'setFloat32' */
  1712. #define DUK_STRIDX_SET_UINT32 98 /* 'setUint32' */
  1713. #define DUK_STRIDX_SET_INT32 99 /* 'setInt32' */
  1714. #define DUK_STRIDX_SET_UINT16 100 /* 'setUint16' */
  1715. #define DUK_STRIDX_SET_INT16 101 /* 'setInt16' */
  1716. #define DUK_STRIDX_SET_UINT8 102 /* 'setUint8' */
  1717. #define DUK_STRIDX_SET_INT8 103 /* 'setInt8' */
  1718. #define DUK_STRIDX_GET_FLOAT64 104 /* 'getFloat64' */
  1719. #define DUK_STRIDX_GET_FLOAT32 105 /* 'getFloat32' */
  1720. #define DUK_STRIDX_GET_UINT32 106 /* 'getUint32' */
  1721. #define DUK_STRIDX_GET_INT32 107 /* 'getInt32' */
  1722. #define DUK_STRIDX_GET_UINT16 108 /* 'getUint16' */
  1723. #define DUK_STRIDX_GET_INT16 109 /* 'getInt16' */
  1724. #define DUK_STRIDX_GET_UINT8 110 /* 'getUint8' */
  1725. #define DUK_STRIDX_GET_INT8 111 /* 'getInt8' */
  1726. #define DUK_STRIDX_SUBARRAY 112 /* 'subarray' */
  1727. #define DUK_STRIDX_BYTES_PER_ELEMENT 113 /* 'BYTES_PER_ELEMENT' */
  1728. #define DUK_STRIDX_BYTE_OFFSET 114 /* 'byteOffset' */
  1729. #define DUK_STRIDX_LC_BUFFER 115 /* 'buffer' */
  1730. #define DUK_STRIDX_IS_VIEW 116 /* 'isView' */
  1731. #define DUK_STRIDX_DATA 117 /* 'data' */
  1732. #define DUK_STRIDX_TYPE 118 /* 'type' */
  1733. #define DUK_STRIDX_WRITE_INT_BE 119 /* 'writeIntBE' */
  1734. #define DUK_STRIDX_WRITE_INT_LE 120 /* 'writeIntLE' */
  1735. #define DUK_STRIDX_WRITE_UINT_BE 121 /* 'writeUIntBE' */
  1736. #define DUK_STRIDX_WRITE_UINT_LE 122 /* 'writeUIntLE' */
  1737. #define DUK_STRIDX_WRITE_DOUBLE_BE 123 /* 'writeDoubleBE' */
  1738. #define DUK_STRIDX_WRITE_DOUBLE_LE 124 /* 'writeDoubleLE' */
  1739. #define DUK_STRIDX_WRITE_FLOAT_BE 125 /* 'writeFloatBE' */
  1740. #define DUK_STRIDX_WRITE_FLOAT_LE 126 /* 'writeFloatLE' */
  1741. #define DUK_STRIDX_WRITE_INT32_BE 127 /* 'writeInt32BE' */
  1742. #define DUK_STRIDX_WRITE_INT32_LE 128 /* 'writeInt32LE' */
  1743. #define DUK_STRIDX_WRITE_UINT32_BE 129 /* 'writeUInt32BE' */
  1744. #define DUK_STRIDX_WRITE_UINT32_LE 130 /* 'writeUInt32LE' */
  1745. #define DUK_STRIDX_WRITE_INT16_BE 131 /* 'writeInt16BE' */
  1746. #define DUK_STRIDX_WRITE_INT16_LE 132 /* 'writeInt16LE' */
  1747. #define DUK_STRIDX_WRITE_UINT16_BE 133 /* 'writeUInt16BE' */
  1748. #define DUK_STRIDX_WRITE_UINT16_LE 134 /* 'writeUInt16LE' */
  1749. #define DUK_STRIDX_WRITE_INT8 135 /* 'writeInt8' */
  1750. #define DUK_STRIDX_WRITE_UINT8 136 /* 'writeUInt8' */
  1751. #define DUK_STRIDX_READ_INT_BE 137 /* 'readIntBE' */
  1752. #define DUK_STRIDX_READ_INT_LE 138 /* 'readIntLE' */
  1753. #define DUK_STRIDX_READ_UINT_BE 139 /* 'readUIntBE' */
  1754. #define DUK_STRIDX_READ_UINT_LE 140 /* 'readUIntLE' */
  1755. #define DUK_STRIDX_READ_DOUBLE_BE 141 /* 'readDoubleBE' */
  1756. #define DUK_STRIDX_READ_DOUBLE_LE 142 /* 'readDoubleLE' */
  1757. #define DUK_STRIDX_READ_FLOAT_BE 143 /* 'readFloatBE' */
  1758. #define DUK_STRIDX_READ_FLOAT_LE 144 /* 'readFloatLE' */
  1759. #define DUK_STRIDX_READ_INT32_BE 145 /* 'readInt32BE' */
  1760. #define DUK_STRIDX_READ_INT32_LE 146 /* 'readInt32LE' */
  1761. #define DUK_STRIDX_READ_UINT32_BE 147 /* 'readUInt32BE' */
  1762. #define DUK_STRIDX_READ_UINT32_LE 148 /* 'readUInt32LE' */
  1763. #define DUK_STRIDX_READ_INT16_BE 149 /* 'readInt16BE' */
  1764. #define DUK_STRIDX_READ_INT16_LE 150 /* 'readInt16LE' */
  1765. #define DUK_STRIDX_READ_UINT16_BE 151 /* 'readUInt16BE' */
  1766. #define DUK_STRIDX_READ_UINT16_LE 152 /* 'readUInt16LE' */
  1767. #define DUK_STRIDX_READ_INT8 153 /* 'readInt8' */
  1768. #define DUK_STRIDX_READ_UINT8 154 /* 'readUInt8' */
  1769. #define DUK_STRIDX_COPY 155 /* 'copy' */
  1770. #define DUK_STRIDX_EQUALS 156 /* 'equals' */
  1771. #define DUK_STRIDX_FILL 157 /* 'fill' */
  1772. #define DUK_STRIDX_WRITE 158 /* 'write' */
  1773. #define DUK_STRIDX_COMPARE 159 /* 'compare' */
  1774. #define DUK_STRIDX_BYTE_LENGTH 160 /* 'byteLength' */
  1775. #define DUK_STRIDX_IS_BUFFER 161 /* 'isBuffer' */
  1776. #define DUK_STRIDX_IS_ENCODING 162 /* 'isEncoding' */
  1777. #define DUK_STRIDX_EXPORTS 163 /* 'exports' */
  1778. #define DUK_STRIDX_ID 164 /* 'id' */
  1779. #define DUK_STRIDX_REQUIRE 165 /* 'require' */
  1780. #define DUK_STRIDX___PROTO__ 166 /* '__proto__' */
  1781. #define DUK_STRIDX_SET_PROTOTYPE_OF 167 /* 'setPrototypeOf' */
  1782. #define DUK_STRIDX_OWN_KEYS 168 /* 'ownKeys' */
  1783. #define DUK_STRIDX_ENUMERATE 169 /* 'enumerate' */
  1784. #define DUK_STRIDX_DELETE_PROPERTY 170 /* 'deleteProperty' */
  1785. #define DUK_STRIDX_HAS 171 /* 'has' */
  1786. #define DUK_STRIDX_PROXY 172 /* 'Proxy' */
  1787. #define DUK_STRIDX_CALLEE 173 /* 'callee' */
  1788. #define DUK_STRIDX_INVALID_DATE 174 /* 'Invalid Date' */
  1789. #define DUK_STRIDX_BRACKETED_ELLIPSIS 175 /* '[...]' */
  1790. #define DUK_STRIDX_NEWLINE_TAB 176 /* '\n\t' */
  1791. #define DUK_STRIDX_SPACE 177 /* ' ' */
  1792. #define DUK_STRIDX_COMMA 178 /* ',' */
  1793. #define DUK_STRIDX_MINUS_ZERO 179 /* '-0' */
  1794. #define DUK_STRIDX_PLUS_ZERO 180 /* '+0' */
  1795. #define DUK_STRIDX_ZERO 181 /* '0' */
  1796. #define DUK_STRIDX_MINUS_INFINITY 182 /* '-Infinity' */
  1797. #define DUK_STRIDX_PLUS_INFINITY 183 /* '+Infinity' */
  1798. #define DUK_STRIDX_INFINITY 184 /* 'Infinity' */
  1799. #define DUK_STRIDX_LC_OBJECT 185 /* 'object' */
  1800. #define DUK_STRIDX_LC_STRING 186 /* 'string' */
  1801. #define DUK_STRIDX_LC_NUMBER 187 /* 'number' */
  1802. #define DUK_STRIDX_LC_BOOLEAN 188 /* 'boolean' */
  1803. #define DUK_STRIDX_LC_UNDEFINED 189 /* 'undefined' */
  1804. #define DUK_STRIDX_STRINGIFY 190 /* 'stringify' */
  1805. #define DUK_STRIDX_TAN 191 /* 'tan' */
  1806. #define DUK_STRIDX_SQRT 192 /* 'sqrt' */
  1807. #define DUK_STRIDX_SIN 193 /* 'sin' */
  1808. #define DUK_STRIDX_ROUND 194 /* 'round' */
  1809. #define DUK_STRIDX_RANDOM 195 /* 'random' */
  1810. #define DUK_STRIDX_POW 196 /* 'pow' */
  1811. #define DUK_STRIDX_MIN 197 /* 'min' */
  1812. #define DUK_STRIDX_MAX 198 /* 'max' */
  1813. #define DUK_STRIDX_LOG 199 /* 'log' */
  1814. #define DUK_STRIDX_FLOOR 200 /* 'floor' */
  1815. #define DUK_STRIDX_EXP 201 /* 'exp' */
  1816. #define DUK_STRIDX_COS 202 /* 'cos' */
  1817. #define DUK_STRIDX_CEIL 203 /* 'ceil' */
  1818. #define DUK_STRIDX_ATAN2 204 /* 'atan2' */
  1819. #define DUK_STRIDX_ATAN 205 /* 'atan' */
  1820. #define DUK_STRIDX_ASIN 206 /* 'asin' */
  1821. #define DUK_STRIDX_ACOS 207 /* 'acos' */
  1822. #define DUK_STRIDX_ABS 208 /* 'abs' */
  1823. #define DUK_STRIDX_SQRT2 209 /* 'SQRT2' */
  1824. #define DUK_STRIDX_SQRT1_2 210 /* 'SQRT1_2' */
  1825. #define DUK_STRIDX_PI 211 /* 'PI' */
  1826. #define DUK_STRIDX_LOG10E 212 /* 'LOG10E' */
  1827. #define DUK_STRIDX_LOG2E 213 /* 'LOG2E' */
  1828. #define DUK_STRIDX_LN2 214 /* 'LN2' */
  1829. #define DUK_STRIDX_LN10 215 /* 'LN10' */
  1830. #define DUK_STRIDX_E 216 /* 'E' */
  1831. #define DUK_STRIDX_MESSAGE 217 /* 'message' */
  1832. #define DUK_STRIDX_NAME 218 /* 'name' */
  1833. #define DUK_STRIDX_INPUT 219 /* 'input' */
  1834. #define DUK_STRIDX_INDEX 220 /* 'index' */
  1835. #define DUK_STRIDX_ESCAPED_EMPTY_REGEXP 221 /* '(?:)' */
  1836. #define DUK_STRIDX_LAST_INDEX 222 /* 'lastIndex' */
  1837. #define DUK_STRIDX_MULTILINE 223 /* 'multiline' */
  1838. #define DUK_STRIDX_IGNORE_CASE 224 /* 'ignoreCase' */
  1839. #define DUK_STRIDX_SOURCE 225 /* 'source' */
  1840. #define DUK_STRIDX_TEST 226 /* 'test' */
  1841. #define DUK_STRIDX_EXEC 227 /* 'exec' */
  1842. #define DUK_STRIDX_TO_GMT_STRING 228 /* 'toGMTString' */
  1843. #define DUK_STRIDX_SET_YEAR 229 /* 'setYear' */
  1844. #define DUK_STRIDX_GET_YEAR 230 /* 'getYear' */
  1845. #define DUK_STRIDX_TO_JSON 231 /* 'toJSON' */
  1846. #define DUK_STRIDX_TO_ISO_STRING 232 /* 'toISOString' */
  1847. #define DUK_STRIDX_TO_UTC_STRING 233 /* 'toUTCString' */
  1848. #define DUK_STRIDX_SET_UTC_FULL_YEAR 234 /* 'setUTCFullYear' */
  1849. #define DUK_STRIDX_SET_FULL_YEAR 235 /* 'setFullYear' */
  1850. #define DUK_STRIDX_SET_UTC_MONTH 236 /* 'setUTCMonth' */
  1851. #define DUK_STRIDX_SET_MONTH 237 /* 'setMonth' */
  1852. #define DUK_STRIDX_SET_UTC_DATE 238 /* 'setUTCDate' */
  1853. #define DUK_STRIDX_SET_DATE 239 /* 'setDate' */
  1854. #define DUK_STRIDX_SET_UTC_HOURS 240 /* 'setUTCHours' */
  1855. #define DUK_STRIDX_SET_HOURS 241 /* 'setHours' */
  1856. #define DUK_STRIDX_SET_UTC_MINUTES 242 /* 'setUTCMinutes' */
  1857. #define DUK_STRIDX_SET_MINUTES 243 /* 'setMinutes' */
  1858. #define DUK_STRIDX_SET_UTC_SECONDS 244 /* 'setUTCSeconds' */
  1859. #define DUK_STRIDX_SET_SECONDS 245 /* 'setSeconds' */
  1860. #define DUK_STRIDX_SET_UTC_MILLISECONDS 246 /* 'setUTCMilliseconds' */
  1861. #define DUK_STRIDX_SET_MILLISECONDS 247 /* 'setMilliseconds' */
  1862. #define DUK_STRIDX_SET_TIME 248 /* 'setTime' */
  1863. #define DUK_STRIDX_GET_TIMEZONE_OFFSET 249 /* 'getTimezoneOffset' */
  1864. #define DUK_STRIDX_GET_UTC_MILLISECONDS 250 /* 'getUTCMilliseconds' */
  1865. #define DUK_STRIDX_GET_MILLISECONDS 251 /* 'getMilliseconds' */
  1866. #define DUK_STRIDX_GET_UTC_SECONDS 252 /* 'getUTCSeconds' */
  1867. #define DUK_STRIDX_GET_SECONDS 253 /* 'getSeconds' */
  1868. #define DUK_STRIDX_GET_UTC_MINUTES 254 /* 'getUTCMinutes' */
  1869. #define DUK_STRIDX_GET_MINUTES 255 /* 'getMinutes' */
  1870. #define DUK_STRIDX_GET_UTC_HOURS 256 /* 'getUTCHours' */
  1871. #define DUK_STRIDX_GET_HOURS 257 /* 'getHours' */
  1872. #define DUK_STRIDX_GET_UTC_DAY 258 /* 'getUTCDay' */
  1873. #define DUK_STRIDX_GET_DAY 259 /* 'getDay' */
  1874. #define DUK_STRIDX_GET_UTC_DATE 260 /* 'getUTCDate' */
  1875. #define DUK_STRIDX_GET_DATE 261 /* 'getDate' */
  1876. #define DUK_STRIDX_GET_UTC_MONTH 262 /* 'getUTCMonth' */
  1877. #define DUK_STRIDX_GET_MONTH 263 /* 'getMonth' */
  1878. #define DUK_STRIDX_GET_UTC_FULL_YEAR 264 /* 'getUTCFullYear' */
  1879. #define DUK_STRIDX_GET_FULL_YEAR 265 /* 'getFullYear' */
  1880. #define DUK_STRIDX_GET_TIME 266 /* 'getTime' */
  1881. #define DUK_STRIDX_TO_LOCALE_TIME_STRING 267 /* 'toLocaleTimeString' */
  1882. #define DUK_STRIDX_TO_LOCALE_DATE_STRING 268 /* 'toLocaleDateString' */
  1883. #define DUK_STRIDX_TO_TIME_STRING 269 /* 'toTimeString' */
  1884. #define DUK_STRIDX_TO_DATE_STRING 270 /* 'toDateString' */
  1885. #define DUK_STRIDX_NOW 271 /* 'now' */
  1886. #define DUK_STRIDX_UTC 272 /* 'UTC' */
  1887. #define DUK_STRIDX_PARSE 273 /* 'parse' */
  1888. #define DUK_STRIDX_TO_PRECISION 274 /* 'toPrecision' */
  1889. #define DUK_STRIDX_TO_EXPONENTIAL 275 /* 'toExponential' */
  1890. #define DUK_STRIDX_TO_FIXED 276 /* 'toFixed' */
  1891. #define DUK_STRIDX_POSITIVE_INFINITY 277 /* 'POSITIVE_INFINITY' */
  1892. #define DUK_STRIDX_NEGATIVE_INFINITY 278 /* 'NEGATIVE_INFINITY' */
  1893. #define DUK_STRIDX_NAN 279 /* 'NaN' */
  1894. #define DUK_STRIDX_MIN_VALUE 280 /* 'MIN_VALUE' */
  1895. #define DUK_STRIDX_MAX_VALUE 281 /* 'MAX_VALUE' */
  1896. #define DUK_STRIDX_SUBSTR 282 /* 'substr' */
  1897. #define DUK_STRIDX_TRIM 283 /* 'trim' */
  1898. #define DUK_STRIDX_TO_LOCALE_UPPER_CASE 284 /* 'toLocaleUpperCase' */
  1899. #define DUK_STRIDX_TO_UPPER_CASE 285 /* 'toUpperCase' */
  1900. #define DUK_STRIDX_TO_LOCALE_LOWER_CASE 286 /* 'toLocaleLowerCase' */
  1901. #define DUK_STRIDX_TO_LOWER_CASE 287 /* 'toLowerCase' */
  1902. #define DUK_STRIDX_SUBSTRING 288 /* 'substring' */
  1903. #define DUK_STRIDX_SPLIT 289 /* 'split' */
  1904. #define DUK_STRIDX_SEARCH 290 /* 'search' */
  1905. #define DUK_STRIDX_REPLACE 291 /* 'replace' */
  1906. #define DUK_STRIDX_MATCH 292 /* 'match' */
  1907. #define DUK_STRIDX_LOCALE_COMPARE 293 /* 'localeCompare' */
  1908. #define DUK_STRIDX_CHAR_CODE_AT 294 /* 'charCodeAt' */
  1909. #define DUK_STRIDX_CHAR_AT 295 /* 'charAt' */
  1910. #define DUK_STRIDX_FROM_CHAR_CODE 296 /* 'fromCharCode' */
  1911. #define DUK_STRIDX_REDUCE_RIGHT 297 /* 'reduceRight' */
  1912. #define DUK_STRIDX_REDUCE 298 /* 'reduce' */
  1913. #define DUK_STRIDX_FILTER 299 /* 'filter' */
  1914. #define DUK_STRIDX_MAP 300 /* 'map' */
  1915. #define DUK_STRIDX_FOR_EACH 301 /* 'forEach' */
  1916. #define DUK_STRIDX_SOME 302 /* 'some' */
  1917. #define DUK_STRIDX_EVERY 303 /* 'every' */
  1918. #define DUK_STRIDX_LAST_INDEX_OF 304 /* 'lastIndexOf' */
  1919. #define DUK_STRIDX_INDEX_OF 305 /* 'indexOf' */
  1920. #define DUK_STRIDX_UNSHIFT 306 /* 'unshift' */
  1921. #define DUK_STRIDX_SPLICE 307 /* 'splice' */
  1922. #define DUK_STRIDX_SORT 308 /* 'sort' */
  1923. #define DUK_STRIDX_SLICE 309 /* 'slice' */
  1924. #define DUK_STRIDX_SHIFT 310 /* 'shift' */
  1925. #define DUK_STRIDX_REVERSE 311 /* 'reverse' */
  1926. #define DUK_STRIDX_PUSH 312 /* 'push' */
  1927. #define DUK_STRIDX_POP 313 /* 'pop' */
  1928. #define DUK_STRIDX_JOIN 314 /* 'join' */
  1929. #define DUK_STRIDX_CONCAT 315 /* 'concat' */
  1930. #define DUK_STRIDX_IS_ARRAY 316 /* 'isArray' */
  1931. #define DUK_STRIDX_LC_ARGUMENTS 317 /* 'arguments' */
  1932. #define DUK_STRIDX_CALLER 318 /* 'caller' */
  1933. #define DUK_STRIDX_BIND 319 /* 'bind' */
  1934. #define DUK_STRIDX_CALL 320 /* 'call' */
  1935. #define DUK_STRIDX_APPLY 321 /* 'apply' */
  1936. #define DUK_STRIDX_PROPERTY_IS_ENUMERABLE 322 /* 'propertyIsEnumerable' */
  1937. #define DUK_STRIDX_IS_PROTOTYPE_OF 323 /* 'isPrototypeOf' */
  1938. #define DUK_STRIDX_HAS_OWN_PROPERTY 324 /* 'hasOwnProperty' */
  1939. #define DUK_STRIDX_VALUE_OF 325 /* 'valueOf' */
  1940. #define DUK_STRIDX_TO_LOCALE_STRING 326 /* 'toLocaleString' */
  1941. #define DUK_STRIDX_TO_STRING 327 /* 'toString' */
  1942. #define DUK_STRIDX_CONSTRUCTOR 328 /* 'constructor' */
  1943. #define DUK_STRIDX_SET 329 /* 'set' */
  1944. #define DUK_STRIDX_GET 330 /* 'get' */
  1945. #define DUK_STRIDX_ENUMERABLE 331 /* 'enumerable' */
  1946. #define DUK_STRIDX_CONFIGURABLE 332 /* 'configurable' */
  1947. #define DUK_STRIDX_WRITABLE 333 /* 'writable' */
  1948. #define DUK_STRIDX_VALUE 334 /* 'value' */
  1949. #define DUK_STRIDX_KEYS 335 /* 'keys' */
  1950. #define DUK_STRIDX_IS_EXTENSIBLE 336 /* 'isExtensible' */
  1951. #define DUK_STRIDX_IS_FROZEN 337 /* 'isFrozen' */
  1952. #define DUK_STRIDX_IS_SEALED 338 /* 'isSealed' */
  1953. #define DUK_STRIDX_PREVENT_EXTENSIONS 339 /* 'preventExtensions' */
  1954. #define DUK_STRIDX_FREEZE 340 /* 'freeze' */
  1955. #define DUK_STRIDX_SEAL 341 /* 'seal' */
  1956. #define DUK_STRIDX_DEFINE_PROPERTIES 342 /* 'defineProperties' */
  1957. #define DUK_STRIDX_DEFINE_PROPERTY 343 /* 'defineProperty' */
  1958. #define DUK_STRIDX_CREATE 344 /* 'create' */
  1959. #define DUK_STRIDX_GET_OWN_PROPERTY_NAMES 345 /* 'getOwnPropertyNames' */
  1960. #define DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR 346 /* 'getOwnPropertyDescriptor' */
  1961. #define DUK_STRIDX_GET_PROTOTYPE_OF 347 /* 'getPrototypeOf' */
  1962. #define DUK_STRIDX_PROTOTYPE 348 /* 'prototype' */
  1963. #define DUK_STRIDX_LENGTH 349 /* 'length' */
  1964. #define DUK_STRIDX_ALERT 350 /* 'alert' */
  1965. #define DUK_STRIDX_PRINT 351 /* 'print' */
  1966. #define DUK_STRIDX_UNESCAPE 352 /* 'unescape' */
  1967. #define DUK_STRIDX_ESCAPE 353 /* 'escape' */
  1968. #define DUK_STRIDX_ENCODE_URI_COMPONENT 354 /* 'encodeURIComponent' */
  1969. #define DUK_STRIDX_ENCODE_URI 355 /* 'encodeURI' */
  1970. #define DUK_STRIDX_DECODE_URI_COMPONENT 356 /* 'decodeURIComponent' */
  1971. #define DUK_STRIDX_DECODE_URI 357 /* 'decodeURI' */
  1972. #define DUK_STRIDX_IS_FINITE 358 /* 'isFinite' */
  1973. #define DUK_STRIDX_IS_NAN 359 /* 'isNaN' */
  1974. #define DUK_STRIDX_PARSE_FLOAT 360 /* 'parseFloat' */
  1975. #define DUK_STRIDX_PARSE_INT 361 /* 'parseInt' */
  1976. #define DUK_STRIDX_EVAL 362 /* 'eval' */
  1977. #define DUK_STRIDX_URI_ERROR 363 /* 'URIError' */
  1978. #define DUK_STRIDX_TYPE_ERROR 364 /* 'TypeError' */
  1979. #define DUK_STRIDX_SYNTAX_ERROR 365 /* 'SyntaxError' */
  1980. #define DUK_STRIDX_REFERENCE_ERROR 366 /* 'ReferenceError' */
  1981. #define DUK_STRIDX_RANGE_ERROR 367 /* 'RangeError' */
  1982. #define DUK_STRIDX_EVAL_ERROR 368 /* 'EvalError' */
  1983. #define DUK_STRIDX_BREAK 369 /* 'break' */
  1984. #define DUK_STRIDX_CASE 370 /* 'case' */
  1985. #define DUK_STRIDX_CATCH 371 /* 'catch' */
  1986. #define DUK_STRIDX_CONTINUE 372 /* 'continue' */
  1987. #define DUK_STRIDX_DEBUGGER 373 /* 'debugger' */
  1988. #define DUK_STRIDX_DEFAULT 374 /* 'default' */
  1989. #define DUK_STRIDX_DELETE 375 /* 'delete' */
  1990. #define DUK_STRIDX_DO 376 /* 'do' */
  1991. #define DUK_STRIDX_ELSE 377 /* 'else' */
  1992. #define DUK_STRIDX_FINALLY 378 /* 'finally' */
  1993. #define DUK_STRIDX_FOR 379 /* 'for' */
  1994. #define DUK_STRIDX_LC_FUNCTION 380 /* 'function' */
  1995. #define DUK_STRIDX_IF 381 /* 'if' */
  1996. #define DUK_STRIDX_IN 382 /* 'in' */
  1997. #define DUK_STRIDX_INSTANCEOF 383 /* 'instanceof' */
  1998. #define DUK_STRIDX_NEW 384 /* 'new' */
  1999. #define DUK_STRIDX_RETURN 385 /* 'return' */
  2000. #define DUK_STRIDX_SWITCH 386 /* 'switch' */
  2001. #define DUK_STRIDX_THIS 387 /* 'this' */
  2002. #define DUK_STRIDX_THROW 388 /* 'throw' */
  2003. #define DUK_STRIDX_TRY 389 /* 'try' */
  2004. #define DUK_STRIDX_TYPEOF 390 /* 'typeof' */
  2005. #define DUK_STRIDX_VAR 391 /* 'var' */
  2006. #define DUK_STRIDX_VOID 392 /* 'void' */
  2007. #define DUK_STRIDX_WHILE 393 /* 'while' */
  2008. #define DUK_STRIDX_WITH 394 /* 'with' */
  2009. #define DUK_STRIDX_CLASS 395 /* 'class' */
  2010. #define DUK_STRIDX_CONST 396 /* 'const' */
  2011. #define DUK_STRIDX_ENUM 397 /* 'enum' */
  2012. #define DUK_STRIDX_EXPORT 398 /* 'export' */
  2013. #define DUK_STRIDX_EXTENDS 399 /* 'extends' */
  2014. #define DUK_STRIDX_IMPORT 400 /* 'import' */
  2015. #define DUK_STRIDX_SUPER 401 /* 'super' */
  2016. #define DUK_STRIDX_LC_NULL 402 /* 'null' */
  2017. #define DUK_STRIDX_TRUE 403 /* 'true' */
  2018. #define DUK_STRIDX_FALSE 404 /* 'false' */
  2019. #define DUK_STRIDX_IMPLEMENTS 405 /* 'implements' */
  2020. #define DUK_STRIDX_INTERFACE 406 /* 'interface' */
  2021. #define DUK_STRIDX_LET 407 /* 'let' */
  2022. #define DUK_STRIDX_PACKAGE 408 /* 'package' */
  2023. #define DUK_STRIDX_PRIVATE 409 /* 'private' */
  2024. #define DUK_STRIDX_PROTECTED 410 /* 'protected' */
  2025. #define DUK_STRIDX_PUBLIC 411 /* 'public' */
  2026. #define DUK_STRIDX_STATIC 412 /* 'static' */
  2027. #define DUK_STRIDX_YIELD 413 /* 'yield' */
  2028. #define DUK_HEAP_STRING_UC_LOGGER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_LOGGER)
  2029. #define DUK_HTHREAD_STRING_UC_LOGGER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_LOGGER)
  2030. #define DUK_HEAP_STRING_UC_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_THREAD)
  2031. #define DUK_HTHREAD_STRING_UC_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_THREAD)
  2032. #define DUK_HEAP_STRING_UC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_POINTER)
  2033. #define DUK_HTHREAD_STRING_UC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_POINTER)
  2034. #define DUK_HEAP_STRING_DEC_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEC_ENV)
  2035. #define DUK_HTHREAD_STRING_DEC_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEC_ENV)
  2036. #define DUK_HEAP_STRING_OBJ_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OBJ_ENV)
  2037. #define DUK_HTHREAD_STRING_OBJ_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OBJ_ENV)
  2038. #define DUK_HEAP_STRING_FLOAT64_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT64_ARRAY)
  2039. #define DUK_HTHREAD_STRING_FLOAT64_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT64_ARRAY)
  2040. #define DUK_HEAP_STRING_FLOAT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT32_ARRAY)
  2041. #define DUK_HTHREAD_STRING_FLOAT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT32_ARRAY)
  2042. #define DUK_HEAP_STRING_UINT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT32_ARRAY)
  2043. #define DUK_HTHREAD_STRING_UINT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT32_ARRAY)
  2044. #define DUK_HEAP_STRING_INT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT32_ARRAY)
  2045. #define DUK_HTHREAD_STRING_INT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT32_ARRAY)
  2046. #define DUK_HEAP_STRING_UINT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT16_ARRAY)
  2047. #define DUK_HTHREAD_STRING_UINT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT16_ARRAY)
  2048. #define DUK_HEAP_STRING_INT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT16_ARRAY)
  2049. #define DUK_HTHREAD_STRING_INT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT16_ARRAY)
  2050. #define DUK_HEAP_STRING_UINT8_CLAMPED_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_CLAMPED_ARRAY)
  2051. #define DUK_HTHREAD_STRING_UINT8_CLAMPED_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_CLAMPED_ARRAY)
  2052. #define DUK_HEAP_STRING_UINT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_ARRAY)
  2053. #define DUK_HTHREAD_STRING_UINT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_ARRAY)
  2054. #define DUK_HEAP_STRING_INT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT8_ARRAY)
  2055. #define DUK_HTHREAD_STRING_INT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT8_ARRAY)
  2056. #define DUK_HEAP_STRING_DATA_VIEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA_VIEW)
  2057. #define DUK_HTHREAD_STRING_DATA_VIEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA_VIEW)
  2058. #define DUK_HEAP_STRING_ARRAY_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY_BUFFER)
  2059. #define DUK_HTHREAD_STRING_ARRAY_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY_BUFFER)
  2060. #define DUK_HEAP_STRING_UC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BUFFER)
  2061. #define DUK_HTHREAD_STRING_UC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BUFFER)
  2062. #define DUK_HEAP_STRING_EMPTY_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EMPTY_STRING)
  2063. #define DUK_HTHREAD_STRING_EMPTY_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EMPTY_STRING)
  2064. #define DUK_HEAP_STRING_GLOBAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GLOBAL)
  2065. #define DUK_HTHREAD_STRING_GLOBAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GLOBAL)
  2066. #define DUK_HEAP_STRING_UC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ARGUMENTS)
  2067. #define DUK_HTHREAD_STRING_UC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ARGUMENTS)
  2068. #define DUK_HEAP_STRING_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON)
  2069. #define DUK_HTHREAD_STRING_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON)
  2070. #define DUK_HEAP_STRING_MATH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MATH)
  2071. #define DUK_HTHREAD_STRING_MATH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MATH)
  2072. #define DUK_HEAP_STRING_UC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ERROR)
  2073. #define DUK_HTHREAD_STRING_UC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ERROR)
  2074. #define DUK_HEAP_STRING_REG_EXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REG_EXP)
  2075. #define DUK_HTHREAD_STRING_REG_EXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REG_EXP)
  2076. #define DUK_HEAP_STRING_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATE)
  2077. #define DUK_HTHREAD_STRING_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATE)
  2078. #define DUK_HEAP_STRING_UC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NUMBER)
  2079. #define DUK_HTHREAD_STRING_UC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NUMBER)
  2080. #define DUK_HEAP_STRING_UC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BOOLEAN)
  2081. #define DUK_HTHREAD_STRING_UC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BOOLEAN)
  2082. #define DUK_HEAP_STRING_UC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_STRING)
  2083. #define DUK_HTHREAD_STRING_UC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_STRING)
  2084. #define DUK_HEAP_STRING_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY)
  2085. #define DUK_HTHREAD_STRING_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY)
  2086. #define DUK_HEAP_STRING_UC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_FUNCTION)
  2087. #define DUK_HTHREAD_STRING_UC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_FUNCTION)
  2088. #define DUK_HEAP_STRING_UC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_OBJECT)
  2089. #define DUK_HTHREAD_STRING_UC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_OBJECT)
  2090. #define DUK_HEAP_STRING_UC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NULL)
  2091. #define DUK_HTHREAD_STRING_UC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NULL)
  2092. #define DUK_HEAP_STRING_UC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_UNDEFINED)
  2093. #define DUK_HTHREAD_STRING_UC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_UNDEFINED)
  2094. #define DUK_HEAP_STRING_JSON_EXT_FUNCTION2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION2)
  2095. #define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION2)
  2096. #define DUK_HEAP_STRING_JSON_EXT_FUNCTION1(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION1)
  2097. #define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION1(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION1)
  2098. #define DUK_HEAP_STRING_JSON_EXT_NEGINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NEGINF)
  2099. #define DUK_HTHREAD_STRING_JSON_EXT_NEGINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NEGINF)
  2100. #define DUK_HEAP_STRING_JSON_EXT_POSINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_POSINF)
  2101. #define DUK_HTHREAD_STRING_JSON_EXT_POSINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_POSINF)
  2102. #define DUK_HEAP_STRING_JSON_EXT_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NAN)
  2103. #define DUK_HTHREAD_STRING_JSON_EXT_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NAN)
  2104. #define DUK_HEAP_STRING_JSON_EXT_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_UNDEFINED)
  2105. #define DUK_HTHREAD_STRING_JSON_EXT_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_UNDEFINED)
  2106. #define DUK_HEAP_STRING_TO_LOG_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOG_STRING)
  2107. #define DUK_HTHREAD_STRING_TO_LOG_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOG_STRING)
  2108. #define DUK_HEAP_STRING_CLOG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLOG)
  2109. #define DUK_HTHREAD_STRING_CLOG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLOG)
  2110. #define DUK_HEAP_STRING_LC_L(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_L)
  2111. #define DUK_HTHREAD_STRING_LC_L(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_L)
  2112. #define DUK_HEAP_STRING_LC_N(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_N)
  2113. #define DUK_HTHREAD_STRING_LC_N(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_N)
  2114. #define DUK_HEAP_STRING_LC_FATAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FATAL)
  2115. #define DUK_HTHREAD_STRING_LC_FATAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FATAL)
  2116. #define DUK_HEAP_STRING_LC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ERROR)
  2117. #define DUK_HTHREAD_STRING_LC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ERROR)
  2118. #define DUK_HEAP_STRING_LC_WARN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_WARN)
  2119. #define DUK_HTHREAD_STRING_LC_WARN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_WARN)
  2120. #define DUK_HEAP_STRING_LC_DEBUG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_DEBUG)
  2121. #define DUK_HTHREAD_STRING_LC_DEBUG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_DEBUG)
  2122. #define DUK_HEAP_STRING_LC_TRACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_TRACE)
  2123. #define DUK_HTHREAD_STRING_LC_TRACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_TRACE)
  2124. #define DUK_HEAP_STRING_RAW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RAW)
  2125. #define DUK_HTHREAD_STRING_RAW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RAW)
  2126. #define DUK_HEAP_STRING_FMT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FMT)
  2127. #define DUK_HTHREAD_STRING_FMT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FMT)
  2128. #define DUK_HEAP_STRING_CURRENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CURRENT)
  2129. #define DUK_HTHREAD_STRING_CURRENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CURRENT)
  2130. #define DUK_HEAP_STRING_RESUME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RESUME)
  2131. #define DUK_HTHREAD_STRING_RESUME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RESUME)
  2132. #define DUK_HEAP_STRING_COMPACT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPACT)
  2133. #define DUK_HTHREAD_STRING_COMPACT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPACT)
  2134. #define DUK_HEAP_STRING_JC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JC)
  2135. #define DUK_HTHREAD_STRING_JC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JC)
  2136. #define DUK_HEAP_STRING_JX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JX)
  2137. #define DUK_HTHREAD_STRING_JX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JX)
  2138. #define DUK_HEAP_STRING_BASE64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BASE64)
  2139. #define DUK_HTHREAD_STRING_BASE64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BASE64)
  2140. #define DUK_HEAP_STRING_HEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HEX)
  2141. #define DUK_HTHREAD_STRING_HEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HEX)
  2142. #define DUK_HEAP_STRING_DEC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEC)
  2143. #define DUK_HTHREAD_STRING_DEC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEC)
  2144. #define DUK_HEAP_STRING_ENC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENC)
  2145. #define DUK_HTHREAD_STRING_ENC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENC)
  2146. #define DUK_HEAP_STRING_FIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FIN)
  2147. #define DUK_HTHREAD_STRING_FIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FIN)
  2148. #define DUK_HEAP_STRING_GC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GC)
  2149. #define DUK_HTHREAD_STRING_GC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GC)
  2150. #define DUK_HEAP_STRING_ACT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ACT)
  2151. #define DUK_HTHREAD_STRING_ACT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ACT)
  2152. #define DUK_HEAP_STRING_LC_INFO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_INFO)
  2153. #define DUK_HTHREAD_STRING_LC_INFO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_INFO)
  2154. #define DUK_HEAP_STRING_VERSION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VERSION)
  2155. #define DUK_HTHREAD_STRING_VERSION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VERSION)
  2156. #define DUK_HEAP_STRING_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENV)
  2157. #define DUK_HTHREAD_STRING_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENV)
  2158. #define DUK_HEAP_STRING_MOD_LOADED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MOD_LOADED)
  2159. #define DUK_HTHREAD_STRING_MOD_LOADED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MOD_LOADED)
  2160. #define DUK_HEAP_STRING_MOD_SEARCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MOD_SEARCH)
  2161. #define DUK_HTHREAD_STRING_MOD_SEARCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MOD_SEARCH)
  2162. #define DUK_HEAP_STRING_ERR_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_THROW)
  2163. #define DUK_HTHREAD_STRING_ERR_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_THROW)
  2164. #define DUK_HEAP_STRING_ERR_CREATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_CREATE)
  2165. #define DUK_HTHREAD_STRING_ERR_CREATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_CREATE)
  2166. #define DUK_HEAP_STRING_COMPILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPILE)
  2167. #define DUK_HTHREAD_STRING_COMPILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPILE)
  2168. #define DUK_HEAP_STRING_INT_REGBASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_REGBASE)
  2169. #define DUK_HTHREAD_STRING_INT_REGBASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_REGBASE)
  2170. #define DUK_HEAP_STRING_INT_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_THREAD)
  2171. #define DUK_HTHREAD_STRING_INT_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_THREAD)
  2172. #define DUK_HEAP_STRING_INT_HANDLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_HANDLER)
  2173. #define DUK_HTHREAD_STRING_INT_HANDLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_HANDLER)
  2174. #define DUK_HEAP_STRING_INT_FINALIZER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FINALIZER)
  2175. #define DUK_HTHREAD_STRING_INT_FINALIZER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FINALIZER)
  2176. #define DUK_HEAP_STRING_INT_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_CALLEE)
  2177. #define DUK_HTHREAD_STRING_INT_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_CALLEE)
  2178. #define DUK_HEAP_STRING_INT_MAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_MAP)
  2179. #define DUK_HTHREAD_STRING_INT_MAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_MAP)
  2180. #define DUK_HEAP_STRING_INT_ARGS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_ARGS)
  2181. #define DUK_HTHREAD_STRING_INT_ARGS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_ARGS)
  2182. #define DUK_HEAP_STRING_INT_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_THIS)
  2183. #define DUK_HTHREAD_STRING_INT_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_THIS)
  2184. #define DUK_HEAP_STRING_INT_PC2LINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_PC2LINE)
  2185. #define DUK_HTHREAD_STRING_INT_PC2LINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_PC2LINE)
  2186. #define DUK_HEAP_STRING_INT_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_SOURCE)
  2187. #define DUK_HTHREAD_STRING_INT_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_SOURCE)
  2188. #define DUK_HEAP_STRING_INT_VARENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARENV)
  2189. #define DUK_HTHREAD_STRING_INT_VARENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARENV)
  2190. #define DUK_HEAP_STRING_INT_LEXENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_LEXENV)
  2191. #define DUK_HTHREAD_STRING_INT_LEXENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_LEXENV)
  2192. #define DUK_HEAP_STRING_INT_VARMAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARMAP)
  2193. #define DUK_HTHREAD_STRING_INT_VARMAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARMAP)
  2194. #define DUK_HEAP_STRING_INT_FORMALS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FORMALS)
  2195. #define DUK_HTHREAD_STRING_INT_FORMALS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FORMALS)
  2196. #define DUK_HEAP_STRING_INT_BYTECODE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_BYTECODE)
  2197. #define DUK_HTHREAD_STRING_INT_BYTECODE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_BYTECODE)
  2198. #define DUK_HEAP_STRING_INT_NEXT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_NEXT)
  2199. #define DUK_HTHREAD_STRING_INT_NEXT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_NEXT)
  2200. #define DUK_HEAP_STRING_INT_TARGET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TARGET)
  2201. #define DUK_HTHREAD_STRING_INT_TARGET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TARGET)
  2202. #define DUK_HEAP_STRING_INT_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VALUE)
  2203. #define DUK_HTHREAD_STRING_INT_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VALUE)
  2204. #define DUK_HEAP_STRING_LC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_POINTER)
  2205. #define DUK_HTHREAD_STRING_LC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_POINTER)
  2206. #define DUK_HEAP_STRING_INT_TRACEDATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TRACEDATA)
  2207. #define DUK_HTHREAD_STRING_INT_TRACEDATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TRACEDATA)
  2208. #define DUK_HEAP_STRING_LINE_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LINE_NUMBER)
  2209. #define DUK_HTHREAD_STRING_LINE_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LINE_NUMBER)
  2210. #define DUK_HEAP_STRING_FILE_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILE_NAME)
  2211. #define DUK_HTHREAD_STRING_FILE_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILE_NAME)
  2212. #define DUK_HEAP_STRING_PC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PC)
  2213. #define DUK_HTHREAD_STRING_PC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PC)
  2214. #define DUK_HEAP_STRING_STACK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STACK)
  2215. #define DUK_HTHREAD_STRING_STACK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STACK)
  2216. #define DUK_HEAP_STRING_THROW_TYPE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THROW_TYPE_ERROR)
  2217. #define DUK_HTHREAD_STRING_THROW_TYPE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THROW_TYPE_ERROR)
  2218. #define DUK_HEAP_STRING_DUKTAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DUKTAPE)
  2219. #define DUK_HTHREAD_STRING_DUKTAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DUKTAPE)
  2220. #define DUK_HEAP_STRING_SET_FLOAT64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FLOAT64)
  2221. #define DUK_HTHREAD_STRING_SET_FLOAT64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FLOAT64)
  2222. #define DUK_HEAP_STRING_SET_FLOAT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FLOAT32)
  2223. #define DUK_HTHREAD_STRING_SET_FLOAT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FLOAT32)
  2224. #define DUK_HEAP_STRING_SET_UINT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT32)
  2225. #define DUK_HTHREAD_STRING_SET_UINT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT32)
  2226. #define DUK_HEAP_STRING_SET_INT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT32)
  2227. #define DUK_HTHREAD_STRING_SET_INT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT32)
  2228. #define DUK_HEAP_STRING_SET_UINT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT16)
  2229. #define DUK_HTHREAD_STRING_SET_UINT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT16)
  2230. #define DUK_HEAP_STRING_SET_INT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT16)
  2231. #define DUK_HTHREAD_STRING_SET_INT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT16)
  2232. #define DUK_HEAP_STRING_SET_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT8)
  2233. #define DUK_HTHREAD_STRING_SET_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT8)
  2234. #define DUK_HEAP_STRING_SET_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT8)
  2235. #define DUK_HTHREAD_STRING_SET_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT8)
  2236. #define DUK_HEAP_STRING_GET_FLOAT64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FLOAT64)
  2237. #define DUK_HTHREAD_STRING_GET_FLOAT64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FLOAT64)
  2238. #define DUK_HEAP_STRING_GET_FLOAT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FLOAT32)
  2239. #define DUK_HTHREAD_STRING_GET_FLOAT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FLOAT32)
  2240. #define DUK_HEAP_STRING_GET_UINT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT32)
  2241. #define DUK_HTHREAD_STRING_GET_UINT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT32)
  2242. #define DUK_HEAP_STRING_GET_INT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT32)
  2243. #define DUK_HTHREAD_STRING_GET_INT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT32)
  2244. #define DUK_HEAP_STRING_GET_UINT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT16)
  2245. #define DUK_HTHREAD_STRING_GET_UINT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT16)
  2246. #define DUK_HEAP_STRING_GET_INT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT16)
  2247. #define DUK_HTHREAD_STRING_GET_INT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT16)
  2248. #define DUK_HEAP_STRING_GET_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT8)
  2249. #define DUK_HTHREAD_STRING_GET_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT8)
  2250. #define DUK_HEAP_STRING_GET_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT8)
  2251. #define DUK_HTHREAD_STRING_GET_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT8)
  2252. #define DUK_HEAP_STRING_SUBARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBARRAY)
  2253. #define DUK_HTHREAD_STRING_SUBARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBARRAY)
  2254. #define DUK_HEAP_STRING_BYTES_PER_ELEMENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTES_PER_ELEMENT)
  2255. #define DUK_HTHREAD_STRING_BYTES_PER_ELEMENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTES_PER_ELEMENT)
  2256. #define DUK_HEAP_STRING_BYTE_OFFSET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTE_OFFSET)
  2257. #define DUK_HTHREAD_STRING_BYTE_OFFSET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTE_OFFSET)
  2258. #define DUK_HEAP_STRING_LC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BUFFER)
  2259. #define DUK_HTHREAD_STRING_LC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BUFFER)
  2260. #define DUK_HEAP_STRING_IS_VIEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_VIEW)
  2261. #define DUK_HTHREAD_STRING_IS_VIEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_VIEW)
  2262. #define DUK_HEAP_STRING_DATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA)
  2263. #define DUK_HTHREAD_STRING_DATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA)
  2264. #define DUK_HEAP_STRING_TYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPE)
  2265. #define DUK_HTHREAD_STRING_TYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPE)
  2266. #define DUK_HEAP_STRING_WRITE_INT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT_BE)
  2267. #define DUK_HTHREAD_STRING_WRITE_INT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT_BE)
  2268. #define DUK_HEAP_STRING_WRITE_INT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT_LE)
  2269. #define DUK_HTHREAD_STRING_WRITE_INT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT_LE)
  2270. #define DUK_HEAP_STRING_WRITE_UINT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT_BE)
  2271. #define DUK_HTHREAD_STRING_WRITE_UINT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT_BE)
  2272. #define DUK_HEAP_STRING_WRITE_UINT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT_LE)
  2273. #define DUK_HTHREAD_STRING_WRITE_UINT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT_LE)
  2274. #define DUK_HEAP_STRING_WRITE_DOUBLE_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_DOUBLE_BE)
  2275. #define DUK_HTHREAD_STRING_WRITE_DOUBLE_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_DOUBLE_BE)
  2276. #define DUK_HEAP_STRING_WRITE_DOUBLE_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_DOUBLE_LE)
  2277. #define DUK_HTHREAD_STRING_WRITE_DOUBLE_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_DOUBLE_LE)
  2278. #define DUK_HEAP_STRING_WRITE_FLOAT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_FLOAT_BE)
  2279. #define DUK_HTHREAD_STRING_WRITE_FLOAT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_FLOAT_BE)
  2280. #define DUK_HEAP_STRING_WRITE_FLOAT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_FLOAT_LE)
  2281. #define DUK_HTHREAD_STRING_WRITE_FLOAT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_FLOAT_LE)
  2282. #define DUK_HEAP_STRING_WRITE_INT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT32_BE)
  2283. #define DUK_HTHREAD_STRING_WRITE_INT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT32_BE)
  2284. #define DUK_HEAP_STRING_WRITE_INT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT32_LE)
  2285. #define DUK_HTHREAD_STRING_WRITE_INT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT32_LE)
  2286. #define DUK_HEAP_STRING_WRITE_UINT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT32_BE)
  2287. #define DUK_HTHREAD_STRING_WRITE_UINT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT32_BE)
  2288. #define DUK_HEAP_STRING_WRITE_UINT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT32_LE)
  2289. #define DUK_HTHREAD_STRING_WRITE_UINT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT32_LE)
  2290. #define DUK_HEAP_STRING_WRITE_INT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT16_BE)
  2291. #define DUK_HTHREAD_STRING_WRITE_INT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT16_BE)
  2292. #define DUK_HEAP_STRING_WRITE_INT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT16_LE)
  2293. #define DUK_HTHREAD_STRING_WRITE_INT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT16_LE)
  2294. #define DUK_HEAP_STRING_WRITE_UINT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT16_BE)
  2295. #define DUK_HTHREAD_STRING_WRITE_UINT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT16_BE)
  2296. #define DUK_HEAP_STRING_WRITE_UINT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT16_LE)
  2297. #define DUK_HTHREAD_STRING_WRITE_UINT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT16_LE)
  2298. #define DUK_HEAP_STRING_WRITE_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT8)
  2299. #define DUK_HTHREAD_STRING_WRITE_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT8)
  2300. #define DUK_HEAP_STRING_WRITE_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT8)
  2301. #define DUK_HTHREAD_STRING_WRITE_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT8)
  2302. #define DUK_HEAP_STRING_READ_INT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT_BE)
  2303. #define DUK_HTHREAD_STRING_READ_INT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT_BE)
  2304. #define DUK_HEAP_STRING_READ_INT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT_LE)
  2305. #define DUK_HTHREAD_STRING_READ_INT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT_LE)
  2306. #define DUK_HEAP_STRING_READ_UINT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT_BE)
  2307. #define DUK_HTHREAD_STRING_READ_UINT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT_BE)
  2308. #define DUK_HEAP_STRING_READ_UINT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT_LE)
  2309. #define DUK_HTHREAD_STRING_READ_UINT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT_LE)
  2310. #define DUK_HEAP_STRING_READ_DOUBLE_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_DOUBLE_BE)
  2311. #define DUK_HTHREAD_STRING_READ_DOUBLE_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_DOUBLE_BE)
  2312. #define DUK_HEAP_STRING_READ_DOUBLE_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_DOUBLE_LE)
  2313. #define DUK_HTHREAD_STRING_READ_DOUBLE_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_DOUBLE_LE)
  2314. #define DUK_HEAP_STRING_READ_FLOAT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_FLOAT_BE)
  2315. #define DUK_HTHREAD_STRING_READ_FLOAT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_FLOAT_BE)
  2316. #define DUK_HEAP_STRING_READ_FLOAT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_FLOAT_LE)
  2317. #define DUK_HTHREAD_STRING_READ_FLOAT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_FLOAT_LE)
  2318. #define DUK_HEAP_STRING_READ_INT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT32_BE)
  2319. #define DUK_HTHREAD_STRING_READ_INT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT32_BE)
  2320. #define DUK_HEAP_STRING_READ_INT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT32_LE)
  2321. #define DUK_HTHREAD_STRING_READ_INT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT32_LE)
  2322. #define DUK_HEAP_STRING_READ_UINT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT32_BE)
  2323. #define DUK_HTHREAD_STRING_READ_UINT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT32_BE)
  2324. #define DUK_HEAP_STRING_READ_UINT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT32_LE)
  2325. #define DUK_HTHREAD_STRING_READ_UINT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT32_LE)
  2326. #define DUK_HEAP_STRING_READ_INT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT16_BE)
  2327. #define DUK_HTHREAD_STRING_READ_INT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT16_BE)
  2328. #define DUK_HEAP_STRING_READ_INT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT16_LE)
  2329. #define DUK_HTHREAD_STRING_READ_INT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT16_LE)
  2330. #define DUK_HEAP_STRING_READ_UINT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT16_BE)
  2331. #define DUK_HTHREAD_STRING_READ_UINT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT16_BE)
  2332. #define DUK_HEAP_STRING_READ_UINT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT16_LE)
  2333. #define DUK_HTHREAD_STRING_READ_UINT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT16_LE)
  2334. #define DUK_HEAP_STRING_READ_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT8)
  2335. #define DUK_HTHREAD_STRING_READ_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT8)
  2336. #define DUK_HEAP_STRING_READ_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT8)
  2337. #define DUK_HTHREAD_STRING_READ_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT8)
  2338. #define DUK_HEAP_STRING_COPY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COPY)
  2339. #define DUK_HTHREAD_STRING_COPY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COPY)
  2340. #define DUK_HEAP_STRING_EQUALS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EQUALS)
  2341. #define DUK_HTHREAD_STRING_EQUALS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EQUALS)
  2342. #define DUK_HEAP_STRING_FILL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILL)
  2343. #define DUK_HTHREAD_STRING_FILL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILL)
  2344. #define DUK_HEAP_STRING_WRITE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE)
  2345. #define DUK_HTHREAD_STRING_WRITE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE)
  2346. #define DUK_HEAP_STRING_COMPARE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPARE)
  2347. #define DUK_HTHREAD_STRING_COMPARE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPARE)
  2348. #define DUK_HEAP_STRING_BYTE_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTE_LENGTH)
  2349. #define DUK_HTHREAD_STRING_BYTE_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTE_LENGTH)
  2350. #define DUK_HEAP_STRING_IS_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_BUFFER)
  2351. #define DUK_HTHREAD_STRING_IS_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_BUFFER)
  2352. #define DUK_HEAP_STRING_IS_ENCODING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_ENCODING)
  2353. #define DUK_HTHREAD_STRING_IS_ENCODING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_ENCODING)
  2354. #define DUK_HEAP_STRING_EXPORTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORTS)
  2355. #define DUK_HTHREAD_STRING_EXPORTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORTS)
  2356. #define DUK_HEAP_STRING_ID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ID)
  2357. #define DUK_HTHREAD_STRING_ID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ID)
  2358. #define DUK_HEAP_STRING_REQUIRE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REQUIRE)
  2359. #define DUK_HTHREAD_STRING_REQUIRE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REQUIRE)
  2360. #define DUK_HEAP_STRING___PROTO__(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX___PROTO__)
  2361. #define DUK_HTHREAD_STRING___PROTO__(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX___PROTO__)
  2362. #define DUK_HEAP_STRING_SET_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_PROTOTYPE_OF)
  2363. #define DUK_HTHREAD_STRING_SET_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_PROTOTYPE_OF)
  2364. #define DUK_HEAP_STRING_OWN_KEYS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OWN_KEYS)
  2365. #define DUK_HTHREAD_STRING_OWN_KEYS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OWN_KEYS)
  2366. #define DUK_HEAP_STRING_ENUMERATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERATE)
  2367. #define DUK_HTHREAD_STRING_ENUMERATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERATE)
  2368. #define DUK_HEAP_STRING_DELETE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE_PROPERTY)
  2369. #define DUK_HTHREAD_STRING_DELETE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE_PROPERTY)
  2370. #define DUK_HEAP_STRING_HAS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HAS)
  2371. #define DUK_HTHREAD_STRING_HAS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HAS)
  2372. #define DUK_HEAP_STRING_PROXY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROXY)
  2373. #define DUK_HTHREAD_STRING_PROXY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROXY)
  2374. #define DUK_HEAP_STRING_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLEE)
  2375. #define DUK_HTHREAD_STRING_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLEE)
  2376. #define DUK_HEAP_STRING_INVALID_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INVALID_DATE)
  2377. #define DUK_HTHREAD_STRING_INVALID_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INVALID_DATE)
  2378. #define DUK_HEAP_STRING_BRACKETED_ELLIPSIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BRACKETED_ELLIPSIS)
  2379. #define DUK_HTHREAD_STRING_BRACKETED_ELLIPSIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BRACKETED_ELLIPSIS)
  2380. #define DUK_HEAP_STRING_NEWLINE_TAB(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEWLINE_TAB)
  2381. #define DUK_HTHREAD_STRING_NEWLINE_TAB(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEWLINE_TAB)
  2382. #define DUK_HEAP_STRING_SPACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPACE)
  2383. #define DUK_HTHREAD_STRING_SPACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPACE)
  2384. #define DUK_HEAP_STRING_COMMA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMMA)
  2385. #define DUK_HTHREAD_STRING_COMMA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMMA)
  2386. #define DUK_HEAP_STRING_MINUS_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_ZERO)
  2387. #define DUK_HTHREAD_STRING_MINUS_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_ZERO)
  2388. #define DUK_HEAP_STRING_PLUS_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PLUS_ZERO)
  2389. #define DUK_HTHREAD_STRING_PLUS_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PLUS_ZERO)
  2390. #define DUK_HEAP_STRING_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ZERO)
  2391. #define DUK_HTHREAD_STRING_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ZERO)
  2392. #define DUK_HEAP_STRING_MINUS_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_INFINITY)
  2393. #define DUK_HTHREAD_STRING_MINUS_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_INFINITY)
  2394. #define DUK_HEAP_STRING_PLUS_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PLUS_INFINITY)
  2395. #define DUK_HTHREAD_STRING_PLUS_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PLUS_INFINITY)
  2396. #define DUK_HEAP_STRING_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INFINITY)
  2397. #define DUK_HTHREAD_STRING_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INFINITY)
  2398. #define DUK_HEAP_STRING_LC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_OBJECT)
  2399. #define DUK_HTHREAD_STRING_LC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_OBJECT)
  2400. #define DUK_HEAP_STRING_LC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_STRING)
  2401. #define DUK_HTHREAD_STRING_LC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_STRING)
  2402. #define DUK_HEAP_STRING_LC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NUMBER)
  2403. #define DUK_HTHREAD_STRING_LC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NUMBER)
  2404. #define DUK_HEAP_STRING_LC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BOOLEAN)
  2405. #define DUK_HTHREAD_STRING_LC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BOOLEAN)
  2406. #define DUK_HEAP_STRING_LC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_UNDEFINED)
  2407. #define DUK_HTHREAD_STRING_LC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_UNDEFINED)
  2408. #define DUK_HEAP_STRING_STRINGIFY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STRINGIFY)
  2409. #define DUK_HTHREAD_STRING_STRINGIFY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STRINGIFY)
  2410. #define DUK_HEAP_STRING_TAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TAN)
  2411. #define DUK_HTHREAD_STRING_TAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TAN)
  2412. #define DUK_HEAP_STRING_SQRT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT)
  2413. #define DUK_HTHREAD_STRING_SQRT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT)
  2414. #define DUK_HEAP_STRING_SIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SIN)
  2415. #define DUK_HTHREAD_STRING_SIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SIN)
  2416. #define DUK_HEAP_STRING_ROUND(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ROUND)
  2417. #define DUK_HTHREAD_STRING_ROUND(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ROUND)
  2418. #define DUK_HEAP_STRING_RANDOM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RANDOM)
  2419. #define DUK_HTHREAD_STRING_RANDOM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RANDOM)
  2420. #define DUK_HEAP_STRING_POW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POW)
  2421. #define DUK_HTHREAD_STRING_POW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POW)
  2422. #define DUK_HEAP_STRING_MIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MIN)
  2423. #define DUK_HTHREAD_STRING_MIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MIN)
  2424. #define DUK_HEAP_STRING_MAX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAX)
  2425. #define DUK_HTHREAD_STRING_MAX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAX)
  2426. #define DUK_HEAP_STRING_LOG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG)
  2427. #define DUK_HTHREAD_STRING_LOG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG)
  2428. #define DUK_HEAP_STRING_FLOOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOOR)
  2429. #define DUK_HTHREAD_STRING_FLOOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOOR)
  2430. #define DUK_HEAP_STRING_EXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXP)
  2431. #define DUK_HTHREAD_STRING_EXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXP)
  2432. #define DUK_HEAP_STRING_COS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COS)
  2433. #define DUK_HTHREAD_STRING_COS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COS)
  2434. #define DUK_HEAP_STRING_CEIL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CEIL)
  2435. #define DUK_HTHREAD_STRING_CEIL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CEIL)
  2436. #define DUK_HEAP_STRING_ATAN2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ATAN2)
  2437. #define DUK_HTHREAD_STRING_ATAN2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ATAN2)
  2438. #define DUK_HEAP_STRING_ATAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ATAN)
  2439. #define DUK_HTHREAD_STRING_ATAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ATAN)
  2440. #define DUK_HEAP_STRING_ASIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ASIN)
  2441. #define DUK_HTHREAD_STRING_ASIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ASIN)
  2442. #define DUK_HEAP_STRING_ACOS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ACOS)
  2443. #define DUK_HTHREAD_STRING_ACOS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ACOS)
  2444. #define DUK_HEAP_STRING_ABS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ABS)
  2445. #define DUK_HTHREAD_STRING_ABS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ABS)
  2446. #define DUK_HEAP_STRING_SQRT2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT2)
  2447. #define DUK_HTHREAD_STRING_SQRT2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT2)
  2448. #define DUK_HEAP_STRING_SQRT1_2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT1_2)
  2449. #define DUK_HTHREAD_STRING_SQRT1_2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT1_2)
  2450. #define DUK_HEAP_STRING_PI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PI)
  2451. #define DUK_HTHREAD_STRING_PI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PI)
  2452. #define DUK_HEAP_STRING_LOG10E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG10E)
  2453. #define DUK_HTHREAD_STRING_LOG10E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG10E)
  2454. #define DUK_HEAP_STRING_LOG2E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG2E)
  2455. #define DUK_HTHREAD_STRING_LOG2E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG2E)
  2456. #define DUK_HEAP_STRING_LN2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LN2)
  2457. #define DUK_HTHREAD_STRING_LN2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LN2)
  2458. #define DUK_HEAP_STRING_LN10(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LN10)
  2459. #define DUK_HTHREAD_STRING_LN10(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LN10)
  2460. #define DUK_HEAP_STRING_E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_E)
  2461. #define DUK_HTHREAD_STRING_E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_E)
  2462. #define DUK_HEAP_STRING_MESSAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MESSAGE)
  2463. #define DUK_HTHREAD_STRING_MESSAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MESSAGE)
  2464. #define DUK_HEAP_STRING_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAME)
  2465. #define DUK_HTHREAD_STRING_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAME)
  2466. #define DUK_HEAP_STRING_INPUT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INPUT)
  2467. #define DUK_HTHREAD_STRING_INPUT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INPUT)
  2468. #define DUK_HEAP_STRING_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INDEX)
  2469. #define DUK_HTHREAD_STRING_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INDEX)
  2470. #define DUK_HEAP_STRING_ESCAPED_EMPTY_REGEXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ESCAPED_EMPTY_REGEXP)
  2471. #define DUK_HTHREAD_STRING_ESCAPED_EMPTY_REGEXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ESCAPED_EMPTY_REGEXP)
  2472. #define DUK_HEAP_STRING_LAST_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LAST_INDEX)
  2473. #define DUK_HTHREAD_STRING_LAST_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LAST_INDEX)
  2474. #define DUK_HEAP_STRING_MULTILINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MULTILINE)
  2475. #define DUK_HTHREAD_STRING_MULTILINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MULTILINE)
  2476. #define DUK_HEAP_STRING_IGNORE_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IGNORE_CASE)
  2477. #define DUK_HTHREAD_STRING_IGNORE_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IGNORE_CASE)
  2478. #define DUK_HEAP_STRING_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SOURCE)
  2479. #define DUK_HTHREAD_STRING_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SOURCE)
  2480. #define DUK_HEAP_STRING_TEST(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TEST)
  2481. #define DUK_HTHREAD_STRING_TEST(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TEST)
  2482. #define DUK_HEAP_STRING_EXEC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXEC)
  2483. #define DUK_HTHREAD_STRING_EXEC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXEC)
  2484. #define DUK_HEAP_STRING_TO_GMT_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_GMT_STRING)
  2485. #define DUK_HTHREAD_STRING_TO_GMT_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_GMT_STRING)
  2486. #define DUK_HEAP_STRING_SET_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_YEAR)
  2487. #define DUK_HTHREAD_STRING_SET_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_YEAR)
  2488. #define DUK_HEAP_STRING_GET_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_YEAR)
  2489. #define DUK_HTHREAD_STRING_GET_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_YEAR)
  2490. #define DUK_HEAP_STRING_TO_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_JSON)
  2491. #define DUK_HTHREAD_STRING_TO_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_JSON)
  2492. #define DUK_HEAP_STRING_TO_ISO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_ISO_STRING)
  2493. #define DUK_HTHREAD_STRING_TO_ISO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_ISO_STRING)
  2494. #define DUK_HEAP_STRING_TO_UTC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_UTC_STRING)
  2495. #define DUK_HTHREAD_STRING_TO_UTC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_UTC_STRING)
  2496. #define DUK_HEAP_STRING_SET_UTC_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_FULL_YEAR)
  2497. #define DUK_HTHREAD_STRING_SET_UTC_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_FULL_YEAR)
  2498. #define DUK_HEAP_STRING_SET_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FULL_YEAR)
  2499. #define DUK_HTHREAD_STRING_SET_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FULL_YEAR)
  2500. #define DUK_HEAP_STRING_SET_UTC_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MONTH)
  2501. #define DUK_HTHREAD_STRING_SET_UTC_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MONTH)
  2502. #define DUK_HEAP_STRING_SET_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MONTH)
  2503. #define DUK_HTHREAD_STRING_SET_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MONTH)
  2504. #define DUK_HEAP_STRING_SET_UTC_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_DATE)
  2505. #define DUK_HTHREAD_STRING_SET_UTC_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_DATE)
  2506. #define DUK_HEAP_STRING_SET_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_DATE)
  2507. #define DUK_HTHREAD_STRING_SET_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_DATE)
  2508. #define DUK_HEAP_STRING_SET_UTC_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_HOURS)
  2509. #define DUK_HTHREAD_STRING_SET_UTC_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_HOURS)
  2510. #define DUK_HEAP_STRING_SET_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_HOURS)
  2511. #define DUK_HTHREAD_STRING_SET_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_HOURS)
  2512. #define DUK_HEAP_STRING_SET_UTC_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MINUTES)
  2513. #define DUK_HTHREAD_STRING_SET_UTC_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MINUTES)
  2514. #define DUK_HEAP_STRING_SET_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MINUTES)
  2515. #define DUK_HTHREAD_STRING_SET_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MINUTES)
  2516. #define DUK_HEAP_STRING_SET_UTC_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_SECONDS)
  2517. #define DUK_HTHREAD_STRING_SET_UTC_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_SECONDS)
  2518. #define DUK_HEAP_STRING_SET_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_SECONDS)
  2519. #define DUK_HTHREAD_STRING_SET_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_SECONDS)
  2520. #define DUK_HEAP_STRING_SET_UTC_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MILLISECONDS)
  2521. #define DUK_HTHREAD_STRING_SET_UTC_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MILLISECONDS)
  2522. #define DUK_HEAP_STRING_SET_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MILLISECONDS)
  2523. #define DUK_HTHREAD_STRING_SET_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MILLISECONDS)
  2524. #define DUK_HEAP_STRING_SET_TIME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_TIME)
  2525. #define DUK_HTHREAD_STRING_SET_TIME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_TIME)
  2526. #define DUK_HEAP_STRING_GET_TIMEZONE_OFFSET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_TIMEZONE_OFFSET)
  2527. #define DUK_HTHREAD_STRING_GET_TIMEZONE_OFFSET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_TIMEZONE_OFFSET)
  2528. #define DUK_HEAP_STRING_GET_UTC_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MILLISECONDS)
  2529. #define DUK_HTHREAD_STRING_GET_UTC_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MILLISECONDS)
  2530. #define DUK_HEAP_STRING_GET_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MILLISECONDS)
  2531. #define DUK_HTHREAD_STRING_GET_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MILLISECONDS)
  2532. #define DUK_HEAP_STRING_GET_UTC_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_SECONDS)
  2533. #define DUK_HTHREAD_STRING_GET_UTC_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_SECONDS)
  2534. #define DUK_HEAP_STRING_GET_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_SECONDS)
  2535. #define DUK_HTHREAD_STRING_GET_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_SECONDS)
  2536. #define DUK_HEAP_STRING_GET_UTC_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MINUTES)
  2537. #define DUK_HTHREAD_STRING_GET_UTC_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MINUTES)
  2538. #define DUK_HEAP_STRING_GET_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MINUTES)
  2539. #define DUK_HTHREAD_STRING_GET_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MINUTES)
  2540. #define DUK_HEAP_STRING_GET_UTC_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_HOURS)
  2541. #define DUK_HTHREAD_STRING_GET_UTC_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_HOURS)
  2542. #define DUK_HEAP_STRING_GET_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_HOURS)
  2543. #define DUK_HTHREAD_STRING_GET_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_HOURS)
  2544. #define DUK_HEAP_STRING_GET_UTC_DAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_DAY)
  2545. #define DUK_HTHREAD_STRING_GET_UTC_DAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_DAY)
  2546. #define DUK_HEAP_STRING_GET_DAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_DAY)
  2547. #define DUK_HTHREAD_STRING_GET_DAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_DAY)
  2548. #define DUK_HEAP_STRING_GET_UTC_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_DATE)
  2549. #define DUK_HTHREAD_STRING_GET_UTC_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_DATE)
  2550. #define DUK_HEAP_STRING_GET_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_DATE)
  2551. #define DUK_HTHREAD_STRING_GET_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_DATE)
  2552. #define DUK_HEAP_STRING_GET_UTC_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MONTH)
  2553. #define DUK_HTHREAD_STRING_GET_UTC_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MONTH)
  2554. #define DUK_HEAP_STRING_GET_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MONTH)
  2555. #define DUK_HTHREAD_STRING_GET_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MONTH)
  2556. #define DUK_HEAP_STRING_GET_UTC_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_FULL_YEAR)
  2557. #define DUK_HTHREAD_STRING_GET_UTC_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_FULL_YEAR)
  2558. #define DUK_HEAP_STRING_GET_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FULL_YEAR)
  2559. #define DUK_HTHREAD_STRING_GET_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FULL_YEAR)
  2560. #define DUK_HEAP_STRING_GET_TIME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_TIME)
  2561. #define DUK_HTHREAD_STRING_GET_TIME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_TIME)
  2562. #define DUK_HEAP_STRING_TO_LOCALE_TIME_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_TIME_STRING)
  2563. #define DUK_HTHREAD_STRING_TO_LOCALE_TIME_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_TIME_STRING)
  2564. #define DUK_HEAP_STRING_TO_LOCALE_DATE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_DATE_STRING)
  2565. #define DUK_HTHREAD_STRING_TO_LOCALE_DATE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_DATE_STRING)
  2566. #define DUK_HEAP_STRING_TO_TIME_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_TIME_STRING)
  2567. #define DUK_HTHREAD_STRING_TO_TIME_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_TIME_STRING)
  2568. #define DUK_HEAP_STRING_TO_DATE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_DATE_STRING)
  2569. #define DUK_HTHREAD_STRING_TO_DATE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_DATE_STRING)
  2570. #define DUK_HEAP_STRING_NOW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NOW)
  2571. #define DUK_HTHREAD_STRING_NOW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NOW)
  2572. #define DUK_HEAP_STRING_UTC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UTC)
  2573. #define DUK_HTHREAD_STRING_UTC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UTC)
  2574. #define DUK_HEAP_STRING_PARSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE)
  2575. #define DUK_HTHREAD_STRING_PARSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE)
  2576. #define DUK_HEAP_STRING_TO_PRECISION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_PRECISION)
  2577. #define DUK_HTHREAD_STRING_TO_PRECISION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_PRECISION)
  2578. #define DUK_HEAP_STRING_TO_EXPONENTIAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_EXPONENTIAL)
  2579. #define DUK_HTHREAD_STRING_TO_EXPONENTIAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_EXPONENTIAL)
  2580. #define DUK_HEAP_STRING_TO_FIXED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_FIXED)
  2581. #define DUK_HTHREAD_STRING_TO_FIXED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_FIXED)
  2582. #define DUK_HEAP_STRING_POSITIVE_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POSITIVE_INFINITY)
  2583. #define DUK_HTHREAD_STRING_POSITIVE_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POSITIVE_INFINITY)
  2584. #define DUK_HEAP_STRING_NEGATIVE_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEGATIVE_INFINITY)
  2585. #define DUK_HTHREAD_STRING_NEGATIVE_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEGATIVE_INFINITY)
  2586. #define DUK_HEAP_STRING_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAN)
  2587. #define DUK_HTHREAD_STRING_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAN)
  2588. #define DUK_HEAP_STRING_MIN_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MIN_VALUE)
  2589. #define DUK_HTHREAD_STRING_MIN_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MIN_VALUE)
  2590. #define DUK_HEAP_STRING_MAX_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAX_VALUE)
  2591. #define DUK_HTHREAD_STRING_MAX_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAX_VALUE)
  2592. #define DUK_HEAP_STRING_SUBSTR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBSTR)
  2593. #define DUK_HTHREAD_STRING_SUBSTR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBSTR)
  2594. #define DUK_HEAP_STRING_TRIM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRIM)
  2595. #define DUK_HTHREAD_STRING_TRIM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRIM)
  2596. #define DUK_HEAP_STRING_TO_LOCALE_UPPER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_UPPER_CASE)
  2597. #define DUK_HTHREAD_STRING_TO_LOCALE_UPPER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_UPPER_CASE)
  2598. #define DUK_HEAP_STRING_TO_UPPER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_UPPER_CASE)
  2599. #define DUK_HTHREAD_STRING_TO_UPPER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_UPPER_CASE)
  2600. #define DUK_HEAP_STRING_TO_LOCALE_LOWER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_LOWER_CASE)
  2601. #define DUK_HTHREAD_STRING_TO_LOCALE_LOWER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_LOWER_CASE)
  2602. #define DUK_HEAP_STRING_TO_LOWER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOWER_CASE)
  2603. #define DUK_HTHREAD_STRING_TO_LOWER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOWER_CASE)
  2604. #define DUK_HEAP_STRING_SUBSTRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBSTRING)
  2605. #define DUK_HTHREAD_STRING_SUBSTRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBSTRING)
  2606. #define DUK_HEAP_STRING_SPLIT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPLIT)
  2607. #define DUK_HTHREAD_STRING_SPLIT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPLIT)
  2608. #define DUK_HEAP_STRING_SEARCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SEARCH)
  2609. #define DUK_HTHREAD_STRING_SEARCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SEARCH)
  2610. #define DUK_HEAP_STRING_REPLACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REPLACE)
  2611. #define DUK_HTHREAD_STRING_REPLACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REPLACE)
  2612. #define DUK_HEAP_STRING_MATCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MATCH)
  2613. #define DUK_HTHREAD_STRING_MATCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MATCH)
  2614. #define DUK_HEAP_STRING_LOCALE_COMPARE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOCALE_COMPARE)
  2615. #define DUK_HTHREAD_STRING_LOCALE_COMPARE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOCALE_COMPARE)
  2616. #define DUK_HEAP_STRING_CHAR_CODE_AT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CHAR_CODE_AT)
  2617. #define DUK_HTHREAD_STRING_CHAR_CODE_AT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CHAR_CODE_AT)
  2618. #define DUK_HEAP_STRING_CHAR_AT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CHAR_AT)
  2619. #define DUK_HTHREAD_STRING_CHAR_AT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CHAR_AT)
  2620. #define DUK_HEAP_STRING_FROM_CHAR_CODE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FROM_CHAR_CODE)
  2621. #define DUK_HTHREAD_STRING_FROM_CHAR_CODE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FROM_CHAR_CODE)
  2622. #define DUK_HEAP_STRING_REDUCE_RIGHT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REDUCE_RIGHT)
  2623. #define DUK_HTHREAD_STRING_REDUCE_RIGHT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REDUCE_RIGHT)
  2624. #define DUK_HEAP_STRING_REDUCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REDUCE)
  2625. #define DUK_HTHREAD_STRING_REDUCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REDUCE)
  2626. #define DUK_HEAP_STRING_FILTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILTER)
  2627. #define DUK_HTHREAD_STRING_FILTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILTER)
  2628. #define DUK_HEAP_STRING_MAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAP)
  2629. #define DUK_HTHREAD_STRING_MAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAP)
  2630. #define DUK_HEAP_STRING_FOR_EACH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FOR_EACH)
  2631. #define DUK_HTHREAD_STRING_FOR_EACH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FOR_EACH)
  2632. #define DUK_HEAP_STRING_SOME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SOME)
  2633. #define DUK_HTHREAD_STRING_SOME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SOME)
  2634. #define DUK_HEAP_STRING_EVERY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVERY)
  2635. #define DUK_HTHREAD_STRING_EVERY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVERY)
  2636. #define DUK_HEAP_STRING_LAST_INDEX_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LAST_INDEX_OF)
  2637. #define DUK_HTHREAD_STRING_LAST_INDEX_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LAST_INDEX_OF)
  2638. #define DUK_HEAP_STRING_INDEX_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INDEX_OF)
  2639. #define DUK_HTHREAD_STRING_INDEX_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INDEX_OF)
  2640. #define DUK_HEAP_STRING_UNSHIFT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UNSHIFT)
  2641. #define DUK_HTHREAD_STRING_UNSHIFT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UNSHIFT)
  2642. #define DUK_HEAP_STRING_SPLICE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPLICE)
  2643. #define DUK_HTHREAD_STRING_SPLICE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPLICE)
  2644. #define DUK_HEAP_STRING_SORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SORT)
  2645. #define DUK_HTHREAD_STRING_SORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SORT)
  2646. #define DUK_HEAP_STRING_SLICE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SLICE)
  2647. #define DUK_HTHREAD_STRING_SLICE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SLICE)
  2648. #define DUK_HEAP_STRING_SHIFT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SHIFT)
  2649. #define DUK_HTHREAD_STRING_SHIFT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SHIFT)
  2650. #define DUK_HEAP_STRING_REVERSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REVERSE)
  2651. #define DUK_HTHREAD_STRING_REVERSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REVERSE)
  2652. #define DUK_HEAP_STRING_PUSH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PUSH)
  2653. #define DUK_HTHREAD_STRING_PUSH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PUSH)
  2654. #define DUK_HEAP_STRING_POP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POP)
  2655. #define DUK_HTHREAD_STRING_POP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POP)
  2656. #define DUK_HEAP_STRING_JOIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JOIN)
  2657. #define DUK_HTHREAD_STRING_JOIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JOIN)
  2658. #define DUK_HEAP_STRING_CONCAT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONCAT)
  2659. #define DUK_HTHREAD_STRING_CONCAT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONCAT)
  2660. #define DUK_HEAP_STRING_IS_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_ARRAY)
  2661. #define DUK_HTHREAD_STRING_IS_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_ARRAY)
  2662. #define DUK_HEAP_STRING_LC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ARGUMENTS)
  2663. #define DUK_HTHREAD_STRING_LC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ARGUMENTS)
  2664. #define DUK_HEAP_STRING_CALLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLER)
  2665. #define DUK_HTHREAD_STRING_CALLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLER)
  2666. #define DUK_HEAP_STRING_BIND(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BIND)
  2667. #define DUK_HTHREAD_STRING_BIND(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BIND)
  2668. #define DUK_HEAP_STRING_CALL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALL)
  2669. #define DUK_HTHREAD_STRING_CALL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALL)
  2670. #define DUK_HEAP_STRING_APPLY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_APPLY)
  2671. #define DUK_HTHREAD_STRING_APPLY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_APPLY)
  2672. #define DUK_HEAP_STRING_PROPERTY_IS_ENUMERABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROPERTY_IS_ENUMERABLE)
  2673. #define DUK_HTHREAD_STRING_PROPERTY_IS_ENUMERABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROPERTY_IS_ENUMERABLE)
  2674. #define DUK_HEAP_STRING_IS_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_PROTOTYPE_OF)
  2675. #define DUK_HTHREAD_STRING_IS_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_PROTOTYPE_OF)
  2676. #define DUK_HEAP_STRING_HAS_OWN_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HAS_OWN_PROPERTY)
  2677. #define DUK_HTHREAD_STRING_HAS_OWN_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HAS_OWN_PROPERTY)
  2678. #define DUK_HEAP_STRING_VALUE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE_OF)
  2679. #define DUK_HTHREAD_STRING_VALUE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE_OF)
  2680. #define DUK_HEAP_STRING_TO_LOCALE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_STRING)
  2681. #define DUK_HTHREAD_STRING_TO_LOCALE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_STRING)
  2682. #define DUK_HEAP_STRING_TO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_STRING)
  2683. #define DUK_HTHREAD_STRING_TO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_STRING)
  2684. #define DUK_HEAP_STRING_CONSTRUCTOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONSTRUCTOR)
  2685. #define DUK_HTHREAD_STRING_CONSTRUCTOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONSTRUCTOR)
  2686. #define DUK_HEAP_STRING_SET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET)
  2687. #define DUK_HTHREAD_STRING_SET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET)
  2688. #define DUK_HEAP_STRING_GET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET)
  2689. #define DUK_HTHREAD_STRING_GET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET)
  2690. #define DUK_HEAP_STRING_ENUMERABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERABLE)
  2691. #define DUK_HTHREAD_STRING_ENUMERABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERABLE)
  2692. #define DUK_HEAP_STRING_CONFIGURABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONFIGURABLE)
  2693. #define DUK_HTHREAD_STRING_CONFIGURABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONFIGURABLE)
  2694. #define DUK_HEAP_STRING_WRITABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITABLE)
  2695. #define DUK_HTHREAD_STRING_WRITABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITABLE)
  2696. #define DUK_HEAP_STRING_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE)
  2697. #define DUK_HTHREAD_STRING_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE)
  2698. #define DUK_HEAP_STRING_KEYS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_KEYS)
  2699. #define DUK_HTHREAD_STRING_KEYS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_KEYS)
  2700. #define DUK_HEAP_STRING_IS_EXTENSIBLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_EXTENSIBLE)
  2701. #define DUK_HTHREAD_STRING_IS_EXTENSIBLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_EXTENSIBLE)
  2702. #define DUK_HEAP_STRING_IS_FROZEN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_FROZEN)
  2703. #define DUK_HTHREAD_STRING_IS_FROZEN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_FROZEN)
  2704. #define DUK_HEAP_STRING_IS_SEALED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_SEALED)
  2705. #define DUK_HTHREAD_STRING_IS_SEALED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_SEALED)
  2706. #define DUK_HEAP_STRING_PREVENT_EXTENSIONS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PREVENT_EXTENSIONS)
  2707. #define DUK_HTHREAD_STRING_PREVENT_EXTENSIONS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PREVENT_EXTENSIONS)
  2708. #define DUK_HEAP_STRING_FREEZE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FREEZE)
  2709. #define DUK_HTHREAD_STRING_FREEZE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FREEZE)
  2710. #define DUK_HEAP_STRING_SEAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SEAL)
  2711. #define DUK_HTHREAD_STRING_SEAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SEAL)
  2712. #define DUK_HEAP_STRING_DEFINE_PROPERTIES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFINE_PROPERTIES)
  2713. #define DUK_HTHREAD_STRING_DEFINE_PROPERTIES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFINE_PROPERTIES)
  2714. #define DUK_HEAP_STRING_DEFINE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFINE_PROPERTY)
  2715. #define DUK_HTHREAD_STRING_DEFINE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFINE_PROPERTY)
  2716. #define DUK_HEAP_STRING_CREATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CREATE)
  2717. #define DUK_HTHREAD_STRING_CREATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CREATE)
  2718. #define DUK_HEAP_STRING_GET_OWN_PROPERTY_NAMES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_OWN_PROPERTY_NAMES)
  2719. #define DUK_HTHREAD_STRING_GET_OWN_PROPERTY_NAMES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_OWN_PROPERTY_NAMES)
  2720. #define DUK_HEAP_STRING_GET_OWN_PROPERTY_DESCRIPTOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR)
  2721. #define DUK_HTHREAD_STRING_GET_OWN_PROPERTY_DESCRIPTOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR)
  2722. #define DUK_HEAP_STRING_GET_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_PROTOTYPE_OF)
  2723. #define DUK_HTHREAD_STRING_GET_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_PROTOTYPE_OF)
  2724. #define DUK_HEAP_STRING_PROTOTYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTOTYPE)
  2725. #define DUK_HTHREAD_STRING_PROTOTYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTOTYPE)
  2726. #define DUK_HEAP_STRING_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LENGTH)
  2727. #define DUK_HTHREAD_STRING_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LENGTH)
  2728. #define DUK_HEAP_STRING_ALERT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ALERT)
  2729. #define DUK_HTHREAD_STRING_ALERT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ALERT)
  2730. #define DUK_HEAP_STRING_PRINT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PRINT)
  2731. #define DUK_HTHREAD_STRING_PRINT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PRINT)
  2732. #define DUK_HEAP_STRING_UNESCAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UNESCAPE)
  2733. #define DUK_HTHREAD_STRING_UNESCAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UNESCAPE)
  2734. #define DUK_HEAP_STRING_ESCAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ESCAPE)
  2735. #define DUK_HTHREAD_STRING_ESCAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ESCAPE)
  2736. #define DUK_HEAP_STRING_ENCODE_URI_COMPONENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENCODE_URI_COMPONENT)
  2737. #define DUK_HTHREAD_STRING_ENCODE_URI_COMPONENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENCODE_URI_COMPONENT)
  2738. #define DUK_HEAP_STRING_ENCODE_URI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENCODE_URI)
  2739. #define DUK_HTHREAD_STRING_ENCODE_URI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENCODE_URI)
  2740. #define DUK_HEAP_STRING_DECODE_URI_COMPONENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DECODE_URI_COMPONENT)
  2741. #define DUK_HTHREAD_STRING_DECODE_URI_COMPONENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DECODE_URI_COMPONENT)
  2742. #define DUK_HEAP_STRING_DECODE_URI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DECODE_URI)
  2743. #define DUK_HTHREAD_STRING_DECODE_URI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DECODE_URI)
  2744. #define DUK_HEAP_STRING_IS_FINITE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_FINITE)
  2745. #define DUK_HTHREAD_STRING_IS_FINITE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_FINITE)
  2746. #define DUK_HEAP_STRING_IS_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_NAN)
  2747. #define DUK_HTHREAD_STRING_IS_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_NAN)
  2748. #define DUK_HEAP_STRING_PARSE_FLOAT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE_FLOAT)
  2749. #define DUK_HTHREAD_STRING_PARSE_FLOAT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE_FLOAT)
  2750. #define DUK_HEAP_STRING_PARSE_INT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE_INT)
  2751. #define DUK_HTHREAD_STRING_PARSE_INT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE_INT)
  2752. #define DUK_HEAP_STRING_EVAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVAL)
  2753. #define DUK_HTHREAD_STRING_EVAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVAL)
  2754. #define DUK_HEAP_STRING_URI_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_URI_ERROR)
  2755. #define DUK_HTHREAD_STRING_URI_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_URI_ERROR)
  2756. #define DUK_HEAP_STRING_TYPE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPE_ERROR)
  2757. #define DUK_HTHREAD_STRING_TYPE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPE_ERROR)
  2758. #define DUK_HEAP_STRING_SYNTAX_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SYNTAX_ERROR)
  2759. #define DUK_HTHREAD_STRING_SYNTAX_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SYNTAX_ERROR)
  2760. #define DUK_HEAP_STRING_REFERENCE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REFERENCE_ERROR)
  2761. #define DUK_HTHREAD_STRING_REFERENCE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REFERENCE_ERROR)
  2762. #define DUK_HEAP_STRING_RANGE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RANGE_ERROR)
  2763. #define DUK_HTHREAD_STRING_RANGE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RANGE_ERROR)
  2764. #define DUK_HEAP_STRING_EVAL_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVAL_ERROR)
  2765. #define DUK_HTHREAD_STRING_EVAL_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVAL_ERROR)
  2766. #define DUK_HEAP_STRING_BREAK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BREAK)
  2767. #define DUK_HTHREAD_STRING_BREAK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BREAK)
  2768. #define DUK_HEAP_STRING_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CASE)
  2769. #define DUK_HTHREAD_STRING_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CASE)
  2770. #define DUK_HEAP_STRING_CATCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CATCH)
  2771. #define DUK_HTHREAD_STRING_CATCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CATCH)
  2772. #define DUK_HEAP_STRING_CONTINUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONTINUE)
  2773. #define DUK_HTHREAD_STRING_CONTINUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONTINUE)
  2774. #define DUK_HEAP_STRING_DEBUGGER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEBUGGER)
  2775. #define DUK_HTHREAD_STRING_DEBUGGER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEBUGGER)
  2776. #define DUK_HEAP_STRING_DEFAULT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFAULT)
  2777. #define DUK_HTHREAD_STRING_DEFAULT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFAULT)
  2778. #define DUK_HEAP_STRING_DELETE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE)
  2779. #define DUK_HTHREAD_STRING_DELETE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE)
  2780. #define DUK_HEAP_STRING_DO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DO)
  2781. #define DUK_HTHREAD_STRING_DO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DO)
  2782. #define DUK_HEAP_STRING_ELSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ELSE)
  2783. #define DUK_HTHREAD_STRING_ELSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ELSE)
  2784. #define DUK_HEAP_STRING_FINALLY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FINALLY)
  2785. #define DUK_HTHREAD_STRING_FINALLY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FINALLY)
  2786. #define DUK_HEAP_STRING_FOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FOR)
  2787. #define DUK_HTHREAD_STRING_FOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FOR)
  2788. #define DUK_HEAP_STRING_LC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FUNCTION)
  2789. #define DUK_HTHREAD_STRING_LC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FUNCTION)
  2790. #define DUK_HEAP_STRING_IF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IF)
  2791. #define DUK_HTHREAD_STRING_IF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IF)
  2792. #define DUK_HEAP_STRING_IN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IN)
  2793. #define DUK_HTHREAD_STRING_IN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IN)
  2794. #define DUK_HEAP_STRING_INSTANCEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INSTANCEOF)
  2795. #define DUK_HTHREAD_STRING_INSTANCEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INSTANCEOF)
  2796. #define DUK_HEAP_STRING_NEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEW)
  2797. #define DUK_HTHREAD_STRING_NEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEW)
  2798. #define DUK_HEAP_STRING_RETURN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RETURN)
  2799. #define DUK_HTHREAD_STRING_RETURN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RETURN)
  2800. #define DUK_HEAP_STRING_SWITCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SWITCH)
  2801. #define DUK_HTHREAD_STRING_SWITCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SWITCH)
  2802. #define DUK_HEAP_STRING_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THIS)
  2803. #define DUK_HTHREAD_STRING_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THIS)
  2804. #define DUK_HEAP_STRING_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THROW)
  2805. #define DUK_HTHREAD_STRING_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THROW)
  2806. #define DUK_HEAP_STRING_TRY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRY)
  2807. #define DUK_HTHREAD_STRING_TRY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRY)
  2808. #define DUK_HEAP_STRING_TYPEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPEOF)
  2809. #define DUK_HTHREAD_STRING_TYPEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPEOF)
  2810. #define DUK_HEAP_STRING_VAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VAR)
  2811. #define DUK_HTHREAD_STRING_VAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VAR)
  2812. #define DUK_HEAP_STRING_VOID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VOID)
  2813. #define DUK_HTHREAD_STRING_VOID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VOID)
  2814. #define DUK_HEAP_STRING_WHILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WHILE)
  2815. #define DUK_HTHREAD_STRING_WHILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WHILE)
  2816. #define DUK_HEAP_STRING_WITH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WITH)
  2817. #define DUK_HTHREAD_STRING_WITH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WITH)
  2818. #define DUK_HEAP_STRING_CLASS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLASS)
  2819. #define DUK_HTHREAD_STRING_CLASS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLASS)
  2820. #define DUK_HEAP_STRING_CONST(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONST)
  2821. #define DUK_HTHREAD_STRING_CONST(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONST)
  2822. #define DUK_HEAP_STRING_ENUM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUM)
  2823. #define DUK_HTHREAD_STRING_ENUM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUM)
  2824. #define DUK_HEAP_STRING_EXPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORT)
  2825. #define DUK_HTHREAD_STRING_EXPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORT)
  2826. #define DUK_HEAP_STRING_EXTENDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXTENDS)
  2827. #define DUK_HTHREAD_STRING_EXTENDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXTENDS)
  2828. #define DUK_HEAP_STRING_IMPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPORT)
  2829. #define DUK_HTHREAD_STRING_IMPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPORT)
  2830. #define DUK_HEAP_STRING_SUPER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUPER)
  2831. #define DUK_HTHREAD_STRING_SUPER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUPER)
  2832. #define DUK_HEAP_STRING_LC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NULL)
  2833. #define DUK_HTHREAD_STRING_LC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NULL)
  2834. #define DUK_HEAP_STRING_TRUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRUE)
  2835. #define DUK_HTHREAD_STRING_TRUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRUE)
  2836. #define DUK_HEAP_STRING_FALSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FALSE)
  2837. #define DUK_HTHREAD_STRING_FALSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FALSE)
  2838. #define DUK_HEAP_STRING_IMPLEMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPLEMENTS)
  2839. #define DUK_HTHREAD_STRING_IMPLEMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPLEMENTS)
  2840. #define DUK_HEAP_STRING_INTERFACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INTERFACE)
  2841. #define DUK_HTHREAD_STRING_INTERFACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INTERFACE)
  2842. #define DUK_HEAP_STRING_LET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LET)
  2843. #define DUK_HTHREAD_STRING_LET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LET)
  2844. #define DUK_HEAP_STRING_PACKAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PACKAGE)
  2845. #define DUK_HTHREAD_STRING_PACKAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PACKAGE)
  2846. #define DUK_HEAP_STRING_PRIVATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PRIVATE)
  2847. #define DUK_HTHREAD_STRING_PRIVATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PRIVATE)
  2848. #define DUK_HEAP_STRING_PROTECTED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTECTED)
  2849. #define DUK_HTHREAD_STRING_PROTECTED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTECTED)
  2850. #define DUK_HEAP_STRING_PUBLIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PUBLIC)
  2851. #define DUK_HTHREAD_STRING_PUBLIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PUBLIC)
  2852. #define DUK_HEAP_STRING_STATIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STATIC)
  2853. #define DUK_HTHREAD_STRING_STATIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STATIC)
  2854. #define DUK_HEAP_STRING_YIELD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_YIELD)
  2855. #define DUK_HTHREAD_STRING_YIELD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_YIELD)
  2856. #define DUK_HEAP_NUM_STRINGS 414
  2857. #define DUK_STRIDX_START_RESERVED 369
  2858. #define DUK_STRIDX_START_STRICT_RESERVED 405
  2859. #define DUK_STRIDX_END_RESERVED 414 /* exclusive endpoint */
  2860. #if !defined(DUK_SINGLE_FILE)
  2861. DUK_INTERNAL_DECL const duk_c_function duk_bi_native_functions[147];
  2862. DUK_INTERNAL_DECL const duk_uint8_t duk_builtins_data[1952];
  2863. #ifdef DUK_USE_BUILTIN_INITJS
  2864. DUK_INTERNAL_DECL const duk_uint8_t duk_initjs_data[187];
  2865. #endif /* DUK_USE_BUILTIN_INITJS */
  2866. #endif /* !DUK_SINGLE_FILE */
  2867. #define DUK_BUILTINS_DATA_LENGTH 1952
  2868. #ifdef DUK_USE_BUILTIN_INITJS
  2869. #define DUK_BUILTIN_INITJS_DATA_LENGTH 187
  2870. #endif /* DUK_USE_BUILTIN_INITJS */
  2871. #define DUK_BIDX_GLOBAL 0
  2872. #define DUK_BIDX_GLOBAL_ENV 1
  2873. #define DUK_BIDX_OBJECT_CONSTRUCTOR 2
  2874. #define DUK_BIDX_OBJECT_PROTOTYPE 3
  2875. #define DUK_BIDX_FUNCTION_CONSTRUCTOR 4
  2876. #define DUK_BIDX_FUNCTION_PROTOTYPE 5
  2877. #define DUK_BIDX_ARRAY_CONSTRUCTOR 6
  2878. #define DUK_BIDX_ARRAY_PROTOTYPE 7
  2879. #define DUK_BIDX_STRING_CONSTRUCTOR 8
  2880. #define DUK_BIDX_STRING_PROTOTYPE 9
  2881. #define DUK_BIDX_BOOLEAN_CONSTRUCTOR 10
  2882. #define DUK_BIDX_BOOLEAN_PROTOTYPE 11
  2883. #define DUK_BIDX_NUMBER_CONSTRUCTOR 12
  2884. #define DUK_BIDX_NUMBER_PROTOTYPE 13
  2885. #define DUK_BIDX_DATE_CONSTRUCTOR 14
  2886. #define DUK_BIDX_DATE_PROTOTYPE 15
  2887. #define DUK_BIDX_REGEXP_CONSTRUCTOR 16
  2888. #define DUK_BIDX_REGEXP_PROTOTYPE 17
  2889. #define DUK_BIDX_ERROR_CONSTRUCTOR 18
  2890. #define DUK_BIDX_ERROR_PROTOTYPE 19
  2891. #define DUK_BIDX_EVAL_ERROR_CONSTRUCTOR 20
  2892. #define DUK_BIDX_EVAL_ERROR_PROTOTYPE 21
  2893. #define DUK_BIDX_RANGE_ERROR_CONSTRUCTOR 22
  2894. #define DUK_BIDX_RANGE_ERROR_PROTOTYPE 23
  2895. #define DUK_BIDX_REFERENCE_ERROR_CONSTRUCTOR 24
  2896. #define DUK_BIDX_REFERENCE_ERROR_PROTOTYPE 25
  2897. #define DUK_BIDX_SYNTAX_ERROR_CONSTRUCTOR 26
  2898. #define DUK_BIDX_SYNTAX_ERROR_PROTOTYPE 27
  2899. #define DUK_BIDX_TYPE_ERROR_CONSTRUCTOR 28
  2900. #define DUK_BIDX_TYPE_ERROR_PROTOTYPE 29
  2901. #define DUK_BIDX_URI_ERROR_CONSTRUCTOR 30
  2902. #define DUK_BIDX_URI_ERROR_PROTOTYPE 31
  2903. #define DUK_BIDX_MATH 32
  2904. #define DUK_BIDX_JSON 33
  2905. #define DUK_BIDX_TYPE_ERROR_THROWER 34
  2906. #define DUK_BIDX_PROXY_CONSTRUCTOR 35
  2907. #define DUK_BIDX_DUKTAPE 36
  2908. #define DUK_BIDX_THREAD_CONSTRUCTOR 37
  2909. #define DUK_BIDX_THREAD_PROTOTYPE 38
  2910. #define DUK_BIDX_BUFFER_CONSTRUCTOR 39
  2911. #define DUK_BIDX_BUFFER_PROTOTYPE 40
  2912. #define DUK_BIDX_POINTER_CONSTRUCTOR 41
  2913. #define DUK_BIDX_POINTER_PROTOTYPE 42
  2914. #define DUK_BIDX_LOGGER_CONSTRUCTOR 43
  2915. #define DUK_BIDX_LOGGER_PROTOTYPE 44
  2916. #define DUK_BIDX_DOUBLE_ERROR 45
  2917. #define DUK_BIDX_ARRAYBUFFER_CONSTRUCTOR 46
  2918. #define DUK_BIDX_ARRAYBUFFER_PROTOTYPE 47
  2919. #define DUK_BIDX_DATAVIEW_CONSTRUCTOR 48
  2920. #define DUK_BIDX_DATAVIEW_PROTOTYPE 49
  2921. #define DUK_BIDX_TYPEDARRAY_PROTOTYPE 50
  2922. #define DUK_BIDX_INT8ARRAY_CONSTRUCTOR 51
  2923. #define DUK_BIDX_INT8ARRAY_PROTOTYPE 52
  2924. #define DUK_BIDX_UINT8ARRAY_CONSTRUCTOR 53
  2925. #define DUK_BIDX_UINT8ARRAY_PROTOTYPE 54
  2926. #define DUK_BIDX_UINT8CLAMPEDARRAY_CONSTRUCTOR 55
  2927. #define DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE 56
  2928. #define DUK_BIDX_INT16ARRAY_CONSTRUCTOR 57
  2929. #define DUK_BIDX_INT16ARRAY_PROTOTYPE 58
  2930. #define DUK_BIDX_UINT16ARRAY_CONSTRUCTOR 59
  2931. #define DUK_BIDX_UINT16ARRAY_PROTOTYPE 60
  2932. #define DUK_BIDX_INT32ARRAY_CONSTRUCTOR 61
  2933. #define DUK_BIDX_INT32ARRAY_PROTOTYPE 62
  2934. #define DUK_BIDX_UINT32ARRAY_CONSTRUCTOR 63
  2935. #define DUK_BIDX_UINT32ARRAY_PROTOTYPE 64
  2936. #define DUK_BIDX_FLOAT32ARRAY_CONSTRUCTOR 65
  2937. #define DUK_BIDX_FLOAT32ARRAY_PROTOTYPE 66
  2938. #define DUK_BIDX_FLOAT64ARRAY_CONSTRUCTOR 67
  2939. #define DUK_BIDX_FLOAT64ARRAY_PROTOTYPE 68
  2940. #define DUK_BIDX_NODEJS_BUFFER_CONSTRUCTOR 69
  2941. #define DUK_BIDX_NODEJS_BUFFER_PROTOTYPE 70
  2942. #define DUK_NUM_BUILTINS 71
  2943. #elif defined(DUK_USE_DOUBLE_ME)
  2944. #if !defined(DUK_SINGLE_FILE)
  2945. DUK_INTERNAL_DECL const duk_uint8_t duk_strings_data[2624];
  2946. #endif /* !DUK_SINGLE_FILE */
  2947. #define DUK_STRDATA_DATA_LENGTH 2624
  2948. #define DUK_STRDATA_MAX_STRLEN 24
  2949. #define DUK_STRIDX_UC_LOGGER 0 /* 'Logger' */
  2950. #define DUK_STRIDX_UC_THREAD 1 /* 'Thread' */
  2951. #define DUK_STRIDX_UC_POINTER 2 /* 'Pointer' */
  2952. #define DUK_STRIDX_DEC_ENV 3 /* 'DecEnv' */
  2953. #define DUK_STRIDX_OBJ_ENV 4 /* 'ObjEnv' */
  2954. #define DUK_STRIDX_FLOAT64_ARRAY 5 /* 'Float64Array' */
  2955. #define DUK_STRIDX_FLOAT32_ARRAY 6 /* 'Float32Array' */
  2956. #define DUK_STRIDX_UINT32_ARRAY 7 /* 'Uint32Array' */
  2957. #define DUK_STRIDX_INT32_ARRAY 8 /* 'Int32Array' */
  2958. #define DUK_STRIDX_UINT16_ARRAY 9 /* 'Uint16Array' */
  2959. #define DUK_STRIDX_INT16_ARRAY 10 /* 'Int16Array' */
  2960. #define DUK_STRIDX_UINT8_CLAMPED_ARRAY 11 /* 'Uint8ClampedArray' */
  2961. #define DUK_STRIDX_UINT8_ARRAY 12 /* 'Uint8Array' */
  2962. #define DUK_STRIDX_INT8_ARRAY 13 /* 'Int8Array' */
  2963. #define DUK_STRIDX_DATA_VIEW 14 /* 'DataView' */
  2964. #define DUK_STRIDX_ARRAY_BUFFER 15 /* 'ArrayBuffer' */
  2965. #define DUK_STRIDX_UC_BUFFER 16 /* 'Buffer' */
  2966. #define DUK_STRIDX_EMPTY_STRING 17 /* '' */
  2967. #define DUK_STRIDX_GLOBAL 18 /* 'global' */
  2968. #define DUK_STRIDX_UC_ARGUMENTS 19 /* 'Arguments' */
  2969. #define DUK_STRIDX_JSON 20 /* 'JSON' */
  2970. #define DUK_STRIDX_MATH 21 /* 'Math' */
  2971. #define DUK_STRIDX_UC_ERROR 22 /* 'Error' */
  2972. #define DUK_STRIDX_REG_EXP 23 /* 'RegExp' */
  2973. #define DUK_STRIDX_DATE 24 /* 'Date' */
  2974. #define DUK_STRIDX_UC_NUMBER 25 /* 'Number' */
  2975. #define DUK_STRIDX_UC_BOOLEAN 26 /* 'Boolean' */
  2976. #define DUK_STRIDX_UC_STRING 27 /* 'String' */
  2977. #define DUK_STRIDX_ARRAY 28 /* 'Array' */
  2978. #define DUK_STRIDX_UC_FUNCTION 29 /* 'Function' */
  2979. #define DUK_STRIDX_UC_OBJECT 30 /* 'Object' */
  2980. #define DUK_STRIDX_UC_NULL 31 /* 'Null' */
  2981. #define DUK_STRIDX_UC_UNDEFINED 32 /* 'Undefined' */
  2982. #define DUK_STRIDX_JSON_EXT_FUNCTION2 33 /* '{_func:true}' */
  2983. #define DUK_STRIDX_JSON_EXT_FUNCTION1 34 /* '{"_func":true}' */
  2984. #define DUK_STRIDX_JSON_EXT_NEGINF 35 /* '{"_ninf":true}' */
  2985. #define DUK_STRIDX_JSON_EXT_POSINF 36 /* '{"_inf":true}' */
  2986. #define DUK_STRIDX_JSON_EXT_NAN 37 /* '{"_nan":true}' */
  2987. #define DUK_STRIDX_JSON_EXT_UNDEFINED 38 /* '{"_undef":true}' */
  2988. #define DUK_STRIDX_TO_LOG_STRING 39 /* 'toLogString' */
  2989. #define DUK_STRIDX_CLOG 40 /* 'clog' */
  2990. #define DUK_STRIDX_LC_L 41 /* 'l' */
  2991. #define DUK_STRIDX_LC_N 42 /* 'n' */
  2992. #define DUK_STRIDX_LC_FATAL 43 /* 'fatal' */
  2993. #define DUK_STRIDX_LC_ERROR 44 /* 'error' */
  2994. #define DUK_STRIDX_LC_WARN 45 /* 'warn' */
  2995. #define DUK_STRIDX_LC_DEBUG 46 /* 'debug' */
  2996. #define DUK_STRIDX_LC_TRACE 47 /* 'trace' */
  2997. #define DUK_STRIDX_RAW 48 /* 'raw' */
  2998. #define DUK_STRIDX_FMT 49 /* 'fmt' */
  2999. #define DUK_STRIDX_CURRENT 50 /* 'current' */
  3000. #define DUK_STRIDX_RESUME 51 /* 'resume' */
  3001. #define DUK_STRIDX_COMPACT 52 /* 'compact' */
  3002. #define DUK_STRIDX_JC 53 /* 'jc' */
  3003. #define DUK_STRIDX_JX 54 /* 'jx' */
  3004. #define DUK_STRIDX_BASE64 55 /* 'base64' */
  3005. #define DUK_STRIDX_HEX 56 /* 'hex' */
  3006. #define DUK_STRIDX_DEC 57 /* 'dec' */
  3007. #define DUK_STRIDX_ENC 58 /* 'enc' */
  3008. #define DUK_STRIDX_FIN 59 /* 'fin' */
  3009. #define DUK_STRIDX_GC 60 /* 'gc' */
  3010. #define DUK_STRIDX_ACT 61 /* 'act' */
  3011. #define DUK_STRIDX_LC_INFO 62 /* 'info' */
  3012. #define DUK_STRIDX_VERSION 63 /* 'version' */
  3013. #define DUK_STRIDX_ENV 64 /* 'env' */
  3014. #define DUK_STRIDX_MOD_LOADED 65 /* 'modLoaded' */
  3015. #define DUK_STRIDX_MOD_SEARCH 66 /* 'modSearch' */
  3016. #define DUK_STRIDX_ERR_THROW 67 /* 'errThrow' */
  3017. #define DUK_STRIDX_ERR_CREATE 68 /* 'errCreate' */
  3018. #define DUK_STRIDX_COMPILE 69 /* 'compile' */
  3019. #define DUK_STRIDX_INT_REGBASE 70 /* '\x00Regbase' */
  3020. #define DUK_STRIDX_INT_THREAD 71 /* '\x00Thread' */
  3021. #define DUK_STRIDX_INT_HANDLER 72 /* '\x00Handler' */
  3022. #define DUK_STRIDX_INT_FINALIZER 73 /* '\x00Finalizer' */
  3023. #define DUK_STRIDX_INT_CALLEE 74 /* '\x00Callee' */
  3024. #define DUK_STRIDX_INT_MAP 75 /* '\x00Map' */
  3025. #define DUK_STRIDX_INT_ARGS 76 /* '\x00Args' */
  3026. #define DUK_STRIDX_INT_THIS 77 /* '\x00This' */
  3027. #define DUK_STRIDX_INT_PC2LINE 78 /* '\x00Pc2line' */
  3028. #define DUK_STRIDX_INT_SOURCE 79 /* '\x00Source' */
  3029. #define DUK_STRIDX_INT_VARENV 80 /* '\x00Varenv' */
  3030. #define DUK_STRIDX_INT_LEXENV 81 /* '\x00Lexenv' */
  3031. #define DUK_STRIDX_INT_VARMAP 82 /* '\x00Varmap' */
  3032. #define DUK_STRIDX_INT_FORMALS 83 /* '\x00Formals' */
  3033. #define DUK_STRIDX_INT_BYTECODE 84 /* '\x00Bytecode' */
  3034. #define DUK_STRIDX_INT_NEXT 85 /* '\x00Next' */
  3035. #define DUK_STRIDX_INT_TARGET 86 /* '\x00Target' */
  3036. #define DUK_STRIDX_INT_VALUE 87 /* '\x00Value' */
  3037. #define DUK_STRIDX_LC_POINTER 88 /* 'pointer' */
  3038. #define DUK_STRIDX_INT_TRACEDATA 89 /* '\x00Tracedata' */
  3039. #define DUK_STRIDX_LINE_NUMBER 90 /* 'lineNumber' */
  3040. #define DUK_STRIDX_FILE_NAME 91 /* 'fileName' */
  3041. #define DUK_STRIDX_PC 92 /* 'pc' */
  3042. #define DUK_STRIDX_STACK 93 /* 'stack' */
  3043. #define DUK_STRIDX_THROW_TYPE_ERROR 94 /* 'ThrowTypeError' */
  3044. #define DUK_STRIDX_DUKTAPE 95 /* 'Duktape' */
  3045. #define DUK_STRIDX_SET_FLOAT64 96 /* 'setFloat64' */
  3046. #define DUK_STRIDX_SET_FLOAT32 97 /* 'setFloat32' */
  3047. #define DUK_STRIDX_SET_UINT32 98 /* 'setUint32' */
  3048. #define DUK_STRIDX_SET_INT32 99 /* 'setInt32' */
  3049. #define DUK_STRIDX_SET_UINT16 100 /* 'setUint16' */
  3050. #define DUK_STRIDX_SET_INT16 101 /* 'setInt16' */
  3051. #define DUK_STRIDX_SET_UINT8 102 /* 'setUint8' */
  3052. #define DUK_STRIDX_SET_INT8 103 /* 'setInt8' */
  3053. #define DUK_STRIDX_GET_FLOAT64 104 /* 'getFloat64' */
  3054. #define DUK_STRIDX_GET_FLOAT32 105 /* 'getFloat32' */
  3055. #define DUK_STRIDX_GET_UINT32 106 /* 'getUint32' */
  3056. #define DUK_STRIDX_GET_INT32 107 /* 'getInt32' */
  3057. #define DUK_STRIDX_GET_UINT16 108 /* 'getUint16' */
  3058. #define DUK_STRIDX_GET_INT16 109 /* 'getInt16' */
  3059. #define DUK_STRIDX_GET_UINT8 110 /* 'getUint8' */
  3060. #define DUK_STRIDX_GET_INT8 111 /* 'getInt8' */
  3061. #define DUK_STRIDX_SUBARRAY 112 /* 'subarray' */
  3062. #define DUK_STRIDX_BYTES_PER_ELEMENT 113 /* 'BYTES_PER_ELEMENT' */
  3063. #define DUK_STRIDX_BYTE_OFFSET 114 /* 'byteOffset' */
  3064. #define DUK_STRIDX_LC_BUFFER 115 /* 'buffer' */
  3065. #define DUK_STRIDX_IS_VIEW 116 /* 'isView' */
  3066. #define DUK_STRIDX_DATA 117 /* 'data' */
  3067. #define DUK_STRIDX_TYPE 118 /* 'type' */
  3068. #define DUK_STRIDX_WRITE_INT_BE 119 /* 'writeIntBE' */
  3069. #define DUK_STRIDX_WRITE_INT_LE 120 /* 'writeIntLE' */
  3070. #define DUK_STRIDX_WRITE_UINT_BE 121 /* 'writeUIntBE' */
  3071. #define DUK_STRIDX_WRITE_UINT_LE 122 /* 'writeUIntLE' */
  3072. #define DUK_STRIDX_WRITE_DOUBLE_BE 123 /* 'writeDoubleBE' */
  3073. #define DUK_STRIDX_WRITE_DOUBLE_LE 124 /* 'writeDoubleLE' */
  3074. #define DUK_STRIDX_WRITE_FLOAT_BE 125 /* 'writeFloatBE' */
  3075. #define DUK_STRIDX_WRITE_FLOAT_LE 126 /* 'writeFloatLE' */
  3076. #define DUK_STRIDX_WRITE_INT32_BE 127 /* 'writeInt32BE' */
  3077. #define DUK_STRIDX_WRITE_INT32_LE 128 /* 'writeInt32LE' */
  3078. #define DUK_STRIDX_WRITE_UINT32_BE 129 /* 'writeUInt32BE' */
  3079. #define DUK_STRIDX_WRITE_UINT32_LE 130 /* 'writeUInt32LE' */
  3080. #define DUK_STRIDX_WRITE_INT16_BE 131 /* 'writeInt16BE' */
  3081. #define DUK_STRIDX_WRITE_INT16_LE 132 /* 'writeInt16LE' */
  3082. #define DUK_STRIDX_WRITE_UINT16_BE 133 /* 'writeUInt16BE' */
  3083. #define DUK_STRIDX_WRITE_UINT16_LE 134 /* 'writeUInt16LE' */
  3084. #define DUK_STRIDX_WRITE_INT8 135 /* 'writeInt8' */
  3085. #define DUK_STRIDX_WRITE_UINT8 136 /* 'writeUInt8' */
  3086. #define DUK_STRIDX_READ_INT_BE 137 /* 'readIntBE' */
  3087. #define DUK_STRIDX_READ_INT_LE 138 /* 'readIntLE' */
  3088. #define DUK_STRIDX_READ_UINT_BE 139 /* 'readUIntBE' */
  3089. #define DUK_STRIDX_READ_UINT_LE 140 /* 'readUIntLE' */
  3090. #define DUK_STRIDX_READ_DOUBLE_BE 141 /* 'readDoubleBE' */
  3091. #define DUK_STRIDX_READ_DOUBLE_LE 142 /* 'readDoubleLE' */
  3092. #define DUK_STRIDX_READ_FLOAT_BE 143 /* 'readFloatBE' */
  3093. #define DUK_STRIDX_READ_FLOAT_LE 144 /* 'readFloatLE' */
  3094. #define DUK_STRIDX_READ_INT32_BE 145 /* 'readInt32BE' */
  3095. #define DUK_STRIDX_READ_INT32_LE 146 /* 'readInt32LE' */
  3096. #define DUK_STRIDX_READ_UINT32_BE 147 /* 'readUInt32BE' */
  3097. #define DUK_STRIDX_READ_UINT32_LE 148 /* 'readUInt32LE' */
  3098. #define DUK_STRIDX_READ_INT16_BE 149 /* 'readInt16BE' */
  3099. #define DUK_STRIDX_READ_INT16_LE 150 /* 'readInt16LE' */
  3100. #define DUK_STRIDX_READ_UINT16_BE 151 /* 'readUInt16BE' */
  3101. #define DUK_STRIDX_READ_UINT16_LE 152 /* 'readUInt16LE' */
  3102. #define DUK_STRIDX_READ_INT8 153 /* 'readInt8' */
  3103. #define DUK_STRIDX_READ_UINT8 154 /* 'readUInt8' */
  3104. #define DUK_STRIDX_COPY 155 /* 'copy' */
  3105. #define DUK_STRIDX_EQUALS 156 /* 'equals' */
  3106. #define DUK_STRIDX_FILL 157 /* 'fill' */
  3107. #define DUK_STRIDX_WRITE 158 /* 'write' */
  3108. #define DUK_STRIDX_COMPARE 159 /* 'compare' */
  3109. #define DUK_STRIDX_BYTE_LENGTH 160 /* 'byteLength' */
  3110. #define DUK_STRIDX_IS_BUFFER 161 /* 'isBuffer' */
  3111. #define DUK_STRIDX_IS_ENCODING 162 /* 'isEncoding' */
  3112. #define DUK_STRIDX_EXPORTS 163 /* 'exports' */
  3113. #define DUK_STRIDX_ID 164 /* 'id' */
  3114. #define DUK_STRIDX_REQUIRE 165 /* 'require' */
  3115. #define DUK_STRIDX___PROTO__ 166 /* '__proto__' */
  3116. #define DUK_STRIDX_SET_PROTOTYPE_OF 167 /* 'setPrototypeOf' */
  3117. #define DUK_STRIDX_OWN_KEYS 168 /* 'ownKeys' */
  3118. #define DUK_STRIDX_ENUMERATE 169 /* 'enumerate' */
  3119. #define DUK_STRIDX_DELETE_PROPERTY 170 /* 'deleteProperty' */
  3120. #define DUK_STRIDX_HAS 171 /* 'has' */
  3121. #define DUK_STRIDX_PROXY 172 /* 'Proxy' */
  3122. #define DUK_STRIDX_CALLEE 173 /* 'callee' */
  3123. #define DUK_STRIDX_INVALID_DATE 174 /* 'Invalid Date' */
  3124. #define DUK_STRIDX_BRACKETED_ELLIPSIS 175 /* '[...]' */
  3125. #define DUK_STRIDX_NEWLINE_TAB 176 /* '\n\t' */
  3126. #define DUK_STRIDX_SPACE 177 /* ' ' */
  3127. #define DUK_STRIDX_COMMA 178 /* ',' */
  3128. #define DUK_STRIDX_MINUS_ZERO 179 /* '-0' */
  3129. #define DUK_STRIDX_PLUS_ZERO 180 /* '+0' */
  3130. #define DUK_STRIDX_ZERO 181 /* '0' */
  3131. #define DUK_STRIDX_MINUS_INFINITY 182 /* '-Infinity' */
  3132. #define DUK_STRIDX_PLUS_INFINITY 183 /* '+Infinity' */
  3133. #define DUK_STRIDX_INFINITY 184 /* 'Infinity' */
  3134. #define DUK_STRIDX_LC_OBJECT 185 /* 'object' */
  3135. #define DUK_STRIDX_LC_STRING 186 /* 'string' */
  3136. #define DUK_STRIDX_LC_NUMBER 187 /* 'number' */
  3137. #define DUK_STRIDX_LC_BOOLEAN 188 /* 'boolean' */
  3138. #define DUK_STRIDX_LC_UNDEFINED 189 /* 'undefined' */
  3139. #define DUK_STRIDX_STRINGIFY 190 /* 'stringify' */
  3140. #define DUK_STRIDX_TAN 191 /* 'tan' */
  3141. #define DUK_STRIDX_SQRT 192 /* 'sqrt' */
  3142. #define DUK_STRIDX_SIN 193 /* 'sin' */
  3143. #define DUK_STRIDX_ROUND 194 /* 'round' */
  3144. #define DUK_STRIDX_RANDOM 195 /* 'random' */
  3145. #define DUK_STRIDX_POW 196 /* 'pow' */
  3146. #define DUK_STRIDX_MIN 197 /* 'min' */
  3147. #define DUK_STRIDX_MAX 198 /* 'max' */
  3148. #define DUK_STRIDX_LOG 199 /* 'log' */
  3149. #define DUK_STRIDX_FLOOR 200 /* 'floor' */
  3150. #define DUK_STRIDX_EXP 201 /* 'exp' */
  3151. #define DUK_STRIDX_COS 202 /* 'cos' */
  3152. #define DUK_STRIDX_CEIL 203 /* 'ceil' */
  3153. #define DUK_STRIDX_ATAN2 204 /* 'atan2' */
  3154. #define DUK_STRIDX_ATAN 205 /* 'atan' */
  3155. #define DUK_STRIDX_ASIN 206 /* 'asin' */
  3156. #define DUK_STRIDX_ACOS 207 /* 'acos' */
  3157. #define DUK_STRIDX_ABS 208 /* 'abs' */
  3158. #define DUK_STRIDX_SQRT2 209 /* 'SQRT2' */
  3159. #define DUK_STRIDX_SQRT1_2 210 /* 'SQRT1_2' */
  3160. #define DUK_STRIDX_PI 211 /* 'PI' */
  3161. #define DUK_STRIDX_LOG10E 212 /* 'LOG10E' */
  3162. #define DUK_STRIDX_LOG2E 213 /* 'LOG2E' */
  3163. #define DUK_STRIDX_LN2 214 /* 'LN2' */
  3164. #define DUK_STRIDX_LN10 215 /* 'LN10' */
  3165. #define DUK_STRIDX_E 216 /* 'E' */
  3166. #define DUK_STRIDX_MESSAGE 217 /* 'message' */
  3167. #define DUK_STRIDX_NAME 218 /* 'name' */
  3168. #define DUK_STRIDX_INPUT 219 /* 'input' */
  3169. #define DUK_STRIDX_INDEX 220 /* 'index' */
  3170. #define DUK_STRIDX_ESCAPED_EMPTY_REGEXP 221 /* '(?:)' */
  3171. #define DUK_STRIDX_LAST_INDEX 222 /* 'lastIndex' */
  3172. #define DUK_STRIDX_MULTILINE 223 /* 'multiline' */
  3173. #define DUK_STRIDX_IGNORE_CASE 224 /* 'ignoreCase' */
  3174. #define DUK_STRIDX_SOURCE 225 /* 'source' */
  3175. #define DUK_STRIDX_TEST 226 /* 'test' */
  3176. #define DUK_STRIDX_EXEC 227 /* 'exec' */
  3177. #define DUK_STRIDX_TO_GMT_STRING 228 /* 'toGMTString' */
  3178. #define DUK_STRIDX_SET_YEAR 229 /* 'setYear' */
  3179. #define DUK_STRIDX_GET_YEAR 230 /* 'getYear' */
  3180. #define DUK_STRIDX_TO_JSON 231 /* 'toJSON' */
  3181. #define DUK_STRIDX_TO_ISO_STRING 232 /* 'toISOString' */
  3182. #define DUK_STRIDX_TO_UTC_STRING 233 /* 'toUTCString' */
  3183. #define DUK_STRIDX_SET_UTC_FULL_YEAR 234 /* 'setUTCFullYear' */
  3184. #define DUK_STRIDX_SET_FULL_YEAR 235 /* 'setFullYear' */
  3185. #define DUK_STRIDX_SET_UTC_MONTH 236 /* 'setUTCMonth' */
  3186. #define DUK_STRIDX_SET_MONTH 237 /* 'setMonth' */
  3187. #define DUK_STRIDX_SET_UTC_DATE 238 /* 'setUTCDate' */
  3188. #define DUK_STRIDX_SET_DATE 239 /* 'setDate' */
  3189. #define DUK_STRIDX_SET_UTC_HOURS 240 /* 'setUTCHours' */
  3190. #define DUK_STRIDX_SET_HOURS 241 /* 'setHours' */
  3191. #define DUK_STRIDX_SET_UTC_MINUTES 242 /* 'setUTCMinutes' */
  3192. #define DUK_STRIDX_SET_MINUTES 243 /* 'setMinutes' */
  3193. #define DUK_STRIDX_SET_UTC_SECONDS 244 /* 'setUTCSeconds' */
  3194. #define DUK_STRIDX_SET_SECONDS 245 /* 'setSeconds' */
  3195. #define DUK_STRIDX_SET_UTC_MILLISECONDS 246 /* 'setUTCMilliseconds' */
  3196. #define DUK_STRIDX_SET_MILLISECONDS 247 /* 'setMilliseconds' */
  3197. #define DUK_STRIDX_SET_TIME 248 /* 'setTime' */
  3198. #define DUK_STRIDX_GET_TIMEZONE_OFFSET 249 /* 'getTimezoneOffset' */
  3199. #define DUK_STRIDX_GET_UTC_MILLISECONDS 250 /* 'getUTCMilliseconds' */
  3200. #define DUK_STRIDX_GET_MILLISECONDS 251 /* 'getMilliseconds' */
  3201. #define DUK_STRIDX_GET_UTC_SECONDS 252 /* 'getUTCSeconds' */
  3202. #define DUK_STRIDX_GET_SECONDS 253 /* 'getSeconds' */
  3203. #define DUK_STRIDX_GET_UTC_MINUTES 254 /* 'getUTCMinutes' */
  3204. #define DUK_STRIDX_GET_MINUTES 255 /* 'getMinutes' */
  3205. #define DUK_STRIDX_GET_UTC_HOURS 256 /* 'getUTCHours' */
  3206. #define DUK_STRIDX_GET_HOURS 257 /* 'getHours' */
  3207. #define DUK_STRIDX_GET_UTC_DAY 258 /* 'getUTCDay' */
  3208. #define DUK_STRIDX_GET_DAY 259 /* 'getDay' */
  3209. #define DUK_STRIDX_GET_UTC_DATE 260 /* 'getUTCDate' */
  3210. #define DUK_STRIDX_GET_DATE 261 /* 'getDate' */
  3211. #define DUK_STRIDX_GET_UTC_MONTH 262 /* 'getUTCMonth' */
  3212. #define DUK_STRIDX_GET_MONTH 263 /* 'getMonth' */
  3213. #define DUK_STRIDX_GET_UTC_FULL_YEAR 264 /* 'getUTCFullYear' */
  3214. #define DUK_STRIDX_GET_FULL_YEAR 265 /* 'getFullYear' */
  3215. #define DUK_STRIDX_GET_TIME 266 /* 'getTime' */
  3216. #define DUK_STRIDX_TO_LOCALE_TIME_STRING 267 /* 'toLocaleTimeString' */
  3217. #define DUK_STRIDX_TO_LOCALE_DATE_STRING 268 /* 'toLocaleDateString' */
  3218. #define DUK_STRIDX_TO_TIME_STRING 269 /* 'toTimeString' */
  3219. #define DUK_STRIDX_TO_DATE_STRING 270 /* 'toDateString' */
  3220. #define DUK_STRIDX_NOW 271 /* 'now' */
  3221. #define DUK_STRIDX_UTC 272 /* 'UTC' */
  3222. #define DUK_STRIDX_PARSE 273 /* 'parse' */
  3223. #define DUK_STRIDX_TO_PRECISION 274 /* 'toPrecision' */
  3224. #define DUK_STRIDX_TO_EXPONENTIAL 275 /* 'toExponential' */
  3225. #define DUK_STRIDX_TO_FIXED 276 /* 'toFixed' */
  3226. #define DUK_STRIDX_POSITIVE_INFINITY 277 /* 'POSITIVE_INFINITY' */
  3227. #define DUK_STRIDX_NEGATIVE_INFINITY 278 /* 'NEGATIVE_INFINITY' */
  3228. #define DUK_STRIDX_NAN 279 /* 'NaN' */
  3229. #define DUK_STRIDX_MIN_VALUE 280 /* 'MIN_VALUE' */
  3230. #define DUK_STRIDX_MAX_VALUE 281 /* 'MAX_VALUE' */
  3231. #define DUK_STRIDX_SUBSTR 282 /* 'substr' */
  3232. #define DUK_STRIDX_TRIM 283 /* 'trim' */
  3233. #define DUK_STRIDX_TO_LOCALE_UPPER_CASE 284 /* 'toLocaleUpperCase' */
  3234. #define DUK_STRIDX_TO_UPPER_CASE 285 /* 'toUpperCase' */
  3235. #define DUK_STRIDX_TO_LOCALE_LOWER_CASE 286 /* 'toLocaleLowerCase' */
  3236. #define DUK_STRIDX_TO_LOWER_CASE 287 /* 'toLowerCase' */
  3237. #define DUK_STRIDX_SUBSTRING 288 /* 'substring' */
  3238. #define DUK_STRIDX_SPLIT 289 /* 'split' */
  3239. #define DUK_STRIDX_SEARCH 290 /* 'search' */
  3240. #define DUK_STRIDX_REPLACE 291 /* 'replace' */
  3241. #define DUK_STRIDX_MATCH 292 /* 'match' */
  3242. #define DUK_STRIDX_LOCALE_COMPARE 293 /* 'localeCompare' */
  3243. #define DUK_STRIDX_CHAR_CODE_AT 294 /* 'charCodeAt' */
  3244. #define DUK_STRIDX_CHAR_AT 295 /* 'charAt' */
  3245. #define DUK_STRIDX_FROM_CHAR_CODE 296 /* 'fromCharCode' */
  3246. #define DUK_STRIDX_REDUCE_RIGHT 297 /* 'reduceRight' */
  3247. #define DUK_STRIDX_REDUCE 298 /* 'reduce' */
  3248. #define DUK_STRIDX_FILTER 299 /* 'filter' */
  3249. #define DUK_STRIDX_MAP 300 /* 'map' */
  3250. #define DUK_STRIDX_FOR_EACH 301 /* 'forEach' */
  3251. #define DUK_STRIDX_SOME 302 /* 'some' */
  3252. #define DUK_STRIDX_EVERY 303 /* 'every' */
  3253. #define DUK_STRIDX_LAST_INDEX_OF 304 /* 'lastIndexOf' */
  3254. #define DUK_STRIDX_INDEX_OF 305 /* 'indexOf' */
  3255. #define DUK_STRIDX_UNSHIFT 306 /* 'unshift' */
  3256. #define DUK_STRIDX_SPLICE 307 /* 'splice' */
  3257. #define DUK_STRIDX_SORT 308 /* 'sort' */
  3258. #define DUK_STRIDX_SLICE 309 /* 'slice' */
  3259. #define DUK_STRIDX_SHIFT 310 /* 'shift' */
  3260. #define DUK_STRIDX_REVERSE 311 /* 'reverse' */
  3261. #define DUK_STRIDX_PUSH 312 /* 'push' */
  3262. #define DUK_STRIDX_POP 313 /* 'pop' */
  3263. #define DUK_STRIDX_JOIN 314 /* 'join' */
  3264. #define DUK_STRIDX_CONCAT 315 /* 'concat' */
  3265. #define DUK_STRIDX_IS_ARRAY 316 /* 'isArray' */
  3266. #define DUK_STRIDX_LC_ARGUMENTS 317 /* 'arguments' */
  3267. #define DUK_STRIDX_CALLER 318 /* 'caller' */
  3268. #define DUK_STRIDX_BIND 319 /* 'bind' */
  3269. #define DUK_STRIDX_CALL 320 /* 'call' */
  3270. #define DUK_STRIDX_APPLY 321 /* 'apply' */
  3271. #define DUK_STRIDX_PROPERTY_IS_ENUMERABLE 322 /* 'propertyIsEnumerable' */
  3272. #define DUK_STRIDX_IS_PROTOTYPE_OF 323 /* 'isPrototypeOf' */
  3273. #define DUK_STRIDX_HAS_OWN_PROPERTY 324 /* 'hasOwnProperty' */
  3274. #define DUK_STRIDX_VALUE_OF 325 /* 'valueOf' */
  3275. #define DUK_STRIDX_TO_LOCALE_STRING 326 /* 'toLocaleString' */
  3276. #define DUK_STRIDX_TO_STRING 327 /* 'toString' */
  3277. #define DUK_STRIDX_CONSTRUCTOR 328 /* 'constructor' */
  3278. #define DUK_STRIDX_SET 329 /* 'set' */
  3279. #define DUK_STRIDX_GET 330 /* 'get' */
  3280. #define DUK_STRIDX_ENUMERABLE 331 /* 'enumerable' */
  3281. #define DUK_STRIDX_CONFIGURABLE 332 /* 'configurable' */
  3282. #define DUK_STRIDX_WRITABLE 333 /* 'writable' */
  3283. #define DUK_STRIDX_VALUE 334 /* 'value' */
  3284. #define DUK_STRIDX_KEYS 335 /* 'keys' */
  3285. #define DUK_STRIDX_IS_EXTENSIBLE 336 /* 'isExtensible' */
  3286. #define DUK_STRIDX_IS_FROZEN 337 /* 'isFrozen' */
  3287. #define DUK_STRIDX_IS_SEALED 338 /* 'isSealed' */
  3288. #define DUK_STRIDX_PREVENT_EXTENSIONS 339 /* 'preventExtensions' */
  3289. #define DUK_STRIDX_FREEZE 340 /* 'freeze' */
  3290. #define DUK_STRIDX_SEAL 341 /* 'seal' */
  3291. #define DUK_STRIDX_DEFINE_PROPERTIES 342 /* 'defineProperties' */
  3292. #define DUK_STRIDX_DEFINE_PROPERTY 343 /* 'defineProperty' */
  3293. #define DUK_STRIDX_CREATE 344 /* 'create' */
  3294. #define DUK_STRIDX_GET_OWN_PROPERTY_NAMES 345 /* 'getOwnPropertyNames' */
  3295. #define DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR 346 /* 'getOwnPropertyDescriptor' */
  3296. #define DUK_STRIDX_GET_PROTOTYPE_OF 347 /* 'getPrototypeOf' */
  3297. #define DUK_STRIDX_PROTOTYPE 348 /* 'prototype' */
  3298. #define DUK_STRIDX_LENGTH 349 /* 'length' */
  3299. #define DUK_STRIDX_ALERT 350 /* 'alert' */
  3300. #define DUK_STRIDX_PRINT 351 /* 'print' */
  3301. #define DUK_STRIDX_UNESCAPE 352 /* 'unescape' */
  3302. #define DUK_STRIDX_ESCAPE 353 /* 'escape' */
  3303. #define DUK_STRIDX_ENCODE_URI_COMPONENT 354 /* 'encodeURIComponent' */
  3304. #define DUK_STRIDX_ENCODE_URI 355 /* 'encodeURI' */
  3305. #define DUK_STRIDX_DECODE_URI_COMPONENT 356 /* 'decodeURIComponent' */
  3306. #define DUK_STRIDX_DECODE_URI 357 /* 'decodeURI' */
  3307. #define DUK_STRIDX_IS_FINITE 358 /* 'isFinite' */
  3308. #define DUK_STRIDX_IS_NAN 359 /* 'isNaN' */
  3309. #define DUK_STRIDX_PARSE_FLOAT 360 /* 'parseFloat' */
  3310. #define DUK_STRIDX_PARSE_INT 361 /* 'parseInt' */
  3311. #define DUK_STRIDX_EVAL 362 /* 'eval' */
  3312. #define DUK_STRIDX_URI_ERROR 363 /* 'URIError' */
  3313. #define DUK_STRIDX_TYPE_ERROR 364 /* 'TypeError' */
  3314. #define DUK_STRIDX_SYNTAX_ERROR 365 /* 'SyntaxError' */
  3315. #define DUK_STRIDX_REFERENCE_ERROR 366 /* 'ReferenceError' */
  3316. #define DUK_STRIDX_RANGE_ERROR 367 /* 'RangeError' */
  3317. #define DUK_STRIDX_EVAL_ERROR 368 /* 'EvalError' */
  3318. #define DUK_STRIDX_BREAK 369 /* 'break' */
  3319. #define DUK_STRIDX_CASE 370 /* 'case' */
  3320. #define DUK_STRIDX_CATCH 371 /* 'catch' */
  3321. #define DUK_STRIDX_CONTINUE 372 /* 'continue' */
  3322. #define DUK_STRIDX_DEBUGGER 373 /* 'debugger' */
  3323. #define DUK_STRIDX_DEFAULT 374 /* 'default' */
  3324. #define DUK_STRIDX_DELETE 375 /* 'delete' */
  3325. #define DUK_STRIDX_DO 376 /* 'do' */
  3326. #define DUK_STRIDX_ELSE 377 /* 'else' */
  3327. #define DUK_STRIDX_FINALLY 378 /* 'finally' */
  3328. #define DUK_STRIDX_FOR 379 /* 'for' */
  3329. #define DUK_STRIDX_LC_FUNCTION 380 /* 'function' */
  3330. #define DUK_STRIDX_IF 381 /* 'if' */
  3331. #define DUK_STRIDX_IN 382 /* 'in' */
  3332. #define DUK_STRIDX_INSTANCEOF 383 /* 'instanceof' */
  3333. #define DUK_STRIDX_NEW 384 /* 'new' */
  3334. #define DUK_STRIDX_RETURN 385 /* 'return' */
  3335. #define DUK_STRIDX_SWITCH 386 /* 'switch' */
  3336. #define DUK_STRIDX_THIS 387 /* 'this' */
  3337. #define DUK_STRIDX_THROW 388 /* 'throw' */
  3338. #define DUK_STRIDX_TRY 389 /* 'try' */
  3339. #define DUK_STRIDX_TYPEOF 390 /* 'typeof' */
  3340. #define DUK_STRIDX_VAR 391 /* 'var' */
  3341. #define DUK_STRIDX_VOID 392 /* 'void' */
  3342. #define DUK_STRIDX_WHILE 393 /* 'while' */
  3343. #define DUK_STRIDX_WITH 394 /* 'with' */
  3344. #define DUK_STRIDX_CLASS 395 /* 'class' */
  3345. #define DUK_STRIDX_CONST 396 /* 'const' */
  3346. #define DUK_STRIDX_ENUM 397 /* 'enum' */
  3347. #define DUK_STRIDX_EXPORT 398 /* 'export' */
  3348. #define DUK_STRIDX_EXTENDS 399 /* 'extends' */
  3349. #define DUK_STRIDX_IMPORT 400 /* 'import' */
  3350. #define DUK_STRIDX_SUPER 401 /* 'super' */
  3351. #define DUK_STRIDX_LC_NULL 402 /* 'null' */
  3352. #define DUK_STRIDX_TRUE 403 /* 'true' */
  3353. #define DUK_STRIDX_FALSE 404 /* 'false' */
  3354. #define DUK_STRIDX_IMPLEMENTS 405 /* 'implements' */
  3355. #define DUK_STRIDX_INTERFACE 406 /* 'interface' */
  3356. #define DUK_STRIDX_LET 407 /* 'let' */
  3357. #define DUK_STRIDX_PACKAGE 408 /* 'package' */
  3358. #define DUK_STRIDX_PRIVATE 409 /* 'private' */
  3359. #define DUK_STRIDX_PROTECTED 410 /* 'protected' */
  3360. #define DUK_STRIDX_PUBLIC 411 /* 'public' */
  3361. #define DUK_STRIDX_STATIC 412 /* 'static' */
  3362. #define DUK_STRIDX_YIELD 413 /* 'yield' */
  3363. #define DUK_HEAP_STRING_UC_LOGGER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_LOGGER)
  3364. #define DUK_HTHREAD_STRING_UC_LOGGER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_LOGGER)
  3365. #define DUK_HEAP_STRING_UC_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_THREAD)
  3366. #define DUK_HTHREAD_STRING_UC_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_THREAD)
  3367. #define DUK_HEAP_STRING_UC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_POINTER)
  3368. #define DUK_HTHREAD_STRING_UC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_POINTER)
  3369. #define DUK_HEAP_STRING_DEC_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEC_ENV)
  3370. #define DUK_HTHREAD_STRING_DEC_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEC_ENV)
  3371. #define DUK_HEAP_STRING_OBJ_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OBJ_ENV)
  3372. #define DUK_HTHREAD_STRING_OBJ_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OBJ_ENV)
  3373. #define DUK_HEAP_STRING_FLOAT64_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT64_ARRAY)
  3374. #define DUK_HTHREAD_STRING_FLOAT64_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT64_ARRAY)
  3375. #define DUK_HEAP_STRING_FLOAT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT32_ARRAY)
  3376. #define DUK_HTHREAD_STRING_FLOAT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT32_ARRAY)
  3377. #define DUK_HEAP_STRING_UINT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT32_ARRAY)
  3378. #define DUK_HTHREAD_STRING_UINT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT32_ARRAY)
  3379. #define DUK_HEAP_STRING_INT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT32_ARRAY)
  3380. #define DUK_HTHREAD_STRING_INT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT32_ARRAY)
  3381. #define DUK_HEAP_STRING_UINT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT16_ARRAY)
  3382. #define DUK_HTHREAD_STRING_UINT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT16_ARRAY)
  3383. #define DUK_HEAP_STRING_INT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT16_ARRAY)
  3384. #define DUK_HTHREAD_STRING_INT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT16_ARRAY)
  3385. #define DUK_HEAP_STRING_UINT8_CLAMPED_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_CLAMPED_ARRAY)
  3386. #define DUK_HTHREAD_STRING_UINT8_CLAMPED_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_CLAMPED_ARRAY)
  3387. #define DUK_HEAP_STRING_UINT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_ARRAY)
  3388. #define DUK_HTHREAD_STRING_UINT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_ARRAY)
  3389. #define DUK_HEAP_STRING_INT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT8_ARRAY)
  3390. #define DUK_HTHREAD_STRING_INT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT8_ARRAY)
  3391. #define DUK_HEAP_STRING_DATA_VIEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA_VIEW)
  3392. #define DUK_HTHREAD_STRING_DATA_VIEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA_VIEW)
  3393. #define DUK_HEAP_STRING_ARRAY_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY_BUFFER)
  3394. #define DUK_HTHREAD_STRING_ARRAY_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY_BUFFER)
  3395. #define DUK_HEAP_STRING_UC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BUFFER)
  3396. #define DUK_HTHREAD_STRING_UC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BUFFER)
  3397. #define DUK_HEAP_STRING_EMPTY_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EMPTY_STRING)
  3398. #define DUK_HTHREAD_STRING_EMPTY_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EMPTY_STRING)
  3399. #define DUK_HEAP_STRING_GLOBAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GLOBAL)
  3400. #define DUK_HTHREAD_STRING_GLOBAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GLOBAL)
  3401. #define DUK_HEAP_STRING_UC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ARGUMENTS)
  3402. #define DUK_HTHREAD_STRING_UC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ARGUMENTS)
  3403. #define DUK_HEAP_STRING_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON)
  3404. #define DUK_HTHREAD_STRING_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON)
  3405. #define DUK_HEAP_STRING_MATH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MATH)
  3406. #define DUK_HTHREAD_STRING_MATH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MATH)
  3407. #define DUK_HEAP_STRING_UC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ERROR)
  3408. #define DUK_HTHREAD_STRING_UC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ERROR)
  3409. #define DUK_HEAP_STRING_REG_EXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REG_EXP)
  3410. #define DUK_HTHREAD_STRING_REG_EXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REG_EXP)
  3411. #define DUK_HEAP_STRING_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATE)
  3412. #define DUK_HTHREAD_STRING_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATE)
  3413. #define DUK_HEAP_STRING_UC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NUMBER)
  3414. #define DUK_HTHREAD_STRING_UC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NUMBER)
  3415. #define DUK_HEAP_STRING_UC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BOOLEAN)
  3416. #define DUK_HTHREAD_STRING_UC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BOOLEAN)
  3417. #define DUK_HEAP_STRING_UC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_STRING)
  3418. #define DUK_HTHREAD_STRING_UC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_STRING)
  3419. #define DUK_HEAP_STRING_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY)
  3420. #define DUK_HTHREAD_STRING_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY)
  3421. #define DUK_HEAP_STRING_UC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_FUNCTION)
  3422. #define DUK_HTHREAD_STRING_UC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_FUNCTION)
  3423. #define DUK_HEAP_STRING_UC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_OBJECT)
  3424. #define DUK_HTHREAD_STRING_UC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_OBJECT)
  3425. #define DUK_HEAP_STRING_UC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NULL)
  3426. #define DUK_HTHREAD_STRING_UC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NULL)
  3427. #define DUK_HEAP_STRING_UC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_UNDEFINED)
  3428. #define DUK_HTHREAD_STRING_UC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_UNDEFINED)
  3429. #define DUK_HEAP_STRING_JSON_EXT_FUNCTION2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION2)
  3430. #define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION2)
  3431. #define DUK_HEAP_STRING_JSON_EXT_FUNCTION1(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION1)
  3432. #define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION1(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION1)
  3433. #define DUK_HEAP_STRING_JSON_EXT_NEGINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NEGINF)
  3434. #define DUK_HTHREAD_STRING_JSON_EXT_NEGINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NEGINF)
  3435. #define DUK_HEAP_STRING_JSON_EXT_POSINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_POSINF)
  3436. #define DUK_HTHREAD_STRING_JSON_EXT_POSINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_POSINF)
  3437. #define DUK_HEAP_STRING_JSON_EXT_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NAN)
  3438. #define DUK_HTHREAD_STRING_JSON_EXT_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NAN)
  3439. #define DUK_HEAP_STRING_JSON_EXT_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_UNDEFINED)
  3440. #define DUK_HTHREAD_STRING_JSON_EXT_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_UNDEFINED)
  3441. #define DUK_HEAP_STRING_TO_LOG_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOG_STRING)
  3442. #define DUK_HTHREAD_STRING_TO_LOG_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOG_STRING)
  3443. #define DUK_HEAP_STRING_CLOG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLOG)
  3444. #define DUK_HTHREAD_STRING_CLOG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLOG)
  3445. #define DUK_HEAP_STRING_LC_L(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_L)
  3446. #define DUK_HTHREAD_STRING_LC_L(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_L)
  3447. #define DUK_HEAP_STRING_LC_N(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_N)
  3448. #define DUK_HTHREAD_STRING_LC_N(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_N)
  3449. #define DUK_HEAP_STRING_LC_FATAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FATAL)
  3450. #define DUK_HTHREAD_STRING_LC_FATAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FATAL)
  3451. #define DUK_HEAP_STRING_LC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ERROR)
  3452. #define DUK_HTHREAD_STRING_LC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ERROR)
  3453. #define DUK_HEAP_STRING_LC_WARN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_WARN)
  3454. #define DUK_HTHREAD_STRING_LC_WARN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_WARN)
  3455. #define DUK_HEAP_STRING_LC_DEBUG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_DEBUG)
  3456. #define DUK_HTHREAD_STRING_LC_DEBUG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_DEBUG)
  3457. #define DUK_HEAP_STRING_LC_TRACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_TRACE)
  3458. #define DUK_HTHREAD_STRING_LC_TRACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_TRACE)
  3459. #define DUK_HEAP_STRING_RAW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RAW)
  3460. #define DUK_HTHREAD_STRING_RAW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RAW)
  3461. #define DUK_HEAP_STRING_FMT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FMT)
  3462. #define DUK_HTHREAD_STRING_FMT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FMT)
  3463. #define DUK_HEAP_STRING_CURRENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CURRENT)
  3464. #define DUK_HTHREAD_STRING_CURRENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CURRENT)
  3465. #define DUK_HEAP_STRING_RESUME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RESUME)
  3466. #define DUK_HTHREAD_STRING_RESUME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RESUME)
  3467. #define DUK_HEAP_STRING_COMPACT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPACT)
  3468. #define DUK_HTHREAD_STRING_COMPACT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPACT)
  3469. #define DUK_HEAP_STRING_JC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JC)
  3470. #define DUK_HTHREAD_STRING_JC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JC)
  3471. #define DUK_HEAP_STRING_JX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JX)
  3472. #define DUK_HTHREAD_STRING_JX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JX)
  3473. #define DUK_HEAP_STRING_BASE64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BASE64)
  3474. #define DUK_HTHREAD_STRING_BASE64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BASE64)
  3475. #define DUK_HEAP_STRING_HEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HEX)
  3476. #define DUK_HTHREAD_STRING_HEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HEX)
  3477. #define DUK_HEAP_STRING_DEC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEC)
  3478. #define DUK_HTHREAD_STRING_DEC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEC)
  3479. #define DUK_HEAP_STRING_ENC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENC)
  3480. #define DUK_HTHREAD_STRING_ENC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENC)
  3481. #define DUK_HEAP_STRING_FIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FIN)
  3482. #define DUK_HTHREAD_STRING_FIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FIN)
  3483. #define DUK_HEAP_STRING_GC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GC)
  3484. #define DUK_HTHREAD_STRING_GC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GC)
  3485. #define DUK_HEAP_STRING_ACT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ACT)
  3486. #define DUK_HTHREAD_STRING_ACT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ACT)
  3487. #define DUK_HEAP_STRING_LC_INFO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_INFO)
  3488. #define DUK_HTHREAD_STRING_LC_INFO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_INFO)
  3489. #define DUK_HEAP_STRING_VERSION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VERSION)
  3490. #define DUK_HTHREAD_STRING_VERSION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VERSION)
  3491. #define DUK_HEAP_STRING_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENV)
  3492. #define DUK_HTHREAD_STRING_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENV)
  3493. #define DUK_HEAP_STRING_MOD_LOADED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MOD_LOADED)
  3494. #define DUK_HTHREAD_STRING_MOD_LOADED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MOD_LOADED)
  3495. #define DUK_HEAP_STRING_MOD_SEARCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MOD_SEARCH)
  3496. #define DUK_HTHREAD_STRING_MOD_SEARCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MOD_SEARCH)
  3497. #define DUK_HEAP_STRING_ERR_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_THROW)
  3498. #define DUK_HTHREAD_STRING_ERR_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_THROW)
  3499. #define DUK_HEAP_STRING_ERR_CREATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_CREATE)
  3500. #define DUK_HTHREAD_STRING_ERR_CREATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_CREATE)
  3501. #define DUK_HEAP_STRING_COMPILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPILE)
  3502. #define DUK_HTHREAD_STRING_COMPILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPILE)
  3503. #define DUK_HEAP_STRING_INT_REGBASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_REGBASE)
  3504. #define DUK_HTHREAD_STRING_INT_REGBASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_REGBASE)
  3505. #define DUK_HEAP_STRING_INT_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_THREAD)
  3506. #define DUK_HTHREAD_STRING_INT_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_THREAD)
  3507. #define DUK_HEAP_STRING_INT_HANDLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_HANDLER)
  3508. #define DUK_HTHREAD_STRING_INT_HANDLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_HANDLER)
  3509. #define DUK_HEAP_STRING_INT_FINALIZER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FINALIZER)
  3510. #define DUK_HTHREAD_STRING_INT_FINALIZER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FINALIZER)
  3511. #define DUK_HEAP_STRING_INT_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_CALLEE)
  3512. #define DUK_HTHREAD_STRING_INT_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_CALLEE)
  3513. #define DUK_HEAP_STRING_INT_MAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_MAP)
  3514. #define DUK_HTHREAD_STRING_INT_MAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_MAP)
  3515. #define DUK_HEAP_STRING_INT_ARGS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_ARGS)
  3516. #define DUK_HTHREAD_STRING_INT_ARGS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_ARGS)
  3517. #define DUK_HEAP_STRING_INT_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_THIS)
  3518. #define DUK_HTHREAD_STRING_INT_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_THIS)
  3519. #define DUK_HEAP_STRING_INT_PC2LINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_PC2LINE)
  3520. #define DUK_HTHREAD_STRING_INT_PC2LINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_PC2LINE)
  3521. #define DUK_HEAP_STRING_INT_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_SOURCE)
  3522. #define DUK_HTHREAD_STRING_INT_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_SOURCE)
  3523. #define DUK_HEAP_STRING_INT_VARENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARENV)
  3524. #define DUK_HTHREAD_STRING_INT_VARENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARENV)
  3525. #define DUK_HEAP_STRING_INT_LEXENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_LEXENV)
  3526. #define DUK_HTHREAD_STRING_INT_LEXENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_LEXENV)
  3527. #define DUK_HEAP_STRING_INT_VARMAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARMAP)
  3528. #define DUK_HTHREAD_STRING_INT_VARMAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARMAP)
  3529. #define DUK_HEAP_STRING_INT_FORMALS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FORMALS)
  3530. #define DUK_HTHREAD_STRING_INT_FORMALS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FORMALS)
  3531. #define DUK_HEAP_STRING_INT_BYTECODE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_BYTECODE)
  3532. #define DUK_HTHREAD_STRING_INT_BYTECODE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_BYTECODE)
  3533. #define DUK_HEAP_STRING_INT_NEXT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_NEXT)
  3534. #define DUK_HTHREAD_STRING_INT_NEXT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_NEXT)
  3535. #define DUK_HEAP_STRING_INT_TARGET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TARGET)
  3536. #define DUK_HTHREAD_STRING_INT_TARGET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TARGET)
  3537. #define DUK_HEAP_STRING_INT_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VALUE)
  3538. #define DUK_HTHREAD_STRING_INT_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VALUE)
  3539. #define DUK_HEAP_STRING_LC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_POINTER)
  3540. #define DUK_HTHREAD_STRING_LC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_POINTER)
  3541. #define DUK_HEAP_STRING_INT_TRACEDATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TRACEDATA)
  3542. #define DUK_HTHREAD_STRING_INT_TRACEDATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TRACEDATA)
  3543. #define DUK_HEAP_STRING_LINE_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LINE_NUMBER)
  3544. #define DUK_HTHREAD_STRING_LINE_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LINE_NUMBER)
  3545. #define DUK_HEAP_STRING_FILE_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILE_NAME)
  3546. #define DUK_HTHREAD_STRING_FILE_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILE_NAME)
  3547. #define DUK_HEAP_STRING_PC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PC)
  3548. #define DUK_HTHREAD_STRING_PC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PC)
  3549. #define DUK_HEAP_STRING_STACK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STACK)
  3550. #define DUK_HTHREAD_STRING_STACK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STACK)
  3551. #define DUK_HEAP_STRING_THROW_TYPE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THROW_TYPE_ERROR)
  3552. #define DUK_HTHREAD_STRING_THROW_TYPE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THROW_TYPE_ERROR)
  3553. #define DUK_HEAP_STRING_DUKTAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DUKTAPE)
  3554. #define DUK_HTHREAD_STRING_DUKTAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DUKTAPE)
  3555. #define DUK_HEAP_STRING_SET_FLOAT64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FLOAT64)
  3556. #define DUK_HTHREAD_STRING_SET_FLOAT64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FLOAT64)
  3557. #define DUK_HEAP_STRING_SET_FLOAT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FLOAT32)
  3558. #define DUK_HTHREAD_STRING_SET_FLOAT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FLOAT32)
  3559. #define DUK_HEAP_STRING_SET_UINT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT32)
  3560. #define DUK_HTHREAD_STRING_SET_UINT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT32)
  3561. #define DUK_HEAP_STRING_SET_INT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT32)
  3562. #define DUK_HTHREAD_STRING_SET_INT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT32)
  3563. #define DUK_HEAP_STRING_SET_UINT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT16)
  3564. #define DUK_HTHREAD_STRING_SET_UINT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT16)
  3565. #define DUK_HEAP_STRING_SET_INT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT16)
  3566. #define DUK_HTHREAD_STRING_SET_INT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT16)
  3567. #define DUK_HEAP_STRING_SET_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UINT8)
  3568. #define DUK_HTHREAD_STRING_SET_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UINT8)
  3569. #define DUK_HEAP_STRING_SET_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_INT8)
  3570. #define DUK_HTHREAD_STRING_SET_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_INT8)
  3571. #define DUK_HEAP_STRING_GET_FLOAT64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FLOAT64)
  3572. #define DUK_HTHREAD_STRING_GET_FLOAT64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FLOAT64)
  3573. #define DUK_HEAP_STRING_GET_FLOAT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FLOAT32)
  3574. #define DUK_HTHREAD_STRING_GET_FLOAT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FLOAT32)
  3575. #define DUK_HEAP_STRING_GET_UINT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT32)
  3576. #define DUK_HTHREAD_STRING_GET_UINT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT32)
  3577. #define DUK_HEAP_STRING_GET_INT32(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT32)
  3578. #define DUK_HTHREAD_STRING_GET_INT32(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT32)
  3579. #define DUK_HEAP_STRING_GET_UINT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT16)
  3580. #define DUK_HTHREAD_STRING_GET_UINT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT16)
  3581. #define DUK_HEAP_STRING_GET_INT16(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT16)
  3582. #define DUK_HTHREAD_STRING_GET_INT16(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT16)
  3583. #define DUK_HEAP_STRING_GET_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UINT8)
  3584. #define DUK_HTHREAD_STRING_GET_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UINT8)
  3585. #define DUK_HEAP_STRING_GET_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_INT8)
  3586. #define DUK_HTHREAD_STRING_GET_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_INT8)
  3587. #define DUK_HEAP_STRING_SUBARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBARRAY)
  3588. #define DUK_HTHREAD_STRING_SUBARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBARRAY)
  3589. #define DUK_HEAP_STRING_BYTES_PER_ELEMENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTES_PER_ELEMENT)
  3590. #define DUK_HTHREAD_STRING_BYTES_PER_ELEMENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTES_PER_ELEMENT)
  3591. #define DUK_HEAP_STRING_BYTE_OFFSET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTE_OFFSET)
  3592. #define DUK_HTHREAD_STRING_BYTE_OFFSET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTE_OFFSET)
  3593. #define DUK_HEAP_STRING_LC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BUFFER)
  3594. #define DUK_HTHREAD_STRING_LC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BUFFER)
  3595. #define DUK_HEAP_STRING_IS_VIEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_VIEW)
  3596. #define DUK_HTHREAD_STRING_IS_VIEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_VIEW)
  3597. #define DUK_HEAP_STRING_DATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA)
  3598. #define DUK_HTHREAD_STRING_DATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA)
  3599. #define DUK_HEAP_STRING_TYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPE)
  3600. #define DUK_HTHREAD_STRING_TYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPE)
  3601. #define DUK_HEAP_STRING_WRITE_INT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT_BE)
  3602. #define DUK_HTHREAD_STRING_WRITE_INT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT_BE)
  3603. #define DUK_HEAP_STRING_WRITE_INT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT_LE)
  3604. #define DUK_HTHREAD_STRING_WRITE_INT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT_LE)
  3605. #define DUK_HEAP_STRING_WRITE_UINT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT_BE)
  3606. #define DUK_HTHREAD_STRING_WRITE_UINT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT_BE)
  3607. #define DUK_HEAP_STRING_WRITE_UINT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT_LE)
  3608. #define DUK_HTHREAD_STRING_WRITE_UINT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT_LE)
  3609. #define DUK_HEAP_STRING_WRITE_DOUBLE_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_DOUBLE_BE)
  3610. #define DUK_HTHREAD_STRING_WRITE_DOUBLE_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_DOUBLE_BE)
  3611. #define DUK_HEAP_STRING_WRITE_DOUBLE_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_DOUBLE_LE)
  3612. #define DUK_HTHREAD_STRING_WRITE_DOUBLE_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_DOUBLE_LE)
  3613. #define DUK_HEAP_STRING_WRITE_FLOAT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_FLOAT_BE)
  3614. #define DUK_HTHREAD_STRING_WRITE_FLOAT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_FLOAT_BE)
  3615. #define DUK_HEAP_STRING_WRITE_FLOAT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_FLOAT_LE)
  3616. #define DUK_HTHREAD_STRING_WRITE_FLOAT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_FLOAT_LE)
  3617. #define DUK_HEAP_STRING_WRITE_INT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT32_BE)
  3618. #define DUK_HTHREAD_STRING_WRITE_INT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT32_BE)
  3619. #define DUK_HEAP_STRING_WRITE_INT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT32_LE)
  3620. #define DUK_HTHREAD_STRING_WRITE_INT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT32_LE)
  3621. #define DUK_HEAP_STRING_WRITE_UINT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT32_BE)
  3622. #define DUK_HTHREAD_STRING_WRITE_UINT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT32_BE)
  3623. #define DUK_HEAP_STRING_WRITE_UINT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT32_LE)
  3624. #define DUK_HTHREAD_STRING_WRITE_UINT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT32_LE)
  3625. #define DUK_HEAP_STRING_WRITE_INT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT16_BE)
  3626. #define DUK_HTHREAD_STRING_WRITE_INT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT16_BE)
  3627. #define DUK_HEAP_STRING_WRITE_INT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT16_LE)
  3628. #define DUK_HTHREAD_STRING_WRITE_INT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT16_LE)
  3629. #define DUK_HEAP_STRING_WRITE_UINT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT16_BE)
  3630. #define DUK_HTHREAD_STRING_WRITE_UINT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT16_BE)
  3631. #define DUK_HEAP_STRING_WRITE_UINT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT16_LE)
  3632. #define DUK_HTHREAD_STRING_WRITE_UINT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT16_LE)
  3633. #define DUK_HEAP_STRING_WRITE_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_INT8)
  3634. #define DUK_HTHREAD_STRING_WRITE_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_INT8)
  3635. #define DUK_HEAP_STRING_WRITE_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE_UINT8)
  3636. #define DUK_HTHREAD_STRING_WRITE_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE_UINT8)
  3637. #define DUK_HEAP_STRING_READ_INT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT_BE)
  3638. #define DUK_HTHREAD_STRING_READ_INT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT_BE)
  3639. #define DUK_HEAP_STRING_READ_INT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT_LE)
  3640. #define DUK_HTHREAD_STRING_READ_INT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT_LE)
  3641. #define DUK_HEAP_STRING_READ_UINT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT_BE)
  3642. #define DUK_HTHREAD_STRING_READ_UINT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT_BE)
  3643. #define DUK_HEAP_STRING_READ_UINT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT_LE)
  3644. #define DUK_HTHREAD_STRING_READ_UINT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT_LE)
  3645. #define DUK_HEAP_STRING_READ_DOUBLE_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_DOUBLE_BE)
  3646. #define DUK_HTHREAD_STRING_READ_DOUBLE_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_DOUBLE_BE)
  3647. #define DUK_HEAP_STRING_READ_DOUBLE_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_DOUBLE_LE)
  3648. #define DUK_HTHREAD_STRING_READ_DOUBLE_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_DOUBLE_LE)
  3649. #define DUK_HEAP_STRING_READ_FLOAT_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_FLOAT_BE)
  3650. #define DUK_HTHREAD_STRING_READ_FLOAT_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_FLOAT_BE)
  3651. #define DUK_HEAP_STRING_READ_FLOAT_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_FLOAT_LE)
  3652. #define DUK_HTHREAD_STRING_READ_FLOAT_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_FLOAT_LE)
  3653. #define DUK_HEAP_STRING_READ_INT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT32_BE)
  3654. #define DUK_HTHREAD_STRING_READ_INT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT32_BE)
  3655. #define DUK_HEAP_STRING_READ_INT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT32_LE)
  3656. #define DUK_HTHREAD_STRING_READ_INT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT32_LE)
  3657. #define DUK_HEAP_STRING_READ_UINT32_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT32_BE)
  3658. #define DUK_HTHREAD_STRING_READ_UINT32_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT32_BE)
  3659. #define DUK_HEAP_STRING_READ_UINT32_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT32_LE)
  3660. #define DUK_HTHREAD_STRING_READ_UINT32_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT32_LE)
  3661. #define DUK_HEAP_STRING_READ_INT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT16_BE)
  3662. #define DUK_HTHREAD_STRING_READ_INT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT16_BE)
  3663. #define DUK_HEAP_STRING_READ_INT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT16_LE)
  3664. #define DUK_HTHREAD_STRING_READ_INT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT16_LE)
  3665. #define DUK_HEAP_STRING_READ_UINT16_BE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT16_BE)
  3666. #define DUK_HTHREAD_STRING_READ_UINT16_BE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT16_BE)
  3667. #define DUK_HEAP_STRING_READ_UINT16_LE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT16_LE)
  3668. #define DUK_HTHREAD_STRING_READ_UINT16_LE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT16_LE)
  3669. #define DUK_HEAP_STRING_READ_INT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_INT8)
  3670. #define DUK_HTHREAD_STRING_READ_INT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_INT8)
  3671. #define DUK_HEAP_STRING_READ_UINT8(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_READ_UINT8)
  3672. #define DUK_HTHREAD_STRING_READ_UINT8(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_READ_UINT8)
  3673. #define DUK_HEAP_STRING_COPY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COPY)
  3674. #define DUK_HTHREAD_STRING_COPY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COPY)
  3675. #define DUK_HEAP_STRING_EQUALS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EQUALS)
  3676. #define DUK_HTHREAD_STRING_EQUALS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EQUALS)
  3677. #define DUK_HEAP_STRING_FILL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILL)
  3678. #define DUK_HTHREAD_STRING_FILL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILL)
  3679. #define DUK_HEAP_STRING_WRITE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITE)
  3680. #define DUK_HTHREAD_STRING_WRITE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITE)
  3681. #define DUK_HEAP_STRING_COMPARE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPARE)
  3682. #define DUK_HTHREAD_STRING_COMPARE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPARE)
  3683. #define DUK_HEAP_STRING_BYTE_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTE_LENGTH)
  3684. #define DUK_HTHREAD_STRING_BYTE_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTE_LENGTH)
  3685. #define DUK_HEAP_STRING_IS_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_BUFFER)
  3686. #define DUK_HTHREAD_STRING_IS_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_BUFFER)
  3687. #define DUK_HEAP_STRING_IS_ENCODING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_ENCODING)
  3688. #define DUK_HTHREAD_STRING_IS_ENCODING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_ENCODING)
  3689. #define DUK_HEAP_STRING_EXPORTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORTS)
  3690. #define DUK_HTHREAD_STRING_EXPORTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORTS)
  3691. #define DUK_HEAP_STRING_ID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ID)
  3692. #define DUK_HTHREAD_STRING_ID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ID)
  3693. #define DUK_HEAP_STRING_REQUIRE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REQUIRE)
  3694. #define DUK_HTHREAD_STRING_REQUIRE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REQUIRE)
  3695. #define DUK_HEAP_STRING___PROTO__(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX___PROTO__)
  3696. #define DUK_HTHREAD_STRING___PROTO__(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX___PROTO__)
  3697. #define DUK_HEAP_STRING_SET_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_PROTOTYPE_OF)
  3698. #define DUK_HTHREAD_STRING_SET_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_PROTOTYPE_OF)
  3699. #define DUK_HEAP_STRING_OWN_KEYS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OWN_KEYS)
  3700. #define DUK_HTHREAD_STRING_OWN_KEYS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OWN_KEYS)
  3701. #define DUK_HEAP_STRING_ENUMERATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERATE)
  3702. #define DUK_HTHREAD_STRING_ENUMERATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERATE)
  3703. #define DUK_HEAP_STRING_DELETE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE_PROPERTY)
  3704. #define DUK_HTHREAD_STRING_DELETE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE_PROPERTY)
  3705. #define DUK_HEAP_STRING_HAS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HAS)
  3706. #define DUK_HTHREAD_STRING_HAS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HAS)
  3707. #define DUK_HEAP_STRING_PROXY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROXY)
  3708. #define DUK_HTHREAD_STRING_PROXY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROXY)
  3709. #define DUK_HEAP_STRING_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLEE)
  3710. #define DUK_HTHREAD_STRING_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLEE)
  3711. #define DUK_HEAP_STRING_INVALID_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INVALID_DATE)
  3712. #define DUK_HTHREAD_STRING_INVALID_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INVALID_DATE)
  3713. #define DUK_HEAP_STRING_BRACKETED_ELLIPSIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BRACKETED_ELLIPSIS)
  3714. #define DUK_HTHREAD_STRING_BRACKETED_ELLIPSIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BRACKETED_ELLIPSIS)
  3715. #define DUK_HEAP_STRING_NEWLINE_TAB(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEWLINE_TAB)
  3716. #define DUK_HTHREAD_STRING_NEWLINE_TAB(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEWLINE_TAB)
  3717. #define DUK_HEAP_STRING_SPACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPACE)
  3718. #define DUK_HTHREAD_STRING_SPACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPACE)
  3719. #define DUK_HEAP_STRING_COMMA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMMA)
  3720. #define DUK_HTHREAD_STRING_COMMA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMMA)
  3721. #define DUK_HEAP_STRING_MINUS_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_ZERO)
  3722. #define DUK_HTHREAD_STRING_MINUS_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_ZERO)
  3723. #define DUK_HEAP_STRING_PLUS_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PLUS_ZERO)
  3724. #define DUK_HTHREAD_STRING_PLUS_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PLUS_ZERO)
  3725. #define DUK_HEAP_STRING_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ZERO)
  3726. #define DUK_HTHREAD_STRING_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ZERO)
  3727. #define DUK_HEAP_STRING_MINUS_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_INFINITY)
  3728. #define DUK_HTHREAD_STRING_MINUS_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_INFINITY)
  3729. #define DUK_HEAP_STRING_PLUS_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PLUS_INFINITY)
  3730. #define DUK_HTHREAD_STRING_PLUS_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PLUS_INFINITY)
  3731. #define DUK_HEAP_STRING_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INFINITY)
  3732. #define DUK_HTHREAD_STRING_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INFINITY)
  3733. #define DUK_HEAP_STRING_LC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_OBJECT)
  3734. #define DUK_HTHREAD_STRING_LC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_OBJECT)
  3735. #define DUK_HEAP_STRING_LC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_STRING)
  3736. #define DUK_HTHREAD_STRING_LC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_STRING)
  3737. #define DUK_HEAP_STRING_LC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NUMBER)
  3738. #define DUK_HTHREAD_STRING_LC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NUMBER)
  3739. #define DUK_HEAP_STRING_LC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BOOLEAN)
  3740. #define DUK_HTHREAD_STRING_LC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BOOLEAN)
  3741. #define DUK_HEAP_STRING_LC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_UNDEFINED)
  3742. #define DUK_HTHREAD_STRING_LC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_UNDEFINED)
  3743. #define DUK_HEAP_STRING_STRINGIFY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STRINGIFY)
  3744. #define DUK_HTHREAD_STRING_STRINGIFY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STRINGIFY)
  3745. #define DUK_HEAP_STRING_TAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TAN)
  3746. #define DUK_HTHREAD_STRING_TAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TAN)
  3747. #define DUK_HEAP_STRING_SQRT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT)
  3748. #define DUK_HTHREAD_STRING_SQRT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT)
  3749. #define DUK_HEAP_STRING_SIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SIN)
  3750. #define DUK_HTHREAD_STRING_SIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SIN)
  3751. #define DUK_HEAP_STRING_ROUND(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ROUND)
  3752. #define DUK_HTHREAD_STRING_ROUND(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ROUND)
  3753. #define DUK_HEAP_STRING_RANDOM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RANDOM)
  3754. #define DUK_HTHREAD_STRING_RANDOM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RANDOM)
  3755. #define DUK_HEAP_STRING_POW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POW)
  3756. #define DUK_HTHREAD_STRING_POW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POW)
  3757. #define DUK_HEAP_STRING_MIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MIN)
  3758. #define DUK_HTHREAD_STRING_MIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MIN)
  3759. #define DUK_HEAP_STRING_MAX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAX)
  3760. #define DUK_HTHREAD_STRING_MAX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAX)
  3761. #define DUK_HEAP_STRING_LOG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG)
  3762. #define DUK_HTHREAD_STRING_LOG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG)
  3763. #define DUK_HEAP_STRING_FLOOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOOR)
  3764. #define DUK_HTHREAD_STRING_FLOOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOOR)
  3765. #define DUK_HEAP_STRING_EXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXP)
  3766. #define DUK_HTHREAD_STRING_EXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXP)
  3767. #define DUK_HEAP_STRING_COS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COS)
  3768. #define DUK_HTHREAD_STRING_COS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COS)
  3769. #define DUK_HEAP_STRING_CEIL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CEIL)
  3770. #define DUK_HTHREAD_STRING_CEIL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CEIL)
  3771. #define DUK_HEAP_STRING_ATAN2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ATAN2)
  3772. #define DUK_HTHREAD_STRING_ATAN2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ATAN2)
  3773. #define DUK_HEAP_STRING_ATAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ATAN)
  3774. #define DUK_HTHREAD_STRING_ATAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ATAN)
  3775. #define DUK_HEAP_STRING_ASIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ASIN)
  3776. #define DUK_HTHREAD_STRING_ASIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ASIN)
  3777. #define DUK_HEAP_STRING_ACOS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ACOS)
  3778. #define DUK_HTHREAD_STRING_ACOS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ACOS)
  3779. #define DUK_HEAP_STRING_ABS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ABS)
  3780. #define DUK_HTHREAD_STRING_ABS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ABS)
  3781. #define DUK_HEAP_STRING_SQRT2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT2)
  3782. #define DUK_HTHREAD_STRING_SQRT2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT2)
  3783. #define DUK_HEAP_STRING_SQRT1_2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SQRT1_2)
  3784. #define DUK_HTHREAD_STRING_SQRT1_2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SQRT1_2)
  3785. #define DUK_HEAP_STRING_PI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PI)
  3786. #define DUK_HTHREAD_STRING_PI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PI)
  3787. #define DUK_HEAP_STRING_LOG10E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG10E)
  3788. #define DUK_HTHREAD_STRING_LOG10E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG10E)
  3789. #define DUK_HEAP_STRING_LOG2E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOG2E)
  3790. #define DUK_HTHREAD_STRING_LOG2E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOG2E)
  3791. #define DUK_HEAP_STRING_LN2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LN2)
  3792. #define DUK_HTHREAD_STRING_LN2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LN2)
  3793. #define DUK_HEAP_STRING_LN10(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LN10)
  3794. #define DUK_HTHREAD_STRING_LN10(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LN10)
  3795. #define DUK_HEAP_STRING_E(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_E)
  3796. #define DUK_HTHREAD_STRING_E(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_E)
  3797. #define DUK_HEAP_STRING_MESSAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MESSAGE)
  3798. #define DUK_HTHREAD_STRING_MESSAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MESSAGE)
  3799. #define DUK_HEAP_STRING_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAME)
  3800. #define DUK_HTHREAD_STRING_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAME)
  3801. #define DUK_HEAP_STRING_INPUT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INPUT)
  3802. #define DUK_HTHREAD_STRING_INPUT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INPUT)
  3803. #define DUK_HEAP_STRING_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INDEX)
  3804. #define DUK_HTHREAD_STRING_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INDEX)
  3805. #define DUK_HEAP_STRING_ESCAPED_EMPTY_REGEXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ESCAPED_EMPTY_REGEXP)
  3806. #define DUK_HTHREAD_STRING_ESCAPED_EMPTY_REGEXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ESCAPED_EMPTY_REGEXP)
  3807. #define DUK_HEAP_STRING_LAST_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LAST_INDEX)
  3808. #define DUK_HTHREAD_STRING_LAST_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LAST_INDEX)
  3809. #define DUK_HEAP_STRING_MULTILINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MULTILINE)
  3810. #define DUK_HTHREAD_STRING_MULTILINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MULTILINE)
  3811. #define DUK_HEAP_STRING_IGNORE_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IGNORE_CASE)
  3812. #define DUK_HTHREAD_STRING_IGNORE_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IGNORE_CASE)
  3813. #define DUK_HEAP_STRING_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SOURCE)
  3814. #define DUK_HTHREAD_STRING_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SOURCE)
  3815. #define DUK_HEAP_STRING_TEST(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TEST)
  3816. #define DUK_HTHREAD_STRING_TEST(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TEST)
  3817. #define DUK_HEAP_STRING_EXEC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXEC)
  3818. #define DUK_HTHREAD_STRING_EXEC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXEC)
  3819. #define DUK_HEAP_STRING_TO_GMT_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_GMT_STRING)
  3820. #define DUK_HTHREAD_STRING_TO_GMT_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_GMT_STRING)
  3821. #define DUK_HEAP_STRING_SET_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_YEAR)
  3822. #define DUK_HTHREAD_STRING_SET_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_YEAR)
  3823. #define DUK_HEAP_STRING_GET_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_YEAR)
  3824. #define DUK_HTHREAD_STRING_GET_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_YEAR)
  3825. #define DUK_HEAP_STRING_TO_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_JSON)
  3826. #define DUK_HTHREAD_STRING_TO_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_JSON)
  3827. #define DUK_HEAP_STRING_TO_ISO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_ISO_STRING)
  3828. #define DUK_HTHREAD_STRING_TO_ISO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_ISO_STRING)
  3829. #define DUK_HEAP_STRING_TO_UTC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_UTC_STRING)
  3830. #define DUK_HTHREAD_STRING_TO_UTC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_UTC_STRING)
  3831. #define DUK_HEAP_STRING_SET_UTC_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_FULL_YEAR)
  3832. #define DUK_HTHREAD_STRING_SET_UTC_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_FULL_YEAR)
  3833. #define DUK_HEAP_STRING_SET_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_FULL_YEAR)
  3834. #define DUK_HTHREAD_STRING_SET_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_FULL_YEAR)
  3835. #define DUK_HEAP_STRING_SET_UTC_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MONTH)
  3836. #define DUK_HTHREAD_STRING_SET_UTC_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MONTH)
  3837. #define DUK_HEAP_STRING_SET_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MONTH)
  3838. #define DUK_HTHREAD_STRING_SET_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MONTH)
  3839. #define DUK_HEAP_STRING_SET_UTC_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_DATE)
  3840. #define DUK_HTHREAD_STRING_SET_UTC_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_DATE)
  3841. #define DUK_HEAP_STRING_SET_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_DATE)
  3842. #define DUK_HTHREAD_STRING_SET_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_DATE)
  3843. #define DUK_HEAP_STRING_SET_UTC_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_HOURS)
  3844. #define DUK_HTHREAD_STRING_SET_UTC_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_HOURS)
  3845. #define DUK_HEAP_STRING_SET_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_HOURS)
  3846. #define DUK_HTHREAD_STRING_SET_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_HOURS)
  3847. #define DUK_HEAP_STRING_SET_UTC_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MINUTES)
  3848. #define DUK_HTHREAD_STRING_SET_UTC_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MINUTES)
  3849. #define DUK_HEAP_STRING_SET_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MINUTES)
  3850. #define DUK_HTHREAD_STRING_SET_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MINUTES)
  3851. #define DUK_HEAP_STRING_SET_UTC_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_SECONDS)
  3852. #define DUK_HTHREAD_STRING_SET_UTC_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_SECONDS)
  3853. #define DUK_HEAP_STRING_SET_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_SECONDS)
  3854. #define DUK_HTHREAD_STRING_SET_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_SECONDS)
  3855. #define DUK_HEAP_STRING_SET_UTC_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_UTC_MILLISECONDS)
  3856. #define DUK_HTHREAD_STRING_SET_UTC_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_UTC_MILLISECONDS)
  3857. #define DUK_HEAP_STRING_SET_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_MILLISECONDS)
  3858. #define DUK_HTHREAD_STRING_SET_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_MILLISECONDS)
  3859. #define DUK_HEAP_STRING_SET_TIME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_TIME)
  3860. #define DUK_HTHREAD_STRING_SET_TIME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_TIME)
  3861. #define DUK_HEAP_STRING_GET_TIMEZONE_OFFSET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_TIMEZONE_OFFSET)
  3862. #define DUK_HTHREAD_STRING_GET_TIMEZONE_OFFSET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_TIMEZONE_OFFSET)
  3863. #define DUK_HEAP_STRING_GET_UTC_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MILLISECONDS)
  3864. #define DUK_HTHREAD_STRING_GET_UTC_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MILLISECONDS)
  3865. #define DUK_HEAP_STRING_GET_MILLISECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MILLISECONDS)
  3866. #define DUK_HTHREAD_STRING_GET_MILLISECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MILLISECONDS)
  3867. #define DUK_HEAP_STRING_GET_UTC_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_SECONDS)
  3868. #define DUK_HTHREAD_STRING_GET_UTC_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_SECONDS)
  3869. #define DUK_HEAP_STRING_GET_SECONDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_SECONDS)
  3870. #define DUK_HTHREAD_STRING_GET_SECONDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_SECONDS)
  3871. #define DUK_HEAP_STRING_GET_UTC_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MINUTES)
  3872. #define DUK_HTHREAD_STRING_GET_UTC_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MINUTES)
  3873. #define DUK_HEAP_STRING_GET_MINUTES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MINUTES)
  3874. #define DUK_HTHREAD_STRING_GET_MINUTES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MINUTES)
  3875. #define DUK_HEAP_STRING_GET_UTC_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_HOURS)
  3876. #define DUK_HTHREAD_STRING_GET_UTC_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_HOURS)
  3877. #define DUK_HEAP_STRING_GET_HOURS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_HOURS)
  3878. #define DUK_HTHREAD_STRING_GET_HOURS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_HOURS)
  3879. #define DUK_HEAP_STRING_GET_UTC_DAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_DAY)
  3880. #define DUK_HTHREAD_STRING_GET_UTC_DAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_DAY)
  3881. #define DUK_HEAP_STRING_GET_DAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_DAY)
  3882. #define DUK_HTHREAD_STRING_GET_DAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_DAY)
  3883. #define DUK_HEAP_STRING_GET_UTC_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_DATE)
  3884. #define DUK_HTHREAD_STRING_GET_UTC_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_DATE)
  3885. #define DUK_HEAP_STRING_GET_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_DATE)
  3886. #define DUK_HTHREAD_STRING_GET_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_DATE)
  3887. #define DUK_HEAP_STRING_GET_UTC_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_MONTH)
  3888. #define DUK_HTHREAD_STRING_GET_UTC_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_MONTH)
  3889. #define DUK_HEAP_STRING_GET_MONTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_MONTH)
  3890. #define DUK_HTHREAD_STRING_GET_MONTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_MONTH)
  3891. #define DUK_HEAP_STRING_GET_UTC_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_UTC_FULL_YEAR)
  3892. #define DUK_HTHREAD_STRING_GET_UTC_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_UTC_FULL_YEAR)
  3893. #define DUK_HEAP_STRING_GET_FULL_YEAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_FULL_YEAR)
  3894. #define DUK_HTHREAD_STRING_GET_FULL_YEAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_FULL_YEAR)
  3895. #define DUK_HEAP_STRING_GET_TIME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_TIME)
  3896. #define DUK_HTHREAD_STRING_GET_TIME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_TIME)
  3897. #define DUK_HEAP_STRING_TO_LOCALE_TIME_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_TIME_STRING)
  3898. #define DUK_HTHREAD_STRING_TO_LOCALE_TIME_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_TIME_STRING)
  3899. #define DUK_HEAP_STRING_TO_LOCALE_DATE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_DATE_STRING)
  3900. #define DUK_HTHREAD_STRING_TO_LOCALE_DATE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_DATE_STRING)
  3901. #define DUK_HEAP_STRING_TO_TIME_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_TIME_STRING)
  3902. #define DUK_HTHREAD_STRING_TO_TIME_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_TIME_STRING)
  3903. #define DUK_HEAP_STRING_TO_DATE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_DATE_STRING)
  3904. #define DUK_HTHREAD_STRING_TO_DATE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_DATE_STRING)
  3905. #define DUK_HEAP_STRING_NOW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NOW)
  3906. #define DUK_HTHREAD_STRING_NOW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NOW)
  3907. #define DUK_HEAP_STRING_UTC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UTC)
  3908. #define DUK_HTHREAD_STRING_UTC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UTC)
  3909. #define DUK_HEAP_STRING_PARSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE)
  3910. #define DUK_HTHREAD_STRING_PARSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE)
  3911. #define DUK_HEAP_STRING_TO_PRECISION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_PRECISION)
  3912. #define DUK_HTHREAD_STRING_TO_PRECISION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_PRECISION)
  3913. #define DUK_HEAP_STRING_TO_EXPONENTIAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_EXPONENTIAL)
  3914. #define DUK_HTHREAD_STRING_TO_EXPONENTIAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_EXPONENTIAL)
  3915. #define DUK_HEAP_STRING_TO_FIXED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_FIXED)
  3916. #define DUK_HTHREAD_STRING_TO_FIXED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_FIXED)
  3917. #define DUK_HEAP_STRING_POSITIVE_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POSITIVE_INFINITY)
  3918. #define DUK_HTHREAD_STRING_POSITIVE_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POSITIVE_INFINITY)
  3919. #define DUK_HEAP_STRING_NEGATIVE_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEGATIVE_INFINITY)
  3920. #define DUK_HTHREAD_STRING_NEGATIVE_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEGATIVE_INFINITY)
  3921. #define DUK_HEAP_STRING_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAN)
  3922. #define DUK_HTHREAD_STRING_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAN)
  3923. #define DUK_HEAP_STRING_MIN_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MIN_VALUE)
  3924. #define DUK_HTHREAD_STRING_MIN_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MIN_VALUE)
  3925. #define DUK_HEAP_STRING_MAX_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAX_VALUE)
  3926. #define DUK_HTHREAD_STRING_MAX_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAX_VALUE)
  3927. #define DUK_HEAP_STRING_SUBSTR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBSTR)
  3928. #define DUK_HTHREAD_STRING_SUBSTR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBSTR)
  3929. #define DUK_HEAP_STRING_TRIM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRIM)
  3930. #define DUK_HTHREAD_STRING_TRIM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRIM)
  3931. #define DUK_HEAP_STRING_TO_LOCALE_UPPER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_UPPER_CASE)
  3932. #define DUK_HTHREAD_STRING_TO_LOCALE_UPPER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_UPPER_CASE)
  3933. #define DUK_HEAP_STRING_TO_UPPER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_UPPER_CASE)
  3934. #define DUK_HTHREAD_STRING_TO_UPPER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_UPPER_CASE)
  3935. #define DUK_HEAP_STRING_TO_LOCALE_LOWER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_LOWER_CASE)
  3936. #define DUK_HTHREAD_STRING_TO_LOCALE_LOWER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_LOWER_CASE)
  3937. #define DUK_HEAP_STRING_TO_LOWER_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOWER_CASE)
  3938. #define DUK_HTHREAD_STRING_TO_LOWER_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOWER_CASE)
  3939. #define DUK_HEAP_STRING_SUBSTRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUBSTRING)
  3940. #define DUK_HTHREAD_STRING_SUBSTRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUBSTRING)
  3941. #define DUK_HEAP_STRING_SPLIT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPLIT)
  3942. #define DUK_HTHREAD_STRING_SPLIT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPLIT)
  3943. #define DUK_HEAP_STRING_SEARCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SEARCH)
  3944. #define DUK_HTHREAD_STRING_SEARCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SEARCH)
  3945. #define DUK_HEAP_STRING_REPLACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REPLACE)
  3946. #define DUK_HTHREAD_STRING_REPLACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REPLACE)
  3947. #define DUK_HEAP_STRING_MATCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MATCH)
  3948. #define DUK_HTHREAD_STRING_MATCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MATCH)
  3949. #define DUK_HEAP_STRING_LOCALE_COMPARE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LOCALE_COMPARE)
  3950. #define DUK_HTHREAD_STRING_LOCALE_COMPARE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LOCALE_COMPARE)
  3951. #define DUK_HEAP_STRING_CHAR_CODE_AT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CHAR_CODE_AT)
  3952. #define DUK_HTHREAD_STRING_CHAR_CODE_AT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CHAR_CODE_AT)
  3953. #define DUK_HEAP_STRING_CHAR_AT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CHAR_AT)
  3954. #define DUK_HTHREAD_STRING_CHAR_AT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CHAR_AT)
  3955. #define DUK_HEAP_STRING_FROM_CHAR_CODE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FROM_CHAR_CODE)
  3956. #define DUK_HTHREAD_STRING_FROM_CHAR_CODE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FROM_CHAR_CODE)
  3957. #define DUK_HEAP_STRING_REDUCE_RIGHT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REDUCE_RIGHT)
  3958. #define DUK_HTHREAD_STRING_REDUCE_RIGHT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REDUCE_RIGHT)
  3959. #define DUK_HEAP_STRING_REDUCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REDUCE)
  3960. #define DUK_HTHREAD_STRING_REDUCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REDUCE)
  3961. #define DUK_HEAP_STRING_FILTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILTER)
  3962. #define DUK_HTHREAD_STRING_FILTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILTER)
  3963. #define DUK_HEAP_STRING_MAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MAP)
  3964. #define DUK_HTHREAD_STRING_MAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MAP)
  3965. #define DUK_HEAP_STRING_FOR_EACH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FOR_EACH)
  3966. #define DUK_HTHREAD_STRING_FOR_EACH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FOR_EACH)
  3967. #define DUK_HEAP_STRING_SOME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SOME)
  3968. #define DUK_HTHREAD_STRING_SOME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SOME)
  3969. #define DUK_HEAP_STRING_EVERY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVERY)
  3970. #define DUK_HTHREAD_STRING_EVERY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVERY)
  3971. #define DUK_HEAP_STRING_LAST_INDEX_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LAST_INDEX_OF)
  3972. #define DUK_HTHREAD_STRING_LAST_INDEX_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LAST_INDEX_OF)
  3973. #define DUK_HEAP_STRING_INDEX_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INDEX_OF)
  3974. #define DUK_HTHREAD_STRING_INDEX_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INDEX_OF)
  3975. #define DUK_HEAP_STRING_UNSHIFT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UNSHIFT)
  3976. #define DUK_HTHREAD_STRING_UNSHIFT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UNSHIFT)
  3977. #define DUK_HEAP_STRING_SPLICE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPLICE)
  3978. #define DUK_HTHREAD_STRING_SPLICE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPLICE)
  3979. #define DUK_HEAP_STRING_SORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SORT)
  3980. #define DUK_HTHREAD_STRING_SORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SORT)
  3981. #define DUK_HEAP_STRING_SLICE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SLICE)
  3982. #define DUK_HTHREAD_STRING_SLICE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SLICE)
  3983. #define DUK_HEAP_STRING_SHIFT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SHIFT)
  3984. #define DUK_HTHREAD_STRING_SHIFT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SHIFT)
  3985. #define DUK_HEAP_STRING_REVERSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REVERSE)
  3986. #define DUK_HTHREAD_STRING_REVERSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REVERSE)
  3987. #define DUK_HEAP_STRING_PUSH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PUSH)
  3988. #define DUK_HTHREAD_STRING_PUSH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PUSH)
  3989. #define DUK_HEAP_STRING_POP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_POP)
  3990. #define DUK_HTHREAD_STRING_POP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_POP)
  3991. #define DUK_HEAP_STRING_JOIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JOIN)
  3992. #define DUK_HTHREAD_STRING_JOIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JOIN)
  3993. #define DUK_HEAP_STRING_CONCAT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONCAT)
  3994. #define DUK_HTHREAD_STRING_CONCAT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONCAT)
  3995. #define DUK_HEAP_STRING_IS_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_ARRAY)
  3996. #define DUK_HTHREAD_STRING_IS_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_ARRAY)
  3997. #define DUK_HEAP_STRING_LC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ARGUMENTS)
  3998. #define DUK_HTHREAD_STRING_LC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ARGUMENTS)
  3999. #define DUK_HEAP_STRING_CALLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLER)
  4000. #define DUK_HTHREAD_STRING_CALLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLER)
  4001. #define DUK_HEAP_STRING_BIND(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BIND)
  4002. #define DUK_HTHREAD_STRING_BIND(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BIND)
  4003. #define DUK_HEAP_STRING_CALL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALL)
  4004. #define DUK_HTHREAD_STRING_CALL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALL)
  4005. #define DUK_HEAP_STRING_APPLY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_APPLY)
  4006. #define DUK_HTHREAD_STRING_APPLY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_APPLY)
  4007. #define DUK_HEAP_STRING_PROPERTY_IS_ENUMERABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROPERTY_IS_ENUMERABLE)
  4008. #define DUK_HTHREAD_STRING_PROPERTY_IS_ENUMERABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROPERTY_IS_ENUMERABLE)
  4009. #define DUK_HEAP_STRING_IS_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_PROTOTYPE_OF)
  4010. #define DUK_HTHREAD_STRING_IS_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_PROTOTYPE_OF)
  4011. #define DUK_HEAP_STRING_HAS_OWN_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HAS_OWN_PROPERTY)
  4012. #define DUK_HTHREAD_STRING_HAS_OWN_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HAS_OWN_PROPERTY)
  4013. #define DUK_HEAP_STRING_VALUE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE_OF)
  4014. #define DUK_HTHREAD_STRING_VALUE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE_OF)
  4015. #define DUK_HEAP_STRING_TO_LOCALE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_STRING)
  4016. #define DUK_HTHREAD_STRING_TO_LOCALE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_STRING)
  4017. #define DUK_HEAP_STRING_TO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_STRING)
  4018. #define DUK_HTHREAD_STRING_TO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_STRING)
  4019. #define DUK_HEAP_STRING_CONSTRUCTOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONSTRUCTOR)
  4020. #define DUK_HTHREAD_STRING_CONSTRUCTOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONSTRUCTOR)
  4021. #define DUK_HEAP_STRING_SET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET)
  4022. #define DUK_HTHREAD_STRING_SET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET)
  4023. #define DUK_HEAP_STRING_GET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET)
  4024. #define DUK_HTHREAD_STRING_GET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET)
  4025. #define DUK_HEAP_STRING_ENUMERABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERABLE)
  4026. #define DUK_HTHREAD_STRING_ENUMERABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERABLE)
  4027. #define DUK_HEAP_STRING_CONFIGURABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONFIGURABLE)
  4028. #define DUK_HTHREAD_STRING_CONFIGURABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONFIGURABLE)
  4029. #define DUK_HEAP_STRING_WRITABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITABLE)
  4030. #define DUK_HTHREAD_STRING_WRITABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITABLE)
  4031. #define DUK_HEAP_STRING_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE)
  4032. #define DUK_HTHREAD_STRING_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE)
  4033. #define DUK_HEAP_STRING_KEYS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_KEYS)
  4034. #define DUK_HTHREAD_STRING_KEYS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_KEYS)
  4035. #define DUK_HEAP_STRING_IS_EXTENSIBLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_EXTENSIBLE)
  4036. #define DUK_HTHREAD_STRING_IS_EXTENSIBLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_EXTENSIBLE)
  4037. #define DUK_HEAP_STRING_IS_FROZEN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_FROZEN)
  4038. #define DUK_HTHREAD_STRING_IS_FROZEN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_FROZEN)
  4039. #define DUK_HEAP_STRING_IS_SEALED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_SEALED)
  4040. #define DUK_HTHREAD_STRING_IS_SEALED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_SEALED)
  4041. #define DUK_HEAP_STRING_PREVENT_EXTENSIONS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PREVENT_EXTENSIONS)
  4042. #define DUK_HTHREAD_STRING_PREVENT_EXTENSIONS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PREVENT_EXTENSIONS)
  4043. #define DUK_HEAP_STRING_FREEZE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FREEZE)
  4044. #define DUK_HTHREAD_STRING_FREEZE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FREEZE)
  4045. #define DUK_HEAP_STRING_SEAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SEAL)
  4046. #define DUK_HTHREAD_STRING_SEAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SEAL)
  4047. #define DUK_HEAP_STRING_DEFINE_PROPERTIES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFINE_PROPERTIES)
  4048. #define DUK_HTHREAD_STRING_DEFINE_PROPERTIES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFINE_PROPERTIES)
  4049. #define DUK_HEAP_STRING_DEFINE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFINE_PROPERTY)
  4050. #define DUK_HTHREAD_STRING_DEFINE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFINE_PROPERTY)
  4051. #define DUK_HEAP_STRING_CREATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CREATE)
  4052. #define DUK_HTHREAD_STRING_CREATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CREATE)
  4053. #define DUK_HEAP_STRING_GET_OWN_PROPERTY_NAMES(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_OWN_PROPERTY_NAMES)
  4054. #define DUK_HTHREAD_STRING_GET_OWN_PROPERTY_NAMES(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_OWN_PROPERTY_NAMES)
  4055. #define DUK_HEAP_STRING_GET_OWN_PROPERTY_DESCRIPTOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR)
  4056. #define DUK_HTHREAD_STRING_GET_OWN_PROPERTY_DESCRIPTOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_OWN_PROPERTY_DESCRIPTOR)
  4057. #define DUK_HEAP_STRING_GET_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET_PROTOTYPE_OF)
  4058. #define DUK_HTHREAD_STRING_GET_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET_PROTOTYPE_OF)
  4059. #define DUK_HEAP_STRING_PROTOTYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTOTYPE)
  4060. #define DUK_HTHREAD_STRING_PROTOTYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTOTYPE)
  4061. #define DUK_HEAP_STRING_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LENGTH)
  4062. #define DUK_HTHREAD_STRING_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LENGTH)
  4063. #define DUK_HEAP_STRING_ALERT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ALERT)
  4064. #define DUK_HTHREAD_STRING_ALERT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ALERT)
  4065. #define DUK_HEAP_STRING_PRINT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PRINT)
  4066. #define DUK_HTHREAD_STRING_PRINT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PRINT)
  4067. #define DUK_HEAP_STRING_UNESCAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UNESCAPE)
  4068. #define DUK_HTHREAD_STRING_UNESCAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UNESCAPE)
  4069. #define DUK_HEAP_STRING_ESCAPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ESCAPE)
  4070. #define DUK_HTHREAD_STRING_ESCAPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ESCAPE)
  4071. #define DUK_HEAP_STRING_ENCODE_URI_COMPONENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENCODE_URI_COMPONENT)
  4072. #define DUK_HTHREAD_STRING_ENCODE_URI_COMPONENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENCODE_URI_COMPONENT)
  4073. #define DUK_HEAP_STRING_ENCODE_URI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENCODE_URI)
  4074. #define DUK_HTHREAD_STRING_ENCODE_URI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENCODE_URI)
  4075. #define DUK_HEAP_STRING_DECODE_URI_COMPONENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DECODE_URI_COMPONENT)
  4076. #define DUK_HTHREAD_STRING_DECODE_URI_COMPONENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DECODE_URI_COMPONENT)
  4077. #define DUK_HEAP_STRING_DECODE_URI(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DECODE_URI)
  4078. #define DUK_HTHREAD_STRING_DECODE_URI(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DECODE_URI)
  4079. #define DUK_HEAP_STRING_IS_FINITE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_FINITE)
  4080. #define DUK_HTHREAD_STRING_IS_FINITE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_FINITE)
  4081. #define DUK_HEAP_STRING_IS_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IS_NAN)
  4082. #define DUK_HTHREAD_STRING_IS_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IS_NAN)
  4083. #define DUK_HEAP_STRING_PARSE_FLOAT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE_FLOAT)
  4084. #define DUK_HTHREAD_STRING_PARSE_FLOAT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE_FLOAT)
  4085. #define DUK_HEAP_STRING_PARSE_INT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PARSE_INT)
  4086. #define DUK_HTHREAD_STRING_PARSE_INT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PARSE_INT)
  4087. #define DUK_HEAP_STRING_EVAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVAL)
  4088. #define DUK_HTHREAD_STRING_EVAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVAL)
  4089. #define DUK_HEAP_STRING_URI_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_URI_ERROR)
  4090. #define DUK_HTHREAD_STRING_URI_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_URI_ERROR)
  4091. #define DUK_HEAP_STRING_TYPE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPE_ERROR)
  4092. #define DUK_HTHREAD_STRING_TYPE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPE_ERROR)
  4093. #define DUK_HEAP_STRING_SYNTAX_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SYNTAX_ERROR)
  4094. #define DUK_HTHREAD_STRING_SYNTAX_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SYNTAX_ERROR)
  4095. #define DUK_HEAP_STRING_REFERENCE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REFERENCE_ERROR)
  4096. #define DUK_HTHREAD_STRING_REFERENCE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REFERENCE_ERROR)
  4097. #define DUK_HEAP_STRING_RANGE_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RANGE_ERROR)
  4098. #define DUK_HTHREAD_STRING_RANGE_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RANGE_ERROR)
  4099. #define DUK_HEAP_STRING_EVAL_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVAL_ERROR)
  4100. #define DUK_HTHREAD_STRING_EVAL_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVAL_ERROR)
  4101. #define DUK_HEAP_STRING_BREAK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BREAK)
  4102. #define DUK_HTHREAD_STRING_BREAK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BREAK)
  4103. #define DUK_HEAP_STRING_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CASE)
  4104. #define DUK_HTHREAD_STRING_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CASE)
  4105. #define DUK_HEAP_STRING_CATCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CATCH)
  4106. #define DUK_HTHREAD_STRING_CATCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CATCH)
  4107. #define DUK_HEAP_STRING_CONTINUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONTINUE)
  4108. #define DUK_HTHREAD_STRING_CONTINUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONTINUE)
  4109. #define DUK_HEAP_STRING_DEBUGGER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEBUGGER)
  4110. #define DUK_HTHREAD_STRING_DEBUGGER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEBUGGER)
  4111. #define DUK_HEAP_STRING_DEFAULT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFAULT)
  4112. #define DUK_HTHREAD_STRING_DEFAULT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFAULT)
  4113. #define DUK_HEAP_STRING_DELETE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE)
  4114. #define DUK_HTHREAD_STRING_DELETE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE)
  4115. #define DUK_HEAP_STRING_DO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DO)
  4116. #define DUK_HTHREAD_STRING_DO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DO)
  4117. #define DUK_HEAP_STRING_ELSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ELSE)
  4118. #define DUK_HTHREAD_STRING_ELSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ELSE)
  4119. #define DUK_HEAP_STRING_FINALLY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FINALLY)
  4120. #define DUK_HTHREAD_STRING_FINALLY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FINALLY)
  4121. #define DUK_HEAP_STRING_FOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FOR)
  4122. #define DUK_HTHREAD_STRING_FOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FOR)
  4123. #define DUK_HEAP_STRING_LC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FUNCTION)
  4124. #define DUK_HTHREAD_STRING_LC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FUNCTION)
  4125. #define DUK_HEAP_STRING_IF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IF)
  4126. #define DUK_HTHREAD_STRING_IF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IF)
  4127. #define DUK_HEAP_STRING_IN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IN)
  4128. #define DUK_HTHREAD_STRING_IN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IN)
  4129. #define DUK_HEAP_STRING_INSTANCEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INSTANCEOF)
  4130. #define DUK_HTHREAD_STRING_INSTANCEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INSTANCEOF)
  4131. #define DUK_HEAP_STRING_NEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEW)
  4132. #define DUK_HTHREAD_STRING_NEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEW)
  4133. #define DUK_HEAP_STRING_RETURN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RETURN)
  4134. #define DUK_HTHREAD_STRING_RETURN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RETURN)
  4135. #define DUK_HEAP_STRING_SWITCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SWITCH)
  4136. #define DUK_HTHREAD_STRING_SWITCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SWITCH)
  4137. #define DUK_HEAP_STRING_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THIS)
  4138. #define DUK_HTHREAD_STRING_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THIS)
  4139. #define DUK_HEAP_STRING_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THROW)
  4140. #define DUK_HTHREAD_STRING_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THROW)
  4141. #define DUK_HEAP_STRING_TRY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRY)
  4142. #define DUK_HTHREAD_STRING_TRY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRY)
  4143. #define DUK_HEAP_STRING_TYPEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPEOF)
  4144. #define DUK_HTHREAD_STRING_TYPEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPEOF)
  4145. #define DUK_HEAP_STRING_VAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VAR)
  4146. #define DUK_HTHREAD_STRING_VAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VAR)
  4147. #define DUK_HEAP_STRING_VOID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VOID)
  4148. #define DUK_HTHREAD_STRING_VOID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VOID)
  4149. #define DUK_HEAP_STRING_WHILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WHILE)
  4150. #define DUK_HTHREAD_STRING_WHILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WHILE)
  4151. #define DUK_HEAP_STRING_WITH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WITH)
  4152. #define DUK_HTHREAD_STRING_WITH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WITH)
  4153. #define DUK_HEAP_STRING_CLASS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLASS)
  4154. #define DUK_HTHREAD_STRING_CLASS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLASS)
  4155. #define DUK_HEAP_STRING_CONST(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONST)
  4156. #define DUK_HTHREAD_STRING_CONST(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONST)
  4157. #define DUK_HEAP_STRING_ENUM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUM)
  4158. #define DUK_HTHREAD_STRING_ENUM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUM)
  4159. #define DUK_HEAP_STRING_EXPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORT)
  4160. #define DUK_HTHREAD_STRING_EXPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORT)
  4161. #define DUK_HEAP_STRING_EXTENDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXTENDS)
  4162. #define DUK_HTHREAD_STRING_EXTENDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXTENDS)
  4163. #define DUK_HEAP_STRING_IMPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPORT)
  4164. #define DUK_HTHREAD_STRING_IMPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPORT)
  4165. #define DUK_HEAP_STRING_SUPER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUPER)
  4166. #define DUK_HTHREAD_STRING_SUPER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUPER)
  4167. #define DUK_HEAP_STRING_LC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NULL)
  4168. #define DUK_HTHREAD_STRING_LC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NULL)
  4169. #define DUK_HEAP_STRING_TRUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRUE)
  4170. #define DUK_HTHREAD_STRING_TRUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRUE)
  4171. #define DUK_HEAP_STRING_FALSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FALSE)
  4172. #define DUK_HTHREAD_STRING_FALSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FALSE)
  4173. #define DUK_HEAP_STRING_IMPLEMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPLEMENTS)
  4174. #define DUK_HTHREAD_STRING_IMPLEMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPLEMENTS)
  4175. #define DUK_HEAP_STRING_INTERFACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INTERFACE)
  4176. #define DUK_HTHREAD_STRING_INTERFACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INTERFACE)
  4177. #define DUK_HEAP_STRING_LET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LET)
  4178. #define DUK_HTHREAD_STRING_LET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LET)
  4179. #define DUK_HEAP_STRING_PACKAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PACKAGE)
  4180. #define DUK_HTHREAD_STRING_PACKAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PACKAGE)
  4181. #define DUK_HEAP_STRING_PRIVATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PRIVATE)
  4182. #define DUK_HTHREAD_STRING_PRIVATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PRIVATE)
  4183. #define DUK_HEAP_STRING_PROTECTED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTECTED)
  4184. #define DUK_HTHREAD_STRING_PROTECTED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTECTED)
  4185. #define DUK_HEAP_STRING_PUBLIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PUBLIC)
  4186. #define DUK_HTHREAD_STRING_PUBLIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PUBLIC)
  4187. #define DUK_HEAP_STRING_STATIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STATIC)
  4188. #define DUK_HTHREAD_STRING_STATIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STATIC)
  4189. #define DUK_HEAP_STRING_YIELD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_YIELD)
  4190. #define DUK_HTHREAD_STRING_YIELD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_YIELD)
  4191. #define DUK_HEAP_NUM_STRINGS 414
  4192. #define DUK_STRIDX_START_RESERVED 369
  4193. #define DUK_STRIDX_START_STRICT_RESERVED 405
  4194. #define DUK_STRIDX_END_RESERVED 414 /* exclusive endpoint */
  4195. #if !defined(DUK_SINGLE_FILE)
  4196. DUK_INTERNAL_DECL const duk_c_function duk_bi_native_functions[147];
  4197. DUK_INTERNAL_DECL const duk_uint8_t duk_builtins_data[1952];
  4198. #ifdef DUK_USE_BUILTIN_INITJS
  4199. DUK_INTERNAL_DECL const duk_uint8_t duk_initjs_data[187];
  4200. #endif /* DUK_USE_BUILTIN_INITJS */
  4201. #endif /* !DUK_SINGLE_FILE */
  4202. #define DUK_BUILTINS_DATA_LENGTH 1952
  4203. #ifdef DUK_USE_BUILTIN_INITJS
  4204. #define DUK_BUILTIN_INITJS_DATA_LENGTH 187
  4205. #endif /* DUK_USE_BUILTIN_INITJS */
  4206. #define DUK_BIDX_GLOBAL 0
  4207. #define DUK_BIDX_GLOBAL_ENV 1
  4208. #define DUK_BIDX_OBJECT_CONSTRUCTOR 2
  4209. #define DUK_BIDX_OBJECT_PROTOTYPE 3
  4210. #define DUK_BIDX_FUNCTION_CONSTRUCTOR 4
  4211. #define DUK_BIDX_FUNCTION_PROTOTYPE 5
  4212. #define DUK_BIDX_ARRAY_CONSTRUCTOR 6
  4213. #define DUK_BIDX_ARRAY_PROTOTYPE 7
  4214. #define DUK_BIDX_STRING_CONSTRUCTOR 8
  4215. #define DUK_BIDX_STRING_PROTOTYPE 9
  4216. #define DUK_BIDX_BOOLEAN_CONSTRUCTOR 10
  4217. #define DUK_BIDX_BOOLEAN_PROTOTYPE 11
  4218. #define DUK_BIDX_NUMBER_CONSTRUCTOR 12
  4219. #define DUK_BIDX_NUMBER_PROTOTYPE 13
  4220. #define DUK_BIDX_DATE_CONSTRUCTOR 14
  4221. #define DUK_BIDX_DATE_PROTOTYPE 15
  4222. #define DUK_BIDX_REGEXP_CONSTRUCTOR 16
  4223. #define DUK_BIDX_REGEXP_PROTOTYPE 17
  4224. #define DUK_BIDX_ERROR_CONSTRUCTOR 18
  4225. #define DUK_BIDX_ERROR_PROTOTYPE 19
  4226. #define DUK_BIDX_EVAL_ERROR_CONSTRUCTOR 20
  4227. #define DUK_BIDX_EVAL_ERROR_PROTOTYPE 21
  4228. #define DUK_BIDX_RANGE_ERROR_CONSTRUCTOR 22
  4229. #define DUK_BIDX_RANGE_ERROR_PROTOTYPE 23
  4230. #define DUK_BIDX_REFERENCE_ERROR_CONSTRUCTOR 24
  4231. #define DUK_BIDX_REFERENCE_ERROR_PROTOTYPE 25
  4232. #define DUK_BIDX_SYNTAX_ERROR_CONSTRUCTOR 26
  4233. #define DUK_BIDX_SYNTAX_ERROR_PROTOTYPE 27
  4234. #define DUK_BIDX_TYPE_ERROR_CONSTRUCTOR 28
  4235. #define DUK_BIDX_TYPE_ERROR_PROTOTYPE 29
  4236. #define DUK_BIDX_URI_ERROR_CONSTRUCTOR 30
  4237. #define DUK_BIDX_URI_ERROR_PROTOTYPE 31
  4238. #define DUK_BIDX_MATH 32
  4239. #define DUK_BIDX_JSON 33
  4240. #define DUK_BIDX_TYPE_ERROR_THROWER 34
  4241. #define DUK_BIDX_PROXY_CONSTRUCTOR 35
  4242. #define DUK_BIDX_DUKTAPE 36
  4243. #define DUK_BIDX_THREAD_CONSTRUCTOR 37
  4244. #define DUK_BIDX_THREAD_PROTOTYPE 38
  4245. #define DUK_BIDX_BUFFER_CONSTRUCTOR 39
  4246. #define DUK_BIDX_BUFFER_PROTOTYPE 40
  4247. #define DUK_BIDX_POINTER_CONSTRUCTOR 41
  4248. #define DUK_BIDX_POINTER_PROTOTYPE 42
  4249. #define DUK_BIDX_LOGGER_CONSTRUCTOR 43
  4250. #define DUK_BIDX_LOGGER_PROTOTYPE 44
  4251. #define DUK_BIDX_DOUBLE_ERROR 45
  4252. #define DUK_BIDX_ARRAYBUFFER_CONSTRUCTOR 46
  4253. #define DUK_BIDX_ARRAYBUFFER_PROTOTYPE 47
  4254. #define DUK_BIDX_DATAVIEW_CONSTRUCTOR 48
  4255. #define DUK_BIDX_DATAVIEW_PROTOTYPE 49
  4256. #define DUK_BIDX_TYPEDARRAY_PROTOTYPE 50
  4257. #define DUK_BIDX_INT8ARRAY_CONSTRUCTOR 51
  4258. #define DUK_BIDX_INT8ARRAY_PROTOTYPE 52
  4259. #define DUK_BIDX_UINT8ARRAY_CONSTRUCTOR 53
  4260. #define DUK_BIDX_UINT8ARRAY_PROTOTYPE 54
  4261. #define DUK_BIDX_UINT8CLAMPEDARRAY_CONSTRUCTOR 55
  4262. #define DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE 56
  4263. #define DUK_BIDX_INT16ARRAY_CONSTRUCTOR 57
  4264. #define DUK_BIDX_INT16ARRAY_PROTOTYPE 58
  4265. #define DUK_BIDX_UINT16ARRAY_CONSTRUCTOR 59
  4266. #define DUK_BIDX_UINT16ARRAY_PROTOTYPE 60
  4267. #define DUK_BIDX_INT32ARRAY_CONSTRUCTOR 61
  4268. #define DUK_BIDX_INT32ARRAY_PROTOTYPE 62
  4269. #define DUK_BIDX_UINT32ARRAY_CONSTRUCTOR 63
  4270. #define DUK_BIDX_UINT32ARRAY_PROTOTYPE 64
  4271. #define DUK_BIDX_FLOAT32ARRAY_CONSTRUCTOR 65
  4272. #define DUK_BIDX_FLOAT32ARRAY_PROTOTYPE 66
  4273. #define DUK_BIDX_FLOAT64ARRAY_CONSTRUCTOR 67
  4274. #define DUK_BIDX_FLOAT64ARRAY_PROTOTYPE 68
  4275. #define DUK_BIDX_NODEJS_BUFFER_CONSTRUCTOR 69
  4276. #define DUK_BIDX_NODEJS_BUFFER_PROTOTYPE 70
  4277. #define DUK_NUM_BUILTINS 71
  4278. #else
  4279. #error invalid endianness defines
  4280. #endif
  4281. #endif /* DUK_BUILTINS_H_INCLUDED */
  4282. #line 50 "duk_internal.h"
  4283. #line 1 "duk_strings.h"
  4284. /*
  4285. * Shared error messages: declarations and macros
  4286. *
  4287. * Error messages are accessed through macros with fine-grained, explicit
  4288. * error message distinctions. Concrete error messages are selected by the
  4289. * macros and multiple macros can map to the same concrete string to save
  4290. * on code footprint. This allows flexible footprint/verbosity tuning with
  4291. * minimal code impact. There are a few limitations to this approach:
  4292. * (1) switching between plain messages and format strings doesn't work
  4293. * conveniently, and (2) conditional strings are a bit awkward to handle.
  4294. *
  4295. * Because format strings behave differently in the call site (they need to
  4296. * be followed by format arguments), they have a special prefix (DUK_STR_FMT_
  4297. * and duk_str_fmt_).
  4298. *
  4299. * On some compilers using explicit shared strings is preferable; on others
  4300. * it may be better to use straight literals because the compiler will combine
  4301. * them anyway, and such strings won't end up unnecessarily in a symbol table.
  4302. */
  4303. #ifndef DUK_ERRMSG_H_INCLUDED
  4304. #define DUK_ERRMSG_H_INCLUDED
  4305. #define DUK_STR_INTERNAL_ERROR duk_str_internal_error
  4306. #define DUK_STR_INVALID_COUNT duk_str_invalid_count
  4307. #define DUK_STR_INVALID_CALL_ARGS duk_str_invalid_call_args
  4308. #define DUK_STR_NOT_CONSTRUCTABLE duk_str_not_constructable
  4309. #define DUK_STR_NOT_CALLABLE duk_str_not_callable
  4310. #define DUK_STR_NOT_EXTENSIBLE duk_str_not_extensible
  4311. #define DUK_STR_NOT_WRITABLE duk_str_not_writable
  4312. #define DUK_STR_NOT_CONFIGURABLE duk_str_not_configurable
  4313. #if !defined(DUK_SINGLE_FILE)
  4314. DUK_INTERNAL_DECL const char *duk_str_internal_error;
  4315. DUK_INTERNAL_DECL const char *duk_str_invalid_count;
  4316. DUK_INTERNAL_DECL const char *duk_str_invalid_call_args;
  4317. DUK_INTERNAL_DECL const char *duk_str_not_constructable;
  4318. DUK_INTERNAL_DECL const char *duk_str_not_callable;
  4319. DUK_INTERNAL_DECL const char *duk_str_not_extensible;
  4320. DUK_INTERNAL_DECL const char *duk_str_not_writable;
  4321. DUK_INTERNAL_DECL const char *duk_str_not_configurable;
  4322. #endif /* !DUK_SINGLE_FILE */
  4323. #define DUK_STR_INVALID_CONTEXT duk_str_invalid_context
  4324. #define DUK_STR_INVALID_INDEX duk_str_invalid_index
  4325. #define DUK_STR_PUSH_BEYOND_ALLOC_STACK duk_str_push_beyond_alloc_stack
  4326. #define DUK_STR_NOT_UNDEFINED duk_str_not_undefined
  4327. #define DUK_STR_NOT_NULL duk_str_not_null
  4328. #define DUK_STR_NOT_BOOLEAN duk_str_not_boolean
  4329. #define DUK_STR_NOT_NUMBER duk_str_not_number
  4330. #define DUK_STR_NOT_STRING duk_str_not_string
  4331. #define DUK_STR_NOT_POINTER duk_str_not_pointer
  4332. #define DUK_STR_NOT_BUFFER duk_str_not_buffer
  4333. #define DUK_STR_UNEXPECTED_TYPE duk_str_unexpected_type
  4334. #define DUK_STR_NOT_THREAD duk_str_not_thread
  4335. #if 0 /*unused*/
  4336. #define DUK_STR_NOT_COMPILEDFUNCTION duk_str_not_compiledfunction
  4337. #endif
  4338. #define DUK_STR_NOT_NATIVEFUNCTION duk_str_not_nativefunction
  4339. #define DUK_STR_NOT_C_FUNCTION duk_str_not_c_function
  4340. #define DUK_STR_DEFAULTVALUE_COERCE_FAILED duk_str_defaultvalue_coerce_failed
  4341. #define DUK_STR_NUMBER_OUTSIDE_RANGE duk_str_number_outside_range
  4342. #define DUK_STR_NOT_OBJECT_COERCIBLE duk_str_not_object_coercible
  4343. #define DUK_STR_STRING_TOO_LONG duk_str_string_too_long
  4344. #define DUK_STR_BUFFER_TOO_LONG duk_str_buffer_too_long
  4345. #define DUK_STR_SPRINTF_TOO_LONG duk_str_sprintf_too_long
  4346. #define DUK_STR_ALLOC_FAILED duk_str_alloc_failed
  4347. #define DUK_STR_POP_TOO_MANY duk_str_pop_too_many
  4348. #define DUK_STR_BUFFER_NOT_DYNAMIC duk_str_buffer_not_dynamic
  4349. #define DUK_STR_FAILED_TO_EXTEND_VALSTACK duk_str_failed_to_extend_valstack
  4350. #define DUK_STR_BASE64_ENCODE_FAILED duk_str_base64_encode_failed
  4351. #define DUK_STR_BASE64_DECODE_FAILED duk_str_base64_decode_failed
  4352. #define DUK_STR_HEX_DECODE_FAILED duk_str_hex_decode_failed
  4353. #define DUK_STR_NO_SOURCECODE duk_str_no_sourcecode
  4354. #define DUK_STR_CONCAT_RESULT_TOO_LONG duk_str_concat_result_too_long
  4355. #define DUK_STR_UNIMPLEMENTED duk_str_unimplemented
  4356. #define DUK_STR_ARRAY_LENGTH_OVER_2G duk_str_array_length_over_2g
  4357. #if !defined(DUK_SINGLE_FILE)
  4358. DUK_INTERNAL_DECL const char *duk_str_invalid_context;
  4359. DUK_INTERNAL_DECL const char *duk_str_invalid_index;
  4360. DUK_INTERNAL_DECL const char *duk_str_push_beyond_alloc_stack;
  4361. DUK_INTERNAL_DECL const char *duk_str_not_undefined;
  4362. DUK_INTERNAL_DECL const char *duk_str_not_null;
  4363. DUK_INTERNAL_DECL const char *duk_str_not_boolean;
  4364. DUK_INTERNAL_DECL const char *duk_str_not_number;
  4365. DUK_INTERNAL_DECL const char *duk_str_not_string;
  4366. DUK_INTERNAL_DECL const char *duk_str_not_pointer;
  4367. DUK_INTERNAL_DECL const char *duk_str_not_buffer;
  4368. DUK_INTERNAL_DECL const char *duk_str_unexpected_type;
  4369. DUK_INTERNAL_DECL const char *duk_str_not_thread;
  4370. #if 0 /*unused*/
  4371. DUK_INTERNAL_DECL const char *duk_str_not_compiledfunction;
  4372. #endif
  4373. DUK_INTERNAL_DECL const char *duk_str_not_nativefunction;
  4374. DUK_INTERNAL_DECL const char *duk_str_not_c_function;
  4375. DUK_INTERNAL_DECL const char *duk_str_defaultvalue_coerce_failed;
  4376. DUK_INTERNAL_DECL const char *duk_str_number_outside_range;
  4377. DUK_INTERNAL_DECL const char *duk_str_not_object_coercible;
  4378. DUK_INTERNAL_DECL const char *duk_str_string_too_long;
  4379. DUK_INTERNAL_DECL const char *duk_str_buffer_too_long;
  4380. DUK_INTERNAL_DECL const char *duk_str_sprintf_too_long;
  4381. DUK_INTERNAL_DECL const char *duk_str_alloc_failed;
  4382. DUK_INTERNAL_DECL const char *duk_str_pop_too_many;
  4383. DUK_INTERNAL_DECL const char *duk_str_buffer_not_dynamic;
  4384. DUK_INTERNAL_DECL const char *duk_str_failed_to_extend_valstack;
  4385. DUK_INTERNAL_DECL const char *duk_str_base64_encode_failed;
  4386. DUK_INTERNAL_DECL const char *duk_str_base64_decode_failed;
  4387. DUK_INTERNAL_DECL const char *duk_str_hex_decode_failed;
  4388. DUK_INTERNAL_DECL const char *duk_str_no_sourcecode;
  4389. DUK_INTERNAL_DECL const char *duk_str_concat_result_too_long;
  4390. DUK_INTERNAL_DECL const char *duk_str_unimplemented;
  4391. DUK_INTERNAL_DECL const char *duk_str_array_length_over_2g;
  4392. #endif /* !DUK_SINGLE_FILE */
  4393. #define DUK_STR_FMT_PTR duk_str_fmt_ptr
  4394. #define DUK_STR_FMT_INVALID_JSON duk_str_fmt_invalid_json
  4395. #define DUK_STR_JSONDEC_RECLIMIT duk_str_jsondec_reclimit
  4396. #define DUK_STR_JSONENC_RECLIMIT duk_str_jsonenc_reclimit
  4397. #define DUK_STR_CYCLIC_INPUT duk_str_cyclic_input
  4398. #if !defined(DUK_SINGLE_FILE)
  4399. DUK_INTERNAL_DECL const char *duk_str_fmt_ptr;
  4400. DUK_INTERNAL_DECL const char *duk_str_fmt_invalid_json;
  4401. DUK_INTERNAL_DECL const char *duk_str_jsondec_reclimit;
  4402. DUK_INTERNAL_DECL const char *duk_str_jsonenc_reclimit;
  4403. DUK_INTERNAL_DECL const char *duk_str_cyclic_input;
  4404. #endif /* !DUK_SINGLE_FILE */
  4405. #define DUK_STR_PROXY_REVOKED duk_str_proxy_revoked
  4406. #define DUK_STR_OBJECT_RESIZE_FAILED duk_str_object_resize_failed
  4407. #define DUK_STR_INVALID_BASE duk_str_invalid_base
  4408. #define DUK_STR_STRICT_CALLER_READ duk_str_strict_caller_read
  4409. #define DUK_STR_PROXY_REJECTED duk_str_proxy_rejected
  4410. #define DUK_STR_INVALID_ARRAY_LENGTH duk_str_invalid_array_length
  4411. #define DUK_STR_ARRAY_LENGTH_WRITE_FAILED duk_str_array_length_write_failed
  4412. #define DUK_STR_ARRAY_LENGTH_NOT_WRITABLE duk_str_array_length_not_writable
  4413. #define DUK_STR_SETTER_UNDEFINED duk_str_setter_undefined
  4414. #define DUK_STR_REDEFINE_VIRT_PROP duk_str_redefine_virt_prop
  4415. #define DUK_STR_INVALID_DESCRIPTOR duk_str_invalid_descriptor
  4416. #define DUK_STR_PROPERTY_IS_VIRTUAL duk_str_property_is_virtual
  4417. #if !defined(DUK_SINGLE_FILE)
  4418. DUK_INTERNAL_DECL const char *duk_str_proxy_revoked;
  4419. DUK_INTERNAL_DECL const char *duk_str_object_resize_failed;
  4420. DUK_INTERNAL_DECL const char *duk_str_invalid_base;
  4421. DUK_INTERNAL_DECL const char *duk_str_strict_caller_read;
  4422. DUK_INTERNAL_DECL const char *duk_str_proxy_rejected;
  4423. DUK_INTERNAL_DECL const char *duk_str_invalid_array_length;
  4424. DUK_INTERNAL_DECL const char *duk_str_array_length_write_failed;
  4425. DUK_INTERNAL_DECL const char *duk_str_array_length_not_writable;
  4426. DUK_INTERNAL_DECL const char *duk_str_setter_undefined;
  4427. DUK_INTERNAL_DECL const char *duk_str_redefine_virt_prop;
  4428. DUK_INTERNAL_DECL const char *duk_str_invalid_descriptor;
  4429. DUK_INTERNAL_DECL const char *duk_str_property_is_virtual;
  4430. #endif /* !DUK_SINGLE_FILE */
  4431. #define DUK_STR_PARSE_ERROR duk_str_parse_error
  4432. #define DUK_STR_DUPLICATE_LABEL duk_str_duplicate_label
  4433. #define DUK_STR_INVALID_LABEL duk_str_invalid_label
  4434. #define DUK_STR_INVALID_ARRAY_LITERAL duk_str_invalid_array_literal
  4435. #define DUK_STR_INVALID_OBJECT_LITERAL duk_str_invalid_object_literal
  4436. #define DUK_STR_INVALID_VAR_DECLARATION duk_str_invalid_var_declaration
  4437. #define DUK_STR_CANNOT_DELETE_IDENTIFIER duk_str_cannot_delete_identifier
  4438. #define DUK_STR_INVALID_EXPRESSION duk_str_invalid_expression
  4439. #define DUK_STR_INVALID_LVALUE duk_str_invalid_lvalue
  4440. #define DUK_STR_EXPECTED_IDENTIFIER duk_str_expected_identifier
  4441. #define DUK_STR_EMPTY_EXPR_NOT_ALLOWED duk_str_empty_expr_not_allowed
  4442. #define DUK_STR_INVALID_FOR duk_str_invalid_for
  4443. #define DUK_STR_INVALID_SWITCH duk_str_invalid_switch
  4444. #define DUK_STR_INVALID_BREAK_CONT_LABEL duk_str_invalid_break_cont_label
  4445. #define DUK_STR_INVALID_RETURN duk_str_invalid_return
  4446. #define DUK_STR_INVALID_TRY duk_str_invalid_try
  4447. #define DUK_STR_INVALID_THROW duk_str_invalid_throw
  4448. #define DUK_STR_WITH_IN_STRICT_MODE duk_str_with_in_strict_mode
  4449. #define DUK_STR_FUNC_STMT_NOT_ALLOWED duk_str_func_stmt_not_allowed
  4450. #define DUK_STR_UNTERMINATED_STMT duk_str_unterminated_stmt
  4451. #define DUK_STR_INVALID_ARG_NAME duk_str_invalid_arg_name
  4452. #define DUK_STR_INVALID_FUNC_NAME duk_str_invalid_func_name
  4453. #define DUK_STR_INVALID_GETSET_NAME duk_str_invalid_getset_name
  4454. #define DUK_STR_FUNC_NAME_REQUIRED duk_str_func_name_required
  4455. #if !defined(DUK_SINGLE_FILE)
  4456. DUK_INTERNAL_DECL const char *duk_str_parse_error;
  4457. DUK_INTERNAL_DECL const char *duk_str_duplicate_label;
  4458. DUK_INTERNAL_DECL const char *duk_str_invalid_label;
  4459. DUK_INTERNAL_DECL const char *duk_str_invalid_array_literal;
  4460. DUK_INTERNAL_DECL const char *duk_str_invalid_object_literal;
  4461. DUK_INTERNAL_DECL const char *duk_str_invalid_var_declaration;
  4462. DUK_INTERNAL_DECL const char *duk_str_cannot_delete_identifier;
  4463. DUK_INTERNAL_DECL const char *duk_str_invalid_expression;
  4464. DUK_INTERNAL_DECL const char *duk_str_invalid_lvalue;
  4465. DUK_INTERNAL_DECL const char *duk_str_expected_identifier;
  4466. DUK_INTERNAL_DECL const char *duk_str_empty_expr_not_allowed;
  4467. DUK_INTERNAL_DECL const char *duk_str_invalid_for;
  4468. DUK_INTERNAL_DECL const char *duk_str_invalid_switch;
  4469. DUK_INTERNAL_DECL const char *duk_str_invalid_break_cont_label;
  4470. DUK_INTERNAL_DECL const char *duk_str_invalid_return;
  4471. DUK_INTERNAL_DECL const char *duk_str_invalid_try;
  4472. DUK_INTERNAL_DECL const char *duk_str_invalid_throw;
  4473. DUK_INTERNAL_DECL const char *duk_str_with_in_strict_mode;
  4474. DUK_INTERNAL_DECL const char *duk_str_func_stmt_not_allowed;
  4475. DUK_INTERNAL_DECL const char *duk_str_unterminated_stmt;
  4476. DUK_INTERNAL_DECL const char *duk_str_invalid_arg_name;
  4477. DUK_INTERNAL_DECL const char *duk_str_invalid_func_name;
  4478. DUK_INTERNAL_DECL const char *duk_str_invalid_getset_name;
  4479. DUK_INTERNAL_DECL const char *duk_str_func_name_required;
  4480. #endif /* !DUK_SINGLE_FILE */
  4481. #define DUK_STR_INTERNAL_ERROR_EXEC_LONGJMP duk_str_internal_error_exec_longjmp
  4482. #if !defined(DUK_SINGLE_FILE)
  4483. DUK_INTERNAL_DECL const char *duk_str_internal_error_exec_longjmp;
  4484. #endif /* !DUK_SINGLE_FILE */
  4485. #define DUK_STR_INVALID_QUANTIFIER_NO_ATOM duk_str_invalid_quantifier_no_atom
  4486. #define DUK_STR_INVALID_QUANTIFIER_VALUES duk_str_invalid_quantifier_values
  4487. #define DUK_STR_QUANTIFIER_TOO_MANY_COPIES duk_str_quantifier_too_many_copies
  4488. #define DUK_STR_UNEXPECTED_CLOSING_PAREN duk_str_unexpected_closing_paren
  4489. #define DUK_STR_UNEXPECTED_END_OF_PATTERN duk_str_unexpected_end_of_pattern
  4490. #define DUK_STR_UNEXPECTED_REGEXP_TOKEN duk_str_unexpected_regexp_token
  4491. #define DUK_STR_INVALID_REGEXP_FLAGS duk_str_invalid_regexp_flags
  4492. #define DUK_STR_INVALID_BACKREFS duk_str_invalid_backrefs
  4493. #define DUK_STR_REGEXP_BACKTRACK_FAILED duk_str_regexp_backtrack_failed
  4494. #define DUK_STR_REGEXP_ADVANCE_FAILED duk_str_regexp_advance_failed
  4495. #define DUK_STR_REGEXP_INTERNAL_ERROR duk_str_regexp_internal_error
  4496. #if !defined(DUK_SINGLE_FILE)
  4497. DUK_INTERNAL_DECL const char *duk_str_invalid_quantifier_no_atom;
  4498. DUK_INTERNAL_DECL const char *duk_str_invalid_quantifier_values;
  4499. DUK_INTERNAL_DECL const char *duk_str_quantifier_too_many_copies;
  4500. DUK_INTERNAL_DECL const char *duk_str_unexpected_closing_paren;
  4501. DUK_INTERNAL_DECL const char *duk_str_unexpected_end_of_pattern;
  4502. DUK_INTERNAL_DECL const char *duk_str_unexpected_regexp_token;
  4503. DUK_INTERNAL_DECL const char *duk_str_invalid_regexp_flags;
  4504. DUK_INTERNAL_DECL const char *duk_str_invalid_backrefs;
  4505. DUK_INTERNAL_DECL const char *duk_str_regexp_backtrack_failed;
  4506. DUK_INTERNAL_DECL const char *duk_str_regexp_advance_failed;
  4507. DUK_INTERNAL_DECL const char *duk_str_regexp_internal_error;
  4508. #endif /* !DUK_SINGLE_FILE */
  4509. #define DUK_STR_VALSTACK_LIMIT duk_str_valstack_limit
  4510. #define DUK_STR_CALLSTACK_LIMIT duk_str_callstack_limit
  4511. #define DUK_STR_CATCHSTACK_LIMIT duk_str_catchstack_limit
  4512. #define DUK_STR_OBJECT_PROPERTY_LIMIT duk_str_object_property_limit
  4513. #define DUK_STR_PROTOTYPE_CHAIN_LIMIT duk_str_prototype_chain_limit
  4514. #define DUK_STR_BOUND_CHAIN_LIMIT duk_str_bound_chain_limit
  4515. #define DUK_STR_C_CALLSTACK_LIMIT duk_str_c_callstack_limit
  4516. #define DUK_STR_COMPILER_RECURSION_LIMIT duk_str_compiler_recursion_limit
  4517. #define DUK_STR_BYTECODE_LIMIT duk_str_bytecode_limit
  4518. #define DUK_STR_REG_LIMIT duk_str_reg_limit
  4519. #define DUK_STR_TEMP_LIMIT duk_str_temp_limit
  4520. #define DUK_STR_CONST_LIMIT duk_str_const_limit
  4521. #define DUK_STR_FUNC_LIMIT duk_str_func_limit
  4522. #define DUK_STR_REGEXP_COMPILER_RECURSION_LIMIT duk_str_regexp_compiler_recursion_limit
  4523. #define DUK_STR_REGEXP_EXECUTOR_RECURSION_LIMIT duk_str_regexp_executor_recursion_limit
  4524. #define DUK_STR_REGEXP_EXECUTOR_STEP_LIMIT duk_str_regexp_executor_step_limit
  4525. #if !defined(DUK_SINGLE_FILE)
  4526. DUK_INTERNAL_DECL const char *duk_str_valstack_limit;
  4527. DUK_INTERNAL_DECL const char *duk_str_callstack_limit;
  4528. DUK_INTERNAL_DECL const char *duk_str_catchstack_limit;
  4529. DUK_INTERNAL_DECL const char *duk_str_object_property_limit;
  4530. DUK_INTERNAL_DECL const char *duk_str_prototype_chain_limit;
  4531. DUK_INTERNAL_DECL const char *duk_str_bound_chain_limit;
  4532. DUK_INTERNAL_DECL const char *duk_str_c_callstack_limit;
  4533. DUK_INTERNAL_DECL const char *duk_str_compiler_recursion_limit;
  4534. DUK_INTERNAL_DECL const char *duk_str_bytecode_limit;
  4535. DUK_INTERNAL_DECL const char *duk_str_reg_limit;
  4536. DUK_INTERNAL_DECL const char *duk_str_temp_limit;
  4537. DUK_INTERNAL_DECL const char *duk_str_const_limit;
  4538. DUK_INTERNAL_DECL const char *duk_str_func_limit;
  4539. DUK_INTERNAL_DECL const char *duk_str_regexp_compiler_recursion_limit;
  4540. DUK_INTERNAL_DECL const char *duk_str_regexp_executor_recursion_limit;
  4541. DUK_INTERNAL_DECL const char *duk_str_regexp_executor_step_limit;
  4542. #endif /* !DUK_SINGLE_FILE */
  4543. #define DUK_STR_ANON duk_str_anon
  4544. #define DUK_STR_REALLOC_FAILED duk_str_realloc_failed
  4545. #if !defined(DUK_SINGLE_FILE)
  4546. DUK_INTERNAL_DECL const char *duk_str_anon;
  4547. DUK_INTERNAL_DECL const char *duk_str_realloc_failed;
  4548. #endif /* !DUK_SINGLE_FILE */
  4549. #endif /* DUK_ERRMSG_H_INCLUDED */
  4550. #line 1 "duk_js_bytecode.h"
  4551. /*
  4552. * Ecmascript bytecode
  4553. */
  4554. #ifndef DUK_JS_BYTECODE_H_INCLUDED
  4555. #define DUK_JS_BYTECODE_H_INCLUDED
  4556. /*
  4557. * Logical instruction layout
  4558. * ==========================
  4559. *
  4560. * !3!3!2!2!2!2!2!2!2!2!2!2!1!1!1!1!1!1!1!1!1!1! ! ! ! ! ! ! ! ! ! !
  4561. * !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!
  4562. * +---------------------------------------------------+-----------+
  4563. * ! C ! B ! A ! OP !
  4564. * +---------------------------------------------------+-----------+
  4565. *
  4566. * OP (6 bits): opcode (DUK_OP_*), access should be fastest
  4567. * A (8 bits): typically a target register number
  4568. * B (9 bits): typically first source register/constant number
  4569. * C (9 bits): typically second source register/constant number
  4570. *
  4571. * Some instructions combine BC or ABC together for larger parameter values.
  4572. * Signed integers (e.g. jump offsets) are encoded as unsigned, with an opcode
  4573. * specific bias. B and C may denote a register or a constant, see
  4574. * DUK_BC_ISREG() and DUK_BC_ISCONST().
  4575. *
  4576. * Note: macro naming is a bit misleading, e.g. "ABC" in macro name but
  4577. * the field layout is logically "CBA".
  4578. */
  4579. typedef duk_uint32_t duk_instr_t;
  4580. #define DUK_DEC_OP(x) ((x) & 0x3fUL)
  4581. #define DUK_DEC_A(x) (((x) >> 6) & 0xffUL)
  4582. #define DUK_DEC_B(x) (((x) >> 14) & 0x1ffUL)
  4583. #define DUK_DEC_C(x) (((x) >> 23) & 0x1ffUL)
  4584. #define DUK_DEC_BC(x) (((x) >> 14) & 0x3ffffUL)
  4585. #define DUK_DEC_ABC(x) (((x) >> 6) & 0x3ffffffUL)
  4586. #define DUK_ENC_OP(op) ((duk_instr_t) (op))
  4587. #define DUK_ENC_OP_ABC(op,abc) ((duk_instr_t) ( \
  4588. (((duk_instr_t) (abc)) << 6) | \
  4589. ((duk_instr_t) (op)) \
  4590. ))
  4591. #define DUK_ENC_OP_A_BC(op,a,bc) ((duk_instr_t) ( \
  4592. (((duk_instr_t) (bc)) << 14) | \
  4593. (((duk_instr_t) (a)) << 6) | \
  4594. ((duk_instr_t) (op)) \
  4595. ))
  4596. #define DUK_ENC_OP_A_B_C(op,a,b,c) ((duk_instr_t) ( \
  4597. (((duk_instr_t) (c)) << 23) | \
  4598. (((duk_instr_t) (b)) << 14) | \
  4599. (((duk_instr_t) (a)) << 6) | \
  4600. ((duk_instr_t) (op)) \
  4601. ))
  4602. #define DUK_ENC_OP_A_B(op,a,b) DUK_ENC_OP_A_B_C(op,a,b,0)
  4603. #define DUK_ENC_OP_A(op,a) DUK_ENC_OP_A_B_C(op,a,0,0)
  4604. /* Constants should be signed so that signed arithmetic involving them
  4605. * won't cause values to be coerced accidentally to unsigned.
  4606. */
  4607. #define DUK_BC_OP_MIN 0
  4608. #define DUK_BC_OP_MAX 0x3fL
  4609. #define DUK_BC_A_MIN 0
  4610. #define DUK_BC_A_MAX 0xffL
  4611. #define DUK_BC_B_MIN 0
  4612. #define DUK_BC_B_MAX 0x1ffL
  4613. #define DUK_BC_C_MIN 0
  4614. #define DUK_BC_C_MAX 0x1ffL
  4615. #define DUK_BC_BC_MIN 0
  4616. #define DUK_BC_BC_MAX 0x3ffffL
  4617. #define DUK_BC_ABC_MIN 0
  4618. #define DUK_BC_ABC_MAX 0x3ffffffL
  4619. #define DUK_BC_EXTRAOP_MIN DUK_BC_A_MIN
  4620. #define DUK_BC_EXTRAOP_MAX DUK_BC_A_MAX
  4621. #define DUK_OP_LDREG 0
  4622. #define DUK_OP_STREG 1
  4623. #define DUK_OP_LDCONST 2
  4624. #define DUK_OP_LDINT 3
  4625. #define DUK_OP_LDINTX 4
  4626. #define DUK_OP_MPUTOBJ 5
  4627. #define DUK_OP_MPUTOBJI 6
  4628. #define DUK_OP_MPUTARR 7
  4629. #define DUK_OP_MPUTARRI 8
  4630. #define DUK_OP_NEW 9
  4631. #define DUK_OP_NEWI 10
  4632. #define DUK_OP_REGEXP 11
  4633. #define DUK_OP_CSREG 12
  4634. #define DUK_OP_CSREGI 13
  4635. #define DUK_OP_GETVAR 14
  4636. #define DUK_OP_PUTVAR 15
  4637. #define DUK_OP_DECLVAR 16
  4638. #define DUK_OP_DELVAR 17
  4639. #define DUK_OP_CSVAR 18
  4640. #define DUK_OP_CSVARI 19
  4641. #define DUK_OP_CLOSURE 20
  4642. #define DUK_OP_GETPROP 21
  4643. #define DUK_OP_PUTPROP 22
  4644. #define DUK_OP_DELPROP 23
  4645. #define DUK_OP_CSPROP 24
  4646. #define DUK_OP_CSPROPI 25
  4647. #define DUK_OP_ADD 26
  4648. #define DUK_OP_SUB 27
  4649. #define DUK_OP_MUL 28
  4650. #define DUK_OP_DIV 29
  4651. #define DUK_OP_MOD 30
  4652. #define DUK_OP_BAND 31
  4653. #define DUK_OP_BOR 32
  4654. #define DUK_OP_BXOR 33
  4655. #define DUK_OP_BASL 34
  4656. #define DUK_OP_BLSR 35
  4657. #define DUK_OP_BASR 36
  4658. #define DUK_OP_EQ 37
  4659. #define DUK_OP_NEQ 38
  4660. #define DUK_OP_SEQ 39
  4661. #define DUK_OP_SNEQ 40
  4662. #define DUK_OP_GT 41
  4663. #define DUK_OP_GE 42
  4664. #define DUK_OP_LT 43
  4665. #define DUK_OP_LE 44
  4666. #define DUK_OP_IF 45
  4667. #define DUK_OP_JUMP 46
  4668. #define DUK_OP_RETURN 47
  4669. #define DUK_OP_CALL 48
  4670. #define DUK_OP_CALLI 49
  4671. #define DUK_OP_TRYCATCH 50
  4672. #define DUK_OP_EXTRA 51
  4673. #define DUK_OP_PREINCR 52 /* pre/post opcode values have constraints, */
  4674. #define DUK_OP_PREDECR 53 /* see duk_js_executor.c */
  4675. #define DUK_OP_POSTINCR 54
  4676. #define DUK_OP_POSTDECR 55
  4677. #define DUK_OP_PREINCV 56
  4678. #define DUK_OP_PREDECV 57
  4679. #define DUK_OP_POSTINCV 58
  4680. #define DUK_OP_POSTDECV 59
  4681. #define DUK_OP_PREINCP 60
  4682. #define DUK_OP_PREDECP 61
  4683. #define DUK_OP_POSTINCP 62
  4684. #define DUK_OP_POSTDECP 63
  4685. #define DUK_OP_NONE 64 /* dummy value used as marker */
  4686. /* DUK_OP_EXTRA, sub-operation in A */
  4687. #define DUK_EXTRAOP_NOP 0
  4688. #define DUK_EXTRAOP_INVALID 1
  4689. #define DUK_EXTRAOP_LDTHIS 2
  4690. #define DUK_EXTRAOP_LDUNDEF 3
  4691. #define DUK_EXTRAOP_LDNULL 4
  4692. #define DUK_EXTRAOP_LDTRUE 5
  4693. #define DUK_EXTRAOP_LDFALSE 6
  4694. #define DUK_EXTRAOP_NEWOBJ 7
  4695. #define DUK_EXTRAOP_NEWARR 8
  4696. #define DUK_EXTRAOP_SETALEN 9
  4697. #define DUK_EXTRAOP_TYPEOF 10
  4698. #define DUK_EXTRAOP_TYPEOFID 11
  4699. #define DUK_EXTRAOP_INITENUM 12
  4700. #define DUK_EXTRAOP_NEXTENUM 13
  4701. #define DUK_EXTRAOP_INITSET 14
  4702. #define DUK_EXTRAOP_INITSETI 15
  4703. #define DUK_EXTRAOP_INITGET 16
  4704. #define DUK_EXTRAOP_INITGETI 17
  4705. #define DUK_EXTRAOP_ENDTRY 18
  4706. #define DUK_EXTRAOP_ENDCATCH 19
  4707. #define DUK_EXTRAOP_ENDFIN 20
  4708. #define DUK_EXTRAOP_THROW 21
  4709. #define DUK_EXTRAOP_INVLHS 22
  4710. #define DUK_EXTRAOP_UNM 23
  4711. #define DUK_EXTRAOP_UNP 24
  4712. #define DUK_EXTRAOP_DEBUGGER 25
  4713. #define DUK_EXTRAOP_BREAK 26
  4714. #define DUK_EXTRAOP_CONTINUE 27
  4715. #define DUK_EXTRAOP_BNOT 28
  4716. #define DUK_EXTRAOP_LNOT 29
  4717. #define DUK_EXTRAOP_INSTOF 30
  4718. #define DUK_EXTRAOP_IN 31
  4719. #define DUK_EXTRAOP_LABEL 32
  4720. #define DUK_EXTRAOP_ENDLABEL 33
  4721. /* DUK_OP_EXTRA for debugging */
  4722. #define DUK_EXTRAOP_DUMPREG 128
  4723. #define DUK_EXTRAOP_DUMPREGS 129
  4724. #define DUK_EXTRAOP_LOGMARK 130
  4725. /* DUK_OP_CALL flags in A */
  4726. #define DUK_BC_CALL_FLAG_TAILCALL (1 << 0)
  4727. #define DUK_BC_CALL_FLAG_EVALCALL (1 << 1)
  4728. /* DUK_OP_TRYCATCH flags in A */
  4729. #define DUK_BC_TRYCATCH_FLAG_HAVE_CATCH (1 << 0)
  4730. #define DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY (1 << 1)
  4731. #define DUK_BC_TRYCATCH_FLAG_CATCH_BINDING (1 << 2)
  4732. #define DUK_BC_TRYCATCH_FLAG_WITH_BINDING (1 << 3)
  4733. /* DUK_OP_RETURN flags in A */
  4734. #define DUK_BC_RETURN_FLAG_FAST (1 << 0)
  4735. #define DUK_BC_RETURN_FLAG_HAVE_RETVAL (1 << 1)
  4736. /* DUK_OP_DECLVAR flags in A; bottom bits are reserved for propdesc flags (DUK_PROPDESC_FLAG_XXX) */
  4737. #define DUK_BC_DECLVAR_FLAG_UNDEF_VALUE (1 << 4) /* use 'undefined' for value automatically */
  4738. #define DUK_BC_DECLVAR_FLAG_FUNC_DECL (1 << 5) /* function declaration */
  4739. /* misc constants and helper macros */
  4740. #define DUK_BC_REGLIMIT 256 /* if B/C is >= this value, refers to a const */
  4741. #define DUK_BC_ISREG(x) ((x) < DUK_BC_REGLIMIT)
  4742. #define DUK_BC_ISCONST(x) ((x) >= DUK_BC_REGLIMIT)
  4743. #define DUK_BC_LDINT_BIAS (1L << 17)
  4744. #define DUK_BC_LDINTX_SHIFT 18
  4745. #define DUK_BC_JUMP_BIAS (1L << 25)
  4746. #endif /* DUK_JS_BYTECODE_H_INCLUDED */
  4747. #line 1 "duk_lexer.h"
  4748. /*
  4749. * Lexer defines.
  4750. */
  4751. #ifndef DUK_LEXER_H_INCLUDED
  4752. #define DUK_LEXER_H_INCLUDED
  4753. typedef void (*duk_re_range_callback)(void *user, duk_codepoint_t r1, duk_codepoint_t r2, duk_bool_t direct);
  4754. /*
  4755. * A token is interpreted as any possible production of InputElementDiv
  4756. * and InputElementRegExp, see E5 Section 7 in its entirety. Note that
  4757. * the E5 "Token" production does not cover all actual tokens of the
  4758. * language (which is explicitly stated in the specification, Section 7.5).
  4759. * Null and boolean literals are defined as part of both ReservedWord
  4760. * (E5 Section 7.6.1) and Literal (E5 Section 7.8) productions. Here,
  4761. * null and boolean values have literal tokens, and are not reserved
  4762. * words.
  4763. *
  4764. * Decimal literal negative/positive sign is -not- part of DUK_TOK_NUMBER.
  4765. * The number tokens always have a non-negative value. The unary minus
  4766. * operator in "-1.0" is optimized during compilation to yield a single
  4767. * negative constant.
  4768. *
  4769. * Token numbering is free except that reserved words are required to be
  4770. * in a continuous range and in a particular order. See genstrings.py.
  4771. */
  4772. #define DUK_LEXER_INITCTX(ctx) duk_lexer_initctx((ctx))
  4773. #define DUK_LEXER_SETPOINT(ctx,pt) duk_lexer_setpoint((ctx), (pt))
  4774. #define DUK_LEXER_GETPOINT(ctx,pt) do { (pt)->offset = (ctx)->offsets[0]; \
  4775. (pt)->line = (ctx)->lines[0]; } while (0)
  4776. /* currently 6 characters of lookup are actually needed (duk_lexer.c) */
  4777. #define DUK_LEXER_WINDOW_SIZE 8
  4778. #define DUK_TOK_MINVAL 0
  4779. /* returned after EOF (infinite amount) */
  4780. #define DUK_TOK_EOF 0
  4781. /* line terminator or multi-line comment with internal lineterm (E5 Sections 7.3, 7.4) */
  4782. #define DUK_TOK_LINETERM 1
  4783. /* single-line comment or multi-line comment without internal lineterm (E5 Section 7.4) */
  4784. #define DUK_TOK_COMMENT 2
  4785. /* identifier names (E5 Section 7.6) */
  4786. #define DUK_TOK_IDENTIFIER 3
  4787. /* reserved words: keywords */
  4788. #define DUK_TOK_START_RESERVED 4
  4789. #define DUK_TOK_BREAK 4
  4790. #define DUK_TOK_CASE 5
  4791. #define DUK_TOK_CATCH 6
  4792. #define DUK_TOK_CONTINUE 7
  4793. #define DUK_TOK_DEBUGGER 8
  4794. #define DUK_TOK_DEFAULT 9
  4795. #define DUK_TOK_DELETE 10
  4796. #define DUK_TOK_DO 11
  4797. #define DUK_TOK_ELSE 12
  4798. #define DUK_TOK_FINALLY 13
  4799. #define DUK_TOK_FOR 14
  4800. #define DUK_TOK_FUNCTION 15
  4801. #define DUK_TOK_IF 16
  4802. #define DUK_TOK_IN 17
  4803. #define DUK_TOK_INSTANCEOF 18
  4804. #define DUK_TOK_NEW 19
  4805. #define DUK_TOK_RETURN 20
  4806. #define DUK_TOK_SWITCH 21
  4807. #define DUK_TOK_THIS 22
  4808. #define DUK_TOK_THROW 23
  4809. #define DUK_TOK_TRY 24
  4810. #define DUK_TOK_TYPEOF 25
  4811. #define DUK_TOK_VAR 26
  4812. #define DUK_TOK_VOID 27
  4813. #define DUK_TOK_WHILE 28
  4814. #define DUK_TOK_WITH 29
  4815. /* reserved words: future reserved words */
  4816. #define DUK_TOK_CLASS 30
  4817. #define DUK_TOK_CONST 31
  4818. #define DUK_TOK_ENUM 32
  4819. #define DUK_TOK_EXPORT 33
  4820. #define DUK_TOK_EXTENDS 34
  4821. #define DUK_TOK_IMPORT 35
  4822. #define DUK_TOK_SUPER 36
  4823. /* "null", "true", and "false" are always reserved words.
  4824. * Note that "get" and "set" are not!
  4825. */
  4826. #define DUK_TOK_NULL 37
  4827. #define DUK_TOK_TRUE 38
  4828. #define DUK_TOK_FALSE 39
  4829. /* reserved words: additional future reserved words in strict mode */
  4830. #define DUK_TOK_START_STRICT_RESERVED 40 /* inclusive */
  4831. #define DUK_TOK_IMPLEMENTS 40
  4832. #define DUK_TOK_INTERFACE 41
  4833. #define DUK_TOK_LET 42
  4834. #define DUK_TOK_PACKAGE 43
  4835. #define DUK_TOK_PRIVATE 44
  4836. #define DUK_TOK_PROTECTED 45
  4837. #define DUK_TOK_PUBLIC 46
  4838. #define DUK_TOK_STATIC 47
  4839. #define DUK_TOK_YIELD 48
  4840. #define DUK_TOK_END_RESERVED 49 /* exclusive */
  4841. /* "get" and "set" are tokens but NOT ReservedWords. They are currently
  4842. * parsed and identifiers and these defines are actually now unused.
  4843. */
  4844. #define DUK_TOK_GET 49
  4845. #define DUK_TOK_SET 50
  4846. /* punctuators (unlike the spec, also includes "/" and "/=") */
  4847. #define DUK_TOK_LCURLY 51
  4848. #define DUK_TOK_RCURLY 52
  4849. #define DUK_TOK_LBRACKET 53
  4850. #define DUK_TOK_RBRACKET 54
  4851. #define DUK_TOK_LPAREN 55
  4852. #define DUK_TOK_RPAREN 56
  4853. #define DUK_TOK_PERIOD 57
  4854. #define DUK_TOK_SEMICOLON 58
  4855. #define DUK_TOK_COMMA 59
  4856. #define DUK_TOK_LT 60
  4857. #define DUK_TOK_GT 61
  4858. #define DUK_TOK_LE 62
  4859. #define DUK_TOK_GE 63
  4860. #define DUK_TOK_EQ 64
  4861. #define DUK_TOK_NEQ 65
  4862. #define DUK_TOK_SEQ 66
  4863. #define DUK_TOK_SNEQ 67
  4864. #define DUK_TOK_ADD 68
  4865. #define DUK_TOK_SUB 69
  4866. #define DUK_TOK_MUL 70
  4867. #define DUK_TOK_DIV 71
  4868. #define DUK_TOK_MOD 72
  4869. #define DUK_TOK_INCREMENT 73
  4870. #define DUK_TOK_DECREMENT 74
  4871. #define DUK_TOK_ALSHIFT 75 /* named "arithmetic" because result is signed */
  4872. #define DUK_TOK_ARSHIFT 76
  4873. #define DUK_TOK_RSHIFT 77
  4874. #define DUK_TOK_BAND 78
  4875. #define DUK_TOK_BOR 79
  4876. #define DUK_TOK_BXOR 80
  4877. #define DUK_TOK_LNOT 81
  4878. #define DUK_TOK_BNOT 82
  4879. #define DUK_TOK_LAND 83
  4880. #define DUK_TOK_LOR 84
  4881. #define DUK_TOK_QUESTION 85
  4882. #define DUK_TOK_COLON 86
  4883. #define DUK_TOK_EQUALSIGN 87
  4884. #define DUK_TOK_ADD_EQ 88
  4885. #define DUK_TOK_SUB_EQ 89
  4886. #define DUK_TOK_MUL_EQ 90
  4887. #define DUK_TOK_DIV_EQ 91
  4888. #define DUK_TOK_MOD_EQ 92
  4889. #define DUK_TOK_ALSHIFT_EQ 93
  4890. #define DUK_TOK_ARSHIFT_EQ 94
  4891. #define DUK_TOK_RSHIFT_EQ 95
  4892. #define DUK_TOK_BAND_EQ 96
  4893. #define DUK_TOK_BOR_EQ 97
  4894. #define DUK_TOK_BXOR_EQ 98
  4895. /* literals (E5 Section 7.8), except null, true, false, which are treated
  4896. * like reserved words (above).
  4897. */
  4898. #define DUK_TOK_NUMBER 99
  4899. #define DUK_TOK_STRING 100
  4900. #define DUK_TOK_REGEXP 101
  4901. #define DUK_TOK_MAXVAL 101 /* inclusive */
  4902. /* Convert heap string index to a token (reserved words) */
  4903. #define DUK_STRIDX_TO_TOK(x) ((x) - DUK_STRIDX_START_RESERVED + DUK_TOK_START_RESERVED)
  4904. /* Sanity check */
  4905. #if (DUK_TOK_MAXVAL > 255)
  4906. #error DUK_TOK_MAXVAL too large, code assumes it fits into 8 bits
  4907. #endif
  4908. /* Sanity checks for string and token defines */
  4909. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_BREAK) != DUK_TOK_BREAK)
  4910. #error mismatch in token defines
  4911. #endif
  4912. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CASE) != DUK_TOK_CASE)
  4913. #error mismatch in token defines
  4914. #endif
  4915. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CATCH) != DUK_TOK_CATCH)
  4916. #error mismatch in token defines
  4917. #endif
  4918. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CONTINUE) != DUK_TOK_CONTINUE)
  4919. #error mismatch in token defines
  4920. #endif
  4921. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DEBUGGER) != DUK_TOK_DEBUGGER)
  4922. #error mismatch in token defines
  4923. #endif
  4924. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DEFAULT) != DUK_TOK_DEFAULT)
  4925. #error mismatch in token defines
  4926. #endif
  4927. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DELETE) != DUK_TOK_DELETE)
  4928. #error mismatch in token defines
  4929. #endif
  4930. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DO) != DUK_TOK_DO)
  4931. #error mismatch in token defines
  4932. #endif
  4933. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_ELSE) != DUK_TOK_ELSE)
  4934. #error mismatch in token defines
  4935. #endif
  4936. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_FINALLY) != DUK_TOK_FINALLY)
  4937. #error mismatch in token defines
  4938. #endif
  4939. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_FOR) != DUK_TOK_FOR)
  4940. #error mismatch in token defines
  4941. #endif
  4942. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_LC_FUNCTION) != DUK_TOK_FUNCTION)
  4943. #error mismatch in token defines
  4944. #endif
  4945. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IF) != DUK_TOK_IF)
  4946. #error mismatch in token defines
  4947. #endif
  4948. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IN) != DUK_TOK_IN)
  4949. #error mismatch in token defines
  4950. #endif
  4951. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_INSTANCEOF) != DUK_TOK_INSTANCEOF)
  4952. #error mismatch in token defines
  4953. #endif
  4954. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_NEW) != DUK_TOK_NEW)
  4955. #error mismatch in token defines
  4956. #endif
  4957. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_RETURN) != DUK_TOK_RETURN)
  4958. #error mismatch in token defines
  4959. #endif
  4960. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_SWITCH) != DUK_TOK_SWITCH)
  4961. #error mismatch in token defines
  4962. #endif
  4963. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_THIS) != DUK_TOK_THIS)
  4964. #error mismatch in token defines
  4965. #endif
  4966. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_THROW) != DUK_TOK_THROW)
  4967. #error mismatch in token defines
  4968. #endif
  4969. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_TRY) != DUK_TOK_TRY)
  4970. #error mismatch in token defines
  4971. #endif
  4972. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_TYPEOF) != DUK_TOK_TYPEOF)
  4973. #error mismatch in token defines
  4974. #endif
  4975. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_VAR) != DUK_TOK_VAR)
  4976. #error mismatch in token defines
  4977. #endif
  4978. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_VOID) != DUK_TOK_VOID)
  4979. #error mismatch in token defines
  4980. #endif
  4981. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_WHILE) != DUK_TOK_WHILE)
  4982. #error mismatch in token defines
  4983. #endif
  4984. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_WITH) != DUK_TOK_WITH)
  4985. #error mismatch in token defines
  4986. #endif
  4987. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CLASS) != DUK_TOK_CLASS)
  4988. #error mismatch in token defines
  4989. #endif
  4990. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CONST) != DUK_TOK_CONST)
  4991. #error mismatch in token defines
  4992. #endif
  4993. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_ENUM) != DUK_TOK_ENUM)
  4994. #error mismatch in token defines
  4995. #endif
  4996. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_EXPORT) != DUK_TOK_EXPORT)
  4997. #error mismatch in token defines
  4998. #endif
  4999. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_EXTENDS) != DUK_TOK_EXTENDS)
  5000. #error mismatch in token defines
  5001. #endif
  5002. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IMPORT) != DUK_TOK_IMPORT)
  5003. #error mismatch in token defines
  5004. #endif
  5005. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_SUPER) != DUK_TOK_SUPER)
  5006. #error mismatch in token defines
  5007. #endif
  5008. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_LC_NULL) != DUK_TOK_NULL)
  5009. #error mismatch in token defines
  5010. #endif
  5011. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_TRUE) != DUK_TOK_TRUE)
  5012. #error mismatch in token defines
  5013. #endif
  5014. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_FALSE) != DUK_TOK_FALSE)
  5015. #error mismatch in token defines
  5016. #endif
  5017. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IMPLEMENTS) != DUK_TOK_IMPLEMENTS)
  5018. #error mismatch in token defines
  5019. #endif
  5020. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_INTERFACE) != DUK_TOK_INTERFACE)
  5021. #error mismatch in token defines
  5022. #endif
  5023. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_LET) != DUK_TOK_LET)
  5024. #error mismatch in token defines
  5025. #endif
  5026. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PACKAGE) != DUK_TOK_PACKAGE)
  5027. #error mismatch in token defines
  5028. #endif
  5029. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PRIVATE) != DUK_TOK_PRIVATE)
  5030. #error mismatch in token defines
  5031. #endif
  5032. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PROTECTED) != DUK_TOK_PROTECTED)
  5033. #error mismatch in token defines
  5034. #endif
  5035. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PUBLIC) != DUK_TOK_PUBLIC)
  5036. #error mismatch in token defines
  5037. #endif
  5038. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_STATIC) != DUK_TOK_STATIC)
  5039. #error mismatch in token defines
  5040. #endif
  5041. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_YIELD) != DUK_TOK_YIELD)
  5042. #error mismatch in token defines
  5043. #endif
  5044. /* Regexp tokens */
  5045. #define DUK_RETOK_EOF 0
  5046. #define DUK_RETOK_DISJUNCTION 1
  5047. #define DUK_RETOK_QUANTIFIER 2
  5048. #define DUK_RETOK_ASSERT_START 3
  5049. #define DUK_RETOK_ASSERT_END 4
  5050. #define DUK_RETOK_ASSERT_WORD_BOUNDARY 5
  5051. #define DUK_RETOK_ASSERT_NOT_WORD_BOUNDARY 6
  5052. #define DUK_RETOK_ASSERT_START_POS_LOOKAHEAD 7
  5053. #define DUK_RETOK_ASSERT_START_NEG_LOOKAHEAD 8
  5054. #define DUK_RETOK_ATOM_PERIOD 9
  5055. #define DUK_RETOK_ATOM_CHAR 10
  5056. #define DUK_RETOK_ATOM_DIGIT 11
  5057. #define DUK_RETOK_ATOM_NOT_DIGIT 12
  5058. #define DUK_RETOK_ATOM_WHITE 13
  5059. #define DUK_RETOK_ATOM_NOT_WHITE 14
  5060. #define DUK_RETOK_ATOM_WORD_CHAR 15
  5061. #define DUK_RETOK_ATOM_NOT_WORD_CHAR 16
  5062. #define DUK_RETOK_ATOM_BACKREFERENCE 17
  5063. #define DUK_RETOK_ATOM_START_CAPTURE_GROUP 18
  5064. #define DUK_RETOK_ATOM_START_NONCAPTURE_GROUP 19
  5065. #define DUK_RETOK_ATOM_START_CHARCLASS 20
  5066. #define DUK_RETOK_ATOM_START_CHARCLASS_INVERTED 21
  5067. #define DUK_RETOK_ATOM_END_GROUP 22
  5068. /* constants for duk_lexer_ctx.buf */
  5069. #define DUK_LEXER_TEMP_BUF_INITIAL 64
  5070. #define DUK_LEXER_TEMP_BUF_LIMIT 256
  5071. /* A token value. Can be memcpy()'d, but note that slot1/slot2 values are on the valstack. */
  5072. struct duk_token {
  5073. duk_small_int_t t; /* token type (with reserved word identification) */
  5074. duk_small_int_t t_nores; /* token type (with reserved words as DUK_TOK_IDENTIFER) */
  5075. duk_double_t num; /* numeric value of token */
  5076. duk_hstring *str1; /* string 1 of token (borrowed, stored to ctx->slot1_idx) */
  5077. duk_hstring *str2; /* string 2 of token (borrowed, stored to ctx->slot1_idx) */
  5078. duk_size_t start_offset; /* start byte offset of token in lexer input */
  5079. duk_int_t start_line; /* start line of token (first char) */
  5080. duk_int_t num_escapes; /* number of escapes and line continuations (for directive prologue) */
  5081. duk_bool_t lineterm; /* token was preceded by a lineterm */
  5082. duk_bool_t allow_auto_semi; /* token allows automatic semicolon insertion (eof or preceded by newline) */
  5083. };
  5084. #define DUK_RE_QUANTIFIER_INFINITE ((duk_uint32_t) 0xffffffffUL)
  5085. /* A regexp token value. */
  5086. struct duk_re_token {
  5087. duk_small_int_t t; /* token type */
  5088. duk_small_int_t greedy;
  5089. duk_uint_fast32_t num; /* numeric value (character, count) */
  5090. duk_uint_fast32_t qmin;
  5091. duk_uint_fast32_t qmax;
  5092. };
  5093. /* A structure for 'snapshotting' a point for rewinding */
  5094. struct duk_lexer_point {
  5095. duk_size_t offset;
  5096. duk_int_t line;
  5097. };
  5098. /* Lexer context. Same context is used for Ecmascript and Regexp parsing. */
  5099. struct duk_lexer_ctx {
  5100. duk_hthread *thr; /* thread; minimizes argument passing */
  5101. const duk_uint8_t *input; /* input string (may be a user pointer) */
  5102. duk_size_t input_length; /* input byte length */
  5103. duk_size_t input_offset; /* input offset for window leading edge (not window[0]) */
  5104. duk_codepoint_t window[DUK_LEXER_WINDOW_SIZE]; /* window of unicode code points */
  5105. duk_size_t offsets[DUK_LEXER_WINDOW_SIZE]; /* input byte offset for each char */
  5106. duk_int_t lines[DUK_LEXER_WINDOW_SIZE]; /* input lines for each char */
  5107. duk_int_t input_line; /* input linenumber at input_offset (not window[0]), init to 1 */
  5108. duk_idx_t slot1_idx; /* valstack slot for 1st token value */
  5109. duk_idx_t slot2_idx; /* valstack slot for 2nd token value */
  5110. duk_idx_t buf_idx; /* valstack slot for temp buffer */
  5111. duk_hbuffer_dynamic *buf; /* temp accumulation buffer (on valstack) */
  5112. duk_int_t token_count; /* number of tokens parsed */
  5113. duk_int_t token_limit; /* maximum token count before error (sanity backstop) */
  5114. };
  5115. /*
  5116. * Prototypes
  5117. */
  5118. DUK_INTERNAL_DECL void duk_lexer_initctx(duk_lexer_ctx *lex_ctx);
  5119. DUK_INTERNAL_DECL void duk_lexer_setpoint(duk_lexer_ctx *lex_ctx, duk_lexer_point *pt);
  5120. DUK_INTERNAL_DECL
  5121. void duk_lexer_parse_js_input_element(duk_lexer_ctx *lex_ctx,
  5122. duk_token *out_token,
  5123. duk_bool_t strict_mode,
  5124. duk_bool_t regexp_mode);
  5125. #ifdef DUK_USE_REGEXP_SUPPORT
  5126. DUK_INTERNAL_DECL void duk_lexer_parse_re_token(duk_lexer_ctx *lex_ctx, duk_re_token *out_token);
  5127. DUK_INTERNAL_DECL void duk_lexer_parse_re_ranges(duk_lexer_ctx *lex_ctx, duk_re_range_callback gen_range, void *userdata);
  5128. #endif /* DUK_USE_REGEXP_SUPPORT */
  5129. #endif /* DUK_LEXER_H_INCLUDED */
  5130. #line 1 "duk_js_compiler.h"
  5131. /*
  5132. * Ecmascript compiler.
  5133. */
  5134. #ifndef DUK_JS_COMPILER_H_INCLUDED
  5135. #define DUK_JS_COMPILER_H_INCLUDED
  5136. /* ecmascript compiler limits */
  5137. #if defined(DUK_USE_DEEP_C_STACK)
  5138. #define DUK_COMPILER_RECURSION_LIMIT 2500L
  5139. #else
  5140. #define DUK_COMPILER_RECURSION_LIMIT 50L
  5141. #endif
  5142. #define DUK_COMPILER_TOKEN_LIMIT 100000000L /* 1e8: protects against deeply nested inner functions */
  5143. /* maximum loopcount for peephole optimization */
  5144. #define DUK_COMPILER_PEEPHOLE_MAXITER 3
  5145. /* maximum bytecode length in instructions */
  5146. #define DUK_COMPILER_MAX_BYTECODE_LENGTH (256L * 1024L * 1024L) /* 1 GB */
  5147. /*
  5148. * Compiler intermediate values
  5149. *
  5150. * Intermediate values describe either plain values (e.g. strings or
  5151. * numbers) or binary operations which have not yet been coerced into
  5152. * either a left-hand-side or right-hand-side role (e.g. object property).
  5153. */
  5154. #define DUK_IVAL_NONE 0 /* no value */
  5155. #define DUK_IVAL_PLAIN 1 /* register, constant, or value */
  5156. #define DUK_IVAL_ARITH 2 /* binary arithmetic; DUK_OP_ADD, DUK_OP_EQ, other binary ops */
  5157. #define DUK_IVAL_ARITH_EXTRAOP 3 /* binary arithmetic using extraops; DUK_EXTRAOP_INSTOF etc */
  5158. #define DUK_IVAL_PROP 4 /* property access */
  5159. #define DUK_IVAL_VAR 5 /* variable access */
  5160. #define DUK_ISPEC_NONE 0 /* no value */
  5161. #define DUK_ISPEC_VALUE 1 /* value resides in 'valstack_idx' */
  5162. #define DUK_ISPEC_REGCONST 2 /* value resides in a register or constant */
  5163. /* bit mask which indicates that a regconst is a constant instead of a register */
  5164. #define DUK_JS_CONST_MARKER 0x80000000UL
  5165. /* type to represent a reg/const reference during compilation */
  5166. typedef duk_uint32_t duk_regconst_t;
  5167. /* type to represent a straight register reference, with <0 indicating none */
  5168. typedef duk_int32_t duk_reg_t;
  5169. typedef struct {
  5170. duk_small_uint_t t; /* DUK_ISPEC_XXX */
  5171. duk_regconst_t regconst;
  5172. duk_idx_t valstack_idx; /* always set; points to a reserved valstack slot */
  5173. } duk_ispec;
  5174. typedef struct {
  5175. /*
  5176. * PLAIN: x1
  5177. * ARITH: x1 <op> x2
  5178. * PROP: x1.x2
  5179. * VAR: x1 (name)
  5180. */
  5181. /* XXX: can be optimized for smaller footprint esp. on 32-bit environments */
  5182. duk_small_uint_t t; /* DUK_IVAL_XXX */
  5183. duk_small_uint_t op; /* bytecode opcode (or extraop) for binary ops */
  5184. duk_ispec x1;
  5185. duk_ispec x2;
  5186. } duk_ivalue;
  5187. /*
  5188. * Bytecode instruction representation during compilation
  5189. *
  5190. * Contains the actual instruction and (optionally) debug info.
  5191. */
  5192. struct duk_compiler_instr {
  5193. duk_instr_t ins;
  5194. #if defined(DUK_USE_PC2LINE)
  5195. duk_uint32_t line;
  5196. #endif
  5197. };
  5198. /*
  5199. * Compiler state
  5200. */
  5201. #define DUK_LABEL_FLAG_ALLOW_BREAK (1 << 0)
  5202. #define DUK_LABEL_FLAG_ALLOW_CONTINUE (1 << 1)
  5203. #define DUK_DECL_TYPE_VAR 0
  5204. #define DUK_DECL_TYPE_FUNC 1
  5205. /* XXX: optimize to 16 bytes */
  5206. typedef struct {
  5207. duk_small_uint_t flags;
  5208. duk_int_t label_id; /* numeric label_id (-1 reserved as marker) */
  5209. duk_hstring *h_label; /* borrowed label name */
  5210. duk_int_t catch_depth; /* catch depth at point of definition */
  5211. duk_int_t pc_label; /* pc of label statement:
  5212. * pc+1: break jump site
  5213. * pc+2: continue jump site
  5214. */
  5215. /* Fast jumps (which avoid longjmp) jump directly to the jump sites
  5216. * which are always known even while the iteration/switch statement
  5217. * is still being parsed. A final peephole pass "straightens out"
  5218. * the jumps.
  5219. */
  5220. } duk_labelinfo;
  5221. /* Compiling state of one function, eventually converted to duk_hcompiledfunction */
  5222. struct duk_compiler_func {
  5223. /* These pointers are at the start of the struct so that they pack
  5224. * nicely. Mixing pointers and integer values is bad on some
  5225. * platforms (e.g. if int is 32 bits and pointers are 64 bits).
  5226. */
  5227. duk_hstring *h_name; /* function name (borrowed reference), ends up in _name */
  5228. duk_hbuffer_dynamic *h_code; /* C array of duk_compiler_instr */
  5229. duk_hobject *h_consts; /* array */
  5230. duk_hobject *h_funcs; /* array of function templates: [func1, offset1, line1, func2, offset2, line2]
  5231. * offset/line points to closing brace to allow skipping on pass 2
  5232. */
  5233. duk_hobject *h_decls; /* array of declarations: [ name1, val1, name2, val2, ... ]
  5234. * valN = (typeN) | (fnum << 8), where fnum is inner func number (0 for vars)
  5235. * record function and variable declarations in pass 1
  5236. */
  5237. duk_hobject *h_labelnames; /* array of active label names */
  5238. duk_hbuffer_dynamic *h_labelinfos; /* C array of duk_labelinfo */
  5239. duk_hobject *h_argnames; /* array of formal argument names (-> _Formals) */
  5240. duk_hobject *h_varmap; /* variable map for pass 2 (identifier -> register number or null (unmapped)) */
  5241. /* value stack indices for tracking objects */
  5242. duk_idx_t code_idx;
  5243. duk_idx_t consts_idx;
  5244. duk_idx_t funcs_idx;
  5245. duk_idx_t decls_idx;
  5246. duk_idx_t labelnames_idx;
  5247. duk_idx_t labelinfos_idx;
  5248. duk_idx_t argnames_idx;
  5249. duk_idx_t varmap_idx;
  5250. /* temp reg handling */
  5251. duk_reg_t temp_first; /* first register that is a temporary (below: variables) */
  5252. duk_reg_t temp_next; /* next temporary register to allocate */
  5253. duk_reg_t temp_max; /* highest value of temp_reg (temp_max - 1 is highest used reg) */
  5254. /* shuffle registers if large number of regs/consts */
  5255. duk_reg_t shuffle1;
  5256. duk_reg_t shuffle2;
  5257. duk_reg_t shuffle3;
  5258. /* stats for current expression being parsed */
  5259. duk_int_t nud_count;
  5260. duk_int_t led_count;
  5261. duk_int_t paren_level; /* parenthesis count, 0 = top level */
  5262. duk_bool_t expr_lhs; /* expression is left-hand-side compatible */
  5263. duk_bool_t allow_in; /* current paren level allows 'in' token */
  5264. /* misc */
  5265. duk_int_t stmt_next; /* statement id allocation (running counter) */
  5266. duk_int_t label_next; /* label id allocation (running counter) */
  5267. duk_int_t catch_depth; /* catch stack depth */
  5268. duk_int_t with_depth; /* with stack depth (affects identifier lookups) */
  5269. duk_int_t fnum_next; /* inner function numbering */
  5270. duk_int_t num_formals; /* number of formal arguments */
  5271. duk_reg_t reg_stmt_value; /* register for writing value of 'non-empty' statements (global or eval code), -1 is marker */
  5272. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  5273. duk_int_t min_line; /* XXX: typing (duk_hcompiledfunction has duk_uint32_t) */
  5274. duk_int_t max_line;
  5275. #endif
  5276. /* status booleans */
  5277. duk_bool_t is_function; /* is an actual function (not global/eval code) */
  5278. duk_bool_t is_eval; /* is eval code */
  5279. duk_bool_t is_global; /* is global code */
  5280. duk_bool_t is_setget; /* is a setter/getter */
  5281. duk_bool_t is_decl; /* is a function declaration (as opposed to function expression) */
  5282. duk_bool_t is_strict; /* function is strict */
  5283. duk_bool_t is_notail; /* function must not be tailcalled */
  5284. duk_bool_t in_directive_prologue; /* parsing in "directive prologue", recognize directives */
  5285. duk_bool_t in_scanning; /* parsing in "scanning" phase (first pass) */
  5286. duk_bool_t may_direct_eval; /* function may call direct eval */
  5287. duk_bool_t id_access_arguments; /* function refers to 'arguments' identifier */
  5288. duk_bool_t id_access_slow; /* function makes one or more slow path accesses */
  5289. duk_bool_t is_arguments_shadowed; /* argument/function declaration shadows 'arguments' */
  5290. duk_bool_t needs_shuffle; /* function needs shuffle registers */
  5291. duk_bool_t reject_regexp_in_adv; /* reject RegExp literal on next advance() call; needed for handling IdentifierName productions */
  5292. };
  5293. struct duk_compiler_ctx {
  5294. duk_hthread *thr;
  5295. /* filename being compiled (ends up in functions' '_filename' property) */
  5296. duk_hstring *h_filename; /* borrowed reference */
  5297. /* lexing (tokenization) state (contains two valstack slot indices) */
  5298. duk_lexer_ctx lex;
  5299. /* current and previous token for parsing */
  5300. duk_token prev_token;
  5301. duk_token curr_token;
  5302. duk_idx_t tok11_idx; /* curr_token slot1 (matches 'lex' slot1_idx) */
  5303. duk_idx_t tok12_idx; /* curr_token slot2 (matches 'lex' slot2_idx) */
  5304. duk_idx_t tok21_idx; /* prev_token slot1 */
  5305. duk_idx_t tok22_idx; /* prev_token slot2 */
  5306. /* recursion limit */
  5307. duk_int_t recursion_depth;
  5308. duk_int_t recursion_limit;
  5309. /* code emission temporary */
  5310. duk_int_t emit_jumpslot_pc;
  5311. /* current function being compiled (embedded instead of pointer for more compact access) */
  5312. duk_compiler_func curr_func;
  5313. };
  5314. /*
  5315. * Prototypes
  5316. */
  5317. #define DUK_JS_COMPILE_FLAG_EVAL (1 << 0) /* source is eval code (not program) */
  5318. #define DUK_JS_COMPILE_FLAG_STRICT (1 << 1) /* strict outer context */
  5319. #define DUK_JS_COMPILE_FLAG_FUNCEXPR (1 << 2) /* source is a function expression (used for Function constructor) */
  5320. 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);
  5321. #endif /* DUK_JS_COMPILER_H_INCLUDED */
  5322. #line 1 "duk_regexp.h"
  5323. /*
  5324. * Regular expression structs, constants, and bytecode defines.
  5325. */
  5326. #ifndef DUK_REGEXP_H_INCLUDED
  5327. #define DUK_REGEXP_H_INCLUDED
  5328. /* maximum bytecode copies for {n,m} quantifiers */
  5329. #define DUK_RE_MAX_ATOM_COPIES 1000
  5330. /* regexp compilation limits */
  5331. #if defined(DUK_USE_DEEP_C_STACK)
  5332. #define DUK_RE_COMPILE_RECURSION_LIMIT 10000
  5333. #else
  5334. #define DUK_RE_COMPILE_RECURSION_LIMIT 100
  5335. #endif
  5336. #define DUK_RE_COMPILE_TOKEN_LIMIT 100000000L /* 1e8 */
  5337. /* regexp execution limits */
  5338. #if defined(DUK_USE_DEEP_C_STACK)
  5339. #define DUK_RE_EXECUTE_RECURSION_LIMIT 10000
  5340. #else
  5341. #define DUK_RE_EXECUTE_RECURSION_LIMIT 100
  5342. #endif
  5343. #define DUK_RE_EXECUTE_STEPS_LIMIT 1000000000L /* 1e9 */
  5344. /* regexp opcodes */
  5345. #define DUK_REOP_MATCH 1
  5346. #define DUK_REOP_CHAR 2
  5347. #define DUK_REOP_PERIOD 3
  5348. #define DUK_REOP_RANGES 4
  5349. #define DUK_REOP_INVRANGES 5
  5350. #define DUK_REOP_JUMP 6
  5351. #define DUK_REOP_SPLIT1 7
  5352. #define DUK_REOP_SPLIT2 8
  5353. #define DUK_REOP_SQMINIMAL 9
  5354. #define DUK_REOP_SQGREEDY 10
  5355. #define DUK_REOP_SAVE 11
  5356. #define DUK_REOP_WIPERANGE 12
  5357. #define DUK_REOP_LOOKPOS 13
  5358. #define DUK_REOP_LOOKNEG 14
  5359. #define DUK_REOP_BACKREFERENCE 15
  5360. #define DUK_REOP_ASSERT_START 16
  5361. #define DUK_REOP_ASSERT_END 17
  5362. #define DUK_REOP_ASSERT_WORD_BOUNDARY 18
  5363. #define DUK_REOP_ASSERT_NOT_WORD_BOUNDARY 19
  5364. /* flags */
  5365. #define DUK_RE_FLAG_GLOBAL (1 << 0)
  5366. #define DUK_RE_FLAG_IGNORE_CASE (1 << 1)
  5367. #define DUK_RE_FLAG_MULTILINE (1 << 2)
  5368. struct duk_re_matcher_ctx {
  5369. duk_hthread *thr;
  5370. duk_uint32_t re_flags;
  5371. const duk_uint8_t *input;
  5372. const duk_uint8_t *input_end;
  5373. const duk_uint8_t *bytecode;
  5374. const duk_uint8_t *bytecode_end;
  5375. const duk_uint8_t **saved; /* allocated from valstack (fixed buffer) */
  5376. duk_uint32_t nsaved;
  5377. duk_uint32_t recursion_depth;
  5378. duk_uint32_t recursion_limit;
  5379. duk_uint32_t steps_count;
  5380. duk_uint32_t steps_limit;
  5381. };
  5382. struct duk_re_compiler_ctx {
  5383. duk_hthread *thr;
  5384. duk_uint32_t re_flags;
  5385. duk_lexer_ctx lex;
  5386. duk_re_token curr_token;
  5387. duk_hbuffer_dynamic *buf;
  5388. duk_uint32_t captures; /* highest capture number emitted so far (used as: ++captures) */
  5389. duk_uint32_t highest_backref;
  5390. duk_uint32_t recursion_depth;
  5391. duk_uint32_t recursion_limit;
  5392. duk_uint32_t nranges; /* internal temporary value, used for char classes */
  5393. };
  5394. /*
  5395. * Prototypes
  5396. */
  5397. DUK_INTERNAL_DECL void duk_regexp_compile(duk_hthread *thr);
  5398. DUK_INTERNAL_DECL void duk_regexp_create_instance(duk_hthread *thr);
  5399. DUK_INTERNAL_DECL void duk_regexp_match(duk_hthread *thr);
  5400. DUK_INTERNAL_DECL void duk_regexp_match_force_global(duk_hthread *thr); /* hacky helper for String.prototype.split() */
  5401. #endif /* DUK_REGEXP_H_INCLUDED */
  5402. #line 1 "duk_tval.h"
  5403. /*
  5404. * Tagged type definition (duk_tval) and accessor macros.
  5405. *
  5406. * Access all fields through the accessor macros, as the representation
  5407. * is quite tricky.
  5408. *
  5409. * There are two packed type alternatives: an 8-byte representation
  5410. * based on an IEEE double (preferred for compactness), and a 12-byte
  5411. * representation (portability). The latter is needed also in e.g.
  5412. * 64-bit environments (it usually pads to 16 bytes per value).
  5413. *
  5414. * Selecting the tagged type format involves many trade-offs (memory
  5415. * use, size and performance of generated code, portability, etc),
  5416. * see doc/types.txt for a detailed discussion (especially of how the
  5417. * IEEE double format is used to pack tagged values).
  5418. *
  5419. * NB: because macro arguments are often expressions, macros should
  5420. * avoid evaluating their argument more than once.
  5421. */
  5422. #ifndef DUK_TVAL_H_INCLUDED
  5423. #define DUK_TVAL_H_INCLUDED
  5424. /* sanity */
  5425. #if !defined(DUK_USE_DOUBLE_LE) && !defined(DUK_USE_DOUBLE_ME) && !defined(DUK_USE_DOUBLE_BE)
  5426. #error unsupported: cannot determine byte order variant
  5427. #endif
  5428. #ifdef DUK_USE_PACKED_TVAL
  5429. /* ======================================================================== */
  5430. /*
  5431. * Packed 8-byte representation
  5432. */
  5433. /* sanity */
  5434. #if !defined(DUK_USE_PACKED_TVAL_POSSIBLE)
  5435. #error packed representation not supported
  5436. #endif
  5437. /* use duk_double_union as duk_tval directly */
  5438. typedef union duk_double_union duk_tval;
  5439. /* tags */
  5440. #define DUK_TAG_NORMALIZED_NAN 0x7ff8UL /* the NaN variant we use */
  5441. /* avoid tag 0xfff0, no risk of confusion with negative infinity */
  5442. #if defined(DUK_USE_FASTINT)
  5443. #define DUK_TAG_FASTINT 0xfff1UL /* embed: integer value */
  5444. #endif
  5445. #define DUK_TAG_UNDEFINED 0xfff2UL /* embed: 0 or 1 (normal or unused) */
  5446. #define DUK_TAG_NULL 0xfff3UL /* embed: nothing */
  5447. #define DUK_TAG_BOOLEAN 0xfff4UL /* embed: 0 or 1 (false or true) */
  5448. /* DUK_TAG_NUMBER would logically go here, but it has multiple 'tags' */
  5449. #define DUK_TAG_POINTER 0xfff5UL /* embed: void ptr */
  5450. #define DUK_TAG_LIGHTFUNC 0xfff6UL /* embed: func ptr */
  5451. #define DUK_TAG_STRING 0xfff7UL /* embed: duk_hstring ptr */
  5452. #define DUK_TAG_OBJECT 0xfff8UL /* embed: duk_hobject ptr */
  5453. #define DUK_TAG_BUFFER 0xfff9UL /* embed: duk_hbuffer ptr */
  5454. /* for convenience */
  5455. #define DUK_XTAG_UNDEFINED_ACTUAL 0xfff20000UL
  5456. #define DUK_XTAG_UNDEFINED_UNUSED 0xfff20001UL
  5457. #define DUK_XTAG_NULL 0xfff30000UL
  5458. #define DUK_XTAG_BOOLEAN_FALSE 0xfff40000UL
  5459. #define DUK_XTAG_BOOLEAN_TRUE 0xfff40001UL
  5460. /* two casts to avoid gcc warning: "warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]" */
  5461. #ifdef DUK_USE_64BIT_OPS
  5462. #ifdef DUK_USE_DOUBLE_ME
  5463. #define DUK__TVAL_SET_TAGGEDPOINTER(v,h,tag) do { \
  5464. (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) (tag)) << 16) | (((duk_uint64_t) (duk_uint32_t) (h)) << 32); \
  5465. } while (0)
  5466. #else
  5467. #define DUK__TVAL_SET_TAGGEDPOINTER(v,h,tag) do { \
  5468. (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) (tag)) << 48) | ((duk_uint64_t) (duk_uint32_t) (h)); \
  5469. } while (0)
  5470. #endif
  5471. #else /* DUK_USE_64BIT_OPS */
  5472. #define DUK__TVAL_SET_TAGGEDPOINTER(v,h,tag) do { \
  5473. (v)->ui[DUK_DBL_IDX_UI0] = ((duk_uint32_t) (tag)) << 16; \
  5474. (v)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (h); \
  5475. } while (0)
  5476. #endif /* DUK_USE_64BIT_OPS */
  5477. #ifdef DUK_USE_64BIT_OPS
  5478. /* Double casting for pointer to avoid gcc warning (cast from pointer to integer of different size) */
  5479. #ifdef DUK_USE_DOUBLE_ME
  5480. #define DUK__TVAL_SET_LIGHTFUNC(v,fp,flags) do { \
  5481. (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_LIGHTFUNC) << 16) | \
  5482. ((duk_uint64_t) (flags)) | \
  5483. (((duk_uint64_t) (duk_uint32_t) (fp)) << 32); \
  5484. } while (0)
  5485. #else
  5486. #define DUK__TVAL_SET_LIGHTFUNC(v,fp,flags) do { \
  5487. (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_LIGHTFUNC) << 48) | \
  5488. (((duk_uint64_t) (flags)) << 32) | \
  5489. ((duk_uint64_t) (duk_uint32_t) (fp)); \
  5490. } while (0)
  5491. #endif
  5492. #else /* DUK_USE_64BIT_OPS */
  5493. #define DUK__TVAL_SET_LIGHTFUNC(v,fp,flags) do { \
  5494. (v)->ui[DUK_DBL_IDX_UI0] = (((duk_uint32_t) DUK_TAG_LIGHTFUNC) << 16) | ((duk_uint32_t) (flags)); \
  5495. (v)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (fp); \
  5496. } while (0)
  5497. #endif /* DUK_USE_64BIT_OPS */
  5498. #if defined(DUK_USE_FASTINT)
  5499. /* Note: masking is done for 'i' to deal with negative numbers correctly */
  5500. #ifdef DUK_USE_DOUBLE_ME
  5501. #define DUK__TVAL_SET_FASTINT(v,i) do { \
  5502. (v)->ui[DUK_DBL_IDX_UI0] = ((duk_uint32_t) DUK_TAG_FASTINT) << 16 | (((duk_uint32_t) ((i) >> 32)) & 0x0000ffffUL); \
  5503. (v)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (i); \
  5504. } while (0)
  5505. #define DUK__TVAL_SET_FASTINT_U32(v,i) do { \
  5506. (v)->ui[DUK_DBL_IDX_UI0] = ((duk_uint32_t) DUK_TAG_FASTINT) << 16; \
  5507. (v)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (i); \
  5508. } while (0)
  5509. #else
  5510. #define DUK__TVAL_SET_FASTINT(v,i) do { \
  5511. (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_FASTINT) << 48) | (((duk_uint64_t) (i)) & 0x0000ffffffffffffULL); \
  5512. } while (0)
  5513. #define DUK__TVAL_SET_FASTINT_U32(v,i) do { \
  5514. (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_FASTINT) << 48) | (duk_uint64_t) (i); \
  5515. } while (0)
  5516. #endif
  5517. #define DUK__TVAL_SET_FASTINT_I32(v,i) do { \
  5518. duk_int64_t duk__tmp = (duk_int64_t) (i); \
  5519. DUK_TVAL_SET_FASTINT((v), duk__tmp); \
  5520. } while (0)
  5521. /* XXX: clumsy sign extend and masking of 16 topmost bits */
  5522. #ifdef DUK_USE_DOUBLE_ME
  5523. #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)
  5524. #else
  5525. #define DUK__TVAL_GET_FASTINT(v) ((((duk_int64_t) (v)->ull[DUK_DBL_IDX_ULL0]) << 16) >> 16)
  5526. #endif
  5527. #define DUK__TVAL_GET_FASTINT_U32(v) ((v)->ui[DUK_DBL_IDX_UI1])
  5528. #define DUK__TVAL_GET_FASTINT_I32(v) ((duk_int32_t) (v)->ui[DUK_DBL_IDX_UI1])
  5529. #endif /* DUK_USE_FASTINT */
  5530. #define DUK_TVAL_SET_UNDEFINED_ACTUAL(v) DUK_DBLUNION_SET_HIGH32((v), DUK_XTAG_UNDEFINED_ACTUAL)
  5531. #define DUK_TVAL_SET_UNDEFINED_UNUSED(v) DUK_DBLUNION_SET_HIGH32((v), DUK_XTAG_UNDEFINED_UNUSED)
  5532. /* Note: 16-bit initializer suffices (unlike for undefined/boolean) */
  5533. #define DUK_TVAL_SET_NULL(v) do { \
  5534. (v)->us[DUK_DBL_IDX_US0] = (duk_uint16_t) DUK_TAG_NULL; \
  5535. } while (0)
  5536. #define DUK_TVAL_SET_BOOLEAN(v,val) DUK_DBLUNION_SET_HIGH32((v), (((duk_uint32_t) DUK_TAG_BOOLEAN) << 16) | ((duk_uint32_t) (val)))
  5537. #define DUK_TVAL_SET_NAN(v) DUK_DBLUNION_SET_NAN_FULL((v))
  5538. /* Assumes that caller has normalized NaNs, otherwise trouble ahead. */
  5539. #if defined(DUK_USE_FASTINT)
  5540. #define DUK_TVAL_SET_DOUBLE(v,d) DUK_DBLUNION_SET_DOUBLE((v), (d))
  5541. #define DUK_TVAL_SET_FASTINT(v,i) DUK__TVAL_SET_FASTINT((v), (i))
  5542. #define DUK_TVAL_SET_FASTINT_I32(v,i) DUK__TVAL_SET_FASTINT_I32((v), (i))
  5543. #define DUK_TVAL_SET_FASTINT_U32(v,i) DUK__TVAL_SET_FASTINT_U32((v), (i))
  5544. #define DUK_TVAL_SET_NUMBER_CHKFAST(v,d) duk_tval_set_number_chkfast((v), (d))
  5545. #define DUK_TVAL_SET_NUMBER(v,d) DUK_DBLUNION_SET_DOUBLE((v), (d))
  5546. #define DUK_TVAL_CHKFAST_INPLACE(v) do { \
  5547. duk_tval *duk__tv; \
  5548. duk_double_t duk__d; \
  5549. duk__tv = (v); \
  5550. if (DUK_TVAL_IS_DOUBLE(duk__tv)) { \
  5551. duk__d = DUK_TVAL_GET_DOUBLE(duk__tv); \
  5552. DUK_TVAL_SET_NUMBER_CHKFAST(duk__tv, duk__d); \
  5553. } \
  5554. } while (0)
  5555. #else
  5556. #define DUK_TVAL_SET_NUMBER(v,d) DUK_DBLUNION_SET_DOUBLE((v), (d))
  5557. #define DUK_TVAL_SET_NUMBER_CHKFAST(v,d) DUK_TVAL_SET_NUMBER((v), (d))
  5558. #define DUK_TVAL_SET_DOUBLE(v,d) DUK_TVAL_SET_NUMBER((v), (d))
  5559. #define DUK_TVAL_CHKFAST_INPLACE(v) do { } while (0)
  5560. #endif
  5561. #define DUK_TVAL_SET_LIGHTFUNC(v,fp,flags) DUK__TVAL_SET_LIGHTFUNC((v), (fp), (flags))
  5562. #define DUK_TVAL_SET_STRING(v,h) DUK__TVAL_SET_TAGGEDPOINTER((v), (h), DUK_TAG_STRING)
  5563. #define DUK_TVAL_SET_OBJECT(v,h) DUK__TVAL_SET_TAGGEDPOINTER((v), (h), DUK_TAG_OBJECT)
  5564. #define DUK_TVAL_SET_BUFFER(v,h) DUK__TVAL_SET_TAGGEDPOINTER((v), (h), DUK_TAG_BUFFER)
  5565. #define DUK_TVAL_SET_POINTER(v,p) DUK__TVAL_SET_TAGGEDPOINTER((v), (p), DUK_TAG_POINTER)
  5566. #define DUK_TVAL_SET_TVAL(v,x) do { *(v) = *(x); } while (0)
  5567. /* getters */
  5568. #define DUK_TVAL_GET_BOOLEAN(v) ((int) (v)->us[DUK_DBL_IDX_US1])
  5569. #if defined(DUK_USE_FASTINT)
  5570. #define DUK_TVAL_GET_DOUBLE(v) ((v)->d)
  5571. #define DUK_TVAL_GET_FASTINT(v) DUK__TVAL_GET_FASTINT((v))
  5572. #define DUK_TVAL_GET_FASTINT_U32(v) DUK__TVAL_GET_FASTINT_U32((v))
  5573. #define DUK_TVAL_GET_FASTINT_I32(v) DUK__TVAL_GET_FASTINT_I32((v))
  5574. #define DUK_TVAL_GET_NUMBER(v) duk_tval_get_number_packed((v))
  5575. #else
  5576. #define DUK_TVAL_GET_NUMBER(v) ((v)->d)
  5577. #define DUK_TVAL_GET_DOUBLE(v) ((v)->d)
  5578. #endif
  5579. #define DUK_TVAL_GET_LIGHTFUNC(v,out_fp,out_flags) do { \
  5580. (out_flags) = (v)->ui[DUK_DBL_IDX_UI0] & 0xffffUL; \
  5581. (out_fp) = (duk_c_function) (v)->ui[DUK_DBL_IDX_UI1]; \
  5582. } while (0)
  5583. #define DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(v) ((duk_c_function) ((v)->ui[DUK_DBL_IDX_UI1]))
  5584. #define DUK_TVAL_GET_LIGHTFUNC_FLAGS(v) (((int) (v)->ui[DUK_DBL_IDX_UI0]) & 0xffffUL)
  5585. #define DUK_TVAL_GET_STRING(v) ((duk_hstring *) (v)->vp[DUK_DBL_IDX_VP1])
  5586. #define DUK_TVAL_GET_OBJECT(v) ((duk_hobject *) (v)->vp[DUK_DBL_IDX_VP1])
  5587. #define DUK_TVAL_GET_BUFFER(v) ((duk_hbuffer *) (v)->vp[DUK_DBL_IDX_VP1])
  5588. #define DUK_TVAL_GET_POINTER(v) ((void *) (v)->vp[DUK_DBL_IDX_VP1])
  5589. #define DUK_TVAL_GET_HEAPHDR(v) ((duk_heaphdr *) (v)->vp[DUK_DBL_IDX_VP1])
  5590. /* decoding */
  5591. #define DUK_TVAL_GET_TAG(v) ((duk_small_uint_t) (v)->us[DUK_DBL_IDX_US0])
  5592. #define DUK_TVAL_IS_UNDEFINED(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_UNDEFINED)
  5593. #define DUK_TVAL_IS_UNDEFINED_ACTUAL(v) ((v)->ui[DUK_DBL_IDX_UI0] == DUK_XTAG_UNDEFINED_ACTUAL)
  5594. #define DUK_TVAL_IS_UNDEFINED_UNUSED(v) ((v)->ui[DUK_DBL_IDX_UI0] == DUK_XTAG_UNDEFINED_UNUSED)
  5595. #define DUK_TVAL_IS_NULL(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_NULL)
  5596. #define DUK_TVAL_IS_BOOLEAN(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_BOOLEAN)
  5597. #define DUK_TVAL_IS_BOOLEAN_TRUE(v) ((v)->ui[DUK_DBL_IDX_UI0] == DUK_XTAG_BOOLEAN_TRUE)
  5598. #define DUK_TVAL_IS_BOOLEAN_FALSE(v) ((v)->ui[DUK_DBL_IDX_UI0] == DUK_XTAG_BOOLEAN_FALSE)
  5599. #define DUK_TVAL_IS_LIGHTFUNC(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_LIGHTFUNC)
  5600. #define DUK_TVAL_IS_STRING(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_STRING)
  5601. #define DUK_TVAL_IS_OBJECT(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_OBJECT)
  5602. #define DUK_TVAL_IS_BUFFER(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_BUFFER)
  5603. #define DUK_TVAL_IS_POINTER(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_POINTER)
  5604. #if defined(DUK_USE_FASTINT)
  5605. /* 0xfff0 is -Infinity */
  5606. #define DUK_TVAL_IS_DOUBLE(v) (DUK_TVAL_GET_TAG((v)) <= 0xfff0UL)
  5607. #define DUK_TVAL_IS_FASTINT(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_FASTINT)
  5608. #define DUK_TVAL_IS_NUMBER(v) (DUK_TVAL_GET_TAG((v)) <= 0xfff1UL)
  5609. #else
  5610. #define DUK_TVAL_IS_NUMBER(v) (DUK_TVAL_GET_TAG((v)) <= 0xfff0UL)
  5611. #define DUK_TVAL_IS_DOUBLE(v) DUK_TVAL_IS_NUMBER((v))
  5612. #endif
  5613. #define DUK_TVAL_IS_HEAP_ALLOCATED(v) (DUK_TVAL_GET_TAG((v)) >= DUK_TAG_STRING)
  5614. #if defined(DUK_USE_FASTINT)
  5615. /* Inlining is only effective in a single file build. */
  5616. DUK_INTERNAL_DECL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_packed(duk_tval *tv);
  5617. #endif
  5618. #else /* DUK_USE_PACKED_TVAL */
  5619. /* ======================================================================== */
  5620. /*
  5621. * Portable 12-byte representation
  5622. */
  5623. /* Note: not initializing all bytes is normally not an issue: Duktape won't
  5624. * read or use the uninitialized bytes so valgrind won't issue warnings.
  5625. * In some special cases a harmless valgrind warning may be issued though.
  5626. * For example, the DumpHeap debugger command writes out a compiled function's
  5627. * 'data' area as is, including any uninitialized bytes, which causes a
  5628. * valgrind warning.
  5629. */
  5630. typedef struct duk_tval_struct duk_tval;
  5631. struct duk_tval_struct {
  5632. duk_small_uint_t t;
  5633. duk_small_uint_t v_extra;
  5634. union {
  5635. duk_double_t d;
  5636. duk_small_int_t i;
  5637. #if defined(DUK_USE_FASTINT)
  5638. duk_int64_t fi; /* if present, forces 16-byte duk_tval */
  5639. #endif
  5640. void *voidptr;
  5641. duk_hstring *hstring;
  5642. duk_hobject *hobject;
  5643. duk_hcompiledfunction *hcompiledfunction;
  5644. duk_hnativefunction *hnativefunction;
  5645. duk_hthread *hthread;
  5646. duk_hbuffer *hbuffer;
  5647. duk_heaphdr *heaphdr;
  5648. duk_c_function lightfunc;
  5649. } v;
  5650. };
  5651. #define DUK__TAG_NUMBER 0 /* not exposed */
  5652. #if defined(DUK_USE_FASTINT)
  5653. #define DUK_TAG_FASTINT 1
  5654. #endif
  5655. #define DUK_TAG_UNDEFINED 2
  5656. #define DUK_TAG_NULL 3
  5657. #define DUK_TAG_BOOLEAN 4
  5658. #define DUK_TAG_POINTER 5
  5659. #define DUK_TAG_LIGHTFUNC 6
  5660. #define DUK_TAG_STRING 7
  5661. #define DUK_TAG_OBJECT 8
  5662. #define DUK_TAG_BUFFER 9
  5663. /* DUK__TAG_NUMBER is intentionally first, as it is the default clause in code
  5664. * to support the 8-byte representation. Further, it is a non-heap-allocated
  5665. * type so it should come before DUK_TAG_STRING. Finally, it should not break
  5666. * the tag value ranges covered by case-clauses in a switch-case.
  5667. */
  5668. /* setters */
  5669. #define DUK_TVAL_SET_UNDEFINED_ACTUAL(tv) do { \
  5670. (tv)->t = DUK_TAG_UNDEFINED; \
  5671. (tv)->v.i = 0; \
  5672. } while (0)
  5673. #define DUK_TVAL_SET_UNDEFINED_UNUSED(tv) do { \
  5674. (tv)->t = DUK_TAG_UNDEFINED; \
  5675. (tv)->v.i = 1; \
  5676. } while (0)
  5677. #define DUK_TVAL_SET_NULL(tv) do { \
  5678. (tv)->t = DUK_TAG_NULL; \
  5679. } while (0)
  5680. #define DUK_TVAL_SET_BOOLEAN(tv,val) do { \
  5681. (tv)->t = DUK_TAG_BOOLEAN; \
  5682. (tv)->v.i = (val); \
  5683. } while (0)
  5684. #if defined(DUK_USE_FASTINT)
  5685. #define DUK_TVAL_SET_DOUBLE(tv,val) do { \
  5686. (tv)->t = DUK__TAG_NUMBER; \
  5687. (tv)->v.d = (val); \
  5688. } while (0)
  5689. #define DUK_TVAL_SET_FASTINT(tv,val) do { \
  5690. (tv)->t = DUK_TAG_FASTINT; \
  5691. (tv)->v.fi = (val); \
  5692. } while (0)
  5693. #define DUK_TVAL_SET_FASTINT_U32(tv,val) do { \
  5694. (tv)->t = DUK_TAG_FASTINT; \
  5695. (tv)->v.fi = (duk_int64_t) (val); \
  5696. } while (0)
  5697. #define DUK_TVAL_SET_FASTINT_I32(tv,val) do { \
  5698. (tv)->t = DUK_TAG_FASTINT; \
  5699. (tv)->v.fi = (duk_int64_t) (val); \
  5700. } while (0)
  5701. #define DUK_TVAL_SET_NUMBER_CHKFAST(tv,d) \
  5702. duk_tval_set_number_chkfast((tv), (d))
  5703. #define DUK_TVAL_SET_NUMBER(tv,val) \
  5704. DUK_TVAL_SET_DOUBLE((tv), (val))
  5705. #define DUK_TVAL_CHKFAST_INPLACE(v) do { \
  5706. duk_tval *duk__tv; \
  5707. duk_double_t duk__d; \
  5708. duk__tv = (v); \
  5709. if (DUK_TVAL_IS_DOUBLE(duk__tv)) { \
  5710. duk__d = DUK_TVAL_GET_DOUBLE(duk__tv); \
  5711. DUK_TVAL_SET_NUMBER_CHKFAST(duk__tv, duk__d); \
  5712. } \
  5713. } while (0)
  5714. #else
  5715. #define DUK_TVAL_SET_NUMBER(tv,val) do { \
  5716. (tv)->t = DUK__TAG_NUMBER; \
  5717. (tv)->v.d = (val); \
  5718. } while (0)
  5719. #define DUK_TVAL_SET_NUMBER_CHKFAST(v,d) \
  5720. DUK_TVAL_SET_NUMBER((tv), (d))
  5721. #define DUK_TVAL_SET_DOUBLE(v,d) \
  5722. DUK_TVAL_SET_NUMBER((tv), (d))
  5723. #define DUK_TVAL_CHKFAST_INPLACE(v) do { } while (0)
  5724. #endif /* DUK_USE_FASTINT */
  5725. #define DUK_TVAL_SET_POINTER(tv,hptr) do { \
  5726. (tv)->t = DUK_TAG_POINTER; \
  5727. (tv)->v.voidptr = (hptr); \
  5728. } while (0)
  5729. #define DUK_TVAL_SET_LIGHTFUNC(tv,fp,flags) do { \
  5730. (tv)->t = DUK_TAG_LIGHTFUNC; \
  5731. (tv)->v_extra = (flags); \
  5732. (tv)->v.lightfunc = (duk_c_function) (fp); \
  5733. } while (0)
  5734. #define DUK_TVAL_SET_STRING(tv,hptr) do { \
  5735. (tv)->t = DUK_TAG_STRING; \
  5736. (tv)->v.hstring = (hptr); \
  5737. } while (0)
  5738. #define DUK_TVAL_SET_OBJECT(tv,hptr) do { \
  5739. (tv)->t = DUK_TAG_OBJECT; \
  5740. (tv)->v.hobject = (hptr); \
  5741. } while (0)
  5742. #define DUK_TVAL_SET_BUFFER(tv,hptr) do { \
  5743. (tv)->t = DUK_TAG_BUFFER; \
  5744. (tv)->v.hbuffer = (hptr); \
  5745. } while (0)
  5746. #define DUK_TVAL_SET_NAN(tv) do { \
  5747. /* in non-packed representation we don't care about which NaN is used */ \
  5748. (tv)->t = DUK__TAG_NUMBER; \
  5749. (tv)->v.d = DUK_DOUBLE_NAN; \
  5750. } while (0)
  5751. #define DUK_TVAL_SET_TVAL(v,x) do { *(v) = *(x); } while (0)
  5752. /* getters */
  5753. #define DUK_TVAL_GET_BOOLEAN(tv) ((tv)->v.i)
  5754. #if defined(DUK_USE_FASTINT)
  5755. #define DUK_TVAL_GET_DOUBLE(tv) ((tv)->v.d)
  5756. #define DUK_TVAL_GET_FASTINT(tv) ((tv)->v.fi)
  5757. #define DUK_TVAL_GET_FASTINT_U32(tv) ((duk_uint32_t) ((tv)->v.fi))
  5758. #define DUK_TVAL_GET_FASTINT_I32(tv) ((duk_int32_t) ((tv)->v.fi))
  5759. #if 0
  5760. #define DUK_TVAL_GET_NUMBER(tv) (DUK_TVAL_IS_FASTINT((tv)) ? \
  5761. (duk_double_t) DUK_TVAL_GET_FASTINT((tv)) : \
  5762. DUK_TVAL_GET_DOUBLE((tv)))
  5763. #define DUK_TVAL_GET_NUMBER(tv) duk_tval_get_number_unpacked((tv))
  5764. #else
  5765. /* This seems reasonable overall. */
  5766. #define DUK_TVAL_GET_NUMBER(tv) (DUK_TVAL_IS_FASTINT((tv)) ? \
  5767. duk_tval_get_number_unpacked_fastint((tv)) : \
  5768. DUK_TVAL_GET_DOUBLE((tv)))
  5769. #endif
  5770. #else
  5771. #define DUK_TVAL_GET_NUMBER(tv) ((tv)->v.d)
  5772. #define DUK_TVAL_GET_DOUBLE(tv) ((tv)->v.d)
  5773. #endif /* DUK_USE_FASTINT */
  5774. #define DUK_TVAL_GET_POINTER(tv) ((tv)->v.voidptr)
  5775. #define DUK_TVAL_GET_LIGHTFUNC(tv,out_fp,out_flags) do { \
  5776. (out_flags) = (duk_uint32_t) (tv)->v_extra; \
  5777. (out_fp) = (tv)->v.lightfunc; \
  5778. } while (0)
  5779. #define DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv) ((tv)->v.lightfunc)
  5780. #define DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv) ((duk_uint32_t) ((tv)->v_extra))
  5781. #define DUK_TVAL_GET_STRING(tv) ((tv)->v.hstring)
  5782. #define DUK_TVAL_GET_OBJECT(tv) ((tv)->v.hobject)
  5783. #define DUK_TVAL_GET_BUFFER(tv) ((tv)->v.hbuffer)
  5784. #define DUK_TVAL_GET_HEAPHDR(tv) ((tv)->v.heaphdr)
  5785. /* decoding */
  5786. #define DUK_TVAL_GET_TAG(tv) ((tv)->t)
  5787. #define DUK_TVAL_IS_UNDEFINED(tv) ((tv)->t == DUK_TAG_UNDEFINED)
  5788. #define DUK_TVAL_IS_UNDEFINED_ACTUAL(tv) (((tv)->t == DUK_TAG_UNDEFINED) && ((tv)->v.i == 0))
  5789. #define DUK_TVAL_IS_UNDEFINED_UNUSED(tv) (((tv)->t == DUK_TAG_UNDEFINED) && ((tv)->v.i != 0))
  5790. #define DUK_TVAL_IS_NULL(tv) ((tv)->t == DUK_TAG_NULL)
  5791. #define DUK_TVAL_IS_BOOLEAN(tv) ((tv)->t == DUK_TAG_BOOLEAN)
  5792. #define DUK_TVAL_IS_BOOLEAN_TRUE(tv) (((tv)->t == DUK_TAG_BOOLEAN) && ((tv)->v.i != 0))
  5793. #define DUK_TVAL_IS_BOOLEAN_FALSE(tv) (((tv)->t == DUK_TAG_BOOLEAN) && ((tv)->v.i == 0))
  5794. #if defined(DUK_USE_FASTINT)
  5795. #define DUK_TVAL_IS_DOUBLE(tv) ((tv)->t == DUK__TAG_NUMBER)
  5796. #define DUK_TVAL_IS_FASTINT(tv) ((tv)->t == DUK_TAG_FASTINT)
  5797. #define DUK_TVAL_IS_NUMBER(tv) ((tv)->t == DUK__TAG_NUMBER || \
  5798. (tv)->t == DUK_TAG_FASTINT)
  5799. #else
  5800. #define DUK_TVAL_IS_NUMBER(tv) ((tv)->t == DUK__TAG_NUMBER)
  5801. #define DUK_TVAL_IS_DOUBLE(v) DUK_TVAL_IS_NUMBER((v))
  5802. #endif /* DUK_USE_FASTINT */
  5803. #define DUK_TVAL_IS_POINTER(tv) ((tv)->t == DUK_TAG_POINTER)
  5804. #define DUK_TVAL_IS_LIGHTFUNC(tv) ((tv)->t == DUK_TAG_LIGHTFUNC)
  5805. #define DUK_TVAL_IS_STRING(tv) ((tv)->t == DUK_TAG_STRING)
  5806. #define DUK_TVAL_IS_OBJECT(tv) ((tv)->t == DUK_TAG_OBJECT)
  5807. #define DUK_TVAL_IS_BUFFER(tv) ((tv)->t == DUK_TAG_BUFFER)
  5808. #define DUK_TVAL_IS_HEAP_ALLOCATED(tv) ((tv)->t >= DUK_TAG_STRING)
  5809. #if defined(DUK_USE_FASTINT)
  5810. /* Inlining is only effective in a single file build. */
  5811. #if 0
  5812. DUK_INTERNAL_DECL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_unpacked(duk_tval *tv);
  5813. #endif
  5814. DUK_INTERNAL_DECL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_unpacked_fastint(duk_tval *tv);
  5815. #endif
  5816. #endif /* DUK_USE_PACKED_TVAL */
  5817. /*
  5818. * Convenience (independent of representation)
  5819. */
  5820. #define DUK_TVAL_SET_BOOLEAN_TRUE(v) DUK_TVAL_SET_BOOLEAN(v, 1)
  5821. #define DUK_TVAL_SET_BOOLEAN_FALSE(v) DUK_TVAL_SET_BOOLEAN(v, 0)
  5822. /* Lightfunc flags packing and unpacking. */
  5823. /* Sign extend: 0x0000##00 -> 0x##000000 -> sign extend to 0xssssss## */
  5824. #define DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags) \
  5825. ((((duk_int32_t) (lf_flags)) << 16) >> 24)
  5826. #define DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags) \
  5827. (((lf_flags) >> 4) & 0x0f)
  5828. #define DUK_LFUNC_FLAGS_GET_NARGS(lf_flags) \
  5829. ((lf_flags) & 0x0f)
  5830. #define DUK_LFUNC_FLAGS_PACK(magic,length,nargs) \
  5831. (((magic) & 0xff) << 8) | ((length) << 4) | (nargs)
  5832. #define DUK_LFUNC_NARGS_VARARGS 0x0f /* varargs marker */
  5833. #define DUK_LFUNC_NARGS_MIN 0x00
  5834. #define DUK_LFUNC_NARGS_MAX 0x0e /* max, excl. varargs marker */
  5835. #define DUK_LFUNC_LENGTH_MIN 0x00
  5836. #define DUK_LFUNC_LENGTH_MAX 0x0f
  5837. #define DUK_LFUNC_MAGIC_MIN (-0x80)
  5838. #define DUK_LFUNC_MAGIC_MAX 0x7f
  5839. /* fastint constants etc */
  5840. #if defined(DUK_USE_FASTINT)
  5841. #define DUK_FASTINT_MIN (-0x800000000000LL)
  5842. #define DUK_FASTINT_MAX 0x7fffffffffffLL
  5843. #define DUK_FASTINT_BITS 48
  5844. DUK_INTERNAL_DECL void duk_tval_set_number_chkfast(duk_tval *tv, duk_double_t x);
  5845. #endif
  5846. #endif /* DUK_TVAL_H_INCLUDED */
  5847. #line 1 "duk_heaphdr.h"
  5848. /*
  5849. * Heap header definition and assorted macros, including ref counting.
  5850. * Access all fields through the accessor macros.
  5851. */
  5852. #ifndef DUK_HEAPHDR_H_INCLUDED
  5853. #define DUK_HEAPHDR_H_INCLUDED
  5854. /*
  5855. * Common heap header
  5856. *
  5857. * All heap objects share the same flags and refcount fields. Objects other
  5858. * than strings also need to have a single or double linked list pointers
  5859. * for insertion into the "heap allocated" list. Strings are held in the
  5860. * heap-wide string table so they don't need link pointers.
  5861. *
  5862. * Technically, 'h_refcount' must be wide enough to guarantee that it cannot
  5863. * wrap (otherwise objects might be freed incorrectly after wrapping). This
  5864. * means essentially that the refcount field must be as wide as data pointers.
  5865. * On 64-bit platforms this means that the refcount needs to be 64 bits even
  5866. * if an 'int' is 32 bits. This is a bit unfortunate, and compromising on
  5867. * this might be reasonable in the future.
  5868. *
  5869. * Heap header size on 32-bit platforms: 8 bytes without reference counting,
  5870. * 16 bytes with reference counting.
  5871. */
  5872. struct duk_heaphdr {
  5873. duk_uint32_t h_flags;
  5874. #if defined(DUK_USE_REFERENCE_COUNTING)
  5875. #if defined(DUK_USE_REFCOUNT16)
  5876. duk_uint16_t h_refcount16;
  5877. #else
  5878. duk_size_t h_refcount;
  5879. #endif
  5880. #endif
  5881. #if defined(DUK_USE_HEAPPTR16)
  5882. duk_uint16_t h_next16;
  5883. #else
  5884. duk_heaphdr *h_next;
  5885. #endif
  5886. #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
  5887. /* refcounting requires direct heap frees, which in turn requires a dual linked heap */
  5888. #if defined(DUK_USE_HEAPPTR16)
  5889. duk_uint16_t h_prev16;
  5890. #else
  5891. duk_heaphdr *h_prev;
  5892. #endif
  5893. #endif
  5894. /* When DUK_USE_HEAPPTR16 (and DUK_USE_REFCOUNT16) is in use, the
  5895. * struct won't align nicely to 4 bytes. This 16-bit extra field
  5896. * is added to make the alignment clean; the field can be used by
  5897. * heap objects when 16-bit packing is used. This field is now
  5898. * conditional to DUK_USE_HEAPPTR16 only, but it is intended to be
  5899. * used with DUK_USE_REFCOUNT16 and DUK_USE_DOUBLE_LINKED_HEAP;
  5900. * this only matter to low memory environments anyway.
  5901. */
  5902. #if defined(DUK_USE_HEAPPTR16)
  5903. duk_uint16_t h_extra16;
  5904. #endif
  5905. };
  5906. struct duk_heaphdr_string {
  5907. /* 16 bits would be enough for shared heaphdr flags and duk_hstring
  5908. * flags. The initial parts of duk_heaphdr_string and duk_heaphdr
  5909. * must match so changing the flags field size here would be quite
  5910. * awkward. However, to minimize struct size, we can pack at least
  5911. * 16 bits of duk_hstring data into the flags field.
  5912. */
  5913. duk_uint32_t h_flags;
  5914. #if defined(DUK_USE_REFERENCE_COUNTING)
  5915. #if defined(DUK_USE_REFCOUNT16)
  5916. duk_uint16_t h_refcount16;
  5917. #else
  5918. duk_size_t h_refcount;
  5919. #endif
  5920. #endif
  5921. };
  5922. #define DUK_HEAPHDR_FLAGS_TYPE_MASK 0x00000003UL
  5923. #define DUK_HEAPHDR_FLAGS_FLAG_MASK (~DUK_HEAPHDR_FLAGS_TYPE_MASK)
  5924. /* 2 bits for heap type */
  5925. #define DUK_HEAPHDR_FLAGS_HEAP_START 2 /* 4 heap flags */
  5926. #define DUK_HEAPHDR_FLAGS_USER_START 6 /* 26 user flags */
  5927. #define DUK_HEAPHDR_HEAP_FLAG_NUMBER(n) (DUK_HEAPHDR_FLAGS_HEAP_START + (n))
  5928. #define DUK_HEAPHDR_USER_FLAG_NUMBER(n) (DUK_HEAPHDR_FLAGS_USER_START + (n))
  5929. #define DUK_HEAPHDR_HEAP_FLAG(n) (1UL << (DUK_HEAPHDR_FLAGS_HEAP_START + (n)))
  5930. #define DUK_HEAPHDR_USER_FLAG(n) (1UL << (DUK_HEAPHDR_FLAGS_USER_START + (n)))
  5931. #define DUK_HEAPHDR_FLAG_REACHABLE DUK_HEAPHDR_HEAP_FLAG(0) /* mark-and-sweep: reachable */
  5932. #define DUK_HEAPHDR_FLAG_TEMPROOT DUK_HEAPHDR_HEAP_FLAG(1) /* mark-and-sweep: children not processed */
  5933. #define DUK_HEAPHDR_FLAG_FINALIZABLE DUK_HEAPHDR_HEAP_FLAG(2) /* mark-and-sweep: finalizable (on current pass) */
  5934. #define DUK_HEAPHDR_FLAG_FINALIZED DUK_HEAPHDR_HEAP_FLAG(3) /* mark-and-sweep: finalized (on previous pass) */
  5935. #define DUK_HTYPE_MIN 1
  5936. #define DUK_HTYPE_STRING 1
  5937. #define DUK_HTYPE_OBJECT 2
  5938. #define DUK_HTYPE_BUFFER 3
  5939. #define DUK_HTYPE_MAX 3
  5940. #if defined(DUK_USE_HEAPPTR16)
  5941. #define DUK_HEAPHDR_GET_NEXT(heap,h) \
  5942. ((duk_heaphdr *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->h_next16))
  5943. #define DUK_HEAPHDR_SET_NEXT(heap,h,val) do { \
  5944. (h)->h_next16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) val); \
  5945. } while (0)
  5946. #else
  5947. #define DUK_HEAPHDR_GET_NEXT(heap,h) ((h)->h_next)
  5948. #define DUK_HEAPHDR_SET_NEXT(heap,h,val) do { \
  5949. (h)->h_next = (val); \
  5950. } while (0)
  5951. #endif
  5952. #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
  5953. #if defined(DUK_USE_HEAPPTR16)
  5954. #define DUK_HEAPHDR_GET_PREV(heap,h) \
  5955. ((duk_heaphdr *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->h_prev16))
  5956. #define DUK_HEAPHDR_SET_PREV(heap,h,val) do { \
  5957. (h)->h_prev16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (val)); \
  5958. } while (0)
  5959. #else
  5960. #define DUK_HEAPHDR_GET_PREV(heap,h) ((h)->h_prev)
  5961. #define DUK_HEAPHDR_SET_PREV(heap,h,val) do { \
  5962. (h)->h_prev = (val); \
  5963. } while (0)
  5964. #endif
  5965. #endif
  5966. #if defined(DUK_USE_REFERENCE_COUNTING)
  5967. #if defined(DUK_USE_REFCOUNT16)
  5968. #define DUK_HEAPHDR_GET_REFCOUNT(h) ((h)->h_refcount16)
  5969. #define DUK_HEAPHDR_SET_REFCOUNT(h,val) do { \
  5970. (h)->h_refcount16 = (val); \
  5971. } while (0)
  5972. #define DUK_HEAPHDR_PREINC_REFCOUNT(h) (++(h)->h_refcount16) /* result: updated refcount */
  5973. #define DUK_HEAPHDR_PREDEC_REFCOUNT(h) (--(h)->h_refcount16) /* result: updated refcount */
  5974. #else
  5975. #define DUK_HEAPHDR_GET_REFCOUNT(h) ((h)->h_refcount)
  5976. #define DUK_HEAPHDR_SET_REFCOUNT(h,val) do { \
  5977. (h)->h_refcount = (val); \
  5978. } while (0)
  5979. #define DUK_HEAPHDR_PREINC_REFCOUNT(h) (++(h)->h_refcount) /* result: updated refcount */
  5980. #define DUK_HEAPHDR_PREDEC_REFCOUNT(h) (--(h)->h_refcount) /* result: updated refcount */
  5981. #endif
  5982. #else
  5983. /* refcount macros not defined without refcounting, caller must #ifdef now */
  5984. #endif /* DUK_USE_REFERENCE_COUNTING */
  5985. /*
  5986. * Note: type is treated as a field separate from flags, so some masking is
  5987. * involved in the macros below.
  5988. */
  5989. #define DUK_HEAPHDR_GET_FLAGS_RAW(h) ((h)->h_flags)
  5990. #define DUK_HEAPHDR_GET_FLAGS(h) ((h)->h_flags & DUK_HEAPHDR_FLAGS_FLAG_MASK)
  5991. #define DUK_HEAPHDR_SET_FLAGS(h,val) do { \
  5992. (h)->h_flags = ((h)->h_flags & ~(DUK_HEAPHDR_FLAGS_FLAG_MASK)) | (val); \
  5993. } while (0)
  5994. #define DUK_HEAPHDR_GET_TYPE(h) ((h)->h_flags & DUK_HEAPHDR_FLAGS_TYPE_MASK)
  5995. #define DUK_HEAPHDR_SET_TYPE(h,val) do { \
  5996. (h)->h_flags = ((h)->h_flags & ~(DUK_HEAPHDR_FLAGS_TYPE_MASK)) | (val); \
  5997. } while (0)
  5998. #define DUK_HEAPHDR_HTYPE_VALID(h) ( \
  5999. DUK_HEAPHDR_GET_TYPE((h)) >= DUK_HTYPE_MIN && \
  6000. DUK_HEAPHDR_GET_TYPE((h)) <= DUK_HTYPE_MAX \
  6001. )
  6002. #define DUK_HEAPHDR_SET_TYPE_AND_FLAGS(h,tval,fval) do { \
  6003. (h)->h_flags = ((tval) & DUK_HEAPHDR_FLAGS_TYPE_MASK) | \
  6004. ((fval) & DUK_HEAPHDR_FLAGS_FLAG_MASK); \
  6005. } while (0)
  6006. #define DUK_HEAPHDR_SET_FLAG_BITS(h,bits) do { \
  6007. DUK_ASSERT(((bits) & ~(DUK_HEAPHDR_FLAGS_FLAG_MASK)) == 0); \
  6008. (h)->h_flags |= (bits); \
  6009. } while (0)
  6010. #define DUK_HEAPHDR_CLEAR_FLAG_BITS(h,bits) do { \
  6011. DUK_ASSERT(((bits) & ~(DUK_HEAPHDR_FLAGS_FLAG_MASK)) == 0); \
  6012. (h)->h_flags &= ~((bits)); \
  6013. } while (0)
  6014. #define DUK_HEAPHDR_CHECK_FLAG_BITS(h,bits) (((h)->h_flags & (bits)) != 0)
  6015. #define DUK_HEAPHDR_SET_REACHABLE(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_REACHABLE)
  6016. #define DUK_HEAPHDR_CLEAR_REACHABLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_REACHABLE)
  6017. #define DUK_HEAPHDR_HAS_REACHABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_REACHABLE)
  6018. #define DUK_HEAPHDR_SET_TEMPROOT(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_TEMPROOT)
  6019. #define DUK_HEAPHDR_CLEAR_TEMPROOT(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_TEMPROOT)
  6020. #define DUK_HEAPHDR_HAS_TEMPROOT(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_TEMPROOT)
  6021. #define DUK_HEAPHDR_SET_FINALIZABLE(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZABLE)
  6022. #define DUK_HEAPHDR_CLEAR_FINALIZABLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZABLE)
  6023. #define DUK_HEAPHDR_HAS_FINALIZABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZABLE)
  6024. #define DUK_HEAPHDR_SET_FINALIZED(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZED)
  6025. #define DUK_HEAPHDR_CLEAR_FINALIZED(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZED)
  6026. #define DUK_HEAPHDR_HAS_FINALIZED(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZED)
  6027. /* get or set a range of flags; m=first bit number, n=number of bits */
  6028. #define DUK_HEAPHDR_GET_FLAG_RANGE(h,m,n) (((h)->h_flags >> (m)) & ((1UL << (n)) - 1UL))
  6029. #define DUK_HEAPHDR_SET_FLAG_RANGE(h,m,n,v) do { \
  6030. (h)->h_flags = \
  6031. ((h)->h_flags & (~(((1 << (n)) - 1) << (m)))) \
  6032. | ((v) << (m)); \
  6033. } while (0)
  6034. /* init pointer fields to null */
  6035. #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
  6036. #define DUK_HEAPHDR_INIT_NULLS(h) do { \
  6037. DUK_HEAPHDR_SET_NEXT((h), (void *) NULL); \
  6038. DUK_HEAPHDR_SET_PREV((h), (void *) NULL); \
  6039. } while (0)
  6040. #else
  6041. #define DUK_HEAPHDR_INIT_NULLS(h) do { \
  6042. DUK_HEAPHDR_SET_NEXT((h), (void *) NULL); \
  6043. } while (0)
  6044. #endif
  6045. #define DUK_HEAPHDR_STRING_INIT_NULLS(h) /* currently nop */
  6046. /*
  6047. * Reference counting helper macros. The macros take a thread argument
  6048. * and must thus always be executed in a specific thread context. The
  6049. * thread argument is needed for features like finalization. Currently
  6050. * it is not required for INCREF, but it is included just in case.
  6051. *
  6052. * Note that 'raw' macros such as DUK_HEAPHDR_GET_REFCOUNT() are not
  6053. * defined without DUK_USE_REFERENCE_COUNTING, so caller must #ifdef
  6054. * around them.
  6055. */
  6056. #if defined(DUK_USE_REFERENCE_COUNTING)
  6057. /* Fast variants, inline refcount operations except for refzero handling.
  6058. * Can be used explicitly when speed is always more important than size.
  6059. * For a good compiler and a single file build, these are basically the
  6060. * same as a forced inline.
  6061. */
  6062. #define DUK_TVAL_INCREF_FAST(thr,tv) do { \
  6063. duk_tval *duk__tv = (tv); \
  6064. DUK_ASSERT(duk__tv != NULL); \
  6065. if (DUK_TVAL_IS_HEAP_ALLOCATED(duk__tv)) { \
  6066. duk_heaphdr *duk__h = DUK_TVAL_GET_HEAPHDR(duk__tv); \
  6067. DUK_ASSERT(duk__h != NULL); \
  6068. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \
  6069. DUK_HEAPHDR_PREINC_REFCOUNT(duk__h); \
  6070. } \
  6071. } while (0)
  6072. #define DUK_TVAL_DECREF_FAST(thr,tv) do { \
  6073. duk_tval *duk__tv = (tv); \
  6074. DUK_ASSERT(duk__tv != NULL); \
  6075. if (DUK_TVAL_IS_HEAP_ALLOCATED(duk__tv)) { \
  6076. duk_heaphdr *duk__h = DUK_TVAL_GET_HEAPHDR(duk__tv); \
  6077. DUK_ASSERT(duk__h != NULL); \
  6078. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \
  6079. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(duk__h) > 0); \
  6080. if (DUK_HEAPHDR_PREDEC_REFCOUNT(duk__h) == 0) { \
  6081. duk_heaphdr_refzero((thr), duk__h); \
  6082. } \
  6083. } \
  6084. } while (0)
  6085. #define DUK_HEAPHDR_INCREF_FAST(thr,h) do { \
  6086. duk_heaphdr *duk__h = (duk_heaphdr *) (h); \
  6087. DUK_ASSERT(duk__h != NULL); \
  6088. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \
  6089. DUK_HEAPHDR_PREINC_REFCOUNT(duk__h); \
  6090. } while (0)
  6091. #define DUK_HEAPHDR_DECREF_FAST(thr,h) do { \
  6092. duk_heaphdr *duk__h = (duk_heaphdr *) (h); \
  6093. DUK_ASSERT(duk__h != NULL); \
  6094. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \
  6095. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(duk__h) > 0); \
  6096. if (DUK_HEAPHDR_PREDEC_REFCOUNT(duk__h) == 0) { \
  6097. duk_heaphdr_refzero((thr), duk__h); \
  6098. } \
  6099. } while (0)
  6100. /* Slow variants, call to a helper to reduce code size.
  6101. * Can be used explicitly when size is always more important than speed.
  6102. */
  6103. #define DUK_TVAL_INCREF_SLOW(thr,tv) do { \
  6104. duk_tval_incref((tv)); \
  6105. } while (0)
  6106. #define DUK_TVAL_DECREF_SLOW(thr,tv) do { \
  6107. duk_tval_decref((thr), (tv)); \
  6108. } while (0)
  6109. #define DUK_HEAPHDR_INCREF_SLOW(thr,h) do { \
  6110. duk_heaphdr_incref((duk_heaphdr *) (h)); \
  6111. } while (0)
  6112. #define DUK_HEAPHDR_DECREF_SLOW(thr,h) do { \
  6113. duk_heaphdr_decref((thr), (duk_heaphdr *) (h)); \
  6114. } while (0)
  6115. /* Default variants. Selection depends on speed/size preference.
  6116. * Concretely: with gcc 4.8.1 -Os x64 the difference in final binary
  6117. * is about +1kB for _FAST variants.
  6118. */
  6119. #if defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  6120. #define DUK_TVAL_INCREF(thr,tv) DUK_TVAL_INCREF_FAST((thr),(tv))
  6121. #define DUK_TVAL_DECREF(thr,tv) DUK_TVAL_DECREF_FAST((thr),(tv))
  6122. #define DUK_HEAPHDR_INCREF(thr,h) DUK_HEAPHDR_INCREF_FAST((thr),(h))
  6123. #define DUK_HEAPHDR_DECREF(thr,h) DUK_HEAPHDR_DECREF_FAST((thr),(h))
  6124. #else
  6125. #define DUK_TVAL_INCREF(thr,tv) DUK_TVAL_INCREF_SLOW((thr),(tv))
  6126. #define DUK_TVAL_DECREF(thr,tv) DUK_TVAL_DECREF_SLOW((thr),(tv))
  6127. #define DUK_HEAPHDR_INCREF(thr,h) DUK_HEAPHDR_INCREF_SLOW((thr),(h))
  6128. #define DUK_HEAPHDR_DECREF(thr,h) DUK_HEAPHDR_DECREF_SLOW((thr),(h))
  6129. #endif
  6130. /* Casting convenience. */
  6131. #define DUK_HSTRING_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h))
  6132. #define DUK_HSTRING_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) (h))
  6133. #define DUK_HOBJECT_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h))
  6134. #define DUK_HOBJECT_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) (h))
  6135. #define DUK_HBUFFER_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h))
  6136. #define DUK_HBUFFER_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) (h))
  6137. #define DUK_HCOMPILEDFUNCTION_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj)
  6138. #define DUK_HCOMPILEDFUNCTION_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) &(h)->obj)
  6139. #define DUK_HNATIVEFUNCTION_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj)
  6140. #define DUK_HNATIVEFUNCTION_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) &(h)->obj)
  6141. #define DUK_HBUFFEROBJECT_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj)
  6142. #define DUK_HBUFFEROBJECT_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) &(h)->obj)
  6143. #define DUK_HTHREAD_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj)
  6144. #define DUK_HTHREAD_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) &(h)->obj)
  6145. /* Convenience for some situations; the above macros don't allow NULLs
  6146. * for performance reasons.
  6147. */
  6148. #define DUK_HOBJECT_INCREF_ALLOWNULL(thr,h) do { \
  6149. if ((h) != NULL) { \
  6150. DUK_HEAPHDR_INCREF((thr), (duk_heaphdr *) (h)); \
  6151. } \
  6152. } while (0)
  6153. #define DUK_HOBJECT_DECREF_ALLOWNULL(thr,h) do { \
  6154. if ((h) != NULL) { \
  6155. DUK_HEAPHDR_DECREF((thr), (duk_heaphdr *) (h)); \
  6156. } \
  6157. } while (0)
  6158. #else /* DUK_USE_REFERENCE_COUNTING */
  6159. #define DUK_TVAL_INCREF_FAST(thr,v) do {} while (0) /* nop */
  6160. #define DUK_TVAL_DECREF_FAST(thr,v) do {} while (0) /* nop */
  6161. #define DUK_TVAL_INCREF_SLOW(thr,v) do {} while (0) /* nop */
  6162. #define DUK_TVAL_DECREF_SLOW(thr,v) do {} while (0) /* nop */
  6163. #define DUK_TVAL_INCREF(thr,v) do {} while (0) /* nop */
  6164. #define DUK_TVAL_DECREF(thr,v) do {} while (0) /* nop */
  6165. #define DUK_HEAPHDR_INCREF_FAST(thr,h) do {} while (0) /* nop */
  6166. #define DUK_HEAPHDR_DECREF_FAST(thr,h) do {} while (0) /* nop */
  6167. #define DUK_HEAPHDR_INCREF_SLOW(thr,h) do {} while (0) /* nop */
  6168. #define DUK_HEAPHDR_DECREF_SLOW(thr,h) do {} while (0) /* nop */
  6169. #define DUK_HEAPHDR_INCREF(thr,h) do {} while (0) /* nop */
  6170. #define DUK_HEAPHDR_DECREF(thr,h) do {} while (0) /* nop */
  6171. #define DUK_HSTRING_INCREF(thr,h) do {} while (0) /* nop */
  6172. #define DUK_HSTRING_DECREF(thr,h) do {} while (0) /* nop */
  6173. #define DUK_HOBJECT_INCREF(thr,h) do {} while (0) /* nop */
  6174. #define DUK_HOBJECT_DECREF(thr,h) do {} while (0) /* nop */
  6175. #define DUK_HBUFFER_INCREF(thr,h) do {} while (0) /* nop */
  6176. #define DUK_HBUFFER_DECREF(thr,h) do {} while (0) /* nop */
  6177. #define DUK_HCOMPILEDFUNCTION_INCREF(thr,h) do {} while (0) /* nop */
  6178. #define DUK_HCOMPILEDFUNCTION_DECREF(thr,h) do {} while (0) /* nop */
  6179. #define DUK_HNATIVEFUNCTION_INCREF(thr,h) do {} while (0) /* nop */
  6180. #define DUK_HNATIVEFUNCTION_DECREF(thr,h) do {} while (0) /* nop */
  6181. #define DUK_HBUFFEROBJECT_INCREF(thr,h) do {} while (0) /* nop */
  6182. #define DUK_HBUFFEROBJECT_DECREF(thr,h) do {} while (0) /* nop */
  6183. #define DUK_HTHREAD_INCREF(thr,h) do {} while (0) /* nop */
  6184. #define DUK_HTHREAD_DECREF(thr,h) do {} while (0) /* nop */
  6185. #define DUK_HOBJECT_INCREF_ALLOWNULL(thr,h) do {} while (0) /* nop */
  6186. #define DUK_HOBJECT_DECREF_ALLOWNULL(thr,h) do {} while (0) /* nop */
  6187. #endif /* DUK_USE_REFERENCE_COUNTING */
  6188. #endif /* DUK_HEAPHDR_H_INCLUDED */
  6189. #line 1 "duk_api_internal.h"
  6190. /*
  6191. * Internal API calls which have (stack and other) semantics similar
  6192. * to the public API.
  6193. */
  6194. #ifndef DUK_API_INTERNAL_H_INCLUDED
  6195. #define DUK_API_INTERNAL_H_INCLUDED
  6196. /* duk_push_sprintf constants */
  6197. #define DUK_PUSH_SPRINTF_INITIAL_SIZE 256L
  6198. #define DUK_PUSH_SPRINTF_SANITY_LIMIT (1L * 1024L * 1024L * 1024L)
  6199. /* Flag ORed to err_code to indicate __FILE__ / __LINE__ is not
  6200. * blamed as source of error for error fileName / lineNumber.
  6201. */
  6202. #define DUK_ERRCODE_FLAG_NOBLAME_FILELINE (1L << 24)
  6203. /* Valstack resize flags */
  6204. #define DUK_VSRESIZE_FLAG_SHRINK (1 << 0)
  6205. #define DUK_VSRESIZE_FLAG_COMPACT (1 << 1)
  6206. #define DUK_VSRESIZE_FLAG_THROW (1 << 2)
  6207. /* Current convention is to use duk_size_t for value stack sizes and global indices,
  6208. * and duk_idx_t for local frame indices.
  6209. */
  6210. DUK_INTERNAL_DECL
  6211. duk_bool_t duk_valstack_resize_raw(duk_context *ctx,
  6212. duk_size_t min_new_size,
  6213. duk_small_uint_t flags);
  6214. DUK_INTERNAL_DECL duk_tval *duk_get_tval(duk_context *ctx, duk_idx_t index);
  6215. DUK_INTERNAL_DECL duk_tval *duk_require_tval(duk_context *ctx, duk_idx_t index);
  6216. DUK_INTERNAL_DECL void duk_push_tval(duk_context *ctx, duk_tval *tv);
  6217. /* Push the current 'this' binding; throw TypeError if binding is not object
  6218. * coercible (CheckObjectCoercible).
  6219. */
  6220. DUK_INTERNAL_DECL void duk_push_this_check_object_coercible(duk_context *ctx);
  6221. /* duk_push_this() + CheckObjectCoercible() + duk_to_object() */
  6222. DUK_INTERNAL_DECL duk_hobject *duk_push_this_coercible_to_object(duk_context *ctx);
  6223. /* duk_push_this() + CheckObjectCoercible() + duk_to_string() */
  6224. DUK_INTERNAL_DECL duk_hstring *duk_push_this_coercible_to_string(duk_context *ctx);
  6225. /* Get a borrowed duk_tval pointer to the current 'this' binding. Caller must
  6226. * make sure there's an active callstack entry. Note that the returned pointer
  6227. * is unstable with regards to side effects.
  6228. */
  6229. DUK_INTERNAL_DECL duk_tval *duk_get_borrowed_this_tval(duk_context *ctx);
  6230. /* XXX: add fastint support? */
  6231. #define duk_push_u64(ctx,val) \
  6232. duk_push_number((ctx), (duk_double_t) (val))
  6233. #define duk_push_i64(ctx,val) \
  6234. duk_push_number((ctx), (duk_double_t) (val))
  6235. /* duk_push_(u)int() is guaranteed to support at least (un)signed 32-bit range */
  6236. #define duk_push_u32(ctx,val) \
  6237. duk_push_uint((ctx), (duk_uint_t) (val))
  6238. #define duk_push_i32(ctx,val) \
  6239. duk_push_int((ctx), (duk_int_t) (val))
  6240. /* sometimes stack and array indices need to go on the stack */
  6241. #define duk_push_idx(ctx,val) \
  6242. duk_push_int((ctx), (duk_int_t) (val))
  6243. #define duk_push_uarridx(ctx,val) \
  6244. duk_push_uint((ctx), (duk_uint_t) (val))
  6245. #define duk_push_size_t(ctx,val) \
  6246. duk_push_uint((ctx), (duk_uint_t) (val)) /* XXX: assumed to fit for now */
  6247. /* internal helper for looking up a tagged type */
  6248. #define DUK_GETTAGGED_FLAG_ALLOW_NULL (1L << 24)
  6249. #define DUK_GETTAGGED_FLAG_CHECK_CLASS (1L << 25)
  6250. #define DUK_GETTAGGED_CLASS_SHIFT 16
  6251. DUK_INTERNAL_DECL duk_heaphdr *duk_get_tagged_heaphdr_raw(duk_context *ctx, duk_idx_t index, duk_uint_t flags_and_tag);
  6252. DUK_INTERNAL_DECL duk_hstring *duk_get_hstring(duk_context *ctx, duk_idx_t index);
  6253. DUK_INTERNAL_DECL duk_hobject *duk_get_hobject(duk_context *ctx, duk_idx_t index);
  6254. DUK_INTERNAL_DECL duk_hbuffer *duk_get_hbuffer(duk_context *ctx, duk_idx_t index);
  6255. DUK_INTERNAL_DECL duk_hthread *duk_get_hthread(duk_context *ctx, duk_idx_t index);
  6256. DUK_INTERNAL_DECL duk_hcompiledfunction *duk_get_hcompiledfunction(duk_context *ctx, duk_idx_t index);
  6257. DUK_INTERNAL_DECL duk_hnativefunction *duk_get_hnativefunction(duk_context *ctx, duk_idx_t index);
  6258. #define duk_get_hobject_with_class(ctx,index,classnum) \
  6259. ((duk_hobject *) duk_get_tagged_heaphdr_raw((ctx), (index), \
  6260. DUK_TAG_OBJECT | DUK_GETTAGGED_FLAG_ALLOW_NULL | \
  6261. DUK_GETTAGGED_FLAG_CHECK_CLASS | ((classnum) << DUK_GETTAGGED_CLASS_SHIFT)))
  6262. #if 0 /* This would be pointless: unexpected type and lightfunc would both return NULL */
  6263. DUK_INTERNAL_DECL duk_hobject *duk_get_hobject_or_lfunc(duk_context *ctx, duk_idx_t index);
  6264. #endif
  6265. DUK_INTERNAL_DECL duk_hobject *duk_get_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index);
  6266. #if 0 /*unused*/
  6267. DUK_INTERNAL_DECL void *duk_get_voidptr(duk_context *ctx, duk_idx_t index);
  6268. #endif
  6269. DUK_INTERNAL_DECL duk_hstring *duk_to_hstring(duk_context *ctx, duk_idx_t index);
  6270. 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 */
  6271. 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);
  6272. 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);
  6273. DUK_INTERNAL_DECL duk_uint8_t duk_to_uint8clamped(duk_context *ctx, duk_idx_t index);
  6274. DUK_INTERNAL_DECL duk_hstring *duk_require_hstring(duk_context *ctx, duk_idx_t index);
  6275. DUK_INTERNAL_DECL duk_hobject *duk_require_hobject(duk_context *ctx, duk_idx_t index);
  6276. DUK_INTERNAL_DECL duk_hbuffer *duk_require_hbuffer(duk_context *ctx, duk_idx_t index);
  6277. DUK_INTERNAL_DECL duk_hthread *duk_require_hthread(duk_context *ctx, duk_idx_t index);
  6278. #if 0 /*unused */
  6279. DUK_INTERNAL_DECL duk_hcompiledfunction *duk_require_hcompiledfunction(duk_context *ctx, duk_idx_t index);
  6280. #endif
  6281. DUK_INTERNAL_DECL duk_hnativefunction *duk_require_hnativefunction(duk_context *ctx, duk_idx_t index);
  6282. #define duk_require_hobject_with_class(ctx,index,classnum) \
  6283. ((duk_hobject *) duk_get_tagged_heaphdr_raw((ctx), (index), \
  6284. DUK_TAG_OBJECT | \
  6285. DUK_GETTAGGED_FLAG_CHECK_CLASS | ((classnum) << DUK_GETTAGGED_CLASS_SHIFT)))
  6286. DUK_INTERNAL_DECL duk_hobject *duk_require_hobject_or_lfunc(duk_context *ctx, duk_idx_t index);
  6287. DUK_INTERNAL_DECL duk_hobject *duk_require_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index);
  6288. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  6289. DUK_INTERNAL_DECL void duk_push_unused(duk_context *ctx);
  6290. #endif
  6291. DUK_INTERNAL_DECL void duk_push_hstring(duk_context *ctx, duk_hstring *h);
  6292. DUK_INTERNAL_DECL void duk_push_hstring_stridx(duk_context *ctx, duk_small_int_t stridx);
  6293. DUK_INTERNAL_DECL void duk_push_hobject(duk_context *ctx, duk_hobject *h);
  6294. DUK_INTERNAL_DECL void duk_push_hbuffer(duk_context *ctx, duk_hbuffer *h);
  6295. #define duk_push_hthread(ctx,h) \
  6296. duk_push_hobject((ctx), (duk_hobject *) (h))
  6297. #define duk_push_hcompiledfunction(ctx,h) \
  6298. duk_push_hobject((ctx), (duk_hobject *) (h))
  6299. #define duk_push_hnativefunction(ctx,h) \
  6300. duk_push_hobject((ctx), (duk_hobject *) (h))
  6301. DUK_INTERNAL_DECL void duk_push_hobject_bidx(duk_context *ctx, duk_small_int_t builtin_idx);
  6302. 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);
  6303. DUK_INTERNAL_DECL duk_idx_t duk_push_object_helper_proto(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_hobject *proto);
  6304. DUK_INTERNAL_DECL duk_idx_t duk_push_object_internal(duk_context *ctx);
  6305. DUK_INTERNAL_DECL duk_idx_t duk_push_compiledfunction(duk_context *ctx);
  6306. DUK_INTERNAL_DECL void duk_push_c_function_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs);
  6307. DUK_INTERNAL_DECL void duk_push_c_function_noconstruct_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs);
  6308. DUK_INTERNAL_DECL void duk_push_string_funcptr(duk_context *ctx, duk_uint8_t *ptr, duk_size_t sz);
  6309. DUK_INTERNAL_DECL void duk_push_lightfunc_name(duk_context *ctx, duk_tval *tv);
  6310. DUK_INTERNAL_DECL void duk_push_lightfunc_tostring(duk_context *ctx, duk_tval *tv);
  6311. DUK_INTERNAL_DECL duk_hbufferobject *duk_push_bufferobject(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx);
  6312. DUK_INTERNAL_DECL duk_bool_t duk_get_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx); /* [] -> [val] */
  6313. DUK_INTERNAL_DECL duk_bool_t duk_put_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx); /* [val] -> [] */
  6314. DUK_INTERNAL_DECL duk_bool_t duk_del_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx); /* [] -> [] */
  6315. DUK_INTERNAL_DECL duk_bool_t duk_has_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx); /* [] -> [] */
  6316. 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); /* [] -> [] */
  6317. DUK_INTERNAL_DECL void duk_xdef_prop(duk_context *ctx, duk_idx_t obj_index, duk_small_uint_t desc_flags); /* [key val] -> [] */
  6318. 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] -> [] */
  6319. 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] -> [] */
  6320. 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); /* [] -> [] */
  6321. 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); /* [] -> [] */
  6322. /* These are macros for now, but could be separate functions to reduce code
  6323. * footprint (check call site count before refactoring).
  6324. */
  6325. #define duk_xdef_prop_wec(ctx,obj_index) \
  6326. duk_xdef_prop((ctx), (obj_index), DUK_PROPDESC_FLAGS_WEC)
  6327. #define duk_xdef_prop_index_wec(ctx,obj_index,arr_index) \
  6328. duk_xdef_prop_index((ctx), (obj_index), (arr_index), DUK_PROPDESC_FLAGS_WEC)
  6329. #define duk_xdef_prop_stridx_wec(ctx,obj_index,stridx) \
  6330. duk_xdef_prop_stridx((ctx), (obj_index), (stridx), DUK_PROPDESC_FLAGS_WEC)
  6331. /* Set object 'length'. */
  6332. DUK_INTERNAL_DECL void duk_set_length(duk_context *ctx, duk_idx_t index, duk_size_t length);
  6333. #endif /* DUK_API_INTERNAL_H_INCLUDED */
  6334. #line 1 "duk_hstring.h"
  6335. /*
  6336. * Heap string representation.
  6337. *
  6338. * Strings are byte sequences ordinarily stored in extended UTF-8 format,
  6339. * allowing values larger than the official UTF-8 range (used internally)
  6340. * and also allowing UTF-8 encoding of surrogate pairs (CESU-8 format).
  6341. * Strings may also be invalid UTF-8 altogether which is the case e.g. with
  6342. * strings used as internal property names and raw buffers converted to
  6343. * strings. In such cases the 'clen' field contains an inaccurate value.
  6344. *
  6345. * Ecmascript requires support for 32-bit long strings. However, since each
  6346. * 16-bit codepoint can take 3 bytes in CESU-8, this representation can only
  6347. * support about 1.4G codepoint long strings in extreme cases. This is not
  6348. * really a practical issue.
  6349. */
  6350. #ifndef DUK_HSTRING_H_INCLUDED
  6351. #define DUK_HSTRING_H_INCLUDED
  6352. /* Impose a maximum string length for now. Restricted artificially to
  6353. * ensure adding a heap header length won't overflow size_t. The limit
  6354. * should be synchronized with DUK_HBUFFER_MAX_BYTELEN.
  6355. *
  6356. * E5.1 makes provisions to support strings longer than 4G characters.
  6357. * This limit should be eliminated on 64-bit platforms (and increased
  6358. * closer to maximum support on 32-bit platforms).
  6359. */
  6360. #if defined(DUK_USE_STRLEN16)
  6361. #define DUK_HSTRING_MAX_BYTELEN (0x0000ffffUL)
  6362. #else
  6363. #define DUK_HSTRING_MAX_BYTELEN (0x7fffffffUL)
  6364. #endif
  6365. /* XXX: could add flags for "is valid CESU-8" (Ecmascript compatible strings),
  6366. * "is valid UTF-8", "is valid extended UTF-8" (internal strings are not,
  6367. * regexp bytecode is), and "contains non-BMP characters". These are not
  6368. * needed right now.
  6369. */
  6370. #define DUK_HSTRING_FLAG_ARRIDX DUK_HEAPHDR_USER_FLAG(0) /* string is a valid array index */
  6371. #define DUK_HSTRING_FLAG_INTERNAL DUK_HEAPHDR_USER_FLAG(1) /* string is internal */
  6372. #define DUK_HSTRING_FLAG_RESERVED_WORD DUK_HEAPHDR_USER_FLAG(2) /* string is a reserved word (non-strict) */
  6373. #define DUK_HSTRING_FLAG_STRICT_RESERVED_WORD DUK_HEAPHDR_USER_FLAG(3) /* string is a reserved word (strict) */
  6374. #define DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS DUK_HEAPHDR_USER_FLAG(4) /* string is 'eval' or 'arguments' */
  6375. #define DUK_HSTRING_FLAG_EXTDATA DUK_HEAPHDR_USER_FLAG(5) /* string data is external (duk_hstring_external) */
  6376. #define DUK_HSTRING_HAS_ARRIDX(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ARRIDX)
  6377. #define DUK_HSTRING_HAS_INTERNAL(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_INTERNAL)
  6378. #define DUK_HSTRING_HAS_RESERVED_WORD(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_RESERVED_WORD)
  6379. #define DUK_HSTRING_HAS_STRICT_RESERVED_WORD(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_STRICT_RESERVED_WORD)
  6380. #define DUK_HSTRING_HAS_EVAL_OR_ARGUMENTS(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS)
  6381. #define DUK_HSTRING_HAS_EXTDATA(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EXTDATA)
  6382. #define DUK_HSTRING_SET_ARRIDX(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ARRIDX)
  6383. #define DUK_HSTRING_SET_INTERNAL(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_INTERNAL)
  6384. #define DUK_HSTRING_SET_RESERVED_WORD(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_RESERVED_WORD)
  6385. #define DUK_HSTRING_SET_STRICT_RESERVED_WORD(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_STRICT_RESERVED_WORD)
  6386. #define DUK_HSTRING_SET_EVAL_OR_ARGUMENTS(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS)
  6387. #define DUK_HSTRING_SET_EXTDATA(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EXTDATA)
  6388. #define DUK_HSTRING_CLEAR_ARRIDX(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ARRIDX)
  6389. #define DUK_HSTRING_CLEAR_INTERNAL(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_INTERNAL)
  6390. #define DUK_HSTRING_CLEAR_RESERVED_WORD(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_RESERVED_WORD)
  6391. #define DUK_HSTRING_CLEAR_STRICT_RESERVED_WORD(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_STRICT_RESERVED_WORD)
  6392. #define DUK_HSTRING_CLEAR_EVAL_OR_ARGUMENTS(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS)
  6393. #define DUK_HSTRING_CLEAR_EXTDATA(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EXTDATA)
  6394. #define DUK_HSTRING_IS_ASCII(x) (DUK_HSTRING_GET_BYTELEN((x)) == DUK_HSTRING_GET_CHARLEN((x)))
  6395. #define DUK_HSTRING_IS_EMPTY(x) (DUK_HSTRING_GET_BYTELEN((x)) == 0)
  6396. #if defined(DUK_USE_STRHASH16)
  6397. #define DUK_HSTRING_GET_HASH(x) ((x)->hdr.h_flags >> 16)
  6398. #define DUK_HSTRING_SET_HASH(x,v) do { \
  6399. (x)->hdr.h_flags = ((x)->hdr.h_flags & 0x0000ffffUL) | ((v) << 16); \
  6400. } while (0)
  6401. #else
  6402. #define DUK_HSTRING_GET_HASH(x) ((x)->hash)
  6403. #define DUK_HSTRING_SET_HASH(x,v) do { \
  6404. (x)->hash = (v); \
  6405. } while (0)
  6406. #endif
  6407. #if defined(DUK_USE_STRLEN16)
  6408. #define DUK_HSTRING_GET_BYTELEN(x) ((x)->blen16)
  6409. #define DUK_HSTRING_SET_BYTELEN(x,v) do { \
  6410. (x)->blen16 = (v); \
  6411. } while (0)
  6412. #define DUK_HSTRING_GET_CHARLEN(x) ((x)->clen16)
  6413. #define DUK_HSTRING_SET_CHARLEN(x,v) do { \
  6414. (x)->clen16 = (v); \
  6415. } while (0)
  6416. #else
  6417. #define DUK_HSTRING_GET_BYTELEN(x) ((x)->blen)
  6418. #define DUK_HSTRING_SET_BYTELEN(x,v) do { \
  6419. (x)->blen = (v); \
  6420. } while (0)
  6421. #define DUK_HSTRING_GET_CHARLEN(x) ((x)->clen)
  6422. #define DUK_HSTRING_SET_CHARLEN(x,v) do { \
  6423. (x)->clen = (v); \
  6424. } while (0)
  6425. #endif
  6426. #if defined(DUK_USE_HSTRING_EXTDATA)
  6427. #define DUK_HSTRING_GET_EXTDATA(x) \
  6428. ((x)->extdata)
  6429. #define DUK_HSTRING_GET_DATA(x) \
  6430. (DUK_HSTRING_HAS_EXTDATA((x)) ? \
  6431. DUK_HSTRING_GET_EXTDATA((duk_hstring_external *) (x)) : ((const duk_uint8_t *) ((x) + 1)))
  6432. #else
  6433. #define DUK_HSTRING_GET_DATA(x) \
  6434. ((const duk_uint8_t *) ((x) + 1))
  6435. #endif
  6436. #define DUK_HSTRING_GET_DATA_END(x) \
  6437. (DUK_HSTRING_GET_DATA((x)) + (x)->blen)
  6438. /* marker value; in E5 2^32-1 is not a valid array index (2^32-2 is highest valid) */
  6439. #define DUK_HSTRING_NO_ARRAY_INDEX (0xffffffffUL)
  6440. /* get array index related to string (or return DUK_HSTRING_NO_ARRAY_INDEX);
  6441. * avoids helper call if string has no array index value.
  6442. */
  6443. #define DUK_HSTRING_GET_ARRIDX_FAST(h) \
  6444. (DUK_HSTRING_HAS_ARRIDX((h)) ? duk_js_to_arrayindex_string_helper((h)) : DUK_HSTRING_NO_ARRAY_INDEX)
  6445. /* slower but more compact variant */
  6446. #define DUK_HSTRING_GET_ARRIDX_SLOW(h) \
  6447. (duk_js_to_arrayindex_string_helper((h)))
  6448. /*
  6449. * Misc
  6450. */
  6451. struct duk_hstring {
  6452. /* Smaller heaphdr than for other objects, because strings are held
  6453. * in string intern table which requires no link pointers. Much of
  6454. * the 32-bit flags field is unused by flags, so we can stuff a 16-bit
  6455. * field in there.
  6456. */
  6457. duk_heaphdr_string hdr;
  6458. /* Note: we could try to stuff a partial hash (e.g. 16 bits) into the
  6459. * shared heap header. Good hashing needs more hash bits though.
  6460. */
  6461. /* string hash */
  6462. #if defined(DUK_USE_STRHASH16)
  6463. /* If 16-bit hash is in use, stuff it into duk_heaphdr_string flags. */
  6464. #else
  6465. duk_uint32_t hash;
  6466. #endif
  6467. /* length in bytes (not counting NUL term) */
  6468. #if defined(DUK_USE_STRLEN16)
  6469. duk_uint16_t blen16;
  6470. #else
  6471. duk_uint32_t blen;
  6472. #endif
  6473. /* length in codepoints (must be E5 compatible) */
  6474. #if defined(DUK_USE_STRLEN16)
  6475. duk_uint16_t clen16;
  6476. #else
  6477. duk_uint32_t clen;
  6478. #endif
  6479. /*
  6480. * String value of 'blen+1' bytes follows (+1 for NUL termination
  6481. * convenience for C API). No alignment needs to be guaranteed
  6482. * for strings, but fields above should guarantee alignment-by-4
  6483. * (but not alignment-by-8).
  6484. */
  6485. };
  6486. /* The external string struct is defined even when the feature is inactive. */
  6487. struct duk_hstring_external {
  6488. duk_hstring str;
  6489. /*
  6490. * For an external string, the NUL-terminated string data is stored
  6491. * externally. The user must guarantee that data behind this pointer
  6492. * doesn't change while it's used.
  6493. */
  6494. const duk_uint8_t *extdata;
  6495. };
  6496. /*
  6497. * Prototypes
  6498. */
  6499. DUK_INTERNAL_DECL duk_ucodepoint_t duk_hstring_char_code_at_raw(duk_hthread *thr, duk_hstring *h, duk_uint_t pos);
  6500. #endif /* DUK_HSTRING_H_INCLUDED */
  6501. #line 1 "duk_hobject.h"
  6502. /*
  6503. * Heap object representation.
  6504. *
  6505. * Heap objects are used for Ecmascript objects, arrays, and functions,
  6506. * but also for internal control like declarative and object environment
  6507. * records. Compiled functions, native functions, and threads are also
  6508. * objects but with an extended C struct.
  6509. *
  6510. * Objects provide the required Ecmascript semantics and exotic behaviors
  6511. * especially for property access.
  6512. *
  6513. * Properties are stored in three conceptual parts:
  6514. *
  6515. * 1. A linear 'entry part' contains ordered key-value-attributes triples
  6516. * and is the main method of string properties.
  6517. *
  6518. * 2. An optional linear 'array part' is used for array objects to store a
  6519. * (dense) range of [0,N[ array indexed entries with default attributes
  6520. * (writable, enumerable, configurable). If the array part would become
  6521. * sparse or non-default attributes are required, the array part is
  6522. * abandoned and moved to the 'entry part'.
  6523. *
  6524. * 3. An optional 'hash part' is used to optimize lookups of the entry
  6525. * part; it is used only for objects with sufficiently many properties
  6526. * and can be abandoned without loss of information.
  6527. *
  6528. * These three conceptual parts are stored in a single memory allocated area.
  6529. * This minimizes memory allocation overhead but also means that all three
  6530. * parts are resized together, and makes property access a bit complicated.
  6531. */
  6532. #ifndef DUK_HOBJECT_H_INCLUDED
  6533. #define DUK_HOBJECT_H_INCLUDED
  6534. /* there are currently 26 flag bits available */
  6535. #define DUK_HOBJECT_FLAG_EXTENSIBLE DUK_HEAPHDR_USER_FLAG(0) /* object is extensible */
  6536. #define DUK_HOBJECT_FLAG_CONSTRUCTABLE DUK_HEAPHDR_USER_FLAG(1) /* object is constructable */
  6537. #define DUK_HOBJECT_FLAG_BOUND DUK_HEAPHDR_USER_FLAG(2) /* object established using Function.prototype.bind() */
  6538. #define DUK_HOBJECT_FLAG_COMPILEDFUNCTION DUK_HEAPHDR_USER_FLAG(4) /* object is a compiled function (duk_hcompiledfunction) */
  6539. #define DUK_HOBJECT_FLAG_NATIVEFUNCTION DUK_HEAPHDR_USER_FLAG(5) /* object is a native function (duk_hnativefunction) */
  6540. #define DUK_HOBJECT_FLAG_BUFFEROBJECT DUK_HEAPHDR_USER_FLAG(6) /* object is a buffer object (duk_hbufferobject) (always exotic) */
  6541. #define DUK_HOBJECT_FLAG_THREAD DUK_HEAPHDR_USER_FLAG(7) /* object is a thread (duk_hthread) */
  6542. #define DUK_HOBJECT_FLAG_ARRAY_PART DUK_HEAPHDR_USER_FLAG(8) /* object has an array part (a_size may still be 0) */
  6543. #define DUK_HOBJECT_FLAG_STRICT DUK_HEAPHDR_USER_FLAG(9) /* function: function object is strict */
  6544. #define DUK_HOBJECT_FLAG_NOTAIL DUK_HEAPHDR_USER_FLAG(10) /* function: function must not be tailcalled */
  6545. #define DUK_HOBJECT_FLAG_NEWENV DUK_HEAPHDR_USER_FLAG(11) /* function: create new environment when called (see duk_hcompiledfunction) */
  6546. #define DUK_HOBJECT_FLAG_NAMEBINDING DUK_HEAPHDR_USER_FLAG(12) /* function: create binding for func name (function templates only, used for named function expressions) */
  6547. #define DUK_HOBJECT_FLAG_CREATEARGS DUK_HEAPHDR_USER_FLAG(13) /* function: create an arguments object on function call */
  6548. #define DUK_HOBJECT_FLAG_ENVRECCLOSED DUK_HEAPHDR_USER_FLAG(14) /* envrec: (declarative) record is closed */
  6549. #define DUK_HOBJECT_FLAG_EXOTIC_ARRAY DUK_HEAPHDR_USER_FLAG(15) /* 'Array' object, array length and index exotic behavior */
  6550. #define DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ DUK_HEAPHDR_USER_FLAG(16) /* 'String' object, array index exotic behavior */
  6551. #define DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS DUK_HEAPHDR_USER_FLAG(17) /* 'Arguments' object and has arguments exotic behavior (non-strict callee) */
  6552. #define DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC DUK_HEAPHDR_USER_FLAG(18) /* Duktape/C (nativefunction) object, exotic 'length' */
  6553. #define DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ DUK_HEAPHDR_USER_FLAG(19) /* 'Proxy' object */
  6554. #define DUK_HOBJECT_FLAG_CLASS_BASE DUK_HEAPHDR_USER_FLAG_NUMBER(21)
  6555. #define DUK_HOBJECT_FLAG_CLASS_BITS 5
  6556. #define DUK_HOBJECT_GET_CLASS_NUMBER(h) \
  6557. DUK_HEAPHDR_GET_FLAG_RANGE(&(h)->hdr, DUK_HOBJECT_FLAG_CLASS_BASE, DUK_HOBJECT_FLAG_CLASS_BITS)
  6558. #define DUK_HOBJECT_SET_CLASS_NUMBER(h,v) \
  6559. DUK_HEAPHDR_SET_FLAG_RANGE(&(h)->hdr, DUK_HOBJECT_FLAG_CLASS_BASE, DUK_HOBJECT_FLAG_CLASS_BITS, (v))
  6560. /* Macro for creating flag initializer from a class number.
  6561. * Unsigned type cast is needed to avoid warnings about coercing
  6562. * a signed integer to an unsigned one; the largest class values
  6563. * have the highest bit (bit 31) set which causes this.
  6564. */
  6565. #define DUK_HOBJECT_CLASS_AS_FLAGS(v) (((duk_uint_t) (v)) << DUK_HOBJECT_FLAG_CLASS_BASE)
  6566. /* E5 Section 8.6.2 + custom classes */
  6567. #define DUK_HOBJECT_CLASS_UNUSED 0
  6568. #define DUK_HOBJECT_CLASS_ARGUMENTS 1
  6569. #define DUK_HOBJECT_CLASS_ARRAY 2
  6570. #define DUK_HOBJECT_CLASS_BOOLEAN 3
  6571. #define DUK_HOBJECT_CLASS_DATE 4
  6572. #define DUK_HOBJECT_CLASS_ERROR 5
  6573. #define DUK_HOBJECT_CLASS_FUNCTION 6
  6574. #define DUK_HOBJECT_CLASS_JSON 7
  6575. #define DUK_HOBJECT_CLASS_MATH 8
  6576. #define DUK_HOBJECT_CLASS_NUMBER 9
  6577. #define DUK_HOBJECT_CLASS_OBJECT 10
  6578. #define DUK_HOBJECT_CLASS_REGEXP 11
  6579. #define DUK_HOBJECT_CLASS_STRING 12
  6580. #define DUK_HOBJECT_CLASS_GLOBAL 13
  6581. #define DUK_HOBJECT_CLASS_OBJENV 14 /* custom */
  6582. #define DUK_HOBJECT_CLASS_DECENV 15 /* custom */
  6583. #define DUK_HOBJECT_CLASS_BUFFER 16 /* custom; implies DUK_HOBJECT_IS_BUFFEROBJECT */
  6584. #define DUK_HOBJECT_CLASS_POINTER 17 /* custom */
  6585. #define DUK_HOBJECT_CLASS_THREAD 18 /* custom; implies DUK_HOBJECT_IS_THREAD */
  6586. #define DUK_HOBJECT_CLASS_ARRAYBUFFER 19 /* implies DUK_HOBJECT_IS_BUFFEROBJECT */
  6587. #define DUK_HOBJECT_CLASS_DATAVIEW 20
  6588. #define DUK_HOBJECT_CLASS_INT8ARRAY 21
  6589. #define DUK_HOBJECT_CLASS_UINT8ARRAY 22
  6590. #define DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY 23
  6591. #define DUK_HOBJECT_CLASS_INT16ARRAY 24
  6592. #define DUK_HOBJECT_CLASS_UINT16ARRAY 25
  6593. #define DUK_HOBJECT_CLASS_INT32ARRAY 26
  6594. #define DUK_HOBJECT_CLASS_UINT32ARRAY 27
  6595. #define DUK_HOBJECT_CLASS_FLOAT32ARRAY 28
  6596. #define DUK_HOBJECT_CLASS_FLOAT64ARRAY 29
  6597. #define DUK_HOBJECT_IS_OBJENV(h) (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_OBJENV)
  6598. #define DUK_HOBJECT_IS_DECENV(h) (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_DECENV)
  6599. #define DUK_HOBJECT_IS_ENV(h) (DUK_HOBJECT_IS_OBJENV((h)) || DUK_HOBJECT_IS_DECENV((h)))
  6600. #define DUK_HOBJECT_IS_ARRAY(h) (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_ARRAY)
  6601. #define DUK_HOBJECT_IS_COMPILEDFUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPILEDFUNCTION)
  6602. #define DUK_HOBJECT_IS_NATIVEFUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATIVEFUNCTION)
  6603. #define DUK_HOBJECT_IS_BUFFEROBJECT(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFFEROBJECT)
  6604. #define DUK_HOBJECT_IS_THREAD(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_THREAD)
  6605. #define DUK_HOBJECT_IS_NONBOUND_FUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, \
  6606. DUK_HOBJECT_FLAG_COMPILEDFUNCTION | \
  6607. DUK_HOBJECT_FLAG_NATIVEFUNCTION)
  6608. #define DUK_HOBJECT_IS_FUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, \
  6609. DUK_HOBJECT_FLAG_BOUND | \
  6610. DUK_HOBJECT_FLAG_COMPILEDFUNCTION | \
  6611. DUK_HOBJECT_FLAG_NATIVEFUNCTION)
  6612. #define DUK_HOBJECT_IS_CALLABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, \
  6613. DUK_HOBJECT_FLAG_BOUND | \
  6614. DUK_HOBJECT_FLAG_COMPILEDFUNCTION | \
  6615. DUK_HOBJECT_FLAG_NATIVEFUNCTION)
  6616. /* object has any exotic behavior(s) */
  6617. #define DUK_HOBJECT_EXOTIC_BEHAVIOR_FLAGS (DUK_HOBJECT_FLAG_EXOTIC_ARRAY | \
  6618. DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS | \
  6619. DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ | \
  6620. DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC | \
  6621. DUK_HOBJECT_FLAG_BUFFEROBJECT | \
  6622. DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ)
  6623. #define DUK_HOBJECT_HAS_EXOTIC_BEHAVIOR(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_EXOTIC_BEHAVIOR_FLAGS)
  6624. #define DUK_HOBJECT_HAS_EXTENSIBLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXTENSIBLE)
  6625. #define DUK_HOBJECT_HAS_CONSTRUCTABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CONSTRUCTABLE)
  6626. #define DUK_HOBJECT_HAS_BOUND(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BOUND)
  6627. #define DUK_HOBJECT_HAS_COMPILEDFUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPILEDFUNCTION)
  6628. #define DUK_HOBJECT_HAS_NATIVEFUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATIVEFUNCTION)
  6629. #define DUK_HOBJECT_HAS_BUFFEROBJECT(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFFEROBJECT)
  6630. #define DUK_HOBJECT_HAS_THREAD(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_THREAD)
  6631. #define DUK_HOBJECT_HAS_ARRAY_PART(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ARRAY_PART)
  6632. #define DUK_HOBJECT_HAS_STRICT(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_STRICT)
  6633. #define DUK_HOBJECT_HAS_NOTAIL(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NOTAIL)
  6634. #define DUK_HOBJECT_HAS_NEWENV(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NEWENV)
  6635. #define DUK_HOBJECT_HAS_NAMEBINDING(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NAMEBINDING)
  6636. #define DUK_HOBJECT_HAS_CREATEARGS(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CREATEARGS)
  6637. #define DUK_HOBJECT_HAS_ENVRECCLOSED(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ENVRECCLOSED)
  6638. #define DUK_HOBJECT_HAS_EXOTIC_ARRAY(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARRAY)
  6639. #define DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ)
  6640. #define DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS)
  6641. #define DUK_HOBJECT_HAS_EXOTIC_DUKFUNC(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC)
  6642. #define DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ)
  6643. #define DUK_HOBJECT_SET_EXTENSIBLE(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXTENSIBLE)
  6644. #define DUK_HOBJECT_SET_CONSTRUCTABLE(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CONSTRUCTABLE)
  6645. #define DUK_HOBJECT_SET_BOUND(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BOUND)
  6646. #define DUK_HOBJECT_SET_COMPILEDFUNCTION(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPILEDFUNCTION)
  6647. #define DUK_HOBJECT_SET_NATIVEFUNCTION(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATIVEFUNCTION)
  6648. #define DUK_HOBJECT_SET_BUFFEROBJECT(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFFEROBJECT)
  6649. #define DUK_HOBJECT_SET_THREAD(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_THREAD)
  6650. #define DUK_HOBJECT_SET_ARRAY_PART(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ARRAY_PART)
  6651. #define DUK_HOBJECT_SET_STRICT(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_STRICT)
  6652. #define DUK_HOBJECT_SET_NOTAIL(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NOTAIL)
  6653. #define DUK_HOBJECT_SET_NEWENV(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NEWENV)
  6654. #define DUK_HOBJECT_SET_NAMEBINDING(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NAMEBINDING)
  6655. #define DUK_HOBJECT_SET_CREATEARGS(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CREATEARGS)
  6656. #define DUK_HOBJECT_SET_ENVRECCLOSED(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ENVRECCLOSED)
  6657. #define DUK_HOBJECT_SET_EXOTIC_ARRAY(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARRAY)
  6658. #define DUK_HOBJECT_SET_EXOTIC_STRINGOBJ(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ)
  6659. #define DUK_HOBJECT_SET_EXOTIC_ARGUMENTS(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS)
  6660. #define DUK_HOBJECT_SET_EXOTIC_DUKFUNC(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC)
  6661. #define DUK_HOBJECT_SET_EXOTIC_PROXYOBJ(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ)
  6662. #define DUK_HOBJECT_CLEAR_EXTENSIBLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXTENSIBLE)
  6663. #define DUK_HOBJECT_CLEAR_CONSTRUCTABLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CONSTRUCTABLE)
  6664. #define DUK_HOBJECT_CLEAR_BOUND(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BOUND)
  6665. #define DUK_HOBJECT_CLEAR_COMPILEDFUNCTION(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPILEDFUNCTION)
  6666. #define DUK_HOBJECT_CLEAR_NATIVEFUNCTION(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATIVEFUNCTION)
  6667. #define DUK_HOBJECT_CLEAR_BUFFEROBJECT(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFFEROBJECT)
  6668. #define DUK_HOBJECT_CLEAR_THREAD(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_THREAD)
  6669. #define DUK_HOBJECT_CLEAR_ARRAY_PART(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ARRAY_PART)
  6670. #define DUK_HOBJECT_CLEAR_STRICT(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_STRICT)
  6671. #define DUK_HOBJECT_CLEAR_NOTAIL(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NOTAIL)
  6672. #define DUK_HOBJECT_CLEAR_NEWENV(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NEWENV)
  6673. #define DUK_HOBJECT_CLEAR_NAMEBINDING(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NAMEBINDING)
  6674. #define DUK_HOBJECT_CLEAR_CREATEARGS(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CREATEARGS)
  6675. #define DUK_HOBJECT_CLEAR_ENVRECCLOSED(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ENVRECCLOSED)
  6676. #define DUK_HOBJECT_CLEAR_EXOTIC_ARRAY(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARRAY)
  6677. #define DUK_HOBJECT_CLEAR_EXOTIC_STRINGOBJ(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ)
  6678. #define DUK_HOBJECT_CLEAR_EXOTIC_ARGUMENTS(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS)
  6679. #define DUK_HOBJECT_CLEAR_EXOTIC_DUKFUNC(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC)
  6680. #define DUK_HOBJECT_CLEAR_EXOTIC_PROXYOBJ(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ)
  6681. /* flags used for property attributes in duk_propdesc and packed flags */
  6682. #define DUK_PROPDESC_FLAG_WRITABLE (1 << 0) /* E5 Section 8.6.1 */
  6683. #define DUK_PROPDESC_FLAG_ENUMERABLE (1 << 1) /* E5 Section 8.6.1 */
  6684. #define DUK_PROPDESC_FLAG_CONFIGURABLE (1 << 2) /* E5 Section 8.6.1 */
  6685. #define DUK_PROPDESC_FLAG_ACCESSOR (1 << 3) /* accessor */
  6686. #define DUK_PROPDESC_FLAG_VIRTUAL (1 << 4) /* property is virtual: used in duk_propdesc, never stored
  6687. * (used by e.g. buffer virtual properties)
  6688. */
  6689. #define DUK_PROPDESC_FLAGS_MASK (DUK_PROPDESC_FLAG_WRITABLE | \
  6690. DUK_PROPDESC_FLAG_ENUMERABLE | \
  6691. DUK_PROPDESC_FLAG_CONFIGURABLE | \
  6692. DUK_PROPDESC_FLAG_ACCESSOR)
  6693. /* additional flags which are passed in the same flags argument as property
  6694. * flags but are not stored in object properties.
  6695. */
  6696. #define DUK_PROPDESC_FLAG_NO_OVERWRITE (1 << 4) /* internal define property: skip write silently if exists */
  6697. /* convenience */
  6698. #define DUK_PROPDESC_FLAGS_NONE 0
  6699. #define DUK_PROPDESC_FLAGS_W (DUK_PROPDESC_FLAG_WRITABLE)
  6700. #define DUK_PROPDESC_FLAGS_E (DUK_PROPDESC_FLAG_ENUMERABLE)
  6701. #define DUK_PROPDESC_FLAGS_C (DUK_PROPDESC_FLAG_CONFIGURABLE)
  6702. #define DUK_PROPDESC_FLAGS_WE (DUK_PROPDESC_FLAG_WRITABLE | DUK_PROPDESC_FLAG_ENUMERABLE)
  6703. #define DUK_PROPDESC_FLAGS_WC (DUK_PROPDESC_FLAG_WRITABLE | DUK_PROPDESC_FLAG_CONFIGURABLE)
  6704. #define DUK_PROPDESC_FLAGS_EC (DUK_PROPDESC_FLAG_ENUMERABLE | DUK_PROPDESC_FLAG_CONFIGURABLE)
  6705. #define DUK_PROPDESC_FLAGS_WEC (DUK_PROPDESC_FLAG_WRITABLE | \
  6706. DUK_PROPDESC_FLAG_ENUMERABLE | \
  6707. DUK_PROPDESC_FLAG_CONFIGURABLE)
  6708. /*
  6709. * Macros to access the 'props' allocation.
  6710. */
  6711. #if defined(DUK_USE_HEAPPTR16)
  6712. #define DUK_HOBJECT_GET_PROPS(heap,h) \
  6713. ((duk_uint8_t *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, ((duk_heaphdr *) (h))->h_extra16))
  6714. #define DUK_HOBJECT_SET_PROPS(heap,h,x) do { \
  6715. ((duk_heaphdr *) (h))->h_extra16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (x)); \
  6716. } while (0)
  6717. #else
  6718. #define DUK_HOBJECT_GET_PROPS(heap,h) \
  6719. ((h)->props)
  6720. #define DUK_HOBJECT_SET_PROPS(heap,h,x) do { \
  6721. (h)->props = (x); \
  6722. } while (0)
  6723. #endif
  6724. #if defined(DUK_USE_HOBJECT_LAYOUT_1)
  6725. /* LAYOUT 1 */
  6726. #define DUK_HOBJECT_E_GET_KEY_BASE(heap,h) \
  6727. ((duk_hstring **) ( \
  6728. DUK_HOBJECT_GET_PROPS((heap), (h)) \
  6729. ))
  6730. #define DUK_HOBJECT_E_GET_VALUE_BASE(heap,h) \
  6731. ((duk_propvalue *) ( \
  6732. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  6733. DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_hstring *) \
  6734. ))
  6735. #define DUK_HOBJECT_E_GET_FLAGS_BASE(heap,h) \
  6736. ((duk_uint8_t *) ( \
  6737. DUK_HOBJECT_GET_PROPS((heap), (h)) + DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue)) \
  6738. ))
  6739. #define DUK_HOBJECT_A_GET_BASE(heap,h) \
  6740. ((duk_tval *) ( \
  6741. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  6742. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) \
  6743. ))
  6744. #define DUK_HOBJECT_H_GET_BASE(heap,h) \
  6745. ((duk_uint32_t *) ( \
  6746. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  6747. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \
  6748. DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \
  6749. ))
  6750. #define DUK_HOBJECT_P_COMPUTE_SIZE(n_ent,n_arr,n_hash) \
  6751. ( \
  6752. (n_ent) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \
  6753. (n_arr) * sizeof(duk_tval) + \
  6754. (n_hash) * sizeof(duk_uint32_t) \
  6755. )
  6756. #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 { \
  6757. (set_e_k) = (duk_hstring **) (p_base); \
  6758. (set_e_pv) = (duk_propvalue *) ((set_e_k) + (n_ent)); \
  6759. (set_e_f) = (duk_uint8_t *) ((set_e_pv) + (n_ent)); \
  6760. (set_a) = (duk_tval *) ((set_e_f) + (n_ent)); \
  6761. (set_h) = (duk_uint32_t *) ((set_a) + (n_arr)); \
  6762. } while (0)
  6763. #elif defined(DUK_USE_HOBJECT_LAYOUT_2)
  6764. /* LAYOUT 2 */
  6765. #if defined(DUK_USE_ALIGN_4)
  6766. #define DUK_HOBJECT_E_FLAG_PADDING(e_sz) ((4 - (e_sz)) & 0x03)
  6767. #elif defined(DUK_USE_ALIGN_8)
  6768. #define DUK_HOBJECT_E_FLAG_PADDING(e_sz) ((8 - (e_sz)) & 0x07)
  6769. #else
  6770. #define DUK_HOBJECT_E_FLAG_PADDING(e_sz) 0
  6771. #endif
  6772. #define DUK_HOBJECT_E_GET_KEY_BASE(heap,h) \
  6773. ((duk_hstring **) ( \
  6774. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  6775. DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_propvalue) \
  6776. ))
  6777. #define DUK_HOBJECT_E_GET_VALUE_BASE(heap,h) \
  6778. ((duk_propvalue *) ( \
  6779. DUK_HOBJECT_GET_PROPS((heap), (h)) \
  6780. ))
  6781. #define DUK_HOBJECT_E_GET_FLAGS_BASE(heap,h) \
  6782. ((duk_uint8_t *) ( \
  6783. DUK_HOBJECT_GET_PROPS((heap), (h)) + DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue)) \
  6784. ))
  6785. #define DUK_HOBJECT_A_GET_BASE(heap,h) \
  6786. ((duk_tval *) ( \
  6787. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  6788. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \
  6789. DUK_HOBJECT_E_FLAG_PADDING(DUK_HOBJECT_GET_ESIZE((h))) \
  6790. ))
  6791. #define DUK_HOBJECT_H_GET_BASE(heap,h) \
  6792. ((duk_uint32_t *) ( \
  6793. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  6794. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \
  6795. DUK_HOBJECT_E_FLAG_PADDING(DUK_HOBJECT_GET_ESIZE((h))) + \
  6796. DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \
  6797. ))
  6798. #define DUK_HOBJECT_P_COMPUTE_SIZE(n_ent,n_arr,n_hash) \
  6799. ( \
  6800. (n_ent) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \
  6801. DUK_HOBJECT_E_FLAG_PADDING((n_ent)) + \
  6802. (n_arr) * sizeof(duk_tval) + \
  6803. (n_hash) * sizeof(duk_uint32_t) \
  6804. )
  6805. #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 { \
  6806. (set_e_pv) = (duk_propvalue *) (p_base); \
  6807. (set_e_k) = (duk_hstring **) ((set_e_pv) + (n_ent)); \
  6808. (set_e_f) = (duk_uint8_t *) ((set_e_k) + (n_ent)); \
  6809. (set_a) = (duk_tval *) (((duk_uint8_t *) (set_e_f)) + \
  6810. sizeof(duk_uint8_t) * (n_ent) + \
  6811. DUK_HOBJECT_E_FLAG_PADDING((n_ent))); \
  6812. (set_h) = (duk_uint32_t *) ((set_a) + (n_arr)); \
  6813. } while (0)
  6814. #elif defined(DUK_USE_HOBJECT_LAYOUT_3)
  6815. /* LAYOUT 3 */
  6816. #define DUK_HOBJECT_E_GET_KEY_BASE(heap,h) \
  6817. ((duk_hstring **) ( \
  6818. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  6819. DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_propvalue) + \
  6820. DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \
  6821. ))
  6822. #define DUK_HOBJECT_E_GET_VALUE_BASE(heap,h) \
  6823. ((duk_propvalue *) ( \
  6824. DUK_HOBJECT_GET_PROPS((heap), (h)) \
  6825. ))
  6826. #define DUK_HOBJECT_E_GET_FLAGS_BASE(heap,h) \
  6827. ((duk_uint8_t *) ( \
  6828. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  6829. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_propvalue) + sizeof(duk_hstring *)) + \
  6830. DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) + \
  6831. DUK_HOBJECT_GET_HSIZE((h)) * sizeof(duk_uint32_t) \
  6832. ))
  6833. #define DUK_HOBJECT_A_GET_BASE(heap,h) \
  6834. ((duk_tval *) ( \
  6835. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  6836. DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_propvalue) \
  6837. ))
  6838. #define DUK_HOBJECT_H_GET_BASE(heap,h) \
  6839. ((duk_uint32_t *) ( \
  6840. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  6841. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_propvalue) + sizeof(duk_hstring *)) + \
  6842. DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \
  6843. ))
  6844. #define DUK_HOBJECT_P_COMPUTE_SIZE(n_ent,n_arr,n_hash) \
  6845. ( \
  6846. (n_ent) * (sizeof(duk_propvalue) + sizeof(duk_hstring *) + sizeof(duk_uint8_t)) + \
  6847. (n_arr) * sizeof(duk_tval) + \
  6848. (n_hash) * sizeof(duk_uint32_t) \
  6849. )
  6850. #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 { \
  6851. (set_e_pv) = (duk_propvalue *) (p_base); \
  6852. (set_a) = (duk_tval *) ((set_e_pv) + (n_ent)); \
  6853. (set_e_k) = (duk_hstring **) ((set_a) + (n_arr)); \
  6854. (set_h) = (duk_uint32_t *) ((set_e_k) + (n_ent)); \
  6855. (set_e_f) = (duk_uint8_t *) ((set_h) + (n_hash)); \
  6856. } while (0)
  6857. #else
  6858. #error invalid hobject layout defines
  6859. #endif /* hobject property layout */
  6860. #define DUK_HOBJECT_E_ALLOC_SIZE(h) \
  6861. DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE((h)), DUK_HOBJECT_GET_ASIZE((h)), DUK_HOBJECT_GET_HSIZE((h)))
  6862. #define DUK_HOBJECT_E_GET_KEY(heap,h,i) (DUK_HOBJECT_E_GET_KEY_BASE((heap), (h))[(i)])
  6863. #define DUK_HOBJECT_E_GET_KEY_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_KEY_BASE((heap), (h))[(i)])
  6864. #define DUK_HOBJECT_E_GET_VALUE(heap,h,i) (DUK_HOBJECT_E_GET_VALUE_BASE((heap), (h))[(i)])
  6865. #define DUK_HOBJECT_E_GET_VALUE_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE_BASE((heap), (h))[(i)])
  6866. #define DUK_HOBJECT_E_GET_VALUE_TVAL(heap,h,i) (DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).v)
  6867. #define DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).v)
  6868. #define DUK_HOBJECT_E_GET_VALUE_GETTER(heap,h,i) (DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.get)
  6869. #define DUK_HOBJECT_E_GET_VALUE_GETTER_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.get)
  6870. #define DUK_HOBJECT_E_GET_VALUE_SETTER(heap,h,i) (DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.set)
  6871. #define DUK_HOBJECT_E_GET_VALUE_SETTER_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.set)
  6872. #define DUK_HOBJECT_E_GET_FLAGS(heap,h,i) (DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)])
  6873. #define DUK_HOBJECT_E_GET_FLAGS_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)])
  6874. #define DUK_HOBJECT_A_GET_VALUE(heap,h,i) (DUK_HOBJECT_A_GET_BASE((heap), (h))[(i)])
  6875. #define DUK_HOBJECT_A_GET_VALUE_PTR(heap,h,i) (&DUK_HOBJECT_A_GET_BASE((heap), (h))[(i)])
  6876. #define DUK_HOBJECT_H_GET_INDEX(heap,h,i) (DUK_HOBJECT_H_GET_BASE((heap), (h))[(i)])
  6877. #define DUK_HOBJECT_H_GET_INDEX_PTR(heap,h,i) (&DUK_HOBJECT_H_GET_BASE((heap), (h))[(i)])
  6878. #define DUK_HOBJECT_E_SET_KEY(heap,h,i,k) do { \
  6879. DUK_HOBJECT_E_GET_KEY((heap), (h), (i)) = (k); \
  6880. } while (0)
  6881. #define DUK_HOBJECT_E_SET_VALUE(heap,h,i,v) do { \
  6882. DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)) = (v); \
  6883. } while (0)
  6884. #define DUK_HOBJECT_E_SET_VALUE_TVAL(heap,h,i,v) do { \
  6885. DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).v = (v); \
  6886. } while (0)
  6887. #define DUK_HOBJECT_E_SET_VALUE_GETTER(heap,h,i,v) do { \
  6888. DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.get = (v); \
  6889. } while (0)
  6890. #define DUK_HOBJECT_E_SET_VALUE_SETTER(heap,h,i,v) do { \
  6891. DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.set = (v); \
  6892. } while (0)
  6893. #define DUK_HOBJECT_E_SET_FLAGS(heap,h,i,f) do { \
  6894. DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) = (f); \
  6895. } while (0)
  6896. #define DUK_HOBJECT_A_SET_VALUE(heap,h,i,v) do { \
  6897. DUK_HOBJECT_A_GET_VALUE((heap), (h), (i)) = (v); \
  6898. } while (0)
  6899. #define DUK_HOBJECT_A_SET_VALUE_TVAL(heap,h,i,v) \
  6900. DUK_HOBJECT_A_SET_VALUE((heap), (h), (i), (v)) /* alias for above */
  6901. #define DUK_HOBJECT_H_SET_INDEX(heap,h,i,v) do { \
  6902. DUK_HOBJECT_H_GET_INDEX((heap), (h), (i)) = (v); \
  6903. } while (0)
  6904. #define DUK_HOBJECT_E_SET_FLAG_BITS(heap,h,i,mask) do { \
  6905. DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)] |= (mask); \
  6906. } while (0)
  6907. #define DUK_HOBJECT_E_CLEAR_FLAG_BITS(heap,h,i,mask) do { \
  6908. DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)] &= ~(mask); \
  6909. } while (0)
  6910. #define DUK_HOBJECT_E_SLOT_IS_WRITABLE(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_WRITABLE) != 0)
  6911. #define DUK_HOBJECT_E_SLOT_IS_ENUMERABLE(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_ENUMERABLE) != 0)
  6912. #define DUK_HOBJECT_E_SLOT_IS_CONFIGURABLE(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_CONFIGURABLE) != 0)
  6913. #define DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_ACCESSOR) != 0)
  6914. #define DUK_HOBJECT_E_SLOT_SET_WRITABLE(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_WRITABLE)
  6915. #define DUK_HOBJECT_E_SLOT_SET_ENUMERABLE(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ENUMERABLE)
  6916. #define DUK_HOBJECT_E_SLOT_SET_CONFIGURABLE(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_CONFIGURABLE)
  6917. #define DUK_HOBJECT_E_SLOT_SET_ACCESSOR(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ACCESSOR)
  6918. #define DUK_HOBJECT_E_SLOT_CLEAR_WRITABLE(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_WRITABLE)
  6919. #define DUK_HOBJECT_E_SLOT_CLEAR_ENUMERABLE(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ENUMERABLE)
  6920. #define DUK_HOBJECT_E_SLOT_CLEAR_CONFIGURABLE(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_CONFIGURABLE)
  6921. #define DUK_HOBJECT_E_SLOT_CLEAR_ACCESSOR(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ACCESSOR)
  6922. #define DUK_PROPDESC_IS_WRITABLE(p) (((p)->flags & DUK_PROPDESC_FLAG_WRITABLE) != 0)
  6923. #define DUK_PROPDESC_IS_ENUMERABLE(p) (((p)->flags & DUK_PROPDESC_FLAG_ENUMERABLE) != 0)
  6924. #define DUK_PROPDESC_IS_CONFIGURABLE(p) (((p)->flags & DUK_PROPDESC_FLAG_CONFIGURABLE) != 0)
  6925. #define DUK_PROPDESC_IS_ACCESSOR(p) (((p)->flags & DUK_PROPDESC_FLAG_ACCESSOR) != 0)
  6926. #define DUK_HOBJECT_HASHIDX_UNUSED 0xffffffffUL
  6927. #define DUK_HOBJECT_HASHIDX_DELETED 0xfffffffeUL
  6928. /*
  6929. * Macros for accessing size fields
  6930. */
  6931. #if defined(DUK_USE_OBJSIZES16)
  6932. #define DUK_HOBJECT_GET_ESIZE(h) ((h)->e_size16)
  6933. #define DUK_HOBJECT_SET_ESIZE(h,v) do { (h)->e_size16 = (v); } while (0)
  6934. #define DUK_HOBJECT_GET_ENEXT(h) ((h)->e_next16)
  6935. #define DUK_HOBJECT_SET_ENEXT(h,v) do { (h)->e_next16 = (v); } while (0)
  6936. #define DUK_HOBJECT_POSTINC_ENEXT(h) ((h)->e_next16++)
  6937. #define DUK_HOBJECT_GET_ASIZE(h) ((h)->a_size16)
  6938. #define DUK_HOBJECT_SET_ASIZE(h,v) do { (h)->a_size16 = (v); } while (0)
  6939. #if defined(DUK_USE_HOBJECT_HASH_PART)
  6940. #define DUK_HOBJECT_GET_HSIZE(h) ((h)->h_size16)
  6941. #define DUK_HOBJECT_SET_HSIZE(h,v) do { (h)->h_size16 = (v); } while (0)
  6942. #else
  6943. #define DUK_HOBJECT_GET_HSIZE(h) 0
  6944. #define DUK_HOBJECT_SET_HSIZE(h,v) do { DUK_ASSERT((v) == 0); } while (0)
  6945. #endif
  6946. #else
  6947. #define DUK_HOBJECT_GET_ESIZE(h) ((h)->e_size)
  6948. #define DUK_HOBJECT_SET_ESIZE(h,v) do { (h)->e_size = (v); } while (0)
  6949. #define DUK_HOBJECT_GET_ENEXT(h) ((h)->e_next)
  6950. #define DUK_HOBJECT_SET_ENEXT(h,v) do { (h)->e_next = (v); } while (0)
  6951. #define DUK_HOBJECT_POSTINC_ENEXT(h) ((h)->e_next++)
  6952. #define DUK_HOBJECT_GET_ASIZE(h) ((h)->a_size)
  6953. #define DUK_HOBJECT_SET_ASIZE(h,v) do { (h)->a_size = (v); } while (0)
  6954. #if defined(DUK_USE_HOBJECT_HASH_PART)
  6955. #define DUK_HOBJECT_GET_HSIZE(h) ((h)->h_size)
  6956. #define DUK_HOBJECT_SET_HSIZE(h,v) do { (h)->h_size = (v); } while (0)
  6957. #else
  6958. #define DUK_HOBJECT_GET_HSIZE(h) 0
  6959. #define DUK_HOBJECT_SET_HSIZE(h,v) do { DUK_ASSERT((v) == 0); } while (0)
  6960. #endif
  6961. #endif
  6962. /*
  6963. * Misc
  6964. */
  6965. /* Maximum prototype traversal depth. Sanity limit which handles e.g.
  6966. * prototype loops (even complex ones like 1->2->3->4->2->3->4->2->3->4).
  6967. */
  6968. #define DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY 10000L
  6969. /* Maximum traversal depth for "bound function" chains. */
  6970. #define DUK_HOBJECT_BOUND_CHAIN_SANITY 10000L
  6971. /*
  6972. * Ecmascript [[Class]]
  6973. */
  6974. /* range check not necessary because all 4-bit values are mapped */
  6975. #define DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(n) duk_class_number_to_stridx[(n)]
  6976. #define DUK_HOBJECT_GET_CLASS_STRING(heap,h) \
  6977. DUK_HEAP_GET_STRING( \
  6978. (heap), \
  6979. DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(DUK_HOBJECT_GET_CLASS_NUMBER((h))) \
  6980. )
  6981. /*
  6982. * Macros for property handling
  6983. */
  6984. #if defined(DUK_USE_HEAPPTR16)
  6985. #define DUK_HOBJECT_GET_PROTOTYPE(heap,h) \
  6986. ((duk_hobject *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->prototype16))
  6987. #define DUK_HOBJECT_SET_PROTOTYPE(heap,h,x) do { \
  6988. (h)->prototype16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (x)); \
  6989. } while (0)
  6990. #else
  6991. #define DUK_HOBJECT_GET_PROTOTYPE(heap,h) \
  6992. ((h)->prototype)
  6993. #define DUK_HOBJECT_SET_PROTOTYPE(heap,h,x) do { \
  6994. (h)->prototype = (x); \
  6995. } while (0)
  6996. #endif
  6997. /* note: this updates refcounts */
  6998. #define DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr,h,p) duk_hobject_set_prototype((thr), (h), (p))
  6999. /*
  7000. * Resizing and hash behavior
  7001. */
  7002. /* Sanity limit on max number of properties (allocated, not necessarily used).
  7003. * This is somewhat arbitrary, but if we're close to 2**32 properties some
  7004. * algorithms will fail (e.g. hash size selection, next prime selection).
  7005. * Also, we use negative array/entry table indices to indicate 'not found',
  7006. * so anything above 0x80000000 will cause trouble now.
  7007. */
  7008. #if defined(DUK_USE_OBJSIZES16)
  7009. #define DUK_HOBJECT_MAX_PROPERTIES 0x0000ffffUL
  7010. #else
  7011. #define DUK_HOBJECT_MAX_PROPERTIES 0x7fffffffUL /* 2**31-1 ~= 2G properties */
  7012. #endif
  7013. /* higher value conserves memory; also note that linear scan is cache friendly */
  7014. #define DUK_HOBJECT_E_USE_HASH_LIMIT 32
  7015. /* hash size relative to entries size: for value X, approx. hash_prime(e_size + e_size / X) */
  7016. #define DUK_HOBJECT_H_SIZE_DIVISOR 4 /* hash size approx. 1.25 times entries size */
  7017. /* if new_size < L * old_size, resize without abandon check; L = 3-bit fixed point, e.g. 9 -> 9/8 = 112.5% */
  7018. #define DUK_HOBJECT_A_FAST_RESIZE_LIMIT 9 /* 112.5%, i.e. new size less than 12.5% higher -> fast resize */
  7019. /* if density < L, abandon array part, L = 3-bit fixed point, e.g. 2 -> 2/8 = 25% */
  7020. /* limit is quite low: one array entry is 8 bytes, one normal entry is 4+1+8+4 = 17 bytes (with hash entry) */
  7021. #define DUK_HOBJECT_A_ABANDON_LIMIT 2 /* 25%, i.e. less than 25% used -> abandon */
  7022. /* internal align target for props allocation, must be 2*n for some n */
  7023. #if defined(DUK_USE_ALIGN_4)
  7024. #define DUK_HOBJECT_ALIGN_TARGET 4
  7025. #elif defined(DUK_USE_ALIGN_8)
  7026. #define DUK_HOBJECT_ALIGN_TARGET 8
  7027. #else
  7028. #define DUK_HOBJECT_ALIGN_TARGET 1
  7029. #endif
  7030. /* controls for minimum entry part growth */
  7031. #define DUK_HOBJECT_E_MIN_GROW_ADD 16
  7032. #define DUK_HOBJECT_E_MIN_GROW_DIVISOR 8 /* 2^3 -> 1/8 = 12.5% min growth */
  7033. /* controls for minimum array part growth */
  7034. #define DUK_HOBJECT_A_MIN_GROW_ADD 16
  7035. #define DUK_HOBJECT_A_MIN_GROW_DIVISOR 8 /* 2^3 -> 1/8 = 12.5% min growth */
  7036. /* probe sequence */
  7037. #define DUK_HOBJECT_HASH_INITIAL(hash,h_size) ((hash) % (h_size))
  7038. #define DUK_HOBJECT_HASH_PROBE_STEP(hash) DUK_UTIL_GET_HASH_PROBE_STEP((hash))
  7039. /*
  7040. * PC-to-line constants
  7041. */
  7042. #define DUK_PC2LINE_SKIP 64
  7043. /* maximum length for a SKIP-1 diffstream: 35 bits per entry, rounded up to bytes */
  7044. #define DUK_PC2LINE_MAX_DIFF_LENGTH (((DUK_PC2LINE_SKIP - 1) * 35 + 7) / 8)
  7045. /*
  7046. * Struct defs
  7047. */
  7048. struct duk_propaccessor {
  7049. duk_hobject *get;
  7050. duk_hobject *set;
  7051. };
  7052. union duk_propvalue {
  7053. /* The get/set pointers could be 16-bit pointer compressed but it
  7054. * would make no difference on 32-bit platforms because duk_tval is
  7055. * 8 bytes or more anyway.
  7056. */
  7057. duk_tval v;
  7058. duk_propaccessor a;
  7059. };
  7060. struct duk_propdesc {
  7061. /* read-only values 'lifted' for ease of use */
  7062. duk_small_int_t flags;
  7063. duk_hobject *get;
  7064. duk_hobject *set;
  7065. /* for updating (all are set to < 0 for virtual properties) */
  7066. duk_int_t e_idx; /* prop index in 'entry part', < 0 if not there */
  7067. duk_int_t h_idx; /* prop index in 'hash part', < 0 if not there */
  7068. duk_int_t a_idx; /* prop index in 'array part', < 0 if not there */
  7069. };
  7070. struct duk_hobject {
  7071. duk_heaphdr hdr;
  7072. /*
  7073. * 'props' contains {key,value,flags} entries, optional array entries, and
  7074. * an optional hash lookup table for non-array entries in a single 'sliced'
  7075. * allocation. There are several layout options, which differ slightly in
  7076. * generated code size/speed and alignment/padding; duk_features.h selects
  7077. * the layout used.
  7078. *
  7079. * Layout 1 (DUK_USE_HOBJECT_LAYOUT_1):
  7080. *
  7081. * e_size * sizeof(duk_hstring *) bytes of entry keys (e_next gc reachable)
  7082. * e_size * sizeof(duk_propvalue) bytes of entry values (e_next gc reachable)
  7083. * e_size * sizeof(duk_uint8_t) bytes of entry flags (e_next gc reachable)
  7084. * a_size * sizeof(duk_tval) bytes of (opt) array values (plain only) (all gc reachable)
  7085. * h_size * sizeof(duk_uint32_t) bytes of (opt) hash indexes to entries (e_size),
  7086. * 0xffffffffUL = unused, 0xfffffffeUL = deleted
  7087. *
  7088. * Layout 2 (DUK_USE_HOBJECT_LAYOUT_2):
  7089. *
  7090. * e_size * sizeof(duk_propvalue) bytes of entry values (e_next gc reachable)
  7091. * e_size * sizeof(duk_hstring *) bytes of entry keys (e_next gc reachable)
  7092. * e_size * sizeof(duk_uint8_t) + pad bytes of entry flags (e_next gc reachable)
  7093. * a_size * sizeof(duk_tval) bytes of (opt) array values (plain only) (all gc reachable)
  7094. * h_size * sizeof(duk_uint32_t) bytes of (opt) hash indexes to entries (e_size),
  7095. * 0xffffffffUL = unused, 0xfffffffeUL = deleted
  7096. *
  7097. * Layout 3 (DUK_USE_HOBJECT_LAYOUT_3):
  7098. *
  7099. * e_size * sizeof(duk_propvalue) bytes of entry values (e_next gc reachable)
  7100. * a_size * sizeof(duk_tval) bytes of (opt) array values (plain only) (all gc reachable)
  7101. * e_size * sizeof(duk_hstring *) bytes of entry keys (e_next gc reachable)
  7102. * h_size * sizeof(duk_uint32_t) bytes of (opt) hash indexes to entries (e_size),
  7103. * 0xffffffffUL = unused, 0xfffffffeUL = deleted
  7104. * e_size * sizeof(duk_uint8_t) bytes of entry flags (e_next gc reachable)
  7105. *
  7106. * In layout 1, the 'e_next' count is rounded to 4 or 8 on platforms
  7107. * requiring 4 or 8 byte alignment. This ensures proper alignment
  7108. * for the entries, at the cost of memory footprint. However, it's
  7109. * probably preferable to use another layout on such platforms instead.
  7110. *
  7111. * In layout 2, the key and value parts are swapped to avoid padding
  7112. * the key array on platforms requiring alignment by 8. The flags part
  7113. * is padded to get alignment for array entries. The 'e_next' count does
  7114. * not need to be rounded as in layout 1.
  7115. *
  7116. * In layout 3, entry values and array values are always aligned properly,
  7117. * and assuming pointers are at most 8 bytes, so are the entry keys. Hash
  7118. * indices will be properly aligned (assuming pointers are at least 4 bytes).
  7119. * Finally, flags don't need additional alignment. This layout provides
  7120. * compact allocations without padding (even on platforms with alignment
  7121. * requirements) at the cost of a bit slower lookups.
  7122. *
  7123. * Objects with few keys don't have a hash index; keys are looked up linearly,
  7124. * which is cache efficient because the keys are consecutive. Larger objects
  7125. * have a hash index part which contains integer indexes to the entries part.
  7126. *
  7127. * A single allocation reduces memory allocation overhead but requires more
  7128. * work when any part needs to be resized. A sliced allocation for entries
  7129. * makes linear key matching faster on most platforms (more locality) and
  7130. * skimps on flags size (which would be followed by 3 bytes of padding in
  7131. * most architectures if entries were placed in a struct).
  7132. *
  7133. * 'props' also contains internal properties distinguished with a non-BMP
  7134. * prefix. Often used properties should be placed early in 'props' whenever
  7135. * possible to make accessing them as fast a possible.
  7136. */
  7137. #if defined(DUK_USE_HEAPPTR16)
  7138. /* Located in duk_heaphdr h_extra16. Subclasses of duk_hobject (like
  7139. * duk_hcompiledfunction) are not free to use h_extra16 for this reason.
  7140. */
  7141. #else
  7142. duk_uint8_t *props;
  7143. #endif
  7144. /* prototype: the only internal property lifted outside 'e' as it is so central */
  7145. #if defined(DUK_USE_HEAPPTR16)
  7146. duk_uint16_t prototype16;
  7147. #else
  7148. duk_hobject *prototype;
  7149. #endif
  7150. #if defined(DUK_USE_OBJSIZES16)
  7151. duk_uint16_t e_size16;
  7152. duk_uint16_t e_next16;
  7153. duk_uint16_t a_size16;
  7154. #if defined(DUK_USE_HOBJECT_HASH_PART)
  7155. duk_uint16_t h_size16;
  7156. #endif
  7157. #else
  7158. duk_uint32_t e_size; /* entry part size */
  7159. duk_uint32_t e_next; /* index for next new key ([0,e_next[ are gc reachable) */
  7160. duk_uint32_t a_size; /* array part size (entirely gc reachable) */
  7161. #if defined(DUK_USE_HOBJECT_HASH_PART)
  7162. duk_uint32_t h_size; /* hash part size or 0 if unused */
  7163. #endif
  7164. #endif
  7165. };
  7166. /*
  7167. * Exposed data
  7168. */
  7169. #if !defined(DUK_SINGLE_FILE)
  7170. DUK_INTERNAL_DECL duk_uint8_t duk_class_number_to_stridx[32];
  7171. #endif /* !DUK_SINGLE_FILE */
  7172. /*
  7173. * Prototypes
  7174. */
  7175. /* alloc and init */
  7176. DUK_INTERNAL_DECL duk_hobject *duk_hobject_alloc(duk_heap *heap, duk_uint_t hobject_flags);
  7177. #if 0 /* unused */
  7178. DUK_INTERNAL_DECL duk_hobject *duk_hobject_alloc_checked(duk_hthread *thr, duk_uint_t hobject_flags);
  7179. #endif
  7180. DUK_INTERNAL_DECL duk_hcompiledfunction *duk_hcompiledfunction_alloc(duk_heap *heap, duk_uint_t hobject_flags);
  7181. DUK_INTERNAL_DECL duk_hnativefunction *duk_hnativefunction_alloc(duk_heap *heap, duk_uint_t hobject_flags);
  7182. DUK_INTERNAL duk_hbufferobject *duk_hbufferobject_alloc(duk_heap *heap, duk_uint_t hobject_flags);
  7183. DUK_INTERNAL_DECL duk_hthread *duk_hthread_alloc(duk_heap *heap, duk_uint_t hobject_flags);
  7184. /* low-level property functions */
  7185. 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);
  7186. DUK_INTERNAL_DECL duk_tval *duk_hobject_find_existing_entry_tval_ptr(duk_heap *heap, duk_hobject *obj, duk_hstring *key);
  7187. 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);
  7188. DUK_INTERNAL_DECL duk_tval *duk_hobject_find_existing_array_entry_tval_ptr(duk_heap *heap, duk_hobject *obj, duk_uarridx_t i);
  7189. /* core property functions */
  7190. DUK_INTERNAL_DECL duk_bool_t duk_hobject_getprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key);
  7191. 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);
  7192. 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);
  7193. DUK_INTERNAL_DECL duk_bool_t duk_hobject_hasprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key);
  7194. /* internal property functions */
  7195. #define DUK_DELPROP_FLAG_THROW (1 << 0)
  7196. #define DUK_DELPROP_FLAG_FORCE (1 << 1)
  7197. DUK_INTERNAL_DECL duk_bool_t duk_hobject_delprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags);
  7198. DUK_INTERNAL_DECL duk_bool_t duk_hobject_hasprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key);
  7199. DUK_INTERNAL_DECL void duk_hobject_define_property_internal(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags);
  7200. 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);
  7201. 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);
  7202. DUK_INTERNAL_DECL void duk_hobject_set_length(duk_hthread *thr, duk_hobject *obj, duk_uint32_t length); /* XXX: duk_uarridx_t? */
  7203. DUK_INTERNAL_DECL void duk_hobject_set_length_zero(duk_hthread *thr, duk_hobject *obj);
  7204. DUK_INTERNAL_DECL duk_uint32_t duk_hobject_get_length(duk_hthread *thr, duk_hobject *obj); /* XXX: duk_uarridx_t? */
  7205. /* helpers for defineProperty() and defineProperties() */
  7206. DUK_INTERNAL_DECL
  7207. void duk_hobject_prepare_property_descriptor(duk_context *ctx,
  7208. duk_idx_t idx_in,
  7209. duk_uint_t *out_defprop_flags,
  7210. duk_idx_t *out_idx_value,
  7211. duk_hobject **out_getter,
  7212. duk_hobject **out_setter);
  7213. DUK_INTERNAL_DECL
  7214. void duk_hobject_define_property_helper(duk_context *ctx,
  7215. duk_uint_t defprop_flags,
  7216. duk_hobject *obj,
  7217. duk_hstring *key,
  7218. duk_idx_t idx_value,
  7219. duk_hobject *get,
  7220. duk_hobject *set);
  7221. /* Object built-in methods */
  7222. DUK_INTERNAL_DECL duk_ret_t duk_hobject_object_get_own_property_descriptor(duk_context *ctx);
  7223. DUK_INTERNAL_DECL void duk_hobject_object_seal_freeze_helper(duk_hthread *thr, duk_hobject *obj, duk_bool_t is_freeze);
  7224. DUK_INTERNAL_DECL duk_bool_t duk_hobject_object_is_sealed_frozen_helper(duk_hthread *thr, duk_hobject *obj, duk_bool_t is_frozen);
  7225. DUK_INTERNAL_DECL duk_bool_t duk_hobject_object_ownprop_helper(duk_context *ctx, duk_small_uint_t required_desc_flags);
  7226. /* internal properties */
  7227. DUK_INTERNAL_DECL duk_bool_t duk_hobject_get_internal_value(duk_heap *heap, duk_hobject *obj, duk_tval *tv);
  7228. DUK_INTERNAL_DECL duk_hstring *duk_hobject_get_internal_value_string(duk_heap *heap, duk_hobject *obj);
  7229. /* hobject management functions */
  7230. DUK_INTERNAL_DECL void duk_hobject_compact_props(duk_hthread *thr, duk_hobject *obj);
  7231. /* ES6 proxy */
  7232. #if defined(DUK_USE_ES6_PROXY)
  7233. DUK_INTERNAL_DECL duk_bool_t duk_hobject_proxy_check(duk_hthread *thr, duk_hobject *obj, duk_hobject **out_target, duk_hobject **out_handler);
  7234. #endif
  7235. /* enumeration */
  7236. DUK_INTERNAL_DECL void duk_hobject_enumerator_create(duk_context *ctx, duk_small_uint_t enum_flags);
  7237. DUK_INTERNAL_DECL duk_ret_t duk_hobject_get_enumerated_keys(duk_context *ctx, duk_small_uint_t enum_flags);
  7238. DUK_INTERNAL_DECL duk_bool_t duk_hobject_enumerator_next(duk_context *ctx, duk_bool_t get_value);
  7239. /* macros */
  7240. DUK_INTERNAL_DECL void duk_hobject_set_prototype(duk_hthread *thr, duk_hobject *h, duk_hobject *p);
  7241. /* finalization */
  7242. DUK_INTERNAL_DECL void duk_hobject_run_finalizer(duk_hthread *thr, duk_hobject *obj);
  7243. /* pc2line */
  7244. #if defined(DUK_USE_PC2LINE)
  7245. DUK_INTERNAL_DECL void duk_hobject_pc2line_pack(duk_hthread *thr, duk_compiler_instr *instrs, duk_uint_fast32_t length);
  7246. DUK_INTERNAL_DECL duk_uint_fast32_t duk_hobject_pc2line_query(duk_context *ctx, duk_idx_t idx_func, duk_uint_fast32_t pc);
  7247. #endif
  7248. /* misc */
  7249. 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);
  7250. #endif /* DUK_HOBJECT_H_INCLUDED */
  7251. #line 1 "duk_hcompiledfunction.h"
  7252. /*
  7253. * Heap compiled function (Ecmascript function) representation.
  7254. *
  7255. * There is a single data buffer containing the Ecmascript function's
  7256. * bytecode, constants, and inner functions.
  7257. */
  7258. #ifndef DUK_HCOMPILEDFUNCTION_H_INCLUDED
  7259. #define DUK_HCOMPILEDFUNCTION_H_INCLUDED
  7260. /*
  7261. * Field accessor macros
  7262. */
  7263. /* XXX: casts could be improved, especially for GET/SET DATA */
  7264. #if defined(DUK_USE_HEAPPTR16)
  7265. #define DUK_HCOMPILEDFUNCTION_GET_DATA(heap,h) \
  7266. ((duk_hbuffer_fixed *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->data16))
  7267. #define DUK_HCOMPILEDFUNCTION_SET_DATA(heap,h,v) do { \
  7268. (h)->data16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \
  7269. } while (0)
  7270. #define DUK_HCOMPILEDFUNCTION_GET_FUNCS(heap,h) \
  7271. ((duk_hobject **) (DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->funcs16)))
  7272. #define DUK_HCOMPILEDFUNCTION_SET_FUNCS(heap,h,v) do { \
  7273. (h)->funcs16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \
  7274. } while (0)
  7275. #define DUK_HCOMPILEDFUNCTION_GET_BYTECODE(heap,h) \
  7276. ((duk_instr_t *) (DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->bytecode16)))
  7277. #define DUK_HCOMPILEDFUNCTION_SET_BYTECODE(heap,h,v) do { \
  7278. (h)->bytecode16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \
  7279. } while (0)
  7280. #else
  7281. #define DUK_HCOMPILEDFUNCTION_GET_DATA(heap,h) \
  7282. ((duk_hbuffer_fixed *) (h)->data)
  7283. #define DUK_HCOMPILEDFUNCTION_SET_DATA(heap,h,v) do { \
  7284. (h)->data = (duk_hbuffer *) (v); \
  7285. } while (0)
  7286. #define DUK_HCOMPILEDFUNCTION_GET_FUNCS(heap,h) \
  7287. ((h)->funcs)
  7288. #define DUK_HCOMPILEDFUNCTION_SET_FUNCS(heap,h,v) do { \
  7289. (h)->funcs = (v); \
  7290. } while (0)
  7291. #define DUK_HCOMPILEDFUNCTION_GET_BYTECODE(heap,h) \
  7292. ((h)->bytecode)
  7293. #define DUK_HCOMPILEDFUNCTION_SET_BYTECODE(heap,h,v) do { \
  7294. (h)->bytecode = (v); \
  7295. } while (0)
  7296. #endif
  7297. /*
  7298. * Accessor macros for function specific data areas
  7299. */
  7300. /* Note: assumes 'data' is always a fixed buffer */
  7301. #define DUK_HCOMPILEDFUNCTION_GET_BUFFER_BASE(heap,h) \
  7302. DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), DUK_HCOMPILEDFUNCTION_GET_DATA((heap), (h)))
  7303. #define DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(heap,h) \
  7304. ((duk_tval *) DUK_HCOMPILEDFUNCTION_GET_BUFFER_BASE((heap), (h)))
  7305. #define DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(heap,h) \
  7306. DUK_HCOMPILEDFUNCTION_GET_FUNCS((heap), (h))
  7307. #define DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(heap,h) \
  7308. DUK_HCOMPILEDFUNCTION_GET_BYTECODE((heap), (h))
  7309. #define DUK_HCOMPILEDFUNCTION_GET_CONSTS_END(heap,h) \
  7310. ((duk_tval *) DUK_HCOMPILEDFUNCTION_GET_FUNCS((heap), (h)))
  7311. #define DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(heap,h) \
  7312. ((duk_hobject **) DUK_HCOMPILEDFUNCTION_GET_BYTECODE((heap), (h)))
  7313. /* XXX: double evaluation of DUK_HCOMPILEDFUNCTION_GET_DATA() */
  7314. #define DUK_HCOMPILEDFUNCTION_GET_CODE_END(heap,h) \
  7315. ((duk_instr_t *) (DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), DUK_HCOMPILEDFUNCTION_GET_DATA((heap), (h))) + \
  7316. DUK_HBUFFER_GET_SIZE((duk_hbuffer *) DUK_HCOMPILEDFUNCTION_GET_DATA((heap), h))))
  7317. #define DUK_HCOMPILEDFUNCTION_GET_CONSTS_SIZE(heap,h) \
  7318. ( \
  7319. (duk_size_t) \
  7320. ( \
  7321. ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_END((heap), (h))) - \
  7322. ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE((heap), (h))) \
  7323. ) \
  7324. )
  7325. #define DUK_HCOMPILEDFUNCTION_GET_FUNCS_SIZE(heap,h) \
  7326. ( \
  7327. (duk_size_t) \
  7328. ( \
  7329. ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_END((heap), (h))) - \
  7330. ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE((heap), (h))) \
  7331. ) \
  7332. )
  7333. #define DUK_HCOMPILEDFUNCTION_GET_CODE_SIZE(heap,h) \
  7334. ( \
  7335. (duk_size_t) \
  7336. ( \
  7337. ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_CODE_END((heap),(h))) - \
  7338. ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_CODE_BASE((heap),(h))) \
  7339. ) \
  7340. )
  7341. #define DUK_HCOMPILEDFUNCTION_GET_CONSTS_COUNT(heap,h) \
  7342. ((duk_size_t) (DUK_HCOMPILEDFUNCTION_GET_CONSTS_SIZE((heap), (h)) / sizeof(duk_tval)))
  7343. #define DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(heap,h) \
  7344. ((duk_size_t) (DUK_HCOMPILEDFUNCTION_GET_FUNCS_SIZE((heap), (h)) / sizeof(duk_hobject *)))
  7345. #define DUK_HCOMPILEDFUNCTION_GET_CODE_COUNT(heap,h) \
  7346. ((duk_size_t) (DUK_HCOMPILEDFUNCTION_GET_CODE_SIZE((heap), (h)) / sizeof(duk_instr_t)))
  7347. /*
  7348. * Main struct
  7349. */
  7350. struct duk_hcompiledfunction {
  7351. /* shared object part */
  7352. duk_hobject obj;
  7353. /*
  7354. * Pointers to function data area for faster access. Function
  7355. * data is a buffer shared between all closures of the same
  7356. * "template" function. The data buffer is always fixed (non-
  7357. * dynamic, hence stable), with a layout as follows:
  7358. *
  7359. * constants (duk_tval)
  7360. * inner functions (duk_hobject *)
  7361. * bytecode (duk_instr_t)
  7362. *
  7363. * Note: bytecode end address can be computed from 'data' buffer
  7364. * size. It is not strictly necessary functionally, assuming
  7365. * bytecode never jumps outside its allocated area. However,
  7366. * it's a safety/robustness feature for avoiding the chance of
  7367. * executing random data as bytecode due to a compiler error.
  7368. *
  7369. * Note: values in the data buffer must be incref'd (they will
  7370. * be decref'd on release) for every compiledfunction referring
  7371. * to the 'data' element.
  7372. */
  7373. /* Data area, fixed allocation, stable data ptrs. */
  7374. #if defined(DUK_USE_HEAPPTR16)
  7375. duk_uint16_t data16;
  7376. #else
  7377. duk_hbuffer *data;
  7378. #endif
  7379. /* No need for constants pointer (= same as data).
  7380. *
  7381. * When using 16-bit packing alignment to 4 is nice. 'funcs' will be
  7382. * 4-byte aligned because 'constants' are duk_tvals. For now the
  7383. * inner function pointers are not compressed, so that 'bytecode' will
  7384. * also be 4-byte aligned.
  7385. */
  7386. #if defined(DUK_USE_HEAPPTR16)
  7387. duk_uint16_t funcs16;
  7388. duk_uint16_t bytecode16;
  7389. #else
  7390. duk_hobject **funcs;
  7391. duk_instr_t *bytecode;
  7392. #endif
  7393. /*
  7394. * 'nregs' registers are allocated on function entry, at most 'nargs'
  7395. * are initialized to arguments, and the rest to undefined. Arguments
  7396. * above 'nregs' are not mapped to registers. All registers in the
  7397. * active stack range must be initialized because they are GC reachable.
  7398. * 'nargs' is needed so that if the function is given more than 'nargs'
  7399. * arguments, the additional arguments do not 'clobber' registers
  7400. * beyond 'nregs' which must be consistently initialized to undefined.
  7401. *
  7402. * Usually there is no need to know which registers are mapped to
  7403. * local variables. Registers may be allocated to variable in any
  7404. * way (even including gaps). However, a register-variable mapping
  7405. * must be the same for the duration of the function execution and
  7406. * the register cannot be used for anything else.
  7407. *
  7408. * When looking up variables by name, the '_Varmap' map is used.
  7409. * When an activation closes, registers mapped to arguments are
  7410. * copied into the environment record based on the same map. The
  7411. * reverse map (from register to variable) is not currently needed
  7412. * at run time, except for debugging, so it is not maintained.
  7413. */
  7414. duk_uint16_t nregs; /* regs to allocate */
  7415. duk_uint16_t nargs; /* number of arguments allocated to regs */
  7416. /*
  7417. * Additional control information is placed into the object itself
  7418. * as internal properties to avoid unnecessary fields for the
  7419. * majority of functions. The compiler tries to omit internal
  7420. * control fields when possible.
  7421. *
  7422. * Function templates:
  7423. *
  7424. * {
  7425. * name: "func", // declaration, named function expressions
  7426. * fileName: <debug info for creating nice errors>
  7427. * _Varmap: { "arg1": 0, "arg2": 1, "varname": 2 },
  7428. * _Formals: [ "arg1", "arg2" ],
  7429. * _Source: "function func(arg1, arg2) { ... }",
  7430. * _Pc2line: <debug info for pc-to-line mapping>,
  7431. * }
  7432. *
  7433. * Function instances:
  7434. *
  7435. * {
  7436. * length: 2,
  7437. * prototype: { constructor: <func> },
  7438. * caller: <thrower>,
  7439. * arguments: <thrower>,
  7440. * name: "func", // declaration, named function expressions
  7441. * fileName: <debug info for creating nice errors>
  7442. * _Varmap: { "arg1": 0, "arg2": 1, "varname": 2 },
  7443. * _Formals: [ "arg1", "arg2" ],
  7444. * _Source: "function func(arg1, arg2) { ... }",
  7445. * _Pc2line: <debug info for pc-to-line mapping>,
  7446. * _Varenv: <variable environment of closure>,
  7447. * _Lexenv: <lexical environment of closure (if differs from _Varenv)>
  7448. * }
  7449. *
  7450. * More detailed description of these properties can be found
  7451. * in the documentation.
  7452. */
  7453. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  7454. /* Line number range for function. Needed during debugging to
  7455. * determine active breakpoints.
  7456. */
  7457. duk_uint32_t start_line;
  7458. duk_uint32_t end_line;
  7459. #endif
  7460. };
  7461. #endif /* DUK_HCOMPILEDFUNCTION_H_INCLUDED */
  7462. #line 1 "duk_hnativefunction.h"
  7463. /*
  7464. * Heap native function representation.
  7465. */
  7466. #ifndef DUK_HNATIVEFUNCTION_H_INCLUDED
  7467. #define DUK_HNATIVEFUNCTION_H_INCLUDED
  7468. #define DUK_HNATIVEFUNCTION_NARGS_VARARGS ((duk_int16_t) -1)
  7469. #define DUK_HNATIVEFUNCTION_NARGS_MAX ((duk_int16_t) 0x7fff)
  7470. struct duk_hnativefunction {
  7471. /* shared object part */
  7472. duk_hobject obj;
  7473. duk_c_function func;
  7474. duk_int16_t nargs;
  7475. duk_int16_t magic;
  7476. /* The 'magic' field allows an opaque 16-bit field to be accessed by the
  7477. * Duktape/C function. This allows, for instance, the same native function
  7478. * to be used for a set of very similar functions, with the 'magic' field
  7479. * providing the necessary non-argument flags / values to guide the behavior
  7480. * of the native function. The value is signed on purpose: it is easier to
  7481. * convert a signed value to unsigned (simply AND with 0xffff) than vice
  7482. * versa.
  7483. *
  7484. * Note: cannot place nargs/magic into the heaphdr flags, because
  7485. * duk_hobject takes almost all flags already (and needs the spare).
  7486. */
  7487. };
  7488. #endif /* DUK_HNATIVEFUNCTION_H_INCLUDED */
  7489. #line 1 "duk_hbufferobject.h"
  7490. /*
  7491. * Heap Buffer object representation. Used for all Buffer variants.
  7492. */
  7493. #ifndef DUK_HBUFFEROBJECT_H_INCLUDED
  7494. #define DUK_HBUFFEROBJECT_H_INCLUDED
  7495. /* All element accessors are host endian now (driven by TypedArray spec). */
  7496. #define DUK_HBUFFEROBJECT_ELEM_UINT8 0
  7497. #define DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED 1
  7498. #define DUK_HBUFFEROBJECT_ELEM_INT8 2
  7499. #define DUK_HBUFFEROBJECT_ELEM_UINT16 3
  7500. #define DUK_HBUFFEROBJECT_ELEM_INT16 4
  7501. #define DUK_HBUFFEROBJECT_ELEM_UINT32 5
  7502. #define DUK_HBUFFEROBJECT_ELEM_INT32 6
  7503. #define DUK_HBUFFEROBJECT_ELEM_FLOAT32 7
  7504. #define DUK_HBUFFEROBJECT_ELEM_FLOAT64 8
  7505. #define DUK_HBUFFEROBJECT_ELEM_MAX 8
  7506. #define DUK_ASSERT_HBUFFEROBJECT_VALID(h) do { \
  7507. DUK_ASSERT((h) != NULL); \
  7508. DUK_ASSERT((h)->shift <= 3); \
  7509. DUK_ASSERT((h)->elem_type <= DUK_HBUFFEROBJECT_ELEM_MAX); \
  7510. DUK_ASSERT(((h)->shift == 0 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8) || \
  7511. ((h)->shift == 0 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED) || \
  7512. ((h)->shift == 0 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_INT8) || \
  7513. ((h)->shift == 1 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT16) || \
  7514. ((h)->shift == 1 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_INT16) || \
  7515. ((h)->shift == 2 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT32) || \
  7516. ((h)->shift == 2 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_INT32) || \
  7517. ((h)->shift == 2 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_FLOAT32) || \
  7518. ((h)->shift == 3 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_FLOAT64)); \
  7519. DUK_ASSERT((h)->is_view == 0 || (h)->is_view == 1); \
  7520. DUK_ASSERT(DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) (h))); \
  7521. if ((h)->buf == NULL) { \
  7522. DUK_ASSERT((h)->offset == 0); \
  7523. DUK_ASSERT((h)->length == 0); \
  7524. } else { \
  7525. /* No assertions for offset or length; in particular, \
  7526. * it's OK for length to be longer than underlying \
  7527. * buffer. \
  7528. */ \
  7529. } \
  7530. } while (0)
  7531. /* Get the current data pointer (caller must ensure buf != NULL) as a
  7532. * duk_uint8_t ptr.
  7533. */
  7534. #define DUK_HBUFFEROBJECT_GET_SLICE_BASE(heap,h) \
  7535. (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \
  7536. (((duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR((heap), (h)->buf)) + (h)->offset))
  7537. /* True if slice is full, i.e. offset is zero and length covers the entire
  7538. * buffer. This status may change independently of the duk_hbufferobject if
  7539. * the underlying buffer is dynamic and changes without the hbufferobject
  7540. * being changed.
  7541. */
  7542. #define DUK_HBUFFEROBJECT_FULL_SLICE(h) \
  7543. (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \
  7544. ((h)->offset == 0 && (h)->length == DUK_HBUFFER_GET_SIZE((h)->buf)))
  7545. /* Validate that the whole slice [0,length[ is contained in the underlying
  7546. * buffer. Caller must ensure 'buf' != NULL.
  7547. */
  7548. #define DUK_HBUFFEROBJECT_VALID_SLICE(h) \
  7549. (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \
  7550. ((h)->offset + (h)->length <= DUK_HBUFFER_GET_SIZE((h)->buf)))
  7551. /* Validate byte read/write for virtual 'offset', i.e. check that the
  7552. * offset, taking into account h->offset, is within the underlying
  7553. * buffer size. This is a safety check which is needed to ensure
  7554. * that even a misconfigured duk_hbufferobject never causes memory
  7555. * unsafe behavior (e.g. if an underlying dynamic buffer changes
  7556. * after being setup). Caller must ensure 'buf' != NULL.
  7557. */
  7558. #define DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_INCL(h,off) \
  7559. (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \
  7560. ((h)->offset + (off) < DUK_HBUFFER_GET_SIZE((h)->buf)))
  7561. #define DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h,off) \
  7562. (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \
  7563. ((h)->offset + (off) <= DUK_HBUFFER_GET_SIZE((h)->buf)))
  7564. /* Clamp an input byte length (already assumed to be within the nominal
  7565. * duk_hbufferobject 'length') to the current dynamic buffer limits to
  7566. * yield a byte length limit that's safe for memory accesses. This value
  7567. * can be invalidated by any side effect because it may trigger a user
  7568. * callback that resizes the underlying buffer.
  7569. */
  7570. #define DUK_HBUFFEROBJECT_CLAMP_BYTELENGTH(h,len) \
  7571. (DUK_ASSERT_EXPR((h) != NULL), \
  7572. duk_hbufferobject_clamp_bytelength((h), (len)))
  7573. struct duk_hbufferobject {
  7574. /* Shared object part. */
  7575. duk_hobject obj;
  7576. /* Underlying buffer (refcounted), may be NULL. */
  7577. duk_hbuffer *buf;
  7578. /* Slice and accessor information.
  7579. *
  7580. * Because the underlying buffer may be dynamic, these may be
  7581. * invalidated by the buffer being modified so that both offset
  7582. * and length should be validated before every access. Behavior
  7583. * when the underlying buffer has changed doesn't need to be clean:
  7584. * virtual 'length' doesn't need to be affected, reads can return
  7585. * zero/NaN, and writes can be ignored.
  7586. *
  7587. * Note that a data pointer cannot be precomputed because 'buf' may
  7588. * be dynamic and its pointer unstable.
  7589. */
  7590. duk_uint_t offset; /* byte offset to buf */
  7591. duk_uint_t length; /* byte index limit for element access, exclusive */
  7592. duk_uint8_t shift; /* element size shift:
  7593. * 0 = u8/i8
  7594. * 1 = u16/i16
  7595. * 2 = u32/i32/float
  7596. * 3 = double
  7597. */
  7598. duk_uint8_t elem_type; /* element type */
  7599. duk_uint8_t is_view;
  7600. };
  7601. DUK_INTERNAL_DECL duk_uint_t duk_hbufferobject_clamp_bytelength(duk_hbufferobject *h_bufobj, duk_uint_t len);
  7602. 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);
  7603. 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);
  7604. #endif /* DUK_HBUFFEROBJECT_H_INCLUDED */
  7605. #line 1 "duk_hthread.h"
  7606. /*
  7607. * Heap thread object representation.
  7608. *
  7609. * duk_hthread is also the 'context' (duk_context) for exposed APIs
  7610. * which mostly operate on the topmost frame of the value stack.
  7611. */
  7612. #ifndef DUK_HTHREAD_H_INCLUDED
  7613. #define DUK_HTHREAD_H_INCLUDED
  7614. /*
  7615. * Stack constants
  7616. */
  7617. #define DUK_VALSTACK_GROW_STEP 128 /* roughly 1 kiB */
  7618. #define DUK_VALSTACK_SHRINK_THRESHOLD 256 /* roughly 2 kiB */
  7619. #define DUK_VALSTACK_SHRINK_SPARE 64 /* roughly 0.5 kiB */
  7620. #define DUK_VALSTACK_INITIAL_SIZE 128 /* roughly 1.0 kiB -> but rounds up to DUK_VALSTACK_GROW_STEP in practice */
  7621. #define DUK_VALSTACK_INTERNAL_EXTRA 64 /* internal extra elements assumed on function entry,
  7622. * always added to user-defined 'extra' for e.g. the
  7623. * duk_check_stack() call.
  7624. */
  7625. #define DUK_VALSTACK_API_ENTRY_MINIMUM DUK_API_ENTRY_STACK
  7626. /* number of elements guaranteed to be user accessible
  7627. * (in addition to call arguments) on Duktape/C function entry.
  7628. */
  7629. /* Note: DUK_VALSTACK_INITIAL_SIZE must be >= DUK_VALSTACK_API_ENTRY_MINIMUM
  7630. * + DUK_VALSTACK_INTERNAL_EXTRA so that the initial stack conforms to spare
  7631. * requirements.
  7632. */
  7633. #define DUK_VALSTACK_DEFAULT_MAX 1000000L
  7634. #define DUK_CALLSTACK_GROW_STEP 8 /* roughly 256 bytes */
  7635. #define DUK_CALLSTACK_SHRINK_THRESHOLD 16 /* roughly 512 bytes */
  7636. #define DUK_CALLSTACK_SHRINK_SPARE 8 /* roughly 256 bytes */
  7637. #define DUK_CALLSTACK_INITIAL_SIZE 8
  7638. #define DUK_CALLSTACK_DEFAULT_MAX 10000L
  7639. #define DUK_CATCHSTACK_GROW_STEP 4 /* roughly 64 bytes */
  7640. #define DUK_CATCHSTACK_SHRINK_THRESHOLD 8 /* roughly 128 bytes */
  7641. #define DUK_CATCHSTACK_SHRINK_SPARE 4 /* roughly 64 bytes */
  7642. #define DUK_CATCHSTACK_INITIAL_SIZE 4
  7643. #define DUK_CATCHSTACK_DEFAULT_MAX 10000L
  7644. /*
  7645. * Activation defines
  7646. */
  7647. #define DUK_ACT_FLAG_STRICT (1 << 0) /* function executes in strict mode */
  7648. #define DUK_ACT_FLAG_TAILCALLED (1 << 1) /* activation has tailcalled one or more times */
  7649. #define DUK_ACT_FLAG_CONSTRUCT (1 << 2) /* function executes as a constructor (called via "new") */
  7650. #define DUK_ACT_FLAG_PREVENT_YIELD (1 << 3) /* activation prevents yield (native call or "new") */
  7651. #define DUK_ACT_FLAG_DIRECT_EVAL (1 << 4) /* activation is a direct eval call */
  7652. #define DUK_ACT_FLAG_BREAKPOINT_ACTIVE (1 << 5) /* activation has active breakpoint(s) */
  7653. #define DUK_ACT_GET_FUNC(act) ((act)->func)
  7654. /*
  7655. * Flags for __FILE__ / __LINE__ registered into tracedata
  7656. */
  7657. #define DUK_TB_FLAG_NOBLAME_FILELINE (1 << 0) /* don't report __FILE__ / __LINE__ as fileName/lineNumber */
  7658. /*
  7659. * Catcher defines
  7660. */
  7661. /* flags field: LLLLLLFT, L = label (24 bits), F = flags (4 bits), T = type (4 bits) */
  7662. #define DUK_CAT_TYPE_MASK 0x0000000fUL
  7663. #define DUK_CAT_TYPE_BITS 4
  7664. #define DUK_CAT_LABEL_MASK 0xffffff00UL
  7665. #define DUK_CAT_LABEL_BITS 24
  7666. #define DUK_CAT_LABEL_SHIFT 8
  7667. #define DUK_CAT_FLAG_CATCH_ENABLED (1 << 4) /* catch part will catch */
  7668. #define DUK_CAT_FLAG_FINALLY_ENABLED (1 << 5) /* finally part will catch */
  7669. #define DUK_CAT_FLAG_CATCH_BINDING_ENABLED (1 << 6) /* request to create catch binding */
  7670. #define DUK_CAT_FLAG_LEXENV_ACTIVE (1 << 7) /* catch or with binding is currently active */
  7671. #define DUK_CAT_TYPE_UNKNOWN 0
  7672. #define DUK_CAT_TYPE_TCF 1
  7673. #define DUK_CAT_TYPE_LABEL 2
  7674. #define DUK_CAT_GET_TYPE(c) ((c)->flags & DUK_CAT_TYPE_MASK)
  7675. #define DUK_CAT_GET_LABEL(c) (((c)->flags & DUK_CAT_LABEL_MASK) >> DUK_CAT_LABEL_SHIFT)
  7676. #define DUK_CAT_HAS_CATCH_ENABLED(c) ((c)->flags & DUK_CAT_FLAG_CATCH_ENABLED)
  7677. #define DUK_CAT_HAS_FINALLY_ENABLED(c) ((c)->flags & DUK_CAT_FLAG_FINALLY_ENABLED)
  7678. #define DUK_CAT_HAS_CATCH_BINDING_ENABLED(c) ((c)->flags & DUK_CAT_FLAG_CATCH_BINDING_ENABLED)
  7679. #define DUK_CAT_HAS_LEXENV_ACTIVE(c) ((c)->flags & DUK_CAT_FLAG_LEXENV_ACTIVE)
  7680. #define DUK_CAT_SET_CATCH_ENABLED(c) do { \
  7681. (c)->flags |= DUK_CAT_FLAG_CATCH_ENABLED; \
  7682. } while (0)
  7683. #define DUK_CAT_SET_FINALLY_ENABLED(c) do { \
  7684. (c)->flags |= DUK_CAT_FLAG_FINALLY_ENABLED; \
  7685. } while (0)
  7686. #define DUK_CAT_SET_CATCH_BINDING_ENABLED(c) do { \
  7687. (c)->flags |= DUK_CAT_FLAG_CATCH_BINDING_ENABLED; \
  7688. } while (0)
  7689. #define DUK_CAT_SET_LEXENV_ACTIVE(c) do { \
  7690. (c)->flags |= DUK_CAT_FLAG_LEXENV_ACTIVE; \
  7691. } while (0)
  7692. #define DUK_CAT_CLEAR_CATCH_ENABLED(c) do { \
  7693. (c)->flags &= ~DUK_CAT_FLAG_CATCH_ENABLED; \
  7694. } while (0)
  7695. #define DUK_CAT_CLEAR_FINALLY_ENABLED(c) do { \
  7696. (c)->flags &= ~DUK_CAT_FLAG_FINALLY_ENABLED; \
  7697. } while (0)
  7698. #define DUK_CAT_CLEAR_CATCH_BINDING_ENABLED(c) do { \
  7699. (c)->flags &= ~DUK_CAT_FLAG_CATCH_BINDING_ENABLED; \
  7700. } while (0)
  7701. #define DUK_CAT_CLEAR_LEXENV_ACTIVE(c) do { \
  7702. (c)->flags &= ~DUK_CAT_FLAG_LEXENV_ACTIVE; \
  7703. } while (0)
  7704. /*
  7705. * Thread defines
  7706. */
  7707. #if defined(DUK_USE_HEAPPTR16)
  7708. #define DUK_HTHREAD_GET_STRING(thr,idx) \
  7709. ((duk_hstring *) DUK_USE_HEAPPTR_DEC16((thr)->heap->heap_udata, (thr)->strs16[(idx)]))
  7710. #else
  7711. #define DUK_HTHREAD_GET_STRING(thr,idx) \
  7712. ((thr)->strs[(idx)])
  7713. #endif
  7714. #define DUK_HTHREAD_GET_CURRENT_ACTIVATION(thr) (&(thr)->callstack[(thr)->callstack_top - 1])
  7715. /* values for the state field */
  7716. #define DUK_HTHREAD_STATE_INACTIVE 1 /* thread not currently running */
  7717. #define DUK_HTHREAD_STATE_RUNNING 2 /* thread currently running (only one at a time) */
  7718. #define DUK_HTHREAD_STATE_RESUMED 3 /* thread resumed another thread (active but not running) */
  7719. #define DUK_HTHREAD_STATE_YIELDED 4 /* thread has yielded */
  7720. #define DUK_HTHREAD_STATE_TERMINATED 5 /* thread has terminated */
  7721. /*
  7722. * Assert context is valid: non-NULL pointer, fields look sane.
  7723. *
  7724. * This is used by public API call entrypoints to catch invalid 'ctx' pointers
  7725. * as early as possible; invalid 'ctx' pointers cause very odd and difficult to
  7726. * diagnose behavior so it's worth checking even when the check is not 100%.
  7727. */
  7728. #define DUK_ASSERT_CTX_VALID(ctx) do { \
  7729. DUK_ASSERT((ctx) != NULL); \
  7730. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) (ctx)) == DUK_HTYPE_OBJECT); \
  7731. DUK_ASSERT(DUK_HOBJECT_IS_THREAD((duk_hobject *) (ctx))); \
  7732. DUK_ASSERT(((duk_hthread *) (ctx))->unused1 == 0); \
  7733. DUK_ASSERT(((duk_hthread *) (ctx))->unused2 == 0); \
  7734. DUK_ASSERT(((duk_hthread *) (ctx))->valstack != NULL); \
  7735. DUK_ASSERT(((duk_hthread *) (ctx))->valstack_end >= ((duk_hthread *) (ctx))->valstack); \
  7736. DUK_ASSERT(((duk_hthread *) (ctx))->valstack_top >= ((duk_hthread *) (ctx))->valstack); \
  7737. DUK_ASSERT(((duk_hthread *) (ctx))->valstack_top >= ((duk_hthread *) (ctx))->valstack_bottom); \
  7738. } while (0)
  7739. /*
  7740. * Struct defines
  7741. */
  7742. /* XXX: for a memory-code tradeoff, remove 'func' and make it's access either a function
  7743. * or a macro. This would make the activation 32 bytes long on 32-bit platforms again.
  7744. */
  7745. /* Note: it's nice if size is 2^N (at least for 32-bit platforms). */
  7746. struct duk_activation {
  7747. duk_tval tv_func; /* borrowed: full duk_tval for function being executed; for lightfuncs */
  7748. duk_hobject *func; /* borrowed: function being executed; for bound function calls, this is the final, real function, NULL for lightfuncs */
  7749. duk_hobject *var_env; /* current variable environment (may be NULL if delayed) */
  7750. duk_hobject *lex_env; /* current lexical environment (may be NULL if delayed) */
  7751. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  7752. /* Previous value of 'func' caller, restored when unwound. Only in use
  7753. * when 'func' is non-strict.
  7754. */
  7755. duk_hobject *prev_caller;
  7756. #endif
  7757. duk_small_uint_t flags;
  7758. duk_uint32_t pc; /* next instruction to execute */
  7759. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  7760. duk_uint32_t prev_line; /* needed for stepping */
  7761. #endif
  7762. /* idx_bottom and idx_retval are only used for book-keeping of
  7763. * Ecmascript-initiated calls, to allow returning to an Ecmascript
  7764. * function properly. They are duk_size_t to match the convention
  7765. * that value stack sizes are duk_size_t and local frame indices
  7766. * are duk_idx_t.
  7767. */
  7768. /* Bottom of valstack for this activation, used to reset
  7769. * valstack_bottom on return; index is absolute. Note:
  7770. * idx_top not needed because top is set to 'nregs' always
  7771. * when returning to an Ecmascript activation.
  7772. */
  7773. duk_size_t idx_bottom;
  7774. /* Return value when returning to this activation (points to caller
  7775. * reg, not callee reg); index is absolute (only set if activation is
  7776. * not topmost).
  7777. *
  7778. * Note: idx_bottom is always set, while idx_retval is only applicable
  7779. * for activations below the topmost one. Currently idx_retval for
  7780. * the topmost activation is considered garbage (and it not initialized
  7781. * on entry or cleared on return; may contain previous or garbage
  7782. * values).
  7783. */
  7784. duk_size_t idx_retval;
  7785. /* Current 'this' binding is the value just below idx_bottom.
  7786. * Previously, 'this' binding was handled with an index to the
  7787. * (calling) valstack. This works for everything except tail
  7788. * calls, which must not "cumulate" valstack temps.
  7789. */
  7790. };
  7791. /* Note: it's nice if size is 2^N (not 4x4 = 16 bytes on 32 bit) */
  7792. struct duk_catcher {
  7793. duk_hstring *h_varname; /* borrowed reference to catch variable name (or NULL if none) */
  7794. /* (reference is valid as long activation exists) */
  7795. duk_size_t callstack_index; /* callstack index of related activation */
  7796. duk_size_t idx_base; /* idx_base and idx_base+1 get completion value and type */
  7797. duk_uint32_t pc_base; /* resume execution from pc_base or pc_base+1 */
  7798. duk_uint32_t flags; /* type and control flags, label number */
  7799. };
  7800. struct duk_hthread {
  7801. /* shared object part */
  7802. duk_hobject obj;
  7803. /* backpointers */
  7804. duk_heap *heap;
  7805. /* current strictness flag: affects API calls */
  7806. duk_uint8_t strict;
  7807. duk_uint8_t state;
  7808. duk_uint8_t unused1;
  7809. duk_uint8_t unused2;
  7810. /* sanity limits */
  7811. duk_size_t valstack_max;
  7812. duk_size_t callstack_max;
  7813. duk_size_t catchstack_max;
  7814. /* XXX: valstack, callstack, and catchstack are currently assumed
  7815. * to have non-NULL pointers. Relaxing this would not lead to big
  7816. * benefits (except perhaps for terminated threads).
  7817. */
  7818. /* value stack: these are expressed as pointers for faster stack manipulation */
  7819. duk_tval *valstack; /* start of valstack allocation */
  7820. duk_tval *valstack_end; /* end of valstack allocation (exclusive) */
  7821. duk_tval *valstack_bottom; /* bottom of current frame */
  7822. duk_tval *valstack_top; /* top of current frame (exclusive) */
  7823. /* call stack */
  7824. duk_activation *callstack;
  7825. duk_size_t callstack_size; /* allocation size */
  7826. duk_size_t callstack_top; /* next to use, highest used is top - 1 */
  7827. duk_size_t callstack_preventcount; /* number of activation records in callstack preventing a yield */
  7828. /* catch stack */
  7829. duk_catcher *catchstack;
  7830. duk_size_t catchstack_size; /* allocation size */
  7831. duk_size_t catchstack_top; /* next to use, highest used is top - 1 */
  7832. /* yield/resume book-keeping */
  7833. duk_hthread *resumer; /* who resumed us (if any) */
  7834. /* current compiler state (if any), used for augmenting SyntaxErrors */
  7835. duk_compiler_ctx *compile_ctx;
  7836. #ifdef DUK_USE_INTERRUPT_COUNTER
  7837. /* Interrupt counter for triggering a slow path check for execution
  7838. * timeout, debugger interaction such as breakpoints, etc. This is
  7839. * actually a value copied from the heap structure into the current
  7840. * thread to be more convenient for the bytecode executor inner loop.
  7841. * The final value is copied back to the heap structure on a thread
  7842. * switch by DUK_HEAP_SWITCH_THREAD().
  7843. */
  7844. duk_int_t interrupt_counter;
  7845. #endif
  7846. /* Builtin-objects; may or may not be shared with other threads,
  7847. * threads existing in different "compartments" will have different
  7848. * built-ins. Must be stored on a per-thread basis because there
  7849. * is no intermediate structure for a thread group / compartment.
  7850. * This takes quite a lot of space, currently 43x4 = 172 bytes on
  7851. * 32-bit platforms.
  7852. */
  7853. duk_hobject *builtins[DUK_NUM_BUILTINS];
  7854. /* convenience copies from heap/vm for faster access */
  7855. #if defined(DUK_USE_HEAPPTR16)
  7856. duk_uint16_t *strs16;
  7857. #else
  7858. duk_hstring **strs;
  7859. #endif
  7860. };
  7861. /*
  7862. * Prototypes
  7863. */
  7864. DUK_INTERNAL_DECL void duk_hthread_copy_builtin_objects(duk_hthread *thr_from, duk_hthread *thr_to);
  7865. DUK_INTERNAL_DECL void duk_hthread_create_builtin_objects(duk_hthread *thr);
  7866. DUK_INTERNAL_DECL duk_bool_t duk_hthread_init_stacks(duk_heap *heap, duk_hthread *thr);
  7867. DUK_INTERNAL_DECL void duk_hthread_terminate(duk_hthread *thr);
  7868. DUK_INTERNAL_DECL void duk_hthread_callstack_grow(duk_hthread *thr);
  7869. DUK_INTERNAL_DECL void duk_hthread_callstack_shrink_check(duk_hthread *thr);
  7870. DUK_INTERNAL_DECL void duk_hthread_callstack_unwind(duk_hthread *thr, duk_size_t new_top);
  7871. DUK_INTERNAL_DECL void duk_hthread_catchstack_grow(duk_hthread *thr);
  7872. DUK_INTERNAL_DECL void duk_hthread_catchstack_shrink_check(duk_hthread *thr);
  7873. DUK_INTERNAL_DECL void duk_hthread_catchstack_unwind(duk_hthread *thr, duk_size_t new_top);
  7874. DUK_INTERNAL_DECL duk_activation *duk_hthread_get_current_activation(duk_hthread *thr);
  7875. DUK_INTERNAL_DECL void *duk_hthread_get_valstack_ptr(duk_heap *heap, void *ud); /* indirect allocs */
  7876. DUK_INTERNAL_DECL void *duk_hthread_get_callstack_ptr(duk_heap *heap, void *ud); /* indirect allocs */
  7877. DUK_INTERNAL_DECL void *duk_hthread_get_catchstack_ptr(duk_heap *heap, void *ud); /* indirect allocs */
  7878. #endif /* DUK_HTHREAD_H_INCLUDED */
  7879. #line 1 "duk_hbuffer.h"
  7880. /*
  7881. * Heap buffer representation.
  7882. *
  7883. * Heap allocated user data buffer which is either:
  7884. *
  7885. * 1. A fixed size buffer (data follows header statically)
  7886. * 2. A dynamic size buffer (data pointer follows header)
  7887. *
  7888. * The data pointer for a variable size buffer of zero size may be NULL.
  7889. */
  7890. #ifndef DUK_HBUFFER_H_INCLUDED
  7891. #define DUK_HBUFFER_H_INCLUDED
  7892. /*
  7893. * Flags
  7894. */
  7895. #define DUK_HBUFFER_FLAG_DYNAMIC DUK_HEAPHDR_USER_FLAG(0) /* buffer is resizable */
  7896. #define DUK_HBUFFER_HAS_DYNAMIC(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_DYNAMIC)
  7897. #define DUK_HBUFFER_SET_DYNAMIC(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_DYNAMIC)
  7898. #define DUK_HBUFFER_CLEAR_DYNAMIC(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_DYNAMIC)
  7899. #define DUK_HBUFFER_FIXED_GET_DATA_PTR(heap,x) ((duk_uint8_t *) (((duk_hbuffer_fixed *) (x)) + 1))
  7900. /*
  7901. * Misc defines
  7902. */
  7903. /* Impose a maximum buffer length for now. Restricted artificially to
  7904. * ensure resize computations or adding a heap header length won't
  7905. * overflow size_t and that a signed duk_int_t can hold a buffer
  7906. * length. The limit should be synchronized with DUK_HSTRING_MAX_BYTELEN.
  7907. */
  7908. #if defined(DUK_USE_BUFLEN16)
  7909. #define DUK_HBUFFER_MAX_BYTELEN (0x0000ffffUL)
  7910. #else
  7911. #define DUK_HBUFFER_MAX_BYTELEN (0x7fffffffUL)
  7912. #endif
  7913. /*
  7914. * Field access
  7915. */
  7916. /* Get/set the current user visible size, without accounting for a dynamic
  7917. * buffer's "spare" (= usable size).
  7918. */
  7919. #if defined(DUK_USE_BUFLEN16)
  7920. /* size stored in duk_heaphdr unused flag bits */
  7921. #define DUK_HBUFFER_GET_SIZE(x) ((x)->hdr.h_flags >> 16)
  7922. #define DUK_HBUFFER_SET_SIZE(x,v) do { \
  7923. (x)->hdr.h_flags = ((x)->hdr.h_flags & 0x0000ffffUL) | ((v) << 16); \
  7924. } while (0)
  7925. #define DUK_HBUFFER_ADD_SIZE(x,dv) do { \
  7926. (x)->hdr.h_flags += ((dv) << 16); \
  7927. } while (0)
  7928. #define DUK_HBUFFER_SUB_SIZE(x,dv) do { \
  7929. (x)->hdr.h_flags -= ((dv) << 16); \
  7930. } while (0)
  7931. #else
  7932. #define DUK_HBUFFER_GET_SIZE(x) (((duk_hbuffer *) (x))->size)
  7933. #define DUK_HBUFFER_SET_SIZE(x,v) do { \
  7934. ((duk_hbuffer *) (x))->size = (v); \
  7935. } while (0)
  7936. #define DUK_HBUFFER_ADD_SIZE(x,dv) do { \
  7937. (x)->size += (dv); \
  7938. } while (0)
  7939. #define DUK_HBUFFER_SUB_SIZE(x,dv) do { \
  7940. (x)->size -= (dv); \
  7941. } while (0)
  7942. #endif
  7943. #define DUK_HBUFFER_FIXED_GET_SIZE(x) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) (x))
  7944. #define DUK_HBUFFER_FIXED_SET_SIZE(x,v) DUK_HBUFFER_SET_SIZE((duk_hbuffer *) (x))
  7945. #define DUK_HBUFFER_DYNAMIC_GET_SIZE(x) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) (x))
  7946. #define DUK_HBUFFER_DYNAMIC_SET_SIZE(x,v) DUK_HBUFFER_SET_SIZE((duk_hbuffer *) (x), (v))
  7947. #define DUK_HBUFFER_DYNAMIC_ADD_SIZE(x,dv) DUK_HBUFFER_ADD_SIZE((duk_hbuffer *) (x), (dv))
  7948. #define DUK_HBUFFER_DYNAMIC_SUB_SIZE(x,dv) DUK_HBUFFER_SUB_SIZE((duk_hbuffer *) (x), (dv))
  7949. #if defined(DUK_USE_BUFLEN16) && defined(DUK_USE_HEAPPTR16)
  7950. /* alloc_size16 stored in duk_heaphdr h_extra16, available with pointer compression. */
  7951. #define DUK_HBUFFER_DYNAMIC_GET_ALLOC_SIZE(x) ((duk_size_t) ((x)->hdr.h_extra16))
  7952. #define DUK_HBUFFER_DYNAMIC_SET_ALLOC_SIZE(x,v) do { \
  7953. (x)->hdr.h_extra16 = (duk_uint16_t) (v); \
  7954. } while (0)
  7955. #elif defined(DUK_USE_BUFLEN16)
  7956. /* alloc_size16 stored in an explicit 16-bit fields. */
  7957. #define DUK_HBUFFER_DYNAMIC_GET_ALLOC_SIZE(x) ((duk_size_t) ((x)->alloc_size16))
  7958. #define DUK_HBUFFER_DYNAMIC_SET_ALLOC_SIZE(x,v) do { \
  7959. (x)->alloc_size16 = (duk_uint16_t) (v); \
  7960. } while (0)
  7961. #else
  7962. /* normal case */
  7963. #define DUK_HBUFFER_DYNAMIC_GET_ALLOC_SIZE(x) ((x)->alloc_size)
  7964. #define DUK_HBUFFER_DYNAMIC_SET_ALLOC_SIZE(x,v) do { \
  7965. (x)->alloc_size = (v); \
  7966. } while (0)
  7967. #endif
  7968. #define DUK_HBUFFER_DYNAMIC_GET_SPARE_SIZE(x) \
  7969. (duk_size_t) (DUK_HBUFFER_DYNAMIC_GET_ALLOC_SIZE((x)) - DUK_HBUFFER_DYNAMIC_GET_SIZE((x)))
  7970. #if defined(DUK_USE_HEAPPTR16)
  7971. #define DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap,x) \
  7972. ((void *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (x)->curr_alloc16))
  7973. #define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(heap,x,v) do { \
  7974. (x)->curr_alloc16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \
  7975. } while (0)
  7976. #define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR_NULL(heap,x) do { \
  7977. (x)->curr_alloc16 = 0; /* assume 0 <=> NULL */ \
  7978. } while (0)
  7979. #else
  7980. #define DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap,x) ((x)->curr_alloc)
  7981. #define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(heap,x,v) do { \
  7982. (x)->curr_alloc = (void *) (v); \
  7983. } while (0)
  7984. #define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR_NULL(heap,x) do { \
  7985. (x)->curr_alloc = (void *) NULL; \
  7986. } while (0)
  7987. #endif
  7988. /* Gets the actual buffer contents which matches the current allocation size
  7989. * (may be NULL for zero size dynamic buffer).
  7990. */
  7991. #define DUK_HBUFFER_GET_DATA_PTR(heap,x) ( \
  7992. DUK_HBUFFER_HAS_DYNAMIC((x)) ? \
  7993. DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((heap), (duk_hbuffer_dynamic *) (x)) : \
  7994. DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), (duk_hbuffer_fixed *) (x)) \
  7995. )
  7996. /* Growth parameters for dynamic buffers. */
  7997. #define DUK_HBUFFER_SPARE_ADD 16
  7998. #define DUK_HBUFFER_SPARE_DIVISOR 16 /* 2^4 -> 1/16 = 6.25% spare */
  7999. /*
  8000. * Structs
  8001. */
  8002. struct duk_hbuffer {
  8003. duk_heaphdr hdr;
  8004. /* It's not strictly necessary to track the current size, but
  8005. * it is useful for writing robust native code.
  8006. */
  8007. /* Current size (not counting a dynamic buffer's "spare"). */
  8008. #if defined(DUK_USE_BUFLEN16)
  8009. /* Stored in duk_heaphdr unused flags. */
  8010. #else
  8011. duk_size_t size;
  8012. #endif
  8013. /*
  8014. * Data following the header depends on the DUK_HBUFFER_FLAG_DYNAMIC
  8015. * flag.
  8016. *
  8017. * If the flag is clear (the buffer is a fixed size one), the buffer
  8018. * data follows the header directly, consisting of 'size' bytes.
  8019. *
  8020. * If the flag is set, the actual buffer is allocated separately, and
  8021. * a few control fields follow the header. Specifically:
  8022. *
  8023. * - a "void *" pointing to the current allocation
  8024. * - a duk_size_t indicating the full allocated size (always >= 'size')
  8025. *
  8026. * Unlike strings, no terminator byte (NUL) is guaranteed after the
  8027. * data. This would be convenient, but would pad aligned user buffers
  8028. * unnecessarily upwards in size. For instance, if user code requested
  8029. * a 64-byte dynamic buffer, 65 bytes would actually be allocated which
  8030. * would then potentially round upwards to perhaps 68 or 72 bytes.
  8031. */
  8032. };
  8033. #if defined(DUK_USE_ALIGN_8) && defined(DUK_USE_PACK_MSVC_PRAGMA)
  8034. #pragma pack(push, 8)
  8035. #endif
  8036. struct duk_hbuffer_fixed {
  8037. /* A union is used here as a portable struct size / alignment trick:
  8038. * by adding a 32-bit or a 64-bit (unused) union member, the size of
  8039. * the struct is effectively forced to be a multiple of 4 or 8 bytes
  8040. * (respectively) without increasing the size of the struct unless
  8041. * necessary.
  8042. */
  8043. union {
  8044. struct {
  8045. duk_heaphdr hdr;
  8046. #if defined(DUK_USE_BUFLEN16)
  8047. /* Stored in duk_heaphdr unused flags. */
  8048. #else
  8049. duk_size_t size;
  8050. #endif
  8051. } s;
  8052. #if defined(DUK_USE_ALIGN_4)
  8053. duk_uint32_t dummy_for_align4;
  8054. #elif defined(DUK_USE_ALIGN_8)
  8055. duk_double_t dummy_for_align8;
  8056. #else
  8057. /* no extra padding */
  8058. #endif
  8059. } u;
  8060. /*
  8061. * Data follows the struct header. The struct size is padded by the
  8062. * compiler based on the struct members. This guarantees that the
  8063. * buffer data will be aligned-by-4 but not necessarily aligned-by-8.
  8064. *
  8065. * On platforms where alignment does not matter, the struct padding
  8066. * could be removed (if there is any). On platforms where alignment
  8067. * by 8 is required, the struct size must be forced to be a multiple
  8068. * of 8 by some means. Without it, some user code may break, and also
  8069. * Duktape itself breaks (e.g. the compiler stores duk_tvals in a
  8070. * dynamic buffer).
  8071. */
  8072. }
  8073. #if defined(DUK_USE_ALIGN_8) && defined(DUK_USE_PACK_GCC_ATTR)
  8074. __attribute__ ((aligned (8)))
  8075. #elif defined(DUK_USE_ALIGN_8) && defined(DUK_USE_PACK_CLANG_ATTR)
  8076. __attribute__ ((aligned (8)))
  8077. #endif
  8078. ;
  8079. #if defined(DUK_USE_ALIGN_8) && defined(DUK_USE_PACK_MSVC_PRAGMA)
  8080. #pragma pack(pop)
  8081. #endif
  8082. struct duk_hbuffer_dynamic {
  8083. duk_heaphdr hdr;
  8084. #if defined(DUK_USE_BUFLEN16)
  8085. /* Stored in duk_heaphdr unused flags. */
  8086. #else
  8087. duk_size_t size;
  8088. #endif
  8089. #if defined(DUK_USE_BUFLEN16) && defined(DUK_USE_HEAPPTR16)
  8090. /* Stored in duk_heaphdr h_extra16. */
  8091. #elif defined(DUK_USE_BUFLEN16)
  8092. duk_uint16_t alloc_size16;
  8093. #else
  8094. duk_size_t alloc_size;
  8095. #endif
  8096. #if defined(DUK_USE_HEAPPTR16)
  8097. duk_uint16_t curr_alloc16;
  8098. #else
  8099. void *curr_alloc; /* may be NULL if alloc_size == 0 */
  8100. #endif
  8101. /*
  8102. * Allocation size for 'curr_alloc' is alloc_size. There is no
  8103. * automatic NUL terminator for buffers (see above for rationale).
  8104. *
  8105. * 'curr_alloc' is explicitly allocated with heap allocation
  8106. * primitives and will thus always have alignment suitable for
  8107. * e.g. duk_tval and an IEEE double.
  8108. */
  8109. };
  8110. /*
  8111. * Prototypes
  8112. */
  8113. DUK_INTERNAL_DECL duk_hbuffer *duk_hbuffer_alloc(duk_heap *heap, duk_size_t size, duk_small_uint_t flags);
  8114. DUK_INTERNAL_DECL void *duk_hbuffer_get_dynalloc_ptr(duk_heap *heap, void *ud); /* indirect allocs */
  8115. /* dynamic buffer ops */
  8116. DUK_INTERNAL_DECL void duk_hbuffer_resize(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t new_size, duk_size_t new_alloc_size);
  8117. DUK_INTERNAL_DECL void duk_hbuffer_reset(duk_hthread *thr, duk_hbuffer_dynamic *buf);
  8118. #if 0 /*unused*/
  8119. DUK_INTERNAL_DECL void duk_hbuffer_compact(duk_hthread *thr, duk_hbuffer_dynamic *buf);
  8120. #endif
  8121. DUK_INTERNAL_DECL void duk_hbuffer_append_bytes(duk_hthread *thr, duk_hbuffer_dynamic *buf, const duk_uint8_t *data, duk_size_t length);
  8122. DUK_INTERNAL_DECL void duk_hbuffer_append_byte(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_uint8_t byte);
  8123. DUK_INTERNAL_DECL duk_size_t duk_hbuffer_append_cstring(duk_hthread *thr, duk_hbuffer_dynamic *buf, const char *str);
  8124. DUK_INTERNAL_DECL duk_size_t duk_hbuffer_append_hstring(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_hstring *str);
  8125. DUK_INTERNAL_DECL duk_size_t duk_hbuffer_append_xutf8(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_ucodepoint_t codepoint);
  8126. DUK_INTERNAL_DECL duk_size_t duk_hbuffer_append_cesu8(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_ucodepoint_t codepoint);
  8127. #if 0
  8128. DUK_INTERNAL_DECL void duk_hbuffer_append_native_u32(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_uint32_t val);
  8129. #endif
  8130. DUK_INTERNAL_DECL void duk_hbuffer_insert_bytes(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t offset, const duk_uint8_t *data, duk_size_t length);
  8131. #if 0 /*unused*/
  8132. DUK_INTERNAL_DECL void duk_hbuffer_insert_byte(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t offset, duk_uint8_t byte);
  8133. #endif
  8134. #if 0 /*unused*/
  8135. DUK_INTERNAL_DECL duk_size_t duk_hbuffer_insert_cstring(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t offset, const char *str);
  8136. #endif
  8137. #if 0 /*unused*/
  8138. DUK_INTERNAL_DECL duk_size_t duk_hbuffer_insert_hstring(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t offset, duk_hstring *str);
  8139. #endif
  8140. DUK_INTERNAL_DECL duk_size_t duk_hbuffer_insert_xutf8(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t offset, duk_ucodepoint_t codepoint);
  8141. #if 0 /*unused*/
  8142. DUK_INTERNAL_DECL duk_size_t duk_hbuffer_insert_cesu8(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t offset, duk_ucodepoint_t codepoint);
  8143. #endif
  8144. DUK_INTERNAL_DECL void duk_hbuffer_remove_slice(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t offset, duk_size_t length);
  8145. DUK_INTERNAL_DECL void duk_hbuffer_insert_slice(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t dst_offset, duk_size_t src_offset, duk_size_t length);
  8146. DUK_INTERNAL_DECL void duk_hbuffer_append_slice(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t src_offset, duk_size_t length);
  8147. #endif /* DUK_HBUFFER_H_INCLUDED */
  8148. #line 1 "duk_heap.h"
  8149. /*
  8150. * Heap structure.
  8151. *
  8152. * Heap contains allocated heap objects, interned strings, and built-in
  8153. * strings for one or more threads.
  8154. */
  8155. #ifndef DUK_HEAP_H_INCLUDED
  8156. #define DUK_HEAP_H_INCLUDED
  8157. /* alloc function typedefs in duktape.h */
  8158. /*
  8159. * Heap flags
  8160. */
  8161. #define DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING (1 << 0) /* mark-and-sweep is currently running */
  8162. #define DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED (1 << 1) /* mark-and-sweep marking reached a recursion limit and must use multi-pass marking */
  8163. #define DUK_HEAP_FLAG_REFZERO_FREE_RUNNING (1 << 2) /* refcount code is processing refzero list */
  8164. #define DUK_HEAP_FLAG_ERRHANDLER_RUNNING (1 << 3) /* an error handler (user callback to augment/replace error) is running */
  8165. #define DUK_HEAP_FLAG_INTERRUPT_RUNNING (1 << 4) /* executor interrupt running (used to avoid nested interrupts) */
  8166. #define DUK__HEAP_HAS_FLAGS(heap,bits) ((heap)->flags & (bits))
  8167. #define DUK__HEAP_SET_FLAGS(heap,bits) do { \
  8168. (heap)->flags |= (bits); \
  8169. } while (0)
  8170. #define DUK__HEAP_CLEAR_FLAGS(heap,bits) do { \
  8171. (heap)->flags &= ~(bits); \
  8172. } while (0)
  8173. #define DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING)
  8174. #define DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED)
  8175. #define DUK_HEAP_HAS_REFZERO_FREE_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING)
  8176. #define DUK_HEAP_HAS_ERRHANDLER_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING)
  8177. #define DUK_HEAP_HAS_INTERRUPT_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING)
  8178. #define DUK_HEAP_SET_MARKANDSWEEP_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING)
  8179. #define DUK_HEAP_SET_MARKANDSWEEP_RECLIMIT_REACHED(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED)
  8180. #define DUK_HEAP_SET_REFZERO_FREE_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING)
  8181. #define DUK_HEAP_SET_ERRHANDLER_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING)
  8182. #define DUK_HEAP_SET_INTERRUPT_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING)
  8183. #define DUK_HEAP_CLEAR_MARKANDSWEEP_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING)
  8184. #define DUK_HEAP_CLEAR_MARKANDSWEEP_RECLIMIT_REACHED(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED)
  8185. #define DUK_HEAP_CLEAR_REFZERO_FREE_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING)
  8186. #define DUK_HEAP_CLEAR_ERRHANDLER_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING)
  8187. #define DUK_HEAP_CLEAR_INTERRUPT_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING)
  8188. /*
  8189. * Longjmp types, also double as identifying continuation type for a rethrow (in 'finally')
  8190. */
  8191. #define DUK_LJ_TYPE_UNKNOWN 0 /* unused */
  8192. #define DUK_LJ_TYPE_RETURN 1 /* value1 -> return value */
  8193. #define DUK_LJ_TYPE_THROW 2 /* value1 -> error object */
  8194. #define DUK_LJ_TYPE_BREAK 3 /* value1 -> label number */
  8195. #define DUK_LJ_TYPE_CONTINUE 4 /* value1 -> label number */
  8196. #define DUK_LJ_TYPE_YIELD 5 /* value1 -> yield value, iserror -> error / normal */
  8197. #define DUK_LJ_TYPE_RESUME 6 /* value1 -> resume value, value2 -> resumee thread, iserror -> error/normal */
  8198. #define DUK_LJ_TYPE_NORMAL 7 /* pseudo-type to indicate a normal continuation (for 'finally' rethrowing) */
  8199. /*
  8200. * Mark-and-sweep flags
  8201. *
  8202. * These are separate from heap level flags now but could be merged.
  8203. * The heap structure only contains a 'base mark-and-sweep flags'
  8204. * field and the GC caller can impose further flags.
  8205. */
  8206. #define DUK_MS_FLAG_EMERGENCY (1 << 0) /* emergency mode: try extra hard */
  8207. #define DUK_MS_FLAG_NO_STRINGTABLE_RESIZE (1 << 1) /* don't resize stringtable (but may sweep it); needed during stringtable resize */
  8208. #define DUK_MS_FLAG_NO_FINALIZERS (1 << 2) /* don't run finalizers (which may have arbitrary side effects) */
  8209. #define DUK_MS_FLAG_NO_OBJECT_COMPACTION (1 << 3) /* don't compact objects; needed during object property allocation resize */
  8210. /*
  8211. * Thread switching
  8212. *
  8213. * To switch heap->curr_thread, use the macro below so that interrupt counters
  8214. * get updated correctly. The macro allows a NULL target thread because that
  8215. * happens e.g. in call handling.
  8216. */
  8217. #if defined(DUK_USE_INTERRUPT_COUNTER)
  8218. #define DUK_HEAP_SWITCH_THREAD(heap,newthr) duk_heap_switch_thread((heap), (newthr))
  8219. #else
  8220. #define DUK_HEAP_SWITCH_THREAD(heap,newthr) do { \
  8221. (heap)->curr_thread = (newthr); \
  8222. } while (0)
  8223. #endif
  8224. /*
  8225. * Other heap related defines
  8226. */
  8227. /* Maximum duk_handle_call / duk_handle_safe_call depth. Note that this
  8228. * does not limit bytecode executor internal call depth at all (e.g.
  8229. * for Ecmascript-to-Ecmascript calls, thread yields/resumes, etc).
  8230. * There is a separate callstack depth limit for threads.
  8231. */
  8232. #if defined(DUK_USE_DEEP_C_STACK)
  8233. #define DUK_HEAP_DEFAULT_CALL_RECURSION_LIMIT 1000 /* assuming 0.5 kB between calls, about 500kB of stack */
  8234. #else
  8235. #define DUK_HEAP_DEFAULT_CALL_RECURSION_LIMIT 60 /* assuming 0.5 kB between calls, about 30kB of stack */
  8236. #endif
  8237. /* Mark-and-sweep C recursion depth for marking phase; if reached,
  8238. * mark object as a TEMPROOT and use multi-pass marking.
  8239. */
  8240. #if defined(DUK_USE_MARK_AND_SWEEP)
  8241. #if defined(DUK_USE_GC_TORTURE)
  8242. #define DUK_HEAP_MARK_AND_SWEEP_RECURSION_LIMIT 3
  8243. #elif defined(DUK_USE_DEEP_C_STACK)
  8244. #define DUK_HEAP_MARK_AND_SWEEP_RECURSION_LIMIT 256
  8245. #else
  8246. #define DUK_HEAP_MARK_AND_SWEEP_RECURSION_LIMIT 32
  8247. #endif
  8248. #endif
  8249. /* Mark-and-sweep interval is relative to combined count of objects and
  8250. * strings kept in the heap during the latest mark-and-sweep pass.
  8251. * Fixed point .8 multiplier and .0 adder. Trigger count (interval) is
  8252. * decreased by each (re)allocation attempt (regardless of size), and each
  8253. * refzero processed object.
  8254. *
  8255. * 'SKIP' indicates how many (re)allocations to wait until a retry if
  8256. * GC is skipped because there is no thread do it with yet (happens
  8257. * only during init phases).
  8258. */
  8259. #if defined(DUK_USE_MARK_AND_SWEEP)
  8260. #if defined(DUK_USE_REFERENCE_COUNTING)
  8261. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_MULT 12800L /* 50x heap size */
  8262. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_ADD 1024L
  8263. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_SKIP 256L
  8264. #else
  8265. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_MULT 256L /* 1x heap size */
  8266. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_ADD 1024L
  8267. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_SKIP 256L
  8268. #endif
  8269. #endif
  8270. /* Stringcache is used for speeding up char-offset-to-byte-offset
  8271. * translations for non-ASCII strings.
  8272. */
  8273. #define DUK_HEAP_STRCACHE_SIZE 4
  8274. #define DUK_HEAP_STRINGCACHE_NOCACHE_LIMIT 16 /* strings up to the this length are not cached */
  8275. /* helper to insert a (non-string) heap object into heap allocated list */
  8276. #define DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap,hdr) duk_heap_insert_into_heap_allocated((heap),(hdr))
  8277. /* Executor interrupt default interval when nothing else requires a
  8278. * smaller value. The default interval must be small enough to allow
  8279. * for reasonable execution timeout checking.
  8280. */
  8281. #if defined(DUK_USE_INTERRUPT_COUNTER)
  8282. #define DUK_HEAP_INTCTR_DEFAULT (256L * 1024L)
  8283. #endif
  8284. /*
  8285. * Stringtable
  8286. */
  8287. /* initial stringtable size, must be prime and higher than DUK_UTIL_MIN_HASH_PRIME */
  8288. #define DUK_STRTAB_INITIAL_SIZE 17
  8289. /* indicates a deleted string; any fixed non-NULL, non-hstring pointer works */
  8290. #define DUK_STRTAB_DELETED_MARKER(heap) ((duk_hstring *) heap)
  8291. /* resizing parameters */
  8292. #define DUK_STRTAB_MIN_FREE_DIVISOR 4 /* load factor max 75% */
  8293. #define DUK_STRTAB_MIN_USED_DIVISOR 4 /* load factor min 25% */
  8294. #define DUK_STRTAB_GROW_ST_SIZE(n) ((n) + (n)) /* used entries + approx 100% -> reset load to 50% */
  8295. #define DUK_STRTAB_U32_MAX_STRLEN 10 /* 4'294'967'295 */
  8296. #define DUK_STRTAB_HIGHEST_32BIT_PRIME 0xfffffffbUL
  8297. /* probe sequence (open addressing) */
  8298. #define DUK_STRTAB_HASH_INITIAL(hash,h_size) ((hash) % (h_size))
  8299. #define DUK_STRTAB_HASH_PROBE_STEP(hash) DUK_UTIL_GET_HASH_PROBE_STEP((hash))
  8300. /* fixed top level hashtable size (separate chaining) */
  8301. #define DUK_STRTAB_CHAIN_SIZE DUK_USE_STRTAB_CHAIN_SIZE
  8302. /*
  8303. * Built-in strings
  8304. */
  8305. /* heap string indices are autogenerated in duk_strings.h */
  8306. #if defined(DUK_USE_HEAPPTR16)
  8307. #define DUK_HEAP_GET_STRING(heap,idx) \
  8308. ((duk_hstring *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (heap)->strs16[(idx)]))
  8309. #else
  8310. #define DUK_HEAP_GET_STRING(heap,idx) \
  8311. ((heap)->strs[(idx)])
  8312. #endif
  8313. /*
  8314. * Raw memory calls: relative to heap, but no GC interaction
  8315. */
  8316. #define DUK_ALLOC_RAW(heap,size) \
  8317. ((heap)->alloc_func((heap)->heap_udata, (size)))
  8318. #define DUK_REALLOC_RAW(heap,ptr,newsize) \
  8319. ((heap)->realloc_func((heap)->heap_udata, (void *) (ptr), (newsize)))
  8320. #define DUK_FREE_RAW(heap,ptr) \
  8321. ((heap)->free_func((heap)->heap_udata, (void *) (ptr)))
  8322. /*
  8323. * Memory calls: relative to heap, GC interaction, but no error throwing.
  8324. *
  8325. * XXX: Currently a mark-and-sweep triggered by memory allocation will run
  8326. * using the heap->heap_thread. This thread is also used for running
  8327. * mark-and-sweep finalization; this is not ideal because it breaks the
  8328. * isolation between multiple global environments.
  8329. *
  8330. * Notes:
  8331. *
  8332. * - DUK_FREE() is required to ignore NULL and any other possible return
  8333. * value of a zero-sized alloc/realloc (same as ANSI C free()).
  8334. *
  8335. * - There is no DUK_REALLOC_ZEROED because we don't assume to know the
  8336. * old size. Caller must zero the reallocated memory.
  8337. *
  8338. * - DUK_REALLOC_INDIRECT() must be used when a mark-and-sweep triggered
  8339. * by an allocation failure might invalidate the original 'ptr', thus
  8340. * causing a realloc retry to use an invalid pointer. Example: we're
  8341. * reallocating the value stack and a finalizer resizes the same value
  8342. * stack during mark-and-sweep. The indirect variant requests for the
  8343. * current location of the pointer being reallocated using a callback
  8344. * right before every realloc attempt; this circuitous approach is used
  8345. * to avoid strict aliasing issues in a more straightforward indirect
  8346. * pointer (void **) approach. Note: the pointer in the storage
  8347. * location is read but is NOT updated; the caller must do that.
  8348. */
  8349. /* callback for indirect reallocs, request for current pointer */
  8350. typedef void *(*duk_mem_getptr)(duk_heap *heap, void *ud);
  8351. #define DUK_ALLOC(heap,size) duk_heap_mem_alloc((heap), (size))
  8352. #define DUK_ALLOC_ZEROED(heap,size) duk_heap_mem_alloc_zeroed((heap), (size))
  8353. #define DUK_REALLOC(heap,ptr,newsize) duk_heap_mem_realloc((heap), (ptr), (newsize))
  8354. #define DUK_REALLOC_INDIRECT(heap,cb,ud,newsize) duk_heap_mem_realloc_indirect((heap), (cb), (ud), (newsize))
  8355. #define DUK_FREE(heap,ptr) duk_heap_mem_free((heap), (ptr))
  8356. /*
  8357. * Memory constants
  8358. */
  8359. #define DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT 5 /* Retry allocation after mark-and-sweep for this
  8360. * many times. A single mark-and-sweep round is
  8361. * not guaranteed to free all unreferenced memory
  8362. * because of finalization (in fact, ANY number of
  8363. * rounds is strictly not enough).
  8364. */
  8365. #define DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT 3 /* Starting from this round, use emergency mode
  8366. * for mark-and-sweep.
  8367. */
  8368. /*
  8369. * Debugger support
  8370. */
  8371. /* Maximum number of breakpoints. Only breakpoints that are set are
  8372. * consulted so increasing this has no performance impact.
  8373. */
  8374. #define DUK_HEAP_MAX_BREAKPOINTS 16
  8375. /* Opcode interval for a Date-based status/peek rate limit check. Only
  8376. * relevant when debugger is attached. Requesting a timestamp may be a
  8377. * slow operation on some platforms so this shouldn't be too low. On the
  8378. * other hand a high value makes Duktape react to a pause request slowly.
  8379. */
  8380. #define DUK_HEAP_DBG_RATELIMIT_OPCODES 4000
  8381. /* Milliseconds between status notify and transport peeks. */
  8382. #define DUK_HEAP_DBG_RATELIMIT_MILLISECS 200
  8383. /* Step types */
  8384. #define DUK_STEP_TYPE_NONE 0
  8385. #define DUK_STEP_TYPE_INTO 1
  8386. #define DUK_STEP_TYPE_OVER 2
  8387. #define DUK_STEP_TYPE_OUT 3
  8388. struct duk_breakpoint {
  8389. duk_hstring *filename;
  8390. duk_uint32_t line;
  8391. };
  8392. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  8393. #define DUK_HEAP_IS_DEBUGGER_ATTACHED(heap) ((heap)->dbg_read_cb != NULL)
  8394. #define DUK_HEAP_CLEAR_STEP_STATE(heap) do { \
  8395. (heap)->dbg_step_type = DUK_STEP_TYPE_NONE; \
  8396. (heap)->dbg_step_thread = NULL; \
  8397. (heap)->dbg_step_csindex = 0; \
  8398. (heap)->dbg_step_startline = 0; \
  8399. } while (0)
  8400. #define DUK_HEAP_SET_PAUSED(heap) do { \
  8401. (heap)->dbg_paused = 1; \
  8402. (heap)->dbg_state_dirty = 1; \
  8403. DUK_HEAP_CLEAR_STEP_STATE((heap)); \
  8404. } while (0)
  8405. #define DUK_HEAP_CLEAR_PAUSED(heap) do { \
  8406. (heap)->dbg_paused = 0; \
  8407. (heap)->dbg_state_dirty = 1; \
  8408. DUK_HEAP_CLEAR_STEP_STATE((heap)); \
  8409. } while (0)
  8410. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  8411. /*
  8412. * String cache should ideally be at duk_hthread level, but that would
  8413. * cause string finalization to slow down relative to the number of
  8414. * threads; string finalization must check the string cache for "weak"
  8415. * references to the string being finalized to avoid dead pointers.
  8416. *
  8417. * Thus, string caches are now at the heap level now.
  8418. */
  8419. struct duk_strcache {
  8420. duk_hstring *h;
  8421. duk_uint32_t bidx;
  8422. duk_uint32_t cidx;
  8423. };
  8424. /*
  8425. * Longjmp state, contains the information needed to perform a longjmp.
  8426. * Longjmp related values are written to value1, value2, and iserror.
  8427. */
  8428. struct duk_ljstate {
  8429. duk_jmpbuf *jmpbuf_ptr; /* current setjmp() catchpoint */
  8430. duk_small_uint_t type; /* longjmp type */
  8431. duk_bool_t iserror; /* isError flag for yield */
  8432. duk_tval value1; /* 1st related value (type specific) */
  8433. duk_tval value2; /* 2nd related value (type specific) */
  8434. };
  8435. /*
  8436. * Stringtable entry for fixed size stringtable
  8437. */
  8438. struct duk_strtab_entry {
  8439. #if defined(DUK_USE_HEAPPTR16)
  8440. /* A 16-bit listlen makes sense with 16-bit heap pointers: there
  8441. * won't be space for 64k strings anyway.
  8442. */
  8443. duk_uint16_t listlen; /* if 0, 'str16' used, if > 0, 'strlist16' used */
  8444. union {
  8445. duk_uint16_t strlist16;
  8446. duk_uint16_t str16;
  8447. } u;
  8448. #else
  8449. duk_size_t listlen; /* if 0, 'str' used, if > 0, 'strlist' used */
  8450. union {
  8451. duk_hstring **strlist;
  8452. duk_hstring *str;
  8453. } u;
  8454. #endif
  8455. };
  8456. /*
  8457. * Main heap structure
  8458. */
  8459. struct duk_heap {
  8460. duk_small_uint_t flags;
  8461. /* Allocator functions. */
  8462. duk_alloc_function alloc_func;
  8463. duk_realloc_function realloc_func;
  8464. duk_free_function free_func;
  8465. /* Heap udata, used for allocator functions but also for other heap
  8466. * level callbacks like pointer compression, etc.
  8467. */
  8468. void *heap_udata;
  8469. /* Precomputed pointers when using 16-bit heap pointer packing. */
  8470. #if defined(DUK_USE_HEAPPTR16)
  8471. duk_uint16_t heapptr_null16;
  8472. duk_uint16_t heapptr_deleted16;
  8473. #endif
  8474. /* Fatal error handling, called e.g. when a longjmp() is needed but
  8475. * lj.jmpbuf_ptr is NULL. fatal_func must never return; it's not
  8476. * declared as "noreturn" because doing that for typedefs is a bit
  8477. * challenging portability-wise.
  8478. */
  8479. duk_fatal_function fatal_func;
  8480. /* allocated heap objects */
  8481. duk_heaphdr *heap_allocated;
  8482. /* work list for objects whose refcounts are zero but which have not been
  8483. * "finalized"; avoids recursive C calls when refcounts go to zero in a
  8484. * chain of objects.
  8485. */
  8486. #if defined(DUK_USE_REFERENCE_COUNTING)
  8487. duk_heaphdr *refzero_list;
  8488. duk_heaphdr *refzero_list_tail;
  8489. #endif
  8490. #if defined(DUK_USE_MARK_AND_SWEEP)
  8491. /* mark-and-sweep control */
  8492. #if defined(DUK_USE_VOLUNTARY_GC)
  8493. duk_int_t mark_and_sweep_trigger_counter;
  8494. #endif
  8495. duk_int_t mark_and_sweep_recursion_depth;
  8496. /* mark-and-sweep flags automatically active (used for critical sections) */
  8497. duk_small_uint_t mark_and_sweep_base_flags;
  8498. /* work list for objects to be finalized (by mark-and-sweep) */
  8499. duk_heaphdr *finalize_list;
  8500. #endif
  8501. /* longjmp state */
  8502. duk_ljstate lj;
  8503. /* marker for detecting internal "double faults", see duk_error_throw.c */
  8504. duk_bool_t handling_error;
  8505. /* heap thread, used internally and for finalization */
  8506. duk_hthread *heap_thread;
  8507. /* current thread */
  8508. duk_hthread *curr_thread; /* currently running thread */
  8509. /* heap level "stash" object (e.g., various reachability roots) */
  8510. duk_hobject *heap_object;
  8511. /* heap level temporary log formatting buffer */
  8512. duk_hbuffer_dynamic *log_buffer;
  8513. /* duk_handle_call / duk_handle_safe_call recursion depth limiting */
  8514. duk_int_t call_recursion_depth;
  8515. duk_int_t call_recursion_limit;
  8516. /* mix-in value for computing string hashes; should be reasonably unpredictable */
  8517. duk_uint32_t hash_seed;
  8518. /* rnd_state for duk_util_tinyrandom.c */
  8519. duk_uint32_t rnd_state;
  8520. /* interrupt counter */
  8521. #if defined(DUK_USE_INTERRUPT_COUNTER)
  8522. duk_int_t interrupt_init; /* start value for current countdown */
  8523. duk_int_t interrupt_counter; /* countdown state (mirrored in current thread state) */
  8524. #endif
  8525. /* debugger */
  8526. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  8527. /* callbacks and udata; dbg_read_cb != NULL is used to indicate attached state */
  8528. duk_debug_read_function dbg_read_cb; /* required, NULL implies detached */
  8529. duk_debug_write_function dbg_write_cb; /* required */
  8530. duk_debug_peek_function dbg_peek_cb;
  8531. duk_debug_read_flush_function dbg_read_flush_cb;
  8532. duk_debug_write_flush_function dbg_write_flush_cb;
  8533. duk_debug_detached_function dbg_detached_cb;
  8534. void *dbg_udata;
  8535. /* debugger state, only relevant when attached */
  8536. duk_bool_t dbg_processing; /* currently processing messages or breakpoints: don't enter message processing recursively (e.g. no breakpoints when processing debugger eval) */
  8537. duk_bool_t dbg_paused; /* currently paused: talk with debug client until step/resume */
  8538. duk_bool_t dbg_state_dirty; /* resend state next time executor is about to run */
  8539. duk_small_uint_t dbg_step_type; /* step type: none, step into, step over, step out */
  8540. duk_hthread *dbg_step_thread; /* borrowed; NULL if no step state (NULLed in unwind) */
  8541. duk_size_t dbg_step_csindex; /* callstack index */
  8542. duk_uint32_t dbg_step_startline; /* starting line number */
  8543. duk_breakpoint dbg_breakpoints[DUK_HEAP_MAX_BREAKPOINTS]; /* breakpoints: [0,breakpoint_count[ gc reachable */
  8544. duk_small_uint_t dbg_breakpoint_count;
  8545. duk_breakpoint *dbg_breakpoints_active[DUK_HEAP_MAX_BREAKPOINTS + 1]; /* currently active breakpoints: NULL term, borrowed pointers */
  8546. /* XXX: make active breakpoints actual copies instead of pointers? */
  8547. /* These are for rate limiting Status notifications and transport peeking. */
  8548. duk_uint32_t dbg_exec_counter; /* cumulative opcode execution count (overflows are OK) */
  8549. duk_uint32_t dbg_last_counter; /* value of dbg_exec_counter when we last did a Date-based check */
  8550. duk_double_t dbg_last_time; /* time when status/peek was last done (Date-based rate limit) */
  8551. #endif
  8552. /* string intern table (weak refs) */
  8553. #if defined(DUK_USE_STRTAB_PROBE)
  8554. #if defined(DUK_USE_HEAPPTR16)
  8555. duk_uint16_t *strtable16;
  8556. #else
  8557. duk_hstring **strtable;
  8558. #endif
  8559. duk_uint32_t st_size; /* alloc size in elements */
  8560. duk_uint32_t st_used; /* used elements (includes DELETED) */
  8561. #endif
  8562. /* XXX: static alloc is OK until separate chaining stringtable
  8563. * resizing is implemented.
  8564. */
  8565. #if defined(DUK_USE_STRTAB_CHAIN)
  8566. duk_strtab_entry strtable[DUK_STRTAB_CHAIN_SIZE];
  8567. #endif
  8568. /* string access cache (codepoint offset -> byte offset) for fast string
  8569. * character looping; 'weak' reference which needs special handling in GC.
  8570. */
  8571. duk_strcache strcache[DUK_HEAP_STRCACHE_SIZE];
  8572. /* built-in strings */
  8573. #if defined(DUK_USE_HEAPPTR16)
  8574. duk_uint16_t strs16[DUK_HEAP_NUM_STRINGS];
  8575. #else
  8576. duk_hstring *strs[DUK_HEAP_NUM_STRINGS];
  8577. #endif
  8578. };
  8579. /*
  8580. * Prototypes
  8581. */
  8582. DUK_INTERNAL_DECL
  8583. duk_heap *duk_heap_alloc(duk_alloc_function alloc_func,
  8584. duk_realloc_function realloc_func,
  8585. duk_free_function free_func,
  8586. void *heap_udata,
  8587. duk_fatal_function fatal_func);
  8588. DUK_INTERNAL_DECL void duk_heap_free(duk_heap *heap);
  8589. DUK_INTERNAL_DECL void duk_free_hobject_inner(duk_heap *heap, duk_hobject *h);
  8590. DUK_INTERNAL_DECL void duk_free_hbuffer_inner(duk_heap *heap, duk_hbuffer *h);
  8591. DUK_INTERNAL_DECL void duk_free_hstring_inner(duk_heap *heap, duk_hstring *h);
  8592. DUK_INTERNAL_DECL void duk_heap_free_heaphdr_raw(duk_heap *heap, duk_heaphdr *hdr);
  8593. DUK_INTERNAL_DECL void duk_heap_insert_into_heap_allocated(duk_heap *heap, duk_heaphdr *hdr);
  8594. #if defined(DUK_USE_DOUBLE_LINKED_HEAP) && defined(DUK_USE_REFERENCE_COUNTING)
  8595. DUK_INTERNAL_DECL void duk_heap_remove_any_from_heap_allocated(duk_heap *heap, duk_heaphdr *hdr);
  8596. #endif
  8597. #if defined(DUK_USE_INTERRUPT_COUNTER)
  8598. DUK_INTERNAL_DECL void duk_heap_switch_thread(duk_heap *heap, duk_hthread *new_thr);
  8599. #endif
  8600. #if 0 /*unused*/
  8601. DUK_INTERNAL_DECL duk_hstring *duk_heap_string_lookup(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen);
  8602. #endif
  8603. DUK_INTERNAL_DECL duk_hstring *duk_heap_string_intern(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen);
  8604. DUK_INTERNAL_DECL duk_hstring *duk_heap_string_intern_checked(duk_hthread *thr, const duk_uint8_t *str, duk_uint32_t len);
  8605. #if 0 /*unused*/
  8606. DUK_INTERNAL_DECL duk_hstring *duk_heap_string_lookup_u32(duk_heap *heap, duk_uint32_t val);
  8607. #endif
  8608. DUK_INTERNAL_DECL duk_hstring *duk_heap_string_intern_u32(duk_heap *heap, duk_uint32_t val);
  8609. DUK_INTERNAL_DECL duk_hstring *duk_heap_string_intern_u32_checked(duk_hthread *thr, duk_uint32_t val);
  8610. DUK_INTERNAL_DECL void duk_heap_string_remove(duk_heap *heap, duk_hstring *h);
  8611. #if defined(DUK_USE_MARK_AND_SWEEP) && defined(DUK_USE_MS_STRINGTABLE_RESIZE)
  8612. DUK_INTERNAL_DECL void duk_heap_force_strtab_resize(duk_heap *heap);
  8613. #endif
  8614. DUK_INTERNAL void duk_heap_free_strtab(duk_heap *heap);
  8615. #if defined(DUK_USE_DEBUG)
  8616. DUK_INTERNAL void duk_heap_dump_strtab(duk_heap *heap);
  8617. #endif
  8618. DUK_INTERNAL_DECL void duk_heap_strcache_string_remove(duk_heap *heap, duk_hstring *h);
  8619. DUK_INTERNAL_DECL duk_uint_fast32_t duk_heap_strcache_offset_char2byte(duk_hthread *thr, duk_hstring *h, duk_uint_fast32_t char_offset);
  8620. #if defined(DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS)
  8621. DUK_INTERNAL_DECL void *duk_default_alloc_function(void *udata, duk_size_t size);
  8622. DUK_INTERNAL_DECL void *duk_default_realloc_function(void *udata, void *ptr, duk_size_t newsize);
  8623. DUK_INTERNAL_DECL void duk_default_free_function(void *udata, void *ptr);
  8624. #endif
  8625. DUK_INTERNAL_DECL void *duk_heap_mem_alloc(duk_heap *heap, duk_size_t size);
  8626. DUK_INTERNAL_DECL void *duk_heap_mem_alloc_zeroed(duk_heap *heap, duk_size_t size);
  8627. DUK_INTERNAL_DECL void *duk_heap_mem_realloc(duk_heap *heap, void *ptr, duk_size_t newsize);
  8628. DUK_INTERNAL_DECL void *duk_heap_mem_realloc_indirect(duk_heap *heap, duk_mem_getptr cb, void *ud, duk_size_t newsize);
  8629. DUK_INTERNAL_DECL void duk_heap_mem_free(duk_heap *heap, void *ptr);
  8630. #ifdef DUK_USE_REFERENCE_COUNTING
  8631. #if !defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  8632. DUK_INTERNAL_DECL void duk_tval_incref(duk_tval *tv);
  8633. #endif
  8634. #if 0 /* unused */
  8635. DUK_INTERNAL_DECL void duk_tval_incref_allownull(duk_tval *tv);
  8636. #endif
  8637. DUK_INTERNAL_DECL void duk_tval_decref(duk_hthread *thr, duk_tval *tv);
  8638. #if 0 /* unused */
  8639. DUK_INTERNAL_DECL void duk_tval_decref_allownull(duk_hthread *thr, duk_tval *tv);
  8640. #endif
  8641. #if !defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  8642. DUK_INTERNAL_DECL void duk_heaphdr_incref(duk_heaphdr *h);
  8643. #endif
  8644. #if 0 /* unused */
  8645. DUK_INTERNAL_DECL void duk_heaphdr_incref_allownull(duk_heaphdr *h);
  8646. #endif
  8647. DUK_INTERNAL_DECL void duk_heaphdr_decref(duk_hthread *thr, duk_heaphdr *h);
  8648. DUK_INTERNAL_DECL void duk_heaphdr_decref_allownull(duk_hthread *thr, duk_heaphdr *h);
  8649. DUK_INTERNAL_DECL void duk_heaphdr_refzero(duk_hthread *thr, duk_heaphdr *h);
  8650. DUK_INTERNAL_DECL void duk_heaphdr_refcount_finalize(duk_hthread *thr, duk_heaphdr *hdr);
  8651. #else
  8652. /* no refcounting */
  8653. #endif
  8654. #if defined(DUK_USE_MARK_AND_SWEEP)
  8655. DUK_INTERNAL_DECL duk_bool_t duk_heap_mark_and_sweep(duk_heap *heap, duk_small_uint_t flags);
  8656. #endif
  8657. DUK_INTERNAL_DECL duk_uint32_t duk_heap_hashstring(duk_heap *heap, const duk_uint8_t *str, duk_size_t len);
  8658. #endif /* DUK_HEAP_H_INCLUDED */
  8659. #line 1 "duk_debugger.h"
  8660. #ifndef DUK_DEBUGGER_H_INCLUDED
  8661. #define DUK_DEBUGGER_H_INCLUDED
  8662. /* Debugger protocol version is defined in the public API header. */
  8663. #define DUK_DBG_MARKER_EOM 0x00
  8664. #define DUK_DBG_MARKER_REQUEST 0x01
  8665. #define DUK_DBG_MARKER_REPLY 0x02
  8666. #define DUK_DBG_MARKER_ERROR 0x03
  8667. #define DUK_DBG_MARKER_NOTIFY 0x04
  8668. #define DUK_DBG_ERR_UNKNOWN 0x00
  8669. #define DUK_DBG_ERR_UNSUPPORTED 0x01
  8670. #define DUK_DBG_ERR_TOOMANY 0x02
  8671. #define DUK_DBG_ERR_NOTFOUND 0x03
  8672. /* Initiated by Duktape */
  8673. #define DUK_DBG_CMD_STATUS 0x01
  8674. #define DUK_DBG_CMD_PRINT 0x02
  8675. #define DUK_DBG_CMD_ALERT 0x03
  8676. #define DUK_DBG_CMD_LOG 0x04
  8677. /* Initiated by debug client */
  8678. #define DUK_DBG_CMD_BASICINFO 0x10
  8679. #define DUK_DBG_CMD_TRIGGERSTATUS 0x11
  8680. #define DUK_DBG_CMD_PAUSE 0x12
  8681. #define DUK_DBG_CMD_RESUME 0x13
  8682. #define DUK_DBG_CMD_STEPINTO 0x14
  8683. #define DUK_DBG_CMD_STEPOVER 0x15
  8684. #define DUK_DBG_CMD_STEPOUT 0x16
  8685. #define DUK_DBG_CMD_LISTBREAK 0x17
  8686. #define DUK_DBG_CMD_ADDBREAK 0x18
  8687. #define DUK_DBG_CMD_DELBREAK 0x19
  8688. #define DUK_DBG_CMD_GETVAR 0x1a
  8689. #define DUK_DBG_CMD_PUTVAR 0x1b
  8690. #define DUK_DBG_CMD_GETCALLSTACK 0x1c
  8691. #define DUK_DBG_CMD_GETLOCALS 0x1d
  8692. #define DUK_DBG_CMD_EVAL 0x1e
  8693. #define DUK_DBG_CMD_DETACH 0x1f
  8694. #define DUK_DBG_CMD_DUMPHEAP 0x20
  8695. #define DUK_DBG_CMD_GETBYTECODE 0x21
  8696. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  8697. DUK_INTERNAL_DECL void duk_debug_do_detach(duk_heap *heap);
  8698. DUK_INTERNAL_DECL duk_bool_t duk_debug_read_peek(duk_hthread *thr);
  8699. DUK_INTERNAL_DECL void duk_debug_write_flush(duk_hthread *thr);
  8700. DUK_INTERNAL_DECL void duk_debug_skip_bytes(duk_hthread *thr, duk_size_t length);
  8701. DUK_INTERNAL_DECL void duk_debug_skip_byte(duk_hthread *thr);
  8702. DUK_INTERNAL_DECL void duk_debug_read_bytes(duk_hthread *thr, duk_uint8_t *data, duk_size_t length);
  8703. DUK_INTERNAL_DECL duk_uint8_t duk_debug_read_byte(duk_hthread *thr);
  8704. DUK_INTERNAL_DECL duk_int32_t duk_debug_read_int(duk_hthread *thr);
  8705. DUK_INTERNAL_DECL duk_hstring *duk_debug_read_hstring(duk_hthread *thr);
  8706. /* XXX: exposed duk_debug_read_pointer */
  8707. /* XXX: exposed duk_debug_read_buffer */
  8708. /* XXX: exposed duk_debug_read_hbuffer */
  8709. DUK_INTERNAL_DECL void duk_debug_read_tval(duk_hthread *thr);
  8710. DUK_INTERNAL_DECL void duk_debug_write_bytes(duk_hthread *thr, const duk_uint8_t *data, duk_size_t length);
  8711. DUK_INTERNAL_DECL void duk_debug_write_byte(duk_hthread *thr, duk_uint8_t x);
  8712. DUK_INTERNAL_DECL void duk_debug_write_unused(duk_hthread *thr);
  8713. DUK_INTERNAL_DECL void duk_debug_write_undefined(duk_hthread *thr);
  8714. DUK_INTERNAL_DECL void duk_debug_write_int(duk_hthread *thr, duk_int32_t x);
  8715. DUK_INTERNAL_DECL void duk_debug_write_uint(duk_hthread *thr, duk_uint32_t x);
  8716. DUK_INTERNAL_DECL void duk_debug_write_string(duk_hthread *thr, const char *data, duk_size_t length);
  8717. DUK_INTERNAL_DECL void duk_debug_write_cstring(duk_hthread *thr, const char *data);
  8718. DUK_INTERNAL_DECL void duk_debug_write_hstring(duk_hthread *thr, duk_hstring *h);
  8719. DUK_INTERNAL_DECL void duk_debug_write_buffer(duk_hthread *thr, const char *data, duk_size_t length);
  8720. DUK_INTERNAL_DECL void duk_debug_write_hbuffer(duk_hthread *thr, duk_hbuffer *h);
  8721. DUK_INTERNAL_DECL void duk_debug_write_pointer(duk_hthread *thr, const void *ptr);
  8722. #if defined(DUK_USE_DEBUGGER_DUMPHEAP)
  8723. DUK_INTERNAL_DECL void duk_debug_write_heapptr(duk_hthread *thr, duk_heaphdr *h);
  8724. #endif
  8725. DUK_INTERNAL_DECL void duk_debug_write_hobject(duk_hthread *thr, duk_hobject *obj);
  8726. DUK_INTERNAL_DECL void duk_debug_write_tval(duk_hthread *thr, duk_tval *tv);
  8727. #if 0 /* unused */
  8728. DUK_INTERNAL_DECL void duk_debug_write_request(duk_hthread *thr, duk_small_uint_t command);
  8729. #endif
  8730. DUK_INTERNAL_DECL void duk_debug_write_reply(duk_hthread *thr);
  8731. DUK_INTERNAL_DECL void duk_debug_write_error_eom(duk_hthread *thr, duk_small_uint_t err_code, const char *msg);
  8732. DUK_INTERNAL_DECL void duk_debug_write_notify(duk_hthread *thr, duk_small_uint_t command);
  8733. DUK_INTERNAL_DECL void duk_debug_write_eom(duk_hthread *thr);
  8734. DUK_INTERNAL duk_uint_fast32_t duk_debug_curr_line(duk_hthread *thr);
  8735. DUK_INTERNAL void duk_debug_send_status(duk_hthread *thr);
  8736. DUK_INTERNAL_DECL duk_bool_t duk_debug_process_messages(duk_hthread *thr, duk_bool_t no_block);
  8737. DUK_INTERNAL_DECL duk_small_int_t duk_debug_add_breakpoint(duk_hthread *thr, duk_hstring *filename, duk_uint32_t line);
  8738. DUK_INTERNAL_DECL duk_bool_t duk_debug_remove_breakpoint(duk_hthread *thr, duk_small_uint_t breakpoint_index);
  8739. #endif
  8740. #endif /* DUK_DEBUGGER_H_INCLUDED */
  8741. #line 1 "duk_debug.h"
  8742. /*
  8743. * Debugging macros, DUK_DPRINT() and its variants in particular.
  8744. *
  8745. * DUK_DPRINT() allows formatted debug prints, and supports standard
  8746. * and Duktape specific formatters. See duk_debug_vsnprintf.c for details.
  8747. *
  8748. * DUK_D(x), DUK_DD(x), and DUK_DDD(x) are used together with log macros
  8749. * for technical reasons. They are concretely used to hide 'x' from the
  8750. * compiler when the corresponding log level is disabled. This allows
  8751. * clean builds on non-C99 compilers, at the cost of more verbose code.
  8752. * Examples:
  8753. *
  8754. * DUK_D(DUK_DPRINT("foo"));
  8755. * DUK_DD(DUK_DDPRINT("foo"));
  8756. * DUK_DDD(DUK_DDDPRINT("foo"));
  8757. *
  8758. * This approach is preferable to the old "double parentheses" hack because
  8759. * double parentheses make the C99 solution worse: __FILE__ and __LINE__ can
  8760. * no longer be added transparently without going through globals, which
  8761. * works poorly with threading.
  8762. */
  8763. #ifndef DUK_DEBUG_H_INCLUDED
  8764. #define DUK_DEBUG_H_INCLUDED
  8765. #ifdef DUK_USE_DEBUG
  8766. #if defined(DUK_USE_DPRINT)
  8767. #define DUK_D(x) x
  8768. #else
  8769. #define DUK_D(x) do { } while (0) /* omit */
  8770. #endif
  8771. #if defined(DUK_USE_DDPRINT)
  8772. #define DUK_DD(x) x
  8773. #else
  8774. #define DUK_DD(x) do { } while (0) /* omit */
  8775. #endif
  8776. #if defined(DUK_USE_DDDPRINT)
  8777. #define DUK_DDD(x) x
  8778. #else
  8779. #define DUK_DDD(x) do { } while (0) /* omit */
  8780. #endif
  8781. /*
  8782. * Exposed debug macros: debugging enabled
  8783. */
  8784. #define DUK_LEVEL_DEBUG 1
  8785. #define DUK_LEVEL_DDEBUG 2
  8786. #define DUK_LEVEL_DDDEBUG 3
  8787. #ifdef DUK_USE_VARIADIC_MACROS
  8788. /* Note: combining __FILE__, __LINE__, and __func__ into fmt would be
  8789. * possible compile time, but waste some space with shared function names.
  8790. */
  8791. #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__);
  8792. #define DUK_DPRINT(...) DUK__DEBUG_LOG(DUK_LEVEL_DEBUG, __VA_ARGS__)
  8793. #ifdef DUK_USE_DDPRINT
  8794. #define DUK_DDPRINT(...) DUK__DEBUG_LOG(DUK_LEVEL_DDEBUG, __VA_ARGS__)
  8795. #else
  8796. #define DUK_DDPRINT(...)
  8797. #endif
  8798. #ifdef DUK_USE_DDDPRINT
  8799. #define DUK_DDDPRINT(...) DUK__DEBUG_LOG(DUK_LEVEL_DDDEBUG, __VA_ARGS__)
  8800. #else
  8801. #define DUK_DDDPRINT(...)
  8802. #endif
  8803. #else /* DUK_USE_VARIADIC_MACROS */
  8804. #define DUK__DEBUG_STASH(lev) \
  8805. (void) DUK_SNPRINTF(duk_debug_file_stash, DUK_DEBUG_STASH_SIZE, "%s", (const char *) DUK_FILE_MACRO), \
  8806. duk_debug_file_stash[DUK_DEBUG_STASH_SIZE - 1] = (char) 0; \
  8807. (void) DUK_SNPRINTF(duk_debug_line_stash, DUK_DEBUG_STASH_SIZE, "%ld", (long) DUK_LINE_MACRO), \
  8808. duk_debug_line_stash[DUK_DEBUG_STASH_SIZE - 1] = (char) 0; \
  8809. (void) DUK_SNPRINTF(duk_debug_func_stash, DUK_DEBUG_STASH_SIZE, "%s", (const char *) DUK_FUNC_MACRO), \
  8810. duk_debug_func_stash[DUK_DEBUG_STASH_SIZE - 1] = (char) 0; \
  8811. (void) (duk_debug_level_stash = (lev))
  8812. /* Without variadic macros resort to comma expression trickery to handle debug
  8813. * prints. This generates a lot of harmless warnings. These hacks are not
  8814. * needed normally because DUK_D() and friends will hide the entire debug log
  8815. * statement from the compiler.
  8816. */
  8817. #ifdef DUK_USE_DPRINT
  8818. #define DUK_DPRINT DUK__DEBUG_STASH(DUK_LEVEL_DEBUG), (void) duk_debug_log /* args go here in parens */
  8819. #else
  8820. #define DUK_DPRINT 0 && /* args go here as a comma expression in parens */
  8821. #endif
  8822. #ifdef DUK_USE_DDPRINT
  8823. #define DUK_DDPRINT DUK__DEBUG_STASH(DUK_LEVEL_DDEBUG), (void) duk_debug_log /* args go here in parens */
  8824. #else
  8825. #define DUK_DDPRINT 0 && /* args */
  8826. #endif
  8827. #ifdef DUK_USE_DDDPRINT
  8828. #define DUK_DDDPRINT DUK__DEBUG_STASH(DUK_LEVEL_DDDEBUG), (void) duk_debug_log /* args go here in parens */
  8829. #else
  8830. #define DUK_DDDPRINT 0 && /* args */
  8831. #endif
  8832. #endif /* DUK_USE_VARIADIC_MACROS */
  8833. #else /* DUK_USE_DEBUG */
  8834. /*
  8835. * Exposed debug macros: debugging disabled
  8836. */
  8837. #define DUK_D(x) do { } while (0) /* omit */
  8838. #define DUK_DD(x) do { } while (0) /* omit */
  8839. #define DUK_DDD(x) do { } while (0) /* omit */
  8840. #ifdef DUK_USE_VARIADIC_MACROS
  8841. #define DUK_DPRINT(...)
  8842. #define DUK_DDPRINT(...)
  8843. #define DUK_DDDPRINT(...)
  8844. #else /* DUK_USE_VARIADIC_MACROS */
  8845. #define DUK_DPRINT 0 && /* args go here as a comma expression in parens */
  8846. #define DUK_DDPRINT 0 && /* args */
  8847. #define DUK_DDDPRINT 0 && /* args */
  8848. #endif /* DUK_USE_VARIADIC_MACROS */
  8849. #endif /* DUK_USE_DEBUG */
  8850. /*
  8851. * Structs
  8852. */
  8853. #ifdef DUK_USE_DEBUG
  8854. struct duk_fixedbuffer {
  8855. duk_uint8_t *buffer;
  8856. duk_size_t length;
  8857. duk_size_t offset;
  8858. duk_bool_t truncated;
  8859. };
  8860. #endif
  8861. /*
  8862. * Prototypes
  8863. */
  8864. #ifdef DUK_USE_DEBUG
  8865. DUK_INTERNAL_DECL duk_int_t duk_debug_vsnprintf(char *str, duk_size_t size, const char *format, va_list ap);
  8866. #if 0 /*unused*/
  8867. DUK_INTERNAL_DECL duk_int_t duk_debug_snprintf(char *str, duk_size_t size, const char *format, ...);
  8868. #endif
  8869. DUK_INTERNAL_DECL void duk_debug_format_funcptr(char *buf, duk_size_t buf_size, duk_uint8_t *fptr, duk_size_t fptr_size);
  8870. #ifdef DUK_USE_VARIADIC_MACROS
  8871. 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, ...);
  8872. #else /* DUK_USE_VARIADIC_MACROS */
  8873. /* parameter passing, not thread safe */
  8874. #define DUK_DEBUG_STASH_SIZE 128
  8875. #if !defined(DUK_SINGLE_FILE)
  8876. DUK_INTERNAL_DECL char duk_debug_file_stash[DUK_DEBUG_STASH_SIZE];
  8877. DUK_INTERNAL_DECL char duk_debug_line_stash[DUK_DEBUG_STASH_SIZE];
  8878. DUK_INTERNAL_DECL char duk_debug_func_stash[DUK_DEBUG_STASH_SIZE];
  8879. DUK_INTERNAL_DECL duk_small_int_t duk_debug_level_stash;
  8880. #endif
  8881. DUK_INTERNAL_DECL void duk_debug_log(const char *fmt, ...);
  8882. #endif /* DUK_USE_VARIADIC_MACROS */
  8883. DUK_INTERNAL_DECL void duk_fb_put_bytes(duk_fixedbuffer *fb, duk_uint8_t *buffer, duk_size_t length);
  8884. DUK_INTERNAL_DECL void duk_fb_put_byte(duk_fixedbuffer *fb, duk_uint8_t x);
  8885. DUK_INTERNAL_DECL void duk_fb_put_cstring(duk_fixedbuffer *fb, const char *x);
  8886. DUK_INTERNAL_DECL void duk_fb_sprintf(duk_fixedbuffer *fb, const char *fmt, ...);
  8887. DUK_INTERNAL_DECL void duk_fb_put_funcptr(duk_fixedbuffer *fb, duk_uint8_t *fptr, duk_size_t fptr_size);
  8888. DUK_INTERNAL_DECL duk_bool_t duk_fb_is_full(duk_fixedbuffer *fb);
  8889. #endif /* DUK_USE_DEBUG */
  8890. #endif /* DUK_DEBUG_H_INCLUDED */
  8891. #line 1 "duk_error.h"
  8892. /*
  8893. * Error handling macros, assertion macro, error codes.
  8894. *
  8895. * There are three level of 'errors':
  8896. *
  8897. * 1. Ordinary errors, relative to a thread, cause a longjmp, catchable.
  8898. * 2. Fatal errors, relative to a heap, cause fatal handler to be called.
  8899. * 3. Panic errors, unrelated to a heap and cause a process exit.
  8900. *
  8901. * Panics are used by the default fatal error handler and by debug code
  8902. * such as assertions. By providing a proper fatal error handler, user
  8903. * code can avoid panics in non-debug builds.
  8904. */
  8905. #ifndef DUK_ERROR_H_INCLUDED
  8906. #define DUK_ERROR_H_INCLUDED
  8907. /*
  8908. * Error codes: defined in duktape.h
  8909. *
  8910. * Error codes are used as a shorthand to throw exceptions from inside
  8911. * the implementation. The appropriate Ecmascript object is constructed
  8912. * based on the code. Ecmascript code throws objects directly. The error
  8913. * codes are defined in the public API header because they are also used
  8914. * by calling code.
  8915. */
  8916. /*
  8917. * Normal error
  8918. *
  8919. * Normal error is thrown with a longjmp() through the current setjmp()
  8920. * catchpoint record in the duk_heap. The 'curr_thread' of the duk_heap
  8921. * identifies the throwing thread.
  8922. *
  8923. * Error formatting is not always necessary but there are no separate calls
  8924. * (to minimize code size). Error object creation will consume a considerable
  8925. * amount of time, compared to which formatting is probably trivial. Note
  8926. * that special formatting (provided by DUK_DEBUG macros) is NOT available.
  8927. *
  8928. * The _RAW variants allow the caller to specify file and line. This makes
  8929. * it easier to write checked calls which want to use the call site of the
  8930. * checked function, not the error macro call inside the checked function.
  8931. *
  8932. * We prefer the standard variadic macros; if they are not available, we
  8933. * fall back to awkward hacks.
  8934. */
  8935. #ifdef DUK_USE_VERBOSE_ERRORS
  8936. #ifdef DUK_USE_VARIADIC_MACROS
  8937. /* __VA_ARGS__ has comma issues for empty lists, so we mandate at least 1 argument for '...' (format string) */
  8938. #define DUK_ERROR(thr,err,...) duk_err_handle_error(DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (thr), (err), __VA_ARGS__)
  8939. #define DUK_ERROR_RAW(file,line,thr,err,...) duk_err_handle_error((file), (line), (thr), (err), __VA_ARGS__)
  8940. #else /* DUK_USE_VARIADIC_MACROS */
  8941. /* Parameter passing here is not thread safe. We rely on the __FILE__
  8942. * pointer being a constant which can be passed through a global.
  8943. */
  8944. #define DUK_ERROR \
  8945. (void) (duk_err_file_stash = (const char *) DUK_FILE_MACRO, \
  8946. duk_err_line_stash = (duk_int_t) DUK_LINE_MACRO, \
  8947. duk_err_handle_error_stash) /* arguments follow */
  8948. #define DUK_ERROR_RAW duk_err_handle_error
  8949. #endif /* DUK_USE_VARIADIC_MACROS */
  8950. #else /* DUK_USE_VERBOSE_ERRORS */
  8951. #ifdef DUK_USE_VARIADIC_MACROS
  8952. #define DUK_ERROR(thr,err,...) duk_err_handle_error((thr), (err))
  8953. #define DUK_ERROR_RAW(file,line,thr,err,...) duk_err_handle_error((thr), (err))
  8954. #else /* DUK_USE_VARIADIC_MACROS */
  8955. /* This is sub-optimal because arguments will be passed but ignored, and the strings
  8956. * will go into the object file. Can't think of how to do this portably and still
  8957. * relatively conveniently.
  8958. */
  8959. #define DUK_ERROR duk_err_handle_error_nonverbose1
  8960. #define DUK_ERROR_RAW duk_err_handle_error_nonverbose2
  8961. #endif /* DUK_USE_VARIADIC_MACROS */
  8962. #endif /* DUK_USE_VERBOSE_ERRORS */
  8963. /*
  8964. * Fatal error
  8965. *
  8966. * There are no fatal error macros at the moment. There are so few call
  8967. * sites that the fatal error handler is called directly.
  8968. */
  8969. /*
  8970. * Panic error
  8971. *
  8972. * Panic errors are not relative to either a heap or a thread, and cause
  8973. * DUK_PANIC() macro to be invoked. Unlesa a user provides DUK_OPT_PANIC_HANDLER,
  8974. * DUK_PANIC() calls a helper which prints out the error and causes a process
  8975. * exit.
  8976. *
  8977. * The user can override the macro to provide custom handling. A macro is
  8978. * used to allow the user to have inline panic handling if desired (without
  8979. * causing a potentially risky function call).
  8980. *
  8981. * Panics are only used in debug code such as assertions, and by the default
  8982. * fatal error handler.
  8983. */
  8984. #if defined(DUK_USE_PANIC_HANDLER)
  8985. /* already defined, good */
  8986. #define DUK_PANIC(code,msg) DUK_USE_PANIC_HANDLER((code),(msg))
  8987. #else
  8988. #define DUK_PANIC(code,msg) duk_default_panic_handler((code),(msg))
  8989. #endif /* DUK_USE_PANIC_HANDLER */
  8990. /*
  8991. * Assert macro: failure causes panic.
  8992. */
  8993. #ifdef DUK_USE_ASSERTIONS
  8994. /* the message should be a compile time constant without formatting (less risk);
  8995. * we don't care about assertion text size because they're not used in production
  8996. * builds.
  8997. */
  8998. #define DUK_ASSERT(x) do { \
  8999. if (!(x)) { \
  9000. DUK_PANIC(DUK_ERR_ASSERTION_ERROR, \
  9001. "assertion failed: " #x \
  9002. " (" DUK_FILE_MACRO ":" DUK_MACRO_STRINGIFY(DUK_LINE_MACRO) ")"); \
  9003. } \
  9004. } while (0)
  9005. /* Assertion compatible inside a comma expression, evaluates to void.
  9006. * Currently not compatible with DUK_OPT_PANIC_HANDLER() which may have
  9007. * a statement block.
  9008. */
  9009. #if defined(DUK_USE_PANIC_HANDLER)
  9010. /* XXX: resolve macro definition issue or call through a helper function? */
  9011. #define DUK_ASSERT_EXPR(x) ((void) 0)
  9012. #else
  9013. #define DUK_ASSERT_EXPR(x) \
  9014. ((void) ((x) ? 0 : (DUK_PANIC(DUK_ERR_ASSERTION_ERROR, \
  9015. "assertion failed: " #x \
  9016. " (" DUK_FILE_MACRO ":" DUK_MACRO_STRINGIFY(DUK_LINE_MACRO) ")"), 0)))
  9017. #endif
  9018. #else /* DUK_USE_ASSERTIONS */
  9019. #define DUK_ASSERT(x) do { /* assertion omitted */ } while (0)
  9020. #define DUK_ASSERT_EXPR(x) ((void) 0)
  9021. #endif /* DUK_USE_ASSERTIONS */
  9022. /* this variant is used when an assert would generate a compile warning by
  9023. * being always true (e.g. >= 0 comparison for an unsigned value
  9024. */
  9025. #define DUK_ASSERT_DISABLE(x) do { /* assertion disabled */ } while (0)
  9026. /*
  9027. * Assertion helpers
  9028. */
  9029. #if defined(DUK_USE_ASSERTIONS) && defined(DUK_USE_REFERENCE_COUNTING)
  9030. #define DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(h) do { \
  9031. DUK_ASSERT((h) == NULL || DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) (h)) > 0); \
  9032. } while (0)
  9033. #define DUK_ASSERT_REFCOUNT_NONZERO_TVAL(tv) do { \
  9034. if ((tv) != NULL && DUK_TVAL_IS_HEAP_ALLOCATED((tv))) { \
  9035. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(DUK_TVAL_GET_HEAPHDR((tv))) > 0); \
  9036. } \
  9037. } while (0)
  9038. #else
  9039. #define DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(h) /* no refcount check */
  9040. #define DUK_ASSERT_REFCOUNT_NONZERO_TVAL(tv) /* no refcount check */
  9041. #endif
  9042. #define DUK_ASSERT_TOP(ctx,n) DUK_ASSERT((duk_idx_t) duk_get_top((ctx)) == (duk_idx_t) (n))
  9043. #if defined(DUK_USE_ASSERTIONS) && defined(DUK_USE_PACKED_TVAL)
  9044. #define DUK_ASSERT_DOUBLE_IS_NORMALIZED(dval) do { \
  9045. duk_double_union assert_tmp_du; \
  9046. assert_tmp_du.d = (dval); \
  9047. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&assert_tmp_du)); \
  9048. } while (0)
  9049. #else
  9050. #define DUK_ASSERT_DOUBLE_IS_NORMALIZED(dval) /* nop */
  9051. #endif
  9052. /*
  9053. * Helper for valstack space
  9054. *
  9055. * Caller of DUK_ASSERT_VALSTACK_SPACE() estimates the number of free stack entries
  9056. * required for its own use, and any child calls which are not (a) Duktape API calls
  9057. * or (b) Duktape calls which involve extending the valstack (e.g. getter call).
  9058. */
  9059. #define DUK_VALSTACK_ASSERT_EXTRA 5 /* this is added to checks to allow for Duktape
  9060. * API calls in addition to function's own use
  9061. */
  9062. #if defined(DUK_USE_ASSERTIONS)
  9063. #define DUK_ASSERT_VALSTACK_SPACE(thr,n) do { \
  9064. DUK_ASSERT((thr) != NULL); \
  9065. DUK_ASSERT((thr)->valstack_end - (thr)->valstack_top >= (n) + DUK_VALSTACK_ASSERT_EXTRA); \
  9066. } while (0)
  9067. #else
  9068. #define DUK_ASSERT_VALSTACK_SPACE(thr,n) /* no valstack space check */
  9069. #endif
  9070. /*
  9071. * Prototypes
  9072. */
  9073. #ifdef DUK_USE_VERBOSE_ERRORS
  9074. #ifdef DUK_USE_VARIADIC_MACROS
  9075. 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, ...));
  9076. #else /* DUK_USE_VARIADIC_MACROS */
  9077. #if !defined(DUK_SINGLE_FILE)
  9078. DUK_INTERNAL_DECL const char *duk_err_file_stash;
  9079. DUK_INTERNAL_DECL duk_int_t duk_err_line_stash;
  9080. #endif /* !DUK_SINGLE_FILE */
  9081. 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, ...));
  9082. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error_stash(duk_hthread *thr, duk_errcode_t code, const char *fmt, ...));
  9083. #endif /* DUK_USE_VARIADIC_MACROS */
  9084. #else /* DUK_USE_VERBOSE_ERRORS */
  9085. #ifdef DUK_USE_VARIADIC_MACROS
  9086. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error(duk_hthread *thr, duk_errcode_t code));
  9087. #else /* DUK_USE_VARIADIC_MACROS */
  9088. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error_nonverbose1(duk_hthread *thr, duk_errcode_t code, const char *fmt, ...));
  9089. 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, ...));
  9090. #endif /* DUK_USE_VARIADIC_MACROS */
  9091. #endif /* DUK_USE_VERBOSE_ERRORS */
  9092. #ifdef DUK_USE_VERBOSE_ERRORS
  9093. 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));
  9094. #else
  9095. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_create_and_throw(duk_hthread *thr, duk_errcode_t code));
  9096. #endif
  9097. DUK_NORETURN(DUK_INTERNAL_DECL void duk_error_throw_from_negative_rc(duk_hthread *thr, duk_ret_t rc));
  9098. #if defined(DUK_USE_AUGMENT_ERROR_CREATE)
  9099. 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);
  9100. #endif
  9101. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  9102. DUK_INTERNAL_DECL void duk_err_augment_error_throw(duk_hthread *thr);
  9103. #endif
  9104. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_longjmp(duk_hthread *thr));
  9105. DUK_NORETURN(DUK_INTERNAL_DECL void duk_default_fatal_handler(duk_context *ctx, duk_errcode_t code, const char *msg));
  9106. #if !defined(DUK_USE_PANIC_HANDLER)
  9107. DUK_NORETURN(DUK_INTERNAL_DECL void duk_default_panic_handler(duk_errcode_t code, const char *msg));
  9108. #endif
  9109. DUK_INTERNAL_DECL void duk_err_setup_heap_ljstate(duk_hthread *thr, duk_small_int_t lj_type);
  9110. DUK_INTERNAL_DECL duk_hobject *duk_error_prototype_from_code(duk_hthread *thr, duk_errcode_t err_code);
  9111. #endif /* DUK_ERROR_H_INCLUDED */
  9112. #line 1 "duk_util.h"
  9113. /*
  9114. * Utilities
  9115. */
  9116. #ifndef DUK_UTIL_H_INCLUDED
  9117. #define DUK_UTIL_H_INCLUDED
  9118. #define DUK_UTIL_MIN_HASH_PRIME 17 /* must match genhashsizes.py */
  9119. #define DUK_UTIL_GET_HASH_PROBE_STEP(hash) (duk_util_probe_steps[(hash) & 0x1f])
  9120. /*
  9121. * Bitstream decoder
  9122. */
  9123. struct duk_bitdecoder_ctx {
  9124. const duk_uint8_t *data;
  9125. duk_size_t offset;
  9126. duk_size_t length;
  9127. duk_uint32_t currval;
  9128. duk_small_int_t currbits;
  9129. };
  9130. /*
  9131. * Bitstream encoder
  9132. */
  9133. struct duk_bitencoder_ctx {
  9134. duk_uint8_t *data;
  9135. duk_size_t offset;
  9136. duk_size_t length;
  9137. duk_uint32_t currval;
  9138. duk_small_int_t currbits;
  9139. duk_small_int_t truncated;
  9140. };
  9141. /*
  9142. * Externs and prototypes
  9143. */
  9144. #if !defined(DUK_SINGLE_FILE)
  9145. DUK_INTERNAL_DECL duk_uint8_t duk_lc_digits[36];
  9146. DUK_INTERNAL_DECL duk_uint8_t duk_uc_nybbles[16];
  9147. DUK_INTERNAL_DECL duk_int8_t duk_hex_dectab[256];
  9148. #endif /* !DUK_SINGLE_FILE */
  9149. /* Note: assumes that duk_util_probe_steps size is 32 */
  9150. #if defined(DUK_USE_HOBJECT_HASH_PART) || defined(DUK_USE_STRTAB_PROBE)
  9151. #if !defined(DUK_SINGLE_FILE)
  9152. DUK_INTERNAL_DECL duk_uint8_t duk_util_probe_steps[32];
  9153. #endif /* !DUK_SINGLE_FILE */
  9154. #endif
  9155. DUK_INTERNAL_DECL duk_uint32_t duk_util_hashbytes(const duk_uint8_t *data, duk_size_t len, duk_uint32_t seed);
  9156. #if defined(DUK_USE_HOBJECT_HASH_PART) || defined(DUK_USE_STRTAB_PROBE)
  9157. DUK_INTERNAL_DECL duk_uint32_t duk_util_get_hash_prime(duk_uint32_t size);
  9158. #endif
  9159. DUK_INTERNAL_DECL duk_int32_t duk_bd_decode(duk_bitdecoder_ctx *ctx, duk_small_int_t bits);
  9160. DUK_INTERNAL_DECL duk_small_int_t duk_bd_decode_flag(duk_bitdecoder_ctx *ctx);
  9161. DUK_INTERNAL_DECL duk_int32_t duk_bd_decode_flagged(duk_bitdecoder_ctx *ctx, duk_small_int_t bits, duk_int32_t def_value);
  9162. DUK_INTERNAL_DECL void duk_be_encode(duk_bitencoder_ctx *ctx, duk_uint32_t data, duk_small_int_t bits);
  9163. DUK_INTERNAL_DECL void duk_be_finish(duk_bitencoder_ctx *ctx);
  9164. DUK_INTERNAL_DECL duk_uint32_t duk_util_tinyrandom_get_bits(duk_hthread *thr, duk_small_int_t n);
  9165. DUK_INTERNAL_DECL duk_double_t duk_util_tinyrandom_get_double(duk_hthread *thr);
  9166. #if defined(DUK_USE_DEBUGGER_SUPPORT) /* For now only needed by the debugger. */
  9167. DUK_INTERNAL void duk_byteswap_bytes(duk_uint8_t *p, duk_small_uint_t len);
  9168. #endif
  9169. #endif /* DUK_UTIL_H_INCLUDED */
  9170. #line 1 "duk_unicode.h"
  9171. /*
  9172. * Unicode helpers
  9173. */
  9174. #ifndef DUK_UNICODE_H_INCLUDED
  9175. #define DUK_UNICODE_H_INCLUDED
  9176. /*
  9177. * UTF-8 / XUTF-8 / CESU-8 constants
  9178. */
  9179. #define DUK_UNICODE_MAX_XUTF8_LENGTH 7 /* up to 36 bit codepoints */
  9180. #define DUK_UNICODE_MAX_CESU8_LENGTH 6 /* all codepoints up to U+10FFFF */
  9181. /*
  9182. * Useful Unicode codepoints
  9183. *
  9184. * Integer constants must be signed to avoid unexpected coercions
  9185. * in comparisons.
  9186. */
  9187. #define DUK_UNICODE_CP_ZWNJ 0x200cL /* zero-width non-joiner */
  9188. #define DUK_UNICODE_CP_ZWJ 0x200dL /* zero-width joiner */
  9189. #define DUK_UNICODE_CP_REPLACEMENT_CHARACTER 0xfffdL /* http://en.wikipedia.org/wiki/Replacement_character#Replacement_character */
  9190. /*
  9191. * ASCII character constants
  9192. *
  9193. * C character literals like 'x' have a platform specific value and do
  9194. * not match ASCII (UTF-8) values on e.g. EBCDIC platforms. So, use
  9195. * these (admittedly awkward) constants instead. These constants must
  9196. * also have signed values to avoid unexpected coercions in comparisons.
  9197. *
  9198. * http://en.wikipedia.org/wiki/ASCII
  9199. */
  9200. #define DUK_ASC_NUL 0x00
  9201. #define DUK_ASC_SOH 0x01
  9202. #define DUK_ASC_STX 0x02
  9203. #define DUK_ASC_ETX 0x03
  9204. #define DUK_ASC_EOT 0x04
  9205. #define DUK_ASC_ENQ 0x05
  9206. #define DUK_ASC_ACK 0x06
  9207. #define DUK_ASC_BEL 0x07
  9208. #define DUK_ASC_BS 0x08
  9209. #define DUK_ASC_HT 0x09
  9210. #define DUK_ASC_LF 0x0a
  9211. #define DUK_ASC_VT 0x0b
  9212. #define DUK_ASC_FF 0x0c
  9213. #define DUK_ASC_CR 0x0d
  9214. #define DUK_ASC_SO 0x0e
  9215. #define DUK_ASC_SI 0x0f
  9216. #define DUK_ASC_DLE 0x10
  9217. #define DUK_ASC_DC1 0x11
  9218. #define DUK_ASC_DC2 0x12
  9219. #define DUK_ASC_DC3 0x13
  9220. #define DUK_ASC_DC4 0x14
  9221. #define DUK_ASC_NAK 0x15
  9222. #define DUK_ASC_SYN 0x16
  9223. #define DUK_ASC_ETB 0x17
  9224. #define DUK_ASC_CAN 0x18
  9225. #define DUK_ASC_EM 0x19
  9226. #define DUK_ASC_SUB 0x1a
  9227. #define DUK_ASC_ESC 0x1b
  9228. #define DUK_ASC_FS 0x1c
  9229. #define DUK_ASC_GS 0x1d
  9230. #define DUK_ASC_RS 0x1e
  9231. #define DUK_ASC_US 0x1f
  9232. #define DUK_ASC_SPACE 0x20
  9233. #define DUK_ASC_EXCLAMATION 0x21
  9234. #define DUK_ASC_DOUBLEQUOTE 0x22
  9235. #define DUK_ASC_HASH 0x23
  9236. #define DUK_ASC_DOLLAR 0x24
  9237. #define DUK_ASC_PERCENT 0x25
  9238. #define DUK_ASC_AMP 0x26
  9239. #define DUK_ASC_SINGLEQUOTE 0x27
  9240. #define DUK_ASC_LPAREN 0x28
  9241. #define DUK_ASC_RPAREN 0x29
  9242. #define DUK_ASC_STAR 0x2a
  9243. #define DUK_ASC_PLUS 0x2b
  9244. #define DUK_ASC_COMMA 0x2c
  9245. #define DUK_ASC_MINUS 0x2d
  9246. #define DUK_ASC_PERIOD 0x2e
  9247. #define DUK_ASC_SLASH 0x2f
  9248. #define DUK_ASC_0 0x30
  9249. #define DUK_ASC_1 0x31
  9250. #define DUK_ASC_2 0x32
  9251. #define DUK_ASC_3 0x33
  9252. #define DUK_ASC_4 0x34
  9253. #define DUK_ASC_5 0x35
  9254. #define DUK_ASC_6 0x36
  9255. #define DUK_ASC_7 0x37
  9256. #define DUK_ASC_8 0x38
  9257. #define DUK_ASC_9 0x39
  9258. #define DUK_ASC_COLON 0x3a
  9259. #define DUK_ASC_SEMICOLON 0x3b
  9260. #define DUK_ASC_LANGLE 0x3c
  9261. #define DUK_ASC_EQUALS 0x3d
  9262. #define DUK_ASC_RANGLE 0x3e
  9263. #define DUK_ASC_QUESTION 0x3f
  9264. #define DUK_ASC_ATSIGN 0x40
  9265. #define DUK_ASC_UC_A 0x41
  9266. #define DUK_ASC_UC_B 0x42
  9267. #define DUK_ASC_UC_C 0x43
  9268. #define DUK_ASC_UC_D 0x44
  9269. #define DUK_ASC_UC_E 0x45
  9270. #define DUK_ASC_UC_F 0x46
  9271. #define DUK_ASC_UC_G 0x47
  9272. #define DUK_ASC_UC_H 0x48
  9273. #define DUK_ASC_UC_I 0x49
  9274. #define DUK_ASC_UC_J 0x4a
  9275. #define DUK_ASC_UC_K 0x4b
  9276. #define DUK_ASC_UC_L 0x4c
  9277. #define DUK_ASC_UC_M 0x4d
  9278. #define DUK_ASC_UC_N 0x4e
  9279. #define DUK_ASC_UC_O 0x4f
  9280. #define DUK_ASC_UC_P 0x50
  9281. #define DUK_ASC_UC_Q 0x51
  9282. #define DUK_ASC_UC_R 0x52
  9283. #define DUK_ASC_UC_S 0x53
  9284. #define DUK_ASC_UC_T 0x54
  9285. #define DUK_ASC_UC_U 0x55
  9286. #define DUK_ASC_UC_V 0x56
  9287. #define DUK_ASC_UC_W 0x57
  9288. #define DUK_ASC_UC_X 0x58
  9289. #define DUK_ASC_UC_Y 0x59
  9290. #define DUK_ASC_UC_Z 0x5a
  9291. #define DUK_ASC_LBRACKET 0x5b
  9292. #define DUK_ASC_BACKSLASH 0x5c
  9293. #define DUK_ASC_RBRACKET 0x5d
  9294. #define DUK_ASC_CARET 0x5e
  9295. #define DUK_ASC_UNDERSCORE 0x5f
  9296. #define DUK_ASC_GRAVE 0x60
  9297. #define DUK_ASC_LC_A 0x61
  9298. #define DUK_ASC_LC_B 0x62
  9299. #define DUK_ASC_LC_C 0x63
  9300. #define DUK_ASC_LC_D 0x64
  9301. #define DUK_ASC_LC_E 0x65
  9302. #define DUK_ASC_LC_F 0x66
  9303. #define DUK_ASC_LC_G 0x67
  9304. #define DUK_ASC_LC_H 0x68
  9305. #define DUK_ASC_LC_I 0x69
  9306. #define DUK_ASC_LC_J 0x6a
  9307. #define DUK_ASC_LC_K 0x6b
  9308. #define DUK_ASC_LC_L 0x6c
  9309. #define DUK_ASC_LC_M 0x6d
  9310. #define DUK_ASC_LC_N 0x6e
  9311. #define DUK_ASC_LC_O 0x6f
  9312. #define DUK_ASC_LC_P 0x70
  9313. #define DUK_ASC_LC_Q 0x71
  9314. #define DUK_ASC_LC_R 0x72
  9315. #define DUK_ASC_LC_S 0x73
  9316. #define DUK_ASC_LC_T 0x74
  9317. #define DUK_ASC_LC_U 0x75
  9318. #define DUK_ASC_LC_V 0x76
  9319. #define DUK_ASC_LC_W 0x77
  9320. #define DUK_ASC_LC_X 0x78
  9321. #define DUK_ASC_LC_Y 0x79
  9322. #define DUK_ASC_LC_Z 0x7a
  9323. #define DUK_ASC_LCURLY 0x7b
  9324. #define DUK_ASC_PIPE 0x7c
  9325. #define DUK_ASC_RCURLY 0x7d
  9326. #define DUK_ASC_TILDE 0x7e
  9327. #define DUK_ASC_DEL 0x7f
  9328. /*
  9329. * Unicode tables
  9330. */
  9331. #ifdef DUK_USE_SOURCE_NONBMP
  9332. /*
  9333. * Automatically generated by extract_chars.py, do not edit!
  9334. */
  9335. extern const duk_uint8_t duk_unicode_ids_noa[791];
  9336. #else
  9337. /*
  9338. * Automatically generated by extract_chars.py, do not edit!
  9339. */
  9340. extern const duk_uint8_t duk_unicode_ids_noabmp[611];
  9341. #endif
  9342. #ifdef DUK_USE_SOURCE_NONBMP
  9343. /*
  9344. * Automatically generated by extract_chars.py, do not edit!
  9345. */
  9346. extern const duk_uint8_t duk_unicode_ids_m_let_noa[42];
  9347. #else
  9348. /*
  9349. * Automatically generated by extract_chars.py, do not edit!
  9350. */
  9351. extern const duk_uint8_t duk_unicode_ids_m_let_noabmp[24];
  9352. #endif
  9353. #ifdef DUK_USE_SOURCE_NONBMP
  9354. /*
  9355. * Automatically generated by extract_chars.py, do not edit!
  9356. */
  9357. extern const duk_uint8_t duk_unicode_idp_m_ids_noa[397];
  9358. #else
  9359. /*
  9360. * Automatically generated by extract_chars.py, do not edit!
  9361. */
  9362. extern const duk_uint8_t duk_unicode_idp_m_ids_noabmp[348];
  9363. #endif
  9364. /*
  9365. * Automatically generated by extract_caseconv.py, do not edit!
  9366. */
  9367. extern const duk_uint8_t duk_unicode_caseconv_uc[1288];
  9368. extern const duk_uint8_t duk_unicode_caseconv_lc[616];
  9369. /*
  9370. * Extern
  9371. */
  9372. /* duk_unicode_support.c */
  9373. #if !defined(DUK_SINGLE_FILE)
  9374. DUK_INTERNAL_DECL duk_uint8_t duk_unicode_xutf8_markers[7];
  9375. DUK_INTERNAL_DECL duk_uint16_t duk_unicode_re_ranges_digit[2];
  9376. DUK_INTERNAL_DECL duk_uint16_t duk_unicode_re_ranges_white[22];
  9377. DUK_INTERNAL_DECL duk_uint16_t duk_unicode_re_ranges_wordchar[8];
  9378. DUK_INTERNAL_DECL duk_uint16_t duk_unicode_re_ranges_not_digit[4];
  9379. DUK_INTERNAL_DECL duk_uint16_t duk_unicode_re_ranges_not_white[24];
  9380. DUK_INTERNAL_DECL duk_uint16_t duk_unicode_re_ranges_not_wordchar[10];
  9381. #endif /* !DUK_SINGLE_FILE */
  9382. /*
  9383. * Prototypes
  9384. */
  9385. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_get_xutf8_length(duk_ucodepoint_t cp);
  9386. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_encode_xutf8(duk_ucodepoint_t cp, duk_uint8_t *out);
  9387. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_encode_cesu8(duk_ucodepoint_t cp, duk_uint8_t *out);
  9388. 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);
  9389. 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);
  9390. DUK_INTERNAL_DECL duk_size_t duk_unicode_unvalidated_utf8_length(const duk_uint8_t *data, duk_size_t blen);
  9391. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_whitespace(duk_codepoint_t cp);
  9392. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_line_terminator(duk_codepoint_t cp);
  9393. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_identifier_start(duk_codepoint_t cp);
  9394. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_identifier_part(duk_codepoint_t cp);
  9395. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_letter(duk_codepoint_t cp);
  9396. DUK_INTERNAL_DECL void duk_unicode_case_convert_string(duk_hthread *thr, duk_bool_t uppercase);
  9397. DUK_INTERNAL_DECL duk_codepoint_t duk_unicode_re_canonicalize_char(duk_hthread *thr, duk_codepoint_t cp);
  9398. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_re_is_wordchar(duk_codepoint_t cp);
  9399. #endif /* DUK_UNICODE_H_INCLUDED */
  9400. #line 1 "duk_json.h"
  9401. /*
  9402. * Defines for JSON, especially duk_bi_json.c.
  9403. */
  9404. #ifndef DUK_JSON_H_INCLUDED
  9405. #define DUK_JSON_H_INCLUDED
  9406. /* Object/array recursion limit (to protect C stack) */
  9407. #if defined(DUK_USE_DEEP_C_STACK)
  9408. #define DUK_JSON_ENC_RECURSION_LIMIT 1000
  9409. #define DUK_JSON_DEC_RECURSION_LIMIT 1000
  9410. #else
  9411. #define DUK_JSON_ENC_RECURSION_LIMIT 100
  9412. #define DUK_JSON_DEC_RECURSION_LIMIT 100
  9413. #endif
  9414. /* Encoding/decoding flags */
  9415. #define DUK_JSON_FLAG_ASCII_ONLY (1 << 0) /* escape any non-ASCII characters */
  9416. #define DUK_JSON_FLAG_AVOID_KEY_QUOTES (1 << 1) /* avoid key quotes when key is an ASCII Identifier */
  9417. #define DUK_JSON_FLAG_EXT_CUSTOM (1 << 2) /* extended types: custom encoding */
  9418. #define DUK_JSON_FLAG_EXT_COMPATIBLE (1 << 3) /* extended types: compatible encoding */
  9419. /* How much stack to require on entry to object/array encode */
  9420. #define DUK_JSON_ENC_REQSTACK 32
  9421. /* How much stack to require on entry to object/array decode */
  9422. #define DUK_JSON_DEC_REQSTACK 32
  9423. /* Encoding state. Heap object references are all borrowed. */
  9424. typedef struct {
  9425. duk_hthread *thr;
  9426. duk_hbuffer_dynamic *h_buf;
  9427. duk_hobject *h_replacer; /* replacer function */
  9428. duk_hstring *h_gap; /* gap (if empty string, NULL) */
  9429. duk_hstring *h_indent; /* current indent (if gap is NULL, this is NULL) */
  9430. duk_idx_t idx_proplist; /* explicit PropertyList */
  9431. duk_idx_t idx_loop; /* valstack index of loop detection object */
  9432. duk_small_uint_t flags;
  9433. duk_small_uint_t flag_ascii_only;
  9434. duk_small_uint_t flag_avoid_key_quotes;
  9435. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  9436. duk_small_uint_t flag_ext_custom;
  9437. duk_small_uint_t flag_ext_compatible;
  9438. #endif
  9439. duk_int_t recursion_depth;
  9440. duk_int_t recursion_limit;
  9441. duk_uint_t mask_for_undefined; /* type bit mask: types which certainly produce 'undefined' */
  9442. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  9443. duk_small_uint_t stridx_custom_undefined;
  9444. duk_small_uint_t stridx_custom_nan;
  9445. duk_small_uint_t stridx_custom_neginf;
  9446. duk_small_uint_t stridx_custom_posinf;
  9447. duk_small_uint_t stridx_custom_function;
  9448. #endif
  9449. } duk_json_enc_ctx;
  9450. typedef struct {
  9451. duk_hthread *thr;
  9452. const duk_uint8_t *p;
  9453. const duk_uint8_t *p_start;
  9454. const duk_uint8_t *p_end;
  9455. duk_idx_t idx_reviver;
  9456. duk_small_uint_t flags;
  9457. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  9458. duk_small_uint_t flag_ext_custom;
  9459. duk_small_uint_t flag_ext_compatible;
  9460. #endif
  9461. duk_int_t recursion_depth;
  9462. duk_int_t recursion_limit;
  9463. } duk_json_dec_ctx;
  9464. #endif /* DUK_JSON_H_INCLUDED */
  9465. #line 1 "duk_js.h"
  9466. /*
  9467. * Ecmascript execution, support primitives.
  9468. */
  9469. #ifndef DUK_JS_H_INCLUDED
  9470. #define DUK_JS_H_INCLUDED
  9471. /* Flags for call handling. */
  9472. #define DUK_CALL_FLAG_PROTECTED (1 << 0) /* duk_handle_call: call is protected */
  9473. #define DUK_CALL_FLAG_IGNORE_RECLIMIT (1 << 1) /* duk_handle_call: call ignores C recursion limit (for errhandler calls) */
  9474. #define DUK_CALL_FLAG_CONSTRUCTOR_CALL (1 << 2) /* duk_handle_call: constructor call (i.e. called as 'new Foo()') */
  9475. #define DUK_CALL_FLAG_IS_RESUME (1 << 3) /* duk_handle_ecma_call_setup: setup for a resume() */
  9476. #define DUK_CALL_FLAG_IS_TAILCALL (1 << 4) /* duk_handle_ecma_call_setup: setup for a tailcall */
  9477. #define DUK_CALL_FLAG_DIRECT_EVAL (1 << 5) /* call is a direct eval call */
  9478. /* Flags for duk_js_equals_helper(). */
  9479. #define DUK_EQUALS_FLAG_SAMEVALUE (1 << 0) /* use SameValue instead of non-strict equality */
  9480. #define DUK_EQUALS_FLAG_STRICT (1 << 1) /* use strict equality instead of non-strict equality */
  9481. /* Flags for duk_js_compare_helper(). */
  9482. #define DUK_COMPARE_FLAG_EVAL_LEFT_FIRST (1 << 0) /* eval left argument first */
  9483. #define DUK_COMPARE_FLAG_NEGATE (1 << 1) /* negate result */
  9484. /* conversions, coercions, comparison, etc */
  9485. DUK_INTERNAL_DECL duk_bool_t duk_js_toboolean(duk_tval *tv);
  9486. DUK_INTERNAL_DECL duk_double_t duk_js_tonumber(duk_hthread *thr, duk_tval *tv);
  9487. DUK_INTERNAL_DECL duk_double_t duk_js_tointeger_number(duk_double_t x);
  9488. DUK_INTERNAL_DECL duk_double_t duk_js_tointeger(duk_hthread *thr, duk_tval *tv);
  9489. DUK_INTERNAL_DECL duk_uint32_t duk_js_touint32(duk_hthread *thr, duk_tval *tv);
  9490. DUK_INTERNAL_DECL duk_int32_t duk_js_toint32(duk_hthread *thr, duk_tval *tv);
  9491. DUK_INTERNAL_DECL duk_uint16_t duk_js_touint16(duk_hthread *thr, duk_tval *tv);
  9492. 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);
  9493. DUK_INTERNAL_DECL duk_uarridx_t duk_js_to_arrayindex_string_helper(duk_hstring *h);
  9494. 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);
  9495. 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);
  9496. DUK_INTERNAL_DECL duk_small_int_t duk_js_string_compare(duk_hstring *h1, duk_hstring *h2);
  9497. #if 0 /* unused */
  9498. DUK_INTERNAL_DECL duk_small_int_t duk_js_buffer_compare(duk_heap *heap, duk_hbuffer *h1, duk_hbuffer *h2);
  9499. #endif
  9500. 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);
  9501. DUK_INTERNAL_DECL duk_bool_t duk_js_instanceof(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y);
  9502. DUK_INTERNAL_DECL duk_bool_t duk_js_in(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y);
  9503. DUK_INTERNAL_DECL duk_hstring *duk_js_typeof(duk_hthread *thr, duk_tval *tv_x);
  9504. #define duk_js_equals(thr,tv_x,tv_y) \
  9505. duk_js_equals_helper((thr), (tv_x), (tv_y), 0)
  9506. #define duk_js_strict_equals(tv_x,tv_y) \
  9507. duk_js_equals_helper(NULL, (tv_x), (tv_y), DUK_EQUALS_FLAG_STRICT)
  9508. #define duk_js_samevalue(tv_x,tv_y) \
  9509. duk_js_equals_helper(NULL, (tv_x), (tv_y), DUK_EQUALS_FLAG_SAMEVALUE)
  9510. /* E5 Sections 11.8.1, 11.8.5; x < y */
  9511. #define duk_js_lessthan(thr,tv_x,tv_y) \
  9512. duk_js_compare_helper((thr), (tv_x), (tv_Y), DUK_COMPARE_FLAG_EVAL_LEFT_FIRST)
  9513. /* E5 Sections 11.8.2, 11.8.5; x > y --> y < x */
  9514. #define duk_js_greaterthan(thr,tv_x,tv_y) \
  9515. duk_js_compare_helper((thr), (tv_y), (tv_x), 0)
  9516. /* E5 Sections 11.8.3, 11.8.5; x <= y --> not (x > y) --> not (y < x) */
  9517. #define duk_js_lessthanorequal(thr,tv_x,tv_y) \
  9518. duk_js_compare_helper((thr), (tv_y), (tv_x), DUK_COMPARE_FLAG_NEGATE)
  9519. /* E5 Sections 11.8.4, 11.8.5; x >= y --> not (x < y) */
  9520. #define duk_js_greaterthanorequal(thr,tv_x,tv_y) \
  9521. duk_js_compare_helper((thr), (tv_x), (tv_y), DUK_COMPARE_FLAG_EVAL_LEFT_FIRST | DUK_COMPARE_FLAG_NEGATE)
  9522. /* identifiers and environment handling */
  9523. #if 0 /*unused*/
  9524. DUK_INTERNAL duk_bool_t duk_js_hasvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name);
  9525. #endif
  9526. DUK_INTERNAL_DECL duk_bool_t duk_js_getvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name, duk_bool_t throw_flag);
  9527. DUK_INTERNAL_DECL duk_bool_t duk_js_getvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name, duk_bool_t throw_flag);
  9528. DUK_INTERNAL_DECL void duk_js_putvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name, duk_tval *val, duk_bool_t strict);
  9529. DUK_INTERNAL_DECL void duk_js_putvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name, duk_tval *val, duk_bool_t strict);
  9530. #if 0 /*unused*/
  9531. DUK_INTERNAL_DECL duk_bool_t duk_js_delvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name);
  9532. #endif
  9533. DUK_INTERNAL_DECL duk_bool_t duk_js_delvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name);
  9534. 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);
  9535. DUK_INTERNAL_DECL void duk_js_init_activation_environment_records_delayed(duk_hthread *thr, duk_activation *act);
  9536. DUK_INTERNAL_DECL void duk_js_close_environment_record(duk_hthread *thr, duk_hobject *env, duk_hobject *func, duk_size_t regbase);
  9537. DUK_INTERNAL_DECL duk_hobject *duk_create_activation_environment_record(duk_hthread *thr, duk_hobject *func, duk_size_t idx_bottom);
  9538. DUK_INTERNAL_DECL
  9539. void duk_js_push_closure(duk_hthread *thr,
  9540. duk_hcompiledfunction *fun_temp,
  9541. duk_hobject *outer_var_env,
  9542. duk_hobject *outer_lex_env);
  9543. /* call handling */
  9544. DUK_INTERNAL_DECL duk_int_t duk_handle_call(duk_hthread *thr, duk_idx_t num_stack_args, duk_small_uint_t call_flags);
  9545. 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);
  9546. 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);
  9547. /* bytecode execution */
  9548. DUK_INTERNAL_DECL void duk_js_execute_bytecode(duk_hthread *exec_thr);
  9549. #endif /* DUK_JS_H_INCLUDED */
  9550. #line 1 "duk_numconv.h"
  9551. #ifndef DUK_NUMCONV_H_INCLUDED
  9552. #define DUK_NUMCONV_H_INCLUDED
  9553. /*
  9554. * Number-to-string conversion. The semantics of these is very tightly
  9555. * bound with the Ecmascript semantics required for call sites.
  9556. */
  9557. /* Output a specified number of digits instead of using the shortest
  9558. * form. Used for toPrecision() and toFixed().
  9559. */
  9560. #define DUK_N2S_FLAG_FIXED_FORMAT (1 << 0)
  9561. /* Force exponential format. Used for toExponential(). */
  9562. #define DUK_N2S_FLAG_FORCE_EXP (1 << 1)
  9563. /* If number would need zero padding (for whole number part), use
  9564. * exponential format instead. E.g. if input number is 12300, 3
  9565. * digits are generated ("123"), output "1.23e+4" instead of "12300".
  9566. * Used for toPrecision().
  9567. */
  9568. #define DUK_N2S_FLAG_NO_ZERO_PAD (1 << 2)
  9569. /* Digit count indicates number of fractions (i.e. an absolute
  9570. * digit index instead of a relative one). Used together with
  9571. * DUK_N2S_FLAG_FIXED_FORMAT for toFixed().
  9572. */
  9573. #define DUK_N2S_FLAG_FRACTION_DIGITS (1 << 3)
  9574. /*
  9575. * String-to-number conversion
  9576. */
  9577. /* Maximum exponent value when parsing numbers. This is not strictly
  9578. * compliant as there should be no upper limit, but as we parse the
  9579. * exponent without a bigint, impose some limit.
  9580. */
  9581. #define DUK_S2N_MAX_EXPONENT 1000000000
  9582. /* Trim white space (= allow leading and trailing whitespace) */
  9583. #define DUK_S2N_FLAG_TRIM_WHITE (1 << 0)
  9584. /* Allow exponent */
  9585. #define DUK_S2N_FLAG_ALLOW_EXP (1 << 1)
  9586. /* Allow trailing garbage (e.g. treat "123foo" as "123) */
  9587. #define DUK_S2N_FLAG_ALLOW_GARBAGE (1 << 2)
  9588. /* Allow leading plus sign */
  9589. #define DUK_S2N_FLAG_ALLOW_PLUS (1 << 3)
  9590. /* Allow leading minus sign */
  9591. #define DUK_S2N_FLAG_ALLOW_MINUS (1 << 4)
  9592. /* Allow 'Infinity' */
  9593. #define DUK_S2N_FLAG_ALLOW_INF (1 << 5)
  9594. /* Allow fraction part */
  9595. #define DUK_S2N_FLAG_ALLOW_FRAC (1 << 6)
  9596. /* Allow naked fraction (e.g. ".123") */
  9597. #define DUK_S2N_FLAG_ALLOW_NAKED_FRAC (1 << 7)
  9598. /* Allow empty fraction (e.g. "123.") */
  9599. #define DUK_S2N_FLAG_ALLOW_EMPTY_FRAC (1 << 8)
  9600. /* Allow empty string to be interpreted as 0 */
  9601. #define DUK_S2N_FLAG_ALLOW_EMPTY_AS_ZERO (1 << 9)
  9602. /* Allow leading zeroes (e.g. "0123" -> "123") */
  9603. #define DUK_S2N_FLAG_ALLOW_LEADING_ZERO (1 << 10)
  9604. /* Allow automatic detection of hex base ("0x" or "0X" prefix),
  9605. * overrides radix argument and forces integer mode.
  9606. */
  9607. #define DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT (1 << 11)
  9608. /* Allow automatic detection of octal base, overrides radix
  9609. * argument and forces integer mode.
  9610. */
  9611. #define DUK_S2N_FLAG_ALLOW_AUTO_OCT_INT (1 << 12)
  9612. /*
  9613. * Prototypes
  9614. */
  9615. 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);
  9616. DUK_INTERNAL_DECL void duk_numconv_parse(duk_context *ctx, duk_small_int_t radix, duk_small_uint_t flags);
  9617. #endif /* DUK_NUMCONV_H_INCLUDED */
  9618. #line 1 "duk_bi_protos.h"
  9619. /*
  9620. * Prototypes for all built-in functions.
  9621. */
  9622. #ifndef DUK_BUILTIN_PROTOS_H_INCLUDED
  9623. #define DUK_BUILTIN_PROTOS_H_INCLUDED
  9624. /* Buffer size needed for duk_bi_date_format_timeval().
  9625. * Accurate value is 32 + 1 for NUL termination:
  9626. * >>> len('+123456-01-23T12:34:56.123+12:34')
  9627. * 32
  9628. * Include additional space to be safe.
  9629. */
  9630. #define DUK_BI_DATE_ISO8601_BUFSIZE 48
  9631. /* Buffer size for "short log message" which use a heap-level pre-allocated
  9632. * dynamic buffer to reduce memory churn.
  9633. */
  9634. #define DUK_BI_LOGGER_SHORT_MSG_LIMIT 256
  9635. /* Maximum length of CommonJS module identifier to resolve. Length includes
  9636. * both current module ID, requested (possibly relative) module ID, and a
  9637. * slash in between.
  9638. */
  9639. #define DUK_BI_COMMONJS_MODULE_ID_LIMIT 256
  9640. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_constructor(duk_context *ctx);
  9641. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_constructor_is_array(duk_context *ctx);
  9642. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_to_string(duk_context *ctx);
  9643. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_concat(duk_context *ctx);
  9644. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_join_shared(duk_context *ctx);
  9645. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_pop(duk_context *ctx);
  9646. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_push(duk_context *ctx);
  9647. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_reverse(duk_context *ctx);
  9648. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_shift(duk_context *ctx);
  9649. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_slice(duk_context *ctx);
  9650. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_sort(duk_context *ctx);
  9651. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_splice(duk_context *ctx);
  9652. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_unshift(duk_context *ctx);
  9653. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_indexof_shared(duk_context *ctx);
  9654. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_iter_shared(duk_context *ctx);
  9655. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_reduce_shared(duk_context *ctx);
  9656. DUK_INTERNAL_DECL duk_ret_t duk_bi_boolean_constructor(duk_context *ctx);
  9657. DUK_INTERNAL_DECL duk_ret_t duk_bi_boolean_prototype_tostring_shared(duk_context *ctx);
  9658. /* XXX: naming is inconsistent with other builtins, "prototype" not used as
  9659. * part of function name.
  9660. */
  9661. DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_constructor(duk_context *ctx);
  9662. DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_prototype_tostring_shared(duk_context *ctx);
  9663. DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_readfield(duk_context *ctx);
  9664. DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_writefield(duk_context *ctx);
  9665. DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_compare_shared(duk_context *ctx);
  9666. DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_slice_shared(duk_context *ctx);
  9667. DUK_INTERNAL_DECL duk_ret_t duk_bi_arraybuffer_constructor(duk_context *ctx);
  9668. DUK_INTERNAL_DECL duk_ret_t duk_bi_arraybuffer_isview(duk_context *ctx);
  9669. DUK_INTERNAL_DECL duk_ret_t duk_bi_dataview_constructor(duk_context *ctx);
  9670. DUK_INTERNAL_DECL duk_ret_t duk_bi_typedarray_constructor(duk_context *ctx);
  9671. DUK_INTERNAL_DECL duk_ret_t duk_bi_typedarray_set(duk_context *ctx);
  9672. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_constructor(duk_context *ctx);
  9673. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_concat(duk_context *ctx);
  9674. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_is_encoding(duk_context *ctx);
  9675. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_is_buffer(duk_context *ctx);
  9676. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_byte_length(duk_context *ctx);
  9677. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_context *ctx);
  9678. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_tojson(duk_context *ctx);
  9679. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_fill(duk_context *ctx);
  9680. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_copy(duk_context *ctx);
  9681. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_write(duk_context *ctx);
  9682. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor(duk_context *ctx);
  9683. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor_parse(duk_context *ctx);
  9684. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor_utc(duk_context *ctx);
  9685. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor_now(duk_context *ctx);
  9686. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_tostring_shared(duk_context *ctx);
  9687. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_value_of(duk_context *ctx);
  9688. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_to_json(duk_context *ctx);
  9689. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_get_shared(duk_context *ctx);
  9690. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_get_timezone_offset(duk_context *ctx);
  9691. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_set_shared(duk_context *ctx);
  9692. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_set_time(duk_context *ctx);
  9693. /* Helpers exposed for internal use */
  9694. DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now(duk_context *ctx);
  9695. DUK_INTERNAL_DECL void duk_bi_date_format_timeval(duk_double_t timeval, duk_uint8_t *out_buf);
  9696. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_info(duk_context *ctx);
  9697. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_act(duk_context *ctx);
  9698. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_gc(duk_context *ctx);
  9699. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_fin(duk_context *ctx);
  9700. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_enc(duk_context *ctx);
  9701. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_dec(duk_context *ctx);
  9702. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_compact(duk_context *ctx);
  9703. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_constructor_shared(duk_context *ctx);
  9704. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_to_string(duk_context *ctx);
  9705. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_stack_getter(duk_context *ctx);
  9706. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_filename_getter(duk_context *ctx);
  9707. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_linenumber_getter(duk_context *ctx);
  9708. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_nop_setter(duk_context *ctx);
  9709. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_constructor(duk_context *ctx);
  9710. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype(duk_context *ctx);
  9711. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_to_string(duk_context *ctx);
  9712. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_apply(duk_context *ctx);
  9713. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_call(duk_context *ctx);
  9714. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_bind(duk_context *ctx);
  9715. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_eval(duk_context *ctx);
  9716. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_parse_int(duk_context *ctx);
  9717. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_parse_float(duk_context *ctx);
  9718. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_is_nan(duk_context *ctx);
  9719. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_is_finite(duk_context *ctx);
  9720. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_decode_uri(duk_context *ctx);
  9721. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_decode_uri_component(duk_context *ctx);
  9722. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_encode_uri(duk_context *ctx);
  9723. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_encode_uri_component(duk_context *ctx);
  9724. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_escape(duk_context *ctx);
  9725. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_unescape(duk_context *ctx);
  9726. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_print_helper(duk_context *ctx);
  9727. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_require(duk_context *ctx);
  9728. DUK_INTERNAL_DECL
  9729. void duk_bi_json_parse_helper(duk_context *ctx,
  9730. duk_idx_t idx_value,
  9731. duk_idx_t idx_reviver,
  9732. duk_small_uint_t flags);
  9733. DUK_INTERNAL_DECL
  9734. void duk_bi_json_stringify_helper(duk_context *ctx,
  9735. duk_idx_t idx_value,
  9736. duk_idx_t idx_replacer,
  9737. duk_idx_t idx_space,
  9738. duk_small_uint_t flags);
  9739. DUK_INTERNAL_DECL duk_ret_t duk_bi_json_object_parse(duk_context *ctx);
  9740. DUK_INTERNAL_DECL duk_ret_t duk_bi_json_object_stringify(duk_context *ctx);
  9741. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_onearg_shared(duk_context *ctx);
  9742. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_twoarg_shared(duk_context *ctx);
  9743. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_max(duk_context *ctx);
  9744. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_min(duk_context *ctx);
  9745. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_random(duk_context *ctx);
  9746. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_constructor(duk_context *ctx);
  9747. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_string(duk_context *ctx);
  9748. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_locale_string(duk_context *ctx);
  9749. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_value_of(duk_context *ctx);
  9750. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_fixed(duk_context *ctx);
  9751. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_exponential(duk_context *ctx);
  9752. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_precision(duk_context *ctx);
  9753. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_getprototype_shared(duk_context *ctx);
  9754. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_setprototype_shared(duk_context *ctx);
  9755. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor(duk_context *ctx);
  9756. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_get_own_property_descriptor(duk_context *ctx);
  9757. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_create(duk_context *ctx);
  9758. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_define_property(duk_context *ctx);
  9759. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_define_properties(duk_context *ctx);
  9760. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_seal_freeze_shared(duk_context *ctx);
  9761. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_prevent_extensions(duk_context *ctx);
  9762. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_is_sealed_frozen_shared(duk_context *ctx);
  9763. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_is_extensible(duk_context *ctx);
  9764. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_keys_shared(duk_context *ctx);
  9765. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_to_string(duk_context *ctx);
  9766. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_to_locale_string(duk_context *ctx);
  9767. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_value_of(duk_context *ctx);
  9768. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_has_own_property(duk_context *ctx);
  9769. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_is_prototype_of(duk_context *ctx);
  9770. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_property_is_enumerable(duk_context *ctx);
  9771. DUK_INTERNAL_DECL duk_ret_t duk_bi_pointer_constructor(duk_context *ctx);
  9772. DUK_INTERNAL_DECL duk_ret_t duk_bi_pointer_prototype_tostring_shared(duk_context *ctx);
  9773. DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_constructor(duk_context *ctx);
  9774. DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_exec(duk_context *ctx);
  9775. DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_test(duk_context *ctx);
  9776. DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_to_string(duk_context *ctx);
  9777. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_constructor(duk_context *ctx);
  9778. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_constructor_from_char_code(duk_context *ctx);
  9779. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_to_string(duk_context *ctx);
  9780. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_char_at(duk_context *ctx);
  9781. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_char_code_at(duk_context *ctx);
  9782. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_concat(duk_context *ctx);
  9783. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_indexof_shared(duk_context *ctx);
  9784. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_locale_compare(duk_context *ctx);
  9785. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_match(duk_context *ctx);
  9786. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_replace(duk_context *ctx);
  9787. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_search(duk_context *ctx);
  9788. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_slice(duk_context *ctx);
  9789. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_split(duk_context *ctx);
  9790. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_substring(duk_context *ctx);
  9791. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_caseconv_shared(duk_context *ctx);
  9792. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_trim(duk_context *ctx);
  9793. /* Note: present even if DUK_OPT_NO_SECTION_B given because genbuiltins.py
  9794. * will point to it.
  9795. */
  9796. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_substr(duk_context *ctx);
  9797. DUK_INTERNAL_DECL duk_ret_t duk_bi_proxy_constructor(duk_context *ctx);
  9798. #if 0 /* unimplemented now */
  9799. DUK_INTERNAL_DECL duk_ret_t duk_bi_proxy_constructor_revocable(duk_context *ctx);
  9800. #endif
  9801. DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_constructor(duk_context *ctx);
  9802. DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_resume(duk_context *ctx);
  9803. DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_yield(duk_context *ctx);
  9804. DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_current(duk_context *ctx);
  9805. DUK_INTERNAL_DECL duk_ret_t duk_bi_logger_constructor(duk_context *ctx);
  9806. DUK_INTERNAL_DECL duk_ret_t duk_bi_logger_prototype_fmt(duk_context *ctx);
  9807. DUK_INTERNAL_DECL duk_ret_t duk_bi_logger_prototype_raw(duk_context *ctx);
  9808. DUK_INTERNAL_DECL duk_ret_t duk_bi_logger_prototype_log_shared(duk_context *ctx);
  9809. DUK_INTERNAL_DECL duk_ret_t duk_bi_type_error_thrower(duk_context *ctx);
  9810. #endif /* DUK_BUILTIN_PROTOS_H_INCLUDED */
  9811. #line 1 "duk_selftest.h"
  9812. /*
  9813. * Selftest code
  9814. */
  9815. #ifndef DUK_SELFTEST_H_INCLUDED
  9816. #define DUK_SELFTEST_H_INCLUDED
  9817. #if defined(DUK_USE_SELF_TESTS)
  9818. DUK_INTERNAL_DECL void duk_selftest_run_tests(void);
  9819. #endif
  9820. #endif /* DUK_SELFTEST_H_INCLUDED */
  9821. #line 77 "duk_internal.h"
  9822. #endif /* DUK_INTERNAL_H_INCLUDED */
  9823. #line 1 "duk_strings.c"
  9824. /*
  9825. * Shared error message strings
  9826. *
  9827. * To minimize code footprint, try to share error messages inside Duktape
  9828. * code.
  9829. */
  9830. /* include removed: duk_internal.h */
  9831. /* Mostly API and built-in method related */
  9832. DUK_INTERNAL const char *duk_str_internal_error = "internal error";
  9833. DUK_INTERNAL const char *duk_str_invalid_count = "invalid count";
  9834. DUK_INTERNAL const char *duk_str_invalid_call_args = "invalid call args";
  9835. DUK_INTERNAL const char *duk_str_not_constructable = "not constructable";
  9836. DUK_INTERNAL const char *duk_str_not_callable = "not callable";
  9837. DUK_INTERNAL const char *duk_str_not_extensible = "not extensible";
  9838. DUK_INTERNAL const char *duk_str_not_writable = "not writable";
  9839. DUK_INTERNAL const char *duk_str_not_configurable = "not configurable";
  9840. DUK_INTERNAL const char *duk_str_invalid_context = "invalid context";
  9841. DUK_INTERNAL const char *duk_str_invalid_index = "invalid index";
  9842. DUK_INTERNAL const char *duk_str_push_beyond_alloc_stack = "attempt to push beyond currently allocated stack";
  9843. DUK_INTERNAL const char *duk_str_not_undefined = "not undefined";
  9844. DUK_INTERNAL const char *duk_str_not_null = "not null";
  9845. DUK_INTERNAL const char *duk_str_not_boolean = "not boolean";
  9846. DUK_INTERNAL const char *duk_str_not_number = "not number";
  9847. DUK_INTERNAL const char *duk_str_not_string = "not string";
  9848. DUK_INTERNAL const char *duk_str_not_pointer = "not pointer";
  9849. DUK_INTERNAL const char *duk_str_not_buffer = "not buffer";
  9850. DUK_INTERNAL const char *duk_str_unexpected_type = "unexpected type";
  9851. DUK_INTERNAL const char *duk_str_not_thread = "not thread";
  9852. #if 0 /*unused*/
  9853. DUK_INTERNAL const char *duk_str_not_compiledfunction = "not compiledfunction";
  9854. #endif
  9855. DUK_INTERNAL const char *duk_str_not_nativefunction = "not nativefunction";
  9856. DUK_INTERNAL const char *duk_str_not_c_function = "not c function";
  9857. DUK_INTERNAL const char *duk_str_defaultvalue_coerce_failed = "[[DefaultValue]] coerce failed";
  9858. DUK_INTERNAL const char *duk_str_number_outside_range = "number outside range";
  9859. DUK_INTERNAL const char *duk_str_not_object_coercible = "not object coercible";
  9860. DUK_INTERNAL const char *duk_str_string_too_long = "string too long";
  9861. DUK_INTERNAL const char *duk_str_buffer_too_long = "buffer too long";
  9862. DUK_INTERNAL const char *duk_str_sprintf_too_long = "sprintf message too long";
  9863. DUK_INTERNAL const char *duk_str_alloc_failed = "alloc failed";
  9864. DUK_INTERNAL const char *duk_str_pop_too_many = "attempt to pop too many entries";
  9865. DUK_INTERNAL const char *duk_str_buffer_not_dynamic = "buffer is not dynamic";
  9866. DUK_INTERNAL const char *duk_str_failed_to_extend_valstack = "failed to extend valstack";
  9867. DUK_INTERNAL const char *duk_str_base64_encode_failed = "base64 encode failed";
  9868. DUK_INTERNAL const char *duk_str_base64_decode_failed = "base64 decode failed";
  9869. DUK_INTERNAL const char *duk_str_hex_decode_failed = "hex decode failed";
  9870. DUK_INTERNAL const char *duk_str_no_sourcecode = "no sourcecode";
  9871. DUK_INTERNAL const char *duk_str_concat_result_too_long = "concat result too long";
  9872. DUK_INTERNAL const char *duk_str_unimplemented = "unimplemented";
  9873. DUK_INTERNAL const char *duk_str_array_length_over_2g = "array length over 2G";
  9874. /* JSON */
  9875. DUK_INTERNAL const char *duk_str_fmt_ptr = "%p";
  9876. DUK_INTERNAL const char *duk_str_fmt_invalid_json = "invalid json (at offset %ld)";
  9877. DUK_INTERNAL const char *duk_str_jsondec_reclimit = "json decode recursion limit";
  9878. DUK_INTERNAL const char *duk_str_jsonenc_reclimit = "json encode recursion limit";
  9879. DUK_INTERNAL const char *duk_str_cyclic_input = "cyclic input";
  9880. /* Object property access */
  9881. DUK_INTERNAL const char *duk_str_proxy_revoked = "proxy revoked";
  9882. DUK_INTERNAL const char *duk_str_object_resize_failed = "object resize failed";
  9883. DUK_INTERNAL const char *duk_str_invalid_base = "invalid base value";
  9884. DUK_INTERNAL const char *duk_str_strict_caller_read = "attempt to read strict 'caller'";
  9885. DUK_INTERNAL const char *duk_str_proxy_rejected = "proxy rejected";
  9886. DUK_INTERNAL const char *duk_str_invalid_array_length = "invalid array length";
  9887. DUK_INTERNAL const char *duk_str_array_length_write_failed = "array length write failed";
  9888. DUK_INTERNAL const char *duk_str_array_length_not_writable = "array length non-writable";
  9889. DUK_INTERNAL const char *duk_str_setter_undefined = "setter undefined";
  9890. DUK_INTERNAL const char *duk_str_redefine_virt_prop = "attempt to redefine virtual property";
  9891. DUK_INTERNAL const char *duk_str_invalid_descriptor = "invalid descriptor";
  9892. DUK_INTERNAL const char *duk_str_property_is_virtual = "property is virtual";
  9893. /* Compiler */
  9894. DUK_INTERNAL const char *duk_str_parse_error = "parse error";
  9895. DUK_INTERNAL const char *duk_str_duplicate_label = "duplicate label";
  9896. DUK_INTERNAL const char *duk_str_invalid_label = "invalid label";
  9897. DUK_INTERNAL const char *duk_str_invalid_array_literal = "invalid array literal";
  9898. DUK_INTERNAL const char *duk_str_invalid_object_literal = "invalid object literal";
  9899. DUK_INTERNAL const char *duk_str_invalid_var_declaration = "invalid variable declaration";
  9900. DUK_INTERNAL const char *duk_str_cannot_delete_identifier = "cannot delete identifier";
  9901. DUK_INTERNAL const char *duk_str_invalid_expression = "invalid expression";
  9902. DUK_INTERNAL const char *duk_str_invalid_lvalue = "invalid lvalue";
  9903. DUK_INTERNAL const char *duk_str_expected_identifier = "expected identifier";
  9904. DUK_INTERNAL const char *duk_str_empty_expr_not_allowed = "empty expression not allowed";
  9905. DUK_INTERNAL const char *duk_str_invalid_for = "invalid for statement";
  9906. DUK_INTERNAL const char *duk_str_invalid_switch = "invalid switch statement";
  9907. DUK_INTERNAL const char *duk_str_invalid_break_cont_label = "invalid break/continue label";
  9908. DUK_INTERNAL const char *duk_str_invalid_return = "invalid return";
  9909. DUK_INTERNAL const char *duk_str_invalid_try = "invalid try";
  9910. DUK_INTERNAL const char *duk_str_invalid_throw = "invalid throw";
  9911. DUK_INTERNAL const char *duk_str_with_in_strict_mode = "with in strict mode";
  9912. DUK_INTERNAL const char *duk_str_func_stmt_not_allowed = "function statement not allowed";
  9913. DUK_INTERNAL const char *duk_str_unterminated_stmt = "unterminated statement";
  9914. DUK_INTERNAL const char *duk_str_invalid_arg_name = "invalid argument name";
  9915. DUK_INTERNAL const char *duk_str_invalid_func_name = "invalid function name";
  9916. DUK_INTERNAL const char *duk_str_invalid_getset_name = "invalid getter/setter name";
  9917. DUK_INTERNAL const char *duk_str_func_name_required = "function name required";
  9918. /* Executor */
  9919. DUK_INTERNAL const char *duk_str_internal_error_exec_longjmp = "internal error in bytecode executor longjmp handler";
  9920. /* Regexp */
  9921. DUK_INTERNAL const char *duk_str_invalid_quantifier_no_atom = "quantifier without preceding atom";
  9922. DUK_INTERNAL const char *duk_str_invalid_quantifier_values = "quantifier values invalid (qmin > qmax)";
  9923. DUK_INTERNAL const char *duk_str_quantifier_too_many_copies = "quantifier expansion requires too many atom copies";
  9924. DUK_INTERNAL const char *duk_str_unexpected_closing_paren = "unexpected closing parenthesis";
  9925. DUK_INTERNAL const char *duk_str_unexpected_end_of_pattern = "unexpected end of pattern";
  9926. DUK_INTERNAL const char *duk_str_unexpected_regexp_token = "unexpected token in regexp";
  9927. DUK_INTERNAL const char *duk_str_invalid_regexp_flags = "invalid regexp flags";
  9928. DUK_INTERNAL const char *duk_str_invalid_backrefs = "invalid backreference(s)";
  9929. DUK_INTERNAL const char *duk_str_regexp_backtrack_failed = "regexp backtrack failed";
  9930. DUK_INTERNAL const char *duk_str_regexp_advance_failed = "regexp advance failed";
  9931. DUK_INTERNAL const char *duk_str_regexp_internal_error = "regexp internal error";
  9932. /* Limits */
  9933. DUK_INTERNAL const char *duk_str_valstack_limit = "valstack limit";
  9934. DUK_INTERNAL const char *duk_str_callstack_limit = "callstack limit";
  9935. DUK_INTERNAL const char *duk_str_catchstack_limit = "catchstack limit";
  9936. DUK_INTERNAL const char *duk_str_object_property_limit = "object property limit";
  9937. DUK_INTERNAL const char *duk_str_prototype_chain_limit = "prototype chain limit";
  9938. DUK_INTERNAL const char *duk_str_bound_chain_limit = "function call bound chain limit";
  9939. DUK_INTERNAL const char *duk_str_c_callstack_limit = "C call stack depth limit";
  9940. DUK_INTERNAL const char *duk_str_compiler_recursion_limit = "compiler recursion limit";
  9941. DUK_INTERNAL const char *duk_str_bytecode_limit = "bytecode limit";
  9942. DUK_INTERNAL const char *duk_str_reg_limit = "register limit";
  9943. DUK_INTERNAL const char *duk_str_temp_limit = "temp limit";
  9944. DUK_INTERNAL const char *duk_str_const_limit = "const limit";
  9945. DUK_INTERNAL const char *duk_str_func_limit = "function limit";
  9946. DUK_INTERNAL const char *duk_str_regexp_compiler_recursion_limit = "regexp compiler recursion limit";
  9947. DUK_INTERNAL const char *duk_str_regexp_executor_recursion_limit = "regexp executor recursion limit";
  9948. DUK_INTERNAL const char *duk_str_regexp_executor_step_limit = "regexp step limit";
  9949. /* Misc */
  9950. DUK_INTERNAL const char *duk_str_anon = "anon";
  9951. DUK_INTERNAL const char *duk_str_realloc_failed = "realloc failed";
  9952. #line 1 "duk_debug_macros.c"
  9953. /*
  9954. * Debugging macro calls.
  9955. */
  9956. /* include removed: duk_internal.h */
  9957. #ifdef DUK_USE_DEBUG
  9958. /*
  9959. * Debugging enabled
  9960. */
  9961. #include <stdio.h>
  9962. #include <stdlib.h>
  9963. #include <stdarg.h>
  9964. #define DUK__DEBUG_BUFSIZE DUK_USE_DEBUG_BUFSIZE
  9965. DUK_LOCAL char duk__debug_buf[DUK__DEBUG_BUFSIZE];
  9966. DUK_LOCAL const char *duk__get_level_string(duk_small_int_t level) {
  9967. switch ((int) level) {
  9968. case DUK_LEVEL_DEBUG:
  9969. return "D";
  9970. case DUK_LEVEL_DDEBUG:
  9971. return "DD";
  9972. case DUK_LEVEL_DDDEBUG:
  9973. return "DDD";
  9974. }
  9975. return "???";
  9976. }
  9977. #ifdef DUK_USE_DPRINT_COLORS
  9978. /* http://en.wikipedia.org/wiki/ANSI_escape_code */
  9979. #define DUK__TERM_REVERSE "\x1b[7m"
  9980. #define DUK__TERM_BRIGHT "\x1b[1m"
  9981. #define DUK__TERM_RESET "\x1b[0m"
  9982. #define DUK__TERM_BLUE "\x1b[34m"
  9983. #define DUK__TERM_RED "\x1b[31m"
  9984. DUK_LOCAL const char *duk__get_term_1(duk_small_int_t level) {
  9985. DUK_UNREF(level);
  9986. return (const char *) DUK__TERM_RED;
  9987. }
  9988. DUK_LOCAL const char *duk__get_term_2(duk_small_int_t level) {
  9989. switch ((int) level) {
  9990. case DUK_LEVEL_DEBUG:
  9991. return (const char *) (DUK__TERM_RESET DUK__TERM_BRIGHT);
  9992. case DUK_LEVEL_DDEBUG:
  9993. return (const char *) (DUK__TERM_RESET);
  9994. case DUK_LEVEL_DDDEBUG:
  9995. return (const char *) (DUK__TERM_RESET DUK__TERM_BLUE);
  9996. }
  9997. return (const char *) DUK__TERM_RESET;
  9998. }
  9999. DUK_LOCAL const char *duk__get_term_3(duk_small_int_t level) {
  10000. DUK_UNREF(level);
  10001. return (const char *) DUK__TERM_RESET;
  10002. }
  10003. #else
  10004. DUK_LOCAL const char *duk__get_term_1(duk_small_int_t level) {
  10005. DUK_UNREF(level);
  10006. return (const char *) "";
  10007. }
  10008. DUK_LOCAL const char *duk__get_term_2(duk_small_int_t level) {
  10009. DUK_UNREF(level);
  10010. return (const char *) "";
  10011. }
  10012. DUK_LOCAL const char *duk__get_term_3(duk_small_int_t level) {
  10013. DUK_UNREF(level);
  10014. return (const char *) "";
  10015. }
  10016. #endif /* DUK_USE_DPRINT_COLORS */
  10017. #ifdef DUK_USE_VARIADIC_MACROS
  10018. DUK_INTERNAL void duk_debug_log(duk_small_int_t level, const char *file, duk_int_t line, const char *func, const char *fmt, ...) {
  10019. va_list ap;
  10020. va_start(ap, fmt);
  10021. DUK_MEMZERO((void *) duk__debug_buf, (size_t) DUK__DEBUG_BUFSIZE);
  10022. duk_debug_vsnprintf(duk__debug_buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap);
  10023. #ifdef DUK_USE_DPRINT_RDTSC
  10024. DUK_FPRINTF(DUK_STDERR, "%s[%s] <%llu> %s:%ld (%s):%s %s%s\n",
  10025. (const char *) duk__get_term_1(level),
  10026. (const char *) duk__get_level_string(level),
  10027. (unsigned long long) duk_rdtsc(), /* match the inline asm in duk_features.h */
  10028. (const char *) file,
  10029. (long) line,
  10030. (const char *) func,
  10031. (const char *) duk__get_term_2(level),
  10032. (const char *) duk__debug_buf,
  10033. (const char *) duk__get_term_3(level));
  10034. #else
  10035. DUK_FPRINTF(DUK_STDERR, "%s[%s] %s:%ld (%s):%s %s%s\n",
  10036. (const char *) duk__get_term_1(level),
  10037. (const char *) duk__get_level_string(level),
  10038. (const char *) file,
  10039. (long) line,
  10040. (const char *) func,
  10041. (const char *) duk__get_term_2(level),
  10042. (const char *) duk__debug_buf,
  10043. (const char *) duk__get_term_3(level));
  10044. #endif
  10045. DUK_FFLUSH(DUK_STDERR);
  10046. va_end(ap);
  10047. }
  10048. #else /* DUK_USE_VARIADIC_MACROS */
  10049. DUK_INTERNAL char duk_debug_file_stash[DUK_DEBUG_STASH_SIZE];
  10050. DUK_INTERNAL char duk_debug_line_stash[DUK_DEBUG_STASH_SIZE];
  10051. DUK_INTERNAL char duk_debug_func_stash[DUK_DEBUG_STASH_SIZE];
  10052. DUK_INTERNAL duk_small_int_t duk_debug_level_stash;
  10053. DUK_INTERNAL void duk_debug_log(const char *fmt, ...) {
  10054. va_list ap;
  10055. duk_small_int_t level = duk_debug_level_stash;
  10056. va_start(ap, fmt);
  10057. DUK_MEMZERO((void *) duk__debug_buf, (size_t) DUK__DEBUG_BUFSIZE);
  10058. duk_debug_vsnprintf(duk__debug_buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap);
  10059. #ifdef DUK_USE_DPRINT_RDTSC
  10060. DUK_FPRINTF(DUK_STDERR, "%s[%s] <%llu> %s:%s (%s):%s %s%s\n",
  10061. (const char *) duk__get_term_1(level),
  10062. (const char *) duk__get_level_string(duk_debug_level_stash),
  10063. (unsigned long long) duk_rdtsc(), /* match duk_features.h */
  10064. (const char *) duk_debug_file_stash,
  10065. (const char *) duk_debug_line_stash,
  10066. (const char *) duk_debug_func_stash,
  10067. (const char *) duk__get_term_2(level),
  10068. (const char *) duk__debug_buf,
  10069. (const char *) duk__get_term_3(level));
  10070. #else
  10071. DUK_FPRINTF(DUK_STDERR, "%s[%s] %s:%s (%s):%s %s%s\n",
  10072. (const char *) duk__get_term_1(level),
  10073. (const char *) duk__get_level_string(duk_debug_level_stash),
  10074. (const char *) duk_debug_file_stash,
  10075. (const char *) duk_debug_line_stash,
  10076. (const char *) duk_debug_func_stash,
  10077. (const char *) duk__get_term_2(level),
  10078. (const char *) duk__debug_buf,
  10079. (const char *) duk__get_term_3(level));
  10080. #endif
  10081. DUK_FFLUSH(DUK_STDERR);
  10082. va_end(ap);
  10083. }
  10084. #endif /* DUK_USE_VARIADIC_MACROS */
  10085. #else /* DUK_USE_DEBUG */
  10086. /*
  10087. * Debugging disabled
  10088. */
  10089. #endif /* DUK_USE_DEBUG */
  10090. #line 1 "duk_builtins.c"
  10091. /*
  10092. * Automatically generated by genbuiltins.py, do not edit!
  10093. */
  10094. /* include removed: duk_internal.h */
  10095. #if defined(DUK_USE_DOUBLE_LE)
  10096. DUK_INTERNAL const duk_uint8_t duk_strings_data[2624] = {
  10097. 55,86,227,24,145,55,102,120,144,3,63,94,228,54,100,137,186,50,11,164,109,
  10098. 77,215,5,61,35,106,206,149,110,4,254,219,237,58,8,196,24,103,74,183,2,127,
  10099. 103,246,93,4,98,12,47,180,67,103,246,127,101,208,70,32,194,186,134,207,236,
  10100. 254,203,160,140,65,133,246,136,108,254,199,237,186,8,196,24,87,80,217,253,
  10101. 143,219,116,17,136,49,30,209,13,159,220,116,75,3,30,65,244,17,136,48,174,
  10102. 209,13,159,220,116,17,136,48,158,161,179,251,142,130,49,6,17,209,130,96,
  10103. 237,80,75,47,160,140,65,142,134,133,41,34,110,134,133,41,34,3,25,110,8,22,
  10104. 158,130,38,163,8,217,200,158,76,156,210,117,128,153,203,210,70,46,137,187,
  10105. 18,27,164,187,201,209,130,100,55,91,70,4,145,63,66,231,44,128,105,187,41,
  10106. 197,13,49,122,8,196,24,71,75,70,138,104,115,77,215,5,36,20,201,214,209,107,
  10107. 79,104,209,144,168,105,6,207,251,209,104,209,125,212,227,66,127,235,191,
  10108. 239,232,180,90,52,95,69,247,83,141,9,255,174,255,191,162,211,80,210,253,23,
  10109. 221,78,52,39,254,183,254,254,139,72,105,126,139,238,167,26,19,255,91,255,
  10110. 127,69,166,129,191,69,247,83,141,9,255,175,255,191,162,213,26,50,23,232,
  10111. 190,234,113,161,63,245,115,119,86,227,118,83,138,26,98,9,110,48,86,22,148,
  10112. 160,152,22,82,70,46,137,44,8,180,163,32,104,98,206,32,17,7,16,88,101,100,
  10113. 206,42,70,36,108,205,18,74,140,33,196,230,60,2,152,146,33,38,230,8,36,79,
  10114. 182,251,65,156,151,24,200,33,145,162,25,80,209,24,67,0,166,68,52,174,61,73,
  10115. 25,33,205,25,27,84,177,195,234,220,1,144,105,99,135,217,16,17,17,208,72,
  10116. 199,179,60,93,100,146,49,232,162,64,76,135,19,152,244,44,136,223,98,67,4,
  10117. 18,33,247,217,158,36,0,209,190,156,13,26,201,21,111,165,67,64,180,100,145,
  10118. 62,250,32,45,100,33,55,214,1,229,223,65,19,72,187,236,206,137,35,125,120,
  10119. 190,201,104,105,15,190,201,212,136,136,125,246,160,137,27,83,239,171,37,
  10120. 200,218,159,125,168,34,192,61,27,233,93,22,1,114,78,250,28,76,130,112,200,
  10121. 93,245,164,188,207,190,204,17,49,38,109,246,160,93,8,119,185,13,153,34,173,
  10122. 246,113,0,136,48,76,10,90,26,78,182,140,9,34,130,161,100,235,64,194,9,226,
  10123. 44,166,1,41,221,153,226,235,118,120,121,58,72,197,209,63,71,69,76,15,34,
  10124. 164,73,244,171,112,39,246,223,104,169,18,125,42,220,9,253,159,217,38,68,
  10125. 159,104,134,207,236,254,201,18,36,250,134,207,236,254,201,50,36,251,68,54,
  10126. 127,99,246,200,145,39,212,54,127,99,246,200,145,39,218,33,179,251,131,200,
  10127. 147,234,27,63,184,81,137,62,149,110,4,254,219,237,20,98,79,165,91,129,63,
  10128. 179,251,36,152,147,237,16,217,253,159,217,32,196,159,80,217,253,159,217,36,
  10129. 196,159,104,134,207,236,126,217,6,36,250,134,207,236,126,217,6,36,251,68,
  10130. 54,127,112,115,18,125,67,103,247,8,149,2,8,196,24,143,131,137,146,90,121,
  10131. 35,162,44,140,35,102,160,226,100,235,138,89,18,102,13,10,82,68,200,151,106,
  10132. 130,88,131,4,192,73,225,228,85,162,137,147,168,108,252,18,42,209,68,201,
  10133. 212,54,126,89,23,104,162,100,245,17,230,207,193,34,237,20,76,158,162,60,
  10134. 217,249,100,109,162,137,147,163,117,2,178,120,36,109,162,137,147,163,117,2,
  10135. 178,121,100,101,162,137,147,165,91,129,63,4,140,180,81,50,116,171,112,39,
  10136. 229,145,150,138,38,78,161,179,251,63,178,240,72,203,69,19,39,80,217,253,
  10137. 159,217,121,100,109,162,137,147,212,71,155,63,179,251,47,4,141,180,81,50,
  10138. 122,136,243,103,246,127,101,229,145,150,138,38,78,161,179,251,31,182,240,
  10139. 72,203,69,19,39,80,217,253,143,219,121,100,109,162,137,147,212,71,155,63,
  10140. 177,251,111,4,141,180,81,50,122,136,243,103,246,63,109,229,145,54,138,38,
  10141. 78,161,179,251,133,90,40,153,61,68,121,179,251,132,196,128,31,80,217,248,
  10142. 36,76,72,1,245,13,159,150,69,68,128,31,168,143,54,126,9,21,18,0,126,162,60,
  10143. 217,249,100,100,72,1,244,110,160,86,79,4,140,137,0,62,141,212,10,201,229,
  10144. 145,113,32,7,210,173,192,159,130,69,196,128,31,74,183,2,126,89,23,18,0,125,
  10145. 67,103,246,127,101,224,145,113,32,7,212,54,127,103,246,94,89,25,18,0,126,
  10146. 162,60,217,253,159,217,120,36,100,72,1,250,136,243,103,246,127,101,229,145,
  10147. 113,32,7,212,54,127,99,246,222,9,23,18,0,125,67,103,246,63,109,229,145,145,
  10148. 32,7,234,35,205,159,216,253,183,130,70,68,128,31,168,143,54,127,99,246,222,
  10149. 89,17,18,0,125,67,103,247,9,137,0,63,81,30,108,254,224,130,115,240,98,66,
  10150. 128,92,136,84,45,101,180,81,50,28,78,99,193,18,40,56,153,58,178,52,211,58,
  10151. 17,46,134,133,41,34,164,75,164,104,156,52,52,199,37,222,232,206,66,64,207,
  10152. 18,66,136,137,19,173,62,46,155,181,167,72,147,235,226,233,186,120,121,58,
  10153. 226,157,214,111,84,76,73,36,109,24,72,130,100,112,200,178,76,157,124,92,
  10154. 242,70,120,25,193,34,245,241,117,240,97,1,107,33,25,212,54,160,90,7,244,29,
  10155. 24,38,66,254,223,215,125,119,215,126,232,190,43,226,67,244,1,250,193,125,
  10156. 111,216,11,234,254,192,63,96,159,173,234,26,84,53,19,194,126,175,168,105,
  10157. 80,212,79,8,234,26,84,53,19,193,156,20,144,83,52,167,20,52,198,109,24,18,
  10158. 68,225,115,150,64,53,52,104,200,84,52,131,76,167,20,52,200,46,7,48,52,146,
  10159. 132,102,57,33,165,139,168,209,154,32,104,220,193,189,214,27,16,209,176,23,
  10160. 26,220,98,149,110,116,70,75,188,98,116,136,34,33,101,4,192,223,178,32,38,6,
  10161. 144,18,67,72,1,58,67,0,100,95,74,17,159,217,31,210,132,103,246,58,251,33,
  10162. 121,232,55,150,227,125,143,216,16,190,91,141,246,68,31,150,223,178,39,150,
  10163. 223,177,251,0,244,135,97,37,32,24,132,104,24,66,161,175,164,202,134,140,
  10164. 151,39,212,125,255,221,125,74,86,9,79,168,104,201,116,178,139,154,22,134,
  10165. 145,72,51,93,18,116,64,145,13,39,82,34,33,38,73,76,132,185,4,185,187,198,
  10166. 100,229,233,197,13,49,228,73,247,4,4,78,98,79,184,32,34,105,187,201,147,
  10167. 154,185,187,200,147,165,233,197,13,50,230,239,82,98,151,167,20,52,206,145,
  10168. 39,234,76,69,245,22,190,224,128,138,228,73,244,180,90,251,130,2,43,145,39,
  10169. 234,76,76,243,155,51,162,68,159,88,230,204,234,145,39,234,76,67,240,38,67,
  10170. 200,147,232,193,50,46,68,159,169,49,31,206,164,100,137,18,125,59,169,25,54,
  10171. 68,159,169,49,51,200,109,38,73,42,68,159,88,134,210,100,147,100,73,250,147,
  10172. 20,188,65,57,163,146,164,73,246,68,19,154,57,74,68,159,169,49,51,200,90,
  10173. 209,34,9,205,28,159,34,79,172,66,214,137,16,78,104,228,121,18,125,154,24,
  10174. 72,152,147,236,208,194,101,205,39,92,82,200,147,145,137,63,82,98,103,144,
  10175. 181,162,68,19,154,57,60,196,159,88,133,173,18,32,156,209,201,166,36,253,73,
  10176. 138,94,32,156,209,201,70,36,251,34,9,205,28,154,98,79,212,152,153,228,54,
  10177. 147,36,148,98,79,172,67,105,50,73,102,36,253,73,136,254,117,35,36,24,147,
  10178. 233,221,72,201,38,36,253,73,136,126,6,12,98,79,163,6,20,98,79,212,152,135,
  10179. 224,76,135,49,39,209,130,100,89,137,63,82,98,103,156,217,157,6,36,250,199,
  10180. 54,103,113,137,63,82,98,47,168,181,247,4,4,86,98,79,165,162,215,220,16,17,
  10181. 57,137,62,205,12,36,166,238,173,194,2,201,217,161,132,236,167,20,52,210,
  10182. 155,186,183,8,11,39,70,9,147,178,156,80,211,50,110,236,208,194,118,83,138,
  10183. 26,102,77,221,24,38,78,202,113,67,76,54,186,195,245,38,34,188,17,145,23,55,
  10184. 117,241,32,145,36,57,173,155,186,75,189,205,35,102,128,44,243,119,74,139,
  10185. 144,113,243,221,36,77,21,38,144,210,161,168,158,35,230,144,192,154,42,77,
  10186. 33,165,67,81,60,15,173,7,90,159,49,13,213,64,186,17,62,96,47,170,129,116,
  10187. 33,165,64,202,113,36,226,134,70,110,234,220,32,44,157,163,222,72,244,64,
  10188. 145,23,55,118,143,121,35,209,2,68,140,221,213,184,64,89,58,183,88,145,232,
  10189. 129,34,46,110,234,221,98,71,162,4,136,153,80,50,156,80,211,22,79,90,38,105,
  10190. 16,17,17,207,18,61,96,17,10,192,76,71,106,220,32,44,157,19,152,240,68,138,
  10191. 17,193,30,137,195,39,65,51,8,224,143,65,54,22,46,103,68,112,71,162,112,200,
  10192. 184,144,116,17,59,20,24,243,52,72,58,8,134,42,23,50,68,108,3,206,87,71,164,
  10193. 0,142,73,57,132,41,42,72,225,107,4,167,212,52,100,191,92,83,161,163,37,250,
  10194. 226,158,141,145,208,89,154,79,90,4,66,73,209,153,100,180,8,133,145,208,89,
  10195. 158,36,169,35,34,17,244,145,198,247,60,137,114,26,97,57,162,4,206,137,116,
  10196. 17,136,48,144,68,212,97,27,57,24,64,90,201,18,5,13,25,4,5,172,160,123,215,
  10197. 138,62,46,121,35,60,117,18,233,27,70,18,32,10,200,212,75,175,139,166,233,
  10198. 225,228,235,138,227,130,93,117,155,215,197,207,36,103,131,212,11,161,58,
  10199. 226,186,110,234,220,32,44,157,148,226,134,153,19,119,101,56,161,166,88,156,
  10200. 217,78,52,20,221,17,200,147,25,137,53,17,180,97,34,0,172,140,19,154,84,26,
  10201. 145,0,86,68,90,40,152,2,178,22,160,93,8,69,19,18,98,37,210,94,100,108,144,
  10202. 21,145,8,151,75,23,100,141,66,37,217,16,11,32,226,248,146,164,108,250,75,
  10203. 204,141,146,28,217,24,177,33,50,66,72,128,92,6,66,161,164,235,226,231,146,
  10204. 51,65,36,225,144,168,105,58,248,185,228,140,240,97,68,128,153,38,98,79,174,
  10205. 179,122,248,185,228,140,241,214,129,132,150,12,73,245,214,111,95,23,60,145,
  10206. 158,58,50,72,81,67,230,232,184,196,159,95,23,77,211,195,201,215,21,47,139,
  10207. 166,233,225,228,50,200,211,76,229,2,201,25,149,241,67,102,138,52,146,16,30,
  10208. 67,18,66,3,201,34,52,78,25,61,72,160,94,115,30,230,145,179,73,26,39,12,158,
  10209. 164,81,33,144,78,25,61,72,160,94,115,30,230,145,179,72,200,39,12,158,164,
  10210. 80,132,75,165,67,81,50,21,18,235,65,214,169,224,140,137,210,173,192,154,30,
  10211. 8,200,157,67,102,66,84,11,71,169,20,19,209,139,162,158,207,15,39,73,24,186,
  10212. 43,236,176,217,130,253,36,98,232,187,177,33,73,18,52,68,233,35,23,69,93,
  10213. 136,26,98,116,145,139,162,158,146,160,95,73,24,186,37,12,72,5,16,64,145,10,
  10214. 32,76,71,64,156,217,161,180,34,6,64,208,198,36,78,50,20,20,92,204,50,44,
  10215. 147,32,134,226,17,114,33,202,134,129,107,192,202,232,160,180,104,166,135,
  10216. 52,72,40,144,213,33,178,152,26,34,56,163,105,44,104,146,116,139,77,43,34,
  10217. 98,57,38,116,72,179,60,93,97,206,56,52,240,242,56,163,168,34,74,185,3,45,
  10218. 142,133,144,150,68,206,81,44,18,145,68,230,202,100,35,104,195,18,239,116,
  10219. 102,114,94,100,104,228,100,49,238,140,203,42,60,145,35,104,181,146,113,161,
  10220. 10,80,46,68,82,24,245,145,132,108,228,148,54,100,137,64,34,13,100,153,222,
  10221. 1,40,6,33,223,20,84,19,34,95,23,76,130,153,6,103,208,43,64,141,41,130,104,
  10222. 17,112,130,44,96,
  10223. };
  10224. /* to convert a heap stridx to a token number, subtract
  10225. * DUK_STRIDX_START_RESERVED and add DUK_TOK_START_RESERVED.
  10226. */
  10227. /* native functions: 147 */
  10228. DUK_INTERNAL const duk_c_function duk_bi_native_functions[147] = {
  10229. duk_bi_array_constructor,
  10230. duk_bi_array_constructor_is_array,
  10231. duk_bi_array_prototype_concat,
  10232. duk_bi_array_prototype_indexof_shared,
  10233. duk_bi_array_prototype_iter_shared,
  10234. duk_bi_array_prototype_join_shared,
  10235. duk_bi_array_prototype_pop,
  10236. duk_bi_array_prototype_push,
  10237. duk_bi_array_prototype_reduce_shared,
  10238. duk_bi_array_prototype_reverse,
  10239. duk_bi_array_prototype_shift,
  10240. duk_bi_array_prototype_slice,
  10241. duk_bi_array_prototype_sort,
  10242. duk_bi_array_prototype_splice,
  10243. duk_bi_array_prototype_to_string,
  10244. duk_bi_array_prototype_unshift,
  10245. duk_bi_arraybuffer_constructor,
  10246. duk_bi_arraybuffer_isview,
  10247. duk_bi_boolean_constructor,
  10248. duk_bi_boolean_prototype_tostring_shared,
  10249. duk_bi_buffer_compare_shared,
  10250. duk_bi_buffer_constructor,
  10251. duk_bi_buffer_prototype_tostring_shared,
  10252. duk_bi_buffer_readfield,
  10253. duk_bi_buffer_slice_shared,
  10254. duk_bi_buffer_writefield,
  10255. duk_bi_dataview_constructor,
  10256. duk_bi_date_constructor,
  10257. duk_bi_date_constructor_now,
  10258. duk_bi_date_constructor_parse,
  10259. duk_bi_date_constructor_utc,
  10260. duk_bi_date_prototype_get_shared,
  10261. duk_bi_date_prototype_get_timezone_offset,
  10262. duk_bi_date_prototype_set_shared,
  10263. duk_bi_date_prototype_set_time,
  10264. duk_bi_date_prototype_to_json,
  10265. duk_bi_date_prototype_tostring_shared,
  10266. duk_bi_date_prototype_value_of,
  10267. duk_bi_duktape_object_act,
  10268. duk_bi_duktape_object_compact,
  10269. duk_bi_duktape_object_dec,
  10270. duk_bi_duktape_object_enc,
  10271. duk_bi_duktape_object_fin,
  10272. duk_bi_duktape_object_gc,
  10273. duk_bi_duktape_object_info,
  10274. duk_bi_error_constructor_shared,
  10275. duk_bi_error_prototype_filename_getter,
  10276. duk_bi_error_prototype_linenumber_getter,
  10277. duk_bi_error_prototype_nop_setter,
  10278. duk_bi_error_prototype_stack_getter,
  10279. duk_bi_error_prototype_to_string,
  10280. duk_bi_function_constructor,
  10281. duk_bi_function_prototype,
  10282. duk_bi_function_prototype_apply,
  10283. duk_bi_function_prototype_bind,
  10284. duk_bi_function_prototype_call,
  10285. duk_bi_function_prototype_to_string,
  10286. duk_bi_global_object_decode_uri,
  10287. duk_bi_global_object_decode_uri_component,
  10288. duk_bi_global_object_encode_uri,
  10289. duk_bi_global_object_encode_uri_component,
  10290. duk_bi_global_object_escape,
  10291. duk_bi_global_object_eval,
  10292. duk_bi_global_object_is_finite,
  10293. duk_bi_global_object_is_nan,
  10294. duk_bi_global_object_parse_float,
  10295. duk_bi_global_object_parse_int,
  10296. duk_bi_global_object_print_helper,
  10297. duk_bi_global_object_require,
  10298. duk_bi_global_object_unescape,
  10299. duk_bi_json_object_parse,
  10300. duk_bi_json_object_stringify,
  10301. duk_bi_logger_constructor,
  10302. duk_bi_logger_prototype_fmt,
  10303. duk_bi_logger_prototype_log_shared,
  10304. duk_bi_logger_prototype_raw,
  10305. duk_bi_math_object_max,
  10306. duk_bi_math_object_min,
  10307. duk_bi_math_object_onearg_shared,
  10308. duk_bi_math_object_random,
  10309. duk_bi_math_object_twoarg_shared,
  10310. duk_bi_nodejs_buffer_byte_length,
  10311. duk_bi_nodejs_buffer_concat,
  10312. duk_bi_nodejs_buffer_constructor,
  10313. duk_bi_nodejs_buffer_copy,
  10314. duk_bi_nodejs_buffer_fill,
  10315. duk_bi_nodejs_buffer_is_buffer,
  10316. duk_bi_nodejs_buffer_is_encoding,
  10317. duk_bi_nodejs_buffer_tojson,
  10318. duk_bi_nodejs_buffer_tostring,
  10319. duk_bi_nodejs_buffer_write,
  10320. duk_bi_number_constructor,
  10321. duk_bi_number_prototype_to_exponential,
  10322. duk_bi_number_prototype_to_fixed,
  10323. duk_bi_number_prototype_to_locale_string,
  10324. duk_bi_number_prototype_to_precision,
  10325. duk_bi_number_prototype_to_string,
  10326. duk_bi_number_prototype_value_of,
  10327. duk_bi_object_constructor,
  10328. duk_bi_object_constructor_create,
  10329. duk_bi_object_constructor_define_properties,
  10330. duk_bi_object_constructor_define_property,
  10331. duk_bi_object_constructor_get_own_property_descriptor,
  10332. duk_bi_object_constructor_is_extensible,
  10333. duk_bi_object_constructor_is_sealed_frozen_shared,
  10334. duk_bi_object_constructor_keys_shared,
  10335. duk_bi_object_constructor_prevent_extensions,
  10336. duk_bi_object_constructor_seal_freeze_shared,
  10337. duk_bi_object_getprototype_shared,
  10338. duk_bi_object_prototype_has_own_property,
  10339. duk_bi_object_prototype_is_prototype_of,
  10340. duk_bi_object_prototype_property_is_enumerable,
  10341. duk_bi_object_prototype_to_locale_string,
  10342. duk_bi_object_prototype_to_string,
  10343. duk_bi_object_prototype_value_of,
  10344. duk_bi_object_setprototype_shared,
  10345. duk_bi_pointer_constructor,
  10346. duk_bi_pointer_prototype_tostring_shared,
  10347. duk_bi_proxy_constructor,
  10348. duk_bi_regexp_constructor,
  10349. duk_bi_regexp_prototype_exec,
  10350. duk_bi_regexp_prototype_test,
  10351. duk_bi_regexp_prototype_to_string,
  10352. duk_bi_string_constructor,
  10353. duk_bi_string_constructor_from_char_code,
  10354. duk_bi_string_prototype_caseconv_shared,
  10355. duk_bi_string_prototype_char_at,
  10356. duk_bi_string_prototype_char_code_at,
  10357. duk_bi_string_prototype_concat,
  10358. duk_bi_string_prototype_indexof_shared,
  10359. duk_bi_string_prototype_locale_compare,
  10360. duk_bi_string_prototype_match,
  10361. duk_bi_string_prototype_replace,
  10362. duk_bi_string_prototype_search,
  10363. duk_bi_string_prototype_slice,
  10364. duk_bi_string_prototype_split,
  10365. duk_bi_string_prototype_substr,
  10366. duk_bi_string_prototype_substring,
  10367. duk_bi_string_prototype_to_string,
  10368. duk_bi_string_prototype_trim,
  10369. duk_bi_thread_constructor,
  10370. duk_bi_thread_current,
  10371. duk_bi_thread_resume,
  10372. duk_bi_thread_yield,
  10373. duk_bi_type_error_thrower,
  10374. duk_bi_typedarray_constructor,
  10375. duk_bi_typedarray_set,
  10376. };
  10377. DUK_INTERNAL const duk_uint8_t duk_builtins_data[1952] = {
  10378. 105,195,75,16,121,40,105,51,14,252,104,52,8,131,72,0,115,225,65,165,236,55,
  10379. 243,6,145,32,210,24,210,182,25,249,35,120,216,99,226,13,78,225,116,177,164,
  10380. 180,44,192,4,202,52,150,220,24,0,169,70,146,219,123,0,23,40,210,91,110,96,
  10381. 3,37,26,75,109,172,0,108,163,73,109,177,128,14,148,105,45,181,176,1,242,
  10382. 144,56,209,32,94,6,167,101,98,80,211,24,1,250,67,72,168,67,232,13,46,128,
  10383. 47,162,52,164,0,62,80,163,72,128,61,40,107,26,7,37,20,53,200,131,88,0,66,
  10384. 134,185,16,98,80,215,34,11,96,0,138,26,228,65,76,0,69,67,92,136,37,128,6,
  10385. 168,107,145,4,48,1,165,13,114,32,118,0,44,161,174,68,12,192,7,148,53,200,
  10386. 129,88,1,26,134,165,48,130,80,31,255,241,69,224,0,0,0,0,0,0,124,63,174,32,
  10387. 0,0,0,0,0,0,120,63,175,98,7,140,16,116,194,7,12,48,108,196,6,140,80,100,
  10388. 198,6,12,112,92,200,5,140,149,192,202,91,204,181,184,204,91,76,213,176,206,
  10389. 90,204,240,84,208,5,13,9,124,210,43,13,24,64,226,131,205,112,56,216,3,77,
  10390. 152,48,218,130,205,184,40,220,130,77,216,32,222,129,205,248,24,224,129,78,
  10391. 25,214,163,226,90,80,145,104,65,37,157,0,150,99,242,89,78,73,100,58,37,140,
  10392. 236,150,35,194,88,79,73,96,69,37,125,12,122,188,134,62,0,2,165,68,39,255,
  10393. 255,193,43,67,0,0,80,127,192,58,182,216,80,0,21,59,154,64,0,107,76,200,172,
  10394. 180,146,176,198,138,187,43,42,204,136,170,181,146,168,214,80,0,26,155,81,
  10395. 42,77,4,168,180,20,0,6,160,206,74,123,73,64,0,127,255,4,10,153,219,28,198,
  10396. 163,184,130,140,224,10,43,144,40,141,164,161,183,18,132,222,64,161,127,128,
  10397. 0,63,225,1,109,74,8,137,71,56,5,4,213,20,3,115,233,249,177,240,80,255,192,
  10398. 6,120,2,64,127,195,0,173,28,56,20,96,80,128,0,206,192,143,167,64,164,156,
  10399. 131,2,112,14,125,55,9,4,216,40,19,80,180,77,3,9,51,13,94,153,7,159,76,64,
  10400. 207,192,0,102,0,103,255,255,242,240,67,73,112,33,168,0,12,180,16,212,0,10,
  10401. 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,
  10402. 135,195,224,127,196,2,87,132,17,82,143,20,10,44,80,36,239,196,147,63,146,
  10403. 119,0,125,49,129,52,152,64,154,128,0,201,96,137,36,131,36,142,17,18,40,82,
  10404. 77,97,145,33,135,68,130,37,17,247,208,71,159,65,29,125,8,0,12,113,244,32,0,
  10405. 49,184,176,70,162,16,20,95,240,0,7,252,80,37,120,193,81,196,194,0,3,69,19,
  10406. 0,81,191,197,140,192,255,255,255,255,255,255,239,127,140,64,1,0,0,0,0,0,0,
  10407. 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,
  10408. 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,
  10409. 16,69,23,73,19,92,36,73,124,129,71,255,0,56,136,233,34,3,223,208,241,192,3,
  10410. 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,
  10411. 42,49,32,64,0,225,132,129,0,4,133,146,4,0,21,210,72,16,0,103,65,32,64,1,
  10412. 220,228,100,162,146,130,20,74,8,72,248,64,2,33,3,225,0,9,131,143,132,0,42,
  10413. 12,62,16,0,184,40,248,64,3,32,131,225,0,13,129,143,132,0,58,4,62,16,0,248,
  10414. 8,248,64,4,32,3,225,0,17,127,143,132,0,73,252,62,16,1,55,232,248,64,5,31,
  10415. 131,225,0,21,125,143,132,0,89,244,62,16,1,119,201,0,31,4,68,123,144,148,0,
  10416. 97,236,66,80,1,151,169,10,248,0,211,208,133,124,0,109,230,66,254,0,56,242,
  10417. 33,127,0,29,120,144,207,128,15,60,8,103,192,7,221,228,37,0,32,119,16,148,0,
  10418. 133,218,66,190,0,68,236,33,95,0,35,117,144,191,128,18,58,136,95,192,9,92,
  10419. 195,225,0,38,114,144,148,0,156,41,31,224,0,15,249,1,138,144,64,192,2,2,225,
  10420. 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,
  10421. 239,64,10,79,248,0,3,254,72,86,209,5,155,36,17,46,185,137,129,109,203,140,
  10422. 11,78,94,96,13,28,200,1,74,255,0,2,127,202,4,218,43,131,100,130,32,5,47,
  10423. 252,0,9,255,44,19,104,173,237,146,8,128,20,207,240,0,39,252,192,77,162,183,
  10424. 54,72,34,0,83,127,192,0,159,243,65,54,138,218,217,32,136,1,78,255,0,2,127,
  10425. 206,4,218,43,99,100,130,32,5,63,252,0,9,255,60,19,104,173,109,146,8,128,15,
  10426. 255,242,27,16,26,85,197,34,194,175,193,80,26,240,5,149,109,110,236,90,192,
  10427. 144,26,208,59,206,126,191,144,139,185,143,218,176,63,160,138,217,81,197,
  10428. 125,207,218,144,3,185,73,133,94,242,246,207,218,112,6,11,81,21,62,200,66,
  10429. 80,26,80,51,78,223,217,167,168,57,143,218,48,51,78,223,217,167,168,61,143,
  10430. 210,104,39,17,158,156,80,0,22,114,113,64,0,153,169,197,0,3,102,40,33,150,
  10431. 156,80,0,70,82,113,64,1,89,41,197,0,6,100,39,20,0,29,142,156,80,0,134,50,
  10432. 98,243,21,53,121,136,160,144,0,22,26,120,24,73,197,0,9,96,167,20,0,41,128,
  10433. 156,80,0,181,250,113,64,3,1,255,254,0,81,20,100,47,145,216,23,255,240,0,11,
  10434. 255,248,0,3,255,252,81,252,0,0,0,0,8,1,220,68,0,129,167,1,26,144,9,165,0,
  10435. 26,177,199,197,132,30,147,16,120,86,65,217,80,240,232,164,120,114,80,60,52,
  10436. 39,32,84,223,192,15,59,30,129,156,115,6,81,160,7,253,40,0,5,81,252,0,1,255,
  10437. 78,0,84,113,96,128,0,209,69,128,21,87,240,0,7,253,72,1,81,221,66,0,3,69,
  10438. 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,
  10439. 113,3,20,146,12,18,200,47,74,30,23,37,15,128,0,143,146,135,192,0,133,169,
  10440. 67,224,0,98,196,161,240,0,65,90,80,248,0,41,63,255,194,109,65,11,137,191,
  10441. 174,45,153,98,242,229,191,147,102,8,190,94,92,183,242,65,167,114,12,188,
  10442. 185,111,228,131,70,29,217,54,105,221,156,0,171,255,128,9,208,68,128,255,
  10443. 174,0,25,168,194,64,0,130,177,254,0,0,255,176,1,3,120,186,64,12,13,194,233,
  10444. 0,32,54,139,164,0,196,216,46,144,2,19,88,186,64,12,141,66,233,0,34,52,139,
  10445. 164,0,140,208,46,144,2,67,56,203,64,12,12,195,45,0,32,50,140,180,0,196,200,
  10446. 50,208,2,19,24,203,64,12,140,67,45,0,34,48,140,180,0,140,192,50,208,2,64,
  10447. 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,
  10448. 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,
  10449. 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,
  10450. 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,
  10451. 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,
  10452. 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,
  10453. 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,
  10454. 80,171,17,64,162,132,248,162,64,0,193,255,138,5,137,161,116,38,69,210,0,32,
  10455. 152,23,72,0,10,92,93,32,1,41,97,116,128,8,165,69,210,0,50,148,23,72,0,18,
  10456. 76,93,32,1,73,33,116,128,9,36,69,210,0,52,144,23,72,0,26,60,93,32,1,104,
  10457. 225,116,128,2,35,69,210,0,24,140,23,104,0,42,44,93,160,1,168,161,118,128,
  10458. 10,162,69,218,0,58,136,25,98,28,101,160,2,8,97,150,128,0,161,70,90,0,18,
  10459. 132,25,104,0,138,12,101,160,3,40,33,150,128,1,32,70,90,0,20,128,25,104,0,
  10460. 145,252,101,160,3,71,225,150,128,1,159,70,90,0,22,124,25,104,0,33,236,101,
  10461. 160,1,135,161,152,128,2,158,70,98,0,26,120,25,136,0,169,220,102,32,3,180,
  10462. 117,150,57,214,0,157,85,98,112,80,137,241,66,128,0,166,213,33,53,24,66,121,
  10463. 106,0,
  10464. };
  10465. #ifdef DUK_USE_BUILTIN_INITJS
  10466. DUK_INTERNAL const duk_uint8_t duk_initjs_data[187] = {
  10467. 40,102,117,110,99,116,105,111,110,40,100,44,97,41,123,102,117,110,99,116,
  10468. 105,111,110,32,98,40,97,44,98,44,99,41,123,79,98,106,101,99,116,46,100,101,
  10469. 102,105,110,101,80,114,111,112,101,114,116,121,40,97,44,98,44,123,118,97,
  10470. 108,117,101,58,99,44,119,114,105,116,97,98,108,101,58,33,48,44,101,110,117,
  10471. 109,101,114,97,98,108,101,58,33,49,44,99,111,110,102,105,103,117,114,97,98,
  10472. 108,101,58,33,48,125,41,125,98,40,97,46,76,111,103,103,101,114,44,34,99,
  10473. 108,111,103,34,44,110,101,119,32,97,46,76,111,103,103,101,114,40,34,67,34,
  10474. 41,41,59,98,40,97,44,34,109,111,100,76,111,97,100,101,100,34,44,123,125,41,
  10475. 125,41,40,116,104,105,115,44,68,117,107,116,97,112,101,41,59,10,0,
  10476. };
  10477. #endif /* DUK_USE_BUILTIN_INITJS */
  10478. #elif defined(DUK_USE_DOUBLE_BE)
  10479. DUK_INTERNAL const duk_uint8_t duk_strings_data[2624] = {
  10480. 55,86,227,24,145,55,102,120,144,3,63,94,228,54,100,137,186,50,11,164,109,
  10481. 77,215,5,61,35,106,206,149,110,4,254,219,237,58,8,196,24,103,74,183,2,127,
  10482. 103,246,93,4,98,12,47,180,67,103,246,127,101,208,70,32,194,186,134,207,236,
  10483. 254,203,160,140,65,133,246,136,108,254,199,237,186,8,196,24,87,80,217,253,
  10484. 143,219,116,17,136,49,30,209,13,159,220,116,75,3,30,65,244,17,136,48,174,
  10485. 209,13,159,220,116,17,136,48,158,161,179,251,142,130,49,6,17,209,130,96,
  10486. 237,80,75,47,160,140,65,142,134,133,41,34,110,134,133,41,34,3,25,110,8,22,
  10487. 158,130,38,163,8,217,200,158,76,156,210,117,128,153,203,210,70,46,137,187,
  10488. 18,27,164,187,201,209,130,100,55,91,70,4,145,63,66,231,44,128,105,187,41,
  10489. 197,13,49,122,8,196,24,71,75,70,138,104,115,77,215,5,36,20,201,214,209,107,
  10490. 79,104,209,144,168,105,6,207,251,209,104,209,125,212,227,66,127,235,191,
  10491. 239,232,180,90,52,95,69,247,83,141,9,255,174,255,191,162,211,80,210,253,23,
  10492. 221,78,52,39,254,183,254,254,139,72,105,126,139,238,167,26,19,255,91,255,
  10493. 127,69,166,129,191,69,247,83,141,9,255,175,255,191,162,213,26,50,23,232,
  10494. 190,234,113,161,63,245,115,119,86,227,118,83,138,26,98,9,110,48,86,22,148,
  10495. 160,152,22,82,70,46,137,44,8,180,163,32,104,98,206,32,17,7,16,88,101,100,
  10496. 206,42,70,36,108,205,18,74,140,33,196,230,60,2,152,146,33,38,230,8,36,79,
  10497. 182,251,65,156,151,24,200,33,145,162,25,80,209,24,67,0,166,68,52,174,61,73,
  10498. 25,33,205,25,27,84,177,195,234,220,1,144,105,99,135,217,16,17,17,208,72,
  10499. 199,179,60,93,100,146,49,232,162,64,76,135,19,152,244,44,136,223,98,67,4,
  10500. 18,33,247,217,158,36,0,209,190,156,13,26,201,21,111,165,67,64,180,100,145,
  10501. 62,250,32,45,100,33,55,214,1,229,223,65,19,72,187,236,206,137,35,125,120,
  10502. 190,201,104,105,15,190,201,212,136,136,125,246,160,137,27,83,239,171,37,
  10503. 200,218,159,125,168,34,192,61,27,233,93,22,1,114,78,250,28,76,130,112,200,
  10504. 93,245,164,188,207,190,204,17,49,38,109,246,160,93,8,119,185,13,153,34,173,
  10505. 246,113,0,136,48,76,10,90,26,78,182,140,9,34,130,161,100,235,64,194,9,226,
  10506. 44,166,1,41,221,153,226,235,118,120,121,58,72,197,209,63,71,69,76,15,34,
  10507. 164,73,244,171,112,39,246,223,104,169,18,125,42,220,9,253,159,217,38,68,
  10508. 159,104,134,207,236,254,201,18,36,250,134,207,236,254,201,50,36,251,68,54,
  10509. 127,99,246,200,145,39,212,54,127,99,246,200,145,39,218,33,179,251,131,200,
  10510. 147,234,27,63,184,81,137,62,149,110,4,254,219,237,20,98,79,165,91,129,63,
  10511. 179,251,36,152,147,237,16,217,253,159,217,32,196,159,80,217,253,159,217,36,
  10512. 196,159,104,134,207,236,126,217,6,36,250,134,207,236,126,217,6,36,251,68,
  10513. 54,127,112,115,18,125,67,103,247,8,149,2,8,196,24,143,131,137,146,90,121,
  10514. 35,162,44,140,35,102,160,226,100,235,138,89,18,102,13,10,82,68,200,151,106,
  10515. 130,88,131,4,192,73,225,228,85,162,137,147,168,108,252,18,42,209,68,201,
  10516. 212,54,126,89,23,104,162,100,245,17,230,207,193,34,237,20,76,158,162,60,
  10517. 217,249,100,109,162,137,147,163,117,2,178,120,36,109,162,137,147,163,117,2,
  10518. 178,121,100,101,162,137,147,165,91,129,63,4,140,180,81,50,116,171,112,39,
  10519. 229,145,150,138,38,78,161,179,251,63,178,240,72,203,69,19,39,80,217,253,
  10520. 159,217,121,100,109,162,137,147,212,71,155,63,179,251,47,4,141,180,81,50,
  10521. 122,136,243,103,246,127,101,229,145,150,138,38,78,161,179,251,31,182,240,
  10522. 72,203,69,19,39,80,217,253,143,219,121,100,109,162,137,147,212,71,155,63,
  10523. 177,251,111,4,141,180,81,50,122,136,243,103,246,63,109,229,145,54,138,38,
  10524. 78,161,179,251,133,90,40,153,61,68,121,179,251,132,196,128,31,80,217,248,
  10525. 36,76,72,1,245,13,159,150,69,68,128,31,168,143,54,126,9,21,18,0,126,162,60,
  10526. 217,249,100,100,72,1,244,110,160,86,79,4,140,137,0,62,141,212,10,201,229,
  10527. 145,113,32,7,210,173,192,159,130,69,196,128,31,74,183,2,126,89,23,18,0,125,
  10528. 67,103,246,127,101,224,145,113,32,7,212,54,127,103,246,94,89,25,18,0,126,
  10529. 162,60,217,253,159,217,120,36,100,72,1,250,136,243,103,246,127,101,229,145,
  10530. 113,32,7,212,54,127,99,246,222,9,23,18,0,125,67,103,246,63,109,229,145,145,
  10531. 32,7,234,35,205,159,216,253,183,130,70,68,128,31,168,143,54,127,99,246,222,
  10532. 89,17,18,0,125,67,103,247,9,137,0,63,81,30,108,254,224,130,115,240,98,66,
  10533. 128,92,136,84,45,101,180,81,50,28,78,99,193,18,40,56,153,58,178,52,211,58,
  10534. 17,46,134,133,41,34,164,75,164,104,156,52,52,199,37,222,232,206,66,64,207,
  10535. 18,66,136,137,19,173,62,46,155,181,167,72,147,235,226,233,186,120,121,58,
  10536. 226,157,214,111,84,76,73,36,109,24,72,130,100,112,200,178,76,157,124,92,
  10537. 242,70,120,25,193,34,245,241,117,240,97,1,107,33,25,212,54,160,90,7,244,29,
  10538. 24,38,66,254,223,215,125,119,215,126,232,190,43,226,67,244,1,250,193,125,
  10539. 111,216,11,234,254,192,63,96,159,173,234,26,84,53,19,194,126,175,168,105,
  10540. 80,212,79,8,234,26,84,53,19,193,156,20,144,83,52,167,20,52,198,109,24,18,
  10541. 68,225,115,150,64,53,52,104,200,84,52,131,76,167,20,52,200,46,7,48,52,146,
  10542. 132,102,57,33,165,139,168,209,154,32,104,220,193,189,214,27,16,209,176,23,
  10543. 26,220,98,149,110,116,70,75,188,98,116,136,34,33,101,4,192,223,178,32,38,6,
  10544. 144,18,67,72,1,58,67,0,100,95,74,17,159,217,31,210,132,103,246,58,251,33,
  10545. 121,232,55,150,227,125,143,216,16,190,91,141,246,68,31,150,223,178,39,150,
  10546. 223,177,251,0,244,135,97,37,32,24,132,104,24,66,161,175,164,202,134,140,
  10547. 151,39,212,125,255,221,125,74,86,9,79,168,104,201,116,178,139,154,22,134,
  10548. 145,72,51,93,18,116,64,145,13,39,82,34,33,38,73,76,132,185,4,185,187,198,
  10549. 100,229,233,197,13,49,228,73,247,4,4,78,98,79,184,32,34,105,187,201,147,
  10550. 154,185,187,200,147,165,233,197,13,50,230,239,82,98,151,167,20,52,206,145,
  10551. 39,234,76,69,245,22,190,224,128,138,228,73,244,180,90,251,130,2,43,145,39,
  10552. 234,76,76,243,155,51,162,68,159,88,230,204,234,145,39,234,76,67,240,38,67,
  10553. 200,147,232,193,50,46,68,159,169,49,31,206,164,100,137,18,125,59,169,25,54,
  10554. 68,159,169,49,51,200,109,38,73,42,68,159,88,134,210,100,147,100,73,250,147,
  10555. 20,188,65,57,163,146,164,73,246,68,19,154,57,74,68,159,169,49,51,200,90,
  10556. 209,34,9,205,28,159,34,79,172,66,214,137,16,78,104,228,121,18,125,154,24,
  10557. 72,152,147,236,208,194,101,205,39,92,82,200,147,145,137,63,82,98,103,144,
  10558. 181,162,68,19,154,57,60,196,159,88,133,173,18,32,156,209,201,166,36,253,73,
  10559. 138,94,32,156,209,201,70,36,251,34,9,205,28,154,98,79,212,152,153,228,54,
  10560. 147,36,148,98,79,172,67,105,50,73,102,36,253,73,136,254,117,35,36,24,147,
  10561. 233,221,72,201,38,36,253,73,136,126,6,12,98,79,163,6,20,98,79,212,152,135,
  10562. 224,76,135,49,39,209,130,100,89,137,63,82,98,103,156,217,157,6,36,250,199,
  10563. 54,103,113,137,63,82,98,47,168,181,247,4,4,86,98,79,165,162,215,220,16,17,
  10564. 57,137,62,205,12,36,166,238,173,194,2,201,217,161,132,236,167,20,52,210,
  10565. 155,186,183,8,11,39,70,9,147,178,156,80,211,50,110,236,208,194,118,83,138,
  10566. 26,102,77,221,24,38,78,202,113,67,76,54,186,195,245,38,34,188,17,145,23,55,
  10567. 117,241,32,145,36,57,173,155,186,75,189,205,35,102,128,44,243,119,74,139,
  10568. 144,113,243,221,36,77,21,38,144,210,161,168,158,35,230,144,192,154,42,77,
  10569. 33,165,67,81,60,15,173,7,90,159,49,13,213,64,186,17,62,96,47,170,129,116,
  10570. 33,165,64,202,113,36,226,134,70,110,234,220,32,44,157,163,222,72,244,64,
  10571. 145,23,55,118,143,121,35,209,2,68,140,221,213,184,64,89,58,183,88,145,232,
  10572. 129,34,46,110,234,221,98,71,162,4,136,153,80,50,156,80,211,22,79,90,38,105,
  10573. 16,17,17,207,18,61,96,17,10,192,76,71,106,220,32,44,157,19,152,240,68,138,
  10574. 17,193,30,137,195,39,65,51,8,224,143,65,54,22,46,103,68,112,71,162,112,200,
  10575. 184,144,116,17,59,20,24,243,52,72,58,8,134,42,23,50,68,108,3,206,87,71,164,
  10576. 0,142,73,57,132,41,42,72,225,107,4,167,212,52,100,191,92,83,161,163,37,250,
  10577. 226,158,141,145,208,89,154,79,90,4,66,73,209,153,100,180,8,133,145,208,89,
  10578. 158,36,169,35,34,17,244,145,198,247,60,137,114,26,97,57,162,4,206,137,116,
  10579. 17,136,48,144,68,212,97,27,57,24,64,90,201,18,5,13,25,4,5,172,160,123,215,
  10580. 138,62,46,121,35,60,117,18,233,27,70,18,32,10,200,212,75,175,139,166,233,
  10581. 225,228,235,138,227,130,93,117,155,215,197,207,36,103,131,212,11,161,58,
  10582. 226,186,110,234,220,32,44,157,148,226,134,153,19,119,101,56,161,166,88,156,
  10583. 217,78,52,20,221,17,200,147,25,137,53,17,180,97,34,0,172,140,19,154,84,26,
  10584. 145,0,86,68,90,40,152,2,178,22,160,93,8,69,19,18,98,37,210,94,100,108,144,
  10585. 21,145,8,151,75,23,100,141,66,37,217,16,11,32,226,248,146,164,108,250,75,
  10586. 204,141,146,28,217,24,177,33,50,66,72,128,92,6,66,161,164,235,226,231,146,
  10587. 51,65,36,225,144,168,105,58,248,185,228,140,240,97,68,128,153,38,98,79,174,
  10588. 179,122,248,185,228,140,241,214,129,132,150,12,73,245,214,111,95,23,60,145,
  10589. 158,58,50,72,81,67,230,232,184,196,159,95,23,77,211,195,201,215,21,47,139,
  10590. 166,233,225,228,50,200,211,76,229,2,201,25,149,241,67,102,138,52,146,16,30,
  10591. 67,18,66,3,201,34,52,78,25,61,72,160,94,115,30,230,145,179,73,26,39,12,158,
  10592. 164,81,33,144,78,25,61,72,160,94,115,30,230,145,179,72,200,39,12,158,164,
  10593. 80,132,75,165,67,81,50,21,18,235,65,214,169,224,140,137,210,173,192,154,30,
  10594. 8,200,157,67,102,66,84,11,71,169,20,19,209,139,162,158,207,15,39,73,24,186,
  10595. 43,236,176,217,130,253,36,98,232,187,177,33,73,18,52,68,233,35,23,69,93,
  10596. 136,26,98,116,145,139,162,158,146,160,95,73,24,186,37,12,72,5,16,64,145,10,
  10597. 32,76,71,64,156,217,161,180,34,6,64,208,198,36,78,50,20,20,92,204,50,44,
  10598. 147,32,134,226,17,114,33,202,134,129,107,192,202,232,160,180,104,166,135,
  10599. 52,72,40,144,213,33,178,152,26,34,56,163,105,44,104,146,116,139,77,43,34,
  10600. 98,57,38,116,72,179,60,93,97,206,56,52,240,242,56,163,168,34,74,185,3,45,
  10601. 142,133,144,150,68,206,81,44,18,145,68,230,202,100,35,104,195,18,239,116,
  10602. 102,114,94,100,104,228,100,49,238,140,203,42,60,145,35,104,181,146,113,161,
  10603. 10,80,46,68,82,24,245,145,132,108,228,148,54,100,137,64,34,13,100,153,222,
  10604. 1,40,6,33,223,20,84,19,34,95,23,76,130,153,6,103,208,43,64,141,41,130,104,
  10605. 17,112,130,44,96,
  10606. };
  10607. /* to convert a heap stridx to a token number, subtract
  10608. * DUK_STRIDX_START_RESERVED and add DUK_TOK_START_RESERVED.
  10609. */
  10610. /* native functions: 147 */
  10611. DUK_INTERNAL const duk_c_function duk_bi_native_functions[147] = {
  10612. duk_bi_array_constructor,
  10613. duk_bi_array_constructor_is_array,
  10614. duk_bi_array_prototype_concat,
  10615. duk_bi_array_prototype_indexof_shared,
  10616. duk_bi_array_prototype_iter_shared,
  10617. duk_bi_array_prototype_join_shared,
  10618. duk_bi_array_prototype_pop,
  10619. duk_bi_array_prototype_push,
  10620. duk_bi_array_prototype_reduce_shared,
  10621. duk_bi_array_prototype_reverse,
  10622. duk_bi_array_prototype_shift,
  10623. duk_bi_array_prototype_slice,
  10624. duk_bi_array_prototype_sort,
  10625. duk_bi_array_prototype_splice,
  10626. duk_bi_array_prototype_to_string,
  10627. duk_bi_array_prototype_unshift,
  10628. duk_bi_arraybuffer_constructor,
  10629. duk_bi_arraybuffer_isview,
  10630. duk_bi_boolean_constructor,
  10631. duk_bi_boolean_prototype_tostring_shared,
  10632. duk_bi_buffer_compare_shared,
  10633. duk_bi_buffer_constructor,
  10634. duk_bi_buffer_prototype_tostring_shared,
  10635. duk_bi_buffer_readfield,
  10636. duk_bi_buffer_slice_shared,
  10637. duk_bi_buffer_writefield,
  10638. duk_bi_dataview_constructor,
  10639. duk_bi_date_constructor,
  10640. duk_bi_date_constructor_now,
  10641. duk_bi_date_constructor_parse,
  10642. duk_bi_date_constructor_utc,
  10643. duk_bi_date_prototype_get_shared,
  10644. duk_bi_date_prototype_get_timezone_offset,
  10645. duk_bi_date_prototype_set_shared,
  10646. duk_bi_date_prototype_set_time,
  10647. duk_bi_date_prototype_to_json,
  10648. duk_bi_date_prototype_tostring_shared,
  10649. duk_bi_date_prototype_value_of,
  10650. duk_bi_duktape_object_act,
  10651. duk_bi_duktape_object_compact,
  10652. duk_bi_duktape_object_dec,
  10653. duk_bi_duktape_object_enc,
  10654. duk_bi_duktape_object_fin,
  10655. duk_bi_duktape_object_gc,
  10656. duk_bi_duktape_object_info,
  10657. duk_bi_error_constructor_shared,
  10658. duk_bi_error_prototype_filename_getter,
  10659. duk_bi_error_prototype_linenumber_getter,
  10660. duk_bi_error_prototype_nop_setter,
  10661. duk_bi_error_prototype_stack_getter,
  10662. duk_bi_error_prototype_to_string,
  10663. duk_bi_function_constructor,
  10664. duk_bi_function_prototype,
  10665. duk_bi_function_prototype_apply,
  10666. duk_bi_function_prototype_bind,
  10667. duk_bi_function_prototype_call,
  10668. duk_bi_function_prototype_to_string,
  10669. duk_bi_global_object_decode_uri,
  10670. duk_bi_global_object_decode_uri_component,
  10671. duk_bi_global_object_encode_uri,
  10672. duk_bi_global_object_encode_uri_component,
  10673. duk_bi_global_object_escape,
  10674. duk_bi_global_object_eval,
  10675. duk_bi_global_object_is_finite,
  10676. duk_bi_global_object_is_nan,
  10677. duk_bi_global_object_parse_float,
  10678. duk_bi_global_object_parse_int,
  10679. duk_bi_global_object_print_helper,
  10680. duk_bi_global_object_require,
  10681. duk_bi_global_object_unescape,
  10682. duk_bi_json_object_parse,
  10683. duk_bi_json_object_stringify,
  10684. duk_bi_logger_constructor,
  10685. duk_bi_logger_prototype_fmt,
  10686. duk_bi_logger_prototype_log_shared,
  10687. duk_bi_logger_prototype_raw,
  10688. duk_bi_math_object_max,
  10689. duk_bi_math_object_min,
  10690. duk_bi_math_object_onearg_shared,
  10691. duk_bi_math_object_random,
  10692. duk_bi_math_object_twoarg_shared,
  10693. duk_bi_nodejs_buffer_byte_length,
  10694. duk_bi_nodejs_buffer_concat,
  10695. duk_bi_nodejs_buffer_constructor,
  10696. duk_bi_nodejs_buffer_copy,
  10697. duk_bi_nodejs_buffer_fill,
  10698. duk_bi_nodejs_buffer_is_buffer,
  10699. duk_bi_nodejs_buffer_is_encoding,
  10700. duk_bi_nodejs_buffer_tojson,
  10701. duk_bi_nodejs_buffer_tostring,
  10702. duk_bi_nodejs_buffer_write,
  10703. duk_bi_number_constructor,
  10704. duk_bi_number_prototype_to_exponential,
  10705. duk_bi_number_prototype_to_fixed,
  10706. duk_bi_number_prototype_to_locale_string,
  10707. duk_bi_number_prototype_to_precision,
  10708. duk_bi_number_prototype_to_string,
  10709. duk_bi_number_prototype_value_of,
  10710. duk_bi_object_constructor,
  10711. duk_bi_object_constructor_create,
  10712. duk_bi_object_constructor_define_properties,
  10713. duk_bi_object_constructor_define_property,
  10714. duk_bi_object_constructor_get_own_property_descriptor,
  10715. duk_bi_object_constructor_is_extensible,
  10716. duk_bi_object_constructor_is_sealed_frozen_shared,
  10717. duk_bi_object_constructor_keys_shared,
  10718. duk_bi_object_constructor_prevent_extensions,
  10719. duk_bi_object_constructor_seal_freeze_shared,
  10720. duk_bi_object_getprototype_shared,
  10721. duk_bi_object_prototype_has_own_property,
  10722. duk_bi_object_prototype_is_prototype_of,
  10723. duk_bi_object_prototype_property_is_enumerable,
  10724. duk_bi_object_prototype_to_locale_string,
  10725. duk_bi_object_prototype_to_string,
  10726. duk_bi_object_prototype_value_of,
  10727. duk_bi_object_setprototype_shared,
  10728. duk_bi_pointer_constructor,
  10729. duk_bi_pointer_prototype_tostring_shared,
  10730. duk_bi_proxy_constructor,
  10731. duk_bi_regexp_constructor,
  10732. duk_bi_regexp_prototype_exec,
  10733. duk_bi_regexp_prototype_test,
  10734. duk_bi_regexp_prototype_to_string,
  10735. duk_bi_string_constructor,
  10736. duk_bi_string_constructor_from_char_code,
  10737. duk_bi_string_prototype_caseconv_shared,
  10738. duk_bi_string_prototype_char_at,
  10739. duk_bi_string_prototype_char_code_at,
  10740. duk_bi_string_prototype_concat,
  10741. duk_bi_string_prototype_indexof_shared,
  10742. duk_bi_string_prototype_locale_compare,
  10743. duk_bi_string_prototype_match,
  10744. duk_bi_string_prototype_replace,
  10745. duk_bi_string_prototype_search,
  10746. duk_bi_string_prototype_slice,
  10747. duk_bi_string_prototype_split,
  10748. duk_bi_string_prototype_substr,
  10749. duk_bi_string_prototype_substring,
  10750. duk_bi_string_prototype_to_string,
  10751. duk_bi_string_prototype_trim,
  10752. duk_bi_thread_constructor,
  10753. duk_bi_thread_current,
  10754. duk_bi_thread_resume,
  10755. duk_bi_thread_yield,
  10756. duk_bi_type_error_thrower,
  10757. duk_bi_typedarray_constructor,
  10758. duk_bi_typedarray_set,
  10759. };
  10760. DUK_INTERNAL const duk_uint8_t duk_builtins_data[1952] = {
  10761. 105,195,75,16,121,40,105,51,14,252,104,52,8,131,72,0,115,225,65,165,236,55,
  10762. 243,6,145,32,210,24,210,182,25,249,35,120,216,99,226,13,78,225,116,177,164,
  10763. 180,44,192,4,202,52,150,220,24,0,169,70,146,219,123,0,23,40,210,91,110,96,
  10764. 3,37,26,75,109,172,0,108,163,73,109,177,128,14,148,105,45,181,176,1,242,
  10765. 144,56,209,32,94,6,167,101,98,80,211,24,1,250,67,72,168,67,232,13,46,128,
  10766. 47,162,52,164,0,62,80,163,72,128,61,40,107,26,7,37,20,53,200,131,88,0,66,
  10767. 134,185,16,98,80,215,34,11,96,0,138,26,228,65,76,0,69,67,92,136,37,128,6,
  10768. 168,107,145,4,48,1,165,13,114,32,118,0,44,161,174,68,12,192,7,148,53,200,
  10769. 129,88,1,26,134,165,48,130,80,31,255,241,69,224,63,252,0,0,0,0,0,0,46,32,
  10770. 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,
  10771. 6,12,112,92,200,5,140,149,192,202,91,204,181,184,204,91,76,213,176,206,90,
  10772. 204,240,84,208,5,13,9,124,210,43,13,24,64,226,131,205,112,56,216,3,77,152,
  10773. 48,218,130,205,184,40,220,130,77,216,32,222,129,205,248,24,224,129,78,25,
  10774. 214,163,226,90,80,145,104,65,37,157,0,150,99,242,89,78,73,100,58,37,140,
  10775. 236,150,35,194,88,79,73,96,69,37,125,12,122,188,134,62,0,2,165,68,39,255,
  10776. 255,193,43,67,0,0,80,127,192,58,182,216,80,0,21,59,154,64,0,107,76,200,172,
  10777. 180,146,176,198,138,187,43,42,204,136,170,181,146,168,214,80,0,26,155,81,
  10778. 42,77,4,168,180,20,0,6,160,206,74,123,73,64,0,127,255,4,10,153,219,28,198,
  10779. 163,184,130,140,224,10,43,144,40,141,164,161,183,18,132,222,64,161,127,128,
  10780. 0,63,225,1,109,74,8,137,71,56,5,4,213,20,3,115,233,249,177,240,80,255,192,
  10781. 6,120,2,64,127,195,0,173,28,56,20,96,80,128,0,206,192,143,167,64,164,156,
  10782. 131,2,112,14,125,55,9,4,216,40,19,80,180,77,3,9,51,13,94,153,7,159,76,64,
  10783. 207,192,0,102,0,103,255,255,242,240,67,73,112,33,168,0,12,180,16,212,0,10,
  10784. 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,
  10785. 135,195,224,127,196,2,87,132,17,82,143,20,10,44,80,36,239,196,147,63,146,
  10786. 119,0,125,49,129,52,152,64,154,128,0,201,96,137,36,131,36,142,17,18,40,82,
  10787. 77,97,145,33,135,68,130,37,17,247,208,71,159,65,29,125,8,0,12,113,244,32,0,
  10788. 49,184,176,70,162,16,20,95,240,0,7,252,80,37,120,193,81,196,194,0,3,69,19,
  10789. 0,81,191,197,140,192,127,239,255,255,255,255,255,255,140,64,0,0,0,0,0,0,0,
  10790. 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,
  10791. 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,
  10792. 16,69,23,73,19,92,36,73,124,129,71,255,0,56,136,233,34,3,223,208,241,192,3,
  10793. 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,
  10794. 42,49,32,64,0,225,132,129,0,4,133,146,4,0,21,210,72,16,0,103,65,32,64,1,
  10795. 220,228,100,162,146,130,20,74,8,72,248,64,2,33,3,225,0,9,131,143,132,0,42,
  10796. 12,62,16,0,184,40,248,64,3,32,131,225,0,13,129,143,132,0,58,4,62,16,0,248,
  10797. 8,248,64,4,32,3,225,0,17,127,143,132,0,73,252,62,16,1,55,232,248,64,5,31,
  10798. 131,225,0,21,125,143,132,0,89,244,62,16,1,119,201,0,31,4,68,123,144,148,0,
  10799. 97,236,66,80,1,151,169,10,248,0,211,208,133,124,0,109,230,66,254,0,56,242,
  10800. 33,127,0,29,120,144,207,128,15,60,8,103,192,7,221,228,37,0,32,119,16,148,0,
  10801. 133,218,66,190,0,68,236,33,95,0,35,117,144,191,128,18,58,136,95,192,9,92,
  10802. 195,225,0,38,114,144,148,0,156,41,31,224,0,15,249,1,138,144,64,192,2,2,225,
  10803. 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,
  10804. 239,64,10,79,248,0,3,254,72,86,209,5,155,36,17,46,185,137,129,109,203,140,
  10805. 11,78,94,96,13,28,200,1,74,255,0,2,127,202,4,218,43,131,100,130,32,5,47,
  10806. 252,0,9,255,44,19,104,173,237,146,8,128,20,207,240,0,39,252,192,77,162,183,
  10807. 54,72,34,0,83,127,192,0,159,243,65,54,138,218,217,32,136,1,78,255,0,2,127,
  10808. 206,4,218,43,99,100,130,32,5,63,252,0,9,255,60,19,104,173,109,146,8,128,15,
  10809. 255,242,27,16,16,1,111,194,162,197,21,218,90,240,16,0,154,236,110,237,85,
  10810. 69,154,208,15,249,139,144,191,190,142,123,218,176,15,253,197,81,217,74,224,
  10811. 191,154,144,15,246,242,222,197,73,185,67,154,112,16,2,72,126,213,17,11,70,
  10812. 26,80,15,249,168,39,153,159,206,243,90,48,15,253,168,39,153,159,206,243,82,
  10813. 104,39,17,158,156,80,0,22,114,113,64,0,153,169,197,0,3,102,40,33,150,156,
  10814. 80,0,70,82,113,64,1,89,41,197,0,6,100,39,20,0,29,142,156,80,0,134,50,98,
  10815. 243,21,53,121,136,160,144,0,22,26,120,24,73,197,0,9,96,167,20,0,41,128,156,
  10816. 80,0,181,250,113,64,3,1,255,254,0,81,20,100,47,145,216,23,255,240,0,11,255,
  10817. 248,0,3,255,252,81,252,4,12,65,216,0,0,0,0,0,129,167,1,26,144,9,165,0,26,
  10818. 177,199,197,132,30,147,16,120,86,65,217,80,240,232,164,120,114,80,60,52,39,
  10819. 32,84,223,192,15,59,30,129,156,115,6,81,160,7,253,40,0,5,81,252,0,1,255,78,
  10820. 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,
  10821. 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,
  10822. 20,146,12,18,200,47,74,30,23,37,15,128,0,143,146,135,192,0,133,169,67,224,
  10823. 0,98,196,161,240,0,65,90,80,248,0,41,63,255,194,109,65,11,137,191,174,45,
  10824. 153,98,242,229,191,147,102,8,190,94,92,183,242,65,167,114,12,188,185,111,
  10825. 228,131,70,29,217,54,105,221,156,0,171,255,128,9,208,68,128,255,174,0,25,
  10826. 168,194,64,0,130,177,254,0,0,255,176,1,3,120,186,64,12,13,194,233,0,32,54,
  10827. 139,164,0,196,216,46,144,2,19,88,186,64,12,141,66,233,0,34,52,139,164,0,
  10828. 140,208,46,144,2,67,56,203,64,12,12,195,45,0,32,50,140,180,0,196,200,50,
  10829. 208,2,19,24,203,64,12,140,67,45,0,34,48,140,180,0,140,192,50,208,2,64,127,
  10830. 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,
  10831. 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,
  10832. 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,
  10833. 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,
  10834. 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,
  10835. 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,
  10836. 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,
  10837. 17,64,162,132,248,162,64,0,193,255,138,5,137,161,116,38,69,210,0,32,152,23,
  10838. 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,
  10839. 1,73,33,116,128,9,36,69,210,0,52,144,23,72,0,26,60,93,32,1,104,225,116,128,
  10840. 2,35,69,210,0,24,140,23,104,0,42,44,93,160,1,168,161,118,128,10,162,69,218,
  10841. 0,58,136,25,98,28,101,160,2,8,97,150,128,0,161,70,90,0,18,132,25,104,0,138,
  10842. 12,101,160,3,40,33,150,128,1,32,70,90,0,20,128,25,104,0,145,252,101,160,3,
  10843. 71,225,150,128,1,159,70,90,0,22,124,25,104,0,33,236,101,160,1,135,161,152,
  10844. 128,2,158,70,98,0,26,120,25,136,0,169,220,102,32,3,180,117,150,57,214,0,
  10845. 157,85,98,112,80,137,241,66,128,0,166,213,33,53,24,66,121,106,0,
  10846. };
  10847. #ifdef DUK_USE_BUILTIN_INITJS
  10848. DUK_INTERNAL const duk_uint8_t duk_initjs_data[187] = {
  10849. 40,102,117,110,99,116,105,111,110,40,100,44,97,41,123,102,117,110,99,116,
  10850. 105,111,110,32,98,40,97,44,98,44,99,41,123,79,98,106,101,99,116,46,100,101,
  10851. 102,105,110,101,80,114,111,112,101,114,116,121,40,97,44,98,44,123,118,97,
  10852. 108,117,101,58,99,44,119,114,105,116,97,98,108,101,58,33,48,44,101,110,117,
  10853. 109,101,114,97,98,108,101,58,33,49,44,99,111,110,102,105,103,117,114,97,98,
  10854. 108,101,58,33,48,125,41,125,98,40,97,46,76,111,103,103,101,114,44,34,99,
  10855. 108,111,103,34,44,110,101,119,32,97,46,76,111,103,103,101,114,40,34,67,34,
  10856. 41,41,59,98,40,97,44,34,109,111,100,76,111,97,100,101,100,34,44,123,125,41,
  10857. 125,41,40,116,104,105,115,44,68,117,107,116,97,112,101,41,59,10,0,
  10858. };
  10859. #endif /* DUK_USE_BUILTIN_INITJS */
  10860. #elif defined(DUK_USE_DOUBLE_ME)
  10861. DUK_INTERNAL const duk_uint8_t duk_strings_data[2624] = {
  10862. 55,86,227,24,145,55,102,120,144,3,63,94,228,54,100,137,186,50,11,164,109,
  10863. 77,215,5,61,35,106,206,149,110,4,254,219,237,58,8,196,24,103,74,183,2,127,
  10864. 103,246,93,4,98,12,47,180,67,103,246,127,101,208,70,32,194,186,134,207,236,
  10865. 254,203,160,140,65,133,246,136,108,254,199,237,186,8,196,24,87,80,217,253,
  10866. 143,219,116,17,136,49,30,209,13,159,220,116,75,3,30,65,244,17,136,48,174,
  10867. 209,13,159,220,116,17,136,48,158,161,179,251,142,130,49,6,17,209,130,96,
  10868. 237,80,75,47,160,140,65,142,134,133,41,34,110,134,133,41,34,3,25,110,8,22,
  10869. 158,130,38,163,8,217,200,158,76,156,210,117,128,153,203,210,70,46,137,187,
  10870. 18,27,164,187,201,209,130,100,55,91,70,4,145,63,66,231,44,128,105,187,41,
  10871. 197,13,49,122,8,196,24,71,75,70,138,104,115,77,215,5,36,20,201,214,209,107,
  10872. 79,104,209,144,168,105,6,207,251,209,104,209,125,212,227,66,127,235,191,
  10873. 239,232,180,90,52,95,69,247,83,141,9,255,174,255,191,162,211,80,210,253,23,
  10874. 221,78,52,39,254,183,254,254,139,72,105,126,139,238,167,26,19,255,91,255,
  10875. 127,69,166,129,191,69,247,83,141,9,255,175,255,191,162,213,26,50,23,232,
  10876. 190,234,113,161,63,245,115,119,86,227,118,83,138,26,98,9,110,48,86,22,148,
  10877. 160,152,22,82,70,46,137,44,8,180,163,32,104,98,206,32,17,7,16,88,101,100,
  10878. 206,42,70,36,108,205,18,74,140,33,196,230,60,2,152,146,33,38,230,8,36,79,
  10879. 182,251,65,156,151,24,200,33,145,162,25,80,209,24,67,0,166,68,52,174,61,73,
  10880. 25,33,205,25,27,84,177,195,234,220,1,144,105,99,135,217,16,17,17,208,72,
  10881. 199,179,60,93,100,146,49,232,162,64,76,135,19,152,244,44,136,223,98,67,4,
  10882. 18,33,247,217,158,36,0,209,190,156,13,26,201,21,111,165,67,64,180,100,145,
  10883. 62,250,32,45,100,33,55,214,1,229,223,65,19,72,187,236,206,137,35,125,120,
  10884. 190,201,104,105,15,190,201,212,136,136,125,246,160,137,27,83,239,171,37,
  10885. 200,218,159,125,168,34,192,61,27,233,93,22,1,114,78,250,28,76,130,112,200,
  10886. 93,245,164,188,207,190,204,17,49,38,109,246,160,93,8,119,185,13,153,34,173,
  10887. 246,113,0,136,48,76,10,90,26,78,182,140,9,34,130,161,100,235,64,194,9,226,
  10888. 44,166,1,41,221,153,226,235,118,120,121,58,72,197,209,63,71,69,76,15,34,
  10889. 164,73,244,171,112,39,246,223,104,169,18,125,42,220,9,253,159,217,38,68,
  10890. 159,104,134,207,236,254,201,18,36,250,134,207,236,254,201,50,36,251,68,54,
  10891. 127,99,246,200,145,39,212,54,127,99,246,200,145,39,218,33,179,251,131,200,
  10892. 147,234,27,63,184,81,137,62,149,110,4,254,219,237,20,98,79,165,91,129,63,
  10893. 179,251,36,152,147,237,16,217,253,159,217,32,196,159,80,217,253,159,217,36,
  10894. 196,159,104,134,207,236,126,217,6,36,250,134,207,236,126,217,6,36,251,68,
  10895. 54,127,112,115,18,125,67,103,247,8,149,2,8,196,24,143,131,137,146,90,121,
  10896. 35,162,44,140,35,102,160,226,100,235,138,89,18,102,13,10,82,68,200,151,106,
  10897. 130,88,131,4,192,73,225,228,85,162,137,147,168,108,252,18,42,209,68,201,
  10898. 212,54,126,89,23,104,162,100,245,17,230,207,193,34,237,20,76,158,162,60,
  10899. 217,249,100,109,162,137,147,163,117,2,178,120,36,109,162,137,147,163,117,2,
  10900. 178,121,100,101,162,137,147,165,91,129,63,4,140,180,81,50,116,171,112,39,
  10901. 229,145,150,138,38,78,161,179,251,63,178,240,72,203,69,19,39,80,217,253,
  10902. 159,217,121,100,109,162,137,147,212,71,155,63,179,251,47,4,141,180,81,50,
  10903. 122,136,243,103,246,127,101,229,145,150,138,38,78,161,179,251,31,182,240,
  10904. 72,203,69,19,39,80,217,253,143,219,121,100,109,162,137,147,212,71,155,63,
  10905. 177,251,111,4,141,180,81,50,122,136,243,103,246,63,109,229,145,54,138,38,
  10906. 78,161,179,251,133,90,40,153,61,68,121,179,251,132,196,128,31,80,217,248,
  10907. 36,76,72,1,245,13,159,150,69,68,128,31,168,143,54,126,9,21,18,0,126,162,60,
  10908. 217,249,100,100,72,1,244,110,160,86,79,4,140,137,0,62,141,212,10,201,229,
  10909. 145,113,32,7,210,173,192,159,130,69,196,128,31,74,183,2,126,89,23,18,0,125,
  10910. 67,103,246,127,101,224,145,113,32,7,212,54,127,103,246,94,89,25,18,0,126,
  10911. 162,60,217,253,159,217,120,36,100,72,1,250,136,243,103,246,127,101,229,145,
  10912. 113,32,7,212,54,127,99,246,222,9,23,18,0,125,67,103,246,63,109,229,145,145,
  10913. 32,7,234,35,205,159,216,253,183,130,70,68,128,31,168,143,54,127,99,246,222,
  10914. 89,17,18,0,125,67,103,247,9,137,0,63,81,30,108,254,224,130,115,240,98,66,
  10915. 128,92,136,84,45,101,180,81,50,28,78,99,193,18,40,56,153,58,178,52,211,58,
  10916. 17,46,134,133,41,34,164,75,164,104,156,52,52,199,37,222,232,206,66,64,207,
  10917. 18,66,136,137,19,173,62,46,155,181,167,72,147,235,226,233,186,120,121,58,
  10918. 226,157,214,111,84,76,73,36,109,24,72,130,100,112,200,178,76,157,124,92,
  10919. 242,70,120,25,193,34,245,241,117,240,97,1,107,33,25,212,54,160,90,7,244,29,
  10920. 24,38,66,254,223,215,125,119,215,126,232,190,43,226,67,244,1,250,193,125,
  10921. 111,216,11,234,254,192,63,96,159,173,234,26,84,53,19,194,126,175,168,105,
  10922. 80,212,79,8,234,26,84,53,19,193,156,20,144,83,52,167,20,52,198,109,24,18,
  10923. 68,225,115,150,64,53,52,104,200,84,52,131,76,167,20,52,200,46,7,48,52,146,
  10924. 132,102,57,33,165,139,168,209,154,32,104,220,193,189,214,27,16,209,176,23,
  10925. 26,220,98,149,110,116,70,75,188,98,116,136,34,33,101,4,192,223,178,32,38,6,
  10926. 144,18,67,72,1,58,67,0,100,95,74,17,159,217,31,210,132,103,246,58,251,33,
  10927. 121,232,55,150,227,125,143,216,16,190,91,141,246,68,31,150,223,178,39,150,
  10928. 223,177,251,0,244,135,97,37,32,24,132,104,24,66,161,175,164,202,134,140,
  10929. 151,39,212,125,255,221,125,74,86,9,79,168,104,201,116,178,139,154,22,134,
  10930. 145,72,51,93,18,116,64,145,13,39,82,34,33,38,73,76,132,185,4,185,187,198,
  10931. 100,229,233,197,13,49,228,73,247,4,4,78,98,79,184,32,34,105,187,201,147,
  10932. 154,185,187,200,147,165,233,197,13,50,230,239,82,98,151,167,20,52,206,145,
  10933. 39,234,76,69,245,22,190,224,128,138,228,73,244,180,90,251,130,2,43,145,39,
  10934. 234,76,76,243,155,51,162,68,159,88,230,204,234,145,39,234,76,67,240,38,67,
  10935. 200,147,232,193,50,46,68,159,169,49,31,206,164,100,137,18,125,59,169,25,54,
  10936. 68,159,169,49,51,200,109,38,73,42,68,159,88,134,210,100,147,100,73,250,147,
  10937. 20,188,65,57,163,146,164,73,246,68,19,154,57,74,68,159,169,49,51,200,90,
  10938. 209,34,9,205,28,159,34,79,172,66,214,137,16,78,104,228,121,18,125,154,24,
  10939. 72,152,147,236,208,194,101,205,39,92,82,200,147,145,137,63,82,98,103,144,
  10940. 181,162,68,19,154,57,60,196,159,88,133,173,18,32,156,209,201,166,36,253,73,
  10941. 138,94,32,156,209,201,70,36,251,34,9,205,28,154,98,79,212,152,153,228,54,
  10942. 147,36,148,98,79,172,67,105,50,73,102,36,253,73,136,254,117,35,36,24,147,
  10943. 233,221,72,201,38,36,253,73,136,126,6,12,98,79,163,6,20,98,79,212,152,135,
  10944. 224,76,135,49,39,209,130,100,89,137,63,82,98,103,156,217,157,6,36,250,199,
  10945. 54,103,113,137,63,82,98,47,168,181,247,4,4,86,98,79,165,162,215,220,16,17,
  10946. 57,137,62,205,12,36,166,238,173,194,2,201,217,161,132,236,167,20,52,210,
  10947. 155,186,183,8,11,39,70,9,147,178,156,80,211,50,110,236,208,194,118,83,138,
  10948. 26,102,77,221,24,38,78,202,113,67,76,54,186,195,245,38,34,188,17,145,23,55,
  10949. 117,241,32,145,36,57,173,155,186,75,189,205,35,102,128,44,243,119,74,139,
  10950. 144,113,243,221,36,77,21,38,144,210,161,168,158,35,230,144,192,154,42,77,
  10951. 33,165,67,81,60,15,173,7,90,159,49,13,213,64,186,17,62,96,47,170,129,116,
  10952. 33,165,64,202,113,36,226,134,70,110,234,220,32,44,157,163,222,72,244,64,
  10953. 145,23,55,118,143,121,35,209,2,68,140,221,213,184,64,89,58,183,88,145,232,
  10954. 129,34,46,110,234,221,98,71,162,4,136,153,80,50,156,80,211,22,79,90,38,105,
  10955. 16,17,17,207,18,61,96,17,10,192,76,71,106,220,32,44,157,19,152,240,68,138,
  10956. 17,193,30,137,195,39,65,51,8,224,143,65,54,22,46,103,68,112,71,162,112,200,
  10957. 184,144,116,17,59,20,24,243,52,72,58,8,134,42,23,50,68,108,3,206,87,71,164,
  10958. 0,142,73,57,132,41,42,72,225,107,4,167,212,52,100,191,92,83,161,163,37,250,
  10959. 226,158,141,145,208,89,154,79,90,4,66,73,209,153,100,180,8,133,145,208,89,
  10960. 158,36,169,35,34,17,244,145,198,247,60,137,114,26,97,57,162,4,206,137,116,
  10961. 17,136,48,144,68,212,97,27,57,24,64,90,201,18,5,13,25,4,5,172,160,123,215,
  10962. 138,62,46,121,35,60,117,18,233,27,70,18,32,10,200,212,75,175,139,166,233,
  10963. 225,228,235,138,227,130,93,117,155,215,197,207,36,103,131,212,11,161,58,
  10964. 226,186,110,234,220,32,44,157,148,226,134,153,19,119,101,56,161,166,88,156,
  10965. 217,78,52,20,221,17,200,147,25,137,53,17,180,97,34,0,172,140,19,154,84,26,
  10966. 145,0,86,68,90,40,152,2,178,22,160,93,8,69,19,18,98,37,210,94,100,108,144,
  10967. 21,145,8,151,75,23,100,141,66,37,217,16,11,32,226,248,146,164,108,250,75,
  10968. 204,141,146,28,217,24,177,33,50,66,72,128,92,6,66,161,164,235,226,231,146,
  10969. 51,65,36,225,144,168,105,58,248,185,228,140,240,97,68,128,153,38,98,79,174,
  10970. 179,122,248,185,228,140,241,214,129,132,150,12,73,245,214,111,95,23,60,145,
  10971. 158,58,50,72,81,67,230,232,184,196,159,95,23,77,211,195,201,215,21,47,139,
  10972. 166,233,225,228,50,200,211,76,229,2,201,25,149,241,67,102,138,52,146,16,30,
  10973. 67,18,66,3,201,34,52,78,25,61,72,160,94,115,30,230,145,179,73,26,39,12,158,
  10974. 164,81,33,144,78,25,61,72,160,94,115,30,230,145,179,72,200,39,12,158,164,
  10975. 80,132,75,165,67,81,50,21,18,235,65,214,169,224,140,137,210,173,192,154,30,
  10976. 8,200,157,67,102,66,84,11,71,169,20,19,209,139,162,158,207,15,39,73,24,186,
  10977. 43,236,176,217,130,253,36,98,232,187,177,33,73,18,52,68,233,35,23,69,93,
  10978. 136,26,98,116,145,139,162,158,146,160,95,73,24,186,37,12,72,5,16,64,145,10,
  10979. 32,76,71,64,156,217,161,180,34,6,64,208,198,36,78,50,20,20,92,204,50,44,
  10980. 147,32,134,226,17,114,33,202,134,129,107,192,202,232,160,180,104,166,135,
  10981. 52,72,40,144,213,33,178,152,26,34,56,163,105,44,104,146,116,139,77,43,34,
  10982. 98,57,38,116,72,179,60,93,97,206,56,52,240,242,56,163,168,34,74,185,3,45,
  10983. 142,133,144,150,68,206,81,44,18,145,68,230,202,100,35,104,195,18,239,116,
  10984. 102,114,94,100,104,228,100,49,238,140,203,42,60,145,35,104,181,146,113,161,
  10985. 10,80,46,68,82,24,245,145,132,108,228,148,54,100,137,64,34,13,100,153,222,
  10986. 1,40,6,33,223,20,84,19,34,95,23,76,130,153,6,103,208,43,64,141,41,130,104,
  10987. 17,112,130,44,96,
  10988. };
  10989. /* to convert a heap stridx to a token number, subtract
  10990. * DUK_STRIDX_START_RESERVED and add DUK_TOK_START_RESERVED.
  10991. */
  10992. /* native functions: 147 */
  10993. DUK_INTERNAL const duk_c_function duk_bi_native_functions[147] = {
  10994. duk_bi_array_constructor,
  10995. duk_bi_array_constructor_is_array,
  10996. duk_bi_array_prototype_concat,
  10997. duk_bi_array_prototype_indexof_shared,
  10998. duk_bi_array_prototype_iter_shared,
  10999. duk_bi_array_prototype_join_shared,
  11000. duk_bi_array_prototype_pop,
  11001. duk_bi_array_prototype_push,
  11002. duk_bi_array_prototype_reduce_shared,
  11003. duk_bi_array_prototype_reverse,
  11004. duk_bi_array_prototype_shift,
  11005. duk_bi_array_prototype_slice,
  11006. duk_bi_array_prototype_sort,
  11007. duk_bi_array_prototype_splice,
  11008. duk_bi_array_prototype_to_string,
  11009. duk_bi_array_prototype_unshift,
  11010. duk_bi_arraybuffer_constructor,
  11011. duk_bi_arraybuffer_isview,
  11012. duk_bi_boolean_constructor,
  11013. duk_bi_boolean_prototype_tostring_shared,
  11014. duk_bi_buffer_compare_shared,
  11015. duk_bi_buffer_constructor,
  11016. duk_bi_buffer_prototype_tostring_shared,
  11017. duk_bi_buffer_readfield,
  11018. duk_bi_buffer_slice_shared,
  11019. duk_bi_buffer_writefield,
  11020. duk_bi_dataview_constructor,
  11021. duk_bi_date_constructor,
  11022. duk_bi_date_constructor_now,
  11023. duk_bi_date_constructor_parse,
  11024. duk_bi_date_constructor_utc,
  11025. duk_bi_date_prototype_get_shared,
  11026. duk_bi_date_prototype_get_timezone_offset,
  11027. duk_bi_date_prototype_set_shared,
  11028. duk_bi_date_prototype_set_time,
  11029. duk_bi_date_prototype_to_json,
  11030. duk_bi_date_prototype_tostring_shared,
  11031. duk_bi_date_prototype_value_of,
  11032. duk_bi_duktape_object_act,
  11033. duk_bi_duktape_object_compact,
  11034. duk_bi_duktape_object_dec,
  11035. duk_bi_duktape_object_enc,
  11036. duk_bi_duktape_object_fin,
  11037. duk_bi_duktape_object_gc,
  11038. duk_bi_duktape_object_info,
  11039. duk_bi_error_constructor_shared,
  11040. duk_bi_error_prototype_filename_getter,
  11041. duk_bi_error_prototype_linenumber_getter,
  11042. duk_bi_error_prototype_nop_setter,
  11043. duk_bi_error_prototype_stack_getter,
  11044. duk_bi_error_prototype_to_string,
  11045. duk_bi_function_constructor,
  11046. duk_bi_function_prototype,
  11047. duk_bi_function_prototype_apply,
  11048. duk_bi_function_prototype_bind,
  11049. duk_bi_function_prototype_call,
  11050. duk_bi_function_prototype_to_string,
  11051. duk_bi_global_object_decode_uri,
  11052. duk_bi_global_object_decode_uri_component,
  11053. duk_bi_global_object_encode_uri,
  11054. duk_bi_global_object_encode_uri_component,
  11055. duk_bi_global_object_escape,
  11056. duk_bi_global_object_eval,
  11057. duk_bi_global_object_is_finite,
  11058. duk_bi_global_object_is_nan,
  11059. duk_bi_global_object_parse_float,
  11060. duk_bi_global_object_parse_int,
  11061. duk_bi_global_object_print_helper,
  11062. duk_bi_global_object_require,
  11063. duk_bi_global_object_unescape,
  11064. duk_bi_json_object_parse,
  11065. duk_bi_json_object_stringify,
  11066. duk_bi_logger_constructor,
  11067. duk_bi_logger_prototype_fmt,
  11068. duk_bi_logger_prototype_log_shared,
  11069. duk_bi_logger_prototype_raw,
  11070. duk_bi_math_object_max,
  11071. duk_bi_math_object_min,
  11072. duk_bi_math_object_onearg_shared,
  11073. duk_bi_math_object_random,
  11074. duk_bi_math_object_twoarg_shared,
  11075. duk_bi_nodejs_buffer_byte_length,
  11076. duk_bi_nodejs_buffer_concat,
  11077. duk_bi_nodejs_buffer_constructor,
  11078. duk_bi_nodejs_buffer_copy,
  11079. duk_bi_nodejs_buffer_fill,
  11080. duk_bi_nodejs_buffer_is_buffer,
  11081. duk_bi_nodejs_buffer_is_encoding,
  11082. duk_bi_nodejs_buffer_tojson,
  11083. duk_bi_nodejs_buffer_tostring,
  11084. duk_bi_nodejs_buffer_write,
  11085. duk_bi_number_constructor,
  11086. duk_bi_number_prototype_to_exponential,
  11087. duk_bi_number_prototype_to_fixed,
  11088. duk_bi_number_prototype_to_locale_string,
  11089. duk_bi_number_prototype_to_precision,
  11090. duk_bi_number_prototype_to_string,
  11091. duk_bi_number_prototype_value_of,
  11092. duk_bi_object_constructor,
  11093. duk_bi_object_constructor_create,
  11094. duk_bi_object_constructor_define_properties,
  11095. duk_bi_object_constructor_define_property,
  11096. duk_bi_object_constructor_get_own_property_descriptor,
  11097. duk_bi_object_constructor_is_extensible,
  11098. duk_bi_object_constructor_is_sealed_frozen_shared,
  11099. duk_bi_object_constructor_keys_shared,
  11100. duk_bi_object_constructor_prevent_extensions,
  11101. duk_bi_object_constructor_seal_freeze_shared,
  11102. duk_bi_object_getprototype_shared,
  11103. duk_bi_object_prototype_has_own_property,
  11104. duk_bi_object_prototype_is_prototype_of,
  11105. duk_bi_object_prototype_property_is_enumerable,
  11106. duk_bi_object_prototype_to_locale_string,
  11107. duk_bi_object_prototype_to_string,
  11108. duk_bi_object_prototype_value_of,
  11109. duk_bi_object_setprototype_shared,
  11110. duk_bi_pointer_constructor,
  11111. duk_bi_pointer_prototype_tostring_shared,
  11112. duk_bi_proxy_constructor,
  11113. duk_bi_regexp_constructor,
  11114. duk_bi_regexp_prototype_exec,
  11115. duk_bi_regexp_prototype_test,
  11116. duk_bi_regexp_prototype_to_string,
  11117. duk_bi_string_constructor,
  11118. duk_bi_string_constructor_from_char_code,
  11119. duk_bi_string_prototype_caseconv_shared,
  11120. duk_bi_string_prototype_char_at,
  11121. duk_bi_string_prototype_char_code_at,
  11122. duk_bi_string_prototype_concat,
  11123. duk_bi_string_prototype_indexof_shared,
  11124. duk_bi_string_prototype_locale_compare,
  11125. duk_bi_string_prototype_match,
  11126. duk_bi_string_prototype_replace,
  11127. duk_bi_string_prototype_search,
  11128. duk_bi_string_prototype_slice,
  11129. duk_bi_string_prototype_split,
  11130. duk_bi_string_prototype_substr,
  11131. duk_bi_string_prototype_substring,
  11132. duk_bi_string_prototype_to_string,
  11133. duk_bi_string_prototype_trim,
  11134. duk_bi_thread_constructor,
  11135. duk_bi_thread_current,
  11136. duk_bi_thread_resume,
  11137. duk_bi_thread_yield,
  11138. duk_bi_type_error_thrower,
  11139. duk_bi_typedarray_constructor,
  11140. duk_bi_typedarray_set,
  11141. };
  11142. DUK_INTERNAL const duk_uint8_t duk_builtins_data[1952] = {
  11143. 105,195,75,16,121,40,105,51,14,252,104,52,8,131,72,0,115,225,65,165,236,55,
  11144. 243,6,145,32,210,24,210,182,25,249,35,120,216,99,226,13,78,225,116,177,164,
  11145. 180,44,192,4,202,52,150,220,24,0,169,70,146,219,123,0,23,40,210,91,110,96,
  11146. 3,37,26,75,109,172,0,108,163,73,109,177,128,14,148,105,45,181,176,1,242,
  11147. 144,56,209,32,94,6,167,101,98,80,211,24,1,250,67,72,168,67,232,13,46,128,
  11148. 47,162,52,164,0,62,80,163,72,128,61,40,107,26,7,37,20,53,200,131,88,0,66,
  11149. 134,185,16,98,80,215,34,11,96,0,138,26,228,65,76,0,69,67,92,136,37,128,6,
  11150. 168,107,145,4,48,1,165,13,114,32,118,0,44,161,174,68,12,192,7,148,53,200,
  11151. 129,88,1,26,134,165,48,130,80,31,255,241,69,224,0,0,124,63,128,0,0,0,46,32,
  11152. 0,0,120,63,128,0,0,0,47,98,7,140,16,116,194,7,12,48,108,196,6,140,80,100,
  11153. 198,6,12,112,92,200,5,140,149,192,202,91,204,181,184,204,91,76,213,176,206,
  11154. 90,204,240,84,208,5,13,9,124,210,43,13,24,64,226,131,205,112,56,216,3,77,
  11155. 152,48,218,130,205,184,40,220,130,77,216,32,222,129,205,248,24,224,129,78,
  11156. 25,214,163,226,90,80,145,104,65,37,157,0,150,99,242,89,78,73,100,58,37,140,
  11157. 236,150,35,194,88,79,73,96,69,37,125,12,122,188,134,62,0,2,165,68,39,255,
  11158. 255,193,43,67,0,0,80,127,192,58,182,216,80,0,21,59,154,64,0,107,76,200,172,
  11159. 180,146,176,198,138,187,43,42,204,136,170,181,146,168,214,80,0,26,155,81,
  11160. 42,77,4,168,180,20,0,6,160,206,74,123,73,64,0,127,255,4,10,153,219,28,198,
  11161. 163,184,130,140,224,10,43,144,40,141,164,161,183,18,132,222,64,161,127,128,
  11162. 0,63,225,1,109,74,8,137,71,56,5,4,213,20,3,115,233,249,177,240,80,255,192,
  11163. 6,120,2,64,127,195,0,173,28,56,20,96,80,128,0,206,192,143,167,64,164,156,
  11164. 131,2,112,14,125,55,9,4,216,40,19,80,180,77,3,9,51,13,94,153,7,159,76,64,
  11165. 207,192,0,102,0,103,255,255,242,240,67,73,112,33,168,0,12,180,16,212,0,10,
  11166. 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,
  11167. 135,195,224,127,196,2,87,132,17,82,143,20,10,44,80,36,239,196,147,63,146,
  11168. 119,0,125,49,129,52,152,64,154,128,0,201,96,137,36,131,36,142,17,18,40,82,
  11169. 77,97,145,33,135,68,130,37,17,247,208,71,159,65,29,125,8,0,12,113,244,32,0,
  11170. 49,184,176,70,162,16,20,95,240,0,7,252,80,37,120,193,81,196,194,0,3,69,19,
  11171. 0,81,191,197,140,192,255,255,239,127,255,255,255,255,140,64,0,0,0,0,1,0,0,
  11172. 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,
  11173. 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,
  11174. 16,69,23,73,19,92,36,73,124,129,71,255,0,56,136,233,34,3,223,208,241,192,3,
  11175. 254,56,18,188,128,0,15,135,240,0,0,0,11,104,228,128,135,18,4,0,6,26,72,16,
  11176. 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,
  11177. 220,228,100,162,146,130,20,74,8,72,248,64,2,33,3,225,0,9,131,143,132,0,42,
  11178. 12,62,16,0,184,40,248,64,3,32,131,225,0,13,129,143,132,0,58,4,62,16,0,248,
  11179. 8,248,64,4,32,3,225,0,17,127,143,132,0,73,252,62,16,1,55,232,248,64,5,31,
  11180. 131,225,0,21,125,143,132,0,89,244,62,16,1,119,201,0,31,4,68,123,144,148,0,
  11181. 97,236,66,80,1,151,169,10,248,0,211,208,133,124,0,109,230,66,254,0,56,242,
  11182. 33,127,0,29,120,144,207,128,15,60,8,103,192,7,221,228,37,0,32,119,16,148,0,
  11183. 133,218,66,190,0,68,236,33,95,0,35,117,144,191,128,18,58,136,95,192,9,92,
  11184. 195,225,0,38,114,144,148,0,156,41,31,224,0,15,249,1,138,144,64,192,2,2,225,
  11185. 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,
  11186. 239,64,10,79,248,0,3,254,72,86,209,5,155,36,17,46,185,137,129,109,203,140,
  11187. 11,78,94,96,13,28,200,1,74,255,0,2,127,202,4,218,43,131,100,130,32,5,47,
  11188. 252,0,9,255,44,19,104,173,237,146,8,128,20,207,240,0,39,252,192,77,162,183,
  11189. 54,72,34,0,83,127,192,0,159,243,65,54,138,218,217,32,136,1,78,255,0,2,127,
  11190. 206,4,218,43,99,100,130,32,5,63,252,0,9,255,60,19,104,173,109,146,8,128,15,
  11191. 255,242,27,16,2,175,193,80,26,85,197,34,218,240,44,90,192,144,5,149,109,
  11192. 110,218,208,16,139,185,143,251,206,126,191,154,176,17,197,125,207,255,160,
  11193. 138,217,90,144,30,242,246,207,195,185,73,133,90,112,62,200,66,80,6,11,81,
  11194. 21,26,80,39,168,57,143,243,78,223,217,154,48,39,168,61,143,243,78,223,217,
  11195. 146,104,39,17,158,156,80,0,22,114,113,64,0,153,169,197,0,3,102,40,33,150,
  11196. 156,80,0,70,82,113,64,1,89,41,197,0,6,100,39,20,0,29,142,156,80,0,134,50,
  11197. 98,243,21,53,121,136,160,144,0,22,26,120,24,73,197,0,9,96,167,20,0,41,128,
  11198. 156,80,0,181,250,113,64,3,1,255,254,0,81,20,100,47,145,216,23,255,240,0,11,
  11199. 255,248,0,3,255,252,81,252,8,1,220,68,0,0,0,0,0,129,167,1,26,144,9,165,0,
  11200. 26,177,199,197,132,30,147,16,120,86,65,217,80,240,232,164,120,114,80,60,52,
  11201. 39,32,84,223,192,15,59,30,129,156,115,6,81,160,7,253,40,0,5,81,252,0,1,255,
  11202. 78,0,84,113,96,128,0,209,69,128,21,87,240,0,7,253,72,1,81,221,66,0,3,69,
  11203. 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,
  11204. 113,3,20,146,12,18,200,47,74,30,23,37,15,128,0,143,146,135,192,0,133,169,
  11205. 67,224,0,98,196,161,240,0,65,90,80,248,0,41,63,255,194,109,65,11,137,191,
  11206. 174,45,153,98,242,229,191,147,102,8,190,94,92,183,242,65,167,114,12,188,
  11207. 185,111,228,131,70,29,217,54,105,221,156,0,171,255,128,9,208,68,128,255,
  11208. 174,0,25,168,194,64,0,130,177,254,0,0,255,176,1,3,120,186,64,12,13,194,233,
  11209. 0,32,54,139,164,0,196,216,46,144,2,19,88,186,64,12,141,66,233,0,34,52,139,
  11210. 164,0,140,208,46,144,2,67,56,203,64,12,12,195,45,0,32,50,140,180,0,196,200,
  11211. 50,208,2,19,24,203,64,12,140,67,45,0,34,48,140,180,0,140,192,50,208,2,64,
  11212. 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,
  11213. 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,
  11214. 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,
  11215. 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,
  11216. 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,
  11217. 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,
  11218. 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,
  11219. 171,17,64,162,132,248,162,64,0,193,255,138,5,137,161,116,38,69,210,0,32,
  11220. 152,23,72,0,10,92,93,32,1,41,97,116,128,8,165,69,210,0,50,148,23,72,0,18,
  11221. 76,93,32,1,73,33,116,128,9,36,69,210,0,52,144,23,72,0,26,60,93,32,1,104,
  11222. 225,116,128,2,35,69,210,0,24,140,23,104,0,42,44,93,160,1,168,161,118,128,
  11223. 10,162,69,218,0,58,136,25,98,28,101,160,2,8,97,150,128,0,161,70,90,0,18,
  11224. 132,25,104,0,138,12,101,160,3,40,33,150,128,1,32,70,90,0,20,128,25,104,0,
  11225. 145,252,101,160,3,71,225,150,128,1,159,70,90,0,22,124,25,104,0,33,236,101,
  11226. 160,1,135,161,152,128,2,158,70,98,0,26,120,25,136,0,169,220,102,32,3,180,
  11227. 117,150,57,214,0,157,85,98,112,80,137,241,66,128,0,166,213,33,53,24,66,121,
  11228. 106,0,
  11229. };
  11230. #ifdef DUK_USE_BUILTIN_INITJS
  11231. DUK_INTERNAL const duk_uint8_t duk_initjs_data[187] = {
  11232. 40,102,117,110,99,116,105,111,110,40,100,44,97,41,123,102,117,110,99,116,
  11233. 105,111,110,32,98,40,97,44,98,44,99,41,123,79,98,106,101,99,116,46,100,101,
  11234. 102,105,110,101,80,114,111,112,101,114,116,121,40,97,44,98,44,123,118,97,
  11235. 108,117,101,58,99,44,119,114,105,116,97,98,108,101,58,33,48,44,101,110,117,
  11236. 109,101,114,97,98,108,101,58,33,49,44,99,111,110,102,105,103,117,114,97,98,
  11237. 108,101,58,33,48,125,41,125,98,40,97,46,76,111,103,103,101,114,44,34,99,
  11238. 108,111,103,34,44,110,101,119,32,97,46,76,111,103,103,101,114,40,34,67,34,
  11239. 41,41,59,98,40,97,44,34,109,111,100,76,111,97,100,101,100,34,44,123,125,41,
  11240. 125,41,40,116,104,105,115,44,68,117,107,116,97,112,101,41,59,10,0,
  11241. };
  11242. #endif /* DUK_USE_BUILTIN_INITJS */
  11243. #else
  11244. #error invalid endianness defines
  11245. #endif
  11246. #line 1 "duk_error_macros.c"
  11247. /*
  11248. * Error, fatal, and panic handling.
  11249. */
  11250. /* include removed: duk_internal.h */
  11251. #define DUK__ERRFMT_BUFSIZE 256 /* size for formatting buffers */
  11252. #ifdef DUK_USE_VERBOSE_ERRORS
  11253. #ifdef DUK_USE_VARIADIC_MACROS
  11254. DUK_INTERNAL void duk_err_handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
  11255. va_list ap;
  11256. char msg[DUK__ERRFMT_BUFSIZE];
  11257. va_start(ap, fmt);
  11258. (void) DUK_VSNPRINTF(msg, sizeof(msg), fmt, ap);
  11259. msg[sizeof(msg) - 1] = (char) 0;
  11260. duk_err_create_and_throw(thr, code, msg, filename, line);
  11261. va_end(ap); /* dead code, but ensures portability (see Linux man page notes) */
  11262. }
  11263. #else /* DUK_USE_VARIADIC_MACROS */
  11264. DUK_INTERNAL const char *duk_err_file_stash = NULL;
  11265. DUK_INTERNAL duk_int_t duk_err_line_stash = 0;
  11266. 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));
  11267. 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) {
  11268. char msg[DUK__ERRFMT_BUFSIZE];
  11269. (void) DUK_VSNPRINTF(msg, sizeof(msg), fmt, ap);
  11270. msg[sizeof(msg) - 1] = (char) 0;
  11271. duk_err_create_and_throw(thr, code, msg, filename, line);
  11272. }
  11273. DUK_INTERNAL void duk_err_handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
  11274. va_list ap;
  11275. va_start(ap, fmt);
  11276. duk__handle_error(filename, line, thr, code, fmt, ap);
  11277. va_end(ap); /* dead code */
  11278. }
  11279. DUK_INTERNAL void duk_err_handle_error_stash(duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
  11280. va_list ap;
  11281. va_start(ap, fmt);
  11282. duk__handle_error(duk_err_file_stash, duk_err_line_stash, thr, code, fmt, ap);
  11283. va_end(ap); /* dead code */
  11284. }
  11285. #endif /* DUK_USE_VARIADIC_MACROS */
  11286. #else /* DUK_USE_VERBOSE_ERRORS */
  11287. #ifdef DUK_USE_VARIADIC_MACROS
  11288. DUK_INTERNAL void duk_err_handle_error(duk_hthread *thr, duk_errcode_t code) {
  11289. duk_err_create_and_throw(thr, code);
  11290. }
  11291. #else /* DUK_USE_VARIADIC_MACROS */
  11292. DUK_INTERNAL void duk_err_handle_error_nonverbose1(duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
  11293. DUK_UNREF(fmt);
  11294. duk_err_create_and_throw(thr, code);
  11295. }
  11296. 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, ...) {
  11297. DUK_UNREF(filename);
  11298. DUK_UNREF(line);
  11299. DUK_UNREF(fmt);
  11300. duk_err_create_and_throw(thr, code);
  11301. }
  11302. #endif /* DUK_USE_VARIADIC_MACROS */
  11303. #endif /* DUK_USE_VERBOSE_ERRORS */
  11304. /*
  11305. * Default fatal error handler
  11306. */
  11307. DUK_INTERNAL void duk_default_fatal_handler(duk_context *ctx, duk_errcode_t code, const char *msg) {
  11308. DUK_UNREF(ctx);
  11309. #ifdef DUK_USE_FILE_IO
  11310. DUK_FPRINTF(DUK_STDERR, "FATAL %ld: %s\n", (long) code, (const char *) (msg ? msg : "null"));
  11311. DUK_FFLUSH(DUK_STDERR);
  11312. #else
  11313. /* omit print */
  11314. #endif
  11315. DUK_D(DUK_DPRINT("default fatal handler called, code %ld -> calling DUK_PANIC()", (long) code));
  11316. DUK_PANIC(code, msg);
  11317. DUK_UNREACHABLE();
  11318. }
  11319. /*
  11320. * Default panic handler
  11321. */
  11322. #if !defined(DUK_USE_PANIC_HANDLER)
  11323. DUK_INTERNAL void duk_default_panic_handler(duk_errcode_t code, const char *msg) {
  11324. #ifdef DUK_USE_FILE_IO
  11325. DUK_FPRINTF(DUK_STDERR, "PANIC %ld: %s ("
  11326. #if defined(DUK_USE_PANIC_ABORT)
  11327. "calling abort"
  11328. #elif defined(DUK_USE_PANIC_EXIT)
  11329. "calling exit"
  11330. #elif defined(DUK_USE_PANIC_SEGFAULT)
  11331. "segfaulting on purpose"
  11332. #else
  11333. #error no DUK_USE_PANIC_xxx macro defined
  11334. #endif
  11335. ")\n", (long) code, (const char *) (msg ? msg : "null"));
  11336. DUK_FFLUSH(DUK_STDERR);
  11337. #else
  11338. /* omit print */
  11339. DUK_UNREF(code);
  11340. DUK_UNREF(msg);
  11341. #endif
  11342. #if defined(DUK_USE_PANIC_ABORT)
  11343. DUK_ABORT();
  11344. #elif defined(DUK_USE_PANIC_EXIT)
  11345. DUK_EXIT(-1);
  11346. #elif defined(DUK_USE_PANIC_SEGFAULT)
  11347. /* exit() afterwards to satisfy "noreturn" */
  11348. DUK_CAUSE_SEGFAULT(); /* SCANBUILD: "Dereference of null pointer", normal */
  11349. DUK_EXIT(-1);
  11350. #else
  11351. #error no DUK_USE_PANIC_xxx macro defined
  11352. #endif
  11353. DUK_UNREACHABLE();
  11354. }
  11355. #endif /* !DUK_USE_PANIC_HANDLER */
  11356. #undef DUK__ERRFMT_BUFSIZE
  11357. #line 1 "duk_unicode_support.c"
  11358. /*
  11359. * Various Unicode help functions for character classification predicates,
  11360. * case conversion, decoding, etc.
  11361. */
  11362. /* include removed: duk_internal.h */
  11363. /*
  11364. * XUTF-8 and CESU-8 encoding/decoding
  11365. */
  11366. DUK_INTERNAL duk_small_int_t duk_unicode_get_xutf8_length(duk_ucodepoint_t cp) {
  11367. duk_uint_fast32_t x = (duk_uint_fast32_t) cp;
  11368. if (x < 0x80UL) {
  11369. /* 7 bits */
  11370. return 1;
  11371. } else if (x < 0x800UL) {
  11372. /* 11 bits */
  11373. return 2;
  11374. } else if (x < 0x10000UL) {
  11375. /* 16 bits */
  11376. return 3;
  11377. } else if (x < 0x200000UL) {
  11378. /* 21 bits */
  11379. return 4;
  11380. } else if (x < 0x4000000UL) {
  11381. /* 26 bits */
  11382. return 5;
  11383. } else if (x < (duk_ucodepoint_t) 0x80000000UL) {
  11384. /* 31 bits */
  11385. return 6;
  11386. } else {
  11387. /* 36 bits */
  11388. return 7;
  11389. }
  11390. }
  11391. DUK_INTERNAL duk_uint8_t duk_unicode_xutf8_markers[7] = {
  11392. 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
  11393. };
  11394. /* Encode to extended UTF-8; 'out' must have space for at least
  11395. * DUK_UNICODE_MAX_XUTF8_LENGTH bytes. Allows encoding of any
  11396. * 32-bit (unsigned) codepoint.
  11397. */
  11398. DUK_INTERNAL duk_small_int_t duk_unicode_encode_xutf8(duk_ucodepoint_t cp, duk_uint8_t *out) {
  11399. duk_uint_fast32_t x = (duk_uint_fast32_t) cp;
  11400. duk_small_int_t len;
  11401. duk_uint8_t marker;
  11402. duk_small_int_t i;
  11403. len = duk_unicode_get_xutf8_length(cp);
  11404. DUK_ASSERT(len > 0);
  11405. marker = duk_unicode_xutf8_markers[len - 1]; /* 64-bit OK because always >= 0 */
  11406. i = len;
  11407. DUK_ASSERT(i > 0);
  11408. do {
  11409. i--;
  11410. if (i > 0) {
  11411. out[i] = (duk_uint8_t) (0x80 + (x & 0x3f));
  11412. x >>= 6;
  11413. } else {
  11414. /* Note: masking of 'x' is not necessary because of
  11415. * range check and shifting -> no bits overlapping
  11416. * the marker should be set.
  11417. */
  11418. out[0] = (duk_uint8_t) (marker + x);
  11419. }
  11420. } while (i > 0);
  11421. return len;
  11422. }
  11423. /* Encode to CESU-8; 'out' must have space for at least
  11424. * DUK_UNICODE_MAX_CESU8_LENGTH bytes; codepoints above U+10FFFF
  11425. * will encode to garbage but won't overwrite the output buffer.
  11426. */
  11427. DUK_INTERNAL duk_small_int_t duk_unicode_encode_cesu8(duk_ucodepoint_t cp, duk_uint8_t *out) {
  11428. duk_uint_fast32_t x = (duk_uint_fast32_t) cp;
  11429. duk_small_int_t len;
  11430. if (x < 0x80UL) {
  11431. out[0] = (duk_uint8_t) x;
  11432. len = 1;
  11433. } else if (x < 0x800UL) {
  11434. out[0] = (duk_uint8_t) (0xc0 + ((x >> 6) & 0x1f));
  11435. out[1] = (duk_uint8_t) (0x80 + (x & 0x3f));
  11436. len = 2;
  11437. } else if (x < 0x10000UL) {
  11438. /* surrogate pairs get encoded here */
  11439. out[0] = (duk_uint8_t) (0xe0 + ((x >> 12) & 0x0f));
  11440. out[1] = (duk_uint8_t) (0x80 + ((x >> 6) & 0x3f));
  11441. out[2] = (duk_uint8_t) (0x80 + (x & 0x3f));
  11442. len = 3;
  11443. } else {
  11444. /*
  11445. * Unicode codepoints above U+FFFF are encoded as surrogate
  11446. * pairs here. This ensures that all CESU-8 codepoints are
  11447. * 16-bit values as expected in Ecmascript. The surrogate
  11448. * pairs always get a 3-byte encoding (each) in CESU-8.
  11449. * See: http://en.wikipedia.org/wiki/Surrogate_pair
  11450. *
  11451. * 20-bit codepoint, 10 bits (A and B) per surrogate pair:
  11452. *
  11453. * x = 0b00000000 0000AAAA AAAAAABB BBBBBBBB
  11454. * sp1 = 0b110110AA AAAAAAAA (0xd800 + ((x >> 10) & 0x3ff))
  11455. * sp2 = 0b110111BB BBBBBBBB (0xdc00 + (x & 0x3ff))
  11456. *
  11457. * Encoded into CESU-8:
  11458. *
  11459. * sp1 -> 0b11101101 (0xe0 + ((sp1 >> 12) & 0x0f))
  11460. * -> 0b1010AAAA (0x80 + ((sp1 >> 6) & 0x3f))
  11461. * -> 0b10AAAAAA (0x80 + (sp1 & 0x3f))
  11462. * sp2 -> 0b11101101 (0xe0 + ((sp2 >> 12) & 0x0f))
  11463. * -> 0b1011BBBB (0x80 + ((sp2 >> 6) & 0x3f))
  11464. * -> 0b10BBBBBB (0x80 + (sp2 & 0x3f))
  11465. *
  11466. * Note that 0x10000 must be subtracted first. The code below
  11467. * avoids the sp1, sp2 temporaries which saves around 20 bytes
  11468. * of code.
  11469. */
  11470. x -= 0x10000UL;
  11471. out[0] = (duk_uint8_t) (0xed);
  11472. out[1] = (duk_uint8_t) (0xa0 + ((x >> 16) & 0x0f));
  11473. out[2] = (duk_uint8_t) (0x80 + ((x >> 10) & 0x3f));
  11474. out[3] = (duk_uint8_t) (0xed);
  11475. out[4] = (duk_uint8_t) (0xb0 + ((x >> 6) & 0x0f));
  11476. out[5] = (duk_uint8_t) (0x80 + (x & 0x3f));
  11477. len = 6;
  11478. }
  11479. return len;
  11480. }
  11481. /* Decode helper. Return zero on error. */
  11482. 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) {
  11483. const duk_uint8_t *p;
  11484. duk_uint32_t res;
  11485. duk_uint_fast8_t ch;
  11486. duk_small_int_t n;
  11487. DUK_UNREF(thr);
  11488. p = *ptr;
  11489. if (p < ptr_start || p >= ptr_end) {
  11490. goto fail;
  11491. }
  11492. /*
  11493. * UTF-8 decoder which accepts longer than standard byte sequences.
  11494. * This allows full 32-bit code points to be used.
  11495. */
  11496. ch = (duk_uint_fast8_t) (*p++);
  11497. if (ch < 0x80) {
  11498. /* 0xxx xxxx [7 bits] */
  11499. res = (duk_uint32_t) (ch & 0x7f);
  11500. n = 0;
  11501. } else if (ch < 0xc0) {
  11502. /* 10xx xxxx -> invalid */
  11503. goto fail;
  11504. } else if (ch < 0xe0) {
  11505. /* 110x xxxx 10xx xxxx [11 bits] */
  11506. res = (duk_uint32_t) (ch & 0x1f);
  11507. n = 1;
  11508. } else if (ch < 0xf0) {
  11509. /* 1110 xxxx 10xx xxxx 10xx xxxx [16 bits] */
  11510. res = (duk_uint32_t) (ch & 0x0f);
  11511. n = 2;
  11512. } else if (ch < 0xf8) {
  11513. /* 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx [21 bits] */
  11514. res = (duk_uint32_t) (ch & 0x07);
  11515. n = 3;
  11516. } else if (ch < 0xfc) {
  11517. /* 1111 10xx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [26 bits] */
  11518. res = (duk_uint32_t) (ch & 0x03);
  11519. n = 4;
  11520. } else if (ch < 0xfe) {
  11521. /* 1111 110x 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [31 bits] */
  11522. res = (duk_uint32_t) (ch & 0x01);
  11523. n = 5;
  11524. } else if (ch < 0xff) {
  11525. /* 1111 1110 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [36 bits] */
  11526. res = (duk_uint32_t) (0);
  11527. n = 6;
  11528. } else {
  11529. /* 8-byte format could be:
  11530. * 1111 1111 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [41 bits]
  11531. *
  11532. * However, this format would not have a zero bit following the
  11533. * leading one bits and would not allow 0xFF to be used as an
  11534. * "invalid xutf-8" marker for internal keys. Further, 8-byte
  11535. * encodings (up to 41 bit code points) are not currently needed.
  11536. */
  11537. goto fail;
  11538. }
  11539. DUK_ASSERT(p >= ptr_start); /* verified at beginning */
  11540. if (p + n > ptr_end) {
  11541. /* check pointer at end */
  11542. goto fail;
  11543. }
  11544. while (n > 0) {
  11545. DUK_ASSERT(p >= ptr_start && p < ptr_end);
  11546. res = res << 6;
  11547. res += (duk_uint32_t) ((*p++) & 0x3f);
  11548. n--;
  11549. }
  11550. *ptr = p;
  11551. *out_cp = res;
  11552. return 1;
  11553. fail:
  11554. return 0;
  11555. }
  11556. /* used by e.g. duk_regexp_executor.c, string built-ins */
  11557. 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) {
  11558. duk_ucodepoint_t cp;
  11559. if (duk_unicode_decode_xutf8(thr, ptr, ptr_start, ptr_end, &cp)) {
  11560. return cp;
  11561. }
  11562. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, "utf-8 decode failed");
  11563. DUK_UNREACHABLE();
  11564. return 0;
  11565. }
  11566. /* (extended) utf-8 length without codepoint encoding validation, used
  11567. * for string interning (should probably be inlined).
  11568. */
  11569. DUK_INTERNAL duk_size_t duk_unicode_unvalidated_utf8_length(const duk_uint8_t *data, duk_size_t blen) {
  11570. const duk_uint8_t *p = data;
  11571. const duk_uint8_t *p_end = data + blen;
  11572. duk_size_t clen = 0;
  11573. while (p < p_end) {
  11574. duk_uint8_t x = *p++;
  11575. if (x < 0x80 || x >= 0xc0) {
  11576. /* 10xxxxxx = continuation chars (0x80...0xbf), above
  11577. * and below that initial bytes.
  11578. */
  11579. clen++;
  11580. }
  11581. }
  11582. return clen;
  11583. }
  11584. /*
  11585. * Unicode range matcher
  11586. *
  11587. * Matches a codepoint against a packed bitstream of character ranges.
  11588. * Used for slow path Unicode matching.
  11589. */
  11590. /* Must match src/extract_chars.py, generate_match_table3(). */
  11591. DUK_LOCAL duk_uint32_t duk__uni_decode_value(duk_bitdecoder_ctx *bd_ctx) {
  11592. duk_uint32_t t;
  11593. t = (duk_uint32_t) duk_bd_decode(bd_ctx, 4);
  11594. if (t <= 0x0eU) {
  11595. return t;
  11596. }
  11597. t = (duk_uint32_t) duk_bd_decode(bd_ctx, 8);
  11598. if (t <= 0xfdU) {
  11599. return t + 0x0f;
  11600. }
  11601. if (t == 0xfeU) {
  11602. t = (duk_uint32_t) duk_bd_decode(bd_ctx, 12);
  11603. return t + 0x0fU + 0xfeU;
  11604. } else {
  11605. t = (duk_uint32_t) duk_bd_decode(bd_ctx, 24);
  11606. return t + 0x0fU + 0xfeU + 0x1000UL;
  11607. }
  11608. }
  11609. DUK_LOCAL duk_small_int_t duk__uni_range_match(const duk_uint8_t *unitab, duk_size_t unilen, duk_codepoint_t cp) {
  11610. duk_bitdecoder_ctx bd_ctx;
  11611. duk_codepoint_t prev_re;
  11612. DUK_MEMZERO(&bd_ctx, sizeof(bd_ctx));
  11613. bd_ctx.data = (duk_uint8_t *) unitab;
  11614. bd_ctx.length = (duk_size_t) unilen;
  11615. prev_re = 0;
  11616. for (;;) {
  11617. duk_codepoint_t r1, r2;
  11618. r1 = (duk_codepoint_t) duk__uni_decode_value(&bd_ctx);
  11619. if (r1 == 0) {
  11620. break;
  11621. }
  11622. r2 = (duk_codepoint_t) duk__uni_decode_value(&bd_ctx);
  11623. r1 = prev_re + r1;
  11624. r2 = r1 + r2;
  11625. prev_re = r2;
  11626. /* [r1,r2] is the range */
  11627. DUK_DDD(DUK_DDDPRINT("duk__uni_range_match: cp=%06lx range=[0x%06lx,0x%06lx]",
  11628. (unsigned long) cp, (unsigned long) r1, (unsigned long) r2));
  11629. if (cp >= r1 && cp <= r2) {
  11630. return 1;
  11631. }
  11632. }
  11633. return 0;
  11634. }
  11635. /*
  11636. * "WhiteSpace" production check.
  11637. */
  11638. DUK_INTERNAL duk_small_int_t duk_unicode_is_whitespace(duk_codepoint_t cp) {
  11639. /*
  11640. * E5 Section 7.2 specifies six characters specifically as
  11641. * white space:
  11642. *
  11643. * 0009;<control>;Cc;0;S;;;;;N;CHARACTER TABULATION;;;;
  11644. * 000B;<control>;Cc;0;S;;;;;N;LINE TABULATION;;;;
  11645. * 000C;<control>;Cc;0;WS;;;;;N;FORM FEED (FF);;;;
  11646. * 0020;SPACE;Zs;0;WS;;;;;N;;;;;
  11647. * 00A0;NO-BREAK SPACE;Zs;0;CS;<noBreak> 0020;;;;N;NON-BREAKING SPACE;;;;
  11648. * FEFF;ZERO WIDTH NO-BREAK SPACE;Cf;0;BN;;;;;N;BYTE ORDER MARK;;;;
  11649. *
  11650. * It also specifies any Unicode category 'Zs' characters as white
  11651. * space. These can be extracted with the "src/extract_chars.py" script.
  11652. * Current result:
  11653. *
  11654. * RAW OUTPUT:
  11655. * ===========
  11656. * 0020;SPACE;Zs;0;WS;;;;;N;;;;;
  11657. * 00A0;NO-BREAK SPACE;Zs;0;CS;<noBreak> 0020;;;;N;NON-BREAKING SPACE;;;;
  11658. * 1680;OGHAM SPACE MARK;Zs;0;WS;;;;;N;;;;;
  11659. * 180E;MONGOLIAN VOWEL SEPARATOR;Zs;0;WS;;;;;N;;;;;
  11660. * 2000;EN QUAD;Zs;0;WS;2002;;;;N;;;;;
  11661. * 2001;EM QUAD;Zs;0;WS;2003;;;;N;;;;;
  11662. * 2002;EN SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11663. * 2003;EM SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11664. * 2004;THREE-PER-EM SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11665. * 2005;FOUR-PER-EM SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11666. * 2006;SIX-PER-EM SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11667. * 2007;FIGURE SPACE;Zs;0;WS;<noBreak> 0020;;;;N;;;;;
  11668. * 2008;PUNCTUATION SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11669. * 2009;THIN SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11670. * 200A;HAIR SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11671. * 202F;NARROW NO-BREAK SPACE;Zs;0;CS;<noBreak> 0020;;;;N;;;;;
  11672. * 205F;MEDIUM MATHEMATICAL SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11673. * 3000;IDEOGRAPHIC SPACE;Zs;0;WS;<wide> 0020;;;;N;;;;;
  11674. *
  11675. * RANGES:
  11676. * =======
  11677. * 0x0020
  11678. * 0x00a0
  11679. * 0x1680
  11680. * 0x180e
  11681. * 0x2000 ... 0x200a
  11682. * 0x202f
  11683. * 0x205f
  11684. * 0x3000
  11685. *
  11686. * A manual decoder (below) is probably most compact for this.
  11687. */
  11688. duk_uint_fast8_t lo;
  11689. duk_uint_fast32_t hi;
  11690. /* cp == -1 (EOF) never matches and causes return value 0 */
  11691. lo = (duk_uint_fast8_t) (cp & 0xff);
  11692. hi = (duk_uint_fast32_t) (cp >> 8); /* does not fit into an uchar */
  11693. if (hi == 0x0000UL) {
  11694. if (lo == 0x09U || lo == 0x0bU || lo == 0x0cU ||
  11695. lo == 0x20U || lo == 0xa0U) {
  11696. return 1;
  11697. }
  11698. } else if (hi == 0x0020UL) {
  11699. if (lo <= 0x0aU || lo == 0x2fU || lo == 0x5fU) {
  11700. return 1;
  11701. }
  11702. } else if (cp == 0x1680L || cp == 0x180eL || cp == 0x3000L ||
  11703. cp == 0xfeffL) {
  11704. return 1;
  11705. }
  11706. return 0;
  11707. }
  11708. /*
  11709. * "LineTerminator" production check.
  11710. */
  11711. DUK_INTERNAL duk_small_int_t duk_unicode_is_line_terminator(duk_codepoint_t cp) {
  11712. /*
  11713. * E5 Section 7.3
  11714. *
  11715. * A LineTerminatorSequence essentially merges <CR> <LF> sequences
  11716. * into a single line terminator. This must be handled by the caller.
  11717. */
  11718. if (cp == 0x000aL || cp == 0x000dL || cp == 0x2028L ||
  11719. cp == 0x2029L) {
  11720. return 1;
  11721. }
  11722. return 0;
  11723. }
  11724. /*
  11725. * "IdentifierStart" production check.
  11726. */
  11727. DUK_INTERNAL duk_small_int_t duk_unicode_is_identifier_start(duk_codepoint_t cp) {
  11728. /*
  11729. * E5 Section 7.6:
  11730. *
  11731. * IdentifierStart:
  11732. * UnicodeLetter
  11733. * $
  11734. * _
  11735. * \ UnicodeEscapeSequence
  11736. *
  11737. * IdentifierStart production has one multi-character production:
  11738. *
  11739. * \ UnicodeEscapeSequence
  11740. *
  11741. * The '\' character is -not- matched by this function. Rather, the caller
  11742. * should decode the escape and then call this function to check whether the
  11743. * decoded character is acceptable (see discussion in E5 Section 7.6).
  11744. *
  11745. * The "UnicodeLetter" alternative of the production allows letters
  11746. * from various Unicode categories. These can be extracted with the
  11747. * "src/extract_chars.py" script.
  11748. *
  11749. * Because the result has hundreds of Unicode codepoint ranges, matching
  11750. * for any values >= 0x80 are done using a very slow range-by-range scan
  11751. * and a packed range format.
  11752. *
  11753. * The ASCII portion (codepoints 0x00 ... 0x7f) is fast-pathed below because
  11754. * it matters the most. The ASCII related ranges of IdentifierStart are:
  11755. *
  11756. * 0x0041 ... 0x005a ['A' ... 'Z']
  11757. * 0x0061 ... 0x007a ['a' ... 'z']
  11758. * 0x0024 ['$']
  11759. * 0x005f ['_']
  11760. */
  11761. /* ASCII (and EOF) fast path -- quick accept and reject */
  11762. if (cp <= 0x7fL) {
  11763. if ((cp >= 'a' && cp <= 'z') ||
  11764. (cp >= 'A' && cp <= 'Z') ||
  11765. cp == '_' || cp == '$') {
  11766. return 1;
  11767. }
  11768. return 0;
  11769. }
  11770. /* Non-ASCII slow path (range-by-range linear comparison), very slow */
  11771. #ifdef DUK_USE_SOURCE_NONBMP
  11772. if (duk__uni_range_match(duk_unicode_ids_noa,
  11773. (duk_size_t) sizeof(duk_unicode_ids_noa),
  11774. (duk_codepoint_t) cp)) {
  11775. return 1;
  11776. }
  11777. return 0;
  11778. #else
  11779. if (cp < 0x10000L) {
  11780. if (duk__uni_range_match(duk_unicode_ids_noabmp,
  11781. sizeof(duk_unicode_ids_noabmp),
  11782. (duk_codepoint_t) cp)) {
  11783. return 1;
  11784. }
  11785. return 0;
  11786. } else {
  11787. /* without explicit non-BMP support, assume non-BMP characters
  11788. * are always accepted as identifier characters.
  11789. */
  11790. return 1;
  11791. }
  11792. #endif
  11793. }
  11794. /*
  11795. * "IdentifierPart" production check.
  11796. */
  11797. DUK_INTERNAL duk_small_int_t duk_unicode_is_identifier_part(duk_codepoint_t cp) {
  11798. /*
  11799. * E5 Section 7.6:
  11800. *
  11801. * IdentifierPart:
  11802. * IdentifierStart
  11803. * UnicodeCombiningMark
  11804. * UnicodeDigit
  11805. * UnicodeConnectorPunctuation
  11806. * <ZWNJ> [U+200C]
  11807. * <ZWJ> [U+200D]
  11808. *
  11809. * IdentifierPart production has one multi-character production
  11810. * as part of its IdentifierStart alternative. The '\' character
  11811. * of an escape sequence is not matched here, see discussion in
  11812. * duk_unicode_is_identifier_start().
  11813. *
  11814. * To match non-ASCII characters (codepoints >= 0x80), a very slow
  11815. * linear range-by-range scan is used. The codepoint is first compared
  11816. * to the IdentifierStart ranges, and if it doesn't match, then to a
  11817. * set consisting of code points in IdentifierPart but not in
  11818. * IdentifierStart. This is done to keep the unicode range data small,
  11819. * at the expense of speed.
  11820. *
  11821. * The ASCII fast path consists of:
  11822. *
  11823. * 0x0030 ... 0x0039 ['0' ... '9', UnicodeDigit]
  11824. * 0x0041 ... 0x005a ['A' ... 'Z', IdentifierStart]
  11825. * 0x0061 ... 0x007a ['a' ... 'z', IdentifierStart]
  11826. * 0x0024 ['$', IdentifierStart]
  11827. * 0x005f ['_', IdentifierStart and
  11828. * UnicodeConnectorPunctuation]
  11829. *
  11830. * UnicodeCombiningMark has no code points <= 0x7f.
  11831. *
  11832. * The matching code reuses the "identifier start" tables, and then
  11833. * consults a separate range set for characters in "identifier part"
  11834. * but not in "identifier start". These can be extracted with the
  11835. * "src/extract_chars.py" script.
  11836. *
  11837. * UnicodeCombiningMark -> categories Mn, Mc
  11838. * UnicodeDigit -> categories Nd
  11839. * UnicodeConnectorPunctuation -> categories Pc
  11840. */
  11841. /* ASCII (and EOF) fast path -- quick accept and reject */
  11842. if (cp <= 0x7fL) {
  11843. if ((cp >= 'a' && cp <= 'z') ||
  11844. (cp >= 'A' && cp <= 'Z') ||
  11845. (cp >= '0' && cp <= '9') ||
  11846. cp == '_' || cp == '$') {
  11847. return 1;
  11848. }
  11849. return 0;
  11850. }
  11851. /* Non-ASCII slow path (range-by-range linear comparison), very slow */
  11852. #ifdef DUK_USE_SOURCE_NONBMP
  11853. if (duk__uni_range_match(duk_unicode_ids_noa,
  11854. sizeof(duk_unicode_ids_noa),
  11855. (duk_codepoint_t) cp) ||
  11856. duk__uni_range_match(duk_unicode_idp_m_ids_noa,
  11857. sizeof(duk_unicode_idp_m_ids_noa),
  11858. (duk_codepoint_t) cp)) {
  11859. return 1;
  11860. }
  11861. return 0;
  11862. #else
  11863. if (cp < 0x10000L) {
  11864. if (duk__uni_range_match(duk_unicode_ids_noabmp,
  11865. sizeof(duk_unicode_ids_noabmp),
  11866. (duk_codepoint_t) cp) ||
  11867. duk__uni_range_match(duk_unicode_idp_m_ids_noabmp,
  11868. sizeof(duk_unicode_idp_m_ids_noabmp),
  11869. (duk_codepoint_t) cp)) {
  11870. return 1;
  11871. }
  11872. return 0;
  11873. } else {
  11874. /* without explicit non-BMP support, assume non-BMP characters
  11875. * are always accepted as identifier characters.
  11876. */
  11877. return 1;
  11878. }
  11879. #endif
  11880. }
  11881. /*
  11882. * Unicode letter check.
  11883. */
  11884. DUK_INTERNAL duk_small_int_t duk_unicode_is_letter(duk_codepoint_t cp) {
  11885. /*
  11886. * Unicode letter is now taken to be the categories:
  11887. *
  11888. * Lu, Ll, Lt, Lm, Lo
  11889. *
  11890. * (Not sure if this is exactly correct.)
  11891. *
  11892. * The ASCII fast path consists of:
  11893. *
  11894. * 0x0041 ... 0x005a ['A' ... 'Z']
  11895. * 0x0061 ... 0x007a ['a' ... 'z']
  11896. */
  11897. /* ASCII (and EOF) fast path -- quick accept and reject */
  11898. if (cp <= 0x7fL) {
  11899. if ((cp >= 'a' && cp <= 'z') ||
  11900. (cp >= 'A' && cp <= 'Z')) {
  11901. return 1;
  11902. }
  11903. return 0;
  11904. }
  11905. /* Non-ASCII slow path (range-by-range linear comparison), very slow */
  11906. #ifdef DUK_USE_SOURCE_NONBMP
  11907. if (duk__uni_range_match(duk_unicode_ids_noa,
  11908. sizeof(duk_unicode_ids_noa),
  11909. (duk_codepoint_t) cp) &&
  11910. !duk__uni_range_match(duk_unicode_ids_m_let_noa,
  11911. sizeof(duk_unicode_ids_m_let_noa),
  11912. (duk_codepoint_t) cp)) {
  11913. return 1;
  11914. }
  11915. return 0;
  11916. #else
  11917. if (cp < 0x10000L) {
  11918. if (duk__uni_range_match(duk_unicode_ids_noabmp,
  11919. sizeof(duk_unicode_ids_noabmp),
  11920. (duk_codepoint_t) cp) &&
  11921. !duk__uni_range_match(duk_unicode_ids_m_let_noabmp,
  11922. sizeof(duk_unicode_ids_m_let_noabmp),
  11923. (duk_codepoint_t) cp)) {
  11924. return 1;
  11925. }
  11926. return 0;
  11927. } else {
  11928. /* without explicit non-BMP support, assume non-BMP characters
  11929. * are always accepted as letters.
  11930. */
  11931. return 1;
  11932. }
  11933. #endif
  11934. }
  11935. /*
  11936. * Complex case conversion helper which decodes a bit-packed conversion
  11937. * control stream generated by unicode/extract_caseconv.py. The conversion
  11938. * is very slow because it runs through the conversion data in a linear
  11939. * fashion to save space (which is why ASCII characters have a special
  11940. * fast path before arriving here).
  11941. *
  11942. * The particular bit counts etc have been determined experimentally to
  11943. * be small but still sufficient, and must match the Python script
  11944. * (src/extract_caseconv.py).
  11945. *
  11946. * The return value is the case converted codepoint or -1 if the conversion
  11947. * results in multiple characters (this is useful for regexp Canonicalization
  11948. * operation). If 'buf' is not NULL, the result codepoint(s) are also
  11949. * appended to the hbuffer.
  11950. *
  11951. * Context and locale specific rules must be checked before consulting
  11952. * this function.
  11953. */
  11954. DUK_LOCAL
  11955. duk_codepoint_t duk__slow_case_conversion(duk_hthread *thr,
  11956. duk_hbuffer_dynamic *buf,
  11957. duk_codepoint_t cp,
  11958. duk_bitdecoder_ctx *bd_ctx) {
  11959. duk_small_int_t skip = 0;
  11960. duk_small_int_t n;
  11961. duk_small_int_t t;
  11962. duk_small_int_t count;
  11963. duk_codepoint_t tmp_cp;
  11964. duk_codepoint_t start_i;
  11965. duk_codepoint_t start_o;
  11966. DUK_DDD(DUK_DDDPRINT("slow case conversion for codepoint: %ld", (long) cp));
  11967. /* range conversion with a "skip" */
  11968. DUK_DDD(DUK_DDDPRINT("checking ranges"));
  11969. for (;;) {
  11970. skip++;
  11971. n = (duk_small_int_t) duk_bd_decode(bd_ctx, 6);
  11972. if (n == 0x3f) {
  11973. /* end marker */
  11974. break;
  11975. }
  11976. DUK_DDD(DUK_DDDPRINT("skip=%ld, n=%ld", (long) skip, (long) n));
  11977. while (n--) {
  11978. start_i = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  11979. start_o = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  11980. count = (duk_small_int_t) duk_bd_decode(bd_ctx, 7);
  11981. DUK_DDD(DUK_DDDPRINT("range: start_i=%ld, start_o=%ld, count=%ld, skip=%ld",
  11982. (long) start_i, (long) start_o, (long) count, (long) skip));
  11983. if (cp >= start_i) {
  11984. tmp_cp = cp - start_i; /* always >= 0 */
  11985. if (tmp_cp < (duk_codepoint_t) count * (duk_codepoint_t) skip &&
  11986. (tmp_cp % (duk_codepoint_t) skip) == 0) {
  11987. DUK_DDD(DUK_DDDPRINT("range matches input codepoint"));
  11988. cp = start_o + tmp_cp;
  11989. goto single;
  11990. }
  11991. }
  11992. }
  11993. }
  11994. /* 1:1 conversion */
  11995. n = (duk_small_int_t) duk_bd_decode(bd_ctx, 6);
  11996. DUK_DDD(DUK_DDDPRINT("checking 1:1 conversions (count %ld)", (long) n));
  11997. while (n--) {
  11998. start_i = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  11999. start_o = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  12000. DUK_DDD(DUK_DDDPRINT("1:1 conversion %ld -> %ld", (long) start_i, (long) start_o));
  12001. if (cp == start_i) {
  12002. DUK_DDD(DUK_DDDPRINT("1:1 matches input codepoint"));
  12003. cp = start_o;
  12004. goto single;
  12005. }
  12006. }
  12007. /* complex, multicharacter conversion */
  12008. n = (duk_small_int_t) duk_bd_decode(bd_ctx, 7);
  12009. DUK_DDD(DUK_DDDPRINT("checking 1:n conversions (count %ld)", (long) n));
  12010. while (n--) {
  12011. start_i = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  12012. t = (duk_small_int_t) duk_bd_decode(bd_ctx, 2);
  12013. DUK_DDD(DUK_DDDPRINT("1:n conversion %ld -> %ld chars", (long) start_i, (long) t));
  12014. if (cp == start_i) {
  12015. DUK_DDD(DUK_DDDPRINT("1:n matches input codepoint"));
  12016. if (buf) {
  12017. while (t--) {
  12018. tmp_cp = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  12019. DUK_ASSERT(buf != NULL);
  12020. duk_hbuffer_append_xutf8(thr, buf, (duk_ucodepoint_t) tmp_cp);
  12021. }
  12022. }
  12023. return -1;
  12024. } else {
  12025. while (t--) {
  12026. (void) duk_bd_decode(bd_ctx, 16);
  12027. }
  12028. }
  12029. }
  12030. /* default: no change */
  12031. DUK_DDD(DUK_DDDPRINT("no rule matches, output is same as input"));
  12032. /* fall through */
  12033. single:
  12034. if (buf) {
  12035. duk_hbuffer_append_xutf8(thr, buf, cp);
  12036. }
  12037. return cp;
  12038. }
  12039. /*
  12040. * Case conversion helper, with context/local sensitivity.
  12041. * For proper case conversion, one needs to know the character
  12042. * and the preceding and following characters, as well as
  12043. * locale/language.
  12044. */
  12045. /* XXX: add 'language' argument when locale/language sensitive rule
  12046. * support added.
  12047. */
  12048. DUK_LOCAL
  12049. duk_codepoint_t duk__case_transform_helper(duk_hthread *thr,
  12050. duk_hbuffer_dynamic *buf,
  12051. duk_codepoint_t cp,
  12052. duk_codepoint_t prev,
  12053. duk_codepoint_t next,
  12054. duk_bool_t uppercase) {
  12055. duk_bitdecoder_ctx bd_ctx;
  12056. /* fast path for ASCII */
  12057. if (cp < 0x80L) {
  12058. /* XXX: there are language sensitive rules for the ASCII range.
  12059. * If/when language/locale support is implemented, they need to
  12060. * be implemented here for the fast path. There are no context
  12061. * sensitive rules for ASCII range.
  12062. */
  12063. if (uppercase) {
  12064. if (cp >= 'a' && cp <= 'z') {
  12065. cp = cp - 'a' + 'A';
  12066. }
  12067. } else {
  12068. if (cp >= 'A' && cp <= 'Z') {
  12069. cp = cp - 'A' + 'a';
  12070. }
  12071. }
  12072. goto singlechar;
  12073. }
  12074. /* context and locale specific rules which cannot currently be represented
  12075. * in the caseconv bitstream: hardcoded rules in C
  12076. */
  12077. if (uppercase) {
  12078. /* XXX: turkish / azeri */
  12079. } else {
  12080. /*
  12081. * Final sigma context specific rule. This is a rather tricky
  12082. * rule and this handling is probably not 100% correct now.
  12083. * The rule is not locale/language specific so it is supported.
  12084. */
  12085. if (cp == 0x03a3L && /* U+03A3 = GREEK CAPITAL LETTER SIGMA */
  12086. duk_unicode_is_letter(prev) && /* prev exists and is not a letter */
  12087. !duk_unicode_is_letter(next)) { /* next does not exist or next is not a letter */
  12088. /* Capital sigma occurred at "end of word", lowercase to
  12089. * U+03C2 = GREEK SMALL LETTER FINAL SIGMA. Otherwise
  12090. * fall through and let the normal rules lowercase it to
  12091. * U+03C3 = GREEK SMALL LETTER SIGMA.
  12092. */
  12093. cp = 0x03c2L;
  12094. goto singlechar;
  12095. }
  12096. /* XXX: lithuanian not implemented */
  12097. /* XXX: lithuanian, explicit dot rules */
  12098. /* XXX: turkish / azeri, lowercase rules */
  12099. }
  12100. /* 1:1 or special conversions, but not locale/context specific: script generated rules */
  12101. DUK_MEMZERO(&bd_ctx, sizeof(bd_ctx));
  12102. if (uppercase) {
  12103. bd_ctx.data = (duk_uint8_t *) duk_unicode_caseconv_uc;
  12104. bd_ctx.length = (duk_size_t) sizeof(duk_unicode_caseconv_uc);
  12105. } else {
  12106. bd_ctx.data = (duk_uint8_t *) duk_unicode_caseconv_lc;
  12107. bd_ctx.length = (duk_size_t) sizeof(duk_unicode_caseconv_lc);
  12108. }
  12109. return duk__slow_case_conversion(thr, buf, cp, &bd_ctx);
  12110. singlechar:
  12111. if (buf) {
  12112. duk_hbuffer_append_xutf8(thr, buf, cp);
  12113. }
  12114. return cp;
  12115. /* unused now, not needed until Turkish/Azeri */
  12116. #if 0
  12117. nochar:
  12118. return -1;
  12119. #endif
  12120. }
  12121. /*
  12122. * Replace valstack top with case converted version.
  12123. */
  12124. DUK_INTERNAL void duk_unicode_case_convert_string(duk_hthread *thr, duk_small_int_t uppercase) {
  12125. duk_context *ctx = (duk_context *) thr;
  12126. duk_hstring *h_input;
  12127. duk_hbuffer_dynamic *h_buf;
  12128. const duk_uint8_t *p, *p_start, *p_end;
  12129. duk_codepoint_t prev, curr, next;
  12130. h_input = duk_require_hstring(ctx, -1);
  12131. DUK_ASSERT(h_input != NULL);
  12132. /* XXX: should init the buffer with a spare of at least h_input->blen
  12133. * to avoid unnecessary growth steps.
  12134. */
  12135. duk_push_dynamic_buffer(ctx, 0);
  12136. h_buf = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, -1);
  12137. DUK_ASSERT(h_buf != NULL);
  12138. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(h_buf));
  12139. /* [ ... input buffer ] */
  12140. p_start = (duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
  12141. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  12142. p = p_start;
  12143. prev = -1; DUK_UNREF(prev);
  12144. curr = -1;
  12145. next = -1;
  12146. for (;;) {
  12147. prev = curr;
  12148. curr = next;
  12149. next = -1;
  12150. if (p < p_end) {
  12151. next = (int) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end);
  12152. } else {
  12153. /* end of input and last char has been processed */
  12154. if (curr < 0) {
  12155. break;
  12156. }
  12157. }
  12158. /* on first round, skip */
  12159. if (curr >= 0) {
  12160. /* may generate any number of output codepoints */
  12161. duk__case_transform_helper(thr,
  12162. h_buf,
  12163. (duk_codepoint_t) curr,
  12164. prev,
  12165. next,
  12166. uppercase);
  12167. }
  12168. }
  12169. duk_to_string(ctx, -1); /* invalidates h_buf pointer */
  12170. duk_remove(ctx, -2);
  12171. }
  12172. #ifdef DUK_USE_REGEXP_SUPPORT
  12173. /*
  12174. * Canonicalize() abstract operation needed for canonicalization of individual
  12175. * codepoints during regexp compilation and execution, see E5 Section 15.10.2.8.
  12176. * Note that codepoints are canonicalized one character at a time, so no context
  12177. * specific rules can apply. Locale specific rules can apply, though.
  12178. */
  12179. DUK_INTERNAL duk_codepoint_t duk_unicode_re_canonicalize_char(duk_hthread *thr, duk_codepoint_t cp) {
  12180. duk_codepoint_t y;
  12181. y = duk__case_transform_helper(thr,
  12182. NULL, /* buf */
  12183. cp, /* curr char */
  12184. -1, /* prev char */
  12185. -1, /* next char */
  12186. 1); /* uppercase */
  12187. if ((y < 0) || (cp >= 0x80 && y < 0x80)) {
  12188. /* multiple codepoint conversion or non-ASCII mapped to ASCII
  12189. * --> leave as is.
  12190. */
  12191. return cp;
  12192. }
  12193. return y;
  12194. }
  12195. /*
  12196. * E5 Section 15.10.2.6 "IsWordChar" abstract operation. Assume
  12197. * x < 0 for characters read outside the string.
  12198. */
  12199. DUK_INTERNAL duk_small_int_t duk_unicode_re_is_wordchar(duk_codepoint_t x) {
  12200. /*
  12201. * Note: the description in E5 Section 15.10.2.6 has a typo, it
  12202. * contains 'A' twice and lacks 'a'; the intent is [0-9a-zA-Z_].
  12203. */
  12204. if ((x >= '0' && x <= '9') ||
  12205. (x >= 'a' && x <= 'z') ||
  12206. (x >= 'A' && x <= 'Z') ||
  12207. (x == '_')) {
  12208. return 1;
  12209. }
  12210. return 0;
  12211. }
  12212. /*
  12213. * Regexp range tables
  12214. */
  12215. /* exposed because lexer needs these too */
  12216. DUK_INTERNAL duk_uint16_t duk_unicode_re_ranges_digit[2] = {
  12217. (duk_uint16_t) 0x0030UL, (duk_uint16_t) 0x0039UL,
  12218. };
  12219. DUK_INTERNAL duk_uint16_t duk_unicode_re_ranges_white[22] = {
  12220. (duk_uint16_t) 0x0009UL, (duk_uint16_t) 0x000DUL,
  12221. (duk_uint16_t) 0x0020UL, (duk_uint16_t) 0x0020UL,
  12222. (duk_uint16_t) 0x00A0UL, (duk_uint16_t) 0x00A0UL,
  12223. (duk_uint16_t) 0x1680UL, (duk_uint16_t) 0x1680UL,
  12224. (duk_uint16_t) 0x180EUL, (duk_uint16_t) 0x180EUL,
  12225. (duk_uint16_t) 0x2000UL, (duk_uint16_t) 0x200AUL,
  12226. (duk_uint16_t) 0x2028UL, (duk_uint16_t) 0x2029UL,
  12227. (duk_uint16_t) 0x202FUL, (duk_uint16_t) 0x202FUL,
  12228. (duk_uint16_t) 0x205FUL, (duk_uint16_t) 0x205FUL,
  12229. (duk_uint16_t) 0x3000UL, (duk_uint16_t) 0x3000UL,
  12230. (duk_uint16_t) 0xFEFFUL, (duk_uint16_t) 0xFEFFUL,
  12231. };
  12232. DUK_INTERNAL duk_uint16_t duk_unicode_re_ranges_wordchar[8] = {
  12233. (duk_uint16_t) 0x0030UL, (duk_uint16_t) 0x0039UL,
  12234. (duk_uint16_t) 0x0041UL, (duk_uint16_t) 0x005AUL,
  12235. (duk_uint16_t) 0x005FUL, (duk_uint16_t) 0x005FUL,
  12236. (duk_uint16_t) 0x0061UL, (duk_uint16_t) 0x007AUL,
  12237. };
  12238. DUK_INTERNAL duk_uint16_t duk_unicode_re_ranges_not_digit[4] = {
  12239. (duk_uint16_t) 0x0000UL, (duk_uint16_t) 0x002FUL,
  12240. (duk_uint16_t) 0x003AUL, (duk_uint16_t) 0xFFFFUL,
  12241. };
  12242. DUK_INTERNAL duk_uint16_t duk_unicode_re_ranges_not_white[24] = {
  12243. (duk_uint16_t) 0x0000UL, (duk_uint16_t) 0x0008UL,
  12244. (duk_uint16_t) 0x000EUL, (duk_uint16_t) 0x001FUL,
  12245. (duk_uint16_t) 0x0021UL, (duk_uint16_t) 0x009FUL,
  12246. (duk_uint16_t) 0x00A1UL, (duk_uint16_t) 0x167FUL,
  12247. (duk_uint16_t) 0x1681UL, (duk_uint16_t) 0x180DUL,
  12248. (duk_uint16_t) 0x180FUL, (duk_uint16_t) 0x1FFFUL,
  12249. (duk_uint16_t) 0x200BUL, (duk_uint16_t) 0x2027UL,
  12250. (duk_uint16_t) 0x202AUL, (duk_uint16_t) 0x202EUL,
  12251. (duk_uint16_t) 0x2030UL, (duk_uint16_t) 0x205EUL,
  12252. (duk_uint16_t) 0x2060UL, (duk_uint16_t) 0x2FFFUL,
  12253. (duk_uint16_t) 0x3001UL, (duk_uint16_t) 0xFEFEUL,
  12254. (duk_uint16_t) 0xFF00UL, (duk_uint16_t) 0xFFFFUL,
  12255. };
  12256. DUK_INTERNAL duk_uint16_t duk_unicode_re_ranges_not_wordchar[10] = {
  12257. (duk_uint16_t) 0x0000UL, (duk_uint16_t) 0x002FUL,
  12258. (duk_uint16_t) 0x003AUL, (duk_uint16_t) 0x0040UL,
  12259. (duk_uint16_t) 0x005BUL, (duk_uint16_t) 0x005EUL,
  12260. (duk_uint16_t) 0x0060UL, (duk_uint16_t) 0x0060UL,
  12261. (duk_uint16_t) 0x007BUL, (duk_uint16_t) 0xFFFFUL,
  12262. };
  12263. #endif /* DUK_USE_REGEXP_SUPPORT */
  12264. #line 1 "duk_util_misc.c"
  12265. /*
  12266. * Misc util stuff
  12267. */
  12268. /* include removed: duk_internal.h */
  12269. /*
  12270. * Lowercase digits for radix values 2 to 36. Also doubles as lowercase
  12271. * hex nybble table.
  12272. */
  12273. DUK_INTERNAL duk_uint8_t duk_lc_digits[36] = {
  12274. '0', '1', '2', '3', '4', '5', '6', '7',
  12275. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
  12276. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  12277. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  12278. 'w', 'x', 'y', 'z'
  12279. };
  12280. DUK_INTERNAL duk_uint8_t duk_uc_nybbles[16] = {
  12281. '0', '1', '2', '3', '4', '5', '6', '7',
  12282. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  12283. };
  12284. /*
  12285. * Table for decoding ASCII hex digits, -1 if invalid.
  12286. */
  12287. DUK_INTERNAL duk_int8_t duk_hex_dectab[256] = {
  12288. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0f */
  12289. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1f */
  12290. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20-0x2f */
  12291. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0x30-0x3f */
  12292. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40-0x4f */
  12293. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5f */
  12294. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x60-0x6f */
  12295. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x70-0x7f */
  12296. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x80-0x8f */
  12297. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x90-0x9f */
  12298. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xa0-0xaf */
  12299. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xb0-0xbf */
  12300. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xc0-0xcf */
  12301. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xd0-0xdf */
  12302. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xe0-0xef */
  12303. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 0xf0-0xff */
  12304. };
  12305. /*
  12306. * Arbitrary byteswap for potentially unaligned values
  12307. *
  12308. * Used to byteswap pointers e.g. in debugger code.
  12309. */
  12310. #if defined(DUK_USE_DEBUGGER_SUPPORT) /* For now only needed by the debugger. */
  12311. DUK_INTERNAL void duk_byteswap_bytes(duk_uint8_t *p, duk_small_uint_t len) {
  12312. duk_uint8_t tmp;
  12313. duk_uint8_t *q = p + len - 1;
  12314. while (p - q < 0) {
  12315. tmp = *p;
  12316. *p = *q;
  12317. *q = tmp;
  12318. p++;
  12319. q--;
  12320. }
  12321. }
  12322. #endif
  12323. #line 1 "duk_util_hashprime.c"
  12324. /*
  12325. * Round a number upwards to a prime (not usually the nearest one).
  12326. *
  12327. * Uses a table of successive 32-bit primes whose ratio is roughly
  12328. * constant. This keeps the relative upwards 'rounding error' bounded
  12329. * and the data size small. A simple 'predict-correct' compression is
  12330. * used to compress primes to one byte per prime. See genhashsizes.py
  12331. * for details.
  12332. *
  12333. * The minimum prime returned here must be coordinated with the possible
  12334. * probe sequence steps in duk_hobject and duk_heap stringtable.
  12335. */
  12336. /* include removed: duk_internal.h */
  12337. /* Awkward inclusion condition: drop out of compilation if not needed by any
  12338. * call site: object hash part or probing stringtable.
  12339. */
  12340. #if defined(DUK_USE_HOBJECT_HASH_PART) || defined(DUK_USE_STRTAB_PROBE)
  12341. /* hash size ratio goal, must match genhashsizes.py */
  12342. #define DUK__HASH_SIZE_RATIO 1177 /* floor(1.15 * (1 << 10)) */
  12343. /* prediction corrections for prime list (see genhashsizes.py) */
  12344. DUK_LOCAL const duk_int8_t duk__hash_size_corrections[] = {
  12345. 17, /* minimum prime */
  12346. 4, 3, 4, 1, 4, 1, 1, 2, 2, 2, 2, 1, 6, 6, 9, 5, 1, 2, 2, 5, 1, 3, 3, 3,
  12347. 5, 4, 4, 2, 4, 8, 3, 4, 23, 2, 4, 7, 8, 11, 2, 12, 15, 10, 1, 1, 5, 1, 5,
  12348. 8, 9, 17, 14, 10, 7, 5, 2, 46, 21, 1, 9, 9, 4, 4, 10, 23, 36, 6, 20, 29,
  12349. 18, 6, 19, 21, 16, 11, 5, 5, 48, 9, 1, 39, 14, 8, 4, 29, 9, 1, 15, 48, 12,
  12350. 22, 6, 15, 27, 4, 2, 17, 28, 8, 9, 4, 5, 8, 3, 3, 8, 37, 11, 15, 8, 30,
  12351. 43, 6, 33, 41, 5, 20, 32, 41, 38, 24, 77, 14, 19, 11, 4, 35, 18, 19, 41,
  12352. 10, 23, 16, 9, 2,
  12353. -1
  12354. };
  12355. /* probe steps (see genhashsizes.py), currently assumed to be 32 entries long
  12356. * (DUK_UTIL_GET_HASH_PROBE_STEP macro).
  12357. */
  12358. DUK_INTERNAL duk_uint8_t duk_util_probe_steps[32] = {
  12359. 2, 3, 5, 7, 11, 13, 19, 31, 41, 47, 59, 67, 73, 79, 89, 101, 103, 107,
  12360. 109, 127, 137, 139, 149, 157, 163, 167, 173, 181, 191, 193, 197, 199
  12361. };
  12362. DUK_INTERNAL duk_uint32_t duk_util_get_hash_prime(duk_uint32_t size) {
  12363. const duk_int8_t *p = duk__hash_size_corrections;
  12364. duk_uint32_t curr;
  12365. curr = (duk_uint32_t) *p++;
  12366. for (;;) {
  12367. duk_small_int_t t = (duk_small_int_t) *p++;
  12368. if (t < 0) {
  12369. /* may happen if size is very close to 2^32-1 */
  12370. break;
  12371. }
  12372. /* prediction: portable variant using doubles if 64-bit values not available */
  12373. #ifdef DUK_USE_64BIT_OPS
  12374. curr = (duk_uint32_t) ((((duk_uint64_t) curr) * ((duk_uint64_t) DUK__HASH_SIZE_RATIO)) >> 10);
  12375. #else
  12376. /* 32-bit x 11-bit = 43-bit, fits accurately into a double */
  12377. curr = (duk_uint32_t) DUK_FLOOR(((double) curr) * ((double) DUK__HASH_SIZE_RATIO) / 1024.0);
  12378. #endif
  12379. /* correction */
  12380. curr += t;
  12381. DUK_DDD(DUK_DDDPRINT("size=%ld, curr=%ld", (long) size, (long) curr));
  12382. if (curr >= size) {
  12383. return curr;
  12384. }
  12385. }
  12386. return 0;
  12387. }
  12388. #endif /* DUK_USE_HOBJECT_HASH_PART || DUK_USE_STRTAB_PROBE */
  12389. #line 1 "duk_hobject_class.c"
  12390. /*
  12391. * Hobject Ecmascript [[Class]].
  12392. */
  12393. /* include removed: duk_internal.h */
  12394. #if (DUK_STRIDX_UC_ARGUMENTS > 255)
  12395. #error constant too large
  12396. #endif
  12397. #if (DUK_STRIDX_ARRAY > 255)
  12398. #error constant too large
  12399. #endif
  12400. #if (DUK_STRIDX_UC_BOOLEAN > 255)
  12401. #error constant too large
  12402. #endif
  12403. #if (DUK_STRIDX_DATE > 255)
  12404. #error constant too large
  12405. #endif
  12406. #if (DUK_STRIDX_UC_ERROR > 255)
  12407. #error constant too large
  12408. #endif
  12409. #if (DUK_STRIDX_UC_FUNCTION > 255)
  12410. #error constant too large
  12411. #endif
  12412. #if (DUK_STRIDX_JSON > 255)
  12413. #error constant too large
  12414. #endif
  12415. #if (DUK_STRIDX_MATH > 255)
  12416. #error constant too large
  12417. #endif
  12418. #if (DUK_STRIDX_UC_NUMBER > 255)
  12419. #error constant too large
  12420. #endif
  12421. #if (DUK_STRIDX_UC_OBJECT > 255)
  12422. #error constant too large
  12423. #endif
  12424. #if (DUK_STRIDX_REG_EXP > 255)
  12425. #error constant too large
  12426. #endif
  12427. #if (DUK_STRIDX_UC_STRING > 255)
  12428. #error constant too large
  12429. #endif
  12430. #if (DUK_STRIDX_GLOBAL > 255)
  12431. #error constant too large
  12432. #endif
  12433. #if (DUK_STRIDX_OBJ_ENV > 255)
  12434. #error constant too large
  12435. #endif
  12436. #if (DUK_STRIDX_DEC_ENV > 255)
  12437. #error constant too large
  12438. #endif
  12439. #if (DUK_STRIDX_UC_BUFFER > 255)
  12440. #error constant too large
  12441. #endif
  12442. #if (DUK_STRIDX_UC_POINTER > 255)
  12443. #error constant too large
  12444. #endif
  12445. #if (DUK_STRIDX_UC_THREAD > 255)
  12446. #error constant too large
  12447. #endif
  12448. #if (DUK_STRIDX_ARRAY_BUFFER > 255)
  12449. #error constant too large
  12450. #endif
  12451. #if (DUK_STRIDX_DATA_VIEW > 255)
  12452. #error constant too large
  12453. #endif
  12454. #if (DUK_STRIDX_INT8_ARRAY > 255)
  12455. #error constant too large
  12456. #endif
  12457. #if (DUK_STRIDX_UINT8_ARRAY > 255)
  12458. #error constant too large
  12459. #endif
  12460. #if (DUK_STRIDX_UINT8_CLAMPED_ARRAY > 255)
  12461. #error constant too large
  12462. #endif
  12463. #if (DUK_STRIDX_INT16_ARRAY > 255)
  12464. #error constant too large
  12465. #endif
  12466. #if (DUK_STRIDX_UINT16_ARRAY > 255)
  12467. #error constant too large
  12468. #endif
  12469. #if (DUK_STRIDX_INT32_ARRAY > 255)
  12470. #error constant too large
  12471. #endif
  12472. #if (DUK_STRIDX_UINT32_ARRAY > 255)
  12473. #error constant too large
  12474. #endif
  12475. #if (DUK_STRIDX_FLOAT32_ARRAY > 255)
  12476. #error constant too large
  12477. #endif
  12478. #if (DUK_STRIDX_FLOAT64_ARRAY > 255)
  12479. #error constant too large
  12480. #endif
  12481. #if (DUK_STRIDX_EMPTY_STRING > 255)
  12482. #error constant too large
  12483. #endif
  12484. /* Note: assumes that these string indexes are 8-bit, genstrings.py must ensure that */
  12485. DUK_INTERNAL duk_uint8_t duk_class_number_to_stridx[32] = {
  12486. DUK_STRIDX_EMPTY_STRING, /* UNUSED, intentionally empty */
  12487. DUK_STRIDX_UC_ARGUMENTS,
  12488. DUK_STRIDX_ARRAY,
  12489. DUK_STRIDX_UC_BOOLEAN,
  12490. DUK_STRIDX_DATE,
  12491. DUK_STRIDX_UC_ERROR,
  12492. DUK_STRIDX_UC_FUNCTION,
  12493. DUK_STRIDX_JSON,
  12494. DUK_STRIDX_MATH,
  12495. DUK_STRIDX_UC_NUMBER,
  12496. DUK_STRIDX_UC_OBJECT,
  12497. DUK_STRIDX_REG_EXP,
  12498. DUK_STRIDX_UC_STRING,
  12499. DUK_STRIDX_GLOBAL,
  12500. DUK_STRIDX_OBJ_ENV,
  12501. DUK_STRIDX_DEC_ENV,
  12502. DUK_STRIDX_UC_BUFFER,
  12503. DUK_STRIDX_UC_POINTER,
  12504. DUK_STRIDX_UC_THREAD,
  12505. DUK_STRIDX_ARRAY_BUFFER,
  12506. DUK_STRIDX_DATA_VIEW,
  12507. DUK_STRIDX_INT8_ARRAY,
  12508. DUK_STRIDX_UINT8_ARRAY,
  12509. DUK_STRIDX_UINT8_CLAMPED_ARRAY,
  12510. DUK_STRIDX_INT16_ARRAY,
  12511. DUK_STRIDX_UINT16_ARRAY,
  12512. DUK_STRIDX_INT32_ARRAY,
  12513. DUK_STRIDX_UINT32_ARRAY,
  12514. DUK_STRIDX_FLOAT32_ARRAY,
  12515. DUK_STRIDX_FLOAT64_ARRAY,
  12516. DUK_STRIDX_EMPTY_STRING, /* UNUSED, intentionally empty */
  12517. DUK_STRIDX_EMPTY_STRING, /* UNUSED, intentionally empty */
  12518. };
  12519. #line 1 "duk_alloc_default.c"
  12520. /*
  12521. * Default allocation functions.
  12522. *
  12523. * Assumes behavior such as malloc allowing zero size, yielding
  12524. * a NULL or a unique pointer which is a no-op for free.
  12525. */
  12526. /* include removed: duk_internal.h */
  12527. DUK_INTERNAL void *duk_default_alloc_function(void *udata, duk_size_t size) {
  12528. void *res;
  12529. DUK_UNREF(udata);
  12530. res = DUK_ANSI_MALLOC(size);
  12531. DUK_DDD(DUK_DDDPRINT("default alloc function: %lu -> %p",
  12532. (unsigned long) size, (void *) res));
  12533. return res;
  12534. }
  12535. DUK_INTERNAL void *duk_default_realloc_function(void *udata, void *ptr, duk_size_t newsize) {
  12536. void *res;
  12537. DUK_UNREF(udata);
  12538. res = DUK_ANSI_REALLOC(ptr, newsize);
  12539. DUK_DDD(DUK_DDDPRINT("default realloc function: %p %lu -> %p",
  12540. (void *) ptr, (unsigned long) newsize, (void *) res));
  12541. return res;
  12542. }
  12543. DUK_INTERNAL void duk_default_free_function(void *udata, void *ptr) {
  12544. DUK_DDD(DUK_DDDPRINT("default free function: %p", (void *) ptr));
  12545. DUK_UNREF(udata);
  12546. DUK_ANSI_FREE(ptr);
  12547. }
  12548. #line 1 "duk_api_buffer.c"
  12549. /*
  12550. * Buffer
  12551. */
  12552. /* include removed: duk_internal.h */
  12553. DUK_EXTERNAL void *duk_resize_buffer(duk_context *ctx, duk_idx_t index, duk_size_t new_size) {
  12554. duk_hthread *thr = (duk_hthread *) ctx;
  12555. duk_hbuffer_dynamic *h;
  12556. DUK_ASSERT_CTX_VALID(ctx);
  12557. h = (duk_hbuffer_dynamic *) duk_require_hbuffer(ctx, index);
  12558. DUK_ASSERT(h != NULL);
  12559. if (!DUK_HBUFFER_HAS_DYNAMIC(h)) {
  12560. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_BUFFER_NOT_DYNAMIC);
  12561. }
  12562. /* maximum size check is handled by callee */
  12563. duk_hbuffer_resize(thr, h, new_size, new_size); /* snug */
  12564. return DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h);
  12565. }
  12566. #line 1 "duk_api_call.c"
  12567. /*
  12568. * Calls.
  12569. *
  12570. * Protected variants should avoid ever throwing an error.
  12571. */
  12572. /* include removed: duk_internal.h */
  12573. /* Prepare value stack for a method call through an object property.
  12574. * May currently throw an error e.g. when getting the property.
  12575. */
  12576. DUK_LOCAL void duk__call_prop_prep_stack(duk_context *ctx, duk_idx_t normalized_obj_index, duk_idx_t nargs) {
  12577. DUK_ASSERT_CTX_VALID(ctx);
  12578. DUK_DDD(DUK_DDDPRINT("duk__call_prop_prep_stack, normalized_obj_index=%ld, nargs=%ld, stacktop=%ld",
  12579. (long) normalized_obj_index, (long) nargs, (long) duk_get_top(ctx)));
  12580. /* [... key arg1 ... argN] */
  12581. /* duplicate key */
  12582. duk_dup(ctx, -nargs - 1); /* Note: -nargs alone would fail for nargs == 0, this is OK */
  12583. duk_get_prop(ctx, normalized_obj_index);
  12584. DUK_DDD(DUK_DDDPRINT("func: %!T", (duk_tval *) duk_get_tval(ctx, -1)));
  12585. /* [... key arg1 ... argN func] */
  12586. duk_replace(ctx, -nargs - 2);
  12587. /* [... func arg1 ... argN] */
  12588. duk_dup(ctx, normalized_obj_index);
  12589. duk_insert(ctx, -nargs - 1);
  12590. /* [... func this arg1 ... argN] */
  12591. }
  12592. DUK_EXTERNAL void duk_call(duk_context *ctx, duk_idx_t nargs) {
  12593. duk_hthread *thr = (duk_hthread *) ctx;
  12594. duk_small_uint_t call_flags;
  12595. duk_idx_t idx_func;
  12596. duk_int_t rc;
  12597. DUK_ASSERT_CTX_VALID(ctx);
  12598. DUK_ASSERT(thr != NULL);
  12599. idx_func = duk_get_top(ctx) - nargs - 1;
  12600. if (idx_func < 0 || nargs < 0) {
  12601. /* note that we can't reliably pop anything here */
  12602. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  12603. }
  12604. /* XXX: awkward; we assume there is space for this, overwrite
  12605. * directly instead?
  12606. */
  12607. duk_push_undefined(ctx);
  12608. duk_insert(ctx, idx_func + 1);
  12609. call_flags = 0; /* not protected, respect reclimit, not constructor */
  12610. rc = duk_handle_call(thr, /* thread */
  12611. nargs, /* num_stack_args */
  12612. call_flags); /* call_flags */
  12613. DUK_UNREF(rc);
  12614. }
  12615. DUK_EXTERNAL void duk_call_method(duk_context *ctx, duk_idx_t nargs) {
  12616. duk_hthread *thr = (duk_hthread *) ctx;
  12617. duk_small_uint_t call_flags;
  12618. duk_idx_t idx_func;
  12619. duk_int_t rc;
  12620. DUK_ASSERT_CTX_VALID(ctx);
  12621. DUK_ASSERT(thr != NULL);
  12622. idx_func = duk_get_top(ctx) - nargs - 2; /* must work for nargs <= 0 */
  12623. if (idx_func < 0 || nargs < 0) {
  12624. /* note that we can't reliably pop anything here */
  12625. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  12626. }
  12627. call_flags = 0; /* not protected, respect reclimit, not constructor */
  12628. rc = duk_handle_call(thr, /* thread */
  12629. nargs, /* num_stack_args */
  12630. call_flags); /* call_flags */
  12631. DUK_UNREF(rc);
  12632. }
  12633. DUK_EXTERNAL void duk_call_prop(duk_context *ctx, duk_idx_t obj_index, duk_idx_t nargs) {
  12634. /*
  12635. * XXX: if duk_handle_call() took values through indices, this could be
  12636. * made much more sensible. However, duk_handle_call() needs to fudge
  12637. * the 'this' and 'func' values to handle bound function chains, which
  12638. * is now done "in-place", so this is not a trivial change.
  12639. */
  12640. DUK_ASSERT_CTX_VALID(ctx);
  12641. obj_index = duk_require_normalize_index(ctx, obj_index); /* make absolute */
  12642. duk__call_prop_prep_stack(ctx, obj_index, nargs);
  12643. duk_call_method(ctx, nargs);
  12644. }
  12645. DUK_EXTERNAL duk_int_t duk_pcall(duk_context *ctx, duk_idx_t nargs) {
  12646. duk_hthread *thr = (duk_hthread *) ctx;
  12647. duk_small_uint_t call_flags;
  12648. duk_idx_t idx_func;
  12649. duk_int_t rc;
  12650. DUK_ASSERT_CTX_VALID(ctx);
  12651. DUK_ASSERT(thr != NULL);
  12652. idx_func = duk_get_top(ctx) - nargs - 1; /* must work for nargs <= 0 */
  12653. if (idx_func < 0 || nargs < 0) {
  12654. /* We can't reliably pop anything here because the stack input
  12655. * shape is incorrect. So we throw an error; if the caller has
  12656. * no catch point for this, a fatal error will occur. Another
  12657. * alternative would be to just return an error. But then the
  12658. * stack would be in an unknown state which might cause some
  12659. * very hard to diagnose problems later on. Also note that even
  12660. * if we did not throw an error here, the underlying call handler
  12661. * might STILL throw an out-of-memory error or some other internal
  12662. * fatal error.
  12663. */
  12664. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  12665. return DUK_EXEC_ERROR; /* unreachable */
  12666. }
  12667. /* awkward; we assume there is space for this */
  12668. duk_push_undefined(ctx);
  12669. duk_insert(ctx, idx_func + 1);
  12670. call_flags = DUK_CALL_FLAG_PROTECTED; /* protected, respect reclimit, not constructor */
  12671. rc = duk_handle_call(thr, /* thread */
  12672. nargs, /* num_stack_args */
  12673. call_flags); /* call_flags */
  12674. return rc;
  12675. }
  12676. DUK_EXTERNAL duk_int_t duk_pcall_method(duk_context *ctx, duk_idx_t nargs) {
  12677. duk_hthread *thr = (duk_hthread *) ctx;
  12678. duk_small_uint_t call_flags;
  12679. duk_idx_t idx_func;
  12680. duk_int_t rc;
  12681. DUK_ASSERT_CTX_VALID(ctx);
  12682. DUK_ASSERT(thr != NULL);
  12683. idx_func = duk_get_top(ctx) - nargs - 2; /* must work for nargs <= 0 */
  12684. if (idx_func < 0 || nargs < 0) {
  12685. /* See comments in duk_pcall(). */
  12686. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  12687. return DUK_EXEC_ERROR; /* unreachable */
  12688. }
  12689. call_flags = DUK_CALL_FLAG_PROTECTED; /* protected, respect reclimit, not constructor */
  12690. rc = duk_handle_call(thr, /* thread */
  12691. nargs, /* num_stack_args */
  12692. call_flags); /* call_flags */
  12693. return rc;
  12694. }
  12695. DUK_LOCAL duk_ret_t duk__pcall_prop_raw(duk_context *ctx) {
  12696. duk_idx_t obj_index;
  12697. duk_idx_t nargs;
  12698. /* Get the original arguments. Note that obj_index may be a relative
  12699. * index so the stack must have the same top when we use it.
  12700. */
  12701. DUK_ASSERT_CTX_VALID(ctx);
  12702. obj_index = (duk_idx_t) duk_get_int(ctx, -2);
  12703. nargs = (duk_idx_t) duk_get_int(ctx, -1);
  12704. duk_pop_2(ctx);
  12705. obj_index = duk_require_normalize_index(ctx, obj_index); /* make absolute */
  12706. duk__call_prop_prep_stack(ctx, obj_index, nargs);
  12707. duk_call_method(ctx, nargs);
  12708. return 1;
  12709. }
  12710. DUK_EXTERNAL duk_int_t duk_pcall_prop(duk_context *ctx, duk_idx_t obj_index, duk_idx_t nargs) {
  12711. /*
  12712. * Must be careful to catch errors related to value stack manipulation
  12713. * and property lookup, not just the call itself.
  12714. */
  12715. DUK_ASSERT_CTX_VALID(ctx);
  12716. duk_push_idx(ctx, obj_index);
  12717. duk_push_idx(ctx, nargs);
  12718. /* Inputs: explicit arguments (nargs), +1 for key, +2 for obj_index/nargs passing.
  12719. * If the value stack does not contain enough args, an error is thrown; this matches
  12720. * behavior of the other protected call API functions.
  12721. */
  12722. return duk_safe_call(ctx, duk__pcall_prop_raw, nargs + 1 + 2 /*nargs*/, 1 /*nrets*/);
  12723. }
  12724. DUK_EXTERNAL duk_int_t duk_safe_call(duk_context *ctx, duk_safe_call_function func, duk_idx_t nargs, duk_idx_t nrets) {
  12725. duk_hthread *thr = (duk_hthread *) ctx;
  12726. duk_int_t rc;
  12727. DUK_ASSERT_CTX_VALID(ctx);
  12728. DUK_ASSERT(thr != NULL);
  12729. if (duk_get_top(ctx) < nargs || nrets < 0) {
  12730. /* See comments in duk_pcall(). */
  12731. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  12732. return DUK_EXEC_ERROR; /* unreachable */
  12733. }
  12734. rc = duk_handle_safe_call(thr, /* thread */
  12735. func, /* func */
  12736. nargs, /* num_stack_args */
  12737. nrets); /* num_stack_res */
  12738. return rc;
  12739. }
  12740. DUK_EXTERNAL void duk_new(duk_context *ctx, duk_idx_t nargs) {
  12741. /*
  12742. * There are two [[Construct]] operations in the specification:
  12743. *
  12744. * - E5 Section 13.2.2: for Function objects
  12745. * - E5 Section 15.3.4.5.2: for "bound" Function objects
  12746. *
  12747. * The chain of bound functions is resolved in Section 15.3.4.5.2,
  12748. * with arguments "piling up" until the [[Construct]] internal
  12749. * method is called on the final, actual Function object. Note
  12750. * that the "prototype" property is looked up *only* from the
  12751. * final object, *before* calling the constructor.
  12752. *
  12753. * Currently we follow the bound function chain here to get the
  12754. * "prototype" property value from the final, non-bound function.
  12755. * However, we let duk_handle_call() handle the argument "piling"
  12756. * when the constructor is called. The bound function chain is
  12757. * thus now processed twice.
  12758. *
  12759. * When constructing new Array instances, an unnecessary object is
  12760. * created and discarded now: the standard [[Construct]] creates an
  12761. * object, and calls the Array constructor. The Array constructor
  12762. * returns an Array instance, which is used as the result value for
  12763. * the "new" operation; the object created before the Array constructor
  12764. * call is discarded.
  12765. *
  12766. * This would be easy to fix, e.g. by knowing that the Array constructor
  12767. * will always create a replacement object and skip creating the fallback
  12768. * object in that case.
  12769. *
  12770. * Note: functions called via "new" need to know they are called as a
  12771. * constructor. For instance, built-in constructors behave differently
  12772. * depending on how they are called.
  12773. */
  12774. /* XXX: merge this with duk_js_call.c, as this function implements
  12775. * core semantics (or perhaps merge the two files altogether).
  12776. */
  12777. duk_hthread *thr = (duk_hthread *) ctx;
  12778. duk_hobject *proto;
  12779. duk_hobject *cons;
  12780. duk_hobject *fallback;
  12781. duk_idx_t idx_cons;
  12782. duk_small_uint_t call_flags;
  12783. duk_int_t rc;
  12784. DUK_ASSERT_CTX_VALID(ctx);
  12785. /* [... constructor arg1 ... argN] */
  12786. idx_cons = duk_require_normalize_index(ctx, -nargs - 1);
  12787. DUK_DDD(DUK_DDDPRINT("top=%ld, nargs=%ld, idx_cons=%ld",
  12788. (long) duk_get_top(ctx), (long) nargs, (long) idx_cons));
  12789. /* XXX: code duplication */
  12790. /*
  12791. * Figure out the final, non-bound constructor, to get "prototype"
  12792. * property.
  12793. */
  12794. duk_dup(ctx, idx_cons);
  12795. for (;;) {
  12796. cons = duk_get_hobject(ctx, -1);
  12797. if (cons == NULL || !DUK_HOBJECT_HAS_CONSTRUCTABLE(cons)) {
  12798. /* Checking constructability from anything else than the
  12799. * initial constructor is not strictly necessary, but a
  12800. * nice sanity check.
  12801. */
  12802. goto not_constructable;
  12803. }
  12804. if (!DUK_HOBJECT_HAS_BOUND(cons)) {
  12805. break;
  12806. }
  12807. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_TARGET); /* -> [... cons target] */
  12808. duk_remove(ctx, -2); /* -> [... target] */
  12809. }
  12810. DUK_ASSERT(cons != NULL && !DUK_HOBJECT_HAS_BOUND(cons));
  12811. /* [... constructor arg1 ... argN final_cons] */
  12812. /*
  12813. * Create "fallback" object to be used as the object instance,
  12814. * unless the constructor returns a replacement value.
  12815. * Its internal prototype needs to be set based on "prototype"
  12816. * property of the constructor.
  12817. */
  12818. duk_push_object(ctx); /* class Object, extensible */
  12819. /* [... constructor arg1 ... argN final_cons fallback] */
  12820. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_PROTOTYPE);
  12821. proto = duk_get_hobject(ctx, -1);
  12822. if (!proto) {
  12823. DUK_DDD(DUK_DDDPRINT("constructor has no 'prototype' property, or value not an object "
  12824. "-> leave standard Object prototype as fallback prototype"));
  12825. } else {
  12826. DUK_DDD(DUK_DDDPRINT("constructor has 'prototype' property with object value "
  12827. "-> set fallback prototype to that value: %!iO", (duk_heaphdr *) proto));
  12828. fallback = duk_get_hobject(ctx, -2);
  12829. DUK_ASSERT(fallback != NULL);
  12830. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, fallback, proto);
  12831. }
  12832. duk_pop(ctx);
  12833. /* [... constructor arg1 ... argN final_cons fallback] */
  12834. /*
  12835. * Manipulate callstack for the call.
  12836. */
  12837. duk_dup_top(ctx);
  12838. duk_insert(ctx, idx_cons + 1); /* use fallback as 'this' value */
  12839. duk_insert(ctx, idx_cons); /* also stash it before constructor,
  12840. * in case we need it (as the fallback value)
  12841. */
  12842. duk_pop(ctx); /* pop final_cons */
  12843. /* [... fallback constructor fallback(this) arg1 ... argN];
  12844. * Note: idx_cons points to first 'fallback', not 'constructor'.
  12845. */
  12846. DUK_DDD(DUK_DDDPRINT("before call, idx_cons+1 (constructor) -> %!T, idx_cons+2 (fallback/this) -> %!T, "
  12847. "nargs=%ld, top=%ld",
  12848. (duk_tval *) duk_get_tval(ctx, idx_cons + 1),
  12849. (duk_tval *) duk_get_tval(ctx, idx_cons + 2),
  12850. (long) nargs,
  12851. (long) duk_get_top(ctx)));
  12852. /*
  12853. * Call the constructor function (called in "constructor mode").
  12854. */
  12855. call_flags = DUK_CALL_FLAG_CONSTRUCTOR_CALL; /* not protected, respect reclimit, is a constructor call */
  12856. rc = duk_handle_call(thr, /* thread */
  12857. nargs, /* num_stack_args */
  12858. call_flags); /* call_flags */
  12859. DUK_UNREF(rc);
  12860. /* [... fallback retval] */
  12861. DUK_DDD(DUK_DDDPRINT("constructor call finished, rc=%ld, fallback=%!iT, retval=%!iT",
  12862. (long) rc,
  12863. (duk_tval *) duk_get_tval(ctx, -2),
  12864. (duk_tval *) duk_get_tval(ctx, -1)));
  12865. /*
  12866. * Determine whether to use the constructor return value as the created
  12867. * object instance or not.
  12868. */
  12869. if (duk_is_object(ctx, -1)) {
  12870. duk_remove(ctx, -2);
  12871. } else {
  12872. duk_pop(ctx);
  12873. }
  12874. /*
  12875. * Augment created errors upon creation (not when they are thrown or
  12876. * rethrown). __FILE__ and __LINE__ are not desirable here; the call
  12877. * stack reflects the caller which is correct.
  12878. */
  12879. #ifdef DUK_USE_AUGMENT_ERROR_CREATE
  12880. duk_err_augment_error_create(thr, thr, NULL, 0, 1 /*noblame_fileline*/);
  12881. #endif
  12882. /* [... retval] */
  12883. return;
  12884. not_constructable:
  12885. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_CONSTRUCTABLE);
  12886. }
  12887. DUK_EXTERNAL duk_bool_t duk_is_constructor_call(duk_context *ctx) {
  12888. duk_hthread *thr = (duk_hthread *) ctx;
  12889. duk_activation *act;
  12890. DUK_ASSERT_CTX_VALID(ctx);
  12891. DUK_ASSERT(thr != NULL);
  12892. DUK_ASSERT_DISABLE(thr->callstack_top >= 0);
  12893. act = duk_hthread_get_current_activation(thr);
  12894. DUK_ASSERT(act != NULL); /* because callstack_top > 0 */
  12895. return ((act->flags & DUK_ACT_FLAG_CONSTRUCT) != 0 ? 1 : 0);
  12896. }
  12897. DUK_EXTERNAL duk_bool_t duk_is_strict_call(duk_context *ctx) {
  12898. duk_hthread *thr = (duk_hthread *) ctx;
  12899. duk_activation *act;
  12900. /* For user code this could just return 1 (strict) always
  12901. * because all Duktape/C functions are considered strict,
  12902. * and strict is also the default when nothing is running.
  12903. * However, Duktape may call this function internally when
  12904. * the current activation is an Ecmascript function, so
  12905. * this cannot be replaced by a 'return 1' without fixing
  12906. * the internal call sites.
  12907. */
  12908. DUK_ASSERT_CTX_VALID(ctx);
  12909. DUK_ASSERT(thr != NULL);
  12910. DUK_ASSERT_DISABLE(thr->callstack_top >= 0);
  12911. act = duk_hthread_get_current_activation(thr);
  12912. if (act == NULL) {
  12913. /* Strict by default. */
  12914. return 1;
  12915. }
  12916. return ((act->flags & DUK_ACT_FLAG_STRICT) != 0 ? 1 : 0);
  12917. }
  12918. /*
  12919. * Duktape/C function magic
  12920. */
  12921. DUK_EXTERNAL duk_int_t duk_get_current_magic(duk_context *ctx) {
  12922. duk_hthread *thr = (duk_hthread *) ctx;
  12923. duk_activation *act;
  12924. duk_hobject *func;
  12925. DUK_ASSERT_CTX_VALID(ctx);
  12926. DUK_ASSERT(thr != NULL);
  12927. DUK_ASSERT_DISABLE(thr->callstack_top >= 0);
  12928. act = duk_hthread_get_current_activation(thr);
  12929. if (act) {
  12930. func = DUK_ACT_GET_FUNC(act);
  12931. if (!func) {
  12932. duk_tval *tv = &act->tv_func;
  12933. duk_small_uint_t lf_flags;
  12934. lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv);
  12935. return (duk_int_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags);
  12936. }
  12937. DUK_ASSERT(func != NULL);
  12938. if (DUK_HOBJECT_IS_NATIVEFUNCTION(func)) {
  12939. duk_hnativefunction *nf = (duk_hnativefunction *) func;
  12940. return (duk_int_t) nf->magic;
  12941. }
  12942. }
  12943. return 0;
  12944. }
  12945. DUK_EXTERNAL duk_int_t duk_get_magic(duk_context *ctx, duk_idx_t index) {
  12946. duk_hthread *thr = (duk_hthread *) ctx;
  12947. duk_tval *tv;
  12948. duk_hobject *h;
  12949. DUK_ASSERT_CTX_VALID(ctx);
  12950. tv = duk_require_tval(ctx, index);
  12951. if (DUK_TVAL_IS_OBJECT(tv)) {
  12952. h = DUK_TVAL_GET_OBJECT(tv);
  12953. DUK_ASSERT(h != NULL);
  12954. if (!DUK_HOBJECT_HAS_NATIVEFUNCTION(h)) {
  12955. goto type_error;
  12956. }
  12957. return (duk_int_t) ((duk_hnativefunction *) h)->magic;
  12958. } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  12959. duk_small_uint_t lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv);
  12960. return (duk_int_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags);
  12961. }
  12962. /* fall through */
  12963. type_error:
  12964. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_UNEXPECTED_TYPE);
  12965. return 0;
  12966. }
  12967. DUK_EXTERNAL void duk_set_magic(duk_context *ctx, duk_idx_t index, duk_int_t magic) {
  12968. duk_hnativefunction *nf;
  12969. DUK_ASSERT_CTX_VALID(ctx);
  12970. nf = duk_require_hnativefunction(ctx, index);
  12971. DUK_ASSERT(nf != NULL);
  12972. nf->magic = (duk_int16_t) magic;
  12973. }
  12974. #line 1 "duk_api_codec.c"
  12975. /*
  12976. * Encoding and decoding basic formats: hex, base64.
  12977. *
  12978. * These are in-place operations which may allow an optimized implementation.
  12979. */
  12980. /* include removed: duk_internal.h */
  12981. /* dst length must be exactly ceil(len/3)*4 */
  12982. DUK_LOCAL void duk__base64_encode_helper(const duk_uint8_t *src, const duk_uint8_t *src_end,
  12983. duk_uint8_t *dst, duk_uint8_t *dst_end) {
  12984. duk_small_uint_t i, snip;
  12985. duk_uint_fast32_t t;
  12986. duk_uint_fast8_t x, y;
  12987. DUK_UNREF(dst_end);
  12988. while (src < src_end) {
  12989. /* read 3 bytes into 't', padded by zero */
  12990. snip = 4;
  12991. t = 0;
  12992. for (i = 0; i < 3; i++) {
  12993. t = t << 8;
  12994. if (src >= src_end) {
  12995. snip--;
  12996. } else {
  12997. t += (duk_uint_fast32_t) (*src++);
  12998. }
  12999. }
  13000. /*
  13001. * Missing bytes snip base64 example
  13002. * 0 4 XXXX
  13003. * 1 3 XXX=
  13004. * 2 2 XX==
  13005. */
  13006. DUK_ASSERT(snip >= 2 && snip <= 4);
  13007. for (i = 0; i < 4; i++) {
  13008. x = (duk_uint_fast8_t) ((t >> 18) & 0x3f);
  13009. t = t << 6;
  13010. /* A straightforward 64-byte lookup would be faster
  13011. * and cleaner, but this is shorter.
  13012. */
  13013. if (i >= snip) {
  13014. y = '=';
  13015. } else if (x <= 25) {
  13016. y = x + 'A';
  13017. } else if (x <= 51) {
  13018. y = x - 26 + 'a';
  13019. } else if (x <= 61) {
  13020. y = x - 52 + '0';
  13021. } else if (x == 62) {
  13022. y = '+';
  13023. } else {
  13024. y = '/';
  13025. }
  13026. DUK_ASSERT(dst < dst_end);
  13027. *dst++ = (duk_uint8_t) y;
  13028. }
  13029. }
  13030. }
  13031. DUK_LOCAL duk_bool_t duk__base64_decode_helper(const duk_uint8_t *src, const duk_uint8_t *src_end,
  13032. duk_uint8_t *dst, duk_uint8_t *dst_end, duk_uint8_t **out_dst_final) {
  13033. duk_uint_fast32_t t;
  13034. duk_uint_fast8_t x, y;
  13035. duk_small_uint_t group_idx;
  13036. DUK_UNREF(dst_end);
  13037. t = 0;
  13038. group_idx = 0;
  13039. while (src < src_end) {
  13040. x = *src++;
  13041. if (x >= 'A' && x <= 'Z') {
  13042. y = x - 'A' + 0;
  13043. } else if (x >= 'a' && x <= 'z') {
  13044. y = x - 'a' + 26;
  13045. } else if (x >= '0' && x <= '9') {
  13046. y = x - '0' + 52;
  13047. } else if (x == '+') {
  13048. y = 62;
  13049. } else if (x == '/') {
  13050. y = 63;
  13051. } else if (x == '=') {
  13052. /* We don't check the zero padding bytes here right now.
  13053. * This seems to be common behavior for base-64 decoders.
  13054. */
  13055. if (group_idx == 2) {
  13056. /* xx== -> 1 byte, t contains 12 bits, 4 on right are zero */
  13057. t = t >> 4;
  13058. DUK_ASSERT(dst < dst_end);
  13059. *dst++ = (duk_uint8_t) t;
  13060. if (src >= src_end) {
  13061. goto error;
  13062. }
  13063. x = *src++;
  13064. if (x != '=') {
  13065. goto error;
  13066. }
  13067. } else if (group_idx == 3) {
  13068. /* xxx= -> 2 bytes, t contains 18 bits, 2 on right are zero */
  13069. t = t >> 2;
  13070. DUK_ASSERT(dst < dst_end);
  13071. *dst++ = (duk_uint8_t) ((t >> 8) & 0xff);
  13072. DUK_ASSERT(dst < dst_end);
  13073. *dst++ = (duk_uint8_t) (t & 0xff);
  13074. } else {
  13075. goto error;
  13076. }
  13077. /* Here we can choose either to end parsing and ignore
  13078. * whatever follows, or to continue parsing in case
  13079. * multiple (possibly padded) base64 strings have been
  13080. * concatenated. Currently, keep on parsing.
  13081. */
  13082. t = 0;
  13083. group_idx = 0;
  13084. continue;
  13085. } else if (x == 0x09 || x == 0x0a || x == 0x0d || x == 0x20) {
  13086. /* allow basic ASCII whitespace */
  13087. continue;
  13088. } else {
  13089. goto error;
  13090. }
  13091. t = (t << 6) + y;
  13092. if (group_idx == 3) {
  13093. /* output 3 bytes from 't' */
  13094. DUK_ASSERT(dst < dst_end);
  13095. *dst++ = (duk_uint8_t) ((t >> 16) & 0xff);
  13096. DUK_ASSERT(dst < dst_end);
  13097. *dst++ = (duk_uint8_t) ((t >> 8) & 0xff);
  13098. DUK_ASSERT(dst < dst_end);
  13099. *dst++ = (duk_uint8_t) (t & 0xff);
  13100. t = 0;
  13101. group_idx = 0;
  13102. } else {
  13103. group_idx++;
  13104. }
  13105. }
  13106. if (group_idx != 0) {
  13107. /* Here we'd have the option of decoding unpadded base64
  13108. * (e.g. "xxxxyy" instead of "xxxxyy==". Currently not
  13109. * accepted.
  13110. */
  13111. goto error;
  13112. }
  13113. *out_dst_final = dst;
  13114. return 1;
  13115. error:
  13116. return 0;
  13117. }
  13118. /* Shared handling for encode/decode argument. Fast path handling for
  13119. * buffer and string values because they're the most common. In particular,
  13120. * avoid creating a temporary string or buffer when possible.
  13121. */
  13122. DUK_LOCAL const duk_uint8_t *duk__prep_codec_arg(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) {
  13123. DUK_ASSERT(duk_is_valid_index(ctx, index)); /* checked by caller */
  13124. if (duk_is_buffer(ctx, index)) {
  13125. return (const duk_uint8_t *) duk_get_buffer(ctx, index, out_len);
  13126. } else {
  13127. return (const duk_uint8_t *) duk_to_lstring(ctx, index, out_len);
  13128. }
  13129. }
  13130. DUK_EXTERNAL const char *duk_base64_encode(duk_context *ctx, duk_idx_t index) {
  13131. duk_hthread *thr = (duk_hthread *) ctx;
  13132. duk_uint8_t *src;
  13133. duk_size_t srclen;
  13134. duk_size_t dstlen;
  13135. duk_uint8_t *dst;
  13136. const char *ret;
  13137. DUK_ASSERT_CTX_VALID(ctx);
  13138. /* XXX: optimize for string inputs: no need to coerce to a buffer
  13139. * which makes a copy of the input.
  13140. */
  13141. index = duk_require_normalize_index(ctx, index);
  13142. src = (duk_uint8_t *) duk_to_buffer(ctx, index, &srclen);
  13143. /* Note: for srclen=0, src may be NULL */
  13144. /* Computation must not wrap; this limit works for 32-bit size_t:
  13145. * >>> srclen = 3221225469
  13146. * >>> '%x' % ((srclen + 2) / 3 * 4)
  13147. * 'fffffffc'
  13148. */
  13149. if (srclen > 3221225469UL) {
  13150. goto type_error;
  13151. }
  13152. dstlen = (srclen + 2) / 3 * 4;
  13153. dst = (duk_uint8_t *) duk_push_fixed_buffer(ctx, dstlen);
  13154. duk__base64_encode_helper((const duk_uint8_t *) src, (const duk_uint8_t *) (src + srclen),
  13155. dst, (dst + dstlen));
  13156. ret = duk_to_string(ctx, -1);
  13157. duk_replace(ctx, index);
  13158. return ret;
  13159. type_error:
  13160. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_BASE64_ENCODE_FAILED);
  13161. return NULL; /* never here */
  13162. }
  13163. DUK_EXTERNAL void duk_base64_decode(duk_context *ctx, duk_idx_t index) {
  13164. duk_hthread *thr = (duk_hthread *) ctx;
  13165. const duk_uint8_t *src;
  13166. duk_size_t srclen;
  13167. duk_size_t dstlen;
  13168. duk_uint8_t *dst;
  13169. duk_uint8_t *dst_final;
  13170. duk_bool_t retval;
  13171. DUK_ASSERT_CTX_VALID(ctx);
  13172. /* XXX: optimize for buffer inputs: no need to coerce to a string
  13173. * which causes an unnecessary interning.
  13174. */
  13175. index = duk_require_normalize_index(ctx, index);
  13176. src = (const duk_uint8_t *) duk_to_lstring(ctx, index, &srclen);
  13177. /* Computation must not wrap, only srclen + 3 is at risk of
  13178. * wrapping because after that the number gets smaller.
  13179. * This limit works for 32-bit size_t:
  13180. * 0x100000000 - 3 - 1 = 4294967292
  13181. */
  13182. if (srclen > 4294967292UL) {
  13183. goto type_error;
  13184. }
  13185. dstlen = (srclen + 3) / 4 * 3; /* upper limit */
  13186. dst = (duk_uint8_t *) duk_push_dynamic_buffer(ctx, dstlen);
  13187. /* Note: for dstlen=0, dst may be NULL */
  13188. retval = duk__base64_decode_helper((const duk_uint8_t *) src, (const duk_uint8_t *) (src + srclen),
  13189. dst, dst + dstlen, &dst_final);
  13190. if (!retval) {
  13191. goto type_error;
  13192. }
  13193. /* XXX: convert to fixed buffer? */
  13194. (void) duk_resize_buffer(ctx, -1, (duk_size_t) (dst_final - dst));
  13195. duk_replace(ctx, index);
  13196. return;
  13197. type_error:
  13198. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_BASE64_DECODE_FAILED);
  13199. }
  13200. DUK_EXTERNAL const char *duk_hex_encode(duk_context *ctx, duk_idx_t index) {
  13201. const duk_uint8_t *inp;
  13202. duk_size_t len;
  13203. duk_size_t i;
  13204. duk_small_uint_t t;
  13205. duk_uint8_t *buf;
  13206. const char *ret;
  13207. DUK_ASSERT_CTX_VALID(ctx);
  13208. index = duk_require_normalize_index(ctx, index);
  13209. inp = duk__prep_codec_arg(ctx, index, &len);
  13210. DUK_ASSERT(inp != NULL || len == 0);
  13211. /* Fixed buffer, no zeroing because we'll fill all the data. */
  13212. buf = (duk_uint8_t *) duk_push_buffer_raw(ctx, len * 2, DUK_BUF_FLAG_NOZERO /*flags*/);
  13213. DUK_ASSERT(buf != NULL);
  13214. for (i = 0; i < len; i++) {
  13215. /* XXX: by using two 256-entry tables could avoid shifting and masking. */
  13216. t = (duk_small_uint_t) inp[i];
  13217. buf[i*2 + 0] = duk_lc_digits[t >> 4];
  13218. buf[i*2 + 1] = duk_lc_digits[t & 0x0f];
  13219. }
  13220. /* XXX: Using a string return value forces a string intern which is
  13221. * not always necessary. As a rough performance measure, hex encode
  13222. * time for perf-testcases/test-hex-encode.js dropped from ~35s to
  13223. * ~15s without string coercion. Change to returning a buffer and
  13224. * let the caller coerce to string if necessary?
  13225. */
  13226. ret = duk_to_string(ctx, -1);
  13227. duk_replace(ctx, index);
  13228. return ret;
  13229. }
  13230. DUK_EXTERNAL void duk_hex_decode(duk_context *ctx, duk_idx_t index) {
  13231. duk_hthread *thr = (duk_hthread *) ctx;
  13232. const duk_uint8_t *inp;
  13233. duk_size_t len;
  13234. duk_size_t i;
  13235. duk_small_int_t t;
  13236. duk_uint8_t *buf;
  13237. DUK_ASSERT_CTX_VALID(ctx);
  13238. index = duk_require_normalize_index(ctx, index);
  13239. inp = duk__prep_codec_arg(ctx, index, &len);
  13240. DUK_ASSERT(inp != NULL || len == 0);
  13241. if (len & 0x01) {
  13242. goto type_error;
  13243. }
  13244. /* Fixed buffer, no zeroing because we'll fill all the data. */
  13245. buf = (duk_uint8_t *) duk_push_buffer_raw(ctx, len / 2, DUK_BUF_FLAG_NOZERO /*flags*/);
  13246. DUK_ASSERT(buf != NULL);
  13247. for (i = 0; i < len; i += 2) {
  13248. /* For invalid characters the value -1 gets extended to
  13249. * at least 16 bits. If either nybble is invalid, the
  13250. * resulting 't' will be < 0.
  13251. */
  13252. t = (((duk_small_int_t) duk_hex_dectab[inp[i]]) << 4) |
  13253. ((duk_small_int_t) duk_hex_dectab[inp[i + 1]]);
  13254. if (DUK_UNLIKELY(t < 0)) {
  13255. goto type_error;
  13256. }
  13257. buf[i >> 1] = (duk_uint8_t) t;
  13258. }
  13259. duk_replace(ctx, index);
  13260. return;
  13261. type_error:
  13262. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_HEX_DECODE_FAILED);
  13263. }
  13264. DUK_EXTERNAL const char *duk_json_encode(duk_context *ctx, duk_idx_t index) {
  13265. #ifdef DUK_USE_ASSERTIONS
  13266. duk_idx_t top_at_entry;
  13267. #endif
  13268. const char *ret;
  13269. DUK_ASSERT_CTX_VALID(ctx);
  13270. #ifdef DUK_USE_ASSERTIONS
  13271. top_at_entry = duk_get_top(ctx);
  13272. #endif
  13273. index = duk_require_normalize_index(ctx, index);
  13274. duk_bi_json_stringify_helper(ctx,
  13275. index /*idx_value*/,
  13276. DUK_INVALID_INDEX /*idx_replacer*/,
  13277. DUK_INVALID_INDEX /*idx_space*/,
  13278. 0 /*flags*/);
  13279. DUK_ASSERT(duk_is_string(ctx, -1));
  13280. duk_replace(ctx, index);
  13281. ret = duk_get_string(ctx, index);
  13282. DUK_ASSERT(duk_get_top(ctx) == top_at_entry);
  13283. return ret;
  13284. }
  13285. DUK_EXTERNAL void duk_json_decode(duk_context *ctx, duk_idx_t index) {
  13286. #ifdef DUK_USE_ASSERTIONS
  13287. duk_idx_t top_at_entry;
  13288. #endif
  13289. DUK_ASSERT_CTX_VALID(ctx);
  13290. #ifdef DUK_USE_ASSERTIONS
  13291. top_at_entry = duk_get_top(ctx);
  13292. #endif
  13293. index = duk_require_normalize_index(ctx, index);
  13294. duk_bi_json_parse_helper(ctx,
  13295. index /*idx_value*/,
  13296. DUK_INVALID_INDEX /*idx_reviver*/,
  13297. 0 /*flags*/);
  13298. duk_replace(ctx, index);
  13299. DUK_ASSERT(duk_get_top(ctx) == top_at_entry);
  13300. }
  13301. #line 1 "duk_api_compile.c"
  13302. /*
  13303. * Compilation and evaluation
  13304. */
  13305. /* include removed: duk_internal.h */
  13306. typedef struct duk__compile_raw_args duk__compile_raw_args;
  13307. struct duk__compile_raw_args {
  13308. duk_size_t src_length; /* should be first on 64-bit platforms */
  13309. const duk_uint8_t *src_buffer;
  13310. duk_uint_t flags;
  13311. };
  13312. /* Eval is just a wrapper now. */
  13313. DUK_EXTERNAL duk_int_t duk_eval_raw(duk_context *ctx, const char *src_buffer, duk_size_t src_length, duk_uint_t flags) {
  13314. duk_uint_t comp_flags;
  13315. duk_int_t rc;
  13316. DUK_ASSERT_CTX_VALID(ctx);
  13317. /* Note: strictness is *not* inherited from the current Duktape/C.
  13318. * This would be confusing because the current strictness state
  13319. * depends on whether we're running inside a Duktape/C activation
  13320. * (= strict mode) or outside of any activation (= non-strict mode).
  13321. * See api-testcases/test-eval-strictness.c for more discussion.
  13322. */
  13323. /* [ ... source? filename ] (depends on flags) */
  13324. comp_flags = flags;
  13325. comp_flags |= DUK_COMPILE_EVAL;
  13326. rc = duk_compile_raw(ctx, src_buffer, src_length, comp_flags); /* may be safe, or non-safe depending on flags */
  13327. /* [ ... closure/error ] */
  13328. if (rc != DUK_EXEC_SUCCESS) {
  13329. rc = DUK_EXEC_ERROR;
  13330. goto got_rc;
  13331. }
  13332. if (flags & DUK_COMPILE_SAFE) {
  13333. rc = duk_pcall(ctx, 0);
  13334. } else {
  13335. duk_call(ctx, 0);
  13336. rc = DUK_EXEC_SUCCESS;
  13337. }
  13338. /* [ ... result/error ] */
  13339. got_rc:
  13340. if (flags & DUK_COMPILE_NORESULT) {
  13341. duk_pop(ctx);
  13342. }
  13343. return rc;
  13344. }
  13345. /* Helper which can be called both directly and with duk_safe_call(). */
  13346. DUK_LOCAL duk_ret_t duk__do_compile(duk_context *ctx) {
  13347. duk_hthread *thr = (duk_hthread *) ctx;
  13348. duk__compile_raw_args *comp_args;
  13349. duk_uint_t flags;
  13350. duk_small_uint_t comp_flags;
  13351. duk_hcompiledfunction *h_templ;
  13352. DUK_ASSERT_CTX_VALID(ctx);
  13353. /* Note: strictness is not inherited from the current Duktape/C
  13354. * context. Otherwise it would not be possible to compile
  13355. * non-strict code inside a Duktape/C activation (which is
  13356. * always strict now). See api-testcases/test-eval-strictness.c
  13357. * for discussion.
  13358. */
  13359. /* [ ... source? filename &comp_args ] (depends on flags) */
  13360. comp_args = (duk__compile_raw_args *) duk_require_pointer(ctx, -1);
  13361. flags = comp_args->flags;
  13362. duk_pop(ctx);
  13363. /* [ ... source? filename ] */
  13364. if (!comp_args->src_buffer) {
  13365. duk_hstring *h_sourcecode;
  13366. h_sourcecode = duk_get_hstring(ctx, -2);
  13367. if ((flags & DUK_COMPILE_NOSOURCE) || /* args incorrect */
  13368. (h_sourcecode == NULL)) { /* e.g. duk_push_file_string_raw() pushed undefined */
  13369. /* XXX: when this error is caused by a nonexistent
  13370. * file given to duk_peval_file() or similar, the
  13371. * error message is not the best possible.
  13372. */
  13373. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_NO_SOURCECODE);
  13374. }
  13375. DUK_ASSERT(h_sourcecode != NULL);
  13376. comp_args->src_buffer = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_sourcecode);
  13377. comp_args->src_length = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h_sourcecode);
  13378. }
  13379. DUK_ASSERT(comp_args->src_buffer != NULL);
  13380. /* XXX: unnecessary translation of flags */
  13381. comp_flags = 0;
  13382. if (flags & DUK_COMPILE_EVAL) {
  13383. comp_flags |= DUK_JS_COMPILE_FLAG_EVAL;
  13384. }
  13385. if (flags & DUK_COMPILE_FUNCTION) {
  13386. comp_flags |= DUK_JS_COMPILE_FLAG_EVAL |
  13387. DUK_JS_COMPILE_FLAG_FUNCEXPR;
  13388. }
  13389. if (flags & DUK_COMPILE_STRICT) {
  13390. comp_flags |= DUK_JS_COMPILE_FLAG_STRICT;
  13391. }
  13392. /* [ ... source? filename ] */
  13393. duk_js_compile(thr, comp_args->src_buffer, comp_args->src_length, comp_flags);
  13394. /* [ ... source? func_template ] */
  13395. if (flags & DUK_COMPILE_NOSOURCE) {
  13396. ;
  13397. } else {
  13398. duk_remove(ctx, -2);
  13399. }
  13400. /* [ ... func_template ] */
  13401. h_templ = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1);
  13402. DUK_ASSERT(h_templ != NULL);
  13403. duk_js_push_closure(thr,
  13404. h_templ,
  13405. thr->builtins[DUK_BIDX_GLOBAL_ENV],
  13406. thr->builtins[DUK_BIDX_GLOBAL_ENV]);
  13407. duk_remove(ctx, -2); /* -> [ ... closure ] */
  13408. /* [ ... closure ] */
  13409. return 1;
  13410. }
  13411. DUK_EXTERNAL duk_int_t duk_compile_raw(duk_context *ctx, const char *src_buffer, duk_size_t src_length, duk_uint_t flags) {
  13412. duk__compile_raw_args comp_args_alloc;
  13413. duk__compile_raw_args *comp_args = &comp_args_alloc;
  13414. DUK_ASSERT_CTX_VALID(ctx);
  13415. if ((flags & DUK_COMPILE_STRLEN) && (src_buffer != NULL)) {
  13416. /* String length is computed here to avoid multiple evaluation
  13417. * of a macro argument in the calling side.
  13418. */
  13419. src_length = DUK_STRLEN(src_buffer);
  13420. }
  13421. comp_args->src_buffer = (const duk_uint8_t *) src_buffer;
  13422. comp_args->src_length = src_length;
  13423. comp_args->flags = flags;
  13424. duk_push_pointer(ctx, (void *) comp_args);
  13425. /* [ ... source? filename &comp_args ] (depends on flags) */
  13426. if (flags & DUK_COMPILE_SAFE) {
  13427. duk_int_t rc;
  13428. duk_int_t nargs;
  13429. duk_int_t nrets = 1;
  13430. /* Arguments are either: [ filename &comp_args ] or [ source filename &comp_args ] */
  13431. nargs = (flags & DUK_COMPILE_NOSOURCE) ? 2 : 3;
  13432. rc = duk_safe_call(ctx, duk__do_compile, nargs, nrets);
  13433. /* [ ... closure ] */
  13434. return rc;
  13435. }
  13436. (void) duk__do_compile(ctx);
  13437. /* [ ... closure ] */
  13438. return DUK_EXEC_SUCCESS;
  13439. }
  13440. #line 1 "duk_api_debug.c"
  13441. /*
  13442. * Debugging related API calls
  13443. */
  13444. /* include removed: duk_internal.h */
  13445. DUK_EXTERNAL void duk_push_context_dump(duk_context *ctx) {
  13446. duk_idx_t idx;
  13447. duk_idx_t top;
  13448. DUK_ASSERT_CTX_VALID(ctx);
  13449. /* We don't duk_require_stack() here now, but rely on the caller having
  13450. * enough space.
  13451. */
  13452. top = duk_get_top(ctx);
  13453. duk_push_array(ctx);
  13454. for (idx = 0; idx < top; idx++) {
  13455. duk_dup(ctx, idx);
  13456. duk_put_prop_index(ctx, -2, idx);
  13457. }
  13458. /* XXX: conversion errors should not propagate outwards.
  13459. * Perhaps values need to be coerced individually?
  13460. */
  13461. duk_bi_json_stringify_helper(ctx,
  13462. duk_get_top_index(ctx), /*idx_value*/
  13463. DUK_INVALID_INDEX, /*idx_replacer*/
  13464. DUK_INVALID_INDEX, /*idx_space*/
  13465. DUK_JSON_FLAG_EXT_CUSTOM |
  13466. DUK_JSON_FLAG_ASCII_ONLY |
  13467. DUK_JSON_FLAG_AVOID_KEY_QUOTES /*flags*/);
  13468. duk_push_sprintf(ctx, "ctx: top=%ld, stack=%s", (long) top, (const char *) duk_safe_to_string(ctx, -1));
  13469. duk_replace(ctx, -3); /* [ ... arr jsonx(arr) res ] -> [ ... res jsonx(arr) ] */
  13470. duk_pop(ctx);
  13471. DUK_ASSERT(duk_is_string(ctx, -1));
  13472. }
  13473. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  13474. DUK_EXTERNAL void duk_debugger_attach(duk_context *ctx,
  13475. duk_debug_read_function read_cb,
  13476. duk_debug_write_function write_cb,
  13477. duk_debug_peek_function peek_cb,
  13478. duk_debug_read_flush_function read_flush_cb,
  13479. duk_debug_write_flush_function write_flush_cb,
  13480. duk_debug_detached_function detached_cb,
  13481. void *udata) {
  13482. duk_hthread *thr = (duk_hthread *) ctx;
  13483. duk_heap *heap;
  13484. const char *str;
  13485. duk_size_t len;
  13486. DUK_ASSERT_CTX_VALID(ctx);
  13487. DUK_ASSERT(read_cb != NULL);
  13488. DUK_ASSERT(write_cb != NULL);
  13489. /* Other callbacks are optional. */
  13490. heap = thr->heap;
  13491. heap->dbg_read_cb = read_cb;
  13492. heap->dbg_write_cb = write_cb;
  13493. heap->dbg_peek_cb = peek_cb;
  13494. heap->dbg_read_flush_cb = read_flush_cb;
  13495. heap->dbg_write_flush_cb = write_flush_cb;
  13496. heap->dbg_detached_cb = detached_cb;
  13497. heap->dbg_udata = udata;
  13498. /* Start in paused state. */
  13499. heap->dbg_processing = 0;
  13500. heap->dbg_paused = 1;
  13501. heap->dbg_state_dirty = 1;
  13502. heap->dbg_step_type = 0;
  13503. heap->dbg_step_thread = NULL;
  13504. heap->dbg_step_csindex = 0;
  13505. heap->dbg_step_startline = 0;
  13506. heap->dbg_exec_counter = 0;
  13507. heap->dbg_last_counter = 0;
  13508. heap->dbg_last_time = 0.0;
  13509. /* Send version identification and flush right afterwards. Note that
  13510. * we must write raw, unframed bytes here.
  13511. */
  13512. duk_push_sprintf(ctx, "%ld %ld %s %s\n",
  13513. (long) DUK_DEBUG_PROTOCOL_VERSION,
  13514. (long) DUK_VERSION,
  13515. (const char *) DUK_GIT_DESCRIBE,
  13516. (const char *) DUK_USE_TARGET_INFO);
  13517. str = duk_get_lstring(ctx, -1, &len);
  13518. DUK_ASSERT(str != NULL);
  13519. duk_debug_write_bytes(thr, (const duk_uint8_t *) str, len);
  13520. duk_debug_write_flush(thr);
  13521. duk_pop(ctx);
  13522. }
  13523. DUK_EXTERNAL void duk_debugger_detach(duk_context *ctx) {
  13524. duk_hthread *thr;
  13525. DUK_ASSERT_CTX_VALID(ctx);
  13526. thr = (duk_hthread *) ctx;
  13527. DUK_ASSERT(thr != NULL);
  13528. DUK_ASSERT(thr->heap != NULL);
  13529. /* Can be called muliple times with no harm. */
  13530. duk_debug_do_detach(thr->heap);
  13531. }
  13532. DUK_EXTERNAL void duk_debugger_cooperate(duk_context *ctx) {
  13533. duk_hthread *thr;
  13534. duk_bool_t processed_messages;
  13535. DUK_ASSERT_CTX_VALID(ctx);
  13536. thr = (duk_hthread *) ctx;
  13537. DUK_ASSERT(thr != NULL);
  13538. DUK_ASSERT(thr->heap != NULL);
  13539. if (!DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) {
  13540. return;
  13541. }
  13542. if (thr->callstack_top > 0 || thr->heap->dbg_processing) {
  13543. /* Calling duk_debugger_cooperate() while Duktape is being
  13544. * called into is not supported. This is not a 100% check
  13545. * but prevents any damage in most cases.
  13546. */
  13547. return;
  13548. }
  13549. thr->heap->dbg_processing = 1;
  13550. processed_messages = duk_debug_process_messages(thr, 1 /*no_block*/);
  13551. thr->heap->dbg_processing = 0;
  13552. DUK_UNREF(processed_messages);
  13553. }
  13554. #else /* DUK_USE_DEBUGGER_SUPPORT */
  13555. DUK_EXTERNAL void duk_debugger_attach(duk_context *ctx,
  13556. duk_debug_read_function read_cb,
  13557. duk_debug_write_function write_cb,
  13558. duk_debug_peek_function peek_cb,
  13559. duk_debug_read_flush_function read_flush_cb,
  13560. duk_debug_write_flush_function write_flush_cb,
  13561. duk_debug_detached_function detached_cb,
  13562. void *udata) {
  13563. DUK_ASSERT_CTX_VALID(ctx);
  13564. DUK_UNREF(read_cb);
  13565. DUK_UNREF(write_cb);
  13566. DUK_UNREF(peek_cb);
  13567. DUK_UNREF(read_flush_cb);
  13568. DUK_UNREF(write_flush_cb);
  13569. DUK_UNREF(detached_cb);
  13570. DUK_UNREF(udata);
  13571. duk_error(ctx, DUK_ERR_API_ERROR, "no debugger support");
  13572. }
  13573. DUK_EXTERNAL void duk_debugger_detach(duk_context *ctx) {
  13574. DUK_ASSERT_CTX_VALID(ctx);
  13575. duk_error(ctx, DUK_ERR_API_ERROR, "no debugger support");
  13576. }
  13577. DUK_EXTERNAL void duk_debugger_cooperate(duk_context *ctx) {
  13578. /* nop */
  13579. DUK_ASSERT_CTX_VALID(ctx);
  13580. DUK_UNREF(ctx);
  13581. }
  13582. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  13583. #line 1 "duk_api_heap.c"
  13584. /*
  13585. * Heap creation and destruction
  13586. */
  13587. /* include removed: duk_internal.h */
  13588. DUK_EXTERNAL
  13589. duk_context *duk_create_heap(duk_alloc_function alloc_func,
  13590. duk_realloc_function realloc_func,
  13591. duk_free_function free_func,
  13592. void *heap_udata,
  13593. duk_fatal_function fatal_handler) {
  13594. duk_heap *heap = NULL;
  13595. duk_context *ctx;
  13596. /* Assume that either all memory funcs are NULL or non-NULL, mixed
  13597. * cases will now be unsafe.
  13598. */
  13599. /* XXX: just assert non-NULL values here and make caller arguments
  13600. * do the defaulting to the default implementations (smaller code)?
  13601. */
  13602. if (!alloc_func) {
  13603. DUK_ASSERT(realloc_func == NULL);
  13604. DUK_ASSERT(free_func == NULL);
  13605. alloc_func = duk_default_alloc_function;
  13606. realloc_func = duk_default_realloc_function;
  13607. free_func = duk_default_free_function;
  13608. } else {
  13609. DUK_ASSERT(realloc_func != NULL);
  13610. DUK_ASSERT(free_func != NULL);
  13611. }
  13612. if (!fatal_handler) {
  13613. fatal_handler = duk_default_fatal_handler;
  13614. }
  13615. DUK_ASSERT(alloc_func != NULL);
  13616. DUK_ASSERT(realloc_func != NULL);
  13617. DUK_ASSERT(free_func != NULL);
  13618. DUK_ASSERT(fatal_handler != NULL);
  13619. heap = duk_heap_alloc(alloc_func, realloc_func, free_func, heap_udata, fatal_handler);
  13620. if (!heap) {
  13621. return NULL;
  13622. }
  13623. ctx = (duk_context *) heap->heap_thread;
  13624. DUK_ASSERT(ctx != NULL);
  13625. DUK_ASSERT(((duk_hthread *) ctx)->heap != NULL);
  13626. return ctx;
  13627. }
  13628. DUK_EXTERNAL void duk_destroy_heap(duk_context *ctx) {
  13629. duk_hthread *thr = (duk_hthread *) ctx;
  13630. duk_heap *heap;
  13631. if (!ctx) {
  13632. return;
  13633. }
  13634. heap = thr->heap;
  13635. DUK_ASSERT(heap != NULL);
  13636. duk_heap_free(heap);
  13637. }
  13638. /* XXX: better place for this */
  13639. DUK_EXTERNAL void duk_set_global_object(duk_context *ctx) {
  13640. duk_hthread *thr = (duk_hthread *) ctx;
  13641. duk_hobject *h_glob;
  13642. duk_hobject *h_prev_glob;
  13643. duk_hobject *h_env;
  13644. duk_hobject *h_prev_env;
  13645. DUK_D(DUK_DPRINT("replace global object with: %!T", duk_get_tval(ctx, -1)));
  13646. h_glob = duk_require_hobject(ctx, -1);
  13647. DUK_ASSERT(h_glob != NULL);
  13648. /*
  13649. * Replace global object.
  13650. */
  13651. h_prev_glob = thr->builtins[DUK_BIDX_GLOBAL];
  13652. thr->builtins[DUK_BIDX_GLOBAL] = h_glob;
  13653. DUK_HOBJECT_INCREF(thr, h_glob);
  13654. DUK_HOBJECT_DECREF_ALLOWNULL(thr, h_prev_glob); /* side effects, in theory (referenced by global env) */
  13655. /*
  13656. * Replace lexical environment for global scope
  13657. *
  13658. * Create a new object environment for the global lexical scope.
  13659. * We can't just reset the _Target property of the current one,
  13660. * because the lexical scope is shared by other threads with the
  13661. * same (initial) built-ins.
  13662. */
  13663. (void) duk_push_object_helper(ctx,
  13664. DUK_HOBJECT_FLAG_EXTENSIBLE |
  13665. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJENV),
  13666. -1); /* no prototype, updated below */
  13667. duk_dup(ctx, -2);
  13668. duk_dup(ctx, -3);
  13669. /* [ ... new_glob new_env new_glob new_glob ] */
  13670. duk_xdef_prop_stridx(thr, -3, DUK_STRIDX_INT_TARGET, DUK_PROPDESC_FLAGS_NONE);
  13671. duk_xdef_prop_stridx(thr, -2, DUK_STRIDX_INT_THIS, DUK_PROPDESC_FLAGS_NONE);
  13672. /* [ ... new_glob new_env ] */
  13673. h_env = duk_get_hobject(ctx, -1);
  13674. DUK_ASSERT(h_env != NULL);
  13675. h_prev_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  13676. thr->builtins[DUK_BIDX_GLOBAL_ENV] = h_env;
  13677. DUK_HOBJECT_INCREF(thr, h_env);
  13678. DUK_HOBJECT_DECREF_ALLOWNULL(thr, h_prev_env); /* side effects */
  13679. DUK_UNREF(h_env); /* without refcounts */
  13680. DUK_UNREF(h_prev_env);
  13681. /* [ ... new_glob new_env ] */
  13682. duk_pop_2(ctx);
  13683. /* [ ... ] */
  13684. }
  13685. #line 1 "duk_api_logging.c"
  13686. /*
  13687. * Logging
  13688. *
  13689. * Current logging primitive is a sprintf-style log which is convenient
  13690. * for most C code. Another useful primitive would be to log N arguments
  13691. * from value stack (like the Ecmascript binding does).
  13692. */
  13693. /* include removed: duk_internal.h */
  13694. DUK_EXTERNAL void duk_log_va(duk_context *ctx, duk_int_t level, const char *fmt, va_list ap) {
  13695. /* stridx_logfunc[] must be static to allow initializer with old compilers like BCC */
  13696. static const duk_uint16_t stridx_logfunc[6] = {
  13697. DUK_STRIDX_LC_TRACE, DUK_STRIDX_LC_DEBUG, DUK_STRIDX_LC_INFO,
  13698. DUK_STRIDX_LC_WARN, DUK_STRIDX_LC_ERROR, DUK_STRIDX_LC_FATAL
  13699. };
  13700. DUK_ASSERT_CTX_VALID(ctx);
  13701. if (level < 0) {
  13702. level = 0;
  13703. } else if (level > (int) (sizeof(stridx_logfunc) / sizeof(duk_uint16_t)) - 1) {
  13704. level = (int) (sizeof(stridx_logfunc) / sizeof(duk_uint16_t)) - 1;
  13705. }
  13706. duk_push_hobject_bidx(ctx, DUK_BIDX_LOGGER_CONSTRUCTOR);
  13707. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_CLOG);
  13708. duk_get_prop_stridx(ctx, -1, stridx_logfunc[level]);
  13709. duk_dup(ctx, -2);
  13710. /* [ ... Logger clog logfunc clog ] */
  13711. duk_push_vsprintf(ctx, fmt, ap);
  13712. /* [ ... Logger clog logfunc clog(=this) msg ] */
  13713. duk_call_method(ctx, 1 /*nargs*/);
  13714. /* [ ... Logger clog res ] */
  13715. duk_pop_3(ctx);
  13716. }
  13717. DUK_EXTERNAL void duk_log(duk_context *ctx, duk_int_t level, const char *fmt, ...) {
  13718. va_list ap;
  13719. DUK_ASSERT_CTX_VALID(ctx);
  13720. va_start(ap, fmt);
  13721. duk_log_va(ctx, level, fmt, ap);
  13722. va_end(ap);
  13723. }
  13724. #line 1 "duk_api_memory.c"
  13725. /*
  13726. * Memory calls.
  13727. */
  13728. /* include removed: duk_internal.h */
  13729. DUK_EXTERNAL void *duk_alloc_raw(duk_context *ctx, duk_size_t size) {
  13730. duk_hthread *thr = (duk_hthread *) ctx;
  13731. DUK_ASSERT_CTX_VALID(ctx);
  13732. return DUK_ALLOC_RAW(thr->heap, size);
  13733. }
  13734. DUK_EXTERNAL void duk_free_raw(duk_context *ctx, void *ptr) {
  13735. duk_hthread *thr = (duk_hthread *) ctx;
  13736. DUK_ASSERT_CTX_VALID(ctx);
  13737. DUK_FREE_RAW(thr->heap, ptr);
  13738. }
  13739. DUK_EXTERNAL void *duk_realloc_raw(duk_context *ctx, void *ptr, duk_size_t size) {
  13740. duk_hthread *thr = (duk_hthread *) ctx;
  13741. DUK_ASSERT_CTX_VALID(ctx);
  13742. return DUK_REALLOC_RAW(thr->heap, ptr, size);
  13743. }
  13744. DUK_EXTERNAL void *duk_alloc(duk_context *ctx, duk_size_t size) {
  13745. duk_hthread *thr = (duk_hthread *) ctx;
  13746. DUK_ASSERT_CTX_VALID(ctx);
  13747. return DUK_ALLOC(thr->heap, size);
  13748. }
  13749. DUK_EXTERNAL void duk_free(duk_context *ctx, void *ptr) {
  13750. duk_hthread *thr = (duk_hthread *) ctx;
  13751. DUK_ASSERT_CTX_VALID(ctx);
  13752. DUK_FREE(thr->heap, ptr);
  13753. }
  13754. DUK_EXTERNAL void *duk_realloc(duk_context *ctx, void *ptr, duk_size_t size) {
  13755. duk_hthread *thr = (duk_hthread *) ctx;
  13756. DUK_ASSERT_CTX_VALID(ctx);
  13757. /*
  13758. * Note: since this is an exposed API call, there should be
  13759. * no way a mark-and-sweep could have a side effect on the
  13760. * memory allocation behind 'ptr'; the pointer should never
  13761. * be something that Duktape wants to change.
  13762. *
  13763. * Thus, no need to use DUK_REALLOC_INDIRECT (and we don't
  13764. * have the storage location here anyway).
  13765. */
  13766. return DUK_REALLOC(thr->heap, ptr, size);
  13767. }
  13768. DUK_EXTERNAL void duk_get_memory_functions(duk_context *ctx, duk_memory_functions *out_funcs) {
  13769. duk_hthread *thr = (duk_hthread *) ctx;
  13770. duk_heap *heap;
  13771. DUK_ASSERT_CTX_VALID(ctx);
  13772. DUK_ASSERT(out_funcs != NULL);
  13773. DUK_ASSERT(thr != NULL);
  13774. DUK_ASSERT(thr->heap != NULL);
  13775. heap = thr->heap;
  13776. out_funcs->alloc_func = heap->alloc_func;
  13777. out_funcs->realloc_func = heap->realloc_func;
  13778. out_funcs->free_func = heap->free_func;
  13779. out_funcs->udata = heap->heap_udata;
  13780. }
  13781. DUK_EXTERNAL void duk_gc(duk_context *ctx, duk_uint_t flags) {
  13782. #ifdef DUK_USE_MARK_AND_SWEEP
  13783. duk_hthread *thr = (duk_hthread *) ctx;
  13784. duk_heap *heap;
  13785. DUK_UNREF(flags);
  13786. /* NULL accepted */
  13787. if (!ctx) {
  13788. return;
  13789. }
  13790. DUK_ASSERT_CTX_VALID(ctx);
  13791. heap = thr->heap;
  13792. DUK_ASSERT(heap != NULL);
  13793. DUK_D(DUK_DPRINT("mark-and-sweep requested by application"));
  13794. duk_heap_mark_and_sweep(heap, 0);
  13795. #else
  13796. DUK_D(DUK_DPRINT("mark-and-sweep requested by application but mark-and-sweep not enabled, ignoring"));
  13797. DUK_UNREF(ctx);
  13798. DUK_UNREF(flags);
  13799. #endif
  13800. }
  13801. #line 1 "duk_api_object.c"
  13802. /*
  13803. * Object handling: property access and other support functions.
  13804. */
  13805. /* include removed: duk_internal.h */
  13806. /*
  13807. * Property handling
  13808. *
  13809. * The API exposes only the most common property handling functions.
  13810. * The caller can invoke Ecmascript built-ins for full control (e.g.
  13811. * defineProperty, getOwnPropertyDescriptor).
  13812. */
  13813. DUK_EXTERNAL duk_bool_t duk_get_prop(duk_context *ctx, duk_idx_t obj_index) {
  13814. duk_hthread *thr = (duk_hthread *) ctx;
  13815. duk_tval *tv_obj;
  13816. duk_tval *tv_key;
  13817. duk_bool_t rc;
  13818. DUK_ASSERT_CTX_VALID(ctx);
  13819. /* Note: copying tv_obj and tv_key to locals to shield against a valstack
  13820. * resize is not necessary for a property get right now.
  13821. */
  13822. tv_obj = duk_require_tval(ctx, obj_index);
  13823. tv_key = duk_require_tval(ctx, -1);
  13824. rc = duk_hobject_getprop(thr, tv_obj, tv_key);
  13825. DUK_ASSERT(rc == 0 || rc == 1);
  13826. /* a value is left on stack regardless of rc */
  13827. duk_remove(ctx, -2); /* remove key */
  13828. return rc; /* 1 if property found, 0 otherwise */
  13829. }
  13830. DUK_EXTERNAL duk_bool_t duk_get_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) {
  13831. DUK_ASSERT_CTX_VALID(ctx);
  13832. DUK_ASSERT(key != NULL);
  13833. obj_index = duk_require_normalize_index(ctx, obj_index);
  13834. duk_push_string(ctx, key);
  13835. return duk_get_prop(ctx, obj_index);
  13836. }
  13837. DUK_EXTERNAL duk_bool_t duk_get_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) {
  13838. DUK_ASSERT_CTX_VALID(ctx);
  13839. obj_index = duk_require_normalize_index(ctx, obj_index);
  13840. duk_push_uarridx(ctx, arr_index);
  13841. return duk_get_prop(ctx, obj_index);
  13842. }
  13843. DUK_INTERNAL duk_bool_t duk_get_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx) {
  13844. duk_hthread *thr = (duk_hthread *) ctx;
  13845. DUK_ASSERT_CTX_VALID(ctx);
  13846. DUK_ASSERT_DISABLE(stridx >= 0);
  13847. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  13848. obj_index = duk_require_normalize_index(ctx, obj_index);
  13849. duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx));
  13850. return duk_get_prop(ctx, obj_index);
  13851. }
  13852. 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) {
  13853. duk_bool_t rc;
  13854. DUK_ASSERT_CTX_VALID(ctx);
  13855. DUK_ASSERT_DISABLE(stridx >= 0);
  13856. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  13857. rc = duk_get_prop_stridx(ctx, obj_index, stridx);
  13858. if (out_has_prop) {
  13859. *out_has_prop = rc;
  13860. }
  13861. rc = duk_to_boolean(ctx, -1);
  13862. DUK_ASSERT(rc == 0 || rc == 1);
  13863. duk_pop(ctx);
  13864. return rc;
  13865. }
  13866. DUK_EXTERNAL duk_bool_t duk_put_prop(duk_context *ctx, duk_idx_t obj_index) {
  13867. duk_hthread *thr = (duk_hthread *) ctx;
  13868. duk_tval *tv_obj;
  13869. duk_tval *tv_key;
  13870. duk_tval *tv_val;
  13871. duk_small_int_t throw_flag;
  13872. duk_bool_t rc;
  13873. DUK_ASSERT_CTX_VALID(ctx);
  13874. /* Note: copying tv_obj and tv_key to locals to shield against a valstack
  13875. * resize is not necessary for a property put right now (putprop protects
  13876. * against it internally).
  13877. */
  13878. tv_obj = duk_require_tval(ctx, obj_index);
  13879. tv_key = duk_require_tval(ctx, -2);
  13880. tv_val = duk_require_tval(ctx, -1);
  13881. throw_flag = duk_is_strict_call(ctx);
  13882. rc = duk_hobject_putprop(thr, tv_obj, tv_key, tv_val, throw_flag);
  13883. DUK_ASSERT(rc == 0 || rc == 1);
  13884. duk_pop_2(ctx); /* remove key and value */
  13885. return rc; /* 1 if property found, 0 otherwise */
  13886. }
  13887. DUK_EXTERNAL duk_bool_t duk_put_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) {
  13888. DUK_ASSERT_CTX_VALID(ctx);
  13889. DUK_ASSERT(key != NULL);
  13890. obj_index = duk_require_normalize_index(ctx, obj_index);
  13891. duk_push_string(ctx, key);
  13892. duk_swap_top(ctx, -2); /* [val key] -> [key val] */
  13893. return duk_put_prop(ctx, obj_index);
  13894. }
  13895. DUK_EXTERNAL duk_bool_t duk_put_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) {
  13896. DUK_ASSERT_CTX_VALID(ctx);
  13897. obj_index = duk_require_normalize_index(ctx, obj_index);
  13898. duk_push_uarridx(ctx, arr_index);
  13899. duk_swap_top(ctx, -2); /* [val key] -> [key val] */
  13900. return duk_put_prop(ctx, obj_index);
  13901. }
  13902. DUK_INTERNAL duk_bool_t duk_put_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx) {
  13903. duk_hthread *thr = (duk_hthread *) ctx;
  13904. DUK_ASSERT_CTX_VALID(ctx);
  13905. DUK_ASSERT_DISABLE(stridx >= 0);
  13906. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  13907. obj_index = duk_require_normalize_index(ctx, obj_index);
  13908. duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx));
  13909. duk_swap_top(ctx, -2); /* [val key] -> [key val] */
  13910. return duk_put_prop(ctx, obj_index);
  13911. }
  13912. DUK_EXTERNAL duk_bool_t duk_del_prop(duk_context *ctx, duk_idx_t obj_index) {
  13913. duk_hthread *thr = (duk_hthread *) ctx;
  13914. duk_tval *tv_obj;
  13915. duk_tval *tv_key;
  13916. duk_small_int_t throw_flag;
  13917. duk_bool_t rc;
  13918. DUK_ASSERT_CTX_VALID(ctx);
  13919. /* Note: copying tv_obj and tv_key to locals to shield against a valstack
  13920. * resize is not necessary for a property delete right now.
  13921. */
  13922. tv_obj = duk_require_tval(ctx, obj_index);
  13923. tv_key = duk_require_tval(ctx, -1);
  13924. throw_flag = duk_is_strict_call(ctx);
  13925. rc = duk_hobject_delprop(thr, tv_obj, tv_key, throw_flag);
  13926. DUK_ASSERT(rc == 0 || rc == 1);
  13927. duk_pop(ctx); /* remove key */
  13928. return rc;
  13929. }
  13930. DUK_EXTERNAL duk_bool_t duk_del_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) {
  13931. DUK_ASSERT_CTX_VALID(ctx);
  13932. DUK_ASSERT(key != NULL);
  13933. obj_index = duk_require_normalize_index(ctx, obj_index);
  13934. duk_push_string(ctx, key);
  13935. return duk_del_prop(ctx, obj_index);
  13936. }
  13937. DUK_EXTERNAL duk_bool_t duk_del_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) {
  13938. DUK_ASSERT_CTX_VALID(ctx);
  13939. obj_index = duk_require_normalize_index(ctx, obj_index);
  13940. duk_push_uarridx(ctx, arr_index);
  13941. return duk_del_prop(ctx, obj_index);
  13942. }
  13943. DUK_INTERNAL duk_bool_t duk_del_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx) {
  13944. duk_hthread *thr = (duk_hthread *) ctx;
  13945. DUK_ASSERT_CTX_VALID(ctx);
  13946. DUK_ASSERT_DISABLE(stridx >= 0);
  13947. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  13948. obj_index = duk_require_normalize_index(ctx, obj_index);
  13949. duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx));
  13950. return duk_del_prop(ctx, obj_index);
  13951. }
  13952. DUK_EXTERNAL duk_bool_t duk_has_prop(duk_context *ctx, duk_idx_t obj_index) {
  13953. duk_hthread *thr = (duk_hthread *) ctx;
  13954. duk_tval *tv_obj;
  13955. duk_tval *tv_key;
  13956. duk_bool_t rc;
  13957. DUK_ASSERT_CTX_VALID(ctx);
  13958. /* Note: copying tv_obj and tv_key to locals to shield against a valstack
  13959. * resize is not necessary for a property existence check right now.
  13960. */
  13961. tv_obj = duk_require_tval(ctx, obj_index);
  13962. tv_key = duk_require_tval(ctx, -1);
  13963. rc = duk_hobject_hasprop(thr, tv_obj, tv_key);
  13964. DUK_ASSERT(rc == 0 || rc == 1);
  13965. duk_pop(ctx); /* remove key */
  13966. return rc; /* 1 if property found, 0 otherwise */
  13967. }
  13968. DUK_EXTERNAL duk_bool_t duk_has_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) {
  13969. DUK_ASSERT_CTX_VALID(ctx);
  13970. DUK_ASSERT(key != NULL);
  13971. obj_index = duk_require_normalize_index(ctx, obj_index);
  13972. duk_push_string(ctx, key);
  13973. return duk_has_prop(ctx, obj_index);
  13974. }
  13975. DUK_EXTERNAL duk_bool_t duk_has_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) {
  13976. DUK_ASSERT_CTX_VALID(ctx);
  13977. obj_index = duk_require_normalize_index(ctx, obj_index);
  13978. duk_push_uarridx(ctx, arr_index);
  13979. return duk_has_prop(ctx, obj_index);
  13980. }
  13981. DUK_INTERNAL duk_bool_t duk_has_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx) {
  13982. duk_hthread *thr = (duk_hthread *) ctx;
  13983. DUK_ASSERT_CTX_VALID(ctx);
  13984. DUK_ASSERT_DISABLE(stridx >= 0);
  13985. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  13986. obj_index = duk_require_normalize_index(ctx, obj_index);
  13987. duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx));
  13988. return duk_has_prop(ctx, obj_index);
  13989. }
  13990. /* Define own property without inheritance looks and such. This differs from
  13991. * [[DefineOwnProperty]] because special behaviors (like Array 'length') are
  13992. * not invoked by this method. The caller must be careful to invoke any such
  13993. * behaviors if necessary.
  13994. */
  13995. DUK_INTERNAL void duk_xdef_prop(duk_context *ctx, duk_idx_t obj_index, duk_small_uint_t desc_flags) {
  13996. duk_hthread *thr = (duk_hthread *) ctx;
  13997. duk_hobject *obj;
  13998. duk_hstring *key;
  13999. DUK_ASSERT_CTX_VALID(ctx);
  14000. obj = duk_require_hobject(ctx, obj_index);
  14001. DUK_ASSERT(obj != NULL);
  14002. key = duk_to_hstring(ctx, -2);
  14003. DUK_ASSERT(key != NULL);
  14004. DUK_ASSERT(duk_require_tval(ctx, -1) != NULL);
  14005. duk_hobject_define_property_internal(thr, obj, key, desc_flags);
  14006. duk_pop(ctx); /* pop key */
  14007. }
  14008. 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) {
  14009. duk_hthread *thr = (duk_hthread *) ctx;
  14010. duk_hobject *obj;
  14011. DUK_ASSERT_CTX_VALID(ctx);
  14012. obj = duk_require_hobject(ctx, obj_index);
  14013. DUK_ASSERT(obj != NULL);
  14014. duk_hobject_define_property_internal_arridx(thr, obj, arr_index, desc_flags);
  14015. /* value popped by call */
  14016. }
  14017. 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) {
  14018. duk_hthread *thr = (duk_hthread *) ctx;
  14019. duk_hobject *obj;
  14020. duk_hstring *key;
  14021. DUK_ASSERT_CTX_VALID(ctx);
  14022. DUK_ASSERT_DISABLE(stridx >= 0);
  14023. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  14024. obj = duk_require_hobject(ctx, obj_index);
  14025. DUK_ASSERT(obj != NULL);
  14026. key = DUK_HTHREAD_GET_STRING(thr, stridx);
  14027. DUK_ASSERT(key != NULL);
  14028. DUK_ASSERT(duk_require_tval(ctx, -1) != NULL);
  14029. duk_hobject_define_property_internal(thr, obj, key, desc_flags);
  14030. /* value popped by call */
  14031. }
  14032. 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) {
  14033. duk_hthread *thr = (duk_hthread *) ctx;
  14034. duk_hobject *obj;
  14035. duk_hstring *key;
  14036. DUK_ASSERT_CTX_VALID(ctx);
  14037. DUK_ASSERT_DISABLE(stridx >= 0);
  14038. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  14039. DUK_ASSERT_DISABLE(builtin_idx >= 0);
  14040. DUK_ASSERT(builtin_idx < DUK_NUM_BUILTINS);
  14041. obj = duk_require_hobject(ctx, obj_index);
  14042. DUK_ASSERT(obj != NULL);
  14043. key = DUK_HTHREAD_GET_STRING(thr, stridx);
  14044. DUK_ASSERT(key != NULL);
  14045. duk_push_hobject(ctx, thr->builtins[builtin_idx]);
  14046. duk_hobject_define_property_internal(thr, obj, key, desc_flags);
  14047. /* value popped by call */
  14048. }
  14049. /* This is a rare property helper; it sets the global thrower (E5 Section 13.2.3)
  14050. * setter/getter into an object property. This is needed by the 'arguments'
  14051. * object creation code, function instance creation code, and Function.prototype.bind().
  14052. */
  14053. 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) {
  14054. duk_hthread *thr = (duk_hthread *) ctx;
  14055. duk_hobject *obj = duk_require_hobject(ctx, obj_index);
  14056. duk_hobject *thrower = thr->builtins[DUK_BIDX_TYPE_ERROR_THROWER];
  14057. duk_hobject_define_accessor_internal(thr, obj, DUK_HTHREAD_GET_STRING(thr, stridx), thrower, thrower, desc_flags);
  14058. }
  14059. /* Object.defineProperty() equivalent C binding. */
  14060. DUK_EXTERNAL void duk_def_prop(duk_context *ctx, duk_idx_t obj_index, duk_uint_t flags) {
  14061. duk_hthread *thr = (duk_hthread *) ctx;
  14062. duk_idx_t idx_base;
  14063. duk_hobject *obj;
  14064. duk_hstring *key;
  14065. duk_idx_t idx_value;
  14066. duk_hobject *get;
  14067. duk_hobject *set;
  14068. duk_uint_t is_data_desc;
  14069. duk_uint_t is_acc_desc;
  14070. DUK_ASSERT_CTX_VALID(ctx);
  14071. obj = duk_require_hobject(ctx, obj_index);
  14072. is_data_desc = flags & (DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE);
  14073. is_acc_desc = flags & (DUK_DEFPROP_HAVE_GETTER | DUK_DEFPROP_HAVE_SETTER);
  14074. if (is_data_desc && is_acc_desc) {
  14075. /* "Have" flags must not be conflicting so that they would
  14076. * apply to both a plain property and an accessor at the same
  14077. * time.
  14078. */
  14079. goto fail_invalid_desc;
  14080. }
  14081. idx_base = duk_get_top_index(ctx);
  14082. if (flags & DUK_DEFPROP_HAVE_SETTER) {
  14083. duk_require_type_mask(ctx, idx_base, DUK_TYPE_MASK_UNDEFINED |
  14084. DUK_TYPE_MASK_OBJECT |
  14085. DUK_TYPE_MASK_LIGHTFUNC);
  14086. set = duk_get_hobject_or_lfunc_coerce(ctx, idx_base);
  14087. if (set != NULL && !DUK_HOBJECT_IS_CALLABLE(set)) {
  14088. goto fail_not_callable;
  14089. }
  14090. idx_base--;
  14091. } else {
  14092. set = NULL;
  14093. }
  14094. if (flags & DUK_DEFPROP_HAVE_GETTER) {
  14095. duk_require_type_mask(ctx, idx_base, DUK_TYPE_MASK_UNDEFINED |
  14096. DUK_TYPE_MASK_OBJECT |
  14097. DUK_TYPE_MASK_LIGHTFUNC);
  14098. get = duk_get_hobject_or_lfunc_coerce(ctx, idx_base);
  14099. if (get != NULL && !DUK_HOBJECT_IS_CALLABLE(get)) {
  14100. goto fail_not_callable;
  14101. }
  14102. idx_base--;
  14103. } else {
  14104. get = NULL;
  14105. }
  14106. if (flags & DUK_DEFPROP_HAVE_VALUE) {
  14107. idx_value = idx_base;
  14108. idx_base--;
  14109. } else {
  14110. idx_value = (duk_idx_t) -1;
  14111. }
  14112. key = duk_require_hstring(ctx, idx_base);
  14113. duk_require_valid_index(ctx, idx_base);
  14114. duk_hobject_define_property_helper(ctx,
  14115. flags /*defprop_flags*/,
  14116. obj,
  14117. key,
  14118. idx_value,
  14119. get,
  14120. set);
  14121. /* Clean up stack */
  14122. duk_set_top(ctx, idx_base);
  14123. /* [ ... obj ... ] */
  14124. return;
  14125. fail_invalid_desc:
  14126. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_DESCRIPTOR);
  14127. return;
  14128. fail_not_callable:
  14129. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_CALLABLE);
  14130. return;
  14131. }
  14132. /*
  14133. * Object related
  14134. *
  14135. * Note: seal() and freeze() are accessible through Ecmascript bindings,
  14136. * and are not exposed through the API.
  14137. */
  14138. DUK_EXTERNAL void duk_compact(duk_context *ctx, duk_idx_t obj_index) {
  14139. duk_hthread *thr = (duk_hthread *) ctx;
  14140. duk_hobject *obj;
  14141. DUK_ASSERT_CTX_VALID(ctx);
  14142. obj = duk_get_hobject(ctx, obj_index);
  14143. if (obj) {
  14144. /* Note: this may fail, caller should protect the call if necessary */
  14145. duk_hobject_compact_props(thr, obj);
  14146. }
  14147. }
  14148. /* XXX: the duk_hobject_enum.c stack APIs should be reworked */
  14149. DUK_EXTERNAL void duk_enum(duk_context *ctx, duk_idx_t obj_index, duk_uint_t enum_flags) {
  14150. DUK_ASSERT_CTX_VALID(ctx);
  14151. duk_dup(ctx, obj_index);
  14152. duk_require_hobject_or_lfunc_coerce(ctx, -1);
  14153. duk_hobject_enumerator_create(ctx, enum_flags); /* [target] -> [enum] */
  14154. }
  14155. DUK_EXTERNAL duk_bool_t duk_next(duk_context *ctx, duk_idx_t enum_index, duk_bool_t get_value) {
  14156. DUK_ASSERT_CTX_VALID(ctx);
  14157. duk_require_hobject(ctx, enum_index);
  14158. duk_dup(ctx, enum_index);
  14159. return duk_hobject_enumerator_next(ctx, get_value);
  14160. }
  14161. /*
  14162. * Helpers for writing multiple properties
  14163. */
  14164. DUK_EXTERNAL void duk_put_function_list(duk_context *ctx, duk_idx_t obj_index, const duk_function_list_entry *funcs) {
  14165. const duk_function_list_entry *ent = funcs;
  14166. DUK_ASSERT_CTX_VALID(ctx);
  14167. obj_index = duk_require_normalize_index(ctx, obj_index);
  14168. if (ent != NULL) {
  14169. while (ent->key != NULL) {
  14170. duk_push_c_function(ctx, ent->value, ent->nargs);
  14171. duk_put_prop_string(ctx, obj_index, ent->key);
  14172. ent++;
  14173. }
  14174. }
  14175. }
  14176. DUK_EXTERNAL void duk_put_number_list(duk_context *ctx, duk_idx_t obj_index, const duk_number_list_entry *numbers) {
  14177. const duk_number_list_entry *ent = numbers;
  14178. DUK_ASSERT_CTX_VALID(ctx);
  14179. obj_index = duk_require_normalize_index(ctx, obj_index);
  14180. if (ent != NULL) {
  14181. while (ent->key != NULL) {
  14182. duk_push_number(ctx, ent->value);
  14183. duk_put_prop_string(ctx, obj_index, ent->key);
  14184. ent++;
  14185. }
  14186. }
  14187. }
  14188. /*
  14189. * Shortcut for accessing global object properties
  14190. */
  14191. DUK_EXTERNAL duk_bool_t duk_get_global_string(duk_context *ctx, const char *key) {
  14192. duk_hthread *thr = (duk_hthread *) ctx;
  14193. duk_bool_t ret;
  14194. DUK_ASSERT_CTX_VALID(ctx);
  14195. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
  14196. /* XXX: direct implementation */
  14197. duk_push_hobject(ctx, thr->builtins[DUK_BIDX_GLOBAL]);
  14198. ret = duk_get_prop_string(ctx, -1, key);
  14199. duk_remove(ctx, -2);
  14200. return ret;
  14201. }
  14202. DUK_EXTERNAL duk_bool_t duk_put_global_string(duk_context *ctx, const char *key) {
  14203. duk_hthread *thr = (duk_hthread *) ctx;
  14204. duk_bool_t ret;
  14205. DUK_ASSERT_CTX_VALID(ctx);
  14206. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
  14207. /* XXX: direct implementation */
  14208. duk_push_hobject(ctx, thr->builtins[DUK_BIDX_GLOBAL]);
  14209. duk_insert(ctx, -2);
  14210. ret = duk_put_prop_string(ctx, -2, key); /* [ ... global val ] -> [ ... global ] */
  14211. duk_pop(ctx);
  14212. return ret;
  14213. }
  14214. /*
  14215. * Object prototype
  14216. */
  14217. DUK_EXTERNAL void duk_get_prototype(duk_context *ctx, duk_idx_t index) {
  14218. duk_hthread *thr = (duk_hthread *) ctx;
  14219. duk_hobject *obj;
  14220. duk_hobject *proto;
  14221. DUK_ASSERT_CTX_VALID(ctx);
  14222. DUK_UNREF(thr);
  14223. obj = duk_require_hobject(ctx, index);
  14224. DUK_ASSERT(obj != NULL);
  14225. /* XXX: shared helper for duk_push_hobject_or_undefined()? */
  14226. proto = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, obj);
  14227. if (proto) {
  14228. duk_push_hobject(ctx, proto);
  14229. } else {
  14230. duk_push_undefined(ctx);
  14231. }
  14232. }
  14233. DUK_EXTERNAL void duk_set_prototype(duk_context *ctx, duk_idx_t index) {
  14234. duk_hthread *thr = (duk_hthread *) ctx;
  14235. duk_hobject *obj;
  14236. duk_hobject *proto;
  14237. DUK_ASSERT_CTX_VALID(ctx);
  14238. obj = duk_require_hobject(ctx, index);
  14239. DUK_ASSERT(obj != NULL);
  14240. duk_require_type_mask(ctx, -1, DUK_TYPE_MASK_UNDEFINED |
  14241. DUK_TYPE_MASK_OBJECT);
  14242. proto = duk_get_hobject(ctx, -1);
  14243. /* proto can also be NULL here (allowed explicitly) */
  14244. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, obj, proto);
  14245. duk_pop(ctx);
  14246. }
  14247. /*
  14248. * Object finalizer
  14249. */
  14250. /* XXX: these could be implemented as macros calling an internal function
  14251. * directly.
  14252. * XXX: same issue as with Duktape.fin: there's no way to delete the property
  14253. * now (just set it to undefined).
  14254. */
  14255. DUK_EXTERNAL void duk_get_finalizer(duk_context *ctx, duk_idx_t index) {
  14256. DUK_ASSERT_CTX_VALID(ctx);
  14257. duk_get_prop_stridx(ctx, index, DUK_STRIDX_INT_FINALIZER);
  14258. }
  14259. DUK_EXTERNAL void duk_set_finalizer(duk_context *ctx, duk_idx_t index) {
  14260. DUK_ASSERT_CTX_VALID(ctx);
  14261. duk_put_prop_stridx(ctx, index, DUK_STRIDX_INT_FINALIZER);
  14262. }
  14263. #line 1 "duk_api_stack.c"
  14264. /*
  14265. * API calls related to general value stack manipulation: resizing the value
  14266. * stack, pushing and popping values, type checking and reading values,
  14267. * coercing values, etc.
  14268. *
  14269. * Also contains internal functions (such as duk_get_tval()), defined
  14270. * in duk_api_internal.h, with semantics similar to the public API.
  14271. */
  14272. /* XXX: repetition of stack pre-checks -> helper or macro or inline */
  14273. /* XXX: shared api error strings, and perhaps even throw code for rare cases? */
  14274. /* include removed: duk_internal.h */
  14275. /*
  14276. * Forward declarations
  14277. */
  14278. 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);
  14279. /*
  14280. * Global state for working around missing variadic macros
  14281. */
  14282. #ifndef DUK_USE_VARIADIC_MACROS
  14283. DUK_EXTERNAL const char *duk_api_global_filename = NULL;
  14284. DUK_EXTERNAL duk_int_t duk_api_global_line = 0;
  14285. #endif
  14286. /*
  14287. * Helpers
  14288. */
  14289. #if defined(DUK_USE_VALSTACK_UNSAFE)
  14290. /* Faster but value stack overruns are memory unsafe. */
  14291. #define DUK__CHECK_SPACE() do { \
  14292. DUK_ASSERT(!(thr->valstack_top >= thr->valstack_end)); \
  14293. } while (0)
  14294. #else
  14295. #define DUK__CHECK_SPACE() do { \
  14296. if (thr->valstack_top >= thr->valstack_end) { \
  14297. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK); \
  14298. } \
  14299. } while (0)
  14300. #endif
  14301. DUK_LOCAL duk_int_t duk__api_coerce_d2i(duk_context *ctx, duk_idx_t index, duk_bool_t require) {
  14302. duk_hthread *thr;
  14303. duk_tval *tv;
  14304. duk_small_int_t c;
  14305. duk_double_t d;
  14306. thr = (duk_hthread *) ctx;
  14307. tv = duk_get_tval(ctx, index);
  14308. if (tv == NULL) {
  14309. goto error_notnumber;
  14310. }
  14311. /*
  14312. * Special cases like NaN and +/- Infinity are handled explicitly
  14313. * because a plain C coercion from double to int handles these cases
  14314. * in undesirable ways. For instance, NaN may coerce to INT_MIN
  14315. * (not zero), and INT_MAX + 1 may coerce to INT_MIN (not INT_MAX).
  14316. *
  14317. * This double-to-int coercion differs from ToInteger() because it
  14318. * has a finite range (ToInteger() allows e.g. +/- Infinity). It
  14319. * also differs from ToInt32() because the INT_MIN/INT_MAX clamping
  14320. * depends on the size of the int type on the platform. In particular,
  14321. * on platforms with a 64-bit int type, the full range is allowed.
  14322. */
  14323. #if defined(DUK_USE_FASTINT)
  14324. if (DUK_TVAL_IS_FASTINT(tv)) {
  14325. duk_int64_t t = DUK_TVAL_GET_FASTINT(tv);
  14326. #if (DUK_INT_MAX <= 0x7fffffffL)
  14327. /* Clamping only necessary for 32-bit ints. */
  14328. if (t < DUK_INT_MIN) {
  14329. t = DUK_INT_MIN;
  14330. } else if (t > DUK_INT_MAX) {
  14331. t = DUK_INT_MAX;
  14332. }
  14333. #endif
  14334. return (duk_int_t) t;
  14335. }
  14336. #endif
  14337. if (DUK_TVAL_IS_NUMBER(tv)) {
  14338. d = DUK_TVAL_GET_NUMBER(tv);
  14339. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  14340. if (c == DUK_FP_NAN) {
  14341. return 0;
  14342. } else if (d < (duk_double_t) DUK_INT_MIN) {
  14343. /* covers -Infinity */
  14344. return DUK_INT_MIN;
  14345. } else if (d > (duk_double_t) DUK_INT_MAX) {
  14346. /* covers +Infinity */
  14347. return DUK_INT_MAX;
  14348. } else {
  14349. /* coerce towards zero */
  14350. return (duk_int_t) d;
  14351. }
  14352. }
  14353. error_notnumber:
  14354. if (require) {
  14355. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_NUMBER);
  14356. } else {
  14357. return 0;
  14358. }
  14359. }
  14360. DUK_LOCAL duk_uint_t duk__api_coerce_d2ui(duk_context *ctx, duk_idx_t index, duk_bool_t require) {
  14361. duk_hthread *thr;
  14362. duk_tval *tv;
  14363. duk_small_int_t c;
  14364. duk_double_t d;
  14365. /* Same as above but for unsigned int range. */
  14366. thr = (duk_hthread *) ctx;
  14367. tv = duk_get_tval(ctx, index);
  14368. if (tv == NULL) {
  14369. goto error_notnumber;
  14370. }
  14371. #if defined(DUK_USE_FASTINT)
  14372. if (DUK_TVAL_IS_FASTINT(tv)) {
  14373. duk_int64_t t = DUK_TVAL_GET_FASTINT(tv);
  14374. if (t < 0) {
  14375. t = 0;
  14376. }
  14377. #if (DUK_UINT_MAX <= 0xffffffffUL)
  14378. /* Clamping only necessary for 32-bit ints. */
  14379. else if (t > DUK_UINT_MAX) {
  14380. t = DUK_UINT_MAX;
  14381. }
  14382. #endif
  14383. return (duk_uint_t) t;
  14384. }
  14385. #endif
  14386. if (DUK_TVAL_IS_NUMBER(tv)) {
  14387. d = DUK_TVAL_GET_NUMBER(tv);
  14388. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  14389. if (c == DUK_FP_NAN) {
  14390. return 0;
  14391. } else if (d < 0.0) {
  14392. /* covers -Infinity */
  14393. return (duk_uint_t) 0;
  14394. } else if (d > (duk_double_t) DUK_UINT_MAX) {
  14395. /* covers +Infinity */
  14396. return (duk_uint_t) DUK_UINT_MAX;
  14397. } else {
  14398. /* coerce towards zero */
  14399. return (duk_uint_t) d;
  14400. }
  14401. }
  14402. error_notnumber:
  14403. if (require) {
  14404. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_NUMBER);
  14405. } else {
  14406. return 0;
  14407. }
  14408. }
  14409. /*
  14410. * Stack index validation/normalization and getting a stack duk_tval ptr.
  14411. *
  14412. * These are called by many API entrypoints so the implementations must be
  14413. * fast and "inlined".
  14414. *
  14415. * There's some repetition because of this; keep the functions in sync.
  14416. */
  14417. DUK_EXTERNAL duk_idx_t duk_normalize_index(duk_context *ctx, duk_idx_t index) {
  14418. duk_hthread *thr = (duk_hthread *) ctx;
  14419. duk_idx_t vs_size;
  14420. DUK_ASSERT_CTX_VALID(ctx);
  14421. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  14422. /* Care must be taken to avoid pointer wrapping in the index
  14423. * validation. For instance, on a 32-bit platform with 8-byte
  14424. * duk_tval the index 0x20000000UL would wrap the memory space
  14425. * once.
  14426. */
  14427. /* Assume value stack sizes (in elements) fits into duk_idx_t. */
  14428. vs_size = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  14429. DUK_ASSERT(vs_size >= 0);
  14430. if (index < 0) {
  14431. index = vs_size + index;
  14432. if (DUK_UNLIKELY(index < 0)) {
  14433. /* Also catches index == DUK_INVALID_INDEX: vs_size >= 0
  14434. * so that vs_size + DUK_INVALID_INDEX cannot underflow
  14435. * and will always be negative.
  14436. */
  14437. return DUK_INVALID_INDEX;
  14438. }
  14439. } else {
  14440. /* since index non-negative */
  14441. DUK_ASSERT(index != DUK_INVALID_INDEX);
  14442. if (DUK_UNLIKELY(index >= vs_size)) {
  14443. return DUK_INVALID_INDEX;
  14444. }
  14445. }
  14446. DUK_ASSERT(index >= 0);
  14447. DUK_ASSERT(index < vs_size);
  14448. return index;
  14449. }
  14450. DUK_EXTERNAL duk_idx_t duk_require_normalize_index(duk_context *ctx, duk_idx_t index) {
  14451. duk_hthread *thr = (duk_hthread *) ctx;
  14452. duk_idx_t vs_size;
  14453. DUK_ASSERT_CTX_VALID(ctx);
  14454. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  14455. vs_size = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  14456. DUK_ASSERT(vs_size >= 0);
  14457. if (index < 0) {
  14458. index = vs_size + index;
  14459. if (DUK_UNLIKELY(index < 0)) {
  14460. goto invalid_index;
  14461. }
  14462. } else {
  14463. DUK_ASSERT(index != DUK_INVALID_INDEX);
  14464. if (DUK_UNLIKELY(index >= vs_size)) {
  14465. goto invalid_index;
  14466. }
  14467. }
  14468. DUK_ASSERT(index >= 0);
  14469. DUK_ASSERT(index < vs_size);
  14470. return index;
  14471. invalid_index:
  14472. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_INDEX);
  14473. return 0; /* unreachable */
  14474. }
  14475. DUK_INTERNAL duk_tval *duk_get_tval(duk_context *ctx, duk_idx_t index) {
  14476. duk_hthread *thr = (duk_hthread *) ctx;
  14477. duk_idx_t vs_size;
  14478. DUK_ASSERT_CTX_VALID(ctx);
  14479. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  14480. vs_size = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  14481. DUK_ASSERT(vs_size >= 0);
  14482. if (index < 0) {
  14483. index = vs_size + index;
  14484. if (DUK_UNLIKELY(index < 0)) {
  14485. return NULL;
  14486. }
  14487. } else {
  14488. DUK_ASSERT(index != DUK_INVALID_INDEX);
  14489. if (DUK_UNLIKELY(index >= vs_size)) {
  14490. return NULL;
  14491. }
  14492. }
  14493. DUK_ASSERT(index >= 0);
  14494. DUK_ASSERT(index < vs_size);
  14495. return thr->valstack_bottom + index;
  14496. }
  14497. DUK_INTERNAL duk_tval *duk_require_tval(duk_context *ctx, duk_idx_t index) {
  14498. duk_hthread *thr = (duk_hthread *) ctx;
  14499. duk_idx_t vs_size;
  14500. DUK_ASSERT_CTX_VALID(ctx);
  14501. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  14502. vs_size = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  14503. DUK_ASSERT(vs_size >= 0);
  14504. if (index < 0) {
  14505. index = vs_size + index;
  14506. if (DUK_UNLIKELY(index < 0)) {
  14507. goto invalid_index;
  14508. }
  14509. } else {
  14510. DUK_ASSERT(index != DUK_INVALID_INDEX);
  14511. if (DUK_UNLIKELY(index >= vs_size)) {
  14512. goto invalid_index;
  14513. }
  14514. }
  14515. DUK_ASSERT(index >= 0);
  14516. DUK_ASSERT(index < vs_size);
  14517. return thr->valstack_bottom + index;
  14518. invalid_index:
  14519. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_INDEX);
  14520. return NULL;
  14521. }
  14522. /* Non-critical. */
  14523. DUK_EXTERNAL duk_bool_t duk_is_valid_index(duk_context *ctx, duk_idx_t index) {
  14524. DUK_ASSERT_CTX_VALID(ctx);
  14525. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  14526. return (duk_normalize_index(ctx, index) >= 0);
  14527. }
  14528. /* Non-critical. */
  14529. DUK_EXTERNAL void duk_require_valid_index(duk_context *ctx, duk_idx_t index) {
  14530. duk_hthread *thr = (duk_hthread *) ctx;
  14531. DUK_ASSERT_CTX_VALID(ctx);
  14532. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  14533. if (duk_normalize_index(ctx, index) < 0) {
  14534. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_INDEX);
  14535. }
  14536. }
  14537. /*
  14538. * Value stack top handling
  14539. */
  14540. DUK_EXTERNAL duk_idx_t duk_get_top(duk_context *ctx) {
  14541. duk_hthread *thr = (duk_hthread *) ctx;
  14542. DUK_ASSERT_CTX_VALID(ctx);
  14543. return (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  14544. }
  14545. /* set stack top within currently allocated range, but don't reallocate */
  14546. DUK_EXTERNAL void duk_set_top(duk_context *ctx, duk_idx_t index) {
  14547. duk_hthread *thr = (duk_hthread *) ctx;
  14548. duk_idx_t vs_size;
  14549. duk_idx_t vs_limit;
  14550. duk_idx_t count;
  14551. duk_tval tv_tmp;
  14552. duk_tval *tv;
  14553. DUK_ASSERT_CTX_VALID(ctx);
  14554. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  14555. vs_size = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  14556. vs_limit = (duk_idx_t) (thr->valstack_end - thr->valstack_bottom);
  14557. if (index < 0) {
  14558. /* Negative indices are always within allocated stack but
  14559. * must not go below zero index.
  14560. */
  14561. index = vs_size + index;
  14562. if (index < 0) {
  14563. /* Also catches index == DUK_INVALID_INDEX. */
  14564. goto invalid_index;
  14565. }
  14566. } else {
  14567. /* Positive index can be higher than valstack top but must
  14568. * not go above allocated stack (equality is OK).
  14569. */
  14570. if (index > vs_limit) {
  14571. goto invalid_index;
  14572. }
  14573. }
  14574. DUK_ASSERT(index >= 0);
  14575. DUK_ASSERT(index <= vs_limit);
  14576. if (index >= vs_size) {
  14577. /* Stack size increases or stays the same. Fill the new
  14578. * entries (if any) with undefined. No pointer stability
  14579. * issues here so we can use a running pointer.
  14580. */
  14581. tv = thr->valstack_top;
  14582. count = index - vs_size;
  14583. DUK_ASSERT(count >= 0);
  14584. while (count > 0) {
  14585. /* no need to decref previous or new value */
  14586. count--;
  14587. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED_UNUSED(tv));
  14588. DUK_TVAL_SET_UNDEFINED_ACTUAL(tv);
  14589. tv++;
  14590. }
  14591. thr->valstack_top = tv;
  14592. } else {
  14593. /* Stack size decreases, DECREF entries which are above the
  14594. * new top. Each DECREF potentially invalidates valstack
  14595. * pointers, so don't hold on to pointers. The valstack top
  14596. * must also be updated on every loop in case a GC is triggered.
  14597. */
  14598. /* XXX: Here it would be useful to have a DECREF macro which
  14599. * doesn't need a NULL check, and does refzero queueing without
  14600. * running the refzero algorithm. There would be no pointer
  14601. * instability in this case, and code could be inlined. After
  14602. * the loop, one call to refzero would be needed.
  14603. */
  14604. count = vs_size - index;
  14605. DUK_ASSERT(count > 0);
  14606. while (count > 0) {
  14607. count--;
  14608. tv = --thr->valstack_top; /* tv -> value just before prev top value */
  14609. DUK_ASSERT(tv >= thr->valstack_bottom);
  14610. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  14611. DUK_TVAL_SET_UNDEFINED_UNUSED(tv);
  14612. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  14613. /* XXX: fast primitive to set a bunch of values to UNDEFINED_UNUSED */
  14614. }
  14615. }
  14616. return;
  14617. invalid_index:
  14618. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_INDEX);
  14619. }
  14620. DUK_EXTERNAL duk_idx_t duk_get_top_index(duk_context *ctx) {
  14621. duk_hthread *thr = (duk_hthread *) ctx;
  14622. duk_idx_t ret;
  14623. DUK_ASSERT_CTX_VALID(ctx);
  14624. ret = ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1;
  14625. if (DUK_UNLIKELY(ret < 0)) {
  14626. /* Return invalid index; if caller uses this without checking
  14627. * in another API call, the index won't map to a valid stack
  14628. * entry.
  14629. */
  14630. return DUK_INVALID_INDEX;
  14631. }
  14632. return ret;
  14633. }
  14634. DUK_EXTERNAL duk_idx_t duk_require_top_index(duk_context *ctx) {
  14635. duk_hthread *thr = (duk_hthread *) ctx;
  14636. duk_idx_t ret;
  14637. DUK_ASSERT_CTX_VALID(ctx);
  14638. ret = ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1;
  14639. if (DUK_UNLIKELY(ret < 0)) {
  14640. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_INDEX);
  14641. }
  14642. return ret;
  14643. }
  14644. /*
  14645. * Value stack resizing.
  14646. *
  14647. * This resizing happens above the current "top": the value stack can be
  14648. * grown or shrunk, but the "top" is not affected. The value stack cannot
  14649. * be resized to a size below the current "top".
  14650. *
  14651. * The low level reallocation primitive must carefully recompute all value
  14652. * stack pointers, and must also work if ALL pointers are NULL. The resize
  14653. * is quite tricky because the valstack realloc may cause a mark-and-sweep,
  14654. * which may run finalizers. Running finalizers may resize the valstack
  14655. * recursively (the same value stack we're working on). So, after realloc
  14656. * returns, we know that the valstack "top" should still be the same (there
  14657. * should not be live values above the "top"), but its underlying size and
  14658. * pointer may have changed.
  14659. */
  14660. /* XXX: perhaps refactor this to allow caller to specify some parameters, or
  14661. * at least a 'compact' flag which skips any spare or round-up .. useful for
  14662. * emergency gc.
  14663. */
  14664. DUK_LOCAL duk_bool_t duk__resize_valstack(duk_context *ctx, duk_size_t new_size) {
  14665. duk_hthread *thr = (duk_hthread *) ctx;
  14666. duk_ptrdiff_t old_bottom_offset;
  14667. duk_ptrdiff_t old_top_offset;
  14668. duk_ptrdiff_t old_end_offset_post;
  14669. #ifdef DUK_USE_DEBUG
  14670. duk_ptrdiff_t old_end_offset_pre;
  14671. duk_tval *old_valstack_pre;
  14672. duk_tval *old_valstack_post;
  14673. #endif
  14674. duk_tval *new_valstack;
  14675. duk_tval *p;
  14676. duk_size_t new_alloc_size;
  14677. DUK_ASSERT_CTX_VALID(ctx);
  14678. DUK_ASSERT(thr != NULL);
  14679. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  14680. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  14681. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  14682. DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack) <= new_size); /* can't resize below 'top' */
  14683. DUK_ASSERT(new_size <= thr->valstack_max); /* valstack limit caller has check, prevents wrapping */
  14684. DUK_ASSERT(new_size <= DUK_SIZE_MAX / sizeof(duk_tval)); /* specific assert for wrapping */
  14685. /* get pointer offsets for tweaking below */
  14686. old_bottom_offset = (((duk_uint8_t *) thr->valstack_bottom) - ((duk_uint8_t *) thr->valstack));
  14687. old_top_offset = (((duk_uint8_t *) thr->valstack_top) - ((duk_uint8_t *) thr->valstack));
  14688. #ifdef DUK_USE_DEBUG
  14689. old_end_offset_pre = (((duk_uint8_t *) thr->valstack_end) - ((duk_uint8_t *) thr->valstack)); /* not very useful, used for debugging */
  14690. old_valstack_pre = thr->valstack;
  14691. #endif
  14692. /* Allocate a new valstack.
  14693. *
  14694. * Note: cannot use a plain DUK_REALLOC() because a mark-and-sweep may
  14695. * invalidate the original thr->valstack base pointer inside the realloc
  14696. * process. See doc/memory-management.txt.
  14697. */
  14698. new_alloc_size = sizeof(duk_tval) * new_size;
  14699. new_valstack = (duk_tval *) DUK_REALLOC_INDIRECT(thr->heap, duk_hthread_get_valstack_ptr, (void *) thr, new_alloc_size);
  14700. if (!new_valstack) {
  14701. /* Because new_size != 0, if condition doesn't need to be
  14702. * (new_valstack != NULL || new_size == 0).
  14703. */
  14704. DUK_ASSERT(new_size != 0);
  14705. DUK_D(DUK_DPRINT("failed to resize valstack to %lu entries (%lu bytes)",
  14706. (unsigned long) new_size, (unsigned long) new_alloc_size));
  14707. return 0;
  14708. }
  14709. /* Note: the realloc may have triggered a mark-and-sweep which may
  14710. * have resized our valstack internally. However, the mark-and-sweep
  14711. * MUST NOT leave the stack bottom/top in a different state. Particular
  14712. * assumptions and facts:
  14713. *
  14714. * - The thr->valstack pointer may be different after realloc,
  14715. * and the offset between thr->valstack_end <-> thr->valstack
  14716. * may have changed.
  14717. * - The offset between thr->valstack_bottom <-> thr->valstack
  14718. * and thr->valstack_top <-> thr->valstack MUST NOT have changed,
  14719. * because mark-and-sweep must adhere to a strict stack policy.
  14720. * In other words, logical bottom and top MUST NOT have changed.
  14721. * - All values above the top are unreachable but are initialized
  14722. * to UNDEFINED_UNUSED, up to the post-realloc valstack_end.
  14723. * - 'old_end_offset' must be computed after realloc to be correct.
  14724. */
  14725. DUK_ASSERT((((duk_uint8_t *) thr->valstack_bottom) - ((duk_uint8_t *) thr->valstack)) == old_bottom_offset);
  14726. DUK_ASSERT((((duk_uint8_t *) thr->valstack_top) - ((duk_uint8_t *) thr->valstack)) == old_top_offset);
  14727. /* success, fixup pointers */
  14728. old_end_offset_post = (((duk_uint8_t *) thr->valstack_end) - ((duk_uint8_t *) thr->valstack)); /* must be computed after realloc */
  14729. #ifdef DUK_USE_DEBUG
  14730. old_valstack_post = thr->valstack;
  14731. #endif
  14732. thr->valstack = new_valstack;
  14733. thr->valstack_end = new_valstack + new_size;
  14734. thr->valstack_bottom = (duk_tval *) ((duk_uint8_t *) new_valstack + old_bottom_offset);
  14735. thr->valstack_top = (duk_tval *) ((duk_uint8_t *) new_valstack + old_top_offset);
  14736. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  14737. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  14738. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  14739. /* useful for debugging */
  14740. #ifdef DUK_USE_DEBUG
  14741. if (old_end_offset_pre != old_end_offset_post) {
  14742. DUK_D(DUK_DPRINT("valstack was resized during valstack_resize(), probably by mark-and-sweep; "
  14743. "end offset changed: %lu -> %lu",
  14744. (unsigned long) old_end_offset_pre,
  14745. (unsigned long) old_end_offset_post));
  14746. }
  14747. if (old_valstack_pre != old_valstack_post) {
  14748. DUK_D(DUK_DPRINT("valstack pointer changed during valstack_resize(), probably by mark-and-sweep: %p -> %p",
  14749. (void *) old_valstack_pre,
  14750. (void *) old_valstack_post));
  14751. }
  14752. #endif
  14753. DUK_DD(DUK_DDPRINT("resized valstack to %lu elements (%lu bytes), bottom=%ld, top=%ld, "
  14754. "new pointers: start=%p end=%p bottom=%p top=%p",
  14755. (unsigned long) new_size, (unsigned long) new_alloc_size,
  14756. (long) (thr->valstack_bottom - thr->valstack),
  14757. (long) (thr->valstack_top - thr->valstack),
  14758. (void *) thr->valstack, (void *) thr->valstack_end,
  14759. (void *) thr->valstack_bottom, (void *) thr->valstack_top));
  14760. /* init newly allocated slots (only) */
  14761. p = (duk_tval *) ((duk_uint8_t *) thr->valstack + old_end_offset_post);
  14762. while (p < thr->valstack_end) {
  14763. /* never executed if new size is smaller */
  14764. DUK_TVAL_SET_UNDEFINED_UNUSED(p);
  14765. p++;
  14766. }
  14767. /* assertion check: we maintain elements above top in known state */
  14768. #ifdef DUK_USE_ASSERTIONS
  14769. p = thr->valstack_top;
  14770. while (p < thr->valstack_end) {
  14771. /* everything above old valstack top should be preinitialized now */
  14772. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED_UNUSED(p));
  14773. p++;
  14774. }
  14775. #endif
  14776. return 1;
  14777. }
  14778. DUK_INTERNAL
  14779. duk_bool_t duk_valstack_resize_raw(duk_context *ctx,
  14780. duk_size_t min_new_size,
  14781. duk_small_uint_t flags) {
  14782. duk_hthread *thr = (duk_hthread *) ctx;
  14783. duk_size_t old_size;
  14784. duk_size_t new_size;
  14785. duk_bool_t is_shrink = 0;
  14786. duk_small_uint_t shrink_flag = (flags & DUK_VSRESIZE_FLAG_SHRINK);
  14787. duk_small_uint_t compact_flag = (flags & DUK_VSRESIZE_FLAG_COMPACT);
  14788. duk_small_uint_t throw_flag = (flags & DUK_VSRESIZE_FLAG_THROW);
  14789. DUK_DDD(DUK_DDDPRINT("check valstack resize: min_new_size=%lu, curr_size=%ld, curr_top=%ld, "
  14790. "curr_bottom=%ld, shrink=%d, compact=%d, throw=%d",
  14791. (unsigned long) min_new_size,
  14792. (long) (thr->valstack_end - thr->valstack),
  14793. (long) (thr->valstack_top - thr->valstack),
  14794. (long) (thr->valstack_bottom - thr->valstack),
  14795. (int) shrink_flag, (int) compact_flag, (int) throw_flag));
  14796. DUK_ASSERT_CTX_VALID(ctx);
  14797. DUK_ASSERT(thr != NULL);
  14798. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  14799. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  14800. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  14801. old_size = (duk_size_t) (thr->valstack_end - thr->valstack);
  14802. if (min_new_size <= old_size) {
  14803. is_shrink = 1;
  14804. if (!shrink_flag ||
  14805. old_size - min_new_size < DUK_VALSTACK_SHRINK_THRESHOLD) {
  14806. DUK_DDD(DUK_DDDPRINT("no need to grow or shrink valstack"));
  14807. return 1;
  14808. }
  14809. }
  14810. new_size = min_new_size;
  14811. if (!compact_flag) {
  14812. if (is_shrink) {
  14813. /* shrink case; leave some spare */
  14814. new_size += DUK_VALSTACK_SHRINK_SPARE;
  14815. }
  14816. /* round up roughly to next 'grow step' */
  14817. new_size = (new_size / DUK_VALSTACK_GROW_STEP + 1) * DUK_VALSTACK_GROW_STEP;
  14818. }
  14819. DUK_DD(DUK_DDPRINT("want to %s valstack: %lu -> %lu elements (min_new_size %lu)",
  14820. (const char *) (new_size > old_size ? "grow" : "shrink"),
  14821. (unsigned long) old_size, (unsigned long) new_size,
  14822. (unsigned long) min_new_size));
  14823. if (new_size > thr->valstack_max) {
  14824. /* Note: may be triggered even if minimal new_size would not reach the limit,
  14825. * plan limit accordingly (taking DUK_VALSTACK_GROW_STEP into account).
  14826. */
  14827. if (throw_flag) {
  14828. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_VALSTACK_LIMIT);
  14829. } else {
  14830. return 0;
  14831. }
  14832. }
  14833. /*
  14834. * When resizing the valstack, a mark-and-sweep may be triggered for
  14835. * the allocation of the new valstack. If the mark-and-sweep needs
  14836. * to use our thread for something, it may cause *the same valstack*
  14837. * to be resized recursively. This happens e.g. when mark-and-sweep
  14838. * finalizers are called. This is taken into account carefully in
  14839. * duk__resize_valstack().
  14840. *
  14841. * 'new_size' is known to be <= valstack_max, which ensures that
  14842. * size_t and pointer arithmetic won't wrap in duk__resize_valstack().
  14843. */
  14844. if (!duk__resize_valstack(ctx, new_size)) {
  14845. if (is_shrink) {
  14846. DUK_DD(DUK_DDPRINT("valstack resize failed, but is a shrink, ignore"));
  14847. return 1;
  14848. }
  14849. DUK_DD(DUK_DDPRINT("valstack resize failed"));
  14850. if (throw_flag) {
  14851. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_FAILED_TO_EXTEND_VALSTACK);
  14852. } else {
  14853. return 0;
  14854. }
  14855. }
  14856. DUK_DDD(DUK_DDDPRINT("valstack resize successful"));
  14857. return 1;
  14858. }
  14859. DUK_EXTERNAL duk_bool_t duk_check_stack(duk_context *ctx, duk_idx_t extra) {
  14860. duk_hthread *thr = (duk_hthread *) ctx;
  14861. duk_size_t min_new_size;
  14862. DUK_ASSERT_CTX_VALID(ctx);
  14863. DUK_ASSERT(thr != NULL);
  14864. if (DUK_UNLIKELY(extra < 0)) {
  14865. /* Clamping to zero makes the API more robust to calling code
  14866. * calculation errors.
  14867. */
  14868. extra = 0;
  14869. }
  14870. min_new_size = (thr->valstack_top - thr->valstack) + extra + DUK_VALSTACK_INTERNAL_EXTRA;
  14871. return duk_valstack_resize_raw(ctx,
  14872. min_new_size, /* min_new_size */
  14873. 0 /* no shrink */ | /* flags */
  14874. 0 /* no compact */ |
  14875. 0 /* no throw */);
  14876. }
  14877. DUK_EXTERNAL void duk_require_stack(duk_context *ctx, duk_idx_t extra) {
  14878. duk_hthread *thr = (duk_hthread *) ctx;
  14879. duk_size_t min_new_size;
  14880. DUK_ASSERT_CTX_VALID(ctx);
  14881. DUK_ASSERT(thr != NULL);
  14882. if (DUK_UNLIKELY(extra < 0)) {
  14883. /* Clamping to zero makes the API more robust to calling code
  14884. * calculation errors.
  14885. */
  14886. extra = 0;
  14887. }
  14888. min_new_size = (thr->valstack_top - thr->valstack) + extra + DUK_VALSTACK_INTERNAL_EXTRA;
  14889. (void) duk_valstack_resize_raw(ctx,
  14890. min_new_size, /* min_new_size */
  14891. 0 /* no shrink */ | /* flags */
  14892. 0 /* no compact */ |
  14893. DUK_VSRESIZE_FLAG_THROW);
  14894. }
  14895. DUK_EXTERNAL duk_bool_t duk_check_stack_top(duk_context *ctx, duk_idx_t top) {
  14896. duk_size_t min_new_size;
  14897. DUK_ASSERT_CTX_VALID(ctx);
  14898. if (DUK_UNLIKELY(top < 0)) {
  14899. /* Clamping to zero makes the API more robust to calling code
  14900. * calculation errors.
  14901. */
  14902. top = 0;
  14903. }
  14904. min_new_size = top + DUK_VALSTACK_INTERNAL_EXTRA;
  14905. return duk_valstack_resize_raw(ctx,
  14906. min_new_size, /* min_new_size */
  14907. 0 /* no shrink */ | /* flags */
  14908. 0 /* no compact */ |
  14909. 0 /* no throw */);
  14910. }
  14911. DUK_EXTERNAL void duk_require_stack_top(duk_context *ctx, duk_idx_t top) {
  14912. duk_size_t min_new_size;
  14913. DUK_ASSERT_CTX_VALID(ctx);
  14914. if (DUK_UNLIKELY(top < 0)) {
  14915. /* Clamping to zero makes the API more robust to calling code
  14916. * calculation errors.
  14917. */
  14918. top = 0;
  14919. }
  14920. min_new_size = top + DUK_VALSTACK_INTERNAL_EXTRA;
  14921. (void) duk_valstack_resize_raw(ctx,
  14922. min_new_size, /* min_new_size */
  14923. 0 /* no shrink */ | /* flags */
  14924. 0 /* no compact */ |
  14925. DUK_VSRESIZE_FLAG_THROW);
  14926. }
  14927. /*
  14928. * Basic stack manipulation: swap, dup, insert, replace, etc
  14929. */
  14930. DUK_EXTERNAL void duk_swap(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) {
  14931. duk_tval *tv1;
  14932. duk_tval *tv2;
  14933. duk_tval tv_tmp;
  14934. DUK_ASSERT_CTX_VALID(ctx);
  14935. tv1 = duk_require_tval(ctx, index1);
  14936. DUK_ASSERT(tv1 != NULL);
  14937. tv2 = duk_require_tval(ctx, index2);
  14938. DUK_ASSERT(tv2 != NULL);
  14939. /* If tv1==tv2 this is a NOP, no check is needed */
  14940. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  14941. DUK_TVAL_SET_TVAL(tv1, tv2);
  14942. DUK_TVAL_SET_TVAL(tv2, &tv_tmp);
  14943. }
  14944. DUK_EXTERNAL void duk_swap_top(duk_context *ctx, duk_idx_t index) {
  14945. DUK_ASSERT_CTX_VALID(ctx);
  14946. duk_swap(ctx, index, -1);
  14947. }
  14948. DUK_EXTERNAL void duk_dup(duk_context *ctx, duk_idx_t from_index) {
  14949. duk_hthread *thr;
  14950. duk_tval *tv_from;
  14951. duk_tval *tv_to;
  14952. DUK_ASSERT_CTX_VALID(ctx);
  14953. thr = (duk_hthread *) ctx;
  14954. DUK__CHECK_SPACE();
  14955. tv_from = duk_require_tval(ctx, from_index);
  14956. tv_to = thr->valstack_top++;
  14957. DUK_ASSERT(tv_from != NULL);
  14958. DUK_ASSERT(tv_to != NULL);
  14959. DUK_TVAL_SET_TVAL(tv_to, tv_from);
  14960. DUK_TVAL_INCREF(thr, tv_to); /* no side effects */
  14961. }
  14962. DUK_EXTERNAL void duk_dup_top(duk_context *ctx) {
  14963. duk_hthread *thr;
  14964. duk_tval *tv_from;
  14965. duk_tval *tv_to;
  14966. DUK_ASSERT_CTX_VALID(ctx);
  14967. thr = (duk_hthread *) ctx;
  14968. DUK__CHECK_SPACE();
  14969. if (thr->valstack_top - thr->valstack_bottom <= 0) {
  14970. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_INDEX);
  14971. }
  14972. tv_from = thr->valstack_top - 1;
  14973. tv_to = thr->valstack_top++;
  14974. DUK_ASSERT(tv_from != NULL);
  14975. DUK_ASSERT(tv_to != NULL);
  14976. DUK_TVAL_SET_TVAL(tv_to, tv_from);
  14977. DUK_TVAL_INCREF(thr, tv_to); /* no side effects */
  14978. }
  14979. DUK_EXTERNAL void duk_insert(duk_context *ctx, duk_idx_t to_index) {
  14980. duk_tval *p;
  14981. duk_tval *q;
  14982. duk_tval tv_tmp;
  14983. duk_size_t nbytes;
  14984. DUK_ASSERT_CTX_VALID(ctx);
  14985. p = duk_require_tval(ctx, to_index);
  14986. DUK_ASSERT(p != NULL);
  14987. q = duk_require_tval(ctx, -1);
  14988. DUK_ASSERT(q != NULL);
  14989. DUK_ASSERT(q >= p);
  14990. /* nbytes
  14991. * <--------->
  14992. * [ ... | p | x | x | q ]
  14993. * => [ ... | q | p | x | x ]
  14994. */
  14995. nbytes = (duk_size_t) (((duk_uint8_t *) q) - ((duk_uint8_t *) p)); /* Note: 'q' is top-1 */
  14996. DUK_DDD(DUK_DDDPRINT("duk_insert: to_index=%ld, p=%p, q=%p, nbytes=%lu",
  14997. (long) to_index, (void *) p, (void *) q, (unsigned long) nbytes));
  14998. /* No net refcount changes. */
  14999. if (nbytes > 0) {
  15000. DUK_TVAL_SET_TVAL(&tv_tmp, q);
  15001. DUK_ASSERT(nbytes > 0);
  15002. DUK_MEMMOVE((void *) (p + 1), (void *) p, nbytes);
  15003. DUK_TVAL_SET_TVAL(p, &tv_tmp);
  15004. } else {
  15005. /* nop: insert top to top */
  15006. DUK_ASSERT(nbytes == 0);
  15007. DUK_ASSERT(p == q);
  15008. }
  15009. }
  15010. DUK_EXTERNAL void duk_replace(duk_context *ctx, duk_idx_t to_index) {
  15011. duk_hthread *thr = (duk_hthread *) ctx;
  15012. duk_tval *tv1;
  15013. duk_tval *tv2;
  15014. duk_tval tv_tmp;
  15015. DUK_ASSERT_CTX_VALID(ctx);
  15016. tv1 = duk_require_tval(ctx, -1);
  15017. DUK_ASSERT(tv1 != NULL);
  15018. tv2 = duk_require_tval(ctx, to_index);
  15019. DUK_ASSERT(tv2 != NULL);
  15020. /* For tv1 == tv2, both pointing to stack top, the end result
  15021. * is same as duk_pop(ctx).
  15022. */
  15023. DUK_TVAL_SET_TVAL(&tv_tmp, tv2);
  15024. DUK_TVAL_SET_TVAL(tv2, tv1);
  15025. DUK_TVAL_SET_UNDEFINED_UNUSED(tv1);
  15026. thr->valstack_top--;
  15027. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  15028. }
  15029. DUK_EXTERNAL void duk_copy(duk_context *ctx, duk_idx_t from_index, duk_idx_t to_index) {
  15030. duk_hthread *thr = (duk_hthread *) ctx;
  15031. duk_tval *tv1;
  15032. duk_tval *tv2;
  15033. duk_tval tv_tmp;
  15034. DUK_ASSERT_CTX_VALID(ctx);
  15035. DUK_UNREF(thr); /* w/o refcounting */
  15036. tv1 = duk_require_tval(ctx, from_index);
  15037. DUK_ASSERT(tv1 != NULL);
  15038. tv2 = duk_require_tval(ctx, to_index);
  15039. DUK_ASSERT(tv2 != NULL);
  15040. /* For tv1 == tv2, this is a no-op (no explicit check needed). */
  15041. DUK_TVAL_SET_TVAL(&tv_tmp, tv2);
  15042. DUK_TVAL_SET_TVAL(tv2, tv1);
  15043. DUK_TVAL_INCREF(thr, tv2); /* no side effects */
  15044. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  15045. }
  15046. DUK_EXTERNAL void duk_remove(duk_context *ctx, duk_idx_t index) {
  15047. duk_hthread *thr = (duk_hthread *) ctx;
  15048. duk_tval *p;
  15049. duk_tval *q;
  15050. #ifdef DUK_USE_REFERENCE_COUNTING
  15051. duk_tval tv_tmp;
  15052. #endif
  15053. duk_size_t nbytes;
  15054. DUK_ASSERT_CTX_VALID(ctx);
  15055. p = duk_require_tval(ctx, index);
  15056. DUK_ASSERT(p != NULL);
  15057. q = duk_require_tval(ctx, -1);
  15058. DUK_ASSERT(q != NULL);
  15059. DUK_ASSERT(q >= p);
  15060. /* nbytes zero size case
  15061. * <--------->
  15062. * [ ... | p | x | x | q ] [ ... | p==q ]
  15063. * => [ ... | x | x | q ] [ ... ]
  15064. */
  15065. #ifdef DUK_USE_REFERENCE_COUNTING
  15066. /* use a temp: decref only when valstack reachable values are correct */
  15067. DUK_TVAL_SET_TVAL(&tv_tmp, p);
  15068. #endif
  15069. nbytes = (duk_size_t) (((duk_uint8_t *) q) - ((duk_uint8_t *) p)); /* Note: 'q' is top-1 */
  15070. DUK_MEMMOVE(p, p + 1, nbytes); /* zero size not an issue: pointers are valid */
  15071. DUK_TVAL_SET_UNDEFINED_UNUSED(q);
  15072. thr->valstack_top--;
  15073. #ifdef DUK_USE_REFERENCE_COUNTING
  15074. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  15075. #endif
  15076. }
  15077. /*
  15078. * Stack slice primitives
  15079. */
  15080. DUK_EXTERNAL void duk_xcopymove_raw(duk_context *to_ctx, duk_context *from_ctx, duk_idx_t count, duk_bool_t is_copy) {
  15081. duk_hthread *to_thr = (duk_hthread *) to_ctx;
  15082. duk_hthread *from_thr = (duk_hthread *) from_ctx;
  15083. void *src;
  15084. duk_size_t nbytes;
  15085. duk_tval *p;
  15086. duk_tval *q;
  15087. /* XXX: several pointer comparison issues here */
  15088. DUK_ASSERT_CTX_VALID(to_ctx);
  15089. DUK_ASSERT_CTX_VALID(from_ctx);
  15090. DUK_ASSERT(to_ctx != NULL);
  15091. DUK_ASSERT(from_ctx != NULL);
  15092. if (to_ctx == from_ctx) {
  15093. DUK_ERROR(to_thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CONTEXT);
  15094. return;
  15095. }
  15096. if ((count < 0) ||
  15097. (count > (duk_idx_t) to_thr->valstack_max)) {
  15098. /* Maximum value check ensures 'nbytes' won't wrap below. */
  15099. DUK_ERROR(to_thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_COUNT);
  15100. return;
  15101. }
  15102. nbytes = sizeof(duk_tval) * count;
  15103. if (nbytes == 0) {
  15104. return;
  15105. }
  15106. DUK_ASSERT(to_thr->valstack_top <= to_thr->valstack_end);
  15107. if ((duk_size_t) ((duk_uint8_t *) to_thr->valstack_end - (duk_uint8_t *) to_thr->valstack_top) < nbytes) {
  15108. DUK_ERROR(to_thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  15109. }
  15110. src = (void *) ((duk_uint8_t *) from_thr->valstack_top - nbytes);
  15111. if (src < (void *) from_thr->valstack_bottom) {
  15112. DUK_ERROR(to_thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_COUNT);
  15113. }
  15114. /* copy values (no overlap even if to_ctx == from_ctx; that's not
  15115. * allowed now anyway)
  15116. */
  15117. DUK_ASSERT(nbytes > 0);
  15118. DUK_MEMCPY((void *) to_thr->valstack_top, src, nbytes);
  15119. p = to_thr->valstack_top;
  15120. to_thr->valstack_top = (duk_tval *) (((duk_uint8_t *) p) + nbytes);
  15121. if (is_copy) {
  15122. /* incref copies, keep originals */
  15123. q = to_thr->valstack_top;
  15124. while (p < q) {
  15125. DUK_TVAL_INCREF(to_thr, p); /* no side effects */
  15126. p++;
  15127. }
  15128. } else {
  15129. /* no net refcount change */
  15130. p = from_thr->valstack_top;
  15131. q = (duk_tval *) (((duk_uint8_t *) p) - nbytes);
  15132. from_thr->valstack_top = q;
  15133. /* elements above stack top are kept UNUSED */
  15134. while (p > q) {
  15135. p--;
  15136. DUK_TVAL_SET_UNDEFINED_UNUSED(p);
  15137. /* XXX: fast primitive to set a bunch of values to UNDEFINED_UNUSED */
  15138. }
  15139. }
  15140. }
  15141. /*
  15142. * Get/require
  15143. */
  15144. DUK_EXTERNAL void duk_require_undefined(duk_context *ctx, duk_idx_t index) {
  15145. duk_hthread *thr = (duk_hthread *) ctx;
  15146. duk_tval *tv;
  15147. DUK_ASSERT_CTX_VALID(ctx);
  15148. tv = duk_get_tval(ctx, index);
  15149. if (tv && DUK_TVAL_IS_UNDEFINED(tv)) {
  15150. /* Note: accept both 'actual' and 'unused' undefined */
  15151. return;
  15152. }
  15153. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_UNDEFINED);
  15154. }
  15155. DUK_EXTERNAL void duk_require_null(duk_context *ctx, duk_idx_t index) {
  15156. duk_hthread *thr = (duk_hthread *) ctx;
  15157. duk_tval *tv;
  15158. DUK_ASSERT_CTX_VALID(ctx);
  15159. tv = duk_get_tval(ctx, index);
  15160. if (tv && DUK_TVAL_IS_NULL(tv)) {
  15161. return;
  15162. }
  15163. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_NULL);
  15164. return; /* not reachable */
  15165. }
  15166. DUK_EXTERNAL duk_bool_t duk_get_boolean(duk_context *ctx, duk_idx_t index) {
  15167. duk_bool_t ret = 0; /* default: false */
  15168. duk_tval *tv;
  15169. DUK_ASSERT_CTX_VALID(ctx);
  15170. tv = duk_get_tval(ctx, index);
  15171. if (tv && DUK_TVAL_IS_BOOLEAN(tv)) {
  15172. ret = DUK_TVAL_GET_BOOLEAN(tv);
  15173. }
  15174. DUK_ASSERT(ret == 0 || ret == 1);
  15175. return ret;
  15176. }
  15177. DUK_EXTERNAL duk_bool_t duk_require_boolean(duk_context *ctx, duk_idx_t index) {
  15178. duk_hthread *thr = (duk_hthread *) ctx;
  15179. duk_tval *tv;
  15180. DUK_ASSERT_CTX_VALID(ctx);
  15181. tv = duk_get_tval(ctx, index);
  15182. if (tv && DUK_TVAL_IS_BOOLEAN(tv)) {
  15183. duk_bool_t ret = DUK_TVAL_GET_BOOLEAN(tv);
  15184. DUK_ASSERT(ret == 0 || ret == 1);
  15185. return ret;
  15186. }
  15187. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_BOOLEAN);
  15188. return 0; /* not reachable */
  15189. }
  15190. DUK_EXTERNAL duk_double_t duk_get_number(duk_context *ctx, duk_idx_t index) {
  15191. duk_double_union ret;
  15192. duk_tval *tv;
  15193. DUK_ASSERT_CTX_VALID(ctx);
  15194. ret.d = DUK_DOUBLE_NAN; /* default: NaN */
  15195. tv = duk_get_tval(ctx, index);
  15196. if (tv && DUK_TVAL_IS_NUMBER(tv)) {
  15197. ret.d = DUK_TVAL_GET_NUMBER(tv);
  15198. }
  15199. /*
  15200. * Number should already be in NaN-normalized form, but let's
  15201. * normalize anyway.
  15202. */
  15203. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&ret);
  15204. return ret.d;
  15205. }
  15206. DUK_EXTERNAL duk_double_t duk_require_number(duk_context *ctx, duk_idx_t index) {
  15207. duk_hthread *thr = (duk_hthread *) ctx;
  15208. duk_tval *tv;
  15209. DUK_ASSERT_CTX_VALID(ctx);
  15210. tv = duk_get_tval(ctx, index);
  15211. if (tv && DUK_TVAL_IS_NUMBER(tv)) {
  15212. duk_double_union ret;
  15213. ret.d = DUK_TVAL_GET_NUMBER(tv);
  15214. /*
  15215. * Number should already be in NaN-normalized form,
  15216. * but let's normalize anyway.
  15217. */
  15218. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&ret);
  15219. return ret.d;
  15220. }
  15221. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_NUMBER);
  15222. return DUK_DOUBLE_NAN; /* not reachable */
  15223. }
  15224. DUK_EXTERNAL duk_int_t duk_get_int(duk_context *ctx, duk_idx_t index) {
  15225. /* Custom coercion for API */
  15226. DUK_ASSERT_CTX_VALID(ctx);
  15227. return (duk_int_t) duk__api_coerce_d2i(ctx, index, 0 /*require*/);
  15228. }
  15229. DUK_EXTERNAL duk_uint_t duk_get_uint(duk_context *ctx, duk_idx_t index) {
  15230. /* Custom coercion for API */
  15231. DUK_ASSERT_CTX_VALID(ctx);
  15232. return (duk_uint_t) duk__api_coerce_d2ui(ctx, index, 0 /*require*/);
  15233. }
  15234. DUK_EXTERNAL duk_int_t duk_require_int(duk_context *ctx, duk_idx_t index) {
  15235. /* Custom coercion for API */
  15236. DUK_ASSERT_CTX_VALID(ctx);
  15237. return (duk_int_t) duk__api_coerce_d2i(ctx, index, 1 /*require*/);
  15238. }
  15239. DUK_EXTERNAL duk_uint_t duk_require_uint(duk_context *ctx, duk_idx_t index) {
  15240. /* Custom coercion for API */
  15241. DUK_ASSERT_CTX_VALID(ctx);
  15242. return (duk_uint_t) duk__api_coerce_d2ui(ctx, index, 1 /*require*/);
  15243. }
  15244. DUK_EXTERNAL const char *duk_get_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) {
  15245. const char *ret;
  15246. duk_tval *tv;
  15247. DUK_ASSERT_CTX_VALID(ctx);
  15248. /* default: NULL, length 0 */
  15249. ret = NULL;
  15250. if (out_len) {
  15251. *out_len = 0;
  15252. }
  15253. tv = duk_get_tval(ctx, index);
  15254. if (tv && DUK_TVAL_IS_STRING(tv)) {
  15255. /* Here we rely on duk_hstring instances always being zero
  15256. * terminated even if the actual string is not.
  15257. */
  15258. duk_hstring *h = DUK_TVAL_GET_STRING(tv);
  15259. DUK_ASSERT(h != NULL);
  15260. ret = (const char *) DUK_HSTRING_GET_DATA(h);
  15261. if (out_len) {
  15262. *out_len = DUK_HSTRING_GET_BYTELEN(h);
  15263. }
  15264. }
  15265. return ret;
  15266. }
  15267. DUK_EXTERNAL const char *duk_require_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) {
  15268. duk_hthread *thr = (duk_hthread *) ctx;
  15269. const char *ret;
  15270. DUK_ASSERT_CTX_VALID(ctx);
  15271. /* Note: this check relies on the fact that even a zero-size string
  15272. * has a non-NULL pointer.
  15273. */
  15274. ret = duk_get_lstring(ctx, index, out_len);
  15275. if (ret) {
  15276. return ret;
  15277. }
  15278. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_STRING);
  15279. return NULL; /* not reachable */
  15280. }
  15281. DUK_EXTERNAL const char *duk_get_string(duk_context *ctx, duk_idx_t index) {
  15282. DUK_ASSERT_CTX_VALID(ctx);
  15283. return duk_get_lstring(ctx, index, NULL);
  15284. }
  15285. DUK_EXTERNAL const char *duk_require_string(duk_context *ctx, duk_idx_t index) {
  15286. DUK_ASSERT_CTX_VALID(ctx);
  15287. return duk_require_lstring(ctx, index, NULL);
  15288. }
  15289. DUK_EXTERNAL void *duk_get_pointer(duk_context *ctx, duk_idx_t index) {
  15290. duk_tval *tv;
  15291. DUK_ASSERT_CTX_VALID(ctx);
  15292. tv = duk_get_tval(ctx, index);
  15293. if (tv && DUK_TVAL_IS_POINTER(tv)) {
  15294. void *p = DUK_TVAL_GET_POINTER(tv); /* may be NULL */
  15295. return (void *) p;
  15296. }
  15297. return NULL;
  15298. }
  15299. DUK_EXTERNAL void *duk_require_pointer(duk_context *ctx, duk_idx_t index) {
  15300. duk_hthread *thr = (duk_hthread *) ctx;
  15301. duk_tval *tv;
  15302. DUK_ASSERT_CTX_VALID(ctx);
  15303. /* Note: here we must be wary of the fact that a pointer may be
  15304. * valid and be a NULL.
  15305. */
  15306. tv = duk_get_tval(ctx, index);
  15307. if (tv && DUK_TVAL_IS_POINTER(tv)) {
  15308. void *p = DUK_TVAL_GET_POINTER(tv); /* may be NULL */
  15309. return (void *) p;
  15310. }
  15311. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_POINTER);
  15312. return NULL; /* not reachable */
  15313. }
  15314. #if 0 /*unused*/
  15315. DUK_INTERNAL void *duk_get_voidptr(duk_context *ctx, duk_idx_t index) {
  15316. duk_tval *tv;
  15317. DUK_ASSERT_CTX_VALID(ctx);
  15318. tv = duk_get_tval(ctx, index);
  15319. if (tv && DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  15320. duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
  15321. DUK_ASSERT(h != NULL);
  15322. return (void *) h;
  15323. }
  15324. return NULL;
  15325. }
  15326. #endif
  15327. DUK_EXTERNAL void *duk_get_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) {
  15328. duk_hthread *thr = (duk_hthread *) ctx;
  15329. duk_tval *tv;
  15330. DUK_ASSERT_CTX_VALID(ctx);
  15331. DUK_UNREF(thr);
  15332. if (out_size != NULL) {
  15333. *out_size = 0;
  15334. }
  15335. tv = duk_get_tval(ctx, index);
  15336. if (tv && DUK_TVAL_IS_BUFFER(tv)) {
  15337. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  15338. DUK_ASSERT(h != NULL);
  15339. if (out_size) {
  15340. *out_size = DUK_HBUFFER_GET_SIZE(h);
  15341. }
  15342. return (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h); /* may be NULL (but only if size is 0) */
  15343. }
  15344. return NULL;
  15345. }
  15346. DUK_EXTERNAL void *duk_require_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) {
  15347. duk_hthread *thr = (duk_hthread *) ctx;
  15348. duk_tval *tv;
  15349. DUK_ASSERT_CTX_VALID(ctx);
  15350. if (out_size != NULL) {
  15351. *out_size = 0;
  15352. }
  15353. /* Note: here we must be wary of the fact that a data pointer may
  15354. * be a NULL for a zero-size buffer.
  15355. */
  15356. tv = duk_get_tval(ctx, index);
  15357. if (tv && DUK_TVAL_IS_BUFFER(tv)) {
  15358. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  15359. DUK_ASSERT(h != NULL);
  15360. if (out_size) {
  15361. *out_size = DUK_HBUFFER_GET_SIZE(h);
  15362. }
  15363. return (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h); /* may be NULL (but only if size is 0) */
  15364. }
  15365. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_BUFFER);
  15366. return NULL; /* not reachable */
  15367. }
  15368. /* Raw helper for getting a value from the stack, checking its tag, and possible its object class.
  15369. * The tag cannot be a number because numbers don't have an internal tag in the packed representation.
  15370. */
  15371. DUK_INTERNAL duk_heaphdr *duk_get_tagged_heaphdr_raw(duk_context *ctx, duk_idx_t index, duk_uint_t flags_and_tag) {
  15372. duk_hthread *thr = (duk_hthread *) ctx;
  15373. duk_tval *tv;
  15374. duk_small_uint_t tag = flags_and_tag & 0xffffU; /* tags can be up to 16 bits */
  15375. DUK_ASSERT_CTX_VALID(ctx);
  15376. tv = duk_get_tval(ctx, index);
  15377. if (tv && (DUK_TVAL_GET_TAG(tv) == tag)) {
  15378. duk_heaphdr *ret;
  15379. /* Note: tag comparison in general doesn't work for numbers,
  15380. * but it does work for everything else (heap objects here).
  15381. */
  15382. ret = DUK_TVAL_GET_HEAPHDR(tv);
  15383. DUK_ASSERT(ret != NULL); /* tagged null pointers should never occur */
  15384. /* If class check has been requested, tag must also be DUK_TAG_OBJECT.
  15385. * This allows us to just check the class check flag without checking
  15386. * the tag also.
  15387. */
  15388. DUK_ASSERT((flags_and_tag & DUK_GETTAGGED_FLAG_CHECK_CLASS) == 0 ||
  15389. tag == DUK_TAG_OBJECT);
  15390. if ((flags_and_tag & DUK_GETTAGGED_FLAG_CHECK_CLASS) == 0 || /* no class check */
  15391. (duk_int_t) DUK_HOBJECT_GET_CLASS_NUMBER((duk_hobject *) ret) == /* or class check matches */
  15392. (duk_int_t) ((flags_and_tag >> DUK_GETTAGGED_CLASS_SHIFT) & 0xff)) {
  15393. return ret;
  15394. }
  15395. }
  15396. if (flags_and_tag & DUK_GETTAGGED_FLAG_ALLOW_NULL) {
  15397. return (duk_heaphdr *) NULL;
  15398. }
  15399. /* Formatting the tag number here is not very useful: the tag value
  15400. * is Duktape internal (not the same as DUK_TYPE_xxx) and even depends
  15401. * on the duk_tval layout. If anything, add a human readable type here.
  15402. */
  15403. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_UNEXPECTED_TYPE);
  15404. return NULL; /* not reachable */
  15405. }
  15406. DUK_INTERNAL duk_hstring *duk_get_hstring(duk_context *ctx, duk_idx_t index) {
  15407. return (duk_hstring *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_STRING | DUK_GETTAGGED_FLAG_ALLOW_NULL);
  15408. }
  15409. DUK_INTERNAL duk_hstring *duk_require_hstring(duk_context *ctx, duk_idx_t index) {
  15410. return (duk_hstring *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_STRING);
  15411. }
  15412. DUK_INTERNAL duk_hobject *duk_get_hobject(duk_context *ctx, duk_idx_t index) {
  15413. return (duk_hobject *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT | DUK_GETTAGGED_FLAG_ALLOW_NULL);
  15414. }
  15415. DUK_INTERNAL duk_hobject *duk_require_hobject(duk_context *ctx, duk_idx_t index) {
  15416. return (duk_hobject *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT);
  15417. }
  15418. DUK_INTERNAL duk_hbuffer *duk_get_hbuffer(duk_context *ctx, duk_idx_t index) {
  15419. return (duk_hbuffer *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_BUFFER | DUK_GETTAGGED_FLAG_ALLOW_NULL);
  15420. }
  15421. DUK_INTERNAL duk_hbuffer *duk_require_hbuffer(duk_context *ctx, duk_idx_t index) {
  15422. return (duk_hbuffer *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_BUFFER);
  15423. }
  15424. DUK_INTERNAL duk_hthread *duk_get_hthread(duk_context *ctx, duk_idx_t index) {
  15425. duk_hobject *h = (duk_hobject *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT | DUK_GETTAGGED_FLAG_ALLOW_NULL);
  15426. if (h != NULL && !DUK_HOBJECT_IS_THREAD(h)) {
  15427. h = NULL;
  15428. }
  15429. return (duk_hthread *) h;
  15430. }
  15431. DUK_INTERNAL duk_hthread *duk_require_hthread(duk_context *ctx, duk_idx_t index) {
  15432. duk_hthread *thr = (duk_hthread *) ctx;
  15433. duk_hobject *h = (duk_hobject *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT);
  15434. DUK_ASSERT(h != NULL);
  15435. if (!DUK_HOBJECT_IS_THREAD(h)) {
  15436. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_THREAD);
  15437. }
  15438. return (duk_hthread *) h;
  15439. }
  15440. DUK_INTERNAL duk_hcompiledfunction *duk_get_hcompiledfunction(duk_context *ctx, duk_idx_t index) {
  15441. duk_hobject *h = (duk_hobject *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT | DUK_GETTAGGED_FLAG_ALLOW_NULL);
  15442. if (h != NULL && !DUK_HOBJECT_IS_COMPILEDFUNCTION(h)) {
  15443. h = NULL;
  15444. }
  15445. return (duk_hcompiledfunction *) h;
  15446. }
  15447. #if 0 /*unused*/
  15448. DUK_INTERNAL duk_hcompiledfunction *duk_require_hcompiledfunction(duk_context *ctx, duk_idx_t index) {
  15449. duk_hthread *thr = (duk_hthread *) ctx;
  15450. duk_hobject *h = (duk_hobject *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT);
  15451. DUK_ASSERT(h != NULL);
  15452. if (!DUK_HOBJECT_IS_COMPILEDFUNCTION(h)) {
  15453. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_COMPILEDFUNCTION);
  15454. }
  15455. return (duk_hcompiledfunction *) h;
  15456. }
  15457. #endif
  15458. DUK_INTERNAL duk_hnativefunction *duk_get_hnativefunction(duk_context *ctx, duk_idx_t index) {
  15459. duk_hobject *h = (duk_hobject *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT | DUK_GETTAGGED_FLAG_ALLOW_NULL);
  15460. if (h != NULL && !DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
  15461. h = NULL;
  15462. }
  15463. return (duk_hnativefunction *) h;
  15464. }
  15465. DUK_INTERNAL duk_hnativefunction *duk_require_hnativefunction(duk_context *ctx, duk_idx_t index) {
  15466. duk_hthread *thr = (duk_hthread *) ctx;
  15467. duk_hobject *h = (duk_hobject *) duk_get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT);
  15468. DUK_ASSERT(h != NULL);
  15469. if (!DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
  15470. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_NATIVEFUNCTION);
  15471. }
  15472. return (duk_hnativefunction *) h;
  15473. }
  15474. DUK_EXTERNAL duk_c_function duk_get_c_function(duk_context *ctx, duk_idx_t index) {
  15475. duk_tval *tv;
  15476. duk_hobject *h;
  15477. duk_hnativefunction *f;
  15478. DUK_ASSERT_CTX_VALID(ctx);
  15479. tv = duk_get_tval(ctx, index);
  15480. if (!tv) {
  15481. return NULL;
  15482. }
  15483. if (!DUK_TVAL_IS_OBJECT(tv)) {
  15484. return NULL;
  15485. }
  15486. h = DUK_TVAL_GET_OBJECT(tv);
  15487. DUK_ASSERT(h != NULL);
  15488. if (!DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
  15489. return NULL;
  15490. }
  15491. DUK_ASSERT(DUK_HOBJECT_HAS_NATIVEFUNCTION(h));
  15492. f = (duk_hnativefunction *) h;
  15493. return f->func;
  15494. }
  15495. DUK_EXTERNAL duk_c_function duk_require_c_function(duk_context *ctx, duk_idx_t index) {
  15496. duk_hthread *thr = (duk_hthread *) ctx;
  15497. duk_c_function ret;
  15498. DUK_ASSERT_CTX_VALID(ctx);
  15499. ret = duk_get_c_function(ctx, index);
  15500. if (!ret) {
  15501. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_C_FUNCTION);
  15502. }
  15503. return ret;
  15504. }
  15505. DUK_EXTERNAL duk_context *duk_get_context(duk_context *ctx, duk_idx_t index) {
  15506. DUK_ASSERT_CTX_VALID(ctx);
  15507. return (duk_context *) duk_get_hthread(ctx, index);
  15508. }
  15509. DUK_EXTERNAL duk_context *duk_require_context(duk_context *ctx, duk_idx_t index) {
  15510. DUK_ASSERT_CTX_VALID(ctx);
  15511. return (duk_context *) duk_require_hthread(ctx, index);
  15512. }
  15513. DUK_EXTERNAL void *duk_get_heapptr(duk_context *ctx, duk_idx_t index) {
  15514. duk_tval *tv;
  15515. void *ret;
  15516. DUK_ASSERT_CTX_VALID(ctx);
  15517. tv = duk_get_tval(ctx, index);
  15518. if (tv && DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  15519. ret = (void *) DUK_TVAL_GET_HEAPHDR(tv);
  15520. DUK_ASSERT(ret != NULL);
  15521. return ret;
  15522. }
  15523. return (void *) NULL;
  15524. }
  15525. DUK_EXTERNAL void *duk_require_heapptr(duk_context *ctx, duk_idx_t index) {
  15526. duk_hthread *thr = (duk_hthread *) ctx;
  15527. duk_tval *tv;
  15528. void *ret;
  15529. DUK_ASSERT_CTX_VALID(ctx);
  15530. tv = duk_require_tval(ctx, index);
  15531. DUK_ASSERT(tv != NULL);
  15532. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  15533. ret = (void *) DUK_TVAL_GET_HEAPHDR(tv);
  15534. DUK_ASSERT(ret != NULL);
  15535. return ret;
  15536. }
  15537. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_UNEXPECTED_TYPE);
  15538. return (void *) NULL; /* not reachable */
  15539. }
  15540. #if 0
  15541. /* This would be pointless: we'd return NULL for both lightfuncs and
  15542. * unexpected types.
  15543. */
  15544. duk_hobject *duk_get_hobject_or_lfunc(duk_context *ctx, duk_idx_t index) {
  15545. }
  15546. #endif
  15547. /* Useful for internal call sites where we either expect an object (function)
  15548. * or a lightfunc. Accepts an object (returned as is) or a lightfunc (coerced
  15549. * to an object). Return value is NULL if value is neither an object nor a
  15550. * lightfunc.
  15551. */
  15552. duk_hobject *duk_get_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index) {
  15553. duk_tval *tv;
  15554. DUK_ASSERT_CTX_VALID(ctx);
  15555. tv = duk_require_tval(ctx, index);
  15556. DUK_ASSERT(tv != NULL);
  15557. if (DUK_TVAL_IS_OBJECT(tv)) {
  15558. return DUK_TVAL_GET_OBJECT(tv);
  15559. } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  15560. duk_to_object(ctx, index);
  15561. return duk_require_hobject(ctx, index);
  15562. }
  15563. return NULL;
  15564. }
  15565. /* Useful for internal call sites where we either expect an object (function)
  15566. * or a lightfunc. Returns NULL for a lightfunc.
  15567. */
  15568. DUK_INTERNAL duk_hobject *duk_require_hobject_or_lfunc(duk_context *ctx, duk_idx_t index) {
  15569. duk_hthread *thr = (duk_hthread *) ctx;
  15570. duk_tval *tv;
  15571. DUK_ASSERT_CTX_VALID(ctx);
  15572. tv = duk_require_tval(ctx, index);
  15573. DUK_ASSERT(tv != NULL);
  15574. if (DUK_TVAL_IS_OBJECT(tv)) {
  15575. return DUK_TVAL_GET_OBJECT(tv);
  15576. } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  15577. return NULL;
  15578. }
  15579. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_UNEXPECTED_TYPE);
  15580. return NULL; /* not reachable */
  15581. }
  15582. /* Useful for internal call sites where we either expect an object (function)
  15583. * or a lightfunc. Accepts an object (returned as is) or a lightfunc (coerced
  15584. * to an object). Return value is never NULL.
  15585. */
  15586. DUK_INTERNAL duk_hobject *duk_require_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index) {
  15587. duk_hthread *thr = (duk_hthread *) ctx;
  15588. duk_tval *tv;
  15589. DUK_ASSERT_CTX_VALID(ctx);
  15590. tv = duk_require_tval(ctx, index);
  15591. if (DUK_TVAL_IS_OBJECT(tv)) {
  15592. return DUK_TVAL_GET_OBJECT(tv);
  15593. } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  15594. duk_to_object(ctx, index);
  15595. return duk_require_hobject(ctx, index);
  15596. }
  15597. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_UNEXPECTED_TYPE);
  15598. return NULL; /* not reachable */
  15599. }
  15600. DUK_EXTERNAL duk_size_t duk_get_length(duk_context *ctx, duk_idx_t index) {
  15601. duk_tval *tv;
  15602. DUK_ASSERT_CTX_VALID(ctx);
  15603. tv = duk_get_tval(ctx, index);
  15604. if (!tv) {
  15605. return 0;
  15606. }
  15607. switch (DUK_TVAL_GET_TAG(tv)) {
  15608. case DUK_TAG_UNDEFINED:
  15609. case DUK_TAG_NULL:
  15610. case DUK_TAG_BOOLEAN:
  15611. case DUK_TAG_POINTER:
  15612. return 0;
  15613. case DUK_TAG_STRING: {
  15614. duk_hstring *h = DUK_TVAL_GET_STRING(tv);
  15615. DUK_ASSERT(h != NULL);
  15616. return (duk_size_t) DUK_HSTRING_GET_CHARLEN(h);
  15617. }
  15618. case DUK_TAG_OBJECT: {
  15619. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  15620. DUK_ASSERT(h != NULL);
  15621. return (duk_size_t) duk_hobject_get_length((duk_hthread *) ctx, h);
  15622. }
  15623. case DUK_TAG_BUFFER: {
  15624. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  15625. DUK_ASSERT(h != NULL);
  15626. return (duk_size_t) DUK_HBUFFER_GET_SIZE(h);
  15627. }
  15628. case DUK_TAG_LIGHTFUNC: {
  15629. duk_small_uint_t lf_flags;
  15630. lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv);
  15631. return (duk_size_t) DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags);
  15632. }
  15633. #if defined(DUK_USE_FASTINT)
  15634. case DUK_TAG_FASTINT:
  15635. #endif
  15636. default:
  15637. /* number */
  15638. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  15639. return 0;
  15640. }
  15641. DUK_UNREACHABLE();
  15642. }
  15643. DUK_INTERNAL void duk_set_length(duk_context *ctx, duk_idx_t index, duk_size_t length) {
  15644. duk_hthread *thr = (duk_hthread *) ctx;
  15645. duk_hobject *h;
  15646. DUK_ASSERT_CTX_VALID(ctx);
  15647. h = duk_get_hobject(ctx, index);
  15648. if (!h) {
  15649. return;
  15650. }
  15651. duk_hobject_set_length(thr, h, (duk_uint32_t) length); /* XXX: typing */
  15652. }
  15653. /*
  15654. * Conversions and coercions
  15655. *
  15656. * The conversion/coercions are in-place operations on the value stack.
  15657. * Some operations are implemented here directly, while others call a
  15658. * helper in duk_js_ops.c after validating arguments.
  15659. */
  15660. /* E5 Section 8.12.8 */
  15661. DUK_LOCAL duk_bool_t duk__defaultvalue_coerce_attempt(duk_context *ctx, duk_idx_t index, duk_small_int_t func_stridx) {
  15662. if (duk_get_prop_stridx(ctx, index, func_stridx)) {
  15663. /* [ ... func ] */
  15664. if (duk_is_callable(ctx, -1)) {
  15665. duk_dup(ctx, index); /* -> [ ... func this ] */
  15666. duk_call_method(ctx, 0); /* -> [ ... retval ] */
  15667. if (duk_is_primitive(ctx, -1)) {
  15668. duk_replace(ctx, index);
  15669. return 1;
  15670. }
  15671. /* [ ... retval ]; popped below */
  15672. }
  15673. }
  15674. duk_pop(ctx); /* [ ... func/retval ] -> [ ... ] */
  15675. return 0;
  15676. }
  15677. DUK_EXTERNAL void duk_to_defaultvalue(duk_context *ctx, duk_idx_t index, duk_int_t hint) {
  15678. duk_hthread *thr = (duk_hthread *) ctx;
  15679. duk_hobject *obj;
  15680. /* inline initializer for coercers[] is not allowed by old compilers like BCC */
  15681. duk_small_int_t coercers[2];
  15682. DUK_ASSERT_CTX_VALID(ctx);
  15683. DUK_ASSERT(thr != NULL);
  15684. coercers[0] = DUK_STRIDX_VALUE_OF;
  15685. coercers[1] = DUK_STRIDX_TO_STRING;
  15686. index = duk_require_normalize_index(ctx, index);
  15687. obj = duk_require_hobject_or_lfunc(ctx, index);
  15688. if (hint == DUK_HINT_NONE) {
  15689. if (obj != NULL && DUK_HOBJECT_GET_CLASS_NUMBER(obj) == DUK_HOBJECT_CLASS_DATE) {
  15690. hint = DUK_HINT_STRING;
  15691. } else {
  15692. hint = DUK_HINT_NUMBER;
  15693. }
  15694. }
  15695. if (hint == DUK_HINT_STRING) {
  15696. coercers[0] = DUK_STRIDX_TO_STRING;
  15697. coercers[1] = DUK_STRIDX_VALUE_OF;
  15698. }
  15699. if (duk__defaultvalue_coerce_attempt(ctx, index, coercers[0])) {
  15700. return;
  15701. }
  15702. if (duk__defaultvalue_coerce_attempt(ctx, index, coercers[1])) {
  15703. return;
  15704. }
  15705. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_DEFAULTVALUE_COERCE_FAILED);
  15706. }
  15707. DUK_EXTERNAL void duk_to_undefined(duk_context *ctx, duk_idx_t index) {
  15708. duk_hthread *thr = (duk_hthread *) ctx;
  15709. duk_tval *tv;
  15710. duk_tval tv_tmp;
  15711. DUK_ASSERT_CTX_VALID(ctx);
  15712. DUK_UNREF(thr);
  15713. tv = duk_require_tval(ctx, index);
  15714. DUK_ASSERT(tv != NULL);
  15715. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  15716. DUK_TVAL_SET_UNDEFINED_ACTUAL(tv); /* no need to incref */
  15717. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  15718. }
  15719. DUK_EXTERNAL void duk_to_null(duk_context *ctx, duk_idx_t index) {
  15720. duk_hthread *thr = (duk_hthread *) ctx;
  15721. duk_tval *tv;
  15722. duk_tval tv_tmp;
  15723. DUK_ASSERT_CTX_VALID(ctx);
  15724. DUK_UNREF(thr);
  15725. tv = duk_require_tval(ctx, index);
  15726. DUK_ASSERT(tv != NULL);
  15727. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  15728. DUK_TVAL_SET_NULL(tv); /* no need to incref */
  15729. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  15730. }
  15731. /* E5 Section 9.1 */
  15732. DUK_EXTERNAL void duk_to_primitive(duk_context *ctx, duk_idx_t index, duk_int_t hint) {
  15733. DUK_ASSERT_CTX_VALID(ctx);
  15734. DUK_ASSERT(hint == DUK_HINT_NONE || hint == DUK_HINT_NUMBER || hint == DUK_HINT_STRING);
  15735. index = duk_require_normalize_index(ctx, index);
  15736. if (!duk_check_type_mask(ctx, index, DUK_TYPE_MASK_OBJECT |
  15737. DUK_TYPE_MASK_LIGHTFUNC)) {
  15738. /* everything except object stay as is */
  15739. return;
  15740. }
  15741. duk_to_defaultvalue(ctx, index, hint);
  15742. }
  15743. /* E5 Section 9.2 */
  15744. DUK_EXTERNAL duk_bool_t duk_to_boolean(duk_context *ctx, duk_idx_t index) {
  15745. duk_hthread *thr = (duk_hthread *) ctx;
  15746. duk_tval *tv;
  15747. duk_tval tv_tmp;
  15748. duk_bool_t val;
  15749. DUK_ASSERT_CTX_VALID(ctx);
  15750. DUK_UNREF(thr);
  15751. index = duk_require_normalize_index(ctx, index);
  15752. tv = duk_require_tval(ctx, index);
  15753. DUK_ASSERT(tv != NULL);
  15754. val = duk_js_toboolean(tv);
  15755. DUK_ASSERT(val == 0 || val == 1);
  15756. /* Note: no need to re-lookup tv, conversion is side effect free */
  15757. DUK_ASSERT(tv != NULL);
  15758. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  15759. DUK_TVAL_SET_BOOLEAN(tv, val); /* no need to incref */
  15760. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  15761. return val;
  15762. }
  15763. DUK_EXTERNAL duk_double_t duk_to_number(duk_context *ctx, duk_idx_t index) {
  15764. duk_hthread *thr = (duk_hthread *) ctx;
  15765. duk_tval *tv;
  15766. duk_tval tv_tmp;
  15767. duk_double_t d;
  15768. DUK_ASSERT_CTX_VALID(ctx);
  15769. tv = duk_require_tval(ctx, index);
  15770. DUK_ASSERT(tv != NULL);
  15771. /* XXX: fastint? */
  15772. d = duk_js_tonumber(thr, tv);
  15773. /* Note: need to re-lookup because ToNumber() may have side effects */
  15774. tv = duk_require_tval(ctx, index);
  15775. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  15776. DUK_TVAL_SET_NUMBER(tv, d); /* no need to incref */
  15777. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  15778. return d;
  15779. }
  15780. /* XXX: combine all the integer conversions: they share everything
  15781. * but the helper function for coercion.
  15782. */
  15783. typedef duk_double_t (*duk__toint_coercer)(duk_hthread *thr, duk_tval *tv);
  15784. DUK_LOCAL duk_double_t duk__to_int_uint_helper(duk_context *ctx, duk_idx_t index, duk__toint_coercer coerce_func) {
  15785. duk_hthread *thr = (duk_hthread *) ctx;
  15786. duk_tval *tv;
  15787. duk_tval tv_tmp;
  15788. duk_double_t d;
  15789. DUK_ASSERT_CTX_VALID(ctx);
  15790. tv = duk_require_tval(ctx, index);
  15791. DUK_ASSERT(tv != NULL);
  15792. d = coerce_func(thr, tv);
  15793. /* XXX: fastint? */
  15794. /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
  15795. tv = duk_require_tval(ctx, index);
  15796. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  15797. DUK_TVAL_SET_NUMBER(tv, d); /* no need to incref */
  15798. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  15799. return d;
  15800. }
  15801. DUK_EXTERNAL duk_int_t duk_to_int(duk_context *ctx, duk_idx_t index) {
  15802. /* Value coercion (in stack): ToInteger(), E5 Section 9.4
  15803. * API return value coercion: custom
  15804. */
  15805. DUK_ASSERT_CTX_VALID(ctx);
  15806. (void) duk__to_int_uint_helper(ctx, index, duk_js_tointeger);
  15807. return (duk_int_t) duk__api_coerce_d2i(ctx, index, 0 /*require*/);
  15808. }
  15809. DUK_EXTERNAL duk_uint_t duk_to_uint(duk_context *ctx, duk_idx_t index) {
  15810. /* Value coercion (in stack): ToInteger(), E5 Section 9.4
  15811. * API return value coercion: custom
  15812. */
  15813. DUK_ASSERT_CTX_VALID(ctx);
  15814. (void) duk__to_int_uint_helper(ctx, index, duk_js_tointeger);
  15815. return (duk_uint_t) duk__api_coerce_d2ui(ctx, index, 0 /*require*/);
  15816. }
  15817. DUK_EXTERNAL duk_int32_t duk_to_int32(duk_context *ctx, duk_idx_t index) {
  15818. duk_hthread *thr = (duk_hthread *) ctx;
  15819. duk_tval *tv;
  15820. duk_tval tv_tmp;
  15821. duk_int32_t ret;
  15822. DUK_ASSERT_CTX_VALID(ctx);
  15823. tv = duk_require_tval(ctx, index);
  15824. DUK_ASSERT(tv != NULL);
  15825. ret = duk_js_toint32(thr, tv);
  15826. /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
  15827. tv = duk_require_tval(ctx, index);
  15828. #if defined(DUK_USE_FASTINT)
  15829. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  15830. DUK_TVAL_SET_FASTINT_I32(tv, ret); /* no need to incref */
  15831. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  15832. return ret;
  15833. #else
  15834. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  15835. DUK_TVAL_SET_NUMBER(tv, (duk_double_t) ret); /* no need to incref */
  15836. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  15837. return ret;
  15838. #endif
  15839. }
  15840. DUK_EXTERNAL duk_uint32_t duk_to_uint32(duk_context *ctx, duk_idx_t index) {
  15841. duk_hthread *thr = (duk_hthread *) ctx;
  15842. duk_tval *tv;
  15843. duk_tval tv_tmp;
  15844. duk_uint32_t ret;
  15845. DUK_ASSERT_CTX_VALID(ctx);
  15846. tv = duk_require_tval(ctx, index);
  15847. DUK_ASSERT(tv != NULL);
  15848. ret = duk_js_touint32(thr, tv);
  15849. /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
  15850. tv = duk_require_tval(ctx, index);
  15851. #if defined(DUK_USE_FASTINT)
  15852. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  15853. DUK_TVAL_SET_FASTINT_U32(tv, ret); /* no need to incref */
  15854. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  15855. return ret;
  15856. #else
  15857. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  15858. DUK_TVAL_SET_NUMBER(tv, (duk_double_t) ret); /* no need to incref */
  15859. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  15860. #endif
  15861. return ret;
  15862. }
  15863. DUK_EXTERNAL duk_uint16_t duk_to_uint16(duk_context *ctx, duk_idx_t index) {
  15864. duk_hthread *thr = (duk_hthread *) ctx;
  15865. duk_tval *tv;
  15866. duk_tval tv_tmp;
  15867. duk_uint16_t ret;
  15868. DUK_ASSERT_CTX_VALID(ctx);
  15869. tv = duk_require_tval(ctx, index);
  15870. DUK_ASSERT(tv != NULL);
  15871. ret = duk_js_touint16(thr, tv);
  15872. /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
  15873. tv = duk_require_tval(ctx, index);
  15874. #if defined(DUK_USE_FASTINT)
  15875. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  15876. DUK_TVAL_SET_FASTINT_U32(tv, ret); /* no need to incref */
  15877. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  15878. return ret;
  15879. #else
  15880. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  15881. DUK_TVAL_SET_NUMBER(tv, (duk_double_t) ret); /* no need to incref */
  15882. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  15883. #endif
  15884. return ret;
  15885. }
  15886. /* Special coercion for Uint8ClampedArray. */
  15887. DUK_INTERNAL duk_uint8_t duk_to_uint8clamped(duk_context *ctx, duk_idx_t index) {
  15888. duk_double_t d;
  15889. duk_double_t t;
  15890. duk_uint8_t ret;
  15891. /* XXX: Simplify this algorithm, should be possible to come up with
  15892. * a shorter and faster algorithm by inspecting IEEE representation
  15893. * directly.
  15894. */
  15895. d = duk_to_number(ctx, index);
  15896. if (d <= 0.0) {
  15897. return 0;
  15898. } else if (d >= 255) {
  15899. return 255;
  15900. } else if (DUK_ISNAN(d)) {
  15901. /* Avoid NaN-to-integer coercion as it is compiler specific. */
  15902. return 0;
  15903. }
  15904. t = d - DUK_FLOOR(d);
  15905. if (t == 0.5) {
  15906. /* Exact halfway, round to even. */
  15907. ret = (duk_uint8_t) d;
  15908. ret = (ret + 1) & 0xfe; /* Example: d=3.5, t=0.5 -> ret = (3 + 1) & 0xfe = 4 & 0xfe = 4
  15909. * Example: d=4.5, t=0.5 -> ret = (4 + 1) & 0xfe = 5 & 0xfe = 4
  15910. */
  15911. } else {
  15912. /* Not halfway, round to nearest. */
  15913. ret = (duk_uint8_t) (d + 0.5);
  15914. }
  15915. return ret;
  15916. }
  15917. DUK_EXTERNAL const char *duk_to_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) {
  15918. DUK_ASSERT_CTX_VALID(ctx);
  15919. (void) duk_to_string(ctx, index);
  15920. return duk_require_lstring(ctx, index, out_len);
  15921. }
  15922. DUK_LOCAL duk_ret_t duk__safe_to_string_raw(duk_context *ctx) {
  15923. DUK_ASSERT_CTX_VALID(ctx);
  15924. duk_to_string(ctx, -1);
  15925. return 1;
  15926. }
  15927. DUK_EXTERNAL const char *duk_safe_to_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) {
  15928. DUK_ASSERT_CTX_VALID(ctx);
  15929. index = duk_require_normalize_index(ctx, index);
  15930. /* We intentionally ignore the duk_safe_call() return value and only
  15931. * check the output type. This way we don't also need to check that
  15932. * the returned value is indeed a string in the success case.
  15933. */
  15934. duk_dup(ctx, index);
  15935. (void) duk_safe_call(ctx, duk__safe_to_string_raw, 1 /*nargs*/, 1 /*nrets*/);
  15936. if (!duk_is_string(ctx, -1)) {
  15937. /* Error: try coercing error to string once. */
  15938. (void) duk_safe_call(ctx, duk__safe_to_string_raw, 1 /*nargs*/, 1 /*nrets*/);
  15939. if (!duk_is_string(ctx, -1)) {
  15940. /* Double error */
  15941. duk_pop(ctx);
  15942. duk_push_hstring_stridx(ctx, DUK_STRIDX_UC_ERROR);
  15943. } else {
  15944. ;
  15945. }
  15946. } else {
  15947. ;
  15948. }
  15949. DUK_ASSERT(duk_is_string(ctx, -1));
  15950. duk_replace(ctx, index);
  15951. return duk_require_lstring(ctx, index, out_len);
  15952. }
  15953. /* XXX: other variants like uint, u32 etc */
  15954. 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) {
  15955. duk_hthread *thr = (duk_hthread *) ctx;
  15956. duk_tval *tv;
  15957. duk_tval tv_tmp;
  15958. duk_double_t d, dmin, dmax;
  15959. duk_int_t res;
  15960. duk_bool_t clamped = 0;
  15961. DUK_ASSERT_CTX_VALID(ctx);
  15962. tv = duk_require_tval(ctx, index);
  15963. DUK_ASSERT(tv != NULL);
  15964. d = duk_js_tointeger(thr, tv); /* E5 Section 9.4, ToInteger() */
  15965. dmin = (duk_double_t) minval;
  15966. dmax = (duk_double_t) maxval;
  15967. if (d < dmin) {
  15968. clamped = 1;
  15969. res = minval;
  15970. d = dmin;
  15971. } else if (d > dmax) {
  15972. clamped = 1;
  15973. res = maxval;
  15974. d = dmax;
  15975. } else {
  15976. res = (duk_int_t) d;
  15977. }
  15978. /* 'd' and 'res' agree here */
  15979. /* Relookup in case duk_js_tointeger() ends up e.g. coercing an object. */
  15980. tv = duk_require_tval(ctx, index);
  15981. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  15982. #if defined(DUK_USE_FASTINT)
  15983. #if (DUK_INT_MAX <= 0x7fffffffL)
  15984. DUK_TVAL_SET_FASTINT_I32(tv, res);
  15985. #else
  15986. /* Clamping needed if duk_int_t is 64 bits. */
  15987. if (res >= DUK_FASTINT_MIN && res <= DUK_FASTINT_MAX) {
  15988. DUK_TVAL_SET_FASTINT(tv, res);
  15989. } else {
  15990. DUK_TVAL_SET_NUMBER(tv, d);
  15991. }
  15992. #endif
  15993. #else
  15994. DUK_TVAL_SET_NUMBER(tv, d); /* no need to incref */
  15995. #endif
  15996. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  15997. if (out_clamped) {
  15998. *out_clamped = clamped;
  15999. } else {
  16000. /* coerced value is updated to value stack even when RangeError thrown */
  16001. if (clamped) {
  16002. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_NUMBER_OUTSIDE_RANGE);
  16003. }
  16004. }
  16005. return res;
  16006. }
  16007. DUK_INTERNAL duk_int_t duk_to_int_clamped(duk_context *ctx, duk_idx_t index, duk_idx_t minval, duk_idx_t maxval) {
  16008. duk_bool_t dummy;
  16009. return duk_to_int_clamped_raw(ctx, index, minval, maxval, &dummy);
  16010. }
  16011. 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) {
  16012. return duk_to_int_clamped_raw(ctx, index, minval, maxval, NULL); /* out_clamped==NULL -> RangeError if outside range */
  16013. }
  16014. DUK_EXTERNAL const char *duk_to_string(duk_context *ctx, duk_idx_t index) {
  16015. duk_hthread *thr = (duk_hthread *) ctx;
  16016. duk_tval *tv;
  16017. DUK_ASSERT_CTX_VALID(ctx);
  16018. DUK_UNREF(thr);
  16019. index = duk_require_normalize_index(ctx, index);
  16020. tv = duk_require_tval(ctx, index);
  16021. DUK_ASSERT(tv != NULL);
  16022. switch (DUK_TVAL_GET_TAG(tv)) {
  16023. case DUK_TAG_UNDEFINED: {
  16024. duk_push_hstring_stridx(ctx, DUK_STRIDX_LC_UNDEFINED);
  16025. break;
  16026. }
  16027. case DUK_TAG_NULL: {
  16028. duk_push_hstring_stridx(ctx, DUK_STRIDX_LC_NULL);
  16029. break;
  16030. }
  16031. case DUK_TAG_BOOLEAN: {
  16032. if (DUK_TVAL_GET_BOOLEAN(tv)) {
  16033. duk_push_hstring_stridx(ctx, DUK_STRIDX_TRUE);
  16034. } else {
  16035. duk_push_hstring_stridx(ctx, DUK_STRIDX_FALSE);
  16036. }
  16037. break;
  16038. }
  16039. case DUK_TAG_STRING: {
  16040. /* nop */
  16041. goto skip_replace;
  16042. }
  16043. case DUK_TAG_OBJECT: {
  16044. duk_to_primitive(ctx, index, DUK_HINT_STRING);
  16045. return duk_to_string(ctx, index); /* Note: recursive call */
  16046. }
  16047. case DUK_TAG_BUFFER: {
  16048. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  16049. /* Note: this allows creation of internal strings. */
  16050. DUK_ASSERT(h != NULL);
  16051. duk_push_lstring(ctx,
  16052. (const char *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h),
  16053. (duk_size_t) DUK_HBUFFER_GET_SIZE(h));
  16054. break;
  16055. }
  16056. case DUK_TAG_POINTER: {
  16057. void *ptr = DUK_TVAL_GET_POINTER(tv);
  16058. if (ptr != NULL) {
  16059. duk_push_sprintf(ctx, DUK_STR_FMT_PTR, (void *) ptr);
  16060. } else {
  16061. /* Represent a null pointer as 'null' to be consistent with
  16062. * the JX format variant. Native '%p' format for a NULL
  16063. * pointer may be e.g. '(nil)'.
  16064. */
  16065. duk_push_hstring_stridx(ctx, DUK_STRIDX_LC_NULL);
  16066. }
  16067. break;
  16068. }
  16069. case DUK_TAG_LIGHTFUNC: {
  16070. /* Should match Function.prototype.toString() */
  16071. duk_push_lightfunc_tostring(ctx, tv);
  16072. break;
  16073. }
  16074. #if defined(DUK_USE_FASTINT)
  16075. case DUK_TAG_FASTINT:
  16076. #endif
  16077. default: {
  16078. /* number */
  16079. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  16080. duk_push_tval(ctx, tv);
  16081. duk_numconv_stringify(ctx,
  16082. 10 /*radix*/,
  16083. 0 /*precision:shortest*/,
  16084. 0 /*force_exponential*/);
  16085. break;
  16086. }
  16087. }
  16088. duk_replace(ctx, index);
  16089. skip_replace:
  16090. return duk_require_string(ctx, index);
  16091. }
  16092. DUK_INTERNAL duk_hstring *duk_to_hstring(duk_context *ctx, duk_idx_t index) {
  16093. duk_hstring *ret;
  16094. DUK_ASSERT_CTX_VALID(ctx);
  16095. duk_to_string(ctx, index);
  16096. ret = duk_get_hstring(ctx, index);
  16097. DUK_ASSERT(ret != NULL);
  16098. return ret;
  16099. }
  16100. DUK_EXTERNAL void *duk_to_buffer_raw(duk_context *ctx, duk_idx_t index, duk_size_t *out_size, duk_uint_t mode) {
  16101. duk_hthread *thr = (duk_hthread *) ctx;
  16102. duk_hbuffer *h_buf;
  16103. const duk_uint8_t *src_data;
  16104. duk_size_t src_size;
  16105. duk_uint8_t *dst_data;
  16106. DUK_ASSERT_CTX_VALID(ctx);
  16107. DUK_UNREF(thr);
  16108. index = duk_require_normalize_index(ctx, index);
  16109. h_buf = duk_get_hbuffer(ctx, index);
  16110. if (h_buf != NULL) {
  16111. /* Buffer is kept as is, with the fixed/dynamic nature of the
  16112. * buffer only changed if requested.
  16113. */
  16114. duk_uint_t tmp;
  16115. src_data = (const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_buf);
  16116. src_size = DUK_HBUFFER_GET_SIZE(h_buf);
  16117. tmp = (DUK_HBUFFER_HAS_DYNAMIC(h_buf) ? DUK_BUF_MODE_DYNAMIC : DUK_BUF_MODE_FIXED);
  16118. if (tmp == mode || mode == DUK_BUF_MODE_DONTCARE) {
  16119. /* Note: src_data may be NULL if input is a zero-size
  16120. * dynamic buffer.
  16121. */
  16122. dst_data = (duk_uint8_t *) src_data;
  16123. goto skip_copy;
  16124. }
  16125. } else {
  16126. /* Non-buffer value is first ToString() coerced, then converted
  16127. * to a buffer (fixed buffer is used unless a dynamic buffer is
  16128. * explicitly requested).
  16129. */
  16130. src_data = (const duk_uint8_t *) duk_to_lstring(ctx, index, &src_size);
  16131. }
  16132. dst_data = (duk_uint8_t *) duk_push_buffer(ctx, src_size, (mode == DUK_BUF_MODE_DYNAMIC) /*dynamic*/);
  16133. if (DUK_LIKELY(src_size > 0)) {
  16134. /* When src_size == 0, src_data may be NULL (if source
  16135. * buffer is dynamic), and dst_data may be NULL (if
  16136. * target buffer is dynamic). Avoid zero-size memcpy()
  16137. * with an invalid pointer.
  16138. */
  16139. DUK_MEMCPY(dst_data, src_data, src_size);
  16140. }
  16141. duk_replace(ctx, index);
  16142. skip_copy:
  16143. if (out_size) {
  16144. *out_size = src_size;
  16145. }
  16146. return dst_data;
  16147. }
  16148. DUK_EXTERNAL void *duk_to_pointer(duk_context *ctx, duk_idx_t index) {
  16149. duk_tval *tv;
  16150. void *res;
  16151. DUK_ASSERT_CTX_VALID(ctx);
  16152. index = duk_require_normalize_index(ctx, index);
  16153. tv = duk_require_tval(ctx, index);
  16154. DUK_ASSERT(tv != NULL);
  16155. switch (DUK_TVAL_GET_TAG(tv)) {
  16156. case DUK_TAG_UNDEFINED:
  16157. case DUK_TAG_NULL:
  16158. case DUK_TAG_BOOLEAN:
  16159. res = NULL;
  16160. break;
  16161. case DUK_TAG_POINTER:
  16162. res = DUK_TVAL_GET_POINTER(tv);
  16163. break;
  16164. case DUK_TAG_STRING:
  16165. case DUK_TAG_OBJECT:
  16166. case DUK_TAG_BUFFER:
  16167. /* Heap allocated: return heap pointer which is NOT useful
  16168. * for the caller, except for debugging.
  16169. */
  16170. res = (void *) DUK_TVAL_GET_HEAPHDR(tv);
  16171. break;
  16172. case DUK_TAG_LIGHTFUNC:
  16173. /* Function pointers do not always cast correctly to void *
  16174. * (depends on memory and segmentation model for instance),
  16175. * so they coerce to NULL.
  16176. */
  16177. res = NULL;
  16178. break;
  16179. #if defined(DUK_USE_FASTINT)
  16180. case DUK_TAG_FASTINT:
  16181. #endif
  16182. default:
  16183. /* number */
  16184. res = NULL;
  16185. break;
  16186. }
  16187. duk_push_pointer(ctx, res);
  16188. duk_replace(ctx, index);
  16189. return res;
  16190. }
  16191. DUK_EXTERNAL void duk_to_object(duk_context *ctx, duk_idx_t index) {
  16192. duk_hthread *thr = (duk_hthread *) ctx;
  16193. duk_tval *tv;
  16194. duk_uint_t flags = 0; /* shared flags for a subset of types */
  16195. duk_small_int_t proto = 0;
  16196. DUK_ASSERT_CTX_VALID(ctx);
  16197. index = duk_require_normalize_index(ctx, index);
  16198. tv = duk_require_tval(ctx, index);
  16199. DUK_ASSERT(tv != NULL);
  16200. switch (DUK_TVAL_GET_TAG(tv)) {
  16201. case DUK_TAG_UNDEFINED:
  16202. case DUK_TAG_NULL: {
  16203. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_OBJECT_COERCIBLE);
  16204. break;
  16205. }
  16206. case DUK_TAG_BOOLEAN: {
  16207. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  16208. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BOOLEAN);
  16209. proto = DUK_BIDX_BOOLEAN_PROTOTYPE;
  16210. goto create_object;
  16211. }
  16212. case DUK_TAG_STRING: {
  16213. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  16214. DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ |
  16215. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_STRING);
  16216. proto = DUK_BIDX_STRING_PROTOTYPE;
  16217. goto create_object;
  16218. }
  16219. case DUK_TAG_OBJECT: {
  16220. /* nop */
  16221. break;
  16222. }
  16223. case DUK_TAG_BUFFER: {
  16224. /* A plain buffer coerces to a Duktape.Buffer because it's the
  16225. * object counterpart of the plain buffer value. But it might
  16226. * still make more sense to produce an ArrayBuffer here?
  16227. */
  16228. duk_hbufferobject *h_bufobj;
  16229. duk_hbuffer *h_val;
  16230. h_val = DUK_TVAL_GET_BUFFER(tv);
  16231. DUK_ASSERT(h_val != NULL);
  16232. h_bufobj = duk_push_bufferobject(ctx,
  16233. DUK_HOBJECT_FLAG_EXTENSIBLE |
  16234. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  16235. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER),
  16236. DUK_BIDX_BUFFER_PROTOTYPE);
  16237. DUK_ASSERT(h_bufobj != NULL);
  16238. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE((duk_hobject *) h_bufobj));
  16239. DUK_ASSERT(DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) h_bufobj));
  16240. h_bufobj->buf = h_val;
  16241. DUK_HBUFFER_INCREF(thr, h_val);
  16242. DUK_ASSERT(h_bufobj->offset == 0);
  16243. h_bufobj->length = DUK_HBUFFER_GET_SIZE(h_val);
  16244. DUK_ASSERT(h_bufobj->shift == 0);
  16245. DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8);
  16246. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  16247. goto replace_value;
  16248. }
  16249. case DUK_TAG_POINTER: {
  16250. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  16251. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_POINTER);
  16252. proto = DUK_BIDX_POINTER_PROTOTYPE;
  16253. goto create_object;
  16254. }
  16255. case DUK_TAG_LIGHTFUNC: {
  16256. /* Lightfunc coerces to a Function instance with concrete
  16257. * properties. Since 'length' is virtual for Duktape/C
  16258. * functions, don't need to define that.
  16259. *
  16260. * The result is made extensible to mimic what happens to
  16261. * strings:
  16262. * > Object.isExtensible(Object('foo'))
  16263. * true
  16264. */
  16265. duk_small_uint_t lf_flags;
  16266. duk_small_uint_t nargs;
  16267. duk_small_uint_t lf_len;
  16268. duk_c_function func;
  16269. duk_hnativefunction *nf;
  16270. DUK_TVAL_GET_LIGHTFUNC(tv, func, lf_flags);
  16271. nargs = DUK_LFUNC_FLAGS_GET_NARGS(lf_flags);
  16272. if (nargs == DUK_LFUNC_NARGS_VARARGS) {
  16273. nargs = DUK_VARARGS;
  16274. }
  16275. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  16276. DUK_HOBJECT_FLAG_CONSTRUCTABLE |
  16277. DUK_HOBJECT_FLAG_NATIVEFUNCTION |
  16278. DUK_HOBJECT_FLAG_NEWENV |
  16279. DUK_HOBJECT_FLAG_STRICT |
  16280. DUK_HOBJECT_FLAG_NOTAIL |
  16281. /* DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC: omitted here intentionally */
  16282. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION);
  16283. (void) duk__push_c_function_raw(ctx, func, (duk_idx_t) nargs, flags);
  16284. lf_len = DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags);
  16285. if (lf_len != nargs) {
  16286. /* Explicit length is only needed if it differs from 'nargs'. */
  16287. duk_push_int(ctx, (duk_int_t) lf_len);
  16288. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE);
  16289. }
  16290. duk_push_lightfunc_name(ctx, tv);
  16291. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE);
  16292. nf = duk_get_hnativefunction(ctx, -1);
  16293. DUK_ASSERT(nf != NULL);
  16294. nf->magic = (duk_int16_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags);
  16295. /* Enable DUKFUNC exotic behavior once properties are set up. */
  16296. DUK_HOBJECT_SET_EXOTIC_DUKFUNC((duk_hobject *) nf);
  16297. goto replace_value;
  16298. }
  16299. #if defined(DUK_USE_FASTINT)
  16300. case DUK_TAG_FASTINT:
  16301. #endif
  16302. default: {
  16303. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  16304. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_NUMBER);
  16305. proto = DUK_BIDX_NUMBER_PROTOTYPE;
  16306. goto create_object;
  16307. }
  16308. }
  16309. return;
  16310. create_object:
  16311. (void) duk_push_object_helper(ctx, flags, proto);
  16312. /* Note: Boolean prototype's internal value property is not writable,
  16313. * but duk_xdef_prop_stridx() disregards the write protection. Boolean
  16314. * instances are immutable.
  16315. *
  16316. * String and buffer special behaviors are already enabled which is not
  16317. * ideal, but a write to the internal value is not affected by them.
  16318. */
  16319. duk_dup(ctx, index);
  16320. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE);
  16321. replace_value:
  16322. duk_replace(ctx, index);
  16323. }
  16324. /*
  16325. * Type checking
  16326. */
  16327. DUK_LOCAL duk_bool_t duk__tag_check(duk_context *ctx, duk_idx_t index, duk_small_uint_t tag) {
  16328. duk_tval *tv;
  16329. tv = duk_get_tval(ctx, index);
  16330. if (!tv) {
  16331. return 0;
  16332. }
  16333. return (DUK_TVAL_GET_TAG(tv) == tag);
  16334. }
  16335. DUK_LOCAL duk_bool_t duk__obj_flag_any_default_false(duk_context *ctx, duk_idx_t index, duk_uint_t flag_mask) {
  16336. duk_hobject *obj;
  16337. DUK_ASSERT_CTX_VALID(ctx);
  16338. obj = duk_get_hobject(ctx, index);
  16339. if (obj) {
  16340. return (DUK_HEAPHDR_CHECK_FLAG_BITS((duk_heaphdr *) obj, flag_mask) ? 1 : 0);
  16341. }
  16342. return 0;
  16343. }
  16344. DUK_EXTERNAL duk_int_t duk_get_type(duk_context *ctx, duk_idx_t index) {
  16345. duk_tval *tv;
  16346. DUK_ASSERT_CTX_VALID(ctx);
  16347. tv = duk_get_tval(ctx, index);
  16348. if (!tv) {
  16349. return DUK_TYPE_NONE;
  16350. }
  16351. switch (DUK_TVAL_GET_TAG(tv)) {
  16352. case DUK_TAG_UNDEFINED:
  16353. return DUK_TYPE_UNDEFINED;
  16354. case DUK_TAG_NULL:
  16355. return DUK_TYPE_NULL;
  16356. case DUK_TAG_BOOLEAN:
  16357. return DUK_TYPE_BOOLEAN;
  16358. case DUK_TAG_STRING:
  16359. return DUK_TYPE_STRING;
  16360. case DUK_TAG_OBJECT:
  16361. return DUK_TYPE_OBJECT;
  16362. case DUK_TAG_BUFFER:
  16363. return DUK_TYPE_BUFFER;
  16364. case DUK_TAG_POINTER:
  16365. return DUK_TYPE_POINTER;
  16366. case DUK_TAG_LIGHTFUNC:
  16367. return DUK_TYPE_LIGHTFUNC;
  16368. #if defined(DUK_USE_FASTINT)
  16369. case DUK_TAG_FASTINT:
  16370. #endif
  16371. default:
  16372. /* Note: number has no explicit tag (in 8-byte representation) */
  16373. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  16374. return DUK_TYPE_NUMBER;
  16375. }
  16376. DUK_UNREACHABLE();
  16377. }
  16378. DUK_EXTERNAL duk_bool_t duk_check_type(duk_context *ctx, duk_idx_t index, duk_int_t type) {
  16379. DUK_ASSERT_CTX_VALID(ctx);
  16380. return (duk_get_type(ctx, index) == type) ? 1 : 0;
  16381. }
  16382. DUK_EXTERNAL duk_uint_t duk_get_type_mask(duk_context *ctx, duk_idx_t index) {
  16383. duk_tval *tv;
  16384. DUK_ASSERT_CTX_VALID(ctx);
  16385. tv = duk_get_tval(ctx, index);
  16386. if (!tv) {
  16387. return DUK_TYPE_MASK_NONE;
  16388. }
  16389. switch (DUK_TVAL_GET_TAG(tv)) {
  16390. case DUK_TAG_UNDEFINED:
  16391. return DUK_TYPE_MASK_UNDEFINED;
  16392. case DUK_TAG_NULL:
  16393. return DUK_TYPE_MASK_NULL;
  16394. case DUK_TAG_BOOLEAN:
  16395. return DUK_TYPE_MASK_BOOLEAN;
  16396. case DUK_TAG_STRING:
  16397. return DUK_TYPE_MASK_STRING;
  16398. case DUK_TAG_OBJECT:
  16399. return DUK_TYPE_MASK_OBJECT;
  16400. case DUK_TAG_BUFFER:
  16401. return DUK_TYPE_MASK_BUFFER;
  16402. case DUK_TAG_POINTER:
  16403. return DUK_TYPE_MASK_POINTER;
  16404. case DUK_TAG_LIGHTFUNC:
  16405. return DUK_TYPE_MASK_LIGHTFUNC;
  16406. #if defined(DUK_USE_FASTINT)
  16407. case DUK_TAG_FASTINT:
  16408. #endif
  16409. default:
  16410. /* Note: number has no explicit tag (in 8-byte representation) */
  16411. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  16412. return DUK_TYPE_MASK_NUMBER;
  16413. }
  16414. DUK_UNREACHABLE();
  16415. }
  16416. DUK_EXTERNAL duk_bool_t duk_check_type_mask(duk_context *ctx, duk_idx_t index, duk_uint_t mask) {
  16417. duk_hthread *thr = (duk_hthread *) ctx;
  16418. DUK_ASSERT_CTX_VALID(ctx);
  16419. if (duk_get_type_mask(ctx, index) & mask) {
  16420. return 1;
  16421. }
  16422. if (mask & DUK_TYPE_MASK_THROW) {
  16423. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_UNEXPECTED_TYPE);
  16424. DUK_UNREACHABLE();
  16425. }
  16426. return 0;
  16427. }
  16428. DUK_EXTERNAL duk_bool_t duk_is_undefined(duk_context *ctx, duk_idx_t index) {
  16429. DUK_ASSERT_CTX_VALID(ctx);
  16430. return duk__tag_check(ctx, index, DUK_TAG_UNDEFINED);
  16431. }
  16432. DUK_EXTERNAL duk_bool_t duk_is_null(duk_context *ctx, duk_idx_t index) {
  16433. DUK_ASSERT_CTX_VALID(ctx);
  16434. return duk__tag_check(ctx, index, DUK_TAG_NULL);
  16435. }
  16436. DUK_EXTERNAL duk_bool_t duk_is_null_or_undefined(duk_context *ctx, duk_idx_t index) {
  16437. duk_tval *tv;
  16438. duk_small_uint_t tag;
  16439. DUK_ASSERT_CTX_VALID(ctx);
  16440. tv = duk_get_tval(ctx, index);
  16441. if (!tv) {
  16442. return 0;
  16443. }
  16444. tag = DUK_TVAL_GET_TAG(tv);
  16445. return (tag == DUK_TAG_UNDEFINED) || (tag == DUK_TAG_NULL);
  16446. }
  16447. DUK_EXTERNAL duk_bool_t duk_is_boolean(duk_context *ctx, duk_idx_t index) {
  16448. DUK_ASSERT_CTX_VALID(ctx);
  16449. return duk__tag_check(ctx, index, DUK_TAG_BOOLEAN);
  16450. }
  16451. DUK_EXTERNAL duk_bool_t duk_is_number(duk_context *ctx, duk_idx_t index) {
  16452. duk_tval *tv;
  16453. DUK_ASSERT_CTX_VALID(ctx);
  16454. /*
  16455. * Number is special because it doesn't have a specific
  16456. * tag in the 8-byte representation.
  16457. */
  16458. /* XXX: shorter version for 12-byte representation? */
  16459. tv = duk_get_tval(ctx, index);
  16460. if (!tv) {
  16461. return 0;
  16462. }
  16463. return DUK_TVAL_IS_NUMBER(tv);
  16464. }
  16465. DUK_EXTERNAL duk_bool_t duk_is_nan(duk_context *ctx, duk_idx_t index) {
  16466. /* XXX: This will now return false for non-numbers, even though they would
  16467. * coerce to NaN (as a general rule). In particular, duk_get_number()
  16468. * returns a NaN for non-numbers, so should this function also return
  16469. * true for non-numbers?
  16470. */
  16471. duk_tval *tv;
  16472. DUK_ASSERT_CTX_VALID(ctx);
  16473. tv = duk_get_tval(ctx, index);
  16474. if (!tv || !DUK_TVAL_IS_NUMBER(tv)) {
  16475. return 0;
  16476. }
  16477. return DUK_ISNAN(DUK_TVAL_GET_NUMBER(tv));
  16478. }
  16479. DUK_EXTERNAL duk_bool_t duk_is_string(duk_context *ctx, duk_idx_t index) {
  16480. DUK_ASSERT_CTX_VALID(ctx);
  16481. return duk__tag_check(ctx, index, DUK_TAG_STRING);
  16482. }
  16483. DUK_EXTERNAL duk_bool_t duk_is_object(duk_context *ctx, duk_idx_t index) {
  16484. DUK_ASSERT_CTX_VALID(ctx);
  16485. return duk__tag_check(ctx, index, DUK_TAG_OBJECT);
  16486. }
  16487. DUK_EXTERNAL duk_bool_t duk_is_buffer(duk_context *ctx, duk_idx_t index) {
  16488. DUK_ASSERT_CTX_VALID(ctx);
  16489. return duk__tag_check(ctx, index, DUK_TAG_BUFFER);
  16490. }
  16491. DUK_EXTERNAL duk_bool_t duk_is_pointer(duk_context *ctx, duk_idx_t index) {
  16492. DUK_ASSERT_CTX_VALID(ctx);
  16493. return duk__tag_check(ctx, index, DUK_TAG_POINTER);
  16494. }
  16495. DUK_EXTERNAL duk_bool_t duk_is_lightfunc(duk_context *ctx, duk_idx_t index) {
  16496. DUK_ASSERT_CTX_VALID(ctx);
  16497. return duk__tag_check(ctx, index, DUK_TAG_LIGHTFUNC);
  16498. }
  16499. DUK_EXTERNAL duk_bool_t duk_is_array(duk_context *ctx, duk_idx_t index) {
  16500. duk_hobject *obj;
  16501. DUK_ASSERT_CTX_VALID(ctx);
  16502. obj = duk_get_hobject(ctx, index);
  16503. if (obj) {
  16504. return (DUK_HOBJECT_GET_CLASS_NUMBER(obj) == DUK_HOBJECT_CLASS_ARRAY ? 1 : 0);
  16505. }
  16506. return 0;
  16507. }
  16508. DUK_EXTERNAL duk_bool_t duk_is_function(duk_context *ctx, duk_idx_t index) {
  16509. duk_tval *tv;
  16510. DUK_ASSERT_CTX_VALID(ctx);
  16511. tv = duk_get_tval(ctx, index);
  16512. if (tv && DUK_TVAL_IS_LIGHTFUNC(tv)) {
  16513. return 1;
  16514. }
  16515. return duk__obj_flag_any_default_false(ctx,
  16516. index,
  16517. DUK_HOBJECT_FLAG_COMPILEDFUNCTION |
  16518. DUK_HOBJECT_FLAG_NATIVEFUNCTION |
  16519. DUK_HOBJECT_FLAG_BOUND);
  16520. }
  16521. DUK_EXTERNAL duk_bool_t duk_is_c_function(duk_context *ctx, duk_idx_t index) {
  16522. DUK_ASSERT_CTX_VALID(ctx);
  16523. return duk__obj_flag_any_default_false(ctx,
  16524. index,
  16525. DUK_HOBJECT_FLAG_NATIVEFUNCTION);
  16526. }
  16527. DUK_EXTERNAL duk_bool_t duk_is_ecmascript_function(duk_context *ctx, duk_idx_t index) {
  16528. DUK_ASSERT_CTX_VALID(ctx);
  16529. return duk__obj_flag_any_default_false(ctx,
  16530. index,
  16531. DUK_HOBJECT_FLAG_COMPILEDFUNCTION);
  16532. }
  16533. DUK_EXTERNAL duk_bool_t duk_is_bound_function(duk_context *ctx, duk_idx_t index) {
  16534. DUK_ASSERT_CTX_VALID(ctx);
  16535. return duk__obj_flag_any_default_false(ctx,
  16536. index,
  16537. DUK_HOBJECT_FLAG_BOUND);
  16538. }
  16539. DUK_EXTERNAL duk_bool_t duk_is_thread(duk_context *ctx, duk_idx_t index) {
  16540. DUK_ASSERT_CTX_VALID(ctx);
  16541. return duk__obj_flag_any_default_false(ctx,
  16542. index,
  16543. DUK_HOBJECT_FLAG_THREAD);
  16544. }
  16545. DUK_EXTERNAL duk_bool_t duk_is_callable(duk_context *ctx, duk_idx_t index) {
  16546. /* XXX: currently same as duk_is_function() */
  16547. DUK_ASSERT_CTX_VALID(ctx);
  16548. return duk_is_function(ctx, index);
  16549. }
  16550. DUK_EXTERNAL duk_bool_t duk_is_dynamic_buffer(duk_context *ctx, duk_idx_t index) {
  16551. duk_tval *tv;
  16552. DUK_ASSERT_CTX_VALID(ctx);
  16553. tv = duk_get_tval(ctx, index);
  16554. if (DUK_TVAL_IS_BUFFER(tv)) {
  16555. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  16556. DUK_ASSERT(h != NULL);
  16557. return (DUK_HBUFFER_HAS_DYNAMIC(h) ? 1 : 0);
  16558. }
  16559. return 0;
  16560. }
  16561. DUK_EXTERNAL duk_bool_t duk_is_fixed_buffer(duk_context *ctx, duk_idx_t index) {
  16562. duk_tval *tv;
  16563. DUK_ASSERT_CTX_VALID(ctx);
  16564. tv = duk_get_tval(ctx, index);
  16565. if (DUK_TVAL_IS_BUFFER(tv)) {
  16566. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  16567. DUK_ASSERT(h != NULL);
  16568. return (DUK_HBUFFER_HAS_DYNAMIC(h) ? 0 : 1);
  16569. }
  16570. return 0;
  16571. }
  16572. /* XXX: make macro in API */
  16573. DUK_EXTERNAL duk_bool_t duk_is_primitive(duk_context *ctx, duk_idx_t index) {
  16574. DUK_ASSERT_CTX_VALID(ctx);
  16575. return !duk_is_object(ctx, index);
  16576. }
  16577. DUK_EXTERNAL duk_errcode_t duk_get_error_code(duk_context *ctx, duk_idx_t index) {
  16578. duk_hthread *thr = (duk_hthread *) ctx;
  16579. duk_hobject *h;
  16580. duk_uint_t sanity;
  16581. DUK_ASSERT_CTX_VALID(ctx);
  16582. h = duk_get_hobject(ctx, index);
  16583. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  16584. do {
  16585. if (!h) {
  16586. return DUK_ERR_NONE;
  16587. }
  16588. if (h == thr->builtins[DUK_BIDX_EVAL_ERROR_PROTOTYPE]) {
  16589. return DUK_ERR_EVAL_ERROR;
  16590. }
  16591. if (h == thr->builtins[DUK_BIDX_RANGE_ERROR_PROTOTYPE]) {
  16592. return DUK_ERR_RANGE_ERROR;
  16593. }
  16594. if (h == thr->builtins[DUK_BIDX_REFERENCE_ERROR_PROTOTYPE]) {
  16595. return DUK_ERR_REFERENCE_ERROR;
  16596. }
  16597. if (h == thr->builtins[DUK_BIDX_SYNTAX_ERROR_PROTOTYPE]) {
  16598. return DUK_ERR_SYNTAX_ERROR;
  16599. }
  16600. if (h == thr->builtins[DUK_BIDX_TYPE_ERROR_PROTOTYPE]) {
  16601. return DUK_ERR_TYPE_ERROR;
  16602. }
  16603. if (h == thr->builtins[DUK_BIDX_URI_ERROR_PROTOTYPE]) {
  16604. return DUK_ERR_URI_ERROR;
  16605. }
  16606. if (h == thr->builtins[DUK_BIDX_ERROR_PROTOTYPE]) {
  16607. return DUK_ERR_ERROR;
  16608. }
  16609. h = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
  16610. } while (--sanity > 0);
  16611. return DUK_ERR_NONE;
  16612. }
  16613. /*
  16614. * Pushers
  16615. */
  16616. DUK_INTERNAL void duk_push_tval(duk_context *ctx, duk_tval *tv) {
  16617. duk_hthread *thr;
  16618. duk_tval *tv_slot;
  16619. DUK_ASSERT_CTX_VALID(ctx);
  16620. DUK_ASSERT(tv != NULL);
  16621. thr = (duk_hthread *) ctx;
  16622. DUK__CHECK_SPACE();
  16623. tv_slot = thr->valstack_top++;
  16624. DUK_TVAL_SET_TVAL(tv_slot, tv);
  16625. DUK_TVAL_INCREF(thr, tv); /* no side effects */
  16626. }
  16627. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  16628. /* Right now only needed by the debugger. */
  16629. DUK_INTERNAL void duk_push_unused(duk_context *ctx) {
  16630. duk_hthread *thr;
  16631. duk_tval *tv_slot;
  16632. DUK_ASSERT_CTX_VALID(ctx);
  16633. thr = (duk_hthread *) ctx;
  16634. DUK__CHECK_SPACE();
  16635. tv_slot = thr->valstack_top++;
  16636. DUK_TVAL_SET_UNDEFINED_UNUSED(tv_slot);
  16637. }
  16638. #endif
  16639. DUK_EXTERNAL void duk_push_undefined(duk_context *ctx) {
  16640. duk_hthread *thr;
  16641. duk_tval *tv_slot;
  16642. DUK_ASSERT_CTX_VALID(ctx);
  16643. thr = (duk_hthread *) ctx;
  16644. DUK__CHECK_SPACE();
  16645. tv_slot = thr->valstack_top++;
  16646. DUK_TVAL_SET_UNDEFINED_ACTUAL(tv_slot);
  16647. }
  16648. DUK_EXTERNAL void duk_push_null(duk_context *ctx) {
  16649. duk_hthread *thr;
  16650. duk_tval *tv_slot;
  16651. DUK_ASSERT_CTX_VALID(ctx);
  16652. thr = (duk_hthread *) ctx;
  16653. DUK__CHECK_SPACE();
  16654. tv_slot = thr->valstack_top++;
  16655. DUK_TVAL_SET_NULL(tv_slot);
  16656. }
  16657. DUK_EXTERNAL void duk_push_boolean(duk_context *ctx, duk_bool_t val) {
  16658. duk_hthread *thr;
  16659. duk_tval *tv_slot;
  16660. duk_small_int_t b;
  16661. DUK_ASSERT_CTX_VALID(ctx);
  16662. thr = (duk_hthread *) ctx;
  16663. DUK__CHECK_SPACE();
  16664. b = (val ? 1 : 0); /* ensure value is 1 or 0 (not other non-zero) */
  16665. tv_slot = thr->valstack_top++;
  16666. DUK_TVAL_SET_BOOLEAN(tv_slot, b);
  16667. }
  16668. DUK_EXTERNAL void duk_push_true(duk_context *ctx) {
  16669. duk_hthread *thr;
  16670. duk_tval *tv_slot;
  16671. DUK_ASSERT_CTX_VALID(ctx);
  16672. thr = (duk_hthread *) ctx;
  16673. DUK__CHECK_SPACE();
  16674. tv_slot = thr->valstack_top++;
  16675. DUK_TVAL_SET_BOOLEAN_TRUE(tv_slot);
  16676. }
  16677. DUK_EXTERNAL void duk_push_false(duk_context *ctx) {
  16678. duk_hthread *thr;
  16679. duk_tval *tv_slot;
  16680. DUK_ASSERT_CTX_VALID(ctx);
  16681. thr = (duk_hthread *) ctx;
  16682. DUK__CHECK_SPACE();
  16683. tv_slot = thr->valstack_top++;
  16684. DUK_TVAL_SET_BOOLEAN_FALSE(tv_slot);
  16685. }
  16686. /* normalize NaN which may not match our canonical internal NaN */
  16687. DUK_EXTERNAL void duk_push_number(duk_context *ctx, duk_double_t val) {
  16688. duk_hthread *thr;
  16689. duk_tval *tv_slot;
  16690. duk_double_union du;
  16691. DUK_ASSERT_CTX_VALID(ctx);
  16692. thr = (duk_hthread *) ctx;
  16693. DUK__CHECK_SPACE();
  16694. du.d = val;
  16695. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  16696. tv_slot = thr->valstack_top++;
  16697. DUK_TVAL_SET_NUMBER(tv_slot, du.d);
  16698. }
  16699. DUK_EXTERNAL void duk_push_int(duk_context *ctx, duk_int_t val) {
  16700. #if defined(DUK_USE_FASTINT)
  16701. duk_hthread *thr;
  16702. duk_tval *tv_slot;
  16703. DUK_ASSERT_CTX_VALID(ctx);
  16704. thr = (duk_hthread *) ctx;
  16705. DUK__CHECK_SPACE();
  16706. tv_slot = thr->valstack_top++;
  16707. #if DUK_INT_MAX <= 0x7fffffffL
  16708. DUK_TVAL_SET_FASTINT_I32(tv_slot, (duk_int32_t) val);
  16709. #else
  16710. if (val >= DUK_FASTINT_MIN && val <= DUK_FASTINT_MAX) {
  16711. DUK_TVAL_SET_FASTINT(tv_slot, (duk_int64_t) val);
  16712. } else {
  16713. duk_double_t = (duk_double_t) val;
  16714. DUK_TVAL_SET_NUMBER(tv_slot, d);
  16715. }
  16716. #endif
  16717. #else /* DUK_USE_FASTINT */
  16718. duk_hthread *thr;
  16719. duk_tval *tv_slot;
  16720. duk_double_t d;
  16721. DUK_ASSERT_CTX_VALID(ctx);
  16722. thr = (duk_hthread *) ctx;
  16723. DUK__CHECK_SPACE();
  16724. d = (duk_double_t) val;
  16725. tv_slot = thr->valstack_top++;
  16726. DUK_TVAL_SET_NUMBER(tv_slot, d);
  16727. #endif /* DUK_USE_FASTINT */
  16728. }
  16729. DUK_EXTERNAL void duk_push_uint(duk_context *ctx, duk_uint_t val) {
  16730. #if defined(DUK_USE_FASTINT)
  16731. duk_hthread *thr;
  16732. duk_tval *tv_slot;
  16733. DUK_ASSERT_CTX_VALID(ctx);
  16734. thr = (duk_hthread *) ctx;
  16735. DUK__CHECK_SPACE();
  16736. tv_slot = thr->valstack_top++;
  16737. #if DUK_UINT_MAX <= 0xffffffffUL
  16738. DUK_TVAL_SET_FASTINT_U32(tv_slot, (duk_uint32_t) val);
  16739. #else
  16740. if (val <= DUK_FASTINT_MAX) { /* val is unsigned so >= 0 */
  16741. /* XXX: take advantage of val being unsigned, no need to mask */
  16742. DUK_TVAL_SET_FASTINT(tv_slot, (duk_int64_t) val);
  16743. } else {
  16744. duk_double_t = (duk_double_t) val;
  16745. DUK_TVAL_SET_NUMBER(tv_slot, d);
  16746. }
  16747. #endif
  16748. #else /* DUK_USE_FASTINT */
  16749. duk_hthread *thr;
  16750. duk_tval *tv_slot;
  16751. duk_double_t d;
  16752. DUK_ASSERT_CTX_VALID(ctx);
  16753. thr = (duk_hthread *) ctx;
  16754. DUK__CHECK_SPACE();
  16755. d = (duk_double_t) val;
  16756. tv_slot = thr->valstack_top++;
  16757. DUK_TVAL_SET_NUMBER(tv_slot, d);
  16758. #endif /* DUK_USE_FASTINT */
  16759. }
  16760. DUK_EXTERNAL void duk_push_nan(duk_context *ctx) {
  16761. duk_hthread *thr;
  16762. duk_tval *tv_slot;
  16763. duk_double_union du;
  16764. DUK_ASSERT_CTX_VALID(ctx);
  16765. thr = (duk_hthread *) ctx;
  16766. DUK__CHECK_SPACE();
  16767. DUK_DBLUNION_SET_NAN(&du);
  16768. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  16769. tv_slot = thr->valstack_top++;
  16770. DUK_TVAL_SET_NUMBER(tv_slot, du.d);
  16771. }
  16772. DUK_EXTERNAL const char *duk_push_lstring(duk_context *ctx, const char *str, duk_size_t len) {
  16773. duk_hthread *thr = (duk_hthread *) ctx;
  16774. duk_hstring *h;
  16775. duk_tval *tv_slot;
  16776. DUK_ASSERT_CTX_VALID(ctx);
  16777. /* check stack before interning (avoid hanging temp) */
  16778. if (thr->valstack_top >= thr->valstack_end) {
  16779. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  16780. }
  16781. /* NULL with zero length represents an empty string; NULL with higher
  16782. * length is also now trated like an empty string although it is
  16783. * a bit dubious. This is unlike duk_push_string() which pushes a
  16784. * 'null' if the input string is a NULL.
  16785. */
  16786. if (!str) {
  16787. len = 0;
  16788. }
  16789. /* Check for maximum string length */
  16790. if (len > DUK_HSTRING_MAX_BYTELEN) {
  16791. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_STRING_TOO_LONG);
  16792. }
  16793. h = duk_heap_string_intern_checked(thr, (duk_uint8_t *) str, (duk_uint32_t) len);
  16794. DUK_ASSERT(h != NULL);
  16795. tv_slot = thr->valstack_top++;
  16796. DUK_TVAL_SET_STRING(tv_slot, h);
  16797. DUK_HSTRING_INCREF(thr, h); /* no side effects */
  16798. return (const char *) DUK_HSTRING_GET_DATA(h);
  16799. }
  16800. DUK_EXTERNAL const char *duk_push_string(duk_context *ctx, const char *str) {
  16801. DUK_ASSERT_CTX_VALID(ctx);
  16802. if (str) {
  16803. return duk_push_lstring(ctx, str, DUK_STRLEN(str));
  16804. } else {
  16805. duk_push_null(ctx);
  16806. return NULL;
  16807. }
  16808. }
  16809. #ifdef DUK_USE_FILE_IO
  16810. /* This is a bit clunky because it is ANSI C portable. Should perhaps
  16811. * relocate to another file because this is potentially platform
  16812. * dependent.
  16813. */
  16814. DUK_EXTERNAL const char *duk_push_string_file_raw(duk_context *ctx, const char *path, duk_uint_t flags) {
  16815. duk_hthread *thr = (duk_hthread *) ctx;
  16816. duk_file *f = NULL;
  16817. char *buf;
  16818. long sz; /* ANSI C typing */
  16819. DUK_ASSERT_CTX_VALID(ctx);
  16820. if (!path) {
  16821. goto fail;
  16822. }
  16823. f = DUK_FOPEN(path, "rb");
  16824. if (!f) {
  16825. goto fail;
  16826. }
  16827. if (DUK_FSEEK(f, 0, SEEK_END) < 0) {
  16828. goto fail;
  16829. }
  16830. sz = DUK_FTELL(f);
  16831. if (sz < 0) {
  16832. goto fail;
  16833. }
  16834. if (DUK_FSEEK(f, 0, SEEK_SET) < 0) {
  16835. goto fail;
  16836. }
  16837. buf = (char *) duk_push_fixed_buffer(ctx, (duk_size_t) sz);
  16838. DUK_ASSERT(buf != NULL);
  16839. if ((duk_size_t) DUK_FREAD(buf, 1, (size_t) sz, f) != (duk_size_t) sz) {
  16840. goto fail;
  16841. }
  16842. (void) DUK_FCLOSE(f); /* ignore fclose() error */
  16843. f = NULL;
  16844. return duk_to_string(ctx, -1);
  16845. fail:
  16846. if (f) {
  16847. DUK_FCLOSE(f);
  16848. }
  16849. if (flags != 0) {
  16850. DUK_ASSERT(flags == DUK_STRING_PUSH_SAFE); /* only flag now */
  16851. duk_push_undefined(ctx);
  16852. } else {
  16853. /* XXX: string not shared because it is conditional */
  16854. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "read file error");
  16855. }
  16856. return NULL;
  16857. }
  16858. #else
  16859. DUK_EXTERNAL const char *duk_push_string_file_raw(duk_context *ctx, const char *path, duk_uint_t flags) {
  16860. duk_hthread *thr = (duk_hthread *) ctx;
  16861. DUK_ASSERT_CTX_VALID(ctx);
  16862. DUK_UNREF(path);
  16863. if (flags != 0) {
  16864. DUK_ASSERT(flags == DUK_STRING_PUSH_SAFE); /* only flag now */
  16865. duk_push_undefined(ctx);
  16866. } else {
  16867. /* XXX: string not shared because it is conditional */
  16868. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "file I/O disabled");
  16869. }
  16870. return NULL;
  16871. }
  16872. #endif /* DUK_USE_FILE_IO */
  16873. DUK_EXTERNAL void duk_push_pointer(duk_context *ctx, void *val) {
  16874. duk_hthread *thr;
  16875. duk_tval *tv_slot;
  16876. DUK_ASSERT_CTX_VALID(ctx);
  16877. thr = (duk_hthread *) ctx;
  16878. DUK__CHECK_SPACE();
  16879. tv_slot = thr->valstack_top++;
  16880. DUK_TVAL_SET_POINTER(tv_slot, val);
  16881. }
  16882. #define DUK__PUSH_THIS_FLAG_CHECK_COERC (1 << 0)
  16883. #define DUK__PUSH_THIS_FLAG_TO_OBJECT (1 << 1)
  16884. #define DUK__PUSH_THIS_FLAG_TO_STRING (1 << 2)
  16885. DUK_LOCAL void duk__push_this_helper(duk_context *ctx, duk_small_uint_t flags) {
  16886. duk_hthread *thr = (duk_hthread *) ctx;
  16887. DUK_ASSERT(thr != NULL);
  16888. DUK_ASSERT_CTX_VALID(ctx);
  16889. DUK_ASSERT_DISABLE(thr->callstack_top >= 0); /* avoid warning (unsigned) */
  16890. DUK_ASSERT(thr->callstack_top <= thr->callstack_size);
  16891. if (thr->callstack_top == 0) {
  16892. if (flags & DUK__PUSH_THIS_FLAG_CHECK_COERC) {
  16893. goto type_error;
  16894. }
  16895. duk_push_undefined(ctx);
  16896. } else {
  16897. duk_tval tv_tmp;
  16898. duk_tval *tv;
  16899. /* 'this' binding is just before current activation's bottom */
  16900. DUK_ASSERT(thr->valstack_bottom > thr->valstack);
  16901. tv = thr->valstack_bottom - 1;
  16902. if (flags & DUK__PUSH_THIS_FLAG_CHECK_COERC) {
  16903. if (DUK_TVAL_IS_UNDEFINED(tv) || DUK_TVAL_IS_NULL(tv)) {
  16904. goto type_error;
  16905. }
  16906. }
  16907. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  16908. duk_push_tval(ctx, &tv_tmp);
  16909. }
  16910. if (flags & DUK__PUSH_THIS_FLAG_TO_OBJECT) {
  16911. duk_to_object(ctx, -1);
  16912. } else if (flags & DUK__PUSH_THIS_FLAG_TO_STRING) {
  16913. duk_to_string(ctx, -1);
  16914. }
  16915. return;
  16916. type_error:
  16917. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_OBJECT_COERCIBLE);
  16918. }
  16919. DUK_EXTERNAL void duk_push_this(duk_context *ctx) {
  16920. DUK_ASSERT_CTX_VALID(ctx);
  16921. duk__push_this_helper(ctx, 0 /*flags*/);
  16922. }
  16923. DUK_INTERNAL void duk_push_this_check_object_coercible(duk_context *ctx) {
  16924. DUK_ASSERT_CTX_VALID(ctx);
  16925. duk__push_this_helper(ctx, DUK__PUSH_THIS_FLAG_CHECK_COERC /*flags*/);
  16926. }
  16927. DUK_INTERNAL duk_hobject *duk_push_this_coercible_to_object(duk_context *ctx) {
  16928. duk_hobject *h;
  16929. DUK_ASSERT_CTX_VALID(ctx);
  16930. duk__push_this_helper(ctx, DUK__PUSH_THIS_FLAG_CHECK_COERC |
  16931. DUK__PUSH_THIS_FLAG_TO_OBJECT /*flags*/);
  16932. h = duk_get_hobject(ctx, -1);
  16933. DUK_ASSERT(h != NULL);
  16934. return h;
  16935. }
  16936. DUK_INTERNAL duk_hstring *duk_push_this_coercible_to_string(duk_context *ctx) {
  16937. duk_hstring *h;
  16938. DUK_ASSERT_CTX_VALID(ctx);
  16939. duk__push_this_helper(ctx, DUK__PUSH_THIS_FLAG_CHECK_COERC |
  16940. DUK__PUSH_THIS_FLAG_TO_STRING /*flags*/);
  16941. h = duk_get_hstring(ctx, -1);
  16942. DUK_ASSERT(h != NULL);
  16943. return h;
  16944. }
  16945. DUK_INTERNAL duk_tval *duk_get_borrowed_this_tval(duk_context *ctx) {
  16946. duk_hthread *thr;
  16947. DUK_ASSERT(ctx != NULL);
  16948. thr = (duk_hthread *) ctx;
  16949. DUK_ASSERT(thr->callstack_top > 0); /* caller required to know */
  16950. DUK_ASSERT(thr->valstack_bottom > thr->valstack); /* consequence of above */
  16951. DUK_ASSERT(thr->valstack_bottom - 1 >= thr->valstack); /* 'this' binding exists */
  16952. return thr->valstack_bottom - 1;
  16953. }
  16954. DUK_EXTERNAL void duk_push_current_function(duk_context *ctx) {
  16955. duk_hthread *thr = (duk_hthread *) ctx;
  16956. duk_activation *act;
  16957. DUK_ASSERT_CTX_VALID(ctx);
  16958. DUK_ASSERT(thr != NULL);
  16959. DUK_ASSERT_DISABLE(thr->callstack_top >= 0);
  16960. DUK_ASSERT(thr->callstack_top <= thr->callstack_size);
  16961. act = duk_hthread_get_current_activation(thr);
  16962. if (act) {
  16963. duk_push_tval(ctx, &act->tv_func);
  16964. } else {
  16965. duk_push_undefined(ctx);
  16966. }
  16967. }
  16968. DUK_EXTERNAL void duk_push_current_thread(duk_context *ctx) {
  16969. duk_hthread *thr = (duk_hthread *) ctx;
  16970. DUK_ASSERT_CTX_VALID(ctx);
  16971. DUK_ASSERT(thr != NULL);
  16972. if (thr->heap->curr_thread) {
  16973. duk_push_hobject(ctx, (duk_hobject *) thr->heap->curr_thread);
  16974. } else {
  16975. duk_push_undefined(ctx);
  16976. }
  16977. }
  16978. DUK_EXTERNAL void duk_push_global_object(duk_context *ctx) {
  16979. DUK_ASSERT_CTX_VALID(ctx);
  16980. duk_push_hobject_bidx(ctx, DUK_BIDX_GLOBAL);
  16981. }
  16982. /* XXX: size optimize */
  16983. DUK_LOCAL void duk__push_stash(duk_context *ctx) {
  16984. DUK_ASSERT_CTX_VALID(ctx);
  16985. if (!duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE)) {
  16986. DUK_DDD(DUK_DDDPRINT("creating heap/global/thread stash on first use"));
  16987. duk_pop(ctx);
  16988. duk_push_object_internal(ctx);
  16989. duk_dup_top(ctx);
  16990. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_C); /* [ ... parent stash stash ] -> [ ... parent stash ] */
  16991. }
  16992. duk_remove(ctx, -2);
  16993. }
  16994. DUK_EXTERNAL void duk_push_heap_stash(duk_context *ctx) {
  16995. duk_hthread *thr = (duk_hthread *) ctx;
  16996. duk_heap *heap;
  16997. DUK_ASSERT_CTX_VALID(ctx);
  16998. heap = thr->heap;
  16999. DUK_ASSERT(heap->heap_object != NULL);
  17000. duk_push_hobject(ctx, heap->heap_object);
  17001. duk__push_stash(ctx);
  17002. }
  17003. DUK_EXTERNAL void duk_push_global_stash(duk_context *ctx) {
  17004. DUK_ASSERT_CTX_VALID(ctx);
  17005. duk_push_global_object(ctx);
  17006. duk__push_stash(ctx);
  17007. }
  17008. DUK_EXTERNAL void duk_push_thread_stash(duk_context *ctx, duk_context *target_ctx) {
  17009. duk_hthread *thr = (duk_hthread *) ctx;
  17010. DUK_ASSERT_CTX_VALID(ctx);
  17011. if (!target_ctx) {
  17012. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  17013. return; /* not reached */
  17014. }
  17015. duk_push_hobject(ctx, (duk_hobject *) target_ctx);
  17016. duk__push_stash(ctx);
  17017. }
  17018. /* XXX: duk_ssize_t would be useful here */
  17019. DUK_LOCAL duk_int_t duk__try_push_vsprintf(duk_context *ctx, void *buf, duk_size_t sz, const char *fmt, va_list ap) {
  17020. duk_int_t len;
  17021. DUK_ASSERT_CTX_VALID(ctx);
  17022. DUK_UNREF(ctx);
  17023. /* NUL terminator handling doesn't matter here */
  17024. len = DUK_VSNPRINTF((char *) buf, sz, fmt, ap);
  17025. if (len < (duk_int_t) sz) {
  17026. /* Return value of 'sz' or more indicates output was (potentially)
  17027. * truncated.
  17028. */
  17029. return (duk_int_t) len;
  17030. }
  17031. return -1;
  17032. }
  17033. DUK_EXTERNAL const char *duk_push_vsprintf(duk_context *ctx, const char *fmt, va_list ap) {
  17034. duk_hthread *thr = (duk_hthread *) ctx;
  17035. duk_uint8_t stack_buf[DUK_PUSH_SPRINTF_INITIAL_SIZE];
  17036. duk_size_t sz = DUK_PUSH_SPRINTF_INITIAL_SIZE;
  17037. duk_bool_t pushed_buf = 0;
  17038. void *buf;
  17039. duk_int_t len; /* XXX: duk_ssize_t */
  17040. const char *res;
  17041. DUK_ASSERT_CTX_VALID(ctx);
  17042. /* special handling of fmt==NULL */
  17043. if (!fmt) {
  17044. duk_hstring *h_str;
  17045. duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING);
  17046. h_str = DUK_HTHREAD_STRING_EMPTY_STRING(thr); /* rely on interning, must be this string */
  17047. return (const char *) DUK_HSTRING_GET_DATA(h_str);
  17048. }
  17049. /* initial estimate based on format string */
  17050. sz = DUK_STRLEN(fmt) + 16; /* format plus something to avoid just missing */
  17051. if (sz < DUK_PUSH_SPRINTF_INITIAL_SIZE) {
  17052. sz = DUK_PUSH_SPRINTF_INITIAL_SIZE;
  17053. }
  17054. DUK_ASSERT(sz > 0);
  17055. /* Try to make do with a stack buffer to avoid allocating a temporary buffer.
  17056. * This works 99% of the time which is quite nice.
  17057. */
  17058. for (;;) {
  17059. va_list ap_copy; /* copied so that 'ap' can be reused */
  17060. if (sz <= sizeof(stack_buf)) {
  17061. buf = stack_buf;
  17062. } else if (!pushed_buf) {
  17063. pushed_buf = 1;
  17064. buf = duk_push_dynamic_buffer(ctx, sz);
  17065. } else {
  17066. buf = duk_resize_buffer(ctx, -1, sz);
  17067. }
  17068. DUK_ASSERT(buf != NULL);
  17069. DUK_VA_COPY(ap_copy, ap);
  17070. len = duk__try_push_vsprintf(ctx, buf, sz, fmt, ap_copy);
  17071. va_end(ap_copy);
  17072. if (len >= 0) {
  17073. break;
  17074. }
  17075. /* failed, resize and try again */
  17076. sz = sz * 2;
  17077. if (sz >= DUK_PUSH_SPRINTF_SANITY_LIMIT) {
  17078. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_SPRINTF_TOO_LONG);
  17079. }
  17080. }
  17081. /* Cannot use duk_to_string() on the buffer because it is usually
  17082. * larger than 'len'. Also, 'buf' is usually a stack buffer.
  17083. */
  17084. res = duk_push_lstring(ctx, (const char *) buf, (duk_size_t) len); /* [ buf? res ] */
  17085. if (pushed_buf) {
  17086. duk_remove(ctx, -2);
  17087. }
  17088. return res;
  17089. }
  17090. DUK_EXTERNAL const char *duk_push_sprintf(duk_context *ctx, const char *fmt, ...) {
  17091. va_list ap;
  17092. const char *ret;
  17093. DUK_ASSERT_CTX_VALID(ctx);
  17094. /* allow fmt==NULL */
  17095. va_start(ap, fmt);
  17096. ret = duk_push_vsprintf(ctx, fmt, ap);
  17097. va_end(ap);
  17098. return ret;
  17099. }
  17100. 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) {
  17101. duk_hthread *thr = (duk_hthread *) ctx;
  17102. duk_tval *tv_slot;
  17103. duk_hobject *h;
  17104. duk_idx_t ret;
  17105. DUK_ASSERT_CTX_VALID(ctx);
  17106. DUK_ASSERT(prototype_bidx == -1 ||
  17107. (prototype_bidx >= 0 && prototype_bidx < DUK_NUM_BUILTINS));
  17108. /* check stack first */
  17109. if (thr->valstack_top >= thr->valstack_end) {
  17110. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  17111. }
  17112. h = duk_hobject_alloc(thr->heap, hobject_flags_and_class);
  17113. if (!h) {
  17114. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_ALLOC_FAILED);
  17115. }
  17116. DUK_DDD(DUK_DDDPRINT("created object with flags: 0x%08lx", (unsigned long) h->hdr.h_flags));
  17117. tv_slot = thr->valstack_top;
  17118. DUK_TVAL_SET_OBJECT(tv_slot, h);
  17119. DUK_HOBJECT_INCREF(thr, h); /* no side effects */
  17120. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  17121. thr->valstack_top++;
  17122. /* object is now reachable */
  17123. if (prototype_bidx >= 0) {
  17124. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, thr->builtins[prototype_bidx]);
  17125. } else {
  17126. DUK_ASSERT(prototype_bidx == -1);
  17127. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h) == NULL);
  17128. }
  17129. return ret;
  17130. }
  17131. DUK_INTERNAL duk_idx_t duk_push_object_helper_proto(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_hobject *proto) {
  17132. duk_hthread *thr = (duk_hthread *) ctx;
  17133. duk_idx_t ret;
  17134. duk_hobject *h;
  17135. DUK_ASSERT_CTX_VALID(ctx);
  17136. ret = duk_push_object_helper(ctx, hobject_flags_and_class, -1);
  17137. h = duk_get_hobject(ctx, -1);
  17138. DUK_ASSERT(h != NULL);
  17139. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h) == NULL);
  17140. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, proto);
  17141. return ret;
  17142. }
  17143. DUK_EXTERNAL duk_idx_t duk_push_object(duk_context *ctx) {
  17144. DUK_ASSERT_CTX_VALID(ctx);
  17145. return duk_push_object_helper(ctx,
  17146. DUK_HOBJECT_FLAG_EXTENSIBLE |
  17147. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  17148. DUK_BIDX_OBJECT_PROTOTYPE);
  17149. }
  17150. DUK_EXTERNAL duk_idx_t duk_push_array(duk_context *ctx) {
  17151. duk_hthread *thr = (duk_hthread *) ctx;
  17152. duk_hobject *obj;
  17153. duk_idx_t ret;
  17154. DUK_ASSERT_CTX_VALID(ctx);
  17155. ret = duk_push_object_helper(ctx,
  17156. DUK_HOBJECT_FLAG_EXTENSIBLE |
  17157. DUK_HOBJECT_FLAG_ARRAY_PART |
  17158. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAY),
  17159. DUK_BIDX_ARRAY_PROTOTYPE);
  17160. obj = duk_require_hobject(ctx, ret);
  17161. /*
  17162. * An array must have a 'length' property (E5 Section 15.4.5.2).
  17163. * The special array behavior flag must only be enabled once the
  17164. * length property has been added.
  17165. *
  17166. * The internal property must be a number (and preferably a
  17167. * fastint if fastint support is enabled).
  17168. */
  17169. duk_push_int(ctx, 0);
  17170. #if defined(DUK_USE_FASTINT)
  17171. DUK_ASSERT(DUK_TVAL_IS_FASTINT(duk_require_tval(ctx, -1)));
  17172. #endif
  17173. duk_hobject_define_property_internal(thr,
  17174. obj,
  17175. DUK_HTHREAD_STRING_LENGTH(thr),
  17176. DUK_PROPDESC_FLAGS_W);
  17177. DUK_HOBJECT_SET_EXOTIC_ARRAY(obj);
  17178. return ret;
  17179. }
  17180. DUK_EXTERNAL duk_idx_t duk_push_thread_raw(duk_context *ctx, duk_uint_t flags) {
  17181. duk_hthread *thr = (duk_hthread *) ctx;
  17182. duk_hthread *obj;
  17183. duk_idx_t ret;
  17184. duk_tval *tv_slot;
  17185. DUK_ASSERT_CTX_VALID(ctx);
  17186. /* check stack first */
  17187. if (thr->valstack_top >= thr->valstack_end) {
  17188. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  17189. }
  17190. obj = duk_hthread_alloc(thr->heap,
  17191. DUK_HOBJECT_FLAG_EXTENSIBLE |
  17192. DUK_HOBJECT_FLAG_THREAD |
  17193. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_THREAD));
  17194. if (!obj) {
  17195. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_ALLOC_FAILED);
  17196. }
  17197. obj->state = DUK_HTHREAD_STATE_INACTIVE;
  17198. #if defined(DUK_USE_HEAPPTR16)
  17199. obj->strs16 = thr->strs16;
  17200. #else
  17201. obj->strs = thr->strs;
  17202. #endif
  17203. DUK_DDD(DUK_DDDPRINT("created thread object with flags: 0x%08lx", (unsigned long) obj->obj.hdr.h_flags));
  17204. /* make the new thread reachable */
  17205. tv_slot = thr->valstack_top;
  17206. DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
  17207. DUK_HTHREAD_INCREF(thr, obj);
  17208. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  17209. thr->valstack_top++;
  17210. /* important to do this *after* pushing, to make the thread reachable for gc */
  17211. if (!duk_hthread_init_stacks(thr->heap, obj)) {
  17212. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_ALLOC_FAILED);
  17213. }
  17214. /* initialize built-ins - either by copying or creating new ones */
  17215. if (flags & DUK_THREAD_NEW_GLOBAL_ENV) {
  17216. duk_hthread_create_builtin_objects(obj);
  17217. } else {
  17218. duk_hthread_copy_builtin_objects(thr, obj);
  17219. }
  17220. /* default prototype (Note: 'obj' must be reachable) */
  17221. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, obj->builtins[DUK_BIDX_THREAD_PROTOTYPE]);
  17222. /* Initial stack size satisfies the stack spare constraints so there
  17223. * is no need to require stack here.
  17224. */
  17225. DUK_ASSERT(DUK_VALSTACK_INITIAL_SIZE >=
  17226. DUK_VALSTACK_API_ENTRY_MINIMUM + DUK_VALSTACK_INTERNAL_EXTRA);
  17227. return ret;
  17228. }
  17229. DUK_INTERNAL duk_idx_t duk_push_compiledfunction(duk_context *ctx) {
  17230. duk_hthread *thr = (duk_hthread *) ctx;
  17231. duk_hcompiledfunction *obj;
  17232. duk_idx_t ret;
  17233. duk_tval *tv_slot;
  17234. DUK_ASSERT_CTX_VALID(ctx);
  17235. /* check stack first */
  17236. if (thr->valstack_top >= thr->valstack_end) {
  17237. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  17238. }
  17239. /* Template functions are not strictly constructable (they don't
  17240. * have a "prototype" property for instance), so leave the
  17241. * DUK_HOBJECT_FLAG_CONSRUCTABLE flag cleared here.
  17242. */
  17243. obj = duk_hcompiledfunction_alloc(thr->heap,
  17244. DUK_HOBJECT_FLAG_EXTENSIBLE |
  17245. DUK_HOBJECT_FLAG_COMPILEDFUNCTION |
  17246. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION));
  17247. if (!obj) {
  17248. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_ALLOC_FAILED);
  17249. }
  17250. DUK_DDD(DUK_DDDPRINT("created compiled function object with flags: 0x%08lx", (unsigned long) obj->obj.hdr.h_flags));
  17251. tv_slot = thr->valstack_top;
  17252. DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
  17253. DUK_HOBJECT_INCREF(thr, obj);
  17254. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  17255. thr->valstack_top++;
  17256. /* default prototype (Note: 'obj' must be reachable) */
  17257. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  17258. return ret;
  17259. }
  17260. 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) {
  17261. duk_hthread *thr = (duk_hthread *) ctx;
  17262. duk_hnativefunction *obj;
  17263. duk_idx_t ret;
  17264. duk_tval *tv_slot;
  17265. duk_uint16_t func_nargs;
  17266. DUK_ASSERT_CTX_VALID(ctx);
  17267. /* check stack first */
  17268. if (thr->valstack_top >= thr->valstack_end) {
  17269. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  17270. }
  17271. if (func == NULL) {
  17272. goto api_error;
  17273. }
  17274. if (nargs >= 0 && nargs < DUK_HNATIVEFUNCTION_NARGS_MAX) {
  17275. func_nargs = (duk_uint16_t) nargs;
  17276. } else if (nargs == DUK_VARARGS) {
  17277. func_nargs = DUK_HNATIVEFUNCTION_NARGS_VARARGS;
  17278. } else {
  17279. goto api_error;
  17280. }
  17281. obj = duk_hnativefunction_alloc(thr->heap, flags);
  17282. if (!obj) {
  17283. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_ALLOC_FAILED);
  17284. }
  17285. obj->func = func;
  17286. obj->nargs = func_nargs;
  17287. DUK_DDD(DUK_DDDPRINT("created native function object with flags: 0x%08lx, nargs=%ld",
  17288. (unsigned long) obj->obj.hdr.h_flags, (long) obj->nargs));
  17289. tv_slot = thr->valstack_top;
  17290. DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
  17291. DUK_HOBJECT_INCREF(thr, obj);
  17292. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  17293. thr->valstack_top++;
  17294. /* default prototype (Note: 'obj' must be reachable) */
  17295. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  17296. return ret;
  17297. api_error:
  17298. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  17299. return 0; /* not reached */
  17300. }
  17301. DUK_EXTERNAL duk_idx_t duk_push_c_function(duk_context *ctx, duk_c_function func, duk_int_t nargs) {
  17302. duk_uint_t flags;
  17303. DUK_ASSERT_CTX_VALID(ctx);
  17304. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  17305. DUK_HOBJECT_FLAG_CONSTRUCTABLE |
  17306. DUK_HOBJECT_FLAG_NATIVEFUNCTION |
  17307. DUK_HOBJECT_FLAG_NEWENV |
  17308. DUK_HOBJECT_FLAG_STRICT |
  17309. DUK_HOBJECT_FLAG_NOTAIL |
  17310. DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC |
  17311. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION);
  17312. return duk__push_c_function_raw(ctx, func, nargs, flags);
  17313. }
  17314. DUK_INTERNAL void duk_push_c_function_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs) {
  17315. duk_uint_t flags;
  17316. DUK_ASSERT_CTX_VALID(ctx);
  17317. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  17318. DUK_HOBJECT_FLAG_CONSTRUCTABLE |
  17319. DUK_HOBJECT_FLAG_NATIVEFUNCTION |
  17320. DUK_HOBJECT_FLAG_NEWENV |
  17321. DUK_HOBJECT_FLAG_STRICT |
  17322. DUK_HOBJECT_FLAG_NOTAIL |
  17323. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION);
  17324. (void) duk__push_c_function_raw(ctx, func, nargs, flags);
  17325. }
  17326. DUK_INTERNAL void duk_push_c_function_noconstruct_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs) {
  17327. duk_uint_t flags;
  17328. DUK_ASSERT_CTX_VALID(ctx);
  17329. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  17330. DUK_HOBJECT_FLAG_NATIVEFUNCTION |
  17331. DUK_HOBJECT_FLAG_NEWENV |
  17332. DUK_HOBJECT_FLAG_STRICT |
  17333. DUK_HOBJECT_FLAG_NOTAIL |
  17334. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION);
  17335. (void) duk__push_c_function_raw(ctx, func, nargs, flags);
  17336. }
  17337. 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) {
  17338. duk_hthread *thr = (duk_hthread *) ctx;
  17339. duk_tval tv_tmp;
  17340. duk_small_uint_t lf_flags;
  17341. DUK_ASSERT_CTX_VALID(ctx);
  17342. /* check stack first */
  17343. if (thr->valstack_top >= thr->valstack_end) {
  17344. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  17345. }
  17346. if (nargs >= DUK_LFUNC_NARGS_MIN && nargs <= DUK_LFUNC_NARGS_MAX) {
  17347. /* as is */
  17348. } else if (nargs == DUK_VARARGS) {
  17349. nargs = DUK_LFUNC_NARGS_VARARGS;
  17350. } else {
  17351. goto api_error;
  17352. }
  17353. if (!(length >= DUK_LFUNC_LENGTH_MIN && length <= DUK_LFUNC_LENGTH_MAX)) {
  17354. goto api_error;
  17355. }
  17356. if (!(magic >= DUK_LFUNC_MAGIC_MIN && magic <= DUK_LFUNC_MAGIC_MAX)) {
  17357. goto api_error;
  17358. }
  17359. lf_flags = DUK_LFUNC_FLAGS_PACK(magic, length, nargs);
  17360. DUK_TVAL_SET_LIGHTFUNC(&tv_tmp, func, lf_flags);
  17361. duk_push_tval(ctx, &tv_tmp); /* XXX: direct valstack write */
  17362. DUK_ASSERT(thr->valstack_top != thr->valstack_bottom);
  17363. return ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1;
  17364. api_error:
  17365. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  17366. return 0; /* not reached */
  17367. }
  17368. DUK_INTERNAL duk_hbufferobject *duk_push_bufferobject(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx) {
  17369. duk_hthread *thr = (duk_hthread *) ctx;
  17370. duk_hbufferobject *obj;
  17371. duk_tval *tv_slot;
  17372. DUK_ASSERT(ctx != NULL);
  17373. DUK_ASSERT(prototype_bidx >= 0);
  17374. /* check stack first */
  17375. if (thr->valstack_top >= thr->valstack_end) {
  17376. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  17377. }
  17378. obj = duk_hbufferobject_alloc(thr->heap, hobject_flags_and_class);
  17379. if (!obj) {
  17380. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_ALLOC_FAILED);
  17381. }
  17382. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, thr->builtins[prototype_bidx]);
  17383. DUK_ASSERT_HBUFFEROBJECT_VALID(obj);
  17384. tv_slot = thr->valstack_top;
  17385. DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
  17386. DUK_HOBJECT_INCREF(thr, obj);
  17387. thr->valstack_top++;
  17388. return obj;
  17389. }
  17390. 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) {
  17391. duk_hthread *thr = (duk_hthread *) ctx;
  17392. duk_idx_t ret;
  17393. duk_hobject *proto;
  17394. #ifdef DUK_USE_AUGMENT_ERROR_CREATE
  17395. duk_bool_t noblame_fileline;
  17396. #endif
  17397. DUK_ASSERT_CTX_VALID(ctx);
  17398. DUK_ASSERT(thr != NULL);
  17399. DUK_UNREF(filename);
  17400. DUK_UNREF(line);
  17401. /* Error code also packs a tracedata related flag. */
  17402. #ifdef DUK_USE_AUGMENT_ERROR_CREATE
  17403. noblame_fileline = err_code & DUK_ERRCODE_FLAG_NOBLAME_FILELINE;
  17404. #endif
  17405. err_code = err_code & (~DUK_ERRCODE_FLAG_NOBLAME_FILELINE);
  17406. /* error gets its 'name' from the prototype */
  17407. proto = duk_error_prototype_from_code(thr, err_code);
  17408. ret = duk_push_object_helper_proto(ctx,
  17409. DUK_HOBJECT_FLAG_EXTENSIBLE |
  17410. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ERROR),
  17411. proto);
  17412. /* ... and its 'message' from an instance property */
  17413. if (fmt) {
  17414. duk_push_vsprintf(ctx, fmt, ap);
  17415. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC);
  17416. } else {
  17417. /* If no explicit message given, put error code into message field
  17418. * (as a number). This is not fully in keeping with the Ecmascript
  17419. * error model because messages are supposed to be strings (Error
  17420. * constructors use ToString() on their argument). However, it's
  17421. * probably more useful than having a separate 'code' property.
  17422. */
  17423. duk_push_int(ctx, err_code);
  17424. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC);
  17425. }
  17426. #if 0
  17427. /* Disabled for now, not sure this is a useful property */
  17428. duk_push_int(ctx, err_code);
  17429. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_CODE, DUK_PROPDESC_FLAGS_WC);
  17430. #endif
  17431. /* Creation time error augmentation */
  17432. #ifdef DUK_USE_AUGMENT_ERROR_CREATE
  17433. /* filename may be NULL in which case file/line is not recorded */
  17434. duk_err_augment_error_create(thr, thr, filename, line, noblame_fileline); /* may throw an error */
  17435. #endif
  17436. return ret;
  17437. }
  17438. 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, ...) {
  17439. va_list ap;
  17440. duk_idx_t ret;
  17441. DUK_ASSERT_CTX_VALID(ctx);
  17442. va_start(ap, fmt);
  17443. ret = duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap);
  17444. va_end(ap);
  17445. return ret;
  17446. }
  17447. #if !defined(DUK_USE_VARIADIC_MACROS)
  17448. DUK_EXTERNAL duk_idx_t duk_push_error_object_stash(duk_context *ctx, duk_errcode_t err_code, const char *fmt, ...) {
  17449. const char *filename = duk_api_global_filename;
  17450. duk_int_t line = duk_api_global_line;
  17451. va_list ap;
  17452. duk_idx_t ret;
  17453. DUK_ASSERT_CTX_VALID(ctx);
  17454. duk_api_global_filename = NULL;
  17455. duk_api_global_line = 0;
  17456. va_start(ap, fmt);
  17457. ret = duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap);
  17458. va_end(ap);
  17459. return ret;
  17460. }
  17461. #endif /* DUK_USE_VARIADIC_MACROS */
  17462. DUK_EXTERNAL void *duk_push_buffer_raw(duk_context *ctx, duk_size_t size, duk_small_uint_t flags) {
  17463. duk_hthread *thr = (duk_hthread *) ctx;
  17464. duk_tval *tv_slot;
  17465. duk_hbuffer *h;
  17466. DUK_ASSERT_CTX_VALID(ctx);
  17467. /* check stack first */
  17468. if (thr->valstack_top >= thr->valstack_end) {
  17469. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  17470. }
  17471. /* Check for maximum buffer length. */
  17472. if (size > DUK_HBUFFER_MAX_BYTELEN) {
  17473. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_BUFFER_TOO_LONG);
  17474. }
  17475. h = duk_hbuffer_alloc(thr->heap, size, flags);
  17476. if (!h) {
  17477. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_ALLOC_FAILED);
  17478. }
  17479. tv_slot = thr->valstack_top;
  17480. DUK_TVAL_SET_BUFFER(tv_slot, h);
  17481. DUK_HBUFFER_INCREF(thr, h);
  17482. thr->valstack_top++;
  17483. return DUK_HBUFFER_GET_DATA_PTR(thr->heap, h);
  17484. }
  17485. DUK_EXTERNAL duk_idx_t duk_push_heapptr(duk_context *ctx, void *ptr) {
  17486. duk_hthread *thr = (duk_hthread *) ctx;
  17487. duk_idx_t ret;
  17488. DUK_ASSERT_CTX_VALID(ctx);
  17489. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  17490. if (ptr == NULL) {
  17491. goto push_undefined;
  17492. }
  17493. switch (DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) ptr)) {
  17494. case DUK_HTYPE_STRING:
  17495. duk_push_hstring(ctx, (duk_hstring *) ptr);
  17496. break;
  17497. case DUK_HTYPE_OBJECT:
  17498. duk_push_hobject(ctx, (duk_hobject *) ptr);
  17499. break;
  17500. case DUK_HTYPE_BUFFER:
  17501. duk_push_hbuffer(ctx, (duk_hbuffer *) ptr);
  17502. break;
  17503. default:
  17504. goto push_undefined;
  17505. }
  17506. return ret;
  17507. push_undefined:
  17508. duk_push_undefined(ctx);
  17509. return ret;
  17510. }
  17511. DUK_INTERNAL duk_idx_t duk_push_object_internal(duk_context *ctx) {
  17512. return duk_push_object_helper(ctx,
  17513. DUK_HOBJECT_FLAG_EXTENSIBLE |
  17514. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  17515. -1); /* no prototype */
  17516. }
  17517. DUK_INTERNAL void duk_push_hstring(duk_context *ctx, duk_hstring *h) {
  17518. duk_tval tv;
  17519. DUK_ASSERT_CTX_VALID(ctx);
  17520. DUK_ASSERT(h != NULL);
  17521. DUK_TVAL_SET_STRING(&tv, h);
  17522. duk_push_tval(ctx, &tv);
  17523. }
  17524. DUK_INTERNAL void duk_push_hstring_stridx(duk_context *ctx, duk_small_int_t stridx) {
  17525. duk_hthread *thr = (duk_hthread *) ctx;
  17526. DUK_ASSERT(stridx >= 0 && stridx < DUK_HEAP_NUM_STRINGS);
  17527. duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx));
  17528. }
  17529. DUK_INTERNAL void duk_push_hobject(duk_context *ctx, duk_hobject *h) {
  17530. duk_tval tv;
  17531. DUK_ASSERT_CTX_VALID(ctx);
  17532. DUK_ASSERT(h != NULL);
  17533. DUK_TVAL_SET_OBJECT(&tv, h);
  17534. duk_push_tval(ctx, &tv);
  17535. }
  17536. DUK_INTERNAL void duk_push_hbuffer(duk_context *ctx, duk_hbuffer *h) {
  17537. duk_tval tv;
  17538. DUK_ASSERT_CTX_VALID(ctx);
  17539. DUK_ASSERT(h != NULL);
  17540. DUK_TVAL_SET_BUFFER(&tv, h);
  17541. duk_push_tval(ctx, &tv);
  17542. }
  17543. DUK_INTERNAL void duk_push_hobject_bidx(duk_context *ctx, duk_small_int_t builtin_idx) {
  17544. duk_hthread *thr = (duk_hthread *) ctx;
  17545. DUK_ASSERT_CTX_VALID(ctx);
  17546. DUK_ASSERT(thr != NULL);
  17547. DUK_ASSERT(builtin_idx >= 0 && builtin_idx < DUK_NUM_BUILTINS);
  17548. DUK_ASSERT(thr->builtins[builtin_idx] != NULL);
  17549. duk_push_hobject(ctx, thr->builtins[builtin_idx]);
  17550. }
  17551. /*
  17552. * Poppers
  17553. */
  17554. DUK_EXTERNAL void duk_pop_n(duk_context *ctx, duk_idx_t count) {
  17555. duk_hthread *thr = (duk_hthread *) ctx;
  17556. DUK_ASSERT_CTX_VALID(ctx);
  17557. if (count < 0) {
  17558. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_COUNT);
  17559. return;
  17560. }
  17561. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  17562. if ((duk_size_t) (thr->valstack_top - thr->valstack_bottom) < (duk_size_t) count) {
  17563. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_POP_TOO_MANY);
  17564. }
  17565. /*
  17566. * Must be very careful here, every DECREF may cause reallocation
  17567. * of our valstack.
  17568. */
  17569. /* XXX: inlined DECREF macro would be nice here: no NULL check,
  17570. * refzero queueing but no refzero algorithm run (= no pointer
  17571. * instability), inline code.
  17572. */
  17573. #ifdef DUK_USE_REFERENCE_COUNTING
  17574. while (count > 0) {
  17575. duk_tval tv_tmp;
  17576. duk_tval *tv;
  17577. tv = --thr->valstack_top; /* tv points to element just below prev top */
  17578. DUK_ASSERT(tv >= thr->valstack_bottom);
  17579. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  17580. DUK_TVAL_SET_UNDEFINED_UNUSED(tv);
  17581. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  17582. count--;
  17583. }
  17584. #else
  17585. while (count > 0) {
  17586. duk_tval *tv;
  17587. tv = --thr->valstack_top;
  17588. DUK_ASSERT(tv >= thr->valstack_bottom);
  17589. DUK_TVAL_SET_UNDEFINED_UNUSED(tv);
  17590. count--;
  17591. }
  17592. #endif
  17593. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  17594. }
  17595. DUK_EXTERNAL void duk_pop(duk_context *ctx) {
  17596. DUK_ASSERT_CTX_VALID(ctx);
  17597. duk_pop_n(ctx, 1);
  17598. }
  17599. DUK_EXTERNAL void duk_pop_2(duk_context *ctx) {
  17600. DUK_ASSERT_CTX_VALID(ctx);
  17601. duk_pop_n(ctx, 2);
  17602. }
  17603. DUK_EXTERNAL void duk_pop_3(duk_context *ctx) {
  17604. DUK_ASSERT_CTX_VALID(ctx);
  17605. duk_pop_n(ctx, 3);
  17606. }
  17607. /*
  17608. * Error throwing
  17609. */
  17610. DUK_EXTERNAL void duk_throw(duk_context *ctx) {
  17611. duk_hthread *thr = (duk_hthread *) ctx;
  17612. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  17613. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  17614. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  17615. if (thr->valstack_top == thr->valstack_bottom) {
  17616. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  17617. }
  17618. /* Errors are augmented when they are created, not when they are
  17619. * thrown or re-thrown. The current error handler, however, runs
  17620. * just before an error is thrown.
  17621. */
  17622. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  17623. DUK_DDD(DUK_DDDPRINT("THROW ERROR (API): %!dT (before throw augment)", (duk_tval *) duk_get_tval(ctx, -1)));
  17624. duk_err_augment_error_throw(thr);
  17625. #endif
  17626. DUK_DDD(DUK_DDDPRINT("THROW ERROR (API): %!dT (after throw augment)", (duk_tval *) duk_get_tval(ctx, -1)));
  17627. duk_err_setup_heap_ljstate(thr, DUK_LJ_TYPE_THROW);
  17628. /* thr->heap->lj.jmpbuf_ptr is checked by duk_err_longjmp() so we don't
  17629. * need to check that here. If the value is NULL, a panic occurs because
  17630. * we can't return.
  17631. */
  17632. duk_err_longjmp(thr);
  17633. DUK_UNREACHABLE();
  17634. }
  17635. DUK_EXTERNAL void duk_fatal(duk_context *ctx, duk_errcode_t err_code, const char *err_msg) {
  17636. duk_hthread *thr = (duk_hthread *) ctx;
  17637. DUK_ASSERT_CTX_VALID(ctx);
  17638. DUK_ASSERT(thr != NULL);
  17639. DUK_ASSERT(thr->heap != NULL);
  17640. DUK_ASSERT(thr->heap->fatal_func != NULL);
  17641. DUK_D(DUK_DPRINT("fatal error occurred, code %ld, message %s",
  17642. (long) err_code, (const char *) err_msg));
  17643. /* fatal_func should be noreturn, but noreturn declarations on function
  17644. * pointers has a very spotty support apparently so it's not currently
  17645. * done.
  17646. */
  17647. thr->heap->fatal_func(ctx, err_code, err_msg);
  17648. DUK_PANIC(DUK_ERR_API_ERROR, "fatal handler returned");
  17649. }
  17650. 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) {
  17651. DUK_ASSERT_CTX_VALID(ctx);
  17652. duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap);
  17653. duk_throw(ctx);
  17654. }
  17655. DUK_EXTERNAL void duk_error_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, ...) {
  17656. va_list ap;
  17657. DUK_ASSERT_CTX_VALID(ctx);
  17658. va_start(ap, fmt);
  17659. duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap);
  17660. va_end(ap);
  17661. duk_throw(ctx);
  17662. }
  17663. #if !defined(DUK_USE_VARIADIC_MACROS)
  17664. DUK_EXTERNAL void duk_error_stash(duk_context *ctx, duk_errcode_t err_code, const char *fmt, ...) {
  17665. const char *filename;
  17666. duk_int_t line;
  17667. va_list ap;
  17668. DUK_ASSERT_CTX_VALID(ctx);
  17669. filename = duk_api_global_filename;
  17670. line = duk_api_global_line;
  17671. duk_api_global_filename = NULL;
  17672. duk_api_global_line = 0;
  17673. va_start(ap, fmt);
  17674. duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap);
  17675. va_end(ap);
  17676. duk_throw(ctx);
  17677. }
  17678. #endif /* DUK_USE_VARIADIC_MACROS */
  17679. /*
  17680. * Comparison
  17681. */
  17682. DUK_EXTERNAL duk_bool_t duk_equals(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) {
  17683. duk_hthread *thr = (duk_hthread *) ctx;
  17684. duk_tval *tv1, *tv2;
  17685. DUK_ASSERT_CTX_VALID(ctx);
  17686. tv1 = duk_get_tval(ctx, index1);
  17687. tv2 = duk_get_tval(ctx, index2);
  17688. if ((tv1 == NULL) || (tv2 == NULL)) {
  17689. return 0;
  17690. }
  17691. /* Coercion may be needed, the helper handles that by pushing the
  17692. * tagged values to the stack.
  17693. */
  17694. return duk_js_equals(thr, tv1, tv2);
  17695. }
  17696. DUK_EXTERNAL duk_bool_t duk_strict_equals(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) {
  17697. duk_tval *tv1, *tv2;
  17698. DUK_ASSERT_CTX_VALID(ctx);
  17699. tv1 = duk_get_tval(ctx, index1);
  17700. tv2 = duk_get_tval(ctx, index2);
  17701. if ((tv1 == NULL) || (tv2 == NULL)) {
  17702. return 0;
  17703. }
  17704. /* No coercions or other side effects, so safe */
  17705. return duk_js_strict_equals(tv1, tv2);
  17706. }
  17707. /*
  17708. * instanceof
  17709. */
  17710. DUK_EXTERNAL duk_bool_t duk_instanceof(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) {
  17711. duk_tval *tv1, *tv2;
  17712. DUK_ASSERT_CTX_VALID(ctx);
  17713. /* Index validation is strict, which differs from duk_equals().
  17714. * The strict behavior mimics how instanceof itself works, e.g.
  17715. * it is a TypeError if rval is not a -callable- object. It would
  17716. * be somewhat inconsistent if rval would be allowed to be
  17717. * non-existent without a TypeError.
  17718. */
  17719. tv1 = duk_require_tval(ctx, index1);
  17720. DUK_ASSERT(tv1 != NULL);
  17721. tv2 = duk_require_tval(ctx, index2);
  17722. DUK_ASSERT(tv2 != NULL);
  17723. return duk_js_instanceof((duk_hthread *) ctx, tv1, tv2);
  17724. }
  17725. /*
  17726. * Lightfunc
  17727. */
  17728. DUK_INTERNAL void duk_push_lightfunc_name(duk_context *ctx, duk_tval *tv) {
  17729. duk_c_function func;
  17730. DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv));
  17731. /* Lightfunc name, includes Duktape/C native function pointer, which
  17732. * can often be used to locate the function from a symbol table.
  17733. * The name also includes the 16-bit duk_tval flags field because it
  17734. * includes the magic value. Because a single native function often
  17735. * provides different functionality depending on the magic value, it
  17736. * seems reasonably to include it in the name.
  17737. *
  17738. * On the other hand, a complicated name increases string table
  17739. * pressure in low memory environments (but only when function name
  17740. * is accessed).
  17741. */
  17742. func = DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv);
  17743. duk_push_sprintf(ctx, "light_");
  17744. duk_push_string_funcptr(ctx, (duk_uint8_t *) &func, sizeof(func));
  17745. duk_push_sprintf(ctx, "_%04x", (unsigned int) DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv));
  17746. duk_concat(ctx, 3);
  17747. }
  17748. DUK_INTERNAL void duk_push_lightfunc_tostring(duk_context *ctx, duk_tval *tv) {
  17749. DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv));
  17750. duk_push_string(ctx, "function ");
  17751. duk_push_lightfunc_name(ctx, tv);
  17752. duk_push_string(ctx, "() {/* light */}");
  17753. duk_concat(ctx, 3);
  17754. }
  17755. /*
  17756. * Function pointers
  17757. *
  17758. * Printing function pointers is non-portable, so we do that by hex printing
  17759. * bytes from memory.
  17760. */
  17761. DUK_INTERNAL void duk_push_string_funcptr(duk_context *ctx, duk_uint8_t *ptr, duk_size_t sz) {
  17762. duk_uint8_t buf[32 * 2];
  17763. duk_uint8_t *p, *q;
  17764. duk_small_uint_t i;
  17765. duk_small_uint_t t;
  17766. DUK_ASSERT(sz <= 32); /* sanity limit for function pointer size */
  17767. p = buf;
  17768. #if defined(DUK_USE_INTEGER_LE)
  17769. q = ptr + sz;
  17770. #else
  17771. q = ptr;
  17772. #endif
  17773. for (i = 0; i < sz; i++) {
  17774. #if defined(DUK_USE_INTEGER_LE)
  17775. t = *(--q);
  17776. #else
  17777. t = *(q++);
  17778. #endif
  17779. *p++ = duk_lc_digits[t >> 4];
  17780. *p++ = duk_lc_digits[t & 0x0f];
  17781. }
  17782. duk_push_lstring(ctx, (const char *) buf, sz * 2);
  17783. }
  17784. #undef DUK__CHECK_SPACE
  17785. #line 1 "duk_api_string.c"
  17786. /*
  17787. * String manipulation
  17788. */
  17789. /* include removed: duk_internal.h */
  17790. DUK_LOCAL void duk__concat_and_join_helper(duk_context *ctx, duk_idx_t count_in, duk_bool_t is_join) {
  17791. duk_hthread *thr = (duk_hthread *) ctx;
  17792. duk_uint_t count;
  17793. duk_uint_t i;
  17794. duk_size_t idx;
  17795. duk_size_t len;
  17796. duk_hstring *h;
  17797. duk_uint8_t *buf;
  17798. DUK_ASSERT_CTX_VALID(ctx);
  17799. if (DUK_UNLIKELY(count_in <= 0)) {
  17800. if (count_in < 0) {
  17801. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_COUNT);
  17802. return;
  17803. }
  17804. DUK_ASSERT(count_in == 0);
  17805. duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING);
  17806. return;
  17807. }
  17808. count = (duk_uint_t) count_in;
  17809. if (is_join) {
  17810. duk_size_t t1, t2, limit;
  17811. h = duk_to_hstring(ctx, -((duk_idx_t) count) - 1);
  17812. DUK_ASSERT(h != NULL);
  17813. /* A bit tricky overflow test, see doc/code-issues.txt. */
  17814. t1 = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h);
  17815. t2 = (duk_size_t) (count - 1);
  17816. limit = (duk_size_t) DUK_HSTRING_MAX_BYTELEN;
  17817. if (DUK_UNLIKELY(t2 != 0 && t1 > limit / t2)) {
  17818. /* Combined size of separators already overflows */
  17819. goto error_overflow;
  17820. }
  17821. len = (duk_size_t) (t1 * t2);
  17822. } else {
  17823. len = (duk_size_t) 0;
  17824. }
  17825. for (i = count; i >= 1; i--) {
  17826. duk_size_t new_len;
  17827. duk_to_string(ctx, -((duk_idx_t) i));
  17828. h = duk_require_hstring(ctx, -((duk_idx_t) i));
  17829. new_len = len + (duk_size_t) DUK_HSTRING_GET_BYTELEN(h);
  17830. /* Impose a string maximum length, need to handle overflow
  17831. * correctly.
  17832. */
  17833. if (new_len < len || /* wrapped */
  17834. new_len > (duk_size_t) DUK_HSTRING_MAX_BYTELEN) {
  17835. goto error_overflow;
  17836. }
  17837. len = new_len;
  17838. }
  17839. DUK_DDD(DUK_DDDPRINT("join/concat %lu strings, total length %lu bytes",
  17840. (unsigned long) count, (unsigned long) len));
  17841. /* use stack allocated buffer to ensure reachability in errors (e.g. intern error) */
  17842. buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, len);
  17843. DUK_ASSERT(buf != NULL);
  17844. /* [... (sep) str1 str2 ... strN buf] */
  17845. idx = 0;
  17846. for (i = count; i >= 1; i--) {
  17847. if (is_join && i != count) {
  17848. h = duk_require_hstring(ctx, -((duk_idx_t) count) - 2); /* extra -1 for buffer */
  17849. DUK_MEMCPY(buf + idx, DUK_HSTRING_GET_DATA(h), DUK_HSTRING_GET_BYTELEN(h));
  17850. idx += DUK_HSTRING_GET_BYTELEN(h);
  17851. }
  17852. h = duk_require_hstring(ctx, -((duk_idx_t) i) - 1); /* extra -1 for buffer */
  17853. DUK_MEMCPY(buf + idx, DUK_HSTRING_GET_DATA(h), DUK_HSTRING_GET_BYTELEN(h));
  17854. idx += DUK_HSTRING_GET_BYTELEN(h);
  17855. }
  17856. DUK_ASSERT(idx == len);
  17857. /* [... (sep) str1 str2 ... strN buf] */
  17858. /* get rid of the strings early to minimize memory use before intern */
  17859. if (is_join) {
  17860. duk_replace(ctx, -((duk_idx_t) count) - 2); /* overwrite sep */
  17861. duk_pop_n(ctx, count);
  17862. } else {
  17863. duk_replace(ctx, -((duk_idx_t) count) - 1); /* overwrite str1 */
  17864. duk_pop_n(ctx, count-1);
  17865. }
  17866. /* [... buf] */
  17867. (void) duk_to_string(ctx, -1);
  17868. /* [... res] */
  17869. return;
  17870. error_overflow:
  17871. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_CONCAT_RESULT_TOO_LONG);
  17872. }
  17873. DUK_EXTERNAL void duk_concat(duk_context *ctx, duk_idx_t count) {
  17874. DUK_ASSERT_CTX_VALID(ctx);
  17875. duk__concat_and_join_helper(ctx, count, 0 /*is_join*/);
  17876. }
  17877. DUK_EXTERNAL void duk_join(duk_context *ctx, duk_idx_t count) {
  17878. DUK_ASSERT_CTX_VALID(ctx);
  17879. duk__concat_and_join_helper(ctx, count, 1 /*is_join*/);
  17880. }
  17881. /* XXX: could map/decode be unified with duk_unicode_support.c code?
  17882. * Case conversion needs also the character surroundings though.
  17883. */
  17884. DUK_EXTERNAL void duk_decode_string(duk_context *ctx, duk_idx_t index, duk_decode_char_function callback, void *udata) {
  17885. duk_hthread *thr = (duk_hthread *) ctx;
  17886. duk_hstring *h_input;
  17887. const duk_uint8_t *p, *p_start, *p_end;
  17888. duk_codepoint_t cp;
  17889. DUK_ASSERT_CTX_VALID(ctx);
  17890. h_input = duk_require_hstring(ctx, index);
  17891. DUK_ASSERT(h_input != NULL);
  17892. p_start = (duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
  17893. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  17894. p = p_start;
  17895. for (;;) {
  17896. if (p >= p_end) {
  17897. break;
  17898. }
  17899. cp = (int) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end);
  17900. callback(udata, cp);
  17901. }
  17902. }
  17903. DUK_EXTERNAL void duk_map_string(duk_context *ctx, duk_idx_t index, duk_map_char_function callback, void *udata) {
  17904. duk_hthread *thr = (duk_hthread *) ctx;
  17905. duk_hstring *h_input;
  17906. duk_hbuffer_dynamic *h_buf;
  17907. const duk_uint8_t *p, *p_start, *p_end;
  17908. duk_codepoint_t cp;
  17909. DUK_ASSERT_CTX_VALID(ctx);
  17910. index = duk_normalize_index(ctx, index);
  17911. h_input = duk_require_hstring(ctx, index);
  17912. DUK_ASSERT(h_input != NULL);
  17913. /* XXX: should init with a spare of at least h_input->blen? */
  17914. duk_push_dynamic_buffer(ctx, 0);
  17915. h_buf = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, -1);
  17916. DUK_ASSERT(h_buf != NULL);
  17917. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(h_buf));
  17918. p_start = (duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
  17919. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  17920. p = p_start;
  17921. for (;;) {
  17922. if (p >= p_end) {
  17923. break;
  17924. }
  17925. cp = (int) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end);
  17926. cp = callback(udata, cp);
  17927. duk_hbuffer_append_xutf8(thr, h_buf, cp);
  17928. }
  17929. duk_to_string(ctx, -1); /* invalidates h_buf pointer */
  17930. duk_replace(ctx, index);
  17931. }
  17932. DUK_EXTERNAL void duk_substring(duk_context *ctx, duk_idx_t index, duk_size_t start_offset, duk_size_t end_offset) {
  17933. duk_hthread *thr = (duk_hthread *) ctx;
  17934. duk_hstring *h;
  17935. duk_hstring *res;
  17936. duk_size_t start_byte_offset;
  17937. duk_size_t end_byte_offset;
  17938. DUK_ASSERT_CTX_VALID(ctx);
  17939. index = duk_require_normalize_index(ctx, index);
  17940. h = duk_require_hstring(ctx, index);
  17941. DUK_ASSERT(h != NULL);
  17942. if (end_offset >= DUK_HSTRING_GET_CHARLEN(h)) {
  17943. end_offset = DUK_HSTRING_GET_CHARLEN(h);
  17944. }
  17945. if (start_offset > end_offset) {
  17946. start_offset = end_offset;
  17947. }
  17948. DUK_ASSERT_DISABLE(start_offset >= 0);
  17949. DUK_ASSERT(start_offset <= end_offset && start_offset <= DUK_HSTRING_GET_CHARLEN(h));
  17950. DUK_ASSERT_DISABLE(end_offset >= 0);
  17951. DUK_ASSERT(end_offset >= start_offset && end_offset <= DUK_HSTRING_GET_CHARLEN(h));
  17952. /* guaranteed by string limits */
  17953. DUK_ASSERT(start_offset <= DUK_UINT32_MAX);
  17954. DUK_ASSERT(end_offset <= DUK_UINT32_MAX);
  17955. start_byte_offset = (duk_size_t) duk_heap_strcache_offset_char2byte(thr, h, (duk_uint_fast32_t) start_offset);
  17956. end_byte_offset = (duk_size_t) duk_heap_strcache_offset_char2byte(thr, h, (duk_uint_fast32_t) end_offset);
  17957. DUK_ASSERT(end_byte_offset >= start_byte_offset);
  17958. DUK_ASSERT(end_byte_offset - start_byte_offset <= DUK_UINT32_MAX); /* guaranteed by string limits */
  17959. /* no size check is necessary */
  17960. res = duk_heap_string_intern_checked(thr,
  17961. DUK_HSTRING_GET_DATA(h) + start_byte_offset,
  17962. (duk_uint32_t) (end_byte_offset - start_byte_offset));
  17963. duk_push_hstring(ctx, res);
  17964. duk_replace(ctx, index);
  17965. }
  17966. /* XXX: this is quite clunky. Add Unicode helpers to scan backwards and
  17967. * forwards with a callback to process codepoints?
  17968. */
  17969. DUK_EXTERNAL void duk_trim(duk_context *ctx, duk_idx_t index) {
  17970. duk_hthread *thr = (duk_hthread *) ctx;
  17971. duk_hstring *h;
  17972. const duk_uint8_t *p, *p_start, *p_end, *p_tmp1, *p_tmp2; /* pointers for scanning */
  17973. const duk_uint8_t *q_start, *q_end; /* start (incl) and end (excl) of trimmed part */
  17974. duk_codepoint_t cp;
  17975. DUK_ASSERT_CTX_VALID(ctx);
  17976. index = duk_require_normalize_index(ctx, index);
  17977. h = duk_require_hstring(ctx, index);
  17978. DUK_ASSERT(h != NULL);
  17979. p_start = DUK_HSTRING_GET_DATA(h);
  17980. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h);
  17981. p = p_start;
  17982. while (p < p_end) {
  17983. p_tmp1 = p;
  17984. cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &p_tmp1, p_start, p_end);
  17985. if (!(duk_unicode_is_whitespace(cp) || duk_unicode_is_line_terminator(cp))) {
  17986. break;
  17987. }
  17988. p = p_tmp1;
  17989. }
  17990. q_start = p;
  17991. if (p == p_end) {
  17992. /* entire string is whitespace */
  17993. q_end = p;
  17994. goto scan_done;
  17995. }
  17996. p = p_end;
  17997. while (p > p_start) {
  17998. p_tmp1 = p;
  17999. while (p > p_start) {
  18000. p--;
  18001. if (((*p) & 0xc0) != 0x80) {
  18002. break;
  18003. }
  18004. }
  18005. p_tmp2 = p;
  18006. cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &p_tmp2, p_start, p_end);
  18007. if (!(duk_unicode_is_whitespace(cp) || duk_unicode_is_line_terminator(cp))) {
  18008. p = p_tmp1;
  18009. break;
  18010. }
  18011. }
  18012. q_end = p;
  18013. scan_done:
  18014. /* This may happen when forward and backward scanning disagree
  18015. * (possible for non-extended-UTF-8 strings).
  18016. */
  18017. if (q_end < q_start) {
  18018. q_end = q_start;
  18019. }
  18020. DUK_ASSERT(q_start >= p_start && q_start <= p_end);
  18021. DUK_ASSERT(q_end >= p_start && q_end <= p_end);
  18022. DUK_ASSERT(q_end >= q_start);
  18023. DUK_DDD(DUK_DDDPRINT("trim: p_start=%p, p_end=%p, q_start=%p, q_end=%p",
  18024. (void *) p_start, (void *) p_end, (void *) q_start, (void *) q_end));
  18025. if (q_start == p_start && q_end == p_end) {
  18026. DUK_DDD(DUK_DDDPRINT("nothing was trimmed: avoid interning (hashing etc)"));
  18027. return;
  18028. }
  18029. duk_push_lstring(ctx, (const char *) q_start, (duk_size_t) (q_end - q_start));
  18030. duk_replace(ctx, index);
  18031. }
  18032. DUK_EXTERNAL duk_codepoint_t duk_char_code_at(duk_context *ctx, duk_idx_t index, duk_size_t char_offset) {
  18033. duk_hthread *thr = (duk_hthread *) ctx;
  18034. duk_hstring *h;
  18035. duk_ucodepoint_t cp;
  18036. DUK_ASSERT_CTX_VALID(ctx);
  18037. h = duk_require_hstring(ctx, index);
  18038. DUK_ASSERT(h != NULL);
  18039. DUK_ASSERT_DISABLE(char_offset >= 0); /* always true, arg is unsigned */
  18040. if (char_offset >= DUK_HSTRING_GET_CHARLEN(h)) {
  18041. return 0;
  18042. }
  18043. DUK_ASSERT(char_offset <= DUK_UINT_MAX); /* guaranteed by string limits */
  18044. cp = duk_hstring_char_code_at_raw(thr, h, (duk_uint_t) char_offset);
  18045. return (duk_codepoint_t) cp;
  18046. }
  18047. #line 1 "duk_api_var.c"
  18048. /*
  18049. * Variable access
  18050. */
  18051. /* include removed: duk_internal.h */
  18052. DUK_EXTERNAL void duk_get_var(duk_context *ctx) {
  18053. duk_hthread *thr = (duk_hthread *) ctx;
  18054. duk_activation *act;
  18055. duk_hstring *h_varname;
  18056. duk_small_int_t throw_flag = 1; /* always throw ReferenceError for unresolvable */
  18057. DUK_ASSERT_CTX_VALID(ctx);
  18058. h_varname = duk_require_hstring(ctx, -1); /* XXX: tostring? */
  18059. DUK_ASSERT(h_varname != NULL);
  18060. act = duk_hthread_get_current_activation(thr);
  18061. if (act) {
  18062. (void) duk_js_getvar_activation(thr, act, h_varname, throw_flag); /* -> [ ... varname val this ] */
  18063. } else {
  18064. /* Outside any activation -> look up from global. */
  18065. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL_ENV] != NULL);
  18066. (void) duk_js_getvar_envrec(thr, thr->builtins[DUK_BIDX_GLOBAL_ENV], h_varname, throw_flag);
  18067. }
  18068. /* [ ... varname val this ] (because throw_flag == 1, always resolved) */
  18069. duk_pop(ctx);
  18070. duk_remove(ctx, -2);
  18071. /* [ ... val ] */
  18072. /* Return value would be pointless: because throw_flag==1, we always
  18073. * throw if the identifier doesn't resolve.
  18074. */
  18075. return;
  18076. }
  18077. DUK_EXTERNAL void duk_put_var(duk_context *ctx) {
  18078. duk_hthread *thr = (duk_hthread *) ctx;
  18079. duk_activation *act;
  18080. duk_hstring *h_varname;
  18081. duk_tval *tv_val;
  18082. duk_small_int_t throw_flag;
  18083. DUK_ASSERT_CTX_VALID(ctx);
  18084. h_varname = duk_require_hstring(ctx, -2); /* XXX: tostring? */
  18085. DUK_ASSERT(h_varname != NULL);
  18086. tv_val = duk_require_tval(ctx, -1);
  18087. throw_flag = duk_is_strict_call(ctx);
  18088. act = duk_hthread_get_current_activation(thr);
  18089. if (act) {
  18090. duk_js_putvar_activation(thr, act, h_varname, tv_val, throw_flag); /* -> [ ... varname val this ] */
  18091. } else {
  18092. /* Outside any activation -> put to global. */
  18093. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL_ENV] != NULL);
  18094. duk_js_putvar_envrec(thr, thr->builtins[DUK_BIDX_GLOBAL_ENV], h_varname, tv_val, throw_flag);
  18095. }
  18096. /* [ ... varname val ] */
  18097. duk_pop_2(ctx);
  18098. /* [ ... ] */
  18099. return;
  18100. }
  18101. DUK_EXTERNAL duk_bool_t duk_del_var(duk_context *ctx) {
  18102. DUK_ASSERT_CTX_VALID(ctx);
  18103. DUK_ERROR((duk_hthread *) ctx, DUK_ERR_UNIMPLEMENTED_ERROR, DUK_STR_UNIMPLEMENTED);
  18104. return 0;
  18105. }
  18106. DUK_EXTERNAL duk_bool_t duk_has_var(duk_context *ctx) {
  18107. DUK_ASSERT_CTX_VALID(ctx);
  18108. DUK_ERROR((duk_hthread *) ctx, DUK_ERR_UNIMPLEMENTED_ERROR, DUK_STR_UNIMPLEMENTED);
  18109. return 0;
  18110. }
  18111. #line 1 "duk_bi_array.c"
  18112. /*
  18113. * Array built-ins
  18114. *
  18115. * Note that most Array built-ins are intentionally generic and work even
  18116. * when the 'this' binding is not an Array instance. To ensure this,
  18117. * Array algorithms do not assume "magical" Array behavior for the "length"
  18118. * property, for instance.
  18119. *
  18120. * XXX: the "Throw" flag should be set for (almost?) all [[Put]] and
  18121. * [[Delete]] operations, but it's currently false throughout. Go through
  18122. * all put/delete cases and check throw flag use. Need a new API primitive
  18123. * which allows throws flag to be specified.
  18124. *
  18125. * XXX: array lengths above 2G won't work reliably. There are many places
  18126. * where one needs a full signed 32-bit range ([-0xffffffff, 0xffffffff],
  18127. * i.e. -33- bits). Although array 'length' cannot be written to be outside
  18128. * the unsigned 32-bit range (E5.1 Section 15.4.5.1 throws a RangeError if so)
  18129. * some intermediate values may be above 0xffffffff and this may not be always
  18130. * correctly handled now (duk_uint32_t is not enough for all algorithms).
  18131. *
  18132. * For instance, push() can legitimately write entries beyond length 0xffffffff
  18133. * and cause a RangeError only at the end. To do this properly, the current
  18134. * push() implementation tracks the array index using a 'double' instead of a
  18135. * duk_uint32_t (which is somewhat awkward). See test-bi-array-push-maxlen.js.
  18136. *
  18137. * On using "put" vs. "def" prop
  18138. * =============================
  18139. *
  18140. * Code below must be careful to use the appropriate primitive as it matters
  18141. * for compliance. When using "put" there may be inherited properties in
  18142. * Array.prototype which cause side effects when values are written. When
  18143. * using "define" there are no such side effects, and many test262 test cases
  18144. * check for this (for real world code, such side effects are very rare).
  18145. * Both "put" and "define" are used in the E5.1 specification; as a rule,
  18146. * "put" is used when modifying an existing array (or a non-array 'this'
  18147. * binding) and "define" for setting values into a fresh result array.
  18148. *
  18149. * Also note that Array instance 'length' should be writable, but not
  18150. * enumerable and definitely not configurable: even Duktape code internally
  18151. * assumes that an Array instance will always have a 'length' property.
  18152. * Preventing deletion of the property is critical.
  18153. */
  18154. /* include removed: duk_internal.h */
  18155. /* Perform an intermediate join when this many elements have been pushed
  18156. * on the value stack.
  18157. */
  18158. #define DUK__ARRAY_MID_JOIN_LIMIT 4096
  18159. /* Shared entry code for many Array built-ins. Note that length is left
  18160. * on stack (it could be popped, but that's not necessary).
  18161. */
  18162. DUK_LOCAL duk_uint32_t duk__push_this_obj_len_u32(duk_context *ctx) {
  18163. duk_uint32_t len;
  18164. (void) duk_push_this_coercible_to_object(ctx);
  18165. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LENGTH);
  18166. len = duk_to_uint32(ctx, -1);
  18167. /* -> [ ... ToObject(this) ToUint32(length) ] */
  18168. return len;
  18169. }
  18170. DUK_LOCAL duk_uint32_t duk__push_this_obj_len_u32_limited(duk_context *ctx) {
  18171. /* Range limited to [0, 0x7fffffff] range, i.e. range that can be
  18172. * represented with duk_int32_t. Use this when the method doesn't
  18173. * handle the full 32-bit unsigned range correctly.
  18174. */
  18175. duk_uint32_t ret = duk__push_this_obj_len_u32(ctx);
  18176. if (DUK_UNLIKELY(ret >= 0x80000000UL)) {
  18177. DUK_ERROR((duk_hthread *) ctx, DUK_ERR_INTERNAL_ERROR, DUK_STR_ARRAY_LENGTH_OVER_2G);
  18178. }
  18179. return ret;
  18180. }
  18181. /*
  18182. * Constructor
  18183. */
  18184. DUK_INTERNAL duk_ret_t duk_bi_array_constructor(duk_context *ctx) {
  18185. duk_idx_t nargs;
  18186. duk_double_t d;
  18187. duk_uint32_t len;
  18188. duk_idx_t i;
  18189. nargs = duk_get_top(ctx);
  18190. duk_push_array(ctx);
  18191. if (nargs == 1 && duk_is_number(ctx, 0)) {
  18192. /* XXX: expensive check (also shared elsewhere - so add a shared internal API call?) */
  18193. d = duk_get_number(ctx, 0);
  18194. len = duk_to_uint32(ctx, 0);
  18195. if (((duk_double_t) len) != d) {
  18196. return DUK_RET_RANGE_ERROR;
  18197. }
  18198. /* XXX: if 'len' is low, may want to ensure array part is kept:
  18199. * the caller is likely to want a dense array.
  18200. */
  18201. duk_push_u32(ctx, len);
  18202. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W); /* [ ToUint32(len) array ToUint32(len) ] -> [ ToUint32(len) array ] */
  18203. return 1;
  18204. }
  18205. /* XXX: optimize by creating array into correct size directly, and
  18206. * operating on the array part directly; values can be memcpy()'d from
  18207. * value stack directly as long as refcounts are increased.
  18208. */
  18209. for (i = 0; i < nargs; i++) {
  18210. duk_dup(ctx, i);
  18211. duk_xdef_prop_index_wec(ctx, -2, (duk_uarridx_t) i);
  18212. }
  18213. duk_push_u32(ctx, (duk_uint32_t) nargs);
  18214. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W);
  18215. return 1;
  18216. }
  18217. /*
  18218. * isArray()
  18219. */
  18220. DUK_INTERNAL duk_ret_t duk_bi_array_constructor_is_array(duk_context *ctx) {
  18221. duk_hobject *h;
  18222. h = duk_get_hobject_with_class(ctx, 0, DUK_HOBJECT_CLASS_ARRAY);
  18223. duk_push_boolean(ctx, (h != NULL));
  18224. return 1;
  18225. }
  18226. /*
  18227. * toString()
  18228. */
  18229. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_to_string(duk_context *ctx) {
  18230. (void) duk_push_this_coercible_to_object(ctx);
  18231. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_JOIN);
  18232. /* [ ... this func ] */
  18233. if (!duk_is_callable(ctx, -1)) {
  18234. /* Fall back to the initial (original) Object.toString(). We don't
  18235. * currently have pointers to the built-in functions, only the top
  18236. * level global objects (like "Array") so this is now done in a bit
  18237. * of a hacky manner. It would be cleaner to push the (original)
  18238. * function and use duk_call_method().
  18239. */
  18240. /* XXX: 'this' will be ToObject() coerced twice, which is incorrect
  18241. * but should have no visible side effects.
  18242. */
  18243. DUK_DDD(DUK_DDDPRINT("this.join is not callable, fall back to (original) Object.toString"));
  18244. duk_set_top(ctx, 0);
  18245. return duk_bi_object_prototype_to_string(ctx); /* has access to 'this' binding */
  18246. }
  18247. /* [ ... this func ] */
  18248. duk_insert(ctx, -2);
  18249. /* [ ... func this ] */
  18250. DUK_DDD(DUK_DDDPRINT("calling: func=%!iT, this=%!iT",
  18251. (duk_tval *) duk_get_tval(ctx, -2),
  18252. (duk_tval *) duk_get_tval(ctx, -1)));
  18253. duk_call_method(ctx, 0);
  18254. return 1;
  18255. }
  18256. /*
  18257. * concat()
  18258. */
  18259. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_concat(duk_context *ctx) {
  18260. duk_idx_t i, n;
  18261. duk_uarridx_t idx, idx_last;
  18262. duk_uarridx_t j, len;
  18263. duk_hobject *h;
  18264. /* XXX: the insert here is a bit expensive if there are a lot of items.
  18265. * It could also be special cased in the outermost for loop quite easily
  18266. * (as the element is dup()'d anyway).
  18267. */
  18268. (void) duk_push_this_coercible_to_object(ctx);
  18269. duk_insert(ctx, 0);
  18270. n = duk_get_top(ctx);
  18271. duk_push_array(ctx); /* -> [ ToObject(this) item1 ... itemN arr ] */
  18272. /* NOTE: The Array special behaviors are NOT invoked by duk_xdef_prop_index()
  18273. * (which differs from the official algorithm). If no error is thrown, this
  18274. * doesn't matter as the length is updated at the end. However, if an error
  18275. * is thrown, the length will be unset. That shouldn't matter because the
  18276. * caller won't get a reference to the intermediate value.
  18277. */
  18278. idx = 0;
  18279. idx_last = 0;
  18280. for (i = 0; i < n; i++) {
  18281. DUK_ASSERT_TOP(ctx, n + 1);
  18282. /* [ ToObject(this) item1 ... itemN arr ] */
  18283. duk_dup(ctx, i);
  18284. h = duk_get_hobject_with_class(ctx, -1, DUK_HOBJECT_CLASS_ARRAY);
  18285. if (!h) {
  18286. duk_xdef_prop_index_wec(ctx, -2, idx++);
  18287. idx_last = idx;
  18288. continue;
  18289. }
  18290. /* [ ToObject(this) item1 ... itemN arr item(i) ] */
  18291. /* XXX: an array can have length higher than 32 bits; this is not handled
  18292. * correctly now.
  18293. */
  18294. len = (duk_uarridx_t) duk_get_length(ctx, -1);
  18295. for (j = 0; j < len; j++) {
  18296. if (duk_get_prop_index(ctx, -1, j)) {
  18297. /* [ ToObject(this) item1 ... itemN arr item(i) item(i)[j] ] */
  18298. duk_xdef_prop_index_wec(ctx, -3, idx++);
  18299. idx_last = idx;
  18300. } else {
  18301. idx++;
  18302. duk_pop(ctx);
  18303. #if defined(DUK_USE_NONSTD_ARRAY_CONCAT_TRAILER)
  18304. /* According to E5.1 Section 15.4.4.4 nonexistent trailing
  18305. * elements do not affect 'length' of the result. Test262
  18306. * and other engines disagree, so update idx_last here too.
  18307. */
  18308. idx_last = idx;
  18309. #else
  18310. /* Strict standard behavior, ignore trailing elements for
  18311. * result 'length'.
  18312. */
  18313. #endif
  18314. }
  18315. }
  18316. duk_pop(ctx);
  18317. }
  18318. /* The E5.1 Section 15.4.4.4 algorithm doesn't set the length explicitly
  18319. * in the end, but because we're operating with an internal value which
  18320. * is known to be an array, this should be equivalent.
  18321. */
  18322. duk_push_uarridx(ctx, idx_last);
  18323. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W);
  18324. DUK_ASSERT_TOP(ctx, n + 1);
  18325. return 1;
  18326. }
  18327. /*
  18328. * join(), toLocaleString()
  18329. *
  18330. * Note: checking valstack is necessary, but only in the per-element loop.
  18331. *
  18332. * Note: the trivial approach of pushing all the elements on the value stack
  18333. * and then calling duk_join() fails when the array contains a large number
  18334. * of elements. This problem can't be offloaded to duk_join() because the
  18335. * elements to join must be handled here and have special handling. Current
  18336. * approach is to do intermediate joins with very large number of elements.
  18337. * There is no fancy handling; the prefix gets re-joined multiple times.
  18338. */
  18339. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_join_shared(duk_context *ctx) {
  18340. duk_uint32_t len, count;
  18341. duk_uint32_t idx;
  18342. duk_small_int_t to_locale_string = duk_get_current_magic(ctx);
  18343. duk_idx_t valstack_required;
  18344. /* For join(), nargs is 1. For toLocaleString(), nargs is 0 and
  18345. * setting the top essentially pushes an undefined to the stack,
  18346. * thus defaulting to a comma separator.
  18347. */
  18348. duk_set_top(ctx, 1);
  18349. if (duk_is_undefined(ctx, 0)) {
  18350. duk_pop(ctx);
  18351. duk_push_hstring_stridx(ctx, DUK_STRIDX_COMMA);
  18352. } else {
  18353. duk_to_string(ctx, 0);
  18354. }
  18355. len = duk__push_this_obj_len_u32(ctx);
  18356. /* [ sep ToObject(this) len ] */
  18357. DUK_DDD(DUK_DDDPRINT("sep=%!T, this=%!T, len=%lu",
  18358. (duk_tval *) duk_get_tval(ctx, 0),
  18359. (duk_tval *) duk_get_tval(ctx, 1),
  18360. (unsigned long) len));
  18361. /* The extra (+4) is tight. */
  18362. valstack_required = (len >= DUK__ARRAY_MID_JOIN_LIMIT ?
  18363. DUK__ARRAY_MID_JOIN_LIMIT : len) + 4;
  18364. duk_require_stack(ctx, valstack_required);
  18365. duk_dup(ctx, 0);
  18366. /* [ sep ToObject(this) len sep ] */
  18367. count = 0;
  18368. idx = 0;
  18369. for (;;) {
  18370. if (count >= DUK__ARRAY_MID_JOIN_LIMIT || /* intermediate join to avoid valstack overflow */
  18371. idx >= len) { /* end of loop (careful with len==0) */
  18372. /* [ sep ToObject(this) len sep str0 ... str(count-1) ] */
  18373. DUK_DDD(DUK_DDDPRINT("mid/final join, count=%ld, idx=%ld, len=%ld",
  18374. (long) count, (long) idx, (long) len));
  18375. duk_join(ctx, (duk_idx_t) count); /* -> [ sep ToObject(this) len str ] */
  18376. duk_dup(ctx, 0); /* -> [ sep ToObject(this) len str sep ] */
  18377. duk_insert(ctx, -2); /* -> [ sep ToObject(this) len sep str ] */
  18378. count = 1;
  18379. }
  18380. if (idx >= len) {
  18381. /* if true, the stack already contains the final result */
  18382. break;
  18383. }
  18384. duk_get_prop_index(ctx, 1, (duk_uarridx_t) idx);
  18385. if (duk_is_null_or_undefined(ctx, -1)) {
  18386. duk_pop(ctx);
  18387. duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING);
  18388. } else {
  18389. if (to_locale_string) {
  18390. duk_to_object(ctx, -1);
  18391. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_TO_LOCALE_STRING);
  18392. duk_insert(ctx, -2); /* -> [ ... toLocaleString ToObject(val) ] */
  18393. duk_call_method(ctx, 0);
  18394. duk_to_string(ctx, -1);
  18395. } else {
  18396. duk_to_string(ctx, -1);
  18397. }
  18398. }
  18399. count++;
  18400. idx++;
  18401. }
  18402. /* [ sep ToObject(this) len sep result ] */
  18403. return 1;
  18404. }
  18405. /*
  18406. * pop(), push()
  18407. */
  18408. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_pop(duk_context *ctx) {
  18409. duk_uint32_t len;
  18410. duk_uint32_t idx;
  18411. DUK_ASSERT_TOP(ctx, 0);
  18412. len = duk__push_this_obj_len_u32(ctx);
  18413. if (len == 0) {
  18414. duk_push_int(ctx, 0);
  18415. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LENGTH);
  18416. return 0;
  18417. }
  18418. idx = len - 1;
  18419. duk_get_prop_index(ctx, 0, (duk_uarridx_t) idx);
  18420. duk_del_prop_index(ctx, 0, (duk_uarridx_t) idx);
  18421. duk_push_u32(ctx, idx);
  18422. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LENGTH);
  18423. return 1;
  18424. }
  18425. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_push(duk_context *ctx) {
  18426. /* Note: 'this' is not necessarily an Array object. The push()
  18427. * algorithm is supposed to work for other kinds of objects too,
  18428. * so the algorithm has e.g. an explicit update for the 'length'
  18429. * property which is normally "magical" in arrays.
  18430. */
  18431. duk_uint32_t len;
  18432. duk_idx_t i, n;
  18433. n = duk_get_top(ctx);
  18434. len = duk__push_this_obj_len_u32(ctx);
  18435. /* [ arg1 ... argN obj length ] */
  18436. /* Technically Array.prototype.push() can create an Array with length
  18437. * longer than 2^32-1, i.e. outside the 32-bit range. The final length
  18438. * is *not* wrapped to 32 bits in the specification.
  18439. *
  18440. * This implementation tracks length with a uint32 because it's much
  18441. * more practical.
  18442. *
  18443. * See: test-bi-array-push-maxlen.js.
  18444. */
  18445. if (len + (duk_uint32_t) n < len) {
  18446. DUK_D(DUK_DPRINT("Array.prototype.push() would go beyond 32-bit length, throw"));
  18447. return DUK_RET_RANGE_ERROR;
  18448. }
  18449. for (i = 0; i < n; i++) {
  18450. duk_dup(ctx, i);
  18451. duk_put_prop_index(ctx, -3, len + i);
  18452. }
  18453. len += n;
  18454. duk_push_u32(ctx, len);
  18455. duk_dup_top(ctx);
  18456. duk_put_prop_stridx(ctx, -4, DUK_STRIDX_LENGTH);
  18457. /* [ arg1 ... argN obj length new_length ] */
  18458. return 1;
  18459. }
  18460. /*
  18461. * sort()
  18462. *
  18463. * Currently qsort with random pivot. This is now really, really slow,
  18464. * because there is no fast path for array parts.
  18465. *
  18466. * Signed indices are used because qsort() leaves and degenerate cases
  18467. * may use a negative offset.
  18468. */
  18469. DUK_LOCAL duk_small_int_t duk__array_sort_compare(duk_context *ctx, duk_int_t idx1, duk_int_t idx2) {
  18470. duk_bool_t have1, have2;
  18471. duk_bool_t undef1, undef2;
  18472. duk_small_int_t ret;
  18473. duk_idx_t idx_obj = 1; /* fixed offsets in valstack */
  18474. duk_idx_t idx_fn = 0;
  18475. duk_hstring *h1, *h2;
  18476. /* Fast exit if indices are identical. This is valid for a non-existent property,
  18477. * for an undefined value, and almost always for ToString() coerced comparison of
  18478. * arbitrary values (corner cases where this is not the case include e.g. a an
  18479. * object with varying ToString() coercion).
  18480. *
  18481. * The specification does not prohibit "caching" of values read from the array, so
  18482. * assuming equality for comparing an index with itself falls into the category of
  18483. * "caching".
  18484. *
  18485. * Also, compareFn may be inconsistent, so skipping a call to compareFn here may
  18486. * have an effect on the final result. The specification does not require any
  18487. * specific behavior for inconsistent compare functions, so again, this fast path
  18488. * is OK.
  18489. */
  18490. if (idx1 == idx2) {
  18491. DUK_DDD(DUK_DDDPRINT("duk__array_sort_compare: idx1=%ld, idx2=%ld -> indices identical, quick exit",
  18492. (long) idx1, (long) idx2));
  18493. return 0;
  18494. }
  18495. have1 = duk_get_prop_index(ctx, idx_obj, (duk_uarridx_t) idx1);
  18496. have2 = duk_get_prop_index(ctx, idx_obj, (duk_uarridx_t) idx2);
  18497. DUK_DDD(DUK_DDDPRINT("duk__array_sort_compare: idx1=%ld, idx2=%ld, have1=%ld, have2=%ld, val1=%!T, val2=%!T",
  18498. (long) idx1, (long) idx2, (long) have1, (long) have2,
  18499. (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1)));
  18500. if (have1) {
  18501. if (have2) {
  18502. ;
  18503. } else {
  18504. ret = -1;
  18505. goto pop_ret;
  18506. }
  18507. } else {
  18508. if (have2) {
  18509. ret = 1;
  18510. goto pop_ret;
  18511. } else {
  18512. ret = 0;
  18513. goto pop_ret;
  18514. }
  18515. }
  18516. undef1 = duk_is_undefined(ctx, -2);
  18517. undef2 = duk_is_undefined(ctx, -1);
  18518. if (undef1) {
  18519. if (undef2) {
  18520. ret = 0;
  18521. goto pop_ret;
  18522. } else {
  18523. ret = 1;
  18524. goto pop_ret;
  18525. }
  18526. } else {
  18527. if (undef2) {
  18528. ret = -1;
  18529. goto pop_ret;
  18530. } else {
  18531. ;
  18532. }
  18533. }
  18534. if (!duk_is_undefined(ctx, idx_fn)) {
  18535. duk_double_t d;
  18536. /* no need to check callable; duk_call() will do that */
  18537. duk_dup(ctx, idx_fn); /* -> [ ... x y fn ] */
  18538. duk_insert(ctx, -3); /* -> [ ... fn x y ] */
  18539. duk_call(ctx, 2); /* -> [ ... res ] */
  18540. /* The specification is a bit vague what to do if the return
  18541. * value is not a number. Other implementations seem to
  18542. * tolerate non-numbers but e.g. V8 won't apparently do a
  18543. * ToNumber().
  18544. */
  18545. /* XXX: best behavior for real world compatibility? */
  18546. d = duk_to_number(ctx, -1);
  18547. if (d < 0.0) {
  18548. ret = -1;
  18549. } else if (d > 0.0) {
  18550. ret = 1;
  18551. } else {
  18552. ret = 0;
  18553. }
  18554. duk_pop(ctx);
  18555. DUK_DDD(DUK_DDDPRINT("-> result %ld (from comparefn, after coercion)", (long) ret));
  18556. return ret;
  18557. }
  18558. /* string compare is the default (a bit oddly) */
  18559. h1 = duk_to_hstring(ctx, -2);
  18560. h2 = duk_to_hstring(ctx, -1);
  18561. DUK_ASSERT(h1 != NULL);
  18562. DUK_ASSERT(h2 != NULL);
  18563. ret = duk_js_string_compare(h1, h2); /* retval is directly usable */
  18564. goto pop_ret;
  18565. pop_ret:
  18566. duk_pop_2(ctx);
  18567. DUK_DDD(DUK_DDDPRINT("-> result %ld", (long) ret));
  18568. return ret;
  18569. }
  18570. DUK_LOCAL void duk__array_sort_swap(duk_context *ctx, duk_int_t l, duk_int_t r) {
  18571. duk_bool_t have_l, have_r;
  18572. duk_idx_t idx_obj = 1; /* fixed offset in valstack */
  18573. if (l == r) {
  18574. return;
  18575. }
  18576. /* swap elements; deal with non-existent elements correctly */
  18577. have_l = duk_get_prop_index(ctx, idx_obj, (duk_uarridx_t) l);
  18578. have_r = duk_get_prop_index(ctx, idx_obj, (duk_uarridx_t) r);
  18579. if (have_r) {
  18580. /* right exists, [[Put]] regardless whether or not left exists */
  18581. duk_put_prop_index(ctx, idx_obj, (duk_uarridx_t) l);
  18582. } else {
  18583. duk_del_prop_index(ctx, idx_obj, (duk_uarridx_t) l);
  18584. duk_pop(ctx);
  18585. }
  18586. if (have_l) {
  18587. duk_put_prop_index(ctx, idx_obj, (duk_uarridx_t) r);
  18588. } else {
  18589. duk_del_prop_index(ctx, idx_obj, (duk_uarridx_t) r);
  18590. duk_pop(ctx);
  18591. }
  18592. }
  18593. #if defined(DUK_USE_DDDPRINT)
  18594. /* Debug print which visualizes the qsort partitioning process. */
  18595. DUK_LOCAL void duk__debuglog_qsort_state(duk_context *ctx, duk_int_t lo, duk_int_t hi, duk_int_t pivot) {
  18596. char buf[4096];
  18597. char *ptr = buf;
  18598. duk_int_t i, n;
  18599. n = (duk_int_t) duk_get_length(ctx, 1);
  18600. if (n > 4000) {
  18601. n = 4000;
  18602. }
  18603. *ptr++ = '[';
  18604. for (i = 0; i < n; i++) {
  18605. if (i == pivot) {
  18606. *ptr++ = '|';
  18607. } else if (i == lo) {
  18608. *ptr++ = '<';
  18609. } else if (i == hi) {
  18610. *ptr++ = '>';
  18611. } else if (i >= lo && i <= hi) {
  18612. *ptr++ = '-';
  18613. } else {
  18614. *ptr++ = ' ';
  18615. }
  18616. }
  18617. *ptr++ = ']';
  18618. *ptr++ = '\0';
  18619. DUK_DDD(DUK_DDDPRINT("%s (lo=%ld, hi=%ld, pivot=%ld)",
  18620. (const char *) buf, (long) lo, (long) hi, (long) pivot));
  18621. }
  18622. #endif
  18623. DUK_LOCAL void duk__array_qsort(duk_context *ctx, duk_int_t lo, duk_int_t hi) {
  18624. duk_hthread *thr = (duk_hthread *) ctx;
  18625. duk_int_t p, l, r;
  18626. /* The lo/hi indices may be crossed and hi < 0 is possible at entry. */
  18627. DUK_DDD(DUK_DDDPRINT("duk__array_qsort: lo=%ld, hi=%ld, obj=%!T",
  18628. (long) lo, (long) hi, (duk_tval *) duk_get_tval(ctx, 1)));
  18629. DUK_ASSERT_TOP(ctx, 3);
  18630. /* In some cases it may be that lo > hi, or hi < 0; these
  18631. * degenerate cases happen e.g. for empty arrays, and in
  18632. * recursion leaves.
  18633. */
  18634. /* trivial cases */
  18635. if (hi - lo < 1) {
  18636. DUK_DDD(DUK_DDDPRINT("degenerate case, return immediately"));
  18637. return;
  18638. }
  18639. DUK_ASSERT(hi > lo);
  18640. DUK_ASSERT(hi - lo + 1 >= 2);
  18641. /* randomized pivot selection */
  18642. p = lo + (duk_util_tinyrandom_get_bits(thr, 30) % (hi - lo + 1)); /* rnd in [lo,hi] */
  18643. DUK_ASSERT(p >= lo && p <= hi);
  18644. DUK_DDD(DUK_DDDPRINT("lo=%ld, hi=%ld, chose pivot p=%ld",
  18645. (long) lo, (long) hi, (long) p));
  18646. /* move pivot out of the way */
  18647. duk__array_sort_swap(ctx, p, lo);
  18648. p = lo;
  18649. DUK_DDD(DUK_DDDPRINT("pivot moved out of the way: %!T", (duk_tval *) duk_get_tval(ctx, 1)));
  18650. l = lo + 1;
  18651. r = hi;
  18652. for (;;) {
  18653. /* find elements to swap */
  18654. for (;;) {
  18655. DUK_DDD(DUK_DDDPRINT("left scan: l=%ld, r=%ld, p=%ld",
  18656. (long) l, (long) r, (long) p));
  18657. if (l >= hi) {
  18658. break;
  18659. }
  18660. if (duk__array_sort_compare(ctx, l, p) >= 0) { /* !(l < p) */
  18661. break;
  18662. }
  18663. l++;
  18664. }
  18665. for (;;) {
  18666. DUK_DDD(DUK_DDDPRINT("right scan: l=%ld, r=%ld, p=%ld",
  18667. (long) l, (long) r, (long) p));
  18668. if (r <= lo) {
  18669. break;
  18670. }
  18671. if (duk__array_sort_compare(ctx, p, r) >= 0) { /* !(p < r) */
  18672. break;
  18673. }
  18674. r--;
  18675. }
  18676. if (l >= r) {
  18677. goto done;
  18678. }
  18679. DUK_ASSERT(l < r);
  18680. DUK_DDD(DUK_DDDPRINT("swap %ld and %ld", (long) l, (long) r));
  18681. duk__array_sort_swap(ctx, l, r);
  18682. DUK_DDD(DUK_DDDPRINT("after swap: %!T", (duk_tval *) duk_get_tval(ctx, 1)));
  18683. l++;
  18684. r--;
  18685. }
  18686. done:
  18687. /* Note that 'l' and 'r' may cross, i.e. r < l */
  18688. DUK_ASSERT(l >= lo && l <= hi);
  18689. DUK_ASSERT(r >= lo && r <= hi);
  18690. /* XXX: there's no explicit recursion bound here now. For the average
  18691. * qsort recursion depth O(log n) that's not really necessary: e.g. for
  18692. * 2**32 recursion depth would be about 32 which is OK. However, qsort
  18693. * worst case recursion depth is O(n) which may be a problem.
  18694. */
  18695. /* move pivot to its final place */
  18696. DUK_DDD(DUK_DDDPRINT("before final pivot swap: %!T", (duk_tval *) duk_get_tval(ctx, 1)));
  18697. duk__array_sort_swap(ctx, lo, r);
  18698. #if defined(DUK_USE_DDDPRINT)
  18699. duk__debuglog_qsort_state(ctx, lo, hi, r);
  18700. #endif
  18701. DUK_DDD(DUK_DDDPRINT("recurse: pivot=%ld, obj=%!T", (long) r, (duk_tval *) duk_get_tval(ctx, 1)));
  18702. duk__array_qsort(ctx, lo, r - 1);
  18703. duk__array_qsort(ctx, r + 1, hi);
  18704. }
  18705. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_sort(duk_context *ctx) {
  18706. duk_uint32_t len;
  18707. /* XXX: len >= 0x80000000 won't work below because a signed type
  18708. * is needed by qsort.
  18709. */
  18710. len = duk__push_this_obj_len_u32_limited(ctx);
  18711. /* stack[0] = compareFn
  18712. * stack[1] = ToObject(this)
  18713. * stack[2] = ToUint32(length)
  18714. */
  18715. if (len > 0) {
  18716. /* avoid degenerate cases, so that (len - 1) won't underflow */
  18717. duk__array_qsort(ctx, (duk_int_t) 0, (duk_int_t) (len - 1));
  18718. }
  18719. DUK_ASSERT_TOP(ctx, 3);
  18720. duk_pop(ctx);
  18721. return 1; /* return ToObject(this) */
  18722. }
  18723. /*
  18724. * splice()
  18725. */
  18726. /* XXX: this compiles to over 500 bytes now, even without special handling
  18727. * for an array part. Uses signed ints so does not handle full array range correctly.
  18728. */
  18729. /* XXX: can shift() / unshift() use the same helper?
  18730. * shift() is (close to?) <--> splice(0, 1)
  18731. * unshift is (close to?) <--> splice(0, 0, [items])?
  18732. */
  18733. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_splice(duk_context *ctx) {
  18734. duk_idx_t nargs;
  18735. duk_uint32_t len;
  18736. duk_bool_t have_delcount;
  18737. duk_int_t item_count;
  18738. duk_int_t act_start;
  18739. duk_int_t del_count;
  18740. duk_int_t i, n;
  18741. DUK_UNREF(have_delcount);
  18742. nargs = duk_get_top(ctx);
  18743. if (nargs < 2) {
  18744. duk_set_top(ctx, 2);
  18745. nargs = 2;
  18746. have_delcount = 0;
  18747. } else {
  18748. have_delcount = 1;
  18749. }
  18750. /* XXX: len >= 0x80000000 won't work below because we need to be
  18751. * able to represent -len.
  18752. */
  18753. len = duk__push_this_obj_len_u32_limited(ctx);
  18754. act_start = duk_to_int_clamped(ctx, 0, -((duk_int_t) len), (duk_int_t) len);
  18755. if (act_start < 0) {
  18756. act_start = len + act_start;
  18757. }
  18758. DUK_ASSERT(act_start >= 0 && act_start <= (duk_int_t) len);
  18759. #ifdef DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT
  18760. if (have_delcount) {
  18761. #endif
  18762. del_count = duk_to_int_clamped(ctx, 1, 0, len - act_start);
  18763. #ifdef DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT
  18764. } else {
  18765. /* E5.1 standard behavior when deleteCount is not given would be
  18766. * to treat it just like if 'undefined' was given, which coerces
  18767. * ultimately to 0. Real world behavior is to splice to the end
  18768. * of array, see test-bi-array-proto-splice-no-delcount.js.
  18769. */
  18770. del_count = len - act_start;
  18771. }
  18772. #endif
  18773. DUK_ASSERT(nargs >= 2);
  18774. item_count = (duk_int_t) (nargs - 2);
  18775. DUK_ASSERT(del_count >= 0 && del_count <= (duk_int_t) len - act_start);
  18776. DUK_ASSERT(del_count + act_start <= (duk_int_t) len);
  18777. /* For now, restrict result array into 32-bit length range. */
  18778. if (((duk_double_t) len) - ((duk_double_t) del_count) + ((duk_double_t) item_count) > (duk_double_t) DUK_UINT32_MAX) {
  18779. DUK_D(DUK_DPRINT("Array.prototype.splice() would go beyond 32-bit length, throw"));
  18780. return DUK_RET_RANGE_ERROR;
  18781. }
  18782. duk_push_array(ctx);
  18783. /* stack[0] = start
  18784. * stack[1] = deleteCount
  18785. * stack[2...nargs-1] = items
  18786. * stack[nargs] = ToObject(this) -3
  18787. * stack[nargs+1] = ToUint32(length) -2
  18788. * stack[nargs+2] = result array -1
  18789. */
  18790. DUK_ASSERT_TOP(ctx, nargs + 3);
  18791. /* Step 9: copy elements-to-be-deleted into the result array */
  18792. for (i = 0; i < del_count; i++) {
  18793. if (duk_get_prop_index(ctx, -3, (duk_uarridx_t) (act_start + i))) {
  18794. duk_xdef_prop_index_wec(ctx, -2, i); /* throw flag irrelevant (false in std alg) */
  18795. } else {
  18796. duk_pop(ctx);
  18797. }
  18798. }
  18799. duk_push_u32(ctx, (duk_uint32_t) del_count);
  18800. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W);
  18801. /* Steps 12 and 13: reorganize elements to make room for itemCount elements */
  18802. if (item_count < del_count) {
  18803. /* [ A B C D E F G H ] rel_index = 2, del_count 3, item count 1
  18804. * -> [ A B F G H ] (conceptual intermediate step)
  18805. * -> [ A B . F G H ] (placeholder marked)
  18806. * [ A B C F G H ] (actual result at this point, C will be replaced)
  18807. */
  18808. DUK_ASSERT_TOP(ctx, nargs + 3);
  18809. n = len - del_count;
  18810. for (i = act_start; i < n; i++) {
  18811. if (duk_get_prop_index(ctx, -3, (duk_uarridx_t) (i + del_count))) {
  18812. duk_put_prop_index(ctx, -4, (duk_uarridx_t) (i + item_count));
  18813. } else {
  18814. duk_pop(ctx);
  18815. duk_del_prop_index(ctx, -3, (duk_uarridx_t) (i + item_count));
  18816. }
  18817. }
  18818. DUK_ASSERT_TOP(ctx, nargs + 3);
  18819. /* loop iterator init and limit changed from standard algorithm */
  18820. n = len - del_count + item_count;
  18821. for (i = len - 1; i >= n; i--) {
  18822. duk_del_prop_index(ctx, -3, (duk_uarridx_t) i);
  18823. }
  18824. DUK_ASSERT_TOP(ctx, nargs + 3);
  18825. } else if (item_count > del_count) {
  18826. /* [ A B C D E F G H ] rel_index = 2, del_count 3, item count 4
  18827. * -> [ A B F G H ] (conceptual intermediate step)
  18828. * -> [ A B . . . . F G H ] (placeholder marked)
  18829. * [ A B C D E F F G H ] (actual result at this point)
  18830. */
  18831. DUK_ASSERT_TOP(ctx, nargs + 3);
  18832. /* loop iterator init and limit changed from standard algorithm */
  18833. for (i = len - del_count - 1; i >= act_start; i--) {
  18834. if (duk_get_prop_index(ctx, -3, (duk_uarridx_t) (i + del_count))) {
  18835. duk_put_prop_index(ctx, -4, (duk_uarridx_t) (i + item_count));
  18836. } else {
  18837. duk_pop(ctx);
  18838. duk_del_prop_index(ctx, -3, (duk_uarridx_t) (i + item_count));
  18839. }
  18840. }
  18841. DUK_ASSERT_TOP(ctx, nargs + 3);
  18842. } else {
  18843. /* [ A B C D E F G H ] rel_index = 2, del_count 3, item count 3
  18844. * -> [ A B F G H ] (conceptual intermediate step)
  18845. * -> [ A B . . . F G H ] (placeholder marked)
  18846. * [ A B C D E F G H ] (actual result at this point)
  18847. */
  18848. }
  18849. DUK_ASSERT_TOP(ctx, nargs + 3);
  18850. /* Step 15: insert itemCount elements into the hole made above */
  18851. for (i = 0; i < item_count; i++) {
  18852. duk_dup(ctx, i + 2); /* args start at index 2 */
  18853. duk_put_prop_index(ctx, -4, (duk_uarridx_t) (act_start + i));
  18854. }
  18855. /* Step 16: update length; note that the final length may be above 32 bit range
  18856. * (but we checked above that this isn't the case here)
  18857. */
  18858. duk_push_u32(ctx, len - del_count + item_count);
  18859. duk_put_prop_stridx(ctx, -4, DUK_STRIDX_LENGTH);
  18860. /* result array is already at the top of stack */
  18861. DUK_ASSERT_TOP(ctx, nargs + 3);
  18862. return 1;
  18863. }
  18864. /*
  18865. * reverse()
  18866. */
  18867. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_reverse(duk_context *ctx) {
  18868. duk_uint32_t len;
  18869. duk_uint32_t middle;
  18870. duk_uint32_t lower, upper;
  18871. duk_bool_t have_lower, have_upper;
  18872. len = duk__push_this_obj_len_u32(ctx);
  18873. middle = len / 2;
  18874. /* If len <= 1, middle will be 0 and for-loop bails out
  18875. * immediately (0 < 0 -> false).
  18876. */
  18877. for (lower = 0; lower < middle; lower++) {
  18878. DUK_ASSERT(len >= 2);
  18879. DUK_ASSERT_TOP(ctx, 2);
  18880. DUK_ASSERT(len >= lower + 1);
  18881. upper = len - lower - 1;
  18882. have_lower = duk_get_prop_index(ctx, -2, (duk_uarridx_t) lower);
  18883. have_upper = duk_get_prop_index(ctx, -3, (duk_uarridx_t) upper);
  18884. /* [ ToObject(this) ToUint32(length) lowerValue upperValue ] */
  18885. if (have_upper) {
  18886. duk_put_prop_index(ctx, -4, (duk_uarridx_t) lower);
  18887. } else {
  18888. duk_del_prop_index(ctx, -4, (duk_uarridx_t) lower);
  18889. duk_pop(ctx);
  18890. }
  18891. if (have_lower) {
  18892. duk_put_prop_index(ctx, -3, (duk_uarridx_t) upper);
  18893. } else {
  18894. duk_del_prop_index(ctx, -3, (duk_uarridx_t) upper);
  18895. duk_pop(ctx);
  18896. }
  18897. DUK_ASSERT_TOP(ctx, 2);
  18898. }
  18899. DUK_ASSERT_TOP(ctx, 2);
  18900. duk_pop(ctx); /* -> [ ToObject(this) ] */
  18901. return 1;
  18902. }
  18903. /*
  18904. * slice()
  18905. */
  18906. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_slice(duk_context *ctx) {
  18907. duk_uint32_t len;
  18908. duk_int_t start, end;
  18909. duk_int_t i;
  18910. duk_uarridx_t idx;
  18911. duk_uint32_t res_length = 0;
  18912. /* XXX: len >= 0x80000000 won't work below because we need to be
  18913. * able to represent -len.
  18914. */
  18915. len = duk__push_this_obj_len_u32_limited(ctx);
  18916. duk_push_array(ctx);
  18917. /* stack[0] = start
  18918. * stack[1] = end
  18919. * stack[2] = ToObject(this)
  18920. * stack[3] = ToUint32(length)
  18921. * stack[4] = result array
  18922. */
  18923. start = duk_to_int_clamped(ctx, 0, -((duk_int_t) len), (duk_int_t) len);
  18924. if (start < 0) {
  18925. start = len + start;
  18926. }
  18927. /* XXX: could duk_is_undefined() provide defaulting undefined to 'len'
  18928. * (the upper limit)?
  18929. */
  18930. if (duk_is_undefined(ctx, 1)) {
  18931. end = len;
  18932. } else {
  18933. end = duk_to_int_clamped(ctx, 1, -((duk_int_t) len), (duk_int_t) len);
  18934. if (end < 0) {
  18935. end = len + end;
  18936. }
  18937. }
  18938. DUK_ASSERT(start >= 0 && (duk_uint32_t) start <= len);
  18939. DUK_ASSERT(end >= 0 && (duk_uint32_t) end <= len);
  18940. idx = 0;
  18941. for (i = start; i < end; i++) {
  18942. DUK_ASSERT_TOP(ctx, 5);
  18943. if (duk_get_prop_index(ctx, 2, (duk_uarridx_t) i)) {
  18944. duk_xdef_prop_index_wec(ctx, 4, idx);
  18945. res_length = idx + 1;
  18946. } else {
  18947. duk_pop(ctx);
  18948. }
  18949. idx++;
  18950. DUK_ASSERT_TOP(ctx, 5);
  18951. }
  18952. duk_push_u32(ctx, res_length);
  18953. duk_xdef_prop_stridx(ctx, 4, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W);
  18954. DUK_ASSERT_TOP(ctx, 5);
  18955. return 1;
  18956. }
  18957. /*
  18958. * shift()
  18959. */
  18960. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_shift(duk_context *ctx) {
  18961. duk_uint32_t len;
  18962. duk_uint32_t i;
  18963. len = duk__push_this_obj_len_u32(ctx);
  18964. if (len == 0) {
  18965. duk_push_int(ctx, 0);
  18966. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LENGTH);
  18967. return 0;
  18968. }
  18969. duk_get_prop_index(ctx, 0, 0);
  18970. /* stack[0] = object (this)
  18971. * stack[1] = ToUint32(length)
  18972. * stack[2] = elem at index 0 (retval)
  18973. */
  18974. for (i = 1; i < len; i++) {
  18975. DUK_ASSERT_TOP(ctx, 3);
  18976. if (duk_get_prop_index(ctx, 0, (duk_uarridx_t) i)) {
  18977. /* fromPresent = true */
  18978. duk_put_prop_index(ctx, 0, (duk_uarridx_t) (i - 1));
  18979. } else {
  18980. /* fromPresent = false */
  18981. duk_del_prop_index(ctx, 0, (duk_uarridx_t) (i - 1));
  18982. duk_pop(ctx);
  18983. }
  18984. }
  18985. duk_del_prop_index(ctx, 0, (duk_uarridx_t) (len - 1));
  18986. duk_push_u32(ctx, (duk_uint32_t) (len - 1));
  18987. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LENGTH);
  18988. DUK_ASSERT_TOP(ctx, 3);
  18989. return 1;
  18990. }
  18991. /*
  18992. * unshift()
  18993. */
  18994. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_unshift(duk_context *ctx) {
  18995. duk_idx_t nargs;
  18996. duk_uint32_t len;
  18997. duk_uint32_t i;
  18998. nargs = duk_get_top(ctx);
  18999. len = duk__push_this_obj_len_u32(ctx);
  19000. /* stack[0...nargs-1] = unshift args (vararg)
  19001. * stack[nargs] = ToObject(this)
  19002. * stack[nargs+1] = ToUint32(length)
  19003. */
  19004. DUK_ASSERT_TOP(ctx, nargs + 2);
  19005. /* Note: unshift() may operate on indices above unsigned 32-bit range
  19006. * and the final length may be >= 2**32. However, we restrict the
  19007. * final result to 32-bit range for practicality.
  19008. */
  19009. if (len + (duk_uint32_t) nargs < len) {
  19010. DUK_D(DUK_DPRINT("Array.prototype.unshift() would go beyond 32-bit length, throw"));
  19011. return DUK_RET_RANGE_ERROR;
  19012. }
  19013. i = len;
  19014. while (i > 0) {
  19015. DUK_ASSERT_TOP(ctx, nargs + 2);
  19016. i--;
  19017. /* k+argCount-1; note that may be above 32-bit range */
  19018. if (duk_get_prop_index(ctx, -2, (duk_uarridx_t) i)) {
  19019. /* fromPresent = true */
  19020. /* [ ... ToObject(this) ToUint32(length) val ] */
  19021. duk_put_prop_index(ctx, -3, (duk_uarridx_t) (i + nargs)); /* -> [ ... ToObject(this) ToUint32(length) ] */
  19022. } else {
  19023. /* fromPresent = false */
  19024. /* [ ... ToObject(this) ToUint32(length) val ] */
  19025. duk_pop(ctx);
  19026. duk_del_prop_index(ctx, -2, (duk_uarridx_t) (i + nargs)); /* -> [ ... ToObject(this) ToUint32(length) ] */
  19027. }
  19028. DUK_ASSERT_TOP(ctx, nargs + 2);
  19029. }
  19030. for (i = 0; i < (duk_uint32_t) nargs; i++) {
  19031. DUK_ASSERT_TOP(ctx, nargs + 2);
  19032. duk_dup(ctx, i); /* -> [ ... ToObject(this) ToUint32(length) arg[i] ] */
  19033. duk_put_prop_index(ctx, -3, (duk_uarridx_t) i);
  19034. DUK_ASSERT_TOP(ctx, nargs + 2);
  19035. }
  19036. DUK_ASSERT_TOP(ctx, nargs + 2);
  19037. duk_push_u32(ctx, len + nargs);
  19038. duk_dup_top(ctx); /* -> [ ... ToObject(this) ToUint32(length) final_len final_len ] */
  19039. duk_put_prop_stridx(ctx, -4, DUK_STRIDX_LENGTH);
  19040. return 1;
  19041. }
  19042. /*
  19043. * indexOf(), lastIndexOf()
  19044. */
  19045. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_indexof_shared(duk_context *ctx) {
  19046. duk_idx_t nargs;
  19047. duk_int_t i, len;
  19048. duk_int_t from_index;
  19049. duk_small_int_t idx_step = duk_get_current_magic(ctx); /* idx_step is +1 for indexOf, -1 for lastIndexOf */
  19050. /* lastIndexOf() needs to be a vararg function because we must distinguish
  19051. * between an undefined fromIndex and a "not given" fromIndex; indexOf() is
  19052. * made vararg for symmetry although it doesn't strictly need to be.
  19053. */
  19054. nargs = duk_get_top(ctx);
  19055. duk_set_top(ctx, 2);
  19056. /* XXX: must be able to represent -len */
  19057. len = (duk_int_t) duk__push_this_obj_len_u32_limited(ctx);
  19058. if (len == 0) {
  19059. goto not_found;
  19060. }
  19061. /* Index clamping is a bit tricky, we must ensure that we'll only iterate
  19062. * through elements that exist and that the specific requirements from E5.1
  19063. * Sections 15.4.4.14 and 15.4.4.15 are fulfilled; especially:
  19064. *
  19065. * - indexOf: clamp to [-len,len], negative handling -> [0,len],
  19066. * if clamped result is len, for-loop bails out immediately
  19067. *
  19068. * - lastIndexOf: clamp to [-len-1, len-1], negative handling -> [-1, len-1],
  19069. * if clamped result is -1, for-loop bails out immediately
  19070. *
  19071. * If fromIndex is not given, ToInteger(undefined) = 0, which is correct
  19072. * for indexOf() but incorrect for lastIndexOf(). Hence special handling,
  19073. * and why lastIndexOf() needs to be a vararg function.
  19074. */
  19075. if (nargs >= 2) {
  19076. /* indexOf: clamp fromIndex to [-len, len]
  19077. * (if fromIndex == len, for-loop terminates directly)
  19078. *
  19079. * lastIndexOf: clamp fromIndex to [-len - 1, len - 1]
  19080. * (if clamped to -len-1 -> fromIndex becomes -1, terminates for-loop directly)
  19081. */
  19082. from_index = duk_to_int_clamped(ctx,
  19083. 1,
  19084. (idx_step > 0 ? -len : -len - 1),
  19085. (idx_step > 0 ? len : len - 1));
  19086. if (from_index < 0) {
  19087. /* for lastIndexOf, result may be -1 (mark immediate termination) */
  19088. from_index = len + from_index;
  19089. }
  19090. } else {
  19091. /* for indexOf, ToInteger(undefined) would be 0, i.e. correct, but
  19092. * handle both indexOf and lastIndexOf specially here.
  19093. */
  19094. if (idx_step > 0) {
  19095. from_index = 0;
  19096. } else {
  19097. from_index = len - 1;
  19098. }
  19099. }
  19100. /* stack[0] = searchElement
  19101. * stack[1] = fromIndex
  19102. * stack[2] = object
  19103. * stack[3] = length (not needed, but not popped above)
  19104. */
  19105. for (i = from_index; i >= 0 && i < len; i += idx_step) {
  19106. DUK_ASSERT_TOP(ctx, 4);
  19107. if (duk_get_prop_index(ctx, 2, (duk_uarridx_t) i)) {
  19108. DUK_ASSERT_TOP(ctx, 5);
  19109. if (duk_strict_equals(ctx, 0, 4)) {
  19110. duk_push_int(ctx, i);
  19111. return 1;
  19112. }
  19113. }
  19114. duk_pop(ctx);
  19115. }
  19116. not_found:
  19117. duk_push_int(ctx, -1);
  19118. return 1;
  19119. }
  19120. /*
  19121. * every(), some(), forEach(), map(), filter()
  19122. */
  19123. #define DUK__ITER_EVERY 0
  19124. #define DUK__ITER_SOME 1
  19125. #define DUK__ITER_FOREACH 2
  19126. #define DUK__ITER_MAP 3
  19127. #define DUK__ITER_FILTER 4
  19128. /* XXX: This helper is a bit awkward because the handling for the different iteration
  19129. * callers is quite different. This now compiles to a bit less than 500 bytes, so with
  19130. * 5 callers the net result is about 100 bytes / caller.
  19131. */
  19132. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_iter_shared(duk_context *ctx) {
  19133. duk_uint32_t len;
  19134. duk_uint32_t i;
  19135. duk_uarridx_t k;
  19136. duk_bool_t bval;
  19137. duk_small_int_t iter_type = duk_get_current_magic(ctx);
  19138. duk_uint32_t res_length = 0;
  19139. /* each call this helper serves has nargs==2 */
  19140. DUK_ASSERT_TOP(ctx, 2);
  19141. len = duk__push_this_obj_len_u32(ctx);
  19142. if (!duk_is_callable(ctx, 0)) {
  19143. goto type_error;
  19144. }
  19145. /* if thisArg not supplied, behave as if undefined was supplied */
  19146. if (iter_type == DUK__ITER_MAP || iter_type == DUK__ITER_FILTER) {
  19147. duk_push_array(ctx);
  19148. } else {
  19149. duk_push_undefined(ctx);
  19150. }
  19151. /* stack[0] = callback
  19152. * stack[1] = thisArg
  19153. * stack[2] = object
  19154. * stack[3] = ToUint32(length) (unused, but avoid unnecessary pop)
  19155. * stack[4] = result array (or undefined)
  19156. */
  19157. k = 0; /* result index for filter() */
  19158. for (i = 0; i < len; i++) {
  19159. DUK_ASSERT_TOP(ctx, 5);
  19160. if (!duk_get_prop_index(ctx, 2, (duk_uarridx_t) i)) {
  19161. #if defined(DUK_USE_NONSTD_ARRAY_MAP_TRAILER)
  19162. /* Real world behavior for map(): trailing non-existent
  19163. * elements don't invoke the user callback, but are still
  19164. * counted towards result 'length'.
  19165. */
  19166. if (iter_type == DUK__ITER_MAP) {
  19167. res_length = i + 1;
  19168. }
  19169. #else
  19170. /* Standard behavior for map(): trailing non-existent
  19171. * elements don't invoke the user callback and are not
  19172. * counted towards result 'length'.
  19173. */
  19174. #endif
  19175. duk_pop(ctx);
  19176. continue;
  19177. }
  19178. /* The original value needs to be preserved for filter(), hence
  19179. * this funny order. We can't re-get the value because of side
  19180. * effects.
  19181. */
  19182. duk_dup(ctx, 0);
  19183. duk_dup(ctx, 1);
  19184. duk_dup(ctx, -3);
  19185. duk_push_u32(ctx, i);
  19186. duk_dup(ctx, 2); /* [ ... val callback thisArg val i obj ] */
  19187. duk_call_method(ctx, 3); /* -> [ ... val retval ] */
  19188. switch (iter_type) {
  19189. case DUK__ITER_EVERY:
  19190. bval = duk_to_boolean(ctx, -1);
  19191. if (!bval) {
  19192. /* stack top contains 'false' */
  19193. return 1;
  19194. }
  19195. break;
  19196. case DUK__ITER_SOME:
  19197. bval = duk_to_boolean(ctx, -1);
  19198. if (bval) {
  19199. /* stack top contains 'true' */
  19200. return 1;
  19201. }
  19202. break;
  19203. case DUK__ITER_FOREACH:
  19204. /* nop */
  19205. break;
  19206. case DUK__ITER_MAP:
  19207. duk_dup(ctx, -1);
  19208. duk_xdef_prop_index_wec(ctx, 4, (duk_uarridx_t) i); /* retval to result[i] */
  19209. res_length = i + 1;
  19210. break;
  19211. case DUK__ITER_FILTER:
  19212. bval = duk_to_boolean(ctx, -1);
  19213. if (bval) {
  19214. duk_dup(ctx, -2); /* orig value */
  19215. duk_xdef_prop_index_wec(ctx, 4, (duk_uarridx_t) k);
  19216. k++;
  19217. res_length = k;
  19218. }
  19219. break;
  19220. default:
  19221. DUK_UNREACHABLE();
  19222. break;
  19223. }
  19224. duk_pop_2(ctx);
  19225. DUK_ASSERT_TOP(ctx, 5);
  19226. }
  19227. switch (iter_type) {
  19228. case DUK__ITER_EVERY:
  19229. duk_push_true(ctx);
  19230. break;
  19231. case DUK__ITER_SOME:
  19232. duk_push_false(ctx);
  19233. break;
  19234. case DUK__ITER_FOREACH:
  19235. duk_push_undefined(ctx);
  19236. break;
  19237. case DUK__ITER_MAP:
  19238. case DUK__ITER_FILTER:
  19239. DUK_ASSERT_TOP(ctx, 5);
  19240. DUK_ASSERT(duk_is_array(ctx, -1)); /* topmost element is the result array already */
  19241. duk_push_u32(ctx, res_length);
  19242. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W);
  19243. break;
  19244. default:
  19245. DUK_UNREACHABLE();
  19246. break;
  19247. }
  19248. return 1;
  19249. type_error:
  19250. return DUK_RET_TYPE_ERROR;
  19251. }
  19252. /*
  19253. * reduce(), reduceRight()
  19254. */
  19255. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_reduce_shared(duk_context *ctx) {
  19256. duk_idx_t nargs;
  19257. duk_bool_t have_acc;
  19258. duk_uint32_t i, len;
  19259. duk_small_int_t idx_step = duk_get_current_magic(ctx); /* idx_step is +1 for reduce, -1 for reduceRight */
  19260. /* We're a varargs function because we need to detect whether
  19261. * initialValue was given or not.
  19262. */
  19263. nargs = duk_get_top(ctx);
  19264. DUK_DDD(DUK_DDDPRINT("nargs=%ld", (long) nargs));
  19265. duk_set_top(ctx, 2);
  19266. len = duk__push_this_obj_len_u32(ctx);
  19267. if (!duk_is_callable(ctx, 0)) {
  19268. goto type_error;
  19269. }
  19270. /* stack[0] = callback fn
  19271. * stack[1] = initialValue
  19272. * stack[2] = object (coerced this)
  19273. * stack[3] = length (not needed, but not popped above)
  19274. * stack[4] = accumulator
  19275. */
  19276. have_acc = 0;
  19277. if (nargs >= 2) {
  19278. duk_dup(ctx, 1);
  19279. have_acc = 1;
  19280. }
  19281. DUK_DDD(DUK_DDDPRINT("have_acc=%ld, acc=%!T",
  19282. (long) have_acc, (duk_tval *) duk_get_tval(ctx, 3)));
  19283. /* For len == 0, i is initialized to len - 1 which underflows.
  19284. * The condition (i < len) will then exit the for-loop on the
  19285. * first round which is correct. Similarly, loop termination
  19286. * happens by i underflowing.
  19287. */
  19288. for (i = (idx_step >= 0 ? 0 : len - 1);
  19289. i < len; /* i >= 0 would always be true */
  19290. i += idx_step) {
  19291. DUK_DDD(DUK_DDDPRINT("i=%ld, len=%ld, have_acc=%ld, top=%ld, acc=%!T",
  19292. (long) i, (long) len, (long) have_acc,
  19293. (long) duk_get_top(ctx),
  19294. (duk_tval *) duk_get_tval(ctx, 4)));
  19295. DUK_ASSERT((have_acc && duk_get_top(ctx) == 5) ||
  19296. (!have_acc && duk_get_top(ctx) == 4));
  19297. if (!duk_has_prop_index(ctx, 2, (duk_uarridx_t) i)) {
  19298. continue;
  19299. }
  19300. if (!have_acc) {
  19301. DUK_ASSERT_TOP(ctx, 4);
  19302. duk_get_prop_index(ctx, 2, (duk_uarridx_t) i);
  19303. have_acc = 1;
  19304. DUK_ASSERT_TOP(ctx, 5);
  19305. } else {
  19306. DUK_ASSERT_TOP(ctx, 5);
  19307. duk_dup(ctx, 0);
  19308. duk_dup(ctx, 4);
  19309. duk_get_prop_index(ctx, 2, (duk_uarridx_t) i);
  19310. duk_push_u32(ctx, i);
  19311. duk_dup(ctx, 2);
  19312. DUK_DDD(DUK_DDDPRINT("calling reduce function: func=%!T, prev=%!T, curr=%!T, idx=%!T, obj=%!T",
  19313. (duk_tval *) duk_get_tval(ctx, -5), (duk_tval *) duk_get_tval(ctx, -4),
  19314. (duk_tval *) duk_get_tval(ctx, -3), (duk_tval *) duk_get_tval(ctx, -2),
  19315. (duk_tval *) duk_get_tval(ctx, -1)));
  19316. duk_call(ctx, 4);
  19317. DUK_DDD(DUK_DDDPRINT("-> result: %!T", (duk_tval *) duk_get_tval(ctx, -1)));
  19318. duk_replace(ctx, 4);
  19319. DUK_ASSERT_TOP(ctx, 5);
  19320. }
  19321. }
  19322. if (!have_acc) {
  19323. goto type_error;
  19324. }
  19325. DUK_ASSERT_TOP(ctx, 5);
  19326. return 1;
  19327. type_error:
  19328. return DUK_RET_TYPE_ERROR;
  19329. }
  19330. #undef DUK__ARRAY_MID_JOIN_LIMIT
  19331. #undef DUK__ITER_EVERY
  19332. #undef DUK__ITER_SOME
  19333. #undef DUK__ITER_FOREACH
  19334. #undef DUK__ITER_MAP
  19335. #undef DUK__ITER_FILTER
  19336. #line 1 "duk_bi_boolean.c"
  19337. /*
  19338. * Boolean built-ins
  19339. */
  19340. /* include removed: duk_internal.h */
  19341. /* Shared helper to provide toString() and valueOf(). Checks 'this', gets
  19342. * the primitive value to stack top, and optionally coerces with ToString().
  19343. */
  19344. DUK_INTERNAL duk_ret_t duk_bi_boolean_prototype_tostring_shared(duk_context *ctx) {
  19345. duk_tval *tv;
  19346. duk_hobject *h;
  19347. duk_small_int_t coerce_tostring = duk_get_current_magic(ctx);
  19348. /* XXX: there is room to use a shared helper here, many built-ins
  19349. * check the 'this' type, and if it's an object, check its class,
  19350. * then get its internal value, etc.
  19351. */
  19352. duk_push_this(ctx);
  19353. tv = duk_get_tval(ctx, -1);
  19354. DUK_ASSERT(tv != NULL);
  19355. if (DUK_TVAL_IS_BOOLEAN(tv)) {
  19356. goto type_ok;
  19357. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  19358. h = DUK_TVAL_GET_OBJECT(tv);
  19359. DUK_ASSERT(h != NULL);
  19360. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_BOOLEAN) {
  19361. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE);
  19362. DUK_ASSERT(duk_is_boolean(ctx, -1));
  19363. goto type_ok;
  19364. }
  19365. }
  19366. return DUK_RET_TYPE_ERROR;
  19367. type_ok:
  19368. if (coerce_tostring) {
  19369. duk_to_string(ctx, -1);
  19370. }
  19371. return 1;
  19372. }
  19373. DUK_INTERNAL duk_ret_t duk_bi_boolean_constructor(duk_context *ctx) {
  19374. duk_hthread *thr = (duk_hthread *) ctx;
  19375. duk_hobject *h_this;
  19376. DUK_UNREF(thr);
  19377. duk_to_boolean(ctx, 0);
  19378. if (duk_is_constructor_call(ctx)) {
  19379. /* XXX: helper; rely on Boolean.prototype as being non-writable, non-configurable */
  19380. duk_push_this(ctx);
  19381. h_this = duk_get_hobject(ctx, -1);
  19382. DUK_ASSERT(h_this != NULL);
  19383. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_this) == thr->builtins[DUK_BIDX_BOOLEAN_PROTOTYPE]);
  19384. DUK_HOBJECT_SET_CLASS_NUMBER(h_this, DUK_HOBJECT_CLASS_BOOLEAN);
  19385. duk_dup(ctx, 0); /* -> [ val obj val ] */
  19386. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE); /* XXX: proper flags? */
  19387. } /* unbalanced stack */
  19388. return 1;
  19389. }
  19390. #line 1 "duk_bi_buffer.c"
  19391. /*
  19392. * Duktape.Buffer, Node.js Buffer, and Khronos/ES6 TypedArray built-ins
  19393. */
  19394. /* include removed: duk_internal.h */
  19395. /*
  19396. * Misc helpers
  19397. */
  19398. /* Map DUK_HBUFFEROBJECT_ELEM_xxx to duk_hobject class number.
  19399. * Sync with duk_hbufferobject.h and duk_hobject.h.
  19400. */
  19401. static const duk_uint8_t duk__buffer_class_from_elemtype[9] = {
  19402. DUK_HOBJECT_CLASS_UINT8ARRAY,
  19403. DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY,
  19404. DUK_HOBJECT_CLASS_INT8ARRAY,
  19405. DUK_HOBJECT_CLASS_UINT16ARRAY,
  19406. DUK_HOBJECT_CLASS_INT16ARRAY,
  19407. DUK_HOBJECT_CLASS_UINT32ARRAY,
  19408. DUK_HOBJECT_CLASS_INT32ARRAY,
  19409. DUK_HOBJECT_CLASS_FLOAT32ARRAY,
  19410. DUK_HOBJECT_CLASS_FLOAT64ARRAY
  19411. };
  19412. /* Map DUK_HBUFFEROBJECT_ELEM_xxx to prototype object built-in index.
  19413. * Sync with duk_hbufferobject.h.
  19414. */
  19415. static const duk_uint16_t duk__buffer_proto_from_elemtype[9] = {
  19416. DUK_BIDX_UINT8ARRAY_PROTOTYPE,
  19417. DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE,
  19418. DUK_BIDX_INT8ARRAY_PROTOTYPE,
  19419. DUK_BIDX_UINT16ARRAY_PROTOTYPE,
  19420. DUK_BIDX_INT16ARRAY_PROTOTYPE,
  19421. DUK_BIDX_UINT32ARRAY_PROTOTYPE,
  19422. DUK_BIDX_INT32ARRAY_PROTOTYPE,
  19423. DUK_BIDX_FLOAT32ARRAY_PROTOTYPE,
  19424. DUK_BIDX_FLOAT64ARRAY_PROTOTYPE
  19425. };
  19426. /* Map DUK_HBUFFEROBJECT_ELEM_xxx to byte size.
  19427. */
  19428. static const duk_uint8_t duk__buffer_nbytes_from_fldtype[6] = {
  19429. 1, /* DUK__FLD_8BIT */
  19430. 2, /* DUK__FLD_16BIT */
  19431. 4, /* DUK__FLD_32BIT */
  19432. 4, /* DUK__FLD_FLOAT */
  19433. 8, /* DUK__FLD_DOUBLE */
  19434. 0 /* DUK__FLD_VARINT; not relevant here */
  19435. };
  19436. /* Bitfield for each DUK_HBUFFEROBJECT_ELEM_xxx indicating which element types
  19437. * are compatible with a blind byte copy for the TypedArray set() method (also
  19438. * used for TypedArray constructor). Array index is target buffer elem type,
  19439. * bitfield indicates compatible source types. The types must have same byte
  19440. * size and they must be coercion compatible.
  19441. */
  19442. static duk_uint16_t duk__buffer_elemtype_copy_compatible[9] = {
  19443. /* xxx -> DUK_HBUFFEROBJECT_ELEM_UINT8 */
  19444. (1U << DUK_HBUFFEROBJECT_ELEM_UINT8) |
  19445. (1U << DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED) |
  19446. (1U << DUK_HBUFFEROBJECT_ELEM_INT8),
  19447. /* xxx -> DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED
  19448. * Note: INT8 is -not- copy compatible, e.g. -1 would coerce to 0x00.
  19449. */
  19450. (1U << DUK_HBUFFEROBJECT_ELEM_UINT8) |
  19451. (1U << DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED),
  19452. /* xxx -> DUK_HBUFFEROBJECT_ELEM_INT8 */
  19453. (1U << DUK_HBUFFEROBJECT_ELEM_UINT8) |
  19454. (1U << DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED) |
  19455. (1U << DUK_HBUFFEROBJECT_ELEM_INT8),
  19456. /* xxx -> DUK_HBUFFEROBJECT_ELEM_UINT16 */
  19457. (1U << DUK_HBUFFEROBJECT_ELEM_UINT16) |
  19458. (1U << DUK_HBUFFEROBJECT_ELEM_INT16),
  19459. /* xxx -> DUK_HBUFFEROBJECT_ELEM_INT16 */
  19460. (1U << DUK_HBUFFEROBJECT_ELEM_UINT16) |
  19461. (1U << DUK_HBUFFEROBJECT_ELEM_INT16),
  19462. /* xxx -> DUK_HBUFFEROBJECT_ELEM_UINT32 */
  19463. (1U << DUK_HBUFFEROBJECT_ELEM_UINT32) |
  19464. (1U << DUK_HBUFFEROBJECT_ELEM_INT32),
  19465. /* xxx -> DUK_HBUFFEROBJECT_ELEM_INT32 */
  19466. (1U << DUK_HBUFFEROBJECT_ELEM_UINT32) |
  19467. (1U << DUK_HBUFFEROBJECT_ELEM_INT32),
  19468. /* xxx -> DUK_HBUFFEROBJECT_ELEM_FLOAT32 */
  19469. (1U << DUK_HBUFFEROBJECT_ELEM_FLOAT32),
  19470. /* xxx -> DUK_HBUFFEROBJECT_ELEM_FLOAT64 */
  19471. (1U << DUK_HBUFFEROBJECT_ELEM_FLOAT64)
  19472. };
  19473. /* Shared helper. */
  19474. DUK_LOCAL duk_hbufferobject *duk__getrequire_bufobj_this(duk_context *ctx, duk_bool_t throw_flag) {
  19475. duk_hthread *thr;
  19476. duk_tval *tv;
  19477. duk_hbufferobject *h_this;
  19478. DUK_ASSERT(ctx != NULL);
  19479. thr = (duk_hthread *) ctx;
  19480. tv = duk_get_borrowed_this_tval(ctx);
  19481. DUK_ASSERT(tv != NULL);
  19482. if (DUK_TVAL_IS_OBJECT(tv)) {
  19483. h_this = (duk_hbufferobject *) DUK_TVAL_GET_OBJECT(tv);
  19484. DUK_ASSERT(h_this != NULL);
  19485. if (DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) h_this)) {
  19486. DUK_ASSERT_HBUFFEROBJECT_VALID(h_this);
  19487. return h_this;
  19488. }
  19489. }
  19490. if (throw_flag) {
  19491. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_BUFFER);
  19492. }
  19493. return NULL;
  19494. }
  19495. /* Check that 'this' is a duk_hbufferobject and return a pointer to it. */
  19496. DUK_LOCAL duk_hbufferobject *duk__get_bufobj_this(duk_context *ctx) {
  19497. return duk__getrequire_bufobj_this(ctx, 0);
  19498. }
  19499. /* Check that 'this' is a duk_hbufferobject and return a pointer to it
  19500. * (NULL if not).
  19501. */
  19502. DUK_LOCAL duk_hbufferobject *duk__require_bufobj_this(duk_context *ctx) {
  19503. return duk__getrequire_bufobj_this(ctx, 1);
  19504. }
  19505. /* Check that value is a duk_hbufferobject and return a pointer to it. */
  19506. DUK_LOCAL duk_hbufferobject *duk__require_bufobj_value(duk_context *ctx, duk_idx_t index) {
  19507. duk_hthread *thr;
  19508. duk_tval *tv;
  19509. duk_hbufferobject *h_obj;
  19510. thr = (duk_hthread *) ctx;
  19511. /* Don't accept relative indices now. */
  19512. DUK_ASSERT(index >= 0);
  19513. tv = duk_require_tval(ctx, index);
  19514. DUK_ASSERT(tv != NULL);
  19515. if (DUK_TVAL_IS_OBJECT(tv)) {
  19516. h_obj = (duk_hbufferobject *) DUK_TVAL_GET_OBJECT(tv);
  19517. DUK_ASSERT(h_obj != NULL);
  19518. if (DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) h_obj)) {
  19519. DUK_ASSERT_HBUFFEROBJECT_VALID(h_obj);
  19520. return h_obj;
  19521. }
  19522. }
  19523. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_BUFFER);
  19524. }
  19525. DUK_LOCAL void duk__set_bufobj_buffer(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_hbuffer *h_val) {
  19526. duk_hthread *thr;
  19527. thr = (duk_hthread *) ctx;
  19528. DUK_UNREF(thr);
  19529. DUK_ASSERT(ctx != NULL);
  19530. DUK_ASSERT(h_bufobj != NULL);
  19531. DUK_ASSERT(h_bufobj->buf == NULL); /* no need to decref */
  19532. DUK_ASSERT(h_val != NULL);
  19533. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  19534. h_bufobj->buf = h_val;
  19535. DUK_HBUFFER_INCREF(thr, h_val);
  19536. h_bufobj->length = DUK_HBUFFER_GET_SIZE(h_val);
  19537. DUK_ASSERT(h_bufobj->shift == 0);
  19538. DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8);
  19539. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  19540. }
  19541. DUK_LOCAL duk_hbufferobject *duk__push_arraybuffer_with_length(duk_context *ctx, duk_uint_t len) {
  19542. duk_hbuffer *h_val;
  19543. duk_hbufferobject *h_bufobj;
  19544. (void) duk_push_fixed_buffer(ctx, (duk_size_t) len);
  19545. h_val = (duk_hbuffer *) duk_get_hbuffer(ctx, -1);
  19546. DUK_ASSERT(h_val != NULL);
  19547. h_bufobj = duk_push_bufferobject(ctx,
  19548. DUK_HOBJECT_FLAG_EXTENSIBLE |
  19549. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  19550. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAYBUFFER),
  19551. DUK_BIDX_ARRAYBUFFER_PROTOTYPE);
  19552. DUK_ASSERT(h_bufobj != NULL);
  19553. duk__set_bufobj_buffer(ctx, h_bufobj, h_val);
  19554. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  19555. return h_bufobj;
  19556. }
  19557. /* Shared offset/length coercion helper. */
  19558. DUK_LOCAL void duk__resolve_offset_opt_length(duk_context *ctx,
  19559. duk_hbufferobject *h_bufarg,
  19560. duk_idx_t idx_offset,
  19561. duk_idx_t idx_length,
  19562. duk_uint_t *out_offset,
  19563. duk_uint_t *out_length,
  19564. duk_bool_t throw_flag) {
  19565. duk_hthread *thr;
  19566. duk_int_t offset_signed;
  19567. duk_int_t length_signed;
  19568. duk_uint_t offset;
  19569. duk_uint_t length;
  19570. thr = (duk_hthread *) ctx;
  19571. DUK_UNREF(thr);
  19572. offset_signed = duk_to_int(ctx, idx_offset);
  19573. if (offset_signed < 0) {
  19574. goto fail_range;
  19575. }
  19576. offset = (duk_uint_t) offset_signed;
  19577. if (offset > h_bufarg->length) {
  19578. goto fail_range;
  19579. }
  19580. DUK_ASSERT_DISABLE(offset >= 0); /* unsigned */
  19581. DUK_ASSERT(offset <= h_bufarg->length);
  19582. if (duk_is_undefined(ctx, idx_length)) {
  19583. DUK_ASSERT(h_bufarg->length >= offset);
  19584. length = h_bufarg->length - offset; /* >= 0 */
  19585. } else {
  19586. length_signed = duk_to_int(ctx, idx_length);
  19587. if (length_signed < 0) {
  19588. goto fail_range;
  19589. }
  19590. length = (duk_uint_t) length_signed;
  19591. DUK_ASSERT(h_bufarg->length >= offset);
  19592. if (length > h_bufarg->length - offset) {
  19593. /* Unlike for negative arguments, some call sites
  19594. * want length to be clamped if it's positive.
  19595. */
  19596. if (throw_flag) {
  19597. goto fail_range;
  19598. } else {
  19599. length = h_bufarg->length - offset;
  19600. }
  19601. }
  19602. }
  19603. DUK_ASSERT_DISABLE(length >= 0); /* unsigned */
  19604. DUK_ASSERT(offset + length <= h_bufarg->length);
  19605. *out_offset = offset;
  19606. *out_length = length;
  19607. return;
  19608. fail_range:
  19609. duk_error(thr, DUK_ERR_RANGE_ERROR, DUK_STR_INVALID_CALL_ARGS);
  19610. }
  19611. /* Shared lenient buffer length clamping helper. No negative indices, no
  19612. * element/byte shifting.
  19613. */
  19614. DUK_LOCAL void duk__clamp_startend_nonegidx_noshift(duk_context *ctx,
  19615. duk_hbufferobject *h_bufobj,
  19616. duk_idx_t idx_start,
  19617. duk_idx_t idx_end,
  19618. duk_int_t *out_start_offset,
  19619. duk_int_t *out_end_offset) {
  19620. duk_int_t buffer_length;
  19621. duk_int_t start_offset;
  19622. duk_int_t end_offset;
  19623. DUK_ASSERT(out_start_offset != NULL);
  19624. DUK_ASSERT(out_end_offset != NULL);
  19625. buffer_length = (duk_int_t) h_bufobj->length;
  19626. /* undefined coerces to zero which is correct */
  19627. start_offset = duk_to_int_clamped(ctx, idx_start, 0, buffer_length);
  19628. if (duk_is_undefined(ctx, idx_end)) {
  19629. end_offset = buffer_length;
  19630. } else {
  19631. end_offset = duk_to_int_clamped(ctx, idx_end, start_offset, buffer_length);
  19632. }
  19633. DUK_ASSERT(start_offset >= 0);
  19634. DUK_ASSERT(start_offset <= buffer_length);
  19635. DUK_ASSERT(end_offset >= 0);
  19636. DUK_ASSERT(end_offset <= buffer_length);
  19637. DUK_ASSERT(start_offset <= end_offset);
  19638. *out_start_offset = start_offset;
  19639. *out_end_offset = end_offset;
  19640. }
  19641. /* Shared lenient buffer length clamping helper. Indices are treated as
  19642. * element indices (though output values are byte offsets) which only
  19643. * really matters for TypedArray views as other buffer object have a zero
  19644. * shift. Negative indices are counted from end of input slice; crossed
  19645. * indices are clamped to zero length; and final indices are clamped
  19646. * against input slice. Used for e.g. ArrayBuffer slice().
  19647. */
  19648. DUK_LOCAL void duk__clamp_startend_negidx_shifted(duk_context *ctx,
  19649. duk_hbufferobject *h_bufobj,
  19650. duk_idx_t idx_start,
  19651. duk_idx_t idx_end,
  19652. duk_int_t *out_start_offset,
  19653. duk_int_t *out_end_offset) {
  19654. duk_int_t buffer_length;
  19655. duk_int_t start_offset;
  19656. duk_int_t end_offset;
  19657. DUK_ASSERT(out_start_offset != NULL);
  19658. DUK_ASSERT(out_end_offset != NULL);
  19659. buffer_length = (duk_int_t) h_bufobj->length;
  19660. buffer_length >>= h_bufobj->shift; /* as elements */
  19661. /* Resolve start/end offset as element indices first; arguments
  19662. * at idx_start/idx_end are element offsets. Working with element
  19663. * indices first also avoids potential for wrapping.
  19664. */
  19665. start_offset = duk_to_int(ctx, idx_start);
  19666. if (start_offset < 0) {
  19667. start_offset = buffer_length + start_offset;
  19668. }
  19669. if (duk_is_undefined(ctx, idx_end)) {
  19670. end_offset = buffer_length;
  19671. } else {
  19672. end_offset = duk_to_int(ctx, idx_end);
  19673. if (end_offset < 0) {
  19674. end_offset = buffer_length + end_offset;
  19675. }
  19676. }
  19677. /* Note: start_offset/end_offset can still be < 0 here. */
  19678. if (start_offset < 0) {
  19679. start_offset = 0;
  19680. } else if (start_offset > buffer_length) {
  19681. start_offset = buffer_length;
  19682. }
  19683. if (end_offset < start_offset) {
  19684. end_offset = start_offset;
  19685. } else if (end_offset > buffer_length) {
  19686. end_offset = buffer_length;
  19687. }
  19688. DUK_ASSERT(start_offset >= 0);
  19689. DUK_ASSERT(start_offset <= buffer_length);
  19690. DUK_ASSERT(end_offset >= 0);
  19691. DUK_ASSERT(end_offset <= buffer_length);
  19692. DUK_ASSERT(start_offset <= end_offset);
  19693. /* Convert indices to byte offsets. */
  19694. start_offset <<= h_bufobj->shift;
  19695. end_offset <<= h_bufobj->shift;
  19696. *out_start_offset = start_offset;
  19697. *out_end_offset = end_offset;
  19698. }
  19699. /*
  19700. * Indexed read/write helpers (also used from outside this file)
  19701. */
  19702. 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) {
  19703. duk_double_union du;
  19704. DUK_MEMCPY((void *) du.uc, (const void *) p, elem_size);
  19705. switch (h_bufobj->elem_type) {
  19706. case DUK_HBUFFEROBJECT_ELEM_UINT8:
  19707. case DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED:
  19708. duk_push_uint(ctx, (duk_uint_t) du.uc[0]);
  19709. break;
  19710. case DUK_HBUFFEROBJECT_ELEM_INT8:
  19711. duk_push_int(ctx, (duk_int_t) (duk_int8_t) du.uc[0]);
  19712. break;
  19713. case DUK_HBUFFEROBJECT_ELEM_UINT16:
  19714. duk_push_uint(ctx, (duk_uint_t) du.us[0]);
  19715. break;
  19716. case DUK_HBUFFEROBJECT_ELEM_INT16:
  19717. duk_push_int(ctx, (duk_int_t) (duk_int16_t) du.us[0]);
  19718. break;
  19719. case DUK_HBUFFEROBJECT_ELEM_UINT32:
  19720. duk_push_uint(ctx, (duk_uint_t) du.ui[0]);
  19721. break;
  19722. case DUK_HBUFFEROBJECT_ELEM_INT32:
  19723. duk_push_int(ctx, (duk_int_t) (duk_int32_t) du.ui[0]);
  19724. break;
  19725. case DUK_HBUFFEROBJECT_ELEM_FLOAT32:
  19726. duk_push_number(ctx, (duk_double_t) du.f[0]);
  19727. break;
  19728. case DUK_HBUFFEROBJECT_ELEM_FLOAT64:
  19729. duk_push_number(ctx, (duk_double_t) du.d);
  19730. break;
  19731. default:
  19732. DUK_UNREACHABLE();
  19733. }
  19734. }
  19735. DUK_INTERNAL void duk_hbufferobject_validated_write(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size) {
  19736. duk_double_union du;
  19737. /* NOTE! Caller must ensure that any side effects from the
  19738. * coercions below are safe. If that cannot be guaranteed
  19739. * (which is normally the case), caller must coerce the
  19740. * argument using duk_to_number() before any pointer
  19741. * validations; the result of duk_to_number() always coerces
  19742. * without side effects here.
  19743. */
  19744. switch (h_bufobj->elem_type) {
  19745. case DUK_HBUFFEROBJECT_ELEM_UINT8:
  19746. du.uc[0] = (duk_uint8_t) duk_to_uint32(ctx, -1);
  19747. break;
  19748. case DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED:
  19749. du.uc[0] = (duk_uint8_t) duk_to_uint8clamped(ctx, -1);
  19750. break;
  19751. case DUK_HBUFFEROBJECT_ELEM_INT8:
  19752. du.uc[0] = (duk_uint8_t) duk_to_int32(ctx, -1);
  19753. break;
  19754. case DUK_HBUFFEROBJECT_ELEM_UINT16:
  19755. du.us[0] = (duk_uint16_t) duk_to_uint32(ctx, -1);
  19756. break;
  19757. case DUK_HBUFFEROBJECT_ELEM_INT16:
  19758. du.us[0] = (duk_uint16_t) duk_to_int32(ctx, -1);
  19759. break;
  19760. case DUK_HBUFFEROBJECT_ELEM_UINT32:
  19761. du.ui[0] = (duk_uint32_t) duk_to_uint32(ctx, -1);
  19762. break;
  19763. case DUK_HBUFFEROBJECT_ELEM_INT32:
  19764. du.ui[0] = (duk_uint32_t) duk_to_int32(ctx, -1);
  19765. break;
  19766. case DUK_HBUFFEROBJECT_ELEM_FLOAT32:
  19767. du.f[0] = (duk_float_t) duk_to_number(ctx, -1);
  19768. break;
  19769. case DUK_HBUFFEROBJECT_ELEM_FLOAT64:
  19770. du.d = (duk_double_t) duk_to_number(ctx, -1);
  19771. break;
  19772. default:
  19773. DUK_UNREACHABLE();
  19774. }
  19775. DUK_MEMCPY((void *) p, (const void *) du.uc, elem_size);
  19776. }
  19777. /*
  19778. * Duktape.Buffer: constructor
  19779. */
  19780. DUK_INTERNAL duk_ret_t duk_bi_buffer_constructor(duk_context *ctx) {
  19781. duk_hthread *thr;
  19782. duk_size_t buf_size;
  19783. duk_small_int_t buf_dynamic;
  19784. duk_uint8_t *buf_data;
  19785. const duk_uint8_t *src_data;
  19786. thr = (duk_hthread *) ctx;
  19787. DUK_UNREF(thr);
  19788. /*
  19789. * Constructor arguments are currently somewhat compatible with
  19790. * (keep it that way if possible):
  19791. *
  19792. * http://nodejs.org/api/buffer.html
  19793. *
  19794. * Note that the ToBuffer() coercion (duk_to_buffer()) does NOT match
  19795. * the constructor behavior.
  19796. */
  19797. buf_dynamic = duk_get_boolean(ctx, 1); /* default to false */
  19798. switch (duk_get_type(ctx, 0)) {
  19799. case DUK_TYPE_NUMBER: {
  19800. /* new buffer of specified size */
  19801. buf_size = (duk_size_t) duk_to_int(ctx, 0);
  19802. (void) duk_push_buffer(ctx, buf_size, buf_dynamic);
  19803. break;
  19804. }
  19805. case DUK_TYPE_BUFFER: {
  19806. /* return input buffer, converted to a Duktape.Buffer object
  19807. * if called as a constructor (no change if called as a
  19808. * function).
  19809. */
  19810. duk_set_top(ctx, 1);
  19811. break;
  19812. }
  19813. case DUK_TYPE_STRING: {
  19814. /* new buffer with string contents */
  19815. src_data = (const duk_uint8_t *) duk_get_lstring(ctx, 0, &buf_size);
  19816. DUK_ASSERT(src_data != NULL); /* even for zero-length string */
  19817. buf_data = (duk_uint8_t *) duk_push_buffer(ctx, buf_size, buf_dynamic);
  19818. DUK_MEMCPY((void *) buf_data, (const void *) src_data, (size_t) buf_size);
  19819. break;
  19820. }
  19821. case DUK_TYPE_OBJECT: {
  19822. /* For all duk_hbufferobjects, get the plain buffer inside
  19823. * without making a copy. This is compatible with Duktape 1.2
  19824. * but means that a slice/view information is ignored and the
  19825. * full underlying buffer is returned.
  19826. *
  19827. * If called as a constructor, a new Duktape.Buffer object
  19828. * pointing to the same plain buffer is created below.
  19829. */
  19830. duk_hbufferobject *h_bufobj;
  19831. h_bufobj = (duk_hbufferobject *) duk_get_hobject(ctx, 0);
  19832. DUK_ASSERT(h_bufobj != NULL);
  19833. if (!DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) h_bufobj)) {
  19834. return DUK_RET_TYPE_ERROR;
  19835. }
  19836. if (h_bufobj->buf == NULL) {
  19837. return DUK_RET_TYPE_ERROR;
  19838. }
  19839. duk_push_hbuffer(ctx, h_bufobj->buf);
  19840. break;
  19841. }
  19842. case DUK_TYPE_NONE:
  19843. default: {
  19844. return DUK_RET_TYPE_ERROR;
  19845. }
  19846. }
  19847. DUK_ASSERT(duk_is_buffer(ctx, -1));
  19848. /* stack is unbalanced, but: [ <something> buf ] */
  19849. if (duk_is_constructor_call(ctx)) {
  19850. duk_hbufferobject *h_bufobj;
  19851. duk_hbuffer *h_val;
  19852. h_val = duk_get_hbuffer(ctx, -1);
  19853. DUK_ASSERT(h_val != NULL);
  19854. h_bufobj = duk_push_bufferobject(ctx,
  19855. DUK_HOBJECT_FLAG_EXTENSIBLE |
  19856. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  19857. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER),
  19858. DUK_BIDX_BUFFER_PROTOTYPE);
  19859. DUK_ASSERT(h_bufobj != NULL);
  19860. duk__set_bufobj_buffer(ctx, h_bufobj, h_val);
  19861. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  19862. }
  19863. /* Note: unbalanced stack on purpose */
  19864. return 1;
  19865. }
  19866. /*
  19867. * Node.js Buffer: constructor
  19868. */
  19869. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_constructor(duk_context *ctx) {
  19870. /* Internal class is Object: Object.prototype.toString.call(new Buffer(0))
  19871. * prints "[object Object]".
  19872. */
  19873. duk_int_t len;
  19874. duk_int_t i;
  19875. duk_uint8_t *buf;
  19876. duk_hbuffer *h_buf;
  19877. duk_hbufferobject *h_bufobj;
  19878. duk_size_t buf_size;
  19879. switch (duk_get_type(ctx, 0)) {
  19880. case DUK_TYPE_BUFFER: {
  19881. /* Custom behavior: plain buffer is used as internal buffer
  19882. * without making a copy (matches Duktape.Buffer).
  19883. */
  19884. duk_set_top(ctx, 1); /* -> [ buffer ] */
  19885. break;
  19886. }
  19887. case DUK_TYPE_NUMBER: {
  19888. len = duk_to_int_clamped(ctx, 0, 0, DUK_INT_MAX);
  19889. buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) len);
  19890. break;
  19891. }
  19892. case DUK_TYPE_OBJECT: {
  19893. (void) duk_get_prop_string(ctx, 0, "length");
  19894. len = duk_to_int_clamped(ctx, -1, 0, DUK_INT_MAX);
  19895. duk_pop(ctx);
  19896. buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) len);
  19897. for (i = 0; i < len; i++) {
  19898. /* XXX: fast path for array arguments? */
  19899. duk_get_prop_index(ctx, 0, (duk_uarridx_t) i);
  19900. buf[i] = (duk_uint8_t) (duk_to_uint32(ctx, -1) & 0xffU);
  19901. duk_pop(ctx);
  19902. }
  19903. break;
  19904. }
  19905. case DUK_TYPE_STRING: {
  19906. /* ignore encoding for now */
  19907. duk_dup(ctx, 0);
  19908. buf = (duk_uint8_t *) duk_to_buffer(ctx, -1, &buf_size);
  19909. break;
  19910. }
  19911. default:
  19912. return DUK_RET_TYPE_ERROR;
  19913. }
  19914. DUK_ASSERT(duk_is_buffer(ctx, -1));
  19915. h_buf = duk_get_hbuffer(ctx, -1);
  19916. DUK_ASSERT(h_buf != NULL);
  19917. h_bufobj = duk_push_bufferobject(ctx,
  19918. DUK_HOBJECT_FLAG_EXTENSIBLE |
  19919. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  19920. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER),
  19921. DUK_BIDX_NODEJS_BUFFER_PROTOTYPE);
  19922. DUK_ASSERT(h_bufobj != NULL);
  19923. h_bufobj->buf = h_buf;
  19924. DUK_HBUFFER_INCREF(thr, h_buf);
  19925. DUK_ASSERT(h_bufobj->offset == 0);
  19926. h_bufobj->length = (duk_int_t) DUK_HBUFFER_GET_SIZE(h_buf);
  19927. DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8);
  19928. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  19929. return 1;
  19930. }
  19931. /*
  19932. * ArrayBuffer, DataView, and TypedArray constructors
  19933. */
  19934. DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_constructor(duk_context *ctx) {
  19935. duk_hbufferobject *h_bufobj;
  19936. duk_hbuffer *h_val;
  19937. /* XXX: function flag to make this automatic? */
  19938. if (!duk_is_constructor_call(ctx)) {
  19939. return DUK_RET_TYPE_ERROR;
  19940. }
  19941. if (duk_is_buffer(ctx, 0)) {
  19942. /* Custom behavior: plain buffer is used as internal buffer
  19943. * without making a copy (matches Duktape.Buffer).
  19944. */
  19945. h_val = duk_get_hbuffer(ctx, 0);
  19946. DUK_ASSERT(h_val != NULL);
  19947. /* XXX: accept any duk_hbufferobject type as an input also? */
  19948. } else {
  19949. duk_int_t len;
  19950. len = duk_to_int(ctx, 0);
  19951. if (len < 0) {
  19952. goto fail_length;
  19953. }
  19954. (void) duk_push_fixed_buffer(ctx, (duk_size_t) len);
  19955. h_val = (duk_hbuffer *) duk_get_hbuffer(ctx, -1);
  19956. DUK_ASSERT(h_val != NULL);
  19957. }
  19958. h_bufobj = duk_push_bufferobject(ctx,
  19959. DUK_HOBJECT_FLAG_EXTENSIBLE |
  19960. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  19961. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAYBUFFER),
  19962. DUK_BIDX_ARRAYBUFFER_PROTOTYPE);
  19963. DUK_ASSERT(h_bufobj != NULL);
  19964. duk__set_bufobj_buffer(ctx, h_bufobj, h_val);
  19965. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  19966. return 1;
  19967. fail_length:
  19968. return DUK_RET_RANGE_ERROR;
  19969. }
  19970. /* Format of magic, bits:
  19971. * 0...1: elem size shift (0-3)
  19972. * 2...5: elem type (DUK_HBUFFEROBJECT_ELEM_xxx)
  19973. */
  19974. DUK_INTERNAL duk_ret_t duk_bi_typedarray_constructor(duk_context *ctx) {
  19975. duk_hthread *thr;
  19976. duk_tval *tv;
  19977. duk_hobject *h_obj;
  19978. duk_hbufferobject *h_bufobj = NULL;
  19979. duk_hbufferobject *h_bufarr = NULL;
  19980. duk_hbufferobject *h_bufarg = NULL;
  19981. duk_hbuffer *h_val;
  19982. duk_small_uint_t magic;
  19983. duk_small_uint_t shift;
  19984. duk_small_uint_t elem_type;
  19985. duk_small_uint_t elem_size;
  19986. duk_small_uint_t class_num;
  19987. duk_small_uint_t proto_bidx;
  19988. duk_uint_t align_mask;
  19989. duk_uint_t elem_length;
  19990. duk_int_t elem_length_signed;
  19991. duk_uint_t byte_length;
  19992. duk_small_uint_t copy_mode;
  19993. thr = (duk_hthread *) ctx;
  19994. DUK_UNREF(thr);
  19995. /* XXX: function flag to make this automatic? */
  19996. if (!duk_is_constructor_call(ctx)) {
  19997. return DUK_RET_TYPE_ERROR;
  19998. }
  19999. /* We could fit built-in index into magic but that'd make the magic
  20000. * number dependent on built-in numbering (genbuiltins.py doesn't
  20001. * handle that yet). So map both class and prototype from the
  20002. * element type.
  20003. */
  20004. magic = duk_get_current_magic(ctx);
  20005. shift = magic & 0x03; /* bits 0...1: shift */
  20006. elem_type = (magic >> 2) & 0x0f; /* bits 2...5: type */
  20007. elem_size = 1 << shift;
  20008. align_mask = elem_size - 1;
  20009. DUK_ASSERT(elem_type < sizeof(duk__buffer_proto_from_elemtype) / sizeof(duk_uint8_t));
  20010. proto_bidx = duk__buffer_proto_from_elemtype[elem_type];
  20011. DUK_ASSERT(proto_bidx < DUK_NUM_BUILTINS);
  20012. DUK_ASSERT(elem_type < sizeof(duk__buffer_class_from_elemtype) / sizeof(duk_uint8_t));
  20013. class_num = duk__buffer_class_from_elemtype[elem_type];
  20014. DUK_DD(DUK_DDPRINT("typedarray constructor, magic=%d, shift=%d, elem_type=%d, "
  20015. "elem_size=%d, proto_bidx=%d, class_num=%d",
  20016. (int) magic, (int) shift, (int) elem_type, (int) elem_size,
  20017. (int) proto_bidx, (int) class_num));
  20018. /* Argument variants. When the argument is an ArrayBuffer a view to
  20019. * the same buffer is created; otherwise a new ArrayBuffer is always
  20020. * created.
  20021. */
  20022. tv = duk_get_tval(ctx, 0);
  20023. DUK_ASSERT(tv != NULL); /* arg count */
  20024. if (DUK_TVAL_IS_OBJECT(tv)) {
  20025. h_obj = DUK_TVAL_GET_OBJECT(tv);
  20026. DUK_ASSERT(h_obj != NULL);
  20027. if (DUK_HOBJECT_GET_CLASS_NUMBER(h_obj) == DUK_HOBJECT_CLASS_ARRAYBUFFER) {
  20028. /* ArrayBuffer: unlike any other argument variant, create
  20029. * a view into the existing buffer.
  20030. */
  20031. duk_int_t byte_offset_signed;
  20032. duk_uint_t byte_offset;
  20033. h_bufarg = (duk_hbufferobject *) h_obj;
  20034. byte_offset_signed = duk_to_int(ctx, 1);
  20035. if (byte_offset_signed < 0) {
  20036. goto fail_arguments;
  20037. }
  20038. byte_offset = (duk_uint_t) byte_offset_signed;
  20039. if (byte_offset > h_bufarg->length ||
  20040. (byte_offset & align_mask) != 0) {
  20041. /* Must be >= 0 and multiple of element size. */
  20042. goto fail_arguments;
  20043. }
  20044. if (duk_is_undefined(ctx, 2)) {
  20045. DUK_ASSERT(h_bufarg->length >= byte_offset);
  20046. byte_length = h_bufarg->length - byte_offset;
  20047. if ((byte_length & align_mask) != 0) {
  20048. /* Must be element size multiple from
  20049. * start offset to end of buffer.
  20050. */
  20051. goto fail_arguments;
  20052. }
  20053. elem_length = (byte_length >> shift);
  20054. } else {
  20055. elem_length_signed = duk_to_int(ctx, 2);
  20056. if (elem_length_signed < 0) {
  20057. goto fail_arguments;
  20058. }
  20059. elem_length = (duk_uint_t) elem_length_signed;
  20060. byte_length = elem_length << shift;
  20061. if ((byte_length >> shift) != elem_length) {
  20062. /* Byte length would overflow. */
  20063. /* XXX: easier check with less code? */
  20064. goto fail_arguments;
  20065. }
  20066. DUK_ASSERT(h_bufarg->length >= byte_offset);
  20067. if (byte_length > h_bufarg->length - byte_offset) {
  20068. /* Not enough data. */
  20069. goto fail_arguments;
  20070. }
  20071. }
  20072. DUK_ASSERT_DISABLE(byte_offset >= 0);
  20073. DUK_ASSERT(byte_offset <= h_bufarg->length);
  20074. DUK_ASSERT_DISABLE(byte_length >= 0);
  20075. DUK_ASSERT(byte_offset + byte_length <= h_bufarg->length);
  20076. DUK_ASSERT((elem_length << shift) == byte_length);
  20077. h_bufobj = duk_push_bufferobject(ctx,
  20078. DUK_HOBJECT_FLAG_EXTENSIBLE |
  20079. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  20080. DUK_HOBJECT_CLASS_AS_FLAGS(class_num),
  20081. proto_bidx);
  20082. h_val = h_bufarg->buf;
  20083. if (h_val == NULL) {
  20084. return DUK_RET_TYPE_ERROR;
  20085. }
  20086. h_bufobj->buf = h_val;
  20087. DUK_HBUFFER_INCREF(thr, h_val);
  20088. h_bufobj->offset = h_bufarg->offset + byte_offset;
  20089. h_bufobj->length = byte_length;
  20090. h_bufobj->shift = shift;
  20091. h_bufobj->elem_type = elem_type;
  20092. h_bufobj->is_view = 1;
  20093. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  20094. /* Set .buffer to the argument ArrayBuffer. */
  20095. duk_dup(ctx, 0);
  20096. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE);
  20097. duk_compact(ctx, -1);
  20098. return 1;
  20099. } else if (DUK_HOBJECT_IS_BUFFEROBJECT(h_obj)) {
  20100. /* TypedArray (or other non-ArrayBuffer duk_hbufferobject).
  20101. * Conceptually same behavior as for an Array-like argument,
  20102. * with a few fast paths.
  20103. */
  20104. h_bufarg = (duk_hbufferobject *) h_obj;
  20105. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufarg);
  20106. elem_length_signed = (duk_int_t) (h_bufarg->length >> h_bufarg->shift);
  20107. if (h_bufarg->buf == NULL) {
  20108. return DUK_RET_TYPE_ERROR;
  20109. }
  20110. /* Select copy mode. Must take into account element
  20111. * compatibility and validity of the underlying source
  20112. * buffer.
  20113. */
  20114. DUK_DDD(DUK_DDDPRINT("selecting copy mode for bufobj arg, "
  20115. "src byte_length=%ld, src shift=%d, "
  20116. "src/dst elem_length=%ld; "
  20117. "dst shift=%d -> dst byte_length=%ld",
  20118. (long) h_bufarg->length, (int) h_bufarg->shift,
  20119. (long) elem_length_signed, (int) shift,
  20120. (long) (elem_length_signed << shift)));
  20121. copy_mode = 2; /* default is explicit index read/write copy */
  20122. DUK_ASSERT(elem_type < sizeof(duk__buffer_elemtype_copy_compatible) / sizeof(duk_uint16_t));
  20123. if (DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg)) {
  20124. if ((duk__buffer_elemtype_copy_compatible[elem_type] & (1 << h_bufarg->elem_type)) != 0) {
  20125. DUK_DDD(DUK_DDDPRINT("source/target are copy compatible, memcpy"));
  20126. DUK_ASSERT(shift == h_bufarg->shift); /* byte sizes will match */
  20127. copy_mode = 0;
  20128. } else {
  20129. DUK_DDD(DUK_DDDPRINT("source/target not copy compatible but valid, fast copy"));
  20130. copy_mode = 1;
  20131. }
  20132. }
  20133. } else {
  20134. /* Array or Array-like */
  20135. elem_length_signed = (duk_int_t) duk_get_length(ctx, 0);
  20136. copy_mode = 2;
  20137. }
  20138. } else {
  20139. /* Non-object argument is simply int coerced, matches
  20140. * V8 behavior (except for "null", which we coerce to
  20141. * 0 but V8 TypeErrors).
  20142. */
  20143. elem_length_signed = duk_to_int(ctx, 0);
  20144. copy_mode = 3;
  20145. }
  20146. if (elem_length_signed < 0) {
  20147. goto fail_arguments;
  20148. }
  20149. elem_length = (duk_uint_t) elem_length_signed;
  20150. byte_length = (duk_uint_t) (elem_length << shift);
  20151. if ((byte_length >> shift) != elem_length) {
  20152. /* Byte length would overflow. */
  20153. /* XXX: easier check with less code? */
  20154. goto fail_arguments;
  20155. }
  20156. DUK_DDD(DUK_DDDPRINT("elem_length=%ld, byte_length=%ld",
  20157. (long) elem_length, (long) byte_length));
  20158. /* ArrayBuffer argument is handled specially above; the rest of the
  20159. * argument variants are handled by shared code below.
  20160. */
  20161. /* Push a new ArrayBuffer (becomes view .buffer) */
  20162. h_bufarr = duk__push_arraybuffer_with_length(ctx, byte_length);
  20163. DUK_ASSERT(h_bufarr != NULL);
  20164. h_val = h_bufarr->buf;
  20165. DUK_ASSERT(h_val != NULL);
  20166. /* Push the resulting view object and attach the ArrayBuffer. */
  20167. h_bufobj = duk_push_bufferobject(ctx,
  20168. DUK_HOBJECT_FLAG_EXTENSIBLE |
  20169. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  20170. DUK_HOBJECT_CLASS_AS_FLAGS(class_num),
  20171. proto_bidx);
  20172. h_bufobj->buf = h_val;
  20173. DUK_HBUFFER_INCREF(thr, h_val);
  20174. DUK_ASSERT(h_bufobj->offset == 0);
  20175. h_bufobj->length = byte_length;
  20176. h_bufobj->shift = shift;
  20177. h_bufobj->elem_type = elem_type;
  20178. h_bufobj->is_view = 1;
  20179. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  20180. /* Set .buffer */
  20181. duk_dup(ctx, -2);
  20182. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE);
  20183. duk_compact(ctx, -1);
  20184. /* Copy values, the copy method depends on the arguments.
  20185. *
  20186. * Copy mode decision may depend on the validity of the underlying
  20187. * buffer of the source argument; there must be no harmful side effects
  20188. * from there to here for copy_mode to still be valid.
  20189. */
  20190. DUK_DDD(DUK_DDDPRINT("copy mode: %d", (int) copy_mode));
  20191. switch (copy_mode) {
  20192. case 0: {
  20193. /* Use byte copy. */
  20194. duk_uint8_t *p_src;
  20195. duk_uint8_t *p_dst;
  20196. DUK_ASSERT(h_bufobj != NULL);
  20197. DUK_ASSERT(h_bufobj->buf != NULL);
  20198. DUK_ASSERT(DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj));
  20199. DUK_ASSERT(h_bufarg != NULL);
  20200. DUK_ASSERT(h_bufarg->buf != NULL);
  20201. DUK_ASSERT(DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg));
  20202. p_dst = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufobj);
  20203. p_src = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufarg);
  20204. DUK_DDD(DUK_DDDPRINT("using memcpy: p_src=%p, p_dst=%p, byte_length=%ld",
  20205. (void *) p_src, (void *) p_dst, (long) byte_length));
  20206. DUK_MEMCPY((void *) p_dst, (const void *) p_src, (size_t) byte_length);
  20207. break;
  20208. }
  20209. case 1: {
  20210. /* Copy values through direct validated reads and writes. */
  20211. duk_small_uint_t src_elem_size;
  20212. duk_small_uint_t dst_elem_size;
  20213. duk_uint8_t *p_src;
  20214. duk_uint8_t *p_src_end;
  20215. duk_uint8_t *p_dst;
  20216. DUK_ASSERT(h_bufobj != NULL);
  20217. DUK_ASSERT(h_bufobj->buf != NULL);
  20218. DUK_ASSERT(DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj));
  20219. DUK_ASSERT(h_bufarg != NULL);
  20220. DUK_ASSERT(h_bufarg->buf != NULL);
  20221. DUK_ASSERT(DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg));
  20222. src_elem_size = 1 << h_bufarg->shift;
  20223. dst_elem_size = elem_size;
  20224. p_src = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufarg);
  20225. p_dst = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufobj);
  20226. p_src_end = p_src + h_bufarg->length;
  20227. DUK_DDD(DUK_DDDPRINT("using fast copy: p_src=%p, p_src_end=%p, p_dst=%p, "
  20228. "src_elem_size=%d, dst_elem_size=%d",
  20229. (void *) p_src, (void *) p_src_end, (void *) p_dst,
  20230. (int) src_elem_size, (int) dst_elem_size));
  20231. while (p_src != p_src_end) {
  20232. DUK_DDD(DUK_DDDPRINT("fast path per element copy loop: "
  20233. "p_src=%p, p_src_end=%p, p_dst=%p",
  20234. (void *) p_src, (void *) p_src_end, (void *) p_dst));
  20235. /* A validated read() is always a number, so it's write coercion
  20236. * is always side effect free an won't invalidate pointers etc.
  20237. */
  20238. duk_hbufferobject_push_validated_read(ctx, h_bufarg, p_src, src_elem_size);
  20239. duk_hbufferobject_validated_write(ctx, h_bufobj, p_dst, dst_elem_size);
  20240. duk_pop(ctx);
  20241. p_src += src_elem_size;
  20242. p_dst += dst_elem_size;
  20243. }
  20244. break;
  20245. }
  20246. case 2: {
  20247. /* Copy values by index reads and writes. Let virtual
  20248. * property handling take care of coercion.
  20249. */
  20250. duk_uint_t i;
  20251. DUK_DDD(DUK_DDDPRINT("using slow copy"));
  20252. for (i = 0; i < elem_length; i++) {
  20253. duk_get_prop_index(ctx, 0, (duk_uarridx_t) i);
  20254. duk_put_prop_index(ctx, -2, (duk_uarridx_t) i);
  20255. }
  20256. break;
  20257. }
  20258. default:
  20259. case 3: {
  20260. /* No copy, leave zero bytes in the buffer. There's no
  20261. * ambiguity with Float32/Float64 because zero bytes also
  20262. * represent 0.0.
  20263. */
  20264. DUK_DDD(DUK_DDDPRINT("using no copy"));
  20265. break;
  20266. }
  20267. }
  20268. return 1;
  20269. fail_arguments:
  20270. return DUK_RET_RANGE_ERROR;
  20271. }
  20272. DUK_INTERNAL duk_ret_t duk_bi_dataview_constructor(duk_context *ctx) {
  20273. duk_hbufferobject *h_bufarg;
  20274. duk_hbufferobject *h_bufobj;
  20275. duk_hbuffer *h_val;
  20276. duk_uint_t offset;
  20277. duk_uint_t length;
  20278. /* XXX: function flag to make this automatic? */
  20279. if (!duk_is_constructor_call(ctx)) {
  20280. return DUK_RET_TYPE_ERROR;
  20281. }
  20282. h_bufarg = duk__require_bufobj_value(ctx, 0);
  20283. DUK_ASSERT(h_bufarg != NULL);
  20284. duk__resolve_offset_opt_length(ctx, h_bufarg, 1, 2, &offset, &length, 1 /*throw_flag*/);
  20285. DUK_ASSERT(offset <= h_bufarg->length);
  20286. DUK_ASSERT(offset + length <= h_bufarg->length);
  20287. h_bufobj = duk_push_bufferobject(ctx,
  20288. DUK_HOBJECT_FLAG_EXTENSIBLE |
  20289. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  20290. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DATAVIEW),
  20291. DUK_BIDX_DATAVIEW_PROTOTYPE);
  20292. h_val = h_bufarg->buf;
  20293. if (h_val == NULL) {
  20294. return DUK_RET_TYPE_ERROR;
  20295. }
  20296. h_bufobj->buf = h_val;
  20297. DUK_HBUFFER_INCREF(thr, h_val);
  20298. h_bufobj->offset = h_bufarg->offset + offset;
  20299. h_bufobj->length = length;
  20300. DUK_ASSERT(h_bufobj->shift == 0);
  20301. DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8);
  20302. h_bufobj->is_view = 1;
  20303. /* The DataView .buffer property is ordinarily set to the argument
  20304. * which is an ArrayBuffer. We accept any duk_hbufferobject as
  20305. * an argument and .buffer will be set to the argument regardless
  20306. * of what it is. This may be a bit confusing if the argument
  20307. * is e.g. a DataView or another TypedArray view.
  20308. *
  20309. * XXX: Copy .buffer property from a DataView/TypedArray argument?
  20310. * Create a fresh ArrayBuffer for Duktape.Buffer and Node.js Buffer
  20311. * arguments? See: test-bug-dataview-buffer-prop.js.
  20312. */
  20313. duk_dup(ctx, 0);
  20314. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE);
  20315. duk_compact(ctx, -1);
  20316. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  20317. return 1;
  20318. }
  20319. /*
  20320. * ArrayBuffer.isView()
  20321. */
  20322. DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_isview(duk_context *ctx) {
  20323. duk_hobject *h_obj;
  20324. duk_bool_t ret = 0;
  20325. h_obj = duk_get_hobject(ctx, 0);
  20326. if (h_obj != NULL && DUK_HOBJECT_IS_BUFFEROBJECT(h_obj)) {
  20327. ret = ((duk_hbufferobject *) h_obj)->is_view;
  20328. }
  20329. duk_push_boolean(ctx, ret);
  20330. return 1;
  20331. }
  20332. /*
  20333. * Node.js Buffer: toString([encoding], [start], [end])
  20334. */
  20335. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_context *ctx) {
  20336. duk_hthread *thr;
  20337. duk_hbufferobject *h_this;
  20338. duk_int_t start_offset, end_offset;
  20339. duk_uint8_t *buf_slice;
  20340. duk_size_t slice_length;
  20341. thr = (duk_hthread *) ctx;
  20342. DUK_UNREF(thr);
  20343. h_this = duk__get_bufobj_this(ctx);
  20344. if (h_this == NULL) {
  20345. /* XXX: happens e.g. when evaluating: String(Buffer.prototype). */
  20346. duk_push_string(ctx, "[object Object]");
  20347. return 1;
  20348. }
  20349. DUK_ASSERT_HBUFFEROBJECT_VALID(h_this);
  20350. /* ignore encoding for now */
  20351. duk__clamp_startend_nonegidx_noshift(ctx, h_this, 1 /*idx_start*/, 2 /*idx_end*/, &start_offset, &end_offset);
  20352. slice_length = (duk_size_t) (end_offset - start_offset);
  20353. buf_slice = (duk_uint8_t *) duk_push_fixed_buffer(ctx, slice_length);
  20354. DUK_ASSERT(buf_slice != NULL);
  20355. if (h_this->buf == NULL) {
  20356. goto type_error;
  20357. }
  20358. if (DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_this, start_offset + slice_length)) {
  20359. DUK_MEMCPY((void *) buf_slice,
  20360. (const void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + start_offset),
  20361. slice_length);
  20362. } else {
  20363. /* not covered, return all zeroes */
  20364. ;
  20365. }
  20366. duk_to_string(ctx, -1);
  20367. return 1;
  20368. type_error:
  20369. return DUK_RET_TYPE_ERROR;
  20370. }
  20371. /*
  20372. * Duktape.Buffer: toString(), valueOf()
  20373. */
  20374. DUK_INTERNAL duk_ret_t duk_bi_buffer_prototype_tostring_shared(duk_context *ctx) {
  20375. duk_hthread *thr;
  20376. duk_tval *tv;
  20377. duk_small_int_t to_string = duk_get_current_magic(ctx);
  20378. thr = (duk_hthread *) ctx;
  20379. DUK_UNREF(thr);
  20380. tv = duk_get_borrowed_this_tval(ctx);
  20381. DUK_ASSERT(tv != NULL);
  20382. if (DUK_TVAL_IS_BUFFER(tv)) {
  20383. duk_hbuffer *h_buf;
  20384. h_buf = DUK_TVAL_GET_BUFFER(tv);
  20385. DUK_ASSERT(h_buf != NULL);
  20386. duk_push_hbuffer(ctx, h_buf);
  20387. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  20388. duk_hobject *h;
  20389. duk_hbufferobject *h_bufobj;
  20390. /* Accept any duk_hbufferobject, though we're only normally
  20391. * called for Duktape.Buffer values.
  20392. */
  20393. h = DUK_TVAL_GET_OBJECT(tv);
  20394. DUK_ASSERT(h != NULL);
  20395. if (!DUK_HOBJECT_IS_BUFFEROBJECT(h)) {
  20396. DUK_DD(DUK_DDPRINT("toString/valueOf() called for a non-bufferobject object"));
  20397. goto type_error;
  20398. }
  20399. h_bufobj = (duk_hbufferobject *) h;
  20400. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  20401. if (h_bufobj->buf == NULL) {
  20402. DUK_DD(DUK_DDPRINT("toString/valueOf() called for a bufferobject with NULL buf"));
  20403. goto type_error;
  20404. }
  20405. duk_push_hbuffer(ctx, h_bufobj->buf);
  20406. } else {
  20407. goto type_error;
  20408. }
  20409. if (to_string) {
  20410. duk_to_string(ctx, -1);
  20411. }
  20412. return 1;
  20413. type_error:
  20414. return DUK_RET_TYPE_ERROR;
  20415. }
  20416. /*
  20417. * Node.js Buffer.prototype: toJSON()
  20418. */
  20419. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tojson(duk_context *ctx) {
  20420. duk_hthread *thr;
  20421. duk_hbufferobject *h_this;
  20422. duk_uint8_t *buf;
  20423. duk_uint_t i;
  20424. thr = (duk_hthread *) ctx;
  20425. DUK_UNREF(thr);
  20426. h_this = duk__require_bufobj_this(ctx);
  20427. DUK_ASSERT(h_this != NULL);
  20428. if (h_this->buf == NULL || !DUK_HBUFFEROBJECT_VALID_SLICE(h_this)) {
  20429. /* Serialize uncovered backing buffer as a null; doesn't
  20430. * really matter as long we're memory safe.
  20431. */
  20432. duk_push_null(ctx);
  20433. return 1;
  20434. }
  20435. duk_push_object(ctx);
  20436. duk_push_hstring_stridx(ctx, DUK_STRIDX_UC_BUFFER);
  20437. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_TYPE);
  20438. duk_push_array(ctx);
  20439. for (i = 0; i < h_this->length; i++) {
  20440. /* XXX: regetting the pointer may be overkill - we're writing
  20441. * to a side-effect free array here.
  20442. */
  20443. DUK_ASSERT(h_this->buf != NULL);
  20444. buf = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this);
  20445. duk_push_uint(ctx, (duk_uint_t) buf[i]);
  20446. duk_put_prop_index(ctx, -2, (duk_idx_t) i);
  20447. }
  20448. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_DATA);
  20449. return 1;
  20450. }
  20451. /*
  20452. * Node.js Buffer.prototype.equals()
  20453. * Node.js Buffer.prototype.compare()
  20454. * Node.js Buffer.compare()
  20455. */
  20456. DUK_INTERNAL duk_ret_t duk_bi_buffer_compare_shared(duk_context *ctx) {
  20457. duk_hthread *thr;
  20458. duk_small_uint_t magic;
  20459. duk_hbufferobject *h_bufarg1;
  20460. duk_hbufferobject *h_bufarg2;
  20461. duk_small_int_t comp_res;
  20462. thr = (duk_hthread *) ctx;
  20463. DUK_UNREF(thr);
  20464. magic = duk_get_current_magic(ctx);
  20465. if (magic & 0x02) {
  20466. /* Static call style. */
  20467. h_bufarg1 = duk__require_bufobj_value(ctx, 0);
  20468. h_bufarg2 = duk__require_bufobj_value(ctx, 1);
  20469. } else {
  20470. h_bufarg1 = duk__require_bufobj_this(ctx);
  20471. h_bufarg2 = duk__require_bufobj_value(ctx, 0);
  20472. }
  20473. DUK_ASSERT(h_bufarg1 != NULL);
  20474. DUK_ASSERT(h_bufarg2 != NULL);
  20475. /* We want to compare the slice/view areas of the arguments.
  20476. * If either slice/view is invalid (underlying buffer is shorter)
  20477. * ensure equals() is false, but otherwise the only thing that
  20478. * matters is to be memory safe.
  20479. */
  20480. if (DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg1) &&
  20481. DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg2)) {
  20482. comp_res = duk_js_data_compare((const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufarg1->buf) + h_bufarg1->offset,
  20483. (const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufarg2->buf) + h_bufarg2->offset,
  20484. (duk_size_t) h_bufarg1->length,
  20485. (duk_size_t) h_bufarg2->length);
  20486. } else {
  20487. comp_res = -1; /* either nonzero value is ok */
  20488. }
  20489. if (magic & 0x01) {
  20490. /* compare: similar to string comparison but for buffer data. */
  20491. duk_push_int(ctx, comp_res);
  20492. } else {
  20493. /* equals */
  20494. duk_push_boolean(ctx, (comp_res == 0));
  20495. }
  20496. return 1;
  20497. }
  20498. /*
  20499. * Node.js Buffer.prototype.fill()
  20500. */
  20501. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_fill(duk_context *ctx) {
  20502. duk_hthread *thr;
  20503. duk_hbufferobject *h_this;
  20504. const duk_uint8_t *fill_str_ptr;
  20505. duk_size_t fill_str_len;
  20506. duk_uint8_t fill_value;
  20507. duk_int_t fill_offset;
  20508. duk_int_t fill_end;
  20509. duk_size_t fill_length;
  20510. duk_uint8_t *p;
  20511. thr = (duk_hthread *) ctx;
  20512. DUK_UNREF(thr);
  20513. h_this = duk__require_bufobj_this(ctx);
  20514. DUK_ASSERT(h_this != NULL);
  20515. if (h_this->buf == NULL) {
  20516. return DUK_RET_TYPE_ERROR;
  20517. }
  20518. /* [ value offset end ] */
  20519. if (duk_is_string(ctx, 0)) {
  20520. fill_str_ptr = (const duk_uint8_t *) duk_get_lstring(ctx, 0, &fill_str_len);
  20521. DUK_ASSERT(fill_str_ptr != NULL);
  20522. } else {
  20523. fill_value = (duk_uint8_t) duk_to_uint32(ctx, 0);
  20524. fill_str_ptr = (const duk_uint8_t *) &fill_value;
  20525. fill_str_len = 1;
  20526. }
  20527. /* Fill offset handling is more lenient than in Node.js. */
  20528. duk__clamp_startend_nonegidx_noshift(ctx, h_this, 1 /*idx_start*/, 2 /*idx_end*/, &fill_offset, &fill_end);
  20529. DUK_DDD(DUK_DDDPRINT("fill: fill_value=%02x, fill_offset=%ld, fill_end=%ld, view length=%ld",
  20530. (unsigned int) fill_value, (long) fill_offset, (long) fill_end, (long) h_this->length));
  20531. DUK_ASSERT(fill_end - fill_offset >= 0);
  20532. DUK_ASSERT(h_this->buf != NULL);
  20533. p = (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + fill_offset);
  20534. fill_length = (duk_size_t) (fill_end - fill_offset);
  20535. if (fill_str_len == 1) {
  20536. /* Handle single character fills as memset() even when
  20537. * the fill data comes from a one-char argument.
  20538. */
  20539. DUK_MEMSET((void *) p, (int) fill_str_ptr[0], (size_t) fill_length);
  20540. } else if (fill_str_len > 1) {
  20541. duk_size_t i, n, t;
  20542. for (i = 0, n = (fill_end - fill_offset), t = 0; i < n; i++) {
  20543. p[i] = fill_str_ptr[t++];
  20544. if (t >= fill_str_len) {
  20545. t = 0;
  20546. }
  20547. }
  20548. } else {
  20549. DUK_DDD(DUK_DDDPRINT("zero size fill pattern, ignore silently"));
  20550. }
  20551. /* Return the Buffer to allow chaining: b.fill(0x11).fill(0x22, 3, 5).toString() */
  20552. duk_push_this(ctx);
  20553. return 1;
  20554. }
  20555. /*
  20556. * Node.js Buffer.prototype.write(string, [offset], [length], [encoding])
  20557. */
  20558. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_write(duk_context *ctx) {
  20559. duk_hthread *thr;
  20560. duk_hbufferobject *h_this;
  20561. duk_uint_t offset;
  20562. duk_uint_t length;
  20563. const duk_uint8_t *str_data;
  20564. duk_size_t str_len;
  20565. thr = (duk_hthread *) ctx;
  20566. DUK_UNREF(thr);
  20567. h_this = duk__require_bufobj_this(ctx);
  20568. DUK_ASSERT(h_this != NULL);
  20569. /* Argument must be a string, e.g. a buffer is not allowed. */
  20570. str_data = (const duk_uint8_t *) duk_require_lstring(ctx, 0, &str_len);
  20571. duk__resolve_offset_opt_length(ctx, h_this, 1, 2, &offset, &length, 0 /*throw_flag*/);
  20572. DUK_ASSERT(offset <= h_this->length);
  20573. DUK_ASSERT(offset + length <= h_this->length);
  20574. /* XXX: encoding is ignored now. */
  20575. if (length > str_len) {
  20576. length = str_len;
  20577. }
  20578. if (DUK_HBUFFEROBJECT_VALID_SLICE(h_this)) {
  20579. /* Cannot overlap. */
  20580. DUK_MEMCPY((void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + offset),
  20581. (const void *) str_data,
  20582. (size_t) length);
  20583. } else {
  20584. DUK_DDD(DUK_DDDPRINT("write() target buffer is not covered, silent ignore"));
  20585. }
  20586. duk_push_uint(ctx, length);
  20587. return 1;
  20588. }
  20589. /*
  20590. * Node.js Buffer.prototype.copy()
  20591. */
  20592. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_copy(duk_context *ctx) {
  20593. duk_hthread *thr;
  20594. duk_hbufferobject *h_this;
  20595. duk_hbufferobject *h_bufarg;
  20596. duk_int_t source_length;
  20597. duk_int_t target_length;
  20598. duk_int_t target_start, source_start, source_end;
  20599. duk_uint_t target_ustart, source_ustart, source_uend;
  20600. duk_uint_t copy_size = 0;
  20601. /* [ targetBuffer targetStart sourceStart sourceEnd ] */
  20602. thr = (duk_hthread *) ctx;
  20603. DUK_UNREF(thr);
  20604. h_this = duk__require_bufobj_this(ctx);
  20605. h_bufarg = duk__require_bufobj_value(ctx, 0);
  20606. DUK_ASSERT(h_this != NULL);
  20607. DUK_ASSERT(h_bufarg != NULL);
  20608. source_length = (duk_int_t) h_this->length;
  20609. target_length = (duk_int_t) h_bufarg->length;
  20610. target_start = duk_to_int(ctx, 1);
  20611. source_start = duk_to_int(ctx, 2);
  20612. if (duk_is_undefined(ctx, 3)) {
  20613. source_end = source_length;
  20614. } else {
  20615. source_end = duk_to_int(ctx, 3);
  20616. }
  20617. DUK_DDD(DUK_DDDPRINT("checking copy args: target_start=%ld, target_length=%ld, "
  20618. "source_start=%ld, source_end=%ld, source_length=%ld",
  20619. (long) target_start, (long) h_bufarg->length,
  20620. (long) source_start, (long) source_end, (long) source_length));
  20621. /* This behavior mostly mimics Node.js now. */
  20622. if (source_start < 0 || source_end < 0 || target_start < 0) {
  20623. /* Negative offsets cause a RangeError. */
  20624. goto fail_bounds;
  20625. }
  20626. source_ustart = (duk_uint_t) source_start;
  20627. source_uend = (duk_uint_t) source_end;
  20628. target_ustart = (duk_uint_t) target_start;
  20629. if (source_ustart >= source_uend || /* crossed offsets or zero size */
  20630. source_ustart >= (duk_uint_t) source_length || /* source out-of-bounds (but positive) */
  20631. target_ustart >= (duk_uint_t) target_length) { /* target out-of-bounds (but positive) */
  20632. goto silent_ignore;
  20633. }
  20634. if (source_uend >= (duk_uint_t) source_length) {
  20635. /* Source end clamped silently to available length. */
  20636. source_uend = source_length;
  20637. }
  20638. copy_size = source_uend - source_ustart;
  20639. if (target_ustart + copy_size > (duk_uint_t) target_length) {
  20640. /* Clamp to target's end if too long.
  20641. *
  20642. * NOTE: there's no overflow possibility in the comparison;
  20643. * both target_ustart and copy_size are >= 0 and based on
  20644. * values in duk_int_t range. Adding them as duk_uint_t
  20645. * values is then guaranteed not to overflow.
  20646. */
  20647. DUK_ASSERT(target_ustart + copy_size >= target_ustart); /* no overflow */
  20648. DUK_ASSERT(target_ustart + copy_size >= copy_size); /* no overflow */
  20649. copy_size = (duk_uint_t) target_length - target_ustart;
  20650. }
  20651. DUK_DDD(DUK_DDDPRINT("making copy: target_ustart=%lu source_ustart=%lu copy_size=%lu",
  20652. (unsigned long) target_ustart, (unsigned long) source_ustart,
  20653. (unsigned long) copy_size));
  20654. DUK_ASSERT(copy_size >= 1);
  20655. DUK_ASSERT(source_ustart <= (duk_uint_t) source_length);
  20656. DUK_ASSERT(source_ustart + copy_size <= (duk_uint_t) source_length);
  20657. DUK_ASSERT(target_ustart <= (duk_uint_t) target_length);
  20658. DUK_ASSERT(target_ustart + copy_size <= (duk_uint_t) target_length);
  20659. /* Ensure copy is covered by underlying buffers. */
  20660. DUK_ASSERT(h_bufarg->buf != NULL); /* length check */
  20661. DUK_ASSERT(h_this->buf != NULL); /* length check */
  20662. if (DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_bufarg, target_ustart + copy_size) &&
  20663. DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_this, source_ustart + copy_size)) {
  20664. /* Must use memmove() because copy area may overlap (source and target
  20665. * buffer may be the same, or from different slices.
  20666. */
  20667. DUK_MEMMOVE((void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufarg) + target_ustart),
  20668. (const void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + source_ustart),
  20669. (size_t) copy_size);
  20670. } else {
  20671. DUK_DDD(DUK_DDDPRINT("buffer copy not covered by underlying buffer(s), ignoring"));
  20672. }
  20673. silent_ignore:
  20674. /* Return value is like write(), number of bytes written.
  20675. * The return value matters because of code like:
  20676. * "off += buf.copy(...)".
  20677. */
  20678. duk_push_uint(ctx, copy_size);
  20679. return 1;
  20680. fail_bounds:
  20681. return DUK_RET_RANGE_ERROR;
  20682. }
  20683. /*
  20684. * TypedArray.prototype.set()
  20685. *
  20686. * TypedArray set() is pretty interesting to implement because:
  20687. *
  20688. * - The source argument may be a plain array or a typedarray. If the
  20689. * source is a TypedArray, values are decoded and re-encoded into the
  20690. * target (not as a plain byte copy). This may happen even when the
  20691. * element byte size is the same, e.g. integer values may be re-encoded
  20692. * into floats.
  20693. *
  20694. * - Source and target may refer to the same underlying buffer, so that
  20695. * the set() operation may overlap. The specification requires that this
  20696. * must work as if a copy was made before the operation. Note that this
  20697. * is NOT a simple memmove() situation because the source and target
  20698. * byte sizes may be different -- e.g. a 4-byte source (Int8Array) may
  20699. * expand to a 16-byte target (Uint32Array) so that the target overlaps
  20700. * the source both from beginning and the end (unlike in typical memmove).
  20701. *
  20702. * - Even if 'buf' pointers of the source and target differ, there's no
  20703. * guarantee that their memory areas don't overlap. This may be the
  20704. * case with external buffers.
  20705. *
  20706. * Even so, it is nice to optimize for the common case:
  20707. *
  20708. * - Source and target separate buffers or non-overlapping.
  20709. *
  20710. * - Source and target have a compatible type so that a plain byte copy
  20711. * is possible. Note that while e.g. uint8 and int8 are compatible
  20712. * (coercion one way or another doesn't change the byte representation),
  20713. * e.g. int8 and uint8clamped are NOT compatible when writing int8
  20714. * values into uint8clamped typedarray (-1 would clamp to 0 for instance).
  20715. *
  20716. * See test-bi-typedarray-proto-set.js.
  20717. */
  20718. DUK_INTERNAL duk_ret_t duk_bi_typedarray_set(duk_context *ctx) {
  20719. duk_hthread *thr;
  20720. duk_hbufferobject *h_this;
  20721. duk_hobject *h_obj;
  20722. duk_uarridx_t i, n;
  20723. duk_int_t offset_signed;
  20724. duk_uint_t offset_elems;
  20725. duk_uint_t offset_bytes;
  20726. thr = (duk_hthread *) ctx;
  20727. DUK_UNREF(thr);
  20728. h_this = duk__require_bufobj_this(ctx);
  20729. DUK_ASSERT(h_this != NULL);
  20730. DUK_ASSERT_HBUFFEROBJECT_VALID(h_this);
  20731. if (h_this->buf == NULL) {
  20732. DUK_DDD(DUK_DDDPRINT("source neutered, skip copy"));
  20733. return 0;
  20734. }
  20735. h_obj = duk_require_hobject(ctx, 0);
  20736. DUK_ASSERT(h_obj != NULL);
  20737. /* XXX: V8 throws a TypeError for negative values. Would it
  20738. * be more useful to interpret negative offsets here from the
  20739. * end of the buffer too?
  20740. */
  20741. offset_signed = duk_to_int(ctx, 1);
  20742. if (offset_signed < 0) {
  20743. return DUK_RET_TYPE_ERROR;
  20744. }
  20745. offset_elems = (duk_uint_t) offset_signed;
  20746. offset_bytes = offset_elems << h_this->shift;
  20747. if ((offset_bytes >> h_this->shift) != offset_elems) {
  20748. /* Byte length would overflow. */
  20749. /* XXX: easier check with less code? */
  20750. return DUK_RET_RANGE_ERROR;
  20751. }
  20752. if (offset_bytes > h_this->length) {
  20753. /* Equality may be OK but >length not. Checking
  20754. * this explicitly avoids some overflow cases
  20755. * below.
  20756. */
  20757. return DUK_RET_RANGE_ERROR;
  20758. }
  20759. DUK_ASSERT(offset_bytes <= h_this->length);
  20760. /* Fast path: source is a TypedArray (or any bufferobject). */
  20761. if (DUK_HOBJECT_IS_BUFFEROBJECT(h_obj)) {
  20762. duk_hbufferobject *h_bufarg;
  20763. duk_uint16_t comp_mask;
  20764. duk_small_int_t no_overlap = 0;
  20765. duk_uint_t src_length;
  20766. duk_uint_t dst_length;
  20767. duk_uint_t dst_length_elems;
  20768. duk_uint8_t *p_src_base;
  20769. duk_uint8_t *p_src_end;
  20770. duk_uint8_t *p_src;
  20771. duk_uint8_t *p_dst_base;
  20772. duk_uint8_t *p_dst;
  20773. duk_small_uint_t src_elem_size;
  20774. duk_small_uint_t dst_elem_size;
  20775. h_bufarg = (duk_hbufferobject *) h_obj;
  20776. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufarg);
  20777. if (h_bufarg->buf == NULL) {
  20778. DUK_DDD(DUK_DDDPRINT("target neutered, skip copy"));
  20779. return 0;
  20780. }
  20781. /* Nominal size check. */
  20782. src_length = h_bufarg->length; /* bytes in source */
  20783. dst_length_elems = (src_length >> h_bufarg->shift); /* elems in source and dest */
  20784. dst_length = dst_length_elems << h_this->shift; /* bytes in dest */
  20785. if ((dst_length >> h_this->shift) != dst_length_elems) {
  20786. /* Byte length would overflow. */
  20787. /* XXX: easier check with less code? */
  20788. return DUK_RET_RANGE_ERROR;
  20789. }
  20790. DUK_DDD(DUK_DDDPRINT("nominal size check: src_length=%ld, dst_length=%ld",
  20791. (long) src_length, (long) dst_length));
  20792. DUK_ASSERT(offset_bytes <= h_this->length);
  20793. if (dst_length > h_this->length - offset_bytes) {
  20794. /* Overflow not an issue because subtraction is used on the right
  20795. * side and guaranteed to be >= 0.
  20796. */
  20797. DUK_DDD(DUK_DDDPRINT("copy exceeds target buffer nominal length"));
  20798. return DUK_RET_RANGE_ERROR;
  20799. }
  20800. if (!DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_this, offset_bytes + dst_length)) {
  20801. DUK_DDD(DUK_DDDPRINT("copy not covered by underlying target buffer, ignore"));
  20802. return 0;
  20803. }
  20804. p_src_base = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufarg);
  20805. p_dst_base = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + offset_bytes;
  20806. /* Check actual underlying buffers for validity and that they
  20807. * cover the copy. No side effects are allowed after the check
  20808. * so that the validity status doesn't change.
  20809. */
  20810. if (!DUK_HBUFFEROBJECT_VALID_SLICE(h_this) ||
  20811. !DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg)) {
  20812. /* The condition could be more narrow and check for the
  20813. * copy area only, but there's no need for fine grained
  20814. * behavior when the underlying buffer is misconfigured.
  20815. */
  20816. DUK_DDD(DUK_DDDPRINT("source and/or target not covered by underlying buffer, skip copy"));
  20817. return 0;
  20818. }
  20819. /* We want to do a straight memory copy if possible: this is
  20820. * an important operation because .set() is the TypedArray
  20821. * way to copy chunks of memory. However, because set()
  20822. * conceptually works in terms of elements, not all views are
  20823. * compatible with direct byte copying.
  20824. *
  20825. * If we do manage a direct copy, the "overlap issue" handled
  20826. * below can just be solved using memmove() because the source
  20827. * and destination element sizes are necessarily equal.
  20828. */
  20829. DUK_ASSERT(h_this->elem_type < sizeof(duk__buffer_elemtype_copy_compatible) / sizeof(duk_uint16_t));
  20830. comp_mask = duk__buffer_elemtype_copy_compatible[h_this->elem_type];
  20831. if (comp_mask & (1 << h_bufarg->elem_type)) {
  20832. DUK_ASSERT(src_length == dst_length);
  20833. DUK_DDD(DUK_DDDPRINT("fast path: able to use memmove() because views are compatible"));
  20834. DUK_MEMMOVE((void *) p_dst_base, (const void *) p_src_base, (size_t) dst_length);
  20835. return 0;
  20836. }
  20837. DUK_DDD(DUK_DDDPRINT("fast path: views are not compatible with a byte copy, copy by item"));
  20838. /* We want to avoid making a copy to process set() but that's
  20839. * not always possible: the source and the target may overlap
  20840. * and because element sizes are different, the overlap cannot
  20841. * always be handled with a memmove() or choosing the copy
  20842. * direction in a certain way. For example, if source type is
  20843. * uint8 and target type is uint32, the target area may exceed
  20844. * the source area from both ends!
  20845. *
  20846. * Note that because external buffers may point to the same
  20847. * memory areas, we must ultimately make this check using
  20848. * pointers.
  20849. *
  20850. * NOTE: careful with side effects: any side effect may cause
  20851. * a buffer resize (or external buffer pointer/length update)!
  20852. */
  20853. DUK_DDD(DUK_DDDPRINT("overlap check: p_src_base=%p, src_length=%ld, "
  20854. "p_dst_base=%p, dst_length=%ld",
  20855. (void *) p_src_base, (long) src_length,
  20856. (void *) p_dst_base, (long) dst_length));
  20857. if (p_src_base >= p_dst_base + dst_length || /* source starts after dest ends */
  20858. p_src_base + src_length < p_dst_base) { /* source ends before dest starts */
  20859. no_overlap = 1;
  20860. }
  20861. if (!no_overlap) {
  20862. /* There's overlap: the desired end result is that
  20863. * conceptually a copy is made to avoid "trampling"
  20864. * of source data by destination writes. We make
  20865. * an actual temporary copy to handle this case.
  20866. */
  20867. duk_uint8_t *p_src_copy;
  20868. DUK_DDD(DUK_DDDPRINT("there is overlap, make a copy of the source"));
  20869. p_src_copy = (duk_uint8_t *) duk_push_fixed_buffer(ctx, src_length);
  20870. DUK_ASSERT(p_src_copy != NULL);
  20871. DUK_MEMCPY((void *) p_src_copy, (const void *) p_src_base, (size_t) src_length);
  20872. p_src_base = p_src_copy; /* use p_src_base from now on */
  20873. }
  20874. /* Value stack intentionally mixed size here. */
  20875. DUK_DDD(DUK_DDDPRINT("after overlap check: p_src_base=%p, src_length=%ld, "
  20876. "p_dst_base=%p, dst_length=%ld, valstack top=%ld",
  20877. (void *) p_src_base, (long) src_length,
  20878. (void *) p_dst_base, (long) dst_length,
  20879. (long) duk_get_top(ctx)));
  20880. /* Ready to make the copy. We must proceed element by element
  20881. * and must avoid any side effects that might cause the buffer
  20882. * validity check above to become invalid.
  20883. *
  20884. * Although we work through the value stack here, only plain
  20885. * numbers are handled which should be side effect safe.
  20886. */
  20887. src_elem_size = 1 << h_bufarg->shift;
  20888. dst_elem_size = 1 << h_this->shift;
  20889. p_src = p_src_base;
  20890. p_dst = p_dst_base;
  20891. p_src_end = p_src_base + src_length;
  20892. while (p_src != p_src_end) {
  20893. DUK_DDD(DUK_DDDPRINT("fast path per element copy loop: "
  20894. "p_src=%p, p_src_end=%p, p_dst=%p",
  20895. (void *) p_src, (void *) p_src_end, (void *) p_dst));
  20896. /* A validated read() is always a number, so it's write coercion
  20897. * is always side effect free an won't invalidate pointers etc.
  20898. */
  20899. duk_hbufferobject_push_validated_read(ctx, h_bufarg, p_src, src_elem_size);
  20900. duk_hbufferobject_validated_write(ctx, h_this, p_dst, dst_elem_size);
  20901. duk_pop(ctx);
  20902. p_src += src_elem_size;
  20903. p_dst += dst_elem_size;
  20904. }
  20905. return 0;
  20906. } else {
  20907. /* Slow path: quite slow, but we save space by using the property code
  20908. * to write coerce target values. We don't need to worry about overlap
  20909. * here because the source is not a TypedArray.
  20910. *
  20911. * We could use the bufferobject write coercion helper but since the
  20912. * property read may have arbitrary side effects, full validity checks
  20913. * would be needed for every element anyway.
  20914. */
  20915. n = (duk_uarridx_t) duk_get_length(ctx, 0);
  20916. DUK_ASSERT(offset_bytes <= h_this->length);
  20917. if ((n << h_this->shift) > h_this->length - offset_bytes) {
  20918. /* Overflow not an issue because subtraction is used on the right
  20919. * side and guaranteed to be >= 0.
  20920. */
  20921. DUK_DDD(DUK_DDDPRINT("copy exceeds target buffer nominal length"));
  20922. return DUK_RET_RANGE_ERROR;
  20923. }
  20924. /* There's no need to check for buffer validity status for the
  20925. * target here: the property access code will do that for each
  20926. * element. Moreover, if we did check the validity here, side
  20927. * effects from reading the source argument might invalidate
  20928. * the results anyway.
  20929. */
  20930. DUK_ASSERT_TOP(ctx, 2);
  20931. duk_push_this(ctx);
  20932. for (i = 0; i < n; i++) {
  20933. duk_get_prop_index(ctx, 0, i);
  20934. duk_put_prop_index(ctx, 2, offset_elems + i);
  20935. }
  20936. }
  20937. return 0;
  20938. }
  20939. /*
  20940. * Node.js Buffer.prototype.slice([start], [end])
  20941. * ArrayBuffer.prototype.slice(begin, [end])
  20942. * TypedArray.prototype.slice(begin, [end])
  20943. *
  20944. * The API calls are almost identical; negative indices are counted from end
  20945. * of buffer, and final indices are clamped (allowing crossed indices). Main
  20946. * differences:
  20947. *
  20948. * - Copy/view behavior; Node.js .slice() and TypedArray .subarray() create
  20949. * views, ArrayBuffer .slice() creates a copy
  20950. *
  20951. * - Resulting object has a different class and prototype depending on the
  20952. * call (or 'this' argument)
  20953. *
  20954. * - TypedArray .subarray() arguments are element indices, not byte offsets
  20955. */
  20956. DUK_INTERNAL duk_ret_t duk_bi_buffer_slice_shared(duk_context *ctx) {
  20957. duk_hthread *thr;
  20958. duk_small_int_t magic;
  20959. duk_small_uint_t res_class_num;
  20960. duk_hobject *res_proto;
  20961. duk_hbufferobject *h_this;
  20962. duk_hbufferobject *h_bufobj;
  20963. duk_hbuffer *h_val;
  20964. duk_int_t start_offset, end_offset;
  20965. duk_uint_t slice_length;
  20966. thr = (duk_hthread *) ctx;
  20967. DUK_UNREF(thr);
  20968. /* [ start end ] */
  20969. magic = duk_get_current_magic(ctx);
  20970. h_this = duk__require_bufobj_this(ctx);
  20971. /* Slice offsets are element (not byte) offsets, which only matters
  20972. * for TypedArray views, Node.js Buffer and ArrayBuffer have shift
  20973. * zero so byte and element offsets are the same. Negative indices
  20974. * are counted from end of slice, crossed indices are allowed (and
  20975. * result in zero length result), and final values are clamped
  20976. * against the current slice. There's intentionally no check
  20977. * against the underlying buffer here.
  20978. */
  20979. duk__clamp_startend_negidx_shifted(ctx, h_this, 0 /*idx_start*/, 1 /*idx_end*/, &start_offset, &end_offset);
  20980. DUK_ASSERT(end_offset >= start_offset);
  20981. slice_length = (duk_uint_t) (end_offset - start_offset);
  20982. /* The resulting buffer object gets the same class and prototype as
  20983. * the buffer in 'this', e.g. if the input is a Node.js Buffer the
  20984. * result is a Node.js Buffer; if the input is a Float32Array, the
  20985. * result is a Float32Array.
  20986. *
  20987. * For the class number this seems correct. The internal prototype
  20988. * is not so clear: if 'this' is a bufferobject with a non-standard
  20989. * prototype object, that value gets copied over into the result
  20990. * (instead of using the standard prototype for that object type).
  20991. */
  20992. res_class_num = DUK_HOBJECT_GET_CLASS_NUMBER((duk_hobject *) h_this);
  20993. h_bufobj = duk_push_bufferobject(ctx,
  20994. DUK_HOBJECT_FLAG_EXTENSIBLE |
  20995. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  20996. DUK_HOBJECT_CLASS_AS_FLAGS(res_class_num),
  20997. DUK_BIDX_OBJECT_PROTOTYPE); /* replaced */
  20998. DUK_ASSERT(h_bufobj != NULL);
  20999. res_proto = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) h_this); /* may be NULL */
  21000. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) h_bufobj, res_proto);
  21001. h_bufobj->length = slice_length;
  21002. h_bufobj->shift = h_this->shift; /* inherit */
  21003. h_bufobj->elem_type = h_this->elem_type; /* inherit */
  21004. h_bufobj->is_view = magic & 0x01;
  21005. DUK_ASSERT(h_bufobj->is_view == 0 || h_bufobj->is_view == 1);
  21006. h_val = h_this->buf;
  21007. if (h_val == NULL) {
  21008. return DUK_RET_TYPE_ERROR;
  21009. }
  21010. if (magic & 0x02) {
  21011. /* non-zero: make copy */
  21012. duk_uint8_t *p_copy;
  21013. duk_size_t copy_length;
  21014. p_copy = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) slice_length);
  21015. DUK_ASSERT(p_copy != NULL);
  21016. /* Copy slice, respecting underlying buffer limits; remainder
  21017. * is left as zero.
  21018. */
  21019. copy_length = DUK_HBUFFEROBJECT_CLAMP_BYTELENGTH(h_this, slice_length);
  21020. DUK_MEMCPY((void *) p_copy,
  21021. (const void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + start_offset),
  21022. copy_length);
  21023. h_val = duk_get_hbuffer(ctx, -1);
  21024. DUK_ASSERT(h_val != NULL);
  21025. h_bufobj->buf = h_val;
  21026. DUK_HBUFFER_INCREF(thr, h_val);
  21027. DUK_ASSERT(h_bufobj->offset == 0);
  21028. duk_pop(ctx); /* reachable so pop OK */
  21029. } else {
  21030. h_bufobj->buf = h_val;
  21031. DUK_HBUFFER_INCREF(thr, h_val);
  21032. h_bufobj->offset = (duk_uint_t) (h_this->offset + start_offset);
  21033. /* Copy the .buffer property, needed for TypedArray.prototype.subarray().
  21034. *
  21035. * XXX: limit copy only for TypedArray classes specifically?
  21036. */
  21037. duk_push_this(ctx);
  21038. if (duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LC_BUFFER)) {
  21039. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE);
  21040. duk_pop(ctx);
  21041. } else {
  21042. duk_pop_2(ctx);
  21043. }
  21044. }
  21045. /* unbalanced stack on purpose */
  21046. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  21047. return 1;
  21048. }
  21049. /*
  21050. * Node.js Buffer.isEncoding()
  21051. */
  21052. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_encoding(duk_context *ctx) {
  21053. const char *encoding;
  21054. /* only accept lowercase 'utf8' now. */
  21055. encoding = duk_to_string(ctx, 0);
  21056. DUK_ASSERT(duk_is_string(ctx, 0)); /* guaranteed by duk_to_string() */
  21057. duk_push_boolean(ctx, DUK_STRCMP(encoding, "utf8") == 0);
  21058. return 1;
  21059. }
  21060. /*
  21061. * Node.js Buffer.isBuffer()
  21062. */
  21063. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_buffer(duk_context *ctx) {
  21064. duk_hthread *thr;
  21065. duk_tval *tv;
  21066. duk_hobject *h;
  21067. duk_hobject *h_proto;
  21068. duk_bool_t ret = 0;
  21069. thr = (duk_hthread *) ctx;
  21070. DUK_ASSERT(duk_get_top(ctx) >= 1); /* nargs */
  21071. tv = duk_get_tval(ctx, 0);
  21072. DUK_ASSERT(tv != NULL);
  21073. if (DUK_TVAL_IS_OBJECT(tv)) {
  21074. h = DUK_TVAL_GET_OBJECT(tv);
  21075. DUK_ASSERT(h != NULL);
  21076. h_proto = thr->builtins[DUK_BIDX_NODEJS_BUFFER_PROTOTYPE];
  21077. DUK_ASSERT(h_proto != NULL);
  21078. h = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
  21079. if (h) {
  21080. ret = duk_hobject_prototype_chain_contains(thr, h, h_proto, 0 /*ignore_loop*/);
  21081. }
  21082. }
  21083. duk_push_boolean(ctx, ret);
  21084. return 1;
  21085. }
  21086. /*
  21087. * Node.js Buffer.byteLength()
  21088. */
  21089. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_byte_length(duk_context *ctx) {
  21090. const char *str;
  21091. duk_size_t len;
  21092. /* At the moment Buffer(<str>) will just use the string bytes as
  21093. * is (ignoring encoding), so we return the string length here
  21094. * unconditionally.
  21095. */
  21096. str = duk_to_lstring(ctx, 0, &len);
  21097. DUK_UNREF(str);
  21098. duk_push_size_t(ctx, len);
  21099. return 1;
  21100. }
  21101. /*
  21102. * Node.js Buffer.concat()
  21103. */
  21104. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_concat(duk_context *ctx) {
  21105. duk_hthread *thr;
  21106. duk_hobject *h_arg;
  21107. duk_int_t total_length = 0;
  21108. duk_hbufferobject *h_bufobj;
  21109. duk_hbufferobject *h_bufres;
  21110. duk_hbuffer *h_val;
  21111. duk_uint_t i, n;
  21112. duk_uint8_t *p;
  21113. duk_size_t space_left;
  21114. duk_size_t copy_size;
  21115. thr = (duk_hthread *) ctx;
  21116. DUK_UNREF(thr);
  21117. /* Node.js accepts only actual Arrays. */
  21118. h_arg = duk_require_hobject(ctx, 0);
  21119. if (DUK_HOBJECT_GET_CLASS_NUMBER(h_arg) != DUK_HOBJECT_CLASS_ARRAY) {
  21120. return DUK_RET_TYPE_ERROR;
  21121. }
  21122. /* Compute result length and validate argument buffers. */
  21123. n = (duk_uint_t) duk_get_length(ctx, 0);
  21124. for (i = 0; i < n; i++) {
  21125. /* Neutered checks not necessary here: neutered buffers have
  21126. * zero 'length' so we'll effectively skip them.
  21127. */
  21128. DUK_ASSERT_TOP(ctx, 2); /* [ array totalLength ] */
  21129. duk_get_prop_index(ctx, 0, (duk_uarridx_t) i); /* -> [ array totalLength buf ] */
  21130. h_bufobj = duk__require_bufobj_value(ctx, 2);
  21131. DUK_ASSERT(h_bufobj != NULL);
  21132. total_length += h_bufobj->length;
  21133. duk_pop(ctx);
  21134. }
  21135. if (n == 1) {
  21136. /* For the case n==1 Node.js doesn't seem to type check
  21137. * the sole member but we do it before returning it.
  21138. * For this case only the original buffer object is
  21139. * returned (not a copy).
  21140. */
  21141. duk_get_prop_index(ctx, 0, 0);
  21142. return 1;
  21143. }
  21144. /* User totalLength overrides a computed length, but we'll check
  21145. * every copy in the copy loop. Note that duk_to_uint() can
  21146. * technically have arbitrary side effects so we need to recheck
  21147. * the buffers in the copy loop.
  21148. */
  21149. if (!duk_is_undefined(ctx, 1) && n > 0) {
  21150. /* For n == 0, Node.js ignores totalLength argument and
  21151. * returns a zero length buffer.
  21152. */
  21153. total_length = duk_to_int(ctx, 1);
  21154. }
  21155. if (total_length < 0) {
  21156. return DUK_RET_RANGE_ERROR;
  21157. }
  21158. h_bufres = duk_push_bufferobject(ctx,
  21159. DUK_HOBJECT_FLAG_EXTENSIBLE |
  21160. DUK_HOBJECT_FLAG_BUFFEROBJECT |
  21161. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER),
  21162. DUK_BIDX_NODEJS_BUFFER_PROTOTYPE);
  21163. DUK_ASSERT(h_bufres != NULL);
  21164. p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, total_length);
  21165. DUK_ASSERT(p != NULL);
  21166. space_left = total_length;
  21167. for (i = 0; i < n; i++) {
  21168. DUK_ASSERT_TOP(ctx, 4); /* [ array totalLength bufres buf ] */
  21169. duk_get_prop_index(ctx, 0, (duk_uarridx_t) i);
  21170. h_bufobj = duk__require_bufobj_value(ctx, 4);
  21171. DUK_ASSERT(h_bufobj != NULL);
  21172. copy_size = h_bufobj->length;
  21173. if (copy_size > space_left) {
  21174. copy_size = space_left;
  21175. }
  21176. if (h_bufobj->buf != NULL &&
  21177. DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj)) {
  21178. DUK_MEMCPY((void *) p,
  21179. (const void *) DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufobj),
  21180. copy_size);
  21181. } else {
  21182. /* Just skip, leaving zeroes in the result. */
  21183. ;
  21184. }
  21185. p += copy_size;
  21186. space_left -= copy_size;
  21187. duk_pop(ctx);
  21188. }
  21189. h_val = duk_get_hbuffer(ctx, -1);
  21190. DUK_ASSERT(h_val != NULL);
  21191. duk__set_bufobj_buffer(ctx, h_bufres, h_val);
  21192. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufres);
  21193. duk_pop(ctx); /* pop plain buffer, now reachable through h_bufres */
  21194. return 1; /* return h_bufres */
  21195. }
  21196. /*
  21197. * Shared readfield and writefield methods
  21198. *
  21199. * The readfield/writefield methods need support for endianness and field
  21200. * types. All offsets are byte based so no offset shifting is needed.
  21201. */
  21202. /* Format of magic, bits:
  21203. * 0...1: field type; 0=uint8, 1=uint16, 2=uint32, 3=float, 4=double, 5=unused, 6=unused, 7=unused
  21204. * 3: endianness: 0=little, 1=big
  21205. * 4: signed: 1=yes, 0=no
  21206. * 5: typedarray: 1=yes, 0=no
  21207. */
  21208. #define DUK__FLD_8BIT 0
  21209. #define DUK__FLD_16BIT 1
  21210. #define DUK__FLD_32BIT 2
  21211. #define DUK__FLD_FLOAT 3
  21212. #define DUK__FLD_DOUBLE 4
  21213. #define DUK__FLD_VARINT 5
  21214. #define DUK__FLD_BIGENDIAN (1 << 3)
  21215. #define DUK__FLD_SIGNED (1 << 4)
  21216. #define DUK__FLD_TYPEDARRAY (1 << 5)
  21217. /* XXX: split into separate functions for each field type? */
  21218. DUK_INTERNAL duk_ret_t duk_bi_buffer_readfield(duk_context *ctx) {
  21219. duk_hthread *thr;
  21220. duk_small_int_t magic = (duk_small_int_t) duk_get_current_magic(ctx);
  21221. duk_small_int_t magic_ftype;
  21222. duk_small_int_t magic_bigendian;
  21223. duk_small_int_t magic_signed;
  21224. duk_small_int_t magic_typedarray;
  21225. duk_small_int_t endswap;
  21226. duk_hbufferobject *h_this;
  21227. duk_bool_t no_assert;
  21228. duk_int_t offset_signed;
  21229. duk_uint_t offset;
  21230. duk_uint_t buffer_length;
  21231. duk_uint_t check_length;
  21232. duk_uint8_t *buf;
  21233. duk_double_union du;
  21234. thr = (duk_hthread *) ctx;
  21235. DUK_UNREF(thr);
  21236. magic_ftype = magic & 0x0007;
  21237. magic_bigendian = magic & 0x0008;
  21238. magic_signed = magic & 0x0010;
  21239. magic_typedarray = magic & 0x0020;
  21240. h_this = duk__require_bufobj_this(ctx);
  21241. DUK_ASSERT(h_this != NULL);
  21242. buffer_length = h_this->length;
  21243. /* [ offset noAssert ], when ftype != DUK__FLD_VARINT */
  21244. /* [ offset fieldByteLength noAssert ], when ftype == DUK__FLD_VARINT */
  21245. /* [ offset littleEndian ], when DUK__FLD_TYPEDARRAY (regardless of ftype) */
  21246. /* Handle TypedArray vs. Node.js Buffer arg differences */
  21247. if (magic_typedarray) {
  21248. no_assert = 0;
  21249. #if defined(DUK_USE_INTEGER_LE)
  21250. endswap = !duk_to_boolean(ctx, 1); /* 1=little endian */
  21251. #else
  21252. endswap = duk_to_boolean(ctx, 1); /* 1=little endian */
  21253. #endif
  21254. } else {
  21255. no_assert = duk_to_boolean(ctx, (magic_ftype == DUK__FLD_VARINT) ? 2 : 1);
  21256. #if defined(DUK_USE_INTEGER_LE)
  21257. endswap = magic_bigendian;
  21258. #else
  21259. endswap = !magic_bigendian;
  21260. #endif
  21261. }
  21262. /* Offset is coerced first to signed integer range and then to unsigned.
  21263. * This ensures we can add a small byte length (1-8) to the offset in
  21264. * bound checks and not wrap.
  21265. */
  21266. offset_signed = duk_to_int(ctx, 0);
  21267. offset = (duk_uint_t) offset_signed;
  21268. if (offset_signed < 0) {
  21269. goto fail_bounds;
  21270. }
  21271. DUK_DDD(DUK_DDDPRINT("readfield, buffer_length=%ld, offset=%ld, no_assert=%d, "
  21272. "magic=%04x, magic_fieldtype=%d, magic_bigendian=%d, magic_signed=%d, "
  21273. "endswap=%d",
  21274. (long) buffer_length, (long) offset, (int) no_assert,
  21275. (unsigned int) magic, (int) magic_ftype, (int) (magic_bigendian >> 3),
  21276. (int) (magic_signed >> 4), (int) endswap));
  21277. /* Update 'buffer_length' to be the effective, safe limit which
  21278. * takes into account the underlying buffer. This value will be
  21279. * potentially invalidated by any side effect.
  21280. */
  21281. check_length = DUK_HBUFFEROBJECT_CLAMP_BYTELENGTH(h_this, buffer_length);
  21282. DUK_DDD(DUK_DDDPRINT("buffer_length=%ld, check_length=%ld",
  21283. (long) buffer_length, (long) check_length));
  21284. if (h_this->buf) {
  21285. buf = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this);
  21286. } else {
  21287. /* neutered, value doesn't matter because check_length is 0. */
  21288. DUK_ASSERT(check_length == 0);
  21289. buf = NULL;
  21290. }
  21291. switch (magic_ftype) {
  21292. case DUK__FLD_8BIT: {
  21293. duk_uint8_t tmp;
  21294. if (offset + 1U > check_length) {
  21295. goto fail_bounds;
  21296. }
  21297. tmp = buf[offset];
  21298. if (magic_signed) {
  21299. duk_push_int(ctx, (duk_int_t) ((duk_int8_t) tmp));
  21300. } else {
  21301. duk_push_uint(ctx, (duk_uint_t) tmp);
  21302. }
  21303. break;
  21304. }
  21305. case DUK__FLD_16BIT: {
  21306. duk_uint16_t tmp;
  21307. if (offset + 2U > check_length) {
  21308. goto fail_bounds;
  21309. }
  21310. DUK_MEMCPY((void *) du.uc, (const void *) (buf + offset), 2);
  21311. tmp = du.us[0];
  21312. if (endswap) {
  21313. tmp = DUK_BSWAP16(tmp);
  21314. }
  21315. if (magic_signed) {
  21316. duk_push_int(ctx, (duk_int_t) ((duk_int16_t) tmp));
  21317. } else {
  21318. duk_push_uint(ctx, (duk_uint_t) tmp);
  21319. }
  21320. break;
  21321. }
  21322. case DUK__FLD_32BIT: {
  21323. duk_uint32_t tmp;
  21324. if (offset + 4U > check_length) {
  21325. goto fail_bounds;
  21326. }
  21327. DUK_MEMCPY((void *) du.uc, (const void *) (buf + offset), 4);
  21328. tmp = du.ui[0];
  21329. if (endswap) {
  21330. tmp = DUK_BSWAP32(tmp);
  21331. }
  21332. if (magic_signed) {
  21333. duk_push_int(ctx, (duk_int_t) ((duk_int32_t) tmp));
  21334. } else {
  21335. duk_push_uint(ctx, (duk_uint_t) tmp);
  21336. }
  21337. break;
  21338. }
  21339. case DUK__FLD_FLOAT: {
  21340. duk_uint32_t tmp;
  21341. if (offset + 4U > check_length) {
  21342. goto fail_bounds;
  21343. }
  21344. DUK_MEMCPY((void *) du.uc, (const void *) (buf + offset), 4);
  21345. if (endswap) {
  21346. tmp = du.ui[0];
  21347. tmp = DUK_BSWAP32(tmp);
  21348. du.ui[0] = tmp;
  21349. }
  21350. duk_push_number(ctx, (duk_double_t) du.f[0]);
  21351. break;
  21352. }
  21353. case DUK__FLD_DOUBLE: {
  21354. if (offset + 8U > check_length) {
  21355. goto fail_bounds;
  21356. }
  21357. DUK_MEMCPY((void *) du.uc, (const void *) (buf + offset), 8);
  21358. if (endswap) {
  21359. DUK_DBLUNION_BSWAP64(&du);
  21360. }
  21361. duk_push_number(ctx, (duk_double_t) du.d);
  21362. break;
  21363. }
  21364. case DUK__FLD_VARINT: {
  21365. /* Node.js Buffer variable width integer field. We don't really
  21366. * care about speed here, so aim for shortest algorithm.
  21367. */
  21368. duk_int_t field_bytelen;
  21369. duk_int_t i, i_step, i_end;
  21370. #if defined(DUK_USE_64BIT_OPS)
  21371. duk_int64_t tmp;
  21372. duk_small_uint_t shift_tmp;
  21373. #else
  21374. duk_double_t tmp;
  21375. duk_small_int_t highbyte;
  21376. #endif
  21377. const duk_uint8_t *p;
  21378. field_bytelen = duk_get_int(ctx, 1); /* avoid side effects! */
  21379. if (field_bytelen < 1 || field_bytelen > 6) {
  21380. goto fail_field_length;
  21381. }
  21382. if (offset + (duk_uint_t) field_bytelen > check_length) {
  21383. goto fail_bounds;
  21384. }
  21385. p = (const duk_uint8_t *) (buf + offset);
  21386. /* Slow gathering of value using either 64-bit arithmetic
  21387. * or IEEE doubles if 64-bit types not available. Handling
  21388. * of negative numbers is a bit non-obvious in both cases.
  21389. */
  21390. if (magic_bigendian) {
  21391. /* Gather in big endian */
  21392. i = 0;
  21393. i_step = 1;
  21394. i_end = field_bytelen; /* one i_step over */
  21395. } else {
  21396. /* Gather in little endian */
  21397. i = field_bytelen - 1;
  21398. i_step = -1;
  21399. i_end = -1; /* one i_step over */
  21400. }
  21401. #if defined(DUK_USE_64BIT_OPS)
  21402. tmp = 0;
  21403. do {
  21404. DUK_ASSERT(i >= 0 && i < field_bytelen);
  21405. tmp = (tmp << 8) + (duk_int64_t) p[i];
  21406. i += i_step;
  21407. } while (i != i_end);
  21408. if (magic_signed) {
  21409. /* Shift to sign extend. */
  21410. shift_tmp = 64 - (field_bytelen * 8);
  21411. tmp = (tmp << shift_tmp) >> shift_tmp;
  21412. }
  21413. duk_push_i64(ctx, tmp);
  21414. #else
  21415. highbyte = p[i];
  21416. if (magic_signed && (highbyte & 0x80) != 0) {
  21417. /* 0xff => 255 - 256 = -1; 0x80 => 128 - 256 = -128 */
  21418. tmp = (duk_double_t) (highbyte - 256);
  21419. } else {
  21420. tmp = (duk_double_t) highbyte;
  21421. }
  21422. for (;;) {
  21423. i += i_step;
  21424. if (i == i_end) {
  21425. break;
  21426. }
  21427. DUK_ASSERT(i >= 0 && i < field_bytelen);
  21428. tmp = (tmp * 256.0) + (duk_double_t) p[i];
  21429. }
  21430. duk_push_number(ctx, tmp);
  21431. #endif
  21432. break;
  21433. }
  21434. default: { /* should never happen but default here */
  21435. goto fail_bounds;
  21436. }
  21437. }
  21438. return 1;
  21439. fail_field_length:
  21440. fail_bounds:
  21441. if (no_assert) {
  21442. /* Node.js return value for noAssert out-of-bounds reads is
  21443. * usually (but not always) NaN. Return NaN consistently.
  21444. */
  21445. duk_push_nan(ctx);
  21446. return 1;
  21447. }
  21448. return DUK_RET_RANGE_ERROR;
  21449. }
  21450. /* XXX: split into separate functions for each field type? */
  21451. DUK_INTERNAL duk_ret_t duk_bi_buffer_writefield(duk_context *ctx) {
  21452. duk_hthread *thr;
  21453. duk_small_int_t magic = (duk_small_int_t) duk_get_current_magic(ctx);
  21454. duk_small_int_t magic_ftype;
  21455. duk_small_int_t magic_bigendian;
  21456. duk_small_int_t magic_signed;
  21457. duk_small_int_t magic_typedarray;
  21458. duk_small_int_t endswap;
  21459. duk_hbufferobject *h_this;
  21460. duk_bool_t no_assert;
  21461. duk_int_t offset_signed;
  21462. duk_uint_t offset;
  21463. duk_uint_t buffer_length;
  21464. duk_uint_t check_length;
  21465. duk_uint8_t *buf;
  21466. duk_double_union du;
  21467. duk_int_t nbytes = 0;
  21468. thr = (duk_hthread *) ctx;
  21469. DUK_UNREF(thr);
  21470. magic_ftype = magic & 0x0007;
  21471. magic_bigendian = magic & 0x0008;
  21472. magic_signed = magic & 0x0010;
  21473. magic_typedarray = magic & 0x0020;
  21474. DUK_UNREF(magic_signed);
  21475. h_this = duk__require_bufobj_this(ctx);
  21476. DUK_ASSERT(h_this != NULL);
  21477. buffer_length = h_this->length;
  21478. /* [ value offset noAssert ], when ftype != DUK__FLD_VARINT */
  21479. /* [ value offset fieldByteLength noAssert ], when ftype == DUK__FLD_VARINT */
  21480. /* [ offset value littleEndian ], when DUK__FLD_TYPEDARRAY (regardless of ftype) */
  21481. /* Handle TypedArray vs. Node.js Buffer arg differences */
  21482. if (magic_typedarray) {
  21483. no_assert = 0;
  21484. #if defined(DUK_USE_INTEGER_LE)
  21485. endswap = !duk_to_boolean(ctx, 2); /* 1=little endian */
  21486. #else
  21487. endswap = duk_to_boolean(ctx, 2); /* 1=little endian */
  21488. #endif
  21489. duk_swap(ctx, 0, 1); /* offset/value order different from Node.js */
  21490. } else {
  21491. no_assert = duk_to_boolean(ctx, (magic_ftype == DUK__FLD_VARINT) ? 3 : 2);
  21492. #if defined(DUK_USE_INTEGER_LE)
  21493. endswap = magic_bigendian;
  21494. #else
  21495. endswap = !magic_bigendian;
  21496. #endif
  21497. }
  21498. /* Offset is coerced first to signed integer range and then to unsigned.
  21499. * This ensures we can add a small byte length (1-8) to the offset in
  21500. * bound checks and not wrap.
  21501. */
  21502. offset_signed = duk_to_int(ctx, 1);
  21503. offset = (duk_uint_t) offset_signed;
  21504. /* We need 'nbytes' even for a failed offset; return value must be
  21505. * (offset + nbytes) even when write fails due to invalid offset.
  21506. */
  21507. if (magic_ftype != DUK__FLD_VARINT) {
  21508. DUK_ASSERT(magic_ftype >= 0 && magic_ftype < (duk_small_int_t) (sizeof(duk__buffer_nbytes_from_fldtype) / sizeof(duk_uint8_t)));
  21509. nbytes = duk__buffer_nbytes_from_fldtype[magic_ftype];
  21510. } else {
  21511. nbytes = duk_get_int(ctx, 2);
  21512. if (nbytes < 1 || nbytes > 6) {
  21513. goto fail_field_length;
  21514. }
  21515. }
  21516. DUK_ASSERT(nbytes >= 1 && nbytes <= 8);
  21517. /* Now we can check offset validity. */
  21518. if (offset_signed < 0) {
  21519. goto fail_bounds;
  21520. }
  21521. DUK_DDD(DUK_DDDPRINT("writefield, value=%!T, buffer_length=%ld, offset=%ld, no_assert=%d, "
  21522. "magic=%04x, magic_fieldtype=%d, magic_bigendian=%d, magic_signed=%d, "
  21523. "endswap=%d",
  21524. duk_get_tval(ctx, 0), (long) buffer_length, (long) offset, (int) no_assert,
  21525. (unsigned int) magic, (int) magic_ftype, (int) (magic_bigendian >> 3),
  21526. (int) (magic_signed >> 4), (int) endswap));
  21527. /* Coerce value to a number before computing check_length, so that
  21528. * the field type specific coercion below can't have side effects
  21529. * that would invalidate check_length.
  21530. */
  21531. duk_to_number(ctx, 0);
  21532. /* Update 'buffer_length' to be the effective, safe limit which
  21533. * takes into account the underlying buffer. This value will be
  21534. * potentially invalidated by any side effect.
  21535. */
  21536. check_length = DUK_HBUFFEROBJECT_CLAMP_BYTELENGTH(h_this, buffer_length);
  21537. DUK_DDD(DUK_DDDPRINT("buffer_length=%ld, check_length=%ld",
  21538. (long) buffer_length, (long) check_length));
  21539. if (h_this->buf) {
  21540. buf = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this);
  21541. } else {
  21542. /* neutered, value doesn't matter because check_length is 0. */
  21543. DUK_ASSERT(check_length == 0);
  21544. buf = NULL;
  21545. }
  21546. switch (magic_ftype) {
  21547. case DUK__FLD_8BIT: {
  21548. if (offset + 1U > check_length) {
  21549. goto fail_bounds;
  21550. }
  21551. /* sign doesn't matter when writing */
  21552. buf[offset] = (duk_uint8_t) duk_to_uint32(ctx, 0);
  21553. break;
  21554. }
  21555. case DUK__FLD_16BIT: {
  21556. duk_uint16_t tmp;
  21557. if (offset + 2U > check_length) {
  21558. goto fail_bounds;
  21559. }
  21560. tmp = (duk_uint16_t) duk_to_uint32(ctx, 0);
  21561. if (endswap) {
  21562. tmp = DUK_BSWAP16(tmp);
  21563. }
  21564. du.us[0] = tmp;
  21565. /* sign doesn't matter when writing */
  21566. DUK_MEMCPY((void *) (buf + offset), (const void *) du.uc, 2);
  21567. break;
  21568. }
  21569. case DUK__FLD_32BIT: {
  21570. duk_uint32_t tmp;
  21571. if (offset + 4U > check_length) {
  21572. goto fail_bounds;
  21573. }
  21574. tmp = (duk_uint32_t) duk_to_uint32(ctx, 0);
  21575. if (endswap) {
  21576. tmp = DUK_BSWAP32(tmp);
  21577. }
  21578. du.ui[0] = tmp;
  21579. /* sign doesn't matter when writing */
  21580. DUK_MEMCPY((void *) (buf + offset), (const void *) du.uc, 4);
  21581. break;
  21582. }
  21583. case DUK__FLD_FLOAT: {
  21584. duk_uint32_t tmp;
  21585. if (offset + 4U > check_length) {
  21586. goto fail_bounds;
  21587. }
  21588. du.f[0] = (duk_float_t) duk_to_number(ctx, 0);
  21589. if (endswap) {
  21590. tmp = du.ui[0];
  21591. tmp = DUK_BSWAP32(tmp);
  21592. du.ui[0] = tmp;
  21593. }
  21594. /* sign doesn't matter when writing */
  21595. DUK_MEMCPY((void *) (buf + offset), (const void *) du.uc, 4);
  21596. break;
  21597. }
  21598. case DUK__FLD_DOUBLE: {
  21599. if (offset + 8U > check_length) {
  21600. goto fail_bounds;
  21601. }
  21602. du.d = (duk_double_t) duk_to_number(ctx, 0);
  21603. if (endswap) {
  21604. DUK_DBLUNION_BSWAP64(&du);
  21605. }
  21606. /* sign doesn't matter when writing */
  21607. DUK_MEMCPY((void *) (buf + offset), (const void *) du.uc, 8);
  21608. break;
  21609. }
  21610. case DUK__FLD_VARINT: {
  21611. /* Node.js Buffer variable width integer field. We don't really
  21612. * care about speed here, so aim for shortest algorithm.
  21613. */
  21614. duk_int_t field_bytelen;
  21615. duk_int_t i, i_step, i_end;
  21616. #if defined(DUK_USE_64BIT_OPS)
  21617. duk_int64_t tmp;
  21618. #else
  21619. duk_double_t tmp;
  21620. #endif
  21621. duk_uint8_t *p;
  21622. field_bytelen = (duk_int_t) nbytes;
  21623. if (offset + (duk_uint_t) field_bytelen > check_length) {
  21624. goto fail_bounds;
  21625. }
  21626. /* Slow writing of value using either 64-bit arithmetic
  21627. * or IEEE doubles if 64-bit types not available. There's
  21628. * no special sign handling when writing varints.
  21629. */
  21630. if (magic_bigendian) {
  21631. /* Write in big endian */
  21632. i = field_bytelen; /* one i_step added at top of loop */
  21633. i_step = -1;
  21634. i_end = 0;
  21635. } else {
  21636. /* Write in little endian */
  21637. i = -1; /* one i_step added at top of loop */
  21638. i_step = 1;
  21639. i_end = field_bytelen - 1;
  21640. }
  21641. /* XXX: The duk_to_number() cast followed by integer coercion
  21642. * is platform specific so NaN, +/- Infinity, and out-of-bounds
  21643. * values result in platform specific output now.
  21644. * See: test-bi-nodejs-buffer-proto-varint-special.js
  21645. */
  21646. #if defined(DUK_USE_64BIT_OPS)
  21647. tmp = (duk_int64_t) duk_to_number(ctx, 0);
  21648. p = (duk_uint8_t *) (buf + offset);
  21649. do {
  21650. i += i_step;
  21651. DUK_ASSERT(i >= 0 && i < field_bytelen);
  21652. p[i] = (duk_uint8_t) (tmp & 0xff);
  21653. tmp = tmp >> 8; /* unnecessary shift for last byte */
  21654. } while (i != i_end);
  21655. #else
  21656. tmp = duk_to_number(ctx, 0);
  21657. p = (duk_uint8_t *) (buf + offset);
  21658. do {
  21659. i += i_step;
  21660. tmp = DUK_FLOOR(tmp);
  21661. DUK_ASSERT(i >= 0 && i < field_bytelen);
  21662. p[i] = (duk_uint8_t) (DUK_FMOD(tmp, 256.0));
  21663. tmp = tmp / 256.0; /* unnecessary div for last byte */
  21664. } while (i != i_end);
  21665. #endif
  21666. break;
  21667. }
  21668. default: { /* should never happen but default here */
  21669. goto fail_bounds;
  21670. }
  21671. }
  21672. /* Node.js Buffer: return offset + #bytes written (i.e. next
  21673. * write offset).
  21674. */
  21675. if (magic_typedarray) {
  21676. /* For TypedArrays 'undefined' return value is specified
  21677. * by ES6 (matches V8).
  21678. */
  21679. return 0;
  21680. }
  21681. duk_push_uint(ctx, offset + nbytes);
  21682. return 1;
  21683. fail_field_length:
  21684. fail_bounds:
  21685. if (no_assert) {
  21686. /* Node.js return value for failed writes is offset + #bytes
  21687. * that would have been written.
  21688. */
  21689. /* XXX: for negative input offsets, 'offset' will be a large
  21690. * positive value so the result here is confusing.
  21691. */
  21692. if (magic_typedarray) {
  21693. return 0;
  21694. }
  21695. duk_push_uint(ctx, offset + nbytes);
  21696. return 1;
  21697. }
  21698. return DUK_RET_RANGE_ERROR;
  21699. }
  21700. #undef DUK__FLD_8BIT
  21701. #undef DUK__FLD_16BIT
  21702. #undef DUK__FLD_32BIT
  21703. #undef DUK__FLD_FLOAT
  21704. #undef DUK__FLD_DOUBLE
  21705. #undef DUK__FLD_VARINT
  21706. #undef DUK__FLD_BIGENDIAN
  21707. #undef DUK__FLD_SIGNED
  21708. #undef DUK__FLD_TYPEDARRAY
  21709. #line 1 "duk_bi_date.c"
  21710. /*
  21711. * Date built-ins
  21712. *
  21713. * Unlike most built-ins, Date has a lot of platform dependencies for
  21714. * getting UTC time, converting between UTC and local time, and parsing
  21715. * and formatting time values.
  21716. *
  21717. * See doc/datetime.txt.
  21718. *
  21719. * Platform specific links:
  21720. *
  21721. * - http://msdn.microsoft.com/en-us/library/windows/desktop/ms725473(v=vs.85).aspx
  21722. */
  21723. /* include removed: duk_internal.h */
  21724. /*
  21725. * Platform specific includes and defines
  21726. *
  21727. * Note that necessary system headers (like <sys/time.h>) are included
  21728. * by duk_internal.h (or duk_features.h, which is included by duk_internal.h)
  21729. * because the header locations vary between systems and we don't want
  21730. * that clutter here.
  21731. */
  21732. #define DUK__GET_NOW_TIMEVAL duk_bi_date_get_now
  21733. #define DUK__GET_LOCAL_TZOFFSET duk__get_local_tzoffset
  21734. /* Buffer sizes for some UNIX calls. Larger than strictly necessary
  21735. * to avoid Valgrind errors.
  21736. */
  21737. #define DUK__STRPTIME_BUF_SIZE 64
  21738. #define DUK__STRFTIME_BUF_SIZE 64
  21739. /*
  21740. * Other file level defines
  21741. */
  21742. /* Forward declarations. */
  21743. 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);
  21744. DUK_LOCAL_DECL duk_double_t duk__push_this_get_timeval(duk_context *ctx, duk_small_uint_t flags);
  21745. DUK_LOCAL_DECL void duk__timeval_to_parts(duk_double_t d, duk_int_t *parts, duk_double_t *dparts, duk_small_uint_t flags);
  21746. DUK_LOCAL_DECL duk_double_t duk__get_timeval_from_dparts(duk_double_t *dparts, duk_small_uint_t flags);
  21747. DUK_LOCAL_DECL void duk__twodigit_year_fixup(duk_context *ctx, duk_idx_t idx_val);
  21748. DUK_LOCAL_DECL duk_bool_t duk__is_leap_year(duk_int_t year);
  21749. DUK_LOCAL_DECL duk_bool_t duk__timeval_in_valid_range(duk_double_t x);
  21750. DUK_LOCAL_DECL duk_bool_t duk__timeval_in_leeway_range(duk_double_t x);
  21751. DUK_LOCAL_DECL duk_bool_t duk__year_in_valid_range(duk_double_t year);
  21752. /* Millisecond count constants. */
  21753. #define DUK__MS_SECOND 1000L
  21754. #define DUK__MS_MINUTE (60L * 1000L)
  21755. #define DUK__MS_HOUR (60L * 60L * 1000L)
  21756. #define DUK__MS_DAY (24L * 60L * 60L * 1000L)
  21757. /* Ecmascript date range is 100 million days from Epoch:
  21758. * > 100e6 * 24 * 60 * 60 * 1000 // 100M days in millisecs
  21759. * 8640000000000000
  21760. * (= 8.64e15)
  21761. */
  21762. #define DUK__MS_100M_DAYS (8.64e15)
  21763. #define DUK__MS_100M_DAYS_LEEWAY (8.64e15 + 24 * 3600e3)
  21764. /* Ecmascript year range:
  21765. * > new Date(100e6 * 24 * 3600e3).toISOString()
  21766. * '+275760-09-13T00:00:00.000Z'
  21767. * > new Date(-100e6 * 24 * 3600e3).toISOString()
  21768. * '-271821-04-20T00:00:00.000Z'
  21769. */
  21770. #define DUK__MIN_ECMA_YEAR (-271821)
  21771. #define DUK__MAX_ECMA_YEAR 275760
  21772. /* Part indices for internal breakdowns. Part order from DUK__IDX_YEAR to
  21773. * DUK__IDX_MILLISECOND matches argument ordering of Ecmascript API calls
  21774. * (like Date constructor call). A few functions in this file depend
  21775. * on the specific ordering, so change with care. 16 bits are not enough
  21776. * for all parts (year, specifically).
  21777. *
  21778. * (Must be in-sync with genbuiltins.py.)
  21779. */
  21780. #define DUK__IDX_YEAR 0 /* year */
  21781. #define DUK__IDX_MONTH 1 /* month: 0 to 11 */
  21782. #define DUK__IDX_DAY 2 /* day within month: 0 to 30 */
  21783. #define DUK__IDX_HOUR 3
  21784. #define DUK__IDX_MINUTE 4
  21785. #define DUK__IDX_SECOND 5
  21786. #define DUK__IDX_MILLISECOND 6
  21787. #define DUK__IDX_WEEKDAY 7 /* weekday: 0 to 6, 0=sunday, 1=monday, etc */
  21788. #define DUK__NUM_PARTS 8
  21789. /* Internal API call flags, used for various functions in this file.
  21790. * Certain flags are used by only certain functions, but since the flags
  21791. * don't overlap, a single flags value can be passed around to multiple
  21792. * functions.
  21793. *
  21794. * The unused top bits of the flags field are also used to pass values
  21795. * to helpers (duk__get_part_helper() and duk__set_part_helper()).
  21796. *
  21797. * (Must be in-sync with genbuiltins.py.)
  21798. */
  21799. #define DUK__FLAG_NAN_TO_ZERO (1 << 0) /* timeval breakdown: internal time value NaN -> zero */
  21800. #define DUK__FLAG_NAN_TO_RANGE_ERROR (1 << 1) /* timeval breakdown: internal time value NaN -> RangeError (toISOString) */
  21801. #define DUK__FLAG_ONEBASED (1 << 2) /* timeval breakdown: convert month and day-of-month parts to one-based (default is zero-based) */
  21802. #define DUK__FLAG_EQUIVYEAR (1 << 3) /* timeval breakdown: replace year with equivalent year in the [1971,2037] range for DST calculations */
  21803. #define DUK__FLAG_LOCALTIME (1 << 4) /* convert time value to local time */
  21804. #define DUK__FLAG_SUB1900 (1 << 5) /* getter: subtract 1900 from year when getting year part */
  21805. #define DUK__FLAG_TOSTRING_DATE (1 << 6) /* include date part in string conversion result */
  21806. #define DUK__FLAG_TOSTRING_TIME (1 << 7) /* include time part in string conversion result */
  21807. #define DUK__FLAG_TOSTRING_LOCALE (1 << 8) /* use locale specific formatting if available */
  21808. #define DUK__FLAG_TIMESETTER (1 << 9) /* setter: call is a time setter (affects hour, min, sec, ms); otherwise date setter (affects year, month, day-in-month) */
  21809. #define DUK__FLAG_YEAR_FIXUP (1 << 10) /* setter: perform 2-digit year fixup (00...99 -> 1900...1999) */
  21810. #define DUK__FLAG_SEP_T (1 << 11) /* string conversion: use 'T' instead of ' ' as a separator */
  21811. #define DUK__FLAG_VALUE_SHIFT 12 /* additional values begin at bit 12 */
  21812. /* Debug macro to print all parts and dparts (used manually because of debug level). */
  21813. #define DUK__DPRINT_PARTS_AND_DPARTS(parts,dparts) do { \
  21814. DUK_D(DUK_DPRINT("parts: %ld %ld %ld %ld %ld %ld %ld %ld, dparts: %lf %lf %lf %lf %lf %lf %lf %lf", \
  21815. (long) (parts)[0], (long) (parts)[1], \
  21816. (long) (parts)[2], (long) (parts)[3], \
  21817. (long) (parts)[4], (long) (parts)[5], \
  21818. (long) (parts)[6], (long) (parts)[7], \
  21819. (double) (dparts)[0], (double) (dparts)[1], \
  21820. (double) (dparts)[2], (double) (dparts)[3], \
  21821. (double) (dparts)[4], (double) (dparts)[5], \
  21822. (double) (dparts)[6], (double) (dparts)[7])); \
  21823. } while (0)
  21824. #define DUK__DPRINT_PARTS(parts) do { \
  21825. DUK_D(DUK_DPRINT("parts: %ld %ld %ld %ld %ld %ld %ld %ld", \
  21826. (long) (parts)[0], (long) (parts)[1], \
  21827. (long) (parts)[2], (long) (parts)[3], \
  21828. (long) (parts)[4], (long) (parts)[5], \
  21829. (long) (parts)[6], (long) (parts)[7])); \
  21830. } while (0)
  21831. #define DUK__DPRINT_DPARTS(dparts) do { \
  21832. DUK_D(DUK_DPRINT("dparts: %lf %lf %lf %lf %lf %lf %lf %lf", \
  21833. (double) (dparts)[0], (double) (dparts)[1], \
  21834. (double) (dparts)[2], (double) (dparts)[3], \
  21835. (double) (dparts)[4], (double) (dparts)[5], \
  21836. (double) (dparts)[6], (double) (dparts)[7])); \
  21837. } while (0)
  21838. /* Equivalent year for DST calculations outside [1970,2038[ range, see
  21839. * E5 Section 15.9.1.8. Equivalent year has the same leap-year-ness and
  21840. * starts with the same weekday on Jan 1.
  21841. * https://bugzilla.mozilla.org/show_bug.cgi?id=351066
  21842. */
  21843. #define DUK__YEAR(x) ((duk_uint8_t) ((x) - 1970))
  21844. DUK_LOCAL duk_uint8_t duk__date_equivyear[14] = {
  21845. #if 1
  21846. /* This is based on V8 EquivalentYear() algorithm (see src/genequivyear.py):
  21847. * http://code.google.com/p/v8/source/browse/trunk/src/date.h#146
  21848. */
  21849. /* non-leap year: sunday, monday, ... */
  21850. DUK__YEAR(2023), DUK__YEAR(2035), DUK__YEAR(2019), DUK__YEAR(2031),
  21851. DUK__YEAR(2015), DUK__YEAR(2027), DUK__YEAR(2011),
  21852. /* leap year: sunday, monday, ... */
  21853. DUK__YEAR(2012), DUK__YEAR(2024), DUK__YEAR(2008), DUK__YEAR(2020),
  21854. DUK__YEAR(2032), DUK__YEAR(2016), DUK__YEAR(2028)
  21855. #endif
  21856. #if 0
  21857. /* This is based on Rhino EquivalentYear() algorithm:
  21858. * https://github.com/mozilla/rhino/blob/f99cc11d616f0cdda2c42bde72b3484df6182947/src/org/mozilla/javascript/NativeDate.java
  21859. */
  21860. /* non-leap year: sunday, monday, ... */
  21861. DUK__YEAR(1978), DUK__YEAR(1973), DUK__YEAR(1985), DUK__YEAR(1986),
  21862. DUK__YEAR(1981), DUK__YEAR(1971), DUK__YEAR(1977),
  21863. /* leap year: sunday, monday, ... */
  21864. DUK__YEAR(1984), DUK__YEAR(1996), DUK__YEAR(1980), DUK__YEAR(1992),
  21865. DUK__YEAR(1976), DUK__YEAR(1988), DUK__YEAR(1972)
  21866. #endif
  21867. };
  21868. #undef DUK__YEAR
  21869. /*
  21870. * Platform specific helpers
  21871. */
  21872. #ifdef DUK_USE_DATE_NOW_GETTIMEOFDAY
  21873. /* Get current Ecmascript time (= UNIX/Posix time, but in milliseconds). */
  21874. DUK_INTERNAL duk_double_t duk_bi_date_get_now(duk_context *ctx) {
  21875. duk_hthread *thr = (duk_hthread *) ctx;
  21876. struct timeval tv;
  21877. duk_double_t d;
  21878. if (gettimeofday(&tv, NULL) != 0) {
  21879. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, "gettimeofday failed");
  21880. }
  21881. d = ((duk_double_t) tv.tv_sec) * 1000.0 +
  21882. ((duk_double_t) (tv.tv_usec / 1000));
  21883. DUK_ASSERT(DUK_FLOOR(d) == d); /* no fractions */
  21884. return d;
  21885. }
  21886. #endif /* DUK_USE_DATE_NOW_GETTIMEOFDAY */
  21887. #ifdef DUK_USE_DATE_NOW_TIME
  21888. /* Not a very good provider: only full seconds are available. */
  21889. DUK_INTERNAL duk_double_t duk_bi_date_get_now(duk_context *ctx) {
  21890. time_t t = time(NULL);
  21891. return ((duk_double_t) t) * 1000.0;
  21892. }
  21893. #endif /* DUK_USE_DATE_NOW_TIME */
  21894. #if defined(DUK_USE_DATE_NOW_WINDOWS) || defined(DUK_USE_DATE_TZO_WINDOWS)
  21895. /* Shared Windows helpers. */
  21896. DUK_LOCAL void duk__convert_systime_to_ularge(const SYSTEMTIME *st, ULARGE_INTEGER *res) {
  21897. FILETIME ft;
  21898. if (SystemTimeToFileTime(st, &ft) == 0) {
  21899. DUK_D(DUK_DPRINT("SystemTimeToFileTime() failed, returning 0"));
  21900. res->QuadPart = 0;
  21901. } else {
  21902. res->LowPart = ft.dwLowDateTime;
  21903. res->HighPart = ft.dwHighDateTime;
  21904. }
  21905. }
  21906. DUK_LOCAL void duk__set_systime_jan1970(SYSTEMTIME *st) {
  21907. DUK_MEMZERO((void *) st, sizeof(*st));
  21908. st->wYear = 1970;
  21909. st->wMonth = 1;
  21910. st->wDayOfWeek = 4; /* not sure whether or not needed; Thursday */
  21911. st->wDay = 1;
  21912. DUK_ASSERT(st->wHour == 0);
  21913. DUK_ASSERT(st->wMinute == 0);
  21914. DUK_ASSERT(st->wSecond == 0);
  21915. DUK_ASSERT(st->wMilliseconds == 0);
  21916. }
  21917. #endif /* defined(DUK_USE_DATE_NOW_WINDOWS) || defined(DUK_USE_DATE_TZO_WINDOWS) */
  21918. #ifdef DUK_USE_DATE_NOW_WINDOWS
  21919. DUK_INTERNAL duk_double_t duk_bi_date_get_now(duk_context *ctx) {
  21920. /* Suggested step-by-step method from documentation of RtlTimeToSecondsSince1970:
  21921. * http://msdn.microsoft.com/en-us/library/windows/desktop/ms724928(v=vs.85).aspx
  21922. */
  21923. SYSTEMTIME st1, st2;
  21924. ULARGE_INTEGER tmp1, tmp2;
  21925. DUK_UNREF(ctx);
  21926. GetSystemTime(&st1);
  21927. duk__convert_systime_to_ularge((const SYSTEMTIME *) &st1, &tmp1);
  21928. duk__set_systime_jan1970(&st2);
  21929. duk__convert_systime_to_ularge((const SYSTEMTIME *) &st2, &tmp2);
  21930. /* Difference is in 100ns units, convert to milliseconds w/o fractions */
  21931. return (duk_double_t) ((tmp1.QuadPart - tmp2.QuadPart) / 10000LL);
  21932. }
  21933. #endif /* DUK_USE_DATE_NOW_WINDOWS */
  21934. #if defined(DUK_USE_DATE_TZO_GMTIME) || defined(DUK_USE_DATE_TZO_GMTIME_R)
  21935. /* Get local time offset (in seconds) for a certain (UTC) instant 'd'. */
  21936. DUK_LOCAL duk_int_t duk__get_local_tzoffset(duk_double_t d) {
  21937. time_t t, t1, t2;
  21938. duk_int_t parts[DUK__NUM_PARTS];
  21939. duk_double_t dparts[DUK__NUM_PARTS];
  21940. struct tm tms[2];
  21941. #ifdef DUK_USE_DATE_TZO_GMTIME
  21942. struct tm *tm_ptr;
  21943. #endif
  21944. /* For NaN/inf, the return value doesn't matter. */
  21945. if (!DUK_ISFINITE(d)) {
  21946. return 0;
  21947. }
  21948. /* If not within Ecmascript range, some integer time calculations
  21949. * won't work correctly (and some asserts will fail), so bail out
  21950. * if so. This fixes test-bug-date-insane-setyear.js. There is
  21951. * a +/- 24h leeway in this range check to avoid a test262 corner
  21952. * case documented in test-bug-date-timeval-edges.js.
  21953. */
  21954. if (!duk__timeval_in_leeway_range(d)) {
  21955. DUK_DD(DUK_DDPRINT("timeval not within valid range, skip tzoffset computation to avoid integer overflows"));
  21956. return 0;
  21957. }
  21958. /*
  21959. * This is a bit tricky to implement portably. The result depends
  21960. * on the timestamp (specifically, DST depends on the timestamp).
  21961. * If e.g. UNIX APIs are used, they'll have portability issues with
  21962. * very small and very large years.
  21963. *
  21964. * Current approach:
  21965. *
  21966. * - Stay within portable UNIX limits by using equivalent year mapping.
  21967. * Avoid year 1970 and 2038 as some conversions start to fail, at
  21968. * least on some platforms. Avoiding 1970 means that there are
  21969. * currently DST discrepancies for 1970.
  21970. *
  21971. * - Create a UTC and local time breakdowns from 't'. Then create
  21972. * a time_t using gmtime() and localtime() and compute the time
  21973. * difference between the two.
  21974. *
  21975. * Equivalent year mapping (E5 Section 15.9.1.8):
  21976. *
  21977. * If the host environment provides functionality for determining
  21978. * daylight saving time, the implementation of ECMAScript is free
  21979. * to map the year in question to an equivalent year (same
  21980. * leap-year-ness and same starting week day for the year) for which
  21981. * the host environment provides daylight saving time information.
  21982. * The only restriction is that all equivalent years should produce
  21983. * the same result.
  21984. *
  21985. * This approach is quite reasonable but not entirely correct, e.g.
  21986. * the specification also states (E5 Section 15.9.1.8):
  21987. *
  21988. * The implementation of ECMAScript should not try to determine
  21989. * whether the exact time was subject to daylight saving time, but
  21990. * just whether daylight saving time would have been in effect if
  21991. * the _current daylight saving time algorithm_ had been used at the
  21992. * time. This avoids complications such as taking into account the
  21993. * years that the locale observed daylight saving time year round.
  21994. *
  21995. * Since we rely on the platform APIs for conversions between local
  21996. * time and UTC, we can't guarantee the above. Rather, if the platform
  21997. * has historical DST rules they will be applied. This seems to be the
  21998. * general preferred direction in Ecmascript standardization (or at least
  21999. * implementations) anyway, and even the equivalent year mapping should
  22000. * be disabled if the platform is known to handle DST properly for the
  22001. * full Ecmascript range.
  22002. *
  22003. * The following has useful discussion and links:
  22004. *
  22005. * https://bugzilla.mozilla.org/show_bug.cgi?id=351066
  22006. */
  22007. duk__timeval_to_parts(d, parts, dparts, DUK__FLAG_EQUIVYEAR /*flags*/);
  22008. DUK_ASSERT(parts[DUK__IDX_YEAR] >= 1970 && parts[DUK__IDX_YEAR] <= 2038);
  22009. d = duk__get_timeval_from_dparts(dparts, 0 /*flags*/);
  22010. DUK_ASSERT(d >= 0 && d < 2147483648.0 * 1000.0); /* unsigned 31-bit range */
  22011. t = (time_t) (d / 1000.0);
  22012. DUK_DDD(DUK_DDDPRINT("timeval: %lf -> time_t %ld", (double) d, (long) t));
  22013. t1 = t;
  22014. DUK_MEMZERO((void *) tms, sizeof(struct tm) * 2);
  22015. #if defined(DUK_USE_DATE_TZO_GMTIME_R)
  22016. (void) gmtime_r(&t, &tms[0]);
  22017. (void) localtime_r(&t, &tms[1]);
  22018. #elif defined(DUK_USE_DATE_TZO_GMTIME)
  22019. tm_ptr = gmtime(&t);
  22020. DUK_MEMCPY((void *) &tms[0], tm_ptr, sizeof(struct tm));
  22021. tm_ptr = localtime(&t);
  22022. DUK_MEMCPY((void *) &tms[1], tm_ptr, sizeof(struct tm));
  22023. #else
  22024. #error internal error
  22025. #endif
  22026. DUK_DDD(DUK_DDDPRINT("gmtime result: tm={sec:%ld,min:%ld,hour:%ld,mday:%ld,mon:%ld,year:%ld,"
  22027. "wday:%ld,yday:%ld,isdst:%ld}",
  22028. (long) tms[0].tm_sec, (long) tms[0].tm_min, (long) tms[0].tm_hour,
  22029. (long) tms[0].tm_mday, (long) tms[0].tm_mon, (long) tms[0].tm_year,
  22030. (long) tms[0].tm_wday, (long) tms[0].tm_yday, (long) tms[0].tm_isdst));
  22031. DUK_DDD(DUK_DDDPRINT("localtime result: tm={sec:%ld,min:%ld,hour:%ld,mday:%ld,mon:%ld,year:%ld,"
  22032. "wday:%ld,yday:%ld,isdst:%ld}",
  22033. (long) tms[1].tm_sec, (long) tms[1].tm_min, (long) tms[1].tm_hour,
  22034. (long) tms[1].tm_mday, (long) tms[1].tm_mon, (long) tms[1].tm_year,
  22035. (long) tms[1].tm_wday, (long) tms[1].tm_yday, (long) tms[1].tm_isdst));
  22036. t1 = mktime(&tms[0]); /* UTC */
  22037. t2 = mktime(&tms[1]); /* local */
  22038. if (t1 == (time_t) -1 || t2 == (time_t) -1) {
  22039. /* This check used to be for (t < 0) but on some platforms
  22040. * time_t is unsigned and apparently the proper way to detect
  22041. * an mktime() error return is the cast above. See e.g.:
  22042. * http://pubs.opengroup.org/onlinepubs/009695299/functions/mktime.html
  22043. */
  22044. goto error;
  22045. }
  22046. if (tms[1].tm_isdst > 0) {
  22047. t2 += 3600;
  22048. } else if (tms[1].tm_isdst < 0) {
  22049. DUK_D(DUK_DPRINT("tm_isdst is negative: %d", (int) tms[1].tm_isdst));
  22050. }
  22051. DUK_DDD(DUK_DDDPRINT("t1=%ld (utc), t2=%ld (local)", (long) t1, (long) t2));
  22052. /* Compute final offset in seconds, positive if local time ahead of
  22053. * UTC (returned value is UTC-to-local offset).
  22054. *
  22055. * difftime() returns a double, so coercion to int generates quite
  22056. * a lot of code. Direct subtraction is not portable, however.
  22057. * XXX: allow direct subtraction on known platforms.
  22058. */
  22059. #if 0
  22060. return (duk_int_t) (t2 - t1);
  22061. #endif
  22062. return (duk_int_t) difftime(t2, t1);
  22063. error:
  22064. /* XXX: return something more useful, so that caller can throw? */
  22065. DUK_D(DUK_DPRINT("mktime() failed, d=%lf", (double) d));
  22066. return 0;
  22067. }
  22068. #endif /* DUK_USE_DATE_TZO_GMTIME */
  22069. #if defined(DUK_USE_DATE_TZO_WINDOWS)
  22070. DUK_LOCAL duk_int_t duk__get_local_tzoffset(duk_double_t d) {
  22071. SYSTEMTIME st1;
  22072. SYSTEMTIME st2;
  22073. SYSTEMTIME st3;
  22074. ULARGE_INTEGER tmp1;
  22075. ULARGE_INTEGER tmp2;
  22076. ULARGE_INTEGER tmp3;
  22077. FILETIME ft1;
  22078. /* XXX: handling of timestamps outside Windows supported range.
  22079. * How does Windows deal with dates before 1600? Does windows
  22080. * support all Ecmascript years (like -200000 and +200000)?
  22081. * Should equivalent year mapping be used here too? If so, use
  22082. * a shared helper (currently integrated into timeval-to-parts).
  22083. */
  22084. /* Use the approach described in "Remarks" of FileTimeToLocalFileTime:
  22085. * http://msdn.microsoft.com/en-us/library/windows/desktop/ms724277(v=vs.85).aspx
  22086. */
  22087. duk__set_systime_jan1970(&st1);
  22088. duk__convert_systime_to_ularge((const SYSTEMTIME *) &st1, &tmp1);
  22089. tmp2.QuadPart = (ULONGLONG) (d * 10000.0); /* millisec -> 100ns units since jan 1, 1970 */
  22090. tmp2.QuadPart += tmp1.QuadPart; /* input 'd' in Windows UTC, 100ns units */
  22091. ft1.dwLowDateTime = tmp2.LowPart;
  22092. ft1.dwHighDateTime = tmp2.HighPart;
  22093. FileTimeToSystemTime((const FILETIME *) &ft1, &st2);
  22094. if (SystemTimeToTzSpecificLocalTime((LPTIME_ZONE_INFORMATION) NULL, &st2, &st3) == 0) {
  22095. DUK_D(DUK_DPRINT("SystemTimeToTzSpecificLocalTime() failed, return tzoffset 0"));
  22096. return 0;
  22097. }
  22098. duk__convert_systime_to_ularge((const SYSTEMTIME *) &st3, &tmp3);
  22099. /* Positive if local time ahead of UTC. */
  22100. return (duk_int_t) (((LONGLONG) tmp3.QuadPart - (LONGLONG) tmp2.QuadPart) / 10000000LL); /* seconds */
  22101. }
  22102. #endif /* DUK_USE_DATE_TZO_WINDOWS */
  22103. #ifdef DUK_USE_DATE_PRS_STRPTIME
  22104. DUK_LOCAL duk_bool_t duk__parse_string_strptime(duk_context *ctx, const char *str) {
  22105. struct tm tm;
  22106. time_t t;
  22107. char buf[DUK__STRPTIME_BUF_SIZE];
  22108. /* copy to buffer with spare to avoid Valgrind gripes from strptime */
  22109. DUK_ASSERT(str != NULL);
  22110. DUK_MEMZERO(buf, sizeof(buf)); /* valgrind whine without this */
  22111. DUK_SNPRINTF(buf, sizeof(buf), "%s", (const char *) str);
  22112. buf[sizeof(buf) - 1] = (char) 0;
  22113. DUK_DDD(DUK_DDDPRINT("parsing: '%s'", (const char *) buf));
  22114. DUK_MEMZERO(&tm, sizeof(tm));
  22115. if (strptime((const char *) buf, "%c", &tm) != NULL) {
  22116. DUK_DDD(DUK_DDDPRINT("before mktime: tm={sec:%ld,min:%ld,hour:%ld,mday:%ld,mon:%ld,year:%ld,"
  22117. "wday:%ld,yday:%ld,isdst:%ld}",
  22118. (long) tm.tm_sec, (long) tm.tm_min, (long) tm.tm_hour,
  22119. (long) tm.tm_mday, (long) tm.tm_mon, (long) tm.tm_year,
  22120. (long) tm.tm_wday, (long) tm.tm_yday, (long) tm.tm_isdst));
  22121. tm.tm_isdst = -1; /* negative: dst info not available */
  22122. t = mktime(&tm);
  22123. DUK_DDD(DUK_DDDPRINT("mktime() -> %ld", (long) t));
  22124. if (t >= 0) {
  22125. duk_push_number(ctx, ((duk_double_t) t) * 1000.0);
  22126. return 1;
  22127. }
  22128. }
  22129. return 0;
  22130. }
  22131. #endif /* DUK_USE_DATE_PRS_STRPTIME */
  22132. #ifdef DUK_USE_DATE_PRS_GETDATE
  22133. DUK_LOCAL duk_bool_t duk__parse_string_getdate(duk_context *ctx, const char *str) {
  22134. struct tm tm;
  22135. duk_small_int_t rc;
  22136. time_t t;
  22137. /* For this to work, DATEMSK must be set, so this is not very
  22138. * convenient for an embeddable interpreter.
  22139. */
  22140. DUK_MEMZERO(&tm, sizeof(struct tm));
  22141. rc = (duk_small_int_t) getdate_r(str, &tm);
  22142. DUK_DDD(DUK_DDDPRINT("getdate_r() -> %ld", (long) rc));
  22143. if (rc == 0) {
  22144. t = mktime(&tm);
  22145. DUK_DDD(DUK_DDDPRINT("mktime() -> %ld", (long) t));
  22146. if (t >= 0) {
  22147. duk_push_number(ctx, (duk_double_t) t);
  22148. return 1;
  22149. }
  22150. }
  22151. return 0;
  22152. }
  22153. #endif /* DUK_USE_DATE_PRS_GETDATE */
  22154. #ifdef DUK_USE_DATE_FMT_STRFTIME
  22155. DUK_LOCAL duk_bool_t duk__format_parts_strftime(duk_context *ctx, duk_int_t *parts, duk_int_t tzoffset, duk_small_uint_t flags) {
  22156. char buf[DUK__STRFTIME_BUF_SIZE];
  22157. struct tm tm;
  22158. const char *fmt;
  22159. DUK_UNREF(tzoffset);
  22160. /* If the platform doesn't support the entire Ecmascript range, we need
  22161. * to return 0 so that the caller can fall back to the default formatter.
  22162. *
  22163. * For now, assume that if time_t is 8 bytes or more, the whole Ecmascript
  22164. * range is supported. For smaller time_t values (4 bytes in practice),
  22165. * assumes that the signed 32-bit range is supported.
  22166. *
  22167. * XXX: detect this more correctly per platform. The size of time_t is
  22168. * probably not an accurate guarantee of strftime() supporting or not
  22169. * supporting a large time range (the full Ecmascript range).
  22170. */
  22171. if (sizeof(time_t) < 8 &&
  22172. (parts[DUK__IDX_YEAR] < 1970 || parts[DUK__IDX_YEAR] > 2037)) {
  22173. /* be paranoid for 32-bit time values (even avoiding negative ones) */
  22174. return 0;
  22175. }
  22176. DUK_MEMZERO(&tm, sizeof(tm));
  22177. tm.tm_sec = parts[DUK__IDX_SECOND];
  22178. tm.tm_min = parts[DUK__IDX_MINUTE];
  22179. tm.tm_hour = parts[DUK__IDX_HOUR];
  22180. tm.tm_mday = parts[DUK__IDX_DAY]; /* already one-based */
  22181. tm.tm_mon = parts[DUK__IDX_MONTH] - 1; /* one-based -> zero-based */
  22182. tm.tm_year = parts[DUK__IDX_YEAR] - 1900;
  22183. tm.tm_wday = parts[DUK__IDX_WEEKDAY];
  22184. tm.tm_isdst = 0;
  22185. DUK_MEMZERO(buf, sizeof(buf));
  22186. if ((flags & DUK__FLAG_TOSTRING_DATE) && (flags & DUK__FLAG_TOSTRING_TIME)) {
  22187. fmt = "%c";
  22188. } else if (flags & DUK__FLAG_TOSTRING_DATE) {
  22189. fmt = "%x";
  22190. } else {
  22191. DUK_ASSERT(flags & DUK__FLAG_TOSTRING_TIME);
  22192. fmt = "%X";
  22193. }
  22194. (void) strftime(buf, sizeof(buf) - 1, fmt, &tm);
  22195. DUK_ASSERT(buf[sizeof(buf) - 1] == 0);
  22196. duk_push_string(ctx, buf);
  22197. return 1;
  22198. }
  22199. #endif /* DUK_USE_DATE_FMT_STRFTIME */
  22200. /*
  22201. * ISO 8601 subset parser.
  22202. */
  22203. /* Parser part count. */
  22204. #define DUK__NUM_ISO8601_PARSER_PARTS 9
  22205. /* Parser part indices. */
  22206. #define DUK__PI_YEAR 0
  22207. #define DUK__PI_MONTH 1
  22208. #define DUK__PI_DAY 2
  22209. #define DUK__PI_HOUR 3
  22210. #define DUK__PI_MINUTE 4
  22211. #define DUK__PI_SECOND 5
  22212. #define DUK__PI_MILLISECOND 6
  22213. #define DUK__PI_TZHOUR 7
  22214. #define DUK__PI_TZMINUTE 8
  22215. /* Parser part masks. */
  22216. #define DUK__PM_YEAR (1 << DUK__PI_YEAR)
  22217. #define DUK__PM_MONTH (1 << DUK__PI_MONTH)
  22218. #define DUK__PM_DAY (1 << DUK__PI_DAY)
  22219. #define DUK__PM_HOUR (1 << DUK__PI_HOUR)
  22220. #define DUK__PM_MINUTE (1 << DUK__PI_MINUTE)
  22221. #define DUK__PM_SECOND (1 << DUK__PI_SECOND)
  22222. #define DUK__PM_MILLISECOND (1 << DUK__PI_MILLISECOND)
  22223. #define DUK__PM_TZHOUR (1 << DUK__PI_TZHOUR)
  22224. #define DUK__PM_TZMINUTE (1 << DUK__PI_TZMINUTE)
  22225. /* Parser separator indices. */
  22226. #define DUK__SI_PLUS 0
  22227. #define DUK__SI_MINUS 1
  22228. #define DUK__SI_T 2
  22229. #define DUK__SI_SPACE 3
  22230. #define DUK__SI_COLON 4
  22231. #define DUK__SI_PERIOD 5
  22232. #define DUK__SI_Z 6
  22233. #define DUK__SI_NUL 7
  22234. /* Parser separator masks. */
  22235. #define DUK__SM_PLUS (1 << DUK__SI_PLUS)
  22236. #define DUK__SM_MINUS (1 << DUK__SI_MINUS)
  22237. #define DUK__SM_T (1 << DUK__SI_T)
  22238. #define DUK__SM_SPACE (1 << DUK__SI_SPACE)
  22239. #define DUK__SM_COLON (1 << DUK__SI_COLON)
  22240. #define DUK__SM_PERIOD (1 << DUK__SI_PERIOD)
  22241. #define DUK__SM_Z (1 << DUK__SI_Z)
  22242. #define DUK__SM_NUL (1 << DUK__SI_NUL)
  22243. /* Rule control flags. */
  22244. #define DUK__CF_NEG (1 << 0) /* continue matching, set neg_tzoffset flag */
  22245. #define DUK__CF_ACCEPT (1 << 1) /* accept string */
  22246. #define DUK__CF_ACCEPT_NUL (1 << 2) /* accept string if next char is NUL (otherwise reject) */
  22247. #define DUK__PACK_RULE(partmask,sepmask,nextpart,flags) \
  22248. ((duk_uint32_t) (partmask) + \
  22249. (((duk_uint32_t) (sepmask)) << 9) + \
  22250. (((duk_uint32_t) (nextpart)) << 17) + \
  22251. (((duk_uint32_t) (flags)) << 21))
  22252. #define DUK__UNPACK_RULE(rule,var_nextidx,var_flags) do { \
  22253. (var_nextidx) = (duk_small_uint_t) (((rule) >> 17) & 0x0f); \
  22254. (var_flags) = (duk_small_uint_t) ((rule) >> 21); \
  22255. } while (0)
  22256. #define DUK__RULE_MASK_PART_SEP 0x1ffffUL
  22257. /* Matching separator index is used in the control table */
  22258. DUK_LOCAL const duk_uint8_t duk__parse_iso8601_seps[] = {
  22259. DUK_ASC_PLUS /*0*/, DUK_ASC_MINUS /*1*/, DUK_ASC_UC_T /*2*/, DUK_ASC_SPACE /*3*/,
  22260. DUK_ASC_COLON /*4*/, DUK_ASC_PERIOD /*5*/, DUK_ASC_UC_Z /*6*/, DUK_ASC_NUL /*7*/
  22261. };
  22262. /* Rule table: first matching rule is used to determine what to do next. */
  22263. DUK_LOCAL const duk_uint32_t duk__parse_iso8601_control[] = {
  22264. DUK__PACK_RULE(DUK__PM_YEAR, DUK__SM_MINUS, DUK__PI_MONTH, 0),
  22265. DUK__PACK_RULE(DUK__PM_MONTH, DUK__SM_MINUS, DUK__PI_DAY, 0),
  22266. DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY, DUK__SM_T | DUK__SM_SPACE, DUK__PI_HOUR, 0),
  22267. DUK__PACK_RULE(DUK__PM_HOUR, DUK__SM_COLON, DUK__PI_MINUTE, 0),
  22268. DUK__PACK_RULE(DUK__PM_MINUTE, DUK__SM_COLON, DUK__PI_SECOND, 0),
  22269. DUK__PACK_RULE(DUK__PM_SECOND, DUK__SM_PERIOD, DUK__PI_MILLISECOND, 0),
  22270. DUK__PACK_RULE(DUK__PM_TZHOUR, DUK__SM_COLON, DUK__PI_TZMINUTE, 0),
  22271. 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),
  22272. 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),
  22273. 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),
  22274. 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)
  22275. /* Note1: the specification doesn't require matching a time form with
  22276. * just hours ("HH"), but we accept it here, e.g. "2012-01-02T12Z".
  22277. *
  22278. * Note2: the specification doesn't require matching a timezone offset
  22279. * with just hours ("HH"), but accept it here, e.g. "2012-01-02T03:04:05+02"
  22280. */
  22281. };
  22282. DUK_LOCAL duk_bool_t duk__parse_string_iso8601_subset(duk_context *ctx, const char *str) {
  22283. duk_int_t parts[DUK__NUM_ISO8601_PARSER_PARTS];
  22284. duk_double_t dparts[DUK__NUM_PARTS];
  22285. duk_double_t d;
  22286. const duk_uint8_t *p;
  22287. duk_small_uint_t part_idx = 0;
  22288. duk_int_t accum = 0;
  22289. duk_small_uint_t ndigits = 0;
  22290. duk_bool_t neg_year = 0;
  22291. duk_bool_t neg_tzoffset = 0;
  22292. duk_uint_fast8_t ch;
  22293. duk_small_uint_t i;
  22294. /* During parsing, month and day are one-based; set defaults here. */
  22295. DUK_MEMZERO(parts, sizeof(parts));
  22296. DUK_ASSERT(parts[DUK__IDX_YEAR] == 0); /* don't care value, year is mandatory */
  22297. parts[DUK__IDX_MONTH] = 1;
  22298. parts[DUK__IDX_DAY] = 1;
  22299. /* Special handling for year sign. */
  22300. p = (const duk_uint8_t *) str;
  22301. ch = p[0];
  22302. if (ch == DUK_ASC_PLUS) {
  22303. p++;
  22304. } else if (ch == DUK_ASC_MINUS) {
  22305. neg_year = 1;
  22306. p++;
  22307. }
  22308. for (;;) {
  22309. ch = *p++;
  22310. DUK_DDD(DUK_DDDPRINT("parsing, part_idx=%ld, char=%ld ('%c')",
  22311. (long) part_idx, (long) ch,
  22312. (int) ((ch >= 0x20 && ch <= 0x7e) ? ch : DUK_ASC_QUESTION)));
  22313. if (ch >= DUK_ASC_0 && ch <= DUK_ASC_9) {
  22314. if (ndigits >= 9) {
  22315. DUK_DDD(DUK_DDDPRINT("too many digits -> reject"));
  22316. goto reject;
  22317. }
  22318. if (part_idx == DUK__PI_MILLISECOND /*msec*/ && ndigits >= 3) {
  22319. /* ignore millisecond fractions after 3 */
  22320. } else {
  22321. accum = accum * 10 + ((duk_int_t) ch) - ((duk_int_t) DUK_ASC_0) + 0x00;
  22322. ndigits++;
  22323. }
  22324. } else {
  22325. duk_uint_fast32_t match_val;
  22326. duk_small_int_t sep_idx;
  22327. if (ndigits <= 0) {
  22328. goto reject;
  22329. }
  22330. if (part_idx == DUK__PI_MILLISECOND) {
  22331. /* complete the millisecond field */
  22332. while (ndigits < 3) {
  22333. accum *= 10;
  22334. ndigits++;
  22335. }
  22336. }
  22337. parts[part_idx] = accum;
  22338. DUK_DDD(DUK_DDDPRINT("wrote part %ld -> value %ld", (long) part_idx, (long) accum));
  22339. accum = 0;
  22340. ndigits = 0;
  22341. for (i = 0; i < (duk_small_uint_t) (sizeof(duk__parse_iso8601_seps) / sizeof(duk_uint8_t)); i++) {
  22342. if (duk__parse_iso8601_seps[i] == ch) {
  22343. break;
  22344. }
  22345. }
  22346. if (i == (duk_small_uint_t) (sizeof(duk__parse_iso8601_seps) / sizeof(duk_uint8_t))) {
  22347. DUK_DDD(DUK_DDDPRINT("separator character doesn't match -> reject"));
  22348. goto reject;
  22349. }
  22350. sep_idx = i;
  22351. match_val = (1UL << part_idx) + (1UL << (sep_idx + 9)); /* match against rule part/sep bits */
  22352. for (i = 0; i < (duk_small_uint_t) (sizeof(duk__parse_iso8601_control) / sizeof(duk_uint32_t)); i++) {
  22353. duk_uint_fast32_t rule = duk__parse_iso8601_control[i];
  22354. duk_small_uint_t nextpart;
  22355. duk_small_uint_t cflags;
  22356. DUK_DDD(DUK_DDDPRINT("part_idx=%ld, sep_idx=%ld, match_val=0x%08lx, considering rule=0x%08lx",
  22357. (long) part_idx, (long) sep_idx,
  22358. (unsigned long) match_val, (unsigned long) rule));
  22359. if ((rule & match_val) != match_val) {
  22360. continue;
  22361. }
  22362. DUK__UNPACK_RULE(rule, nextpart, cflags);
  22363. DUK_DDD(DUK_DDDPRINT("rule match -> part_idx=%ld, sep_idx=%ld, match_val=0x%08lx, "
  22364. "rule=0x%08lx -> nextpart=%ld, cflags=0x%02lx",
  22365. (long) part_idx, (long) sep_idx,
  22366. (unsigned long) match_val, (unsigned long) rule,
  22367. (long) nextpart, (unsigned long) cflags));
  22368. if (cflags & DUK__CF_NEG) {
  22369. neg_tzoffset = 1;
  22370. }
  22371. if (cflags & DUK__CF_ACCEPT) {
  22372. goto accept;
  22373. }
  22374. if (cflags & DUK__CF_ACCEPT_NUL) {
  22375. DUK_ASSERT(*(p - 1) != (char) 0);
  22376. if (*p == DUK_ASC_NUL) {
  22377. goto accept;
  22378. }
  22379. goto reject;
  22380. }
  22381. part_idx = nextpart;
  22382. break;
  22383. } /* rule match */
  22384. if (i == (duk_small_uint_t) (sizeof(duk__parse_iso8601_control) / sizeof(duk_uint32_t))) {
  22385. DUK_DDD(DUK_DDDPRINT("no rule matches -> reject"));
  22386. goto reject;
  22387. }
  22388. if (ch == 0) {
  22389. /* This shouldn't be necessary, but check just in case
  22390. * to avoid any chance of overruns.
  22391. */
  22392. DUK_DDD(DUK_DDDPRINT("NUL after rule matching (should not happen) -> reject"));
  22393. goto reject;
  22394. }
  22395. } /* if-digit-else-ctrl */
  22396. } /* char loop */
  22397. /* We should never exit the loop above, but if we do, reject
  22398. * by falling through.
  22399. */
  22400. DUK_DDD(DUK_DDDPRINT("fell out of char loop without explicit accept/reject -> reject"));
  22401. reject:
  22402. DUK_DDD(DUK_DDDPRINT("reject"));
  22403. return 0;
  22404. accept:
  22405. DUK_DDD(DUK_DDDPRINT("accept"));
  22406. /* Apply timezone offset to get the main parts in UTC */
  22407. if (neg_year) {
  22408. parts[DUK__PI_YEAR] = -parts[DUK__PI_YEAR];
  22409. }
  22410. if (neg_tzoffset) {
  22411. parts[DUK__PI_HOUR] += parts[DUK__PI_TZHOUR];
  22412. parts[DUK__PI_MINUTE] += parts[DUK__PI_TZMINUTE];
  22413. } else {
  22414. parts[DUK__PI_HOUR] -= parts[DUK__PI_TZHOUR];
  22415. parts[DUK__PI_MINUTE] -= parts[DUK__PI_TZMINUTE];
  22416. }
  22417. parts[DUK__PI_MONTH] -= 1; /* zero-based month */
  22418. parts[DUK__PI_DAY] -= 1; /* zero-based day */
  22419. /* Use double parts, they tolerate unnormalized time.
  22420. *
  22421. * Note: DUK__IDX_WEEKDAY is initialized with a bogus value (DUK__PI_TZHOUR)
  22422. * on purpose. It won't be actually used by duk__get_timeval_from_dparts(),
  22423. * but will make the value initialized just in case, and avoid any
  22424. * potential for Valgrind issues.
  22425. */
  22426. for (i = 0; i < DUK__NUM_PARTS; i++) {
  22427. DUK_DDD(DUK_DDDPRINT("part[%ld] = %ld", (long) i, (long) parts[i]));
  22428. dparts[i] = parts[i];
  22429. }
  22430. d = duk__get_timeval_from_dparts(dparts, 0 /*flags*/);
  22431. duk_push_number(ctx, d);
  22432. return 1;
  22433. }
  22434. /*
  22435. * Date/time parsing helper.
  22436. *
  22437. * Parse a datetime string into a time value. We must first try to parse
  22438. * the input according to the standard format in E5.1 Section 15.9.1.15.
  22439. * If that fails, we can try to parse using custom parsing, which can
  22440. * either be platform neutral (custom code) or platform specific (using
  22441. * existing platform API calls).
  22442. *
  22443. * Note in particular that we must parse whatever toString(), toUTCString(),
  22444. * and toISOString() can produce; see E5.1 Section 15.9.4.2.
  22445. *
  22446. * Returns 1 to allow tailcalling.
  22447. *
  22448. * There is much room for improvement here with respect to supporting
  22449. * alternative datetime formats. For instance, V8 parses '2012-01-01' as
  22450. * UTC and '2012/01/01' as local time.
  22451. */
  22452. DUK_LOCAL duk_ret_t duk__parse_string(duk_context *ctx, const char *str) {
  22453. /* XXX: there is a small risk here: because the ISO 8601 parser is
  22454. * very loose, it may end up parsing some datetime values which
  22455. * would be better parsed with a platform specific parser.
  22456. */
  22457. DUK_ASSERT(str != NULL);
  22458. DUK_DDD(DUK_DDDPRINT("parse datetime from string '%s'", (const char *) str));
  22459. if (duk__parse_string_iso8601_subset(ctx, str) != 0) {
  22460. return 1;
  22461. }
  22462. #if defined(DUK_USE_DATE_PRS_STRPTIME)
  22463. if (duk__parse_string_strptime(ctx, str) != 0) {
  22464. return 1;
  22465. }
  22466. #elif defined(DUK_USE_DATE_PRS_GETDATE)
  22467. if (duk__parse_string_getdate(ctx, str) != 0) {
  22468. return 1;
  22469. }
  22470. #else
  22471. /* No platform-specific parsing, this is not an error. */
  22472. #endif
  22473. duk_push_nan(ctx);
  22474. return 1;
  22475. }
  22476. /*
  22477. * Calendar helpers
  22478. *
  22479. * Some helpers are used for getters and can operate on normalized values
  22480. * which can be represented with 32-bit signed integers. Other helpers are
  22481. * needed by setters and operate on un-normalized double values, must watch
  22482. * out for non-finite numbers etc.
  22483. */
  22484. DUK_LOCAL duk_uint8_t duk__days_in_month[12] = {
  22485. (duk_uint8_t) 31, (duk_uint8_t) 28, (duk_uint8_t) 31, (duk_uint8_t) 30,
  22486. (duk_uint8_t) 31, (duk_uint8_t) 30, (duk_uint8_t) 31, (duk_uint8_t) 31,
  22487. (duk_uint8_t) 30, (duk_uint8_t) 31, (duk_uint8_t) 30, (duk_uint8_t) 31
  22488. };
  22489. /* Maximum iteration count for computing UTC-to-local time offset when
  22490. * creating an Ecmascript time value from local parts.
  22491. */
  22492. #define DUK__LOCAL_TZOFFSET_MAXITER 4
  22493. /* Because 'day since epoch' can be negative and is used to compute weekday
  22494. * using a modulo operation, add this multiple of 7 to avoid negative values
  22495. * when year is below 1970 epoch. Ecmascript time values are restricted to
  22496. * +/- 100 million days from epoch, so this adder fits nicely into 32 bits.
  22497. * Round to a multiple of 7 (= floor(100000000 / 7) * 7) and add margin.
  22498. */
  22499. #define DUK__WEEKDAY_MOD_ADDER (20000000 * 7) /* 0x08583b00 */
  22500. DUK_LOCAL duk_bool_t duk__is_leap_year(duk_int_t year) {
  22501. if ((year % 4) != 0) {
  22502. return 0;
  22503. }
  22504. if ((year % 100) != 0) {
  22505. return 1;
  22506. }
  22507. if ((year % 400) != 0) {
  22508. return 0;
  22509. }
  22510. return 1;
  22511. }
  22512. DUK_LOCAL duk_bool_t duk__timeval_in_valid_range(duk_double_t x) {
  22513. return (x >= -DUK__MS_100M_DAYS && x <= DUK__MS_100M_DAYS);
  22514. }
  22515. DUK_LOCAL duk_bool_t duk__timeval_in_leeway_range(duk_double_t x) {
  22516. return (x >= -DUK__MS_100M_DAYS_LEEWAY && x <= DUK__MS_100M_DAYS_LEEWAY);
  22517. }
  22518. DUK_LOCAL duk_bool_t duk__year_in_valid_range(duk_double_t x) {
  22519. return (x >= DUK__MIN_ECMA_YEAR && x <= DUK__MAX_ECMA_YEAR);
  22520. }
  22521. DUK_LOCAL duk_double_t duk__timeclip(duk_double_t x) {
  22522. if (!DUK_ISFINITE(x)) {
  22523. return DUK_DOUBLE_NAN;
  22524. }
  22525. if (!duk__timeval_in_valid_range(x)) {
  22526. return DUK_DOUBLE_NAN;
  22527. }
  22528. x = duk_js_tointeger_number(x);
  22529. /* Here we'd have the option to normalize -0 to +0. */
  22530. return x;
  22531. }
  22532. /* Integer division which floors also negative values correctly. */
  22533. DUK_LOCAL duk_int_t duk__div_floor(duk_int_t a, duk_int_t b) {
  22534. DUK_ASSERT(b > 0);
  22535. if (a >= 0) {
  22536. return a / b;
  22537. } else {
  22538. /* e.g. a = -4, b = 5 --> -4 - 5 + 1 / 5 --> -8 / 5 --> -1
  22539. * a = -5, b = 5 --> -5 - 5 + 1 / 5 --> -9 / 5 --> -1
  22540. * a = -6, b = 5 --> -6 - 5 + 1 / 5 --> -10 / 5 --> -2
  22541. */
  22542. return (a - b + 1) / b;
  22543. }
  22544. }
  22545. /* Compute day number of the first day of a given year. */
  22546. DUK_LOCAL duk_int_t duk__day_from_year(duk_int_t year) {
  22547. /* Note: in integer arithmetic, (x / 4) is same as floor(x / 4) for non-negative
  22548. * values, but is incorrect for negative ones.
  22549. */
  22550. return 365 * (year - 1970)
  22551. + duk__div_floor(year - 1969, 4)
  22552. - duk__div_floor(year - 1901, 100)
  22553. + duk__div_floor(year - 1601, 400);
  22554. }
  22555. /* Given a day number, determine year and day-within-year. */
  22556. DUK_LOCAL duk_int_t duk__year_from_day(duk_int_t day, duk_small_int_t *out_day_within_year) {
  22557. duk_int_t year;
  22558. duk_int_t diff_days;
  22559. /* estimate year upwards (towards positive infinity), then back down;
  22560. * two iterations should be enough
  22561. */
  22562. if (day >= 0) {
  22563. year = 1970 + day / 365;
  22564. } else {
  22565. year = 1970 + day / 366;
  22566. }
  22567. for (;;) {
  22568. diff_days = duk__day_from_year(year) - day;
  22569. DUK_DDD(DUK_DDDPRINT("year=%ld day=%ld, diff_days=%ld", (long) year, (long) day, (long) diff_days));
  22570. if (diff_days <= 0) {
  22571. DUK_ASSERT(-diff_days < 366); /* fits into duk_small_int_t */
  22572. *out_day_within_year = -diff_days;
  22573. DUK_DDD(DUK_DDDPRINT("--> year=%ld, day-within-year=%ld",
  22574. (long) year, (long) *out_day_within_year));
  22575. DUK_ASSERT(*out_day_within_year >= 0);
  22576. DUK_ASSERT(*out_day_within_year < (duk__is_leap_year(year) ? 366 : 365));
  22577. return year;
  22578. }
  22579. /* Note: this is very tricky; we must never 'overshoot' the
  22580. * correction downwards.
  22581. */
  22582. year -= 1 + (diff_days - 1) / 366; /* conservative */
  22583. }
  22584. }
  22585. /* Given a (year, month, day-within-month) triple, compute day number.
  22586. * The input triple is un-normalized and may contain non-finite values.
  22587. */
  22588. DUK_LOCAL duk_double_t duk__make_day(duk_double_t year, duk_double_t month, duk_double_t day) {
  22589. duk_int_t day_num;
  22590. duk_bool_t is_leap;
  22591. duk_small_int_t i, n;
  22592. /* Assume that year, month, day are all coerced to whole numbers.
  22593. * They may also be NaN or infinity, in which case this function
  22594. * must return NaN or infinity to ensure time value becomes NaN.
  22595. * If 'day' is NaN, the final return will end up returning a NaN,
  22596. * so it doesn't need to be checked here.
  22597. */
  22598. if (!DUK_ISFINITE(year) || !DUK_ISFINITE(month)) {
  22599. return DUK_DOUBLE_NAN;
  22600. }
  22601. year += DUK_FLOOR(month / 12.0);
  22602. month = DUK_FMOD(month, 12.0);
  22603. if (month < 0.0) {
  22604. /* handle negative values */
  22605. month += 12.0;
  22606. }
  22607. /* The algorithm in E5.1 Section 15.9.1.12 normalizes month, but
  22608. * does not normalize the day-of-month (nor check whether or not
  22609. * it is finite) because it's not necessary for finding the day
  22610. * number which matches the (year,month) pair.
  22611. *
  22612. * We assume that duk__day_from_year() is exact here.
  22613. *
  22614. * Without an explicit infinity / NaN check in the beginning,
  22615. * day_num would be a bogus integer here.
  22616. *
  22617. * It's possible for 'year' to be out of integer range here.
  22618. * If so, we need to return NaN without integer overflow.
  22619. * This fixes test-bug-setyear-overflow.js.
  22620. */
  22621. if (!duk__year_in_valid_range(year)) {
  22622. DUK_DD(DUK_DDPRINT("year not in ecmascript valid range, avoid integer overflow: %lf", (double) year));
  22623. return DUK_DOUBLE_NAN;
  22624. }
  22625. day_num = duk__day_from_year((duk_int_t) year);
  22626. is_leap = duk__is_leap_year((duk_int_t) year);
  22627. n = (duk_small_int_t) month;
  22628. for (i = 0; i < n; i++) {
  22629. day_num += duk__days_in_month[i];
  22630. if (i == 1 && is_leap) {
  22631. day_num++;
  22632. }
  22633. }
  22634. /* If 'day' is NaN, returns NaN. */
  22635. return (duk_double_t) day_num + day;
  22636. }
  22637. /* Split time value into parts. The time value is assumed to be an internal
  22638. * one, i.e. finite, no fractions. Possible local time adjustment has already
  22639. * been applied when reading the time value.
  22640. */
  22641. DUK_LOCAL void duk__timeval_to_parts(duk_double_t d, duk_int_t *parts, duk_double_t *dparts, duk_small_uint_t flags) {
  22642. duk_double_t d1, d2;
  22643. duk_int_t t1, t2;
  22644. duk_int_t day_since_epoch;
  22645. duk_int_t year; /* does not fit into 16 bits */
  22646. duk_small_int_t day_in_year;
  22647. duk_small_int_t month;
  22648. duk_small_int_t day;
  22649. duk_small_int_t dim;
  22650. duk_int_t jan1_since_epoch;
  22651. duk_small_int_t jan1_weekday;
  22652. duk_int_t equiv_year;
  22653. duk_small_uint_t i;
  22654. duk_bool_t is_leap;
  22655. duk_small_int_t arridx;
  22656. DUK_ASSERT(DUK_ISFINITE(d)); /* caller checks */
  22657. DUK_ASSERT(DUK_FLOOR(d) == d); /* no fractions in internal time */
  22658. /* The timevalue must be in valid Ecmascript range, but since a local
  22659. * time offset can be applied, we need to allow a +/- 24h leeway to
  22660. * the value. In other words, although the UTC time is within the
  22661. * Ecmascript range, the local part values can be just outside of it.
  22662. */
  22663. DUK_UNREF(duk__timeval_in_leeway_range);
  22664. DUK_ASSERT(duk__timeval_in_leeway_range(d));
  22665. /* these computations are guaranteed to be exact for the valid
  22666. * E5 time value range, assuming milliseconds without fractions.
  22667. */
  22668. d1 = (duk_double_t) DUK_FMOD(d, (double) DUK__MS_DAY);
  22669. if (d1 < 0.0) {
  22670. /* deal with negative values */
  22671. d1 += (duk_double_t) DUK__MS_DAY;
  22672. }
  22673. d2 = DUK_FLOOR((double) (d / (duk_double_t) DUK__MS_DAY));
  22674. DUK_ASSERT(d2 * ((duk_double_t) DUK__MS_DAY) + d1 == d);
  22675. /* now expected to fit into a 32-bit integer */
  22676. t1 = (duk_int_t) d1;
  22677. t2 = (duk_int_t) d2;
  22678. day_since_epoch = t2;
  22679. DUK_ASSERT((duk_double_t) t1 == d1);
  22680. DUK_ASSERT((duk_double_t) t2 == d2);
  22681. /* t1 = milliseconds within day (fits 32 bit)
  22682. * t2 = day number from epoch (fits 32 bit, may be negative)
  22683. */
  22684. parts[DUK__IDX_MILLISECOND] = t1 % 1000; t1 /= 1000;
  22685. parts[DUK__IDX_SECOND] = t1 % 60; t1 /= 60;
  22686. parts[DUK__IDX_MINUTE] = t1 % 60; t1 /= 60;
  22687. parts[DUK__IDX_HOUR] = t1;
  22688. DUK_ASSERT(parts[DUK__IDX_MILLISECOND] >= 0 && parts[DUK__IDX_MILLISECOND] <= 999);
  22689. DUK_ASSERT(parts[DUK__IDX_SECOND] >= 0 && parts[DUK__IDX_SECOND] <= 59);
  22690. DUK_ASSERT(parts[DUK__IDX_MINUTE] >= 0 && parts[DUK__IDX_MINUTE] <= 59);
  22691. DUK_ASSERT(parts[DUK__IDX_HOUR] >= 0 && parts[DUK__IDX_HOUR] <= 23);
  22692. DUK_DDD(DUK_DDDPRINT("d=%lf, d1=%lf, d2=%lf, t1=%ld, t2=%ld, parts: hour=%ld min=%ld sec=%ld msec=%ld",
  22693. (double) d, (double) d1, (double) d2, (long) t1, (long) t2,
  22694. (long) parts[DUK__IDX_HOUR],
  22695. (long) parts[DUK__IDX_MINUTE],
  22696. (long) parts[DUK__IDX_SECOND],
  22697. (long) parts[DUK__IDX_MILLISECOND]));
  22698. /* This assert depends on the input parts representing time inside
  22699. * the Ecmascript range.
  22700. */
  22701. DUK_ASSERT(t2 + DUK__WEEKDAY_MOD_ADDER >= 0);
  22702. parts[DUK__IDX_WEEKDAY] = (t2 + 4 + DUK__WEEKDAY_MOD_ADDER) % 7; /* E5.1 Section 15.9.1.6 */
  22703. DUK_ASSERT(parts[DUK__IDX_WEEKDAY] >= 0 && parts[DUK__IDX_WEEKDAY] <= 6);
  22704. year = duk__year_from_day(t2, &day_in_year);
  22705. day = day_in_year;
  22706. is_leap = duk__is_leap_year(year);
  22707. for (month = 0; month < 12; month++) {
  22708. dim = duk__days_in_month[month];
  22709. if (month == 1 && is_leap) {
  22710. dim++;
  22711. }
  22712. DUK_DDD(DUK_DDDPRINT("month=%ld, dim=%ld, day=%ld",
  22713. (long) month, (long) dim, (long) day));
  22714. if (day < dim) {
  22715. break;
  22716. }
  22717. day -= dim;
  22718. }
  22719. DUK_DDD(DUK_DDDPRINT("final month=%ld", (long) month));
  22720. DUK_ASSERT(month >= 0 && month <= 11);
  22721. DUK_ASSERT(day >= 0 && day <= 31);
  22722. /* Equivalent year mapping, used to avoid DST trouble when platform
  22723. * may fail to provide reasonable DST answers for dates outside the
  22724. * ordinary range (e.g. 1970-2038). An equivalent year has the same
  22725. * leap-year-ness as the original year and begins on the same weekday
  22726. * (Jan 1).
  22727. *
  22728. * The year 2038 is avoided because there seem to be problems with it
  22729. * on some platforms. The year 1970 is also avoided as there were
  22730. * practical problems with it; an equivalent year is used for it too,
  22731. * which breaks some DST computations for 1970 right now, see e.g.
  22732. * test-bi-date-tzoffset-brute-fi.js.
  22733. */
  22734. if ((flags & DUK__FLAG_EQUIVYEAR) && (year < 1971 || year > 2037)) {
  22735. DUK_ASSERT(is_leap == 0 || is_leap == 1);
  22736. jan1_since_epoch = day_since_epoch - day_in_year; /* day number for Jan 1 since epoch */
  22737. DUK_ASSERT(jan1_since_epoch + DUK__WEEKDAY_MOD_ADDER >= 0);
  22738. jan1_weekday = (jan1_since_epoch + 4 + DUK__WEEKDAY_MOD_ADDER) % 7; /* E5.1 Section 15.9.1.6 */
  22739. DUK_ASSERT(jan1_weekday >= 0 && jan1_weekday <= 6);
  22740. arridx = jan1_weekday;
  22741. if (is_leap) {
  22742. arridx += 7;
  22743. }
  22744. DUK_ASSERT(arridx >= 0 && arridx < (duk_small_int_t) (sizeof(duk__date_equivyear) / sizeof(duk_uint8_t)));
  22745. equiv_year = (duk_int_t) duk__date_equivyear[arridx] + 1970;
  22746. year = equiv_year;
  22747. DUK_DDD(DUK_DDDPRINT("equiv year mapping, year=%ld, day_in_year=%ld, day_since_epoch=%ld, "
  22748. "jan1_since_epoch=%ld, jan1_weekday=%ld -> equiv year %ld",
  22749. (long) year, (long) day_in_year, (long) day_since_epoch,
  22750. (long) jan1_since_epoch, (long) jan1_weekday, (long) equiv_year));
  22751. }
  22752. parts[DUK__IDX_YEAR] = year;
  22753. parts[DUK__IDX_MONTH] = month;
  22754. parts[DUK__IDX_DAY] = day;
  22755. if (flags & DUK__FLAG_ONEBASED) {
  22756. parts[DUK__IDX_MONTH]++; /* zero-based -> one-based */
  22757. parts[DUK__IDX_DAY]++; /* -""- */
  22758. }
  22759. if (dparts != NULL) {
  22760. for (i = 0; i < DUK__NUM_PARTS; i++) {
  22761. dparts[i] = (duk_double_t) parts[i];
  22762. }
  22763. }
  22764. }
  22765. /* Compute time value from (double) parts. The parts can be either UTC
  22766. * or local time; if local, they need to be (conceptually) converted into
  22767. * UTC time. The parts may represent valid or invalid time, and may be
  22768. * wildly out of range (but may cancel each other and still come out in
  22769. * the valid Date range).
  22770. */
  22771. DUK_LOCAL duk_double_t duk__get_timeval_from_dparts(duk_double_t *dparts, duk_small_uint_t flags) {
  22772. #if defined(DUK_USE_PARANOID_DATE_COMPUTATION)
  22773. /* See comments below on MakeTime why these are volatile. */
  22774. volatile duk_double_t tmp_time;
  22775. volatile duk_double_t tmp_day;
  22776. volatile duk_double_t d;
  22777. #else
  22778. duk_double_t tmp_time;
  22779. duk_double_t tmp_day;
  22780. duk_double_t d;
  22781. #endif
  22782. duk_small_uint_t i;
  22783. duk_int_t tzoff, tzoffprev1, tzoffprev2;
  22784. /* Expects 'this' at top of stack on entry. */
  22785. /* Coerce all finite parts with ToInteger(). ToInteger() must not
  22786. * be called for NaN/Infinity because it will convert e.g. NaN to
  22787. * zero. If ToInteger() has already been called, this has no side
  22788. * effects and is idempotent.
  22789. *
  22790. * Don't read dparts[DUK__IDX_WEEKDAY]; it will cause Valgrind issues
  22791. * if the value is uninitialized.
  22792. */
  22793. for (i = 0; i <= DUK__IDX_MILLISECOND; i++) {
  22794. /* SCANBUILD: scan-build complains here about assigned value
  22795. * being garbage or undefined. This is correct but operating
  22796. * on undefined values has no ill effect and is ignored by the
  22797. * caller in the case where this happens.
  22798. */
  22799. d = dparts[i];
  22800. if (DUK_ISFINITE(d)) {
  22801. dparts[i] = duk_js_tointeger_number(d);
  22802. }
  22803. }
  22804. /* Use explicit steps in computation to try to ensure that
  22805. * computation happens with intermediate results coerced to
  22806. * double values (instead of using something more accurate).
  22807. * E.g. E5.1 Section 15.9.1.11 requires use of IEEE 754
  22808. * rules (= Ecmascript '+' and '*' operators).
  22809. *
  22810. * Without 'volatile' even this approach fails on some platform
  22811. * and compiler combinations. For instance, gcc 4.8.1 on Ubuntu
  22812. * 64-bit, with -m32 and without -std=c99, test-bi-date-canceling.js
  22813. * would fail because of some optimizations when computing tmp_time
  22814. * (MakeTime below). Adding 'volatile' to tmp_time solved this
  22815. * particular problem (annoyingly, also adding debug prints or
  22816. * running the executable under valgrind hides it).
  22817. */
  22818. /* MakeTime */
  22819. tmp_time = 0.0;
  22820. tmp_time += dparts[DUK__IDX_HOUR] * ((duk_double_t) DUK__MS_HOUR);
  22821. tmp_time += dparts[DUK__IDX_MINUTE] * ((duk_double_t) DUK__MS_MINUTE);
  22822. tmp_time += dparts[DUK__IDX_SECOND] * ((duk_double_t) DUK__MS_SECOND);
  22823. tmp_time += dparts[DUK__IDX_MILLISECOND];
  22824. /* MakeDay */
  22825. tmp_day = duk__make_day(dparts[DUK__IDX_YEAR], dparts[DUK__IDX_MONTH], dparts[DUK__IDX_DAY]);
  22826. /* MakeDate */
  22827. d = tmp_day * ((duk_double_t) DUK__MS_DAY) + tmp_time;
  22828. DUK_DDD(DUK_DDDPRINT("time=%lf day=%lf --> timeval=%lf",
  22829. (double) tmp_time, (double) tmp_day, (double) d));
  22830. /* Optional UTC conversion. */
  22831. if (flags & DUK__FLAG_LOCALTIME) {
  22832. /* DUK__GET_LOCAL_TZOFFSET() needs to be called with a time
  22833. * value computed from UTC parts. At this point we only have
  22834. * 'd' which is a time value computed from local parts, so it
  22835. * is off by the UTC-to-local time offset which we don't know
  22836. * yet. The current solution for computing the UTC-to-local
  22837. * time offset is to iterate a few times and detect a fixed
  22838. * point or a two-cycle loop (or a sanity iteration limit),
  22839. * see test-bi-date-local-parts.js and test-bi-date-tzoffset-basic-fi.js.
  22840. *
  22841. * E5.1 Section 15.9.1.9:
  22842. * UTC(t) = t - LocalTZA - DaylightSavingTA(t - LocalTZA)
  22843. *
  22844. * For NaN/inf, DUK__GET_LOCAL_TZOFFSET() returns 0.
  22845. */
  22846. #if 0
  22847. /* Old solution: don't iterate, incorrect */
  22848. tzoff = DUK__GET_LOCAL_TZOFFSET(d);
  22849. DUK_DDD(DUK_DDDPRINT("tzoffset w/o iteration, tzoff=%ld", (long) tzoff));
  22850. d -= tzoff * 1000L;
  22851. DUK_UNREF(tzoffprev1);
  22852. DUK_UNREF(tzoffprev2);
  22853. #endif
  22854. /* Iteration solution */
  22855. tzoff = 0;
  22856. tzoffprev1 = 999999999L; /* invalid value which never matches */
  22857. for (i = 0; i < DUK__LOCAL_TZOFFSET_MAXITER; i++) {
  22858. tzoffprev2 = tzoffprev1;
  22859. tzoffprev1 = tzoff;
  22860. tzoff = DUK__GET_LOCAL_TZOFFSET(d - tzoff * 1000L);
  22861. DUK_DDD(DUK_DDDPRINT("tzoffset iteration, i=%d, tzoff=%ld, tzoffprev1=%ld tzoffprev2=%ld",
  22862. (int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2));
  22863. if (tzoff == tzoffprev1) {
  22864. DUK_DDD(DUK_DDDPRINT("tzoffset iteration finished, i=%d, tzoff=%ld, tzoffprev1=%ld, tzoffprev2=%ld",
  22865. (int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2));
  22866. break;
  22867. } else if (tzoff == tzoffprev2) {
  22868. /* Two value cycle, see e.g. test-bi-date-tzoffset-basic-fi.js.
  22869. * In these cases, favor a higher tzoffset to get a consistent
  22870. * result which is independent of iteration count. Not sure if
  22871. * this is a generically correct solution.
  22872. */
  22873. DUK_DDD(DUK_DDDPRINT("tzoffset iteration two-value cycle, i=%d, tzoff=%ld, tzoffprev1=%ld, tzoffprev2=%ld",
  22874. (int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2));
  22875. if (tzoffprev1 > tzoff) {
  22876. tzoff = tzoffprev1;
  22877. }
  22878. break;
  22879. }
  22880. }
  22881. DUK_DDD(DUK_DDDPRINT("tzoffset iteration, tzoff=%ld", (long) tzoff));
  22882. d -= tzoff * 1000L;
  22883. }
  22884. /* TimeClip(), which also handles Infinity -> NaN conversion */
  22885. d = duk__timeclip(d);
  22886. return d;
  22887. }
  22888. /*
  22889. * API oriented helpers
  22890. */
  22891. /* Push 'this' binding, check that it is a Date object; then push the
  22892. * internal time value. At the end, stack is: [ ... this timeval ].
  22893. * Returns the time value. Local time adjustment is done if requested.
  22894. */
  22895. DUK_LOCAL duk_double_t duk__push_this_get_timeval_tzoffset(duk_context *ctx, duk_small_uint_t flags, duk_int_t *out_tzoffset) {
  22896. duk_hthread *thr = (duk_hthread *) ctx;
  22897. duk_hobject *h;
  22898. duk_double_t d;
  22899. duk_int_t tzoffset = 0;
  22900. duk_push_this(ctx);
  22901. h = duk_get_hobject(ctx, -1); /* XXX: getter with class check, useful in built-ins */
  22902. if (h == NULL || DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_DATE) {
  22903. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "expected Date");
  22904. }
  22905. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE);
  22906. d = duk_to_number(ctx, -1);
  22907. duk_pop(ctx);
  22908. if (DUK_ISNAN(d)) {
  22909. if (flags & DUK__FLAG_NAN_TO_ZERO) {
  22910. d = 0.0;
  22911. }
  22912. if (flags & DUK__FLAG_NAN_TO_RANGE_ERROR) {
  22913. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, "Invalid Date");
  22914. }
  22915. }
  22916. /* if no NaN handling flag, may still be NaN here, but not Inf */
  22917. DUK_ASSERT(!DUK_ISINF(d));
  22918. if (flags & DUK__FLAG_LOCALTIME) {
  22919. /* Note: DST adjustment is determined using UTC time.
  22920. * If 'd' is NaN, tzoffset will be 0.
  22921. */
  22922. tzoffset = DUK__GET_LOCAL_TZOFFSET(d); /* seconds */
  22923. d += tzoffset * 1000L;
  22924. }
  22925. if (out_tzoffset) {
  22926. *out_tzoffset = tzoffset;
  22927. }
  22928. /* [ ... this ] */
  22929. return d;
  22930. }
  22931. DUK_LOCAL duk_double_t duk__push_this_get_timeval(duk_context *ctx, duk_small_uint_t flags) {
  22932. return duk__push_this_get_timeval_tzoffset(ctx, flags, NULL);
  22933. }
  22934. /* Set timeval to 'this' from dparts, push the new time value onto the
  22935. * value stack and return 1 (caller can then tailcall us). Expects
  22936. * the value stack to contain 'this' on the stack top.
  22937. */
  22938. DUK_LOCAL duk_ret_t duk__set_this_timeval_from_dparts(duk_context *ctx, duk_double_t *dparts, duk_small_uint_t flags) {
  22939. duk_double_t d;
  22940. /* [ ... this ] */
  22941. d = duk__get_timeval_from_dparts(dparts, flags);
  22942. duk_push_number(ctx, d); /* -> [ ... this timeval_new ] */
  22943. duk_dup_top(ctx); /* -> [ ... this timeval_new timeval_new ] */
  22944. duk_put_prop_stridx(ctx, -3, DUK_STRIDX_INT_VALUE);
  22945. /* stack top: new time value, return 1 to allow tailcalls */
  22946. return 1;
  22947. }
  22948. /* 'out_buf' must be at least DUK_BI_DATE_ISO8601_BUFSIZE long. */
  22949. 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) {
  22950. char yearstr[8]; /* "-123456\0" */
  22951. char tzstr[8]; /* "+11:22\0" */
  22952. char sep = (flags & DUK__FLAG_SEP_T) ? DUK_ASC_UC_T : DUK_ASC_SPACE;
  22953. DUK_ASSERT(parts[DUK__IDX_MONTH] >= 1 && parts[DUK__IDX_MONTH] <= 12);
  22954. DUK_ASSERT(parts[DUK__IDX_DAY] >= 1 && parts[DUK__IDX_DAY] <= 31);
  22955. DUK_ASSERT(parts[DUK__IDX_YEAR] >= -999999 && parts[DUK__IDX_YEAR] <= 999999);
  22956. /* Note: %06d for positive value, %07d for negative value to include
  22957. * sign and 6 digits.
  22958. */
  22959. DUK_SNPRINTF(yearstr,
  22960. sizeof(yearstr),
  22961. (parts[DUK__IDX_YEAR] >= 0 && parts[DUK__IDX_YEAR] <= 9999) ? "%04ld" :
  22962. ((parts[DUK__IDX_YEAR] >= 0) ? "+%06ld" : "%07ld"),
  22963. (long) parts[DUK__IDX_YEAR]);
  22964. yearstr[sizeof(yearstr) - 1] = (char) 0;
  22965. if (flags & DUK__FLAG_LOCALTIME) {
  22966. /* tzoffset seconds are dropped; 16 bits suffice for
  22967. * time offset in minutes
  22968. */
  22969. if (tzoffset >= 0) {
  22970. duk_small_int_t tmp = tzoffset / 60;
  22971. DUK_SNPRINTF(tzstr, sizeof(tzstr), "+%02d:%02d", (int) (tmp / 60), (int) (tmp % 60));
  22972. } else {
  22973. duk_small_int_t tmp = -tzoffset / 60;
  22974. DUK_SNPRINTF(tzstr, sizeof(tzstr), "-%02d:%02d", (int) (tmp / 60), (int) (tmp % 60));
  22975. }
  22976. tzstr[sizeof(tzstr) - 1] = (char) 0;
  22977. } else {
  22978. tzstr[0] = DUK_ASC_UC_Z;
  22979. tzstr[1] = (char) 0;
  22980. }
  22981. /* Unlike year, the other parts fit into 16 bits so %d format
  22982. * is portable.
  22983. */
  22984. if ((flags & DUK__FLAG_TOSTRING_DATE) && (flags & DUK__FLAG_TOSTRING_TIME)) {
  22985. DUK_SPRINTF((char *) out_buf, "%s-%02d-%02d%c%02d:%02d:%02d.%03d%s",
  22986. (const char *) yearstr, (int) parts[DUK__IDX_MONTH], (int) parts[DUK__IDX_DAY], (int) sep,
  22987. (int) parts[DUK__IDX_HOUR], (int) parts[DUK__IDX_MINUTE],
  22988. (int) parts[DUK__IDX_SECOND], (int) parts[DUK__IDX_MILLISECOND], (const char *) tzstr);
  22989. } else if (flags & DUK__FLAG_TOSTRING_DATE) {
  22990. DUK_SPRINTF((char *) out_buf, "%s-%02d-%02d",
  22991. (const char *) yearstr, (int) parts[DUK__IDX_MONTH], (int) parts[DUK__IDX_DAY]);
  22992. } else {
  22993. DUK_ASSERT(flags & DUK__FLAG_TOSTRING_TIME);
  22994. DUK_SPRINTF((char *) out_buf, "%02d:%02d:%02d.%03d%s",
  22995. (int) parts[DUK__IDX_HOUR], (int) parts[DUK__IDX_MINUTE],
  22996. (int) parts[DUK__IDX_SECOND], (int) parts[DUK__IDX_MILLISECOND],
  22997. (const char *) tzstr);
  22998. }
  22999. }
  23000. /* Helper for string conversion calls: check 'this' binding, get the
  23001. * internal time value, and format date and/or time in a few formats.
  23002. * Return value allows tail calls.
  23003. */
  23004. DUK_LOCAL duk_ret_t duk__to_string_helper(duk_context *ctx, duk_small_uint_t flags) {
  23005. duk_double_t d;
  23006. duk_int_t parts[DUK__NUM_PARTS];
  23007. duk_int_t tzoffset; /* seconds, doesn't fit into 16 bits */
  23008. duk_bool_t rc;
  23009. duk_uint8_t buf[DUK_BI_DATE_ISO8601_BUFSIZE];
  23010. DUK_UNREF(rc); /* unreferenced with some options */
  23011. d = duk__push_this_get_timeval_tzoffset(ctx, flags, &tzoffset);
  23012. if (DUK_ISNAN(d)) {
  23013. duk_push_hstring_stridx(ctx, DUK_STRIDX_INVALID_DATE);
  23014. return 1;
  23015. }
  23016. DUK_ASSERT(DUK_ISFINITE(d));
  23017. /* formatters always get one-based month/day-of-month */
  23018. duk__timeval_to_parts(d, parts, NULL, DUK__FLAG_ONEBASED);
  23019. DUK_ASSERT(parts[DUK__IDX_MONTH] >= 1 && parts[DUK__IDX_MONTH] <= 12);
  23020. DUK_ASSERT(parts[DUK__IDX_DAY] >= 1 && parts[DUK__IDX_DAY] <= 31);
  23021. if (flags & DUK__FLAG_TOSTRING_LOCALE) {
  23022. /* try locale specific formatter; if it refuses to format the
  23023. * string, fall back to an ISO 8601 formatted value in local
  23024. * time.
  23025. */
  23026. #ifdef DUK_USE_DATE_FMT_STRFTIME
  23027. rc = duk__format_parts_strftime(ctx, parts, tzoffset, flags);
  23028. if (rc != 0) {
  23029. return 1;
  23030. }
  23031. #else
  23032. /* No locale specific formatter; this is OK, we fall back
  23033. * to ISO 8601.
  23034. */
  23035. #endif
  23036. }
  23037. /* Different calling convention than above used because the helper
  23038. * is shared.
  23039. */
  23040. duk__format_parts_iso8601(parts, tzoffset, flags, buf);
  23041. duk_push_string(ctx, (const char *) buf);
  23042. return 1;
  23043. }
  23044. /* Helper for component getter calls: check 'this' binding, get the
  23045. * internal time value, split it into parts (either as UTC time or
  23046. * local time), push a specified component as a return value to the
  23047. * value stack and return 1 (caller can then tailcall us).
  23048. */
  23049. DUK_LOCAL duk_ret_t duk__get_part_helper(duk_context *ctx, duk_small_uint_t flags_and_idx) {
  23050. duk_double_t d;
  23051. duk_int_t parts[DUK__NUM_PARTS];
  23052. duk_small_uint_t idx_part = (duk_small_uint_t) (flags_and_idx >> DUK__FLAG_VALUE_SHIFT); /* unpack args */
  23053. DUK_ASSERT_DISABLE(idx_part >= 0); /* unsigned */
  23054. DUK_ASSERT(idx_part < DUK__NUM_PARTS);
  23055. d = duk__push_this_get_timeval(ctx, flags_and_idx);
  23056. if (DUK_ISNAN(d)) {
  23057. duk_push_nan(ctx);
  23058. return 1;
  23059. }
  23060. DUK_ASSERT(DUK_ISFINITE(d));
  23061. duk__timeval_to_parts(d, parts, NULL, flags_and_idx); /* no need to mask idx portion */
  23062. /* Setter APIs detect special year numbers (0...99) and apply a +1900
  23063. * only in certain cases. The legacy getYear() getter applies -1900
  23064. * unconditionally.
  23065. */
  23066. duk_push_int(ctx, (flags_and_idx & DUK__FLAG_SUB1900) ? parts[idx_part] - 1900 : parts[idx_part]);
  23067. return 1;
  23068. }
  23069. /* Helper for component setter calls: check 'this' binding, get the
  23070. * internal time value, split it into parts (either as UTC time or
  23071. * local time), modify one or more components as specified, recompute
  23072. * the time value, set it as the internal value. Finally, push the
  23073. * new time value as a return value to the value stack and return 1
  23074. * (caller can then tailcall us).
  23075. */
  23076. DUK_LOCAL duk_ret_t duk__set_part_helper(duk_context *ctx, duk_small_uint_t flags_and_maxnargs) {
  23077. duk_double_t d;
  23078. duk_int_t parts[DUK__NUM_PARTS];
  23079. duk_double_t dparts[DUK__NUM_PARTS];
  23080. duk_idx_t nargs;
  23081. duk_small_uint_t maxnargs = (duk_small_uint_t) (flags_and_maxnargs >> DUK__FLAG_VALUE_SHIFT); /* unpack args */
  23082. duk_small_uint_t idx_first, idx;
  23083. duk_small_uint_t i;
  23084. nargs = duk_get_top(ctx);
  23085. d = duk__push_this_get_timeval(ctx, flags_and_maxnargs);
  23086. DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d));
  23087. if (DUK_ISFINITE(d)) {
  23088. duk__timeval_to_parts(d, parts, dparts, flags_and_maxnargs);
  23089. } else {
  23090. /* NaN timevalue: we need to coerce the arguments, but
  23091. * the resulting internal timestamp needs to remain NaN.
  23092. * This works but is not pretty: parts and dparts will
  23093. * be partially uninitialized, but we only write to them.
  23094. */
  23095. }
  23096. /*
  23097. * Determining which datetime components to overwrite based on
  23098. * stack arguments is a bit complicated, but important to factor
  23099. * out from setters themselves for compactness.
  23100. *
  23101. * If DUK__FLAG_TIMESETTER, maxnargs indicates setter type:
  23102. *
  23103. * 1 -> millisecond
  23104. * 2 -> second, [millisecond]
  23105. * 3 -> minute, [second], [millisecond]
  23106. * 4 -> hour, [minute], [second], [millisecond]
  23107. *
  23108. * Else:
  23109. *
  23110. * 1 -> date
  23111. * 2 -> month, [date]
  23112. * 3 -> year, [month], [date]
  23113. *
  23114. * By comparing nargs and maxnargs (and flags) we know which
  23115. * components to override. We rely on part index ordering.
  23116. */
  23117. if (flags_and_maxnargs & DUK__FLAG_TIMESETTER) {
  23118. DUK_ASSERT(maxnargs >= 1 && maxnargs <= 4);
  23119. idx_first = DUK__IDX_MILLISECOND - (maxnargs - 1);
  23120. } else {
  23121. DUK_ASSERT(maxnargs >= 1 && maxnargs <= 3);
  23122. idx_first = DUK__IDX_DAY - (maxnargs - 1);
  23123. }
  23124. DUK_ASSERT_DISABLE(idx_first >= 0); /* unsigned */
  23125. DUK_ASSERT(idx_first < DUK__NUM_PARTS);
  23126. for (i = 0; i < maxnargs; i++) {
  23127. if ((duk_idx_t) i >= nargs) {
  23128. /* no argument given -> leave components untouched */
  23129. break;
  23130. }
  23131. idx = idx_first + i;
  23132. DUK_ASSERT_DISABLE(idx >= 0); /* unsigned */
  23133. DUK_ASSERT(idx < DUK__NUM_PARTS);
  23134. if (idx == DUK__IDX_YEAR && (flags_and_maxnargs & DUK__FLAG_YEAR_FIXUP)) {
  23135. duk__twodigit_year_fixup(ctx, (duk_idx_t) i);
  23136. }
  23137. dparts[idx] = duk_to_number(ctx, i);
  23138. if (idx == DUK__IDX_DAY) {
  23139. /* Day-of-month is one-based in the API, but zero-based
  23140. * internally, so fix here. Note that month is zero-based
  23141. * both in the API and internally.
  23142. */
  23143. /* SCANBUILD: complains about use of uninitialized values.
  23144. * The complaint is correct, but operating in undefined
  23145. * values here is intentional in some cases and the caller
  23146. * ignores the results.
  23147. */
  23148. dparts[idx] -= 1.0;
  23149. }
  23150. }
  23151. /* Leaves new timevalue on stack top and returns 1, which is correct
  23152. * for part setters.
  23153. */
  23154. if (DUK_ISFINITE(d)) {
  23155. return duk__set_this_timeval_from_dparts(ctx, dparts, flags_and_maxnargs);
  23156. } else {
  23157. /* Internal timevalue is already NaN, so don't touch it. */
  23158. duk_push_nan(ctx);
  23159. return 1;
  23160. }
  23161. }
  23162. /* Apply ToNumber() to specified index; if ToInteger(val) in [0,99], add
  23163. * 1900 and replace value at idx_val.
  23164. */
  23165. DUK_LOCAL void duk__twodigit_year_fixup(duk_context *ctx, duk_idx_t idx_val) {
  23166. duk_double_t d;
  23167. /* XXX: idx_val would fit into 16 bits, but using duk_small_uint_t
  23168. * might not generate better code due to casting.
  23169. */
  23170. /* E5 Sections 15.9.3.1, B.2.4, B.2.5 */
  23171. duk_to_number(ctx, idx_val);
  23172. if (duk_is_nan(ctx, idx_val)) {
  23173. return;
  23174. }
  23175. duk_dup(ctx, idx_val);
  23176. duk_to_int(ctx, -1);
  23177. d = duk_get_number(ctx, -1); /* get as double to handle huge numbers correctly */
  23178. if (d >= 0.0 && d <= 99.0) {
  23179. d += 1900.0;
  23180. duk_push_number(ctx, d);
  23181. duk_replace(ctx, idx_val);
  23182. }
  23183. duk_pop(ctx);
  23184. }
  23185. /* Set datetime parts from stack arguments, defaulting any missing values.
  23186. * Day-of-week is not set; it is not required when setting the time value.
  23187. */
  23188. DUK_LOCAL void duk__set_parts_from_args(duk_context *ctx, duk_double_t *dparts, duk_idx_t nargs) {
  23189. duk_double_t d;
  23190. duk_small_uint_t i;
  23191. duk_small_uint_t idx;
  23192. /* Causes a ToNumber() coercion, but doesn't break coercion order since
  23193. * year is coerced first anyway.
  23194. */
  23195. duk__twodigit_year_fixup(ctx, 0);
  23196. /* There are at most 7 args, but we use 8 here so that also
  23197. * DUK__IDX_WEEKDAY gets initialized (to zero) to avoid the potential
  23198. * for any Valgrind gripes later.
  23199. */
  23200. for (i = 0; i < 8; i++) {
  23201. /* Note: rely on index ordering */
  23202. idx = DUK__IDX_YEAR + i;
  23203. if ((duk_idx_t) i < nargs) {
  23204. d = duk_to_number(ctx, (duk_idx_t) i);
  23205. if (idx == DUK__IDX_DAY) {
  23206. /* Convert day from one-based to zero-based (internal). This may
  23207. * cause the day part to be negative, which is OK.
  23208. */
  23209. d -= 1.0;
  23210. }
  23211. } else {
  23212. /* All components default to 0 except day-of-month which defaults
  23213. * to 1. However, because our internal day-of-month is zero-based,
  23214. * it also defaults to zero here.
  23215. */
  23216. d = 0.0;
  23217. }
  23218. dparts[idx] = d;
  23219. }
  23220. DUK_DDD(DUK_DDDPRINT("parts from args -> %lf %lf %lf %lf %lf %lf %lf %lf",
  23221. (double) dparts[0], (double) dparts[1],
  23222. (double) dparts[2], (double) dparts[3],
  23223. (double) dparts[4], (double) dparts[5],
  23224. (double) dparts[6], (double) dparts[7]));
  23225. }
  23226. /*
  23227. * Helper to format a time value into caller buffer, used by logging.
  23228. * 'out_buf' must be at least DUK_BI_DATE_ISO8601_BUFSIZE long.
  23229. */
  23230. DUK_INTERNAL void duk_bi_date_format_timeval(duk_double_t timeval, duk_uint8_t *out_buf) {
  23231. duk_int_t parts[DUK__NUM_PARTS];
  23232. duk__timeval_to_parts(timeval,
  23233. parts,
  23234. NULL,
  23235. DUK__FLAG_ONEBASED);
  23236. duk__format_parts_iso8601(parts,
  23237. 0 /*tzoffset*/,
  23238. DUK__FLAG_TOSTRING_DATE |
  23239. DUK__FLAG_TOSTRING_TIME |
  23240. DUK__FLAG_SEP_T /*flags*/,
  23241. out_buf);
  23242. }
  23243. /*
  23244. * Indirect magic value lookup for Date methods.
  23245. *
  23246. * Date methods don't put their control flags into the function magic value
  23247. * because they wouldn't fit into a LIGHTFUNC's magic field. Instead, the
  23248. * magic value is set to an index pointing to the array of control flags
  23249. * below.
  23250. *
  23251. * This must be kept in strict sync with genbuiltins.py!
  23252. */
  23253. static duk_uint16_t duk__date_magics[] = {
  23254. /* 0: toString */
  23255. DUK__FLAG_TOSTRING_DATE + DUK__FLAG_TOSTRING_TIME + DUK__FLAG_LOCALTIME,
  23256. /* 1: toDateString */
  23257. DUK__FLAG_TOSTRING_DATE + DUK__FLAG_LOCALTIME,
  23258. /* 2: toTimeString */
  23259. DUK__FLAG_TOSTRING_TIME + DUK__FLAG_LOCALTIME,
  23260. /* 3: toLocaleString */
  23261. DUK__FLAG_TOSTRING_DATE + DUK__FLAG_TOSTRING_TIME + DUK__FLAG_TOSTRING_LOCALE + DUK__FLAG_LOCALTIME,
  23262. /* 4: toLocaleDateString */
  23263. DUK__FLAG_TOSTRING_DATE + DUK__FLAG_TOSTRING_LOCALE + DUK__FLAG_LOCALTIME,
  23264. /* 5: toLocaleTimeString */
  23265. DUK__FLAG_TOSTRING_TIME + DUK__FLAG_TOSTRING_LOCALE + DUK__FLAG_LOCALTIME,
  23266. /* 6: toUTCString */
  23267. DUK__FLAG_TOSTRING_DATE + DUK__FLAG_TOSTRING_TIME,
  23268. /* 7: toISOString */
  23269. DUK__FLAG_TOSTRING_DATE + DUK__FLAG_TOSTRING_TIME + DUK__FLAG_NAN_TO_RANGE_ERROR + DUK__FLAG_SEP_T,
  23270. /* 8: getFullYear */
  23271. DUK__FLAG_LOCALTIME + (DUK__IDX_YEAR << DUK__FLAG_VALUE_SHIFT),
  23272. /* 9: getUTCFullYear */
  23273. 0 + (DUK__IDX_YEAR << DUK__FLAG_VALUE_SHIFT),
  23274. /* 10: getMonth */
  23275. DUK__FLAG_LOCALTIME + (DUK__IDX_MONTH << DUK__FLAG_VALUE_SHIFT),
  23276. /* 11: getUTCMonth */
  23277. 0 + (DUK__IDX_MONTH << DUK__FLAG_VALUE_SHIFT),
  23278. /* 12: getDate */
  23279. DUK__FLAG_ONEBASED + DUK__FLAG_LOCALTIME + (DUK__IDX_DAY << DUK__FLAG_VALUE_SHIFT),
  23280. /* 13: getUTCDate */
  23281. DUK__FLAG_ONEBASED + (DUK__IDX_DAY << DUK__FLAG_VALUE_SHIFT),
  23282. /* 14: getDay */
  23283. DUK__FLAG_LOCALTIME + (DUK__IDX_WEEKDAY << DUK__FLAG_VALUE_SHIFT),
  23284. /* 15: getUTCDay */
  23285. 0 + (DUK__IDX_WEEKDAY << DUK__FLAG_VALUE_SHIFT),
  23286. /* 16: getHours */
  23287. DUK__FLAG_LOCALTIME + (DUK__IDX_HOUR << DUK__FLAG_VALUE_SHIFT),
  23288. /* 17: getUTCHours */
  23289. 0 + (DUK__IDX_HOUR << DUK__FLAG_VALUE_SHIFT),
  23290. /* 18: getMinutes */
  23291. DUK__FLAG_LOCALTIME + (DUK__IDX_MINUTE << DUK__FLAG_VALUE_SHIFT),
  23292. /* 19: getUTCMinutes */
  23293. 0 + (DUK__IDX_MINUTE << DUK__FLAG_VALUE_SHIFT),
  23294. /* 20: getSeconds */
  23295. DUK__FLAG_LOCALTIME + (DUK__IDX_SECOND << DUK__FLAG_VALUE_SHIFT),
  23296. /* 21: getUTCSeconds */
  23297. 0 + (DUK__IDX_SECOND << DUK__FLAG_VALUE_SHIFT),
  23298. /* 22: getMilliseconds */
  23299. DUK__FLAG_LOCALTIME + (DUK__IDX_MILLISECOND << DUK__FLAG_VALUE_SHIFT),
  23300. /* 23: getUTCMilliseconds */
  23301. 0 + (DUK__IDX_MILLISECOND << DUK__FLAG_VALUE_SHIFT),
  23302. /* 24: setMilliseconds */
  23303. DUK__FLAG_TIMESETTER + DUK__FLAG_LOCALTIME + (1 << DUK__FLAG_VALUE_SHIFT),
  23304. /* 25: setUTCMilliseconds */
  23305. DUK__FLAG_TIMESETTER + (1 << DUK__FLAG_VALUE_SHIFT),
  23306. /* 26: setSeconds */
  23307. DUK__FLAG_TIMESETTER + DUK__FLAG_LOCALTIME + (2 << DUK__FLAG_VALUE_SHIFT),
  23308. /* 27: setUTCSeconds */
  23309. DUK__FLAG_TIMESETTER + (2 << DUK__FLAG_VALUE_SHIFT),
  23310. /* 28: setMinutes */
  23311. DUK__FLAG_TIMESETTER + DUK__FLAG_LOCALTIME + (3 << DUK__FLAG_VALUE_SHIFT),
  23312. /* 29: setUTCMinutes */
  23313. DUK__FLAG_TIMESETTER + (3 << DUK__FLAG_VALUE_SHIFT),
  23314. /* 30: setHours */
  23315. DUK__FLAG_TIMESETTER + DUK__FLAG_LOCALTIME + (4 << DUK__FLAG_VALUE_SHIFT),
  23316. /* 31: setUTCHours */
  23317. DUK__FLAG_TIMESETTER + (4 << DUK__FLAG_VALUE_SHIFT),
  23318. /* 32: setDate */
  23319. DUK__FLAG_LOCALTIME + (1 << DUK__FLAG_VALUE_SHIFT),
  23320. /* 33: setUTCDate */
  23321. 0 + (1 << DUK__FLAG_VALUE_SHIFT),
  23322. /* 34: setMonth */
  23323. DUK__FLAG_LOCALTIME + (2 << DUK__FLAG_VALUE_SHIFT),
  23324. /* 35: setUTCMonth */
  23325. 0 + (2 << DUK__FLAG_VALUE_SHIFT),
  23326. /* 36: setFullYear */
  23327. DUK__FLAG_NAN_TO_ZERO + DUK__FLAG_LOCALTIME + (3 << DUK__FLAG_VALUE_SHIFT),
  23328. /* 37: setUTCFullYear */
  23329. DUK__FLAG_NAN_TO_ZERO + (3 << DUK__FLAG_VALUE_SHIFT),
  23330. /* 38: getYear */
  23331. DUK__FLAG_LOCALTIME + DUK__FLAG_SUB1900 + (DUK__IDX_YEAR << DUK__FLAG_VALUE_SHIFT),
  23332. /* 39: setYear */
  23333. DUK__FLAG_NAN_TO_ZERO + DUK__FLAG_YEAR_FIXUP + (3 << DUK__FLAG_VALUE_SHIFT),
  23334. };
  23335. DUK_LOCAL duk_small_uint_t duk__date_get_indirect_magic(duk_context *ctx) {
  23336. duk_small_int_t magicidx = (duk_small_uint_t) duk_get_current_magic(ctx);
  23337. DUK_ASSERT(magicidx >= 0 && magicidx < (duk_small_int_t) (sizeof(duk__date_magics) / sizeof(duk_uint16_t)));
  23338. return (duk_small_uint_t) duk__date_magics[magicidx];
  23339. }
  23340. /*
  23341. * Constructor calls
  23342. */
  23343. DUK_INTERNAL duk_ret_t duk_bi_date_constructor(duk_context *ctx) {
  23344. duk_idx_t nargs = duk_get_top(ctx);
  23345. duk_bool_t is_cons = duk_is_constructor_call(ctx);
  23346. duk_double_t dparts[DUK__NUM_PARTS];
  23347. duk_double_t d;
  23348. DUK_DDD(DUK_DDDPRINT("Date constructor, nargs=%ld, is_cons=%ld", (long) nargs, (long) is_cons));
  23349. duk_push_object_helper(ctx,
  23350. DUK_HOBJECT_FLAG_EXTENSIBLE |
  23351. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DATE),
  23352. DUK_BIDX_DATE_PROTOTYPE);
  23353. /* Unlike most built-ins, the internal [[PrimitiveValue]] of a Date
  23354. * is mutable.
  23355. */
  23356. if (nargs == 0 || !is_cons) {
  23357. d = duk__timeclip(DUK__GET_NOW_TIMEVAL(ctx));
  23358. duk_push_number(ctx, d);
  23359. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_W);
  23360. if (!is_cons) {
  23361. /* called as a normal function: return new Date().toString() */
  23362. duk_to_string(ctx, -1);
  23363. }
  23364. return 1;
  23365. } else if (nargs == 1) {
  23366. duk_to_primitive(ctx, 0, DUK_HINT_NONE);
  23367. if (duk_is_string(ctx, 0)) {
  23368. duk__parse_string(ctx, duk_to_string(ctx, 0));
  23369. duk_replace(ctx, 0); /* may be NaN */
  23370. }
  23371. d = duk__timeclip(duk_to_number(ctx, 0));
  23372. duk_push_number(ctx, d);
  23373. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_W);
  23374. return 1;
  23375. }
  23376. duk__set_parts_from_args(ctx, dparts, nargs);
  23377. /* Parts are in local time, convert when setting. */
  23378. (void) duk__set_this_timeval_from_dparts(ctx, dparts, DUK__FLAG_LOCALTIME /*flags*/); /* -> [ ... this timeval ] */
  23379. duk_pop(ctx); /* -> [ ... this ] */
  23380. return 1;
  23381. }
  23382. DUK_INTERNAL duk_ret_t duk_bi_date_constructor_parse(duk_context *ctx) {
  23383. return duk__parse_string(ctx, duk_to_string(ctx, 0));
  23384. }
  23385. DUK_INTERNAL duk_ret_t duk_bi_date_constructor_utc(duk_context *ctx) {
  23386. duk_idx_t nargs = duk_get_top(ctx);
  23387. duk_double_t dparts[DUK__NUM_PARTS];
  23388. duk_double_t d;
  23389. /* Behavior for nargs < 2 is implementation dependent: currently we'll
  23390. * set a NaN time value (matching V8 behavior) in this case.
  23391. */
  23392. if (nargs < 2) {
  23393. duk_push_nan(ctx);
  23394. } else {
  23395. duk__set_parts_from_args(ctx, dparts, nargs);
  23396. d = duk__get_timeval_from_dparts(dparts, 0 /*flags*/);
  23397. duk_push_number(ctx, d);
  23398. }
  23399. return 1;
  23400. }
  23401. DUK_INTERNAL duk_ret_t duk_bi_date_constructor_now(duk_context *ctx) {
  23402. duk_double_t d;
  23403. d = DUK__GET_NOW_TIMEVAL(ctx);
  23404. DUK_ASSERT(duk__timeclip(d) == d); /* TimeClip() should never be necessary */
  23405. duk_push_number(ctx, d);
  23406. return 1;
  23407. }
  23408. /*
  23409. * String/JSON conversions
  23410. *
  23411. * Human readable conversions are now basically ISO 8601 with a space
  23412. * (instead of 'T') as the date/time separator. This is a good baseline
  23413. * and is platform independent.
  23414. *
  23415. * A shared native helper to provide many conversions. Magic value contains
  23416. * a set of flags. The helper provides:
  23417. *
  23418. * toString()
  23419. * toDateString()
  23420. * toTimeString()
  23421. * toLocaleString()
  23422. * toLocaleDateString()
  23423. * toLocaleTimeString()
  23424. * toUTCString()
  23425. * toISOString()
  23426. *
  23427. * Notes:
  23428. *
  23429. * - Date.prototype.toGMTString() and Date.prototype.toUTCString() are
  23430. * required to be the same Ecmascript function object (!), so it is
  23431. * omitted from here.
  23432. *
  23433. * - Date.prototype.toUTCString(): E5.1 specification does not require a
  23434. * specific format, but result should be human readable. The
  23435. * specification suggests using ISO 8601 format with a space (instead
  23436. * of 'T') separator if a more human readable format is not available.
  23437. *
  23438. * - Date.prototype.toISOString(): unlike other conversion functions,
  23439. * toISOString() requires a RangeError for invalid date values.
  23440. */
  23441. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_tostring_shared(duk_context *ctx) {
  23442. duk_small_uint_t flags = duk__date_get_indirect_magic(ctx);
  23443. return duk__to_string_helper(ctx, flags);
  23444. }
  23445. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_value_of(duk_context *ctx) {
  23446. /* This native function is also used for Date.prototype.getTime()
  23447. * as their behavior is identical.
  23448. */
  23449. duk_double_t d = duk__push_this_get_timeval(ctx, 0 /*flags*/); /* -> [ this ] */
  23450. DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d));
  23451. duk_push_number(ctx, d);
  23452. return 1;
  23453. }
  23454. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_to_json(duk_context *ctx) {
  23455. /* Note: toJSON() is a generic function which works even if 'this'
  23456. * is not a Date. The sole argument is ignored.
  23457. */
  23458. duk_push_this(ctx);
  23459. duk_to_object(ctx, -1);
  23460. duk_dup_top(ctx);
  23461. duk_to_primitive(ctx, -1, DUK_HINT_NUMBER);
  23462. if (duk_is_number(ctx, -1)) {
  23463. duk_double_t d = duk_get_number(ctx, -1);
  23464. if (!DUK_ISFINITE(d)) {
  23465. duk_push_null(ctx);
  23466. return 1;
  23467. }
  23468. }
  23469. duk_pop(ctx);
  23470. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_TO_ISO_STRING);
  23471. duk_dup(ctx, -2); /* -> [ O toIsoString O ] */
  23472. duk_call_method(ctx, 0);
  23473. return 1;
  23474. }
  23475. /*
  23476. * Getters.
  23477. *
  23478. * Implementing getters is quite easy. The internal time value is either
  23479. * NaN, or represents milliseconds (without fractions) from Jan 1, 1970.
  23480. * The internal time value can be converted to integer parts, and each
  23481. * part will be normalized and will fit into a 32-bit signed integer.
  23482. *
  23483. * A shared native helper to provide all getters. Magic value contains
  23484. * a set of flags and also packs the date component index argument. The
  23485. * helper provides:
  23486. *
  23487. * getFullYear()
  23488. * getUTCFullYear()
  23489. * getMonth()
  23490. * getUTCMonth()
  23491. * getDate()
  23492. * getUTCDate()
  23493. * getDay()
  23494. * getUTCDay()
  23495. * getHours()
  23496. * getUTCHours()
  23497. * getMinutes()
  23498. * getUTCMinutes()
  23499. * getSeconds()
  23500. * getUTCSeconds()
  23501. * getMilliseconds()
  23502. * getUTCMilliseconds()
  23503. * getYear()
  23504. *
  23505. * Notes:
  23506. *
  23507. * - Date.prototype.getDate(): 'date' means day-of-month, and is
  23508. * zero-based in internal calculations but public API expects it to
  23509. * be one-based.
  23510. *
  23511. * - Date.prototype.getTime() and Date.prototype.valueOf() have identical
  23512. * behavior. They have separate function objects, but share the same C
  23513. * function (duk_bi_date_prototype_value_of).
  23514. */
  23515. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_get_shared(duk_context *ctx) {
  23516. duk_small_uint_t flags_and_idx = duk__date_get_indirect_magic(ctx);
  23517. return duk__get_part_helper(ctx, flags_and_idx);
  23518. }
  23519. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_get_timezone_offset(duk_context *ctx) {
  23520. /*
  23521. * Return (t - LocalTime(t)) in minutes:
  23522. *
  23523. * t - LocalTime(t) = t - (t + LocalTZA + DaylightSavingTA(t))
  23524. * = -(LocalTZA + DaylightSavingTA(t))
  23525. *
  23526. * where DaylightSavingTA() is checked for time 't'.
  23527. *
  23528. * Note that the sign of the result is opposite to common usage,
  23529. * e.g. for EE(S)T which normally is +2h or +3h from UTC, this
  23530. * function returns -120 or -180.
  23531. *
  23532. */
  23533. duk_double_t d;
  23534. duk_int_t tzoffset;
  23535. /* Note: DST adjustment is determined using UTC time. */
  23536. d = duk__push_this_get_timeval(ctx, 0 /*flags*/);
  23537. DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d));
  23538. if (DUK_ISNAN(d)) {
  23539. duk_push_nan(ctx);
  23540. } else {
  23541. DUK_ASSERT(DUK_ISFINITE(d));
  23542. tzoffset = DUK__GET_LOCAL_TZOFFSET(d);
  23543. duk_push_int(ctx, -tzoffset / 60);
  23544. }
  23545. return 1;
  23546. }
  23547. /*
  23548. * Setters.
  23549. *
  23550. * Setters are a bit more complicated than getters. Component setters
  23551. * break down the current time value into its (normalized) component
  23552. * parts, replace one or more components with -unnormalized- new values,
  23553. * and the components are then converted back into a time value. As an
  23554. * example of using unnormalized values:
  23555. *
  23556. * var d = new Date(1234567890);
  23557. *
  23558. * is equivalent to:
  23559. *
  23560. * var d = new Date(0);
  23561. * d.setUTCMilliseconds(1234567890);
  23562. *
  23563. * A shared native helper to provide almost all setters. Magic value
  23564. * contains a set of flags and also packs the "maxnargs" argument. The
  23565. * helper provides:
  23566. *
  23567. * setMilliseconds()
  23568. * setUTCMilliseconds()
  23569. * setSeconds()
  23570. * setUTCSeconds()
  23571. * setMinutes()
  23572. * setUTCMinutes()
  23573. * setHours()
  23574. * setUTCHours()
  23575. * setDate()
  23576. * setUTCDate()
  23577. * setMonth()
  23578. * setUTCMonth()
  23579. * setFullYear()
  23580. * setUTCFullYear()
  23581. * setYear()
  23582. *
  23583. * Notes:
  23584. *
  23585. * - Date.prototype.setYear() (Section B addition): special year check
  23586. * is omitted. NaN / Infinity will just flow through and ultimately
  23587. * result in a NaN internal time value.
  23588. *
  23589. * - Date.prototype.setYear() does not have optional arguments for
  23590. * setting month and day-in-month (like setFullYear()), but we indicate
  23591. * 'maxnargs' to be 3 to get the year written to the correct component
  23592. * index in duk__set_part_helper(). The function has nargs == 1, so only
  23593. * the year will be set regardless of actual argument count.
  23594. */
  23595. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_set_shared(duk_context *ctx) {
  23596. duk_small_uint_t flags_and_maxnargs = duk__date_get_indirect_magic(ctx);
  23597. return duk__set_part_helper(ctx, flags_and_maxnargs);
  23598. }
  23599. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_set_time(duk_context *ctx) {
  23600. duk_double_t d;
  23601. (void) duk__push_this_get_timeval(ctx, 0 /*flags*/); /* -> [ timeval this ] */
  23602. d = duk__timeclip(duk_to_number(ctx, 0));
  23603. duk_push_number(ctx, d);
  23604. duk_dup_top(ctx);
  23605. duk_put_prop_stridx(ctx, -3, DUK_STRIDX_INT_VALUE); /* -> [ timeval this timeval ] */
  23606. return 1;
  23607. }
  23608. #line 1 "duk_bi_duktape.c"
  23609. /*
  23610. * Duktape built-ins
  23611. *
  23612. * Size optimization note: it might seem that vararg multipurpose functions
  23613. * like fin(), enc(), and dec() are not very size optimal, but using a single
  23614. * user-visible Ecmascript function saves a lot of run-time footprint; each
  23615. * Function instance takes >100 bytes. Using a shared native helper and a
  23616. * 'magic' value won't save much if there are multiple Function instances
  23617. * anyway.
  23618. */
  23619. /* include removed: duk_internal.h */
  23620. /* Raw helper to extract internal information / statistics about a value.
  23621. * The return values are version specific and must not expose anything
  23622. * that would lead to security issues (e.g. exposing compiled function
  23623. * 'data' buffer might be an issue). Currently only counts and sizes and
  23624. * such are given so there should not be a security impact.
  23625. */
  23626. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_info(duk_context *ctx) {
  23627. duk_hthread *thr = (duk_hthread *) ctx;
  23628. duk_tval *tv;
  23629. duk_heaphdr *h;
  23630. duk_int_t i, n;
  23631. DUK_UNREF(thr);
  23632. /* result array */
  23633. duk_push_array(ctx); /* -> [ val arr ] */
  23634. /* type tag (public) */
  23635. duk_push_int(ctx, duk_get_type(ctx, 0));
  23636. /* address */
  23637. tv = duk_get_tval(ctx, 0);
  23638. DUK_ASSERT(tv != NULL); /* because arg count is 1 */
  23639. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  23640. h = DUK_TVAL_GET_HEAPHDR(tv);
  23641. duk_push_pointer(ctx, (void *) h);
  23642. } else {
  23643. /* internal type tag */
  23644. duk_push_int(ctx, (duk_int_t) DUK_TVAL_GET_TAG(tv));
  23645. goto done;
  23646. }
  23647. DUK_ASSERT(h != NULL);
  23648. /* refcount */
  23649. #ifdef DUK_USE_REFERENCE_COUNTING
  23650. duk_push_size_t(ctx, DUK_HEAPHDR_GET_REFCOUNT(h));
  23651. #else
  23652. duk_push_undefined(ctx);
  23653. #endif
  23654. /* heaphdr size and additional allocation size, followed by
  23655. * type specific stuff (with varying value count)
  23656. */
  23657. switch ((duk_small_int_t) DUK_HEAPHDR_GET_TYPE(h)) {
  23658. case DUK_HTYPE_STRING: {
  23659. duk_hstring *h_str = (duk_hstring *) h;
  23660. duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hstring) + DUK_HSTRING_GET_BYTELEN(h_str) + 1));
  23661. break;
  23662. }
  23663. case DUK_HTYPE_OBJECT: {
  23664. duk_hobject *h_obj = (duk_hobject *) h;
  23665. duk_small_uint_t hdr_size;
  23666. if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h_obj)) {
  23667. hdr_size = (duk_small_uint_t) sizeof(duk_hcompiledfunction);
  23668. } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(h_obj)) {
  23669. hdr_size = (duk_small_uint_t) sizeof(duk_hnativefunction);
  23670. } else if (DUK_HOBJECT_IS_THREAD(h_obj)) {
  23671. hdr_size = (duk_small_uint_t) sizeof(duk_hthread);
  23672. } else {
  23673. hdr_size = (duk_small_uint_t) sizeof(duk_hobject);
  23674. }
  23675. duk_push_uint(ctx, (duk_uint_t) hdr_size);
  23676. duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_E_ALLOC_SIZE(h_obj));
  23677. duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_GET_ESIZE(h_obj));
  23678. /* Note: e_next indicates the number of gc-reachable entries
  23679. * in the entry part, and also indicates the index where the
  23680. * next new property would be inserted. It does *not* indicate
  23681. * the number of non-NULL keys present in the object. That
  23682. * value could be counted separately but requires a pass through
  23683. * the key list.
  23684. */
  23685. duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_GET_ENEXT(h_obj));
  23686. duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_GET_ASIZE(h_obj));
  23687. duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_GET_HSIZE(h_obj));
  23688. if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h_obj)) {
  23689. duk_hbuffer *h_data = (duk_hbuffer *) DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, (duk_hcompiledfunction *) h_obj);
  23690. if (h_data) {
  23691. duk_push_uint(ctx, (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_data));
  23692. } else {
  23693. duk_push_uint(ctx, 0);
  23694. }
  23695. }
  23696. break;
  23697. }
  23698. case DUK_HTYPE_BUFFER: {
  23699. duk_hbuffer *h_buf = (duk_hbuffer *) h;
  23700. if (DUK_HBUFFER_HAS_DYNAMIC(h_buf)) {
  23701. /* XXX: when alloc_size == 0, dynamic buf ptr may now be NULL, in which case
  23702. * the second allocation does not exist.
  23703. */
  23704. duk_hbuffer_dynamic *h_dyn = (duk_hbuffer_dynamic *) h;
  23705. duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hbuffer_dynamic)));
  23706. duk_push_uint(ctx, (duk_uint_t) (DUK_HBUFFER_DYNAMIC_GET_ALLOC_SIZE(h_dyn)));
  23707. } else {
  23708. duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hbuffer_fixed) + DUK_HBUFFER_GET_SIZE(h_buf) + 1));
  23709. }
  23710. break;
  23711. }
  23712. }
  23713. done:
  23714. /* set values into ret array */
  23715. /* XXX: primitive to make array from valstack slice */
  23716. n = duk_get_top(ctx);
  23717. for (i = 2; i < n; i++) {
  23718. duk_dup(ctx, i);
  23719. duk_put_prop_index(ctx, 1, i - 2);
  23720. }
  23721. duk_dup(ctx, 1);
  23722. return 1;
  23723. }
  23724. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_act(duk_context *ctx) {
  23725. duk_hthread *thr = (duk_hthread *) ctx;
  23726. duk_activation *act;
  23727. duk_uint_fast32_t pc;
  23728. duk_uint_fast32_t line;
  23729. duk_int_t level;
  23730. /* -1 = top callstack entry, callstack[callstack_top - 1]
  23731. * -callstack_top = bottom callstack entry, callstack[0]
  23732. */
  23733. level = duk_to_int(ctx, 0);
  23734. if (level >= 0 || -level > (duk_int_t) thr->callstack_top) {
  23735. return 0;
  23736. }
  23737. DUK_ASSERT(level >= -((duk_int_t) thr->callstack_top) && level <= -1);
  23738. act = thr->callstack + thr->callstack_top + level;
  23739. duk_push_object(ctx);
  23740. duk_push_tval(ctx, &act->tv_func);
  23741. pc = (duk_uint_fast32_t) act->pc;
  23742. if (pc > 0) {
  23743. /* Relevant PC is just before current one because PC is
  23744. * post-incremented. This should match what error augment
  23745. * code does.
  23746. */
  23747. pc--;
  23748. }
  23749. duk_push_uint(ctx, (duk_uint_t) pc);
  23750. #if defined(DUK_USE_PC2LINE)
  23751. line = duk_hobject_pc2line_query(ctx, -2, pc);
  23752. #else
  23753. line = 0;
  23754. #endif
  23755. duk_push_uint(ctx, (duk_uint_t) line);
  23756. /* Providing access to e.g. act->lex_env would be dangerous: these
  23757. * internal structures must never be accessible to the application.
  23758. * Duktape relies on them having consistent data, and this consistency
  23759. * is only asserted for, not checked for.
  23760. */
  23761. /* [ level obj func pc line ] */
  23762. /* XXX: version specific array format instead? */
  23763. duk_xdef_prop_stridx_wec(ctx, -4, DUK_STRIDX_LINE_NUMBER);
  23764. duk_xdef_prop_stridx_wec(ctx, -3, DUK_STRIDX_PC);
  23765. duk_xdef_prop_stridx_wec(ctx, -2, DUK_STRIDX_LC_FUNCTION);
  23766. return 1;
  23767. }
  23768. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_gc(duk_context *ctx) {
  23769. #ifdef DUK_USE_MARK_AND_SWEEP
  23770. duk_hthread *thr = (duk_hthread *) ctx;
  23771. duk_small_uint_t flags;
  23772. duk_bool_t rc;
  23773. flags = (duk_small_uint_t) duk_get_uint(ctx, 0);
  23774. rc = duk_heap_mark_and_sweep(thr->heap, flags);
  23775. /* XXX: Not sure what the best return value would be in the API.
  23776. * Return a boolean for now. Note that rc == 0 is success (true).
  23777. */
  23778. duk_push_boolean(ctx, !rc);
  23779. return 1;
  23780. #else
  23781. DUK_UNREF(ctx);
  23782. return 0;
  23783. #endif
  23784. }
  23785. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_fin(duk_context *ctx) {
  23786. (void) duk_require_hobject(ctx, 0);
  23787. if (duk_get_top(ctx) >= 2) {
  23788. /* Set: currently a finalizer is disabled by setting it to
  23789. * undefined; this does not remove the property at the moment.
  23790. * The value could be type checked to be either a function
  23791. * or something else; if something else, the property could
  23792. * be deleted.
  23793. */
  23794. duk_set_top(ctx, 2);
  23795. (void) duk_put_prop_stridx(ctx, 0, DUK_STRIDX_INT_FINALIZER);
  23796. return 0;
  23797. } else {
  23798. /* Get. */
  23799. DUK_ASSERT(duk_get_top(ctx) == 1);
  23800. duk_get_prop_stridx(ctx, 0, DUK_STRIDX_INT_FINALIZER);
  23801. return 1;
  23802. }
  23803. }
  23804. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_enc(duk_context *ctx) {
  23805. duk_hthread *thr = (duk_hthread *) ctx;
  23806. duk_hstring *h_str;
  23807. /* Vararg function: must be careful to check/require arguments.
  23808. * The JSON helpers accept invalid indices and treat them like
  23809. * non-existent optional parameters.
  23810. */
  23811. h_str = duk_require_hstring(ctx, 0);
  23812. duk_require_valid_index(ctx, 1);
  23813. if (h_str == DUK_HTHREAD_STRING_HEX(thr)) {
  23814. duk_set_top(ctx, 2);
  23815. duk_hex_encode(ctx, 1);
  23816. DUK_ASSERT_TOP(ctx, 2);
  23817. } else if (h_str == DUK_HTHREAD_STRING_BASE64(thr)) {
  23818. duk_set_top(ctx, 2);
  23819. duk_base64_encode(ctx, 1);
  23820. DUK_ASSERT_TOP(ctx, 2);
  23821. #ifdef DUK_USE_JX
  23822. } else if (h_str == DUK_HTHREAD_STRING_JX(thr)) {
  23823. duk_bi_json_stringify_helper(ctx,
  23824. 1 /*idx_value*/,
  23825. 2 /*idx_replacer*/,
  23826. 3 /*idx_space*/,
  23827. DUK_JSON_FLAG_EXT_CUSTOM |
  23828. DUK_JSON_FLAG_ASCII_ONLY |
  23829. DUK_JSON_FLAG_AVOID_KEY_QUOTES /*flags*/);
  23830. #endif
  23831. #ifdef DUK_USE_JC
  23832. } else if (h_str == DUK_HTHREAD_STRING_JC(thr)) {
  23833. duk_bi_json_stringify_helper(ctx,
  23834. 1 /*idx_value*/,
  23835. 2 /*idx_replacer*/,
  23836. 3 /*idx_space*/,
  23837. DUK_JSON_FLAG_EXT_COMPATIBLE |
  23838. DUK_JSON_FLAG_ASCII_ONLY /*flags*/);
  23839. #endif
  23840. } else {
  23841. return DUK_RET_TYPE_ERROR;
  23842. }
  23843. return 1;
  23844. }
  23845. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_dec(duk_context *ctx) {
  23846. duk_hthread *thr = (duk_hthread *) ctx;
  23847. duk_hstring *h_str;
  23848. /* Vararg function: must be careful to check/require arguments.
  23849. * The JSON helpers accept invalid indices and treat them like
  23850. * non-existent optional parameters.
  23851. */
  23852. h_str = duk_require_hstring(ctx, 0);
  23853. duk_require_valid_index(ctx, 1);
  23854. if (h_str == DUK_HTHREAD_STRING_HEX(thr)) {
  23855. duk_set_top(ctx, 2);
  23856. duk_hex_decode(ctx, 1);
  23857. DUK_ASSERT_TOP(ctx, 2);
  23858. } else if (h_str == DUK_HTHREAD_STRING_BASE64(thr)) {
  23859. duk_set_top(ctx, 2);
  23860. duk_base64_decode(ctx, 1);
  23861. DUK_ASSERT_TOP(ctx, 2);
  23862. #ifdef DUK_USE_JX
  23863. } else if (h_str == DUK_HTHREAD_STRING_JX(thr)) {
  23864. duk_bi_json_parse_helper(ctx,
  23865. 1 /*idx_value*/,
  23866. 2 /*idx_replacer*/,
  23867. DUK_JSON_FLAG_EXT_CUSTOM /*flags*/);
  23868. #endif
  23869. #ifdef DUK_USE_JC
  23870. } else if (h_str == DUK_HTHREAD_STRING_JC(thr)) {
  23871. duk_bi_json_parse_helper(ctx,
  23872. 1 /*idx_value*/,
  23873. 2 /*idx_replacer*/,
  23874. DUK_JSON_FLAG_EXT_COMPATIBLE /*flags*/);
  23875. #endif
  23876. } else {
  23877. return DUK_RET_TYPE_ERROR;
  23878. }
  23879. return 1;
  23880. }
  23881. /*
  23882. * Compact an object
  23883. */
  23884. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_compact(duk_context *ctx) {
  23885. DUK_ASSERT_TOP(ctx, 1);
  23886. duk_compact(ctx, 0);
  23887. return 1; /* return the argument object */
  23888. }
  23889. #line 1 "duk_bi_error.c"
  23890. /*
  23891. * Error built-ins
  23892. */
  23893. /* include removed: duk_internal.h */
  23894. DUK_INTERNAL duk_ret_t duk_bi_error_constructor_shared(duk_context *ctx) {
  23895. /* Behavior for constructor and non-constructor call is
  23896. * the same except for augmenting the created error. When
  23897. * called as a constructor, the caller (duk_new()) will handle
  23898. * augmentation; when called as normal function, we need to do
  23899. * it here.
  23900. */
  23901. duk_hthread *thr = (duk_hthread *) ctx;
  23902. duk_small_int_t bidx_prototype = duk_get_current_magic(ctx);
  23903. /* same for both error and each subclass like TypeError */
  23904. duk_uint_t flags_and_class = DUK_HOBJECT_FLAG_EXTENSIBLE |
  23905. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ERROR);
  23906. DUK_UNREF(thr);
  23907. duk_push_object_helper(ctx, flags_and_class, bidx_prototype);
  23908. /* If message is undefined, the own property 'message' is not set at
  23909. * all to save property space. An empty message is inherited anyway.
  23910. */
  23911. if (!duk_is_undefined(ctx, 0)) {
  23912. duk_to_string(ctx, 0);
  23913. duk_dup(ctx, 0); /* [ message error message ] */
  23914. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC);
  23915. }
  23916. /* Augment the error if called as a normal function. __FILE__ and __LINE__
  23917. * are not desirable in this case.
  23918. */
  23919. #ifdef DUK_USE_AUGMENT_ERROR_CREATE
  23920. if (!duk_is_constructor_call(ctx)) {
  23921. duk_err_augment_error_create(thr, thr, NULL, 0, 1 /*noblame_fileline*/);
  23922. }
  23923. #endif
  23924. return 1;
  23925. }
  23926. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_to_string(duk_context *ctx) {
  23927. /* XXX: optimize with more direct internal access */
  23928. duk_push_this(ctx);
  23929. (void) duk_require_hobject_or_lfunc_coerce(ctx, -1);
  23930. /* [ ... this ] */
  23931. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_NAME);
  23932. if (duk_is_undefined(ctx, -1)) {
  23933. duk_pop(ctx);
  23934. duk_push_string(ctx, "Error");
  23935. } else {
  23936. duk_to_string(ctx, -1);
  23937. }
  23938. /* [ ... this name ] */
  23939. /* XXX: Are steps 6 and 7 in E5 Section 15.11.4.4 duplicated by
  23940. * accident or are they actually needed? The first ToString()
  23941. * could conceivably return 'undefined'.
  23942. */
  23943. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE);
  23944. if (duk_is_undefined(ctx, -1)) {
  23945. duk_pop(ctx);
  23946. duk_push_string(ctx, "");
  23947. } else {
  23948. duk_to_string(ctx, -1);
  23949. }
  23950. /* [ ... this name message ] */
  23951. if (duk_get_length(ctx, -2) == 0) {
  23952. /* name is empty -> return message */
  23953. return 1;
  23954. }
  23955. if (duk_get_length(ctx, -1) == 0) {
  23956. /* message is empty -> return name */
  23957. duk_pop(ctx);
  23958. return 1;
  23959. }
  23960. duk_push_string(ctx, ": ");
  23961. duk_insert(ctx, -2); /* ... name ': ' message */
  23962. duk_concat(ctx, 3);
  23963. return 1;
  23964. }
  23965. #ifdef DUK_USE_TRACEBACKS
  23966. /*
  23967. * Traceback handling
  23968. *
  23969. * The unified helper decodes the traceback and produces various requested
  23970. * outputs. It should be optimized for size, and may leave garbage on stack,
  23971. * only the topmost return value matters. For instance, traceback separator
  23972. * and decoded strings are pushed even when looking for filename only.
  23973. *
  23974. * NOTE: although _Tracedata is an internal property, user code can currently
  23975. * write to the array (or replace it with something other than an array).
  23976. * The code below must tolerate arbitrary _Tracedata. It can throw errors
  23977. * etc, but cannot cause a segfault or memory unsafe behavior.
  23978. */
  23979. /* constants arbitrary, chosen for small loads */
  23980. #define DUK__OUTPUT_TYPE_TRACEBACK (-1)
  23981. #define DUK__OUTPUT_TYPE_FILENAME 0
  23982. #define DUK__OUTPUT_TYPE_LINENUMBER 1
  23983. DUK_LOCAL duk_ret_t duk__traceback_getter_helper(duk_context *ctx, duk_small_int_t output_type) {
  23984. duk_hthread *thr = (duk_hthread *) ctx;
  23985. duk_idx_t idx_td;
  23986. duk_small_int_t i; /* traceback depth fits into 16 bits */
  23987. duk_small_int_t t; /* stack type fits into 16 bits */
  23988. const char *str_tailcalled = " tailcalled";
  23989. const char *str_strict = " strict";
  23990. const char *str_construct = " construct";
  23991. const char *str_prevyield = " preventsyield";
  23992. const char *str_directeval = " directeval";
  23993. const char *str_empty = "";
  23994. DUK_ASSERT_TOP(ctx, 0); /* fixed arg count */
  23995. duk_push_this(ctx);
  23996. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_TRACEDATA);
  23997. idx_td = duk_get_top_index(ctx);
  23998. duk_push_hstring_stridx(ctx, DUK_STRIDX_NEWLINE_TAB);
  23999. duk_push_this(ctx);
  24000. duk_to_string(ctx, -1);
  24001. /* [ ... this tracedata sep ToString(this) ] */
  24002. /* XXX: skip null filename? */
  24003. if (duk_check_type(ctx, idx_td, DUK_TYPE_OBJECT)) {
  24004. /* Current tracedata contains 2 entries per callstack entry. */
  24005. for (i = 0; ; i += 2) {
  24006. duk_int_t pc;
  24007. duk_int_t line;
  24008. duk_int_t flags;
  24009. duk_double_t d;
  24010. const char *funcname;
  24011. const char *filename;
  24012. duk_hobject *h_func;
  24013. duk_hstring *h_name;
  24014. duk_require_stack(ctx, 5);
  24015. duk_get_prop_index(ctx, idx_td, i);
  24016. duk_get_prop_index(ctx, idx_td, i + 1);
  24017. d = duk_to_number(ctx, -1);
  24018. pc = (duk_int_t) DUK_FMOD(d, DUK_DOUBLE_2TO32);
  24019. flags = (duk_int_t) DUK_FLOOR(d / DUK_DOUBLE_2TO32);
  24020. t = (duk_small_int_t) duk_get_type(ctx, -2);
  24021. if (t == DUK_TYPE_OBJECT || t == DUK_TYPE_LIGHTFUNC) {
  24022. /*
  24023. * Ecmascript/native function call or lightfunc call
  24024. */
  24025. /* [ ... v1(func) v2(pc+flags) ] */
  24026. h_func = duk_get_hobject(ctx, -2); /* NULL for lightfunc */
  24027. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_NAME);
  24028. duk_get_prop_stridx(ctx, -3, DUK_STRIDX_FILE_NAME);
  24029. #if defined(DUK_USE_PC2LINE)
  24030. line = duk_hobject_pc2line_query(ctx, -4, (duk_uint_fast32_t) pc);
  24031. #else
  24032. line = 0;
  24033. #endif
  24034. /* [ ... v1 v2 name filename ] */
  24035. if (output_type == DUK__OUTPUT_TYPE_FILENAME) {
  24036. return 1;
  24037. } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) {
  24038. duk_push_int(ctx, line);
  24039. return 1;
  24040. }
  24041. h_name = duk_get_hstring(ctx, -2); /* may be NULL */
  24042. funcname = (h_name == NULL || h_name == DUK_HTHREAD_STRING_EMPTY_STRING(thr)) ?
  24043. "anon" : (const char *) DUK_HSTRING_GET_DATA(h_name);
  24044. filename = duk_get_string(ctx, -1);
  24045. filename = filename ? filename : "";
  24046. DUK_ASSERT(funcname != NULL);
  24047. DUK_ASSERT(filename != NULL);
  24048. if (h_func == NULL) {
  24049. duk_push_sprintf(ctx, "%s light%s%s%s%s%s",
  24050. (const char *) funcname,
  24051. (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty),
  24052. (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcalled : str_empty),
  24053. (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty),
  24054. (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty),
  24055. (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty));
  24056. } else if (DUK_HOBJECT_HAS_NATIVEFUNCTION(h_func)) {
  24057. duk_push_sprintf(ctx, "%s %s native%s%s%s%s%s",
  24058. (const char *) funcname,
  24059. (const char *) filename,
  24060. (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty),
  24061. (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcalled : str_empty),
  24062. (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty),
  24063. (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty),
  24064. (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty));
  24065. } else {
  24066. duk_push_sprintf(ctx, "%s %s:%ld%s%s%s%s%s",
  24067. (const char *) funcname,
  24068. (const char *) filename,
  24069. (long) line,
  24070. (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty),
  24071. (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcalled : str_empty),
  24072. (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty),
  24073. (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty),
  24074. (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty));
  24075. }
  24076. duk_replace(ctx, -5); /* [ ... v1 v2 name filename str ] -> [ ... str v2 name filename ] */
  24077. duk_pop_n(ctx, 3); /* -> [ ... str ] */
  24078. } else if (t == DUK_TYPE_STRING) {
  24079. /*
  24080. * __FILE__ / __LINE__ entry, here 'pc' is line number directly.
  24081. * Sometimes __FILE__ / __LINE__ is reported as the source for
  24082. * the error (fileName, lineNumber), sometimes not.
  24083. */
  24084. /* [ ... v1(filename) v2(line+flags) ] */
  24085. if (!(flags & DUK_TB_FLAG_NOBLAME_FILELINE)) {
  24086. if (output_type == DUK__OUTPUT_TYPE_FILENAME) {
  24087. duk_pop(ctx);
  24088. return 1;
  24089. } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) {
  24090. duk_push_int(ctx, pc);
  24091. return 1;
  24092. }
  24093. }
  24094. duk_push_sprintf(ctx, "%s:%ld",
  24095. (const char *) duk_get_string(ctx, -2), (long) pc);
  24096. duk_replace(ctx, -3); /* [ ... v1 v2 str ] -> [ ... str v2 ] */
  24097. duk_pop(ctx); /* -> [ ... str ] */
  24098. } else {
  24099. /* unknown, ignore */
  24100. duk_pop_2(ctx);
  24101. break;
  24102. }
  24103. }
  24104. if (i >= DUK_USE_TRACEBACK_DEPTH * 2) {
  24105. /* Possibly truncated; there is no explicit truncation
  24106. * marker so this is the best we can do.
  24107. */
  24108. duk_push_hstring_stridx(ctx, DUK_STRIDX_BRACKETED_ELLIPSIS);
  24109. }
  24110. }
  24111. /* [ ... this tracedata sep ToString(this) str1 ... strN ] */
  24112. if (output_type != DUK__OUTPUT_TYPE_TRACEBACK) {
  24113. return 0;
  24114. } else {
  24115. duk_join(ctx, duk_get_top(ctx) - (idx_td + 2) /*count, not including sep*/);
  24116. return 1;
  24117. }
  24118. }
  24119. /* XXX: output type could be encoded into native function 'magic' value to
  24120. * save space.
  24121. */
  24122. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_stack_getter(duk_context *ctx) {
  24123. return duk__traceback_getter_helper(ctx, DUK__OUTPUT_TYPE_TRACEBACK);
  24124. }
  24125. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_filename_getter(duk_context *ctx) {
  24126. return duk__traceback_getter_helper(ctx, DUK__OUTPUT_TYPE_FILENAME);
  24127. }
  24128. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_linenumber_getter(duk_context *ctx) {
  24129. return duk__traceback_getter_helper(ctx, DUK__OUTPUT_TYPE_LINENUMBER);
  24130. }
  24131. #undef DUK__OUTPUT_TYPE_TRACEBACK
  24132. #undef DUK__OUTPUT_TYPE_FILENAME
  24133. #undef DUK__OUTPUT_TYPE_LINENUMBER
  24134. #else /* DUK_USE_TRACEBACKS */
  24135. /*
  24136. * Traceback handling when tracebacks disabled.
  24137. *
  24138. * The fileName / lineNumber stubs are now necessary because built-in
  24139. * data will include the accessor properties in Error.prototype. If those
  24140. * are removed for builds without tracebacks, these can also be removed.
  24141. * 'stack' should still be present and produce a ToString() equivalent:
  24142. * this is useful for user code which prints a stacktrace and expects to
  24143. * see something useful. A normal stacktrace also begins with a ToString()
  24144. * of the error so this makes sense.
  24145. */
  24146. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_stack_getter(duk_context *ctx) {
  24147. /* XXX: remove this native function and map 'stack' accessor
  24148. * to the toString() implementation directly.
  24149. */
  24150. return duk_bi_error_prototype_to_string(ctx);
  24151. }
  24152. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_filename_getter(duk_context *ctx) {
  24153. DUK_UNREF(ctx);
  24154. return 0;
  24155. }
  24156. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_linenumber_getter(duk_context *ctx) {
  24157. DUK_UNREF(ctx);
  24158. return 0;
  24159. }
  24160. #endif /* DUK_USE_TRACEBACKS */
  24161. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_nop_setter(duk_context *ctx) {
  24162. /* Attempt to write 'stack', 'fileName', 'lineNumber' is a silent no-op.
  24163. * User can use Object.defineProperty() to override this behavior.
  24164. */
  24165. DUK_ASSERT_TOP(ctx, 1); /* fixed arg count */
  24166. DUK_UNREF(ctx);
  24167. return 0;
  24168. }
  24169. #line 1 "duk_bi_function.c"
  24170. /*
  24171. * Function built-ins
  24172. */
  24173. /* include removed: duk_internal.h */
  24174. DUK_INTERNAL duk_ret_t duk_bi_function_constructor(duk_context *ctx) {
  24175. duk_hthread *thr = (duk_hthread *) ctx;
  24176. duk_hstring *h_sourcecode;
  24177. duk_idx_t nargs;
  24178. duk_idx_t i;
  24179. duk_small_uint_t comp_flags;
  24180. duk_hcompiledfunction *func;
  24181. duk_hobject *outer_lex_env;
  24182. duk_hobject *outer_var_env;
  24183. /* normal and constructor calls have identical semantics */
  24184. nargs = duk_get_top(ctx);
  24185. for (i = 0; i < nargs; i++) {
  24186. duk_to_string(ctx, i);
  24187. }
  24188. if (nargs == 0) {
  24189. duk_push_string(ctx, "");
  24190. duk_push_string(ctx, "");
  24191. } else if (nargs == 1) {
  24192. /* XXX: cover this with the generic >1 case? */
  24193. duk_push_string(ctx, "");
  24194. } else {
  24195. duk_insert(ctx, 0); /* [ arg1 ... argN-1 body] -> [body arg1 ... argN-1] */
  24196. duk_push_string(ctx, ",");
  24197. duk_insert(ctx, 1);
  24198. duk_join(ctx, nargs - 1);
  24199. }
  24200. /* [ body formals ], formals is comma separated list that needs to be parsed */
  24201. DUK_ASSERT_TOP(ctx, 2);
  24202. /* XXX: this placeholder is not always correct, but use for now.
  24203. * It will fail in corner cases; see test-dev-func-cons-args.js.
  24204. */
  24205. duk_push_string(ctx, "function(");
  24206. duk_dup(ctx, 1);
  24207. duk_push_string(ctx, "){");
  24208. duk_dup(ctx, 0);
  24209. duk_push_string(ctx, "}");
  24210. duk_concat(ctx, 5);
  24211. /* [ body formals source ] */
  24212. DUK_ASSERT_TOP(ctx, 3);
  24213. /* strictness is not inherited, intentional */
  24214. comp_flags = DUK_JS_COMPILE_FLAG_FUNCEXPR;
  24215. duk_push_hstring_stridx(ctx, DUK_STRIDX_COMPILE); /* XXX: copy from caller? */ /* XXX: ignored now */
  24216. h_sourcecode = duk_require_hstring(ctx, -2);
  24217. duk_js_compile(thr,
  24218. (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_sourcecode),
  24219. (duk_size_t) DUK_HSTRING_GET_BYTELEN(h_sourcecode),
  24220. comp_flags);
  24221. func = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1);
  24222. DUK_ASSERT(func != NULL);
  24223. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) func));
  24224. /* [ body formals source template ] */
  24225. /* only outer_lex_env matters, as functions always get a new
  24226. * variable declaration environment.
  24227. */
  24228. outer_lex_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  24229. outer_var_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  24230. duk_js_push_closure(thr, func, outer_var_env, outer_lex_env);
  24231. /* [ body formals source template closure ] */
  24232. return 1;
  24233. }
  24234. DUK_INTERNAL duk_ret_t duk_bi_function_prototype(duk_context *ctx) {
  24235. /* ignore arguments, return undefined (E5 Section 15.3.4) */
  24236. DUK_UNREF(ctx);
  24237. return 0;
  24238. }
  24239. DUK_INTERNAL duk_ret_t duk_bi_function_prototype_to_string(duk_context *ctx) {
  24240. duk_tval *tv;
  24241. /*
  24242. * E5 Section 15.3.4.2 places few requirements on the output of
  24243. * this function:
  24244. *
  24245. * - The result is an implementation dependent representation
  24246. * of the function; in particular
  24247. *
  24248. * - The result must follow the syntax of a FunctionDeclaration.
  24249. * In particular, the function must have a name (even in the
  24250. * case of an anonymous function or a function with an empty
  24251. * name).
  24252. *
  24253. * - Note in particular that the output does NOT need to compile
  24254. * into anything useful.
  24255. */
  24256. /* XXX: faster internal way to get this */
  24257. duk_push_this(ctx);
  24258. tv = duk_get_tval(ctx, -1);
  24259. DUK_ASSERT(tv != NULL);
  24260. if (DUK_TVAL_IS_OBJECT(tv)) {
  24261. duk_hobject *obj = DUK_TVAL_GET_OBJECT(tv);
  24262. const char *func_name = DUK_STR_ANON;
  24263. /* XXX: rework, it would be nice to avoid C formatting functions to
  24264. * ensure there are no Unicode issues.
  24265. */
  24266. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_NAME);
  24267. if (!duk_is_undefined(ctx, -1)) {
  24268. func_name = duk_to_string(ctx, -1);
  24269. DUK_ASSERT(func_name != NULL);
  24270. if (func_name[0] == (char) 0) {
  24271. func_name = DUK_STR_ANON;
  24272. }
  24273. }
  24274. if (DUK_HOBJECT_HAS_COMPILEDFUNCTION(obj)) {
  24275. /* XXX: actual source, if available */
  24276. duk_push_sprintf(ctx, "function %s() {/* ecmascript */}", (const char *) func_name);
  24277. } else if (DUK_HOBJECT_HAS_NATIVEFUNCTION(obj)) {
  24278. duk_push_sprintf(ctx, "function %s() {/* native */}", (const char *) func_name);
  24279. } else if (DUK_HOBJECT_HAS_BOUND(obj)) {
  24280. duk_push_sprintf(ctx, "function %s() {/* bound */}", (const char *) func_name);
  24281. } else {
  24282. goto type_error;
  24283. }
  24284. } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  24285. duk_push_lightfunc_tostring(ctx, tv);
  24286. } else {
  24287. goto type_error;
  24288. }
  24289. return 1;
  24290. type_error:
  24291. return DUK_RET_TYPE_ERROR;
  24292. }
  24293. DUK_INTERNAL duk_ret_t duk_bi_function_prototype_apply(duk_context *ctx) {
  24294. duk_idx_t len;
  24295. duk_idx_t i;
  24296. DUK_ASSERT_TOP(ctx, 2); /* not a vararg function */
  24297. duk_push_this(ctx);
  24298. if (!duk_is_callable(ctx, -1)) {
  24299. DUK_DDD(DUK_DDDPRINT("func is not callable"));
  24300. goto type_error;
  24301. }
  24302. duk_insert(ctx, 0);
  24303. DUK_ASSERT_TOP(ctx, 3);
  24304. DUK_DDD(DUK_DDDPRINT("func=%!iT, thisArg=%!iT, argArray=%!iT",
  24305. (duk_tval *) duk_get_tval(ctx, 0),
  24306. (duk_tval *) duk_get_tval(ctx, 1),
  24307. (duk_tval *) duk_get_tval(ctx, 2)));
  24308. /* [ func thisArg argArray ] */
  24309. if (duk_is_null_or_undefined(ctx, 2)) {
  24310. DUK_DDD(DUK_DDDPRINT("argArray is null/undefined, no args"));
  24311. len = 0;
  24312. } else if (!duk_is_object(ctx, 2)) {
  24313. goto type_error;
  24314. } else {
  24315. DUK_DDD(DUK_DDDPRINT("argArray is an object"));
  24316. /* XXX: make this an internal helper */
  24317. duk_get_prop_stridx(ctx, 2, DUK_STRIDX_LENGTH);
  24318. len = (duk_idx_t) duk_to_uint32(ctx, -1); /* ToUint32() coercion required */
  24319. duk_pop(ctx);
  24320. duk_require_stack(ctx, len);
  24321. DUK_DDD(DUK_DDDPRINT("argArray length is %ld", (long) len));
  24322. for (i = 0; i < len; i++) {
  24323. duk_get_prop_index(ctx, 2, i);
  24324. }
  24325. }
  24326. duk_remove(ctx, 2);
  24327. DUK_ASSERT_TOP(ctx, 2 + len);
  24328. /* [ func thisArg arg1 ... argN ] */
  24329. DUK_DDD(DUK_DDDPRINT("apply, func=%!iT, thisArg=%!iT, len=%ld",
  24330. (duk_tval *) duk_get_tval(ctx, 0),
  24331. (duk_tval *) duk_get_tval(ctx, 1),
  24332. (long) len));
  24333. duk_call_method(ctx, len);
  24334. return 1;
  24335. type_error:
  24336. return DUK_RET_TYPE_ERROR;
  24337. }
  24338. DUK_INTERNAL duk_ret_t duk_bi_function_prototype_call(duk_context *ctx) {
  24339. duk_idx_t nargs;
  24340. /* Step 1 is not necessary because duk_call_method() will take
  24341. * care of it.
  24342. */
  24343. /* vararg function, thisArg needs special handling */
  24344. nargs = duk_get_top(ctx); /* = 1 + arg count */
  24345. if (nargs == 0) {
  24346. duk_push_undefined(ctx);
  24347. nargs++;
  24348. }
  24349. DUK_ASSERT(nargs >= 1);
  24350. /* [ thisArg arg1 ... argN ] */
  24351. duk_push_this(ctx); /* 'func' in the algorithm */
  24352. duk_insert(ctx, 0);
  24353. /* [ func thisArg arg1 ... argN ] */
  24354. DUK_DDD(DUK_DDDPRINT("func=%!iT, thisArg=%!iT, argcount=%ld, top=%ld",
  24355. (duk_tval *) duk_get_tval(ctx, 0),
  24356. (duk_tval *) duk_get_tval(ctx, 1),
  24357. (long) (nargs - 1),
  24358. (long) duk_get_top(ctx)));
  24359. duk_call_method(ctx, nargs - 1);
  24360. return 1;
  24361. }
  24362. /* XXX: the implementation now assumes "chained" bound functions,
  24363. * whereas "collapsed" bound functions (where there is ever only
  24364. * one bound function which directly points to a non-bound, final
  24365. * function) would require a "collapsing" implementation which
  24366. * merges argument lists etc here.
  24367. */
  24368. DUK_INTERNAL duk_ret_t duk_bi_function_prototype_bind(duk_context *ctx) {
  24369. duk_hobject *h_bound;
  24370. duk_hobject *h_target;
  24371. duk_idx_t nargs;
  24372. duk_idx_t i;
  24373. /* vararg function, careful arg handling (e.g. thisArg may not be present) */
  24374. nargs = duk_get_top(ctx); /* = 1 + arg count */
  24375. if (nargs == 0) {
  24376. duk_push_undefined(ctx);
  24377. nargs++;
  24378. }
  24379. DUK_ASSERT(nargs >= 1);
  24380. duk_push_this(ctx);
  24381. if (!duk_is_callable(ctx, -1)) {
  24382. DUK_DDD(DUK_DDDPRINT("func is not callable"));
  24383. goto type_error;
  24384. }
  24385. /* [ thisArg arg1 ... argN func ] (thisArg+args == nargs total) */
  24386. DUK_ASSERT_TOP(ctx, nargs + 1);
  24387. /* create bound function object */
  24388. duk_push_object_helper(ctx,
  24389. DUK_HOBJECT_FLAG_EXTENSIBLE |
  24390. DUK_HOBJECT_FLAG_BOUND |
  24391. DUK_HOBJECT_FLAG_CONSTRUCTABLE |
  24392. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION),
  24393. DUK_BIDX_FUNCTION_PROTOTYPE);
  24394. h_bound = duk_get_hobject(ctx, -1);
  24395. DUK_ASSERT(h_bound != NULL);
  24396. /* [ thisArg arg1 ... argN func boundFunc ] */
  24397. duk_dup(ctx, -2); /* func */
  24398. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_TARGET, DUK_PROPDESC_FLAGS_NONE);
  24399. duk_dup(ctx, 0); /* thisArg */
  24400. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_THIS, DUK_PROPDESC_FLAGS_NONE);
  24401. duk_push_array(ctx);
  24402. /* [ thisArg arg1 ... argN func boundFunc argArray ] */
  24403. for (i = 0; i < nargs - 1; i++) {
  24404. duk_dup(ctx, 1 + i);
  24405. duk_put_prop_index(ctx, -2, i);
  24406. }
  24407. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_ARGS, DUK_PROPDESC_FLAGS_NONE);
  24408. /* [ thisArg arg1 ... argN func boundFunc ] */
  24409. /* bound function 'length' property is interesting */
  24410. h_target = duk_get_hobject(ctx, -2);
  24411. if (h_target == NULL || /* lightfunc */
  24412. DUK_HOBJECT_GET_CLASS_NUMBER(h_target) == DUK_HOBJECT_CLASS_FUNCTION) {
  24413. /* For lightfuncs, simply read the virtual property. */
  24414. duk_int_t tmp;
  24415. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH);
  24416. tmp = duk_to_int(ctx, -1) - (nargs - 1); /* step 15.a */
  24417. duk_pop(ctx);
  24418. duk_push_int(ctx, (tmp < 0 ? 0 : tmp));
  24419. } else {
  24420. duk_push_int(ctx, 0);
  24421. }
  24422. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE); /* attrs in E5 Section 15.3.5.1 */
  24423. /* caller and arguments must use the same thrower, [[ThrowTypeError]] */
  24424. duk_xdef_prop_stridx_thrower(ctx, -1, DUK_STRIDX_CALLER, DUK_PROPDESC_FLAGS_NONE);
  24425. duk_xdef_prop_stridx_thrower(ctx, -1, DUK_STRIDX_LC_ARGUMENTS, DUK_PROPDESC_FLAGS_NONE);
  24426. /* these non-standard properties are copied for convenience */
  24427. /* XXX: 'copy properties' API call? */
  24428. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_NAME);
  24429. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_WC);
  24430. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME);
  24431. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC);
  24432. /* The 'strict' flag is copied to get the special [[Get]] of E5.1
  24433. * Section 15.3.5.4 to apply when a 'caller' value is a strict bound
  24434. * function. Not sure if this is correct, because the specification
  24435. * is a bit ambiguous on this point but it would make sense.
  24436. */
  24437. if (h_target == NULL) {
  24438. /* Lightfuncs are always strict. */
  24439. DUK_HOBJECT_SET_STRICT(h_bound);
  24440. } else if (DUK_HOBJECT_HAS_STRICT(h_target)) {
  24441. DUK_HOBJECT_SET_STRICT(h_bound);
  24442. }
  24443. DUK_DDD(DUK_DDDPRINT("created bound function: %!iT", (duk_tval *) duk_get_tval(ctx, -1)));
  24444. return 1;
  24445. type_error:
  24446. return DUK_RET_TYPE_ERROR;
  24447. }
  24448. #line 1 "duk_bi_global.c"
  24449. /*
  24450. * Global object built-ins
  24451. */
  24452. /* include removed: duk_internal.h */
  24453. /*
  24454. * Encoding/decoding helpers
  24455. */
  24456. /* Macros for creating and checking bitmasks for character encoding.
  24457. * Bit number is a bit counterintuitive, but minimizes code size.
  24458. */
  24459. #define DUK__MKBITS(a,b,c,d,e,f,g,h) ((duk_uint8_t) ( \
  24460. ((a) << 0) | ((b) << 1) | ((c) << 2) | ((d) << 3) | \
  24461. ((e) << 4) | ((f) << 5) | ((g) << 6) | ((h) << 7) \
  24462. ))
  24463. #define DUK__CHECK_BITMASK(table,cp) ((table)[(cp) >> 3] & (1 << ((cp) & 0x07)))
  24464. /* E5.1 Section 15.1.3.3: uriReserved + uriUnescaped + '#' */
  24465. DUK_LOCAL const duk_uint8_t duk__encode_uriunescaped_table[16] = {
  24466. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */
  24467. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */
  24468. DUK__MKBITS(0, 1, 0, 1, 1, 0, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x20-0x2f */
  24469. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 0, 1, 0, 1), /* 0x30-0x3f */
  24470. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x40-0x4f */
  24471. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 1), /* 0x50-0x5f */
  24472. DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x60-0x6f */
  24473. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 1, 0), /* 0x70-0x7f */
  24474. };
  24475. /* E5.1 Section 15.1.3.4: uriUnescaped */
  24476. DUK_LOCAL const duk_uint8_t duk__encode_uricomponent_unescaped_table[16] = {
  24477. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */
  24478. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */
  24479. DUK__MKBITS(0, 1, 0, 0, 0, 0, 0, 1), DUK__MKBITS(1, 1, 1, 0, 0, 1, 1, 0), /* 0x20-0x2f */
  24480. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 0, 0, 0, 0, 0, 0), /* 0x30-0x3f */
  24481. DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x40-0x4f */
  24482. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 1), /* 0x50-0x5f */
  24483. DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x60-0x6f */
  24484. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 1, 0), /* 0x70-0x7f */
  24485. };
  24486. /* E5.1 Section 15.1.3.1: uriReserved + '#' */
  24487. DUK_LOCAL const duk_uint8_t duk__decode_uri_reserved_table[16] = {
  24488. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */
  24489. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */
  24490. DUK__MKBITS(0, 0, 0, 1, 1, 0, 1, 0), DUK__MKBITS(0, 0, 0, 1, 1, 0, 0, 1), /* 0x20-0x2f */
  24491. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 1, 1, 0, 1, 0, 1), /* 0x30-0x3f */
  24492. DUK__MKBITS(1, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x40-0x4f */
  24493. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x50-0x5f */
  24494. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x60-0x6f */
  24495. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x70-0x7f */
  24496. };
  24497. /* E5.1 Section 15.1.3.2: empty */
  24498. DUK_LOCAL const duk_uint8_t duk__decode_uri_component_reserved_table[16] = {
  24499. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */
  24500. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */
  24501. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x20-0x2f */
  24502. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x30-0x3f */
  24503. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x40-0x4f */
  24504. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x50-0x5f */
  24505. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x60-0x6f */
  24506. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x70-0x7f */
  24507. };
  24508. #ifdef DUK_USE_SECTION_B
  24509. /* E5.1 Section B.2.2, step 7. */
  24510. DUK_LOCAL const duk_uint8_t duk__escape_unescaped_table[16] = {
  24511. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */
  24512. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */
  24513. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 1, 1, 0, 1, 1, 1), /* 0x20-0x2f */
  24514. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 0, 0, 0, 0, 0, 0), /* 0x30-0x3f */
  24515. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x40-0x4f */
  24516. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 1), /* 0x50-0x5f */
  24517. DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x60-0x6f */
  24518. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 0) /* 0x70-0x7f */
  24519. };
  24520. #endif /* DUK_USE_SECTION_B */
  24521. typedef struct {
  24522. duk_hthread *thr;
  24523. duk_hstring *h_str;
  24524. duk_hbuffer_dynamic *h_buf;
  24525. const duk_uint8_t *p;
  24526. const duk_uint8_t *p_start;
  24527. const duk_uint8_t *p_end;
  24528. } duk__transform_context;
  24529. typedef void (*duk__transform_callback)(duk__transform_context *tfm_ctx, void *udata, duk_codepoint_t cp);
  24530. /* XXX: refactor and share with other code */
  24531. DUK_LOCAL duk_small_int_t duk__decode_hex_escape(const duk_uint8_t *p, duk_small_int_t n) {
  24532. duk_small_int_t ch;
  24533. duk_small_int_t t = 0;
  24534. while (n > 0) {
  24535. t = t * 16;
  24536. ch = (duk_small_int_t) duk_hex_dectab[*p++];
  24537. if (DUK_LIKELY(ch >= 0)) {
  24538. t += ch;
  24539. } else {
  24540. return -1;
  24541. }
  24542. n--;
  24543. }
  24544. return t;
  24545. }
  24546. DUK_LOCAL int duk__transform_helper(duk_context *ctx, duk__transform_callback callback, void *udata) {
  24547. duk_hthread *thr = (duk_hthread *) ctx;
  24548. duk__transform_context tfm_ctx_alloc;
  24549. duk__transform_context *tfm_ctx = &tfm_ctx_alloc;
  24550. duk_codepoint_t cp;
  24551. tfm_ctx->thr = thr;
  24552. tfm_ctx->h_str = duk_to_hstring(ctx, 0);
  24553. DUK_ASSERT(tfm_ctx->h_str != NULL);
  24554. (void) duk_push_dynamic_buffer(ctx, 0);
  24555. tfm_ctx->h_buf = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, -1);
  24556. DUK_ASSERT(tfm_ctx->h_buf != NULL);
  24557. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(tfm_ctx->h_buf));
  24558. tfm_ctx->p_start = DUK_HSTRING_GET_DATA(tfm_ctx->h_str);
  24559. tfm_ctx->p_end = tfm_ctx->p_start + DUK_HSTRING_GET_BYTELEN(tfm_ctx->h_str);
  24560. tfm_ctx->p = tfm_ctx->p_start;
  24561. while (tfm_ctx->p < tfm_ctx->p_end) {
  24562. cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &tfm_ctx->p, tfm_ctx->p_start, tfm_ctx->p_end);
  24563. callback(tfm_ctx, udata, cp);
  24564. }
  24565. duk_to_string(ctx, -1);
  24566. return 1;
  24567. }
  24568. DUK_LOCAL void duk__transform_callback_encode_uri(duk__transform_context *tfm_ctx, void *udata, duk_codepoint_t cp) {
  24569. duk_uint8_t xutf8_buf[DUK_UNICODE_MAX_XUTF8_LENGTH];
  24570. duk_uint8_t buf[3];
  24571. duk_small_int_t len;
  24572. duk_codepoint_t cp1, cp2;
  24573. duk_small_int_t i, t;
  24574. const duk_uint8_t *unescaped_table = (duk_uint8_t *) udata;
  24575. if (cp < 0) {
  24576. goto uri_error;
  24577. } else if ((cp < 0x80L) && DUK__CHECK_BITMASK(unescaped_table, cp)) {
  24578. duk_hbuffer_append_byte(tfm_ctx->thr, tfm_ctx->h_buf, (duk_uint8_t) cp);
  24579. return;
  24580. } else if (cp >= 0xdc00L && cp <= 0xdfffL) {
  24581. goto uri_error;
  24582. } else if (cp >= 0xd800L && cp <= 0xdbffL) {
  24583. /* Needs lookahead */
  24584. if (duk_unicode_decode_xutf8(tfm_ctx->thr, &tfm_ctx->p, tfm_ctx->p_start, tfm_ctx->p_end, (duk_ucodepoint_t *) &cp2) == 0) {
  24585. goto uri_error;
  24586. }
  24587. if (!(cp2 >= 0xdc00L && cp2 <= 0xdfffL)) {
  24588. goto uri_error;
  24589. }
  24590. cp1 = cp;
  24591. cp = ((cp1 - 0xd800L) << 10) + (cp2 - 0xdc00L) + 0x10000L;
  24592. } else if (cp > 0x10ffffL) {
  24593. /* Although we can allow non-BMP characters (they'll decode
  24594. * back into surrogate pairs), we don't allow extended UTF-8
  24595. * characters; they would encode to URIs which won't decode
  24596. * back because of strict UTF-8 checks in URI decoding.
  24597. * (However, we could just as well allow them here.)
  24598. */
  24599. goto uri_error;
  24600. } else {
  24601. /* Non-BMP characters within valid UTF-8 range: encode as is.
  24602. * They'll decode back into surrogate pairs.
  24603. */
  24604. ;
  24605. }
  24606. len = duk_unicode_encode_xutf8((duk_ucodepoint_t) cp, xutf8_buf);
  24607. buf[0] = (duk_uint8_t) '%';
  24608. for (i = 0; i < len; i++) {
  24609. t = (int) xutf8_buf[i];
  24610. buf[1] = (duk_uint8_t) duk_uc_nybbles[t >> 4];
  24611. buf[2] = (duk_uint8_t) duk_uc_nybbles[t & 0x0f];
  24612. duk_hbuffer_append_bytes(tfm_ctx->thr, tfm_ctx->h_buf, buf, 3);
  24613. }
  24614. return;
  24615. uri_error:
  24616. DUK_ERROR(tfm_ctx->thr, DUK_ERR_URI_ERROR, "invalid input");
  24617. }
  24618. DUK_LOCAL void duk__transform_callback_decode_uri(duk__transform_context *tfm_ctx, void *udata, duk_codepoint_t cp) {
  24619. const duk_uint8_t *reserved_table = (duk_uint8_t *) udata;
  24620. duk_small_uint_t utf8_blen;
  24621. duk_codepoint_t min_cp;
  24622. duk_small_int_t t; /* must be signed */
  24623. duk_small_uint_t i;
  24624. if (cp == (duk_codepoint_t) '%') {
  24625. const duk_uint8_t *p = tfm_ctx->p;
  24626. duk_size_t left = (duk_size_t) (tfm_ctx->p_end - p); /* bytes left */
  24627. DUK_DDD(DUK_DDDPRINT("percent encoding, left=%ld", (long) left));
  24628. if (left < 2) {
  24629. goto uri_error;
  24630. }
  24631. t = duk__decode_hex_escape(p, 2);
  24632. DUK_DDD(DUK_DDDPRINT("first byte: %ld", (long) t));
  24633. if (t < 0) {
  24634. goto uri_error;
  24635. }
  24636. if (t < 0x80) {
  24637. if (DUK__CHECK_BITMASK(reserved_table, t)) {
  24638. /* decode '%xx' to '%xx' if decoded char in reserved set */
  24639. DUK_ASSERT(tfm_ctx->p - 1 >= tfm_ctx->p_start);
  24640. duk_hbuffer_append_bytes(tfm_ctx->thr, tfm_ctx->h_buf, (duk_uint8_t *) (p - 1), 3);
  24641. } else {
  24642. duk_hbuffer_append_byte(tfm_ctx->thr, tfm_ctx->h_buf, (duk_uint8_t) t);
  24643. }
  24644. tfm_ctx->p += 2;
  24645. return;
  24646. }
  24647. /* Decode UTF-8 codepoint from a sequence of hex escapes. The
  24648. * first byte of the sequence has been decoded to 't'.
  24649. *
  24650. * Note that UTF-8 validation must be strict according to the
  24651. * specification: E5.1 Section 15.1.3, decode algorithm step
  24652. * 4.d.vii.8. URIError from non-shortest encodings is also
  24653. * specifically noted in the spec.
  24654. */
  24655. DUK_ASSERT(t >= 0x80);
  24656. if (t < 0xc0) {
  24657. /* continuation byte */
  24658. goto uri_error;
  24659. } else if (t < 0xe0) {
  24660. /* 110x xxxx; 2 bytes */
  24661. utf8_blen = 2;
  24662. min_cp = 0x80L;
  24663. cp = t & 0x1f;
  24664. } else if (t < 0xf0) {
  24665. /* 1110 xxxx; 3 bytes */
  24666. utf8_blen = 3;
  24667. min_cp = 0x800L;
  24668. cp = t & 0x0f;
  24669. } else if (t < 0xf8) {
  24670. /* 1111 0xxx; 4 bytes */
  24671. utf8_blen = 4;
  24672. min_cp = 0x10000L;
  24673. cp = t & 0x07;
  24674. } else {
  24675. /* extended utf-8 not allowed for URIs */
  24676. goto uri_error;
  24677. }
  24678. if (left < utf8_blen * 3 - 1) {
  24679. /* '%xx%xx...%xx', p points to char after first '%' */
  24680. goto uri_error;
  24681. }
  24682. p += 3;
  24683. for (i = 1; i < utf8_blen; i++) {
  24684. /* p points to digit part ('%xy', p points to 'x') */
  24685. t = duk__decode_hex_escape(p, 2);
  24686. DUK_DDD(DUK_DDDPRINT("i=%ld utf8_blen=%ld cp=%ld t=0x%02lx",
  24687. (long) i, (long) utf8_blen, (long) cp, (unsigned long) t));
  24688. if (t < 0) {
  24689. goto uri_error;
  24690. }
  24691. if ((t & 0xc0) != 0x80) {
  24692. goto uri_error;
  24693. }
  24694. cp = (cp << 6) + (t & 0x3f);
  24695. p += 3;
  24696. }
  24697. p--; /* p overshoots */
  24698. tfm_ctx->p = p;
  24699. DUK_DDD(DUK_DDDPRINT("final cp=%ld, min_cp=%ld", (long) cp, (long) min_cp));
  24700. if (cp < min_cp || cp > 0x10ffffL || (cp >= 0xd800L && cp <= 0xdfffL)) {
  24701. goto uri_error;
  24702. }
  24703. /* The E5.1 algorithm checks whether or not a decoded codepoint
  24704. * is below 0x80 and perhaps may be in the "reserved" set.
  24705. * This seems pointless because the single byte UTF-8 case is
  24706. * handled separately, and non-shortest encodings are rejected.
  24707. * So, 'cp' cannot be below 0x80 here, and thus cannot be in
  24708. * the reserved set.
  24709. */
  24710. /* utf-8 validation ensures these */
  24711. DUK_ASSERT(cp >= 0x80L && cp <= 0x10ffffL);
  24712. if (cp >= 0x10000L) {
  24713. cp -= 0x10000L;
  24714. DUK_ASSERT(cp < 0x100000L);
  24715. duk_hbuffer_append_xutf8(tfm_ctx->thr, tfm_ctx->h_buf, (duk_ucodepoint_t) ((cp >> 10) + 0xd800L));
  24716. duk_hbuffer_append_xutf8(tfm_ctx->thr, tfm_ctx->h_buf, (duk_ucodepoint_t) ((cp & 0x03ffUL) + 0xdc00L));
  24717. } else {
  24718. duk_hbuffer_append_xutf8(tfm_ctx->thr, tfm_ctx->h_buf, (duk_ucodepoint_t) cp);
  24719. }
  24720. } else {
  24721. duk_hbuffer_append_xutf8(tfm_ctx->thr, tfm_ctx->h_buf, (duk_ucodepoint_t) cp);
  24722. }
  24723. return;
  24724. uri_error:
  24725. DUK_ERROR(tfm_ctx->thr, DUK_ERR_URI_ERROR, "invalid input");
  24726. }
  24727. #ifdef DUK_USE_SECTION_B
  24728. DUK_LOCAL void duk__transform_callback_escape(duk__transform_context *tfm_ctx, void *udata, duk_codepoint_t cp) {
  24729. duk_uint8_t buf[6];
  24730. duk_small_int_t len;
  24731. DUK_UNREF(udata);
  24732. if (cp < 0) {
  24733. goto esc_error;
  24734. } else if ((cp < 0x80L) && DUK__CHECK_BITMASK(duk__escape_unescaped_table, cp)) {
  24735. buf[0] = (duk_uint8_t) cp;
  24736. len = 1;
  24737. } else if (cp < 0x100L) {
  24738. buf[0] = (duk_uint8_t) '%';
  24739. buf[1] = (duk_uint8_t) duk_uc_nybbles[cp >> 4];
  24740. buf[2] = (duk_uint8_t) duk_uc_nybbles[cp & 0x0f];
  24741. len = 3;
  24742. } else if (cp < 0x10000L) {
  24743. buf[0] = (duk_uint8_t) '%';
  24744. buf[1] = (duk_uint8_t) 'u';
  24745. buf[2] = (duk_uint8_t) duk_uc_nybbles[cp >> 12];
  24746. buf[3] = (duk_uint8_t) duk_uc_nybbles[(cp >> 8) & 0x0f];
  24747. buf[4] = (duk_uint8_t) duk_uc_nybbles[(cp >> 4) & 0x0f];
  24748. buf[5] = (duk_uint8_t) duk_uc_nybbles[cp & 0x0f];
  24749. len = 6;
  24750. } else {
  24751. /* Characters outside BMP cannot be escape()'d. We could
  24752. * encode them as surrogate pairs (for codepoints inside
  24753. * valid UTF-8 range, but not extended UTF-8). Because
  24754. * escape() and unescape() are legacy functions, we don't.
  24755. */
  24756. goto esc_error;
  24757. }
  24758. duk_hbuffer_append_bytes(tfm_ctx->thr, tfm_ctx->h_buf, buf, len);
  24759. return;
  24760. esc_error:
  24761. DUK_ERROR(tfm_ctx->thr, DUK_ERR_TYPE_ERROR, "invalid input");
  24762. }
  24763. DUK_LOCAL void duk__transform_callback_unescape(duk__transform_context *tfm_ctx, void *udata, duk_codepoint_t cp) {
  24764. duk_small_int_t t;
  24765. DUK_UNREF(udata);
  24766. if (cp == (duk_codepoint_t) '%') {
  24767. const duk_uint8_t *p = tfm_ctx->p;
  24768. duk_size_t left = (duk_size_t) (tfm_ctx->p_end - p); /* bytes left */
  24769. if (left >= 5 && p[0] == 'u' &&
  24770. ((t = duk__decode_hex_escape(p + 1, 4)) >= 0)) {
  24771. cp = (duk_codepoint_t) t;
  24772. tfm_ctx->p += 5;
  24773. } else if (left >= 2 &&
  24774. ((t = duk__decode_hex_escape(p, 2)) >= 0)) {
  24775. cp = (duk_codepoint_t) t;
  24776. tfm_ctx->p += 2;
  24777. }
  24778. }
  24779. duk_hbuffer_append_xutf8(tfm_ctx->thr, tfm_ctx->h_buf, cp);
  24780. }
  24781. #endif /* DUK_USE_SECTION_B */
  24782. /*
  24783. * Eval
  24784. *
  24785. * Eval needs to handle both a "direct eval" and an "indirect eval".
  24786. * Direct eval handling needs access to the caller's activation so that its
  24787. * lexical environment can be accessed. A direct eval is only possible from
  24788. * Ecmascript code; an indirect eval call is possible also from C code.
  24789. * When an indirect eval call is made from C code, there may not be a
  24790. * calling activation at all which needs careful handling.
  24791. */
  24792. DUK_INTERNAL duk_ret_t duk_bi_global_object_eval(duk_context *ctx) {
  24793. duk_hthread *thr = (duk_hthread *) ctx;
  24794. duk_hstring *h;
  24795. duk_activation *act_caller;
  24796. duk_activation *act_eval;
  24797. duk_activation *act;
  24798. duk_hcompiledfunction *func;
  24799. duk_hobject *outer_lex_env;
  24800. duk_hobject *outer_var_env;
  24801. duk_bool_t this_to_global = 1;
  24802. duk_small_uint_t comp_flags;
  24803. DUK_ASSERT_TOP(ctx, 1);
  24804. DUK_ASSERT(thr->callstack_top >= 1); /* at least this function exists */
  24805. DUK_ASSERT(((thr->callstack + thr->callstack_top - 1)->flags & DUK_ACT_FLAG_DIRECT_EVAL) == 0 || /* indirect eval */
  24806. (thr->callstack_top >= 2)); /* if direct eval, calling activation must exist */
  24807. /*
  24808. * callstack_top - 1 --> this function
  24809. * callstack_top - 2 --> caller (may not exist)
  24810. *
  24811. * If called directly from C, callstack_top might be 1. If calling
  24812. * activation doesn't exist, call must be indirect.
  24813. */
  24814. h = duk_get_hstring(ctx, 0);
  24815. if (!h) {
  24816. return 1; /* return arg as-is */
  24817. }
  24818. /* [ source ] */
  24819. comp_flags = DUK_JS_COMPILE_FLAG_EVAL;
  24820. act_eval = thr->callstack + thr->callstack_top - 1; /* this function */
  24821. if (thr->callstack_top >= 2) {
  24822. /* Have a calling activation, check for direct eval (otherwise
  24823. * assume indirect eval.
  24824. */
  24825. act_caller = thr->callstack + thr->callstack_top - 2; /* caller */
  24826. if ((act_caller->flags & DUK_ACT_FLAG_STRICT) &&
  24827. (act_eval->flags & DUK_ACT_FLAG_DIRECT_EVAL)) {
  24828. /* Only direct eval inherits strictness from calling code
  24829. * (E5.1 Section 10.1.1).
  24830. */
  24831. comp_flags |= DUK_JS_COMPILE_FLAG_STRICT;
  24832. }
  24833. } else {
  24834. DUK_ASSERT((act_eval->flags & DUK_ACT_FLAG_DIRECT_EVAL) == 0);
  24835. }
  24836. act_caller = NULL; /* avoid dereference after potential callstack realloc */
  24837. act_eval = NULL;
  24838. duk_push_hstring_stridx(ctx, DUK_STRIDX_INPUT); /* XXX: copy from caller? */
  24839. duk_js_compile(thr,
  24840. (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h),
  24841. (duk_size_t) DUK_HSTRING_GET_BYTELEN(h),
  24842. comp_flags);
  24843. func = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1);
  24844. DUK_ASSERT(func != NULL);
  24845. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) func));
  24846. /* [ source template ] */
  24847. /* E5 Section 10.4.2 */
  24848. DUK_ASSERT(thr->callstack_top >= 1);
  24849. act = thr->callstack + thr->callstack_top - 1; /* this function */
  24850. if (act->flags & DUK_ACT_FLAG_DIRECT_EVAL) {
  24851. DUK_ASSERT(thr->callstack_top >= 2);
  24852. act = thr->callstack + thr->callstack_top - 2; /* caller */
  24853. if (act->lex_env == NULL) {
  24854. DUK_ASSERT(act->var_env == NULL);
  24855. DUK_DDD(DUK_DDDPRINT("delayed environment initialization"));
  24856. /* this may have side effects, so re-lookup act */
  24857. duk_js_init_activation_environment_records_delayed(thr, act);
  24858. act = thr->callstack + thr->callstack_top - 2;
  24859. }
  24860. DUK_ASSERT(act->lex_env != NULL);
  24861. DUK_ASSERT(act->var_env != NULL);
  24862. this_to_global = 0;
  24863. if (DUK_HOBJECT_HAS_STRICT((duk_hobject *) func)) {
  24864. duk_hobject *new_env;
  24865. duk_hobject *act_lex_env;
  24866. DUK_DDD(DUK_DDDPRINT("direct eval call to a strict function -> "
  24867. "var_env and lex_env to a fresh env, "
  24868. "this_binding to caller's this_binding"));
  24869. act = thr->callstack + thr->callstack_top - 2; /* caller */
  24870. act_lex_env = act->lex_env;
  24871. act = NULL; /* invalidated */
  24872. (void) duk_push_object_helper_proto(ctx,
  24873. DUK_HOBJECT_FLAG_EXTENSIBLE |
  24874. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV),
  24875. act_lex_env);
  24876. new_env = duk_require_hobject(ctx, -1);
  24877. DUK_ASSERT(new_env != NULL);
  24878. DUK_DDD(DUK_DDDPRINT("new_env allocated: %!iO",
  24879. (duk_heaphdr *) new_env));
  24880. outer_lex_env = new_env;
  24881. outer_var_env = new_env;
  24882. duk_insert(ctx, 0); /* stash to bottom of value stack to keep new_env reachable */
  24883. /* compiler's responsibility */
  24884. DUK_ASSERT(DUK_HOBJECT_HAS_NEWENV((duk_hobject *) func));
  24885. } else {
  24886. DUK_DDD(DUK_DDDPRINT("direct eval call to a non-strict function -> "
  24887. "var_env and lex_env to caller's envs, "
  24888. "this_binding to caller's this_binding"));
  24889. outer_lex_env = act->lex_env;
  24890. outer_var_env = act->var_env;
  24891. /* compiler's responsibility */
  24892. DUK_ASSERT(!DUK_HOBJECT_HAS_NEWENV((duk_hobject *) func));
  24893. }
  24894. } else {
  24895. DUK_DDD(DUK_DDDPRINT("indirect eval call -> var_env and lex_env to "
  24896. "global object, this_binding to global object"));
  24897. this_to_global = 1;
  24898. outer_lex_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  24899. outer_var_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  24900. }
  24901. act = NULL;
  24902. duk_js_push_closure(thr, func, outer_var_env, outer_lex_env);
  24903. /* [ source template closure ] */
  24904. if (this_to_global) {
  24905. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
  24906. duk_push_hobject_bidx(ctx, DUK_BIDX_GLOBAL);
  24907. } else {
  24908. duk_tval *tv;
  24909. DUK_ASSERT(thr->callstack_top >= 2);
  24910. act = thr->callstack + thr->callstack_top - 2; /* caller */
  24911. tv = thr->valstack + act->idx_bottom - 1; /* this is just beneath bottom */
  24912. DUK_ASSERT(tv >= thr->valstack);
  24913. duk_push_tval(ctx, tv);
  24914. }
  24915. DUK_DDD(DUK_DDDPRINT("eval -> lex_env=%!iO, var_env=%!iO, this_binding=%!T",
  24916. (duk_heaphdr *) outer_lex_env,
  24917. (duk_heaphdr *) outer_var_env,
  24918. (duk_tval *) duk_get_tval(ctx, -1)));
  24919. /* [ source template closure this ] */
  24920. duk_call_method(ctx, 0);
  24921. /* [ source template result ] */
  24922. return 1;
  24923. }
  24924. /*
  24925. * Parsing of ints and floats
  24926. */
  24927. DUK_INTERNAL duk_ret_t duk_bi_global_object_parse_int(duk_context *ctx) {
  24928. duk_bool_t strip_prefix;
  24929. duk_int32_t radix;
  24930. duk_small_uint_t s2n_flags;
  24931. DUK_ASSERT_TOP(ctx, 2);
  24932. duk_to_string(ctx, 0);
  24933. strip_prefix = 1;
  24934. radix = duk_to_int32(ctx, 1);
  24935. if (radix != 0) {
  24936. if (radix < 2 || radix > 36) {
  24937. goto ret_nan;
  24938. }
  24939. /* For octal, setting strip_prefix=0 is not necessary, as zero
  24940. * is tolerated anyway:
  24941. *
  24942. * parseInt('123', 8) === parseInt('0123', 8) with or without strip_prefix
  24943. * parseInt('123', 16) === parseInt('0x123', 16) requires strip_prefix = 1
  24944. */
  24945. if (radix != 16) {
  24946. strip_prefix = 0;
  24947. }
  24948. } else {
  24949. radix = 10;
  24950. }
  24951. s2n_flags = DUK_S2N_FLAG_TRIM_WHITE |
  24952. DUK_S2N_FLAG_ALLOW_GARBAGE |
  24953. DUK_S2N_FLAG_ALLOW_PLUS |
  24954. DUK_S2N_FLAG_ALLOW_MINUS |
  24955. DUK_S2N_FLAG_ALLOW_LEADING_ZERO |
  24956. #ifdef DUK_USE_OCTAL_SUPPORT
  24957. (strip_prefix ? (DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT | DUK_S2N_FLAG_ALLOW_AUTO_OCT_INT) : 0)
  24958. #else
  24959. (strip_prefix ? DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT : 0)
  24960. #endif
  24961. ;
  24962. duk_dup(ctx, 0);
  24963. duk_numconv_parse(ctx, radix, s2n_flags);
  24964. return 1;
  24965. ret_nan:
  24966. duk_push_nan(ctx);
  24967. return 1;
  24968. }
  24969. DUK_INTERNAL duk_ret_t duk_bi_global_object_parse_float(duk_context *ctx) {
  24970. duk_small_uint_t s2n_flags;
  24971. duk_int32_t radix;
  24972. DUK_ASSERT_TOP(ctx, 1);
  24973. duk_to_string(ctx, 0);
  24974. radix = 10;
  24975. /* XXX: check flags */
  24976. s2n_flags = DUK_S2N_FLAG_TRIM_WHITE |
  24977. DUK_S2N_FLAG_ALLOW_EXP |
  24978. DUK_S2N_FLAG_ALLOW_GARBAGE |
  24979. DUK_S2N_FLAG_ALLOW_PLUS |
  24980. DUK_S2N_FLAG_ALLOW_MINUS |
  24981. DUK_S2N_FLAG_ALLOW_INF |
  24982. DUK_S2N_FLAG_ALLOW_FRAC |
  24983. DUK_S2N_FLAG_ALLOW_NAKED_FRAC |
  24984. DUK_S2N_FLAG_ALLOW_EMPTY_FRAC |
  24985. DUK_S2N_FLAG_ALLOW_LEADING_ZERO;
  24986. duk_numconv_parse(ctx, radix, s2n_flags);
  24987. return 1;
  24988. }
  24989. /*
  24990. * Number checkers
  24991. */
  24992. DUK_INTERNAL duk_ret_t duk_bi_global_object_is_nan(duk_context *ctx) {
  24993. duk_double_t d = duk_to_number(ctx, 0);
  24994. duk_push_boolean(ctx, DUK_ISNAN(d));
  24995. return 1;
  24996. }
  24997. DUK_INTERNAL duk_ret_t duk_bi_global_object_is_finite(duk_context *ctx) {
  24998. duk_double_t d = duk_to_number(ctx, 0);
  24999. duk_push_boolean(ctx, DUK_ISFINITE(d));
  25000. return 1;
  25001. }
  25002. /*
  25003. * URI handling
  25004. */
  25005. DUK_INTERNAL duk_ret_t duk_bi_global_object_decode_uri(duk_context *ctx) {
  25006. return duk__transform_helper(ctx, duk__transform_callback_decode_uri, (void *) duk__decode_uri_reserved_table);
  25007. }
  25008. DUK_INTERNAL duk_ret_t duk_bi_global_object_decode_uri_component(duk_context *ctx) {
  25009. return duk__transform_helper(ctx, duk__transform_callback_decode_uri, (void *) duk__decode_uri_component_reserved_table);
  25010. }
  25011. DUK_INTERNAL duk_ret_t duk_bi_global_object_encode_uri(duk_context *ctx) {
  25012. return duk__transform_helper(ctx, duk__transform_callback_encode_uri, (void *) duk__encode_uriunescaped_table);
  25013. }
  25014. DUK_INTERNAL duk_ret_t duk_bi_global_object_encode_uri_component(duk_context *ctx) {
  25015. return duk__transform_helper(ctx, duk__transform_callback_encode_uri, (void *) duk__encode_uricomponent_unescaped_table);
  25016. }
  25017. #ifdef DUK_USE_SECTION_B
  25018. DUK_INTERNAL duk_ret_t duk_bi_global_object_escape(duk_context *ctx) {
  25019. return duk__transform_helper(ctx, duk__transform_callback_escape, (void *) NULL);
  25020. }
  25021. DUK_INTERNAL duk_ret_t duk_bi_global_object_unescape(duk_context *ctx) {
  25022. return duk__transform_helper(ctx, duk__transform_callback_unescape, (void *) NULL);
  25023. }
  25024. #else /* DUK_USE_SECTION_B */
  25025. DUK_INTERNAL duk_ret_t duk_bi_global_object_escape(duk_context *ctx) {
  25026. DUK_UNREF(ctx);
  25027. return DUK_RET_UNSUPPORTED_ERROR;
  25028. }
  25029. DUK_INTERNAL duk_ret_t duk_bi_global_object_unescape(duk_context *ctx) {
  25030. DUK_UNREF(ctx);
  25031. return DUK_RET_UNSUPPORTED_ERROR;
  25032. }
  25033. #endif /* DUK_USE_SECTION_B */
  25034. #if defined(DUK_USE_BROWSER_LIKE) && (defined(DUK_USE_FILE_IO) || defined(DUK_USE_DEBUGGER_SUPPORT))
  25035. DUK_INTERNAL duk_ret_t duk_bi_global_object_print_helper(duk_context *ctx) {
  25036. duk_hthread *thr = (duk_hthread *) ctx;
  25037. duk_int_t magic;
  25038. duk_idx_t nargs;
  25039. const duk_uint8_t *buf;
  25040. duk_size_t sz_buf;
  25041. const char nl = (const char) DUK_ASC_LF;
  25042. #ifndef DUK_USE_PREFER_SIZE
  25043. duk_uint8_t buf_stack[256];
  25044. #endif
  25045. #ifdef DUK_USE_FILE_IO
  25046. duk_file *f_out;
  25047. #endif
  25048. magic = duk_get_current_magic(ctx);
  25049. DUK_UNREF(magic);
  25050. nargs = duk_get_top(ctx);
  25051. /* If argument count is 1 and first argument is a buffer, write the buffer
  25052. * as raw data into the file without a newline; this allows exact control
  25053. * over stdout/stderr without an additional entrypoint (useful for now).
  25054. *
  25055. * Otherwise current print/alert semantics are to ToString() coerce
  25056. * arguments, join them with a single space, and append a newline.
  25057. */
  25058. if (nargs == 1 && duk_is_buffer(ctx, 0)) {
  25059. buf = (const duk_uint8_t *) duk_get_buffer(ctx, 0, &sz_buf);
  25060. DUK_ASSERT(buf != NULL);
  25061. } else if (nargs > 0) {
  25062. #ifdef DUK_USE_PREFER_SIZE
  25063. /* Compact but lots of churn. */
  25064. duk_push_hstring_stridx(thr, DUK_STRIDX_SPACE);
  25065. duk_insert(ctx, 0);
  25066. duk_join(ctx, nargs);
  25067. duk_push_string(thr, "\n");
  25068. duk_concat(ctx, 2);
  25069. buf = (const duk_uint8_t *) duk_get_lstring(ctx, -1, &sz_buf);
  25070. DUK_ASSERT(buf != NULL);
  25071. #else /* DUK_USE_PREFER_SIZE */
  25072. /* Higher footprint, less churn. */
  25073. duk_idx_t i;
  25074. duk_size_t sz_str;
  25075. const duk_uint8_t *p_str;
  25076. duk_uint8_t *p;
  25077. sz_buf = (duk_size_t) nargs; /* spaces (nargs - 1) + newline */
  25078. for (i = 0; i < nargs; i++) {
  25079. (void) duk_to_lstring(ctx, i, &sz_str);
  25080. sz_buf += sz_str;
  25081. }
  25082. if (sz_buf <= sizeof(buf_stack)) {
  25083. buf = (const duk_uint8_t *) buf_stack;
  25084. } else {
  25085. buf = (const duk_uint8_t *) duk_push_fixed_buffer(ctx, sz_buf);
  25086. DUK_ASSERT(buf != NULL);
  25087. }
  25088. p = (duk_uint8_t *) buf;
  25089. for (i = 0; i < nargs; i++) {
  25090. p_str = (const duk_uint8_t *) duk_get_lstring(ctx, i, &sz_str);
  25091. DUK_ASSERT(p_str != NULL);
  25092. DUK_MEMCPY((void *) p, (const void *) p_str, sz_str);
  25093. p += sz_str;
  25094. *p++ = (duk_uint8_t) (i == nargs - 1 ? DUK_ASC_LF : DUK_ASC_SPACE);
  25095. }
  25096. DUK_ASSERT((const duk_uint8_t *) p == buf + sz_total);
  25097. #endif /* DUK_USE_PREFER_SIZE */
  25098. } else {
  25099. buf = (const duk_uint8_t *) &nl;
  25100. sz_buf = 1;
  25101. }
  25102. /* 'buf' contains the string to write, 'sz_buf' contains the length
  25103. * (which may be zero).
  25104. */
  25105. DUK_ASSERT(buf != NULL);
  25106. if (sz_buf == 0) {
  25107. return 0;
  25108. }
  25109. #ifdef DUK_USE_FILE_IO
  25110. f_out = (magic ? DUK_STDERR : DUK_STDOUT);
  25111. DUK_FWRITE((const void *) buf, 1, (size_t) sz_buf, f_out);
  25112. DUK_FFLUSH(f_out);
  25113. #endif
  25114. #if defined(DUK_USE_DEBUGGER_SUPPORT) && defined(DUK_USE_DEBUGGER_FWD_PRINTALERT)
  25115. if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) {
  25116. duk_debug_write_notify(thr, magic ? DUK_DBG_CMD_ALERT : DUK_DBG_CMD_PRINT);
  25117. duk_debug_write_string(thr, (const char *) buf, sz_buf);
  25118. duk_debug_write_eom(thr);
  25119. }
  25120. #endif
  25121. return 0;
  25122. }
  25123. #elif defined(DUK_USE_BROWSER_LIKE) /* print provider */
  25124. DUK_INTERNAL duk_ret_t duk_bi_global_object_print_helper(duk_context *ctx) {
  25125. DUK_UNREF(ctx);
  25126. return 0;
  25127. }
  25128. #else /* print provider */
  25129. DUK_INTERNAL duk_ret_t duk_bi_global_object_print_helper(duk_context *ctx) {
  25130. DUK_UNREF(ctx);
  25131. return DUK_RET_UNSUPPORTED_ERROR;
  25132. }
  25133. #endif /* print provider */
  25134. /*
  25135. * CommonJS require() and modules support
  25136. */
  25137. #if defined(DUK_USE_COMMONJS_MODULES)
  25138. DUK_LOCAL void duk__bi_global_resolve_module_id(duk_context *ctx, const char *req_id, const char *mod_id) {
  25139. duk_hthread *thr = (duk_hthread *) ctx;
  25140. duk_size_t mod_id_len;
  25141. duk_size_t req_id_len;
  25142. duk_uint8_t buf_in[DUK_BI_COMMONJS_MODULE_ID_LIMIT];
  25143. duk_uint8_t buf_out[DUK_BI_COMMONJS_MODULE_ID_LIMIT];
  25144. duk_uint8_t *p;
  25145. duk_uint8_t *q;
  25146. DUK_ASSERT(req_id != NULL);
  25147. /* mod_id may be NULL */
  25148. DUK_ASSERT(sizeof(buf_out) >= sizeof(buf_in)); /* bound checking requires this */
  25149. /*
  25150. * A few notes on the algorithm:
  25151. *
  25152. * - Terms are not allowed to begin with a period unless the term
  25153. * is either '.' or '..'. This simplifies implementation (and
  25154. * is within CommonJS modules specification).
  25155. *
  25156. * - There are few output bound checks here. This is on purpose:
  25157. * we check the input length and rely on the output never being
  25158. * longer than the input, so we cannot run out of output space.
  25159. *
  25160. * - Non-ASCII characters are processed as individual bytes and
  25161. * need no special treatment. However, U+0000 terminates the
  25162. * algorithm; this is not an issue because U+0000 is not a
  25163. * desirable term character anyway.
  25164. */
  25165. /*
  25166. * Set up the resolution input which is the requested ID directly
  25167. * (if absolute or no current module path) or with current module
  25168. * ID prepended (if relative and current module path exists).
  25169. *
  25170. * Suppose current module is 'foo/bar' and relative path is './quux'.
  25171. * The 'bar' component must be replaced so the initial input here is
  25172. * 'foo/bar/.././quux'.
  25173. */
  25174. req_id_len = DUK_STRLEN(req_id);
  25175. if (mod_id != NULL && req_id[0] == '.') {
  25176. mod_id_len = DUK_STRLEN(mod_id);
  25177. if (mod_id_len + 4 + req_id_len + 1 >= sizeof(buf_in)) {
  25178. DUK_DD(DUK_DDPRINT("resolve error: current and requested module ID don't fit into resolve input buffer"));
  25179. goto resolve_error;
  25180. }
  25181. (void) DUK_SNPRINTF((char *) buf_in, sizeof(buf_in), "%s/../%s", (const char *) mod_id, (const char *) req_id);
  25182. } else {
  25183. if (req_id_len + 1 >= sizeof(buf_in)) {
  25184. DUK_DD(DUK_DDPRINT("resolve error: requested module ID doesn't fit into resolve input buffer"));
  25185. goto resolve_error;
  25186. }
  25187. (void) DUK_SNPRINTF((char *) buf_in, sizeof(buf_in), "%s", (const char *) req_id);
  25188. }
  25189. buf_in[sizeof(buf_in) - 1] = (duk_uint8_t) 0;
  25190. DUK_DDD(DUK_DDDPRINT("input module id: '%s'", (const char *) buf_in));
  25191. /*
  25192. * Resolution loop. At the top of the loop we're expecting a valid
  25193. * term: '.', '..', or a non-empty identifier not starting with a period.
  25194. */
  25195. p = buf_in;
  25196. q = buf_out;
  25197. for (;;) {
  25198. duk_uint_fast8_t c;
  25199. /* Here 'p' always points to the start of a term. */
  25200. DUK_DDD(DUK_DDDPRINT("resolve loop top: p -> '%s', q=%p, buf_out=%p",
  25201. (const char *) p, (void *) q, (void *) buf_out));
  25202. c = *p++;
  25203. if (DUK_UNLIKELY(c == 0)) {
  25204. DUK_DD(DUK_DDPRINT("resolve error: requested ID must end with a non-empty term"));
  25205. goto resolve_error;
  25206. } else if (DUK_UNLIKELY(c == '.')) {
  25207. c = *p++;
  25208. if (c == '/') {
  25209. /* Term was '.' and is eaten entirely (including dup slashes). */
  25210. goto eat_dup_slashes;
  25211. }
  25212. if (c == '.' && *p == '/') {
  25213. /* Term was '..', backtrack resolved name by one component.
  25214. * q[-1] = previous slash (or beyond start of buffer)
  25215. * q[-2] = last char of previous component (or beyond start of buffer)
  25216. */
  25217. p++; /* eat (first) input slash */
  25218. DUK_ASSERT(q >= buf_out);
  25219. if (q == buf_out) {
  25220. DUK_DD(DUK_DDPRINT("resolve error: term was '..' but nothing to backtrack"));
  25221. goto resolve_error;
  25222. }
  25223. DUK_ASSERT(*(q - 1) == '/');
  25224. q--; /* backtrack to last output slash */
  25225. for (;;) {
  25226. /* Backtrack to previous slash or start of buffer. */
  25227. DUK_ASSERT(q >= buf_out);
  25228. if (q == buf_out) {
  25229. break;
  25230. }
  25231. if (*(q - 1) == '/') {
  25232. break;
  25233. }
  25234. q--;
  25235. }
  25236. goto eat_dup_slashes;
  25237. }
  25238. DUK_DD(DUK_DDPRINT("resolve error: term begins with '.' but is not '.' or '..' (not allowed now)"));
  25239. goto resolve_error;
  25240. } else if (DUK_UNLIKELY(c == '/')) {
  25241. /* e.g. require('/foo'), empty terms not allowed */
  25242. DUK_DD(DUK_DDPRINT("resolve error: empty term (not allowed now)"));
  25243. goto resolve_error;
  25244. } else {
  25245. for (;;) {
  25246. /* Copy term name until end or '/'. */
  25247. *q++ = c;
  25248. c = *p++;
  25249. if (DUK_UNLIKELY(c == 0)) {
  25250. goto loop_done;
  25251. } else if (DUK_UNLIKELY(c == '/')) {
  25252. *q++ = '/';
  25253. break;
  25254. } else {
  25255. /* write on next loop */
  25256. }
  25257. }
  25258. }
  25259. eat_dup_slashes:
  25260. for (;;) {
  25261. /* eat dup slashes */
  25262. c = *p;
  25263. if (DUK_LIKELY(c != '/')) {
  25264. break;
  25265. }
  25266. p++;
  25267. }
  25268. }
  25269. loop_done:
  25270. duk_push_lstring(ctx, (const char *) buf_out, (size_t) (q - buf_out));
  25271. return;
  25272. resolve_error:
  25273. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "cannot resolve module id: %s", (const char *) req_id);
  25274. }
  25275. #endif /* DUK_USE_COMMONJS_MODULES */
  25276. #if defined(DUK_USE_COMMONJS_MODULES)
  25277. DUK_INTERNAL duk_ret_t duk_bi_global_object_require(duk_context *ctx) {
  25278. const char *str_req_id; /* requested identifier */
  25279. const char *str_mod_id; /* require.id of current module */
  25280. duk_int_t pcall_rc;
  25281. /* NOTE: we try to minimize code size by avoiding unnecessary pops,
  25282. * so the stack looks a bit cluttered in this function. DUK_ASSERT_TOP()
  25283. * assertions are used to ensure stack configuration is correct at each
  25284. * step.
  25285. */
  25286. /*
  25287. * Resolve module identifier into canonical absolute form.
  25288. */
  25289. str_req_id = duk_require_string(ctx, 0);
  25290. duk_push_current_function(ctx);
  25291. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_ID);
  25292. str_mod_id = duk_get_string(ctx, 2); /* ignore non-strings */
  25293. DUK_DDD(DUK_DDDPRINT("resolve module id: requested=%!T, currentmodule=%!T",
  25294. (duk_tval *) duk_get_tval(ctx, 0),
  25295. (duk_tval *) duk_get_tval(ctx, 2)));
  25296. duk__bi_global_resolve_module_id(ctx, str_req_id, str_mod_id);
  25297. str_req_id = NULL;
  25298. str_mod_id = NULL;
  25299. DUK_DDD(DUK_DDDPRINT("resolved module id: requested=%!T, currentmodule=%!T, result=%!T",
  25300. (duk_tval *) duk_get_tval(ctx, 0),
  25301. (duk_tval *) duk_get_tval(ctx, 2),
  25302. (duk_tval *) duk_get_tval(ctx, 3)));
  25303. /* [ requested_id require require.id resolved_id ] */
  25304. DUK_ASSERT_TOP(ctx, 4);
  25305. /*
  25306. * Cached module check.
  25307. *
  25308. * If module has been loaded or its loading has already begun without
  25309. * finishing, return the same cached value ('exports'). The value is
  25310. * registered when module load starts so that circular references can
  25311. * be supported to some extent.
  25312. */
  25313. /* [ requested_id require require.id resolved_id ] */
  25314. DUK_ASSERT_TOP(ctx, 4);
  25315. duk_push_hobject_bidx(ctx, DUK_BIDX_DUKTAPE);
  25316. duk_get_prop_stridx(ctx, 4, DUK_STRIDX_MOD_LOADED); /* Duktape.modLoaded */
  25317. (void) duk_require_hobject(ctx, 5);
  25318. /* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded ] */
  25319. DUK_ASSERT_TOP(ctx, 6);
  25320. duk_dup(ctx, 3);
  25321. if (duk_get_prop(ctx, 5)) {
  25322. /* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded Duktape.modLoaded[id] ] */
  25323. DUK_DD(DUK_DDPRINT("module already loaded: %!T",
  25324. (duk_tval *) duk_get_tval(ctx, 3)));
  25325. return 1;
  25326. }
  25327. /* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined ] */
  25328. DUK_ASSERT_TOP(ctx, 7);
  25329. /*
  25330. * Module not loaded (and loading not started previously).
  25331. *
  25332. * Create a new require() function with 'id' set to resolved ID
  25333. * of module being loaded. Also create 'exports' and 'module'
  25334. * tables but don't register exports to the loaded table yet.
  25335. * We don't want to do that unless the user module search callbacks
  25336. * succeeds in finding the module.
  25337. */
  25338. DUK_DD(DUK_DDPRINT("module not yet loaded: %!T",
  25339. (duk_tval *) duk_get_tval(ctx, 3)));
  25340. /* Fresh require: require.id is left configurable (but not writable)
  25341. * so that is not easy to accidentally tweak it, but it can still be
  25342. * done with Object.defineProperty().
  25343. *
  25344. * XXX: require.id could also be just made non-configurable, as there
  25345. * is no practical reason to touch it.
  25346. */
  25347. duk_push_c_function(ctx, duk_bi_global_object_require, 1 /*nargs*/);
  25348. duk_dup(ctx, 3);
  25349. duk_xdef_prop_stridx(ctx, 7, DUK_STRIDX_ID, DUK_PROPDESC_FLAGS_C); /* a fresh require() with require.id = resolved target module id */
  25350. /* Module table:
  25351. * - module.exports: initial exports table (may be replaced by user)
  25352. * - module.id is non-writable and non-configurable, as the CommonJS
  25353. * spec suggests this if possible.
  25354. */
  25355. duk_push_object(ctx); /* exports */
  25356. duk_push_object(ctx); /* module */
  25357. duk_dup(ctx, -2);
  25358. duk_xdef_prop_stridx(ctx, 9, DUK_STRIDX_EXPORTS, DUK_PROPDESC_FLAGS_WC); /* module.exports = exports */
  25359. duk_dup(ctx, 3); /* resolved id: require(id) must return this same module */
  25360. duk_xdef_prop_stridx(ctx, 9, DUK_STRIDX_ID, DUK_PROPDESC_FLAGS_NONE); /* module.id = resolved_id */
  25361. /* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined fresh_require exports module ] */
  25362. DUK_ASSERT_TOP(ctx, 10);
  25363. /*
  25364. * Call user provided module search function and build the wrapped
  25365. * module source code (if necessary). The module search function
  25366. * can be used to implement pure Ecmacsript, pure C, and mixed
  25367. * Ecmascript/C modules.
  25368. *
  25369. * The module search function can operate on the exports table directly
  25370. * (e.g. DLL code can register values to it). It can also return a
  25371. * string which is interpreted as module source code (if a non-string
  25372. * is returned the module is assumed to be a pure C one). If a module
  25373. * cannot be found, an error must be thrown by the user callback.
  25374. *
  25375. * NOTE: the current arrangement allows C modules to be implemented
  25376. * but since the exports table is registered to Duktape.modLoaded only
  25377. * after the search function returns, circular requires / partially
  25378. * loaded modules don't work for C modules. This is rarely an issue,
  25379. * as C modules usually simply expose a set of helper functions.
  25380. */
  25381. duk_push_string(ctx, "(function(require,exports,module){");
  25382. /* Duktape.modSearch(resolved_id, fresh_require, exports, module). */
  25383. duk_get_prop_stridx(ctx, 4, DUK_STRIDX_MOD_SEARCH); /* Duktape.modSearch */
  25384. duk_dup(ctx, 3);
  25385. duk_dup(ctx, 7);
  25386. duk_dup(ctx, 8);
  25387. duk_dup(ctx, 9); /* [ ... Duktape.modSearch resolved_id fresh_require exports module ] */
  25388. duk_call(ctx, 4 /*nargs*/); /* -> [ ... source ] */
  25389. DUK_ASSERT_TOP(ctx, 12);
  25390. /* If user callback did not return source code, module loading
  25391. * is finished (user callback initialized exports table directly).
  25392. */
  25393. if (!duk_is_string(ctx, 11)) {
  25394. /* User callback did not return source code, so module loading
  25395. * is finished: just update modLoaded with final module.exports
  25396. * and we're done.
  25397. */
  25398. goto finish;
  25399. }
  25400. /* Because user callback did not throw an error, remember (current)
  25401. * exports table in case the module self-references, or loads another
  25402. * module that references us etc. After the module function has
  25403. * returned, we'll need to update this value to support module.exports.
  25404. */
  25405. duk_dup(ctx, 3);
  25406. duk_dup(ctx, 8);
  25407. duk_put_prop(ctx, 5); /* Duktape.modLoaded[resolved_id] = exports */
  25408. /* Finish the wrapped module source. Force resolved module ID as the
  25409. * fileName so it gets set for functions defined within a module. This
  25410. * also ensures loggers created within the module get the module ID as
  25411. * their default logger name.
  25412. */
  25413. duk_push_string(ctx, "})");
  25414. duk_concat(ctx, 3);
  25415. duk_dup(ctx, 3); /* resolved module ID for fileName */
  25416. duk_eval_raw(ctx, NULL, 0, DUK_COMPILE_EVAL);
  25417. /* XXX: The module wrapper function is currently anonymous and is shown
  25418. * in stack traces. It would be nice to force it to match the module
  25419. * name (perhaps just the cleaned up last term). At the moment 'name'
  25420. * is write protected so we can't change it directly. Note that we must
  25421. * not introduce an actual name binding into the function scope (which
  25422. * is usually the case with a named function) because it would affect
  25423. * the scope seen by the module and shadow accesses to globals of the
  25424. * same name.
  25425. */
  25426. /*
  25427. * Call the wrapped module function.
  25428. *
  25429. * Use a protected call so that we can update Duktape.modLoaded[resolved_id]
  25430. * even if the module throws an error.
  25431. */
  25432. /* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined fresh_require exports module mod_func ] */
  25433. DUK_ASSERT_TOP(ctx, 11);
  25434. duk_dup(ctx, 8); /* exports (this binding) */
  25435. duk_dup(ctx, 7); /* fresh require (argument) */
  25436. duk_get_prop_stridx(ctx, 9, DUK_STRIDX_EXPORTS); /* relookup exports from module.exports in case it was changed by modSearch */
  25437. duk_dup(ctx, 9); /* module (argument) */
  25438. /* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined fresh_require exports module mod_func exports fresh_require exports module ] */
  25439. DUK_ASSERT_TOP(ctx, 15);
  25440. pcall_rc = duk_pcall_method(ctx, 3 /*nargs*/);
  25441. if (pcall_rc != DUK_EXEC_SUCCESS) {
  25442. /* Module loading failed. Node.js will forget the module
  25443. * registration so that another require() will try to load
  25444. * the module again. Mimic that behavior.
  25445. */
  25446. duk_dup(ctx, 3);
  25447. duk_del_prop(ctx, 5); /* delete Duktape.modLoaded[resolved_id] */
  25448. duk_throw(ctx); /* rethrow original error */
  25449. }
  25450. /* [ requested_id require require.id resolved_id Duktape Duktape.modLoaded undefined fresh_require exports module result(ignored) ] */
  25451. DUK_ASSERT_TOP(ctx, 11);
  25452. /* Update modLoaded registry with final exports value. */
  25453. finish:
  25454. duk_get_prop_stridx(ctx, 9, DUK_STRIDX_EXPORTS);
  25455. duk_dup(ctx, 3);
  25456. duk_dup(ctx, -2);
  25457. duk_put_prop(ctx, 5); /* Duktape.modLoaded[resolved_id] = module.exports */
  25458. return 1; /* return module.exports */
  25459. }
  25460. #else
  25461. DUK_INTERNAL duk_ret_t duk_bi_global_object_require(duk_context *ctx) {
  25462. DUK_UNREF(ctx);
  25463. return DUK_RET_UNSUPPORTED_ERROR;
  25464. }
  25465. #endif /* DUK_USE_COMMONJS_MODULES */
  25466. #line 1 "duk_bi_json.c"
  25467. /*
  25468. * JSON built-ins.
  25469. *
  25470. * See doc/json.txt.
  25471. *
  25472. * Codepoints are handled as duk_uint_fast32_t to ensure that the full
  25473. * unsigned 32-bit range is supported. This matters to e.g. JX.
  25474. */
  25475. /* include removed: duk_internal.h */
  25476. /*
  25477. * Local defines and forward declarations.
  25478. */
  25479. DUK_LOCAL_DECL void duk__dec_syntax_error(duk_json_dec_ctx *js_ctx);
  25480. DUK_LOCAL_DECL void duk__dec_eat_white(duk_json_dec_ctx *js_ctx);
  25481. DUK_LOCAL_DECL duk_small_int_t duk__dec_peek(duk_json_dec_ctx *js_ctx);
  25482. DUK_LOCAL_DECL duk_small_int_t duk__dec_get(duk_json_dec_ctx *js_ctx);
  25483. DUK_LOCAL_DECL duk_small_int_t duk__dec_get_nonwhite(duk_json_dec_ctx *js_ctx);
  25484. DUK_LOCAL_DECL duk_uint_fast32_t duk__dec_decode_hex_escape(duk_json_dec_ctx *js_ctx, duk_small_uint_t n);
  25485. DUK_LOCAL_DECL void duk__dec_req_stridx(duk_json_dec_ctx *js_ctx, duk_small_uint_t stridx);
  25486. DUK_LOCAL_DECL void duk__dec_string(duk_json_dec_ctx *js_ctx);
  25487. #ifdef DUK_USE_JX
  25488. DUK_LOCAL_DECL void duk__dec_plain_string(duk_json_dec_ctx *js_ctx);
  25489. DUK_LOCAL_DECL void duk__dec_pointer(duk_json_dec_ctx *js_ctx);
  25490. DUK_LOCAL_DECL void duk__dec_buffer(duk_json_dec_ctx *js_ctx);
  25491. #endif
  25492. DUK_LOCAL_DECL void duk__dec_number(duk_json_dec_ctx *js_ctx);
  25493. DUK_LOCAL_DECL void duk__dec_objarr_entry(duk_json_dec_ctx *js_ctx);
  25494. DUK_LOCAL_DECL void duk__dec_objarr_exit(duk_json_dec_ctx *js_ctx);
  25495. DUK_LOCAL_DECL void duk__dec_object(duk_json_dec_ctx *js_ctx);
  25496. DUK_LOCAL_DECL void duk__dec_array(duk_json_dec_ctx *js_ctx);
  25497. DUK_LOCAL_DECL void duk__dec_value(duk_json_dec_ctx *js_ctx);
  25498. DUK_LOCAL_DECL void duk__dec_reviver_walk(duk_json_dec_ctx *js_ctx);
  25499. DUK_LOCAL_DECL void duk__emit_1(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch);
  25500. DUK_LOCAL_DECL void duk__emit_2(duk_json_enc_ctx *js_ctx, duk_uint_fast16_t packed_chars);
  25501. DUK_LOCAL_DECL void duk__emit_esc_auto(duk_json_enc_ctx *js_ctx, duk_uint_fast32_t cp);
  25502. DUK_LOCAL_DECL void duk__emit_xutf8(duk_json_enc_ctx *js_ctx, duk_uint_fast32_t cp);
  25503. DUK_LOCAL_DECL void duk__emit_hstring(duk_json_enc_ctx *js_ctx, duk_hstring *h);
  25504. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  25505. DUK_LOCAL_DECL void duk__emit_cstring(duk_json_enc_ctx *js_ctx, const char *p);
  25506. #endif
  25507. DUK_LOCAL_DECL void duk__emit_stridx(duk_json_enc_ctx *js_ctx, duk_small_uint_t stridx);
  25508. DUK_LOCAL_DECL duk_bool_t duk__enc_key_quotes_needed(duk_hstring *h_key);
  25509. DUK_LOCAL_DECL void duk__enc_quote_string(duk_json_enc_ctx *js_ctx, duk_hstring *h_str);
  25510. 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);
  25511. 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);
  25512. DUK_LOCAL_DECL void duk__enc_object(duk_json_enc_ctx *js_ctx);
  25513. DUK_LOCAL_DECL void duk__enc_array(duk_json_enc_ctx *js_ctx);
  25514. DUK_LOCAL_DECL duk_bool_t duk__enc_value1(duk_json_enc_ctx *js_ctx, duk_idx_t idx_holder);
  25515. DUK_LOCAL_DECL void duk__enc_value2(duk_json_enc_ctx *js_ctx);
  25516. DUK_LOCAL_DECL duk_bool_t duk__enc_allow_into_proplist(duk_tval *tv);
  25517. /*
  25518. * Parsing implementation.
  25519. *
  25520. * JSON lexer is now separate from duk_lexer.c because there are numerous
  25521. * small differences making it difficult to share the lexer.
  25522. *
  25523. * The parser here works with raw bytes directly; this works because all
  25524. * JSON delimiters are ASCII characters. Invalid xUTF-8 encoded values
  25525. * inside strings will be passed on without normalization; this is not a
  25526. * compliance concern because compliant inputs will always be valid
  25527. * CESU-8 encodings.
  25528. */
  25529. DUK_LOCAL void duk__dec_syntax_error(duk_json_dec_ctx *js_ctx) {
  25530. /* Shared handler to minimize parser size. Cause will be
  25531. * hidden, unfortunately, but we'll have an offset which
  25532. * is often quite enough.
  25533. */
  25534. DUK_ERROR(js_ctx->thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_FMT_INVALID_JSON,
  25535. (long) (js_ctx->p - js_ctx->p_start));
  25536. }
  25537. DUK_LOCAL void duk__dec_eat_white(duk_json_dec_ctx *js_ctx) {
  25538. duk_small_uint_t t;
  25539. for (;;) {
  25540. if (js_ctx->p >= js_ctx->p_end) {
  25541. break;
  25542. }
  25543. t = (*js_ctx->p);
  25544. if (!(t == 0x20 || t == 0x0a || t == 0x0d || t == 0x09)) {
  25545. break;
  25546. }
  25547. js_ctx->p++;
  25548. }
  25549. }
  25550. DUK_LOCAL duk_small_int_t duk__dec_peek(duk_json_dec_ctx *js_ctx) {
  25551. if (js_ctx->p >= js_ctx->p_end) {
  25552. return -1;
  25553. } else {
  25554. return (duk_small_int_t) (*js_ctx->p);
  25555. }
  25556. }
  25557. DUK_LOCAL duk_small_int_t duk__dec_get(duk_json_dec_ctx *js_ctx) {
  25558. /* Multiple EOFs will now be supplied to the caller. This could also be
  25559. * changed so that reading the second EOF would cause an error automatically.
  25560. */
  25561. if (js_ctx->p >= js_ctx->p_end) {
  25562. return -1;
  25563. } else {
  25564. return (duk_small_int_t) (*js_ctx->p++);
  25565. }
  25566. }
  25567. DUK_LOCAL duk_small_int_t duk__dec_get_nonwhite(duk_json_dec_ctx *js_ctx) {
  25568. duk__dec_eat_white(js_ctx);
  25569. return duk__dec_get(js_ctx);
  25570. }
  25571. /* For JX, expressing the whole unsigned 32-bit range matters. */
  25572. DUK_LOCAL duk_uint_fast32_t duk__dec_decode_hex_escape(duk_json_dec_ctx *js_ctx, duk_small_uint_t n) {
  25573. duk_small_uint_t i;
  25574. duk_uint_fast32_t res = 0;
  25575. duk_small_int_t x;
  25576. for (i = 0; i < n; i++) {
  25577. /* XXX: share helper from lexer; duk_lexer.c / hexval(). */
  25578. x = duk__dec_get(js_ctx);
  25579. DUK_ASSERT((x >= 0 && x <= 0xff) || (x == -1));
  25580. DUK_DDD(DUK_DDDPRINT("decode_hex_escape: i=%ld, n=%ld, res=%ld, x=%ld",
  25581. (long) i, (long) n, (long) res, (long) x));
  25582. /* x == -1 will map to 0xff, dectab returns -1 which causes syntax_error */
  25583. x = duk_hex_dectab[x & 0xff];
  25584. if (DUK_LIKELY(x >= 0)) {
  25585. res = (res * 16) + x;
  25586. } else {
  25587. /* catches EOF and invalid digits */
  25588. goto syntax_error;
  25589. }
  25590. }
  25591. DUK_DDD(DUK_DDDPRINT("final hex decoded value: %ld", (long) res));
  25592. return res;
  25593. syntax_error:
  25594. duk__dec_syntax_error(js_ctx);
  25595. DUK_UNREACHABLE();
  25596. return 0;
  25597. }
  25598. DUK_LOCAL void duk__dec_req_stridx(duk_json_dec_ctx *js_ctx, duk_small_uint_t stridx) {
  25599. duk_hstring *h;
  25600. duk_uint8_t *p;
  25601. duk_uint8_t *p_end;
  25602. duk_small_int_t x;
  25603. /* First character has already been eaten and checked by the caller. */
  25604. DUK_ASSERT_DISABLE(stridx >= 0); /* unsigned */
  25605. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  25606. h = DUK_HTHREAD_GET_STRING(js_ctx->thr, stridx);
  25607. DUK_ASSERT(h != NULL);
  25608. p = (duk_uint8_t *) DUK_HSTRING_GET_DATA(h);
  25609. p_end = ((duk_uint8_t *) DUK_HSTRING_GET_DATA(h)) +
  25610. DUK_HSTRING_GET_BYTELEN(h);
  25611. DUK_ASSERT(*(js_ctx->p - 1) == *p); /* first character has been matched */
  25612. p++; /* first char */
  25613. while (p < p_end) {
  25614. x = duk__dec_get(js_ctx);
  25615. if ((duk_small_int_t) (*p) != x) {
  25616. /* catches EOF */
  25617. goto syntax_error;
  25618. }
  25619. p++;
  25620. }
  25621. return;
  25622. syntax_error:
  25623. duk__dec_syntax_error(js_ctx);
  25624. DUK_UNREACHABLE();
  25625. }
  25626. DUK_LOCAL void duk__dec_string(duk_json_dec_ctx *js_ctx) {
  25627. duk_hthread *thr = js_ctx->thr;
  25628. duk_context *ctx = (duk_context *) thr;
  25629. duk_hbuffer_dynamic *h_buf;
  25630. duk_small_int_t x;
  25631. duk_uint_fast32_t cp;
  25632. /* '"' was eaten by caller */
  25633. /* Note that we currently parse -bytes-, not codepoints.
  25634. * All non-ASCII extended UTF-8 will encode to bytes >= 0x80,
  25635. * so they'll simply pass through (valid UTF-8 or not).
  25636. */
  25637. duk_push_dynamic_buffer(ctx, 0);
  25638. h_buf = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, -1);
  25639. DUK_ASSERT(h_buf != NULL);
  25640. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(h_buf));
  25641. for (;;) {
  25642. x = duk__dec_get(js_ctx);
  25643. if (x == DUK_ASC_DOUBLEQUOTE) {
  25644. break;
  25645. } else if (x == DUK_ASC_BACKSLASH) {
  25646. /* EOF (-1) will be cast to an unsigned value first
  25647. * and then re-cast for the switch. In any case, it
  25648. * will match the default case (syntax error).
  25649. */
  25650. cp = (duk_uint_fast32_t) duk__dec_get(js_ctx);
  25651. switch ((int) cp) {
  25652. case DUK_ASC_BACKSLASH: break;
  25653. case DUK_ASC_DOUBLEQUOTE: break;
  25654. case DUK_ASC_SLASH: break;
  25655. case DUK_ASC_LC_T: cp = 0x09; break;
  25656. case DUK_ASC_LC_N: cp = 0x0a; break;
  25657. case DUK_ASC_LC_R: cp = 0x0d; break;
  25658. case DUK_ASC_LC_F: cp = 0x0c; break;
  25659. case DUK_ASC_LC_B: cp = 0x08; break;
  25660. case DUK_ASC_LC_U: {
  25661. cp = duk__dec_decode_hex_escape(js_ctx, 4);
  25662. break;
  25663. }
  25664. #ifdef DUK_USE_JX
  25665. case DUK_ASC_UC_U: {
  25666. if (js_ctx->flag_ext_custom) {
  25667. cp = duk__dec_decode_hex_escape(js_ctx, 8);
  25668. } else {
  25669. goto syntax_error;
  25670. }
  25671. break;
  25672. }
  25673. case DUK_ASC_LC_X: {
  25674. if (js_ctx->flag_ext_custom) {
  25675. cp = duk__dec_decode_hex_escape(js_ctx, 2);
  25676. } else {
  25677. goto syntax_error;
  25678. }
  25679. break;
  25680. }
  25681. #endif /* DUK_USE_JX */
  25682. default:
  25683. /* catches EOF (-1) */
  25684. goto syntax_error;
  25685. }
  25686. duk_hbuffer_append_xutf8(thr, h_buf, (duk_uint32_t) cp);
  25687. } else if (x < 0x20) {
  25688. /* catches EOF (-1) */
  25689. goto syntax_error;
  25690. } else {
  25691. duk_hbuffer_append_byte(thr, h_buf, (duk_uint8_t) x);
  25692. }
  25693. }
  25694. duk_to_string(ctx, -1);
  25695. /* [ ... str ] */
  25696. return;
  25697. syntax_error:
  25698. duk__dec_syntax_error(js_ctx);
  25699. DUK_UNREACHABLE();
  25700. }
  25701. #ifdef DUK_USE_JX
  25702. /* Decode a plain string consisting entirely of identifier characters.
  25703. * Used to parse plain keys (e.g. "foo: 123").
  25704. */
  25705. DUK_LOCAL void duk__dec_plain_string(duk_json_dec_ctx *js_ctx) {
  25706. duk_hthread *thr = js_ctx->thr;
  25707. duk_context *ctx = (duk_context *) thr;
  25708. const duk_uint8_t *p;
  25709. duk_small_int_t x;
  25710. /* Caller has already eaten the first char so backtrack one byte. */
  25711. js_ctx->p--; /* safe */
  25712. p = js_ctx->p;
  25713. /* Here again we parse bytes, and non-ASCII UTF-8 will cause end of
  25714. * parsing (which is correct except if there are non-shortest encodings).
  25715. * There is also no need to check explicitly for end of input buffer as
  25716. * the input is NUL padded and NUL will exit the parsing loop.
  25717. *
  25718. * Because no unescaping takes place, we can just scan to the end of the
  25719. * plain string and intern from the input buffer.
  25720. */
  25721. for (;;) {
  25722. x = *p;
  25723. /* There is no need to check the first character specially here
  25724. * (i.e. reject digits): the caller only accepts valid initial
  25725. * characters and won't call us if the first character is a digit.
  25726. * This also ensures that the plain string won't be empty.
  25727. */
  25728. if (!duk_unicode_is_identifier_part((duk_codepoint_t) x)) {
  25729. break;
  25730. }
  25731. p++;
  25732. }
  25733. duk_push_lstring(ctx, (const char *) js_ctx->p, (duk_size_t) (p - js_ctx->p));
  25734. js_ctx->p = p;
  25735. /* [ ... str ] */
  25736. }
  25737. #endif /* DUK_USE_JX */
  25738. #ifdef DUK_USE_JX
  25739. DUK_LOCAL void duk__dec_pointer(duk_json_dec_ctx *js_ctx) {
  25740. duk_hthread *thr = js_ctx->thr;
  25741. duk_context *ctx = (duk_context *) thr;
  25742. const duk_uint8_t *p;
  25743. duk_small_int_t x;
  25744. void *voidptr;
  25745. /* Caller has already eaten the first character ('(') which we don't need. */
  25746. p = js_ctx->p;
  25747. for (;;) {
  25748. x = *p;
  25749. /* Assume that the native representation never contains a closing
  25750. * parenthesis.
  25751. */
  25752. if (x == DUK_ASC_RPAREN) {
  25753. break;
  25754. } else if (x <= 0) {
  25755. /* NUL term or -1 (EOF), NUL check would suffice */
  25756. goto syntax_error;
  25757. }
  25758. p++;
  25759. }
  25760. /* There is no need to NUL delimit the sscanf() call: trailing garbage is
  25761. * ignored and there is always a NUL terminator which will force an error
  25762. * if no error is encountered before it. It's possible that the scan
  25763. * would scan further than between [js_ctx->p,p[ though and we'd advance
  25764. * by less than the scanned value.
  25765. *
  25766. * Because pointers are platform specific, a failure to scan a pointer
  25767. * results in a null pointer which is a better placeholder than a missing
  25768. * value or an error.
  25769. */
  25770. voidptr = NULL;
  25771. (void) DUK_SSCANF((const char *) js_ctx->p, DUK_STR_FMT_PTR, &voidptr);
  25772. duk_push_pointer(ctx, voidptr);
  25773. js_ctx->p = p + 1; /* skip ')' */
  25774. /* [ ... ptr ] */
  25775. return;
  25776. syntax_error:
  25777. duk__dec_syntax_error(js_ctx);
  25778. DUK_UNREACHABLE();
  25779. }
  25780. #endif /* DUK_USE_JX */
  25781. #ifdef DUK_USE_JX
  25782. DUK_LOCAL void duk__dec_buffer(duk_json_dec_ctx *js_ctx) {
  25783. duk_hthread *thr = js_ctx->thr;
  25784. duk_context *ctx = (duk_context *) thr;
  25785. const duk_uint8_t *p;
  25786. duk_small_int_t x;
  25787. /* Caller has already eaten the first character ('|') which we don't need. */
  25788. p = js_ctx->p;
  25789. for (;;) {
  25790. x = *p;
  25791. /* This loop intentionally does not ensure characters are valid
  25792. * ([0-9a-fA-F]) because the hex decode call below will do that.
  25793. */
  25794. if (x == DUK_ASC_PIPE) {
  25795. break;
  25796. } else if (x <= 0) {
  25797. /* NUL term or -1 (EOF), NUL check would suffice */
  25798. goto syntax_error;
  25799. }
  25800. p++;
  25801. }
  25802. duk_push_lstring(ctx, (const char *) js_ctx->p, (duk_size_t) (p - js_ctx->p));
  25803. duk_hex_decode(ctx, -1);
  25804. js_ctx->p = p + 1; /* skip '|' */
  25805. /* [ ... buf ] */
  25806. return;
  25807. syntax_error:
  25808. duk__dec_syntax_error(js_ctx);
  25809. DUK_UNREACHABLE();
  25810. }
  25811. #endif /* DUK_USE_JX */
  25812. /* Parse a number, other than NaN or +/- Infinity */
  25813. DUK_LOCAL void duk__dec_number(duk_json_dec_ctx *js_ctx) {
  25814. duk_context *ctx = (duk_context *) js_ctx->thr;
  25815. const duk_uint8_t *p_start;
  25816. duk_small_int_t x;
  25817. duk_small_uint_t s2n_flags;
  25818. DUK_DDD(DUK_DDDPRINT("parse_number"));
  25819. /* Caller has already eaten the first character so backtrack one
  25820. * byte. This is correct because the first character is either
  25821. * '-' or a digit (i.e. an ASCII character).
  25822. */
  25823. js_ctx->p--; /* safe */
  25824. p_start = js_ctx->p;
  25825. /* First pass parse is very lenient (e.g. allows '1.2.3') and extracts a
  25826. * string for strict number parsing.
  25827. */
  25828. for (;;) {
  25829. x = duk__dec_peek(js_ctx);
  25830. DUK_DDD(DUK_DDDPRINT("parse_number: p_start=%p, p=%p, p_end=%p, x=%ld",
  25831. (void *) p_start, (void *) js_ctx->p,
  25832. (void *) js_ctx->p_end, (long) x));
  25833. if (!((x >= DUK_ASC_0 && x <= DUK_ASC_9) ||
  25834. (x == DUK_ASC_PERIOD || x == DUK_ASC_LC_E ||
  25835. x == DUK_ASC_UC_E || x == DUK_ASC_MINUS || x == DUK_ASC_PLUS))) {
  25836. /* Plus sign must be accepted for positive exponents
  25837. * (e.g. '1.5e+2').
  25838. */
  25839. break;
  25840. }
  25841. js_ctx->p++; /* safe, because matched char */
  25842. }
  25843. DUK_ASSERT(js_ctx->p > p_start);
  25844. duk_push_lstring(ctx, (const char *) p_start, (duk_size_t) (js_ctx->p - p_start));
  25845. s2n_flags = DUK_S2N_FLAG_ALLOW_EXP |
  25846. DUK_S2N_FLAG_ALLOW_MINUS | /* but don't allow leading plus */
  25847. DUK_S2N_FLAG_ALLOW_FRAC;
  25848. DUK_DDD(DUK_DDDPRINT("parse_number: string before parsing: %!T",
  25849. (duk_tval *) duk_get_tval(ctx, -1)));
  25850. duk_numconv_parse(ctx, 10 /*radix*/, s2n_flags);
  25851. if (duk_is_nan(ctx, -1)) {
  25852. duk__dec_syntax_error(js_ctx);
  25853. }
  25854. DUK_ASSERT(duk_is_number(ctx, -1));
  25855. DUK_DDD(DUK_DDDPRINT("parse_number: final number: %!T",
  25856. (duk_tval *) duk_get_tval(ctx, -1)));
  25857. /* [ ... num ] */
  25858. }
  25859. DUK_LOCAL void duk__dec_objarr_entry(duk_json_dec_ctx *js_ctx) {
  25860. duk_context *ctx = (duk_context *) js_ctx->thr;
  25861. duk_require_stack(ctx, DUK_JSON_DEC_REQSTACK);
  25862. /* c recursion check */
  25863. DUK_ASSERT(js_ctx->recursion_depth >= 0);
  25864. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  25865. if (js_ctx->recursion_depth >= js_ctx->recursion_limit) {
  25866. DUK_ERROR((duk_hthread *) ctx, DUK_ERR_RANGE_ERROR, DUK_STR_JSONDEC_RECLIMIT);
  25867. }
  25868. js_ctx->recursion_depth++;
  25869. }
  25870. DUK_LOCAL void duk__dec_objarr_exit(duk_json_dec_ctx *js_ctx) {
  25871. /* c recursion check */
  25872. DUK_ASSERT(js_ctx->recursion_depth > 0);
  25873. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  25874. js_ctx->recursion_depth--;
  25875. }
  25876. DUK_LOCAL void duk__dec_object(duk_json_dec_ctx *js_ctx) {
  25877. duk_context *ctx = (duk_context *) js_ctx->thr;
  25878. duk_int_t key_count; /* XXX: a "first" flag would suffice */
  25879. duk_small_int_t x;
  25880. DUK_DDD(DUK_DDDPRINT("parse_object"));
  25881. duk__dec_objarr_entry(js_ctx);
  25882. duk_push_object(ctx);
  25883. /* Initial '{' has been checked and eaten by caller. */
  25884. key_count = 0;
  25885. for (;;) {
  25886. x = duk__dec_get_nonwhite(js_ctx);
  25887. DUK_DDD(DUK_DDDPRINT("parse_object: obj=%!T, x=%ld, key_count=%ld",
  25888. (duk_tval *) duk_get_tval(ctx, -1),
  25889. (long) x, (long) key_count));
  25890. /* handle comma and closing brace */
  25891. if (x == DUK_ASC_COMMA && key_count > 0) {
  25892. /* accept comma, expect new value */
  25893. x = duk__dec_get_nonwhite(js_ctx);
  25894. } else if (x == DUK_ASC_RCURLY) {
  25895. /* eat closing brace */
  25896. break;
  25897. } else if (key_count == 0) {
  25898. /* accept anything, expect first value (EOF will be
  25899. * caught by key parsing below.
  25900. */
  25901. ;
  25902. } else {
  25903. /* catches EOF (and initial comma) */
  25904. goto syntax_error;
  25905. }
  25906. /* parse key and value */
  25907. if (x == DUK_ASC_DOUBLEQUOTE) {
  25908. duk__dec_string(js_ctx);
  25909. #ifdef DUK_USE_JX
  25910. } else if (js_ctx->flag_ext_custom &&
  25911. duk_unicode_is_identifier_start((duk_codepoint_t) x)) {
  25912. duk__dec_plain_string(js_ctx);
  25913. #endif
  25914. } else {
  25915. goto syntax_error;
  25916. }
  25917. /* [ ... obj key ] */
  25918. x = duk__dec_get_nonwhite(js_ctx);
  25919. if (x != DUK_ASC_COLON) {
  25920. goto syntax_error;
  25921. }
  25922. duk__dec_value(js_ctx);
  25923. /* [ ... obj key val ] */
  25924. duk_xdef_prop_wec(ctx, -3);
  25925. /* [ ... obj ] */
  25926. key_count++;
  25927. }
  25928. /* [ ... obj ] */
  25929. DUK_DDD(DUK_DDDPRINT("parse_object: final object is %!T",
  25930. (duk_tval *) duk_get_tval(ctx, -1)));
  25931. duk__dec_objarr_exit(js_ctx);
  25932. return;
  25933. syntax_error:
  25934. duk__dec_syntax_error(js_ctx);
  25935. DUK_UNREACHABLE();
  25936. }
  25937. DUK_LOCAL void duk__dec_array(duk_json_dec_ctx *js_ctx) {
  25938. duk_context *ctx = (duk_context *) js_ctx->thr;
  25939. duk_uarridx_t arr_idx;
  25940. duk_small_int_t x;
  25941. DUK_DDD(DUK_DDDPRINT("parse_array"));
  25942. duk__dec_objarr_entry(js_ctx);
  25943. duk_push_array(ctx);
  25944. /* Initial '[' has been checked and eaten by caller. */
  25945. arr_idx = 0;
  25946. for (;;) {
  25947. x = duk__dec_get_nonwhite(js_ctx);
  25948. DUK_DDD(DUK_DDDPRINT("parse_array: arr=%!T, x=%ld, arr_idx=%ld",
  25949. (duk_tval *) duk_get_tval(ctx, -1),
  25950. (long) x, (long) arr_idx));
  25951. /* handle comma and closing bracket */
  25952. if ((x == DUK_ASC_COMMA) && (arr_idx != 0)) {
  25953. /* accept comma, expect new value */
  25954. ;
  25955. } else if (x == DUK_ASC_RBRACKET) {
  25956. /* eat closing bracket */
  25957. break;
  25958. } else if (arr_idx == 0) {
  25959. /* accept anything, expect first value (EOF will be
  25960. * caught by duk__dec_value() below.
  25961. */
  25962. js_ctx->p--; /* backtrack (safe) */
  25963. } else {
  25964. /* catches EOF (and initial comma) */
  25965. goto syntax_error;
  25966. }
  25967. /* parse value */
  25968. duk__dec_value(js_ctx);
  25969. /* [ ... arr val ] */
  25970. duk_xdef_prop_index_wec(ctx, -2, arr_idx);
  25971. arr_idx++;
  25972. }
  25973. /* Must set 'length' explicitly when using duk_xdef_prop_xxx() to
  25974. * set the values.
  25975. */
  25976. duk_set_length(ctx, -1, arr_idx);
  25977. /* [ ... arr ] */
  25978. DUK_DDD(DUK_DDDPRINT("parse_array: final array is %!T",
  25979. (duk_tval *) duk_get_tval(ctx, -1)));
  25980. duk__dec_objarr_exit(js_ctx);
  25981. return;
  25982. syntax_error:
  25983. duk__dec_syntax_error(js_ctx);
  25984. DUK_UNREACHABLE();
  25985. }
  25986. DUK_LOCAL void duk__dec_value(duk_json_dec_ctx *js_ctx) {
  25987. duk_context *ctx = (duk_context *) js_ctx->thr;
  25988. duk_small_int_t x;
  25989. x = duk__dec_get_nonwhite(js_ctx);
  25990. DUK_DDD(DUK_DDDPRINT("parse_value: initial x=%ld", (long) x));
  25991. /* Note: duk__dec_req_stridx() backtracks one char */
  25992. if (x == DUK_ASC_DOUBLEQUOTE) {
  25993. duk__dec_string(js_ctx);
  25994. } else if ((x >= DUK_ASC_0 && x <= DUK_ASC_9) || (x == DUK_ASC_MINUS)) {
  25995. #ifdef DUK_USE_JX
  25996. if (js_ctx->flag_ext_custom && duk__dec_peek(js_ctx) == DUK_ASC_UC_I) {
  25997. duk__dec_req_stridx(js_ctx, DUK_STRIDX_MINUS_INFINITY); /* "-Infinity" */
  25998. duk_push_number(ctx, -DUK_DOUBLE_INFINITY);
  25999. } else {
  26000. #else
  26001. { /* unconditional block */
  26002. #endif
  26003. /* We already ate 'x', so duk__dec_number() will back up one byte. */
  26004. duk__dec_number(js_ctx);
  26005. }
  26006. } else if (x == DUK_ASC_LC_T) {
  26007. duk__dec_req_stridx(js_ctx, DUK_STRIDX_TRUE);
  26008. duk_push_true(ctx);
  26009. } else if (x == DUK_ASC_LC_F) {
  26010. duk__dec_req_stridx(js_ctx, DUK_STRIDX_FALSE);
  26011. duk_push_false(ctx);
  26012. } else if (x == DUK_ASC_LC_N) {
  26013. duk__dec_req_stridx(js_ctx, DUK_STRIDX_LC_NULL);
  26014. duk_push_null(ctx);
  26015. #ifdef DUK_USE_JX
  26016. } else if (js_ctx->flag_ext_custom && x == DUK_ASC_LC_U) {
  26017. duk__dec_req_stridx(js_ctx, DUK_STRIDX_LC_UNDEFINED);
  26018. duk_push_undefined(ctx);
  26019. } else if (js_ctx->flag_ext_custom && x == DUK_ASC_UC_N) {
  26020. duk__dec_req_stridx(js_ctx, DUK_STRIDX_NAN);
  26021. duk_push_nan(ctx);
  26022. } else if (js_ctx->flag_ext_custom && x == DUK_ASC_UC_I) {
  26023. duk__dec_req_stridx(js_ctx, DUK_STRIDX_INFINITY);
  26024. duk_push_number(ctx, DUK_DOUBLE_INFINITY);
  26025. } else if (js_ctx->flag_ext_custom && x == DUK_ASC_LPAREN) {
  26026. duk__dec_pointer(js_ctx);
  26027. } else if (js_ctx->flag_ext_custom && x == DUK_ASC_PIPE) {
  26028. duk__dec_buffer(js_ctx);
  26029. #endif
  26030. } else if (x == DUK_ASC_LCURLY) {
  26031. duk__dec_object(js_ctx);
  26032. } else if (x == DUK_ASC_LBRACKET) {
  26033. duk__dec_array(js_ctx);
  26034. } else {
  26035. /* catches EOF */
  26036. goto syntax_error;
  26037. }
  26038. duk__dec_eat_white(js_ctx);
  26039. /* [ ... val ] */
  26040. return;
  26041. syntax_error:
  26042. duk__dec_syntax_error(js_ctx);
  26043. DUK_UNREACHABLE();
  26044. }
  26045. /* Recursive value reviver, implements the Walk() algorithm. No C recursion
  26046. * check is done here because the initial parsing step will already ensure
  26047. * there is a reasonable limit on C recursion depth and hence object depth.
  26048. */
  26049. DUK_LOCAL void duk__dec_reviver_walk(duk_json_dec_ctx *js_ctx) {
  26050. duk_context *ctx = (duk_context *) js_ctx->thr;
  26051. duk_hobject *h;
  26052. duk_uarridx_t i, arr_len;
  26053. DUK_DDD(DUK_DDDPRINT("walk: top=%ld, holder=%!T, name=%!T",
  26054. (long) duk_get_top(ctx),
  26055. (duk_tval *) duk_get_tval(ctx, -2),
  26056. (duk_tval *) duk_get_tval(ctx, -1)));
  26057. duk_dup_top(ctx);
  26058. duk_get_prop(ctx, -3); /* -> [ ... holder name val ] */
  26059. h = duk_get_hobject(ctx, -1);
  26060. if (h != NULL) {
  26061. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY) {
  26062. arr_len = (duk_uarridx_t) duk_get_length(ctx, -1);
  26063. for (i = 0; i < arr_len; i++) {
  26064. /* [ ... holder name val ] */
  26065. DUK_DDD(DUK_DDDPRINT("walk: array, top=%ld, i=%ld, arr_len=%ld, holder=%!T, name=%!T, val=%!T",
  26066. (long) duk_get_top(ctx), (long) i, (long) arr_len,
  26067. (duk_tval *) duk_get_tval(ctx, -3), (duk_tval *) duk_get_tval(ctx, -2),
  26068. (duk_tval *) duk_get_tval(ctx, -1)));
  26069. /* XXX: push_uint_string / push_u32_string */
  26070. duk_dup_top(ctx);
  26071. duk_push_uint(ctx, (duk_uint_t) i);
  26072. duk_to_string(ctx, -1); /* -> [ ... holder name val val ToString(i) ] */
  26073. duk__dec_reviver_walk(js_ctx); /* -> [ ... holder name val new_elem ] */
  26074. if (duk_is_undefined(ctx, -1)) {
  26075. duk_pop(ctx);
  26076. duk_del_prop_index(ctx, -1, i);
  26077. } else {
  26078. /* XXX: duk_xdef_prop_index_wec() would be more appropriate
  26079. * here but it currently makes some assumptions that might
  26080. * not hold (e.g. that previous property is not an accessor).
  26081. */
  26082. duk_put_prop_index(ctx, -2, i);
  26083. }
  26084. }
  26085. } else {
  26086. /* [ ... holder name val ] */
  26087. duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY /*flags*/);
  26088. while (duk_next(ctx, -1 /*enum_index*/, 0 /*get_value*/)) {
  26089. DUK_DDD(DUK_DDDPRINT("walk: object, top=%ld, holder=%!T, name=%!T, val=%!T, enum=%!iT, obj_key=%!T",
  26090. (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, -5),
  26091. (duk_tval *) duk_get_tval(ctx, -4), (duk_tval *) duk_get_tval(ctx, -3),
  26092. (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1)));
  26093. /* [ ... holder name val enum obj_key ] */
  26094. duk_dup(ctx, -3);
  26095. duk_dup(ctx, -2);
  26096. /* [ ... holder name val enum obj_key val obj_key ] */
  26097. duk__dec_reviver_walk(js_ctx);
  26098. /* [ ... holder name val enum obj_key new_elem ] */
  26099. if (duk_is_undefined(ctx, -1)) {
  26100. duk_pop(ctx);
  26101. duk_del_prop(ctx, -3);
  26102. } else {
  26103. /* XXX: duk_xdef_prop_index_wec() would be more appropriate
  26104. * here but it currently makes some assumptions that might
  26105. * not hold (e.g. that previous property is not an accessor).
  26106. *
  26107. * Using duk_put_prop() works incorrectly with '__proto__'
  26108. * if the own property with that name has been deleted. This
  26109. * does not happen normally, but a clever reviver can trigger
  26110. * that, see complex reviver case in: test-bug-json-parse-__proto__.js.
  26111. */
  26112. duk_put_prop(ctx, -4);
  26113. }
  26114. }
  26115. duk_pop(ctx); /* pop enum */
  26116. }
  26117. }
  26118. /* [ ... holder name val ] */
  26119. duk_dup(ctx, js_ctx->idx_reviver);
  26120. duk_insert(ctx, -4); /* -> [ ... reviver holder name val ] */
  26121. duk_call_method(ctx, 2); /* -> [ ... res ] */
  26122. DUK_DDD(DUK_DDDPRINT("walk: top=%ld, result=%!T",
  26123. (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, -1)));
  26124. }
  26125. /*
  26126. * Stringify implementation.
  26127. */
  26128. #define DUK__EMIT_1(js_ctx,ch) duk__emit_1((js_ctx), (duk_uint_fast8_t) (ch))
  26129. #define DUK__EMIT_2(js_ctx,ch1,ch2) duk__emit_2((js_ctx), (((duk_uint_fast16_t)(ch1)) << 8) + (duk_uint_fast16_t)(ch2))
  26130. #define DUK__EMIT_ESC_AUTO(js_ctx,cp) duk__emit_esc_auto((js_ctx), (cp))
  26131. #define DUK__EMIT_XUTF8(js_ctx,cp) duk__emit_xutf8((js_ctx), (cp))
  26132. #define DUK__EMIT_HSTR(js_ctx,h) duk__emit_hstring((js_ctx), (h))
  26133. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  26134. #define DUK__EMIT_CSTR(js_ctx,p) duk__emit_cstring((js_ctx), (p))
  26135. #endif
  26136. #define DUK__EMIT_STRIDX(js_ctx,i) duk__emit_stridx((js_ctx), (i))
  26137. DUK_LOCAL void duk__emit_1(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch) {
  26138. duk_hbuffer_append_byte(js_ctx->thr, js_ctx->h_buf, (duk_uint8_t) ch);
  26139. }
  26140. DUK_LOCAL void duk__emit_2(duk_json_enc_ctx *js_ctx, duk_uint_fast16_t packed_chars) {
  26141. duk_uint8_t buf[2];
  26142. buf[0] = (duk_uint8_t) (packed_chars >> 8);
  26143. buf[1] = (duk_uint8_t) (packed_chars & 0xff);
  26144. duk_hbuffer_append_bytes(js_ctx->thr, js_ctx->h_buf, (duk_uint8_t *) buf, 2);
  26145. }
  26146. #define DUK__MKESC(nybbles,esc1,esc2) \
  26147. (((duk_uint_fast32_t) (nybbles)) << 16) | \
  26148. (((duk_uint_fast32_t) (esc1)) << 8) | \
  26149. ((duk_uint_fast32_t) (esc2))
  26150. DUK_LOCAL void duk__emit_esc_auto(duk_json_enc_ctx *js_ctx, duk_uint_fast32_t cp) {
  26151. duk_uint8_t buf[2];
  26152. duk_uint_fast32_t tmp;
  26153. duk_small_uint_t dig;
  26154. /* Select appropriate escape format automatically, and set 'tmp' to a
  26155. * value encoding both the escape format character and the nybble count:
  26156. *
  26157. * (nybble_count << 16) | (escape_char1) | (escape_char2)
  26158. */
  26159. #ifdef DUK_USE_JX
  26160. if (DUK_LIKELY(cp < 0x100UL)) {
  26161. if (DUK_UNLIKELY(js_ctx->flag_ext_custom)) {
  26162. tmp = DUK__MKESC(2, DUK_ASC_BACKSLASH, DUK_ASC_LC_X);
  26163. } else {
  26164. tmp = DUK__MKESC(4, DUK_ASC_BACKSLASH, DUK_ASC_LC_U);
  26165. }
  26166. } else
  26167. #endif
  26168. if (DUK_LIKELY(cp < 0x10000UL)) {
  26169. tmp = DUK__MKESC(4, DUK_ASC_BACKSLASH, DUK_ASC_LC_U);
  26170. } else {
  26171. #ifdef DUK_USE_JX
  26172. if (DUK_LIKELY(js_ctx->flag_ext_custom)) {
  26173. tmp = DUK__MKESC(8, DUK_ASC_BACKSLASH, DUK_ASC_UC_U);
  26174. } else
  26175. #endif
  26176. {
  26177. /* In compatible mode and standard JSON mode, output
  26178. * something useful for non-BMP characters. This won't
  26179. * roundtrip but will still be more or less readable and
  26180. * more useful than an error.
  26181. */
  26182. tmp = DUK__MKESC(8, DUK_ASC_UC_U, DUK_ASC_PLUS);
  26183. }
  26184. }
  26185. buf[0] = (duk_uint8_t) ((tmp >> 8) & 0xff);
  26186. buf[1] = (duk_uint8_t) (tmp & 0xff);
  26187. duk_hbuffer_append_bytes(js_ctx->thr, js_ctx->h_buf, buf, 2);
  26188. tmp = tmp >> 16;
  26189. while (tmp > 0) {
  26190. tmp--;
  26191. dig = (duk_small_uint_t) ((cp >> (4 * tmp)) & 0x0f);
  26192. duk_hbuffer_append_byte(js_ctx->thr, js_ctx->h_buf, duk_lc_digits[dig]);
  26193. }
  26194. }
  26195. DUK_LOCAL void duk__emit_xutf8(duk_json_enc_ctx *js_ctx, duk_uint_fast32_t cp) {
  26196. (void) duk_hbuffer_append_xutf8(js_ctx->thr, js_ctx->h_buf, cp);
  26197. }
  26198. DUK_LOCAL void duk__emit_hstring(duk_json_enc_ctx *js_ctx, duk_hstring *h) {
  26199. DUK_ASSERT(h != NULL);
  26200. duk_hbuffer_append_bytes(js_ctx->thr,
  26201. js_ctx->h_buf,
  26202. (duk_uint8_t *) DUK_HSTRING_GET_DATA(h),
  26203. (duk_size_t) DUK_HSTRING_GET_BYTELEN(h));
  26204. }
  26205. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  26206. DUK_LOCAL void duk__emit_cstring(duk_json_enc_ctx *js_ctx, const char *p) {
  26207. DUK_ASSERT(p != NULL);
  26208. (void) duk_hbuffer_append_cstring(js_ctx->thr, js_ctx->h_buf, p);
  26209. }
  26210. #endif
  26211. DUK_LOCAL void duk__emit_stridx(duk_json_enc_ctx *js_ctx, duk_small_uint_t stridx) {
  26212. DUK_ASSERT_DISABLE(stridx >= 0); /* unsigned */
  26213. DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
  26214. duk__emit_hstring(js_ctx, DUK_HTHREAD_GET_STRING(js_ctx->thr, stridx));
  26215. }
  26216. /* Check whether key quotes would be needed (custom encoding). */
  26217. DUK_LOCAL duk_bool_t duk__enc_key_quotes_needed(duk_hstring *h_key) {
  26218. const duk_uint8_t *p, *p_start, *p_end;
  26219. duk_small_uint_t ch;
  26220. DUK_ASSERT(h_key != NULL);
  26221. p_start = DUK_HSTRING_GET_DATA(h_key);
  26222. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_key);
  26223. p = p_start;
  26224. DUK_DDD(DUK_DDDPRINT("duk__enc_key_quotes_needed: h_key=%!O, p_start=%p, p_end=%p, p=%p",
  26225. (duk_heaphdr *) h_key, (void *) p_start, (void *) p_end, (void *) p));
  26226. /* Since we only accept ASCII characters, there is no need for
  26227. * actual decoding. A non-ASCII character will be >= 0x80 which
  26228. * causes a false return value immediately.
  26229. */
  26230. if (p == p_end) {
  26231. /* Zero length string is not accepted without quotes */
  26232. return 1;
  26233. }
  26234. while (p < p_end) {
  26235. ch = (duk_small_uint_t) (*p);
  26236. /* Accept ASCII IdentifierStart and IdentifierPart if not first char.
  26237. * Function selection is a bit uncommon.
  26238. */
  26239. if ((p > p_start ? duk_unicode_is_identifier_part :
  26240. duk_unicode_is_identifier_start) ((duk_codepoint_t) ch)) {
  26241. p++;
  26242. continue;
  26243. }
  26244. /* all non-ASCII characters also come here (first byte >= 0x80) */
  26245. return 1;
  26246. }
  26247. return 0;
  26248. }
  26249. /* The Quote(value) operation: quote a string.
  26250. *
  26251. * Stack policy: [ ] -> [ ].
  26252. */
  26253. DUK_LOCAL duk_uint8_t duk__quote_esc[14] = {
  26254. DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL,
  26255. DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL,
  26256. DUK_ASC_LC_B, DUK_ASC_LC_T, DUK_ASC_LC_N, DUK_ASC_NUL,
  26257. DUK_ASC_LC_F, DUK_ASC_LC_R
  26258. };
  26259. DUK_LOCAL void duk__enc_quote_string(duk_json_enc_ctx *js_ctx, duk_hstring *h_str) {
  26260. duk_hthread *thr = js_ctx->thr;
  26261. const duk_uint8_t *p, *p_start, *p_end, *p_tmp;
  26262. duk_ucodepoint_t cp; /* typed for duk_unicode_decode_xutf8() */
  26263. DUK_DDD(DUK_DDDPRINT("duk__enc_quote_string: h_str=%!O", (duk_heaphdr *) h_str));
  26264. DUK_ASSERT(h_str != NULL);
  26265. p_start = DUK_HSTRING_GET_DATA(h_str);
  26266. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_str);
  26267. p = p_start;
  26268. DUK__EMIT_1(js_ctx, DUK_ASC_DOUBLEQUOTE);
  26269. while (p < p_end) {
  26270. cp = *p;
  26271. if (DUK_LIKELY(cp <= 0x7f)) {
  26272. /* ascii fast path: avoid decoding utf-8 */
  26273. p++;
  26274. if (cp == 0x22 || cp == 0x5c) {
  26275. /* double quote or backslash */
  26276. DUK__EMIT_2(js_ctx, DUK_ASC_BACKSLASH, cp);
  26277. } else if (cp < 0x20) {
  26278. duk_uint_fast8_t esc_char;
  26279. /* This approach is a bit shorter than a straight
  26280. * if-else-ladder and also a bit faster.
  26281. */
  26282. if (cp < (sizeof(duk__quote_esc) / sizeof(duk_uint8_t)) &&
  26283. (esc_char = duk__quote_esc[cp]) != 0) {
  26284. DUK__EMIT_2(js_ctx, DUK_ASC_BACKSLASH, esc_char);
  26285. } else {
  26286. DUK__EMIT_ESC_AUTO(js_ctx, cp);
  26287. }
  26288. } else if (cp == 0x7f && js_ctx->flag_ascii_only) {
  26289. DUK__EMIT_ESC_AUTO(js_ctx, cp);
  26290. } else {
  26291. /* any other printable -> as is */
  26292. DUK__EMIT_1(js_ctx, cp);
  26293. }
  26294. } else {
  26295. /* slow path decode */
  26296. /* If XUTF-8 decoding fails, treat the offending byte as a codepoint directly
  26297. * and go forward one byte. This is of course very lossy, but allows some kind
  26298. * of output to be produced even for internal strings which don't conform to
  26299. * XUTF-8. All standard Ecmascript strings are always CESU-8, so this behavior
  26300. * does not violate the Ecmascript specification. The behavior is applied to
  26301. * all modes, including Ecmascript standard JSON. Because the current XUTF-8
  26302. * decoding is not very strict, this behavior only really affects initial bytes
  26303. * and truncated codepoints.
  26304. *
  26305. * XXX: another alternative would be to scan forwards to start of next codepoint
  26306. * (or end of input) and emit just one replacement codepoint.
  26307. */
  26308. p_tmp = p;
  26309. if (!duk_unicode_decode_xutf8(thr, &p, p_start, p_end, &cp)) {
  26310. /* Decode failed. */
  26311. cp = *p_tmp;
  26312. p = p_tmp + 1;
  26313. }
  26314. #ifdef DUK_USE_NONSTD_JSON_ESC_U2028_U2029
  26315. if (js_ctx->flag_ascii_only || cp == 0x2028 || cp == 0x2029) {
  26316. #else
  26317. if (js_ctx->flag_ascii_only) {
  26318. #endif
  26319. DUK__EMIT_ESC_AUTO(js_ctx, cp);
  26320. } else {
  26321. /* as is */
  26322. DUK__EMIT_XUTF8(js_ctx, cp);
  26323. }
  26324. }
  26325. }
  26326. DUK__EMIT_1(js_ctx, DUK_ASC_DOUBLEQUOTE);
  26327. }
  26328. /* Shared entry handling for object/array serialization: indent/stepback,
  26329. * loop detection.
  26330. */
  26331. 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) {
  26332. duk_context *ctx = (duk_context *) js_ctx->thr;
  26333. duk_hobject *h_target;
  26334. *entry_top = duk_get_top(ctx);
  26335. duk_require_stack(ctx, DUK_JSON_ENC_REQSTACK);
  26336. /* loop check */
  26337. h_target = duk_get_hobject(ctx, -1); /* object or array */
  26338. DUK_ASSERT(h_target != NULL);
  26339. duk_push_sprintf(ctx, DUK_STR_FMT_PTR, (void *) h_target);
  26340. duk_dup_top(ctx); /* -> [ ... voidp voidp ] */
  26341. if (duk_has_prop(ctx, js_ctx->idx_loop)) {
  26342. DUK_ERROR((duk_hthread *) ctx, DUK_ERR_TYPE_ERROR, DUK_STR_CYCLIC_INPUT);
  26343. }
  26344. duk_push_true(ctx); /* -> [ ... voidp true ] */
  26345. duk_put_prop(ctx, js_ctx->idx_loop); /* -> [ ... ] */
  26346. /* c recursion check */
  26347. DUK_ASSERT(js_ctx->recursion_depth >= 0);
  26348. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  26349. if (js_ctx->recursion_depth >= js_ctx->recursion_limit) {
  26350. DUK_ERROR((duk_hthread *) ctx, DUK_ERR_RANGE_ERROR, DUK_STR_JSONENC_RECLIMIT);
  26351. }
  26352. js_ctx->recursion_depth++;
  26353. /* figure out indent and stepback */
  26354. *h_indent = NULL;
  26355. *h_stepback = NULL;
  26356. if (js_ctx->h_gap != NULL) {
  26357. DUK_ASSERT(js_ctx->h_indent != NULL);
  26358. *h_stepback = js_ctx->h_indent;
  26359. duk_push_hstring(ctx, js_ctx->h_indent);
  26360. duk_push_hstring(ctx, js_ctx->h_gap);
  26361. duk_concat(ctx, 2);
  26362. js_ctx->h_indent = duk_get_hstring(ctx, -1);
  26363. *h_indent = js_ctx->h_indent;
  26364. DUK_ASSERT(js_ctx->h_indent != NULL);
  26365. /* The new indent string is left at value stack top, and will
  26366. * be popped by the shared exit handler.
  26367. */
  26368. } else {
  26369. DUK_ASSERT(js_ctx->h_indent == NULL);
  26370. }
  26371. DUK_DDD(DUK_DDDPRINT("shared entry finished: top=%ld, loop=%!T",
  26372. (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, js_ctx->idx_loop)));
  26373. }
  26374. /* Shared exit handling for object/array serialization. */
  26375. 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) {
  26376. duk_context *ctx = (duk_context *) js_ctx->thr;
  26377. duk_hobject *h_target;
  26378. DUK_UNREF(h_indent);
  26379. if (js_ctx->h_gap != NULL) {
  26380. DUK_ASSERT(js_ctx->h_indent != NULL);
  26381. DUK_ASSERT(*h_stepback != NULL);
  26382. DUK_ASSERT(*h_indent != NULL);
  26383. js_ctx->h_indent = *h_stepback; /* previous js_ctx->h_indent */
  26384. /* Note: we don't need to pop anything because the duk_set_top()
  26385. * at the end will take care of it.
  26386. */
  26387. } else {
  26388. DUK_ASSERT(js_ctx->h_indent == NULL);
  26389. DUK_ASSERT(*h_stepback == NULL);
  26390. DUK_ASSERT(*h_indent == NULL);
  26391. }
  26392. /* c recursion check */
  26393. DUK_ASSERT(js_ctx->recursion_depth > 0);
  26394. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  26395. js_ctx->recursion_depth--;
  26396. /* loop check */
  26397. h_target = duk_get_hobject(ctx, *entry_top - 1); /* original target at entry_top - 1 */
  26398. DUK_ASSERT(h_target != NULL);
  26399. duk_push_sprintf(ctx, DUK_STR_FMT_PTR, (void *) h_target);
  26400. duk_del_prop(ctx, js_ctx->idx_loop); /* -> [ ... ] */
  26401. /* restore stack top after unbalanced code paths */
  26402. duk_set_top(ctx, *entry_top);
  26403. DUK_DDD(DUK_DDDPRINT("shared entry finished: top=%ld, loop=%!T",
  26404. (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, js_ctx->idx_loop)));
  26405. }
  26406. /* The JO(value) operation: encode object.
  26407. *
  26408. * Stack policy: [ object ] -> [ object ].
  26409. */
  26410. DUK_LOCAL void duk__enc_object(duk_json_enc_ctx *js_ctx) {
  26411. duk_context *ctx = (duk_context *) js_ctx->thr;
  26412. duk_hstring *h_stepback;
  26413. duk_hstring *h_indent;
  26414. duk_hstring *h_key;
  26415. duk_idx_t entry_top;
  26416. duk_idx_t idx_obj;
  26417. duk_idx_t idx_keys;
  26418. duk_bool_t first;
  26419. duk_bool_t undef;
  26420. duk_uarridx_t arr_len, i;
  26421. DUK_DDD(DUK_DDDPRINT("duk__enc_object: obj=%!T", (duk_tval *) duk_get_tval(ctx, -1)));
  26422. duk__enc_objarr_entry(js_ctx, &h_stepback, &h_indent, &entry_top);
  26423. idx_obj = entry_top - 1;
  26424. if (js_ctx->idx_proplist >= 0) {
  26425. idx_keys = js_ctx->idx_proplist;
  26426. } else {
  26427. /* XXX: would be nice to enumerate an object at specified index */
  26428. duk_dup(ctx, idx_obj);
  26429. (void) duk_hobject_get_enumerated_keys(ctx, DUK_ENUM_OWN_PROPERTIES_ONLY /*flags*/); /* [ ... target ] -> [ ... target keys ] */
  26430. idx_keys = duk_require_normalize_index(ctx, -1);
  26431. /* leave stack unbalanced on purpose */
  26432. }
  26433. DUK_DDD(DUK_DDDPRINT("idx_keys=%ld, h_keys=%!T",
  26434. (long) idx_keys, (duk_tval *) duk_get_tval(ctx, idx_keys)));
  26435. /* Steps 8-10 have been merged to avoid a "partial" variable. */
  26436. DUK__EMIT_1(js_ctx, DUK_ASC_LCURLY);
  26437. /* XXX: keys is an internal object with all keys to be processed
  26438. * in its (gapless) array part. Because nobody can touch the keys
  26439. * object, we could iterate its array part directly (keeping in mind
  26440. * that it can be reallocated).
  26441. */
  26442. arr_len = (duk_uarridx_t) duk_get_length(ctx, idx_keys);
  26443. first = 1;
  26444. for (i = 0; i < arr_len; i++) {
  26445. duk_get_prop_index(ctx, idx_keys, i); /* -> [ ... key ] */
  26446. DUK_DDD(DUK_DDDPRINT("object property loop: holder=%!T, key=%!T",
  26447. (duk_tval *) duk_get_tval(ctx, idx_obj),
  26448. (duk_tval *) duk_get_tval(ctx, -1)));
  26449. undef = duk__enc_value1(js_ctx, idx_obj);
  26450. if (undef) {
  26451. /* Value would yield 'undefined', so skip key altogether.
  26452. * Side effects have already happened.
  26453. */
  26454. continue;
  26455. }
  26456. /* [ ... key val ] */
  26457. if (first) {
  26458. first = 0;
  26459. } else {
  26460. DUK__EMIT_1(js_ctx, DUK_ASC_COMMA);
  26461. }
  26462. if (h_indent != NULL) {
  26463. DUK__EMIT_1(js_ctx, 0x0a);
  26464. DUK__EMIT_HSTR(js_ctx, h_indent);
  26465. }
  26466. h_key = duk_get_hstring(ctx, -2);
  26467. DUK_ASSERT(h_key != NULL);
  26468. if (js_ctx->flag_avoid_key_quotes && !duk__enc_key_quotes_needed(h_key)) {
  26469. /* emit key as is */
  26470. DUK__EMIT_HSTR(js_ctx, h_key);
  26471. } else {
  26472. duk__enc_quote_string(js_ctx, h_key);
  26473. }
  26474. if (h_indent != NULL) {
  26475. DUK__EMIT_2(js_ctx, DUK_ASC_COLON, DUK_ASC_SPACE);
  26476. } else {
  26477. DUK__EMIT_1(js_ctx, DUK_ASC_COLON);
  26478. }
  26479. /* [ ... key val ] */
  26480. duk__enc_value2(js_ctx); /* -> [ ... ] */
  26481. }
  26482. if (!first) {
  26483. if (h_stepback != NULL) {
  26484. DUK_ASSERT(h_indent != NULL);
  26485. DUK__EMIT_1(js_ctx, 0x0a);
  26486. DUK__EMIT_HSTR(js_ctx, h_stepback);
  26487. }
  26488. }
  26489. DUK__EMIT_1(js_ctx, DUK_ASC_RCURLY);
  26490. duk__enc_objarr_exit(js_ctx, &h_stepback, &h_indent, &entry_top);
  26491. DUK_ASSERT_TOP(ctx, entry_top);
  26492. }
  26493. /* The JA(value) operation: encode array.
  26494. *
  26495. * Stack policy: [ array ] -> [ array ].
  26496. */
  26497. DUK_LOCAL void duk__enc_array(duk_json_enc_ctx *js_ctx) {
  26498. duk_context *ctx = (duk_context *) js_ctx->thr;
  26499. duk_hstring *h_stepback;
  26500. duk_hstring *h_indent;
  26501. duk_idx_t entry_top;
  26502. duk_idx_t idx_arr;
  26503. duk_bool_t undef;
  26504. duk_uarridx_t i, arr_len;
  26505. DUK_DDD(DUK_DDDPRINT("duk__enc_array: array=%!T",
  26506. (duk_tval *) duk_get_tval(ctx, -1)));
  26507. duk__enc_objarr_entry(js_ctx, &h_stepback, &h_indent, &entry_top);
  26508. idx_arr = entry_top - 1;
  26509. /* Steps 8-10 have been merged to avoid a "partial" variable. */
  26510. DUK__EMIT_1(js_ctx, DUK_ASC_LBRACKET);
  26511. arr_len = (duk_uarridx_t) duk_get_length(ctx, idx_arr);
  26512. for (i = 0; i < arr_len; i++) {
  26513. DUK_DDD(DUK_DDDPRINT("array entry loop: array=%!T, h_indent=%!O, h_stepback=%!O, index=%ld, arr_len=%ld",
  26514. (duk_tval *) duk_get_tval(ctx, idx_arr), (duk_heaphdr *) h_indent,
  26515. (duk_heaphdr *) h_stepback, (long) i, (long) arr_len));
  26516. if (i > 0) {
  26517. DUK__EMIT_1(js_ctx, DUK_ASC_COMMA);
  26518. }
  26519. if (h_indent != NULL) {
  26520. DUK__EMIT_1(js_ctx, 0x0a);
  26521. DUK__EMIT_HSTR(js_ctx, h_indent);
  26522. }
  26523. /* XXX: duk_push_uint_string() */
  26524. duk_push_uint(ctx, (duk_uint_t) i);
  26525. duk_to_string(ctx, -1); /* -> [ ... key ] */
  26526. undef = duk__enc_value1(js_ctx, idx_arr);
  26527. if (undef) {
  26528. DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL);
  26529. } else {
  26530. /* [ ... key val ] */
  26531. duk__enc_value2(js_ctx);
  26532. }
  26533. }
  26534. if (arr_len > 0) {
  26535. if (h_stepback != NULL) {
  26536. DUK_ASSERT(h_indent != NULL);
  26537. DUK__EMIT_1(js_ctx, 0x0a);
  26538. DUK__EMIT_HSTR(js_ctx, h_stepback);
  26539. }
  26540. }
  26541. DUK__EMIT_1(js_ctx, DUK_ASC_RBRACKET);
  26542. duk__enc_objarr_exit(js_ctx, &h_stepback, &h_indent, &entry_top);
  26543. DUK_ASSERT_TOP(ctx, entry_top);
  26544. }
  26545. /* The Str(key, holder) operation: encode value, steps 1-4.
  26546. *
  26547. * Returns non-zero if the value between steps 4 and 5 would yield an
  26548. * 'undefined' final result. This is useful in JO() because we need to
  26549. * get the side effects out, but need to know whether or not a key will
  26550. * be omitted from the serialization.
  26551. *
  26552. * Stack policy: [ ... key ] -> [ ... key val ] if retval == 0.
  26553. * -> [ ... ] if retval != 0.
  26554. */
  26555. DUK_LOCAL duk_bool_t duk__enc_value1(duk_json_enc_ctx *js_ctx, duk_idx_t idx_holder) {
  26556. duk_context *ctx = (duk_context *) js_ctx->thr;
  26557. duk_hthread *thr = (duk_hthread *) ctx;
  26558. duk_hobject *h;
  26559. duk_tval *tv;
  26560. duk_small_int_t c;
  26561. DUK_DDD(DUK_DDDPRINT("duk__enc_value1: idx_holder=%ld, holder=%!T, key=%!T",
  26562. (long) idx_holder, (duk_tval *) duk_get_tval(ctx, idx_holder),
  26563. (duk_tval *) duk_get_tval(ctx, -1)));
  26564. DUK_UNREF(thr);
  26565. duk_dup_top(ctx); /* -> [ ... key key ] */
  26566. duk_get_prop(ctx, idx_holder); /* -> [ ... key val ] */
  26567. DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(ctx, -1)));
  26568. h = duk_get_hobject_or_lfunc_coerce(ctx, -1);
  26569. if (h != NULL) {
  26570. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_TO_JSON);
  26571. h = duk_get_hobject_or_lfunc_coerce(ctx, -1); /* toJSON() can also be a lightfunc */
  26572. if (h != NULL && DUK_HOBJECT_IS_CALLABLE(h)) {
  26573. DUK_DDD(DUK_DDDPRINT("value is object, has callable toJSON() -> call it"));
  26574. duk_dup(ctx, -2); /* -> [ ... key val toJSON val ] */
  26575. duk_dup(ctx, -4); /* -> [ ... key val toJSON val key ] */
  26576. duk_call_method(ctx, 1); /* -> [ ... key val val' ] */
  26577. duk_remove(ctx, -2); /* -> [ ... key val' ] */
  26578. } else {
  26579. duk_pop(ctx);
  26580. }
  26581. }
  26582. /* [ ... key val ] */
  26583. DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(ctx, -1)));
  26584. if (js_ctx->h_replacer) {
  26585. /* XXX: here a "slice copy" would be useful */
  26586. DUK_DDD(DUK_DDDPRINT("replacer is set, call replacer"));
  26587. duk_push_hobject(ctx, js_ctx->h_replacer); /* -> [ ... key val replacer ] */
  26588. duk_dup(ctx, idx_holder); /* -> [ ... key val replacer holder ] */
  26589. duk_dup(ctx, -4); /* -> [ ... key val replacer holder key ] */
  26590. duk_dup(ctx, -4); /* -> [ ... key val replacer holder key val ] */
  26591. duk_call_method(ctx, 2); /* -> [ ... key val val' ] */
  26592. duk_remove(ctx, -2); /* -> [ ... key val' ] */
  26593. }
  26594. /* [ ... key val ] */
  26595. DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(ctx, -1)));
  26596. tv = duk_get_tval(ctx, -1);
  26597. DUK_ASSERT(tv != NULL);
  26598. if (DUK_TVAL_IS_OBJECT(tv)) {
  26599. h = DUK_TVAL_GET_OBJECT(tv);
  26600. DUK_ASSERT(h != NULL);
  26601. if (DUK_HOBJECT_IS_BUFFEROBJECT(h)) {
  26602. duk_hbufferobject *h_bufobj;
  26603. h_bufobj = (duk_hbufferobject *) h;
  26604. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  26605. if (h_bufobj->buf == NULL || !DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj)) {
  26606. duk_push_null(ctx);
  26607. } else if (DUK_HBUFFEROBJECT_FULL_SLICE(h_bufobj)) {
  26608. duk_push_hbuffer(ctx, h_bufobj->buf);
  26609. } else {
  26610. /* This is not very good because we're making a copy
  26611. * for serialization, but only for proper views.
  26612. * Better support would be to serialize slices
  26613. * directly but since we only push a raw buffer
  26614. * here we can't convey the slice offset/length.
  26615. */
  26616. duk_uint8_t *p_buf;
  26617. p_buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, h_bufobj->length);
  26618. DUK_MEMCPY((void *) p_buf,
  26619. (const void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufobj)),
  26620. h_bufobj->length);
  26621. }
  26622. duk_remove(ctx, -2);
  26623. } else {
  26624. c = (duk_small_int_t) DUK_HOBJECT_GET_CLASS_NUMBER(h);
  26625. switch ((int) c) {
  26626. case DUK_HOBJECT_CLASS_NUMBER: {
  26627. DUK_DDD(DUK_DDDPRINT("value is a Number object -> coerce with ToNumber()"));
  26628. duk_to_number(ctx, -1);
  26629. break;
  26630. }
  26631. case DUK_HOBJECT_CLASS_STRING: {
  26632. DUK_DDD(DUK_DDDPRINT("value is a String object -> coerce with ToString()"));
  26633. duk_to_string(ctx, -1);
  26634. break;
  26635. }
  26636. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  26637. case DUK_HOBJECT_CLASS_POINTER:
  26638. #endif
  26639. case DUK_HOBJECT_CLASS_BOOLEAN: {
  26640. DUK_DDD(DUK_DDDPRINT("value is a Boolean/Buffer/Pointer object -> get internal value"));
  26641. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE);
  26642. duk_remove(ctx, -2);
  26643. break;
  26644. }
  26645. } /* end switch */
  26646. }
  26647. }
  26648. /* [ ... key val ] */
  26649. DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(ctx, -1)));
  26650. if (duk_check_type_mask(ctx, -1, js_ctx->mask_for_undefined)) {
  26651. /* will result in undefined */
  26652. DUK_DDD(DUK_DDDPRINT("-> will result in undefined (type mask check)"));
  26653. goto undef;
  26654. }
  26655. /* functions are detected specially */
  26656. h = duk_get_hobject(ctx, -1);
  26657. if (h != NULL && DUK_HOBJECT_IS_CALLABLE(h)) {
  26658. if (js_ctx->flags & (DUK_JSON_FLAG_EXT_CUSTOM |
  26659. DUK_JSON_FLAG_EXT_COMPATIBLE)) {
  26660. /* function will be serialized to custom format */
  26661. } else {
  26662. /* functions are not serialized, results in undefined */
  26663. DUK_DDD(DUK_DDDPRINT("-> will result in undefined (function)"));
  26664. goto undef;
  26665. }
  26666. }
  26667. DUK_DDD(DUK_DDDPRINT("-> will not result in undefined"));
  26668. return 0;
  26669. undef:
  26670. duk_pop_2(ctx);
  26671. return 1;
  26672. }
  26673. /* The Str(key, holder) operation: encode value, steps 5-10.
  26674. *
  26675. * This must not be called unless duk__enc_value1() returns non-zero.
  26676. * If so, this is guaranteed to produce a non-undefined result.
  26677. * Non-standard encodings (e.g. for undefined) are only used if
  26678. * duk__enc_value1() indicates they are accepted; they're not
  26679. * checked or asserted here again.
  26680. *
  26681. * Stack policy: [ ... key val ] -> [ ... ].
  26682. */
  26683. DUK_LOCAL void duk__enc_value2(duk_json_enc_ctx *js_ctx) {
  26684. duk_context *ctx = (duk_context *) js_ctx->thr;
  26685. duk_hthread *thr = (duk_hthread *) ctx;
  26686. duk_tval *tv;
  26687. DUK_UNREF(thr);
  26688. DUK_DDD(DUK_DDDPRINT("duk__enc_value2: key=%!T, val=%!T",
  26689. (duk_tval *) duk_get_tval(ctx, -2),
  26690. (duk_tval *) duk_get_tval(ctx, -1)));
  26691. /* [ ... key val ] */
  26692. tv = duk_get_tval(ctx, -1);
  26693. DUK_ASSERT(tv != NULL);
  26694. switch (DUK_TVAL_GET_TAG(tv)) {
  26695. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  26696. /* When JX/JC not in use, duk__enc_value1 will block undefined values. */
  26697. case DUK_TAG_UNDEFINED: {
  26698. DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_undefined);
  26699. break;
  26700. }
  26701. #endif
  26702. case DUK_TAG_NULL: {
  26703. DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL);
  26704. break;
  26705. }
  26706. case DUK_TAG_BOOLEAN: {
  26707. DUK__EMIT_STRIDX(js_ctx, DUK_TVAL_GET_BOOLEAN(tv) ?
  26708. DUK_STRIDX_TRUE : DUK_STRIDX_FALSE);
  26709. break;
  26710. }
  26711. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  26712. /* When JX/JC not in use, duk__enc_value1 will block pointer values. */
  26713. case DUK_TAG_POINTER: {
  26714. char buf[64]; /* XXX: how to figure correct size? */
  26715. const char *fmt;
  26716. void *ptr = DUK_TVAL_GET_POINTER(tv);
  26717. DUK_MEMZERO(buf, sizeof(buf));
  26718. /* The #ifdef clutter here needs to handle the three cases:
  26719. * (1) JX+JC, (2) JX only, (3) JC only.
  26720. */
  26721. #if defined(DUK_USE_JX) && defined(DUK_USE_JC)
  26722. if (js_ctx->flag_ext_custom)
  26723. #endif
  26724. #if defined(DUK_USE_JX)
  26725. {
  26726. fmt = ptr ? "(%p)" : "(null)";
  26727. }
  26728. #endif
  26729. #if defined(DUK_USE_JX) && defined(DUK_USE_JC)
  26730. else
  26731. #endif
  26732. #if defined(DUK_USE_JC)
  26733. {
  26734. fmt = ptr ? "{\"_ptr\":\"%p\"}" : "{\"_ptr\":\"null\"}";
  26735. }
  26736. #endif
  26737. /* When ptr == NULL, the format argument is unused. */
  26738. DUK_SNPRINTF(buf, sizeof(buf) - 1, fmt, ptr); /* must not truncate */
  26739. DUK__EMIT_CSTR(js_ctx, buf);
  26740. break;
  26741. }
  26742. #endif /* DUK_USE_JX || DUK_USE_JC */
  26743. case DUK_TAG_STRING: {
  26744. duk_hstring *h = DUK_TVAL_GET_STRING(tv);
  26745. DUK_ASSERT(h != NULL);
  26746. duk__enc_quote_string(js_ctx, h);
  26747. break;
  26748. }
  26749. case DUK_TAG_OBJECT: {
  26750. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  26751. DUK_ASSERT(h != NULL);
  26752. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  26753. if (DUK_HOBJECT_IS_CALLABLE(h)) {
  26754. /* We only get here when doing non-standard JSON encoding */
  26755. DUK_ASSERT(js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible);
  26756. DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_function);
  26757. } else /* continues below */
  26758. #endif
  26759. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY) {
  26760. duk__enc_array(js_ctx);
  26761. } else {
  26762. duk__enc_object(js_ctx);
  26763. }
  26764. break;
  26765. }
  26766. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  26767. /* When JX/JC not in use, duk__enc_value1 will block buffer values. */
  26768. case DUK_TAG_BUFFER: {
  26769. /* Buffer values are encoded in (lowercase) hex to make the
  26770. * binary data readable. Base64 or similar would be more
  26771. * compact but less readable, and the point of JX/JC
  26772. * variants is to be as useful to a programmer as possible.
  26773. */
  26774. /* The #ifdef clutter here needs to handle the three cases:
  26775. * (1) JX+JC, (2) JX only, (3) JC only.
  26776. */
  26777. #if defined(DUK_USE_JX) && defined(DUK_USE_JC)
  26778. if (js_ctx->flag_ext_custom)
  26779. #endif
  26780. #if defined(DUK_USE_JX)
  26781. {
  26782. duk_uint8_t *p, *p_end;
  26783. duk_small_uint_t x;
  26784. duk_hbuffer *h;
  26785. h = DUK_TVAL_GET_BUFFER(tv);
  26786. DUK_ASSERT(h != NULL);
  26787. p = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h);
  26788. p_end = p + DUK_HBUFFER_GET_SIZE(h);
  26789. DUK__EMIT_1(js_ctx, DUK_ASC_PIPE);
  26790. while (p < p_end) {
  26791. x = *p++;
  26792. duk_hbuffer_append_byte(js_ctx->thr, js_ctx->h_buf, duk_lc_digits[(x >> 4) & 0x0f]);
  26793. duk_hbuffer_append_byte(js_ctx->thr, js_ctx->h_buf, duk_lc_digits[x & 0x0f]);
  26794. }
  26795. DUK__EMIT_1(js_ctx, DUK_ASC_PIPE);
  26796. }
  26797. #endif
  26798. #if defined(DUK_USE_JX) && defined(DUK_USE_JC)
  26799. else
  26800. #endif
  26801. #if defined(DUK_USE_JC)
  26802. {
  26803. DUK_ASSERT(js_ctx->flag_ext_compatible);
  26804. duk_hex_encode(ctx, -1);
  26805. DUK__EMIT_CSTR(js_ctx, "{\"_buf\":");
  26806. duk__enc_quote_string(js_ctx, duk_require_hstring(ctx, -1));
  26807. DUK__EMIT_1(js_ctx, DUK_ASC_RCURLY);
  26808. }
  26809. #endif
  26810. break;
  26811. }
  26812. #endif /* DUK_USE_JX || DUK_USE_JC */
  26813. case DUK_TAG_LIGHTFUNC: {
  26814. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  26815. /* We only get here when doing non-standard JSON encoding */
  26816. DUK_ASSERT(js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible);
  26817. DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_function);
  26818. #else
  26819. /* Standard JSON omits functions */
  26820. DUK_UNREACHABLE();
  26821. #endif
  26822. break;
  26823. }
  26824. #if defined(DUK_USE_FASTINT)
  26825. case DUK_TAG_FASTINT:
  26826. #endif
  26827. default: {
  26828. /* number */
  26829. duk_double_t d;
  26830. duk_small_int_t c;
  26831. duk_small_int_t s;
  26832. duk_small_uint_t stridx;
  26833. duk_small_uint_t n2s_flags;
  26834. duk_hstring *h_str;
  26835. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  26836. d = DUK_TVAL_GET_NUMBER(tv);
  26837. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  26838. s = (duk_small_int_t) DUK_SIGNBIT(d);
  26839. DUK_UNREF(s);
  26840. if (DUK_LIKELY(!(c == DUK_FP_INFINITE || c == DUK_FP_NAN))) {
  26841. DUK_ASSERT(DUK_ISFINITE(d));
  26842. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  26843. /* Negative zero needs special handling in JX/JC because
  26844. * it would otherwise serialize to '0', not '-0'.
  26845. */
  26846. if (DUK_UNLIKELY(c == DUK_FP_ZERO && s != 0 &&
  26847. (js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible))) {
  26848. duk_push_hstring_stridx(ctx, DUK_STRIDX_MINUS_ZERO); /* '-0' */
  26849. } else
  26850. #endif /* DUK_USE_JX || DUK_USE_JC */
  26851. {
  26852. n2s_flags = 0;
  26853. /* [ ... number ] -> [ ... string ] */
  26854. duk_numconv_stringify(ctx, 10 /*radix*/, 0 /*digits*/, n2s_flags);
  26855. }
  26856. h_str = duk_to_hstring(ctx, -1);
  26857. DUK_ASSERT(h_str != NULL);
  26858. DUK__EMIT_HSTR(js_ctx, h_str);
  26859. break;
  26860. }
  26861. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  26862. if (!(js_ctx->flags & (DUK_JSON_FLAG_EXT_CUSTOM |
  26863. DUK_JSON_FLAG_EXT_COMPATIBLE))) {
  26864. stridx = DUK_STRIDX_LC_NULL;
  26865. } else if (c == DUK_FP_NAN) {
  26866. stridx = js_ctx->stridx_custom_nan;
  26867. } else if (s == 0) {
  26868. stridx = js_ctx->stridx_custom_posinf;
  26869. } else {
  26870. stridx = js_ctx->stridx_custom_neginf;
  26871. }
  26872. #else
  26873. stridx = DUK_STRIDX_LC_NULL;
  26874. #endif
  26875. DUK__EMIT_STRIDX(js_ctx, stridx);
  26876. break;
  26877. }
  26878. }
  26879. /* [ ... key val ] -> [ ... ] */
  26880. duk_pop_2(ctx);
  26881. }
  26882. /* E5 Section 15.12.3, main algorithm, step 4.b.ii steps 1-4. */
  26883. DUK_LOCAL duk_bool_t duk__enc_allow_into_proplist(duk_tval *tv) {
  26884. duk_hobject *h;
  26885. duk_small_int_t c;
  26886. DUK_ASSERT(tv != NULL);
  26887. if (DUK_TVAL_IS_STRING(tv) || DUK_TVAL_IS_NUMBER(tv)) {
  26888. return 1;
  26889. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  26890. h = DUK_TVAL_GET_OBJECT(tv);
  26891. DUK_ASSERT(h != NULL);
  26892. c = (duk_small_int_t) DUK_HOBJECT_GET_CLASS_NUMBER(h);
  26893. if (c == DUK_HOBJECT_CLASS_STRING || c == DUK_HOBJECT_CLASS_NUMBER) {
  26894. return 1;
  26895. }
  26896. }
  26897. return 0;
  26898. }
  26899. /*
  26900. * Top level wrappers
  26901. */
  26902. DUK_INTERNAL
  26903. void duk_bi_json_parse_helper(duk_context *ctx,
  26904. duk_idx_t idx_value,
  26905. duk_idx_t idx_reviver,
  26906. duk_small_uint_t flags) {
  26907. duk_hthread *thr = (duk_hthread *) ctx;
  26908. duk_json_dec_ctx js_ctx_alloc;
  26909. duk_json_dec_ctx *js_ctx = &js_ctx_alloc;
  26910. duk_hstring *h_text;
  26911. #ifdef DUK_USE_ASSERTIONS
  26912. duk_idx_t entry_top = duk_get_top(ctx);
  26913. #endif
  26914. /* negative top-relative indices not allowed now */
  26915. DUK_ASSERT(idx_value == DUK_INVALID_INDEX || idx_value >= 0);
  26916. DUK_ASSERT(idx_reviver == DUK_INVALID_INDEX || idx_reviver >= 0);
  26917. DUK_DDD(DUK_DDDPRINT("JSON parse start: text=%!T, reviver=%!T, flags=0x%08lx, stack_top=%ld",
  26918. (duk_tval *) duk_get_tval(ctx, idx_value),
  26919. (duk_tval *) duk_get_tval(ctx, idx_reviver),
  26920. (unsigned long) flags,
  26921. (long) duk_get_top(ctx)));
  26922. DUK_MEMZERO(&js_ctx_alloc, sizeof(js_ctx_alloc));
  26923. js_ctx->thr = thr;
  26924. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  26925. /* nothing now */
  26926. #endif
  26927. js_ctx->recursion_limit = DUK_JSON_DEC_RECURSION_LIMIT;
  26928. /* Flag handling currently assumes that flags are consistent. This is OK
  26929. * because the call sites are now strictly controlled.
  26930. */
  26931. js_ctx->flags = flags;
  26932. #ifdef DUK_USE_JX
  26933. js_ctx->flag_ext_custom = flags & DUK_JSON_FLAG_EXT_CUSTOM;
  26934. #endif
  26935. #ifdef DUK_USE_JC
  26936. js_ctx->flag_ext_compatible = flags & DUK_JSON_FLAG_EXT_COMPATIBLE;
  26937. #endif
  26938. h_text = duk_to_hstring(ctx, idx_value); /* coerce in-place */
  26939. DUK_ASSERT(h_text != NULL);
  26940. js_ctx->p_start = (duk_uint8_t *) DUK_HSTRING_GET_DATA(h_text);
  26941. js_ctx->p = js_ctx->p_start;
  26942. js_ctx->p_end = ((duk_uint8_t *) DUK_HSTRING_GET_DATA(h_text)) +
  26943. DUK_HSTRING_GET_BYTELEN(h_text);
  26944. duk__dec_value(js_ctx); /* -> [ ... value ] */
  26945. /* Trailing whitespace has been eaten by duk__dec_value(), so if
  26946. * we're not at end of input here, it's a SyntaxError.
  26947. */
  26948. if (js_ctx->p != js_ctx->p_end) {
  26949. duk__dec_syntax_error(js_ctx);
  26950. }
  26951. if (duk_is_callable(ctx, idx_reviver)) {
  26952. DUK_DDD(DUK_DDDPRINT("applying reviver: %!T",
  26953. (duk_tval *) duk_get_tval(ctx, idx_reviver)));
  26954. js_ctx->idx_reviver = idx_reviver;
  26955. duk_push_object(ctx);
  26956. duk_dup(ctx, -2); /* -> [ ... val root val ] */
  26957. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_EMPTY_STRING); /* default attrs ok */
  26958. duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING); /* -> [ ... val root "" ] */
  26959. DUK_DDD(DUK_DDDPRINT("start reviver walk, root=%!T, name=%!T",
  26960. (duk_tval *) duk_get_tval(ctx, -2),
  26961. (duk_tval *) duk_get_tval(ctx, -1)));
  26962. duk__dec_reviver_walk(js_ctx); /* [ ... val root "" ] -> [ ... val val' ] */
  26963. duk_remove(ctx, -2); /* -> [ ... val' ] */
  26964. } else {
  26965. DUK_DDD(DUK_DDDPRINT("reviver does not exist or is not callable: %!T",
  26966. (duk_tval *) duk_get_tval(ctx, idx_reviver)));
  26967. }
  26968. /* Final result is at stack top. */
  26969. DUK_DDD(DUK_DDDPRINT("JSON parse end: text=%!T, reviver=%!T, flags=0x%08lx, result=%!T, stack_top=%ld",
  26970. (duk_tval *) duk_get_tval(ctx, idx_value),
  26971. (duk_tval *) duk_get_tval(ctx, idx_reviver),
  26972. (unsigned long) flags,
  26973. (duk_tval *) duk_get_tval(ctx, -1),
  26974. (long) duk_get_top(ctx)));
  26975. DUK_ASSERT(duk_get_top(ctx) == entry_top + 1);
  26976. }
  26977. DUK_INTERNAL
  26978. void duk_bi_json_stringify_helper(duk_context *ctx,
  26979. duk_idx_t idx_value,
  26980. duk_idx_t idx_replacer,
  26981. duk_idx_t idx_space,
  26982. duk_small_uint_t flags) {
  26983. duk_hthread *thr = (duk_hthread *) ctx;
  26984. duk_json_enc_ctx js_ctx_alloc;
  26985. duk_json_enc_ctx *js_ctx = &js_ctx_alloc;
  26986. duk_hobject *h;
  26987. duk_bool_t undef;
  26988. duk_idx_t idx_holder;
  26989. duk_idx_t entry_top;
  26990. /* negative top-relative indices not allowed now */
  26991. DUK_ASSERT(idx_value == DUK_INVALID_INDEX || idx_value >= 0);
  26992. DUK_ASSERT(idx_replacer == DUK_INVALID_INDEX || idx_replacer >= 0);
  26993. DUK_ASSERT(idx_space == DUK_INVALID_INDEX || idx_space >= 0);
  26994. DUK_DDD(DUK_DDDPRINT("JSON stringify start: value=%!T, replacer=%!T, space=%!T, flags=0x%08lx, stack_top=%ld",
  26995. (duk_tval *) duk_get_tval(ctx, idx_value),
  26996. (duk_tval *) duk_get_tval(ctx, idx_replacer),
  26997. (duk_tval *) duk_get_tval(ctx, idx_space),
  26998. (unsigned long) flags,
  26999. (long) duk_get_top(ctx)));
  27000. entry_top = duk_get_top(ctx);
  27001. /*
  27002. * Context init
  27003. */
  27004. DUK_MEMZERO(&js_ctx_alloc, sizeof(js_ctx_alloc));
  27005. js_ctx->thr = thr;
  27006. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  27007. js_ctx->h_replacer = NULL;
  27008. js_ctx->h_gap = NULL;
  27009. js_ctx->h_indent = NULL;
  27010. #endif
  27011. js_ctx->idx_proplist = -1;
  27012. js_ctx->recursion_limit = DUK_JSON_ENC_RECURSION_LIMIT;
  27013. /* Flag handling currently assumes that flags are consistent. This is OK
  27014. * because the call sites are now strictly controlled.
  27015. */
  27016. js_ctx->flags = flags;
  27017. js_ctx->flag_ascii_only = flags & DUK_JSON_FLAG_ASCII_ONLY;
  27018. js_ctx->flag_avoid_key_quotes = flags & DUK_JSON_FLAG_AVOID_KEY_QUOTES;
  27019. #ifdef DUK_USE_JX
  27020. js_ctx->flag_ext_custom = flags & DUK_JSON_FLAG_EXT_CUSTOM;
  27021. #endif
  27022. #ifdef DUK_USE_JC
  27023. js_ctx->flag_ext_compatible = flags & DUK_JSON_FLAG_EXT_COMPATIBLE;
  27024. #endif
  27025. /* The #ifdef clutter here handles the JX/JC enable/disable
  27026. * combinations properly.
  27027. */
  27028. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  27029. #if defined(DUK_USE_JX)
  27030. if (flags & DUK_JSON_FLAG_EXT_CUSTOM) {
  27031. js_ctx->stridx_custom_undefined = DUK_STRIDX_LC_UNDEFINED;
  27032. js_ctx->stridx_custom_nan = DUK_STRIDX_NAN;
  27033. js_ctx->stridx_custom_neginf = DUK_STRIDX_MINUS_INFINITY;
  27034. js_ctx->stridx_custom_posinf = DUK_STRIDX_INFINITY;
  27035. js_ctx->stridx_custom_function =
  27036. (flags & DUK_JSON_FLAG_AVOID_KEY_QUOTES) ?
  27037. DUK_STRIDX_JSON_EXT_FUNCTION2 :
  27038. DUK_STRIDX_JSON_EXT_FUNCTION1;
  27039. }
  27040. #endif /* DUK_USE_JX */
  27041. #if defined(DUK_USE_JX) && defined(DUK_USE_JC)
  27042. else
  27043. #endif /* DUK_USE_JX && DUK_USE_JC */
  27044. #if defined(DUK_USE_JC)
  27045. if (js_ctx->flags & DUK_JSON_FLAG_EXT_COMPATIBLE) {
  27046. js_ctx->stridx_custom_undefined = DUK_STRIDX_JSON_EXT_UNDEFINED;
  27047. js_ctx->stridx_custom_nan = DUK_STRIDX_JSON_EXT_NAN;
  27048. js_ctx->stridx_custom_neginf = DUK_STRIDX_JSON_EXT_NEGINF;
  27049. js_ctx->stridx_custom_posinf = DUK_STRIDX_JSON_EXT_POSINF;
  27050. js_ctx->stridx_custom_function = DUK_STRIDX_JSON_EXT_FUNCTION1;
  27051. }
  27052. #endif /* DUK_USE_JC */
  27053. #endif /* DUK_USE_JX || DUK_USE_JC */
  27054. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  27055. if (js_ctx->flags & (DUK_JSON_FLAG_EXT_CUSTOM |
  27056. DUK_JSON_FLAG_EXT_COMPATIBLE)) {
  27057. DUK_ASSERT(js_ctx->mask_for_undefined == 0); /* already zero */
  27058. }
  27059. else
  27060. #endif /* DUK_USE_JX || DUK_USE_JC */
  27061. {
  27062. js_ctx->mask_for_undefined = DUK_TYPE_MASK_UNDEFINED |
  27063. DUK_TYPE_MASK_POINTER |
  27064. DUK_TYPE_MASK_BUFFER |
  27065. DUK_TYPE_MASK_LIGHTFUNC;
  27066. }
  27067. (void) duk_push_dynamic_buffer(ctx, 0);
  27068. js_ctx->h_buf = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, -1);
  27069. DUK_ASSERT(js_ctx->h_buf != NULL);
  27070. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(js_ctx->h_buf));
  27071. js_ctx->idx_loop = duk_push_object_internal(ctx);
  27072. DUK_ASSERT(js_ctx->idx_loop >= 0);
  27073. /* [ ... buf loop ] */
  27074. /*
  27075. * Process replacer/proplist (2nd argument to JSON.stringify)
  27076. */
  27077. h = duk_get_hobject(ctx, idx_replacer);
  27078. if (h != NULL) {
  27079. if (DUK_HOBJECT_IS_CALLABLE(h)) {
  27080. js_ctx->h_replacer = h;
  27081. } else if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY) {
  27082. /* Here the specification requires correct array index enumeration
  27083. * which is a bit tricky for sparse arrays (it is handled by the
  27084. * enum setup code). We now enumerate ancestors too, although the
  27085. * specification is not very clear on whether that is required.
  27086. */
  27087. duk_uarridx_t plist_idx = 0;
  27088. duk_small_uint_t enum_flags;
  27089. js_ctx->idx_proplist = duk_push_array(ctx); /* XXX: array internal? */
  27090. enum_flags = DUK_ENUM_ARRAY_INDICES_ONLY |
  27091. DUK_ENUM_SORT_ARRAY_INDICES; /* expensive flag */
  27092. duk_enum(ctx, idx_replacer, enum_flags);
  27093. while (duk_next(ctx, -1 /*enum_index*/, 1 /*get_value*/)) {
  27094. /* [ ... proplist enum_obj key val ] */
  27095. if (duk__enc_allow_into_proplist(duk_get_tval(ctx, -1))) {
  27096. /* XXX: duplicates should be eliminated here */
  27097. DUK_DDD(DUK_DDDPRINT("proplist enum: key=%!T, val=%!T --> accept",
  27098. (duk_tval *) duk_get_tval(ctx, -2),
  27099. (duk_tval *) duk_get_tval(ctx, -1)));
  27100. duk_to_string(ctx, -1); /* extra coercion of strings is OK */
  27101. duk_put_prop_index(ctx, -4, plist_idx); /* -> [ ... proplist enum_obj key ] */
  27102. plist_idx++;
  27103. duk_pop(ctx);
  27104. } else {
  27105. DUK_DDD(DUK_DDDPRINT("proplist enum: key=%!T, val=%!T --> reject",
  27106. (duk_tval *) duk_get_tval(ctx, -2),
  27107. (duk_tval *) duk_get_tval(ctx, -1)));
  27108. duk_pop_2(ctx);
  27109. }
  27110. }
  27111. duk_pop(ctx); /* pop enum */
  27112. /* [ ... proplist ] */
  27113. }
  27114. }
  27115. /* [ ... buf loop (proplist) ] */
  27116. /*
  27117. * Process space (3rd argument to JSON.stringify)
  27118. */
  27119. h = duk_get_hobject(ctx, idx_space);
  27120. if (h != NULL) {
  27121. int c = DUK_HOBJECT_GET_CLASS_NUMBER(h);
  27122. if (c == DUK_HOBJECT_CLASS_NUMBER) {
  27123. duk_to_number(ctx, idx_space);
  27124. } else if (c == DUK_HOBJECT_CLASS_STRING) {
  27125. duk_to_string(ctx, idx_space);
  27126. }
  27127. }
  27128. if (duk_is_number(ctx, idx_space)) {
  27129. duk_small_int_t nspace;
  27130. /* spaces[] must be static to allow initializer with old compilers like BCC */
  27131. static const char spaces[10] = {
  27132. DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE,
  27133. DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE,
  27134. DUK_ASC_SPACE, DUK_ASC_SPACE
  27135. }; /* XXX: helper */
  27136. /* ToInteger() coercion; NaN -> 0, infinities are clamped to 0 and 10 */
  27137. nspace = (duk_small_int_t) duk_to_int_clamped(ctx, idx_space, 0 /*minval*/, 10 /*maxval*/);
  27138. DUK_ASSERT(nspace >= 0 && nspace <= 10);
  27139. duk_push_lstring(ctx, spaces, (duk_size_t) nspace);
  27140. js_ctx->h_gap = duk_get_hstring(ctx, -1);
  27141. DUK_ASSERT(js_ctx->h_gap != NULL);
  27142. } else if (duk_is_string(ctx, idx_space)) {
  27143. /* XXX: substring in-place at idx_place? */
  27144. duk_dup(ctx, idx_space);
  27145. duk_substring(ctx, -1, 0, 10); /* clamp to 10 chars */
  27146. js_ctx->h_gap = duk_get_hstring(ctx, -1);
  27147. DUK_ASSERT(js_ctx->h_gap != NULL);
  27148. } else {
  27149. /* nop */
  27150. }
  27151. if (js_ctx->h_gap != NULL) {
  27152. /* if gap is empty, behave as if not given at all */
  27153. if (DUK_HSTRING_GET_CHARLEN(js_ctx->h_gap) == 0) {
  27154. js_ctx->h_gap = NULL;
  27155. } else {
  27156. /* set 'indent' only if it will actually increase */
  27157. js_ctx->h_indent = DUK_HTHREAD_STRING_EMPTY_STRING(thr);
  27158. }
  27159. }
  27160. DUK_ASSERT((js_ctx->h_gap == NULL && js_ctx->h_indent == NULL) ||
  27161. (js_ctx->h_gap != NULL && js_ctx->h_indent != NULL));
  27162. /* [ ... buf loop (proplist) (gap) ] */
  27163. /*
  27164. * Create wrapper object and serialize
  27165. */
  27166. idx_holder = duk_push_object(ctx);
  27167. duk_dup(ctx, idx_value);
  27168. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_EMPTY_STRING);
  27169. DUK_DDD(DUK_DDDPRINT("before: flags=0x%08lx, buf=%!O, loop=%!T, replacer=%!O, "
  27170. "proplist=%!T, gap=%!O, indent=%!O, holder=%!T",
  27171. (unsigned long) js_ctx->flags,
  27172. (duk_heaphdr *) js_ctx->h_buf,
  27173. (duk_tval *) duk_get_tval(ctx, js_ctx->idx_loop),
  27174. (duk_heaphdr *) js_ctx->h_replacer,
  27175. (duk_tval *) (js_ctx->idx_proplist >= 0 ? duk_get_tval(ctx, js_ctx->idx_proplist) : NULL),
  27176. (duk_heaphdr *) js_ctx->h_gap,
  27177. (duk_heaphdr *) js_ctx->h_indent,
  27178. (duk_tval *) duk_get_tval(ctx, -1)));
  27179. /* serialize the wrapper with empty string key */
  27180. duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING);
  27181. /* [ ... buf loop (proplist) (gap) holder "" ] */
  27182. undef = duk__enc_value1(js_ctx, idx_holder); /* [ ... holder key ] -> [ ... holder key val ] */
  27183. DUK_DDD(DUK_DDDPRINT("after: flags=0x%08lx, buf=%!O, loop=%!T, replacer=%!O, "
  27184. "proplist=%!T, gap=%!O, indent=%!O, holder=%!T",
  27185. (unsigned long) js_ctx->flags,
  27186. (duk_heaphdr *) js_ctx->h_buf,
  27187. (duk_tval *) duk_get_tval(ctx, js_ctx->idx_loop),
  27188. (duk_heaphdr *) js_ctx->h_replacer,
  27189. (duk_tval *) (js_ctx->idx_proplist >= 0 ? duk_get_tval(ctx, js_ctx->idx_proplist) : NULL),
  27190. (duk_heaphdr *) js_ctx->h_gap,
  27191. (duk_heaphdr *) js_ctx->h_indent,
  27192. (duk_tval *) duk_get_tval(ctx, -3)));
  27193. if (undef) {
  27194. /*
  27195. * Result is undefined
  27196. */
  27197. duk_push_undefined(ctx);
  27198. } else {
  27199. /*
  27200. * Finish and convert buffer to result string
  27201. */
  27202. duk__enc_value2(js_ctx); /* [ ... key val ] -> [ ... ] */
  27203. DUK_ASSERT(js_ctx->h_buf != NULL);
  27204. duk_push_hbuffer(ctx, (duk_hbuffer *) js_ctx->h_buf);
  27205. duk_to_string(ctx, -1);
  27206. }
  27207. /* The stack has a variable shape here, so force it to the
  27208. * desired one explicitly.
  27209. */
  27210. duk_replace(ctx, entry_top);
  27211. duk_set_top(ctx, entry_top + 1);
  27212. DUK_DDD(DUK_DDDPRINT("JSON stringify end: value=%!T, replacer=%!T, space=%!T, "
  27213. "flags=0x%08lx, result=%!T, stack_top=%ld",
  27214. (duk_tval *) duk_get_tval(ctx, idx_value),
  27215. (duk_tval *) duk_get_tval(ctx, idx_replacer),
  27216. (duk_tval *) duk_get_tval(ctx, idx_space),
  27217. (unsigned long) flags,
  27218. (duk_tval *) duk_get_tval(ctx, -1),
  27219. (long) duk_get_top(ctx)));
  27220. DUK_ASSERT(duk_get_top(ctx) == entry_top + 1);
  27221. }
  27222. /*
  27223. * Entry points
  27224. */
  27225. DUK_INTERNAL duk_ret_t duk_bi_json_object_parse(duk_context *ctx) {
  27226. duk_bi_json_parse_helper(ctx,
  27227. 0 /*idx_value*/,
  27228. 1 /*idx_replacer*/,
  27229. 0 /*flags*/);
  27230. return 1;
  27231. }
  27232. DUK_INTERNAL duk_ret_t duk_bi_json_object_stringify(duk_context *ctx) {
  27233. duk_bi_json_stringify_helper(ctx,
  27234. 0 /*idx_value*/,
  27235. 1 /*idx_replacer*/,
  27236. 2 /*idx_space*/,
  27237. 0 /*flags*/);
  27238. return 1;
  27239. }
  27240. #line 1 "duk_bi_logger.c"
  27241. /*
  27242. * Logging support
  27243. */
  27244. /* include removed: duk_internal.h */
  27245. /* 3-letter log level strings */
  27246. DUK_LOCAL const duk_uint8_t duk__log_level_strings[] = {
  27247. (duk_uint8_t) DUK_ASC_UC_T, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_C,
  27248. (duk_uint8_t) DUK_ASC_UC_D, (duk_uint8_t) DUK_ASC_UC_B, (duk_uint8_t) DUK_ASC_UC_G,
  27249. (duk_uint8_t) DUK_ASC_UC_I, (duk_uint8_t) DUK_ASC_UC_N, (duk_uint8_t) DUK_ASC_UC_F,
  27250. (duk_uint8_t) DUK_ASC_UC_W, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_N,
  27251. (duk_uint8_t) DUK_ASC_UC_E, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_R,
  27252. (duk_uint8_t) DUK_ASC_UC_F, (duk_uint8_t) DUK_ASC_UC_T, (duk_uint8_t) DUK_ASC_UC_L
  27253. };
  27254. /* Constructor */
  27255. DUK_INTERNAL duk_ret_t duk_bi_logger_constructor(duk_context *ctx) {
  27256. duk_hthread *thr = (duk_hthread *) ctx;
  27257. duk_idx_t nargs;
  27258. /* Calling as a non-constructor is not meaningful. */
  27259. if (!duk_is_constructor_call(ctx)) {
  27260. return DUK_RET_TYPE_ERROR;
  27261. }
  27262. nargs = duk_get_top(ctx);
  27263. duk_set_top(ctx, 1);
  27264. duk_push_this(ctx);
  27265. /* [ name this ] */
  27266. if (nargs == 0) {
  27267. /* Automatic defaulting of logger name from caller. This would
  27268. * work poorly with tail calls, but constructor calls are currently
  27269. * never tail calls, so tail calls are not an issue now.
  27270. */
  27271. if (thr->callstack_top >= 2) {
  27272. duk_activation *act_caller = thr->callstack + thr->callstack_top - 2;
  27273. duk_hobject *func_caller;
  27274. func_caller = DUK_ACT_GET_FUNC(act_caller);
  27275. if (func_caller) {
  27276. /* Stripping the filename might be a good idea
  27277. * ("/foo/bar/quux.js" -> logger name "quux"),
  27278. * but now used verbatim.
  27279. */
  27280. duk_push_hobject(ctx, func_caller);
  27281. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_FILE_NAME);
  27282. duk_replace(ctx, 0);
  27283. }
  27284. }
  27285. }
  27286. /* the stack is unbalanced here on purpose; we only rely on the
  27287. * initial two values: [ name this ].
  27288. */
  27289. if (duk_is_string(ctx, 0)) {
  27290. duk_dup(ctx, 0);
  27291. duk_put_prop_stridx(ctx, 1, DUK_STRIDX_LC_N);
  27292. } else {
  27293. /* don't set 'n' at all, inherited value is used as name */
  27294. }
  27295. duk_compact(ctx, 1);
  27296. return 0; /* keep default instance */
  27297. }
  27298. /* Default function to format objects. Tries to use toLogString() but falls
  27299. * back to toString(). Any errors are propagated out without catching.
  27300. */
  27301. DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_fmt(duk_context *ctx) {
  27302. if (duk_get_prop_stridx(ctx, 0, DUK_STRIDX_TO_LOG_STRING)) {
  27303. /* [ arg toLogString ] */
  27304. duk_dup(ctx, 0);
  27305. duk_call_method(ctx, 0);
  27306. /* [ arg result ] */
  27307. return 1;
  27308. }
  27309. /* [ arg undefined ] */
  27310. duk_pop(ctx);
  27311. duk_to_string(ctx, 0);
  27312. return 1;
  27313. }
  27314. /* Default function to write a formatted log line. Writes to stderr,
  27315. * appending a newline to the log line.
  27316. *
  27317. * The argument is a buffer whose visible size contains the log message.
  27318. * This function should avoid coercing the buffer to a string to avoid
  27319. * string table traffic.
  27320. */
  27321. DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_raw(duk_context *ctx) {
  27322. const char *data;
  27323. duk_size_t data_len;
  27324. DUK_UNREF(ctx);
  27325. DUK_UNREF(data);
  27326. DUK_UNREF(data_len);
  27327. #ifdef DUK_USE_FILE_IO
  27328. data = (const char *) duk_require_buffer(ctx, 0, &data_len);
  27329. DUK_FWRITE((const void *) data, 1, data_len, DUK_STDERR);
  27330. DUK_FPUTC((int) '\n', DUK_STDERR);
  27331. DUK_FFLUSH(DUK_STDERR);
  27332. #else
  27333. /* nop */
  27334. #endif
  27335. return 0;
  27336. }
  27337. /* Log frontend shared helper, magic value indicates log level. Provides
  27338. * frontend functions: trace(), debug(), info(), warn(), error(), fatal().
  27339. * This needs to have small footprint, reasonable performance, minimal
  27340. * memory churn, etc.
  27341. */
  27342. DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_log_shared(duk_context *ctx) {
  27343. duk_hthread *thr = (duk_hthread *) ctx;
  27344. duk_double_t now;
  27345. duk_small_int_t entry_lev = duk_get_current_magic(ctx);
  27346. duk_small_int_t logger_lev;
  27347. duk_int_t nargs;
  27348. duk_int_t i;
  27349. duk_size_t tot_len;
  27350. const duk_uint8_t *arg_str;
  27351. duk_size_t arg_len;
  27352. duk_uint8_t *buf, *p;
  27353. const duk_uint8_t *q;
  27354. duk_uint8_t date_buf[DUK_BI_DATE_ISO8601_BUFSIZE];
  27355. duk_size_t date_len;
  27356. duk_small_int_t rc;
  27357. DUK_ASSERT(entry_lev >= 0 && entry_lev <= 5);
  27358. /* XXX: sanitize to printable (and maybe ASCII) */
  27359. /* XXX: better multiline */
  27360. /*
  27361. * Logger arguments are:
  27362. *
  27363. * magic: log level (0-5)
  27364. * this: logger
  27365. * stack: plain log args
  27366. *
  27367. * We want to minimize memory churn so a two-pass approach
  27368. * is used: first pass formats arguments and computes final
  27369. * string length, second pass copies strings either into a
  27370. * pre-allocated and reused buffer (short messages) or into a
  27371. * newly allocated fixed buffer. If the backend function plays
  27372. * nice, it won't coerce the buffer to a string (and thus
  27373. * intern it).
  27374. */
  27375. nargs = duk_get_top(ctx);
  27376. /* [ arg1 ... argN this ] */
  27377. /*
  27378. * Log level check
  27379. */
  27380. duk_push_this(ctx);
  27381. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LC_L);
  27382. logger_lev = (duk_small_int_t) duk_get_int(ctx, -1);
  27383. if (entry_lev < logger_lev) {
  27384. return 0;
  27385. }
  27386. /* log level could be popped but that's not necessary */
  27387. now = duk_bi_date_get_now(ctx);
  27388. duk_bi_date_format_timeval(now, date_buf);
  27389. date_len = DUK_STRLEN((const char *) date_buf);
  27390. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_LC_N);
  27391. duk_to_string(ctx, -1);
  27392. DUK_ASSERT(duk_is_string(ctx, -1));
  27393. /* [ arg1 ... argN this loggerLevel loggerName ] */
  27394. /*
  27395. * Pass 1
  27396. */
  27397. /* Line format: <time> <entryLev> <loggerName>: <msg> */
  27398. tot_len = 0;
  27399. tot_len += 3 + /* separators: space, space, colon */
  27400. 3 + /* level string */
  27401. date_len + /* time */
  27402. duk_get_length(ctx, -1); /* loggerName */
  27403. for (i = 0; i < nargs; i++) {
  27404. /* When formatting an argument to a string, errors may happen from multiple
  27405. * causes. In general we want to catch obvious errors like a toLogString()
  27406. * throwing an error, but we don't currently try to catch every possible
  27407. * error. In particular, internal errors (like out of memory or stack) are
  27408. * not caught. Also, we expect Error toString() to not throw an error.
  27409. */
  27410. if (duk_is_object(ctx, i)) {
  27411. /* duk_pcall_prop() may itself throw an error, but we're content
  27412. * in catching the obvious errors (like toLogString() throwing an
  27413. * error).
  27414. */
  27415. duk_push_hstring_stridx(ctx, DUK_STRIDX_FMT);
  27416. duk_dup(ctx, i);
  27417. /* [ arg1 ... argN this loggerLevel loggerName 'fmt' arg ] */
  27418. /* call: this.fmt(arg) */
  27419. rc = duk_pcall_prop(ctx, -5 /*obj_index*/, 1 /*nargs*/);
  27420. if (rc) {
  27421. /* Keep the error as the result (coercing it might fail below,
  27422. * but we don't catch that now).
  27423. */
  27424. ;
  27425. }
  27426. duk_replace(ctx, i);
  27427. }
  27428. (void) duk_to_lstring(ctx, i, &arg_len);
  27429. tot_len++; /* sep (even before first one) */
  27430. tot_len += arg_len;
  27431. }
  27432. /*
  27433. * Pass 2
  27434. */
  27435. if (tot_len <= DUK_BI_LOGGER_SHORT_MSG_LIMIT) {
  27436. duk_hbuffer_dynamic *h_buf;
  27437. DUK_DDD(DUK_DDDPRINT("reuse existing small log message buffer, tot_len %ld", (long) tot_len));
  27438. /* We can assert for all buffer properties because user code
  27439. * never has access to heap->log_buffer.
  27440. */
  27441. DUK_ASSERT(thr != NULL);
  27442. DUK_ASSERT(thr->heap != NULL);
  27443. h_buf = thr->heap->log_buffer;
  27444. DUK_ASSERT(h_buf != NULL);
  27445. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) h_buf));
  27446. DUK_ASSERT(DUK_HBUFFER_DYNAMIC_GET_ALLOC_SIZE(h_buf) == DUK_BI_LOGGER_SHORT_MSG_LIMIT);
  27447. /* Set buffer 'visible size' to actual message length and
  27448. * push it to the stack.
  27449. */
  27450. DUK_HBUFFER_SET_SIZE((duk_hbuffer *) h_buf, tot_len);
  27451. duk_push_hbuffer(ctx, (duk_hbuffer *) h_buf);
  27452. buf = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h_buf);
  27453. } else {
  27454. DUK_DDD(DUK_DDDPRINT("use a one-off large log message buffer, tot_len %ld", (long) tot_len));
  27455. buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, tot_len);
  27456. }
  27457. DUK_ASSERT(buf != NULL);
  27458. p = buf;
  27459. DUK_MEMCPY((void *) p, (void *) date_buf, date_len);
  27460. p += date_len;
  27461. *p++ = (duk_uint8_t) DUK_ASC_SPACE;
  27462. q = duk__log_level_strings + (entry_lev * 3);
  27463. DUK_MEMCPY((void *) p, (void *) q, (duk_size_t) 3);
  27464. p += 3;
  27465. *p++ = (duk_uint8_t) DUK_ASC_SPACE;
  27466. arg_str = (const duk_uint8_t *) duk_get_lstring(ctx, -2, &arg_len);
  27467. DUK_MEMCPY((void *) p, (const void *) arg_str, arg_len);
  27468. p += arg_len;
  27469. *p++ = (duk_uint8_t) DUK_ASC_COLON;
  27470. for (i = 0; i < nargs; i++) {
  27471. *p++ = (duk_uint8_t) DUK_ASC_SPACE;
  27472. arg_str = (const duk_uint8_t *) duk_get_lstring(ctx, i, &arg_len);
  27473. DUK_ASSERT(arg_str != NULL);
  27474. DUK_MEMCPY((void *) p, (const void *) arg_str, arg_len);
  27475. p += arg_len;
  27476. }
  27477. DUK_ASSERT(buf + tot_len == p);
  27478. /* [ arg1 ... argN this loggerLevel loggerName buffer ] */
  27479. #if defined(DUK_USE_DEBUGGER_SUPPORT) && defined(DUK_USE_DEBUGGER_FWD_LOGGING)
  27480. /* Do debugger forwarding before raw() because the raw() function
  27481. * doesn't get the log level right now.
  27482. */
  27483. if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) {
  27484. const char *log_buf;
  27485. duk_size_t sz_buf;
  27486. log_buf = (const char *) duk_get_buffer(ctx, -1, &sz_buf);
  27487. DUK_ASSERT(log_buf != NULL);
  27488. duk_debug_write_notify(thr, DUK_DBG_CMD_LOG);
  27489. duk_debug_write_int(thr, (duk_int32_t) entry_lev);
  27490. duk_debug_write_string(thr, (const char *) log_buf, sz_buf);
  27491. duk_debug_write_eom(thr);
  27492. }
  27493. #endif
  27494. /* Call this.raw(msg); look up through the instance allows user to override
  27495. * the raw() function in the instance or in the prototype for maximum
  27496. * flexibility.
  27497. */
  27498. duk_push_hstring_stridx(ctx, DUK_STRIDX_RAW);
  27499. duk_dup(ctx, -2);
  27500. /* [ arg1 ... argN this loggerLevel loggerName buffer 'raw' buffer ] */
  27501. duk_call_prop(ctx, -6, 1); /* this.raw(buffer) */
  27502. return 0;
  27503. }
  27504. #line 1 "duk_bi_math.c"
  27505. /*
  27506. * Math built-ins
  27507. */
  27508. /* include removed: duk_internal.h */
  27509. #if defined(DUK_USE_MATH_BUILTIN)
  27510. /*
  27511. * Use static helpers which can work with math.h functions matching
  27512. * the following signatures. This is not portable if any of these math
  27513. * functions is actually a macro.
  27514. *
  27515. * Typing here is intentionally 'double' wherever values interact with
  27516. * the standard library APIs.
  27517. */
  27518. typedef double (*duk__one_arg_func)(double);
  27519. typedef double (*duk__two_arg_func)(double, double);
  27520. DUK_LOCAL duk_ret_t duk__math_minmax(duk_context *ctx, duk_double_t initial, duk__two_arg_func min_max) {
  27521. duk_idx_t n = duk_get_top(ctx);
  27522. duk_idx_t i;
  27523. duk_double_t res = initial;
  27524. duk_double_t t;
  27525. /*
  27526. * Note: fmax() does not match the E5 semantics. E5 requires
  27527. * that if -any- input to Math.max() is a NaN, the result is a
  27528. * NaN. fmax() will return a NaN only if -both- inputs are NaN.
  27529. * Same applies to fmin().
  27530. *
  27531. * Note: every input value must be coerced with ToNumber(), even
  27532. * if we know the result will be a NaN anyway: ToNumber() may have
  27533. * side effects for which even order of evaluation matters.
  27534. */
  27535. for (i = 0; i < n; i++) {
  27536. t = duk_to_number(ctx, i);
  27537. if (DUK_FPCLASSIFY(t) == DUK_FP_NAN || DUK_FPCLASSIFY(res) == DUK_FP_NAN) {
  27538. /* Note: not normalized, but duk_push_number() will normalize */
  27539. res = (duk_double_t) DUK_DOUBLE_NAN;
  27540. } else {
  27541. res = (duk_double_t) min_max(res, (double) t);
  27542. }
  27543. }
  27544. duk_push_number(ctx, res);
  27545. return 1;
  27546. }
  27547. DUK_LOCAL double duk__fmin_fixed(double x, double y) {
  27548. /* fmin() with args -0 and +0 is not guaranteed to return
  27549. * -0 as Ecmascript requires.
  27550. */
  27551. if (x == 0 && y == 0) {
  27552. /* XXX: what's the safest way of creating a negative zero? */
  27553. if (DUK_SIGNBIT(x) != 0 || DUK_SIGNBIT(y) != 0) {
  27554. return -0.0;
  27555. } else {
  27556. return +0.0;
  27557. }
  27558. }
  27559. #ifdef DUK_USE_MATH_FMIN
  27560. return DUK_FMIN(x, y);
  27561. #else
  27562. return (x < y ? x : y);
  27563. #endif
  27564. }
  27565. DUK_LOCAL double duk__fmax_fixed(double x, double y) {
  27566. /* fmax() with args -0 and +0 is not guaranteed to return
  27567. * +0 as Ecmascript requires.
  27568. */
  27569. if (x == 0 && y == 0) {
  27570. if (DUK_SIGNBIT(x) == 0 || DUK_SIGNBIT(y) == 0) {
  27571. return +0.0;
  27572. } else {
  27573. return -0.0;
  27574. }
  27575. }
  27576. #ifdef DUK_USE_MATH_FMAX
  27577. return DUK_FMAX(x, y);
  27578. #else
  27579. return (x > y ? x : y);
  27580. #endif
  27581. }
  27582. DUK_LOCAL double duk__round_fixed(double x) {
  27583. /* Numbers half-way between integers must be rounded towards +Infinity,
  27584. * e.g. -3.5 must be rounded to -3 (not -4). When rounded to zero, zero
  27585. * sign must be set appropriately. E5.1 Section 15.8.2.15.
  27586. *
  27587. * Note that ANSI C round() is "round to nearest integer, away from zero",
  27588. * which is incorrect for negative values. Here we make do with floor().
  27589. */
  27590. duk_small_int_t c = (duk_small_int_t) DUK_FPCLASSIFY(x);
  27591. if (c == DUK_FP_NAN || c == DUK_FP_INFINITE || c == DUK_FP_ZERO) {
  27592. return x;
  27593. }
  27594. /*
  27595. * x is finite and non-zero
  27596. *
  27597. * -1.6 -> floor(-1.1) -> -2
  27598. * -1.5 -> floor(-1.0) -> -1 (towards +Inf)
  27599. * -1.4 -> floor(-0.9) -> -1
  27600. * -0.5 -> -0.0 (special case)
  27601. * -0.1 -> -0.0 (special case)
  27602. * +0.1 -> +0.0 (special case)
  27603. * +0.5 -> floor(+1.0) -> 1 (towards +Inf)
  27604. * +1.4 -> floor(+1.9) -> 1
  27605. * +1.5 -> floor(+2.0) -> 2 (towards +Inf)
  27606. * +1.6 -> floor(+2.1) -> 2
  27607. */
  27608. if (x >= -0.5 && x < 0.5) {
  27609. /* +0.5 is handled by floor, this is on purpose */
  27610. if (x < 0.0) {
  27611. return -0.0;
  27612. } else {
  27613. return +0.0;
  27614. }
  27615. }
  27616. return DUK_FLOOR(x + 0.5);
  27617. }
  27618. DUK_LOCAL double duk__pow_fixed(double x, double y) {
  27619. /* The ANSI C pow() semantics differ from Ecmascript.
  27620. *
  27621. * E.g. when x==1 and y is +/- infinite, the Ecmascript required
  27622. * result is NaN, while at least Linux pow() returns 1.
  27623. */
  27624. duk_small_int_t cx, cy, sx;
  27625. DUK_UNREF(cx);
  27626. DUK_UNREF(sx);
  27627. cy = (duk_small_int_t) DUK_FPCLASSIFY(y);
  27628. if (cy == DUK_FP_NAN) {
  27629. goto ret_nan;
  27630. }
  27631. if (DUK_FABS(x) == 1.0 && cy == DUK_FP_INFINITE) {
  27632. goto ret_nan;
  27633. }
  27634. #if defined(DUK_USE_POW_NETBSD_WORKAROUND)
  27635. /* See test-bug-netbsd-math-pow.js: NetBSD 6.0 on x86 (at least) does not
  27636. * correctly handle some cases where x=+/-0. Specific fixes to these
  27637. * here.
  27638. */
  27639. cx = (duk_small_int_t) DUK_FPCLASSIFY(x);
  27640. if (cx == DUK_FP_ZERO && y < 0.0) {
  27641. sx = (duk_small_int_t) DUK_SIGNBIT(x);
  27642. if (sx == 0) {
  27643. /* Math.pow(+0,y) should be Infinity when y<0. NetBSD pow()
  27644. * returns -Infinity instead when y is <0 and finite. The
  27645. * if-clause also catches y == -Infinity (which works even
  27646. * without the fix).
  27647. */
  27648. return DUK_DOUBLE_INFINITY;
  27649. } else {
  27650. /* Math.pow(-0,y) where y<0 should be:
  27651. * - -Infinity if y<0 and an odd integer
  27652. * - Infinity otherwise
  27653. * NetBSD pow() returns -Infinity for all finite y<0. The
  27654. * if-clause also catches y == -Infinity (which works even
  27655. * without the fix).
  27656. */
  27657. /* fmod() return value has same sign as input (negative) so
  27658. * the result here will be in the range ]-2,0], 1 indicates
  27659. * odd. If x is -Infinity, NaN is returned and the odd check
  27660. * always concludes "not odd" which results in desired outcome.
  27661. */
  27662. double tmp = DUK_FMOD(y, 2);
  27663. if (tmp == -1.0) {
  27664. return -DUK_DOUBLE_INFINITY;
  27665. } else {
  27666. /* Not odd, or y == -Infinity */
  27667. return DUK_DOUBLE_INFINITY;
  27668. }
  27669. }
  27670. }
  27671. #endif
  27672. return DUK_POW(x, y);
  27673. ret_nan:
  27674. return DUK_DOUBLE_NAN;
  27675. }
  27676. /* Wrappers for calling standard math library methods. These may be required
  27677. * on platforms where one or more of the math built-ins are defined as macros
  27678. * or inline functions and are thus not suitable to be used as function pointers.
  27679. */
  27680. #if defined(DUK_USE_AVOID_PLATFORM_FUNCPTRS)
  27681. DUK_LOCAL double duk__fabs(double x) {
  27682. return DUK_FABS(x);
  27683. }
  27684. DUK_LOCAL double duk__acos(double x) {
  27685. return DUK_ACOS(x);
  27686. }
  27687. DUK_LOCAL double duk__asin(double x) {
  27688. return DUK_ASIN(x);
  27689. }
  27690. DUK_LOCAL double duk__atan(double x) {
  27691. return DUK_ATAN(x);
  27692. }
  27693. DUK_LOCAL double duk__ceil(double x) {
  27694. return DUK_CEIL(x);
  27695. }
  27696. DUK_LOCAL double duk__cos(double x) {
  27697. return DUK_COS(x);
  27698. }
  27699. DUK_LOCAL double duk__exp(double x) {
  27700. return DUK_EXP(x);
  27701. }
  27702. DUK_LOCAL double duk__floor(double x) {
  27703. return DUK_FLOOR(x);
  27704. }
  27705. DUK_LOCAL double duk__log(double x) {
  27706. return DUK_LOG(x);
  27707. }
  27708. DUK_LOCAL double duk__sin(double x) {
  27709. return DUK_SIN(x);
  27710. }
  27711. DUK_LOCAL double duk__sqrt(double x) {
  27712. return DUK_SQRT(x);
  27713. }
  27714. DUK_LOCAL double duk__tan(double x) {
  27715. return DUK_TAN(x);
  27716. }
  27717. DUK_LOCAL double duk__atan2(double x, double y) {
  27718. return DUK_ATAN2(x, y);
  27719. }
  27720. #endif /* DUK_USE_AVOID_PLATFORM_FUNCPTRS */
  27721. /* order must match constants in genbuiltins.py */
  27722. DUK_LOCAL const duk__one_arg_func duk__one_arg_funcs[] = {
  27723. #if defined(DUK_USE_AVOID_PLATFORM_FUNCPTRS)
  27724. duk__fabs,
  27725. duk__acos,
  27726. duk__asin,
  27727. duk__atan,
  27728. duk__ceil,
  27729. duk__cos,
  27730. duk__exp,
  27731. duk__floor,
  27732. duk__log,
  27733. duk__round_fixed,
  27734. duk__sin,
  27735. duk__sqrt,
  27736. duk__tan
  27737. #else
  27738. DUK_FABS,
  27739. DUK_ACOS,
  27740. DUK_ASIN,
  27741. DUK_ATAN,
  27742. DUK_CEIL,
  27743. DUK_COS,
  27744. DUK_EXP,
  27745. DUK_FLOOR,
  27746. DUK_LOG,
  27747. duk__round_fixed,
  27748. DUK_SIN,
  27749. DUK_SQRT,
  27750. DUK_TAN
  27751. #endif
  27752. };
  27753. /* order must match constants in genbuiltins.py */
  27754. DUK_LOCAL const duk__two_arg_func duk__two_arg_funcs[] = {
  27755. #if defined(DUK_USE_AVOID_PLATFORM_FUNCPTRS)
  27756. duk__atan2,
  27757. duk__pow_fixed
  27758. #else
  27759. DUK_ATAN2,
  27760. duk__pow_fixed
  27761. #endif
  27762. };
  27763. DUK_INTERNAL duk_ret_t duk_bi_math_object_onearg_shared(duk_context *ctx) {
  27764. duk_small_int_t fun_idx = duk_get_current_magic(ctx);
  27765. duk__one_arg_func fun;
  27766. DUK_ASSERT(fun_idx >= 0);
  27767. DUK_ASSERT(fun_idx < (duk_small_int_t) (sizeof(duk__one_arg_funcs) / sizeof(duk__one_arg_func)));
  27768. fun = duk__one_arg_funcs[fun_idx];
  27769. duk_push_number(ctx, (duk_double_t) fun((double) duk_to_number(ctx, 0)));
  27770. return 1;
  27771. }
  27772. DUK_INTERNAL duk_ret_t duk_bi_math_object_twoarg_shared(duk_context *ctx) {
  27773. duk_small_int_t fun_idx = duk_get_current_magic(ctx);
  27774. duk__two_arg_func fun;
  27775. DUK_ASSERT(fun_idx >= 0);
  27776. DUK_ASSERT(fun_idx < (duk_small_int_t) (sizeof(duk__two_arg_funcs) / sizeof(duk__two_arg_func)));
  27777. fun = duk__two_arg_funcs[fun_idx];
  27778. duk_push_number(ctx, (duk_double_t) fun((double) duk_to_number(ctx, 0), (double) duk_to_number(ctx, 1)));
  27779. return 1;
  27780. }
  27781. DUK_INTERNAL duk_ret_t duk_bi_math_object_max(duk_context *ctx) {
  27782. return duk__math_minmax(ctx, -DUK_DOUBLE_INFINITY, duk__fmax_fixed);
  27783. }
  27784. DUK_INTERNAL duk_ret_t duk_bi_math_object_min(duk_context *ctx) {
  27785. return duk__math_minmax(ctx, DUK_DOUBLE_INFINITY, duk__fmin_fixed);
  27786. }
  27787. DUK_INTERNAL duk_ret_t duk_bi_math_object_random(duk_context *ctx) {
  27788. duk_push_number(ctx, (duk_double_t) duk_util_tinyrandom_get_double((duk_hthread *) ctx));
  27789. return 1;
  27790. }
  27791. #else /* DUK_USE_MATH_BUILTIN */
  27792. /* A stubbed built-in is useful for e.g. compilation torture testing with BCC. */
  27793. DUK_INTERNAL duk_ret_t duk_bi_math_object_onearg_shared(duk_context *ctx) {
  27794. DUK_UNREF(ctx);
  27795. return DUK_RET_UNIMPLEMENTED_ERROR;
  27796. }
  27797. DUK_INTERNAL duk_ret_t duk_bi_math_object_twoarg_shared(duk_context *ctx) {
  27798. DUK_UNREF(ctx);
  27799. return DUK_RET_UNIMPLEMENTED_ERROR;
  27800. }
  27801. DUK_INTERNAL duk_ret_t duk_bi_math_object_max(duk_context *ctx) {
  27802. DUK_UNREF(ctx);
  27803. return DUK_RET_UNIMPLEMENTED_ERROR;
  27804. }
  27805. DUK_INTERNAL duk_ret_t duk_bi_math_object_min(duk_context *ctx) {
  27806. DUK_UNREF(ctx);
  27807. return DUK_RET_UNIMPLEMENTED_ERROR;
  27808. }
  27809. DUK_INTERNAL duk_ret_t duk_bi_math_object_random(duk_context *ctx) {
  27810. DUK_UNREF(ctx);
  27811. return DUK_RET_UNIMPLEMENTED_ERROR;
  27812. }
  27813. #endif /* DUK_USE_MATH_BUILTIN */
  27814. #line 1 "duk_bi_number.c"
  27815. /*
  27816. * Number built-ins
  27817. */
  27818. /* include removed: duk_internal.h */
  27819. DUK_LOCAL duk_double_t duk__push_this_number_plain(duk_context *ctx) {
  27820. duk_hobject *h;
  27821. /* Number built-in accepts a plain number or a Number object (whose
  27822. * internal value is operated on). Other types cause TypeError.
  27823. */
  27824. duk_push_this(ctx);
  27825. if (duk_is_number(ctx, -1)) {
  27826. DUK_DDD(DUK_DDDPRINT("plain number value: %!T", (duk_tval *) duk_get_tval(ctx, -1)));
  27827. goto done;
  27828. }
  27829. h = duk_get_hobject(ctx, -1);
  27830. if (!h ||
  27831. (DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_NUMBER)) {
  27832. DUK_DDD(DUK_DDDPRINT("unacceptable this value: %!T", (duk_tval *) duk_get_tval(ctx, -1)));
  27833. DUK_ERROR((duk_hthread *) ctx, DUK_ERR_TYPE_ERROR, "expected a number");
  27834. }
  27835. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE);
  27836. DUK_ASSERT(duk_is_number(ctx, -1));
  27837. DUK_DDD(DUK_DDDPRINT("number object: %!T, internal value: %!T",
  27838. (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1)));
  27839. duk_remove(ctx, -2);
  27840. done:
  27841. return duk_get_number(ctx, -1);
  27842. }
  27843. DUK_INTERNAL duk_ret_t duk_bi_number_constructor(duk_context *ctx) {
  27844. duk_hthread *thr = (duk_hthread *) ctx;
  27845. duk_idx_t nargs;
  27846. duk_hobject *h_this;
  27847. DUK_UNREF(thr);
  27848. /*
  27849. * The Number constructor uses ToNumber(arg) for number coercion
  27850. * (coercing an undefined argument to NaN). However, if the
  27851. * argument is not given at all, +0 must be used instead. To do
  27852. * this, a vararg function is used.
  27853. */
  27854. nargs = duk_get_top(ctx);
  27855. if (nargs == 0) {
  27856. duk_push_int(ctx, 0);
  27857. }
  27858. duk_to_number(ctx, 0);
  27859. duk_set_top(ctx, 1);
  27860. DUK_ASSERT_TOP(ctx, 1);
  27861. if (!duk_is_constructor_call(ctx)) {
  27862. return 1;
  27863. }
  27864. /*
  27865. * E5 Section 15.7.2.1 requires that the constructed object
  27866. * must have the original Number.prototype as its internal
  27867. * prototype. However, since Number.prototype is non-writable
  27868. * and non-configurable, this doesn't have to be enforced here:
  27869. * The default object (bound to 'this') is OK, though we have
  27870. * to change its class.
  27871. *
  27872. * Internal value set to ToNumber(arg) or +0; if no arg given,
  27873. * ToNumber(undefined) = NaN, so special treatment is needed
  27874. * (above). String internal value is immutable.
  27875. */
  27876. /* XXX: helper */
  27877. duk_push_this(ctx);
  27878. h_this = duk_get_hobject(ctx, -1);
  27879. DUK_ASSERT(h_this != NULL);
  27880. DUK_HOBJECT_SET_CLASS_NUMBER(h_this, DUK_HOBJECT_CLASS_NUMBER);
  27881. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_this) == thr->builtins[DUK_BIDX_NUMBER_PROTOTYPE]);
  27882. DUK_ASSERT(DUK_HOBJECT_GET_CLASS_NUMBER(h_this) == DUK_HOBJECT_CLASS_NUMBER);
  27883. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(h_this));
  27884. duk_dup(ctx, 0); /* -> [ val obj val ] */
  27885. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE);
  27886. return 0; /* no return value -> don't replace created value */
  27887. }
  27888. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_value_of(duk_context *ctx) {
  27889. (void) duk__push_this_number_plain(ctx);
  27890. return 1;
  27891. }
  27892. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_string(duk_context *ctx) {
  27893. duk_small_int_t radix;
  27894. duk_small_uint_t n2s_flags;
  27895. (void) duk__push_this_number_plain(ctx);
  27896. if (duk_is_undefined(ctx, 0)) {
  27897. radix = 10;
  27898. } else {
  27899. radix = (duk_small_int_t) duk_to_int_check_range(ctx, 0, 2, 36);
  27900. }
  27901. DUK_DDD(DUK_DDDPRINT("radix=%ld", (long) radix));
  27902. n2s_flags = 0;
  27903. duk_numconv_stringify(ctx,
  27904. radix /*radix*/,
  27905. 0 /*digits*/,
  27906. n2s_flags /*flags*/);
  27907. return 1;
  27908. }
  27909. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_locale_string(duk_context *ctx) {
  27910. /* XXX: just use toString() for now; permitted although not recommended.
  27911. * nargs==1, so radix is passed to toString().
  27912. */
  27913. return duk_bi_number_prototype_to_string(ctx);
  27914. }
  27915. /*
  27916. * toFixed(), toExponential(), toPrecision()
  27917. */
  27918. /* XXX: shared helper for toFixed(), toExponential(), toPrecision()? */
  27919. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_fixed(duk_context *ctx) {
  27920. duk_small_int_t frac_digits;
  27921. duk_double_t d;
  27922. duk_small_int_t c;
  27923. duk_small_uint_t n2s_flags;
  27924. frac_digits = (duk_small_int_t) duk_to_int_check_range(ctx, 0, 0, 20);
  27925. d = duk__push_this_number_plain(ctx);
  27926. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  27927. if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) {
  27928. goto use_to_string;
  27929. }
  27930. if (d >= 1.0e21 || d <= -1.0e21) {
  27931. goto use_to_string;
  27932. }
  27933. n2s_flags = DUK_N2S_FLAG_FIXED_FORMAT |
  27934. DUK_N2S_FLAG_FRACTION_DIGITS;
  27935. duk_numconv_stringify(ctx,
  27936. 10 /*radix*/,
  27937. frac_digits /*digits*/,
  27938. n2s_flags /*flags*/);
  27939. return 1;
  27940. use_to_string:
  27941. DUK_ASSERT_TOP(ctx, 2);
  27942. duk_to_string(ctx, -1);
  27943. return 1;
  27944. }
  27945. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_exponential(duk_context *ctx) {
  27946. duk_bool_t frac_undefined;
  27947. duk_small_int_t frac_digits;
  27948. duk_double_t d;
  27949. duk_small_int_t c;
  27950. duk_small_uint_t n2s_flags;
  27951. d = duk__push_this_number_plain(ctx);
  27952. frac_undefined = duk_is_undefined(ctx, 0);
  27953. duk_to_int(ctx, 0); /* for side effects */
  27954. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  27955. if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) {
  27956. goto use_to_string;
  27957. }
  27958. frac_digits = (duk_small_int_t) duk_to_int_check_range(ctx, 0, 0, 20);
  27959. n2s_flags = DUK_N2S_FLAG_FORCE_EXP |
  27960. (frac_undefined ? 0 : DUK_N2S_FLAG_FIXED_FORMAT);
  27961. duk_numconv_stringify(ctx,
  27962. 10 /*radix*/,
  27963. frac_digits + 1 /*leading digit + fractions*/,
  27964. n2s_flags /*flags*/);
  27965. return 1;
  27966. use_to_string:
  27967. DUK_ASSERT_TOP(ctx, 2);
  27968. duk_to_string(ctx, -1);
  27969. return 1;
  27970. }
  27971. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_precision(duk_context *ctx) {
  27972. /* The specification has quite awkward order of coercion and
  27973. * checks for toPrecision(). The operations below are a bit
  27974. * reordered, within constraints of observable side effects.
  27975. */
  27976. duk_double_t d;
  27977. duk_small_int_t prec;
  27978. duk_small_int_t c;
  27979. duk_small_uint_t n2s_flags;
  27980. DUK_ASSERT_TOP(ctx, 1);
  27981. d = duk__push_this_number_plain(ctx);
  27982. if (duk_is_undefined(ctx, 0)) {
  27983. goto use_to_string;
  27984. }
  27985. DUK_ASSERT_TOP(ctx, 2);
  27986. duk_to_int(ctx, 0); /* for side effects */
  27987. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  27988. if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) {
  27989. goto use_to_string;
  27990. }
  27991. prec = (duk_small_int_t) duk_to_int_check_range(ctx, 0, 1, 21);
  27992. n2s_flags = DUK_N2S_FLAG_FIXED_FORMAT |
  27993. DUK_N2S_FLAG_NO_ZERO_PAD;
  27994. duk_numconv_stringify(ctx,
  27995. 10 /*radix*/,
  27996. prec /*digits*/,
  27997. n2s_flags /*flags*/);
  27998. return 1;
  27999. use_to_string:
  28000. /* Used when precision is undefined; also used for NaN (-> "NaN"),
  28001. * and +/- infinity (-> "Infinity", "-Infinity").
  28002. */
  28003. DUK_ASSERT_TOP(ctx, 2);
  28004. duk_to_string(ctx, -1);
  28005. return 1;
  28006. }
  28007. #line 1 "duk_bi_object.c"
  28008. /*
  28009. * Object built-ins
  28010. */
  28011. /* include removed: duk_internal.h */
  28012. DUK_INTERNAL duk_ret_t duk_bi_object_constructor(duk_context *ctx) {
  28013. if (!duk_is_constructor_call(ctx) &&
  28014. !duk_is_null_or_undefined(ctx, 0)) {
  28015. duk_to_object(ctx, 0);
  28016. return 1;
  28017. }
  28018. if (duk_is_object(ctx, 0)) {
  28019. return 1;
  28020. }
  28021. /* Pointer and buffer primitive values are treated like other
  28022. * primitives values which have a fully fledged object counterpart:
  28023. * promote to an object value. Lightfuncs are coerced with
  28024. * ToObject() even they could also be returned as is.
  28025. */
  28026. if (duk_check_type_mask(ctx, 0, DUK_TYPE_MASK_STRING |
  28027. DUK_TYPE_MASK_BOOLEAN |
  28028. DUK_TYPE_MASK_NUMBER |
  28029. DUK_TYPE_MASK_POINTER |
  28030. DUK_TYPE_MASK_BUFFER |
  28031. DUK_TYPE_MASK_LIGHTFUNC)) {
  28032. duk_to_object(ctx, 0);
  28033. return 1;
  28034. }
  28035. duk_push_object_helper(ctx,
  28036. DUK_HOBJECT_FLAG_EXTENSIBLE |
  28037. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  28038. DUK_BIDX_OBJECT_PROTOTYPE);
  28039. return 1;
  28040. }
  28041. /* Shared helper to implement Object.getPrototypeOf and the ES6
  28042. * Object.prototype.__proto__ getter.
  28043. *
  28044. * https://people.mozilla.org/~jorendorff/es6-draft.html#sec-get-object.prototype.__proto__
  28045. */
  28046. DUK_INTERNAL duk_ret_t duk_bi_object_getprototype_shared(duk_context *ctx) {
  28047. duk_hthread *thr = (duk_hthread *) ctx;
  28048. duk_hobject *h;
  28049. duk_hobject *proto;
  28050. DUK_UNREF(thr);
  28051. /* magic: 0=getter call, 1=Object.getPrototypeOf */
  28052. if (duk_get_current_magic(ctx) == 0) {
  28053. duk_push_this_coercible_to_object(ctx);
  28054. duk_insert(ctx, 0);
  28055. }
  28056. h = duk_require_hobject_or_lfunc(ctx, 0);
  28057. /* h is NULL for lightfunc */
  28058. /* XXX: should the API call handle this directly, i.e. attempt
  28059. * to duk_push_hobject(ctx, null) would push a null instead?
  28060. * (On the other hand 'undefined' would be just as logical, but
  28061. * not wanted here.)
  28062. */
  28063. if (h == NULL) {
  28064. duk_push_hobject_bidx(ctx, DUK_BIDX_FUNCTION_PROTOTYPE);
  28065. } else {
  28066. proto = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
  28067. if (proto) {
  28068. duk_push_hobject(ctx, proto);
  28069. } else {
  28070. duk_push_null(ctx);
  28071. }
  28072. }
  28073. return 1;
  28074. }
  28075. /* Shared helper to implement ES6 Object.setPrototypeOf and
  28076. * Object.prototype.__proto__ setter.
  28077. *
  28078. * https://people.mozilla.org/~jorendorff/es6-draft.html#sec-get-object.prototype.__proto__
  28079. * https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.setprototypeof
  28080. */
  28081. DUK_INTERNAL duk_ret_t duk_bi_object_setprototype_shared(duk_context *ctx) {
  28082. duk_hthread *thr = (duk_hthread *) ctx;
  28083. duk_hobject *h_obj;
  28084. duk_hobject *h_new_proto;
  28085. duk_hobject *h_curr;
  28086. duk_ret_t ret_success = 1; /* retval for success path */
  28087. /* Preliminaries for __proto__ and setPrototypeOf (E6 19.1.2.18 steps 1-4);
  28088. * magic: 0=setter call, 1=Object.setPrototypeOf
  28089. */
  28090. if (duk_get_current_magic(ctx) == 0) {
  28091. duk_push_this_check_object_coercible(ctx);
  28092. duk_insert(ctx, 0);
  28093. if (!duk_check_type_mask(ctx, 1, DUK_TYPE_MASK_NULL | DUK_TYPE_MASK_OBJECT)) {
  28094. return 0;
  28095. }
  28096. /* __proto__ setter returns 'undefined' on success unlike the
  28097. * setPrototypeOf() call which returns the target object.
  28098. */
  28099. ret_success = 0;
  28100. } else {
  28101. duk_require_object_coercible(ctx, 0);
  28102. duk_require_type_mask(ctx, 1, DUK_TYPE_MASK_NULL | DUK_TYPE_MASK_OBJECT);
  28103. }
  28104. h_new_proto = duk_get_hobject(ctx, 1);
  28105. /* h_new_proto may be NULL */
  28106. if (duk_is_lightfunc(ctx, 0)) {
  28107. if (h_new_proto == thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]) {
  28108. goto skip;
  28109. }
  28110. goto fail_nonextensible;
  28111. }
  28112. h_obj = duk_get_hobject(ctx, 0);
  28113. if (!h_obj) {
  28114. goto skip;
  28115. }
  28116. DUK_ASSERT(h_obj != NULL);
  28117. /* [[SetPrototypeOf]] standard behavior, E6 9.1.2 */
  28118. /* NOTE: steps 7-8 seem to be a cut-paste bug in the E6 draft */
  28119. /* TODO: implement Proxy object support here */
  28120. if (h_new_proto == DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_obj)) {
  28121. goto skip;
  28122. }
  28123. if (!DUK_HOBJECT_HAS_EXTENSIBLE(h_obj)) {
  28124. goto fail_nonextensible;
  28125. }
  28126. for (h_curr = h_new_proto; h_curr != NULL; h_curr = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_curr)) {
  28127. /* Loop prevention */
  28128. if (h_curr == h_obj) {
  28129. goto fail_loop;
  28130. }
  28131. }
  28132. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h_obj, h_new_proto);
  28133. /* fall thru */
  28134. skip:
  28135. duk_set_top(ctx, 1);
  28136. return ret_success;
  28137. fail_nonextensible:
  28138. fail_loop:
  28139. return DUK_RET_TYPE_ERROR;
  28140. }
  28141. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_get_own_property_descriptor(duk_context *ctx) {
  28142. /* XXX: no need for indirect call */
  28143. return duk_hobject_object_get_own_property_descriptor(ctx);
  28144. }
  28145. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_create(duk_context *ctx) {
  28146. duk_tval *tv;
  28147. duk_hobject *proto = NULL;
  28148. DUK_ASSERT_TOP(ctx, 2);
  28149. tv = duk_get_tval(ctx, 0);
  28150. DUK_ASSERT(tv != NULL);
  28151. if (DUK_TVAL_IS_NULL(tv)) {
  28152. ;
  28153. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  28154. proto = DUK_TVAL_GET_OBJECT(tv);
  28155. DUK_ASSERT(proto != NULL);
  28156. } else {
  28157. return DUK_RET_TYPE_ERROR;
  28158. }
  28159. (void) duk_push_object_helper_proto(ctx,
  28160. DUK_HOBJECT_FLAG_EXTENSIBLE |
  28161. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  28162. proto);
  28163. if (!duk_is_undefined(ctx, 1)) {
  28164. /* [ O Properties obj ] */
  28165. duk_replace(ctx, 0);
  28166. /* [ obj Properties ] */
  28167. /* Just call the "original" Object.defineProperties() to
  28168. * finish up.
  28169. */
  28170. return duk_bi_object_constructor_define_properties(ctx);
  28171. }
  28172. /* [ O Properties obj ] */
  28173. return 1;
  28174. }
  28175. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_define_property(duk_context *ctx) {
  28176. duk_hobject *obj;
  28177. duk_hstring *key;
  28178. duk_hobject *get;
  28179. duk_hobject *set;
  28180. duk_idx_t idx_value;
  28181. duk_uint_t defprop_flags;
  28182. DUK_ASSERT(ctx != NULL);
  28183. DUK_DDD(DUK_DDDPRINT("Object.defineProperty(): ctx=%p obj=%!T key=%!T desc=%!T",
  28184. (void *) ctx,
  28185. (duk_tval *) duk_get_tval(ctx, 0),
  28186. (duk_tval *) duk_get_tval(ctx, 1),
  28187. (duk_tval *) duk_get_tval(ctx, 2)));
  28188. /* [ obj key desc ] */
  28189. /* Lightfuncs are currently supported by coercing to a temporary
  28190. * Function object; changes will be allowed (the coerced value is
  28191. * extensible) but will be lost.
  28192. */
  28193. obj = duk_require_hobject_or_lfunc_coerce(ctx, 0);
  28194. (void) duk_to_string(ctx, 1);
  28195. key = duk_require_hstring(ctx, 1);
  28196. (void) duk_require_hobject(ctx, 2);
  28197. DUK_ASSERT(obj != NULL);
  28198. DUK_ASSERT(key != NULL);
  28199. DUK_ASSERT(duk_get_hobject(ctx, 2) != NULL);
  28200. /*
  28201. * Validate and convert argument property descriptor (an Ecmascript
  28202. * object) into a set of defprop_flags and possibly property value,
  28203. * getter, and/or setter values on the value stack.
  28204. *
  28205. * Lightfunc set/get values are coerced to full Functions.
  28206. */
  28207. duk_hobject_prepare_property_descriptor(ctx,
  28208. 2 /*idx_desc*/,
  28209. &defprop_flags,
  28210. &idx_value,
  28211. &get,
  28212. &set);
  28213. /*
  28214. * Use Object.defineProperty() helper for the actual operation.
  28215. */
  28216. duk_hobject_define_property_helper(ctx,
  28217. defprop_flags,
  28218. obj,
  28219. key,
  28220. idx_value,
  28221. get,
  28222. set);
  28223. /* Ignore the normalize/validate helper outputs on the value stack,
  28224. * they're popped automatically.
  28225. */
  28226. /*
  28227. * Return target object.
  28228. */
  28229. duk_push_hobject(ctx, obj);
  28230. return 1;
  28231. }
  28232. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_define_properties(duk_context *ctx) {
  28233. duk_small_uint_t pass;
  28234. duk_uint_t defprop_flags;
  28235. duk_hobject *obj;
  28236. duk_idx_t idx_value;
  28237. duk_hobject *get;
  28238. duk_hobject *set;
  28239. /* Lightfunc handling by ToObject() coercion. */
  28240. obj = duk_require_hobject_or_lfunc_coerce(ctx, 0); /* target */
  28241. DUK_ASSERT(obj != NULL);
  28242. duk_to_object(ctx, 1); /* properties object */
  28243. DUK_DDD(DUK_DDDPRINT("target=%!iT, properties=%!iT",
  28244. (duk_tval *) duk_get_tval(ctx, 0),
  28245. (duk_tval *) duk_get_tval(ctx, 1)));
  28246. /*
  28247. * Two pass approach to processing the property descriptors.
  28248. * On first pass validate and normalize all descriptors before
  28249. * any changes are made to the target object. On second pass
  28250. * make the actual modifications to the target object.
  28251. *
  28252. * Right now we'll just use the same normalize/validate helper
  28253. * on both passes, ignoring its outputs on the first pass.
  28254. */
  28255. for (pass = 0; pass < 2; pass++) {
  28256. duk_set_top(ctx, 2); /* -> [ hobject props ] */
  28257. duk_enum(ctx, 1, DUK_ENUM_OWN_PROPERTIES_ONLY /*enum_flags*/);
  28258. for (;;) {
  28259. duk_hstring *key;
  28260. /* [ hobject props enum(props) ] */
  28261. duk_set_top(ctx, 3);
  28262. if (!duk_next(ctx, 2, 1 /*get_value*/)) {
  28263. break;
  28264. }
  28265. DUK_DDD(DUK_DDDPRINT("-> key=%!iT, desc=%!iT",
  28266. (duk_tval *) duk_get_tval(ctx, -2),
  28267. (duk_tval *) duk_get_tval(ctx, -1)));
  28268. /* [ hobject props enum(props) key desc ] */
  28269. duk_hobject_prepare_property_descriptor(ctx,
  28270. 4 /*idx_desc*/,
  28271. &defprop_flags,
  28272. &idx_value,
  28273. &get,
  28274. &set);
  28275. /* [ hobject props enum(props) key desc value? getter? setter? ] */
  28276. if (pass == 0) {
  28277. continue;
  28278. }
  28279. key = duk_get_hstring(ctx, 3);
  28280. DUK_ASSERT(key != NULL);
  28281. duk_hobject_define_property_helper(ctx,
  28282. defprop_flags,
  28283. obj,
  28284. key,
  28285. idx_value,
  28286. get,
  28287. set);
  28288. }
  28289. }
  28290. /*
  28291. * Return target object
  28292. */
  28293. duk_dup(ctx, 0);
  28294. return 1;
  28295. }
  28296. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_seal_freeze_shared(duk_context *ctx) {
  28297. duk_hthread *thr = (duk_hthread *) ctx;
  28298. duk_hobject *h;
  28299. duk_bool_t is_freeze;
  28300. h = duk_require_hobject_or_lfunc(ctx, 0);
  28301. if (!h) {
  28302. /* Lightfunc, always success. */
  28303. return 1;
  28304. }
  28305. is_freeze = (duk_bool_t) duk_get_current_magic(ctx);
  28306. duk_hobject_object_seal_freeze_helper(thr, h, is_freeze);
  28307. /* Sealed and frozen objects cannot gain any more properties,
  28308. * so this is a good time to compact them.
  28309. */
  28310. duk_hobject_compact_props(thr, h);
  28311. return 1;
  28312. }
  28313. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_prevent_extensions(duk_context *ctx) {
  28314. duk_hthread *thr = (duk_hthread *) ctx;
  28315. duk_hobject *h;
  28316. h = duk_require_hobject_or_lfunc(ctx, 0);
  28317. if (!h) {
  28318. /* Lightfunc, always success. */
  28319. return 1;
  28320. }
  28321. DUK_ASSERT(h != NULL);
  28322. DUK_HOBJECT_CLEAR_EXTENSIBLE(h);
  28323. /* A non-extensible object cannot gain any more properties,
  28324. * so this is a good time to compact.
  28325. */
  28326. duk_hobject_compact_props(thr, h);
  28327. return 1;
  28328. }
  28329. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_is_sealed_frozen_shared(duk_context *ctx) {
  28330. duk_hobject *h;
  28331. duk_bool_t is_frozen;
  28332. duk_bool_t rc;
  28333. h = duk_require_hobject_or_lfunc(ctx, 0);
  28334. if (!h) {
  28335. duk_push_true(ctx); /* frozen and sealed */
  28336. } else {
  28337. is_frozen = duk_get_current_magic(ctx);
  28338. rc = duk_hobject_object_is_sealed_frozen_helper((duk_hthread *) ctx, h, is_frozen /*is_frozen*/);
  28339. duk_push_boolean(ctx, rc);
  28340. }
  28341. return 1;
  28342. }
  28343. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_is_extensible(duk_context *ctx) {
  28344. duk_hobject *h;
  28345. h = duk_require_hobject_or_lfunc(ctx, 0);
  28346. if (!h) {
  28347. duk_push_false(ctx);
  28348. } else {
  28349. duk_push_boolean(ctx, DUK_HOBJECT_HAS_EXTENSIBLE(h));
  28350. }
  28351. return 1;
  28352. }
  28353. /* Shared helper for Object.getOwnPropertyNames() and Object.keys().
  28354. * Magic: 0=getOwnPropertyNames, 1=Object.keys.
  28355. */
  28356. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_keys_shared(duk_context *ctx) {
  28357. duk_hthread *thr = (duk_hthread *) ctx;
  28358. duk_hobject *obj;
  28359. #if defined(DUK_USE_ES6_PROXY)
  28360. duk_hobject *h_proxy_target;
  28361. duk_hobject *h_proxy_handler;
  28362. duk_hobject *h_trap_result;
  28363. duk_uarridx_t i, len, idx;
  28364. #endif
  28365. duk_small_uint_t enum_flags;
  28366. DUK_ASSERT_TOP(ctx, 1);
  28367. DUK_UNREF(thr);
  28368. obj = duk_require_hobject_or_lfunc_coerce(ctx, 0);
  28369. DUK_ASSERT(obj != NULL);
  28370. DUK_UNREF(obj);
  28371. #if defined(DUK_USE_ES6_PROXY)
  28372. if (DUK_LIKELY(!duk_hobject_proxy_check(thr,
  28373. obj,
  28374. &h_proxy_target,
  28375. &h_proxy_handler))) {
  28376. goto skip_proxy;
  28377. }
  28378. duk_push_hobject(ctx, h_proxy_handler);
  28379. if (!duk_get_prop_stridx(ctx, -1, DUK_STRIDX_OWN_KEYS)) {
  28380. /* Careful with reachability here: don't pop 'obj' before pushing
  28381. * proxy target.
  28382. */
  28383. DUK_DDD(DUK_DDDPRINT("no ownKeys trap, get keys of target instead"));
  28384. duk_pop_2(ctx);
  28385. duk_push_hobject(ctx, h_proxy_target);
  28386. duk_replace(ctx, 0);
  28387. DUK_ASSERT_TOP(ctx, 1);
  28388. goto skip_proxy;
  28389. }
  28390. /* [ obj handler trap ] */
  28391. duk_insert(ctx, -2);
  28392. duk_push_hobject(ctx, h_proxy_target); /* -> [ obj trap handler target ] */
  28393. duk_call_method(ctx, 1 /*nargs*/); /* -> [ obj trap_result ] */
  28394. h_trap_result = duk_require_hobject(ctx, -1);
  28395. DUK_UNREF(h_trap_result);
  28396. len = (duk_uarridx_t) duk_get_length(ctx, -1);
  28397. idx = 0;
  28398. duk_push_array(ctx);
  28399. for (i = 0; i < len; i++) {
  28400. /* [ obj trap_result res_arr ] */
  28401. if (duk_get_prop_index(ctx, -2, i) && duk_is_string(ctx, -1)) {
  28402. /* XXX: for Object.keys() we should check enumerability of key */
  28403. /* [ obj trap_result res_arr propname ] */
  28404. duk_put_prop_index(ctx, -2, idx);
  28405. idx++;
  28406. } else {
  28407. duk_pop(ctx);
  28408. }
  28409. }
  28410. /* XXX: for Object.keys() the [[OwnPropertyKeys]] result (trap result)
  28411. * should be filtered so that only enumerable keys remain. Enumerability
  28412. * should be checked with [[GetOwnProperty]] on the original object
  28413. * (i.e., the proxy in this case). If the proxy has a getOwnPropertyDescriptor
  28414. * trap, it should be triggered for every property. If the proxy doesn't have
  28415. * the trap, enumerability should be checked against the target object instead.
  28416. * We don't do any of this now, so Object.keys() and Object.getOwnPropertyNames()
  28417. * return the same result now for proxy traps. We still do clean up the trap
  28418. * result, so that Object.keys() and Object.getOwnPropertyNames() will return a
  28419. * clean array of strings without gaps.
  28420. */
  28421. return 1;
  28422. skip_proxy:
  28423. #endif /* DUK_USE_ES6_PROXY */
  28424. DUK_ASSERT_TOP(ctx, 1);
  28425. if (duk_get_current_magic(ctx)) {
  28426. /* Object.keys */
  28427. enum_flags = DUK_ENUM_OWN_PROPERTIES_ONLY |
  28428. DUK_ENUM_NO_PROXY_BEHAVIOR;
  28429. } else {
  28430. /* Object.getOwnPropertyNames */
  28431. enum_flags = DUK_ENUM_INCLUDE_NONENUMERABLE |
  28432. DUK_ENUM_OWN_PROPERTIES_ONLY |
  28433. DUK_ENUM_NO_PROXY_BEHAVIOR;
  28434. }
  28435. return duk_hobject_get_enumerated_keys(ctx, enum_flags);
  28436. }
  28437. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_to_string(duk_context *ctx) {
  28438. duk_hthread *thr = (duk_hthread *) ctx;
  28439. duk_push_this(ctx);
  28440. duk_push_string(ctx, "[object ");
  28441. if (duk_is_undefined(ctx, -2)) {
  28442. duk_push_hstring_stridx(ctx, DUK_STRIDX_UC_UNDEFINED);
  28443. } else if (duk_is_null(ctx, -2)) {
  28444. duk_push_hstring_stridx(ctx, DUK_STRIDX_UC_NULL);
  28445. } else {
  28446. duk_hobject *h_this;
  28447. duk_hstring *h_classname;
  28448. duk_to_object(ctx, -2);
  28449. h_this = duk_get_hobject(ctx, -2);
  28450. DUK_ASSERT(h_this != NULL);
  28451. h_classname = DUK_HOBJECT_GET_CLASS_STRING(thr->heap, h_this);
  28452. DUK_ASSERT(h_classname != NULL);
  28453. duk_push_hstring(ctx, h_classname);
  28454. }
  28455. duk_push_string(ctx, "]");
  28456. duk_concat(ctx, 3);
  28457. return 1;
  28458. }
  28459. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_to_locale_string(duk_context *ctx) {
  28460. DUK_ASSERT_TOP(ctx, 0);
  28461. (void) duk_push_this_coercible_to_object(ctx);
  28462. duk_get_prop_stridx(ctx, 0, DUK_STRIDX_TO_STRING);
  28463. if (!duk_is_callable(ctx, 1)) {
  28464. return DUK_RET_TYPE_ERROR;
  28465. }
  28466. duk_dup(ctx, 0); /* -> [ O toString O ] */
  28467. duk_call_method(ctx, 0); /* XXX: call method tailcall? */
  28468. return 1;
  28469. }
  28470. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_value_of(duk_context *ctx) {
  28471. (void) duk_push_this_coercible_to_object(ctx);
  28472. return 1;
  28473. }
  28474. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_is_prototype_of(duk_context *ctx) {
  28475. duk_hthread *thr = (duk_hthread *) ctx;
  28476. duk_hobject *h_v;
  28477. duk_hobject *h_obj;
  28478. DUK_ASSERT_TOP(ctx, 1);
  28479. h_v = duk_get_hobject(ctx, 0);
  28480. if (!h_v) {
  28481. duk_push_false(ctx); /* XXX: tail call: return duk_push_false(ctx) */
  28482. return 1;
  28483. }
  28484. h_obj = duk_push_this_coercible_to_object(ctx);
  28485. DUK_ASSERT(h_obj != NULL);
  28486. /* E5.1 Section 15.2.4.6, step 3.a, lookup proto once before compare.
  28487. * Prototype loops should cause an error to be thrown.
  28488. */
  28489. duk_push_boolean(ctx, duk_hobject_prototype_chain_contains(thr, DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_v), h_obj, 0 /*ignore_loop*/));
  28490. return 1;
  28491. }
  28492. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_has_own_property(duk_context *ctx) {
  28493. return duk_hobject_object_ownprop_helper(ctx, 0 /*required_desc_flags*/);
  28494. }
  28495. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_property_is_enumerable(duk_context *ctx) {
  28496. return duk_hobject_object_ownprop_helper(ctx, DUK_PROPDESC_FLAG_ENUMERABLE /*required_desc_flags*/);
  28497. }
  28498. #line 1 "duk_bi_pointer.c"
  28499. /*
  28500. * Pointer built-ins
  28501. */
  28502. /* include removed: duk_internal.h */
  28503. /*
  28504. * Constructor
  28505. */
  28506. DUK_INTERNAL duk_ret_t duk_bi_pointer_constructor(duk_context *ctx) {
  28507. /* XXX: this behavior is quite useless now; it would be nice to be able
  28508. * to create pointer values from e.g. numbers or strings. Numbers are
  28509. * problematic on 64-bit platforms though. Hex encoded strings?
  28510. */
  28511. if (duk_get_top(ctx) == 0) {
  28512. duk_push_pointer(ctx, NULL);
  28513. } else {
  28514. duk_to_pointer(ctx, 0);
  28515. }
  28516. DUK_ASSERT(duk_is_pointer(ctx, 0));
  28517. duk_set_top(ctx, 1);
  28518. if (duk_is_constructor_call(ctx)) {
  28519. duk_push_object_helper(ctx,
  28520. DUK_HOBJECT_FLAG_EXTENSIBLE |
  28521. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_POINTER),
  28522. DUK_BIDX_POINTER_PROTOTYPE);
  28523. /* Pointer object internal value is immutable */
  28524. duk_dup(ctx, 0);
  28525. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE);
  28526. }
  28527. /* Note: unbalanced stack on purpose */
  28528. return 1;
  28529. }
  28530. /*
  28531. * toString(), valueOf()
  28532. */
  28533. DUK_INTERNAL duk_ret_t duk_bi_pointer_prototype_tostring_shared(duk_context *ctx) {
  28534. duk_tval *tv;
  28535. duk_small_int_t to_string = duk_get_current_magic(ctx);
  28536. duk_push_this(ctx);
  28537. tv = duk_require_tval(ctx, -1);
  28538. DUK_ASSERT(tv != NULL);
  28539. if (DUK_TVAL_IS_POINTER(tv)) {
  28540. /* nop */
  28541. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  28542. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  28543. DUK_ASSERT(h != NULL);
  28544. /* Must be a "pointer object", i.e. class "Pointer" */
  28545. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_POINTER) {
  28546. goto type_error;
  28547. }
  28548. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE);
  28549. } else {
  28550. goto type_error;
  28551. }
  28552. if (to_string) {
  28553. duk_to_string(ctx, -1);
  28554. }
  28555. return 1;
  28556. type_error:
  28557. return DUK_RET_TYPE_ERROR;
  28558. }
  28559. #line 1 "duk_bi_proxy.c"
  28560. /*
  28561. * Proxy built-in (ES6 draft)
  28562. */
  28563. /* include removed: duk_internal.h */
  28564. #if defined(DUK_USE_ES6_PROXY)
  28565. DUK_INTERNAL duk_ret_t duk_bi_proxy_constructor(duk_context *ctx) {
  28566. duk_hobject *h_target;
  28567. duk_hobject *h_handler;
  28568. if (!duk_is_constructor_call(ctx)) {
  28569. return DUK_RET_TYPE_ERROR;
  28570. }
  28571. /* Reject a proxy object as the target because it would need
  28572. * special handler in property lookups. (ES6 has no such restriction)
  28573. */
  28574. h_target = duk_require_hobject_or_lfunc_coerce(ctx, 0);
  28575. DUK_ASSERT(h_target != NULL);
  28576. if (DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(h_target)) {
  28577. return DUK_RET_TYPE_ERROR;
  28578. }
  28579. /* Reject a proxy object as the handler because it would cause
  28580. * potentially unbounded recursion. (ES6 has no such restriction)
  28581. */
  28582. h_handler = duk_require_hobject_or_lfunc_coerce(ctx, 1);
  28583. DUK_ASSERT(h_handler != NULL);
  28584. if (DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(h_handler)) {
  28585. return DUK_RET_TYPE_ERROR;
  28586. }
  28587. /* XXX: the returned value is exotic in ES6 (draft), but we use a
  28588. * simple object here with no prototype. Without a prototype,
  28589. * [[DefaultValue]] coercion fails which is abit confusing.
  28590. * No callable check/handling in the current Proxy subset.
  28591. */
  28592. (void) duk_push_object_helper_proto(ctx,
  28593. DUK_HOBJECT_FLAG_EXTENSIBLE |
  28594. DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ |
  28595. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  28596. NULL);
  28597. DUK_ASSERT_TOP(ctx, 3);
  28598. /* Proxy target */
  28599. duk_dup(ctx, 0);
  28600. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_TARGET, DUK_PROPDESC_FLAGS_WC);
  28601. /* Proxy handler */
  28602. duk_dup(ctx, 1);
  28603. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_HANDLER, DUK_PROPDESC_FLAGS_WC);
  28604. return 1; /* replacement handler */
  28605. }
  28606. #else /* DUK_USE_ES6_PROXY */
  28607. DUK_INTERNAL duk_ret_t duk_bi_proxy_constructor(duk_context *ctx) {
  28608. DUK_UNREF(ctx);
  28609. return DUK_RET_UNSUPPORTED_ERROR;
  28610. }
  28611. #endif /* DUK_USE_ES6_PROXY */
  28612. #line 1 "duk_bi_regexp.c"
  28613. /*
  28614. * RegExp built-ins
  28615. */
  28616. /* include removed: duk_internal.h */
  28617. #ifdef DUK_USE_REGEXP_SUPPORT
  28618. DUK_LOCAL void duk__get_this_regexp(duk_context *ctx) {
  28619. duk_hobject *h;
  28620. duk_push_this(ctx);
  28621. h = duk_require_hobject_with_class(ctx, -1, DUK_HOBJECT_CLASS_REGEXP);
  28622. DUK_ASSERT(h != NULL);
  28623. DUK_UNREF(h);
  28624. duk_insert(ctx, 0); /* prepend regexp to valstack 0 index */
  28625. }
  28626. /* XXX: much to improve (code size) */
  28627. DUK_INTERNAL duk_ret_t duk_bi_regexp_constructor(duk_context *ctx) {
  28628. duk_hthread *thr = (duk_hthread *) ctx;
  28629. duk_hobject *h_pattern;
  28630. DUK_ASSERT_TOP(ctx, 2);
  28631. h_pattern = duk_get_hobject(ctx, 0);
  28632. if (!duk_is_constructor_call(ctx) &&
  28633. h_pattern != NULL &&
  28634. DUK_HOBJECT_GET_CLASS_NUMBER(h_pattern) == DUK_HOBJECT_CLASS_REGEXP &&
  28635. duk_is_undefined(ctx, 1)) {
  28636. /* Called as a function, pattern has [[Class]] "RegExp" and
  28637. * flags is undefined -> return object as is.
  28638. */
  28639. duk_dup(ctx, 0);
  28640. return 1;
  28641. }
  28642. /* Else functionality is identical for function call and constructor
  28643. * call.
  28644. */
  28645. if (h_pattern != NULL &&
  28646. DUK_HOBJECT_GET_CLASS_NUMBER(h_pattern) == DUK_HOBJECT_CLASS_REGEXP) {
  28647. if (duk_is_undefined(ctx, 1)) {
  28648. duk_bool_t flag_g, flag_i, flag_m;
  28649. duk_get_prop_stridx(ctx, 0, DUK_STRIDX_SOURCE);
  28650. flag_g = duk_get_prop_stridx_boolean(ctx, 0, DUK_STRIDX_GLOBAL, NULL);
  28651. flag_i = duk_get_prop_stridx_boolean(ctx, 0, DUK_STRIDX_IGNORE_CASE, NULL);
  28652. flag_m = duk_get_prop_stridx_boolean(ctx, 0, DUK_STRIDX_MULTILINE, NULL);
  28653. duk_push_sprintf(ctx, "%s%s%s",
  28654. (const char *) (flag_g ? "g" : ""),
  28655. (const char *) (flag_i ? "i" : ""),
  28656. (const char *) (flag_m ? "m" : ""));
  28657. /* [ ... pattern flags ] */
  28658. } else {
  28659. return DUK_RET_TYPE_ERROR;
  28660. }
  28661. } else {
  28662. if (duk_is_undefined(ctx, 0)) {
  28663. duk_push_string(ctx, "");
  28664. } else {
  28665. duk_dup(ctx, 0);
  28666. duk_to_string(ctx, -1);
  28667. }
  28668. if (duk_is_undefined(ctx, 1)) {
  28669. duk_push_string(ctx, "");
  28670. } else {
  28671. duk_dup(ctx, 1);
  28672. duk_to_string(ctx, -1);
  28673. }
  28674. /* [ ... pattern flags ] */
  28675. }
  28676. DUK_DDD(DUK_DDDPRINT("RegExp constructor/function call, pattern=%!T, flags=%!T",
  28677. (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1)));
  28678. /* [ ... pattern flags ] */
  28679. duk_regexp_compile(thr);
  28680. /* [ ... bytecode escaped_source ] */
  28681. duk_regexp_create_instance(thr);
  28682. /* [ ... RegExp ] */
  28683. return 1;
  28684. }
  28685. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_exec(duk_context *ctx) {
  28686. duk__get_this_regexp(ctx);
  28687. /* [ regexp input ] */
  28688. duk_regexp_match((duk_hthread *) ctx);
  28689. /* [ result ] */
  28690. return 1;
  28691. }
  28692. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_test(duk_context *ctx) {
  28693. duk__get_this_regexp(ctx);
  28694. /* [ regexp input ] */
  28695. /* result object is created and discarded; wasteful but saves code space */
  28696. duk_regexp_match((duk_hthread *) ctx);
  28697. /* [ result ] */
  28698. duk_push_boolean(ctx, (duk_is_null(ctx, -1) ? 0 : 1));
  28699. return 1;
  28700. }
  28701. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_to_string(duk_context *ctx) {
  28702. duk_hstring *h_bc;
  28703. duk_small_int_t re_flags;
  28704. #if 0
  28705. /* A little tricky string approach to provide the flags string.
  28706. * This depends on the specific flag values in duk_regexp.h,
  28707. * which needs to be asserted for. In practice this doesn't
  28708. * produce more compact code than the easier approach in use.
  28709. */
  28710. const char *flag_strings = "gim\0gi\0gm\0g\0";
  28711. duk_uint8_t flag_offsets[8] = {
  28712. (duk_uint8_t) 3, /* flags: "" */
  28713. (duk_uint8_t) 10, /* flags: "g" */
  28714. (duk_uint8_t) 5, /* flags: "i" */
  28715. (duk_uint8_t) 4, /* flags: "gi" */
  28716. (duk_uint8_t) 2, /* flags: "m" */
  28717. (duk_uint8_t) 7, /* flags: "gm" */
  28718. (duk_uint8_t) 1, /* flags: "im" */
  28719. (duk_uint8_t) 0, /* flags: "gim" */
  28720. };
  28721. DUK_ASSERT(DUK_RE_FLAG_GLOBAL == 1);
  28722. DUK_ASSERT(DUK_RE_FLAG_IGNORE_CASE == 2);
  28723. DUK_ASSERT(DUK_RE_FLAG_MULTILINE == 4);
  28724. #endif
  28725. duk__get_this_regexp(ctx);
  28726. /* [ regexp ] */
  28727. duk_get_prop_stridx(ctx, 0, DUK_STRIDX_SOURCE);
  28728. duk_get_prop_stridx(ctx, 0, DUK_STRIDX_INT_BYTECODE);
  28729. h_bc = duk_get_hstring(ctx, -1);
  28730. DUK_ASSERT(h_bc != NULL);
  28731. DUK_ASSERT(DUK_HSTRING_GET_BYTELEN(h_bc) >= 1);
  28732. DUK_ASSERT(DUK_HSTRING_GET_CHARLEN(h_bc) >= 1);
  28733. DUK_ASSERT(DUK_HSTRING_GET_DATA(h_bc)[0] < 0x80);
  28734. re_flags = (duk_small_int_t) DUK_HSTRING_GET_DATA(h_bc)[0];
  28735. /* [ regexp source bytecode ] */
  28736. #if 1
  28737. /* This is a cleaner approach and also produces smaller code than
  28738. * the other alternative. Use duk_require_string() for format
  28739. * safety (although the source property should always exist).
  28740. */
  28741. duk_push_sprintf(ctx, "/%s/%s%s%s",
  28742. (const char *) duk_require_string(ctx, -2), /* require to be safe */
  28743. (re_flags & DUK_RE_FLAG_GLOBAL) ? "g" : "",
  28744. (re_flags & DUK_RE_FLAG_IGNORE_CASE) ? "i" : "",
  28745. (re_flags & DUK_RE_FLAG_MULTILINE) ? "m" : "");
  28746. #else
  28747. /* This should not be necessary because no-one should tamper with the
  28748. * regexp bytecode, but is prudent to avoid potential segfaults if that
  28749. * were to happen for some reason.
  28750. */
  28751. re_flags &= 0x07;
  28752. DUK_ASSERT(re_flags >= 0 && re_flags <= 7); /* three flags */
  28753. duk_push_sprintf(ctx, "/%s/%s",
  28754. (const char *) duk_require_string(ctx, -2),
  28755. (const char *) (flag_strings + flag_offsets[re_flags]));
  28756. #endif
  28757. return 1;
  28758. }
  28759. #else /* DUK_USE_REGEXP_SUPPORT */
  28760. DUK_INTERNAL duk_ret_t duk_bi_regexp_constructor(duk_context *ctx) {
  28761. DUK_UNREF(ctx);
  28762. return DUK_RET_UNSUPPORTED_ERROR;
  28763. }
  28764. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_exec(duk_context *ctx) {
  28765. DUK_UNREF(ctx);
  28766. return DUK_RET_UNSUPPORTED_ERROR;
  28767. }
  28768. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_test(duk_context *ctx) {
  28769. DUK_UNREF(ctx);
  28770. return DUK_RET_UNSUPPORTED_ERROR;
  28771. }
  28772. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_to_string(duk_context *ctx) {
  28773. DUK_UNREF(ctx);
  28774. return DUK_RET_UNSUPPORTED_ERROR;
  28775. }
  28776. #endif /* DUK_USE_REGEXP_SUPPORT */
  28777. #line 1 "duk_bi_string.c"
  28778. /*
  28779. * String built-ins
  28780. */
  28781. /* XXX: There are several limitations in the current implementation for
  28782. * strings with >= 0x80000000UL characters. In some cases one would need
  28783. * to be able to represent the range [-0xffffffff,0xffffffff] and so on.
  28784. * Generally character and byte length are assumed to fit into signed 32
  28785. * bits (< 0x80000000UL). Places with issues are not marked explicitly
  28786. * below in all cases, look for signed type usage (duk_int_t etc) for
  28787. * offsets/lengths.
  28788. */
  28789. /* include removed: duk_internal.h */
  28790. /*
  28791. * Constructor
  28792. */
  28793. DUK_INTERNAL duk_ret_t duk_bi_string_constructor(duk_context *ctx) {
  28794. /* String constructor needs to distinguish between an argument not given at all
  28795. * vs. given as 'undefined'. We're a vararg function to handle this properly.
  28796. */
  28797. if (duk_get_top(ctx) == 0) {
  28798. duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING);
  28799. } else {
  28800. duk_to_string(ctx, 0);
  28801. }
  28802. DUK_ASSERT(duk_is_string(ctx, 0));
  28803. duk_set_top(ctx, 1);
  28804. if (duk_is_constructor_call(ctx)) {
  28805. duk_push_object_helper(ctx,
  28806. DUK_HOBJECT_FLAG_EXTENSIBLE |
  28807. DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ |
  28808. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_STRING),
  28809. DUK_BIDX_STRING_PROTOTYPE);
  28810. /* String object internal value is immutable */
  28811. duk_dup(ctx, 0);
  28812. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE);
  28813. }
  28814. /* Note: unbalanced stack on purpose */
  28815. return 1;
  28816. }
  28817. DUK_INTERNAL duk_ret_t duk_bi_string_constructor_from_char_code(duk_context *ctx) {
  28818. duk_hthread *thr = (duk_hthread *) ctx;
  28819. duk_hbuffer_dynamic *h;
  28820. duk_idx_t i, n;
  28821. duk_ucodepoint_t cp;
  28822. /* XXX: It would be nice to build the string directly but ToUint16()
  28823. * coercion is needed so a generic helper would not be very
  28824. * helpful (perhaps coerce the value stack first here and then
  28825. * build a string from a duk_tval number sequence in one go?).
  28826. */
  28827. n = duk_get_top(ctx);
  28828. duk_push_dynamic_buffer(ctx, 0); /* XXX: initial spare size estimate from 'n' */
  28829. h = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, -1);
  28830. for (i = 0; i < n; i++) {
  28831. #if defined(DUK_USE_NONSTD_STRING_FROMCHARCODE_32BIT)
  28832. /* ToUint16() coercion is mandatory in the E5.1 specification, but
  28833. * this non-compliant behavior makes more sense because we support
  28834. * non-BMP codepoints. Don't use CESU-8 because that'd create
  28835. * surrogate pairs.
  28836. */
  28837. cp = (duk_ucodepoint_t) duk_to_uint32(ctx, i);
  28838. duk_hbuffer_append_xutf8(thr, h, cp);
  28839. #else
  28840. cp = (duk_ucodepoint_t) duk_to_uint16(ctx, i);
  28841. duk_hbuffer_append_cesu8(thr, h, cp);
  28842. #endif
  28843. }
  28844. duk_to_string(ctx, -1);
  28845. return 1;
  28846. }
  28847. /*
  28848. * toString(), valueOf()
  28849. */
  28850. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_to_string(duk_context *ctx) {
  28851. duk_tval *tv;
  28852. duk_push_this(ctx);
  28853. tv = duk_require_tval(ctx, -1);
  28854. DUK_ASSERT(tv != NULL);
  28855. if (DUK_TVAL_IS_STRING(tv)) {
  28856. /* return as is */
  28857. return 1;
  28858. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  28859. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  28860. DUK_ASSERT(h != NULL);
  28861. /* Must be a "string object", i.e. class "String" */
  28862. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_STRING) {
  28863. goto type_error;
  28864. }
  28865. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE);
  28866. DUK_ASSERT(duk_is_string(ctx, -1));
  28867. return 1;
  28868. } else {
  28869. goto type_error;
  28870. }
  28871. /* never here, but fall through */
  28872. type_error:
  28873. return DUK_RET_TYPE_ERROR;
  28874. }
  28875. /*
  28876. * Character and charcode access
  28877. */
  28878. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_char_at(duk_context *ctx) {
  28879. duk_int_t pos;
  28880. /* XXX: faster implementation */
  28881. (void) duk_push_this_coercible_to_string(ctx);
  28882. pos = duk_to_int(ctx, 0);
  28883. duk_substring(ctx, -1, pos, pos + 1);
  28884. return 1;
  28885. }
  28886. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_char_code_at(duk_context *ctx) {
  28887. duk_hthread *thr = (duk_hthread *) ctx;
  28888. duk_int_t pos;
  28889. duk_hstring *h;
  28890. duk_bool_t clamped;
  28891. /* XXX: faster implementation */
  28892. DUK_DDD(DUK_DDDPRINT("arg=%!T", (duk_tval *) duk_get_tval(ctx, 0)));
  28893. h = duk_push_this_coercible_to_string(ctx);
  28894. DUK_ASSERT(h != NULL);
  28895. pos = duk_to_int_clamped_raw(ctx,
  28896. 0 /*index*/,
  28897. 0 /*min(incl)*/,
  28898. DUK_HSTRING_GET_CHARLEN(h) - 1 /*max(incl)*/,
  28899. &clamped /*out_clamped*/);
  28900. if (clamped) {
  28901. duk_push_number(ctx, DUK_DOUBLE_NAN);
  28902. return 1;
  28903. }
  28904. duk_push_u32(ctx, (duk_uint32_t) duk_hstring_char_code_at_raw(thr, h, pos));
  28905. return 1;
  28906. }
  28907. /*
  28908. * substring(), substr(), slice()
  28909. */
  28910. /* XXX: any chance of merging these three similar but still slightly
  28911. * different algorithms so that footprint would be reduced?
  28912. */
  28913. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_substring(duk_context *ctx) {
  28914. duk_hstring *h;
  28915. duk_int_t start_pos, end_pos;
  28916. duk_int_t len;
  28917. h = duk_push_this_coercible_to_string(ctx);
  28918. DUK_ASSERT(h != NULL);
  28919. len = (duk_int_t) DUK_HSTRING_GET_CHARLEN(h);
  28920. /* [ start end str ] */
  28921. start_pos = duk_to_int_clamped(ctx, 0, 0, len);
  28922. if (duk_is_undefined(ctx, 1)) {
  28923. end_pos = len;
  28924. } else {
  28925. end_pos = duk_to_int_clamped(ctx, 1, 0, len);
  28926. }
  28927. DUK_ASSERT(start_pos >= 0 && start_pos <= len);
  28928. DUK_ASSERT(end_pos >= 0 && end_pos <= len);
  28929. if (start_pos > end_pos) {
  28930. duk_int_t tmp = start_pos;
  28931. start_pos = end_pos;
  28932. end_pos = tmp;
  28933. }
  28934. DUK_ASSERT(end_pos >= start_pos);
  28935. duk_substring(ctx, -1, (duk_size_t) start_pos, (duk_size_t) end_pos);
  28936. return 1;
  28937. }
  28938. #ifdef DUK_USE_SECTION_B
  28939. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_substr(duk_context *ctx) {
  28940. duk_hstring *h;
  28941. duk_int_t start_pos, end_pos;
  28942. duk_int_t len;
  28943. /* Unlike non-obsolete String calls, substr() algorithm in E5.1
  28944. * specification will happily coerce undefined and null to strings
  28945. * ("undefined" and "null").
  28946. */
  28947. duk_push_this(ctx);
  28948. h = duk_to_hstring(ctx, -1);
  28949. DUK_ASSERT(h != NULL);
  28950. len = (duk_int_t) DUK_HSTRING_GET_CHARLEN(h);
  28951. /* [ start length str ] */
  28952. /* The implementation for computing of start_pos and end_pos differs
  28953. * from the standard algorithm, but is intended to result in the exactly
  28954. * same behavior. This is not always obvious.
  28955. */
  28956. /* combines steps 2 and 5; -len ensures max() not needed for step 5 */
  28957. start_pos = duk_to_int_clamped(ctx, 0, -len, len);
  28958. if (start_pos < 0) {
  28959. start_pos = len + start_pos;
  28960. }
  28961. DUK_ASSERT(start_pos >= 0 && start_pos <= len);
  28962. /* combines steps 3, 6; step 7 is not needed */
  28963. if (duk_is_undefined(ctx, 1)) {
  28964. end_pos = len;
  28965. } else {
  28966. DUK_ASSERT(start_pos <= len);
  28967. end_pos = start_pos + duk_to_int_clamped(ctx, 1, 0, len - start_pos);
  28968. }
  28969. DUK_ASSERT(start_pos >= 0 && start_pos <= len);
  28970. DUK_ASSERT(end_pos >= 0 && end_pos <= len);
  28971. DUK_ASSERT(end_pos >= start_pos);
  28972. duk_substring(ctx, -1, (duk_size_t) start_pos, (duk_size_t) end_pos);
  28973. return 1;
  28974. }
  28975. #else /* DUK_USE_SECTION_B */
  28976. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_substr(duk_context *ctx) {
  28977. DUK_UNREF(ctx);
  28978. return DUK_RET_UNSUPPORTED_ERROR;
  28979. }
  28980. #endif /* DUK_USE_SECTION_B */
  28981. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_slice(duk_context *ctx) {
  28982. duk_hstring *h;
  28983. duk_int_t start_pos, end_pos;
  28984. duk_int_t len;
  28985. h = duk_push_this_coercible_to_string(ctx);
  28986. DUK_ASSERT(h != NULL);
  28987. len = (duk_int_t) DUK_HSTRING_GET_CHARLEN(h);
  28988. /* [ start end str ] */
  28989. start_pos = duk_to_int_clamped(ctx, 0, -len, len);
  28990. if (start_pos < 0) {
  28991. start_pos = len + start_pos;
  28992. }
  28993. if (duk_is_undefined(ctx, 1)) {
  28994. end_pos = len;
  28995. } else {
  28996. end_pos = duk_to_int_clamped(ctx, 1, -len, len);
  28997. if (end_pos < 0) {
  28998. end_pos = len + end_pos;
  28999. }
  29000. }
  29001. DUK_ASSERT(start_pos >= 0 && start_pos <= len);
  29002. DUK_ASSERT(end_pos >= 0 && end_pos <= len);
  29003. if (end_pos < start_pos) {
  29004. end_pos = start_pos;
  29005. }
  29006. DUK_ASSERT(end_pos >= start_pos);
  29007. duk_substring(ctx, -1, (duk_size_t) start_pos, (duk_size_t) end_pos);
  29008. return 1;
  29009. }
  29010. /*
  29011. * Case conversion
  29012. */
  29013. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_caseconv_shared(duk_context *ctx) {
  29014. duk_hthread *thr = (duk_hthread *) ctx;
  29015. duk_small_int_t uppercase = duk_get_current_magic(ctx);
  29016. (void) duk_push_this_coercible_to_string(ctx);
  29017. duk_unicode_case_convert_string(thr, (duk_bool_t) uppercase);
  29018. return 1;
  29019. }
  29020. /*
  29021. * indexOf() and lastIndexOf()
  29022. */
  29023. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_indexof_shared(duk_context *ctx) {
  29024. duk_hthread *thr = (duk_hthread *) ctx;
  29025. duk_hstring *h_this;
  29026. duk_hstring *h_search;
  29027. duk_int_t clen_this;
  29028. duk_int_t cpos;
  29029. duk_int_t bpos;
  29030. const duk_uint8_t *p_start, *p_end, *p;
  29031. const duk_uint8_t *q_start;
  29032. duk_int_t q_blen;
  29033. duk_uint8_t firstbyte;
  29034. duk_uint8_t t;
  29035. duk_small_int_t is_lastindexof = duk_get_current_magic(ctx); /* 0=indexOf, 1=lastIndexOf */
  29036. h_this = duk_push_this_coercible_to_string(ctx);
  29037. DUK_ASSERT(h_this != NULL);
  29038. clen_this = (duk_int_t) DUK_HSTRING_GET_CHARLEN(h_this);
  29039. h_search = duk_to_hstring(ctx, 0);
  29040. DUK_ASSERT(h_search != NULL);
  29041. q_start = DUK_HSTRING_GET_DATA(h_search);
  29042. q_blen = (duk_int_t) DUK_HSTRING_GET_BYTELEN(h_search);
  29043. duk_to_number(ctx, 1);
  29044. if (duk_is_nan(ctx, 1) && is_lastindexof) {
  29045. /* indexOf: NaN should cause pos to be zero.
  29046. * lastIndexOf: NaN should cause pos to be +Infinity
  29047. * (and later be clamped to len).
  29048. */
  29049. cpos = clen_this;
  29050. } else {
  29051. cpos = duk_to_int_clamped(ctx, 1, 0, clen_this);
  29052. }
  29053. /* Empty searchstring always matches; cpos must be clamped here.
  29054. * (If q_blen were < 0 due to clamped coercion, it would also be
  29055. * caught here.)
  29056. */
  29057. if (q_blen <= 0) {
  29058. duk_push_int(ctx, cpos);
  29059. return 1;
  29060. }
  29061. DUK_ASSERT(q_blen > 0);
  29062. bpos = (duk_int_t) duk_heap_strcache_offset_char2byte(thr, h_this, (duk_uint32_t) cpos);
  29063. p_start = DUK_HSTRING_GET_DATA(h_this);
  29064. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_this);
  29065. p = p_start + bpos;
  29066. /* This loop is optimized for size. For speed, there should be
  29067. * two separate loops, and we should ensure that memcmp() can be
  29068. * used without an extra "will searchstring fit" check. Doing
  29069. * the preconditioning for 'p' and 'p_end' is easy but cpos
  29070. * must be updated if 'p' is wound back (backward scanning).
  29071. */
  29072. firstbyte = q_start[0]; /* leading byte of match string */
  29073. while (p <= p_end && p >= p_start) {
  29074. t = *p;
  29075. /* For Ecmascript strings, this check can only match for
  29076. * initial UTF-8 bytes (not continuation bytes). For other
  29077. * strings all bets are off.
  29078. */
  29079. if ((t == firstbyte) && ((duk_size_t) (p_end - p) >= (duk_size_t) q_blen)) {
  29080. DUK_ASSERT(q_blen > 0); /* no issues with memcmp() zero size, even if broken */
  29081. if (DUK_MEMCMP(p, q_start, (duk_size_t) q_blen) == 0) {
  29082. duk_push_int(ctx, cpos);
  29083. return 1;
  29084. }
  29085. }
  29086. /* track cpos while scanning */
  29087. if (is_lastindexof) {
  29088. /* when going backwards, we decrement cpos 'early';
  29089. * 'p' may point to a continuation byte of the char
  29090. * at offset 'cpos', but that's OK because we'll
  29091. * backtrack all the way to the initial byte.
  29092. */
  29093. if ((t & 0xc0) != 0x80) {
  29094. cpos--;
  29095. }
  29096. p--;
  29097. } else {
  29098. if ((t & 0xc0) != 0x80) {
  29099. cpos++;
  29100. }
  29101. p++;
  29102. }
  29103. }
  29104. /* Not found. Empty string case is handled specially above. */
  29105. duk_push_int(ctx, -1);
  29106. return 1;
  29107. }
  29108. /*
  29109. * replace()
  29110. */
  29111. /* XXX: the current implementation works but is quite clunky; it compiles
  29112. * to almost 1,4kB of x86 code so it needs to be simplified (better approach,
  29113. * shared helpers, etc). Some ideas for refactoring:
  29114. *
  29115. * - a primitive to convert a string into a regexp matcher (reduces matching
  29116. * code at the cost of making matching much slower)
  29117. * - use replace() as a basic helper for match() and split(), which are both
  29118. * much simpler
  29119. * - API call to get_prop and to_boolean
  29120. */
  29121. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_replace(duk_context *ctx) {
  29122. duk_hthread *thr = (duk_hthread *) ctx;
  29123. duk_hstring *h_input;
  29124. duk_hstring *h_match;
  29125. duk_hstring *h_search;
  29126. duk_hobject *h_re;
  29127. duk_hbuffer_dynamic *h_buf;
  29128. #ifdef DUK_USE_REGEXP_SUPPORT
  29129. duk_bool_t is_regexp;
  29130. duk_bool_t is_global;
  29131. #endif
  29132. duk_bool_t is_repl_func;
  29133. duk_uint32_t match_start_coff, match_start_boff;
  29134. #ifdef DUK_USE_REGEXP_SUPPORT
  29135. duk_int_t match_caps;
  29136. #endif
  29137. duk_uint32_t prev_match_end_boff;
  29138. const duk_uint8_t *r_start, *r_end, *r; /* repl string scan */
  29139. DUK_ASSERT_TOP(ctx, 2);
  29140. h_input = duk_push_this_coercible_to_string(ctx);
  29141. DUK_ASSERT(h_input != NULL);
  29142. duk_push_dynamic_buffer(ctx, 0);
  29143. h_buf = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, -1);
  29144. DUK_ASSERT(h_buf != NULL);
  29145. DUK_ASSERT_TOP(ctx, 4);
  29146. /* stack[0] = search value
  29147. * stack[1] = replace value
  29148. * stack[2] = input string
  29149. * stack[3] = result buffer
  29150. */
  29151. h_re = duk_get_hobject_with_class(ctx, 0, DUK_HOBJECT_CLASS_REGEXP);
  29152. if (h_re) {
  29153. #ifdef DUK_USE_REGEXP_SUPPORT
  29154. is_regexp = 1;
  29155. is_global = duk_get_prop_stridx_boolean(ctx, 0, DUK_STRIDX_GLOBAL, NULL);
  29156. if (is_global) {
  29157. /* start match from beginning */
  29158. duk_push_int(ctx, 0);
  29159. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LAST_INDEX);
  29160. }
  29161. #else /* DUK_USE_REGEXP_SUPPORT */
  29162. return DUK_RET_UNSUPPORTED_ERROR;
  29163. #endif /* DUK_USE_REGEXP_SUPPORT */
  29164. } else {
  29165. duk_to_string(ctx, 0);
  29166. #ifdef DUK_USE_REGEXP_SUPPORT
  29167. is_regexp = 0;
  29168. is_global = 0;
  29169. #endif
  29170. }
  29171. if (duk_is_function(ctx, 1)) {
  29172. is_repl_func = 1;
  29173. r_start = NULL;
  29174. r_end = NULL;
  29175. } else {
  29176. duk_hstring *h_repl;
  29177. is_repl_func = 0;
  29178. h_repl = duk_to_hstring(ctx, 1);
  29179. DUK_ASSERT(h_repl != NULL);
  29180. r_start = DUK_HSTRING_GET_DATA(h_repl);
  29181. r_end = r_start + DUK_HSTRING_GET_BYTELEN(h_repl);
  29182. }
  29183. prev_match_end_boff = 0;
  29184. for (;;) {
  29185. /*
  29186. * If matching with a regexp:
  29187. * - non-global RegExp: lastIndex not touched on a match, zeroed
  29188. * on a non-match
  29189. * - global RegExp: on match, lastIndex will be updated by regexp
  29190. * executor to point to next char after the matching part (so that
  29191. * characters in the matching part are not matched again)
  29192. *
  29193. * If matching with a string:
  29194. * - always non-global match, find first occurrence
  29195. *
  29196. * We need:
  29197. * - The character offset of start-of-match for the replacer function
  29198. * - The byte offsets for start-of-match and end-of-match to implement
  29199. * the replacement values $&, $`, and $', and to copy non-matching
  29200. * input string portions (including header and trailer) verbatim.
  29201. *
  29202. * NOTE: the E5.1 specification is a bit vague how the RegExp should
  29203. * behave in the replacement process; e.g. is matching done first for
  29204. * all matches (in the global RegExp case) before any replacer calls
  29205. * are made? See: test-bi-string-proto-replace.js for discussion.
  29206. */
  29207. DUK_ASSERT_TOP(ctx, 4);
  29208. #ifdef DUK_USE_REGEXP_SUPPORT
  29209. if (is_regexp) {
  29210. duk_dup(ctx, 0);
  29211. duk_dup(ctx, 2);
  29212. duk_regexp_match(thr); /* [ ... regexp input ] -> [ res_obj ] */
  29213. if (!duk_is_object(ctx, -1)) {
  29214. duk_pop(ctx);
  29215. break;
  29216. }
  29217. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INDEX);
  29218. DUK_ASSERT(duk_is_number(ctx, -1));
  29219. match_start_coff = duk_get_int(ctx, -1);
  29220. duk_pop(ctx);
  29221. duk_get_prop_index(ctx, -1, 0);
  29222. DUK_ASSERT(duk_is_string(ctx, -1));
  29223. h_match = duk_get_hstring(ctx, -1);
  29224. DUK_ASSERT(h_match != NULL);
  29225. duk_pop(ctx); /* h_match is borrowed, remains reachable through match_obj */
  29226. if (DUK_HSTRING_GET_BYTELEN(h_match) == 0) {
  29227. /* This should be equivalent to match() algorithm step 8.f.iii.2:
  29228. * detect an empty match and allow it, but don't allow it twice.
  29229. */
  29230. duk_uint32_t last_index;
  29231. duk_get_prop_stridx(ctx, 0, DUK_STRIDX_LAST_INDEX);
  29232. last_index = (duk_uint32_t) duk_get_uint(ctx, -1);
  29233. DUK_DDD(DUK_DDDPRINT("empty match, bump lastIndex: %ld -> %ld",
  29234. (long) last_index, (long) (last_index + 1)));
  29235. duk_pop(ctx);
  29236. duk_push_int(ctx, last_index + 1);
  29237. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LAST_INDEX);
  29238. }
  29239. DUK_ASSERT(duk_get_length(ctx, -1) <= DUK_INT_MAX); /* string limits */
  29240. match_caps = (duk_int_t) duk_get_length(ctx, -1);
  29241. } else {
  29242. #else /* DUK_USE_REGEXP_SUPPORT */
  29243. { /* unconditionally */
  29244. #endif /* DUK_USE_REGEXP_SUPPORT */
  29245. const duk_uint8_t *p_start, *p_end, *p; /* input string scan */
  29246. const duk_uint8_t *q_start; /* match string */
  29247. duk_size_t q_blen;
  29248. #ifdef DUK_USE_REGEXP_SUPPORT
  29249. DUK_ASSERT(!is_global); /* single match always */
  29250. #endif
  29251. p_start = DUK_HSTRING_GET_DATA(h_input);
  29252. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  29253. p = p_start;
  29254. h_search = duk_get_hstring(ctx, 0);
  29255. DUK_ASSERT(h_search != NULL);
  29256. q_start = DUK_HSTRING_GET_DATA(h_search);
  29257. q_blen = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h_search);
  29258. p_end -= q_blen; /* ensure full memcmp() fits in while */
  29259. match_start_coff = 0;
  29260. while (p <= p_end) {
  29261. DUK_ASSERT(p + q_blen <= DUK_HSTRING_GET_DATA(h_input) + DUK_HSTRING_GET_BYTELEN(h_input));
  29262. if (DUK_MEMCMP((void *) p, (void *) q_start, (size_t) q_blen) == 0) {
  29263. duk_dup(ctx, 0);
  29264. h_match = duk_get_hstring(ctx, -1);
  29265. DUK_ASSERT(h_match != NULL);
  29266. #ifdef DUK_USE_REGEXP_SUPPORT
  29267. match_caps = 0;
  29268. #endif
  29269. goto found;
  29270. }
  29271. /* track utf-8 non-continuation bytes */
  29272. if ((p[0] & 0xc0) != 0x80) {
  29273. match_start_coff++;
  29274. }
  29275. p++;
  29276. }
  29277. /* not found */
  29278. break;
  29279. }
  29280. found:
  29281. /* stack[0] = search value
  29282. * stack[1] = replace value
  29283. * stack[2] = input string
  29284. * stack[3] = result buffer
  29285. * stack[4] = regexp match OR match string
  29286. */
  29287. match_start_boff = duk_heap_strcache_offset_char2byte(thr, h_input, match_start_coff);
  29288. duk_hbuffer_append_bytes(thr,
  29289. h_buf,
  29290. DUK_HSTRING_GET_DATA(h_input) + prev_match_end_boff,
  29291. (duk_size_t) (match_start_boff - prev_match_end_boff));
  29292. prev_match_end_boff = match_start_boff + DUK_HSTRING_GET_BYTELEN(h_match);
  29293. if (is_repl_func) {
  29294. duk_idx_t idx_args;
  29295. duk_hstring *h_repl;
  29296. /* regexp res_obj is at index 4 */
  29297. duk_dup(ctx, 1);
  29298. idx_args = duk_get_top(ctx);
  29299. #ifdef DUK_USE_REGEXP_SUPPORT
  29300. if (is_regexp) {
  29301. duk_int_t idx;
  29302. duk_require_stack(ctx, match_caps + 2);
  29303. for (idx = 0; idx < match_caps; idx++) {
  29304. /* match followed by capture(s) */
  29305. duk_get_prop_index(ctx, 4, idx);
  29306. }
  29307. } else {
  29308. #else /* DUK_USE_REGEXP_SUPPORT */
  29309. { /* unconditionally */
  29310. #endif /* DUK_USE_REGEXP_SUPPORT */
  29311. /* match == search string, by definition */
  29312. duk_dup(ctx, 0);
  29313. }
  29314. duk_push_int(ctx, match_start_coff);
  29315. duk_dup(ctx, 2);
  29316. /* [ ... replacer match [captures] match_char_offset input ] */
  29317. duk_call(ctx, duk_get_top(ctx) - idx_args);
  29318. h_repl = duk_to_hstring(ctx, -1); /* -> [ ... repl_value ] */
  29319. DUK_ASSERT(h_repl != NULL);
  29320. duk_hbuffer_append_hstring(thr, h_buf, h_repl);
  29321. duk_pop(ctx); /* repl_value */
  29322. } else {
  29323. r = r_start;
  29324. while (r < r_end) {
  29325. duk_int_t ch1;
  29326. duk_int_t ch2;
  29327. #ifdef DUK_USE_REGEXP_SUPPORT
  29328. duk_int_t ch3;
  29329. #endif
  29330. duk_size_t left;
  29331. ch1 = *r++;
  29332. if (ch1 != DUK_ASC_DOLLAR) {
  29333. goto repl_write;
  29334. }
  29335. left = r_end - r;
  29336. if (left <= 0) {
  29337. goto repl_write;
  29338. }
  29339. ch2 = r[0];
  29340. switch ((int) ch2) {
  29341. case DUK_ASC_DOLLAR: {
  29342. ch1 = (1 << 8) + DUK_ASC_DOLLAR;
  29343. goto repl_write;
  29344. }
  29345. case DUK_ASC_AMP: {
  29346. duk_hbuffer_append_hstring(thr, h_buf, h_match);
  29347. r++;
  29348. continue;
  29349. }
  29350. case DUK_ASC_GRAVE: {
  29351. duk_hbuffer_append_bytes(thr,
  29352. h_buf,
  29353. DUK_HSTRING_GET_DATA(h_input),
  29354. match_start_boff);
  29355. r++;
  29356. continue;
  29357. }
  29358. case DUK_ASC_SINGLEQUOTE: {
  29359. duk_uint32_t match_end_boff;
  29360. /* Use match charlen instead of bytelen, just in case the input and
  29361. * match codepoint encodings would have different lengths.
  29362. */
  29363. match_end_boff = duk_heap_strcache_offset_char2byte(thr,
  29364. h_input,
  29365. match_start_coff + DUK_HSTRING_GET_CHARLEN(h_match));
  29366. duk_hbuffer_append_bytes(thr,
  29367. h_buf,
  29368. DUK_HSTRING_GET_DATA(h_input) + match_end_boff,
  29369. DUK_HSTRING_GET_BYTELEN(h_input) - match_end_boff);
  29370. r++;
  29371. continue;
  29372. }
  29373. default: {
  29374. #ifdef DUK_USE_REGEXP_SUPPORT
  29375. duk_int_t capnum, captmp, capadv;
  29376. /* XXX: optional check, match_caps is zero if no regexp,
  29377. * so dollar will be interpreted literally anyway.
  29378. */
  29379. if (!is_regexp) {
  29380. goto repl_write;
  29381. }
  29382. if (!(ch2 >= DUK_ASC_0 && ch2 <= DUK_ASC_9)) {
  29383. goto repl_write;
  29384. }
  29385. capnum = ch2 - DUK_ASC_0;
  29386. capadv = 1;
  29387. if (left >= 2) {
  29388. ch3 = r[1];
  29389. if (ch3 >= DUK_ASC_0 && ch3 <= DUK_ASC_9) {
  29390. captmp = capnum * 10 + (ch3 - DUK_ASC_0);
  29391. if (captmp < match_caps) {
  29392. capnum = captmp;
  29393. capadv = 2;
  29394. }
  29395. }
  29396. }
  29397. if (capnum > 0 && capnum < match_caps) {
  29398. DUK_ASSERT(is_regexp != 0); /* match_caps == 0 without regexps */
  29399. /* regexp res_obj is at offset 4 */
  29400. duk_get_prop_index(ctx, 4, (duk_uarridx_t) capnum);
  29401. if (duk_is_string(ctx, -1)) {
  29402. DUK_ASSERT(duk_get_hstring(ctx, -1) != NULL);
  29403. duk_hbuffer_append_hstring(thr, h_buf, duk_get_hstring(ctx, -1));
  29404. } else {
  29405. /* undefined -> skip (replaced with empty) */
  29406. }
  29407. duk_pop(ctx);
  29408. r += capadv;
  29409. continue;
  29410. } else {
  29411. goto repl_write;
  29412. }
  29413. #else /* DUK_USE_REGEXP_SUPPORT */
  29414. goto repl_write; /* unconditionally */
  29415. #endif /* DUK_USE_REGEXP_SUPPORT */
  29416. } /* default case */
  29417. } /* switch (ch2) */
  29418. repl_write:
  29419. /* ch1 = (r_increment << 8) + byte */
  29420. duk_hbuffer_append_byte(thr, h_buf, (duk_uint8_t) (ch1 & 0xff));
  29421. r += ch1 >> 8;
  29422. } /* while repl */
  29423. } /* if (is_repl_func) */
  29424. duk_pop(ctx); /* pop regexp res_obj or match string */
  29425. #ifdef DUK_USE_REGEXP_SUPPORT
  29426. if (!is_global) {
  29427. #else
  29428. { /* unconditionally; is_global==0 */
  29429. #endif
  29430. break;
  29431. }
  29432. }
  29433. /* trailer */
  29434. duk_hbuffer_append_bytes(thr,
  29435. h_buf,
  29436. DUK_HSTRING_GET_DATA(h_input) + prev_match_end_boff,
  29437. (duk_size_t) (DUK_HSTRING_GET_BYTELEN(h_input) - prev_match_end_boff));
  29438. DUK_ASSERT_TOP(ctx, 4);
  29439. duk_to_string(ctx, -1);
  29440. return 1;
  29441. }
  29442. /*
  29443. * split()
  29444. */
  29445. /* XXX: very messy now, but works; clean up, remove unused variables (nomimally
  29446. * used so compiler doesn't complain).
  29447. */
  29448. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_split(duk_context *ctx) {
  29449. duk_hthread *thr = (duk_hthread *) ctx;
  29450. duk_hstring *h_input;
  29451. duk_hstring *h_sep;
  29452. duk_uint32_t limit;
  29453. duk_uint32_t arr_idx;
  29454. #ifdef DUK_USE_REGEXP_SUPPORT
  29455. duk_bool_t is_regexp;
  29456. #endif
  29457. duk_bool_t matched; /* set to 1 if any match exists (needed for empty input special case) */
  29458. duk_uint32_t prev_match_end_coff, prev_match_end_boff;
  29459. duk_uint32_t match_start_boff, match_start_coff;
  29460. duk_uint32_t match_end_boff, match_end_coff;
  29461. DUK_UNREF(thr);
  29462. h_input = duk_push_this_coercible_to_string(ctx);
  29463. DUK_ASSERT(h_input != NULL);
  29464. duk_push_array(ctx);
  29465. if (duk_is_undefined(ctx, 1)) {
  29466. limit = 0xffffffffUL;
  29467. } else {
  29468. limit = duk_to_uint32(ctx, 1);
  29469. }
  29470. if (limit == 0) {
  29471. return 1;
  29472. }
  29473. /* If the separator is a RegExp, make a "clone" of it. The specification
  29474. * algorithm calls [[Match]] directly for specific indices; we emulate this
  29475. * by tweaking lastIndex and using a "force global" variant of duk_regexp_match()
  29476. * which will use global-style matching even when the RegExp itself is non-global.
  29477. */
  29478. if (duk_is_undefined(ctx, 0)) {
  29479. /* The spec algorithm first does "R = ToString(separator)" before checking
  29480. * whether separator is undefined. Since this is side effect free, we can
  29481. * skip the ToString() here.
  29482. */
  29483. duk_dup(ctx, 2);
  29484. duk_put_prop_index(ctx, 3, 0);
  29485. return 1;
  29486. } else if (duk_get_hobject_with_class(ctx, 0, DUK_HOBJECT_CLASS_REGEXP) != NULL) {
  29487. #ifdef DUK_USE_REGEXP_SUPPORT
  29488. duk_push_hobject_bidx(ctx, DUK_BIDX_REGEXP_CONSTRUCTOR);
  29489. duk_dup(ctx, 0);
  29490. duk_new(ctx, 1); /* [ ... RegExp val ] -> [ ... res ] */
  29491. duk_replace(ctx, 0);
  29492. /* lastIndex is initialized to zero by new RegExp() */
  29493. is_regexp = 1;
  29494. #else
  29495. return DUK_RET_UNSUPPORTED_ERROR;
  29496. #endif
  29497. } else {
  29498. duk_to_string(ctx, 0);
  29499. #ifdef DUK_USE_REGEXP_SUPPORT
  29500. is_regexp = 0;
  29501. #endif
  29502. }
  29503. /* stack[0] = separator (string or regexp)
  29504. * stack[1] = limit
  29505. * stack[2] = input string
  29506. * stack[3] = result array
  29507. */
  29508. prev_match_end_boff = 0;
  29509. prev_match_end_coff = 0;
  29510. arr_idx = 0;
  29511. matched = 0;
  29512. for (;;) {
  29513. /*
  29514. * The specification uses RegExp [[Match]] to attempt match at specific
  29515. * offsets. We don't have such a primitive, so we use an actual RegExp
  29516. * and tweak lastIndex. Since the RegExp may be non-global, we use a
  29517. * special variant which forces global-like behavior for matching.
  29518. */
  29519. DUK_ASSERT_TOP(ctx, 4);
  29520. #ifdef DUK_USE_REGEXP_SUPPORT
  29521. if (is_regexp) {
  29522. duk_dup(ctx, 0);
  29523. duk_dup(ctx, 2);
  29524. duk_regexp_match_force_global(thr); /* [ ... regexp input ] -> [ res_obj ] */
  29525. if (!duk_is_object(ctx, -1)) {
  29526. duk_pop(ctx);
  29527. break;
  29528. }
  29529. matched = 1;
  29530. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INDEX);
  29531. DUK_ASSERT(duk_is_number(ctx, -1));
  29532. match_start_coff = duk_get_int(ctx, -1);
  29533. match_start_boff = duk_heap_strcache_offset_char2byte(thr, h_input, match_start_coff);
  29534. duk_pop(ctx);
  29535. if (match_start_coff == DUK_HSTRING_GET_CHARLEN(h_input)) {
  29536. /* don't allow an empty match at the end of the string */
  29537. duk_pop(ctx);
  29538. break;
  29539. }
  29540. duk_get_prop_stridx(ctx, 0, DUK_STRIDX_LAST_INDEX);
  29541. DUK_ASSERT(duk_is_number(ctx, -1));
  29542. match_end_coff = duk_get_int(ctx, -1);
  29543. match_end_boff = duk_heap_strcache_offset_char2byte(thr, h_input, match_end_coff);
  29544. duk_pop(ctx);
  29545. /* empty match -> bump and continue */
  29546. if (prev_match_end_boff == match_end_boff) {
  29547. duk_push_int(ctx, match_end_coff + 1);
  29548. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LAST_INDEX);
  29549. duk_pop(ctx);
  29550. continue;
  29551. }
  29552. } else {
  29553. #else /* DUK_USE_REGEXP_SUPPORT */
  29554. { /* unconditionally */
  29555. #endif /* DUK_USE_REGEXP_SUPPORT */
  29556. const duk_uint8_t *p_start, *p_end, *p; /* input string scan */
  29557. const duk_uint8_t *q_start; /* match string */
  29558. duk_size_t q_blen, q_clen;
  29559. p_start = DUK_HSTRING_GET_DATA(h_input);
  29560. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  29561. p = p_start + prev_match_end_boff;
  29562. h_sep = duk_get_hstring(ctx, 0);
  29563. DUK_ASSERT(h_sep != NULL);
  29564. q_start = DUK_HSTRING_GET_DATA(h_sep);
  29565. q_blen = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h_sep);
  29566. q_clen = (duk_size_t) DUK_HSTRING_GET_CHARLEN(h_sep);
  29567. p_end -= q_blen; /* ensure full memcmp() fits in while */
  29568. match_start_coff = prev_match_end_coff;
  29569. if (q_blen == 0) {
  29570. /* Handle empty separator case: it will always match, and always
  29571. * triggers the check in step 13.c.iii initially. Note that we
  29572. * must skip to either end of string or start of first codepoint,
  29573. * skipping over any continuation bytes!
  29574. *
  29575. * Don't allow an empty string to match at the end of the input.
  29576. */
  29577. matched = 1; /* empty separator can always match */
  29578. match_start_coff++;
  29579. p++;
  29580. while (p < p_end) {
  29581. if ((p[0] & 0xc0) != 0x80) {
  29582. goto found;
  29583. }
  29584. p++;
  29585. }
  29586. goto not_found;
  29587. }
  29588. DUK_ASSERT(q_blen > 0 && q_clen > 0);
  29589. while (p <= p_end) {
  29590. DUK_ASSERT(p + q_blen <= DUK_HSTRING_GET_DATA(h_input) + DUK_HSTRING_GET_BYTELEN(h_input));
  29591. DUK_ASSERT(q_blen > 0); /* no issues with empty memcmp() */
  29592. if (DUK_MEMCMP((void *) p, (void *) q_start, (duk_size_t) q_blen) == 0) {
  29593. /* never an empty match, so step 13.c.iii can't be triggered */
  29594. goto found;
  29595. }
  29596. /* track utf-8 non-continuation bytes */
  29597. if ((p[0] & 0xc0) != 0x80) {
  29598. match_start_coff++;
  29599. }
  29600. p++;
  29601. }
  29602. not_found:
  29603. /* not found */
  29604. break;
  29605. found:
  29606. matched = 1;
  29607. match_start_boff = (duk_uint32_t) (p - p_start);
  29608. match_end_coff = (duk_uint32_t) (match_start_coff + q_clen); /* constrained by string length */
  29609. match_end_boff = (duk_uint32_t) (match_start_boff + q_blen); /* ditto */
  29610. /* empty match (may happen with empty separator) -> bump and continue */
  29611. if (prev_match_end_boff == match_end_boff) {
  29612. prev_match_end_boff++;
  29613. prev_match_end_coff++;
  29614. continue;
  29615. }
  29616. } /* if (is_regexp) */
  29617. /* stack[0] = separator (string or regexp)
  29618. * stack[1] = limit
  29619. * stack[2] = input string
  29620. * stack[3] = result array
  29621. * stack[4] = regexp res_obj (if is_regexp)
  29622. */
  29623. DUK_DDD(DUK_DDDPRINT("split; match_start b=%ld,c=%ld, match_end b=%ld,c=%ld, prev_end b=%ld,c=%ld",
  29624. (long) match_start_boff, (long) match_start_coff,
  29625. (long) match_end_boff, (long) match_end_coff,
  29626. (long) prev_match_end_boff, (long) prev_match_end_coff));
  29627. duk_push_lstring(ctx,
  29628. (const char *) (DUK_HSTRING_GET_DATA(h_input) + prev_match_end_boff),
  29629. (duk_size_t) (match_start_boff - prev_match_end_boff));
  29630. duk_put_prop_index(ctx, 3, arr_idx);
  29631. arr_idx++;
  29632. if (arr_idx >= limit) {
  29633. goto hit_limit;
  29634. }
  29635. #ifdef DUK_USE_REGEXP_SUPPORT
  29636. if (is_regexp) {
  29637. duk_size_t i, len;
  29638. len = duk_get_length(ctx, 4);
  29639. for (i = 1; i < len; i++) {
  29640. DUK_ASSERT(i <= DUK_UARRIDX_MAX); /* cannot have >4G captures */
  29641. duk_get_prop_index(ctx, 4, (duk_uarridx_t) i);
  29642. duk_put_prop_index(ctx, 3, arr_idx);
  29643. arr_idx++;
  29644. if (arr_idx >= limit) {
  29645. goto hit_limit;
  29646. }
  29647. }
  29648. duk_pop(ctx);
  29649. /* lastIndex already set up for next match */
  29650. } else {
  29651. #else /* DUK_USE_REGEXP_SUPPORT */
  29652. { /* unconditionally */
  29653. #endif /* DUK_USE_REGEXP_SUPPORT */
  29654. /* no action */
  29655. }
  29656. prev_match_end_boff = match_end_boff;
  29657. prev_match_end_coff = match_end_coff;
  29658. continue;
  29659. } /* for */
  29660. /* Combined step 11 (empty string special case) and 14-15. */
  29661. DUK_DDD(DUK_DDDPRINT("split trailer; prev_end b=%ld,c=%ld",
  29662. (long) prev_match_end_boff, (long) prev_match_end_coff));
  29663. if (DUK_HSTRING_GET_CHARLEN(h_input) > 0 || !matched) {
  29664. /* Add trailer if:
  29665. * a) non-empty input
  29666. * b) empty input and no (zero size) match found (step 11)
  29667. */
  29668. duk_push_lstring(ctx,
  29669. (const char *) DUK_HSTRING_GET_DATA(h_input) + prev_match_end_boff,
  29670. (duk_size_t) (DUK_HSTRING_GET_BYTELEN(h_input) - prev_match_end_boff));
  29671. duk_put_prop_index(ctx, 3, arr_idx);
  29672. /* No arr_idx update or limit check */
  29673. }
  29674. return 1;
  29675. hit_limit:
  29676. #ifdef DUK_USE_REGEXP_SUPPORT
  29677. if (is_regexp) {
  29678. duk_pop(ctx);
  29679. }
  29680. #endif
  29681. return 1;
  29682. }
  29683. /*
  29684. * Various
  29685. */
  29686. #ifdef DUK_USE_REGEXP_SUPPORT
  29687. DUK_LOCAL void duk__to_regexp_helper(duk_context *ctx, duk_idx_t index, duk_bool_t force_new) {
  29688. duk_hobject *h;
  29689. /* Shared helper for match() steps 3-4, search() steps 3-4. */
  29690. DUK_ASSERT(index >= 0);
  29691. if (force_new) {
  29692. goto do_new;
  29693. }
  29694. h = duk_get_hobject_with_class(ctx, index, DUK_HOBJECT_CLASS_REGEXP);
  29695. if (!h) {
  29696. goto do_new;
  29697. }
  29698. return;
  29699. do_new:
  29700. duk_push_hobject_bidx(ctx, DUK_BIDX_REGEXP_CONSTRUCTOR);
  29701. duk_dup(ctx, index);
  29702. duk_new(ctx, 1); /* [ ... RegExp val ] -> [ ... res ] */
  29703. duk_replace(ctx, index);
  29704. }
  29705. #endif /* DUK_USE_REGEXP_SUPPORT */
  29706. #ifdef DUK_USE_REGEXP_SUPPORT
  29707. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_search(duk_context *ctx) {
  29708. duk_hthread *thr = (duk_hthread *) ctx;
  29709. /* Easiest way to implement the search required by the specification
  29710. * is to do a RegExp test() with lastIndex forced to zero. To avoid
  29711. * side effects on the argument, "clone" the RegExp if a RegExp was
  29712. * given as input.
  29713. *
  29714. * The global flag of the RegExp should be ignored; setting lastIndex
  29715. * to zero (which happens when "cloning" the RegExp) should have an
  29716. * equivalent effect.
  29717. */
  29718. DUK_ASSERT_TOP(ctx, 1);
  29719. (void) duk_push_this_coercible_to_string(ctx); /* at index 1 */
  29720. duk__to_regexp_helper(ctx, 0 /*index*/, 1 /*force_new*/);
  29721. /* stack[0] = regexp
  29722. * stack[1] = string
  29723. */
  29724. /* Avoid using RegExp.prototype methods, as they're writable and
  29725. * configurable and may have been changed.
  29726. */
  29727. duk_dup(ctx, 0);
  29728. duk_dup(ctx, 1); /* [ ... re_obj input ] */
  29729. duk_regexp_match(thr); /* -> [ ... res_obj ] */
  29730. if (!duk_is_object(ctx, -1)) {
  29731. duk_push_int(ctx, -1);
  29732. return 1;
  29733. }
  29734. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INDEX);
  29735. DUK_ASSERT(duk_is_number(ctx, -1));
  29736. return 1;
  29737. }
  29738. #else /* DUK_USE_REGEXP_SUPPORT */
  29739. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_search(duk_context *ctx) {
  29740. DUK_UNREF(ctx);
  29741. return DUK_RET_UNSUPPORTED_ERROR;
  29742. }
  29743. #endif /* DUK_USE_REGEXP_SUPPORT */
  29744. #ifdef DUK_USE_REGEXP_SUPPORT
  29745. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_match(duk_context *ctx) {
  29746. duk_hthread *thr = (duk_hthread *) ctx;
  29747. duk_bool_t global;
  29748. duk_int_t prev_last_index;
  29749. duk_int_t this_index;
  29750. duk_int_t arr_idx;
  29751. DUK_ASSERT_TOP(ctx, 1);
  29752. (void) duk_push_this_coercible_to_string(ctx);
  29753. duk__to_regexp_helper(ctx, 0 /*index*/, 0 /*force_new*/);
  29754. global = duk_get_prop_stridx_boolean(ctx, 0, DUK_STRIDX_GLOBAL, NULL);
  29755. DUK_ASSERT_TOP(ctx, 2);
  29756. /* stack[0] = regexp
  29757. * stack[1] = string
  29758. */
  29759. if (!global) {
  29760. duk_regexp_match(thr); /* -> [ res_obj ] */
  29761. return 1; /* return 'res_obj' */
  29762. }
  29763. /* Global case is more complex. */
  29764. /* [ regexp string ] */
  29765. duk_push_int(ctx, 0);
  29766. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LAST_INDEX);
  29767. duk_push_array(ctx);
  29768. /* [ regexp string res_arr ] */
  29769. prev_last_index = 0;
  29770. arr_idx = 0;
  29771. for (;;) {
  29772. DUK_ASSERT_TOP(ctx, 3);
  29773. duk_dup(ctx, 0);
  29774. duk_dup(ctx, 1);
  29775. duk_regexp_match(thr); /* -> [ ... regexp string ] -> [ ... res_obj ] */
  29776. if (!duk_is_object(ctx, -1)) {
  29777. duk_pop(ctx);
  29778. break;
  29779. }
  29780. duk_get_prop_stridx(ctx, 0, DUK_STRIDX_LAST_INDEX);
  29781. DUK_ASSERT(duk_is_number(ctx, -1));
  29782. this_index = duk_get_int(ctx, -1);
  29783. duk_pop(ctx);
  29784. if (this_index == prev_last_index) {
  29785. this_index++;
  29786. duk_push_int(ctx, this_index);
  29787. duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LAST_INDEX);
  29788. }
  29789. prev_last_index = this_index;
  29790. duk_get_prop_index(ctx, -1, 0); /* match string */
  29791. duk_put_prop_index(ctx, 2, arr_idx);
  29792. arr_idx++;
  29793. duk_pop(ctx); /* res_obj */
  29794. }
  29795. if (arr_idx == 0) {
  29796. duk_push_null(ctx);
  29797. }
  29798. return 1; /* return 'res_arr' or 'null' */
  29799. }
  29800. #else /* DUK_USE_REGEXP_SUPPORT */
  29801. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_match(duk_context *ctx) {
  29802. DUK_UNREF(ctx);
  29803. return DUK_RET_UNSUPPORTED_ERROR;
  29804. }
  29805. #endif /* DUK_USE_REGEXP_SUPPORT */
  29806. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_concat(duk_context *ctx) {
  29807. /* duk_concat() coerces arguments with ToString() in correct order */
  29808. (void) duk_push_this_coercible_to_string(ctx);
  29809. duk_insert(ctx, 0); /* this is relatively expensive */
  29810. duk_concat(ctx, duk_get_top(ctx));
  29811. return 1;
  29812. }
  29813. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_trim(duk_context *ctx) {
  29814. DUK_ASSERT_TOP(ctx, 0);
  29815. (void) duk_push_this_coercible_to_string(ctx);
  29816. duk_trim(ctx, 0);
  29817. DUK_ASSERT_TOP(ctx, 1);
  29818. return 1;
  29819. }
  29820. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_locale_compare(duk_context *ctx) {
  29821. duk_hstring *h1;
  29822. duk_hstring *h2;
  29823. duk_size_t h1_len, h2_len, prefix_len;
  29824. duk_small_int_t ret = 0;
  29825. duk_small_int_t rc;
  29826. /* The current implementation of localeCompare() is simply a codepoint
  29827. * by codepoint comparison, implemented with a simple string compare
  29828. * because UTF-8 should preserve codepoint ordering (assuming valid
  29829. * shortest UTF-8 encoding).
  29830. *
  29831. * The specification requires that the return value must be related
  29832. * to the sort order: e.g. negative means that 'this' comes before
  29833. * 'that' in sort order. We assume an ascending sort order.
  29834. */
  29835. /* XXX: could share code with duk_js_ops.c, duk_js_compare_helper */
  29836. h1 = duk_push_this_coercible_to_string(ctx);
  29837. DUK_ASSERT(h1 != NULL);
  29838. h2 = duk_to_hstring(ctx, 0);
  29839. DUK_ASSERT(h2 != NULL);
  29840. h1_len = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h1);
  29841. h2_len = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h2);
  29842. prefix_len = (h1_len <= h2_len ? h1_len : h2_len);
  29843. /* Zero size compare not an issue with DUK_MEMCMP. */
  29844. rc = (duk_small_int_t) DUK_MEMCMP((const char *) DUK_HSTRING_GET_DATA(h1),
  29845. (const char *) DUK_HSTRING_GET_DATA(h2),
  29846. prefix_len);
  29847. if (rc < 0) {
  29848. ret = -1;
  29849. goto done;
  29850. } else if (rc > 0) {
  29851. ret = 1;
  29852. goto done;
  29853. }
  29854. /* prefix matches, lengths matter now */
  29855. if (h1_len > h2_len) {
  29856. ret = 1;
  29857. goto done;
  29858. } else if (h1_len == h2_len) {
  29859. DUK_ASSERT(ret == 0);
  29860. goto done;
  29861. }
  29862. ret = -1;
  29863. goto done;
  29864. done:
  29865. duk_push_int(ctx, (duk_int_t) ret);
  29866. return 1;
  29867. }
  29868. #line 1 "duk_bi_thread.c"
  29869. /*
  29870. * Thread builtins
  29871. */
  29872. /* include removed: duk_internal.h */
  29873. /*
  29874. * Constructor
  29875. */
  29876. DUK_INTERNAL duk_ret_t duk_bi_thread_constructor(duk_context *ctx) {
  29877. duk_hthread *new_thr;
  29878. duk_hobject *func;
  29879. /* XXX: need a duk_require_func_or_lfunc_coerce() */
  29880. if (!duk_is_callable(ctx, 0)) {
  29881. return DUK_RET_TYPE_ERROR;
  29882. }
  29883. func = duk_require_hobject_or_lfunc_coerce(ctx, 0);
  29884. DUK_ASSERT(func != NULL);
  29885. duk_push_thread(ctx);
  29886. new_thr = (duk_hthread *) duk_get_hobject(ctx, -1);
  29887. DUK_ASSERT(new_thr != NULL);
  29888. new_thr->state = DUK_HTHREAD_STATE_INACTIVE;
  29889. /* push initial function call to new thread stack; this is
  29890. * picked up by resume().
  29891. */
  29892. duk_push_hobject((duk_context *) new_thr, func);
  29893. return 1; /* return thread */
  29894. }
  29895. /*
  29896. * Resume a thread.
  29897. *
  29898. * The thread must be in resumable state, either (a) new thread which hasn't
  29899. * yet started, or (b) a thread which has previously yielded. This method
  29900. * must be called from an Ecmascript function.
  29901. *
  29902. * Args:
  29903. * - thread
  29904. * - value
  29905. * - isError (defaults to false)
  29906. *
  29907. * Note: yield and resume handling is currently asymmetric.
  29908. */
  29909. DUK_INTERNAL duk_ret_t duk_bi_thread_resume(duk_context *ctx) {
  29910. duk_hthread *thr = (duk_hthread *) ctx;
  29911. duk_hthread *thr_resume;
  29912. duk_tval tv_tmp;
  29913. duk_tval *tv;
  29914. duk_hobject *func;
  29915. duk_hobject *caller_func;
  29916. duk_small_int_t is_error;
  29917. DUK_DDD(DUK_DDDPRINT("Duktape.Thread.resume(): thread=%!T, value=%!T, is_error=%!T",
  29918. (duk_tval *) duk_get_tval(ctx, 0),
  29919. (duk_tval *) duk_get_tval(ctx, 1),
  29920. (duk_tval *) duk_get_tval(ctx, 2)));
  29921. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING);
  29922. DUK_ASSERT(thr->heap->curr_thread == thr);
  29923. thr_resume = duk_require_hthread(ctx, 0);
  29924. is_error = (duk_small_int_t) duk_to_boolean(ctx, 2);
  29925. duk_set_top(ctx, 2);
  29926. /* [ thread value ] */
  29927. /*
  29928. * Thread state and calling context checks
  29929. */
  29930. if (thr->callstack_top < 2) {
  29931. DUK_DD(DUK_DDPRINT("resume state invalid: callstack should contain at least 2 entries (caller and Duktape.Thread.resume)"));
  29932. goto state_error;
  29933. }
  29934. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1) != NULL); /* us */
  29935. DUK_ASSERT(DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1)));
  29936. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2) != NULL); /* caller */
  29937. caller_func = DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2);
  29938. if (!DUK_HOBJECT_IS_COMPILEDFUNCTION(caller_func)) {
  29939. DUK_DD(DUK_DDPRINT("resume state invalid: caller must be Ecmascript code"));
  29940. goto state_error;
  29941. }
  29942. /* Note: there is no requirement that: 'thr->callstack_preventcount == 1'
  29943. * like for yield.
  29944. */
  29945. if (thr_resume->state != DUK_HTHREAD_STATE_INACTIVE &&
  29946. thr_resume->state != DUK_HTHREAD_STATE_YIELDED) {
  29947. DUK_DD(DUK_DDPRINT("resume state invalid: target thread must be INACTIVE or YIELDED"));
  29948. goto state_error;
  29949. }
  29950. DUK_ASSERT(thr_resume->state == DUK_HTHREAD_STATE_INACTIVE ||
  29951. thr_resume->state == DUK_HTHREAD_STATE_YIELDED);
  29952. /* Further state-dependent pre-checks */
  29953. if (thr_resume->state == DUK_HTHREAD_STATE_YIELDED) {
  29954. /* no pre-checks now, assume a previous yield() has left things in
  29955. * tip-top shape (longjmp handler will assert for these).
  29956. */
  29957. } else {
  29958. DUK_ASSERT(thr_resume->state == DUK_HTHREAD_STATE_INACTIVE);
  29959. if ((thr_resume->callstack_top != 0) ||
  29960. (thr_resume->valstack_top - thr_resume->valstack != 1)) {
  29961. goto state_invalid_initial;
  29962. }
  29963. tv = &thr_resume->valstack_top[-1];
  29964. DUK_ASSERT(tv >= thr_resume->valstack && tv < thr_resume->valstack_top);
  29965. if (!DUK_TVAL_IS_OBJECT(tv)) {
  29966. goto state_invalid_initial;
  29967. }
  29968. func = DUK_TVAL_GET_OBJECT(tv);
  29969. DUK_ASSERT(func != NULL);
  29970. if (!DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) {
  29971. /* Note: cannot be a bound function either right now,
  29972. * this would be easy to relax though.
  29973. */
  29974. goto state_invalid_initial;
  29975. }
  29976. }
  29977. /*
  29978. * The error object has been augmented with a traceback and other
  29979. * info from its creation point -- usually another thread. The
  29980. * error handler is called here right before throwing, but it also
  29981. * runs in the resumer's thread. It might be nice to get a traceback
  29982. * from the resumee but this is not the case now.
  29983. */
  29984. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  29985. if (is_error) {
  29986. DUK_ASSERT_TOP(ctx, 2); /* value (error) is at stack top */
  29987. duk_err_augment_error_throw(thr); /* in resumer's context */
  29988. }
  29989. #endif
  29990. #ifdef DUK_USE_DEBUG
  29991. if (is_error) {
  29992. DUK_DDD(DUK_DDDPRINT("RESUME ERROR: thread=%!T, value=%!T",
  29993. (duk_tval *) duk_get_tval(ctx, 0),
  29994. (duk_tval *) duk_get_tval(ctx, 1)));
  29995. } else if (thr_resume->state == DUK_HTHREAD_STATE_YIELDED) {
  29996. DUK_DDD(DUK_DDDPRINT("RESUME NORMAL: thread=%!T, value=%!T",
  29997. (duk_tval *) duk_get_tval(ctx, 0),
  29998. (duk_tval *) duk_get_tval(ctx, 1)));
  29999. } else {
  30000. DUK_DDD(DUK_DDDPRINT("RESUME INITIAL: thread=%!T, value=%!T",
  30001. (duk_tval *) duk_get_tval(ctx, 0),
  30002. (duk_tval *) duk_get_tval(ctx, 1)));
  30003. }
  30004. #endif
  30005. thr->heap->lj.type = DUK_LJ_TYPE_RESUME;
  30006. /* lj value2: thread */
  30007. DUK_ASSERT(thr->valstack_bottom < thr->valstack_top);
  30008. DUK_TVAL_SET_TVAL(&tv_tmp, &thr->heap->lj.value2);
  30009. DUK_TVAL_SET_TVAL(&thr->heap->lj.value2, &thr->valstack_bottom[0]);
  30010. DUK_TVAL_INCREF(thr, &thr->heap->lj.value2);
  30011. DUK_TVAL_DECREF(thr, &tv_tmp);
  30012. /* lj value1: value */
  30013. DUK_ASSERT(thr->valstack_bottom + 1 < thr->valstack_top);
  30014. DUK_TVAL_SET_TVAL(&tv_tmp, &thr->heap->lj.value1);
  30015. DUK_TVAL_SET_TVAL(&thr->heap->lj.value1, &thr->valstack_bottom[1]);
  30016. DUK_TVAL_INCREF(thr, &thr->heap->lj.value1);
  30017. DUK_TVAL_DECREF(thr, &tv_tmp);
  30018. thr->heap->lj.iserror = is_error;
  30019. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* call is from executor, so we know we have a jmpbuf */
  30020. duk_err_longjmp(thr); /* execution resumes in bytecode executor */
  30021. return 0; /* never here */
  30022. state_invalid_initial:
  30023. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "invalid initial thread state/stack");
  30024. return 0; /* never here */
  30025. state_error:
  30026. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "invalid state for resume");
  30027. return 0; /* never here */
  30028. }
  30029. /*
  30030. * Yield the current thread.
  30031. *
  30032. * The thread must be in yieldable state: it must have a resumer, and there
  30033. * must not be any yield-preventing calls (native calls and constructor calls,
  30034. * currently) in the thread's call stack (otherwise a resume would not be
  30035. * possible later). This method must be called from an Ecmascript function.
  30036. *
  30037. * Args:
  30038. * - value
  30039. * - isError (defaults to false)
  30040. *
  30041. * Note: yield and resume handling is currently asymmetric.
  30042. */
  30043. DUK_INTERNAL duk_ret_t duk_bi_thread_yield(duk_context *ctx) {
  30044. duk_hthread *thr = (duk_hthread *) ctx;
  30045. duk_tval tv_tmp;
  30046. duk_hobject *caller_func;
  30047. duk_small_int_t is_error;
  30048. DUK_DDD(DUK_DDDPRINT("Duktape.Thread.yield(): value=%!T, is_error=%!T",
  30049. (duk_tval *) duk_get_tval(ctx, 0),
  30050. (duk_tval *) duk_get_tval(ctx, 1)));
  30051. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING);
  30052. DUK_ASSERT(thr->heap->curr_thread == thr);
  30053. is_error = (duk_small_int_t) duk_to_boolean(ctx, 1);
  30054. duk_set_top(ctx, 1);
  30055. /* [ value ] */
  30056. /*
  30057. * Thread state and calling context checks
  30058. */
  30059. if (!thr->resumer) {
  30060. DUK_DD(DUK_DDPRINT("yield state invalid: current thread must have a resumer"));
  30061. goto state_error;
  30062. }
  30063. DUK_ASSERT(thr->resumer->state == DUK_HTHREAD_STATE_RESUMED);
  30064. if (thr->callstack_top < 2) {
  30065. DUK_DD(DUK_DDPRINT("yield state invalid: callstack should contain at least 2 entries (caller and Duktape.Thread.yield)"));
  30066. goto state_error;
  30067. }
  30068. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1) != NULL); /* us */
  30069. DUK_ASSERT(DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1)));
  30070. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2) != NULL); /* caller */
  30071. caller_func = DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2);
  30072. if (!DUK_HOBJECT_IS_COMPILEDFUNCTION(caller_func)) {
  30073. DUK_DD(DUK_DDPRINT("yield state invalid: caller must be Ecmascript code"));
  30074. goto state_error;
  30075. }
  30076. DUK_ASSERT(thr->callstack_preventcount >= 1); /* should never be zero, because we (Duktape.Thread.yield) are on the stack */
  30077. if (thr->callstack_preventcount != 1) {
  30078. /* Note: the only yield-preventing call is Duktape.Thread.yield(), hence check for 1, not 0 */
  30079. DUK_DD(DUK_DDPRINT("yield state invalid: there must be no yield-preventing calls in current thread callstack (preventcount is %ld)",
  30080. (long) thr->callstack_preventcount));
  30081. goto state_error;
  30082. }
  30083. /*
  30084. * The error object has been augmented with a traceback and other
  30085. * info from its creation point -- usually the current thread.
  30086. * The error handler, however, is called right before throwing
  30087. * and runs in the yielder's thread.
  30088. */
  30089. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  30090. if (is_error) {
  30091. DUK_ASSERT_TOP(ctx, 1); /* value (error) is at stack top */
  30092. duk_err_augment_error_throw(thr); /* in yielder's context */
  30093. }
  30094. #endif
  30095. #ifdef DUK_USE_DEBUG
  30096. if (is_error) {
  30097. DUK_DDD(DUK_DDDPRINT("YIELD ERROR: value=%!T",
  30098. (duk_tval *) duk_get_tval(ctx, 0)));
  30099. } else {
  30100. DUK_DDD(DUK_DDDPRINT("YIELD NORMAL: value=%!T",
  30101. (duk_tval *) duk_get_tval(ctx, 0)));
  30102. }
  30103. #endif
  30104. /*
  30105. * Process yield
  30106. *
  30107. * After longjmp(), processing continues in bytecode executor longjmp
  30108. * handler, which will e.g. update thr->resumer to NULL.
  30109. */
  30110. thr->heap->lj.type = DUK_LJ_TYPE_YIELD;
  30111. /* lj value1: value */
  30112. DUK_ASSERT(thr->valstack_bottom < thr->valstack_top);
  30113. DUK_TVAL_SET_TVAL(&tv_tmp, &thr->heap->lj.value1);
  30114. DUK_TVAL_SET_TVAL(&thr->heap->lj.value1, &thr->valstack_bottom[0]);
  30115. DUK_TVAL_INCREF(thr, &thr->heap->lj.value1);
  30116. DUK_TVAL_DECREF(thr, &tv_tmp);
  30117. thr->heap->lj.iserror = is_error;
  30118. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* call is from executor, so we know we have a jmpbuf */
  30119. duk_err_longjmp(thr); /* execution resumes in bytecode executor */
  30120. return 0; /* never here */
  30121. state_error:
  30122. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "invalid state for yield");
  30123. return 0; /* never here */
  30124. }
  30125. DUK_INTERNAL duk_ret_t duk_bi_thread_current(duk_context *ctx) {
  30126. duk_push_current_thread(ctx);
  30127. return 1;
  30128. }
  30129. #line 1 "duk_bi_thrower.c"
  30130. /*
  30131. * Type error thrower, E5 Section 13.2.3.
  30132. */
  30133. /* include removed: duk_internal.h */
  30134. DUK_INTERNAL duk_ret_t duk_bi_type_error_thrower(duk_context *ctx) {
  30135. DUK_UNREF(ctx);
  30136. return DUK_RET_TYPE_ERROR;
  30137. }
  30138. #line 1 "duk_debug_fixedbuffer.c"
  30139. /*
  30140. * Fixed buffer helper useful for debugging, requires no allocation
  30141. * which is critical for debugging.
  30142. */
  30143. /* include removed: duk_internal.h */
  30144. #ifdef DUK_USE_DEBUG
  30145. DUK_INTERNAL void duk_fb_put_bytes(duk_fixedbuffer *fb, duk_uint8_t *buffer, duk_size_t length) {
  30146. duk_size_t avail;
  30147. duk_size_t copylen;
  30148. avail = (fb->offset >= fb->length ? (duk_size_t) 0 : (duk_size_t) (fb->length - fb->offset));
  30149. if (length > avail) {
  30150. copylen = avail;
  30151. fb->truncated = 1;
  30152. } else {
  30153. copylen = length;
  30154. }
  30155. DUK_MEMCPY(fb->buffer + fb->offset, buffer, copylen);
  30156. fb->offset += copylen;
  30157. }
  30158. DUK_INTERNAL void duk_fb_put_byte(duk_fixedbuffer *fb, duk_uint8_t x) {
  30159. duk_fb_put_bytes(fb, &x, 1);
  30160. }
  30161. DUK_INTERNAL void duk_fb_put_cstring(duk_fixedbuffer *fb, const char *x) {
  30162. duk_fb_put_bytes(fb, (duk_uint8_t *) x, (duk_size_t) DUK_STRLEN(x));
  30163. }
  30164. DUK_INTERNAL void duk_fb_sprintf(duk_fixedbuffer *fb, const char *fmt, ...) {
  30165. duk_size_t avail;
  30166. va_list ap;
  30167. va_start(ap, fmt);
  30168. avail = (fb->offset >= fb->length ? (duk_size_t) 0 : (duk_size_t) (fb->length - fb->offset));
  30169. if (avail > 0) {
  30170. duk_int_t res = (duk_int_t) DUK_VSNPRINTF((char *) (fb->buffer + fb->offset), avail, fmt, ap);
  30171. if (res < 0) {
  30172. /* error */
  30173. } else if ((duk_size_t) res >= avail) {
  30174. /* (maybe) truncated */
  30175. fb->offset += avail;
  30176. if ((duk_size_t) res > avail) {
  30177. /* actual chars dropped (not just NUL term) */
  30178. fb->truncated = 1;
  30179. }
  30180. } else {
  30181. /* normal */
  30182. fb->offset += res;
  30183. }
  30184. }
  30185. va_end(ap);
  30186. }
  30187. DUK_INTERNAL void duk_fb_put_funcptr(duk_fixedbuffer *fb, duk_uint8_t *fptr, duk_size_t fptr_size) {
  30188. char buf[64+1];
  30189. duk_debug_format_funcptr(buf, sizeof(buf), fptr, fptr_size);
  30190. buf[sizeof(buf) - 1] = (char) 0;
  30191. duk_fb_put_cstring(fb, buf);
  30192. }
  30193. DUK_INTERNAL duk_bool_t duk_fb_is_full(duk_fixedbuffer *fb) {
  30194. return (fb->offset >= fb->length);
  30195. }
  30196. #endif /* DUK_USE_DEBUG */
  30197. #line 1 "duk_debug_heap.c"
  30198. /*
  30199. * Debug dumping of duk_heap.
  30200. */
  30201. /* include removed: duk_internal.h */
  30202. #ifdef DUK_USE_DEBUG
  30203. #if 0 /*unused*/
  30204. DUK_LOCAL void duk__sanitize_snippet(char *buf, duk_size_t buf_size, duk_hstring *str) {
  30205. duk_size_t i;
  30206. duk_size_t nchars;
  30207. duk_size_t maxchars;
  30208. duk_uint8_t *data;
  30209. DUK_MEMZERO(buf, buf_size);
  30210. maxchars = (duk_size_t) (buf_size - 1);
  30211. data = DUK_HSTRING_GET_DATA(str);
  30212. nchars = ((duk_size_t) str->blen < maxchars ? (duk_size_t) str->blen : maxchars);
  30213. for (i = 0; i < nchars; i++) {
  30214. duk_small_int_t c = (duk_small_int_t) data[i];
  30215. if (c < 0x20 || c > 0x7e) {
  30216. c = '.';
  30217. }
  30218. buf[i] = (char) c;
  30219. }
  30220. }
  30221. #endif
  30222. #if 0
  30223. DUK_LOCAL const char *duk__get_heap_type_string(duk_heaphdr *hdr) {
  30224. switch (DUK_HEAPHDR_GET_TYPE(hdr)) {
  30225. case DUK_HTYPE_STRING:
  30226. return "string";
  30227. case DUK_HTYPE_OBJECT:
  30228. return "object";
  30229. case DUK_HTYPE_BUFFER:
  30230. return "buffer";
  30231. default:
  30232. return "???";
  30233. }
  30234. }
  30235. #endif
  30236. #if 0
  30237. DUK_LOCAL void duk__dump_indented(duk_heaphdr *obj, int index) {
  30238. DUK_UNREF(obj);
  30239. DUK_UNREF(index);
  30240. DUK_UNREF(duk__get_heap_type_string);
  30241. #ifdef DUK_USE_REFERENCE_COUNTING
  30242. DUK_D(DUK_DPRINT(" [%ld]: %p %s (flags: 0x%08lx, ref: %ld) -> %!O",
  30243. (long) index,
  30244. (void *) obj,
  30245. (const char *) duk__get_heap_type_string(obj),
  30246. (unsigned long) DUK_HEAPHDR_GET_FLAGS(obj),
  30247. (long) DUK_HEAPHDR_GET_REFCOUNT(obj),
  30248. (duk_heaphdr *) obj));
  30249. #else
  30250. DUK_D(DUK_DPRINT(" [%ld]: %p %s (flags: 0x%08lx) -> %!O",
  30251. (long) index,
  30252. (void *) obj,
  30253. (const char *) duk__get_heap_type_string(obj),
  30254. (unsigned long) DUK_HEAPHDR_GET_FLAGS(obj),
  30255. (duk_heaphdr *) obj));
  30256. #endif
  30257. }
  30258. #endif
  30259. #if 0 /*unused*/
  30260. DUK_LOCAL void duk__dump_heaphdr_list(duk_heap *heap, duk_heaphdr *root, const char *name) {
  30261. duk_int_t count;
  30262. duk_heaphdr *curr;
  30263. DUK_UNREF(heap);
  30264. DUK_UNREF(name);
  30265. count = 0;
  30266. curr = root;
  30267. while (curr) {
  30268. count++;
  30269. curr = DUK_HEAPHDR_GET_NEXT(curr);
  30270. }
  30271. DUK_D(DUK_DPRINT("%s, %ld objects", (const char *) name, (long) count));
  30272. count = 0;
  30273. curr = root;
  30274. while (curr) {
  30275. count++;
  30276. duk__dump_indented(curr, count);
  30277. curr = DUK_HEAPHDR_GET_NEXT(curr);
  30278. }
  30279. }
  30280. #endif
  30281. #if 0 /*unused*/
  30282. DUK_LOCAL void duk__dump_stringtable(duk_heap *heap) {
  30283. duk_uint_fast32_t i;
  30284. char buf[64+1];
  30285. DUK_D(DUK_DPRINT("stringtable %p, used %ld, size %ld, load %ld%%",
  30286. (void *) heap->strtable,
  30287. (long) heap->st_used,
  30288. (long) heap->st_size,
  30289. (long) (((double) heap->st_used) / ((double) heap->st_size) * 100.0)));
  30290. for (i = 0; i < (duk_uint_fast32_t) heap->st_size; i++) {
  30291. duk_hstring *e = heap->strtable[i];
  30292. if (!e) {
  30293. DUK_D(DUK_DPRINT(" [%ld]: NULL", (long) i));
  30294. } else if (e == DUK_STRTAB_DELETED_MARKER(heap)) {
  30295. DUK_D(DUK_DPRINT(" [%ld]: DELETED", (long) i));
  30296. } else {
  30297. duk__sanitize_snippet(buf, sizeof(buf), e);
  30298. #ifdef DUK_USE_REFERENCE_COUNTING
  30299. DUK_D(DUK_DPRINT(" [%ld]: %p (flags: 0x%08lx, ref: %ld) '%s', strhash=0x%08lx, blen=%ld, clen=%ld, "
  30300. "arridx=%ld, internal=%ld, reserved_word=%ld, strict_reserved_word=%ld, eval_or_arguments=%ld",
  30301. (long) i,
  30302. (void *) e,
  30303. (unsigned long) DUK_HEAPHDR_GET_FLAGS((duk_heaphdr *) e),
  30304. (long) DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) e),
  30305. (const char *) buf,
  30306. (unsigned long) e->hash,
  30307. (long) e->blen,
  30308. (long) e->clen,
  30309. (long) (DUK_HSTRING_HAS_ARRIDX(e) ? 1 : 0),
  30310. (long) (DUK_HSTRING_HAS_INTERNAL(e) ? 1 : 0),
  30311. (long) (DUK_HSTRING_HAS_RESERVED_WORD(e) ? 1 : 0),
  30312. (long) (DUK_HSTRING_HAS_STRICT_RESERVED_WORD(e) ? 1 : 0),
  30313. (long) (DUK_HSTRING_HAS_EVAL_OR_ARGUMENTS(e) ? 1 : 0)));
  30314. #else
  30315. DUK_D(DUK_DPRINT(" [%ld]: %p (flags: 0x%08lx) '%s', strhash=0x%08lx, blen=%ld, clen=%ld, "
  30316. "arridx=%ld, internal=%ld, reserved_word=%ld, strict_reserved_word=%ld, eval_or_arguments=%ld",
  30317. (long) i,
  30318. (void *) e,
  30319. (unsigned long) DUK_HEAPHDR_GET_FLAGS((duk_heaphdr *) e),
  30320. (const char *) buf,
  30321. (long) e->hash,
  30322. (long) e->blen,
  30323. (long) e->clen,
  30324. (long) (DUK_HSTRING_HAS_ARRIDX(e) ? 1 : 0),
  30325. (long) (DUK_HSTRING_HAS_INTERNAL(e) ? 1 : 0),
  30326. (long) (DUK_HSTRING_HAS_RESERVED_WORD(e) ? 1 : 0),
  30327. (long) (DUK_HSTRING_HAS_STRICT_RESERVED_WORD(e) ? 1 : 0),
  30328. (long) (DUK_HSTRING_HAS_EVAL_OR_ARGUMENTS(e) ? 1 : 0)));
  30329. #endif
  30330. }
  30331. }
  30332. }
  30333. #endif
  30334. #if 0 /*unused*/
  30335. DUK_LOCAL void duk__dump_strcache(duk_heap *heap) {
  30336. duk_uint_fast32_t i;
  30337. char buf[64+1];
  30338. DUK_D(DUK_DPRINT("stringcache"));
  30339. for (i = 0; i < (duk_uint_fast32_t) DUK_HEAP_STRCACHE_SIZE; i++) {
  30340. duk_strcache *c = &heap->strcache[i];
  30341. if (!c->h) {
  30342. DUK_D(DUK_DPRINT(" [%ld]: bidx=%ld, cidx=%ld, str=NULL",
  30343. (long) i, (long) c->bidx, (long) c->cidx));
  30344. } else {
  30345. duk__sanitize_snippet(buf, sizeof(buf), c->h);
  30346. DUK_D(DUK_DPRINT(" [%ld]: bidx=%ld cidx=%ld str=%s",
  30347. (long) i, (long) c->bidx, (long) c->cidx, (const char *) buf));
  30348. }
  30349. }
  30350. }
  30351. #endif
  30352. #if 0 /*unused*/
  30353. DUK_INTERNAL void duk_debug_dump_heap(duk_heap *heap) {
  30354. char buf[64+1];
  30355. DUK_D(DUK_DPRINT("=== heap %p ===", (void *) heap));
  30356. DUK_D(DUK_DPRINT(" flags: 0x%08lx", (unsigned long) heap->flags));
  30357. /* Note: there is no standard formatter for function pointers */
  30358. #ifdef DUK_USE_GCC_PRAGMAS
  30359. #pragma GCC diagnostic push
  30360. #pragma GCC diagnostic ignored "-pedantic"
  30361. #endif
  30362. duk_debug_format_funcptr(buf, sizeof(buf), (duk_uint8_t *) &heap->alloc_func, sizeof(heap->alloc_func));
  30363. DUK_D(DUK_DPRINT(" alloc_func: %s", (const char *) buf));
  30364. duk_debug_format_funcptr(buf, sizeof(buf), (duk_uint8_t *) &heap->realloc_func, sizeof(heap->realloc_func));
  30365. DUK_D(DUK_DPRINT(" realloc_func: %s", (const char *) buf));
  30366. duk_debug_format_funcptr(buf, sizeof(buf), (duk_uint8_t *) &heap->free_func, sizeof(heap->free_func));
  30367. DUK_D(DUK_DPRINT(" free_func: %s", (const char *) buf));
  30368. duk_debug_format_funcptr(buf, sizeof(buf), (duk_uint8_t *) &heap->fatal_func, sizeof(heap->fatal_func));
  30369. DUK_D(DUK_DPRINT(" fatal_func: %s", (const char *) buf));
  30370. #ifdef DUK_USE_GCC_PRAGMAS
  30371. #pragma GCC diagnostic pop
  30372. #endif
  30373. DUK_D(DUK_DPRINT(" heap_udata: %p", (void *) heap->heap_udata));
  30374. #ifdef DUK_USE_MARK_AND_SWEEP
  30375. #ifdef DUK_USE_VOLUNTARY_GC
  30376. DUK_D(DUK_DPRINT(" mark-and-sweep trig counter: %ld", (long) heap->mark_and_sweep_trigger_counter));
  30377. #endif
  30378. DUK_D(DUK_DPRINT(" mark-and-sweep rec depth: %ld", (long) heap->mark_and_sweep_recursion_depth));
  30379. DUK_D(DUK_DPRINT(" mark-and-sweep base flags: 0x%08lx", (unsigned long) heap->mark_and_sweep_base_flags));
  30380. #endif
  30381. DUK_D(DUK_DPRINT(" lj.jmpbuf_ptr: %p", (void *) heap->lj.jmpbuf_ptr));
  30382. DUK_D(DUK_DPRINT(" lj.type: %ld", (long) heap->lj.type));
  30383. DUK_D(DUK_DPRINT(" lj.value1: %!T", (duk_tval *) &heap->lj.value1));
  30384. DUK_D(DUK_DPRINT(" lj.value2: %!T", (duk_tval *) &heap->lj.value2));
  30385. DUK_D(DUK_DPRINT(" lj.iserror: %ld", (long) heap->lj.iserror));
  30386. DUK_D(DUK_DPRINT(" handling_error: %ld", (long) heap->handling_error));
  30387. DUK_D(DUK_DPRINT(" heap_thread: %!@O", (duk_heaphdr *) heap->heap_thread));
  30388. DUK_D(DUK_DPRINT(" curr_thread: %!@O", (duk_heaphdr *) heap->curr_thread));
  30389. DUK_D(DUK_DPRINT(" heap_object: %!@O", (duk_heaphdr *) heap->heap_object));
  30390. DUK_D(DUK_DPRINT(" call_recursion_depth: %ld", (long) heap->call_recursion_depth));
  30391. DUK_D(DUK_DPRINT(" call_recursion_limit: %ld", (long) heap->call_recursion_limit));
  30392. DUK_D(DUK_DPRINT(" hash_seed: 0x%08lx", (unsigned long) heap->hash_seed));
  30393. DUK_D(DUK_DPRINT(" rnd_state: 0x%08lx", (unsigned long) heap->rnd_state));
  30394. duk__dump_strcache(heap);
  30395. duk__dump_heaphdr_list(heap, heap->heap_allocated, "heap allocated");
  30396. #ifdef DUK_USE_REFERENCE_COUNTING
  30397. duk__dump_heaphdr_list(heap, heap->refzero_list, "refcounting refzero list");
  30398. #endif
  30399. #ifdef DUK_USE_MARK_AND_SWEEP
  30400. duk__dump_heaphdr_list(heap, heap->finalize_list, "mark-and-sweep finalize list");
  30401. #endif
  30402. duk__dump_stringtable(heap);
  30403. /* heap->strs: not worth dumping */
  30404. }
  30405. #endif
  30406. #endif /* DUK_USE_DEBUG */
  30407. #line 1 "duk_debug_vsnprintf.c"
  30408. /*
  30409. * Custom formatter for debug printing, allowing Duktape specific data
  30410. * structures (such as tagged values and heap objects) to be printed with
  30411. * a nice format string. Because debug printing should not affect execution
  30412. * state, formatting here must be independent of execution (see implications
  30413. * below) and must not allocate memory.
  30414. *
  30415. * Custom format tags begin with a '%!' to safely distinguish them from
  30416. * standard format tags. The following conversions are supported:
  30417. *
  30418. * %!T tagged value (duk_tval *)
  30419. * %!O heap object (duk_heaphdr *)
  30420. * %!I decoded bytecode instruction
  30421. * %!C bytecode instruction opcode name (arg is long)
  30422. *
  30423. * Everything is serialized in a JSON-like manner. The default depth is one
  30424. * level, internal prototype is not followed, and internal properties are not
  30425. * serialized. The following modifiers change this behavior:
  30426. *
  30427. * @ print pointers
  30428. * # print binary representations (where applicable)
  30429. * d deep traversal of own properties (not prototype)
  30430. * p follow prototype chain (useless without 'd')
  30431. * i include internal properties (other than prototype)
  30432. * x hexdump buffers
  30433. * h heavy formatting
  30434. *
  30435. * For instance, the following serializes objects recursively, but does not
  30436. * follow the prototype chain nor print internal properties: "%!dO".
  30437. *
  30438. * Notes:
  30439. *
  30440. * * Standard snprintf return value semantics seem to vary. This
  30441. * implementation returns the number of bytes it actually wrote
  30442. * (excluding the null terminator). If retval == buffer size,
  30443. * output was truncated (except for corner cases).
  30444. *
  30445. * * Output format is intentionally different from Ecmascript
  30446. * formatting requirements, as formatting here serves debugging
  30447. * of internals.
  30448. *
  30449. * * Depth checking (and updating) is done in each type printer
  30450. * separately, to allow them to call each other freely.
  30451. *
  30452. * * Some pathological structures might take ages to print (e.g.
  30453. * self recursion with 100 properties pointing to the object
  30454. * itself). To guard against these, each printer also checks
  30455. * whether the output buffer is full; if so, early exit.
  30456. *
  30457. * * Reference loops are detected using a loop stack.
  30458. */
  30459. /* include removed: duk_internal.h */
  30460. #ifdef DUK_USE_DEBUG
  30461. #include <stdio.h>
  30462. #include <stdarg.h>
  30463. #include <string.h>
  30464. /* list of conversion specifiers that terminate a format tag;
  30465. * this is unfortunately guesswork.
  30466. */
  30467. #define DUK__ALLOWED_STANDARD_SPECIFIERS "diouxXeEfFgGaAcsCSpnm"
  30468. /* maximum length of standard format tag that we support */
  30469. #define DUK__MAX_FORMAT_TAG_LENGTH 32
  30470. /* heapobj recursion depth when deep printing is selected */
  30471. #define DUK__DEEP_DEPTH_LIMIT 8
  30472. /* maximum recursion depth for loop detection stacks */
  30473. #define DUK__LOOP_STACK_DEPTH 256
  30474. /* must match bytecode defines now; build autogenerate? */
  30475. DUK_LOCAL const char *duk__bc_optab[64] = {
  30476. "LDREG", "STREG", "LDCONST", "LDINT", "LDINTX", "MPUTOBJ", "MPUTOBJI", "MPUTARR", "MPUTARRI", "NEW",
  30477. "NEWI", "REGEXP", "CSREG", "CSREGI", "GETVAR", "PUTVAR", "DECLVAR", "DELVAR", "CSVAR", "CSVARI",
  30478. "CLOSURE", "GETPROP", "PUTPROP", "DELPROP", "CSPROP", "CSPROPI", "ADD", "SUB", "MUL", "DIV",
  30479. "MOD", "BAND", "BOR", "BXOR", "BASL", "BLSR", "BASR", "EQ", "NEQ", "SEQ",
  30480. "SNEQ", "GT", "GE", "LT", "LE", "IF", "JUMP", "RETURN", "CALL", "CALLI",
  30481. "TRYCATCH", "EXTRA", "PREINCR", "PREDECR", "POSTINCR", "POSTDECR", "PREINCV", "PREDECV", "POSTINCV", "POSTDECV",
  30482. "PREINCP", "PREDECP", "POSTINCP", "POSTDECP"
  30483. };
  30484. DUK_LOCAL const char *duk__bc_extraoptab[256] = {
  30485. "NOP", "INVALID", "LDTHIS", "LDUNDEF", "LDNULL", "LDTRUE", "LDFALSE", "NEWOBJ", "NEWARR", "SETALEN",
  30486. "TYPEOF", "TYPEOFID", "INITENUM", "NEXTENUM", "INITSET", "INITSETI", "INITGET", "INITGETI", "ENDTRY", "ENDCATCH",
  30487. "ENDFIN", "THROW", "INVLHS", "UNM", "UNP", "DEBUGGER", "BREAK", "CONTINUE", "BNOT", "LNOT",
  30488. "INSTOF", "IN", "LABEL", "ENDLABEL", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30489. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30490. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30491. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30492. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30493. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30494. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30495. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30496. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30497. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30498. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30499. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30500. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30501. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30502. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30503. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30504. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30505. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30506. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30507. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30508. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30509. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "XXX",
  30510. "XXX", "XXX", "XXX", "XXX", "XXX", "XXX"
  30511. };
  30512. typedef struct duk__dprint_state duk__dprint_state;
  30513. struct duk__dprint_state {
  30514. duk_fixedbuffer *fb;
  30515. /* loop_stack_index could be perhaps be replaced by 'depth', but it's nice
  30516. * to not couple these two mechanisms unnecessarily.
  30517. */
  30518. duk_hobject *loop_stack[DUK__LOOP_STACK_DEPTH];
  30519. duk_int_t loop_stack_index;
  30520. duk_int_t loop_stack_limit;
  30521. duk_int_t depth;
  30522. duk_int_t depth_limit;
  30523. duk_bool_t pointer;
  30524. duk_bool_t heavy;
  30525. duk_bool_t binary;
  30526. duk_bool_t follow_proto;
  30527. duk_bool_t internal;
  30528. duk_bool_t hexdump;
  30529. };
  30530. /* helpers */
  30531. DUK_LOCAL_DECL void duk__print_hstring(duk__dprint_state *st, duk_hstring *k, duk_bool_t quotes);
  30532. DUK_LOCAL_DECL void duk__print_hobject(duk__dprint_state *st, duk_hobject *h);
  30533. DUK_LOCAL_DECL void duk__print_hbuffer(duk__dprint_state *st, duk_hbuffer *h);
  30534. DUK_LOCAL_DECL void duk__print_tval(duk__dprint_state *st, duk_tval *tv);
  30535. DUK_LOCAL_DECL void duk__print_instr(duk__dprint_state *st, duk_instr_t ins);
  30536. DUK_LOCAL_DECL void duk__print_heaphdr(duk__dprint_state *st, duk_heaphdr *h);
  30537. DUK_LOCAL_DECL void duk__print_shared_heaphdr(duk__dprint_state *st, duk_heaphdr *h);
  30538. DUK_LOCAL_DECL void duk__print_shared_heaphdr_string(duk__dprint_state *st, duk_heaphdr_string *h);
  30539. DUK_LOCAL void duk__print_shared_heaphdr(duk__dprint_state *st, duk_heaphdr *h) {
  30540. duk_fixedbuffer *fb = st->fb;
  30541. if (st->heavy) {
  30542. duk_fb_sprintf(fb, "(%p)", (void *) h);
  30543. }
  30544. if (!h) {
  30545. return;
  30546. }
  30547. if (st->binary) {
  30548. duk_size_t i;
  30549. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_LBRACKET);
  30550. for (i = 0; i < (duk_size_t) sizeof(*h); i++) {
  30551. duk_fb_sprintf(fb, "%02lx", (unsigned long) ((duk_uint8_t *)h)[i]);
  30552. }
  30553. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_RBRACKET);
  30554. }
  30555. #ifdef DUK_USE_REFERENCE_COUNTING /* currently implicitly also DUK_USE_DOUBLE_LINKED_HEAP */
  30556. if (st->heavy) {
  30557. duk_fb_sprintf(fb, "[h_next=%p,h_prev=%p,h_refcount=%lu,h_flags=%08lx,type=%ld,"
  30558. "reachable=%ld,temproot=%ld,finalizable=%ld,finalized=%ld]",
  30559. (void *) DUK_HEAPHDR_GET_NEXT(NULL, h),
  30560. (void *) DUK_HEAPHDR_GET_PREV(NULL, h),
  30561. (unsigned long) DUK_HEAPHDR_GET_REFCOUNT(h),
  30562. (unsigned long) DUK_HEAPHDR_GET_FLAGS(h),
  30563. (long) DUK_HEAPHDR_GET_TYPE(h),
  30564. (long) (DUK_HEAPHDR_HAS_REACHABLE(h) ? 1 : 0),
  30565. (long) (DUK_HEAPHDR_HAS_TEMPROOT(h) ? 1 : 0),
  30566. (long) (DUK_HEAPHDR_HAS_FINALIZABLE(h) ? 1 : 0),
  30567. (long) (DUK_HEAPHDR_HAS_FINALIZED(h) ? 1 : 0));
  30568. }
  30569. #else
  30570. if (st->heavy) {
  30571. duk_fb_sprintf(fb, "[h_next=%p,h_flags=%08lx,type=%ld,reachable=%ld,temproot=%ld,finalizable=%ld,finalized=%ld]",
  30572. (void *) DUK_HEAPHDR_GET_NEXT(NULL, h),
  30573. (unsigned long) DUK_HEAPHDR_GET_FLAGS(h),
  30574. (long) DUK_HEAPHDR_GET_TYPE(h),
  30575. (long) (DUK_HEAPHDR_HAS_REACHABLE(h) ? 1 : 0),
  30576. (long) (DUK_HEAPHDR_HAS_TEMPROOT(h) ? 1 : 0),
  30577. (long) (DUK_HEAPHDR_HAS_FINALIZABLE(h) ? 1 : 0),
  30578. (long) (DUK_HEAPHDR_HAS_FINALIZED(h) ? 1 : 0));
  30579. }
  30580. #endif
  30581. }
  30582. DUK_LOCAL void duk__print_shared_heaphdr_string(duk__dprint_state *st, duk_heaphdr_string *h) {
  30583. duk_fixedbuffer *fb = st->fb;
  30584. if (st->heavy) {
  30585. duk_fb_sprintf(fb, "(%p)", (void *) h);
  30586. }
  30587. if (!h) {
  30588. return;
  30589. }
  30590. if (st->binary) {
  30591. duk_size_t i;
  30592. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_LBRACKET);
  30593. for (i = 0; i < (duk_size_t) sizeof(*h); i++) {
  30594. duk_fb_sprintf(fb, "%02lx", (unsigned long) ((duk_uint8_t *)h)[i]);
  30595. }
  30596. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_RBRACKET);
  30597. }
  30598. #ifdef DUK_USE_REFERENCE_COUNTING
  30599. if (st->heavy) {
  30600. duk_fb_sprintf(fb, "[h_refcount=%lu,h_flags=%08lx,type=%ld,reachable=%ld,temproot=%ld,finalizable=%ld,finalized=%ld]",
  30601. (unsigned long) DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h),
  30602. (unsigned long) DUK_HEAPHDR_GET_FLAGS((duk_heaphdr *) h),
  30603. (long) DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) h),
  30604. (long) (DUK_HEAPHDR_HAS_REACHABLE((duk_heaphdr *) h) ? 1 : 0),
  30605. (long) (DUK_HEAPHDR_HAS_TEMPROOT((duk_heaphdr *) h) ? 1 : 0),
  30606. (long) (DUK_HEAPHDR_HAS_FINALIZABLE((duk_heaphdr *) h) ? 1 : 0),
  30607. (long) (DUK_HEAPHDR_HAS_FINALIZED((duk_heaphdr *) h) ? 1 : 0));
  30608. }
  30609. #else
  30610. if (st->heavy) {
  30611. duk_fb_sprintf(fb, "[h_flags=%08lx,type=%ld,reachable=%ld,temproot=%ld,finalizable=%ld,finalized=%ld]",
  30612. (unsigned long) DUK_HEAPHDR_GET_FLAGS((duk_heaphdr *) h),
  30613. (long) DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) h),
  30614. (long) (DUK_HEAPHDR_HAS_REACHABLE((duk_heaphdr *) h) ? 1 : 0),
  30615. (long) (DUK_HEAPHDR_HAS_TEMPROOT((duk_heaphdr *) h) ? 1 : 0),
  30616. (long) (DUK_HEAPHDR_HAS_FINALIZABLE((duk_heaphdr *) h) ? 1 : 0),
  30617. (long) (DUK_HEAPHDR_HAS_FINALIZED((duk_heaphdr *) h) ? 1 : 0));
  30618. }
  30619. #endif
  30620. }
  30621. DUK_LOCAL void duk__print_hstring(duk__dprint_state *st, duk_hstring *h, duk_bool_t quotes) {
  30622. duk_fixedbuffer *fb = st->fb;
  30623. const duk_uint8_t *p;
  30624. const duk_uint8_t *p_end;
  30625. /* terminal type: no depth check */
  30626. if (duk_fb_is_full(fb)) {
  30627. return;
  30628. }
  30629. duk__print_shared_heaphdr_string(st, &h->hdr);
  30630. if (!h) {
  30631. duk_fb_put_cstring(fb, "NULL");
  30632. return;
  30633. }
  30634. p = DUK_HSTRING_GET_DATA(h);
  30635. p_end = p + DUK_HSTRING_GET_BYTELEN(h);
  30636. if (p_end > p && p[0] == DUK_ASC_UNDERSCORE) {
  30637. /* if property key begins with underscore, encode it with
  30638. * forced quotes (e.g. "_Foo") to distinguish it from encoded
  30639. * internal properties (e.g. \xffBar -> _Bar).
  30640. */
  30641. quotes = 1;
  30642. }
  30643. if (quotes) {
  30644. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_DOUBLEQUOTE);
  30645. }
  30646. while (p < p_end) {
  30647. duk_uint8_t ch = *p++;
  30648. /* two special escapes: '\' and '"', other printables as is */
  30649. if (ch == '\\') {
  30650. duk_fb_sprintf(fb, "\\\\");
  30651. } else if (ch == '"') {
  30652. duk_fb_sprintf(fb, "\\\"");
  30653. } else if (ch >= 0x20 && ch <= 0x7e) {
  30654. duk_fb_put_byte(fb, ch);
  30655. } else if (ch == 0xff && !quotes) {
  30656. /* encode \xffBar as _Bar if no quotes are applied, this is for
  30657. * readable internal keys.
  30658. */
  30659. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_UNDERSCORE);
  30660. } else {
  30661. duk_fb_sprintf(fb, "\\x%02lx", (unsigned long) ch);
  30662. }
  30663. }
  30664. if (quotes) {
  30665. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_DOUBLEQUOTE);
  30666. }
  30667. #ifdef DUK_USE_REFERENCE_COUNTING
  30668. /* XXX: limit to quoted strings only, to save keys from being cluttered? */
  30669. duk_fb_sprintf(fb, "/%lu", (unsigned long) DUK_HEAPHDR_GET_REFCOUNT(&h->hdr));
  30670. #endif
  30671. }
  30672. #ifdef DUK__COMMA
  30673. #undef DUK__COMMA
  30674. #endif
  30675. #define DUK__COMMA() do { \
  30676. if (first) { \
  30677. first = 0; \
  30678. } else { \
  30679. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_COMMA); \
  30680. } \
  30681. } while (0)
  30682. DUK_LOCAL void duk__print_hobject(duk__dprint_state *st, duk_hobject *h) {
  30683. duk_fixedbuffer *fb = st->fb;
  30684. duk_uint_fast32_t i;
  30685. duk_tval *tv;
  30686. duk_hstring *key;
  30687. duk_bool_t first = 1;
  30688. const char *brace1 = "{";
  30689. const char *brace2 = "}";
  30690. duk_bool_t pushed_loopstack = 0;
  30691. if (duk_fb_is_full(fb)) {
  30692. return;
  30693. }
  30694. duk__print_shared_heaphdr(st, &h->hdr);
  30695. if (h && DUK_HOBJECT_HAS_ARRAY_PART(h)) {
  30696. brace1 = "[";
  30697. brace2 = "]";
  30698. }
  30699. if (!h) {
  30700. duk_fb_put_cstring(fb, "NULL");
  30701. goto finished;
  30702. }
  30703. if (st->depth >= st->depth_limit) {
  30704. if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h)) {
  30705. duk_fb_sprintf(fb, "%sobject/compiledfunction %p%s", (const char *) brace1, (void *) h, (const char *) brace2);
  30706. } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
  30707. duk_fb_sprintf(fb, "%sobject/nativefunction %p%s", (const char *) brace1, (void *) h, (const char *) brace2);
  30708. } else if (DUK_HOBJECT_IS_THREAD(h)) {
  30709. duk_fb_sprintf(fb, "%sobject/thread %p%s", (const char *) brace1, (void *) h, (const char *) brace2);
  30710. } else {
  30711. duk_fb_sprintf(fb, "%sobject %p%s", (const char *) brace1, (void *) h, (const char *) brace2); /* may be NULL */
  30712. }
  30713. return;
  30714. }
  30715. for (i = 0; i < (duk_uint_fast32_t) st->loop_stack_index; i++) {
  30716. if (st->loop_stack[i] == h) {
  30717. duk_fb_sprintf(fb, "%sLOOP:%p%s", (const char *) brace1, (void *) h, (const char *) brace2);
  30718. return;
  30719. }
  30720. }
  30721. /* after this, return paths should 'goto finished' for decrement */
  30722. st->depth++;
  30723. if (st->loop_stack_index >= st->loop_stack_limit) {
  30724. duk_fb_sprintf(fb, "%sOUT-OF-LOOP-STACK%s", (const char *) brace1, (const char *) brace2);
  30725. goto finished;
  30726. }
  30727. st->loop_stack[st->loop_stack_index++] = h;
  30728. pushed_loopstack = 1;
  30729. /*
  30730. * Notation: double underscore used for internal properties which are not
  30731. * stored in the property allocation (e.g. '__valstack').
  30732. */
  30733. duk_fb_put_cstring(fb, brace1);
  30734. if (DUK_HOBJECT_GET_PROPS(NULL, h)) {
  30735. duk_uint32_t a_limit;
  30736. a_limit = DUK_HOBJECT_GET_ASIZE(h);
  30737. if (st->internal) {
  30738. /* dump all allocated entries, unused entries print as 'unused',
  30739. * note that these may extend beyond current 'length' and look
  30740. * a bit funny.
  30741. */
  30742. } else {
  30743. /* leave out trailing 'unused' elements */
  30744. while (a_limit > 0) {
  30745. tv = DUK_HOBJECT_A_GET_VALUE_PTR(NULL, h, a_limit - 1);
  30746. if (!DUK_TVAL_IS_UNDEFINED_UNUSED(tv)) {
  30747. break;
  30748. }
  30749. a_limit--;
  30750. }
  30751. }
  30752. for (i = 0; i < a_limit; i++) {
  30753. tv = DUK_HOBJECT_A_GET_VALUE_PTR(NULL, h, i);
  30754. DUK__COMMA();
  30755. duk__print_tval(st, tv);
  30756. }
  30757. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(h); i++) {
  30758. key = DUK_HOBJECT_E_GET_KEY(NULL, h, i);
  30759. if (!key) {
  30760. continue;
  30761. }
  30762. if (!st->internal &&
  30763. DUK_HSTRING_GET_BYTELEN(key) > 0 &&
  30764. DUK_HSTRING_GET_DATA(key)[0] == 0xff) {
  30765. /* XXX: use DUK_HSTRING_FLAG_INTERNAL? */
  30766. continue;
  30767. }
  30768. DUK__COMMA();
  30769. duk__print_hstring(st, key, 0);
  30770. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_COLON);
  30771. if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(NULL, h, i)) {
  30772. duk_fb_sprintf(fb, "[get:%p,set:%p]",
  30773. (void *) DUK_HOBJECT_E_GET_VALUE(NULL, h, i).a.get,
  30774. (void *) DUK_HOBJECT_E_GET_VALUE(NULL, h, i).a.set);
  30775. } else {
  30776. tv = &DUK_HOBJECT_E_GET_VALUE(NULL, h, i).v;
  30777. duk__print_tval(st, tv);
  30778. }
  30779. if (st->heavy) {
  30780. duk_fb_sprintf(fb, "<%02lx>", (unsigned long) DUK_HOBJECT_E_GET_FLAGS(NULL, h, i));
  30781. }
  30782. }
  30783. }
  30784. if (st->internal) {
  30785. if (DUK_HOBJECT_HAS_EXTENSIBLE(h)) {
  30786. DUK__COMMA(); duk_fb_sprintf(fb, "__extensible:true");
  30787. } else {
  30788. ;
  30789. }
  30790. if (DUK_HOBJECT_HAS_CONSTRUCTABLE(h)) {
  30791. DUK__COMMA(); duk_fb_sprintf(fb, "__constructable:true");
  30792. } else {
  30793. ;
  30794. }
  30795. if (DUK_HOBJECT_HAS_BOUND(h)) {
  30796. DUK__COMMA(); duk_fb_sprintf(fb, "__bound:true");
  30797. } else {
  30798. ;
  30799. }
  30800. if (DUK_HOBJECT_HAS_COMPILEDFUNCTION(h)) {
  30801. DUK__COMMA(); duk_fb_sprintf(fb, "__compiledfunction:true");
  30802. } else {
  30803. ;
  30804. }
  30805. if (DUK_HOBJECT_HAS_NATIVEFUNCTION(h)) {
  30806. DUK__COMMA(); duk_fb_sprintf(fb, "__nativefunction:true");
  30807. } else {
  30808. ;
  30809. }
  30810. if (DUK_HOBJECT_HAS_THREAD(h)) {
  30811. DUK__COMMA(); duk_fb_sprintf(fb, "__thread:true");
  30812. } else {
  30813. ;
  30814. }
  30815. if (DUK_HOBJECT_HAS_ARRAY_PART(h)) {
  30816. DUK__COMMA(); duk_fb_sprintf(fb, "__array_part:true");
  30817. } else {
  30818. ;
  30819. }
  30820. if (DUK_HOBJECT_HAS_STRICT(h)) {
  30821. DUK__COMMA(); duk_fb_sprintf(fb, "__strict:true");
  30822. } else {
  30823. ;
  30824. }
  30825. if (DUK_HOBJECT_HAS_NEWENV(h)) {
  30826. DUK__COMMA(); duk_fb_sprintf(fb, "__newenv:true");
  30827. } else {
  30828. ;
  30829. }
  30830. if (DUK_HOBJECT_HAS_NAMEBINDING(h)) {
  30831. DUK__COMMA(); duk_fb_sprintf(fb, "__namebinding:true");
  30832. } else {
  30833. ;
  30834. }
  30835. if (DUK_HOBJECT_HAS_CREATEARGS(h)) {
  30836. DUK__COMMA(); duk_fb_sprintf(fb, "__createargs:true");
  30837. } else {
  30838. ;
  30839. }
  30840. if (DUK_HOBJECT_HAS_ENVRECCLOSED(h)) {
  30841. DUK__COMMA(); duk_fb_sprintf(fb, "__envrecclosed:true");
  30842. } else {
  30843. ;
  30844. }
  30845. if (DUK_HOBJECT_HAS_EXOTIC_ARRAY(h)) {
  30846. DUK__COMMA(); duk_fb_sprintf(fb, "__special_array:true");
  30847. } else {
  30848. ;
  30849. }
  30850. if (DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(h)) {
  30851. DUK__COMMA(); duk_fb_sprintf(fb, "__special_stringobj:true");
  30852. } else {
  30853. ;
  30854. }
  30855. if (DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(h)) {
  30856. DUK__COMMA(); duk_fb_sprintf(fb, "__special_arguments:true");
  30857. } else {
  30858. ;
  30859. }
  30860. if (DUK_HOBJECT_HAS_EXOTIC_DUKFUNC(h)) {
  30861. DUK__COMMA(); duk_fb_sprintf(fb, "__special_dukfunc:true");
  30862. } else {
  30863. ;
  30864. }
  30865. if (DUK_HOBJECT_IS_BUFFEROBJECT(h)) {
  30866. DUK__COMMA(); duk_fb_sprintf(fb, "__special_bufferobj:true");
  30867. } else {
  30868. ;
  30869. }
  30870. if (DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(h)) {
  30871. DUK__COMMA(); duk_fb_sprintf(fb, "__special_proxyobj:true");
  30872. } else {
  30873. ;
  30874. }
  30875. }
  30876. if (st->internal && DUK_HOBJECT_IS_COMPILEDFUNCTION(h)) {
  30877. duk_hcompiledfunction *f = (duk_hcompiledfunction *) h;
  30878. DUK__COMMA(); duk_fb_put_cstring(fb, "__data:");
  30879. duk__print_hbuffer(st, (duk_hbuffer *) DUK_HCOMPILEDFUNCTION_GET_DATA(NULL, f));
  30880. DUK__COMMA(); duk_fb_sprintf(fb, "__nregs:%ld", (long) f->nregs);
  30881. DUK__COMMA(); duk_fb_sprintf(fb, "__nargs:%ld", (long) f->nargs);
  30882. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  30883. DUK__COMMA(); duk_fb_sprintf(fb, "__start_line:%ld", (long) f->start_line);
  30884. DUK__COMMA(); duk_fb_sprintf(fb, "__end_line:%ld", (long) f->end_line);
  30885. #endif
  30886. DUK__COMMA(); duk_fb_put_cstring(fb, "__data:");
  30887. duk__print_hbuffer(st, (duk_hbuffer *) DUK_HCOMPILEDFUNCTION_GET_DATA(NULL, f));
  30888. } else if (st->internal && DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
  30889. duk_hnativefunction *f = (duk_hnativefunction *) h;
  30890. DUK__COMMA(); duk_fb_sprintf(fb, "__func:");
  30891. duk_fb_put_funcptr(fb, (duk_uint8_t *) &f->func, sizeof(f->func));
  30892. DUK__COMMA(); duk_fb_sprintf(fb, "__nargs:%ld", (long) f->nargs);
  30893. } else if (st->internal && DUK_HOBJECT_IS_BUFFEROBJECT(h)) {
  30894. duk_hbufferobject *b = (duk_hbufferobject *) h;
  30895. DUK__COMMA(); duk_fb_sprintf(fb, "__buf:");
  30896. duk__print_hbuffer(st, (duk_hbuffer *) b->buf);
  30897. DUK__COMMA(); duk_fb_sprintf(fb, "__offset:%ld", (long) b->offset);
  30898. DUK__COMMA(); duk_fb_sprintf(fb, "__length:%ld", (long) b->length);
  30899. DUK__COMMA(); duk_fb_sprintf(fb, "__shift:%ld", (long) b->shift);
  30900. DUK__COMMA(); duk_fb_sprintf(fb, "__elemtype:%ld", (long) b->elem_type);
  30901. } else if (st->internal && DUK_HOBJECT_IS_THREAD(h)) {
  30902. duk_hthread *t = (duk_hthread *) h;
  30903. DUK__COMMA(); duk_fb_sprintf(fb, "__strict:%ld", (long) t->strict);
  30904. DUK__COMMA(); duk_fb_sprintf(fb, "__state:%ld", (long) t->state);
  30905. DUK__COMMA(); duk_fb_sprintf(fb, "__unused1:%ld", (long) t->unused1);
  30906. DUK__COMMA(); duk_fb_sprintf(fb, "__unused2:%ld", (long) t->unused2);
  30907. DUK__COMMA(); duk_fb_sprintf(fb, "__valstack_max:%ld", (long) t->valstack_max);
  30908. DUK__COMMA(); duk_fb_sprintf(fb, "__callstack_max:%ld", (long) t->callstack_max);
  30909. DUK__COMMA(); duk_fb_sprintf(fb, "__catchstack_max:%ld", (long) t->catchstack_max);
  30910. DUK__COMMA(); duk_fb_sprintf(fb, "__valstack:%p", (void *) t->valstack);
  30911. DUK__COMMA(); duk_fb_sprintf(fb, "__valstack_end:%p/%ld", (void *) t->valstack_end, (long) (t->valstack_end - t->valstack));
  30912. DUK__COMMA(); duk_fb_sprintf(fb, "__valstack_bottom:%p/%ld", (void *) t->valstack_bottom, (long) (t->valstack_bottom - t->valstack));
  30913. DUK__COMMA(); duk_fb_sprintf(fb, "__valstack_top:%p/%ld", (void *) t->valstack_top, (long) (t->valstack_top - t->valstack));
  30914. DUK__COMMA(); duk_fb_sprintf(fb, "__catchstack:%p", (void *) t->catchstack);
  30915. DUK__COMMA(); duk_fb_sprintf(fb, "__catchstack_size:%ld", (long) t->catchstack_size);
  30916. DUK__COMMA(); duk_fb_sprintf(fb, "__catchstack_top:%ld", (long) t->catchstack_top);
  30917. DUK__COMMA(); duk_fb_sprintf(fb, "__resumer:"); duk__print_hobject(st, (duk_hobject *) t->resumer);
  30918. /* XXX: print built-ins array? */
  30919. }
  30920. #ifdef DUK_USE_REFERENCE_COUNTING
  30921. if (st->internal) {
  30922. DUK__COMMA(); duk_fb_sprintf(fb, "__refcount:%lu", (unsigned long) DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h));
  30923. }
  30924. #endif
  30925. if (st->internal) {
  30926. DUK__COMMA(); duk_fb_sprintf(fb, "__class:%ld", (long) DUK_HOBJECT_GET_CLASS_NUMBER(h));
  30927. }
  30928. /* prototype should be last, for readability */
  30929. if (st->follow_proto && DUK_HOBJECT_GET_PROTOTYPE(NULL, h)) {
  30930. DUK__COMMA(); duk_fb_put_cstring(fb, "__prototype:"); duk__print_hobject(st, DUK_HOBJECT_GET_PROTOTYPE(NULL, h));
  30931. }
  30932. duk_fb_put_cstring(fb, brace2);
  30933. #if defined(DUK_USE_HOBJECT_HASH_PART)
  30934. if (st->heavy && DUK_HOBJECT_GET_HSIZE(h) > 0) {
  30935. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_LANGLE);
  30936. for (i = 0; i < DUK_HOBJECT_GET_HSIZE(h); i++) {
  30937. duk_uint_t h_idx = DUK_HOBJECT_H_GET_INDEX(NULL, h, i);
  30938. if (i > 0) {
  30939. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_COMMA);
  30940. }
  30941. if (h_idx == DUK_HOBJECT_HASHIDX_UNUSED) {
  30942. duk_fb_sprintf(fb, "u");
  30943. } else if (h_idx == DUK_HOBJECT_HASHIDX_DELETED) {
  30944. duk_fb_sprintf(fb, "d");
  30945. } else {
  30946. duk_fb_sprintf(fb, "%ld", (long) h_idx);
  30947. }
  30948. }
  30949. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_RANGLE);
  30950. }
  30951. #endif
  30952. finished:
  30953. st->depth--;
  30954. if (pushed_loopstack) {
  30955. st->loop_stack_index--;
  30956. st->loop_stack[st->loop_stack_index] = NULL;
  30957. }
  30958. }
  30959. #undef DUK__COMMA
  30960. DUK_LOCAL void duk__print_hbuffer(duk__dprint_state *st, duk_hbuffer *h) {
  30961. duk_fixedbuffer *fb = st->fb;
  30962. duk_size_t i, n;
  30963. duk_uint8_t *p;
  30964. if (duk_fb_is_full(fb)) {
  30965. return;
  30966. }
  30967. /* terminal type: no depth check */
  30968. if (!h) {
  30969. duk_fb_put_cstring(fb, "NULL");
  30970. return;
  30971. }
  30972. if (DUK_HBUFFER_HAS_DYNAMIC(h)) {
  30973. duk_hbuffer_dynamic *g = (duk_hbuffer_dynamic *) h;
  30974. duk_fb_sprintf(fb, "buffer:dynamic:%p:%ld:%ld",
  30975. (void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(NULL, g),
  30976. (long) DUK_HBUFFER_DYNAMIC_GET_SIZE(g),
  30977. (long) DUK_HBUFFER_DYNAMIC_GET_ALLOC_SIZE(g));
  30978. } else {
  30979. duk_fb_sprintf(fb, "buffer:fixed:%ld", (long) DUK_HBUFFER_GET_SIZE(h));
  30980. }
  30981. #ifdef DUK_USE_REFERENCE_COUNTING
  30982. duk_fb_sprintf(fb, "/%lu", (unsigned long) DUK_HEAPHDR_GET_REFCOUNT(&h->hdr));
  30983. #endif
  30984. if (st->hexdump) {
  30985. duk_fb_sprintf(fb, "=[");
  30986. n = DUK_HBUFFER_GET_SIZE(h);
  30987. p = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(NULL, h);
  30988. for (i = 0; i < n; i++) {
  30989. duk_fb_sprintf(fb, "%02lx", (unsigned long) p[i]);
  30990. }
  30991. duk_fb_sprintf(fb, "]");
  30992. }
  30993. }
  30994. DUK_LOCAL void duk__print_heaphdr(duk__dprint_state *st, duk_heaphdr *h) {
  30995. duk_fixedbuffer *fb = st->fb;
  30996. if (duk_fb_is_full(fb)) {
  30997. return;
  30998. }
  30999. if (!h) {
  31000. duk_fb_put_cstring(fb, "NULL");
  31001. return;
  31002. }
  31003. switch (DUK_HEAPHDR_GET_TYPE(h)) {
  31004. case DUK_HTYPE_STRING:
  31005. duk__print_hstring(st, (duk_hstring *) h, 1);
  31006. break;
  31007. case DUK_HTYPE_OBJECT:
  31008. duk__print_hobject(st, (duk_hobject *) h);
  31009. break;
  31010. case DUK_HTYPE_BUFFER:
  31011. duk__print_hbuffer(st, (duk_hbuffer *) h);
  31012. break;
  31013. default:
  31014. duk_fb_sprintf(fb, "[unknown htype %ld]", (long) DUK_HEAPHDR_GET_TYPE(h));
  31015. break;
  31016. }
  31017. }
  31018. DUK_LOCAL void duk__print_tval(duk__dprint_state *st, duk_tval *tv) {
  31019. duk_fixedbuffer *fb = st->fb;
  31020. if (duk_fb_is_full(fb)) {
  31021. return;
  31022. }
  31023. /* depth check is done when printing an actual type */
  31024. if (st->heavy) {
  31025. duk_fb_sprintf(fb, "(%p)", (void *) tv);
  31026. }
  31027. if (!tv) {
  31028. duk_fb_put_cstring(fb, "NULL");
  31029. return;
  31030. }
  31031. if (st->binary) {
  31032. duk_size_t i;
  31033. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_LBRACKET);
  31034. for (i = 0; i < (duk_size_t) sizeof(*tv); i++) {
  31035. duk_fb_sprintf(fb, "%02lx", (unsigned long) ((duk_uint8_t *)tv)[i]);
  31036. }
  31037. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_RBRACKET);
  31038. }
  31039. if (st->heavy) {
  31040. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_LANGLE);
  31041. }
  31042. switch (DUK_TVAL_GET_TAG(tv)) {
  31043. case DUK_TAG_UNDEFINED: {
  31044. if (DUK_TVAL_IS_UNDEFINED_UNUSED(tv)) {
  31045. duk_fb_put_cstring(fb, "unused");
  31046. } else {
  31047. duk_fb_put_cstring(fb, "undefined");
  31048. }
  31049. break;
  31050. }
  31051. case DUK_TAG_NULL: {
  31052. duk_fb_put_cstring(fb, "null");
  31053. break;
  31054. }
  31055. case DUK_TAG_BOOLEAN: {
  31056. duk_fb_put_cstring(fb, DUK_TVAL_GET_BOOLEAN(tv) ? "true" : "false");
  31057. break;
  31058. }
  31059. case DUK_TAG_STRING: {
  31060. /* Note: string is a terminal heap object, so no depth check here */
  31061. duk__print_hstring(st, DUK_TVAL_GET_STRING(tv), 1);
  31062. break;
  31063. }
  31064. case DUK_TAG_OBJECT: {
  31065. duk__print_hobject(st, DUK_TVAL_GET_OBJECT(tv));
  31066. break;
  31067. }
  31068. case DUK_TAG_BUFFER: {
  31069. duk__print_hbuffer(st, DUK_TVAL_GET_BUFFER(tv));
  31070. break;
  31071. }
  31072. case DUK_TAG_POINTER: {
  31073. duk_fb_sprintf(fb, "pointer:%p", (void *) DUK_TVAL_GET_POINTER(tv));
  31074. break;
  31075. }
  31076. case DUK_TAG_LIGHTFUNC: {
  31077. duk_c_function func;
  31078. duk_small_uint_t lf_flags;
  31079. DUK_TVAL_GET_LIGHTFUNC(tv, func, lf_flags);
  31080. duk_fb_sprintf(fb, "lightfunc:");
  31081. duk_fb_put_funcptr(fb, (duk_uint8_t *) &func, sizeof(func));
  31082. duk_fb_sprintf(fb, ":%04lx", (long) lf_flags);
  31083. break;
  31084. }
  31085. #if defined(DUK_USE_FASTINT)
  31086. case DUK_TAG_FASTINT:
  31087. #endif
  31088. default: {
  31089. /* IEEE double is approximately 16 decimal digits; print a couple extra */
  31090. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  31091. duk_fb_sprintf(fb, "%.18g", (double) DUK_TVAL_GET_NUMBER(tv));
  31092. break;
  31093. }
  31094. }
  31095. if (st->heavy) {
  31096. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_RANGLE);
  31097. }
  31098. }
  31099. DUK_LOCAL void duk__print_instr(duk__dprint_state *st, duk_instr_t ins) {
  31100. duk_fixedbuffer *fb = st->fb;
  31101. duk_small_int_t op;
  31102. const char *op_name;
  31103. const char *extraop_name;
  31104. op = (duk_small_int_t) DUK_DEC_OP(ins);
  31105. op_name = duk__bc_optab[op];
  31106. /* XXX: option to fix opcode length so it lines up nicely */
  31107. if (op == DUK_OP_EXTRA) {
  31108. extraop_name = duk__bc_extraoptab[DUK_DEC_A(ins)];
  31109. duk_fb_sprintf(fb, "%s %ld, %ld",
  31110. (const char *) extraop_name, (long) DUK_DEC_B(ins), (long) DUK_DEC_C(ins));
  31111. } else if (op == DUK_OP_JUMP) {
  31112. duk_int_t diff1 = DUK_DEC_ABC(ins) - DUK_BC_JUMP_BIAS; /* from next pc */
  31113. duk_int_t diff2 = diff1 + 1; /* from curr pc */
  31114. duk_fb_sprintf(fb, "%s %ld (to pc%c%ld)",
  31115. (const char *) op_name, (long) diff1,
  31116. (int) (diff2 >= 0 ? '+' : '-'), /* char format: use int */
  31117. (long) (diff2 >= 0 ? diff2 : -diff2));
  31118. } else {
  31119. duk_fb_sprintf(fb, "%s %ld, %ld, %ld",
  31120. (const char *) op_name, (long) DUK_DEC_A(ins),
  31121. (long) DUK_DEC_B(ins), (long) DUK_DEC_C(ins));
  31122. }
  31123. }
  31124. DUK_LOCAL void duk__print_opcode(duk__dprint_state *st, duk_small_int_t opcode) {
  31125. duk_fixedbuffer *fb = st->fb;
  31126. if (opcode < DUK_BC_OP_MIN || opcode > DUK_BC_OP_MAX) {
  31127. duk_fb_sprintf(fb, "?(%ld)", (long) opcode);
  31128. } else {
  31129. duk_fb_sprintf(fb, "%s", (const char *) duk__bc_optab[opcode]);
  31130. }
  31131. }
  31132. DUK_INTERNAL duk_int_t duk_debug_vsnprintf(char *str, duk_size_t size, const char *format, va_list ap) {
  31133. duk_fixedbuffer fb;
  31134. const char *p = format;
  31135. const char *p_end = p + DUK_STRLEN(format);
  31136. duk_int_t retval;
  31137. DUK_MEMZERO(&fb, sizeof(fb));
  31138. fb.buffer = (duk_uint8_t *) str;
  31139. fb.length = size;
  31140. fb.offset = 0;
  31141. fb.truncated = 0;
  31142. while (p < p_end) {
  31143. char ch = *p++;
  31144. const char *p_begfmt = NULL;
  31145. duk_bool_t got_exclamation = 0;
  31146. duk_bool_t got_long = 0; /* %lf, %ld etc */
  31147. duk__dprint_state st;
  31148. if (ch != DUK_ASC_PERCENT) {
  31149. duk_fb_put_byte(&fb, (duk_uint8_t) ch);
  31150. continue;
  31151. }
  31152. /*
  31153. * Format tag parsing. Since we don't understand all the
  31154. * possible format tags allowed, we just scan for a terminating
  31155. * specifier and keep track of relevant modifiers that we do
  31156. * understand. See man 3 printf.
  31157. */
  31158. DUK_MEMZERO(&st, sizeof(st));
  31159. st.fb = &fb;
  31160. st.depth = 0;
  31161. st.depth_limit = 1;
  31162. st.loop_stack_index = 0;
  31163. st.loop_stack_limit = DUK__LOOP_STACK_DEPTH;
  31164. p_begfmt = p - 1;
  31165. while (p < p_end) {
  31166. ch = *p++;
  31167. if (ch == DUK_ASC_STAR) {
  31168. /* unsupported: would consume multiple args */
  31169. goto error;
  31170. } else if (ch == DUK_ASC_PERCENT) {
  31171. duk_fb_put_byte(&fb, (duk_uint8_t) DUK_ASC_PERCENT);
  31172. break;
  31173. } else if (ch == DUK_ASC_EXCLAMATION) {
  31174. got_exclamation = 1;
  31175. } else if (!got_exclamation && ch == DUK_ASC_LC_L) {
  31176. got_long = 1;
  31177. } else if (got_exclamation && ch == DUK_ASC_LC_D) {
  31178. st.depth_limit = DUK__DEEP_DEPTH_LIMIT;
  31179. } else if (got_exclamation && ch == DUK_ASC_LC_P) {
  31180. st.follow_proto = 1;
  31181. } else if (got_exclamation && ch == DUK_ASC_LC_I) {
  31182. st.internal = 1;
  31183. } else if (got_exclamation && ch == DUK_ASC_LC_X) {
  31184. st.hexdump = 1;
  31185. } else if (got_exclamation && ch == DUK_ASC_LC_H) {
  31186. st.heavy = 1;
  31187. } else if (got_exclamation && ch == DUK_ASC_ATSIGN) {
  31188. st.pointer = 1;
  31189. } else if (got_exclamation && ch == DUK_ASC_HASH) {
  31190. st.binary = 1;
  31191. } else if (got_exclamation && ch == DUK_ASC_UC_T) {
  31192. duk_tval *t = va_arg(ap, duk_tval *);
  31193. if (st.pointer && !st.heavy) {
  31194. duk_fb_sprintf(&fb, "(%p)", (void *) t);
  31195. }
  31196. duk__print_tval(&st, t);
  31197. break;
  31198. } else if (got_exclamation && ch == DUK_ASC_UC_O) {
  31199. duk_heaphdr *t = va_arg(ap, duk_heaphdr *);
  31200. if (st.pointer && !st.heavy) {
  31201. duk_fb_sprintf(&fb, "(%p)", (void *) t);
  31202. }
  31203. duk__print_heaphdr(&st, t);
  31204. break;
  31205. } else if (got_exclamation && ch == DUK_ASC_UC_I) {
  31206. duk_instr_t t = va_arg(ap, duk_instr_t);
  31207. duk__print_instr(&st, t);
  31208. break;
  31209. } else if (got_exclamation && ch == DUK_ASC_UC_C) {
  31210. long t = va_arg(ap, long);
  31211. duk__print_opcode(&st, (duk_small_int_t) t);
  31212. break;
  31213. } else if (!got_exclamation && strchr(DUK__ALLOWED_STANDARD_SPECIFIERS, (int) ch)) {
  31214. char fmtbuf[DUK__MAX_FORMAT_TAG_LENGTH];
  31215. duk_size_t fmtlen;
  31216. DUK_ASSERT(p >= p_begfmt);
  31217. fmtlen = (duk_size_t) (p - p_begfmt);
  31218. if (fmtlen >= sizeof(fmtbuf)) {
  31219. /* format is too large, abort */
  31220. goto error;
  31221. }
  31222. DUK_MEMZERO(fmtbuf, sizeof(fmtbuf));
  31223. DUK_MEMCPY(fmtbuf, p_begfmt, fmtlen);
  31224. /* assume exactly 1 arg, which is why '*' is forbidden; arg size still
  31225. * depends on type though.
  31226. */
  31227. if (ch == DUK_ASC_LC_F || ch == DUK_ASC_LC_G || ch == DUK_ASC_LC_E) {
  31228. /* %f and %lf both consume a 'long' */
  31229. double arg = va_arg(ap, double);
  31230. duk_fb_sprintf(&fb, fmtbuf, arg);
  31231. } else if (ch == DUK_ASC_LC_D && got_long) {
  31232. /* %ld */
  31233. long arg = va_arg(ap, long);
  31234. duk_fb_sprintf(&fb, fmtbuf, arg);
  31235. } else if (ch == DUK_ASC_LC_D) {
  31236. /* %d; only 16 bits are guaranteed */
  31237. int arg = va_arg(ap, int);
  31238. duk_fb_sprintf(&fb, fmtbuf, arg);
  31239. } else if (ch == DUK_ASC_LC_U && got_long) {
  31240. /* %lu */
  31241. unsigned long arg = va_arg(ap, unsigned long);
  31242. duk_fb_sprintf(&fb, fmtbuf, arg);
  31243. } else if (ch == DUK_ASC_LC_U) {
  31244. /* %u; only 16 bits are guaranteed */
  31245. unsigned int arg = va_arg(ap, unsigned int);
  31246. duk_fb_sprintf(&fb, fmtbuf, arg);
  31247. } else if (ch == DUK_ASC_LC_X && got_long) {
  31248. /* %lx */
  31249. unsigned long arg = va_arg(ap, unsigned long);
  31250. duk_fb_sprintf(&fb, fmtbuf, arg);
  31251. } else if (ch == DUK_ASC_LC_X) {
  31252. /* %x; only 16 bits are guaranteed */
  31253. unsigned int arg = va_arg(ap, unsigned int);
  31254. duk_fb_sprintf(&fb, fmtbuf, arg);
  31255. } else if (ch == DUK_ASC_LC_S) {
  31256. /* %s */
  31257. const char *arg = va_arg(ap, const char *);
  31258. if (arg == NULL) {
  31259. /* '%s' and NULL is not portable, so special case
  31260. * it for debug printing.
  31261. */
  31262. duk_fb_sprintf(&fb, "NULL");
  31263. } else {
  31264. duk_fb_sprintf(&fb, fmtbuf, arg);
  31265. }
  31266. } else if (ch == DUK_ASC_LC_P) {
  31267. /* %p */
  31268. void *arg = va_arg(ap, void *);
  31269. if (arg == NULL) {
  31270. /* '%p' and NULL is portable, but special case it
  31271. * anyway to get a standard NULL marker in logs.
  31272. */
  31273. duk_fb_sprintf(&fb, "NULL");
  31274. } else {
  31275. duk_fb_sprintf(&fb, fmtbuf, arg);
  31276. }
  31277. } else if (ch == DUK_ASC_LC_C) {
  31278. /* '%c', passed concretely as int */
  31279. int arg = va_arg(ap, int);
  31280. duk_fb_sprintf(&fb, fmtbuf, arg);
  31281. } else {
  31282. /* Should not happen. */
  31283. duk_fb_sprintf(&fb, "INVALID-FORMAT(%s)", (const char *) fmtbuf);
  31284. }
  31285. break;
  31286. } else {
  31287. /* ignore */
  31288. }
  31289. }
  31290. }
  31291. goto done;
  31292. error:
  31293. duk_fb_put_cstring(&fb, "FMTERR");
  31294. /* fall through */
  31295. done:
  31296. retval = (duk_int_t) fb.offset;
  31297. duk_fb_put_byte(&fb, (duk_uint8_t) 0);
  31298. /* return total chars written excluding terminator */
  31299. return retval;
  31300. }
  31301. #if 0 /*unused*/
  31302. DUK_INTERNAL duk_int_t duk_debug_snprintf(char *str, duk_size_t size, const char *format, ...) {
  31303. duk_int_t retval;
  31304. va_list ap;
  31305. va_start(ap, format);
  31306. retval = duk_debug_vsnprintf(str, size, format, ap);
  31307. va_end(ap);
  31308. return retval;
  31309. }
  31310. #endif
  31311. /* Formatting function pointers is tricky: there is no standard pointer for
  31312. * function pointers and the size of a function pointer may depend on the
  31313. * specific pointer type. This helper formats a function pointer based on
  31314. * its memory layout to get something useful on most platforms.
  31315. */
  31316. DUK_INTERNAL void duk_debug_format_funcptr(char *buf, duk_size_t buf_size, duk_uint8_t *fptr, duk_size_t fptr_size) {
  31317. duk_size_t i;
  31318. duk_uint8_t *p = (duk_uint8_t *) buf;
  31319. duk_uint8_t *p_end = (duk_uint8_t *) (buf + buf_size - 1);
  31320. DUK_MEMZERO(buf, buf_size);
  31321. for (i = 0; i < fptr_size; i++) {
  31322. duk_int_t left = (duk_int_t) (p_end - p);
  31323. duk_uint8_t ch;
  31324. if (left <= 0) {
  31325. break;
  31326. }
  31327. /* Quite approximate but should be useful for little and big endian. */
  31328. #ifdef DUK_USE_INTEGER_BE
  31329. ch = fptr[i];
  31330. #else
  31331. ch = fptr[fptr_size - 1 - i];
  31332. #endif
  31333. p += DUK_SNPRINTF((char *) p, left, "%02lx", (unsigned long) ch);
  31334. }
  31335. }
  31336. #endif /* DUK_USE_DEBUG */
  31337. #line 1 "duk_debugger.c"
  31338. /*
  31339. * Duktape debugger
  31340. */
  31341. /* include removed: duk_internal.h */
  31342. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  31343. /*
  31344. * Helper structs
  31345. */
  31346. typedef union {
  31347. void *p;
  31348. duk_uint_t b[1];
  31349. /* Use b[] to access the size of the union, which is strictly not
  31350. * correct. Can't use fixed size unless there's feature detection
  31351. * for pointer byte size.
  31352. */
  31353. } duk__ptr_union;
  31354. /*
  31355. * Detach handling
  31356. */
  31357. #define DUK__SET_CONN_BROKEN(thr) do { \
  31358. /* For now shared handler is fine. */ \
  31359. duk_debug_do_detach((thr)->heap); \
  31360. } while (0)
  31361. DUK_INTERNAL void duk_debug_do_detach(duk_heap *heap) {
  31362. /* Can be called muliple times with no harm. */
  31363. heap->dbg_read_cb = NULL;
  31364. heap->dbg_write_cb = NULL;
  31365. heap->dbg_peek_cb = NULL;
  31366. heap->dbg_read_flush_cb = NULL;
  31367. heap->dbg_write_flush_cb = NULL;
  31368. if (heap->dbg_detached_cb) {
  31369. heap->dbg_detached_cb(heap->dbg_udata);
  31370. }
  31371. heap->dbg_detached_cb = NULL;
  31372. heap->dbg_udata = NULL;
  31373. heap->dbg_processing = 0;
  31374. heap->dbg_paused = 0;
  31375. heap->dbg_state_dirty = 0;
  31376. heap->dbg_step_type = 0;
  31377. heap->dbg_step_thread = NULL;
  31378. heap->dbg_step_csindex = 0;
  31379. heap->dbg_step_startline = 0;
  31380. /* Ensure there are no stale active breakpoint pointers.
  31381. * Breakpoint list is currently kept - we could empty it
  31382. * here but we'd need to handle refcounts correctly, and
  31383. * we'd need a 'thr' reference for that.
  31384. *
  31385. * XXX: clear breakpoint on either attach or detach?
  31386. */
  31387. heap->dbg_breakpoints_active[0] = (duk_breakpoint *) NULL;
  31388. }
  31389. /*
  31390. * Debug connection peek and flush primitives
  31391. */
  31392. DUK_INTERNAL duk_bool_t duk_debug_read_peek(duk_hthread *thr) {
  31393. duk_heap *heap;
  31394. DUK_ASSERT(thr != NULL);
  31395. heap = thr->heap;
  31396. if (heap->dbg_read_cb == NULL) {
  31397. DUK_D(DUK_DPRINT("attempt to peek in detached state, return zero (= no data)"));
  31398. return 0;
  31399. }
  31400. if (heap->dbg_peek_cb == NULL) {
  31401. DUK_DD(DUK_DDPRINT("no peek callback, return zero (= no data)"));
  31402. return 0;
  31403. }
  31404. return (duk_bool_t) (heap->dbg_peek_cb(heap->dbg_udata) > 0);
  31405. }
  31406. DUK_INTERNAL void duk_debug_read_flush(duk_hthread *thr) {
  31407. duk_heap *heap;
  31408. DUK_ASSERT(thr != NULL);
  31409. heap = thr->heap;
  31410. if (heap->dbg_read_cb == NULL) {
  31411. DUK_D(DUK_DPRINT("attempt to read flush in detached state, ignore"));
  31412. return;
  31413. }
  31414. if (heap->dbg_read_flush_cb == NULL) {
  31415. DUK_DD(DUK_DDPRINT("no read flush callback, ignore"));
  31416. return;
  31417. }
  31418. heap->dbg_read_flush_cb(heap->dbg_udata);
  31419. }
  31420. DUK_INTERNAL void duk_debug_write_flush(duk_hthread *thr) {
  31421. duk_heap *heap;
  31422. DUK_ASSERT(thr != NULL);
  31423. heap = thr->heap;
  31424. if (heap->dbg_read_cb == NULL) {
  31425. DUK_D(DUK_DPRINT("attempt to write flush in detached state, ignore"));
  31426. return;
  31427. }
  31428. if (heap->dbg_write_flush_cb == NULL) {
  31429. DUK_DD(DUK_DDPRINT("no write flush callback, ignore"));
  31430. return;
  31431. }
  31432. heap->dbg_write_flush_cb(heap->dbg_udata);
  31433. }
  31434. /*
  31435. * Debug connection skip primitives
  31436. */
  31437. /* Skip fully. */
  31438. DUK_INTERNAL void duk_debug_skip_bytes(duk_hthread *thr, duk_size_t length) {
  31439. duk_uint8_t dummy[64];
  31440. duk_size_t now;
  31441. DUK_ASSERT(thr != NULL);
  31442. while (length > 0) {
  31443. now = (length > sizeof(dummy) ? sizeof(dummy) : length);
  31444. duk_debug_read_bytes(thr, dummy, now);
  31445. length -= now;
  31446. }
  31447. }
  31448. DUK_INTERNAL void duk_debug_skip_byte(duk_hthread *thr) {
  31449. DUK_ASSERT(thr != NULL);
  31450. (void) duk_debug_read_byte(thr);
  31451. }
  31452. /*
  31453. * Debug connection read primitives
  31454. */
  31455. /* Read fully. */
  31456. DUK_INTERNAL void duk_debug_read_bytes(duk_hthread *thr, duk_uint8_t *data, duk_size_t length) {
  31457. duk_heap *heap;
  31458. duk_uint8_t *p;
  31459. duk_size_t left;
  31460. duk_size_t got;
  31461. DUK_ASSERT(thr != NULL);
  31462. heap = thr->heap;
  31463. if (heap->dbg_read_cb == NULL) {
  31464. DUK_D(DUK_DPRINT("attempt to read %ld bytes in detached state, return zero data", (long) length));
  31465. goto fail;
  31466. }
  31467. p = data;
  31468. for (;;) {
  31469. left = (duk_size_t) ((data + length) - p);
  31470. if (left == 0) {
  31471. break;
  31472. }
  31473. DUK_ASSERT(heap->dbg_read_cb != NULL);
  31474. DUK_ASSERT(left >= 1);
  31475. #if defined(DUK_USE_DEBUGGER_TRANSPORT_TORTURE)
  31476. left = 1;
  31477. #endif
  31478. got = heap->dbg_read_cb(heap->dbg_udata, (char *) p, left);
  31479. if (got == 0 || got > left) {
  31480. DUK_D(DUK_DPRINT("connection error during read, return zero data"));
  31481. DUK__SET_CONN_BROKEN(thr);
  31482. goto fail;
  31483. }
  31484. p += got;
  31485. }
  31486. return;
  31487. fail:
  31488. DUK_MEMZERO((void *) data, (size_t) length);
  31489. }
  31490. DUK_INTERNAL duk_uint8_t duk_debug_read_byte(duk_hthread *thr) {
  31491. duk_heap *heap;
  31492. duk_size_t got;
  31493. duk_uint8_t x;
  31494. DUK_ASSERT(thr != NULL);
  31495. heap = thr->heap;
  31496. if (heap->dbg_read_cb == NULL) {
  31497. DUK_D(DUK_DPRINT("attempt to read 1 bytes in detached state, return zero data"));
  31498. return 0;
  31499. }
  31500. x = 0; /* just in case callback is broken and won't write 'x' */
  31501. DUK_ASSERT(heap->dbg_read_cb != NULL);
  31502. got = heap->dbg_read_cb(heap->dbg_udata, (char *) (&x), 1);
  31503. if (got != 1) {
  31504. DUK_D(DUK_DPRINT("connection error during read, return zero data"));
  31505. DUK__SET_CONN_BROKEN(thr);
  31506. return 0;
  31507. }
  31508. return x;
  31509. }
  31510. DUK_LOCAL duk_uint32_t duk__debug_read_uint32_raw(duk_hthread *thr) {
  31511. duk_uint8_t buf[4];
  31512. DUK_ASSERT(thr != NULL);
  31513. duk_debug_read_bytes(thr, buf, 4);
  31514. return ((duk_uint32_t) buf[0] << 24) |
  31515. ((duk_uint32_t) buf[1] << 16) |
  31516. ((duk_uint32_t) buf[2] << 8) |
  31517. (duk_uint32_t) buf[3];
  31518. }
  31519. DUK_LOCAL duk_uint32_t duk__debug_read_int32_raw(duk_hthread *thr) {
  31520. return (duk_int32_t) duk__debug_read_uint32_raw(thr);
  31521. }
  31522. DUK_LOCAL duk_uint16_t duk__debug_read_uint16_raw(duk_hthread *thr) {
  31523. duk_uint8_t buf[2];
  31524. DUK_ASSERT(thr != NULL);
  31525. duk_debug_read_bytes(thr, buf, 2);
  31526. return ((duk_uint16_t) buf[0] << 8) |
  31527. (duk_uint16_t) buf[1];
  31528. }
  31529. DUK_INTERNAL duk_int32_t duk_debug_read_int(duk_hthread *thr) {
  31530. duk_small_uint_t x;
  31531. duk_small_uint_t t;
  31532. DUK_ASSERT(thr != NULL);
  31533. x = duk_debug_read_byte(thr);
  31534. if (x >= 0xc0) {
  31535. t = duk_debug_read_byte(thr);
  31536. return (duk_int32_t) (((x - 0xc0) << 8) + t);
  31537. } else if (x >= 0x80) {
  31538. return (duk_int32_t) (x - 0x80);
  31539. } else if (x == 0x10) {
  31540. return (duk_int32_t) duk__debug_read_uint32_raw(thr);
  31541. }
  31542. DUK_D(DUK_DPRINT("debug connection error: failed to decode int"));
  31543. DUK__SET_CONN_BROKEN(thr);
  31544. return 0;
  31545. }
  31546. DUK_LOCAL duk_hstring *duk__debug_read_hstring_raw(duk_hthread *thr, duk_uint32_t len) {
  31547. duk_context *ctx = (duk_context *) thr;
  31548. duk_uint8_t buf[31];
  31549. duk_uint8_t *p;
  31550. if (len <= sizeof(buf)) {
  31551. duk_debug_read_bytes(thr, buf, (duk_size_t) len);
  31552. duk_push_lstring(ctx, (const char *) buf, (duk_size_t) len);
  31553. } else {
  31554. p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) len);
  31555. DUK_ASSERT(p != NULL);
  31556. duk_debug_read_bytes(thr, p, (duk_size_t) len);
  31557. duk_to_string(ctx, -1);
  31558. }
  31559. return duk_require_hstring(ctx, -1);
  31560. }
  31561. DUK_INTERNAL duk_hstring *duk_debug_read_hstring(duk_hthread *thr) {
  31562. duk_context *ctx = (duk_context *) thr;
  31563. duk_small_uint_t x;
  31564. duk_uint32_t len;
  31565. DUK_ASSERT(thr != NULL);
  31566. x = duk_debug_read_byte(thr);
  31567. if (x >= 0x60 && x <= 0x7f) {
  31568. /* For short strings, use a fixed temp buffer. */
  31569. len = (duk_uint32_t) (x - 0x60);
  31570. } else if (x == 0x12) {
  31571. len = (duk_uint32_t) duk__debug_read_uint16_raw(thr);
  31572. } else if (x == 0x11) {
  31573. len = (duk_uint32_t) duk__debug_read_uint32_raw(thr);
  31574. } else {
  31575. goto fail;
  31576. }
  31577. return duk__debug_read_hstring_raw(thr, len);
  31578. fail:
  31579. DUK_D(DUK_DPRINT("debug connection error: failed to decode int"));
  31580. DUK__SET_CONN_BROKEN(thr);
  31581. duk_push_hstring_stridx(thr, DUK_STRIDX_EMPTY_STRING); /* always push some string */
  31582. return duk_require_hstring(ctx, -1);
  31583. }
  31584. DUK_LOCAL duk_hbuffer *duk__debug_read_hbuffer_raw(duk_hthread *thr, duk_uint32_t len) {
  31585. duk_context *ctx = (duk_context *) thr;
  31586. duk_uint8_t *p;
  31587. p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) len);
  31588. DUK_ASSERT(p != NULL);
  31589. duk_debug_read_bytes(thr, p, (duk_size_t) len);
  31590. return duk_require_hbuffer(ctx, -1);
  31591. }
  31592. DUK_LOCAL const void *duk__debug_read_pointer_raw(duk_hthread *thr) {
  31593. duk_small_uint_t x;
  31594. volatile duk__ptr_union pu;
  31595. DUK_ASSERT(thr != NULL);
  31596. x = duk_debug_read_byte(thr);
  31597. if (x != sizeof(pu)) {
  31598. goto fail;
  31599. }
  31600. duk_debug_read_bytes(thr, (duk_uint8_t *) &pu.p, sizeof(pu));
  31601. #if defined(DUK_USE_INTEGER_LE)
  31602. duk_byteswap_bytes((duk_uint8_t *) pu.b, sizeof(pu));
  31603. #endif
  31604. return (const void *) pu.p;
  31605. fail:
  31606. DUK_D(DUK_DPRINT("debug connection error: failed to decode pointer"));
  31607. DUK__SET_CONN_BROKEN(thr);
  31608. return (const void *) NULL;
  31609. }
  31610. DUK_LOCAL duk_double_t duk__debug_read_double_raw(duk_hthread *thr) {
  31611. duk_double_union du;
  31612. DUK_ASSERT(sizeof(du.uc) == 8);
  31613. duk_debug_read_bytes(thr, (duk_uint8_t *) du.uc, sizeof(du.uc));
  31614. DUK_DBLUNION_DOUBLE_NTOH(&du);
  31615. return du.d;
  31616. }
  31617. DUK_INTERNAL void duk_debug_read_tval(duk_hthread *thr) {
  31618. duk_context *ctx = (duk_context *) thr;
  31619. duk_uint8_t x;
  31620. duk_uint_t t;
  31621. duk_uint32_t len;
  31622. DUK_ASSERT(thr != NULL);
  31623. x = duk_debug_read_byte(thr);
  31624. if (x >= 0xc0) {
  31625. t = (duk_uint_t) (x - 0xc0);
  31626. t = (t << 8) + duk_debug_read_byte(thr);
  31627. duk_push_uint(ctx, (duk_uint_t) t);
  31628. return;
  31629. }
  31630. if (x >= 0x80) {
  31631. duk_push_uint(ctx, (duk_uint_t) (x - 0x80));
  31632. return;
  31633. }
  31634. if (x >= 0x60) {
  31635. len = (duk_uint32_t) (x - 0x60);
  31636. duk__debug_read_hstring_raw(thr, len);
  31637. return;
  31638. }
  31639. switch (x) {
  31640. case 0x10: {
  31641. duk_int32_t i = duk__debug_read_int32_raw(thr);
  31642. duk_push_i32(ctx, i);
  31643. break;
  31644. }
  31645. case 0x11:
  31646. len = duk__debug_read_uint32_raw(thr);
  31647. duk__debug_read_hstring_raw(thr, len);
  31648. break;
  31649. case 0x12:
  31650. len = duk__debug_read_uint16_raw(thr);
  31651. duk__debug_read_hstring_raw(thr, len);
  31652. break;
  31653. case 0x13:
  31654. len = duk__debug_read_uint32_raw(thr);
  31655. duk__debug_read_hbuffer_raw(thr, len);
  31656. break;
  31657. case 0x14:
  31658. len = duk__debug_read_uint16_raw(thr);
  31659. duk__debug_read_hbuffer_raw(thr, len);
  31660. break;
  31661. case 0x15:
  31662. duk_push_unused(ctx);
  31663. break;
  31664. case 0x16:
  31665. duk_push_undefined(ctx);
  31666. break;
  31667. case 0x17:
  31668. duk_push_null(ctx);
  31669. break;
  31670. case 0x18:
  31671. duk_push_true(ctx);
  31672. break;
  31673. case 0x19:
  31674. duk_push_false(ctx);
  31675. break;
  31676. case 0x1a: {
  31677. duk_double_t d;
  31678. d = duk__debug_read_double_raw(thr);
  31679. duk_push_number(ctx, d);
  31680. break;
  31681. }
  31682. case 0x1b:
  31683. /* XXX: not needed for now, so not implemented */
  31684. DUK_D(DUK_DPRINT("reading object values unimplemented"));
  31685. goto fail;
  31686. case 0x1c: {
  31687. const void *ptr;
  31688. ptr = duk__debug_read_pointer_raw(thr);
  31689. duk_push_pointer(thr, (void *) ptr);
  31690. break;
  31691. }
  31692. case 0x1d:
  31693. /* XXX: not needed for now, so not implemented */
  31694. DUK_D(DUK_DPRINT("reading lightfunc values unimplemented"));
  31695. goto fail;
  31696. case 0x1e: {
  31697. duk_heaphdr *h;
  31698. h = (duk_heaphdr *) duk__debug_read_pointer_raw(thr);
  31699. duk_push_heapptr(thr, (void *) h);
  31700. break;
  31701. }
  31702. default:
  31703. goto fail;
  31704. }
  31705. return;
  31706. fail:
  31707. DUK_D(DUK_DPRINT("debug connection error: failed to decode tval"));
  31708. DUK__SET_CONN_BROKEN(thr);
  31709. }
  31710. /*
  31711. * Debug connection write primitives
  31712. */
  31713. /* Write fully. */
  31714. DUK_INTERNAL void duk_debug_write_bytes(duk_hthread *thr, const duk_uint8_t *data, duk_size_t length) {
  31715. duk_heap *heap;
  31716. const duk_uint8_t *p;
  31717. duk_size_t left;
  31718. duk_size_t got;
  31719. DUK_ASSERT(thr != NULL);
  31720. DUK_ASSERT(length == 0 || data != NULL);
  31721. heap = thr->heap;
  31722. if (heap->dbg_write_cb == NULL) {
  31723. DUK_D(DUK_DPRINT("attempt to write %ld bytes in detached state, ignore", (long) length));
  31724. return;
  31725. }
  31726. if (length == 0) {
  31727. /* Avoid doing an actual write callback with length == 0,
  31728. * because that's reserved for a write flush.
  31729. */
  31730. return;
  31731. }
  31732. DUK_ASSERT(data != NULL);
  31733. p = data;
  31734. for (;;) {
  31735. left = (duk_size_t) ((data + length) - p);
  31736. if (left == 0) {
  31737. break;
  31738. }
  31739. DUK_ASSERT(heap->dbg_write_cb != NULL);
  31740. DUK_ASSERT(left >= 1);
  31741. #if defined(DUK_USE_DEBUGGER_TRANSPORT_TORTURE)
  31742. left = 1;
  31743. #endif
  31744. got = heap->dbg_write_cb(heap->dbg_udata, (const char *) p, left);
  31745. if (got == 0 || got > left) {
  31746. DUK_D(DUK_DPRINT("connection error during write"));
  31747. DUK__SET_CONN_BROKEN(thr);
  31748. return;
  31749. }
  31750. p += got;
  31751. }
  31752. }
  31753. DUK_INTERNAL void duk_debug_write_byte(duk_hthread *thr, duk_uint8_t x) {
  31754. duk_heap *heap;
  31755. duk_size_t got;
  31756. DUK_ASSERT(thr != NULL);
  31757. heap = thr->heap;
  31758. if (heap->dbg_write_cb == NULL) {
  31759. DUK_D(DUK_DPRINT("attempt to write 1 bytes in detached state, ignore"));
  31760. return;
  31761. }
  31762. DUK_ASSERT(heap->dbg_write_cb != NULL);
  31763. got = heap->dbg_write_cb(heap->dbg_udata, (const char *) (&x), 1);
  31764. if (got != 1) {
  31765. DUK_D(DUK_DPRINT("connection error during write"));
  31766. DUK__SET_CONN_BROKEN(thr);
  31767. }
  31768. }
  31769. DUK_INTERNAL void duk_debug_write_unused(duk_hthread *thr) {
  31770. duk_debug_write_byte(thr, 0x15);
  31771. }
  31772. DUK_INTERNAL void duk_debug_write_undefined(duk_hthread *thr) {
  31773. duk_debug_write_byte(thr, 0x16);
  31774. }
  31775. /* Write signed 32-bit integer. */
  31776. DUK_INTERNAL void duk_debug_write_int(duk_hthread *thr, duk_int32_t x) {
  31777. duk_uint8_t buf[5];
  31778. duk_size_t len;
  31779. DUK_ASSERT(thr != NULL);
  31780. if (x >= 0 && x <= 0x3fL) {
  31781. buf[0] = (duk_uint8_t) (0x80 + x);
  31782. len = 1;
  31783. } else if (x >= 0 && x <= 0x3fffL) {
  31784. buf[0] = (duk_uint8_t) (0xc0 + (x >> 8));
  31785. buf[1] = (duk_uint8_t) (x & 0xff);
  31786. len = 2;
  31787. } else {
  31788. /* Signed integers always map to 4 bytes now. */
  31789. buf[0] = (duk_uint8_t) 0x10;
  31790. buf[1] = (duk_uint8_t) ((x >> 24) & 0xff);
  31791. buf[2] = (duk_uint8_t) ((x >> 16) & 0xff);
  31792. buf[3] = (duk_uint8_t) ((x >> 8) & 0xff);
  31793. buf[4] = (duk_uint8_t) (x & 0xff);
  31794. len = 5;
  31795. }
  31796. duk_debug_write_bytes(thr, buf, len);
  31797. }
  31798. /* Write unsigned 32-bit integer. */
  31799. DUK_INTERNAL void duk_debug_write_uint(duk_hthread *thr, duk_uint32_t x) {
  31800. /* XXX: there's currently no need to support full 32-bit unsigned
  31801. * integer range in practice. If that becomes necessary, add a new
  31802. * dvalue type or encode as an IEEE double.
  31803. */
  31804. duk_debug_write_int(thr, (duk_int32_t) x);
  31805. }
  31806. DUK_INTERNAL void duk_debug_write_strbuf(duk_hthread *thr, const char *data, duk_size_t length, duk_uint8_t marker_base) {
  31807. duk_uint8_t buf[5];
  31808. duk_size_t buflen;
  31809. DUK_ASSERT(thr != NULL);
  31810. DUK_ASSERT(length == 0 || data != NULL);
  31811. if (length <= 0x1fUL && marker_base == 0x11) {
  31812. /* For strings, special form for short lengths. */
  31813. buf[0] = (duk_uint8_t) (0x60 + length);
  31814. buflen = 1;
  31815. } else if (length <= 0xffffUL) {
  31816. buf[0] = (duk_uint8_t) (marker_base + 1);
  31817. buf[1] = (duk_uint8_t) (length >> 8);
  31818. buf[2] = (duk_uint8_t) (length & 0xff);
  31819. buflen = 3;
  31820. } else {
  31821. buf[0] = (duk_uint8_t) marker_base;
  31822. buf[1] = (duk_uint8_t) (length >> 24);
  31823. buf[2] = (duk_uint8_t) ((length >> 16) & 0xff);
  31824. buf[3] = (duk_uint8_t) ((length >> 8) & 0xff);
  31825. buf[4] = (duk_uint8_t) (length & 0xff);
  31826. buflen = 5;
  31827. }
  31828. duk_debug_write_bytes(thr, (const duk_uint8_t *) buf, buflen);
  31829. duk_debug_write_bytes(thr, (const duk_uint8_t *) data, length);
  31830. }
  31831. DUK_INTERNAL void duk_debug_write_string(duk_hthread *thr, const char *data, duk_size_t length) {
  31832. duk_debug_write_strbuf(thr, data, length, 0x11);
  31833. }
  31834. DUK_INTERNAL void duk_debug_write_cstring(duk_hthread *thr, const char *data) {
  31835. DUK_ASSERT(thr != NULL);
  31836. duk_debug_write_string(thr,
  31837. data,
  31838. data ? DUK_STRLEN(data) : 0);
  31839. }
  31840. DUK_INTERNAL void duk_debug_write_hstring(duk_hthread *thr, duk_hstring *h) {
  31841. DUK_ASSERT(thr != NULL);
  31842. /* XXX: differentiate null pointer from empty string? */
  31843. duk_debug_write_string(thr,
  31844. (h != NULL ? (const char *) DUK_HSTRING_GET_DATA(h) : NULL),
  31845. (h != NULL ? (duk_size_t) DUK_HSTRING_GET_BYTELEN(h) : 0));
  31846. }
  31847. DUK_INTERNAL void duk_debug_write_buffer(duk_hthread *thr, const char *data, duk_size_t length) {
  31848. duk_debug_write_strbuf(thr, data, length, 0x13);
  31849. }
  31850. DUK_INTERNAL void duk_debug_write_hbuffer(duk_hthread *thr, duk_hbuffer *h) {
  31851. DUK_ASSERT(thr != NULL);
  31852. duk_debug_write_buffer(thr,
  31853. (h != NULL ? (const char *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h) : NULL),
  31854. (h != NULL ? (duk_size_t) DUK_HBUFFER_GET_SIZE(h) : 0));
  31855. }
  31856. DUK_LOCAL void duk__debug_write_pointer_raw(duk_hthread *thr, const void *ptr, duk_uint8_t ibyte) {
  31857. duk_uint8_t buf[2];
  31858. volatile duk__ptr_union pu;
  31859. DUK_ASSERT(thr != NULL);
  31860. DUK_ASSERT(sizeof(ptr) >= 1 && sizeof(ptr) <= 16);
  31861. /* ptr may be NULL */
  31862. buf[0] = ibyte;
  31863. buf[1] = sizeof(pu);
  31864. duk_debug_write_bytes(thr, buf, 2);
  31865. pu.p = (void *) ptr;
  31866. #if defined(DUK_USE_INTEGER_LE)
  31867. duk_byteswap_bytes((duk_uint8_t *) pu.b, sizeof(pu));
  31868. #endif
  31869. duk_debug_write_bytes(thr, (const duk_uint8_t *) &pu.p, (duk_size_t) sizeof(pu));
  31870. }
  31871. DUK_INTERNAL void duk_debug_write_pointer(duk_hthread *thr, const void *ptr) {
  31872. duk__debug_write_pointer_raw(thr, ptr, 0x1c);
  31873. }
  31874. #if defined(DUK_USE_DEBUGGER_DUMPHEAP)
  31875. DUK_INTERNAL void duk_debug_write_heapptr(duk_hthread *thr, duk_heaphdr *h) {
  31876. duk__debug_write_pointer_raw(thr, (const void *) h, 0x1e);
  31877. }
  31878. #endif /* DUK_USE_DEBUGGER_DUMPHEAP */
  31879. DUK_INTERNAL void duk_debug_write_hobject(duk_hthread *thr, duk_hobject *obj) {
  31880. duk_uint8_t buf[3];
  31881. volatile duk__ptr_union pu;
  31882. DUK_ASSERT(thr != NULL);
  31883. DUK_ASSERT(sizeof(obj) >= 1 && sizeof(obj) <= 16);
  31884. DUK_ASSERT(obj != NULL);
  31885. buf[0] = 0x1b;
  31886. buf[1] = (duk_uint8_t) DUK_HOBJECT_GET_CLASS_NUMBER(obj);
  31887. buf[2] = sizeof(pu);
  31888. duk_debug_write_bytes(thr, buf, 3);
  31889. pu.p = (void *) obj;
  31890. #if defined(DUK_USE_INTEGER_LE)
  31891. duk_byteswap_bytes((duk_uint8_t *) pu.b, sizeof(pu));
  31892. #endif
  31893. duk_debug_write_bytes(thr, (const duk_uint8_t *) &pu.p, (duk_size_t) sizeof(pu));
  31894. }
  31895. DUK_INTERNAL void duk_debug_write_tval(duk_hthread *thr, duk_tval *tv) {
  31896. duk_c_function lf_func;
  31897. duk_small_uint_t lf_flags;
  31898. duk_uint8_t buf[4];
  31899. duk_double_union du;
  31900. DUK_ASSERT(thr != NULL);
  31901. DUK_ASSERT(tv != NULL);
  31902. switch (DUK_TVAL_GET_TAG(tv)) {
  31903. case DUK_TAG_UNDEFINED:
  31904. duk_debug_write_byte(thr,
  31905. DUK_TVAL_IS_UNDEFINED_UNUSED(tv) ? 0x15 : 0x16);
  31906. break;
  31907. case DUK_TAG_NULL:
  31908. duk_debug_write_byte(thr, 0x17);
  31909. break;
  31910. case DUK_TAG_BOOLEAN:
  31911. DUK_ASSERT(DUK_TVAL_GET_BOOLEAN(tv) == 0 ||
  31912. DUK_TVAL_GET_BOOLEAN(tv) == 1);
  31913. duk_debug_write_byte(thr, DUK_TVAL_GET_BOOLEAN(tv) ? 0x18 : 0x19);
  31914. break;
  31915. case DUK_TAG_POINTER:
  31916. duk_debug_write_pointer(thr, (const void *) DUK_TVAL_GET_POINTER(tv));
  31917. break;
  31918. case DUK_TAG_LIGHTFUNC:
  31919. DUK_TVAL_GET_LIGHTFUNC(tv, lf_func, lf_flags);
  31920. buf[0] = 0x1d;
  31921. buf[1] = (duk_uint8_t) (lf_flags >> 8);
  31922. buf[2] = (duk_uint8_t) (lf_flags & 0xff);
  31923. buf[3] = sizeof(lf_func);
  31924. duk_debug_write_bytes(thr, buf, 4);
  31925. duk_debug_write_bytes(thr, (const duk_uint8_t *) &lf_func, sizeof(lf_func));
  31926. break;
  31927. case DUK_TAG_STRING:
  31928. duk_debug_write_hstring(thr, DUK_TVAL_GET_STRING(tv));
  31929. break;
  31930. case DUK_TAG_OBJECT:
  31931. duk_debug_write_hobject(thr, DUK_TVAL_GET_OBJECT(tv));
  31932. break;
  31933. case DUK_TAG_BUFFER:
  31934. duk_debug_write_hbuffer(thr, DUK_TVAL_GET_BUFFER(tv));
  31935. break;
  31936. #if defined(DUK_USE_FASTINT)
  31937. case DUK_TAG_FASTINT:
  31938. #endif
  31939. default:
  31940. /* Numbers are normalized to big (network) endian. */
  31941. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  31942. du.d = DUK_TVAL_GET_NUMBER(tv);
  31943. DUK_DBLUNION_DOUBLE_HTON(&du);
  31944. duk_debug_write_byte(thr, 0x1a);
  31945. duk_debug_write_bytes(thr, (const duk_uint8_t *) du.uc, sizeof(du.uc));
  31946. }
  31947. }
  31948. #if defined(DUK_USE_DEBUGGER_DUMPHEAP)
  31949. /* Variant for writing duk_tvals so that any heap allocated values are
  31950. * written out as tagged heap pointers.
  31951. */
  31952. DUK_LOCAL void duk__debug_write_tval_heapptr(duk_hthread *thr, duk_tval *tv) {
  31953. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  31954. duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
  31955. duk_debug_write_heapptr(thr, h);
  31956. } else {
  31957. duk_debug_write_tval(thr, tv);
  31958. }
  31959. }
  31960. #endif /* DUK_USE_DEBUGGER_DUMPHEAP */
  31961. /*
  31962. * Debug connection message write helpers
  31963. */
  31964. #if 0 /* unused */
  31965. DUK_INTERNAL void duk_debug_write_request(duk_hthread *thr, duk_small_uint_t command) {
  31966. duk_debug_write_byte(thr, DUK_DBG_MARKER_REQUEST);
  31967. duk_debug_write_int(thr, command);
  31968. }
  31969. #endif
  31970. DUK_INTERNAL void duk_debug_write_reply(duk_hthread *thr) {
  31971. duk_debug_write_byte(thr, DUK_DBG_MARKER_REPLY);
  31972. }
  31973. DUK_INTERNAL void duk_debug_write_error_eom(duk_hthread *thr, duk_small_uint_t err_code, const char *msg) {
  31974. /* Allow NULL 'msg' */
  31975. duk_debug_write_byte(thr, DUK_DBG_MARKER_ERROR);
  31976. duk_debug_write_int(thr, (duk_int32_t) err_code);
  31977. duk_debug_write_cstring(thr, msg);
  31978. duk_debug_write_eom(thr);
  31979. }
  31980. DUK_INTERNAL void duk_debug_write_notify(duk_hthread *thr, duk_small_uint_t command) {
  31981. duk_debug_write_byte(thr, DUK_DBG_MARKER_NOTIFY);
  31982. duk_debug_write_int(thr, command);
  31983. }
  31984. DUK_INTERNAL void duk_debug_write_eom(duk_hthread *thr) {
  31985. duk_debug_write_byte(thr, DUK_DBG_MARKER_EOM);
  31986. /* As an initial implementation, write flush after every EOM (and the
  31987. * version identifier). A better implementation would flush only when
  31988. * Duktape is finished processing messages so that a flush only happens
  31989. * after all outbound messages are finished on that occasion.
  31990. */
  31991. duk_debug_write_flush(thr);
  31992. }
  31993. /*
  31994. * Status message and helpers
  31995. */
  31996. DUK_INTERNAL duk_uint_fast32_t duk_debug_curr_line(duk_hthread *thr) {
  31997. duk_context *ctx = (duk_context *) thr;
  31998. duk_activation *act;
  31999. duk_uint_fast32_t line;
  32000. duk_uint_fast32_t pc;
  32001. if (thr->callstack_top == 0) {
  32002. return 0;
  32003. }
  32004. act = thr->callstack + thr->callstack_top - 1;
  32005. /* act->pc indicates the next instruction about to be executed. This
  32006. * is usually correct, but for the 'debugger' statement it will be the
  32007. * instruction after that.
  32008. */
  32009. pc = (duk_uint_fast32_t) act->pc;
  32010. /* XXX: this should be optimized to be a raw query and avoid valstack
  32011. * operations if possible.
  32012. */
  32013. duk_push_hobject(ctx, act->func);
  32014. line = duk_hobject_pc2line_query(ctx, -1, pc);
  32015. duk_pop(ctx);
  32016. return line;
  32017. }
  32018. DUK_INTERNAL void duk_debug_send_status(duk_hthread *thr) {
  32019. duk_context *ctx = (duk_context *) thr;
  32020. duk_activation *act;
  32021. duk_debug_write_notify(thr, DUK_DBG_CMD_STATUS);
  32022. duk_debug_write_int(thr, thr->heap->dbg_paused);
  32023. DUK_ASSERT_DISABLE(thr->callstack_top >= 0); /* unsigned */
  32024. if (thr->callstack_top == 0) {
  32025. duk_debug_write_undefined(thr);
  32026. duk_debug_write_undefined(thr);
  32027. duk_debug_write_int(thr, 0);
  32028. duk_debug_write_int(thr, 0);
  32029. } else {
  32030. act = thr->callstack + thr->callstack_top - 1;
  32031. duk_push_hobject(ctx, act->func);
  32032. duk_get_prop_string(ctx, -1, "fileName");
  32033. duk_safe_to_string(ctx, -1);
  32034. duk_debug_write_hstring(thr, duk_require_hstring(ctx, -1));
  32035. duk_get_prop_string(ctx, -2, "name");
  32036. duk_safe_to_string(ctx, -1);
  32037. duk_debug_write_hstring(thr, duk_require_hstring(ctx, -1));
  32038. duk_pop_3(ctx);
  32039. duk_debug_write_uint(thr, (duk_uint32_t) duk_debug_curr_line(thr));
  32040. duk_debug_write_uint(thr, (duk_uint32_t) act->pc);
  32041. }
  32042. duk_debug_write_eom(thr);
  32043. }
  32044. /*
  32045. * Debug message processing
  32046. */
  32047. /* Skip dvalue. */
  32048. DUK_LOCAL duk_bool_t duk__debug_skip_dvalue(duk_hthread *thr) {
  32049. duk_uint8_t x;
  32050. duk_uint32_t len;
  32051. x = duk_debug_read_byte(thr);
  32052. if (x >= 0xc0) {
  32053. duk_debug_skip_byte(thr);
  32054. return 0;
  32055. }
  32056. if (x >= 0x80) {
  32057. return 0;
  32058. }
  32059. if (x >= 0x60) {
  32060. duk_debug_skip_bytes(thr, x - 0x60);
  32061. return 0;
  32062. }
  32063. switch(x) {
  32064. case 0x00:
  32065. return 1; /* Return 1: got EOM */
  32066. case 0x01:
  32067. case 0x02:
  32068. case 0x03:
  32069. case 0x04:
  32070. break;
  32071. case 0x10:
  32072. (void) duk__debug_read_uint32_raw(thr);
  32073. break;
  32074. case 0x11:
  32075. case 0x13:
  32076. len = duk__debug_read_uint32_raw(thr);
  32077. duk_debug_skip_bytes(thr, len);
  32078. break;
  32079. case 0x12:
  32080. case 0x14:
  32081. len = duk__debug_read_uint16_raw(thr);
  32082. duk_debug_skip_bytes(thr, len);
  32083. break;
  32084. case 0x15:
  32085. case 0x16:
  32086. case 0x17:
  32087. case 0x18:
  32088. case 0x19:
  32089. break;
  32090. case 0x1a:
  32091. duk_debug_skip_bytes(thr, 8);
  32092. break;
  32093. case 0x1b:
  32094. duk_debug_skip_byte(thr);
  32095. len = duk_debug_read_byte(thr);
  32096. duk_debug_skip_bytes(thr, len);
  32097. break;
  32098. case 0x1c:
  32099. len = duk_debug_read_byte(thr);
  32100. duk_debug_skip_bytes(thr, len);
  32101. break;
  32102. case 0x1d:
  32103. duk_debug_skip_bytes(thr, 2);
  32104. len = duk_debug_read_byte(thr);
  32105. duk_debug_skip_bytes(thr, len);
  32106. break;
  32107. default:
  32108. goto fail;
  32109. }
  32110. return 0;
  32111. fail:
  32112. DUK__SET_CONN_BROKEN(thr);
  32113. return 1; /* Pretend like we got EOM */
  32114. }
  32115. /* Skip dvalues to EOM. */
  32116. DUK_LOCAL void duk__debug_skip_to_eom(duk_hthread *thr) {
  32117. for (;;) {
  32118. if (duk__debug_skip_dvalue(thr)) {
  32119. break;
  32120. }
  32121. }
  32122. }
  32123. /*
  32124. * Process incoming debug requests
  32125. */
  32126. DUK_LOCAL void duk__debug_handle_basic_info(duk_hthread *thr, duk_heap *heap) {
  32127. DUK_UNREF(heap);
  32128. DUK_D(DUK_DPRINT("debug command version"));
  32129. duk_debug_write_reply(thr);
  32130. duk_debug_write_int(thr, DUK_VERSION);
  32131. duk_debug_write_cstring(thr, DUK_GIT_DESCRIBE);
  32132. duk_debug_write_cstring(thr, DUK_USE_TARGET_INFO);
  32133. #if defined(DUK_USE_DOUBLE_LE)
  32134. duk_debug_write_int(thr, 1);
  32135. #elif defined(DUK_USE_DOUBLE_ME)
  32136. duk_debug_write_int(thr, 2);
  32137. #elif defined(DUK_USE_DOUBLE_BE)
  32138. duk_debug_write_int(thr, 3);
  32139. #else
  32140. duk_debug_write_int(thr, 0);
  32141. #endif
  32142. duk_debug_write_eom(thr);
  32143. }
  32144. DUK_LOCAL void duk__debug_handle_trigger_status(duk_hthread *thr, duk_heap *heap) {
  32145. DUK_UNREF(heap);
  32146. DUK_D(DUK_DPRINT("debug command triggerstatus"));
  32147. duk_debug_write_reply(thr);
  32148. duk_debug_write_eom(thr);
  32149. heap->dbg_state_dirty = 1;
  32150. }
  32151. DUK_LOCAL void duk__debug_handle_pause(duk_hthread *thr, duk_heap *heap) {
  32152. DUK_D(DUK_DPRINT("debug command pause"));
  32153. DUK_HEAP_SET_PAUSED(heap);
  32154. duk_debug_write_reply(thr);
  32155. duk_debug_write_eom(thr);
  32156. }
  32157. DUK_LOCAL void duk__debug_handle_resume(duk_hthread *thr, duk_heap *heap) {
  32158. DUK_D(DUK_DPRINT("debug command resume"));
  32159. DUK_HEAP_CLEAR_PAUSED(heap);
  32160. duk_debug_write_reply(thr);
  32161. duk_debug_write_eom(thr);
  32162. }
  32163. DUK_LOCAL void duk__debug_handle_step(duk_hthread *thr, duk_heap *heap, duk_int32_t cmd) {
  32164. duk_small_uint_t step_type;
  32165. duk_uint_fast32_t line;
  32166. if (cmd == DUK_DBG_CMD_STEPINTO) {
  32167. step_type = DUK_STEP_TYPE_INTO;
  32168. } else if (cmd == DUK_DBG_CMD_STEPOVER) {
  32169. step_type = DUK_STEP_TYPE_OVER;
  32170. } else {
  32171. DUK_ASSERT(cmd == DUK_DBG_CMD_STEPOUT);
  32172. step_type = DUK_STEP_TYPE_OUT;
  32173. }
  32174. DUK_D(DUK_DPRINT("debug command stepinto/stepover/stepout: %d", (int) cmd));
  32175. line = duk_debug_curr_line(thr);
  32176. if (line > 0) {
  32177. heap->dbg_paused = 0;
  32178. heap->dbg_step_type = step_type;
  32179. heap->dbg_step_thread = thr;
  32180. heap->dbg_step_csindex = thr->callstack_top - 1;
  32181. heap->dbg_step_startline = line;
  32182. heap->dbg_state_dirty = 1;
  32183. } else {
  32184. DUK_D(DUK_DPRINT("cannot determine current line, stepinto/stepover/stepout ignored"));
  32185. }
  32186. duk_debug_write_reply(thr);
  32187. duk_debug_write_eom(thr);
  32188. }
  32189. DUK_LOCAL void duk__debug_handle_list_break(duk_hthread *thr, duk_heap *heap) {
  32190. duk_small_int_t i;
  32191. DUK_D(DUK_DPRINT("debug command listbreak"));
  32192. duk_debug_write_reply(thr);
  32193. for (i = 0; i < (duk_small_int_t) heap->dbg_breakpoint_count; i++) {
  32194. duk_debug_write_hstring(thr, heap->dbg_breakpoints[i].filename);
  32195. duk_debug_write_uint(thr, (duk_uint32_t) heap->dbg_breakpoints[i].line);
  32196. }
  32197. duk_debug_write_eom(thr);
  32198. }
  32199. DUK_LOCAL void duk__debug_handle_add_break(duk_hthread *thr, duk_heap *heap) {
  32200. duk_context *ctx = (duk_context *) thr;
  32201. duk_hstring *filename;
  32202. duk_uint32_t linenumber;
  32203. duk_small_int_t idx;
  32204. DUK_UNREF(heap);
  32205. filename = duk_debug_read_hstring(thr);
  32206. linenumber = (duk_uint32_t) duk_debug_read_int(thr);
  32207. DUK_D(DUK_DPRINT("debug command addbreak: %!O:%ld", (duk_hobject *) filename, (long) linenumber));
  32208. idx = duk_debug_add_breakpoint(thr, filename, linenumber);
  32209. if (idx >= 0) {
  32210. duk_debug_write_reply(thr);
  32211. duk_debug_write_int(thr, (duk_int32_t) idx);
  32212. duk_debug_write_eom(thr);
  32213. } else {
  32214. duk_debug_write_error_eom(thr, DUK_DBG_ERR_TOOMANY, "no space for breakpoint");
  32215. }
  32216. duk_pop(ctx);
  32217. }
  32218. DUK_LOCAL void duk__debug_handle_del_break(duk_hthread *thr, duk_heap *heap) {
  32219. duk_small_uint_t idx;
  32220. DUK_UNREF(heap);
  32221. DUK_D(DUK_DPRINT("debug command delbreak"));
  32222. idx = (duk_small_uint_t) duk_debug_read_int(thr);
  32223. if (duk_debug_remove_breakpoint(thr, idx)) {
  32224. duk_debug_write_reply(thr);
  32225. duk_debug_write_eom(thr);
  32226. } else {
  32227. duk_debug_write_error_eom(thr, DUK_DBG_ERR_NOTFOUND, "invalid breakpoint index");
  32228. }
  32229. }
  32230. DUK_LOCAL void duk__debug_handle_get_var(duk_hthread *thr, duk_heap *heap) {
  32231. duk_context *ctx = (duk_context *) thr;
  32232. duk_hstring *str;
  32233. duk_bool_t rc;
  32234. DUK_UNREF(heap);
  32235. DUK_D(DUK_DPRINT("debug command getvar"));
  32236. str = duk_debug_read_hstring(thr); /* push to stack */
  32237. DUK_ASSERT(str != NULL);
  32238. if (thr->callstack_top > 0) {
  32239. rc = duk_js_getvar_activation(thr,
  32240. thr->callstack + thr->callstack_top - 1,
  32241. str,
  32242. 0);
  32243. } else {
  32244. /* No activation, no variable access. Could also pretend
  32245. * we're in the global program context and read stuff off
  32246. * the global object.
  32247. */
  32248. DUK_D(DUK_DPRINT("callstack empty, no activation -> ignore getvar"));
  32249. rc = 0;
  32250. }
  32251. duk_debug_write_reply(thr);
  32252. if (rc) {
  32253. duk_debug_write_int(thr, 1);
  32254. duk_debug_write_tval(thr, duk_require_tval(ctx, -2));
  32255. duk_pop_2(ctx);
  32256. } else {
  32257. duk_debug_write_int(thr, 0);
  32258. duk_debug_write_unused(thr);
  32259. }
  32260. duk_pop(ctx);
  32261. duk_debug_write_eom(thr);
  32262. }
  32263. DUK_LOCAL void duk__debug_handle_put_var(duk_hthread *thr, duk_heap *heap) {
  32264. duk_context *ctx = (duk_context *) thr;
  32265. duk_hstring *str;
  32266. duk_tval *tv;
  32267. DUK_UNREF(heap);
  32268. DUK_D(DUK_DPRINT("debug command putvar"));
  32269. str = duk_debug_read_hstring(thr); /* push to stack */
  32270. DUK_ASSERT(str != NULL);
  32271. duk_debug_read_tval(thr); /* push to stack */
  32272. tv = duk_require_tval(ctx, -1);
  32273. if (thr->callstack_top > 0) {
  32274. duk_js_putvar_activation(thr,
  32275. thr->callstack + thr->callstack_top - 1,
  32276. str,
  32277. tv,
  32278. 0);
  32279. } else {
  32280. DUK_D(DUK_DPRINT("callstack empty, no activation -> ignore putvar"));
  32281. }
  32282. duk_pop_2(ctx);
  32283. /* XXX: Current putvar implementation doesn't have a success flag,
  32284. * add one and send to debug client?
  32285. */
  32286. duk_debug_write_reply(thr);
  32287. duk_debug_write_eom(thr);
  32288. }
  32289. DUK_LOCAL void duk__debug_handle_get_call_stack(duk_hthread *thr, duk_heap *heap) {
  32290. duk_context *ctx = (duk_context *) thr;
  32291. duk_hthread *curr_thr = thr;
  32292. duk_activation *curr_act;
  32293. duk_uint_fast32_t line;
  32294. duk_size_t i;
  32295. DUK_UNREF(heap);
  32296. duk_debug_write_reply(thr);
  32297. while (curr_thr != NULL) {
  32298. i = curr_thr->callstack_top;
  32299. while (i > 0) {
  32300. i--;
  32301. curr_act = curr_thr->callstack + i;
  32302. /* XXX: optimize to use direct reads,
  32303. * i.e. avoid value stack operations.
  32304. */
  32305. duk_push_tval(ctx, &curr_act->tv_func);
  32306. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_FILE_NAME);
  32307. duk_safe_to_string(ctx, -1);
  32308. duk_debug_write_hstring(thr, duk_get_hstring(ctx, -1));
  32309. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_NAME);
  32310. duk_safe_to_string(ctx, -1);
  32311. duk_debug_write_hstring(thr, duk_get_hstring(ctx, -1));
  32312. line = duk_hobject_pc2line_query(ctx, -3, curr_act->pc);
  32313. duk_debug_write_uint(thr, (duk_uint32_t) line);
  32314. duk_debug_write_uint(thr, (duk_uint32_t) curr_act->pc);
  32315. duk_pop_3(ctx);
  32316. }
  32317. curr_thr = curr_thr->resumer;
  32318. }
  32319. duk_debug_write_eom(thr);
  32320. }
  32321. DUK_LOCAL void duk__debug_handle_get_locals(duk_hthread *thr, duk_heap *heap) {
  32322. duk_context *ctx = (duk_context *) thr;
  32323. duk_activation *curr_act;
  32324. duk_hstring *varname;
  32325. DUK_UNREF(heap);
  32326. duk_debug_write_reply(thr);
  32327. if (thr->callstack_top == 0) {
  32328. goto callstack_empty;
  32329. }
  32330. curr_act = thr->callstack + thr->callstack_top - 1;
  32331. /* XXX: several nice-to-have improvements here:
  32332. * - Use direct reads avoiding value stack operations
  32333. * - Avoid triggering getters, indicate getter values to debug client
  32334. * - If side effects are possible, add error catching
  32335. */
  32336. duk_push_tval(ctx, &curr_act->tv_func);
  32337. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VARMAP);
  32338. if (duk_is_object(ctx, -1)) {
  32339. duk_enum(ctx, -1, 0 /*enum_flags*/);
  32340. while (duk_next(ctx, -1 /*enum_index*/, 0 /*get_value*/)) {
  32341. varname = duk_get_hstring(ctx, -1);
  32342. DUK_ASSERT(varname != NULL);
  32343. duk_js_getvar_activation(thr, curr_act, varname, 0 /*throw_flag*/);
  32344. /* [ ... func varmap enum key value this ] */
  32345. duk_debug_write_hstring(thr, duk_get_hstring(ctx, -3));
  32346. duk_debug_write_tval(thr, duk_get_tval(ctx, -2));
  32347. duk_pop_3(ctx); /* -> [ ... func varmap enum ] */
  32348. }
  32349. duk_pop(ctx);
  32350. } else {
  32351. DUK_D(DUK_DPRINT("varmap is not an object in GetLocals, ignore"));
  32352. }
  32353. duk_pop_2(ctx);
  32354. callstack_empty:
  32355. duk_debug_write_eom(thr);
  32356. }
  32357. DUK_LOCAL void duk__debug_handle_eval(duk_hthread *thr, duk_heap *heap) {
  32358. duk_context *ctx = (duk_context *) thr;
  32359. duk_small_uint_t call_flags;
  32360. duk_int_t call_ret;
  32361. duk_small_int_t eval_err;
  32362. #if defined(DUK_USE_ASSERTIONS)
  32363. duk_idx_t entry_top;
  32364. #endif
  32365. DUK_UNREF(heap);
  32366. DUK_D(DUK_DPRINT("debug command eval"));
  32367. /* The eval code must be executed within the current (topmost)
  32368. * activation. For now, use global object eval() function, with
  32369. * the eval considered a 'direct call to eval'.
  32370. */
  32371. #if defined(DUK_USE_ASSERTIONS)
  32372. entry_top = duk_get_top(ctx);
  32373. #endif
  32374. duk_push_c_function(ctx, duk_bi_global_object_eval, 1 /*nargs*/);
  32375. duk_push_undefined(ctx); /* 'this' binding shouldn't matter here */
  32376. (void) duk_debug_read_hstring(thr);
  32377. /* [ ... eval "eval" eval_input ] */
  32378. call_flags = DUK_CALL_FLAG_PROTECTED;
  32379. if (thr->callstack_top >= 1) {
  32380. duk_activation *act;
  32381. duk_hobject *fun;
  32382. act = thr->callstack + thr->callstack_top - 1;
  32383. fun = DUK_ACT_GET_FUNC(act);
  32384. if (fun && DUK_HOBJECT_IS_COMPILEDFUNCTION(fun)) {
  32385. /* Direct eval requires that there's a current
  32386. * activation and it is an Ecmascript function.
  32387. * When Eval is executed from e.g. cooperate API
  32388. * call we'll need to an indirect eval instead.
  32389. */
  32390. call_flags |= DUK_CALL_FLAG_DIRECT_EVAL;
  32391. }
  32392. }
  32393. call_ret = duk_handle_call(thr, 1 /*num_stack_args*/, call_flags);
  32394. if (call_ret == DUK_EXEC_SUCCESS) {
  32395. eval_err = 0;
  32396. /* Use result value as is. */
  32397. } else {
  32398. /* For errors a string coerced result is most informative
  32399. * right now, as the debug client doesn't have the capability
  32400. * to traverse the error object.
  32401. */
  32402. eval_err = 1;
  32403. duk_safe_to_string(ctx, -1);
  32404. }
  32405. /* [ ... result ] */
  32406. duk_debug_write_reply(thr);
  32407. duk_debug_write_int(thr, (duk_int32_t) eval_err);
  32408. duk_debug_write_tval(thr, duk_require_tval(ctx, -1));
  32409. duk_debug_write_eom(thr);
  32410. duk_pop(ctx);
  32411. DUK_ASSERT(duk_get_top(ctx) == entry_top);
  32412. }
  32413. DUK_LOCAL void duk__debug_handle_detach(duk_hthread *thr, duk_heap *heap) {
  32414. DUK_UNREF(heap);
  32415. DUK_D(DUK_DPRINT("debug command detach"));
  32416. duk_debug_write_reply(thr);
  32417. duk_debug_write_eom(thr);
  32418. DUK_D(DUK_DPRINT("debug connection detached, mark broken"));
  32419. DUK__SET_CONN_BROKEN(thr);
  32420. }
  32421. #if defined(DUK_USE_DEBUGGER_DUMPHEAP)
  32422. DUK_LOCAL void duk__debug_dump_heaphdr(duk_hthread *thr, duk_heap *heap, duk_heaphdr *hdr) {
  32423. DUK_UNREF(heap);
  32424. duk_debug_write_heapptr(thr, hdr);
  32425. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HEAPHDR_GET_TYPE(hdr));
  32426. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HEAPHDR_GET_FLAGS_RAW(hdr));
  32427. #if defined(DUK_USE_REFERENCE_COUNTING)
  32428. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HEAPHDR_GET_REFCOUNT(hdr));
  32429. #else
  32430. duk_debug_write_int(thr, (duk_int32_t) -1);
  32431. #endif
  32432. switch (DUK_HEAPHDR_GET_TYPE(hdr)) {
  32433. case DUK_HTYPE_STRING: {
  32434. duk_hstring *h = (duk_hstring *) hdr;
  32435. duk_debug_write_uint(thr, (duk_int32_t) DUK_HSTRING_GET_BYTELEN(h));
  32436. duk_debug_write_uint(thr, (duk_int32_t) DUK_HSTRING_GET_CHARLEN(h));
  32437. duk_debug_write_uint(thr, (duk_int32_t) DUK_HSTRING_GET_HASH(h));
  32438. duk_debug_write_hstring(thr, h);
  32439. break;
  32440. }
  32441. case DUK_HTYPE_OBJECT: {
  32442. duk_hobject *h = (duk_hobject *) hdr;
  32443. duk_hstring *k;
  32444. duk_uint_fast32_t i;
  32445. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_GET_CLASS_NUMBER(h));
  32446. duk_debug_write_heapptr(thr, (duk_heaphdr *) DUK_HOBJECT_GET_PROTOTYPE(heap, h));
  32447. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_GET_ESIZE(h));
  32448. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_GET_ENEXT(h));
  32449. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_GET_ASIZE(h));
  32450. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_GET_HSIZE(h));
  32451. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(h); i++) {
  32452. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_E_GET_FLAGS(heap, h, i));
  32453. k = DUK_HOBJECT_E_GET_KEY(heap, h, i);
  32454. duk_debug_write_heapptr(thr, (duk_heaphdr *) k);
  32455. if (k == NULL) {
  32456. duk_debug_write_int(thr, 0); /* isAccessor */
  32457. duk_debug_write_unused(thr);
  32458. continue;
  32459. }
  32460. if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap, h, i)) {
  32461. duk_debug_write_int(thr, 1); /* isAccessor */
  32462. duk_debug_write_heapptr(thr, (duk_heaphdr *) DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->a.get);
  32463. duk_debug_write_heapptr(thr, (duk_heaphdr *) DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->a.set);
  32464. } else {
  32465. duk_debug_write_int(thr, 0); /* isAccessor */
  32466. duk__debug_write_tval_heapptr(thr, &DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->v);
  32467. }
  32468. }
  32469. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(h); i++) {
  32470. /* Note: array dump will include elements beyond
  32471. * 'length'.
  32472. */
  32473. duk__debug_write_tval_heapptr(thr, DUK_HOBJECT_A_GET_VALUE_PTR(heap, h, i));
  32474. }
  32475. break;
  32476. }
  32477. case DUK_HTYPE_BUFFER: {
  32478. duk_hbuffer *h = (duk_hbuffer *) hdr;
  32479. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HBUFFER_GET_SIZE(h));
  32480. if (DUK_HBUFFER_HAS_DYNAMIC(h)) {
  32481. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HBUFFER_DYNAMIC_GET_ALLOC_SIZE((duk_hbuffer_dynamic *) h));
  32482. } else {
  32483. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HBUFFER_GET_SIZE(h));
  32484. }
  32485. duk_debug_write_buffer(thr, (const char *) DUK_HBUFFER_GET_DATA_PTR(heap, h), (duk_size_t) DUK_HBUFFER_GET_SIZE(h));
  32486. break;
  32487. }
  32488. default: {
  32489. DUK_D(DUK_DPRINT("invalid htype: %d", (int) DUK_HEAPHDR_GET_TYPE(hdr)));
  32490. }
  32491. }
  32492. }
  32493. DUK_LOCAL void duk__debug_dump_heap_allocated(duk_hthread *thr, duk_heap *heap) {
  32494. duk_heaphdr *hdr;
  32495. hdr = heap->heap_allocated;
  32496. while (hdr != NULL) {
  32497. duk__debug_dump_heaphdr(thr, heap, hdr);
  32498. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  32499. }
  32500. }
  32501. #if defined(DUK_USE_STRTAB_CHAIN)
  32502. DUK_LOCAL void duk__debug_dump_strtab_chain(duk_hthread *thr, duk_heap *heap) {
  32503. duk_uint_fast32_t i, j;
  32504. duk_strtab_entry *e;
  32505. #if defined(DUK_USE_HEAPPTR16)
  32506. duk_uint16_t *lst;
  32507. #else
  32508. duk_hstring **lst;
  32509. #endif
  32510. duk_hstring *h;
  32511. for (i = 0; i < DUK_STRTAB_CHAIN_SIZE; i++) {
  32512. e = heap->strtable + i;
  32513. if (e->listlen > 0) {
  32514. #if defined(DUK_USE_HEAPPTR16)
  32515. lst = (duk_uint16_t *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.strlist16);
  32516. #else
  32517. lst = e->u.strlist;
  32518. #endif
  32519. DUK_ASSERT(lst != NULL);
  32520. for (j = 0; j < e->listlen; j++) {
  32521. #if defined(DUK_USE_HEAPPTR16)
  32522. h = DUK_USE_HEAPPTR_DEC16(heap->heap_udata, lst[j]);
  32523. #else
  32524. h = lst[j];
  32525. #endif
  32526. if (h != NULL) {
  32527. duk__debug_dump_heaphdr(thr, heap, (duk_heaphdr *) h);
  32528. }
  32529. }
  32530. } else {
  32531. #if defined(DUK_USE_HEAPPTR16)
  32532. h = DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.str16);
  32533. #else
  32534. h = e->u.str;
  32535. #endif
  32536. if (h != NULL) {
  32537. duk__debug_dump_heaphdr(thr, heap, (duk_heaphdr *) h);
  32538. }
  32539. }
  32540. }
  32541. }
  32542. #endif /* DUK_USE_STRTAB_CHAIN */
  32543. #if defined(DUK_USE_STRTAB_PROBE)
  32544. DUK_LOCAL void duk__debug_dump_strtab_probe(duk_hthread *thr, duk_heap *heap) {
  32545. duk_uint32_t i;
  32546. duk_hstring *h;
  32547. for (i = 0; i < heap->st_size; i++) {
  32548. #if defined(DUK_USE_HEAPPTR16)
  32549. h = DUK_USE_HEAPPTR_DEC16(heap->strtable16[i]);
  32550. #else
  32551. h = heap->strtable[i];
  32552. #endif
  32553. if (h == NULL || h == DUK_STRTAB_DELETED_MARKER(heap)) {
  32554. continue;
  32555. }
  32556. duk__debug_dump_heaphdr(thr, heap, (duk_heaphdr *) h);
  32557. }
  32558. }
  32559. #endif /* DUK_USE_STRTAB_PROBE */
  32560. DUK_LOCAL void duk__debug_handle_dump_heap(duk_hthread *thr, duk_heap *heap) {
  32561. DUK_D(DUK_DPRINT("debug command dumpheap"));
  32562. duk_debug_write_reply(thr);
  32563. duk__debug_dump_heap_allocated(thr, heap);
  32564. #if defined(DUK_USE_STRTAB_CHAIN)
  32565. duk__debug_dump_strtab_chain(thr, heap);
  32566. #endif
  32567. #if defined(DUK_USE_STRTAB_PROBE)
  32568. duk__debug_dump_strtab_probe(thr, heap);
  32569. #endif
  32570. duk_debug_write_eom(thr);
  32571. }
  32572. #endif /* DUK_USE_DEBUGGER_DUMPHEAP */
  32573. DUK_LOCAL void duk__debug_handle_get_bytecode(duk_hthread *thr, duk_heap *heap) {
  32574. duk_activation *act;
  32575. duk_hcompiledfunction *fun;
  32576. duk_size_t i, n;
  32577. duk_tval *tv;
  32578. duk_hobject **fn;
  32579. DUK_UNREF(heap);
  32580. DUK_D(DUK_DPRINT("debug command getbytecode"));
  32581. duk_debug_write_reply(thr);
  32582. if (thr->callstack_top == 0) {
  32583. fun = NULL;
  32584. } else {
  32585. act = thr->callstack + thr->callstack_top - 1;
  32586. fun = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act);
  32587. if (!DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) fun)) {
  32588. fun = NULL;
  32589. }
  32590. }
  32591. DUK_ASSERT(fun == NULL || DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) fun));
  32592. if (fun) {
  32593. n = DUK_HCOMPILEDFUNCTION_GET_CONSTS_COUNT(heap, fun);
  32594. duk_debug_write_int(thr, (int) n);
  32595. tv = DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(heap, fun);
  32596. for (i = 0; i < n; i++) {
  32597. duk_debug_write_tval(thr, tv);
  32598. tv++;
  32599. }
  32600. n = DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(heap, fun);
  32601. duk_debug_write_int(thr, (int) n);
  32602. fn = DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(heap, fun);
  32603. for (i = 0; i < n; i++) {
  32604. duk_debug_write_hobject(thr, *fn);
  32605. fn++;
  32606. }
  32607. duk_debug_write_string(thr,
  32608. (const char *) DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(heap, fun),
  32609. (duk_size_t) DUK_HCOMPILEDFUNCTION_GET_CODE_SIZE(heap, fun));
  32610. } else {
  32611. duk_debug_write_int(thr, 0);
  32612. duk_debug_write_int(thr, 0);
  32613. duk_debug_write_cstring(thr, "");
  32614. }
  32615. duk_debug_write_eom(thr);
  32616. }
  32617. DUK_LOCAL void duk__debug_process_message(duk_hthread *thr) {
  32618. duk_context *ctx = (duk_context *) thr;
  32619. duk_heap *heap;
  32620. duk_uint8_t x;
  32621. duk_int32_t cmd;
  32622. DUK_ASSERT(thr != NULL);
  32623. heap = thr->heap;
  32624. DUK_ASSERT(heap != NULL);
  32625. DUK_UNREF(ctx);
  32626. x = duk_debug_read_byte(thr);
  32627. switch (x) {
  32628. case DUK_DBG_MARKER_REQUEST: {
  32629. cmd = duk_debug_read_int(thr);
  32630. switch (cmd) {
  32631. case DUK_DBG_CMD_BASICINFO: {
  32632. duk__debug_handle_basic_info(thr, heap);
  32633. break;
  32634. }
  32635. case DUK_DBG_CMD_TRIGGERSTATUS: {
  32636. duk__debug_handle_trigger_status(thr, heap);
  32637. break;
  32638. }
  32639. case DUK_DBG_CMD_PAUSE: {
  32640. duk__debug_handle_pause(thr, heap);
  32641. break;
  32642. }
  32643. case DUK_DBG_CMD_RESUME: {
  32644. duk__debug_handle_resume(thr, heap);
  32645. break;
  32646. }
  32647. case DUK_DBG_CMD_STEPINTO:
  32648. case DUK_DBG_CMD_STEPOVER:
  32649. case DUK_DBG_CMD_STEPOUT: {
  32650. duk__debug_handle_step(thr, heap, cmd);
  32651. break;
  32652. }
  32653. case DUK_DBG_CMD_LISTBREAK: {
  32654. duk__debug_handle_list_break(thr, heap);
  32655. break;
  32656. }
  32657. case DUK_DBG_CMD_ADDBREAK: {
  32658. duk__debug_handle_add_break(thr, heap);
  32659. break;
  32660. }
  32661. case DUK_DBG_CMD_DELBREAK: {
  32662. duk__debug_handle_del_break(thr, heap);
  32663. break;
  32664. }
  32665. case DUK_DBG_CMD_GETVAR: {
  32666. duk__debug_handle_get_var(thr, heap);
  32667. break;
  32668. }
  32669. case DUK_DBG_CMD_PUTVAR: {
  32670. duk__debug_handle_put_var(thr, heap);
  32671. break;
  32672. }
  32673. case DUK_DBG_CMD_GETCALLSTACK: {
  32674. duk__debug_handle_get_call_stack(thr, heap);
  32675. break;
  32676. }
  32677. case DUK_DBG_CMD_GETLOCALS: {
  32678. duk__debug_handle_get_locals(thr, heap);
  32679. break;
  32680. }
  32681. case DUK_DBG_CMD_EVAL: {
  32682. duk__debug_handle_eval(thr, heap);
  32683. break;
  32684. }
  32685. case DUK_DBG_CMD_DETACH: {
  32686. duk__debug_handle_detach(thr, heap);
  32687. break;
  32688. }
  32689. #if defined(DUK_USE_DEBUGGER_DUMPHEAP)
  32690. case DUK_DBG_CMD_DUMPHEAP: {
  32691. duk__debug_handle_dump_heap(thr, heap);
  32692. break;
  32693. }
  32694. #endif /* DUK_USE_DEBUGGER_DUMPHEAP */
  32695. case DUK_DBG_CMD_GETBYTECODE: {
  32696. duk__debug_handle_get_bytecode(thr, heap);
  32697. break;
  32698. }
  32699. default: {
  32700. DUK_D(DUK_DPRINT("debug command unsupported: %d", (int) cmd));
  32701. duk_debug_write_error_eom(thr, DUK_DBG_ERR_UNSUPPORTED, "unsupported command");
  32702. }
  32703. } /* switch cmd */
  32704. break;
  32705. }
  32706. case DUK_DBG_MARKER_REPLY: {
  32707. DUK_D(DUK_DPRINT("debug reply, skipping"));
  32708. break;
  32709. }
  32710. case DUK_DBG_MARKER_ERROR: {
  32711. DUK_D(DUK_DPRINT("debug error, skipping"));
  32712. break;
  32713. }
  32714. case DUK_DBG_MARKER_NOTIFY: {
  32715. DUK_D(DUK_DPRINT("debug notify, skipping"));
  32716. break;
  32717. }
  32718. default: {
  32719. DUK_D(DUK_DPRINT("invalid initial byte, drop connection: %d", (int) x));
  32720. goto fail;
  32721. }
  32722. } /* switch initial byte */
  32723. duk__debug_skip_to_eom(thr);
  32724. return;
  32725. fail:
  32726. DUK__SET_CONN_BROKEN(thr);
  32727. return;
  32728. }
  32729. DUK_INTERNAL duk_bool_t duk_debug_process_messages(duk_hthread *thr, duk_bool_t no_block) {
  32730. duk_context *ctx = (duk_context *) thr;
  32731. #if defined(DUK_USE_ASSERTIONS)
  32732. duk_idx_t entry_top;
  32733. #endif
  32734. duk_bool_t retval = 0;
  32735. DUK_ASSERT(thr != NULL);
  32736. DUK_UNREF(ctx);
  32737. #if defined(DUK_USE_ASSERTIONS)
  32738. entry_top = duk_get_top(ctx);
  32739. #endif
  32740. DUK_DD(DUK_DDPRINT("top at entry: %ld", (long) duk_get_top(ctx)));
  32741. for (;;) {
  32742. /* Process messages until we're no longer paused or we peek
  32743. * and see there's nothing to read right now.
  32744. */
  32745. DUK_DD(DUK_DDPRINT("top at loop top: %ld", (long) duk_get_top(ctx)));
  32746. if (thr->heap->dbg_read_cb == NULL) {
  32747. DUK_D(DUK_DPRINT("debug connection broken, stop processing messages"));
  32748. break;
  32749. } else if (!thr->heap->dbg_paused || no_block) {
  32750. if (!duk_debug_read_peek(thr)) {
  32751. DUK_D(DUK_DPRINT("processing debug message, peek indicated no data, stop processing"));
  32752. break;
  32753. }
  32754. DUK_D(DUK_DPRINT("processing debug message, peek indicated there is data, handle it"));
  32755. } else {
  32756. DUK_D(DUK_DPRINT("paused, process debug message, blocking if necessary"));
  32757. }
  32758. duk__debug_process_message(thr);
  32759. if (thr->heap->dbg_state_dirty) {
  32760. /* Executed something that may have affected status,
  32761. * resend.
  32762. */
  32763. duk_debug_send_status(thr);
  32764. thr->heap->dbg_state_dirty = 0;
  32765. }
  32766. retval = 1; /* processed one or more messages */
  32767. }
  32768. /* As an initial implementation, read flush after exiting the message
  32769. * loop.
  32770. */
  32771. duk_debug_read_flush(thr);
  32772. DUK_DD(DUK_DDPRINT("top at exit: %ld", (long) duk_get_top(ctx)));
  32773. #if defined(DUK_USE_ASSERTIONS)
  32774. /* Easy to get wrong, so assert for it. */
  32775. DUK_ASSERT(entry_top == duk_get_top(ctx));
  32776. #endif
  32777. return retval;
  32778. }
  32779. /*
  32780. * Breakpoint management
  32781. */
  32782. DUK_INTERNAL duk_small_int_t duk_debug_add_breakpoint(duk_hthread *thr, duk_hstring *filename, duk_uint32_t line) {
  32783. duk_heap *heap;
  32784. duk_breakpoint *b;
  32785. /* Caller must trigger recomputation of active breakpoint list. To
  32786. * ensure stale values are not used if that doesn't happen, clear the
  32787. * active breakpoint list here.
  32788. */
  32789. DUK_ASSERT(thr != NULL);
  32790. DUK_ASSERT(filename != NULL);
  32791. heap = thr->heap;
  32792. if (heap->dbg_breakpoint_count >= DUK_HEAP_MAX_BREAKPOINTS) {
  32793. DUK_D(DUK_DPRINT("failed to add breakpoint for %O:%ld, all breakpoint slots used",
  32794. (duk_heaphdr *) filename, (long) line));
  32795. return -1;
  32796. }
  32797. heap->dbg_breakpoints_active[0] = (duk_breakpoint *) NULL;
  32798. b = heap->dbg_breakpoints + (heap->dbg_breakpoint_count++);
  32799. b->filename = filename;
  32800. b->line = line;
  32801. DUK_HSTRING_INCREF(thr, filename);
  32802. return heap->dbg_breakpoint_count - 1; /* index */
  32803. }
  32804. DUK_INTERNAL duk_bool_t duk_debug_remove_breakpoint(duk_hthread *thr, duk_small_uint_t breakpoint_index) {
  32805. duk_heap *heap = thr->heap;
  32806. duk_hstring *h;
  32807. duk_breakpoint *b;
  32808. duk_size_t move_size;
  32809. /* Caller must trigger recomputation of active breakpoint list. To
  32810. * ensure stale values are not used if that doesn't happen, clear the
  32811. * active breakpoint list here.
  32812. */
  32813. DUK_ASSERT(thr != NULL);
  32814. DUK_ASSERT_DISABLE(breakpoint_index >= 0); /* unsigned */
  32815. if (breakpoint_index >= heap->dbg_breakpoint_count) {
  32816. DUK_D(DUK_DPRINT("invalid breakpoint index: %ld", (long) breakpoint_index));
  32817. return 0;
  32818. }
  32819. b = heap->dbg_breakpoints + breakpoint_index;
  32820. h = b->filename;
  32821. DUK_ASSERT(h != NULL);
  32822. move_size = sizeof(duk_breakpoint) * (heap->dbg_breakpoint_count - breakpoint_index - 1);
  32823. if (move_size > 0) {
  32824. DUK_MEMMOVE((void *) b,
  32825. (void *) (b + 1),
  32826. move_size);
  32827. }
  32828. heap->dbg_breakpoint_count--;
  32829. heap->dbg_breakpoints_active[0] = (duk_breakpoint *) NULL;
  32830. DUK_HSTRING_DECREF(thr, h); /* side effects */
  32831. /* Breakpoint entries above the used area are left as garbage. */
  32832. return 1;
  32833. }
  32834. #undef DUK__SET_CONN_BROKEN
  32835. #else /* DUK_USE_DEBUGGER_SUPPORT */
  32836. /* No debugger support. */
  32837. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  32838. #line 1 "duk_error_augment.c"
  32839. /*
  32840. * Augmenting errors at their creation site and their throw site.
  32841. *
  32842. * When errors are created, traceback data is added by built-in code
  32843. * and a user error handler (if defined) can process or replace the
  32844. * error. Similarly, when errors are thrown, a user error handler
  32845. * (if defined) can process or replace the error.
  32846. *
  32847. * Augmentation and other processing at error creation time is nice
  32848. * because an error is only created once, but it may be thrown and
  32849. * rethrown multiple times. User error handler registered for processing
  32850. * an error at its throw site must be careful to handle rethrowing in
  32851. * a useful manner.
  32852. *
  32853. * Error augmentation may throw an internal error (e.g. alloc error).
  32854. *
  32855. * Ecmascript allows throwing any values, so all values cannot be
  32856. * augmented. Currently, the built-in augmentation at error creation
  32857. * only augments error values which are Error instances (= have the
  32858. * built-in Error.prototype in their prototype chain) and are also
  32859. * extensible. User error handlers have no limitations in this respect.
  32860. */
  32861. /* include removed: duk_internal.h */
  32862. /*
  32863. * Helper for calling a user error handler.
  32864. *
  32865. * 'thr' must be the currently active thread; the error handler is called
  32866. * in its context. The valstack of 'thr' must have the error value on
  32867. * top, and will be replaced by another error value based on the return
  32868. * value of the error handler.
  32869. *
  32870. * The helper calls duk_handle_call() recursively in protected mode.
  32871. * Before that call happens, no longjmps should happen; as a consequence,
  32872. * we must assume that the valstack contains enough temporary space for
  32873. * arguments and such.
  32874. *
  32875. * While the error handler runs, any errors thrown will not trigger a
  32876. * recursive error handler call (this is implemented using a heap level
  32877. * flag which will "follow" through any coroutines resumed inside the
  32878. * error handler). If the error handler is not callable or throws an
  32879. * error, the resulting error replaces the original error (for Duktape
  32880. * internal errors, duk_error_throw.c further substitutes this error with
  32881. * a DoubleError which is not ideal). This would be easy to change and
  32882. * even signal to the caller.
  32883. *
  32884. * The user error handler is stored in 'Duktape.errCreate' or
  32885. * 'Duktape.errThrow' depending on whether we're augmenting the error at
  32886. * creation or throw time. There are several alternatives to this approach,
  32887. * see doc/error-objects.txt for discussion.
  32888. *
  32889. * Note: since further longjmp()s may occur while calling the error handler
  32890. * (for many reasons, e.g. a labeled 'break' inside the handler), the
  32891. * caller can make no assumptions on the thr->heap->lj state after the
  32892. * call (this affects especially duk_error_throw.c). This is not an issue
  32893. * as long as the caller writes to the lj state only after the error handler
  32894. * finishes.
  32895. */
  32896. #if defined(DUK_USE_ERRTHROW) || defined(DUK_USE_ERRCREATE)
  32897. DUK_LOCAL void duk__err_augment_user(duk_hthread *thr, duk_small_uint_t stridx_cb) {
  32898. duk_context *ctx = (duk_context *) thr;
  32899. duk_tval *tv_hnd;
  32900. duk_small_uint_t call_flags;
  32901. duk_int_t rc;
  32902. DUK_ASSERT(thr != NULL);
  32903. DUK_ASSERT(thr->heap != NULL);
  32904. DUK_ASSERT_DISABLE(stridx_cb >= 0); /* unsigned */
  32905. DUK_ASSERT(stridx_cb < DUK_HEAP_NUM_STRINGS);
  32906. if (DUK_HEAP_HAS_ERRHANDLER_RUNNING(thr->heap)) {
  32907. DUK_DD(DUK_DDPRINT("recursive call to error handler, ignore"));
  32908. return;
  32909. }
  32910. /*
  32911. * Check whether or not we have an error handler.
  32912. *
  32913. * We must be careful of not triggering an error when looking up the
  32914. * property. For instance, if the property is a getter, we don't want
  32915. * to call it, only plain values are allowed. The value, if it exists,
  32916. * is not checked. If the value is not a function, a TypeError happens
  32917. * when it is called and that error replaces the original one.
  32918. */
  32919. DUK_ASSERT_VALSTACK_SPACE(thr, 4); /* 3 entries actually needed below */
  32920. /* [ ... errval ] */
  32921. if (thr->builtins[DUK_BIDX_DUKTAPE] == NULL) {
  32922. /* When creating built-ins, some of the built-ins may not be set
  32923. * and we want to tolerate that when throwing errors.
  32924. */
  32925. DUK_DD(DUK_DDPRINT("error occurred when DUK_BIDX_DUKTAPE is NULL, ignoring"));
  32926. return;
  32927. }
  32928. tv_hnd = duk_hobject_find_existing_entry_tval_ptr(thr->heap,
  32929. thr->builtins[DUK_BIDX_DUKTAPE],
  32930. DUK_HTHREAD_GET_STRING(thr, stridx_cb));
  32931. if (tv_hnd == NULL) {
  32932. DUK_DD(DUK_DDPRINT("error handler does not exist or is not a plain value: %!T",
  32933. (duk_tval *) tv_hnd));
  32934. return;
  32935. }
  32936. DUK_DDD(DUK_DDDPRINT("error handler dump (callability not checked): %!T",
  32937. (duk_tval *) tv_hnd));
  32938. duk_push_tval(ctx, tv_hnd);
  32939. /* [ ... errval errhandler ] */
  32940. duk_insert(ctx, -2); /* -> [ ... errhandler errval ] */
  32941. duk_push_undefined(ctx);
  32942. duk_insert(ctx, -2); /* -> [ ... errhandler undefined(= this) errval ] */
  32943. /* [ ... errhandler undefined errval ] */
  32944. /*
  32945. * DUK_CALL_FLAG_IGNORE_RECLIMIT causes duk_handle_call() to ignore C
  32946. * recursion depth limit (and won't increase it either). This is
  32947. * dangerous, but useful because it allows the error handler to run
  32948. * even if the original error is caused by C recursion depth limit.
  32949. *
  32950. * The heap level DUK_HEAP_FLAG_ERRHANDLER_RUNNING is set for the
  32951. * duration of the error handler and cleared afterwards. This flag
  32952. * prevents the error handler from running recursively. The flag is
  32953. * heap level so that the flag properly controls even coroutines
  32954. * launched by an error handler. Since the flag is heap level, it is
  32955. * critical to restore it correctly.
  32956. *
  32957. * We ignore errors now: a success return and an error value both
  32958. * replace the original error value. (This would be easy to change.)
  32959. */
  32960. DUK_ASSERT(!DUK_HEAP_HAS_ERRHANDLER_RUNNING(thr->heap)); /* since no recursive error handler calls */
  32961. DUK_HEAP_SET_ERRHANDLER_RUNNING(thr->heap);
  32962. call_flags = DUK_CALL_FLAG_PROTECTED |
  32963. DUK_CALL_FLAG_IGNORE_RECLIMIT; /* protected, ignore reclimit, not constructor */
  32964. rc = duk_handle_call(thr,
  32965. 1, /* num args */
  32966. call_flags); /* call_flags */
  32967. DUK_UNREF(rc); /* no need to check now: both success and error are OK */
  32968. DUK_ASSERT(DUK_HEAP_HAS_ERRHANDLER_RUNNING(thr->heap));
  32969. DUK_HEAP_CLEAR_ERRHANDLER_RUNNING(thr->heap);
  32970. /* [ ... errval ] */
  32971. }
  32972. #endif /* DUK_USE_ERRTHROW || DUK_USE_ERRHANDLE */
  32973. /*
  32974. * Add tracedata to an error on the stack top.
  32975. */
  32976. #ifdef DUK_USE_TRACEBACKS
  32977. 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) {
  32978. duk_context *ctx = (duk_context *) thr;
  32979. duk_small_uint_t depth;
  32980. duk_int_t i, i_min;
  32981. duk_uarridx_t arr_idx;
  32982. duk_double_t d;
  32983. DUK_ASSERT(thr != NULL);
  32984. DUK_ASSERT(thr_callstack != NULL);
  32985. DUK_ASSERT(ctx != NULL);
  32986. /* [ ... error ] */
  32987. /*
  32988. * The traceback format is pretty arcane in an attempt to keep it compact
  32989. * and cheap to create. It may change arbitrarily from version to version.
  32990. * It should be decoded/accessed through version specific accessors only.
  32991. *
  32992. * See doc/error-objects.txt.
  32993. */
  32994. DUK_DDD(DUK_DDDPRINT("adding traceback to object: %!T",
  32995. (duk_tval *) duk_get_tval(ctx, -1)));
  32996. duk_push_array(ctx); /* XXX: specify array size, as we know it */
  32997. arr_idx = 0;
  32998. /* compiler SyntaxErrors (and other errors) come first; blame the source
  32999. * code file/line primarily.
  33000. */
  33001. if (thr->compile_ctx != NULL && thr->compile_ctx->h_filename != NULL) {
  33002. duk_push_hstring(ctx, thr->compile_ctx->h_filename);
  33003. duk_xdef_prop_index_wec(ctx, -2, arr_idx);
  33004. arr_idx++;
  33005. duk_push_uint(ctx, (duk_uint_t) thr->compile_ctx->curr_token.start_line); /* (flags<<32) + (line), flags = 0 */
  33006. duk_xdef_prop_index_wec(ctx, -2, arr_idx);
  33007. arr_idx++;
  33008. }
  33009. /* filename/line from C macros (__FILE__, __LINE__) are added as an
  33010. * entry with a special format: (string, number). The number contains
  33011. * the line and flags.
  33012. */
  33013. /* XXX: optimize: allocate an array part to the necessary size (upwards
  33014. * estimate) and fill in the values directly into the array part; finally
  33015. * update 'length'.
  33016. */
  33017. /* XXX: using duk_put_prop_index() would cause obscure error cases when Array.prototype
  33018. * has write-protected array index named properties. This was seen as DoubleErrors
  33019. * in e.g. some test262 test cases. Using duk_xdef_prop_index() is better but heavier.
  33020. * The best fix is to fill in the tracedata directly into the array part.
  33021. */
  33022. /* [ ... error arr ] */
  33023. if (c_filename) {
  33024. duk_push_string(ctx, c_filename);
  33025. duk_xdef_prop_index_wec(ctx, -2, arr_idx);
  33026. arr_idx++;
  33027. d = (noblame_fileline ? ((duk_double_t) DUK_TB_FLAG_NOBLAME_FILELINE) * DUK_DOUBLE_2TO32 : 0.0) +
  33028. (duk_double_t) c_line;
  33029. duk_push_number(ctx, d);
  33030. duk_xdef_prop_index_wec(ctx, -2, arr_idx);
  33031. arr_idx++;
  33032. }
  33033. /* traceback depth doesn't take into account the filename/line
  33034. * special handling above (intentional)
  33035. */
  33036. depth = DUK_USE_TRACEBACK_DEPTH;
  33037. i_min = (thr_callstack->callstack_top > (duk_size_t) depth ? (duk_int_t) (thr_callstack->callstack_top - depth) : 0);
  33038. DUK_ASSERT(i_min >= 0);
  33039. /* [ ... error arr ] */
  33040. DUK_ASSERT(thr_callstack->callstack_top <= DUK_INT_MAX); /* callstack limits */
  33041. for (i = (duk_int_t) (thr_callstack->callstack_top - 1); i >= i_min; i--) {
  33042. duk_uint32_t pc;
  33043. /*
  33044. * Note: each API operation potentially resizes the callstack,
  33045. * so be careful to re-lookup after every operation. Currently
  33046. * these is no issue because we don't store a temporary 'act'
  33047. * pointer at all. (This would be a non-issue if we operated
  33048. * directly on the array part.)
  33049. */
  33050. /* [... arr] */
  33051. DUK_ASSERT_DISABLE(thr_callstack->callstack[i].pc >= 0); /* unsigned */
  33052. /* Add function object. */
  33053. duk_push_tval(ctx, &(thr_callstack->callstack + i)->tv_func);
  33054. duk_xdef_prop_index_wec(ctx, -2, arr_idx);
  33055. arr_idx++;
  33056. /* Add a number containing: pc, activation flags.
  33057. *
  33058. * PC points to next instruction, find offending PC. Note that
  33059. * PC == 0 for native code.
  33060. */
  33061. pc = thr_callstack->callstack[i].pc;
  33062. if (pc > 0) {
  33063. pc--;
  33064. }
  33065. DUK_ASSERT_DISABLE(pc >= 0); /* unsigned */
  33066. DUK_ASSERT((duk_double_t) pc < DUK_DOUBLE_2TO32); /* assume PC is at most 32 bits and non-negative */
  33067. d = ((duk_double_t) thr_callstack->callstack[i].flags) * DUK_DOUBLE_2TO32 + (duk_double_t) pc;
  33068. duk_push_number(ctx, d); /* -> [... arr num] */
  33069. duk_xdef_prop_index_wec(ctx, -2, arr_idx);
  33070. arr_idx++;
  33071. }
  33072. /* XXX: set with duk_hobject_set_length() when tracedata is filled directly */
  33073. duk_push_uint(ctx, (duk_uint_t) arr_idx);
  33074. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_WC);
  33075. /* [ ... error arr ] */
  33076. duk_xdef_prop_stridx_wec(ctx, -2, DUK_STRIDX_INT_TRACEDATA); /* -> [ ... error ] */
  33077. }
  33078. #endif /* DUK_USE_TRACEBACKS */
  33079. #if defined(DUK_USE_AUGMENT_ERROR_CREATE)
  33080. 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) {
  33081. duk_context *ctx = (duk_context *) thr;
  33082. #ifdef DUK_USE_ASSERTIONS
  33083. duk_int_t entry_top;
  33084. #endif
  33085. #ifdef DUK_USE_ASSERTIONS
  33086. entry_top = duk_get_top(ctx);
  33087. #endif
  33088. DUK_ASSERT(obj != NULL);
  33089. DUK_UNREF(obj); /* unreferenced w/o tracebacks */
  33090. DUK_UNREF(ctx); /* unreferenced w/ tracebacks */
  33091. #ifdef DUK_USE_TRACEBACKS
  33092. /*
  33093. * If tracebacks are enabled, the '_Tracedata' property is the only
  33094. * thing we need: 'fileName' and 'lineNumber' are virtual properties
  33095. * which use '_Tracedata'.
  33096. */
  33097. if (duk_hobject_hasprop_raw(thr, obj, DUK_HTHREAD_STRING_INT_TRACEDATA(thr))) {
  33098. DUK_DDD(DUK_DDDPRINT("error value already has a '_Tracedata' property, not modifying it"));
  33099. } else {
  33100. duk__add_traceback(thr, thr_callstack, c_filename, c_line, noblame_fileline);
  33101. }
  33102. #else
  33103. /*
  33104. * If tracebacks are disabled, 'fileName' and 'lineNumber' are added
  33105. * as plain own properties. Since Error.prototype has accessors of
  33106. * the same name, we need to define own properties directly (cannot
  33107. * just use e.g. duk_put_prop_stridx). Existing properties are not
  33108. * overwritten in case they already exist.
  33109. */
  33110. if (thr->compile_ctx != NULL && thr->compile_ctx->h_filename != NULL) {
  33111. /* Compiler SyntaxError (or other error) gets the primary blame. */
  33112. duk_push_hstring(ctx, thr->compile_ctx->h_filename);
  33113. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE);
  33114. duk_push_uint(ctx, (duk_uint_t) thr->compile_ctx->curr_token.start_line);
  33115. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LINE_NUMBER, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE);
  33116. } else if (c_filename && !noblame_fileline) {
  33117. /* XXX: file/line is disabled in minimal builds, so disable this too
  33118. * when appropriate.
  33119. */
  33120. duk_push_string(ctx, c_filename);
  33121. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE);
  33122. duk_push_int(ctx, c_line);
  33123. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LINE_NUMBER, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE);
  33124. } else if (thr_callstack->callstack_top > 0) {
  33125. duk_activation *act;
  33126. duk_hobject *func;
  33127. act = thr_callstack->callstack + thr_callstack->callstack_top - 1;
  33128. DUK_ASSERT(act >= thr_callstack->callstack && act < thr_callstack->callstack + thr_callstack->callstack_size);
  33129. func = DUK_ACT_GET_FUNC(act);
  33130. if (func) {
  33131. duk_uint32_t pc;
  33132. /* PC points to next instruction, find offending PC. Note that
  33133. * PC == 0 for native code.
  33134. */
  33135. pc = act->pc;
  33136. if (pc > 0) {
  33137. pc--;
  33138. }
  33139. DUK_ASSERT_DISABLE(pc >= 0); /* unsigned */
  33140. DUK_ASSERT((duk_double_t) pc < DUK_DOUBLE_2TO32); /* assume PC is at most 32 bits and non-negative */
  33141. act = NULL; /* invalidated by pushes, so get out of the way */
  33142. duk_push_hobject(ctx, func);
  33143. /* [ ... error func ] */
  33144. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_FILE_NAME);
  33145. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE);
  33146. #if defined(DUK_USE_PC2LINE)
  33147. if (DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) {
  33148. duk_uint32_t ecma_line;
  33149. #if 0
  33150. duk_push_u32(ctx, pc);
  33151. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_PC, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAGS_NO_OVERWRITE);
  33152. #endif
  33153. ecma_line = duk_hobject_pc2line_query(ctx, -1, (duk_uint_fast32_t) pc);
  33154. if (ecma_line > 0) {
  33155. duk_push_u32(ctx, (duk_uint32_t) ecma_line); /* -> [ ... error func line ] */
  33156. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_LINE_NUMBER, DUK_PROPDESC_FLAGS_WC | DUK_PROPDESC_FLAG_NO_OVERWRITE);
  33157. }
  33158. } else {
  33159. /* Native function, no relevant lineNumber. */
  33160. }
  33161. #endif /* DUK_USE_PC2LINE */
  33162. duk_pop(ctx);
  33163. }
  33164. }
  33165. #endif /* DUK_USE_TRACEBACKS */
  33166. #ifdef DUK_USE_ASSERTIONS
  33167. DUK_ASSERT(duk_get_top(ctx) == entry_top);
  33168. #endif
  33169. }
  33170. #endif /* DUK_USE_AUGMENT_ERROR_CREATE */
  33171. /*
  33172. * Augment an error at creation time with _Tracedata/fileName/lineNumber
  33173. * and allow a user error handler (if defined) to process/replace the error.
  33174. * The error to be augmented is at the stack top.
  33175. *
  33176. * thr: thread containing the error value
  33177. * thr_callstack: thread which should be used for generating callstack etc.
  33178. * c_filename: C __FILE__ related to the error
  33179. * c_line: C __LINE__ related to the error
  33180. * noblame_fileline: if true, don't fileName/line as error source, otherwise use traceback
  33181. * (needed because user code filename/line are reported but internal ones
  33182. * are not)
  33183. *
  33184. * XXX: rename noblame_fileline to flags field; combine it to some existing
  33185. * field (there are only a few call sites so this may not be worth it).
  33186. */
  33187. #if defined(DUK_USE_AUGMENT_ERROR_CREATE)
  33188. 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) {
  33189. duk_context *ctx = (duk_context *) thr;
  33190. duk_hobject *obj;
  33191. DUK_ASSERT(thr != NULL);
  33192. DUK_ASSERT(thr_callstack != NULL);
  33193. DUK_ASSERT(ctx != NULL);
  33194. /* [ ... error ] */
  33195. /*
  33196. * Criteria for augmenting:
  33197. *
  33198. * - augmentation enabled in build (naturally)
  33199. * - error value internal prototype chain contains the built-in
  33200. * Error prototype object (i.e. 'val instanceof Error')
  33201. *
  33202. * Additional criteria for built-in augmenting:
  33203. *
  33204. * - error value is an extensible object
  33205. */
  33206. obj = duk_get_hobject(ctx, -1);
  33207. if (!obj) {
  33208. DUK_DDD(DUK_DDDPRINT("value is not an object, skip both built-in and user augment"));
  33209. return;
  33210. }
  33211. if (!duk_hobject_prototype_chain_contains(thr, obj, thr->builtins[DUK_BIDX_ERROR_PROTOTYPE], 1 /*ignore_loop*/)) {
  33212. /* If the value has a prototype loop, it's critical not to
  33213. * throw here. Instead, assume the value is not to be
  33214. * augmented.
  33215. */
  33216. DUK_DDD(DUK_DDDPRINT("value is not an error instance, skip both built-in and user augment"));
  33217. return;
  33218. }
  33219. if (DUK_HOBJECT_HAS_EXTENSIBLE(obj)) {
  33220. DUK_DDD(DUK_DDDPRINT("error meets criteria, built-in augment"));
  33221. duk__err_augment_builtin_throw(thr, thr_callstack, c_filename, c_line, noblame_fileline, obj);
  33222. } else {
  33223. DUK_DDD(DUK_DDDPRINT("error does not meet criteria, no built-in augment"));
  33224. }
  33225. /* [ ... error ] */
  33226. #if defined(DUK_USE_ERRCREATE)
  33227. duk__err_augment_user(thr, DUK_STRIDX_ERR_CREATE);
  33228. #endif
  33229. }
  33230. #endif /* DUK_USE_AUGMENT_ERROR_CREATE */
  33231. /*
  33232. * Augment an error at throw time; allow a user error handler (if defined)
  33233. * to process/replace the error. The error to be augmented is at the
  33234. * stack top.
  33235. */
  33236. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  33237. DUK_INTERNAL void duk_err_augment_error_throw(duk_hthread *thr) {
  33238. #if defined(DUK_USE_ERRTHROW)
  33239. duk__err_augment_user(thr, DUK_STRIDX_ERR_THROW);
  33240. #endif /* DUK_USE_ERRTHROW */
  33241. }
  33242. #endif /* DUK_USE_AUGMENT_ERROR_THROW */
  33243. #line 1 "duk_error_longjmp.c"
  33244. /*
  33245. * Do a longjmp call, calling the fatal error handler if no
  33246. * catchpoint exists.
  33247. */
  33248. /* include removed: duk_internal.h */
  33249. DUK_INTERNAL void duk_err_longjmp(duk_hthread *thr) {
  33250. DUK_ASSERT(thr != NULL);
  33251. if (!thr->heap->lj.jmpbuf_ptr) {
  33252. /*
  33253. * If we don't have a jmpbuf_ptr, there is little we can do
  33254. * except panic. The caller's expectation is that we never
  33255. * return.
  33256. */
  33257. DUK_D(DUK_DPRINT("uncaught error: type=%d iserror=%d value1=%!T value2=%!T",
  33258. (int) thr->heap->lj.type, (int) thr->heap->lj.iserror,
  33259. &thr->heap->lj.value1, &thr->heap->lj.value2));
  33260. duk_fatal((duk_context *) thr, DUK_ERR_UNCAUGHT_ERROR, "uncaught error");
  33261. DUK_UNREACHABLE();
  33262. }
  33263. DUK_LONGJMP(thr->heap->lj.jmpbuf_ptr->jb);
  33264. DUK_UNREACHABLE();
  33265. }
  33266. #line 1 "duk_error_misc.c"
  33267. /*
  33268. * Error helpers
  33269. */
  33270. /* include removed: duk_internal.h */
  33271. /*
  33272. * Get prototype object for an integer error code.
  33273. */
  33274. DUK_INTERNAL duk_hobject *duk_error_prototype_from_code(duk_hthread *thr, duk_errcode_t code) {
  33275. switch (code) {
  33276. case DUK_ERR_EVAL_ERROR:
  33277. return thr->builtins[DUK_BIDX_EVAL_ERROR_PROTOTYPE];
  33278. case DUK_ERR_RANGE_ERROR:
  33279. return thr->builtins[DUK_BIDX_RANGE_ERROR_PROTOTYPE];
  33280. case DUK_ERR_REFERENCE_ERROR:
  33281. return thr->builtins[DUK_BIDX_REFERENCE_ERROR_PROTOTYPE];
  33282. case DUK_ERR_SYNTAX_ERROR:
  33283. return thr->builtins[DUK_BIDX_SYNTAX_ERROR_PROTOTYPE];
  33284. case DUK_ERR_TYPE_ERROR:
  33285. return thr->builtins[DUK_BIDX_TYPE_ERROR_PROTOTYPE];
  33286. case DUK_ERR_URI_ERROR:
  33287. return thr->builtins[DUK_BIDX_URI_ERROR_PROTOTYPE];
  33288. /* XXX: more specific error classes? */
  33289. case DUK_ERR_UNIMPLEMENTED_ERROR:
  33290. case DUK_ERR_INTERNAL_ERROR:
  33291. case DUK_ERR_ALLOC_ERROR:
  33292. case DUK_ERR_ASSERTION_ERROR:
  33293. case DUK_ERR_API_ERROR:
  33294. case DUK_ERR_ERROR:
  33295. default:
  33296. return thr->builtins[DUK_BIDX_ERROR_PROTOTYPE];
  33297. }
  33298. }
  33299. /*
  33300. * Exposed helper for setting up heap longjmp state.
  33301. */
  33302. DUK_INTERNAL void duk_err_setup_heap_ljstate(duk_hthread *thr, duk_small_int_t lj_type) {
  33303. duk_tval tv_tmp;
  33304. thr->heap->lj.type = lj_type;
  33305. DUK_ASSERT(thr->valstack_top > thr->valstack);
  33306. DUK_TVAL_SET_TVAL(&tv_tmp, &thr->heap->lj.value1);
  33307. DUK_TVAL_SET_TVAL(&thr->heap->lj.value1, thr->valstack_top - 1);
  33308. DUK_TVAL_INCREF(thr, &thr->heap->lj.value1);
  33309. DUK_TVAL_DECREF(thr, &tv_tmp);
  33310. duk_pop((duk_context *) thr);
  33311. }
  33312. #line 1 "duk_error_throw.c"
  33313. /*
  33314. * Create and throw an Ecmascript error object based on a code and a message.
  33315. *
  33316. * Used when we throw errors internally. Ecmascript generated error objects
  33317. * are created by Ecmascript code, and the throwing is handled by the bytecode
  33318. * executor.
  33319. */
  33320. /* include removed: duk_internal.h */
  33321. /*
  33322. * Create and throw an error (originating from Duktape internally)
  33323. *
  33324. * Push an error object on top of the stack, possibly throw augmenting
  33325. * the error, and finally longjmp.
  33326. *
  33327. * If an error occurs while we're dealing with the current error, we might
  33328. * enter an infinite recursion loop. This is prevented by detecting a
  33329. * "double fault" through the heap->handling_error flag; the recursion
  33330. * then stops at the second level.
  33331. */
  33332. #ifdef DUK_USE_VERBOSE_ERRORS
  33333. 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) {
  33334. #else
  33335. DUK_INTERNAL void duk_err_create_and_throw(duk_hthread *thr, duk_errcode_t code) {
  33336. #endif
  33337. duk_context *ctx = (duk_context *) thr;
  33338. duk_bool_t double_error = thr->heap->handling_error;
  33339. #ifdef DUK_USE_VERBOSE_ERRORS
  33340. DUK_DD(DUK_DDPRINT("duk_err_create_and_throw(): code=%ld, msg=%s, filename=%s, line=%ld",
  33341. (long) code, (const char *) msg,
  33342. (const char *) filename, (long) line));
  33343. #else
  33344. DUK_DD(DUK_DDPRINT("duk_err_create_and_throw(): code=%ld", (long) code));
  33345. #endif
  33346. DUK_ASSERT(thr != NULL);
  33347. DUK_ASSERT(ctx != NULL);
  33348. thr->heap->handling_error = 1;
  33349. /*
  33350. * Create and push an error object onto the top of stack.
  33351. * If a "double error" occurs, use a fixed error instance
  33352. * to avoid further trouble.
  33353. */
  33354. /* XXX: if attempt to push beyond allocated valstack, this double fault
  33355. * handling fails miserably. We should really write the double error
  33356. * directly to thr->heap->lj.value1 and avoid valstack use entirely.
  33357. */
  33358. if (double_error) {
  33359. if (thr->builtins[DUK_BIDX_DOUBLE_ERROR]) {
  33360. DUK_D(DUK_DPRINT("double fault detected -> push built-in fixed 'double error' instance"));
  33361. duk_push_hobject_bidx(ctx, DUK_BIDX_DOUBLE_ERROR);
  33362. } else {
  33363. DUK_D(DUK_DPRINT("double fault detected; there is no built-in fixed 'double error' instance "
  33364. "-> push the error code as a number"));
  33365. duk_push_int(ctx, (duk_int_t) code);
  33366. }
  33367. } else {
  33368. /* Error object is augmented at its creation here. */
  33369. duk_require_stack(ctx, 1);
  33370. /* XXX: unnecessary '%s' formatting here, but cannot use
  33371. * 'msg' as a format string directly.
  33372. */
  33373. #ifdef DUK_USE_VERBOSE_ERRORS
  33374. duk_push_error_object_raw(ctx,
  33375. code | DUK_ERRCODE_FLAG_NOBLAME_FILELINE,
  33376. filename,
  33377. line,
  33378. "%s",
  33379. (const char *) msg);
  33380. #else
  33381. duk_push_error_object_raw(ctx,
  33382. code | DUK_ERRCODE_FLAG_NOBLAME_FILELINE,
  33383. NULL,
  33384. 0,
  33385. NULL);
  33386. #endif
  33387. }
  33388. /*
  33389. * Augment error (throw time), unless alloc/double error
  33390. */
  33391. if (double_error || code == DUK_ERR_ALLOC_ERROR) {
  33392. DUK_D(DUK_DPRINT("alloc or double error: skip throw augmenting to avoid further trouble"));
  33393. } else {
  33394. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  33395. DUK_DDD(DUK_DDDPRINT("THROW ERROR (INTERNAL): %!iT (before throw augment)",
  33396. (duk_tval *) duk_get_tval(ctx, -1)));
  33397. duk_err_augment_error_throw(thr);
  33398. #endif
  33399. }
  33400. /*
  33401. * Finally, longjmp
  33402. */
  33403. thr->heap->handling_error = 0;
  33404. duk_err_setup_heap_ljstate(thr, DUK_LJ_TYPE_THROW);
  33405. DUK_DDD(DUK_DDDPRINT("THROW ERROR (INTERNAL): %!iT, %!iT (after throw augment)",
  33406. (duk_tval *) &thr->heap->lj.value1, (duk_tval *) &thr->heap->lj.value2));
  33407. duk_err_longjmp(thr);
  33408. DUK_UNREACHABLE();
  33409. }
  33410. /*
  33411. * Helper for C function call negative return values.
  33412. */
  33413. DUK_INTERNAL void duk_error_throw_from_negative_rc(duk_hthread *thr, duk_ret_t rc) {
  33414. duk_context *ctx = (duk_context *) thr;
  33415. const char *msg;
  33416. duk_errcode_t code;
  33417. DUK_ASSERT(thr != NULL);
  33418. DUK_ASSERT(rc < 0);
  33419. /* XXX: this generates quite large code - perhaps select the error
  33420. * class based on the code and then just use the error 'name'?
  33421. */
  33422. code = -rc;
  33423. switch (rc) {
  33424. case DUK_RET_UNIMPLEMENTED_ERROR: msg = "unimplemented"; break;
  33425. case DUK_RET_UNSUPPORTED_ERROR: msg = "unsupported"; break;
  33426. case DUK_RET_INTERNAL_ERROR: msg = "internal"; break;
  33427. case DUK_RET_ALLOC_ERROR: msg = "alloc"; break;
  33428. case DUK_RET_ASSERTION_ERROR: msg = "assertion"; break;
  33429. case DUK_RET_API_ERROR: msg = "api"; break;
  33430. case DUK_RET_UNCAUGHT_ERROR: msg = "uncaught"; break;
  33431. case DUK_RET_ERROR: msg = "error"; break;
  33432. case DUK_RET_EVAL_ERROR: msg = "eval"; break;
  33433. case DUK_RET_RANGE_ERROR: msg = "range"; break;
  33434. case DUK_RET_REFERENCE_ERROR: msg = "reference"; break;
  33435. case DUK_RET_SYNTAX_ERROR: msg = "syntax"; break;
  33436. case DUK_RET_TYPE_ERROR: msg = "type"; break;
  33437. case DUK_RET_URI_ERROR: msg = "uri"; break;
  33438. default: msg = "unknown"; break;
  33439. }
  33440. DUK_ASSERT(msg != NULL);
  33441. /*
  33442. * The __FILE__ and __LINE__ information is intentionally not used in the
  33443. * creation of the error object, as it isn't useful in the tracedata. The
  33444. * tracedata still contains the function which returned the negative return
  33445. * code, and having the file/line of this function isn't very useful.
  33446. */
  33447. duk_error_raw(ctx, code, NULL, 0, "%s error (rc %ld)", (const char *) msg, (long) rc);
  33448. DUK_UNREACHABLE();
  33449. }
  33450. #line 1 "duk_hbuffer_alloc.c"
  33451. /*
  33452. * duk_hbuffer allocation and freeing.
  33453. */
  33454. /* include removed: duk_internal.h */
  33455. DUK_INTERNAL duk_hbuffer *duk_hbuffer_alloc(duk_heap *heap, duk_size_t size, duk_small_uint_t flags) {
  33456. duk_hbuffer *res = NULL;
  33457. duk_size_t alloc_size;
  33458. DUK_DDD(DUK_DDDPRINT("allocate hbuffer"));
  33459. /* Size sanity check. Should not be necessary because caller is
  33460. * required to check this, but we don't want to cause a segfault
  33461. * if the size wraps either in duk_size_t computation or when
  33462. * storing the size in a 16-bit field.
  33463. */
  33464. if (size > DUK_HBUFFER_MAX_BYTELEN) {
  33465. DUK_D(DUK_DPRINT("hbuffer alloc failed: size too large: %ld", (long) size));
  33466. return NULL;
  33467. }
  33468. if (flags & DUK_BUF_FLAG_DYNAMIC) {
  33469. alloc_size = sizeof(duk_hbuffer_dynamic);
  33470. } else {
  33471. alloc_size = sizeof(duk_hbuffer_fixed) + size;
  33472. DUK_ASSERT(alloc_size >= sizeof(duk_hbuffer_fixed)); /* no wrapping */
  33473. }
  33474. res = (duk_hbuffer *) DUK_ALLOC(heap, alloc_size);
  33475. if (!res) {
  33476. goto error;
  33477. }
  33478. /* zero everything unless requested not to do so */
  33479. #if defined(DUK_USE_ZERO_BUFFER_DATA)
  33480. DUK_MEMZERO((void *) res,
  33481. (flags & DUK_BUF_FLAG_NOZERO) ?
  33482. ((flags & DUK_BUF_FLAG_DYNAMIC) ?
  33483. sizeof(duk_hbuffer_dynamic) :
  33484. sizeof(duk_hbuffer_fixed)) :
  33485. alloc_size);
  33486. #else
  33487. DUK_MEMZERO((void *) res,
  33488. (flags & DUK_BUF_FLAG_DYNAMIC) ? sizeof(duk_hbuffer_dynamic) : sizeof(duk_hbuffer_fixed));
  33489. #endif
  33490. if (flags & DUK_BUF_FLAG_DYNAMIC) {
  33491. duk_hbuffer_dynamic *h = (duk_hbuffer_dynamic *) res;
  33492. void *ptr;
  33493. if (size > 0) {
  33494. DUK_DDD(DUK_DDDPRINT("dynamic buffer with nonzero size, alloc actual buffer"));
  33495. #ifdef DUK_USE_ZERO_BUFFER_DATA
  33496. ptr = DUK_ALLOC_ZEROED(heap, size);
  33497. #else
  33498. ptr = DUK_ALLOC(heap, size);
  33499. #endif
  33500. if (!ptr) {
  33501. /* Because size > 0, NULL check is correct */
  33502. goto error;
  33503. }
  33504. DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(heap, h, ptr);
  33505. DUK_HBUFFER_DYNAMIC_SET_ALLOC_SIZE(h, size); /* snug */
  33506. } else {
  33507. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  33508. h->curr_alloc = NULL;
  33509. #endif
  33510. DUK_ASSERT(DUK_HBUFFER_DYNAMIC_GET_ALLOC_SIZE(h) == 0);
  33511. }
  33512. }
  33513. DUK_HBUFFER_SET_SIZE(res, size);
  33514. DUK_HEAPHDR_SET_TYPE(&res->hdr, DUK_HTYPE_BUFFER);
  33515. if (flags & DUK_BUF_FLAG_DYNAMIC) {
  33516. DUK_HBUFFER_SET_DYNAMIC(res);
  33517. }
  33518. DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap, &res->hdr);
  33519. DUK_DDD(DUK_DDDPRINT("allocated hbuffer: %p", (void *) res));
  33520. return res;
  33521. error:
  33522. DUK_DD(DUK_DDPRINT("hbuffer allocation failed"));
  33523. DUK_FREE(heap, res);
  33524. return NULL;
  33525. }
  33526. /* For indirect allocs. */
  33527. DUK_INTERNAL void *duk_hbuffer_get_dynalloc_ptr(duk_heap *heap, void *ud) {
  33528. duk_hbuffer_dynamic *buf = (duk_hbuffer_dynamic *) ud;
  33529. DUK_UNREF(heap);
  33530. return (void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap, buf);
  33531. }
  33532. #line 1 "duk_hbuffer_ops.c"
  33533. /*
  33534. * duk_hbuffer operations such as resizing and inserting/appending data to
  33535. * a dynamic buffer.
  33536. *
  33537. * Append operations append to the end of the buffer and they are relatively
  33538. * efficient: the buffer is grown with a "spare" part relative to the buffer
  33539. * size to minimize reallocations. Insert operations need to move existing
  33540. * data forward in the buffer with memmove() and are not very efficient.
  33541. * They are used e.g. by the regexp compiler to "backpatch" regexp bytecode.
  33542. */
  33543. /* include removed: duk_internal.h */
  33544. /*
  33545. * Resizing
  33546. */
  33547. DUK_LOCAL duk_size_t duk__add_spare(duk_size_t size) {
  33548. duk_size_t spare = (size / DUK_HBUFFER_SPARE_DIVISOR) + DUK_HBUFFER_SPARE_ADD;
  33549. duk_size_t res;
  33550. res = size + spare;
  33551. if (res < size) {
  33552. /* XXX: handle corner cases where size is close to size limit (wraparound) */
  33553. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "duk_size_t wrapped");
  33554. }
  33555. DUK_ASSERT(res >= size);
  33556. return res;
  33557. }
  33558. DUK_INTERNAL void duk_hbuffer_resize(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t new_size, duk_size_t new_alloc_size) {
  33559. void *res;
  33560. duk_size_t prev_alloc_size;
  33561. DUK_ASSERT(thr != NULL);
  33562. DUK_ASSERT(buf != NULL);
  33563. DUK_ASSERT(new_alloc_size >= new_size);
  33564. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33565. /*
  33566. * Maximum size check
  33567. */
  33568. if (new_alloc_size > DUK_HBUFFER_MAX_BYTELEN) {
  33569. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, "buffer too long");
  33570. }
  33571. /*
  33572. * Note: use indirect realloc variant just in case mark-and-sweep
  33573. * (finalizers) might resize this same buffer during garbage
  33574. * collection.
  33575. */
  33576. res = DUK_REALLOC_INDIRECT(thr->heap, duk_hbuffer_get_dynalloc_ptr, (void *) buf, new_alloc_size);
  33577. if (res != NULL || new_alloc_size == 0) {
  33578. /* 'res' may be NULL if new allocation size is 0. */
  33579. DUK_DDD(DUK_DDDPRINT("resized dynamic buffer %p:%ld:%ld -> %p:%ld:%ld",
  33580. (void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, buf),
  33581. (long) DUK_HBUFFER_DYNAMIC_GET_SIZE(buf),
  33582. (long) DUK_HBUFFER_DYNAMIC_GET_ALLOC_SIZE(buf),
  33583. (void *) res,
  33584. (long) new_size,
  33585. (long) new_alloc_size));
  33586. /*
  33587. * The entire allocated buffer area, regardless of actual used
  33588. * size, is kept zeroed in resizes for simplicity. If the buffer
  33589. * is grown, zero the new part. Another policy would be to
  33590. * ensure data is zeroed as the used part is extended. The
  33591. * current approach is much more simple and is not a big deal
  33592. * because the spare part is relatively small.
  33593. */
  33594. prev_alloc_size = DUK_HBUFFER_DYNAMIC_GET_ALLOC_SIZE(buf);
  33595. if (new_alloc_size > prev_alloc_size) {
  33596. DUK_ASSERT(new_alloc_size - prev_alloc_size > 0);
  33597. #ifdef DUK_USE_ZERO_BUFFER_DATA
  33598. DUK_MEMZERO((void *) ((char *) res + prev_alloc_size),
  33599. new_alloc_size - prev_alloc_size);
  33600. #endif
  33601. }
  33602. DUK_HBUFFER_DYNAMIC_SET_SIZE(buf, new_size);
  33603. DUK_HBUFFER_DYNAMIC_SET_ALLOC_SIZE(buf, new_alloc_size);
  33604. DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(thr->heap, buf, res);
  33605. } else {
  33606. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, "buffer resize failed: %ld:%ld to %ld:%ld",
  33607. (long) DUK_HBUFFER_DYNAMIC_GET_SIZE(buf),
  33608. (long) DUK_HBUFFER_DYNAMIC_GET_ALLOC_SIZE(buf),
  33609. (long) new_size,
  33610. (long) new_alloc_size);
  33611. }
  33612. DUK_ASSERT(res != NULL || new_alloc_size == 0);
  33613. }
  33614. DUK_INTERNAL void duk_hbuffer_reset(duk_hthread *thr, duk_hbuffer_dynamic *buf) {
  33615. DUK_ASSERT(thr != NULL);
  33616. DUK_ASSERT(buf != NULL);
  33617. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33618. duk_hbuffer_resize(thr, buf, 0, 0);
  33619. }
  33620. #if 0 /*unused*/
  33621. DUK_INTERNAL void duk_hbuffer_compact(duk_hthread *thr, duk_hbuffer_dynamic *buf) {
  33622. duk_size_t curr_size;
  33623. DUK_ASSERT(thr != NULL);
  33624. DUK_ASSERT(buf != NULL);
  33625. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33626. curr_size = DUK_HBUFFER_GET_SIZE(buf);
  33627. duk_hbuffer_resize(thr, buf, curr_size, curr_size);
  33628. }
  33629. #endif
  33630. /*
  33631. * Inserts
  33632. */
  33633. DUK_INTERNAL void duk_hbuffer_insert_bytes(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t offset, const duk_uint8_t *data, duk_size_t length) {
  33634. duk_uint8_t *p;
  33635. /* XXX: allow inserts with offset > curr_size? i.e., insert zeroes automatically? */
  33636. DUK_ASSERT(thr != NULL);
  33637. DUK_ASSERT(buf != NULL);
  33638. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33639. DUK_ASSERT_DISABLE(offset >= 0); /* unsigned, so always true */
  33640. DUK_ASSERT(offset <= DUK_HBUFFER_GET_SIZE(buf)); /* equality is OK (= append) */
  33641. DUK_ASSERT(data != NULL);
  33642. DUK_ASSERT_DISABLE(length >= 0); /* unsigned, so always true */
  33643. if (length == 0) {
  33644. return;
  33645. }
  33646. if (DUK_HBUFFER_DYNAMIC_GET_SPARE_SIZE(buf) < length) {
  33647. duk_hbuffer_resize(thr,
  33648. buf,
  33649. DUK_HBUFFER_GET_SIZE(buf),
  33650. duk__add_spare(DUK_HBUFFER_GET_SIZE(buf) + length));
  33651. }
  33652. DUK_ASSERT(DUK_HBUFFER_DYNAMIC_GET_SPARE_SIZE(buf) >= length);
  33653. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, buf);
  33654. if (offset < DUK_HBUFFER_GET_SIZE(buf)) {
  33655. /* not an append */
  33656. DUK_ASSERT(DUK_HBUFFER_GET_SIZE(buf) - offset > 0);
  33657. DUK_MEMMOVE((void *) (p + offset + length),
  33658. (void *) (p + offset),
  33659. DUK_HBUFFER_GET_SIZE(buf) - offset);
  33660. }
  33661. DUK_ASSERT(length > 0);
  33662. DUK_MEMCPY((void *) (p + offset),
  33663. data,
  33664. length);
  33665. DUK_HBUFFER_DYNAMIC_ADD_SIZE(buf, length);
  33666. }
  33667. #if 0 /*unused*/
  33668. DUK_INTERNAL void duk_hbuffer_insert_byte(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t offset, duk_uint8_t byte) {
  33669. DUK_ASSERT(thr != NULL);
  33670. DUK_ASSERT(buf != NULL);
  33671. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33672. duk_hbuffer_insert_bytes(thr, buf, offset, &byte, 1);
  33673. }
  33674. #endif
  33675. #if 0 /*unused*/
  33676. DUK_INTERNAL duk_size_t duk_hbuffer_insert_cstring(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t offset, const char *str) {
  33677. duk_size_t len;
  33678. DUK_ASSERT(thr != NULL);
  33679. DUK_ASSERT(buf != NULL);
  33680. DUK_ASSERT(str != NULL);
  33681. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33682. len = DUK_STRLEN(str);
  33683. duk_hbuffer_insert_bytes(thr, buf, offset, (duk_uint8_t *) str, len);
  33684. return len;
  33685. }
  33686. #endif
  33687. #if 0 /*unused*/
  33688. DUK_INTERNAL duk_size_t duk_hbuffer_insert_hstring(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t offset, duk_hstring *str) {
  33689. duk_size_t len;
  33690. DUK_ASSERT(thr != NULL);
  33691. DUK_ASSERT(buf != NULL);
  33692. DUK_ASSERT(str != NULL);
  33693. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33694. len = DUK_HSTRING_GET_BYTELEN(str);
  33695. duk_hbuffer_insert_bytes(thr, buf, offset, (duk_uint8_t *) DUK_HSTRING_GET_DATA(str), len);
  33696. return len;
  33697. }
  33698. #endif
  33699. DUK_INTERNAL duk_size_t duk_hbuffer_insert_xutf8(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t offset, duk_ucodepoint_t codepoint) {
  33700. duk_uint8_t tmp[DUK_UNICODE_MAX_XUTF8_LENGTH];
  33701. duk_size_t len;
  33702. DUK_ASSERT(thr != NULL);
  33703. DUK_ASSERT(buf != NULL);
  33704. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33705. /* No range assertion for 'codepoint' */
  33706. /* Intentionally no fast path: insertion is not that central */
  33707. len = (duk_size_t) duk_unicode_encode_xutf8(codepoint, tmp);
  33708. duk_hbuffer_insert_bytes(thr, buf, offset, tmp, len);
  33709. return len;
  33710. }
  33711. /* Append a Unicode codepoint to the buffer in CESU-8 format, i.e., convert
  33712. * non-BMP characters to surrogate pairs which are then "UTF-8" encoded.
  33713. * If the codepoint is initially a surrogate, it is also encoded into CESU-8.
  33714. * Codepoints above valid Unicode range (> U+10FFFF) are mangled.
  33715. */
  33716. #if 0 /*unused*/
  33717. DUK_INTERNAL duk_size_t duk_hbuffer_insert_cesu8(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t offset, duk_ucodepoint_t codepoint) {
  33718. duk_uint8_t tmp[DUK_UNICODE_MAX_CESU8_LENGTH];
  33719. duk_size_t len;
  33720. DUK_ASSERT(thr != NULL);
  33721. DUK_ASSERT(buf != NULL);
  33722. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33723. DUK_ASSERT_DISABLE(codepoint >= 0); /* unsigned */
  33724. DUK_ASSERT(codepoint <= 0x10ffff); /* if not in this range, results are garbage (but no crash) */
  33725. /* Intentionally no fast path: insertion is not that central */
  33726. len = (duk_size_t) duk_unicode_encode_cesu8(codepoint, tmp);
  33727. duk_hbuffer_insert_bytes(thr, buf, offset, tmp, len);
  33728. return len;
  33729. }
  33730. #endif
  33731. /*
  33732. * Appends
  33733. *
  33734. * Note: an optimized duk_hbuffer_append_bytes() could be implemented, but
  33735. * it is more compact to use duk_hbuffer_insert_bytes() instead. The
  33736. * important fast paths bypass these functions. anyway.
  33737. */
  33738. DUK_INTERNAL void duk_hbuffer_append_bytes(duk_hthread *thr, duk_hbuffer_dynamic *buf, const duk_uint8_t *data, duk_size_t length) {
  33739. DUK_ASSERT(thr != NULL);
  33740. DUK_ASSERT(buf != NULL);
  33741. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33742. DUK_ASSERT(data != NULL);
  33743. duk_hbuffer_insert_bytes(thr, buf, DUK_HBUFFER_GET_SIZE(buf), data, length);
  33744. }
  33745. DUK_INTERNAL void duk_hbuffer_append_byte(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_uint8_t byte) {
  33746. DUK_ASSERT(thr != NULL);
  33747. DUK_ASSERT(buf != NULL);
  33748. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33749. duk_hbuffer_insert_bytes(thr, buf, DUK_HBUFFER_GET_SIZE(buf), &byte, 1);
  33750. }
  33751. DUK_INTERNAL duk_size_t duk_hbuffer_append_cstring(duk_hthread *thr, duk_hbuffer_dynamic *buf, const char *str) {
  33752. duk_size_t len;
  33753. DUK_ASSERT(thr != NULL);
  33754. DUK_ASSERT(buf != NULL);
  33755. DUK_ASSERT(str != NULL);
  33756. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33757. len = DUK_STRLEN(str);
  33758. duk_hbuffer_insert_bytes(thr, buf, DUK_HBUFFER_GET_SIZE(buf), (duk_uint8_t *) str, len);
  33759. return len;
  33760. }
  33761. DUK_INTERNAL duk_size_t duk_hbuffer_append_hstring(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_hstring *str) {
  33762. duk_size_t len;
  33763. DUK_ASSERT(thr != NULL);
  33764. DUK_ASSERT(buf != NULL);
  33765. DUK_ASSERT(str != NULL);
  33766. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33767. len = DUK_HSTRING_GET_BYTELEN(str);
  33768. duk_hbuffer_insert_bytes(thr, buf, DUK_HBUFFER_GET_SIZE(buf), (duk_uint8_t *) DUK_HSTRING_GET_DATA(str), len);
  33769. return len;
  33770. }
  33771. /* Append a Unicode codepoint to the buffer in extended UTF-8 format, i.e.
  33772. * allow codepoints above standard Unicode range (> U+10FFFF) up to seven
  33773. * byte encoding (36 bits, but argument type is 32 bits). In particular,
  33774. * allows encoding of all unsigned 32-bit integers. If the codepoint is
  33775. * initially a surrogate, it is encoded without checking (and will become,
  33776. * effectively, CESU-8 encoded).
  33777. */
  33778. DUK_INTERNAL duk_size_t duk_hbuffer_append_xutf8(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_ucodepoint_t codepoint) {
  33779. duk_uint8_t tmp[DUK_UNICODE_MAX_XUTF8_LENGTH];
  33780. duk_size_t len;
  33781. duk_size_t sz;
  33782. DUK_ASSERT(thr != NULL);
  33783. DUK_ASSERT(buf != NULL);
  33784. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33785. /* No range assertion for 'codepoint' */
  33786. if (DUK_LIKELY(codepoint < 0x80 && DUK_HBUFFER_DYNAMIC_GET_SPARE_SIZE(buf) > 0)) {
  33787. /* fast path: ASCII and there is spare */
  33788. duk_uint8_t *p = ((duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, buf));
  33789. sz = DUK_HBUFFER_DYNAMIC_GET_SIZE(buf);
  33790. p[sz++] = (duk_uint8_t) codepoint;
  33791. DUK_HBUFFER_DYNAMIC_SET_SIZE(buf, sz);
  33792. return 1;
  33793. }
  33794. len = (duk_size_t) duk_unicode_encode_xutf8(codepoint, tmp);
  33795. duk_hbuffer_insert_bytes(thr, buf, DUK_HBUFFER_GET_SIZE(buf), tmp, len);
  33796. return len;
  33797. }
  33798. /* Append a Unicode codepoint to the buffer in CESU-8 format, i.e., convert
  33799. * non-BMP characters to surrogate pairs which are then "UTF-8" encoded.
  33800. * If the codepoint is initially a surrogate, it is also encoded into CESU-8.
  33801. * Codepoints above valid Unicode range (> U+10FFFF) are mangled.
  33802. */
  33803. DUK_INTERNAL duk_size_t duk_hbuffer_append_cesu8(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_ucodepoint_t codepoint) {
  33804. duk_uint8_t tmp[DUK_UNICODE_MAX_CESU8_LENGTH];
  33805. duk_size_t len;
  33806. duk_size_t sz;
  33807. DUK_ASSERT(thr != NULL);
  33808. DUK_ASSERT(buf != NULL);
  33809. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33810. DUK_ASSERT_DISABLE(codepoint >= 0); /* unsigned */
  33811. DUK_ASSERT(codepoint <= 0x10ffff); /* if not in this range, results are garbage (but no crash) */
  33812. if (DUK_LIKELY(codepoint < 0x80 && DUK_HBUFFER_DYNAMIC_GET_SPARE_SIZE(buf) > 0)) {
  33813. /* fast path: ASCII and there is spare */
  33814. duk_uint8_t *p = ((duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, buf));
  33815. sz = DUK_HBUFFER_DYNAMIC_GET_SIZE(buf);
  33816. p[sz++] = (duk_uint8_t) codepoint;
  33817. DUK_HBUFFER_DYNAMIC_SET_SIZE(buf, sz);
  33818. return 1;
  33819. }
  33820. len = (duk_size_t) duk_unicode_encode_cesu8(codepoint, tmp);
  33821. duk_hbuffer_insert_bytes(thr, buf, DUK_HBUFFER_GET_SIZE(buf), tmp, len);
  33822. return len;
  33823. }
  33824. /* Append an duk_uint32_t in native byte order. */
  33825. #if 0 /*unused*/
  33826. DUK_INTERNAL void duk_hbuffer_append_native_u32(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_uint32_t val) {
  33827. /* relies on duk_uint32_t being exactly right size */
  33828. DUK_ASSERT(sizeof(val) == 4);
  33829. duk_hbuffer_insert_bytes(thr,
  33830. buf,
  33831. DUK_HBUFFER_GET_SIZE(buf),
  33832. (duk_uint8_t *) &val,
  33833. sizeof(duk_uint32_t));
  33834. }
  33835. #endif
  33836. /*
  33837. * In-buffer "slices"
  33838. *
  33839. * Slices are identified with an offset+length pair, referring to the current
  33840. * buffer data. A caller cannot otherwise reliably refer to existing data,
  33841. * because the buffer may be reallocated before a data pointer is referenced.
  33842. */
  33843. DUK_INTERNAL void duk_hbuffer_remove_slice(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t offset, duk_size_t length) {
  33844. duk_uint8_t *p;
  33845. duk_size_t end_offset;
  33846. DUK_UNREF(thr);
  33847. DUK_ASSERT(thr != NULL);
  33848. DUK_ASSERT(buf != NULL);
  33849. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33850. DUK_ASSERT_DISABLE(offset >= 0); /* always true */
  33851. DUK_ASSERT(offset <= DUK_HBUFFER_GET_SIZE(buf)); /* allow equality */
  33852. DUK_ASSERT_DISABLE(length >= 0); /* always true */
  33853. DUK_ASSERT(offset + length <= DUK_HBUFFER_GET_SIZE(buf)); /* allow equality */
  33854. if (length == 0) {
  33855. return;
  33856. }
  33857. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, buf);
  33858. end_offset = offset + length;
  33859. if (end_offset < DUK_HBUFFER_GET_SIZE(buf)) {
  33860. /* not strictly from end of buffer; need to shuffle data */
  33861. DUK_ASSERT(DUK_HBUFFER_GET_SIZE(buf) - end_offset > 0);
  33862. DUK_MEMMOVE(p + offset,
  33863. p + end_offset,
  33864. DUK_HBUFFER_GET_SIZE(buf) - end_offset);
  33865. }
  33866. /* Here we want to zero data even with automatic buffer zeroing
  33867. * disabled as we depend on this internally too.
  33868. */
  33869. DUK_ASSERT(length > 0);
  33870. DUK_MEMZERO(p + DUK_HBUFFER_GET_SIZE(buf) - length,
  33871. length);
  33872. DUK_HBUFFER_DYNAMIC_SUB_SIZE(buf, length);
  33873. /* Note: no shrink check, intentional */
  33874. }
  33875. DUK_INTERNAL void duk_hbuffer_insert_slice(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t dst_offset, duk_size_t src_offset, duk_size_t length) {
  33876. duk_uint8_t *p;
  33877. duk_size_t src_end_offset; /* source end (exclusive) in initial buffer */
  33878. duk_size_t len;
  33879. DUK_ASSERT(thr != NULL);
  33880. DUK_ASSERT(buf != NULL);
  33881. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33882. DUK_ASSERT_DISABLE(dst_offset >= 0); /* always true */
  33883. DUK_ASSERT(dst_offset <= DUK_HBUFFER_GET_SIZE(buf)); /* allow equality */
  33884. DUK_ASSERT_DISABLE(src_offset >= 0); /* always true */
  33885. DUK_ASSERT(src_offset <= DUK_HBUFFER_GET_SIZE(buf)); /* allow equality */
  33886. DUK_ASSERT_DISABLE(length >= 0); /* always true */
  33887. DUK_ASSERT(src_offset + length <= DUK_HBUFFER_GET_SIZE(buf)); /* allow equality */
  33888. if (length == 0) {
  33889. return;
  33890. }
  33891. if (DUK_HBUFFER_DYNAMIC_GET_SPARE_SIZE(buf) < length) {
  33892. duk_hbuffer_resize(thr,
  33893. buf,
  33894. DUK_HBUFFER_GET_SIZE(buf),
  33895. duk__add_spare(DUK_HBUFFER_GET_SIZE(buf) + length));
  33896. }
  33897. DUK_ASSERT(DUK_HBUFFER_DYNAMIC_GET_SPARE_SIZE(buf) >= length);
  33898. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, buf);
  33899. DUK_ASSERT(p != NULL); /* must be the case because length > 0, and buffer has been resized if necessary */
  33900. /*
  33901. * src_offset and dst_offset refer to the state of the buffer
  33902. * before any changes are made. This must be taken into account
  33903. * when moving data around; in particular, the source data may
  33904. * "straddle" the dst_offset, so the insert may need to be handled
  33905. * in two pieces.
  33906. */
  33907. src_end_offset = src_offset + length;
  33908. /* create a hole for the insert */
  33909. len = DUK_HBUFFER_GET_SIZE(buf) - dst_offset;
  33910. DUK_MEMMOVE(p + dst_offset + length,
  33911. p + dst_offset,
  33912. len); /* zero size is not an issue: pointers are valid */
  33913. if (src_offset < dst_offset) {
  33914. if (src_end_offset <= dst_offset) {
  33915. /* entire source is before 'dst_offset' */
  33916. DUK_MEMCPY(p + dst_offset,
  33917. p + src_offset,
  33918. length);
  33919. } else {
  33920. /* part of the source is before 'dst_offset'; straddles */
  33921. len = dst_offset - src_offset;
  33922. DUK_ASSERT(len >= 1 && len < length);
  33923. DUK_ASSERT(length - len >= 1);
  33924. DUK_MEMCPY(p + dst_offset,
  33925. p + src_offset,
  33926. len);
  33927. DUK_MEMCPY(p + dst_offset + len,
  33928. p + src_offset + length + len, /* take above memmove() into account */
  33929. length - len);
  33930. }
  33931. } else {
  33932. /* entire source is after 'dst_offset' */
  33933. DUK_MEMCPY(p + dst_offset,
  33934. p + src_offset + length, /* take above memmove() into account */
  33935. length);
  33936. }
  33937. DUK_HBUFFER_DYNAMIC_ADD_SIZE(buf, length);
  33938. }
  33939. DUK_INTERNAL void duk_hbuffer_append_slice(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t src_offset, duk_size_t length) {
  33940. DUK_ASSERT(thr != NULL);
  33941. DUK_ASSERT(buf != NULL);
  33942. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  33943. DUK_ASSERT_DISABLE(src_offset >= 0); /* always true */
  33944. DUK_ASSERT(src_offset <= DUK_HBUFFER_GET_SIZE(buf)); /* allow equality */
  33945. DUK_ASSERT_DISABLE(length >= 0); /* always true */
  33946. DUK_ASSERT(src_offset + length <= DUK_HBUFFER_GET_SIZE(buf)); /* allow equality */
  33947. duk_hbuffer_insert_slice(thr,
  33948. buf,
  33949. DUK_HBUFFER_GET_SIZE(buf),
  33950. src_offset,
  33951. length);
  33952. }
  33953. /* include removed: duk_internal.h */
  33954. #line 2 "duk_hbufferobject_misc.c"
  33955. DUK_INTERNAL duk_uint_t duk_hbufferobject_clamp_bytelength(duk_hbufferobject *h_bufobj, duk_uint_t len) {
  33956. duk_uint_t buf_size;
  33957. duk_uint_t buf_avail;
  33958. DUK_ASSERT(h_bufobj != NULL);
  33959. DUK_ASSERT(h_bufobj->buf != NULL);
  33960. buf_size = DUK_HBUFFER_GET_SIZE(h_bufobj->buf);
  33961. if (h_bufobj->offset > buf_size) {
  33962. /* Slice starting point is beyond current length. */
  33963. return 0;
  33964. }
  33965. buf_avail = buf_size - h_bufobj->offset;
  33966. return buf_avail >= len ? len : buf_avail;
  33967. }
  33968. #line 1 "duk_heap_alloc.c"
  33969. /*
  33970. * duk_heap allocation and freeing.
  33971. */
  33972. /* include removed: duk_internal.h */
  33973. /* constants for built-in string data depacking */
  33974. #define DUK__BITPACK_LETTER_LIMIT 26
  33975. #define DUK__BITPACK_UNDERSCORE 26
  33976. #define DUK__BITPACK_FF 27
  33977. #define DUK__BITPACK_SWITCH1 29
  33978. #define DUK__BITPACK_SWITCH 30
  33979. #define DUK__BITPACK_SEVENBIT 31
  33980. /*
  33981. * Free a heap object.
  33982. *
  33983. * Free heap object and its internal (non-heap) pointers. Assumes that
  33984. * caller has removed the object from heap allocated list or the string
  33985. * intern table, and any weak references (which strings may have) have
  33986. * been already dealt with.
  33987. */
  33988. DUK_INTERNAL void duk_free_hobject_inner(duk_heap *heap, duk_hobject *h) {
  33989. DUK_ASSERT(heap != NULL);
  33990. DUK_ASSERT(h != NULL);
  33991. DUK_FREE(heap, DUK_HOBJECT_GET_PROPS(heap, h));
  33992. if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h)) {
  33993. duk_hcompiledfunction *f = (duk_hcompiledfunction *) h;
  33994. DUK_UNREF(f);
  33995. /* Currently nothing to free; 'data' is a heap object */
  33996. } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
  33997. duk_hnativefunction *f = (duk_hnativefunction *) h;
  33998. DUK_UNREF(f);
  33999. /* Currently nothing to free */
  34000. } else if (DUK_HOBJECT_IS_THREAD(h)) {
  34001. duk_hthread *t = (duk_hthread *) h;
  34002. DUK_FREE(heap, t->valstack);
  34003. DUK_FREE(heap, t->callstack);
  34004. DUK_FREE(heap, t->catchstack);
  34005. /* Don't free h->resumer because it exists in the heap.
  34006. * Callstack entries also contain function pointers which
  34007. * are not freed for the same reason.
  34008. */
  34009. /* XXX: with 'caller' property the callstack would need
  34010. * to be unwound to update the 'caller' properties of
  34011. * functions in the callstack.
  34012. */
  34013. }
  34014. }
  34015. DUK_INTERNAL void duk_free_hbuffer_inner(duk_heap *heap, duk_hbuffer *h) {
  34016. DUK_ASSERT(heap != NULL);
  34017. DUK_ASSERT(h != NULL);
  34018. if (DUK_HBUFFER_HAS_DYNAMIC(h)) {
  34019. duk_hbuffer_dynamic *g = (duk_hbuffer_dynamic *) h;
  34020. DUK_DDD(DUK_DDDPRINT("free dynamic buffer %p", (void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap, g)));
  34021. DUK_FREE(heap, DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap, g));
  34022. }
  34023. }
  34024. DUK_INTERNAL void duk_free_hstring_inner(duk_heap *heap, duk_hstring *h) {
  34025. DUK_ASSERT(heap != NULL);
  34026. DUK_ASSERT(h != NULL);
  34027. DUK_UNREF(heap);
  34028. DUK_UNREF(h);
  34029. #if defined(DUK_USE_HSTRING_EXTDATA) && defined(DUK_USE_EXTSTR_FREE)
  34030. if (DUK_HSTRING_HAS_EXTDATA(h)) {
  34031. DUK_DDD(DUK_DDDPRINT("free extstr: hstring %!O, extdata: %p",
  34032. h, DUK_HSTRING_GET_EXTDATA((duk_hstring_external *) h)));
  34033. DUK_USE_EXTSTR_FREE(heap->heap_udata, (const void *) DUK_HSTRING_GET_EXTDATA((duk_hstring_external *) h));
  34034. }
  34035. #endif
  34036. }
  34037. DUK_INTERNAL void duk_heap_free_heaphdr_raw(duk_heap *heap, duk_heaphdr *hdr) {
  34038. DUK_ASSERT(heap);
  34039. DUK_ASSERT(hdr);
  34040. DUK_DDD(DUK_DDDPRINT("free heaphdr %p, htype %ld", (void *) hdr, (long) DUK_HEAPHDR_GET_TYPE(hdr)));
  34041. switch ((int) DUK_HEAPHDR_GET_TYPE(hdr)) {
  34042. case DUK_HTYPE_STRING:
  34043. duk_free_hstring_inner(heap, (duk_hstring *) hdr);
  34044. break;
  34045. case DUK_HTYPE_OBJECT:
  34046. duk_free_hobject_inner(heap, (duk_hobject *) hdr);
  34047. break;
  34048. case DUK_HTYPE_BUFFER:
  34049. duk_free_hbuffer_inner(heap, (duk_hbuffer *) hdr);
  34050. break;
  34051. default:
  34052. DUK_UNREACHABLE();
  34053. }
  34054. DUK_FREE(heap, hdr);
  34055. }
  34056. /*
  34057. * Free the heap.
  34058. *
  34059. * Frees heap-related non-heap-tracked allocations such as the
  34060. * string intern table; then frees the heap allocated objects;
  34061. * and finally frees the heap structure itself. Reference counts
  34062. * and GC markers are ignored (and not updated) in this process,
  34063. * and finalizers won't be called.
  34064. *
  34065. * The heap pointer and heap object pointers must not be used
  34066. * after this call.
  34067. */
  34068. DUK_LOCAL void duk__free_allocated(duk_heap *heap) {
  34069. duk_heaphdr *curr;
  34070. duk_heaphdr *next;
  34071. curr = heap->heap_allocated;
  34072. while (curr) {
  34073. /* We don't log or warn about freeing zero refcount objects
  34074. * because they may happen with finalizer processing.
  34075. */
  34076. DUK_DDD(DUK_DDDPRINT("FINALFREE (allocated): %!iO",
  34077. (duk_heaphdr *) curr));
  34078. next = DUK_HEAPHDR_GET_NEXT(heap, curr);
  34079. duk_heap_free_heaphdr_raw(heap, curr);
  34080. curr = next;
  34081. }
  34082. }
  34083. #ifdef DUK_USE_REFERENCE_COUNTING
  34084. DUK_LOCAL void duk__free_refzero_list(duk_heap *heap) {
  34085. duk_heaphdr *curr;
  34086. duk_heaphdr *next;
  34087. curr = heap->refzero_list;
  34088. while (curr) {
  34089. DUK_DDD(DUK_DDDPRINT("FINALFREE (refzero_list): %!iO",
  34090. (duk_heaphdr *) curr));
  34091. next = DUK_HEAPHDR_GET_NEXT(heap, curr);
  34092. duk_heap_free_heaphdr_raw(heap, curr);
  34093. curr = next;
  34094. }
  34095. }
  34096. #endif
  34097. #ifdef DUK_USE_MARK_AND_SWEEP
  34098. DUK_LOCAL void duk__free_markandsweep_finalize_list(duk_heap *heap) {
  34099. duk_heaphdr *curr;
  34100. duk_heaphdr *next;
  34101. curr = heap->finalize_list;
  34102. while (curr) {
  34103. DUK_DDD(DUK_DDDPRINT("FINALFREE (finalize_list): %!iO",
  34104. (duk_heaphdr *) curr));
  34105. next = DUK_HEAPHDR_GET_NEXT(heap, curr);
  34106. duk_heap_free_heaphdr_raw(heap, curr);
  34107. curr = next;
  34108. }
  34109. }
  34110. #endif
  34111. DUK_LOCAL void duk__free_stringtable(duk_heap *heap) {
  34112. /* strings are only tracked by stringtable */
  34113. duk_heap_free_strtab(heap);
  34114. }
  34115. DUK_LOCAL void duk__free_run_finalizers(duk_heap *heap) {
  34116. duk_hthread *thr;
  34117. duk_heaphdr *curr;
  34118. #ifdef DUK_USE_DEBUG
  34119. duk_size_t count_obj = 0;
  34120. #endif
  34121. DUK_ASSERT(heap != NULL);
  34122. DUK_ASSERT(heap->heap_thread != NULL);
  34123. #ifdef DUK_USE_REFERENCE_COUNTING
  34124. DUK_ASSERT(heap->refzero_list == NULL); /* refzero not running -> must be empty */
  34125. #endif
  34126. #ifdef DUK_USE_MARK_AND_SWEEP
  34127. DUK_ASSERT(heap->finalize_list == NULL); /* mark-and-sweep not running -> must be empty */
  34128. #endif
  34129. /* XXX: here again finalizer thread is the heap_thread which needs
  34130. * to be coordinated with finalizer thread fixes.
  34131. */
  34132. thr = heap->heap_thread;
  34133. DUK_ASSERT(thr != NULL);
  34134. curr = heap->heap_allocated;
  34135. while (curr) {
  34136. if (DUK_HEAPHDR_GET_TYPE(curr) == DUK_HTYPE_OBJECT) {
  34137. /* Only objects in heap_allocated may have finalizers. Check that
  34138. * the object itself has a _Finalizer property so that we don't
  34139. * execute finalizers for e.g. Proxy objects.
  34140. */
  34141. DUK_ASSERT(thr != NULL);
  34142. DUK_ASSERT(curr != NULL);
  34143. if (duk_hobject_hasprop_raw(thr, (duk_hobject *) curr, DUK_HTHREAD_STRING_INT_FINALIZER(thr))) {
  34144. duk_hobject_run_finalizer(thr, (duk_hobject *) curr);
  34145. }
  34146. #ifdef DUK_USE_DEBUG
  34147. count_obj++;
  34148. #endif
  34149. }
  34150. curr = DUK_HEAPHDR_GET_NEXT(heap, curr);
  34151. }
  34152. /* Note: count includes all objects, not only those with an actual finalizer. */
  34153. #ifdef DUK_USE_DEBUG
  34154. DUK_D(DUK_DPRINT("checked %ld objects for finalizers before freeing heap", (long) count_obj));
  34155. #endif
  34156. }
  34157. DUK_INTERNAL void duk_heap_free(duk_heap *heap) {
  34158. DUK_D(DUK_DPRINT("free heap: %p", (void *) heap));
  34159. #if defined(DUK_USE_DEBUG)
  34160. duk_heap_dump_strtab(heap);
  34161. #endif
  34162. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  34163. /* Detach a debugger if attached (can be called multiple times)
  34164. * safely.
  34165. */
  34166. duk_debug_do_detach(heap);
  34167. #endif
  34168. /* Execute finalizers before freeing the heap, even for reachable
  34169. * objects, and regardless of whether or not mark-and-sweep is
  34170. * enabled. This gives finalizers the chance to free any native
  34171. * resources like file handles, allocations made outside Duktape,
  34172. * etc.
  34173. *
  34174. * XXX: this perhaps requires an execution time limit.
  34175. */
  34176. DUK_D(DUK_DPRINT("execute finalizers before freeing heap"));
  34177. #ifdef DUK_USE_MARK_AND_SWEEP
  34178. /* run mark-and-sweep a few times just in case (unreachable
  34179. * object finalizers run already here)
  34180. */
  34181. duk_heap_mark_and_sweep(heap, 0);
  34182. duk_heap_mark_and_sweep(heap, 0);
  34183. #endif
  34184. duk__free_run_finalizers(heap);
  34185. /* Note: heap->heap_thread, heap->curr_thread, heap->heap_object,
  34186. * and heap->log_buffer are on the heap allocated list.
  34187. */
  34188. DUK_D(DUK_DPRINT("freeing heap objects of heap: %p", (void *) heap));
  34189. duk__free_allocated(heap);
  34190. #ifdef DUK_USE_REFERENCE_COUNTING
  34191. DUK_D(DUK_DPRINT("freeing refzero list of heap: %p", (void *) heap));
  34192. duk__free_refzero_list(heap);
  34193. #endif
  34194. #ifdef DUK_USE_MARK_AND_SWEEP
  34195. DUK_D(DUK_DPRINT("freeing mark-and-sweep finalize list of heap: %p", (void *) heap));
  34196. duk__free_markandsweep_finalize_list(heap);
  34197. #endif
  34198. DUK_D(DUK_DPRINT("freeing string table of heap: %p", (void *) heap));
  34199. duk__free_stringtable(heap);
  34200. DUK_D(DUK_DPRINT("freeing heap structure: %p", (void *) heap));
  34201. heap->free_func(heap->heap_udata, heap);
  34202. }
  34203. /*
  34204. * Allocate a heap.
  34205. *
  34206. * String table is initialized with built-in strings from genstrings.py.
  34207. */
  34208. /* intern built-in strings from precooked data (genstrings.py) */
  34209. DUK_LOCAL duk_bool_t duk__init_heap_strings(duk_heap *heap) {
  34210. duk_bitdecoder_ctx bd_ctx;
  34211. duk_bitdecoder_ctx *bd = &bd_ctx; /* convenience */
  34212. duk_small_uint_t i, j;
  34213. DUK_MEMZERO(&bd_ctx, sizeof(bd_ctx));
  34214. bd->data = (const duk_uint8_t *) duk_strings_data;
  34215. bd->length = (duk_size_t) DUK_STRDATA_DATA_LENGTH;
  34216. for (i = 0; i < DUK_HEAP_NUM_STRINGS; i++) {
  34217. duk_uint8_t tmp[DUK_STRDATA_MAX_STRLEN];
  34218. duk_hstring *h;
  34219. duk_small_uint_t len;
  34220. duk_small_uint_t mode;
  34221. duk_small_uint_t t;
  34222. len = duk_bd_decode(bd, 5);
  34223. mode = 32; /* 0 = uppercase, 32 = lowercase (= 'a' - 'A') */
  34224. for (j = 0; j < len; j++) {
  34225. t = duk_bd_decode(bd, 5);
  34226. if (t < DUK__BITPACK_LETTER_LIMIT) {
  34227. t = t + DUK_ASC_UC_A + mode;
  34228. } else if (t == DUK__BITPACK_UNDERSCORE) {
  34229. t = DUK_ASC_UNDERSCORE;
  34230. } else if (t == DUK__BITPACK_FF) {
  34231. /* Internal keys are prefixed with 0xFF in the stringtable
  34232. * (which makes them invalid UTF-8 on purpose).
  34233. */
  34234. t = 0xff;
  34235. } else if (t == DUK__BITPACK_SWITCH1) {
  34236. t = duk_bd_decode(bd, 5);
  34237. DUK_ASSERT_DISABLE(t >= 0); /* unsigned */
  34238. DUK_ASSERT(t <= 25);
  34239. t = t + DUK_ASC_UC_A + (mode ^ 32);
  34240. } else if (t == DUK__BITPACK_SWITCH) {
  34241. mode = mode ^ 32;
  34242. t = duk_bd_decode(bd, 5);
  34243. DUK_ASSERT_DISABLE(t >= 0);
  34244. DUK_ASSERT(t <= 25);
  34245. t = t + DUK_ASC_UC_A + mode;
  34246. } else if (t == DUK__BITPACK_SEVENBIT) {
  34247. t = duk_bd_decode(bd, 7);
  34248. }
  34249. tmp[j] = (duk_uint8_t) t;
  34250. }
  34251. /* No need to length check string: it will never exceed even
  34252. * the 16-bit length maximum.
  34253. */
  34254. DUK_ASSERT(len <= 0xffffUL);
  34255. DUK_DDD(DUK_DDDPRINT("intern built-in string %ld", (long) i));
  34256. h = duk_heap_string_intern(heap, tmp, len);
  34257. if (!h) {
  34258. goto error;
  34259. }
  34260. /* Special flags checks. Since these strings are always
  34261. * reachable and a string cannot appear twice in the string
  34262. * table, there's no need to check/set these flags elsewhere.
  34263. * The 'internal' flag is set by string intern code.
  34264. */
  34265. if (i == DUK_STRIDX_EVAL || i == DUK_STRIDX_LC_ARGUMENTS) {
  34266. DUK_HSTRING_SET_EVAL_OR_ARGUMENTS(h);
  34267. }
  34268. if (i >= DUK_STRIDX_START_RESERVED && i < DUK_STRIDX_END_RESERVED) {
  34269. DUK_HSTRING_SET_RESERVED_WORD(h);
  34270. if (i >= DUK_STRIDX_START_STRICT_RESERVED) {
  34271. DUK_HSTRING_SET_STRICT_RESERVED_WORD(h);
  34272. }
  34273. }
  34274. DUK_DDD(DUK_DDDPRINT("interned: %!O", (duk_heaphdr *) h));
  34275. /* XXX: The incref macro takes a thread pointer but doesn't
  34276. * use it right now.
  34277. */
  34278. DUK_HSTRING_INCREF(_never_referenced_, h);
  34279. #if defined(DUK_USE_HEAPPTR16)
  34280. heap->strs16[i] = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) h);
  34281. #else
  34282. heap->strs[i] = h;
  34283. #endif
  34284. }
  34285. return 1;
  34286. error:
  34287. return 0;
  34288. }
  34289. DUK_LOCAL duk_bool_t duk__init_heap_thread(duk_heap *heap) {
  34290. duk_hthread *thr;
  34291. DUK_DD(DUK_DDPRINT("heap init: alloc heap thread"));
  34292. thr = duk_hthread_alloc(heap,
  34293. DUK_HOBJECT_FLAG_EXTENSIBLE |
  34294. DUK_HOBJECT_FLAG_THREAD |
  34295. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_THREAD));
  34296. if (!thr) {
  34297. DUK_D(DUK_DPRINT("failed to alloc heap_thread"));
  34298. return 0;
  34299. }
  34300. thr->state = DUK_HTHREAD_STATE_INACTIVE;
  34301. #if defined(DUK_USE_HEAPPTR16)
  34302. thr->strs16 = heap->strs16;
  34303. #else
  34304. thr->strs = heap->strs;
  34305. #endif
  34306. heap->heap_thread = thr;
  34307. DUK_HTHREAD_INCREF(thr, thr); /* Note: first argument not really used */
  34308. /* 'thr' is now reachable */
  34309. if (!duk_hthread_init_stacks(heap, thr)) {
  34310. return 0;
  34311. }
  34312. /* XXX: this may now fail, and is not handled correctly */
  34313. duk_hthread_create_builtin_objects(thr);
  34314. /* default prototype (Note: 'thr' must be reachable) */
  34315. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) thr, thr->builtins[DUK_BIDX_THREAD_PROTOTYPE]);
  34316. return 1;
  34317. }
  34318. #ifdef DUK_USE_DEBUG
  34319. #define DUK__DUMPSZ(t) do { \
  34320. DUK_D(DUK_DPRINT("" #t "=%ld", (long) sizeof(t))); \
  34321. } while (0)
  34322. /* These is not 100% because format would need to be non-portable "long long".
  34323. * Also print out as doubles to catch cases where the "long" type is not wide
  34324. * enough; the limits will then not be printed accurately but the magnitude
  34325. * will be correct.
  34326. */
  34327. #define DUK__DUMPLM_SIGNED_RAW(t,a,b) do { \
  34328. DUK_D(DUK_DPRINT(t "=[%ld,%ld]=[%lf,%lf]", \
  34329. (long) (a), (long) (b), \
  34330. (double) (a), (double) (b))); \
  34331. } while(0)
  34332. #define DUK__DUMPLM_UNSIGNED_RAW(t,a,b) do { \
  34333. DUK_D(DUK_DPRINT(t "=[%lu,%lu]=[%lf,%lf]", \
  34334. (unsigned long) (a), (unsigned long) (b), \
  34335. (double) (a), (double) (b))); \
  34336. } while(0)
  34337. #define DUK__DUMPLM_SIGNED(t) do { \
  34338. DUK__DUMPLM_SIGNED_RAW("DUK_" #t "_{MIN,MAX}", DUK_##t##_MIN, DUK_##t##_MAX); \
  34339. } while(0)
  34340. #define DUK__DUMPLM_UNSIGNED(t) do { \
  34341. DUK__DUMPLM_UNSIGNED_RAW("DUK_" #t "_{MIN,MAX}", DUK_##t##_MIN, DUK_##t##_MAX); \
  34342. } while(0)
  34343. DUK_LOCAL void duk__dump_type_sizes(void) {
  34344. DUK_D(DUK_DPRINT("sizeof()"));
  34345. /* basic platform types */
  34346. DUK__DUMPSZ(char);
  34347. DUK__DUMPSZ(short);
  34348. DUK__DUMPSZ(int);
  34349. DUK__DUMPSZ(long);
  34350. DUK__DUMPSZ(double);
  34351. DUK__DUMPSZ(void *);
  34352. DUK__DUMPSZ(size_t);
  34353. /* basic types from duk_features.h */
  34354. DUK__DUMPSZ(duk_uint8_t);
  34355. DUK__DUMPSZ(duk_int8_t);
  34356. DUK__DUMPSZ(duk_uint16_t);
  34357. DUK__DUMPSZ(duk_int16_t);
  34358. DUK__DUMPSZ(duk_uint32_t);
  34359. DUK__DUMPSZ(duk_int32_t);
  34360. DUK__DUMPSZ(duk_uint64_t);
  34361. DUK__DUMPSZ(duk_int64_t);
  34362. DUK__DUMPSZ(duk_uint_least8_t);
  34363. DUK__DUMPSZ(duk_int_least8_t);
  34364. DUK__DUMPSZ(duk_uint_least16_t);
  34365. DUK__DUMPSZ(duk_int_least16_t);
  34366. DUK__DUMPSZ(duk_uint_least32_t);
  34367. DUK__DUMPSZ(duk_int_least32_t);
  34368. #if defined(DUK_USE_64BIT_OPS)
  34369. DUK__DUMPSZ(duk_uint_least64_t);
  34370. DUK__DUMPSZ(duk_int_least64_t);
  34371. #endif
  34372. DUK__DUMPSZ(duk_uint_fast8_t);
  34373. DUK__DUMPSZ(duk_int_fast8_t);
  34374. DUK__DUMPSZ(duk_uint_fast16_t);
  34375. DUK__DUMPSZ(duk_int_fast16_t);
  34376. DUK__DUMPSZ(duk_uint_fast32_t);
  34377. DUK__DUMPSZ(duk_int_fast32_t);
  34378. #if defined(DUK_USE_64BIT_OPS)
  34379. DUK__DUMPSZ(duk_uint_fast64_t);
  34380. DUK__DUMPSZ(duk_int_fast64_t);
  34381. #endif
  34382. DUK__DUMPSZ(duk_uintptr_t);
  34383. DUK__DUMPSZ(duk_intptr_t);
  34384. DUK__DUMPSZ(duk_uintmax_t);
  34385. DUK__DUMPSZ(duk_intmax_t);
  34386. DUK__DUMPSZ(duk_double_t);
  34387. /* important chosen base types */
  34388. DUK__DUMPSZ(duk_int_t);
  34389. DUK__DUMPSZ(duk_uint_t);
  34390. DUK__DUMPSZ(duk_int_fast_t);
  34391. DUK__DUMPSZ(duk_uint_fast_t);
  34392. DUK__DUMPSZ(duk_small_int_t);
  34393. DUK__DUMPSZ(duk_small_uint_t);
  34394. DUK__DUMPSZ(duk_small_int_fast_t);
  34395. DUK__DUMPSZ(duk_small_uint_fast_t);
  34396. /* some derived types */
  34397. DUK__DUMPSZ(duk_codepoint_t);
  34398. DUK__DUMPSZ(duk_ucodepoint_t);
  34399. DUK__DUMPSZ(duk_idx_t);
  34400. DUK__DUMPSZ(duk_errcode_t);
  34401. DUK__DUMPSZ(duk_uarridx_t);
  34402. /* tval */
  34403. DUK__DUMPSZ(duk_double_union);
  34404. DUK__DUMPSZ(duk_tval);
  34405. /* structs from duk_forwdecl.h */
  34406. DUK__DUMPSZ(duk_jmpbuf);
  34407. DUK__DUMPSZ(duk_heaphdr);
  34408. DUK__DUMPSZ(duk_heaphdr_string);
  34409. DUK__DUMPSZ(duk_hstring);
  34410. DUK__DUMPSZ(duk_hstring_external);
  34411. DUK__DUMPSZ(duk_hobject);
  34412. DUK__DUMPSZ(duk_hcompiledfunction);
  34413. DUK__DUMPSZ(duk_hnativefunction);
  34414. DUK__DUMPSZ(duk_hthread);
  34415. DUK__DUMPSZ(duk_hbuffer);
  34416. DUK__DUMPSZ(duk_hbuffer_fixed);
  34417. DUK__DUMPSZ(duk_hbuffer_dynamic);
  34418. DUK__DUMPSZ(duk_propaccessor);
  34419. DUK__DUMPSZ(duk_propvalue);
  34420. DUK__DUMPSZ(duk_propdesc);
  34421. DUK__DUMPSZ(duk_heap);
  34422. #if defined(DUK_USE_STRTAB_CHAIN)
  34423. DUK__DUMPSZ(duk_strtab_entry);
  34424. #endif
  34425. DUK__DUMPSZ(duk_activation);
  34426. DUK__DUMPSZ(duk_catcher);
  34427. DUK__DUMPSZ(duk_strcache);
  34428. DUK__DUMPSZ(duk_ljstate);
  34429. DUK__DUMPSZ(duk_fixedbuffer);
  34430. DUK__DUMPSZ(duk_bitdecoder_ctx);
  34431. DUK__DUMPSZ(duk_bitencoder_ctx);
  34432. DUK__DUMPSZ(duk_token);
  34433. DUK__DUMPSZ(duk_re_token);
  34434. DUK__DUMPSZ(duk_lexer_point);
  34435. DUK__DUMPSZ(duk_lexer_ctx);
  34436. DUK__DUMPSZ(duk_compiler_instr);
  34437. DUK__DUMPSZ(duk_compiler_func);
  34438. DUK__DUMPSZ(duk_compiler_ctx);
  34439. DUK__DUMPSZ(duk_re_matcher_ctx);
  34440. DUK__DUMPSZ(duk_re_compiler_ctx);
  34441. }
  34442. DUK_LOCAL void duk__dump_type_limits(void) {
  34443. DUK_D(DUK_DPRINT("limits"));
  34444. /* basic types */
  34445. DUK__DUMPLM_SIGNED(INT8);
  34446. DUK__DUMPLM_UNSIGNED(UINT8);
  34447. DUK__DUMPLM_SIGNED(INT_FAST8);
  34448. DUK__DUMPLM_UNSIGNED(UINT_FAST8);
  34449. DUK__DUMPLM_SIGNED(INT_LEAST8);
  34450. DUK__DUMPLM_UNSIGNED(UINT_LEAST8);
  34451. DUK__DUMPLM_SIGNED(INT16);
  34452. DUK__DUMPLM_UNSIGNED(UINT16);
  34453. DUK__DUMPLM_SIGNED(INT_FAST16);
  34454. DUK__DUMPLM_UNSIGNED(UINT_FAST16);
  34455. DUK__DUMPLM_SIGNED(INT_LEAST16);
  34456. DUK__DUMPLM_UNSIGNED(UINT_LEAST16);
  34457. DUK__DUMPLM_SIGNED(INT32);
  34458. DUK__DUMPLM_UNSIGNED(UINT32);
  34459. DUK__DUMPLM_SIGNED(INT_FAST32);
  34460. DUK__DUMPLM_UNSIGNED(UINT_FAST32);
  34461. DUK__DUMPLM_SIGNED(INT_LEAST32);
  34462. DUK__DUMPLM_UNSIGNED(UINT_LEAST32);
  34463. #if defined(DUK_USE_64BIT_OPS)
  34464. DUK__DUMPLM_SIGNED(INT64);
  34465. DUK__DUMPLM_UNSIGNED(UINT64);
  34466. DUK__DUMPLM_SIGNED(INT_FAST64);
  34467. DUK__DUMPLM_UNSIGNED(UINT_FAST64);
  34468. DUK__DUMPLM_SIGNED(INT_LEAST64);
  34469. DUK__DUMPLM_UNSIGNED(UINT_LEAST64);
  34470. #endif
  34471. DUK__DUMPLM_SIGNED(INTPTR);
  34472. DUK__DUMPLM_UNSIGNED(UINTPTR);
  34473. DUK__DUMPLM_SIGNED(INTMAX);
  34474. DUK__DUMPLM_UNSIGNED(UINTMAX);
  34475. /* derived types */
  34476. DUK__DUMPLM_SIGNED(INT);
  34477. DUK__DUMPLM_UNSIGNED(UINT);
  34478. DUK__DUMPLM_SIGNED(INT_FAST);
  34479. DUK__DUMPLM_UNSIGNED(UINT_FAST);
  34480. DUK__DUMPLM_SIGNED(SMALL_INT);
  34481. DUK__DUMPLM_UNSIGNED(SMALL_UINT);
  34482. DUK__DUMPLM_SIGNED(SMALL_INT_FAST);
  34483. DUK__DUMPLM_UNSIGNED(SMALL_UINT_FAST);
  34484. }
  34485. #undef DUK__DUMPSZ
  34486. #undef DUK__DUMPLM_SIGNED_RAW
  34487. #undef DUK__DUMPLM_UNSIGNED_RAW
  34488. #undef DUK__DUMPLM_SIGNED
  34489. #undef DUK__DUMPLM_UNSIGNED
  34490. DUK_LOCAL void duk__dump_misc_options(void) {
  34491. DUK_D(DUK_DPRINT("DUK_VERSION: %ld", (long) DUK_VERSION));
  34492. DUK_D(DUK_DPRINT("DUK_GIT_DESCRIBE: %s", DUK_GIT_DESCRIBE));
  34493. #if defined(DUK_USE_PACKED_TVAL)
  34494. DUK_D(DUK_DPRINT("DUK_USE_PACKED_TVAL: yes"));
  34495. #else
  34496. DUK_D(DUK_DPRINT("DUK_USE_PACKED_TVAL: no"));
  34497. #endif
  34498. #if defined(DUK_USE_INTEGER_LE)
  34499. DUK_D(DUK_DPRINT("Integer endianness: little"));
  34500. #elif defined(DUK_USE_INTEGER_ME)
  34501. DUK_D(DUK_DPRINT("Integer endianness: mixed"));
  34502. #elif defined(DUK_USE_INTEGER_BE)
  34503. DUK_D(DUK_DPRINT("Integer endianness: big"));
  34504. #else
  34505. DUK_D(DUK_DPRINT("Integer endianness: ???"));
  34506. #endif
  34507. #if defined(DUK_USE_DOUBLE_LE)
  34508. DUK_D(DUK_DPRINT("IEEE double endianness: little"));
  34509. #elif defined(DUK_USE_DOUBLE_ME)
  34510. DUK_D(DUK_DPRINT("IEEE double endianness: mixed"));
  34511. #elif defined(DUK_USE_DOUBLE_BE)
  34512. DUK_D(DUK_DPRINT("IEEE double endianness: big"));
  34513. #else
  34514. DUK_D(DUK_DPRINT("IEEE double endianness: ???"));
  34515. #endif
  34516. }
  34517. #endif /* DUK_USE_DEBUG */
  34518. DUK_INTERNAL
  34519. duk_heap *duk_heap_alloc(duk_alloc_function alloc_func,
  34520. duk_realloc_function realloc_func,
  34521. duk_free_function free_func,
  34522. void *heap_udata,
  34523. duk_fatal_function fatal_func) {
  34524. duk_heap *res = NULL;
  34525. DUK_D(DUK_DPRINT("allocate heap"));
  34526. /*
  34527. * Debug dump type sizes
  34528. */
  34529. #ifdef DUK_USE_DEBUG
  34530. duk__dump_misc_options();
  34531. duk__dump_type_sizes();
  34532. duk__dump_type_limits();
  34533. #endif
  34534. /*
  34535. * If selftests enabled, run them as early as possible
  34536. */
  34537. #ifdef DUK_USE_SELF_TESTS
  34538. DUK_D(DUK_DPRINT("running self tests"));
  34539. duk_selftest_run_tests();
  34540. DUK_D(DUK_DPRINT("self tests passed"));
  34541. #endif
  34542. #ifdef DUK_USE_COMPUTED_NAN
  34543. do {
  34544. /* Workaround for some exotic platforms where NAN is missing
  34545. * and the expression (0.0 / 0.0) does NOT result in a NaN.
  34546. * Such platforms use the global 'duk_computed_nan' which must
  34547. * be initialized at runtime. Use 'volatile' to ensure that
  34548. * the compiler will actually do the computation and not try
  34549. * to do constant folding which might result in the original
  34550. * problem.
  34551. */
  34552. volatile double dbl1 = 0.0;
  34553. volatile double dbl2 = 0.0;
  34554. duk_computed_nan = dbl1 / dbl2;
  34555. } while (0);
  34556. #endif
  34557. /*
  34558. * Computed values (e.g. INFINITY)
  34559. */
  34560. #ifdef DUK_USE_COMPUTED_INFINITY
  34561. do {
  34562. /* Similar workaround for INFINITY. */
  34563. volatile double dbl1 = 1.0;
  34564. volatile double dbl2 = 0.0;
  34565. duk_computed_infinity = dbl1 / dbl2;
  34566. } while (0);
  34567. #endif
  34568. /*
  34569. * Allocate heap struct
  34570. *
  34571. * Use a raw call, all macros expect the heap to be initialized
  34572. */
  34573. res = (duk_heap *) alloc_func(heap_udata, sizeof(duk_heap));
  34574. if (!res) {
  34575. goto error;
  34576. }
  34577. /*
  34578. * Zero the struct, and start initializing roughly in order
  34579. */
  34580. DUK_MEMZERO(res, sizeof(*res));
  34581. /* explicit NULL inits */
  34582. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  34583. res->heap_udata = NULL;
  34584. res->heap_allocated = NULL;
  34585. #ifdef DUK_USE_REFERENCE_COUNTING
  34586. res->refzero_list = NULL;
  34587. res->refzero_list_tail = NULL;
  34588. #endif
  34589. #ifdef DUK_USE_MARK_AND_SWEEP
  34590. res->finalize_list = NULL;
  34591. #endif
  34592. res->heap_thread = NULL;
  34593. res->curr_thread = NULL;
  34594. res->heap_object = NULL;
  34595. res->log_buffer = NULL;
  34596. #if defined(DUK_USE_STRTAB_CHAIN)
  34597. /* nothing to NULL */
  34598. #elif defined(DUK_USE_STRTAB_PROBE)
  34599. #if defined(DUK_USE_HEAPPTR16)
  34600. res->strtable16 = (duk_uint16_t *) NULL;
  34601. #else
  34602. res->strtable = (duk_hstring **) NULL;
  34603. #endif
  34604. #endif
  34605. {
  34606. duk_small_uint_t i;
  34607. for (i = 0; i < DUK_HEAP_NUM_STRINGS; i++) {
  34608. res->strs[i] = NULL;
  34609. }
  34610. }
  34611. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  34612. res->dbg_read_cb = NULL;
  34613. res->dbg_write_cb = NULL;
  34614. res->dbg_peek_cb = NULL;
  34615. res->dbg_read_flush_cb = NULL;
  34616. res->dbg_write_flush_cb = NULL;
  34617. res->dbg_udata = NULL;
  34618. res->dbg_step_thread = NULL;
  34619. #endif
  34620. #endif /* DUK_USE_EXPLICIT_NULL_INIT */
  34621. res->alloc_func = alloc_func;
  34622. res->realloc_func = realloc_func;
  34623. res->free_func = free_func;
  34624. res->heap_udata = heap_udata;
  34625. res->fatal_func = fatal_func;
  34626. #if defined(DUK_USE_HEAPPTR16)
  34627. /* XXX: zero assumption */
  34628. res->heapptr_null16 = DUK_USE_HEAPPTR_ENC16(res->heap_udata, (void *) NULL);
  34629. res->heapptr_deleted16 = DUK_USE_HEAPPTR_ENC16(res->heap_udata, (void *) DUK_STRTAB_DELETED_MARKER(res));
  34630. #endif
  34631. /* res->mark_and_sweep_trigger_counter == 0 -> now causes immediate GC; which is OK */
  34632. res->call_recursion_depth = 0;
  34633. res->call_recursion_limit = DUK_HEAP_DEFAULT_CALL_RECURSION_LIMIT;
  34634. /* XXX: use the pointer as a seed for now: mix in time at least */
  34635. /* The casts through duk_intr_pt is to avoid the following GCC warning:
  34636. *
  34637. * warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  34638. *
  34639. * This still generates a /Wp64 warning on VS2010 when compiling for x86.
  34640. */
  34641. res->hash_seed = (duk_uint32_t) (duk_intptr_t) res;
  34642. res->rnd_state = (duk_uint32_t) (duk_intptr_t) res;
  34643. #ifdef DUK_USE_INTERRUPT_COUNTER
  34644. /* zero value causes an interrupt before executing first instruction */
  34645. DUK_ASSERT(res->interrupt_counter == 0);
  34646. DUK_ASSERT(res->interrupt_init == 0);
  34647. #endif
  34648. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  34649. res->lj.jmpbuf_ptr = NULL;
  34650. #endif
  34651. DUK_ASSERT(res->lj.type == DUK_LJ_TYPE_UNKNOWN); /* zero */
  34652. DUK_TVAL_SET_UNDEFINED_UNUSED(&res->lj.value1);
  34653. DUK_TVAL_SET_UNDEFINED_UNUSED(&res->lj.value2);
  34654. #if (DUK_STRTAB_INITIAL_SIZE < DUK_UTIL_MIN_HASH_PRIME)
  34655. #error initial heap stringtable size is defined incorrectly
  34656. #endif
  34657. /*
  34658. * Init stringtable: fixed variant
  34659. */
  34660. #if defined(DUK_USE_STRTAB_CHAIN)
  34661. DUK_MEMZERO(res->strtable, sizeof(duk_strtab_entry) * DUK_STRTAB_CHAIN_SIZE);
  34662. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  34663. {
  34664. duk_small_uint_t i;
  34665. for (i = 0; i < DUK_STRTAB_CHAIN_SIZE; i++) {
  34666. #if defined(DUK_USE_HEAPPTR16)
  34667. res->strtable[i].u.str16 = res->heapptr_null16;
  34668. #else
  34669. res->strtable[i].u.str = NULL;
  34670. #endif
  34671. }
  34672. }
  34673. #endif /* DUK_USE_EXPLICIT_NULL_INIT */
  34674. #endif /* DUK_USE_STRTAB_CHAIN */
  34675. /*
  34676. * Init stringtable: probe variant
  34677. */
  34678. #if defined(DUK_USE_STRTAB_PROBE)
  34679. #if defined(DUK_USE_HEAPPTR16)
  34680. res->strtable16 = (duk_uint16_t *) alloc_func(heap_udata, sizeof(duk_uint16_t) * DUK_STRTAB_INITIAL_SIZE);
  34681. if (!res->strtable16) {
  34682. goto error;
  34683. }
  34684. #else /* DUK_USE_HEAPPTR16 */
  34685. res->strtable = (duk_hstring **) alloc_func(heap_udata, sizeof(duk_hstring *) * DUK_STRTAB_INITIAL_SIZE);
  34686. if (!res->strtable) {
  34687. goto error;
  34688. }
  34689. #endif /* DUK_USE_HEAPPTR16 */
  34690. res->st_size = DUK_STRTAB_INITIAL_SIZE;
  34691. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  34692. {
  34693. duk_small_uint_t i;
  34694. DUK_ASSERT(res->st_size == DUK_STRTAB_INITIAL_SIZE);
  34695. for (i = 0; i < DUK_STRTAB_INITIAL_SIZE; i++) {
  34696. #if defined(DUK_USE_HEAPPTR16)
  34697. res->strtable16[i] = res->heapptr_null16;
  34698. #else
  34699. res->strtable[i] = NULL;
  34700. #endif
  34701. }
  34702. }
  34703. #else /* DUK_USE_EXPLICIT_NULL_INIT */
  34704. #if defined(DUK_USE_HEAPPTR16)
  34705. DUK_MEMZERO(res->strtable16, sizeof(duk_uint16_t) * DUK_STRTAB_INITIAL_SIZE);
  34706. #else
  34707. DUK_MEMZERO(res->strtable, sizeof(duk_hstring *) * DUK_STRTAB_INITIAL_SIZE);
  34708. #endif
  34709. #endif /* DUK_USE_EXPLICIT_NULL_INIT */
  34710. #endif /* DUK_USE_STRTAB_PROBE */
  34711. /*
  34712. * Init stringcache
  34713. */
  34714. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  34715. {
  34716. duk_small_uint_t i;
  34717. for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
  34718. res->strcache[i].h = NULL;
  34719. }
  34720. }
  34721. #endif
  34722. /* XXX: error handling is incomplete. It would be cleanest if
  34723. * there was a setjmp catchpoint, so that all init code could
  34724. * freely throw errors. If that were the case, the return code
  34725. * passing here could be removed.
  34726. */
  34727. /*
  34728. * Init built-in strings
  34729. */
  34730. DUK_DD(DUK_DDPRINT("HEAP: INIT STRINGS"));
  34731. if (!duk__init_heap_strings(res)) {
  34732. goto error;
  34733. }
  34734. /*
  34735. * Init the heap thread
  34736. */
  34737. DUK_DD(DUK_DDPRINT("HEAP: INIT HEAP THREAD"));
  34738. if (!duk__init_heap_thread(res)) {
  34739. goto error;
  34740. }
  34741. /*
  34742. * Init the heap object
  34743. */
  34744. DUK_DD(DUK_DDPRINT("HEAP: INIT HEAP OBJECT"));
  34745. DUK_ASSERT(res->heap_thread != NULL);
  34746. res->heap_object = duk_hobject_alloc(res, DUK_HOBJECT_FLAG_EXTENSIBLE |
  34747. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT));
  34748. if (!res->heap_object) {
  34749. goto error;
  34750. }
  34751. DUK_HOBJECT_INCREF(res->heap_thread, res->heap_object);
  34752. /*
  34753. * Init log buffer
  34754. */
  34755. DUK_DD(DUK_DDPRINT("HEAP: INIT LOG BUFFER"));
  34756. res->log_buffer = (duk_hbuffer_dynamic *) duk_hbuffer_alloc(res,
  34757. DUK_BI_LOGGER_SHORT_MSG_LIMIT,
  34758. DUK_BUF_FLAG_DYNAMIC /*flags*/);
  34759. if (!res->log_buffer) {
  34760. goto error;
  34761. }
  34762. DUK_HBUFFER_INCREF(res->heap_thread, res->log_buffer);
  34763. /*
  34764. * All done
  34765. */
  34766. DUK_D(DUK_DPRINT("allocated heap: %p", (void *) res));
  34767. return res;
  34768. error:
  34769. DUK_D(DUK_DPRINT("heap allocation failed"));
  34770. if (res) {
  34771. /* assumes that allocated pointers and alloc funcs are valid
  34772. * if res exists
  34773. */
  34774. DUK_ASSERT(res->alloc_func != NULL);
  34775. DUK_ASSERT(res->realloc_func != NULL);
  34776. DUK_ASSERT(res->free_func != NULL);
  34777. duk_heap_free(res);
  34778. }
  34779. return NULL;
  34780. }
  34781. #line 1 "duk_heap_hashstring.c"
  34782. /*
  34783. * String hash computation (interning).
  34784. */
  34785. /* include removed: duk_internal.h */
  34786. /* constants for duk_hashstring() */
  34787. #define DUK__STRHASH_SHORTSTRING 4096L
  34788. #define DUK__STRHASH_MEDIUMSTRING (256L * 1024L)
  34789. #define DUK__STRHASH_BLOCKSIZE 256L
  34790. DUK_INTERNAL duk_uint32_t duk_heap_hashstring(duk_heap *heap, const duk_uint8_t *str, duk_size_t len) {
  34791. duk_uint32_t hash;
  34792. /*
  34793. * Sampling long strings by byte skipping (like Lua does) is potentially
  34794. * a cache problem. Here we do 'block skipping' instead for long strings:
  34795. * hash an initial part, and then sample the rest of the string with
  34796. * reasonably sized chunks.
  34797. *
  34798. * Skip should depend on length and bound the total time to roughly
  34799. * logarithmic.
  34800. *
  34801. * With current values:
  34802. *
  34803. * 1M string => 256 * 241 = 61696 bytes (0.06M) of hashing
  34804. * 1G string => 256 * 16321 = 4178176 bytes (3.98M) of hashing
  34805. *
  34806. * After an initial part has been hashed, an offset is applied before
  34807. * starting the sampling. The initial offset is computed from the
  34808. * hash of the initial part of the string. The idea is to avoid the
  34809. * case that all long strings have certain offset ranges that are never
  34810. * sampled.
  34811. */
  34812. /* note: mixing len into seed improves hashing when skipping */
  34813. duk_uint32_t str_seed = heap->hash_seed ^ ((duk_uint32_t) len);
  34814. if (len <= DUK__STRHASH_SHORTSTRING) {
  34815. hash = duk_util_hashbytes(str, len, str_seed);
  34816. } else {
  34817. duk_size_t off;
  34818. duk_size_t skip;
  34819. if (len <= DUK__STRHASH_MEDIUMSTRING) {
  34820. skip = (duk_size_t) (16 * DUK__STRHASH_BLOCKSIZE + DUK__STRHASH_BLOCKSIZE);
  34821. } else {
  34822. skip = (duk_size_t) (256 * DUK__STRHASH_BLOCKSIZE + DUK__STRHASH_BLOCKSIZE);
  34823. }
  34824. hash = duk_util_hashbytes(str, (duk_size_t) DUK__STRHASH_SHORTSTRING, str_seed);
  34825. off = DUK__STRHASH_SHORTSTRING + (skip * (hash % 256)) / 256;
  34826. /* XXX: inefficient loop */
  34827. while (off < len) {
  34828. duk_size_t left = len - off;
  34829. duk_size_t now = (duk_size_t) (left > DUK__STRHASH_BLOCKSIZE ? DUK__STRHASH_BLOCKSIZE : left);
  34830. hash ^= duk_util_hashbytes(str + off, now, str_seed);
  34831. off += skip;
  34832. }
  34833. }
  34834. #if defined(DUK_USE_STRHASH16)
  34835. /* Truncate to 16 bits here, so that a computed hash can be compared
  34836. * against a hash stored in a 16-bit field.
  34837. */
  34838. hash &= 0x0000ffffUL;
  34839. #endif
  34840. return hash;
  34841. }
  34842. #line 1 "duk_heap_markandsweep.c"
  34843. /*
  34844. * Mark-and-sweep garbage collection.
  34845. */
  34846. /* include removed: duk_internal.h */
  34847. #ifdef DUK_USE_MARK_AND_SWEEP
  34848. DUK_LOCAL_DECL void duk__mark_heaphdr(duk_heap *heap, duk_heaphdr *h);
  34849. DUK_LOCAL_DECL void duk__mark_tval(duk_heap *heap, duk_tval *tv);
  34850. /*
  34851. * Misc
  34852. */
  34853. /* Select a thread for mark-and-sweep use.
  34854. *
  34855. * XXX: This needs to change later.
  34856. */
  34857. DUK_LOCAL duk_hthread *duk__get_temp_hthread(duk_heap *heap) {
  34858. if (heap->curr_thread) {
  34859. return heap->curr_thread;
  34860. }
  34861. return heap->heap_thread; /* may be NULL, too */
  34862. }
  34863. /*
  34864. * Marking functions for heap types: mark children recursively
  34865. */
  34866. DUK_LOCAL void duk__mark_hstring(duk_heap *heap, duk_hstring *h) {
  34867. DUK_UNREF(heap);
  34868. DUK_UNREF(h);
  34869. DUK_DDD(DUK_DDDPRINT("duk__mark_hstring: %p", (void *) h));
  34870. DUK_ASSERT(h);
  34871. /* nothing to process */
  34872. }
  34873. DUK_LOCAL void duk__mark_hobject(duk_heap *heap, duk_hobject *h) {
  34874. duk_uint_fast32_t i;
  34875. DUK_DDD(DUK_DDDPRINT("duk__mark_hobject: %p", (void *) h));
  34876. DUK_ASSERT(h);
  34877. /* XXX: use advancing pointers instead of index macros -> faster and smaller? */
  34878. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(h); i++) {
  34879. duk_hstring *key = DUK_HOBJECT_E_GET_KEY(heap, h, i);
  34880. if (!key) {
  34881. continue;
  34882. }
  34883. duk__mark_heaphdr(heap, (duk_heaphdr *) key);
  34884. if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap, h, i)) {
  34885. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->a.get);
  34886. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->a.set);
  34887. } else {
  34888. duk__mark_tval(heap, &DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->v);
  34889. }
  34890. }
  34891. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(h); i++) {
  34892. duk__mark_tval(heap, DUK_HOBJECT_A_GET_VALUE_PTR(heap, h, i));
  34893. }
  34894. /* hash part is a 'weak reference' and does not contribute */
  34895. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_HOBJECT_GET_PROTOTYPE(heap, h));
  34896. if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h)) {
  34897. duk_hcompiledfunction *f = (duk_hcompiledfunction *) h;
  34898. duk_tval *tv, *tv_end;
  34899. duk_hobject **funcs, **funcs_end;
  34900. /* 'data' is reachable through every compiled function which
  34901. * contains a reference.
  34902. */
  34903. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_HCOMPILEDFUNCTION_GET_DATA(heap, f));
  34904. tv = DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(heap, f);
  34905. tv_end = DUK_HCOMPILEDFUNCTION_GET_CONSTS_END(heap, f);
  34906. while (tv < tv_end) {
  34907. duk__mark_tval(heap, tv);
  34908. tv++;
  34909. }
  34910. funcs = DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(heap, f);
  34911. funcs_end = DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(heap, f);
  34912. while (funcs < funcs_end) {
  34913. duk__mark_heaphdr(heap, (duk_heaphdr *) *funcs);
  34914. funcs++;
  34915. }
  34916. } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
  34917. duk_hnativefunction *f = (duk_hnativefunction *) h;
  34918. DUK_UNREF(f);
  34919. /* nothing to mark */
  34920. } else if (DUK_HOBJECT_IS_BUFFEROBJECT(h)) {
  34921. duk_hbufferobject *b = (duk_hbufferobject *) h;
  34922. duk__mark_heaphdr(heap, (duk_heaphdr *) b->buf);
  34923. } else if (DUK_HOBJECT_IS_THREAD(h)) {
  34924. duk_hthread *t = (duk_hthread *) h;
  34925. duk_tval *tv;
  34926. tv = t->valstack;
  34927. while (tv < t->valstack_end) {
  34928. duk__mark_tval(heap, tv);
  34929. tv++;
  34930. }
  34931. for (i = 0; i < (duk_uint_fast32_t) t->callstack_top; i++) {
  34932. duk_activation *act = t->callstack + i;
  34933. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_ACT_GET_FUNC(act));
  34934. duk__mark_heaphdr(heap, (duk_heaphdr *) act->var_env);
  34935. duk__mark_heaphdr(heap, (duk_heaphdr *) act->lex_env);
  34936. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  34937. duk__mark_heaphdr(heap, (duk_heaphdr *) act->prev_caller);
  34938. #endif
  34939. }
  34940. #if 0 /* nothing now */
  34941. for (i = 0; i < (duk_uint_fast32_t) t->catchstack_top; i++) {
  34942. duk_catcher *cat = t->catchstack + i;
  34943. }
  34944. #endif
  34945. duk__mark_heaphdr(heap, (duk_heaphdr *) t->resumer);
  34946. /* XXX: duk_small_uint_t would be enough for this loop */
  34947. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  34948. duk__mark_heaphdr(heap, (duk_heaphdr *) t->builtins[i]);
  34949. }
  34950. }
  34951. }
  34952. /* recursion tracking happens here only */
  34953. DUK_LOCAL void duk__mark_heaphdr(duk_heap *heap, duk_heaphdr *h) {
  34954. DUK_DDD(DUK_DDDPRINT("duk__mark_heaphdr %p, type %ld",
  34955. (void *) h,
  34956. (h != NULL ? (long) DUK_HEAPHDR_GET_TYPE(h) : (long) -1)));
  34957. if (!h) {
  34958. return;
  34959. }
  34960. if (DUK_HEAPHDR_HAS_REACHABLE(h)) {
  34961. DUK_DDD(DUK_DDDPRINT("already marked reachable, skip"));
  34962. return;
  34963. }
  34964. DUK_HEAPHDR_SET_REACHABLE(h);
  34965. if (heap->mark_and_sweep_recursion_depth >= DUK_HEAP_MARK_AND_SWEEP_RECURSION_LIMIT) {
  34966. /* log this with a normal debug level because this should be relatively rare */
  34967. DUK_D(DUK_DPRINT("mark-and-sweep recursion limit reached, marking as temproot: %p", (void *) h));
  34968. DUK_HEAP_SET_MARKANDSWEEP_RECLIMIT_REACHED(heap);
  34969. DUK_HEAPHDR_SET_TEMPROOT(h);
  34970. return;
  34971. }
  34972. heap->mark_and_sweep_recursion_depth++;
  34973. switch ((int) DUK_HEAPHDR_GET_TYPE(h)) {
  34974. case DUK_HTYPE_STRING:
  34975. duk__mark_hstring(heap, (duk_hstring *) h);
  34976. break;
  34977. case DUK_HTYPE_OBJECT:
  34978. duk__mark_hobject(heap, (duk_hobject *) h);
  34979. break;
  34980. case DUK_HTYPE_BUFFER:
  34981. /* nothing to mark */
  34982. break;
  34983. default:
  34984. DUK_D(DUK_DPRINT("attempt to mark heaphdr %p with invalid htype %ld", (void *) h, (long) DUK_HEAPHDR_GET_TYPE(h)));
  34985. DUK_UNREACHABLE();
  34986. }
  34987. heap->mark_and_sweep_recursion_depth--;
  34988. }
  34989. DUK_LOCAL void duk__mark_tval(duk_heap *heap, duk_tval *tv) {
  34990. DUK_DDD(DUK_DDDPRINT("duk__mark_tval %p", (void *) tv));
  34991. if (!tv) {
  34992. return;
  34993. }
  34994. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  34995. duk__mark_heaphdr(heap, DUK_TVAL_GET_HEAPHDR(tv));
  34996. }
  34997. }
  34998. /*
  34999. * Mark the heap.
  35000. */
  35001. DUK_LOCAL void duk__mark_roots_heap(duk_heap *heap) {
  35002. duk_small_uint_t i;
  35003. DUK_DD(DUK_DDPRINT("duk__mark_roots_heap: %p", (void *) heap));
  35004. duk__mark_heaphdr(heap, (duk_heaphdr *) heap->heap_thread);
  35005. duk__mark_heaphdr(heap, (duk_heaphdr *) heap->heap_object);
  35006. duk__mark_heaphdr(heap, (duk_heaphdr *) heap->log_buffer);
  35007. for (i = 0; i < DUK_HEAP_NUM_STRINGS; i++) {
  35008. duk_hstring *h = DUK_HEAP_GET_STRING(heap, i);
  35009. duk__mark_heaphdr(heap, (duk_heaphdr *) h);
  35010. }
  35011. duk__mark_tval(heap, &heap->lj.value1);
  35012. duk__mark_tval(heap, &heap->lj.value2);
  35013. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  35014. for (i = 0; i < heap->dbg_breakpoint_count; i++) {
  35015. duk__mark_heaphdr(heap, (duk_heaphdr *) heap->dbg_breakpoints[i].filename);
  35016. }
  35017. #endif
  35018. }
  35019. /*
  35020. * Mark refzero_list objects.
  35021. *
  35022. * Objects on the refzero_list have no inbound references. They might have
  35023. * outbound references to objects that we might free, which would invalidate
  35024. * any references held by the refzero objects. A refzero object might also
  35025. * be rescued by refcount finalization. Refzero objects are treated as
  35026. * reachability roots to ensure they (or anything they point to) are not
  35027. * freed in mark-and-sweep.
  35028. */
  35029. #ifdef DUK_USE_REFERENCE_COUNTING
  35030. DUK_LOCAL void duk__mark_refzero_list(duk_heap *heap) {
  35031. duk_heaphdr *hdr;
  35032. DUK_DD(DUK_DDPRINT("duk__mark_refzero_list: %p", (void *) heap));
  35033. hdr = heap->refzero_list;
  35034. while (hdr) {
  35035. duk__mark_heaphdr(heap, hdr);
  35036. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  35037. }
  35038. }
  35039. #endif
  35040. /*
  35041. * Mark unreachable, finalizable objects.
  35042. *
  35043. * Such objects will be moved aside and their finalizers run later. They have
  35044. * to be treated as reachability roots for their properties etc to remain
  35045. * allocated. This marking is only done for unreachable values which would
  35046. * be swept later (refzero_list is thus excluded).
  35047. *
  35048. * Objects are first marked FINALIZABLE and only then marked as reachability
  35049. * roots; otherwise circular references might be handled inconsistently.
  35050. */
  35051. DUK_LOCAL void duk__mark_finalizable(duk_heap *heap) {
  35052. duk_hthread *thr;
  35053. duk_heaphdr *hdr;
  35054. duk_size_t count_finalizable = 0;
  35055. DUK_DD(DUK_DDPRINT("duk__mark_finalizable: %p", (void *) heap));
  35056. thr = duk__get_temp_hthread(heap);
  35057. DUK_ASSERT(thr != NULL);
  35058. hdr = heap->heap_allocated;
  35059. while (hdr) {
  35060. /* A finalizer is looked up from the object and up its prototype chain
  35061. * (which allows inherited finalizers). A prototype loop must not cause
  35062. * an error to be thrown here; duk_hobject_hasprop_raw() will ignore a
  35063. * prototype loop silently and indicate that the property doesn't exist.
  35064. */
  35065. if (!DUK_HEAPHDR_HAS_REACHABLE(hdr) &&
  35066. DUK_HEAPHDR_GET_TYPE(hdr) == DUK_HTYPE_OBJECT &&
  35067. !DUK_HEAPHDR_HAS_FINALIZED(hdr) &&
  35068. duk_hobject_hasprop_raw(thr, (duk_hobject *) hdr, DUK_HTHREAD_STRING_INT_FINALIZER(thr))) {
  35069. /* heaphdr:
  35070. * - is not reachable
  35071. * - is an object
  35072. * - is not a finalized object
  35073. * - has a finalizer
  35074. */
  35075. DUK_DD(DUK_DDPRINT("unreachable heap object will be "
  35076. "finalized -> mark as finalizable "
  35077. "and treat as a reachability root: %p",
  35078. (void *) hdr));
  35079. DUK_HEAPHDR_SET_FINALIZABLE(hdr);
  35080. count_finalizable ++;
  35081. }
  35082. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  35083. }
  35084. if (count_finalizable == 0) {
  35085. return;
  35086. }
  35087. DUK_DD(DUK_DDPRINT("marked %ld heap objects as finalizable, now mark them reachable",
  35088. (long) count_finalizable));
  35089. hdr = heap->heap_allocated;
  35090. while (hdr) {
  35091. if (DUK_HEAPHDR_HAS_FINALIZABLE(hdr)) {
  35092. duk__mark_heaphdr(heap, hdr);
  35093. }
  35094. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  35095. }
  35096. /* Caller will finish the marking process if we hit a recursion limit. */
  35097. }
  35098. /*
  35099. * Mark objects on finalize_list.
  35100. *
  35101. */
  35102. DUK_LOCAL void duk__mark_finalize_list(duk_heap *heap) {
  35103. duk_heaphdr *hdr;
  35104. #ifdef DUK_USE_DEBUG
  35105. duk_size_t count_finalize_list = 0;
  35106. #endif
  35107. DUK_DD(DUK_DDPRINT("duk__mark_finalize_list: %p", (void *) heap));
  35108. hdr = heap->finalize_list;
  35109. while (hdr) {
  35110. duk__mark_heaphdr(heap, hdr);
  35111. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  35112. #ifdef DUK_USE_DEBUG
  35113. count_finalize_list++;
  35114. #endif
  35115. }
  35116. #ifdef DUK_USE_DEBUG
  35117. if (count_finalize_list > 0) {
  35118. DUK_D(DUK_DPRINT("marked %ld objects on the finalize_list as reachable (previous finalizer run skipped)",
  35119. (long) count_finalize_list));
  35120. }
  35121. #endif
  35122. }
  35123. /*
  35124. * Fallback marking handler if recursion limit is reached.
  35125. *
  35126. * Iterates 'temproots' until recursion limit is no longer hit. Note
  35127. * that temproots may reside either in heap allocated list or the
  35128. * refzero work list. This is a slow scan, but guarantees that we
  35129. * finish with a bounded C stack.
  35130. *
  35131. * Note that nodes may have been marked as temproots before this
  35132. * scan begun, OR they may have been marked during the scan (as
  35133. * we process nodes recursively also during the scan). This is
  35134. * intended behavior.
  35135. */
  35136. #ifdef DUK_USE_DEBUG
  35137. DUK_LOCAL void duk__handle_temproot(duk_heap *heap, duk_heaphdr *hdr, duk_size_t *count) {
  35138. #else
  35139. DUK_LOCAL void duk__handle_temproot(duk_heap *heap, duk_heaphdr *hdr) {
  35140. #endif
  35141. if (!DUK_HEAPHDR_HAS_TEMPROOT(hdr)) {
  35142. DUK_DDD(DUK_DDDPRINT("not a temp root: %p", (void *) hdr));
  35143. return;
  35144. }
  35145. DUK_DDD(DUK_DDDPRINT("found a temp root: %p", (void *) hdr));
  35146. DUK_HEAPHDR_CLEAR_TEMPROOT(hdr);
  35147. DUK_HEAPHDR_CLEAR_REACHABLE(hdr); /* done so that duk__mark_heaphdr() works correctly */
  35148. duk__mark_heaphdr(heap, hdr);
  35149. #ifdef DUK_USE_DEBUG
  35150. (*count)++;
  35151. #endif
  35152. }
  35153. DUK_LOCAL void duk__mark_temproots_by_heap_scan(duk_heap *heap) {
  35154. duk_heaphdr *hdr;
  35155. #ifdef DUK_USE_DEBUG
  35156. duk_size_t count;
  35157. #endif
  35158. DUK_DD(DUK_DDPRINT("duk__mark_temproots_by_heap_scan: %p", (void *) heap));
  35159. while (DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED(heap)) {
  35160. DUK_DD(DUK_DDPRINT("recursion limit reached, doing heap scan to continue from temproots"));
  35161. #ifdef DUK_USE_DEBUG
  35162. count = 0;
  35163. #endif
  35164. DUK_HEAP_CLEAR_MARKANDSWEEP_RECLIMIT_REACHED(heap);
  35165. hdr = heap->heap_allocated;
  35166. while (hdr) {
  35167. #ifdef DUK_USE_DEBUG
  35168. duk__handle_temproot(heap, hdr, &count);
  35169. #else
  35170. duk__handle_temproot(heap, hdr);
  35171. #endif
  35172. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  35173. }
  35174. /* must also check refzero_list */
  35175. #ifdef DUK_USE_REFERENCE_COUNTING
  35176. hdr = heap->refzero_list;
  35177. while (hdr) {
  35178. #ifdef DUK_USE_DEBUG
  35179. duk__handle_temproot(heap, hdr, &count);
  35180. #else
  35181. duk__handle_temproot(heap, hdr);
  35182. #endif
  35183. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  35184. }
  35185. #endif /* DUK_USE_REFERENCE_COUNTING */
  35186. #ifdef DUK_USE_DEBUG
  35187. DUK_DD(DUK_DDPRINT("temproot mark heap scan processed %ld temp roots", (long) count));
  35188. #endif
  35189. }
  35190. }
  35191. /*
  35192. * Finalize refcounts for heap elements just about to be freed.
  35193. * This must be done for all objects before freeing to avoid any
  35194. * stale pointer dereferences.
  35195. *
  35196. * Note that this must deduce the set of objects to be freed
  35197. * identically to duk__sweep_heap().
  35198. */
  35199. #ifdef DUK_USE_REFERENCE_COUNTING
  35200. DUK_LOCAL void duk__finalize_refcounts(duk_heap *heap) {
  35201. duk_hthread *thr;
  35202. duk_heaphdr *hdr;
  35203. thr = duk__get_temp_hthread(heap);
  35204. DUK_ASSERT(thr != NULL);
  35205. DUK_DD(DUK_DDPRINT("duk__finalize_refcounts: heap=%p, hthread=%p",
  35206. (void *) heap, (void *) thr));
  35207. hdr = heap->heap_allocated;
  35208. while (hdr) {
  35209. if (!DUK_HEAPHDR_HAS_REACHABLE(hdr)) {
  35210. /*
  35211. * Unreachable object about to be swept. Finalize target refcounts
  35212. * (objects which the unreachable object points to) without doing
  35213. * refzero processing. Recursive decrefs are also prevented when
  35214. * refzero processing is disabled.
  35215. *
  35216. * Value cannot be a finalizable object, as they have been made
  35217. * temporarily reachable for this round.
  35218. */
  35219. DUK_DDD(DUK_DDDPRINT("unreachable object, refcount finalize before sweeping: %p", (void *) hdr));
  35220. duk_heaphdr_refcount_finalize(thr, hdr);
  35221. }
  35222. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  35223. }
  35224. }
  35225. #endif /* DUK_USE_REFERENCE_COUNTING */
  35226. /*
  35227. * Clear (reachable) flags of refzero work list.
  35228. */
  35229. #ifdef DUK_USE_REFERENCE_COUNTING
  35230. DUK_LOCAL void duk__clear_refzero_list_flags(duk_heap *heap) {
  35231. duk_heaphdr *hdr;
  35232. DUK_DD(DUK_DDPRINT("duk__clear_refzero_list_flags: %p", (void *) heap));
  35233. hdr = heap->refzero_list;
  35234. while (hdr) {
  35235. DUK_HEAPHDR_CLEAR_REACHABLE(hdr);
  35236. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(hdr));
  35237. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(hdr));
  35238. DUK_ASSERT(!DUK_HEAPHDR_HAS_TEMPROOT(hdr));
  35239. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  35240. }
  35241. }
  35242. #endif /* DUK_USE_REFERENCE_COUNTING */
  35243. /*
  35244. * Clear (reachable) flags of finalize_list
  35245. *
  35246. * We could mostly do in the sweep phase when we move objects from the
  35247. * heap into the finalize_list. However, if a finalizer run is skipped
  35248. * during a mark-and-sweep, the objects on the finalize_list will be marked
  35249. * reachable during the next mark-and-sweep. Since they're already on the
  35250. * finalize_list, no-one will be clearing their REACHABLE flag so we do it
  35251. * here. (This now overlaps with the sweep handling in a harmless way.)
  35252. */
  35253. DUK_LOCAL void duk__clear_finalize_list_flags(duk_heap *heap) {
  35254. duk_heaphdr *hdr;
  35255. DUK_DD(DUK_DDPRINT("duk__clear_finalize_list_flags: %p", (void *) heap));
  35256. hdr = heap->finalize_list;
  35257. while (hdr) {
  35258. DUK_HEAPHDR_CLEAR_REACHABLE(hdr);
  35259. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(hdr));
  35260. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(hdr));
  35261. DUK_ASSERT(!DUK_HEAPHDR_HAS_TEMPROOT(hdr));
  35262. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  35263. }
  35264. }
  35265. /*
  35266. * Sweep stringtable
  35267. */
  35268. #if defined(DUK_USE_STRTAB_CHAIN)
  35269. /* XXX: skip count_free w/o debug? */
  35270. #if defined(DUK_USE_HEAPPTR16)
  35271. DUK_LOCAL void duk__sweep_string_chain16(duk_heap *heap, duk_uint16_t *slot, duk_size_t *count_keep, duk_size_t *count_free) {
  35272. duk_uint16_t h16 = *slot;
  35273. duk_hstring *h;
  35274. duk_uint16_t null16 = heap->heapptr_null16;
  35275. if (h16 == null16) {
  35276. /* nop */
  35277. return;
  35278. }
  35279. h = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, h16);
  35280. DUK_ASSERT(h != NULL);
  35281. if (DUK_HEAPHDR_HAS_REACHABLE((duk_heaphdr *) h)) {
  35282. DUK_HEAPHDR_CLEAR_REACHABLE((duk_heaphdr *) h);
  35283. (*count_keep)++;
  35284. } else {
  35285. #if defined(DUK_USE_REFERENCE_COUNTING)
  35286. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h) == 0);
  35287. #endif
  35288. /* deal with weak references first */
  35289. duk_heap_strcache_string_remove(heap, (duk_hstring *) h);
  35290. *slot = null16;
  35291. /* free inner references (these exist e.g. when external
  35292. * strings are enabled)
  35293. */
  35294. duk_free_hstring_inner(heap, h);
  35295. DUK_FREE(heap, h);
  35296. (*count_free)++;
  35297. }
  35298. }
  35299. #else /* DUK_USE_HEAPPTR16 */
  35300. DUK_LOCAL void duk__sweep_string_chain(duk_heap *heap, duk_hstring **slot, duk_size_t *count_keep, duk_size_t *count_free) {
  35301. duk_hstring *h = *slot;
  35302. if (h == NULL) {
  35303. /* nop */
  35304. return;
  35305. }
  35306. if (DUK_HEAPHDR_HAS_REACHABLE((duk_heaphdr *) h)) {
  35307. DUK_HEAPHDR_CLEAR_REACHABLE((duk_heaphdr *) h);
  35308. (*count_keep)++;
  35309. } else {
  35310. #if defined(DUK_USE_REFERENCE_COUNTING)
  35311. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h) == 0);
  35312. #endif
  35313. /* deal with weak references first */
  35314. duk_heap_strcache_string_remove(heap, (duk_hstring *) h);
  35315. *slot = NULL;
  35316. /* free inner references (these exist e.g. when external
  35317. * strings are enabled)
  35318. */
  35319. duk_free_hstring_inner(heap, h);
  35320. DUK_FREE(heap, h);
  35321. (*count_free)++;
  35322. }
  35323. }
  35324. #endif /* DUK_USE_HEAPPTR16 */
  35325. DUK_LOCAL void duk__sweep_stringtable_chain(duk_heap *heap, duk_size_t *out_count_keep) {
  35326. duk_strtab_entry *e;
  35327. duk_uint_fast32_t i;
  35328. duk_size_t count_free = 0;
  35329. duk_size_t count_keep = 0;
  35330. duk_size_t j, n;
  35331. #if defined(DUK_USE_HEAPPTR16)
  35332. duk_uint16_t *lst;
  35333. #else
  35334. duk_hstring **lst;
  35335. #endif
  35336. DUK_DD(DUK_DDPRINT("duk__sweep_stringtable: %p", (void *) heap));
  35337. /* Non-zero refcounts should not happen for unreachable strings,
  35338. * because we refcount finalize all unreachable objects which
  35339. * should have decreased unreachable string refcounts to zero
  35340. * (even for cycles).
  35341. */
  35342. for (i = 0; i < DUK_STRTAB_CHAIN_SIZE; i++) {
  35343. e = heap->strtable + i;
  35344. if (e->listlen == 0) {
  35345. #if defined(DUK_USE_HEAPPTR16)
  35346. duk__sweep_string_chain16(heap, &e->u.str16, &count_keep, &count_free);
  35347. #else
  35348. duk__sweep_string_chain(heap, &e->u.str, &count_keep, &count_free);
  35349. #endif
  35350. } else {
  35351. #if defined(DUK_USE_HEAPPTR16)
  35352. lst = (duk_uint16_t *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.strlist16);
  35353. #else
  35354. lst = e->u.strlist;
  35355. #endif
  35356. for (j = 0, n = e->listlen; j < n; j++) {
  35357. #if defined(DUK_USE_HEAPPTR16)
  35358. duk__sweep_string_chain16(heap, lst + j, &count_keep, &count_free);
  35359. #else
  35360. duk__sweep_string_chain(heap, lst + j, &count_keep, &count_free);
  35361. #endif
  35362. }
  35363. }
  35364. }
  35365. DUK_D(DUK_DPRINT("mark-and-sweep sweep stringtable: %ld freed, %ld kept",
  35366. (long) count_free, (long) count_keep));
  35367. *out_count_keep = count_keep;
  35368. }
  35369. #endif /* DUK_USE_STRTAB_CHAIN */
  35370. #if defined(DUK_USE_STRTAB_PROBE)
  35371. DUK_LOCAL void duk__sweep_stringtable_probe(duk_heap *heap, duk_size_t *out_count_keep) {
  35372. duk_hstring *h;
  35373. duk_uint_fast32_t i;
  35374. #ifdef DUK_USE_DEBUG
  35375. duk_size_t count_free = 0;
  35376. #endif
  35377. duk_size_t count_keep = 0;
  35378. DUK_DD(DUK_DDPRINT("duk__sweep_stringtable: %p", (void *) heap));
  35379. for (i = 0; i < heap->st_size; i++) {
  35380. #if defined(DUK_USE_HEAPPTR16)
  35381. h = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->strtable16[i]);
  35382. #else
  35383. h = heap->strtable[i];
  35384. #endif
  35385. if (h == NULL || h == DUK_STRTAB_DELETED_MARKER(heap)) {
  35386. continue;
  35387. } else if (DUK_HEAPHDR_HAS_REACHABLE((duk_heaphdr *) h)) {
  35388. DUK_HEAPHDR_CLEAR_REACHABLE((duk_heaphdr *) h);
  35389. count_keep++;
  35390. continue;
  35391. }
  35392. #ifdef DUK_USE_DEBUG
  35393. count_free++;
  35394. #endif
  35395. #if defined(DUK_USE_REFERENCE_COUNTING)
  35396. /* Non-zero refcounts should not happen for unreachable strings,
  35397. * because we refcount finalize all unreachable objects which
  35398. * should have decreased unreachable string refcounts to zero
  35399. * (even for cycles).
  35400. */
  35401. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h) == 0);
  35402. #endif
  35403. DUK_DDD(DUK_DDDPRINT("sweep string, not reachable: %p", (void *) h));
  35404. /* deal with weak references first */
  35405. duk_heap_strcache_string_remove(heap, (duk_hstring *) h);
  35406. /* remove the string (mark DELETED), could also call
  35407. * duk_heap_string_remove() but that would be slow and
  35408. * pointless because we already know the slot.
  35409. */
  35410. #if defined(DUK_USE_HEAPPTR16)
  35411. heap->strtable16[i] = heap->heapptr_deleted16;
  35412. #else
  35413. heap->strtable[i] = DUK_STRTAB_DELETED_MARKER(heap);
  35414. #endif
  35415. /* free inner references (these exist e.g. when external
  35416. * strings are enabled)
  35417. */
  35418. duk_free_hstring_inner(heap, (duk_hstring *) h);
  35419. /* finally free the struct itself */
  35420. DUK_FREE(heap, h);
  35421. }
  35422. #ifdef DUK_USE_DEBUG
  35423. DUK_D(DUK_DPRINT("mark-and-sweep sweep stringtable: %ld freed, %ld kept",
  35424. (long) count_free, (long) count_keep));
  35425. #endif
  35426. *out_count_keep = count_keep;
  35427. }
  35428. #endif /* DUK_USE_STRTAB_PROBE */
  35429. /*
  35430. * Sweep heap
  35431. */
  35432. DUK_LOCAL void duk__sweep_heap(duk_heap *heap, duk_int_t flags, duk_size_t *out_count_keep) {
  35433. duk_heaphdr *prev; /* last element that was left in the heap */
  35434. duk_heaphdr *curr;
  35435. duk_heaphdr *next;
  35436. #ifdef DUK_USE_DEBUG
  35437. duk_size_t count_free = 0;
  35438. duk_size_t count_finalize = 0;
  35439. duk_size_t count_rescue = 0;
  35440. #endif
  35441. duk_size_t count_keep = 0;
  35442. DUK_UNREF(flags);
  35443. DUK_DD(DUK_DDPRINT("duk__sweep_heap: %p", (void *) heap));
  35444. prev = NULL;
  35445. curr = heap->heap_allocated;
  35446. heap->heap_allocated = NULL;
  35447. while (curr) {
  35448. /* strings are never placed on the heap allocated list */
  35449. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(curr) != DUK_HTYPE_STRING);
  35450. next = DUK_HEAPHDR_GET_NEXT(heap, curr);
  35451. if (DUK_HEAPHDR_HAS_REACHABLE(curr)) {
  35452. /*
  35453. * Reachable object, keep
  35454. */
  35455. DUK_DDD(DUK_DDDPRINT("sweep, reachable: %p", (void *) curr));
  35456. if (DUK_HEAPHDR_HAS_FINALIZABLE(curr)) {
  35457. /*
  35458. * If object has been marked finalizable, move it to the
  35459. * "to be finalized" work list. It will be collected on
  35460. * the next mark-and-sweep if it is still unreachable
  35461. * after running the finalizer.
  35462. */
  35463. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(curr));
  35464. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(curr) == DUK_HTYPE_OBJECT);
  35465. DUK_DDD(DUK_DDDPRINT("object has finalizer, move to finalization work list: %p", (void *) curr));
  35466. #ifdef DUK_USE_DOUBLE_LINKED_HEAP
  35467. if (heap->finalize_list) {
  35468. DUK_HEAPHDR_SET_PREV(heap, heap->finalize_list, curr);
  35469. }
  35470. DUK_HEAPHDR_SET_PREV(heap, curr, NULL);
  35471. #endif
  35472. DUK_HEAPHDR_SET_NEXT(heap, curr, heap->finalize_list);
  35473. heap->finalize_list = curr;
  35474. #ifdef DUK_USE_DEBUG
  35475. count_finalize++;
  35476. #endif
  35477. } else {
  35478. /*
  35479. * Object will be kept; queue object back to heap_allocated (to tail)
  35480. */
  35481. if (DUK_HEAPHDR_HAS_FINALIZED(curr)) {
  35482. /*
  35483. * Object's finalizer was executed on last round, and
  35484. * object has been happily rescued.
  35485. */
  35486. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(curr));
  35487. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(curr) == DUK_HTYPE_OBJECT);
  35488. DUK_DD(DUK_DDPRINT("object rescued during mark-and-sweep finalization: %p", (void *) curr));
  35489. #ifdef DUK_USE_DEBUG
  35490. count_rescue++;
  35491. #endif
  35492. } else {
  35493. /*
  35494. * Plain, boring reachable object.
  35495. */
  35496. count_keep++;
  35497. }
  35498. if (!heap->heap_allocated) {
  35499. heap->heap_allocated = curr;
  35500. }
  35501. if (prev) {
  35502. DUK_HEAPHDR_SET_NEXT(heap, prev, curr);
  35503. }
  35504. #ifdef DUK_USE_DOUBLE_LINKED_HEAP
  35505. DUK_HEAPHDR_SET_PREV(heap, curr, prev);
  35506. #endif
  35507. prev = curr;
  35508. }
  35509. DUK_HEAPHDR_CLEAR_REACHABLE(curr);
  35510. DUK_HEAPHDR_CLEAR_FINALIZED(curr);
  35511. DUK_HEAPHDR_CLEAR_FINALIZABLE(curr);
  35512. DUK_ASSERT(!DUK_HEAPHDR_HAS_REACHABLE(curr));
  35513. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(curr));
  35514. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(curr));
  35515. curr = next;
  35516. } else {
  35517. /*
  35518. * Unreachable object, free
  35519. */
  35520. DUK_DDD(DUK_DDDPRINT("sweep, not reachable: %p", (void *) curr));
  35521. #if defined(DUK_USE_REFERENCE_COUNTING)
  35522. /* Non-zero refcounts should not happen because we refcount
  35523. * finalize all unreachable objects which should cancel out
  35524. * refcounts (even for cycles).
  35525. */
  35526. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(curr) == 0);
  35527. #endif
  35528. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(curr));
  35529. if (DUK_HEAPHDR_HAS_FINALIZED(curr)) {
  35530. DUK_DDD(DUK_DDDPRINT("finalized object not rescued: %p", (void *) curr));
  35531. }
  35532. /* Note: object cannot be a finalizable unreachable object, as
  35533. * they have been marked temporarily reachable for this round,
  35534. * and are handled above.
  35535. */
  35536. #ifdef DUK_USE_DEBUG
  35537. count_free++;
  35538. #endif
  35539. /* weak refs should be handled here, but no weak refs for
  35540. * any non-string objects exist right now.
  35541. */
  35542. /* free object and all auxiliary (non-heap) allocs */
  35543. duk_heap_free_heaphdr_raw(heap, curr);
  35544. curr = next;
  35545. }
  35546. }
  35547. if (prev) {
  35548. DUK_HEAPHDR_SET_NEXT(heap, prev, NULL);
  35549. }
  35550. #ifdef DUK_USE_DEBUG
  35551. DUK_D(DUK_DPRINT("mark-and-sweep sweep objects (non-string): %ld freed, %ld kept, %ld rescued, %ld queued for finalization",
  35552. (long) count_free, (long) count_keep, (long) count_rescue, (long) count_finalize));
  35553. #endif
  35554. *out_count_keep = count_keep;
  35555. }
  35556. /*
  35557. * Run (object) finalizers in the "to be finalized" work list.
  35558. */
  35559. DUK_LOCAL void duk__run_object_finalizers(duk_heap *heap) {
  35560. duk_heaphdr *curr;
  35561. duk_heaphdr *next;
  35562. #ifdef DUK_USE_DEBUG
  35563. duk_size_t count = 0;
  35564. #endif
  35565. duk_hthread *thr;
  35566. DUK_DD(DUK_DDPRINT("duk__run_object_finalizers: %p", (void *) heap));
  35567. thr = duk__get_temp_hthread(heap);
  35568. DUK_ASSERT(thr != NULL);
  35569. curr = heap->finalize_list;
  35570. while (curr) {
  35571. DUK_DDD(DUK_DDDPRINT("mark-and-sweep finalize: %p", (void *) curr));
  35572. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(curr) == DUK_HTYPE_OBJECT); /* only objects have finalizers */
  35573. DUK_ASSERT(!DUK_HEAPHDR_HAS_REACHABLE(curr)); /* flags have been already cleared */
  35574. DUK_ASSERT(!DUK_HEAPHDR_HAS_TEMPROOT(curr));
  35575. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(curr));
  35576. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(curr));
  35577. /* run the finalizer */
  35578. duk_hobject_run_finalizer(thr, (duk_hobject *) curr); /* must never longjmp */
  35579. /* mark FINALIZED, for next mark-and-sweep (will collect unless has become reachable;
  35580. * prevent running finalizer again if reachable)
  35581. */
  35582. DUK_HEAPHDR_SET_FINALIZED(curr);
  35583. /* queue back to heap_allocated */
  35584. next = DUK_HEAPHDR_GET_NEXT(heap, curr);
  35585. DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap, curr);
  35586. curr = next;
  35587. #ifdef DUK_USE_DEBUG
  35588. count++;
  35589. #endif
  35590. }
  35591. /* finalize_list will always be processed completely */
  35592. heap->finalize_list = NULL;
  35593. #ifdef DUK_USE_DEBUG
  35594. DUK_D(DUK_DPRINT("mark-and-sweep finalize objects: %ld finalizers called", (long) count));
  35595. #endif
  35596. }
  35597. /*
  35598. * Object compaction.
  35599. *
  35600. * Compaction is assumed to never throw an error.
  35601. */
  35602. DUK_LOCAL int duk__protected_compact_object(duk_context *ctx) {
  35603. /* XXX: for threads, compact value stack, call stack, catch stack? */
  35604. duk_hobject *obj = duk_get_hobject(ctx, -1);
  35605. DUK_ASSERT(obj != NULL);
  35606. duk_hobject_compact_props((duk_hthread *) ctx, obj);
  35607. return 0;
  35608. }
  35609. #ifdef DUK_USE_DEBUG
  35610. 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) {
  35611. #else
  35612. DUK_LOCAL void duk__compact_object_list(duk_heap *heap, duk_hthread *thr, duk_heaphdr *start) {
  35613. #endif
  35614. duk_heaphdr *curr;
  35615. #ifdef DUK_USE_DEBUG
  35616. duk_size_t old_size, new_size;
  35617. #endif
  35618. duk_hobject *obj;
  35619. DUK_UNREF(heap);
  35620. curr = start;
  35621. while (curr) {
  35622. DUK_DDD(DUK_DDDPRINT("mark-and-sweep compact: %p", (void *) curr));
  35623. if (DUK_HEAPHDR_GET_TYPE(curr) != DUK_HTYPE_OBJECT) {
  35624. goto next;
  35625. }
  35626. obj = (duk_hobject *) curr;
  35627. #ifdef DUK_USE_DEBUG
  35628. old_size = DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE(obj),
  35629. DUK_HOBJECT_GET_ASIZE(obj),
  35630. DUK_HOBJECT_GET_HSIZE(obj));
  35631. #endif
  35632. DUK_DD(DUK_DDPRINT("compact object: %p", (void *) obj));
  35633. duk_push_hobject((duk_context *) thr, obj);
  35634. /* XXX: disable error handlers for duration of compaction? */
  35635. duk_safe_call((duk_context *) thr, duk__protected_compact_object, 1, 0);
  35636. #ifdef DUK_USE_DEBUG
  35637. new_size = DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE(obj),
  35638. DUK_HOBJECT_GET_ASIZE(obj),
  35639. DUK_HOBJECT_GET_HSIZE(obj));
  35640. #endif
  35641. #ifdef DUK_USE_DEBUG
  35642. (*p_count_compact)++;
  35643. (*p_count_bytes_saved) += (duk_size_t) (old_size - new_size);
  35644. #endif
  35645. next:
  35646. curr = DUK_HEAPHDR_GET_NEXT(heap, curr);
  35647. #ifdef DUK_USE_DEBUG
  35648. (*p_count_check)++;
  35649. #endif
  35650. }
  35651. }
  35652. DUK_LOCAL void duk__compact_objects(duk_heap *heap) {
  35653. /* XXX: which lists should participate? to be finalized? */
  35654. #ifdef DUK_USE_DEBUG
  35655. duk_size_t count_check = 0;
  35656. duk_size_t count_compact = 0;
  35657. duk_size_t count_bytes_saved = 0;
  35658. #endif
  35659. duk_hthread *thr;
  35660. DUK_DD(DUK_DDPRINT("duk__compact_objects: %p", (void *) heap));
  35661. thr = duk__get_temp_hthread(heap);
  35662. DUK_ASSERT(thr != NULL);
  35663. #ifdef DUK_USE_DEBUG
  35664. duk__compact_object_list(heap, thr, heap->heap_allocated, &count_check, &count_compact, &count_bytes_saved);
  35665. duk__compact_object_list(heap, thr, heap->finalize_list, &count_check, &count_compact, &count_bytes_saved);
  35666. #ifdef DUK_USE_REFERENCE_COUNTING
  35667. duk__compact_object_list(heap, thr, heap->refzero_list, &count_check, &count_compact, &count_bytes_saved);
  35668. #endif
  35669. #else
  35670. duk__compact_object_list(heap, thr, heap->heap_allocated);
  35671. duk__compact_object_list(heap, thr, heap->finalize_list);
  35672. #ifdef DUK_USE_REFERENCE_COUNTING
  35673. duk__compact_object_list(heap, thr, heap->refzero_list);
  35674. #endif
  35675. #endif
  35676. #ifdef DUK_USE_DEBUG
  35677. DUK_D(DUK_DPRINT("mark-and-sweep compact objects: %ld checked, %ld compaction attempts, %ld bytes saved by compaction",
  35678. (long) count_check, (long) count_compact, (long) count_bytes_saved));
  35679. #endif
  35680. }
  35681. /*
  35682. * Assertion helpers.
  35683. */
  35684. #ifdef DUK_USE_ASSERTIONS
  35685. DUK_LOCAL void duk__assert_heaphdr_flags(duk_heap *heap) {
  35686. duk_heaphdr *hdr;
  35687. hdr = heap->heap_allocated;
  35688. while (hdr) {
  35689. DUK_ASSERT(!DUK_HEAPHDR_HAS_REACHABLE(hdr));
  35690. DUK_ASSERT(!DUK_HEAPHDR_HAS_TEMPROOT(hdr));
  35691. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(hdr));
  35692. /* may have FINALIZED */
  35693. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  35694. }
  35695. #ifdef DUK_USE_REFERENCE_COUNTING
  35696. hdr = heap->refzero_list;
  35697. while (hdr) {
  35698. DUK_ASSERT(!DUK_HEAPHDR_HAS_REACHABLE(hdr));
  35699. DUK_ASSERT(!DUK_HEAPHDR_HAS_TEMPROOT(hdr));
  35700. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(hdr));
  35701. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(hdr));
  35702. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  35703. }
  35704. #endif /* DUK_USE_REFERENCE_COUNTING */
  35705. }
  35706. #ifdef DUK_USE_REFERENCE_COUNTING
  35707. DUK_LOCAL void duk__assert_valid_refcounts(duk_heap *heap) {
  35708. duk_heaphdr *hdr = heap->heap_allocated;
  35709. while (hdr) {
  35710. if (DUK_HEAPHDR_GET_REFCOUNT(hdr) == 0 &&
  35711. DUK_HEAPHDR_HAS_FINALIZED(hdr)) {
  35712. /* An object may be in heap_allocated list with a zero
  35713. * refcount if it has just been finalized and is waiting
  35714. * to be collected by the next cycle.
  35715. */
  35716. } else if (DUK_HEAPHDR_GET_REFCOUNT(hdr) == 0) {
  35717. /* An object may be in heap_allocated list with a zero
  35718. * refcount also if it is a temporary object created by
  35719. * a finalizer; because finalization now runs inside
  35720. * mark-and-sweep, such objects will not be queued to
  35721. * refzero_list and will thus appear here with refcount
  35722. * zero.
  35723. */
  35724. #if 0 /* this case can no longer occur because refcount is unsigned */
  35725. } else if (DUK_HEAPHDR_GET_REFCOUNT(hdr) < 0) {
  35726. DUK_D(DUK_DPRINT("invalid refcount: %ld, %p -> %!O",
  35727. (hdr != NULL ? (long) DUK_HEAPHDR_GET_REFCOUNT(hdr) : (long) 0),
  35728. (void *) hdr, (duk_heaphdr *) hdr));
  35729. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(hdr) > 0);
  35730. #endif
  35731. }
  35732. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  35733. }
  35734. }
  35735. #endif /* DUK_USE_REFERENCE_COUNTING */
  35736. #endif /* DUK_USE_ASSERTIONS */
  35737. /*
  35738. * Main mark-and-sweep function.
  35739. *
  35740. * 'flags' represents the features requested by the caller. The current
  35741. * heap->mark_and_sweep_base_flags is ORed automatically into the flags;
  35742. * the base flags mask typically prevents certain mark-and-sweep operations
  35743. * to avoid trouble.
  35744. */
  35745. DUK_INTERNAL duk_bool_t duk_heap_mark_and_sweep(duk_heap *heap, duk_small_uint_t flags) {
  35746. duk_hthread *thr;
  35747. duk_size_t count_keep_obj;
  35748. duk_size_t count_keep_str;
  35749. #ifdef DUK_USE_VOLUNTARY_GC
  35750. duk_size_t tmp;
  35751. #endif
  35752. /* XXX: thread selection for mark-and-sweep is currently a hack.
  35753. * If we don't have a thread, the entire mark-and-sweep is now
  35754. * skipped (although we could just skip finalizations).
  35755. */
  35756. thr = duk__get_temp_hthread(heap);
  35757. if (thr == NULL) {
  35758. DUK_D(DUK_DPRINT("temporary hack: gc skipped because we don't have a temp thread"));
  35759. /* reset voluntary gc trigger count */
  35760. #ifdef DUK_USE_VOLUNTARY_GC
  35761. heap->mark_and_sweep_trigger_counter = DUK_HEAP_MARK_AND_SWEEP_TRIGGER_SKIP;
  35762. #endif
  35763. return 0; /* OK */
  35764. }
  35765. DUK_D(DUK_DPRINT("garbage collect (mark-and-sweep) starting, requested flags: 0x%08lx, effective flags: 0x%08lx",
  35766. (unsigned long) flags, (unsigned long) (flags | heap->mark_and_sweep_base_flags)));
  35767. flags |= heap->mark_and_sweep_base_flags;
  35768. /*
  35769. * Assertions before
  35770. */
  35771. #ifdef DUK_USE_ASSERTIONS
  35772. DUK_ASSERT(!DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap));
  35773. DUK_ASSERT(!DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED(heap));
  35774. DUK_ASSERT(heap->mark_and_sweep_recursion_depth == 0);
  35775. duk__assert_heaphdr_flags(heap);
  35776. #ifdef DUK_USE_REFERENCE_COUNTING
  35777. /* Note: DUK_HEAP_HAS_REFZERO_FREE_RUNNING(heap) may be true; a refcount
  35778. * finalizer may trigger a mark-and-sweep.
  35779. */
  35780. duk__assert_valid_refcounts(heap);
  35781. #endif /* DUK_USE_REFERENCE_COUNTING */
  35782. #endif /* DUK_USE_ASSERTIONS */
  35783. /*
  35784. * Begin
  35785. */
  35786. DUK_HEAP_SET_MARKANDSWEEP_RUNNING(heap);
  35787. /*
  35788. * Mark roots, hoping that recursion limit is not normally hit.
  35789. * If recursion limit is hit, run additional reachability rounds
  35790. * starting from "temproots" until marking is complete.
  35791. *
  35792. * Marking happens in two phases: first we mark actual reachability
  35793. * roots (and run "temproots" to complete the process). Then we
  35794. * check which objects are unreachable and are finalizable; such
  35795. * objects are marked as FINALIZABLE and marked as reachability
  35796. * (and "temproots" is run again to complete the process).
  35797. *
  35798. * The heap finalize_list must also be marked as a reachability root.
  35799. * There may be objects on the list from a previous round if the
  35800. * previous run had finalizer skip flag.
  35801. */
  35802. duk__mark_roots_heap(heap); /* main reachability roots */
  35803. #ifdef DUK_USE_REFERENCE_COUNTING
  35804. duk__mark_refzero_list(heap); /* refzero_list treated as reachability roots */
  35805. #endif
  35806. duk__mark_temproots_by_heap_scan(heap); /* temproots */
  35807. duk__mark_finalizable(heap); /* mark finalizable as reachability roots */
  35808. duk__mark_finalize_list(heap); /* mark finalizer work list as reachability roots */
  35809. duk__mark_temproots_by_heap_scan(heap); /* temproots */
  35810. /*
  35811. * Sweep garbage and remove marking flags, and move objects with
  35812. * finalizers to the finalizer work list.
  35813. *
  35814. * Objects to be swept need to get their refcounts finalized before
  35815. * they are swept. In other words, their target object refcounts
  35816. * need to be decreased. This has to be done before freeing any
  35817. * objects to avoid decref'ing dangling pointers (which may happen
  35818. * even without bugs, e.g. with reference loops)
  35819. *
  35820. * Because strings don't point to other heap objects, similar
  35821. * finalization is not necessary for strings.
  35822. */
  35823. /* XXX: more emergency behavior, e.g. find smaller hash sizes etc */
  35824. #ifdef DUK_USE_REFERENCE_COUNTING
  35825. duk__finalize_refcounts(heap);
  35826. #endif
  35827. duk__sweep_heap(heap, flags, &count_keep_obj);
  35828. #if defined(DUK_USE_STRTAB_CHAIN)
  35829. duk__sweep_stringtable_chain(heap, &count_keep_str);
  35830. #elif defined(DUK_USE_STRTAB_PROBE)
  35831. duk__sweep_stringtable_probe(heap, &count_keep_str);
  35832. #else
  35833. #error internal error, invalid strtab options
  35834. #endif
  35835. #ifdef DUK_USE_REFERENCE_COUNTING
  35836. duk__clear_refzero_list_flags(heap);
  35837. #endif
  35838. duk__clear_finalize_list_flags(heap);
  35839. /*
  35840. * Object compaction (emergency only).
  35841. *
  35842. * Object compaction is a separate step after sweeping, as there is
  35843. * more free memory for it to work with. Also, currently compaction
  35844. * may insert new objects into the heap allocated list and the string
  35845. * table which we don't want to do during a sweep (the reachability
  35846. * flags of such objects would be incorrect). The objects inserted
  35847. * are currently:
  35848. *
  35849. * - a temporary duk_hbuffer for a new properties allocation
  35850. * - if array part is abandoned, string keys are interned
  35851. *
  35852. * The object insertions go to the front of the list, so they do not
  35853. * cause an infinite loop (they are not compacted).
  35854. */
  35855. if ((flags & DUK_MS_FLAG_EMERGENCY) &&
  35856. !(flags & DUK_MS_FLAG_NO_OBJECT_COMPACTION)) {
  35857. duk__compact_objects(heap);
  35858. }
  35859. /*
  35860. * String table resize check.
  35861. *
  35862. * Note: this may silently (and safely) fail if GC is caused by an
  35863. * allocation call in stringtable resize_hash(). Resize_hash()
  35864. * will prevent a recursive call to itself by setting the
  35865. * DUK_MS_FLAG_NO_STRINGTABLE_RESIZE in heap->mark_and_sweep_base_flags.
  35866. */
  35867. /* XXX: stringtable emergency compaction? */
  35868. #if defined(DUK_USE_MS_STRINGTABLE_RESIZE)
  35869. if (!(flags & DUK_MS_FLAG_NO_STRINGTABLE_RESIZE)) {
  35870. DUK_DD(DUK_DDPRINT("resize stringtable: %p", (void *) heap));
  35871. duk_heap_force_strtab_resize(heap);
  35872. } else {
  35873. DUK_D(DUK_DPRINT("stringtable resize skipped because DUK_MS_FLAG_NO_STRINGTABLE_RESIZE is set"));
  35874. }
  35875. #endif
  35876. /*
  35877. * Finalize objects in the finalization work list. Finalized
  35878. * objects are queued back to heap_allocated with FINALIZED set.
  35879. *
  35880. * Since finalizers may cause arbitrary side effects, they are
  35881. * prevented during string table and object property allocation
  35882. * resizing using the DUK_MS_FLAG_NO_FINALIZERS flag in
  35883. * heap->mark_and_sweep_base_flags. In this case the objects
  35884. * remain in the finalization work list after mark-and-sweep
  35885. * exits and they may be finalized on the next pass.
  35886. *
  35887. * Finalization currently happens inside "MARKANDSWEEP_RUNNING"
  35888. * protection (no mark-and-sweep may be triggered by the
  35889. * finalizers). As a side effect:
  35890. *
  35891. * 1) an out-of-memory error inside a finalizer will not
  35892. * cause a mark-and-sweep and may cause the finalizer
  35893. * to fail unnecessarily
  35894. *
  35895. * 2) any temporary objects whose refcount decreases to zero
  35896. * during finalization will not be put into refzero_list;
  35897. * they can only be collected by another mark-and-sweep
  35898. *
  35899. * This is not optimal, but since the sweep for this phase has
  35900. * already happened, this is probably good enough for now.
  35901. */
  35902. if (!(flags & DUK_MS_FLAG_NO_FINALIZERS)) {
  35903. duk__run_object_finalizers(heap);
  35904. } else {
  35905. DUK_D(DUK_DPRINT("finalizer run skipped because DUK_MS_FLAG_NO_FINALIZERS is set"));
  35906. }
  35907. /*
  35908. * Finish
  35909. */
  35910. DUK_HEAP_CLEAR_MARKANDSWEEP_RUNNING(heap);
  35911. /*
  35912. * Assertions after
  35913. */
  35914. #ifdef DUK_USE_ASSERTIONS
  35915. DUK_ASSERT(!DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap));
  35916. DUK_ASSERT(!DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED(heap));
  35917. DUK_ASSERT(heap->mark_and_sweep_recursion_depth == 0);
  35918. duk__assert_heaphdr_flags(heap);
  35919. #ifdef DUK_USE_REFERENCE_COUNTING
  35920. /* Note: DUK_HEAP_HAS_REFZERO_FREE_RUNNING(heap) may be true; a refcount
  35921. * finalizer may trigger a mark-and-sweep.
  35922. */
  35923. duk__assert_valid_refcounts(heap);
  35924. #endif /* DUK_USE_REFERENCE_COUNTING */
  35925. #endif /* DUK_USE_ASSERTIONS */
  35926. /*
  35927. * Reset trigger counter
  35928. */
  35929. #ifdef DUK_USE_VOLUNTARY_GC
  35930. tmp = (count_keep_obj + count_keep_str) / 256;
  35931. heap->mark_and_sweep_trigger_counter = (duk_int_t) (
  35932. (tmp * DUK_HEAP_MARK_AND_SWEEP_TRIGGER_MULT) +
  35933. DUK_HEAP_MARK_AND_SWEEP_TRIGGER_ADD);
  35934. DUK_D(DUK_DPRINT("garbage collect (mark-and-sweep) finished: %ld objects kept, %ld strings kept, trigger reset to %ld",
  35935. (long) count_keep_obj, (long) count_keep_str, (long) heap->mark_and_sweep_trigger_counter));
  35936. #else
  35937. DUK_D(DUK_DPRINT("garbage collect (mark-and-sweep) finished: %ld objects kept, %ld strings kept, no voluntary trigger",
  35938. (long) count_keep_obj, (long) count_keep_str));
  35939. #endif
  35940. return 0; /* OK */
  35941. }
  35942. #else /* DUK_USE_MARK_AND_SWEEP */
  35943. /* no mark-and-sweep gc */
  35944. #endif /* DUK_USE_MARK_AND_SWEEP */
  35945. #line 1 "duk_heap_memory.c"
  35946. /*
  35947. * Memory allocation handling.
  35948. */
  35949. /* include removed: duk_internal.h */
  35950. /*
  35951. * Helpers
  35952. *
  35953. * The fast path checks are done within a macro to ensure "inlining"
  35954. * while the slow path actions use a helper (which won't typically be
  35955. * inlined in size optimized builds).
  35956. */
  35957. #if defined(DUK_USE_MARK_AND_SWEEP) && defined(DUK_USE_VOLUNTARY_GC)
  35958. #define DUK__VOLUNTARY_PERIODIC_GC(heap) do { \
  35959. (heap)->mark_and_sweep_trigger_counter--; \
  35960. if ((heap)->mark_and_sweep_trigger_counter <= 0) { \
  35961. duk__run_voluntary_gc(heap); \
  35962. } \
  35963. } while (0)
  35964. DUK_LOCAL void duk__run_voluntary_gc(duk_heap *heap) {
  35965. if (DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)) {
  35966. DUK_DD(DUK_DDPRINT("mark-and-sweep in progress -> skip voluntary mark-and-sweep now"));
  35967. } else {
  35968. duk_small_uint_t flags;
  35969. duk_bool_t rc;
  35970. DUK_D(DUK_DPRINT("triggering voluntary mark-and-sweep"));
  35971. flags = 0;
  35972. rc = duk_heap_mark_and_sweep(heap, flags);
  35973. DUK_UNREF(rc);
  35974. }
  35975. }
  35976. #else
  35977. #define DUK__VOLUNTARY_PERIODIC_GC(heap) /* no voluntary gc */
  35978. #endif /* DUK_USE_MARK_AND_SWEEP && DUK_USE_VOLUNTARY_GC */
  35979. /*
  35980. * Allocate memory with garbage collection
  35981. */
  35982. #ifdef DUK_USE_MARK_AND_SWEEP
  35983. DUK_INTERNAL void *duk_heap_mem_alloc(duk_heap *heap, duk_size_t size) {
  35984. void *res;
  35985. duk_bool_t rc;
  35986. duk_small_int_t i;
  35987. DUK_ASSERT(heap != NULL);
  35988. DUK_ASSERT_DISABLE(size >= 0);
  35989. /*
  35990. * Voluntary periodic GC (if enabled)
  35991. */
  35992. DUK__VOLUNTARY_PERIODIC_GC(heap);
  35993. /*
  35994. * First attempt
  35995. */
  35996. #ifdef DUK_USE_GC_TORTURE
  35997. /* simulate alloc failure on every alloc (except when mark-and-sweep is running) */
  35998. if (!DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)) {
  35999. DUK_DDD(DUK_DDDPRINT("gc torture enabled, pretend that first alloc attempt fails"));
  36000. res = NULL;
  36001. DUK_UNREF(res);
  36002. goto skip_attempt;
  36003. }
  36004. #endif
  36005. res = heap->alloc_func(heap->heap_udata, size);
  36006. if (res || size == 0) {
  36007. /* for zero size allocations NULL is allowed */
  36008. return res;
  36009. }
  36010. #ifdef DUK_USE_GC_TORTURE
  36011. skip_attempt:
  36012. #endif
  36013. DUK_D(DUK_DPRINT("first alloc attempt failed, attempt to gc and retry"));
  36014. /*
  36015. * Avoid a GC if GC is already running. This can happen at a late
  36016. * stage in a GC when we try to e.g. resize the stringtable
  36017. * or compact objects.
  36018. */
  36019. if (DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)) {
  36020. DUK_D(DUK_DPRINT("duk_heap_mem_alloc() failed, gc in progress (gc skipped), alloc size %ld", (long) size));
  36021. return NULL;
  36022. }
  36023. /*
  36024. * Retry with several GC attempts. Initial attempts are made without
  36025. * emergency mode; later attempts use emergency mode which minimizes
  36026. * memory allocations forcibly.
  36027. */
  36028. for (i = 0; i < DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT; i++) {
  36029. duk_small_uint_t flags;
  36030. flags = 0;
  36031. if (i >= DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT - 1) {
  36032. flags |= DUK_MS_FLAG_EMERGENCY;
  36033. }
  36034. rc = duk_heap_mark_and_sweep(heap, flags);
  36035. DUK_UNREF(rc);
  36036. res = heap->alloc_func(heap->heap_udata, size);
  36037. if (res) {
  36038. DUK_D(DUK_DPRINT("duk_heap_mem_alloc() succeeded after gc (pass %ld), alloc size %ld",
  36039. (long) (i + 1), (long) size));
  36040. return res;
  36041. }
  36042. }
  36043. DUK_D(DUK_DPRINT("duk_heap_mem_alloc() failed even after gc, alloc size %ld", (long) size));
  36044. return NULL;
  36045. }
  36046. #else /* DUK_USE_MARK_AND_SWEEP */
  36047. /*
  36048. * Compared to a direct macro expansion this wrapper saves a few
  36049. * instructions because no heap dereferencing is required.
  36050. */
  36051. DUK_INTERNAL void *duk_heap_mem_alloc(duk_heap *heap, duk_size_t size) {
  36052. DUK_ASSERT(heap != NULL);
  36053. DUK_ASSERT_DISABLE(size >= 0);
  36054. return heap->alloc_func(heap->heap_udata, size);
  36055. }
  36056. #endif /* DUK_USE_MARK_AND_SWEEP */
  36057. DUK_INTERNAL void *duk_heap_mem_alloc_zeroed(duk_heap *heap, duk_size_t size) {
  36058. void *res;
  36059. DUK_ASSERT(heap != NULL);
  36060. DUK_ASSERT_DISABLE(size >= 0);
  36061. res = DUK_ALLOC(heap, size);
  36062. if (res) {
  36063. /* assume memset with zero size is OK */
  36064. DUK_MEMZERO(res, size);
  36065. }
  36066. return res;
  36067. }
  36068. /*
  36069. * Reallocate memory with garbage collection
  36070. */
  36071. #ifdef DUK_USE_MARK_AND_SWEEP
  36072. DUK_INTERNAL void *duk_heap_mem_realloc(duk_heap *heap, void *ptr, duk_size_t newsize) {
  36073. void *res;
  36074. duk_bool_t rc;
  36075. duk_small_int_t i;
  36076. DUK_ASSERT(heap != NULL);
  36077. /* ptr may be NULL */
  36078. DUK_ASSERT_DISABLE(newsize >= 0);
  36079. /*
  36080. * Voluntary periodic GC (if enabled)
  36081. */
  36082. DUK__VOLUNTARY_PERIODIC_GC(heap);
  36083. /*
  36084. * First attempt
  36085. */
  36086. #ifdef DUK_USE_GC_TORTURE
  36087. /* simulate alloc failure on every realloc (except when mark-and-sweep is running) */
  36088. if (!DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)) {
  36089. DUK_DDD(DUK_DDDPRINT("gc torture enabled, pretend that first realloc attempt fails"));
  36090. res = NULL;
  36091. DUK_UNREF(res);
  36092. goto skip_attempt;
  36093. }
  36094. #endif
  36095. res = heap->realloc_func(heap->heap_udata, ptr, newsize);
  36096. if (res || newsize == 0) {
  36097. /* for zero size allocations NULL is allowed */
  36098. return res;
  36099. }
  36100. #ifdef DUK_USE_GC_TORTURE
  36101. skip_attempt:
  36102. #endif
  36103. DUK_D(DUK_DPRINT("first realloc attempt failed, attempt to gc and retry"));
  36104. /*
  36105. * Avoid a GC if GC is already running. See duk_heap_mem_alloc().
  36106. */
  36107. if (DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)) {
  36108. DUK_D(DUK_DPRINT("duk_heap_mem_realloc() failed, gc in progress (gc skipped), alloc size %ld", (long) newsize));
  36109. return NULL;
  36110. }
  36111. /*
  36112. * Retry with several GC attempts. Initial attempts are made without
  36113. * emergency mode; later attempts use emergency mode which minimizes
  36114. * memory allocations forcibly.
  36115. */
  36116. for (i = 0; i < DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT; i++) {
  36117. duk_small_uint_t flags;
  36118. flags = 0;
  36119. if (i >= DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT - 1) {
  36120. flags |= DUK_MS_FLAG_EMERGENCY;
  36121. }
  36122. rc = duk_heap_mark_and_sweep(heap, flags);
  36123. DUK_UNREF(rc);
  36124. res = heap->realloc_func(heap->heap_udata, ptr, newsize);
  36125. if (res || newsize == 0) {
  36126. DUK_D(DUK_DPRINT("duk_heap_mem_realloc() succeeded after gc (pass %ld), alloc size %ld",
  36127. (long) (i + 1), (long) newsize));
  36128. return res;
  36129. }
  36130. }
  36131. DUK_D(DUK_DPRINT("duk_heap_mem_realloc() failed even after gc, alloc size %ld", (long) newsize));
  36132. return NULL;
  36133. }
  36134. #else /* DUK_USE_MARK_AND_SWEEP */
  36135. /* saves a few instructions to have this wrapper (see comment on duk_heap_mem_alloc) */
  36136. DUK_INTERNAL void *duk_heap_mem_realloc(duk_heap *heap, void *ptr, duk_size_t newsize) {
  36137. DUK_ASSERT(heap != NULL);
  36138. /* ptr may be NULL */
  36139. DUK_ASSERT_DISABLE(newsize >= 0);
  36140. return heap->realloc_func(heap->heap_udata, ptr, newsize);
  36141. }
  36142. #endif /* DUK_USE_MARK_AND_SWEEP */
  36143. /*
  36144. * Reallocate memory with garbage collection, using a callback to provide
  36145. * the current allocated pointer. This variant is used when a mark-and-sweep
  36146. * (e.g. finalizers) might change the original pointer.
  36147. */
  36148. #ifdef DUK_USE_MARK_AND_SWEEP
  36149. DUK_INTERNAL void *duk_heap_mem_realloc_indirect(duk_heap *heap, duk_mem_getptr cb, void *ud, duk_size_t newsize) {
  36150. void *res;
  36151. duk_bool_t rc;
  36152. duk_small_int_t i;
  36153. DUK_ASSERT(heap != NULL);
  36154. DUK_ASSERT_DISABLE(newsize >= 0);
  36155. /*
  36156. * Voluntary periodic GC (if enabled)
  36157. */
  36158. DUK__VOLUNTARY_PERIODIC_GC(heap);
  36159. /*
  36160. * First attempt
  36161. */
  36162. #ifdef DUK_USE_GC_TORTURE
  36163. /* simulate alloc failure on every realloc (except when mark-and-sweep is running) */
  36164. if (!DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)) {
  36165. DUK_DDD(DUK_DDDPRINT("gc torture enabled, pretend that first indirect realloc attempt fails"));
  36166. res = NULL;
  36167. DUK_UNREF(res);
  36168. goto skip_attempt;
  36169. }
  36170. #endif
  36171. res = heap->realloc_func(heap->heap_udata, cb(heap, ud), newsize);
  36172. if (res || newsize == 0) {
  36173. /* for zero size allocations NULL is allowed */
  36174. return res;
  36175. }
  36176. #ifdef DUK_USE_GC_TORTURE
  36177. skip_attempt:
  36178. #endif
  36179. DUK_D(DUK_DPRINT("first indirect realloc attempt failed, attempt to gc and retry"));
  36180. /*
  36181. * Avoid a GC if GC is already running. See duk_heap_mem_alloc().
  36182. */
  36183. if (DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)) {
  36184. DUK_D(DUK_DPRINT("duk_heap_mem_realloc_indirect() failed, gc in progress (gc skipped), alloc size %ld", (long) newsize));
  36185. return NULL;
  36186. }
  36187. /*
  36188. * Retry with several GC attempts. Initial attempts are made without
  36189. * emergency mode; later attempts use emergency mode which minimizes
  36190. * memory allocations forcibly.
  36191. */
  36192. for (i = 0; i < DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT; i++) {
  36193. duk_small_uint_t flags;
  36194. #ifdef DUK_USE_ASSERTIONS
  36195. void *ptr_pre; /* ptr before mark-and-sweep */
  36196. void *ptr_post;
  36197. #endif
  36198. #ifdef DUK_USE_ASSERTIONS
  36199. ptr_pre = cb(heap, ud);
  36200. #endif
  36201. flags = 0;
  36202. if (i >= DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT - 1) {
  36203. flags |= DUK_MS_FLAG_EMERGENCY;
  36204. }
  36205. rc = duk_heap_mark_and_sweep(heap, flags);
  36206. DUK_UNREF(rc);
  36207. #ifdef DUK_USE_ASSERTIONS
  36208. ptr_post = cb(heap, ud);
  36209. if (ptr_pre != ptr_post) {
  36210. /* useful for debugging */
  36211. DUK_DD(DUK_DDPRINT("note: base pointer changed by mark-and-sweep: %p -> %p",
  36212. (void *) ptr_pre, (void *) ptr_post));
  36213. }
  36214. #endif
  36215. /* Note: key issue here is to re-lookup the base pointer on every attempt.
  36216. * The pointer being reallocated may change after every mark-and-sweep.
  36217. */
  36218. res = heap->realloc_func(heap->heap_udata, cb(heap, ud), newsize);
  36219. if (res || newsize == 0) {
  36220. DUK_D(DUK_DPRINT("duk_heap_mem_realloc_indirect() succeeded after gc (pass %ld), alloc size %ld",
  36221. (long) (i + 1), (long) newsize));
  36222. return res;
  36223. }
  36224. }
  36225. DUK_D(DUK_DPRINT("duk_heap_mem_realloc_indirect() failed even after gc, alloc size %ld", (long) newsize));
  36226. return NULL;
  36227. }
  36228. #else /* DUK_USE_MARK_AND_SWEEP */
  36229. /* saves a few instructions to have this wrapper (see comment on duk_heap_mem_alloc) */
  36230. DUK_INTERNAL void *duk_heap_mem_realloc_indirect(duk_heap *heap, duk_mem_getptr cb, void *ud, duk_size_t newsize) {
  36231. return heap->realloc_func(heap->heap_udata, cb(heap, ud), newsize);
  36232. }
  36233. #endif /* DUK_USE_MARK_AND_SWEEP */
  36234. /*
  36235. * Free memory
  36236. */
  36237. #ifdef DUK_USE_MARK_AND_SWEEP
  36238. DUK_INTERNAL void duk_heap_mem_free(duk_heap *heap, void *ptr) {
  36239. DUK_ASSERT(heap != NULL);
  36240. /* ptr may be NULL */
  36241. /* Must behave like a no-op with NULL and any pointer returned from
  36242. * malloc/realloc with zero size.
  36243. */
  36244. heap->free_func(heap->heap_udata, ptr);
  36245. /* Count free operations toward triggering a GC but never actually trigger
  36246. * a GC from a free. Otherwise code which frees internal structures would
  36247. * need to put in NULLs at every turn to ensure the object is always in
  36248. * consistent state for a mark-and-sweep.
  36249. */
  36250. #ifdef DUK_USE_VOLUNTARY_GC
  36251. heap->mark_and_sweep_trigger_counter--;
  36252. #endif
  36253. }
  36254. #else
  36255. /* saves a few instructions to have this wrapper (see comment on duk_heap_mem_alloc) */
  36256. DUK_INTERNAL void duk_heap_mem_free(duk_heap *heap, void *ptr) {
  36257. DUK_ASSERT(heap != NULL);
  36258. /* ptr may be NULL */
  36259. /* Note: must behave like a no-op with NULL and any pointer
  36260. * returned from malloc/realloc with zero size.
  36261. */
  36262. heap->free_func(heap->heap_udata, ptr);
  36263. }
  36264. #endif
  36265. #line 1 "duk_heap_misc.c"
  36266. /*
  36267. * Support functions for duk_heap.
  36268. */
  36269. /* include removed: duk_internal.h */
  36270. #if defined(DUK_USE_DOUBLE_LINKED_HEAP) && defined(DUK_USE_REFERENCE_COUNTING)
  36271. /* arbitrary remove only works with double linked heap, and is only required by
  36272. * reference counting so far.
  36273. */
  36274. DUK_INTERNAL void duk_heap_remove_any_from_heap_allocated(duk_heap *heap, duk_heaphdr *hdr) {
  36275. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(hdr) != DUK_HTYPE_STRING);
  36276. if (DUK_HEAPHDR_GET_PREV(heap, hdr)) {
  36277. DUK_HEAPHDR_SET_NEXT(heap, DUK_HEAPHDR_GET_PREV(heap, hdr), DUK_HEAPHDR_GET_NEXT(heap, hdr));
  36278. } else {
  36279. heap->heap_allocated = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  36280. }
  36281. if (DUK_HEAPHDR_GET_NEXT(heap, hdr)) {
  36282. DUK_HEAPHDR_SET_PREV(heap, DUK_HEAPHDR_GET_NEXT(heap, hdr), DUK_HEAPHDR_GET_PREV(heap, hdr));
  36283. } else {
  36284. ;
  36285. }
  36286. }
  36287. #endif
  36288. DUK_INTERNAL void duk_heap_insert_into_heap_allocated(duk_heap *heap, duk_heaphdr *hdr) {
  36289. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(hdr) != DUK_HTYPE_STRING);
  36290. #ifdef DUK_USE_DOUBLE_LINKED_HEAP
  36291. if (heap->heap_allocated) {
  36292. DUK_ASSERT(DUK_HEAPHDR_GET_PREV(heap, heap->heap_allocated) == NULL);
  36293. DUK_HEAPHDR_SET_PREV(heap, heap->heap_allocated, hdr);
  36294. }
  36295. DUK_HEAPHDR_SET_PREV(heap, hdr, NULL);
  36296. #endif
  36297. DUK_HEAPHDR_SET_NEXT(heap, hdr, heap->heap_allocated);
  36298. heap->heap_allocated = hdr;
  36299. }
  36300. #ifdef DUK_USE_INTERRUPT_COUNTER
  36301. DUK_INTERNAL void duk_heap_switch_thread(duk_heap *heap, duk_hthread *new_thr) {
  36302. /* Copy currently active interrupt counter from the active thread
  36303. * back to the heap structure. It doesn't need to be copied to
  36304. * the target thread, as the bytecode executor does that when it
  36305. * resumes execution for a new thread.
  36306. */
  36307. if (heap->curr_thread != NULL) {
  36308. heap->interrupt_counter = heap->curr_thread->interrupt_counter;
  36309. }
  36310. heap->curr_thread = new_thr; /* may be NULL */
  36311. }
  36312. #endif /* DUK_USE_INTERRUPT_COUNTER */
  36313. #line 1 "duk_heap_refcount.c"
  36314. /*
  36315. * Reference counting implementation.
  36316. */
  36317. /* include removed: duk_internal.h */
  36318. #ifdef DUK_USE_REFERENCE_COUNTING
  36319. #ifndef DUK_USE_DOUBLE_LINKED_HEAP
  36320. #error internal error, reference counting requires a double linked heap
  36321. #endif
  36322. /*
  36323. * Misc
  36324. */
  36325. DUK_LOCAL void duk__queue_refzero(duk_heap *heap, duk_heaphdr *hdr) {
  36326. /* tail insert: don't disturb head in case refzero is running */
  36327. if (heap->refzero_list != NULL) {
  36328. duk_heaphdr *hdr_prev;
  36329. hdr_prev = heap->refzero_list_tail;
  36330. DUK_ASSERT(hdr_prev != NULL);
  36331. DUK_ASSERT(DUK_HEAPHDR_GET_NEXT(heap, hdr_prev) == NULL);
  36332. DUK_HEAPHDR_SET_NEXT(heap, hdr, NULL);
  36333. DUK_HEAPHDR_SET_PREV(heap, hdr, hdr_prev);
  36334. DUK_HEAPHDR_SET_NEXT(heap, hdr_prev, hdr);
  36335. heap->refzero_list_tail = hdr;
  36336. } else {
  36337. DUK_ASSERT(heap->refzero_list_tail == NULL);
  36338. DUK_HEAPHDR_SET_NEXT(heap, hdr, NULL);
  36339. DUK_HEAPHDR_SET_PREV(heap, hdr, NULL);
  36340. heap->refzero_list = hdr;
  36341. heap->refzero_list_tail = hdr;
  36342. }
  36343. }
  36344. /*
  36345. * Heap object refcount finalization.
  36346. *
  36347. * When an object is about to be freed, all other objects it refers to must
  36348. * be decref'd. Refcount finalization does NOT free the object or its inner
  36349. * allocations (mark-and-sweep shares these helpers), it just manipulates
  36350. * the refcounts.
  36351. *
  36352. * Note that any of the decref's may cause a refcount to drop to zero, BUT
  36353. * it will not be processed inline; instead, because refzero is already
  36354. * running, the objects will just be queued to refzero list and processed
  36355. * later. This eliminates C recursion.
  36356. */
  36357. DUK_LOCAL void duk__refcount_finalize_hobject(duk_hthread *thr, duk_hobject *h) {
  36358. duk_uint_fast32_t i;
  36359. DUK_ASSERT(h);
  36360. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) h) == DUK_HTYPE_OBJECT);
  36361. /* XXX: better to get base and walk forwards? */
  36362. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(h); i++) {
  36363. duk_hstring *key = DUK_HOBJECT_E_GET_KEY(thr->heap, h, i);
  36364. if (!key) {
  36365. continue;
  36366. }
  36367. duk_heaphdr_decref(thr, (duk_heaphdr *) key);
  36368. if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, h, i)) {
  36369. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, h, i));
  36370. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, h, i));
  36371. } else {
  36372. duk_tval_decref(thr, DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, h, i));
  36373. }
  36374. }
  36375. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(h); i++) {
  36376. duk_tval_decref(thr, DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, h, i));
  36377. }
  36378. /* hash part is a 'weak reference' and does not contribute */
  36379. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h));
  36380. if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h)) {
  36381. duk_hcompiledfunction *f = (duk_hcompiledfunction *) h;
  36382. duk_tval *tv, *tv_end;
  36383. duk_hobject **funcs, **funcs_end;
  36384. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, f) != NULL); /* compiled functions must be created 'atomically' */
  36385. tv = DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, f);
  36386. tv_end = DUK_HCOMPILEDFUNCTION_GET_CONSTS_END(thr->heap, f);
  36387. while (tv < tv_end) {
  36388. duk_tval_decref(thr, tv);
  36389. tv++;
  36390. }
  36391. funcs = DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, f);
  36392. funcs_end = DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(thr->heap, f);
  36393. while (funcs < funcs_end) {
  36394. duk_heaphdr_decref(thr, (duk_heaphdr *) *funcs);
  36395. funcs++;
  36396. }
  36397. duk_heaphdr_decref(thr, (duk_heaphdr *) DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, f));
  36398. } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(h)) {
  36399. duk_hnativefunction *f = (duk_hnativefunction *) h;
  36400. DUK_UNREF(f);
  36401. /* nothing to finalize */
  36402. } else if (DUK_HOBJECT_IS_BUFFEROBJECT(h)) {
  36403. duk_hbufferobject *b = (duk_hbufferobject *) h;
  36404. if (b->buf) {
  36405. duk_heaphdr_decref(thr, (duk_heaphdr *) b->buf);
  36406. }
  36407. } else if (DUK_HOBJECT_IS_THREAD(h)) {
  36408. duk_hthread *t = (duk_hthread *) h;
  36409. duk_tval *tv;
  36410. tv = t->valstack;
  36411. while (tv < t->valstack_end) {
  36412. duk_tval_decref(thr, tv);
  36413. tv++;
  36414. }
  36415. for (i = 0; i < (duk_uint_fast32_t) t->callstack_top; i++) {
  36416. duk_activation *act = t->callstack + i;
  36417. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) DUK_ACT_GET_FUNC(act));
  36418. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) act->var_env);
  36419. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) act->lex_env);
  36420. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  36421. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) act->prev_caller);
  36422. #endif
  36423. }
  36424. #if 0 /* nothing now */
  36425. for (i = 0; i < (duk_uint_fast32_t) t->catchstack_top; i++) {
  36426. duk_catcher *cat = t->catchstack + i;
  36427. }
  36428. #endif
  36429. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  36430. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) t->builtins[i]);
  36431. }
  36432. duk_heaphdr_decref_allownull(thr, (duk_heaphdr *) t->resumer);
  36433. }
  36434. }
  36435. DUK_INTERNAL void duk_heaphdr_refcount_finalize(duk_hthread *thr, duk_heaphdr *hdr) {
  36436. DUK_ASSERT(hdr);
  36437. switch ((int) DUK_HEAPHDR_GET_TYPE(hdr)) {
  36438. case DUK_HTYPE_OBJECT:
  36439. duk__refcount_finalize_hobject(thr, (duk_hobject *) hdr);
  36440. break;
  36441. case DUK_HTYPE_BUFFER:
  36442. /* nothing to finalize */
  36443. break;
  36444. case DUK_HTYPE_STRING:
  36445. /* cannot happen: strings are not put into refzero list (they don't even have the next/prev pointers) */
  36446. default:
  36447. DUK_UNREACHABLE();
  36448. }
  36449. }
  36450. /*
  36451. * Refcount memory freeing loop.
  36452. *
  36453. * Frees objects in the refzero_pending list until the list becomes
  36454. * empty. When an object is freed, its references get decref'd and
  36455. * may cause further objects to be queued for freeing.
  36456. *
  36457. * This could be expanded to allow incremental freeing: just bail out
  36458. * early and resume at a future alloc/decref/refzero.
  36459. */
  36460. DUK_LOCAL void duk__refzero_free_pending(duk_hthread *thr) {
  36461. duk_heaphdr *h1, *h2;
  36462. duk_heap *heap;
  36463. duk_int_t count = 0;
  36464. DUK_ASSERT(thr != NULL);
  36465. DUK_ASSERT(thr->heap != NULL);
  36466. heap = thr->heap;
  36467. DUK_ASSERT(heap != NULL);
  36468. /*
  36469. * Detect recursive invocation
  36470. */
  36471. if (DUK_HEAP_HAS_REFZERO_FREE_RUNNING(heap)) {
  36472. DUK_DDD(DUK_DDDPRINT("refzero free running, skip run"));
  36473. return;
  36474. }
  36475. /*
  36476. * Churn refzero_list until empty
  36477. */
  36478. DUK_HEAP_SET_REFZERO_FREE_RUNNING(heap);
  36479. while (heap->refzero_list) {
  36480. duk_hobject *obj;
  36481. duk_bool_t rescued = 0;
  36482. /*
  36483. * Pick an object from the head (don't remove yet).
  36484. */
  36485. h1 = heap->refzero_list;
  36486. obj = (duk_hobject *) h1;
  36487. DUK_DD(DUK_DDPRINT("refzero processing %p: %!O", (void *) h1, (duk_heaphdr *) h1));
  36488. DUK_ASSERT(DUK_HEAPHDR_GET_PREV(heap, h1) == NULL);
  36489. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(h1) == DUK_HTYPE_OBJECT); /* currently, always the case */
  36490. /*
  36491. * Finalizer check.
  36492. *
  36493. * Note: running a finalizer may have arbitrary side effects, e.g.
  36494. * queue more objects on refzero_list (tail), or even trigger a
  36495. * mark-and-sweep.
  36496. *
  36497. * Note: quick reject check should match vast majority of
  36498. * objects and must be safe (not throw any errors, ever).
  36499. */
  36500. /* XXX: If object has FINALIZED, it was finalized by mark-and-sweep on
  36501. * its previous run. Any point in running finalizer again here? If
  36502. * finalization semantics is changed so that finalizer is only run once,
  36503. * checking for FINALIZED would happen here.
  36504. */
  36505. /* A finalizer is looked up from the object and up its prototype chain
  36506. * (which allows inherited finalizers).
  36507. */
  36508. if (duk_hobject_hasprop_raw(thr, obj, DUK_HTHREAD_STRING_INT_FINALIZER(thr))) {
  36509. DUK_DDD(DUK_DDDPRINT("object has a finalizer, run it"));
  36510. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(h1) == 0);
  36511. DUK_HEAPHDR_PREINC_REFCOUNT(h1); /* bump refcount to prevent refzero during finalizer processing */
  36512. duk_hobject_run_finalizer(thr, obj); /* must never longjmp */
  36513. DUK_HEAPHDR_PREDEC_REFCOUNT(h1); /* remove artificial bump */
  36514. DUK_ASSERT_DISABLE(h1->h_refcount >= 0); /* refcount is unsigned, so always true */
  36515. if (DUK_HEAPHDR_GET_REFCOUNT(h1) != 0) {
  36516. DUK_DDD(DUK_DDDPRINT("-> object refcount after finalization non-zero, object will be rescued"));
  36517. rescued = 1;
  36518. } else {
  36519. DUK_DDD(DUK_DDDPRINT("-> object refcount still zero after finalization, object will be freed"));
  36520. }
  36521. }
  36522. /* Refzero head is still the same. This is the case even if finalizer
  36523. * inserted more refzero objects; they are inserted to the tail.
  36524. */
  36525. DUK_ASSERT(h1 == heap->refzero_list);
  36526. /*
  36527. * Remove the object from the refzero list. This cannot be done
  36528. * before a possible finalizer has been executed; the finalizer
  36529. * may trigger a mark-and-sweep, and mark-and-sweep must be able
  36530. * to traverse a complete refzero_list.
  36531. */
  36532. h2 = DUK_HEAPHDR_GET_NEXT(heap, h1);
  36533. if (h2) {
  36534. DUK_HEAPHDR_SET_PREV(heap, h2, NULL); /* not strictly necessary */
  36535. heap->refzero_list = h2;
  36536. } else {
  36537. heap->refzero_list = NULL;
  36538. heap->refzero_list_tail = NULL;
  36539. }
  36540. /*
  36541. * Rescue or free.
  36542. */
  36543. if (rescued) {
  36544. /* yes -> move back to heap allocated */
  36545. DUK_DD(DUK_DDPRINT("object rescued during refcount finalization: %p", (void *) h1));
  36546. DUK_HEAPHDR_SET_PREV(heap, h1, NULL);
  36547. DUK_HEAPHDR_SET_NEXT(heap, h1, heap->heap_allocated);
  36548. heap->heap_allocated = h1;
  36549. } else {
  36550. /* no -> decref members, then free */
  36551. duk__refcount_finalize_hobject(thr, obj);
  36552. duk_heap_free_heaphdr_raw(heap, h1);
  36553. }
  36554. count++;
  36555. }
  36556. DUK_HEAP_CLEAR_REFZERO_FREE_RUNNING(heap);
  36557. DUK_DDD(DUK_DDDPRINT("refzero processed %ld objects", (long) count));
  36558. /*
  36559. * Once the whole refzero cascade has been freed, check for
  36560. * a voluntary mark-and-sweep.
  36561. */
  36562. #if defined(DUK_USE_MARK_AND_SWEEP) && defined(DUK_USE_VOLUNTARY_GC)
  36563. /* 'count' is more or less comparable to normal trigger counter update
  36564. * which happens in memory block (re)allocation.
  36565. */
  36566. heap->mark_and_sweep_trigger_counter -= count;
  36567. if (heap->mark_and_sweep_trigger_counter <= 0) {
  36568. duk_bool_t rc;
  36569. duk_small_uint_t flags = 0; /* not emergency */
  36570. DUK_D(DUK_DPRINT("refcount triggering mark-and-sweep"));
  36571. rc = duk_heap_mark_and_sweep(heap, flags);
  36572. DUK_UNREF(rc);
  36573. DUK_D(DUK_DPRINT("refcount triggered mark-and-sweep => rc %ld", (long) rc));
  36574. }
  36575. #endif /* DUK_USE_MARK_AND_SWEEP && DUK_USE_VOLUNTARY_GC */
  36576. }
  36577. /*
  36578. * Incref and decref functions.
  36579. *
  36580. * Decref may trigger immediate refzero handling, which may free and finalize
  36581. * an arbitrary number of objects.
  36582. *
  36583. */
  36584. DUK_INTERNAL void duk_heaphdr_refzero(duk_hthread *thr, duk_heaphdr *h) {
  36585. duk_heap *heap;
  36586. DUK_ASSERT(thr != NULL);
  36587. DUK_ASSERT(h != NULL);
  36588. heap = thr->heap;
  36589. DUK_DDD(DUK_DDDPRINT("refzero %p: %!O", (void *) h, (duk_heaphdr *) h));
  36590. #ifdef DUK_USE_MARK_AND_SWEEP
  36591. /*
  36592. * If mark-and-sweep is running, don't process 'refzero' situations at all.
  36593. * They may happen because mark-and-sweep needs to finalize refcounts for
  36594. * each object it sweeps. Otherwise the target objects of swept objects
  36595. * would have incorrect refcounts.
  36596. *
  36597. * Note: mark-and-sweep could use a separate decref handler to avoid coming
  36598. * here at all. However, mark-and-sweep may also call finalizers, which
  36599. * can do arbitrary operations and would use this decref variant anyway.
  36600. */
  36601. if (DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap)) {
  36602. DUK_DDD(DUK_DDDPRINT("refzero handling suppressed when mark-and-sweep running, object: %p", (void *) h));
  36603. return;
  36604. }
  36605. #endif
  36606. switch ((duk_small_int_t) DUK_HEAPHDR_GET_TYPE(h)) {
  36607. case DUK_HTYPE_STRING:
  36608. /*
  36609. * Strings have no internal references but do have "weak"
  36610. * references in the string cache. Also note that strings
  36611. * are not on the heap_allocated list like other heap
  36612. * elements.
  36613. */
  36614. duk_heap_strcache_string_remove(heap, (duk_hstring *) h);
  36615. duk_heap_string_remove(heap, (duk_hstring *) h);
  36616. duk_heap_free_heaphdr_raw(heap, h);
  36617. break;
  36618. case DUK_HTYPE_OBJECT:
  36619. /*
  36620. * Objects have internal references. Must finalize through
  36621. * the "refzero" work list.
  36622. */
  36623. duk_heap_remove_any_from_heap_allocated(heap, h);
  36624. duk__queue_refzero(heap, h);
  36625. duk__refzero_free_pending(thr);
  36626. break;
  36627. case DUK_HTYPE_BUFFER:
  36628. /*
  36629. * Buffers have no internal references. However, a dynamic
  36630. * buffer has a separate allocation for the buffer. This is
  36631. * freed by duk_heap_free_heaphdr_raw().
  36632. */
  36633. duk_heap_remove_any_from_heap_allocated(heap, h);
  36634. duk_heap_free_heaphdr_raw(heap, h);
  36635. break;
  36636. default:
  36637. DUK_D(DUK_DPRINT("invalid heap type in decref: %ld", (long) DUK_HEAPHDR_GET_TYPE(h)));
  36638. DUK_UNREACHABLE();
  36639. }
  36640. }
  36641. #if !defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  36642. DUK_INTERNAL void duk_tval_incref(duk_tval *tv) {
  36643. DUK_ASSERT(tv != NULL);
  36644. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  36645. duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
  36646. DUK_ASSERT(h != NULL);
  36647. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  36648. DUK_ASSERT_DISABLE(h->h_refcount >= 0);
  36649. DUK_HEAPHDR_PREINC_REFCOUNT(h);
  36650. }
  36651. }
  36652. #endif
  36653. #if 0 /* unused */
  36654. DUK_INTERNAL void duk_tval_incref_allownull(duk_tval *tv) {
  36655. if (tv == NULL) {
  36656. return;
  36657. }
  36658. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  36659. duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
  36660. DUK_ASSERT(h != NULL);
  36661. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  36662. DUK_ASSERT_DISABLE(h->h_refcount >= 0);
  36663. DUK_HEAPHDR_PREINC_REFCOUNT(h);
  36664. }
  36665. }
  36666. #endif
  36667. DUK_INTERNAL void duk_tval_decref(duk_hthread *thr, duk_tval *tv) {
  36668. DUK_ASSERT(thr != NULL);
  36669. DUK_ASSERT(tv != NULL);
  36670. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  36671. duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
  36672. DUK_ASSERT(h != NULL);
  36673. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  36674. duk_heaphdr_decref(thr, h);
  36675. }
  36676. }
  36677. #if 0 /* unused */
  36678. DUK_INTERNAL void duk_tval_decref_allownull(duk_hthread *thr, duk_tval *tv) {
  36679. DUK_ASSERT(thr != NULL);
  36680. if (tv == NULL) {
  36681. return;
  36682. }
  36683. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  36684. duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
  36685. DUK_ASSERT(h != NULL);
  36686. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  36687. duk_heaphdr_decref(thr, h);
  36688. }
  36689. }
  36690. #endif
  36691. #if !defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  36692. DUK_INTERNAL void duk_heaphdr_incref(duk_heaphdr *h) {
  36693. DUK_ASSERT(h != NULL);
  36694. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  36695. DUK_ASSERT_DISABLE(DUK_HEAPHDR_GET_REFCOUNT(h) >= 0);
  36696. DUK_HEAPHDR_PREINC_REFCOUNT(h);
  36697. }
  36698. #endif
  36699. #if 0 /* unused */
  36700. DUK_INTERNAL void duk_heaphdr_incref_allownull(duk_heaphdr *h) {
  36701. if (h == NULL) {
  36702. return;
  36703. }
  36704. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  36705. DUK_ASSERT_DISABLE(DUK_HEAPHDR_GET_REFCOUNT(h) >= 0);
  36706. DUK_HEAPHDR_PREINC_REFCOUNT(h);
  36707. }
  36708. #endif
  36709. DUK_INTERNAL void duk_heaphdr_decref(duk_hthread *thr, duk_heaphdr *h) {
  36710. DUK_ASSERT(thr != NULL);
  36711. DUK_ASSERT(thr->heap != NULL);
  36712. DUK_ASSERT(h != NULL);
  36713. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  36714. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(h) >= 1);
  36715. if (DUK_HEAPHDR_PREDEC_REFCOUNT(h) != 0) {
  36716. return;
  36717. }
  36718. duk_heaphdr_refzero(thr, h);
  36719. }
  36720. DUK_INTERNAL void duk_heaphdr_decref_allownull(duk_hthread *thr, duk_heaphdr *h) {
  36721. DUK_ASSERT(thr != NULL);
  36722. DUK_ASSERT(thr->heap != NULL);
  36723. if (h == NULL) {
  36724. return;
  36725. }
  36726. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  36727. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(h) >= 1);
  36728. if (DUK_HEAPHDR_PREDEC_REFCOUNT(h) != 0) {
  36729. return;
  36730. }
  36731. duk_heaphdr_refzero(thr, h);
  36732. }
  36733. #else
  36734. /* no refcounting */
  36735. #endif /* DUK_USE_REFERENCE_COUNTING */
  36736. #line 1 "duk_heap_stringcache.c"
  36737. /*
  36738. * String cache.
  36739. *
  36740. * Provides a cache to optimize indexed string lookups. The cache keeps
  36741. * track of (byte offset, char offset) states for a fixed number of strings.
  36742. * Otherwise we'd need to scan from either end of the string, as we store
  36743. * strings in (extended) UTF-8.
  36744. */
  36745. /* include removed: duk_internal.h */
  36746. /*
  36747. * Delete references to given hstring from the heap string cache.
  36748. *
  36749. * String cache references are 'weak': they are not counted towards
  36750. * reference counts, nor serve as roots for mark-and-sweep. When an
  36751. * object is about to be freed, such references need to be removed.
  36752. */
  36753. DUK_INTERNAL void duk_heap_strcache_string_remove(duk_heap *heap, duk_hstring *h) {
  36754. duk_small_int_t i;
  36755. for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
  36756. duk_strcache *c = heap->strcache + i;
  36757. if (c->h == h) {
  36758. DUK_DD(DUK_DDPRINT("deleting weak strcache reference to hstring %p from heap %p",
  36759. (void *) h, (void *) heap));
  36760. c->h = NULL;
  36761. /* XXX: the string shouldn't appear twice, but we now loop to the
  36762. * end anyway; if fixed, add a looping assertion to ensure there
  36763. * is no duplicate.
  36764. */
  36765. }
  36766. }
  36767. }
  36768. /*
  36769. * String scanning helpers
  36770. */
  36771. DUK_LOCAL duk_uint8_t *duk__scan_forwards(duk_uint8_t *p, duk_uint8_t *q, duk_uint_fast32_t n) {
  36772. while (n > 0) {
  36773. for (;;) {
  36774. p++;
  36775. if (p >= q) {
  36776. return NULL;
  36777. }
  36778. if ((*p & 0xc0) != 0x80) {
  36779. break;
  36780. }
  36781. }
  36782. n--;
  36783. }
  36784. return p;
  36785. }
  36786. DUK_LOCAL duk_uint8_t *duk__scan_backwards(duk_uint8_t *p, duk_uint8_t *q, duk_uint_fast32_t n) {
  36787. while (n > 0) {
  36788. for (;;) {
  36789. p--;
  36790. if (p < q) {
  36791. return NULL;
  36792. }
  36793. if ((*p & 0xc0) != 0x80) {
  36794. break;
  36795. }
  36796. }
  36797. n--;
  36798. }
  36799. return p;
  36800. }
  36801. /*
  36802. * Convert char offset to byte offset
  36803. *
  36804. * Avoid using the string cache if possible: for ASCII strings byte and
  36805. * char offsets are equal and for short strings direct scanning may be
  36806. * better than using the string cache (which may evict a more important
  36807. * entry).
  36808. *
  36809. * Typing now assumes 32-bit string byte/char offsets (duk_uint_fast32_t).
  36810. * Better typing might be to use duk_size_t.
  36811. */
  36812. DUK_INTERNAL duk_uint_fast32_t duk_heap_strcache_offset_char2byte(duk_hthread *thr, duk_hstring *h, duk_uint_fast32_t char_offset) {
  36813. duk_heap *heap;
  36814. duk_strcache *sce;
  36815. duk_uint_fast32_t byte_offset;
  36816. duk_small_int_t i;
  36817. duk_bool_t use_cache;
  36818. duk_uint_fast32_t dist_start, dist_end, dist_sce;
  36819. duk_uint8_t *p_start;
  36820. duk_uint8_t *p_end;
  36821. duk_uint8_t *p_found;
  36822. if (char_offset > DUK_HSTRING_GET_CHARLEN(h)) {
  36823. goto error;
  36824. }
  36825. /*
  36826. * For ASCII strings, the answer is simple.
  36827. */
  36828. if (DUK_HSTRING_IS_ASCII(h)) {
  36829. /* clen == blen -> pure ascii */
  36830. return char_offset;
  36831. }
  36832. /*
  36833. * For non-ASCII strings, we need to scan forwards or backwards
  36834. * from some starting point. The starting point may be the start
  36835. * or end of the string, or some cached midpoint in the string
  36836. * cache.
  36837. *
  36838. * For "short" strings we simply scan without checking or updating
  36839. * the cache. For longer strings we check and update the cache as
  36840. * necessary, inserting a new cache entry if none exists.
  36841. */
  36842. DUK_DDD(DUK_DDDPRINT("non-ascii string %p, char_offset=%ld, clen=%ld, blen=%ld",
  36843. (void *) h, (long) char_offset,
  36844. (long) DUK_HSTRING_GET_CHARLEN(h),
  36845. (long) DUK_HSTRING_GET_BYTELEN(h)));
  36846. heap = thr->heap;
  36847. sce = NULL;
  36848. use_cache = (DUK_HSTRING_GET_CHARLEN(h) > DUK_HEAP_STRINGCACHE_NOCACHE_LIMIT);
  36849. if (use_cache) {
  36850. #ifdef DUK_USE_DDDPRINT
  36851. DUK_DDD(DUK_DDDPRINT("stringcache before char2byte (using cache):"));
  36852. for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
  36853. duk_strcache *c = heap->strcache + i;
  36854. DUK_DDD(DUK_DDDPRINT(" [%ld] -> h=%p, cidx=%ld, bidx=%ld",
  36855. (long) i, (void *) c->h, (long) c->cidx, (long) c->bidx));
  36856. }
  36857. #endif
  36858. for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
  36859. duk_strcache *c = heap->strcache + i;
  36860. if (c->h == h) {
  36861. sce = c;
  36862. break;
  36863. }
  36864. }
  36865. }
  36866. /*
  36867. * Scan from shortest distance:
  36868. * - start of string
  36869. * - end of string
  36870. * - cache entry (if exists)
  36871. */
  36872. DUK_ASSERT(DUK_HSTRING_GET_CHARLEN(h) >= char_offset);
  36873. dist_start = char_offset;
  36874. dist_end = DUK_HSTRING_GET_CHARLEN(h) - char_offset;
  36875. dist_sce = 0; DUK_UNREF(dist_sce); /* initialize for debug prints, needed if sce==NULL */
  36876. p_start = (duk_uint8_t *) DUK_HSTRING_GET_DATA(h);
  36877. p_end = (duk_uint8_t *) (p_start + DUK_HSTRING_GET_BYTELEN(h));
  36878. p_found = NULL;
  36879. if (sce) {
  36880. if (char_offset >= sce->cidx) {
  36881. dist_sce = char_offset - sce->cidx;
  36882. if ((dist_sce <= dist_start) && (dist_sce <= dist_end)) {
  36883. DUK_DDD(DUK_DDDPRINT("non-ascii string, use_cache=%ld, sce=%p:%ld:%ld, "
  36884. "dist_start=%ld, dist_end=%ld, dist_sce=%ld => "
  36885. "scan forwards from sce",
  36886. (long) use_cache, (void *) (sce ? sce->h : NULL),
  36887. (sce ? (long) sce->cidx : (long) -1),
  36888. (sce ? (long) sce->bidx : (long) -1),
  36889. (long) dist_start, (long) dist_end, (long) dist_sce));
  36890. p_found = duk__scan_forwards(p_start + sce->bidx,
  36891. p_end,
  36892. dist_sce);
  36893. goto scan_done;
  36894. }
  36895. } else {
  36896. dist_sce = sce->cidx - char_offset;
  36897. if ((dist_sce <= dist_start) && (dist_sce <= dist_end)) {
  36898. DUK_DDD(DUK_DDDPRINT("non-ascii string, use_cache=%ld, sce=%p:%ld:%ld, "
  36899. "dist_start=%ld, dist_end=%ld, dist_sce=%ld => "
  36900. "scan backwards from sce",
  36901. (long) use_cache, (void *) (sce ? sce->h : NULL),
  36902. (sce ? (long) sce->cidx : (long) -1),
  36903. (sce ? (long) sce->bidx : (long) -1),
  36904. (long) dist_start, (long) dist_end, (long) dist_sce));
  36905. p_found = duk__scan_backwards(p_start + sce->bidx,
  36906. p_start,
  36907. dist_sce);
  36908. goto scan_done;
  36909. }
  36910. }
  36911. }
  36912. /* no sce, or sce scan not best */
  36913. if (dist_start <= dist_end) {
  36914. DUK_DDD(DUK_DDDPRINT("non-ascii string, use_cache=%ld, sce=%p:%ld:%ld, "
  36915. "dist_start=%ld, dist_end=%ld, dist_sce=%ld => "
  36916. "scan forwards from string start",
  36917. (long) use_cache, (void *) (sce ? sce->h : NULL),
  36918. (sce ? (long) sce->cidx : (long) -1),
  36919. (sce ? (long) sce->bidx : (long) -1),
  36920. (long) dist_start, (long) dist_end, (long) dist_sce));
  36921. p_found = duk__scan_forwards(p_start,
  36922. p_end,
  36923. dist_start);
  36924. } else {
  36925. DUK_DDD(DUK_DDDPRINT("non-ascii string, use_cache=%ld, sce=%p:%ld:%ld, "
  36926. "dist_start=%ld, dist_end=%ld, dist_sce=%ld => "
  36927. "scan backwards from string end",
  36928. (long) use_cache, (void *) (sce ? sce->h : NULL),
  36929. (sce ? (long) sce->cidx : (long) -1),
  36930. (sce ? (long) sce->bidx : (long) -1),
  36931. (long) dist_start, (long) dist_end, (long) dist_sce));
  36932. p_found = duk__scan_backwards(p_end,
  36933. p_start,
  36934. dist_end);
  36935. }
  36936. scan_done:
  36937. if (!p_found) {
  36938. /* Scan error: this shouldn't normally happen; it could happen if
  36939. * string is not valid UTF-8 data, and clen/blen are not consistent
  36940. * with the scanning algorithm.
  36941. */
  36942. goto error;
  36943. }
  36944. DUK_ASSERT(p_found >= p_start);
  36945. DUK_ASSERT(p_found <= p_end); /* may be equal */
  36946. byte_offset = (duk_uint32_t) (p_found - p_start);
  36947. DUK_DDD(DUK_DDDPRINT("-> string %p, cidx %ld -> bidx %ld",
  36948. (void *) h, (long) char_offset, (long) byte_offset));
  36949. /*
  36950. * Update cache entry (allocating if necessary), and move the
  36951. * cache entry to the first place (in an "LRU" policy).
  36952. */
  36953. if (use_cache) {
  36954. /* update entry, allocating if necessary */
  36955. if (!sce) {
  36956. sce = heap->strcache + DUK_HEAP_STRCACHE_SIZE - 1; /* take last entry */
  36957. sce->h = h;
  36958. }
  36959. DUK_ASSERT(sce != NULL);
  36960. sce->bidx = (duk_uint32_t) (p_found - p_start);
  36961. sce->cidx = (duk_uint32_t) char_offset;
  36962. /* LRU: move our entry to first */
  36963. if (sce > &heap->strcache[0]) {
  36964. /*
  36965. * A C
  36966. * B A
  36967. * C <- sce ==> B
  36968. * D D
  36969. */
  36970. duk_strcache tmp;
  36971. tmp = *sce;
  36972. DUK_MEMMOVE((void *) (&heap->strcache[1]),
  36973. (void *) (&heap->strcache[0]),
  36974. (size_t) (((char *) sce) - ((char *) &heap->strcache[0])));
  36975. heap->strcache[0] = tmp;
  36976. /* 'sce' points to the wrong entry here, but is no longer used */
  36977. }
  36978. #ifdef DUK_USE_DDDPRINT
  36979. DUK_DDD(DUK_DDDPRINT("stringcache after char2byte (using cache):"));
  36980. for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
  36981. duk_strcache *c = heap->strcache + i;
  36982. DUK_DDD(DUK_DDDPRINT(" [%ld] -> h=%p, cidx=%ld, bidx=%ld",
  36983. (long) i, (void *) c->h, (long) c->cidx, (long) c->bidx));
  36984. }
  36985. #endif
  36986. }
  36987. return byte_offset;
  36988. error:
  36989. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, "string scan error");
  36990. return 0;
  36991. }
  36992. #line 1 "duk_heap_stringtable.c"
  36993. /*
  36994. * Heap stringtable handling, string interning.
  36995. */
  36996. /* include removed: duk_internal.h */
  36997. #if defined(DUK_USE_STRTAB_PROBE)
  36998. #define DUK__HASH_INITIAL(hash,h_size) DUK_STRTAB_HASH_INITIAL((hash),(h_size))
  36999. #define DUK__HASH_PROBE_STEP(hash) DUK_STRTAB_HASH_PROBE_STEP((hash))
  37000. #define DUK__DELETED_MARKER(heap) DUK_STRTAB_DELETED_MARKER((heap))
  37001. #endif
  37002. /*
  37003. * Create a hstring and insert into the heap. The created object
  37004. * is directly garbage collectable with reference count zero.
  37005. *
  37006. * The caller must place the interned string into the stringtable
  37007. * immediately (without chance of a longjmp); otherwise the string
  37008. * is lost.
  37009. */
  37010. DUK_LOCAL
  37011. duk_hstring *duk__alloc_init_hstring(duk_heap *heap,
  37012. const duk_uint8_t *str,
  37013. duk_uint32_t blen,
  37014. duk_uint32_t strhash,
  37015. const duk_uint8_t *extdata) {
  37016. duk_hstring *res = NULL;
  37017. duk_uint8_t *data;
  37018. duk_size_t alloc_size;
  37019. duk_uarridx_t dummy;
  37020. duk_uint32_t clen;
  37021. #if defined(DUK_USE_STRLEN16)
  37022. /* If blen <= 0xffffUL, clen is also guaranteed to be <= 0xffffUL. */
  37023. if (blen > 0xffffUL) {
  37024. DUK_D(DUK_DPRINT("16-bit string blen/clen active and blen over 16 bits, reject intern"));
  37025. return NULL;
  37026. }
  37027. #endif
  37028. if (extdata) {
  37029. alloc_size = (duk_size_t) sizeof(duk_hstring_external);
  37030. res = (duk_hstring *) DUK_ALLOC(heap, alloc_size);
  37031. if (!res) {
  37032. goto alloc_error;
  37033. }
  37034. DUK_MEMZERO(res, sizeof(duk_hstring_external));
  37035. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  37036. DUK_HEAPHDR_STRING_INIT_NULLS(&res->hdr);
  37037. #endif
  37038. DUK_HEAPHDR_SET_TYPE_AND_FLAGS(&res->hdr, DUK_HTYPE_STRING, DUK_HSTRING_FLAG_EXTDATA);
  37039. ((duk_hstring_external *) res)->extdata = extdata;
  37040. } else {
  37041. /* NUL terminate for convenient C access */
  37042. alloc_size = (duk_size_t) (sizeof(duk_hstring) + blen + 1);
  37043. res = (duk_hstring *) DUK_ALLOC(heap, alloc_size);
  37044. if (!res) {
  37045. goto alloc_error;
  37046. }
  37047. DUK_MEMZERO(res, sizeof(duk_hstring));
  37048. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  37049. DUK_HEAPHDR_STRING_INIT_NULLS(&res->hdr);
  37050. #endif
  37051. DUK_HEAPHDR_SET_TYPE_AND_FLAGS(&res->hdr, DUK_HTYPE_STRING, 0);
  37052. data = (duk_uint8_t *) (res + 1);
  37053. DUK_MEMCPY(data, str, blen);
  37054. data[blen] = (duk_uint8_t) 0;
  37055. }
  37056. if (duk_js_to_arrayindex_raw_string(str, blen, &dummy)) {
  37057. DUK_HSTRING_SET_ARRIDX(res);
  37058. }
  37059. /* All strings beginning with 0xff are treated as "internal",
  37060. * even strings interned by the user. This allows user code to
  37061. * create internal properties too, and makes behavior consistent
  37062. * in case user code happens to use a string also used by Duktape
  37063. * (such as string has already been interned and has the 'internal'
  37064. * flag set).
  37065. */
  37066. if (blen > 0 && str[0] == (duk_uint8_t) 0xff) {
  37067. DUK_HSTRING_SET_INTERNAL(res);
  37068. }
  37069. DUK_HSTRING_SET_HASH(res, strhash);
  37070. DUK_HSTRING_SET_BYTELEN(res, blen);
  37071. clen = (duk_uint32_t) duk_unicode_unvalidated_utf8_length(str, (duk_size_t) blen);
  37072. DUK_ASSERT(clen <= blen);
  37073. DUK_HSTRING_SET_CHARLEN(res, clen);
  37074. DUK_DDD(DUK_DDDPRINT("interned string, hash=0x%08lx, blen=%ld, clen=%ld, has_arridx=%ld, has_extdata=%ld",
  37075. (unsigned long) DUK_HSTRING_GET_HASH(res),
  37076. (long) DUK_HSTRING_GET_BYTELEN(res),
  37077. (long) DUK_HSTRING_GET_CHARLEN(res),
  37078. (long) DUK_HSTRING_HAS_ARRIDX(res) ? 1 : 0,
  37079. (long) DUK_HSTRING_HAS_EXTDATA(res) ? 1 : 0));
  37080. return res;
  37081. alloc_error:
  37082. DUK_FREE(heap, res);
  37083. return NULL;
  37084. }
  37085. /*
  37086. * String table algorithm: fixed size string table with array chaining
  37087. *
  37088. * The top level string table has a fixed size, with each slot holding
  37089. * either NULL, string pointer, or pointer to a separately allocated
  37090. * string pointer list.
  37091. *
  37092. * This is good for low memory environments using a pool allocator: the
  37093. * top level allocation has a fixed size and the pointer lists have quite
  37094. * small allocation size, which further matches the typical pool sizes
  37095. * needed by objects, strings, property tables, etc.
  37096. */
  37097. #if defined(DUK_USE_STRTAB_CHAIN)
  37098. #if defined(DUK_USE_HEAPPTR16)
  37099. DUK_LOCAL duk_bool_t duk__insert_hstring_chain(duk_heap *heap, duk_hstring *h) {
  37100. duk_small_uint_t slotidx;
  37101. duk_strtab_entry *e;
  37102. duk_uint16_t *lst;
  37103. duk_uint16_t *new_lst;
  37104. duk_size_t i, n;
  37105. duk_uint16_t null16 = heap->heapptr_null16;
  37106. duk_uint16_t h16 = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) h);
  37107. DUK_ASSERT(heap != NULL);
  37108. DUK_ASSERT(h != NULL);
  37109. slotidx = DUK_HSTRING_GET_HASH(h) % DUK_STRTAB_CHAIN_SIZE;
  37110. DUK_ASSERT(slotidx < DUK_STRTAB_CHAIN_SIZE);
  37111. e = heap->strtable + slotidx;
  37112. if (e->listlen == 0) {
  37113. if (e->u.str16 == null16) {
  37114. e->u.str16 = h16;
  37115. } else {
  37116. /* Now two entries in the same slot, alloc list */
  37117. lst = (duk_uint16_t *) DUK_ALLOC(heap, sizeof(duk_uint16_t) * 2);
  37118. if (lst == NULL) {
  37119. return 1; /* fail */
  37120. }
  37121. lst[0] = e->u.str16;
  37122. lst[1] = h16;
  37123. e->u.strlist16 = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) lst);
  37124. e->listlen = 2;
  37125. }
  37126. } else {
  37127. DUK_ASSERT(e->u.strlist16 != null16);
  37128. lst = (duk_uint16_t *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.strlist16);
  37129. DUK_ASSERT(lst != NULL);
  37130. for (i = 0, n = e->listlen; i < n; i++) {
  37131. if (lst[i] == null16) {
  37132. lst[i] = h16;
  37133. return 0;
  37134. }
  37135. }
  37136. if (e->listlen + 1 == 0) {
  37137. /* Overflow, relevant mainly when listlen is 16 bits. */
  37138. return 1; /* fail */
  37139. }
  37140. new_lst = (duk_uint16_t *) DUK_REALLOC(heap, lst, sizeof(duk_uint16_t) * (e->listlen + 1));
  37141. if (new_lst == NULL) {
  37142. return 1; /* fail */
  37143. }
  37144. new_lst[e->listlen++] = h16;
  37145. e->u.strlist16 = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) new_lst);
  37146. }
  37147. return 0;
  37148. }
  37149. #else /* DUK_USE_HEAPPTR16 */
  37150. DUK_LOCAL duk_bool_t duk__insert_hstring_chain(duk_heap *heap, duk_hstring *h) {
  37151. duk_small_uint_t slotidx;
  37152. duk_strtab_entry *e;
  37153. duk_hstring **lst;
  37154. duk_hstring **new_lst;
  37155. duk_size_t i, n;
  37156. DUK_ASSERT(heap != NULL);
  37157. DUK_ASSERT(h != NULL);
  37158. slotidx = DUK_HSTRING_GET_HASH(h) % DUK_STRTAB_CHAIN_SIZE;
  37159. DUK_ASSERT(slotidx < DUK_STRTAB_CHAIN_SIZE);
  37160. e = heap->strtable + slotidx;
  37161. if (e->listlen == 0) {
  37162. if (e->u.str == NULL) {
  37163. e->u.str = h;
  37164. } else {
  37165. /* Now two entries in the same slot, alloc list */
  37166. lst = (duk_hstring **) DUK_ALLOC(heap, sizeof(duk_hstring *) * 2);
  37167. if (lst == NULL) {
  37168. return 1; /* fail */
  37169. }
  37170. lst[0] = e->u.str;
  37171. lst[1] = h;
  37172. e->u.strlist = lst;
  37173. e->listlen = 2;
  37174. }
  37175. } else {
  37176. DUK_ASSERT(e->u.strlist != NULL);
  37177. lst = e->u.strlist;
  37178. for (i = 0, n = e->listlen; i < n; i++) {
  37179. if (lst[i] == NULL) {
  37180. lst[i] = h;
  37181. return 0;
  37182. }
  37183. }
  37184. if (e->listlen + 1 == 0) {
  37185. /* Overflow, relevant mainly when listlen is 16 bits. */
  37186. return 1; /* fail */
  37187. }
  37188. new_lst = (duk_hstring **) DUK_REALLOC(heap, e->u.strlist, sizeof(duk_hstring *) * (e->listlen + 1));
  37189. if (new_lst == NULL) {
  37190. return 1; /* fail */
  37191. }
  37192. new_lst[e->listlen++] = h;
  37193. e->u.strlist = new_lst;
  37194. }
  37195. return 0;
  37196. }
  37197. #endif /* DUK_USE_HEAPPTR16 */
  37198. #if defined(DUK_USE_HEAPPTR16)
  37199. 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) {
  37200. duk_small_uint_t slotidx;
  37201. duk_strtab_entry *e;
  37202. duk_uint16_t *lst;
  37203. duk_size_t i, n;
  37204. duk_uint16_t null16 = heap->heapptr_null16;
  37205. DUK_ASSERT(heap != NULL);
  37206. slotidx = strhash % DUK_STRTAB_CHAIN_SIZE;
  37207. DUK_ASSERT(slotidx < DUK_STRTAB_CHAIN_SIZE);
  37208. e = heap->strtable + slotidx;
  37209. if (e->listlen == 0) {
  37210. if (e->u.str16 != null16) {
  37211. duk_hstring *h = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.str16);
  37212. DUK_ASSERT(h != NULL);
  37213. if (DUK_HSTRING_GET_BYTELEN(h) == blen &&
  37214. DUK_MEMCMP(str, DUK_HSTRING_GET_DATA(h), blen) == 0) {
  37215. return h;
  37216. }
  37217. }
  37218. } else {
  37219. DUK_ASSERT(e->u.strlist16 != null16);
  37220. lst = (duk_uint16_t *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.strlist16);
  37221. DUK_ASSERT(lst != NULL);
  37222. for (i = 0, n = e->listlen; i < n; i++) {
  37223. if (lst[i] != null16) {
  37224. duk_hstring *h = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, lst[i]);
  37225. DUK_ASSERT(h != NULL);
  37226. if (DUK_HSTRING_GET_BYTELEN(h) == blen &&
  37227. DUK_MEMCMP(str, DUK_HSTRING_GET_DATA(h), blen) == 0) {
  37228. return h;
  37229. }
  37230. }
  37231. }
  37232. }
  37233. return NULL;
  37234. }
  37235. #else /* DUK_USE_HEAPPTR16 */
  37236. 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) {
  37237. duk_small_uint_t slotidx;
  37238. duk_strtab_entry *e;
  37239. duk_hstring **lst;
  37240. duk_size_t i, n;
  37241. DUK_ASSERT(heap != NULL);
  37242. slotidx = strhash % DUK_STRTAB_CHAIN_SIZE;
  37243. DUK_ASSERT(slotidx < DUK_STRTAB_CHAIN_SIZE);
  37244. e = heap->strtable + slotidx;
  37245. if (e->listlen == 0) {
  37246. if (e->u.str != NULL &&
  37247. DUK_HSTRING_GET_BYTELEN(e->u.str) == blen &&
  37248. DUK_MEMCMP(str, DUK_HSTRING_GET_DATA(e->u.str), blen) == 0) {
  37249. return e->u.str;
  37250. }
  37251. } else {
  37252. DUK_ASSERT(e->u.strlist != NULL);
  37253. lst = e->u.strlist;
  37254. for (i = 0, n = e->listlen; i < n; i++) {
  37255. if (lst[i] != NULL &&
  37256. DUK_HSTRING_GET_BYTELEN(lst[i]) == blen &&
  37257. DUK_MEMCMP(str, DUK_HSTRING_GET_DATA(lst[i]), blen) == 0) {
  37258. return lst[i];
  37259. }
  37260. }
  37261. }
  37262. return NULL;
  37263. }
  37264. #endif /* DUK_USE_HEAPPTR16 */
  37265. #if defined(DUK_USE_HEAPPTR16)
  37266. DUK_LOCAL void duk__remove_matching_hstring_chain(duk_heap *heap, duk_hstring *h) {
  37267. duk_small_uint_t slotidx;
  37268. duk_strtab_entry *e;
  37269. duk_uint16_t *lst;
  37270. duk_size_t i, n;
  37271. duk_uint16_t h16;
  37272. duk_uint16_t null16 = heap->heapptr_null16;
  37273. DUK_ASSERT(heap != NULL);
  37274. DUK_ASSERT(h != NULL);
  37275. slotidx = DUK_HSTRING_GET_HASH(h) % DUK_STRTAB_CHAIN_SIZE;
  37276. DUK_ASSERT(slotidx < DUK_STRTAB_CHAIN_SIZE);
  37277. DUK_ASSERT(h != NULL);
  37278. h16 = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) h);
  37279. e = heap->strtable + slotidx;
  37280. if (e->listlen == 0) {
  37281. if (e->u.str16 == h16) {
  37282. e->u.str16 = null16;
  37283. return;
  37284. }
  37285. } else {
  37286. DUK_ASSERT(e->u.strlist16 != null16);
  37287. lst = (duk_uint16_t *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.strlist16);
  37288. DUK_ASSERT(lst != NULL);
  37289. for (i = 0, n = e->listlen; i < n; i++) {
  37290. if (lst[i] == h16) {
  37291. lst[i] = null16;
  37292. return;
  37293. }
  37294. }
  37295. }
  37296. DUK_D(DUK_DPRINT("failed to find string that should be in stringtable"));
  37297. DUK_UNREACHABLE();
  37298. return;
  37299. }
  37300. #else /* DUK_USE_HEAPPTR16 */
  37301. DUK_LOCAL void duk__remove_matching_hstring_chain(duk_heap *heap, duk_hstring *h) {
  37302. duk_small_uint_t slotidx;
  37303. duk_strtab_entry *e;
  37304. duk_hstring **lst;
  37305. duk_size_t i, n;
  37306. DUK_ASSERT(heap != NULL);
  37307. DUK_ASSERT(h != NULL);
  37308. slotidx = DUK_HSTRING_GET_HASH(h) % DUK_STRTAB_CHAIN_SIZE;
  37309. DUK_ASSERT(slotidx < DUK_STRTAB_CHAIN_SIZE);
  37310. e = heap->strtable + slotidx;
  37311. if (e->listlen == 0) {
  37312. DUK_ASSERT(h != NULL);
  37313. if (e->u.str == h) {
  37314. e->u.str = NULL;
  37315. return;
  37316. }
  37317. } else {
  37318. DUK_ASSERT(e->u.strlist != NULL);
  37319. lst = e->u.strlist;
  37320. for (i = 0, n = e->listlen; i < n; i++) {
  37321. DUK_ASSERT(h != NULL);
  37322. if (lst[i] == h) {
  37323. lst[i] = NULL;
  37324. return;
  37325. }
  37326. }
  37327. }
  37328. DUK_D(DUK_DPRINT("failed to find string that should be in stringtable"));
  37329. DUK_UNREACHABLE();
  37330. return;
  37331. }
  37332. #endif /* DUK_USE_HEAPPTR16 */
  37333. #if defined(DUK_USE_DEBUG)
  37334. DUK_INTERNAL void duk_heap_dump_strtab(duk_heap *heap) {
  37335. duk_strtab_entry *e;
  37336. duk_small_uint_t i;
  37337. duk_size_t j, n, used;
  37338. #if defined(DUK_USE_HEAPPTR16)
  37339. duk_uint16_t *lst;
  37340. duk_uint16_t null16 = heap->heapptr_null16;
  37341. #else
  37342. duk_hstring **lst;
  37343. #endif
  37344. DUK_ASSERT(heap != NULL);
  37345. for (i = 0; i < DUK_STRTAB_CHAIN_SIZE; i++) {
  37346. e = heap->strtable + i;
  37347. if (e->listlen == 0) {
  37348. #if defined(DUK_USE_HEAPPTR16)
  37349. DUK_DD(DUK_DDPRINT("[%03d] -> plain %d", (int) i, (int) (e->u.str16 != null16 ? 1 : 0)));
  37350. #else
  37351. DUK_DD(DUK_DDPRINT("[%03d] -> plain %d", (int) i, (int) (e->u.str ? 1 : 0)));
  37352. #endif
  37353. } else {
  37354. used = 0;
  37355. #if defined(DUK_USE_HEAPPTR16)
  37356. lst = (duk_uint16_t *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.strlist16);
  37357. #else
  37358. lst = e->u.strlist;
  37359. #endif
  37360. DUK_ASSERT(lst != NULL);
  37361. for (j = 0, n = e->listlen; j < n; j++) {
  37362. #if defined(DUK_USE_HEAPPTR16)
  37363. if (lst[j] != null16) {
  37364. #else
  37365. if (lst[j] != NULL) {
  37366. #endif
  37367. used++;
  37368. }
  37369. }
  37370. DUK_DD(DUK_DDPRINT("[%03d] -> array %d/%d", (int) i, (int) used, (int) e->listlen));
  37371. }
  37372. }
  37373. }
  37374. #endif /* DUK_USE_DEBUG */
  37375. #endif /* DUK_USE_STRTAB_CHAIN */
  37376. /*
  37377. * String table algorithm: closed hashing with a probe sequence
  37378. *
  37379. * This is the default algorithm and works fine for environments with
  37380. * minimal memory constraints.
  37381. */
  37382. #if defined(DUK_USE_STRTAB_PROBE)
  37383. /* Count actually used (non-NULL, non-DELETED) entries. */
  37384. DUK_LOCAL duk_int_t duk__count_used_probe(duk_heap *heap) {
  37385. duk_int_t res = 0;
  37386. duk_uint_fast32_t i, n;
  37387. #if defined(DUK_USE_HEAPPTR16)
  37388. duk_uint16_t null16 = heap->heapptr_null16;
  37389. duk_uint16_t deleted16 = heap->heapptr_deleted16;
  37390. #endif
  37391. n = (duk_uint_fast32_t) heap->st_size;
  37392. for (i = 0; i < n; i++) {
  37393. #if defined(DUK_USE_HEAPPTR16)
  37394. if (heap->strtable16[i] != null16 && heap->strtable16[i] != deleted16) {
  37395. #else
  37396. if (heap->strtable[i] != NULL && heap->strtable[i] != DUK__DELETED_MARKER(heap)) {
  37397. #endif
  37398. res++;
  37399. }
  37400. }
  37401. return res;
  37402. }
  37403. #if defined(DUK_USE_HEAPPTR16)
  37404. 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) {
  37405. #else
  37406. 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) {
  37407. #endif
  37408. duk_uint32_t i;
  37409. duk_uint32_t step;
  37410. #if defined(DUK_USE_HEAPPTR16)
  37411. duk_uint16_t null16 = heap->heapptr_null16;
  37412. duk_uint16_t deleted16 = heap->heapptr_deleted16;
  37413. #endif
  37414. DUK_ASSERT(size > 0);
  37415. i = DUK__HASH_INITIAL(DUK_HSTRING_GET_HASH(h), size);
  37416. step = DUK__HASH_PROBE_STEP(DUK_HSTRING_GET_HASH(h));
  37417. for (;;) {
  37418. #if defined(DUK_USE_HEAPPTR16)
  37419. duk_uint16_t e16 = entries16[i];
  37420. #else
  37421. duk_hstring *e = entries[i];
  37422. #endif
  37423. #if defined(DUK_USE_HEAPPTR16)
  37424. /* XXX: could check for e16 == 0 because NULL is guaranteed to
  37425. * encode to zero.
  37426. */
  37427. if (e16 == null16) {
  37428. #else
  37429. if (e == NULL) {
  37430. #endif
  37431. DUK_DDD(DUK_DDDPRINT("insert hit (null): %ld", (long) i));
  37432. #if defined(DUK_USE_HEAPPTR16)
  37433. entries16[i] = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) h);
  37434. #else
  37435. entries[i] = h;
  37436. #endif
  37437. (*p_used)++;
  37438. break;
  37439. #if defined(DUK_USE_HEAPPTR16)
  37440. } else if (e16 == deleted16) {
  37441. #else
  37442. } else if (e == DUK__DELETED_MARKER(heap)) {
  37443. #endif
  37444. /* st_used remains the same, DELETED is counted as used */
  37445. DUK_DDD(DUK_DDDPRINT("insert hit (deleted): %ld", (long) i));
  37446. #if defined(DUK_USE_HEAPPTR16)
  37447. entries16[i] = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) h);
  37448. #else
  37449. entries[i] = h;
  37450. #endif
  37451. break;
  37452. }
  37453. DUK_DDD(DUK_DDDPRINT("insert miss: %ld", (long) i));
  37454. i = (i + step) % size;
  37455. /* looping should never happen */
  37456. DUK_ASSERT(i != DUK__HASH_INITIAL(DUK_HSTRING_GET_HASH(h), size));
  37457. }
  37458. }
  37459. #if defined(DUK_USE_HEAPPTR16)
  37460. 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) {
  37461. #else
  37462. 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) {
  37463. #endif
  37464. duk_uint32_t i;
  37465. duk_uint32_t step;
  37466. DUK_ASSERT(size > 0);
  37467. i = DUK__HASH_INITIAL(strhash, size);
  37468. step = DUK__HASH_PROBE_STEP(strhash);
  37469. for (;;) {
  37470. duk_hstring *e;
  37471. #if defined(DUK_USE_HEAPPTR16)
  37472. e = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, entries16[i]);
  37473. #else
  37474. e = entries[i];
  37475. #endif
  37476. if (!e) {
  37477. return NULL;
  37478. }
  37479. if (e != DUK__DELETED_MARKER(heap) && DUK_HSTRING_GET_BYTELEN(e) == blen) {
  37480. if (DUK_MEMCMP(str, DUK_HSTRING_GET_DATA(e), blen) == 0) {
  37481. DUK_DDD(DUK_DDDPRINT("find matching hit: %ld (step %ld, size %ld)",
  37482. (long) i, (long) step, (long) size));
  37483. return e;
  37484. }
  37485. }
  37486. DUK_DDD(DUK_DDDPRINT("find matching miss: %ld (step %ld, size %ld)",
  37487. (long) i, (long) step, (long) size));
  37488. i = (i + step) % size;
  37489. /* looping should never happen */
  37490. DUK_ASSERT(i != DUK__HASH_INITIAL(strhash, size));
  37491. }
  37492. DUK_UNREACHABLE();
  37493. }
  37494. #if defined(DUK_USE_HEAPPTR16)
  37495. DUK_LOCAL void duk__remove_matching_hstring_probe(duk_heap *heap, duk_uint16_t *entries16, duk_uint32_t size, duk_hstring *h) {
  37496. #else
  37497. DUK_LOCAL void duk__remove_matching_hstring_probe(duk_heap *heap, duk_hstring **entries, duk_uint32_t size, duk_hstring *h) {
  37498. #endif
  37499. duk_uint32_t i;
  37500. duk_uint32_t step;
  37501. duk_uint32_t hash;
  37502. #if defined(DUK_USE_HEAPPTR16)
  37503. duk_uint16_t null16 = heap->heapptr_null16;
  37504. duk_uint16_t h16 = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) h);
  37505. #endif
  37506. DUK_ASSERT(size > 0);
  37507. hash = DUK_HSTRING_GET_HASH(h);
  37508. i = DUK__HASH_INITIAL(hash, size);
  37509. step = DUK__HASH_PROBE_STEP(hash);
  37510. for (;;) {
  37511. #if defined(DUK_USE_HEAPPTR16)
  37512. duk_uint16_t e16 = entries16[i];
  37513. #else
  37514. duk_hstring *e = entries[i];
  37515. #endif
  37516. #if defined(DUK_USE_HEAPPTR16)
  37517. if (e16 == null16) {
  37518. #else
  37519. if (!e) {
  37520. #endif
  37521. DUK_UNREACHABLE();
  37522. break;
  37523. }
  37524. #if defined(DUK_USE_HEAPPTR16)
  37525. if (e16 == h16) {
  37526. #else
  37527. if (e == h) {
  37528. #endif
  37529. /* st_used remains the same, DELETED is counted as used */
  37530. DUK_DDD(DUK_DDDPRINT("free matching hit: %ld", (long) i));
  37531. #if defined(DUK_USE_HEAPPTR16)
  37532. entries16[i] = heap->heapptr_deleted16;
  37533. #else
  37534. entries[i] = DUK__DELETED_MARKER(heap);
  37535. #endif
  37536. break;
  37537. }
  37538. DUK_DDD(DUK_DDDPRINT("free matching miss: %ld", (long) i));
  37539. i = (i + step) % size;
  37540. /* looping should never happen */
  37541. DUK_ASSERT(i != DUK__HASH_INITIAL(hash, size));
  37542. }
  37543. }
  37544. DUK_LOCAL duk_bool_t duk__resize_strtab_raw_probe(duk_heap *heap, duk_uint32_t new_size) {
  37545. #ifdef DUK_USE_MARK_AND_SWEEP
  37546. duk_small_uint_t prev_mark_and_sweep_base_flags;
  37547. #endif
  37548. #ifdef DUK_USE_DEBUG
  37549. duk_uint32_t old_used = heap->st_used;
  37550. #endif
  37551. duk_uint32_t old_size = heap->st_size;
  37552. #if defined(DUK_USE_HEAPPTR16)
  37553. duk_uint16_t *old_entries = heap->strtable16;
  37554. duk_uint16_t *new_entries = NULL;
  37555. #else
  37556. duk_hstring **old_entries = heap->strtable;
  37557. duk_hstring **new_entries = NULL;
  37558. #endif
  37559. duk_uint32_t new_used = 0;
  37560. duk_uint32_t i;
  37561. #ifdef DUK_USE_DEBUG
  37562. DUK_UNREF(old_used); /* unused with some debug level combinations */
  37563. #endif
  37564. #ifdef DUK_USE_DDDPRINT
  37565. DUK_DDD(DUK_DDDPRINT("attempt to resize stringtable: %ld entries, %ld bytes, %ld used, %ld%% load -> %ld entries, %ld bytes, %ld used, %ld%% load",
  37566. (long) old_size, (long) (sizeof(duk_hstring *) * old_size), (long) old_used,
  37567. (long) (((double) old_used) / ((double) old_size) * 100.0),
  37568. (long) new_size, (long) (sizeof(duk_hstring *) * new_size), (long) duk__count_used_probe(heap),
  37569. (long) (((double) duk__count_used_probe(heap)) / ((double) new_size) * 100.0)));
  37570. #endif
  37571. DUK_ASSERT(new_size > (duk_uint32_t) duk__count_used_probe(heap)); /* required for rehash to succeed, equality not that useful */
  37572. DUK_ASSERT(old_entries);
  37573. #ifdef DUK_USE_MARK_AND_SWEEP
  37574. DUK_ASSERT((heap->mark_and_sweep_base_flags & DUK_MS_FLAG_NO_STRINGTABLE_RESIZE) == 0);
  37575. #endif
  37576. /*
  37577. * The attempt to allocate may cause a GC. Such a GC must not attempt to resize
  37578. * the stringtable (though it can be swept); finalizer execution and object
  37579. * compaction must also be postponed to avoid the pressure to add strings to the
  37580. * string table.
  37581. */
  37582. #ifdef DUK_USE_MARK_AND_SWEEP
  37583. prev_mark_and_sweep_base_flags = heap->mark_and_sweep_base_flags;
  37584. heap->mark_and_sweep_base_flags |= \
  37585. DUK_MS_FLAG_NO_STRINGTABLE_RESIZE | /* avoid recursive call here */
  37586. DUK_MS_FLAG_NO_FINALIZERS | /* avoid pressure to add/remove strings */
  37587. DUK_MS_FLAG_NO_OBJECT_COMPACTION; /* avoid array abandoning which interns strings */
  37588. #endif
  37589. #if defined(DUK_USE_HEAPPTR16)
  37590. new_entries = (duk_uint16_t *) DUK_ALLOC(heap, sizeof(duk_uint16_t) * new_size);
  37591. #else
  37592. new_entries = (duk_hstring **) DUK_ALLOC(heap, sizeof(duk_hstring *) * new_size);
  37593. #endif
  37594. #ifdef DUK_USE_MARK_AND_SWEEP
  37595. heap->mark_and_sweep_base_flags = prev_mark_and_sweep_base_flags;
  37596. #endif
  37597. if (!new_entries) {
  37598. goto resize_error;
  37599. }
  37600. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  37601. for (i = 0; i < new_size; i++) {
  37602. #if defined(DUK_USE_HEAPPTR16)
  37603. new_entries[i] = heap->heapptr_null16;
  37604. #else
  37605. new_entries[i] = NULL;
  37606. #endif
  37607. }
  37608. #else
  37609. #if defined(DUK_USE_HEAPPTR16)
  37610. /* Relies on NULL encoding to zero. */
  37611. DUK_MEMZERO(new_entries, sizeof(duk_uint16_t) * new_size);
  37612. #else
  37613. DUK_MEMZERO(new_entries, sizeof(duk_hstring *) * new_size);
  37614. #endif
  37615. #endif
  37616. /* Because new_size > duk__count_used_probe(heap), guaranteed to work */
  37617. for (i = 0; i < old_size; i++) {
  37618. duk_hstring *e;
  37619. #if defined(DUK_USE_HEAPPTR16)
  37620. e = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, old_entries[i]);
  37621. #else
  37622. e = old_entries[i];
  37623. #endif
  37624. if (e == NULL || e == DUK__DELETED_MARKER(heap)) {
  37625. continue;
  37626. }
  37627. /* checking for DUK__DELETED_MARKER is not necessary here, but helper does it now */
  37628. duk__insert_hstring_probe(heap, new_entries, new_size, &new_used, e);
  37629. }
  37630. #ifdef DUK_USE_DDPRINT
  37631. DUK_DD(DUK_DDPRINT("resized stringtable: %ld entries, %ld bytes, %ld used, %ld%% load -> %ld entries, %ld bytes, %ld used, %ld%% load",
  37632. (long) old_size, (long) (sizeof(duk_hstring *) * old_size), (long) old_used,
  37633. (long) (((double) old_used) / ((double) old_size) * 100.0),
  37634. (long) new_size, (long) (sizeof(duk_hstring *) * new_size), (long) new_used,
  37635. (long) (((double) new_used) / ((double) new_size) * 100.0)));
  37636. #endif
  37637. #if defined(DUK_USE_HEAPPTR16)
  37638. DUK_FREE(heap, heap->strtable16);
  37639. heap->strtable16 = new_entries;
  37640. #else
  37641. DUK_FREE(heap, heap->strtable);
  37642. heap->strtable = new_entries;
  37643. #endif
  37644. heap->st_size = new_size;
  37645. heap->st_used = new_used; /* may be less, since DELETED entries are NULLed by rehash */
  37646. return 0; /* OK */
  37647. resize_error:
  37648. DUK_FREE(heap, new_entries);
  37649. return 1; /* FAIL */
  37650. }
  37651. DUK_LOCAL duk_bool_t duk__resize_strtab_probe(duk_heap *heap) {
  37652. duk_uint32_t new_size;
  37653. duk_bool_t ret;
  37654. new_size = (duk_uint32_t) duk__count_used_probe(heap);
  37655. if (new_size >= 0x80000000UL) {
  37656. new_size = DUK_STRTAB_HIGHEST_32BIT_PRIME;
  37657. } else {
  37658. new_size = duk_util_get_hash_prime(DUK_STRTAB_GROW_ST_SIZE(new_size));
  37659. new_size = duk_util_get_hash_prime(new_size);
  37660. }
  37661. DUK_ASSERT(new_size > 0);
  37662. /* rehash even if old and new sizes are the same to get rid of
  37663. * DELETED entries.
  37664. */
  37665. ret = duk__resize_strtab_raw_probe(heap, new_size);
  37666. return ret;
  37667. }
  37668. DUK_LOCAL duk_bool_t duk__recheck_strtab_size_probe(duk_heap *heap, duk_uint32_t new_used) {
  37669. duk_uint32_t new_free;
  37670. duk_uint32_t tmp1;
  37671. duk_uint32_t tmp2;
  37672. DUK_ASSERT(new_used <= heap->st_size); /* grow by at most one */
  37673. new_free = heap->st_size - new_used; /* unsigned intentionally */
  37674. /* new_free / size <= 1 / DIV <=> new_free <= size / DIV */
  37675. /* new_used / size <= 1 / DIV <=> new_used <= size / DIV */
  37676. tmp1 = heap->st_size / DUK_STRTAB_MIN_FREE_DIVISOR;
  37677. tmp2 = heap->st_size / DUK_STRTAB_MIN_USED_DIVISOR;
  37678. if (new_free <= tmp1 || new_used <= tmp2) {
  37679. /* load factor too low or high, count actually used entries and resize */
  37680. return duk__resize_strtab_probe(heap);
  37681. } else {
  37682. return 0; /* OK */
  37683. }
  37684. }
  37685. #if defined(DUK_USE_DEBUG)
  37686. DUK_INTERNAL void duk_heap_dump_strtab(duk_heap *heap) {
  37687. duk_uint32_t i;
  37688. duk_hstring *h;
  37689. DUK_ASSERT(heap != NULL);
  37690. #if defined(DUK_USE_HEAPPTR16)
  37691. DUK_ASSERT(heap->strtable16 != NULL);
  37692. #else
  37693. DUK_ASSERT(heap->strtable != NULL);
  37694. #endif
  37695. DUK_UNREF(h);
  37696. for (i = 0; i < heap->st_size; i++) {
  37697. #if defined(DUK_USE_HEAPPTR16)
  37698. h = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->strtable16[i]);
  37699. #else
  37700. h = heap->strtable[i];
  37701. #endif
  37702. DUK_DD(DUK_DDPRINT("[%03d] -> %p", (int) i, (void *) h));
  37703. }
  37704. }
  37705. #endif /* DUK_USE_DEBUG */
  37706. #endif /* DUK_USE_STRTAB_PROBE */
  37707. /*
  37708. * Raw intern and lookup
  37709. */
  37710. DUK_LOCAL duk_hstring *duk__do_intern(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen, duk_uint32_t strhash) {
  37711. duk_hstring *res;
  37712. const duk_uint8_t *extdata;
  37713. #if defined(DUK_USE_STRTAB_PROBE)
  37714. if (duk__recheck_strtab_size_probe(heap, heap->st_used + 1)) {
  37715. return NULL;
  37716. }
  37717. #endif
  37718. /* For manual testing only. */
  37719. #if 0
  37720. {
  37721. duk_size_t i;
  37722. DUK_PRINTF("INTERN: \"");
  37723. for (i = 0; i < blen; i++) {
  37724. duk_uint8_t x = str[i];
  37725. if (x >= 0x20 && x <= 0x7e && x != '"' && x != '\\') {
  37726. DUK_PRINTF("%c", (int) x); /* char: use int cast */
  37727. } else {
  37728. DUK_PRINTF("\\x%02lx", (long) x);
  37729. }
  37730. }
  37731. DUK_PRINTF("\"\n");
  37732. }
  37733. #endif
  37734. #if defined(DUK_USE_HSTRING_EXTDATA) && defined(DUK_USE_EXTSTR_INTERN_CHECK)
  37735. extdata = (const duk_uint8_t *) DUK_USE_EXTSTR_INTERN_CHECK(heap->heap_udata, (void *) str, (duk_size_t) blen);
  37736. #else
  37737. extdata = (const duk_uint8_t *) NULL;
  37738. #endif
  37739. res = duk__alloc_init_hstring(heap, str, blen, strhash, extdata);
  37740. if (!res) {
  37741. return NULL;
  37742. }
  37743. #if defined(DUK_USE_STRTAB_CHAIN)
  37744. if (duk__insert_hstring_chain(heap, res)) {
  37745. /* failed */
  37746. DUK_FREE(heap, res);
  37747. return NULL;
  37748. }
  37749. #elif defined(DUK_USE_STRTAB_PROBE)
  37750. /* guaranteed to succeed */
  37751. duk__insert_hstring_probe(heap,
  37752. #if defined(DUK_USE_HEAPPTR16)
  37753. heap->strtable16,
  37754. #else
  37755. heap->strtable,
  37756. #endif
  37757. heap->st_size,
  37758. &heap->st_used,
  37759. res);
  37760. #else
  37761. #error internal error, invalid strtab options
  37762. #endif
  37763. /* Note: hstring is in heap but has refcount zero and is not strongly reachable.
  37764. * Caller should increase refcount and make the hstring reachable before any
  37765. * operations which require allocation (and possible gc).
  37766. */
  37767. return res;
  37768. }
  37769. DUK_LOCAL duk_hstring *duk__do_lookup(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen, duk_uint32_t *out_strhash) {
  37770. duk_hstring *res;
  37771. DUK_ASSERT(out_strhash);
  37772. *out_strhash = duk_heap_hashstring(heap, str, (duk_size_t) blen);
  37773. #if defined(DUK_USE_STRTAB_CHAIN)
  37774. res = duk__find_matching_string_chain(heap, str, blen, *out_strhash);
  37775. #elif defined(DUK_USE_STRTAB_PROBE)
  37776. res = duk__find_matching_string_probe(heap,
  37777. #if defined(DUK_USE_HEAPPTR16)
  37778. heap->strtable16,
  37779. #else
  37780. heap->strtable,
  37781. #endif
  37782. heap->st_size,
  37783. str,
  37784. blen,
  37785. *out_strhash);
  37786. #else
  37787. #error internal error, invalid strtab options
  37788. #endif
  37789. return res;
  37790. }
  37791. /*
  37792. * Exposed calls
  37793. */
  37794. #if 0 /*unused*/
  37795. DUK_INTERNAL duk_hstring *duk_heap_string_lookup(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen) {
  37796. duk_uint32_t strhash; /* dummy */
  37797. return duk__do_lookup(heap, str, blen, &strhash);
  37798. }
  37799. #endif
  37800. DUK_INTERNAL duk_hstring *duk_heap_string_intern(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen) {
  37801. duk_hstring *res;
  37802. duk_uint32_t strhash;
  37803. /* caller is responsible for ensuring this */
  37804. DUK_ASSERT(blen <= DUK_HSTRING_MAX_BYTELEN);
  37805. res = duk__do_lookup(heap, str, blen, &strhash);
  37806. if (res) {
  37807. return res;
  37808. }
  37809. res = duk__do_intern(heap, str, blen, strhash);
  37810. return res; /* may be NULL */
  37811. }
  37812. DUK_INTERNAL duk_hstring *duk_heap_string_intern_checked(duk_hthread *thr, const duk_uint8_t *str, duk_uint32_t blen) {
  37813. duk_hstring *res = duk_heap_string_intern(thr->heap, str, blen);
  37814. if (!res) {
  37815. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, "failed to intern string");
  37816. }
  37817. return res;
  37818. }
  37819. #if 0 /*unused*/
  37820. DUK_INTERNAL duk_hstring *duk_heap_string_lookup_u32(duk_heap *heap, duk_uint32_t val) {
  37821. char buf[DUK_STRTAB_U32_MAX_STRLEN+1];
  37822. DUK_SNPRINTF(buf, sizeof(buf), "%lu", (unsigned long) val);
  37823. buf[sizeof(buf) - 1] = (char) 0;
  37824. DUK_ASSERT(DUK_STRLEN(buf) <= DUK_UINT32_MAX); /* formatted result limited */
  37825. return duk_heap_string_lookup(heap, (const duk_uint8_t *) buf, (duk_uint32_t) DUK_STRLEN(buf));
  37826. }
  37827. #endif
  37828. DUK_INTERNAL duk_hstring *duk_heap_string_intern_u32(duk_heap *heap, duk_uint32_t val) {
  37829. char buf[DUK_STRTAB_U32_MAX_STRLEN+1];
  37830. DUK_SNPRINTF(buf, sizeof(buf), "%lu", (unsigned long) val);
  37831. buf[sizeof(buf) - 1] = (char) 0;
  37832. DUK_ASSERT(DUK_STRLEN(buf) <= DUK_UINT32_MAX); /* formatted result limited */
  37833. return duk_heap_string_intern(heap, (const duk_uint8_t *) buf, (duk_uint32_t) DUK_STRLEN(buf));
  37834. }
  37835. DUK_INTERNAL duk_hstring *duk_heap_string_intern_u32_checked(duk_hthread *thr, duk_uint32_t val) {
  37836. duk_hstring *res = duk_heap_string_intern_u32(thr->heap, val);
  37837. if (!res) {
  37838. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, "failed to intern string");
  37839. }
  37840. return res;
  37841. }
  37842. /* find and remove string from stringtable; caller must free the string itself */
  37843. DUK_INTERNAL void duk_heap_string_remove(duk_heap *heap, duk_hstring *h) {
  37844. DUK_DDD(DUK_DDDPRINT("remove string from stringtable: %!O", (duk_heaphdr *) h));
  37845. #if defined(DUK_USE_STRTAB_CHAIN)
  37846. duk__remove_matching_hstring_chain(heap, h);
  37847. #elif defined(DUK_USE_STRTAB_PROBE)
  37848. duk__remove_matching_hstring_probe(heap,
  37849. #if defined(DUK_USE_HEAPPTR16)
  37850. heap->strtable16,
  37851. #else
  37852. heap->strtable,
  37853. #endif
  37854. heap->st_size,
  37855. h);
  37856. #else
  37857. #error internal error, invalid strtab options
  37858. #endif
  37859. }
  37860. #if defined(DUK_USE_MARK_AND_SWEEP) && defined(DUK_USE_MS_STRINGTABLE_RESIZE)
  37861. DUK_INTERNAL void duk_heap_force_strtab_resize(duk_heap *heap) {
  37862. /* Force a resize so that DELETED entries are eliminated.
  37863. * Another option would be duk__recheck_strtab_size_probe();
  37864. * but since that happens on every intern anyway, this whole
  37865. * check can now be disabled.
  37866. */
  37867. #if defined(DUK_USE_STRTAB_CHAIN)
  37868. DUK_UNREF(heap);
  37869. #elif defined(DUK_USE_STRTAB_PROBE)
  37870. duk__resize_strtab_probe(heap);
  37871. #endif
  37872. }
  37873. #endif
  37874. #if defined(DUK_USE_STRTAB_CHAIN)
  37875. DUK_INTERNAL void duk_heap_free_strtab(duk_heap *heap) {
  37876. /* Free strings in the stringtable and any allocations needed
  37877. * by the stringtable itself.
  37878. */
  37879. duk_uint_fast32_t i, j;
  37880. duk_strtab_entry *e;
  37881. #if defined(DUK_USE_HEAPPTR16)
  37882. duk_uint16_t *lst;
  37883. duk_uint16_t null16 = heap->heapptr_null16;
  37884. #else
  37885. duk_hstring **lst;
  37886. #endif
  37887. duk_hstring *h;
  37888. for (i = 0; i < DUK_STRTAB_CHAIN_SIZE; i++) {
  37889. e = heap->strtable + i;
  37890. if (e->listlen > 0) {
  37891. #if defined(DUK_USE_HEAPPTR16)
  37892. lst = (duk_uint16_t *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.strlist16);
  37893. #else
  37894. lst = e->u.strlist;
  37895. #endif
  37896. DUK_ASSERT(lst != NULL);
  37897. for (j = 0; j < e->listlen; j++) {
  37898. #if defined(DUK_USE_HEAPPTR16)
  37899. h = DUK_USE_HEAPPTR_DEC16(heap->heap_udata, lst[j]);
  37900. lst[j] = null16;
  37901. #else
  37902. h = lst[j];
  37903. lst[j] = NULL;
  37904. #endif
  37905. /* strings may have inner refs (extdata) in some cases */
  37906. if (h != NULL) {
  37907. duk_free_hstring_inner(heap, h);
  37908. DUK_FREE(heap, h);
  37909. }
  37910. }
  37911. #if defined(DUK_USE_HEAPPTR16)
  37912. e->u.strlist16 = null16;
  37913. #else
  37914. e->u.strlist = NULL;
  37915. #endif
  37916. DUK_FREE(heap, lst);
  37917. } else {
  37918. #if defined(DUK_USE_HEAPPTR16)
  37919. h = DUK_USE_HEAPPTR_DEC16(heap->heap_udata, e->u.str16);
  37920. e->u.str16 = null16;
  37921. #else
  37922. h = e->u.str;
  37923. e->u.str = NULL;
  37924. #endif
  37925. if (h != NULL) {
  37926. duk_free_hstring_inner(heap, h);
  37927. DUK_FREE(heap, h);
  37928. }
  37929. }
  37930. e->listlen = 0;
  37931. }
  37932. }
  37933. #endif /* DUK_USE_STRTAB_CHAIN */
  37934. #if defined(DUK_USE_STRTAB_PROBE)
  37935. DUK_INTERNAL void duk_heap_free_strtab(duk_heap *heap) {
  37936. duk_uint_fast32_t i;
  37937. duk_hstring *h;
  37938. #if defined(DUK_USE_HEAPPTR16)
  37939. if (heap->strtable16) {
  37940. #else
  37941. if (heap->strtable) {
  37942. #endif
  37943. for (i = 0; i < (duk_uint_fast32_t) heap->st_size; i++) {
  37944. #if defined(DUK_USE_HEAPPTR16)
  37945. h = (duk_hstring *) DUK_USE_HEAPPTR_DEC16(heap->heap_udata, heap->strtable16[i]);
  37946. #else
  37947. h = heap->strtable[i];
  37948. #endif
  37949. if (h == NULL || h == DUK_STRTAB_DELETED_MARKER(heap)) {
  37950. continue;
  37951. }
  37952. DUK_ASSERT(h != NULL);
  37953. /* strings may have inner refs (extdata) in some cases */
  37954. duk_free_hstring_inner(heap, h);
  37955. DUK_FREE(heap, h);
  37956. #if 0 /* not strictly necessary */
  37957. heap->strtable[i] = NULL;
  37958. #endif
  37959. }
  37960. #if defined(DUK_USE_HEAPPTR16)
  37961. DUK_FREE(heap, heap->strtable16);
  37962. #else
  37963. DUK_FREE(heap, heap->strtable);
  37964. #endif
  37965. #if 0 /* not strictly necessary */
  37966. heap->strtable = NULL;
  37967. #endif
  37968. }
  37969. }
  37970. #endif /* DUK_USE_STRTAB_PROBE */
  37971. /* Undefine local defines */
  37972. #undef DUK__HASH_INITIAL
  37973. #undef DUK__HASH_PROBE_STEP
  37974. #undef DUK__DELETED_MARKER
  37975. #line 1 "duk_hobject_alloc.c"
  37976. /*
  37977. * Hobject allocation.
  37978. *
  37979. * Provides primitive allocation functions for all object types (plain object,
  37980. * compiled function, native function, thread). The object return is not yet
  37981. * in "heap allocated" list and has a refcount of zero, so caller must careful.
  37982. */
  37983. /* include removed: duk_internal.h */
  37984. DUK_LOCAL void duk__init_object_parts(duk_heap *heap, duk_hobject *obj, duk_uint_t hobject_flags) {
  37985. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  37986. DUK_HOBJECT_SET_PROPS(heap, obj, NULL);
  37987. #endif
  37988. /* XXX: macro? sets both heaphdr and object flags */
  37989. obj->hdr.h_flags = hobject_flags;
  37990. DUK_HEAPHDR_SET_TYPE(&obj->hdr, DUK_HTYPE_OBJECT); /* also goes into flags */
  37991. #if defined(DUK_USE_HEAPPTR16)
  37992. /* Zero encoded pointer is required to match NULL */
  37993. DUK_HEAPHDR_SET_NEXT(heap, &obj->hdr, NULL);
  37994. #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
  37995. DUK_HEAPHDR_SET_PREV(heap, &obj->hdr, NULL);
  37996. #endif
  37997. #endif
  37998. DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap, &obj->hdr);
  37999. /*
  38000. * obj->props is intentionally left as NULL, and duk_hobject_props.c must deal
  38001. * with this properly. This is intentional: empty objects consume a minimum
  38002. * amount of memory. Further, an initial allocation might fail and cause
  38003. * 'obj' to "leak" (require a mark-and-sweep) since it is not reachable yet.
  38004. */
  38005. }
  38006. /*
  38007. * Allocate an duk_hobject.
  38008. *
  38009. * The allocated object has no allocation for properties; the caller may
  38010. * want to force a resize if a desired size is known.
  38011. *
  38012. * The allocated object has zero reference count and is not reachable.
  38013. * The caller MUST make the object reachable and increase its reference
  38014. * count before invoking any operation that might require memory allocation.
  38015. */
  38016. DUK_INTERNAL duk_hobject *duk_hobject_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
  38017. duk_hobject *res;
  38018. DUK_ASSERT(heap != NULL);
  38019. /* different memory layout, alloc size, and init */
  38020. DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_COMPILEDFUNCTION) == 0);
  38021. DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_NATIVEFUNCTION) == 0);
  38022. DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_THREAD) == 0);
  38023. res = (duk_hobject *) DUK_ALLOC(heap, sizeof(duk_hobject));
  38024. if (!res) {
  38025. return NULL;
  38026. }
  38027. DUK_MEMZERO(res, sizeof(duk_hobject));
  38028. duk__init_object_parts(heap, res, hobject_flags);
  38029. return res;
  38030. }
  38031. DUK_INTERNAL duk_hcompiledfunction *duk_hcompiledfunction_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
  38032. duk_hcompiledfunction *res;
  38033. res = (duk_hcompiledfunction *) DUK_ALLOC(heap, sizeof(duk_hcompiledfunction));
  38034. if (!res) {
  38035. return NULL;
  38036. }
  38037. DUK_MEMZERO(res, sizeof(duk_hcompiledfunction));
  38038. duk__init_object_parts(heap, &res->obj, hobject_flags);
  38039. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  38040. #ifdef DUK_HEAPPTR16
  38041. /* NULL pointer is required to encode to zero, so memset is enough. */
  38042. #else
  38043. res->data = NULL;
  38044. res->funcs = NULL;
  38045. res->bytecode = NULL;
  38046. #endif
  38047. #endif
  38048. return res;
  38049. }
  38050. DUK_INTERNAL duk_hnativefunction *duk_hnativefunction_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
  38051. duk_hnativefunction *res;
  38052. res = (duk_hnativefunction *) DUK_ALLOC(heap, sizeof(duk_hnativefunction));
  38053. if (!res) {
  38054. return NULL;
  38055. }
  38056. DUK_MEMZERO(res, sizeof(duk_hnativefunction));
  38057. duk__init_object_parts(heap, &res->obj, hobject_flags);
  38058. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  38059. res->func = NULL;
  38060. #endif
  38061. return res;
  38062. }
  38063. DUK_INTERNAL duk_hbufferobject *duk_hbufferobject_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
  38064. duk_hbufferobject *res;
  38065. res = (duk_hbufferobject *) DUK_ALLOC(heap, sizeof(duk_hbufferobject));
  38066. if (!res) {
  38067. return NULL;
  38068. }
  38069. DUK_MEMZERO(res, sizeof(duk_hbufferobject));
  38070. duk__init_object_parts(heap, &res->obj, hobject_flags);
  38071. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  38072. res->buf = NULL;
  38073. #endif
  38074. DUK_ASSERT_HBUFFEROBJECT_VALID(res);
  38075. return res;
  38076. }
  38077. /*
  38078. * Allocate a new thread.
  38079. *
  38080. * Leaves the built-ins array uninitialized. The caller must either
  38081. * initialize a new global context or share existing built-ins from
  38082. * another thread.
  38083. */
  38084. DUK_INTERNAL duk_hthread *duk_hthread_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
  38085. duk_hthread *res;
  38086. res = (duk_hthread *) DUK_ALLOC(heap, sizeof(duk_hthread));
  38087. if (!res) {
  38088. return NULL;
  38089. }
  38090. DUK_MEMZERO(res, sizeof(duk_hthread));
  38091. duk__init_object_parts(heap, &res->obj, hobject_flags);
  38092. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  38093. res->heap = NULL;
  38094. res->valstack = NULL;
  38095. res->valstack_end = NULL;
  38096. res->valstack_bottom = NULL;
  38097. res->valstack_top = NULL;
  38098. res->callstack = NULL;
  38099. res->catchstack = NULL;
  38100. res->resumer = NULL;
  38101. res->compile_ctx = NULL,
  38102. res->strs = NULL;
  38103. {
  38104. int i;
  38105. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  38106. res->builtins[i] = NULL;
  38107. }
  38108. }
  38109. #endif
  38110. /* when nothing is running, API calls are in non-strict mode */
  38111. DUK_ASSERT(res->strict == 0);
  38112. res->heap = heap;
  38113. res->valstack_max = DUK_VALSTACK_DEFAULT_MAX;
  38114. res->callstack_max = DUK_CALLSTACK_DEFAULT_MAX;
  38115. res->catchstack_max = DUK_CATCHSTACK_DEFAULT_MAX;
  38116. return res;
  38117. }
  38118. #if 0 /* unused now */
  38119. DUK_INTERNAL duk_hobject *duk_hobject_alloc_checked(duk_hthread *thr, duk_uint_t hobject_flags) {
  38120. duk_hobject *res = duk_hobject_alloc(thr->heap, hobject_flags);
  38121. if (!res) {
  38122. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, "failed to allocate an object");
  38123. }
  38124. return res;
  38125. }
  38126. #endif
  38127. #line 1 "duk_hobject_enum.c"
  38128. /*
  38129. * Hobject enumeration support.
  38130. *
  38131. * Creates an internal enumeration state object to be used e.g. with for-in
  38132. * enumeration. The state object contains a snapshot of target object keys
  38133. * and internal control state for enumeration. Enumerator flags allow caller
  38134. * to e.g. request internal/non-enumerable properties, and to enumerate only
  38135. * "own" properties.
  38136. *
  38137. * Also creates the result value for e.g. Object.keys() based on the same
  38138. * internal structure.
  38139. *
  38140. * This snapshot-based enumeration approach is used to simplify enumeration:
  38141. * non-snapshot-based approaches are difficult to reconcile with mutating
  38142. * the enumeration target, running multiple long-lived enumerators at the
  38143. * same time, garbage collection details, etc. The downside is that the
  38144. * enumerator object is memory inefficient especially for iterating arrays.
  38145. */
  38146. /* include removed: duk_internal.h */
  38147. /* XXX: identify enumeration target with an object index (not top of stack) */
  38148. /* must match exactly the number of internal properties inserted to enumerator */
  38149. #define DUK__ENUM_START_INDEX 2
  38150. DUK_LOCAL const duk_uint16_t duk__bufferobject_virtual_props[] = {
  38151. DUK_STRIDX_LENGTH,
  38152. DUK_STRIDX_BYTE_LENGTH,
  38153. DUK_STRIDX_BYTE_OFFSET,
  38154. DUK_STRIDX_BYTES_PER_ELEMENT
  38155. };
  38156. /*
  38157. * Helper to sort array index keys. The keys are in the enumeration object
  38158. * entry part, starting from DUK__ENUM_START_INDEX, and the entry part is dense.
  38159. *
  38160. * We use insertion sort because it is simple (leading to compact code,)
  38161. * works nicely in-place, and minimizes operations if data is already sorted
  38162. * or nearly sorted (which is a very common case here). It also minimizes
  38163. * the use of element comparisons in general. This is nice because element
  38164. * comparisons here involve re-parsing the string keys into numbers each
  38165. * time, which is naturally very expensive.
  38166. *
  38167. * Note that the entry part values are all "true", e.g.
  38168. *
  38169. * "1" -> true, "3" -> true, "2" -> true
  38170. *
  38171. * so it suffices to only work in the key part without exchanging any keys,
  38172. * simplifying the sort.
  38173. *
  38174. * http://en.wikipedia.org/wiki/Insertion_sort
  38175. *
  38176. * (Compiles to about 160 bytes now as a stand-alone function.)
  38177. */
  38178. DUK_LOCAL void duk__sort_array_indices(duk_hthread *thr, duk_hobject *h_obj) {
  38179. duk_hstring **keys;
  38180. duk_hstring **p_curr, **p_insert, **p_end;
  38181. duk_hstring *h_curr;
  38182. duk_uarridx_t val_highest, val_curr, val_insert;
  38183. DUK_ASSERT(h_obj != NULL);
  38184. DUK_ASSERT(DUK_HOBJECT_GET_ENEXT(h_obj) >= 2); /* control props */
  38185. DUK_UNREF(thr);
  38186. if (DUK_HOBJECT_GET_ENEXT(h_obj) <= 1 + DUK__ENUM_START_INDEX) {
  38187. return;
  38188. }
  38189. keys = DUK_HOBJECT_E_GET_KEY_BASE(thr->heap, h_obj);
  38190. p_end = keys + DUK_HOBJECT_GET_ENEXT(h_obj);
  38191. keys += DUK__ENUM_START_INDEX;
  38192. DUK_DDD(DUK_DDDPRINT("keys=%p, p_end=%p (after skipping enum props)",
  38193. (void *) keys, (void *) p_end));
  38194. #ifdef DUK_USE_DDDPRINT
  38195. {
  38196. duk_uint_fast32_t i;
  38197. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(h_obj); i++) {
  38198. DUK_DDD(DUK_DDDPRINT("initial: %ld %p -> %!O",
  38199. (long) i,
  38200. (void *) DUK_HOBJECT_E_GET_KEY_PTR(thr->heap, h_obj, i),
  38201. (duk_heaphdr *) DUK_HOBJECT_E_GET_KEY(thr->heap, h_obj, i)));
  38202. }
  38203. }
  38204. #endif
  38205. val_highest = DUK_HSTRING_GET_ARRIDX_SLOW(keys[0]);
  38206. for (p_curr = keys + 1; p_curr < p_end; p_curr++) {
  38207. DUK_ASSERT(*p_curr != NULL);
  38208. val_curr = DUK_HSTRING_GET_ARRIDX_SLOW(*p_curr);
  38209. if (val_curr >= val_highest) {
  38210. DUK_DDD(DUK_DDDPRINT("p_curr=%p, p_end=%p, val_highest=%ld, val_curr=%ld -> "
  38211. "already in correct order, next",
  38212. (void *) p_curr, (void *) p_end, (long) val_highest, (long) val_curr));
  38213. val_highest = val_curr;
  38214. continue;
  38215. }
  38216. DUK_DDD(DUK_DDDPRINT("p_curr=%p, p_end=%p, val_highest=%ld, val_curr=%ld -> "
  38217. "needs to be inserted",
  38218. (void *) p_curr, (void *) p_end, (long) val_highest, (long) val_curr));
  38219. /* Needs to be inserted; scan backwards, since we optimize
  38220. * for the case where elements are nearly in order.
  38221. */
  38222. p_insert = p_curr - 1;
  38223. for (;;) {
  38224. val_insert = DUK_HSTRING_GET_ARRIDX_SLOW(*p_insert);
  38225. if (val_insert < val_curr) {
  38226. DUK_DDD(DUK_DDDPRINT("p_insert=%p, val_insert=%ld, val_curr=%ld -> insert after this",
  38227. (void *) p_insert, (long) val_insert, (long) val_curr));
  38228. p_insert++;
  38229. break;
  38230. }
  38231. if (p_insert == keys) {
  38232. DUK_DDD(DUK_DDDPRINT("p_insert=%p -> out of keys, insert to beginning", (void *) p_insert));
  38233. break;
  38234. }
  38235. DUK_DDD(DUK_DDDPRINT("p_insert=%p, val_insert=%ld, val_curr=%ld -> search backwards",
  38236. (void *) p_insert, (long) val_insert, (long) val_curr));
  38237. p_insert--;
  38238. }
  38239. DUK_DDD(DUK_DDDPRINT("final p_insert=%p", (void *) p_insert));
  38240. /* .-- p_insert .-- p_curr
  38241. * v v
  38242. * | ... | insert | ... | curr
  38243. */
  38244. h_curr = *p_curr;
  38245. DUK_DDD(DUK_DDDPRINT("memmove: dest=%p, src=%p, size=%ld, h_curr=%p",
  38246. (void *) (p_insert + 1), (void *) p_insert,
  38247. (long) (p_curr - p_insert), (void *) h_curr));
  38248. DUK_MEMMOVE((void *) (p_insert + 1),
  38249. (void *) p_insert,
  38250. (size_t) ((p_curr - p_insert) * sizeof(duk_hstring *)));
  38251. *p_insert = h_curr;
  38252. /* keep val_highest */
  38253. }
  38254. #ifdef DUK_USE_DDDPRINT
  38255. {
  38256. duk_uint_fast32_t i;
  38257. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(h_obj); i++) {
  38258. DUK_DDD(DUK_DDDPRINT("final: %ld %p -> %!O",
  38259. (long) i,
  38260. (void *) DUK_HOBJECT_E_GET_KEY_PTR(thr->heap, h_obj, i),
  38261. (duk_heaphdr *) DUK_HOBJECT_E_GET_KEY(thr->heap, h_obj, i)));
  38262. }
  38263. }
  38264. #endif
  38265. }
  38266. /*
  38267. * Create an internal enumerator object E, which has its keys ordered
  38268. * to match desired enumeration ordering. Also initialize internal control
  38269. * properties for enumeration.
  38270. *
  38271. * Note: if an array was used to hold enumeration keys instead, an array
  38272. * scan would be needed to eliminate duplicates found in the prototype chain.
  38273. */
  38274. DUK_INTERNAL void duk_hobject_enumerator_create(duk_context *ctx, duk_small_uint_t enum_flags) {
  38275. duk_hthread *thr = (duk_hthread *) ctx;
  38276. duk_hobject *enum_target;
  38277. duk_hobject *curr;
  38278. duk_hobject *res;
  38279. #if defined(DUK_USE_ES6_PROXY)
  38280. duk_hobject *h_proxy_target;
  38281. duk_hobject *h_proxy_handler;
  38282. duk_hobject *h_trap_result;
  38283. #endif
  38284. duk_uint_fast32_t i, len; /* used for array, stack, and entry indices */
  38285. DUK_ASSERT(ctx != NULL);
  38286. DUK_DDD(DUK_DDDPRINT("create enumerator, stack top: %ld", (long) duk_get_top(ctx)));
  38287. enum_target = duk_require_hobject(ctx, -1);
  38288. DUK_ASSERT(enum_target != NULL);
  38289. duk_push_object_internal(ctx);
  38290. res = duk_require_hobject(ctx, -1);
  38291. DUK_DDD(DUK_DDDPRINT("created internal object"));
  38292. /* [enum_target res] */
  38293. /* Target must be stored so that we can recheck whether or not
  38294. * keys still exist when we enumerate. This is not done if the
  38295. * enumeration result comes from a proxy trap as there is no
  38296. * real object to check against.
  38297. */
  38298. duk_push_hobject(ctx, enum_target);
  38299. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_INT_TARGET);
  38300. /* Initialize index so that we skip internal control keys. */
  38301. duk_push_int(ctx, DUK__ENUM_START_INDEX);
  38302. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_INT_NEXT);
  38303. /*
  38304. * Proxy object handling
  38305. */
  38306. #if defined(DUK_USE_ES6_PROXY)
  38307. if (DUK_LIKELY((enum_flags & DUK_ENUM_NO_PROXY_BEHAVIOR) != 0)) {
  38308. goto skip_proxy;
  38309. }
  38310. if (DUK_LIKELY(!duk_hobject_proxy_check(thr,
  38311. enum_target,
  38312. &h_proxy_target,
  38313. &h_proxy_handler))) {
  38314. goto skip_proxy;
  38315. }
  38316. DUK_DDD(DUK_DDDPRINT("proxy enumeration"));
  38317. duk_push_hobject(ctx, h_proxy_handler);
  38318. if (!duk_get_prop_stridx(ctx, -1, DUK_STRIDX_ENUMERATE)) {
  38319. /* No need to replace the 'enum_target' value in stack, only the
  38320. * enum_target reference. This also ensures that the original
  38321. * enum target is reachable, which keeps the proxy and the proxy
  38322. * target reachable. We do need to replace the internal _Target.
  38323. */
  38324. DUK_DDD(DUK_DDDPRINT("no enumerate trap, enumerate proxy target instead"));
  38325. DUK_DDD(DUK_DDDPRINT("h_proxy_target=%!O", (duk_heaphdr *) h_proxy_target));
  38326. enum_target = h_proxy_target;
  38327. duk_push_hobject(ctx, enum_target); /* -> [ ... enum_target res handler undefined target ] */
  38328. duk_put_prop_stridx(ctx, -4, DUK_STRIDX_INT_TARGET);
  38329. duk_pop_2(ctx); /* -> [ ... enum_target res ] */
  38330. goto skip_proxy;
  38331. }
  38332. /* [ ... enum_target res handler trap ] */
  38333. duk_insert(ctx, -2);
  38334. duk_push_hobject(ctx, h_proxy_target); /* -> [ ... enum_target res trap handler target ] */
  38335. duk_call_method(ctx, 1 /*nargs*/); /* -> [ ... enum_target res trap_result ] */
  38336. h_trap_result = duk_require_hobject(ctx, -1);
  38337. DUK_UNREF(h_trap_result);
  38338. /* Copy trap result keys into the enumerator object. */
  38339. len = (duk_uint_fast32_t) duk_get_length(ctx, -1);
  38340. for (i = 0; i < len; i++) {
  38341. /* XXX: not sure what the correct semantic details are here,
  38342. * e.g. handling of missing values (gaps), handling of non-array
  38343. * trap results, etc.
  38344. *
  38345. * For keys, we simply skip non-string keys which seems to be
  38346. * consistent with how e.g. Object.keys() will process proxy trap
  38347. * results (ES6 draft, Section 19.1.2.14).
  38348. */
  38349. if (duk_get_prop_index(ctx, -1, i) && duk_is_string(ctx, -1)) {
  38350. /* [ ... enum_target res trap_result val ] */
  38351. duk_push_true(ctx);
  38352. /* [ ... enum_target res trap_result val true ] */
  38353. duk_put_prop(ctx, -4);
  38354. } else {
  38355. duk_pop(ctx);
  38356. }
  38357. }
  38358. /* [ ... enum_target res trap_result ] */
  38359. duk_pop(ctx);
  38360. duk_remove(ctx, -2);
  38361. /* [ ... res ] */
  38362. /* The internal _Target property is kept pointing to the original
  38363. * enumeration target (the proxy object), so that the enumerator
  38364. * 'next' operation can read property values if so requested. The
  38365. * fact that the _Target is a proxy disables key existence check
  38366. * during enumeration.
  38367. */
  38368. DUK_DDD(DUK_DDDPRINT("proxy enumeration, final res: %!O", (duk_heaphdr *) res));
  38369. goto compact_and_return;
  38370. skip_proxy:
  38371. #endif /* DUK_USE_ES6_PROXY */
  38372. curr = enum_target;
  38373. while (curr) {
  38374. /*
  38375. * Virtual properties.
  38376. *
  38377. * String and buffer indices are virtual and always enumerable,
  38378. * 'length' is virtual and non-enumerable. Array and arguments
  38379. * object props have special behavior but are concrete.
  38380. */
  38381. if (DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(curr) ||
  38382. DUK_HOBJECT_IS_BUFFEROBJECT(curr)) {
  38383. /* String and buffer enumeration behavior is identical now,
  38384. * so use shared handler.
  38385. */
  38386. if (DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(curr)) {
  38387. duk_hstring *h_val;
  38388. h_val = duk_hobject_get_internal_value_string(thr->heap, curr);
  38389. DUK_ASSERT(h_val != NULL); /* string objects must not created without internal value */
  38390. len = (duk_uint_fast32_t) DUK_HSTRING_GET_CHARLEN(h_val);
  38391. } else {
  38392. duk_hbufferobject *h_bufobj;
  38393. DUK_ASSERT(DUK_HOBJECT_IS_BUFFEROBJECT(curr));
  38394. h_bufobj = (duk_hbufferobject *) curr;
  38395. if (h_bufobj == NULL) {
  38396. /* Neutered buffer, zero length seems
  38397. * like good behavior here.
  38398. */
  38399. len = 0;
  38400. } else {
  38401. /* There's intentionally no check for
  38402. * current underlying buffer length.
  38403. */
  38404. len = (duk_uint_fast32_t) (h_bufobj->length >> h_bufobj->shift);
  38405. }
  38406. }
  38407. for (i = 0; i < len; i++) {
  38408. duk_hstring *k;
  38409. k = duk_heap_string_intern_u32_checked(thr, i);
  38410. DUK_ASSERT(k);
  38411. duk_push_hstring(ctx, k);
  38412. duk_push_true(ctx);
  38413. /* [enum_target res key true] */
  38414. duk_put_prop(ctx, -3);
  38415. /* [enum_target res] */
  38416. }
  38417. /* 'length' and other virtual properties are not
  38418. * enumerable, but are included if non-enumerable
  38419. * properties are requested.
  38420. */
  38421. if (enum_flags & DUK_ENUM_INCLUDE_NONENUMERABLE) {
  38422. duk_uint_fast32_t n;
  38423. if (DUK_HOBJECT_IS_BUFFEROBJECT(curr)) {
  38424. n = sizeof(duk__bufferobject_virtual_props) / sizeof(duk_uint16_t);
  38425. } else {
  38426. DUK_ASSERT(DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(curr));
  38427. DUK_ASSERT(duk__bufferobject_virtual_props[0] == DUK_STRIDX_LENGTH);
  38428. n = 1; /* only 'length' */
  38429. }
  38430. for (i = 0; i < n; i++) {
  38431. duk_push_hstring_stridx(ctx, duk__bufferobject_virtual_props[i]);
  38432. duk_push_true(ctx);
  38433. duk_put_prop(ctx, -3);
  38434. }
  38435. }
  38436. } else if (DUK_HOBJECT_HAS_EXOTIC_DUKFUNC(curr)) {
  38437. if (enum_flags & DUK_ENUM_INCLUDE_NONENUMERABLE) {
  38438. duk_push_hstring_stridx(ctx, DUK_STRIDX_LENGTH);
  38439. duk_push_true(ctx);
  38440. duk_put_prop(ctx, -3);
  38441. }
  38442. }
  38443. /*
  38444. * Array part
  38445. *
  38446. * Note: ordering between array and entry part must match 'abandon array'
  38447. * behavior in duk_hobject_props.c: key order after an array is abandoned
  38448. * must be the same.
  38449. */
  38450. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(curr); i++) {
  38451. duk_hstring *k;
  38452. duk_tval *tv;
  38453. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, curr, i);
  38454. if (DUK_TVAL_IS_UNDEFINED_UNUSED(tv)) {
  38455. continue;
  38456. }
  38457. k = duk_heap_string_intern_u32_checked(thr, i);
  38458. DUK_ASSERT(k);
  38459. duk_push_hstring(ctx, k);
  38460. duk_push_true(ctx);
  38461. /* [enum_target res key true] */
  38462. duk_put_prop(ctx, -3);
  38463. /* [enum_target res] */
  38464. }
  38465. /*
  38466. * Entries part
  38467. */
  38468. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(curr); i++) {
  38469. duk_hstring *k;
  38470. k = DUK_HOBJECT_E_GET_KEY(thr->heap, curr, i);
  38471. if (!k) {
  38472. continue;
  38473. }
  38474. if (!DUK_HOBJECT_E_SLOT_IS_ENUMERABLE(thr->heap, curr, i) &&
  38475. !(enum_flags & DUK_ENUM_INCLUDE_NONENUMERABLE)) {
  38476. continue;
  38477. }
  38478. if (DUK_HSTRING_HAS_INTERNAL(k) &&
  38479. !(enum_flags & DUK_ENUM_INCLUDE_INTERNAL)) {
  38480. continue;
  38481. }
  38482. if ((enum_flags & DUK_ENUM_ARRAY_INDICES_ONLY) &&
  38483. (DUK_HSTRING_GET_ARRIDX_SLOW(k) == DUK_HSTRING_NO_ARRAY_INDEX)) {
  38484. continue;
  38485. }
  38486. DUK_ASSERT(DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, curr, i) ||
  38487. !DUK_TVAL_IS_UNDEFINED_UNUSED(&DUK_HOBJECT_E_GET_VALUE_PTR(thr->heap, curr, i)->v));
  38488. duk_push_hstring(ctx, k);
  38489. duk_push_true(ctx);
  38490. /* [enum_target res key true] */
  38491. duk_put_prop(ctx, -3);
  38492. /* [enum_target res] */
  38493. }
  38494. if (enum_flags & DUK_ENUM_OWN_PROPERTIES_ONLY) {
  38495. break;
  38496. }
  38497. curr = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, curr);
  38498. }
  38499. /* [enum_target res] */
  38500. duk_remove(ctx, -2);
  38501. /* [res] */
  38502. if ((enum_flags & (DUK_ENUM_ARRAY_INDICES_ONLY | DUK_ENUM_SORT_ARRAY_INDICES)) ==
  38503. (DUK_ENUM_ARRAY_INDICES_ONLY | DUK_ENUM_SORT_ARRAY_INDICES)) {
  38504. /*
  38505. * Some E5/E5.1 algorithms require that array indices are iterated
  38506. * in a strictly ascending order. This is the case for e.g.
  38507. * Array.prototype.forEach() and JSON.stringify() PropertyList
  38508. * handling.
  38509. *
  38510. * To ensure this property for arrays with an array part (and
  38511. * arbitrary objects too, since e.g. forEach() can be applied
  38512. * to an array), the caller can request that we sort the keys
  38513. * here.
  38514. */
  38515. /* XXX: avoid this at least when enum_target is an Array, it has an
  38516. * array part, and no ancestor properties were included? Not worth
  38517. * it for JSON, but maybe worth it for forEach().
  38518. */
  38519. /* XXX: may need a 'length' filter for forEach()
  38520. */
  38521. DUK_DDD(DUK_DDDPRINT("sort array indices by caller request"));
  38522. duk__sort_array_indices(thr, res);
  38523. }
  38524. #if defined(DUK_USE_ES6_PROXY)
  38525. compact_and_return:
  38526. #endif
  38527. /* compact; no need to seal because object is internal */
  38528. duk_hobject_compact_props(thr, res);
  38529. DUK_DDD(DUK_DDDPRINT("created enumerator object: %!iT", (duk_tval *) duk_get_tval(ctx, -1)));
  38530. }
  38531. /*
  38532. * Returns non-zero if a key and/or value was enumerated, and:
  38533. *
  38534. * [enum] -> [key] (get_value == 0)
  38535. * [enum] -> [key value] (get_value == 1)
  38536. *
  38537. * Returns zero without pushing anything on the stack otherwise.
  38538. */
  38539. DUK_INTERNAL duk_bool_t duk_hobject_enumerator_next(duk_context *ctx, duk_bool_t get_value) {
  38540. duk_hthread *thr = (duk_hthread *) ctx;
  38541. duk_hobject *e;
  38542. duk_hobject *enum_target;
  38543. duk_hstring *res = NULL;
  38544. duk_uint_fast32_t idx;
  38545. duk_bool_t check_existence;
  38546. DUK_ASSERT(ctx != NULL);
  38547. /* [... enum] */
  38548. e = duk_require_hobject(ctx, -1);
  38549. /* XXX use get tval ptr, more efficient */
  38550. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_NEXT);
  38551. idx = (duk_uint_fast32_t) duk_require_uint(ctx, -1);
  38552. duk_pop(ctx);
  38553. DUK_DDD(DUK_DDDPRINT("enumeration: index is: %ld", (long) idx));
  38554. /* Enumeration keys are checked against the enumeration target (to see
  38555. * that they still exist). In the proxy enumeration case _Target will
  38556. * be the proxy, and checking key existence against the proxy is not
  38557. * required (or sensible, as the keys may be fully virtual).
  38558. */
  38559. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_TARGET);
  38560. enum_target = duk_require_hobject(ctx, -1);
  38561. DUK_ASSERT(enum_target != NULL);
  38562. #if defined(DUK_USE_ES6_PROXY)
  38563. check_existence = (!DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(enum_target));
  38564. #else
  38565. check_existence = 1;
  38566. #endif
  38567. duk_pop(ctx); /* still reachable */
  38568. DUK_DDD(DUK_DDDPRINT("getting next enum value, enum_target=%!iO, enumerator=%!iT",
  38569. (duk_heaphdr *) enum_target, (duk_tval *) duk_get_tval(ctx, -1)));
  38570. /* no array part */
  38571. for (;;) {
  38572. duk_hstring *k;
  38573. if (idx >= DUK_HOBJECT_GET_ENEXT(e)) {
  38574. DUK_DDD(DUK_DDDPRINT("enumeration: ran out of elements"));
  38575. break;
  38576. }
  38577. /* we know these because enum objects are internally created */
  38578. k = DUK_HOBJECT_E_GET_KEY(thr->heap, e, idx);
  38579. DUK_ASSERT(k != NULL);
  38580. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, e, idx));
  38581. DUK_ASSERT(!DUK_TVAL_IS_UNDEFINED_UNUSED(&DUK_HOBJECT_E_GET_VALUE(thr->heap, e, idx).v));
  38582. idx++;
  38583. /* recheck that the property still exists */
  38584. if (check_existence && !duk_hobject_hasprop_raw(thr, enum_target, k)) {
  38585. DUK_DDD(DUK_DDDPRINT("property deleted during enumeration, skip"));
  38586. continue;
  38587. }
  38588. DUK_DDD(DUK_DDDPRINT("enumeration: found element, key: %!O", (duk_heaphdr *) k));
  38589. res = k;
  38590. break;
  38591. }
  38592. DUK_DDD(DUK_DDDPRINT("enumeration: updating next index to %ld", (long) idx));
  38593. duk_push_u32(ctx, (duk_uint32_t) idx);
  38594. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_INT_NEXT);
  38595. /* [... enum] */
  38596. if (res) {
  38597. duk_push_hstring(ctx, res);
  38598. if (get_value) {
  38599. duk_push_hobject(ctx, enum_target);
  38600. duk_dup(ctx, -2); /* -> [... enum key enum_target key] */
  38601. duk_get_prop(ctx, -2); /* -> [... enum key enum_target val] */
  38602. duk_remove(ctx, -2); /* -> [... enum key val] */
  38603. duk_remove(ctx, -3); /* -> [... key val] */
  38604. } else {
  38605. duk_remove(ctx, -2); /* -> [... key] */
  38606. }
  38607. return 1;
  38608. } else {
  38609. duk_pop(ctx); /* -> [...] */
  38610. return 0;
  38611. }
  38612. }
  38613. /*
  38614. * Get enumerated keys in an Ecmascript array. Matches Object.keys() behavior
  38615. * described in E5 Section 15.2.3.14.
  38616. */
  38617. DUK_INTERNAL duk_ret_t duk_hobject_get_enumerated_keys(duk_context *ctx, duk_small_uint_t enum_flags) {
  38618. duk_hthread *thr = (duk_hthread *) ctx;
  38619. duk_hobject *e;
  38620. duk_uint_fast32_t i;
  38621. duk_uint_fast32_t idx;
  38622. DUK_ASSERT(ctx != NULL);
  38623. DUK_ASSERT(duk_get_hobject(ctx, -1) != NULL);
  38624. DUK_UNREF(thr);
  38625. /* Create a temporary enumerator to get the (non-duplicated) key list;
  38626. * the enumerator state is initialized without being needed, but that
  38627. * has little impact.
  38628. */
  38629. duk_hobject_enumerator_create(ctx, enum_flags);
  38630. duk_push_array(ctx);
  38631. /* [enum_target enum res] */
  38632. e = duk_require_hobject(ctx, -2);
  38633. DUK_ASSERT(e != NULL);
  38634. idx = 0;
  38635. for (i = DUK__ENUM_START_INDEX; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(e); i++) {
  38636. duk_hstring *k;
  38637. k = DUK_HOBJECT_E_GET_KEY(thr->heap, e, i);
  38638. DUK_ASSERT(k); /* enumerator must have no keys deleted */
  38639. /* [enum_target enum res] */
  38640. duk_push_hstring(ctx, k);
  38641. duk_put_prop_index(ctx, -2, idx);
  38642. idx++;
  38643. }
  38644. /* [enum_target enum res] */
  38645. duk_remove(ctx, -2);
  38646. /* [enum_target res] */
  38647. return 1; /* return 1 to allow callers to tail call */
  38648. }
  38649. #line 1 "duk_hobject_finalizer.c"
  38650. /*
  38651. * Run an duk_hobject finalizer. Used for both reference counting
  38652. * and mark-and-sweep algorithms. Must never throw an error.
  38653. *
  38654. * There is no return value. Any return value or error thrown by
  38655. * the finalizer is ignored (although errors are debug logged).
  38656. *
  38657. * Notes:
  38658. *
  38659. * - The thread used for calling the finalizer is the same as the
  38660. * 'thr' argument. This may need to change later.
  38661. *
  38662. * - The finalizer thread 'top' assertions are there because it is
  38663. * critical that strict stack policy is observed (i.e. no cruft
  38664. * left on the finalizer stack).
  38665. */
  38666. /* include removed: duk_internal.h */
  38667. DUK_LOCAL duk_ret_t duk__finalize_helper(duk_context *ctx) {
  38668. DUK_ASSERT(ctx != NULL);
  38669. DUK_DDD(DUK_DDDPRINT("protected finalization helper running"));
  38670. /* [... obj] */
  38671. /* XXX: Finalizer lookup should traverse the prototype chain (to allow
  38672. * inherited finalizers) but should not invoke accessors or proxy object
  38673. * behavior. At the moment this lookup will invoke proxy behavior, so
  38674. * caller must ensure that this function is not called if the target is
  38675. * a Proxy.
  38676. */
  38677. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_FINALIZER); /* -> [... obj finalizer] */
  38678. if (!duk_is_callable(ctx, -1)) {
  38679. DUK_DDD(DUK_DDDPRINT("-> no finalizer or finalizer not callable"));
  38680. return 0;
  38681. }
  38682. duk_dup(ctx, -2); /* -> [... obj finalizer obj] */
  38683. DUK_DDD(DUK_DDDPRINT("-> finalizer found, calling finalizer"));
  38684. duk_call(ctx, 1); /* -> [... obj retval] */
  38685. DUK_DDD(DUK_DDDPRINT("finalizer finished successfully"));
  38686. return 0;
  38687. /* Note: we rely on duk_safe_call() to fix up the stack for the caller,
  38688. * so we don't need to pop stuff here. There is no return value;
  38689. * caller determines rescued status based on object refcount.
  38690. */
  38691. }
  38692. DUK_INTERNAL void duk_hobject_run_finalizer(duk_hthread *thr, duk_hobject *obj) {
  38693. duk_context *ctx = (duk_context *) thr;
  38694. duk_ret_t rc;
  38695. #ifdef DUK_USE_ASSERTIONS
  38696. duk_idx_t entry_top;
  38697. #endif
  38698. DUK_DDD(DUK_DDDPRINT("running object finalizer for object: %p", (void *) obj));
  38699. DUK_ASSERT(thr != NULL);
  38700. DUK_ASSERT(ctx != NULL);
  38701. DUK_ASSERT(obj != NULL);
  38702. DUK_ASSERT_VALSTACK_SPACE(thr, 1);
  38703. #ifdef DUK_USE_ASSERTIONS
  38704. entry_top = duk_get_top(ctx);
  38705. #endif
  38706. /*
  38707. * Get and call the finalizer. All of this must be wrapped
  38708. * in a protected call, because even getting the finalizer
  38709. * may trigger an error (getter may throw one, for instance).
  38710. */
  38711. /* XXX: use a NULL error handler for the finalizer call? */
  38712. DUK_DDD(DUK_DDDPRINT("-> finalizer found, calling wrapped finalize helper"));
  38713. duk_push_hobject(ctx, obj); /* this also increases refcount by one */
  38714. rc = duk_safe_call(ctx, duk__finalize_helper, 0 /*nargs*/, 1 /*nrets*/); /* -> [... obj retval/error] */
  38715. DUK_ASSERT_TOP(ctx, entry_top + 2); /* duk_safe_call discipline */
  38716. if (rc != DUK_EXEC_SUCCESS) {
  38717. /* Note: we ask for one return value from duk_safe_call to get this
  38718. * error debugging here.
  38719. */
  38720. DUK_D(DUK_DPRINT("wrapped finalizer call failed for object %p (ignored); error: %!T",
  38721. (void *) obj, (duk_tval *) duk_get_tval(ctx, -1)));
  38722. }
  38723. duk_pop_2(ctx); /* -> [...] */
  38724. DUK_ASSERT_TOP(ctx, entry_top);
  38725. }
  38726. #line 1 "duk_hobject_misc.c"
  38727. /*
  38728. * Misc support functions
  38729. */
  38730. /* include removed: duk_internal.h */
  38731. DUK_INTERNAL duk_bool_t duk_hobject_prototype_chain_contains(duk_hthread *thr, duk_hobject *h, duk_hobject *p, duk_bool_t ignore_loop) {
  38732. duk_uint_t sanity;
  38733. DUK_ASSERT(thr != NULL);
  38734. DUK_ASSERT(h != NULL);
  38735. /* allow 'p' to be NULL; then the result is always false */
  38736. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  38737. do {
  38738. if (h == p) {
  38739. return 1;
  38740. }
  38741. if (sanity-- == 0) {
  38742. if (ignore_loop) {
  38743. break;
  38744. } else {
  38745. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  38746. }
  38747. }
  38748. h = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
  38749. } while (h);
  38750. return 0;
  38751. }
  38752. DUK_INTERNAL void duk_hobject_set_prototype(duk_hthread *thr, duk_hobject *h, duk_hobject *p) {
  38753. #ifdef DUK_USE_REFERENCE_COUNTING
  38754. duk_hobject *tmp;
  38755. DUK_ASSERT(h);
  38756. tmp = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
  38757. DUK_HOBJECT_SET_PROTOTYPE(thr->heap, h, p);
  38758. DUK_HOBJECT_INCREF_ALLOWNULL(thr, p); /* avoid problems if p == h->prototype */
  38759. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  38760. #else
  38761. DUK_ASSERT(h);
  38762. DUK_UNREF(thr);
  38763. DUK_HOBJECT_SET_PROTOTYPE(thr->heap, h, p);
  38764. #endif
  38765. }
  38766. #line 1 "duk_hobject_pc2line.c"
  38767. /*
  38768. * Helpers for creating and querying pc2line debug data, which
  38769. * converts a bytecode program counter to a source line number.
  38770. *
  38771. * The run-time pc2line data is bit-packed, and documented in:
  38772. *
  38773. * doc/function-objects.txt
  38774. */
  38775. /* include removed: duk_internal.h */
  38776. #if defined(DUK_USE_PC2LINE)
  38777. /* Generate pc2line data for an instruction sequence, leaving a buffer on stack top. */
  38778. DUK_INTERNAL void duk_hobject_pc2line_pack(duk_hthread *thr, duk_compiler_instr *instrs, duk_uint_fast32_t length) {
  38779. duk_context *ctx = (duk_context *) thr;
  38780. duk_hbuffer_dynamic *h_buf;
  38781. duk_bitencoder_ctx be_ctx_alloc;
  38782. duk_bitencoder_ctx *be_ctx = &be_ctx_alloc;
  38783. duk_uint32_t *hdr;
  38784. duk_size_t new_size;
  38785. duk_uint_fast32_t num_header_entries;
  38786. duk_uint_fast32_t curr_offset;
  38787. duk_int_fast32_t curr_line, next_line, diff_line;
  38788. duk_uint_fast32_t curr_pc;
  38789. duk_uint_fast32_t hdr_index;
  38790. DUK_ASSERT(length <= DUK_COMPILER_MAX_BYTECODE_LENGTH);
  38791. /* XXX: add proper spare handling to dynamic buffer, to minimize
  38792. * reallocs; currently there is no spare at all.
  38793. */
  38794. num_header_entries = (length + DUK_PC2LINE_SKIP - 1) / DUK_PC2LINE_SKIP;
  38795. curr_offset = (duk_uint_fast32_t) (sizeof(duk_uint32_t) + num_header_entries * sizeof(duk_uint32_t) * 2);
  38796. duk_push_dynamic_buffer(ctx, (duk_size_t) curr_offset);
  38797. h_buf = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, -1);
  38798. DUK_ASSERT(h_buf != NULL);
  38799. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(h_buf));
  38800. hdr = (duk_uint32_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h_buf);
  38801. DUK_ASSERT(hdr != NULL);
  38802. hdr[0] = (duk_uint32_t) length; /* valid pc range is [0, length[ */
  38803. curr_pc = 0U;
  38804. while (curr_pc < length) {
  38805. new_size = (duk_size_t) (curr_offset + DUK_PC2LINE_MAX_DIFF_LENGTH);
  38806. duk_hbuffer_resize(thr, h_buf, new_size, new_size);
  38807. hdr = (duk_uint32_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h_buf);
  38808. DUK_ASSERT(hdr != NULL);
  38809. DUK_ASSERT(curr_pc < length);
  38810. hdr_index = 1 + (curr_pc / DUK_PC2LINE_SKIP) * 2;
  38811. curr_line = (duk_int_fast32_t) instrs[curr_pc].line;
  38812. hdr[hdr_index + 0] = (duk_uint32_t) curr_line;
  38813. hdr[hdr_index + 1] = (duk_uint32_t) curr_offset;
  38814. #if 0
  38815. DUK_DDD(DUK_DDDPRINT("hdr[%ld]: pc=%ld line=%ld offset=%ld",
  38816. (long) (curr_pc / DUK_PC2LINE_SKIP),
  38817. (long) curr_pc,
  38818. (long) hdr[hdr_index + 0],
  38819. (long) hdr[hdr_index + 1]));
  38820. #endif
  38821. DUK_MEMZERO(be_ctx, sizeof(*be_ctx));
  38822. be_ctx->data = ((duk_uint8_t *) hdr) + curr_offset;
  38823. be_ctx->length = (duk_size_t) DUK_PC2LINE_MAX_DIFF_LENGTH;
  38824. for (;;) {
  38825. curr_pc++;
  38826. if ( ((curr_pc % DUK_PC2LINE_SKIP) == 0) || /* end of diff run */
  38827. (curr_pc >= length) ) { /* end of bytecode */
  38828. break;
  38829. }
  38830. DUK_ASSERT(curr_pc < length);
  38831. next_line = (duk_int32_t) instrs[curr_pc].line;
  38832. diff_line = next_line - curr_line;
  38833. #if 0
  38834. DUK_DDD(DUK_DDDPRINT("curr_line=%ld, next_line=%ld -> diff_line=%ld",
  38835. (long) curr_line, (long) next_line, (long) diff_line));
  38836. #endif
  38837. if (diff_line == 0) {
  38838. /* 0 */
  38839. duk_be_encode(be_ctx, 0, 1);
  38840. } else if (diff_line >= 1 && diff_line <= 4) {
  38841. /* 1 0 <2 bits> */
  38842. duk_be_encode(be_ctx, (0x02 << 2) + (diff_line - 1), 4);
  38843. } else if (diff_line >= -0x80 && diff_line <= 0x7f) {
  38844. /* 1 1 0 <8 bits> */
  38845. DUK_ASSERT(diff_line + 0x80 >= 0 && diff_line + 0x80 <= 0xff);
  38846. duk_be_encode(be_ctx, (0x06 << 8) + (diff_line + 0x80), 11);
  38847. } else {
  38848. /* 1 1 1 <32 bits>
  38849. * Encode in two parts to avoid bitencode 24-bit limitation
  38850. */
  38851. duk_be_encode(be_ctx, (0x07 << 16) + ((next_line >> 16) & 0xffffU), 19);
  38852. duk_be_encode(be_ctx, next_line & 0xffffU, 16);
  38853. }
  38854. curr_line = next_line;
  38855. }
  38856. duk_be_finish(be_ctx);
  38857. DUK_ASSERT(!be_ctx->truncated);
  38858. /* be_ctx->offset == length of encoded bitstream */
  38859. curr_offset += (duk_uint_fast32_t) be_ctx->offset;
  38860. }
  38861. /* compact */
  38862. new_size = (duk_size_t) curr_offset;
  38863. duk_hbuffer_resize(thr, h_buf, new_size, new_size);
  38864. (void) duk_to_fixed_buffer(ctx, -1, NULL);
  38865. DUK_DDD(DUK_DDDPRINT("final pc2line data: pc_limit=%ld, length=%ld, %lf bits/opcode --> %!ixT",
  38866. (long) length, (long) new_size, (double) new_size * 8.0 / (double) length,
  38867. (duk_tval *) duk_get_tval(ctx, -1)));
  38868. }
  38869. /* PC is unsigned. If caller does PC arithmetic and gets a negative result,
  38870. * it will map to a large PC which is out of bounds and causes a zero to be
  38871. * returned.
  38872. */
  38873. DUK_LOCAL duk_uint_fast32_t duk__hobject_pc2line_query_raw(duk_hthread *thr, duk_hbuffer_fixed *buf, duk_uint_fast32_t pc) {
  38874. duk_bitdecoder_ctx bd_ctx_alloc;
  38875. duk_bitdecoder_ctx *bd_ctx = &bd_ctx_alloc;
  38876. duk_uint32_t *hdr;
  38877. duk_uint_fast32_t start_offset;
  38878. duk_uint_fast32_t pc_limit;
  38879. duk_uint_fast32_t hdr_index;
  38880. duk_uint_fast32_t pc_base;
  38881. duk_uint_fast32_t n;
  38882. duk_uint_fast32_t curr_line;
  38883. DUK_ASSERT(buf != NULL);
  38884. DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) buf));
  38885. DUK_UNREF(thr);
  38886. /*
  38887. * Use the index in the header to find the right starting point
  38888. */
  38889. hdr_index = pc / DUK_PC2LINE_SKIP;
  38890. pc_base = hdr_index * DUK_PC2LINE_SKIP;
  38891. n = pc - pc_base;
  38892. if (DUK_HBUFFER_FIXED_GET_SIZE(buf) <= sizeof(duk_uint32_t)) {
  38893. DUK_DD(DUK_DDPRINT("pc2line lookup failed: buffer is smaller than minimal header"));
  38894. goto error;
  38895. }
  38896. hdr = (duk_uint32_t *) DUK_HBUFFER_FIXED_GET_DATA_PTR(thr->heap, buf);
  38897. pc_limit = hdr[0];
  38898. if (pc >= pc_limit) {
  38899. /* Note: pc is unsigned and cannot be negative */
  38900. DUK_DD(DUK_DDPRINT("pc2line lookup failed: pc out of bounds (pc=%ld, limit=%ld)",
  38901. (long) pc, (long) pc_limit));
  38902. goto error;
  38903. }
  38904. curr_line = hdr[1 + hdr_index * 2];
  38905. start_offset = hdr[1 + hdr_index * 2 + 1];
  38906. if ((duk_size_t) start_offset > DUK_HBUFFER_FIXED_GET_SIZE(buf)) {
  38907. DUK_DD(DUK_DDPRINT("pc2line lookup failed: start_offset out of bounds (start_offset=%ld, buffer_size=%ld)",
  38908. (long) start_offset, (long) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) buf)));
  38909. goto error;
  38910. }
  38911. /*
  38912. * Iterate the bitstream (line diffs) until PC is reached
  38913. */
  38914. DUK_MEMZERO(bd_ctx, sizeof(*bd_ctx));
  38915. bd_ctx->data = ((duk_uint8_t *) hdr) + start_offset;
  38916. bd_ctx->length = (duk_size_t) (DUK_HBUFFER_FIXED_GET_SIZE(buf) - start_offset);
  38917. #if 0
  38918. DUK_DDD(DUK_DDDPRINT("pc2line lookup: pc=%ld -> hdr_index=%ld, pc_base=%ld, n=%ld, start_offset=%ld",
  38919. (long) pc, (long) hdr_index, (long) pc_base, (long) n, (long) start_offset));
  38920. #endif
  38921. while (n > 0) {
  38922. #if 0
  38923. DUK_DDD(DUK_DDDPRINT("lookup: n=%ld, curr_line=%ld", (long) n, (long) curr_line));
  38924. #endif
  38925. if (duk_bd_decode_flag(bd_ctx)) {
  38926. if (duk_bd_decode_flag(bd_ctx)) {
  38927. if (duk_bd_decode_flag(bd_ctx)) {
  38928. /* 1 1 1 <32 bits> */
  38929. duk_uint_fast32_t t;
  38930. t = duk_bd_decode(bd_ctx, 16); /* workaround: max nbits = 24 now */
  38931. t = (t << 16) + duk_bd_decode(bd_ctx, 16);
  38932. curr_line = t;
  38933. } else {
  38934. /* 1 1 0 <8 bits> */
  38935. duk_uint_fast32_t t;
  38936. t = duk_bd_decode(bd_ctx, 8);
  38937. curr_line = curr_line + t - 0x80;
  38938. }
  38939. } else {
  38940. /* 1 0 <2 bits> */
  38941. duk_uint_fast32_t t;
  38942. t = duk_bd_decode(bd_ctx, 2);
  38943. curr_line = curr_line + t + 1;
  38944. }
  38945. } else {
  38946. /* 0: no change */
  38947. }
  38948. n--;
  38949. }
  38950. DUK_DDD(DUK_DDDPRINT("pc2line lookup result: pc %ld -> line %ld", (long) pc, (long) curr_line));
  38951. return curr_line;
  38952. error:
  38953. DUK_D(DUK_DPRINT("pc2line conversion failed for pc=%ld", (long) pc));
  38954. return 0;
  38955. }
  38956. DUK_INTERNAL duk_uint_fast32_t duk_hobject_pc2line_query(duk_context *ctx, duk_idx_t idx_func, duk_uint_fast32_t pc) {
  38957. duk_hbuffer_fixed *pc2line;
  38958. duk_uint_fast32_t line;
  38959. /* XXX: now that pc2line is used by the debugger quite heavily in
  38960. * checked execution, this should be optimized to avoid value stack
  38961. * and perhaps also implement some form of pc2line caching (see
  38962. * future work in debugger.rst).
  38963. */
  38964. duk_get_prop_stridx(ctx, idx_func, DUK_STRIDX_INT_PC2LINE);
  38965. pc2line = (duk_hbuffer_fixed *) duk_get_hbuffer(ctx, -1);
  38966. if (pc2line != NULL) {
  38967. DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) pc2line));
  38968. line = duk__hobject_pc2line_query_raw((duk_hthread *) ctx, pc2line, (duk_uint_fast32_t) pc);
  38969. } else {
  38970. line = 0;
  38971. }
  38972. duk_pop(ctx);
  38973. return line;
  38974. }
  38975. #endif /* DUK_USE_PC2LINE */
  38976. #line 1 "duk_hobject_props.c"
  38977. /*
  38978. * Hobject property set/get functionality.
  38979. *
  38980. * This is very central functionality for size, performance, and compliance.
  38981. * It is also rather intricate; see hobject-algorithms.txt for discussion on
  38982. * the algorithms and memory-management.txt for discussion on refcounts and
  38983. * side effect issues.
  38984. *
  38985. * Notes:
  38986. *
  38987. * - It might be tempting to assert "refcount nonzero" for objects
  38988. * being operated on, but that's not always correct: objects with
  38989. * a zero refcount may be operated on by the refcount implementation
  38990. * (finalization) for instance. Hence, no refcount assertions are made.
  38991. *
  38992. * - Many operations (memory allocation, identifier operations, etc)
  38993. * may cause arbitrary side effects (e.g. through GC and finalization).
  38994. * These side effects may invalidate duk_tval pointers which point to
  38995. * areas subject to reallocation (like value stack). Heap objects
  38996. * themselves have stable pointers. Holding heap object pointers or
  38997. * duk_tval copies is not problematic with respect to side effects;
  38998. * care must be taken when holding and using argument duk_tval pointers.
  38999. *
  39000. * - If a finalizer is executed, it may operate on the the same object
  39001. * we're currently dealing with. For instance, the finalizer might
  39002. * delete a certain property which has already been looked up and
  39003. * confirmed to exist. Ideally finalizers would be disabled if GC
  39004. * happens during property access. At the moment property table realloc
  39005. * disables finalizers, and all DECREFs may cause arbitrary changes so
  39006. * handle DECREF carefully.
  39007. *
  39008. * - The order of operations for a DECREF matters. When DECREF is executed,
  39009. * the entire object graph must be consistent; note that a refzero may
  39010. * lead to a mark-and-sweep through a refcount finalizer.
  39011. */
  39012. /*
  39013. * XXX: array indices are mostly typed as duk_uint32_t here; duk_uarridx_t
  39014. * might be more appropriate.
  39015. */
  39016. /*
  39017. * XXX: duk_uint_fast32_t should probably be used in many places here.
  39018. */
  39019. /* include removed: duk_internal.h */
  39020. /*
  39021. * Local defines
  39022. */
  39023. #define DUK__NO_ARRAY_INDEX DUK_HSTRING_NO_ARRAY_INDEX
  39024. /* hash probe sequence */
  39025. #define DUK__HASH_INITIAL(hash,h_size) DUK_HOBJECT_HASH_INITIAL((hash),(h_size))
  39026. #define DUK__HASH_PROBE_STEP(hash) DUK_HOBJECT_HASH_PROBE_STEP((hash))
  39027. /* marker values for hash part */
  39028. #define DUK__HASH_UNUSED DUK_HOBJECT_HASHIDX_UNUSED
  39029. #define DUK__HASH_DELETED DUK_HOBJECT_HASHIDX_DELETED
  39030. /* valstack space that suffices for all local calls, including recursion
  39031. * of other than Duktape calls (getters etc)
  39032. */
  39033. #define DUK__VALSTACK_SPACE 10
  39034. /* valstack space allocated especially for proxy lookup which does a
  39035. * recursive property lookup
  39036. */
  39037. #define DUK__VALSTACK_PROXY_LOOKUP 20
  39038. /*
  39039. * Local prototypes
  39040. */
  39041. #define DUK__DESC_FLAG_PUSH_VALUE (1 << 0) /* push value to stack */
  39042. #define DUK__DESC_FLAG_IGNORE_PROTOLOOP (1 << 1) /* don't throw for prototype loop */
  39043. 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);
  39044. 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);
  39045. DUK_LOCAL_DECL void duk__check_arguments_map_for_delete(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *temp_desc);
  39046. 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);
  39047. DUK_LOCAL_DECL duk_bool_t duk__handle_put_array_length(duk_hthread *thr, duk_hobject *obj);
  39048. 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);
  39049. 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);
  39050. 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);
  39051. DUK_LOCAL duk_uint32_t duk__get_old_array_length(duk_hthread *thr, duk_hobject *obj, duk_propdesc *temp_desc);
  39052. /*
  39053. * Misc helpers
  39054. */
  39055. /* Convert a duk_tval number (caller checks) to a 32-bit index. Returns
  39056. * DUK__NO_ARRAY_INDEX if the number is not whole or not a valid array
  39057. * index.
  39058. */
  39059. /* XXX: for fastints, could use a variant which assumes a double duk_tval
  39060. * (and doesn't need to check for fastint again).
  39061. */
  39062. DUK_LOCAL duk_uint32_t duk__tval_number_to_arr_idx(duk_tval *tv) {
  39063. duk_double_t dbl;
  39064. duk_uint32_t idx;
  39065. DUK_ASSERT(tv != NULL);
  39066. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  39067. dbl = DUK_TVAL_GET_NUMBER(tv);
  39068. idx = (duk_uint32_t) dbl;
  39069. if ((duk_double_t) idx == dbl) {
  39070. /* Is whole and within 32 bit range. If the value happens to be 0xFFFFFFFF,
  39071. * it's not a valid array index but will then match DUK__NO_ARRAY_INDEX.
  39072. */
  39073. return idx;
  39074. }
  39075. return DUK__NO_ARRAY_INDEX;
  39076. }
  39077. #if defined(DUK_USE_FASTINT)
  39078. /* Convert a duk_tval fastint (caller checks) to a 32-bit index. */
  39079. DUK_LOCAL duk_uint32_t duk__tval_fastint_to_arr_idx(duk_tval *tv) {
  39080. duk_int64_t t;
  39081. DUK_ASSERT(tv != NULL);
  39082. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv));
  39083. t = DUK_TVAL_GET_FASTINT(tv);
  39084. if ((t & ~0xffffffffULL) != 0) {
  39085. /* Catches >0x100000000 and negative values. */
  39086. return DUK__NO_ARRAY_INDEX;
  39087. }
  39088. /* If the value happens to be 0xFFFFFFFF, it's not a valid array index
  39089. * but will then match DUK__NO_ARRAY_INDEX.
  39090. */
  39091. return (duk_uint32_t) t;
  39092. }
  39093. #endif /* DUK_USE_FASTINT */
  39094. /* Push an arbitrary duk_tval to the stack, coerce it to string, and return
  39095. * both a duk_hstring pointer and an array index (or DUK__NO_ARRAY_INDEX).
  39096. */
  39097. DUK_LOCAL duk_uint32_t duk__push_tval_to_hstring_arr_idx(duk_context *ctx, duk_tval *tv, duk_hstring **out_h) {
  39098. duk_uint32_t arr_idx;
  39099. duk_hstring *h;
  39100. DUK_ASSERT(ctx != NULL);
  39101. DUK_ASSERT(tv != NULL);
  39102. DUK_ASSERT(out_h != NULL);
  39103. duk_push_tval(ctx, tv);
  39104. duk_to_string(ctx, -1);
  39105. h = duk_get_hstring(ctx, -1);
  39106. DUK_ASSERT(h != NULL);
  39107. *out_h = h;
  39108. arr_idx = DUK_HSTRING_GET_ARRIDX_FAST(h);
  39109. return arr_idx;
  39110. }
  39111. /* String is an own (virtual) property of a lightfunc. */
  39112. DUK_LOCAL duk_bool_t duk__key_is_lightfunc_ownprop(duk_hthread *thr, duk_hstring *key) {
  39113. return (key == DUK_HTHREAD_STRING_LENGTH(thr) ||
  39114. key == DUK_HTHREAD_STRING_NAME(thr));
  39115. }
  39116. /*
  39117. * Helpers for managing property storage size
  39118. */
  39119. /* Get default hash part size for a certain entry part size. */
  39120. #if defined(DUK_USE_HOBJECT_HASH_PART)
  39121. DUK_LOCAL duk_uint32_t duk__get_default_h_size(duk_uint32_t e_size) {
  39122. DUK_ASSERT(e_size <= DUK_HOBJECT_MAX_PROPERTIES);
  39123. if (e_size >= DUK_HOBJECT_E_USE_HASH_LIMIT) {
  39124. duk_uint32_t res;
  39125. /* result: hash_prime(floor(1.2 * e_size)) */
  39126. res = duk_util_get_hash_prime(e_size + e_size / DUK_HOBJECT_H_SIZE_DIVISOR);
  39127. /* if fails, e_size will be zero = not an issue, except performance-wise */
  39128. DUK_ASSERT(res == 0 || res > e_size);
  39129. return res;
  39130. } else {
  39131. return 0;
  39132. }
  39133. }
  39134. #endif /* USE_PROP_HASH_PART */
  39135. /* Get minimum entry part growth for a certain size. */
  39136. DUK_LOCAL duk_uint32_t duk__get_min_grow_e(duk_uint32_t e_size) {
  39137. duk_uint32_t res;
  39138. DUK_ASSERT(e_size <= DUK_HOBJECT_MAX_PROPERTIES);
  39139. res = (e_size + DUK_HOBJECT_E_MIN_GROW_ADD) / DUK_HOBJECT_E_MIN_GROW_DIVISOR;
  39140. DUK_ASSERT(res >= 1); /* important for callers */
  39141. return res;
  39142. }
  39143. /* Get minimum array part growth for a certain size. */
  39144. DUK_LOCAL duk_uint32_t duk__get_min_grow_a(duk_uint32_t a_size) {
  39145. duk_uint32_t res;
  39146. DUK_ASSERT((duk_size_t) a_size <= DUK_HOBJECT_MAX_PROPERTIES);
  39147. res = (a_size + DUK_HOBJECT_A_MIN_GROW_ADD) / DUK_HOBJECT_A_MIN_GROW_DIVISOR;
  39148. DUK_ASSERT(res >= 1); /* important for callers */
  39149. return res;
  39150. }
  39151. /* Count actually used entry part entries (non-NULL keys). */
  39152. DUK_LOCAL duk_uint32_t duk__count_used_e_keys(duk_hthread *thr, duk_hobject *obj) {
  39153. duk_uint_fast32_t i;
  39154. duk_uint_fast32_t n = 0;
  39155. duk_hstring **e;
  39156. DUK_ASSERT(obj != NULL);
  39157. DUK_UNREF(thr);
  39158. e = DUK_HOBJECT_E_GET_KEY_BASE(thr->heap, obj);
  39159. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  39160. if (*e++) {
  39161. n++;
  39162. }
  39163. }
  39164. return (duk_uint32_t) n;
  39165. }
  39166. /* Count actually used array part entries and array minimum size.
  39167. * NOTE: 'out_min_size' can be computed much faster by starting from the
  39168. * end and breaking out early when finding first used entry, but this is
  39169. * not needed now.
  39170. */
  39171. DUK_LOCAL void duk__compute_a_stats(duk_hthread *thr, duk_hobject *obj, duk_uint32_t *out_used, duk_uint32_t *out_min_size) {
  39172. duk_uint_fast32_t i;
  39173. duk_uint_fast32_t used = 0;
  39174. duk_uint_fast32_t highest_idx = (duk_uint_fast32_t) -1; /* see below */
  39175. duk_tval *a;
  39176. DUK_ASSERT(obj != NULL);
  39177. DUK_ASSERT(out_used != NULL);
  39178. DUK_ASSERT(out_min_size != NULL);
  39179. DUK_UNREF(thr);
  39180. a = DUK_HOBJECT_A_GET_BASE(thr->heap, obj);
  39181. for (i = 0; i < DUK_HOBJECT_GET_ASIZE(obj); i++) {
  39182. duk_tval *tv = a++;
  39183. if (!DUK_TVAL_IS_UNDEFINED_UNUSED(tv)) {
  39184. used++;
  39185. highest_idx = i;
  39186. }
  39187. }
  39188. /* Initial value for highest_idx is -1 coerced to unsigned. This
  39189. * is a bit odd, but (highest_idx + 1) will then wrap to 0 below
  39190. * for out_min_size as intended.
  39191. */
  39192. *out_used = used;
  39193. *out_min_size = highest_idx + 1; /* 0 if no used entries */
  39194. }
  39195. /* Check array density and indicate whether or not the array part should be abandoned. */
  39196. DUK_LOCAL duk_bool_t duk__abandon_array_density_check(duk_uint32_t a_used, duk_uint32_t a_size) {
  39197. /*
  39198. * Array abandon check; abandon if:
  39199. *
  39200. * new_used / new_size < limit
  39201. * new_used < limit * new_size || limit is 3 bits fixed point
  39202. * new_used < limit' / 8 * new_size || *8
  39203. * 8*new_used < limit' * new_size || :8
  39204. * new_used < limit' * (new_size / 8)
  39205. *
  39206. * Here, new_used = a_used, new_size = a_size.
  39207. *
  39208. * Note: some callers use approximate values for a_used and/or a_size
  39209. * (e.g. dropping a '+1' term). This doesn't affect the usefulness
  39210. * of the check, but may confuse debugging.
  39211. */
  39212. return (a_used < DUK_HOBJECT_A_ABANDON_LIMIT * (a_size >> 3));
  39213. }
  39214. /* Fast check for extending array: check whether or not a slow density check is required. */
  39215. DUK_LOCAL duk_bool_t duk__abandon_array_slow_check_required(duk_uint32_t arr_idx, duk_uint32_t old_size) {
  39216. /*
  39217. * In a fast check we assume old_size equals old_used (i.e., existing
  39218. * array is fully dense).
  39219. *
  39220. * Slow check if:
  39221. *
  39222. * (new_size - old_size) / old_size > limit
  39223. * new_size - old_size > limit * old_size
  39224. * new_size > (1 + limit) * old_size || limit' is 3 bits fixed point
  39225. * new_size > (1 + (limit' / 8)) * old_size || * 8
  39226. * 8 * new_size > (8 + limit') * old_size || : 8
  39227. * new_size > (8 + limit') * (old_size / 8)
  39228. * new_size > limit'' * (old_size / 8) || limit'' = 9 -> max 25% increase
  39229. * arr_idx + 1 > limit'' * (old_size / 8)
  39230. *
  39231. * This check doesn't work well for small values, so old_size is rounded
  39232. * up for the check (and the '+ 1' of arr_idx can be ignored in practice):
  39233. *
  39234. * arr_idx > limit'' * ((old_size + 7) / 8)
  39235. */
  39236. return (arr_idx > DUK_HOBJECT_A_FAST_RESIZE_LIMIT * ((old_size + 7) >> 3));
  39237. }
  39238. /*
  39239. * Proxy helpers
  39240. */
  39241. #if defined(DUK_USE_ES6_PROXY)
  39242. DUK_INTERNAL duk_bool_t duk_hobject_proxy_check(duk_hthread *thr, duk_hobject *obj, duk_hobject **out_target, duk_hobject **out_handler) {
  39243. duk_tval *tv_target;
  39244. duk_tval *tv_handler;
  39245. duk_hobject *h_target;
  39246. duk_hobject *h_handler;
  39247. DUK_ASSERT(thr != NULL);
  39248. DUK_ASSERT(obj != NULL);
  39249. DUK_ASSERT(out_target != NULL);
  39250. DUK_ASSERT(out_handler != NULL);
  39251. /* Caller doesn't need to check exotic proxy behavior (but does so for
  39252. * some fast paths).
  39253. */
  39254. if (DUK_LIKELY(!DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(obj))) {
  39255. return 0;
  39256. }
  39257. tv_handler = duk_hobject_find_existing_entry_tval_ptr(thr->heap, obj, DUK_HTHREAD_STRING_INT_HANDLER(thr));
  39258. if (!tv_handler) {
  39259. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROXY_REVOKED);
  39260. return 0;
  39261. }
  39262. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv_handler));
  39263. h_handler = DUK_TVAL_GET_OBJECT(tv_handler);
  39264. DUK_ASSERT(h_handler != NULL);
  39265. *out_handler = h_handler;
  39266. tv_handler = NULL; /* avoid issues with relocation */
  39267. tv_target = duk_hobject_find_existing_entry_tval_ptr(thr->heap, obj, DUK_HTHREAD_STRING_INT_TARGET(thr));
  39268. if (!tv_target) {
  39269. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROXY_REVOKED);
  39270. return 0;
  39271. }
  39272. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv_target));
  39273. h_target = DUK_TVAL_GET_OBJECT(tv_target);
  39274. DUK_ASSERT(h_target != NULL);
  39275. *out_target = h_target;
  39276. tv_target = NULL; /* avoid issues with relocation */
  39277. return 1;
  39278. }
  39279. #endif
  39280. #if defined(DUK_USE_ES6_PROXY)
  39281. 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) {
  39282. duk_context *ctx = (duk_context *) thr;
  39283. duk_hobject *h_handler;
  39284. DUK_ASSERT(thr != NULL);
  39285. DUK_ASSERT(obj != NULL);
  39286. DUK_ASSERT(tv_key != NULL);
  39287. DUK_ASSERT(out_target != NULL);
  39288. if (!duk_hobject_proxy_check(thr, obj, out_target, &h_handler)) {
  39289. return 0;
  39290. }
  39291. DUK_ASSERT(*out_target != NULL);
  39292. DUK_ASSERT(h_handler != NULL);
  39293. /* XXX: At the moment Duktape accesses internal keys like _Finalizer using a
  39294. * normal property set/get which would allow a proxy handler to interfere with
  39295. * such behavior and to get access to internal key strings. This is not a problem
  39296. * as such because internal key strings can be created in other ways too (e.g.
  39297. * through buffers). The best fix is to change Duktape internal lookups to
  39298. * skip proxy behavior. Until that, internal property accesses bypass the
  39299. * proxy and are applied to the target (as if the handler did not exist).
  39300. * This has some side effects, see test-bi-proxy-internal-keys.js.
  39301. */
  39302. if (DUK_TVAL_IS_STRING(tv_key)) {
  39303. duk_hstring *h_key = (duk_hstring *) DUK_TVAL_GET_STRING(tv_key);
  39304. DUK_ASSERT(h_key != NULL);
  39305. if (DUK_HSTRING_HAS_INTERNAL(h_key)) {
  39306. DUK_DDD(DUK_DDDPRINT("internal key, skip proxy handler and apply to target"));
  39307. return 0;
  39308. }
  39309. }
  39310. /* The handler is looked up with a normal property lookup; it may be an
  39311. * accessor or the handler object itself may be a proxy object. If the
  39312. * handler is a proxy, we need to extend the valstack as we make a
  39313. * recursive proxy check without a function call in between (in fact
  39314. * there is no limit to the potential recursion here).
  39315. *
  39316. * (For sanity, proxy creation rejects another proxy object as either
  39317. * the handler or the target at the moment so recursive proxy cases
  39318. * are not realized now.)
  39319. */
  39320. /* XXX: C recursion limit if proxies are allowed as handler/target values */
  39321. duk_require_stack(ctx, DUK__VALSTACK_PROXY_LOOKUP);
  39322. duk_push_hobject(ctx, h_handler);
  39323. if (duk_get_prop_stridx(ctx, -1, stridx_trap)) {
  39324. /* -> [ ... handler trap ] */
  39325. duk_insert(ctx, -2); /* -> [ ... trap handler ] */
  39326. /* stack prepped for func call: [ ... trap handler ] */
  39327. return 1;
  39328. } else {
  39329. duk_pop_2(ctx);
  39330. return 0;
  39331. }
  39332. }
  39333. #endif /* DUK_USE_ES6_PROXY */
  39334. /*
  39335. * Reallocate property allocation, moving properties to the new allocation.
  39336. *
  39337. * Includes key compaction, rehashing, and can also optionally abandoning
  39338. * the array part, 'migrating' array entries into the beginning of the
  39339. * new entry part. Arguments are not validated here, so e.g. new_h_size
  39340. * MUST be a valid prime.
  39341. *
  39342. * There is no support for in-place reallocation or just compacting keys
  39343. * without resizing the property allocation. This is intentional to keep
  39344. * code size minimal.
  39345. *
  39346. * The implementation is relatively straightforward, except for the array
  39347. * abandonment process. Array abandonment requires that new string keys
  39348. * are interned, which may trigger GC. All keys interned so far must be
  39349. * reachable for GC at all times; valstack is used for that now.
  39350. *
  39351. * Also, a GC triggered during this reallocation process must not interfere
  39352. * with the object being resized. This is currently controlled by using
  39353. * heap->mark_and_sweep_base_flags to indicate that no finalizers will be
  39354. * executed (as they can affect ANY object) and no objects are compacted
  39355. * (it would suffice to protect this particular object only, though).
  39356. *
  39357. * Note: a non-checked variant would be nice but is a bit tricky to
  39358. * implement for the array abandonment process. It's easy for
  39359. * everything else.
  39360. *
  39361. * Note: because we need to potentially resize the valstack (as part
  39362. * of abandoning the array part), any tval pointers to the valstack
  39363. * will become invalid after this call.
  39364. */
  39365. DUK_LOCAL
  39366. void duk__realloc_props(duk_hthread *thr,
  39367. duk_hobject *obj,
  39368. duk_uint32_t new_e_size,
  39369. duk_uint32_t new_a_size,
  39370. duk_uint32_t new_h_size,
  39371. duk_bool_t abandon_array) {
  39372. duk_context *ctx = (duk_context *) thr;
  39373. #ifdef DUK_USE_MARK_AND_SWEEP
  39374. duk_small_uint_t prev_mark_and_sweep_base_flags;
  39375. #endif
  39376. duk_uint32_t new_alloc_size;
  39377. duk_uint32_t new_e_size_adjusted;
  39378. duk_uint8_t *new_p;
  39379. duk_hstring **new_e_k;
  39380. duk_propvalue *new_e_pv;
  39381. duk_uint8_t *new_e_f;
  39382. duk_tval *new_a;
  39383. duk_uint32_t *new_h;
  39384. duk_uint32_t new_e_next;
  39385. duk_uint_fast32_t i;
  39386. DUK_ASSERT(thr != NULL);
  39387. DUK_ASSERT(ctx != NULL);
  39388. DUK_ASSERT(obj != NULL);
  39389. DUK_ASSERT(!abandon_array || new_a_size == 0); /* if abandon_array, new_a_size must be 0 */
  39390. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, obj) != NULL || (DUK_HOBJECT_GET_ESIZE(obj) == 0 && DUK_HOBJECT_GET_ASIZE(obj) == 0));
  39391. DUK_ASSERT(new_h_size == 0 || new_h_size >= new_e_size); /* required to guarantee success of rehashing,
  39392. * intentionally use unadjusted new_e_size
  39393. */
  39394. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  39395. /*
  39396. * Pre resize assertions.
  39397. */
  39398. #ifdef DUK_USE_ASSERTIONS
  39399. /* XXX: pre-checks (such as no duplicate keys) */
  39400. #endif
  39401. /*
  39402. * For property layout 1, tweak e_size to ensure that the whole entry
  39403. * part (key + val + flags) is a suitable multiple for alignment
  39404. * (platform specific).
  39405. *
  39406. * Property layout 2 does not require this tweaking and is preferred
  39407. * on low RAM platforms requiring alignment.
  39408. */
  39409. #if defined(DUK_USE_HOBJECT_LAYOUT_2) || defined(DUK_USE_HOBJECT_LAYOUT_3)
  39410. DUK_DDD(DUK_DDDPRINT("using layout 2 or 3, no need to pad e_size: %ld", (long) new_e_size));
  39411. new_e_size_adjusted = new_e_size;
  39412. #elif defined(DUK_USE_HOBJECT_LAYOUT_1) && (DUK_HOBJECT_ALIGN_TARGET == 1)
  39413. DUK_DDD(DUK_DDDPRINT("using layout 1, but no need to pad e_size: %ld", (long) new_e_size));
  39414. new_e_size_adjusted = new_e_size;
  39415. #elif defined(DUK_USE_HOBJECT_LAYOUT_1) && ((DUK_HOBJECT_ALIGN_TARGET == 4) || (DUK_HOBJECT_ALIGN_TARGET == 8))
  39416. new_e_size_adjusted = (new_e_size + DUK_HOBJECT_ALIGN_TARGET - 1) & (~(DUK_HOBJECT_ALIGN_TARGET - 1));
  39417. DUK_DDD(DUK_DDDPRINT("using layout 1, and alignment target is %ld, adjusted e_size: %ld -> %ld",
  39418. (long) DUK_HOBJECT_ALIGN_TARGET, (long) new_e_size, (long) new_e_size_adjusted));
  39419. DUK_ASSERT(new_e_size_adjusted >= new_e_size);
  39420. #else
  39421. #error invalid hobject layout defines
  39422. #endif
  39423. /*
  39424. * Debug logging after adjustment.
  39425. */
  39426. 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 "
  39427. "{e_size=%ld,a_size=%ld,h_size=%ld}, abandon_array=%ld, unadjusted new_e_size=%ld",
  39428. (void *) obj,
  39429. (long) DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE(obj),
  39430. DUK_HOBJECT_GET_ASIZE(obj),
  39431. DUK_HOBJECT_GET_HSIZE(obj)),
  39432. (long) DUK_HOBJECT_P_COMPUTE_SIZE(new_e_size_adjusted, new_a_size, new_h_size),
  39433. (void *) DUK_HOBJECT_GET_PROPS(thr->heap, obj),
  39434. (long) DUK_HOBJECT_GET_ESIZE(obj),
  39435. (long) DUK_HOBJECT_GET_ENEXT(obj),
  39436. (long) DUK_HOBJECT_GET_ASIZE(obj),
  39437. (long) DUK_HOBJECT_GET_HSIZE(obj),
  39438. (long) new_e_size_adjusted,
  39439. (long) new_a_size,
  39440. (long) new_h_size,
  39441. (long) abandon_array,
  39442. (long) new_e_size));
  39443. /*
  39444. * Property count check. This is the only point where we ensure that
  39445. * we don't get more (allocated) property space that we can handle.
  39446. * There aren't hard limits as such, but some algorithms fail (e.g.
  39447. * finding next higher prime, selecting hash part size) if we get too
  39448. * close to the 4G property limit.
  39449. *
  39450. * Since this works based on allocation size (not actually used size),
  39451. * the limit is a bit approximate but good enough in practice.
  39452. */
  39453. if (new_e_size_adjusted + new_a_size > DUK_HOBJECT_MAX_PROPERTIES) {
  39454. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_OBJECT_PROPERTY_LIMIT);
  39455. }
  39456. /*
  39457. * Compute new alloc size and alloc new area.
  39458. *
  39459. * The new area is allocated as a dynamic buffer and placed into the
  39460. * valstack for reachability. The actual buffer is then detached at
  39461. * the end.
  39462. *
  39463. * Note: heap_mark_and_sweep_base_flags are altered here to ensure
  39464. * no-one touches this object while we're resizing and rehashing it.
  39465. * The flags must be reset on every exit path after it. Finalizers
  39466. * and compaction is prevented currently for all objects while it
  39467. * would be enough to restrict it only for the current object.
  39468. */
  39469. #ifdef DUK_USE_MARK_AND_SWEEP
  39470. prev_mark_and_sweep_base_flags = thr->heap->mark_and_sweep_base_flags;
  39471. thr->heap->mark_and_sweep_base_flags |=
  39472. DUK_MS_FLAG_NO_FINALIZERS | /* avoid attempts to add/remove object keys */
  39473. DUK_MS_FLAG_NO_OBJECT_COMPACTION; /* avoid attempt to compact the current object */
  39474. #endif
  39475. new_alloc_size = DUK_HOBJECT_P_COMPUTE_SIZE(new_e_size_adjusted, new_a_size, new_h_size);
  39476. DUK_DDD(DUK_DDDPRINT("new hobject allocation size is %ld", (long) new_alloc_size));
  39477. if (new_alloc_size == 0) {
  39478. /* for zero size, don't push anything on valstack */
  39479. DUK_ASSERT(new_e_size_adjusted == 0);
  39480. DUK_ASSERT(new_a_size == 0);
  39481. DUK_ASSERT(new_h_size == 0);
  39482. new_p = NULL;
  39483. } else {
  39484. /* This may trigger mark-and-sweep with arbitrary side effects,
  39485. * including an attempted resize of the object we're resizing,
  39486. * executing a finalizer which may add or remove properties of
  39487. * the object we're resizing etc.
  39488. */
  39489. /* Note: buffer is dynamic so that we can 'steal' the actual
  39490. * allocation later.
  39491. */
  39492. new_p = (duk_uint8_t *) duk_push_dynamic_buffer(ctx, new_alloc_size); /* errors out if out of memory */
  39493. DUK_ASSERT(new_p != NULL); /* since new_alloc_size > 0 */
  39494. }
  39495. /* Set up pointers to the new property area: this is hidden behind a macro
  39496. * because it is memory layout specific.
  39497. */
  39498. DUK_HOBJECT_P_SET_REALLOC_PTRS(new_p, new_e_k, new_e_pv, new_e_f, new_a, new_h,
  39499. new_e_size_adjusted, new_a_size, new_h_size);
  39500. DUK_UNREF(new_h); /* happens when hash part dropped */
  39501. new_e_next = 0;
  39502. /* if new_p == NULL, all of these pointers are NULL */
  39503. DUK_ASSERT((new_p != NULL) ||
  39504. (new_e_k == NULL && new_e_pv == NULL && new_e_f == NULL &&
  39505. new_a == NULL && new_h == NULL));
  39506. 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",
  39507. (long) new_alloc_size, (void *) new_e_k, (void *) new_e_pv, (void *) new_e_f,
  39508. (void *) new_a, (void *) new_h));
  39509. /*
  39510. * Migrate array to start of entries if requested.
  39511. *
  39512. * Note: from an enumeration perspective the order of entry keys matters.
  39513. * Array keys should appear wherever they appeared before the array abandon
  39514. * operation.
  39515. */
  39516. if (abandon_array) {
  39517. /*
  39518. * Note: assuming new_a_size == 0, and that entry part contains
  39519. * no conflicting keys, refcounts do not need to be adjusted for
  39520. * the values, as they remain exactly the same.
  39521. *
  39522. * The keys, however, need to be interned, incref'd, and be
  39523. * reachable for GC. Any intern attempt may trigger a GC and
  39524. * claim any non-reachable strings, so every key must be reachable
  39525. * at all times.
  39526. *
  39527. * A longjmp must not occur here, as the new_p allocation would
  39528. * be freed without these keys being decref'd, hence the messy
  39529. * decref handling if intern fails.
  39530. */
  39531. DUK_ASSERT(new_a_size == 0);
  39532. for (i = 0; i < DUK_HOBJECT_GET_ASIZE(obj); i++) {
  39533. duk_tval *tv1;
  39534. duk_tval *tv2;
  39535. duk_hstring *key;
  39536. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, obj) != NULL);
  39537. tv1 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, i);
  39538. if (DUK_TVAL_IS_UNDEFINED_UNUSED(tv1)) {
  39539. continue;
  39540. }
  39541. DUK_ASSERT(new_p != NULL && new_e_k != NULL &&
  39542. new_e_pv != NULL && new_e_f != NULL);
  39543. /*
  39544. * Intern key via the valstack to ensure reachability behaves
  39545. * properly. We must avoid longjmp's here so use non-checked
  39546. * primitives.
  39547. *
  39548. * Note: duk_check_stack() potentially reallocs the valstack,
  39549. * invalidating any duk_tval pointers to valstack. Callers
  39550. * must be careful.
  39551. */
  39552. /* never shrinks; auto-adds DUK_VALSTACK_INTERNAL_EXTRA, which is generous */
  39553. if (!duk_check_stack(ctx, 1)) {
  39554. goto abandon_error;
  39555. }
  39556. DUK_ASSERT_VALSTACK_SPACE(thr, 1);
  39557. key = duk_heap_string_intern_u32(thr->heap, i);
  39558. if (!key) {
  39559. goto abandon_error;
  39560. }
  39561. duk_push_hstring(ctx, key); /* keep key reachable for GC etc; guaranteed not to fail */
  39562. /* key is now reachable in the valstack */
  39563. DUK_HSTRING_INCREF(thr, key); /* second incref for the entry reference */
  39564. new_e_k[new_e_next] = key;
  39565. tv2 = &new_e_pv[new_e_next].v; /* array entries are all plain values */
  39566. DUK_TVAL_SET_TVAL(tv2, tv1);
  39567. new_e_f[new_e_next] = DUK_PROPDESC_FLAG_WRITABLE |
  39568. DUK_PROPDESC_FLAG_ENUMERABLE |
  39569. DUK_PROPDESC_FLAG_CONFIGURABLE;
  39570. new_e_next++;
  39571. /* Note: new_e_next matches pushed temp key count, and nothing can
  39572. * fail above between the push and this point.
  39573. */
  39574. }
  39575. DUK_DDD(DUK_DDDPRINT("abandon array: pop %ld key temps from valstack", (long) new_e_next));
  39576. duk_pop_n(ctx, new_e_next);
  39577. }
  39578. /*
  39579. * Copy keys and values in the entry part (compacting them at the same time).
  39580. */
  39581. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  39582. duk_hstring *key;
  39583. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, obj) != NULL);
  39584. key = DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i);
  39585. if (!key) {
  39586. continue;
  39587. }
  39588. DUK_ASSERT(new_p != NULL && new_e_k != NULL &&
  39589. new_e_pv != NULL && new_e_f != NULL);
  39590. new_e_k[new_e_next] = key;
  39591. new_e_pv[new_e_next] = DUK_HOBJECT_E_GET_VALUE(thr->heap, obj, i);
  39592. new_e_f[new_e_next] = DUK_HOBJECT_E_GET_FLAGS(thr->heap, obj, i);
  39593. new_e_next++;
  39594. }
  39595. /* the entries [new_e_next, new_e_size_adjusted[ are left uninitialized on purpose (ok, not gc reachable) */
  39596. /*
  39597. * Copy array elements to new array part.
  39598. */
  39599. if (new_a_size > DUK_HOBJECT_GET_ASIZE(obj)) {
  39600. /* copy existing entries as is */
  39601. DUK_ASSERT(new_p != NULL && new_a != NULL);
  39602. if (DUK_HOBJECT_GET_ASIZE(obj) > 0) {
  39603. /* Avoid zero copy with an invalid pointer. If obj->p is NULL,
  39604. * the 'new_a' pointer will be invalid which is not allowed even
  39605. * when copy size is zero.
  39606. */
  39607. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, obj) != NULL);
  39608. DUK_ASSERT(DUK_HOBJECT_GET_ASIZE(obj) > 0);
  39609. DUK_MEMCPY((void *) new_a, (void *) DUK_HOBJECT_A_GET_BASE(thr->heap, obj), sizeof(duk_tval) * DUK_HOBJECT_GET_ASIZE(obj));
  39610. }
  39611. /* fill new entries with -unused- (required, gc reachable) */
  39612. for (i = DUK_HOBJECT_GET_ASIZE(obj); i < new_a_size; i++) {
  39613. duk_tval *tv = &new_a[i];
  39614. DUK_TVAL_SET_UNDEFINED_UNUSED(tv);
  39615. }
  39616. } else {
  39617. #ifdef DUK_USE_ASSERTIONS
  39618. /* caller must have decref'd values above new_a_size (if that is necessary) */
  39619. if (!abandon_array) {
  39620. for (i = new_a_size; i < DUK_HOBJECT_GET_ASIZE(obj); i++) {
  39621. duk_tval *tv;
  39622. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, i);
  39623. /* current assertion is quite strong: decref's and set to unused */
  39624. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED_UNUSED(tv));
  39625. }
  39626. }
  39627. #endif
  39628. if (new_a_size > 0) {
  39629. /* Avoid zero copy with an invalid pointer. If obj->p is NULL,
  39630. * the 'new_a' pointer will be invalid which is not allowed even
  39631. * when copy size is zero.
  39632. */
  39633. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, obj) != NULL);
  39634. DUK_ASSERT(new_a_size > 0);
  39635. DUK_MEMCPY((void *) new_a, (void *) DUK_HOBJECT_A_GET_BASE(thr->heap, obj), sizeof(duk_tval) * new_a_size);
  39636. }
  39637. }
  39638. /*
  39639. * Rebuild the hash part always from scratch (guaranteed to finish).
  39640. *
  39641. * Any resize of hash part requires rehashing. In addition, by rehashing
  39642. * get rid of any elements marked deleted (DUK__HASH_DELETED) which is critical
  39643. * to ensuring the hash part never fills up.
  39644. */
  39645. #if defined(DUK_USE_HOBJECT_HASH_PART)
  39646. if (DUK_UNLIKELY(new_h_size > 0)) {
  39647. DUK_ASSERT(new_h != NULL);
  39648. /* fill new_h with u32 0xff = UNUSED */
  39649. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, obj) != NULL);
  39650. DUK_ASSERT(new_h_size > 0);
  39651. DUK_MEMSET(new_h, 0xff, sizeof(duk_uint32_t) * new_h_size);
  39652. DUK_ASSERT(new_e_next <= new_h_size); /* equality not actually possible */
  39653. for (i = 0; i < new_e_next; i++) {
  39654. duk_hstring *key = new_e_k[i];
  39655. duk_uint32_t j, step;
  39656. DUK_ASSERT(key != NULL);
  39657. j = DUK__HASH_INITIAL(DUK_HSTRING_GET_HASH(key), new_h_size);
  39658. step = DUK__HASH_PROBE_STEP(DUK_HSTRING_GET_HASH(key));
  39659. for (;;) {
  39660. DUK_ASSERT(new_h[j] != DUK__HASH_DELETED); /* should never happen */
  39661. if (new_h[j] == DUK__HASH_UNUSED) {
  39662. DUK_DDD(DUK_DDDPRINT("rebuild hit %ld -> %ld", (long) j, (long) i));
  39663. new_h[j] = i;
  39664. break;
  39665. }
  39666. DUK_DDD(DUK_DDDPRINT("rebuild miss %ld, step %ld", (long) j, (long) step));
  39667. j = (j + step) % new_h_size;
  39668. /* guaranteed to finish */
  39669. DUK_ASSERT(j != (duk_uint32_t) DUK__HASH_INITIAL(DUK_HSTRING_GET_HASH(key), new_h_size));
  39670. }
  39671. }
  39672. } else {
  39673. DUK_DDD(DUK_DDDPRINT("no hash part, no rehash"));
  39674. }
  39675. #endif /* DUK_USE_HOBJECT_HASH_PART */
  39676. /*
  39677. * Nice debug log.
  39678. */
  39679. 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 "
  39680. "{p=%p,e_size=%ld,e_next=%ld,a_size=%ld,h_size=%ld}, abandon_array=%ld, unadjusted new_e_size=%ld",
  39681. (void *) obj,
  39682. (long) DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE(obj),
  39683. DUK_HOBJECT_GET_ASIZE(obj),
  39684. DUK_HOBJECT_GET_HSIZE(obj)),
  39685. (long) new_alloc_size,
  39686. (void *) DUK_HOBJECT_GET_PROPS(thr->heap, obj),
  39687. (long) DUK_HOBJECT_GET_ESIZE(obj),
  39688. (long) DUK_HOBJECT_GET_ENEXT(obj),
  39689. (long) DUK_HOBJECT_GET_ASIZE(obj),
  39690. (long) DUK_HOBJECT_GET_HSIZE(obj),
  39691. (void *) new_p,
  39692. (long) new_e_size_adjusted,
  39693. (long) new_e_next,
  39694. (long) new_a_size,
  39695. (long) new_h_size,
  39696. (long) abandon_array,
  39697. (long) new_e_size));
  39698. /*
  39699. * All done, switch properties ('p') allocation to new one.
  39700. */
  39701. DUK_FREE(thr->heap, DUK_HOBJECT_GET_PROPS(thr->heap, obj)); /* NULL obj->p is OK */
  39702. DUK_HOBJECT_SET_PROPS(thr->heap, obj, new_p);
  39703. DUK_HOBJECT_SET_ESIZE(obj, new_e_size_adjusted);
  39704. DUK_HOBJECT_SET_ENEXT(obj, new_e_next);
  39705. DUK_HOBJECT_SET_ASIZE(obj, new_a_size);
  39706. DUK_HOBJECT_SET_HSIZE(obj, new_h_size);
  39707. if (new_p) {
  39708. /*
  39709. * Detach actual buffer from dynamic buffer in valstack, and
  39710. * pop it from the stack.
  39711. *
  39712. * XXX: the buffer object is certainly not reachable at this point,
  39713. * so it would be nice to free it forcibly even with only
  39714. * mark-and-sweep enabled. Not a big issue though.
  39715. */
  39716. duk_hbuffer_dynamic *buf;
  39717. DUK_ASSERT(new_alloc_size > 0);
  39718. DUK_ASSERT(duk_is_buffer(ctx, -1));
  39719. buf = (duk_hbuffer_dynamic *) duk_require_hbuffer(ctx, -1);
  39720. DUK_ASSERT(buf != NULL);
  39721. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  39722. DUK_HBUFFER_DYNAMIC_SET_DATA_PTR_NULL(thr->heap, buf);
  39723. DUK_HBUFFER_DYNAMIC_SET_SIZE(buf, 0); /* these size resets are not strictly necessary, but nice for consistency */
  39724. DUK_HBUFFER_DYNAMIC_SET_ALLOC_SIZE(buf, 0);
  39725. duk_pop(ctx);
  39726. } else {
  39727. DUK_ASSERT(new_alloc_size == 0);
  39728. /* no need to pop, nothing was pushed */
  39729. }
  39730. /* clear array part flag only after switching */
  39731. if (abandon_array) {
  39732. DUK_HOBJECT_CLEAR_ARRAY_PART(obj);
  39733. }
  39734. DUK_DDD(DUK_DDDPRINT("resize result: %!O", (duk_heaphdr *) obj));
  39735. #ifdef DUK_USE_MARK_AND_SWEEP
  39736. thr->heap->mark_and_sweep_base_flags = prev_mark_and_sweep_base_flags;
  39737. #endif
  39738. /*
  39739. * Post resize assertions.
  39740. */
  39741. #ifdef DUK_USE_ASSERTIONS
  39742. /* XXX: post-checks (such as no duplicate keys) */
  39743. #endif
  39744. return;
  39745. /*
  39746. * Abandon array failed, need to decref keys already inserted
  39747. * into the beginning of new_e_k before unwinding valstack.
  39748. */
  39749. abandon_error:
  39750. DUK_D(DUK_DPRINT("hobject resize failed during abandon array, decref keys"));
  39751. i = new_e_next;
  39752. while (i > 0) {
  39753. i--;
  39754. DUK_ASSERT(new_e_k != NULL);
  39755. DUK_ASSERT(new_e_k[i] != NULL);
  39756. DUK_HSTRING_DECREF(thr, new_e_k[i]);
  39757. }
  39758. #ifdef DUK_USE_MARK_AND_SWEEP
  39759. thr->heap->mark_and_sweep_base_flags = prev_mark_and_sweep_base_flags;
  39760. #endif
  39761. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_OBJECT_RESIZE_FAILED);
  39762. }
  39763. /*
  39764. * Helpers to resize properties allocation on specific needs.
  39765. */
  39766. /* Grow entry part allocation for one additional entry. */
  39767. DUK_LOCAL void duk__grow_props_for_new_entry_item(duk_hthread *thr, duk_hobject *obj) {
  39768. duk_uint32_t old_e_used; /* actually used, non-NULL entries */
  39769. duk_uint32_t new_e_size;
  39770. duk_uint32_t new_a_size;
  39771. duk_uint32_t new_h_size;
  39772. DUK_ASSERT(thr != NULL);
  39773. DUK_ASSERT(obj != NULL);
  39774. /* Duktape 0.11.0 and prior tried to optimize the resize by not
  39775. * counting the number of actually used keys prior to the resize.
  39776. * This worked mostly well but also caused weird leak-like behavior
  39777. * as in: test-bug-object-prop-alloc-unbounded.js. So, now we count
  39778. * the keys explicitly to compute the new entry part size.
  39779. */
  39780. old_e_used = duk__count_used_e_keys(thr, obj);
  39781. new_e_size = old_e_used + duk__get_min_grow_e(old_e_used);
  39782. #if defined(DUK_USE_HOBJECT_HASH_PART)
  39783. new_h_size = duk__get_default_h_size(new_e_size);
  39784. #else
  39785. new_h_size = 0;
  39786. #endif
  39787. new_a_size = DUK_HOBJECT_GET_ASIZE(obj);
  39788. DUK_ASSERT(new_e_size >= old_e_used + 1); /* duk__get_min_grow_e() is always >= 1 */
  39789. duk__realloc_props(thr, obj, new_e_size, new_a_size, new_h_size, 0);
  39790. }
  39791. /* Grow array part for a new highest array index. */
  39792. DUK_LOCAL void duk__grow_props_for_array_item(duk_hthread *thr, duk_hobject *obj, duk_uint32_t highest_arr_idx) {
  39793. duk_uint32_t new_e_size;
  39794. duk_uint32_t new_a_size;
  39795. duk_uint32_t new_h_size;
  39796. DUK_ASSERT(thr != NULL);
  39797. DUK_ASSERT(obj != NULL);
  39798. DUK_ASSERT(highest_arr_idx >= DUK_HOBJECT_GET_ASIZE(obj));
  39799. /* minimum new length is highest_arr_idx + 1 */
  39800. new_e_size = DUK_HOBJECT_GET_ESIZE(obj);
  39801. new_h_size = DUK_HOBJECT_GET_HSIZE(obj);
  39802. new_a_size = highest_arr_idx + duk__get_min_grow_a(highest_arr_idx);
  39803. DUK_ASSERT(new_a_size >= highest_arr_idx + 1); /* duk__get_min_grow_a() is always >= 1 */
  39804. duk__realloc_props(thr, obj, new_e_size, new_a_size, new_h_size, 0);
  39805. }
  39806. /* Abandon array part, moving array entries into entries part.
  39807. * This requires a props resize, which is a heavy operation.
  39808. * We also compact the entries part while we're at it, although
  39809. * this is not strictly required.
  39810. */
  39811. DUK_LOCAL void duk__abandon_array_checked(duk_hthread *thr, duk_hobject *obj) {
  39812. duk_uint32_t new_e_size;
  39813. duk_uint32_t new_a_size;
  39814. duk_uint32_t new_h_size;
  39815. duk_uint32_t e_used; /* actually used, non-NULL keys */
  39816. duk_uint32_t a_used;
  39817. duk_uint32_t a_size;
  39818. DUK_ASSERT(thr != NULL);
  39819. DUK_ASSERT(obj != NULL);
  39820. e_used = duk__count_used_e_keys(thr, obj);
  39821. duk__compute_a_stats(thr, obj, &a_used, &a_size);
  39822. /*
  39823. * Must guarantee all actually used array entries will fit into
  39824. * new entry part. Add one growth step to ensure we don't run out
  39825. * of space right away.
  39826. */
  39827. new_e_size = e_used + a_used;
  39828. new_e_size = new_e_size + duk__get_min_grow_e(new_e_size);
  39829. new_a_size = 0;
  39830. #if defined(DUK_USE_HOBJECT_HASH_PART)
  39831. new_h_size = duk__get_default_h_size(new_e_size);
  39832. #else
  39833. new_h_size = 0;
  39834. #endif
  39835. DUK_DD(DUK_DDPRINT("abandon array part for hobject %p, "
  39836. "array stats before: e_used=%ld, a_used=%ld, a_size=%ld; "
  39837. "resize to e_size=%ld, a_size=%ld, h_size=%ld",
  39838. (void *) obj, (long) e_used, (long) a_used, (long) a_size,
  39839. (long) new_e_size, (long) new_a_size, (long) new_h_size));
  39840. duk__realloc_props(thr, obj, new_e_size, new_a_size, new_h_size, 1);
  39841. }
  39842. /*
  39843. * Compact an object. Minimizes allocation size for objects which are
  39844. * not likely to be extended. This is useful for internal and non-
  39845. * extensible objects, but can also be called for non-extensible objects.
  39846. * May abandon the array part if it is computed to be too sparse.
  39847. *
  39848. * This call is relatively expensive, as it needs to scan both the
  39849. * entries and the array part.
  39850. *
  39851. * The call may fail due to allocation error.
  39852. */
  39853. DUK_INTERNAL void duk_hobject_compact_props(duk_hthread *thr, duk_hobject *obj) {
  39854. duk_uint32_t e_size; /* currently used -> new size */
  39855. duk_uint32_t a_size; /* currently required */
  39856. duk_uint32_t a_used; /* actually used */
  39857. duk_uint32_t h_size;
  39858. duk_bool_t abandon_array;
  39859. DUK_ASSERT(thr != NULL);
  39860. DUK_ASSERT(obj != NULL);
  39861. e_size = duk__count_used_e_keys(thr, obj);
  39862. duk__compute_a_stats(thr, obj, &a_used, &a_size);
  39863. DUK_DD(DUK_DDPRINT("compacting hobject, used e keys %ld, used a keys %ld, min a size %ld, "
  39864. "resized array density would be: %ld/%ld = %lf",
  39865. (long) e_size, (long) a_used, (long) a_size,
  39866. (long) a_used, (long) a_size,
  39867. (double) a_used / (double) a_size));
  39868. if (duk__abandon_array_density_check(a_used, a_size)) {
  39869. DUK_DD(DUK_DDPRINT("decided to abandon array during compaction, a_used=%ld, a_size=%ld",
  39870. (long) a_used, (long) a_size));
  39871. abandon_array = 1;
  39872. e_size += a_used;
  39873. a_size = 0;
  39874. } else {
  39875. DUK_DD(DUK_DDPRINT("decided to keep array during compaction"));
  39876. abandon_array = 0;
  39877. }
  39878. #if defined(DUK_USE_HOBJECT_HASH_PART)
  39879. if (e_size >= DUK_HOBJECT_E_USE_HASH_LIMIT) {
  39880. h_size = duk__get_default_h_size(e_size);
  39881. } else {
  39882. h_size = 0;
  39883. }
  39884. #else
  39885. h_size = 0;
  39886. #endif
  39887. DUK_DD(DUK_DDPRINT("compacting hobject -> new e_size %ld, new a_size=%ld, new h_size=%ld, abandon_array=%ld",
  39888. (long) e_size, (long) a_size, (long) h_size, (long) abandon_array));
  39889. duk__realloc_props(thr, obj, e_size, a_size, h_size, abandon_array);
  39890. }
  39891. /*
  39892. * Find an existing key from entry part either by linear scan or by
  39893. * using the hash index (if it exists).
  39894. *
  39895. * Sets entry index (and possibly the hash index) to output variables,
  39896. * which allows the caller to update the entry and hash entries in-place.
  39897. * If entry is not found, both values are set to -1. If entry is found
  39898. * but there is no hash part, h_idx is set to -1.
  39899. */
  39900. 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) {
  39901. DUK_ASSERT(obj != NULL);
  39902. DUK_ASSERT(key != NULL);
  39903. DUK_ASSERT(e_idx != NULL);
  39904. DUK_ASSERT(h_idx != NULL);
  39905. DUK_UNREF(heap);
  39906. if (DUK_LIKELY(DUK_HOBJECT_GET_HSIZE(obj) == 0))
  39907. {
  39908. /* Linear scan: more likely because most objects are small.
  39909. * This is an important fast path.
  39910. *
  39911. * XXX: this might be worth inlining for property lookups.
  39912. */
  39913. duk_uint_fast32_t i;
  39914. duk_uint_fast32_t n;
  39915. duk_hstring **h_keys_base;
  39916. DUK_DDD(DUK_DDDPRINT("duk_hobject_find_existing_entry() using linear scan for lookup"));
  39917. h_keys_base = DUK_HOBJECT_E_GET_KEY_BASE(heap, obj);
  39918. n = DUK_HOBJECT_GET_ENEXT(obj);
  39919. for (i = 0; i < n; i++) {
  39920. if (h_keys_base[i] == key) {
  39921. *e_idx = i;
  39922. *h_idx = -1;
  39923. return;
  39924. }
  39925. }
  39926. }
  39927. #if defined(DUK_USE_HOBJECT_HASH_PART)
  39928. else
  39929. {
  39930. /* hash lookup */
  39931. duk_uint32_t n;
  39932. duk_uint32_t i, step;
  39933. duk_uint32_t *h_base;
  39934. DUK_DDD(DUK_DDDPRINT("duk_hobject_find_existing_entry() using hash part for lookup"));
  39935. h_base = DUK_HOBJECT_H_GET_BASE(heap, obj);
  39936. n = DUK_HOBJECT_GET_HSIZE(obj);
  39937. i = DUK__HASH_INITIAL(DUK_HSTRING_GET_HASH(key), n);
  39938. step = DUK__HASH_PROBE_STEP(DUK_HSTRING_GET_HASH(key));
  39939. for (;;) {
  39940. duk_uint32_t t;
  39941. DUK_ASSERT_DISABLE(i >= 0); /* unsigned */
  39942. DUK_ASSERT(i < DUK_HOBJECT_GET_HSIZE(obj));
  39943. t = h_base[i];
  39944. DUK_ASSERT(t == DUK__HASH_UNUSED || t == DUK__HASH_DELETED ||
  39945. (t < DUK_HOBJECT_GET_ESIZE(obj))); /* t >= 0 always true, unsigned */
  39946. if (t == DUK__HASH_UNUSED) {
  39947. break;
  39948. } else if (t == DUK__HASH_DELETED) {
  39949. DUK_DDD(DUK_DDDPRINT("lookup miss (deleted) i=%ld, t=%ld",
  39950. (long) i, (long) t));
  39951. } else {
  39952. DUK_ASSERT(t < DUK_HOBJECT_GET_ESIZE(obj));
  39953. if (DUK_HOBJECT_E_GET_KEY(heap, obj, t) == key) {
  39954. DUK_DDD(DUK_DDDPRINT("lookup hit i=%ld, t=%ld -> key %p",
  39955. (long) i, (long) t, (void *) key));
  39956. *e_idx = t;
  39957. *h_idx = i;
  39958. return;
  39959. }
  39960. DUK_DDD(DUK_DDDPRINT("lookup miss i=%ld, t=%ld",
  39961. (long) i, (long) t));
  39962. }
  39963. i = (i + step) % n;
  39964. /* guaranteed to finish, as hash is never full */
  39965. DUK_ASSERT(i != (duk_uint32_t) DUK__HASH_INITIAL(DUK_HSTRING_GET_HASH(key), n));
  39966. }
  39967. }
  39968. #endif /* DUK_USE_HOBJECT_HASH_PART */
  39969. /* not found */
  39970. *e_idx = -1;
  39971. *h_idx = -1;
  39972. }
  39973. /* For internal use: get non-accessor entry value */
  39974. DUK_INTERNAL duk_tval *duk_hobject_find_existing_entry_tval_ptr(duk_heap *heap, duk_hobject *obj, duk_hstring *key) {
  39975. duk_int_t e_idx;
  39976. duk_int_t h_idx;
  39977. DUK_ASSERT(obj != NULL);
  39978. DUK_ASSERT(key != NULL);
  39979. DUK_UNREF(heap);
  39980. duk_hobject_find_existing_entry(heap, obj, key, &e_idx, &h_idx);
  39981. if (e_idx >= 0 && !DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap, obj, e_idx)) {
  39982. return DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(heap, obj, e_idx);
  39983. } else {
  39984. return NULL;
  39985. }
  39986. }
  39987. /* For internal use: get non-accessor entry value and attributes */
  39988. 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) {
  39989. duk_int_t e_idx;
  39990. duk_int_t h_idx;
  39991. DUK_ASSERT(obj != NULL);
  39992. DUK_ASSERT(key != NULL);
  39993. DUK_ASSERT(out_attrs != NULL);
  39994. DUK_UNREF(heap);
  39995. duk_hobject_find_existing_entry(heap, obj, key, &e_idx, &h_idx);
  39996. if (e_idx >= 0 && !DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap, obj, e_idx)) {
  39997. *out_attrs = DUK_HOBJECT_E_GET_FLAGS(heap, obj, e_idx);
  39998. return DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(heap, obj, e_idx);
  39999. } else {
  40000. *out_attrs = 0;
  40001. return NULL;
  40002. }
  40003. }
  40004. /* For internal use: get array part value */
  40005. DUK_INTERNAL duk_tval *duk_hobject_find_existing_array_entry_tval_ptr(duk_heap *heap, duk_hobject *obj, duk_uarridx_t i) {
  40006. duk_tval *tv;
  40007. DUK_ASSERT(obj != NULL);
  40008. DUK_UNREF(heap);
  40009. if (!DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  40010. return NULL;
  40011. }
  40012. if (i >= DUK_HOBJECT_GET_ASIZE(obj)) {
  40013. return NULL;
  40014. }
  40015. tv = DUK_HOBJECT_A_GET_VALUE_PTR(heap, obj, i);
  40016. return tv;
  40017. }
  40018. /*
  40019. * Allocate and initialize a new entry, resizing the properties allocation
  40020. * if necessary. Returns entry index (e_idx) or throws an error if alloc fails.
  40021. *
  40022. * Sets the key of the entry (increasing the key's refcount), and updates
  40023. * the hash part if it exists. Caller must set value and flags, and update
  40024. * the entry value refcount. A decref for the previous value is not necessary.
  40025. */
  40026. DUK_LOCAL duk_bool_t duk__alloc_entry_checked(duk_hthread *thr, duk_hobject *obj, duk_hstring *key) {
  40027. duk_uint32_t idx;
  40028. DUK_ASSERT(thr != NULL);
  40029. DUK_ASSERT(obj != NULL);
  40030. DUK_ASSERT(key != NULL);
  40031. DUK_ASSERT(DUK_HOBJECT_GET_ENEXT(obj) <= DUK_HOBJECT_GET_ESIZE(obj));
  40032. #ifdef DUK_USE_ASSERTIONS
  40033. /* key must not already exist in entry part */
  40034. {
  40035. duk_uint_fast32_t i;
  40036. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  40037. DUK_ASSERT(DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i) != key);
  40038. }
  40039. }
  40040. #endif
  40041. if (DUK_HOBJECT_GET_ENEXT(obj) >= DUK_HOBJECT_GET_ESIZE(obj)) {
  40042. /* only need to guarantee 1 more slot, but allocation growth is in chunks */
  40043. DUK_DDD(DUK_DDDPRINT("entry part full, allocate space for one more entry"));
  40044. duk__grow_props_for_new_entry_item(thr, obj);
  40045. }
  40046. DUK_ASSERT(DUK_HOBJECT_GET_ENEXT(obj) < DUK_HOBJECT_GET_ESIZE(obj));
  40047. idx = DUK_HOBJECT_POSTINC_ENEXT(obj);
  40048. /* previous value is assumed to be garbage, so don't touch it */
  40049. DUK_HOBJECT_E_SET_KEY(thr->heap, obj, idx, key);
  40050. DUK_HSTRING_INCREF(thr, key);
  40051. #if defined(DUK_USE_HOBJECT_HASH_PART)
  40052. if (DUK_UNLIKELY(DUK_HOBJECT_GET_HSIZE(obj) > 0)) {
  40053. duk_uint32_t n;
  40054. duk_uint32_t i, step;
  40055. duk_uint32_t *h_base = DUK_HOBJECT_H_GET_BASE(thr->heap, obj);
  40056. n = DUK_HOBJECT_GET_HSIZE(obj);
  40057. i = DUK__HASH_INITIAL(DUK_HSTRING_GET_HASH(key), n);
  40058. step = DUK__HASH_PROBE_STEP(DUK_HSTRING_GET_HASH(key));
  40059. for (;;) {
  40060. duk_uint32_t t = h_base[i];
  40061. if (t == DUK__HASH_UNUSED || t == DUK__HASH_DELETED) {
  40062. DUK_DDD(DUK_DDDPRINT("duk__alloc_entry_checked() inserted key into hash part, %ld -> %ld",
  40063. (long) i, (long) idx));
  40064. DUK_ASSERT_DISABLE(i >= 0); /* unsigned */
  40065. DUK_ASSERT(i < DUK_HOBJECT_GET_HSIZE(obj));
  40066. DUK_ASSERT_DISABLE(idx >= 0);
  40067. DUK_ASSERT(idx < DUK_HOBJECT_GET_ESIZE(obj));
  40068. h_base[i] = idx;
  40069. break;
  40070. }
  40071. DUK_DDD(DUK_DDDPRINT("duk__alloc_entry_checked() miss %ld", (long) i));
  40072. i = (i + step) % n;
  40073. /* guaranteed to find an empty slot */
  40074. DUK_ASSERT(i != (duk_uint32_t) DUK__HASH_INITIAL(DUK_HSTRING_GET_HASH(key), DUK_HOBJECT_GET_HSIZE(obj)));
  40075. }
  40076. }
  40077. #endif /* DUK_USE_HOBJECT_HASH_PART */
  40078. /* Note: we could return the hash index here too, but it's not
  40079. * needed right now.
  40080. */
  40081. DUK_ASSERT_DISABLE(idx >= 0);
  40082. DUK_ASSERT(idx < DUK_HOBJECT_GET_ESIZE(obj));
  40083. DUK_ASSERT(idx < DUK_HOBJECT_GET_ENEXT(obj));
  40084. return idx;
  40085. }
  40086. /*
  40087. * Object internal value
  40088. *
  40089. * Returned value is guaranteed to be reachable / incref'd, caller does not need
  40090. * to incref OR decref. No proxies or accessors are invoked, no prototype walk.
  40091. */
  40092. DUK_INTERNAL duk_bool_t duk_hobject_get_internal_value(duk_heap *heap, duk_hobject *obj, duk_tval *tv_out) {
  40093. duk_int_t e_idx;
  40094. duk_int_t h_idx;
  40095. DUK_ASSERT(heap != NULL);
  40096. DUK_ASSERT(obj != NULL);
  40097. DUK_ASSERT(tv_out != NULL);
  40098. DUK_TVAL_SET_UNDEFINED_UNUSED(tv_out);
  40099. /* always in entry part, no need to look up parents etc */
  40100. duk_hobject_find_existing_entry(heap, obj, DUK_HEAP_STRING_INT_VALUE(heap), &e_idx, &h_idx);
  40101. if (e_idx >= 0) {
  40102. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap, obj, e_idx));
  40103. DUK_TVAL_SET_TVAL(tv_out, DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(heap, obj, e_idx));
  40104. return 1;
  40105. }
  40106. return 0;
  40107. }
  40108. DUK_INTERNAL duk_hstring *duk_hobject_get_internal_value_string(duk_heap *heap, duk_hobject *obj) {
  40109. duk_tval tv;
  40110. DUK_ASSERT(heap != NULL);
  40111. DUK_ASSERT(obj != NULL);
  40112. if (duk_hobject_get_internal_value(heap, obj, &tv)) {
  40113. duk_hstring *h;
  40114. DUK_ASSERT(DUK_TVAL_IS_STRING(&tv));
  40115. h = DUK_TVAL_GET_STRING(&tv);
  40116. return h;
  40117. }
  40118. return NULL;
  40119. }
  40120. /*
  40121. * Arguments handling helpers (argument map mainly).
  40122. *
  40123. * An arguments object has exotic behavior for some numeric indices.
  40124. * Accesses may translate to identifier operations which may have
  40125. * arbitrary side effects (potentially invalidating any duk_tval
  40126. * pointers).
  40127. */
  40128. /* Lookup 'key' from arguments internal 'map', perform a variable lookup
  40129. * if mapped, and leave the result on top of stack (and return non-zero).
  40130. * Used in E5 Section 10.6 algorithms [[Get]] and [[GetOwnProperty]].
  40131. */
  40132. DUK_LOCAL
  40133. duk_bool_t duk__lookup_arguments_map(duk_hthread *thr,
  40134. duk_hobject *obj,
  40135. duk_hstring *key,
  40136. duk_propdesc *temp_desc,
  40137. duk_hobject **out_map,
  40138. duk_hobject **out_varenv) {
  40139. duk_context *ctx = (duk_context *) thr;
  40140. duk_hobject *map;
  40141. duk_hobject *varenv;
  40142. duk_bool_t rc;
  40143. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  40144. DUK_DDD(DUK_DDDPRINT("arguments map lookup: thr=%p, obj=%p, key=%p, temp_desc=%p "
  40145. "(obj -> %!O, key -> %!O)",
  40146. (void *) thr, (void *) obj, (void *) key, (void *) temp_desc,
  40147. (duk_heaphdr *) obj, (duk_heaphdr *) key));
  40148. if (!duk__get_own_property_desc(thr, obj, DUK_HTHREAD_STRING_INT_MAP(thr), temp_desc, DUK__DESC_FLAG_PUSH_VALUE)) {
  40149. DUK_DDD(DUK_DDDPRINT("-> no 'map'"));
  40150. return 0;
  40151. }
  40152. map = duk_require_hobject(ctx, -1);
  40153. DUK_ASSERT(map != NULL);
  40154. duk_pop(ctx); /* map is reachable through obj */
  40155. if (!duk__get_own_property_desc(thr, map, key, temp_desc, DUK__DESC_FLAG_PUSH_VALUE)) {
  40156. DUK_DDD(DUK_DDDPRINT("-> 'map' exists, but key not in map"));
  40157. return 0;
  40158. }
  40159. /* [... varname] */
  40160. DUK_DDD(DUK_DDDPRINT("-> 'map' exists, and contains key, key is mapped to argument/variable binding %!T",
  40161. (duk_tval *) duk_get_tval(ctx, -1)));
  40162. DUK_ASSERT(duk_is_string(ctx, -1)); /* guaranteed when building arguments */
  40163. /* get varenv for varname (callee's declarative lexical environment) */
  40164. rc = duk__get_own_property_desc(thr, obj, DUK_HTHREAD_STRING_INT_VARENV(thr), temp_desc, DUK__DESC_FLAG_PUSH_VALUE);
  40165. DUK_UNREF(rc);
  40166. DUK_ASSERT(rc != 0); /* arguments MUST have an initialized lexical environment reference */
  40167. varenv = duk_require_hobject(ctx, -1);
  40168. DUK_ASSERT(varenv != NULL);
  40169. duk_pop(ctx); /* varenv remains reachable through 'obj' */
  40170. DUK_DDD(DUK_DDDPRINT("arguments varenv is: %!dO", (duk_heaphdr *) varenv));
  40171. /* success: leave varname in stack */
  40172. *out_map = map;
  40173. *out_varenv = varenv;
  40174. return 1; /* [... varname] */
  40175. }
  40176. /* Lookup 'key' from arguments internal 'map', and leave replacement value
  40177. * on stack top if mapped (and return non-zero).
  40178. * Used in E5 Section 10.6 algorithm for [[GetOwnProperty]] (used by [[Get]]).
  40179. */
  40180. DUK_LOCAL duk_bool_t duk__check_arguments_map_for_get(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *temp_desc) {
  40181. duk_context *ctx = (duk_context *) thr;
  40182. duk_hobject *map;
  40183. duk_hobject *varenv;
  40184. duk_hstring *varname;
  40185. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  40186. if (!duk__lookup_arguments_map(thr, obj, key, temp_desc, &map, &varenv)) {
  40187. DUK_DDD(DUK_DDDPRINT("arguments: key not mapped, no exotic get behavior"));
  40188. return 0;
  40189. }
  40190. /* [... varname] */
  40191. varname = duk_require_hstring(ctx, -1);
  40192. DUK_ASSERT(varname != NULL);
  40193. duk_pop(ctx); /* varname is still reachable */
  40194. DUK_DDD(DUK_DDDPRINT("arguments object automatic getvar for a bound variable; "
  40195. "key=%!O, varname=%!O",
  40196. (duk_heaphdr *) key,
  40197. (duk_heaphdr *) varname));
  40198. (void) duk_js_getvar_envrec(thr, varenv, varname, 1 /*throw*/);
  40199. /* [... value this_binding] */
  40200. duk_pop(ctx);
  40201. /* leave result on stack top */
  40202. return 1;
  40203. }
  40204. /* Lookup 'key' from arguments internal 'map', perform a variable write if mapped.
  40205. * Used in E5 Section 10.6 algorithm for [[DefineOwnProperty]] (used by [[Put]]).
  40206. * Assumes stack top contains 'put' value (which is NOT popped).
  40207. */
  40208. 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) {
  40209. duk_context *ctx = (duk_context *) thr;
  40210. duk_hobject *map;
  40211. duk_hobject *varenv;
  40212. duk_hstring *varname;
  40213. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  40214. if (!duk__lookup_arguments_map(thr, obj, key, temp_desc, &map, &varenv)) {
  40215. DUK_DDD(DUK_DDDPRINT("arguments: key not mapped, no exotic put behavior"));
  40216. return;
  40217. }
  40218. /* [... put_value varname] */
  40219. varname = duk_require_hstring(ctx, -1);
  40220. DUK_ASSERT(varname != NULL);
  40221. duk_pop(ctx); /* varname is still reachable */
  40222. DUK_DDD(DUK_DDDPRINT("arguments object automatic putvar for a bound variable; "
  40223. "key=%!O, varname=%!O, value=%!T",
  40224. (duk_heaphdr *) key,
  40225. (duk_heaphdr *) varname,
  40226. (duk_tval *) duk_require_tval(ctx, -1)));
  40227. /* [... put_value] */
  40228. /*
  40229. * Note: although arguments object variable mappings are only established
  40230. * for non-strict functions (and a call to a non-strict function created
  40231. * the arguments object in question), an inner strict function may be doing
  40232. * the actual property write. Hence the throw_flag applied here comes from
  40233. * the property write call.
  40234. */
  40235. duk_js_putvar_envrec(thr, varenv, varname, duk_require_tval(ctx, -1), throw_flag);
  40236. /* [... put_value] */
  40237. }
  40238. /* Lookup 'key' from arguments internal 'map', delete mapping if found.
  40239. * Used in E5 Section 10.6 algorithm for [[Delete]]. Note that the
  40240. * variable/argument itself (where the map points) is not deleted.
  40241. */
  40242. DUK_LOCAL void duk__check_arguments_map_for_delete(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *temp_desc) {
  40243. duk_context *ctx = (duk_context *) thr;
  40244. duk_hobject *map;
  40245. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  40246. if (!duk__get_own_property_desc(thr, obj, DUK_HTHREAD_STRING_INT_MAP(thr), temp_desc, DUK__DESC_FLAG_PUSH_VALUE)) {
  40247. DUK_DDD(DUK_DDDPRINT("arguments: key not mapped, no exotic delete behavior"));
  40248. return;
  40249. }
  40250. map = duk_require_hobject(ctx, -1);
  40251. DUK_ASSERT(map != NULL);
  40252. duk_pop(ctx); /* map is reachable through obj */
  40253. DUK_DDD(DUK_DDDPRINT("-> have 'map', delete key %!O from map (if exists)); ignore result",
  40254. (duk_heaphdr *) key));
  40255. /* Note: no recursion issue, we can trust 'map' to behave */
  40256. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_BEHAVIOR(map));
  40257. DUK_DDD(DUK_DDDPRINT("map before deletion: %!O", (duk_heaphdr *) map));
  40258. (void) duk_hobject_delprop_raw(thr, map, key, 0); /* ignore result */
  40259. DUK_DDD(DUK_DDDPRINT("map after deletion: %!O", (duk_heaphdr *) map));
  40260. }
  40261. /*
  40262. * Ecmascript compliant [[GetOwnProperty]](P), for internal use only.
  40263. *
  40264. * If property is found:
  40265. * - Fills descriptor fields to 'out_desc'
  40266. * - If DUK__DESC_FLAG_PUSH_VALUE is set, pushes a value related to the
  40267. * property onto the stack ('undefined' for accessor properties).
  40268. * - Returns non-zero
  40269. *
  40270. * If property is not found:
  40271. * - 'out_desc' is left in untouched state (possibly garbage)
  40272. * - Nothing is pushed onto the stack (not even with DUK__DESC_FLAG_PUSH_VALUE
  40273. * set)
  40274. * - Returns zero
  40275. *
  40276. * Notes:
  40277. *
  40278. * - Getting a property descriptor may cause an allocation (and hence
  40279. * GC) to take place, hence reachability and refcount of all related
  40280. * values matter. Reallocation of value stack, properties, etc may
  40281. * invalidate many duk_tval pointers (concretely, those which reside
  40282. * in memory areas subject to reallocation). However, heap object
  40283. * pointers are never affected (heap objects have stable pointers).
  40284. *
  40285. * - The value of a plain property is always reachable and has a non-zero
  40286. * reference count.
  40287. *
  40288. * - The value of a virtual property is not necessarily reachable from
  40289. * elsewhere and may have a refcount of zero. Hence we push it onto
  40290. * the valstack for the caller, which ensures it remains reachable
  40291. * while it is needed.
  40292. *
  40293. * - There are no virtual accessor properties. Hence, all getters and
  40294. * setters are always related to concretely stored properties, which
  40295. * ensures that the get/set functions in the resulting descriptor are
  40296. * reachable and have non-zero refcounts. Should there be virtual
  40297. * accessor properties later, this would need to change.
  40298. */
  40299. 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) {
  40300. duk_context *ctx = (duk_context *) thr;
  40301. duk_tval *tv;
  40302. DUK_DDD(DUK_DDDPRINT("duk__get_own_property_desc: thr=%p, obj=%p, key=%p, out_desc=%p, flags=%lx, "
  40303. "arr_idx=%ld (obj -> %!O, key -> %!O)",
  40304. (void *) thr, (void *) obj, (void *) key, (void *) out_desc,
  40305. (long) flags, (long) arr_idx,
  40306. (duk_heaphdr *) obj, (duk_heaphdr *) key));
  40307. DUK_ASSERT(ctx != NULL);
  40308. DUK_ASSERT(thr != NULL);
  40309. DUK_ASSERT(thr->heap != NULL);
  40310. DUK_ASSERT(obj != NULL);
  40311. DUK_ASSERT(key != NULL);
  40312. DUK_ASSERT(out_desc != NULL);
  40313. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  40314. /* XXX: optimize this filling behavior later */
  40315. out_desc->flags = 0;
  40316. out_desc->get = NULL;
  40317. out_desc->set = NULL;
  40318. out_desc->e_idx = -1;
  40319. out_desc->h_idx = -1;
  40320. out_desc->a_idx = -1;
  40321. /*
  40322. * Array part
  40323. */
  40324. if (DUK_HOBJECT_HAS_ARRAY_PART(obj) && arr_idx != DUK__NO_ARRAY_INDEX) {
  40325. if (arr_idx < DUK_HOBJECT_GET_ASIZE(obj)) {
  40326. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, arr_idx);
  40327. if (!DUK_TVAL_IS_UNDEFINED_UNUSED(tv)) {
  40328. DUK_DDD(DUK_DDDPRINT("-> found in array part"));
  40329. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  40330. duk_push_tval(ctx, tv);
  40331. }
  40332. /* implicit attributes */
  40333. out_desc->flags = DUK_PROPDESC_FLAG_WRITABLE |
  40334. DUK_PROPDESC_FLAG_CONFIGURABLE |
  40335. DUK_PROPDESC_FLAG_ENUMERABLE;
  40336. out_desc->a_idx = arr_idx;
  40337. goto prop_found;
  40338. }
  40339. }
  40340. /* assume array part is comprehensive (contains all array indexed elements
  40341. * or none of them); hence no need to check the entries part here.
  40342. */
  40343. DUK_DDD(DUK_DDDPRINT("-> not found as a concrete property (has array part, "
  40344. "should be there if present)"));
  40345. goto prop_not_found_concrete;
  40346. }
  40347. /*
  40348. * Entries part
  40349. */
  40350. duk_hobject_find_existing_entry(thr->heap, obj, key, &out_desc->e_idx, &out_desc->h_idx);
  40351. if (out_desc->e_idx >= 0) {
  40352. duk_int_t e_idx = out_desc->e_idx;
  40353. out_desc->flags = DUK_HOBJECT_E_GET_FLAGS(thr->heap, obj, e_idx);
  40354. if (out_desc->flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  40355. DUK_DDD(DUK_DDDPRINT("-> found accessor property in entry part"));
  40356. out_desc->get = DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, obj, e_idx);
  40357. out_desc->set = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, obj, e_idx);
  40358. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  40359. /* a dummy undefined value is pushed to make valstack
  40360. * behavior uniform for caller
  40361. */
  40362. duk_push_undefined(ctx);
  40363. }
  40364. } else {
  40365. DUK_DDD(DUK_DDDPRINT("-> found plain property in entry part"));
  40366. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, e_idx);
  40367. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  40368. duk_push_tval(ctx, tv);
  40369. }
  40370. }
  40371. goto prop_found;
  40372. }
  40373. /*
  40374. * Not found as a concrete property, check whether a String object
  40375. * virtual property matches.
  40376. */
  40377. prop_not_found_concrete:
  40378. if (DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(obj)) {
  40379. DUK_DDD(DUK_DDDPRINT("string object exotic property get for key: %!O, arr_idx: %ld",
  40380. (duk_heaphdr *) key, (long) arr_idx));
  40381. if (arr_idx != DUK__NO_ARRAY_INDEX) {
  40382. duk_hstring *h_val;
  40383. DUK_DDD(DUK_DDDPRINT("array index exists"));
  40384. h_val = duk_hobject_get_internal_value_string(thr->heap, obj);
  40385. DUK_ASSERT(h_val);
  40386. if (arr_idx < DUK_HSTRING_GET_CHARLEN(h_val)) {
  40387. DUK_DDD(DUK_DDDPRINT("-> found, array index inside string"));
  40388. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  40389. duk_push_hstring(ctx, h_val);
  40390. duk_substring(ctx, -1, arr_idx, arr_idx + 1); /* [str] -> [substr] */
  40391. }
  40392. out_desc->flags = DUK_PROPDESC_FLAG_ENUMERABLE | /* E5 Section 15.5.5.2 */
  40393. DUK_PROPDESC_FLAG_VIRTUAL;
  40394. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj));
  40395. return 1; /* cannot be e.g. arguments exotic, since exotic 'traits' are mutually exclusive */
  40396. } else {
  40397. /* index is above internal string length -> property is fully normal */
  40398. DUK_DDD(DUK_DDDPRINT("array index outside string -> normal property"));
  40399. }
  40400. } else if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  40401. duk_hstring *h_val;
  40402. DUK_DDD(DUK_DDDPRINT("-> found, key is 'length', length exotic behavior"));
  40403. h_val = duk_hobject_get_internal_value_string(thr->heap, obj);
  40404. DUK_ASSERT(h_val != NULL);
  40405. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  40406. duk_push_uint(ctx, (duk_uint_t) DUK_HSTRING_GET_CHARLEN(h_val));
  40407. }
  40408. out_desc->flags = DUK_PROPDESC_FLAG_VIRTUAL; /* E5 Section 15.5.5.1 */
  40409. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj));
  40410. return 1; /* cannot be arguments exotic */
  40411. }
  40412. } else if (DUK_HOBJECT_IS_BUFFEROBJECT(obj)) {
  40413. duk_hbufferobject *h_bufobj;
  40414. duk_uint_t byte_off;
  40415. duk_small_uint_t elem_size;
  40416. h_bufobj = (duk_hbufferobject *) obj;
  40417. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  40418. DUK_DDD(DUK_DDDPRINT("bufferobject property get for key: %!O, arr_idx: %ld",
  40419. (duk_heaphdr *) key, (long) arr_idx));
  40420. if (arr_idx != DUK__NO_ARRAY_INDEX) {
  40421. DUK_DDD(DUK_DDDPRINT("array index exists"));
  40422. /* Careful with wrapping: arr_idx upshift may easily wrap, whereas
  40423. * length downshift won't.
  40424. */
  40425. if (arr_idx < (h_bufobj->length >> h_bufobj->shift)) {
  40426. byte_off = arr_idx << h_bufobj->shift; /* no wrap assuming h_bufobj->length is valid */
  40427. elem_size = 1 << h_bufobj->shift;
  40428. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  40429. duk_uint8_t *data;
  40430. if (h_bufobj->buf != NULL && DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_bufobj, byte_off + elem_size)) {
  40431. data = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf) + h_bufobj->offset + byte_off;
  40432. duk_hbufferobject_push_validated_read(ctx, h_bufobj, data, elem_size);
  40433. } else {
  40434. DUK_D(DUK_DPRINT("bufferobject access out of underlying buffer, ignoring (read zero)"));
  40435. duk_push_uint(ctx, 0);
  40436. }
  40437. }
  40438. out_desc->flags = DUK_PROPDESC_FLAG_WRITABLE |
  40439. DUK_PROPDESC_FLAG_ENUMERABLE |
  40440. DUK_PROPDESC_FLAG_VIRTUAL;
  40441. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj));
  40442. return 1; /* cannot be e.g. arguments exotic, since exotic 'traits' are mutually exclusive */
  40443. } else {
  40444. /* index is above internal buffer length -> property is fully normal */
  40445. DUK_DDD(DUK_DDDPRINT("array index outside buffer -> normal property"));
  40446. }
  40447. } else if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  40448. DUK_DDD(DUK_DDDPRINT("-> found, key is 'length', length exotic behavior"));
  40449. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  40450. /* Length in elements: take into account shift, but
  40451. * intentionally don't check the underlying buffer here.
  40452. */
  40453. duk_push_uint(ctx, h_bufobj->length >> h_bufobj->shift);
  40454. }
  40455. out_desc->flags = DUK_PROPDESC_FLAG_VIRTUAL;
  40456. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj));
  40457. return 1; /* cannot be arguments exotic */
  40458. } else if (key == DUK_HTHREAD_STRING_BYTE_LENGTH(thr)) {
  40459. /* If neutered must return 0; length is zeroed during
  40460. * neutering.
  40461. */
  40462. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  40463. duk_push_uint(ctx, h_bufobj->length);
  40464. }
  40465. out_desc->flags = DUK_PROPDESC_FLAG_VIRTUAL;
  40466. return 1; /* cannot be arguments exotic */
  40467. } else if (key == DUK_HTHREAD_STRING_BYTE_OFFSET(thr)) {
  40468. /* If neutered must return 0; offset is zeroed during
  40469. * neutering.
  40470. */
  40471. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  40472. duk_push_uint(ctx, h_bufobj->offset);
  40473. }
  40474. out_desc->flags = DUK_PROPDESC_FLAG_VIRTUAL;
  40475. return 1; /* cannot be arguments exotic */
  40476. } else if (key == DUK_HTHREAD_STRING_BYTES_PER_ELEMENT(thr)) {
  40477. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  40478. duk_push_uint(ctx, 1 << h_bufobj->shift);
  40479. }
  40480. out_desc->flags = DUK_PROPDESC_FLAG_VIRTUAL;
  40481. return 1; /* cannot be arguments exotic */
  40482. }
  40483. } else if (DUK_HOBJECT_HAS_EXOTIC_DUKFUNC(obj)) {
  40484. DUK_DDD(DUK_DDDPRINT("duktape/c object exotic property get for key: %!O, arr_idx: %ld",
  40485. (duk_heaphdr *) key, (long) arr_idx));
  40486. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  40487. DUK_DDD(DUK_DDDPRINT("-> found, key is 'length', length exotic behavior"));
  40488. if (flags & DUK__DESC_FLAG_PUSH_VALUE) {
  40489. duk_int16_t func_nargs = ((duk_hnativefunction *) obj)->nargs;
  40490. duk_push_int(ctx, func_nargs == DUK_HNATIVEFUNCTION_NARGS_VARARGS ? 0 : func_nargs);
  40491. }
  40492. out_desc->flags = DUK_PROPDESC_FLAG_VIRTUAL; /* not enumerable */
  40493. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj));
  40494. return 1; /* cannot be arguments exotic */
  40495. }
  40496. }
  40497. /* Array properties have exotic behavior but they are concrete,
  40498. * so no special handling here.
  40499. *
  40500. * Arguments exotic behavior (E5 Section 10.6, [[GetOwnProperty]]
  40501. * is only relevant as a post-check implemented below; hence no
  40502. * check here.
  40503. */
  40504. /*
  40505. * Not found as concrete or virtual
  40506. */
  40507. DUK_DDD(DUK_DDDPRINT("-> not found (virtual, entry part, or array part)"));
  40508. return 0;
  40509. /*
  40510. * Found
  40511. *
  40512. * Arguments object has exotic post-processing, see E5 Section 10.6,
  40513. * description of [[GetOwnProperty]] variant for arguments.
  40514. */
  40515. prop_found:
  40516. DUK_DDD(DUK_DDDPRINT("-> property found, checking for arguments exotic post-behavior"));
  40517. /* Notes:
  40518. * - only numbered indices are relevant, so arr_idx fast reject is good
  40519. * (this is valid unless there are more than 4**32-1 arguments).
  40520. * - since variable lookup has no side effects, this can be skipped if
  40521. * DUK__DESC_FLAG_PUSH_VALUE is not set.
  40522. */
  40523. if (DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj) &&
  40524. arr_idx != DUK__NO_ARRAY_INDEX &&
  40525. (flags & DUK__DESC_FLAG_PUSH_VALUE)) {
  40526. duk_propdesc temp_desc;
  40527. /* Magically bound variable cannot be an accessor. However,
  40528. * there may be an accessor property (or a plain property) in
  40529. * place with magic behavior removed. This happens e.g. when
  40530. * a magic property is redefined with defineProperty().
  40531. * Cannot assert for "not accessor" here.
  40532. */
  40533. /* replaces top of stack with new value if necessary */
  40534. DUK_ASSERT((flags & DUK__DESC_FLAG_PUSH_VALUE) != 0);
  40535. if (duk__check_arguments_map_for_get(thr, obj, key, &temp_desc)) {
  40536. DUK_DDD(DUK_DDDPRINT("-> arguments exotic behavior overrides result: %!T -> %!T",
  40537. (duk_tval *) duk_get_tval(ctx, -2),
  40538. (duk_tval *) duk_get_tval(ctx, -1)));
  40539. /* [... old_result result] -> [... result] */
  40540. duk_remove(ctx, -2);
  40541. }
  40542. }
  40543. return 1;
  40544. }
  40545. 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) {
  40546. DUK_ASSERT(thr != NULL);
  40547. DUK_ASSERT(obj != NULL);
  40548. DUK_ASSERT(key != NULL);
  40549. DUK_ASSERT(out_desc != NULL);
  40550. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  40551. return duk__get_own_property_desc_raw(thr, obj, key, DUK_HSTRING_GET_ARRIDX_SLOW(key), out_desc, flags);
  40552. }
  40553. /*
  40554. * Ecmascript compliant [[GetProperty]](P), for internal use only.
  40555. *
  40556. * If property is found:
  40557. * - Fills descriptor fields to 'out_desc'
  40558. * - If DUK__DESC_FLAG_PUSH_VALUE is set, pushes a value related to the
  40559. * property onto the stack ('undefined' for accessor properties).
  40560. * - Returns non-zero
  40561. *
  40562. * If property is not found:
  40563. * - 'out_desc' is left in untouched state (possibly garbage)
  40564. * - Nothing is pushed onto the stack (not even with DUK__DESC_FLAG_PUSH_VALUE
  40565. * set)
  40566. * - Returns zero
  40567. *
  40568. * May cause arbitrary side effects and invalidate (most) duk_tval
  40569. * pointers.
  40570. */
  40571. 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) {
  40572. duk_hobject *curr;
  40573. duk_uint32_t arr_idx;
  40574. duk_uint_t sanity;
  40575. DUK_ASSERT(thr != NULL);
  40576. DUK_ASSERT(thr->heap != NULL);
  40577. DUK_ASSERT(obj != NULL);
  40578. DUK_ASSERT(key != NULL);
  40579. DUK_ASSERT(out_desc != NULL);
  40580. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  40581. arr_idx = DUK_HSTRING_GET_ARRIDX_FAST(key);
  40582. DUK_DDD(DUK_DDDPRINT("duk__get_property_desc: thr=%p, obj=%p, key=%p, out_desc=%p, flags=%lx, "
  40583. "arr_idx=%ld (obj -> %!O, key -> %!O)",
  40584. (void *) thr, (void *) obj, (void *) key, (void *) out_desc,
  40585. (long) flags, (long) arr_idx,
  40586. (duk_heaphdr *) obj, (duk_heaphdr *) key));
  40587. curr = obj;
  40588. DUK_ASSERT(curr != NULL);
  40589. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  40590. do {
  40591. if (duk__get_own_property_desc_raw(thr, curr, key, arr_idx, out_desc, flags)) {
  40592. /* stack contains value (if requested), 'out_desc' is set */
  40593. return 1;
  40594. }
  40595. /* not found in 'curr', next in prototype chain; impose max depth */
  40596. if (sanity-- == 0) {
  40597. if (flags & DUK__DESC_FLAG_IGNORE_PROTOLOOP) {
  40598. /* treat like property not found */
  40599. break;
  40600. } else {
  40601. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  40602. }
  40603. }
  40604. curr = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, curr);
  40605. } while (curr);
  40606. /* out_desc is left untouched (possibly garbage), caller must use return
  40607. * value to determine whether out_desc can be looked up
  40608. */
  40609. return 0;
  40610. }
  40611. /*
  40612. * Shallow fast path checks for accessing array elements with numeric
  40613. * indices. The goal is to try to avoid coercing an array index to an
  40614. * (interned) string for the most common lookups, in particular, for
  40615. * standard Array objects.
  40616. *
  40617. * Interning is avoided but only for a very narrow set of cases:
  40618. * - Object has array part, index is within array allocation, and
  40619. * value is not unused (= key exists)
  40620. * - Object has no interfering exotic behavior (e.g. arguments or
  40621. * string object exotic behaviors interfere, array exotic
  40622. * behavior does not).
  40623. *
  40624. * Current shortcoming: if key does not exist (even if it is within
  40625. * the array allocation range) a slow path lookup with interning is
  40626. * always required. This can probably be fixed so that there is a
  40627. * quick fast path for non-existent elements as well, at least for
  40628. * standard Array objects.
  40629. */
  40630. DUK_LOCAL duk_tval *duk__getprop_shallow_fastpath_array_tval(duk_hthread *thr, duk_hobject *obj, duk_tval *tv_key) {
  40631. duk_tval *tv;
  40632. duk_uint32_t idx;
  40633. DUK_UNREF(thr);
  40634. if (!(DUK_HOBJECT_HAS_ARRAY_PART(obj) &&
  40635. !DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj) &&
  40636. !DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(obj) &&
  40637. !DUK_HOBJECT_IS_BUFFEROBJECT(obj) &&
  40638. !DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(obj))) {
  40639. /* Must have array part and no conflicting exotic behaviors.
  40640. * Doesn't need to have array special behavior, e.g. Arguments
  40641. * object has array part.
  40642. */
  40643. return NULL;
  40644. }
  40645. /* Arrays never have other exotic behaviors. */
  40646. DUK_DDD(DUK_DDDPRINT("fast path attempt (no exotic string/arguments/buffer "
  40647. "behavior, object has array part)"));
  40648. #if defined(DUK_USE_FASTINT)
  40649. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  40650. idx = duk__tval_fastint_to_arr_idx(tv_key);
  40651. } else
  40652. #endif
  40653. if (DUK_TVAL_IS_DOUBLE(tv_key)) {
  40654. idx = duk__tval_number_to_arr_idx(tv_key);
  40655. } else {
  40656. DUK_DDD(DUK_DDDPRINT("key is not a number"));
  40657. return NULL;
  40658. }
  40659. /* If index is not valid, idx will be DUK__NO_ARRAY_INDEX which
  40660. * is 0xffffffffUL. We don't need to check for that explicitly
  40661. * because 0xffffffffUL will never be inside object 'a_size'.
  40662. */
  40663. if (idx >= DUK_HOBJECT_GET_ASIZE(obj)) {
  40664. DUK_DDD(DUK_DDDPRINT("key is not an array index or outside array part"));
  40665. return NULL;
  40666. }
  40667. DUK_ASSERT(idx != 0xffffffffUL);
  40668. DUK_ASSERT(idx != DUK__NO_ARRAY_INDEX);
  40669. /* XXX: for array instances we could take a shortcut here and assume
  40670. * Array.prototype doesn't contain an array index property.
  40671. */
  40672. DUK_DDD(DUK_DDDPRINT("key is a valid array index and inside array part"));
  40673. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, idx);
  40674. if (!DUK_TVAL_IS_UNDEFINED_UNUSED(tv)) {
  40675. DUK_DDD(DUK_DDDPRINT("-> fast path successful"));
  40676. return tv;
  40677. }
  40678. DUK_DDD(DUK_DDDPRINT("fast path attempt failed, fall back to slow path"));
  40679. return NULL;
  40680. }
  40681. 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) {
  40682. duk_tval *tv;
  40683. duk_uint32_t idx;
  40684. duk_tval tv_tmp;
  40685. duk_uint32_t old_len, new_len;
  40686. if (!(DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj) &&
  40687. DUK_HOBJECT_HAS_ARRAY_PART(obj) &&
  40688. DUK_HOBJECT_HAS_EXTENSIBLE(obj))) {
  40689. return 0;
  40690. }
  40691. #if defined(DUK_USE_FASTINT)
  40692. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  40693. idx = duk__tval_fastint_to_arr_idx(tv_key);
  40694. } else
  40695. #endif
  40696. if (DUK_TVAL_IS_DOUBLE(tv_key)) {
  40697. idx = duk__tval_number_to_arr_idx(tv_key);
  40698. } else {
  40699. DUK_DDD(DUK_DDDPRINT("key is not a number"));
  40700. return 0;
  40701. }
  40702. /* If index is not valid, idx will be DUK__NO_ARRAY_INDEX which
  40703. * is 0xffffffffUL. We don't need to check for that explicitly
  40704. * because 0xffffffffUL will never be inside object 'a_size'.
  40705. */
  40706. if (idx >= DUK_HOBJECT_GET_ASIZE(obj)) { /* for resizing of array part, use slow path */
  40707. return 0;
  40708. }
  40709. DUK_ASSERT(idx != 0xffffffffUL);
  40710. DUK_ASSERT(idx != DUK__NO_ARRAY_INDEX);
  40711. old_len = duk__get_old_array_length(thr, obj, temp_desc);
  40712. if (idx >= old_len) {
  40713. DUK_DDD(DUK_DDDPRINT("write new array entry requires length update "
  40714. "(arr_idx=%ld, old_len=%ld)",
  40715. (long) idx, (long) old_len));
  40716. if (!(temp_desc->flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  40717. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_WRITABLE);
  40718. return 0; /* not reachable */
  40719. }
  40720. new_len = idx + 1;
  40721. /* No resize has occurred so temp_desc->e_idx is still OK */
  40722. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, temp_desc->e_idx);
  40723. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  40724. #if defined(DUK_USE_FASTINT)
  40725. DUK_TVAL_SET_FASTINT_U32(tv, new_len); /* no need for decref/incref because value is a number */
  40726. #else
  40727. DUK_TVAL_SET_NUMBER(tv, (duk_double_t) new_len); /* no need for decref/incref because value is a number */
  40728. #endif
  40729. } else {
  40730. ;
  40731. }
  40732. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, idx);
  40733. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  40734. DUK_TVAL_SET_TVAL(tv, tv_val);
  40735. DUK_TVAL_INCREF(thr, tv);
  40736. DUK_TVAL_DECREF(thr, &tv_tmp); /* note: may trigger gc and props compaction, must be last */
  40737. DUK_DDD(DUK_DDDPRINT("array fast path success for index %ld", (long) idx));
  40738. return 1;
  40739. }
  40740. /*
  40741. * Fast path for bufferobject getprop/putprop
  40742. */
  40743. DUK_LOCAL duk_bool_t duk__getprop_fastpath_bufobj_tval(duk_hthread *thr, duk_hobject *obj, duk_tval *tv_key) {
  40744. duk_context *ctx;
  40745. duk_uint32_t idx;
  40746. duk_hbufferobject *h_bufobj;
  40747. duk_uint_t byte_off;
  40748. duk_small_uint_t elem_size;
  40749. duk_uint8_t *data;
  40750. ctx = (duk_context *) thr;
  40751. if (!DUK_HOBJECT_IS_BUFFEROBJECT(obj)) {
  40752. return 0;
  40753. }
  40754. h_bufobj = (duk_hbufferobject *) obj;
  40755. #if defined(DUK_USE_FASTINT)
  40756. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  40757. idx = duk__tval_fastint_to_arr_idx(tv_key);
  40758. } else
  40759. #endif
  40760. if (DUK_TVAL_IS_DOUBLE(tv_key)) {
  40761. idx = duk__tval_number_to_arr_idx(tv_key);
  40762. } else {
  40763. return 0;
  40764. }
  40765. /* If index is not valid, idx will be DUK__NO_ARRAY_INDEX which
  40766. * is 0xffffffffUL. We don't need to check for that explicitly
  40767. * because 0xffffffffUL will never be inside bufferobject length.
  40768. */
  40769. /* Careful with wrapping (left shifting idx would be unsafe). */
  40770. if (idx >= (h_bufobj->length >> h_bufobj->shift)) {
  40771. return 0;
  40772. }
  40773. DUK_ASSERT(idx != DUK__NO_ARRAY_INDEX);
  40774. byte_off = idx << h_bufobj->shift; /* no wrap assuming h_bufobj->length is valid */
  40775. elem_size = 1 << h_bufobj->shift;
  40776. if (h_bufobj->buf != NULL && DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_bufobj, byte_off + elem_size)) {
  40777. data = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf) + h_bufobj->offset + byte_off;
  40778. duk_hbufferobject_push_validated_read(ctx, h_bufobj, data, elem_size);
  40779. } else {
  40780. DUK_D(DUK_DPRINT("bufferobject access out of underlying buffer, ignoring (read zero)"));
  40781. duk_push_uint(ctx, 0);
  40782. }
  40783. return 1;
  40784. }
  40785. DUK_LOCAL duk_bool_t duk__putprop_fastpath_bufobj_tval(duk_hthread *thr, duk_hobject *obj, duk_tval *tv_key, duk_tval *tv_val) {
  40786. duk_context *ctx;
  40787. duk_uint32_t idx;
  40788. duk_hbufferobject *h_bufobj;
  40789. duk_uint_t byte_off;
  40790. duk_small_uint_t elem_size;
  40791. duk_uint8_t *data;
  40792. ctx = (duk_context *) thr;
  40793. if (!(DUK_HOBJECT_IS_BUFFEROBJECT(obj) &&
  40794. DUK_TVAL_IS_NUMBER(tv_val))) {
  40795. return 0;
  40796. }
  40797. h_bufobj = (duk_hbufferobject *) obj;
  40798. #if defined(DUK_USE_FASTINT)
  40799. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  40800. idx = duk__tval_fastint_to_arr_idx(tv_key);
  40801. } else
  40802. #endif
  40803. if (DUK_TVAL_IS_DOUBLE(tv_key)) {
  40804. idx = duk__tval_number_to_arr_idx(tv_key);
  40805. } else {
  40806. return 0;
  40807. }
  40808. /* If index is not valid, idx will be DUK__NO_ARRAY_INDEX which
  40809. * is 0xffffffffUL. We don't need to check for that explicitly
  40810. * because 0xffffffffUL will never be inside bufferobject length.
  40811. */
  40812. /* Careful with wrapping (left shifting idx would be unsafe). */
  40813. if (idx >= (h_bufobj->length >> h_bufobj->shift)) {
  40814. return 0;
  40815. }
  40816. DUK_ASSERT(idx != DUK__NO_ARRAY_INDEX);
  40817. byte_off = idx << h_bufobj->shift; /* no wrap assuming h_bufobj->length is valid */
  40818. elem_size = 1 << h_bufobj->shift;
  40819. /* Value is required to be a number in the fast path so there
  40820. * are no side effects in write coercion.
  40821. */
  40822. duk_push_tval(ctx, tv_val);
  40823. DUK_ASSERT(duk_is_number(ctx, -1));
  40824. if (h_bufobj->buf != NULL && DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_bufobj, byte_off + elem_size)) {
  40825. data = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf) + h_bufobj->offset + byte_off;
  40826. duk_hbufferobject_validated_write(ctx, h_bufobj, data, elem_size);
  40827. } else {
  40828. DUK_D(DUK_DPRINT("bufferobject access out of underlying buffer, ignoring (write skipped)"));
  40829. }
  40830. duk_pop(ctx);
  40831. return 1;
  40832. }
  40833. /*
  40834. * GETPROP: Ecmascript property read.
  40835. */
  40836. DUK_INTERNAL duk_bool_t duk_hobject_getprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key) {
  40837. duk_context *ctx = (duk_context *) thr;
  40838. duk_tval tv_obj_copy;
  40839. duk_tval tv_key_copy;
  40840. duk_hobject *curr = NULL;
  40841. duk_hstring *key = NULL;
  40842. duk_uint32_t arr_idx = DUK__NO_ARRAY_INDEX;
  40843. duk_propdesc desc;
  40844. duk_uint_t sanity;
  40845. DUK_DDD(DUK_DDDPRINT("getprop: thr=%p, obj=%p, key=%p (obj -> %!T, key -> %!T)",
  40846. (void *) thr, (void *) tv_obj, (void *) tv_key,
  40847. (duk_tval *) tv_obj, (duk_tval *) tv_key));
  40848. DUK_ASSERT(ctx != NULL);
  40849. DUK_ASSERT(thr != NULL);
  40850. DUK_ASSERT(thr->heap != NULL);
  40851. DUK_ASSERT(tv_obj != NULL);
  40852. DUK_ASSERT(tv_key != NULL);
  40853. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  40854. /*
  40855. * Make a copy of tv_obj, tv_key, and tv_val to avoid any issues of
  40856. * them being invalidated by a valstack resize.
  40857. *
  40858. * XXX: this is now an overkill for many fast paths. Rework this
  40859. * to be faster (although switching to a valstack discipline might
  40860. * be a better solution overall).
  40861. */
  40862. DUK_TVAL_SET_TVAL(&tv_obj_copy, tv_obj);
  40863. DUK_TVAL_SET_TVAL(&tv_key_copy, tv_key);
  40864. tv_obj = &tv_obj_copy;
  40865. tv_key = &tv_key_copy;
  40866. /*
  40867. * Coercion and fast path processing
  40868. */
  40869. switch (DUK_TVAL_GET_TAG(tv_obj)) {
  40870. case DUK_TAG_UNDEFINED:
  40871. case DUK_TAG_NULL: {
  40872. /* Note: unconditional throw */
  40873. DUK_DDD(DUK_DDDPRINT("base object is undefined or null -> reject"));
  40874. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_BASE);
  40875. return 0;
  40876. }
  40877. case DUK_TAG_BOOLEAN: {
  40878. DUK_DDD(DUK_DDDPRINT("base object is a boolean, start lookup from boolean prototype"));
  40879. curr = thr->builtins[DUK_BIDX_BOOLEAN_PROTOTYPE];
  40880. break;
  40881. }
  40882. case DUK_TAG_STRING: {
  40883. duk_hstring *h = DUK_TVAL_GET_STRING(tv_obj);
  40884. duk_int_t pop_count;
  40885. #if defined(DUK_USE_FASTINT)
  40886. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  40887. arr_idx = duk__tval_fastint_to_arr_idx(tv_key);
  40888. DUK_DDD(DUK_DDDPRINT("base object string, key is a fast-path fastint; arr_idx %ld", (long) arr_idx));
  40889. pop_count = 0;
  40890. } else
  40891. #endif
  40892. if (DUK_TVAL_IS_NUMBER(tv_key)) {
  40893. arr_idx = duk__tval_number_to_arr_idx(tv_key);
  40894. DUK_DDD(DUK_DDDPRINT("base object string, key is a fast-path number; arr_idx %ld", (long) arr_idx));
  40895. pop_count = 0;
  40896. } else {
  40897. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  40898. DUK_ASSERT(key != NULL);
  40899. DUK_DDD(DUK_DDDPRINT("base object string, key is a non-fast-path number; after "
  40900. "coercion key is %!T, arr_idx %ld",
  40901. (duk_tval *) duk_get_tval(ctx, -1), (long) arr_idx));
  40902. pop_count = 1;
  40903. }
  40904. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  40905. arr_idx < DUK_HSTRING_GET_CHARLEN(h)) {
  40906. duk_pop_n(ctx, pop_count);
  40907. duk_push_hstring(ctx, h);
  40908. duk_substring(ctx, -1, arr_idx, arr_idx + 1); /* [str] -> [substr] */
  40909. DUK_DDD(DUK_DDDPRINT("-> %!T (base is string, key is an index inside string length "
  40910. "after coercion -> return char)",
  40911. (duk_tval *) duk_get_tval(ctx, -1)));
  40912. return 1;
  40913. }
  40914. if (pop_count == 0) {
  40915. /* This is a pretty awkward control flow, but we need to recheck the
  40916. * key coercion here.
  40917. */
  40918. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  40919. DUK_ASSERT(key != NULL);
  40920. DUK_DDD(DUK_DDDPRINT("base object string, key is a non-fast-path number; after "
  40921. "coercion key is %!T, arr_idx %ld",
  40922. (duk_tval *) duk_get_tval(ctx, -1), (long) arr_idx));
  40923. }
  40924. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  40925. duk_pop(ctx); /* [key] -> [] */
  40926. duk_push_uint(ctx, (duk_uint_t) DUK_HSTRING_GET_CHARLEN(h)); /* [] -> [res] */
  40927. DUK_DDD(DUK_DDDPRINT("-> %!T (base is string, key is 'length' after coercion -> "
  40928. "return string length)",
  40929. (duk_tval *) duk_get_tval(ctx, -1)));
  40930. return 1;
  40931. }
  40932. DUK_DDD(DUK_DDDPRINT("base object is a string, start lookup from string prototype"));
  40933. curr = thr->builtins[DUK_BIDX_STRING_PROTOTYPE];
  40934. goto lookup; /* avoid double coercion */
  40935. }
  40936. case DUK_TAG_OBJECT: {
  40937. duk_tval *tmp;
  40938. curr = DUK_TVAL_GET_OBJECT(tv_obj);
  40939. DUK_ASSERT(curr != NULL);
  40940. tmp = duk__getprop_shallow_fastpath_array_tval(thr, curr, tv_key);
  40941. if (tmp) {
  40942. duk_push_tval(ctx, tmp);
  40943. DUK_DDD(DUK_DDDPRINT("-> %!T (base is object, key is a number, array part "
  40944. "fast path)",
  40945. (duk_tval *) duk_get_tval(ctx, -1)));
  40946. return 1;
  40947. }
  40948. if (duk__getprop_fastpath_bufobj_tval(thr, curr, tv_key) != 0) {
  40949. /* Read value pushed on stack. */
  40950. DUK_DDD(DUK_DDDPRINT("-> %!T (base is bufobj, key is a number, bufferobject "
  40951. "fast path)",
  40952. (duk_tval *) duk_get_tval(ctx, -1)));
  40953. return 1;
  40954. }
  40955. #if defined(DUK_USE_ES6_PROXY)
  40956. if (DUK_UNLIKELY(DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(curr))) {
  40957. duk_hobject *h_target;
  40958. if (duk__proxy_check_prop(thr, curr, DUK_STRIDX_GET, tv_key, &h_target)) {
  40959. /* -> [ ... trap handler ] */
  40960. DUK_DDD(DUK_DDDPRINT("-> proxy object 'get' for key %!T", (duk_tval *) tv_key));
  40961. duk_push_hobject(ctx, h_target); /* target */
  40962. duk_push_tval(ctx, tv_key); /* P */
  40963. duk_push_tval(ctx, tv_obj); /* Receiver: Proxy object */
  40964. duk_call_method(ctx, 3 /*nargs*/);
  40965. /* Target object must be checked for a conflicting
  40966. * non-configurable property.
  40967. */
  40968. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  40969. DUK_ASSERT(key != NULL);
  40970. if (duk__get_own_property_desc_raw(thr, h_target, key, arr_idx, &desc, DUK__DESC_FLAG_PUSH_VALUE)) {
  40971. duk_tval *tv_hook = duk_require_tval(ctx, -3); /* value from hook */
  40972. duk_tval *tv_targ = duk_require_tval(ctx, -1); /* value from target */
  40973. duk_bool_t datadesc_reject;
  40974. duk_bool_t accdesc_reject;
  40975. DUK_DDD(DUK_DDDPRINT("proxy 'get': target has matching property %!O, check for "
  40976. "conflicting property; tv_hook=%!T, tv_targ=%!T, desc.flags=0x%08lx, "
  40977. "desc.get=%p, desc.set=%p",
  40978. (duk_heaphdr *) key, (duk_tval *) tv_hook, (duk_tval *) tv_targ,
  40979. (unsigned long) desc.flags,
  40980. (void *) desc.get, (void *) desc.set));
  40981. datadesc_reject = !(desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) &&
  40982. !(desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) &&
  40983. !(desc.flags & DUK_PROPDESC_FLAG_WRITABLE) &&
  40984. !duk_js_samevalue(tv_hook, tv_targ);
  40985. accdesc_reject = (desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) &&
  40986. !(desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) &&
  40987. (desc.get == NULL) &&
  40988. !DUK_TVAL_IS_UNDEFINED(tv_hook);
  40989. if (datadesc_reject || accdesc_reject) {
  40990. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROXY_REJECTED);
  40991. }
  40992. duk_pop_2(ctx);
  40993. } else {
  40994. duk_pop(ctx);
  40995. }
  40996. return 1; /* return value */
  40997. }
  40998. curr = h_target; /* resume lookup from target */
  40999. DUK_TVAL_SET_OBJECT(tv_obj, curr);
  41000. }
  41001. #endif /* DUK_USE_ES6_PROXY */
  41002. if (DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(curr)) {
  41003. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  41004. DUK_ASSERT(key != NULL);
  41005. if (duk__check_arguments_map_for_get(thr, curr, key, &desc)) {
  41006. DUK_DDD(DUK_DDDPRINT("-> %!T (base is object with arguments exotic behavior, "
  41007. "key matches magically bound property -> skip standard "
  41008. "Get with replacement value)",
  41009. (duk_tval *) duk_get_tval(ctx, -1)));
  41010. /* no need for 'caller' post-check, because 'key' must be an array index */
  41011. duk_remove(ctx, -2); /* [key result] -> [result] */
  41012. return 1;
  41013. }
  41014. goto lookup; /* avoid double coercion */
  41015. }
  41016. break;
  41017. }
  41018. /* Buffer has virtual properties similar to string, but indexed values
  41019. * are numbers, not 1-byte buffers/strings which would perform badly.
  41020. */
  41021. case DUK_TAG_BUFFER: {
  41022. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv_obj);
  41023. duk_int_t pop_count;
  41024. /*
  41025. * Because buffer values are often looped over, a number fast path
  41026. * is important.
  41027. */
  41028. #if defined(DUK_USE_FASTINT)
  41029. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  41030. arr_idx = duk__tval_fastint_to_arr_idx(tv_key);
  41031. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a fast-path fastint; arr_idx %ld", (long) arr_idx));
  41032. pop_count = 0;
  41033. }
  41034. else
  41035. #endif
  41036. if (DUK_TVAL_IS_NUMBER(tv_key)) {
  41037. arr_idx = duk__tval_number_to_arr_idx(tv_key);
  41038. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a fast-path number; arr_idx %ld", (long) arr_idx));
  41039. pop_count = 0;
  41040. } else {
  41041. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  41042. DUK_ASSERT(key != NULL);
  41043. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a non-fast-path number; after "
  41044. "coercion key is %!T, arr_idx %ld",
  41045. (duk_tval *) duk_get_tval(ctx, -1), (long) arr_idx));
  41046. pop_count = 1;
  41047. }
  41048. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  41049. arr_idx < DUK_HBUFFER_GET_SIZE(h)) {
  41050. duk_pop_n(ctx, pop_count);
  41051. duk_push_uint(ctx, ((duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h))[arr_idx]);
  41052. DUK_DDD(DUK_DDDPRINT("-> %!T (base is buffer, key is an index inside buffer length "
  41053. "after coercion -> return byte as number)",
  41054. (duk_tval *) duk_get_tval(ctx, -1)));
  41055. return 1;
  41056. }
  41057. if (pop_count == 0) {
  41058. /* This is a pretty awkward control flow, but we need to recheck the
  41059. * key coercion here.
  41060. */
  41061. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  41062. DUK_ASSERT(key != NULL);
  41063. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a non-fast-path number; after "
  41064. "coercion key is %!T, arr_idx %ld",
  41065. (duk_tval *) duk_get_tval(ctx, -1), (long) arr_idx));
  41066. }
  41067. if (key == DUK_HTHREAD_STRING_LENGTH(thr) ||
  41068. key == DUK_HTHREAD_STRING_BYTE_LENGTH(thr)) {
  41069. duk_pop(ctx); /* [key] -> [] */
  41070. duk_push_uint(ctx, (duk_uint_t) DUK_HBUFFER_GET_SIZE(h)); /* [] -> [res] */
  41071. DUK_DDD(DUK_DDDPRINT("-> %!T (base is buffer, key is 'length' or 'byteLength' "
  41072. "after coercion -> return buffer length)",
  41073. (duk_tval *) duk_get_tval(ctx, -1)));
  41074. return 1;
  41075. } else if (key == DUK_HTHREAD_STRING_BYTE_OFFSET(thr)) {
  41076. duk_pop(ctx); /* [key] -> [] */
  41077. duk_push_uint(ctx, 0); /* [] -> [res] */
  41078. DUK_DDD(DUK_DDDPRINT("-> %!T (base is buffer, key is 'byteOffset' after coercion -> "
  41079. "return 0 for consistency with Buffer objects)",
  41080. (duk_tval *) duk_get_tval(ctx, -1)));
  41081. return 1;
  41082. } else if (key == DUK_HTHREAD_STRING_BYTES_PER_ELEMENT(thr)) {
  41083. duk_pop(ctx); /* [key] -> [] */
  41084. duk_push_uint(ctx, 1); /* [] -> [res] */
  41085. DUK_DDD(DUK_DDDPRINT("-> %!T (base is buffer, key is 'BYTES_PER_ELEMENT' after coercion -> "
  41086. "return 1 for consistency with Buffer objects)",
  41087. (duk_tval *) duk_get_tval(ctx, -1)));
  41088. return 1;
  41089. }
  41090. DUK_DDD(DUK_DDDPRINT("base object is a buffer, start lookup from buffer prototype"));
  41091. curr = thr->builtins[DUK_BIDX_BUFFER_PROTOTYPE];
  41092. goto lookup; /* avoid double coercion */
  41093. }
  41094. case DUK_TAG_POINTER: {
  41095. DUK_DDD(DUK_DDDPRINT("base object is a pointer, start lookup from pointer prototype"));
  41096. curr = thr->builtins[DUK_BIDX_POINTER_PROTOTYPE];
  41097. break;
  41098. }
  41099. case DUK_TAG_LIGHTFUNC: {
  41100. duk_int_t lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv_obj);
  41101. /* Must coerce key: if key is an object, it may coerce to e.g. 'length'. */
  41102. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  41103. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  41104. duk_int_t lf_len = DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags);
  41105. duk_pop(ctx);
  41106. duk_push_int(ctx, lf_len);
  41107. return 1;
  41108. } else if (key == DUK_HTHREAD_STRING_NAME(thr)) {
  41109. duk_pop(ctx);
  41110. duk_push_lightfunc_name(ctx, tv_obj);
  41111. return 1;
  41112. }
  41113. DUK_DDD(DUK_DDDPRINT("base object is a lightfunc, start lookup from function prototype"));
  41114. curr = thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE];
  41115. goto lookup; /* avoid double coercion */
  41116. }
  41117. #if defined(DUK_USE_FASTINT)
  41118. case DUK_TAG_FASTINT:
  41119. #endif
  41120. default: {
  41121. /* number */
  41122. DUK_DDD(DUK_DDDPRINT("base object is a number, start lookup from number prototype"));
  41123. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_obj));
  41124. curr = thr->builtins[DUK_BIDX_NUMBER_PROTOTYPE];
  41125. break;
  41126. }
  41127. }
  41128. /* key coercion (unless already coerced above) */
  41129. DUK_ASSERT(key == NULL);
  41130. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  41131. DUK_ASSERT(key != NULL);
  41132. /*
  41133. * Property lookup
  41134. */
  41135. lookup:
  41136. /* [key] (coerced) */
  41137. DUK_ASSERT(curr != NULL);
  41138. DUK_ASSERT(key != NULL);
  41139. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  41140. do {
  41141. if (!duk__get_own_property_desc_raw(thr, curr, key, arr_idx, &desc, DUK__DESC_FLAG_PUSH_VALUE)) {
  41142. goto next_in_chain;
  41143. }
  41144. if (desc.get != NULL) {
  41145. /* accessor with defined getter */
  41146. DUK_ASSERT((desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) != 0);
  41147. duk_pop(ctx); /* [key undefined] -> [key] */
  41148. duk_push_hobject(ctx, desc.get);
  41149. duk_push_tval(ctx, tv_obj); /* note: original, uncoerced base */
  41150. #ifdef DUK_USE_NONSTD_GETTER_KEY_ARGUMENT
  41151. duk_dup(ctx, -3);
  41152. duk_call_method(ctx, 1); /* [key getter this key] -> [key retval] */
  41153. #else
  41154. duk_call_method(ctx, 0); /* [key getter this] -> [key retval] */
  41155. #endif
  41156. } else {
  41157. /* [key value] or [key undefined] */
  41158. /* data property or accessor without getter */
  41159. DUK_ASSERT(((desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) == 0) ||
  41160. (desc.get == NULL));
  41161. /* if accessor without getter, return value is undefined */
  41162. DUK_ASSERT(((desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) == 0) ||
  41163. duk_is_undefined(ctx, -1));
  41164. /* Note: for an accessor without getter, falling through to
  41165. * check for "caller" exotic behavior is unnecessary as
  41166. * "undefined" will never activate the behavior. But it does
  41167. * no harm, so we'll do it anyway.
  41168. */
  41169. }
  41170. goto found; /* [key result] */
  41171. next_in_chain:
  41172. /* XXX: option to pretend property doesn't exist if sanity limit is
  41173. * hit might be useful.
  41174. */
  41175. if (sanity-- == 0) {
  41176. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  41177. }
  41178. curr = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, curr);
  41179. } while (curr);
  41180. /*
  41181. * Not found
  41182. */
  41183. duk_to_undefined(ctx, -1); /* [key] -> [undefined] (default value) */
  41184. DUK_DDD(DUK_DDDPRINT("-> %!T (not found)", (duk_tval *) duk_get_tval(ctx, -1)));
  41185. return 0;
  41186. /*
  41187. * Found; post-processing (Function and arguments objects)
  41188. */
  41189. found:
  41190. /* [key result] */
  41191. #if !defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY)
  41192. /* Special behavior for 'caller' property of (non-bound) function objects
  41193. * and non-strict Arguments objects: if 'caller' -value- (!) is a strict
  41194. * mode function, throw a TypeError (E5 Sections 15.3.5.4, 10.6).
  41195. * Quite interestingly, a non-strict function with no formal arguments
  41196. * will get an arguments object -without- special 'caller' behavior!
  41197. *
  41198. * The E5.1 spec is a bit ambiguous if this special behavior applies when
  41199. * a bound function is the base value (not the 'caller' value): Section
  41200. * 15.3.4.5 (describing bind()) states that [[Get]] for bound functions
  41201. * matches that of Section 15.3.5.4 ([[Get]] for Function instances).
  41202. * However, Section 13.3.5.4 has "NOTE: Function objects created using
  41203. * Function.prototype.bind use the default [[Get]] internal method."
  41204. * The current implementation assumes this means that bound functions
  41205. * should not have the special [[Get]] behavior.
  41206. *
  41207. * The E5.1 spec is also a bit unclear if the TypeError throwing is
  41208. * applied if the 'caller' value is a strict bound function. The
  41209. * current implementation will throw even for both strict non-bound
  41210. * and strict bound functions.
  41211. *
  41212. * See test-dev-strict-func-as-caller-prop-value.js for quite extensive
  41213. * tests.
  41214. *
  41215. * This exotic behavior is disabled when the non-standard 'caller' property
  41216. * is enabled, as it conflicts with the free use of 'caller'.
  41217. */
  41218. if (key == DUK_HTHREAD_STRING_CALLER(thr) &&
  41219. DUK_TVAL_IS_OBJECT(tv_obj)) {
  41220. duk_hobject *orig = DUK_TVAL_GET_OBJECT(tv_obj);
  41221. DUK_ASSERT(orig != NULL);
  41222. if (DUK_HOBJECT_IS_NONBOUND_FUNCTION(orig) ||
  41223. DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(orig)) {
  41224. duk_hobject *h;
  41225. /* XXX: The TypeError is currently not applied to bound
  41226. * functions because the 'strict' flag is not copied by
  41227. * bind(). This may or may not be correct, the specification
  41228. * only refers to the value being a "strict mode Function
  41229. * object" which is ambiguous.
  41230. */
  41231. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(orig));
  41232. h = duk_get_hobject(ctx, -1); /* NULL if not an object */
  41233. if (h &&
  41234. DUK_HOBJECT_IS_FUNCTION(h) &&
  41235. DUK_HOBJECT_HAS_STRICT(h)) {
  41236. /* XXX: sufficient to check 'strict', assert for 'is function' */
  41237. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_STRICT_CALLER_READ);
  41238. }
  41239. }
  41240. }
  41241. #endif /* !DUK_USE_NONSTD_FUNC_CALLER_PROPERTY */
  41242. duk_remove(ctx, -2); /* [key result] -> [result] */
  41243. DUK_DDD(DUK_DDDPRINT("-> %!T (found)", (duk_tval *) duk_get_tval(ctx, -1)));
  41244. return 1;
  41245. }
  41246. /*
  41247. * HASPROP: Ecmascript property existence check ("in" operator).
  41248. *
  41249. * Interestingly, the 'in' operator does not do any coercion of
  41250. * the target object.
  41251. */
  41252. DUK_INTERNAL duk_bool_t duk_hobject_hasprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key) {
  41253. duk_context *ctx = (duk_context *) thr;
  41254. duk_tval tv_key_copy;
  41255. duk_hobject *obj;
  41256. duk_hstring *key;
  41257. duk_uint32_t arr_idx;
  41258. duk_bool_t rc;
  41259. duk_propdesc desc;
  41260. DUK_DDD(DUK_DDDPRINT("hasprop: thr=%p, obj=%p, key=%p (obj -> %!T, key -> %!T)",
  41261. (void *) thr, (void *) tv_obj, (void *) tv_key,
  41262. (duk_tval *) tv_obj, (duk_tval *) tv_key));
  41263. DUK_ASSERT(thr != NULL);
  41264. DUK_ASSERT(thr->heap != NULL);
  41265. DUK_ASSERT(tv_obj != NULL);
  41266. DUK_ASSERT(tv_key != NULL);
  41267. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  41268. DUK_TVAL_SET_TVAL(&tv_key_copy, tv_key);
  41269. tv_key = &tv_key_copy;
  41270. /*
  41271. * The 'in' operator requires an object as its right hand side,
  41272. * throwing a TypeError unconditionally if this is not the case.
  41273. *
  41274. * However, lightfuncs need to behave like fully fledged objects
  41275. * here to be maximally transparent, so we need to handle them
  41276. * here.
  41277. */
  41278. /* XXX: Refactor key coercion so that it's only called once. It can't
  41279. * be trivially lifted here because the object must be type checked
  41280. * first.
  41281. */
  41282. if (DUK_TVAL_IS_OBJECT(tv_obj)) {
  41283. obj = DUK_TVAL_GET_OBJECT(tv_obj);
  41284. DUK_ASSERT(obj != NULL);
  41285. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  41286. } else if (DUK_TVAL_IS_LIGHTFUNC(tv_obj)) {
  41287. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  41288. if (duk__key_is_lightfunc_ownprop(thr, key)) {
  41289. /* FOUND */
  41290. rc = 1;
  41291. goto pop_and_return;
  41292. }
  41293. /* If not found, resume existence check from Function.prototype.
  41294. * We can just substitute the value in this case; nothing will
  41295. * need the original base value (as would be the case with e.g.
  41296. * setters/getters.
  41297. */
  41298. obj = thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE];
  41299. } else {
  41300. /* Note: unconditional throw */
  41301. DUK_DDD(DUK_DDDPRINT("base object is not an object -> reject"));
  41302. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_BASE);
  41303. }
  41304. /* XXX: fast path for arrays? */
  41305. DUK_ASSERT(key != NULL);
  41306. DUK_ASSERT(obj != NULL);
  41307. DUK_UNREF(arr_idx);
  41308. #if defined(DUK_USE_ES6_PROXY)
  41309. if (DUK_UNLIKELY(DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(obj))) {
  41310. duk_hobject *h_target;
  41311. duk_bool_t tmp_bool;
  41312. /* XXX: the key in 'key in obj' is string coerced before we're called
  41313. * (which is the required behavior in E5/E5.1/E6) so the key is a string
  41314. * here already.
  41315. */
  41316. if (duk__proxy_check_prop(thr, obj, DUK_STRIDX_HAS, tv_key, &h_target)) {
  41317. /* [ ... key trap handler ] */
  41318. DUK_DDD(DUK_DDDPRINT("-> proxy object 'has' for key %!T", (duk_tval *) tv_key));
  41319. duk_push_hobject(ctx, h_target); /* target */
  41320. duk_push_tval(ctx, tv_key); /* P */
  41321. duk_call_method(ctx, 2 /*nargs*/);
  41322. tmp_bool = duk_to_boolean(ctx, -1);
  41323. if (!tmp_bool) {
  41324. /* Target object must be checked for a conflicting
  41325. * non-configurable property.
  41326. */
  41327. if (duk__get_own_property_desc_raw(thr, h_target, key, arr_idx, &desc, 0 /*flags*/)) { /* don't push value */
  41328. DUK_DDD(DUK_DDDPRINT("proxy 'has': target has matching property %!O, check for "
  41329. "conflicting property; desc.flags=0x%08lx, "
  41330. "desc.get=%p, desc.set=%p",
  41331. (duk_heaphdr *) key, (unsigned long) desc.flags,
  41332. (void *) desc.get, (void *) desc.set));
  41333. /* XXX: Extensibility check for target uses IsExtensible(). If we
  41334. * implemented the isExtensible trap and didn't reject proxies as
  41335. * proxy targets, it should be respected here.
  41336. */
  41337. if (!((desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && /* property is configurable and */
  41338. DUK_HOBJECT_HAS_EXTENSIBLE(h_target))) { /* ... target is extensible */
  41339. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROXY_REJECTED);
  41340. }
  41341. }
  41342. }
  41343. duk_pop_2(ctx); /* [ key trap_result ] -> [] */
  41344. return tmp_bool;
  41345. }
  41346. obj = h_target; /* resume check from proxy target */
  41347. }
  41348. #endif /* DUK_USE_ES6_PROXY */
  41349. /* XXX: inline into a prototype walking loop? */
  41350. rc = duk__get_property_desc(thr, obj, key, &desc, 0 /*flags*/); /* don't push value */
  41351. /* fall through */
  41352. pop_and_return:
  41353. duk_pop(ctx); /* [ key ] -> [] */
  41354. return rc;
  41355. }
  41356. /*
  41357. * HASPROP variant used internally.
  41358. *
  41359. * This primitive must never throw an error, callers rely on this.
  41360. * In particular, don't throw an error for prototype loops; instead,
  41361. * pretend like the property doesn't exist if a prototype sanity limit
  41362. * is reached.
  41363. *
  41364. * Does not implement proxy behavior: if applied to a proxy object,
  41365. * returns key existence on the proxy object itself.
  41366. */
  41367. DUK_INTERNAL duk_bool_t duk_hobject_hasprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key) {
  41368. duk_propdesc dummy;
  41369. DUK_ASSERT(thr != NULL);
  41370. DUK_ASSERT(thr->heap != NULL);
  41371. DUK_ASSERT(obj != NULL);
  41372. DUK_ASSERT(key != NULL);
  41373. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  41374. return duk__get_property_desc(thr, obj, key, &dummy, DUK__DESC_FLAG_IGNORE_PROTOLOOP); /* don't push value */
  41375. }
  41376. /*
  41377. * Helper: handle Array object 'length' write which automatically
  41378. * deletes properties, see E5 Section 15.4.5.1, step 3. This is
  41379. * quite tricky to get right.
  41380. *
  41381. * Used by duk_hobject_putprop().
  41382. */
  41383. DUK_LOCAL duk_uint32_t duk__get_old_array_length(duk_hthread *thr, duk_hobject *obj, duk_propdesc *temp_desc) {
  41384. duk_bool_t rc;
  41385. duk_tval *tv;
  41386. duk_uint32_t res;
  41387. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  41388. /* This function is only called for objects with array exotic behavior.
  41389. * The [[DefineOwnProperty]] algorithm for arrays requires that
  41390. * 'length' can never have a value outside the unsigned 32-bit range,
  41391. * attempt to write such a value is a RangeError. Here we can thus
  41392. * assert for this. When Duktape internals go around the official
  41393. * property write interface (doesn't happen often) this assumption is
  41394. * easy to accidentally break, so such code must be written carefully.
  41395. * See test-bi-array-push-maxlen.js.
  41396. */
  41397. 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 */
  41398. DUK_UNREF(rc);
  41399. DUK_ASSERT(rc != 0); /* arrays MUST have a 'length' property */
  41400. DUK_ASSERT(temp_desc->e_idx >= 0);
  41401. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, temp_desc->e_idx);
  41402. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); /* array 'length' is always a number, as we coerce it */
  41403. DUK_ASSERT(DUK_TVAL_GET_NUMBER(tv) >= 0.0);
  41404. DUK_ASSERT(DUK_TVAL_GET_NUMBER(tv) <= (double) 0xffffffffUL);
  41405. DUK_ASSERT((duk_double_t) (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv) == DUK_TVAL_GET_NUMBER(tv));
  41406. #if defined(DUK_USE_FASTINT)
  41407. /* Downgrade checks are not made everywhere, so 'length' is not always
  41408. * a fastint (it is a number though). This can be removed once length
  41409. * is always guaranteed to be a fastint.
  41410. */
  41411. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv) || DUK_TVAL_IS_DOUBLE(tv));
  41412. if (DUK_TVAL_IS_FASTINT(tv)) {
  41413. res = (duk_uint32_t) DUK_TVAL_GET_FASTINT_U32(tv);
  41414. } else {
  41415. res = (duk_uint32_t) DUK_TVAL_GET_DOUBLE(tv);
  41416. }
  41417. #else
  41418. res = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv);
  41419. #endif /* DUK_USE_FASTINT */
  41420. return res;
  41421. }
  41422. DUK_LOCAL duk_uint32_t duk__to_new_array_length_checked(duk_hthread *thr) {
  41423. duk_context *ctx = (duk_context *) thr;
  41424. duk_uint32_t res;
  41425. duk_double_t d;
  41426. /* Input value should be on stack top and will be coerced and
  41427. * popped. Refuse to update an Array's 'length' to a value
  41428. * outside the 32-bit range. Negative zero is accepted as zero.
  41429. */
  41430. /* XXX: fastint */
  41431. d = duk_to_number(ctx, -1);
  41432. res = (duk_uint32_t) d;
  41433. if ((duk_double_t) res != d) {
  41434. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_INVALID_ARRAY_LENGTH);
  41435. }
  41436. duk_pop(ctx);
  41437. return res;
  41438. }
  41439. /* Delete elements required by a smaller length, taking into account
  41440. * potentially non-configurable elements. Returns non-zero if all
  41441. * elements could be deleted, and zero if all or some elements could
  41442. * not be deleted. Also writes final "target length" to 'out_result_len'.
  41443. * This is the length value that should go into the 'length' property
  41444. * (must be set by the caller). Never throws an error.
  41445. */
  41446. DUK_LOCAL
  41447. duk_bool_t duk__handle_put_array_length_smaller(duk_hthread *thr,
  41448. duk_hobject *obj,
  41449. duk_uint32_t old_len,
  41450. duk_uint32_t new_len,
  41451. duk_bool_t force_flag,
  41452. duk_uint32_t *out_result_len) {
  41453. duk_uint32_t target_len;
  41454. duk_uint_fast32_t i;
  41455. duk_uint32_t arr_idx;
  41456. duk_hstring *key;
  41457. duk_tval *tv;
  41458. duk_tval tv_tmp;
  41459. duk_bool_t rc;
  41460. DUK_DDD(DUK_DDDPRINT("new array length smaller than old (%ld -> %ld), "
  41461. "probably need to remove elements",
  41462. (long) old_len, (long) new_len));
  41463. /*
  41464. * New length is smaller than old length, need to delete properties above
  41465. * the new length.
  41466. *
  41467. * If array part exists, this is straightforward: array entries cannot
  41468. * be non-configurable so this is guaranteed to work.
  41469. *
  41470. * If array part does not exist, array-indexed values are scattered
  41471. * in the entry part, and some may not be configurable (preventing length
  41472. * from becoming lower than their index + 1). To handle the algorithm
  41473. * in E5 Section 15.4.5.1, step l correctly, we scan the entire property
  41474. * set twice.
  41475. */
  41476. DUK_ASSERT(thr != NULL);
  41477. DUK_ASSERT(obj != NULL);
  41478. DUK_ASSERT(new_len < old_len);
  41479. DUK_ASSERT(out_result_len != NULL);
  41480. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  41481. if (DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  41482. /*
  41483. * All defined array-indexed properties are in the array part
  41484. * (we assume the array part is comprehensive), and all array
  41485. * entries are writable, configurable, and enumerable. Thus,
  41486. * nothing can prevent array entries from being deleted.
  41487. */
  41488. DUK_DDD(DUK_DDDPRINT("have array part, easy case"));
  41489. if (old_len < DUK_HOBJECT_GET_ASIZE(obj)) {
  41490. /* XXX: assertion that entries >= old_len are already unused */
  41491. i = old_len;
  41492. } else {
  41493. i = DUK_HOBJECT_GET_ASIZE(obj);
  41494. }
  41495. DUK_ASSERT(i <= DUK_HOBJECT_GET_ASIZE(obj));
  41496. while (i > new_len) {
  41497. i--;
  41498. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, i);
  41499. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  41500. DUK_TVAL_SET_UNDEFINED_UNUSED(tv);
  41501. DUK_TVAL_DECREF(thr, &tv_tmp);
  41502. }
  41503. *out_result_len = new_len;
  41504. return 1;
  41505. } else {
  41506. /*
  41507. * Entries part is a bit more complex
  41508. */
  41509. /* Stage 1: find highest preventing non-configurable entry (if any).
  41510. * When forcing, ignore non-configurability.
  41511. */
  41512. DUK_DDD(DUK_DDDPRINT("no array part, slow case"));
  41513. DUK_DDD(DUK_DDDPRINT("array length write, no array part, stage 1: find target_len "
  41514. "(highest preventing non-configurable entry (if any))"));
  41515. target_len = new_len;
  41516. if (force_flag) {
  41517. DUK_DDD(DUK_DDDPRINT("array length write, no array part; force flag -> skip stage 1"));
  41518. goto skip_stage1;
  41519. }
  41520. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  41521. key = DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i);
  41522. if (!key) {
  41523. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: null key", (long) i));
  41524. continue;
  41525. }
  41526. if (!DUK_HSTRING_HAS_ARRIDX(key)) {
  41527. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: key not an array index", (long) i));
  41528. continue;
  41529. }
  41530. DUK_ASSERT(DUK_HSTRING_HAS_ARRIDX(key)); /* XXX: macro checks for array index flag, which is unnecessary here */
  41531. arr_idx = DUK_HSTRING_GET_ARRIDX_SLOW(key);
  41532. DUK_ASSERT(arr_idx != DUK__NO_ARRAY_INDEX);
  41533. DUK_ASSERT(arr_idx < old_len); /* consistency requires this */
  41534. if (arr_idx < new_len) {
  41535. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: key is array index %ld, below new_len",
  41536. (long) i, (long) arr_idx));
  41537. continue;
  41538. }
  41539. if (DUK_HOBJECT_E_SLOT_IS_CONFIGURABLE(thr->heap, obj, i)) {
  41540. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: key is a relevant array index %ld, but configurable",
  41541. (long) i, (long) arr_idx));
  41542. continue;
  41543. }
  41544. /* relevant array index is non-configurable, blocks write */
  41545. if (arr_idx >= target_len) {
  41546. DUK_DDD(DUK_DDDPRINT("entry at index %ld has arr_idx %ld, is not configurable, "
  41547. "update target_len %ld -> %ld",
  41548. (long) i, (long) arr_idx, (long) target_len,
  41549. (long) (arr_idx + 1)));
  41550. target_len = arr_idx + 1;
  41551. }
  41552. }
  41553. skip_stage1:
  41554. /* stage 2: delete configurable entries above target length */
  41555. DUK_DDD(DUK_DDDPRINT("old_len=%ld, new_len=%ld, target_len=%ld",
  41556. (long) old_len, (long) new_len, (long) target_len));
  41557. DUK_DDD(DUK_DDDPRINT("array length write, no array part, stage 2: remove "
  41558. "entries >= target_len"));
  41559. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  41560. key = DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i);
  41561. if (!key) {
  41562. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: null key", (long) i));
  41563. continue;
  41564. }
  41565. if (!DUK_HSTRING_HAS_ARRIDX(key)) {
  41566. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: key not an array index", (long) i));
  41567. continue;
  41568. }
  41569. DUK_ASSERT(DUK_HSTRING_HAS_ARRIDX(key)); /* XXX: macro checks for array index flag, which is unnecessary here */
  41570. arr_idx = DUK_HSTRING_GET_ARRIDX_SLOW(key);
  41571. DUK_ASSERT(arr_idx != DUK__NO_ARRAY_INDEX);
  41572. DUK_ASSERT(arr_idx < old_len); /* consistency requires this */
  41573. if (arr_idx < target_len) {
  41574. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: key is array index %ld, below target_len",
  41575. (long) i, (long) arr_idx));
  41576. continue;
  41577. }
  41578. DUK_ASSERT(force_flag || DUK_HOBJECT_E_SLOT_IS_CONFIGURABLE(thr->heap, obj, i)); /* stage 1 guarantees */
  41579. DUK_DDD(DUK_DDDPRINT("delete entry index %ld: key is array index %ld",
  41580. (long) i, (long) arr_idx));
  41581. /*
  41582. * Slow delete, but we don't care as we're already in a very slow path.
  41583. * The delete always succeeds: key has no exotic behavior, property
  41584. * is configurable, and no resize occurs.
  41585. */
  41586. rc = duk_hobject_delprop_raw(thr, obj, key, force_flag ? DUK_DELPROP_FLAG_FORCE : 0);
  41587. DUK_UNREF(rc);
  41588. DUK_ASSERT(rc != 0);
  41589. }
  41590. /* stage 3: update length (done by caller), decide return code */
  41591. DUK_DDD(DUK_DDDPRINT("array length write, no array part, stage 3: update length (done by caller)"));
  41592. *out_result_len = target_len;
  41593. if (target_len == new_len) {
  41594. DUK_DDD(DUK_DDDPRINT("target_len matches new_len, return success"));
  41595. return 1;
  41596. }
  41597. DUK_DDD(DUK_DDDPRINT("target_len does not match new_len (some entry prevented "
  41598. "full length adjustment), return error"));
  41599. return 0;
  41600. }
  41601. DUK_UNREACHABLE();
  41602. }
  41603. /* XXX: is valstack top best place for argument? */
  41604. DUK_LOCAL duk_bool_t duk__handle_put_array_length(duk_hthread *thr, duk_hobject *obj) {
  41605. duk_context *ctx = (duk_context *) thr;
  41606. duk_propdesc desc;
  41607. duk_uint32_t old_len;
  41608. duk_uint32_t new_len;
  41609. duk_uint32_t result_len;
  41610. duk_tval *tv;
  41611. duk_bool_t rc;
  41612. DUK_DDD(DUK_DDDPRINT("handling a put operation to array 'length' exotic property, "
  41613. "new val: %!T",
  41614. (duk_tval *) duk_get_tval(ctx, -1)));
  41615. DUK_ASSERT(thr != NULL);
  41616. DUK_ASSERT(ctx != NULL);
  41617. DUK_ASSERT(obj != NULL);
  41618. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  41619. DUK_ASSERT(duk_is_valid_index(ctx, -1));
  41620. /*
  41621. * Get old and new length
  41622. */
  41623. old_len = duk__get_old_array_length(thr, obj, &desc);
  41624. duk_dup(ctx, -1); /* [in_val in_val] */
  41625. new_len = duk__to_new_array_length_checked(thr); /* -> [in_val] */
  41626. DUK_DDD(DUK_DDDPRINT("old_len=%ld, new_len=%ld", (long) old_len, (long) new_len));
  41627. /*
  41628. * Writability check
  41629. */
  41630. if (!(desc.flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  41631. DUK_DDD(DUK_DDDPRINT("length is not writable, fail"));
  41632. return 0;
  41633. }
  41634. /*
  41635. * New length not lower than old length => no changes needed
  41636. * (not even array allocation).
  41637. */
  41638. if (new_len >= old_len) {
  41639. DUK_DDD(DUK_DDDPRINT("new length is higher than old length, just update length, no deletions"));
  41640. DUK_ASSERT(desc.e_idx >= 0);
  41641. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, desc.e_idx));
  41642. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, desc.e_idx);
  41643. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  41644. /* no decref needed for a number */
  41645. #if defined(DUK_USE_FASTINT)
  41646. DUK_TVAL_SET_FASTINT_U32(tv, new_len);
  41647. #else
  41648. DUK_TVAL_SET_NUMBER(tv, (duk_double_t) new_len);
  41649. #endif
  41650. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  41651. return 1;
  41652. }
  41653. DUK_DDD(DUK_DDDPRINT("new length is lower than old length, probably must delete entries"));
  41654. /*
  41655. * New length lower than old length => delete elements, then
  41656. * update length.
  41657. *
  41658. * Note: even though a bunch of elements have been deleted, the 'desc' is
  41659. * still valid as properties haven't been resized (and entries compacted).
  41660. */
  41661. rc = duk__handle_put_array_length_smaller(thr, obj, old_len, new_len, 0 /*force_flag*/, &result_len);
  41662. DUK_ASSERT(result_len >= new_len && result_len <= old_len);
  41663. DUK_ASSERT(desc.e_idx >= 0);
  41664. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, desc.e_idx));
  41665. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, desc.e_idx);
  41666. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  41667. /* no decref needed for a number */
  41668. #if defined(DUK_USE_FASTINT)
  41669. DUK_TVAL_SET_FASTINT_U32(tv, result_len);
  41670. #else
  41671. DUK_TVAL_SET_NUMBER(tv, (duk_double_t) result_len);
  41672. #endif
  41673. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  41674. /* XXX: shrink array allocation or entries compaction here? */
  41675. return rc;
  41676. }
  41677. /*
  41678. * PUTPROP: Ecmascript property write.
  41679. *
  41680. * Unlike Ecmascript primitive which returns nothing, returns 1 to indicate
  41681. * success and 0 to indicate failure (assuming throw is not set).
  41682. *
  41683. * This is an extremely tricky function. Some examples:
  41684. *
  41685. * * Currently a decref may trigger a GC, which may compact an object's
  41686. * property allocation. Consequently, any entry indices (e_idx) will
  41687. * be potentially invalidated by a decref.
  41688. *
  41689. * * Exotic behaviors (strings, arrays, arguments object) require,
  41690. * among other things:
  41691. *
  41692. * - Preprocessing before and postprocessing after an actual property
  41693. * write. For example, array index write requires pre-checking the
  41694. * array 'length' property for access control, and may require an
  41695. * array 'length' update after the actual write has succeeded (but
  41696. * not if it fails).
  41697. *
  41698. * - Deletion of multiple entries, as a result of array 'length' write.
  41699. *
  41700. * * Input values are taken as pointers which may point to the valstack.
  41701. * If valstack is resized because of the put (this may happen at least
  41702. * when the array part is abandoned), the pointers can be invalidated.
  41703. * (We currently make a copy of all of the input values to avoid issues.)
  41704. */
  41705. 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) {
  41706. duk_context *ctx = (duk_context *) thr;
  41707. duk_tval tv_obj_copy;
  41708. duk_tval tv_key_copy;
  41709. duk_tval tv_val_copy;
  41710. duk_hobject *orig = NULL; /* NULL if tv_obj is primitive */
  41711. duk_hobject *curr;
  41712. duk_hstring *key = NULL;
  41713. duk_propdesc desc;
  41714. duk_tval *tv;
  41715. duk_uint32_t arr_idx;
  41716. duk_bool_t rc;
  41717. duk_int_t e_idx;
  41718. duk_uint_t sanity;
  41719. duk_uint32_t new_array_length = 0; /* 0 = no update */
  41720. DUK_DDD(DUK_DDDPRINT("putprop: thr=%p, obj=%p, key=%p, val=%p, throw=%ld "
  41721. "(obj -> %!T, key -> %!T, val -> %!T)",
  41722. (void *) thr, (void *) tv_obj, (void *) tv_key, (void *) tv_val,
  41723. (long) throw_flag, (duk_tval *) tv_obj, (duk_tval *) tv_key, (duk_tval *) tv_val));
  41724. DUK_ASSERT(thr != NULL);
  41725. DUK_ASSERT(thr->heap != NULL);
  41726. DUK_ASSERT(ctx != NULL);
  41727. DUK_ASSERT(tv_obj != NULL);
  41728. DUK_ASSERT(tv_key != NULL);
  41729. DUK_ASSERT(tv_val != NULL);
  41730. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  41731. /*
  41732. * Make a copy of tv_obj, tv_key, and tv_val to avoid any issues of
  41733. * them being invalidated by a valstack resize.
  41734. *
  41735. * XXX: this is an overkill for some paths, so optimize this later
  41736. * (or maybe switch to a stack arguments model entirely).
  41737. */
  41738. DUK_TVAL_SET_TVAL(&tv_obj_copy, tv_obj);
  41739. DUK_TVAL_SET_TVAL(&tv_key_copy, tv_key);
  41740. DUK_TVAL_SET_TVAL(&tv_val_copy, tv_val);
  41741. tv_obj = &tv_obj_copy;
  41742. tv_key = &tv_key_copy;
  41743. tv_val = &tv_val_copy;
  41744. /*
  41745. * Coercion and fast path processing.
  41746. */
  41747. switch (DUK_TVAL_GET_TAG(tv_obj)) {
  41748. case DUK_TAG_UNDEFINED:
  41749. case DUK_TAG_NULL: {
  41750. /* Note: unconditional throw */
  41751. DUK_DDD(DUK_DDDPRINT("base object is undefined or null -> reject (object=%!iT)",
  41752. (duk_tval *) tv_obj));
  41753. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_BASE);
  41754. return 0;
  41755. }
  41756. case DUK_TAG_BOOLEAN: {
  41757. DUK_DDD(DUK_DDDPRINT("base object is a boolean, start lookup from boolean prototype"));
  41758. curr = thr->builtins[DUK_BIDX_BOOLEAN_PROTOTYPE];
  41759. break;
  41760. }
  41761. case DUK_TAG_STRING: {
  41762. duk_hstring *h = DUK_TVAL_GET_STRING(tv_obj);
  41763. /*
  41764. * Note: currently no fast path for array index writes.
  41765. * They won't be possible anyway as strings are immutable.
  41766. */
  41767. DUK_ASSERT(key == NULL);
  41768. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  41769. DUK_ASSERT(key != NULL);
  41770. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  41771. goto fail_not_writable;
  41772. }
  41773. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  41774. arr_idx < DUK_HSTRING_GET_CHARLEN(h)) {
  41775. goto fail_not_writable;
  41776. }
  41777. DUK_DDD(DUK_DDDPRINT("base object is a string, start lookup from string prototype"));
  41778. curr = thr->builtins[DUK_BIDX_STRING_PROTOTYPE];
  41779. goto lookup; /* avoid double coercion */
  41780. }
  41781. case DUK_TAG_OBJECT: {
  41782. orig = DUK_TVAL_GET_OBJECT(tv_obj);
  41783. DUK_ASSERT(orig != NULL);
  41784. /* The fast path for array property put is not fully compliant:
  41785. * If one places conflicting number-indexed properties into
  41786. * Array.prototype (for example, a non-writable Array.prototype[7])
  41787. * the fast path will incorrectly ignore them.
  41788. *
  41789. * This fast path could be made compliant by falling through
  41790. * to the slow path if the previous value was UNDEFINED_UNUSED.
  41791. * This would also remove the need to check for extensibility.
  41792. * Right now a non-extensible array is slower than an extensible
  41793. * one as far as writes are concerned.
  41794. *
  41795. * The fast path behavior is documented in more detail here:
  41796. * ecmascript-testcases/test-misc-array-fast-write.js
  41797. */
  41798. if (duk__putprop_shallow_fastpath_array_tval(thr, orig, tv_key, tv_val, &desc) != 0) {
  41799. DUK_DDD(DUK_DDDPRINT("array fast path success for index %ld", (long) arr_idx));
  41800. return 1;
  41801. }
  41802. if (duk__putprop_fastpath_bufobj_tval(thr, orig, tv_key, tv_val) != 0) {
  41803. DUK_DDD(DUK_DDDPRINT("base is bufobj, key is a number, bufferobject fast path"));
  41804. return 1;
  41805. }
  41806. #if defined(DUK_USE_ES6_PROXY)
  41807. if (DUK_UNLIKELY(DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(orig))) {
  41808. duk_hobject *h_target;
  41809. duk_bool_t tmp_bool;
  41810. if (duk__proxy_check_prop(thr, orig, DUK_STRIDX_SET, tv_key, &h_target)) {
  41811. /* -> [ ... trap handler ] */
  41812. DUK_DDD(DUK_DDDPRINT("-> proxy object 'set' for key %!T", (duk_tval *) tv_key));
  41813. duk_push_hobject(ctx, h_target); /* target */
  41814. duk_push_tval(ctx, tv_key); /* P */
  41815. duk_push_tval(ctx, tv_val); /* V */
  41816. duk_push_tval(ctx, tv_obj); /* Receiver: Proxy object */
  41817. duk_call_method(ctx, 4 /*nargs*/);
  41818. tmp_bool = duk_to_boolean(ctx, -1);
  41819. duk_pop(ctx);
  41820. if (!tmp_bool) {
  41821. goto fail_proxy_rejected;
  41822. }
  41823. /* Target object must be checked for a conflicting
  41824. * non-configurable property.
  41825. */
  41826. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  41827. DUK_ASSERT(key != NULL);
  41828. if (duk__get_own_property_desc_raw(thr, h_target, key, arr_idx, &desc, DUK__DESC_FLAG_PUSH_VALUE)) {
  41829. duk_tval *tv_targ = duk_require_tval(ctx, -1);
  41830. duk_bool_t datadesc_reject;
  41831. duk_bool_t accdesc_reject;
  41832. DUK_DDD(DUK_DDDPRINT("proxy 'set': target has matching property %!O, check for "
  41833. "conflicting property; tv_val=%!T, tv_targ=%!T, desc.flags=0x%08lx, "
  41834. "desc.get=%p, desc.set=%p",
  41835. (duk_heaphdr *) key, (duk_tval *) tv_val, (duk_tval *) tv_targ,
  41836. (unsigned long) desc.flags,
  41837. (void *) desc.get, (void *) desc.set));
  41838. datadesc_reject = !(desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) &&
  41839. !(desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) &&
  41840. !(desc.flags & DUK_PROPDESC_FLAG_WRITABLE) &&
  41841. !duk_js_samevalue(tv_val, tv_targ);
  41842. accdesc_reject = (desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) &&
  41843. !(desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) &&
  41844. (desc.set == NULL);
  41845. if (datadesc_reject || accdesc_reject) {
  41846. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROXY_REJECTED);
  41847. }
  41848. duk_pop_2(ctx);
  41849. } else {
  41850. duk_pop(ctx);
  41851. }
  41852. return 1; /* success */
  41853. }
  41854. orig = h_target; /* resume write to target */
  41855. DUK_TVAL_SET_OBJECT(tv_obj, orig);
  41856. }
  41857. #endif /* DUK_USE_ES6_PROXY */
  41858. curr = orig;
  41859. break;
  41860. }
  41861. case DUK_TAG_BUFFER: {
  41862. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv_obj);
  41863. duk_int_t pop_count = 0;
  41864. /*
  41865. * Because buffer values may be looped over and read/written
  41866. * from, an array index fast path is important.
  41867. */
  41868. #if defined(DUK_USE_FASTINT)
  41869. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  41870. arr_idx = duk__tval_fastint_to_arr_idx(tv_key);
  41871. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a fast-path fastint; arr_idx %ld", (long) arr_idx));
  41872. pop_count = 0;
  41873. } else
  41874. #endif
  41875. if (DUK_TVAL_IS_NUMBER(tv_key)) {
  41876. arr_idx = duk__tval_number_to_arr_idx(tv_key);
  41877. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a fast-path number; arr_idx %ld", (long) arr_idx));
  41878. pop_count = 0;
  41879. } else {
  41880. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  41881. DUK_ASSERT(key != NULL);
  41882. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a non-fast-path number; after "
  41883. "coercion key is %!T, arr_idx %ld",
  41884. (duk_tval *) duk_get_tval(ctx, -1), (long) arr_idx));
  41885. pop_count = 1;
  41886. }
  41887. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  41888. arr_idx < DUK_HBUFFER_GET_SIZE(h)) {
  41889. duk_uint8_t *data;
  41890. DUK_DDD(DUK_DDDPRINT("writing to buffer data at index %ld", (long) arr_idx));
  41891. data = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h);
  41892. /* XXX: duk_to_int() ensures we'll get 8 lowest bits as
  41893. * as input is within duk_int_t range (capped outside it).
  41894. */
  41895. #if defined(DUK_USE_FASTINT)
  41896. /* Buffer writes are often integers. */
  41897. if (DUK_TVAL_IS_FASTINT(tv_val)) {
  41898. data[arr_idx] = (duk_uint8_t) DUK_TVAL_GET_FASTINT_U32(tv_val);
  41899. }
  41900. else
  41901. #endif
  41902. {
  41903. duk_push_tval(ctx, tv_val);
  41904. data[arr_idx] = (duk_uint8_t) duk_to_uint32(ctx, -1);
  41905. pop_count++;
  41906. }
  41907. duk_pop_n(ctx, pop_count);
  41908. DUK_DDD(DUK_DDDPRINT("result: success (buffer data write)"));
  41909. return 1;
  41910. }
  41911. if (pop_count == 0) {
  41912. /* This is a pretty awkward control flow, but we need to recheck the
  41913. * key coercion here.
  41914. */
  41915. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  41916. DUK_ASSERT(key != NULL);
  41917. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a non-fast-path number; after "
  41918. "coercion key is %!T, arr_idx %ld",
  41919. (duk_tval *) duk_get_tval(ctx, -1), (long) arr_idx));
  41920. }
  41921. if (key == DUK_HTHREAD_STRING_LENGTH(thr) ||
  41922. key == DUK_HTHREAD_STRING_BYTE_LENGTH(thr) ||
  41923. key == DUK_HTHREAD_STRING_BYTE_OFFSET(thr) ||
  41924. key == DUK_HTHREAD_STRING_BYTES_PER_ELEMENT(thr)) {
  41925. goto fail_not_writable;
  41926. }
  41927. DUK_DDD(DUK_DDDPRINT("base object is a buffer, start lookup from buffer prototype"));
  41928. curr = thr->builtins[DUK_BIDX_BUFFER_PROTOTYPE];
  41929. goto lookup; /* avoid double coercion */
  41930. }
  41931. case DUK_TAG_POINTER: {
  41932. DUK_DDD(DUK_DDDPRINT("base object is a pointer, start lookup from pointer prototype"));
  41933. curr = thr->builtins[DUK_BIDX_POINTER_PROTOTYPE];
  41934. break;
  41935. }
  41936. case DUK_TAG_LIGHTFUNC: {
  41937. /* All lightfunc own properties are non-writable and the lightfunc
  41938. * is considered non-extensible. However, the write may be captured
  41939. * by an inherited setter which means we can't stop the lookup here.
  41940. */
  41941. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  41942. if (duk__key_is_lightfunc_ownprop(thr, key)) {
  41943. goto fail_not_writable;
  41944. }
  41945. DUK_DDD(DUK_DDDPRINT("base object is a lightfunc, start lookup from function prototype"));
  41946. curr = thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE];
  41947. goto lookup; /* avoid double coercion */
  41948. }
  41949. #if defined(DUK_USE_FASTINT)
  41950. case DUK_TAG_FASTINT:
  41951. #endif
  41952. default: {
  41953. /* number */
  41954. DUK_DDD(DUK_DDDPRINT("base object is a number, start lookup from number prototype"));
  41955. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_obj));
  41956. curr = thr->builtins[DUK_BIDX_NUMBER_PROTOTYPE];
  41957. break;
  41958. }
  41959. }
  41960. DUK_ASSERT(key == NULL);
  41961. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  41962. DUK_ASSERT(key != NULL);
  41963. lookup:
  41964. /*
  41965. * Check whether the property already exists in the prototype chain.
  41966. * Note that the actual write goes into the original base object
  41967. * (except if an accessor property captures the write).
  41968. */
  41969. /* [key] */
  41970. DUK_ASSERT(curr != NULL);
  41971. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  41972. do {
  41973. if (!duk__get_own_property_desc_raw(thr, curr, key, arr_idx, &desc, 0 /*flags*/)) { /* don't push value */
  41974. goto next_in_chain;
  41975. }
  41976. if (desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  41977. /*
  41978. * Found existing accessor property (own or inherited).
  41979. * Call setter with 'this' set to orig, and value as the only argument.
  41980. *
  41981. * Note: no exotic arguments object behavior, because [[Put]] never
  41982. * calls [[DefineOwnProperty]] (E5 Section 8.12.5, step 5.b).
  41983. */
  41984. duk_hobject *setter;
  41985. DUK_DD(DUK_DDPRINT("put to an own or inherited accessor, calling setter"));
  41986. setter = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, curr, desc.e_idx);
  41987. if (!setter) {
  41988. goto fail_no_setter;
  41989. }
  41990. duk_push_hobject(ctx, setter);
  41991. duk_push_tval(ctx, tv_obj); /* note: original, uncoerced base */
  41992. duk_push_tval(ctx, tv_val); /* [key setter this val] */
  41993. #ifdef DUK_USE_NONSTD_SETTER_KEY_ARGUMENT
  41994. duk_dup(ctx, -4);
  41995. duk_call_method(ctx, 2); /* [key setter this val key] -> [key retval] */
  41996. #else
  41997. duk_call_method(ctx, 1); /* [key setter this val] -> [key retval] */
  41998. #endif
  41999. duk_pop(ctx); /* ignore retval -> [key] */
  42000. goto success_no_arguments_exotic;
  42001. }
  42002. if (orig == NULL) {
  42003. /*
  42004. * Found existing own or inherited plain property, but original
  42005. * base is a primitive value.
  42006. */
  42007. DUK_DD(DUK_DDPRINT("attempt to create a new property in a primitive base object"));
  42008. goto fail_base_primitive;
  42009. }
  42010. if (curr != orig) {
  42011. /*
  42012. * Found existing inherited plain property.
  42013. * Do an access control check, and if OK, write
  42014. * new property to 'orig'.
  42015. */
  42016. if (!DUK_HOBJECT_HAS_EXTENSIBLE(orig)) {
  42017. DUK_DD(DUK_DDPRINT("found existing inherited plain property, but original object is not extensible"));
  42018. goto fail_not_extensible;
  42019. }
  42020. if (!(desc.flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  42021. DUK_DD(DUK_DDPRINT("found existing inherited plain property, original object is extensible, but inherited property is not writable"));
  42022. goto fail_not_writable;
  42023. }
  42024. DUK_DD(DUK_DDPRINT("put to new property, object extensible, inherited property found and is writable"));
  42025. goto create_new;
  42026. } else {
  42027. /*
  42028. * Found existing own (non-inherited) plain property.
  42029. * Do an access control check and update in place.
  42030. */
  42031. if (!(desc.flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  42032. DUK_DD(DUK_DDPRINT("found existing own (non-inherited) plain property, but property is not writable"));
  42033. goto fail_not_writable;
  42034. }
  42035. if (desc.flags & DUK_PROPDESC_FLAG_VIRTUAL) {
  42036. DUK_DD(DUK_DDPRINT("found existing own (non-inherited) virtual property, property is writable"));
  42037. if (DUK_HOBJECT_IS_BUFFEROBJECT(curr)) {
  42038. duk_hbufferobject *h_bufobj;
  42039. duk_uint_t byte_off;
  42040. duk_small_uint_t elem_size;
  42041. h_bufobj = (duk_hbufferobject *) curr;
  42042. DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj);
  42043. DUK_DD(DUK_DDPRINT("writable virtual property is in buffer object"));
  42044. /* Careful with wrapping: arr_idx upshift may easily wrap, whereas
  42045. * length downshift won't.
  42046. */
  42047. if (arr_idx < (h_bufobj->length >> h_bufobj->shift)) {
  42048. duk_uint8_t *data;
  42049. DUK_DDD(DUK_DDDPRINT("writing to buffer data at index %ld", (long) arr_idx));
  42050. DUK_ASSERT(arr_idx != DUK__NO_ARRAY_INDEX); /* index/length check guarantees */
  42051. byte_off = arr_idx << h_bufobj->shift; /* no wrap assuming h_bufobj->length is valid */
  42052. elem_size = 1 << h_bufobj->shift;
  42053. /* Coerce to number before validating pointers etc so that the
  42054. * number coercions in duk_hbufferobject_validated_write() are
  42055. * guaranteed to be side effect free and not invalidate the
  42056. * pointer checks we do here.
  42057. */
  42058. duk_push_tval(ctx, tv_val);
  42059. duk_to_number(ctx, -1);
  42060. if (h_bufobj->buf != NULL && DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_bufobj, byte_off + elem_size)) {
  42061. data = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf) + h_bufobj->offset + byte_off;
  42062. duk_hbufferobject_validated_write(ctx, h_bufobj, data, elem_size);
  42063. } else {
  42064. DUK_D(DUK_DPRINT("bufferobject access out of underlying buffer, ignoring (write skipped)"));
  42065. }
  42066. duk_pop(ctx);
  42067. goto success_no_arguments_exotic;
  42068. }
  42069. }
  42070. goto fail_internal; /* should not happen */
  42071. }
  42072. DUK_DD(DUK_DDPRINT("put to existing own plain property, property is writable"));
  42073. goto update_old;
  42074. }
  42075. DUK_UNREACHABLE();
  42076. next_in_chain:
  42077. /* XXX: option to pretend property doesn't exist if sanity limit is
  42078. * hit might be useful.
  42079. */
  42080. if (sanity-- == 0) {
  42081. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  42082. }
  42083. curr = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, curr);
  42084. } while (curr);
  42085. /*
  42086. * Property not found in prototype chain.
  42087. */
  42088. DUK_DDD(DUK_DDDPRINT("property not found in prototype chain"));
  42089. if (orig == NULL) {
  42090. DUK_DD(DUK_DDPRINT("attempt to create a new property in a primitive base object"));
  42091. goto fail_base_primitive;
  42092. }
  42093. if (!DUK_HOBJECT_HAS_EXTENSIBLE(orig)) {
  42094. DUK_DD(DUK_DDPRINT("put to a new property (not found in prototype chain), but original object not extensible"));
  42095. goto fail_not_extensible;
  42096. }
  42097. goto create_new;
  42098. update_old:
  42099. /*
  42100. * Update an existing property of the base object.
  42101. */
  42102. /* [key] */
  42103. DUK_DDD(DUK_DDDPRINT("update an existing property of the original object"));
  42104. DUK_ASSERT(orig != NULL);
  42105. /* Although there are writable virtual properties (e.g. plain buffer
  42106. * and buffer object number indices), they are handled before we come
  42107. * here.
  42108. */
  42109. DUK_ASSERT((desc.flags & DUK_PROPDESC_FLAG_VIRTUAL) == 0);
  42110. DUK_ASSERT(desc.a_idx >= 0 || desc.e_idx >= 0);
  42111. if (DUK_HOBJECT_HAS_EXOTIC_ARRAY(orig) &&
  42112. key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  42113. /*
  42114. * Write to 'length' of an array is a very complex case
  42115. * handled in a helper which updates both the array elements
  42116. * and writes the new 'length'. The write may result in an
  42117. * unconditional RangeError or a partial write (indicated
  42118. * by a return code).
  42119. *
  42120. * Note: the helper has an unnecessary writability check
  42121. * for 'length', we already know it is writable.
  42122. */
  42123. DUK_DDD(DUK_DDDPRINT("writing existing 'length' property to array exotic, invoke complex helper"));
  42124. /* XXX: the helper currently assumes stack top contains new
  42125. * 'length' value and the whole calling convention is not very
  42126. * compatible with what we need.
  42127. */
  42128. duk_push_tval(ctx, tv_val); /* [key val] */
  42129. rc = duk__handle_put_array_length(thr, orig);
  42130. duk_pop(ctx); /* [key val] -> [key] */
  42131. if (!rc) {
  42132. goto fail_array_length_partial;
  42133. }
  42134. /* key is 'length', cannot match argument exotic behavior */
  42135. goto success_no_arguments_exotic;
  42136. }
  42137. if (desc.e_idx >= 0) {
  42138. duk_tval tv_tmp;
  42139. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, orig, desc.e_idx);
  42140. DUK_DDD(DUK_DDDPRINT("previous entry value: %!iT", (duk_tval *) tv));
  42141. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  42142. DUK_TVAL_SET_TVAL(tv, tv_val);
  42143. DUK_TVAL_INCREF(thr, tv);
  42144. DUK_TVAL_DECREF(thr, &tv_tmp); /* note: may trigger gc and props compaction, must be last */
  42145. /* don't touch property attributes or hash part */
  42146. DUK_DD(DUK_DDPRINT("put to an existing entry at index %ld -> new value %!iT",
  42147. (long) desc.e_idx, (duk_tval *) tv));
  42148. } else {
  42149. /* Note: array entries are always writable, so the writability check
  42150. * above is pointless for them. The check could be avoided with some
  42151. * refactoring but is probably not worth it.
  42152. */
  42153. duk_tval tv_tmp;
  42154. DUK_ASSERT(desc.a_idx >= 0);
  42155. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, orig, desc.a_idx);
  42156. DUK_DDD(DUK_DDDPRINT("previous array value: %!iT", (duk_tval *) tv));
  42157. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  42158. DUK_TVAL_SET_TVAL(tv, tv_val);
  42159. DUK_TVAL_INCREF(thr, tv);
  42160. DUK_TVAL_DECREF(thr, &tv_tmp); /* note: may trigger gc and props compaction, must be last */
  42161. DUK_DD(DUK_DDPRINT("put to an existing array entry at index %ld -> new value %!iT",
  42162. (long) desc.a_idx, (duk_tval *) tv));
  42163. }
  42164. /* Regardless of whether property is found in entry or array part,
  42165. * it may have arguments exotic behavior (array indices may reside
  42166. * in entry part for abandoned / non-existent array parts).
  42167. */
  42168. goto success_with_arguments_exotic;
  42169. create_new:
  42170. /*
  42171. * Create a new property in the original object.
  42172. *
  42173. * Exotic properties need to be reconsidered here from a write
  42174. * perspective (not just property attributes perspective).
  42175. * However, the property does not exist in the object already,
  42176. * so this limits the kind of exotic properties that apply.
  42177. */
  42178. /* [key] */
  42179. DUK_DDD(DUK_DDDPRINT("create new property to original object"));
  42180. DUK_ASSERT(orig != NULL);
  42181. /* Not possible because array object 'length' is present
  42182. * from its creation and cannot be deleted, and is thus
  42183. * caught as an existing property above.
  42184. */
  42185. DUK_ASSERT(!(DUK_HOBJECT_HAS_EXOTIC_ARRAY(orig) &&
  42186. key == DUK_HTHREAD_STRING_LENGTH(thr)));
  42187. if (DUK_HOBJECT_HAS_EXOTIC_ARRAY(orig) &&
  42188. arr_idx != DUK__NO_ARRAY_INDEX) {
  42189. /* automatic length update */
  42190. duk_uint32_t old_len;
  42191. old_len = duk__get_old_array_length(thr, orig, &desc);
  42192. if (arr_idx >= old_len) {
  42193. DUK_DDD(DUK_DDDPRINT("write new array entry requires length update "
  42194. "(arr_idx=%ld, old_len=%ld)",
  42195. (long) arr_idx, (long) old_len));
  42196. if (!(desc.flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  42197. DUK_DD(DUK_DDPRINT("attempt to extend array, but array 'length' is not writable"));
  42198. goto fail_not_writable;
  42199. }
  42200. /* Note: actual update happens once write has been completed
  42201. * without error below. The write should always succeed
  42202. * from a specification viewpoint, but we may e.g. run out
  42203. * of memory. It's safer in this order.
  42204. */
  42205. DUK_ASSERT(arr_idx != 0xffffffffUL);
  42206. new_array_length = arr_idx + 1; /* flag for later write */
  42207. } else {
  42208. DUK_DDD(DUK_DDDPRINT("write new array entry does not require length update "
  42209. "(arr_idx=%ld, old_len=%ld)",
  42210. (long) arr_idx, (long) old_len));
  42211. }
  42212. }
  42213. /* write_to_array_part: */
  42214. /*
  42215. * Write to array part?
  42216. *
  42217. * Note: array abandonding requires a property resize which uses
  42218. * 'rechecks' valstack for temporaries and may cause any existing
  42219. * valstack pointers to be invalidated. To protect against this,
  42220. * tv_obj, tv_key, and tv_val are copies of the original inputs.
  42221. */
  42222. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  42223. DUK_HOBJECT_HAS_ARRAY_PART(orig)) {
  42224. if (arr_idx < DUK_HOBJECT_GET_ASIZE(orig)) {
  42225. goto no_array_growth;
  42226. }
  42227. /*
  42228. * Array needs to grow, but we don't want it becoming too sparse.
  42229. * If it were to become sparse, abandon array part, moving all
  42230. * array entries into the entries part (for good).
  42231. *
  42232. * Since we don't keep track of actual density (used vs. size) of
  42233. * the array part, we need to estimate somehow. The check is made
  42234. * in two parts:
  42235. *
  42236. * - Check whether the resize need is small compared to the
  42237. * current size (relatively); if so, resize without further
  42238. * checking (essentially we assume that the original part is
  42239. * "dense" so that the result would be dense enough).
  42240. *
  42241. * - Otherwise, compute the resize using an actual density
  42242. * measurement based on counting the used array entries.
  42243. */
  42244. DUK_DDD(DUK_DDDPRINT("write to new array requires array resize, decide whether to do a "
  42245. "fast resize without abandon check (arr_idx=%ld, old_size=%ld)",
  42246. (long) arr_idx, (long) DUK_HOBJECT_GET_ASIZE(orig)));
  42247. if (duk__abandon_array_slow_check_required(arr_idx, DUK_HOBJECT_GET_ASIZE(orig))) {
  42248. duk_uint32_t old_used;
  42249. duk_uint32_t old_size;
  42250. DUK_DDD(DUK_DDDPRINT("=> fast check is NOT OK, do slow check for array abandon"));
  42251. duk__compute_a_stats(thr, orig, &old_used, &old_size);
  42252. DUK_DDD(DUK_DDDPRINT("abandon check, array stats: old_used=%ld, old_size=%ld, arr_idx=%ld",
  42253. (long) old_used, (long) old_size, (long) arr_idx));
  42254. /* Note: intentionally use approximations to shave a few instructions:
  42255. * a_used = old_used (accurate: old_used + 1)
  42256. * a_size = arr_idx (accurate: arr_idx + 1)
  42257. */
  42258. if (duk__abandon_array_density_check(old_used, arr_idx)) {
  42259. DUK_DD(DUK_DDPRINT("write to new array entry beyond current length, "
  42260. "decided to abandon array part (would become too sparse)"));
  42261. /* abandoning requires a props allocation resize and
  42262. * 'rechecks' the valstack, invalidating any existing
  42263. * valstack value pointers!
  42264. */
  42265. duk__abandon_array_checked(thr, orig);
  42266. DUK_ASSERT(!DUK_HOBJECT_HAS_ARRAY_PART(orig));
  42267. goto write_to_entry_part;
  42268. }
  42269. DUK_DDD(DUK_DDDPRINT("=> decided to keep array part"));
  42270. } else {
  42271. DUK_DDD(DUK_DDDPRINT("=> fast resize is OK"));
  42272. }
  42273. DUK_DD(DUK_DDPRINT("write to new array entry beyond current length, "
  42274. "decided to extend current allocation"));
  42275. duk__grow_props_for_array_item(thr, orig, arr_idx);
  42276. no_array_growth:
  42277. /* Note: assume array part is comprehensive, so that either
  42278. * the write goes to the array part, or we've abandoned the
  42279. * array above (and will not come here).
  42280. */
  42281. DUK_ASSERT(DUK_HOBJECT_HAS_ARRAY_PART(orig));
  42282. DUK_ASSERT(arr_idx < DUK_HOBJECT_GET_ASIZE(orig));
  42283. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, orig, arr_idx);
  42284. /* prev value must be unused, no decref */
  42285. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED_UNUSED(tv));
  42286. DUK_TVAL_SET_TVAL(tv, tv_val);
  42287. DUK_TVAL_INCREF(thr, tv);
  42288. DUK_DD(DUK_DDPRINT("put to new array entry: %ld -> %!T",
  42289. (long) arr_idx, (duk_tval *) tv));
  42290. /* Note: array part values are [[Writable]], [[Enumerable]],
  42291. * and [[Configurable]] which matches the required attributes
  42292. * here.
  42293. */
  42294. goto entry_updated;
  42295. }
  42296. write_to_entry_part:
  42297. /*
  42298. * Write to entry part
  42299. */
  42300. /* entry allocation updates hash part and increases the key
  42301. * refcount; may need a props allocation resize but doesn't
  42302. * 'recheck' the valstack.
  42303. */
  42304. e_idx = duk__alloc_entry_checked(thr, orig, key);
  42305. DUK_ASSERT(e_idx >= 0);
  42306. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, orig, e_idx);
  42307. /* prev value can be garbage, no decref */
  42308. DUK_TVAL_SET_TVAL(tv, tv_val);
  42309. DUK_TVAL_INCREF(thr, tv);
  42310. DUK_HOBJECT_E_SET_FLAGS(thr->heap, orig, e_idx, DUK_PROPDESC_FLAGS_WEC);
  42311. goto entry_updated;
  42312. entry_updated:
  42313. /*
  42314. * Possible pending array length update, which must only be done
  42315. * if the actual entry write succeeded.
  42316. */
  42317. if (new_array_length > 0) {
  42318. /*
  42319. * Note: zero works as a "no update" marker because the new length
  42320. * can never be zero after a new property is written.
  42321. *
  42322. * Note: must re-lookup because calls above (e.g. duk__alloc_entry_checked())
  42323. * may realloc and compact properties and hence change e_idx.
  42324. */
  42325. DUK_DDD(DUK_DDDPRINT("write successful, pending array length update to: %ld",
  42326. (long) new_array_length));
  42327. 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 */
  42328. DUK_UNREF(rc);
  42329. DUK_ASSERT(rc != 0);
  42330. DUK_ASSERT(desc.e_idx >= 0);
  42331. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, orig, desc.e_idx);
  42332. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  42333. /* no need for decref/incref because value is a number */
  42334. #if defined(DUK_USE_FASTINT)
  42335. DUK_TVAL_SET_FASTINT_U32(tv, new_array_length);
  42336. #else
  42337. DUK_TVAL_SET_NUMBER(tv, (duk_double_t) new_array_length);
  42338. #endif
  42339. }
  42340. /*
  42341. * Arguments exotic behavior not possible for new properties: all
  42342. * magically bound properties are initially present in the arguments
  42343. * object, and if they are deleted, the binding is also removed from
  42344. * parameter map.
  42345. */
  42346. goto success_no_arguments_exotic;
  42347. success_with_arguments_exotic:
  42348. /*
  42349. * Arguments objects have exotic [[DefineOwnProperty]] which updates
  42350. * the internal 'map' of arguments for writes to currently mapped
  42351. * arguments. More conretely, writes to mapped arguments generate
  42352. * a write to a bound variable.
  42353. *
  42354. * The [[Put]] algorithm invokes [[DefineOwnProperty]] for existing
  42355. * data properties and new properties, but not for existing accessors.
  42356. * Hence, in E5 Section 10.6 ([[DefinedOwnProperty]] algorithm), we
  42357. * have a Desc with 'Value' (and possibly other properties too), and
  42358. * we end up in step 5.b.i.
  42359. */
  42360. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  42361. DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(orig)) {
  42362. /* Note: only numbered indices are relevant, so arr_idx fast reject
  42363. * is good (this is valid unless there are more than 4**32-1 arguments).
  42364. */
  42365. DUK_DDD(DUK_DDDPRINT("putprop successful, arguments exotic behavior needed"));
  42366. /* Note: we can reuse 'desc' here */
  42367. /* XXX: top of stack must contain value, which helper doesn't touch,
  42368. * rework to use tv_val directly?
  42369. */
  42370. duk_push_tval(ctx, tv_val);
  42371. (void) duk__check_arguments_map_for_put(thr, orig, key, &desc, throw_flag);
  42372. duk_pop(ctx);
  42373. }
  42374. /* fall thru */
  42375. success_no_arguments_exotic:
  42376. /* shared exit path now */
  42377. DUK_DDD(DUK_DDDPRINT("result: success"));
  42378. duk_pop(ctx); /* remove key */
  42379. return 1;
  42380. fail_proxy_rejected:
  42381. DUK_DDD(DUK_DDDPRINT("result: error, proxy rejects"));
  42382. if (throw_flag) {
  42383. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROXY_REJECTED);
  42384. }
  42385. /* Note: no key on stack */
  42386. return 0;
  42387. fail_base_primitive:
  42388. DUK_DDD(DUK_DDDPRINT("result: error, base primitive"));
  42389. if (throw_flag) {
  42390. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_BASE);
  42391. }
  42392. duk_pop(ctx); /* remove key */
  42393. return 0;
  42394. fail_not_extensible:
  42395. DUK_DDD(DUK_DDDPRINT("result: error, not extensible"));
  42396. if (throw_flag) {
  42397. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_EXTENSIBLE);
  42398. }
  42399. duk_pop(ctx); /* remove key */
  42400. return 0;
  42401. fail_not_writable:
  42402. DUK_DDD(DUK_DDDPRINT("result: error, not writable"));
  42403. if (throw_flag) {
  42404. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_WRITABLE);
  42405. }
  42406. duk_pop(ctx); /* remove key */
  42407. return 0;
  42408. fail_array_length_partial:
  42409. DUK_DDD(DUK_DDDPRINT("result: error, array length write only partially successful"));
  42410. if (throw_flag) {
  42411. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_ARRAY_LENGTH_WRITE_FAILED);
  42412. }
  42413. duk_pop(ctx); /* remove key */
  42414. return 0;
  42415. fail_no_setter:
  42416. DUK_DDD(DUK_DDDPRINT("result: error, accessor property without setter"));
  42417. if (throw_flag) {
  42418. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_SETTER_UNDEFINED);
  42419. }
  42420. duk_pop(ctx); /* remove key */
  42421. return 0;
  42422. fail_internal:
  42423. DUK_DDD(DUK_DDDPRINT("result: error, internal"));
  42424. if (throw_flag) {
  42425. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INTERNAL_ERROR);
  42426. }
  42427. duk_pop(ctx); /* remove key */
  42428. return 0;
  42429. }
  42430. /*
  42431. * Ecmascript compliant [[Delete]](P, Throw).
  42432. */
  42433. DUK_INTERNAL duk_bool_t duk_hobject_delprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags) {
  42434. duk_propdesc desc;
  42435. duk_tval *tv;
  42436. duk_tval tv_tmp;
  42437. duk_uint32_t arr_idx;
  42438. duk_bool_t throw_flag;
  42439. duk_bool_t force_flag;
  42440. throw_flag = (flags & DUK_DELPROP_FLAG_THROW);
  42441. force_flag = (flags & DUK_DELPROP_FLAG_FORCE);
  42442. DUK_DDD(DUK_DDDPRINT("delprop_raw: thr=%p, obj=%p, key=%p, throw=%ld, force=%ld (obj -> %!O, key -> %!O)",
  42443. (void *) thr, (void *) obj, (void *) key, (long) throw_flag, (long) force_flag,
  42444. (duk_heaphdr *) obj, (duk_heaphdr *) key));
  42445. DUK_ASSERT(thr != NULL);
  42446. DUK_ASSERT(thr->heap != NULL);
  42447. DUK_ASSERT(obj != NULL);
  42448. DUK_ASSERT(key != NULL);
  42449. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  42450. arr_idx = DUK_HSTRING_GET_ARRIDX_FAST(key);
  42451. /* 0 = don't push current value */
  42452. if (!duk__get_own_property_desc_raw(thr, obj, key, arr_idx, &desc, 0 /*flags*/)) { /* don't push value */
  42453. DUK_DDD(DUK_DDDPRINT("property not found, succeed always"));
  42454. goto success;
  42455. }
  42456. if ((desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) == 0 && !force_flag) {
  42457. goto fail_not_configurable;
  42458. }
  42459. if (desc.a_idx < 0 && desc.e_idx < 0) {
  42460. /* Currently there are no deletable virtual properties, but
  42461. * with force_flag we might attempt to delete one.
  42462. */
  42463. goto fail_virtual;
  42464. }
  42465. if (desc.a_idx >= 0) {
  42466. DUK_ASSERT(desc.e_idx < 0);
  42467. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, desc.a_idx);
  42468. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  42469. DUK_TVAL_SET_UNDEFINED_UNUSED(tv);
  42470. DUK_TVAL_DECREF(thr, &tv_tmp);
  42471. goto success;
  42472. } else {
  42473. DUK_ASSERT(desc.a_idx < 0);
  42474. /* remove hash entry (no decref) */
  42475. #if defined(DUK_USE_HOBJECT_HASH_PART)
  42476. if (desc.h_idx >= 0) {
  42477. duk_uint32_t *h_base = DUK_HOBJECT_H_GET_BASE(thr->heap, obj);
  42478. DUK_DDD(DUK_DDDPRINT("removing hash entry at h_idx %ld", (long) desc.h_idx));
  42479. DUK_ASSERT(DUK_HOBJECT_GET_HSIZE(obj) > 0);
  42480. DUK_ASSERT((duk_uint32_t) desc.h_idx < DUK_HOBJECT_GET_HSIZE(obj));
  42481. h_base[desc.h_idx] = DUK__HASH_DELETED;
  42482. } else {
  42483. DUK_ASSERT(DUK_HOBJECT_GET_HSIZE(obj) == 0);
  42484. }
  42485. #else
  42486. DUK_ASSERT(DUK_HOBJECT_GET_HSIZE(obj) == 0);
  42487. #endif
  42488. /* remove value */
  42489. DUK_DDD(DUK_DDDPRINT("before removing value, e_idx %ld, key %p, key at slot %p",
  42490. (long) desc.e_idx, (void *) key, (void *) DUK_HOBJECT_E_GET_KEY(thr->heap, obj, desc.e_idx)));
  42491. DUK_DDD(DUK_DDDPRINT("removing value at e_idx %ld", (long) desc.e_idx));
  42492. if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, desc.e_idx)) {
  42493. duk_hobject *tmp;
  42494. tmp = DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, obj, desc.e_idx);
  42495. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, desc.e_idx, NULL);
  42496. DUK_UNREF(tmp);
  42497. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  42498. tmp = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, obj, desc.e_idx);
  42499. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, desc.e_idx, NULL);
  42500. DUK_UNREF(tmp);
  42501. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  42502. } else {
  42503. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, desc.e_idx);
  42504. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  42505. DUK_TVAL_SET_UNDEFINED_UNUSED(tv);
  42506. DUK_TVAL_DECREF(thr, &tv_tmp);
  42507. }
  42508. /* this is not strictly necessary because if key == NULL, value MUST be ignored */
  42509. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, desc.e_idx, 0);
  42510. DUK_TVAL_SET_UNDEFINED_UNUSED(DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, desc.e_idx));
  42511. /* remove key */
  42512. DUK_DDD(DUK_DDDPRINT("before removing key, e_idx %ld, key %p, key at slot %p",
  42513. (long) desc.e_idx, (void *) key, (void *) DUK_HOBJECT_E_GET_KEY(thr->heap, obj, desc.e_idx)));
  42514. DUK_DDD(DUK_DDDPRINT("removing key at e_idx %ld", (long) desc.e_idx));
  42515. DUK_ASSERT(key == DUK_HOBJECT_E_GET_KEY(thr->heap, obj, desc.e_idx));
  42516. DUK_HOBJECT_E_SET_KEY(thr->heap, obj, desc.e_idx, NULL);
  42517. DUK_HSTRING_DECREF(thr, key);
  42518. goto success;
  42519. }
  42520. DUK_UNREACHABLE();
  42521. success:
  42522. /*
  42523. * Argument exotic [[Delete]] behavior (E5 Section 10.6) is
  42524. * a post-check, keeping arguments internal 'map' in sync with
  42525. * any successful deletes (note that property does not need to
  42526. * exist for delete to 'succeed').
  42527. *
  42528. * Delete key from 'map'. Since 'map' only contains array index
  42529. * keys, we can use arr_idx for a fast skip.
  42530. */
  42531. DUK_DDD(DUK_DDDPRINT("delete successful, check for arguments exotic behavior"));
  42532. if (arr_idx != DUK__NO_ARRAY_INDEX && DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj)) {
  42533. /* Note: only numbered indices are relevant, so arr_idx fast reject
  42534. * is good (this is valid unless there are more than 4**32-1 arguments).
  42535. */
  42536. DUK_DDD(DUK_DDDPRINT("delete successful, arguments exotic behavior needed"));
  42537. /* Note: we can reuse 'desc' here */
  42538. (void) duk__check_arguments_map_for_delete(thr, obj, key, &desc);
  42539. }
  42540. DUK_DDD(DUK_DDDPRINT("delete successful"));
  42541. return 1;
  42542. fail_virtual:
  42543. DUK_DDD(DUK_DDDPRINT("delete failed: property found, force flag, but virtual"));
  42544. if (throw_flag) {
  42545. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROPERTY_IS_VIRTUAL);
  42546. }
  42547. return 0;
  42548. fail_not_configurable:
  42549. DUK_DDD(DUK_DDDPRINT("delete failed: property found, not configurable"));
  42550. if (throw_flag) {
  42551. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_CONFIGURABLE);
  42552. }
  42553. return 0;
  42554. }
  42555. /*
  42556. * DELPROP: Ecmascript property deletion.
  42557. */
  42558. DUK_INTERNAL duk_bool_t duk_hobject_delprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key, duk_bool_t throw_flag) {
  42559. duk_context *ctx = (duk_context *) thr;
  42560. duk_hstring *key = NULL;
  42561. #if defined(DUK_USE_ES6_PROXY)
  42562. duk_propdesc desc;
  42563. #endif
  42564. duk_int_t entry_top;
  42565. duk_uint32_t arr_idx = DUK__NO_ARRAY_INDEX;
  42566. duk_bool_t rc;
  42567. DUK_DDD(DUK_DDDPRINT("delprop: thr=%p, obj=%p, key=%p (obj -> %!T, key -> %!T)",
  42568. (void *) thr, (void *) tv_obj, (void *) tv_key,
  42569. (duk_tval *) tv_obj, (duk_tval *) tv_key));
  42570. DUK_ASSERT(ctx != NULL);
  42571. DUK_ASSERT(thr != NULL);
  42572. DUK_ASSERT(thr->heap != NULL);
  42573. DUK_ASSERT(tv_obj != NULL);
  42574. DUK_ASSERT(tv_key != NULL);
  42575. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  42576. /* Storing the entry top is cheaper here to ensure stack is correct at exit,
  42577. * as there are several paths out.
  42578. */
  42579. entry_top = duk_get_top(ctx);
  42580. if (DUK_TVAL_IS_UNDEFINED(tv_obj) ||
  42581. DUK_TVAL_IS_NULL(tv_obj)) {
  42582. DUK_DDD(DUK_DDDPRINT("base object is undefined or null -> reject"));
  42583. goto fail_invalid_base_uncond;
  42584. }
  42585. duk_push_tval(ctx, tv_obj);
  42586. duk_push_tval(ctx, tv_key);
  42587. tv_obj = duk_get_tval(ctx, -2);
  42588. if (DUK_TVAL_IS_OBJECT(tv_obj)) {
  42589. duk_hobject *obj = DUK_TVAL_GET_OBJECT(tv_obj);
  42590. DUK_ASSERT(obj != NULL);
  42591. #if defined(DUK_USE_ES6_PROXY)
  42592. if (DUK_UNLIKELY(DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(obj))) {
  42593. duk_hobject *h_target;
  42594. duk_bool_t tmp_bool;
  42595. /* Note: proxy handling must happen before key is string coerced. */
  42596. if (duk__proxy_check_prop(thr, obj, DUK_STRIDX_DELETE_PROPERTY, tv_key, &h_target)) {
  42597. /* -> [ ... trap handler ] */
  42598. DUK_DDD(DUK_DDDPRINT("-> proxy object 'deleteProperty' for key %!T", (duk_tval *) tv_key));
  42599. duk_push_hobject(ctx, h_target); /* target */
  42600. duk_push_tval(ctx, tv_key); /* P */
  42601. duk_call_method(ctx, 2 /*nargs*/);
  42602. tmp_bool = duk_to_boolean(ctx, -1);
  42603. duk_pop(ctx);
  42604. if (!tmp_bool) {
  42605. goto fail_proxy_rejected; /* retval indicates delete failed */
  42606. }
  42607. /* Target object must be checked for a conflicting
  42608. * non-configurable property.
  42609. */
  42610. arr_idx = duk__push_tval_to_hstring_arr_idx(ctx, tv_key, &key);
  42611. DUK_ASSERT(key != NULL);
  42612. if (duk__get_own_property_desc_raw(thr, h_target, key, arr_idx, &desc, 0 /*flags*/)) { /* don't push value */
  42613. int desc_reject;
  42614. DUK_DDD(DUK_DDDPRINT("proxy 'deleteProperty': target has matching property %!O, check for "
  42615. "conflicting property; desc.flags=0x%08lx, "
  42616. "desc.get=%p, desc.set=%p",
  42617. (duk_heaphdr *) key, (unsigned long) desc.flags,
  42618. (void *) desc.get, (void *) desc.set));
  42619. desc_reject = !(desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE);
  42620. if (desc_reject) {
  42621. /* unconditional */
  42622. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROXY_REJECTED);
  42623. }
  42624. }
  42625. rc = 1; /* success */
  42626. goto done_rc;
  42627. }
  42628. obj = h_target; /* resume delete to target */
  42629. }
  42630. #endif /* DUK_USE_ES6_PROXY */
  42631. duk_to_string(ctx, -1);
  42632. key = duk_get_hstring(ctx, -1);
  42633. DUK_ASSERT(key != NULL);
  42634. rc = duk_hobject_delprop_raw(thr, obj, key, throw_flag ? DUK_DELPROP_FLAG_THROW : 0);
  42635. goto done_rc;
  42636. } else if (DUK_TVAL_IS_STRING(tv_obj)) {
  42637. /* XXX: unnecessary string coercion for array indices,
  42638. * intentional to keep small.
  42639. */
  42640. duk_hstring *h = DUK_TVAL_GET_STRING(tv_obj);
  42641. DUK_ASSERT(h != NULL);
  42642. duk_to_string(ctx, -1);
  42643. key = duk_get_hstring(ctx, -1);
  42644. DUK_ASSERT(key != NULL);
  42645. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  42646. goto fail_not_configurable;
  42647. }
  42648. arr_idx = DUK_HSTRING_GET_ARRIDX_FAST(key);
  42649. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  42650. arr_idx < DUK_HSTRING_GET_CHARLEN(h)) {
  42651. goto fail_not_configurable;
  42652. }
  42653. } else if (DUK_TVAL_IS_BUFFER(tv_obj)) {
  42654. /* XXX: unnecessary string coercion for array indices,
  42655. * intentional to keep small; some overlap with string
  42656. * handling.
  42657. */
  42658. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv_obj);
  42659. DUK_ASSERT(h != NULL);
  42660. duk_to_string(ctx, -1);
  42661. key = duk_get_hstring(ctx, -1);
  42662. DUK_ASSERT(key != NULL);
  42663. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  42664. goto fail_not_configurable;
  42665. }
  42666. arr_idx = DUK_HSTRING_GET_ARRIDX_FAST(key);
  42667. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  42668. arr_idx < DUK_HBUFFER_GET_SIZE(h)) {
  42669. goto fail_not_configurable;
  42670. }
  42671. } else if (DUK_TVAL_IS_LIGHTFUNC(tv_obj)) {
  42672. /* Lightfunc virtual properties are non-configurable, so
  42673. * reject if match any of them.
  42674. */
  42675. duk_to_string(ctx, -1);
  42676. key = duk_get_hstring(ctx, -1);
  42677. DUK_ASSERT(key != NULL);
  42678. if (duk__key_is_lightfunc_ownprop(thr, key)) {
  42679. goto fail_not_configurable;
  42680. }
  42681. }
  42682. /* non-object base, no offending virtual property */
  42683. rc = 1;
  42684. goto done_rc;
  42685. done_rc:
  42686. duk_set_top(ctx, entry_top);
  42687. return rc;
  42688. fail_invalid_base_uncond:
  42689. /* Note: unconditional throw */
  42690. DUK_ASSERT(duk_get_top(ctx) == entry_top);
  42691. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_BASE);
  42692. return 0;
  42693. fail_proxy_rejected:
  42694. if (throw_flag) {
  42695. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROXY_REJECTED);
  42696. }
  42697. duk_set_top(ctx, entry_top);
  42698. return 0;
  42699. fail_not_configurable:
  42700. if (throw_flag) {
  42701. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_CONFIGURABLE);
  42702. }
  42703. duk_set_top(ctx, entry_top);
  42704. return 0;
  42705. }
  42706. /*
  42707. * Internal helper to define a property with specific flags, ignoring
  42708. * normal semantics such as extensibility, write protection etc.
  42709. * Overwrites any existing value and attributes unless caller requests
  42710. * that value only be updated if it doesn't already exists.
  42711. *
  42712. * Does not support:
  42713. * - virtual properties (error if write attempted)
  42714. * - getter/setter properties (error if write attempted)
  42715. * - non-default (!= WEC) attributes for array entries (error if attempted)
  42716. * - array abandoning: if array part exists, it is always extended
  42717. * - array 'length' updating
  42718. *
  42719. * Stack: [... in_val] -> []
  42720. *
  42721. * Used for e.g. built-in initialization and environment record
  42722. * operations.
  42723. */
  42724. DUK_INTERNAL void duk_hobject_define_property_internal(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags) {
  42725. duk_context *ctx = (duk_context *) thr;
  42726. duk_propdesc desc;
  42727. duk_uint32_t arr_idx;
  42728. duk_int_t e_idx;
  42729. duk_tval tv_tmp;
  42730. duk_tval *tv1 = NULL;
  42731. duk_tval *tv2 = NULL;
  42732. duk_small_uint_t propflags = flags & DUK_PROPDESC_FLAGS_MASK; /* mask out flags not actually stored */
  42733. DUK_DDD(DUK_DDDPRINT("define new property (internal): thr=%p, obj=%!O, key=%!O, flags=0x%02lx, val=%!T",
  42734. (void *) thr, (duk_heaphdr *) obj, (duk_heaphdr *) key,
  42735. (unsigned long) flags, (duk_tval *) duk_get_tval(ctx, -1)));
  42736. DUK_ASSERT(thr != NULL);
  42737. DUK_ASSERT(thr->heap != NULL);
  42738. DUK_ASSERT(obj != NULL);
  42739. DUK_ASSERT(key != NULL);
  42740. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  42741. DUK_ASSERT(duk_is_valid_index(ctx, -1)); /* contains value */
  42742. arr_idx = DUK_HSTRING_GET_ARRIDX_SLOW(key);
  42743. if (duk__get_own_property_desc_raw(thr, obj, key, arr_idx, &desc, 0 /*flags*/)) { /* don't push value */
  42744. if (desc.e_idx >= 0) {
  42745. if (flags & DUK_PROPDESC_FLAG_NO_OVERWRITE) {
  42746. DUK_DDD(DUK_DDDPRINT("property already exists in the entry part -> skip as requested"));
  42747. goto pop_exit;
  42748. }
  42749. DUK_DDD(DUK_DDDPRINT("property already exists in the entry part -> update value and attributes"));
  42750. if (DUK_UNLIKELY(DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, desc.e_idx))) {
  42751. DUK_D(DUK_DPRINT("existing property is an accessor, not supported"));
  42752. goto error_internal;
  42753. }
  42754. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, desc.e_idx, propflags);
  42755. tv1 = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, desc.e_idx);
  42756. } else if (desc.a_idx >= 0) {
  42757. if (flags & DUK_PROPDESC_FLAG_NO_OVERWRITE) {
  42758. DUK_DDD(DUK_DDDPRINT("property already exists in the array part -> skip as requested"));
  42759. goto pop_exit;
  42760. }
  42761. DUK_DDD(DUK_DDDPRINT("property already exists in the array part -> update value (assert attributes)"));
  42762. if (propflags != DUK_PROPDESC_FLAGS_WEC) {
  42763. DUK_D(DUK_DPRINT("existing property in array part, but propflags not WEC (0x%02lx)",
  42764. (unsigned long) propflags));
  42765. goto error_internal;
  42766. }
  42767. tv1 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, desc.a_idx);
  42768. } else {
  42769. if (flags & DUK_PROPDESC_FLAG_NO_OVERWRITE) {
  42770. DUK_DDD(DUK_DDDPRINT("property already exists but is virtual -> skip as requested"));
  42771. goto pop_exit;
  42772. }
  42773. DUK_DDD(DUK_DDDPRINT("property already exists but is virtual -> failure"));
  42774. goto error_virtual;
  42775. }
  42776. goto write_value;
  42777. }
  42778. if (DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  42779. if (arr_idx != DUK__NO_ARRAY_INDEX) {
  42780. DUK_DDD(DUK_DDDPRINT("property does not exist, object has array part -> possibly extend array part and write value (assert attributes)"));
  42781. DUK_ASSERT(propflags == DUK_PROPDESC_FLAGS_WEC);
  42782. /* always grow the array, no sparse / abandon support here */
  42783. if (arr_idx >= DUK_HOBJECT_GET_ASIZE(obj)) {
  42784. duk__grow_props_for_array_item(thr, obj, arr_idx);
  42785. }
  42786. DUK_ASSERT(arr_idx < DUK_HOBJECT_GET_ASIZE(obj));
  42787. tv1 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, arr_idx);
  42788. goto write_value;
  42789. }
  42790. }
  42791. DUK_DDD(DUK_DDDPRINT("property does not exist, object belongs in entry part -> allocate new entry and write value and attributes"));
  42792. e_idx = duk__alloc_entry_checked(thr, obj, key); /* increases key refcount */
  42793. DUK_ASSERT(e_idx >= 0);
  42794. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, e_idx, propflags);
  42795. tv1 = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, e_idx);
  42796. /* new entry: previous value is garbage; set to undefined to share write_value */
  42797. DUK_TVAL_SET_UNDEFINED_ACTUAL(tv1);
  42798. goto write_value;
  42799. write_value:
  42800. /* tv1 points to value storage */
  42801. tv2 = duk_require_tval(ctx, -1); /* late lookup, avoid side effects */
  42802. DUK_DDD(DUK_DDDPRINT("writing/updating value: %!T -> %!T",
  42803. (duk_tval *) tv1, (duk_tval *) tv2));
  42804. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  42805. DUK_TVAL_SET_TVAL(tv1, tv2);
  42806. DUK_TVAL_INCREF(thr, tv1);
  42807. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  42808. goto pop_exit;
  42809. pop_exit:
  42810. duk_pop(ctx); /* remove in_val */
  42811. return;
  42812. error_internal:
  42813. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR);
  42814. return;
  42815. error_virtual:
  42816. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_REDEFINE_VIRT_PROP);
  42817. return;
  42818. }
  42819. /*
  42820. * Fast path for defining array indexed values without interning the key.
  42821. * This is used by e.g. code for Array prototype and traceback creation so
  42822. * must avoid interning.
  42823. */
  42824. 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) {
  42825. duk_context *ctx = (duk_context *) thr;
  42826. duk_hstring *key;
  42827. duk_tval *tv1, *tv2;
  42828. duk_tval tv_tmp;
  42829. DUK_DDD(DUK_DDDPRINT("define new property (internal) arr_idx fast path: thr=%p, obj=%!O, "
  42830. "arr_idx=%ld, flags=0x%02lx, val=%!T",
  42831. (void *) thr, obj, (long) arr_idx, (unsigned long) flags,
  42832. (duk_tval *) duk_get_tval(ctx, -1)));
  42833. DUK_ASSERT(thr != NULL);
  42834. DUK_ASSERT(thr->heap != NULL);
  42835. DUK_ASSERT(obj != NULL);
  42836. if (DUK_HOBJECT_HAS_ARRAY_PART(obj) &&
  42837. arr_idx != DUK__NO_ARRAY_INDEX &&
  42838. flags == DUK_PROPDESC_FLAGS_WEC) {
  42839. DUK_ASSERT((flags & DUK_PROPDESC_FLAG_NO_OVERWRITE) == 0); /* covered by comparison */
  42840. DUK_DDD(DUK_DDDPRINT("define property to array part (property may or may not exist yet)"));
  42841. /* always grow the array, no sparse / abandon support here */
  42842. if (arr_idx >= DUK_HOBJECT_GET_ASIZE(obj)) {
  42843. duk__grow_props_for_array_item(thr, obj, arr_idx);
  42844. }
  42845. DUK_ASSERT(arr_idx < DUK_HOBJECT_GET_ASIZE(obj));
  42846. tv1 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, arr_idx);
  42847. tv2 = duk_require_tval(ctx, -1);
  42848. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  42849. DUK_TVAL_SET_TVAL(tv1, tv2);
  42850. DUK_TVAL_INCREF(thr, tv1);
  42851. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  42852. duk_pop(ctx); /* [ ...val ] -> [ ... ] */
  42853. return;
  42854. }
  42855. DUK_DDD(DUK_DDDPRINT("define property fast path didn't work, use slow path"));
  42856. duk_push_uint(ctx, (duk_uint_t) arr_idx);
  42857. key = duk_to_hstring(ctx, -1);
  42858. DUK_ASSERT(key != NULL);
  42859. duk_insert(ctx, -2); /* [ ... val key ] -> [ ... key val ] */
  42860. duk_hobject_define_property_internal(thr, obj, key, flags);
  42861. duk_pop(ctx); /* [ ... key ] -> [ ... ] */
  42862. }
  42863. /*
  42864. * Internal helper for defining an accessor property, ignoring
  42865. * normal semantics such as extensibility, write protection etc.
  42866. * Overwrites any existing value and attributes. This is called
  42867. * very rarely, so the implementation first sets a value to undefined
  42868. * and then changes the entry to an accessor (this is to save code space).
  42869. */
  42870. 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) {
  42871. duk_context *ctx = (duk_context *) thr;
  42872. duk_int_t e_idx;
  42873. duk_int_t h_idx;
  42874. DUK_DDD(DUK_DDDPRINT("define new accessor (internal): thr=%p, obj=%!O, key=%!O, "
  42875. "getter=%!O, setter=%!O, flags=0x%02lx",
  42876. (void *) thr, (duk_heaphdr *) obj, (duk_heaphdr *) key,
  42877. (duk_heaphdr *) getter, (duk_heaphdr *) setter,
  42878. (unsigned long) propflags));
  42879. DUK_ASSERT(thr != NULL);
  42880. DUK_ASSERT(thr->heap != NULL);
  42881. DUK_ASSERT(obj != NULL);
  42882. DUK_ASSERT(key != NULL);
  42883. DUK_ASSERT((propflags & ~DUK_PROPDESC_FLAGS_MASK) == 0);
  42884. /* setter and/or getter may be NULL */
  42885. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  42886. /* force the property to 'undefined' to create a slot for it */
  42887. duk_push_undefined(ctx);
  42888. duk_hobject_define_property_internal(thr, obj, key, propflags);
  42889. duk_hobject_find_existing_entry(thr->heap, obj, key, &e_idx, &h_idx);
  42890. DUK_DDD(DUK_DDDPRINT("accessor slot: e_idx=%ld, h_idx=%ld", (long) e_idx, (long) h_idx));
  42891. DUK_ASSERT(e_idx >= 0);
  42892. DUK_ASSERT((duk_uint32_t) e_idx < DUK_HOBJECT_GET_ENEXT(obj));
  42893. /* no need to decref, as previous value is 'undefined' */
  42894. DUK_HOBJECT_E_SLOT_SET_ACCESSOR(thr->heap, obj, e_idx);
  42895. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, e_idx, getter);
  42896. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, e_idx, setter);
  42897. DUK_HOBJECT_INCREF_ALLOWNULL(thr, getter);
  42898. DUK_HOBJECT_INCREF_ALLOWNULL(thr, setter);
  42899. }
  42900. /*
  42901. * Internal helpers for managing object 'length'
  42902. */
  42903. /* XXX: awkward helpers */
  42904. DUK_INTERNAL void duk_hobject_set_length(duk_hthread *thr, duk_hobject *obj, duk_uint32_t length) {
  42905. duk_context *ctx = (duk_context *) thr;
  42906. duk_push_hobject(ctx, obj);
  42907. duk_push_hstring_stridx(ctx, DUK_STRIDX_LENGTH);
  42908. duk_push_u32(ctx, length);
  42909. (void) duk_hobject_putprop(thr, duk_get_tval(ctx, -3), duk_get_tval(ctx, -2), duk_get_tval(ctx, -1), 0);
  42910. duk_pop_n(ctx, 3);
  42911. }
  42912. DUK_INTERNAL void duk_hobject_set_length_zero(duk_hthread *thr, duk_hobject *obj) {
  42913. duk_hobject_set_length(thr, obj, 0);
  42914. }
  42915. DUK_INTERNAL duk_uint32_t duk_hobject_get_length(duk_hthread *thr, duk_hobject *obj) {
  42916. duk_context *ctx = (duk_context *) thr;
  42917. duk_double_t val;
  42918. duk_push_hobject(ctx, obj);
  42919. duk_push_hstring_stridx(ctx, DUK_STRIDX_LENGTH);
  42920. (void) duk_hobject_getprop(thr, duk_get_tval(ctx, -2), duk_get_tval(ctx, -1));
  42921. val = duk_to_number(ctx, -1);
  42922. duk_pop_n(ctx, 3);
  42923. if (val >= 0.0 && val < DUK_DOUBLE_2TO32) {
  42924. return (duk_uint32_t) val;
  42925. }
  42926. return 0;
  42927. }
  42928. /*
  42929. * Object.getOwnPropertyDescriptor() (E5 Sections 15.2.3.3, 8.10.4)
  42930. *
  42931. * This is an actual function call.
  42932. */
  42933. DUK_INTERNAL duk_ret_t duk_hobject_object_get_own_property_descriptor(duk_context *ctx) {
  42934. duk_hthread *thr = (duk_hthread *) ctx;
  42935. duk_hobject *obj;
  42936. duk_hstring *key;
  42937. duk_propdesc pd;
  42938. duk_bool_t rc;
  42939. DUK_ASSERT(ctx != NULL);
  42940. DUK_ASSERT(thr != NULL);
  42941. DUK_ASSERT(thr->heap != NULL);
  42942. obj = duk_require_hobject_or_lfunc_coerce(ctx, 0);
  42943. (void) duk_to_string(ctx, 1);
  42944. key = duk_require_hstring(ctx, 1);
  42945. DUK_ASSERT(obj != NULL);
  42946. DUK_ASSERT(key != NULL);
  42947. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  42948. rc = duk__get_own_property_desc(thr, obj, key, &pd, DUK__DESC_FLAG_PUSH_VALUE);
  42949. if (!rc) {
  42950. duk_push_undefined(ctx);
  42951. /* [obj key undefined] */
  42952. return 1;
  42953. }
  42954. duk_push_object(ctx);
  42955. /* [obj key value desc] */
  42956. if (DUK_PROPDESC_IS_ACCESSOR(&pd)) {
  42957. /* If a setter/getter is missing (undefined), the descriptor must
  42958. * still have the property present with the value 'undefined'.
  42959. */
  42960. if (pd.get) {
  42961. duk_push_hobject(ctx, pd.get);
  42962. } else {
  42963. duk_push_undefined(ctx);
  42964. }
  42965. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_GET);
  42966. if (pd.set) {
  42967. duk_push_hobject(ctx, pd.set);
  42968. } else {
  42969. duk_push_undefined(ctx);
  42970. }
  42971. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_SET);
  42972. } else {
  42973. duk_dup(ctx, -2); /* [obj key value desc value] */
  42974. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_VALUE);
  42975. duk_push_boolean(ctx, DUK_PROPDESC_IS_WRITABLE(&pd));
  42976. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_WRITABLE);
  42977. /* [obj key value desc] */
  42978. }
  42979. duk_push_boolean(ctx, DUK_PROPDESC_IS_ENUMERABLE(&pd));
  42980. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_ENUMERABLE);
  42981. duk_push_boolean(ctx, DUK_PROPDESC_IS_CONFIGURABLE(&pd));
  42982. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_CONFIGURABLE);
  42983. /* [obj key value desc] */
  42984. return 1;
  42985. }
  42986. /*
  42987. * NormalizePropertyDescriptor() related helper.
  42988. *
  42989. * Internal helper which validates and normalizes a property descriptor
  42990. * represented as an Ecmascript object (e.g. argument to defineProperty()).
  42991. * The output of this conversion is a set of defprop_flags and possibly
  42992. * some values pushed on the value stack; some subset of: property value,
  42993. * getter, setter. Caller must manage stack top carefully because the
  42994. * number of values pushed depends on the input property descriptor.
  42995. *
  42996. * The original descriptor object must not be altered in the process.
  42997. */
  42998. /* XXX: very basic optimization -> duk_get_prop_stridx_top */
  42999. DUK_INTERNAL
  43000. void duk_hobject_prepare_property_descriptor(duk_context *ctx,
  43001. duk_idx_t idx_in,
  43002. duk_uint_t *out_defprop_flags,
  43003. duk_idx_t *out_idx_value,
  43004. duk_hobject **out_getter,
  43005. duk_hobject **out_setter) {
  43006. duk_hthread *thr = (duk_hthread *) ctx;
  43007. duk_idx_t idx_value = -1;
  43008. duk_hobject *getter = NULL;
  43009. duk_hobject *setter = NULL;
  43010. duk_bool_t is_data_desc = 0;
  43011. duk_bool_t is_acc_desc = 0;
  43012. duk_uint_t defprop_flags = 0;
  43013. DUK_ASSERT(ctx != NULL);
  43014. DUK_ASSERT(out_defprop_flags != NULL);
  43015. DUK_ASSERT(out_idx_value != NULL);
  43016. DUK_ASSERT(out_getter != NULL);
  43017. DUK_ASSERT(out_setter != NULL);
  43018. /* Must be an object, otherwise TypeError (E5.1 Section 8.10.5, step 1). */
  43019. idx_in = duk_require_normalize_index(ctx, idx_in);
  43020. (void) duk_require_hobject(ctx, idx_in);
  43021. /* The coercion order must match the ToPropertyDescriptor() algorithm
  43022. * so that side effects in coercion happen in the correct order.
  43023. * (This order also happens to be compatible with duk_def_prop(),
  43024. * although it doesn't matter in practice.)
  43025. */
  43026. if (duk_get_prop_stridx(ctx, idx_in, DUK_STRIDX_VALUE)) {
  43027. is_data_desc = 1;
  43028. defprop_flags |= DUK_DEFPROP_HAVE_VALUE;
  43029. idx_value = duk_get_top_index(ctx);
  43030. /* Leave 'value' on stack */
  43031. } else {
  43032. duk_pop(ctx);
  43033. }
  43034. if (duk_get_prop_stridx(ctx, idx_in, DUK_STRIDX_WRITABLE)) {
  43035. is_data_desc = 1;
  43036. if (duk_to_boolean(ctx, -1)) {
  43037. defprop_flags |= DUK_DEFPROP_HAVE_WRITABLE | DUK_DEFPROP_WRITABLE;
  43038. } else {
  43039. defprop_flags |= DUK_DEFPROP_HAVE_WRITABLE;
  43040. }
  43041. }
  43042. duk_pop(ctx);
  43043. if (duk_get_prop_stridx(ctx, idx_in, DUK_STRIDX_GET)) {
  43044. duk_tval *tv = duk_require_tval(ctx, -1);
  43045. duk_hobject *h_get;
  43046. if (DUK_TVAL_IS_UNDEFINED(tv)) {
  43047. /* undefined is accepted */
  43048. DUK_ASSERT(getter == NULL);
  43049. } else {
  43050. /* NOTE: lightfuncs are coerced to full functions because
  43051. * lightfuncs don't fit into a property value slot. This
  43052. * has some side effects, see test-dev-lightfunc-accessor.js.
  43053. */
  43054. h_get = duk_get_hobject_or_lfunc_coerce(ctx, -1);
  43055. if (h_get == NULL || !DUK_HOBJECT_IS_CALLABLE(h_get)) {
  43056. goto type_error;
  43057. }
  43058. getter = h_get;
  43059. }
  43060. is_acc_desc = 1;
  43061. defprop_flags |= DUK_DEFPROP_HAVE_GETTER;
  43062. /* Leave 'getter' on stack */
  43063. } else {
  43064. duk_pop(ctx);
  43065. }
  43066. if (duk_get_prop_stridx(ctx, idx_in, DUK_STRIDX_SET)) {
  43067. duk_tval *tv = duk_require_tval(ctx, -1);
  43068. duk_hobject *h_set;
  43069. is_acc_desc = 1;
  43070. if (DUK_TVAL_IS_UNDEFINED(tv)) {
  43071. /* undefined is accepted */
  43072. DUK_ASSERT(setter == NULL);
  43073. } else {
  43074. /* NOTE: lightfuncs are coerced to full functions because
  43075. * lightfuncs don't fit into a property value slot. This
  43076. * has some side effects, see test-dev-lightfunc-accessor.js.
  43077. */
  43078. h_set = duk_get_hobject_or_lfunc_coerce(ctx, -1);
  43079. if (h_set == NULL || !DUK_HOBJECT_IS_CALLABLE(h_set)) {
  43080. goto type_error;
  43081. }
  43082. setter = h_set;
  43083. }
  43084. is_acc_desc = 1;
  43085. defprop_flags |= DUK_DEFPROP_HAVE_SETTER;
  43086. /* Leave 'setter' on stack */
  43087. } else {
  43088. duk_pop(ctx);
  43089. }
  43090. if (duk_get_prop_stridx(ctx, idx_in, DUK_STRIDX_ENUMERABLE)) {
  43091. if (duk_to_boolean(ctx, -1)) {
  43092. defprop_flags |= DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE;
  43093. } else {
  43094. defprop_flags |= DUK_DEFPROP_HAVE_ENUMERABLE;
  43095. }
  43096. }
  43097. duk_pop(ctx);
  43098. if (duk_get_prop_stridx(ctx, idx_in, DUK_STRIDX_CONFIGURABLE)) {
  43099. if (duk_to_boolean(ctx, -1)) {
  43100. defprop_flags |= DUK_DEFPROP_HAVE_CONFIGURABLE | DUK_DEFPROP_CONFIGURABLE;
  43101. } else {
  43102. defprop_flags |= DUK_DEFPROP_HAVE_CONFIGURABLE;
  43103. }
  43104. }
  43105. duk_pop(ctx);
  43106. if (is_data_desc && is_acc_desc) {
  43107. goto type_error;
  43108. }
  43109. *out_defprop_flags = defprop_flags;
  43110. *out_idx_value = idx_value;
  43111. *out_getter = getter;
  43112. *out_setter = setter;
  43113. /* [ ... value? getter? setter? ] */
  43114. return;
  43115. type_error:
  43116. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_DESCRIPTOR);
  43117. }
  43118. /*
  43119. * Object.defineProperty() related helper (E5 Section 15.2.3.6)
  43120. *
  43121. * Inlines all [[DefineOwnProperty]] exotic behaviors.
  43122. *
  43123. * Note: Ecmascript compliant [[DefineOwnProperty]](P, Desc, Throw) is not
  43124. * implemented directly, but Object.defineProperty() serves its purpose.
  43125. * We don't need the [[DefineOwnProperty]] internally and we don't have a
  43126. * property descriptor with 'missing values' so it's easier to avoid it
  43127. * entirely.
  43128. *
  43129. * Note: this is only called for actual objects, not primitive values.
  43130. * This must support virtual properties for full objects (e.g. Strings)
  43131. * but not for plain values (e.g. strings). Lightfuncs, even though
  43132. * primitive in a sense, are treated like objects and accepted as target
  43133. * values.
  43134. */
  43135. /* XXX: this is a major target for size optimization */
  43136. DUK_INTERNAL
  43137. void duk_hobject_define_property_helper(duk_context *ctx,
  43138. duk_uint_t defprop_flags,
  43139. duk_hobject *obj,
  43140. duk_hstring *key,
  43141. duk_idx_t idx_value,
  43142. duk_hobject *get,
  43143. duk_hobject *set) {
  43144. duk_hthread *thr = (duk_hthread *) ctx;
  43145. duk_uint32_t arr_idx;
  43146. duk_tval tv;
  43147. duk_bool_t has_enumerable;
  43148. duk_bool_t has_configurable;
  43149. duk_bool_t has_writable;
  43150. duk_bool_t has_value;
  43151. duk_bool_t has_get;
  43152. duk_bool_t has_set;
  43153. duk_bool_t is_enumerable;
  43154. duk_bool_t is_configurable;
  43155. duk_bool_t is_writable;
  43156. duk_bool_t throw_flag;
  43157. duk_bool_t force_flag;
  43158. duk_small_uint_t new_flags;
  43159. duk_propdesc curr;
  43160. duk_uint32_t arridx_new_array_length; /* != 0 => post-update for array 'length' (used when key is an array index) */
  43161. duk_uint32_t arrlen_old_len;
  43162. duk_uint32_t arrlen_new_len;
  43163. duk_bool_t pending_write_protect;
  43164. DUK_ASSERT(thr != NULL);
  43165. DUK_ASSERT(thr->heap != NULL);
  43166. DUK_ASSERT(ctx != NULL);
  43167. DUK_ASSERT(obj != NULL);
  43168. DUK_ASSERT(key != NULL);
  43169. /* idx_value may be < 0 (no value), set and get may be NULL */
  43170. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  43171. /* All the flags fit in 16 bits, so will fit into duk_bool_t. */
  43172. has_writable = (defprop_flags & DUK_DEFPROP_HAVE_WRITABLE);
  43173. has_enumerable = (defprop_flags & DUK_DEFPROP_HAVE_ENUMERABLE);
  43174. has_configurable = (defprop_flags & DUK_DEFPROP_HAVE_CONFIGURABLE);
  43175. has_value = (defprop_flags & DUK_DEFPROP_HAVE_VALUE);
  43176. has_get = (defprop_flags & DUK_DEFPROP_HAVE_GETTER);
  43177. has_set = (defprop_flags & DUK_DEFPROP_HAVE_SETTER);
  43178. is_writable = (defprop_flags & DUK_DEFPROP_WRITABLE);
  43179. is_enumerable = (defprop_flags & DUK_DEFPROP_ENUMERABLE);
  43180. is_configurable = (defprop_flags & DUK_DEFPROP_CONFIGURABLE);
  43181. throw_flag = 1; /* Object.defineProperty() calls [[DefineOwnProperty]] with Throw=true */
  43182. force_flag = (defprop_flags & DUK_DEFPROP_FORCE);
  43183. arr_idx = DUK_HSTRING_GET_ARRIDX_SLOW(key);
  43184. arridx_new_array_length = 0;
  43185. pending_write_protect = 0;
  43186. arrlen_old_len = 0;
  43187. arrlen_new_len = 0;
  43188. DUK_DDD(DUK_DDDPRINT("has_enumerable=%ld is_enumerable=%ld "
  43189. "has_configurable=%ld is_configurable=%ld "
  43190. "has_writable=%ld is_writable=%ld "
  43191. "has_value=%ld value=%!T "
  43192. "has_get=%ld get=%p=%!O "
  43193. "has_set=%ld set=%p=%!O "
  43194. "arr_idx=%ld",
  43195. (long) has_enumerable, (long) is_enumerable,
  43196. (long) has_configurable, (long) is_configurable,
  43197. (long) has_writable, (long) is_writable,
  43198. (long) has_value, (duk_tval *) (idx_value >= 0 ? duk_get_tval(ctx, idx_value) : NULL),
  43199. (long) has_get, (void *) get, (duk_heaphdr *) get,
  43200. (long) has_set, (void *) set, (duk_heaphdr *) set,
  43201. (long) arr_idx));
  43202. /*
  43203. * Array exotic behaviors can be implemented at this point. The local variables
  43204. * are essentially a 'value copy' of the input descriptor (Desc), which is modified
  43205. * by the Array [[DefineOwnProperty]] (E5 Section 15.4.5.1).
  43206. */
  43207. if (!DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj)) {
  43208. goto skip_array_exotic;
  43209. }
  43210. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  43211. /* E5 Section 15.4.5.1, step 3, steps a - i are implemented here, j - n at the end */
  43212. if (!has_value) {
  43213. DUK_DDD(DUK_DDDPRINT("exotic array behavior for 'length', but no value in descriptor -> normal behavior"));
  43214. goto skip_array_exotic;
  43215. }
  43216. DUK_DDD(DUK_DDDPRINT("exotic array behavior for 'length', value present in descriptor -> exotic behavior"));
  43217. /*
  43218. * Get old and new length
  43219. */
  43220. /* Note: reuse 'curr' as a temp propdesc */
  43221. arrlen_old_len = duk__get_old_array_length(thr, obj, &curr);
  43222. duk_dup(ctx, idx_value);
  43223. arrlen_new_len = duk__to_new_array_length_checked(thr);
  43224. duk_push_u32(ctx, arrlen_new_len);
  43225. duk_replace(ctx, idx_value); /* step 3.e: replace 'Desc.[[Value]]' */
  43226. DUK_DDD(DUK_DDDPRINT("old_len=%ld, new_len=%ld", (long) arrlen_old_len, (long) arrlen_new_len));
  43227. if (arrlen_new_len >= arrlen_old_len) {
  43228. /* standard behavior, step 3.f.i */
  43229. DUK_DDD(DUK_DDDPRINT("new length is same or higher as previous => standard behavior"));
  43230. goto skip_array_exotic;
  43231. }
  43232. DUK_DDD(DUK_DDDPRINT("new length is smaller than previous => exotic post behavior"));
  43233. /* XXX: consolidated algorithm step 15.f -> redundant? */
  43234. if (!(curr.flags & DUK_PROPDESC_FLAG_WRITABLE) && !force_flag) {
  43235. /* Note: 'curr' refers to 'length' propdesc */
  43236. goto fail_not_writable_array_length;
  43237. }
  43238. /* steps 3.h and 3.i */
  43239. if (has_writable && !is_writable) {
  43240. DUK_DDD(DUK_DDDPRINT("desc writable is false, force it back to true, and flag pending write protect"));
  43241. is_writable = 1;
  43242. pending_write_protect = 1;
  43243. }
  43244. /* remaining actual steps are carried out if standard DefineOwnProperty succeeds */
  43245. } else if (arr_idx != DUK__NO_ARRAY_INDEX) {
  43246. /* XXX: any chance of unifying this with the 'length' key handling? */
  43247. /* E5 Section 15.4.5.1, step 4 */
  43248. duk_uint32_t old_len;
  43249. /* Note: use 'curr' as a temp propdesc */
  43250. old_len = duk__get_old_array_length(thr, obj, &curr);
  43251. if (arr_idx >= old_len) {
  43252. DUK_DDD(DUK_DDDPRINT("defineProperty requires array length update "
  43253. "(arr_idx=%ld, old_len=%ld)",
  43254. (long) arr_idx, (long) old_len));
  43255. if (!(curr.flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  43256. /* Note: 'curr' refers to 'length' propdesc */
  43257. goto fail_not_writable_array_length;
  43258. }
  43259. /* actual update happens once write has been completed without
  43260. * error below.
  43261. */
  43262. DUK_ASSERT(arr_idx != 0xffffffffUL);
  43263. arridx_new_array_length = arr_idx + 1;
  43264. } else {
  43265. DUK_DDD(DUK_DDDPRINT("defineProperty does not require length update "
  43266. "(arr_idx=%ld, old_len=%ld) -> standard behavior",
  43267. (long) arr_idx, (long) old_len));
  43268. }
  43269. }
  43270. skip_array_exotic:
  43271. /* XXX: There is currently no support for writing buffer object
  43272. * indexed elements here. Attempt to do so will succeed and
  43273. * write a concrete property into the buffer object. This should
  43274. * be fixed at some point but because buffers are a custom feature
  43275. * anyway, this is relatively unimportant.
  43276. */
  43277. /*
  43278. * Actual Object.defineProperty() default algorithm.
  43279. */
  43280. /*
  43281. * First check whether property exists; if not, simple case. This covers
  43282. * steps 1-4.
  43283. */
  43284. if (!duk__get_own_property_desc_raw(thr, obj, key, arr_idx, &curr, DUK__DESC_FLAG_PUSH_VALUE)) {
  43285. DUK_DDD(DUK_DDDPRINT("property does not exist"));
  43286. if (!DUK_HOBJECT_HAS_EXTENSIBLE(obj) && !force_flag) {
  43287. goto fail_not_extensible;
  43288. }
  43289. /* XXX: share final setting code for value and flags? difficult because
  43290. * refcount code is different. Share entry allocation? But can't allocate
  43291. * until array index checked.
  43292. */
  43293. /* steps 4.a and 4.b are tricky */
  43294. if (has_set || has_get) {
  43295. duk_int_t e_idx;
  43296. DUK_DDD(DUK_DDDPRINT("create new accessor property"));
  43297. DUK_ASSERT(has_set || set == NULL);
  43298. DUK_ASSERT(has_get || get == NULL);
  43299. DUK_ASSERT(!has_value);
  43300. DUK_ASSERT(!has_writable);
  43301. new_flags = DUK_PROPDESC_FLAG_ACCESSOR; /* defaults, E5 Section 8.6.1, Table 7 */
  43302. if (has_enumerable && is_enumerable) {
  43303. new_flags |= DUK_PROPDESC_FLAG_ENUMERABLE;
  43304. }
  43305. if (has_configurable && is_configurable) {
  43306. new_flags |= DUK_PROPDESC_FLAG_CONFIGURABLE;
  43307. }
  43308. if (arr_idx != DUK__NO_ARRAY_INDEX && DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  43309. DUK_DDD(DUK_DDDPRINT("accessor cannot go to array part, abandon array"));
  43310. duk__abandon_array_checked(thr, obj);
  43311. }
  43312. /* write to entry part */
  43313. e_idx = duk__alloc_entry_checked(thr, obj, key);
  43314. DUK_ASSERT(e_idx >= 0);
  43315. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, e_idx, get);
  43316. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, e_idx, set);
  43317. DUK_HOBJECT_INCREF_ALLOWNULL(thr, get);
  43318. DUK_HOBJECT_INCREF_ALLOWNULL(thr, set);
  43319. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, e_idx, new_flags);
  43320. goto success_exotics;
  43321. } else {
  43322. duk_int_t e_idx;
  43323. duk_tval *tv2;
  43324. DUK_DDD(DUK_DDDPRINT("create new data property"));
  43325. DUK_ASSERT(!has_set);
  43326. DUK_ASSERT(!has_get);
  43327. new_flags = 0; /* defaults, E5 Section 8.6.1, Table 7 */
  43328. if (has_writable && is_writable) {
  43329. new_flags |= DUK_PROPDESC_FLAG_WRITABLE;
  43330. }
  43331. if (has_enumerable && is_enumerable) {
  43332. new_flags |= DUK_PROPDESC_FLAG_ENUMERABLE;
  43333. }
  43334. if (has_configurable && is_configurable) {
  43335. new_flags |= DUK_PROPDESC_FLAG_CONFIGURABLE;
  43336. }
  43337. if (has_value) {
  43338. duk_tval *tv_tmp = duk_require_tval(ctx, idx_value);
  43339. DUK_TVAL_SET_TVAL(&tv, tv_tmp);
  43340. } else {
  43341. DUK_TVAL_SET_UNDEFINED_ACTUAL(&tv); /* default value */
  43342. }
  43343. if (arr_idx != DUK__NO_ARRAY_INDEX && DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  43344. if (new_flags == DUK_PROPDESC_FLAGS_WEC) {
  43345. #if 0
  43346. DUK_DDD(DUK_DDDPRINT("new data property attributes match array defaults, attempt to write to array part"));
  43347. /* may become sparse...*/
  43348. #endif
  43349. /* XXX: handling for array part missing now; this doesn't affect
  43350. * compliance but causes array entry writes using defineProperty()
  43351. * to always abandon array part.
  43352. */
  43353. }
  43354. DUK_DDD(DUK_DDDPRINT("new data property cannot go to array part, abandon array"));
  43355. duk__abandon_array_checked(thr, obj);
  43356. /* fall through */
  43357. }
  43358. /* write to entry part */
  43359. e_idx = duk__alloc_entry_checked(thr, obj, key);
  43360. DUK_ASSERT(e_idx >= 0);
  43361. tv2 = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, e_idx);
  43362. DUK_TVAL_SET_TVAL(tv2, &tv);
  43363. DUK_TVAL_INCREF(thr, tv2);
  43364. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, e_idx, new_flags);
  43365. goto success_exotics;
  43366. }
  43367. DUK_UNREACHABLE();
  43368. }
  43369. /* we currently assume virtual properties are not configurable (as none of them are) */
  43370. DUK_ASSERT((curr.e_idx >= 0 || curr.a_idx >= 0) || !(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE));
  43371. /* [obj key desc value get set curr_value] */
  43372. /*
  43373. * Property already exists. Steps 5-6 detect whether any changes need
  43374. * to be made.
  43375. */
  43376. if (has_enumerable) {
  43377. if (is_enumerable) {
  43378. if (!(curr.flags & DUK_PROPDESC_FLAG_ENUMERABLE)) {
  43379. goto need_check;
  43380. }
  43381. } else {
  43382. if (curr.flags & DUK_PROPDESC_FLAG_ENUMERABLE) {
  43383. goto need_check;
  43384. }
  43385. }
  43386. }
  43387. if (has_configurable) {
  43388. if (is_configurable) {
  43389. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE)) {
  43390. goto need_check;
  43391. }
  43392. } else {
  43393. if (curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) {
  43394. goto need_check;
  43395. }
  43396. }
  43397. }
  43398. if (has_value) {
  43399. duk_tval *tmp1;
  43400. duk_tval *tmp2;
  43401. /* attempt to change from accessor to data property */
  43402. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  43403. goto need_check;
  43404. }
  43405. tmp1 = duk_require_tval(ctx, -1); /* curr value */
  43406. tmp2 = duk_require_tval(ctx, idx_value); /* new value */
  43407. if (!duk_js_samevalue(tmp1, tmp2)) {
  43408. goto need_check;
  43409. }
  43410. }
  43411. if (has_writable) {
  43412. /* attempt to change from accessor to data property */
  43413. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  43414. goto need_check;
  43415. }
  43416. if (is_writable) {
  43417. if (!(curr.flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  43418. goto need_check;
  43419. }
  43420. } else {
  43421. if (curr.flags & DUK_PROPDESC_FLAG_WRITABLE) {
  43422. goto need_check;
  43423. }
  43424. }
  43425. }
  43426. if (has_set) {
  43427. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  43428. if (set != curr.set) {
  43429. goto need_check;
  43430. }
  43431. } else {
  43432. goto need_check;
  43433. }
  43434. }
  43435. if (has_get) {
  43436. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  43437. if (get != curr.get) {
  43438. goto need_check;
  43439. }
  43440. } else {
  43441. goto need_check;
  43442. }
  43443. }
  43444. /* property exists, either 'desc' is empty, or all values
  43445. * match (SameValue)
  43446. */
  43447. goto success_no_exotics;
  43448. need_check:
  43449. /*
  43450. * Some change(s) need to be made. Steps 7-11.
  43451. */
  43452. /* shared checks for all descriptor types */
  43453. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && !force_flag) {
  43454. if (has_configurable && is_configurable) {
  43455. goto fail_not_configurable;
  43456. }
  43457. if (has_enumerable) {
  43458. if (curr.flags & DUK_PROPDESC_FLAG_ENUMERABLE) {
  43459. if (!is_enumerable) {
  43460. goto fail_not_configurable;
  43461. }
  43462. } else {
  43463. if (is_enumerable) {
  43464. goto fail_not_configurable;
  43465. }
  43466. }
  43467. }
  43468. }
  43469. /* Reject attempt to change virtual properties: not part of the
  43470. * standard algorithm, applies currently to e.g. virtual index
  43471. * properties of buffer objects (which are virtual but writable).
  43472. * (Cannot "force" modification of a virtual property.)
  43473. */
  43474. if (curr.flags & DUK_PROPDESC_FLAG_VIRTUAL) {
  43475. goto fail_virtual;
  43476. }
  43477. /* descriptor type specific checks */
  43478. if (has_set || has_get) {
  43479. /* IsAccessorDescriptor(desc) == true */
  43480. DUK_ASSERT(!has_writable);
  43481. DUK_ASSERT(!has_value);
  43482. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  43483. /* curr and desc are accessors */
  43484. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && !force_flag) {
  43485. if (has_set && set != curr.set) {
  43486. goto fail_not_configurable;
  43487. }
  43488. if (has_get && get != curr.get) {
  43489. goto fail_not_configurable;
  43490. }
  43491. }
  43492. } else {
  43493. duk_bool_t rc;
  43494. duk_tval tv_tmp;
  43495. duk_tval *tv1;
  43496. /* curr is data, desc is accessor */
  43497. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && !force_flag) {
  43498. goto fail_not_configurable;
  43499. }
  43500. DUK_DDD(DUK_DDDPRINT("convert property to accessor property"));
  43501. if (curr.a_idx >= 0) {
  43502. DUK_DDD(DUK_DDDPRINT("property to convert is stored in an array entry, abandon array and re-lookup"));
  43503. duk__abandon_array_checked(thr, obj);
  43504. duk_pop(ctx); /* remove old value */
  43505. rc = duk__get_own_property_desc_raw(thr, obj, key, arr_idx, &curr, DUK__DESC_FLAG_PUSH_VALUE);
  43506. DUK_UNREF(rc);
  43507. DUK_ASSERT(rc != 0);
  43508. DUK_ASSERT(curr.e_idx >= 0 && curr.a_idx < 0);
  43509. }
  43510. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  43511. tv1 = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, curr.e_idx);
  43512. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  43513. DUK_TVAL_SET_UNDEFINED_UNUSED(tv1);
  43514. DUK_TVAL_DECREF(thr, &tv_tmp);
  43515. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, curr.e_idx, NULL);
  43516. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, curr.e_idx, NULL);
  43517. DUK_HOBJECT_E_SLOT_CLEAR_WRITABLE(thr->heap, obj, curr.e_idx);
  43518. DUK_HOBJECT_E_SLOT_SET_ACCESSOR(thr->heap, obj, curr.e_idx);
  43519. DUK_DDD(DUK_DDDPRINT("flags after data->accessor conversion: 0x%02lx",
  43520. (unsigned long) DUK_HOBJECT_E_GET_FLAGS(thr->heap, obj, curr.e_idx)));
  43521. /* re-lookup to update curr.flags
  43522. * XXX: would be faster to update directly
  43523. */
  43524. duk_pop(ctx); /* remove old value */
  43525. rc = duk__get_own_property_desc_raw(thr, obj, key, arr_idx, &curr, DUK__DESC_FLAG_PUSH_VALUE);
  43526. DUK_UNREF(rc);
  43527. DUK_ASSERT(rc != 0);
  43528. }
  43529. } else if (has_value || has_writable) {
  43530. /* IsDataDescriptor(desc) == true */
  43531. DUK_ASSERT(!has_set);
  43532. DUK_ASSERT(!has_get);
  43533. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  43534. duk_bool_t rc;
  43535. duk_hobject *tmp;
  43536. /* curr is accessor, desc is data */
  43537. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && !force_flag) {
  43538. goto fail_not_configurable;
  43539. }
  43540. /* curr is accessor -> cannot be in array part */
  43541. DUK_ASSERT(curr.e_idx >= 0 && curr.a_idx < 0);
  43542. DUK_DDD(DUK_DDDPRINT("convert property to data property"));
  43543. DUK_ASSERT(DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  43544. tmp = DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, obj, curr.e_idx);
  43545. DUK_UNREF(tmp);
  43546. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, curr.e_idx, NULL);
  43547. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  43548. tmp = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, obj, curr.e_idx);
  43549. DUK_UNREF(tmp);
  43550. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, curr.e_idx, NULL);
  43551. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  43552. DUK_TVAL_SET_UNDEFINED_ACTUAL(DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, curr.e_idx));
  43553. DUK_HOBJECT_E_SLOT_CLEAR_WRITABLE(thr->heap, obj, curr.e_idx);
  43554. DUK_HOBJECT_E_SLOT_CLEAR_ACCESSOR(thr->heap, obj, curr.e_idx);
  43555. DUK_DDD(DUK_DDDPRINT("flags after accessor->data conversion: 0x%02lx",
  43556. (unsigned long) DUK_HOBJECT_E_GET_FLAGS(thr->heap, obj, curr.e_idx)));
  43557. /* re-lookup to update curr.flags
  43558. * XXX: would be faster to update directly
  43559. */
  43560. duk_pop(ctx); /* remove old value */
  43561. rc = duk__get_own_property_desc_raw(thr, obj, key, arr_idx, &curr, DUK__DESC_FLAG_PUSH_VALUE);
  43562. DUK_UNREF(rc);
  43563. DUK_ASSERT(rc != 0);
  43564. } else {
  43565. /* curr and desc are data */
  43566. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && !force_flag) {
  43567. if (!(curr.flags & DUK_PROPDESC_FLAG_WRITABLE) && has_writable && is_writable) {
  43568. goto fail_not_configurable;
  43569. }
  43570. /* Note: changing from writable to non-writable is OK */
  43571. if (!(curr.flags & DUK_PROPDESC_FLAG_WRITABLE) && has_value) {
  43572. duk_tval *tmp1 = duk_require_tval(ctx, -1); /* curr value */
  43573. duk_tval *tmp2 = duk_require_tval(ctx, idx_value); /* new value */
  43574. if (!duk_js_samevalue(tmp1, tmp2)) {
  43575. goto fail_not_configurable;
  43576. }
  43577. }
  43578. }
  43579. }
  43580. } else {
  43581. /* IsGenericDescriptor(desc) == true; this means in practice that 'desc'
  43582. * only has [[Enumerable]] or [[Configurable]] flag updates, which are
  43583. * allowed at this point.
  43584. */
  43585. DUK_ASSERT(!has_value && !has_writable && !has_get && !has_set);
  43586. }
  43587. /*
  43588. * Start doing property attributes updates. Steps 12-13.
  43589. *
  43590. * Start by computing new attribute flags without writing yet.
  43591. * Property type conversion is done above if necessary.
  43592. */
  43593. new_flags = curr.flags;
  43594. if (has_enumerable) {
  43595. if (is_enumerable) {
  43596. new_flags |= DUK_PROPDESC_FLAG_ENUMERABLE;
  43597. } else {
  43598. new_flags &= ~DUK_PROPDESC_FLAG_ENUMERABLE;
  43599. }
  43600. }
  43601. if (has_configurable) {
  43602. if (is_configurable) {
  43603. new_flags |= DUK_PROPDESC_FLAG_CONFIGURABLE;
  43604. } else {
  43605. new_flags &= ~DUK_PROPDESC_FLAG_CONFIGURABLE;
  43606. }
  43607. }
  43608. if (has_writable) {
  43609. if (is_writable) {
  43610. new_flags |= DUK_PROPDESC_FLAG_WRITABLE;
  43611. } else {
  43612. new_flags &= ~DUK_PROPDESC_FLAG_WRITABLE;
  43613. }
  43614. }
  43615. /* XXX: write protect after flag? -> any chance of handling it here? */
  43616. DUK_DDD(DUK_DDDPRINT("new flags that we want to write: 0x%02lx",
  43617. (unsigned long) new_flags));
  43618. /*
  43619. * Check whether we need to abandon an array part (if it exists)
  43620. */
  43621. if (curr.a_idx >= 0) {
  43622. duk_bool_t rc;
  43623. DUK_ASSERT(curr.e_idx < 0);
  43624. if (new_flags == DUK_PROPDESC_FLAGS_WEC) {
  43625. duk_tval *tv1, *tv2;
  43626. duk_tval tv_tmp;
  43627. DUK_DDD(DUK_DDDPRINT("array index, new property attributes match array defaults, update in-place"));
  43628. DUK_ASSERT(curr.flags == DUK_PROPDESC_FLAGS_WEC); /* must have been, since in array part */
  43629. DUK_ASSERT(!has_set);
  43630. DUK_ASSERT(!has_get);
  43631. tv2 = duk_require_tval(ctx, idx_value);
  43632. tv1 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, curr.a_idx);
  43633. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  43634. DUK_TVAL_SET_TVAL(tv1, tv2);
  43635. DUK_TVAL_INCREF(thr, tv1);
  43636. DUK_TVAL_DECREF(thr, &tv_tmp);
  43637. goto success_exotics;
  43638. }
  43639. DUK_DDD(DUK_DDDPRINT("array index, new property attributes do not match array defaults, abandon array and re-lookup"));
  43640. duk__abandon_array_checked(thr, obj);
  43641. duk_pop(ctx); /* remove old value */
  43642. rc = duk__get_own_property_desc_raw(thr, obj, key, arr_idx, &curr, DUK__DESC_FLAG_PUSH_VALUE);
  43643. DUK_UNREF(rc);
  43644. DUK_ASSERT(rc != 0);
  43645. DUK_ASSERT(curr.e_idx >= 0 && curr.a_idx < 0);
  43646. }
  43647. DUK_DDD(DUK_DDDPRINT("updating existing property in entry part"));
  43648. /* array case is handled comprehensively above */
  43649. DUK_ASSERT(curr.e_idx >= 0 && curr.a_idx < 0);
  43650. DUK_DDD(DUK_DDDPRINT("update existing property attributes"));
  43651. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, curr.e_idx, new_flags);
  43652. if (has_set) {
  43653. duk_hobject *tmp;
  43654. DUK_DDD(DUK_DDDPRINT("update existing property setter"));
  43655. DUK_ASSERT(DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  43656. tmp = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, obj, curr.e_idx);
  43657. DUK_UNREF(tmp);
  43658. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, curr.e_idx, set);
  43659. DUK_HOBJECT_INCREF_ALLOWNULL(thr, set);
  43660. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  43661. }
  43662. if (has_get) {
  43663. duk_hobject *tmp;
  43664. DUK_DDD(DUK_DDDPRINT("update existing property getter"));
  43665. DUK_ASSERT(DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  43666. tmp = DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, obj, curr.e_idx);
  43667. DUK_UNREF(tmp);
  43668. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, curr.e_idx, get);
  43669. DUK_HOBJECT_INCREF_ALLOWNULL(thr, get);
  43670. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  43671. }
  43672. if (has_value) {
  43673. duk_tval *tv1, *tv2;
  43674. duk_tval tv_tmp;
  43675. DUK_DDD(DUK_DDDPRINT("update existing property value"));
  43676. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  43677. tv2 = duk_require_tval(ctx, idx_value);
  43678. tv1 = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, curr.e_idx);
  43679. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  43680. DUK_TVAL_SET_TVAL(tv1, tv2);
  43681. DUK_TVAL_INCREF(thr, tv1);
  43682. DUK_TVAL_DECREF(thr, &tv_tmp);
  43683. }
  43684. /*
  43685. * Standard algorithm succeeded without errors, check for exotic post-behaviors.
  43686. *
  43687. * Arguments exotic behavior in E5 Section 10.6 occurs after the standard
  43688. * [[DefineOwnProperty]] has completed successfully.
  43689. *
  43690. * Array exotic behavior in E5 Section 15.4.5.1 is implemented partly
  43691. * prior to the default [[DefineOwnProperty]], but:
  43692. * - for an array index key (e.g. "10") the final 'length' update occurs here
  43693. * - for 'length' key the element deletion and 'length' update occurs here
  43694. */
  43695. success_exotics:
  43696. /* [obj key desc value get set curr_value] */
  43697. if (DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj)) {
  43698. if (arridx_new_array_length > 0) {
  43699. duk_tval *tmp;
  43700. duk_bool_t rc;
  43701. /*
  43702. * Note: zero works as a "no update" marker because the new length
  43703. * can never be zero after a new property is written.
  43704. */
  43705. /* E5 Section 15.4.5.1, steps 4.e.i - 4.e.ii */
  43706. DUK_DDD(DUK_DDDPRINT("defineProperty successful, pending array length update to: %ld",
  43707. (long) arridx_new_array_length));
  43708. /* Note: reuse 'curr' */
  43709. 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 */
  43710. DUK_UNREF(rc);
  43711. DUK_ASSERT(rc != 0);
  43712. DUK_ASSERT(curr.e_idx >= 0);
  43713. tmp = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, curr.e_idx);
  43714. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tmp));
  43715. /* no need for decref/incref because value is a number */
  43716. #if defined(DUK_USE_FASTINT)
  43717. DUK_TVAL_SET_FASTINT_U32(tmp, arridx_new_array_length);
  43718. #else
  43719. DUK_TVAL_SET_NUMBER(tmp, (duk_double_t) arridx_new_array_length);
  43720. #endif
  43721. }
  43722. if (key == DUK_HTHREAD_STRING_LENGTH(thr) && arrlen_new_len < arrlen_old_len) {
  43723. /*
  43724. * E5 Section 15.4.5.1, steps 3.k - 3.n. The order at the end combines
  43725. * the error case 3.l.iii and the success case 3.m-3.n.
  43726. *
  43727. * Note: 'length' is always in entries part, so no array abandon issues for
  43728. * 'writable' update.
  43729. */
  43730. /* XXX: investigate whether write protect can be handled above, if we
  43731. * just update length here while ignoring its protected status
  43732. */
  43733. duk_tval *tmp;
  43734. duk_uint32_t result_len;
  43735. duk_bool_t rc;
  43736. DUK_DDD(DUK_DDDPRINT("defineProperty successful, key is 'length', exotic array behavior, "
  43737. "doing array element deletion and length update"));
  43738. rc = duk__handle_put_array_length_smaller(thr, obj, arrlen_old_len, arrlen_new_len, force_flag, &result_len);
  43739. /* update length (curr points to length, and we assume it's still valid) */
  43740. DUK_ASSERT(result_len >= arrlen_new_len && result_len <= arrlen_old_len);
  43741. DUK_ASSERT(curr.e_idx >= 0);
  43742. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  43743. tmp = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, curr.e_idx);
  43744. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tmp));
  43745. /* no decref needed for a number */
  43746. #if defined(DUK_USE_FASTINT)
  43747. DUK_TVAL_SET_FASTINT_U32(tmp, result_len);
  43748. #else
  43749. DUK_TVAL_SET_NUMBER(tmp, (duk_double_t) result_len);
  43750. #endif
  43751. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tmp));
  43752. if (pending_write_protect) {
  43753. DUK_DDD(DUK_DDDPRINT("setting array length non-writable (pending writability update)"));
  43754. DUK_HOBJECT_E_SLOT_CLEAR_WRITABLE(thr->heap, obj, curr.e_idx);
  43755. }
  43756. /*
  43757. * XXX: shrink array allocation or entries compaction here?
  43758. */
  43759. if (!rc) {
  43760. goto fail_array_length_partial;
  43761. }
  43762. }
  43763. } else if (arr_idx != DUK__NO_ARRAY_INDEX && DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj)) {
  43764. duk_hobject *map;
  43765. duk_hobject *varenv;
  43766. DUK_ASSERT(arridx_new_array_length == 0);
  43767. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj)); /* traits are separate; in particular, arguments not an array */
  43768. map = NULL;
  43769. varenv = NULL;
  43770. if (!duk__lookup_arguments_map(thr, obj, key, &curr, &map, &varenv)) {
  43771. goto success_no_exotics;
  43772. }
  43773. DUK_ASSERT(map != NULL);
  43774. DUK_ASSERT(varenv != NULL);
  43775. /* [obj key desc value get set curr_value varname] */
  43776. if (has_set || has_get) {
  43777. /* = IsAccessorDescriptor(Desc) */
  43778. DUK_DDD(DUK_DDDPRINT("defineProperty successful, key mapped to arguments 'map' "
  43779. "changed to an accessor, delete arguments binding"));
  43780. (void) duk_hobject_delprop_raw(thr, map, key, 0); /* ignore result */
  43781. } else {
  43782. /* Note: this order matters (final value before deleting map entry must be done) */
  43783. DUK_DDD(DUK_DDDPRINT("defineProperty successful, key mapped to arguments 'map', "
  43784. "check for value update / binding deletion"));
  43785. if (has_value) {
  43786. duk_hstring *varname;
  43787. DUK_DDD(DUK_DDDPRINT("defineProperty successful, key mapped to arguments 'map', "
  43788. "update bound value (variable/argument)"));
  43789. varname = duk_require_hstring(ctx, -1);
  43790. DUK_ASSERT(varname != NULL);
  43791. DUK_DDD(DUK_DDDPRINT("arguments object automatic putvar for a bound variable; "
  43792. "key=%!O, varname=%!O, value=%!T",
  43793. (duk_heaphdr *) key,
  43794. (duk_heaphdr *) varname,
  43795. (duk_tval *) duk_require_tval(ctx, idx_value)));
  43796. /* strict flag for putvar comes from our caller (currently: fixed) */
  43797. duk_js_putvar_envrec(thr, varenv, varname, duk_require_tval(ctx, idx_value), throw_flag);
  43798. }
  43799. if (has_writable && !is_writable) {
  43800. DUK_DDD(DUK_DDDPRINT("defineProperty successful, key mapped to arguments 'map', "
  43801. "changed to non-writable, delete arguments binding"));
  43802. (void) duk_hobject_delprop_raw(thr, map, key, 0); /* ignore result */
  43803. }
  43804. }
  43805. /* 'varname' is in stack in this else branch, leaving an unbalanced stack below,
  43806. * but this doesn't matter now.
  43807. */
  43808. }
  43809. success_no_exotics:
  43810. return;
  43811. fail_virtual:
  43812. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_PROPERTY_IS_VIRTUAL);
  43813. return;
  43814. fail_not_writable_array_length:
  43815. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_ARRAY_LENGTH_NOT_WRITABLE);
  43816. return;
  43817. fail_not_extensible:
  43818. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_EXTENSIBLE);
  43819. return;
  43820. fail_not_configurable:
  43821. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_CONFIGURABLE);
  43822. return;
  43823. fail_array_length_partial:
  43824. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_ARRAY_LENGTH_WRITE_FAILED);
  43825. return;
  43826. }
  43827. /*
  43828. * Object.prototype.hasOwnProperty() and Object.prototype.propertyIsEnumerable().
  43829. */
  43830. DUK_INTERNAL duk_bool_t duk_hobject_object_ownprop_helper(duk_context *ctx, duk_small_uint_t required_desc_flags) {
  43831. duk_hthread *thr = (duk_hthread *) ctx;
  43832. duk_hstring *h_v;
  43833. duk_hobject *h_obj;
  43834. duk_propdesc desc;
  43835. duk_bool_t ret;
  43836. /* coercion order matters */
  43837. h_v = duk_to_hstring(ctx, 0);
  43838. DUK_ASSERT(h_v != NULL);
  43839. h_obj = duk_push_this_coercible_to_object(ctx);
  43840. DUK_ASSERT(h_obj != NULL);
  43841. ret = duk__get_own_property_desc(thr, h_obj, h_v, &desc, 0 /*flags*/); /* don't push value */
  43842. duk_push_boolean(ctx, ret && ((desc.flags & required_desc_flags) == required_desc_flags));
  43843. return 1;
  43844. }
  43845. /*
  43846. * Object.seal() and Object.freeze() (E5 Sections 15.2.3.8 and 15.2.3.9)
  43847. *
  43848. * Since the algorithms are similar, a helper provides both functions.
  43849. * Freezing is essentially sealing + making plain properties non-writable.
  43850. *
  43851. * Note: virtual (non-concrete) properties which are non-configurable but
  43852. * writable would pose some problems, but such properties do not currently
  43853. * exist (all virtual properties are non-configurable and non-writable).
  43854. * If they did exist, the non-configurability does NOT prevent them from
  43855. * becoming non-writable. However, this change should be recorded somehow
  43856. * so that it would turn up (e.g. when getting the property descriptor),
  43857. * requiring some additional flags in the object.
  43858. */
  43859. DUK_INTERNAL void duk_hobject_object_seal_freeze_helper(duk_hthread *thr, duk_hobject *obj, duk_bool_t is_freeze) {
  43860. duk_uint_fast32_t i;
  43861. DUK_ASSERT(thr != NULL);
  43862. DUK_ASSERT(thr->heap != NULL);
  43863. DUK_ASSERT(obj != NULL);
  43864. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  43865. /*
  43866. * Abandon array part because all properties must become non-configurable.
  43867. * Note that this is now done regardless of whether this is always the case
  43868. * (skips check, but performance problem if caller would do this many times
  43869. * for the same object; not likely).
  43870. */
  43871. duk__abandon_array_checked(thr, obj);
  43872. DUK_ASSERT(DUK_HOBJECT_GET_ASIZE(obj) == 0);
  43873. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  43874. duk_uint8_t *fp;
  43875. /* since duk__abandon_array_checked() causes a resize, there should be no gaps in keys */
  43876. DUK_ASSERT(DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i) != NULL);
  43877. /* avoid multiple computations of flags address; bypasses macros */
  43878. fp = DUK_HOBJECT_E_GET_FLAGS_PTR(thr->heap, obj, i);
  43879. if (is_freeze && !((*fp) & DUK_PROPDESC_FLAG_ACCESSOR)) {
  43880. *fp &= ~(DUK_PROPDESC_FLAG_WRITABLE | DUK_PROPDESC_FLAG_CONFIGURABLE);
  43881. } else {
  43882. *fp &= ~DUK_PROPDESC_FLAG_CONFIGURABLE;
  43883. }
  43884. }
  43885. DUK_HOBJECT_CLEAR_EXTENSIBLE(obj);
  43886. /* no need to compact since we already did that in duk__abandon_array_checked()
  43887. * (regardless of whether an array part existed or not.
  43888. */
  43889. return;
  43890. }
  43891. /*
  43892. * Object.isSealed() and Object.isFrozen() (E5 Sections 15.2.3.11, 15.2.3.13)
  43893. *
  43894. * Since the algorithms are similar, a helper provides both functions.
  43895. * Freezing is essentially sealing + making plain properties non-writable.
  43896. *
  43897. * Note: all virtual (non-concrete) properties are currently non-configurable
  43898. * and non-writable (and there are no accessor virtual properties), so they don't
  43899. * need to be considered here now.
  43900. */
  43901. DUK_INTERNAL duk_bool_t duk_hobject_object_is_sealed_frozen_helper(duk_hthread *thr, duk_hobject *obj, duk_bool_t is_frozen) {
  43902. duk_uint_fast32_t i;
  43903. DUK_ASSERT(obj != NULL);
  43904. DUK_UNREF(thr);
  43905. /* Note: no allocation pressure, no need to check refcounts etc */
  43906. /* must not be extensible */
  43907. if (DUK_HOBJECT_HAS_EXTENSIBLE(obj)) {
  43908. return 0;
  43909. }
  43910. /* all virtual properties are non-configurable and non-writable */
  43911. /* entry part must not contain any configurable properties, or
  43912. * writable properties (if is_frozen).
  43913. */
  43914. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  43915. duk_small_uint_t flags;
  43916. if (!DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i)) {
  43917. continue;
  43918. }
  43919. /* avoid multiple computations of flags address; bypasses macros */
  43920. flags = (duk_small_uint_t) DUK_HOBJECT_E_GET_FLAGS(thr->heap, obj, i);
  43921. if (flags & DUK_PROPDESC_FLAG_CONFIGURABLE) {
  43922. return 0;
  43923. }
  43924. if (is_frozen &&
  43925. !(flags & DUK_PROPDESC_FLAG_ACCESSOR) &&
  43926. (flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  43927. return 0;
  43928. }
  43929. }
  43930. /* array part must not contain any non-unused properties, as they would
  43931. * be configurable and writable.
  43932. */
  43933. for (i = 0; i < DUK_HOBJECT_GET_ASIZE(obj); i++) {
  43934. duk_tval *tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, i);
  43935. if (!DUK_TVAL_IS_UNDEFINED_UNUSED(tv)) {
  43936. return 0;
  43937. }
  43938. }
  43939. return 1;
  43940. }
  43941. /*
  43942. * Object.preventExtensions() and Object.isExtensible() (E5 Sections 15.2.3.10, 15.2.3.13)
  43943. *
  43944. * Not needed, implemented by macros DUK_HOBJECT_{HAS,CLEAR,SET}_EXTENSIBLE
  43945. * and the Object built-in bindings.
  43946. */
  43947. /* Undefine local defines */
  43948. #undef DUK__NO_ARRAY_INDEX
  43949. #undef DUK__HASH_INITIAL
  43950. #undef DUK__HASH_PROBE_STEP
  43951. #undef DUK__HASH_UNUSED
  43952. #undef DUK__HASH_DELETED
  43953. #undef DUK__VALSTACK_SPACE
  43954. #line 1 "duk_hstring_misc.c"
  43955. /*
  43956. * Misc support functions
  43957. */
  43958. /* include removed: duk_internal.h */
  43959. DUK_INTERNAL duk_ucodepoint_t duk_hstring_char_code_at_raw(duk_hthread *thr, duk_hstring *h, duk_uint_t pos) {
  43960. duk_uint32_t boff;
  43961. const duk_uint8_t *p, *p_start, *p_end;
  43962. duk_ucodepoint_t cp;
  43963. /* Caller must check character offset to be inside the string. */
  43964. DUK_ASSERT(thr != NULL);
  43965. DUK_ASSERT(h != NULL);
  43966. DUK_ASSERT_DISABLE(pos >= 0); /* unsigned */
  43967. DUK_ASSERT(pos < (duk_uint_t) DUK_HSTRING_GET_CHARLEN(h));
  43968. boff = duk_heap_strcache_offset_char2byte(thr, h, (duk_uint32_t) pos);
  43969. DUK_DDD(DUK_DDDPRINT("charCodeAt: pos=%ld -> boff=%ld, str=%!O",
  43970. (long) pos, (long) boff, (duk_heaphdr *) h));
  43971. DUK_ASSERT_DISABLE(boff >= 0);
  43972. DUK_ASSERT(boff < DUK_HSTRING_GET_BYTELEN(h));
  43973. p_start = DUK_HSTRING_GET_DATA(h);
  43974. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h);
  43975. p = p_start + boff;
  43976. DUK_DDD(DUK_DDDPRINT("p_start=%p, p_end=%p, p=%p",
  43977. (void *) p_start, (void *) p_end, (void *) p));
  43978. /* This may throw an error though not for valid E5 strings. */
  43979. cp = duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end);
  43980. return cp;
  43981. }
  43982. #line 1 "duk_hthread_alloc.c"
  43983. /*
  43984. * duk_hthread allocation and freeing.
  43985. */
  43986. /* include removed: duk_internal.h */
  43987. /*
  43988. * Allocate initial stacks for a thread. Note that 'thr' must be reachable
  43989. * as a garbage collection may be triggered by the allocation attempts.
  43990. * Returns zero (without leaking memory) if init fails.
  43991. */
  43992. DUK_INTERNAL duk_bool_t duk_hthread_init_stacks(duk_heap *heap, duk_hthread *thr) {
  43993. duk_size_t alloc_size;
  43994. duk_size_t i;
  43995. DUK_ASSERT(heap != NULL);
  43996. DUK_ASSERT(thr != NULL);
  43997. DUK_ASSERT(thr->valstack == NULL);
  43998. DUK_ASSERT(thr->valstack_end == NULL);
  43999. DUK_ASSERT(thr->valstack_bottom == NULL);
  44000. DUK_ASSERT(thr->valstack_top == NULL);
  44001. DUK_ASSERT(thr->callstack == NULL);
  44002. DUK_ASSERT(thr->catchstack == NULL);
  44003. /* valstack */
  44004. alloc_size = sizeof(duk_tval) * DUK_VALSTACK_INITIAL_SIZE;
  44005. thr->valstack = (duk_tval *) DUK_ALLOC(heap, alloc_size);
  44006. if (!thr->valstack) {
  44007. goto fail;
  44008. }
  44009. DUK_MEMZERO(thr->valstack, alloc_size);
  44010. thr->valstack_end = thr->valstack + DUK_VALSTACK_INITIAL_SIZE;
  44011. thr->valstack_bottom = thr->valstack;
  44012. thr->valstack_top = thr->valstack;
  44013. for (i = 0; i < DUK_VALSTACK_INITIAL_SIZE; i++) {
  44014. DUK_TVAL_SET_UNDEFINED_UNUSED(&thr->valstack[i]);
  44015. }
  44016. /* callstack */
  44017. alloc_size = sizeof(duk_activation) * DUK_CALLSTACK_INITIAL_SIZE;
  44018. thr->callstack = (duk_activation *) DUK_ALLOC(heap, alloc_size);
  44019. if (!thr->callstack) {
  44020. goto fail;
  44021. }
  44022. DUK_MEMZERO(thr->callstack, alloc_size);
  44023. thr->callstack_size = DUK_CALLSTACK_INITIAL_SIZE;
  44024. DUK_ASSERT(thr->callstack_top == 0);
  44025. /* catchstack */
  44026. alloc_size = sizeof(duk_catcher) * DUK_CATCHSTACK_INITIAL_SIZE;
  44027. thr->catchstack = (duk_catcher *) DUK_ALLOC(heap, alloc_size);
  44028. if (!thr->catchstack) {
  44029. goto fail;
  44030. }
  44031. DUK_MEMZERO(thr->catchstack, alloc_size);
  44032. thr->catchstack_size = DUK_CATCHSTACK_INITIAL_SIZE;
  44033. DUK_ASSERT(thr->catchstack_top == 0);
  44034. return 1;
  44035. fail:
  44036. DUK_FREE(heap, thr->valstack);
  44037. DUK_FREE(heap, thr->callstack);
  44038. DUK_FREE(heap, thr->catchstack);
  44039. thr->valstack = NULL;
  44040. thr->callstack = NULL;
  44041. thr->catchstack = NULL;
  44042. return 0;
  44043. }
  44044. /* For indirect allocs. */
  44045. DUK_INTERNAL void *duk_hthread_get_valstack_ptr(duk_heap *heap, void *ud) {
  44046. duk_hthread *thr = (duk_hthread *) ud;
  44047. DUK_UNREF(heap);
  44048. return (void *) thr->valstack;
  44049. }
  44050. DUK_INTERNAL void *duk_hthread_get_callstack_ptr(duk_heap *heap, void *ud) {
  44051. duk_hthread *thr = (duk_hthread *) ud;
  44052. DUK_UNREF(heap);
  44053. return (void *) thr->callstack;
  44054. }
  44055. DUK_INTERNAL void *duk_hthread_get_catchstack_ptr(duk_heap *heap, void *ud) {
  44056. duk_hthread *thr = (duk_hthread *) ud;
  44057. DUK_UNREF(heap);
  44058. return (void *) thr->catchstack;
  44059. }
  44060. #line 1 "duk_hthread_builtins.c"
  44061. /*
  44062. * Initialize built-in objects. Current thread must have a valstack
  44063. * and initialization errors may longjmp, so a setjmp() catch point
  44064. * must exist.
  44065. */
  44066. /* include removed: duk_internal.h */
  44067. /*
  44068. * Encoding constants, must match genbuiltins.py
  44069. */
  44070. #define DUK__CLASS_BITS 5
  44071. #define DUK__BIDX_BITS 7
  44072. #define DUK__STRIDX_BITS 9 /* XXX: try to optimize to 8 */
  44073. #define DUK__NATIDX_BITS 8
  44074. #define DUK__NUM_NORMAL_PROPS_BITS 6
  44075. #define DUK__NUM_FUNC_PROPS_BITS 6
  44076. #define DUK__PROP_FLAGS_BITS 3
  44077. #define DUK__STRING_LENGTH_BITS 8
  44078. #define DUK__STRING_CHAR_BITS 7
  44079. #define DUK__LENGTH_PROP_BITS 3
  44080. #define DUK__NARGS_BITS 3
  44081. #define DUK__PROP_TYPE_BITS 3
  44082. #define DUK__MAGIC_BITS 16
  44083. #define DUK__NARGS_VARARGS_MARKER 0x07
  44084. #define DUK__NO_CLASS_MARKER 0x00 /* 0 = DUK_HOBJECT_CLASS_UNUSED */
  44085. #define DUK__NO_BIDX_MARKER 0x7f
  44086. #define DUK__NO_STRIDX_MARKER 0xff
  44087. #define DUK__PROP_TYPE_DOUBLE 0
  44088. #define DUK__PROP_TYPE_STRING 1
  44089. #define DUK__PROP_TYPE_STRIDX 2
  44090. #define DUK__PROP_TYPE_BUILTIN 3
  44091. #define DUK__PROP_TYPE_UNDEFINED 4
  44092. #define DUK__PROP_TYPE_BOOLEAN_TRUE 5
  44093. #define DUK__PROP_TYPE_BOOLEAN_FALSE 6
  44094. #define DUK__PROP_TYPE_ACCESSOR 7
  44095. /*
  44096. * Create built-in objects by parsing an init bitstream generated
  44097. * by genbuiltins.py.
  44098. */
  44099. DUK_INTERNAL void duk_hthread_create_builtin_objects(duk_hthread *thr) {
  44100. duk_context *ctx = (duk_context *) thr;
  44101. duk_bitdecoder_ctx bd_ctx;
  44102. duk_bitdecoder_ctx *bd = &bd_ctx; /* convenience */
  44103. duk_hobject *h;
  44104. duk_small_uint_t i, j;
  44105. DUK_D(DUK_DPRINT("INITBUILTINS BEGIN"));
  44106. DUK_MEMZERO(&bd_ctx, sizeof(bd_ctx));
  44107. bd->data = (const duk_uint8_t *) duk_builtins_data;
  44108. bd->length = (duk_size_t) DUK_BUILTINS_DATA_LENGTH;
  44109. /*
  44110. * First create all built-in bare objects on the empty valstack.
  44111. * During init, their indices will correspond to built-in indices.
  44112. *
  44113. * Built-ins will be reachable from both valstack and thr->builtins.
  44114. */
  44115. /* XXX: there is no need to resize valstack because builtin count
  44116. * is much less than the default space; assert for it.
  44117. */
  44118. DUK_DD(DUK_DDPRINT("create empty built-ins"));
  44119. DUK_ASSERT_TOP(ctx, 0);
  44120. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  44121. duk_small_uint_t class_num;
  44122. duk_small_int_t len = -1; /* must be signed */
  44123. class_num = (duk_small_uint_t) duk_bd_decode(bd, DUK__CLASS_BITS);
  44124. len = (duk_small_int_t) duk_bd_decode_flagged(bd, DUK__LENGTH_PROP_BITS, (duk_int32_t) -1 /*def_value*/);
  44125. if (class_num == DUK_HOBJECT_CLASS_FUNCTION) {
  44126. duk_small_uint_t natidx;
  44127. duk_small_uint_t stridx;
  44128. duk_int_t c_nargs; /* must hold DUK_VARARGS */
  44129. duk_c_function c_func;
  44130. duk_int16_t magic;
  44131. DUK_DDD(DUK_DDDPRINT("len=%ld", (long) len));
  44132. DUK_ASSERT(len >= 0);
  44133. natidx = (duk_small_uint_t) duk_bd_decode(bd, DUK__NATIDX_BITS);
  44134. stridx = (duk_small_uint_t) duk_bd_decode(bd, DUK__STRIDX_BITS);
  44135. c_func = duk_bi_native_functions[natidx];
  44136. c_nargs = (duk_small_uint_t) duk_bd_decode_flagged(bd, DUK__NARGS_BITS, len /*def_value*/);
  44137. if (c_nargs == DUK__NARGS_VARARGS_MARKER) {
  44138. c_nargs = DUK_VARARGS;
  44139. }
  44140. /* XXX: set magic directly here? (it could share the c_nargs arg) */
  44141. duk_push_c_function_noexotic(ctx, c_func, c_nargs);
  44142. h = duk_require_hobject(ctx, -1);
  44143. DUK_ASSERT(h != NULL);
  44144. /* Currently all built-in native functions are strict.
  44145. * duk_push_c_function() now sets strict flag, so
  44146. * assert for it.
  44147. */
  44148. DUK_ASSERT(DUK_HOBJECT_HAS_STRICT(h));
  44149. /* XXX: function properties */
  44150. duk_push_hstring_stridx(ctx, stridx);
  44151. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE);
  44152. /* Almost all global level Function objects are constructable
  44153. * but not all: Function.prototype is a non-constructable,
  44154. * callable Function.
  44155. */
  44156. if (duk_bd_decode_flag(bd)) {
  44157. DUK_ASSERT(DUK_HOBJECT_HAS_CONSTRUCTABLE(h));
  44158. } else {
  44159. DUK_HOBJECT_CLEAR_CONSTRUCTABLE(h);
  44160. }
  44161. /* Cast converts magic to 16-bit signed value */
  44162. magic = (duk_int16_t) duk_bd_decode_flagged(bd, DUK__MAGIC_BITS, 0 /*def_value*/);
  44163. ((duk_hnativefunction *) h)->magic = magic;
  44164. } else {
  44165. /* XXX: ARRAY_PART for Array prototype? */
  44166. duk_push_object_helper(ctx,
  44167. DUK_HOBJECT_FLAG_EXTENSIBLE,
  44168. -1); /* no prototype or class yet */
  44169. h = duk_require_hobject(ctx, -1);
  44170. DUK_ASSERT(h != NULL);
  44171. }
  44172. DUK_HOBJECT_SET_CLASS_NUMBER(h, class_num);
  44173. thr->builtins[i] = h;
  44174. DUK_HOBJECT_INCREF(thr, &h->hdr);
  44175. if (len >= 0) {
  44176. /*
  44177. * For top-level objects, 'length' property has the following
  44178. * default attributes: non-writable, non-enumerable, non-configurable
  44179. * (E5 Section 15).
  44180. *
  44181. * However, 'length' property for Array.prototype has attributes
  44182. * expected of an Array instance which are different: writable,
  44183. * non-enumerable, non-configurable (E5 Section 15.4.5.2).
  44184. *
  44185. * This is currently determined implicitly based on class; there are
  44186. * no attribute flags in the init data.
  44187. */
  44188. duk_push_int(ctx, len);
  44189. duk_xdef_prop_stridx(ctx,
  44190. -2,
  44191. DUK_STRIDX_LENGTH,
  44192. (class_num == DUK_HOBJECT_CLASS_ARRAY ? /* only Array.prototype matches */
  44193. DUK_PROPDESC_FLAGS_W : DUK_PROPDESC_FLAGS_NONE));
  44194. }
  44195. /* enable exotic behaviors last */
  44196. if (class_num == DUK_HOBJECT_CLASS_ARRAY) {
  44197. DUK_HOBJECT_SET_EXOTIC_ARRAY(h);
  44198. }
  44199. if (class_num == DUK_HOBJECT_CLASS_STRING) {
  44200. DUK_HOBJECT_SET_EXOTIC_STRINGOBJ(h);
  44201. }
  44202. /* some assertions */
  44203. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(h));
  44204. /* DUK_HOBJECT_FLAG_CONSTRUCTABLE varies */
  44205. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(h));
  44206. DUK_ASSERT(!DUK_HOBJECT_HAS_COMPILEDFUNCTION(h));
  44207. /* DUK_HOBJECT_FLAG_NATIVEFUNCTION varies */
  44208. DUK_ASSERT(!DUK_HOBJECT_HAS_THREAD(h));
  44209. DUK_ASSERT(!DUK_HOBJECT_HAS_ARRAY_PART(h)); /* currently, even for Array.prototype */
  44210. /* DUK_HOBJECT_FLAG_STRICT varies */
  44211. DUK_ASSERT(!DUK_HOBJECT_HAS_NATIVEFUNCTION(h) || /* all native functions have NEWENV */
  44212. DUK_HOBJECT_HAS_NEWENV(h));
  44213. DUK_ASSERT(!DUK_HOBJECT_HAS_NAMEBINDING(h));
  44214. DUK_ASSERT(!DUK_HOBJECT_HAS_CREATEARGS(h));
  44215. DUK_ASSERT(!DUK_HOBJECT_HAS_ENVRECCLOSED(h));
  44216. /* DUK_HOBJECT_FLAG_EXOTIC_ARRAY varies */
  44217. /* DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ varies */
  44218. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(h));
  44219. DUK_DDD(DUK_DDDPRINT("created built-in %ld, class=%ld, length=%ld", (long) i, (long) class_num, (long) len));
  44220. }
  44221. /*
  44222. * Then decode the builtins init data (see genbuiltins.py) to
  44223. * init objects
  44224. */
  44225. DUK_DD(DUK_DDPRINT("initialize built-in object properties"));
  44226. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  44227. duk_small_uint_t t;
  44228. duk_small_uint_t num;
  44229. DUK_DDD(DUK_DDDPRINT("initializing built-in object at index %ld", (long) i));
  44230. h = thr->builtins[i];
  44231. t = (duk_small_uint_t) duk_bd_decode(bd, DUK__BIDX_BITS);
  44232. if (t != DUK__NO_BIDX_MARKER) {
  44233. DUK_DDD(DUK_DDDPRINT("set internal prototype: built-in %ld", (long) t));
  44234. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, thr->builtins[t]);
  44235. }
  44236. t = (duk_small_uint_t) duk_bd_decode(bd, DUK__BIDX_BITS);
  44237. if (t != DUK__NO_BIDX_MARKER) {
  44238. /* 'prototype' property for all built-in objects (which have it) has attributes:
  44239. * [[Writable]] = false,
  44240. * [[Enumerable]] = false,
  44241. * [[Configurable]] = false
  44242. */
  44243. DUK_DDD(DUK_DDDPRINT("set external prototype: built-in %ld", (long) t));
  44244. duk_xdef_prop_stridx_builtin(ctx, i, DUK_STRIDX_PROTOTYPE, t, DUK_PROPDESC_FLAGS_NONE);
  44245. }
  44246. t = (duk_small_uint_t) duk_bd_decode(bd, DUK__BIDX_BITS);
  44247. if (t != DUK__NO_BIDX_MARKER) {
  44248. /* 'constructor' property for all built-in objects (which have it) has attributes:
  44249. * [[Writable]] = true,
  44250. * [[Enumerable]] = false,
  44251. * [[Configurable]] = true
  44252. */
  44253. DUK_DDD(DUK_DDDPRINT("set external constructor: built-in %ld", (long) t));
  44254. duk_xdef_prop_stridx_builtin(ctx, i, DUK_STRIDX_CONSTRUCTOR, t, DUK_PROPDESC_FLAGS_WC);
  44255. }
  44256. /* normal valued properties */
  44257. num = (duk_small_uint_t) duk_bd_decode(bd, DUK__NUM_NORMAL_PROPS_BITS);
  44258. DUK_DDD(DUK_DDDPRINT("built-in object %ld, %ld normal valued properties", (long) i, (long) num));
  44259. for (j = 0; j < num; j++) {
  44260. duk_small_uint_t stridx;
  44261. duk_small_uint_t prop_flags;
  44262. stridx = (duk_small_uint_t) duk_bd_decode(bd, DUK__STRIDX_BITS);
  44263. /*
  44264. * Property attribute defaults are defined in E5 Section 15 (first
  44265. * few pages); there is a default for all properties and a special
  44266. * default for 'length' properties. Variation from the defaults is
  44267. * signaled using a single flag bit in the bitstream.
  44268. */
  44269. if (duk_bd_decode_flag(bd)) {
  44270. prop_flags = (duk_small_uint_t) duk_bd_decode(bd, DUK__PROP_FLAGS_BITS);
  44271. } else {
  44272. if (stridx == DUK_STRIDX_LENGTH) {
  44273. prop_flags = DUK_PROPDESC_FLAGS_NONE;
  44274. } else {
  44275. prop_flags = DUK_PROPDESC_FLAGS_WC;
  44276. }
  44277. }
  44278. t = (duk_small_uint_t) duk_bd_decode(bd, DUK__PROP_TYPE_BITS);
  44279. DUK_DDD(DUK_DDDPRINT("built-in %ld, normal-valued property %ld, stridx %ld, flags 0x%02lx, type %ld",
  44280. (long) i, (long) j, (long) stridx, (unsigned long) prop_flags, (long) t));
  44281. switch (t) {
  44282. case DUK__PROP_TYPE_DOUBLE: {
  44283. duk_double_union du;
  44284. duk_small_uint_t k;
  44285. for (k = 0; k < 8; k++) {
  44286. /* Encoding endianness must match target memory layout,
  44287. * build scripts and genbuiltins.py must ensure this.
  44288. */
  44289. du.uc[k] = (duk_uint8_t) duk_bd_decode(bd, 8);
  44290. }
  44291. duk_push_number(ctx, du.d); /* push operation normalizes NaNs */
  44292. break;
  44293. }
  44294. case DUK__PROP_TYPE_STRING: {
  44295. duk_small_uint_t n;
  44296. duk_small_uint_t k;
  44297. duk_uint8_t *p;
  44298. n = (duk_small_uint_t) duk_bd_decode(bd, DUK__STRING_LENGTH_BITS);
  44299. p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, n);
  44300. for (k = 0; k < n; k++) {
  44301. *p++ = (duk_uint8_t) duk_bd_decode(bd, DUK__STRING_CHAR_BITS);
  44302. }
  44303. duk_to_string(ctx, -1);
  44304. break;
  44305. }
  44306. case DUK__PROP_TYPE_STRIDX: {
  44307. duk_small_uint_t n;
  44308. n = (duk_small_uint_t) duk_bd_decode(bd, DUK__STRIDX_BITS);
  44309. DUK_ASSERT_DISABLE(n >= 0); /* unsigned */
  44310. DUK_ASSERT(n < DUK_HEAP_NUM_STRINGS);
  44311. duk_push_hstring_stridx(ctx, n);
  44312. break;
  44313. }
  44314. case DUK__PROP_TYPE_BUILTIN: {
  44315. duk_small_uint_t bidx;
  44316. bidx = (duk_small_uint_t) duk_bd_decode(bd, DUK__BIDX_BITS);
  44317. DUK_ASSERT(bidx != DUK__NO_BIDX_MARKER);
  44318. duk_dup(ctx, (duk_idx_t) bidx);
  44319. break;
  44320. }
  44321. case DUK__PROP_TYPE_UNDEFINED: {
  44322. duk_push_undefined(ctx);
  44323. break;
  44324. }
  44325. case DUK__PROP_TYPE_BOOLEAN_TRUE: {
  44326. duk_push_true(ctx);
  44327. break;
  44328. }
  44329. case DUK__PROP_TYPE_BOOLEAN_FALSE: {
  44330. duk_push_false(ctx);
  44331. break;
  44332. }
  44333. case DUK__PROP_TYPE_ACCESSOR: {
  44334. duk_small_uint_t natidx_getter = (duk_small_uint_t) duk_bd_decode(bd, DUK__NATIDX_BITS);
  44335. duk_small_uint_t natidx_setter = (duk_small_uint_t) duk_bd_decode(bd, DUK__NATIDX_BITS);
  44336. duk_c_function c_func_getter;
  44337. duk_c_function c_func_setter;
  44338. /* XXX: this is a bit awkward because there is no exposed helper
  44339. * in the API style, only this internal helper.
  44340. */
  44341. DUK_DDD(DUK_DDDPRINT("built-in accessor property: objidx=%ld, stridx=%ld, getteridx=%ld, setteridx=%ld, flags=0x%04lx",
  44342. (long) i, (long) stridx, (long) natidx_getter, (long) natidx_setter, (unsigned long) prop_flags));
  44343. c_func_getter = duk_bi_native_functions[natidx_getter];
  44344. c_func_setter = duk_bi_native_functions[natidx_setter];
  44345. duk_push_c_function_noconstruct_noexotic(ctx, c_func_getter, 0); /* always 0 args */
  44346. duk_push_c_function_noconstruct_noexotic(ctx, c_func_setter, 1); /* always 1 arg */
  44347. /* XXX: magic for getter/setter? */
  44348. prop_flags |= DUK_PROPDESC_FLAG_ACCESSOR; /* accessor flag not encoded explicitly */
  44349. duk_hobject_define_accessor_internal(thr,
  44350. duk_require_hobject(ctx, i),
  44351. DUK_HTHREAD_GET_STRING(thr, stridx),
  44352. duk_require_hobject(ctx, -2),
  44353. duk_require_hobject(ctx, -1),
  44354. prop_flags);
  44355. duk_pop_2(ctx); /* getter and setter, now reachable through object */
  44356. goto skip_value;
  44357. }
  44358. default: {
  44359. /* exhaustive */
  44360. DUK_UNREACHABLE();
  44361. }
  44362. }
  44363. DUK_ASSERT((prop_flags & DUK_PROPDESC_FLAG_ACCESSOR) == 0);
  44364. duk_xdef_prop_stridx(ctx, i, stridx, prop_flags);
  44365. skip_value:
  44366. continue; /* avoid empty label at the end of a compound statement */
  44367. }
  44368. /* native function properties */
  44369. num = (duk_small_uint_t) duk_bd_decode(bd, DUK__NUM_FUNC_PROPS_BITS);
  44370. DUK_DDD(DUK_DDDPRINT("built-in object %ld, %ld function valued properties", (long) i, (long) num));
  44371. for (j = 0; j < num; j++) {
  44372. duk_small_uint_t stridx;
  44373. duk_small_uint_t natidx;
  44374. duk_int_t c_nargs; /* must hold DUK_VARARGS */
  44375. duk_small_uint_t c_length;
  44376. duk_int16_t magic;
  44377. duk_c_function c_func;
  44378. duk_hnativefunction *h_func;
  44379. #if defined(DUK_USE_LIGHTFUNC_BUILTINS)
  44380. duk_small_int_t lightfunc_eligible;
  44381. #endif
  44382. stridx = (duk_small_uint_t) duk_bd_decode(bd, DUK__STRIDX_BITS);
  44383. natidx = (duk_small_uint_t) duk_bd_decode(bd, DUK__NATIDX_BITS);
  44384. c_length = (duk_small_uint_t) duk_bd_decode(bd, DUK__LENGTH_PROP_BITS);
  44385. c_nargs = (duk_int_t) duk_bd_decode_flagged(bd, DUK__NARGS_BITS, (duk_int32_t) c_length /*def_value*/);
  44386. if (c_nargs == DUK__NARGS_VARARGS_MARKER) {
  44387. c_nargs = DUK_VARARGS;
  44388. }
  44389. c_func = duk_bi_native_functions[natidx];
  44390. DUK_DDD(DUK_DDDPRINT("built-in %ld, function-valued property %ld, stridx %ld, natidx %ld, length %ld, nargs %ld",
  44391. (long) i, (long) j, (long) stridx, (long) natidx, (long) c_length,
  44392. (c_nargs == DUK_VARARGS ? (long) -1 : (long) c_nargs)));
  44393. /* Cast converts magic to 16-bit signed value */
  44394. magic = (duk_int16_t) duk_bd_decode_flagged(bd, DUK__MAGIC_BITS, 0);
  44395. #if defined(DUK_USE_LIGHTFUNC_BUILTINS)
  44396. lightfunc_eligible =
  44397. ((c_nargs >= DUK_LFUNC_NARGS_MIN && c_nargs <= DUK_LFUNC_NARGS_MAX) || (c_nargs == DUK_VARARGS)) &&
  44398. (c_length <= DUK_LFUNC_LENGTH_MAX) &&
  44399. (magic >= DUK_LFUNC_MAGIC_MIN && magic <= DUK_LFUNC_MAGIC_MAX);
  44400. if (stridx == DUK_STRIDX_EVAL ||
  44401. stridx == DUK_STRIDX_YIELD ||
  44402. stridx == DUK_STRIDX_RESUME ||
  44403. stridx == DUK_STRIDX_REQUIRE) {
  44404. /* These functions have trouble working as lightfuncs.
  44405. * Some of them have specific asserts and some may have
  44406. * additional properties (e.g. 'require.id' may be written).
  44407. */
  44408. DUK_D(DUK_DPRINT("reject as lightfunc: stridx=%d, i=%d, j=%d", (int) stridx, (int) i, (int) j));
  44409. lightfunc_eligible = 0;
  44410. }
  44411. if (lightfunc_eligible) {
  44412. duk_tval tv_lfunc;
  44413. duk_small_uint_t lf_nargs = (c_nargs == DUK_VARARGS ? DUK_LFUNC_NARGS_VARARGS : c_nargs);
  44414. duk_small_uint_t lf_flags = DUK_LFUNC_FLAGS_PACK(magic, c_length, lf_nargs);
  44415. DUK_TVAL_SET_LIGHTFUNC(&tv_lfunc, c_func, lf_flags);
  44416. duk_push_tval(ctx, &tv_lfunc);
  44417. 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)));
  44418. goto lightfunc_skip;
  44419. }
  44420. 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));
  44421. #endif /* DUK_USE_LIGHTFUNC_BUILTINS */
  44422. /* [ (builtin objects) ] */
  44423. duk_push_c_function_noconstruct_noexotic(ctx, c_func, c_nargs);
  44424. h_func = duk_require_hnativefunction(ctx, -1);
  44425. DUK_UNREF(h_func);
  44426. /* Currently all built-in native functions are strict.
  44427. * This doesn't matter for many functions, but e.g.
  44428. * String.prototype.charAt (and other string functions)
  44429. * rely on being strict so that their 'this' binding is
  44430. * not automatically coerced.
  44431. */
  44432. DUK_HOBJECT_SET_STRICT((duk_hobject *) h_func);
  44433. /* No built-in functions are constructable except the top
  44434. * level ones (Number, etc).
  44435. */
  44436. DUK_ASSERT(!DUK_HOBJECT_HAS_CONSTRUCTABLE((duk_hobject *) h_func));
  44437. /* XXX: any way to avoid decoding magic bit; there are quite
  44438. * many function properties and relatively few with magic values.
  44439. */
  44440. h_func->magic = magic;
  44441. /* [ (builtin objects) func ] */
  44442. duk_push_int(ctx, c_length);
  44443. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE);
  44444. duk_push_hstring_stridx(ctx, stridx);
  44445. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE);
  44446. /* XXX: other properties of function instances; 'arguments', 'caller'. */
  44447. DUK_DD(DUK_DDPRINT("built-in object %ld, function property %ld -> %!T",
  44448. (long) i, (long) j, (duk_tval *) duk_get_tval(ctx, -1)));
  44449. /* [ (builtin objects) func ] */
  44450. /*
  44451. * The default property attributes are correct for all
  44452. * function valued properties of built-in objects now.
  44453. */
  44454. #if defined(DUK_USE_LIGHTFUNC_BUILTINS)
  44455. lightfunc_skip:
  44456. #endif
  44457. duk_xdef_prop_stridx(ctx, i, stridx, DUK_PROPDESC_FLAGS_WC);
  44458. /* [ (builtin objects) ] */
  44459. }
  44460. }
  44461. /*
  44462. * Special post-tweaks, for cases not covered by the init data format.
  44463. *
  44464. * - Set Date.prototype.toGMTString to Date.prototype.toUTCString.
  44465. * toGMTString is required to have the same Function object as
  44466. * toUTCString in E5 Section B.2.6. Note that while Smjs respects
  44467. * this, V8 does not (the Function objects are distinct).
  44468. *
  44469. * - Make DoubleError non-extensible.
  44470. *
  44471. * - Add info about most important effective compile options to Duktape.
  44472. *
  44473. * - Possibly remove some properties (values or methods) which are not
  44474. * desirable with current feature options but are not currently
  44475. * conditional in init data.
  44476. */
  44477. duk_get_prop_stridx(ctx, DUK_BIDX_DATE_PROTOTYPE, DUK_STRIDX_TO_UTC_STRING);
  44478. duk_xdef_prop_stridx(ctx, DUK_BIDX_DATE_PROTOTYPE, DUK_STRIDX_TO_GMT_STRING, DUK_PROPDESC_FLAGS_WC);
  44479. h = duk_require_hobject(ctx, DUK_BIDX_DOUBLE_ERROR);
  44480. DUK_ASSERT(h != NULL);
  44481. DUK_HOBJECT_CLEAR_EXTENSIBLE(h);
  44482. #if !defined(DUK_USE_ES6_OBJECT_PROTO_PROPERTY)
  44483. DUK_DD(DUK_DDPRINT("delete Object.prototype.__proto__ built-in which is not enabled in features"));
  44484. (void) duk_hobject_delprop_raw(thr, thr->builtins[DUK_BIDX_OBJECT_PROTOTYPE], DUK_HTHREAD_STRING___PROTO__(thr), DUK_DELPROP_FLAG_THROW);
  44485. #endif
  44486. #if !defined(DUK_USE_ES6_OBJECT_SETPROTOTYPEOF)
  44487. DUK_DD(DUK_DDPRINT("delete Object.setPrototypeOf built-in which is not enabled in features"));
  44488. (void) duk_hobject_delprop_raw(thr, thr->builtins[DUK_BIDX_OBJECT_CONSTRUCTOR], DUK_HTHREAD_STRING_SET_PROTOTYPE_OF(thr), DUK_DELPROP_FLAG_THROW);
  44489. #endif
  44490. duk_push_string(ctx,
  44491. /* Endianness indicator */
  44492. #if defined(DUK_USE_INTEGER_LE)
  44493. "l"
  44494. #elif defined(DUK_USE_INTEGER_BE)
  44495. "b"
  44496. #elif defined(DUK_USE_INTEGER_ME) /* integer mixed endian not really used now */
  44497. "m"
  44498. #else
  44499. "?"
  44500. #endif
  44501. #if defined(DUK_USE_DOUBLE_LE)
  44502. "l"
  44503. #elif defined(DUK_USE_DOUBLE_BE)
  44504. "b"
  44505. #elif defined(DUK_USE_DOUBLE_ME)
  44506. "m"
  44507. #else
  44508. "?"
  44509. #endif
  44510. #if defined(DUK_USE_BYTEORDER_FORCED)
  44511. "f"
  44512. #endif
  44513. " "
  44514. /* Packed or unpacked tval */
  44515. #if defined(DUK_USE_PACKED_TVAL)
  44516. "p"
  44517. #else
  44518. "u"
  44519. #endif
  44520. #if defined(DUK_USE_FASTINT)
  44521. "f"
  44522. #endif
  44523. " "
  44524. /* Low memory options */
  44525. #if defined(DUK_USE_STRTAB_CHAIN)
  44526. "c" /* chain */
  44527. #elif defined(DUK_USE_STRTAB_PROBE)
  44528. "p" /* probe */
  44529. #else
  44530. "?"
  44531. #endif
  44532. #if !defined(DUK_USE_HEAPPTR16) && !defined(DUK_DATAPTR16) && !defined(DUK_FUNCPTR16)
  44533. "n"
  44534. #endif
  44535. #if defined(DUK_USE_HEAPPTR16)
  44536. "h"
  44537. #endif
  44538. #if defined(DUK_USE_DATAPTR16)
  44539. "d"
  44540. #endif
  44541. #if defined(DUK_USE_FUNCPTR16)
  44542. "f"
  44543. #endif
  44544. #if defined(DUK_USE_REFCOUNT16)
  44545. "R"
  44546. #endif
  44547. #if defined(DUK_USE_STRHASH16)
  44548. "H"
  44549. #endif
  44550. #if defined(DUK_USE_STRLEN16)
  44551. "S"
  44552. #endif
  44553. #if defined(DUK_USE_BUFLEN16)
  44554. "B"
  44555. #endif
  44556. #if defined(DUK_USE_OBJSIZES16)
  44557. "O"
  44558. #endif
  44559. #if defined(DUK_USE_LIGHTFUNC_BUILTINS)
  44560. "L"
  44561. #endif
  44562. " "
  44563. /* Object property allocation layout */
  44564. #if defined(DUK_USE_HOBJECT_LAYOUT_1)
  44565. "p1"
  44566. #elif defined(DUK_USE_HOBJECT_LAYOUT_2)
  44567. "p2"
  44568. #elif defined(DUK_USE_HOBJECT_LAYOUT_3)
  44569. "p3"
  44570. #else
  44571. "p?"
  44572. #endif
  44573. " "
  44574. /* Alignment guarantee */
  44575. #if defined(DUK_USE_ALIGN_4)
  44576. "a4"
  44577. #elif defined(DUK_USE_ALIGN_8)
  44578. "a8"
  44579. #else
  44580. "a1"
  44581. #endif
  44582. " "
  44583. /* Architecture, OS, and compiler strings */
  44584. DUK_USE_ARCH_STRING
  44585. " "
  44586. DUK_USE_OS_STRING
  44587. " "
  44588. DUK_USE_COMPILER_STRING);
  44589. duk_xdef_prop_stridx(ctx, DUK_BIDX_DUKTAPE, DUK_STRIDX_ENV, DUK_PROPDESC_FLAGS_WC);
  44590. /*
  44591. * InitJS code - Ecmascript code evaluated from a built-in source
  44592. * which provides e.g. backward compatibility. User can also provide
  44593. * JS code to be evaluated at startup.
  44594. */
  44595. #ifdef DUK_USE_BUILTIN_INITJS
  44596. /* XXX: compression */
  44597. DUK_DD(DUK_DDPRINT("running built-in initjs"));
  44598. duk_eval_string(ctx, (const char *) duk_initjs_data); /* initjs data is NUL terminated */
  44599. duk_pop(ctx);
  44600. #endif /* DUK_USE_BUILTIN_INITJS */
  44601. #ifdef DUK_USE_USER_INITJS
  44602. /* XXX: compression (as an option) */
  44603. DUK_DD(DUK_DDPRINT("running user initjs"));
  44604. duk_eval_string_noresult(ctx, (const char *) DUK_USE_USER_INITJS);
  44605. #endif /* DUK_USE_USER_INITJS */
  44606. /*
  44607. * Since built-ins are not often extended, compact them.
  44608. */
  44609. DUK_DD(DUK_DDPRINT("compact built-ins"));
  44610. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  44611. duk_hobject_compact_props(thr, thr->builtins[i]);
  44612. }
  44613. DUK_D(DUK_DPRINT("INITBUILTINS END"));
  44614. #ifdef DUK_USE_DDPRINT
  44615. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  44616. DUK_DD(DUK_DDPRINT("built-in object %ld after initialization and compacting: %!@iO",
  44617. (long) i, (duk_heaphdr *) thr->builtins[i]));
  44618. }
  44619. #endif
  44620. /*
  44621. * Pop built-ins from stack: they are now INCREF'd and
  44622. * reachable from the builtins[] array.
  44623. */
  44624. duk_pop_n(ctx, DUK_NUM_BUILTINS);
  44625. DUK_ASSERT_TOP(ctx, 0);
  44626. }
  44627. DUK_INTERNAL void duk_hthread_copy_builtin_objects(duk_hthread *thr_from, duk_hthread *thr_to) {
  44628. duk_small_uint_t i;
  44629. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  44630. thr_to->builtins[i] = thr_from->builtins[i];
  44631. DUK_HOBJECT_INCREF_ALLOWNULL(thr_to, thr_to->builtins[i]); /* side effect free */
  44632. }
  44633. }
  44634. #line 1 "duk_hthread_misc.c"
  44635. /*
  44636. * Thread support.
  44637. */
  44638. /* include removed: duk_internal.h */
  44639. DUK_INTERNAL void duk_hthread_terminate(duk_hthread *thr) {
  44640. DUK_ASSERT(thr != NULL);
  44641. /* Order of unwinding is important */
  44642. duk_hthread_catchstack_unwind(thr, 0);
  44643. duk_hthread_callstack_unwind(thr, 0); /* side effects, possibly errors */
  44644. thr->valstack_bottom = thr->valstack;
  44645. duk_set_top((duk_context *) thr, 0); /* unwinds valstack, updating refcounts */
  44646. thr->state = DUK_HTHREAD_STATE_TERMINATED;
  44647. /* Here we could remove references to built-ins, but it may not be
  44648. * worth the effort because built-ins are quite likely to be shared
  44649. * with another (unterminated) thread, and terminated threads are also
  44650. * usually garbage collected quite quickly. Also, doing DECREFs
  44651. * could trigger finalization, which would run on the current thread
  44652. * and have access to only some of the built-ins. Garbage collection
  44653. * deals with this correctly already.
  44654. */
  44655. /* XXX: Shrink the stacks to minimize memory usage? May not
  44656. * be worth the effort because terminated threads are usually
  44657. * garbage collected quite soon.
  44658. */
  44659. }
  44660. DUK_INTERNAL duk_activation *duk_hthread_get_current_activation(duk_hthread *thr) {
  44661. DUK_ASSERT(thr != NULL);
  44662. if (thr->callstack_top > 0) {
  44663. return thr->callstack + thr->callstack_top - 1;
  44664. } else {
  44665. return NULL;
  44666. }
  44667. }
  44668. #line 1 "duk_hthread_stacks.c"
  44669. /*
  44670. * Manipulation of thread stacks (valstack, callstack, catchstack).
  44671. *
  44672. * Ideally unwinding of stacks should have no side effects, which would
  44673. * then favor separate unwinding and shrink check primitives for each
  44674. * stack type. A shrink check may realloc and thus have side effects.
  44675. *
  44676. * However, currently callstack unwinding itself has side effects, as it
  44677. * needs to DECREF multiple objects, close environment records, etc.
  44678. * Stacks must thus be unwound in the correct order by the caller.
  44679. *
  44680. * (XXX: This should be probably reworked so that there is a shared
  44681. * unwind primitive which handles all stacks as requested, and knows
  44682. * the proper order for unwinding.)
  44683. *
  44684. * Valstack entries above 'top' are always kept initialized to
  44685. * "undefined unused". Callstack and catchstack entries above 'top'
  44686. * are not zeroed and are left as garbage.
  44687. *
  44688. * Value stack handling is mostly a part of the API implementation.
  44689. */
  44690. /* include removed: duk_internal.h */
  44691. /* check that there is space for at least one new entry */
  44692. DUK_INTERNAL void duk_hthread_callstack_grow(duk_hthread *thr) {
  44693. duk_activation *new_ptr;
  44694. duk_size_t old_size;
  44695. duk_size_t new_size;
  44696. DUK_ASSERT(thr != NULL);
  44697. DUK_ASSERT_DISABLE(thr->callstack_top >= 0); /* avoid warning (unsigned) */
  44698. DUK_ASSERT(thr->callstack_size >= thr->callstack_top);
  44699. if (thr->callstack_top < thr->callstack_size) {
  44700. return;
  44701. }
  44702. old_size = thr->callstack_size;
  44703. new_size = old_size + DUK_CALLSTACK_GROW_STEP;
  44704. /* this is a bit approximate (errors out before max is reached); this is OK */
  44705. if (new_size >= thr->callstack_max) {
  44706. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_CALLSTACK_LIMIT);
  44707. }
  44708. DUK_DD(DUK_DDPRINT("growing callstack %ld -> %ld", (long) old_size, (long) new_size));
  44709. /*
  44710. * Note: must use indirect variant of DUK_REALLOC() because underlying
  44711. * pointer may be changed by mark-and-sweep.
  44712. */
  44713. DUK_ASSERT(new_size > 0);
  44714. new_ptr = (duk_activation *) DUK_REALLOC_INDIRECT(thr->heap, duk_hthread_get_callstack_ptr, (void *) thr, sizeof(duk_activation) * new_size);
  44715. if (!new_ptr) {
  44716. /* No need for a NULL/zero-size check because new_size > 0) */
  44717. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_REALLOC_FAILED);
  44718. }
  44719. thr->callstack = new_ptr;
  44720. thr->callstack_size = new_size;
  44721. /* note: any entries above the callstack top are garbage and not zeroed */
  44722. }
  44723. DUK_INTERNAL void duk_hthread_callstack_shrink_check(duk_hthread *thr) {
  44724. duk_size_t new_size;
  44725. duk_activation *p;
  44726. DUK_ASSERT(thr != NULL);
  44727. DUK_ASSERT_DISABLE(thr->callstack_top >= 0); /* avoid warning (unsigned) */
  44728. DUK_ASSERT(thr->callstack_size >= thr->callstack_top);
  44729. if (thr->callstack_size - thr->callstack_top < DUK_CALLSTACK_SHRINK_THRESHOLD) {
  44730. return;
  44731. }
  44732. new_size = thr->callstack_top + DUK_CALLSTACK_SHRINK_SPARE;
  44733. DUK_ASSERT(new_size >= thr->callstack_top);
  44734. DUK_DD(DUK_DDPRINT("shrinking callstack %ld -> %ld", (long) thr->callstack_size, (long) new_size));
  44735. /*
  44736. * Note: must use indirect variant of DUK_REALLOC() because underlying
  44737. * pointer may be changed by mark-and-sweep.
  44738. */
  44739. /* shrink failure is not fatal */
  44740. p = (duk_activation *) DUK_REALLOC_INDIRECT(thr->heap, duk_hthread_get_callstack_ptr, (void *) thr, sizeof(duk_activation) * new_size);
  44741. if (p) {
  44742. thr->callstack = p;
  44743. thr->callstack_size = new_size;
  44744. } else {
  44745. /* Because new_size != 0, if condition doesn't need to be
  44746. * (p != NULL || new_size == 0).
  44747. */
  44748. DUK_ASSERT(new_size != 0);
  44749. DUK_D(DUK_DPRINT("callstack shrink failed, ignoring"));
  44750. }
  44751. /* note: any entries above the callstack top are garbage and not zeroed */
  44752. }
  44753. DUK_INTERNAL void duk_hthread_callstack_unwind(duk_hthread *thr, duk_size_t new_top) {
  44754. duk_size_t idx;
  44755. DUK_DDD(DUK_DDDPRINT("unwind callstack top of thread %p from %ld to %ld",
  44756. (void *) thr,
  44757. (thr != NULL ? (long) thr->callstack_top : (long) -1),
  44758. (long) new_top));
  44759. DUK_ASSERT(thr);
  44760. DUK_ASSERT(thr->heap);
  44761. DUK_ASSERT_DISABLE(new_top >= 0); /* unsigned */
  44762. DUK_ASSERT((duk_size_t) new_top <= thr->callstack_top); /* cannot grow */
  44763. /*
  44764. * The loop below must avoid issues with potential callstack
  44765. * reallocations. A resize (and other side effects) may happen
  44766. * e.g. due to finalizer/errhandler calls caused by a refzero or
  44767. * mark-and-sweep. Arbitrary finalizers may run, because when
  44768. * an environment record is refzero'd, it may refer to arbitrary
  44769. * values which also become refzero'd.
  44770. *
  44771. * So, the pointer 'p' is re-looked-up below whenever a side effect
  44772. * might have changed it.
  44773. */
  44774. idx = thr->callstack_top;
  44775. while (idx > new_top) {
  44776. duk_activation *act;
  44777. duk_hobject *func;
  44778. #ifdef DUK_USE_REFERENCE_COUNTING
  44779. duk_hobject *tmp;
  44780. #endif
  44781. #ifdef DUK_USE_DEBUGGER_SUPPORT
  44782. duk_heap *heap;
  44783. #endif
  44784. idx--;
  44785. DUK_ASSERT_DISABLE(idx >= 0); /* unsigned */
  44786. DUK_ASSERT((duk_size_t) idx < thr->callstack_size); /* true, despite side effect resizes */
  44787. act = thr->callstack + idx;
  44788. /* With lightfuncs, act 'func' may be NULL */
  44789. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  44790. /*
  44791. * Restore 'caller' property for non-strict callee functions.
  44792. */
  44793. func = DUK_ACT_GET_FUNC(act);
  44794. if (func != NULL && !DUK_HOBJECT_HAS_STRICT(func)) {
  44795. duk_tval *tv_caller;
  44796. duk_tval tv_tmp;
  44797. duk_hobject *h_tmp;
  44798. tv_caller = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_CALLER(thr));
  44799. /* The act->prev_caller should only be set if the entry for 'caller'
  44800. * exists (as it is only set in that case, and the property is not
  44801. * configurable), but handle all the cases anyway.
  44802. */
  44803. if (tv_caller) {
  44804. DUK_TVAL_SET_TVAL(&tv_tmp, tv_caller);
  44805. if (act->prev_caller) {
  44806. /* Just transfer the refcount from act->prev_caller to tv_caller,
  44807. * so no need for a refcount update. This is the expected case.
  44808. */
  44809. DUK_TVAL_SET_OBJECT(tv_caller, act->prev_caller);
  44810. act->prev_caller = NULL;
  44811. } else {
  44812. DUK_TVAL_SET_NULL(tv_caller); /* no incref needed */
  44813. DUK_ASSERT(act->prev_caller == NULL);
  44814. }
  44815. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  44816. } else {
  44817. h_tmp = act->prev_caller;
  44818. if (h_tmp) {
  44819. act->prev_caller = NULL;
  44820. DUK_HOBJECT_DECREF(thr, h_tmp); /* side effects */
  44821. }
  44822. }
  44823. act = thr->callstack + idx; /* avoid side effects */
  44824. DUK_ASSERT(act->prev_caller == NULL);
  44825. }
  44826. #endif
  44827. /*
  44828. * Unwind debugger state. If we unwind while stepping
  44829. * (either step over or step into), pause execution.
  44830. */
  44831. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  44832. heap = thr->heap;
  44833. if (heap->dbg_step_thread == thr &&
  44834. heap->dbg_step_csindex == idx) {
  44835. /* Pause for all step types: step into, step over, step out.
  44836. * This is the only place explicitly handling a step out.
  44837. */
  44838. DUK_HEAP_SET_PAUSED(heap);
  44839. DUK_ASSERT(heap->dbg_step_thread == NULL);
  44840. }
  44841. #endif
  44842. /*
  44843. * Close environment record(s) if they exist.
  44844. *
  44845. * Only variable environments are closed. If lex_env != var_env, it
  44846. * cannot currently contain any register bound declarations.
  44847. *
  44848. * Only environments created for a NEWENV function are closed. If an
  44849. * environment is created for e.g. an eval call, it must not be closed.
  44850. */
  44851. func = DUK_ACT_GET_FUNC(act);
  44852. if (func != NULL && !DUK_HOBJECT_HAS_NEWENV(func)) {
  44853. DUK_DDD(DUK_DDDPRINT("skip closing environments, envs not owned by this activation"));
  44854. goto skip_env_close;
  44855. }
  44856. /* func is NULL for lightfunc */
  44857. DUK_ASSERT(act->lex_env == act->var_env);
  44858. if (act->var_env != NULL) {
  44859. DUK_DDD(DUK_DDDPRINT("closing var_env record %p -> %!O",
  44860. (void *) act->var_env, (duk_heaphdr *) act->var_env));
  44861. duk_js_close_environment_record(thr, act->var_env, func, act->idx_bottom);
  44862. act = thr->callstack + idx; /* avoid side effect issues */
  44863. }
  44864. #if 0
  44865. if (act->lex_env != NULL) {
  44866. if (act->lex_env == act->var_env) {
  44867. /* common case, already closed, so skip */
  44868. DUK_DD(DUK_DDPRINT("lex_env and var_env are the same and lex_env "
  44869. "already closed -> skip closing lex_env"));
  44870. ;
  44871. } else {
  44872. DUK_DD(DUK_DDPRINT("closing lex_env record %p -> %!O",
  44873. (void *) act->lex_env, (duk_heaphdr *) act->lex_env));
  44874. duk_js_close_environment_record(thr, act->lex_env, DUK_ACT_GET_FUNC(act), act->idx_bottom);
  44875. act = thr->callstack + idx; /* avoid side effect issues */
  44876. }
  44877. }
  44878. #endif
  44879. DUK_ASSERT((act->lex_env == NULL) ||
  44880. ((duk_hobject_find_existing_entry_tval_ptr(thr->heap, act->lex_env, DUK_HTHREAD_STRING_INT_CALLEE(thr)) == NULL) &&
  44881. (duk_hobject_find_existing_entry_tval_ptr(thr->heap, act->lex_env, DUK_HTHREAD_STRING_INT_VARMAP(thr)) == NULL) &&
  44882. (duk_hobject_find_existing_entry_tval_ptr(thr->heap, act->lex_env, DUK_HTHREAD_STRING_INT_THREAD(thr)) == NULL) &&
  44883. (duk_hobject_find_existing_entry_tval_ptr(thr->heap, act->lex_env, DUK_HTHREAD_STRING_INT_REGBASE(thr)) == NULL)));
  44884. DUK_ASSERT((act->var_env == NULL) ||
  44885. ((duk_hobject_find_existing_entry_tval_ptr(thr->heap, act->var_env, DUK_HTHREAD_STRING_INT_CALLEE(thr)) == NULL) &&
  44886. (duk_hobject_find_existing_entry_tval_ptr(thr->heap, act->var_env, DUK_HTHREAD_STRING_INT_VARMAP(thr)) == NULL) &&
  44887. (duk_hobject_find_existing_entry_tval_ptr(thr->heap, act->var_env, DUK_HTHREAD_STRING_INT_THREAD(thr)) == NULL) &&
  44888. (duk_hobject_find_existing_entry_tval_ptr(thr->heap, act->var_env, DUK_HTHREAD_STRING_INT_REGBASE(thr)) == NULL)));
  44889. skip_env_close:
  44890. /*
  44891. * Update preventcount
  44892. */
  44893. if (act->flags & DUK_ACT_FLAG_PREVENT_YIELD) {
  44894. DUK_ASSERT(thr->callstack_preventcount >= 1);
  44895. thr->callstack_preventcount--;
  44896. }
  44897. /*
  44898. * Reference count updates
  44899. *
  44900. * Note: careful manipulation of refcounts. The top is
  44901. * not updated yet, so all the activations are reachable
  44902. * for mark-and-sweep (which may be triggered by decref).
  44903. * However, the pointers are NULL so this is not an issue.
  44904. */
  44905. #ifdef DUK_USE_REFERENCE_COUNTING
  44906. tmp = act->var_env;
  44907. #endif
  44908. act->var_env = NULL;
  44909. #ifdef DUK_USE_REFERENCE_COUNTING
  44910. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  44911. act = thr->callstack + idx; /* avoid side effect issues */
  44912. #endif
  44913. #ifdef DUK_USE_REFERENCE_COUNTING
  44914. tmp = act->lex_env;
  44915. #endif
  44916. act->lex_env = NULL;
  44917. #ifdef DUK_USE_REFERENCE_COUNTING
  44918. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  44919. act = thr->callstack + idx; /* avoid side effect issues */
  44920. #endif
  44921. /* Note: this may cause a corner case situation where a finalizer
  44922. * may see a currently reachable activation whose 'func' is NULL.
  44923. */
  44924. #ifdef DUK_USE_REFERENCE_COUNTING
  44925. tmp = DUK_ACT_GET_FUNC(act);
  44926. #endif
  44927. act->func = NULL;
  44928. #ifdef DUK_USE_REFERENCE_COUNTING
  44929. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  44930. act = thr->callstack + idx; /* avoid side effect issues */
  44931. DUK_UNREF(act);
  44932. #endif
  44933. }
  44934. thr->callstack_top = new_top;
  44935. /*
  44936. * We could clear the book-keeping variables for the topmost activation,
  44937. * but don't do so now.
  44938. */
  44939. #if 0
  44940. if (thr->callstack_top > 0) {
  44941. duk_activation *act = thr->callstack + thr->callstack_top - 1;
  44942. act->idx_retval = 0;
  44943. }
  44944. #endif
  44945. /* Note: any entries above the callstack top are garbage and not zeroed.
  44946. * Also topmost activation idx_retval is garbage (not zeroed), and must
  44947. * be ignored.
  44948. */
  44949. }
  44950. DUK_INTERNAL void duk_hthread_catchstack_grow(duk_hthread *thr) {
  44951. duk_catcher *new_ptr;
  44952. duk_size_t old_size;
  44953. duk_size_t new_size;
  44954. DUK_ASSERT(thr != NULL);
  44955. DUK_ASSERT_DISABLE(thr->catchstack_top); /* avoid warning (unsigned) */
  44956. DUK_ASSERT(thr->catchstack_size >= thr->catchstack_top);
  44957. if (thr->catchstack_top < thr->catchstack_size) {
  44958. return;
  44959. }
  44960. old_size = thr->catchstack_size;
  44961. new_size = old_size + DUK_CATCHSTACK_GROW_STEP;
  44962. /* this is a bit approximate (errors out before max is reached); this is OK */
  44963. if (new_size >= thr->catchstack_max) {
  44964. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_CATCHSTACK_LIMIT);
  44965. }
  44966. DUK_DD(DUK_DDPRINT("growing catchstack %ld -> %ld", (long) old_size, (long) new_size));
  44967. /*
  44968. * Note: must use indirect variant of DUK_REALLOC() because underlying
  44969. * pointer may be changed by mark-and-sweep.
  44970. */
  44971. DUK_ASSERT(new_size > 0);
  44972. new_ptr = (duk_catcher *) DUK_REALLOC_INDIRECT(thr->heap, duk_hthread_get_catchstack_ptr, (void *) thr, sizeof(duk_catcher) * new_size);
  44973. if (!new_ptr) {
  44974. /* No need for a NULL/zero-size check because new_size > 0) */
  44975. DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, DUK_STR_REALLOC_FAILED);
  44976. }
  44977. thr->catchstack = new_ptr;
  44978. thr->catchstack_size = new_size;
  44979. /* note: any entries above the catchstack top are garbage and not zeroed */
  44980. }
  44981. DUK_INTERNAL void duk_hthread_catchstack_shrink_check(duk_hthread *thr) {
  44982. duk_size_t new_size;
  44983. duk_catcher *p;
  44984. DUK_ASSERT(thr != NULL);
  44985. DUK_ASSERT_DISABLE(thr->catchstack_top >= 0); /* avoid warning (unsigned) */
  44986. DUK_ASSERT(thr->catchstack_size >= thr->catchstack_top);
  44987. if (thr->catchstack_size - thr->catchstack_top < DUK_CATCHSTACK_SHRINK_THRESHOLD) {
  44988. return;
  44989. }
  44990. new_size = thr->catchstack_top + DUK_CATCHSTACK_SHRINK_SPARE;
  44991. DUK_ASSERT(new_size >= thr->catchstack_top);
  44992. DUK_DD(DUK_DDPRINT("shrinking catchstack %ld -> %ld", (long) thr->catchstack_size, (long) new_size));
  44993. /*
  44994. * Note: must use indirect variant of DUK_REALLOC() because underlying
  44995. * pointer may be changed by mark-and-sweep.
  44996. */
  44997. /* shrink failure is not fatal */
  44998. p = (duk_catcher *) DUK_REALLOC_INDIRECT(thr->heap, duk_hthread_get_catchstack_ptr, (void *) thr, sizeof(duk_catcher) * new_size);
  44999. if (p) {
  45000. thr->catchstack = p;
  45001. thr->catchstack_size = new_size;
  45002. } else {
  45003. /* Because new_size != 0, if condition doesn't need to be
  45004. * (p != NULL || new_size == 0).
  45005. */
  45006. DUK_ASSERT(new_size != 0);
  45007. DUK_D(DUK_DPRINT("catchstack shrink failed, ignoring"));
  45008. }
  45009. /* note: any entries above the catchstack top are garbage and not zeroed */
  45010. }
  45011. DUK_INTERNAL void duk_hthread_catchstack_unwind(duk_hthread *thr, duk_size_t new_top) {
  45012. duk_size_t idx;
  45013. DUK_DDD(DUK_DDDPRINT("unwind catchstack top of thread %p from %ld to %ld",
  45014. (void *) thr,
  45015. (thr != NULL ? (long) thr->catchstack_top : (long) -1),
  45016. (long) new_top));
  45017. DUK_ASSERT(thr);
  45018. DUK_ASSERT(thr->heap);
  45019. DUK_ASSERT_DISABLE(new_top >= 0); /* unsigned */
  45020. DUK_ASSERT((duk_size_t) new_top <= thr->catchstack_top); /* cannot grow */
  45021. /*
  45022. * Since there are no references in the catcher structure,
  45023. * unwinding is quite simple. The only thing we need to
  45024. * look out for is popping a possible lexical environment
  45025. * established for an active catch clause.
  45026. */
  45027. idx = thr->catchstack_top;
  45028. while (idx > new_top) {
  45029. duk_catcher *p;
  45030. duk_activation *act;
  45031. duk_hobject *env;
  45032. idx--;
  45033. DUK_ASSERT_DISABLE(idx >= 0); /* unsigned */
  45034. DUK_ASSERT((duk_size_t) idx < thr->catchstack_size);
  45035. p = thr->catchstack + idx;
  45036. if (DUK_CAT_HAS_LEXENV_ACTIVE(p)) {
  45037. DUK_DDD(DUK_DDDPRINT("unwinding catchstack idx %ld, callstack idx %ld, callstack top %ld: lexical environment active",
  45038. (long) idx, (long) p->callstack_index, (long) thr->callstack_top));
  45039. /* XXX: Here we have a nasty dependency: the need to manipulate
  45040. * the callstack means that catchstack must always be unwound by
  45041. * the caller before unwinding the callstack. This should be fixed
  45042. * later.
  45043. */
  45044. /* Note that multiple catchstack entries may refer to the same
  45045. * callstack entry.
  45046. */
  45047. act = thr->callstack + p->callstack_index;
  45048. DUK_ASSERT(act >= thr->callstack);
  45049. DUK_ASSERT(act < thr->callstack + thr->callstack_top);
  45050. DUK_DDD(DUK_DDDPRINT("catchstack_index=%ld, callstack_index=%ld, lex_env=%!iO",
  45051. (long) idx, (long) p->callstack_index,
  45052. (duk_heaphdr *) act->lex_env));
  45053. env = act->lex_env; /* current lex_env of the activation (created for catcher) */
  45054. DUK_ASSERT(env != NULL); /* must be, since env was created when catcher was created */
  45055. act->lex_env = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, env); /* prototype is lex_env before catcher created */
  45056. DUK_HOBJECT_DECREF(thr, env);
  45057. /* There is no need to decref anything else than 'env': if 'env'
  45058. * becomes unreachable, refzero will handle decref'ing its prototype.
  45059. */
  45060. }
  45061. }
  45062. thr->catchstack_top = new_top;
  45063. /* note: any entries above the catchstack top are garbage and not zeroed */
  45064. }
  45065. #line 1 "duk_js_call.c"
  45066. /*
  45067. * Call handling.
  45068. *
  45069. * The main work horse functions are:
  45070. * - duk_handle_call(): call to a C/Ecmascript functions
  45071. * - duk_handle_safe_call(): make a protected C call within current activation
  45072. * - duk_handle_ecma_call_setup(): Ecmascript-to-Ecmascript calls, including
  45073. * tail calls and coroutine resume
  45074. */
  45075. /* include removed: duk_internal.h */
  45076. /*
  45077. * Arguments object creation.
  45078. *
  45079. * Creating arguments objects is a bit finicky, see E5 Section 10.6 for the
  45080. * specific requirements. Much of the arguments object exotic behavior is
  45081. * implemented in duk_hobject_props.c, and is enabled by the object flag
  45082. * DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS.
  45083. */
  45084. DUK_LOCAL
  45085. void duk__create_arguments_object(duk_hthread *thr,
  45086. duk_hobject *func,
  45087. duk_hobject *varenv,
  45088. duk_idx_t idx_argbase, /* idx of first argument on stack */
  45089. duk_idx_t num_stack_args) { /* num args starting from idx_argbase */
  45090. duk_context *ctx = (duk_context *) thr;
  45091. duk_hobject *arg; /* 'arguments' */
  45092. duk_hobject *formals; /* formals for 'func' (may be NULL if func is a C function) */
  45093. duk_idx_t i_arg;
  45094. duk_idx_t i_map;
  45095. duk_idx_t i_mappednames;
  45096. duk_idx_t i_formals;
  45097. duk_idx_t i_argbase;
  45098. duk_idx_t n_formals;
  45099. duk_idx_t idx;
  45100. duk_bool_t need_map;
  45101. DUK_DDD(DUK_DDDPRINT("creating arguments object for func=%!iO, varenv=%!iO, "
  45102. "idx_argbase=%ld, num_stack_args=%ld",
  45103. (duk_heaphdr *) func, (duk_heaphdr *) varenv,
  45104. (long) idx_argbase, (long) num_stack_args));
  45105. DUK_ASSERT(thr != NULL);
  45106. DUK_ASSERT(func != NULL);
  45107. DUK_ASSERT(DUK_HOBJECT_IS_NONBOUND_FUNCTION(func));
  45108. DUK_ASSERT(varenv != NULL);
  45109. DUK_ASSERT(idx_argbase >= 0); /* assumed to bottom relative */
  45110. DUK_ASSERT(num_stack_args >= 0);
  45111. need_map = 0;
  45112. i_argbase = idx_argbase;
  45113. DUK_ASSERT(i_argbase >= 0);
  45114. duk_push_hobject(ctx, func);
  45115. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_FORMALS);
  45116. formals = duk_get_hobject(ctx, -1);
  45117. n_formals = 0;
  45118. if (formals) {
  45119. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LENGTH);
  45120. n_formals = (duk_idx_t) duk_require_int(ctx, -1);
  45121. duk_pop(ctx);
  45122. }
  45123. duk_remove(ctx, -2); /* leave formals on stack for later use */
  45124. i_formals = duk_require_top_index(ctx);
  45125. DUK_ASSERT(n_formals >= 0);
  45126. DUK_ASSERT(formals != NULL || n_formals == 0);
  45127. DUK_DDD(DUK_DDDPRINT("func=%!O, formals=%!O, n_formals=%ld",
  45128. (duk_heaphdr *) func, (duk_heaphdr *) formals,
  45129. (long) n_formals));
  45130. /* [ ... formals ] */
  45131. /*
  45132. * Create required objects:
  45133. * - 'arguments' object: array-like, but not an array
  45134. * - 'map' object: internal object, tied to 'arguments'
  45135. * - 'mappedNames' object: temporary value used during construction
  45136. */
  45137. i_arg = duk_push_object_helper(ctx,
  45138. DUK_HOBJECT_FLAG_EXTENSIBLE |
  45139. DUK_HOBJECT_FLAG_ARRAY_PART |
  45140. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARGUMENTS),
  45141. DUK_BIDX_OBJECT_PROTOTYPE);
  45142. DUK_ASSERT(i_arg >= 0);
  45143. arg = duk_require_hobject(ctx, -1);
  45144. DUK_ASSERT(arg != NULL);
  45145. i_map = duk_push_object_helper(ctx,
  45146. DUK_HOBJECT_FLAG_EXTENSIBLE |
  45147. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  45148. -1); /* no prototype */
  45149. DUK_ASSERT(i_map >= 0);
  45150. i_mappednames = duk_push_object_helper(ctx,
  45151. DUK_HOBJECT_FLAG_EXTENSIBLE |
  45152. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  45153. -1); /* no prototype */
  45154. DUK_ASSERT(i_mappednames >= 0);
  45155. /* [... formals arguments map mappedNames] */
  45156. DUK_DDD(DUK_DDDPRINT("created arguments related objects: "
  45157. "arguments at index %ld -> %!O "
  45158. "map at index %ld -> %!O "
  45159. "mappednames at index %ld -> %!O",
  45160. (long) i_arg, (duk_heaphdr *) duk_get_hobject(ctx, i_arg),
  45161. (long) i_map, (duk_heaphdr *) duk_get_hobject(ctx, i_map),
  45162. (long) i_mappednames, (duk_heaphdr *) duk_get_hobject(ctx, i_mappednames)));
  45163. /*
  45164. * Init arguments properties, map, etc.
  45165. */
  45166. duk_push_int(ctx, num_stack_args);
  45167. duk_xdef_prop_stridx(ctx, i_arg, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_WC);
  45168. /*
  45169. * Init argument related properties
  45170. */
  45171. /* step 11 */
  45172. idx = num_stack_args - 1;
  45173. while (idx >= 0) {
  45174. DUK_DDD(DUK_DDDPRINT("arg idx %ld, argbase=%ld, argidx=%ld",
  45175. (long) idx, (long) i_argbase, (long) (i_argbase + idx)));
  45176. DUK_DDD(DUK_DDDPRINT("define arguments[%ld]=arg", (long) idx));
  45177. duk_dup(ctx, i_argbase + idx);
  45178. duk_xdef_prop_index_wec(ctx, i_arg, (duk_uarridx_t) idx);
  45179. DUK_DDD(DUK_DDDPRINT("defined arguments[%ld]=arg", (long) idx));
  45180. /* step 11.c is relevant only if non-strict (checked in 11.c.ii) */
  45181. if (!DUK_HOBJECT_HAS_STRICT(func) && idx < n_formals) {
  45182. DUK_ASSERT(formals != NULL);
  45183. DUK_DDD(DUK_DDDPRINT("strict function, index within formals (%ld < %ld)",
  45184. (long) idx, (long) n_formals));
  45185. duk_get_prop_index(ctx, i_formals, idx);
  45186. DUK_ASSERT(duk_is_string(ctx, -1));
  45187. duk_dup(ctx, -1); /* [... name name] */
  45188. if (!duk_has_prop(ctx, i_mappednames)) {
  45189. /* steps 11.c.ii.1 - 11.c.ii.4, but our internal book-keeping
  45190. * differs from the reference model
  45191. */
  45192. /* [... name] */
  45193. need_map = 1;
  45194. DUK_DDD(DUK_DDDPRINT("set mappednames[%s]=%ld",
  45195. (const char *) duk_get_string(ctx, -1),
  45196. (long) idx));
  45197. duk_dup(ctx, -1); /* name */
  45198. duk_push_uint(ctx, (duk_uint_t) idx); /* index */
  45199. duk_to_string(ctx, -1);
  45200. duk_xdef_prop_wec(ctx, i_mappednames); /* out of spec, must be configurable */
  45201. DUK_DDD(DUK_DDDPRINT("set map[%ld]=%s",
  45202. (long) idx,
  45203. duk_get_string(ctx, -1)));
  45204. duk_dup(ctx, -1); /* name */
  45205. duk_xdef_prop_index_wec(ctx, i_map, (duk_uarridx_t) idx); /* out of spec, must be configurable */
  45206. } else {
  45207. /* duk_has_prop() popped the second 'name' */
  45208. }
  45209. /* [... name] */
  45210. duk_pop(ctx); /* pop 'name' */
  45211. }
  45212. idx--;
  45213. }
  45214. DUK_DDD(DUK_DDDPRINT("actual arguments processed"));
  45215. /* step 12 */
  45216. if (need_map) {
  45217. DUK_DDD(DUK_DDDPRINT("adding 'map' and 'varenv' to arguments object"));
  45218. /* should never happen for a strict callee */
  45219. DUK_ASSERT(!DUK_HOBJECT_HAS_STRICT(func));
  45220. duk_dup(ctx, i_map);
  45221. duk_xdef_prop_stridx(ctx, i_arg, DUK_STRIDX_INT_MAP, DUK_PROPDESC_FLAGS_NONE); /* out of spec, don't care */
  45222. /* The variable environment for magic variable bindings needs to be
  45223. * given by the caller and recorded in the arguments object.
  45224. *
  45225. * See E5 Section 10.6, the creation of setters/getters.
  45226. *
  45227. * The variable environment also provides access to the callee, so
  45228. * an explicit (internal) callee property is not needed.
  45229. */
  45230. duk_push_hobject(ctx, varenv);
  45231. duk_xdef_prop_stridx(ctx, i_arg, DUK_STRIDX_INT_VARENV, DUK_PROPDESC_FLAGS_NONE); /* out of spec, don't care */
  45232. }
  45233. /* steps 13-14 */
  45234. if (DUK_HOBJECT_HAS_STRICT(func)) {
  45235. /*
  45236. * Note: callee/caller are throwers and are not deletable etc.
  45237. * They could be implemented as virtual properties, but currently
  45238. * there is no support for virtual properties which are accessors
  45239. * (only plain virtual properties). This would not be difficult
  45240. * to change in duk_hobject_props, but we can make the throwers
  45241. * normal, concrete properties just as easily.
  45242. *
  45243. * Note that the specification requires that the *same* thrower
  45244. * built-in object is used here! See E5 Section 10.6 main
  45245. * algoritm, step 14, and Section 13.2.3 which describes the
  45246. * thrower. See test case test-arguments-throwers.js.
  45247. */
  45248. DUK_DDD(DUK_DDDPRINT("strict function, setting caller/callee to throwers"));
  45249. duk_xdef_prop_stridx_thrower(ctx, i_arg, DUK_STRIDX_CALLER, DUK_PROPDESC_FLAGS_NONE);
  45250. duk_xdef_prop_stridx_thrower(ctx, i_arg, DUK_STRIDX_CALLEE, DUK_PROPDESC_FLAGS_NONE);
  45251. } else {
  45252. DUK_DDD(DUK_DDDPRINT("non-strict function, setting callee to actual value"));
  45253. duk_push_hobject(ctx, func);
  45254. duk_xdef_prop_stridx(ctx, i_arg, DUK_STRIDX_CALLEE, DUK_PROPDESC_FLAGS_WC);
  45255. }
  45256. /* set exotic behavior only after we're done */
  45257. if (need_map) {
  45258. /*
  45259. * Note: exotic behaviors are only enabled for arguments
  45260. * objects which have a parameter map (see E5 Section 10.6
  45261. * main algorithm, step 12).
  45262. *
  45263. * In particular, a non-strict arguments object with no
  45264. * mapped formals does *NOT* get exotic behavior, even
  45265. * for e.g. "caller" property. This seems counterintuitive
  45266. * but seems to be the case.
  45267. */
  45268. /* cannot be strict (never mapped variables) */
  45269. DUK_ASSERT(!DUK_HOBJECT_HAS_STRICT(func));
  45270. DUK_DDD(DUK_DDDPRINT("enabling exotic behavior for arguments object"));
  45271. DUK_HOBJECT_SET_EXOTIC_ARGUMENTS(arg);
  45272. } else {
  45273. DUK_DDD(DUK_DDDPRINT("not enabling exotic behavior for arguments object"));
  45274. }
  45275. /* nice log */
  45276. DUK_DDD(DUK_DDDPRINT("final arguments related objects: "
  45277. "arguments at index %ld -> %!O "
  45278. "map at index %ld -> %!O "
  45279. "mappednames at index %ld -> %!O",
  45280. (long) i_arg, (duk_heaphdr *) duk_get_hobject(ctx, i_arg),
  45281. (long) i_map, (duk_heaphdr *) duk_get_hobject(ctx, i_map),
  45282. (long) i_mappednames, (duk_heaphdr *) duk_get_hobject(ctx, i_mappednames)));
  45283. /* [args(n) [crud] formals arguments map mappednames] -> [args [crud] arguments] */
  45284. duk_pop_2(ctx);
  45285. duk_remove(ctx, -2);
  45286. }
  45287. /* Helper for creating the arguments object and adding it to the env record
  45288. * on top of the value stack. This helper has a very strict dependency on
  45289. * the shape of the input stack.
  45290. */
  45291. DUK_LOCAL
  45292. void duk__handle_createargs_for_call(duk_hthread *thr,
  45293. duk_hobject *func,
  45294. duk_hobject *env,
  45295. duk_idx_t num_stack_args) {
  45296. duk_context *ctx = (duk_context *) thr;
  45297. DUK_DDD(DUK_DDDPRINT("creating arguments object for function call"));
  45298. DUK_ASSERT(thr != NULL);
  45299. DUK_ASSERT(func != NULL);
  45300. DUK_ASSERT(env != NULL);
  45301. DUK_ASSERT(DUK_HOBJECT_HAS_CREATEARGS(func));
  45302. DUK_ASSERT(duk_get_top(ctx) >= num_stack_args + 1);
  45303. /* [... arg1 ... argN envobj] */
  45304. duk__create_arguments_object(thr,
  45305. func,
  45306. env,
  45307. duk_get_top(ctx) - num_stack_args - 1, /* idx_argbase */
  45308. num_stack_args);
  45309. /* [... arg1 ... argN envobj argobj] */
  45310. duk_xdef_prop_stridx(ctx,
  45311. -2,
  45312. DUK_STRIDX_LC_ARGUMENTS,
  45313. DUK_HOBJECT_HAS_STRICT(func) ? DUK_PROPDESC_FLAGS_E : /* strict: non-deletable, non-writable */
  45314. DUK_PROPDESC_FLAGS_WE); /* non-strict: non-deletable, writable */
  45315. /* [... arg1 ... argN envobj] */
  45316. }
  45317. /*
  45318. * Helper for handling a "bound function" chain when a call is being made.
  45319. *
  45320. * Follows the bound function chain until a non-bound function is found.
  45321. * Prepends the bound arguments to the value stack (at idx_func + 2),
  45322. * updating 'num_stack_args' in the process. The 'this' binding is also
  45323. * updated if necessary (at idx_func + 1). Note that for constructor calls
  45324. * the 'this' binding is never updated by [[BoundThis]].
  45325. *
  45326. * XXX: bound function chains could be collapsed at bound function creation
  45327. * time so that each bound function would point directly to a non-bound
  45328. * function. This would make call time handling much easier.
  45329. */
  45330. DUK_LOCAL
  45331. void duk__handle_bound_chain_for_call(duk_hthread *thr,
  45332. duk_idx_t idx_func,
  45333. duk_idx_t *p_num_stack_args, /* may be changed by call */
  45334. duk_bool_t is_constructor_call) {
  45335. duk_context *ctx = (duk_context *) thr;
  45336. duk_idx_t num_stack_args;
  45337. duk_tval *tv_func;
  45338. duk_hobject *func;
  45339. duk_uint_t sanity;
  45340. DUK_ASSERT(thr != NULL);
  45341. DUK_ASSERT(p_num_stack_args != NULL);
  45342. /* On entry, item at idx_func is a bound, non-lightweight function,
  45343. * but we don't rely on that below.
  45344. */
  45345. num_stack_args = *p_num_stack_args;
  45346. sanity = DUK_HOBJECT_BOUND_CHAIN_SANITY;
  45347. do {
  45348. duk_idx_t i, len;
  45349. tv_func = duk_require_tval(ctx, idx_func);
  45350. DUK_ASSERT(tv_func != NULL);
  45351. if (DUK_TVAL_IS_LIGHTFUNC(tv_func)) {
  45352. /* Lightweight function: never bound, so terminate. */
  45353. break;
  45354. } else if (DUK_TVAL_IS_OBJECT(tv_func)) {
  45355. func = DUK_TVAL_GET_OBJECT(tv_func);
  45356. if (!DUK_HOBJECT_HAS_BOUND(func)) {
  45357. /* Normal non-bound function. */
  45358. break;
  45359. }
  45360. } else {
  45361. /* Function.prototype.bind() should never let this happen,
  45362. * ugly error message is enough.
  45363. */
  45364. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR);
  45365. }
  45366. DUK_ASSERT(DUK_TVAL_GET_OBJECT(tv_func) != NULL);
  45367. /* XXX: this could be more compact by accessing the internal properties
  45368. * directly as own properties (they cannot be inherited, and are not
  45369. * externally visible).
  45370. */
  45371. DUK_DDD(DUK_DDDPRINT("bound function encountered, ptr=%p, num_stack_args=%ld: %!T",
  45372. (void *) DUK_TVAL_GET_OBJECT(tv_func), (long) num_stack_args, tv_func));
  45373. /* [ ... func this arg1 ... argN ] */
  45374. if (is_constructor_call) {
  45375. /* See: ecmascript-testcases/test-spec-bound-constructor.js */
  45376. DUK_DDD(DUK_DDDPRINT("constructor call: don't update this binding"));
  45377. } else {
  45378. duk_get_prop_stridx(ctx, idx_func, DUK_STRIDX_INT_THIS);
  45379. duk_replace(ctx, idx_func + 1); /* idx_this = idx_func + 1 */
  45380. }
  45381. /* [ ... func this arg1 ... argN ] */
  45382. /* XXX: duk_get_length? */
  45383. duk_get_prop_stridx(ctx, idx_func, DUK_STRIDX_INT_ARGS); /* -> [ ... func this arg1 ... argN _Args ] */
  45384. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LENGTH); /* -> [ ... func this arg1 ... argN _Args length ] */
  45385. len = (duk_idx_t) duk_require_int(ctx, -1);
  45386. duk_pop(ctx);
  45387. for (i = 0; i < len; i++) {
  45388. /* XXX: very slow - better to bulk allocate a gap, and copy
  45389. * from args_array directly (we know it has a compact array
  45390. * part, etc).
  45391. */
  45392. /* [ ... func this <some bound args> arg1 ... argN _Args ] */
  45393. duk_get_prop_index(ctx, -1, i);
  45394. duk_insert(ctx, idx_func + 2 + i); /* idx_args = idx_func + 2 */
  45395. }
  45396. num_stack_args += len; /* must be updated to work properly (e.g. creation of 'arguments') */
  45397. duk_pop(ctx);
  45398. /* [ ... func this <bound args> arg1 ... argN ] */
  45399. duk_get_prop_stridx(ctx, idx_func, DUK_STRIDX_INT_TARGET);
  45400. duk_replace(ctx, idx_func); /* replace in stack */
  45401. DUK_DDD(DUK_DDDPRINT("bound function handled, num_stack_args=%ld, idx_func=%ld, curr func=%!T",
  45402. (long) num_stack_args, (long) idx_func, duk_get_tval(ctx, idx_func)));
  45403. } while (--sanity > 0);
  45404. if (sanity == 0) {
  45405. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_BOUND_CHAIN_LIMIT);
  45406. }
  45407. DUK_DDD(DUK_DDDPRINT("final non-bound function is: %!T", duk_get_tval(ctx, idx_func)));
  45408. #ifdef DUK_USE_ASSERTIONS
  45409. tv_func = duk_require_tval(ctx, idx_func);
  45410. DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv_func) || DUK_TVAL_IS_OBJECT(tv_func));
  45411. if (DUK_TVAL_IS_OBJECT(tv_func)) {
  45412. func = DUK_TVAL_GET_OBJECT(tv_func);
  45413. DUK_ASSERT(func != NULL);
  45414. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func));
  45415. DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(func) ||
  45416. DUK_HOBJECT_HAS_NATIVEFUNCTION(func));
  45417. }
  45418. #endif
  45419. /* write back */
  45420. *p_num_stack_args = num_stack_args;
  45421. }
  45422. /*
  45423. * Helper for setting up var_env and lex_env of an activation,
  45424. * assuming it does NOT have the DUK_HOBJECT_FLAG_NEWENV flag.
  45425. */
  45426. DUK_LOCAL
  45427. void duk__handle_oldenv_for_call(duk_hthread *thr,
  45428. duk_hobject *func,
  45429. duk_activation *act) {
  45430. duk_tval *tv;
  45431. DUK_ASSERT(thr != NULL);
  45432. DUK_ASSERT(func != NULL);
  45433. DUK_ASSERT(act != NULL);
  45434. DUK_ASSERT(!DUK_HOBJECT_HAS_NEWENV(func));
  45435. DUK_ASSERT(!DUK_HOBJECT_HAS_CREATEARGS(func));
  45436. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_INT_LEXENV(thr));
  45437. if (tv) {
  45438. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  45439. DUK_ASSERT(DUK_HOBJECT_IS_ENV(DUK_TVAL_GET_OBJECT(tv)));
  45440. act->lex_env = DUK_TVAL_GET_OBJECT(tv);
  45441. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_INT_VARENV(thr));
  45442. if (tv) {
  45443. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  45444. DUK_ASSERT(DUK_HOBJECT_IS_ENV(DUK_TVAL_GET_OBJECT(tv)));
  45445. act->var_env = DUK_TVAL_GET_OBJECT(tv);
  45446. } else {
  45447. act->var_env = act->lex_env;
  45448. }
  45449. } else {
  45450. act->lex_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  45451. act->var_env = act->lex_env;
  45452. }
  45453. DUK_HOBJECT_INCREF_ALLOWNULL(thr, act->lex_env);
  45454. DUK_HOBJECT_INCREF_ALLOWNULL(thr, act->var_env);
  45455. }
  45456. /*
  45457. * Helper for updating callee 'caller' property.
  45458. */
  45459. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  45460. DUK_LOCAL void duk__update_func_caller_prop(duk_hthread *thr, duk_hobject *func) {
  45461. duk_tval *tv_caller;
  45462. duk_hobject *h_tmp;
  45463. duk_activation *act_callee;
  45464. duk_activation *act_caller;
  45465. DUK_ASSERT(thr != NULL);
  45466. DUK_ASSERT(func != NULL);
  45467. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func)); /* bound chain resolved */
  45468. DUK_ASSERT(thr->callstack_top >= 1);
  45469. if (DUK_HOBJECT_HAS_STRICT(func)) {
  45470. /* Strict functions don't get their 'caller' updated. */
  45471. return;
  45472. }
  45473. act_callee = thr->callstack + thr->callstack_top - 1;
  45474. act_caller = (thr->callstack_top >= 2 ? act_callee - 1 : NULL);
  45475. /* Backup 'caller' property and update its value. */
  45476. tv_caller = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_CALLER(thr));
  45477. if (tv_caller) {
  45478. /* If caller is global/eval code, 'caller' should be set to
  45479. * 'null'.
  45480. *
  45481. * XXX: there is no exotic flag to infer this correctly now.
  45482. * The NEWENV flag is used now which works as intended for
  45483. * everything (global code, non-strict eval code, and functions)
  45484. * except strict eval code. Bound functions are never an issue
  45485. * because 'func' has been resolved to a non-bound function.
  45486. */
  45487. if (act_caller) {
  45488. /* act_caller->func may be NULL in some finalization cases,
  45489. * just treat like we don't know the caller.
  45490. */
  45491. if (act_caller->func && !DUK_HOBJECT_HAS_NEWENV(act_caller->func)) {
  45492. /* Setting to NULL causes 'caller' to be set to
  45493. * 'null' as desired.
  45494. */
  45495. act_caller = NULL;
  45496. }
  45497. }
  45498. if (DUK_TVAL_IS_OBJECT(tv_caller)) {
  45499. h_tmp = DUK_TVAL_GET_OBJECT(tv_caller);
  45500. DUK_ASSERT(h_tmp != NULL);
  45501. act_callee->prev_caller = h_tmp;
  45502. /* Previous value doesn't need refcount changes because its ownership
  45503. * is transferred to prev_caller.
  45504. */
  45505. if (act_caller) {
  45506. DUK_ASSERT(act_caller->func != NULL);
  45507. DUK_TVAL_SET_OBJECT(tv_caller, act_caller->func);
  45508. DUK_TVAL_INCREF(thr, tv_caller);
  45509. } else {
  45510. DUK_TVAL_SET_NULL(tv_caller); /* no incref */
  45511. }
  45512. } else {
  45513. /* 'caller' must only take on 'null' or function value */
  45514. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_caller));
  45515. DUK_ASSERT(act_callee->prev_caller == NULL);
  45516. if (act_caller && act_caller->func) {
  45517. /* Tolerate act_caller->func == NULL which happens in
  45518. * some finalization cases; treat like unknown caller.
  45519. */
  45520. DUK_TVAL_SET_OBJECT(tv_caller, act_caller->func);
  45521. DUK_TVAL_INCREF(thr, tv_caller);
  45522. } else {
  45523. DUK_TVAL_SET_NULL(tv_caller); /* no incref */
  45524. }
  45525. }
  45526. }
  45527. }
  45528. #endif /* DUK_USE_NONSTD_FUNC_CALLER_PROPERTY */
  45529. /*
  45530. * Determine the effective 'this' binding and coerce the current value
  45531. * on the valstack to the effective one (in-place, at idx_this).
  45532. *
  45533. * The current this value in the valstack (at idx_this) represents either:
  45534. * - the caller's requested 'this' binding; or
  45535. * - a 'this' binding accumulated from the bound function chain
  45536. *
  45537. * The final 'this' binding for the target function may still be
  45538. * different, and is determined as described in E5 Section 10.4.3.
  45539. *
  45540. * For global and eval code (E5 Sections 10.4.1 and 10.4.2), we assume
  45541. * that the caller has provided the correct 'this' binding explicitly
  45542. * when calling, i.e.:
  45543. *
  45544. * - global code: this=global object
  45545. * - direct eval: this=copy from eval() caller's this binding
  45546. * - other eval: this=global object
  45547. *
  45548. * Note: this function may cause a recursive function call with arbitrary
  45549. * side effects, because ToObject() may be called.
  45550. */
  45551. DUK_LOCAL
  45552. void duk__coerce_effective_this_binding(duk_hthread *thr,
  45553. duk_hobject *func,
  45554. duk_idx_t idx_this) {
  45555. duk_context *ctx = (duk_context *) thr;
  45556. duk_small_int_t strict;
  45557. if (func) {
  45558. strict = DUK_HOBJECT_HAS_STRICT(func);
  45559. } else {
  45560. /* Lightfuncs are always considered strict. */
  45561. strict = 1;
  45562. }
  45563. if (strict) {
  45564. DUK_DDD(DUK_DDDPRINT("this binding: strict -> use directly"));
  45565. } else {
  45566. duk_tval *tv_this = duk_require_tval(ctx, idx_this);
  45567. duk_hobject *obj_global;
  45568. if (DUK_TVAL_IS_OBJECT(tv_this)) {
  45569. DUK_DDD(DUK_DDDPRINT("this binding: non-strict, object -> use directly"));
  45570. } else if (DUK_TVAL_IS_LIGHTFUNC(tv_this)) {
  45571. /* Lightfuncs are treated like objects and not coerced. */
  45572. DUK_DDD(DUK_DDDPRINT("this binding: non-strict, lightfunc -> use directly"));
  45573. } else if (DUK_TVAL_IS_UNDEFINED(tv_this) || DUK_TVAL_IS_NULL(tv_this)) {
  45574. DUK_DDD(DUK_DDDPRINT("this binding: non-strict, undefined/null -> use global object"));
  45575. obj_global = thr->builtins[DUK_BIDX_GLOBAL];
  45576. if (obj_global) {
  45577. duk_push_hobject(ctx, obj_global);
  45578. } else {
  45579. /*
  45580. * This may only happen if built-ins are being "torn down".
  45581. * This behavior is out of specification scope.
  45582. */
  45583. DUK_D(DUK_DPRINT("this binding: wanted to use global object, but it is NULL -> using undefined instead"));
  45584. duk_push_undefined(ctx);
  45585. }
  45586. duk_replace(ctx, idx_this);
  45587. } else {
  45588. DUK_DDD(DUK_DDDPRINT("this binding: non-strict, not object/undefined/null -> use ToObject(value)"));
  45589. duk_to_object(ctx, idx_this); /* may have side effects */
  45590. }
  45591. }
  45592. }
  45593. /*
  45594. * Shared helper for non-bound func lookup.
  45595. *
  45596. * Returns duk_hobject * to the final non-bound function (NULL for lightfunc).
  45597. */
  45598. DUK_LOCAL
  45599. duk_hobject *duk__nonbound_func_lookup(duk_context *ctx,
  45600. duk_idx_t idx_func,
  45601. duk_idx_t *out_num_stack_args,
  45602. duk_tval **out_tv_func,
  45603. duk_small_uint_t call_flags) {
  45604. duk_hthread *thr = (duk_hthread *) ctx;
  45605. duk_tval *tv_func;
  45606. duk_hobject *func;
  45607. for (;;) {
  45608. /* Use loop to minimize code size of relookup after bound function case */
  45609. tv_func = duk_get_tval(ctx, idx_func);
  45610. DUK_ASSERT(tv_func != NULL);
  45611. if (DUK_TVAL_IS_OBJECT(tv_func)) {
  45612. func = DUK_TVAL_GET_OBJECT(tv_func);
  45613. if (!DUK_HOBJECT_IS_CALLABLE(func)) {
  45614. goto not_callable_error;
  45615. }
  45616. if (DUK_HOBJECT_HAS_BOUND(func)) {
  45617. duk__handle_bound_chain_for_call(thr, idx_func, out_num_stack_args, call_flags & DUK_CALL_FLAG_CONSTRUCTOR_CALL);
  45618. /* The final object may be a normal function or a lightfunc.
  45619. * We need to re-lookup tv_func because it may have changed
  45620. * (also value stack may have been resized). Loop again to
  45621. * do that; we're guaranteed not to come here again.
  45622. */
  45623. DUK_ASSERT(DUK_TVAL_IS_OBJECT(duk_require_tval(ctx, idx_func)) ||
  45624. DUK_TVAL_IS_LIGHTFUNC(duk_require_tval(ctx, idx_func)));
  45625. continue;
  45626. }
  45627. } else if (DUK_TVAL_IS_LIGHTFUNC(tv_func)) {
  45628. func = NULL;
  45629. } else {
  45630. goto not_callable_error;
  45631. }
  45632. break;
  45633. }
  45634. DUK_ASSERT((DUK_TVAL_IS_OBJECT(tv_func) && DUK_HOBJECT_IS_CALLABLE(DUK_TVAL_GET_OBJECT(tv_func))) ||
  45635. DUK_TVAL_IS_LIGHTFUNC(tv_func));
  45636. DUK_ASSERT(func == NULL || !DUK_HOBJECT_HAS_BOUND(func));
  45637. DUK_ASSERT(func == NULL || (DUK_HOBJECT_IS_COMPILEDFUNCTION(func) ||
  45638. DUK_HOBJECT_IS_NATIVEFUNCTION(func)));
  45639. *out_tv_func = tv_func;
  45640. return func;
  45641. not_callable_error:
  45642. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_CALLABLE);
  45643. DUK_UNREACHABLE();
  45644. return NULL; /* never executed */
  45645. }
  45646. /*
  45647. * Value stack resize and stack top adjustment helper
  45648. *
  45649. * XXX: This should all be merged to duk_valstack_resize_raw().
  45650. */
  45651. DUK_LOCAL
  45652. 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) {
  45653. duk_context *ctx = (duk_context *) thr;
  45654. duk_size_t vs_min_size;
  45655. duk_bool_t adjusted_top = 0;
  45656. vs_min_size = (thr->valstack_bottom - thr->valstack) + /* bottom of current func */
  45657. idx_args; /* bottom of new func */
  45658. if (nregs >= 0) {
  45659. DUK_ASSERT(nargs >= 0);
  45660. DUK_ASSERT(nregs >= nargs);
  45661. vs_min_size += nregs;
  45662. } else {
  45663. /* 'func' wants stack "as is" */
  45664. vs_min_size += num_stack_args; /* num entries of new func at entry */
  45665. }
  45666. if (func == NULL || DUK_HOBJECT_IS_NATIVEFUNCTION(func)) {
  45667. vs_min_size += DUK_VALSTACK_API_ENTRY_MINIMUM; /* Duktape/C API guaranteed entries (on top of args) */
  45668. }
  45669. vs_min_size += DUK_VALSTACK_INTERNAL_EXTRA; /* + spare */
  45670. /* XXX: Awkward fix for GH-107: we can't resize the value stack to
  45671. * a size smaller than the current top, so the order of the resize
  45672. * and adjusting the stack top depends on the current vs. final size
  45673. * of the value stack. Ideally duk_valstack_resize_raw() would have
  45674. * a combined algorithm to avoid this.
  45675. */
  45676. if (vs_min_size < (duk_size_t) (thr->valstack_top - thr->valstack)) {
  45677. DUK_DDD(DUK_DDDPRINT(("final size smaller, set top before resize")));
  45678. DUK_ASSERT(nregs >= 0); /* can't happen when keeping current stack size */
  45679. duk_set_top(ctx, idx_args + nargs); /* clamp anything above nargs */
  45680. duk_set_top(ctx, idx_args + nregs); /* extend with undefined */
  45681. adjusted_top = 1;
  45682. }
  45683. (void) duk_valstack_resize_raw((duk_context *) thr,
  45684. vs_min_size,
  45685. DUK_VSRESIZE_FLAG_SHRINK | /* flags */
  45686. 0 /* no compact */ |
  45687. DUK_VSRESIZE_FLAG_THROW);
  45688. if (!adjusted_top) {
  45689. if (nregs >= 0) {
  45690. DUK_ASSERT(nregs >= nargs);
  45691. duk_set_top(ctx, idx_args + nargs); /* clamp anything above nargs */
  45692. duk_set_top(ctx, idx_args + nregs); /* extend with undefined */
  45693. }
  45694. }
  45695. }
  45696. /*
  45697. * Helper for making various kinds of calls.
  45698. *
  45699. * Call flags:
  45700. *
  45701. * DUK_CALL_FLAG_PROTECTED <--> protected call
  45702. * DUK_CALL_FLAG_IGNORE_RECLIMIT <--> ignore C recursion limit,
  45703. * for errhandler calls
  45704. * DUK_CALL_FLAG_CONSTRUCTOR_CALL <--> for 'new Foo()' calls
  45705. *
  45706. * Input stack:
  45707. *
  45708. * [ func this arg1 ... argN ]
  45709. *
  45710. * Output stack:
  45711. *
  45712. * [ retval ] (DUK_EXEC_SUCCESS)
  45713. * [ errobj ] (DUK_EXEC_ERROR (normal error), protected call)
  45714. *
  45715. * Even when executing a protected call an error may be thrown in rare cases.
  45716. * For instance, if we run out of memory when setting up the return stack
  45717. * after a caught error, the out of memory is propagated to the caller.
  45718. * Similarly, API errors (such as invalid input stack shape and invalid
  45719. * indices) cause an error to propagate out of this function. If there is
  45720. * no catchpoint for this error, the fatal error handler is called.
  45721. *
  45722. * See 'execution.txt'.
  45723. *
  45724. * The allowed thread states for making a call are:
  45725. * - thr matches heap->curr_thread, and thr is already RUNNING
  45726. * - thr does not match heap->curr_thread (may be NULL or other),
  45727. * and thr is INACTIVE (in this case, a setjmp() catchpoint is
  45728. * always used for thread book-keeping to work properly)
  45729. *
  45730. * Like elsewhere, gotos are used to keep indent level minimal and
  45731. * avoiding a dozen helpers with awkward plumbing.
  45732. *
  45733. * Note: setjmp() and local variables have a nasty interaction,
  45734. * see execution.txt; non-volatile locals modified after setjmp()
  45735. * call are not guaranteed to keep their value.
  45736. */
  45737. DUK_INTERNAL
  45738. duk_int_t duk_handle_call(duk_hthread *thr,
  45739. duk_idx_t num_stack_args,
  45740. duk_small_uint_t call_flags) {
  45741. duk_context *ctx = (duk_context *) thr;
  45742. duk_size_t entry_valstack_bottom_index;
  45743. duk_size_t entry_valstack_end;
  45744. duk_size_t entry_callstack_top;
  45745. duk_size_t entry_catchstack_top;
  45746. duk_int_t entry_call_recursion_depth;
  45747. duk_hthread *entry_curr_thread;
  45748. duk_uint_fast8_t entry_thread_state;
  45749. volatile duk_bool_t need_setjmp;
  45750. duk_jmpbuf * volatile old_jmpbuf_ptr = NULL; /* ptr is volatile (not the target) */
  45751. duk_idx_t idx_func; /* valstack index of 'func' and retval (relative to entry valstack_bottom) */
  45752. duk_idx_t idx_args; /* valstack index of start of args (arg1) (relative to entry valstack_bottom) */
  45753. duk_idx_t nargs; /* # argument registers target function wants (< 0 => "as is") */
  45754. duk_idx_t nregs; /* # total registers target function wants on entry (< 0 => "as is") */
  45755. duk_hobject *func; /* 'func' on stack (borrowed reference) */
  45756. duk_tval *tv_func; /* duk_tval ptr for 'func' on stack (borrowed reference) or tv_func_copy */
  45757. duk_tval tv_func_copy; /* to avoid relookups */
  45758. duk_activation *act;
  45759. duk_hobject *env;
  45760. duk_jmpbuf our_jmpbuf;
  45761. duk_tval tv_tmp;
  45762. duk_int_t retval = DUK_EXEC_ERROR;
  45763. duk_ret_t rc;
  45764. DUK_ASSERT(thr != NULL);
  45765. DUK_ASSERT(ctx != NULL);
  45766. DUK_ASSERT(num_stack_args >= 0);
  45767. /* XXX: currently NULL allocations are not supported; remove if later allowed */
  45768. DUK_ASSERT(thr->valstack != NULL);
  45769. DUK_ASSERT(thr->callstack != NULL);
  45770. DUK_ASSERT(thr->catchstack != NULL);
  45771. /*
  45772. * Preliminaries, required by setjmp() handler.
  45773. *
  45774. * Must be careful not to throw an unintended error here.
  45775. *
  45776. * Note: careful with indices like '-x'; if 'x' is zero, it
  45777. * refers to valstack_bottom.
  45778. */
  45779. entry_valstack_bottom_index = (duk_size_t) (thr->valstack_bottom - thr->valstack);
  45780. entry_valstack_end = (duk_size_t) (thr->valstack_end - thr->valstack);
  45781. entry_callstack_top = thr->callstack_top;
  45782. entry_catchstack_top = thr->catchstack_top;
  45783. entry_call_recursion_depth = thr->heap->call_recursion_depth;
  45784. entry_curr_thread = thr->heap->curr_thread; /* Note: may be NULL if first call */
  45785. entry_thread_state = thr->state;
  45786. idx_func = duk_normalize_index(ctx, -num_stack_args - 2); /* idx_func must be valid, note: non-throwing! */
  45787. idx_args = idx_func + 2; /* idx_args is not necessarily valid if num_stack_args == 0 (idx_args then equals top) */
  45788. /* Need a setjmp() catchpoint if a protected call OR if we need to
  45789. * do mandatory cleanup.
  45790. */
  45791. need_setjmp = ((call_flags & DUK_CALL_FLAG_PROTECTED) != 0) || (thr->heap->curr_thread != thr);
  45792. DUK_DD(DUK_DDPRINT("duk_handle_call: thr=%p, num_stack_args=%ld, "
  45793. "call_flags=0x%08lx (protected=%ld, ignorerec=%ld, constructor=%ld), need_setjmp=%ld, "
  45794. "valstack_top=%ld, idx_func=%ld, idx_args=%ld, rec_depth=%ld/%ld, "
  45795. "entry_valstack_bottom_index=%ld, entry_callstack_top=%ld, entry_catchstack_top=%ld, "
  45796. "entry_call_recursion_depth=%ld, entry_curr_thread=%p, entry_thread_state=%ld",
  45797. (void *) thr,
  45798. (long) num_stack_args,
  45799. (unsigned long) call_flags,
  45800. (long) ((call_flags & DUK_CALL_FLAG_PROTECTED) != 0 ? 1 : 0),
  45801. (long) ((call_flags & DUK_CALL_FLAG_IGNORE_RECLIMIT) != 0 ? 1 : 0),
  45802. (long) ((call_flags & DUK_CALL_FLAG_CONSTRUCTOR_CALL) != 0 ? 1 : 0),
  45803. (long) need_setjmp,
  45804. (long) duk_get_top(ctx),
  45805. (long) idx_func,
  45806. (long) idx_args,
  45807. (long) thr->heap->call_recursion_depth,
  45808. (long) thr->heap->call_recursion_limit,
  45809. (long) entry_valstack_bottom_index,
  45810. (long) entry_callstack_top,
  45811. (long) entry_catchstack_top,
  45812. (long) entry_call_recursion_depth,
  45813. (void *) entry_curr_thread,
  45814. (long) entry_thread_state));
  45815. /* XXX: Multiple tv_func lookups are now avoided by making a local
  45816. * copy of tv_func. Another approach would be to compute an offset
  45817. * for tv_func from valstack bottom and recomputing the tv_func
  45818. * pointer quickly as valstack + offset instead of calling duk_get_tval().
  45819. */
  45820. if (idx_func < 0 || idx_args < 0) {
  45821. /*
  45822. * Since stack indices are not reliable, we can't do anything useful
  45823. * here. Invoke the existing setjmp catcher, or if it doesn't exist,
  45824. * call the fatal error handler.
  45825. */
  45826. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  45827. }
  45828. /*
  45829. * Setup a setjmp() catchpoint first because even the call setup
  45830. * may fail.
  45831. */
  45832. if (!need_setjmp) {
  45833. DUK_DDD(DUK_DDDPRINT("don't need a setjmp catchpoint"));
  45834. goto handle_call;
  45835. }
  45836. old_jmpbuf_ptr = thr->heap->lj.jmpbuf_ptr;
  45837. thr->heap->lj.jmpbuf_ptr = &our_jmpbuf;
  45838. if (DUK_SETJMP(thr->heap->lj.jmpbuf_ptr->jb) == 0) {
  45839. DUK_DDD(DUK_DDDPRINT("setjmp catchpoint setup complete"));
  45840. goto handle_call;
  45841. }
  45842. /*
  45843. * Error during setup, call, or postprocessing of the call.
  45844. * The error value is in heap->lj.value1.
  45845. *
  45846. * Note: any local variables accessed here must have their value
  45847. * assigned *before* the setjmp() call, OR they must be declared
  45848. * volatile. Otherwise their value is not guaranteed to be correct.
  45849. *
  45850. * The following are such variables:
  45851. * - duk_handle_call() parameters
  45852. * - entry_*
  45853. * - idx_func
  45854. * - idx_args
  45855. *
  45856. * The very first thing we do is restore the previous setjmp catcher.
  45857. * This means that any error in error handling will propagate outwards
  45858. * instead of causing a setjmp() re-entry above. The *only* actual
  45859. * errors that should happen here are allocation errors.
  45860. */
  45861. DUK_DDD(DUK_DDDPRINT("error caught during protected duk_handle_call(): %!T",
  45862. (duk_tval *) &thr->heap->lj.value1));
  45863. DUK_ASSERT(thr->heap->lj.type == DUK_LJ_TYPE_THROW);
  45864. DUK_ASSERT(thr->callstack_top >= entry_callstack_top);
  45865. DUK_ASSERT(thr->catchstack_top >= entry_catchstack_top);
  45866. /*
  45867. * Restore previous setjmp catchpoint
  45868. */
  45869. /* Note: either pointer may be NULL (at entry), so don't assert */
  45870. DUK_DDD(DUK_DDDPRINT("restore jmpbuf_ptr: %p -> %p",
  45871. (void *) (thr && thr->heap ? thr->heap->lj.jmpbuf_ptr : NULL),
  45872. (void *) old_jmpbuf_ptr));
  45873. thr->heap->lj.jmpbuf_ptr = old_jmpbuf_ptr;
  45874. if (!(call_flags & DUK_CALL_FLAG_PROTECTED)) {
  45875. /*
  45876. * Caller did not request a protected call but a setjmp
  45877. * catchpoint was set up to allow cleanup. So, clean up
  45878. * and rethrow.
  45879. *
  45880. * We must restore curr_thread here to ensure that its
  45881. * current value doesn't end up pointing to a thread object
  45882. * which has been freed. This is now a problem because some
  45883. * call sites (namely duk_safe_call()) *first* unwind stacks
  45884. * and only then deal with curr_thread. If those call sites
  45885. * were fixed, this wouldn't matter here.
  45886. *
  45887. * Note: this case happens e.g. when heap->curr_thread is
  45888. * NULL on entry.
  45889. */
  45890. DUK_DDD(DUK_DDDPRINT("call is not protected -> clean up and rethrow"));
  45891. DUK_HEAP_SWITCH_THREAD(thr->heap, entry_curr_thread); /* may be NULL */
  45892. thr->state = entry_thread_state;
  45893. DUK_ASSERT((thr->state == DUK_HTHREAD_STATE_INACTIVE && thr->heap->curr_thread == NULL) || /* first call */
  45894. (thr->state == DUK_HTHREAD_STATE_INACTIVE && thr->heap->curr_thread != NULL) || /* other call */
  45895. (thr->state == DUK_HTHREAD_STATE_RUNNING && thr->heap->curr_thread == thr)); /* current thread */
  45896. /* XXX: should setjmp catcher be responsible for this instead? */
  45897. thr->heap->call_recursion_depth = entry_call_recursion_depth;
  45898. duk_err_longjmp(thr);
  45899. DUK_UNREACHABLE();
  45900. }
  45901. duk_hthread_catchstack_unwind(thr, entry_catchstack_top);
  45902. duk_hthread_callstack_unwind(thr, entry_callstack_top);
  45903. thr->valstack_bottom = thr->valstack + entry_valstack_bottom_index;
  45904. /* [ ... func this (crud) errobj ] */
  45905. /* XXX: is there space? better implementation: write directly over
  45906. * 'func' slot to avoid valstack grow issues.
  45907. */
  45908. duk_push_tval(ctx, &thr->heap->lj.value1);
  45909. /* [ ... func this (crud) errobj ] */
  45910. duk_replace(ctx, idx_func);
  45911. duk_set_top(ctx, idx_func + 1);
  45912. /* [ ... errobj ] */
  45913. /* Ensure there is internal valstack spare before we exit; this may
  45914. * throw an alloc error. The same guaranteed size must be available
  45915. * as before the call. This is not optimal now: we store the valstack
  45916. * allocated size during entry; this value may be higher than the
  45917. * minimal guarantee for an application.
  45918. */
  45919. (void) duk_valstack_resize_raw((duk_context *) thr,
  45920. entry_valstack_end, /* same as during entry */
  45921. DUK_VSRESIZE_FLAG_SHRINK | /* flags */
  45922. DUK_VSRESIZE_FLAG_COMPACT |
  45923. DUK_VSRESIZE_FLAG_THROW);
  45924. /* Note: currently a second setjmp restoration is done at the target;
  45925. * this is OK, but could be refactored away.
  45926. */
  45927. retval = DUK_EXEC_ERROR;
  45928. goto shrink_and_finished;
  45929. handle_call:
  45930. /*
  45931. * Thread state check and book-keeping.
  45932. */
  45933. if (thr == thr->heap->curr_thread) {
  45934. /* same thread */
  45935. if (thr->state != DUK_HTHREAD_STATE_RUNNING) {
  45936. /* should actually never happen, but check anyway */
  45937. goto thread_state_error;
  45938. }
  45939. } else {
  45940. /* different thread */
  45941. DUK_ASSERT(thr->heap->curr_thread == NULL ||
  45942. thr->heap->curr_thread->state == DUK_HTHREAD_STATE_RUNNING);
  45943. if (thr->state != DUK_HTHREAD_STATE_INACTIVE) {
  45944. goto thread_state_error;
  45945. }
  45946. DUK_HEAP_SWITCH_THREAD(thr->heap, thr);
  45947. thr->state = DUK_HTHREAD_STATE_RUNNING;
  45948. /* Note: multiple threads may be simultaneously in the RUNNING
  45949. * state, but not in the same "resume chain".
  45950. */
  45951. }
  45952. DUK_ASSERT(thr->heap->curr_thread == thr);
  45953. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING);
  45954. /*
  45955. * C call recursion depth check, which provides a reasonable upper
  45956. * bound on maximum C stack size (arbitrary C stack growth is only
  45957. * possible by recursive handle_call / handle_safe_call calls).
  45958. */
  45959. DUK_ASSERT(thr->heap->call_recursion_depth >= 0);
  45960. DUK_ASSERT(thr->heap->call_recursion_depth <= thr->heap->call_recursion_limit);
  45961. if (call_flags & DUK_CALL_FLAG_IGNORE_RECLIMIT) {
  45962. DUK_DD(DUK_DDPRINT("ignoring reclimit for this call (probably an errhandler call)"));
  45963. } else {
  45964. if (thr->heap->call_recursion_depth >= thr->heap->call_recursion_limit) {
  45965. /* XXX: error message is a bit misleading: we reached a recursion
  45966. * limit which is also essentially the same as a C callstack limit
  45967. * (except perhaps with some relaxed threading assumptions).
  45968. */
  45969. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_C_CALLSTACK_LIMIT);
  45970. }
  45971. thr->heap->call_recursion_depth++;
  45972. }
  45973. /*
  45974. * Check the function type, handle bound function chains, and prepare
  45975. * parameters for the rest of the call handling. Also figure out the
  45976. * effective 'this' binding, which replaces the current value at
  45977. * idx_func + 1.
  45978. *
  45979. * If the target function is a 'bound' one, follow the chain of 'bound'
  45980. * functions until a non-bound function is found. During this process,
  45981. * bound arguments are 'prepended' to existing ones, and the "this"
  45982. * binding is overridden. See E5 Section 15.3.4.5.1.
  45983. *
  45984. * Lightfunc detection happens here too. Note that lightweight functions
  45985. * can be wrapped by (non-lightweight) bound functions so we must resolve
  45986. * the bound function chain first.
  45987. */
  45988. func = duk__nonbound_func_lookup(ctx, idx_func, &num_stack_args, &tv_func, call_flags);
  45989. DUK_TVAL_SET_TVAL(&tv_func_copy, tv_func);
  45990. tv_func = &tv_func_copy; /* local copy to avoid relookups */
  45991. DUK_ASSERT(func == NULL || !DUK_HOBJECT_HAS_BOUND(func));
  45992. DUK_ASSERT(func == NULL || (DUK_HOBJECT_IS_COMPILEDFUNCTION(func) ||
  45993. DUK_HOBJECT_IS_NATIVEFUNCTION(func)));
  45994. duk__coerce_effective_this_binding(thr, func, idx_func + 1);
  45995. DUK_DDD(DUK_DDDPRINT("effective 'this' binding is: %!T",
  45996. (duk_tval *) duk_get_tval(ctx, idx_func + 1)));
  45997. /* These base values are never used, but if the compiler doesn't know
  45998. * that DUK_ERROR() won't return, these are needed to silence warnings.
  45999. * On the other hand, scan-build will warn about the values not being
  46000. * used, so add a DUK_UNREF.
  46001. */
  46002. nargs = 0; DUK_UNREF(nargs);
  46003. nregs = 0; DUK_UNREF(nregs);
  46004. if (func == NULL) {
  46005. duk_small_uint_t lf_flags;
  46006. DUK_DDD(DUK_DDDPRINT("lightfunc call handling"));
  46007. DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv_func));
  46008. lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv_func);
  46009. nargs = DUK_LFUNC_FLAGS_GET_NARGS(lf_flags);
  46010. if (nargs == DUK_LFUNC_NARGS_VARARGS) {
  46011. nargs = -1; /* vararg */
  46012. }
  46013. nregs = nargs;
  46014. } else if (DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) {
  46015. nargs = ((duk_hcompiledfunction *) func)->nargs;
  46016. nregs = ((duk_hcompiledfunction *) func)->nregs;
  46017. DUK_ASSERT(nregs >= nargs);
  46018. } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(func)) {
  46019. /* Note: nargs (and nregs) may be negative for a native,
  46020. * function, which indicates that the function wants the
  46021. * input stack "as is" (i.e. handles "vararg" arguments).
  46022. */
  46023. nargs = ((duk_hnativefunction *) func)->nargs;
  46024. nregs = nargs;
  46025. } else {
  46026. /* XXX: this should be an assert */
  46027. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_CALLABLE);
  46028. }
  46029. /* [ ... func this arg1 ... argN ] */
  46030. /*
  46031. * Setup a preliminary activation.
  46032. *
  46033. * Don't touch valstack_bottom or valstack_top yet so that Duktape API
  46034. * calls work normally.
  46035. */
  46036. duk_hthread_callstack_grow(thr);
  46037. if (thr->callstack_top > 0) {
  46038. /*
  46039. * Update idx_retval of current activation.
  46040. *
  46041. * Although it might seem this is not necessary (bytecode executor
  46042. * does this for Ecmascript-to-Ecmascript calls; other calls are
  46043. * handled here), this turns out to be necessary for handling yield
  46044. * and resume. For them, an Ecmascript-to-native call happens, and
  46045. * the Ecmascript call's idx_retval must be set for things to work.
  46046. */
  46047. (thr->callstack + thr->callstack_top - 1)->idx_retval = entry_valstack_bottom_index + idx_func;
  46048. }
  46049. DUK_ASSERT(thr->callstack_top < thr->callstack_size);
  46050. act = thr->callstack + thr->callstack_top;
  46051. thr->callstack_top++;
  46052. DUK_ASSERT(thr->callstack_top <= thr->callstack_size);
  46053. DUK_ASSERT(thr->valstack_top > thr->valstack_bottom); /* at least effective 'this' */
  46054. DUK_ASSERT(func == NULL || !DUK_HOBJECT_HAS_BOUND(func));
  46055. act->flags = 0;
  46056. if (func == NULL || DUK_HOBJECT_HAS_STRICT(func)) {
  46057. act->flags |= DUK_ACT_FLAG_STRICT;
  46058. }
  46059. if (call_flags & DUK_CALL_FLAG_CONSTRUCTOR_CALL) {
  46060. act->flags |= DUK_ACT_FLAG_CONSTRUCT;
  46061. /*act->flags |= DUK_ACT_FLAG_PREVENT_YIELD;*/
  46062. }
  46063. if (func == NULL || DUK_HOBJECT_IS_NATIVEFUNCTION(func)) {
  46064. /*act->flags |= DUK_ACT_FLAG_PREVENT_YIELD;*/
  46065. }
  46066. if (call_flags & DUK_CALL_FLAG_DIRECT_EVAL) {
  46067. act->flags |= DUK_ACT_FLAG_DIRECT_EVAL;
  46068. }
  46069. /* As a first approximation, all calls except Ecmascript-to-Ecmascript
  46070. * calls prevent a yield.
  46071. */
  46072. act->flags |= DUK_ACT_FLAG_PREVENT_YIELD;
  46073. act->func = func; /* NULL for lightfunc */
  46074. act->var_env = NULL;
  46075. act->lex_env = NULL;
  46076. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  46077. act->prev_caller = NULL;
  46078. #endif
  46079. act->pc = 0;
  46080. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  46081. act->prev_line = 0;
  46082. #endif
  46083. act->idx_bottom = entry_valstack_bottom_index + idx_args;
  46084. #if 0 /* topmost activation idx_retval is considered garbage, no need to init */
  46085. act->idx_retval = 0;
  46086. #endif
  46087. DUK_TVAL_SET_TVAL(&act->tv_func, tv_func); /* borrowed, no refcount */
  46088. if (act->flags & DUK_ACT_FLAG_PREVENT_YIELD) {
  46089. /* duk_hthread_callstack_unwind() will decrease this on unwind */
  46090. thr->callstack_preventcount++;
  46091. }
  46092. /* XXX: Is this INCREF necessary? 'func' is always a borrowed
  46093. * reference reachable through the value stack? If changed, stack
  46094. * unwind code also needs to be fixed to match.
  46095. */
  46096. DUK_HOBJECT_INCREF_ALLOWNULL(thr, func); /* act->func */
  46097. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  46098. if (func) {
  46099. duk__update_func_caller_prop(thr, func);
  46100. }
  46101. act = thr->callstack + thr->callstack_top - 1;
  46102. #endif
  46103. /* [... func this arg1 ... argN] */
  46104. /*
  46105. * Environment record creation and 'arguments' object creation.
  46106. * Named function expression name binding is handled by the
  46107. * compiler; the compiled function's parent env will contain
  46108. * the (immutable) binding already.
  46109. *
  46110. * This handling is now identical for C and Ecmascript functions.
  46111. * C functions always have the 'NEWENV' flag set, so their
  46112. * environment record initialization is delayed (which is good).
  46113. *
  46114. * Delayed creation (on demand) is handled in duk_js_var.c.
  46115. */
  46116. DUK_ASSERT(func == NULL || !DUK_HOBJECT_HAS_BOUND(func)); /* bound function chain has already been resolved */
  46117. if (func != NULL && !DUK_HOBJECT_HAS_NEWENV(func)) {
  46118. /* use existing env (e.g. for non-strict eval); cannot have
  46119. * an own 'arguments' object (but can refer to the existing one)
  46120. */
  46121. DUK_ASSERT(!DUK_HOBJECT_HAS_CREATEARGS(func));
  46122. duk__handle_oldenv_for_call(thr, func, act);
  46123. DUK_ASSERT(act->lex_env != NULL);
  46124. DUK_ASSERT(act->var_env != NULL);
  46125. goto env_done;
  46126. }
  46127. DUK_ASSERT(func == NULL || DUK_HOBJECT_HAS_NEWENV(func));
  46128. if (func == NULL || !DUK_HOBJECT_HAS_CREATEARGS(func)) {
  46129. /* no need to create environment record now; leave as NULL */
  46130. DUK_ASSERT(act->lex_env == NULL);
  46131. DUK_ASSERT(act->var_env == NULL);
  46132. goto env_done;
  46133. }
  46134. /* third arg: absolute index (to entire valstack) of idx_bottom of new activation */
  46135. env = duk_create_activation_environment_record(thr, func, act->idx_bottom);
  46136. DUK_ASSERT(env != NULL);
  46137. /* [... func this arg1 ... argN envobj] */
  46138. DUK_ASSERT(DUK_HOBJECT_HAS_CREATEARGS(func));
  46139. duk__handle_createargs_for_call(thr, func, env, num_stack_args);
  46140. /* [... func this arg1 ... argN envobj] */
  46141. act->lex_env = env;
  46142. act->var_env = env;
  46143. DUK_HOBJECT_INCREF(thr, env);
  46144. DUK_HOBJECT_INCREF(thr, env); /* XXX: incref by count (2) directly */
  46145. duk_pop(ctx);
  46146. env_done:
  46147. /* [... func this arg1 ... argN] */
  46148. /*
  46149. * Setup value stack: clamp to 'nargs', fill up to 'nregs'
  46150. *
  46151. * Value stack may either grow or shrink, depending on the
  46152. * number of func registers and the number of actual arguments.
  46153. * If nregs >= 0, func wants args clamped to 'nargs'; else it
  46154. * wants all args (= 'num_stack_args').
  46155. */
  46156. duk__adjust_valstack_and_top(thr,
  46157. num_stack_args,
  46158. idx_args,
  46159. nregs,
  46160. nargs,
  46161. func);
  46162. /*
  46163. * Determine call type; then setup activation and call
  46164. */
  46165. if (func != NULL && DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) {
  46166. goto ecmascript_call;
  46167. } else {
  46168. goto native_call;
  46169. }
  46170. DUK_UNREACHABLE();
  46171. /*
  46172. * Native (C) call
  46173. */
  46174. native_call:
  46175. /*
  46176. * Shift to new valstack_bottom.
  46177. */
  46178. thr->valstack_bottom = thr->valstack_bottom + idx_args;
  46179. /* keep current valstack_top */
  46180. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  46181. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  46182. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  46183. DUK_ASSERT(func == NULL || ((duk_hnativefunction *) func)->func != NULL);
  46184. /* [... func this | arg1 ... argN] ('this' must precede new bottom) */
  46185. /*
  46186. * Actual function call and return value check.
  46187. *
  46188. * Return values:
  46189. * 0 success, no return value (default to 'undefined')
  46190. * 1 success, one return value on top of stack
  46191. * < 0 error, throw a "magic" error
  46192. * other invalid
  46193. */
  46194. if (func) {
  46195. rc = ((duk_hnativefunction *) func)->func((duk_context *) thr);
  46196. } else {
  46197. duk_c_function funcptr = DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv_func);
  46198. rc = funcptr((duk_context *) thr);
  46199. }
  46200. if (rc < 0) {
  46201. duk_error_throw_from_negative_rc(thr, rc);
  46202. DUK_UNREACHABLE();
  46203. } else if (rc > 1) {
  46204. DUK_ERROR(thr, DUK_ERR_API_ERROR, "c function returned invalid rc");
  46205. }
  46206. DUK_ASSERT(rc == 0 || rc == 1);
  46207. /*
  46208. * Unwind stack(s) and shift back to old valstack_bottom.
  46209. */
  46210. DUK_ASSERT(thr->catchstack_top == entry_catchstack_top);
  46211. DUK_ASSERT(thr->callstack_top == entry_callstack_top + 1);
  46212. #if 0 /* should be no need to unwind */
  46213. duk_hthread_catchstack_unwind(thr, entry_catchstack_top);
  46214. #endif
  46215. duk_hthread_callstack_unwind(thr, entry_callstack_top);
  46216. thr->valstack_bottom = thr->valstack + entry_valstack_bottom_index;
  46217. /* keep current valstack_top */
  46218. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  46219. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  46220. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  46221. DUK_ASSERT(thr->valstack_top - thr->valstack_bottom >= idx_func + 1);
  46222. /*
  46223. * Manipulate value stack so that return value is on top
  46224. * (pushing an 'undefined' if necessary).
  46225. */
  46226. /* XXX: should this happen in the callee's activation or after unwinding? */
  46227. if (rc == 0) {
  46228. duk_require_stack(ctx, 1);
  46229. duk_push_undefined(ctx);
  46230. }
  46231. /* [... func this (crud) retval] */
  46232. DUK_DDD(DUK_DDDPRINT("native call retval -> %!T (rc=%ld)",
  46233. (duk_tval *) duk_get_tval(ctx, -1), (long) rc));
  46234. duk_replace(ctx, idx_func);
  46235. duk_set_top(ctx, idx_func + 1);
  46236. /* [... retval] */
  46237. /* Ensure there is internal valstack spare before we exit; this may
  46238. * throw an alloc error. The same guaranteed size must be available
  46239. * as before the call. This is not optimal now: we store the valstack
  46240. * allocated size during entry; this value may be higher than the
  46241. * minimal guarantee for an application.
  46242. */
  46243. (void) duk_valstack_resize_raw((duk_context *) thr,
  46244. entry_valstack_end, /* same as during entry */
  46245. DUK_VSRESIZE_FLAG_SHRINK | /* flags */
  46246. DUK_VSRESIZE_FLAG_COMPACT |
  46247. DUK_VSRESIZE_FLAG_THROW);
  46248. /*
  46249. * Shrink checks and return with success.
  46250. */
  46251. retval = DUK_EXEC_SUCCESS;
  46252. goto shrink_and_finished;
  46253. /*
  46254. * Ecmascript call
  46255. */
  46256. ecmascript_call:
  46257. /*
  46258. * Shift to new valstack_bottom.
  46259. */
  46260. thr->valstack_bottom = thr->valstack_bottom + idx_args;
  46261. /* keep current valstack_top */
  46262. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  46263. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  46264. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  46265. /* [... func this | arg1 ... argN] ('this' must precede new bottom) */
  46266. /*
  46267. * Bytecode executor call.
  46268. *
  46269. * Execute bytecode, handling any recursive function calls and
  46270. * thread resumptions. Returns when execution would return from
  46271. * the entry level activation. When the executor returns, a
  46272. * single return value is left on the stack top.
  46273. *
  46274. * The only possible longjmp() is an error (DUK_LJ_TYPE_THROW),
  46275. * other types are handled internally by the executor.
  46276. *
  46277. */
  46278. DUK_DDD(DUK_DDDPRINT("entering bytecode execution"));
  46279. duk_js_execute_bytecode(thr);
  46280. DUK_DDD(DUK_DDDPRINT("returned from bytecode execution"));
  46281. /*
  46282. * Unwind stack(s) and shift back to old valstack_bottom.
  46283. */
  46284. DUK_ASSERT(thr->callstack_top == entry_callstack_top + 1);
  46285. duk_hthread_catchstack_unwind(thr, entry_catchstack_top);
  46286. duk_hthread_callstack_unwind(thr, entry_callstack_top);
  46287. thr->valstack_bottom = thr->valstack + entry_valstack_bottom_index;
  46288. /* keep current valstack_top */
  46289. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  46290. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  46291. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  46292. DUK_ASSERT(thr->valstack_top - thr->valstack_bottom >= idx_func + 1);
  46293. /*
  46294. * Manipulate value stack so that return value is on top.
  46295. */
  46296. /* [... func this (crud) retval] */
  46297. duk_replace(ctx, idx_func);
  46298. duk_set_top(ctx, idx_func + 1);
  46299. /* [... retval] */
  46300. /* Ensure there is internal valstack spare before we exit; this may
  46301. * throw an alloc error. The same guaranteed size must be available
  46302. * as before the call. This is not optimal now: we store the valstack
  46303. * allocated size during entry; this value may be higher than the
  46304. * minimal guarantee for an application.
  46305. */
  46306. (void) duk_valstack_resize_raw((duk_context *) thr,
  46307. entry_valstack_end, /* same as during entry */
  46308. DUK_VSRESIZE_FLAG_SHRINK | /* flags */
  46309. DUK_VSRESIZE_FLAG_COMPACT |
  46310. DUK_VSRESIZE_FLAG_THROW);
  46311. /*
  46312. * Shrink checks and return with success.
  46313. */
  46314. retval = DUK_EXEC_SUCCESS;
  46315. goto shrink_and_finished;
  46316. shrink_and_finished:
  46317. #if defined(DUK_OPT_FASTINT)
  46318. /* Explicit check for fastint downgrade. */
  46319. {
  46320. duk_tval *tv_fi;
  46321. tv_fi = duk_get_tval(ctx, -1);
  46322. DUK_ASSERT(tv_fi != NULL);
  46323. DUK_TVAL_CHKFAST_INPLACE(tv_fi);
  46324. }
  46325. #endif
  46326. /* these are "soft" shrink checks, whose failures are ignored */
  46327. /* XXX: would be nice if fast path was inlined */
  46328. duk_hthread_catchstack_shrink_check(thr);
  46329. duk_hthread_callstack_shrink_check(thr);
  46330. goto finished;
  46331. finished:
  46332. if (need_setjmp) {
  46333. /* Note: either pointer may be NULL (at entry), so don't assert;
  46334. * this is now done potentially twice, which is OK
  46335. */
  46336. DUK_DDD(DUK_DDDPRINT("restore jmpbuf_ptr: %p -> %p (possibly already done)",
  46337. (void *) (thr && thr->heap ? thr->heap->lj.jmpbuf_ptr : NULL),
  46338. (void *) old_jmpbuf_ptr));
  46339. thr->heap->lj.jmpbuf_ptr = old_jmpbuf_ptr;
  46340. /* These are just convenience "wiping" of state */
  46341. thr->heap->lj.type = DUK_LJ_TYPE_UNKNOWN;
  46342. thr->heap->lj.iserror = 0;
  46343. /* Side effects should not be an issue here: tv_tmp is local and
  46344. * thr->heap (and thr->heap->lj) have a stable pointer. Finalizer
  46345. * runs etc capture even out-of-memory errors so nothing should
  46346. * throw here.
  46347. */
  46348. DUK_TVAL_SET_TVAL(&tv_tmp, &thr->heap->lj.value1);
  46349. DUK_TVAL_SET_UNDEFINED_UNUSED(&thr->heap->lj.value1);
  46350. DUK_TVAL_DECREF(thr, &tv_tmp);
  46351. DUK_TVAL_SET_TVAL(&tv_tmp, &thr->heap->lj.value2);
  46352. DUK_TVAL_SET_UNDEFINED_UNUSED(&thr->heap->lj.value2);
  46353. DUK_TVAL_DECREF(thr, &tv_tmp);
  46354. DUK_DDD(DUK_DDDPRINT("setjmp catchpoint torn down"));
  46355. }
  46356. DUK_HEAP_SWITCH_THREAD(thr->heap, entry_curr_thread); /* may be NULL */
  46357. thr->state = (duk_uint8_t) entry_thread_state;
  46358. DUK_ASSERT((thr->state == DUK_HTHREAD_STATE_INACTIVE && thr->heap->curr_thread == NULL) || /* first call */
  46359. (thr->state == DUK_HTHREAD_STATE_INACTIVE && thr->heap->curr_thread != NULL) || /* other call */
  46360. (thr->state == DUK_HTHREAD_STATE_RUNNING && thr->heap->curr_thread == thr)); /* current thread */
  46361. thr->heap->call_recursion_depth = entry_call_recursion_depth;
  46362. return retval;
  46363. thread_state_error:
  46364. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "invalid thread state for call (%ld)", (long) thr->state);
  46365. DUK_UNREACHABLE();
  46366. return DUK_EXEC_ERROR; /* never executed */
  46367. }
  46368. /*
  46369. * Manipulate value stack so that exactly 'num_stack_rets' return
  46370. * values are at 'idx_retbase' in every case, assuming there are
  46371. * 'rc' return values on top of stack.
  46372. *
  46373. * This is a bit tricky, because the called C function operates in
  46374. * the same activation record and may have e.g. popped the stack
  46375. * empty (below idx_retbase).
  46376. */
  46377. 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) {
  46378. duk_context *ctx = (duk_context *) thr;
  46379. duk_idx_t idx_rcbase;
  46380. DUK_ASSERT(thr != NULL);
  46381. DUK_ASSERT(idx_retbase >= 0);
  46382. DUK_ASSERT(num_stack_rets >= 0);
  46383. DUK_ASSERT(num_actual_rets >= 0);
  46384. idx_rcbase = duk_get_top(ctx) - num_actual_rets; /* base of known return values */
  46385. DUK_DDD(DUK_DDDPRINT("adjust valstack after func call: "
  46386. "num_stack_rets=%ld, num_actual_rets=%ld, stack_top=%ld, idx_retbase=%ld, idx_rcbase=%ld",
  46387. (long) num_stack_rets, (long) num_actual_rets, (long) duk_get_top(ctx),
  46388. (long) idx_retbase, (long) idx_rcbase));
  46389. DUK_ASSERT(idx_rcbase >= 0); /* caller must check */
  46390. /* ensure space for final configuration (idx_retbase + num_stack_rets) and
  46391. * intermediate configurations
  46392. */
  46393. duk_require_stack_top(ctx,
  46394. (idx_rcbase > idx_retbase ? idx_rcbase : idx_retbase) +
  46395. num_stack_rets);
  46396. /* chop extra retvals away / extend with undefined */
  46397. duk_set_top(ctx, idx_rcbase + num_stack_rets);
  46398. if (idx_rcbase >= idx_retbase) {
  46399. duk_idx_t count = idx_rcbase - idx_retbase;
  46400. duk_idx_t i;
  46401. DUK_DDD(DUK_DDDPRINT("elements at/after idx_retbase have enough to cover func retvals "
  46402. "(idx_retbase=%ld, idx_rcbase=%ld)", (long) idx_retbase, (long) idx_rcbase));
  46403. /* nuke values at idx_retbase to get the first retval (initially
  46404. * at idx_rcbase) to idx_retbase
  46405. */
  46406. DUK_ASSERT(count >= 0);
  46407. for (i = 0; i < count; i++) {
  46408. /* XXX: inefficient; block remove primitive */
  46409. duk_remove(ctx, idx_retbase);
  46410. }
  46411. } else {
  46412. duk_idx_t count = idx_retbase - idx_rcbase;
  46413. duk_idx_t i;
  46414. DUK_DDD(DUK_DDDPRINT("not enough elements at/after idx_retbase to cover func retvals "
  46415. "(idx_retbase=%ld, idx_rcbase=%ld)", (long) idx_retbase, (long) idx_rcbase));
  46416. /* insert 'undefined' values at idx_rcbase to get the
  46417. * return values to idx_retbase
  46418. */
  46419. DUK_ASSERT(count > 0);
  46420. for (i = 0; i < count; i++) {
  46421. /* XXX: inefficient; block insert primitive */
  46422. duk_push_undefined(ctx);
  46423. duk_insert(ctx, idx_rcbase);
  46424. }
  46425. }
  46426. }
  46427. /*
  46428. * Make a "C protected call" within the current activation.
  46429. *
  46430. * The allowed thread states for making a call are the same as for
  46431. * duk_handle_call().
  46432. *
  46433. * Note that like duk_handle_call(), even if this call is protected,
  46434. * there are a few situations where the current (pre-entry) setjmp
  46435. * catcher (or a fatal error handler if no such catcher exists) is
  46436. * invoked:
  46437. *
  46438. * - Blatant API argument errors (e.g. num_stack_args is invalid,
  46439. * so we can't form a reasonable return stack)
  46440. *
  46441. * - Errors during error handling, e.g. failure to reallocate
  46442. * space in the value stack due to an alloc error
  46443. *
  46444. * Such errors propagate outwards, ultimately to the fatal error
  46445. * handler if nothing else.
  46446. */
  46447. /* XXX: bump preventcount by one for the duration of this call? */
  46448. DUK_INTERNAL
  46449. duk_int_t duk_handle_safe_call(duk_hthread *thr,
  46450. duk_safe_call_function func,
  46451. duk_idx_t num_stack_args,
  46452. duk_idx_t num_stack_rets) {
  46453. duk_context *ctx = (duk_context *) thr;
  46454. duk_size_t entry_valstack_bottom_index;
  46455. duk_size_t entry_callstack_top;
  46456. duk_size_t entry_catchstack_top;
  46457. duk_int_t entry_call_recursion_depth;
  46458. duk_hthread *entry_curr_thread;
  46459. duk_uint_fast8_t entry_thread_state;
  46460. duk_jmpbuf *old_jmpbuf_ptr = NULL;
  46461. duk_jmpbuf our_jmpbuf;
  46462. duk_tval tv_tmp;
  46463. duk_idx_t idx_retbase;
  46464. duk_int_t retval;
  46465. duk_ret_t rc;
  46466. DUK_ASSERT(thr != NULL);
  46467. DUK_ASSERT(ctx != NULL);
  46468. /* Note: careful with indices like '-x'; if 'x' is zero, it refers to bottom */
  46469. entry_valstack_bottom_index = (duk_size_t) (thr->valstack_bottom - thr->valstack);
  46470. entry_callstack_top = thr->callstack_top;
  46471. entry_catchstack_top = thr->catchstack_top;
  46472. entry_call_recursion_depth = thr->heap->call_recursion_depth;
  46473. entry_curr_thread = thr->heap->curr_thread; /* Note: may be NULL if first call */
  46474. entry_thread_state = thr->state;
  46475. idx_retbase = duk_get_top(ctx) - num_stack_args; /* Note: not a valid stack index if num_stack_args == 0 */
  46476. /* Note: cannot portably debug print a function pointer, hence 'func' not printed! */
  46477. DUK_DD(DUK_DDPRINT("duk_handle_safe_call: thr=%p, num_stack_args=%ld, num_stack_rets=%ld, "
  46478. "valstack_top=%ld, idx_retbase=%ld, rec_depth=%ld/%ld, "
  46479. "entry_valstack_bottom_index=%ld, entry_callstack_top=%ld, entry_catchstack_top=%ld, "
  46480. "entry_call_recursion_depth=%ld, entry_curr_thread=%p, entry_thread_state=%ld",
  46481. (void *) thr,
  46482. (long) num_stack_args,
  46483. (long) num_stack_rets,
  46484. (long) duk_get_top(ctx),
  46485. (long) idx_retbase,
  46486. (long) thr->heap->call_recursion_depth,
  46487. (long) thr->heap->call_recursion_limit,
  46488. (long) entry_valstack_bottom_index,
  46489. (long) entry_callstack_top,
  46490. (long) entry_catchstack_top,
  46491. (long) entry_call_recursion_depth,
  46492. (void *) entry_curr_thread,
  46493. (long) entry_thread_state));
  46494. if (idx_retbase < 0) {
  46495. /*
  46496. * Since stack indices are not reliable, we can't do anything useful
  46497. * here. Invoke the existing setjmp catcher, or if it doesn't exist,
  46498. * call the fatal error handler.
  46499. */
  46500. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  46501. }
  46502. /* setjmp catchpoint setup */
  46503. old_jmpbuf_ptr = thr->heap->lj.jmpbuf_ptr;
  46504. thr->heap->lj.jmpbuf_ptr = &our_jmpbuf;
  46505. if (DUK_SETJMP(thr->heap->lj.jmpbuf_ptr->jb) == 0) {
  46506. goto handle_call;
  46507. }
  46508. /*
  46509. * Error during call. The error value is at heap->lj.value1.
  46510. *
  46511. * Careful with variable accesses here; must be assigned to before
  46512. * setjmp() or be declared volatile. See duk_handle_call().
  46513. *
  46514. * The following are such variables:
  46515. * - duk_handle_safe_call() parameters
  46516. * - entry_*
  46517. * - idx_retbase
  46518. *
  46519. * The very first thing we do is restore the previous setjmp catcher.
  46520. * This means that any error in error handling will propagate outwards
  46521. * instead of causing a setjmp() re-entry above. The *only* actual
  46522. * errors that should happen here are allocation errors.
  46523. */
  46524. DUK_DDD(DUK_DDDPRINT("error caught during protected duk_handle_safe_call()"));
  46525. DUK_ASSERT(thr->heap->lj.type == DUK_LJ_TYPE_THROW);
  46526. DUK_ASSERT(thr->callstack_top >= entry_callstack_top);
  46527. DUK_ASSERT(thr->catchstack_top >= entry_catchstack_top);
  46528. /* Note: either pointer may be NULL (at entry), so don't assert;
  46529. * these are now restored twice which is OK.
  46530. */
  46531. thr->heap->lj.jmpbuf_ptr = old_jmpbuf_ptr;
  46532. duk_hthread_catchstack_unwind(thr, entry_catchstack_top);
  46533. duk_hthread_callstack_unwind(thr, entry_callstack_top);
  46534. thr->valstack_bottom = thr->valstack + entry_valstack_bottom_index;
  46535. /* [ ... | (crud) ] */
  46536. /* XXX: space in valstack? see discussion in duk_handle_call. */
  46537. duk_push_tval(ctx, &thr->heap->lj.value1);
  46538. /* [ ... | (crud) errobj ] */
  46539. DUK_ASSERT(duk_get_top(ctx) >= 1); /* at least errobj must be on stack */
  46540. /* check that the valstack has space for the final amount and any
  46541. * intermediate space needed; this is unoptimal but should be safe
  46542. */
  46543. duk_require_stack_top(ctx, idx_retbase + num_stack_rets); /* final configuration */
  46544. duk_require_stack(ctx, num_stack_rets);
  46545. duk__safe_call_adjust_valstack(thr, idx_retbase, num_stack_rets, 1); /* 1 = num actual 'return values' */
  46546. /* [ ... | ] or [ ... | errobj (M * undefined)] where M = num_stack_rets - 1 */
  46547. retval = DUK_EXEC_ERROR;
  46548. goto shrink_and_finished;
  46549. /*
  46550. * Handle call (inside setjmp)
  46551. */
  46552. handle_call:
  46553. DUK_DDD(DUK_DDDPRINT("safe_call setjmp catchpoint setup complete"));
  46554. /*
  46555. * Thread state check and book-keeping.
  46556. */
  46557. if (thr == thr->heap->curr_thread) {
  46558. /* same thread */
  46559. if (thr->state != DUK_HTHREAD_STATE_RUNNING) {
  46560. /* should actually never happen, but check anyway */
  46561. goto thread_state_error;
  46562. }
  46563. } else {
  46564. /* different thread */
  46565. DUK_ASSERT(thr->heap->curr_thread == NULL ||
  46566. thr->heap->curr_thread->state == DUK_HTHREAD_STATE_RUNNING);
  46567. if (thr->state != DUK_HTHREAD_STATE_INACTIVE) {
  46568. goto thread_state_error;
  46569. }
  46570. DUK_HEAP_SWITCH_THREAD(thr->heap, thr);
  46571. thr->state = DUK_HTHREAD_STATE_RUNNING;
  46572. /* Note: multiple threads may be simultaneously in the RUNNING
  46573. * state, but not in the same "resume chain".
  46574. */
  46575. }
  46576. DUK_ASSERT(thr->heap->curr_thread == thr);
  46577. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING);
  46578. /*
  46579. * Recursion limit check.
  46580. *
  46581. * Note: there is no need for an "ignore recursion limit" flag
  46582. * for duk_handle_safe_call now.
  46583. */
  46584. DUK_ASSERT(thr->heap->call_recursion_depth >= 0);
  46585. DUK_ASSERT(thr->heap->call_recursion_depth <= thr->heap->call_recursion_limit);
  46586. if (thr->heap->call_recursion_depth >= thr->heap->call_recursion_limit) {
  46587. /* XXX: error message is a bit misleading: we reached a recursion
  46588. * limit which is also essentially the same as a C callstack limit
  46589. * (except perhaps with some relaxed threading assumptions).
  46590. */
  46591. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_C_CALLSTACK_LIMIT);
  46592. }
  46593. thr->heap->call_recursion_depth++;
  46594. /*
  46595. * Valstack spare check
  46596. */
  46597. duk_require_stack(ctx, 0); /* internal spare */
  46598. /*
  46599. * Make the C call
  46600. */
  46601. rc = func(ctx);
  46602. DUK_DDD(DUK_DDDPRINT("safe_call, func rc=%ld", (long) rc));
  46603. /*
  46604. * Valstack manipulation for results
  46605. */
  46606. /* we're running inside the caller's activation, so no change in call/catch stack or valstack bottom */
  46607. DUK_ASSERT(thr->callstack_top == entry_callstack_top);
  46608. DUK_ASSERT(thr->catchstack_top == entry_catchstack_top);
  46609. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  46610. DUK_ASSERT((duk_size_t) (thr->valstack_bottom - thr->valstack) == entry_valstack_bottom_index);
  46611. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  46612. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  46613. if (rc < 0) {
  46614. duk_error_throw_from_negative_rc(thr, rc);
  46615. }
  46616. DUK_ASSERT(rc >= 0);
  46617. if (duk_get_top(ctx) < rc) {
  46618. DUK_ERROR(thr, DUK_ERR_API_ERROR, "not enough stack values for safe_call rc");
  46619. }
  46620. duk__safe_call_adjust_valstack(thr, idx_retbase, num_stack_rets, rc);
  46621. /* Note: no need from callstack / catchstack shrink check */
  46622. retval = DUK_EXEC_SUCCESS;
  46623. goto finished;
  46624. shrink_and_finished:
  46625. /* these are "soft" shrink checks, whose failures are ignored */
  46626. /* XXX: would be nice if fast path was inlined */
  46627. duk_hthread_catchstack_shrink_check(thr);
  46628. duk_hthread_callstack_shrink_check(thr);
  46629. goto finished;
  46630. finished:
  46631. /* Note: either pointer may be NULL (at entry), so don't assert */
  46632. thr->heap->lj.jmpbuf_ptr = old_jmpbuf_ptr;
  46633. /* These are just convenience "wiping" of state */
  46634. thr->heap->lj.type = DUK_LJ_TYPE_UNKNOWN;
  46635. thr->heap->lj.iserror = 0;
  46636. /* Side effects should not be an issue here: tv_tmp is local and
  46637. * thr->heap (and thr->heap->lj) have a stable pointer. Finalizer
  46638. * runs etc capture even out-of-memory errors so nothing should
  46639. * throw here.
  46640. */
  46641. DUK_TVAL_SET_TVAL(&tv_tmp, &thr->heap->lj.value1);
  46642. DUK_TVAL_SET_UNDEFINED_UNUSED(&thr->heap->lj.value1);
  46643. DUK_TVAL_DECREF(thr, &tv_tmp);
  46644. DUK_TVAL_SET_TVAL(&tv_tmp, &thr->heap->lj.value2);
  46645. DUK_TVAL_SET_UNDEFINED_UNUSED(&thr->heap->lj.value2);
  46646. DUK_TVAL_DECREF(thr, &tv_tmp);
  46647. DUK_DDD(DUK_DDDPRINT("setjmp catchpoint torn down"));
  46648. /* XXX: because we unwind stacks above, thr->heap->curr_thread is at
  46649. * risk of pointing to an already freed thread. This was indeed the
  46650. * case in test-bug-multithread-valgrind.c, until duk_handle_call()
  46651. * was fixed to restore thr->heap->curr_thread before rethrowing an
  46652. * uncaught error.
  46653. */
  46654. DUK_HEAP_SWITCH_THREAD(thr->heap, entry_curr_thread); /* may be NULL */
  46655. thr->state = (duk_uint8_t) entry_thread_state;
  46656. DUK_ASSERT((thr->state == DUK_HTHREAD_STATE_INACTIVE && thr->heap->curr_thread == NULL) || /* first call */
  46657. (thr->state == DUK_HTHREAD_STATE_INACTIVE && thr->heap->curr_thread != NULL) || /* other call */
  46658. (thr->state == DUK_HTHREAD_STATE_RUNNING && thr->heap->curr_thread == thr)); /* current thread */
  46659. thr->heap->call_recursion_depth = entry_call_recursion_depth;
  46660. /* stack discipline consistency check */
  46661. DUK_ASSERT(duk_get_top(ctx) == idx_retbase + num_stack_rets);
  46662. return retval;
  46663. thread_state_error:
  46664. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "invalid thread state for safe_call (%ld)", (long) thr->state);
  46665. DUK_UNREACHABLE();
  46666. return DUK_EXEC_ERROR; /* never executed */
  46667. }
  46668. /*
  46669. * Helper for handling an Ecmascript-to-Ecmascript call or an Ecmascript
  46670. * function (initial) Duktape.Thread.resume().
  46671. *
  46672. * Compared to normal calls handled by duk_handle_call(), there are a
  46673. * bunch of differences:
  46674. *
  46675. * - the call is never protected
  46676. * - there is no C recursion depth increase (hence an "ignore recursion
  46677. * limit" flag is not applicable)
  46678. * - instead of making the call, this helper just performs the thread
  46679. * setup and returns; the bytecode executor then restarts execution
  46680. * internally
  46681. * - ecmascript functions are never 'vararg' functions (they access
  46682. * varargs through the 'arguments' object)
  46683. *
  46684. * The callstack of the target contains an earlier Ecmascript call in case
  46685. * of an Ecmascript-to-Ecmascript call (whose idx_retval is updated), or
  46686. * is empty in case of an initial Duktape.Thread.resume().
  46687. *
  46688. * The first thing to do here is to figure out whether an ecma-to-ecma
  46689. * call is actually possible. It's not always the case if the target is
  46690. * a bound function; the final function may be native. In that case,
  46691. * return an error so caller can fall back to a normal call path.
  46692. */
  46693. DUK_INTERNAL
  46694. duk_bool_t duk_handle_ecma_call_setup(duk_hthread *thr,
  46695. duk_idx_t num_stack_args,
  46696. duk_small_uint_t call_flags) {
  46697. duk_context *ctx = (duk_context *) thr;
  46698. duk_size_t entry_valstack_bottom_index;
  46699. duk_idx_t idx_func; /* valstack index of 'func' and retval (relative to entry valstack_bottom) */
  46700. duk_idx_t idx_args; /* valstack index of start of args (arg1) (relative to entry valstack_bottom) */
  46701. duk_idx_t nargs; /* # argument registers target function wants (< 0 => never for ecma calls) */
  46702. duk_idx_t nregs; /* # total registers target function wants on entry (< 0 => never for ecma calls) */
  46703. duk_hobject *func; /* 'func' on stack (borrowed reference) */
  46704. duk_tval *tv_func; /* duk_tval ptr for 'func' on stack (borrowed reference) */
  46705. duk_activation *act;
  46706. duk_hobject *env;
  46707. duk_bool_t use_tailcall;
  46708. DUK_ASSERT(thr != NULL);
  46709. DUK_ASSERT(ctx != NULL);
  46710. DUK_ASSERT(!((call_flags & DUK_CALL_FLAG_IS_RESUME) != 0 && (call_flags & DUK_CALL_FLAG_IS_TAILCALL) != 0));
  46711. /* XXX: assume these? */
  46712. DUK_ASSERT(thr->valstack != NULL);
  46713. DUK_ASSERT(thr->callstack != NULL);
  46714. DUK_ASSERT(thr->catchstack != NULL);
  46715. /* no need to handle thread state book-keeping here */
  46716. DUK_ASSERT((call_flags & DUK_CALL_FLAG_IS_RESUME) != 0 ||
  46717. (thr->state == DUK_HTHREAD_STATE_RUNNING &&
  46718. thr->heap->curr_thread == thr));
  46719. /* if a tailcall:
  46720. * - an Ecmascript activation must be on top of the callstack
  46721. * - there cannot be any active catchstack entries
  46722. */
  46723. #ifdef DUK_USE_ASSERTIONS
  46724. if (call_flags & DUK_CALL_FLAG_IS_TAILCALL) {
  46725. duk_size_t our_callstack_index;
  46726. duk_size_t i;
  46727. DUK_ASSERT(thr->callstack_top >= 1);
  46728. our_callstack_index = thr->callstack_top - 1;
  46729. DUK_ASSERT_DISABLE(our_callstack_index >= 0);
  46730. DUK_ASSERT(our_callstack_index < thr->callstack_size);
  46731. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + our_callstack_index) != NULL);
  46732. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + our_callstack_index)));
  46733. /* No entry in the catchstack which would actually catch a
  46734. * throw can refer to the callstack entry being reused.
  46735. * There *can* be catchstack entries referring to the current
  46736. * callstack entry as long as they don't catch (e.g. label sites).
  46737. */
  46738. for (i = 0; i < thr->catchstack_top; i++) {
  46739. DUK_ASSERT(thr->catchstack[i].callstack_index < our_callstack_index || /* refer to callstack entries below current */
  46740. DUK_CAT_GET_TYPE(thr->catchstack + i) == DUK_CAT_TYPE_LABEL); /* or a non-catching entry */
  46741. }
  46742. }
  46743. #endif /* DUK_USE_ASSERTIONS */
  46744. entry_valstack_bottom_index = (duk_size_t) (thr->valstack_bottom - thr->valstack);
  46745. idx_func = duk_normalize_index(thr, -num_stack_args - 2);
  46746. idx_args = idx_func + 2;
  46747. DUK_DD(DUK_DDPRINT("handle_ecma_call_setup: thr=%p, "
  46748. "num_stack_args=%ld, call_flags=0x%08lx (resume=%ld, tailcall=%ld), "
  46749. "idx_func=%ld, idx_args=%ld, entry_valstack_bottom_index=%ld",
  46750. (void *) thr,
  46751. (long) num_stack_args,
  46752. (unsigned long) call_flags,
  46753. (long) ((call_flags & DUK_CALL_FLAG_IS_RESUME) != 0 ? 1 : 0),
  46754. (long) ((call_flags & DUK_CALL_FLAG_IS_TAILCALL) != 0 ? 1 : 0),
  46755. (long) idx_func,
  46756. (long) idx_args,
  46757. (long) entry_valstack_bottom_index));
  46758. if (idx_func < 0 || idx_args < 0) {
  46759. /* XXX: assert? compiler is responsible for this never happening */
  46760. DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
  46761. }
  46762. /*
  46763. * Check the function type, handle bound function chains, and prepare
  46764. * parameters for the rest of the call handling. Also figure out the
  46765. * effective 'this' binding, which replaces the current value at
  46766. * idx_func + 1.
  46767. *
  46768. * If the target function is a 'bound' one, follow the chain of 'bound'
  46769. * functions until a non-bound function is found. During this process,
  46770. * bound arguments are 'prepended' to existing ones, and the "this"
  46771. * binding is overridden. See E5 Section 15.3.4.5.1.
  46772. *
  46773. * If the final target function cannot be handled by an ecma-to-ecma
  46774. * call, return to the caller with a return value indicating this case.
  46775. * The bound chain is resolved and the caller can resume with a plain
  46776. * function call.
  46777. */
  46778. func = duk__nonbound_func_lookup(ctx, idx_func, &num_stack_args, &tv_func, call_flags);
  46779. if (func == NULL || !DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) {
  46780. DUK_DDD(DUK_DDDPRINT("final target is a lightfunc/nativefunc, cannot do ecma-to-ecma call"));
  46781. return 0;
  46782. }
  46783. /* XXX: tv_func is not actually needed */
  46784. DUK_ASSERT(func != NULL);
  46785. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func));
  46786. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(func));
  46787. duk__coerce_effective_this_binding(thr, func, idx_func + 1);
  46788. DUK_DDD(DUK_DDDPRINT("effective 'this' binding is: %!T",
  46789. duk_get_tval(ctx, idx_func + 1)));
  46790. nargs = ((duk_hcompiledfunction *) func)->nargs;
  46791. nregs = ((duk_hcompiledfunction *) func)->nregs;
  46792. DUK_ASSERT(nregs >= nargs);
  46793. /* [ ... func this arg1 ... argN ] */
  46794. /*
  46795. * Preliminary activation record and valstack manipulation.
  46796. * The concrete actions depend on whether the we're dealing
  46797. * with a tailcall (reuse an existing activation), a resume,
  46798. * or a normal call.
  46799. *
  46800. * The basic actions, in varying order, are:
  46801. *
  46802. * - Check stack size for call handling
  46803. * - Grow call stack if necessary (non-tail-calls)
  46804. * - Update current activation (idx_retval) if necessary
  46805. * (non-tail, non-resume calls)
  46806. * - Move start of args (idx_args) to valstack bottom
  46807. * (tail calls)
  46808. *
  46809. * Don't touch valstack_bottom or valstack_top yet so that Duktape API
  46810. * calls work normally.
  46811. */
  46812. /* XXX: some overlapping code; cleanup */
  46813. use_tailcall = call_flags & DUK_CALL_FLAG_IS_TAILCALL;
  46814. #if !defined(DUK_USE_TAILCALL)
  46815. DUK_ASSERT(use_tailcall == 0); /* compiler ensures this */
  46816. #endif
  46817. if (use_tailcall) {
  46818. /* tailcall cannot be flagged to resume calls, and a
  46819. * previous frame must exist
  46820. */
  46821. DUK_ASSERT(thr->callstack_top >= 1);
  46822. DUK_ASSERT((call_flags & DUK_CALL_FLAG_IS_RESUME) == 0);
  46823. act = thr->callstack + thr->callstack_top - 1;
  46824. if (act->flags & DUK_ACT_FLAG_PREVENT_YIELD) {
  46825. /* See: test-bug-tailcall-preventyield-assert.c. */
  46826. DUK_DDD(DUK_DDDPRINT("tailcall prevented by current activation having DUK_ACT_FLAG_PREVENTYIELD"));
  46827. use_tailcall = 0;
  46828. } else if (DUK_HOBJECT_HAS_NOTAIL(func)) {
  46829. DUK_D(DUK_DPRINT("tailcall prevented by function having a notail flag"));
  46830. use_tailcall = 0;
  46831. }
  46832. }
  46833. if (use_tailcall) {
  46834. duk_tval *tv1, *tv2;
  46835. duk_tval tv_tmp;
  46836. duk_size_t cs_index;
  46837. duk_int_t i_stk; /* must be signed for loop structure */
  46838. duk_idx_t i_arg;
  46839. /*
  46840. * Tailcall handling
  46841. *
  46842. * Although the callstack entry is reused, we need to explicitly unwind
  46843. * the current activation (or simulate an unwind). In particular, the
  46844. * current activation must be closed, otherwise something like
  46845. * test-bug-reduce-judofyr.js results. Also catchstack needs be unwound
  46846. * because there may be non-error-catching label entries in valid tailcalls.
  46847. */
  46848. DUK_DDD(DUK_DDDPRINT("is tailcall, reusing activation at callstack top, at index %ld",
  46849. (long) (thr->callstack_top - 1)));
  46850. /* 'act' already set above */
  46851. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func));
  46852. DUK_ASSERT(!DUK_HOBJECT_HAS_NATIVEFUNCTION(func));
  46853. DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(func));
  46854. DUK_ASSERT((act->flags & DUK_ACT_FLAG_PREVENT_YIELD) == 0);
  46855. /* Unwind catchstack entries referring to the callstack entry we're reusing */
  46856. cs_index = thr->callstack_top - 1;
  46857. DUK_ASSERT(thr->catchstack_top <= DUK_INT_MAX); /* catchstack limits */
  46858. for (i_stk = (duk_int_t) (thr->catchstack_top - 1); i_stk >= 0; i_stk--) {
  46859. duk_catcher *cat = thr->catchstack + i_stk;
  46860. if (cat->callstack_index != cs_index) {
  46861. /* 'i' is the first entry we'll keep */
  46862. break;
  46863. }
  46864. }
  46865. duk_hthread_catchstack_unwind(thr, i_stk + 1);
  46866. /* Unwind the topmost callstack entry before reusing it */
  46867. DUK_ASSERT(thr->callstack_top > 0);
  46868. duk_hthread_callstack_unwind(thr, thr->callstack_top - 1);
  46869. /* Then reuse the unwound activation; callstack was not shrunk so there is always space */
  46870. thr->callstack_top++;
  46871. DUK_ASSERT(thr->callstack_top <= thr->callstack_size);
  46872. act = thr->callstack + thr->callstack_top - 1;
  46873. /* Start filling in the activation */
  46874. act->func = func; /* don't want an intermediate exposed state with func == NULL */
  46875. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  46876. act->prev_caller = NULL;
  46877. #endif
  46878. act->pc = 0; /* don't want an intermediate exposed state with invalid pc */
  46879. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  46880. act->prev_line = 0;
  46881. #endif
  46882. DUK_TVAL_SET_OBJECT(&act->tv_func, func); /* borrowed, no refcount */
  46883. #ifdef DUK_USE_REFERENCE_COUNTING
  46884. DUK_HOBJECT_INCREF(thr, func);
  46885. act = thr->callstack + thr->callstack_top - 1; /* side effects (currently none though) */
  46886. #endif
  46887. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  46888. #ifdef DUK_USE_TAILCALL
  46889. #error incorrect options: tailcalls enabled with function caller property
  46890. #endif
  46891. /* XXX: this doesn't actually work properly for tail calls, so
  46892. * tail calls are disabled when DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  46893. * is in use.
  46894. */
  46895. duk__update_func_caller_prop(thr, func);
  46896. act = thr->callstack + thr->callstack_top - 1;
  46897. #endif
  46898. act->flags = (DUK_HOBJECT_HAS_STRICT(func) ?
  46899. DUK_ACT_FLAG_STRICT | DUK_ACT_FLAG_TAILCALLED :
  46900. DUK_ACT_FLAG_TAILCALLED);
  46901. DUK_ASSERT(DUK_ACT_GET_FUNC(act) == func); /* already updated */
  46902. DUK_ASSERT(act->var_env == NULL); /* already NULLed (by unwind) */
  46903. DUK_ASSERT(act->lex_env == NULL); /* already NULLed (by unwind) */
  46904. DUK_ASSERT(act->pc == 0); /* already zeroed */
  46905. act->idx_bottom = entry_valstack_bottom_index; /* tail call -> reuse current "frame" */
  46906. DUK_ASSERT(nregs >= 0);
  46907. #if 0 /* topmost activation idx_retval is considered garbage, no need to init */
  46908. act->idx_retval = 0;
  46909. #endif
  46910. /*
  46911. * Manipulate valstack so that args are on the current bottom and the
  46912. * previous caller's 'this' binding (which is the value preceding the
  46913. * current bottom) is replaced with the new 'this' binding:
  46914. *
  46915. * [ ... this_old | (crud) func this_new arg1 ... argN ]
  46916. * --> [ ... this_new | arg1 ... argN ]
  46917. *
  46918. * For tailcalling to work properly, the valstack bottom must not grow
  46919. * here; otherwise crud would accumulate on the valstack.
  46920. */
  46921. tv1 = thr->valstack_bottom - 1;
  46922. tv2 = thr->valstack_bottom + idx_func + 1;
  46923. DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top); /* tv1 is -below- valstack_bottom */
  46924. DUK_ASSERT(tv2 >= thr->valstack_bottom && tv2 < thr->valstack_top);
  46925. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  46926. DUK_TVAL_SET_TVAL(tv1, tv2);
  46927. DUK_TVAL_INCREF(thr, tv1);
  46928. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  46929. for (i_arg = 0; i_arg < idx_args; i_arg++) {
  46930. /* XXX: block removal API primitive */
  46931. /* Note: 'func' is popped from valstack here, but it is
  46932. * already reachable from the activation.
  46933. */
  46934. duk_remove(ctx, 0);
  46935. }
  46936. idx_func = 0; DUK_UNREF(idx_func); /* really 'not applicable' anymore, should not be referenced after this */
  46937. idx_args = 0;
  46938. /* [ ... this_new | arg1 ... argN ] */
  46939. } else {
  46940. DUK_DDD(DUK_DDDPRINT("not a tailcall, pushing a new activation to callstack, to index %ld",
  46941. (long) (thr->callstack_top)));
  46942. duk_hthread_callstack_grow(thr);
  46943. if (call_flags & DUK_CALL_FLAG_IS_RESUME) {
  46944. DUK_DDD(DUK_DDDPRINT("is resume -> no update to current activation (may not even exist)"));
  46945. } else {
  46946. DUK_DDD(DUK_DDDPRINT("update to current activation idx_retval"));
  46947. DUK_ASSERT(thr->callstack_top < thr->callstack_size);
  46948. DUK_ASSERT(thr->callstack_top >= 1);
  46949. act = thr->callstack + thr->callstack_top - 1;
  46950. DUK_ASSERT(DUK_ACT_GET_FUNC(act) != NULL);
  46951. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(act)));
  46952. act->idx_retval = entry_valstack_bottom_index + idx_func;
  46953. }
  46954. DUK_ASSERT(thr->callstack_top < thr->callstack_size);
  46955. act = thr->callstack + thr->callstack_top;
  46956. thr->callstack_top++;
  46957. DUK_ASSERT(thr->callstack_top <= thr->callstack_size);
  46958. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func));
  46959. DUK_ASSERT(!DUK_HOBJECT_HAS_NATIVEFUNCTION(func));
  46960. DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(func));
  46961. act->flags = (DUK_HOBJECT_HAS_STRICT(func) ?
  46962. DUK_ACT_FLAG_STRICT :
  46963. 0);
  46964. act->func = func;
  46965. act->var_env = NULL;
  46966. act->lex_env = NULL;
  46967. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  46968. act->prev_caller = NULL;
  46969. #endif
  46970. act->pc = 0;
  46971. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  46972. act->prev_line = 0;
  46973. #endif
  46974. act->idx_bottom = entry_valstack_bottom_index + idx_args;
  46975. DUK_ASSERT(nregs >= 0);
  46976. #if 0 /* topmost activation idx_retval is considered garbage, no need to init */
  46977. act->idx_retval = 0;
  46978. #endif
  46979. DUK_TVAL_SET_OBJECT(&act->tv_func, func); /* borrowed, no refcount */
  46980. DUK_HOBJECT_INCREF(thr, func); /* act->func */
  46981. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  46982. duk__update_func_caller_prop(thr, func);
  46983. act = thr->callstack + thr->callstack_top - 1;
  46984. #endif
  46985. }
  46986. /* [... func this arg1 ... argN] (not tail call)
  46987. * [this | arg1 ... argN] (tail call)
  46988. *
  46989. * idx_args updated to match
  46990. */
  46991. /*
  46992. * Environment record creation and 'arguments' object creation.
  46993. * Named function expression name binding is handled by the
  46994. * compiler; the compiled function's parent env will contain
  46995. * the (immutable) binding already.
  46996. *
  46997. * Delayed creation (on demand) is handled in duk_js_var.c.
  46998. */
  46999. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func)); /* bound function chain has already been resolved */
  47000. if (!DUK_HOBJECT_HAS_NEWENV(func)) {
  47001. /* use existing env (e.g. for non-strict eval); cannot have
  47002. * an own 'arguments' object (but can refer to the existing one)
  47003. */
  47004. duk__handle_oldenv_for_call(thr, func, act);
  47005. DUK_ASSERT(act->lex_env != NULL);
  47006. DUK_ASSERT(act->var_env != NULL);
  47007. goto env_done;
  47008. }
  47009. DUK_ASSERT(DUK_HOBJECT_HAS_NEWENV(func));
  47010. if (!DUK_HOBJECT_HAS_CREATEARGS(func)) {
  47011. /* no need to create environment record now; leave as NULL */
  47012. DUK_ASSERT(act->lex_env == NULL);
  47013. DUK_ASSERT(act->var_env == NULL);
  47014. goto env_done;
  47015. }
  47016. /* third arg: absolute index (to entire valstack) of idx_bottom of new activation */
  47017. env = duk_create_activation_environment_record(thr, func, act->idx_bottom);
  47018. DUK_ASSERT(env != NULL);
  47019. /* [... arg1 ... argN envobj] */
  47020. /* original input stack before nargs/nregs handling must be
  47021. * intact for 'arguments' object
  47022. */
  47023. DUK_ASSERT(DUK_HOBJECT_HAS_CREATEARGS(func));
  47024. duk__handle_createargs_for_call(thr, func, env, num_stack_args);
  47025. /* [... arg1 ... argN envobj] */
  47026. act->lex_env = env;
  47027. act->var_env = env;
  47028. DUK_HOBJECT_INCREF(thr, act->lex_env);
  47029. DUK_HOBJECT_INCREF(thr, act->var_env);
  47030. duk_pop(ctx);
  47031. env_done:
  47032. /* [... arg1 ... argN] */
  47033. /*
  47034. * Setup value stack: clamp to 'nargs', fill up to 'nregs'
  47035. */
  47036. duk__adjust_valstack_and_top(thr,
  47037. num_stack_args,
  47038. idx_args,
  47039. nregs,
  47040. nargs,
  47041. func);
  47042. /*
  47043. * Shift to new valstack_bottom.
  47044. */
  47045. thr->valstack_bottom = thr->valstack_bottom + idx_args;
  47046. /* keep current valstack_top */
  47047. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  47048. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  47049. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  47050. /*
  47051. * Return to bytecode executor, which will resume execution from
  47052. * the topmost activation.
  47053. */
  47054. return 1;
  47055. }
  47056. #line 1 "duk_js_compiler.c"
  47057. /*
  47058. * Ecmascript compiler.
  47059. *
  47060. * Parses an input string and generates a function template result.
  47061. * Compilation may happen in multiple contexts (global code, eval
  47062. * code, function code).
  47063. *
  47064. * The parser uses a traditional top-down recursive parsing for the
  47065. * statement level, and an operator precedence based top-down approach
  47066. * for the expression level. The attempt is to minimize the C stack
  47067. * depth. Bytecode is generated directly without an intermediate
  47068. * representation (tree), at the cost of needing two passes over each
  47069. * function.
  47070. *
  47071. * The top-down recursive parser functions are named "duk__parse_XXX".
  47072. *
  47073. * Recursion limits are in key functions to prevent arbitrary C recursion:
  47074. * function body parsing, statement parsing, and expression parsing.
  47075. *
  47076. * See doc/compiler.txt for discussion on the design.
  47077. *
  47078. * A few typing notes:
  47079. *
  47080. * - duk_regconst_t: unsigned, no marker value for "none"
  47081. * - duk_reg_t: signed, < 0 = none
  47082. * - PC values: duk_int_t, negative values used as markers
  47083. */
  47084. /* include removed: duk_internal.h */
  47085. /* if highest bit of a register number is set, it refers to a constant instead */
  47086. #define DUK__CONST_MARKER DUK_JS_CONST_MARKER
  47087. /* for array and object literals */
  47088. #define DUK__MAX_ARRAY_INIT_VALUES 20
  47089. #define DUK__MAX_OBJECT_INIT_PAIRS 10
  47090. /* XXX: hack, remove when const lookup is not O(n) */
  47091. #define DUK__GETCONST_MAX_CONSTS_CHECK 256
  47092. /* These limits are based on bytecode limits. Max temps is limited
  47093. * by duk_hcompiledfunction nargs/nregs fields being 16 bits.
  47094. */
  47095. #define DUK__MAX_CONSTS DUK_BC_BC_MAX
  47096. #define DUK__MAX_FUNCS DUK_BC_BC_MAX
  47097. #define DUK__MAX_TEMPS 0xffffL
  47098. #define DUK__RECURSION_INCREASE(comp_ctx,thr) do { \
  47099. DUK_DDD(DUK_DDDPRINT("RECURSION INCREASE: %s:%ld", (const char *) DUK_FILE_MACRO, (long) DUK_LINE_MACRO)); \
  47100. duk__recursion_increase((comp_ctx)); \
  47101. } while (0)
  47102. #define DUK__RECURSION_DECREASE(comp_ctx,thr) do { \
  47103. DUK_DDD(DUK_DDDPRINT("RECURSION DECREASE: %s:%ld", (const char *) DUK_FILE_MACRO, (long) DUK_LINE_MACRO)); \
  47104. duk__recursion_decrease((comp_ctx)); \
  47105. } while (0)
  47106. /* Value stack slot limits: these are quite approximate right now, and
  47107. * because they overlap in control flow, some could be eliminated.
  47108. */
  47109. #define DUK__COMPILE_ENTRY_SLOTS 8
  47110. #define DUK__FUNCTION_INIT_REQUIRE_SLOTS 16
  47111. #define DUK__FUNCTION_BODY_REQUIRE_SLOTS 16
  47112. #define DUK__PARSE_STATEMENTS_SLOTS 16
  47113. #define DUK__PARSE_EXPR_SLOTS 16
  47114. /* Temporary structure used to pass a stack allocated region through
  47115. * duk_safe_call().
  47116. */
  47117. typedef struct {
  47118. duk_small_uint_t flags;
  47119. duk_compiler_ctx comp_ctx_alloc;
  47120. duk_lexer_point lex_pt_alloc;
  47121. } duk__compiler_stkstate;
  47122. /*
  47123. * Prototypes
  47124. */
  47125. /* lexing */
  47126. DUK_LOCAL_DECL void duk__advance_helper(duk_compiler_ctx *comp_ctx, duk_small_int_t expect);
  47127. DUK_LOCAL_DECL void duk__advance_expect(duk_compiler_ctx *comp_ctx, duk_small_int_t expect);
  47128. DUK_LOCAL_DECL void duk__advance(duk_compiler_ctx *ctx);
  47129. /* function helpers */
  47130. DUK_LOCAL_DECL void duk__init_func_valstack_slots(duk_compiler_ctx *comp_ctx);
  47131. DUK_LOCAL_DECL void duk__reset_func_for_pass2(duk_compiler_ctx *comp_ctx);
  47132. DUK_LOCAL_DECL void duk__init_varmap_and_prologue_for_pass2(duk_compiler_ctx *comp_ctx, duk_reg_t *out_stmt_value_reg);
  47133. DUK_LOCAL_DECL void duk__convert_to_func_template(duk_compiler_ctx *comp_ctx, duk_bool_t force_no_namebind);
  47134. DUK_LOCAL_DECL duk_int_t duk__cleanup_varmap(duk_compiler_ctx *comp_ctx);
  47135. /* code emission */
  47136. DUK_LOCAL_DECL duk_int_t duk__get_current_pc(duk_compiler_ctx *comp_ctx);
  47137. DUK_LOCAL_DECL duk_compiler_instr *duk__get_instr_ptr(duk_compiler_ctx *comp_ctx, duk_int_t pc);
  47138. DUK_LOCAL_DECL void duk__emit(duk_compiler_ctx *comp_ctx, duk_instr_t ins);
  47139. #if 0 /* unused */
  47140. DUK_LOCAL_DECL void duk__emit_op_only(duk_compiler_ctx *comp_ctx, duk_small_uint_t op);
  47141. #endif
  47142. 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);
  47143. 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);
  47144. #if 0 /* unused */
  47145. DUK_LOCAL_DECL void duk__emit_a(duk_compiler_ctx *comp_ctx, duk_small_uint_t op_flags, duk_regconst_t a);
  47146. #endif
  47147. 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);
  47148. DUK_LOCAL_DECL void duk__emit_abc(duk_compiler_ctx *comp_ctx, duk_small_uint_t op, duk_regconst_t abc);
  47149. 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);
  47150. DUK_LOCAL_DECL void duk__emit_extraop_b(duk_compiler_ctx *comp_ctx, duk_small_uint_t extraop_flags, duk_regconst_t b);
  47151. DUK_LOCAL_DECL void duk__emit_extraop_bc(duk_compiler_ctx *comp_ctx, duk_small_uint_t extraop, duk_regconst_t bc);
  47152. DUK_LOCAL_DECL void duk__emit_extraop_only(duk_compiler_ctx *comp_ctx, duk_small_uint_t extraop_flags);
  47153. DUK_LOCAL_DECL void duk__emit_load_int32(duk_compiler_ctx *comp_ctx, duk_reg_t reg, duk_int32_t val);
  47154. DUK_LOCAL_DECL void duk__emit_load_int32_noshuffle(duk_compiler_ctx *comp_ctx, duk_reg_t reg, duk_int32_t val);
  47155. DUK_LOCAL_DECL void duk__emit_jump(duk_compiler_ctx *comp_ctx, duk_int_t target_pc);
  47156. DUK_LOCAL_DECL duk_int_t duk__emit_jump_empty(duk_compiler_ctx *comp_ctx);
  47157. DUK_LOCAL_DECL void duk__insert_jump_entry(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc);
  47158. DUK_LOCAL_DECL void duk__patch_jump(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc, duk_int_t target_pc);
  47159. DUK_LOCAL_DECL void duk__patch_jump_here(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc);
  47160. DUK_LOCAL_DECL void duk__patch_trycatch(duk_compiler_ctx *comp_ctx, duk_int_t trycatch_pc, duk_regconst_t reg_catch, duk_regconst_t const_varname, duk_small_uint_t flags);
  47161. DUK_LOCAL_DECL void duk__emit_if_false_skip(duk_compiler_ctx *comp_ctx, duk_regconst_t regconst);
  47162. DUK_LOCAL_DECL void duk__emit_if_true_skip(duk_compiler_ctx *comp_ctx, duk_regconst_t regconst);
  47163. DUK_LOCAL_DECL void duk__emit_invalid(duk_compiler_ctx *comp_ctx);
  47164. /* ivalue/ispec helpers */
  47165. DUK_LOCAL_DECL void duk__copy_ispec(duk_compiler_ctx *comp_ctx, duk_ispec *src, duk_ispec *dst);
  47166. DUK_LOCAL_DECL void duk__copy_ivalue(duk_compiler_ctx *comp_ctx, duk_ivalue *src, duk_ivalue *dst);
  47167. DUK_LOCAL_DECL duk_bool_t duk__is_whole_get_int32(duk_double_t x, duk_int32_t *ival);
  47168. DUK_LOCAL_DECL duk_reg_t duk__alloctemps(duk_compiler_ctx *comp_ctx, duk_small_int_t num);
  47169. DUK_LOCAL_DECL duk_reg_t duk__alloctemp(duk_compiler_ctx *comp_ctx);
  47170. DUK_LOCAL_DECL void duk__settemp_checkmax(duk_compiler_ctx *comp_ctx, duk_reg_t temp_next);
  47171. DUK_LOCAL_DECL duk_regconst_t duk__getconst(duk_compiler_ctx *comp_ctx);
  47172. DUK_LOCAL_DECL
  47173. duk_regconst_t duk__ispec_toregconst_raw(duk_compiler_ctx *comp_ctx,
  47174. duk_ispec *x,
  47175. duk_reg_t forced_reg,
  47176. duk_small_uint_t flags);
  47177. DUK_LOCAL_DECL void duk__ispec_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ispec *x, duk_reg_t forced_reg);
  47178. DUK_LOCAL_DECL void duk__ivalue_toplain_raw(duk_compiler_ctx *comp_ctx, duk_ivalue *x, duk_reg_t forced_reg);
  47179. DUK_LOCAL_DECL void duk__ivalue_toplain(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  47180. DUK_LOCAL_DECL void duk__ivalue_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  47181. DUK_LOCAL_DECL
  47182. duk_regconst_t duk__ivalue_toregconst_raw(duk_compiler_ctx *comp_ctx,
  47183. duk_ivalue *x,
  47184. duk_reg_t forced_reg,
  47185. duk_small_uint_t flags);
  47186. DUK_LOCAL_DECL duk_reg_t duk__ivalue_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  47187. #if 0 /* unused */
  47188. DUK_LOCAL_DECL duk_reg_t duk__ivalue_totempreg(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  47189. #endif
  47190. DUK_LOCAL_DECL void duk__ivalue_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ivalue *x, duk_int_t forced_reg);
  47191. DUK_LOCAL_DECL duk_regconst_t duk__ivalue_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  47192. /* identifier handling */
  47193. DUK_LOCAL_DECL duk_reg_t duk__lookup_active_register_binding(duk_compiler_ctx *comp_ctx);
  47194. 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);
  47195. /* label handling */
  47196. 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);
  47197. DUK_LOCAL_DECL void duk__update_label_flags(duk_compiler_ctx *comp_ctx, duk_int_t label_id, duk_small_uint_t flags);
  47198. 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);
  47199. DUK_LOCAL_DECL void duk__reset_labels_to_length(duk_compiler_ctx *comp_ctx, duk_int_t len);
  47200. /* top-down expression parser */
  47201. DUK_LOCAL_DECL void duk__expr_nud(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  47202. DUK_LOCAL_DECL void duk__expr_led(duk_compiler_ctx *comp_ctx, duk_ivalue *left, duk_ivalue *res);
  47203. DUK_LOCAL_DECL duk_small_uint_t duk__expr_lbp(duk_compiler_ctx *comp_ctx);
  47204. DUK_LOCAL_DECL duk_bool_t duk__expr_is_empty(duk_compiler_ctx *comp_ctx);
  47205. /* exprtop is the top level variant which resets nud/led counts */
  47206. DUK_LOCAL_DECL void duk__expr(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  47207. DUK_LOCAL_DECL void duk__exprtop(duk_compiler_ctx *ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  47208. /* convenience helpers */
  47209. DUK_LOCAL_DECL duk_reg_t duk__expr_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  47210. #if 0 /* unused */
  47211. DUK_LOCAL_DECL duk_reg_t duk__expr_totempreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  47212. #endif
  47213. 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);
  47214. DUK_LOCAL_DECL duk_regconst_t duk__expr_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  47215. DUK_LOCAL_DECL void duk__expr_toplain(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  47216. DUK_LOCAL_DECL void duk__expr_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  47217. DUK_LOCAL_DECL duk_reg_t duk__exprtop_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  47218. #if 0 /* unused */
  47219. DUK_LOCAL_DECL duk_reg_t duk__exprtop_totempreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  47220. 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);
  47221. #endif
  47222. DUK_LOCAL_DECL duk_regconst_t duk__exprtop_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  47223. #if 0 /* unused */
  47224. DUK_LOCAL_DECL void duk__exprtop_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  47225. #endif
  47226. /* expression parsing helpers */
  47227. DUK_LOCAL_DECL duk_int_t duk__parse_arguments(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  47228. DUK_LOCAL_DECL void duk__nud_array_literal(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  47229. DUK_LOCAL_DECL void duk__nud_object_literal(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  47230. DUK_LOCAL_DECL duk_bool_t duk__nud_object_literal_key_check(duk_compiler_ctx *comp_ctx, duk_small_uint_t new_key_flags);
  47231. /* statement parsing */
  47232. 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);
  47233. DUK_LOCAL_DECL void duk__parse_var_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  47234. DUK_LOCAL_DECL void duk__parse_for_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site);
  47235. DUK_LOCAL_DECL void duk__parse_switch_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site);
  47236. DUK_LOCAL_DECL void duk__parse_if_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  47237. DUK_LOCAL_DECL void duk__parse_do_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site);
  47238. DUK_LOCAL_DECL void duk__parse_while_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site);
  47239. DUK_LOCAL_DECL void duk__parse_break_or_continue_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  47240. DUK_LOCAL_DECL void duk__parse_return_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  47241. DUK_LOCAL_DECL void duk__parse_throw_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  47242. DUK_LOCAL_DECL void duk__parse_try_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  47243. DUK_LOCAL_DECL void duk__parse_with_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  47244. DUK_LOCAL_DECL void duk__parse_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_bool_t allow_source_elem);
  47245. DUK_LOCAL_DECL duk_int_t duk__stmt_label_site(duk_compiler_ctx *comp_ctx, duk_int_t label_id);
  47246. DUK_LOCAL_DECL void duk__parse_stmts(duk_compiler_ctx *comp_ctx, duk_bool_t allow_source_elem, duk_bool_t expect_eof);
  47247. 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);
  47248. DUK_LOCAL_DECL void duk__parse_func_formals(duk_compiler_ctx *comp_ctx);
  47249. DUK_LOCAL_DECL void duk__parse_func_like_raw(duk_compiler_ctx *comp_ctx, duk_bool_t is_decl, duk_bool_t is_setget);
  47250. 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);
  47251. /*
  47252. * Parser control values for tokens. The token table is ordered by the
  47253. * DUK_TOK_XXX defines.
  47254. *
  47255. * The binding powers are for lbp() use (i.e. for use in led() context).
  47256. * Binding powers are positive for typing convenience, and bits at the
  47257. * top should be reserved for flags. Binding power step must be higher
  47258. * than 1 so that binding power "lbp - 1" can be used for right associative
  47259. * operators. Currently a step of 2 is used (which frees one more bit for
  47260. * flags).
  47261. */
  47262. /* XXX: actually single step levels would work just fine, clean up */
  47263. /* binding power "levels" (see doc/compiler.txt) */
  47264. #define DUK__BP_INVALID 0 /* always terminates led() */
  47265. #define DUK__BP_EOF 2
  47266. #define DUK__BP_CLOSING 4 /* token closes expression, e.g. ')', ']' */
  47267. #define DUK__BP_FOR_EXPR DUK__BP_CLOSING /* bp to use when parsing a top level Expression */
  47268. #define DUK__BP_COMMA 6
  47269. #define DUK__BP_ASSIGNMENT 8
  47270. #define DUK__BP_CONDITIONAL 10
  47271. #define DUK__BP_LOR 12
  47272. #define DUK__BP_LAND 14
  47273. #define DUK__BP_BOR 16
  47274. #define DUK__BP_BXOR 18
  47275. #define DUK__BP_BAND 20
  47276. #define DUK__BP_EQUALITY 22
  47277. #define DUK__BP_RELATIONAL 24
  47278. #define DUK__BP_SHIFT 26
  47279. #define DUK__BP_ADDITIVE 28
  47280. #define DUK__BP_MULTIPLICATIVE 30
  47281. #define DUK__BP_POSTFIX 32
  47282. #define DUK__BP_CALL 34
  47283. #define DUK__BP_MEMBER 36
  47284. #define DUK__TOKEN_LBP_BP_MASK 0x1f
  47285. #define DUK__TOKEN_LBP_FLAG_NO_REGEXP (1 << 5) /* regexp literal must not follow this token */
  47286. #define DUK__TOKEN_LBP_FLAG_TERMINATES (1 << 6) /* terminates expression; e.g. post-increment/-decrement */
  47287. #define DUK__TOKEN_LBP_FLAG_UNUSED (1 << 7) /* spare */
  47288. #define DUK__TOKEN_LBP_GET_BP(x) ((duk_small_uint_t) (((x) & DUK__TOKEN_LBP_BP_MASK) * 2))
  47289. #define DUK__MK_LBP(bp) ((bp) >> 1) /* bp is assumed to be even */
  47290. #define DUK__MK_LBP_FLAGS(bp,flags) (((bp) >> 1) | (flags))
  47291. DUK_LOCAL const duk_uint8_t duk__token_lbp[] = {
  47292. DUK__MK_LBP(DUK__BP_EOF), /* DUK_TOK_EOF */
  47293. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_LINETERM */
  47294. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_COMMENT */
  47295. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_IDENTIFIER */
  47296. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_BREAK */
  47297. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_CASE */
  47298. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_CATCH */
  47299. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_CONTINUE */
  47300. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_DEBUGGER */
  47301. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_DEFAULT */
  47302. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_DELETE */
  47303. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_DO */
  47304. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_ELSE */
  47305. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_FINALLY */
  47306. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_FOR */
  47307. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_FUNCTION */
  47308. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_IF */
  47309. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_IN */
  47310. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_INSTANCEOF */
  47311. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_NEW */
  47312. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_RETURN */
  47313. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_SWITCH */
  47314. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_THIS */
  47315. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_THROW */
  47316. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_TRY */
  47317. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_TYPEOF */
  47318. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_VAR */
  47319. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_VOID */
  47320. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_WHILE */
  47321. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_WITH */
  47322. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_CLASS */
  47323. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_CONST */
  47324. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_ENUM */
  47325. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_EXPORT */
  47326. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_EXTENDS */
  47327. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_IMPORT */
  47328. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_SUPER */
  47329. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_NULL */
  47330. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_TRUE */
  47331. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_FALSE */
  47332. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_GET */
  47333. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_SET */
  47334. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_IMPLEMENTS */
  47335. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_INTERFACE */
  47336. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_LET */
  47337. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_PACKAGE */
  47338. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_PRIVATE */
  47339. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_PROTECTED */
  47340. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_PUBLIC */
  47341. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_STATIC */
  47342. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_YIELD */
  47343. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_LCURLY */
  47344. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_RCURLY */
  47345. DUK__MK_LBP(DUK__BP_MEMBER), /* DUK_TOK_LBRACKET */
  47346. DUK__MK_LBP_FLAGS(DUK__BP_CLOSING, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_RBRACKET */
  47347. DUK__MK_LBP(DUK__BP_CALL), /* DUK_TOK_LPAREN */
  47348. DUK__MK_LBP_FLAGS(DUK__BP_CLOSING, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_RPAREN */
  47349. DUK__MK_LBP(DUK__BP_MEMBER), /* DUK_TOK_PERIOD */
  47350. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_SEMICOLON */
  47351. DUK__MK_LBP(DUK__BP_COMMA), /* DUK_TOK_COMMA */
  47352. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_LT */
  47353. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_GT */
  47354. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_LE */
  47355. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_GE */
  47356. DUK__MK_LBP(DUK__BP_EQUALITY), /* DUK_TOK_EQ */
  47357. DUK__MK_LBP(DUK__BP_EQUALITY), /* DUK_TOK_NEQ */
  47358. DUK__MK_LBP(DUK__BP_EQUALITY), /* DUK_TOK_SEQ */
  47359. DUK__MK_LBP(DUK__BP_EQUALITY), /* DUK_TOK_SNEQ */
  47360. DUK__MK_LBP(DUK__BP_ADDITIVE), /* DUK_TOK_ADD */
  47361. DUK__MK_LBP(DUK__BP_ADDITIVE), /* DUK_TOK_SUB */
  47362. DUK__MK_LBP(DUK__BP_MULTIPLICATIVE), /* DUK_TOK_MUL */
  47363. DUK__MK_LBP(DUK__BP_MULTIPLICATIVE), /* DUK_TOK_DIV */
  47364. DUK__MK_LBP(DUK__BP_MULTIPLICATIVE), /* DUK_TOK_MOD */
  47365. DUK__MK_LBP(DUK__BP_POSTFIX), /* DUK_TOK_INCREMENT */
  47366. DUK__MK_LBP(DUK__BP_POSTFIX), /* DUK_TOK_DECREMENT */
  47367. DUK__MK_LBP(DUK__BP_SHIFT), /* DUK_TOK_ALSHIFT */
  47368. DUK__MK_LBP(DUK__BP_SHIFT), /* DUK_TOK_ARSHIFT */
  47369. DUK__MK_LBP(DUK__BP_SHIFT), /* DUK_TOK_RSHIFT */
  47370. DUK__MK_LBP(DUK__BP_BAND), /* DUK_TOK_BAND */
  47371. DUK__MK_LBP(DUK__BP_BOR), /* DUK_TOK_BOR */
  47372. DUK__MK_LBP(DUK__BP_BXOR), /* DUK_TOK_BXOR */
  47373. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_LNOT */
  47374. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_BNOT */
  47375. DUK__MK_LBP(DUK__BP_LAND), /* DUK_TOK_LAND */
  47376. DUK__MK_LBP(DUK__BP_LOR), /* DUK_TOK_LOR */
  47377. DUK__MK_LBP(DUK__BP_CONDITIONAL), /* DUK_TOK_QUESTION */
  47378. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_COLON */
  47379. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_EQUALSIGN */
  47380. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_ADD_EQ */
  47381. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_SUB_EQ */
  47382. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_MUL_EQ */
  47383. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_DIV_EQ */
  47384. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_MOD_EQ */
  47385. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_ALSHIFT_EQ */
  47386. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_ARSHIFT_EQ */
  47387. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_RSHIFT_EQ */
  47388. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_BAND_EQ */
  47389. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_BOR_EQ */
  47390. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_BXOR_EQ */
  47391. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_NUMBER */
  47392. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_STRING */
  47393. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_REGEXP */
  47394. };
  47395. /*
  47396. * Misc helpers
  47397. */
  47398. DUK_LOCAL void duk__recursion_increase(duk_compiler_ctx *comp_ctx) {
  47399. DUK_ASSERT(comp_ctx != NULL);
  47400. DUK_ASSERT(comp_ctx->recursion_depth >= 0);
  47401. if (comp_ctx->recursion_depth >= comp_ctx->recursion_limit) {
  47402. DUK_ERROR(comp_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_COMPILER_RECURSION_LIMIT);
  47403. }
  47404. comp_ctx->recursion_depth++;
  47405. }
  47406. DUK_LOCAL void duk__recursion_decrease(duk_compiler_ctx *comp_ctx) {
  47407. DUK_ASSERT(comp_ctx != NULL);
  47408. DUK_ASSERT(comp_ctx->recursion_depth > 0);
  47409. comp_ctx->recursion_depth--;
  47410. }
  47411. DUK_LOCAL duk_bool_t duk__hstring_is_eval_or_arguments(duk_compiler_ctx *comp_ctx, duk_hstring *h) {
  47412. DUK_UNREF(comp_ctx);
  47413. DUK_ASSERT(h != NULL);
  47414. return DUK_HSTRING_HAS_EVAL_OR_ARGUMENTS(h);
  47415. }
  47416. DUK_LOCAL duk_bool_t duk__hstring_is_eval_or_arguments_in_strict_mode(duk_compiler_ctx *comp_ctx, duk_hstring *h) {
  47417. DUK_ASSERT(h != NULL);
  47418. return (comp_ctx->curr_func.is_strict &&
  47419. DUK_HSTRING_HAS_EVAL_OR_ARGUMENTS(h));
  47420. }
  47421. /*
  47422. * Parser duk__advance() token eating functions
  47423. */
  47424. /* XXX: valstack handling is awkward. Add a valstack helper which
  47425. * avoids dup():ing; valstack_copy(src, dst)?
  47426. */
  47427. DUK_LOCAL void duk__advance_helper(duk_compiler_ctx *comp_ctx, duk_small_int_t expect) {
  47428. duk_hthread *thr = comp_ctx->thr;
  47429. duk_context *ctx = (duk_context *) thr;
  47430. duk_bool_t regexp;
  47431. DUK_ASSERT(comp_ctx->curr_token.t >= 0 && comp_ctx->curr_token.t <= DUK_TOK_MAXVAL); /* MAXVAL is inclusive */
  47432. /*
  47433. * Use current token to decide whether a RegExp can follow.
  47434. *
  47435. * We can use either 't' or 't_nores'; the latter would not
  47436. * recognize keywords. Some keywords can be followed by a
  47437. * RegExp (e.g. "return"), so using 't' is better. This is
  47438. * not trivial, see doc/compiler.txt.
  47439. */
  47440. regexp = 1;
  47441. if (duk__token_lbp[comp_ctx->curr_token.t] & DUK__TOKEN_LBP_FLAG_NO_REGEXP) {
  47442. regexp = 0;
  47443. }
  47444. if (comp_ctx->curr_func.reject_regexp_in_adv) {
  47445. comp_ctx->curr_func.reject_regexp_in_adv = 0;
  47446. regexp = 0;
  47447. }
  47448. if (expect >= 0 && comp_ctx->curr_token.t != expect) {
  47449. DUK_D(DUK_DPRINT("parse error: expect=%ld, got=%ld",
  47450. (long) expect, (long) comp_ctx->curr_token.t));
  47451. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_PARSE_ERROR);
  47452. }
  47453. /* make current token the previous; need to fiddle with valstack "backing store" */
  47454. DUK_MEMCPY(&comp_ctx->prev_token, &comp_ctx->curr_token, sizeof(duk_token));
  47455. duk_copy(ctx, comp_ctx->tok11_idx, comp_ctx->tok21_idx);
  47456. duk_copy(ctx, comp_ctx->tok12_idx, comp_ctx->tok22_idx);
  47457. /* parse new token */
  47458. duk_lexer_parse_js_input_element(&comp_ctx->lex,
  47459. &comp_ctx->curr_token,
  47460. comp_ctx->curr_func.is_strict,
  47461. regexp);
  47462. DUK_DDD(DUK_DDDPRINT("advance: curr: tok=%ld/%ld,%ld,term=%ld,%!T,%!T "
  47463. "prev: tok=%ld/%ld,%ld,term=%ld,%!T,%!T",
  47464. (long) comp_ctx->curr_token.t,
  47465. (long) comp_ctx->curr_token.t_nores,
  47466. (long) comp_ctx->curr_token.start_line,
  47467. (long) comp_ctx->curr_token.lineterm,
  47468. (duk_tval *) duk_get_tval(ctx, comp_ctx->tok11_idx),
  47469. (duk_tval *) duk_get_tval(ctx, comp_ctx->tok12_idx),
  47470. (long) comp_ctx->prev_token.t,
  47471. (long) comp_ctx->prev_token.t_nores,
  47472. (long) comp_ctx->prev_token.start_line,
  47473. (long) comp_ctx->prev_token.lineterm,
  47474. (duk_tval *) duk_get_tval(ctx, comp_ctx->tok21_idx),
  47475. (duk_tval *) duk_get_tval(ctx, comp_ctx->tok22_idx)));
  47476. }
  47477. /* advance, expecting current token to be a specific token; parse next token in regexp context */
  47478. DUK_LOCAL void duk__advance_expect(duk_compiler_ctx *comp_ctx, duk_small_int_t expect) {
  47479. duk__advance_helper(comp_ctx, expect);
  47480. }
  47481. /* advance, whatever the current token is; parse next token in regexp context */
  47482. DUK_LOCAL void duk__advance(duk_compiler_ctx *comp_ctx) {
  47483. duk__advance_helper(comp_ctx, -1);
  47484. }
  47485. /*
  47486. * Helpers for duk_compiler_func.
  47487. */
  47488. /* init function state: inits valstack allocations */
  47489. DUK_LOCAL void duk__init_func_valstack_slots(duk_compiler_ctx *comp_ctx) {
  47490. duk_compiler_func *func = &comp_ctx->curr_func;
  47491. duk_hthread *thr = comp_ctx->thr;
  47492. duk_context *ctx = (duk_context *) thr;
  47493. duk_idx_t entry_top;
  47494. entry_top = duk_get_top(ctx);
  47495. DUK_MEMZERO(func, sizeof(*func)); /* intentional overlap with earlier memzero */
  47496. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  47497. func->h_name = NULL;
  47498. func->h_code = NULL;
  47499. func->h_consts = NULL;
  47500. func->h_funcs = NULL;
  47501. func->h_decls = NULL;
  47502. func->h_labelnames = NULL;
  47503. func->h_labelinfos = NULL;
  47504. func->h_argnames = NULL;
  47505. func->h_varmap = NULL;
  47506. #endif
  47507. duk_require_stack(ctx, DUK__FUNCTION_INIT_REQUIRE_SLOTS);
  47508. /* XXX: getter for dynamic buffer */
  47509. duk_push_dynamic_buffer(ctx, 0);
  47510. func->code_idx = entry_top + 0;
  47511. func->h_code = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, entry_top + 0);
  47512. DUK_ASSERT(func->h_code != NULL);
  47513. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(func->h_code));
  47514. duk_push_array(ctx);
  47515. func->consts_idx = entry_top + 1;
  47516. func->h_consts = duk_get_hobject(ctx, entry_top + 1);
  47517. DUK_ASSERT(func->h_consts != NULL);
  47518. duk_push_array(ctx);
  47519. func->funcs_idx = entry_top + 2;
  47520. func->h_funcs = duk_get_hobject(ctx, entry_top + 2);
  47521. DUK_ASSERT(func->h_funcs != NULL);
  47522. DUK_ASSERT(func->fnum_next == 0);
  47523. duk_push_array(ctx);
  47524. func->decls_idx = entry_top + 3;
  47525. func->h_decls = duk_get_hobject(ctx, entry_top + 3);
  47526. DUK_ASSERT(func->h_decls != NULL);
  47527. duk_push_array(ctx);
  47528. func->labelnames_idx = entry_top + 4;
  47529. func->h_labelnames = duk_get_hobject(ctx, entry_top + 4);
  47530. DUK_ASSERT(func->h_labelnames != NULL);
  47531. duk_push_dynamic_buffer(ctx, 0);
  47532. func->labelinfos_idx = entry_top + 5;
  47533. func->h_labelinfos = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, entry_top + 5);
  47534. DUK_ASSERT(func->h_labelinfos != NULL);
  47535. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(func->h_labelinfos));
  47536. duk_push_array(ctx);
  47537. func->argnames_idx = entry_top + 6;
  47538. func->h_argnames = duk_get_hobject(ctx, entry_top + 6);
  47539. DUK_ASSERT(func->h_argnames != NULL);
  47540. duk_push_object_internal(ctx);
  47541. func->varmap_idx = entry_top + 7;
  47542. func->h_varmap = duk_get_hobject(ctx, entry_top + 7);
  47543. DUK_ASSERT(func->h_varmap != NULL);
  47544. }
  47545. /* reset function state (prepare for pass 2) */
  47546. DUK_LOCAL void duk__reset_func_for_pass2(duk_compiler_ctx *comp_ctx) {
  47547. duk_compiler_func *func = &comp_ctx->curr_func;
  47548. duk_hthread *thr = comp_ctx->thr;
  47549. duk_context *ctx = (duk_context *) thr;
  47550. /* XXX: reset buffers while keeping existing spare */
  47551. duk_hbuffer_reset(thr, func->h_code);
  47552. duk_hobject_set_length_zero(thr, func->h_consts);
  47553. /* keep func->h_funcs; inner functions are not reparsed to avoid O(depth^2) parsing */
  47554. func->fnum_next = 0;
  47555. /* duk_hobject_set_length_zero(thr, func->h_funcs); */
  47556. duk_hobject_set_length_zero(thr, func->h_labelnames);
  47557. duk_hbuffer_reset(thr, func->h_labelinfos);
  47558. /* keep func->h_argnames; it is fixed for all passes */
  47559. /* truncated in case pass 3 needed */
  47560. duk_push_object_internal(ctx);
  47561. duk_replace(ctx, func->varmap_idx);
  47562. func->h_varmap = duk_get_hobject(ctx, func->varmap_idx);
  47563. DUK_ASSERT(func->h_varmap != NULL);
  47564. }
  47565. /* cleanup varmap from any null entries, compact it, etc; returns number
  47566. * of final entries after cleanup.
  47567. */
  47568. DUK_LOCAL duk_int_t duk__cleanup_varmap(duk_compiler_ctx *comp_ctx) {
  47569. duk_hthread *thr = comp_ctx->thr;
  47570. duk_context *ctx = (duk_context *) thr;
  47571. duk_hobject *h_varmap;
  47572. duk_hstring *h_key;
  47573. duk_tval *tv;
  47574. duk_uint32_t i, e_next;
  47575. duk_int_t ret;
  47576. /* [ ... varmap ] */
  47577. h_varmap = duk_get_hobject(ctx, -1);
  47578. DUK_ASSERT(h_varmap != NULL);
  47579. ret = 0;
  47580. e_next = DUK_HOBJECT_GET_ENEXT(h_varmap);
  47581. for (i = 0; i < e_next; i++) {
  47582. h_key = DUK_HOBJECT_E_GET_KEY(thr->heap, h_varmap, i);
  47583. if (!h_key) {
  47584. continue;
  47585. }
  47586. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, h_varmap, i));
  47587. /* The entries can either be register numbers or 'null' values.
  47588. * Thus, no need to DECREF them and get side effects. DECREF'ing
  47589. * the keys (strings) can cause memory to be freed but no side
  47590. * effects as strings don't have finalizers. This is why we can
  47591. * rely on the object properties not changing from underneath us.
  47592. */
  47593. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, h_varmap, i);
  47594. if (!DUK_TVAL_IS_NUMBER(tv)) {
  47595. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv));
  47596. DUK_TVAL_SET_UNDEFINED_UNUSED(tv);
  47597. DUK_HOBJECT_E_SET_KEY(thr->heap, h_varmap, i, NULL);
  47598. DUK_HSTRING_DECREF(thr, h_key);
  47599. } else {
  47600. ret++;
  47601. }
  47602. }
  47603. duk_compact(ctx, -1);
  47604. return ret;
  47605. }
  47606. /* convert duk_compiler_func into a function template, leaving the result
  47607. * on top of stack.
  47608. */
  47609. /* XXX: awkward and bloated asm -- use faster internal accesses */
  47610. DUK_LOCAL void duk__convert_to_func_template(duk_compiler_ctx *comp_ctx, duk_bool_t force_no_namebind) {
  47611. duk_compiler_func *func = &comp_ctx->curr_func;
  47612. duk_hthread *thr = comp_ctx->thr;
  47613. duk_context *ctx = (duk_context *) thr;
  47614. duk_hcompiledfunction *h_res;
  47615. duk_hbuffer_fixed *h_data;
  47616. duk_size_t consts_count;
  47617. duk_size_t funcs_count;
  47618. duk_size_t code_count;
  47619. duk_size_t code_size;
  47620. duk_size_t data_size;
  47621. duk_size_t i;
  47622. duk_tval *p_const;
  47623. duk_hobject **p_func;
  47624. duk_instr_t *p_instr;
  47625. duk_compiler_instr *q_instr;
  47626. duk_tval *tv;
  47627. DUK_DDD(DUK_DDDPRINT("converting duk_compiler_func to function/template"));
  47628. DUK_DD(DUK_DDPRINT("code=%!xO consts=%!O funcs=%!O",
  47629. (duk_heaphdr *) func->h_code,
  47630. (duk_heaphdr *) func->h_consts,
  47631. (duk_heaphdr *) func->h_funcs));
  47632. /*
  47633. * Push result object and init its flags
  47634. */
  47635. /* Valstack should suffice here, required on function valstack init */
  47636. (void) duk_push_compiledfunction(ctx);
  47637. h_res = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1); /* XXX: specific getter */
  47638. if (func->is_function) {
  47639. DUK_DDD(DUK_DDDPRINT("function -> set NEWENV"));
  47640. DUK_HOBJECT_SET_NEWENV((duk_hobject *) h_res);
  47641. if (!func->is_arguments_shadowed) {
  47642. /* arguments object would be accessible; note that shadowing
  47643. * bindings are arguments or function declarations, neither
  47644. * of which are deletable, so this is safe.
  47645. */
  47646. if (func->id_access_arguments || func->may_direct_eval) {
  47647. DUK_DDD(DUK_DDDPRINT("function may access 'arguments' object directly or "
  47648. "indirectly -> set CREATEARGS"));
  47649. DUK_HOBJECT_SET_CREATEARGS((duk_hobject *) h_res);
  47650. }
  47651. }
  47652. } else if (func->is_eval && func->is_strict) {
  47653. DUK_DDD(DUK_DDDPRINT("strict eval code -> set NEWENV"));
  47654. DUK_HOBJECT_SET_NEWENV((duk_hobject *) h_res);
  47655. } else {
  47656. /* non-strict eval: env is caller's env or global env (direct vs. indirect call)
  47657. * global code: env is is global env
  47658. */
  47659. DUK_DDD(DUK_DDDPRINT("non-strict eval code or global code -> no NEWENV"));
  47660. DUK_ASSERT(!DUK_HOBJECT_HAS_NEWENV((duk_hobject *) h_res));
  47661. }
  47662. if (func->is_function && !func->is_decl && func->h_name != NULL && !force_no_namebind) {
  47663. /* Object literal set/get functions have a name (property
  47664. * name) but must not have a lexical name binding, see
  47665. * test-bug-getset-func-name.js.
  47666. */
  47667. DUK_DDD(DUK_DDDPRINT("function expression with a name -> set NAMEBINDING"));
  47668. DUK_HOBJECT_SET_NAMEBINDING((duk_hobject *) h_res);
  47669. }
  47670. if (func->is_strict) {
  47671. DUK_DDD(DUK_DDDPRINT("function is strict -> set STRICT"));
  47672. DUK_HOBJECT_SET_STRICT((duk_hobject *) h_res);
  47673. }
  47674. if (func->is_notail) {
  47675. DUK_DDD(DUK_DDDPRINT("function is notail -> set NOTAIL"));
  47676. DUK_HOBJECT_SET_NOTAIL((duk_hobject *) h_res);
  47677. }
  47678. /*
  47679. * Build function fixed size 'data' buffer, which contains bytecode,
  47680. * constants, and inner function references.
  47681. *
  47682. * During the building phase 'data' is reachable but incomplete.
  47683. * Only incref's occur during building (no refzero or GC happens),
  47684. * so the building process is atomic.
  47685. */
  47686. consts_count = duk_hobject_get_length(comp_ctx->thr, func->h_consts);
  47687. funcs_count = duk_hobject_get_length(comp_ctx->thr, func->h_funcs) / 3;
  47688. code_count = DUK_HBUFFER_GET_SIZE(func->h_code) / sizeof(duk_compiler_instr);
  47689. code_size = code_count * sizeof(duk_instr_t);
  47690. data_size = consts_count * sizeof(duk_tval) +
  47691. funcs_count * sizeof(duk_hobject *) +
  47692. code_size;
  47693. DUK_DDD(DUK_DDDPRINT("consts_count=%ld, funcs_count=%ld, code_size=%ld -> "
  47694. "data_size=%ld*%ld + %ld*%ld + %ld = %ld",
  47695. (long) consts_count, (long) funcs_count, (long) code_size,
  47696. (long) consts_count, (long) sizeof(duk_tval),
  47697. (long) funcs_count, (long) sizeof(duk_hobject *),
  47698. (long) code_size, (long) data_size));
  47699. duk_push_fixed_buffer(ctx, data_size);
  47700. h_data = (duk_hbuffer_fixed *) duk_get_hbuffer(ctx, -1);
  47701. DUK_ASSERT(h_data != NULL);
  47702. DUK_HCOMPILEDFUNCTION_SET_DATA(thr->heap, h_res, (duk_hbuffer *) h_data);
  47703. DUK_HEAPHDR_INCREF(thr, h_data);
  47704. p_const = (duk_tval *) DUK_HBUFFER_FIXED_GET_DATA_PTR(thr->heap, h_data);
  47705. for (i = 0; i < consts_count; i++) {
  47706. DUK_ASSERT(i <= DUK_UARRIDX_MAX); /* const limits */
  47707. tv = duk_hobject_find_existing_array_entry_tval_ptr(thr->heap, func->h_consts, (duk_uarridx_t) i);
  47708. DUK_ASSERT(tv != NULL);
  47709. DUK_TVAL_SET_TVAL(p_const, tv);
  47710. p_const++;
  47711. DUK_TVAL_INCREF(thr, tv); /* may be a string constant */
  47712. DUK_DDD(DUK_DDDPRINT("constant: %!T", (duk_tval *) tv));
  47713. }
  47714. p_func = (duk_hobject **) p_const;
  47715. DUK_HCOMPILEDFUNCTION_SET_FUNCS(thr->heap, h_res, p_func);
  47716. for (i = 0; i < funcs_count; i++) {
  47717. duk_hobject *h;
  47718. DUK_ASSERT(i * 3 <= DUK_UARRIDX_MAX); /* func limits */
  47719. tv = duk_hobject_find_existing_array_entry_tval_ptr(thr->heap, func->h_funcs, (duk_uarridx_t) (i * 3));
  47720. DUK_ASSERT(tv != NULL);
  47721. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  47722. h = DUK_TVAL_GET_OBJECT(tv);
  47723. DUK_ASSERT(h != NULL);
  47724. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(h));
  47725. *p_func++ = h;
  47726. DUK_HOBJECT_INCREF(thr, h);
  47727. DUK_DDD(DUK_DDDPRINT("inner function: %p -> %!iO",
  47728. (void *) h, (duk_heaphdr *) h));
  47729. }
  47730. p_instr = (duk_instr_t *) p_func;
  47731. DUK_HCOMPILEDFUNCTION_SET_BYTECODE(thr->heap, h_res, p_instr);
  47732. /* copy bytecode instructions one at a time */
  47733. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(func->h_code));
  47734. q_instr = (duk_compiler_instr *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, func->h_code);
  47735. for (i = 0; i < code_count; i++) {
  47736. p_instr[i] = q_instr[i].ins;
  47737. }
  47738. /* Note: 'q_instr' is still used below */
  47739. DUK_ASSERT((duk_uint8_t *) (p_instr + code_count) == DUK_HBUFFER_FIXED_GET_DATA_PTR(thr->heap, h_data) + data_size);
  47740. duk_pop(ctx); /* 'data' (and everything in it) is reachable through h_res now */
  47741. /*
  47742. * Init object properties
  47743. *
  47744. * Properties should be added in decreasing order of access frequency.
  47745. * (Not very critical for function templates.)
  47746. */
  47747. DUK_DDD(DUK_DDDPRINT("init function properties"));
  47748. /* [ ... res ] */
  47749. /* _Varmap: omitted if function is guaranteed not to do slow path identifier
  47750. * accesses or if it would turn out to be empty of actual register mappings
  47751. * after a cleanup. When debugging is enabled, we always need the varmap to
  47752. * be able to lookup variables at any point.
  47753. */
  47754. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  47755. if (1) {
  47756. #else
  47757. if (func->id_access_slow || /* directly uses slow accesses */
  47758. func->may_direct_eval || /* may indirectly slow access through a direct eval */
  47759. funcs_count > 0) { /* has inner functions which may slow access (XXX: this can be optimized by looking at the inner functions) */
  47760. #endif
  47761. duk_int_t num_used;
  47762. duk_dup(ctx, func->varmap_idx);
  47763. num_used = duk__cleanup_varmap(comp_ctx);
  47764. DUK_DDD(DUK_DDDPRINT("cleaned up varmap: %!T (num_used=%ld)",
  47765. (duk_tval *) duk_get_tval(ctx, -1), (long) num_used));
  47766. if (num_used > 0) {
  47767. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VARMAP, DUK_PROPDESC_FLAGS_NONE);
  47768. } else {
  47769. DUK_DDD(DUK_DDDPRINT("varmap is empty after cleanup -> no need to add"));
  47770. duk_pop(ctx);
  47771. }
  47772. }
  47773. /* _Formals: omitted if function is guaranteed not to need a (non-strict) arguments object */
  47774. if (1) {
  47775. /* XXX: Add a proper condition. If formals list is omitted, recheck
  47776. * handling for 'length' in duk_js_push_closure(); it currently relies
  47777. * on _Formals being set. Removal may need to be conditional to debugging
  47778. * being enabled/disabled too.
  47779. */
  47780. duk_dup(ctx, func->argnames_idx);
  47781. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_FORMALS, DUK_PROPDESC_FLAGS_NONE);
  47782. }
  47783. /* name */
  47784. if (func->h_name) {
  47785. duk_push_hstring(ctx, func->h_name);
  47786. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE);
  47787. }
  47788. /* _Source */
  47789. #if defined(DUK_USE_NONSTD_FUNC_SOURCE_PROPERTY)
  47790. if (0) {
  47791. /* XXX: Currently function source code is not stored, as it is not
  47792. * required by the standard. Source code should not be stored by
  47793. * default (user should enable it explicitly), and the source should
  47794. * probably be compressed with a trivial text compressor; average
  47795. * compression of 20-30% is quite easy to achieve even with a trivial
  47796. * compressor (RLE + backwards lookup).
  47797. *
  47798. * Debugging needs source code to be useful: sometimes input code is
  47799. * not found in files as it may be generated and then eval()'d, given
  47800. * by dynamic C code, etc.
  47801. *
  47802. * Other issues:
  47803. *
  47804. * - Need tokenizer indices for start and end to substring
  47805. * - Always normalize function declaration part?
  47806. * - If we keep _Formals, only need to store body
  47807. */
  47808. /*
  47809. * For global or eval code this is straightforward. For functions
  47810. * created with the Function constructor we only get the source for
  47811. * the body and must manufacture the "function ..." part.
  47812. *
  47813. * For instance, for constructed functions (v8):
  47814. *
  47815. * > a = new Function("foo", "bar", "print(foo)");
  47816. * [Function]
  47817. * > a.toString()
  47818. * 'function anonymous(foo,bar) {\nprint(foo)\n}'
  47819. *
  47820. * Similarly for e.g. getters (v8):
  47821. *
  47822. * > x = { get a(foo,bar) { print(foo); } }
  47823. * { a: [Getter] }
  47824. * > Object.getOwnPropertyDescriptor(x, 'a').get.toString()
  47825. * 'function a(foo,bar) { print(foo); }'
  47826. */
  47827. #if 0
  47828. duk_push_string(ctx, "XXX");
  47829. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_SOURCE, DUK_PROPDESC_FLAGS_NONE);
  47830. #endif
  47831. }
  47832. #endif /* DUK_USE_NONSTD_FUNC_SOURCE_PROPERTY */
  47833. /* _Pc2line */
  47834. #if defined(DUK_USE_PC2LINE)
  47835. if (1) {
  47836. /*
  47837. * Size-optimized pc->line mapping.
  47838. */
  47839. DUK_ASSERT(code_count <= DUK_COMPILER_MAX_BYTECODE_LENGTH);
  47840. duk_hobject_pc2line_pack(thr, q_instr, (duk_uint_fast32_t) code_count); /* -> pushes fixed buffer */
  47841. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_PC2LINE, DUK_PROPDESC_FLAGS_NONE);
  47842. /* XXX: if assertions enabled, walk through all valid PCs
  47843. * and check line mapping.
  47844. */
  47845. }
  47846. #endif /* DUK_USE_PC2LINE */
  47847. /* fileName */
  47848. if (comp_ctx->h_filename) {
  47849. /*
  47850. * Source filename (or equivalent), for identifying thrown errors.
  47851. */
  47852. duk_push_hstring(ctx, comp_ctx->h_filename);
  47853. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_NONE);
  47854. }
  47855. /*
  47856. * Init remaining result fields
  47857. *
  47858. * 'nregs' controls how large a register frame is allocated.
  47859. *
  47860. * 'nargs' controls how many formal arguments are written to registers:
  47861. * r0, ... r(nargs-1). The remaining registers are initialized to
  47862. * undefined.
  47863. */
  47864. DUK_ASSERT(func->temp_max >= 0);
  47865. h_res->nregs = func->temp_max;
  47866. h_res->nargs = duk_hobject_get_length(thr, func->h_argnames);
  47867. DUK_ASSERT(h_res->nregs >= h_res->nargs); /* pass2 allocation handles this */
  47868. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  47869. h_res->start_line = (duk_uint32_t) func->min_line;
  47870. h_res->end_line = (duk_uint32_t) func->max_line;
  47871. #endif
  47872. DUK_DD(DUK_DDPRINT("converted function: %!ixT",
  47873. (duk_tval *) duk_get_tval(ctx, -1)));
  47874. /*
  47875. * Compact the function template.
  47876. */
  47877. duk_compact(ctx, -1);
  47878. /*
  47879. * Debug dumping
  47880. */
  47881. #ifdef DUK_USE_DDDPRINT
  47882. {
  47883. duk_hcompiledfunction *h;
  47884. duk_instr_t *p, *p_start, *p_end;
  47885. h = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1);
  47886. p_start = (duk_instr_t *) DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, h);
  47887. p_end = (duk_instr_t *) DUK_HCOMPILEDFUNCTION_GET_CODE_END(thr->heap, h);
  47888. p = p_start;
  47889. while (p < p_end) {
  47890. DUK_DDD(DUK_DDDPRINT("BC %04ld: %!I ; 0x%08lx op=%ld (%!C) a=%ld b=%ld c=%ld",
  47891. (long) (p - p_start),
  47892. (duk_instr_t) (*p),
  47893. (unsigned long) (*p),
  47894. (long) DUK_DEC_OP(*p),
  47895. (long) DUK_DEC_OP(*p),
  47896. (long) DUK_DEC_A(*p),
  47897. (long) DUK_DEC_B(*p),
  47898. (long) DUK_DEC_C(*p)));
  47899. p++;
  47900. }
  47901. }
  47902. #endif
  47903. }
  47904. /*
  47905. * Code emission helpers
  47906. *
  47907. * Some emission helpers understand the range of target and source reg/const
  47908. * values and automatically emit shuffling code if necessary. This is the
  47909. * case when the slot in question (A, B, C) is used in the standard way and
  47910. * for opcodes the emission helpers explicitly understand (like DUK_OP_CALL).
  47911. *
  47912. * The standard way is that:
  47913. * - slot A is a target register
  47914. * - slot B is a source register/constant
  47915. * - slot C is a source register/constant
  47916. *
  47917. * If a slot is used in a non-standard way the caller must indicate this
  47918. * somehow. If a slot is used as a target instead of a source (or vice
  47919. * versa), this can be indicated with a flag to trigger proper shuffling
  47920. * (e.g. DUK__EMIT_FLAG_B_IS_TARGET). If the value in the slot is not
  47921. * register/const related at all, the caller must ensure that the raw value
  47922. * fits into the corresponding slot so as to not trigger shuffling. The
  47923. * caller must set a "no shuffle" flag to ensure compilation fails if
  47924. * shuffling were to be triggered because of an internal error.
  47925. *
  47926. * For slots B and C the raw slot size is 9 bits but one bit is reserved for
  47927. * the reg/const indicator. To use the full 9-bit range for a raw value,
  47928. * shuffling must be disabled with the DUK__EMIT_FLAG_NO_SHUFFLE_{B,C} flag.
  47929. * Shuffling is only done for A, B, and C slots, not the larger BC or ABC slots.
  47930. *
  47931. * There is call handling specific understanding in the A-B-C emitter to
  47932. * convert call setup and call instructions into indirect ones if necessary.
  47933. */
  47934. /* Code emission flags, passed in the 'opcode' field. Opcode + flags
  47935. * fit into 16 bits for now, so use duk_small_uint.t.
  47936. */
  47937. #define DUK__EMIT_FLAG_NO_SHUFFLE_A (1 << 8)
  47938. #define DUK__EMIT_FLAG_NO_SHUFFLE_B (1 << 9)
  47939. #define DUK__EMIT_FLAG_NO_SHUFFLE_C (1 << 10)
  47940. #define DUK__EMIT_FLAG_A_IS_SOURCE (1 << 11) /* slot A is a source (default: target) */
  47941. #define DUK__EMIT_FLAG_B_IS_TARGET (1 << 12) /* slot B is a target (default: source) */
  47942. #define DUK__EMIT_FLAG_C_IS_TARGET (1 << 13) /* slot C is a target (default: source) */
  47943. #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 */
  47944. #define DUK__EMIT_FLAG_RESERVE_JUMPSLOT (1 << 15) /* reserve a jumpslot after instr before target spilling, used for NEXTENUM */
  47945. /* XXX: clarify on when and where DUK__CONST_MARKER is allowed */
  47946. /* XXX: opcode specific assertions on when consts are allowed */
  47947. /* XXX: macro smaller than call? */
  47948. DUK_LOCAL duk_int_t duk__get_current_pc(duk_compiler_ctx *comp_ctx) {
  47949. return (duk_int_t) (DUK_HBUFFER_GET_SIZE(comp_ctx->curr_func.h_code) / sizeof(duk_compiler_instr));
  47950. }
  47951. DUK_LOCAL duk_compiler_instr *duk__get_instr_ptr(duk_compiler_ctx *comp_ctx, duk_int_t pc) {
  47952. duk_compiler_func *f = &comp_ctx->curr_func;
  47953. duk_uint8_t *p;
  47954. duk_compiler_instr *code_begin, *code_end;
  47955. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(comp_ctx->thr->heap, f->h_code);
  47956. code_begin = (duk_compiler_instr *) p;
  47957. code_end = (duk_compiler_instr *) (p + DUK_HBUFFER_GET_SIZE(f->h_code));
  47958. DUK_UNREF(code_end);
  47959. DUK_ASSERT(pc >= 0);
  47960. DUK_ASSERT((duk_size_t) pc < (duk_size_t) (code_end - code_begin));
  47961. return code_begin + pc;
  47962. }
  47963. /* emit instruction; could return PC but that's not needed in the majority
  47964. * of cases.
  47965. */
  47966. DUK_LOCAL void duk__emit(duk_compiler_ctx *comp_ctx, duk_instr_t ins) {
  47967. duk_hbuffer_dynamic *h;
  47968. #if defined(DUK_USE_PC2LINE)
  47969. duk_int_t line;
  47970. #endif
  47971. duk_compiler_instr instr;
  47972. DUK_DDD(DUK_DDDPRINT("duk__emit: 0x%08lx curr_token.start_line=%ld prev_token.start_line=%ld pc=%ld --> %!I",
  47973. (unsigned long) ins,
  47974. (long) comp_ctx->curr_token.start_line,
  47975. (long) comp_ctx->prev_token.start_line,
  47976. (long) duk__get_current_pc(comp_ctx),
  47977. (duk_instr_t) ins));
  47978. h = comp_ctx->curr_func.h_code;
  47979. #if defined(DUK_USE_PC2LINE)
  47980. /* The line number tracking is a bit inconsistent right now, which
  47981. * affects debugger accuracy. Mostly call sites emit opcodes when
  47982. * they have parsed a token (say a terminating semicolon) and called
  47983. * duk__advance(). In this case the line number of the previous
  47984. * token is the most accurate one (except in prologue where
  47985. * prev_token.start_line is 0). This is probably not 100% correct
  47986. * right now.
  47987. */
  47988. /* approximation, close enough */
  47989. line = comp_ctx->prev_token.start_line;
  47990. if (line == 0) {
  47991. line = comp_ctx->curr_token.start_line;
  47992. }
  47993. #endif
  47994. instr.ins = ins;
  47995. #if defined(DUK_USE_PC2LINE)
  47996. instr.line = line;
  47997. #endif
  47998. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  47999. if (line < comp_ctx->curr_func.min_line) {
  48000. comp_ctx->curr_func.min_line = line;
  48001. }
  48002. if (line > comp_ctx->curr_func.max_line) {
  48003. comp_ctx->curr_func.max_line = line;
  48004. }
  48005. #endif
  48006. /* Limit checks for bytecode byte size and line number. */
  48007. #if defined(DUK_USE_PC2LINE) && defined(DUK_USE_ESBC_LIMITS)
  48008. #if defined(DUK_USE_BUFLEN16)
  48009. /* Buffer length is bounded to 0xffff automatically, avoid compile warning. */
  48010. if (DUK_UNLIKELY(line > DUK_USE_ESBC_MAX_LINENUMBER)) {
  48011. DUK_ERROR(comp_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_BYTECODE_LIMIT);
  48012. }
  48013. #else
  48014. if (DUK_UNLIKELY(line > DUK_USE_ESBC_MAX_LINENUMBER ||
  48015. DUK_HBUFFER_GET_SIZE((duk_hbuffer *) h) > DUK_USE_ESBC_MAX_BYTES)) {
  48016. DUK_ERROR(comp_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_BYTECODE_LIMIT);
  48017. }
  48018. #endif
  48019. #endif
  48020. duk_hbuffer_append_bytes(comp_ctx->thr, h, (duk_uint8_t *) &instr, sizeof(instr));
  48021. }
  48022. /* Update function min/max line from current token. Needed to improve
  48023. * function line range information for debugging, so that e.g. opening
  48024. * curly brace is covered by line range even when no opcodes are emitted
  48025. * for the line containing the brace.
  48026. */
  48027. DUK_LOCAL void duk__update_lineinfo_currtoken(duk_compiler_ctx *comp_ctx) {
  48028. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  48029. duk_int_t line;
  48030. line = comp_ctx->curr_token.start_line;
  48031. if (line == 0) {
  48032. return;
  48033. }
  48034. if (line < comp_ctx->curr_func.min_line) {
  48035. comp_ctx->curr_func.min_line = line;
  48036. }
  48037. if (line > comp_ctx->curr_func.max_line) {
  48038. comp_ctx->curr_func.max_line = line;
  48039. }
  48040. #else
  48041. DUK_UNREF(comp_ctx);
  48042. #endif
  48043. }
  48044. #if 0 /* unused */
  48045. DUK_LOCAL void duk__emit_op_only(duk_compiler_ctx *comp_ctx, duk_small_uint_t op) {
  48046. duk__emit(comp_ctx, DUK_ENC_OP_ABC(op, 0));
  48047. }
  48048. #endif
  48049. /* Important main primitive. */
  48050. 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) {
  48051. duk_instr_t ins = 0;
  48052. duk_int_t a_out = -1;
  48053. duk_int_t b_out = -1;
  48054. duk_int_t c_out = -1;
  48055. duk_int_t tmp;
  48056. DUK_DDD(DUK_DDDPRINT("emit: op_flags=%04lx, a=%ld, b=%ld, c=%ld",
  48057. (unsigned long) op_flags, (long) a, (long) b, (long) c));
  48058. /* We could rely on max temp/const checks: if they don't exceed BC
  48059. * limit, nothing here can either (just asserts would be enough).
  48060. * Currently we check for the limits, which provides additional
  48061. * protection against creating invalid bytecode due to compiler
  48062. * bugs.
  48063. */
  48064. DUK_ASSERT_DISABLE((op_flags & 0xff) >= DUK_BC_OP_MIN); /* unsigned */
  48065. DUK_ASSERT((op_flags & 0xff) <= DUK_BC_OP_MAX);
  48066. /* Input shuffling happens before the actual operation, while output
  48067. * shuffling happens afterwards. Output shuffling decisions are still
  48068. * made at the same time to reduce branch clutter; output shuffle decisions
  48069. * are recorded into X_out variables.
  48070. */
  48071. /* Slot A */
  48072. #if defined(DUK_USE_SHUFFLE_TORTURE)
  48073. if (a <= DUK_BC_A_MAX && (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_A)) {
  48074. #else
  48075. if (a <= DUK_BC_A_MAX) {
  48076. #endif
  48077. ;
  48078. } else if (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_A) {
  48079. DUK_D(DUK_DPRINT("out of regs: 'a' (reg) needs shuffling but shuffle prohibited, a: %ld", (long) a));
  48080. goto error_outofregs;
  48081. } else if (a <= DUK_BC_BC_MAX) {
  48082. comp_ctx->curr_func.needs_shuffle = 1;
  48083. tmp = comp_ctx->curr_func.shuffle1;
  48084. if (op_flags & DUK__EMIT_FLAG_A_IS_SOURCE) {
  48085. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDREG, tmp, a));
  48086. } else {
  48087. duk_small_int_t op = op_flags & 0xff;
  48088. if (op == DUK_OP_CSVAR || op == DUK_OP_CSREG || op == DUK_OP_CSPROP) {
  48089. /* Special handling for call setup instructions. The target
  48090. * is expressed indirectly, but there is no output shuffling.
  48091. */
  48092. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_A_IS_SOURCE) == 0);
  48093. duk__emit_load_int32_noshuffle(comp_ctx, tmp, a);
  48094. DUK_ASSERT(DUK_OP_CSVARI == DUK_OP_CSVAR + 1);
  48095. DUK_ASSERT(DUK_OP_CSREGI == DUK_OP_CSREG + 1);
  48096. DUK_ASSERT(DUK_OP_CSPROPI == DUK_OP_CSPROP + 1);
  48097. op_flags++; /* indirect opcode follows direct */
  48098. } else {
  48099. /* Output shuffle needed after main operation */
  48100. a_out = a;
  48101. }
  48102. }
  48103. a = tmp;
  48104. } else {
  48105. DUK_D(DUK_DPRINT("out of regs: 'a' (reg) needs shuffling but does not fit into BC, a: %ld", (long) a));
  48106. goto error_outofregs;
  48107. }
  48108. /* Slot B */
  48109. if (b & DUK__CONST_MARKER) {
  48110. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_B) == 0);
  48111. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_B_IS_TARGET) == 0);
  48112. DUK_ASSERT((op_flags & 0xff) != DUK_OP_CALL);
  48113. DUK_ASSERT((op_flags & 0xff) != DUK_OP_NEW);
  48114. b = b & ~DUK__CONST_MARKER;
  48115. #if defined(DUK_USE_SHUFFLE_TORTURE)
  48116. if (0) {
  48117. #else
  48118. if (b <= 0xff) {
  48119. #endif
  48120. ins |= DUK_ENC_OP_A_B_C(0, 0, 0x100, 0); /* const flag for B */
  48121. } else if (b <= DUK_BC_BC_MAX) {
  48122. comp_ctx->curr_func.needs_shuffle = 1;
  48123. tmp = comp_ctx->curr_func.shuffle2;
  48124. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDCONST, tmp, b));
  48125. b = tmp;
  48126. } else {
  48127. DUK_D(DUK_DPRINT("out of regs: 'b' (const) needs shuffling but does not fit into BC, b: %ld", (long) b));
  48128. goto error_outofregs;
  48129. }
  48130. } else {
  48131. #if defined(DUK_USE_SHUFFLE_TORTURE)
  48132. if (b <= 0xff && (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_B)) {
  48133. #else
  48134. if (b <= 0xff) {
  48135. #endif
  48136. ;
  48137. } else if (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_B) {
  48138. if (b > DUK_BC_B_MAX) {
  48139. /* Note: 0xff != DUK_BC_B_MAX */
  48140. DUK_D(DUK_DPRINT("out of regs: 'b' (reg) needs shuffling but shuffle prohibited, b: %ld", (long) b));
  48141. goto error_outofregs;
  48142. }
  48143. } else if (b <= DUK_BC_BC_MAX) {
  48144. comp_ctx->curr_func.needs_shuffle = 1;
  48145. tmp = comp_ctx->curr_func.shuffle2;
  48146. if (op_flags & DUK__EMIT_FLAG_B_IS_TARGET) {
  48147. /* Output shuffle needed after main operation */
  48148. b_out = b;
  48149. }
  48150. if (!(op_flags & DUK__EMIT_FLAG_B_IS_TARGET) || (op_flags & DUK__EMIT_FLAG_B_IS_TARGETSOURCE)) {
  48151. duk_small_int_t op = op_flags & 0xff;
  48152. if (op == DUK_OP_CALL || op == DUK_OP_NEW ||
  48153. op == DUK_OP_MPUTOBJ || op == DUK_OP_MPUTARR) {
  48154. /* Special handling for CALL/NEW/MPUTOBJ/MPUTARR shuffling.
  48155. * For each, slot B identifies the first register of a range
  48156. * of registers, so normal shuffling won't work. Instead,
  48157. * an indirect version of the opcode is used.
  48158. */
  48159. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_B_IS_TARGET) == 0);
  48160. duk__emit_load_int32_noshuffle(comp_ctx, tmp, b);
  48161. DUK_ASSERT(DUK_OP_CALLI == DUK_OP_CALL + 1);
  48162. DUK_ASSERT(DUK_OP_NEWI == DUK_OP_NEW + 1);
  48163. DUK_ASSERT(DUK_OP_MPUTOBJI == DUK_OP_MPUTOBJ + 1);
  48164. DUK_ASSERT(DUK_OP_MPUTARRI == DUK_OP_MPUTARR + 1);
  48165. op_flags++; /* indirect opcode follows direct */
  48166. } else {
  48167. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDREG, tmp, b));
  48168. }
  48169. }
  48170. b = tmp;
  48171. } else {
  48172. DUK_D(DUK_DPRINT("out of regs: 'b' (reg) needs shuffling but does not fit into BC, b: %ld", (long) b));
  48173. goto error_outofregs;
  48174. }
  48175. }
  48176. /* Slot C */
  48177. if (c & DUK__CONST_MARKER) {
  48178. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_C) == 0);
  48179. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_C_IS_TARGET) == 0);
  48180. c = c & ~DUK__CONST_MARKER;
  48181. #if defined(DUK_USE_SHUFFLE_TORTURE)
  48182. if (0) {
  48183. #else
  48184. if (c <= 0xff) {
  48185. #endif
  48186. ins |= DUK_ENC_OP_A_B_C(0, 0, 0, 0x100); /* const flag for C */
  48187. } else if (c <= DUK_BC_BC_MAX) {
  48188. comp_ctx->curr_func.needs_shuffle = 1;
  48189. tmp = comp_ctx->curr_func.shuffle3;
  48190. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDCONST, tmp, c));
  48191. c = tmp;
  48192. } else {
  48193. DUK_D(DUK_DPRINT("out of regs: 'c' (const) needs shuffling but does not fit into BC, c: %ld", (long) c));
  48194. goto error_outofregs;
  48195. }
  48196. } else {
  48197. #if defined(DUK_USE_SHUFFLE_TORTURE)
  48198. if (c <= 0xff && (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_C)) {
  48199. #else
  48200. if (c <= 0xff) {
  48201. #endif
  48202. ;
  48203. } else if (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_C) {
  48204. if (c > DUK_BC_C_MAX) {
  48205. /* Note: 0xff != DUK_BC_C_MAX */
  48206. DUK_D(DUK_DPRINT("out of regs: 'c' (reg) needs shuffling but shuffle prohibited, c: %ld", (long) c));
  48207. goto error_outofregs;
  48208. }
  48209. } else if (c <= DUK_BC_BC_MAX) {
  48210. comp_ctx->curr_func.needs_shuffle = 1;
  48211. tmp = comp_ctx->curr_func.shuffle3;
  48212. if (op_flags & DUK__EMIT_FLAG_C_IS_TARGET) {
  48213. /* Output shuffle needed after main operation */
  48214. c_out = c;
  48215. } else {
  48216. duk_small_int_t op = op_flags & 0xff;
  48217. if (op == DUK_OP_EXTRA &&
  48218. (a == DUK_EXTRAOP_INITGET || a == DUK_EXTRAOP_INITSET)) {
  48219. /* Special shuffling for INITGET/INITSET, where slot C
  48220. * identifies a register pair and cannot be shuffled
  48221. * normally. Use an indirect variant instead.
  48222. */
  48223. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_C_IS_TARGET) == 0);
  48224. duk__emit_load_int32_noshuffle(comp_ctx, tmp, c);
  48225. DUK_ASSERT(DUK_EXTRAOP_INITGETI == DUK_EXTRAOP_INITGET + 1);
  48226. DUK_ASSERT(DUK_EXTRAOP_INITSETI == DUK_EXTRAOP_INITSET + 1);
  48227. a++; /* indirect opcode follows direct */
  48228. } else {
  48229. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDREG, tmp, c));
  48230. }
  48231. }
  48232. c = tmp;
  48233. } else {
  48234. DUK_D(DUK_DPRINT("out of regs: 'c' (reg) needs shuffling but does not fit into BC, c: %ld", (long) c));
  48235. goto error_outofregs;
  48236. }
  48237. }
  48238. /* Main operation */
  48239. DUK_ASSERT_DISABLE(a >= DUK_BC_A_MIN); /* unsigned */
  48240. DUK_ASSERT(a <= DUK_BC_A_MAX);
  48241. DUK_ASSERT_DISABLE(b >= DUK_BC_B_MIN); /* unsigned */
  48242. DUK_ASSERT(b <= DUK_BC_B_MAX);
  48243. DUK_ASSERT_DISABLE(c >= DUK_BC_C_MIN); /* unsigned */
  48244. DUK_ASSERT(c <= DUK_BC_C_MAX);
  48245. ins |= DUK_ENC_OP_A_B_C(op_flags & 0xff, a, b, c);
  48246. duk__emit(comp_ctx, ins);
  48247. /* NEXTENUM needs a jump slot right after the main instruction.
  48248. * When the JUMP is taken, output spilling is not needed so this
  48249. * workaround is possible. The jump slot PC is exceptionally
  48250. * plumbed through comp_ctx to minimize call sites.
  48251. */
  48252. if (op_flags & DUK__EMIT_FLAG_RESERVE_JUMPSLOT) {
  48253. comp_ctx->emit_jumpslot_pc = duk__get_current_pc(comp_ctx);
  48254. duk__emit_abc(comp_ctx, DUK_OP_JUMP, 0);
  48255. }
  48256. /* Output shuffling: only one output register is realistically possible.
  48257. *
  48258. * (Zero would normally be an OK marker value: if the target register
  48259. * was zero, it would never be shuffled. But with DUK_USE_SHUFFLE_TORTURE
  48260. * this is no longer true, so use -1 as a marker instead.)
  48261. */
  48262. if (a_out >= 0) {
  48263. DUK_ASSERT(b_out < 0);
  48264. DUK_ASSERT(c_out < 0);
  48265. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_STREG, a, a_out));
  48266. } else if (b_out >= 0) {
  48267. DUK_ASSERT(a_out < 0);
  48268. DUK_ASSERT(c_out < 0);
  48269. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_STREG, b, b_out));
  48270. } else if (c_out >= 0) {
  48271. DUK_ASSERT(b_out < 0);
  48272. DUK_ASSERT(c_out < 0);
  48273. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_STREG, c, c_out));
  48274. }
  48275. return;
  48276. error_outofregs:
  48277. DUK_ERROR(comp_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_REG_LIMIT);
  48278. }
  48279. 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) {
  48280. duk__emit_a_b_c(comp_ctx, op_flags | DUK__EMIT_FLAG_NO_SHUFFLE_C, a, b, 0);
  48281. }
  48282. #if 0 /* unused */
  48283. DUK_LOCAL void duk__emit_a(duk_compiler_ctx *comp_ctx, int op_flags, int a) {
  48284. duk__emit_a_b_c(comp_ctx, op_flags | DUK__EMIT_FLAG_NO_SHUFFLE_B | DUK__EMIT_FLAG_NO_SHUFFLE_C, a, 0, 0);
  48285. }
  48286. #endif
  48287. 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) {
  48288. duk_instr_t ins;
  48289. duk_int_t tmp;
  48290. /* allow caller to give a const number with the DUK__CONST_MARKER */
  48291. bc = bc & (~DUK__CONST_MARKER);
  48292. DUK_ASSERT_DISABLE((op_flags & 0xff) >= DUK_BC_OP_MIN); /* unsigned */
  48293. DUK_ASSERT((op_flags & 0xff) <= DUK_BC_OP_MAX);
  48294. DUK_ASSERT_DISABLE(bc >= DUK_BC_BC_MIN); /* unsigned */
  48295. DUK_ASSERT(bc <= DUK_BC_BC_MAX);
  48296. DUK_ASSERT((bc & DUK__CONST_MARKER) == 0);
  48297. if (bc <= DUK_BC_BC_MAX) {
  48298. ;
  48299. } else {
  48300. /* No BC shuffling now. */
  48301. goto error_outofregs;
  48302. }
  48303. #if defined(DUK_USE_SHUFFLE_TORTURE)
  48304. if (a <= DUK_BC_A_MAX && (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_A)) {
  48305. #else
  48306. if (a <= DUK_BC_A_MAX) {
  48307. #endif
  48308. ins = DUK_ENC_OP_A_BC(op_flags & 0xff, a, bc);
  48309. duk__emit(comp_ctx, ins);
  48310. } else if (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_A) {
  48311. goto error_outofregs;
  48312. } else if (a <= DUK_BC_BC_MAX) {
  48313. comp_ctx->curr_func.needs_shuffle = 1;
  48314. tmp = comp_ctx->curr_func.shuffle1;
  48315. ins = DUK_ENC_OP_A_BC(op_flags & 0xff, tmp, bc);
  48316. if (op_flags & DUK__EMIT_FLAG_A_IS_SOURCE) {
  48317. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDREG, tmp, a));
  48318. duk__emit(comp_ctx, ins);
  48319. } else {
  48320. duk__emit(comp_ctx, ins);
  48321. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_STREG, tmp, a));
  48322. }
  48323. } else {
  48324. goto error_outofregs;
  48325. }
  48326. return;
  48327. error_outofregs:
  48328. DUK_ERROR(comp_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_REG_LIMIT);
  48329. }
  48330. DUK_LOCAL void duk__emit_abc(duk_compiler_ctx *comp_ctx, duk_small_uint_t op, duk_regconst_t abc) {
  48331. duk_instr_t ins;
  48332. DUK_ASSERT_DISABLE(op >= DUK_BC_OP_MIN); /* unsigned */
  48333. DUK_ASSERT(op <= DUK_BC_OP_MAX);
  48334. DUK_ASSERT_DISABLE(abc >= DUK_BC_ABC_MIN); /* unsigned */
  48335. DUK_ASSERT(abc <= DUK_BC_ABC_MAX);
  48336. DUK_ASSERT((abc & DUK__CONST_MARKER) == 0);
  48337. if (abc <= DUK_BC_ABC_MAX) {
  48338. ;
  48339. } else {
  48340. goto error_outofregs;
  48341. }
  48342. ins = DUK_ENC_OP_ABC(op, abc);
  48343. DUK_DDD(DUK_DDDPRINT("duk__emit_abc: 0x%08lx line=%ld pc=%ld op=%ld (%!C) abc=%ld (%!I)",
  48344. (unsigned long) ins, (long) comp_ctx->curr_token.start_line,
  48345. (long) duk__get_current_pc(comp_ctx), (long) op, (long) op,
  48346. (long) abc, (duk_instr_t) ins));
  48347. duk__emit(comp_ctx, ins);
  48348. return;
  48349. error_outofregs:
  48350. DUK_ERROR(comp_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_REG_LIMIT);
  48351. }
  48352. 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) {
  48353. DUK_ASSERT_DISABLE((extraop_flags & 0xff) >= DUK_BC_EXTRAOP_MIN); /* unsigned */
  48354. DUK_ASSERT((extraop_flags & 0xff) <= DUK_BC_EXTRAOP_MAX);
  48355. /* Setting "no shuffle A" is covered by the assert, but it's needed
  48356. * with DUK_USE_SHUFFLE_TORTURE.
  48357. */
  48358. duk__emit_a_b_c(comp_ctx,
  48359. DUK_OP_EXTRA | DUK__EMIT_FLAG_NO_SHUFFLE_A | (extraop_flags & ~0xff), /* transfer flags */
  48360. extraop_flags & 0xff,
  48361. b,
  48362. c);
  48363. }
  48364. DUK_LOCAL void duk__emit_extraop_b(duk_compiler_ctx *comp_ctx, duk_small_uint_t extraop_flags, duk_regconst_t b) {
  48365. DUK_ASSERT_DISABLE((extraop_flags & 0xff) >= DUK_BC_EXTRAOP_MIN); /* unsigned */
  48366. DUK_ASSERT((extraop_flags & 0xff) <= DUK_BC_EXTRAOP_MAX);
  48367. /* Setting "no shuffle A" is covered by the assert, but it's needed
  48368. * with DUK_USE_SHUFFLE_TORTURE.
  48369. */
  48370. duk__emit_a_b_c(comp_ctx,
  48371. DUK_OP_EXTRA | DUK__EMIT_FLAG_NO_SHUFFLE_A | (extraop_flags & ~0xff), /* transfer flags */
  48372. extraop_flags & 0xff,
  48373. b,
  48374. 0);
  48375. }
  48376. DUK_LOCAL void duk__emit_extraop_bc(duk_compiler_ctx *comp_ctx, duk_small_uint_t extraop, duk_regconst_t bc) {
  48377. DUK_ASSERT_DISABLE(extraop >= DUK_BC_EXTRAOP_MIN); /* unsigned */
  48378. DUK_ASSERT(extraop <= DUK_BC_EXTRAOP_MAX);
  48379. /* Setting "no shuffle A" is covered by the assert, but it's needed
  48380. * with DUK_USE_SHUFFLE_TORTURE.
  48381. */
  48382. duk__emit_a_bc(comp_ctx,
  48383. DUK_OP_EXTRA | DUK__EMIT_FLAG_NO_SHUFFLE_A,
  48384. extraop,
  48385. bc);
  48386. }
  48387. DUK_LOCAL void duk__emit_extraop_only(duk_compiler_ctx *comp_ctx, duk_small_uint_t extraop_flags) {
  48388. DUK_ASSERT_DISABLE((extraop_flags & 0xff) >= DUK_BC_EXTRAOP_MIN); /* unsigned */
  48389. DUK_ASSERT((extraop_flags & 0xff) <= DUK_BC_EXTRAOP_MAX);
  48390. /* Setting "no shuffle A" is covered by the assert, but it's needed
  48391. * with DUK_USE_SHUFFLE_TORTURE.
  48392. */
  48393. duk__emit_a_b_c(comp_ctx,
  48394. DUK_OP_EXTRA | DUK__EMIT_FLAG_NO_SHUFFLE_A | DUK__EMIT_FLAG_NO_SHUFFLE_B |
  48395. DUK__EMIT_FLAG_NO_SHUFFLE_C | (extraop_flags & ~0xff), /* transfer flags */
  48396. extraop_flags & 0xff,
  48397. 0,
  48398. 0);
  48399. }
  48400. 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) {
  48401. /* XXX: Shuffling support could be implemented here so that LDINT+LDINTX
  48402. * would only shuffle once (instead of twice). The current code works
  48403. * though, and has a smaller compiler footprint.
  48404. */
  48405. if ((val >= (duk_int32_t) DUK_BC_BC_MIN - (duk_int32_t) DUK_BC_LDINT_BIAS) &&
  48406. (val <= (duk_int32_t) DUK_BC_BC_MAX - (duk_int32_t) DUK_BC_LDINT_BIAS)) {
  48407. DUK_DDD(DUK_DDDPRINT("emit LDINT to reg %ld for %ld", (long) reg, (long) val));
  48408. duk__emit_a_bc(comp_ctx, DUK_OP_LDINT | op_flags, reg, (duk_regconst_t) (val + (duk_int32_t) DUK_BC_LDINT_BIAS));
  48409. } else {
  48410. duk_int32_t hi = val >> DUK_BC_LDINTX_SHIFT;
  48411. duk_int32_t lo = val & ((((duk_int32_t) 1) << DUK_BC_LDINTX_SHIFT) - 1);
  48412. DUK_ASSERT(lo >= 0);
  48413. DUK_DDD(DUK_DDDPRINT("emit LDINT+LDINTX to reg %ld for %ld -> hi %ld, lo %ld",
  48414. (long) reg, (long) val, (long) hi, (long) lo));
  48415. duk__emit_a_bc(comp_ctx, DUK_OP_LDINT | op_flags, reg, (duk_regconst_t) (hi + (duk_int32_t) DUK_BC_LDINT_BIAS));
  48416. duk__emit_a_bc(comp_ctx, DUK_OP_LDINTX | op_flags, reg, (duk_regconst_t) lo);
  48417. }
  48418. }
  48419. DUK_LOCAL void duk__emit_load_int32(duk_compiler_ctx *comp_ctx, duk_reg_t reg, duk_int32_t val) {
  48420. duk__emit_load_int32_raw(comp_ctx, reg, val, 0 /*op_flags*/);
  48421. }
  48422. #if defined(DUK_USE_SHUFFLE_TORTURE)
  48423. /* Used by duk__emit*() calls so that we don't shuffle the loadints that
  48424. * are needed to handle indirect opcodes.
  48425. */
  48426. DUK_LOCAL void duk__emit_load_int32_noshuffle(duk_compiler_ctx *comp_ctx, duk_reg_t reg, duk_int32_t val) {
  48427. duk__emit_load_int32_raw(comp_ctx, reg, val, DUK__EMIT_FLAG_NO_SHUFFLE_A /*op_flags*/);
  48428. }
  48429. #else
  48430. DUK_LOCAL void duk__emit_load_int32_noshuffle(duk_compiler_ctx *comp_ctx, duk_reg_t reg, duk_int32_t val) {
  48431. /* When torture not enabled, can just use the same helper because
  48432. * 'reg' won't get spilled.
  48433. */
  48434. DUK_ASSERT(reg <= DUK_BC_A_MAX);
  48435. duk__emit_load_int32(comp_ctx, reg, val);
  48436. }
  48437. #endif
  48438. DUK_LOCAL void duk__emit_jump(duk_compiler_ctx *comp_ctx, duk_int_t target_pc) {
  48439. duk_hbuffer_dynamic *h;
  48440. duk_int_t curr_pc;
  48441. duk_int_t offset;
  48442. h = comp_ctx->curr_func.h_code;
  48443. curr_pc = (duk_int_t) (DUK_HBUFFER_GET_SIZE(h) / sizeof(duk_compiler_instr));
  48444. offset = (duk_int_t) target_pc - (duk_int_t) curr_pc - 1;
  48445. DUK_ASSERT(offset + DUK_BC_JUMP_BIAS >= DUK_BC_ABC_MIN);
  48446. DUK_ASSERT(offset + DUK_BC_JUMP_BIAS <= DUK_BC_ABC_MAX);
  48447. duk__emit_abc(comp_ctx, DUK_OP_JUMP, (duk_regconst_t) (offset + DUK_BC_JUMP_BIAS));
  48448. }
  48449. DUK_LOCAL duk_int_t duk__emit_jump_empty(duk_compiler_ctx *comp_ctx) {
  48450. duk_int_t ret;
  48451. ret = duk__get_current_pc(comp_ctx); /* useful for patching jumps later */
  48452. duk__emit_abc(comp_ctx, DUK_OP_JUMP, 0);
  48453. return ret;
  48454. }
  48455. /* Insert an empty jump in the middle of code emitted earlier. This is
  48456. * currently needed for compiling for-in.
  48457. */
  48458. DUK_LOCAL void duk__insert_jump_entry(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc) {
  48459. duk_hbuffer_dynamic *h;
  48460. #if defined(DUK_USE_PC2LINE)
  48461. duk_int_t line;
  48462. #endif
  48463. duk_compiler_instr instr;
  48464. duk_size_t offset;
  48465. h = comp_ctx->curr_func.h_code;
  48466. #if defined(DUK_USE_PC2LINE)
  48467. line = comp_ctx->curr_token.start_line; /* approximation, close enough */
  48468. #endif
  48469. instr.ins = DUK_ENC_OP_ABC(DUK_OP_JUMP, 0);
  48470. #if defined(DUK_USE_PC2LINE)
  48471. instr.line = line;
  48472. #endif
  48473. offset = jump_pc * sizeof(duk_compiler_instr);
  48474. duk_hbuffer_insert_bytes(comp_ctx->thr, h, offset, (duk_uint8_t *) &instr, sizeof(instr));
  48475. }
  48476. /* Does not assume that jump_pc contains a DUK_OP_JUMP previously; this is intentional
  48477. * to allow e.g. an INVALID opcode be overwritten with a JUMP (label management uses this).
  48478. */
  48479. DUK_LOCAL void duk__patch_jump(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc, duk_int_t target_pc) {
  48480. duk_compiler_instr *instr;
  48481. duk_int_t offset;
  48482. /* allow negative PCs, behave as a no-op */
  48483. if (jump_pc < 0) {
  48484. DUK_DDD(DUK_DDDPRINT("duk__patch_jump(): nop call, jump_pc=%ld (<0), target_pc=%ld",
  48485. (long) jump_pc, (long) target_pc));
  48486. return;
  48487. }
  48488. DUK_ASSERT(jump_pc >= 0);
  48489. /* XXX: range assert */
  48490. instr = duk__get_instr_ptr(comp_ctx, jump_pc);
  48491. DUK_ASSERT(instr != NULL);
  48492. /* XXX: range assert */
  48493. offset = target_pc - jump_pc - 1;
  48494. instr->ins = DUK_ENC_OP_ABC(DUK_OP_JUMP, offset + DUK_BC_JUMP_BIAS);
  48495. DUK_DDD(DUK_DDDPRINT("duk__patch_jump(): jump_pc=%ld, target_pc=%ld, offset=%ld",
  48496. (long) jump_pc, (long) target_pc, (long) offset));
  48497. }
  48498. DUK_LOCAL void duk__patch_jump_here(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc) {
  48499. duk__patch_jump(comp_ctx, jump_pc, duk__get_current_pc(comp_ctx));
  48500. }
  48501. DUK_LOCAL void duk__patch_trycatch(duk_compiler_ctx *comp_ctx, duk_int_t trycatch_pc, duk_regconst_t reg_catch, duk_regconst_t const_varname, duk_small_uint_t flags) {
  48502. duk_compiler_instr *instr;
  48503. instr = duk__get_instr_ptr(comp_ctx, trycatch_pc);
  48504. DUK_ASSERT(instr != NULL);
  48505. DUK_ASSERT_DISABLE(flags >= DUK_BC_A_MIN);
  48506. DUK_ASSERT(flags <= DUK_BC_A_MAX);
  48507. DUK_ASSERT((reg_catch & DUK__CONST_MARKER) == 0);
  48508. const_varname = const_varname & (~DUK__CONST_MARKER);
  48509. if (reg_catch > DUK_BC_B_MAX || const_varname > DUK_BC_C_MAX) {
  48510. /* Catch attempts to use out-of-range reg/const. Without this
  48511. * check Duktape 0.12.0 could generate invalid code which caused
  48512. * an assert failure on execution. This error is triggered e.g.
  48513. * for functions with a lot of constants and a try-catch statement.
  48514. * Shuffling or opcode semantics change is needed to fix the issue.
  48515. * See: test-bug-trycatch-many-constants.js.
  48516. */
  48517. DUK_D(DUK_DPRINT("failed to patch trycatch: flags=%ld, reg_catch=%ld, const_varname=%ld (0x%08lx)",
  48518. (long) flags, (long) reg_catch, (long) const_varname, (long) const_varname));
  48519. DUK_ERROR(comp_ctx->thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_REG_LIMIT);
  48520. }
  48521. instr->ins = DUK_ENC_OP_A_B_C(DUK_OP_TRYCATCH, flags, reg_catch, const_varname);
  48522. }
  48523. DUK_LOCAL void duk__emit_if_false_skip(duk_compiler_ctx *comp_ctx, duk_regconst_t regconst) {
  48524. duk__emit_a_b_c(comp_ctx,
  48525. DUK_OP_IF | DUK__EMIT_FLAG_NO_SHUFFLE_A | DUK__EMIT_FLAG_NO_SHUFFLE_C,
  48526. 0 /*false*/,
  48527. regconst,
  48528. 0 /*unused*/);
  48529. }
  48530. DUK_LOCAL void duk__emit_if_true_skip(duk_compiler_ctx *comp_ctx, duk_regconst_t regconst) {
  48531. duk__emit_a_b_c(comp_ctx,
  48532. DUK_OP_IF | DUK__EMIT_FLAG_NO_SHUFFLE_A | DUK__EMIT_FLAG_NO_SHUFFLE_C,
  48533. 1 /*true*/,
  48534. regconst,
  48535. 0 /*unused*/);
  48536. }
  48537. DUK_LOCAL void duk__emit_invalid(duk_compiler_ctx *comp_ctx) {
  48538. duk__emit_extraop_bc(comp_ctx, DUK_EXTRAOP_INVALID, 0);
  48539. }
  48540. /*
  48541. * Peephole optimizer for finished bytecode.
  48542. *
  48543. * Does not remove opcodes; currently only straightens out unconditional
  48544. * jump chains which are generated by several control structures.
  48545. */
  48546. DUK_LOCAL void duk__peephole_optimize_bytecode(duk_compiler_ctx *comp_ctx) {
  48547. duk_hbuffer_dynamic *h;
  48548. duk_compiler_instr *bc;
  48549. duk_small_uint_t iter;
  48550. duk_int_t i, n;
  48551. duk_int_t count_opt;
  48552. h = comp_ctx->curr_func.h_code;
  48553. DUK_ASSERT(h != NULL);
  48554. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(h));
  48555. bc = (duk_compiler_instr *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(comp_ctx->thr->heap, h);
  48556. #if defined(DUK_USE_BUFLEN16)
  48557. /* No need to assert, buffer size maximum is 0xffff. */
  48558. #else
  48559. DUK_ASSERT(DUK_HBUFFER_GET_SIZE(h) / sizeof(duk_compiler_instr) <= DUK_INT_MAX); /* bytecode limits */
  48560. #endif
  48561. n = (duk_int_t) (DUK_HBUFFER_GET_SIZE(h) / sizeof(duk_compiler_instr));
  48562. for (iter = 0; iter < DUK_COMPILER_PEEPHOLE_MAXITER; iter++) {
  48563. count_opt = 0;
  48564. for (i = 0; i < n; i++) {
  48565. duk_instr_t ins;
  48566. duk_int_t target_pc1;
  48567. duk_int_t target_pc2;
  48568. ins = bc[i].ins;
  48569. if (DUK_DEC_OP(ins) != DUK_OP_JUMP) {
  48570. continue;
  48571. }
  48572. target_pc1 = i + 1 + DUK_DEC_ABC(ins) - DUK_BC_JUMP_BIAS;
  48573. DUK_DDD(DUK_DDDPRINT("consider jump at pc %ld; target_pc=%ld", (long) i, (long) target_pc1));
  48574. DUK_ASSERT(target_pc1 >= 0);
  48575. DUK_ASSERT(target_pc1 < n);
  48576. /* Note: if target_pc1 == i, we'll optimize a jump to itself.
  48577. * This does not need to be checked for explicitly; the case
  48578. * is rare and max iter breaks us out.
  48579. */
  48580. ins = bc[target_pc1].ins;
  48581. if (DUK_DEC_OP(ins) != DUK_OP_JUMP) {
  48582. continue;
  48583. }
  48584. target_pc2 = target_pc1 + 1 + DUK_DEC_ABC(ins) - DUK_BC_JUMP_BIAS;
  48585. DUK_DDD(DUK_DDDPRINT("optimizing jump at pc %ld; old target is %ld -> new target is %ld",
  48586. (long) i, (long) target_pc1, (long) target_pc2));
  48587. bc[i].ins = DUK_ENC_OP_ABC(DUK_OP_JUMP, target_pc2 - (i + 1) + DUK_BC_JUMP_BIAS);
  48588. count_opt++;
  48589. }
  48590. DUK_DD(DUK_DDPRINT("optimized %ld jumps on peephole round %ld", (long) count_opt, (long) (iter + 1)));
  48591. if (count_opt == 0) {
  48592. break;
  48593. }
  48594. }
  48595. }
  48596. /*
  48597. * Intermediate value helpers
  48598. */
  48599. #define DUK__ISREG(comp_ctx,x) (((x) & DUK__CONST_MARKER) == 0)
  48600. #define DUK__ISCONST(comp_ctx,x) (((x) & DUK__CONST_MARKER) != 0)
  48601. #define DUK__ISTEMP(comp_ctx,x) (DUK__ISREG((comp_ctx), (x)) && (duk_regconst_t) (x) >= (duk_regconst_t) ((comp_ctx)->curr_func.temp_first))
  48602. #define DUK__GETTEMP(comp_ctx) ((comp_ctx)->curr_func.temp_next)
  48603. #define DUK__SETTEMP(comp_ctx,x) ((comp_ctx)->curr_func.temp_next = (x)) /* dangerous: must only lower (temp_max not updated) */
  48604. #define DUK__SETTEMP_CHECKMAX(comp_ctx,x) duk__settemp_checkmax((comp_ctx),(x))
  48605. #define DUK__ALLOCTEMP(comp_ctx) duk__alloctemp((comp_ctx))
  48606. #define DUK__ALLOCTEMPS(comp_ctx,count) duk__alloctemps((comp_ctx),(count))
  48607. /* Flags for intermediate value coercions. A flag for using a forced reg
  48608. * is not needed, the forced_reg argument suffices and generates better
  48609. * code (it is checked as it is used).
  48610. */
  48611. #define DUK__IVAL_FLAG_ALLOW_CONST (1 << 0) /* allow a constant to be returned */
  48612. #define DUK__IVAL_FLAG_REQUIRE_TEMP (1 << 1) /* require a (mutable) temporary as a result */
  48613. #define DUK__IVAL_FLAG_REQUIRE_SHORT (1 << 2) /* require a short (8-bit) reg/const which fits into bytecode B/C slot */
  48614. /* XXX: some code might benefit from DUK__SETTEMP_IFTEMP(ctx,x) */
  48615. DUK_LOCAL void duk__copy_ispec(duk_compiler_ctx *comp_ctx, duk_ispec *src, duk_ispec *dst) {
  48616. duk_context *ctx = (duk_context *) comp_ctx->thr;
  48617. dst->t = src->t;
  48618. dst->regconst = src->regconst;
  48619. duk_copy(ctx, src->valstack_idx, dst->valstack_idx);
  48620. }
  48621. DUK_LOCAL void duk__copy_ivalue(duk_compiler_ctx *comp_ctx, duk_ivalue *src, duk_ivalue *dst) {
  48622. duk_context *ctx = (duk_context *) comp_ctx->thr;
  48623. dst->t = src->t;
  48624. dst->op = src->op;
  48625. dst->x1.t = src->x1.t;
  48626. dst->x1.regconst = src->x1.regconst;
  48627. dst->x2.t = src->x2.t;
  48628. dst->x2.regconst = src->x2.regconst;
  48629. duk_copy(ctx, src->x1.valstack_idx, dst->x1.valstack_idx);
  48630. duk_copy(ctx, src->x2.valstack_idx, dst->x2.valstack_idx);
  48631. }
  48632. /* XXX: to util */
  48633. DUK_LOCAL duk_bool_t duk__is_whole_get_int32(duk_double_t x, duk_int32_t *ival) {
  48634. duk_small_int_t c;
  48635. duk_int32_t t;
  48636. c = DUK_FPCLASSIFY(x);
  48637. if (c == DUK_FP_NORMAL || (c == DUK_FP_ZERO && !DUK_SIGNBIT(x))) {
  48638. /* Don't allow negative zero as it will cause trouble with
  48639. * LDINT+LDINTX. But positive zero is OK.
  48640. */
  48641. t = (duk_int32_t) x;
  48642. if ((duk_double_t) t == x) {
  48643. *ival = t;
  48644. return 1;
  48645. }
  48646. }
  48647. return 0;
  48648. }
  48649. DUK_LOCAL duk_reg_t duk__alloctemps(duk_compiler_ctx *comp_ctx, duk_small_int_t num) {
  48650. duk_reg_t res;
  48651. res = comp_ctx->curr_func.temp_next;
  48652. comp_ctx->curr_func.temp_next += num;
  48653. if (comp_ctx->curr_func.temp_next > DUK__MAX_TEMPS) { /* == DUK__MAX_TEMPS is OK */
  48654. DUK_ERROR(comp_ctx->thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_TEMP_LIMIT);
  48655. }
  48656. /* maintain highest 'used' temporary, needed to figure out nregs of function */
  48657. if (comp_ctx->curr_func.temp_next > comp_ctx->curr_func.temp_max) {
  48658. comp_ctx->curr_func.temp_max = comp_ctx->curr_func.temp_next;
  48659. }
  48660. return res;
  48661. }
  48662. DUK_LOCAL duk_reg_t duk__alloctemp(duk_compiler_ctx *comp_ctx) {
  48663. return duk__alloctemps(comp_ctx, 1);
  48664. }
  48665. DUK_LOCAL void duk__settemp_checkmax(duk_compiler_ctx *comp_ctx, duk_reg_t temp_next) {
  48666. comp_ctx->curr_func.temp_next = temp_next;
  48667. if (temp_next > comp_ctx->curr_func.temp_max) {
  48668. comp_ctx->curr_func.temp_max = temp_next;
  48669. }
  48670. }
  48671. /* get const for value at valstack top */
  48672. DUK_LOCAL duk_regconst_t duk__getconst(duk_compiler_ctx *comp_ctx) {
  48673. duk_hthread *thr = comp_ctx->thr;
  48674. duk_context *ctx = (duk_context *) thr;
  48675. duk_compiler_func *f = &comp_ctx->curr_func;
  48676. duk_tval *tv1;
  48677. duk_int_t i, n, n_check;
  48678. n = (duk_int_t) duk_get_length(ctx, f->consts_idx);
  48679. tv1 = duk_get_tval(ctx, -1);
  48680. DUK_ASSERT(tv1 != NULL);
  48681. #if defined(DUK_USE_FASTINT)
  48682. /* Explicit check for fastint downgrade. */
  48683. DUK_TVAL_CHKFAST_INPLACE(tv1);
  48684. #endif
  48685. /* Sanity workaround for handling functions with a large number of
  48686. * constants at least somewhat reasonably. Otherwise checking whether
  48687. * we already have the constant would grow very slow (as it is O(N^2)).
  48688. */
  48689. n_check = (n > DUK__GETCONST_MAX_CONSTS_CHECK ? DUK__GETCONST_MAX_CONSTS_CHECK : n);
  48690. for (i = 0; i < n_check; i++) {
  48691. duk_tval *tv2 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, f->h_consts, i);
  48692. /* Strict equality is NOT enough, because we cannot use the same
  48693. * constant for e.g. +0 and -0.
  48694. */
  48695. if (duk_js_samevalue(tv1, tv2)) {
  48696. DUK_DDD(DUK_DDDPRINT("reused existing constant for %!T -> const index %ld",
  48697. (duk_tval *) tv1, (long) i));
  48698. duk_pop(ctx);
  48699. return (duk_regconst_t) (i | DUK__CONST_MARKER);
  48700. }
  48701. }
  48702. if (n > DUK__MAX_CONSTS) {
  48703. DUK_ERROR(comp_ctx->thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_CONST_LIMIT);
  48704. }
  48705. DUK_DDD(DUK_DDDPRINT("allocating new constant for %!T -> const index %ld",
  48706. (duk_tval *) tv1, (long) n));
  48707. (void) duk_put_prop_index(ctx, f->consts_idx, n); /* invalidates tv1, tv2 */
  48708. return (duk_regconst_t) (n | DUK__CONST_MARKER);
  48709. }
  48710. /* Get the value represented by an duk_ispec to a register or constant.
  48711. * The caller can control the result by indicating whether or not:
  48712. *
  48713. * (1) a constant is allowed (sometimes the caller needs the result to
  48714. * be in a register)
  48715. *
  48716. * (2) a temporary register is required (usually when caller requires
  48717. * the register to be safely mutable; normally either a bound
  48718. * register or a temporary register are both OK)
  48719. *
  48720. * (3) a forced register target needs to be used
  48721. *
  48722. * Bytecode may be emitted to generate the necessary value. The return
  48723. * value is either a register or a constant.
  48724. */
  48725. DUK_LOCAL
  48726. duk_regconst_t duk__ispec_toregconst_raw(duk_compiler_ctx *comp_ctx,
  48727. duk_ispec *x,
  48728. duk_reg_t forced_reg,
  48729. duk_small_uint_t flags) {
  48730. duk_hthread *thr = comp_ctx->thr;
  48731. duk_context *ctx = (duk_context *) thr;
  48732. DUK_DDD(DUK_DDDPRINT("duk__ispec_toregconst_raw(): x={%ld:%ld:%!T}, "
  48733. "forced_reg=%ld, flags 0x%08lx: allow_const=%ld require_temp=%ld require_short=%ld",
  48734. (long) x->t,
  48735. (long) x->regconst,
  48736. (duk_tval *) duk_get_tval(ctx, x->valstack_idx),
  48737. (long) forced_reg,
  48738. (unsigned long) flags,
  48739. (long) ((flags & DUK__IVAL_FLAG_ALLOW_CONST) ? 1 : 0),
  48740. (long) ((flags & DUK__IVAL_FLAG_REQUIRE_TEMP) ? 1 : 0),
  48741. (long) ((flags & DUK__IVAL_FLAG_REQUIRE_SHORT) ? 1 : 0)));
  48742. switch (x->t) {
  48743. case DUK_ISPEC_VALUE: {
  48744. duk_tval *tv;
  48745. tv = duk_get_tval(ctx, x->valstack_idx);
  48746. DUK_ASSERT(tv != NULL);
  48747. switch (DUK_TVAL_GET_TAG(tv)) {
  48748. case DUK_TAG_UNDEFINED: {
  48749. /* Note: although there is no 'undefined' literal, undefined
  48750. * values can occur during compilation as a result of e.g.
  48751. * the 'void' operator.
  48752. */
  48753. duk_reg_t dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  48754. duk__emit_extraop_bc(comp_ctx, DUK_EXTRAOP_LDUNDEF, (duk_regconst_t) dest);
  48755. return (duk_regconst_t) dest;
  48756. }
  48757. case DUK_TAG_NULL: {
  48758. duk_reg_t dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  48759. duk__emit_extraop_bc(comp_ctx, DUK_EXTRAOP_LDNULL, (duk_regconst_t) dest);
  48760. return (duk_regconst_t) dest;
  48761. }
  48762. case DUK_TAG_BOOLEAN: {
  48763. duk_reg_t dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  48764. duk__emit_extraop_bc(comp_ctx,
  48765. (DUK_TVAL_GET_BOOLEAN(tv) ? DUK_EXTRAOP_LDTRUE : DUK_EXTRAOP_LDFALSE),
  48766. (duk_regconst_t) dest);
  48767. return (duk_regconst_t) dest;
  48768. }
  48769. case DUK_TAG_POINTER: {
  48770. DUK_UNREACHABLE();
  48771. break;
  48772. }
  48773. case DUK_TAG_STRING: {
  48774. duk_hstring *h;
  48775. duk_reg_t dest;
  48776. duk_regconst_t constidx;
  48777. h = DUK_TVAL_GET_STRING(tv);
  48778. DUK_UNREF(h);
  48779. DUK_ASSERT(h != NULL);
  48780. #if 0 /* XXX: to be implemented? */
  48781. /* Use special opcodes to load short strings */
  48782. if (DUK_HSTRING_GET_BYTELEN(h) <= 2) {
  48783. /* Encode into a single opcode (18 bits can encode 1-2 bytes + length indicator) */
  48784. } else if (DUK_HSTRING_GET_BYTELEN(h) <= 6) {
  48785. /* Encode into a double constant (53 bits can encode 6*8 = 48 bits + 3-bit length */
  48786. }
  48787. #endif
  48788. duk_dup(ctx, x->valstack_idx);
  48789. constidx = duk__getconst(comp_ctx);
  48790. if (flags & DUK__IVAL_FLAG_ALLOW_CONST) {
  48791. return constidx;
  48792. }
  48793. dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  48794. duk__emit_a_bc(comp_ctx, DUK_OP_LDCONST, (duk_regconst_t) dest, constidx);
  48795. return (duk_regconst_t) dest;
  48796. }
  48797. case DUK_TAG_OBJECT: {
  48798. DUK_UNREACHABLE();
  48799. break;
  48800. }
  48801. case DUK_TAG_BUFFER: {
  48802. DUK_UNREACHABLE();
  48803. break;
  48804. }
  48805. case DUK_TAG_LIGHTFUNC: {
  48806. DUK_UNREACHABLE();
  48807. break;
  48808. }
  48809. #if defined(DUK_USE_FASTINT)
  48810. case DUK_TAG_FASTINT:
  48811. #endif
  48812. default: {
  48813. /* number */
  48814. duk_reg_t dest;
  48815. duk_regconst_t constidx;
  48816. duk_double_t dval;
  48817. duk_int32_t ival;
  48818. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  48819. dval = DUK_TVAL_GET_NUMBER(tv);
  48820. if (!(flags & DUK__IVAL_FLAG_ALLOW_CONST)) {
  48821. /* A number can be loaded either through a constant, using
  48822. * LDINT, or using LDINT+LDINTX. LDINT is always a size win,
  48823. * LDINT+LDINTX is not if the constant is used multiple times.
  48824. * Currently always prefer LDINT+LDINTX over a double constant.
  48825. */
  48826. if (duk__is_whole_get_int32(dval, &ival)) {
  48827. dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  48828. duk__emit_load_int32(comp_ctx, dest, ival);
  48829. return (duk_regconst_t) dest;
  48830. }
  48831. }
  48832. duk_dup(ctx, x->valstack_idx);
  48833. constidx = duk__getconst(comp_ctx);
  48834. if (flags & DUK__IVAL_FLAG_ALLOW_CONST) {
  48835. return constidx;
  48836. } else {
  48837. dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  48838. duk__emit_a_bc(comp_ctx, DUK_OP_LDCONST, (duk_regconst_t) dest, constidx);
  48839. return (duk_regconst_t) dest;
  48840. }
  48841. }
  48842. } /* end switch */
  48843. }
  48844. case DUK_ISPEC_REGCONST: {
  48845. if ((x->regconst & DUK__CONST_MARKER) && !(flags & DUK__IVAL_FLAG_ALLOW_CONST)) {
  48846. duk_reg_t dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  48847. duk__emit_a_bc(comp_ctx, DUK_OP_LDCONST, (duk_regconst_t) dest, x->regconst);
  48848. return (duk_regconst_t) dest;
  48849. } else {
  48850. if (forced_reg >= 0) {
  48851. if (x->regconst != (duk_regconst_t) forced_reg) {
  48852. duk__emit_a_bc(comp_ctx, DUK_OP_LDREG, forced_reg, x->regconst);
  48853. }
  48854. return (duk_regconst_t) forced_reg;
  48855. } else {
  48856. if ((flags & DUK__IVAL_FLAG_REQUIRE_TEMP) && !DUK__ISTEMP(comp_ctx, x->regconst)) {
  48857. duk_reg_t dest = DUK__ALLOCTEMP(comp_ctx);
  48858. duk__emit_a_bc(comp_ctx, DUK_OP_LDREG, (duk_regconst_t) dest, x->regconst);
  48859. return (duk_regconst_t) dest;
  48860. } else {
  48861. return x->regconst;
  48862. }
  48863. }
  48864. }
  48865. }
  48866. default: {
  48867. break;
  48868. }
  48869. }
  48870. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR);
  48871. return 0;
  48872. }
  48873. DUK_LOCAL void duk__ispec_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ispec *x, duk_reg_t forced_reg) {
  48874. DUK_ASSERT(forced_reg >= 0);
  48875. (void) duk__ispec_toregconst_raw(comp_ctx, x, forced_reg, 0 /*flags*/);
  48876. }
  48877. /* Coerce an duk_ivalue to a 'plain' value by generating the necessary
  48878. * arithmetic operations, property access, or variable access bytecode.
  48879. * The duk_ivalue argument ('x') is converted into a plain value as a
  48880. * side effect.
  48881. */
  48882. DUK_LOCAL void duk__ivalue_toplain_raw(duk_compiler_ctx *comp_ctx, duk_ivalue *x, duk_reg_t forced_reg) {
  48883. duk_hthread *thr = comp_ctx->thr;
  48884. duk_context *ctx = (duk_context *) thr;
  48885. DUK_DDD(DUK_DDDPRINT("duk__ivalue_toplain_raw(): x={t=%ld,op=%ld,x1={%ld:%ld:%!T},x2={%ld:%ld:%!T}}, "
  48886. "forced_reg=%ld",
  48887. (long) x->t, (long) x->op,
  48888. (long) x->x1.t, (long) x->x1.regconst,
  48889. (duk_tval *) duk_get_tval(ctx, x->x1.valstack_idx),
  48890. (long) x->x2.t, (long) x->x2.regconst,
  48891. (duk_tval *) duk_get_tval(ctx, x->x2.valstack_idx),
  48892. (long) forced_reg));
  48893. switch (x->t) {
  48894. case DUK_IVAL_PLAIN: {
  48895. return;
  48896. }
  48897. /* XXX: support unary arithmetic ivalues (useful?) */
  48898. case DUK_IVAL_ARITH:
  48899. case DUK_IVAL_ARITH_EXTRAOP: {
  48900. duk_regconst_t arg1;
  48901. duk_regconst_t arg2;
  48902. duk_reg_t dest;
  48903. duk_tval *tv1;
  48904. duk_tval *tv2;
  48905. DUK_DDD(DUK_DDDPRINT("arith to plain conversion"));
  48906. /* inline arithmetic check for constant values */
  48907. /* XXX: use the exactly same arithmetic function here as in executor */
  48908. if (x->x1.t == DUK_ISPEC_VALUE && x->x2.t == DUK_ISPEC_VALUE && x->t == DUK_IVAL_ARITH) {
  48909. tv1 = duk_get_tval(ctx, x->x1.valstack_idx);
  48910. tv2 = duk_get_tval(ctx, x->x2.valstack_idx);
  48911. DUK_ASSERT(tv1 != NULL);
  48912. DUK_ASSERT(tv2 != NULL);
  48913. DUK_DDD(DUK_DDDPRINT("arith: tv1=%!T, tv2=%!T",
  48914. (duk_tval *) tv1,
  48915. (duk_tval *) tv2));
  48916. if (DUK_TVAL_IS_NUMBER(tv1) && DUK_TVAL_IS_NUMBER(tv2)) {
  48917. duk_double_t d1 = DUK_TVAL_GET_NUMBER(tv1);
  48918. duk_double_t d2 = DUK_TVAL_GET_NUMBER(tv2);
  48919. duk_double_t d3;
  48920. duk_bool_t accept = 1;
  48921. DUK_DDD(DUK_DDDPRINT("arith inline check: d1=%lf, d2=%lf, op=%ld",
  48922. (double) d1, (double) d2, (long) x->op));
  48923. switch (x->op) {
  48924. case DUK_OP_ADD: d3 = d1 + d2; break;
  48925. case DUK_OP_SUB: d3 = d1 - d2; break;
  48926. case DUK_OP_MUL: d3 = d1 * d2; break;
  48927. case DUK_OP_DIV: d3 = d1 / d2; break;
  48928. default: accept = 0; break;
  48929. }
  48930. if (accept) {
  48931. duk_double_union du;
  48932. du.d = d3;
  48933. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  48934. d3 = du.d;
  48935. x->t = DUK_IVAL_PLAIN;
  48936. DUK_ASSERT(x->x1.t == DUK_ISPEC_VALUE);
  48937. DUK_TVAL_SET_NUMBER(tv1, d3); /* old value is number: no refcount */
  48938. return;
  48939. }
  48940. } else if (x->op == DUK_OP_ADD && DUK_TVAL_IS_STRING(tv1) && DUK_TVAL_IS_STRING(tv2)) {
  48941. /* inline string concatenation */
  48942. duk_dup(ctx, x->x1.valstack_idx);
  48943. duk_dup(ctx, x->x2.valstack_idx);
  48944. duk_concat(ctx, 2);
  48945. duk_replace(ctx, x->x1.valstack_idx);
  48946. x->t = DUK_IVAL_PLAIN;
  48947. DUK_ASSERT(x->x1.t == DUK_ISPEC_VALUE);
  48948. return;
  48949. }
  48950. }
  48951. arg1 = duk__ispec_toregconst_raw(comp_ctx, &x->x1, -1, DUK__IVAL_FLAG_ALLOW_CONST | DUK__IVAL_FLAG_REQUIRE_SHORT /*flags*/);
  48952. arg2 = duk__ispec_toregconst_raw(comp_ctx, &x->x2, -1, DUK__IVAL_FLAG_ALLOW_CONST | DUK__IVAL_FLAG_REQUIRE_SHORT /*flags*/);
  48953. /* If forced reg, use it as destination. Otherwise try to
  48954. * use either coerced ispec if it is a temporary.
  48955. *
  48956. * When using extraops, avoid reusing arg2 as dest because that
  48957. * would lead to an LDREG shuffle below. We still can't guarantee
  48958. * dest != arg2 because we may have a forced_reg.
  48959. */
  48960. if (forced_reg >= 0) {
  48961. dest = forced_reg;
  48962. } else if (DUK__ISTEMP(comp_ctx, arg1)) {
  48963. dest = (duk_reg_t) arg1;
  48964. } else if (DUK__ISTEMP(comp_ctx, arg2) && x->t != DUK_IVAL_ARITH_EXTRAOP) {
  48965. dest = (duk_reg_t) arg2;
  48966. } else {
  48967. dest = DUK__ALLOCTEMP(comp_ctx);
  48968. }
  48969. /* Extraop arithmetic opcodes must have destination same as
  48970. * first source. If second source matches destination we need
  48971. * a temporary register to avoid clobbering the second source.
  48972. *
  48973. * XXX: change calling code to avoid this situation in most cases.
  48974. */
  48975. if (x->t == DUK_IVAL_ARITH_EXTRAOP) {
  48976. if (!(DUK__ISREG(comp_ctx, arg1) && (duk_reg_t) arg1 == dest)) {
  48977. if (DUK__ISREG(comp_ctx, arg2) && (duk_reg_t) arg2 == dest) {
  48978. /* arg2 would be clobbered so reassign it to a temp. */
  48979. duk_reg_t tempreg;
  48980. tempreg = DUK__ALLOCTEMP(comp_ctx);
  48981. duk__emit_a_bc(comp_ctx, DUK_OP_LDREG, tempreg, arg2);
  48982. arg2 = tempreg;
  48983. }
  48984. if (DUK__ISREG(comp_ctx, arg1)) {
  48985. duk__emit_a_bc(comp_ctx, DUK_OP_LDREG, dest, arg1);
  48986. } else {
  48987. DUK_ASSERT(DUK__ISCONST(comp_ctx, arg1));
  48988. duk__emit_a_bc(comp_ctx, DUK_OP_LDCONST, dest, arg1);
  48989. }
  48990. }
  48991. /* Note: special DUK__EMIT_FLAG_B_IS_TARGETSOURCE
  48992. * used to indicate that B is both a source and a
  48993. * target register. When shuffled, it needs to be
  48994. * both input and output shuffled.
  48995. */
  48996. DUK_ASSERT(DUK__ISREG(comp_ctx, dest));
  48997. duk__emit_extraop_b_c(comp_ctx,
  48998. x->op | DUK__EMIT_FLAG_B_IS_TARGET |
  48999. DUK__EMIT_FLAG_B_IS_TARGETSOURCE,
  49000. (duk_regconst_t) dest,
  49001. (duk_regconst_t) arg2);
  49002. } else {
  49003. DUK_ASSERT(DUK__ISREG(comp_ctx, dest));
  49004. duk__emit_a_b_c(comp_ctx, x->op, (duk_regconst_t) dest, arg1, arg2);
  49005. }
  49006. x->t = DUK_IVAL_PLAIN;
  49007. x->x1.t = DUK_ISPEC_REGCONST;
  49008. x->x1.regconst = (duk_regconst_t) dest;
  49009. return;
  49010. }
  49011. case DUK_IVAL_PROP: {
  49012. /* XXX: very similar to DUK_IVAL_ARITH - merge? */
  49013. duk_regconst_t arg1;
  49014. duk_regconst_t arg2;
  49015. duk_reg_t dest;
  49016. /* Need a short reg/const, does not have to be a mutable temp. */
  49017. arg1 = duk__ispec_toregconst_raw(comp_ctx, &x->x1, -1, DUK__IVAL_FLAG_ALLOW_CONST | DUK__IVAL_FLAG_REQUIRE_SHORT /*flags*/);
  49018. arg2 = duk__ispec_toregconst_raw(comp_ctx, &x->x2, -1, DUK__IVAL_FLAG_ALLOW_CONST | DUK__IVAL_FLAG_REQUIRE_SHORT /*flags*/);
  49019. /* Pick a destination register. If either base value or key
  49020. * happens to be a temp value, reuse it as the destination.
  49021. *
  49022. * XXX: The temp must be a "mutable" one, i.e. such that no
  49023. * other expression is using it anymore. Here this should be
  49024. * the case because the value of a property access expression
  49025. * is neither the base nor the key, but the lookup result.
  49026. */
  49027. if (forced_reg >= 0) {
  49028. dest = forced_reg;
  49029. } else if (DUK__ISTEMP(comp_ctx, arg1)) {
  49030. dest = (duk_reg_t) arg1;
  49031. } else if (DUK__ISTEMP(comp_ctx, arg2)) {
  49032. dest = (duk_reg_t) arg2;
  49033. } else {
  49034. dest = DUK__ALLOCTEMP(comp_ctx);
  49035. }
  49036. duk__emit_a_b_c(comp_ctx, DUK_OP_GETPROP, (duk_regconst_t) dest, arg1, arg2);
  49037. x->t = DUK_IVAL_PLAIN;
  49038. x->x1.t = DUK_ISPEC_REGCONST;
  49039. x->x1.regconst = (duk_regconst_t) dest;
  49040. return;
  49041. }
  49042. case DUK_IVAL_VAR: {
  49043. /* x1 must be a string */
  49044. duk_reg_t dest;
  49045. duk_reg_t reg_varbind;
  49046. duk_regconst_t rc_varname;
  49047. DUK_ASSERT(x->x1.t == DUK_ISPEC_VALUE);
  49048. duk_dup(ctx, x->x1.valstack_idx);
  49049. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  49050. x->t = DUK_IVAL_PLAIN;
  49051. x->x1.t = DUK_ISPEC_REGCONST;
  49052. x->x1.regconst = (duk_regconst_t) reg_varbind;
  49053. } else {
  49054. dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  49055. duk__emit_a_bc(comp_ctx, DUK_OP_GETVAR, (duk_regconst_t) dest, rc_varname);
  49056. x->t = DUK_IVAL_PLAIN;
  49057. x->x1.t = DUK_ISPEC_REGCONST;
  49058. x->x1.regconst = (duk_regconst_t) dest;
  49059. }
  49060. return;
  49061. }
  49062. case DUK_IVAL_NONE:
  49063. default: {
  49064. break;
  49065. }
  49066. }
  49067. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR);
  49068. return;
  49069. }
  49070. /* evaluate to plain value, no forced register (temp/bound reg both ok) */
  49071. DUK_LOCAL void duk__ivalue_toplain(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  49072. duk__ivalue_toplain_raw(comp_ctx, x, -1 /*forced_reg*/);
  49073. }
  49074. /* evaluate to final form (e.g. coerce GETPROP to code), throw away temp */
  49075. DUK_LOCAL void duk__ivalue_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  49076. duk_reg_t temp;
  49077. /* If duk__ivalue_toplain_raw() allocates a temp, forget it and
  49078. * restore next temp state.
  49079. */
  49080. temp = DUK__GETTEMP(comp_ctx);
  49081. duk__ivalue_toplain_raw(comp_ctx, x, -1 /*forced_reg*/);
  49082. DUK__SETTEMP(comp_ctx, temp);
  49083. }
  49084. /* Coerce an duk_ivalue to a register or constant; result register may
  49085. * be a temp or a bound register.
  49086. *
  49087. * The duk_ivalue argument ('x') is converted into a regconst as a
  49088. * side effect.
  49089. */
  49090. DUK_LOCAL
  49091. duk_regconst_t duk__ivalue_toregconst_raw(duk_compiler_ctx *comp_ctx,
  49092. duk_ivalue *x,
  49093. duk_reg_t forced_reg,
  49094. duk_small_uint_t flags) {
  49095. duk_hthread *thr = comp_ctx->thr;
  49096. duk_context *ctx = (duk_context *) thr;
  49097. duk_regconst_t reg;
  49098. DUK_UNREF(thr);
  49099. DUK_UNREF(ctx);
  49100. DUK_DDD(DUK_DDDPRINT("duk__ivalue_toregconst_raw(): x={t=%ld,op=%ld,x1={%ld:%ld:%!T},x2={%ld:%ld:%!T}}, "
  49101. "forced_reg=%ld, flags 0x%08lx: allow_const=%ld require_temp=%ld require_short=%ld",
  49102. (long) x->t, (long) x->op,
  49103. (long) x->x1.t, (long) x->x1.regconst,
  49104. (duk_tval *) duk_get_tval(ctx, x->x1.valstack_idx),
  49105. (long) x->x2.t, (long) x->x2.regconst,
  49106. (duk_tval *) duk_get_tval(ctx, x->x2.valstack_idx),
  49107. (long) forced_reg,
  49108. (unsigned long) flags,
  49109. (long) ((flags & DUK__IVAL_FLAG_ALLOW_CONST) ? 1 : 0),
  49110. (long) ((flags & DUK__IVAL_FLAG_REQUIRE_TEMP) ? 1 : 0),
  49111. (long) ((flags & DUK__IVAL_FLAG_REQUIRE_SHORT) ? 1 : 0)));
  49112. /* first coerce to a plain value */
  49113. duk__ivalue_toplain_raw(comp_ctx, x, forced_reg);
  49114. DUK_ASSERT(x->t == DUK_IVAL_PLAIN);
  49115. /* then to a register */
  49116. reg = duk__ispec_toregconst_raw(comp_ctx, &x->x1, forced_reg, flags);
  49117. x->x1.t = DUK_ISPEC_REGCONST;
  49118. x->x1.regconst = reg;
  49119. return reg;
  49120. }
  49121. DUK_LOCAL duk_reg_t duk__ivalue_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  49122. return duk__ivalue_toregconst_raw(comp_ctx, x, -1, 0 /*flags*/);
  49123. }
  49124. #if 0 /* unused */
  49125. DUK_LOCAL duk_reg_t duk__ivalue_totempreg(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  49126. return duk__ivalue_toregconst_raw(comp_ctx, x, -1, DUK__IVAL_FLAG_REQUIRE_TEMP /*flags*/);
  49127. }
  49128. #endif
  49129. DUK_LOCAL void duk__ivalue_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ivalue *x, duk_int_t forced_reg) {
  49130. DUK_ASSERT(forced_reg >= 0);
  49131. (void) duk__ivalue_toregconst_raw(comp_ctx, x, forced_reg, 0 /*flags*/);
  49132. }
  49133. DUK_LOCAL duk_regconst_t duk__ivalue_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  49134. return duk__ivalue_toregconst_raw(comp_ctx, x, -1, DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  49135. }
  49136. /* The issues below can be solved with better flags */
  49137. /* XXX: many operations actually want toforcedtemp() -- brand new temp? */
  49138. /* XXX: need a toplain_ignore() which will only coerce a value to a temp
  49139. * register if it might have a side effect. Side-effect free values do not
  49140. * need to be coerced.
  49141. */
  49142. /*
  49143. * Identifier handling
  49144. */
  49145. DUK_LOCAL duk_reg_t duk__lookup_active_register_binding(duk_compiler_ctx *comp_ctx) {
  49146. duk_hthread *thr = comp_ctx->thr;
  49147. duk_context *ctx = (duk_context *) thr;
  49148. duk_hstring *h_varname;
  49149. duk_reg_t ret;
  49150. DUK_DDD(DUK_DDDPRINT("resolving identifier reference to '%!T'",
  49151. (duk_tval *) duk_get_tval(ctx, -1)));
  49152. /*
  49153. * Special name handling
  49154. */
  49155. h_varname = duk_get_hstring(ctx, -1);
  49156. DUK_ASSERT(h_varname != NULL);
  49157. if (h_varname == DUK_HTHREAD_STRING_LC_ARGUMENTS(thr)) {
  49158. DUK_DDD(DUK_DDDPRINT("flagging function as accessing 'arguments'"));
  49159. comp_ctx->curr_func.id_access_arguments = 1;
  49160. }
  49161. /*
  49162. * Inside one or more 'with' statements fall back to slow path always.
  49163. * (See e.g. test-stmt-with.js.)
  49164. */
  49165. if (comp_ctx->curr_func.with_depth > 0) {
  49166. DUK_DDD(DUK_DDDPRINT("identifier lookup inside a 'with' -> fall back to slow path"));
  49167. goto slow_path;
  49168. }
  49169. /*
  49170. * Any catch bindings ("catch (e)") also affect identifier binding.
  49171. *
  49172. * Currently, the varmap is modified for the duration of the catch
  49173. * clause to ensure any identifier accesses with the catch variable
  49174. * name will use slow path.
  49175. */
  49176. duk_get_prop(ctx, comp_ctx->curr_func.varmap_idx);
  49177. if (duk_is_number(ctx, -1)) {
  49178. ret = duk_to_int(ctx, -1);
  49179. duk_pop(ctx);
  49180. } else {
  49181. duk_pop(ctx);
  49182. goto slow_path;
  49183. }
  49184. DUK_DDD(DUK_DDDPRINT("identifier lookup -> reg %ld", (long) ret));
  49185. return ret;
  49186. slow_path:
  49187. DUK_DDD(DUK_DDDPRINT("identifier lookup -> slow path"));
  49188. comp_ctx->curr_func.id_access_slow = 1;
  49189. return (duk_reg_t) -1;
  49190. }
  49191. /* Lookup an identifier name in the current varmap, indicating whether the
  49192. * identifier is register-bound and if not, allocating a constant for the
  49193. * identifier name. Returns 1 if register-bound, 0 otherwise. Caller can
  49194. * also check (out_reg_varbind >= 0) to check whether or not identifier is
  49195. * register bound. The caller must NOT use out_rc_varname at all unless
  49196. * return code is 0 or out_reg_varbind is < 0; this is becuase out_rc_varname
  49197. * is unsigned and doesn't have a "unused" / none value.
  49198. */
  49199. 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) {
  49200. duk_hthread *thr = comp_ctx->thr;
  49201. duk_context *ctx = (duk_context *) thr;
  49202. duk_reg_t reg_varbind;
  49203. duk_regconst_t rc_varname;
  49204. /* [ ... varname ] */
  49205. duk_dup_top(ctx);
  49206. reg_varbind = duk__lookup_active_register_binding(comp_ctx);
  49207. if (reg_varbind >= 0) {
  49208. *out_reg_varbind = reg_varbind;
  49209. *out_rc_varname = 0; /* duk_regconst_t is unsigned, so use 0 as dummy value (ignored by caller) */
  49210. duk_pop(ctx);
  49211. return 1;
  49212. } else {
  49213. rc_varname = duk__getconst(comp_ctx);
  49214. *out_reg_varbind = -1;
  49215. *out_rc_varname = rc_varname;
  49216. return 0;
  49217. }
  49218. }
  49219. /*
  49220. * Label handling
  49221. *
  49222. * Labels are initially added with flags prohibiting both break and continue.
  49223. * When the statement type is finally uncovered (after potentially multiple
  49224. * labels), all the labels are updated to allow/prohibit break and continue.
  49225. */
  49226. 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) {
  49227. duk_hthread *thr = comp_ctx->thr;
  49228. duk_context *ctx = (duk_context *) thr;
  49229. duk_size_t n;
  49230. duk_size_t new_size;
  49231. duk_uint8_t *p;
  49232. duk_labelinfo *li_start, *li;
  49233. /* Duplicate (shadowing) labels are not allowed, except for the empty
  49234. * labels (which are used as default labels for switch and iteration
  49235. * statements).
  49236. *
  49237. * We could also allow shadowing of non-empty pending labels without any
  49238. * other issues than breaking the required label shadowing requirements
  49239. * of the E5 specification, see Section 12.12.
  49240. */
  49241. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, comp_ctx->curr_func.h_labelinfos);
  49242. li_start = (duk_labelinfo *) p;
  49243. li = (duk_labelinfo *) (p + DUK_HBUFFER_GET_SIZE(comp_ctx->curr_func.h_labelinfos));
  49244. n = (duk_size_t) (li - li_start);
  49245. while (li > li_start) {
  49246. li--;
  49247. if (li->h_label == h_label && h_label != DUK_HTHREAD_STRING_EMPTY_STRING(thr)) {
  49248. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_DUPLICATE_LABEL);
  49249. }
  49250. }
  49251. duk_push_hstring(ctx, h_label);
  49252. DUK_ASSERT(n <= DUK_UARRIDX_MAX); /* label limits */
  49253. (void) duk_put_prop_index(ctx, comp_ctx->curr_func.labelnames_idx, (duk_uarridx_t) n);
  49254. new_size = (n + 1) * sizeof(duk_labelinfo);
  49255. duk_hbuffer_resize(thr, comp_ctx->curr_func.h_labelinfos, new_size, new_size);
  49256. /* XXX: spare handling, slow now */
  49257. /* relookup after possible realloc */
  49258. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, comp_ctx->curr_func.h_labelinfos);
  49259. li_start = (duk_labelinfo *) p;
  49260. DUK_UNREF(li_start); /* silence scan-build warning */
  49261. li = (duk_labelinfo *) (p + DUK_HBUFFER_GET_SIZE(comp_ctx->curr_func.h_labelinfos));
  49262. li--;
  49263. /* Labels can be used for iteration statements but also for other statements,
  49264. * in particular a label can be used for a block statement. All cases of a
  49265. * named label accept a 'break' so that flag is set here. Iteration statements
  49266. * also allow 'continue', so that flag is updated when we figure out the
  49267. * statement type.
  49268. */
  49269. li->flags = DUK_LABEL_FLAG_ALLOW_BREAK;
  49270. li->label_id = label_id;
  49271. li->h_label = h_label;
  49272. li->catch_depth = comp_ctx->curr_func.catch_depth; /* catch depth from current func */
  49273. li->pc_label = pc_label;
  49274. DUK_DDD(DUK_DDDPRINT("registered label: flags=0x%08lx, id=%ld, name=%!O, catch_depth=%ld, pc_label=%ld",
  49275. (unsigned long) li->flags, (long) li->label_id, (duk_heaphdr *) li->h_label,
  49276. (long) li->catch_depth, (long) li->pc_label));
  49277. }
  49278. /* Update all labels with matching label_id. */
  49279. DUK_LOCAL void duk__update_label_flags(duk_compiler_ctx *comp_ctx, duk_int_t label_id, duk_small_uint_t flags) {
  49280. duk_uint8_t *p;
  49281. duk_labelinfo *li_start, *li;
  49282. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(comp_ctx->thr->heap, comp_ctx->curr_func.h_labelinfos);
  49283. li_start = (duk_labelinfo *) p;
  49284. li = (duk_labelinfo *) (p + DUK_HBUFFER_GET_SIZE(comp_ctx->curr_func.h_labelinfos));
  49285. /* Match labels starting from latest; once label_id no longer matches, we can
  49286. * safely exit without checking the rest of the labels (only the topmost labels
  49287. * are ever updated).
  49288. */
  49289. while (li > li_start) {
  49290. li--;
  49291. if (li->label_id != label_id) {
  49292. break;
  49293. }
  49294. DUK_DDD(DUK_DDDPRINT("updating (overwriting) label flags for li=%p, label_id=%ld, flags=%ld",
  49295. (void *) li, (long) label_id, (long) flags));
  49296. li->flags = flags;
  49297. }
  49298. }
  49299. /* Lookup active label information. Break/continue distinction is necessary to handle switch
  49300. * statement related labels correctly: a switch will only catch a 'break', not a 'continue'.
  49301. *
  49302. * An explicit label cannot appear multiple times in the active set, but empty labels (unlabelled
  49303. * iteration and switch statements) can. A break will match the closest unlabelled or labelled
  49304. * statement. A continue will match the closest unlabelled or labelled iteration statement. It is
  49305. * a syntax error if a continue matches a labelled switch statement; because an explicit label cannot
  49306. * be duplicated, the continue cannot match any valid label outside the switch.
  49307. *
  49308. * A side effect of these rules is that a LABEL statement related to a switch should never actually
  49309. * catch a continue abrupt completion at run-time. Hence an INVALID opcode can be placed in the
  49310. * continue slot of the switch's LABEL statement.
  49311. */
  49312. /* XXX: awkward, especially the bunch of separate output values -> output struct? */
  49313. 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) {
  49314. duk_hthread *thr = comp_ctx->thr;
  49315. duk_context *ctx = (duk_context *) thr;
  49316. duk_uint8_t *p;
  49317. duk_labelinfo *li_start, *li_end, *li;
  49318. duk_bool_t match = 0;
  49319. DUK_DDD(DUK_DDDPRINT("looking up active label: label='%!O', is_break=%ld",
  49320. (duk_heaphdr *) h_label, (long) is_break));
  49321. DUK_UNREF(ctx);
  49322. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, comp_ctx->curr_func.h_labelinfos);
  49323. li_start = (duk_labelinfo *) p;
  49324. li_end = (duk_labelinfo *) (p + DUK_HBUFFER_GET_SIZE(comp_ctx->curr_func.h_labelinfos));
  49325. li = li_end;
  49326. /* Match labels starting from latest label because there can be duplicate empty
  49327. * labels in the label set.
  49328. */
  49329. while (li > li_start) {
  49330. li--;
  49331. if (li->h_label != h_label) {
  49332. DUK_DDD(DUK_DDDPRINT("labelinfo[%ld] ->'%!O' != %!O",
  49333. (long) (li - li_start),
  49334. (duk_heaphdr *) li->h_label,
  49335. (duk_heaphdr *) h_label));
  49336. continue;
  49337. }
  49338. DUK_DDD(DUK_DDDPRINT("labelinfo[%ld] -> '%!O' label name matches (still need to check type)",
  49339. (long) (li - li_start), (duk_heaphdr *) h_label));
  49340. /* currently all labels accept a break, so no explicit check for it now */
  49341. DUK_ASSERT(li->flags & DUK_LABEL_FLAG_ALLOW_BREAK);
  49342. if (is_break) {
  49343. /* break matches always */
  49344. match = 1;
  49345. break;
  49346. } else if (li->flags & DUK_LABEL_FLAG_ALLOW_CONTINUE) {
  49347. /* iteration statements allow continue */
  49348. match = 1;
  49349. break;
  49350. } else {
  49351. /* continue matched this label -- we can only continue if this is the empty
  49352. * label, for which duplication is allowed, and thus there is hope of
  49353. * finding a match deeper in the label stack.
  49354. */
  49355. if (h_label != DUK_HTHREAD_STRING_EMPTY_STRING(thr)) {
  49356. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_LABEL);
  49357. } else {
  49358. DUK_DDD(DUK_DDDPRINT("continue matched an empty label which does not "
  49359. "allow a continue -> continue lookup deeper in label stack"));
  49360. }
  49361. }
  49362. }
  49363. /* XXX: match flag is awkward, rework */
  49364. if (!match) {
  49365. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_LABEL);
  49366. }
  49367. DUK_DDD(DUK_DDDPRINT("label match: %!O -> label_id %ld, catch_depth=%ld, pc_label=%ld",
  49368. (duk_heaphdr *) h_label, (long) li->label_id,
  49369. (long) li->catch_depth, (long) li->pc_label));
  49370. *out_label_id = li->label_id;
  49371. *out_label_catch_depth = li->catch_depth;
  49372. *out_label_pc = li->pc_label;
  49373. *out_is_closest = (li == li_end - 1);
  49374. }
  49375. DUK_LOCAL void duk__reset_labels_to_length(duk_compiler_ctx *comp_ctx, duk_int_t len) {
  49376. duk_hthread *thr = comp_ctx->thr;
  49377. duk_context *ctx = (duk_context *) thr;
  49378. duk_size_t new_size;
  49379. /* XXX: duk_set_length */
  49380. new_size = sizeof(duk_labelinfo) * (duk_size_t) len;
  49381. duk_push_int(ctx, len);
  49382. duk_put_prop_stridx(ctx, comp_ctx->curr_func.labelnames_idx, DUK_STRIDX_LENGTH);
  49383. duk_hbuffer_resize(thr, comp_ctx->curr_func.h_labelinfos, new_size, new_size); /* XXX: spare handling */
  49384. }
  49385. /*
  49386. * Expression parsing: duk__expr_nud(), duk__expr_led(), duk__expr_lbp(), and helpers.
  49387. *
  49388. * - duk__expr_nud(): ("null denotation"): process prev_token as a "start" of an expression (e.g. literal)
  49389. * - duk__expr_led(): ("left denotation"): process prev_token in the "middle" of an expression (e.g. operator)
  49390. * - duk__expr_lbp(): ("left-binding power"): return left-binding power of curr_token
  49391. */
  49392. /* object literal key tracking flags */
  49393. #define DUK__OBJ_LIT_KEY_PLAIN (1 << 0) /* key encountered as a plain property */
  49394. #define DUK__OBJ_LIT_KEY_GET (1 << 1) /* key encountered as a getter */
  49395. #define DUK__OBJ_LIT_KEY_SET (1 << 2) /* key encountered as a setter */
  49396. DUK_LOCAL void duk__nud_array_literal(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  49397. duk_hthread *thr = comp_ctx->thr;
  49398. duk_reg_t reg_obj; /* result reg */
  49399. duk_reg_t reg_temp; /* temp reg */
  49400. duk_reg_t temp_start; /* temp reg value for start of loop */
  49401. duk_small_uint_t max_init_values; /* max # of values initialized in one MPUTARR set */
  49402. duk_small_uint_t num_values; /* number of values in current MPUTARR set */
  49403. duk_uarridx_t curr_idx; /* current (next) array index */
  49404. duk_uarridx_t start_idx; /* start array index of current MPUTARR set */
  49405. duk_uarridx_t init_idx; /* last array index explicitly initialized, +1 */
  49406. duk_bool_t require_comma; /* next loop requires a comma */
  49407. /* DUK_TOK_LBRACKET already eaten, current token is right after that */
  49408. DUK_ASSERT(comp_ctx->prev_token.t == DUK_TOK_LBRACKET);
  49409. max_init_values = DUK__MAX_ARRAY_INIT_VALUES; /* XXX: depend on available temps? */
  49410. reg_obj = DUK__ALLOCTEMP(comp_ctx);
  49411. duk__emit_extraop_b_c(comp_ctx,
  49412. DUK_EXTRAOP_NEWARR | DUK__EMIT_FLAG_B_IS_TARGET,
  49413. reg_obj,
  49414. 0); /* XXX: patch initial size afterwards? */
  49415. temp_start = DUK__GETTEMP(comp_ctx);
  49416. /*
  49417. * Emit initializers in sets of maximum max_init_values.
  49418. * Corner cases such as single value initializers do not have
  49419. * special handling now.
  49420. *
  49421. * Elided elements must not be emitted as 'undefined' values,
  49422. * because such values would be enumerable (which is incorrect).
  49423. * Also note that trailing elisions must be reflected in the
  49424. * length of the final array but cause no elements to be actually
  49425. * inserted.
  49426. */
  49427. curr_idx = 0;
  49428. init_idx = 0; /* tracks maximum initialized index + 1 */
  49429. start_idx = 0;
  49430. require_comma = 0;
  49431. for (;;) {
  49432. num_values = 0;
  49433. DUK__SETTEMP(comp_ctx, temp_start);
  49434. if (comp_ctx->curr_token.t == DUK_TOK_RBRACKET) {
  49435. break;
  49436. }
  49437. for (;;) {
  49438. if (comp_ctx->curr_token.t == DUK_TOK_RBRACKET) {
  49439. /* the outer loop will recheck and exit */
  49440. break;
  49441. }
  49442. /* comma check */
  49443. if (require_comma) {
  49444. if (comp_ctx->curr_token.t == DUK_TOK_COMMA) {
  49445. /* comma after a value, expected */
  49446. duk__advance(comp_ctx);
  49447. require_comma = 0;
  49448. continue;
  49449. } else {
  49450. goto syntax_error;
  49451. }
  49452. } else {
  49453. if (comp_ctx->curr_token.t == DUK_TOK_COMMA) {
  49454. /* elision - flush */
  49455. curr_idx++;
  49456. duk__advance(comp_ctx);
  49457. /* if num_values > 0, MPUTARR emitted by outer loop after break */
  49458. break;
  49459. }
  49460. }
  49461. /* else an array initializer element */
  49462. /* initial index */
  49463. if (num_values == 0) {
  49464. start_idx = curr_idx;
  49465. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  49466. duk__emit_load_int32(comp_ctx, reg_temp, (duk_int32_t) start_idx);
  49467. }
  49468. reg_temp = DUK__ALLOCTEMP(comp_ctx); /* alloc temp just in case, to update max temp */
  49469. DUK__SETTEMP(comp_ctx, reg_temp);
  49470. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_COMMA /*rbp_flags*/, reg_temp /*forced_reg*/);
  49471. DUK__SETTEMP(comp_ctx, reg_temp + 1);
  49472. num_values++;
  49473. curr_idx++;
  49474. require_comma = 1;
  49475. if (num_values >= max_init_values) {
  49476. /* MPUTARR emitted by outer loop */
  49477. break;
  49478. }
  49479. }
  49480. if (num_values > 0) {
  49481. /* - A is a source register (it's not a write target, but used
  49482. * to identify the target object) but can be shuffled.
  49483. * - B cannot be shuffled normally because it identifies a range
  49484. * of registers, the emitter has special handling for this
  49485. * (the "no shuffle" flag must not be set).
  49486. * - C is a non-register number and cannot be shuffled, but
  49487. * never needs to be.
  49488. */
  49489. duk__emit_a_b_c(comp_ctx,
  49490. DUK_OP_MPUTARR |
  49491. DUK__EMIT_FLAG_NO_SHUFFLE_C |
  49492. DUK__EMIT_FLAG_A_IS_SOURCE,
  49493. (duk_regconst_t) reg_obj,
  49494. (duk_regconst_t) temp_start,
  49495. (duk_regconst_t) num_values);
  49496. init_idx = start_idx + num_values;
  49497. /* num_values and temp_start reset at top of outer loop */
  49498. }
  49499. }
  49500. DUK_ASSERT(comp_ctx->curr_token.t == DUK_TOK_RBRACKET);
  49501. duk__advance(comp_ctx);
  49502. DUK_DDD(DUK_DDDPRINT("array literal done, curridx=%ld, initidx=%ld",
  49503. (long) curr_idx, (long) init_idx));
  49504. /* trailing elisions? */
  49505. if (curr_idx > init_idx) {
  49506. /* yes, must set array length explicitly */
  49507. DUK_DDD(DUK_DDDPRINT("array literal has trailing elisions which affect its length"));
  49508. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  49509. duk__emit_load_int32(comp_ctx, reg_temp, (duk_int_t) curr_idx);
  49510. duk__emit_extraop_b_c(comp_ctx,
  49511. DUK_EXTRAOP_SETALEN,
  49512. (duk_regconst_t) reg_obj,
  49513. (duk_regconst_t) reg_temp);
  49514. }
  49515. DUK__SETTEMP(comp_ctx, temp_start);
  49516. res->t = DUK_IVAL_PLAIN;
  49517. res->x1.t = DUK_ISPEC_REGCONST;
  49518. res->x1.regconst = (duk_regconst_t) reg_obj;
  49519. return;
  49520. syntax_error:
  49521. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_ARRAY_LITERAL);
  49522. }
  49523. /* duplicate/invalid key checks; returns 1 if syntax error */
  49524. DUK_LOCAL duk_bool_t duk__nud_object_literal_key_check(duk_compiler_ctx *comp_ctx, duk_small_uint_t new_key_flags) {
  49525. duk_hthread *thr = comp_ctx->thr;
  49526. duk_context *ctx = (duk_context *) thr;
  49527. duk_small_uint_t key_flags;
  49528. /* [ ... key_obj key ] */
  49529. DUK_ASSERT(duk_is_string(ctx, -1));
  49530. /*
  49531. * 'key_obj' tracks keys encountered so far by associating an
  49532. * integer with flags with already encountered keys. The checks
  49533. * below implement E5 Section 11.1.5, step 4 for production:
  49534. *
  49535. * PropertyNameAndValueList: PropertyNameAndValueList , PropertyAssignment
  49536. */
  49537. duk_dup(ctx, -1); /* [ ... key_obj key key ] */
  49538. duk_get_prop(ctx, -3); /* [ ... key_obj key val ] */
  49539. key_flags = duk_to_int(ctx, -1);
  49540. duk_pop(ctx); /* [ ... key_obj key ] */
  49541. if (new_key_flags & DUK__OBJ_LIT_KEY_PLAIN) {
  49542. if ((key_flags & DUK__OBJ_LIT_KEY_PLAIN) && comp_ctx->curr_func.is_strict) {
  49543. /* step 4.a */
  49544. DUK_DDD(DUK_DDDPRINT("duplicate key: plain key appears twice in strict mode"));
  49545. return 1;
  49546. }
  49547. if (key_flags & (DUK__OBJ_LIT_KEY_GET | DUK__OBJ_LIT_KEY_SET)) {
  49548. /* step 4.c */
  49549. DUK_DDD(DUK_DDDPRINT("duplicate key: plain key encountered after setter/getter"));
  49550. return 1;
  49551. }
  49552. } else {
  49553. if (key_flags & DUK__OBJ_LIT_KEY_PLAIN) {
  49554. /* step 4.b */
  49555. DUK_DDD(DUK_DDDPRINT("duplicate key: getter/setter encountered after plain key"));
  49556. return 1;
  49557. }
  49558. if (key_flags & new_key_flags) {
  49559. /* step 4.d */
  49560. DUK_DDD(DUK_DDDPRINT("duplicate key: getter/setter encountered twice"));
  49561. return 1;
  49562. }
  49563. }
  49564. new_key_flags |= key_flags;
  49565. DUK_DDD(DUK_DDDPRINT("setting/updating key %!T flags: 0x%08lx -> 0x%08lx",
  49566. (duk_tval *) duk_get_tval(ctx, -1),
  49567. (unsigned long) key_flags,
  49568. (unsigned long) new_key_flags));
  49569. duk_dup(ctx, -1);
  49570. duk_push_int(ctx, new_key_flags); /* [ ... key_obj key key flags ] */
  49571. duk_put_prop(ctx, -4); /* [ ... key_obj key ] */
  49572. return 0;
  49573. }
  49574. DUK_LOCAL void duk__nud_object_literal(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  49575. duk_hthread *thr = comp_ctx->thr;
  49576. duk_context *ctx = (duk_context *) thr;
  49577. duk_reg_t reg_obj; /* result reg */
  49578. duk_reg_t reg_key; /* temp reg for key literal */
  49579. duk_reg_t reg_temp; /* temp reg */
  49580. duk_reg_t temp_start; /* temp reg value for start of loop */
  49581. duk_small_uint_t max_init_pairs; /* max # of key-value pairs initialized in one MPUTOBJ set */
  49582. duk_small_uint_t num_pairs; /* number of pairs in current MPUTOBJ set */
  49583. duk_bool_t first; /* first value: comma must not precede the value */
  49584. duk_bool_t is_set, is_get; /* temps */
  49585. DUK_ASSERT(comp_ctx->prev_token.t == DUK_TOK_LCURLY);
  49586. max_init_pairs = DUK__MAX_OBJECT_INIT_PAIRS; /* XXX: depend on available temps? */
  49587. reg_obj = DUK__ALLOCTEMP(comp_ctx);
  49588. duk__emit_extraop_b_c(comp_ctx,
  49589. DUK_EXTRAOP_NEWOBJ | DUK__EMIT_FLAG_B_IS_TARGET,
  49590. reg_obj,
  49591. 0); /* XXX: patch initial size afterwards? */
  49592. temp_start = DUK__GETTEMP(comp_ctx);
  49593. /* temp object for tracking / detecting duplicate keys */
  49594. duk_push_object(ctx);
  49595. /*
  49596. * Emit initializers in sets of maximum max_init_pairs keys.
  49597. * Setter/getter is handled separately and terminates the
  49598. * current set of initializer values. Corner cases such as
  49599. * single value initializers do not have special handling now.
  49600. */
  49601. first = 1;
  49602. for (;;) {
  49603. num_pairs = 0;
  49604. DUK__SETTEMP(comp_ctx, temp_start);
  49605. if (comp_ctx->curr_token.t == DUK_TOK_RCURLY) {
  49606. break;
  49607. }
  49608. for (;;) {
  49609. /*
  49610. * Three possible element formats:
  49611. * 1) PropertyName : AssignmentExpression
  49612. * 2) get PropertyName () { FunctionBody }
  49613. * 3) set PropertyName ( PropertySetParameterList ) { FunctionBody }
  49614. *
  49615. * PropertyName can be IdentifierName (includes reserved words), a string
  49616. * literal, or a number literal. Note that IdentifierName allows 'get' and
  49617. * 'set' too, so we need to look ahead to the next token to distinguish:
  49618. *
  49619. * { get : 1 }
  49620. *
  49621. * and
  49622. *
  49623. * { get foo() { return 1 } }
  49624. * { get get() { return 1 } } // 'get' as getter propertyname
  49625. *
  49626. * Finally, a trailing comma is allowed.
  49627. *
  49628. * Key name is coerced to string at compile time (and ends up as a
  49629. * a string constant) even for numeric keys (e.g. "{1:'foo'}").
  49630. * These could be emitted using e.g. LDINT, but that seems hardly
  49631. * worth the effort and would increase code size.
  49632. */
  49633. DUK_DDD(DUK_DDDPRINT("object literal inner loop, curr_token->t = %ld",
  49634. (long) comp_ctx->curr_token.t));
  49635. if (comp_ctx->curr_token.t == DUK_TOK_RCURLY) {
  49636. /* the outer loop will recheck and exit */
  49637. break;
  49638. }
  49639. if (num_pairs >= max_init_pairs) {
  49640. /* MPUTOBJ emitted by outer loop */
  49641. break;
  49642. }
  49643. if (first) {
  49644. first = 0;
  49645. } else {
  49646. if (comp_ctx->curr_token.t != DUK_TOK_COMMA) {
  49647. goto syntax_error;
  49648. }
  49649. duk__advance(comp_ctx);
  49650. if (comp_ctx->curr_token.t == DUK_TOK_RCURLY) {
  49651. /* trailing comma followed by rcurly */
  49652. break;
  49653. }
  49654. }
  49655. /* advance to get one step of lookup */
  49656. duk__advance(comp_ctx);
  49657. /* NOTE: "get" and "set" are not officially ReservedWords and the lexer
  49658. * currently treats them always like ordinary identifiers (DUK_TOK_GET
  49659. * and DUK_TOK_SET are unused). They need to be detected based on the
  49660. * identifier string content.
  49661. */
  49662. is_get = (comp_ctx->prev_token.t == DUK_TOK_IDENTIFIER &&
  49663. comp_ctx->prev_token.str1 == DUK_HTHREAD_STRING_GET(thr));
  49664. is_set = (comp_ctx->prev_token.t == DUK_TOK_IDENTIFIER &&
  49665. comp_ctx->prev_token.str1 == DUK_HTHREAD_STRING_SET(thr));
  49666. if ((is_get || is_set) && comp_ctx->curr_token.t != DUK_TOK_COLON) {
  49667. /* getter/setter */
  49668. duk_int_t fnum;
  49669. if (comp_ctx->curr_token.t_nores == DUK_TOK_IDENTIFIER ||
  49670. comp_ctx->curr_token.t_nores == DUK_TOK_STRING) {
  49671. /* same handling for identifiers and strings */
  49672. DUK_ASSERT(comp_ctx->curr_token.str1 != NULL);
  49673. duk_push_hstring(ctx, comp_ctx->curr_token.str1);
  49674. } else if (comp_ctx->curr_token.t == DUK_TOK_NUMBER) {
  49675. duk_push_number(ctx, comp_ctx->curr_token.num);
  49676. duk_to_string(ctx, -1);
  49677. } else {
  49678. goto syntax_error;
  49679. }
  49680. DUK_ASSERT(duk_is_string(ctx, -1));
  49681. if (duk__nud_object_literal_key_check(comp_ctx,
  49682. (is_get ? DUK__OBJ_LIT_KEY_GET : DUK__OBJ_LIT_KEY_SET))) {
  49683. goto syntax_error;
  49684. }
  49685. reg_key = duk__getconst(comp_ctx);
  49686. if (num_pairs > 0) {
  49687. /* - A is a source register (it's not a write target, but used
  49688. * to identify the target object) but can be shuffled.
  49689. * - B cannot be shuffled normally because it identifies a range
  49690. * of registers, the emitter has special handling for this
  49691. * (the "no shuffle" flag must not be set).
  49692. * - C is a non-register number and cannot be shuffled, but
  49693. * never needs to be.
  49694. */
  49695. duk__emit_a_b_c(comp_ctx,
  49696. DUK_OP_MPUTOBJ |
  49697. DUK__EMIT_FLAG_NO_SHUFFLE_C |
  49698. DUK__EMIT_FLAG_A_IS_SOURCE,
  49699. reg_obj,
  49700. temp_start,
  49701. num_pairs);
  49702. num_pairs = 0;
  49703. DUK__SETTEMP(comp_ctx, temp_start);
  49704. }
  49705. /* curr_token = get/set name */
  49706. fnum = duk__parse_func_like_fnum(comp_ctx, 0 /*is_decl*/, 1 /*is_setget*/);
  49707. DUK_ASSERT(DUK__GETTEMP(comp_ctx) == temp_start);
  49708. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  49709. duk__emit_a_bc(comp_ctx,
  49710. DUK_OP_LDCONST,
  49711. (duk_regconst_t) reg_temp,
  49712. (duk_regconst_t) reg_key);
  49713. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  49714. duk__emit_a_bc(comp_ctx,
  49715. DUK_OP_CLOSURE,
  49716. (duk_regconst_t) reg_temp,
  49717. (duk_regconst_t) fnum);
  49718. /* Slot C is used in a non-standard fashion (range of regs),
  49719. * emitter code has special handling for it (must not set the
  49720. * "no shuffle" flag).
  49721. */
  49722. duk__emit_extraop_b_c(comp_ctx,
  49723. (is_get ? DUK_EXTRAOP_INITGET : DUK_EXTRAOP_INITSET),
  49724. reg_obj,
  49725. temp_start); /* temp_start+0 = key, temp_start+1 = closure */
  49726. DUK__SETTEMP(comp_ctx, temp_start);
  49727. } else {
  49728. /* normal key/value */
  49729. if (comp_ctx->prev_token.t_nores == DUK_TOK_IDENTIFIER ||
  49730. comp_ctx->prev_token.t_nores == DUK_TOK_STRING) {
  49731. /* same handling for identifiers and strings */
  49732. DUK_ASSERT(comp_ctx->prev_token.str1 != NULL);
  49733. duk_push_hstring(ctx, comp_ctx->prev_token.str1);
  49734. } else if (comp_ctx->prev_token.t == DUK_TOK_NUMBER) {
  49735. duk_push_number(ctx, comp_ctx->prev_token.num);
  49736. duk_to_string(ctx, -1);
  49737. } else {
  49738. goto syntax_error;
  49739. }
  49740. DUK_ASSERT(duk_is_string(ctx, -1));
  49741. if (duk__nud_object_literal_key_check(comp_ctx, DUK__OBJ_LIT_KEY_PLAIN)) {
  49742. goto syntax_error;
  49743. }
  49744. reg_key = duk__getconst(comp_ctx);
  49745. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  49746. duk__emit_a_bc(comp_ctx,
  49747. DUK_OP_LDCONST,
  49748. (duk_regconst_t) reg_temp,
  49749. (duk_regconst_t) reg_key);
  49750. duk__advance_expect(comp_ctx, DUK_TOK_COLON);
  49751. reg_temp = DUK__ALLOCTEMP(comp_ctx); /* alloc temp just in case, to update max temp */
  49752. DUK__SETTEMP(comp_ctx, reg_temp);
  49753. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_COMMA /*rbp_flags*/, reg_temp /*forced_reg*/);
  49754. DUK__SETTEMP(comp_ctx, reg_temp + 1);
  49755. num_pairs++;
  49756. }
  49757. }
  49758. if (num_pairs > 0) {
  49759. /* See MPUTOBJ comments above. */
  49760. duk__emit_a_b_c(comp_ctx,
  49761. DUK_OP_MPUTOBJ |
  49762. DUK__EMIT_FLAG_NO_SHUFFLE_C |
  49763. DUK__EMIT_FLAG_A_IS_SOURCE,
  49764. reg_obj,
  49765. temp_start,
  49766. num_pairs);
  49767. /* num_pairs and temp_start reset at top of outer loop */
  49768. }
  49769. }
  49770. DUK_ASSERT(comp_ctx->curr_token.t == DUK_TOK_RCURLY);
  49771. duk__advance(comp_ctx);
  49772. DUK__SETTEMP(comp_ctx, temp_start);
  49773. res->t = DUK_IVAL_PLAIN;
  49774. res->x1.t = DUK_ISPEC_REGCONST;
  49775. res->x1.regconst = (duk_regconst_t) reg_obj;
  49776. DUK_DDD(DUK_DDDPRINT("final tracking object: %!T",
  49777. (duk_tval *) duk_get_tval(ctx, -1)));
  49778. duk_pop(ctx);
  49779. return;
  49780. syntax_error:
  49781. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_OBJECT_LITERAL);
  49782. }
  49783. /* Parse argument list. Arguments are written to temps starting from
  49784. * "next temp". Returns number of arguments parsed. Expects left paren
  49785. * to be already eaten, and eats the right paren before returning.
  49786. */
  49787. DUK_LOCAL duk_int_t duk__parse_arguments(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  49788. duk_int_t nargs = 0;
  49789. duk_reg_t reg_temp;
  49790. /* Note: expect that caller has already eaten the left paren */
  49791. DUK_DDD(DUK_DDDPRINT("start parsing arguments, prev_token.t=%ld, curr_token.t=%ld",
  49792. (long) comp_ctx->prev_token.t, (long) comp_ctx->curr_token.t));
  49793. for (;;) {
  49794. if (comp_ctx->curr_token.t == DUK_TOK_RPAREN) {
  49795. break;
  49796. }
  49797. if (nargs > 0) {
  49798. duk__advance_expect(comp_ctx, DUK_TOK_COMMA);
  49799. }
  49800. /* We want the argument expression value to go to "next temp"
  49801. * without additional moves. That should almost always be the
  49802. * case, but we double check after expression parsing.
  49803. *
  49804. * This is not the cleanest possible approach.
  49805. */
  49806. reg_temp = DUK__ALLOCTEMP(comp_ctx); /* bump up "allocated" reg count, just in case */
  49807. DUK__SETTEMP(comp_ctx, reg_temp);
  49808. /* binding power must be high enough to NOT allow comma expressions directly */
  49809. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_COMMA /*rbp_flags*/, reg_temp); /* always allow 'in', coerce to 'tr' just in case */
  49810. DUK__SETTEMP(comp_ctx, reg_temp + 1);
  49811. nargs++;
  49812. DUK_DDD(DUK_DDDPRINT("argument #%ld written into reg %ld", (long) nargs, (long) reg_temp));
  49813. }
  49814. /* eat the right paren */
  49815. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  49816. DUK_DDD(DUK_DDDPRINT("end parsing arguments"));
  49817. return nargs;
  49818. }
  49819. DUK_LOCAL duk_bool_t duk__expr_is_empty(duk_compiler_ctx *comp_ctx) {
  49820. /* empty expressions can be detected conveniently with nud/led counts */
  49821. return (comp_ctx->curr_func.nud_count == 0) &&
  49822. (comp_ctx->curr_func.led_count == 0);
  49823. }
  49824. DUK_LOCAL void duk__expr_nud(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  49825. duk_hthread *thr = comp_ctx->thr;
  49826. duk_context *ctx = (duk_context *) thr;
  49827. duk_token *tk;
  49828. duk_reg_t temp_at_entry;
  49829. duk_small_int_t tok;
  49830. duk_uint32_t args; /* temp variable to pass constants and flags to shared code */
  49831. /*
  49832. * ctx->prev_token token to process with duk__expr_nud()
  49833. * ctx->curr_token updated by caller
  49834. *
  49835. * Note: the token in the switch below has already been eaten.
  49836. */
  49837. temp_at_entry = DUK__GETTEMP(comp_ctx);
  49838. comp_ctx->curr_func.nud_count++;
  49839. tk = &comp_ctx->prev_token;
  49840. tok = tk->t;
  49841. res->t = DUK_IVAL_NONE;
  49842. DUK_DDD(DUK_DDDPRINT("duk__expr_nud(), prev_token.t=%ld, allow_in=%ld, paren_level=%ld",
  49843. (long) tk->t, (long) comp_ctx->curr_func.allow_in, (long) comp_ctx->curr_func.paren_level));
  49844. switch (tok) {
  49845. /* PRIMARY EXPRESSIONS */
  49846. case DUK_TOK_THIS: {
  49847. duk_reg_t reg_temp;
  49848. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  49849. duk__emit_extraop_bc(comp_ctx,
  49850. DUK_EXTRAOP_LDTHIS,
  49851. (duk_regconst_t) reg_temp);
  49852. res->t = DUK_IVAL_PLAIN;
  49853. res->x1.t = DUK_ISPEC_REGCONST;
  49854. res->x1.regconst = (duk_regconst_t) reg_temp;
  49855. return;
  49856. }
  49857. case DUK_TOK_IDENTIFIER: {
  49858. res->t = DUK_IVAL_VAR;
  49859. res->x1.t = DUK_ISPEC_VALUE;
  49860. duk_push_hstring(ctx, tk->str1);
  49861. duk_replace(ctx, res->x1.valstack_idx);
  49862. return;
  49863. }
  49864. case DUK_TOK_NULL: {
  49865. duk_push_null(ctx);
  49866. goto plain_value;
  49867. }
  49868. case DUK_TOK_TRUE: {
  49869. duk_push_true(ctx);
  49870. goto plain_value;
  49871. }
  49872. case DUK_TOK_FALSE: {
  49873. duk_push_false(ctx);
  49874. goto plain_value;
  49875. }
  49876. case DUK_TOK_NUMBER: {
  49877. duk_push_number(ctx, tk->num);
  49878. goto plain_value;
  49879. }
  49880. case DUK_TOK_STRING: {
  49881. DUK_ASSERT(tk->str1 != NULL);
  49882. duk_push_hstring(ctx, tk->str1);
  49883. goto plain_value;
  49884. }
  49885. case DUK_TOK_REGEXP: {
  49886. #ifdef DUK_USE_REGEXP_SUPPORT
  49887. duk_reg_t reg_temp;
  49888. duk_regconst_t rc_re_bytecode; /* const */
  49889. duk_regconst_t rc_re_source; /* const */
  49890. DUK_ASSERT(tk->str1 != NULL);
  49891. DUK_ASSERT(tk->str2 != NULL);
  49892. DUK_DDD(DUK_DDDPRINT("emitting regexp op, str1=%!O, str2=%!O",
  49893. (duk_heaphdr *) tk->str1,
  49894. (duk_heaphdr *) tk->str2));
  49895. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  49896. duk_push_hstring(ctx, tk->str1);
  49897. duk_push_hstring(ctx, tk->str2);
  49898. /* [ ... pattern flags ] */
  49899. duk_regexp_compile(thr);
  49900. /* [ ... escaped_source bytecode ] */
  49901. rc_re_bytecode = duk__getconst(comp_ctx);
  49902. rc_re_source = duk__getconst(comp_ctx);
  49903. duk__emit_a_b_c(comp_ctx,
  49904. DUK_OP_REGEXP,
  49905. (duk_regconst_t) reg_temp /*a*/,
  49906. rc_re_bytecode /*b*/,
  49907. rc_re_source /*c*/);
  49908. res->t = DUK_IVAL_PLAIN;
  49909. res->x1.t = DUK_ISPEC_REGCONST;
  49910. res->x1.regconst = (duk_regconst_t) reg_temp;
  49911. return;
  49912. #else /* DUK_USE_REGEXP_SUPPORT */
  49913. goto syntax_error;
  49914. #endif /* DUK_USE_REGEXP_SUPPORT */
  49915. }
  49916. case DUK_TOK_LBRACKET: {
  49917. DUK_DDD(DUK_DDDPRINT("parsing array literal"));
  49918. duk__nud_array_literal(comp_ctx, res);
  49919. return;
  49920. }
  49921. case DUK_TOK_LCURLY: {
  49922. DUK_DDD(DUK_DDDPRINT("parsing object literal"));
  49923. duk__nud_object_literal(comp_ctx, res);
  49924. return;
  49925. }
  49926. case DUK_TOK_LPAREN: {
  49927. duk_bool_t prev_allow_in;
  49928. comp_ctx->curr_func.paren_level++;
  49929. prev_allow_in = comp_ctx->curr_func.allow_in;
  49930. comp_ctx->curr_func.allow_in = 1; /* reset 'allow_in' for parenthesized expression */
  49931. duk__expr(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/); /* Expression, terminates at a ')' */
  49932. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  49933. comp_ctx->curr_func.allow_in = prev_allow_in;
  49934. comp_ctx->curr_func.paren_level--;
  49935. return;
  49936. }
  49937. /* MEMBER/NEW/CALL EXPRESSIONS */
  49938. case DUK_TOK_NEW: {
  49939. /*
  49940. * Parsing an expression starting with 'new' is tricky because
  49941. * there are multiple possible productions deriving from
  49942. * LeftHandSideExpression which begin with 'new'.
  49943. *
  49944. * We currently resort to one-token lookahead to distinguish the
  49945. * cases. Hopefully this is correct. The binding power must be
  49946. * such that parsing ends at an LPAREN (CallExpression) but not at
  49947. * a PERIOD or LBRACKET (MemberExpression).
  49948. *
  49949. * See doc/compiler.txt for discussion on the parsing approach,
  49950. * and testcases/test-dev-new.js for a bunch of documented tests.
  49951. */
  49952. duk_reg_t reg_target;
  49953. duk_int_t nargs;
  49954. DUK_DDD(DUK_DDDPRINT("begin parsing new expression"));
  49955. reg_target = DUK__ALLOCTEMP(comp_ctx);
  49956. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_CALL /*rbp_flags*/, reg_target /*forced_reg*/);
  49957. DUK__SETTEMP(comp_ctx, reg_target + 1);
  49958. if (comp_ctx->curr_token.t == DUK_TOK_LPAREN) {
  49959. /* 'new' MemberExpression Arguments */
  49960. DUK_DDD(DUK_DDDPRINT("new expression has argument list"));
  49961. duk__advance(comp_ctx);
  49962. nargs = duk__parse_arguments(comp_ctx, res); /* parse args starting from "next temp", reg_target + 1 */
  49963. /* right paren eaten */
  49964. } else {
  49965. /* 'new' MemberExpression */
  49966. DUK_DDD(DUK_DDDPRINT("new expression has no argument list"));
  49967. nargs = 0;
  49968. }
  49969. /* Opcode slot C is used in a non-standard way, so shuffling
  49970. * is not allowed.
  49971. */
  49972. duk__emit_a_b_c(comp_ctx,
  49973. DUK_OP_NEW | DUK__EMIT_FLAG_NO_SHUFFLE_A | DUK__EMIT_FLAG_NO_SHUFFLE_C,
  49974. 0 /*unused*/,
  49975. reg_target /*target*/,
  49976. nargs /*num_args*/);
  49977. DUK_DDD(DUK_DDDPRINT("end parsing new expression"));
  49978. res->t = DUK_IVAL_PLAIN;
  49979. res->x1.t = DUK_ISPEC_REGCONST;
  49980. res->x1.regconst = (duk_regconst_t) reg_target;
  49981. return;
  49982. }
  49983. /* FUNCTION EXPRESSIONS */
  49984. case DUK_TOK_FUNCTION: {
  49985. /* Function expression. Note that any statement beginning with 'function'
  49986. * is handled by the statement parser as a function declaration, or a
  49987. * non-standard function expression/statement (or a SyntaxError). We only
  49988. * handle actual function expressions (occurring inside an expression) here.
  49989. *
  49990. * O(depth^2) parse count for inner functions is handled by recording a
  49991. * lexer offset on the first compilation pass, so that the function can
  49992. * be efficiently skipped on the second pass. This is encapsulated into
  49993. * duk__parse_func_like_fnum().
  49994. */
  49995. duk_reg_t reg_temp;
  49996. duk_int_t fnum;
  49997. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  49998. /* curr_token follows 'function' */
  49999. fnum = duk__parse_func_like_fnum(comp_ctx, 0 /*is_decl*/, 0 /*is_setget*/);
  50000. DUK_DDD(DUK_DDDPRINT("parsed inner function -> fnum %ld", (long) fnum));
  50001. duk__emit_a_bc(comp_ctx,
  50002. DUK_OP_CLOSURE,
  50003. (duk_regconst_t) reg_temp /*a*/,
  50004. (duk_regconst_t) fnum /*bc*/);
  50005. res->t = DUK_IVAL_PLAIN;
  50006. res->x1.t = DUK_ISPEC_REGCONST;
  50007. res->x1.regconst = (duk_regconst_t) reg_temp;
  50008. return;
  50009. }
  50010. /* UNARY EXPRESSIONS */
  50011. case DUK_TOK_DELETE: {
  50012. /* Delete semantics are a bit tricky. The description in E5 specification
  50013. * is kind of confusing, because it distinguishes between resolvability of
  50014. * a reference (which is only known at runtime) seemingly at compile time
  50015. * (= SyntaxError throwing).
  50016. */
  50017. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  50018. if (res->t == DUK_IVAL_VAR) {
  50019. /* not allowed in strict mode, regardless of whether resolves;
  50020. * in non-strict mode DELVAR handles both non-resolving and
  50021. * resolving cases (the specification description is a bit confusing).
  50022. */
  50023. duk_reg_t reg_temp;
  50024. duk_reg_t reg_varbind;
  50025. duk_regconst_t rc_varname;
  50026. if (comp_ctx->curr_func.is_strict) {
  50027. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_CANNOT_DELETE_IDENTIFIER);
  50028. }
  50029. DUK__SETTEMP(comp_ctx, temp_at_entry);
  50030. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  50031. duk_dup(ctx, res->x1.valstack_idx);
  50032. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  50033. /* register bound variables are non-configurable -> always false */
  50034. duk__emit_extraop_bc(comp_ctx,
  50035. DUK_EXTRAOP_LDFALSE,
  50036. (duk_regconst_t) reg_temp);
  50037. } else {
  50038. duk_dup(ctx, res->x1.valstack_idx);
  50039. rc_varname = duk__getconst(comp_ctx);
  50040. duk__emit_a_b(comp_ctx,
  50041. DUK_OP_DELVAR,
  50042. (duk_regconst_t) reg_temp,
  50043. (duk_regconst_t) rc_varname);
  50044. }
  50045. res->t = DUK_IVAL_PLAIN;
  50046. res->x1.t = DUK_ISPEC_REGCONST;
  50047. res->x1.regconst = (duk_regconst_t) reg_temp;
  50048. } else if (res->t == DUK_IVAL_PROP) {
  50049. duk_reg_t reg_temp;
  50050. duk_reg_t reg_obj;
  50051. duk_regconst_t rc_key;
  50052. DUK__SETTEMP(comp_ctx, temp_at_entry);
  50053. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  50054. reg_obj = duk__ispec_toregconst_raw(comp_ctx, &res->x1, -1 /*forced_reg*/, 0 /*flags*/); /* don't allow const */
  50055. rc_key = duk__ispec_toregconst_raw(comp_ctx, &res->x2, -1 /*forced_reg*/, DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  50056. duk__emit_a_b_c(comp_ctx,
  50057. DUK_OP_DELPROP,
  50058. (duk_regconst_t) reg_temp,
  50059. (duk_regconst_t) reg_obj,
  50060. rc_key);
  50061. res->t = DUK_IVAL_PLAIN;
  50062. res->x1.t = DUK_ISPEC_REGCONST;
  50063. res->x1.regconst = (duk_regconst_t) reg_temp;
  50064. } else {
  50065. /* non-Reference deletion is always 'true', even in strict mode */
  50066. duk_push_true(ctx);
  50067. goto plain_value;
  50068. }
  50069. return;
  50070. }
  50071. case DUK_TOK_VOID: {
  50072. duk__expr_toplain_ignore(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  50073. duk_push_undefined(ctx);
  50074. goto plain_value;
  50075. }
  50076. case DUK_TOK_TYPEOF: {
  50077. /* 'typeof' must handle unresolvable references without throwing
  50078. * a ReferenceError (E5 Section 11.4.3). Register mapped values
  50079. * will never be unresolvable so special handling is only required
  50080. * when an identifier is a "slow path" one.
  50081. */
  50082. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  50083. if (res->t == DUK_IVAL_VAR) {
  50084. duk_reg_t reg_varbind;
  50085. duk_regconst_t rc_varname;
  50086. duk_reg_t reg_temp;
  50087. duk_dup(ctx, res->x1.valstack_idx);
  50088. if (!duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  50089. DUK_DDD(DUK_DDDPRINT("typeof for an identifier name which could not be resolved "
  50090. "at compile time, need to use special run-time handling"));
  50091. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  50092. duk__emit_extraop_b_c(comp_ctx,
  50093. DUK_EXTRAOP_TYPEOFID | DUK__EMIT_FLAG_B_IS_TARGET,
  50094. reg_temp,
  50095. rc_varname);
  50096. res->t = DUK_IVAL_PLAIN;
  50097. res->x1.t = DUK_ISPEC_REGCONST;
  50098. res->x1.regconst = (duk_regconst_t) reg_temp;
  50099. return;
  50100. }
  50101. }
  50102. args = (DUK_EXTRAOP_TYPEOF << 8) + 0;
  50103. goto unary_extraop;
  50104. }
  50105. case DUK_TOK_INCREMENT: {
  50106. args = (DUK_OP_PREINCR << 8) + 0;
  50107. goto preincdec;
  50108. }
  50109. case DUK_TOK_DECREMENT: {
  50110. args = (DUK_OP_PREDECR << 8) + 0;
  50111. goto preincdec;
  50112. }
  50113. case DUK_TOK_ADD: {
  50114. /* unary plus */
  50115. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  50116. if (res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_VALUE &&
  50117. duk_is_number(ctx, res->x1.valstack_idx)) {
  50118. /* unary plus of a number is identity */
  50119. ;
  50120. return;
  50121. }
  50122. args = (DUK_EXTRAOP_UNP << 8) + 0;
  50123. goto unary_extraop;
  50124. }
  50125. case DUK_TOK_SUB: {
  50126. /* unary minus */
  50127. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  50128. if (res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_VALUE &&
  50129. duk_is_number(ctx, res->x1.valstack_idx)) {
  50130. /* this optimization is important to handle negative literals (which are not directly
  50131. * provided by the lexical grammar
  50132. */
  50133. duk_tval *tv_num = duk_get_tval(ctx, res->x1.valstack_idx);
  50134. duk_double_union du;
  50135. DUK_ASSERT(tv_num != NULL);
  50136. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_num));
  50137. du.d = DUK_TVAL_GET_NUMBER(tv_num);
  50138. du.d = -du.d;
  50139. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  50140. DUK_TVAL_SET_NUMBER(tv_num, du.d);
  50141. return;
  50142. }
  50143. args = (DUK_EXTRAOP_UNM << 8) + 0;
  50144. goto unary_extraop;
  50145. }
  50146. case DUK_TOK_BNOT: {
  50147. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  50148. args = (DUK_EXTRAOP_BNOT << 8) + 0;
  50149. goto unary_extraop;
  50150. }
  50151. case DUK_TOK_LNOT: {
  50152. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  50153. if (res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_VALUE) {
  50154. /* Very minimal inlining to handle common idioms '!0' and '!1',
  50155. * and also boolean arguments like '!false' and '!true'.
  50156. */
  50157. duk_tval *tv_val = duk_get_tval(ctx, res->x1.valstack_idx);
  50158. DUK_ASSERT(tv_val != NULL);
  50159. if (DUK_TVAL_IS_NUMBER(tv_val)) {
  50160. duk_double_t d;
  50161. d = DUK_TVAL_GET_NUMBER(tv_val);
  50162. if (d == 0.0) {
  50163. /* Matches both +0 and -0 on purpose. */
  50164. DUK_DDD(DUK_DDDPRINT("inlined lnot: !0 -> true"));
  50165. DUK_TVAL_SET_BOOLEAN_TRUE(tv_val);
  50166. return;
  50167. } else if (d == 1.0) {
  50168. DUK_DDD(DUK_DDDPRINT("inlined lnot: !1 -> false"));
  50169. DUK_TVAL_SET_BOOLEAN_FALSE(tv_val);
  50170. return;
  50171. }
  50172. } else if (DUK_TVAL_IS_BOOLEAN(tv_val)) {
  50173. duk_small_int_t v;
  50174. v = DUK_TVAL_GET_BOOLEAN(tv_val);
  50175. DUK_DDD(DUK_DDDPRINT("inlined lnot boolean: %ld", (long) v));
  50176. DUK_ASSERT(v == 0 || v == 1);
  50177. DUK_TVAL_SET_BOOLEAN(tv_val, v ^ 0x01);
  50178. return;
  50179. }
  50180. }
  50181. args = (DUK_EXTRAOP_LNOT << 8) + 0;
  50182. goto unary_extraop;
  50183. }
  50184. } /* end switch */
  50185. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_PARSE_ERROR);
  50186. return;
  50187. unary_extraop:
  50188. {
  50189. /* Note: must coerce to a (writable) temp register, so that e.g. "!x" where x
  50190. * is a reg-mapped variable works correctly (does not mutate the variable register).
  50191. */
  50192. duk_reg_t reg_temp;
  50193. reg_temp = duk__ivalue_toregconst_raw(comp_ctx, res, -1 /*forced_reg*/, DUK__IVAL_FLAG_REQUIRE_TEMP /*flags*/);
  50194. duk__emit_extraop_bc(comp_ctx,
  50195. (args >> 8),
  50196. (duk_regconst_t) reg_temp);
  50197. res->t = DUK_IVAL_PLAIN;
  50198. res->x1.t = DUK_ISPEC_REGCONST;
  50199. res->x1.regconst = (duk_regconst_t) reg_temp;
  50200. return;
  50201. }
  50202. preincdec:
  50203. {
  50204. /* preincrement and predecrement */
  50205. duk_reg_t reg_res;
  50206. duk_small_uint_t args_op = args >> 8;
  50207. /* Specific assumptions for opcode numbering. */
  50208. DUK_ASSERT(DUK_OP_PREINCR + 4 == DUK_OP_PREINCV);
  50209. DUK_ASSERT(DUK_OP_PREDECR + 4 == DUK_OP_PREDECV);
  50210. DUK_ASSERT(DUK_OP_PREINCR + 8 == DUK_OP_PREINCP);
  50211. DUK_ASSERT(DUK_OP_PREDECR + 8 == DUK_OP_PREDECP);
  50212. reg_res = DUK__ALLOCTEMP(comp_ctx);
  50213. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  50214. if (res->t == DUK_IVAL_VAR) {
  50215. duk_hstring *h_varname;
  50216. duk_reg_t reg_varbind;
  50217. duk_regconst_t rc_varname;
  50218. h_varname = duk_get_hstring(ctx, res->x1.valstack_idx);
  50219. DUK_ASSERT(h_varname != NULL);
  50220. if (duk__hstring_is_eval_or_arguments_in_strict_mode(comp_ctx, h_varname)) {
  50221. goto syntax_error;
  50222. }
  50223. duk_dup(ctx, res->x1.valstack_idx);
  50224. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  50225. duk__emit_a_bc(comp_ctx,
  50226. args_op, /* e.g. DUK_OP_PREINCR */
  50227. (duk_regconst_t) reg_res,
  50228. (duk_regconst_t) reg_varbind);
  50229. } else {
  50230. duk__emit_a_bc(comp_ctx,
  50231. args_op + 4, /* e.g. DUK_OP_PREINCV */
  50232. (duk_regconst_t) reg_res,
  50233. rc_varname);
  50234. }
  50235. DUK_DDD(DUK_DDDPRINT("preincdec to '%!O' -> reg_varbind=%ld, rc_varname=%ld",
  50236. (duk_heaphdr *) h_varname, (long) reg_varbind, (long) rc_varname));
  50237. } else if (res->t == DUK_IVAL_PROP) {
  50238. duk_reg_t reg_obj; /* allocate to reg only (not const) */
  50239. duk_regconst_t rc_key;
  50240. reg_obj = duk__ispec_toregconst_raw(comp_ctx, &res->x1, -1 /*forced_reg*/, 0 /*flags*/); /* don't allow const */
  50241. rc_key = duk__ispec_toregconst_raw(comp_ctx, &res->x2, -1 /*forced_reg*/, DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  50242. duk__emit_a_b_c(comp_ctx,
  50243. args_op + 8, /* e.g. DUK_OP_PREINCP */
  50244. (duk_regconst_t) reg_res,
  50245. (duk_regconst_t) reg_obj,
  50246. rc_key);
  50247. } else {
  50248. /* Technically return value is not needed because INVLHS will
  50249. * unconditially throw a ReferenceError. Coercion is necessary
  50250. * for proper semantics (consider ToNumber() called for an object).
  50251. * Use DUK_EXTRAOP_UNP with a dummy register to get ToNumber().
  50252. */
  50253. duk__ivalue_toforcedreg(comp_ctx, res, reg_res);
  50254. duk__emit_extraop_bc(comp_ctx,
  50255. DUK_EXTRAOP_UNP,
  50256. reg_res); /* for side effects, result ignored */
  50257. duk__emit_extraop_only(comp_ctx,
  50258. DUK_EXTRAOP_INVLHS);
  50259. }
  50260. res->t = DUK_IVAL_PLAIN;
  50261. res->x1.t = DUK_ISPEC_REGCONST;
  50262. res->x1.regconst = (duk_regconst_t) reg_res;
  50263. DUK__SETTEMP(comp_ctx, reg_res + 1);
  50264. return;
  50265. }
  50266. plain_value:
  50267. {
  50268. /* Stack top contains plain value */
  50269. res->t = DUK_IVAL_PLAIN;
  50270. res->x1.t = DUK_ISPEC_VALUE;
  50271. duk_replace(ctx, res->x1.valstack_idx);
  50272. return;
  50273. }
  50274. syntax_error:
  50275. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_EXPRESSION);
  50276. }
  50277. /* XXX: add flag to indicate whether caller cares about return value; this
  50278. * affects e.g. handling of assignment expressions. This change needs API
  50279. * changes elsewhere too.
  50280. */
  50281. DUK_LOCAL void duk__expr_led(duk_compiler_ctx *comp_ctx, duk_ivalue *left, duk_ivalue *res) {
  50282. duk_hthread *thr = comp_ctx->thr;
  50283. duk_context *ctx = (duk_context *) thr;
  50284. duk_token *tk;
  50285. duk_small_int_t tok;
  50286. duk_uint32_t args; /* temp variable to pass constants and flags to shared code */
  50287. /*
  50288. * ctx->prev_token token to process with duk__expr_led()
  50289. * ctx->curr_token updated by caller
  50290. */
  50291. comp_ctx->curr_func.led_count++;
  50292. /* The token in the switch has already been eaten here */
  50293. tk = &comp_ctx->prev_token;
  50294. tok = tk->t;
  50295. DUK_DDD(DUK_DDDPRINT("duk__expr_led(), prev_token.t=%ld, allow_in=%ld, paren_level=%ld",
  50296. (long) tk->t, (long) comp_ctx->curr_func.allow_in, (long) comp_ctx->curr_func.paren_level));
  50297. /* XXX: default priority for infix operators is duk__expr_lbp(tok) -> get it here? */
  50298. switch (tok) {
  50299. /* PRIMARY EXPRESSIONS */
  50300. case DUK_TOK_PERIOD: {
  50301. /* Property access expressions are critical for correct LHS ordering,
  50302. * see comments in duk__expr()!
  50303. */
  50304. /* XXX: this now coerces an identifier into a GETVAR to a temp, which
  50305. * causes an extra LDREG in call setup. It's sufficient to coerce to a
  50306. * unary ivalue?
  50307. */
  50308. duk__ivalue_toplain(comp_ctx, left);
  50309. /* NB: must accept reserved words as property name */
  50310. if (comp_ctx->curr_token.t_nores != DUK_TOK_IDENTIFIER) {
  50311. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_EXPECTED_IDENTIFIER);
  50312. }
  50313. res->t = DUK_IVAL_PROP;
  50314. duk__copy_ispec(comp_ctx, &left->x1, &res->x1); /* left.x1 -> res.x1 */
  50315. DUK_ASSERT(comp_ctx->curr_token.str1 != NULL);
  50316. duk_push_hstring(ctx, comp_ctx->curr_token.str1);
  50317. duk_replace(ctx, res->x2.valstack_idx);
  50318. res->x2.t = DUK_ISPEC_VALUE;
  50319. /* special RegExp literal handling after IdentifierName */
  50320. comp_ctx->curr_func.reject_regexp_in_adv = 1;
  50321. duk__advance(comp_ctx);
  50322. return;
  50323. }
  50324. case DUK_TOK_LBRACKET: {
  50325. /* Property access expressions are critical for correct LHS ordering,
  50326. * see comments in duk__expr()!
  50327. */
  50328. /* XXX: optimize temp reg use */
  50329. /* XXX: similar coercion issue as in DUK_TOK_PERIOD */
  50330. /* XXX: coerce to regs? it might be better for enumeration use, where the
  50331. * same PROP ivalue is used multiple times. Or perhaps coerce PROP further
  50332. * there?
  50333. */
  50334. duk__ivalue_toplain(comp_ctx, left);
  50335. duk__expr_toplain(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/); /* Expression, ']' terminates */
  50336. duk__advance_expect(comp_ctx, DUK_TOK_RBRACKET);
  50337. res->t = DUK_IVAL_PROP;
  50338. duk__copy_ispec(comp_ctx, &res->x1, &res->x2); /* res.x1 -> res.x2 */
  50339. duk__copy_ispec(comp_ctx, &left->x1, &res->x1); /* left.x1 -> res.x1 */
  50340. return;
  50341. }
  50342. case DUK_TOK_LPAREN: {
  50343. /* function call */
  50344. duk_reg_t reg_cs = DUK__ALLOCTEMPS(comp_ctx, 2);
  50345. duk_int_t nargs;
  50346. duk_small_uint_t call_flags = 0;
  50347. /*
  50348. * XXX: attempt to get the call result to "next temp" whenever
  50349. * possible to avoid unnecessary register shuffles.
  50350. *
  50351. * XXX: CSPROP (and CSREG) can overwrite the call target register, and save one temp,
  50352. * if the call target is a temporary register and at the top of the temp reg "stack".
  50353. */
  50354. /*
  50355. * Setup call: target and 'this' binding. Three cases:
  50356. *
  50357. * 1. Identifier base (e.g. "foo()")
  50358. * 2. Property base (e.g. "foo.bar()")
  50359. * 3. Register base (e.g. "foo()()"; i.e. when a return value is a function)
  50360. */
  50361. if (left->t == DUK_IVAL_VAR) {
  50362. duk_hstring *h_varname;
  50363. duk_reg_t reg_varbind;
  50364. duk_regconst_t rc_varname;
  50365. DUK_DDD(DUK_DDDPRINT("function call with identifier base"));
  50366. h_varname = duk_get_hstring(ctx, left->x1.valstack_idx);
  50367. DUK_ASSERT(h_varname != NULL);
  50368. if (h_varname == DUK_HTHREAD_STRING_EVAL(thr)) {
  50369. /* Potential direct eval call detected, flag the CALL
  50370. * so that a run-time "direct eval" check is made and
  50371. * special behavior may be triggered. Note that this
  50372. * does not prevent 'eval' from being register bound.
  50373. */
  50374. DUK_DDD(DUK_DDDPRINT("function call with identifier 'eval' "
  50375. "-> enabling EVALCALL flag, marking function "
  50376. "as may_direct_eval"));
  50377. call_flags |= DUK_BC_CALL_FLAG_EVALCALL;
  50378. comp_ctx->curr_func.may_direct_eval = 1;
  50379. }
  50380. duk_dup(ctx, left->x1.valstack_idx);
  50381. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  50382. duk__emit_a_b(comp_ctx,
  50383. DUK_OP_CSREG,
  50384. (duk_regconst_t) (reg_cs + 0),
  50385. (duk_regconst_t) reg_varbind);
  50386. } else {
  50387. duk__emit_a_b(comp_ctx,
  50388. DUK_OP_CSVAR,
  50389. (duk_regconst_t) (reg_cs + 0),
  50390. rc_varname);
  50391. }
  50392. } else if (left->t == DUK_IVAL_PROP) {
  50393. DUK_DDD(DUK_DDDPRINT("function call with property base"));
  50394. duk__ispec_toforcedreg(comp_ctx, &left->x1, reg_cs + 0); /* base */
  50395. duk__ispec_toforcedreg(comp_ctx, &left->x2, reg_cs + 1); /* key */
  50396. duk__emit_a_b_c(comp_ctx,
  50397. DUK_OP_CSPROP,
  50398. (duk_regconst_t) (reg_cs + 0),
  50399. (duk_regconst_t) (reg_cs + 0),
  50400. (duk_regconst_t) (reg_cs + 1)); /* in-place setup */
  50401. } else {
  50402. DUK_DDD(DUK_DDDPRINT("function call with register base"));
  50403. duk__ivalue_toforcedreg(comp_ctx, left, reg_cs + 0);
  50404. duk__emit_a_b(comp_ctx,
  50405. DUK_OP_CSREG,
  50406. (duk_regconst_t) (reg_cs + 0),
  50407. (duk_regconst_t) (reg_cs + 0)); /* in-place setup */
  50408. }
  50409. DUK__SETTEMP(comp_ctx, reg_cs + 2);
  50410. nargs = duk__parse_arguments(comp_ctx, res); /* parse args starting from "next temp" */
  50411. /* Tailcalls are handled by back-patching the TAILCALL flag to the
  50412. * already emitted instruction later (in return statement parser).
  50413. * Since A and C have a special meaning here, they cannot be "shuffled".
  50414. */
  50415. duk__emit_a_b_c(comp_ctx,
  50416. DUK_OP_CALL | DUK__EMIT_FLAG_NO_SHUFFLE_A | DUK__EMIT_FLAG_NO_SHUFFLE_C,
  50417. (duk_regconst_t) call_flags /*flags*/,
  50418. (duk_regconst_t) reg_cs /*basereg*/,
  50419. (duk_regconst_t) nargs /*numargs*/);
  50420. DUK__SETTEMP(comp_ctx, reg_cs + 1); /* result in csreg */
  50421. res->t = DUK_IVAL_PLAIN;
  50422. res->x1.t = DUK_ISPEC_REGCONST;
  50423. res->x1.regconst = (duk_regconst_t) reg_cs;
  50424. return;
  50425. }
  50426. /* POSTFIX EXPRESSION */
  50427. case DUK_TOK_INCREMENT: {
  50428. args = (DUK_OP_POSTINCR << 8) + 0;
  50429. goto postincdec;
  50430. }
  50431. case DUK_TOK_DECREMENT: {
  50432. args = (DUK_OP_POSTDECR << 8) + 0;
  50433. goto postincdec;
  50434. }
  50435. /* MULTIPLICATIVE EXPRESSION */
  50436. case DUK_TOK_MUL: {
  50437. args = (DUK_OP_MUL << 8) + DUK__BP_MULTIPLICATIVE; /* UnaryExpression */
  50438. goto binary;
  50439. }
  50440. case DUK_TOK_DIV: {
  50441. args = (DUK_OP_DIV << 8) + DUK__BP_MULTIPLICATIVE; /* UnaryExpression */
  50442. goto binary;
  50443. }
  50444. case DUK_TOK_MOD: {
  50445. args = (DUK_OP_MOD << 8) + DUK__BP_MULTIPLICATIVE; /* UnaryExpression */
  50446. goto binary;
  50447. }
  50448. /* ADDITIVE EXPRESSION */
  50449. case DUK_TOK_ADD: {
  50450. args = (DUK_OP_ADD << 8) + DUK__BP_ADDITIVE; /* MultiplicativeExpression */
  50451. goto binary;
  50452. }
  50453. case DUK_TOK_SUB: {
  50454. args = (DUK_OP_SUB << 8) + DUK__BP_ADDITIVE; /* MultiplicativeExpression */
  50455. goto binary;
  50456. }
  50457. /* SHIFT EXPRESSION */
  50458. case DUK_TOK_ALSHIFT: {
  50459. /* << */
  50460. args = (DUK_OP_BASL << 8) + DUK__BP_SHIFT;
  50461. goto binary;
  50462. }
  50463. case DUK_TOK_ARSHIFT: {
  50464. /* >> */
  50465. args = (DUK_OP_BASR << 8) + DUK__BP_SHIFT;
  50466. goto binary;
  50467. }
  50468. case DUK_TOK_RSHIFT: {
  50469. /* >>> */
  50470. args = (DUK_OP_BLSR << 8) + DUK__BP_SHIFT;
  50471. goto binary;
  50472. }
  50473. /* RELATIONAL EXPRESSION */
  50474. case DUK_TOK_LT: {
  50475. /* < */
  50476. args = (DUK_OP_LT << 8) + DUK__BP_RELATIONAL;
  50477. goto binary;
  50478. }
  50479. case DUK_TOK_GT: {
  50480. args = (DUK_OP_GT << 8) + DUK__BP_RELATIONAL;
  50481. goto binary;
  50482. }
  50483. case DUK_TOK_LE: {
  50484. args = (DUK_OP_LE << 8) + DUK__BP_RELATIONAL;
  50485. goto binary;
  50486. }
  50487. case DUK_TOK_GE: {
  50488. args = (DUK_OP_GE << 8) + DUK__BP_RELATIONAL;
  50489. goto binary;
  50490. }
  50491. case DUK_TOK_INSTANCEOF: {
  50492. args = (1 << 16 /*is_extra*/) + (DUK_EXTRAOP_INSTOF << 8) + DUK__BP_RELATIONAL;
  50493. goto binary;
  50494. }
  50495. case DUK_TOK_IN: {
  50496. args = (1 << 16 /*is_extra*/) + (DUK_EXTRAOP_IN << 8) + DUK__BP_RELATIONAL;
  50497. goto binary;
  50498. }
  50499. /* EQUALITY EXPRESSION */
  50500. case DUK_TOK_EQ: {
  50501. args = (DUK_OP_EQ << 8) + DUK__BP_EQUALITY;
  50502. goto binary;
  50503. }
  50504. case DUK_TOK_NEQ: {
  50505. args = (DUK_OP_NEQ << 8) + DUK__BP_EQUALITY;
  50506. goto binary;
  50507. }
  50508. case DUK_TOK_SEQ: {
  50509. args = (DUK_OP_SEQ << 8) + DUK__BP_EQUALITY;
  50510. goto binary;
  50511. }
  50512. case DUK_TOK_SNEQ: {
  50513. args = (DUK_OP_SNEQ << 8) + DUK__BP_EQUALITY;
  50514. goto binary;
  50515. }
  50516. /* BITWISE EXPRESSIONS */
  50517. case DUK_TOK_BAND: {
  50518. args = (DUK_OP_BAND << 8) + DUK__BP_BAND;
  50519. goto binary;
  50520. }
  50521. case DUK_TOK_BXOR: {
  50522. args = (DUK_OP_BXOR << 8) + DUK__BP_BXOR;
  50523. goto binary;
  50524. }
  50525. case DUK_TOK_BOR: {
  50526. args = (DUK_OP_BOR << 8) + DUK__BP_BOR;
  50527. goto binary;
  50528. }
  50529. /* LOGICAL EXPRESSIONS */
  50530. case DUK_TOK_LAND: {
  50531. /* syntactically left-associative but parsed as right-associative */
  50532. args = (1 << 8) + DUK__BP_LAND - 1;
  50533. goto binary_logical;
  50534. }
  50535. case DUK_TOK_LOR: {
  50536. /* syntactically left-associative but parsed as right-associative */
  50537. args = (0 << 8) + DUK__BP_LOR - 1;
  50538. goto binary_logical;
  50539. }
  50540. /* CONDITIONAL EXPRESSION */
  50541. case DUK_TOK_QUESTION: {
  50542. /* XXX: common reg allocation need is to reuse a sub-expression's temp reg,
  50543. * but only if it really is a temp. Nothing fancy here now.
  50544. */
  50545. duk_reg_t reg_temp;
  50546. duk_int_t pc_jump1;
  50547. duk_int_t pc_jump2;
  50548. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  50549. duk__ivalue_toforcedreg(comp_ctx, left, reg_temp);
  50550. duk__emit_if_true_skip(comp_ctx, reg_temp);
  50551. pc_jump1 = duk__emit_jump_empty(comp_ctx); /* jump to false */
  50552. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_COMMA /*rbp_flags*/, reg_temp /*forced_reg*/); /* AssignmentExpression */
  50553. duk__advance_expect(comp_ctx, DUK_TOK_COLON);
  50554. pc_jump2 = duk__emit_jump_empty(comp_ctx); /* jump to end */
  50555. duk__patch_jump_here(comp_ctx, pc_jump1);
  50556. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_COMMA /*rbp_flags*/, reg_temp /*forced_reg*/); /* AssignmentExpression */
  50557. duk__patch_jump_here(comp_ctx, pc_jump2);
  50558. DUK__SETTEMP(comp_ctx, reg_temp + 1);
  50559. res->t = DUK_IVAL_PLAIN;
  50560. res->x1.t = DUK_ISPEC_REGCONST;
  50561. res->x1.regconst = (duk_regconst_t) reg_temp;
  50562. return;
  50563. }
  50564. /* ASSIGNMENT EXPRESSION */
  50565. case DUK_TOK_EQUALSIGN: {
  50566. /*
  50567. * Assignments are right associative, allows e.g.
  50568. * a = 5;
  50569. * a += b = 9; // same as a += (b = 9)
  50570. * -> expression value 14, a = 14, b = 9
  50571. *
  50572. * Right associativiness is reflected in the BP for recursion,
  50573. * "-1" ensures assignment operations are allowed.
  50574. *
  50575. * XXX: just use DUK__BP_COMMA (i.e. no need for 2-step bp levels)?
  50576. */
  50577. args = (DUK_OP_NONE << 8) + DUK__BP_ASSIGNMENT - 1; /* DUK_OP_NONE marks a 'plain' assignment */
  50578. goto assign;
  50579. }
  50580. case DUK_TOK_ADD_EQ: {
  50581. /* right associative */
  50582. args = (DUK_OP_ADD << 8) + DUK__BP_ASSIGNMENT - 1;
  50583. goto assign;
  50584. }
  50585. case DUK_TOK_SUB_EQ: {
  50586. /* right associative */
  50587. args = (DUK_OP_SUB << 8) + DUK__BP_ASSIGNMENT - 1;
  50588. goto assign;
  50589. }
  50590. case DUK_TOK_MUL_EQ: {
  50591. /* right associative */
  50592. args = (DUK_OP_MUL << 8) + DUK__BP_ASSIGNMENT - 1;
  50593. goto assign;
  50594. }
  50595. case DUK_TOK_DIV_EQ: {
  50596. /* right associative */
  50597. args = (DUK_OP_DIV << 8) + DUK__BP_ASSIGNMENT - 1;
  50598. goto assign;
  50599. }
  50600. case DUK_TOK_MOD_EQ: {
  50601. /* right associative */
  50602. args = (DUK_OP_MOD << 8) + DUK__BP_ASSIGNMENT - 1;
  50603. goto assign;
  50604. }
  50605. case DUK_TOK_ALSHIFT_EQ: {
  50606. /* right associative */
  50607. args = (DUK_OP_BASL << 8) + DUK__BP_ASSIGNMENT - 1;
  50608. goto assign;
  50609. }
  50610. case DUK_TOK_ARSHIFT_EQ: {
  50611. /* right associative */
  50612. args = (DUK_OP_BASR << 8) + DUK__BP_ASSIGNMENT - 1;
  50613. goto assign;
  50614. }
  50615. case DUK_TOK_RSHIFT_EQ: {
  50616. /* right associative */
  50617. args = (DUK_OP_BLSR << 8) + DUK__BP_ASSIGNMENT - 1;
  50618. goto assign;
  50619. }
  50620. case DUK_TOK_BAND_EQ: {
  50621. /* right associative */
  50622. args = (DUK_OP_BAND << 8) + DUK__BP_ASSIGNMENT - 1;
  50623. goto assign;
  50624. }
  50625. case DUK_TOK_BOR_EQ: {
  50626. /* right associative */
  50627. args = (DUK_OP_BOR << 8) + DUK__BP_ASSIGNMENT - 1;
  50628. goto assign;
  50629. }
  50630. case DUK_TOK_BXOR_EQ: {
  50631. /* right associative */
  50632. args = (DUK_OP_BXOR << 8) + DUK__BP_ASSIGNMENT - 1;
  50633. goto assign;
  50634. }
  50635. /* COMMA */
  50636. case DUK_TOK_COMMA: {
  50637. /* right associative */
  50638. duk__ivalue_toplain_ignore(comp_ctx, left); /* need side effects, not value */
  50639. duk__expr_toplain(comp_ctx, res, DUK__BP_COMMA - 1 /*rbp_flags*/);
  50640. /* return 'res' (of right part) as our result */
  50641. return;
  50642. }
  50643. default: {
  50644. break;
  50645. }
  50646. }
  50647. DUK_D(DUK_DPRINT("parse error: unexpected token: %ld", (long) tok));
  50648. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_PARSE_ERROR);
  50649. return;
  50650. #if 0
  50651. /* XXX: shared handling for 'duk__expr_lhs'? */
  50652. if (comp_ctx->curr_func.paren_level == 0 && XXX) {
  50653. comp_ctx->curr_func.duk__expr_lhs = 0;
  50654. }
  50655. #endif
  50656. binary:
  50657. /*
  50658. * Shared handling of binary operations
  50659. *
  50660. * args = (is_extraop << 16) + (opcode << 8) + rbp
  50661. */
  50662. {
  50663. duk__ivalue_toplain(comp_ctx, left);
  50664. duk__expr_toplain(comp_ctx, res, args & 0xff /*rbp_flags*/);
  50665. /* combine left->x1 and res->x1 (right->x1, really) -> (left->x1 OP res->x1) */
  50666. DUK_ASSERT(left->t == DUK_IVAL_PLAIN);
  50667. DUK_ASSERT(res->t == DUK_IVAL_PLAIN);
  50668. res->t = (args >> 16) ? DUK_IVAL_ARITH_EXTRAOP : DUK_IVAL_ARITH;
  50669. res->op = (args >> 8) & 0xff;
  50670. res->x2.t = res->x1.t;
  50671. res->x2.regconst = res->x1.regconst;
  50672. duk_copy(ctx, res->x1.valstack_idx, res->x2.valstack_idx);
  50673. res->x1.t = left->x1.t;
  50674. res->x1.regconst = left->x1.regconst;
  50675. duk_copy(ctx, left->x1.valstack_idx, res->x1.valstack_idx);
  50676. DUK_DDD(DUK_DDDPRINT("binary op, res: t=%ld, x1.t=%ld, x1.regconst=0x%08lx, x2.t=%ld, x2.regconst=0x%08lx",
  50677. (long) res->t, (long) res->x1.t, (unsigned long) res->x1.regconst, (long) res->x2.t, (unsigned long) res->x2.regconst));
  50678. return;
  50679. }
  50680. binary_logical:
  50681. /*
  50682. * Shared handling for logical AND and logical OR.
  50683. *
  50684. * args = (truthval << 8) + rbp
  50685. *
  50686. * Truthval determines when to skip right-hand-side.
  50687. * For logical AND truthval=1, for logical OR truthval=0.
  50688. *
  50689. * See doc/compiler.txt for discussion on compiling logical
  50690. * AND and OR expressions. The approach here is very simplistic,
  50691. * generating extra jumps and multiple evaluations of truth values,
  50692. * but generates code on-the-fly with only local back-patching.
  50693. *
  50694. * Both logical AND and OR are syntactically left-associated.
  50695. * However, logical ANDs are compiled as right associative
  50696. * expressions, i.e. "A && B && C" as "A && (B && C)", to allow
  50697. * skip jumps to skip over the entire tail. Similarly for logical OR.
  50698. */
  50699. {
  50700. duk_reg_t reg_temp;
  50701. duk_int_t pc_jump;
  50702. duk_small_uint_t args_truthval = args >> 8;
  50703. duk_small_uint_t args_rbp = args & 0xff;
  50704. /* XXX: unoptimal use of temps, resetting */
  50705. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  50706. duk__ivalue_toforcedreg(comp_ctx, left, reg_temp);
  50707. duk__emit_a_b(comp_ctx,
  50708. DUK_OP_IF | DUK__EMIT_FLAG_NO_SHUFFLE_A,
  50709. (duk_regconst_t) args_truthval,
  50710. (duk_regconst_t) reg_temp); /* skip jump conditionally */
  50711. pc_jump = duk__emit_jump_empty(comp_ctx);
  50712. duk__expr_toforcedreg(comp_ctx, res, args_rbp /*rbp_flags*/, reg_temp /*forced_reg*/);
  50713. duk__patch_jump_here(comp_ctx, pc_jump);
  50714. res->t = DUK_IVAL_PLAIN;
  50715. res->x1.t = DUK_ISPEC_REGCONST;
  50716. res->x1.regconst = (duk_regconst_t) reg_temp;
  50717. return;
  50718. }
  50719. assign:
  50720. /*
  50721. * Shared assignment expression handling
  50722. *
  50723. * args = (opcode << 8) + rbp
  50724. *
  50725. * If 'opcode' is DUK_OP_NONE, plain assignment without arithmetic.
  50726. * Syntactically valid left-hand-side forms which are not accepted as
  50727. * left-hand-side values (e.g. as in "f() = 1") must NOT cause a
  50728. * SyntaxError, but rather a run-time ReferenceError.
  50729. */
  50730. {
  50731. duk_small_uint_t args_op = args >> 8;
  50732. duk_small_uint_t args_rbp = args & 0xff;
  50733. /* XXX: here we need to know if 'left' is left-hand-side compatible.
  50734. * That information is no longer available from current expr parsing
  50735. * state; it would need to be carried into the 'left' ivalue or by
  50736. * some other means.
  50737. */
  50738. if (left->t == DUK_IVAL_VAR) {
  50739. duk_hstring *h_varname;
  50740. duk_reg_t reg_varbind;
  50741. duk_regconst_t rc_varname;
  50742. duk_regconst_t rc_res;
  50743. duk_reg_t reg_temp;
  50744. /* already in fluly evaluated form */
  50745. DUK_ASSERT(left->x1.t == DUK_ISPEC_VALUE);
  50746. duk__expr_toreg(comp_ctx, res, args_rbp /*rbp_flags*/);
  50747. DUK_ASSERT(res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_REGCONST);
  50748. h_varname = duk_get_hstring(ctx, left->x1.valstack_idx);
  50749. DUK_ASSERT(h_varname != NULL);
  50750. /* E5 Section 11.13.1 (and others for other assignments), step 4 */
  50751. if (duk__hstring_is_eval_or_arguments_in_strict_mode(comp_ctx, h_varname)) {
  50752. goto syntax_error_lvalue;
  50753. }
  50754. duk_dup(ctx, left->x1.valstack_idx);
  50755. (void) duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname);
  50756. DUK_DDD(DUK_DDDPRINT("assign to '%!O' -> reg_varbind=%ld, rc_varname=%ld",
  50757. (duk_heaphdr *) h_varname, (long) reg_varbind, (long) rc_varname));
  50758. if (args_op == DUK_OP_NONE) {
  50759. rc_res = res->x1.regconst;
  50760. } else {
  50761. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  50762. if (reg_varbind >= 0) {
  50763. duk__emit_a_b_c(comp_ctx,
  50764. args_op,
  50765. (duk_regconst_t) reg_temp,
  50766. (duk_regconst_t) reg_varbind,
  50767. res->x1.regconst);
  50768. } else {
  50769. duk__emit_a_bc(comp_ctx,
  50770. DUK_OP_GETVAR,
  50771. (duk_regconst_t) reg_temp,
  50772. rc_varname);
  50773. duk__emit_a_b_c(comp_ctx,
  50774. args_op,
  50775. (duk_regconst_t) reg_temp,
  50776. (duk_regconst_t) reg_temp,
  50777. res->x1.regconst);
  50778. }
  50779. rc_res = (duk_regconst_t) reg_temp;
  50780. }
  50781. if (reg_varbind >= 0) {
  50782. duk__emit_a_bc(comp_ctx,
  50783. DUK_OP_LDREG,
  50784. (duk_regconst_t) reg_varbind,
  50785. rc_res);
  50786. } else {
  50787. /* Only a reg fits into 'A' and reg_res may be a const in
  50788. * straight assignment.
  50789. *
  50790. * XXX: here the current A/B/C split is suboptimal: we could
  50791. * just use 9 bits for reg_res (and support constants) and 17
  50792. * instead of 18 bits for the varname const index.
  50793. */
  50794. if (DUK__ISCONST(comp_ctx, rc_res)) {
  50795. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  50796. duk__emit_a_bc(comp_ctx,
  50797. DUK_OP_LDCONST,
  50798. (duk_regconst_t) reg_temp,
  50799. rc_res);
  50800. rc_res = (duk_regconst_t) reg_temp;
  50801. }
  50802. duk__emit_a_bc(comp_ctx,
  50803. DUK_OP_PUTVAR | DUK__EMIT_FLAG_A_IS_SOURCE,
  50804. rc_res,
  50805. rc_varname);
  50806. }
  50807. res->t = DUK_IVAL_PLAIN;
  50808. res->x1.t = DUK_ISPEC_REGCONST;
  50809. res->x1.regconst = rc_res;
  50810. } else if (left->t == DUK_IVAL_PROP) {
  50811. /* E5 Section 11.13.1 (and others) step 4 never matches for prop writes -> no check */
  50812. duk_reg_t reg_obj;
  50813. duk_regconst_t rc_key;
  50814. duk_regconst_t rc_res;
  50815. duk_reg_t reg_temp;
  50816. /* Property access expressions ('a[b]') are critical to correct
  50817. * LHS evaluation ordering, see test-dev-assign-eval-order*.js.
  50818. * We must make sure that the LHS target slot (base object and
  50819. * key) don't change during RHS evaluation. The only concrete
  50820. * problem is a register reference to a variable-bound register
  50821. * (i.e., non-temp). Require temp regs for both key and base.
  50822. *
  50823. * Don't allow a constant for the object (even for a number
  50824. * etc), as it goes into the 'A' field of the opcode.
  50825. */
  50826. reg_obj = duk__ispec_toregconst_raw(comp_ctx,
  50827. &left->x1,
  50828. -1 /*forced_reg*/,
  50829. DUK__IVAL_FLAG_REQUIRE_TEMP /*flags*/);
  50830. rc_key = duk__ispec_toregconst_raw(comp_ctx,
  50831. &left->x2,
  50832. -1 /*forced_reg*/,
  50833. DUK__IVAL_FLAG_REQUIRE_TEMP | DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  50834. /* Evaluate RHS only when LHS is safe. */
  50835. duk__expr_toregconst(comp_ctx, res, args_rbp /*rbp_flags*/);
  50836. DUK_ASSERT(res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_REGCONST);
  50837. if (args_op == DUK_OP_NONE) {
  50838. rc_res = res->x1.regconst;
  50839. } else {
  50840. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  50841. duk__emit_a_b_c(comp_ctx,
  50842. DUK_OP_GETPROP,
  50843. (duk_regconst_t) reg_temp,
  50844. (duk_regconst_t) reg_obj,
  50845. rc_key);
  50846. duk__emit_a_b_c(comp_ctx,
  50847. args_op,
  50848. (duk_regconst_t) reg_temp,
  50849. (duk_regconst_t) reg_temp,
  50850. res->x1.regconst);
  50851. rc_res = (duk_regconst_t) reg_temp;
  50852. }
  50853. duk__emit_a_b_c(comp_ctx,
  50854. DUK_OP_PUTPROP | DUK__EMIT_FLAG_A_IS_SOURCE,
  50855. (duk_regconst_t) reg_obj,
  50856. rc_key,
  50857. rc_res);
  50858. res->t = DUK_IVAL_PLAIN;
  50859. res->x1.t = DUK_ISPEC_REGCONST;
  50860. res->x1.regconst = rc_res;
  50861. } else {
  50862. /* No support for lvalues returned from new or function call expressions.
  50863. * However, these must NOT cause compile-time SyntaxErrors, but run-time
  50864. * ReferenceErrors. Both left and right sides of the assignment must be
  50865. * evaluated before throwing a ReferenceError. For instance:
  50866. *
  50867. * f() = g();
  50868. *
  50869. * must result in f() being evaluated, then g() being evaluated, and
  50870. * finally, a ReferenceError being thrown. See E5 Section 11.13.1.
  50871. */
  50872. duk_regconst_t rc_res;
  50873. /* first evaluate LHS fully to ensure all side effects are out */
  50874. duk__ivalue_toplain_ignore(comp_ctx, left);
  50875. /* then evaluate RHS fully (its value becomes the expression value too) */
  50876. rc_res = duk__expr_toregconst(comp_ctx, res, args_rbp /*rbp_flags*/);
  50877. duk__emit_extraop_only(comp_ctx,
  50878. DUK_EXTRAOP_INVLHS);
  50879. /* XXX: this value is irrelevant because of INVLHS? */
  50880. res->t = DUK_IVAL_PLAIN;
  50881. res->x1.t = DUK_ISPEC_REGCONST;
  50882. res->x1.regconst = rc_res;
  50883. }
  50884. return;
  50885. }
  50886. postincdec:
  50887. {
  50888. /*
  50889. * Post-increment/decrement will return the original value as its
  50890. * result value. However, even that value will be coerced using
  50891. * ToNumber() which is quite awkward. Specific bytecode opcodes
  50892. * are used to handle these semantics.
  50893. *
  50894. * Note that post increment/decrement has a "no LineTerminator here"
  50895. * restriction. This is handled by duk__expr_lbp(), which forcibly terminates
  50896. * the previous expression if a LineTerminator occurs before '++'/'--'.
  50897. */
  50898. duk_reg_t reg_res;
  50899. duk_small_uint_t args_op = args >> 8;
  50900. /* Specific assumptions for opcode numbering. */
  50901. DUK_ASSERT(DUK_OP_POSTINCR + 4 == DUK_OP_POSTINCV);
  50902. DUK_ASSERT(DUK_OP_POSTDECR + 4 == DUK_OP_POSTDECV);
  50903. DUK_ASSERT(DUK_OP_POSTINCR + 8 == DUK_OP_POSTINCP);
  50904. DUK_ASSERT(DUK_OP_POSTDECR + 8 == DUK_OP_POSTDECP);
  50905. reg_res = DUK__ALLOCTEMP(comp_ctx);
  50906. if (left->t == DUK_IVAL_VAR) {
  50907. duk_hstring *h_varname;
  50908. duk_reg_t reg_varbind;
  50909. duk_regconst_t rc_varname;
  50910. h_varname = duk_get_hstring(ctx, left->x1.valstack_idx);
  50911. DUK_ASSERT(h_varname != NULL);
  50912. if (duk__hstring_is_eval_or_arguments_in_strict_mode(comp_ctx, h_varname)) {
  50913. goto syntax_error;
  50914. }
  50915. duk_dup(ctx, left->x1.valstack_idx);
  50916. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  50917. duk__emit_a_bc(comp_ctx,
  50918. args_op, /* e.g. DUK_OP_POSTINCR */
  50919. (duk_regconst_t) reg_res,
  50920. (duk_regconst_t) reg_varbind);
  50921. } else {
  50922. duk__emit_a_bc(comp_ctx,
  50923. args_op + 4, /* e.g. DUK_OP_POSTINCV */
  50924. (duk_regconst_t) reg_res,
  50925. rc_varname);
  50926. }
  50927. DUK_DDD(DUK_DDDPRINT("postincdec to '%!O' -> reg_varbind=%ld, rc_varname=%ld",
  50928. (duk_heaphdr *) h_varname, (long) reg_varbind, (long) rc_varname));
  50929. } else if (left->t == DUK_IVAL_PROP) {
  50930. duk_reg_t reg_obj; /* allocate to reg only (not const) */
  50931. duk_regconst_t rc_key;
  50932. reg_obj = duk__ispec_toregconst_raw(comp_ctx, &left->x1, -1 /*forced_reg*/, 0 /*flags*/); /* don't allow const */
  50933. rc_key = duk__ispec_toregconst_raw(comp_ctx, &left->x2, -1 /*forced_reg*/, DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  50934. duk__emit_a_b_c(comp_ctx,
  50935. args_op + 8, /* e.g. DUK_OP_POSTINCP */
  50936. (duk_regconst_t) reg_res,
  50937. (duk_regconst_t) reg_obj,
  50938. rc_key);
  50939. } else {
  50940. /* Technically return value is not needed because INVLHS will
  50941. * unconditially throw a ReferenceError. Coercion is necessary
  50942. * for proper semantics (consider ToNumber() called for an object).
  50943. * Use DUK_EXTRAOP_UNP with a dummy register to get ToNumber().
  50944. */
  50945. duk__ivalue_toforcedreg(comp_ctx, left, reg_res);
  50946. duk__emit_extraop_bc(comp_ctx,
  50947. DUK_EXTRAOP_UNP,
  50948. reg_res); /* for side effects, result ignored */
  50949. duk__emit_extraop_only(comp_ctx,
  50950. DUK_EXTRAOP_INVLHS);
  50951. }
  50952. res->t = DUK_IVAL_PLAIN;
  50953. res->x1.t = DUK_ISPEC_REGCONST;
  50954. res->x1.regconst = (duk_regconst_t) reg_res;
  50955. DUK__SETTEMP(comp_ctx, reg_res + 1);
  50956. return;
  50957. }
  50958. syntax_error:
  50959. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_EXPRESSION);
  50960. return;
  50961. syntax_error_lvalue:
  50962. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_LVALUE);
  50963. return;
  50964. }
  50965. DUK_LOCAL duk_small_uint_t duk__expr_lbp(duk_compiler_ctx *comp_ctx) {
  50966. duk_small_int_t tok = comp_ctx->curr_token.t;
  50967. DUK_ASSERT(tok >= DUK_TOK_MINVAL && tok <= DUK_TOK_MAXVAL);
  50968. DUK_ASSERT(sizeof(duk__token_lbp) == DUK_TOK_MAXVAL + 1);
  50969. /* XXX: integrate support for this into led() instead?
  50970. * Similar issue as post-increment/post-decrement.
  50971. */
  50972. /* prevent duk__expr_led() by using a binding power less than anything valid */
  50973. if (tok == DUK_TOK_IN && !comp_ctx->curr_func.allow_in) {
  50974. return 0;
  50975. }
  50976. if ((tok == DUK_TOK_DECREMENT || tok == DUK_TOK_INCREMENT) &&
  50977. (comp_ctx->curr_token.lineterm)) {
  50978. /* '++' or '--' in a post-increment/decrement position,
  50979. * and a LineTerminator occurs between the operator and
  50980. * the preceding expression. Force the previous expr
  50981. * to terminate, in effect treating e.g. "a,b\n++" as
  50982. * "a,b;++" (= SyntaxError).
  50983. */
  50984. return 0;
  50985. }
  50986. return DUK__TOKEN_LBP_GET_BP(duk__token_lbp[tok]); /* format is bit packed */
  50987. }
  50988. /*
  50989. * Expression parsing.
  50990. *
  50991. * Upon entry to 'expr' and its variants, 'curr_tok' is assumed to be the
  50992. * first token of the expression. Upon exit, 'curr_tok' will be the first
  50993. * token not part of the expression (e.g. semicolon terminating an expression
  50994. * statement).
  50995. */
  50996. #define DUK__EXPR_RBP_MASK 0xff
  50997. #define DUK__EXPR_FLAG_REJECT_IN (1 << 8)
  50998. #define DUK__EXPR_FLAG_ALLOW_EMPTY (1 << 9)
  50999. /* main expression parser function */
  51000. DUK_LOCAL void duk__expr(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  51001. duk_hthread *thr = comp_ctx->thr;
  51002. duk_context *ctx = (duk_context *) thr;
  51003. duk_ivalue tmp_alloc; /* 'res' is used for "left", and 'tmp' for "right" */
  51004. duk_ivalue *tmp = &tmp_alloc;
  51005. duk_small_uint_t rbp;
  51006. DUK__RECURSION_INCREASE(comp_ctx, thr);
  51007. duk_require_stack(ctx, DUK__PARSE_EXPR_SLOTS);
  51008. /* filter out flags from exprtop rbp_flags here to save space */
  51009. rbp = rbp_flags & DUK__EXPR_RBP_MASK;
  51010. DUK_DDD(DUK_DDDPRINT("duk__expr(), rbp_flags=%ld, rbp=%ld, allow_in=%ld, paren_level=%ld",
  51011. (long) rbp_flags, (long) rbp, (long) comp_ctx->curr_func.allow_in,
  51012. (long) comp_ctx->curr_func.paren_level));
  51013. DUK_MEMZERO(&tmp_alloc, sizeof(tmp_alloc));
  51014. tmp->x1.valstack_idx = duk_get_top(ctx);
  51015. tmp->x2.valstack_idx = tmp->x1.valstack_idx + 1;
  51016. duk_push_undefined(ctx);
  51017. duk_push_undefined(ctx);
  51018. /* XXX: where to release temp regs in intermediate expressions?
  51019. * e.g. 1+2+3 -> don't inflate temp register count when parsing this.
  51020. * that particular expression temp regs can be forced here.
  51021. */
  51022. /* XXX: increase ctx->expr_tokens here for every consumed token
  51023. * (this would be a nice statistic)?
  51024. */
  51025. if (comp_ctx->curr_token.t == DUK_TOK_SEMICOLON || comp_ctx->curr_token.t == DUK_TOK_RPAREN) {
  51026. /* XXX: possibly incorrect handling of empty expression */
  51027. DUK_DDD(DUK_DDDPRINT("empty expression"));
  51028. if (!(rbp_flags & DUK__EXPR_FLAG_ALLOW_EMPTY)) {
  51029. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_EMPTY_EXPR_NOT_ALLOWED);
  51030. }
  51031. res->t = DUK_IVAL_PLAIN;
  51032. res->x1.t = DUK_ISPEC_VALUE;
  51033. duk_push_undefined(ctx);
  51034. duk_replace(ctx, res->x1.valstack_idx);
  51035. goto cleanup;
  51036. }
  51037. duk__advance(comp_ctx);
  51038. duk__expr_nud(comp_ctx, res); /* reuse 'res' as 'left' */
  51039. while (rbp < duk__expr_lbp(comp_ctx)) {
  51040. duk__advance(comp_ctx);
  51041. duk__expr_led(comp_ctx, res, tmp);
  51042. duk__copy_ivalue(comp_ctx, tmp, res); /* tmp -> res */
  51043. }
  51044. cleanup:
  51045. /* final result is already in 'res' */
  51046. duk_pop_2(ctx);
  51047. DUK__RECURSION_DECREASE(comp_ctx, thr);
  51048. }
  51049. DUK_LOCAL void duk__exprtop(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  51050. duk_hthread *thr = comp_ctx->thr;
  51051. /* Note: these variables must reside in 'curr_func' instead of the global
  51052. * context: when parsing function expressions, expression parsing is nested.
  51053. */
  51054. comp_ctx->curr_func.nud_count = 0;
  51055. comp_ctx->curr_func.led_count = 0;
  51056. comp_ctx->curr_func.paren_level = 0;
  51057. comp_ctx->curr_func.expr_lhs = 1;
  51058. comp_ctx->curr_func.allow_in = (rbp_flags & DUK__EXPR_FLAG_REJECT_IN ? 0 : 1);
  51059. duk__expr(comp_ctx, res, rbp_flags);
  51060. if (!(rbp_flags & DUK__EXPR_FLAG_ALLOW_EMPTY) && duk__expr_is_empty(comp_ctx)) {
  51061. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_EMPTY_EXPR_NOT_ALLOWED);
  51062. }
  51063. }
  51064. /* A bunch of helpers (for size optimization) that combine duk__expr()/duk__exprtop()
  51065. * and result conversions.
  51066. *
  51067. * Each helper needs at least 2-3 calls to make it worth while to wrap.
  51068. */
  51069. DUK_LOCAL duk_reg_t duk__expr_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  51070. duk__expr(comp_ctx, res, rbp_flags);
  51071. return duk__ivalue_toreg(comp_ctx, res);
  51072. }
  51073. #if 0 /* unused */
  51074. DUK_LOCAL duk_reg_t duk__expr_totempreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  51075. duk__expr(comp_ctx, res, rbp_flags);
  51076. return duk__ivalue_totempreg(comp_ctx, res);
  51077. }
  51078. #endif
  51079. 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) {
  51080. DUK_ASSERT(forced_reg >= 0);
  51081. duk__expr(comp_ctx, res, rbp_flags);
  51082. duk__ivalue_toforcedreg(comp_ctx, res, forced_reg);
  51083. }
  51084. DUK_LOCAL duk_regconst_t duk__expr_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  51085. duk__expr(comp_ctx, res, rbp_flags);
  51086. return duk__ivalue_toregconst(comp_ctx, res);
  51087. }
  51088. DUK_LOCAL void duk__expr_toplain(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  51089. duk__expr(comp_ctx, res, rbp_flags);
  51090. duk__ivalue_toplain(comp_ctx, res);
  51091. }
  51092. DUK_LOCAL void duk__expr_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  51093. duk__expr(comp_ctx, res, rbp_flags);
  51094. duk__ivalue_toplain_ignore(comp_ctx, res);
  51095. }
  51096. DUK_LOCAL duk_reg_t duk__exprtop_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  51097. duk__exprtop(comp_ctx, res, rbp_flags);
  51098. return duk__ivalue_toreg(comp_ctx, res);
  51099. }
  51100. #if 0 /* unused */
  51101. DUK_LOCAL duk_reg_t duk__exprtop_totempreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  51102. duk__exprtop(comp_ctx, res, rbp_flags);
  51103. return duk__ivalue_totempreg(comp_ctx, res);
  51104. }
  51105. #endif
  51106. #if 0 /* unused */
  51107. 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) {
  51108. DUK_ASSERT(forced_reg >= 0);
  51109. duk__exprtop(comp_ctx, res, rbp_flags);
  51110. duk__ivalue_toforcedreg(comp_ctx, res, forced_reg);
  51111. }
  51112. #endif
  51113. DUK_LOCAL duk_regconst_t duk__exprtop_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  51114. duk__exprtop(comp_ctx, res, rbp_flags);
  51115. return duk__ivalue_toregconst(comp_ctx, res);
  51116. }
  51117. #if 0 /* unused */
  51118. DUK_LOCAL void duk__exprtop_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *res, int rbp_flags) {
  51119. duk__exprtop(comp_ctx, res, rbp_flags);
  51120. duk__ivalue_toplain_ignore(comp_ctx, res);
  51121. }
  51122. #endif
  51123. /*
  51124. * Parse an individual source element (top level statement) or a statement.
  51125. *
  51126. * Handles labeled statements automatically (peeling away labels before
  51127. * parsing an expression that follows the label(s)).
  51128. *
  51129. * Upon entry, 'curr_tok' contains the first token of the statement (parsed
  51130. * in "allow regexp literal" mode). Upon exit, 'curr_tok' contains the first
  51131. * token following the statement (if the statement has a terminator, this is
  51132. * the token after the terminator).
  51133. */
  51134. #ifdef DUK__HAS_VAL
  51135. #undef DUK__HAS_VAL
  51136. #endif
  51137. #ifdef DUK__HAS_TERM
  51138. #undef DUK__HAS_TERM
  51139. #endif
  51140. #ifdef DUK__ALLOW_AUTO_SEMI_ALWAYS
  51141. #undef DUK__ALLOW_AUTO_SEMI_ALWAYS
  51142. #endif
  51143. #ifdef DUK__STILL_PROLOGUE
  51144. #undef DUK__STILL_PROLOGUE
  51145. #endif
  51146. #ifdef DUK__IS_TERMINAL
  51147. #undef DUK__IS_TERMINAL
  51148. #endif
  51149. #define DUK__HAS_VAL (1 << 0) /* stmt has non-empty value */
  51150. #define DUK__HAS_TERM (1 << 1) /* stmt has explicit/implicit semicolon terminator */
  51151. #define DUK__ALLOW_AUTO_SEMI_ALWAYS (1 << 2) /* allow automatic semicolon even without lineterm (compatibility) */
  51152. #define DUK__STILL_PROLOGUE (1 << 3) /* statement does not terminate directive prologue */
  51153. #define DUK__IS_TERMINAL (1 << 4) /* statement is guaranteed to be terminal (control doesn't flow to next statement) */
  51154. /* Parse a single variable declaration (e.g. "i" or "i=10"). A leading 'var'
  51155. * has already been eaten. These is no return value in 'res', it is used only
  51156. * as a temporary.
  51157. *
  51158. * When called from 'for-in' statement parser, the initializer expression must
  51159. * not allow the 'in' token. The caller supply additional expression parsing
  51160. * flags (like DUK__EXPR_FLAG_REJECT_IN) in 'expr_flags'.
  51161. *
  51162. * Finally, out_rc_varname and out_reg_varbind are updated to reflect where
  51163. * the identifier is bound:
  51164. *
  51165. * If register bound: out_reg_varbind >= 0, out_rc_varname == 0 (ignore)
  51166. * If not register bound: out_reg_varbind < 0, out_rc_varname >= 0
  51167. *
  51168. * These allow the caller to use the variable for further assignment, e.g.
  51169. * as is done in 'for-in' parsing.
  51170. */
  51171. 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) {
  51172. duk_hthread *thr = comp_ctx->thr;
  51173. duk_context *ctx = (duk_context *) thr;
  51174. duk_hstring *h_varname;
  51175. duk_reg_t reg_varbind;
  51176. duk_regconst_t rc_varname;
  51177. /* assume 'var' has been eaten */
  51178. /* Note: Identifier rejects reserved words */
  51179. if (comp_ctx->curr_token.t != DUK_TOK_IDENTIFIER) {
  51180. goto syntax_error;
  51181. }
  51182. h_varname = comp_ctx->curr_token.str1;
  51183. DUK_ASSERT(h_varname != NULL);
  51184. /* strict mode restrictions (E5 Section 12.2.1) */
  51185. if (duk__hstring_is_eval_or_arguments_in_strict_mode(comp_ctx, h_varname)) {
  51186. goto syntax_error;
  51187. }
  51188. /* register declarations in first pass */
  51189. if (comp_ctx->curr_func.in_scanning) {
  51190. duk_uarridx_t n;
  51191. DUK_DDD(DUK_DDDPRINT("register variable declaration %!O in pass 1",
  51192. (duk_heaphdr *) h_varname));
  51193. n = (duk_uarridx_t) duk_get_length(ctx, comp_ctx->curr_func.decls_idx);
  51194. duk_push_hstring(ctx, h_varname);
  51195. duk_put_prop_index(ctx, comp_ctx->curr_func.decls_idx, n);
  51196. duk_push_int(ctx, DUK_DECL_TYPE_VAR + (0 << 8));
  51197. duk_put_prop_index(ctx, comp_ctx->curr_func.decls_idx, n + 1);
  51198. }
  51199. duk_push_hstring(ctx, h_varname); /* push before advancing to keep reachable */
  51200. /* register binding lookup is based on varmap (even in first pass) */
  51201. duk_dup_top(ctx);
  51202. (void) duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname);
  51203. duk__advance(comp_ctx); /* eat identifier */
  51204. if (comp_ctx->curr_token.t == DUK_TOK_EQUALSIGN) {
  51205. duk__advance(comp_ctx);
  51206. DUK_DDD(DUK_DDDPRINT("vardecl, assign to '%!O' -> reg_varbind=%ld, rc_varname=%ld",
  51207. (duk_heaphdr *) h_varname, (long) reg_varbind, (long) rc_varname));
  51208. duk__exprtop(comp_ctx, res, DUK__BP_COMMA | expr_flags /*rbp_flags*/); /* AssignmentExpression */
  51209. if (reg_varbind >= 0) {
  51210. duk__ivalue_toforcedreg(comp_ctx, res, reg_varbind);
  51211. } else {
  51212. duk_reg_t reg_val;
  51213. reg_val = duk__ivalue_toreg(comp_ctx, res);
  51214. duk__emit_a_bc(comp_ctx,
  51215. DUK_OP_PUTVAR | DUK__EMIT_FLAG_A_IS_SOURCE,
  51216. (duk_regconst_t) reg_val,
  51217. rc_varname);
  51218. }
  51219. }
  51220. duk_pop(ctx); /* pop varname */
  51221. *out_rc_varname = rc_varname;
  51222. *out_reg_varbind = reg_varbind;
  51223. return;
  51224. syntax_error:
  51225. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_VAR_DECLARATION);
  51226. }
  51227. DUK_LOCAL void duk__parse_var_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  51228. duk_reg_t reg_varbind;
  51229. duk_regconst_t rc_varname;
  51230. duk__advance(comp_ctx); /* eat 'var' */
  51231. for (;;) {
  51232. /* rc_varname and reg_varbind are ignored here */
  51233. duk__parse_var_decl(comp_ctx, res, 0, &reg_varbind, &rc_varname);
  51234. if (comp_ctx->curr_token.t != DUK_TOK_COMMA) {
  51235. break;
  51236. }
  51237. duk__advance(comp_ctx);
  51238. }
  51239. }
  51240. DUK_LOCAL void duk__parse_for_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site) {
  51241. duk_hthread *thr = comp_ctx->thr;
  51242. duk_context *ctx = (duk_context *) thr;
  51243. duk_int_t pc_v34_lhs; /* start variant 3/4 left-hand-side code (L1 in doc/compiler.txt example) */
  51244. duk_reg_t temp_reset; /* knock back "next temp" to this whenever possible */
  51245. duk_reg_t reg_temps; /* preallocated temporaries (2) for variants 3 and 4 */
  51246. DUK_DDD(DUK_DDDPRINT("start parsing a for/for-in statement"));
  51247. /* Two temporaries are preallocated here for variants 3 and 4 which need
  51248. * registers which are never clobbered by expressions in the loop
  51249. * (concretely: for the enumerator object and the next enumerated value).
  51250. * Variants 1 and 2 "release" these temps.
  51251. */
  51252. reg_temps = DUK__ALLOCTEMPS(comp_ctx, 2);
  51253. temp_reset = DUK__GETTEMP(comp_ctx);
  51254. /*
  51255. * For/for-in main variants are:
  51256. *
  51257. * 1. for (ExpressionNoIn_opt; Expression_opt; Expression_opt) Statement
  51258. * 2. for (var VariableDeclarationNoIn; Expression_opt; Expression_opt) Statement
  51259. * 3. for (LeftHandSideExpression in Expression) Statement
  51260. * 4. for (var VariableDeclarationNoIn in Expression) Statement
  51261. *
  51262. * Parsing these without arbitrary lookahead or backtracking is relatively
  51263. * tricky but we manage to do so for now.
  51264. *
  51265. * See doc/compiler.txt for a detailed discussion of control flow
  51266. * issues, evaluation order issues, etc.
  51267. */
  51268. duk__advance(comp_ctx); /* eat 'for' */
  51269. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  51270. DUK_DDD(DUK_DDDPRINT("detecting for/for-in loop variant, pc=%ld", (long) duk__get_current_pc(comp_ctx)));
  51271. /* a label site has been emitted by duk__parse_stmt() automatically
  51272. * (it will also emit the ENDLABEL).
  51273. */
  51274. if (comp_ctx->curr_token.t == DUK_TOK_VAR) {
  51275. /*
  51276. * Variant 2 or 4
  51277. */
  51278. duk_reg_t reg_varbind; /* variable binding register if register-bound (otherwise < 0) */
  51279. duk_regconst_t rc_varname; /* variable name reg/const, if variable not register-bound */
  51280. duk__advance(comp_ctx); /* eat 'var' */
  51281. duk__parse_var_decl(comp_ctx, res, DUK__EXPR_FLAG_REJECT_IN, &reg_varbind, &rc_varname);
  51282. DUK__SETTEMP(comp_ctx, temp_reset);
  51283. if (comp_ctx->curr_token.t == DUK_TOK_IN) {
  51284. /*
  51285. * Variant 4
  51286. */
  51287. DUK_DDD(DUK_DDDPRINT("detected for variant 4: for (var VariableDeclarationNoIn in Expression) Statement"));
  51288. pc_v34_lhs = duk__get_current_pc(comp_ctx); /* jump is inserted here */
  51289. if (reg_varbind >= 0) {
  51290. duk__emit_a_bc(comp_ctx,
  51291. DUK_OP_LDREG,
  51292. (duk_regconst_t) reg_varbind,
  51293. (duk_regconst_t) (reg_temps + 0));
  51294. } else {
  51295. duk__emit_a_bc(comp_ctx,
  51296. DUK_OP_PUTVAR | DUK__EMIT_FLAG_A_IS_SOURCE,
  51297. (duk_regconst_t) (reg_temps + 0),
  51298. rc_varname);
  51299. }
  51300. goto parse_3_or_4;
  51301. } else {
  51302. /*
  51303. * Variant 2
  51304. */
  51305. DUK_DDD(DUK_DDDPRINT("detected for variant 2: for (var VariableDeclarationNoIn; Expression_opt; Expression_opt) Statement"));
  51306. for (;;) {
  51307. /* more initializers */
  51308. if (comp_ctx->curr_token.t != DUK_TOK_COMMA) {
  51309. break;
  51310. }
  51311. DUK_DDD(DUK_DDDPRINT("variant 2 has another variable initializer"));
  51312. duk__advance(comp_ctx); /* eat comma */
  51313. duk__parse_var_decl(comp_ctx, res, DUK__EXPR_FLAG_REJECT_IN, &reg_varbind, &rc_varname);
  51314. }
  51315. goto parse_1_or_2;
  51316. }
  51317. } else {
  51318. /*
  51319. * Variant 1 or 3
  51320. */
  51321. pc_v34_lhs = duk__get_current_pc(comp_ctx); /* jump is inserted here (variant 3) */
  51322. /* Note that duk__exprtop() here can clobber any reg above current temp_next,
  51323. * so any loop variables (e.g. enumerator) must be "preallocated".
  51324. */
  51325. /* don't coerce yet to a plain value (variant 3 needs special handling) */
  51326. duk__exprtop(comp_ctx, res, DUK__BP_FOR_EXPR | DUK__EXPR_FLAG_REJECT_IN | DUK__EXPR_FLAG_ALLOW_EMPTY /*rbp_flags*/); /* Expression */
  51327. if (comp_ctx->curr_token.t == DUK_TOK_IN) {
  51328. /*
  51329. * Variant 3
  51330. */
  51331. /* XXX: need to determine LHS type, and check that it is LHS compatible */
  51332. DUK_DDD(DUK_DDDPRINT("detected for variant 3: for (LeftHandSideExpression in Expression) Statement"));
  51333. if (duk__expr_is_empty(comp_ctx)) {
  51334. goto syntax_error; /* LeftHandSideExpression does not allow empty expression */
  51335. }
  51336. if (res->t == DUK_IVAL_VAR) {
  51337. duk_reg_t reg_varbind;
  51338. duk_regconst_t rc_varname;
  51339. duk_dup(ctx, res->x1.valstack_idx);
  51340. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  51341. duk__emit_a_bc(comp_ctx,
  51342. DUK_OP_LDREG,
  51343. (duk_regconst_t) reg_varbind,
  51344. (duk_regconst_t) (reg_temps + 0));
  51345. } else {
  51346. duk__emit_a_bc(comp_ctx,
  51347. DUK_OP_PUTVAR | DUK__EMIT_FLAG_A_IS_SOURCE,
  51348. (duk_regconst_t) (reg_temps + 0),
  51349. rc_varname);
  51350. }
  51351. } else if (res->t == DUK_IVAL_PROP) {
  51352. /* Don't allow a constant for the object (even for a number etc), as
  51353. * it goes into the 'A' field of the opcode.
  51354. */
  51355. duk_reg_t reg_obj;
  51356. duk_regconst_t rc_key;
  51357. reg_obj = duk__ispec_toregconst_raw(comp_ctx, &res->x1, -1 /*forced_reg*/, 0 /*flags*/); /* don't allow const */
  51358. rc_key = duk__ispec_toregconst_raw(comp_ctx, &res->x2, -1 /*forced_reg*/, DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  51359. duk__emit_a_b_c(comp_ctx,
  51360. DUK_OP_PUTPROP | DUK__EMIT_FLAG_A_IS_SOURCE,
  51361. (duk_regconst_t) reg_obj,
  51362. rc_key,
  51363. (duk_regconst_t) (reg_temps + 0));
  51364. } else {
  51365. duk__ivalue_toplain_ignore(comp_ctx, res); /* just in case */
  51366. duk__emit_extraop_only(comp_ctx,
  51367. DUK_EXTRAOP_INVLHS);
  51368. }
  51369. goto parse_3_or_4;
  51370. } else {
  51371. /*
  51372. * Variant 1
  51373. */
  51374. DUK_DDD(DUK_DDDPRINT("detected for variant 1: for (ExpressionNoIn_opt; Expression_opt; Expression_opt) Statement"));
  51375. duk__ivalue_toplain_ignore(comp_ctx, res);
  51376. goto parse_1_or_2;
  51377. }
  51378. }
  51379. parse_1_or_2:
  51380. /*
  51381. * Parse variant 1 or 2. The first part expression (which differs
  51382. * in the variants) has already been parsed and its code emitted.
  51383. *
  51384. * reg_temps + 0: unused
  51385. * reg_temps + 1: unused
  51386. */
  51387. {
  51388. duk_regconst_t rc_cond;
  51389. duk_int_t pc_l1, pc_l2, pc_l3, pc_l4;
  51390. duk_int_t pc_jumpto_l3, pc_jumpto_l4;
  51391. duk_bool_t expr_c_empty;
  51392. DUK_DDD(DUK_DDDPRINT("shared code for parsing variants 1 and 2"));
  51393. /* "release" preallocated temps since we won't need them */
  51394. temp_reset = reg_temps + 0;
  51395. DUK__SETTEMP(comp_ctx, temp_reset);
  51396. duk__advance_expect(comp_ctx, DUK_TOK_SEMICOLON);
  51397. pc_l1 = duk__get_current_pc(comp_ctx);
  51398. duk__exprtop(comp_ctx, res, DUK__BP_FOR_EXPR | DUK__EXPR_FLAG_ALLOW_EMPTY /*rbp_flags*/); /* Expression_opt */
  51399. if (duk__expr_is_empty(comp_ctx)) {
  51400. /* no need to coerce */
  51401. pc_jumpto_l3 = duk__emit_jump_empty(comp_ctx); /* to body */
  51402. pc_jumpto_l4 = -1; /* omitted */
  51403. } else {
  51404. rc_cond = duk__ivalue_toregconst(comp_ctx, res);
  51405. duk__emit_if_false_skip(comp_ctx, rc_cond);
  51406. pc_jumpto_l3 = duk__emit_jump_empty(comp_ctx); /* to body */
  51407. pc_jumpto_l4 = duk__emit_jump_empty(comp_ctx); /* to exit */
  51408. }
  51409. DUK__SETTEMP(comp_ctx, temp_reset);
  51410. duk__advance_expect(comp_ctx, DUK_TOK_SEMICOLON);
  51411. pc_l2 = duk__get_current_pc(comp_ctx);
  51412. duk__exprtop(comp_ctx, res, DUK__BP_FOR_EXPR | DUK__EXPR_FLAG_ALLOW_EMPTY /*rbp_flags*/); /* Expression_opt */
  51413. if (duk__expr_is_empty(comp_ctx)) {
  51414. /* no need to coerce */
  51415. expr_c_empty = 1;
  51416. /* JUMP L1 omitted */
  51417. } else {
  51418. duk__ivalue_toplain_ignore(comp_ctx, res);
  51419. expr_c_empty = 0;
  51420. duk__emit_jump(comp_ctx, pc_l1);
  51421. }
  51422. DUK__SETTEMP(comp_ctx, temp_reset);
  51423. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  51424. pc_l3 = duk__get_current_pc(comp_ctx);
  51425. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  51426. if (expr_c_empty) {
  51427. duk__emit_jump(comp_ctx, pc_l1);
  51428. } else {
  51429. duk__emit_jump(comp_ctx, pc_l2);
  51430. }
  51431. /* temp reset is not necessary after duk__parse_stmt(), which already does it */
  51432. pc_l4 = duk__get_current_pc(comp_ctx);
  51433. DUK_DDD(DUK_DDDPRINT("patching jumps: jumpto_l3: %ld->%ld, jumpto_l4: %ld->%ld, "
  51434. "break: %ld->%ld, continue: %ld->%ld",
  51435. (long) pc_jumpto_l3, (long) pc_l3, (long) pc_jumpto_l4, (long) pc_l4,
  51436. (long) (pc_label_site + 1), (long) pc_l4, (long) (pc_label_site + 2), (long) pc_l2));
  51437. duk__patch_jump(comp_ctx, pc_jumpto_l3, pc_l3);
  51438. duk__patch_jump(comp_ctx, pc_jumpto_l4, pc_l4);
  51439. duk__patch_jump(comp_ctx,
  51440. pc_label_site + 1,
  51441. pc_l4); /* break jump */
  51442. duk__patch_jump(comp_ctx,
  51443. pc_label_site + 2,
  51444. expr_c_empty ? pc_l1 : pc_l2); /* continue jump */
  51445. }
  51446. goto finished;
  51447. parse_3_or_4:
  51448. /*
  51449. * Parse variant 3 or 4.
  51450. *
  51451. * For variant 3 (e.g. "for (A in C) D;") the code for A (except the
  51452. * final property/variable write) has already been emitted. The first
  51453. * instruction of that code is at pc_v34_lhs; a JUMP needs to be inserted
  51454. * there to satisfy control flow needs.
  51455. *
  51456. * For variant 4, if the variable declaration had an initializer
  51457. * (e.g. "for (var A = B in C) D;") the code for the assignment
  51458. * (B) has already been emitted.
  51459. *
  51460. * Variables set before entering here:
  51461. *
  51462. * pc_v34_lhs: insert a "JUMP L2" here (see doc/compiler.txt example).
  51463. * reg_temps + 0: iteration target value (written to LHS)
  51464. * reg_temps + 1: enumerator object
  51465. */
  51466. {
  51467. duk_int_t pc_l1, pc_l2, pc_l3, pc_l4, pc_l5;
  51468. duk_int_t pc_jumpto_l2, pc_jumpto_l3, pc_jumpto_l4, pc_jumpto_l5;
  51469. duk_reg_t reg_target;
  51470. DUK_DDD(DUK_DDDPRINT("shared code for parsing variants 3 and 4, pc_v34_lhs=%ld", (long) pc_v34_lhs));
  51471. DUK__SETTEMP(comp_ctx, temp_reset);
  51472. /* First we need to insert a jump in the middle of previously
  51473. * emitted code to get the control flow right. No jumps can
  51474. * cross the position where the jump is inserted. See doc/compiler.txt
  51475. * for discussion on the intricacies of control flow and side effects
  51476. * for variants 3 and 4.
  51477. */
  51478. duk__insert_jump_entry(comp_ctx, pc_v34_lhs);
  51479. pc_jumpto_l2 = pc_v34_lhs; /* inserted jump */
  51480. pc_l1 = pc_v34_lhs + 1; /* +1, right after inserted jump */
  51481. /* The code for writing reg_temps + 0 to the left hand side has already
  51482. * been emitted.
  51483. */
  51484. pc_jumpto_l3 = duk__emit_jump_empty(comp_ctx); /* -> loop body */
  51485. duk__advance(comp_ctx); /* eat 'in' */
  51486. /* Parse enumeration target and initialize enumerator. For 'null' and 'undefined',
  51487. * INITENUM will creates a 'null' enumerator which works like an empty enumerator
  51488. * (E5 Section 12.6.4, step 3). Note that INITENUM requires the value to be in a
  51489. * register (constant not allowed).
  51490. */
  51491. pc_l2 = duk__get_current_pc(comp_ctx);
  51492. reg_target = duk__exprtop_toreg(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/); /* Expression */
  51493. duk__emit_extraop_b_c(comp_ctx,
  51494. DUK_EXTRAOP_INITENUM | DUK__EMIT_FLAG_B_IS_TARGET,
  51495. (duk_regconst_t) (reg_temps + 1),
  51496. (duk_regconst_t) reg_target);
  51497. pc_jumpto_l4 = duk__emit_jump_empty(comp_ctx);
  51498. DUK__SETTEMP(comp_ctx, temp_reset);
  51499. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  51500. pc_l3 = duk__get_current_pc(comp_ctx);
  51501. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  51502. /* temp reset is not necessary after duk__parse_stmt(), which already does it */
  51503. /* NEXTENUM needs a jump slot right after the main opcode.
  51504. * We need the code emitter to reserve the slot: if there's
  51505. * target shuffling, the target shuffle opcodes must happen
  51506. * after the jump slot (for NEXTENUM the shuffle opcodes are
  51507. * not needed if the enum is finished).
  51508. */
  51509. pc_l4 = duk__get_current_pc(comp_ctx);
  51510. duk__emit_extraop_b_c(comp_ctx,
  51511. DUK_EXTRAOP_NEXTENUM | DUK__EMIT_FLAG_B_IS_TARGET | DUK__EMIT_FLAG_RESERVE_JUMPSLOT,
  51512. (duk_regconst_t) (reg_temps + 0),
  51513. (duk_regconst_t) (reg_temps + 1));
  51514. pc_jumpto_l5 = comp_ctx->emit_jumpslot_pc; /* NEXTENUM jump slot: executed when enum finished */
  51515. duk__emit_jump(comp_ctx, pc_l1); /* jump to next loop, using reg_v34_iter as iterated value */
  51516. pc_l5 = duk__get_current_pc(comp_ctx);
  51517. /* XXX: since the enumerator may be a memory expensive object,
  51518. * perhaps clear it explicitly here? If so, break jump must
  51519. * go through this clearing operation.
  51520. */
  51521. DUK_DDD(DUK_DDDPRINT("patching jumps: jumpto_l2: %ld->%ld, jumpto_l3: %ld->%ld, "
  51522. "jumpto_l4: %ld->%ld, jumpto_l5: %ld->%ld, "
  51523. "break: %ld->%ld, continue: %ld->%ld",
  51524. (long) pc_jumpto_l2, (long) pc_l2, (long) pc_jumpto_l3, (long) pc_l3,
  51525. (long) pc_jumpto_l4, (long) pc_l4, (long) pc_jumpto_l5, (long) pc_l5,
  51526. (long) (pc_label_site + 1), (long) pc_l5, (long) (pc_label_site + 2), (long) pc_l4));
  51527. duk__patch_jump(comp_ctx, pc_jumpto_l2, pc_l2);
  51528. duk__patch_jump(comp_ctx, pc_jumpto_l3, pc_l3);
  51529. duk__patch_jump(comp_ctx, pc_jumpto_l4, pc_l4);
  51530. duk__patch_jump(comp_ctx, pc_jumpto_l5, pc_l5);
  51531. duk__patch_jump(comp_ctx, pc_label_site + 1, pc_l5); /* break jump */
  51532. duk__patch_jump(comp_ctx, pc_label_site + 2, pc_l4); /* continue jump */
  51533. }
  51534. goto finished;
  51535. finished:
  51536. DUK_DDD(DUK_DDDPRINT("end parsing a for/for-in statement"));
  51537. return;
  51538. syntax_error:
  51539. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_FOR);
  51540. }
  51541. DUK_LOCAL void duk__parse_switch_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site) {
  51542. duk_hthread *thr = comp_ctx->thr;
  51543. duk_reg_t temp_at_loop;
  51544. duk_regconst_t rc_switch; /* reg/const for switch value */
  51545. duk_regconst_t rc_case; /* reg/const for case value */
  51546. duk_reg_t reg_temp; /* general temp register */
  51547. duk_int_t pc_prevcase = -1;
  51548. duk_int_t pc_prevstmt = -1;
  51549. duk_int_t pc_default = -1; /* -1 == not set, -2 == pending (next statement list) */
  51550. /* Note: negative pc values are ignored when patching jumps, so no explicit checks needed */
  51551. /*
  51552. * Switch is pretty complicated because of several conflicting concerns:
  51553. *
  51554. * - Want to generate code without an intermediate representation,
  51555. * i.e., in one go
  51556. *
  51557. * - Case selectors are expressions, not values, and may thus e.g. throw
  51558. * exceptions (which causes evaluation order concerns)
  51559. *
  51560. * - Evaluation semantics of case selectors and default clause need to be
  51561. * carefully implemented to provide correct behavior even with case value
  51562. * side effects
  51563. *
  51564. * - Fall through case and default clauses; avoiding dead JUMPs if case
  51565. * ends with an unconditional jump (a break or a continue)
  51566. *
  51567. * - The same case value may occur multiple times, but evaluation rules
  51568. * only process the first match before switching to a "propagation" mode
  51569. * where case values are no longer evaluated
  51570. *
  51571. * See E5 Section 12.11. Also see doc/compiler.txt for compilation
  51572. * discussion.
  51573. */
  51574. duk__advance(comp_ctx);
  51575. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  51576. rc_switch = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  51577. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  51578. duk__advance_expect(comp_ctx, DUK_TOK_LCURLY);
  51579. DUK_DDD(DUK_DDDPRINT("switch value in register %ld", (long) rc_switch));
  51580. temp_at_loop = DUK__GETTEMP(comp_ctx);
  51581. for (;;) {
  51582. duk_int_t num_stmts;
  51583. duk_small_int_t tok;
  51584. /* sufficient for keeping temp reg numbers in check */
  51585. DUK__SETTEMP(comp_ctx, temp_at_loop);
  51586. if (comp_ctx->curr_token.t == DUK_TOK_RCURLY) {
  51587. break;
  51588. }
  51589. /*
  51590. * Parse a case or default clause.
  51591. */
  51592. if (comp_ctx->curr_token.t == DUK_TOK_CASE) {
  51593. /*
  51594. * Case clause.
  51595. *
  51596. * Note: cannot use reg_case as a temp register (for SEQ target)
  51597. * because it may be a constant.
  51598. */
  51599. duk__patch_jump_here(comp_ctx, pc_prevcase); /* chain jumps for case
  51600. * evaluation and checking
  51601. */
  51602. duk__advance(comp_ctx);
  51603. rc_case = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  51604. duk__advance_expect(comp_ctx, DUK_TOK_COLON);
  51605. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  51606. duk__emit_a_b_c(comp_ctx,
  51607. DUK_OP_SEQ,
  51608. (duk_regconst_t) reg_temp,
  51609. rc_switch,
  51610. rc_case);
  51611. duk__emit_if_true_skip(comp_ctx, (duk_regconst_t) reg_temp);
  51612. /* jump to next case clause */
  51613. pc_prevcase = duk__emit_jump_empty(comp_ctx); /* no match, next case */
  51614. /* statements go here (if any) on next loop */
  51615. } else if (comp_ctx->curr_token.t == DUK_TOK_DEFAULT) {
  51616. /*
  51617. * Default clause.
  51618. */
  51619. if (pc_default >= 0) {
  51620. goto syntax_error;
  51621. }
  51622. duk__advance(comp_ctx);
  51623. duk__advance_expect(comp_ctx, DUK_TOK_COLON);
  51624. /* Fix for https://github.com/svaarala/duktape/issues/155:
  51625. * If 'default' is first clause (detected by pc_prevcase < 0)
  51626. * we need to ensure we stay in the matching chain.
  51627. */
  51628. if (pc_prevcase < 0) {
  51629. DUK_DD(DUK_DDPRINT("default clause is first, emit prevcase jump"));
  51630. pc_prevcase = duk__emit_jump_empty(comp_ctx);
  51631. }
  51632. /* default clause matches next statement list (if any) */
  51633. pc_default = -2;
  51634. } else {
  51635. /* Code is not accepted before the first case/default clause */
  51636. goto syntax_error;
  51637. }
  51638. /*
  51639. * Parse code after the clause. Possible terminators are
  51640. * 'case', 'default', and '}'.
  51641. *
  51642. * Note that there may be no code at all, not even an empty statement,
  51643. * between case clauses. This must be handled just like an empty statement
  51644. * (omitting seemingly pointless JUMPs), to avoid situations like
  51645. * test-bug-case-fallthrough.js.
  51646. */
  51647. num_stmts = 0;
  51648. if (pc_default == -2) {
  51649. pc_default = duk__get_current_pc(comp_ctx);
  51650. }
  51651. /* Note: this is correct even for default clause statements:
  51652. * they participate in 'fall-through' behavior even if the
  51653. * default clause is in the middle.
  51654. */
  51655. duk__patch_jump_here(comp_ctx, pc_prevstmt); /* chain jumps for 'fall-through'
  51656. * after a case matches.
  51657. */
  51658. for (;;) {
  51659. tok = comp_ctx->curr_token.t;
  51660. if (tok == DUK_TOK_CASE || tok == DUK_TOK_DEFAULT ||
  51661. tok == DUK_TOK_RCURLY) {
  51662. break;
  51663. }
  51664. num_stmts++;
  51665. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  51666. }
  51667. /* fall-through jump to next code of next case (backpatched) */
  51668. pc_prevstmt = duk__emit_jump_empty(comp_ctx);
  51669. /* XXX: would be nice to omit this jump when the jump is not
  51670. * reachable, at least in the obvious cases (such as the case
  51671. * ending with a 'break'.
  51672. *
  51673. * Perhaps duk__parse_stmt() could provide some info on whether
  51674. * the statement is a "dead end"?
  51675. *
  51676. * If implemented, just set pc_prevstmt to -1 when not needed.
  51677. */
  51678. }
  51679. DUK_ASSERT(comp_ctx->curr_token.t == DUK_TOK_RCURLY);
  51680. duk__advance(comp_ctx);
  51681. /* default case control flow patchup; note that if pc_prevcase < 0
  51682. * (i.e. no case clauses), control enters default case automatically.
  51683. */
  51684. if (pc_default >= 0) {
  51685. /* default case exists: go there if no case matches */
  51686. duk__patch_jump(comp_ctx, pc_prevcase, pc_default);
  51687. } else {
  51688. /* default case does not exist, or no statements present
  51689. * after default case: finish case evaluation
  51690. */
  51691. duk__patch_jump_here(comp_ctx, pc_prevcase);
  51692. }
  51693. /* fall-through control flow patchup; note that pc_prevstmt may be
  51694. * < 0 (i.e. no case clauses), in which case this is a no-op.
  51695. */
  51696. duk__patch_jump_here(comp_ctx, pc_prevstmt);
  51697. /* continue jump not patched, an INVALID opcode remains there */
  51698. duk__patch_jump_here(comp_ctx, pc_label_site + 1); /* break jump */
  51699. /* Note: 'fast' breaks will jump to pc_label_site + 1, which will
  51700. * then jump here. The double jump will be eliminated by a
  51701. * peephole pass, resulting in an optimal jump here. The label
  51702. * site jumps will remain in bytecode and will waste code size.
  51703. */
  51704. return;
  51705. syntax_error:
  51706. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_SWITCH);
  51707. }
  51708. DUK_LOCAL void duk__parse_if_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  51709. duk_reg_t temp_reset;
  51710. duk_regconst_t rc_cond;
  51711. duk_int_t pc_jump_false;
  51712. DUK_DDD(DUK_DDDPRINT("begin parsing if statement"));
  51713. temp_reset = DUK__GETTEMP(comp_ctx);
  51714. duk__advance(comp_ctx); /* eat 'if' */
  51715. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  51716. rc_cond = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  51717. duk__emit_if_true_skip(comp_ctx, rc_cond);
  51718. pc_jump_false = duk__emit_jump_empty(comp_ctx); /* jump to end or else part */
  51719. DUK__SETTEMP(comp_ctx, temp_reset);
  51720. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  51721. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  51722. /* The 'else' ambiguity is resolved by 'else' binding to the innermost
  51723. * construct, so greedy matching is correct here.
  51724. */
  51725. if (comp_ctx->curr_token.t == DUK_TOK_ELSE) {
  51726. duk_int_t pc_jump_end;
  51727. DUK_DDD(DUK_DDDPRINT("if has else part"));
  51728. duk__advance(comp_ctx);
  51729. pc_jump_end = duk__emit_jump_empty(comp_ctx); /* jump from true part to end */
  51730. duk__patch_jump_here(comp_ctx, pc_jump_false);
  51731. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  51732. duk__patch_jump_here(comp_ctx, pc_jump_end);
  51733. } else {
  51734. DUK_DDD(DUK_DDDPRINT("if does not have else part"));
  51735. duk__patch_jump_here(comp_ctx, pc_jump_false);
  51736. }
  51737. DUK_DDD(DUK_DDDPRINT("end parsing if statement"));
  51738. }
  51739. DUK_LOCAL void duk__parse_do_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site) {
  51740. duk_regconst_t rc_cond;
  51741. duk_int_t pc_start;
  51742. DUK_DDD(DUK_DDDPRINT("begin parsing do statement"));
  51743. duk__advance(comp_ctx); /* eat 'do' */
  51744. pc_start = duk__get_current_pc(comp_ctx);
  51745. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  51746. duk__patch_jump_here(comp_ctx, pc_label_site + 2); /* continue jump */
  51747. duk__advance_expect(comp_ctx, DUK_TOK_WHILE);
  51748. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  51749. rc_cond = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  51750. duk__emit_if_false_skip(comp_ctx, rc_cond);
  51751. duk__emit_jump(comp_ctx, pc_start);
  51752. /* no need to reset temps, as we're finished emitting code */
  51753. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  51754. duk__patch_jump_here(comp_ctx, pc_label_site + 1); /* break jump */
  51755. DUK_DDD(DUK_DDDPRINT("end parsing do statement"));
  51756. }
  51757. DUK_LOCAL void duk__parse_while_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site) {
  51758. duk_reg_t temp_reset;
  51759. duk_regconst_t rc_cond;
  51760. duk_int_t pc_start;
  51761. duk_int_t pc_jump_false;
  51762. DUK_DDD(DUK_DDDPRINT("begin parsing while statement"));
  51763. temp_reset = DUK__GETTEMP(comp_ctx);
  51764. duk__advance(comp_ctx); /* eat 'while' */
  51765. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  51766. pc_start = duk__get_current_pc(comp_ctx);
  51767. duk__patch_jump_here(comp_ctx, pc_label_site + 2); /* continue jump */
  51768. rc_cond = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  51769. duk__emit_if_true_skip(comp_ctx, rc_cond);
  51770. pc_jump_false = duk__emit_jump_empty(comp_ctx);
  51771. DUK__SETTEMP(comp_ctx, temp_reset);
  51772. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  51773. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  51774. duk__emit_jump(comp_ctx, pc_start);
  51775. duk__patch_jump_here(comp_ctx, pc_jump_false);
  51776. duk__patch_jump_here(comp_ctx, pc_label_site + 1); /* break jump */
  51777. DUK_DDD(DUK_DDDPRINT("end parsing while statement"));
  51778. }
  51779. DUK_LOCAL void duk__parse_break_or_continue_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  51780. duk_hthread *thr = comp_ctx->thr;
  51781. duk_bool_t is_break = (comp_ctx->curr_token.t == DUK_TOK_BREAK);
  51782. duk_int_t label_id;
  51783. duk_int_t label_catch_depth;
  51784. duk_int_t label_pc; /* points to LABEL; pc+1 = jump site for break; pc+2 = jump site for continue */
  51785. duk_bool_t label_is_closest;
  51786. DUK_UNREF(res);
  51787. duk__advance(comp_ctx); /* eat 'break' or 'continue' */
  51788. if (comp_ctx->curr_token.t == DUK_TOK_SEMICOLON || /* explicit semi follows */
  51789. comp_ctx->curr_token.lineterm || /* automatic semi will be inserted */
  51790. comp_ctx->curr_token.allow_auto_semi) { /* automatic semi will be inserted */
  51791. /* break/continue without label */
  51792. duk__lookup_active_label(comp_ctx, DUK_HTHREAD_STRING_EMPTY_STRING(thr), is_break, &label_id, &label_catch_depth, &label_pc, &label_is_closest);
  51793. } else if (comp_ctx->curr_token.t == DUK_TOK_IDENTIFIER) {
  51794. /* break/continue with label (label cannot be a reserved word, production is 'Identifier' */
  51795. DUK_ASSERT(comp_ctx->curr_token.str1 != NULL);
  51796. duk__lookup_active_label(comp_ctx, comp_ctx->curr_token.str1, is_break, &label_id, &label_catch_depth, &label_pc, &label_is_closest);
  51797. duk__advance(comp_ctx);
  51798. } else {
  51799. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_BREAK_CONT_LABEL);
  51800. }
  51801. /* Use a fast break/continue when possible. A fast break/continue is
  51802. * just a jump to the LABEL break/continue jump slot, which then jumps
  51803. * to an appropriate place (for break, going through ENDLABEL correctly).
  51804. * The peephole optimizer will optimize the jump to a direct one.
  51805. */
  51806. if (label_catch_depth == comp_ctx->curr_func.catch_depth &&
  51807. label_is_closest) {
  51808. DUK_DDD(DUK_DDDPRINT("break/continue: is_break=%ld, label_id=%ld, label_is_closest=%ld, "
  51809. "label_catch_depth=%ld, catch_depth=%ld "
  51810. "-> use fast variant (direct jump)",
  51811. (long) is_break, (long) label_id, (long) label_is_closest,
  51812. (long) label_catch_depth, (long) comp_ctx->curr_func.catch_depth));
  51813. duk__emit_jump(comp_ctx, label_pc + (is_break ? 1 : 2));
  51814. } else {
  51815. DUK_DDD(DUK_DDDPRINT("break/continue: is_break=%ld, label_id=%ld, label_is_closest=%ld, "
  51816. "label_catch_depth=%ld, catch_depth=%ld "
  51817. "-> use slow variant (longjmp)",
  51818. (long) is_break, (long) label_id, (long) label_is_closest,
  51819. (long) label_catch_depth, (long) comp_ctx->curr_func.catch_depth));
  51820. duk__emit_extraop_bc(comp_ctx,
  51821. is_break ? DUK_EXTRAOP_BREAK : DUK_EXTRAOP_CONTINUE,
  51822. (duk_regconst_t) label_id);
  51823. }
  51824. }
  51825. DUK_LOCAL void duk__parse_return_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  51826. duk_hthread *thr = comp_ctx->thr;
  51827. duk_regconst_t rc_val;
  51828. duk_small_uint_t ret_flags;
  51829. duk__advance(comp_ctx); /* eat 'return' */
  51830. /* A 'return' statement is only allowed inside an actual function body,
  51831. * not as part of eval or global code.
  51832. */
  51833. if (!comp_ctx->curr_func.is_function) {
  51834. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_RETURN);
  51835. }
  51836. /* Use a fast return when possible. A fast return does not cause a longjmp()
  51837. * unnecessarily. A fast return can be done when no TCF catchers are active
  51838. * (this includes 'try' and 'with' statements). Active label catches do not
  51839. * prevent a fast return; they're unwound on return automatically.
  51840. */
  51841. ret_flags = 0;
  51842. if (comp_ctx->curr_token.t == DUK_TOK_SEMICOLON || /* explicit semi follows */
  51843. comp_ctx->curr_token.lineterm || /* automatic semi will be inserted */
  51844. comp_ctx->curr_token.allow_auto_semi) { /* automatic semi will be inserted */
  51845. DUK_DDD(DUK_DDDPRINT("empty return value -> undefined"));
  51846. rc_val = 0;
  51847. } else {
  51848. duk_int_t pc_before_expr;
  51849. duk_int_t pc_after_expr;
  51850. DUK_DDD(DUK_DDDPRINT("return with a value"));
  51851. DUK_UNREF(pc_before_expr);
  51852. DUK_UNREF(pc_after_expr);
  51853. pc_before_expr = duk__get_current_pc(comp_ctx);
  51854. rc_val = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  51855. pc_after_expr = duk__get_current_pc(comp_ctx);
  51856. /* Tail call check: if last opcode emitted was CALL(I), and
  51857. * the context allows it, change the CALL(I) to a tailcall.
  51858. * This doesn't guarantee that a tailcall will be allowed at
  51859. * runtime, so the RETURN must still be emitted. (Duktape
  51860. * 0.10.0 avoided this and simulated a RETURN if a tailcall
  51861. * couldn't be used at runtime; but this didn't work
  51862. * correctly with a thread yield/resume, see
  51863. * test-bug-tailcall-thread-yield-resume.js for discussion.)
  51864. *
  51865. * In addition to the last opcode being CALL, we also need to
  51866. * be sure that 'rc_val' is the result register of the CALL(I).
  51867. * For instance, for the expression 'return 0, (function ()
  51868. * { return 1; }), 2' the last opcode emitted is CALL (no
  51869. * bytecode is emitted for '2') but 'rc_val' indicates
  51870. * constant '2'. Similarly if '2' is replaced by a register
  51871. * bound variable, no opcodes are emitted but tailcall would
  51872. * be incorrect.
  51873. *
  51874. * This is tricky and easy to get wrong. It would be best to
  51875. * track enough expression metadata to check that 'rc_val' came
  51876. * from that last CALL instruction. We don't have that metadata
  51877. * now, so we check that 'rc_val' is a temporary register result
  51878. * (not a constant or a register bound variable). There should
  51879. * be no way currently for 'rc_val' to be a temporary for an
  51880. * expression following the CALL instruction without emitting
  51881. * some opcodes following the CALL. This proxy check is used
  51882. * below.
  51883. *
  51884. * See: test-bug-comma-expr-gh131.js.
  51885. *
  51886. * The non-standard 'caller' property disables tail calls
  51887. * because they pose some special cases which haven't been
  51888. * fixed yet.
  51889. */
  51890. #if defined(DUK_USE_TAILCALL)
  51891. if (comp_ctx->curr_func.catch_depth == 0 && /* no catchers */
  51892. pc_after_expr > pc_before_expr) { /* at least one opcode emitted */
  51893. duk_compiler_instr *instr;
  51894. duk_small_uint_t op;
  51895. instr = duk__get_instr_ptr(comp_ctx, pc_after_expr - 1);
  51896. DUK_ASSERT(instr != NULL);
  51897. op = (duk_small_uint_t) DUK_DEC_OP(instr->ins);
  51898. if ((op == DUK_OP_CALL || op == DUK_OP_CALLI) &&
  51899. DUK__ISTEMP(comp_ctx, rc_val) /* see above */) {
  51900. DUK_DDD(DUK_DDDPRINT("return statement detected a tail call opportunity: "
  51901. "catch depth is 0, duk__exprtop() emitted >= 1 instructions, "
  51902. "and last instruction is a CALL "
  51903. "-> set TAILCALL flag"));
  51904. /* Just flip the single bit. */
  51905. instr->ins |= DUK_ENC_OP_A_B_C(0, DUK_BC_CALL_FLAG_TAILCALL, 0, 0);
  51906. }
  51907. }
  51908. #endif /* DUK_USE_TAILCALL */
  51909. ret_flags = DUK_BC_RETURN_FLAG_HAVE_RETVAL;
  51910. }
  51911. /* XXX: For now, "fast returns" are disabled. The compiler doesn't track
  51912. * label site depth so when it emits a fast return, it doesn't know whether
  51913. * label sites exist or not. Label sites are emitted for e.g. for loops,
  51914. * so it's probably quite relevant to handle them in the executor's fast
  51915. * return handler.
  51916. */
  51917. #if 0
  51918. if (comp_ctx->curr_func.catch_depth == 0) {
  51919. DUK_DDD(DUK_DDDPRINT("fast return allowed -> use fast return"));
  51920. ret_flags |= DUK_BC_RETURN_FLAG_FAST;
  51921. } else {
  51922. DUK_DDD(DUK_DDDPRINT("fast return not allowed -> use slow return"));
  51923. }
  51924. #endif
  51925. duk__emit_a_b(comp_ctx,
  51926. DUK_OP_RETURN | DUK__EMIT_FLAG_NO_SHUFFLE_A,
  51927. (duk_regconst_t) ret_flags /*flags*/,
  51928. rc_val /*reg*/);
  51929. }
  51930. DUK_LOCAL void duk__parse_throw_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  51931. duk_reg_t reg_val;
  51932. duk__advance(comp_ctx); /* eat 'throw' */
  51933. /* Unlike break/continue, throw statement does not allow an empty value. */
  51934. if (comp_ctx->curr_token.lineterm) {
  51935. DUK_ERROR(comp_ctx->thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_THROW);
  51936. }
  51937. reg_val = duk__exprtop_toreg(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  51938. duk__emit_extraop_bc(comp_ctx,
  51939. DUK_EXTRAOP_THROW,
  51940. (duk_regconst_t) reg_val);
  51941. }
  51942. DUK_LOCAL void duk__parse_try_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  51943. duk_hthread *thr = comp_ctx->thr;
  51944. duk_context *ctx = (duk_context *) thr;
  51945. duk_reg_t reg_catch; /* reg_catch+0 and reg_catch+1 are reserved for TRYCATCH */
  51946. duk_regconst_t rc_varname = 0;
  51947. duk_small_uint_t trycatch_flags = 0;
  51948. duk_int_t pc_trycatch = -1;
  51949. duk_int_t pc_catch = -1;
  51950. duk_int_t pc_finally = -1;
  51951. DUK_UNREF(res);
  51952. /*
  51953. * See the following documentation for discussion:
  51954. *
  51955. * doc/execution.txt: control flow details
  51956. *
  51957. * Try, catch, and finally "parts" are Blocks, not Statements, so
  51958. * they must always be delimited by curly braces. This is unlike e.g.
  51959. * the if statement, which accepts any Statement. This eliminates any
  51960. * questions of matching parts of nested try statements. The Block
  51961. * parsing is implemented inline here (instead of calling out).
  51962. *
  51963. * Finally part has a 'let scoped' variable, which requires a few kinks
  51964. * here.
  51965. */
  51966. comp_ctx->curr_func.catch_depth++;
  51967. duk__advance(comp_ctx); /* eat 'try' */
  51968. reg_catch = DUK__ALLOCTEMPS(comp_ctx, 2);
  51969. pc_trycatch = duk__get_current_pc(comp_ctx);
  51970. duk__emit_invalid(comp_ctx); /* TRYCATCH, cannot emit now (not enough info) */
  51971. duk__emit_invalid(comp_ctx); /* jump for 'catch' case */
  51972. duk__emit_invalid(comp_ctx); /* jump for 'finally' case or end (if no finally) */
  51973. /* try part */
  51974. duk__advance_expect(comp_ctx, DUK_TOK_LCURLY);
  51975. duk__parse_stmts(comp_ctx, 0 /*allow_source_elem*/, 0 /*expect_eof*/);
  51976. /* the DUK_TOK_RCURLY is eaten by duk__parse_stmts() */
  51977. duk__emit_extraop_only(comp_ctx,
  51978. DUK_EXTRAOP_ENDTRY);
  51979. if (comp_ctx->curr_token.t == DUK_TOK_CATCH) {
  51980. /*
  51981. * The catch variable must be updated to reflect the new allocated
  51982. * register for the duration of the catch clause. We need to store
  51983. * and restore the original value for the varmap entry (if any).
  51984. */
  51985. /*
  51986. * Note: currently register bindings must be fixed for the entire
  51987. * function. So, even though the catch variable is in a register
  51988. * we know, we must use an explicit environment record and slow path
  51989. * accesses to read/write the catch binding to make closures created
  51990. * within the catch clause work correctly. This restriction should
  51991. * be fixable (at least in common cases) later.
  51992. *
  51993. * See: test-bug-catch-binding-2.js.
  51994. *
  51995. * XXX: improve to get fast path access to most catch clauses.
  51996. */
  51997. duk_hstring *h_var;
  51998. duk_int_t varmap_value; /* for storing/restoring the varmap binding for catch variable */
  51999. DUK_DDD(DUK_DDDPRINT("stack top at start of catch clause: %ld", (long) duk_get_top(ctx)));
  52000. trycatch_flags |= DUK_BC_TRYCATCH_FLAG_HAVE_CATCH;
  52001. pc_catch = duk__get_current_pc(comp_ctx);
  52002. duk__advance(comp_ctx);
  52003. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  52004. if (comp_ctx->curr_token.t != DUK_TOK_IDENTIFIER) {
  52005. /* Identifier, i.e. don't allow reserved words */
  52006. goto syntax_error;
  52007. }
  52008. h_var = comp_ctx->curr_token.str1;
  52009. DUK_ASSERT(h_var != NULL);
  52010. duk_push_hstring(ctx, h_var); /* keep in on valstack, use borrowed ref below */
  52011. if (comp_ctx->curr_func.is_strict &&
  52012. ((h_var == DUK_HTHREAD_STRING_EVAL(thr)) ||
  52013. (h_var == DUK_HTHREAD_STRING_LC_ARGUMENTS(thr)))) {
  52014. DUK_DDD(DUK_DDDPRINT("catch identifier 'eval' or 'arguments' in strict mode -> SyntaxError"));
  52015. goto syntax_error;
  52016. }
  52017. duk_dup_top(ctx);
  52018. rc_varname = duk__getconst(comp_ctx);
  52019. DUK_DDD(DUK_DDDPRINT("catch clause, rc_varname=0x%08lx (%ld)",
  52020. (unsigned long) rc_varname, (long) rc_varname));
  52021. duk__advance(comp_ctx);
  52022. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  52023. duk__advance_expect(comp_ctx, DUK_TOK_LCURLY);
  52024. DUK_DDD(DUK_DDDPRINT("varmap before modifying for catch clause: %!iT",
  52025. (duk_tval *) duk_get_tval(ctx, comp_ctx->curr_func.varmap_idx)));
  52026. duk_dup_top(ctx);
  52027. duk_get_prop(ctx, comp_ctx->curr_func.varmap_idx);
  52028. if (duk_is_undefined(ctx, -1)) {
  52029. varmap_value = -2;
  52030. } else if (duk_is_null(ctx, -1)) {
  52031. varmap_value = -1;
  52032. } else {
  52033. DUK_ASSERT(duk_is_number(ctx, -1));
  52034. varmap_value = duk_get_int(ctx, -1);
  52035. DUK_ASSERT(varmap_value >= 0);
  52036. }
  52037. duk_pop(ctx);
  52038. #if 0
  52039. /* It'd be nice to do something like this - but it doesn't
  52040. * work for closures created inside the catch clause.
  52041. */
  52042. duk_dup_top(ctx);
  52043. duk_push_int(ctx, (duk_int_t) (reg_catch + 0));
  52044. duk_put_prop(ctx, comp_ctx->curr_func.varmap_idx);
  52045. #endif
  52046. duk_dup_top(ctx);
  52047. duk_push_null(ctx);
  52048. duk_put_prop(ctx, comp_ctx->curr_func.varmap_idx);
  52049. duk__emit_a_bc(comp_ctx,
  52050. DUK_OP_PUTVAR | DUK__EMIT_FLAG_A_IS_SOURCE,
  52051. (duk_regconst_t) (reg_catch + 0) /*value*/,
  52052. rc_varname /*varname*/);
  52053. DUK_DDD(DUK_DDDPRINT("varmap before parsing catch clause: %!iT",
  52054. (duk_tval *) duk_get_tval(ctx, comp_ctx->curr_func.varmap_idx)));
  52055. duk__parse_stmts(comp_ctx, 0 /*allow_source_elem*/, 0 /*expect_eof*/);
  52056. /* the DUK_TOK_RCURLY is eaten by duk__parse_stmts() */
  52057. if (varmap_value == -2) {
  52058. /* not present */
  52059. duk_del_prop(ctx, comp_ctx->curr_func.varmap_idx);
  52060. } else {
  52061. if (varmap_value == -1) {
  52062. duk_push_null(ctx);
  52063. } else {
  52064. DUK_ASSERT(varmap_value >= 0);
  52065. duk_push_int(ctx, varmap_value);
  52066. }
  52067. duk_put_prop(ctx, comp_ctx->curr_func.varmap_idx);
  52068. }
  52069. /* varname is popped by above code */
  52070. DUK_DDD(DUK_DDDPRINT("varmap after restore catch clause: %!iT",
  52071. (duk_tval *) duk_get_tval(ctx, comp_ctx->curr_func.varmap_idx)));
  52072. duk__emit_extraop_only(comp_ctx,
  52073. DUK_EXTRAOP_ENDCATCH);
  52074. /*
  52075. * XXX: for now, indicate that an expensive catch binding
  52076. * declarative environment is always needed. If we don't
  52077. * need it, we don't need the const_varname either.
  52078. */
  52079. trycatch_flags |= DUK_BC_TRYCATCH_FLAG_CATCH_BINDING;
  52080. DUK_DDD(DUK_DDDPRINT("stack top at end of catch clause: %ld", (long) duk_get_top(ctx)));
  52081. }
  52082. if (comp_ctx->curr_token.t == DUK_TOK_FINALLY) {
  52083. trycatch_flags |= DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY;
  52084. pc_finally = duk__get_current_pc(comp_ctx);
  52085. duk__advance(comp_ctx);
  52086. duk__advance_expect(comp_ctx, DUK_TOK_LCURLY);
  52087. duk__parse_stmts(comp_ctx, 0 /*allow_source_elem*/, 0 /*expect_eof*/);
  52088. /* the DUK_TOK_RCURLY is eaten by duk__parse_stmts() */
  52089. duk__emit_extraop_b(comp_ctx,
  52090. DUK_EXTRAOP_ENDFIN,
  52091. reg_catch); /* rethrow */
  52092. }
  52093. if (!(trycatch_flags & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH) &&
  52094. !(trycatch_flags & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY)) {
  52095. /* must have catch and/or finally */
  52096. goto syntax_error;
  52097. }
  52098. duk__patch_trycatch(comp_ctx,
  52099. pc_trycatch,
  52100. reg_catch,
  52101. rc_varname,
  52102. trycatch_flags);
  52103. if (trycatch_flags & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH) {
  52104. DUK_ASSERT(pc_catch >= 0);
  52105. duk__patch_jump(comp_ctx, pc_trycatch + 1, pc_catch);
  52106. }
  52107. if (trycatch_flags & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY) {
  52108. DUK_ASSERT(pc_finally >= 0);
  52109. duk__patch_jump(comp_ctx, pc_trycatch + 2, pc_finally);
  52110. } else {
  52111. /* without finally, the second jump slot is used to jump to end of stmt */
  52112. duk__patch_jump_here(comp_ctx, pc_trycatch + 2);
  52113. }
  52114. comp_ctx->curr_func.catch_depth--;
  52115. return;
  52116. syntax_error:
  52117. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_TRY);
  52118. }
  52119. DUK_LOCAL void duk__parse_with_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  52120. duk_int_t pc_trycatch;
  52121. duk_int_t pc_finished;
  52122. duk_reg_t reg_catch;
  52123. duk_regconst_t rc_target;
  52124. duk_small_uint_t trycatch_flags;
  52125. if (comp_ctx->curr_func.is_strict) {
  52126. DUK_ERROR(comp_ctx->thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_WITH_IN_STRICT_MODE);
  52127. }
  52128. comp_ctx->curr_func.catch_depth++;
  52129. duk__advance(comp_ctx); /* eat 'with' */
  52130. reg_catch = DUK__ALLOCTEMPS(comp_ctx, 2);
  52131. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  52132. rc_target = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  52133. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  52134. /* XXX: the trycatch shuffle flags are now very limiting and a fix
  52135. * is needed to allow trycatch to work in functions with a very large
  52136. * number of temporaries or constants.
  52137. */
  52138. pc_trycatch = duk__get_current_pc(comp_ctx);
  52139. trycatch_flags = DUK_BC_TRYCATCH_FLAG_WITH_BINDING;
  52140. duk__emit_a_b_c(comp_ctx,
  52141. DUK_OP_TRYCATCH | DUK__EMIT_FLAG_NO_SHUFFLE_A
  52142. | DUK__EMIT_FLAG_NO_SHUFFLE_B
  52143. | DUK__EMIT_FLAG_NO_SHUFFLE_C,
  52144. (duk_regconst_t) trycatch_flags /*a*/,
  52145. (duk_regconst_t) reg_catch /*b*/,
  52146. rc_target /*c*/);
  52147. duk__emit_invalid(comp_ctx); /* catch jump */
  52148. duk__emit_invalid(comp_ctx); /* finished jump */
  52149. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  52150. duk__emit_extraop_only(comp_ctx,
  52151. DUK_EXTRAOP_ENDTRY);
  52152. pc_finished = duk__get_current_pc(comp_ctx);
  52153. duk__patch_jump(comp_ctx, pc_trycatch + 2, pc_finished);
  52154. comp_ctx->curr_func.catch_depth--;
  52155. }
  52156. DUK_LOCAL duk_int_t duk__stmt_label_site(duk_compiler_ctx *comp_ctx, duk_int_t label_id) {
  52157. /* if a site already exists, nop: max one label site per statement */
  52158. if (label_id >= 0) {
  52159. return label_id;
  52160. }
  52161. label_id = comp_ctx->curr_func.label_next++;
  52162. DUK_DDD(DUK_DDDPRINT("allocated new label id for label site: %ld", (long) label_id));
  52163. duk__emit_extraop_bc(comp_ctx,
  52164. DUK_EXTRAOP_LABEL,
  52165. (duk_regconst_t) label_id);
  52166. duk__emit_invalid(comp_ctx);
  52167. duk__emit_invalid(comp_ctx);
  52168. return label_id;
  52169. }
  52170. /* Parse a single statement.
  52171. *
  52172. * Creates a label site (with an empty label) automatically for iteration
  52173. * statements. Also "peels off" any label statements for explicit labels.
  52174. */
  52175. DUK_LOCAL void duk__parse_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_bool_t allow_source_elem) {
  52176. duk_hthread *thr = comp_ctx->thr;
  52177. duk_context *ctx = (duk_context *) thr;
  52178. duk_bool_t dir_prol_at_entry; /* directive prologue status at entry */
  52179. duk_reg_t temp_at_entry;
  52180. duk_uarridx_t labels_len_at_entry;
  52181. duk_int_t pc_at_entry; /* assumed to also be PC of "LABEL" */
  52182. duk_int_t stmt_id;
  52183. duk_small_uint_t stmt_flags = 0;
  52184. duk_int_t label_id = -1;
  52185. duk_small_uint_t tok;
  52186. DUK__RECURSION_INCREASE(comp_ctx, thr);
  52187. temp_at_entry = DUK__GETTEMP(comp_ctx);
  52188. pc_at_entry = duk__get_current_pc(comp_ctx);
  52189. labels_len_at_entry = (duk_uarridx_t) duk_get_length(ctx, comp_ctx->curr_func.labelnames_idx);
  52190. stmt_id = comp_ctx->curr_func.stmt_next++;
  52191. dir_prol_at_entry = comp_ctx->curr_func.in_directive_prologue;
  52192. DUK_UNREF(stmt_id);
  52193. DUK_DDD(DUK_DDDPRINT("parsing a statement, stmt_id=%ld, temp_at_entry=%ld, labels_len_at_entry=%ld, "
  52194. "is_strict=%ld, in_directive_prologue=%ld, catch_depth=%ld",
  52195. (long) stmt_id, (long) temp_at_entry, (long) labels_len_at_entry,
  52196. (long) comp_ctx->curr_func.is_strict, (long) comp_ctx->curr_func.in_directive_prologue,
  52197. (long) comp_ctx->curr_func.catch_depth));
  52198. /* The directive prologue flag is cleared by default so that it is
  52199. * unset for any recursive statement parsing. It is only "revived"
  52200. * if a directive is detected. (We could also make directives only
  52201. * allowed if 'allow_source_elem' was true.)
  52202. */
  52203. comp_ctx->curr_func.in_directive_prologue = 0;
  52204. retry_parse:
  52205. DUK_DDD(DUK_DDDPRINT("try stmt parse, stmt_id=%ld, label_id=%ld, allow_source_elem=%ld, catch_depth=%ld",
  52206. (long) stmt_id, (long) label_id, (long) allow_source_elem,
  52207. (long) comp_ctx->curr_func.catch_depth));
  52208. /*
  52209. * Detect iteration statements; if encountered, establish an
  52210. * empty label.
  52211. */
  52212. tok = comp_ctx->curr_token.t;
  52213. if (tok == DUK_TOK_FOR || tok == DUK_TOK_DO || tok == DUK_TOK_WHILE ||
  52214. tok == DUK_TOK_SWITCH) {
  52215. DUK_DDD(DUK_DDDPRINT("iteration/switch statement -> add empty label"));
  52216. label_id = duk__stmt_label_site(comp_ctx, label_id);
  52217. duk__add_label(comp_ctx,
  52218. DUK_HTHREAD_STRING_EMPTY_STRING(thr),
  52219. pc_at_entry /*pc_label*/,
  52220. label_id);
  52221. }
  52222. /*
  52223. * Main switch for statement / source element type.
  52224. */
  52225. switch (comp_ctx->curr_token.t) {
  52226. case DUK_TOK_FUNCTION: {
  52227. /*
  52228. * Function declaration, function expression, or (non-standard)
  52229. * function statement.
  52230. *
  52231. * The E5 specification only allows function declarations at
  52232. * the top level (in "source elements"). An ExpressionStatement
  52233. * is explicitly not allowed to begin with a "function" keyword
  52234. * (E5 Section 12.4). Hence any non-error semantics for such
  52235. * non-top-level statements are non-standard. Duktape semantics
  52236. * for function statements are modelled after V8, see
  52237. * test-dev-func-decl-outside-top.js.
  52238. */
  52239. #if defined(DUK_USE_NONSTD_FUNC_STMT)
  52240. /* Lenient: allow function declarations outside top level in
  52241. * non-strict mode but reject them in strict mode.
  52242. */
  52243. if (allow_source_elem || !comp_ctx->curr_func.is_strict)
  52244. #else /* DUK_USE_NONSTD_FUNC_STMT */
  52245. /* Strict: never allow function declarations outside top level. */
  52246. if (allow_source_elem)
  52247. #endif /* DUK_USE_NONSTD_FUNC_STMT */
  52248. {
  52249. /* FunctionDeclaration: not strictly a statement but handled as such.
  52250. *
  52251. * O(depth^2) parse count for inner functions is handled by recording a
  52252. * lexer offset on the first compilation pass, so that the function can
  52253. * be efficiently skipped on the second pass. This is encapsulated into
  52254. * duk__parse_func_like_fnum().
  52255. */
  52256. duk_int_t fnum;
  52257. DUK_DDD(DUK_DDDPRINT("function declaration statement"));
  52258. duk__advance(comp_ctx); /* eat 'function' */
  52259. fnum = duk__parse_func_like_fnum(comp_ctx, 1 /*is_decl*/, 0 /*is_setget*/);
  52260. if (comp_ctx->curr_func.in_scanning) {
  52261. duk_uarridx_t n;
  52262. duk_hstring *h_funcname;
  52263. duk_get_prop_index(ctx, comp_ctx->curr_func.funcs_idx, fnum * 3);
  52264. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_NAME); /* -> [ ... func name ] */
  52265. h_funcname = duk_get_hstring(ctx, -1);
  52266. DUK_ASSERT(h_funcname != NULL);
  52267. DUK_DDD(DUK_DDDPRINT("register function declaration %!O in pass 1, fnum %ld",
  52268. (duk_heaphdr *) h_funcname, (long) fnum));
  52269. n = (duk_uarridx_t) duk_get_length(ctx, comp_ctx->curr_func.decls_idx);
  52270. duk_push_hstring(ctx, h_funcname);
  52271. duk_put_prop_index(ctx, comp_ctx->curr_func.decls_idx, n);
  52272. duk_push_int(ctx, (duk_int_t) (DUK_DECL_TYPE_FUNC + (fnum << 8)));
  52273. duk_put_prop_index(ctx, comp_ctx->curr_func.decls_idx, n + 1);
  52274. duk_pop_n(ctx, 2);
  52275. }
  52276. /* no statement value (unlike function expression) */
  52277. stmt_flags = 0;
  52278. break;
  52279. } else {
  52280. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_FUNC_STMT_NOT_ALLOWED);
  52281. }
  52282. break;
  52283. }
  52284. case DUK_TOK_LCURLY: {
  52285. DUK_DDD(DUK_DDDPRINT("block statement"));
  52286. duk__advance(comp_ctx);
  52287. duk__parse_stmts(comp_ctx, 0 /*allow_source_elem*/, 0 /*expect_eof*/);
  52288. /* the DUK_TOK_RCURLY is eaten by duk__parse_stmts() */
  52289. if (label_id >= 0) {
  52290. duk__patch_jump_here(comp_ctx, pc_at_entry + 1); /* break jump */
  52291. }
  52292. stmt_flags = 0;
  52293. break;
  52294. }
  52295. case DUK_TOK_VAR: {
  52296. DUK_DDD(DUK_DDDPRINT("variable declaration statement"));
  52297. duk__parse_var_stmt(comp_ctx, res);
  52298. stmt_flags = DUK__HAS_TERM;
  52299. break;
  52300. }
  52301. case DUK_TOK_SEMICOLON: {
  52302. /* empty statement with an explicit semicolon */
  52303. DUK_DDD(DUK_DDDPRINT("empty statement"));
  52304. stmt_flags = DUK__HAS_TERM;
  52305. break;
  52306. }
  52307. case DUK_TOK_IF: {
  52308. DUK_DDD(DUK_DDDPRINT("if statement"));
  52309. duk__parse_if_stmt(comp_ctx, res);
  52310. if (label_id >= 0) {
  52311. duk__patch_jump_here(comp_ctx, pc_at_entry + 1); /* break jump */
  52312. }
  52313. stmt_flags = 0;
  52314. break;
  52315. }
  52316. case DUK_TOK_DO: {
  52317. /*
  52318. * Do-while statement is mostly trivial, but there is special
  52319. * handling for automatic semicolon handling (triggered by the
  52320. * DUK__ALLOW_AUTO_SEMI_ALWAYS) flag related to a bug filed at:
  52321. *
  52322. * https://bugs.ecmascript.org/show_bug.cgi?id=8
  52323. *
  52324. * See doc/compiler.txt for details.
  52325. */
  52326. DUK_DDD(DUK_DDDPRINT("do statement"));
  52327. DUK_ASSERT(label_id >= 0);
  52328. duk__update_label_flags(comp_ctx,
  52329. label_id,
  52330. DUK_LABEL_FLAG_ALLOW_BREAK | DUK_LABEL_FLAG_ALLOW_CONTINUE);
  52331. duk__parse_do_stmt(comp_ctx, res, pc_at_entry);
  52332. stmt_flags = DUK__HAS_TERM | DUK__ALLOW_AUTO_SEMI_ALWAYS; /* DUK__ALLOW_AUTO_SEMI_ALWAYS workaround */
  52333. break;
  52334. }
  52335. case DUK_TOK_WHILE: {
  52336. DUK_DDD(DUK_DDDPRINT("while statement"));
  52337. DUK_ASSERT(label_id >= 0);
  52338. duk__update_label_flags(comp_ctx,
  52339. label_id,
  52340. DUK_LABEL_FLAG_ALLOW_BREAK | DUK_LABEL_FLAG_ALLOW_CONTINUE);
  52341. duk__parse_while_stmt(comp_ctx, res, pc_at_entry);
  52342. stmt_flags = 0;
  52343. break;
  52344. }
  52345. case DUK_TOK_FOR: {
  52346. /*
  52347. * For/for-in statement is complicated to parse because
  52348. * determining the statement type (three-part for vs. a
  52349. * for-in) requires potential backtracking.
  52350. *
  52351. * See the helper for the messy stuff.
  52352. */
  52353. DUK_DDD(DUK_DDDPRINT("for/for-in statement"));
  52354. DUK_ASSERT(label_id >= 0);
  52355. duk__update_label_flags(comp_ctx,
  52356. label_id,
  52357. DUK_LABEL_FLAG_ALLOW_BREAK | DUK_LABEL_FLAG_ALLOW_CONTINUE);
  52358. duk__parse_for_stmt(comp_ctx, res, pc_at_entry);
  52359. stmt_flags = 0;
  52360. break;
  52361. }
  52362. case DUK_TOK_CONTINUE:
  52363. case DUK_TOK_BREAK: {
  52364. DUK_DDD(DUK_DDDPRINT("break/continue statement"));
  52365. duk__parse_break_or_continue_stmt(comp_ctx, res);
  52366. stmt_flags = DUK__HAS_TERM | DUK__IS_TERMINAL;
  52367. break;
  52368. }
  52369. case DUK_TOK_RETURN: {
  52370. DUK_DDD(DUK_DDDPRINT("return statement"));
  52371. duk__parse_return_stmt(comp_ctx, res);
  52372. stmt_flags = DUK__HAS_TERM | DUK__IS_TERMINAL;
  52373. break;
  52374. }
  52375. case DUK_TOK_WITH: {
  52376. DUK_DDD(DUK_DDDPRINT("with statement"));
  52377. comp_ctx->curr_func.with_depth++;
  52378. duk__parse_with_stmt(comp_ctx, res);
  52379. if (label_id >= 0) {
  52380. duk__patch_jump_here(comp_ctx, pc_at_entry + 1); /* break jump */
  52381. }
  52382. comp_ctx->curr_func.with_depth--;
  52383. stmt_flags = 0;
  52384. break;
  52385. }
  52386. case DUK_TOK_SWITCH: {
  52387. /*
  52388. * The switch statement is pretty messy to compile.
  52389. * See the helper for details.
  52390. */
  52391. DUK_DDD(DUK_DDDPRINT("switch statement"));
  52392. DUK_ASSERT(label_id >= 0);
  52393. duk__update_label_flags(comp_ctx,
  52394. label_id,
  52395. DUK_LABEL_FLAG_ALLOW_BREAK); /* don't allow continue */
  52396. duk__parse_switch_stmt(comp_ctx, res, pc_at_entry);
  52397. stmt_flags = 0;
  52398. break;
  52399. }
  52400. case DUK_TOK_THROW: {
  52401. DUK_DDD(DUK_DDDPRINT("throw statement"));
  52402. duk__parse_throw_stmt(comp_ctx, res);
  52403. stmt_flags = DUK__HAS_TERM | DUK__IS_TERMINAL;
  52404. break;
  52405. }
  52406. case DUK_TOK_TRY: {
  52407. DUK_DDD(DUK_DDDPRINT("try statement"));
  52408. duk__parse_try_stmt(comp_ctx, res);
  52409. stmt_flags = 0;
  52410. break;
  52411. }
  52412. case DUK_TOK_DEBUGGER: {
  52413. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  52414. DUK_DDD(DUK_DDDPRINT("debugger statement: debugging enabled, emit debugger opcode"));
  52415. duk__emit_extraop_only(comp_ctx, DUK_EXTRAOP_DEBUGGER);
  52416. #else
  52417. DUK_DDD(DUK_DDDPRINT("debugger statement: ignored"));
  52418. #endif
  52419. duk__advance(comp_ctx);
  52420. stmt_flags = DUK__HAS_TERM;
  52421. break;
  52422. }
  52423. default: {
  52424. /*
  52425. * Else, must be one of:
  52426. * - ExpressionStatement, possibly a directive (String)
  52427. * - LabelledStatement (Identifier followed by ':')
  52428. *
  52429. * Expressions beginning with 'function' keyword are covered by a case
  52430. * above (such expressions are not allowed in standard E5 anyway).
  52431. * Also expressions starting with '{' are interpreted as block
  52432. * statements. See E5 Section 12.4.
  52433. *
  52434. * Directive detection is tricky; see E5 Section 14.1 on directive
  52435. * prologue. A directive is an expression statement with a single
  52436. * string literal and an explicit or automatic semicolon. Escape
  52437. * characters are significant and no parens etc are allowed:
  52438. *
  52439. * 'use strict'; // valid 'use strict' directive
  52440. * 'use\u0020strict'; // valid directive, not a 'use strict' directive
  52441. * ('use strict'); // not a valid directive
  52442. *
  52443. * The expression is determined to consist of a single string literal
  52444. * based on duk__expr_nud() and duk__expr_led() call counts. The string literal
  52445. * of a 'use strict' directive is determined to lack any escapes based
  52446. * num_escapes count from the lexer. Note that other directives may be
  52447. * allowed to contain escapes, so a directive with escapes does not
  52448. * terminate a directive prologue.
  52449. *
  52450. * We rely on the fact that the expression parser will not emit any
  52451. * code for a single token expression. However, it will generate an
  52452. * intermediate value which we will then successfully ignore.
  52453. *
  52454. * A similar approach is used for labels.
  52455. */
  52456. duk_bool_t single_token;
  52457. DUK_DDD(DUK_DDDPRINT("expression statement"));
  52458. duk__exprtop(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  52459. single_token = (comp_ctx->curr_func.nud_count == 1 && /* one token */
  52460. comp_ctx->curr_func.led_count == 0); /* no operators */
  52461. if (single_token &&
  52462. comp_ctx->prev_token.t == DUK_TOK_IDENTIFIER &&
  52463. comp_ctx->curr_token.t == DUK_TOK_COLON) {
  52464. /*
  52465. * Detected label
  52466. */
  52467. duk_hstring *h_lab;
  52468. /* expected ival */
  52469. DUK_ASSERT(res->t == DUK_IVAL_VAR);
  52470. DUK_ASSERT(res->x1.t == DUK_ISPEC_VALUE);
  52471. DUK_ASSERT(DUK_TVAL_IS_STRING(duk_get_tval(ctx, res->x1.valstack_idx)));
  52472. h_lab = comp_ctx->prev_token.str1;
  52473. DUK_ASSERT(h_lab != NULL);
  52474. DUK_DDD(DUK_DDDPRINT("explicit label site for label '%!O'",
  52475. (duk_heaphdr *) h_lab));
  52476. duk__advance(comp_ctx); /* eat colon */
  52477. label_id = duk__stmt_label_site(comp_ctx, label_id);
  52478. duk__add_label(comp_ctx,
  52479. h_lab,
  52480. pc_at_entry /*pc_label*/,
  52481. label_id);
  52482. /* a statement following a label cannot be a source element
  52483. * (a function declaration).
  52484. */
  52485. allow_source_elem = 0;
  52486. DUK_DDD(DUK_DDDPRINT("label handled, retry statement parsing"));
  52487. goto retry_parse;
  52488. }
  52489. stmt_flags = 0;
  52490. if (dir_prol_at_entry && /* still in prologue */
  52491. single_token && /* single string token */
  52492. comp_ctx->prev_token.t == DUK_TOK_STRING) {
  52493. /*
  52494. * Detected a directive
  52495. */
  52496. duk_hstring *h_dir;
  52497. /* expected ival */
  52498. DUK_ASSERT(res->t == DUK_IVAL_PLAIN);
  52499. DUK_ASSERT(res->x1.t == DUK_ISPEC_VALUE);
  52500. DUK_ASSERT(DUK_TVAL_IS_STRING(duk_get_tval(ctx, res->x1.valstack_idx)));
  52501. h_dir = comp_ctx->prev_token.str1;
  52502. DUK_ASSERT(h_dir != NULL);
  52503. DUK_DDD(DUK_DDDPRINT("potential directive: %!O", h_dir));
  52504. stmt_flags |= DUK__STILL_PROLOGUE;
  52505. /* Note: escaped characters differentiate directives */
  52506. if (comp_ctx->prev_token.num_escapes > 0) {
  52507. DUK_DDD(DUK_DDDPRINT("directive contains escapes: valid directive "
  52508. "but we ignore such directives"));
  52509. } else {
  52510. /*
  52511. * The length comparisons are present to handle
  52512. * strings like "use strict\u0000foo" as required.
  52513. */
  52514. if (DUK_HSTRING_GET_BYTELEN(h_dir) == 10 &&
  52515. DUK_STRNCMP((const char *) DUK_HSTRING_GET_DATA(h_dir), "use strict", 10) == 0) {
  52516. #if defined(DUK_USE_STRICT_DECL)
  52517. DUK_DDD(DUK_DDDPRINT("use strict directive detected: strict flag %ld -> %ld",
  52518. (long) comp_ctx->curr_func.is_strict, (long) 1));
  52519. comp_ctx->curr_func.is_strict = 1;
  52520. #else
  52521. DUK_DDD(DUK_DDDPRINT("use strict detected but strict declarations disabled, ignoring"));
  52522. #endif
  52523. } else if (DUK_HSTRING_GET_BYTELEN(h_dir) == 14 &&
  52524. DUK_STRNCMP((const char *) DUK_HSTRING_GET_DATA(h_dir), "use duk notail", 14) == 0) {
  52525. DUK_DDD(DUK_DDDPRINT("use duk notail directive detected: notail flag %ld -> %ld",
  52526. (long) comp_ctx->curr_func.is_notail, (long) 1));
  52527. comp_ctx->curr_func.is_notail = 1;
  52528. } else {
  52529. DUK_DD(DUK_DDPRINT("unknown directive: '%!O', ignoring but not terminating "
  52530. "directive prologue", (duk_hobject *) h_dir));
  52531. }
  52532. }
  52533. } else {
  52534. DUK_DDD(DUK_DDDPRINT("non-directive expression statement or no longer in prologue; "
  52535. "prologue terminated if still active"));
  52536. }
  52537. stmt_flags |= DUK__HAS_VAL | DUK__HAS_TERM;
  52538. }
  52539. } /* end switch (tok) */
  52540. /*
  52541. * Statement value handling.
  52542. *
  52543. * Global code and eval code has an implicit return value
  52544. * which comes from the last statement with a value
  52545. * (technically a non-"empty" continuation, which is
  52546. * different from an empty statement).
  52547. *
  52548. * Since we don't know whether a later statement will
  52549. * override the value of the current statement, we need
  52550. * to coerce the statement value to a register allocated
  52551. * for implicit return values. In other cases we need
  52552. * to coerce the statement value to a plain value to get
  52553. * any side effects out (consider e.g. "foo.bar;").
  52554. */
  52555. /* XXX: what about statements which leave a half-cooked value in 'res'
  52556. * but have no stmt value? Any such statements?
  52557. */
  52558. if (stmt_flags & DUK__HAS_VAL) {
  52559. duk_reg_t reg_stmt_value = comp_ctx->curr_func.reg_stmt_value;
  52560. if (reg_stmt_value >= 0) {
  52561. duk__ivalue_toforcedreg(comp_ctx, res, reg_stmt_value);
  52562. } else {
  52563. duk__ivalue_toplain_ignore(comp_ctx, res);
  52564. }
  52565. } else {
  52566. ;
  52567. }
  52568. /*
  52569. * Statement terminator check, including automatic semicolon
  52570. * handling. After this step, 'curr_tok' should be the first
  52571. * token after a possible statement terminator.
  52572. */
  52573. if (stmt_flags & DUK__HAS_TERM) {
  52574. if (comp_ctx->curr_token.t == DUK_TOK_SEMICOLON) {
  52575. DUK_DDD(DUK_DDDPRINT("explicit semicolon terminates statement"));
  52576. duk__advance(comp_ctx);
  52577. } else {
  52578. if (comp_ctx->curr_token.allow_auto_semi) {
  52579. DUK_DDD(DUK_DDDPRINT("automatic semicolon terminates statement"));
  52580. } else if (stmt_flags & DUK__ALLOW_AUTO_SEMI_ALWAYS) {
  52581. /* XXX: make this lenience dependent on flags or strictness? */
  52582. DUK_DDD(DUK_DDDPRINT("automatic semicolon terminates statement (allowed for compatibility "
  52583. "even though no lineterm present before next token)"));
  52584. } else {
  52585. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_UNTERMINATED_STMT);
  52586. }
  52587. }
  52588. } else {
  52589. DUK_DDD(DUK_DDDPRINT("statement has no terminator"));
  52590. }
  52591. /*
  52592. * Directive prologue tracking.
  52593. */
  52594. if (stmt_flags & DUK__STILL_PROLOGUE) {
  52595. DUK_DDD(DUK_DDDPRINT("setting in_directive_prologue"));
  52596. comp_ctx->curr_func.in_directive_prologue = 1;
  52597. }
  52598. /*
  52599. * Cleanups (all statement parsing flows through here).
  52600. *
  52601. * Pop label site and reset labels. Reset 'next temp' to value at
  52602. * entry to reuse temps.
  52603. */
  52604. if (label_id >= 0) {
  52605. duk__emit_extraop_bc(comp_ctx,
  52606. DUK_EXTRAOP_ENDLABEL,
  52607. (duk_regconst_t) label_id);
  52608. }
  52609. DUK__SETTEMP(comp_ctx, temp_at_entry);
  52610. duk__reset_labels_to_length(comp_ctx, labels_len_at_entry);
  52611. /* XXX: return indication of "terminalness" (e.g. a 'throw' is terminal) */
  52612. DUK__RECURSION_DECREASE(comp_ctx, thr);
  52613. }
  52614. #undef DUK__HAS_VAL
  52615. #undef DUK__HAS_TERM
  52616. #undef DUK__ALLOW_AUTO_SEMI_ALWAYS
  52617. /*
  52618. * Parse a statement list.
  52619. *
  52620. * Handles automatic semicolon insertion and implicit return value.
  52621. *
  52622. * Upon entry, 'curr_tok' should contain the first token of the first
  52623. * statement (parsed in the "allow regexp literal" mode). Upon exit,
  52624. * 'curr_tok' contains the token following the statement list terminator
  52625. * (EOF or closing brace).
  52626. */
  52627. DUK_LOCAL void duk__parse_stmts(duk_compiler_ctx *comp_ctx, duk_bool_t allow_source_elem, duk_bool_t expect_eof) {
  52628. duk_hthread *thr = comp_ctx->thr;
  52629. duk_context *ctx = (duk_context *) thr;
  52630. duk_ivalue res_alloc;
  52631. duk_ivalue *res = &res_alloc;
  52632. /* Setup state. Initial ivalue is 'undefined'. */
  52633. duk_require_stack(ctx, DUK__PARSE_STATEMENTS_SLOTS);
  52634. /* XXX: 'res' setup can be moved to function body level; in fact, two 'res'
  52635. * intermediate values suffice for parsing of each function. Nesting is needed
  52636. * for nested functions (which may occur inside expressions).
  52637. */
  52638. DUK_MEMZERO(&res_alloc, sizeof(res_alloc));
  52639. res->t = DUK_IVAL_PLAIN;
  52640. res->x1.t = DUK_ISPEC_VALUE;
  52641. res->x1.valstack_idx = duk_get_top(ctx);
  52642. res->x2.valstack_idx = res->x1.valstack_idx + 1;
  52643. duk_push_undefined(ctx);
  52644. duk_push_undefined(ctx);
  52645. /* Parse statements until a closing token (EOF or '}') is found. */
  52646. for (;;) {
  52647. /* Check whether statement list ends. */
  52648. if (expect_eof) {
  52649. if (comp_ctx->curr_token.t == DUK_TOK_EOF) {
  52650. break;
  52651. }
  52652. } else {
  52653. if (comp_ctx->curr_token.t == DUK_TOK_RCURLY) {
  52654. break;
  52655. }
  52656. }
  52657. /* Check statement type based on the first token type.
  52658. *
  52659. * Note: expression parsing helpers expect 'curr_tok' to
  52660. * contain the first token of the expression upon entry.
  52661. */
  52662. DUK_DDD(DUK_DDDPRINT("TOKEN %ld (non-whitespace, non-comment)", (long) comp_ctx->curr_token.t));
  52663. duk__parse_stmt(comp_ctx, res, allow_source_elem);
  52664. }
  52665. duk__advance(comp_ctx);
  52666. /* Tear down state. */
  52667. duk_pop_2(ctx);
  52668. }
  52669. /*
  52670. * Declaration binding instantiation conceptually happens when calling a
  52671. * function; for us it essentially means that function prologue. The
  52672. * conceptual process is described in E5 Section 10.5.
  52673. *
  52674. * We need to keep track of all encountered identifiers to (1) create an
  52675. * identifier-to-register map ("varmap"); and (2) detect duplicate
  52676. * declarations. Identifiers which are not bound to registers still need
  52677. * to be tracked for detecting duplicates. Currently such identifiers
  52678. * are put into the varmap with a 'null' value, which is later cleaned up.
  52679. *
  52680. * To support functions with a large number of variable and function
  52681. * declarations, registers are not allocated beyond a certain limit;
  52682. * after that limit, variables and functions need slow path access.
  52683. * Arguments are currently always register bound, which imposes a hard
  52684. * (and relatively small) argument count limit.
  52685. *
  52686. * Some bindings in E5 are not configurable (= deletable) and almost all
  52687. * are mutable (writable). Exceptions are:
  52688. *
  52689. * - The 'arguments' binding, established only if no shadowing argument
  52690. * or function declaration exists. We handle 'arguments' creation
  52691. * and binding through an explicit slow path environment record.
  52692. *
  52693. * - The "name" binding for a named function expression. This is also
  52694. * handled through an explicit slow path environment record.
  52695. */
  52696. /* XXX: add support for variables to not be register bound always, to
  52697. * handle cases with a very large number of variables?
  52698. */
  52699. DUK_LOCAL void duk__init_varmap_and_prologue_for_pass2(duk_compiler_ctx *comp_ctx, duk_reg_t *out_stmt_value_reg) {
  52700. duk_hthread *thr = comp_ctx->thr;
  52701. duk_context *ctx = (duk_context *) thr;
  52702. duk_hstring *h_name;
  52703. duk_bool_t configurable_bindings;
  52704. duk_uarridx_t num_args;
  52705. duk_uarridx_t num_decls;
  52706. duk_regconst_t rc_name;
  52707. duk_small_uint_t declvar_flags;
  52708. duk_uarridx_t i;
  52709. #ifdef DUK_USE_ASSERTIONS
  52710. duk_idx_t entry_top;
  52711. #endif
  52712. #ifdef DUK_USE_ASSERTIONS
  52713. entry_top = duk_get_top(ctx);
  52714. #endif
  52715. /*
  52716. * Preliminaries
  52717. */
  52718. configurable_bindings = comp_ctx->curr_func.is_eval;
  52719. DUK_DDD(DUK_DDDPRINT("configurable_bindings=%ld", (long) configurable_bindings));
  52720. /* varmap is already in comp_ctx->curr_func.varmap_idx */
  52721. /*
  52722. * Function formal arguments, always bound to registers
  52723. * (there's no support for shuffling them now).
  52724. */
  52725. num_args = (duk_uarridx_t) duk_get_length(ctx, comp_ctx->curr_func.argnames_idx);
  52726. DUK_DDD(DUK_DDDPRINT("num_args=%ld", (long) num_args));
  52727. /* XXX: check num_args */
  52728. for (i = 0; i < num_args; i++) {
  52729. duk_get_prop_index(ctx, comp_ctx->curr_func.argnames_idx, i);
  52730. h_name = duk_get_hstring(ctx, -1);
  52731. DUK_ASSERT(h_name != NULL);
  52732. if (comp_ctx->curr_func.is_strict) {
  52733. if (duk__hstring_is_eval_or_arguments(comp_ctx, h_name)) {
  52734. DUK_DDD(DUK_DDDPRINT("arg named 'eval' or 'arguments' in strict mode -> SyntaxError"));
  52735. goto error_argname;
  52736. }
  52737. duk_dup_top(ctx);
  52738. if (duk_has_prop(ctx, comp_ctx->curr_func.varmap_idx)) {
  52739. DUK_DDD(DUK_DDDPRINT("duplicate arg name in strict mode -> SyntaxError"));
  52740. goto error_argname;
  52741. }
  52742. /* Ensure argument name is not a reserved word in current
  52743. * (final) strictness. Formal argument parsing may not
  52744. * catch reserved names if strictness changes during
  52745. * parsing.
  52746. *
  52747. * We only need to do this in strict mode because non-strict
  52748. * keyword are always detected in formal argument parsing.
  52749. */
  52750. if (DUK_HSTRING_HAS_STRICT_RESERVED_WORD(h_name)) {
  52751. goto error_argname;
  52752. }
  52753. }
  52754. /* overwrite any previous binding of the same name; the effect is
  52755. * that last argument of a certain name wins.
  52756. */
  52757. /* only functions can have arguments */
  52758. DUK_ASSERT(comp_ctx->curr_func.is_function);
  52759. duk_push_uarridx(ctx, i); /* -> [ ... name index ] */
  52760. duk_put_prop(ctx, comp_ctx->curr_func.varmap_idx); /* -> [ ... ] */
  52761. /* no code needs to be emitted, the regs already have values */
  52762. }
  52763. /* use temp_next for tracking register allocations */
  52764. DUK__SETTEMP_CHECKMAX(comp_ctx, (duk_reg_t) num_args);
  52765. /*
  52766. * After arguments, allocate special registers (like shuffling temps)
  52767. */
  52768. if (out_stmt_value_reg) {
  52769. *out_stmt_value_reg = DUK__ALLOCTEMP(comp_ctx);
  52770. }
  52771. if (comp_ctx->curr_func.needs_shuffle) {
  52772. duk_reg_t shuffle_base = DUK__ALLOCTEMPS(comp_ctx, 3);
  52773. comp_ctx->curr_func.shuffle1 = shuffle_base;
  52774. comp_ctx->curr_func.shuffle2 = shuffle_base + 1;
  52775. comp_ctx->curr_func.shuffle3 = shuffle_base + 2;
  52776. DUK_D(DUK_DPRINT("shuffle registers needed by function, allocated: %ld %ld %ld",
  52777. (long) comp_ctx->curr_func.shuffle1,
  52778. (long) comp_ctx->curr_func.shuffle2,
  52779. (long) comp_ctx->curr_func.shuffle3));
  52780. }
  52781. if (comp_ctx->curr_func.temp_next > 0x100) {
  52782. DUK_D(DUK_DPRINT("not enough 8-bit regs: temp_next=%ld", (long) comp_ctx->curr_func.temp_next));
  52783. goto error_outofregs;
  52784. }
  52785. /*
  52786. * Function declarations
  52787. */
  52788. num_decls = (duk_uarridx_t) duk_get_length(ctx, comp_ctx->curr_func.decls_idx);
  52789. DUK_DDD(DUK_DDDPRINT("num_decls=%ld -> %!T",
  52790. (long) num_decls,
  52791. (duk_tval *) duk_get_tval(ctx, comp_ctx->curr_func.decls_idx)));
  52792. for (i = 0; i < num_decls; i += 2) {
  52793. duk_int_t decl_type;
  52794. duk_int_t fnum;
  52795. duk_get_prop_index(ctx, comp_ctx->curr_func.decls_idx, i + 1); /* decl type */
  52796. decl_type = duk_to_int(ctx, -1);
  52797. fnum = decl_type >> 8; /* XXX: macros */
  52798. decl_type = decl_type & 0xff;
  52799. duk_pop(ctx);
  52800. if (decl_type != DUK_DECL_TYPE_FUNC) {
  52801. continue;
  52802. }
  52803. duk_get_prop_index(ctx, comp_ctx->curr_func.decls_idx, i); /* decl name */
  52804. /* XXX: spilling */
  52805. if (comp_ctx->curr_func.is_function) {
  52806. duk_reg_t reg_bind;
  52807. duk_dup_top(ctx);
  52808. if (duk_has_prop(ctx, comp_ctx->curr_func.varmap_idx)) {
  52809. /* shadowed; update value */
  52810. duk_dup_top(ctx);
  52811. duk_get_prop(ctx, comp_ctx->curr_func.varmap_idx);
  52812. reg_bind = duk_to_int(ctx, -1); /* [ ... name reg_bind ] */
  52813. duk__emit_a_bc(comp_ctx,
  52814. DUK_OP_CLOSURE,
  52815. (duk_regconst_t) reg_bind,
  52816. (duk_regconst_t) fnum);
  52817. } else {
  52818. /* function: always register bound */
  52819. reg_bind = DUK__ALLOCTEMP(comp_ctx);
  52820. duk__emit_a_bc(comp_ctx,
  52821. DUK_OP_CLOSURE,
  52822. (duk_regconst_t) reg_bind,
  52823. (duk_regconst_t) fnum);
  52824. duk_push_int(ctx, (duk_int_t) reg_bind);
  52825. }
  52826. } else {
  52827. /* Function declaration for global/eval code is emitted even
  52828. * for duplicates, because of E5 Section 10.5, step 5.e of
  52829. * E5.1 (special behavior for variable bound to global object).
  52830. *
  52831. * DECLVAR will not re-declare a variable as such, but will
  52832. * update the binding value.
  52833. */
  52834. duk_reg_t reg_temp = DUK__ALLOCTEMP(comp_ctx);
  52835. duk_dup_top(ctx);
  52836. rc_name = duk__getconst(comp_ctx);
  52837. duk_push_null(ctx);
  52838. duk__emit_a_bc(comp_ctx,
  52839. DUK_OP_CLOSURE,
  52840. (duk_regconst_t) reg_temp,
  52841. (duk_regconst_t) fnum);
  52842. declvar_flags = DUK_PROPDESC_FLAG_WRITABLE |
  52843. DUK_PROPDESC_FLAG_ENUMERABLE |
  52844. DUK_BC_DECLVAR_FLAG_FUNC_DECL;
  52845. if (configurable_bindings) {
  52846. declvar_flags |= DUK_PROPDESC_FLAG_CONFIGURABLE;
  52847. }
  52848. duk__emit_a_b_c(comp_ctx,
  52849. DUK_OP_DECLVAR | DUK__EMIT_FLAG_NO_SHUFFLE_A,
  52850. (duk_regconst_t) declvar_flags /*flags*/,
  52851. rc_name /*name*/,
  52852. (duk_regconst_t) reg_temp /*value*/);
  52853. DUK__SETTEMP(comp_ctx, reg_temp); /* forget temp */
  52854. }
  52855. DUK_DDD(DUK_DDDPRINT("function declaration to varmap: %!T -> %!T",
  52856. (duk_tval *) duk_get_tval(ctx, -2),
  52857. (duk_tval *) duk_get_tval(ctx, -1)));
  52858. duk_put_prop(ctx, comp_ctx->curr_func.varmap_idx); /* [ ... name reg/null ] -> [ ... ] */
  52859. }
  52860. /*
  52861. * 'arguments' binding is special; if a shadowing argument or
  52862. * function declaration exists, an arguments object will
  52863. * definitely not be needed, regardless of whether the identifier
  52864. * 'arguments' is referenced inside the function body.
  52865. */
  52866. if (duk_has_prop_stridx(ctx, comp_ctx->curr_func.varmap_idx, DUK_STRIDX_LC_ARGUMENTS)) {
  52867. DUK_DDD(DUK_DDDPRINT("'arguments' is shadowed by argument or function declaration "
  52868. "-> arguments object creation can be skipped"));
  52869. comp_ctx->curr_func.is_arguments_shadowed = 1;
  52870. }
  52871. /*
  52872. * Variable declarations.
  52873. *
  52874. * Unlike function declarations, variable declaration values don't get
  52875. * assigned on entry. If a binding of the same name already exists, just
  52876. * ignore it silently.
  52877. */
  52878. for (i = 0; i < num_decls; i += 2) {
  52879. duk_int_t decl_type;
  52880. duk_get_prop_index(ctx, comp_ctx->curr_func.decls_idx, i + 1); /* decl type */
  52881. decl_type = duk_to_int(ctx, -1);
  52882. decl_type = decl_type & 0xff;
  52883. duk_pop(ctx);
  52884. if (decl_type != DUK_DECL_TYPE_VAR) {
  52885. continue;
  52886. }
  52887. duk_get_prop_index(ctx, comp_ctx->curr_func.decls_idx, i); /* decl name */
  52888. if (duk_has_prop(ctx, comp_ctx->curr_func.varmap_idx)) {
  52889. /* shadowed, ignore */
  52890. } else {
  52891. duk_get_prop_index(ctx, comp_ctx->curr_func.decls_idx, i); /* decl name */
  52892. h_name = duk_get_hstring(ctx, -1);
  52893. DUK_ASSERT(h_name != NULL);
  52894. if (h_name == DUK_HTHREAD_STRING_LC_ARGUMENTS(thr) &&
  52895. !comp_ctx->curr_func.is_arguments_shadowed) {
  52896. /* E5 Section steps 7-8 */
  52897. DUK_DDD(DUK_DDDPRINT("'arguments' not shadowed by a function declaration, "
  52898. "but appears as a variable declaration -> treat as "
  52899. "a no-op for variable declaration purposes"));
  52900. duk_pop(ctx);
  52901. continue;
  52902. }
  52903. /* XXX: spilling */
  52904. if (comp_ctx->curr_func.is_function) {
  52905. duk_reg_t reg_bind = DUK__ALLOCTEMP(comp_ctx);
  52906. /* no need to init reg, it will be undefined on entry */
  52907. duk_push_int(ctx, (duk_int_t) reg_bind);
  52908. } else {
  52909. duk_dup_top(ctx);
  52910. rc_name = duk__getconst(comp_ctx);
  52911. duk_push_null(ctx);
  52912. declvar_flags = DUK_PROPDESC_FLAG_WRITABLE |
  52913. DUK_PROPDESC_FLAG_ENUMERABLE |
  52914. DUK_BC_DECLVAR_FLAG_UNDEF_VALUE;
  52915. if (configurable_bindings) {
  52916. declvar_flags |= DUK_PROPDESC_FLAG_CONFIGURABLE;
  52917. }
  52918. duk__emit_a_b_c(comp_ctx,
  52919. DUK_OP_DECLVAR | DUK__EMIT_FLAG_NO_SHUFFLE_A,
  52920. (duk_regconst_t) declvar_flags /*flags*/,
  52921. rc_name /*name*/,
  52922. (duk_regconst_t) 0 /*value*/);
  52923. }
  52924. duk_put_prop(ctx, comp_ctx->curr_func.varmap_idx); /* [ ... name reg/null ] -> [ ... ] */
  52925. }
  52926. }
  52927. /*
  52928. * Wrap up
  52929. */
  52930. DUK_DDD(DUK_DDDPRINT("varmap: %!T, is_arguments_shadowed=%ld",
  52931. (duk_tval *) duk_get_tval(ctx, comp_ctx->curr_func.varmap_idx),
  52932. (long) comp_ctx->curr_func.is_arguments_shadowed));
  52933. DUK_ASSERT_TOP(ctx, entry_top);
  52934. return;
  52935. error_outofregs:
  52936. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_REG_LIMIT);
  52937. DUK_UNREACHABLE();
  52938. return;
  52939. error_argname:
  52940. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_ARG_NAME);
  52941. DUK_UNREACHABLE();
  52942. return;
  52943. }
  52944. /*
  52945. * Parse a function-body-like expression (FunctionBody or Program
  52946. * in E5 grammar) using a two-pass parse. The productions appear
  52947. * in the following contexts:
  52948. *
  52949. * - function expression
  52950. * - function statement
  52951. * - function declaration
  52952. * - getter in object literal
  52953. * - setter in object literal
  52954. * - global code
  52955. * - eval code
  52956. * - Function constructor body
  52957. *
  52958. * This function only parses the statement list of the body; the argument
  52959. * list and possible function name must be initialized by the caller.
  52960. * For instance, for Function constructor, the argument names are originally
  52961. * on the value stack. The parsing of statements ends either at an EOF or
  52962. * a closing brace; this is controlled by an input flag.
  52963. *
  52964. * Note that there are many differences affecting parsing and even code
  52965. * generation:
  52966. *
  52967. * - Global and eval code have an implicit return value generated
  52968. * by the last statement; function code does not
  52969. *
  52970. * - Global code, eval code, and Function constructor body end in
  52971. * an EOF, other bodies in a closing brace ('}')
  52972. *
  52973. * Upon entry, 'curr_tok' is ignored and the function will pull in the
  52974. * first token on its own. Upon exit, 'curr_tok' is the terminating
  52975. * token (EOF or closing brace).
  52976. */
  52977. 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) {
  52978. duk_compiler_func *func = &comp_ctx->curr_func;
  52979. duk_hthread *thr = comp_ctx->thr;
  52980. duk_context *ctx = (duk_context *) thr;
  52981. duk_reg_t reg_stmt_value = -1;
  52982. duk_lexer_point lex_pt;
  52983. duk_reg_t temp_first;
  52984. duk_small_int_t compile_round = 1;
  52985. DUK_ASSERT(comp_ctx != NULL);
  52986. DUK_ASSERT(func != NULL);
  52987. DUK__RECURSION_INCREASE(comp_ctx, thr);
  52988. duk_require_stack(ctx, DUK__FUNCTION_BODY_REQUIRE_SLOTS);
  52989. /*
  52990. * Store lexer position for a later rewind
  52991. */
  52992. DUK_LEXER_GETPOINT(&comp_ctx->lex, &lex_pt);
  52993. /*
  52994. * Program code (global and eval code) has an implicit return value
  52995. * from the last statement value (e.g. eval("1; 2+3;") returns 3).
  52996. * This is not the case with functions. If implicit statement return
  52997. * value is requested, all statements are coerced to a register
  52998. * allocated here, and used in the implicit return statement below.
  52999. */
  53000. /* XXX: this is pointless here because pass 1 is throw-away */
  53001. if (implicit_return_value) {
  53002. reg_stmt_value = DUK__ALLOCTEMP(comp_ctx);
  53003. /* If an implicit return value is needed by caller, it must be
  53004. * initialized to 'undefined' because we don't know whether any
  53005. * non-empty (where "empty" is a continuation type, and different
  53006. * from an empty statement) statements will be executed.
  53007. *
  53008. * However, since 1st pass is a throwaway one, no need to emit
  53009. * it here.
  53010. */
  53011. #if 0
  53012. duk__emit_extraop_bc(comp_ctx,
  53013. DUK_EXTRAOP_LDUNDEF,
  53014. 0);
  53015. #endif
  53016. }
  53017. /*
  53018. * First pass.
  53019. *
  53020. * Gather variable/function declarations needed for second pass.
  53021. * Code generated is dummy and discarded.
  53022. */
  53023. func->in_directive_prologue = 1;
  53024. func->in_scanning = 1;
  53025. func->may_direct_eval = 0;
  53026. func->id_access_arguments = 0;
  53027. func->id_access_slow = 0;
  53028. func->reg_stmt_value = reg_stmt_value;
  53029. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  53030. func->min_line = DUK_INT_MAX;
  53031. func->max_line = 0;
  53032. #endif
  53033. /* duk__parse_stmts() expects curr_tok to be set; parse in "allow regexp literal" mode with current strictness */
  53034. if (expect_token >= 0) {
  53035. /* Eating a left curly; regexp mode is allowed by left curly
  53036. * based on duk__token_lbp[] automatically.
  53037. */
  53038. DUK_ASSERT(expect_token == DUK_TOK_LCURLY);
  53039. duk__update_lineinfo_currtoken(comp_ctx);
  53040. duk__advance_expect(comp_ctx, expect_token);
  53041. } else {
  53042. /* Need to set curr_token.t because lexing regexp mode depends on current
  53043. * token type. Zero value causes "allow regexp" mode.
  53044. */
  53045. comp_ctx->curr_token.t = 0;
  53046. duk__advance(comp_ctx);
  53047. }
  53048. DUK_DDD(DUK_DDDPRINT("begin 1st pass"));
  53049. duk__parse_stmts(comp_ctx,
  53050. 1, /* allow source elements */
  53051. expect_eof); /* expect EOF instead of } */
  53052. DUK_DDD(DUK_DDDPRINT("end 1st pass"));
  53053. /*
  53054. * Second (and possibly third) pass.
  53055. *
  53056. * Generate actual code. In most cases the need for shuffle
  53057. * registers is detected during pass 1, but in some corner cases
  53058. * we'll only detect it during pass 2 and a third pass is then
  53059. * needed (see GH-115).
  53060. */
  53061. for (;;) {
  53062. duk_bool_t needs_shuffle_before = comp_ctx->curr_func.needs_shuffle;
  53063. compile_round++;
  53064. /*
  53065. * Rewind lexer.
  53066. *
  53067. * duk__parse_stmts() expects curr_tok to be set; parse in "allow regexp
  53068. * literal" mode with current strictness.
  53069. *
  53070. * curr_token line number info should be initialized for pass 2 before
  53071. * generating prologue, to ensure prologue bytecode gets nice line numbers.
  53072. */
  53073. DUK_DDD(DUK_DDDPRINT("rewind lexer"));
  53074. DUK_LEXER_SETPOINT(&comp_ctx->lex, &lex_pt);
  53075. comp_ctx->curr_token.t = 0; /* this is needed for regexp mode */
  53076. comp_ctx->curr_token.start_line = 0; /* needed for line number tracking (becomes prev_token.start_line) */
  53077. duk__advance(comp_ctx);
  53078. /*
  53079. * Reset function state and perform register allocation, which creates
  53080. * 'varmap' for second pass. Function prologue for variable declarations,
  53081. * binding value initializations etc is emitted as a by-product.
  53082. *
  53083. * Strict mode restrictions for duplicate and invalid argument
  53084. * names are checked here now that we know whether the function
  53085. * is actually strict. See: test-dev-strict-mode-boundary.js.
  53086. *
  53087. * Inner functions are compiled during pass 1 and are not reset.
  53088. */
  53089. duk__reset_func_for_pass2(comp_ctx);
  53090. func->in_directive_prologue = 1;
  53091. func->in_scanning = 0;
  53092. /* must be able to emit code, alloc consts, etc. */
  53093. duk__init_varmap_and_prologue_for_pass2(comp_ctx,
  53094. (implicit_return_value ? &reg_stmt_value : NULL));
  53095. func->reg_stmt_value = reg_stmt_value;
  53096. temp_first = DUK__GETTEMP(comp_ctx);
  53097. func->temp_first = temp_first;
  53098. func->temp_next = temp_first;
  53099. func->stmt_next = 0;
  53100. func->label_next = 0;
  53101. /* XXX: init or assert catch depth etc -- all values */
  53102. func->id_access_arguments = 0;
  53103. func->id_access_slow = 0;
  53104. /*
  53105. * Check function name validity now that we know strictness.
  53106. * This only applies to function declarations and expressions,
  53107. * not setter/getter name.
  53108. *
  53109. * See: test-dev-strict-mode-boundary.js
  53110. */
  53111. if (func->is_function && !func->is_setget && func->h_name != NULL) {
  53112. if (func->is_strict) {
  53113. if (duk__hstring_is_eval_or_arguments(comp_ctx, func->h_name)) {
  53114. DUK_DDD(DUK_DDDPRINT("func name is 'eval' or 'arguments' in strict mode"));
  53115. goto error_funcname;
  53116. }
  53117. if (DUK_HSTRING_HAS_STRICT_RESERVED_WORD(func->h_name)) {
  53118. DUK_DDD(DUK_DDDPRINT("func name is a reserved word in strict mode"));
  53119. goto error_funcname;
  53120. }
  53121. } else {
  53122. if (DUK_HSTRING_HAS_RESERVED_WORD(func->h_name) &&
  53123. !DUK_HSTRING_HAS_STRICT_RESERVED_WORD(func->h_name)) {
  53124. DUK_DDD(DUK_DDDPRINT("func name is a reserved word in non-strict mode"));
  53125. goto error_funcname;
  53126. }
  53127. }
  53128. }
  53129. /*
  53130. * Second pass parsing.
  53131. */
  53132. if (implicit_return_value) {
  53133. /* Default implicit return value. */
  53134. duk__emit_extraop_bc(comp_ctx,
  53135. DUK_EXTRAOP_LDUNDEF,
  53136. 0);
  53137. }
  53138. DUK_DDD(DUK_DDDPRINT("begin 2nd pass"));
  53139. duk__parse_stmts(comp_ctx,
  53140. 1, /* allow source elements */
  53141. expect_eof); /* expect EOF instead of } */
  53142. DUK_DDD(DUK_DDDPRINT("end 2nd pass"));
  53143. duk__update_lineinfo_currtoken(comp_ctx);
  53144. if (needs_shuffle_before == comp_ctx->curr_func.needs_shuffle) {
  53145. /* Shuffle decision not changed. */
  53146. break;
  53147. }
  53148. if (compile_round >= 3) {
  53149. /* Should never happen but avoid infinite loop just in case. */
  53150. DUK_D(DUK_DPRINT("more than 3 compile passes needed, should never happen"));
  53151. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR);
  53152. }
  53153. DUK_D(DUK_DPRINT("need additional round to compile function, round now %d", (int) compile_round));
  53154. }
  53155. /*
  53156. * Emit a final RETURN.
  53157. *
  53158. * It would be nice to avoid emitting an unnecessary "return" opcode
  53159. * if the current PC is not reachable. However, this cannot be reliably
  53160. * detected; even if the previous instruction is an unconditional jump,
  53161. * there may be a previous jump which jumps to current PC (which is the
  53162. * case for iteration and conditional statements, for instance).
  53163. */
  53164. /* XXX: request a "last statement is terminal" from duk__parse_stmt() and duk__parse_stmts();
  53165. * we could avoid the last RETURN if we could ensure there is no way to get here
  53166. * (directly or via a jump)
  53167. */
  53168. DUK_ASSERT(comp_ctx->curr_func.catch_depth == 0); /* fast returns are always OK here */
  53169. if (reg_stmt_value >= 0) {
  53170. duk__emit_a_b(comp_ctx,
  53171. DUK_OP_RETURN | DUK__EMIT_FLAG_NO_SHUFFLE_A,
  53172. (duk_regconst_t) (DUK_BC_RETURN_FLAG_HAVE_RETVAL | DUK_BC_RETURN_FLAG_FAST) /*flags*/,
  53173. (duk_regconst_t) reg_stmt_value /*reg*/);
  53174. } else {
  53175. duk__emit_a_b(comp_ctx,
  53176. DUK_OP_RETURN | DUK__EMIT_FLAG_NO_SHUFFLE_A,
  53177. (duk_regconst_t) DUK_BC_RETURN_FLAG_FAST /*flags*/,
  53178. (duk_regconst_t) 0 /*reg(ignored)*/);
  53179. }
  53180. /*
  53181. * Peephole optimize JUMP chains.
  53182. */
  53183. duk__peephole_optimize_bytecode(comp_ctx);
  53184. /*
  53185. * comp_ctx->curr_func is now ready to be converted into an actual
  53186. * function template.
  53187. */
  53188. DUK__RECURSION_DECREASE(comp_ctx, thr);
  53189. return;
  53190. error_funcname:
  53191. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_FUNC_NAME);
  53192. }
  53193. /*
  53194. * Parse a function-like expression:
  53195. *
  53196. * - function expression
  53197. * - function declaration
  53198. * - function statement (non-standard)
  53199. * - setter/getter
  53200. *
  53201. * Adds the function to comp_ctx->curr_func function table and returns the
  53202. * function number.
  53203. *
  53204. * On entry, curr_token points to:
  53205. *
  53206. * - the token after 'function' for function expression/declaration/statement
  53207. * - the token after 'set' or 'get' for setter/getter
  53208. */
  53209. /* Parse formals. */
  53210. DUK_LOCAL void duk__parse_func_formals(duk_compiler_ctx *comp_ctx) {
  53211. duk_hthread *thr = comp_ctx->thr;
  53212. duk_context *ctx = (duk_context *) thr;
  53213. duk_bool_t first = 1;
  53214. duk_uarridx_t n;
  53215. for (;;) {
  53216. if (comp_ctx->curr_token.t == DUK_TOK_RPAREN) {
  53217. break;
  53218. }
  53219. if (first) {
  53220. /* no comma */
  53221. first = 0;
  53222. } else {
  53223. duk__advance_expect(comp_ctx, DUK_TOK_COMMA);
  53224. }
  53225. /* Note: when parsing a formal list in non-strict context, e.g.
  53226. * "implements" is parsed as an identifier. When the function is
  53227. * later detected to be strict, the argument list must be rechecked
  53228. * against a larger set of reserved words (that of strict mode).
  53229. * This is handled by duk__parse_func_body(). Here we recognize
  53230. * whatever tokens are considered reserved in current strictness
  53231. * (which is not always enough).
  53232. */
  53233. if (comp_ctx->curr_token.t != DUK_TOK_IDENTIFIER) {
  53234. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, "expected identifier");
  53235. }
  53236. DUK_ASSERT(comp_ctx->curr_token.t == DUK_TOK_IDENTIFIER);
  53237. DUK_ASSERT(comp_ctx->curr_token.str1 != NULL);
  53238. DUK_DDD(DUK_DDDPRINT("formal argument: %!O",
  53239. (duk_heaphdr *) comp_ctx->curr_token.str1));
  53240. /* XXX: append primitive */
  53241. duk_push_hstring(ctx, comp_ctx->curr_token.str1);
  53242. n = (duk_uarridx_t) duk_get_length(ctx, comp_ctx->curr_func.argnames_idx);
  53243. duk_put_prop_index(ctx, comp_ctx->curr_func.argnames_idx, n);
  53244. duk__advance(comp_ctx); /* eat identifier */
  53245. }
  53246. }
  53247. /* Parse a function-like expression, assuming that 'comp_ctx->curr_func' is
  53248. * correctly set up. Assumes that curr_token is just after 'function' (or
  53249. * 'set'/'get' etc).
  53250. */
  53251. DUK_LOCAL void duk__parse_func_like_raw(duk_compiler_ctx *comp_ctx, duk_bool_t is_decl, duk_bool_t is_setget) {
  53252. duk_hthread *thr = comp_ctx->thr;
  53253. duk_context *ctx = (duk_context *) thr;
  53254. DUK_ASSERT(comp_ctx->curr_func.num_formals == 0);
  53255. DUK_ASSERT(comp_ctx->curr_func.is_function == 1);
  53256. DUK_ASSERT(comp_ctx->curr_func.is_eval == 0);
  53257. DUK_ASSERT(comp_ctx->curr_func.is_global == 0);
  53258. DUK_ASSERT(comp_ctx->curr_func.is_setget == is_setget);
  53259. DUK_ASSERT(comp_ctx->curr_func.is_decl == is_decl);
  53260. duk__update_lineinfo_currtoken(comp_ctx);
  53261. /*
  53262. * Function name (if any)
  53263. *
  53264. * We don't check for prohibited names here, because we don't
  53265. * yet know whether the function will be strict. Function body
  53266. * parsing handles this retroactively.
  53267. *
  53268. * For function expressions and declarations function name must
  53269. * be an Identifer (excludes reserved words). For setter/getter
  53270. * it is a PropertyName which allows reserved words and also
  53271. * strings and numbers (e.g. "{ get 1() { ... } }").
  53272. */
  53273. if (is_setget) {
  53274. /* PropertyName -> IdentifierName | StringLiteral | NumericLiteral */
  53275. if (comp_ctx->curr_token.t_nores == DUK_TOK_IDENTIFIER ||
  53276. comp_ctx->curr_token.t == DUK_TOK_STRING) {
  53277. duk_push_hstring(ctx, comp_ctx->curr_token.str1); /* keep in valstack */
  53278. } else if (comp_ctx->curr_token.t == DUK_TOK_NUMBER) {
  53279. duk_push_number(ctx, comp_ctx->curr_token.num);
  53280. duk_to_string(ctx, -1);
  53281. } else {
  53282. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_GETSET_NAME);
  53283. }
  53284. comp_ctx->curr_func.h_name = duk_get_hstring(ctx, -1); /* borrowed reference */
  53285. DUK_ASSERT(comp_ctx->curr_func.h_name != NULL);
  53286. duk__advance(comp_ctx);
  53287. } else {
  53288. /* Function name is an Identifier (not IdentifierName), but we get
  53289. * the raw name (not recognizing keywords) here and perform the name
  53290. * checks only after pass 1.
  53291. */
  53292. if (comp_ctx->curr_token.t_nores == DUK_TOK_IDENTIFIER) {
  53293. duk_push_hstring(ctx, comp_ctx->curr_token.str1); /* keep in valstack */
  53294. comp_ctx->curr_func.h_name = duk_get_hstring(ctx, -1); /* borrowed reference */
  53295. DUK_ASSERT(comp_ctx->curr_func.h_name != NULL);
  53296. duk__advance(comp_ctx);
  53297. } else {
  53298. /* valstack will be unbalanced, which is OK */
  53299. DUK_ASSERT(!is_setget);
  53300. if (is_decl) {
  53301. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_FUNC_NAME_REQUIRED);
  53302. }
  53303. }
  53304. }
  53305. DUK_DDD(DUK_DDDPRINT("function name: %!O",
  53306. (duk_heaphdr *) comp_ctx->curr_func.h_name));
  53307. /*
  53308. * Formal argument list
  53309. *
  53310. * We don't check for prohibited names or for duplicate argument
  53311. * names here, becase we don't yet know whether the function will
  53312. * be strict. Function body parsing handles this retroactively.
  53313. */
  53314. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  53315. duk__parse_func_formals(comp_ctx);
  53316. DUK_ASSERT(comp_ctx->curr_token.t == DUK_TOK_RPAREN);
  53317. duk__advance(comp_ctx);
  53318. /*
  53319. * Parse function body
  53320. */
  53321. duk__parse_func_body(comp_ctx,
  53322. 0, /* expect_eof */
  53323. 0, /* implicit_return_value */
  53324. DUK_TOK_LCURLY); /* expect_token */
  53325. /*
  53326. * Convert duk_compiler_func to a function template and add it
  53327. * to the parent function table.
  53328. */
  53329. duk__convert_to_func_template(comp_ctx, is_setget /*force_no_namebind*/); /* -> [ ... func ] */
  53330. }
  53331. /* Parse an inner function, adding the function template to the current function's
  53332. * function table. Return a function number to be used by the outer function.
  53333. *
  53334. * Avoiding O(depth^2) inner function parsing is handled here. On the first pass,
  53335. * compile and register the function normally into the 'funcs' array, also recording
  53336. * a lexer point (offset/line) to the closing brace of the function. On the second
  53337. * pass, skip the function and return the same 'fnum' as on the first pass by using
  53338. * a running counter.
  53339. *
  53340. * An unfortunate side effect of this is that when parsing the inner function, almost
  53341. * nothing is known of the outer function, i.e. the inner function's scope. We don't
  53342. * need that information at the moment, but it would allow some optimizations if it
  53343. * were used.
  53344. */
  53345. 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) {
  53346. duk_hthread *thr = comp_ctx->thr;
  53347. duk_context *ctx = (duk_context *) thr;
  53348. duk_compiler_func old_func;
  53349. duk_idx_t entry_top;
  53350. duk_int_t fnum;
  53351. /*
  53352. * On second pass, skip the function.
  53353. */
  53354. if (!comp_ctx->curr_func.in_scanning) {
  53355. duk_lexer_point lex_pt;
  53356. fnum = comp_ctx->curr_func.fnum_next++;
  53357. duk_get_prop_index(ctx, comp_ctx->curr_func.funcs_idx, (duk_uarridx_t) (fnum * 3 + 1));
  53358. lex_pt.offset = duk_to_int(ctx, -1);
  53359. duk_pop(ctx);
  53360. duk_get_prop_index(ctx, comp_ctx->curr_func.funcs_idx, (duk_uarridx_t) (fnum * 3 + 2));
  53361. lex_pt.line = duk_to_int(ctx, -1);
  53362. duk_pop(ctx);
  53363. DUK_DDD(DUK_DDDPRINT("second pass of an inner func, skip the function, reparse closing brace; lex offset=%ld, line=%ld",
  53364. (long) lex_pt.offset, (long) lex_pt.line));
  53365. DUK_LEXER_SETPOINT(&comp_ctx->lex, &lex_pt);
  53366. comp_ctx->curr_token.t = 0; /* this is needed for regexp mode */
  53367. comp_ctx->curr_token.start_line = 0; /* needed for line number tracking (becomes prev_token.start_line) */
  53368. duk__advance(comp_ctx);
  53369. duk__advance_expect(comp_ctx, DUK_TOK_RCURLY);
  53370. return fnum;
  53371. }
  53372. /*
  53373. * On first pass, perform actual parsing. Remember valstack top on entry
  53374. * to restore it later, and switch to using a new function in comp_ctx.
  53375. */
  53376. entry_top = duk_get_top(ctx);
  53377. DUK_DDD(DUK_DDDPRINT("before func: entry_top=%ld, curr_tok.start_offset=%ld",
  53378. (long) entry_top, (long) comp_ctx->curr_token.start_offset));
  53379. DUK_MEMCPY(&old_func, &comp_ctx->curr_func, sizeof(duk_compiler_func));
  53380. DUK_MEMZERO(&comp_ctx->curr_func, sizeof(duk_compiler_func));
  53381. duk__init_func_valstack_slots(comp_ctx);
  53382. DUK_ASSERT(comp_ctx->curr_func.num_formals == 0);
  53383. /* inherit initial strictness from parent */
  53384. comp_ctx->curr_func.is_strict = old_func.is_strict;
  53385. DUK_ASSERT(comp_ctx->curr_func.is_notail == 0);
  53386. comp_ctx->curr_func.is_function = 1;
  53387. DUK_ASSERT(comp_ctx->curr_func.is_eval == 0);
  53388. DUK_ASSERT(comp_ctx->curr_func.is_global == 0);
  53389. comp_ctx->curr_func.is_setget = is_setget;
  53390. comp_ctx->curr_func.is_decl = is_decl;
  53391. /*
  53392. * Parse inner function
  53393. */
  53394. duk__parse_func_like_raw(comp_ctx, is_decl, is_setget); /* pushes function template */
  53395. /* prev_token.start_offset points to the closing brace here; when skipping
  53396. * we're going to reparse the closing brace to ensure semicolon insertion
  53397. * etc work as expected.
  53398. */
  53399. DUK_DDD(DUK_DDDPRINT("after func: prev_tok.start_offset=%ld, curr_tok.start_offset=%ld",
  53400. (long) comp_ctx->prev_token.start_offset, (long) comp_ctx->curr_token.start_offset));
  53401. DUK_ASSERT(comp_ctx->lex.input[comp_ctx->prev_token.start_offset] == (duk_uint8_t) DUK_ASC_RCURLY);
  53402. /* XXX: append primitive */
  53403. DUK_ASSERT(duk_get_length(ctx, old_func.funcs_idx) == (duk_size_t) (old_func.fnum_next * 3));
  53404. fnum = old_func.fnum_next++;
  53405. if (fnum > DUK__MAX_FUNCS) {
  53406. DUK_ERROR(comp_ctx->thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_FUNC_LIMIT);
  53407. }
  53408. /* array writes autoincrement length */
  53409. (void) duk_put_prop_index(ctx, old_func.funcs_idx, (duk_uarridx_t) (fnum * 3));
  53410. duk_push_size_t(ctx, comp_ctx->prev_token.start_offset);
  53411. (void) duk_put_prop_index(ctx, old_func.funcs_idx, (duk_uarridx_t) (fnum * 3 + 1));
  53412. duk_push_int(ctx, comp_ctx->prev_token.start_line);
  53413. (void) duk_put_prop_index(ctx, old_func.funcs_idx, (duk_uarridx_t) (fnum * 3 + 2));
  53414. /*
  53415. * Cleanup: restore original function, restore valstack state.
  53416. */
  53417. DUK_MEMCPY((void *) &comp_ctx->curr_func, (void *) &old_func, sizeof(duk_compiler_func));
  53418. duk_set_top(ctx, entry_top);
  53419. DUK_ASSERT_TOP(ctx, entry_top);
  53420. return fnum;
  53421. }
  53422. /*
  53423. * Compile input string into an executable function template without
  53424. * arguments.
  53425. *
  53426. * The string is parsed as the "Program" production of Ecmascript E5.
  53427. * Compilation context can be either global code or eval code (see E5
  53428. * Sections 14 and 15.1.2.1).
  53429. *
  53430. * Input stack: [ ... filename ]
  53431. * Output stack: [ ... func_template ]
  53432. */
  53433. /* XXX: source code property */
  53434. DUK_LOCAL duk_ret_t duk__js_compile_raw(duk_context *ctx) {
  53435. duk_hthread *thr = (duk_hthread *) ctx;
  53436. duk_hstring *h_filename;
  53437. duk__compiler_stkstate *comp_stk;
  53438. duk_compiler_ctx *comp_ctx;
  53439. duk_lexer_point *lex_pt;
  53440. duk_compiler_func *func;
  53441. duk_idx_t entry_top;
  53442. duk_bool_t is_strict;
  53443. duk_bool_t is_eval;
  53444. duk_bool_t is_funcexpr;
  53445. duk_small_uint_t flags;
  53446. DUK_ASSERT(thr != NULL);
  53447. /*
  53448. * Arguments check
  53449. */
  53450. entry_top = duk_get_top(ctx);
  53451. DUK_ASSERT(entry_top >= 2);
  53452. comp_stk = (duk__compiler_stkstate *) duk_require_pointer(ctx, -1);
  53453. comp_ctx = &comp_stk->comp_ctx_alloc;
  53454. lex_pt = &comp_stk->lex_pt_alloc;
  53455. DUK_ASSERT(comp_ctx != NULL);
  53456. DUK_ASSERT(lex_pt != NULL);
  53457. flags = comp_stk->flags;
  53458. is_eval = (flags & DUK_JS_COMPILE_FLAG_EVAL ? 1 : 0);
  53459. is_strict = (flags & DUK_JS_COMPILE_FLAG_STRICT ? 1 : 0);
  53460. is_funcexpr = (flags & DUK_JS_COMPILE_FLAG_FUNCEXPR ? 1 : 0);
  53461. h_filename = duk_get_hstring(ctx, -2); /* may be undefined */
  53462. /*
  53463. * Init compiler and lexer contexts
  53464. */
  53465. func = &comp_ctx->curr_func;
  53466. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  53467. comp_ctx->thr = NULL;
  53468. comp_ctx->h_filename = NULL;
  53469. comp_ctx->prev_token.str1 = NULL;
  53470. comp_ctx->prev_token.str2 = NULL;
  53471. comp_ctx->curr_token.str1 = NULL;
  53472. comp_ctx->curr_token.str2 = NULL;
  53473. #endif
  53474. duk_require_stack(ctx, DUK__COMPILE_ENTRY_SLOTS);
  53475. duk_push_dynamic_buffer(ctx, 0); /* entry_top + 0 */
  53476. duk_push_undefined(ctx); /* entry_top + 1 */
  53477. duk_push_undefined(ctx); /* entry_top + 2 */
  53478. duk_push_undefined(ctx); /* entry_top + 3 */
  53479. duk_push_undefined(ctx); /* entry_top + 4 */
  53480. comp_ctx->thr = thr;
  53481. comp_ctx->h_filename = h_filename;
  53482. comp_ctx->tok11_idx = entry_top + 1;
  53483. comp_ctx->tok12_idx = entry_top + 2;
  53484. comp_ctx->tok21_idx = entry_top + 3;
  53485. comp_ctx->tok22_idx = entry_top + 4;
  53486. comp_ctx->recursion_limit = DUK_COMPILER_RECURSION_LIMIT;
  53487. /* comp_ctx->lex has been pre-initialized by caller: it has been
  53488. * zeroed and input/input_length has been set.
  53489. */
  53490. comp_ctx->lex.thr = thr;
  53491. /* comp_ctx->lex.input and comp_ctx->lex.input_length filled by caller */
  53492. comp_ctx->lex.slot1_idx = comp_ctx->tok11_idx;
  53493. comp_ctx->lex.slot2_idx = comp_ctx->tok12_idx;
  53494. comp_ctx->lex.buf_idx = entry_top + 0;
  53495. comp_ctx->lex.buf = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, entry_top + 0);
  53496. DUK_ASSERT(comp_ctx->lex.buf != NULL);
  53497. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(comp_ctx->lex.buf));
  53498. comp_ctx->lex.token_limit = DUK_COMPILER_TOKEN_LIMIT;
  53499. lex_pt->offset = 0;
  53500. lex_pt->line = 1;
  53501. DUK_LEXER_SETPOINT(&comp_ctx->lex, lex_pt); /* fills window */
  53502. comp_ctx->curr_token.start_line = 0; /* needed for line number tracking (becomes prev_token.start_line) */
  53503. /*
  53504. * Initialize function state for a zero-argument function
  53505. */
  53506. duk__init_func_valstack_slots(comp_ctx);
  53507. DUK_ASSERT(func->num_formals == 0);
  53508. if (is_funcexpr) {
  53509. /* Name will be filled from function expression, not by caller.
  53510. * This case is used by Function constructor and duk_compile()
  53511. * API with the DUK_COMPILE_FUNCTION option.
  53512. */
  53513. DUK_ASSERT(func->h_name == NULL);
  53514. } else {
  53515. duk_push_hstring_stridx(ctx, (is_eval ? DUK_STRIDX_EVAL :
  53516. DUK_STRIDX_GLOBAL));
  53517. func->h_name = duk_get_hstring(ctx, -1);
  53518. }
  53519. /*
  53520. * Parse a function body or a function-like expression, depending
  53521. * on flags.
  53522. */
  53523. func->is_strict = is_strict;
  53524. func->is_setget = 0;
  53525. func->is_decl = 0;
  53526. if (is_funcexpr) {
  53527. func->is_function = 1;
  53528. func->is_eval = 0;
  53529. func->is_global = 0;
  53530. duk__advance(comp_ctx); /* init 'curr_token' */
  53531. duk__advance_expect(comp_ctx, DUK_TOK_FUNCTION);
  53532. (void) duk__parse_func_like_raw(comp_ctx,
  53533. 0, /* is_decl */
  53534. 0); /* is_setget */
  53535. } else {
  53536. func->is_function = 0;
  53537. func->is_eval = is_eval;
  53538. func->is_global = !is_eval;
  53539. duk__parse_func_body(comp_ctx,
  53540. 1, /* expect_eof */
  53541. 1, /* implicit_return_value */
  53542. -1); /* expect_token */
  53543. }
  53544. /*
  53545. * Convert duk_compiler_func to a function template
  53546. */
  53547. duk__convert_to_func_template(comp_ctx, 0 /*force_no_namebind*/);
  53548. /*
  53549. * Wrapping duk_safe_call() will mangle the stack, just return stack top
  53550. */
  53551. /* [ ... filename (temps) func ] */
  53552. return 1;
  53553. }
  53554. 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) {
  53555. duk_context *ctx = (duk_context *) thr;
  53556. duk__compiler_stkstate comp_stk;
  53557. duk_compiler_ctx *prev_ctx;
  53558. duk_ret_t safe_rc;
  53559. /* XXX: this illustrates that a C catchpoint implemented using duk_safe_call()
  53560. * is a bit heavy at the moment. The wrapper compiles to ~180 bytes on x64.
  53561. * Alternatives would be nice.
  53562. */
  53563. DUK_ASSERT(thr != NULL);
  53564. DUK_ASSERT(src_buffer != NULL);
  53565. /* preinitialize lexer state partially */
  53566. DUK_MEMZERO(&comp_stk, sizeof(comp_stk));
  53567. comp_stk.flags = flags;
  53568. DUK_LEXER_INITCTX(&comp_stk.comp_ctx_alloc.lex);
  53569. comp_stk.comp_ctx_alloc.lex.input = src_buffer;
  53570. comp_stk.comp_ctx_alloc.lex.input_length = src_length;
  53571. duk_push_pointer(ctx, (void *) &comp_stk);
  53572. /* [ ... filename &comp_stk ] */
  53573. prev_ctx = thr->compile_ctx;
  53574. thr->compile_ctx = &comp_stk.comp_ctx_alloc; /* for duk_error_augment.c */
  53575. safe_rc = duk_safe_call(ctx, duk__js_compile_raw, 2 /*nargs*/, 1 /*nret*/);
  53576. thr->compile_ctx = prev_ctx;
  53577. if (safe_rc != DUK_EXEC_SUCCESS) {
  53578. /* Append a "(line NNN)" to the "message" property of any
  53579. * error thrown during compilation. Usually compilation
  53580. * errors are SyntaxErrors but they can also be out-of-memory
  53581. * errors and the like.
  53582. *
  53583. * Source file/line are added to tracedata directly by
  53584. * duk_error_augment.c based on thr->compile_ctx.
  53585. */
  53586. /* [ ... error ] */
  53587. DUK_DDD(DUK_DDDPRINT("compile error, before adding line info: %!T",
  53588. (duk_tval *) duk_get_tval(ctx, -1)));
  53589. if (duk_is_object(ctx, -1)) {
  53590. /* XXX: Now that fileName and lineNumber are set, this is
  53591. * unnecessary. Remove in Duktape 1.3.0?
  53592. */
  53593. if (duk_get_prop_stridx(ctx, -1, DUK_STRIDX_MESSAGE)) {
  53594. duk_push_sprintf(ctx, " (line %ld)", (long) comp_stk.comp_ctx_alloc.curr_token.start_line);
  53595. duk_concat(ctx, 2);
  53596. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE);
  53597. } else {
  53598. duk_pop(ctx);
  53599. }
  53600. }
  53601. DUK_DDD(DUK_DDDPRINT("compile error, after adding line info: %!T",
  53602. (duk_tval *) duk_get_tval(ctx, -1)));
  53603. duk_throw(ctx);
  53604. }
  53605. /* [ ... template ] */
  53606. }
  53607. #line 1 "duk_js_executor.c"
  53608. /*
  53609. * Ecmascript bytecode executor.
  53610. */
  53611. /* include removed: duk_internal.h */
  53612. /*
  53613. * Local forward declarations
  53614. */
  53615. DUK_LOCAL_DECL void duk__reconfig_valstack(duk_hthread *thr, duk_size_t act_idx, duk_small_uint_t retval_count);
  53616. /*
  53617. * Arithmetic, binary, and logical helpers.
  53618. *
  53619. * Note: there is no opcode for logical AND or logical OR; this is on
  53620. * purpose, because the evalution order semantics for them make such
  53621. * opcodes pretty pointless (short circuiting means they are most
  53622. * comfortably implemented as jumps). However, a logical NOT opcode
  53623. * is useful.
  53624. *
  53625. * Note: careful with duk_tval pointers here: they are potentially
  53626. * invalidated by any DECREF and almost any API call.
  53627. */
  53628. DUK_LOCAL duk_double_t duk__compute_mod(duk_double_t d1, duk_double_t d2) {
  53629. /*
  53630. * Ecmascript modulus ('%') does not match IEEE 754 "remainder"
  53631. * operation (implemented by remainder() in C99) but does seem
  53632. * to match ANSI C fmod().
  53633. *
  53634. * Compare E5 Section 11.5.3 and "man fmod".
  53635. */
  53636. return (duk_double_t) DUK_FMOD((double) d1, (double) d2);
  53637. }
  53638. 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) {
  53639. /*
  53640. * Addition operator is different from other arithmetic
  53641. * operations in that it also provides string concatenation.
  53642. * Hence it is implemented separately.
  53643. *
  53644. * There is a fast path for number addition. Other cases go
  53645. * through potentially multiple coercions as described in the
  53646. * E5 specification. It may be possible to reduce the number
  53647. * of coercions, but this must be done carefully to preserve
  53648. * the exact semantics.
  53649. *
  53650. * E5 Section 11.6.1.
  53651. *
  53652. * Custom types also have special behavior implemented here.
  53653. */
  53654. duk_context *ctx = (duk_context *) thr;
  53655. duk_double_union du;
  53656. DUK_ASSERT(thr != NULL);
  53657. DUK_ASSERT(ctx != NULL);
  53658. DUK_ASSERT(tv_x != NULL); /* may be reg or const */
  53659. DUK_ASSERT(tv_y != NULL); /* may be reg or const */
  53660. DUK_ASSERT_DISABLE(idx_z >= 0); /* unsigned */
  53661. DUK_ASSERT((duk_uint_t) idx_z < (duk_uint_t) duk_get_top(ctx));
  53662. /*
  53663. * Fast paths
  53664. */
  53665. #if defined(DUK_USE_FASTINT)
  53666. if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) {
  53667. duk_int64_t v1, v2, v3;
  53668. duk_int32_t v3_hi;
  53669. duk_tval tv_tmp;
  53670. duk_tval *tv_z;
  53671. /* Input values are signed 48-bit so we can detect overflow
  53672. * reliably from high bits or just a comparison.
  53673. */
  53674. v1 = DUK_TVAL_GET_FASTINT(tv_x);
  53675. v2 = DUK_TVAL_GET_FASTINT(tv_y);
  53676. v3 = v1 + v2;
  53677. v3_hi = (duk_int32_t) (v3 >> 32);
  53678. if (DUK_LIKELY(v3_hi >= -0x8000LL && v3_hi <= 0x7fffLL)) {
  53679. tv_z = thr->valstack_bottom + idx_z;
  53680. DUK_TVAL_SET_TVAL(&tv_tmp, tv_z);
  53681. DUK_TVAL_SET_FASTINT(tv_z, v3);
  53682. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z)); /* no need to incref */
  53683. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  53684. return;
  53685. } else {
  53686. /* overflow, fall through */
  53687. ;
  53688. }
  53689. }
  53690. #endif /* DUK_USE_FASTINT */
  53691. if (DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y)) {
  53692. duk_tval tv_tmp;
  53693. duk_tval *tv_z;
  53694. du.d = DUK_TVAL_GET_NUMBER(tv_x) + DUK_TVAL_GET_NUMBER(tv_y);
  53695. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  53696. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  53697. tv_z = thr->valstack_bottom + idx_z;
  53698. DUK_TVAL_SET_TVAL(&tv_tmp, tv_z);
  53699. DUK_TVAL_SET_NUMBER(tv_z, du.d);
  53700. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z)); /* no need to incref */
  53701. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  53702. return;
  53703. }
  53704. /*
  53705. * Slow path: potentially requires function calls for coercion
  53706. */
  53707. duk_push_tval(ctx, tv_x);
  53708. duk_push_tval(ctx, tv_y);
  53709. duk_to_primitive(ctx, -2, DUK_HINT_NONE); /* side effects -> don't use tv_x, tv_y after */
  53710. duk_to_primitive(ctx, -1, DUK_HINT_NONE);
  53711. /* As a first approximation, buffer values are coerced to strings
  53712. * for addition. This means that adding two buffers currently
  53713. * results in a string.
  53714. */
  53715. if (duk_check_type_mask(ctx, -2, DUK_TYPE_MASK_STRING | DUK_TYPE_MASK_BUFFER) ||
  53716. duk_check_type_mask(ctx, -1, DUK_TYPE_MASK_STRING | DUK_TYPE_MASK_BUFFER)) {
  53717. duk_to_string(ctx, -2);
  53718. duk_to_string(ctx, -1);
  53719. duk_concat(ctx, 2); /* [... s1 s2] -> [... s1+s2] */
  53720. duk_replace(ctx, (duk_idx_t) idx_z); /* side effects */
  53721. } else {
  53722. duk_double_t d1, d2;
  53723. d1 = duk_to_number(ctx, -2);
  53724. d2 = duk_to_number(ctx, -1);
  53725. DUK_ASSERT(duk_is_number(ctx, -2));
  53726. DUK_ASSERT(duk_is_number(ctx, -1));
  53727. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d1);
  53728. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2);
  53729. du.d = d1 + d2;
  53730. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  53731. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  53732. duk_pop_2(ctx);
  53733. duk_push_number(ctx, du.d);
  53734. duk_replace(ctx, (duk_idx_t) idx_z); /* side effects */
  53735. }
  53736. }
  53737. 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) {
  53738. /*
  53739. * Arithmetic operations other than '+' have number-only semantics
  53740. * and are implemented here. The separate switch-case here means a
  53741. * "double dispatch" of the arithmetic opcode, but saves code space.
  53742. *
  53743. * E5 Sections 11.5, 11.5.1, 11.5.2, 11.5.3, 11.6, 11.6.1, 11.6.2, 11.6.3.
  53744. */
  53745. duk_context *ctx = (duk_context *) thr;
  53746. duk_tval tv_tmp;
  53747. duk_tval *tv_z;
  53748. duk_double_t d1, d2;
  53749. duk_double_union du;
  53750. DUK_ASSERT(thr != NULL);
  53751. DUK_ASSERT(ctx != NULL);
  53752. DUK_ASSERT(tv_x != NULL); /* may be reg or const */
  53753. DUK_ASSERT(tv_y != NULL); /* may be reg or const */
  53754. DUK_ASSERT_DISABLE(idx_z >= 0); /* unsigned */
  53755. DUK_ASSERT((duk_uint_t) idx_z < (duk_uint_t) duk_get_top(ctx));
  53756. #if defined(DUK_USE_FASTINT)
  53757. if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) {
  53758. duk_int64_t v1, v2, v3;
  53759. duk_int32_t v3_hi;
  53760. v1 = DUK_TVAL_GET_FASTINT(tv_x);
  53761. v2 = DUK_TVAL_GET_FASTINT(tv_y);
  53762. switch (opcode) {
  53763. case DUK_OP_SUB: {
  53764. v3 = v1 - v2;
  53765. break;
  53766. }
  53767. case DUK_OP_MUL: {
  53768. /* Must ensure result is 64-bit (no overflow); a
  53769. * simple and sufficient fast path is to allow only
  53770. * 32-bit inputs. Avoid zero inputs to avoid
  53771. * negative zero issues (-1 * 0 = -0, for instance).
  53772. */
  53773. if (v1 >= -0x80000000LL && v1 <= 0x7fffffffLL && v1 != 0 &&
  53774. v2 >= -0x80000000LL && v2 <= 0x7fffffffLL && v2 != 0) {
  53775. v3 = v1 * v2;
  53776. } else {
  53777. goto skip_fastint;
  53778. }
  53779. break;
  53780. }
  53781. case DUK_OP_DIV: {
  53782. /* Don't allow a zero divisor. Fast path check by
  53783. * "verifying" with multiplication. Also avoid zero
  53784. * dividend to avoid negative zero issues (0 / -1 = -0
  53785. * for instance).
  53786. */
  53787. if (v1 == 0 || v2 == 0) {
  53788. goto skip_fastint;
  53789. }
  53790. v3 = v1 / v2;
  53791. if (v3 * v2 != v1) {
  53792. goto skip_fastint;
  53793. }
  53794. break;
  53795. }
  53796. case DUK_OP_MOD: {
  53797. /* Don't allow a zero divisor. Restrict both v1 and
  53798. * v2 to positive values to avoid compiler specific
  53799. * behavior.
  53800. */
  53801. if (v1 < 1 || v2 < 1) {
  53802. goto skip_fastint;
  53803. }
  53804. v3 = v1 % v2;
  53805. DUK_ASSERT(v3 >= 0);
  53806. DUK_ASSERT(v3 < v2);
  53807. DUK_ASSERT(v1 - (v1 / v2) * v2 == v3);
  53808. break;
  53809. }
  53810. default: {
  53811. DUK_UNREACHABLE();
  53812. goto skip_fastint;
  53813. }
  53814. }
  53815. v3_hi = (duk_int32_t) (v3 >> 32);
  53816. if (DUK_LIKELY(v3_hi >= -0x8000LL && v3_hi <= 0x7fffLL)) {
  53817. tv_z = thr->valstack_bottom + idx_z;
  53818. DUK_TVAL_SET_TVAL(&tv_tmp, tv_z);
  53819. DUK_TVAL_SET_FASTINT(tv_z, v3);
  53820. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z)); /* no need to incref */
  53821. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  53822. return;
  53823. }
  53824. /* fall through if overflow etc */
  53825. }
  53826. skip_fastint:
  53827. #endif /* DUK_USE_FASTINT */
  53828. if (DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y)) {
  53829. /* fast path */
  53830. d1 = DUK_TVAL_GET_NUMBER(tv_x);
  53831. d2 = DUK_TVAL_GET_NUMBER(tv_y);
  53832. } else {
  53833. duk_push_tval(ctx, tv_x);
  53834. duk_push_tval(ctx, tv_y);
  53835. d1 = duk_to_number(ctx, -2); /* side effects */
  53836. d2 = duk_to_number(ctx, -1);
  53837. DUK_ASSERT(duk_is_number(ctx, -2));
  53838. DUK_ASSERT(duk_is_number(ctx, -1));
  53839. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d1);
  53840. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2);
  53841. duk_pop_2(ctx);
  53842. }
  53843. switch (opcode) {
  53844. case DUK_OP_SUB: {
  53845. du.d = d1 - d2;
  53846. break;
  53847. }
  53848. case DUK_OP_MUL: {
  53849. du.d = d1 * d2;
  53850. break;
  53851. }
  53852. case DUK_OP_DIV: {
  53853. du.d = d1 / d2;
  53854. break;
  53855. }
  53856. case DUK_OP_MOD: {
  53857. du.d = duk__compute_mod(d1, d2);
  53858. break;
  53859. }
  53860. default: {
  53861. DUK_UNREACHABLE();
  53862. du.d = DUK_DOUBLE_NAN; /* should not happen */
  53863. break;
  53864. }
  53865. }
  53866. /* important to use normalized NaN with 8-byte tagged types */
  53867. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  53868. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  53869. tv_z = thr->valstack_bottom + idx_z;
  53870. DUK_TVAL_SET_TVAL(&tv_tmp, tv_z);
  53871. DUK_TVAL_SET_NUMBER(tv_z, du.d);
  53872. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z)); /* no need to incref */
  53873. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  53874. }
  53875. 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) {
  53876. /*
  53877. * Binary bitwise operations use different coercions (ToInt32, ToUint32)
  53878. * depending on the operation. We coerce the arguments first using
  53879. * ToInt32(), and then cast to an 32-bit value if necessary. Note that
  53880. * such casts must be correct even if there is no native 32-bit type
  53881. * (e.g., duk_int32_t and duk_uint32_t are 64-bit).
  53882. *
  53883. * E5 Sections 11.10, 11.7.1, 11.7.2, 11.7.3
  53884. */
  53885. duk_context *ctx = (duk_context *) thr;
  53886. duk_tval tv_tmp;
  53887. duk_tval *tv_z;
  53888. duk_int32_t i1, i2, i3;
  53889. duk_uint32_t u1, u2, u3;
  53890. #if defined(DUK_USE_FASTINT)
  53891. duk_int64_t fi3;
  53892. #else
  53893. duk_double_t d3;
  53894. #endif
  53895. DUK_ASSERT(thr != NULL);
  53896. DUK_ASSERT(ctx != NULL);
  53897. DUK_ASSERT(tv_x != NULL); /* may be reg or const */
  53898. DUK_ASSERT(tv_y != NULL); /* may be reg or const */
  53899. DUK_ASSERT_DISABLE(idx_z >= 0); /* unsigned */
  53900. DUK_ASSERT((duk_uint_t) idx_z < (duk_uint_t) duk_get_top(ctx));
  53901. #if defined(DUK_USE_FASTINT)
  53902. if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) {
  53903. i1 = (duk_int32_t) DUK_TVAL_GET_FASTINT_I32(tv_x);
  53904. i2 = (duk_int32_t) DUK_TVAL_GET_FASTINT_I32(tv_y);
  53905. }
  53906. else
  53907. #endif /* DUK_USE_FASTINT */
  53908. {
  53909. duk_push_tval(ctx, tv_x);
  53910. duk_push_tval(ctx, tv_y);
  53911. i1 = duk_to_int32(ctx, -2);
  53912. i2 = duk_to_int32(ctx, -1);
  53913. duk_pop_2(ctx);
  53914. }
  53915. switch (opcode) {
  53916. case DUK_OP_BAND: {
  53917. i3 = i1 & i2;
  53918. break;
  53919. }
  53920. case DUK_OP_BOR: {
  53921. i3 = i1 | i2;
  53922. break;
  53923. }
  53924. case DUK_OP_BXOR: {
  53925. i3 = i1 ^ i2;
  53926. break;
  53927. }
  53928. case DUK_OP_BASL: {
  53929. /* Signed shift, named "arithmetic" (asl) because the result
  53930. * is signed, e.g. 4294967295 << 1 -> -2. Note that result
  53931. * must be masked.
  53932. */
  53933. u2 = ((duk_uint32_t) i2) & 0xffffffffUL;
  53934. i3 = i1 << (u2 & 0x1f); /* E5 Section 11.7.1, steps 7 and 8 */
  53935. i3 = i3 & ((duk_int32_t) 0xffffffffUL); /* Note: left shift, should mask */
  53936. break;
  53937. }
  53938. case DUK_OP_BASR: {
  53939. /* signed shift */
  53940. u2 = ((duk_uint32_t) i2) & 0xffffffffUL;
  53941. i3 = i1 >> (u2 & 0x1f); /* E5 Section 11.7.2, steps 7 and 8 */
  53942. break;
  53943. }
  53944. case DUK_OP_BLSR: {
  53945. /* unsigned shift */
  53946. u1 = ((duk_uint32_t) i1) & 0xffffffffUL;
  53947. u2 = ((duk_uint32_t) i2) & 0xffffffffUL;
  53948. /* special result value handling */
  53949. u3 = u1 >> (u2 & 0x1f); /* E5 Section 11.7.2, steps 7 and 8 */
  53950. #if defined(DUK_USE_FASTINT)
  53951. fi3 = (duk_int64_t) u3;
  53952. goto fastint_result_set;
  53953. #else
  53954. d3 = (duk_double_t) u3;
  53955. goto result_set;
  53956. #endif
  53957. }
  53958. default: {
  53959. DUK_UNREACHABLE();
  53960. i3 = 0; /* should not happen */
  53961. break;
  53962. }
  53963. }
  53964. #if defined(DUK_USE_FASTINT)
  53965. /* Result is always fastint compatible. */
  53966. /* XXX: set 32-bit result (but must handle signed and unsigned) */
  53967. fi3 = (duk_int64_t) i3;
  53968. fastint_result_set:
  53969. tv_z = thr->valstack_bottom + idx_z;
  53970. DUK_TVAL_SET_TVAL(&tv_tmp, tv_z);
  53971. DUK_TVAL_SET_FASTINT(tv_z, fi3);
  53972. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z)); /* no need to incref */
  53973. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  53974. #else
  53975. d3 = (duk_double_t) i3;
  53976. result_set:
  53977. DUK_ASSERT(!DUK_ISNAN(d3)); /* 'd3' is never NaN, so no need to normalize */
  53978. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d3); /* always normalized */
  53979. tv_z = thr->valstack_bottom + idx_z;
  53980. DUK_TVAL_SET_TVAL(&tv_tmp, tv_z);
  53981. DUK_TVAL_SET_NUMBER(tv_z, d3);
  53982. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z)); /* no need to incref */
  53983. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  53984. #endif
  53985. }
  53986. /* In-place unary operation. */
  53987. 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) {
  53988. /*
  53989. * Arithmetic operations other than '+' have number-only semantics
  53990. * and are implemented here. The separate switch-case here means a
  53991. * "double dispatch" of the arithmetic opcode, but saves code space.
  53992. *
  53993. * E5 Sections 11.5, 11.5.1, 11.5.2, 11.5.3, 11.6, 11.6.1, 11.6.2, 11.6.3.
  53994. */
  53995. duk_context *ctx = (duk_context *) thr;
  53996. duk_double_t d1;
  53997. duk_double_union du;
  53998. DUK_ASSERT(thr != NULL);
  53999. DUK_ASSERT(ctx != NULL);
  54000. DUK_ASSERT(opcode == DUK_EXTRAOP_UNM || opcode == DUK_EXTRAOP_UNP);
  54001. #if defined(DUK_USE_FASTINT)
  54002. if (DUK_TVAL_IS_FASTINT(tv_x)) {
  54003. duk_int64_t v1, v2;
  54004. v1 = DUK_TVAL_GET_FASTINT(tv_x);
  54005. if (opcode == DUK_EXTRAOP_UNM) {
  54006. /* The smallest fastint is no longer 48-bit when
  54007. * negated. Positive zero becames negative zero
  54008. * (cannot be represented) when negated.
  54009. */
  54010. if (DUK_LIKELY(v1 != DUK_FASTINT_MIN && v1 != 0)) {
  54011. v2 = -v1;
  54012. DUK_TVAL_SET_FASTINT(tv_x, v2); /* no refcount changes */
  54013. return;
  54014. }
  54015. } else {
  54016. /* ToNumber() for a fastint is a no-op. */
  54017. DUK_ASSERT(opcode == DUK_EXTRAOP_UNP);
  54018. return;
  54019. }
  54020. /* fall through if overflow etc */
  54021. }
  54022. #endif /* DUK_USE_FASTINT */
  54023. if (!DUK_TVAL_IS_NUMBER(tv_x)) {
  54024. duk_to_number(ctx, idx_x); /* side effects, perform in-place */
  54025. tv_x = duk_get_tval(ctx, idx_x);
  54026. DUK_ASSERT(tv_x != NULL);
  54027. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_x));
  54028. }
  54029. d1 = DUK_TVAL_GET_NUMBER(tv_x);
  54030. if (opcode == DUK_EXTRAOP_UNM) {
  54031. du.d = -d1;
  54032. } else {
  54033. /* ToNumber() for a double is a no-op. */
  54034. DUK_ASSERT(opcode == DUK_EXTRAOP_UNP);
  54035. du.d = d1;
  54036. }
  54037. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du); /* mandatory if du.d is a NaN */
  54038. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  54039. #if defined(DUK_USE_FASTINT)
  54040. /* Unary plus is used to force a fastint check, so must include
  54041. * downgrade check.
  54042. */
  54043. DUK_TVAL_SET_NUMBER_CHKFAST(tv_x, du.d); /* no refcount changes */
  54044. #else
  54045. DUK_TVAL_SET_NUMBER(tv_x, du.d); /* no refcount changes */
  54046. #endif
  54047. }
  54048. DUK_LOCAL void duk__vm_bitwise_not(duk_hthread *thr, duk_tval *tv_x, duk_small_uint_fast_t idx_z) {
  54049. /*
  54050. * E5 Section 11.4.8
  54051. */
  54052. duk_context *ctx = (duk_context *) thr;
  54053. duk_tval tv_tmp;
  54054. duk_tval *tv_z;
  54055. duk_int32_t i1, i2;
  54056. #if !defined(DUK_USE_FASTINT)
  54057. duk_double_t d2;
  54058. #endif
  54059. DUK_ASSERT(thr != NULL);
  54060. DUK_ASSERT(ctx != NULL);
  54061. DUK_ASSERT(tv_x != NULL); /* may be reg or const */
  54062. DUK_ASSERT_DISABLE(idx_z >= 0);
  54063. DUK_ASSERT((duk_uint_t) idx_z < (duk_uint_t) duk_get_top(ctx));
  54064. #if defined(DUK_USE_FASTINT)
  54065. if (DUK_TVAL_IS_FASTINT(tv_x)) {
  54066. i1 = (duk_int32_t) DUK_TVAL_GET_FASTINT_I32(tv_x);
  54067. }
  54068. else
  54069. #endif /* DUK_USE_FASTINT */
  54070. {
  54071. duk_push_tval(ctx, tv_x);
  54072. i1 = duk_to_int32(ctx, -1);
  54073. duk_pop(ctx);
  54074. }
  54075. i2 = ~i1;
  54076. #if defined(DUK_USE_FASTINT)
  54077. /* Result is always fastint compatible. */
  54078. tv_z = thr->valstack_bottom + idx_z;
  54079. DUK_TVAL_SET_TVAL(&tv_tmp, tv_z);
  54080. DUK_TVAL_SET_FASTINT_I32(tv_z, i2);
  54081. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z)); /* no need to incref */
  54082. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  54083. #else
  54084. d2 = (duk_double_t) i2;
  54085. DUK_ASSERT(!DUK_ISNAN(d2)); /* 'val' is never NaN, so no need to normalize */
  54086. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2); /* always normalized */
  54087. tv_z = thr->valstack_bottom + idx_z;
  54088. DUK_TVAL_SET_TVAL(&tv_tmp, tv_z);
  54089. DUK_TVAL_SET_NUMBER(tv_z, d2);
  54090. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z)); /* no need to incref */
  54091. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  54092. #endif
  54093. }
  54094. DUK_LOCAL void duk__vm_logical_not(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_z) {
  54095. /*
  54096. * E5 Section 11.4.9
  54097. */
  54098. duk_tval tv_tmp;
  54099. duk_bool_t res;
  54100. DUK_ASSERT(thr != NULL);
  54101. DUK_ASSERT(tv_x != NULL); /* may be reg or const */
  54102. DUK_ASSERT(tv_z != NULL); /* reg */
  54103. DUK_UNREF(thr); /* w/o refcounts */
  54104. /* ToBoolean() does not require any operations with side effects so
  54105. * we can do it efficiently. For footprint it would be better to use
  54106. * duk_js_toboolean() and then push+replace to the result slot.
  54107. */
  54108. res = duk_js_toboolean(tv_x); /* does not modify tv_x */
  54109. DUK_ASSERT(res == 0 || res == 1);
  54110. res ^= 1;
  54111. DUK_TVAL_SET_TVAL(&tv_tmp, tv_z);
  54112. DUK_TVAL_SET_BOOLEAN(tv_z, res); /* no need to incref */
  54113. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  54114. }
  54115. /*
  54116. * Longjmp handler for the bytecode executor (and a bunch of static
  54117. * helpers for it).
  54118. *
  54119. * Any type of longjmp() can be caught here, including intra-function
  54120. * longjmp()s like 'break', 'continue', (slow) 'return', 'yield', etc.
  54121. *
  54122. * Error policy: should not ordinarily throw errors. Errors thrown
  54123. * will bubble outwards.
  54124. *
  54125. * Returns:
  54126. * 0 restart execution
  54127. * 1 bytecode executor finished
  54128. * 2 rethrow longjmp
  54129. */
  54130. /* XXX: duk_api operations for cross-thread reg manipulation? */
  54131. /* XXX: post-condition: value stack must be correct; for ecmascript functions, clamped to 'nregs' */
  54132. #define DUK__LONGJMP_RESTART 0 /* state updated, restart bytecode execution */
  54133. #define DUK__LONGJMP_FINISHED 1 /* exit bytecode executor with return value */
  54134. #define DUK__LONGJMP_RETHROW 2 /* exit bytecode executor by rethrowing an error to caller */
  54135. /* only called when act_idx points to an Ecmascript function */
  54136. DUK_LOCAL void duk__reconfig_valstack(duk_hthread *thr, duk_size_t act_idx, duk_small_uint_t retval_count) {
  54137. duk_hcompiledfunction *h_func;
  54138. DUK_ASSERT(thr != NULL);
  54139. DUK_ASSERT_DISABLE(act_idx >= 0); /* unsigned */
  54140. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + act_idx) != NULL);
  54141. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + act_idx)));
  54142. DUK_ASSERT_DISABLE(thr->callstack[act_idx].idx_retval >= 0); /* unsigned */
  54143. thr->valstack_bottom = thr->valstack + thr->callstack[act_idx].idx_bottom;
  54144. /* clamp so that retval is at the top (retval_count == 1) or register just before
  54145. * intended retval is at the top (retval_count == 0, happens e.g. with 'finally').
  54146. */
  54147. duk_set_top((duk_context *) thr,
  54148. (duk_idx_t) (thr->callstack[act_idx].idx_retval -
  54149. thr->callstack[act_idx].idx_bottom +
  54150. retval_count));
  54151. /*
  54152. * When returning to an Ecmascript function, extend the valstack
  54153. * top to 'nregs' always.
  54154. */
  54155. h_func = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(thr->callstack + act_idx);
  54156. (void) duk_valstack_resize_raw((duk_context *) thr,
  54157. (thr->valstack_bottom - thr->valstack) + /* bottom of current func */
  54158. h_func->nregs + /* reg count */
  54159. DUK_VALSTACK_INTERNAL_EXTRA, /* + spare */
  54160. DUK_VSRESIZE_FLAG_SHRINK | /* flags */
  54161. 0 /* no compact */ |
  54162. DUK_VSRESIZE_FLAG_THROW);
  54163. duk_set_top((duk_context *) thr, h_func->nregs);
  54164. }
  54165. DUK_LOCAL void duk__handle_catch_or_finally(duk_hthread *thr, duk_size_t cat_idx, duk_bool_t is_finally) {
  54166. duk_context *ctx = (duk_context *) thr;
  54167. duk_tval tv_tmp;
  54168. duk_tval *tv1;
  54169. DUK_DDD(DUK_DDDPRINT("handling catch/finally, cat_idx=%ld, is_finally=%ld",
  54170. (long) cat_idx, (long) is_finally));
  54171. /*
  54172. * Set caught value and longjmp type to catcher regs.
  54173. */
  54174. DUK_DDD(DUK_DDDPRINT("writing catch registers: idx_base=%ld -> %!T, idx_base+1=%ld -> %!T",
  54175. (long) thr->catchstack[cat_idx].idx_base,
  54176. (duk_tval *) &thr->heap->lj.value1,
  54177. (long) (thr->catchstack[cat_idx].idx_base + 1),
  54178. (duk_tval *) &thr->heap->lj.value2));
  54179. tv1 = thr->valstack + thr->catchstack[cat_idx].idx_base;
  54180. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  54181. DUK_TVAL_SET_TVAL(tv1, &thr->heap->lj.value1);
  54182. DUK_TVAL_INCREF(thr, tv1);
  54183. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  54184. tv1 = thr->valstack + thr->catchstack[cat_idx].idx_base + 1;
  54185. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  54186. DUK_TVAL_SET_NUMBER(tv1, (duk_double_t) thr->heap->lj.type); /* XXX: set int */
  54187. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv1)); /* no need to incref */
  54188. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  54189. /*
  54190. * Unwind catchstack and callstack.
  54191. *
  54192. * The 'cat_idx' catcher is always kept, even when executing finally.
  54193. */
  54194. duk_hthread_catchstack_unwind(thr, cat_idx + 1);
  54195. duk_hthread_callstack_unwind(thr, thr->catchstack[cat_idx].callstack_index + 1);
  54196. /*
  54197. * Reconfigure valstack to 'nregs' (this is always the case for
  54198. * Ecmascript functions).
  54199. */
  54200. DUK_ASSERT(thr->callstack_top >= 1);
  54201. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1) != NULL);
  54202. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1)));
  54203. thr->valstack_bottom = thr->valstack + (thr->callstack + thr->callstack_top - 1)->idx_bottom;
  54204. duk_set_top((duk_context *) thr, ((duk_hcompiledfunction *) DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1))->nregs);
  54205. /*
  54206. * Reset PC: resume execution from catch or finally jump slot.
  54207. */
  54208. (thr->callstack + thr->callstack_top - 1)->pc =
  54209. thr->catchstack[cat_idx].pc_base + (is_finally ? 1 : 0);
  54210. /*
  54211. * If entering a 'catch' block which requires an automatic
  54212. * catch variable binding, create the lexical environment.
  54213. *
  54214. * The binding is mutable (= writable) but not deletable.
  54215. * Step 4 for the catch production in E5 Section 12.14;
  54216. * no value is given for CreateMutableBinding 'D' argument,
  54217. * which implies the binding is not deletable.
  54218. */
  54219. if (!is_finally && DUK_CAT_HAS_CATCH_BINDING_ENABLED(&thr->catchstack[cat_idx])) {
  54220. duk_activation *act;
  54221. duk_hobject *new_env;
  54222. duk_hobject *act_lex_env;
  54223. DUK_DDD(DUK_DDDPRINT("catcher has an automatic catch binding"));
  54224. /* Note: 'act' is dangerous here because it may get invalidate at many
  54225. * points, so we re-lookup it multiple times.
  54226. */
  54227. DUK_ASSERT(thr->callstack_top >= 1);
  54228. act = thr->callstack + thr->callstack_top - 1;
  54229. if (act->lex_env == NULL) {
  54230. DUK_ASSERT(act->var_env == NULL);
  54231. DUK_DDD(DUK_DDDPRINT("delayed environment initialization"));
  54232. /* this may have side effects, so re-lookup act */
  54233. duk_js_init_activation_environment_records_delayed(thr, act);
  54234. act = thr->callstack + thr->callstack_top - 1;
  54235. }
  54236. DUK_ASSERT(act->lex_env != NULL);
  54237. DUK_ASSERT(act->var_env != NULL);
  54238. DUK_ASSERT(DUK_ACT_GET_FUNC(act) != NULL);
  54239. DUK_UNREF(act); /* unreferenced without assertions */
  54240. act = thr->callstack + thr->callstack_top - 1;
  54241. act_lex_env = act->lex_env;
  54242. act = NULL; /* invalidated */
  54243. (void) duk_push_object_helper_proto(ctx,
  54244. DUK_HOBJECT_FLAG_EXTENSIBLE |
  54245. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV),
  54246. act_lex_env);
  54247. new_env = duk_require_hobject(ctx, -1);
  54248. DUK_ASSERT(new_env != NULL);
  54249. DUK_DDD(DUK_DDDPRINT("new_env allocated: %!iO", (duk_heaphdr *) new_env));
  54250. /* Note: currently the catch binding is handled without a register
  54251. * binding because we don't support dynamic register bindings (they
  54252. * must be fixed for an entire function). So, there is no need to
  54253. * record regbases etc.
  54254. */
  54255. DUK_ASSERT(thr->catchstack[cat_idx].h_varname != NULL);
  54256. duk_push_hstring(ctx, thr->catchstack[cat_idx].h_varname);
  54257. duk_push_tval(ctx, &thr->heap->lj.value1);
  54258. duk_xdef_prop(ctx, -3, DUK_PROPDESC_FLAGS_W); /* writable, not configurable */
  54259. act = thr->callstack + thr->callstack_top - 1;
  54260. act->lex_env = new_env;
  54261. DUK_HOBJECT_INCREF(thr, new_env); /* reachable through activation */
  54262. DUK_CAT_SET_LEXENV_ACTIVE(&thr->catchstack[cat_idx]);
  54263. duk_pop(ctx);
  54264. DUK_DDD(DUK_DDDPRINT("new_env finished: %!iO", (duk_heaphdr *) new_env));
  54265. }
  54266. if (is_finally) {
  54267. DUK_CAT_CLEAR_FINALLY_ENABLED(&thr->catchstack[cat_idx]);
  54268. } else {
  54269. DUK_CAT_CLEAR_CATCH_ENABLED(&thr->catchstack[cat_idx]);
  54270. }
  54271. }
  54272. DUK_LOCAL void duk__handle_label(duk_hthread *thr, duk_size_t cat_idx) {
  54273. duk_activation *act;
  54274. /* no callstack changes, no value stack changes */
  54275. DUK_ASSERT(thr != NULL);
  54276. DUK_ASSERT(thr->callstack_top >= 1);
  54277. act = thr->callstack + thr->callstack_top - 1;
  54278. DUK_ASSERT(DUK_ACT_GET_FUNC(act) != NULL);
  54279. DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(act)));
  54280. /* +0 = break, +1 = continue */
  54281. act->pc = thr->catchstack[cat_idx].pc_base + (thr->heap->lj.type == DUK_LJ_TYPE_CONTINUE ? 1 : 0);
  54282. act = NULL; /* invalidated */
  54283. duk_hthread_catchstack_unwind(thr, cat_idx + 1); /* keep label catcher */
  54284. /* no need to unwind callstack */
  54285. /* valstack should not need changes */
  54286. #if defined(DUK_USE_ASSERTIONS)
  54287. act = thr->callstack + thr->callstack_top - 1;
  54288. DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) ==
  54289. (duk_size_t) ((duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act))->nregs);
  54290. #endif
  54291. }
  54292. /* Note: called for DUK_LJ_TYPE_YIELD and for DUK_LJ_TYPE_RETURN, when a
  54293. * return terminates a thread and yields to the resumer.
  54294. */
  54295. DUK_LOCAL void duk__handle_yield(duk_hthread *thr, duk_hthread *resumer, duk_size_t act_idx) {
  54296. duk_tval tv_tmp;
  54297. duk_tval *tv1;
  54298. /* this may also be called for DUK_LJ_TYPE_RETURN; this is OK as long as
  54299. * lj.value1 is correct.
  54300. */
  54301. DUK_ASSERT(DUK_ACT_GET_FUNC(resumer->callstack + act_idx) != NULL);
  54302. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(resumer->callstack + act_idx))); /* resume caller must be an ecmascript func */
  54303. DUK_DDD(DUK_DDDPRINT("resume idx_retval is %ld", (long) resumer->callstack[act_idx].idx_retval));
  54304. tv1 = resumer->valstack + resumer->callstack[act_idx].idx_retval; /* return value from Duktape.Thread.resume() */
  54305. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  54306. DUK_TVAL_SET_TVAL(tv1, &thr->heap->lj.value1);
  54307. DUK_TVAL_INCREF(thr, tv1);
  54308. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  54309. duk_hthread_callstack_unwind(resumer, act_idx + 1); /* unwind to 'resume' caller */
  54310. /* no need to unwind catchstack */
  54311. duk__reconfig_valstack(resumer, act_idx, 1); /* 1 = have retval */
  54312. /* caller must change active thread, and set thr->resumer to NULL */
  54313. }
  54314. DUK_LOCAL
  54315. duk_small_uint_t duk__handle_longjmp(duk_hthread *thr,
  54316. duk_hthread *entry_thread,
  54317. duk_size_t entry_callstack_top) {
  54318. duk_tval tv_tmp;
  54319. duk_size_t entry_callstack_index;
  54320. duk_small_uint_t retval = DUK__LONGJMP_RESTART;
  54321. DUK_ASSERT(thr != NULL);
  54322. DUK_ASSERT(entry_thread != NULL);
  54323. DUK_ASSERT(entry_callstack_top > 0); /* guarantees entry_callstack_top - 1 >= 0 */
  54324. entry_callstack_index = entry_callstack_top - 1;
  54325. /* 'thr' is the current thread, as no-one resumes except us and we
  54326. * switch 'thr' in that case.
  54327. */
  54328. /*
  54329. * (Re)try handling the longjmp.
  54330. *
  54331. * A longjmp handler may convert the longjmp to a different type and
  54332. * "virtually" rethrow by goto'ing to 'check_longjmp'. Before the goto,
  54333. * the following must be updated:
  54334. * - the heap 'lj' state
  54335. * - 'thr' must reflect the "throwing" thread
  54336. */
  54337. check_longjmp:
  54338. DUK_DD(DUK_DDPRINT("handling longjmp: type=%ld, value1=%!T, value2=%!T, iserror=%ld",
  54339. (long) thr->heap->lj.type,
  54340. (duk_tval *) &thr->heap->lj.value1,
  54341. (duk_tval *) &thr->heap->lj.value2,
  54342. (long) thr->heap->lj.iserror));
  54343. switch (thr->heap->lj.type) {
  54344. case DUK_LJ_TYPE_RESUME: {
  54345. /*
  54346. * Note: lj.value1 is 'value', lj.value2 is 'resumee'.
  54347. * This differs from YIELD.
  54348. */
  54349. duk_tval *tv;
  54350. duk_tval *tv2;
  54351. duk_size_t act_idx;
  54352. duk_hthread *resumee;
  54353. /* duk_bi_duk_object_yield() and duk_bi_duk_object_resume() ensure all of these are met */
  54354. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING); /* unchanged by Duktape.Thread.resume() */
  54355. DUK_ASSERT(thr->callstack_top >= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */
  54356. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1) != NULL &&
  54357. DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1)) &&
  54358. ((duk_hnativefunction *) DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1))->func == duk_bi_thread_resume);
  54359. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2) != NULL &&
  54360. DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2))); /* an Ecmascript function */
  54361. DUK_ASSERT_DISABLE((thr->callstack + thr->callstack_top - 2)->idx_retval >= 0); /* unsigned */
  54362. tv = &thr->heap->lj.value2; /* resumee */
  54363. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  54364. DUK_ASSERT(DUK_TVAL_GET_OBJECT(tv) != NULL);
  54365. DUK_ASSERT(DUK_HOBJECT_IS_THREAD(DUK_TVAL_GET_OBJECT(tv)));
  54366. resumee = (duk_hthread *) DUK_TVAL_GET_OBJECT(tv);
  54367. DUK_ASSERT(resumee != NULL);
  54368. DUK_ASSERT(resumee->resumer == NULL);
  54369. DUK_ASSERT(resumee->state == DUK_HTHREAD_STATE_INACTIVE ||
  54370. resumee->state == DUK_HTHREAD_STATE_YIELDED); /* checked by Duktape.Thread.resume() */
  54371. DUK_ASSERT(resumee->state != DUK_HTHREAD_STATE_YIELDED ||
  54372. resumee->callstack_top >= 2); /* YIELDED: Ecmascript activation + Duktape.Thread.yield() activation */
  54373. DUK_ASSERT(resumee->state != DUK_HTHREAD_STATE_YIELDED ||
  54374. (DUK_ACT_GET_FUNC(resumee->callstack + resumee->callstack_top - 1) != NULL &&
  54375. DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(resumee->callstack + resumee->callstack_top - 1)) &&
  54376. ((duk_hnativefunction *) DUK_ACT_GET_FUNC(resumee->callstack + resumee->callstack_top - 1))->func == duk_bi_thread_yield));
  54377. DUK_ASSERT(resumee->state != DUK_HTHREAD_STATE_YIELDED ||
  54378. (DUK_ACT_GET_FUNC(resumee->callstack + resumee->callstack_top - 2) != NULL &&
  54379. DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(resumee->callstack + resumee->callstack_top - 2)))); /* an Ecmascript function */
  54380. DUK_ASSERT_DISABLE(resumee->state != DUK_HTHREAD_STATE_YIELDED ||
  54381. (resumee->callstack + resumee->callstack_top - 2)->idx_retval >= 0); /* idx_retval unsigned */
  54382. DUK_ASSERT(resumee->state != DUK_HTHREAD_STATE_INACTIVE ||
  54383. resumee->callstack_top == 0); /* INACTIVE: no activation, single function value on valstack */
  54384. DUK_ASSERT(resumee->state != DUK_HTHREAD_STATE_INACTIVE ||
  54385. (resumee->valstack_top == resumee->valstack + 1 &&
  54386. DUK_TVAL_IS_OBJECT(resumee->valstack_top - 1) &&
  54387. DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_TVAL_GET_OBJECT(resumee->valstack_top - 1))));
  54388. if (thr->heap->lj.iserror) {
  54389. /*
  54390. * Throw the error in the resumed thread's context; the
  54391. * error value is pushed onto the resumee valstack.
  54392. *
  54393. * Note: the callstack of the target may empty in this case
  54394. * too (i.e. the target thread has never been resumed). The
  54395. * value stack will contain the initial function in that case,
  54396. * which we simply ignore.
  54397. */
  54398. resumee->resumer = thr;
  54399. resumee->state = DUK_HTHREAD_STATE_RUNNING;
  54400. thr->state = DUK_HTHREAD_STATE_RESUMED;
  54401. DUK_HEAP_SWITCH_THREAD(thr->heap, resumee);
  54402. thr = resumee;
  54403. thr->heap->lj.type = DUK_LJ_TYPE_THROW;
  54404. /* thr->heap->lj.value1 is already the value to throw */
  54405. /* thr->heap->lj.value2 is 'thread', will be wiped out at the end */
  54406. DUK_ASSERT(thr->heap->lj.iserror); /* already set */
  54407. DUK_DD(DUK_DDPRINT("-> resume with an error, converted to a throw in the resumee, propagate"));
  54408. goto check_longjmp;
  54409. } else if (resumee->state == DUK_HTHREAD_STATE_YIELDED) {
  54410. act_idx = resumee->callstack_top - 2; /* Ecmascript function */
  54411. DUK_ASSERT_DISABLE(resumee->callstack[act_idx].idx_retval >= 0); /* unsigned */
  54412. tv = resumee->valstack + resumee->callstack[act_idx].idx_retval; /* return value from Duktape.Thread.yield() */
  54413. DUK_ASSERT(tv >= resumee->valstack && tv < resumee->valstack_top);
  54414. tv2 = &thr->heap->lj.value1;
  54415. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  54416. DUK_TVAL_SET_TVAL(tv, tv2);
  54417. DUK_TVAL_INCREF(thr, tv);
  54418. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  54419. duk_hthread_callstack_unwind(resumee, act_idx + 1); /* unwind to 'yield' caller */
  54420. /* no need to unwind catchstack */
  54421. duk__reconfig_valstack(resumee, act_idx, 1); /* 1 = have retval */
  54422. resumee->resumer = thr;
  54423. resumee->state = DUK_HTHREAD_STATE_RUNNING;
  54424. thr->state = DUK_HTHREAD_STATE_RESUMED;
  54425. DUK_HEAP_SWITCH_THREAD(thr->heap, resumee);
  54426. #if 0
  54427. thr = resumee; /* not needed, as we exit right away */
  54428. #endif
  54429. DUK_DD(DUK_DDPRINT("-> resume with a value, restart execution in resumee"));
  54430. retval = DUK__LONGJMP_RESTART;
  54431. goto wipe_and_return;
  54432. } else {
  54433. duk_small_uint_t call_flags;
  54434. duk_bool_t setup_rc;
  54435. /* resumee: [... initial_func] (currently actually: [initial_func]) */
  54436. duk_push_undefined((duk_context *) resumee);
  54437. tv = &thr->heap->lj.value1;
  54438. duk_push_tval((duk_context *) resumee, tv);
  54439. /* resumee: [... initial_func undefined(= this) resume_value ] */
  54440. call_flags = DUK_CALL_FLAG_IS_RESUME; /* is resume, not a tailcall */
  54441. setup_rc = duk_handle_ecma_call_setup(resumee,
  54442. 1, /* num_stack_args */
  54443. call_flags); /* call_flags */
  54444. if (setup_rc == 0) {
  54445. /* Shouldn't happen but check anyway. */
  54446. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR);
  54447. }
  54448. resumee->resumer = thr;
  54449. resumee->state = DUK_HTHREAD_STATE_RUNNING;
  54450. thr->state = DUK_HTHREAD_STATE_RESUMED;
  54451. DUK_HEAP_SWITCH_THREAD(thr->heap, resumee);
  54452. #if 0
  54453. thr = resumee; /* not needed, as we exit right away */
  54454. #endif
  54455. DUK_DD(DUK_DDPRINT("-> resume with a value, restart execution in resumee"));
  54456. retval = DUK__LONGJMP_RESTART;
  54457. goto wipe_and_return;
  54458. }
  54459. DUK_UNREACHABLE();
  54460. break; /* never here */
  54461. }
  54462. case DUK_LJ_TYPE_YIELD: {
  54463. /*
  54464. * Currently only allowed only if yielding thread has only
  54465. * Ecmascript activations (except for the Duktape.Thread.yield()
  54466. * call at the callstack top) and none of them constructor
  54467. * calls.
  54468. *
  54469. * This excludes the 'entry' thread which will always have
  54470. * a preventcount > 0.
  54471. */
  54472. duk_hthread *resumer;
  54473. /* duk_bi_duk_object_yield() and duk_bi_duk_object_resume() ensure all of these are met */
  54474. DUK_ASSERT(thr != entry_thread); /* Duktape.Thread.yield() should prevent */
  54475. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING); /* unchanged from Duktape.Thread.yield() */
  54476. DUK_ASSERT(thr->callstack_top >= 2); /* Ecmascript activation + Duktape.Thread.yield() activation */
  54477. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1) != NULL &&
  54478. DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1)) &&
  54479. ((duk_hnativefunction *) DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1))->func == duk_bi_thread_yield);
  54480. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2) != NULL &&
  54481. DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2))); /* an Ecmascript function */
  54482. DUK_ASSERT_DISABLE((thr->callstack + thr->callstack_top - 2)->idx_retval >= 0); /* unsigned */
  54483. resumer = thr->resumer;
  54484. DUK_ASSERT(resumer != NULL);
  54485. DUK_ASSERT(resumer->state == DUK_HTHREAD_STATE_RESUMED); /* written by a previous RESUME handling */
  54486. DUK_ASSERT(resumer->callstack_top >= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */
  54487. DUK_ASSERT(DUK_ACT_GET_FUNC(resumer->callstack + resumer->callstack_top - 1) != NULL &&
  54488. DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(resumer->callstack + resumer->callstack_top - 1)) &&
  54489. ((duk_hnativefunction *) DUK_ACT_GET_FUNC(resumer->callstack + resumer->callstack_top - 1))->func == duk_bi_thread_resume);
  54490. DUK_ASSERT(DUK_ACT_GET_FUNC(resumer->callstack + resumer->callstack_top - 2) != NULL &&
  54491. DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(resumer->callstack + resumer->callstack_top - 2))); /* an Ecmascript function */
  54492. DUK_ASSERT_DISABLE((resumer->callstack + resumer->callstack_top - 2)->idx_retval >= 0); /* unsigned */
  54493. if (thr->heap->lj.iserror) {
  54494. thr->state = DUK_HTHREAD_STATE_YIELDED;
  54495. thr->resumer = NULL;
  54496. resumer->state = DUK_HTHREAD_STATE_RUNNING;
  54497. DUK_HEAP_SWITCH_THREAD(thr->heap, resumer);
  54498. thr = resumer;
  54499. thr->heap->lj.type = DUK_LJ_TYPE_THROW;
  54500. /* lj.value1 is already set */
  54501. DUK_ASSERT(thr->heap->lj.iserror); /* already set */
  54502. DUK_DD(DUK_DDPRINT("-> yield an error, converted to a throw in the resumer, propagate"));
  54503. goto check_longjmp;
  54504. } else {
  54505. duk__handle_yield(thr, resumer, resumer->callstack_top - 2);
  54506. thr->state = DUK_HTHREAD_STATE_YIELDED;
  54507. thr->resumer = NULL;
  54508. resumer->state = DUK_HTHREAD_STATE_RUNNING;
  54509. DUK_HEAP_SWITCH_THREAD(thr->heap, resumer);
  54510. #if 0
  54511. thr = resumer; /* not needed, as we exit right away */
  54512. #endif
  54513. DUK_DD(DUK_DDPRINT("-> yield a value, restart execution in resumer"));
  54514. retval = DUK__LONGJMP_RESTART;
  54515. goto wipe_and_return;
  54516. }
  54517. DUK_UNREACHABLE();
  54518. break; /* never here */
  54519. }
  54520. case DUK_LJ_TYPE_RETURN: {
  54521. /*
  54522. * Four possible outcomes:
  54523. * * A 'finally' in the same function catches the 'return'.
  54524. * (or)
  54525. * * The return happens at the entry level of the bytecode
  54526. * executor, so return from the executor (in C stack).
  54527. * (or)
  54528. * * There is a calling (Ecmascript) activation in the call
  54529. * stack => return to it.
  54530. * (or)
  54531. * * There is no calling activation, and the thread is
  54532. * terminated. There is always a resumer in this case,
  54533. * which gets the return value similarly to a 'yield'
  54534. * (except that the current thread can no longer be
  54535. * resumed).
  54536. */
  54537. duk_tval *tv1;
  54538. duk_hthread *resumer;
  54539. duk_catcher *cat;
  54540. duk_size_t orig_callstack_index;
  54541. DUK_ASSERT(thr != NULL);
  54542. DUK_ASSERT(thr->callstack_top >= 1);
  54543. DUK_ASSERT(thr->catchstack != NULL);
  54544. /* XXX: does not work if thr->catchstack is NULL */
  54545. /* XXX: does not work if thr->catchstack is allocated but lowest pointer */
  54546. cat = thr->catchstack + thr->catchstack_top - 1; /* may be < thr->catchstack initially */
  54547. DUK_ASSERT(thr->callstack_top > 0); /* ensures callstack_top - 1 >= 0 */
  54548. orig_callstack_index = thr->callstack_top - 1;
  54549. while (cat >= thr->catchstack) {
  54550. if (cat->callstack_index != orig_callstack_index) {
  54551. break;
  54552. }
  54553. if (DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF &&
  54554. DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
  54555. /* 'finally' catches */
  54556. duk__handle_catch_or_finally(thr,
  54557. cat - thr->catchstack,
  54558. 1); /* is_finally */
  54559. DUK_DD(DUK_DDPRINT("-> return caught by a finally (in the same function), restart execution"));
  54560. retval = DUK__LONGJMP_RESTART;
  54561. goto wipe_and_return;
  54562. }
  54563. cat--;
  54564. }
  54565. /* if out of catchstack, cat = thr->catchstack - 1 */
  54566. DUK_DD(DUK_DDPRINT("no catcher in catch stack, return to calling activation / yield"));
  54567. /* return to calling activation (if any) */
  54568. if (thr == entry_thread &&
  54569. thr->callstack_top == entry_callstack_top) {
  54570. /* return to the bytecode executor caller */
  54571. duk_push_tval((duk_context *) thr, &thr->heap->lj.value1);
  54572. /* [ ... retval ] */
  54573. DUK_DD(DUK_DDPRINT("-> return propagated up to entry level, exit bytecode executor"));
  54574. retval = DUK__LONGJMP_FINISHED;
  54575. goto wipe_and_return;
  54576. }
  54577. if (thr->callstack_top >= 2) {
  54578. /* there is a caller; it MUST be an Ecmascript caller (otherwise it would
  54579. * match entry level check)
  54580. */
  54581. DUK_DDD(DUK_DDDPRINT("slow return to Ecmascript caller, idx_retval=%ld, lj_value1=%!T",
  54582. (long) (thr->callstack + thr->callstack_top - 2)->idx_retval,
  54583. (duk_tval *) &thr->heap->lj.value1));
  54584. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2))); /* must be ecmascript */
  54585. tv1 = thr->valstack + (thr->callstack + thr->callstack_top - 2)->idx_retval;
  54586. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  54587. DUK_TVAL_SET_TVAL(tv1, &thr->heap->lj.value1);
  54588. DUK_TVAL_INCREF(thr, tv1);
  54589. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  54590. DUK_DDD(DUK_DDDPRINT("return value at idx_retval=%ld is %!T",
  54591. (long) (thr->callstack + thr->callstack_top - 2)->idx_retval,
  54592. (duk_tval *) (thr->valstack + (thr->callstack + thr->callstack_top - 2)->idx_retval)));
  54593. duk_hthread_catchstack_unwind(thr, (cat - thr->catchstack) + 1); /* leave 'cat' as top catcher (also works if catchstack exhausted) */
  54594. duk_hthread_callstack_unwind(thr, thr->callstack_top - 1);
  54595. duk__reconfig_valstack(thr, thr->callstack_top - 1, 1); /* new top, i.e. callee */
  54596. DUK_DD(DUK_DDPRINT("-> return not caught, restart execution in caller"));
  54597. retval = DUK__LONGJMP_RESTART;
  54598. goto wipe_and_return;
  54599. }
  54600. DUK_DD(DUK_DDPRINT("no calling activation, thread finishes (similar to yield)"));
  54601. DUK_ASSERT(thr->resumer != NULL);
  54602. DUK_ASSERT(thr->resumer->callstack_top >= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */
  54603. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1) != NULL &&
  54604. DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1)) &&
  54605. ((duk_hnativefunction *) DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1))->func == duk_bi_thread_resume); /* Duktape.Thread.resume() */
  54606. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 2) != NULL &&
  54607. DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 2))); /* an Ecmascript function */
  54608. DUK_ASSERT_DISABLE((thr->resumer->callstack + thr->resumer->callstack_top - 2)->idx_retval >= 0); /* unsigned */
  54609. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING);
  54610. DUK_ASSERT(thr->resumer->state == DUK_HTHREAD_STATE_RESUMED);
  54611. resumer = thr->resumer;
  54612. duk__handle_yield(thr, resumer, resumer->callstack_top - 2);
  54613. duk_hthread_terminate(thr); /* updates thread state, minimizes its allocations */
  54614. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_TERMINATED);
  54615. thr->resumer = NULL;
  54616. resumer->state = DUK_HTHREAD_STATE_RUNNING;
  54617. DUK_HEAP_SWITCH_THREAD(thr->heap, resumer);
  54618. #if 0
  54619. thr = resumer; /* not needed */
  54620. #endif
  54621. DUK_DD(DUK_DDPRINT("-> return not caught, thread terminated; handle like yield, restart execution in resumer"));
  54622. retval = DUK__LONGJMP_RESTART;
  54623. goto wipe_and_return;
  54624. }
  54625. case DUK_LJ_TYPE_BREAK:
  54626. case DUK_LJ_TYPE_CONTINUE: {
  54627. /*
  54628. * Find a matching label catcher or 'finally' catcher in
  54629. * the same function.
  54630. *
  54631. * A label catcher must always exist and will match unless
  54632. * a 'finally' captures the break/continue first. It is the
  54633. * compiler's responsibility to ensure that labels are used
  54634. * correctly.
  54635. */
  54636. duk_catcher *cat;
  54637. duk_size_t orig_callstack_index;
  54638. duk_uint_t lj_label;
  54639. cat = thr->catchstack + thr->catchstack_top - 1;
  54640. orig_callstack_index = cat->callstack_index;
  54641. DUK_ASSERT(DUK_TVAL_IS_NUMBER(&thr->heap->lj.value1));
  54642. lj_label = (duk_uint_t) DUK_TVAL_GET_NUMBER(&thr->heap->lj.value1);
  54643. DUK_DDD(DUK_DDDPRINT("handling break/continue with label=%ld, callstack index=%ld",
  54644. (long) lj_label, (long) cat->callstack_index));
  54645. while (cat >= thr->catchstack) {
  54646. if (cat->callstack_index != orig_callstack_index) {
  54647. break;
  54648. }
  54649. DUK_DDD(DUK_DDDPRINT("considering catcher %ld: type=%ld label=%ld",
  54650. (long) (cat - thr->catchstack),
  54651. (long) DUK_CAT_GET_TYPE(cat),
  54652. (long) DUK_CAT_GET_LABEL(cat)));
  54653. if (DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF &&
  54654. DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
  54655. /* finally catches */
  54656. duk__handle_catch_or_finally(thr,
  54657. cat - thr->catchstack,
  54658. 1); /* is_finally */
  54659. DUK_DD(DUK_DDPRINT("-> break/continue caught by a finally (in the same function), restart execution"));
  54660. retval = DUK__LONGJMP_RESTART;
  54661. goto wipe_and_return;
  54662. }
  54663. if (DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_LABEL &&
  54664. (duk_uint_t) DUK_CAT_GET_LABEL(cat) == lj_label) {
  54665. /* found label */
  54666. duk__handle_label(thr,
  54667. cat - thr->catchstack);
  54668. DUK_DD(DUK_DDPRINT("-> break/continue caught by a label catcher (in the same function), restart execution"));
  54669. retval = DUK__LONGJMP_RESTART;
  54670. goto wipe_and_return;
  54671. }
  54672. cat--;
  54673. }
  54674. /* should never happen, but be robust */
  54675. DUK_D(DUK_DPRINT("break/continue not caught by anything in the current function (should never happen)"));
  54676. goto convert_to_internal_error;
  54677. }
  54678. case DUK_LJ_TYPE_THROW: {
  54679. /*
  54680. * Three possible outcomes:
  54681. * * A try or finally catcher is found => resume there.
  54682. * (or)
  54683. * * The error propagates to the bytecode executor entry
  54684. * level (and we're in the entry thread) => rethrow
  54685. * with a new longjmp(), after restoring the previous
  54686. * catchpoint.
  54687. * * The error is not caught in the current thread, so
  54688. * the thread finishes with an error. This works like
  54689. * a yielded error, except that the thread is finished
  54690. * and can no longer be resumed. (There is always a
  54691. * resumer in this case.)
  54692. *
  54693. * Note: until we hit the entry level, there can only be
  54694. * Ecmascript activations.
  54695. */
  54696. duk_catcher *cat;
  54697. duk_hthread *resumer;
  54698. cat = thr->catchstack + thr->catchstack_top - 1;
  54699. while (cat >= thr->catchstack) {
  54700. if (thr == entry_thread &&
  54701. cat->callstack_index < entry_callstack_index) {
  54702. /* entry level reached */
  54703. break;
  54704. }
  54705. if (DUK_CAT_HAS_CATCH_ENABLED(cat)) {
  54706. /* try catches */
  54707. DUK_ASSERT(DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF);
  54708. duk__handle_catch_or_finally(thr,
  54709. cat - thr->catchstack,
  54710. 0); /* is_finally */
  54711. DUK_DD(DUK_DDPRINT("-> throw caught by a 'catch' clause, restart execution"));
  54712. retval = DUK__LONGJMP_RESTART;
  54713. goto wipe_and_return;
  54714. }
  54715. if (DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
  54716. DUK_ASSERT(DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF);
  54717. DUK_ASSERT(!DUK_CAT_HAS_CATCH_ENABLED(cat));
  54718. duk__handle_catch_or_finally(thr,
  54719. cat - thr->catchstack,
  54720. 1); /* is_finally */
  54721. DUK_DD(DUK_DDPRINT("-> throw caught by a 'finally' clause, restart execution"));
  54722. retval = DUK__LONGJMP_RESTART;
  54723. goto wipe_and_return;
  54724. }
  54725. cat--;
  54726. }
  54727. if (thr == entry_thread) {
  54728. /* not caught by anything before entry level; rethrow and let the
  54729. * final catcher unwind everything
  54730. */
  54731. #if 0
  54732. duk_hthread_catchstack_unwind(thr, (cat - thr->catchstack) + 1); /* leave 'cat' as top catcher (also works if catchstack exhausted) */
  54733. duk_hthread_callstack_unwind(thr, entry_callstack_index + 1);
  54734. #endif
  54735. DUK_D(DUK_DPRINT("-> throw propagated up to entry level, rethrow and exit bytecode executor"));
  54736. retval = DUK__LONGJMP_RETHROW;
  54737. goto just_return;
  54738. /* Note: MUST NOT wipe_and_return here, as heap->lj must remain intact */
  54739. }
  54740. DUK_DD(DUK_DDPRINT("not caught by current thread, yield error to resumer"));
  54741. /* not caught by current thread, thread terminates (yield error to resumer);
  54742. * note that this may cause a cascade if the resumer terminates with an uncaught
  54743. * exception etc (this is OK, but needs careful testing)
  54744. */
  54745. DUK_ASSERT(thr->resumer != NULL);
  54746. DUK_ASSERT(thr->resumer->callstack_top >= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */
  54747. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1) != NULL &&
  54748. DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1)) &&
  54749. ((duk_hnativefunction *) DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1))->func == duk_bi_thread_resume); /* Duktape.Thread.resume() */
  54750. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 2) != NULL &&
  54751. DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 2))); /* an Ecmascript function */
  54752. resumer = thr->resumer;
  54753. /* reset longjmp */
  54754. DUK_ASSERT(thr->heap->lj.type == DUK_LJ_TYPE_THROW); /* already set */
  54755. /* lj.value1 already set */
  54756. duk_hthread_terminate(thr); /* updates thread state, minimizes its allocations */
  54757. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_TERMINATED);
  54758. thr->resumer = NULL;
  54759. resumer->state = DUK_HTHREAD_STATE_RUNNING;
  54760. DUK_HEAP_SWITCH_THREAD(thr->heap, resumer);
  54761. thr = resumer;
  54762. goto check_longjmp;
  54763. }
  54764. case DUK_LJ_TYPE_NORMAL: {
  54765. DUK_D(DUK_DPRINT("caught DUK_LJ_TYPE_NORMAL, should never happen, treat as internal error"));
  54766. goto convert_to_internal_error;
  54767. }
  54768. default: {
  54769. /* should never happen, but be robust */
  54770. DUK_D(DUK_DPRINT("caught unknown longjmp type %ld, treat as internal error", (long) thr->heap->lj.type));
  54771. goto convert_to_internal_error;
  54772. }
  54773. } /* end switch */
  54774. DUK_UNREACHABLE();
  54775. wipe_and_return:
  54776. /* this is not strictly necessary, but helps debugging */
  54777. thr->heap->lj.type = DUK_LJ_TYPE_UNKNOWN;
  54778. thr->heap->lj.iserror = 0;
  54779. DUK_TVAL_SET_TVAL(&tv_tmp, &thr->heap->lj.value1);
  54780. DUK_TVAL_SET_UNDEFINED_UNUSED(&thr->heap->lj.value1);
  54781. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  54782. DUK_TVAL_SET_TVAL(&tv_tmp, &thr->heap->lj.value2);
  54783. DUK_TVAL_SET_UNDEFINED_UNUSED(&thr->heap->lj.value2);
  54784. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  54785. just_return:
  54786. return retval;
  54787. convert_to_internal_error:
  54788. /* This could also be thrown internally (set the error, goto check_longjmp),
  54789. * but it's better for internal errors to bubble outwards.
  54790. */
  54791. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR_EXEC_LONGJMP);
  54792. DUK_UNREACHABLE();
  54793. return retval;
  54794. }
  54795. /* XXX: Disabled for 1.0 release. This needs to handle unwinding for label
  54796. * sites (which are created for explicit labels but also for control statements
  54797. * like for-loops). At that point it's quite close to the "slow return" handler
  54798. * except for longjmp(). Perhaps all returns could initially be handled as fast
  54799. * returns and only converted to longjmp()s when basic handling won't do?
  54800. */
  54801. #if 0
  54802. /* Try a fast return. Return false if fails, so that a slow return can be done
  54803. * instead.
  54804. */
  54805. DUK_LOCAL
  54806. duk_bool_t duk__handle_fast_return(duk_hthread *thr,
  54807. duk_tval *tv_retval,
  54808. duk_hthread *entry_thread,
  54809. duk_size_t entry_callstack_top) {
  54810. duk_tval tv_tmp;
  54811. duk_tval *tv1;
  54812. /* retval == NULL indicates 'undefined' return value */
  54813. if (thr == entry_thread && thr->callstack_top == entry_callstack_top) {
  54814. DUK_DDD(DUK_DDDPRINT("reject fast return: return would exit bytecode executor to caller"));
  54815. return 0;
  54816. }
  54817. if (thr->callstack_top <= 1) {
  54818. DUK_DDD(DUK_DDDPRINT("reject fast return: there is no caller in this callstack (thread yield)"));
  54819. return 0;
  54820. }
  54821. /* There is a caller, and it must be an Ecmascript caller (otherwise
  54822. * it would have matched the entry level check).
  54823. */
  54824. DUK_ASSERT(thr->callstack_top >= 2);
  54825. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2))); /* must be ecmascript */
  54826. tv1 = thr->valstack + (thr->callstack + thr->callstack_top - 2)->idx_retval;
  54827. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  54828. if (tv_retval) {
  54829. DUK_TVAL_SET_TVAL(tv1, tv_retval);
  54830. DUK_TVAL_INCREF(thr, tv1);
  54831. } else {
  54832. DUK_TVAL_SET_UNDEFINED_ACTUAL(tv1);
  54833. /* no need to incref */
  54834. }
  54835. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  54836. /* No catchstack to unwind. */
  54837. #if 0
  54838. duk_hthread_catchstack_unwind(thr, (cat - thr->catchstack) + 1); /* leave 'cat' as top catcher (also works if catchstack exhausted) */
  54839. #endif
  54840. duk_hthread_callstack_unwind(thr, thr->callstack_top - 1);
  54841. duk__reconfig_valstack(thr, thr->callstack_top - 1, 1); /* new top, i.e. callee */
  54842. DUK_DDD(DUK_DDDPRINT("fast return accepted"));
  54843. return 1;
  54844. }
  54845. #endif
  54846. /*
  54847. * Executor interrupt handling
  54848. *
  54849. * The handler is called whenever the interrupt countdown reaches zero
  54850. * (or below). The handler must perform whatever checks are activated,
  54851. * e.g. check for cumulative step count to impose an execution step
  54852. * limit or check for breakpoints or other debugger interaction.
  54853. *
  54854. * When the actions are done, the handler must reinit the interrupt
  54855. * init and counter values. The 'init' value must indicate how many
  54856. * bytecode instructions are executed before the next interrupt. The
  54857. * counter must interface with the bytecode executor loop. Concretely,
  54858. * the new init value is normally one higher than the new counter value.
  54859. * For instance, to execute exactly one bytecode instruction the init
  54860. * value is set to 1 and the counter to 0. If an error is thrown by the
  54861. * interrupt handler, the counters are set to the same value (e.g. both
  54862. * to 0 to cause an interrupt when the next bytecode instruction is about
  54863. * to be executed after error handling).
  54864. *
  54865. * Maintaining the init/counter value properly is important for accurate
  54866. * behavior. For instance, executor step limit needs a cumulative step
  54867. * count which is simply computed as a sum of 'init' values. This must
  54868. * work accurately even when single stepping.
  54869. */
  54870. #ifdef DUK_USE_INTERRUPT_COUNTER
  54871. #define DUK__INT_NOACTION 0 /* no specific action, resume normal execution */
  54872. #define DUK__INT_RESTART 1 /* must "goto restart_execution", e.g. breakpoints changed */
  54873. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  54874. DUK_LOCAL void duk__interrupt_handle_debugger(duk_hthread *thr, duk_bool_t *out_immediate, duk_small_uint_t *out_interrupt_retval) {
  54875. duk_context *ctx;
  54876. duk_activation *act;
  54877. duk_breakpoint *bp;
  54878. duk_breakpoint **bp_active;
  54879. duk_uint_fast32_t line = 0;
  54880. duk_bool_t send_status;
  54881. duk_bool_t process_messages;
  54882. duk_bool_t processed_messages = 0;
  54883. ctx = (duk_context *) thr;
  54884. act = thr->callstack + thr->callstack_top - 1;
  54885. /* It might seem that replacing 'thr->heap' with just 'heap' below
  54886. * might be a good idea, but it increases code size slightly
  54887. * (probably due to unnecessary spilling) at least on x64.
  54888. */
  54889. /*
  54890. * Breakpoint and step state checks
  54891. */
  54892. if (act->flags & DUK_ACT_FLAG_BREAKPOINT_ACTIVE ||
  54893. (thr->heap->dbg_step_thread == thr &&
  54894. thr->heap->dbg_step_csindex == thr->callstack_top - 1)) {
  54895. line = duk_debug_curr_line(thr);
  54896. if (act->prev_line != line) {
  54897. DUK_DDD(DUK_DDDPRINT("PC=%ld line=%ld; line transition: %ld -> %ld",
  54898. (long) act->pc, (long) line, (long) act->prev_line, (long) line));
  54899. /* Stepped? Step out is handled by callstack unwind. */
  54900. if ((thr->heap->dbg_step_type == DUK_STEP_TYPE_INTO ||
  54901. thr->heap->dbg_step_type == DUK_STEP_TYPE_OVER) &&
  54902. (thr->heap->dbg_step_thread == thr) &&
  54903. (thr->heap->dbg_step_csindex == thr->callstack_top - 1) &&
  54904. (line != thr->heap->dbg_step_startline)) {
  54905. DUK_D(DUK_DPRINT("STEP STATE TRIGGERED PAUSE at line %ld",
  54906. (long) line));
  54907. DUK_HEAP_SET_PAUSED(thr->heap);
  54908. }
  54909. /* Check for breakpoints only on line transition.
  54910. * Breakpoint is triggered when we enter or cross
  54911. * the target line, and the previous line was within
  54912. * the same function.
  54913. */
  54914. bp_active = thr->heap->dbg_breakpoints_active;
  54915. for (;;) {
  54916. bp = *bp_active++;
  54917. if (bp == NULL) {
  54918. break;
  54919. }
  54920. DUK_ASSERT(bp->filename != NULL);
  54921. if (act->prev_line < bp->line && line >= bp->line) {
  54922. DUK_D(DUK_DPRINT("BREAKPOINT TRIGGERED at %!O:%ld",
  54923. (duk_heaphdr *) bp->filename, (long) bp->line));
  54924. DUK_HEAP_SET_PAUSED(thr->heap);
  54925. }
  54926. }
  54927. } else {
  54928. DUK_DDD(DUK_DDDPRINT("PC=%ld line=%ld", (long) act->pc, (long) line));
  54929. }
  54930. act->prev_line = line;
  54931. }
  54932. /*
  54933. * Rate limit check for sending status update or peeking into
  54934. * the debug transport. Both can be expensive operations that
  54935. * we don't want to do on every opcode.
  54936. *
  54937. * Making sure the interval remains reasonable on a wide variety
  54938. * of targets and bytecode is difficult without a timestamp, so
  54939. * we use a Date-provided timestamp for the rate limit check.
  54940. * But since it's also expensive to get a timestamp, a bytecode
  54941. * counter is used to rate limit getting timestamps.
  54942. */
  54943. if (thr->heap->dbg_state_dirty || thr->heap->dbg_paused) {
  54944. send_status = 1;
  54945. } else {
  54946. send_status = 0;
  54947. }
  54948. if (thr->heap->dbg_paused) {
  54949. process_messages = 1;
  54950. } else {
  54951. process_messages = 0;
  54952. }
  54953. thr->heap->dbg_exec_counter += thr->heap->interrupt_init;
  54954. if (thr->heap->dbg_exec_counter - thr->heap->dbg_last_counter >= DUK_HEAP_DBG_RATELIMIT_OPCODES) {
  54955. /* Overflow of the execution counter is fine and doesn't break
  54956. * anything here.
  54957. */
  54958. duk_double_t now, diff_last;
  54959. thr->heap->dbg_last_counter = thr->heap->dbg_exec_counter;
  54960. now = duk_bi_date_get_now(ctx);
  54961. diff_last = now - thr->heap->dbg_last_time;
  54962. if (diff_last < 0.0 || diff_last >= (duk_double_t) DUK_HEAP_DBG_RATELIMIT_MILLISECS) {
  54963. /* Negative value checked so that a "time jump" works
  54964. * reasonably.
  54965. *
  54966. * Same interval is now used for status sending and
  54967. * peeking.
  54968. */
  54969. thr->heap->dbg_last_time = now;
  54970. send_status = 1;
  54971. process_messages = 1;
  54972. }
  54973. }
  54974. /*
  54975. * Send status
  54976. */
  54977. act = NULL; /* may be changed */
  54978. if (send_status) {
  54979. duk_debug_send_status(thr);
  54980. thr->heap->dbg_state_dirty = 0;
  54981. }
  54982. /*
  54983. * Process messages. If we're paused, we'll block for new messages.
  54984. * if we're not paused, we'll process anything we can peek but won't
  54985. * block for more.
  54986. */
  54987. if (process_messages) {
  54988. processed_messages = duk_debug_process_messages(thr, 0 /*no_block*/);
  54989. }
  54990. /* XXX: any case here where we need to re-send status? */
  54991. /* Continue checked execution if there are breakpoints or we're stepping.
  54992. * Also use checked execution if paused flag is active - it shouldn't be
  54993. * because the debug message loop shouldn't terminate if it was. Step out
  54994. * is handled by callstack unwind and doesn't need checked execution.
  54995. * Note that debugger may have detached due to error or explicit request
  54996. * above, so we must recheck attach status.
  54997. */
  54998. if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) {
  54999. act = thr->callstack + thr->callstack_top - 1; /* relookup, may have changed */
  55000. if (act->flags & DUK_ACT_FLAG_BREAKPOINT_ACTIVE ||
  55001. ((thr->heap->dbg_step_type == DUK_STEP_TYPE_INTO ||
  55002. thr->heap->dbg_step_type == DUK_STEP_TYPE_OVER) &&
  55003. thr->heap->dbg_step_thread == thr &&
  55004. thr->heap->dbg_step_csindex == thr->callstack_top - 1) ||
  55005. thr->heap->dbg_paused) {
  55006. *out_immediate = 1;
  55007. }
  55008. /* If we processed any debug messages breakpoints may have
  55009. * changed; restart execution to re-check active breakpoints.
  55010. */
  55011. if (processed_messages) {
  55012. DUK_D(DUK_DPRINT("processed debug messages, restart execution to recheck possibly changed breakpoints"));
  55013. *out_interrupt_retval = DUK__INT_RESTART;
  55014. }
  55015. } else {
  55016. DUK_D(DUK_DPRINT("debugger became detached, resume normal execution"));
  55017. }
  55018. }
  55019. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  55020. DUK_LOCAL duk_small_uint_t duk__executor_interrupt(duk_hthread *thr) {
  55021. duk_int_t ctr;
  55022. duk_activation *act;
  55023. duk_hcompiledfunction *fun;
  55024. duk_bool_t immediate = 0;
  55025. duk_small_uint_t retval;
  55026. DUK_ASSERT(thr != NULL);
  55027. DUK_ASSERT(thr->callstack != NULL);
  55028. DUK_ASSERT(thr->callstack_top > 0);
  55029. retval = DUK__INT_NOACTION;
  55030. ctr = DUK_HEAP_INTCTR_DEFAULT;
  55031. /*
  55032. * Avoid nested calls. Concretely this happens during debugging, e.g.
  55033. * when we eval() an expression.
  55034. */
  55035. if (DUK_HEAP_HAS_INTERRUPT_RUNNING(thr->heap)) {
  55036. DUK_DD(DUK_DDPRINT("nested executor interrupt, ignoring"));
  55037. /* Set a high interrupt counter; the original executor
  55038. * interrupt invocation will rewrite before exiting.
  55039. */
  55040. thr->heap->interrupt_init = ctr;
  55041. thr->heap->interrupt_counter = ctr - 1;
  55042. thr->interrupt_counter = ctr - 1;
  55043. return DUK__INT_NOACTION;
  55044. }
  55045. DUK_HEAP_SET_INTERRUPT_RUNNING(thr->heap);
  55046. act = thr->callstack + thr->callstack_top - 1;
  55047. fun = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act);
  55048. DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION((duk_hobject *) fun));
  55049. DUK_UNREF(fun);
  55050. #if defined(DUK_USE_EXEC_TIMEOUT_CHECK)
  55051. /*
  55052. * Execution timeout check
  55053. */
  55054. if (DUK_USE_EXEC_TIMEOUT_CHECK(thr->heap->heap_udata)) {
  55055. /* Keep throwing an error whenever we get here. The unusual values
  55056. * are set this way because no instruction is ever executed, we just
  55057. * throw an error until all try/catch/finally and other catchpoints
  55058. * have been exhausted. Duktape/C code gets control at each protected
  55059. * call but whenever it enters back into Duktape the RangeError gets
  55060. * raised. User exec timeout check must consistently indicate a timeout
  55061. * until we've fully bubbled out of Duktape.
  55062. */
  55063. DUK_D(DUK_DPRINT("execution timeout, throwing a RangeError"));
  55064. thr->heap->interrupt_init = 0;
  55065. thr->heap->interrupt_counter = 0;
  55066. thr->interrupt_counter = 0;
  55067. DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, "execution timeout");
  55068. }
  55069. #endif /* DUK_USE_EXEC_TIMEOUT_CHECK */
  55070. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  55071. if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) {
  55072. duk__interrupt_handle_debugger(thr, &immediate, &retval);
  55073. act = thr->callstack + thr->callstack_top - 1; /* relookup if changed */
  55074. }
  55075. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  55076. /*
  55077. * Update the interrupt counter
  55078. */
  55079. if (immediate) {
  55080. /* Cause an interrupt after executing one instruction. */
  55081. ctr = 1;
  55082. }
  55083. DUK_DDD(DUK_DDDPRINT("executor interrupt finished, cstop=%ld, pc=%ld, nextctr=%ld",
  55084. (long) thr->callstack_top, (long) act->pc, (long) ctr));
  55085. /* The counter value is one less than the init value: init value should
  55086. * indicate how many instructions are executed before interrupt. To
  55087. * execute 1 instruction, counter must be 0.
  55088. */
  55089. thr->heap->interrupt_init = ctr;
  55090. thr->heap->interrupt_counter = ctr - 1;
  55091. thr->interrupt_counter = ctr - 1;
  55092. DUK_HEAP_CLEAR_INTERRUPT_RUNNING(thr->heap);
  55093. return retval;
  55094. }
  55095. #endif /* DUK_USE_INTERRUPT_COUNTER */
  55096. /*
  55097. * Debugger handling for executor restart
  55098. *
  55099. * Check for breakpoints, stepping, etc, and figure out if we should execute
  55100. * in checked or normal mode. Note that we can't do this when an activation
  55101. * is created, because breakpoint status (and stepping status) may change
  55102. * later, so we must recheck every time we're executing an activation.
  55103. */
  55104. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  55105. DUK_LOCAL void duk__executor_handle_debugger(duk_hthread *thr, duk_activation *act, duk_hcompiledfunction *fun) {
  55106. duk_heap *heap;
  55107. duk_tval *tv_tmp;
  55108. duk_hstring *filename;
  55109. duk_small_uint_t bp_idx;
  55110. duk_breakpoint **bp_active;
  55111. DUK_ASSERT(thr != NULL);
  55112. DUK_ASSERT(act != NULL);
  55113. DUK_ASSERT(fun != NULL);
  55114. heap = thr->heap;
  55115. bp_active = heap->dbg_breakpoints_active;
  55116. act->flags &= ~DUK_ACT_FLAG_BREAKPOINT_ACTIVE;
  55117. tv_tmp = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) fun, DUK_HTHREAD_STRING_FILE_NAME(thr));
  55118. if (tv_tmp && DUK_TVAL_IS_STRING(tv_tmp)) {
  55119. filename = DUK_TVAL_GET_STRING(tv_tmp);
  55120. /* Figure out all active breakpoints. A breakpoint is
  55121. * considered active if the current function's fileName
  55122. * matches the breakpoint's fileName, AND there is no
  55123. * inner function that has matching line numbers
  55124. * (otherwise a breakpoint would be triggered both
  55125. * inside and outside of the inner function which would
  55126. * be confusing). Example:
  55127. *
  55128. * function foo() {
  55129. * print('foo');
  55130. * function bar() { <-. breakpoints in these
  55131. * print('bar'); | lines should not affect
  55132. * } <-' foo() execution
  55133. * bar();
  55134. * }
  55135. *
  55136. * We need a few things that are only available when
  55137. * debugger support is enabled: (1) a line range for
  55138. * each function, and (2) access to the function
  55139. * template to access the inner functions (and their
  55140. * line ranges).
  55141. *
  55142. * It's important to have a narrow match for active
  55143. * breakpoints so that we don't enter checked execution
  55144. * when that's not necessary. For instance, if we're
  55145. * running inside a certain function and there's
  55146. * breakpoint outside in (after the call site), we
  55147. * don't want to slow down execution of the function.
  55148. */
  55149. for (bp_idx = 0; bp_idx < heap->dbg_breakpoint_count; bp_idx++) {
  55150. duk_breakpoint *bp = heap->dbg_breakpoints + bp_idx;
  55151. duk_hobject **funcs, **funcs_end;
  55152. duk_hcompiledfunction *inner_fun;
  55153. duk_bool_t bp_match;
  55154. if (bp->filename == filename &&
  55155. bp->line >= fun->start_line && bp->line <= fun->end_line) {
  55156. bp_match = 1;
  55157. DUK_DD(DUK_DDPRINT("breakpoint filename and line match: "
  55158. "%s:%ld vs. %s (line %ld vs. %ld-%ld)",
  55159. DUK_HSTRING_GET_DATA(bp->filename),
  55160. (long) bp->line,
  55161. DUK_HSTRING_GET_DATA(filename),
  55162. (long) bp->line,
  55163. (long) fun->start_line,
  55164. (long) fun->end_line));
  55165. funcs = DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, fun);
  55166. funcs_end = DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(thr->heap, fun);
  55167. while (funcs != funcs_end) {
  55168. inner_fun = (duk_hcompiledfunction *) *funcs;
  55169. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) inner_fun));
  55170. if (bp->line >= inner_fun->start_line && bp->line <= inner_fun->end_line) {
  55171. DUK_DD(DUK_DDPRINT("inner function masks ('captures') breakpoint"));
  55172. bp_match = 0;
  55173. break;
  55174. }
  55175. funcs++;
  55176. }
  55177. if (bp_match) {
  55178. /* No need to check for size of bp_active list,
  55179. * it's always larger than maximum number of
  55180. * breakpoints.
  55181. */
  55182. act->flags |= DUK_ACT_FLAG_BREAKPOINT_ACTIVE;
  55183. *bp_active = heap->dbg_breakpoints + bp_idx;
  55184. bp_active++;
  55185. }
  55186. }
  55187. }
  55188. }
  55189. *bp_active = NULL; /* terminate */
  55190. DUK_DD(DUK_DDPRINT("ACTIVE BREAKPOINTS: %ld", (long) (bp_active - thr->heap->dbg_breakpoints_active)));
  55191. /* Force pause if we were doing "step into" in another activation. */
  55192. if (thr->heap->dbg_step_thread != NULL &&
  55193. thr->heap->dbg_step_type == DUK_STEP_TYPE_INTO &&
  55194. (thr->heap->dbg_step_thread != thr ||
  55195. thr->heap->dbg_step_csindex != thr->callstack_top - 1)) {
  55196. DUK_D(DUK_DPRINT("STEP INTO ACTIVE, FORCE PAUSED"));
  55197. DUK_HEAP_SET_PAUSED(thr->heap);
  55198. }
  55199. /* Force interrupt right away if we're paused or in "checked mode".
  55200. * Step out is handled by callstack unwind.
  55201. */
  55202. if (act->flags & (DUK_ACT_FLAG_BREAKPOINT_ACTIVE) ||
  55203. thr->heap->dbg_paused ||
  55204. (thr->heap->dbg_step_type != DUK_STEP_TYPE_OUT &&
  55205. thr->heap->dbg_step_csindex == thr->callstack_top - 1)) {
  55206. thr->interrupt_counter = 0;
  55207. }
  55208. }
  55209. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  55210. /*
  55211. * Ecmascript bytecode executor.
  55212. *
  55213. * Resume execution for the current thread from its current activation.
  55214. * Returns when execution would return from the entry level activation,
  55215. * leaving a single return value on top of the stack. Function calls
  55216. * and thread resumptions are handled internally. If an error occurs,
  55217. * a longjmp() with type DUK_LJ_TYPE_THROW is called on the entry level
  55218. * setjmp() jmpbuf.
  55219. *
  55220. * Ecmascript function calls and coroutine resumptions are handled
  55221. * internally without recursive C calls. Other function calls are
  55222. * handled using duk_handle_call(), increasing C recursion depth.
  55223. *
  55224. * There are many other tricky control flow situations, such as:
  55225. *
  55226. * - Break and continue (fast and slow)
  55227. * - Return (fast and slow)
  55228. * - Error throwing
  55229. * - Thread resume and yield
  55230. *
  55231. * For more detailed notes, see doc/execution.rst.
  55232. *
  55233. * Also see doc/code-issues.rst for discussion of setjmp(), longjmp(),
  55234. * and volatile.
  55235. */
  55236. #define DUK__STRICT() (DUK_HOBJECT_HAS_STRICT(&(fun)->obj))
  55237. #define DUK__REG(x) (thr->valstack_bottom[(x)])
  55238. #define DUK__REGP(x) (&thr->valstack_bottom[(x)])
  55239. #define DUK__CONST(x) (DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, fun)[(x)])
  55240. #define DUK__CONSTP(x) (&DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, fun)[(x)])
  55241. #define DUK__REGCONST(x) ((x) < DUK_BC_REGLIMIT ? DUK__REG((x)) : DUK__CONST((x) - DUK_BC_REGLIMIT))
  55242. #define DUK__REGCONSTP(x) ((x) < DUK_BC_REGLIMIT ? DUK__REGP((x)) : DUK__CONSTP((x) - DUK_BC_REGLIMIT))
  55243. #ifdef DUK_USE_VERBOSE_EXECUTOR_ERRORS
  55244. #define DUK__INTERNAL_ERROR(msg) do { \
  55245. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, (msg)); \
  55246. } while (0)
  55247. #else
  55248. #define DUK__INTERNAL_ERROR(msg) do { \
  55249. goto internal_error; \
  55250. } while (0)
  55251. #endif
  55252. DUK_INTERNAL void duk_js_execute_bytecode(duk_hthread *exec_thr) {
  55253. /* Entry level info. Although these are assigned to before setjmp()
  55254. * a 'volatile' seems to be needed. Note placement of "volatile" for
  55255. * pointers. See doc/code-issues.rst for more discussion.
  55256. */
  55257. duk_hthread * volatile entry_thread; /* volatile copy of exec_thr */
  55258. volatile duk_size_t entry_callstack_top;
  55259. volatile duk_int_t entry_call_recursion_depth;
  55260. duk_jmpbuf * volatile entry_jmpbuf_ptr;
  55261. /* "hot" variables for interpretation -- not volatile, value not guaranteed in setjmp error handling */
  55262. duk_hthread *thr; /* stable */
  55263. duk_activation *act; /* semi-stable (ok as long as callstack not resized) */
  55264. duk_hcompiledfunction *fun; /* stable */
  55265. duk_instr_t *bcode; /* stable */
  55266. /* 'consts' is computed on-the-fly */
  55267. /* 'funcs' is quite rarely used, so no local for it */
  55268. /* "hot" temps for interpretation -- not volatile, value not guaranteed in setjmp error handling */
  55269. duk_uint_fast32_t ins; /* XXX: check performance impact on x64 between fast/non-fast variant */
  55270. /* jmpbuf */
  55271. duk_jmpbuf jmpbuf;
  55272. #ifdef DUK_USE_INTERRUPT_COUNTER
  55273. duk_int_t int_ctr;
  55274. #endif
  55275. #ifdef DUK_USE_ASSERTIONS
  55276. duk_size_t valstack_top_base; /* valstack top, should match before interpreting each op (no leftovers) */
  55277. #endif
  55278. /* XXX: document assumptions on setjmp and volatile variables
  55279. * (see duk_handle_call()).
  55280. */
  55281. /*
  55282. * Preliminaries
  55283. */
  55284. DUK_ASSERT(exec_thr != NULL);
  55285. DUK_ASSERT(exec_thr->heap != NULL);
  55286. DUK_ASSERT(exec_thr->heap->curr_thread != NULL);
  55287. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR((duk_heaphdr *) exec_thr);
  55288. DUK_ASSERT(exec_thr->callstack_top >= 1); /* at least one activation, ours */
  55289. DUK_ASSERT(DUK_ACT_GET_FUNC(exec_thr->callstack + exec_thr->callstack_top - 1) != NULL);
  55290. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(exec_thr->callstack + exec_thr->callstack_top - 1)));
  55291. entry_thread = exec_thr; /* volatile copy */
  55292. thr = (duk_hthread *) entry_thread;
  55293. entry_callstack_top = thr->callstack_top;
  55294. entry_call_recursion_depth = thr->heap->call_recursion_depth;
  55295. entry_jmpbuf_ptr = thr->heap->lj.jmpbuf_ptr;
  55296. /*
  55297. * Setjmp catchpoint setup.
  55298. *
  55299. * Note: we currently assume that the setjmp() catchpoint is
  55300. * not re-entrant (longjmp() cannot be called more than once
  55301. * for a single setjmp()).
  55302. */
  55303. reset_setjmp_catchpoint:
  55304. DUK_ASSERT(thr != NULL);
  55305. thr->heap->lj.jmpbuf_ptr = &jmpbuf;
  55306. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL);
  55307. if (DUK_SETJMP(thr->heap->lj.jmpbuf_ptr->jb)) {
  55308. /*
  55309. * Note: any local variables accessed here must have their value
  55310. * assigned *before* the setjmp() call, OR they must be declared
  55311. * volatile. Otherwise their value is not guaranteed to be correct.
  55312. *
  55313. * 'thr' might seem to be a risky variable because it is changed
  55314. * for yield and resume. However, yield and resume are handled
  55315. * using longjmp()s.
  55316. */
  55317. duk_small_uint_t lj_ret;
  55318. DUK_DDD(DUK_DDDPRINT("longjmp caught by bytecode executor"));
  55319. /* Relookup 'thr': it's not volatile so its value is not
  55320. * guaranteed. The heap->curr_thread value should always be
  55321. * valid here because longjmp callers don't switch threads,
  55322. * only the longjmp handler does that (even for RESUME and
  55323. * YIELD).
  55324. */
  55325. DUK_ASSERT(entry_thread != NULL);
  55326. thr = entry_thread->heap->curr_thread;
  55327. /* XXX: signalling the need to shrink check (only if unwound) */
  55328. /* Must be restored here to handle e.g. yields properly. */
  55329. thr->heap->call_recursion_depth = entry_call_recursion_depth;
  55330. /* Switch to caller's setjmp() catcher so that if an error occurs
  55331. * during error handling, it is always propagated outwards instead
  55332. * of causing an infinite loop in our own handler.
  55333. */
  55334. DUK_DDD(DUK_DDDPRINT("restore jmpbuf_ptr: %p -> %p",
  55335. (void *) ((thr && thr->heap) ? thr->heap->lj.jmpbuf_ptr : NULL),
  55336. (void *) entry_jmpbuf_ptr));
  55337. thr->heap->lj.jmpbuf_ptr = (duk_jmpbuf *) entry_jmpbuf_ptr;
  55338. lj_ret = duk__handle_longjmp(thr, (duk_hthread *) entry_thread, (duk_size_t) entry_callstack_top);
  55339. if (lj_ret == DUK__LONGJMP_RESTART) {
  55340. /*
  55341. * Restart bytecode execution, possibly with a changed thread.
  55342. */
  55343. thr = thr->heap->curr_thread;
  55344. goto reset_setjmp_catchpoint;
  55345. } else if (lj_ret == DUK__LONGJMP_RETHROW) {
  55346. /*
  55347. * Rethrow error to calling state.
  55348. */
  55349. /* thread may have changed (e.g. YIELD converted to THROW) */
  55350. thr = thr->heap->curr_thread;
  55351. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr == entry_jmpbuf_ptr);
  55352. duk_err_longjmp(thr);
  55353. DUK_UNREACHABLE();
  55354. } else {
  55355. /*
  55356. * Return from bytecode executor with a return value.
  55357. */
  55358. DUK_ASSERT(lj_ret == DUK__LONGJMP_FINISHED);
  55359. /* XXX: return assertions for valstack, callstack, catchstack */
  55360. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr == entry_jmpbuf_ptr);
  55361. return;
  55362. }
  55363. DUK_UNREACHABLE();
  55364. }
  55365. /*
  55366. * Restart execution by reloading thread state.
  55367. *
  55368. * Note that 'thr' and any thread configuration may have changed,
  55369. * so all local variables are suspect.
  55370. *
  55371. * The number of local variables should be kept to a minimum: if
  55372. * the variables are spilled, they will need to be loaded from
  55373. * memory anyway.
  55374. */
  55375. restart_execution:
  55376. /* Lookup current thread; use the volatile 'entry_thread' for this to
  55377. * avoid clobber warnings. (Any valid, reachable 'thr' value would be
  55378. * fine for this, so using 'entry_thread' is just to silence warnings.)
  55379. */
  55380. thr = entry_thread->heap->curr_thread;
  55381. DUK_ASSERT(thr != NULL);
  55382. DUK_ASSERT(thr->callstack_top >= 1);
  55383. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1) != NULL);
  55384. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1)));
  55385. #ifdef DUK_USE_INTERRUPT_COUNTER
  55386. thr->interrupt_counter = thr->heap->interrupt_counter;
  55387. #endif
  55388. /* assume that thr->valstack_bottom has been set-up before getting here */
  55389. act = thr->callstack + thr->callstack_top - 1;
  55390. fun = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act);
  55391. DUK_ASSERT(fun != NULL);
  55392. DUK_ASSERT(thr->valstack_top - thr->valstack_bottom == fun->nregs);
  55393. bcode = DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, fun);
  55394. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  55395. if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap) && !thr->heap->dbg_processing) {
  55396. thr->heap->dbg_processing = 1;
  55397. duk__executor_handle_debugger(thr, act, fun);
  55398. thr->heap->dbg_processing = 0;
  55399. }
  55400. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  55401. /* XXX: shrink check flag? */
  55402. /*
  55403. * Bytecode interpreter.
  55404. *
  55405. * The interpreter must be very careful with memory pointers, as
  55406. * many pointers are not guaranteed to be 'stable' and may be
  55407. * reallocated and relocated on-the-fly quite easily (e.g. by a
  55408. * memory allocation or a property access).
  55409. *
  55410. * The following are assumed to have stable pointers:
  55411. * - the current thread
  55412. * - the current function
  55413. * - the bytecode, constant table, inner function table of the
  55414. * current function (as they are a part of the function allocation)
  55415. *
  55416. * The following are assumed to have semi-stable pointers:
  55417. * - the current activation entry: stable as long as callstack
  55418. * is not changed (reallocated by growing or shrinking), or
  55419. * by any garbage collection invocation (through finalizers)
  55420. * - Note in particular that ANY DECREF can invalidate the
  55421. * activation pointer
  55422. *
  55423. * The following are not assumed to have stable pointers at all:
  55424. * - the value stack (registers) of the current thread
  55425. * - the catch stack of the current thread
  55426. *
  55427. * See execution.txt for discussion.
  55428. */
  55429. DUK_ASSERT(thr != NULL);
  55430. DUK_ASSERT(act != NULL);
  55431. DUK_ASSERT(fun != NULL);
  55432. DUK_ASSERT(bcode != NULL);
  55433. DUK_DD(DUK_DDPRINT("restarting execution, thr %p, act %p (idx %ld), fun %p, bcode %p, "
  55434. "consts %p, funcs %p, lev %ld, regbot %ld, regtop %ld, catchstack_top=%ld, "
  55435. "preventcount=%ld",
  55436. (void *) thr,
  55437. (void *) act,
  55438. (long) (thr->callstack_top - 1),
  55439. (void *) fun,
  55440. (void *) bcode,
  55441. (void *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, fun),
  55442. (void *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, fun),
  55443. (long) (thr->callstack_top - 1),
  55444. (long) (thr->valstack_bottom - thr->valstack),
  55445. (long) (thr->valstack_top - thr->valstack),
  55446. (long) thr->catchstack_top,
  55447. (long) thr->callstack_preventcount));
  55448. #ifdef DUK_USE_ASSERTIONS
  55449. valstack_top_base = (duk_size_t) (thr->valstack_top - thr->valstack);
  55450. #endif
  55451. for (;;) {
  55452. DUK_ASSERT(thr->callstack_top >= 1);
  55453. DUK_ASSERT(thr->valstack_top - thr->valstack_bottom == fun->nregs);
  55454. DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack) == valstack_top_base);
  55455. /* Executor interrupt counter check, used to implement breakpoints,
  55456. * debugging interface, execution timeouts, etc. The counter is heap
  55457. * specific but is maintained in the current thread to make the check
  55458. * as fast as possible. The counter is copied back to the heap struct
  55459. * whenever a thread switch occurs by the DUK_HEAP_SWITCH_THREAD() macro.
  55460. */
  55461. #ifdef DUK_USE_INTERRUPT_COUNTER
  55462. int_ctr = thr->interrupt_counter;
  55463. if (DUK_LIKELY(int_ctr > 0)) {
  55464. thr->interrupt_counter = int_ctr - 1;
  55465. } else {
  55466. /* Trigger at zero or below */
  55467. duk_small_uint_t exec_int_ret;
  55468. exec_int_ret = duk__executor_interrupt(thr);
  55469. if (exec_int_ret == DUK__INT_RESTART) {
  55470. goto restart_execution;
  55471. }
  55472. }
  55473. #endif
  55474. /* Because ANY DECREF potentially invalidates 'act' now (through
  55475. * finalization), we need to re-lookup 'act' in almost every case.
  55476. *
  55477. * XXX: future work for performance optimization:
  55478. * This is not nice; it would be nice if the program counter was a
  55479. * behind a stable pointer. For instance, put a raw bytecode pointer
  55480. * into duk_hthread struct (not into the callstack); since bytecode
  55481. * has a stable pointer this would work nicely. Whenever a call is
  55482. * made, the bytecode pointer could be backed up as an integer index
  55483. * to the calling activation. Perhaps add a macro for setting up a
  55484. * new activation (same as for setting up / switching threads)?
  55485. */
  55486. act = thr->callstack + thr->callstack_top - 1;
  55487. DUK_ASSERT(bcode + act->pc >= DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, fun));
  55488. DUK_ASSERT(bcode + act->pc < DUK_HCOMPILEDFUNCTION_GET_CODE_END(thr->heap, fun));
  55489. DUK_DDD(DUK_DDDPRINT("executing bytecode: pc=%ld ins=0x%08lx, op=%ld, valstack_top=%ld/%ld, nregs=%ld --> %!I",
  55490. (long) act->pc,
  55491. (unsigned long) bcode[act->pc],
  55492. (long) DUK_DEC_OP(bcode[act->pc]),
  55493. (long) (thr->valstack_top - thr->valstack),
  55494. (long) (thr->valstack_end - thr->valstack),
  55495. (long) (fun ? fun->nregs : -1),
  55496. (duk_instr_t) bcode[act->pc]));
  55497. #if defined(DUK_USE_ASSERTIONS)
  55498. /* Quite heavy assert: check that valstack is in correctly
  55499. * initialized state. Improper shuffle instructions can
  55500. * write beyond valstack_end so this check catches them in
  55501. * the act.
  55502. */
  55503. {
  55504. duk_tval *tv;
  55505. tv = thr->valstack_top;
  55506. while (tv != thr->valstack_end) {
  55507. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED_UNUSED(tv));
  55508. tv++;
  55509. }
  55510. }
  55511. #endif
  55512. ins = bcode[act->pc++];
  55513. /* Typing: use duk_small_(u)int_fast_t when decoding small
  55514. * opcode fields (op, A, B, C) and duk_(u)int_fast_t when
  55515. * decoding larger fields (e.g. BC which is 18 bits). Use
  55516. * unsigned variant by default, signed when the value is used
  55517. * in signed arithmetic. Using variable names such as 'a', 'b',
  55518. * 'c', 'bc', etc makes it easier to spot typing mismatches.
  55519. */
  55520. /* XXX: the best typing needs to be validated by perf measurement:
  55521. * e.g. using a small type which is the cast to a larger duk_idx_t
  55522. * may be slower than declaring the variable as a duk_idx_t in the
  55523. * first place.
  55524. */
  55525. /* XXX: use macros for the repetitive tval/refcount handling. */
  55526. switch ((int) DUK_DEC_OP(ins)) {
  55527. /* XXX: switch cast? */
  55528. case DUK_OP_LDREG: {
  55529. duk_small_uint_fast_t a;
  55530. duk_uint_fast_t bc;
  55531. duk_tval tv_tmp;
  55532. duk_tval *tv1, *tv2;
  55533. a = DUK_DEC_A(ins); tv1 = DUK__REGP(a);
  55534. bc = DUK_DEC_BC(ins); tv2 = DUK__REGP(bc);
  55535. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  55536. DUK_TVAL_SET_TVAL(tv1, tv2);
  55537. DUK_TVAL_INCREF(thr, tv1);
  55538. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  55539. break;
  55540. }
  55541. case DUK_OP_STREG: {
  55542. duk_small_uint_fast_t a;
  55543. duk_uint_fast_t bc;
  55544. duk_tval tv_tmp;
  55545. duk_tval *tv1, *tv2;
  55546. a = DUK_DEC_A(ins); tv1 = DUK__REGP(a);
  55547. bc = DUK_DEC_BC(ins); tv2 = DUK__REGP(bc);
  55548. DUK_TVAL_SET_TVAL(&tv_tmp, tv2);
  55549. DUK_TVAL_SET_TVAL(tv2, tv1);
  55550. DUK_TVAL_INCREF(thr, tv2);
  55551. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  55552. break;
  55553. }
  55554. case DUK_OP_LDCONST: {
  55555. duk_small_uint_fast_t a;
  55556. duk_uint_fast_t bc;
  55557. duk_tval tv_tmp;
  55558. duk_tval *tv1, *tv2;
  55559. a = DUK_DEC_A(ins); tv1 = DUK__REGP(a);
  55560. bc = DUK_DEC_BC(ins); tv2 = DUK__CONSTP(bc);
  55561. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  55562. DUK_TVAL_SET_TVAL(tv1, tv2);
  55563. DUK_TVAL_INCREF(thr, tv2); /* may be e.g. string */
  55564. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  55565. break;
  55566. }
  55567. case DUK_OP_LDINT: {
  55568. duk_small_uint_fast_t a;
  55569. duk_int_fast_t bc;
  55570. duk_tval tv_tmp;
  55571. duk_tval *tv1;
  55572. #if defined(DUK_USE_FASTINT)
  55573. duk_int32_t val;
  55574. #else
  55575. duk_double_t val;
  55576. #endif
  55577. #if defined(DUK_USE_FASTINT)
  55578. a = DUK_DEC_A(ins); tv1 = DUK__REGP(a);
  55579. bc = DUK_DEC_BC(ins); val = (duk_int32_t) (bc - DUK_BC_LDINT_BIAS);
  55580. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  55581. DUK_TVAL_SET_FASTINT_I32(tv1, val);
  55582. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  55583. #else
  55584. a = DUK_DEC_A(ins); tv1 = DUK__REGP(a);
  55585. bc = DUK_DEC_BC(ins); val = (duk_double_t) (bc - DUK_BC_LDINT_BIAS);
  55586. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  55587. DUK_TVAL_SET_NUMBER(tv1, val);
  55588. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  55589. #endif
  55590. break;
  55591. }
  55592. case DUK_OP_LDINTX: {
  55593. duk_small_uint_fast_t a;
  55594. duk_tval *tv1;
  55595. duk_double_t val;
  55596. /* LDINTX is not necessarily in FASTINT range, so
  55597. * no fast path for now.
  55598. *
  55599. * XXX: perhaps restrict LDINTX to fastint range, wider
  55600. * range very rarely needed.
  55601. */
  55602. a = DUK_DEC_A(ins); tv1 = DUK__REGP(a);
  55603. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1));
  55604. val = DUK_TVAL_GET_NUMBER(tv1) * ((duk_double_t) (1L << DUK_BC_LDINTX_SHIFT)) +
  55605. (duk_double_t) DUK_DEC_BC(ins);
  55606. #if defined(DUK_USE_FASTINT)
  55607. DUK_TVAL_SET_NUMBER_CHKFAST(tv1, val);
  55608. #else
  55609. DUK_TVAL_SET_NUMBER(tv1, val);
  55610. #endif
  55611. break;
  55612. }
  55613. case DUK_OP_MPUTOBJ:
  55614. case DUK_OP_MPUTOBJI: {
  55615. duk_context *ctx = (duk_context *) thr;
  55616. duk_small_uint_fast_t a;
  55617. duk_tval *tv1;
  55618. duk_hobject *obj;
  55619. duk_uint_fast_t idx;
  55620. duk_small_uint_fast_t count;
  55621. /* A -> register of target object
  55622. * B -> first register of key/value pair list
  55623. * C -> number of key/value pairs
  55624. */
  55625. a = DUK_DEC_A(ins); tv1 = DUK__REGP(a);
  55626. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1));
  55627. obj = DUK_TVAL_GET_OBJECT(tv1);
  55628. idx = (duk_uint_fast_t) DUK_DEC_B(ins);
  55629. if (DUK_DEC_OP(ins) == DUK_OP_MPUTOBJI) {
  55630. duk_tval *tv_ind = DUK__REGP(idx);
  55631. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind));
  55632. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind);
  55633. }
  55634. count = (duk_small_uint_fast_t) DUK_DEC_C(ins);
  55635. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  55636. if (DUK_UNLIKELY(idx + count * 2 > (duk_uint_fast_t) duk_get_top(ctx))) {
  55637. /* XXX: use duk_is_valid_index() instead? */
  55638. /* XXX: improve check; check against nregs, not against top */
  55639. DUK__INTERNAL_ERROR("MPUTOBJ out of bounds");
  55640. }
  55641. #endif
  55642. duk_push_hobject(ctx, obj);
  55643. while (count > 0) {
  55644. /* XXX: faster initialization (direct access or better primitives) */
  55645. duk_push_tval(ctx, DUK__REGP(idx));
  55646. DUK_ASSERT(duk_is_string(ctx, -1));
  55647. duk_push_tval(ctx, DUK__REGP(idx + 1)); /* -> [... obj key value] */
  55648. duk_xdef_prop_wec(ctx, -3); /* -> [... obj] */
  55649. count--;
  55650. idx += 2;
  55651. }
  55652. duk_pop(ctx); /* [... obj] -> [...] */
  55653. break;
  55654. }
  55655. case DUK_OP_MPUTARR:
  55656. case DUK_OP_MPUTARRI: {
  55657. duk_context *ctx = (duk_context *) thr;
  55658. duk_small_uint_fast_t a;
  55659. duk_tval *tv1;
  55660. duk_hobject *obj;
  55661. duk_uint_fast_t idx;
  55662. duk_small_uint_fast_t count;
  55663. duk_uint32_t arr_idx;
  55664. /* A -> register of target object
  55665. * B -> first register of value data (start_index, value1, value2, ..., valueN)
  55666. * C -> number of key/value pairs (N)
  55667. */
  55668. a = DUK_DEC_A(ins); tv1 = DUK__REGP(a);
  55669. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1));
  55670. obj = DUK_TVAL_GET_OBJECT(tv1);
  55671. DUK_ASSERT(obj != NULL);
  55672. idx = (duk_uint_fast_t) DUK_DEC_B(ins);
  55673. if (DUK_DEC_OP(ins) == DUK_OP_MPUTARRI) {
  55674. duk_tval *tv_ind = DUK__REGP(idx);
  55675. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind));
  55676. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind);
  55677. }
  55678. count = (duk_small_uint_fast_t) DUK_DEC_C(ins);
  55679. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  55680. if (idx + count + 1 > (duk_uint_fast_t) duk_get_top(ctx)) {
  55681. /* XXX: use duk_is_valid_index() instead? */
  55682. /* XXX: improve check; check against nregs, not against top */
  55683. DUK__INTERNAL_ERROR("MPUTARR out of bounds");
  55684. }
  55685. #endif
  55686. tv1 = DUK__REGP(idx);
  55687. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1));
  55688. arr_idx = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv1);
  55689. idx++;
  55690. duk_push_hobject(ctx, obj);
  55691. while (count > 0) {
  55692. /* duk_xdef_prop() will define an own property without any array
  55693. * special behaviors. We'll need to set the array length explicitly
  55694. * in the end. For arrays with elisions, the compiler will emit an
  55695. * explicit SETALEN which will update the length.
  55696. */
  55697. /* XXX: because we're dealing with 'own' properties of a fresh array,
  55698. * the array initializer should just ensure that the array has a large
  55699. * enough array part and write the values directly into array part,
  55700. * and finally set 'length' manually in the end (as already happens now).
  55701. */
  55702. duk_push_tval(ctx, DUK__REGP(idx)); /* -> [... obj value] */
  55703. duk_xdef_prop_index_wec(ctx, -2, arr_idx); /* -> [... obj] */
  55704. /* XXX: could use at least one fewer loop counters */
  55705. count--;
  55706. idx++;
  55707. arr_idx++;
  55708. }
  55709. /* XXX: E5.1 Section 11.1.4 coerces the final length through
  55710. * ToUint32() which is odd but happens now as a side effect of
  55711. * 'arr_idx' type.
  55712. */
  55713. duk_hobject_set_length(thr, obj, (duk_uint32_t) arr_idx);
  55714. duk_pop(ctx); /* [... obj] -> [...] */
  55715. break;
  55716. }
  55717. case DUK_OP_NEW:
  55718. case DUK_OP_NEWI: {
  55719. duk_context *ctx = (duk_context *) thr;
  55720. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  55721. duk_uint_fast_t idx;
  55722. duk_small_uint_fast_t i;
  55723. /* A -> unused (reserved for flags, for consistency with DUK_OP_CALL)
  55724. * B -> target register and start reg: constructor, arg1, ..., argN
  55725. * (for DUK_OP_NEWI, 'b' is indirect)
  55726. * C -> num args (N)
  55727. */
  55728. /* Note: duk_new() will call the constuctor using duk_handle_call().
  55729. * A constructor call prevents a yield from inside the constructor,
  55730. * even if the constructor is an Ecmascript function.
  55731. */
  55732. /* XXX: unnecessary copying of values? Just set 'top' to
  55733. * b + c, and let the return handling fix up the stack frame?
  55734. */
  55735. idx = (duk_uint_fast_t) DUK_DEC_B(ins);
  55736. if (DUK_DEC_OP(ins) == DUK_OP_NEWI) {
  55737. duk_tval *tv_ind = DUK__REGP(idx);
  55738. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind));
  55739. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind);
  55740. }
  55741. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  55742. if (idx + c + 1 > (duk_uint_fast_t) duk_get_top(ctx)) {
  55743. /* XXX: use duk_is_valid_index() instead? */
  55744. /* XXX: improve check; check against nregs, not against top */
  55745. DUK__INTERNAL_ERROR("NEW out of bounds");
  55746. }
  55747. #endif
  55748. duk_require_stack(ctx, (duk_idx_t) c);
  55749. duk_push_tval(ctx, DUK__REGP(idx));
  55750. for (i = 0; i < c; i++) {
  55751. duk_push_tval(ctx, DUK__REGP(idx + i + 1));
  55752. }
  55753. duk_new(ctx, (duk_idx_t) c); /* [... constructor arg1 ... argN] -> [retval] */
  55754. DUK_DDD(DUK_DDDPRINT("NEW -> %!iT", (duk_tval *) duk_get_tval(ctx, -1)));
  55755. duk_replace(ctx, (duk_idx_t) idx);
  55756. break;
  55757. }
  55758. case DUK_OP_REGEXP: {
  55759. #ifdef DUK_USE_REGEXP_SUPPORT
  55760. duk_context *ctx = (duk_context *) thr;
  55761. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  55762. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  55763. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  55764. /* A -> target register
  55765. * B -> bytecode (also contains flags)
  55766. * C -> escaped source
  55767. */
  55768. duk_push_tval(ctx, DUK__REGCONSTP(c));
  55769. duk_push_tval(ctx, DUK__REGCONSTP(b)); /* -> [ ... escaped_source bytecode ] */
  55770. duk_regexp_create_instance(thr); /* -> [ ... regexp_instance ] */
  55771. DUK_DDD(DUK_DDDPRINT("regexp instance: %!iT", (duk_tval *) duk_get_tval(ctx, -1)));
  55772. duk_replace(ctx, (duk_idx_t) a);
  55773. #else
  55774. /* The compiler should never emit DUK_OP_REGEXP if there is no
  55775. * regexp support.
  55776. */
  55777. DUK__INTERNAL_ERROR("no regexp support");
  55778. #endif
  55779. break;
  55780. }
  55781. case DUK_OP_CSREG:
  55782. case DUK_OP_CSREGI: {
  55783. /*
  55784. * Assuming a register binds to a variable declared within this
  55785. * function (a declarative binding), the 'this' for the call
  55786. * setup is always 'undefined'. E5 Section 10.2.1.1.6.
  55787. */
  55788. duk_context *ctx = (duk_context *) thr;
  55789. duk_small_uint_fast_t b = DUK_DEC_B(ins); /* restricted to regs */
  55790. duk_uint_fast_t idx;
  55791. /* A -> target register (A, A+1) for call setup
  55792. * (for DUK_OP_CSREGI, 'a' is indirect)
  55793. * B -> register containing target function (not type checked here)
  55794. */
  55795. /* XXX: direct manipulation, or duk_replace_tval() */
  55796. /* Note: target registers a and a+1 may overlap with DUK__REGP(b).
  55797. * Careful here.
  55798. */
  55799. idx = (duk_uint_fast_t) DUK_DEC_A(ins);
  55800. if (DUK_DEC_OP(ins) == DUK_OP_CSREGI) {
  55801. duk_tval *tv_ind = DUK__REGP(idx);
  55802. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind));
  55803. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind);
  55804. }
  55805. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  55806. if (idx + 2 > (duk_uint_fast_t) duk_get_top(ctx)) {
  55807. /* XXX: use duk_is_valid_index() instead? */
  55808. /* XXX: improve check; check against nregs, not against top */
  55809. DUK__INTERNAL_ERROR("CSREG out of bounds");
  55810. }
  55811. #endif
  55812. duk_push_tval(ctx, DUK__REGP(b));
  55813. duk_replace(ctx, (duk_idx_t) idx);
  55814. duk_push_undefined(ctx);
  55815. duk_replace(ctx, (duk_idx_t) (idx + 1));
  55816. break;
  55817. }
  55818. case DUK_OP_GETVAR: {
  55819. duk_context *ctx = (duk_context *) thr;
  55820. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  55821. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  55822. duk_tval *tv1;
  55823. duk_hstring *name;
  55824. tv1 = DUK__CONSTP(bc);
  55825. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  55826. name = DUK_TVAL_GET_STRING(tv1);
  55827. DUK_ASSERT(name != NULL);
  55828. DUK_DDD(DUK_DDDPRINT("GETVAR: '%!O'", (duk_heaphdr *) name));
  55829. (void) duk_js_getvar_activation(thr, act, name, 1 /*throw*/); /* -> [... val this] */
  55830. duk_pop(ctx); /* 'this' binding is not needed here */
  55831. duk_replace(ctx, (duk_idx_t) a);
  55832. break;
  55833. }
  55834. case DUK_OP_PUTVAR: {
  55835. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  55836. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  55837. duk_tval *tv1;
  55838. duk_hstring *name;
  55839. tv1 = DUK__CONSTP(bc);
  55840. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  55841. name = DUK_TVAL_GET_STRING(tv1);
  55842. DUK_ASSERT(name != NULL);
  55843. /* XXX: putvar takes a duk_tval pointer, which is awkward and
  55844. * should be reworked.
  55845. */
  55846. tv1 = DUK__REGP(a); /* val */
  55847. duk_js_putvar_activation(thr, act, name, tv1, DUK__STRICT());
  55848. break;
  55849. }
  55850. case DUK_OP_DECLVAR: {
  55851. duk_context *ctx = (duk_context *) thr;
  55852. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  55853. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  55854. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  55855. duk_tval *tv1;
  55856. duk_hstring *name;
  55857. duk_small_uint_t prop_flags;
  55858. duk_bool_t is_func_decl;
  55859. duk_bool_t is_undef_value;
  55860. tv1 = DUK__REGCONSTP(b);
  55861. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  55862. name = DUK_TVAL_GET_STRING(tv1);
  55863. DUK_ASSERT(name != NULL);
  55864. is_undef_value = ((a & DUK_BC_DECLVAR_FLAG_UNDEF_VALUE) != 0);
  55865. is_func_decl = ((a & DUK_BC_DECLVAR_FLAG_FUNC_DECL) != 0);
  55866. /* XXX: declvar takes an duk_tval pointer, which is awkward and
  55867. * should be reworked.
  55868. */
  55869. /* Compiler is responsible for selecting property flags (configurability,
  55870. * writability, etc).
  55871. */
  55872. prop_flags = a & DUK_PROPDESC_FLAGS_MASK;
  55873. if (is_undef_value) {
  55874. duk_push_undefined(ctx);
  55875. } else {
  55876. duk_push_tval(ctx, DUK__REGCONSTP(c));
  55877. }
  55878. tv1 = duk_get_tval(ctx, -1);
  55879. if (duk_js_declvar_activation(thr, act, name, tv1, prop_flags, is_func_decl)) {
  55880. /* already declared, must update binding value */
  55881. tv1 = duk_get_tval(ctx, -1);
  55882. duk_js_putvar_activation(thr, act, name, tv1, DUK__STRICT());
  55883. }
  55884. duk_pop(ctx);
  55885. break;
  55886. }
  55887. case DUK_OP_DELVAR: {
  55888. duk_context *ctx = (duk_context *) thr;
  55889. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  55890. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  55891. duk_tval *tv1;
  55892. duk_hstring *name;
  55893. duk_bool_t rc;
  55894. tv1 = DUK__REGCONSTP(b);
  55895. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  55896. name = DUK_TVAL_GET_STRING(tv1);
  55897. DUK_ASSERT(name != NULL);
  55898. DUK_DDD(DUK_DDDPRINT("DELVAR '%!O'", (duk_heaphdr *) name));
  55899. rc = duk_js_delvar_activation(thr, act, name);
  55900. duk_push_boolean(ctx, rc);
  55901. duk_replace(ctx, (duk_idx_t) a);
  55902. break;
  55903. }
  55904. case DUK_OP_CSVAR:
  55905. case DUK_OP_CSVARI: {
  55906. /* 'this' value:
  55907. * E5 Section 6.b.i
  55908. *
  55909. * The only (standard) case where the 'this' binding is non-null is when
  55910. * (1) the variable is found in an object environment record, and
  55911. * (2) that object environment record is a 'with' block.
  55912. *
  55913. */
  55914. duk_context *ctx = (duk_context *) thr;
  55915. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  55916. duk_uint_fast_t idx;
  55917. duk_tval *tv1;
  55918. duk_hstring *name;
  55919. tv1 = DUK__REGCONSTP(b);
  55920. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  55921. name = DUK_TVAL_GET_STRING(tv1);
  55922. DUK_ASSERT(name != NULL);
  55923. (void) duk_js_getvar_activation(thr, act, name, 1 /*throw*/); /* -> [... val this] */
  55924. /* Note: target registers a and a+1 may overlap with DUK__REGCONSTP(b)
  55925. * and DUK__REGCONSTP(c). Careful here.
  55926. */
  55927. idx = (duk_uint_fast_t) DUK_DEC_A(ins);
  55928. if (DUK_DEC_OP(ins) == DUK_OP_CSVARI) {
  55929. duk_tval *tv_ind = DUK__REGP(idx);
  55930. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind));
  55931. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind);
  55932. }
  55933. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  55934. if (idx + 2 > (duk_uint_fast_t) duk_get_top(ctx)) {
  55935. /* XXX: use duk_is_valid_index() instead? */
  55936. /* XXX: improve check; check against nregs, not against top */
  55937. DUK__INTERNAL_ERROR("CSVAR out of bounds");
  55938. }
  55939. #endif
  55940. duk_replace(ctx, (duk_idx_t) (idx + 1)); /* 'this' binding */
  55941. duk_replace(ctx, (duk_idx_t) idx); /* variable value (function, we hope, not checked here) */
  55942. break;
  55943. }
  55944. case DUK_OP_CLOSURE: {
  55945. duk_context *ctx = (duk_context *) thr;
  55946. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  55947. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  55948. duk_hobject *fun_temp;
  55949. /* A -> target reg
  55950. * BC -> inner function index
  55951. */
  55952. DUK_DDD(DUK_DDDPRINT("CLOSURE to target register %ld, fnum %ld (count %ld)",
  55953. (long) a, (long) bc, (long) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(thr->heap, fun)));
  55954. DUK_ASSERT_DISABLE(bc >= 0); /* unsigned */
  55955. DUK_ASSERT((duk_uint_t) bc < (duk_uint_t) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(thr->heap, fun));
  55956. fun_temp = DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, fun)[bc];
  55957. DUK_ASSERT(fun_temp != NULL);
  55958. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(fun_temp));
  55959. DUK_DDD(DUK_DDDPRINT("CLOSURE: function template is: %p -> %!O",
  55960. (void *) fun_temp, (duk_heaphdr *) fun_temp));
  55961. if (act->lex_env == NULL) {
  55962. DUK_ASSERT(act->var_env == NULL);
  55963. duk_js_init_activation_environment_records_delayed(thr, act);
  55964. }
  55965. DUK_ASSERT(act->lex_env != NULL);
  55966. DUK_ASSERT(act->var_env != NULL);
  55967. /* functions always have a NEWENV flag, i.e. they get a
  55968. * new variable declaration environment, so only lex_env
  55969. * matters here.
  55970. */
  55971. duk_js_push_closure(thr,
  55972. (duk_hcompiledfunction *) fun_temp,
  55973. act->var_env,
  55974. act->lex_env);
  55975. duk_replace(ctx, (duk_idx_t) a);
  55976. break;
  55977. }
  55978. case DUK_OP_GETPROP: {
  55979. duk_context *ctx = (duk_context *) thr;
  55980. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  55981. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  55982. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  55983. duk_tval *tv_obj;
  55984. duk_tval *tv_key;
  55985. duk_bool_t rc;
  55986. /* A -> target reg
  55987. * B -> object reg/const (may be const e.g. in "'foo'[1]")
  55988. * C -> key reg/const
  55989. */
  55990. tv_obj = DUK__REGCONSTP(b);
  55991. tv_key = DUK__REGCONSTP(c);
  55992. DUK_DDD(DUK_DDDPRINT("GETPROP: a=%ld obj=%!T, key=%!T",
  55993. (long) a,
  55994. (duk_tval *) DUK__REGCONSTP(b),
  55995. (duk_tval *) DUK__REGCONSTP(c)));
  55996. rc = duk_hobject_getprop(thr, tv_obj, tv_key); /* -> [val] */
  55997. DUK_UNREF(rc); /* ignore */
  55998. DUK_DDD(DUK_DDDPRINT("GETPROP --> %!T",
  55999. (duk_tval *) duk_get_tval(ctx, -1)));
  56000. tv_obj = NULL; /* invalidated */
  56001. tv_key = NULL; /* invalidated */
  56002. duk_replace(ctx, (duk_idx_t) a); /* val */
  56003. break;
  56004. }
  56005. case DUK_OP_PUTPROP: {
  56006. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  56007. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56008. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  56009. duk_tval *tv_obj;
  56010. duk_tval *tv_key;
  56011. duk_tval *tv_val;
  56012. duk_bool_t rc;
  56013. /* A -> object reg
  56014. * B -> key reg/const
  56015. * C -> value reg/const
  56016. *
  56017. * Note: intentional difference to register arrangement
  56018. * of e.g. GETPROP; 'A' must contain a register-only value.
  56019. */
  56020. tv_obj = DUK__REGP(a);
  56021. tv_key = DUK__REGCONSTP(b);
  56022. tv_val = DUK__REGCONSTP(c);
  56023. DUK_DDD(DUK_DDDPRINT("PUTPROP: obj=%!T, key=%!T, val=%!T",
  56024. (duk_tval *) DUK__REGP(a),
  56025. (duk_tval *) DUK__REGCONSTP(b),
  56026. (duk_tval *) DUK__REGCONSTP(c)));
  56027. rc = duk_hobject_putprop(thr, tv_obj, tv_key, tv_val, DUK__STRICT());
  56028. DUK_UNREF(rc); /* ignore */
  56029. DUK_DDD(DUK_DDDPRINT("PUTPROP --> obj=%!T, key=%!T, val=%!T",
  56030. (duk_tval *) DUK__REGP(a),
  56031. (duk_tval *) DUK__REGCONSTP(b),
  56032. (duk_tval *) DUK__REGCONSTP(c)));
  56033. tv_obj = NULL; /* invalidated */
  56034. tv_key = NULL; /* invalidated */
  56035. tv_val = NULL; /* invalidated */
  56036. break;
  56037. }
  56038. case DUK_OP_DELPROP: {
  56039. duk_context *ctx = (duk_context *) thr;
  56040. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  56041. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56042. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  56043. duk_tval *tv_obj;
  56044. duk_tval *tv_key;
  56045. duk_bool_t rc;
  56046. /* A -> result reg
  56047. * B -> object reg
  56048. * C -> key reg/const
  56049. */
  56050. tv_obj = DUK__REGP(b);
  56051. tv_key = DUK__REGCONSTP(c);
  56052. rc = duk_hobject_delprop(thr, tv_obj, tv_key, DUK__STRICT());
  56053. tv_obj = NULL; /* invalidated */
  56054. tv_key = NULL; /* invalidated */
  56055. duk_push_boolean(ctx, rc);
  56056. duk_replace(ctx, (duk_idx_t) a); /* result */
  56057. break;
  56058. }
  56059. case DUK_OP_CSPROP:
  56060. case DUK_OP_CSPROPI: {
  56061. duk_context *ctx = (duk_context *) thr;
  56062. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56063. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  56064. duk_uint_fast_t idx;
  56065. duk_tval *tv_obj;
  56066. duk_tval *tv_key;
  56067. duk_bool_t rc;
  56068. /* E5 Section 11.2.3, step 6.a.i */
  56069. /* E5 Section 10.4.3 */
  56070. /* XXX: allow object to be a const, e.g. in 'foo'.toString()?
  56071. * On the other hand, DUK_REGCONSTP() is slower and generates
  56072. * more code.
  56073. */
  56074. tv_obj = DUK__REGP(b);
  56075. tv_key = DUK__REGCONSTP(c);
  56076. rc = duk_hobject_getprop(thr, tv_obj, tv_key); /* -> [val] */
  56077. DUK_UNREF(rc); /* unused */
  56078. tv_obj = NULL; /* invalidated */
  56079. tv_key = NULL; /* invalidated */
  56080. /* Note: target registers a and a+1 may overlap with DUK__REGP(b)
  56081. * and DUK__REGCONSTP(c). Careful here.
  56082. */
  56083. idx = (duk_uint_fast_t) DUK_DEC_A(ins);
  56084. if (DUK_DEC_OP(ins) == DUK_OP_CSPROPI) {
  56085. duk_tval *tv_ind = DUK__REGP(idx);
  56086. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind));
  56087. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind);
  56088. }
  56089. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  56090. if (idx + 2 > (duk_uint_fast_t) duk_get_top(ctx)) {
  56091. /* XXX: use duk_is_valid_index() instead? */
  56092. /* XXX: improve check; check against nregs, not against top */
  56093. DUK__INTERNAL_ERROR("CSPROP out of bounds");
  56094. }
  56095. #endif
  56096. duk_push_tval(ctx, DUK__REGP(b)); /* [ ... val obj ] */
  56097. duk_replace(ctx, (duk_idx_t) (idx + 1)); /* 'this' binding */
  56098. duk_replace(ctx, (duk_idx_t) idx); /* val */
  56099. break;
  56100. }
  56101. case DUK_OP_ADD:
  56102. case DUK_OP_SUB:
  56103. case DUK_OP_MUL:
  56104. case DUK_OP_DIV:
  56105. case DUK_OP_MOD: {
  56106. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  56107. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56108. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  56109. duk_small_uint_fast_t op = DUK_DEC_OP(ins);
  56110. if (op == DUK_OP_ADD) {
  56111. /*
  56112. * Handling DUK_OP_ADD this way is more compact (experimentally)
  56113. * than a separate case with separate argument decoding.
  56114. */
  56115. duk__vm_arith_add(thr, DUK__REGCONSTP(b), DUK__REGCONSTP(c), a);
  56116. } else {
  56117. duk__vm_arith_binary_op(thr, DUK__REGCONSTP(b), DUK__REGCONSTP(c), a, op);
  56118. }
  56119. break;
  56120. }
  56121. case DUK_OP_BAND:
  56122. case DUK_OP_BOR:
  56123. case DUK_OP_BXOR:
  56124. case DUK_OP_BASL:
  56125. case DUK_OP_BLSR:
  56126. case DUK_OP_BASR: {
  56127. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  56128. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56129. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  56130. duk_small_uint_fast_t op = DUK_DEC_OP(ins);
  56131. duk__vm_bitwise_binary_op(thr, DUK__REGCONSTP(b), DUK__REGCONSTP(c), a, op);
  56132. break;
  56133. }
  56134. case DUK_OP_EQ:
  56135. case DUK_OP_NEQ: {
  56136. duk_context *ctx = (duk_context *) thr;
  56137. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  56138. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56139. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  56140. duk_bool_t tmp;
  56141. /* E5 Sections 11.9.1, 11.9.3 */
  56142. tmp = duk_js_equals(thr, DUK__REGCONSTP(b), DUK__REGCONSTP(c));
  56143. if (DUK_DEC_OP(ins) == DUK_OP_NEQ) {
  56144. tmp = !tmp;
  56145. }
  56146. duk_push_boolean(ctx, tmp);
  56147. duk_replace(ctx, (duk_idx_t) a);
  56148. break;
  56149. }
  56150. case DUK_OP_SEQ:
  56151. case DUK_OP_SNEQ: {
  56152. duk_context *ctx = (duk_context *) thr;
  56153. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  56154. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56155. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  56156. duk_bool_t tmp;
  56157. /* E5 Sections 11.9.1, 11.9.3 */
  56158. tmp = duk_js_strict_equals(DUK__REGCONSTP(b), DUK__REGCONSTP(c));
  56159. if (DUK_DEC_OP(ins) == DUK_OP_SNEQ) {
  56160. tmp = !tmp;
  56161. }
  56162. duk_push_boolean(ctx, tmp);
  56163. duk_replace(ctx, (duk_idx_t) a);
  56164. break;
  56165. }
  56166. /* Note: combining comparison ops must be done carefully because
  56167. * of uncomparable values (NaN): it's not necessarily true that
  56168. * (x >= y) === !(x < y). Also, evaluation order matters, and
  56169. * although it would only seem to affect the compiler this is
  56170. * actually not the case, because there are also run-time coercions
  56171. * of the arguments (with potential side effects).
  56172. *
  56173. * XXX: can be combined; check code size.
  56174. */
  56175. case DUK_OP_GT: {
  56176. duk_context *ctx = (duk_context *) thr;
  56177. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  56178. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56179. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  56180. duk_bool_t tmp;
  56181. /* x > y --> y < x */
  56182. tmp = duk_js_compare_helper(thr,
  56183. DUK__REGCONSTP(c), /* y */
  56184. DUK__REGCONSTP(b), /* x */
  56185. 0); /* flags */
  56186. duk_push_boolean(ctx, tmp);
  56187. duk_replace(ctx, (duk_idx_t) a);
  56188. break;
  56189. }
  56190. case DUK_OP_GE: {
  56191. duk_context *ctx = (duk_context *) thr;
  56192. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  56193. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56194. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  56195. duk_bool_t tmp;
  56196. /* x >= y --> not (x < y) */
  56197. tmp = duk_js_compare_helper(thr,
  56198. DUK__REGCONSTP(b), /* x */
  56199. DUK__REGCONSTP(c), /* y */
  56200. DUK_COMPARE_FLAG_EVAL_LEFT_FIRST |
  56201. DUK_COMPARE_FLAG_NEGATE); /* flags */
  56202. duk_push_boolean(ctx, tmp);
  56203. duk_replace(ctx, (duk_idx_t) a);
  56204. break;
  56205. }
  56206. case DUK_OP_LT: {
  56207. duk_context *ctx = (duk_context *) thr;
  56208. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  56209. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56210. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  56211. duk_bool_t tmp;
  56212. /* x < y */
  56213. tmp = duk_js_compare_helper(thr,
  56214. DUK__REGCONSTP(b), /* x */
  56215. DUK__REGCONSTP(c), /* y */
  56216. DUK_COMPARE_FLAG_EVAL_LEFT_FIRST); /* flags */
  56217. duk_push_boolean(ctx, tmp);
  56218. duk_replace(ctx, (duk_idx_t) a);
  56219. break;
  56220. }
  56221. case DUK_OP_LE: {
  56222. duk_context *ctx = (duk_context *) thr;
  56223. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  56224. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56225. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  56226. duk_bool_t tmp;
  56227. /* x <= y --> not (x > y) --> not (y < x) */
  56228. tmp = duk_js_compare_helper(thr,
  56229. DUK__REGCONSTP(c), /* y */
  56230. DUK__REGCONSTP(b), /* x */
  56231. DUK_COMPARE_FLAG_NEGATE); /* flags */
  56232. duk_push_boolean(ctx, tmp);
  56233. duk_replace(ctx, (duk_idx_t) a);
  56234. break;
  56235. }
  56236. case DUK_OP_IF: {
  56237. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  56238. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56239. duk_bool_t tmp;
  56240. tmp = duk_js_toboolean(DUK__REGCONSTP(b));
  56241. if (tmp == (duk_bool_t) a) {
  56242. /* if boolean matches A, skip next inst */
  56243. act->pc++;
  56244. } else {
  56245. ;
  56246. }
  56247. break;
  56248. }
  56249. case DUK_OP_JUMP: {
  56250. duk_int_fast_t abc = DUK_DEC_ABC(ins);
  56251. act->pc += abc - DUK_BC_JUMP_BIAS;
  56252. break;
  56253. }
  56254. case DUK_OP_RETURN: {
  56255. duk_context *ctx = (duk_context *) thr;
  56256. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  56257. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56258. /* duk_small_uint_fast_t c = DUK_DEC_C(ins); */
  56259. duk_tval *tv_val;
  56260. /* A -> flags
  56261. * B -> return value reg/const
  56262. * C -> currently unused
  56263. */
  56264. /* A fast return avoids full longjmp handling for a set of
  56265. * scenarios which hopefully represents the common cases.
  56266. * The compiler is responsible for emitting fast returns
  56267. * only when they are safe. Currently this means that there
  56268. * is nothing on the catch stack (not even label catchers).
  56269. * The speed advantage of fast returns (avoiding longjmp) is
  56270. * not very high, around 10-15%.
  56271. */
  56272. #if 0 /* XXX: Disabled for 1.0 release */
  56273. if (a & DUK_BC_RETURN_FLAG_FAST) {
  56274. DUK_DDD(DUK_DDDPRINT("FASTRETURN attempt a=%ld b=%ld", (long) a, (long) b));
  56275. if (duk__handle_fast_return(thr,
  56276. (a & DUK_BC_RETURN_FLAG_HAVE_RETVAL) ? DUK__REGCONSTP(b) : NULL,
  56277. entry_thread,
  56278. entry_callstack_top)) {
  56279. DUK_DDD(DUK_DDDPRINT("FASTRETURN success a=%ld b=%ld", (long) a, (long) b));
  56280. goto restart_execution;
  56281. }
  56282. }
  56283. #endif
  56284. /* No fast return, slow path. */
  56285. DUK_DDD(DUK_DDDPRINT("SLOWRETURN a=%ld b=%ld", (long) a, (long) b));
  56286. if (a & DUK_BC_RETURN_FLAG_HAVE_RETVAL) {
  56287. tv_val = DUK__REGCONSTP(b);
  56288. #if defined(DUK_OPT_FASTINT)
  56289. /* Explicit check for fastint downgrade. Do
  56290. * it also for consts for now, which is odd
  56291. * but harmless.
  56292. */
  56293. /* XXX: restrict to reg values only? */
  56294. DUK_TVAL_CHKFAST_INPLACE(tv_val);
  56295. #endif
  56296. duk_push_tval(ctx, tv_val);
  56297. } else {
  56298. duk_push_undefined(ctx);
  56299. }
  56300. duk_err_setup_heap_ljstate(thr, DUK_LJ_TYPE_RETURN);
  56301. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* in bytecode executor, should always be set */
  56302. duk_err_longjmp(thr);
  56303. DUK_UNREACHABLE();
  56304. break;
  56305. }
  56306. case DUK_OP_CALL:
  56307. case DUK_OP_CALLI: {
  56308. duk_context *ctx = (duk_context *) thr;
  56309. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  56310. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  56311. duk_uint_fast_t idx;
  56312. duk_small_uint_t call_flags;
  56313. duk_small_uint_t flag_tailcall;
  56314. duk_small_uint_t flag_evalcall;
  56315. duk_tval *tv_func;
  56316. duk_hobject *obj_func;
  56317. duk_bool_t setup_rc;
  56318. duk_idx_t num_stack_args;
  56319. /* A -> flags
  56320. * B -> base register for call (base -> func, base+1 -> this, base+2 -> arg1 ... base+2+N-1 -> argN)
  56321. * (for DUK_OP_CALLI, 'b' is indirect)
  56322. * C -> nargs
  56323. */
  56324. /* these are not necessarily 0 or 1 (may be other non-zero), that's ok */
  56325. flag_tailcall = (a & DUK_BC_CALL_FLAG_TAILCALL);
  56326. flag_evalcall = (a & DUK_BC_CALL_FLAG_EVALCALL);
  56327. idx = (duk_uint_fast_t) DUK_DEC_B(ins);
  56328. if (DUK_DEC_OP(ins) == DUK_OP_CALLI) {
  56329. duk_tval *tv_ind = DUK__REGP(idx);
  56330. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind));
  56331. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind);
  56332. }
  56333. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  56334. if (!duk_is_valid_index(ctx, (duk_idx_t) idx)) {
  56335. /* XXX: improve check; check against nregs, not against top */
  56336. DUK__INTERNAL_ERROR("CALL out of bounds");
  56337. }
  56338. #endif
  56339. /*
  56340. * To determine whether to use an optimized Ecmascript-to-Ecmascript
  56341. * call, we need to know whether the final, non-bound function is an
  56342. * Ecmascript function.
  56343. *
  56344. * This is now implemented so that we start to do an ecma-to-ecma call
  56345. * setup which will resolve the bound chain as the first thing. If the
  56346. * final function is not eligible, the return value indicates that the
  56347. * ecma-to-ecma call is not possible. The setup will overwrite the call
  56348. * target at DUK__REGP(idx) with the final, non-bound function (which
  56349. * may be a lightfunc), and fudge arguments if necessary.
  56350. *
  56351. * XXX: If an ecma-to-ecma call is not possible, this initial call
  56352. * setup will do bound function chain resolution but won't do the
  56353. * "effective this binding" resolution which is quite confusing.
  56354. * Perhaps add a helper for doing bound function and effective this
  56355. * binding resolution - and call that explicitly? Ecma-to-ecma call
  56356. * setup and normal function handling can then assume this prestep has
  56357. * been done by the caller.
  56358. */
  56359. duk_set_top(ctx, (duk_idx_t) (idx + c + 2)); /* [ ... func this arg1 ... argN ] */
  56360. call_flags = 0;
  56361. if (flag_tailcall) {
  56362. /* We request a tailcall, but in some corner cases
  56363. * call handling can decide that a tailcall is
  56364. * actually not possible.
  56365. * See: test-bug-tailcall-preventyield-assert.c.
  56366. */
  56367. call_flags |= DUK_CALL_FLAG_IS_TAILCALL;
  56368. }
  56369. /* Compared to duk_handle_call():
  56370. * - protected call: never
  56371. * - ignore recursion limit: never
  56372. */
  56373. num_stack_args = c;
  56374. setup_rc = duk_handle_ecma_call_setup(thr,
  56375. num_stack_args,
  56376. call_flags);
  56377. if (setup_rc) {
  56378. /* Ecma-to-ecma call possible, may or may not be a tailcall.
  56379. * Avoid C recursion by being clever.
  56380. */
  56381. DUK_DDD(DUK_DDDPRINT("ecma-to-ecma call setup possible, restart execution"));
  56382. goto restart_execution;
  56383. }
  56384. DUK_DDD(DUK_DDDPRINT("ecma-to-ecma call not possible, target is native (may be lightfunc)"));
  56385. /* Recompute argument count: bound function handling may have shifted. */
  56386. num_stack_args = duk_get_top(ctx) - (idx + 2);
  56387. DUK_DDD(DUK_DDDPRINT("recomputed arg count: %ld\n", (long) num_stack_args));
  56388. tv_func = DUK__REGP(idx); /* Relookup if relocated */
  56389. if (DUK_TVAL_IS_LIGHTFUNC(tv_func)) {
  56390. call_flags = 0; /* not protected, respect reclimit, not constructor */
  56391. /* There is no eval() special handling here: eval() is never
  56392. * automatically converted to a lightfunc.
  56393. */
  56394. DUK_ASSERT(DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv_func) != duk_bi_global_object_eval);
  56395. duk_handle_call(thr,
  56396. num_stack_args,
  56397. call_flags);
  56398. /* duk_js_call.c is required to restore the stack reserve
  56399. * so we only need to reset the top.
  56400. */
  56401. duk_set_top(ctx, (duk_idx_t) fun->nregs);
  56402. /* No need to reinit setjmp() catchpoint, as call handling
  56403. * will store and restore our state.
  56404. */
  56405. } else {
  56406. /* Call setup checks callability. */
  56407. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv_func));
  56408. obj_func = DUK_TVAL_GET_OBJECT(tv_func);
  56409. DUK_ASSERT(obj_func != NULL);
  56410. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(obj_func));
  56411. /*
  56412. * Other cases, use C recursion.
  56413. *
  56414. * If a tailcall was requested we ignore it and execute a normal call.
  56415. * Since Duktape 0.11.0 the compiler emits a RETURN opcode even after
  56416. * a tailcall to avoid test-bug-tailcall-thread-yield-resume.js.
  56417. *
  56418. * Direct eval call: (1) call target (before following bound function
  56419. * chain) is the built-in eval() function, and (2) call was made with
  56420. * the identifier 'eval'.
  56421. */
  56422. call_flags = 0; /* not protected, respect reclimit, not constructor */
  56423. if (DUK_HOBJECT_IS_NATIVEFUNCTION(obj_func) &&
  56424. ((duk_hnativefunction *) obj_func)->func == duk_bi_global_object_eval) {
  56425. if (flag_evalcall) {
  56426. DUK_DDD(DUK_DDDPRINT("call target is eval, call identifier was 'eval' -> direct eval"));
  56427. call_flags |= DUK_CALL_FLAG_DIRECT_EVAL;
  56428. } else {
  56429. DUK_DDD(DUK_DDDPRINT("call target is eval, call identifier was not 'eval' -> indirect eval"));
  56430. }
  56431. }
  56432. duk_handle_call(thr,
  56433. num_stack_args,
  56434. call_flags);
  56435. /* duk_js_call.c is required to restore the stack reserve
  56436. * so we only need to reset the top.
  56437. */
  56438. duk_set_top(ctx, (duk_idx_t) fun->nregs);
  56439. /* No need to reinit setjmp() catchpoint, as call handling
  56440. * will store and restore our state.
  56441. */
  56442. }
  56443. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  56444. /* When debugger is enabled, we need to recheck the activation
  56445. * status after returning.
  56446. */
  56447. goto restart_execution;
  56448. #endif
  56449. break;
  56450. }
  56451. case DUK_OP_TRYCATCH: {
  56452. duk_context *ctx = (duk_context *) thr;
  56453. duk_catcher *cat;
  56454. duk_tval *tv1;
  56455. duk_small_uint_fast_t a;
  56456. duk_small_uint_fast_t b;
  56457. duk_small_uint_fast_t c;
  56458. /* A -> flags
  56459. * B -> reg_catch; base register for 2 regs
  56460. * C -> semantics depend on flags: var_name or with_target
  56461. *
  56462. * If DUK_BC_TRYCATCH_FLAG_CATCH_BINDING set:
  56463. * C is constant index for catch binding variable name.
  56464. * Automatic declarative environment is established for
  56465. * the duration of the 'catch' clause.
  56466. *
  56467. * If DUK_BC_TRYCATCH_FLAG_WITH_BINDING set:
  56468. * C is reg/const index for with 'target value', which
  56469. * is coerced to an object and then used as a binding
  56470. * object for an environment record. The binding is
  56471. * initialized here, for the 'try' clause.
  56472. *
  56473. * Note that a TRYCATCH generated for a 'with' statement has no
  56474. * catch or finally parts.
  56475. */
  56476. /* XXX: side effect handling is quite awkward here */
  56477. DUK_DDD(DUK_DDDPRINT("TRYCATCH: reg_catch=%ld, var_name/with_target=%ld, have_catch=%ld, "
  56478. "have_finally=%ld, catch_binding=%ld, with_binding=%ld (flags=0x%02lx)",
  56479. (long) DUK_DEC_B(ins),
  56480. (long) DUK_DEC_C(ins),
  56481. (long) (DUK_DEC_A(ins) & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH ? 1 : 0),
  56482. (long) (DUK_DEC_A(ins) & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY ? 1 : 0),
  56483. (long) (DUK_DEC_A(ins) & DUK_BC_TRYCATCH_FLAG_CATCH_BINDING ? 1 : 0),
  56484. (long) (DUK_DEC_A(ins) & DUK_BC_TRYCATCH_FLAG_WITH_BINDING ? 1 : 0),
  56485. (unsigned long) DUK_DEC_A(ins)));
  56486. a = DUK_DEC_A(ins);
  56487. b = DUK_DEC_B(ins);
  56488. c = DUK_DEC_C(ins);
  56489. DUK_ASSERT(thr->callstack_top >= 1);
  56490. /* 'with' target must be created first, in case we run out of memory */
  56491. /* XXX: refactor out? */
  56492. if (a & DUK_BC_TRYCATCH_FLAG_WITH_BINDING) {
  56493. DUK_DDD(DUK_DDDPRINT("need to initialize a with binding object"));
  56494. if (act->lex_env == NULL) {
  56495. DUK_ASSERT(act->var_env == NULL);
  56496. DUK_DDD(DUK_DDDPRINT("delayed environment initialization"));
  56497. /* must relookup act in case of side effects */
  56498. duk_js_init_activation_environment_records_delayed(thr, act);
  56499. act = thr->callstack + thr->callstack_top - 1;
  56500. }
  56501. DUK_ASSERT(act->lex_env != NULL);
  56502. DUK_ASSERT(act->var_env != NULL);
  56503. (void) duk_push_object_helper(ctx,
  56504. DUK_HOBJECT_FLAG_EXTENSIBLE |
  56505. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJENV),
  56506. -1); /* no prototype, updated below */
  56507. duk_push_tval(ctx, DUK__REGCONSTP(c));
  56508. duk_to_object(ctx, -1);
  56509. duk_dup(ctx, -1);
  56510. /* [ ... env target ] */
  56511. /* [ ... env target target ] */
  56512. duk_xdef_prop_stridx(thr, -3, DUK_STRIDX_INT_TARGET, DUK_PROPDESC_FLAGS_NONE);
  56513. duk_xdef_prop_stridx(thr, -2, DUK_STRIDX_INT_THIS, DUK_PROPDESC_FLAGS_NONE); /* always provideThis=true */
  56514. /* [ ... env ] */
  56515. DUK_DDD(DUK_DDDPRINT("environment for with binding: %!iT",
  56516. (duk_tval *) duk_get_tval(ctx, -1)));
  56517. }
  56518. /* allocate catcher and populate it (should be atomic) */
  56519. duk_hthread_catchstack_grow(thr);
  56520. cat = thr->catchstack + thr->catchstack_top;
  56521. DUK_ASSERT(thr->catchstack_top + 1 <= thr->catchstack_size);
  56522. thr->catchstack_top++;
  56523. cat->flags = DUK_CAT_TYPE_TCF;
  56524. cat->h_varname = NULL;
  56525. if (a & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH) {
  56526. cat->flags |= DUK_CAT_FLAG_CATCH_ENABLED;
  56527. }
  56528. if (a & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY) {
  56529. cat->flags |= DUK_CAT_FLAG_FINALLY_ENABLED;
  56530. }
  56531. if (a & DUK_BC_TRYCATCH_FLAG_CATCH_BINDING) {
  56532. DUK_DDD(DUK_DDDPRINT("catch binding flag set to catcher"));
  56533. cat->flags |= DUK_CAT_FLAG_CATCH_BINDING_ENABLED;
  56534. tv1 = DUK__CONSTP(c);
  56535. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  56536. cat->h_varname = DUK_TVAL_GET_STRING(tv1);
  56537. } else if (a & DUK_BC_TRYCATCH_FLAG_WITH_BINDING) {
  56538. /* env created above to stack top */
  56539. duk_hobject *new_env;
  56540. DUK_DDD(DUK_DDDPRINT("lexenv active flag set to catcher"));
  56541. cat->flags |= DUK_CAT_FLAG_LEXENV_ACTIVE;
  56542. DUK_DDD(DUK_DDDPRINT("activating object env: %!iT",
  56543. (duk_tval *) duk_get_tval(ctx, -1)));
  56544. DUK_ASSERT(act->lex_env != NULL);
  56545. new_env = duk_get_hobject(ctx, -1);
  56546. DUK_ASSERT(new_env != NULL);
  56547. act = thr->callstack + thr->callstack_top - 1; /* relookup (side effects) */
  56548. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, new_env, act->lex_env);
  56549. act = thr->callstack + thr->callstack_top - 1; /* relookup (side effects) */
  56550. act->lex_env = new_env;
  56551. DUK_HOBJECT_INCREF(thr, new_env);
  56552. duk_pop(ctx);
  56553. } else {
  56554. ;
  56555. }
  56556. cat = thr->catchstack + thr->catchstack_top - 1; /* relookup (side effects) */
  56557. cat->callstack_index = thr->callstack_top - 1;
  56558. cat->pc_base = act->pc; /* pre-incremented, points to first jump slot */
  56559. cat->idx_base = (duk_size_t) (thr->valstack_bottom - thr->valstack) + b;
  56560. DUK_DDD(DUK_DDDPRINT("TRYCATCH catcher: flags=0x%08lx, callstack_index=%ld, pc_base=%ld, "
  56561. "idx_base=%ld, h_varname=%!O",
  56562. (unsigned long) cat->flags, (long) cat->callstack_index,
  56563. (long) cat->pc_base, (long) cat->idx_base, (duk_heaphdr *) cat->h_varname));
  56564. act->pc += 2; /* skip jump slots */
  56565. break;
  56566. }
  56567. /* Pre/post inc/dec for register variables, important for loops. */
  56568. case DUK_OP_PREINCR:
  56569. case DUK_OP_PREDECR:
  56570. case DUK_OP_POSTINCR:
  56571. case DUK_OP_POSTDECR: {
  56572. duk_context *ctx = (duk_context *) thr;
  56573. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  56574. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  56575. duk_tval *tv1, *tv2;
  56576. duk_tval tv_tmp;
  56577. duk_double_t x, y, z;
  56578. /* Two lowest bits of opcode are used to distinguish
  56579. * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
  56580. */
  56581. DUK_ASSERT((DUK_OP_PREINCR & 0x03) == 0x00);
  56582. DUK_ASSERT((DUK_OP_PREDECR & 0x03) == 0x01);
  56583. DUK_ASSERT((DUK_OP_POSTINCR & 0x03) == 0x02);
  56584. DUK_ASSERT((DUK_OP_POSTDECR & 0x03) == 0x03);
  56585. tv1 = DUK__REGP(bc);
  56586. #if defined(DUK_USE_FASTINT)
  56587. if (DUK_TVAL_IS_FASTINT(tv1)) {
  56588. duk_int64_t x_fi, y_fi, z_fi;
  56589. x_fi = DUK_TVAL_GET_FASTINT(tv1);
  56590. if (ins & DUK_ENC_OP(0x01)) {
  56591. if (x_fi == DUK_FASTINT_MIN) {
  56592. goto skip_fastint;
  56593. }
  56594. y_fi = x_fi - 1;
  56595. } else {
  56596. if (x_fi == DUK_FASTINT_MAX) {
  56597. goto skip_fastint;
  56598. }
  56599. y_fi = x_fi + 1;
  56600. }
  56601. DUK_TVAL_SET_FASTINT(tv1, y_fi); /* no need for refcount update */
  56602. tv2 = DUK__REGP(a);
  56603. DUK_TVAL_SET_TVAL(&tv_tmp, tv2);
  56604. z_fi = (ins & DUK_ENC_OP(0x02)) ? x_fi : y_fi;
  56605. DUK_TVAL_SET_FASTINT(tv2, z_fi); /* no need for incref */
  56606. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  56607. break;
  56608. }
  56609. skip_fastint:
  56610. #endif
  56611. if (DUK_TVAL_IS_NUMBER(tv1)) {
  56612. /* Fast path for the case where the register
  56613. * is a number (e.g. loop counter).
  56614. */
  56615. x = DUK_TVAL_GET_NUMBER(tv1);
  56616. if (ins & DUK_ENC_OP(0x01)) {
  56617. y = x - 1.0;
  56618. } else {
  56619. y = x + 1.0;
  56620. }
  56621. DUK_TVAL_SET_NUMBER(tv1, y); /* no need for refcount update */
  56622. } else {
  56623. x = duk_to_number(ctx, bc);
  56624. if (ins & DUK_ENC_OP(0x01)) {
  56625. y = x - 1.0;
  56626. } else {
  56627. y = x + 1.0;
  56628. }
  56629. duk_push_number(ctx, y);
  56630. duk_replace(ctx, bc);
  56631. }
  56632. tv2 = DUK__REGP(a);
  56633. DUK_TVAL_SET_TVAL(&tv_tmp, tv2);
  56634. z = (ins & DUK_ENC_OP(0x02)) ? x : y;
  56635. DUK_TVAL_SET_NUMBER(tv2, z); /* no need for incref */
  56636. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  56637. break;
  56638. }
  56639. /* Preinc/predec for var-by-name, slow path. */
  56640. case DUK_OP_PREINCV:
  56641. case DUK_OP_PREDECV:
  56642. case DUK_OP_POSTINCV:
  56643. case DUK_OP_POSTDECV: {
  56644. duk_context *ctx = (duk_context *) thr;
  56645. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  56646. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  56647. duk_double_t x, y;
  56648. duk_tval *tv1;
  56649. duk_hstring *name;
  56650. /* Two lowest bits of opcode are used to distinguish
  56651. * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
  56652. */
  56653. DUK_ASSERT((DUK_OP_PREINCV & 0x03) == 0x00);
  56654. DUK_ASSERT((DUK_OP_PREDECV & 0x03) == 0x01);
  56655. DUK_ASSERT((DUK_OP_POSTINCV & 0x03) == 0x02);
  56656. DUK_ASSERT((DUK_OP_POSTDECV & 0x03) == 0x03);
  56657. tv1 = DUK__CONSTP(bc);
  56658. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  56659. name = DUK_TVAL_GET_STRING(tv1);
  56660. DUK_ASSERT(name != NULL);
  56661. (void) duk_js_getvar_activation(thr, act, name, 1 /*throw*/); /* -> [... val this] */
  56662. /* XXX: fastint fast path would be very useful here */
  56663. x = duk_to_number(ctx, -2);
  56664. duk_pop_2(ctx);
  56665. if (ins & DUK_ENC_OP(0x01)) {
  56666. y = x - 1.0;
  56667. } else {
  56668. y = x + 1.0;
  56669. }
  56670. duk_push_number(ctx, y);
  56671. tv1 = duk_get_tval(ctx, -1);
  56672. DUK_ASSERT(tv1 != NULL);
  56673. duk_js_putvar_activation(thr, act, name, tv1, DUK__STRICT());
  56674. duk_pop(ctx);
  56675. duk_push_number(ctx, (ins & DUK_ENC_OP(0x02)) ? x : y);
  56676. duk_replace(ctx, (duk_idx_t) a);
  56677. break;
  56678. }
  56679. /* Preinc/predec for object properties. */
  56680. case DUK_OP_PREINCP:
  56681. case DUK_OP_PREDECP:
  56682. case DUK_OP_POSTINCP:
  56683. case DUK_OP_POSTDECP: {
  56684. duk_context *ctx = (duk_context *) thr;
  56685. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  56686. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56687. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  56688. duk_tval *tv_obj;
  56689. duk_tval *tv_key;
  56690. duk_tval *tv_val;
  56691. duk_bool_t rc;
  56692. duk_double_t x, y;
  56693. /* A -> target reg
  56694. * B -> object reg/const (may be const e.g. in "'foo'[1]")
  56695. * C -> key reg/const
  56696. */
  56697. /* Two lowest bits of opcode are used to distinguish
  56698. * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
  56699. */
  56700. DUK_ASSERT((DUK_OP_PREINCP & 0x03) == 0x00);
  56701. DUK_ASSERT((DUK_OP_PREDECP & 0x03) == 0x01);
  56702. DUK_ASSERT((DUK_OP_POSTINCP & 0x03) == 0x02);
  56703. DUK_ASSERT((DUK_OP_POSTDECP & 0x03) == 0x03);
  56704. tv_obj = DUK__REGCONSTP(b);
  56705. tv_key = DUK__REGCONSTP(c);
  56706. rc = duk_hobject_getprop(thr, tv_obj, tv_key); /* -> [val] */
  56707. DUK_UNREF(rc); /* ignore */
  56708. tv_obj = NULL; /* invalidated */
  56709. tv_key = NULL; /* invalidated */
  56710. x = duk_to_number(ctx, -1);
  56711. duk_pop(ctx);
  56712. if (ins & DUK_ENC_OP(0x01)) {
  56713. y = x - 1.0;
  56714. } else {
  56715. y = x + 1.0;
  56716. }
  56717. duk_push_number(ctx, y);
  56718. tv_val = duk_get_tval(ctx, -1);
  56719. DUK_ASSERT(tv_val != NULL);
  56720. tv_obj = DUK__REGCONSTP(b);
  56721. tv_key = DUK__REGCONSTP(c);
  56722. rc = duk_hobject_putprop(thr, tv_obj, tv_key, tv_val, DUK__STRICT());
  56723. DUK_UNREF(rc); /* ignore */
  56724. tv_obj = NULL; /* invalidated */
  56725. tv_key = NULL; /* invalidated */
  56726. duk_pop(ctx);
  56727. duk_push_number(ctx, (ins & DUK_ENC_OP(0x02)) ? x : y);
  56728. duk_replace(ctx, (duk_idx_t) a);
  56729. break;
  56730. }
  56731. case DUK_OP_EXTRA: {
  56732. /* XXX: shared decoding of 'b' and 'c'? */
  56733. duk_small_uint_fast_t extraop = DUK_DEC_A(ins);
  56734. switch ((int) extraop) {
  56735. /* XXX: switch cast? */
  56736. case DUK_EXTRAOP_NOP: {
  56737. /* nop */
  56738. break;
  56739. }
  56740. case DUK_EXTRAOP_INVALID: {
  56741. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, "INVALID opcode (%ld)", (long) DUK_DEC_BC(ins));
  56742. break;
  56743. }
  56744. case DUK_EXTRAOP_LDTHIS: {
  56745. /* Note: 'this' may be bound to any value, not just an object */
  56746. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  56747. duk_tval tv_tmp;
  56748. duk_tval *tv1, *tv2;
  56749. tv1 = DUK__REGP(bc);
  56750. tv2 = thr->valstack_bottom - 1; /* 'this binding' is just under bottom */
  56751. DUK_ASSERT(tv2 >= thr->valstack);
  56752. DUK_DDD(DUK_DDDPRINT("LDTHIS: %!T to r%ld", (duk_tval *) tv2, (long) bc));
  56753. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  56754. DUK_TVAL_SET_TVAL(tv1, tv2);
  56755. DUK_TVAL_INCREF(thr, tv1);
  56756. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  56757. break;
  56758. }
  56759. case DUK_EXTRAOP_LDUNDEF: {
  56760. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  56761. duk_tval tv_tmp;
  56762. duk_tval *tv1;
  56763. tv1 = DUK__REGP(bc);
  56764. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  56765. DUK_TVAL_SET_UNDEFINED_ACTUAL(tv1);
  56766. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  56767. break;
  56768. }
  56769. case DUK_EXTRAOP_LDNULL: {
  56770. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  56771. duk_tval tv_tmp;
  56772. duk_tval *tv1;
  56773. tv1 = DUK__REGP(bc);
  56774. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  56775. DUK_TVAL_SET_NULL(tv1);
  56776. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  56777. break;
  56778. }
  56779. case DUK_EXTRAOP_LDTRUE:
  56780. case DUK_EXTRAOP_LDFALSE: {
  56781. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  56782. duk_tval tv_tmp;
  56783. duk_tval *tv1;
  56784. duk_small_uint_fast_t bval = (extraop == DUK_EXTRAOP_LDTRUE ? 1 : 0);
  56785. tv1 = DUK__REGP(bc);
  56786. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  56787. DUK_TVAL_SET_BOOLEAN(tv1, bval);
  56788. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  56789. break;
  56790. }
  56791. case DUK_EXTRAOP_NEWOBJ: {
  56792. duk_context *ctx = (duk_context *) thr;
  56793. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56794. duk_push_object(ctx);
  56795. duk_replace(ctx, (duk_idx_t) b);
  56796. break;
  56797. }
  56798. case DUK_EXTRAOP_NEWARR: {
  56799. duk_context *ctx = (duk_context *) thr;
  56800. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56801. duk_push_array(ctx);
  56802. duk_replace(ctx, (duk_idx_t) b);
  56803. break;
  56804. }
  56805. case DUK_EXTRAOP_SETALEN: {
  56806. duk_small_uint_fast_t b;
  56807. duk_small_uint_fast_t c;
  56808. duk_tval *tv1;
  56809. duk_hobject *h;
  56810. duk_uint32_t len;
  56811. b = DUK_DEC_B(ins); tv1 = DUK__REGP(b);
  56812. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1));
  56813. h = DUK_TVAL_GET_OBJECT(tv1);
  56814. c = DUK_DEC_C(ins); tv1 = DUK__REGP(c);
  56815. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1));
  56816. len = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv1);
  56817. duk_hobject_set_length(thr, h, len);
  56818. break;
  56819. }
  56820. case DUK_EXTRAOP_TYPEOF: {
  56821. duk_context *ctx = (duk_context *) thr;
  56822. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  56823. duk_push_hstring(ctx, duk_js_typeof(thr, DUK__REGP(bc)));
  56824. duk_replace(ctx, (duk_idx_t) bc);
  56825. break;
  56826. }
  56827. case DUK_EXTRAOP_TYPEOFID: {
  56828. duk_context *ctx = (duk_context *) thr;
  56829. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56830. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  56831. duk_hstring *name;
  56832. duk_tval *tv;
  56833. /* B -> target register
  56834. * C -> constant index of identifier name
  56835. */
  56836. tv = DUK__REGCONSTP(c); /* XXX: this could be a DUK__CONSTP instead */
  56837. DUK_ASSERT(DUK_TVAL_IS_STRING(tv));
  56838. name = DUK_TVAL_GET_STRING(tv);
  56839. if (duk_js_getvar_activation(thr, act, name, 0 /*throw*/)) {
  56840. /* -> [... val this] */
  56841. tv = duk_get_tval(ctx, -2);
  56842. duk_push_hstring(ctx, duk_js_typeof(thr, tv));
  56843. duk_replace(ctx, (duk_idx_t) b);
  56844. duk_pop_2(ctx);
  56845. } else {
  56846. /* unresolvable, no stack changes */
  56847. duk_push_hstring_stridx(ctx, DUK_STRIDX_LC_UNDEFINED);
  56848. duk_replace(ctx, (duk_idx_t) b);
  56849. }
  56850. break;
  56851. }
  56852. case DUK_EXTRAOP_INITENUM: {
  56853. duk_context *ctx = (duk_context *) thr;
  56854. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56855. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  56856. /*
  56857. * Enumeration semantics come from for-in statement, E5 Section 12.6.4.
  56858. * If called with 'null' or 'undefined', this opcode returns 'null' as
  56859. * the enumerator, which is special cased in NEXTENUM. This simplifies
  56860. * the compiler part
  56861. */
  56862. /* B -> register for writing enumerator object
  56863. * C -> value to be enumerated (register)
  56864. */
  56865. if (duk_is_null_or_undefined(ctx, (duk_idx_t) c)) {
  56866. duk_push_null(ctx);
  56867. duk_replace(ctx, (duk_idx_t) b);
  56868. } else {
  56869. duk_dup(ctx, (duk_idx_t) c);
  56870. duk_to_object(ctx, -1);
  56871. duk_hobject_enumerator_create(ctx, 0 /*enum_flags*/); /* [ ... val ] --> [ ... enum ] */
  56872. duk_replace(ctx, (duk_idx_t) b);
  56873. }
  56874. break;
  56875. }
  56876. case DUK_EXTRAOP_NEXTENUM: {
  56877. duk_context *ctx = (duk_context *) thr;
  56878. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56879. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  56880. /*
  56881. * NEXTENUM checks whether the enumerator still has unenumerated
  56882. * keys. If so, the next key is loaded to the target register
  56883. * and the next instruction is skipped. Otherwise the next instruction
  56884. * will be executed, jumping out of the enumeration loop.
  56885. */
  56886. /* B -> target register for next key
  56887. * C -> enum register
  56888. */
  56889. DUK_DDD(DUK_DDDPRINT("NEXTENUM: b->%!T, c->%!T",
  56890. (duk_tval *) duk_get_tval(ctx, (duk_idx_t) b),
  56891. (duk_tval *) duk_get_tval(ctx, (duk_idx_t) c)));
  56892. if (duk_is_object(ctx, (duk_idx_t) c)) {
  56893. /* XXX: assert 'c' is an enumerator */
  56894. duk_dup(ctx, (duk_idx_t) c);
  56895. if (duk_hobject_enumerator_next(ctx, 0 /*get_value*/)) {
  56896. /* [ ... enum ] -> [ ... next_key ] */
  56897. DUK_DDD(DUK_DDDPRINT("enum active, next key is %!T, skip jump slot ",
  56898. (duk_tval *) duk_get_tval(ctx, -1)));
  56899. act->pc++;;
  56900. } else {
  56901. /* [ ... enum ] -> [ ... ] */
  56902. DUK_DDD(DUK_DDDPRINT("enum finished, execute jump slot"));
  56903. duk_push_undefined(ctx);
  56904. }
  56905. duk_replace(ctx, (duk_idx_t) b);
  56906. } else {
  56907. /* 'null' enumerator case -> behave as with an empty enumerator */
  56908. DUK_ASSERT(duk_is_null(ctx, (duk_idx_t) c));
  56909. DUK_DDD(DUK_DDDPRINT("enum is null, execute jump slot"));
  56910. }
  56911. break;
  56912. }
  56913. case DUK_EXTRAOP_INITSET:
  56914. case DUK_EXTRAOP_INITSETI:
  56915. case DUK_EXTRAOP_INITGET:
  56916. case DUK_EXTRAOP_INITGETI: {
  56917. duk_context *ctx = (duk_context *) thr;
  56918. duk_bool_t is_set = (extraop == DUK_EXTRAOP_INITSET || extraop == DUK_EXTRAOP_INITSETI);
  56919. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  56920. duk_uint_fast_t idx;
  56921. /* B -> object register
  56922. * C -> C+0 contains key, C+1 closure (value)
  56923. */
  56924. /*
  56925. * INITSET/INITGET are only used to initialize object literal keys.
  56926. * The compiler ensures that there cannot be a previous data property
  56927. * of the same name. It also ensures that setter and getter can only
  56928. * be initialized once (or not at all).
  56929. */
  56930. idx = (duk_uint_fast_t) DUK_DEC_C(ins);
  56931. if (extraop == DUK_EXTRAOP_INITSETI || extraop == DUK_EXTRAOP_INITGETI) {
  56932. duk_tval *tv_ind = DUK__REGP(idx);
  56933. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind));
  56934. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind);
  56935. }
  56936. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  56937. if (idx + 2 > (duk_uint_fast_t) duk_get_top(ctx)) {
  56938. /* XXX: use duk_is_valid_index() instead? */
  56939. /* XXX: improve check; check against nregs, not against top */
  56940. DUK__INTERNAL_ERROR("INITSET/INITGET out of bounds");
  56941. }
  56942. #endif
  56943. /* XXX: this is now a very unoptimal implementation -- this can be
  56944. * made very simple by direct manipulation of the object internals,
  56945. * given the guarantees above.
  56946. */
  56947. duk_push_hobject_bidx(ctx, DUK_BIDX_OBJECT_CONSTRUCTOR);
  56948. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_DEFINE_PROPERTY);
  56949. duk_push_undefined(ctx);
  56950. duk_dup(ctx, (duk_idx_t) b);
  56951. duk_dup(ctx, (duk_idx_t) (idx + 0));
  56952. duk_push_object(ctx); /* -> [ Object defineProperty undefined obj key desc ] */
  56953. duk_push_true(ctx);
  56954. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_ENUMERABLE);
  56955. duk_push_true(ctx);
  56956. duk_put_prop_stridx(ctx, -2, DUK_STRIDX_CONFIGURABLE);
  56957. duk_dup(ctx, (duk_idx_t) (idx + 1));
  56958. duk_put_prop_stridx(ctx, -2, (is_set ? DUK_STRIDX_SET : DUK_STRIDX_GET));
  56959. DUK_DDD(DUK_DDDPRINT("INITGET/INITSET: obj=%!T, key=%!T, desc=%!T",
  56960. (duk_tval *) duk_get_tval(ctx, -3),
  56961. (duk_tval *) duk_get_tval(ctx, -2),
  56962. (duk_tval *) duk_get_tval(ctx, -1)));
  56963. duk_call_method(ctx, 3); /* -> [ Object res ] */
  56964. duk_pop_2(ctx);
  56965. DUK_DDD(DUK_DDDPRINT("INITGET/INITSET AFTER: obj=%!T",
  56966. (duk_tval *) duk_get_tval(ctx, (duk_idx_t) b)));
  56967. break;
  56968. }
  56969. case DUK_EXTRAOP_ENDTRY: {
  56970. duk_catcher *cat;
  56971. duk_tval tv_tmp;
  56972. duk_tval *tv1;
  56973. DUK_ASSERT(thr->catchstack_top >= 1);
  56974. DUK_ASSERT(thr->callstack_top >= 1);
  56975. DUK_ASSERT(thr->catchstack[thr->catchstack_top - 1].callstack_index == thr->callstack_top - 1);
  56976. cat = thr->catchstack + thr->catchstack_top - 1;
  56977. DUK_DDD(DUK_DDDPRINT("ENDTRY: clearing catch active flag (regardless of whether it was set or not)"));
  56978. DUK_CAT_CLEAR_CATCH_ENABLED(cat);
  56979. if (DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
  56980. DUK_DDD(DUK_DDDPRINT("ENDTRY: finally part is active, jump through 2nd jump slot with 'normal continuation'"));
  56981. tv1 = thr->valstack + cat->idx_base;
  56982. DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top);
  56983. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  56984. DUK_TVAL_SET_UNDEFINED_ACTUAL(tv1);
  56985. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  56986. tv1 = NULL;
  56987. tv1 = thr->valstack + cat->idx_base + 1;
  56988. DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top);
  56989. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  56990. DUK_TVAL_SET_NUMBER(tv1, (duk_double_t) DUK_LJ_TYPE_NORMAL); /* XXX: set int */
  56991. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  56992. tv1 = NULL;
  56993. DUK_CAT_CLEAR_FINALLY_ENABLED(cat);
  56994. } else {
  56995. DUK_DDD(DUK_DDDPRINT("ENDTRY: no finally part, dismantle catcher, jump through 2nd jump slot (to end of statement)"));
  56996. duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1);
  56997. /* no need to unwind callstack */
  56998. }
  56999. act->pc = cat->pc_base + 1;
  57000. break;
  57001. }
  57002. case DUK_EXTRAOP_ENDCATCH: {
  57003. duk_catcher *cat;
  57004. duk_tval tv_tmp;
  57005. duk_tval *tv1;
  57006. DUK_ASSERT(thr->catchstack_top >= 1);
  57007. DUK_ASSERT(thr->callstack_top >= 1);
  57008. DUK_ASSERT(thr->catchstack[thr->catchstack_top - 1].callstack_index == thr->callstack_top - 1);
  57009. cat = thr->catchstack + thr->catchstack_top - 1;
  57010. DUK_ASSERT(!DUK_CAT_HAS_CATCH_ENABLED(cat)); /* cleared before entering catch part */
  57011. if (DUK_CAT_HAS_LEXENV_ACTIVE(cat)) {
  57012. duk_hobject *prev_env;
  57013. /* 'with' binding has no catch clause, so can't be here unless a normal try-catch */
  57014. DUK_ASSERT(DUK_CAT_HAS_CATCH_BINDING_ENABLED(cat));
  57015. DUK_ASSERT(act->lex_env != NULL);
  57016. DUK_DDD(DUK_DDDPRINT("ENDCATCH: popping catcher part lexical environment"));
  57017. prev_env = act->lex_env;
  57018. DUK_ASSERT(prev_env != NULL);
  57019. act->lex_env = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, prev_env);
  57020. DUK_CAT_CLEAR_LEXENV_ACTIVE(cat);
  57021. DUK_HOBJECT_DECREF(thr, prev_env); /* side effects */
  57022. }
  57023. if (DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
  57024. DUK_DDD(DUK_DDDPRINT("ENDCATCH: finally part is active, jump through 2nd jump slot with 'normal continuation'"));
  57025. tv1 = thr->valstack + cat->idx_base;
  57026. DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top);
  57027. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  57028. DUK_TVAL_SET_UNDEFINED_ACTUAL(tv1);
  57029. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  57030. tv1 = NULL;
  57031. tv1 = thr->valstack + cat->idx_base + 1;
  57032. DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top);
  57033. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  57034. DUK_TVAL_SET_NUMBER(tv1, (duk_double_t) DUK_LJ_TYPE_NORMAL); /* XXX: set int */
  57035. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  57036. tv1 = NULL;
  57037. DUK_CAT_CLEAR_FINALLY_ENABLED(cat);
  57038. } else {
  57039. DUK_DDD(DUK_DDDPRINT("ENDCATCH: no finally part, dismantle catcher, jump through 2nd jump slot (to end of statement)"));
  57040. duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1);
  57041. /* no need to unwind callstack */
  57042. }
  57043. act->pc = cat->pc_base + 1;
  57044. break;
  57045. }
  57046. case DUK_EXTRAOP_ENDFIN: {
  57047. duk_context *ctx = (duk_context *) thr;
  57048. duk_catcher *cat;
  57049. duk_tval *tv1;
  57050. duk_small_uint_fast_t cont_type;
  57051. DUK_ASSERT(thr->catchstack_top >= 1);
  57052. DUK_ASSERT(thr->callstack_top >= 1);
  57053. DUK_ASSERT(thr->catchstack[thr->catchstack_top - 1].callstack_index == thr->callstack_top - 1);
  57054. cat = thr->catchstack + thr->catchstack_top - 1;
  57055. /* CATCH flag may be enabled or disabled here; it may be enabled if
  57056. * the statement has a catch block but the try block does not throw
  57057. * an error.
  57058. */
  57059. DUK_ASSERT(!DUK_CAT_HAS_FINALLY_ENABLED(cat)); /* cleared before entering finally */
  57060. /* XXX: assert idx_base */
  57061. DUK_DDD(DUK_DDDPRINT("ENDFIN: completion value=%!T, type=%!T",
  57062. (duk_tval *) (thr->valstack + cat->idx_base + 0),
  57063. (duk_tval *) (thr->valstack + cat->idx_base + 1)));
  57064. tv1 = thr->valstack + cat->idx_base + 1; /* type */
  57065. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1));
  57066. cont_type = (duk_small_uint_fast_t) DUK_TVAL_GET_NUMBER(tv1);
  57067. if (cont_type == DUK_LJ_TYPE_NORMAL) {
  57068. DUK_DDD(DUK_DDDPRINT("ENDFIN: finally part finishing with 'normal' (non-abrupt) completion -> "
  57069. "dismantle catcher, resume execution after ENDFIN"));
  57070. duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1);
  57071. /* no need to unwind callstack */
  57072. } else {
  57073. DUK_DDD(DUK_DDDPRINT("ENDFIN: finally part finishing with abrupt completion, lj_type=%ld -> "
  57074. "dismantle catcher, re-throw error",
  57075. (long) cont_type));
  57076. duk_push_tval(ctx, thr->valstack + cat->idx_base);
  57077. /* XXX: assert lj type valid */
  57078. duk_err_setup_heap_ljstate(thr, (duk_small_int_t) cont_type);
  57079. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* always in executor */
  57080. duk_err_longjmp(thr);
  57081. DUK_UNREACHABLE();
  57082. }
  57083. /* continue execution after ENDFIN */
  57084. break;
  57085. }
  57086. case DUK_EXTRAOP_THROW: {
  57087. duk_context *ctx = (duk_context *) thr;
  57088. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  57089. /* Note: errors are augmented when they are created, not
  57090. * when they are thrown. So, don't augment here, it would
  57091. * break re-throwing for instance.
  57092. */
  57093. duk_dup(ctx, (duk_idx_t) bc);
  57094. DUK_DDD(DUK_DDDPRINT("THROW ERROR (BYTECODE): %!dT (before throw augment)",
  57095. (duk_tval *) duk_get_tval(ctx, -1)));
  57096. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  57097. duk_err_augment_error_throw(thr);
  57098. DUK_DDD(DUK_DDDPRINT("THROW ERROR (BYTECODE): %!dT (after throw augment)",
  57099. (duk_tval *) duk_get_tval(ctx, -1)));
  57100. #endif
  57101. duk_err_setup_heap_ljstate(thr, DUK_LJ_TYPE_THROW);
  57102. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* always in executor */
  57103. duk_err_longjmp(thr);
  57104. DUK_UNREACHABLE();
  57105. break;
  57106. }
  57107. case DUK_EXTRAOP_INVLHS: {
  57108. DUK_ERROR(thr, DUK_ERR_REFERENCE_ERROR, "invalid lvalue");
  57109. DUK_UNREACHABLE();
  57110. break;
  57111. }
  57112. case DUK_EXTRAOP_UNM:
  57113. case DUK_EXTRAOP_UNP: {
  57114. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  57115. duk__vm_arith_unary_op(thr, DUK__REGP(bc), bc, extraop);
  57116. break;
  57117. }
  57118. case DUK_EXTRAOP_DEBUGGER: {
  57119. /* Opcode only emitted by compiler when debugger
  57120. * support is enabled. Ignore it silently without
  57121. * debugger support, in case it has been loaded
  57122. * from precompiled bytecode.
  57123. */
  57124. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  57125. DUK_D(DUK_DPRINT("DEBUGGER statement encountered, halt execution"));
  57126. if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) {
  57127. DUK_HEAP_SET_PAUSED(thr->heap);
  57128. goto restart_execution;
  57129. }
  57130. #else
  57131. DUK_D(DUK_DPRINT("DEBUGGER statement ignored, no debugger support"));
  57132. #endif
  57133. break;
  57134. }
  57135. case DUK_EXTRAOP_BREAK: {
  57136. duk_context *ctx = (duk_context *) thr;
  57137. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  57138. /* always the "slow break" variant (longjmp'ing); a "fast break" is
  57139. * simply an DUK_OP_JUMP.
  57140. */
  57141. DUK_DDD(DUK_DDDPRINT("BREAK: %ld", (long) bc));
  57142. duk_push_uint(ctx, (duk_uint_t) bc);
  57143. duk_err_setup_heap_ljstate(thr, DUK_LJ_TYPE_BREAK);
  57144. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* always in executor */
  57145. duk_err_longjmp(thr);
  57146. DUK_UNREACHABLE();
  57147. break;
  57148. }
  57149. case DUK_EXTRAOP_CONTINUE: {
  57150. duk_context *ctx = (duk_context *) thr;
  57151. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  57152. /* always the "slow continue" variant (longjmp'ing); a "fast continue" is
  57153. * simply an DUK_OP_JUMP.
  57154. */
  57155. DUK_DDD(DUK_DDDPRINT("CONTINUE: %ld", (long) bc));
  57156. duk_push_uint(ctx, (duk_uint_t) bc);
  57157. duk_err_setup_heap_ljstate(thr, DUK_LJ_TYPE_CONTINUE);
  57158. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* always in executor */
  57159. duk_err_longjmp(thr);
  57160. DUK_UNREACHABLE();
  57161. break;
  57162. }
  57163. case DUK_EXTRAOP_BNOT: {
  57164. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  57165. duk__vm_bitwise_not(thr, DUK__REGP(bc), bc);
  57166. break;
  57167. }
  57168. case DUK_EXTRAOP_LNOT: {
  57169. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  57170. duk_tval *tv1;
  57171. tv1 = DUK__REGP(bc);
  57172. duk__vm_logical_not(thr, tv1, tv1);
  57173. break;
  57174. }
  57175. case DUK_EXTRAOP_INSTOF: {
  57176. duk_context *ctx = (duk_context *) thr;
  57177. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  57178. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  57179. duk_bool_t tmp;
  57180. tmp = duk_js_instanceof(thr, DUK__REGP(b), DUK__REGCONSTP(c));
  57181. duk_push_boolean(ctx, tmp);
  57182. duk_replace(ctx, (duk_idx_t) b);
  57183. break;
  57184. }
  57185. case DUK_EXTRAOP_IN: {
  57186. duk_context *ctx = (duk_context *) thr;
  57187. duk_small_uint_fast_t b = DUK_DEC_B(ins);
  57188. duk_small_uint_fast_t c = DUK_DEC_C(ins);
  57189. duk_bool_t tmp;
  57190. tmp = duk_js_in(thr, DUK__REGP(b), DUK__REGCONSTP(c));
  57191. duk_push_boolean(ctx, tmp);
  57192. duk_replace(ctx, (duk_idx_t) b);
  57193. break;
  57194. }
  57195. case DUK_EXTRAOP_LABEL: {
  57196. duk_catcher *cat;
  57197. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  57198. /* allocate catcher and populate it (should be atomic) */
  57199. duk_hthread_catchstack_grow(thr);
  57200. cat = thr->catchstack + thr->catchstack_top;
  57201. thr->catchstack_top++;
  57202. cat->flags = DUK_CAT_TYPE_LABEL | (bc << DUK_CAT_LABEL_SHIFT);
  57203. cat->callstack_index = thr->callstack_top - 1;
  57204. cat->pc_base = act->pc; /* pre-incremented, points to first jump slot */
  57205. cat->idx_base = 0; /* unused for label */
  57206. cat->h_varname = NULL;
  57207. DUK_DDD(DUK_DDDPRINT("LABEL catcher: flags=0x%08lx, callstack_index=%ld, pc_base=%ld, "
  57208. "idx_base=%ld, h_varname=%!O, label_id=%ld",
  57209. (long) cat->flags, (long) cat->callstack_index, (long) cat->pc_base,
  57210. (long) cat->idx_base, (duk_heaphdr *) cat->h_varname, (long) DUK_CAT_GET_LABEL(cat)));
  57211. act->pc += 2; /* skip jump slots */
  57212. break;
  57213. }
  57214. case DUK_EXTRAOP_ENDLABEL: {
  57215. duk_catcher *cat;
  57216. #if defined(DUK_USE_DDDPRINT) || defined(DUK_USE_ASSERTIONS)
  57217. duk_uint_fast_t bc = DUK_DEC_BC(ins);
  57218. #endif
  57219. #if defined(DUK_USE_DDDPRINT)
  57220. DUK_DDD(DUK_DDDPRINT("ENDLABEL %ld", (long) bc));
  57221. #endif
  57222. DUK_ASSERT(thr->catchstack_top >= 1);
  57223. cat = thr->catchstack + thr->catchstack_top - 1;
  57224. DUK_UNREF(cat);
  57225. DUK_ASSERT(DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_LABEL);
  57226. DUK_ASSERT((duk_uint_fast_t) DUK_CAT_GET_LABEL(cat) == bc);
  57227. duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1);
  57228. /* no need to unwind callstack */
  57229. break;
  57230. }
  57231. #ifdef DUK_USE_DEBUG
  57232. case DUK_EXTRAOP_DUMPREG: {
  57233. DUK_D(DUK_DPRINT("DUMPREG: %ld -> %!T",
  57234. (long) DUK_DEC_BC(ins),
  57235. (duk_tval *) duk_get_tval((duk_context *) thr, (duk_idx_t) DUK_DEC_BC(ins))));
  57236. break;
  57237. }
  57238. case DUK_EXTRAOP_DUMPREGS: {
  57239. duk_idx_t i, i_top;
  57240. i_top = duk_get_top((duk_context *) thr);
  57241. DUK_D(DUK_DPRINT("DUMPREGS: %ld regs", (long) i_top));
  57242. for (i = 0; i < i_top; i++) {
  57243. DUK_D(DUK_DPRINT(" r%ld -> %!dT",
  57244. (long) i,
  57245. (duk_tval *) duk_get_tval((duk_context *) thr, i)));
  57246. }
  57247. break;
  57248. }
  57249. case DUK_EXTRAOP_LOGMARK: {
  57250. DUK_D(DUK_DPRINT("LOGMARK: mark %ld at pc %ld", (long) DUK_DEC_BC(ins), (long) (act->pc - 1))); /* -1, autoinc */
  57251. break;
  57252. }
  57253. #endif /* DUK_USE_DEBUG */
  57254. default: {
  57255. DUK__INTERNAL_ERROR("invalid extra opcode");
  57256. }
  57257. } /* end switch */
  57258. break;
  57259. }
  57260. default: {
  57261. /* this should never be possible, because the switch-case is
  57262. * comprehensive
  57263. */
  57264. DUK__INTERNAL_ERROR("invalid opcode");
  57265. break;
  57266. }
  57267. } /* end switch */
  57268. }
  57269. DUK_UNREACHABLE();
  57270. #ifndef DUK_USE_VERBOSE_EXECUTOR_ERRORS
  57271. internal_error:
  57272. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, "internal error in bytecode executor");
  57273. #endif
  57274. }
  57275. #undef DUK__INTERNAL_ERROR
  57276. #line 1 "duk_js_ops.c"
  57277. /*
  57278. * Ecmascript specification algorithm and conversion helpers.
  57279. *
  57280. * These helpers encapsulate the primitive Ecmascript operation
  57281. * semantics, and are used by the bytecode executor and the API
  57282. * (among other places). Note that some primitives are only
  57283. * implemented as part of the API and have no "internal" helper.
  57284. * (This is the case when an internal helper would not really be
  57285. * useful; e.g. the operation is rare, uses value stack heavily,
  57286. * etc.)
  57287. *
  57288. * The operation arguments depend on what is required to implement
  57289. * the operation:
  57290. *
  57291. * - If an operation is simple and stateless, and has no side
  57292. * effects, it won't take an duk_hthread argument and its
  57293. * arguments may be duk_tval pointers (which are safe as long
  57294. * as no side effects take place).
  57295. *
  57296. * - If complex coercions are required (e.g. a "ToNumber" coercion)
  57297. * or errors may be thrown, the operation takes an duk_hthread
  57298. * argument. This also implies that the operation may have
  57299. * arbitrary side effects, invalidating any duk_tval pointers.
  57300. *
  57301. * - For operations with potential side effects, arguments can be
  57302. * taken in several ways:
  57303. *
  57304. * a) as duk_tval pointers, which makes sense if the "common case"
  57305. * can be resolved without side effects (e.g. coercion); the
  57306. * arguments are pushed to the valstack for coercion if
  57307. * necessary
  57308. *
  57309. * b) as duk_tval values
  57310. *
  57311. * c) implicitly on value stack top
  57312. *
  57313. * d) as indices to the value stack
  57314. *
  57315. * Future work:
  57316. *
  57317. * - Argument styles may not be the most sensible in every case now.
  57318. *
  57319. * - In-place coercions might be useful for several operations, if
  57320. * in-place coercion is OK for the bytecode executor and the API.
  57321. */
  57322. /* include removed: duk_internal.h */
  57323. /*
  57324. * [[DefaultValue]] (E5 Section 8.12.8)
  57325. *
  57326. * ==> implemented in the API.
  57327. */
  57328. /*
  57329. * ToPrimitive() (E5 Section 9.1)
  57330. *
  57331. * ==> implemented in the API.
  57332. */
  57333. /*
  57334. * ToBoolean() (E5 Section 9.2)
  57335. */
  57336. DUK_INTERNAL duk_bool_t duk_js_toboolean(duk_tval *tv) {
  57337. switch (DUK_TVAL_GET_TAG(tv)) {
  57338. case DUK_TAG_UNDEFINED:
  57339. case DUK_TAG_NULL:
  57340. return 0;
  57341. case DUK_TAG_BOOLEAN:
  57342. return DUK_TVAL_GET_BOOLEAN(tv);
  57343. case DUK_TAG_STRING: {
  57344. duk_hstring *h = DUK_TVAL_GET_STRING(tv);
  57345. DUK_ASSERT(h != NULL);
  57346. return (DUK_HSTRING_GET_BYTELEN(h) > 0 ? 1 : 0);
  57347. }
  57348. case DUK_TAG_OBJECT: {
  57349. return 1;
  57350. }
  57351. case DUK_TAG_BUFFER: {
  57352. /* mimic semantics for strings */
  57353. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  57354. DUK_ASSERT(h != NULL);
  57355. return (DUK_HBUFFER_GET_SIZE(h) > 0 ? 1 : 0);
  57356. }
  57357. case DUK_TAG_POINTER: {
  57358. void *p = DUK_TVAL_GET_POINTER(tv);
  57359. return (p != NULL ? 1 : 0);
  57360. }
  57361. case DUK_TAG_LIGHTFUNC: {
  57362. return 1;
  57363. }
  57364. #if defined(DUK_USE_FASTINT)
  57365. case DUK_TAG_FASTINT:
  57366. if (DUK_TVAL_GET_FASTINT(tv) != 0) {
  57367. return 1;
  57368. } else {
  57369. return 0;
  57370. }
  57371. #endif
  57372. default: {
  57373. /* number */
  57374. duk_double_t d;
  57375. int c;
  57376. DUK_ASSERT(DUK_TVAL_IS_DOUBLE(tv));
  57377. d = DUK_TVAL_GET_DOUBLE(tv);
  57378. c = DUK_FPCLASSIFY((double) d);
  57379. if (c == DUK_FP_ZERO || c == DUK_FP_NAN) {
  57380. return 0;
  57381. } else {
  57382. return 1;
  57383. }
  57384. }
  57385. }
  57386. DUK_UNREACHABLE();
  57387. }
  57388. /*
  57389. * ToNumber() (E5 Section 9.3)
  57390. *
  57391. * Value to convert must be on stack top, and is popped before exit.
  57392. *
  57393. * See: http://www.cs.indiana.edu/~burger/FP-Printing-PLDI96.pdf
  57394. * http://www.cs.indiana.edu/~burger/fp/index.html
  57395. *
  57396. * Notes on the conversion:
  57397. *
  57398. * - There are specific requirements on the accuracy of the conversion
  57399. * through a "Mathematical Value" (MV), so this conversion is not
  57400. * trivial.
  57401. *
  57402. * - Quick rejects (e.g. based on first char) are difficult because
  57403. * the grammar allows leading and trailing white space.
  57404. *
  57405. * - Quick reject based on string length is difficult even after
  57406. * accounting for white space; there may be arbitrarily many
  57407. * decimal digits.
  57408. *
  57409. * - Standard grammar allows decimal values ("123"), hex values
  57410. * ("0x123") and infinities
  57411. *
  57412. * - Unlike source code literals, ToNumber() coerces empty strings
  57413. * and strings with only whitespace to zero (not NaN).
  57414. */
  57415. /* E5 Section 9.3.1 */
  57416. DUK_LOCAL duk_double_t duk__tonumber_string_raw(duk_hthread *thr) {
  57417. duk_context *ctx = (duk_context *) thr;
  57418. duk_small_uint_t s2n_flags;
  57419. duk_double_t d;
  57420. /* Quite lenient, e.g. allow empty as zero, but don't allow trailing
  57421. * garbage.
  57422. */
  57423. s2n_flags = DUK_S2N_FLAG_TRIM_WHITE |
  57424. DUK_S2N_FLAG_ALLOW_EXP |
  57425. DUK_S2N_FLAG_ALLOW_PLUS |
  57426. DUK_S2N_FLAG_ALLOW_MINUS |
  57427. DUK_S2N_FLAG_ALLOW_INF |
  57428. DUK_S2N_FLAG_ALLOW_FRAC |
  57429. DUK_S2N_FLAG_ALLOW_NAKED_FRAC |
  57430. DUK_S2N_FLAG_ALLOW_EMPTY_FRAC |
  57431. DUK_S2N_FLAG_ALLOW_EMPTY_AS_ZERO |
  57432. DUK_S2N_FLAG_ALLOW_LEADING_ZERO |
  57433. DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT;
  57434. duk_numconv_parse(ctx, 10 /*radix*/, s2n_flags);
  57435. d = duk_get_number(ctx, -1);
  57436. duk_pop(ctx);
  57437. return d;
  57438. }
  57439. DUK_INTERNAL duk_double_t duk_js_tonumber(duk_hthread *thr, duk_tval *tv) {
  57440. duk_context *ctx = (duk_hthread *) thr;
  57441. DUK_ASSERT(thr != NULL);
  57442. DUK_ASSERT(tv != NULL);
  57443. switch (DUK_TVAL_GET_TAG(tv)) {
  57444. case DUK_TAG_UNDEFINED: {
  57445. /* return a specific NaN (although not strictly necessary) */
  57446. duk_double_union du;
  57447. DUK_DBLUNION_SET_NAN(&du);
  57448. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  57449. return du.d;
  57450. }
  57451. case DUK_TAG_NULL: {
  57452. /* +0.0 */
  57453. return 0.0;
  57454. }
  57455. case DUK_TAG_BOOLEAN: {
  57456. if (DUK_TVAL_IS_BOOLEAN_TRUE(tv)) {
  57457. return 1.0;
  57458. }
  57459. return 0.0;
  57460. }
  57461. case DUK_TAG_STRING: {
  57462. duk_hstring *h = DUK_TVAL_GET_STRING(tv);
  57463. duk_push_hstring(ctx, h);
  57464. return duk__tonumber_string_raw(thr);
  57465. }
  57466. case DUK_TAG_OBJECT: {
  57467. /* Note: ToPrimitive(object,hint) == [[DefaultValue]](object,hint),
  57468. * so use [[DefaultValue]] directly.
  57469. */
  57470. duk_double_t d;
  57471. duk_push_tval(ctx, tv);
  57472. duk_to_defaultvalue(ctx, -1, DUK_HINT_NUMBER); /* 'tv' becomes invalid */
  57473. /* recursive call for a primitive value (guaranteed not to cause second
  57474. * recursion).
  57475. */
  57476. d = duk_js_tonumber(thr, duk_require_tval(ctx, -1));
  57477. duk_pop(ctx);
  57478. return d;
  57479. }
  57480. case DUK_TAG_BUFFER: {
  57481. /* Coerce like a string. This makes sense because addition also treats
  57482. * buffers like strings.
  57483. */
  57484. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  57485. duk_push_hbuffer(ctx, h);
  57486. duk_to_string(ctx, -1); /* XXX: expensive, but numconv now expects to see a string */
  57487. return duk__tonumber_string_raw(thr);
  57488. }
  57489. case DUK_TAG_POINTER: {
  57490. /* Coerce like boolean */
  57491. void *p = DUK_TVAL_GET_POINTER(tv);
  57492. return (p != NULL ? 1.0 : 0.0);
  57493. }
  57494. case DUK_TAG_LIGHTFUNC: {
  57495. /* +(function(){}) -> NaN */
  57496. return DUK_DOUBLE_NAN;
  57497. }
  57498. #if defined(DUK_USE_FASTINT)
  57499. case DUK_TAG_FASTINT:
  57500. return (duk_double_t) DUK_TVAL_GET_FASTINT(tv);
  57501. #endif
  57502. default: {
  57503. /* number */
  57504. DUK_ASSERT(DUK_TVAL_IS_DOUBLE(tv));
  57505. return DUK_TVAL_GET_DOUBLE(tv);
  57506. }
  57507. }
  57508. DUK_UNREACHABLE();
  57509. }
  57510. /*
  57511. * ToInteger() (E5 Section 9.4)
  57512. */
  57513. /* exposed, used by e.g. duk_bi_date.c */
  57514. DUK_INTERNAL duk_double_t duk_js_tointeger_number(duk_double_t x) {
  57515. duk_small_int_t c = (duk_small_int_t) DUK_FPCLASSIFY(x);
  57516. if (c == DUK_FP_NAN) {
  57517. return 0.0;
  57518. } else if (c == DUK_FP_ZERO || c == DUK_FP_INFINITE) {
  57519. /* XXX: FP_ZERO check can be removed, the else clause handles it
  57520. * correctly (preserving sign).
  57521. */
  57522. return x;
  57523. } else {
  57524. duk_small_int_t s = (duk_small_int_t) DUK_SIGNBIT(x);
  57525. x = DUK_FLOOR(DUK_FABS(x)); /* truncate towards zero */
  57526. if (s) {
  57527. x = -x;
  57528. }
  57529. return x;
  57530. }
  57531. }
  57532. DUK_INTERNAL duk_double_t duk_js_tointeger(duk_hthread *thr, duk_tval *tv) {
  57533. /* XXX: fastint */
  57534. duk_double_t d = duk_js_tonumber(thr, tv); /* invalidates tv */
  57535. return duk_js_tointeger_number(d);
  57536. }
  57537. /*
  57538. * ToInt32(), ToUint32(), ToUint16() (E5 Sections 9.5, 9.6, 9.7)
  57539. */
  57540. /* combined algorithm matching E5 Sections 9.5 and 9.6 */
  57541. DUK_LOCAL duk_double_t duk__toint32_touint32_helper(duk_double_t x, duk_bool_t is_toint32) {
  57542. duk_small_int_t c = (duk_small_int_t) DUK_FPCLASSIFY(x);
  57543. duk_small_int_t s;
  57544. if (c == DUK_FP_NAN || c == DUK_FP_ZERO || c == DUK_FP_INFINITE) {
  57545. return 0.0;
  57546. }
  57547. /* x = sign(x) * floor(abs(x)), i.e. truncate towards zero, keep sign */
  57548. s = (duk_small_int_t) DUK_SIGNBIT(x);
  57549. x = DUK_FLOOR(DUK_FABS(x));
  57550. if (s) {
  57551. x = -x;
  57552. }
  57553. /* NOTE: fmod(x) result sign is same as sign of x, which
  57554. * differs from what Javascript wants (see Section 9.6).
  57555. */
  57556. x = DUK_FMOD(x, DUK_DOUBLE_2TO32); /* -> x in ]-2**32, 2**32[ */
  57557. if (x < 0.0) {
  57558. x += DUK_DOUBLE_2TO32;
  57559. }
  57560. /* -> x in [0, 2**32[ */
  57561. if (is_toint32) {
  57562. if (x >= DUK_DOUBLE_2TO31) {
  57563. /* x in [2**31, 2**32[ */
  57564. x -= DUK_DOUBLE_2TO32; /* -> x in [-2**31,2**31[ */
  57565. }
  57566. }
  57567. return x;
  57568. }
  57569. DUK_INTERNAL duk_int32_t duk_js_toint32(duk_hthread *thr, duk_tval *tv) {
  57570. duk_double_t d;
  57571. #if defined(DUK_USE_FASTINT)
  57572. if (DUK_TVAL_IS_FASTINT(tv)) {
  57573. return DUK_TVAL_GET_FASTINT_I32(tv);
  57574. }
  57575. #endif
  57576. d = duk_js_tonumber(thr, tv); /* invalidates tv */
  57577. d = duk__toint32_touint32_helper(d, 1);
  57578. DUK_ASSERT(DUK_FPCLASSIFY(d) == DUK_FP_ZERO || DUK_FPCLASSIFY(d) == DUK_FP_NORMAL);
  57579. DUK_ASSERT(d >= -2147483648.0 && d <= 2147483647.0); /* [-0x80000000,0x7fffffff] */
  57580. DUK_ASSERT(d == ((duk_double_t) ((duk_int32_t) d))); /* whole, won't clip */
  57581. return (duk_int32_t) d;
  57582. }
  57583. DUK_INTERNAL duk_uint32_t duk_js_touint32(duk_hthread *thr, duk_tval *tv) {
  57584. duk_double_t d;
  57585. #if defined(DUK_USE_FASTINT)
  57586. if (DUK_TVAL_IS_FASTINT(tv)) {
  57587. return DUK_TVAL_GET_FASTINT_U32(tv);
  57588. }
  57589. #endif
  57590. d = duk_js_tonumber(thr, tv); /* invalidates tv */
  57591. d = duk__toint32_touint32_helper(d, 0);
  57592. DUK_ASSERT(DUK_FPCLASSIFY(d) == DUK_FP_ZERO || DUK_FPCLASSIFY(d) == DUK_FP_NORMAL);
  57593. DUK_ASSERT(d >= 0.0 && d <= 4294967295.0); /* [0x00000000, 0xffffffff] */
  57594. DUK_ASSERT(d == ((duk_double_t) ((duk_uint32_t) d))); /* whole, won't clip */
  57595. return (duk_uint32_t) d;
  57596. }
  57597. DUK_INTERNAL duk_uint16_t duk_js_touint16(duk_hthread *thr, duk_tval *tv) {
  57598. /* should be a safe way to compute this */
  57599. return (duk_uint16_t) (duk_js_touint32(thr, tv) & 0x0000ffffU);
  57600. }
  57601. /*
  57602. * ToString() (E5 Section 9.8)
  57603. *
  57604. * ==> implemented in the API.
  57605. */
  57606. /*
  57607. * ToObject() (E5 Section 9.9)
  57608. *
  57609. * ==> implemented in the API.
  57610. */
  57611. /*
  57612. * CheckObjectCoercible() (E5 Section 9.10)
  57613. *
  57614. * Note: no API equivalent now.
  57615. */
  57616. #if 0 /* unused */
  57617. DUK_INTERNAL void duk_js_checkobjectcoercible(duk_hthread *thr, duk_tval *tv_x) {
  57618. duk_small_uint_t tag = DUK_TVAL_GET_TAG(tv_x);
  57619. /* Note: this must match ToObject() behavior */
  57620. if (tag == DUK_TAG_UNDEFINED ||
  57621. tag == DUK_TAG_NULL ||
  57622. tag == DUK_TAG_POINTER ||
  57623. tag == DUK_TAG_BUFFER) {
  57624. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "not object coercible");
  57625. }
  57626. }
  57627. #endif
  57628. /*
  57629. * IsCallable() (E5 Section 9.11)
  57630. *
  57631. * XXX: API equivalent is a separate implementation now, and this has
  57632. * currently no callers.
  57633. */
  57634. #if 0 /* unused */
  57635. DUK_INTERNAL duk_bool_t duk_js_iscallable(duk_tval *tv_x) {
  57636. duk_hobject *obj;
  57637. if (!DUK_TVAL_IS_OBJECT(tv_x)) {
  57638. return 0;
  57639. }
  57640. obj = DUK_TVAL_GET_OBJECT(tv_x);
  57641. DUK_ASSERT(obj != NULL);
  57642. return DUK_HOBJECT_IS_CALLABLE(obj);
  57643. }
  57644. #endif
  57645. /*
  57646. * Loose equality, strict equality, and SameValue (E5 Sections 11.9.1, 11.9.4,
  57647. * 9.12). These have much in common so they can share some helpers.
  57648. *
  57649. * Future work notes:
  57650. *
  57651. * - Current implementation (and spec definition) has recursion; this should
  57652. * be fixed if possible.
  57653. *
  57654. * - String-to-number coercion should be possible without going through the
  57655. * value stack (and be more compact) if a shared helper is invoked.
  57656. */
  57657. /* Note that this is the same operation for strict and loose equality:
  57658. * - E5 Section 11.9.3, step 1.c (loose)
  57659. * - E5 Section 11.9.6, step 4 (strict)
  57660. */
  57661. DUK_LOCAL duk_bool_t duk__js_equals_number(duk_double_t x, duk_double_t y) {
  57662. #if defined(DUK_USE_PARANOID_MATH)
  57663. /* Straightforward algorithm, makes fewer compiler assumptions. */
  57664. duk_small_int_t cx = (duk_small_int_t) DUK_FPCLASSIFY(x);
  57665. duk_small_int_t cy = (duk_small_int_t) DUK_FPCLASSIFY(y);
  57666. if (cx == DUK_FP_NAN || cy == DUK_FP_NAN) {
  57667. return 0;
  57668. }
  57669. if (cx == DUK_FP_ZERO && cy == DUK_FP_ZERO) {
  57670. return 1;
  57671. }
  57672. if (x == y) {
  57673. return 1;
  57674. }
  57675. return 0;
  57676. #else /* DUK_USE_PARANOID_MATH */
  57677. /* Better equivalent algorithm. If the compiler is compliant, C and
  57678. * Ecmascript semantics are identical for this particular comparison.
  57679. * In particular, NaNs must never compare equal and zeroes must compare
  57680. * equal regardless of sign. Could also use a macro, but this inlines
  57681. * already nicely (no difference on gcc, for instance).
  57682. */
  57683. if (x == y) {
  57684. /* IEEE requires that NaNs compare false */
  57685. DUK_ASSERT(DUK_FPCLASSIFY(x) != DUK_FP_NAN);
  57686. DUK_ASSERT(DUK_FPCLASSIFY(y) != DUK_FP_NAN);
  57687. return 1;
  57688. } else {
  57689. /* IEEE requires that zeros compare the same regardless
  57690. * of their signed, so if both x and y are zeroes, they
  57691. * are caught above.
  57692. */
  57693. DUK_ASSERT(!(DUK_FPCLASSIFY(x) == DUK_FP_ZERO && DUK_FPCLASSIFY(y) == DUK_FP_ZERO));
  57694. return 0;
  57695. }
  57696. #endif /* DUK_USE_PARANOID_MATH */
  57697. }
  57698. DUK_LOCAL duk_bool_t duk__js_samevalue_number(duk_double_t x, duk_double_t y) {
  57699. #if defined(DUK_USE_PARANOID_MATH)
  57700. duk_small_int_t cx = (duk_small_int_t) DUK_FPCLASSIFY(x);
  57701. duk_small_int_t cy = (duk_small_int_t) DUK_FPCLASSIFY(y);
  57702. if (cx == DUK_FP_NAN && cy == DUK_FP_NAN) {
  57703. /* SameValue(NaN, NaN) = true, regardless of NaN sign or extra bits */
  57704. return 1;
  57705. }
  57706. if (cx == DUK_FP_ZERO && cy == DUK_FP_ZERO) {
  57707. /* Note: cannot assume that a non-zero return value of signbit() would
  57708. * always be the same -- hence cannot (portably) use something like:
  57709. *
  57710. * signbit(x) == signbit(y)
  57711. */
  57712. duk_small_int_t sx = (DUK_SIGNBIT(x) ? 1 : 0);
  57713. duk_small_int_t sy = (DUK_SIGNBIT(y) ? 1 : 0);
  57714. return (sx == sy);
  57715. }
  57716. /* normal comparison; known:
  57717. * - both x and y are not NaNs (but one of them can be)
  57718. * - both x and y are not zero (but one of them can be)
  57719. * - x and y may be denormal or infinite
  57720. */
  57721. return (x == y);
  57722. #else /* DUK_USE_PARANOID_MATH */
  57723. duk_small_int_t cx = (duk_small_int_t) DUK_FPCLASSIFY(x);
  57724. duk_small_int_t cy = (duk_small_int_t) DUK_FPCLASSIFY(y);
  57725. if (x == y) {
  57726. /* IEEE requires that NaNs compare false */
  57727. DUK_ASSERT(DUK_FPCLASSIFY(x) != DUK_FP_NAN);
  57728. DUK_ASSERT(DUK_FPCLASSIFY(y) != DUK_FP_NAN);
  57729. /* Using classification has smaller footprint than direct comparison. */
  57730. if (DUK_UNLIKELY(cx == DUK_FP_ZERO && cy == DUK_FP_ZERO)) {
  57731. /* Note: cannot assume that a non-zero return value of signbit() would
  57732. * always be the same -- hence cannot (portably) use something like:
  57733. *
  57734. * signbit(x) == signbit(y)
  57735. */
  57736. duk_small_int_t sx = (DUK_SIGNBIT(x) ? 1 : 0);
  57737. duk_small_int_t sy = (DUK_SIGNBIT(y) ? 1 : 0);
  57738. return (sx == sy);
  57739. }
  57740. return 1;
  57741. } else {
  57742. /* IEEE requires that zeros compare the same regardless
  57743. * of their signed, so if both x and y are zeroes, they
  57744. * are caught above.
  57745. */
  57746. DUK_ASSERT(!(DUK_FPCLASSIFY(x) == DUK_FP_ZERO && DUK_FPCLASSIFY(y) == DUK_FP_ZERO));
  57747. /* Difference to non-strict/strict comparison is that NaNs compare
  57748. * equal and signed zero signs matter.
  57749. */
  57750. if (DUK_UNLIKELY(cx == DUK_FP_NAN && cy == DUK_FP_NAN)) {
  57751. /* SameValue(NaN, NaN) = true, regardless of NaN sign or extra bits */
  57752. return 1;
  57753. }
  57754. return 0;
  57755. }
  57756. #endif /* DUK_USE_PARANOID_MATH */
  57757. }
  57758. 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) {
  57759. duk_context *ctx = (duk_context *) thr;
  57760. duk_tval *tv_tmp;
  57761. /* If flags != 0 (strict or SameValue), thr can be NULL. For loose
  57762. * equals comparison it must be != NULL.
  57763. */
  57764. DUK_ASSERT(flags != 0 || thr != NULL);
  57765. /*
  57766. * Same type?
  57767. *
  57768. * Note: since number values have no explicit tag in the 8-byte
  57769. * representation, need the awkward if + switch.
  57770. */
  57771. #if defined(DUK_USE_FASTINT)
  57772. if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) {
  57773. if (DUK_TVAL_GET_FASTINT(tv_x) == DUK_TVAL_GET_FASTINT(tv_y)) {
  57774. return 1;
  57775. } else {
  57776. return 0;
  57777. }
  57778. }
  57779. else
  57780. #endif
  57781. if (DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y)) {
  57782. /* Catches both doubles and cases where only one argument is a fastint */
  57783. if (DUK_UNLIKELY((flags & DUK_EQUALS_FLAG_SAMEVALUE) != 0)) {
  57784. /* SameValue */
  57785. return duk__js_samevalue_number(DUK_TVAL_GET_NUMBER(tv_x),
  57786. DUK_TVAL_GET_NUMBER(tv_y));
  57787. } else {
  57788. /* equals and strict equals */
  57789. return duk__js_equals_number(DUK_TVAL_GET_NUMBER(tv_x),
  57790. DUK_TVAL_GET_NUMBER(tv_y));
  57791. }
  57792. } else if (DUK_TVAL_GET_TAG(tv_x) == DUK_TVAL_GET_TAG(tv_y)) {
  57793. switch (DUK_TVAL_GET_TAG(tv_x)) {
  57794. case DUK_TAG_UNDEFINED:
  57795. case DUK_TAG_NULL: {
  57796. return 1;
  57797. }
  57798. case DUK_TAG_BOOLEAN: {
  57799. return DUK_TVAL_GET_BOOLEAN(tv_x) == DUK_TVAL_GET_BOOLEAN(tv_y);
  57800. }
  57801. case DUK_TAG_POINTER: {
  57802. return DUK_TVAL_GET_POINTER(tv_x) == DUK_TVAL_GET_POINTER(tv_y);
  57803. }
  57804. case DUK_TAG_STRING:
  57805. case DUK_TAG_OBJECT: {
  57806. /* heap pointer comparison suffices */
  57807. return DUK_TVAL_GET_HEAPHDR(tv_x) == DUK_TVAL_GET_HEAPHDR(tv_y);
  57808. }
  57809. case DUK_TAG_BUFFER: {
  57810. if ((flags & (DUK_EQUALS_FLAG_STRICT | DUK_EQUALS_FLAG_SAMEVALUE)) != 0) {
  57811. /* heap pointer comparison suffices */
  57812. return DUK_TVAL_GET_HEAPHDR(tv_x) == DUK_TVAL_GET_HEAPHDR(tv_y);
  57813. } else {
  57814. /* non-strict equality for buffers compares contents */
  57815. duk_hbuffer *h_x = DUK_TVAL_GET_BUFFER(tv_x);
  57816. duk_hbuffer *h_y = DUK_TVAL_GET_BUFFER(tv_y);
  57817. duk_size_t len_x = DUK_HBUFFER_GET_SIZE(h_x);
  57818. duk_size_t len_y = DUK_HBUFFER_GET_SIZE(h_y);
  57819. void *buf_x;
  57820. void *buf_y;
  57821. if (len_x != len_y) {
  57822. return 0;
  57823. }
  57824. buf_x = (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_x);
  57825. buf_y = (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_y);
  57826. /* if len_x == len_y == 0, buf_x and/or buf_y may
  57827. * be NULL, but that's OK.
  57828. */
  57829. DUK_ASSERT(len_x == len_y);
  57830. DUK_ASSERT(len_x == 0 || buf_x != NULL);
  57831. DUK_ASSERT(len_y == 0 || buf_y != NULL);
  57832. return (DUK_MEMCMP(buf_x, buf_y, len_x) == 0) ? 1 : 0;
  57833. }
  57834. }
  57835. case DUK_TAG_LIGHTFUNC: {
  57836. /* At least 'magic' has a significant impact on function
  57837. * identity.
  57838. */
  57839. duk_small_uint_t lf_flags_x;
  57840. duk_small_uint_t lf_flags_y;
  57841. duk_c_function func_x;
  57842. duk_c_function func_y;
  57843. DUK_TVAL_GET_LIGHTFUNC(tv_x, func_x, lf_flags_x);
  57844. DUK_TVAL_GET_LIGHTFUNC(tv_y, func_y, lf_flags_y);
  57845. return ((func_x == func_y) && (lf_flags_x == lf_flags_y)) ? 1 : 0;
  57846. }
  57847. #if defined(DUK_USE_FASTINT)
  57848. case DUK_TAG_FASTINT:
  57849. #endif
  57850. default: {
  57851. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_x));
  57852. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_y));
  57853. DUK_UNREACHABLE();
  57854. return 0;
  57855. }
  57856. }
  57857. }
  57858. if ((flags & (DUK_EQUALS_FLAG_STRICT | DUK_EQUALS_FLAG_SAMEVALUE)) != 0) {
  57859. return 0;
  57860. }
  57861. DUK_ASSERT(flags == 0); /* non-strict equality from here on */
  57862. /*
  57863. * Types are different; various cases for non-strict comparison
  57864. *
  57865. * Since comparison is symmetric, we use a "swap trick" to reduce
  57866. * code size.
  57867. */
  57868. /* Undefined/null are considered equal (e.g. "null == undefined" -> true). */
  57869. if ((DUK_TVAL_IS_UNDEFINED(tv_x) && DUK_TVAL_IS_NULL(tv_y)) ||
  57870. (DUK_TVAL_IS_NULL(tv_x) && DUK_TVAL_IS_UNDEFINED(tv_y))) {
  57871. return 1;
  57872. }
  57873. /* Number/string-or-buffer -> coerce string to number (e.g. "'1.5' == 1.5" -> true). */
  57874. if (DUK_TVAL_IS_NUMBER(tv_x) && (DUK_TVAL_IS_STRING(tv_y) || DUK_TVAL_IS_BUFFER(tv_y))) {
  57875. /* the next 'if' is guaranteed to match after swap */
  57876. tv_tmp = tv_x;
  57877. tv_x = tv_y;
  57878. tv_y = tv_tmp;
  57879. }
  57880. if ((DUK_TVAL_IS_STRING(tv_x) || DUK_TVAL_IS_BUFFER(tv_x)) && DUK_TVAL_IS_NUMBER(tv_y)) {
  57881. /* XXX: this is possible without resorting to the value stack */
  57882. duk_double_t d1, d2;
  57883. d2 = DUK_TVAL_GET_NUMBER(tv_y);
  57884. duk_push_tval(ctx, tv_x);
  57885. duk_to_string(ctx, -1); /* buffer values are coerced first to string here */
  57886. duk_to_number(ctx, -1);
  57887. d1 = duk_require_number(ctx, -1);
  57888. duk_pop(ctx);
  57889. return duk__js_equals_number(d1, d2);
  57890. }
  57891. /* Buffer/string -> compare contents. */
  57892. if (DUK_TVAL_IS_BUFFER(tv_x) && DUK_TVAL_IS_STRING(tv_y)) {
  57893. tv_tmp = tv_x;
  57894. tv_x = tv_y;
  57895. tv_y = tv_tmp;
  57896. }
  57897. if (DUK_TVAL_IS_STRING(tv_x) && DUK_TVAL_IS_BUFFER(tv_y)) {
  57898. duk_hstring *h_x = DUK_TVAL_GET_STRING(tv_x);
  57899. duk_hbuffer *h_y = DUK_TVAL_GET_BUFFER(tv_y);
  57900. duk_size_t len_x = DUK_HSTRING_GET_BYTELEN(h_x);
  57901. duk_size_t len_y = DUK_HBUFFER_GET_SIZE(h_y);
  57902. void *buf_x;
  57903. void *buf_y;
  57904. if (len_x != len_y) {
  57905. return 0;
  57906. }
  57907. buf_x = (void *) DUK_HSTRING_GET_DATA(h_x);
  57908. buf_y = (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_y);
  57909. /* if len_x == len_y == 0, buf_x and/or buf_y may
  57910. * be NULL, but that's OK.
  57911. */
  57912. DUK_ASSERT(len_x == len_y);
  57913. DUK_ASSERT(len_x == 0 || buf_x != NULL);
  57914. DUK_ASSERT(len_y == 0 || buf_y != NULL);
  57915. return (DUK_MEMCMP(buf_x, buf_y, len_x) == 0) ? 1 : 0;
  57916. }
  57917. /* Boolean/any -> coerce boolean to number and try again. If boolean is
  57918. * compared to a pointer, the final comparison after coercion now always
  57919. * yields false (as pointer vs. number compares to false), but this is
  57920. * not special cased.
  57921. */
  57922. if (DUK_TVAL_IS_BOOLEAN(tv_x)) {
  57923. tv_tmp = tv_x;
  57924. tv_x = tv_y;
  57925. tv_y = tv_tmp;
  57926. }
  57927. if (DUK_TVAL_IS_BOOLEAN(tv_y)) {
  57928. /* ToNumber(bool) is +1.0 or 0.0. Tagged boolean value is always 0 or 1. */
  57929. duk_bool_t rc;
  57930. DUK_ASSERT(DUK_TVAL_GET_BOOLEAN(tv_y) == 0 || DUK_TVAL_GET_BOOLEAN(tv_y) == 1);
  57931. duk_push_tval(ctx, tv_x);
  57932. duk_push_int(ctx, DUK_TVAL_GET_BOOLEAN(tv_y));
  57933. rc = duk_js_equals_helper(thr, duk_get_tval(ctx, -2), duk_get_tval(ctx, -1), 0 /*flags:nonstrict*/);
  57934. duk_pop_2(ctx);
  57935. return rc;
  57936. }
  57937. /* String-number-buffer/object -> coerce object to primitive (apparently without hint), then try again. */
  57938. if ((DUK_TVAL_IS_STRING(tv_x) || DUK_TVAL_IS_NUMBER(tv_x) || DUK_TVAL_IS_BUFFER(tv_x)) &&
  57939. DUK_TVAL_IS_OBJECT(tv_y)) {
  57940. tv_tmp = tv_x;
  57941. tv_x = tv_y;
  57942. tv_y = tv_tmp;
  57943. }
  57944. if (DUK_TVAL_IS_OBJECT(tv_x) &&
  57945. (DUK_TVAL_IS_STRING(tv_y) || DUK_TVAL_IS_NUMBER(tv_y) || DUK_TVAL_IS_BUFFER(tv_y))) {
  57946. duk_bool_t rc;
  57947. duk_push_tval(ctx, tv_x);
  57948. duk_push_tval(ctx, tv_y);
  57949. duk_to_primitive(ctx, -2, DUK_HINT_NONE); /* apparently no hint? */
  57950. rc = duk_js_equals_helper(thr, duk_get_tval(ctx, -2), duk_get_tval(ctx, -1), 0 /*flags:nonstrict*/);
  57951. duk_pop_2(ctx);
  57952. return rc;
  57953. }
  57954. /* Nothing worked -> not equal. */
  57955. return 0;
  57956. }
  57957. /*
  57958. * Comparisons (x >= y, x > y, x <= y, x < y)
  57959. *
  57960. * E5 Section 11.8.5: implement 'x < y' and then use negate and eval_left_first
  57961. * flags to get the rest.
  57962. */
  57963. /* XXX: this should probably just operate on the stack top, because it
  57964. * needs to push stuff on the stack anyway...
  57965. */
  57966. 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) {
  57967. duk_size_t prefix_len;
  57968. duk_small_int_t rc;
  57969. prefix_len = (len1 <= len2 ? len1 : len2);
  57970. /* XXX: this special case can now be removed with DUK_MEMCMP */
  57971. /* memcmp() should return zero (equal) for zero length, but avoid
  57972. * it because there are some platform specific bugs. Don't use
  57973. * strncmp() because it stops comparing at a NUL.
  57974. */
  57975. if (prefix_len == 0) {
  57976. rc = 0;
  57977. } else {
  57978. rc = DUK_MEMCMP((const char *) buf1,
  57979. (const char *) buf2,
  57980. prefix_len);
  57981. }
  57982. if (rc < 0) {
  57983. return -1;
  57984. } else if (rc > 0) {
  57985. return 1;
  57986. }
  57987. /* prefix matches, lengths matter now */
  57988. if (len1 < len2) {
  57989. /* e.g. "x" < "xx" */
  57990. return -1;
  57991. } else if (len1 > len2) {
  57992. return 1;
  57993. }
  57994. return 0;
  57995. }
  57996. DUK_INTERNAL duk_small_int_t duk_js_string_compare(duk_hstring *h1, duk_hstring *h2) {
  57997. /*
  57998. * String comparison (E5 Section 11.8.5, step 4), which
  57999. * needs to compare codepoint by codepoint.
  58000. *
  58001. * However, UTF-8 allows us to use strcmp directly: the shared
  58002. * prefix will be encoded identically (UTF-8 has unique encoding)
  58003. * and the first differing character can be compared with a simple
  58004. * unsigned byte comparison (which strcmp does).
  58005. *
  58006. * This will not work properly for non-xutf-8 strings, but this
  58007. * is not an issue for compliance.
  58008. */
  58009. DUK_ASSERT(h1 != NULL);
  58010. DUK_ASSERT(h2 != NULL);
  58011. return duk_js_data_compare((const duk_uint8_t *) DUK_HSTRING_GET_DATA(h1),
  58012. (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h2),
  58013. (duk_size_t) DUK_HSTRING_GET_BYTELEN(h1),
  58014. (duk_size_t) DUK_HSTRING_GET_BYTELEN(h2));
  58015. }
  58016. #if 0 /* unused */
  58017. DUK_INTERNAL duk_small_int_t duk_js_buffer_compare(duk_heap *heap, duk_hbuffer *h1, duk_hbuffer *h2) {
  58018. /* Similar to String comparison. */
  58019. DUK_ASSERT(h1 != NULL);
  58020. DUK_ASSERT(h2 != NULL);
  58021. DUK_UNREF(heap);
  58022. return duk_js_data_compare((const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(heap, h1),
  58023. (const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(heap, h2),
  58024. (duk_size_t) DUK_HBUFFER_GET_SIZE(h1),
  58025. (duk_size_t) DUK_HBUFFER_GET_SIZE(h2));
  58026. }
  58027. #endif
  58028. 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) {
  58029. duk_context *ctx = (duk_context *) thr;
  58030. duk_double_t d1, d2;
  58031. duk_small_int_t c1, c2;
  58032. duk_small_int_t s1, s2;
  58033. duk_small_int_t rc;
  58034. duk_bool_t retval;
  58035. /* Fast path for fastints */
  58036. #if defined(DUK_USE_FASTINT)
  58037. if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) {
  58038. duk_int64_t v1 = DUK_TVAL_GET_FASTINT(tv_x);
  58039. duk_int64_t v2 = DUK_TVAL_GET_FASTINT(tv_y);
  58040. if (v1 < v2) {
  58041. /* 'lt is true' */
  58042. retval = 1;
  58043. } else {
  58044. retval = 0;
  58045. }
  58046. if (flags & DUK_COMPARE_FLAG_NEGATE) {
  58047. retval ^= 1;
  58048. }
  58049. return retval;
  58050. }
  58051. #endif /* DUK_USE_FASTINT */
  58052. /* Fast path for numbers (one of which may be a fastint) */
  58053. #if 1 /* XXX: make fast paths optional for size minimization? */
  58054. if (DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y)) {
  58055. d1 = DUK_TVAL_GET_NUMBER(tv_x);
  58056. d2 = DUK_TVAL_GET_NUMBER(tv_y);
  58057. c1 = DUK_FPCLASSIFY(d1);
  58058. c2 = DUK_FPCLASSIFY(d2);
  58059. if (c1 == DUK_FP_NORMAL && c2 == DUK_FP_NORMAL) {
  58060. /* XXX: this is a very narrow check, and doesn't cover
  58061. * zeroes, subnormals, infinities, which compare normally.
  58062. */
  58063. if (d1 < d2) {
  58064. /* 'lt is true' */
  58065. retval = 1;
  58066. } else {
  58067. retval = 0;
  58068. }
  58069. if (flags & DUK_COMPARE_FLAG_NEGATE) {
  58070. retval ^= 1;
  58071. }
  58072. return retval;
  58073. }
  58074. }
  58075. #endif
  58076. /* Slow path */
  58077. duk_push_tval(ctx, tv_x);
  58078. duk_push_tval(ctx, tv_y);
  58079. if (flags & DUK_COMPARE_FLAG_EVAL_LEFT_FIRST) {
  58080. duk_to_primitive(ctx, -2, DUK_HINT_NUMBER);
  58081. duk_to_primitive(ctx, -1, DUK_HINT_NUMBER);
  58082. } else {
  58083. duk_to_primitive(ctx, -1, DUK_HINT_NUMBER);
  58084. duk_to_primitive(ctx, -2, DUK_HINT_NUMBER);
  58085. }
  58086. /* Note: reuse variables */
  58087. tv_x = duk_get_tval(ctx, -2);
  58088. tv_y = duk_get_tval(ctx, -1);
  58089. if (DUK_TVAL_IS_STRING(tv_x) && DUK_TVAL_IS_STRING(tv_y)) {
  58090. duk_hstring *h1 = DUK_TVAL_GET_STRING(tv_x);
  58091. duk_hstring *h2 = DUK_TVAL_GET_STRING(tv_y);
  58092. DUK_ASSERT(h1 != NULL);
  58093. DUK_ASSERT(h2 != NULL);
  58094. rc = duk_js_string_compare(h1, h2);
  58095. if (rc < 0) {
  58096. goto lt_true;
  58097. } else {
  58098. goto lt_false;
  58099. }
  58100. } else {
  58101. /* Ordering should not matter (E5 Section 11.8.5, step 3.a) but
  58102. * preserve it just in case.
  58103. */
  58104. if (flags & DUK_COMPARE_FLAG_EVAL_LEFT_FIRST) {
  58105. d1 = duk_to_number(ctx, -2);
  58106. d2 = duk_to_number(ctx, -1);
  58107. } else {
  58108. d2 = duk_to_number(ctx, -1);
  58109. d1 = duk_to_number(ctx, -2);
  58110. }
  58111. c1 = (duk_small_int_t) DUK_FPCLASSIFY(d1);
  58112. s1 = (duk_small_int_t) DUK_SIGNBIT(d1);
  58113. c2 = (duk_small_int_t) DUK_FPCLASSIFY(d2);
  58114. s2 = (duk_small_int_t) DUK_SIGNBIT(d2);
  58115. if (c1 == DUK_FP_NAN || c2 == DUK_FP_NAN) {
  58116. goto lt_undefined;
  58117. }
  58118. if (c1 == DUK_FP_ZERO && c2 == DUK_FP_ZERO) {
  58119. /* For all combinations: +0 < +0, +0 < -0, -0 < +0, -0 < -0,
  58120. * steps e, f, and g.
  58121. */
  58122. goto lt_false;
  58123. }
  58124. if (d1 == d2) {
  58125. goto lt_false;
  58126. }
  58127. if (c1 == DUK_FP_INFINITE && s1 == 0) {
  58128. /* x == +Infinity */
  58129. goto lt_false;
  58130. }
  58131. if (c2 == DUK_FP_INFINITE && s2 == 0) {
  58132. /* y == +Infinity */
  58133. goto lt_true;
  58134. }
  58135. if (c2 == DUK_FP_INFINITE && s2 != 0) {
  58136. /* y == -Infinity */
  58137. goto lt_false;
  58138. }
  58139. if (c1 == DUK_FP_INFINITE && s1 != 0) {
  58140. /* x == -Infinity */
  58141. goto lt_true;
  58142. }
  58143. if (d1 < d2) {
  58144. goto lt_true;
  58145. }
  58146. goto lt_false;
  58147. }
  58148. lt_undefined:
  58149. /* Note: undefined from Section 11.8.5 always results in false
  58150. * return (see e.g. Section 11.8.3) - hence special treatment here.
  58151. */
  58152. retval = 0;
  58153. goto cleanup;
  58154. lt_true:
  58155. if (flags & DUK_COMPARE_FLAG_NEGATE) {
  58156. retval = 0;
  58157. goto cleanup;
  58158. } else {
  58159. retval = 1;
  58160. goto cleanup;
  58161. }
  58162. /* never here */
  58163. lt_false:
  58164. if (flags & DUK_COMPARE_FLAG_NEGATE) {
  58165. retval = 1;
  58166. goto cleanup;
  58167. } else {
  58168. retval = 0;
  58169. goto cleanup;
  58170. }
  58171. /* never here */
  58172. cleanup:
  58173. duk_pop_2(ctx);
  58174. return retval;
  58175. }
  58176. /*
  58177. * instanceof
  58178. */
  58179. /*
  58180. * E5 Section 11.8.6 describes the main algorithm, which uses
  58181. * [[HasInstance]]. [[HasInstance]] is defined for only
  58182. * function objects:
  58183. *
  58184. * - Normal functions:
  58185. * E5 Section 15.3.5.3
  58186. * - Functions established with Function.prototype.bind():
  58187. * E5 Section 15.3.4.5.3
  58188. *
  58189. * For other objects, a TypeError is thrown.
  58190. */
  58191. DUK_INTERNAL duk_bool_t duk_js_instanceof(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y) {
  58192. duk_context *ctx = (duk_context *) thr;
  58193. duk_hobject *func;
  58194. duk_hobject *val;
  58195. duk_hobject *proto;
  58196. duk_uint_t sanity;
  58197. /*
  58198. * Get the values onto the stack first. It would be possible to cover
  58199. * some normal cases without resorting to the value stack.
  58200. *
  58201. * The right hand side could be a light function (as they generally
  58202. * behave like objects). Light functions never have a 'prototype'
  58203. * property so E5.1 Section 15.3.5.3 step 3 always throws a TypeError.
  58204. * Using duk_require_hobject() is thus correct (except for error msg).
  58205. */
  58206. duk_push_tval(ctx, tv_x);
  58207. duk_push_tval(ctx, tv_y);
  58208. func = duk_require_hobject(ctx, -1);
  58209. /*
  58210. * For bound objects, [[HasInstance]] just calls the target function
  58211. * [[HasInstance]]. If that is again a bound object, repeat until
  58212. * we find a non-bound Function object.
  58213. */
  58214. /* XXX: this bound function resolution also happens elsewhere,
  58215. * move into a shared helper.
  58216. */
  58217. sanity = DUK_HOBJECT_BOUND_CHAIN_SANITY;
  58218. do {
  58219. /* check func supports [[HasInstance]] (this is checked for every function
  58220. * in the bound chain, including the final one)
  58221. */
  58222. if (!DUK_HOBJECT_IS_CALLABLE(func)) {
  58223. /*
  58224. * Note: of native Ecmascript objects, only Function instances
  58225. * have a [[HasInstance]] internal property. Custom objects might
  58226. * also have it, but not in current implementation.
  58227. *
  58228. * XXX: add a separate flag, DUK_HOBJECT_FLAG_ALLOW_INSTANCEOF?
  58229. */
  58230. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "invalid instanceof rval");
  58231. }
  58232. if (!DUK_HOBJECT_HAS_BOUND(func)) {
  58233. break;
  58234. }
  58235. /* [ ... lval rval ] */
  58236. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_TARGET); /* -> [ ... lval rval new_rval ] */
  58237. duk_replace(ctx, -1); /* -> [ ... lval new_rval ] */
  58238. func = duk_require_hobject(ctx, -1);
  58239. /* func support for [[HasInstance]] checked in the beginning of the loop */
  58240. } while (--sanity > 0);
  58241. if (sanity == 0) {
  58242. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_BOUND_CHAIN_LIMIT);
  58243. }
  58244. /*
  58245. * 'func' is now a non-bound object which supports [[HasInstance]]
  58246. * (which here just means DUK_HOBJECT_FLAG_CALLABLE). Move on
  58247. * to execute E5 Section 15.3.5.3.
  58248. */
  58249. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func));
  58250. DUK_ASSERT(DUK_HOBJECT_IS_CALLABLE(func));
  58251. /* [ ... lval rval(func) ] */
  58252. /* Handle lightfuncs through object coercion for now. */
  58253. /* XXX: direct implementation */
  58254. val = duk_get_hobject_or_lfunc_coerce(ctx, -2);
  58255. if (!val) {
  58256. goto pop_and_false;
  58257. }
  58258. duk_get_prop_stridx(ctx, -1, DUK_STRIDX_PROTOTYPE); /* -> [ ... lval rval rval.prototype ] */
  58259. proto = duk_require_hobject(ctx, -1);
  58260. duk_pop(ctx); /* -> [ ... lval rval ] */
  58261. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  58262. do {
  58263. /*
  58264. * Note: prototype chain is followed BEFORE first comparison. This
  58265. * means that the instanceof lval is never itself compared to the
  58266. * rval.prototype property. This is apparently intentional, see E5
  58267. * Section 15.3.5.3, step 4.a.
  58268. *
  58269. * Also note:
  58270. *
  58271. * js> (function() {}) instanceof Function
  58272. * true
  58273. * js> Function instanceof Function
  58274. * true
  58275. *
  58276. * For the latter, h_proto will be Function.prototype, which is the
  58277. * built-in Function prototype. Because Function.[[Prototype]] is
  58278. * also the built-in Function prototype, the result is true.
  58279. */
  58280. val = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, val);
  58281. if (!val) {
  58282. goto pop_and_false;
  58283. } else if (val == proto) {
  58284. goto pop_and_true;
  58285. }
  58286. /* follow prototype chain */
  58287. } while (--sanity > 0);
  58288. if (sanity == 0) {
  58289. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  58290. }
  58291. DUK_UNREACHABLE();
  58292. pop_and_false:
  58293. duk_pop_2(ctx);
  58294. return 0;
  58295. pop_and_true:
  58296. duk_pop_2(ctx);
  58297. return 1;
  58298. }
  58299. /*
  58300. * in
  58301. */
  58302. /*
  58303. * E5 Sections 11.8.7, 8.12.6.
  58304. *
  58305. * Basically just a property existence check using [[HasProperty]].
  58306. */
  58307. DUK_INTERNAL duk_bool_t duk_js_in(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y) {
  58308. duk_context *ctx = (duk_context *) thr;
  58309. duk_bool_t retval;
  58310. /*
  58311. * Get the values onto the stack first. It would be possible to cover
  58312. * some normal cases without resorting to the value stack (e.g. if
  58313. * lval is already a string).
  58314. */
  58315. /* XXX: The ES5/5.1/6 specifications require that the key in 'key in obj'
  58316. * must be string coerced before the internal HasProperty() algorithm is
  58317. * invoked. A fast path skipping coercion could be safely implemented for
  58318. * numbers (as number-to-string coercion has no side effects). For ES6
  58319. * proxy behavior, the trap 'key' argument must be in a string coerced
  58320. * form (which is a shame).
  58321. */
  58322. /* TypeError if rval is not an object (or lightfunc which should behave
  58323. * like a Function instance).
  58324. */
  58325. duk_push_tval(ctx, tv_x);
  58326. duk_push_tval(ctx, tv_y);
  58327. duk_require_type_mask(ctx, -1, DUK_TYPE_MASK_OBJECT | DUK_TYPE_MASK_LIGHTFUNC);
  58328. duk_to_string(ctx, -2); /* coerce lval with ToString() */
  58329. retval = duk_hobject_hasprop(thr, duk_get_tval(ctx, -1), duk_get_tval(ctx, -2));
  58330. duk_pop_2(ctx);
  58331. return retval;
  58332. }
  58333. /*
  58334. * typeof
  58335. *
  58336. * E5 Section 11.4.3.
  58337. *
  58338. * Very straightforward. The only question is what to return for our
  58339. * non-standard tag / object types.
  58340. *
  58341. * There is an unfortunate string constant define naming problem with
  58342. * typeof return values for e.g. "Object" and "object"; careful with
  58343. * the built-in string defines. The LC_XXX defines are used for the
  58344. * lowercase variants now.
  58345. */
  58346. DUK_INTERNAL duk_hstring *duk_js_typeof(duk_hthread *thr, duk_tval *tv_x) {
  58347. duk_small_int_t stridx = 0;
  58348. switch (DUK_TVAL_GET_TAG(tv_x)) {
  58349. case DUK_TAG_UNDEFINED: {
  58350. stridx = DUK_STRIDX_LC_UNDEFINED;
  58351. break;
  58352. }
  58353. case DUK_TAG_NULL: {
  58354. /* Note: not a typo, "object" is returned for a null value */
  58355. stridx = DUK_STRIDX_LC_OBJECT;
  58356. break;
  58357. }
  58358. case DUK_TAG_BOOLEAN: {
  58359. stridx = DUK_STRIDX_LC_BOOLEAN;
  58360. break;
  58361. }
  58362. case DUK_TAG_POINTER: {
  58363. /* implementation specific */
  58364. stridx = DUK_STRIDX_LC_POINTER;
  58365. break;
  58366. }
  58367. case DUK_TAG_STRING: {
  58368. stridx = DUK_STRIDX_LC_STRING;
  58369. break;
  58370. }
  58371. case DUK_TAG_OBJECT: {
  58372. duk_hobject *obj = DUK_TVAL_GET_OBJECT(tv_x);
  58373. DUK_ASSERT(obj != NULL);
  58374. if (DUK_HOBJECT_IS_CALLABLE(obj)) {
  58375. stridx = DUK_STRIDX_LC_FUNCTION;
  58376. } else {
  58377. stridx = DUK_STRIDX_LC_OBJECT;
  58378. }
  58379. break;
  58380. }
  58381. case DUK_TAG_BUFFER: {
  58382. /* implementation specific */
  58383. stridx = DUK_STRIDX_LC_BUFFER;
  58384. break;
  58385. }
  58386. case DUK_TAG_LIGHTFUNC: {
  58387. stridx = DUK_STRIDX_LC_FUNCTION;
  58388. break;
  58389. }
  58390. #if defined(DUK_USE_FASTINT)
  58391. case DUK_TAG_FASTINT:
  58392. #endif
  58393. default: {
  58394. /* number */
  58395. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_x));
  58396. stridx = DUK_STRIDX_LC_NUMBER;
  58397. break;
  58398. }
  58399. }
  58400. DUK_ASSERT(stridx >= 0 && stridx < DUK_HEAP_NUM_STRINGS);
  58401. return DUK_HTHREAD_GET_STRING(thr, stridx);
  58402. }
  58403. /*
  58404. * Array index and length
  58405. *
  58406. * Array index: E5 Section 15.4
  58407. * Array length: E5 Section 15.4.5.1 steps 3.c - 3.d (array length write)
  58408. *
  58409. * The DUK_HSTRING_GET_ARRIDX_SLOW() and DUK_HSTRING_GET_ARRIDX_FAST() macros
  58410. * call duk_js_to_arrayindex_string_helper().
  58411. */
  58412. 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) {
  58413. duk_uarridx_t res, new_res;
  58414. if (blen == 0 || blen > 10) {
  58415. goto parse_fail;
  58416. }
  58417. if (str[0] == (duk_uint8_t) '0' && blen > 1) {
  58418. goto parse_fail;
  58419. }
  58420. /* Accept 32-bit decimal integers, no leading zeroes, signs, etc.
  58421. * Leading zeroes are not accepted (zero index "0" is an exception
  58422. * handled above).
  58423. */
  58424. res = 0;
  58425. while (blen-- > 0) {
  58426. duk_uint8_t c = *str++;
  58427. if (c >= (duk_uint8_t) '0' && c <= (duk_uint8_t) '9') {
  58428. new_res = res * 10 + (duk_uint32_t) (c - (duk_uint8_t) '0');
  58429. if (new_res < res) {
  58430. /* overflow, more than 32 bits -> not an array index */
  58431. goto parse_fail;
  58432. }
  58433. res = new_res;
  58434. } else {
  58435. goto parse_fail;
  58436. }
  58437. }
  58438. *out_idx = res;
  58439. return 1;
  58440. parse_fail:
  58441. *out_idx = DUK_HSTRING_NO_ARRAY_INDEX;
  58442. return 0;
  58443. }
  58444. /* Called by duk_hstring.h macros */
  58445. DUK_INTERNAL duk_uarridx_t duk_js_to_arrayindex_string_helper(duk_hstring *h) {
  58446. duk_uarridx_t res;
  58447. duk_small_int_t rc;
  58448. if (!DUK_HSTRING_HAS_ARRIDX(h)) {
  58449. return DUK_HSTRING_NO_ARRAY_INDEX;
  58450. }
  58451. rc = duk_js_to_arrayindex_raw_string(DUK_HSTRING_GET_DATA(h),
  58452. DUK_HSTRING_GET_BYTELEN(h),
  58453. &res);
  58454. DUK_UNREF(rc);
  58455. DUK_ASSERT(rc != 0);
  58456. return res;
  58457. }
  58458. #line 1 "duk_js_var.c"
  58459. /*
  58460. * Identifier access and function closure handling.
  58461. *
  58462. * Provides the primitives for slow path identifier accesses: GETVAR,
  58463. * PUTVAR, DELVAR, etc. The fast path, direct register accesses, should
  58464. * be used for most identifier accesses. Consequently, these slow path
  58465. * primitives should be optimized for maximum compactness.
  58466. *
  58467. * Ecmascript environment records (declarative and object) are represented
  58468. * as internal objects with control keys. Environment records have a
  58469. * parent record ("outer environment reference") which is represented by
  58470. * the implicit prototype for technical reasons (in other words, it is a
  58471. * convenient field). The prototype chain is not followed in the ordinary
  58472. * sense for variable lookups.
  58473. *
  58474. * See identifier-handling.txt for more details on the identifier algorithms
  58475. * and the internal representation. See function-objects.txt for details on
  58476. * what function templates and instances are expected to look like.
  58477. *
  58478. * Care must be taken to avoid duk_tval pointer invalidation caused by
  58479. * e.g. value stack or object resizing.
  58480. *
  58481. * TODO: properties for function instances could be initialized much more
  58482. * efficiently by creating a property allocation for a certain size and
  58483. * filling in keys and values directly (and INCREFing both with "bulk incref"
  58484. * primitives.
  58485. *
  58486. * XXX: duk_hobject_getprop() and duk_hobject_putprop() calls are a bit
  58487. * awkward (especially because they follow the prototype chain); rework
  58488. * if "raw" own property helpers are added.
  58489. */
  58490. /* include removed: duk_internal.h */
  58491. /*
  58492. * Local result type for duk__get_identifier_reference() lookup.
  58493. */
  58494. typedef struct {
  58495. duk_hobject *holder; /* for object-bound identifiers */
  58496. duk_tval *value; /* for register-bound and declarative env identifiers */
  58497. duk_int_t attrs; /* property attributes for identifier (relevant if value != NULL) */
  58498. duk_tval *this_binding;
  58499. duk_hobject *env;
  58500. } duk__id_lookup_result;
  58501. /*
  58502. * Create a new function object based on a "template function" which contains
  58503. * compiled bytecode, constants, etc, but lacks a lexical environment.
  58504. *
  58505. * Ecmascript requires that each created closure is a separate object, with
  58506. * its own set of editable properties. However, structured property values
  58507. * (such as the formal arguments list and the variable map) are shared.
  58508. * Also the bytecode, constants, and inner functions are shared.
  58509. *
  58510. * See E5 Section 13.2 for detailed requirements on the function objects;
  58511. * there are no similar requirements for function "templates" which are an
  58512. * implementation dependent internal feature. Also see function-objects.txt
  58513. * for a discussion on the function instance properties provided by this
  58514. * implementation.
  58515. *
  58516. * Notes:
  58517. *
  58518. * * Order of internal properties should match frequency of use, since the
  58519. * properties will be linearly scanned on lookup (functions usually don't
  58520. * have enough properties to warrant a hash part).
  58521. *
  58522. * * The created closure is independent of its template; they do share the
  58523. * same 'data' buffer object, but the template object itself can be freed
  58524. * even if the closure object remains reachable.
  58525. */
  58526. DUK_LOCAL void duk__inc_data_inner_refcounts(duk_hthread *thr, duk_hcompiledfunction *f) {
  58527. duk_tval *tv, *tv_end;
  58528. duk_hobject **funcs, **funcs_end;
  58529. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, f) != NULL); /* compiled functions must be created 'atomically' */
  58530. DUK_UNREF(thr);
  58531. tv = DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, f);
  58532. tv_end = DUK_HCOMPILEDFUNCTION_GET_CONSTS_END(thr->heap, f);
  58533. while (tv < tv_end) {
  58534. DUK_TVAL_INCREF(thr, tv);
  58535. tv++;
  58536. }
  58537. funcs = DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, f);
  58538. funcs_end = DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(thr->heap, f);
  58539. while (funcs < funcs_end) {
  58540. DUK_HEAPHDR_INCREF(thr, (duk_heaphdr *) *funcs);
  58541. funcs++;
  58542. }
  58543. }
  58544. /* Push a new closure on the stack.
  58545. *
  58546. * Note: if fun_temp has NEWENV, i.e. a new lexical and variable declaration
  58547. * is created when the function is called, only outer_lex_env matters
  58548. * (outer_var_env is ignored and may or may not be same as outer_lex_env).
  58549. */
  58550. DUK_LOCAL const duk_uint16_t duk__closure_copy_proplist[] = {
  58551. /* order: most frequent to least frequent */
  58552. DUK_STRIDX_INT_VARMAP,
  58553. DUK_STRIDX_INT_FORMALS,
  58554. DUK_STRIDX_NAME,
  58555. DUK_STRIDX_INT_PC2LINE,
  58556. DUK_STRIDX_FILE_NAME,
  58557. DUK_STRIDX_INT_SOURCE
  58558. };
  58559. DUK_INTERNAL
  58560. void duk_js_push_closure(duk_hthread *thr,
  58561. duk_hcompiledfunction *fun_temp,
  58562. duk_hobject *outer_var_env,
  58563. duk_hobject *outer_lex_env) {
  58564. duk_context *ctx = (duk_context *) thr;
  58565. duk_hcompiledfunction *fun_clos;
  58566. duk_small_uint_t i;
  58567. duk_uint_t len_value;
  58568. DUK_ASSERT(fun_temp != NULL);
  58569. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, fun_temp) != NULL);
  58570. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_FUNCS(thr->heap, fun_temp) != NULL);
  58571. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_BYTECODE(thr->heap, fun_temp) != NULL);
  58572. DUK_ASSERT(outer_var_env != NULL);
  58573. DUK_ASSERT(outer_lex_env != NULL);
  58574. duk_push_compiledfunction(ctx);
  58575. duk_push_hobject(ctx, &fun_temp->obj); /* -> [ ... closure template ] */
  58576. fun_clos = (duk_hcompiledfunction *) duk_get_hcompiledfunction(ctx, -2);
  58577. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) fun_clos));
  58578. DUK_ASSERT(fun_clos != NULL);
  58579. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, fun_clos) == NULL);
  58580. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_FUNCS(thr->heap, fun_clos) == NULL);
  58581. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_BYTECODE(thr->heap, fun_clos) == NULL);
  58582. DUK_HCOMPILEDFUNCTION_SET_DATA(thr->heap, fun_clos, DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, fun_temp));
  58583. DUK_HCOMPILEDFUNCTION_SET_FUNCS(thr->heap, fun_clos, DUK_HCOMPILEDFUNCTION_GET_FUNCS(thr->heap, fun_temp));
  58584. DUK_HCOMPILEDFUNCTION_SET_BYTECODE(thr->heap, fun_clos, DUK_HCOMPILEDFUNCTION_GET_BYTECODE(thr->heap, fun_temp));
  58585. /* Note: all references inside 'data' need to get their refcounts
  58586. * upped too. This is the case because refcounts are decreased
  58587. * through every function referencing 'data' independently.
  58588. */
  58589. DUK_HBUFFER_INCREF(thr, DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, fun_clos));
  58590. duk__inc_data_inner_refcounts(thr, fun_temp);
  58591. fun_clos->nregs = fun_temp->nregs;
  58592. fun_clos->nargs = fun_temp->nargs;
  58593. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  58594. fun_clos->start_line = fun_temp->start_line;
  58595. fun_clos->end_line = fun_temp->end_line;
  58596. #endif
  58597. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, fun_clos) != NULL);
  58598. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_FUNCS(thr->heap, fun_clos) != NULL);
  58599. DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_BYTECODE(thr->heap, fun_clos) != NULL);
  58600. /* XXX: could also copy from template, but there's no way to have any
  58601. * other value here now (used code has no access to the template).
  58602. */
  58603. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, &fun_clos->obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  58604. /*
  58605. * Init/assert flags, copying them where appropriate. Some flags
  58606. * (like NEWENV) are processed separately below.
  58607. */
  58608. /* XXX: copy flags using a mask */
  58609. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(&fun_clos->obj));
  58610. DUK_HOBJECT_SET_CONSTRUCTABLE(&fun_clos->obj); /* Note: not set in template (has no "prototype") */
  58611. DUK_ASSERT(DUK_HOBJECT_HAS_CONSTRUCTABLE(&fun_clos->obj));
  58612. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(&fun_clos->obj));
  58613. DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(&fun_clos->obj));
  58614. DUK_ASSERT(!DUK_HOBJECT_HAS_NATIVEFUNCTION(&fun_clos->obj));
  58615. DUK_ASSERT(!DUK_HOBJECT_HAS_THREAD(&fun_clos->obj));
  58616. /* DUK_HOBJECT_FLAG_ARRAY_PART: don't care */
  58617. if (DUK_HOBJECT_HAS_STRICT(&fun_temp->obj)) {
  58618. DUK_HOBJECT_SET_STRICT(&fun_clos->obj);
  58619. }
  58620. if (DUK_HOBJECT_HAS_NOTAIL(&fun_temp->obj)) {
  58621. DUK_HOBJECT_SET_NOTAIL(&fun_clos->obj);
  58622. }
  58623. /* DUK_HOBJECT_FLAG_NEWENV: handled below */
  58624. DUK_ASSERT(!DUK_HOBJECT_HAS_NAMEBINDING(&fun_clos->obj));
  58625. if (DUK_HOBJECT_HAS_CREATEARGS(&fun_temp->obj)) {
  58626. DUK_HOBJECT_SET_CREATEARGS(&fun_clos->obj);
  58627. }
  58628. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARRAY(&fun_clos->obj));
  58629. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(&fun_clos->obj));
  58630. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(&fun_clos->obj));
  58631. /*
  58632. * Setup environment record properties based on the template and
  58633. * its flags.
  58634. *
  58635. * If DUK_HOBJECT_HAS_NEWENV(fun_temp) is true, the environment
  58636. * records represent identifiers "outside" the function; the
  58637. * "inner" environment records are created on demand. Otherwise,
  58638. * the environment records are those that will be directly used
  58639. * (e.g. for declarations).
  58640. *
  58641. * _Lexenv is always set; _Varenv defaults to _Lexenv if missing,
  58642. * so _Varenv is only set if _Lexenv != _Varenv.
  58643. *
  58644. * This is relatively complex, see doc/identifier-handling.txt.
  58645. */
  58646. if (DUK_HOBJECT_HAS_NEWENV(&fun_temp->obj)) {
  58647. DUK_HOBJECT_SET_NEWENV(&fun_clos->obj);
  58648. if (DUK_HOBJECT_HAS_NAMEBINDING(&fun_temp->obj)) {
  58649. duk_hobject *proto;
  58650. /*
  58651. * Named function expression, name needs to be bound
  58652. * in an intermediate environment record. The "outer"
  58653. * lexical/variable environment will thus be:
  58654. *
  58655. * a) { funcname: <func>, __prototype: outer_lex_env }
  58656. * b) { funcname: <func>, __prototype: <globalenv> } (if outer_lex_env missing)
  58657. */
  58658. DUK_ASSERT(duk_has_prop_stridx(ctx, -1, DUK_STRIDX_NAME)); /* required if NAMEBINDING set */
  58659. if (outer_lex_env) {
  58660. proto = outer_lex_env;
  58661. } else {
  58662. proto = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  58663. }
  58664. /* -> [ ... closure template env ] */
  58665. (void) duk_push_object_helper_proto(ctx,
  58666. DUK_HOBJECT_FLAG_EXTENSIBLE |
  58667. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV),
  58668. proto);
  58669. /* It's important that duk_xdef_prop() is a 'raw define' so that any
  58670. * properties in an ancestor are never an issue (they should never be
  58671. * e.g. non-writable, but just in case).
  58672. */
  58673. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_NAME); /* -> [ ... closure template env funcname ] */
  58674. duk_dup(ctx, -4); /* -> [ ... closure template env funcname closure ] */
  58675. duk_xdef_prop(ctx, -3, DUK_PROPDESC_FLAGS_NONE); /* -> [ ... closure template env ] */
  58676. /* env[funcname] = closure */
  58677. /* [ ... closure template env ] */
  58678. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_INT_LEXENV, DUK_PROPDESC_FLAGS_WC);
  58679. /* since closure has NEWENV, never define DUK_STRIDX_INT_VARENV, as it
  58680. * will be ignored anyway
  58681. */
  58682. /* [ ... closure template ] */
  58683. } else {
  58684. /*
  58685. * Other cases (function declaration, anonymous function expression,
  58686. * strict direct eval code). The "outer" environment will be whatever
  58687. * the caller gave us.
  58688. */
  58689. duk_push_hobject(ctx, outer_lex_env); /* -> [ ... closure template env ] */
  58690. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_INT_LEXENV, DUK_PROPDESC_FLAGS_WC);
  58691. /* since closure has NEWENV, never define DUK_STRIDX_INT_VARENV, as it
  58692. * will be ignored anyway
  58693. */
  58694. /* [ ... closure template ] */
  58695. }
  58696. } else {
  58697. /*
  58698. * Function gets no new environment when called. This is the
  58699. * case for global code, indirect eval code, and non-strict
  58700. * direct eval code. There is no direct correspondence to the
  58701. * E5 specification, as global/eval code is not exposed as a
  58702. * function.
  58703. */
  58704. DUK_ASSERT(!DUK_HOBJECT_HAS_NAMEBINDING(&fun_temp->obj));
  58705. duk_push_hobject(ctx, outer_lex_env); /* -> [ ... closure template env ] */
  58706. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_INT_LEXENV, DUK_PROPDESC_FLAGS_WC);
  58707. if (outer_var_env != outer_lex_env) {
  58708. duk_push_hobject(ctx, outer_var_env); /* -> [ ... closure template env ] */
  58709. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_INT_VARENV, DUK_PROPDESC_FLAGS_WC);
  58710. }
  58711. }
  58712. #ifdef DUK_USE_DDDPRINT
  58713. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_INT_VARENV);
  58714. duk_get_prop_stridx(ctx, -3, DUK_STRIDX_INT_LEXENV);
  58715. DUK_DDD(DUK_DDDPRINT("closure varenv -> %!ipT, lexenv -> %!ipT",
  58716. (duk_tval *) duk_get_tval(ctx, -2),
  58717. (duk_tval *) duk_get_tval(ctx, -1)));
  58718. duk_pop_2(ctx);
  58719. #endif
  58720. /*
  58721. * Copy some internal properties directly
  58722. *
  58723. * The properties will be writable and configurable, but not enumerable.
  58724. */
  58725. /* [ ... closure template ] */
  58726. DUK_DDD(DUK_DDDPRINT("copying properties: closure=%!iT, template=%!iT",
  58727. (duk_tval *) duk_get_tval(ctx, -2),
  58728. (duk_tval *) duk_get_tval(ctx, -1)));
  58729. for (i = 0; i < (duk_small_uint_t) (sizeof(duk__closure_copy_proplist) / sizeof(duk_uint16_t)); i++) {
  58730. duk_small_int_t stridx = (duk_small_int_t) duk__closure_copy_proplist[i];
  58731. if (duk_get_prop_stridx(ctx, -1, stridx)) {
  58732. /* [ ... closure template val ] */
  58733. DUK_DDD(DUK_DDDPRINT("copying property, stridx=%ld -> found", (long) stridx));
  58734. duk_xdef_prop_stridx(ctx, -3, stridx, DUK_PROPDESC_FLAGS_WC);
  58735. } else {
  58736. DUK_DDD(DUK_DDDPRINT("copying property, stridx=%ld -> not found", (long) stridx));
  58737. duk_pop(ctx);
  58738. }
  58739. }
  58740. /*
  58741. * "length" maps to number of formals (E5 Section 13.2) for function
  58742. * declarations/expressions (non-bound functions). Note that 'nargs'
  58743. * is NOT necessarily equal to the number of arguments.
  58744. */
  58745. /* [ ... closure template ] */
  58746. len_value = 0;
  58747. /* XXX: use helper for size optimization */
  58748. if (duk_get_prop_stridx(ctx, -2, DUK_STRIDX_INT_FORMALS)) {
  58749. /* [ ... closure template formals ] */
  58750. DUK_ASSERT(duk_has_prop_stridx(ctx, -1, DUK_STRIDX_LENGTH));
  58751. DUK_ASSERT(duk_get_length(ctx, -1) <= DUK_UINT_MAX); /* formal arg limits */
  58752. len_value = (duk_uint_t) duk_get_length(ctx, -1);
  58753. } else {
  58754. /* XXX: what to do if _Formals is not empty but compiler has
  58755. * optimized it away -- read length from an explicit property
  58756. * then?
  58757. */
  58758. }
  58759. duk_pop(ctx);
  58760. duk_push_uint(ctx, len_value); /* [ ... closure template len_value ] */
  58761. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE);
  58762. /*
  58763. * "prototype" is, by default, a fresh object with the "constructor"
  58764. * property.
  58765. *
  58766. * Note that this creates a circular reference for every function
  58767. * instance (closure) which prevents refcount-based collection of
  58768. * function instances.
  58769. *
  58770. * XXX: Try to avoid creating the default prototype object, because
  58771. * many functions are not used as constructors and the default
  58772. * prototype is unnecessary. Perhaps it could be created on-demand
  58773. * when it is first accessed?
  58774. */
  58775. /* [ ... closure template ] */
  58776. duk_push_object(ctx); /* -> [ ... closure template newobj ] */
  58777. duk_dup(ctx, -3); /* -> [ ... closure template newobj closure ] */
  58778. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_CONSTRUCTOR, DUK_PROPDESC_FLAGS_WC); /* -> [ ... closure template newobj ] */
  58779. duk_compact(ctx, -1); /* compact the prototype */
  58780. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_PROTOTYPE, DUK_PROPDESC_FLAGS_W); /* -> [ ... closure template ] */
  58781. /*
  58782. * "arguments" and "caller" must be mapped to throwers for strict
  58783. * mode and bound functions (E5 Section 15.3.5).
  58784. *
  58785. * XXX: This is expensive to have for every strict function instance.
  58786. * Try to implement as virtual properties or on-demand created properties.
  58787. */
  58788. /* [ ... closure template ] */
  58789. if (DUK_HOBJECT_HAS_STRICT(&fun_clos->obj)) {
  58790. duk_xdef_prop_stridx_thrower(ctx, -2, DUK_STRIDX_CALLER, DUK_PROPDESC_FLAGS_NONE);
  58791. duk_xdef_prop_stridx_thrower(ctx, -2, DUK_STRIDX_LC_ARGUMENTS, DUK_PROPDESC_FLAGS_NONE);
  58792. } else {
  58793. #ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  58794. DUK_DDD(DUK_DDDPRINT("function is non-strict and non-standard 'caller' property in use, add initial 'null' value"));
  58795. duk_push_null(ctx);
  58796. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_CALLER, DUK_PROPDESC_FLAGS_NONE);
  58797. #else
  58798. DUK_DDD(DUK_DDDPRINT("function is non-strict and non-standard 'caller' property not used"));
  58799. #endif
  58800. }
  58801. /*
  58802. * "name" is a non-standard property found in at least V8, Rhino, smjs.
  58803. * For Rhino and smjs it is non-writable, non-enumerable, and non-configurable;
  58804. * for V8 it is writable, non-enumerable, non-configurable. It is also defined
  58805. * for an anonymous function expression in which case the value is an empty string.
  58806. * We could also leave name 'undefined' for anonymous functions but that would
  58807. * differ from behavior of other engines, so use an empty string.
  58808. *
  58809. * XXX: make optional? costs something per function.
  58810. */
  58811. /* [ ... closure template ] */
  58812. if (duk_get_prop_stridx(ctx, -1, DUK_STRIDX_NAME)) {
  58813. /* [ ... closure template name ] */
  58814. DUK_ASSERT(duk_is_string(ctx, -1));
  58815. } else {
  58816. /* [ ... closure template undefined ] */
  58817. duk_pop(ctx);
  58818. duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING);
  58819. }
  58820. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE); /* -> [ ... closure template ] */
  58821. /*
  58822. * Compact the closure, in most cases no properties will be added later.
  58823. * Also, without this the closures end up having unused property slots
  58824. * (e.g. in Duktape 0.9.0, 8 slots would be allocated and only 7 used).
  58825. * A better future solution would be to allocate the closure directly
  58826. * to correct size (and setup the properties directly without going
  58827. * through the API).
  58828. */
  58829. duk_compact(ctx, -2);
  58830. /*
  58831. * Some assertions (E5 Section 13.2).
  58832. */
  58833. DUK_ASSERT(DUK_HOBJECT_GET_CLASS_NUMBER(&fun_clos->obj) == DUK_HOBJECT_CLASS_FUNCTION);
  58834. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, &fun_clos->obj) == thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  58835. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(&fun_clos->obj));
  58836. DUK_ASSERT(duk_has_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH) != 0);
  58837. DUK_ASSERT(duk_has_prop_stridx(ctx, -2, DUK_STRIDX_PROTOTYPE) != 0);
  58838. DUK_ASSERT(duk_has_prop_stridx(ctx, -2, DUK_STRIDX_NAME) != 0); /* non-standard */
  58839. DUK_ASSERT(!DUK_HOBJECT_HAS_STRICT(&fun_clos->obj) ||
  58840. duk_has_prop_stridx(ctx, -2, DUK_STRIDX_CALLER) != 0);
  58841. DUK_ASSERT(!DUK_HOBJECT_HAS_STRICT(&fun_clos->obj) ||
  58842. duk_has_prop_stridx(ctx, -2, DUK_STRIDX_LC_ARGUMENTS) != 0);
  58843. /*
  58844. * Finish
  58845. */
  58846. /* [ ... closure template ] */
  58847. DUK_DDD(DUK_DDDPRINT("created function instance: template=%!iT -> closure=%!iT",
  58848. (duk_tval *) duk_get_tval(ctx, -1),
  58849. (duk_tval *) duk_get_tval(ctx, -2)));
  58850. duk_pop(ctx);
  58851. /* [ ... closure ] */
  58852. }
  58853. /*
  58854. * Delayed activation environment record initialization (for functions
  58855. * with NEWENV).
  58856. *
  58857. * The non-delayed initialization is handled by duk_handle_call().
  58858. */
  58859. /* shared helper */
  58860. DUK_INTERNAL
  58861. duk_hobject *duk_create_activation_environment_record(duk_hthread *thr,
  58862. duk_hobject *func,
  58863. duk_size_t idx_bottom) {
  58864. duk_context *ctx = (duk_context *) thr;
  58865. duk_hobject *env;
  58866. duk_hobject *parent;
  58867. duk_tval *tv;
  58868. DUK_ASSERT(thr != NULL);
  58869. DUK_ASSERT(func != NULL);
  58870. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_INT_LEXENV(thr));
  58871. if (tv) {
  58872. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  58873. DUK_ASSERT(DUK_HOBJECT_IS_ENV(DUK_TVAL_GET_OBJECT(tv)));
  58874. parent = DUK_TVAL_GET_OBJECT(tv);
  58875. } else {
  58876. parent = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  58877. }
  58878. (void) duk_push_object_helper(ctx,
  58879. DUK_HOBJECT_FLAG_EXTENSIBLE |
  58880. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV),
  58881. -1); /* no prototype, updated below */
  58882. env = duk_require_hobject(ctx, -1);
  58883. DUK_ASSERT(env != NULL);
  58884. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, env, parent); /* parent env is the prototype */
  58885. /* open scope information, for compiled functions only */
  58886. if (DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) {
  58887. duk_push_hthread(ctx, thr);
  58888. duk_xdef_prop_stridx_wec(ctx, -2, DUK_STRIDX_INT_THREAD);
  58889. duk_push_hobject(ctx, func);
  58890. duk_xdef_prop_stridx_wec(ctx, -2, DUK_STRIDX_INT_CALLEE);
  58891. duk_push_size_t(ctx, idx_bottom);
  58892. duk_xdef_prop_stridx_wec(ctx, -2, DUK_STRIDX_INT_REGBASE);
  58893. }
  58894. return env;
  58895. }
  58896. DUK_INTERNAL
  58897. void duk_js_init_activation_environment_records_delayed(duk_hthread *thr,
  58898. duk_activation *act) {
  58899. duk_context *ctx = (duk_context *) thr;
  58900. duk_hobject *func;
  58901. duk_hobject *env;
  58902. func = DUK_ACT_GET_FUNC(act);
  58903. DUK_ASSERT(func != NULL);
  58904. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func)); /* bound functions are never in act 'func' */
  58905. /*
  58906. * Delayed initialization only occurs for 'NEWENV' functions.
  58907. */
  58908. DUK_ASSERT(DUK_HOBJECT_HAS_NEWENV(func));
  58909. DUK_ASSERT(act->lex_env == NULL);
  58910. DUK_ASSERT(act->var_env == NULL);
  58911. env = duk_create_activation_environment_record(thr, func, act->idx_bottom);
  58912. DUK_ASSERT(env != NULL);
  58913. DUK_DDD(DUK_DDDPRINT("created delayed fresh env: %!ipO", (duk_heaphdr *) env));
  58914. #ifdef DUK_USE_DDDPRINT
  58915. {
  58916. duk_hobject *p = env;
  58917. while (p) {
  58918. DUK_DDD(DUK_DDDPRINT(" -> %!ipO", (duk_heaphdr *) p));
  58919. p = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, p);
  58920. }
  58921. }
  58922. #endif
  58923. act->lex_env = env;
  58924. act->var_env = env;
  58925. DUK_HOBJECT_INCREF(thr, env); /* XXX: incref by count (here 2 times) */
  58926. DUK_HOBJECT_INCREF(thr, env);
  58927. duk_pop(ctx);
  58928. }
  58929. /*
  58930. * Closing environment records.
  58931. *
  58932. * The environment record MUST be closed with the thread where its activation
  58933. * is. In other words (if 'env' is open):
  58934. *
  58935. * - 'thr' must match _env.thread
  58936. * - 'func' must match _env.callee
  58937. * - 'regbase' must match _env.regbase
  58938. *
  58939. * These are not looked up from the env to minimize code size.
  58940. *
  58941. * XXX: should access the own properties directly instead of using the API
  58942. */
  58943. DUK_INTERNAL void duk_js_close_environment_record(duk_hthread *thr, duk_hobject *env, duk_hobject *func, duk_size_t regbase) {
  58944. duk_context *ctx = (duk_context *) thr;
  58945. duk_uint_fast32_t i;
  58946. DUK_ASSERT(thr != NULL);
  58947. DUK_ASSERT(env != NULL);
  58948. /* func is NULL for lightfuncs */
  58949. if (!DUK_HOBJECT_IS_DECENV(env) || DUK_HOBJECT_HAS_ENVRECCLOSED(env)) {
  58950. DUK_DDD(DUK_DDDPRINT("environment record not a declarative record, "
  58951. "or already closed: %!iO",
  58952. (duk_heaphdr *) env));
  58953. return;
  58954. }
  58955. DUK_DDD(DUK_DDDPRINT("closing environment record: %!iO, func: %!iO, regbase: %ld",
  58956. (duk_heaphdr *) env, (duk_heaphdr *) func, (long) regbase));
  58957. duk_push_hobject(ctx, env);
  58958. /* assertions: env must be closed in the same thread as where it runs */
  58959. #ifdef DUK_USE_ASSERTIONS
  58960. {
  58961. /* [... env] */
  58962. if (duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_CALLEE)) {
  58963. DUK_ASSERT(duk_is_object(ctx, -1));
  58964. DUK_ASSERT(duk_get_hobject(ctx, -1) == (duk_hobject *) func);
  58965. }
  58966. duk_pop(ctx);
  58967. if (duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_THREAD)) {
  58968. DUK_ASSERT(duk_is_object(ctx, -1));
  58969. DUK_ASSERT(duk_get_hobject(ctx, -1) == (duk_hobject *) thr);
  58970. }
  58971. duk_pop(ctx);
  58972. if (duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_REGBASE)) {
  58973. DUK_ASSERT(duk_is_number(ctx, -1));
  58974. DUK_ASSERT(duk_get_number(ctx, -1) == (double) regbase);
  58975. }
  58976. duk_pop(ctx);
  58977. /* [... env] */
  58978. }
  58979. #endif
  58980. if (func != NULL && DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) {
  58981. duk_hobject *varmap;
  58982. duk_hstring *key;
  58983. duk_tval *tv;
  58984. duk_uint_t regnum;
  58985. /* XXX: additional conditions when to close variables? we don't want to do it
  58986. * unless the environment may have "escaped" (referenced in a function closure).
  58987. * With delayed environments, the existence is probably good enough of a check.
  58988. */
  58989. /* XXX: any way to detect faster whether something needs to be closed?
  58990. * We now look up _Callee and then skip the rest.
  58991. */
  58992. /* Note: we rely on the _Varmap having a bunch of nice properties, like:
  58993. * - being compacted and unmodified during this process
  58994. * - not containing an array part
  58995. * - having correct value types
  58996. */
  58997. /* [... env] */
  58998. if (!duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_CALLEE)) {
  58999. DUK_DDD(DUK_DDDPRINT("env has no callee property, nothing to close; re-delete the control properties just in case"));
  59000. duk_pop(ctx);
  59001. goto skip_varmap;
  59002. }
  59003. /* [... env callee] */
  59004. if (!duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VARMAP)) {
  59005. DUK_DDD(DUK_DDDPRINT("callee has no varmap property, nothing to close; delete the control properties"));
  59006. duk_pop_2(ctx);
  59007. goto skip_varmap;
  59008. }
  59009. varmap = duk_require_hobject(ctx, -1);
  59010. DUK_ASSERT(varmap != NULL);
  59011. DUK_DDD(DUK_DDDPRINT("varmap: %!O", (duk_heaphdr *) varmap));
  59012. /* [... env callee varmap] */
  59013. DUK_DDD(DUK_DDDPRINT("copying bound register values, %ld bound regs", (long) DUK_HOBJECT_GET_ENEXT(varmap)));
  59014. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(varmap); i++) {
  59015. key = DUK_HOBJECT_E_GET_KEY(thr->heap, varmap, i);
  59016. DUK_ASSERT(key != NULL); /* assume keys are compacted */
  59017. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, varmap, i)); /* assume plain values */
  59018. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, varmap, i);
  59019. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); /* assume value is a number */
  59020. regnum = (duk_uint_t) DUK_TVAL_GET_NUMBER(tv);
  59021. DUK_ASSERT_DISABLE(regnum >= 0); /* unsigned */
  59022. DUK_ASSERT(regnum < ((duk_hcompiledfunction *) func)->nregs); /* regnum is sane */
  59023. DUK_ASSERT(thr->valstack + regbase + regnum >= thr->valstack);
  59024. DUK_ASSERT(thr->valstack + regbase + regnum < thr->valstack_top);
  59025. /* XXX: slightly awkward */
  59026. duk_push_hstring(ctx, key);
  59027. duk_push_tval(ctx, thr->valstack + regbase + regnum);
  59028. DUK_DDD(DUK_DDDPRINT("closing identifier '%s' -> reg %ld, value %!T",
  59029. (const char *) duk_require_string(ctx, -2),
  59030. (long) regnum,
  59031. (duk_tval *) duk_get_tval(ctx, -1)));
  59032. /* [... env callee varmap key val] */
  59033. /* if property already exists, overwrites silently */
  59034. duk_xdef_prop(ctx, -5, DUK_PROPDESC_FLAGS_WE); /* writable but not deletable */
  59035. }
  59036. duk_pop_2(ctx);
  59037. /* [... env] */
  59038. }
  59039. skip_varmap:
  59040. /* [... env] */
  59041. duk_del_prop_stridx(ctx, -1, DUK_STRIDX_INT_CALLEE);
  59042. duk_del_prop_stridx(ctx, -1, DUK_STRIDX_INT_THREAD);
  59043. duk_del_prop_stridx(ctx, -1, DUK_STRIDX_INT_REGBASE);
  59044. duk_pop(ctx);
  59045. DUK_HOBJECT_SET_ENVRECCLOSED(env);
  59046. DUK_DDD(DUK_DDDPRINT("environment record after being closed: %!O",
  59047. (duk_heaphdr *) env));
  59048. }
  59049. /*
  59050. * GETIDREF: a GetIdentifierReference-like helper.
  59051. *
  59052. * Provides a parent traversing lookup and a single level lookup
  59053. * (for HasBinding).
  59054. *
  59055. * Instead of returning the value, returns a bunch of values allowing
  59056. * the caller to read, write, or delete the binding. Value pointers
  59057. * are duk_tval pointers which can be mutated directly as long as
  59058. * refcounts are properly updated. Note that any operation which may
  59059. * reallocate valstacks or compact objects may invalidate the returned
  59060. * duk_tval (but not object) pointers, so caller must be very careful.
  59061. *
  59062. * If starting environment record 'env' is given, 'act' is ignored.
  59063. * However, if 'env' is NULL, the caller may identify, in 'act', an
  59064. * activation which hasn't had its declarative environment initialized
  59065. * yet. The activation registers are then looked up, and its parent
  59066. * traversed normally.
  59067. *
  59068. * The 'out' structure values are only valid if the function returns
  59069. * success (non-zero).
  59070. */
  59071. /* lookup name from an open declarative record's registers */
  59072. DUK_LOCAL
  59073. duk_bool_t duk__getid_open_decl_env_regs(duk_hthread *thr,
  59074. duk_hstring *name,
  59075. duk_hobject *env,
  59076. duk__id_lookup_result *out) {
  59077. duk_hthread *env_thr;
  59078. duk_hobject *env_func;
  59079. duk_size_t env_regbase;
  59080. duk_hobject *varmap;
  59081. duk_tval *tv;
  59082. duk_size_t reg_rel;
  59083. duk_size_t idx;
  59084. DUK_ASSERT(thr != NULL);
  59085. DUK_ASSERT(name != NULL);
  59086. DUK_ASSERT(env != NULL);
  59087. DUK_ASSERT(out != NULL);
  59088. DUK_ASSERT(DUK_HOBJECT_IS_DECENV(env));
  59089. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_CALLEE(thr));
  59090. if (!tv) {
  59091. /* env is closed, should be missing _Callee, _Thread, _Regbase */
  59092. DUK_ASSERT(duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_CALLEE(thr)) == NULL);
  59093. DUK_ASSERT(duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_THREAD(thr)) == NULL);
  59094. DUK_ASSERT(duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_REGBASE(thr)) == NULL);
  59095. return 0;
  59096. }
  59097. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  59098. DUK_ASSERT(DUK_TVAL_GET_OBJECT(tv) != NULL);
  59099. DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_TVAL_GET_OBJECT(tv)));
  59100. env_func = DUK_TVAL_GET_OBJECT(tv);
  59101. DUK_ASSERT(env_func != NULL);
  59102. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, env_func, DUK_HTHREAD_STRING_INT_VARMAP(thr));
  59103. if (!tv) {
  59104. return 0;
  59105. }
  59106. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  59107. varmap = DUK_TVAL_GET_OBJECT(tv);
  59108. DUK_ASSERT(varmap != NULL);
  59109. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, varmap, name);
  59110. if (!tv) {
  59111. return 0;
  59112. }
  59113. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  59114. reg_rel = (duk_size_t) DUK_TVAL_GET_NUMBER(tv);
  59115. DUK_ASSERT_DISABLE(reg_rel >= 0); /* unsigned */
  59116. DUK_ASSERT(reg_rel < ((duk_hcompiledfunction *) env_func)->nregs);
  59117. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_THREAD(thr));
  59118. DUK_ASSERT(tv != NULL);
  59119. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  59120. DUK_ASSERT(DUK_TVAL_GET_OBJECT(tv) != NULL);
  59121. DUK_ASSERT(DUK_HOBJECT_IS_THREAD(DUK_TVAL_GET_OBJECT(tv)));
  59122. env_thr = (duk_hthread *) DUK_TVAL_GET_OBJECT(tv);
  59123. DUK_ASSERT(env_thr != NULL);
  59124. /* Note: env_thr != thr is quite possible and normal, so careful
  59125. * with what thread is used for valstack lookup.
  59126. */
  59127. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_REGBASE(thr));
  59128. DUK_ASSERT(tv != NULL);
  59129. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  59130. env_regbase = (duk_size_t) DUK_TVAL_GET_NUMBER(tv);
  59131. idx = env_regbase + reg_rel;
  59132. tv = env_thr->valstack + idx;
  59133. DUK_ASSERT(tv >= env_thr->valstack && tv < env_thr->valstack_end); /* XXX: more accurate? */
  59134. out->value = tv;
  59135. out->attrs = DUK_PROPDESC_FLAGS_W; /* registers are mutable, non-deletable */
  59136. out->this_binding = NULL; /* implicit this value always undefined for
  59137. * declarative environment records.
  59138. */
  59139. out->env = env;
  59140. out->holder = NULL;
  59141. return 1;
  59142. }
  59143. /* lookup name from current activation record's functions' registers */
  59144. DUK_LOCAL
  59145. duk_bool_t duk__getid_activation_regs(duk_hthread *thr,
  59146. duk_hstring *name,
  59147. duk_activation *act,
  59148. duk__id_lookup_result *out) {
  59149. duk_tval *tv;
  59150. duk_hobject *func;
  59151. duk_hobject *varmap;
  59152. duk_size_t reg_rel;
  59153. duk_size_t idx;
  59154. DUK_ASSERT(thr != NULL);
  59155. DUK_ASSERT(name != NULL);
  59156. DUK_ASSERT(act != NULL);
  59157. DUK_ASSERT(out != NULL);
  59158. func = DUK_ACT_GET_FUNC(act);
  59159. DUK_ASSERT(func != NULL);
  59160. DUK_ASSERT(DUK_HOBJECT_HAS_NEWENV(func));
  59161. if (!DUK_HOBJECT_IS_COMPILEDFUNCTION(func)) {
  59162. return 0;
  59163. }
  59164. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_INT_VARMAP(thr));
  59165. if (!tv) {
  59166. return 0;
  59167. }
  59168. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  59169. varmap = DUK_TVAL_GET_OBJECT(tv);
  59170. DUK_ASSERT(varmap != NULL);
  59171. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, varmap, name);
  59172. if (!tv) {
  59173. return 0;
  59174. }
  59175. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  59176. reg_rel = (duk_size_t) DUK_TVAL_GET_NUMBER(tv);
  59177. DUK_ASSERT_DISABLE(reg_rel >= 0);
  59178. DUK_ASSERT(reg_rel < ((duk_hcompiledfunction *) func)->nregs);
  59179. idx = act->idx_bottom + reg_rel;
  59180. DUK_ASSERT(idx >= act->idx_bottom);
  59181. tv = thr->valstack + idx;
  59182. out->value = tv;
  59183. out->attrs = DUK_PROPDESC_FLAGS_W; /* registers are mutable, non-deletable */
  59184. out->this_binding = NULL; /* implicit this value always undefined for
  59185. * declarative environment records.
  59186. */
  59187. out->env = NULL;
  59188. out->holder = NULL;
  59189. return 1;
  59190. }
  59191. DUK_LOCAL
  59192. duk_bool_t duk__get_identifier_reference(duk_hthread *thr,
  59193. duk_hobject *env,
  59194. duk_hstring *name,
  59195. duk_activation *act,
  59196. duk_bool_t parents,
  59197. duk__id_lookup_result *out) {
  59198. duk_tval *tv;
  59199. duk_uint_t sanity;
  59200. DUK_ASSERT(thr != NULL);
  59201. DUK_ASSERT(env != NULL || act != NULL);
  59202. DUK_ASSERT(name != NULL);
  59203. DUK_ASSERT(out != NULL);
  59204. DUK_ASSERT(!env || DUK_HOBJECT_IS_ENV(env));
  59205. DUK_ASSERT(!env || !DUK_HOBJECT_HAS_ARRAY_PART(env));
  59206. /*
  59207. * Conceptually, we look for the identifier binding by starting from
  59208. * 'env' and following to chain of environment records (represented
  59209. * by the prototype chain).
  59210. *
  59211. * If 'env' is NULL, the current activation does not yet have an
  59212. * allocated declarative environment record; this should be treated
  59213. * exactly as if the environment record existed but had no bindings
  59214. * other than register bindings.
  59215. *
  59216. * Note: we assume that with the DUK_HOBJECT_FLAG_NEWENV cleared
  59217. * the environment will always be initialized immediately; hence
  59218. * a NULL 'env' should only happen with the flag set. This is the
  59219. * case for: (1) function calls, and (2) strict, direct eval calls.
  59220. */
  59221. if (env == NULL && act != NULL) {
  59222. duk_hobject *func;
  59223. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference: env is NULL, activation is non-NULL -> "
  59224. "delayed env case, look up activation regs first"));
  59225. /*
  59226. * Try registers
  59227. */
  59228. if (duk__getid_activation_regs(thr, name, act, out)) {
  59229. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference successful: "
  59230. "name=%!O -> value=%!T, attrs=%ld, this=%!T, env=%!O, holder=%!O "
  59231. "(found from register bindings when env=NULL)",
  59232. (duk_heaphdr *) name, (duk_tval *) out->value,
  59233. (long) out->attrs, (duk_tval *) out->this_binding,
  59234. (duk_heaphdr *) out->env, (duk_heaphdr *) out->holder));
  59235. return 1;
  59236. }
  59237. DUK_DDD(DUK_DDDPRINT("not found in current activation regs"));
  59238. /*
  59239. * Not found in registers, proceed to the parent record.
  59240. * Here we need to determine what the parent would be,
  59241. * if 'env' was not NULL (i.e. same logic as when initializing
  59242. * the record).
  59243. *
  59244. * Note that environment initialization is only deferred when
  59245. * DUK_HOBJECT_HAS_NEWENV is set, and this only happens for:
  59246. * - Function code
  59247. * - Strict eval code
  59248. *
  59249. * We only need to check _Lexenv here; _Varenv exists only if it
  59250. * differs from _Lexenv (and thus _Lexenv will also be present).
  59251. */
  59252. if (!parents) {
  59253. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference failed, no parent traversal "
  59254. "(not found from register bindings when env=NULL)"));
  59255. goto fail_not_found;
  59256. }
  59257. func = DUK_ACT_GET_FUNC(act);
  59258. DUK_ASSERT(func != NULL);
  59259. DUK_ASSERT(DUK_HOBJECT_HAS_NEWENV(func));
  59260. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_INT_LEXENV(thr));
  59261. if (tv) {
  59262. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  59263. env = DUK_TVAL_GET_OBJECT(tv);
  59264. } else {
  59265. DUK_ASSERT(duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_INT_VARENV(thr)) == NULL);
  59266. env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  59267. }
  59268. DUK_DDD(DUK_DDDPRINT("continue lookup from env: %!iO",
  59269. (duk_heaphdr *) env));
  59270. }
  59271. /*
  59272. * Prototype walking starting from 'env'.
  59273. *
  59274. * ('act' is not needed anywhere here.)
  59275. */
  59276. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  59277. while (env != NULL) {
  59278. duk_small_int_t cl;
  59279. duk_int_t attrs;
  59280. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference, name=%!O, considering env=%p -> %!iO",
  59281. (duk_heaphdr *) name,
  59282. (void *) env,
  59283. (duk_heaphdr *) env));
  59284. DUK_ASSERT(env != NULL);
  59285. DUK_ASSERT(DUK_HOBJECT_IS_ENV(env));
  59286. DUK_ASSERT(!DUK_HOBJECT_HAS_ARRAY_PART(env));
  59287. cl = DUK_HOBJECT_GET_CLASS_NUMBER(env);
  59288. DUK_ASSERT(cl == DUK_HOBJECT_CLASS_OBJENV || cl == DUK_HOBJECT_CLASS_DECENV);
  59289. if (cl == DUK_HOBJECT_CLASS_DECENV) {
  59290. /*
  59291. * Declarative environment record.
  59292. *
  59293. * Identifiers can never be stored in ancestors and are
  59294. * always plain values, so we can use an internal helper
  59295. * and access the value directly with an duk_tval ptr.
  59296. *
  59297. * A closed environment is only indicated by it missing
  59298. * the "book-keeping" properties required for accessing
  59299. * register-bound variables.
  59300. */
  59301. if (DUK_HOBJECT_HAS_ENVRECCLOSED(env)) {
  59302. /* already closed */
  59303. goto skip_regs;
  59304. }
  59305. if (duk__getid_open_decl_env_regs(thr, name, env, out)) {
  59306. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference successful: "
  59307. "name=%!O -> value=%!T, attrs=%ld, this=%!T, env=%!O, holder=%!O "
  59308. "(declarative environment record, scope open, found in regs)",
  59309. (duk_heaphdr *) name, (duk_tval *) out->value,
  59310. (long) out->attrs, (duk_tval *) out->this_binding,
  59311. (duk_heaphdr *) out->env, (duk_heaphdr *) out->holder));
  59312. return 1;
  59313. }
  59314. skip_regs:
  59315. tv = duk_hobject_find_existing_entry_tval_ptr_and_attrs(thr->heap, env, name, &attrs);
  59316. if (tv) {
  59317. out->value = tv;
  59318. out->attrs = attrs;
  59319. out->this_binding = NULL; /* implicit this value always undefined for
  59320. * declarative environment records.
  59321. */
  59322. out->env = env;
  59323. out->holder = env;
  59324. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference successful: "
  59325. "name=%!O -> value=%!T, attrs=%ld, this=%!T, env=%!O, holder=%!O "
  59326. "(declarative environment record, found in properties)",
  59327. (duk_heaphdr *) name, (duk_tval *) out->value,
  59328. (long) out->attrs, (duk_tval *) out->this_binding,
  59329. (duk_heaphdr *) out->env, (duk_heaphdr *) out->holder));
  59330. return 1;
  59331. }
  59332. } else {
  59333. /*
  59334. * Object environment record.
  59335. *
  59336. * Binding (target) object is an external, uncontrolled object.
  59337. * Identifier may be bound in an ancestor property, and may be
  59338. * an accessor.
  59339. */
  59340. /* XXX: we could save space by using _Target OR _This. If _Target, assume
  59341. * this binding is undefined. If _This, assumes this binding is _This, and
  59342. * target is also _This. One property would then be enough.
  59343. */
  59344. duk_hobject *target;
  59345. DUK_ASSERT(cl == DUK_HOBJECT_CLASS_OBJENV);
  59346. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_TARGET(thr));
  59347. DUK_ASSERT(tv != NULL);
  59348. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  59349. target = DUK_TVAL_GET_OBJECT(tv);
  59350. DUK_ASSERT(target != NULL);
  59351. /* Note: we must traverse the prototype chain, so use an actual
  59352. * hasprop call here. The property may also be an accessor, so
  59353. * we can't get an duk_tval pointer here.
  59354. *
  59355. * out->holder is NOT set to the actual duk_hobject where the
  59356. * property is found, but rather the target object.
  59357. */
  59358. if (duk_hobject_hasprop_raw(thr, target, name)) {
  59359. out->value = NULL; /* can't get value, may be accessor */
  59360. out->attrs = 0; /* irrelevant when out->value == NULL */
  59361. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_THIS(thr));
  59362. out->this_binding = tv; /* may be NULL */
  59363. out->env = env;
  59364. out->holder = target;
  59365. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference successful: "
  59366. "name=%!O -> value=%!T, attrs=%ld, this=%!T, env=%!O, holder=%!O "
  59367. "(object environment record)",
  59368. (duk_heaphdr *) name, (duk_tval *) out->value,
  59369. (long) out->attrs, (duk_tval *) out->this_binding,
  59370. (duk_heaphdr *) out->env, (duk_heaphdr *) out->holder));
  59371. return 1;
  59372. }
  59373. }
  59374. if (!parents) {
  59375. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference failed, no parent traversal "
  59376. "(not found from first traversed env)"));
  59377. goto fail_not_found;
  59378. }
  59379. if (sanity-- == 0) {
  59380. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  59381. }
  59382. env = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, env);
  59383. };
  59384. /*
  59385. * Not found (even in global object)
  59386. */
  59387. fail_not_found:
  59388. return 0;
  59389. }
  59390. /*
  59391. * HASVAR: check identifier binding from a given environment record
  59392. * without traversing its parents.
  59393. *
  59394. * This primitive is not exposed to user code as such, but is used
  59395. * internally for e.g. declaration binding instantiation.
  59396. *
  59397. * See E5 Sections:
  59398. * 10.2.1.1.1 HasBinding(N)
  59399. * 10.2.1.2.1 HasBinding(N)
  59400. *
  59401. * Note: strictness has no bearing on this check. Hence we don't take
  59402. * a 'strict' parameter.
  59403. */
  59404. #if 0 /*unused*/
  59405. DUK_INTERNAL
  59406. duk_bool_t duk_js_hasvar_envrec(duk_hthread *thr,
  59407. duk_hobject *env,
  59408. duk_hstring *name) {
  59409. duk__id_lookup_result ref;
  59410. duk_bool_t parents;
  59411. DUK_DDD(DUK_DDDPRINT("hasvar: thr=%p, env=%p, name=%!O "
  59412. "(env -> %!dO)",
  59413. (void *) thr, (void *) env, (duk_heaphdr *) name,
  59414. (duk_heaphdr *) env));
  59415. DUK_ASSERT(thr != NULL);
  59416. DUK_ASSERT(env != NULL);
  59417. DUK_ASSERT(name != NULL);
  59418. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(env);
  59419. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(name);
  59420. DUK_ASSERT(DUK_HOBJECT_IS_ENV(env));
  59421. DUK_ASSERT(!DUK_HOBJECT_HAS_ARRAY_PART(env));
  59422. /* lookup results is ignored */
  59423. parents = 0;
  59424. return duk__get_identifier_reference(thr, env, name, NULL, parents, &ref);
  59425. }
  59426. #endif
  59427. /*
  59428. * GETVAR
  59429. *
  59430. * See E5 Sections:
  59431. * 11.1.2 Identifier Reference
  59432. * 10.3.1 Identifier Resolution
  59433. * 11.13.1 Simple Assignment [example of where the Reference is GetValue'd]
  59434. * 8.7.1 GetValue (V)
  59435. * 8.12.1 [[GetOwnProperty]] (P)
  59436. * 8.12.2 [[GetProperty]] (P)
  59437. * 8.12.3 [[Get]] (P)
  59438. *
  59439. * If 'throw' is true, always leaves two values on top of stack: [val this].
  59440. *
  59441. * If 'throw' is false, returns 0 if identifier cannot be resolved, and the
  59442. * stack will be unaffected in this case. If identifier is resolved, returns
  59443. * 1 and leaves [val this] on top of stack.
  59444. *
  59445. * Note: the 'strict' flag of a reference returned by GetIdentifierReference
  59446. * is ignored by GetValue. Hence we don't take a 'strict' parameter.
  59447. *
  59448. * The 'throw' flag is needed for implementing 'typeof' for an unreferenced
  59449. * identifier. An unreference identifier in other contexts generates a
  59450. * ReferenceError.
  59451. */
  59452. DUK_LOCAL
  59453. duk_bool_t duk__getvar_helper(duk_hthread *thr,
  59454. duk_hobject *env,
  59455. duk_activation *act,
  59456. duk_hstring *name,
  59457. duk_bool_t throw_flag) {
  59458. duk_context *ctx = (duk_context *) thr;
  59459. duk__id_lookup_result ref;
  59460. duk_tval tv_tmp_obj;
  59461. duk_tval tv_tmp_key;
  59462. duk_bool_t parents;
  59463. DUK_DDD(DUK_DDDPRINT("getvar: thr=%p, env=%p, act=%p, name=%!O "
  59464. "(env -> %!dO)",
  59465. (void *) thr, (void *) env, (void *) act,
  59466. (duk_heaphdr *) name, (duk_heaphdr *) env));
  59467. DUK_ASSERT(thr != NULL);
  59468. DUK_ASSERT(name != NULL);
  59469. /* env and act may be NULL */
  59470. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(env);
  59471. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(name);
  59472. parents = 1; /* follow parent chain */
  59473. if (duk__get_identifier_reference(thr, env, name, act, parents, &ref)) {
  59474. if (ref.value) {
  59475. DUK_ASSERT(ref.this_binding == NULL); /* always for register bindings */
  59476. duk_push_tval(ctx, ref.value);
  59477. duk_push_undefined(ctx);
  59478. } else {
  59479. DUK_ASSERT(ref.holder != NULL);
  59480. /* Note: getprop may invoke any getter and invalidate any
  59481. * duk_tval pointers, so this must be done first.
  59482. */
  59483. if (ref.this_binding) {
  59484. duk_push_tval(ctx, ref.this_binding);
  59485. } else {
  59486. duk_push_undefined(ctx);
  59487. }
  59488. DUK_TVAL_SET_OBJECT(&tv_tmp_obj, ref.holder);
  59489. DUK_TVAL_SET_STRING(&tv_tmp_key, name);
  59490. (void) duk_hobject_getprop(thr, &tv_tmp_obj, &tv_tmp_key); /* [this value] */
  59491. /* ref.value, ref.this.binding invalidated here by getprop call */
  59492. duk_insert(ctx, -2); /* [this value] -> [value this] */
  59493. }
  59494. return 1;
  59495. } else {
  59496. if (throw_flag) {
  59497. DUK_ERROR(thr, DUK_ERR_REFERENCE_ERROR,
  59498. "identifier '%s' undefined",
  59499. (const char *) DUK_HSTRING_GET_DATA(name));
  59500. }
  59501. return 0;
  59502. }
  59503. }
  59504. DUK_INTERNAL
  59505. duk_bool_t duk_js_getvar_envrec(duk_hthread *thr,
  59506. duk_hobject *env,
  59507. duk_hstring *name,
  59508. duk_bool_t throw_flag) {
  59509. return duk__getvar_helper(thr, env, NULL, name, throw_flag);
  59510. }
  59511. DUK_INTERNAL
  59512. duk_bool_t duk_js_getvar_activation(duk_hthread *thr,
  59513. duk_activation *act,
  59514. duk_hstring *name,
  59515. duk_bool_t throw_flag) {
  59516. DUK_ASSERT(act != NULL);
  59517. return duk__getvar_helper(thr, act->lex_env, act, name, throw_flag);
  59518. }
  59519. /*
  59520. * PUTVAR
  59521. *
  59522. * See E5 Sections:
  59523. * 11.1.2 Identifier Reference
  59524. * 10.3.1 Identifier Resolution
  59525. * 11.13.1 Simple Assignment [example of where the Reference is PutValue'd]
  59526. * 8.7.2 PutValue (V,W) [see especially step 3.b, undefined -> automatic global in non-strict mode]
  59527. * 8.12.4 [[CanPut]] (P)
  59528. * 8.12.5 [[Put]] (P)
  59529. *
  59530. * Note: may invalidate any valstack (or object) duk_tval pointers because
  59531. * putting a value may reallocate any object or any valstack. Caller beware.
  59532. */
  59533. DUK_LOCAL
  59534. void duk__putvar_helper(duk_hthread *thr,
  59535. duk_hobject *env,
  59536. duk_activation *act,
  59537. duk_hstring *name,
  59538. duk_tval *val,
  59539. duk_bool_t strict) {
  59540. duk__id_lookup_result ref;
  59541. duk_tval tv_tmp_obj;
  59542. duk_tval tv_tmp_key;
  59543. duk_bool_t parents;
  59544. DUK_DDD(DUK_DDDPRINT("putvar: thr=%p, env=%p, act=%p, name=%!O, val=%p, strict=%ld "
  59545. "(env -> %!dO, val -> %!T)",
  59546. (void *) thr, (void *) env, (void *) act,
  59547. (duk_heaphdr *) name, (void *) val, (long) strict,
  59548. (duk_heaphdr *) env, (duk_tval *) val));
  59549. DUK_ASSERT(thr != NULL);
  59550. DUK_ASSERT(name != NULL);
  59551. DUK_ASSERT(val != NULL);
  59552. /* env and act may be NULL */
  59553. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(env);
  59554. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(name);
  59555. DUK_ASSERT_REFCOUNT_NONZERO_TVAL(val);
  59556. /*
  59557. * In strict mode E5 protects 'eval' and 'arguments' from being
  59558. * assigned to (or even declared anywhere). Attempt to do so
  59559. * should result in a compile time SyntaxError. See the internal
  59560. * design documentation for details.
  59561. *
  59562. * Thus, we should never come here, run-time, for strict code,
  59563. * and name 'eval' or 'arguments'.
  59564. */
  59565. DUK_ASSERT(!strict ||
  59566. (name != DUK_HTHREAD_STRING_EVAL(thr) &&
  59567. name != DUK_HTHREAD_STRING_LC_ARGUMENTS(thr)));
  59568. /*
  59569. * Lookup variable and update in-place if found.
  59570. */
  59571. parents = 1; /* follow parent chain */
  59572. if (duk__get_identifier_reference(thr, env, name, act, parents, &ref)) {
  59573. if (ref.value && (ref.attrs & DUK_PROPDESC_FLAG_WRITABLE)) {
  59574. /* Update duk_tval in-place if pointer provided and the
  59575. * property is writable. If the property is not writable
  59576. * (immutable binding), use duk_hobject_putprop() which
  59577. * will respect mutability.
  59578. */
  59579. duk_tval tv_tmp;
  59580. duk_tval *tv_val;
  59581. DUK_ASSERT(ref.this_binding == NULL); /* always for register bindings */
  59582. tv_val = ref.value;
  59583. DUK_ASSERT(tv_val != NULL);
  59584. DUK_TVAL_SET_TVAL(&tv_tmp, tv_val);
  59585. DUK_TVAL_SET_TVAL(tv_val, val);
  59586. DUK_TVAL_INCREF(thr, val);
  59587. DUK_TVAL_DECREF(thr, &tv_tmp); /* must be last */
  59588. /* ref.value and ref.this_binding invalidated here */
  59589. } else {
  59590. DUK_ASSERT(ref.holder != NULL);
  59591. DUK_TVAL_SET_OBJECT(&tv_tmp_obj, ref.holder);
  59592. DUK_TVAL_SET_STRING(&tv_tmp_key, name);
  59593. (void) duk_hobject_putprop(thr, &tv_tmp_obj, &tv_tmp_key, val, strict);
  59594. /* ref.value and ref.this_binding invalidated here */
  59595. }
  59596. return;
  59597. }
  59598. /*
  59599. * Not found: write to global object (non-strict) or ReferenceError
  59600. * (strict); see E5 Section 8.7.2, step 3.
  59601. */
  59602. if (strict) {
  59603. DUK_DDD(DUK_DDDPRINT("identifier binding not found, strict => reference error"));
  59604. DUK_ERROR(thr, DUK_ERR_REFERENCE_ERROR, "identifier not defined");
  59605. }
  59606. DUK_DDD(DUK_DDDPRINT("identifier binding not found, not strict => set to global"));
  59607. DUK_TVAL_SET_OBJECT(&tv_tmp_obj, thr->builtins[DUK_BIDX_GLOBAL]);
  59608. DUK_TVAL_SET_STRING(&tv_tmp_key, name);
  59609. (void) duk_hobject_putprop(thr, &tv_tmp_obj, &tv_tmp_key, val, 0); /* 0 = no throw */
  59610. /* NB: 'val' may be invalidated here because put_value may realloc valstack,
  59611. * caller beware.
  59612. */
  59613. }
  59614. DUK_INTERNAL
  59615. void duk_js_putvar_envrec(duk_hthread *thr,
  59616. duk_hobject *env,
  59617. duk_hstring *name,
  59618. duk_tval *val,
  59619. duk_bool_t strict) {
  59620. duk__putvar_helper(thr, env, NULL, name, val, strict);
  59621. }
  59622. DUK_INTERNAL
  59623. void duk_js_putvar_activation(duk_hthread *thr,
  59624. duk_activation *act,
  59625. duk_hstring *name,
  59626. duk_tval *val,
  59627. duk_bool_t strict) {
  59628. DUK_ASSERT(act != NULL);
  59629. duk__putvar_helper(thr, act->lex_env, act, name, val, strict);
  59630. }
  59631. /*
  59632. * DELVAR
  59633. *
  59634. * See E5 Sections:
  59635. * 11.4.1 The delete operator
  59636. * 10.2.1.1.5 DeleteBinding (N) [declarative environment record]
  59637. * 10.2.1.2.5 DeleteBinding (N) [object environment record]
  59638. *
  59639. * Variable bindings established inside eval() are deletable (configurable),
  59640. * other bindings are not, including variables declared in global level.
  59641. * Registers are always non-deletable, and the deletion of other bindings
  59642. * is controlled by the configurable flag.
  59643. *
  59644. * For strict mode code, the 'delete' operator should fail with a compile
  59645. * time SyntaxError if applied to identifiers. Hence, no strict mode
  59646. * run-time deletion of identifiers should ever happen. This function
  59647. * should never be called from strict mode code!
  59648. */
  59649. DUK_LOCAL
  59650. duk_bool_t duk__delvar_helper(duk_hthread *thr,
  59651. duk_hobject *env,
  59652. duk_activation *act,
  59653. duk_hstring *name) {
  59654. duk__id_lookup_result ref;
  59655. duk_bool_t parents;
  59656. DUK_DDD(DUK_DDDPRINT("delvar: thr=%p, env=%p, act=%p, name=%!O "
  59657. "(env -> %!dO)",
  59658. (void *) thr, (void *) env, (void *) act,
  59659. (duk_heaphdr *) name, (duk_heaphdr *) env));
  59660. DUK_ASSERT(thr != NULL);
  59661. DUK_ASSERT(name != NULL);
  59662. /* env and act may be NULL */
  59663. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(name);
  59664. parents = 1; /* follow parent chain */
  59665. if (duk__get_identifier_reference(thr, env, name, act, parents, &ref)) {
  59666. if (ref.value && !(ref.attrs & DUK_PROPDESC_FLAG_CONFIGURABLE)) {
  59667. /* Identifier found in registers (always non-deletable)
  59668. * or declarative environment record and non-configurable.
  59669. */
  59670. return 0;
  59671. }
  59672. DUK_ASSERT(ref.holder != NULL);
  59673. return duk_hobject_delprop_raw(thr, ref.holder, name, 0);
  59674. }
  59675. /*
  59676. * Not found (even in global object).
  59677. *
  59678. * In non-strict mode this is a silent SUCCESS (!), see E5 Section 11.4.1,
  59679. * step 3.b. In strict mode this case is a compile time SyntaxError so
  59680. * we should not come here.
  59681. */
  59682. DUK_DDD(DUK_DDDPRINT("identifier to be deleted not found: name=%!O "
  59683. "(treated as silent success)",
  59684. (duk_heaphdr *) name));
  59685. return 1;
  59686. }
  59687. #if 0 /*unused*/
  59688. DUK_INTERNAL
  59689. duk_bool_t duk_js_delvar_envrec(duk_hthread *thr,
  59690. duk_hobject *env,
  59691. duk_hstring *name) {
  59692. return duk__delvar_helper(thr, env, NULL, name);
  59693. }
  59694. #endif
  59695. DUK_INTERNAL
  59696. duk_bool_t duk_js_delvar_activation(duk_hthread *thr,
  59697. duk_activation *act,
  59698. duk_hstring *name) {
  59699. DUK_ASSERT(act != NULL);
  59700. return duk__delvar_helper(thr, act->lex_env, act, name);
  59701. }
  59702. /*
  59703. * DECLVAR
  59704. *
  59705. * See E5 Sections:
  59706. * 10.4.3 Entering Function Code
  59707. * 10.5 Declaration Binding Instantion
  59708. * 12.2 Variable Statement
  59709. * 11.1.2 Identifier Reference
  59710. * 10.3.1 Identifier Resolution
  59711. *
  59712. * Variable declaration behavior is mainly discussed in Section 10.5,
  59713. * and is not discussed in the execution semantics (Sections 11-13).
  59714. *
  59715. * Conceptually declarations happen when code (global, eval, function)
  59716. * is entered, before any user code is executed. In practice, register-
  59717. * bound identifiers are 'declared' automatically (by virtue of being
  59718. * allocated to registers with the initial value 'undefined'). Other
  59719. * identifiers are declared in the function prologue with this primitive.
  59720. *
  59721. * Since non-register bindings eventually back to an internal object's
  59722. * properties, the 'prop_flags' argument is used to specify binding
  59723. * type:
  59724. *
  59725. * - Immutable binding: set DUK_PROPDESC_FLAG_WRITABLE to false
  59726. * - Non-deletable binding: set DUK_PROPDESC_FLAG_CONFIGURABLE to false
  59727. * - The flag DUK_PROPDESC_FLAG_ENUMERABLE should be set, although it
  59728. * doesn't really matter for internal objects
  59729. *
  59730. * All bindings are non-deletable mutable bindings except:
  59731. *
  59732. * - Declarations in eval code (mutable, deletable)
  59733. * - 'arguments' binding in strict function code (immutable)
  59734. * - Function name binding of a function expression (immutable)
  59735. *
  59736. * Declarations may go to declarative environment records (always
  59737. * so for functions), but may also go to object environment records
  59738. * (e.g. global code). The global object environment has special
  59739. * behavior when re-declaring a function (but not a variable); see
  59740. * E5.1 specification, Section 10.5, step 5.e.
  59741. *
  59742. * Declarations always go to the 'top-most' environment record, i.e.
  59743. * we never check the record chain. It's not an error even if a
  59744. * property (even an immutable or non-deletable one) of the same name
  59745. * already exists.
  59746. *
  59747. * If a declared variable already exists, its value needs to be updated
  59748. * (if possible). Returns 1 if a PUTVAR needs to be done by the caller;
  59749. * otherwise returns 0.
  59750. */
  59751. DUK_LOCAL
  59752. duk_bool_t duk__declvar_helper(duk_hthread *thr,
  59753. duk_hobject *env,
  59754. duk_hstring *name,
  59755. duk_tval *val,
  59756. duk_small_int_t prop_flags,
  59757. duk_bool_t is_func_decl) {
  59758. duk_context *ctx = (duk_context *) thr;
  59759. duk_hobject *holder;
  59760. duk_bool_t parents;
  59761. duk__id_lookup_result ref;
  59762. duk_tval *tv;
  59763. DUK_DDD(DUK_DDDPRINT("declvar: thr=%p, env=%p, name=%!O, val=%!T, prop_flags=0x%08lx, is_func_decl=%ld "
  59764. "(env -> %!iO)",
  59765. (void *) thr, (void *) env, (duk_heaphdr *) name,
  59766. (duk_tval *) val, (unsigned long) prop_flags,
  59767. (unsigned int) is_func_decl, (duk_heaphdr *) env));
  59768. DUK_ASSERT(thr != NULL);
  59769. DUK_ASSERT(env != NULL);
  59770. DUK_ASSERT(name != NULL);
  59771. DUK_ASSERT(val != NULL);
  59772. /* Note: in strict mode the compiler should reject explicit
  59773. * declaration of 'eval' or 'arguments'. However, internal
  59774. * bytecode may declare 'arguments' in the function prologue.
  59775. * We don't bother checking (or asserting) for these now.
  59776. */
  59777. /* Note: val is a stable duk_tval pointer. The caller makes
  59778. * a value copy into its stack frame, so 'tv_val' is not subject
  59779. * to side effects here.
  59780. */
  59781. /*
  59782. * Check whether already declared.
  59783. *
  59784. * We need to check whether the binding exists in the environment
  59785. * without walking its parents. However, we still need to check
  59786. * register-bound identifiers and the prototype chain of an object
  59787. * environment target object.
  59788. */
  59789. parents = 0; /* just check 'env' */
  59790. if (duk__get_identifier_reference(thr, env, name, NULL, parents, &ref)) {
  59791. duk_int_t e_idx;
  59792. duk_int_t h_idx;
  59793. duk_small_int_t flags;
  59794. /*
  59795. * Variable already declared, ignore re-declaration.
  59796. * The only exception is the updated behavior of E5.1 for
  59797. * global function declarations, E5.1 Section 10.5, step 5.e.
  59798. * This behavior does not apply to global variable declarations.
  59799. */
  59800. if (!(is_func_decl && env == thr->builtins[DUK_BIDX_GLOBAL_ENV])) {
  59801. DUK_DDD(DUK_DDDPRINT("re-declare a binding, ignoring"));
  59802. return 1; /* 1 -> needs a PUTVAR */
  59803. }
  59804. /*
  59805. * Special behavior in E5.1.
  59806. *
  59807. * Note that even though parents == 0, the conflicting property
  59808. * may be an inherited property (currently our global object's
  59809. * prototype is Object.prototype). Step 5.e first operates on
  59810. * the existing property (which is potentially in an ancestor)
  59811. * and then defines a new property in the global object (and
  59812. * never modifies the ancestor).
  59813. *
  59814. * Also note that this logic would become even more complicated
  59815. * if the conflicting property might be a virtual one. Object
  59816. * prototype has no virtual properties, though.
  59817. *
  59818. * XXX: this is now very awkward, rework.
  59819. */
  59820. DUK_DDD(DUK_DDDPRINT("re-declare a function binding in global object, "
  59821. "updated E5.1 processing"));
  59822. DUK_ASSERT(ref.holder != NULL);
  59823. holder = ref.holder;
  59824. /* holder will be set to the target object, not the actual object
  59825. * where the property was found (see duk__get_identifier_reference()).
  59826. */
  59827. DUK_ASSERT(DUK_HOBJECT_GET_CLASS_NUMBER(holder) == DUK_HOBJECT_CLASS_GLOBAL);
  59828. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARRAY(holder)); /* global object doesn't have array part */
  59829. /* XXX: use a helper for prototype traversal; no loop check here */
  59830. /* must be found: was found earlier, and cannot be inherited */
  59831. for (;;) {
  59832. DUK_ASSERT(holder != NULL);
  59833. duk_hobject_find_existing_entry(thr->heap, holder, name, &e_idx, &h_idx);
  59834. if (e_idx >= 0) {
  59835. break;
  59836. }
  59837. /* SCANBUILD: NULL pointer dereference, doesn't actually trigger,
  59838. * asserted above.
  59839. */
  59840. holder = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, holder);
  59841. }
  59842. DUK_ASSERT(holder != NULL);
  59843. DUK_ASSERT(e_idx >= 0);
  59844. /* SCANBUILD: scan-build produces a NULL pointer dereference warning
  59845. * below; it never actually triggers because holder is actually never
  59846. * NULL.
  59847. */
  59848. /* ref.holder is global object, holder is the object with the
  59849. * conflicting property.
  59850. */
  59851. flags = DUK_HOBJECT_E_GET_FLAGS(thr->heap, holder, e_idx);
  59852. if (!(flags & DUK_PROPDESC_FLAG_CONFIGURABLE)) {
  59853. if (flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  59854. DUK_DDD(DUK_DDDPRINT("existing property is a non-configurable "
  59855. "accessor -> reject"));
  59856. goto fail_existing_attributes;
  59857. }
  59858. if (!((flags & DUK_PROPDESC_FLAG_WRITABLE) &&
  59859. (flags & DUK_PROPDESC_FLAG_ENUMERABLE))) {
  59860. DUK_DDD(DUK_DDDPRINT("existing property is a non-configurable "
  59861. "plain property which is not writable and "
  59862. "enumerable -> reject"));
  59863. goto fail_existing_attributes;
  59864. }
  59865. DUK_DDD(DUK_DDDPRINT("existing property is not configurable but "
  59866. "is plain, enumerable, and writable -> "
  59867. "allow redeclaration"));
  59868. }
  59869. if (holder == ref.holder) {
  59870. /* XXX: if duk_hobject_define_property_internal() was updated
  59871. * to handle a pre-existing accessor property, this would be
  59872. * a simple call (like for the ancestor case).
  59873. */
  59874. DUK_DDD(DUK_DDDPRINT("redefine, offending property in global object itself"));
  59875. if (flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  59876. duk_hobject *tmp;
  59877. tmp = DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, holder, e_idx);
  59878. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, holder, e_idx, NULL);
  59879. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  59880. DUK_UNREF(tmp);
  59881. tmp = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, holder, e_idx);
  59882. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, holder, e_idx, NULL);
  59883. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  59884. DUK_UNREF(tmp);
  59885. } else {
  59886. duk_tval tv_tmp;
  59887. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, holder, e_idx);
  59888. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  59889. DUK_TVAL_SET_UNDEFINED_UNUSED(tv);
  59890. DUK_TVAL_DECREF(thr, &tv_tmp);
  59891. }
  59892. /* Here val would be potentially invalid if we didn't make
  59893. * a value copy at the caller.
  59894. */
  59895. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, holder, e_idx);
  59896. DUK_TVAL_SET_TVAL(tv, val);
  59897. DUK_TVAL_INCREF(thr, tv);
  59898. DUK_HOBJECT_E_SET_FLAGS(thr->heap, holder, e_idx, prop_flags);
  59899. DUK_DDD(DUK_DDDPRINT("updated global binding, final result: "
  59900. "value -> %!T, prop_flags=0x%08lx",
  59901. (duk_tval *) DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, holder, e_idx),
  59902. (unsigned long) prop_flags));
  59903. } else {
  59904. DUK_DDD(DUK_DDDPRINT("redefine, offending property in ancestor"));
  59905. DUK_ASSERT(ref.holder == thr->builtins[DUK_BIDX_GLOBAL]);
  59906. duk_push_tval(ctx, val);
  59907. duk_hobject_define_property_internal(thr, ref.holder, name, prop_flags);
  59908. }
  59909. return 0;
  59910. }
  59911. /*
  59912. * Not found (in registers or record objects). Declare
  59913. * to current variable environment.
  59914. */
  59915. /*
  59916. * Get holder object
  59917. */
  59918. if (DUK_HOBJECT_IS_DECENV(env)) {
  59919. holder = env;
  59920. } else {
  59921. DUK_ASSERT(DUK_HOBJECT_IS_OBJENV(env));
  59922. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, env, DUK_HTHREAD_STRING_INT_TARGET(thr));
  59923. DUK_ASSERT(tv != NULL);
  59924. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  59925. holder = DUK_TVAL_GET_OBJECT(tv);
  59926. DUK_ASSERT(holder != NULL);
  59927. }
  59928. /*
  59929. * Define new property
  59930. *
  59931. * Note: this may fail if the holder is not extensible.
  59932. */
  59933. /* XXX: this is awkward as we use an internal method which doesn't handle
  59934. * extensibility etc correctly. Basically we'd want to do a [[DefineOwnProperty]]
  59935. * or Object.defineProperty() here.
  59936. */
  59937. if (!DUK_HOBJECT_HAS_EXTENSIBLE(holder)) {
  59938. goto fail_not_extensible;
  59939. }
  59940. duk_push_hobject(ctx, holder);
  59941. duk_push_hstring(ctx, name);
  59942. duk_push_tval(ctx, val);
  59943. duk_xdef_prop(ctx, -3, prop_flags); /* [holder name val] -> [holder] */
  59944. duk_pop(ctx);
  59945. return 0;
  59946. fail_existing_attributes:
  59947. fail_not_extensible:
  59948. DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "declaration failed");
  59949. return 0;
  59950. }
  59951. DUK_INTERNAL
  59952. duk_bool_t duk_js_declvar_activation(duk_hthread *thr,
  59953. duk_activation *act,
  59954. duk_hstring *name,
  59955. duk_tval *val,
  59956. duk_small_int_t prop_flags,
  59957. duk_bool_t is_func_decl) {
  59958. duk_hobject *env;
  59959. duk_tval tv_val_copy;
  59960. /*
  59961. * Make a value copy of the input val. This ensures that
  59962. * side effects cannot invalidate the pointer.
  59963. */
  59964. DUK_TVAL_SET_TVAL(&tv_val_copy, val);
  59965. val = &tv_val_copy;
  59966. /*
  59967. * Delayed env creation check
  59968. */
  59969. if (!act->var_env) {
  59970. DUK_ASSERT(act->lex_env == NULL);
  59971. duk_js_init_activation_environment_records_delayed(thr, act);
  59972. }
  59973. DUK_ASSERT(act->lex_env != NULL);
  59974. DUK_ASSERT(act->var_env != NULL);
  59975. env = act->var_env;
  59976. DUK_ASSERT(env != NULL);
  59977. DUK_ASSERT(DUK_HOBJECT_IS_ENV(env));
  59978. return duk__declvar_helper(thr, env, name, val, prop_flags, is_func_decl);
  59979. }
  59980. #line 1 "duk_lexer.c"
  59981. /*
  59982. * Lexer for source files, ToNumber() string conversions, RegExp expressions,
  59983. * and JSON.
  59984. *
  59985. * Provides a stream of Ecmascript tokens from an UTF-8/CESU-8 buffer. The
  59986. * caller can also rewind the token stream into a certain position which is
  59987. * needed by the compiler part for multi-pass scanning. Tokens are
  59988. * represented as duk_token structures, and contain line number information.
  59989. * Token types are identified with DUK_TOK_* defines.
  59990. *
  59991. * Characters are decoded into a fixed size lookup window consisting of
  59992. * decoded Unicode code points, with window positions past the end of the
  59993. * input filled with an invalid codepoint (-1). The tokenizer can thus
  59994. * perform multiple character lookups efficiently and with few sanity
  59995. * checks (such as access outside the end of the input), which keeps the
  59996. * tokenization code small at the cost of performance.
  59997. *
  59998. * Character data in tokens (such as identifier names and string literals)
  59999. * is encoded into CESU-8 format on-the-fly while parsing the token in
  60000. * question. The string data is made reachable to garbage collection by
  60001. * placing the token-related values in value stack entries allocated for
  60002. * this purpose by the caller. The characters exist in Unicode code point
  60003. * form only in the fixed size lookup window, which keeps character data
  60004. * expansion (of especially ASCII data) low.
  60005. *
  60006. * Token parsing supports the full range of Unicode characters as described
  60007. * in the E5 specification. Parsing has been optimized for ASCII characters
  60008. * because ordinary Ecmascript code consists almost entirely of ASCII
  60009. * characters. Matching of complex Unicode codepoint sets (such as in the
  60010. * IdentifierStart and IdentifierPart productions) is optimized for size,
  60011. * and is done using a linear scan of a bit-packed list of ranges. This is
  60012. * very slow, but should never be entered unless the source code actually
  60013. * contains Unicode characters.
  60014. *
  60015. * Ecmascript tokenization is partially context sensitive. First,
  60016. * additional future reserved words are recognized in strict mode (see E5
  60017. * Section 7.6.1.2). Second, a forward slash character ('/') can be
  60018. * recognized either as starting a RegExp literal or as a division operator,
  60019. * depending on context. The caller must provide necessary context flags
  60020. * when requesting a new token.
  60021. *
  60022. * Future work:
  60023. *
  60024. * * Make the input window a circular array to avoid copying. This would
  60025. * not necessarily complicate the tokenizer much, although it would make
  60026. * the window fetches more expensive (one AND).
  60027. *
  60028. * * Make line number tracking optional, as it consumes space. Also, is
  60029. * tracking end line really useful for tokens?
  60030. *
  60031. * * Add a feature flag for disabling UTF-8 decoding of input, as most
  60032. * source code is ASCII. Because of Unicode escapes written in ASCII,
  60033. * this does not allow Unicode support to be removed from e.g.
  60034. * duk_unicode_is_identifier_start() nor does it allow removal of CESU-8
  60035. * encoding of e.g. string literals.
  60036. *
  60037. * * Add a feature flag for disabling Unicode compliance of e.g. identifier
  60038. * names. This allows for a build more than a kilobyte smaller, because
  60039. * Unicode ranges needed by duk_unicode_is_identifier_start() and
  60040. * duk_unicode_is_identifier_part() can be dropped. String literals
  60041. * should still be allowed to contain escaped Unicode, so this still does
  60042. * not allow removal of CESU-8 encoding of e.g. string literals.
  60043. *
  60044. * * Character lookup tables for codepoints above BMP could be stripped.
  60045. *
  60046. * * Strictly speaking, E5 specification requires that source code consists
  60047. * of 16-bit code units, and if not, must be conceptually converted to
  60048. * that format first. The current lexer processes Unicode code points
  60049. * and allows characters outside the BMP. These should be converted to
  60050. * surrogate pairs while reading the source characters into the window,
  60051. * not after tokens have been formed (as is done now). However, the fix
  60052. * is not trivial because two characters are decoded from one codepoint.
  60053. *
  60054. * * Optimize for speed as well as size. Large if-else ladders are slow.
  60055. */
  60056. /* include removed: duk_internal.h */
  60057. /*
  60058. * Various defines and file specific helper macros
  60059. */
  60060. #define DUK__MAX_RE_DECESC_DIGITS 9
  60061. #define DUK__MAX_RE_QUANT_DIGITS 9 /* Does not allow e.g. 2**31-1, but one more would allow overflows of u32. */
  60062. #define DUK__LOOKUP(lex_ctx,index) ((lex_ctx)->window[(index)])
  60063. #define DUK__ADVANCE(lex_ctx,count) duk__advance_chars((lex_ctx), (count))
  60064. #define DUK__INITBUFFER(lex_ctx) duk__initbuffer((lex_ctx))
  60065. #define DUK__APPENDBUFFER(lex_ctx,x) duk__appendbuffer((lex_ctx), (duk_codepoint_t) (x))
  60066. /* whether to use macros or helper function depends on call count */
  60067. #define DUK__ISDIGIT(x) ((x) >= DUK_ASC_0 && (x) <= DUK_ASC_9)
  60068. #define DUK__ISHEXDIGIT(x) duk__is_hex_digit((x))
  60069. #define DUK__ISOCTDIGIT(x) ((x) >= DUK_ASC_0 && (x) <= DUK_ASC_7)
  60070. #define DUK__ISDIGIT03(x) ((x) >= DUK_ASC_0 && (x) <= DUK_ASC_3)
  60071. #define DUK__ISDIGIT47(x) ((x) >= DUK_ASC_4 && (x) <= DUK_ASC_7)
  60072. /* lookup shorthands (note: assume context variable is named 'lex_ctx') */
  60073. #define DUK__L0() DUK__LOOKUP(lex_ctx, 0)
  60074. #define DUK__L1() DUK__LOOKUP(lex_ctx, 1)
  60075. #define DUK__L2() DUK__LOOKUP(lex_ctx, 2)
  60076. #define DUK__L3() DUK__LOOKUP(lex_ctx, 3)
  60077. #define DUK__L4() DUK__LOOKUP(lex_ctx, 4)
  60078. #define DUK__L5() DUK__LOOKUP(lex_ctx, 5)
  60079. /* packed advance/token number macro used by multiple functions */
  60080. #define DUK__ADVTOK(adv,tok) (((adv) << 8) + (tok))
  60081. /*
  60082. * Read a character from the window leading edge and update the line counter.
  60083. *
  60084. * Decodes UTF-8/CESU-8 leniently with support for code points from U+0000 to
  60085. * U+10FFFF, causing an error if the input is unparseable. Leniency means:
  60086. *
  60087. * * Unicode code point validation is intentionally not performed,
  60088. * except to check that the codepoint does not exceed 0x10ffff.
  60089. *
  60090. * * In particular, surrogate pairs are allowed and not combined, which
  60091. * allows source files to represent all SourceCharacters with CESU-8.
  60092. * Broken surrogate pairs are allowed, as Ecmascript does not mandate
  60093. * their validation.
  60094. *
  60095. * * Allow non-shortest UTF-8 encodings.
  60096. *
  60097. * Leniency here causes few security concerns because all character data is
  60098. * decoded into Unicode codepoints before lexer processing, and is then
  60099. * re-encoded into CESU-8. The source can be parsed as strict UTF-8 with
  60100. * a compiler option. However, Ecmascript source characters include -all-
  60101. * 16-bit unsigned integer codepoints, so leniency seems to be appropriate.
  60102. *
  60103. * Note that codepoints above the BMP are not strictly SourceCharacters,
  60104. * but the lexer still accepts them as such. Before ending up in a string
  60105. * or an identifier name, codepoints above BMP are converted into surrogate
  60106. * pairs and then CESU-8 encoded, resulting in 16-bit Unicode data as
  60107. * expected by Ecmascript.
  60108. *
  60109. * An alternative approach to dealing with invalid or partial sequences
  60110. * would be to skip them and replace them with e.g. the Unicode replacement
  60111. * character U+FFFD. This has limited utility because a replacement character
  60112. * will most likely cause a parse error, unless it occurs inside a string.
  60113. * Further, Ecmascript source is typically pure ASCII.
  60114. *
  60115. * See:
  60116. *
  60117. * http://en.wikipedia.org/wiki/UTF-8
  60118. * http://en.wikipedia.org/wiki/CESU-8
  60119. * http://tools.ietf.org/html/rfc3629
  60120. * http://en.wikipedia.org/wiki/UTF-8#Invalid_byte_sequences
  60121. *
  60122. * Future work:
  60123. *
  60124. * * Reject other invalid Unicode sequences (see Wikipedia entry for examples)
  60125. * in strict UTF-8 mode.
  60126. *
  60127. * * Size optimize. An attempt to use a 16-byte lookup table for the first
  60128. * byte resulted in a code increase though.
  60129. *
  60130. * * Is checking against maximum 0x10ffff really useful? 4-byte encoding
  60131. * imposes a certain limit anyway.
  60132. */
  60133. DUK_LOCAL duk_codepoint_t duk__read_char(duk_lexer_ctx *lex_ctx) {
  60134. /* attempting to reduce size of 'len' and/or 'i' resulted in larger code */
  60135. duk_codepoint_t x;
  60136. duk_small_int_t len;
  60137. duk_small_int_t i;
  60138. const duk_uint8_t *p;
  60139. #ifdef DUK_USE_STRICT_UTF8_SOURCE
  60140. duk_codepoint_t mincp;
  60141. #endif
  60142. duk_size_t input_offset;
  60143. input_offset = lex_ctx->input_offset;
  60144. if (DUK_UNLIKELY(input_offset >= lex_ctx->input_length)) {
  60145. /* If input_offset were assigned a negative value, it would
  60146. * result in a large positive value. Most likely it would be
  60147. * larger than input_length and be caught here. In any case
  60148. * no memory unsafe behavior would happen.
  60149. */
  60150. return -1;
  60151. }
  60152. p = lex_ctx->input + input_offset;
  60153. x = (int) *p++;
  60154. if (x < 0x80L) {
  60155. /* 0xxx xxxx -> fast path */
  60156. len = 1;
  60157. goto fastpath;
  60158. } else if (x < 0xc0L) {
  60159. /* 10xx xxxx -> invalid */
  60160. goto error_encoding;
  60161. } else if (x < 0xe0L) {
  60162. /* 110x xxxx 10xx xxxx */
  60163. len = 2;
  60164. #ifdef DUK_USE_STRICT_UTF8_SOURCE
  60165. mincp = 0x80L;
  60166. #endif
  60167. x = x & 0x1fL;
  60168. } else if (x < 0xf0L) {
  60169. /* 1110 xxxx 10xx xxxx 10xx xxxx */
  60170. len = 3;
  60171. #ifdef DUK_USE_STRICT_UTF8_SOURCE
  60172. mincp = 0x800L;
  60173. #endif
  60174. x = x & 0x0fL;
  60175. } else if (x < 0xf8L) {
  60176. /* 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx */
  60177. len = 4;
  60178. #ifdef DUK_USE_STRICT_UTF8_SOURCE
  60179. mincp = 0x10000L;
  60180. #endif
  60181. x = x & 0x07;
  60182. } else {
  60183. /* no point in supporting encodings of 5 or more bytes */
  60184. goto error_encoding;
  60185. }
  60186. DUK_ASSERT(lex_ctx->input_length >= lex_ctx->input_offset);
  60187. if ((duk_size_t) len > (duk_size_t) (lex_ctx->input_length - lex_ctx->input_offset)) {
  60188. goto error_clipped;
  60189. }
  60190. for (i = 1; i < len; i++) {
  60191. duk_small_int_t y = *p++;
  60192. if ((y & 0xc0) != 0x80) {
  60193. /* check that byte has the form 10xx xxxx */
  60194. goto error_encoding;
  60195. }
  60196. x = x << 6;
  60197. x += y & 0x3f;
  60198. }
  60199. /* check final character validity */
  60200. if (x > 0x10ffffL) {
  60201. goto error_encoding;
  60202. }
  60203. #ifdef DUK_USE_STRICT_UTF8_SOURCE
  60204. if (x < mincp || (x >= 0xd800L && x <= 0xdfffL) || x == 0xfffeL) {
  60205. goto error_encoding;
  60206. }
  60207. #endif
  60208. /* fall through */
  60209. fastpath:
  60210. /* input offset tracking */
  60211. lex_ctx->input_offset += len;
  60212. /* line tracking */
  60213. if ((x == 0x000aL) ||
  60214. ((x == 0x000dL) && (lex_ctx->input_offset >= lex_ctx->input_length ||
  60215. lex_ctx->input[lex_ctx->input_offset] != 0x000aL)) ||
  60216. (x == 0x2028L) ||
  60217. (x == 0x2029L)) {
  60218. /* lookup for 0x000a above assumes shortest encoding now */
  60219. /* E5 Section 7.3, treat the following as newlines:
  60220. * LF
  60221. * CR [not followed by LF]
  60222. * LS
  60223. * PS
  60224. *
  60225. * For CR LF, CR is ignored if it is followed by LF, and the LF will bump
  60226. * the line number.
  60227. */
  60228. lex_ctx->input_line++;
  60229. }
  60230. return x;
  60231. error_clipped: /* clipped codepoint */
  60232. error_encoding: /* invalid codepoint encoding or codepoint */
  60233. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR, "char decode failed");
  60234. return 0;
  60235. }
  60236. /*
  60237. * Advance lookup window by N characters. Also used to fill the window
  60238. * after position is changed (call with count == DUK_LEXER_WINDOW_SIZE).
  60239. *
  60240. * XXX: A lot of copying now, perhaps change to circular array or at
  60241. * least use memcpy(). For memcpy(), putting all elements of the
  60242. * window (code point, offset, line) into a struct would allow one
  60243. * memcpy() to slide the window, instead of three separate copys.
  60244. */
  60245. DUK_LOCAL void duk__advance_chars(duk_lexer_ctx *lex_ctx, duk_small_int_t count) {
  60246. duk_small_int_t i, n;
  60247. DUK_ASSERT(count >= 0 && count <= DUK_LEXER_WINDOW_SIZE);
  60248. /* Without this check, gcc -O4 will complain the following for the
  60249. * first for-loop below:
  60250. *
  60251. * duk_lexer.c:301:19: error: array subscript is above array bounds [-Werror=array-bounds]
  60252. *
  60253. * Check for range explicitly; this also protects against legitimate
  60254. * internal errors and avoids memory unsafe behavior in such cases.
  60255. */
  60256. if (DUK_UNLIKELY(!(count >= 0 && count <= DUK_LEXER_WINDOW_SIZE))) {
  60257. DUK_D(DUK_DPRINT("invalid count: %ld, should not happen", (long) count));
  60258. DUK_ERROR(lex_ctx->thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR);
  60259. return; /* never here */
  60260. }
  60261. if (count == 0) {
  60262. /* allowing zero count makes some special caller flows easier */
  60263. return;
  60264. }
  60265. n = DUK_LEXER_WINDOW_SIZE - count;
  60266. for (i = 0; i < n; i++) {
  60267. lex_ctx->offsets[i] = lex_ctx->offsets[i + count];
  60268. lex_ctx->lines[i] = lex_ctx->lines[i + count];
  60269. lex_ctx->window[i] = lex_ctx->window[i + count];
  60270. }
  60271. for (; i < DUK_LEXER_WINDOW_SIZE; i++) {
  60272. lex_ctx->offsets[i] = lex_ctx->input_offset;
  60273. lex_ctx->lines[i] = lex_ctx->input_line;
  60274. lex_ctx->window[i] = duk__read_char(lex_ctx);
  60275. }
  60276. }
  60277. /*
  60278. * (Re)initialize the temporary byte buffer. May be called extra times
  60279. * with little impact.
  60280. */
  60281. DUK_LOCAL void duk__initbuffer(duk_lexer_ctx *lex_ctx) {
  60282. if (DUK_HBUFFER_DYNAMIC_GET_ALLOC_SIZE(lex_ctx->buf) < DUK_LEXER_TEMP_BUF_LIMIT) {
  60283. /* Resize (zero) without realloc. */
  60284. DUK_HBUFFER_DYNAMIC_SET_SIZE(lex_ctx->buf, 0);
  60285. } else {
  60286. duk_hbuffer_resize(lex_ctx->thr, lex_ctx->buf, 0, DUK_LEXER_TEMP_BUF_LIMIT);
  60287. }
  60288. }
  60289. /*
  60290. * Append a Unicode codepoint to the temporary byte buffer. Performs
  60291. * CESU-8 surrogate pair encoding for codepoints above the BMP.
  60292. * Existing surrogate pairs are allowed and also encoded into CESU-8.
  60293. */
  60294. DUK_LOCAL void duk__appendbuffer(duk_lexer_ctx *lex_ctx, duk_codepoint_t x) {
  60295. /*
  60296. * Since character data is only generated by decoding the source or by
  60297. * the compiler itself, we rely on the input codepoints being correct
  60298. * and avoid a check here.
  60299. *
  60300. * Character data can also come here through decoding of Unicode
  60301. * escapes ("\udead\ubeef") so all 16-but unsigned values can be
  60302. * present, even when the source file itself is strict UTF-8.
  60303. */
  60304. DUK_ASSERT(x >= 0 && x <= 0x10ffff);
  60305. duk_hbuffer_append_cesu8(lex_ctx->thr, lex_ctx->buf, (duk_ucodepoint_t) x);
  60306. }
  60307. /*
  60308. * Intern the temporary byte buffer into a valstack slot
  60309. * (in practice, slot1 or slot2).
  60310. */
  60311. DUK_LOCAL void duk__internbuffer(duk_lexer_ctx *lex_ctx, duk_idx_t valstack_idx) {
  60312. duk_context *ctx = (duk_context *) lex_ctx->thr;
  60313. DUK_ASSERT(valstack_idx == lex_ctx->slot1_idx || valstack_idx == lex_ctx->slot2_idx);
  60314. duk_dup(ctx, lex_ctx->buf_idx);
  60315. duk_to_string(ctx, -1);
  60316. duk_replace(ctx, valstack_idx);
  60317. }
  60318. /*
  60319. * Init lexer context
  60320. */
  60321. DUK_INTERNAL void duk_lexer_initctx(duk_lexer_ctx *lex_ctx) {
  60322. DUK_ASSERT(lex_ctx != NULL);
  60323. DUK_MEMZERO(lex_ctx, sizeof(*lex_ctx));
  60324. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  60325. lex_ctx->thr = NULL;
  60326. lex_ctx->input = NULL;
  60327. lex_ctx->buf = NULL;
  60328. #endif
  60329. }
  60330. /*
  60331. * Set lexer input position and reinitialize lookup window.
  60332. */
  60333. /* NB: duk_lexer_getpoint() is a macro only */
  60334. DUK_INTERNAL void duk_lexer_setpoint(duk_lexer_ctx *lex_ctx, duk_lexer_point *pt) {
  60335. DUK_ASSERT_DISABLE(pt->offset >= 0); /* unsigned */
  60336. DUK_ASSERT(pt->line >= 1);
  60337. lex_ctx->input_offset = pt->offset;
  60338. lex_ctx->input_line = pt->line;
  60339. duk__advance_chars(lex_ctx, DUK_LEXER_WINDOW_SIZE); /* fill window */
  60340. }
  60341. /*
  60342. * Lexing helpers
  60343. */
  60344. /* numeric value of a hex digit (also covers octal and decimal digits) */
  60345. DUK_LOCAL duk_codepoint_t duk__hexval(duk_lexer_ctx *lex_ctx, duk_codepoint_t x) {
  60346. duk_small_int_t t;
  60347. /* Here 'x' is a Unicode codepoint */
  60348. if (DUK_LIKELY(x >= 0 && x <= 0xff)) {
  60349. t = duk_hex_dectab[x];
  60350. if (DUK_LIKELY(t >= 0)) {
  60351. return t;
  60352. }
  60353. }
  60354. /* Throwing an error this deep makes the error rather vague, but
  60355. * saves hundreds of bytes of code.
  60356. */
  60357. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR, "decode error");
  60358. return 0;
  60359. }
  60360. /* having this as a separate function provided a size benefit */
  60361. DUK_LOCAL duk_bool_t duk__is_hex_digit(duk_codepoint_t x) {
  60362. if (DUK_LIKELY(x >= 0 && x <= 0xff)) {
  60363. return (duk_hex_dectab[x] >= 0);
  60364. }
  60365. return 0;
  60366. }
  60367. DUK_LOCAL duk_codepoint_t duk__decode_hexesc_from_window(duk_lexer_ctx *lex_ctx, duk_small_int_t lookup_offset) {
  60368. /* validation performed by duk__hexval */
  60369. return (duk__hexval(lex_ctx, lex_ctx->window[lookup_offset]) << 4) |
  60370. (duk__hexval(lex_ctx, lex_ctx->window[lookup_offset + 1]));
  60371. }
  60372. DUK_LOCAL duk_codepoint_t duk__decode_uniesc_from_window(duk_lexer_ctx *lex_ctx, duk_small_int_t lookup_offset) {
  60373. /* validation performed by duk__hexval */
  60374. return (duk__hexval(lex_ctx, lex_ctx->window[lookup_offset]) << 12) |
  60375. (duk__hexval(lex_ctx, lex_ctx->window[lookup_offset + 1]) << 8) |
  60376. (duk__hexval(lex_ctx, lex_ctx->window[lookup_offset + 2]) << 4) |
  60377. (duk__hexval(lex_ctx, lex_ctx->window[lookup_offset + 3]));
  60378. }
  60379. /*
  60380. * Eat input characters until first character of window is not
  60381. * a white space (may be -1 if EOF encountered).
  60382. */
  60383. DUK_LOCAL void duk__eat_whitespace(duk_lexer_ctx *lex_ctx) {
  60384. /* guaranteed to finish, as EOF (-1) is not a whitespace */
  60385. while (duk_unicode_is_whitespace(DUK__LOOKUP(lex_ctx, 0))) {
  60386. DUK__ADVANCE(lex_ctx, 1);
  60387. }
  60388. }
  60389. /*
  60390. * Parse Ecmascript source InputElementDiv or InputElementRegExp
  60391. * (E5 Section 7).
  60392. *
  60393. * Possible results are:
  60394. * (1) a token
  60395. * (2) a line terminator
  60396. * (3) a comment
  60397. * (4) EOF
  60398. *
  60399. * White space is automatically skipped from the current position (but
  60400. * not after the input element). If input has already ended, returns
  60401. * DUK_TOK_EOF indefinitely. If a parse error occurs, uses an DUK_ERROR()
  60402. * macro call (and hence a longjmp through current heap longjmp context).
  60403. *
  60404. * The input element being matched is determined by regexp_mode; if set,
  60405. * parses a InputElementRegExp, otherwise a InputElementDiv. The
  60406. * difference between these are handling of productions starting with a
  60407. * forward slash.
  60408. *
  60409. * If strict_mode is set, recognizes additional future reserved words
  60410. * specific to strict mode, and refuses to parse octal literals.
  60411. *
  60412. * The matching strategy below is to (currently) use a six character
  60413. * lookup window to quickly determine which production is the -longest-
  60414. * matching one, and then parse that. The top-level if-else clauses
  60415. * match the first character, and the code blocks for each clause
  60416. * handle -all- alternatives for that first character. Ecmascript
  60417. * specification uses the "longest match wins" semantics, so the order
  60418. * of the if-clauses matters.
  60419. *
  60420. * Misc notes:
  60421. *
  60422. * * Ecmascript numeric literals do not accept a sign character.
  60423. * Consequently e.g. "-1.0" is parsed as two tokens: a negative
  60424. * sign and a positive numeric literal. The compiler performs
  60425. * the negation during compilation, so this has no adverse impact.
  60426. *
  60427. * * There is no token for "undefined": it is just a value available
  60428. * from the global object (or simply established by doing a reference
  60429. * to an undefined value).
  60430. *
  60431. * * Some contexts want Identifier tokens, which are IdentifierNames
  60432. * excluding reserved words, while some contexts want IdentifierNames
  60433. * directly. In the latter case e.g. "while" is interpreted as an
  60434. * identifier name, not a DUK_TOK_WHILE token. The solution here is
  60435. * to provide both token types: DUK_TOK_WHILE goes to 't' while
  60436. * DUK_TOK_IDENTIFIER goes to 't_nores', and 'slot1' always contains
  60437. * the identifier / keyword name.
  60438. *
  60439. * * Directive prologue needs to identify string literals such as
  60440. * "use strict" and 'use strict', which are sensitive to line
  60441. * continuations and escape sequences. For instance, "use\u0020strict"
  60442. * is a valid directive but is distinct from "use strict". The solution
  60443. * here is to decode escapes while tokenizing, but to keep track of the
  60444. * number of escapes. Directive detection can then check that the
  60445. * number of escapes is zero.
  60446. *
  60447. * * Comments are expressed as DUK_TOK_COMMENT tokens, with the type
  60448. * (single- or multi-line) and contents of the comments lost.
  60449. * Furthermore, multi-line comments with one or more internal
  60450. * LineTerminator are treated as DUK_TOK_LINETERM to comply with
  60451. * automatic semicolon insertion and to avoid complicating the
  60452. * tokenization process. See E5 Section 7.4.
  60453. */
  60454. DUK_LOCAL
  60455. void duk__parse_input_element_raw(duk_lexer_ctx *lex_ctx,
  60456. duk_token *out_token,
  60457. duk_bool_t strict_mode,
  60458. duk_bool_t regexp_mode) {
  60459. duk_codepoint_t x, y; /* temporaries, must be signed and 32-bit to hold Unicode code points */
  60460. duk_small_uint_t advtok = 0; /* (advance << 8) + token_type, updated at function end,
  60461. * init is unnecessary but suppresses "may be used uninitialized" warnings.
  60462. */
  60463. if (++lex_ctx->token_count >= lex_ctx->token_limit) {
  60464. DUK_ERROR(lex_ctx->thr, DUK_ERR_RANGE_ERROR, "token limit");
  60465. return; /* unreachable */
  60466. }
  60467. duk__eat_whitespace(lex_ctx);
  60468. out_token->t = DUK_TOK_EOF;
  60469. out_token->t_nores = -1; /* marker: copy t if not changed */
  60470. out_token->num = DUK_DOUBLE_NAN;
  60471. out_token->str1 = NULL;
  60472. out_token->str2 = NULL;
  60473. out_token->num_escapes = 0;
  60474. out_token->start_line = lex_ctx->lines[0];
  60475. out_token->start_offset = lex_ctx->offsets[0];
  60476. /* out_token->lineterm set by caller */
  60477. duk_to_undefined((duk_context *) lex_ctx->thr, lex_ctx->slot1_idx);
  60478. duk_to_undefined((duk_context *) lex_ctx->thr, lex_ctx->slot2_idx);
  60479. /* 'advtok' indicates how much to advance and which token id to assign
  60480. * at the end. This shared functionality minimizes code size. All
  60481. * code paths are required to set 'advtok' to some value, so no default
  60482. * init value is used. Code paths calling DUK_ERROR() never return so
  60483. * they don't need to set advtok.
  60484. */
  60485. /*
  60486. * Matching order:
  60487. *
  60488. * Punctuator first chars, also covers comments, regexps
  60489. * LineTerminator
  60490. * Identifier or reserved word, also covers null/true/false literals
  60491. * NumericLiteral
  60492. * StringLiteral
  60493. * EOF
  60494. *
  60495. * The order does not matter as long as the longest match is
  60496. * always correctly identified. There are order dependencies
  60497. * in the clauses, so it's not trivial to convert to a switch.
  60498. *
  60499. * XXX: This is quite inefficient. Maybe change to a switch
  60500. * statement which handles all single character cases and then
  60501. * use a followup if-else chain? Switch matches need to use
  60502. * goto to bypass the if-else chain.
  60503. */
  60504. x = DUK__L0();
  60505. y = DUK__L1();
  60506. if (x == '/') {
  60507. if (y == '/') {
  60508. /*
  60509. * E5 Section 7.4, allow SourceCharacter (which is any 16-bit
  60510. * code point).
  60511. */
  60512. /* DUK__ADVANCE(lex_ctx, 2) would be correct here, but it unnecessary */
  60513. for (;;) {
  60514. x = DUK__L0();
  60515. if (x < 0 || duk_unicode_is_line_terminator(x)) {
  60516. break;
  60517. }
  60518. DUK__ADVANCE(lex_ctx, 1);
  60519. }
  60520. advtok = DUK__ADVTOK(0, DUK_TOK_COMMENT);
  60521. } else if (y == '*') {
  60522. /*
  60523. * E5 Section 7.4. If the multi-line comment contains a newline,
  60524. * it is treated like a single DUK_TOK_LINETERM to facilitate
  60525. * automatic semicolon insertion.
  60526. */
  60527. duk_bool_t last_asterisk = 0;
  60528. advtok = DUK__ADVTOK(0, DUK_TOK_COMMENT);
  60529. DUK__ADVANCE(lex_ctx, 2);
  60530. for (;;) {
  60531. x = DUK__L0();
  60532. if (x < 0) {
  60533. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  60534. "eof while parsing multiline comment");
  60535. }
  60536. DUK__ADVANCE(lex_ctx, 1);
  60537. if (last_asterisk && x == '/') {
  60538. break;
  60539. }
  60540. if (duk_unicode_is_line_terminator(x)) {
  60541. advtok = DUK__ADVTOK(0, DUK_TOK_LINETERM);
  60542. }
  60543. last_asterisk = (x == '*');
  60544. }
  60545. } else if (regexp_mode) {
  60546. #ifdef DUK_USE_REGEXP_SUPPORT
  60547. /*
  60548. * "/" followed by something in regexp mode. See E5 Section 7.8.5.
  60549. *
  60550. * RegExp parsing is a bit complex. First, the regexp body is delimited
  60551. * by forward slashes, but the body may also contain forward slashes as
  60552. * part of an escape sequence or inside a character class (delimited by
  60553. * square brackets). A mini state machine is used to implement these.
  60554. *
  60555. * Further, an early (parse time) error must be thrown if the regexp
  60556. * would cause a run-time error when used in the expression new RegExp(...).
  60557. * Parsing here simply extracts the (candidate) regexp, and also accepts
  60558. * invalid regular expressions (which are delimited properly). The caller
  60559. * (compiler) must perform final validation and regexp compilation.
  60560. *
  60561. * RegExp first char may not be '/' (single line comment) or '*' (multi-
  60562. * line comment). These have already been checked above, so there is no
  60563. * need below for special handling of the first regexp character as in
  60564. * the E5 productions.
  60565. *
  60566. * About unicode escapes within regexp literals:
  60567. *
  60568. * E5 Section 7.8.5 grammar does NOT accept \uHHHH escapes.
  60569. * However, Section 6 states that regexps accept the escapes,
  60570. * see paragraph starting with "In string literals...".
  60571. * The regexp grammar, which sees the decoded regexp literal
  60572. * (after lexical parsing) DOES have a \uHHHH unicode escape.
  60573. * So, for instance:
  60574. *
  60575. * /\u1234/
  60576. *
  60577. * should first be parsed by the lexical grammar as:
  60578. *
  60579. * '\' 'u' RegularExpressionBackslashSequence
  60580. * '1' RegularExpressionNonTerminator
  60581. * '2' RegularExpressionNonTerminator
  60582. * '3' RegularExpressionNonTerminator
  60583. * '4' RegularExpressionNonTerminator
  60584. *
  60585. * and the escape itself is then parsed by the regexp engine.
  60586. * This is the current implementation.
  60587. *
  60588. * Minor spec inconsistency:
  60589. *
  60590. * E5 Section 7.8.5 RegularExpressionBackslashSequence is:
  60591. *
  60592. * \ RegularExpressionNonTerminator
  60593. *
  60594. * while Section A.1 RegularExpressionBackslashSequence is:
  60595. *
  60596. * \ NonTerminator
  60597. *
  60598. * The latter is not normative and a typo.
  60599. *
  60600. */
  60601. /* first, parse regexp body roughly */
  60602. duk_small_int_t state = 0; /* 0=base, 1=esc, 2=class, 3=class+esc */
  60603. DUK__INITBUFFER(lex_ctx);
  60604. for (;;) {
  60605. DUK__ADVANCE(lex_ctx, 1); /* skip opening slash on first loop */
  60606. x = DUK__L0();
  60607. if (x < 0 || duk_unicode_is_line_terminator(x)) {
  60608. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  60609. "eof or line terminator while parsing regexp");
  60610. }
  60611. x = DUK__L0(); /* re-read to avoid spill / fetch */
  60612. if (state == 0) {
  60613. if (x == '/') {
  60614. DUK__ADVANCE(lex_ctx, 1); /* eat closing slash */
  60615. break;
  60616. } else if (x == '\\') {
  60617. state = 1;
  60618. } else if (x == '[') {
  60619. state = 2;
  60620. }
  60621. } else if (state == 1) {
  60622. state = 0;
  60623. } else if (state == 2) {
  60624. if (x == ']') {
  60625. state = 0;
  60626. } else if (x == '\\') {
  60627. state = 3;
  60628. }
  60629. } else { /* state == 3 */
  60630. state = 2;
  60631. }
  60632. DUK__APPENDBUFFER(lex_ctx, x);
  60633. }
  60634. duk__internbuffer(lex_ctx, lex_ctx->slot1_idx);
  60635. out_token->str1 = duk_get_hstring((duk_context *) lex_ctx->thr, lex_ctx->slot1_idx);
  60636. /* second, parse flags */
  60637. DUK__INITBUFFER(lex_ctx);
  60638. for (;;) {
  60639. x = DUK__L0();
  60640. if (!duk_unicode_is_identifier_part(x)) {
  60641. break;
  60642. }
  60643. x = DUK__L0(); /* re-read to avoid spill / fetch */
  60644. DUK__APPENDBUFFER(lex_ctx, x);
  60645. DUK__ADVANCE(lex_ctx, 1);
  60646. }
  60647. duk__internbuffer(lex_ctx, lex_ctx->slot2_idx);
  60648. out_token->str2 = duk_get_hstring((duk_context *) lex_ctx->thr, lex_ctx->slot2_idx);
  60649. DUK__INITBUFFER(lex_ctx); /* free some memory */
  60650. /* validation of the regexp is caller's responsibility */
  60651. advtok = DUK__ADVTOK(0, DUK_TOK_REGEXP);
  60652. #else
  60653. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR, "regexp support disabled");
  60654. #endif
  60655. } else if (y == '=') {
  60656. /* "/=" and not in regexp mode */
  60657. advtok = DUK__ADVTOK(2, DUK_TOK_DIV_EQ);
  60658. } else {
  60659. /* "/" and not in regexp mode */
  60660. advtok = DUK__ADVTOK(1, DUK_TOK_DIV);
  60661. }
  60662. } else if (x == '{') {
  60663. advtok = DUK__ADVTOK(1, DUK_TOK_LCURLY);
  60664. } else if (x == '}') {
  60665. advtok = DUK__ADVTOK(1, DUK_TOK_RCURLY);
  60666. } else if (x == '(') {
  60667. advtok = DUK__ADVTOK(1, DUK_TOK_LPAREN);
  60668. } else if (x == ')') {
  60669. advtok = DUK__ADVTOK(1, DUK_TOK_RPAREN);
  60670. } else if (x == '[') {
  60671. advtok = DUK__ADVTOK(1, DUK_TOK_LBRACKET);
  60672. } else if (x == ']') {
  60673. advtok = DUK__ADVTOK(1, DUK_TOK_RBRACKET);
  60674. } else if (x == '.' && !DUK__ISDIGIT(y)) {
  60675. /* Note: period followed by a digit can only start DecimalLiteral (captured below) */
  60676. advtok = DUK__ADVTOK(1, DUK_TOK_PERIOD);
  60677. } else if (x == ';') {
  60678. advtok = DUK__ADVTOK(1, DUK_TOK_SEMICOLON);
  60679. } else if (x == ',') {
  60680. advtok = DUK__ADVTOK(1, DUK_TOK_COMMA);
  60681. } else if (x == '<') {
  60682. if (y == '<' && DUK__L2() == '=') {
  60683. advtok = DUK__ADVTOK(3, DUK_TOK_ALSHIFT_EQ);
  60684. } else if (y == '=') {
  60685. advtok = DUK__ADVTOK(2, DUK_TOK_LE);
  60686. } else if (y == '<') {
  60687. advtok = DUK__ADVTOK(2, DUK_TOK_ALSHIFT);
  60688. } else {
  60689. advtok = DUK__ADVTOK(1, DUK_TOK_LT);
  60690. }
  60691. } else if (x == '>') {
  60692. if (y == '>' && DUK__L2() == '>' && DUK__L3() == '=') {
  60693. advtok = DUK__ADVTOK(4, DUK_TOK_RSHIFT_EQ);
  60694. } else if (y == '>' && DUK__L2() == '>') {
  60695. advtok = DUK__ADVTOK(3, DUK_TOK_RSHIFT);
  60696. } else if (y == '>' && DUK__L2() == '=') {
  60697. advtok = DUK__ADVTOK(3, DUK_TOK_ARSHIFT_EQ);
  60698. } else if (y == '=') {
  60699. advtok = DUK__ADVTOK(2, DUK_TOK_GE);
  60700. } else if (y == '>') {
  60701. advtok = DUK__ADVTOK(2, DUK_TOK_ARSHIFT);
  60702. } else {
  60703. advtok = DUK__ADVTOK(1, DUK_TOK_GT);
  60704. }
  60705. } else if (x == '=') {
  60706. if (y == '=' && DUK__L2() == '=') {
  60707. advtok = DUK__ADVTOK(3, DUK_TOK_SEQ);
  60708. } else if (y == '=') {
  60709. advtok = DUK__ADVTOK(2, DUK_TOK_EQ);
  60710. } else {
  60711. advtok = DUK__ADVTOK(1, DUK_TOK_EQUALSIGN);
  60712. }
  60713. } else if (x == '!') {
  60714. if (y == '=' && DUK__L2() == '=') {
  60715. advtok = DUK__ADVTOK(3, DUK_TOK_SNEQ);
  60716. } else if (y == '=') {
  60717. advtok = DUK__ADVTOK(2, DUK_TOK_NEQ);
  60718. } else {
  60719. advtok = DUK__ADVTOK(1, DUK_TOK_LNOT);
  60720. }
  60721. } else if (x == '+') {
  60722. if (y == '+') {
  60723. advtok = DUK__ADVTOK(2, DUK_TOK_INCREMENT);
  60724. } else if (y == '=') {
  60725. advtok = DUK__ADVTOK(2, DUK_TOK_ADD_EQ);
  60726. } else {
  60727. advtok = DUK__ADVTOK(1, DUK_TOK_ADD);
  60728. }
  60729. } else if (x == '-') {
  60730. if (y == '-') {
  60731. advtok = DUK__ADVTOK(2, DUK_TOK_DECREMENT);
  60732. } else if (y == '=') {
  60733. advtok = DUK__ADVTOK(2, DUK_TOK_SUB_EQ);
  60734. } else {
  60735. advtok = DUK__ADVTOK(1, DUK_TOK_SUB);
  60736. }
  60737. } else if (x == '*') {
  60738. if (y == '=') {
  60739. advtok = DUK__ADVTOK(2, DUK_TOK_MUL_EQ);
  60740. } else {
  60741. advtok = DUK__ADVTOK(1, DUK_TOK_MUL);
  60742. }
  60743. } else if (x == '%') {
  60744. if (y == '=') {
  60745. advtok = DUK__ADVTOK(2, DUK_TOK_MOD_EQ);
  60746. } else {
  60747. advtok = DUK__ADVTOK(1, DUK_TOK_MOD);
  60748. }
  60749. } else if (x == '&') {
  60750. if (y == '&') {
  60751. advtok = DUK__ADVTOK(2, DUK_TOK_LAND);
  60752. } else if (y == '=') {
  60753. advtok = DUK__ADVTOK(2, DUK_TOK_BAND_EQ);
  60754. } else {
  60755. advtok = DUK__ADVTOK(1, DUK_TOK_BAND);
  60756. }
  60757. } else if (x == '|') {
  60758. if (y == '|') {
  60759. advtok = DUK__ADVTOK(2, DUK_TOK_LOR);
  60760. } else if (y == '=') {
  60761. advtok = DUK__ADVTOK(2, DUK_TOK_BOR_EQ);
  60762. } else {
  60763. advtok = DUK__ADVTOK(1, DUK_TOK_BOR);
  60764. }
  60765. } else if (x == '^') {
  60766. if (y == '=') {
  60767. advtok = DUK__ADVTOK(2, DUK_TOK_BXOR_EQ);
  60768. } else {
  60769. advtok = DUK__ADVTOK(1, DUK_TOK_BXOR);
  60770. }
  60771. } else if (x == '~') {
  60772. advtok = DUK__ADVTOK(1, DUK_TOK_BNOT);
  60773. } else if (x == '?') {
  60774. advtok = DUK__ADVTOK(1, DUK_TOK_QUESTION);
  60775. } else if (x == ':') {
  60776. advtok = DUK__ADVTOK(1, DUK_TOK_COLON);
  60777. } else if (duk_unicode_is_line_terminator(x)) {
  60778. if (x == 0x000d && y == 0x000a) {
  60779. /*
  60780. * E5 Section 7.3: CR LF is detected as a single line terminator for
  60781. * line numbers. Here we also detect it as a single line terminator
  60782. * token.
  60783. */
  60784. advtok = DUK__ADVTOK(2, DUK_TOK_LINETERM);
  60785. } else {
  60786. advtok = DUK__ADVTOK(1, DUK_TOK_LINETERM);
  60787. }
  60788. } else if (duk_unicode_is_identifier_start(x) || x == '\\') {
  60789. /*
  60790. * Parse an identifier and then check whether it is:
  60791. * - reserved word (keyword or other reserved word)
  60792. * - "null" (NullLiteral)
  60793. * - "true" (BooleanLiteral)
  60794. * - "false" (BooleanLiteral)
  60795. * - anything else => identifier
  60796. *
  60797. * This does not follow the E5 productions cleanly, but is
  60798. * useful and compact.
  60799. *
  60800. * Note that identifiers may contain Unicode escapes,
  60801. * see E5 Sections 6 and 7.6. They must be decoded first,
  60802. * and the result checked against allowed characters.
  60803. * The above if-clause accepts an identifier start and an
  60804. * '\' character -- no other token can begin with a '\'.
  60805. *
  60806. * Note that "get" and "set" are not reserved words in E5
  60807. * specification so they are recognized as plain identifiers
  60808. * (the tokens DUK_TOK_GET and DUK_TOK_SET are actually not
  60809. * used now). The compiler needs to work around this.
  60810. *
  60811. * Strictly speaking, following Ecmascript longest match
  60812. * specification, an invalid escape for the first character
  60813. * should cause a syntax error. However, an invalid escape
  60814. * for IdentifierParts should just terminate the identifier
  60815. * early (longest match), and let the next tokenization
  60816. * fail. For instance Rhino croaks with 'foo\z' when
  60817. * parsing the identifier. This has little practical impact.
  60818. */
  60819. duk_small_int_t i, i_end;
  60820. duk_bool_t first = 1;
  60821. duk_hstring *str;
  60822. DUK__INITBUFFER(lex_ctx);
  60823. for (;;) {
  60824. /* re-lookup first char on first loop */
  60825. if (DUK__L0() == '\\') {
  60826. duk_codepoint_t ch;
  60827. if (DUK__L1() != 'u') {
  60828. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  60829. "invalid unicode escape while parsing identifier");
  60830. }
  60831. ch = duk__decode_uniesc_from_window(lex_ctx, 2);
  60832. /* IdentifierStart is stricter than IdentifierPart, so if the first
  60833. * character is escaped, must have a stricter check here.
  60834. */
  60835. if (!(first ? duk_unicode_is_identifier_start(ch) : duk_unicode_is_identifier_part(ch))) {
  60836. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  60837. "invalid unicode escaped character while parsing identifier");
  60838. }
  60839. DUK__APPENDBUFFER(lex_ctx, ch);
  60840. DUK__ADVANCE(lex_ctx, 6);
  60841. /* Track number of escapes: necessary for proper keyword
  60842. * detection.
  60843. */
  60844. out_token->num_escapes++;
  60845. } else {
  60846. /* Note: first character is checked against this. But because
  60847. * IdentifierPart includes all IdentifierStart characters, and
  60848. * the first character (if unescaped) has already been checked
  60849. * in the if condition, this is OK.
  60850. */
  60851. if (!duk_unicode_is_identifier_part(DUK__L0())) {
  60852. break;
  60853. }
  60854. DUK__APPENDBUFFER(lex_ctx, DUK__L0());
  60855. DUK__ADVANCE(lex_ctx, 1);
  60856. }
  60857. first = 0;
  60858. }
  60859. duk__internbuffer(lex_ctx, lex_ctx->slot1_idx);
  60860. out_token->str1 = duk_get_hstring((duk_context *) lex_ctx->thr, lex_ctx->slot1_idx);
  60861. str = out_token->str1;
  60862. DUK_ASSERT(str != NULL);
  60863. out_token->t_nores = DUK_TOK_IDENTIFIER;
  60864. DUK__INITBUFFER(lex_ctx); /* free some memory */
  60865. /*
  60866. * Interned identifier is compared against reserved words, which are
  60867. * currently interned into the heap context. See genstrings.py.
  60868. *
  60869. * Note that an escape in the identifier disables recognition of
  60870. * keywords; e.g. "\u0069f = 1;" is a valid statement (assigns to
  60871. * identifier named "if"). This is not necessarily compliant,
  60872. * see test-dec-escaped-char-in-keyword.js.
  60873. *
  60874. * Note: "get" and "set" are awkward. They are not officially
  60875. * ReservedWords (and indeed e.g. "var set = 1;" is valid), and
  60876. * must come out as DUK_TOK_IDENTIFIER. The compiler needs to
  60877. * work around this a bit.
  60878. */
  60879. /* XXX: optimize by adding the token numbers directly into the
  60880. * always interned duk_hstring objects (there should be enough
  60881. * flag bits free for that)?
  60882. */
  60883. i_end = (strict_mode ? DUK_STRIDX_END_RESERVED : DUK_STRIDX_START_STRICT_RESERVED);
  60884. advtok = DUK__ADVTOK(0, DUK_TOK_IDENTIFIER);
  60885. if (out_token->num_escapes == 0) {
  60886. for (i = DUK_STRIDX_START_RESERVED; i < i_end; i++) {
  60887. DUK_ASSERT(i >= 0 && i < DUK_HEAP_NUM_STRINGS);
  60888. if (DUK_HTHREAD_GET_STRING(lex_ctx->thr, i) == str) {
  60889. advtok = DUK__ADVTOK(0, DUK_STRIDX_TO_TOK(i));
  60890. break;
  60891. }
  60892. }
  60893. }
  60894. } else if (DUK__ISDIGIT(x) || (x == '.')) {
  60895. /* Note: decimal number may start with a period, but must be followed by a digit */
  60896. /*
  60897. * DecimalLiteral, HexIntegerLiteral, OctalIntegerLiteral
  60898. * "pre-parsing", followed by an actual, accurate parser step.
  60899. *
  60900. * Note: the leading sign character ('+' or '-') is -not- part of
  60901. * the production in E5 grammar, and that the a DecimalLiteral
  60902. * starting with a '0' must be followed by a non-digit. Leading
  60903. * zeroes are syntax errors and must be checked for.
  60904. *
  60905. * XXX: the two step parsing process is quite awkward, it would
  60906. * be more straightforward to allow numconv to parse the longest
  60907. * valid prefix (it already does that, it only needs to indicate
  60908. * where the input ended). However, the lexer decodes characters
  60909. * using a lookup window, so this is not a trivial change.
  60910. */
  60911. /* XXX: because of the final check below (that the literal is not
  60912. * followed by a digit), this could maybe be simplified, if we bail
  60913. * out early from a leading zero (and if there are no periods etc).
  60914. * Maybe too complex.
  60915. */
  60916. duk_double_t val;
  60917. duk_bool_t int_only = 0;
  60918. duk_bool_t allow_hex = 0;
  60919. duk_small_int_t state; /* 0=before period/exp,
  60920. * 1=after period, before exp
  60921. * 2=after exp, allow '+' or '-'
  60922. * 3=after exp and exp sign
  60923. */
  60924. duk_small_uint_t s2n_flags;
  60925. DUK__INITBUFFER(lex_ctx);
  60926. if (x == '0' && (y == 'x' || y == 'X')) {
  60927. DUK__APPENDBUFFER(lex_ctx, x);
  60928. DUK__APPENDBUFFER(lex_ctx, y);
  60929. DUK__ADVANCE(lex_ctx, 2);
  60930. int_only = 1;
  60931. allow_hex = 1;
  60932. #ifdef DUK_USE_OCTAL_SUPPORT
  60933. } else if (!strict_mode && x == '0' && DUK__ISDIGIT(y)) {
  60934. /* Note: if DecimalLiteral starts with a '0', it can only be
  60935. * followed by a period or an exponent indicator which starts
  60936. * with 'e' or 'E'. Hence the if-check above ensures that
  60937. * OctalIntegerLiteral is the only valid NumericLiteral
  60938. * alternative at this point (even if y is, say, '9').
  60939. */
  60940. DUK__APPENDBUFFER(lex_ctx, x);
  60941. DUK__ADVANCE(lex_ctx, 1);
  60942. int_only = 1;
  60943. #endif
  60944. }
  60945. state = 0;
  60946. for (;;) {
  60947. x = DUK__L0(); /* re-lookup curr char on first round */
  60948. if (DUK__ISDIGIT(x)) {
  60949. /* Note: intentionally allow leading zeroes here, as the
  60950. * actual parser will check for them.
  60951. */
  60952. if (state == 2) {
  60953. state = 3;
  60954. }
  60955. } else if (allow_hex && DUK__ISHEXDIGIT(x)) {
  60956. /* Note: 'e' and 'E' are also accepted here. */
  60957. ;
  60958. } else if (x == '.') {
  60959. if (state >= 1 || int_only) {
  60960. break;
  60961. } else {
  60962. state = 1;
  60963. }
  60964. } else if (x == 'e' || x == 'E') {
  60965. if (state >= 2 || int_only) {
  60966. break;
  60967. } else {
  60968. state = 2;
  60969. }
  60970. } else if (x == '-' || x == '+') {
  60971. if (state != 2) {
  60972. break;
  60973. } else {
  60974. state = 3;
  60975. }
  60976. } else {
  60977. break;
  60978. }
  60979. DUK__APPENDBUFFER(lex_ctx, x);
  60980. DUK__ADVANCE(lex_ctx, 1);
  60981. }
  60982. /* XXX: better coercion */
  60983. duk__internbuffer(lex_ctx, lex_ctx->slot1_idx);
  60984. s2n_flags = DUK_S2N_FLAG_ALLOW_EXP |
  60985. DUK_S2N_FLAG_ALLOW_FRAC |
  60986. DUK_S2N_FLAG_ALLOW_NAKED_FRAC |
  60987. DUK_S2N_FLAG_ALLOW_EMPTY_FRAC |
  60988. #ifdef DUK_USE_OCTAL_SUPPORT
  60989. (strict_mode ? 0 : DUK_S2N_FLAG_ALLOW_AUTO_OCT_INT) |
  60990. #endif
  60991. DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT;
  60992. duk_dup((duk_context *) lex_ctx->thr, lex_ctx->slot1_idx);
  60993. duk_numconv_parse((duk_context *) lex_ctx->thr, 10 /*radix*/, s2n_flags);
  60994. val = duk_to_number((duk_context *) lex_ctx->thr, -1);
  60995. if (DUK_ISNAN(val)) {
  60996. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR, "invalid numeric literal");
  60997. }
  60998. duk_replace((duk_context *) lex_ctx->thr, lex_ctx->slot1_idx); /* could also just pop? */
  60999. DUK__INITBUFFER(lex_ctx); /* free some memory */
  61000. /* Section 7.8.3 (note): NumericLiteral must be followed by something other than
  61001. * IdentifierStart or DecimalDigit.
  61002. */
  61003. if (DUK__ISDIGIT(DUK__L0()) || duk_unicode_is_identifier_start(DUK__L0())) {
  61004. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR, "invalid numeric literal");
  61005. }
  61006. out_token->num = val;
  61007. advtok = DUK__ADVTOK(0, DUK_TOK_NUMBER);
  61008. } else if (x == '"' || x == '\'') {
  61009. duk_small_int_t quote = x; /* Note: duk_uint8_t type yields larger code */
  61010. duk_small_int_t adv;
  61011. DUK__INITBUFFER(lex_ctx);
  61012. for (;;) {
  61013. DUK__ADVANCE(lex_ctx, 1); /* eat opening quote on first loop */
  61014. x = DUK__L0();
  61015. if (x < 0 || duk_unicode_is_line_terminator(x)) {
  61016. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61017. "eof or line terminator while parsing string literal");
  61018. }
  61019. if (x == quote) {
  61020. DUK__ADVANCE(lex_ctx, 1); /* eat closing quote */
  61021. break;
  61022. }
  61023. if (x == '\\') {
  61024. /* DUK__L0 -> '\' char
  61025. * DUK__L1 ... DUK__L5 -> more lookup
  61026. */
  61027. x = DUK__L1();
  61028. y = DUK__L2();
  61029. /* How much to advance before next loop; note that next loop
  61030. * will advance by 1 anyway, so -1 from the total escape
  61031. * length (e.g. len('\uXXXX') - 1 = 6 - 1). As a default,
  61032. * 1 is good.
  61033. */
  61034. adv = 2 - 1; /* note: long live range */
  61035. if (x < 0) {
  61036. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61037. "eof while parsing string literal");
  61038. }
  61039. if (duk_unicode_is_line_terminator(x)) {
  61040. /* line continuation */
  61041. if (x == 0x000d && y == 0x000a) {
  61042. /* CR LF again a special case */
  61043. adv = 3 - 1;
  61044. }
  61045. } else if (x == '\'') {
  61046. DUK__APPENDBUFFER(lex_ctx, 0x0027);
  61047. } else if (x == '"') {
  61048. DUK__APPENDBUFFER(lex_ctx, 0x0022);
  61049. } else if (x == '\\') {
  61050. DUK__APPENDBUFFER(lex_ctx, 0x005c);
  61051. } else if (x == 'b') {
  61052. DUK__APPENDBUFFER(lex_ctx, 0x0008);
  61053. } else if (x == 'f') {
  61054. DUK__APPENDBUFFER(lex_ctx, 0x000c);
  61055. } else if (x == 'n') {
  61056. DUK__APPENDBUFFER(lex_ctx, 0x000a);
  61057. } else if (x == 'r') {
  61058. DUK__APPENDBUFFER(lex_ctx, 0x000d);
  61059. } else if (x == 't') {
  61060. DUK__APPENDBUFFER(lex_ctx, 0x0009);
  61061. } else if (x == 'v') {
  61062. DUK__APPENDBUFFER(lex_ctx, 0x000b);
  61063. } else if (x == 'x') {
  61064. adv = 4 - 1;
  61065. DUK__APPENDBUFFER(lex_ctx, duk__decode_hexesc_from_window(lex_ctx, 2));
  61066. } else if (x == 'u') {
  61067. adv = 6 - 1;
  61068. DUK__APPENDBUFFER(lex_ctx, duk__decode_uniesc_from_window(lex_ctx, 2));
  61069. } else if (DUK__ISDIGIT(x)) {
  61070. duk_codepoint_t ch = 0; /* initialized to avoid warnings of unused var */
  61071. /*
  61072. * Octal escape or zero escape:
  61073. * \0 (lookahead not DecimalDigit)
  61074. * \1 ... \7 (lookahead not DecimalDigit)
  61075. * \ZeroToThree OctalDigit (lookahead not DecimalDigit)
  61076. * \FourToSeven OctalDigit (no lookahead restrictions)
  61077. * \ZeroToThree OctalDigit OctalDigit (no lookahead restrictions)
  61078. *
  61079. * Zero escape is part of the standard syntax. Octal escapes are
  61080. * defined in E5 Section B.1.2, and are only allowed in non-strict mode.
  61081. * Any other productions starting with a decimal digit are invalid.
  61082. */
  61083. if (x == '0' && !DUK__ISDIGIT(y)) {
  61084. /* Zero escape (also allowed in non-strict mode) */
  61085. ch = 0;
  61086. /* adv = 2 - 1 default OK */
  61087. #ifdef DUK_USE_OCTAL_SUPPORT
  61088. } else if (strict_mode) {
  61089. /* No other escape beginning with a digit in strict mode */
  61090. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61091. "invalid escape while parsing string literal");
  61092. } else if (DUK__ISDIGIT03(x) && DUK__ISOCTDIGIT(y) && DUK__ISOCTDIGIT(DUK__L3())) {
  61093. /* Three digit octal escape, digits validated. */
  61094. adv = 4 - 1;
  61095. ch = (duk__hexval(lex_ctx, x) << 6) +
  61096. (duk__hexval(lex_ctx, y) << 3) +
  61097. duk__hexval(lex_ctx, DUK__L3());
  61098. } else if (((DUK__ISDIGIT03(x) && !DUK__ISDIGIT(DUK__L3())) || DUK__ISDIGIT47(x)) &&
  61099. DUK__ISOCTDIGIT(y)) {
  61100. /* Two digit octal escape, digits validated.
  61101. *
  61102. * The if-condition is a bit tricky. We could catch e.g.
  61103. * '\039' in the three-digit escape and fail it there (by
  61104. * validating the digits), but we want to avoid extra
  61105. * additional validation code.
  61106. */
  61107. adv = 3 - 1;
  61108. ch = (duk__hexval(lex_ctx, x) << 3) +
  61109. duk__hexval(lex_ctx, y);
  61110. } else if (DUK__ISDIGIT(x) && !DUK__ISDIGIT(y)) {
  61111. /* One digit octal escape, digit validated. */
  61112. /* adv = 2 default OK */
  61113. ch = duk__hexval(lex_ctx, x);
  61114. #else
  61115. /* fall through to error */
  61116. #endif
  61117. } else {
  61118. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61119. "invalid escape while parsing string literal");
  61120. }
  61121. DUK__APPENDBUFFER(lex_ctx, ch);
  61122. } else {
  61123. /* escaped NonEscapeCharacter */
  61124. DUK__APPENDBUFFER(lex_ctx, x);
  61125. }
  61126. DUK__ADVANCE(lex_ctx, adv);
  61127. /* Track number of escapes; count not really needed but directive
  61128. * prologues need to detect whether there were any escapes or line
  61129. * continuations or not.
  61130. */
  61131. out_token->num_escapes++;
  61132. } else {
  61133. /* part of string */
  61134. DUK__APPENDBUFFER(lex_ctx, x);
  61135. }
  61136. }
  61137. duk__internbuffer(lex_ctx, lex_ctx->slot1_idx);
  61138. out_token->str1 = duk_get_hstring((duk_context *) lex_ctx->thr, lex_ctx->slot1_idx);
  61139. DUK__INITBUFFER(lex_ctx); /* free some memory */
  61140. advtok = DUK__ADVTOK(0, DUK_TOK_STRING);
  61141. } else if (x < 0) {
  61142. advtok = DUK__ADVTOK(0, DUK_TOK_EOF);
  61143. } else {
  61144. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR, "error parsing token");
  61145. }
  61146. /*
  61147. * Shared exit path
  61148. */
  61149. DUK__ADVANCE(lex_ctx, advtok >> 8);
  61150. out_token->t = advtok & 0xff;
  61151. if (out_token->t_nores < 0) {
  61152. out_token->t_nores = out_token->t;
  61153. }
  61154. }
  61155. /*
  61156. * Tokenize input until a non-whitespace, non-lineterm token is found.
  61157. * Note in the output token whether a lineterm token preceded the starting
  61158. * point (inclusive) and the result token. This information is needed for
  61159. * automatic semicolon insertion.
  61160. *
  61161. * Future work:
  61162. *
  61163. * * Merge with duk__parse_input_element_raw() because only this function is
  61164. * called in practice.
  61165. */
  61166. /* XXX: change mode flags into one flags argument? */
  61167. DUK_INTERNAL
  61168. void duk_lexer_parse_js_input_element(duk_lexer_ctx *lex_ctx,
  61169. duk_token *out_token,
  61170. duk_bool_t strict_mode,
  61171. duk_bool_t regexp_mode) {
  61172. duk_small_int_t tok;
  61173. duk_bool_t got_lineterm = 0; /* got lineterm preceding non-whitespace, non-lineterm token */
  61174. for (;;) {
  61175. duk__parse_input_element_raw(lex_ctx, out_token, strict_mode, regexp_mode);
  61176. tok = out_token->t;
  61177. DUK_DDD(DUK_DDDPRINT("RAWTOKEN: %ld (line %ld)",
  61178. (long) tok, (long) out_token->start_line));
  61179. if (tok == DUK_TOK_COMMENT) {
  61180. /* single-line comment or multi-line comment without an internal lineterm */
  61181. continue;
  61182. } else if (tok == DUK_TOK_LINETERM) {
  61183. /* lineterm or multi-line comment with an internal lineterm */
  61184. got_lineterm = 1;
  61185. continue;
  61186. } else {
  61187. break;
  61188. }
  61189. }
  61190. out_token->lineterm = got_lineterm;
  61191. /* Automatic semicolon insertion is allowed if a token is preceded
  61192. * by line terminator(s), or terminates a statement list (right curly
  61193. * or EOF).
  61194. */
  61195. if (got_lineterm || tok == DUK_TOK_RCURLY || tok == DUK_TOK_EOF) {
  61196. out_token->allow_auto_semi = 1;
  61197. } else {
  61198. out_token->allow_auto_semi = 0;
  61199. }
  61200. }
  61201. #ifdef DUK_USE_REGEXP_SUPPORT
  61202. /*
  61203. * Parse a RegExp token. The grammar is described in E5 Section 15.10.
  61204. * Terminal constructions (such as quantifiers) are parsed directly here.
  61205. *
  61206. * 0xffffffffU is used as a marker for "infinity" in quantifiers. Further,
  61207. * DUK__MAX_RE_QUANT_DIGITS limits the maximum number of digits that
  61208. * will be accepted for a quantifier.
  61209. */
  61210. DUK_INTERNAL void duk_lexer_parse_re_token(duk_lexer_ctx *lex_ctx, duk_re_token *out_token) {
  61211. duk_small_int_t advtok = 0; /* init is unnecessary but suppresses "may be used uninitialized" warnings */
  61212. duk_codepoint_t x, y;
  61213. if (++lex_ctx->token_count >= lex_ctx->token_limit) {
  61214. DUK_ERROR(lex_ctx->thr, DUK_ERR_RANGE_ERROR, "token limit");
  61215. return; /* unreachable */
  61216. }
  61217. DUK_MEMZERO(out_token, sizeof(*out_token));
  61218. x = DUK__L0();
  61219. y = DUK__L1();
  61220. DUK_DDD(DUK_DDDPRINT("parsing regexp token, L0=%ld, L1=%ld", (long) x, (long) y));
  61221. switch (x) {
  61222. case '|': {
  61223. advtok = DUK__ADVTOK(1, DUK_RETOK_DISJUNCTION);
  61224. break;
  61225. }
  61226. case '^': {
  61227. advtok = DUK__ADVTOK(1, DUK_RETOK_ASSERT_START);
  61228. break;
  61229. }
  61230. case '$': {
  61231. advtok = DUK__ADVTOK(1, DUK_RETOK_ASSERT_END);
  61232. break;
  61233. }
  61234. case '?': {
  61235. out_token->qmin = 0;
  61236. out_token->qmax = 1;
  61237. if (y == '?') {
  61238. advtok = DUK__ADVTOK(2, DUK_RETOK_QUANTIFIER);
  61239. out_token->greedy = 0;
  61240. } else {
  61241. advtok = DUK__ADVTOK(1, DUK_RETOK_QUANTIFIER);
  61242. out_token->greedy = 1;
  61243. }
  61244. break;
  61245. }
  61246. case '*': {
  61247. out_token->qmin = 0;
  61248. out_token->qmax = DUK_RE_QUANTIFIER_INFINITE;
  61249. if (y == '?') {
  61250. advtok = DUK__ADVTOK(2, DUK_RETOK_QUANTIFIER);
  61251. out_token->greedy = 0;
  61252. } else {
  61253. advtok = DUK__ADVTOK(1, DUK_RETOK_QUANTIFIER);
  61254. out_token->greedy = 1;
  61255. }
  61256. break;
  61257. }
  61258. case '+': {
  61259. out_token->qmin = 1;
  61260. out_token->qmax = DUK_RE_QUANTIFIER_INFINITE;
  61261. if (y == '?') {
  61262. advtok = DUK__ADVTOK(2, DUK_RETOK_QUANTIFIER);
  61263. out_token->greedy = 0;
  61264. } else {
  61265. advtok = DUK__ADVTOK(1, DUK_RETOK_QUANTIFIER);
  61266. out_token->greedy = 1;
  61267. }
  61268. break;
  61269. }
  61270. case '{': {
  61271. /* Production allows 'DecimalDigits', including leading zeroes */
  61272. duk_uint_fast32_t val1 = 0;
  61273. duk_uint_fast32_t val2 = DUK_RE_QUANTIFIER_INFINITE;
  61274. duk_small_int_t digits = 0;
  61275. for (;;) {
  61276. DUK__ADVANCE(lex_ctx, 1); /* eat '{' on entry */
  61277. x = DUK__L0();
  61278. if (DUK__ISDIGIT(x)) {
  61279. if (digits >= DUK__MAX_RE_QUANT_DIGITS) {
  61280. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61281. "invalid regexp quantifier (too many digits)");
  61282. }
  61283. digits++;
  61284. val1 = val1 * 10 + (duk_uint_fast32_t) duk__hexval(lex_ctx, x);
  61285. } else if (x == ',') {
  61286. if (val2 != DUK_RE_QUANTIFIER_INFINITE) {
  61287. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61288. "invalid regexp quantifier (double comma)");
  61289. }
  61290. if (DUK__L1() == '}') {
  61291. /* form: { DecimalDigits , }, val1 = min count */
  61292. if (digits == 0) {
  61293. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61294. "invalid regexp quantifier (missing digits)");
  61295. }
  61296. out_token->qmin = val1;
  61297. out_token->qmax = DUK_RE_QUANTIFIER_INFINITE;
  61298. DUK__ADVANCE(lex_ctx, 2);
  61299. break;
  61300. }
  61301. val2 = val1;
  61302. val1 = 0;
  61303. digits = 0; /* not strictly necessary because of lookahead '}' above */
  61304. } else if (x == '}') {
  61305. if (digits == 0) {
  61306. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61307. "invalid regexp quantifier (missing digits)");
  61308. }
  61309. if (val2 != DUK_RE_QUANTIFIER_INFINITE) {
  61310. /* val2 = min count, val1 = max count */
  61311. out_token->qmin = val2;
  61312. out_token->qmax = val1;
  61313. } else {
  61314. /* val1 = count */
  61315. out_token->qmin = val1;
  61316. out_token->qmax = val1;
  61317. }
  61318. DUK__ADVANCE(lex_ctx, 1);
  61319. break;
  61320. } else {
  61321. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61322. "invalid regexp quantifier (unknown char)");
  61323. }
  61324. }
  61325. if (DUK__L0() == '?') {
  61326. out_token->greedy = 0;
  61327. DUK__ADVANCE(lex_ctx, 1);
  61328. } else {
  61329. out_token->greedy = 1;
  61330. }
  61331. advtok = DUK__ADVTOK(0, DUK_RETOK_QUANTIFIER);
  61332. break;
  61333. }
  61334. case '.': {
  61335. advtok = DUK__ADVTOK(1, DUK_RETOK_ATOM_PERIOD);
  61336. break;
  61337. }
  61338. case '\\': {
  61339. /* The E5.1 specification does not seem to allow IdentifierPart characters
  61340. * to be used as identity escapes. Unfortunately this includes '$', which
  61341. * cannot be escaped as '\$'; it needs to be escaped e.g. as '\u0024'.
  61342. * Many other implementations (including V8 and Rhino, for instance) do
  61343. * accept '\$' as a valid identity escape, which is quite pragmatic.
  61344. * See: test-regexp-identity-escape-dollar.js.
  61345. */
  61346. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_CHAR); /* default: char escape (two chars) */
  61347. if (y == 'b') {
  61348. advtok = DUK__ADVTOK(2, DUK_RETOK_ASSERT_WORD_BOUNDARY);
  61349. } else if (y == 'B') {
  61350. advtok = DUK__ADVTOK(2, DUK_RETOK_ASSERT_NOT_WORD_BOUNDARY);
  61351. } else if (y == 'f') {
  61352. out_token->num = 0x000c;
  61353. } else if (y == 'n') {
  61354. out_token->num = 0x000a;
  61355. } else if (y == 't') {
  61356. out_token->num = 0x0009;
  61357. } else if (y == 'r') {
  61358. out_token->num = 0x000d;
  61359. } else if (y == 'v') {
  61360. out_token->num = 0x000b;
  61361. } else if (y == 'c') {
  61362. x = DUK__L2();
  61363. if ((x >= 'a' && x <= 'z') ||
  61364. (x >= 'A' && x <= 'Z')) {
  61365. out_token->num = (x % 32);
  61366. advtok = DUK__ADVTOK(3, DUK_RETOK_ATOM_CHAR);
  61367. } else {
  61368. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61369. "invalid regexp control escape");
  61370. }
  61371. } else if (y == 'x') {
  61372. out_token->num = duk__decode_hexesc_from_window(lex_ctx, 2);
  61373. advtok = DUK__ADVTOK(4, DUK_RETOK_ATOM_CHAR);
  61374. } else if (y == 'u') {
  61375. out_token->num = duk__decode_uniesc_from_window(lex_ctx, 2);
  61376. advtok = DUK__ADVTOK(6, DUK_RETOK_ATOM_CHAR);
  61377. } else if (y == 'd') {
  61378. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_DIGIT);
  61379. } else if (y == 'D') {
  61380. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_NOT_DIGIT);
  61381. } else if (y == 's') {
  61382. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_WHITE);
  61383. } else if (y == 'S') {
  61384. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_NOT_WHITE);
  61385. } else if (y == 'w') {
  61386. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_WORD_CHAR);
  61387. } else if (y == 'W') {
  61388. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_NOT_WORD_CHAR);
  61389. } else if (DUK__ISDIGIT(y)) {
  61390. /* E5 Section 15.10.2.11 */
  61391. if (y == '0') {
  61392. if (DUK__ISDIGIT(DUK__L2())) {
  61393. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61394. "invalid regexp escape");
  61395. }
  61396. out_token->num = 0x0000;
  61397. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_CHAR);
  61398. } else {
  61399. /* XXX: shared parsing? */
  61400. duk_uint_fast32_t val = 0;
  61401. duk_small_int_t i;
  61402. for (i = 0; ; i++) {
  61403. if (i >= DUK__MAX_RE_DECESC_DIGITS) {
  61404. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61405. "invalid regexp escape (decimal escape too long)");
  61406. }
  61407. DUK__ADVANCE(lex_ctx, 1); /* eat backslash on entry */
  61408. x = DUK__L0();
  61409. if (!DUK__ISDIGIT(x)) {
  61410. break;
  61411. }
  61412. val = val * 10 + (duk_uint_fast32_t) duk__hexval(lex_ctx, x);
  61413. }
  61414. /* DUK__L0() cannot be a digit, because the loop doesn't terminate if it is */
  61415. advtok = DUK__ADVTOK(0, DUK_RETOK_ATOM_BACKREFERENCE);
  61416. out_token->num = val;
  61417. }
  61418. } else if ((y >= 0 && !duk_unicode_is_identifier_part(y)) ||
  61419. #if defined(DUK_USE_NONSTD_REGEXP_DOLLAR_ESCAPE)
  61420. y == '$' ||
  61421. #endif
  61422. y == DUK_UNICODE_CP_ZWNJ ||
  61423. y == DUK_UNICODE_CP_ZWJ) {
  61424. /* IdentityEscape, with dollar added as a valid additional
  61425. * non-standard escape (see test-regexp-identity-escape-dollar.js).
  61426. * Careful not to match end-of-buffer (<0) here.
  61427. */
  61428. out_token->num = y;
  61429. } else {
  61430. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61431. "invalid regexp escape");
  61432. }
  61433. break;
  61434. }
  61435. case '(': {
  61436. /* XXX: naming is inconsistent: ATOM_END_GROUP ends an ASSERT_START_LOOKAHEAD */
  61437. if (y == '?') {
  61438. if (DUK__L2() == '=') {
  61439. /* (?= */
  61440. advtok = DUK__ADVTOK(3, DUK_RETOK_ASSERT_START_POS_LOOKAHEAD);
  61441. } else if (DUK__L2() == '!') {
  61442. /* (?! */
  61443. advtok = DUK__ADVTOK(3, DUK_RETOK_ASSERT_START_NEG_LOOKAHEAD);
  61444. } else if (DUK__L2() == ':') {
  61445. /* (?: */
  61446. advtok = DUK__ADVTOK(3, DUK_RETOK_ATOM_START_NONCAPTURE_GROUP);
  61447. }
  61448. } else {
  61449. /* ( */
  61450. advtok = DUK__ADVTOK(1, DUK_RETOK_ATOM_START_CAPTURE_GROUP);
  61451. }
  61452. break;
  61453. }
  61454. case ')': {
  61455. advtok = DUK__ADVTOK(1, DUK_RETOK_ATOM_END_GROUP);
  61456. break;
  61457. }
  61458. case '[': {
  61459. /*
  61460. * To avoid creating a heavy intermediate value for the list of ranges,
  61461. * only the start token ('[' or '[^') is parsed here. The regexp
  61462. * compiler parses the ranges itself.
  61463. */
  61464. advtok = DUK__ADVTOK(1, DUK_RETOK_ATOM_START_CHARCLASS);
  61465. if (y == '^') {
  61466. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_START_CHARCLASS_INVERTED);
  61467. }
  61468. break;
  61469. }
  61470. case ']':
  61471. case '}': {
  61472. /* Although these could be parsed as PatternCharacters unambiguously (here),
  61473. * E5 Section 15.10.1 grammar explicitly forbids these as PatternCharacters.
  61474. */
  61475. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61476. "invalid regexp character");
  61477. break;
  61478. }
  61479. case -1: {
  61480. /* EOF */
  61481. advtok = DUK__ADVTOK(0, DUK_TOK_EOF);
  61482. break;
  61483. }
  61484. default: {
  61485. /* PatternCharacter, all excluded characters are matched by cases above */
  61486. advtok = DUK__ADVTOK(1, DUK_RETOK_ATOM_CHAR);
  61487. out_token->num = x;
  61488. break;
  61489. }
  61490. }
  61491. /*
  61492. * Shared exit path
  61493. */
  61494. DUK__ADVANCE(lex_ctx, advtok >> 8);
  61495. out_token->t = advtok & 0xff;
  61496. }
  61497. /*
  61498. * Special parser for character classes; calls callback for every
  61499. * range parsed and returns the number of ranges present.
  61500. */
  61501. /* XXX: this duplicates functionality in duk_regexp.c where a similar loop is
  61502. * required anyway. We could use that BUT we need to update the regexp compiler
  61503. * 'nranges' too. Work this out a bit more cleanly to save space.
  61504. */
  61505. /* XXX: the handling of character range detection is a bit convoluted.
  61506. * Try to simplify and make smaller.
  61507. */
  61508. /* XXX: logic for handling character ranges is now incorrect, it will accept
  61509. * e.g. [\d-z] whereas it should croak from it? SMJS accepts this too, though.
  61510. *
  61511. * Needs a read through and a lot of additional tests.
  61512. */
  61513. DUK_LOCAL
  61514. void duk__emit_u16_direct_ranges(duk_lexer_ctx *lex_ctx,
  61515. duk_re_range_callback gen_range,
  61516. void *userdata,
  61517. duk_uint16_t *ranges,
  61518. duk_small_int_t num) {
  61519. duk_uint16_t *ranges_end;
  61520. DUK_UNREF(lex_ctx);
  61521. ranges_end = ranges + num;
  61522. while (ranges < ranges_end) {
  61523. /* mark range 'direct', bypass canonicalization (see Wiki) */
  61524. gen_range(userdata, (duk_codepoint_t) ranges[0], (duk_codepoint_t) ranges[1], 1);
  61525. ranges += 2;
  61526. }
  61527. }
  61528. DUK_INTERNAL void duk_lexer_parse_re_ranges(duk_lexer_ctx *lex_ctx, duk_re_range_callback gen_range, void *userdata) {
  61529. duk_codepoint_t start = -1;
  61530. duk_codepoint_t ch;
  61531. duk_codepoint_t x;
  61532. duk_bool_t dash = 0;
  61533. DUK_DD(DUK_DDPRINT("parsing regexp ranges"));
  61534. for (;;) {
  61535. x = DUK__L0();
  61536. DUK__ADVANCE(lex_ctx, 1);
  61537. ch = -1; /* not strictly necessary, but avoids "uninitialized variable" warnings */
  61538. DUK_UNREF(ch);
  61539. if (x < 0) {
  61540. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61541. "eof while parsing character class");
  61542. } else if (x == ']') {
  61543. DUK_ASSERT(!dash); /* lookup should prevent this */
  61544. if (start >= 0) {
  61545. gen_range(userdata, start, start, 0);
  61546. }
  61547. break;
  61548. } else if (x == '-') {
  61549. if (start >= 0 && !dash && DUK__L0() != ']') {
  61550. /* '-' as a range indicator */
  61551. dash = 1;
  61552. continue;
  61553. } else {
  61554. /* '-' verbatim */
  61555. ch = x;
  61556. }
  61557. } else if (x == '\\') {
  61558. /*
  61559. * The escapes are same as outside a character class, except that \b has a
  61560. * different meaning, and \B and backreferences are prohibited (see E5
  61561. * Section 15.10.2.19). However, it's difficult to share code because we
  61562. * handle e.g. "\n" very differently: here we generate a single character
  61563. * range for it.
  61564. */
  61565. x = DUK__L0();
  61566. DUK__ADVANCE(lex_ctx, 1);
  61567. if (x == 'b') {
  61568. /* Note: '\b' in char class is different than outside (assertion),
  61569. * '\B' is not allowed and is caught by the duk_unicode_is_identifier_part()
  61570. * check below.
  61571. */
  61572. ch = 0x0008;
  61573. } else if (x == 'f') {
  61574. ch = 0x000c;
  61575. } else if (x == 'n') {
  61576. ch = 0x000a;
  61577. } else if (x == 't') {
  61578. ch = 0x0009;
  61579. } else if (x == 'r') {
  61580. ch = 0x000d;
  61581. } else if (x == 'v') {
  61582. ch = 0x000b;
  61583. } else if (x == 'c') {
  61584. x = DUK__L0();
  61585. DUK__ADVANCE(lex_ctx, 1);
  61586. if ((x >= 'a' && x <= 'z') ||
  61587. (x >= 'A' && x <= 'Z')) {
  61588. ch = (x % 32);
  61589. } else {
  61590. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61591. "invalid regexp control escape");
  61592. return; /* never reached, but avoids warnings of
  61593. * potentially unused variables.
  61594. */
  61595. }
  61596. } else if (x == 'x') {
  61597. ch = duk__decode_hexesc_from_window(lex_ctx, 0);
  61598. DUK__ADVANCE(lex_ctx, 2);
  61599. } else if (x == 'u') {
  61600. ch = duk__decode_uniesc_from_window(lex_ctx, 0);
  61601. DUK__ADVANCE(lex_ctx, 4);
  61602. } else if (x == 'd') {
  61603. duk__emit_u16_direct_ranges(lex_ctx,
  61604. gen_range,
  61605. userdata,
  61606. duk_unicode_re_ranges_digit,
  61607. sizeof(duk_unicode_re_ranges_digit) / sizeof(duk_uint16_t));
  61608. ch = -1;
  61609. } else if (x == 'D') {
  61610. duk__emit_u16_direct_ranges(lex_ctx,
  61611. gen_range,
  61612. userdata,
  61613. duk_unicode_re_ranges_not_digit,
  61614. sizeof(duk_unicode_re_ranges_not_digit) / sizeof(duk_uint16_t));
  61615. ch = -1;
  61616. } else if (x == 's') {
  61617. duk__emit_u16_direct_ranges(lex_ctx,
  61618. gen_range,
  61619. userdata,
  61620. duk_unicode_re_ranges_white,
  61621. sizeof(duk_unicode_re_ranges_white) / sizeof(duk_uint16_t));
  61622. ch = -1;
  61623. } else if (x == 'S') {
  61624. duk__emit_u16_direct_ranges(lex_ctx,
  61625. gen_range,
  61626. userdata,
  61627. duk_unicode_re_ranges_not_white,
  61628. sizeof(duk_unicode_re_ranges_not_white) / sizeof(duk_uint16_t));
  61629. ch = -1;
  61630. } else if (x == 'w') {
  61631. duk__emit_u16_direct_ranges(lex_ctx,
  61632. gen_range,
  61633. userdata,
  61634. duk_unicode_re_ranges_wordchar,
  61635. sizeof(duk_unicode_re_ranges_wordchar) / sizeof(duk_uint16_t));
  61636. ch = -1;
  61637. } else if (x == 'W') {
  61638. duk__emit_u16_direct_ranges(lex_ctx,
  61639. gen_range,
  61640. userdata,
  61641. duk_unicode_re_ranges_not_wordchar,
  61642. sizeof(duk_unicode_re_ranges_not_wordchar) / sizeof(duk_uint16_t));
  61643. ch = -1;
  61644. } else if (DUK__ISDIGIT(x)) {
  61645. /* DecimalEscape, only \0 is allowed, no leading zeroes are allowed */
  61646. if (x == '0' && !DUK__ISDIGIT(DUK__L0())) {
  61647. ch = 0x0000;
  61648. } else {
  61649. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61650. "invalid decimal escape");
  61651. }
  61652. } else if (!duk_unicode_is_identifier_part(x)
  61653. #if defined(DUK_USE_NONSTD_REGEXP_DOLLAR_ESCAPE)
  61654. || x == '$'
  61655. #endif
  61656. ) {
  61657. /* IdentityEscape */
  61658. ch = x;
  61659. } else {
  61660. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61661. "invalid regexp escape");
  61662. }
  61663. } else {
  61664. /* character represents itself */
  61665. ch = x;
  61666. }
  61667. /* ch is a literal character here or -1 if parsed entity was
  61668. * an escape such as "\s".
  61669. */
  61670. if (ch < 0) {
  61671. /* multi-character sets not allowed as part of ranges, see
  61672. * E5 Section 15.10.2.15, abstract operation CharacterRange.
  61673. */
  61674. if (start >= 0) {
  61675. if (dash) {
  61676. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61677. "invalid range");
  61678. } else {
  61679. gen_range(userdata, start, start, 0);
  61680. start = -1;
  61681. /* dash is already 0 */
  61682. }
  61683. }
  61684. } else {
  61685. if (start >= 0) {
  61686. if (dash) {
  61687. if (start > ch) {
  61688. DUK_ERROR(lex_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  61689. "invalid range");
  61690. }
  61691. gen_range(userdata, start, ch, 0);
  61692. start = -1;
  61693. dash = 0;
  61694. } else {
  61695. gen_range(userdata, start, start, 0);
  61696. start = ch;
  61697. /* dash is already 0 */
  61698. }
  61699. } else {
  61700. start = ch;
  61701. }
  61702. }
  61703. }
  61704. return;
  61705. }
  61706. #endif /* DUK_USE_REGEXP_SUPPORT */
  61707. #line 1 "duk_numconv.c"
  61708. /*
  61709. * Number-to-string and string-to-number conversions.
  61710. *
  61711. * Slow path number-to-string and string-to-number conversion is based on
  61712. * a Dragon4 variant, with fast paths for small integers. Big integer
  61713. * arithmetic is needed for guaranteeing that the conversion is correct
  61714. * and uses a minimum number of digits. The big number arithmetic has a
  61715. * fixed maximum size and does not require dynamic allocations.
  61716. *
  61717. * See: doc/number-conversion.txt.
  61718. */
  61719. /* include removed: duk_internal.h */
  61720. #define DUK__IEEE_DOUBLE_EXP_BIAS 1023
  61721. #define DUK__IEEE_DOUBLE_EXP_MIN (-1022) /* biased exp == 0 -> denormal, exp -1022 */
  61722. #define DUK__DIGITCHAR(x) duk_lc_digits[(x)]
  61723. /*
  61724. * Tables generated with src/gennumdigits.py.
  61725. *
  61726. * duk__str2num_digits_for_radix indicates, for each radix, how many input
  61727. * digits should be considered significant for string-to-number conversion.
  61728. * The input is also padded to this many digits to give the Dragon4
  61729. * conversion enough (apparent) precision to work with.
  61730. *
  61731. * duk__str2num_exp_limits indicates, for each radix, the radix-specific
  61732. * minimum/maximum exponent values (for a Dragon4 integer mantissa)
  61733. * below and above which the number is guaranteed to underflow to zero
  61734. * or overflow to Infinity. This allows parsing to keep bigint values
  61735. * bounded.
  61736. */
  61737. DUK_LOCAL const duk_uint8_t duk__str2num_digits_for_radix[] = {
  61738. 69, 44, 35, 30, 27, 25, 23, 22, 20, 20, /* 2 to 11 */
  61739. 20, 19, 19, 18, 18, 17, 17, 17, 16, 16, /* 12 to 21 */
  61740. 16, 16, 16, 15, 15, 15, 15, 15, 15, 14, /* 22 to 31 */
  61741. 14, 14, 14, 14, 14 /* 31 to 36 */
  61742. };
  61743. typedef struct {
  61744. duk_int16_t upper;
  61745. duk_int16_t lower;
  61746. } duk__exp_limits;
  61747. DUK_LOCAL const duk__exp_limits duk__str2num_exp_limits[] = {
  61748. { 957, -1147 }, { 605, -725 }, { 479, -575 }, { 414, -496 },
  61749. { 372, -446 }, { 342, -411 }, { 321, -384 }, { 304, -364 },
  61750. { 291, -346 }, { 279, -334 }, { 268, -323 }, { 260, -312 },
  61751. { 252, -304 }, { 247, -296 }, { 240, -289 }, { 236, -283 },
  61752. { 231, -278 }, { 227, -273 }, { 223, -267 }, { 220, -263 },
  61753. { 216, -260 }, { 213, -256 }, { 210, -253 }, { 208, -249 },
  61754. { 205, -246 }, { 203, -244 }, { 201, -241 }, { 198, -239 },
  61755. { 196, -237 }, { 195, -234 }, { 193, -232 }, { 191, -230 },
  61756. { 190, -228 }, { 188, -226 }, { 187, -225 },
  61757. };
  61758. /*
  61759. * Limited functionality bigint implementation.
  61760. *
  61761. * Restricted to non-negative numbers with less than 32 * DUK__BI_MAX_PARTS bits,
  61762. * with the caller responsible for ensuring this is never exceeded. No memory
  61763. * allocation (except stack) is needed for bigint computation. Operations
  61764. * have been tailored for number conversion needs.
  61765. *
  61766. * Argument order is "assignment order", i.e. target first, then arguments:
  61767. * x <- y * z --> duk__bi_mul(x, y, z);
  61768. */
  61769. /* This upper value has been experimentally determined; debug build will check
  61770. * bigint size with assertions.
  61771. */
  61772. #define DUK__BI_MAX_PARTS 37 /* 37x32 = 1184 bits */
  61773. #ifdef DUK_USE_DDDPRINT
  61774. #define DUK__BI_PRINT(name,x) duk__bi_print((name),(x))
  61775. #else
  61776. #define DUK__BI_PRINT(name,x)
  61777. #endif
  61778. /* Current size is about 152 bytes. */
  61779. typedef struct {
  61780. duk_small_int_t n;
  61781. duk_uint32_t v[DUK__BI_MAX_PARTS]; /* low to high */
  61782. } duk__bigint;
  61783. #ifdef DUK_USE_DDDPRINT
  61784. DUK_LOCAL void duk__bi_print(const char *name, duk__bigint *x) {
  61785. /* Overestimate required size; debug code so not critical to be tight. */
  61786. char buf[DUK__BI_MAX_PARTS * 9 + 64];
  61787. char *p = buf;
  61788. duk_small_int_t i;
  61789. /* No NUL term checks in this debug code. */
  61790. p += DUK_SPRINTF(p, "%p n=%ld", (void *) x, (long) x->n);
  61791. if (x->n == 0) {
  61792. p += DUK_SPRINTF(p, " 0");
  61793. }
  61794. for (i = x->n - 1; i >= 0; i--) {
  61795. p += DUK_SPRINTF(p, " %08lx", (unsigned long) x->v[i]);
  61796. }
  61797. DUK_DDD(DUK_DDDPRINT("%s: %s", (const char *) name, (const char *) buf));
  61798. }
  61799. #endif
  61800. #ifdef DUK_USE_ASSERTIONS
  61801. DUK_LOCAL duk_small_int_t duk__bi_is_valid(duk__bigint *x) {
  61802. return (duk_small_int_t)
  61803. ( ((x->n >= 0) && (x->n <= DUK__BI_MAX_PARTS)) /* is valid size */ &&
  61804. ((x->n == 0) || (x->v[x->n - 1] != 0)) /* is normalized */ );
  61805. }
  61806. #endif
  61807. DUK_LOCAL void duk__bi_normalize(duk__bigint *x) {
  61808. duk_small_int_t i;
  61809. for (i = x->n - 1; i >= 0; i--) {
  61810. if (x->v[i] != 0) {
  61811. break;
  61812. }
  61813. }
  61814. /* Note: if 'x' is zero, x->n becomes 0 here */
  61815. x->n = i + 1;
  61816. DUK_ASSERT(duk__bi_is_valid(x));
  61817. }
  61818. /* x <- y */
  61819. DUK_LOCAL void duk__bi_copy(duk__bigint *x, duk__bigint *y) {
  61820. duk_small_int_t n;
  61821. n = y->n;
  61822. x->n = n;
  61823. if (n == 0) {
  61824. return;
  61825. }
  61826. DUK_MEMCPY((void *) x->v, (void *) y->v, (size_t) (sizeof(duk_uint32_t) * n));
  61827. }
  61828. DUK_LOCAL void duk__bi_set_small(duk__bigint *x, duk_uint32_t v) {
  61829. if (v == 0U) {
  61830. x->n = 0;
  61831. } else {
  61832. x->n = 1;
  61833. x->v[0] = v;
  61834. }
  61835. DUK_ASSERT(duk__bi_is_valid(x));
  61836. }
  61837. /* Return value: <0 <=> x < y
  61838. * 0 <=> x == y
  61839. * >0 <=> x > y
  61840. */
  61841. DUK_LOCAL int duk__bi_compare(duk__bigint *x, duk__bigint *y) {
  61842. duk_small_int_t i, nx, ny;
  61843. duk_uint32_t tx, ty;
  61844. DUK_ASSERT(duk__bi_is_valid(x));
  61845. DUK_ASSERT(duk__bi_is_valid(y));
  61846. nx = x->n;
  61847. ny = y->n;
  61848. if (nx > ny) {
  61849. goto ret_gt;
  61850. }
  61851. if (nx < ny) {
  61852. goto ret_lt;
  61853. }
  61854. for (i = nx - 1; i >= 0; i--) {
  61855. tx = x->v[i];
  61856. ty = y->v[i];
  61857. if (tx > ty) {
  61858. goto ret_gt;
  61859. }
  61860. if (tx < ty) {
  61861. goto ret_lt;
  61862. }
  61863. }
  61864. return 0;
  61865. ret_gt:
  61866. return 1;
  61867. ret_lt:
  61868. return -1;
  61869. }
  61870. /* x <- y + z */
  61871. #ifdef DUK_USE_64BIT_OPS
  61872. DUK_LOCAL void duk__bi_add(duk__bigint *x, duk__bigint *y, duk__bigint *z) {
  61873. duk_uint64_t tmp;
  61874. duk_small_int_t i, ny, nz;
  61875. DUK_ASSERT(duk__bi_is_valid(y));
  61876. DUK_ASSERT(duk__bi_is_valid(z));
  61877. if (z->n > y->n) {
  61878. duk__bigint *t;
  61879. t = y; y = z; z = t;
  61880. }
  61881. DUK_ASSERT(y->n >= z->n);
  61882. ny = y->n; nz = z->n;
  61883. tmp = 0U;
  61884. for (i = 0; i < ny; i++) {
  61885. DUK_ASSERT(i < DUK__BI_MAX_PARTS);
  61886. tmp += y->v[i];
  61887. if (i < nz) {
  61888. tmp += z->v[i];
  61889. }
  61890. x->v[i] = (duk_uint32_t) (tmp & 0xffffffffUL);
  61891. tmp = tmp >> 32;
  61892. }
  61893. if (tmp != 0U) {
  61894. DUK_ASSERT(i < DUK__BI_MAX_PARTS);
  61895. x->v[i++] = (duk_uint32_t) tmp;
  61896. }
  61897. x->n = i;
  61898. DUK_ASSERT(x->n <= DUK__BI_MAX_PARTS);
  61899. /* no need to normalize */
  61900. DUK_ASSERT(duk__bi_is_valid(x));
  61901. }
  61902. #else /* DUK_USE_64BIT_OPS */
  61903. DUK_LOCAL void duk__bi_add(duk__bigint *x, duk__bigint *y, duk__bigint *z) {
  61904. duk_uint32_t carry, tmp1, tmp2;
  61905. duk_small_int_t i, ny, nz;
  61906. DUK_ASSERT(duk__bi_is_valid(y));
  61907. DUK_ASSERT(duk__bi_is_valid(z));
  61908. if (z->n > y->n) {
  61909. duk__bigint *t;
  61910. t = y; y = z; z = t;
  61911. }
  61912. DUK_ASSERT(y->n >= z->n);
  61913. ny = y->n; nz = z->n;
  61914. carry = 0U;
  61915. for (i = 0; i < ny; i++) {
  61916. /* Carry is detected based on wrapping which relies on exact 32-bit
  61917. * types.
  61918. */
  61919. DUK_ASSERT(i < DUK__BI_MAX_PARTS);
  61920. tmp1 = y->v[i];
  61921. tmp2 = tmp1;
  61922. if (i < nz) {
  61923. tmp2 += z->v[i];
  61924. }
  61925. /* Careful with carry condition:
  61926. * - If carry not added: 0x12345678 + 0 + 0xffffffff = 0x12345677 (< 0x12345678)
  61927. * - If carry added: 0x12345678 + 1 + 0xffffffff = 0x12345678 (== 0x12345678)
  61928. */
  61929. if (carry) {
  61930. tmp2++;
  61931. carry = (tmp2 <= tmp1 ? 1U : 0U);
  61932. } else {
  61933. carry = (tmp2 < tmp1 ? 1U : 0U);
  61934. }
  61935. x->v[i] = tmp2;
  61936. }
  61937. if (carry) {
  61938. DUK_ASSERT(i < DUK__BI_MAX_PARTS);
  61939. DUK_ASSERT(carry == 1U);
  61940. x->v[i++] = carry;
  61941. }
  61942. x->n = i;
  61943. DUK_ASSERT(x->n <= DUK__BI_MAX_PARTS);
  61944. /* no need to normalize */
  61945. DUK_ASSERT(duk__bi_is_valid(x));
  61946. }
  61947. #endif /* DUK_USE_64BIT_OPS */
  61948. /* x <- y + z */
  61949. DUK_LOCAL void duk__bi_add_small(duk__bigint *x, duk__bigint *y, duk_uint32_t z) {
  61950. duk__bigint tmp;
  61951. DUK_ASSERT(duk__bi_is_valid(y));
  61952. /* XXX: this could be optimized; there is only one call site now though */
  61953. duk__bi_set_small(&tmp, z);
  61954. duk__bi_add(x, y, &tmp);
  61955. DUK_ASSERT(duk__bi_is_valid(x));
  61956. }
  61957. #if 0 /* unused */
  61958. /* x <- x + y, use t as temp */
  61959. DUK_LOCAL void duk__bi_add_copy(duk__bigint *x, duk__bigint *y, duk__bigint *t) {
  61960. duk__bi_add(t, x, y);
  61961. duk__bi_copy(x, t);
  61962. }
  61963. #endif
  61964. /* x <- y - z, require x >= y => z >= 0, i.e. y >= z */
  61965. #ifdef DUK_USE_64BIT_OPS
  61966. DUK_LOCAL void duk__bi_sub(duk__bigint *x, duk__bigint *y, duk__bigint *z) {
  61967. duk_small_int_t i, ny, nz;
  61968. duk_uint32_t ty, tz;
  61969. duk_int64_t tmp;
  61970. DUK_ASSERT(duk__bi_is_valid(y));
  61971. DUK_ASSERT(duk__bi_is_valid(z));
  61972. DUK_ASSERT(duk__bi_compare(y, z) >= 0);
  61973. DUK_ASSERT(y->n >= z->n);
  61974. ny = y->n; nz = z->n;
  61975. tmp = 0;
  61976. for (i = 0; i < ny; i++) {
  61977. ty = y->v[i];
  61978. if (i < nz) {
  61979. tz = z->v[i];
  61980. } else {
  61981. tz = 0;
  61982. }
  61983. tmp = (duk_int64_t) ty - (duk_int64_t) tz + tmp;
  61984. x->v[i] = (duk_uint32_t) (tmp & 0xffffffffUL);
  61985. tmp = tmp >> 32; /* 0 or -1 */
  61986. }
  61987. DUK_ASSERT(tmp == 0);
  61988. x->n = i;
  61989. duk__bi_normalize(x); /* need to normalize, may even cancel to 0 */
  61990. DUK_ASSERT(duk__bi_is_valid(x));
  61991. }
  61992. #else
  61993. DUK_LOCAL void duk__bi_sub(duk__bigint *x, duk__bigint *y, duk__bigint *z) {
  61994. duk_small_int_t i, ny, nz;
  61995. duk_uint32_t tmp1, tmp2, borrow;
  61996. DUK_ASSERT(duk__bi_is_valid(y));
  61997. DUK_ASSERT(duk__bi_is_valid(z));
  61998. DUK_ASSERT(duk__bi_compare(y, z) >= 0);
  61999. DUK_ASSERT(y->n >= z->n);
  62000. ny = y->n; nz = z->n;
  62001. borrow = 0U;
  62002. for (i = 0; i < ny; i++) {
  62003. /* Borrow is detected based on wrapping which relies on exact 32-bit
  62004. * types.
  62005. */
  62006. tmp1 = y->v[i];
  62007. tmp2 = tmp1;
  62008. if (i < nz) {
  62009. tmp2 -= z->v[i];
  62010. }
  62011. /* Careful with borrow condition:
  62012. * - If borrow not subtracted: 0x12345678 - 0 - 0xffffffff = 0x12345679 (> 0x12345678)
  62013. * - If borrow subtracted: 0x12345678 - 1 - 0xffffffff = 0x12345678 (== 0x12345678)
  62014. */
  62015. if (borrow) {
  62016. tmp2--;
  62017. borrow = (tmp2 >= tmp1 ? 1U : 0U);
  62018. } else {
  62019. borrow = (tmp2 > tmp1 ? 1U : 0U);
  62020. }
  62021. x->v[i] = tmp2;
  62022. }
  62023. DUK_ASSERT(borrow == 0U);
  62024. x->n = i;
  62025. duk__bi_normalize(x); /* need to normalize, may even cancel to 0 */
  62026. DUK_ASSERT(duk__bi_is_valid(x));
  62027. }
  62028. #endif
  62029. #if 0 /* unused */
  62030. /* x <- y - z */
  62031. DUK_LOCAL void duk__bi_sub_small(duk__bigint *x, duk__bigint *y, duk_uint32_t z) {
  62032. duk__bigint tmp;
  62033. DUK_ASSERT(duk__bi_is_valid(y));
  62034. /* XXX: this could be optimized */
  62035. duk__bi_set_small(&tmp, z);
  62036. duk__bi_sub(x, y, &tmp);
  62037. DUK_ASSERT(duk__bi_is_valid(x));
  62038. }
  62039. #endif
  62040. /* x <- x - y, use t as temp */
  62041. DUK_LOCAL void duk__bi_sub_copy(duk__bigint *x, duk__bigint *y, duk__bigint *t) {
  62042. duk__bi_sub(t, x, y);
  62043. duk__bi_copy(x, t);
  62044. }
  62045. /* x <- y * z */
  62046. DUK_LOCAL void duk__bi_mul(duk__bigint *x, duk__bigint *y, duk__bigint *z) {
  62047. duk_small_int_t i, j, nx, nz;
  62048. DUK_ASSERT(duk__bi_is_valid(y));
  62049. DUK_ASSERT(duk__bi_is_valid(z));
  62050. nx = y->n + z->n; /* max possible */
  62051. DUK_ASSERT(nx <= DUK__BI_MAX_PARTS);
  62052. if (nx == 0) {
  62053. /* Both inputs are zero; cases where only one is zero can go
  62054. * through main algorithm.
  62055. */
  62056. x->n = 0;
  62057. return;
  62058. }
  62059. DUK_MEMZERO((void *) x->v, (size_t) (sizeof(duk_uint32_t) * nx));
  62060. x->n = nx;
  62061. nz = z->n;
  62062. for (i = 0; i < y->n; i++) {
  62063. #ifdef DUK_USE_64BIT_OPS
  62064. duk_uint64_t tmp = 0U;
  62065. for (j = 0; j < nz; j++) {
  62066. tmp += (duk_uint64_t) y->v[i] * (duk_uint64_t) z->v[j] + x->v[i+j];
  62067. x->v[i+j] = (duk_uint32_t) (tmp & 0xffffffffUL);
  62068. tmp = tmp >> 32;
  62069. }
  62070. if (tmp > 0) {
  62071. DUK_ASSERT(i + j < nx);
  62072. DUK_ASSERT(i + j < DUK__BI_MAX_PARTS);
  62073. DUK_ASSERT(x->v[i+j] == 0U);
  62074. x->v[i+j] = (duk_uint32_t) tmp;
  62075. }
  62076. #else
  62077. /*
  62078. * Multiply + add + carry for 32-bit components using only 16x16->32
  62079. * multiplies and carry detection based on unsigned overflow.
  62080. *
  62081. * 1st mult, 32-bit: (A*2^16 + B)
  62082. * 2nd mult, 32-bit: (C*2^16 + D)
  62083. * 3rd add, 32-bit: E
  62084. * 4th add, 32-bit: F
  62085. *
  62086. * (AC*2^16 + B) * (C*2^16 + D) + E + F
  62087. * = AC*2^32 + AD*2^16 + BC*2^16 + BD + E + F
  62088. * = AC*2^32 + (AD + BC)*2^16 + (BD + E + F)
  62089. * = AC*2^32 + AD*2^16 + BC*2^16 + (BD + E + F)
  62090. */
  62091. duk_uint32_t a, b, c, d, e, f;
  62092. duk_uint32_t r, s, t;
  62093. a = y->v[i]; b = a & 0xffffUL; a = a >> 16;
  62094. f = 0;
  62095. for (j = 0; j < nz; j++) {
  62096. c = z->v[j]; d = c & 0xffffUL; c = c >> 16;
  62097. e = x->v[i+j];
  62098. /* build result as: (r << 32) + s: start with (BD + E + F) */
  62099. r = 0;
  62100. s = b * d;
  62101. /* add E */
  62102. t = s + e;
  62103. if (t < s) { r++; } /* carry */
  62104. s = t;
  62105. /* add F */
  62106. t = s + f;
  62107. if (t < s) { r++; } /* carry */
  62108. s = t;
  62109. /* add BC*2^16 */
  62110. t = b * c;
  62111. r += (t >> 16);
  62112. t = s + ((t & 0xffffUL) << 16);
  62113. if (t < s) { r++; } /* carry */
  62114. s = t;
  62115. /* add AD*2^16 */
  62116. t = a * d;
  62117. r += (t >> 16);
  62118. t = s + ((t & 0xffffUL) << 16);
  62119. if (t < s) { r++; } /* carry */
  62120. s = t;
  62121. /* add AC*2^32 */
  62122. t = a * c;
  62123. r += t;
  62124. DUK_DDD(DUK_DDDPRINT("ab=%08lx cd=%08lx ef=%08lx -> rs=%08lx %08lx",
  62125. (unsigned long) y->v[i], (unsigned long) z->v[j],
  62126. (unsigned long) x->v[i+j], (unsigned long) r,
  62127. (unsigned long) s));
  62128. x->v[i+j] = s;
  62129. f = r;
  62130. }
  62131. if (f > 0U) {
  62132. DUK_ASSERT(i + j < nx);
  62133. DUK_ASSERT(i + j < DUK__BI_MAX_PARTS);
  62134. DUK_ASSERT(x->v[i+j] == 0U);
  62135. x->v[i+j] = (duk_uint32_t) f;
  62136. }
  62137. #endif /* DUK_USE_64BIT_OPS */
  62138. }
  62139. duk__bi_normalize(x);
  62140. DUK_ASSERT(duk__bi_is_valid(x));
  62141. }
  62142. /* x <- y * z */
  62143. DUK_LOCAL void duk__bi_mul_small(duk__bigint *x, duk__bigint *y, duk_uint32_t z) {
  62144. duk__bigint tmp;
  62145. DUK_ASSERT(duk__bi_is_valid(y));
  62146. /* XXX: this could be optimized */
  62147. duk__bi_set_small(&tmp, z);
  62148. duk__bi_mul(x, y, &tmp);
  62149. DUK_ASSERT(duk__bi_is_valid(x));
  62150. }
  62151. /* x <- x * y, use t as temp */
  62152. DUK_LOCAL void duk__bi_mul_copy(duk__bigint *x, duk__bigint *y, duk__bigint *t) {
  62153. duk__bi_mul(t, x, y);
  62154. duk__bi_copy(x, t);
  62155. }
  62156. /* x <- x * y, use t as temp */
  62157. DUK_LOCAL void duk__bi_mul_small_copy(duk__bigint *x, duk_uint32_t y, duk__bigint *t) {
  62158. duk__bi_mul_small(t, x, y);
  62159. duk__bi_copy(x, t);
  62160. }
  62161. DUK_LOCAL int duk__bi_is_even(duk__bigint *x) {
  62162. DUK_ASSERT(duk__bi_is_valid(x));
  62163. return (x->n == 0) || ((x->v[0] & 0x01) == 0);
  62164. }
  62165. DUK_LOCAL int duk__bi_is_zero(duk__bigint *x) {
  62166. DUK_ASSERT(duk__bi_is_valid(x));
  62167. return (x->n == 0); /* this is the case for normalized numbers */
  62168. }
  62169. /* Bigint is 2^52. Used to detect normalized IEEE double mantissa values
  62170. * which are at the lowest edge (next floating point value downwards has
  62171. * a different exponent). The lowest mantissa has the form:
  62172. *
  62173. * 1000........000 (52 zeroes; only "hidden bit" is set)
  62174. */
  62175. DUK_LOCAL duk_small_int_t duk__bi_is_2to52(duk__bigint *x) {
  62176. DUK_ASSERT(duk__bi_is_valid(x));
  62177. return (duk_small_int_t)
  62178. (x->n == 2) && (x->v[0] == 0U) && (x->v[1] == (1U << (52-32)));
  62179. }
  62180. /* x <- (1<<y) */
  62181. DUK_LOCAL void duk__bi_twoexp(duk__bigint *x, duk_small_int_t y) {
  62182. duk_small_int_t n, r;
  62183. n = (y / 32) + 1;
  62184. DUK_ASSERT(n > 0);
  62185. r = y % 32;
  62186. DUK_MEMZERO((void *) x->v, sizeof(duk_uint32_t) * n);
  62187. x->n = n;
  62188. x->v[n - 1] = (((duk_uint32_t) 1) << r);
  62189. }
  62190. /* x <- b^y; use t1 and t2 as temps */
  62191. 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) {
  62192. /* Fast path the binary case */
  62193. DUK_ASSERT(x != t1 && x != t2 && t1 != t2); /* distinct bignums, easy mistake to make */
  62194. DUK_ASSERT(b >= 0);
  62195. DUK_ASSERT(y >= 0);
  62196. if (b == 2) {
  62197. duk__bi_twoexp(x, y);
  62198. return;
  62199. }
  62200. /* http://en.wikipedia.org/wiki/Exponentiation_by_squaring */
  62201. DUK_DDD(DUK_DDDPRINT("exp_small: b=%ld, y=%ld", (long) b, (long) y));
  62202. duk__bi_set_small(x, 1);
  62203. duk__bi_set_small(t1, b);
  62204. for (;;) {
  62205. /* Loop structure ensures that we don't compute t1^2 unnecessarily
  62206. * on the final round, as that might create a bignum exceeding the
  62207. * current DUK__BI_MAX_PARTS limit.
  62208. */
  62209. if (y & 0x01) {
  62210. duk__bi_mul_copy(x, t1, t2);
  62211. }
  62212. y = y >> 1;
  62213. if (y == 0) {
  62214. break;
  62215. }
  62216. duk__bi_mul_copy(t1, t1, t2);
  62217. }
  62218. DUK__BI_PRINT("exp_small result", x);
  62219. }
  62220. /*
  62221. * A Dragon4 number-to-string variant, based on:
  62222. *
  62223. * Guy L. Steele Jr., Jon L. White: "How to Print Floating-Point Numbers
  62224. * Accurately"
  62225. *
  62226. * Robert G. Burger, R. Kent Dybvig: "Printing Floating-Point Numbers
  62227. * Quickly and Accurately"
  62228. *
  62229. * The current algorithm is based on Figure 1 of the Burger-Dybvig paper,
  62230. * i.e. the base implementation without logarithm estimation speedups
  62231. * (these would increase code footprint considerably). Fixed-format output
  62232. * does not follow the suggestions in the paper; instead, we generate an
  62233. * extra digit and round-with-carry.
  62234. *
  62235. * The same algorithm is used for number parsing (with b=10 and B=2)
  62236. * by generating one extra digit and doing rounding manually.
  62237. *
  62238. * See doc/number-conversion.txt for limitations.
  62239. */
  62240. /* Maximum number of digits generated. */
  62241. #define DUK__MAX_OUTPUT_DIGITS 1040 /* (Number.MAX_VALUE).toString(2).length == 1024, + spare */
  62242. /* Maximum number of characters in formatted value. */
  62243. #define DUK__MAX_FORMATTED_LENGTH 1040 /* (-Number.MAX_VALUE).toString(2).length == 1025, + spare */
  62244. /* Number and (minimum) size of bigints in the nc_ctx structure. */
  62245. #define DUK__NUMCONV_CTX_NUM_BIGINTS 7
  62246. #define DUK__NUMCONV_CTX_BIGINTS_SIZE (sizeof(duk__bigint) * DUK__NUMCONV_CTX_NUM_BIGINTS)
  62247. typedef struct {
  62248. /* Currently about 7*152 = 1064 bytes. The space for these
  62249. * duk__bigints is used also as a temporary buffer for generating
  62250. * the final string. This is a bit awkard; a union would be
  62251. * more correct.
  62252. */
  62253. duk__bigint f, r, s, mp, mm, t1, t2;
  62254. duk_small_int_t is_s2n; /* if 1, doing a string-to-number; else doing a number-to-string */
  62255. duk_small_int_t is_fixed; /* if 1, doing a fixed format output (not free format) */
  62256. duk_small_int_t req_digits; /* requested number of output digits; 0 = free-format */
  62257. duk_small_int_t abs_pos; /* digit position is absolute, not relative */
  62258. duk_small_int_t e; /* exponent for 'f' */
  62259. duk_small_int_t b; /* input radix */
  62260. duk_small_int_t B; /* output radix */
  62261. duk_small_int_t k; /* see algorithm */
  62262. duk_small_int_t low_ok; /* see algorithm */
  62263. duk_small_int_t high_ok; /* see algorithm */
  62264. duk_small_int_t unequal_gaps; /* m+ != m- (very rarely) */
  62265. /* Buffer used for generated digits, values are in the range [0,B-1]. */
  62266. duk_uint8_t digits[DUK__MAX_OUTPUT_DIGITS];
  62267. duk_small_int_t count; /* digit count */
  62268. } duk__numconv_stringify_ctx;
  62269. /* Note: computes with 'idx' in assertions, so caller beware.
  62270. * 'idx' is preincremented, i.e. '1' on first call, because it
  62271. * is more convenient for the caller.
  62272. */
  62273. #define DUK__DRAGON4_OUTPUT_PREINC(nc_ctx,preinc_idx,x) do { \
  62274. DUK_ASSERT((preinc_idx) - 1 >= 0); \
  62275. DUK_ASSERT((preinc_idx) - 1 < DUK__MAX_OUTPUT_DIGITS); \
  62276. ((nc_ctx)->digits[(preinc_idx) - 1]) = (duk_uint8_t) (x); \
  62277. } while (0)
  62278. DUK_LOCAL duk_size_t duk__dragon4_format_uint32(duk_uint8_t *buf, duk_uint32_t x, duk_small_int_t radix) {
  62279. duk_uint8_t *p;
  62280. duk_size_t len;
  62281. duk_small_int_t dig;
  62282. duk_small_int_t t;
  62283. DUK_ASSERT(radix >= 2 && radix <= 36);
  62284. /* A 32-bit unsigned integer formats to at most 32 digits (the
  62285. * worst case happens with radix == 2). Output the digits backwards,
  62286. * and use a memmove() to get them in the right place.
  62287. */
  62288. p = buf + 32;
  62289. for (;;) {
  62290. t = x / radix;
  62291. dig = x - t * radix;
  62292. x = t;
  62293. DUK_ASSERT(dig >= 0 && dig < 36);
  62294. *(--p) = DUK__DIGITCHAR(dig);
  62295. if (x == 0) {
  62296. break;
  62297. }
  62298. }
  62299. len = (duk_size_t) ((buf + 32) - p);
  62300. DUK_MEMMOVE((void *) buf, (void *) p, (size_t) len);
  62301. return len;
  62302. }
  62303. DUK_LOCAL void duk__dragon4_prepare(duk__numconv_stringify_ctx *nc_ctx) {
  62304. duk_small_int_t lowest_mantissa;
  62305. #if 1
  62306. /* Assume IEEE round-to-even, so that shorter encoding can be used
  62307. * when round-to-even would produce correct result. By removing
  62308. * this check (and having low_ok == high_ok == 0) the results would
  62309. * still be accurate but in some cases longer than necessary.
  62310. */
  62311. if (duk__bi_is_even(&nc_ctx->f)) {
  62312. DUK_DDD(DUK_DDDPRINT("f is even"));
  62313. nc_ctx->low_ok = 1;
  62314. nc_ctx->high_ok = 1;
  62315. } else {
  62316. DUK_DDD(DUK_DDDPRINT("f is odd"));
  62317. nc_ctx->low_ok = 0;
  62318. nc_ctx->high_ok = 0;
  62319. }
  62320. #else
  62321. /* Note: not honoring round-to-even should work but now generates incorrect
  62322. * results. For instance, 1e23 serializes to "a000...", i.e. the first digit
  62323. * equals the radix (10). Scaling stops one step too early in this case.
  62324. * Don't know why this is the case, but since this code path is unused, it
  62325. * doesn't matter.
  62326. */
  62327. nc_ctx->low_ok = 0;
  62328. nc_ctx->high_ok = 0;
  62329. #endif
  62330. /* For string-to-number, pretend we never have the lowest mantissa as there
  62331. * is no natural "precision" for inputs. Having lowest_mantissa == 0, we'll
  62332. * fall into the base cases for both e >= 0 and e < 0.
  62333. */
  62334. if (nc_ctx->is_s2n) {
  62335. lowest_mantissa = 0;
  62336. } else {
  62337. lowest_mantissa = duk__bi_is_2to52(&nc_ctx->f);
  62338. }
  62339. nc_ctx->unequal_gaps = 0;
  62340. if (nc_ctx->e >= 0) {
  62341. /* exponent non-negative (and thus not minimum exponent) */
  62342. if (lowest_mantissa) {
  62343. /* (>= e 0) AND (= f (expt b (- p 1)))
  62344. *
  62345. * be <- (expt b e) == b^e
  62346. * be1 <- (* be b) == (expt b (+ e 1)) == b^(e+1)
  62347. * r <- (* f be1 2) == 2 * f * b^(e+1) [if b==2 -> f * b^(e+2)]
  62348. * s <- (* b 2) [if b==2 -> 4]
  62349. * m+ <- be1 == b^(e+1)
  62350. * m- <- be == b^e
  62351. * k <- 0
  62352. * B <- B
  62353. * low_ok <- round
  62354. * high_ok <- round
  62355. */
  62356. DUK_DDD(DUK_DDDPRINT("non-negative exponent (not smallest exponent); "
  62357. "lowest mantissa value for this exponent -> "
  62358. "unequal gaps"));
  62359. duk__bi_exp_small(&nc_ctx->mm, nc_ctx->b, nc_ctx->e, &nc_ctx->t1, &nc_ctx->t2); /* mm <- b^e */
  62360. duk__bi_mul_small(&nc_ctx->mp, &nc_ctx->mm, nc_ctx->b); /* mp <- b^(e+1) */
  62361. duk__bi_mul_small(&nc_ctx->t1, &nc_ctx->f, 2);
  62362. duk__bi_mul(&nc_ctx->r, &nc_ctx->t1, &nc_ctx->mp); /* r <- (2 * f) * b^(e+1) */
  62363. duk__bi_set_small(&nc_ctx->s, nc_ctx->b * 2); /* s <- 2 * b */
  62364. nc_ctx->unequal_gaps = 1;
  62365. } else {
  62366. /* (>= e 0) AND (not (= f (expt b (- p 1))))
  62367. *
  62368. * be <- (expt b e) == b^e
  62369. * r <- (* f be 2) == 2 * f * b^e [if b==2 -> f * b^(e+1)]
  62370. * s <- 2
  62371. * m+ <- be == b^e
  62372. * m- <- be == b^e
  62373. * k <- 0
  62374. * B <- B
  62375. * low_ok <- round
  62376. * high_ok <- round
  62377. */
  62378. DUK_DDD(DUK_DDDPRINT("non-negative exponent (not smallest exponent); "
  62379. "not lowest mantissa for this exponent -> "
  62380. "equal gaps"));
  62381. duk__bi_exp_small(&nc_ctx->mm, nc_ctx->b, nc_ctx->e, &nc_ctx->t1, &nc_ctx->t2); /* mm <- b^e */
  62382. duk__bi_copy(&nc_ctx->mp, &nc_ctx->mm); /* mp <- b^e */
  62383. duk__bi_mul_small(&nc_ctx->t1, &nc_ctx->f, 2);
  62384. duk__bi_mul(&nc_ctx->r, &nc_ctx->t1, &nc_ctx->mp); /* r <- (2 * f) * b^e */
  62385. duk__bi_set_small(&nc_ctx->s, 2); /* s <- 2 */
  62386. }
  62387. } else {
  62388. /* When doing string-to-number, lowest_mantissa is always 0 so
  62389. * the exponent check, while incorrect, won't matter.
  62390. */
  62391. if (nc_ctx->e > DUK__IEEE_DOUBLE_EXP_MIN /*not minimum exponent*/ &&
  62392. lowest_mantissa /* lowest mantissa for this exponent*/) {
  62393. /* r <- (* f b 2) [if b==2 -> (* f 4)]
  62394. * s <- (* (expt b (- 1 e)) 2) == b^(1-e) * 2 [if b==2 -> b^(2-e)]
  62395. * m+ <- b == 2
  62396. * m- <- 1
  62397. * k <- 0
  62398. * B <- B
  62399. * low_ok <- round
  62400. * high_ok <- round
  62401. */
  62402. DUK_DDD(DUK_DDDPRINT("negative exponent; not minimum exponent and "
  62403. "lowest mantissa for this exponent -> "
  62404. "unequal gaps"));
  62405. duk__bi_mul_small(&nc_ctx->r, &nc_ctx->f, nc_ctx->b * 2); /* r <- (2 * b) * f */
  62406. 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 */
  62407. duk__bi_mul_small(&nc_ctx->s, &nc_ctx->t1, 2); /* s <- b^(1-e) * 2 */
  62408. duk__bi_set_small(&nc_ctx->mp, 2);
  62409. duk__bi_set_small(&nc_ctx->mm, 1);
  62410. nc_ctx->unequal_gaps = 1;
  62411. } else {
  62412. /* r <- (* f 2)
  62413. * s <- (* (expt b (- e)) 2) == b^(-e) * 2 [if b==2 -> b^(1-e)]
  62414. * m+ <- 1
  62415. * m- <- 1
  62416. * k <- 0
  62417. * B <- B
  62418. * low_ok <- round
  62419. * high_ok <- round
  62420. */
  62421. DUK_DDD(DUK_DDDPRINT("negative exponent; minimum exponent or not "
  62422. "lowest mantissa for this exponent -> "
  62423. "equal gaps"));
  62424. duk__bi_mul_small(&nc_ctx->r, &nc_ctx->f, 2); /* r <- 2 * f */
  62425. 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 */
  62426. duk__bi_mul_small(&nc_ctx->s, &nc_ctx->t1, 2); /* s <- b^(-e) * 2 */
  62427. duk__bi_set_small(&nc_ctx->mp, 1);
  62428. duk__bi_set_small(&nc_ctx->mm, 1);
  62429. }
  62430. }
  62431. }
  62432. DUK_LOCAL void duk__dragon4_scale(duk__numconv_stringify_ctx *nc_ctx) {
  62433. duk_small_int_t k = 0;
  62434. /* This is essentially the 'scale' algorithm, with recursion removed.
  62435. * Note that 'k' is either correct immediately, or will move in one
  62436. * direction in the loop. There's no need to do the low/high checks
  62437. * on every round (like the Scheme algorithm does).
  62438. *
  62439. * The scheme algorithm finds 'k' and updates 's' simultaneously,
  62440. * while the logical algorithm finds 'k' with 's' having its initial
  62441. * value, after which 's' is updated separately (see the Burger-Dybvig
  62442. * paper, Section 3.1, steps 2 and 3).
  62443. *
  62444. * The case where m+ == m- (almost always) is optimized for, because
  62445. * it reduces the bigint operations considerably and almost always
  62446. * applies. The scale loop only needs to work with m+, so this works.
  62447. */
  62448. /* XXX: this algorithm could be optimized quite a lot by using e.g.
  62449. * a logarithm based estimator for 'k' and performing B^n multiplication
  62450. * using a lookup table or using some bit-representation based exp
  62451. * algorithm. Currently we just loop, with significant performance
  62452. * impact for very large and very small numbers.
  62453. */
  62454. DUK_DDD(DUK_DDDPRINT("scale: B=%ld, low_ok=%ld, high_ok=%ld",
  62455. (long) nc_ctx->B, (long) nc_ctx->low_ok, (long) nc_ctx->high_ok));
  62456. DUK__BI_PRINT("r(init)", &nc_ctx->r);
  62457. DUK__BI_PRINT("s(init)", &nc_ctx->s);
  62458. DUK__BI_PRINT("mp(init)", &nc_ctx->mp);
  62459. DUK__BI_PRINT("mm(init)", &nc_ctx->mm);
  62460. for (;;) {
  62461. DUK_DDD(DUK_DDDPRINT("scale loop (inc k), k=%ld", (long) k));
  62462. DUK__BI_PRINT("r", &nc_ctx->r);
  62463. DUK__BI_PRINT("s", &nc_ctx->s);
  62464. DUK__BI_PRINT("m+", &nc_ctx->mp);
  62465. DUK__BI_PRINT("m-", &nc_ctx->mm);
  62466. duk__bi_add(&nc_ctx->t1, &nc_ctx->r, &nc_ctx->mp); /* t1 = (+ r m+) */
  62467. if (duk__bi_compare(&nc_ctx->t1, &nc_ctx->s) >= (nc_ctx->high_ok ? 0 : 1)) {
  62468. DUK_DDD(DUK_DDDPRINT("k is too low"));
  62469. /* r <- r
  62470. * s <- (* s B)
  62471. * m+ <- m+
  62472. * m- <- m-
  62473. * k <- (+ k 1)
  62474. */
  62475. duk__bi_mul_small_copy(&nc_ctx->s, nc_ctx->B, &nc_ctx->t1);
  62476. k++;
  62477. } else {
  62478. break;
  62479. }
  62480. }
  62481. /* k > 0 -> k was too low, and cannot be too high */
  62482. if (k > 0) {
  62483. goto skip_dec_k;
  62484. }
  62485. for (;;) {
  62486. DUK_DDD(DUK_DDDPRINT("scale loop (dec k), k=%ld", (long) k));
  62487. DUK__BI_PRINT("r", &nc_ctx->r);
  62488. DUK__BI_PRINT("s", &nc_ctx->s);
  62489. DUK__BI_PRINT("m+", &nc_ctx->mp);
  62490. DUK__BI_PRINT("m-", &nc_ctx->mm);
  62491. duk__bi_add(&nc_ctx->t1, &nc_ctx->r, &nc_ctx->mp); /* t1 = (+ r m+) */
  62492. duk__bi_mul_small(&nc_ctx->t2, &nc_ctx->t1, nc_ctx->B); /* t2 = (* (+ r m+) B) */
  62493. if (duk__bi_compare(&nc_ctx->t2, &nc_ctx->s) <= (nc_ctx->high_ok ? -1 : 0)) {
  62494. DUK_DDD(DUK_DDDPRINT("k is too high"));
  62495. /* r <- (* r B)
  62496. * s <- s
  62497. * m+ <- (* m+ B)
  62498. * m- <- (* m- B)
  62499. * k <- (- k 1)
  62500. */
  62501. duk__bi_mul_small_copy(&nc_ctx->r, nc_ctx->B, &nc_ctx->t1);
  62502. duk__bi_mul_small_copy(&nc_ctx->mp, nc_ctx->B, &nc_ctx->t1);
  62503. if (nc_ctx->unequal_gaps) {
  62504. DUK_DDD(DUK_DDDPRINT("m+ != m- -> need to update m- too"));
  62505. duk__bi_mul_small_copy(&nc_ctx->mm, nc_ctx->B, &nc_ctx->t1);
  62506. }
  62507. k--;
  62508. } else {
  62509. break;
  62510. }
  62511. }
  62512. skip_dec_k:
  62513. if (!nc_ctx->unequal_gaps) {
  62514. DUK_DDD(DUK_DDDPRINT("equal gaps, copy m- from m+"));
  62515. duk__bi_copy(&nc_ctx->mm, &nc_ctx->mp); /* mm <- mp */
  62516. }
  62517. nc_ctx->k = k;
  62518. DUK_DDD(DUK_DDDPRINT("final k: %ld", (long) k));
  62519. DUK__BI_PRINT("r(final)", &nc_ctx->r);
  62520. DUK__BI_PRINT("s(final)", &nc_ctx->s);
  62521. DUK__BI_PRINT("mp(final)", &nc_ctx->mp);
  62522. DUK__BI_PRINT("mm(final)", &nc_ctx->mm);
  62523. }
  62524. DUK_LOCAL void duk__dragon4_generate(duk__numconv_stringify_ctx *nc_ctx) {
  62525. duk_small_int_t tc1, tc2; /* terminating conditions */
  62526. duk_small_int_t d; /* current digit */
  62527. duk_small_int_t count = 0; /* digit count */
  62528. /*
  62529. * Digit generation loop.
  62530. *
  62531. * Different termination conditions:
  62532. *
  62533. * 1. Free format output. Terminate when shortest accurate
  62534. * representation found.
  62535. *
  62536. * 2. Fixed format output, with specific number of digits.
  62537. * Ignore termination conditions, terminate when digits
  62538. * generated. Caller requests an extra digit and rounds.
  62539. *
  62540. * 3. Fixed format output, with a specific absolute cut-off
  62541. * position (e.g. 10 digits after decimal point). Note
  62542. * that we always generate at least one digit, even if
  62543. * the digit is below the cut-off point already.
  62544. */
  62545. for (;;) {
  62546. DUK_DDD(DUK_DDDPRINT("generate loop, count=%ld, k=%ld, B=%ld, low_ok=%ld, high_ok=%ld",
  62547. (long) count, (long) nc_ctx->k, (long) nc_ctx->B,
  62548. (long) nc_ctx->low_ok, (long) nc_ctx->high_ok));
  62549. DUK__BI_PRINT("r", &nc_ctx->r);
  62550. DUK__BI_PRINT("s", &nc_ctx->s);
  62551. DUK__BI_PRINT("m+", &nc_ctx->mp);
  62552. DUK__BI_PRINT("m-", &nc_ctx->mm);
  62553. /* (quotient-remainder (* r B) s) using a dummy subtraction loop */
  62554. duk__bi_mul_small(&nc_ctx->t1, &nc_ctx->r, nc_ctx->B); /* t1 <- (* r B) */
  62555. d = 0;
  62556. for (;;) {
  62557. if (duk__bi_compare(&nc_ctx->t1, &nc_ctx->s) < 0) {
  62558. break;
  62559. }
  62560. duk__bi_sub_copy(&nc_ctx->t1, &nc_ctx->s, &nc_ctx->t2); /* t1 <- t1 - s */
  62561. d++;
  62562. }
  62563. duk__bi_copy(&nc_ctx->r, &nc_ctx->t1); /* r <- (remainder (* r B) s) */
  62564. /* d <- (quotient (* r B) s) (in range 0...B-1) */
  62565. DUK_DDD(DUK_DDDPRINT("-> d(quot)=%ld", (long) d));
  62566. DUK__BI_PRINT("r(rem)", &nc_ctx->r);
  62567. duk__bi_mul_small_copy(&nc_ctx->mp, nc_ctx->B, &nc_ctx->t2); /* m+ <- (* m+ B) */
  62568. duk__bi_mul_small_copy(&nc_ctx->mm, nc_ctx->B, &nc_ctx->t2); /* m- <- (* m- B) */
  62569. DUK__BI_PRINT("mp(upd)", &nc_ctx->mp);
  62570. DUK__BI_PRINT("mm(upd)", &nc_ctx->mm);
  62571. /* Terminating conditions. For fixed width output, we just ignore the
  62572. * terminating conditions (and pretend that tc1 == tc2 == false). The
  62573. * the current shortcut for fixed-format output is to generate a few
  62574. * extra digits and use rounding (with carry) to finish the output.
  62575. */
  62576. if (nc_ctx->is_fixed == 0) {
  62577. /* free-form */
  62578. tc1 = (duk__bi_compare(&nc_ctx->r, &nc_ctx->mm) <= (nc_ctx->low_ok ? 0 : -1));
  62579. duk__bi_add(&nc_ctx->t1, &nc_ctx->r, &nc_ctx->mp); /* t1 <- (+ r m+) */
  62580. tc2 = (duk__bi_compare(&nc_ctx->t1, &nc_ctx->s) >= (&nc_ctx->high_ok ? 0 : 1));
  62581. DUK_DDD(DUK_DDDPRINT("tc1=%ld, tc2=%ld", (long) tc1, (long) tc2));
  62582. } else {
  62583. /* fixed-format */
  62584. tc1 = 0;
  62585. tc2 = 0;
  62586. }
  62587. /* Count is incremented before DUK__DRAGON4_OUTPUT_PREINC() call
  62588. * on purpose, which is taken into account by the macro.
  62589. */
  62590. count++;
  62591. if (tc1) {
  62592. if (tc2) {
  62593. /* tc1 = true, tc2 = true */
  62594. duk__bi_mul_small(&nc_ctx->t1, &nc_ctx->r, 2);
  62595. if (duk__bi_compare(&nc_ctx->t1, &nc_ctx->s) < 0) { /* (< (* r 2) s) */
  62596. DUK_DDD(DUK_DDDPRINT("tc1=true, tc2=true, 2r > s: output d --> %ld (k=%ld)",
  62597. (long) d, (long) nc_ctx->k));
  62598. DUK__DRAGON4_OUTPUT_PREINC(nc_ctx, count, d);
  62599. } else {
  62600. DUK_DDD(DUK_DDDPRINT("tc1=true, tc2=true, 2r <= s: output d+1 --> %ld (k=%ld)",
  62601. (long) (d + 1), (long) nc_ctx->k));
  62602. DUK__DRAGON4_OUTPUT_PREINC(nc_ctx, count, d + 1);
  62603. }
  62604. break;
  62605. } else {
  62606. /* tc1 = true, tc2 = false */
  62607. DUK_DDD(DUK_DDDPRINT("tc1=true, tc2=false: output d --> %ld (k=%ld)",
  62608. (long) d, (long) nc_ctx->k));
  62609. DUK__DRAGON4_OUTPUT_PREINC(nc_ctx, count, d);
  62610. break;
  62611. }
  62612. } else {
  62613. if (tc2) {
  62614. /* tc1 = false, tc2 = true */
  62615. DUK_DDD(DUK_DDDPRINT("tc1=false, tc2=true: output d+1 --> %ld (k=%ld)",
  62616. (long) (d + 1), (long) nc_ctx->k));
  62617. DUK__DRAGON4_OUTPUT_PREINC(nc_ctx, count, d + 1);
  62618. break;
  62619. } else {
  62620. /* tc1 = false, tc2 = false */
  62621. DUK_DDD(DUK_DDDPRINT("tc1=false, tc2=false: output d --> %ld (k=%ld)",
  62622. (long) d, (long) nc_ctx->k));
  62623. DUK__DRAGON4_OUTPUT_PREINC(nc_ctx, count, d);
  62624. /* r <- r (updated above: r <- (remainder (* r B) s)
  62625. * s <- s
  62626. * m+ <- m+ (updated above: m+ <- (* m+ B)
  62627. * m- <- m- (updated above: m- <- (* m- B)
  62628. * B, low_ok, high_ok are fixed
  62629. */
  62630. /* fall through and continue for-loop */
  62631. }
  62632. }
  62633. /* fixed-format termination conditions */
  62634. if (nc_ctx->is_fixed) {
  62635. if (nc_ctx->abs_pos) {
  62636. int pos = nc_ctx->k - count + 1; /* count is already incremented, take into account */
  62637. DUK_DDD(DUK_DDDPRINT("fixed format, absolute: abs pos=%ld, k=%ld, count=%ld, req=%ld",
  62638. (long) pos, (long) nc_ctx->k, (long) count, (long) nc_ctx->req_digits));
  62639. if (pos <= nc_ctx->req_digits) {
  62640. DUK_DDD(DUK_DDDPRINT("digit position reached req_digits, end generate loop"));
  62641. break;
  62642. }
  62643. } else {
  62644. DUK_DDD(DUK_DDDPRINT("fixed format, relative: k=%ld, count=%ld, req=%ld",
  62645. (long) nc_ctx->k, (long) count, (long) nc_ctx->req_digits));
  62646. if (count >= nc_ctx->req_digits) {
  62647. DUK_DDD(DUK_DDDPRINT("digit count reached req_digits, end generate loop"));
  62648. break;
  62649. }
  62650. }
  62651. }
  62652. } /* for */
  62653. nc_ctx->count = count;
  62654. DUK_DDD(DUK_DDDPRINT("generate finished"));
  62655. #ifdef DUK_USE_DDDPRINT
  62656. {
  62657. duk_uint8_t buf[2048];
  62658. duk_small_int_t i, t;
  62659. DUK_MEMZERO(buf, sizeof(buf));
  62660. for (i = 0; i < nc_ctx->count; i++) {
  62661. t = nc_ctx->digits[i];
  62662. if (t < 0 || t > 36) {
  62663. buf[i] = (duk_uint8_t) '?';
  62664. } else {
  62665. buf[i] = (duk_uint8_t) DUK__DIGITCHAR(t);
  62666. }
  62667. }
  62668. DUK_DDD(DUK_DDDPRINT("-> generated digits; k=%ld, digits='%s'",
  62669. (long) nc_ctx->k, (const char *) buf));
  62670. }
  62671. #endif
  62672. }
  62673. /* Round up digits to a given position. If position is out-of-bounds,
  62674. * does nothing. If carry propagates over the first digit, a '1' is
  62675. * prepended to digits and 'k' will be updated. Return value indicates
  62676. * whether carry propagated over the first digit.
  62677. *
  62678. * Note that nc_ctx->count is NOT updated based on the rounding position
  62679. * (it is updated only if carry overflows over the first digit and an
  62680. * extra digit is prepended).
  62681. */
  62682. DUK_LOCAL duk_small_int_t duk__dragon4_fixed_format_round(duk__numconv_stringify_ctx *nc_ctx, duk_small_int_t round_idx) {
  62683. duk_small_int_t t;
  62684. duk_uint8_t *p;
  62685. duk_uint8_t roundup_limit;
  62686. duk_small_int_t ret = 0;
  62687. /*
  62688. * round_idx points to the digit which is considered for rounding; the
  62689. * digit to its left is the final digit of the rounded value. If round_idx
  62690. * is zero, rounding will be performed; the result will either be an empty
  62691. * rounded value or if carry happens a '1' digit is generated.
  62692. */
  62693. if (round_idx >= nc_ctx->count) {
  62694. DUK_DDD(DUK_DDDPRINT("round_idx out of bounds (%ld >= %ld (count)) -> no rounding",
  62695. (long) round_idx, (long) nc_ctx->count));
  62696. return 0;
  62697. } else if (round_idx < 0) {
  62698. DUK_DDD(DUK_DDDPRINT("round_idx out of bounds (%ld < 0) -> no rounding",
  62699. (long) round_idx));
  62700. return 0;
  62701. }
  62702. /*
  62703. * Round-up limit.
  62704. *
  62705. * For even values, divides evenly, e.g. 10 -> roundup_limit=5.
  62706. *
  62707. * For odd values, rounds up, e.g. 3 -> roundup_limit=2.
  62708. * If radix is 3, 0/3 -> down, 1/3 -> down, 2/3 -> up.
  62709. */
  62710. roundup_limit = (duk_uint8_t) ((nc_ctx->B + 1) / 2);
  62711. p = &nc_ctx->digits[round_idx];
  62712. if (*p >= roundup_limit) {
  62713. DUK_DDD(DUK_DDDPRINT("fixed-format rounding carry required"));
  62714. /* carry */
  62715. for (;;) {
  62716. *p = 0;
  62717. if (p == &nc_ctx->digits[0]) {
  62718. DUK_DDD(DUK_DDDPRINT("carry propagated to first digit -> special case handling"));
  62719. DUK_MEMMOVE((void *) (&nc_ctx->digits[1]),
  62720. (void *) (&nc_ctx->digits[0]),
  62721. (size_t) (sizeof(char) * nc_ctx->count));
  62722. nc_ctx->digits[0] = 1; /* don't increase 'count' */
  62723. nc_ctx->k++; /* position of highest digit changed */
  62724. nc_ctx->count++; /* number of digits changed */
  62725. ret = 1;
  62726. break;
  62727. }
  62728. DUK_DDD(DUK_DDDPRINT("fixed-format rounding carry: B=%ld, roundup_limit=%ld, p=%p, digits=%p",
  62729. (long) nc_ctx->B, (long) roundup_limit, (void *) p, (void *) nc_ctx->digits));
  62730. p--;
  62731. t = *p;
  62732. DUK_DDD(DUK_DDDPRINT("digit before carry: %ld", (long) t));
  62733. if (++t < nc_ctx->B) {
  62734. DUK_DDD(DUK_DDDPRINT("rounding carry terminated"));
  62735. *p = t;
  62736. break;
  62737. }
  62738. DUK_DDD(DUK_DDDPRINT("wraps, carry to next digit"));
  62739. }
  62740. }
  62741. return ret;
  62742. }
  62743. #define DUK__NO_EXP (65536) /* arbitrary marker, outside valid exp range */
  62744. DUK_LOCAL void duk__dragon4_convert_and_push(duk__numconv_stringify_ctx *nc_ctx,
  62745. duk_context *ctx,
  62746. duk_small_int_t radix,
  62747. duk_small_int_t digits,
  62748. duk_small_uint_t flags,
  62749. duk_small_int_t neg) {
  62750. duk_small_int_t k;
  62751. duk_small_int_t pos, pos_end;
  62752. duk_small_int_t expt;
  62753. duk_small_int_t dig;
  62754. duk_uint8_t *q;
  62755. duk_uint8_t *buf;
  62756. /*
  62757. * The string conversion here incorporates all the necessary Ecmascript
  62758. * semantics without attempting to be generic. nc_ctx->digits contains
  62759. * nc_ctx->count digits (>= 1), with the topmost digit's 'position'
  62760. * indicated by nc_ctx->k as follows:
  62761. *
  62762. * digits="123" count=3 k=0 --> 0.123
  62763. * digits="123" count=3 k=1 --> 1.23
  62764. * digits="123" count=3 k=5 --> 12300
  62765. * digits="123" count=3 k=-1 --> 0.0123
  62766. *
  62767. * Note that the identifier names used for format selection are different
  62768. * in Burger-Dybvig paper and Ecmascript specification (quite confusingly
  62769. * so, because e.g. 'k' has a totally different meaning in each). See
  62770. * documentation for discussion.
  62771. *
  62772. * Ecmascript doesn't specify any specific behavior for format selection
  62773. * (e.g. when to use exponent notation) for non-base-10 numbers.
  62774. *
  62775. * The bigint space in the context is reused for string output, as there
  62776. * is more than enough space for that (>1kB at the moment), and we avoid
  62777. * allocating even more stack.
  62778. */
  62779. DUK_ASSERT(DUK__NUMCONV_CTX_BIGINTS_SIZE >= DUK__MAX_FORMATTED_LENGTH);
  62780. DUK_ASSERT(nc_ctx->count >= 1);
  62781. k = nc_ctx->k;
  62782. buf = (duk_uint8_t *) &nc_ctx->f; /* XXX: union would be more correct */
  62783. q = buf;
  62784. /* Exponent handling: if exponent format is used, record exponent value and
  62785. * fake k such that one leading digit is generated (e.g. digits=123 -> "1.23").
  62786. *
  62787. * toFixed() prevents exponent use; otherwise apply a set of criteria to
  62788. * match the other API calls (toString(), toPrecision, etc).
  62789. */
  62790. expt = DUK__NO_EXP;
  62791. if (!nc_ctx->abs_pos /* toFixed() */) {
  62792. if ((flags & DUK_N2S_FLAG_FORCE_EXP) || /* exponential notation forced */
  62793. ((flags & DUK_N2S_FLAG_NO_ZERO_PAD) && /* fixed precision and zero padding would be required */
  62794. (k - digits >= 1)) || /* (e.g. k=3, digits=2 -> "12X") */
  62795. ((k > 21 || k <= -6) && (radix == 10))) { /* toString() conditions */
  62796. DUK_DDD(DUK_DDDPRINT("use exponential notation: k=%ld -> expt=%ld",
  62797. (long) k, (long) (k - 1)));
  62798. expt = k - 1; /* e.g. 12.3 -> digits="123" k=2 -> 1.23e1 */
  62799. k = 1; /* generate mantissa with a single leading whole number digit */
  62800. }
  62801. }
  62802. if (neg) {
  62803. *q++ = '-';
  62804. }
  62805. /* Start position (inclusive) and end position (exclusive) */
  62806. pos = (k >= 1 ? k : 1);
  62807. if (nc_ctx->is_fixed) {
  62808. if (nc_ctx->abs_pos) {
  62809. /* toFixed() */
  62810. pos_end = -digits;
  62811. } else {
  62812. pos_end = k - digits;
  62813. }
  62814. } else {
  62815. pos_end = k - nc_ctx->count;
  62816. }
  62817. if (pos_end > 0) {
  62818. pos_end = 0;
  62819. }
  62820. DUK_DDD(DUK_DDDPRINT("expt=%ld, k=%ld, count=%ld, pos=%ld, pos_end=%ld, is_fixed=%ld, "
  62821. "digits=%ld, abs_pos=%ld",
  62822. (long) expt, (long) k, (long) nc_ctx->count, (long) pos, (long) pos_end,
  62823. (long) nc_ctx->is_fixed, (long) digits, (long) nc_ctx->abs_pos));
  62824. /* Digit generation */
  62825. while (pos > pos_end) {
  62826. DUK_DDD(DUK_DDDPRINT("digit generation: pos=%ld, pos_end=%ld",
  62827. (long) pos, (long) pos_end));
  62828. if (pos == 0) {
  62829. *q++ = (duk_uint8_t) '.';
  62830. }
  62831. if (pos > k) {
  62832. *q++ = (duk_uint8_t) '0';
  62833. } else if (pos <= k - nc_ctx->count) {
  62834. *q++ = (duk_uint8_t) '0';
  62835. } else {
  62836. dig = nc_ctx->digits[k - pos];
  62837. DUK_ASSERT(dig >= 0 && dig < nc_ctx->B);
  62838. *q++ = (duk_uint8_t) DUK__DIGITCHAR(dig);
  62839. }
  62840. pos--;
  62841. }
  62842. DUK_ASSERT(pos <= 1);
  62843. /* Exponent */
  62844. if (expt != DUK__NO_EXP) {
  62845. /*
  62846. * Exponent notation for non-base-10 numbers isn't specified in Ecmascript
  62847. * specification, as it never explicitly turns up: non-decimal numbers can
  62848. * only be formatted with Number.prototype.toString([radix]) and for that,
  62849. * behavior is not explicitly specified.
  62850. *
  62851. * Logical choices include formatting the exponent as decimal (e.g. binary
  62852. * 100000 as 1e+5) or in current radix (e.g. binary 100000 as 1e+101).
  62853. * The Dragon4 algorithm (in the original paper) prints the exponent value
  62854. * in the target radix B. However, for radix values 15 and above, the
  62855. * exponent separator 'e' is no longer easily parseable. Consider, for
  62856. * instance, the number "1.faecee+1c".
  62857. */
  62858. duk_size_t len;
  62859. char expt_sign;
  62860. *q++ = 'e';
  62861. if (expt >= 0) {
  62862. expt_sign = '+';
  62863. } else {
  62864. expt_sign = '-';
  62865. expt = -expt;
  62866. }
  62867. *q++ = (duk_uint8_t) expt_sign;
  62868. len = duk__dragon4_format_uint32(q, (duk_uint32_t) expt, radix);
  62869. q += len;
  62870. }
  62871. duk_push_lstring(ctx, (const char *) buf, (size_t) (q - buf));
  62872. }
  62873. /*
  62874. * Conversion helpers
  62875. */
  62876. DUK_LOCAL void duk__dragon4_double_to_ctx(duk__numconv_stringify_ctx *nc_ctx, duk_double_t x) {
  62877. duk_double_union u;
  62878. duk_uint32_t tmp;
  62879. duk_small_int_t expt;
  62880. /*
  62881. * seeeeeee eeeeffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
  62882. * A B C D E F G H
  62883. *
  62884. * s sign bit
  62885. * eee... exponent field
  62886. * fff... fraction
  62887. *
  62888. * ieee value = 1.ffff... * 2^(e - 1023) (normal)
  62889. * = 0.ffff... * 2^(-1022) (denormal)
  62890. *
  62891. * algorithm v = f * b^e
  62892. */
  62893. DUK_DBLUNION_SET_DOUBLE(&u, x);
  62894. nc_ctx->f.n = 2;
  62895. tmp = DUK_DBLUNION_GET_LOW32(&u);
  62896. nc_ctx->f.v[0] = tmp;
  62897. tmp = DUK_DBLUNION_GET_HIGH32(&u);
  62898. nc_ctx->f.v[1] = tmp & 0x000fffffUL;
  62899. expt = (duk_small_int_t) ((tmp >> 20) & 0x07ffUL);
  62900. if (expt == 0) {
  62901. /* denormal */
  62902. expt = DUK__IEEE_DOUBLE_EXP_MIN - 52;
  62903. duk__bi_normalize(&nc_ctx->f);
  62904. } else {
  62905. /* normal: implicit leading 1-bit */
  62906. nc_ctx->f.v[1] |= 0x00100000UL;
  62907. expt = expt - DUK__IEEE_DOUBLE_EXP_BIAS - 52;
  62908. DUK_ASSERT(duk__bi_is_valid(&nc_ctx->f)); /* true, because v[1] has at least one bit set */
  62909. }
  62910. DUK_ASSERT(duk__bi_is_valid(&nc_ctx->f));
  62911. nc_ctx->e = expt;
  62912. }
  62913. DUK_LOCAL void duk__dragon4_ctx_to_double(duk__numconv_stringify_ctx *nc_ctx, duk_double_t *x) {
  62914. duk_double_union u;
  62915. duk_small_int_t expt;
  62916. duk_small_int_t i;
  62917. duk_small_int_t bitstart;
  62918. duk_small_int_t bitround;
  62919. duk_small_int_t bitidx;
  62920. duk_small_int_t skip_round;
  62921. duk_uint32_t t, v;
  62922. DUK_ASSERT(nc_ctx->count == 53 + 1);
  62923. /* Sometimes this assert is not true right now; it will be true after
  62924. * rounding. See: test-bug-numconv-mantissa-assert.js.
  62925. */
  62926. DUK_ASSERT_DISABLE(nc_ctx->digits[0] == 1); /* zero handled by caller */
  62927. /* Should not be required because the code below always sets both high
  62928. * and low parts, but at least gcc-4.4.5 fails to deduce this correctly
  62929. * (perhaps because the low part is set (seemingly) conditionally in a
  62930. * loop), so this is here to avoid the bogus warning.
  62931. */
  62932. DUK_MEMZERO((void *) &u, sizeof(u));
  62933. /*
  62934. * Figure out how generated digits match up with the mantissa,
  62935. * and then perform rounding. If mantissa overflows, need to
  62936. * recompute the exponent (it is bumped and may overflow to
  62937. * infinity).
  62938. *
  62939. * For normal numbers the leading '1' is hidden and ignored,
  62940. * and the last bit is used for rounding:
  62941. *
  62942. * rounding pt
  62943. * <--------52------->|
  62944. * 1 x x x x ... x x x x|y ==> x x x x ... x x x x
  62945. *
  62946. * For denormals, the leading '1' is included in the number,
  62947. * and the rounding point is different:
  62948. *
  62949. * rounding pt
  62950. * <--52 or less--->|
  62951. * 1 x x x x ... x x|x x y ==> 0 0 ... 1 x x ... x x
  62952. *
  62953. * The largest denormals will have a mantissa beginning with
  62954. * a '1' (the explicit leading bit); smaller denormals will
  62955. * have leading zero bits.
  62956. *
  62957. * If the exponent would become too high, the result becomes
  62958. * Infinity. If the exponent is so small that the entire
  62959. * mantissa becomes zero, the result becomes zero.
  62960. *
  62961. * Note: the Dragon4 'k' is off-by-one with respect to the IEEE
  62962. * exponent. For instance, k==0 indicates that the leading '1'
  62963. * digit is at the first binary fraction position (0.1xxx...);
  62964. * the corresponding IEEE exponent would be -1.
  62965. */
  62966. skip_round = 0;
  62967. recheck_exp:
  62968. expt = nc_ctx->k - 1; /* IEEE exp without bias */
  62969. if (expt > 1023) {
  62970. /* Infinity */
  62971. bitstart = -255; /* needed for inf: causes mantissa to become zero,
  62972. * and rounding to be skipped.
  62973. */
  62974. expt = 2047;
  62975. } else if (expt >= -1022) {
  62976. /* normal */
  62977. bitstart = 1; /* skip leading digit */
  62978. expt += DUK__IEEE_DOUBLE_EXP_BIAS;
  62979. DUK_ASSERT(expt >= 1 && expt <= 2046);
  62980. } else {
  62981. /* denormal or zero */
  62982. bitstart = 1023 + expt; /* expt==-1023 -> bitstart=0 (leading 1);
  62983. * expt==-1024 -> bitstart=-1 (one left of leading 1), etc
  62984. */
  62985. expt = 0;
  62986. }
  62987. bitround = bitstart + 52;
  62988. DUK_DDD(DUK_DDDPRINT("ieee expt=%ld, bitstart=%ld, bitround=%ld",
  62989. (long) expt, (long) bitstart, (long) bitround));
  62990. if (!skip_round) {
  62991. if (duk__dragon4_fixed_format_round(nc_ctx, bitround)) {
  62992. /* Corner case: see test-numconv-parse-mant-carry.js. We could
  62993. * just bump the exponent and update bitstart, but it's more robust
  62994. * to recompute (but avoid rounding twice).
  62995. */
  62996. DUK_DDD(DUK_DDDPRINT("rounding caused exponent to be bumped, recheck exponent"));
  62997. skip_round = 1;
  62998. goto recheck_exp;
  62999. }
  63000. }
  63001. /*
  63002. * Create mantissa
  63003. */
  63004. t = 0;
  63005. for (i = 0; i < 52; i++) {
  63006. bitidx = bitstart + 52 - 1 - i;
  63007. if (bitidx >= nc_ctx->count) {
  63008. v = 0;
  63009. } else if (bitidx < 0) {
  63010. v = 0;
  63011. } else {
  63012. v = nc_ctx->digits[bitidx];
  63013. }
  63014. DUK_ASSERT(v == 0 || v == 1);
  63015. t += v << (i % 32);
  63016. if (i == 31) {
  63017. /* low 32 bits is complete */
  63018. DUK_DBLUNION_SET_LOW32(&u, t);
  63019. t = 0;
  63020. }
  63021. }
  63022. /* t has high mantissa */
  63023. DUK_DDD(DUK_DDDPRINT("mantissa is complete: %08lx %08lx",
  63024. (unsigned long) t,
  63025. (unsigned long) DUK_DBLUNION_GET_LOW32(&u)));
  63026. DUK_ASSERT(expt >= 0 && expt <= 0x7ffL);
  63027. t += expt << 20;
  63028. #if 0 /* caller handles sign change */
  63029. if (negative) {
  63030. t |= 0x80000000U;
  63031. }
  63032. #endif
  63033. DUK_DBLUNION_SET_HIGH32(&u, t);
  63034. DUK_DDD(DUK_DDDPRINT("number is complete: %08lx %08lx",
  63035. (unsigned long) DUK_DBLUNION_GET_HIGH32(&u),
  63036. (unsigned long) DUK_DBLUNION_GET_LOW32(&u)));
  63037. *x = DUK_DBLUNION_GET_DOUBLE(&u);
  63038. }
  63039. /*
  63040. * Exposed number-to-string API
  63041. *
  63042. * Input: [ number ]
  63043. * Output: [ string ]
  63044. */
  63045. DUK_INTERNAL void duk_numconv_stringify(duk_context *ctx, duk_small_int_t radix, duk_small_int_t digits, duk_small_uint_t flags) {
  63046. duk_double_t x;
  63047. duk_small_int_t c;
  63048. duk_small_int_t neg;
  63049. duk_uint32_t uval;
  63050. duk__numconv_stringify_ctx nc_ctx_alloc; /* large context; around 2kB now */
  63051. duk__numconv_stringify_ctx *nc_ctx = &nc_ctx_alloc;
  63052. x = (duk_double_t) duk_require_number(ctx, -1);
  63053. duk_pop(ctx);
  63054. /*
  63055. * Handle special cases (NaN, infinity, zero).
  63056. */
  63057. c = (duk_small_int_t) DUK_FPCLASSIFY(x);
  63058. if (DUK_SIGNBIT((double) x)) {
  63059. x = -x;
  63060. neg = 1;
  63061. } else {
  63062. neg = 0;
  63063. }
  63064. /* NaN sign bit is platform specific with unpacked, un-normalized NaNs */
  63065. DUK_ASSERT(c == DUK_FP_NAN || DUK_SIGNBIT((double) x) == 0);
  63066. if (c == DUK_FP_NAN) {
  63067. duk_push_hstring_stridx(ctx, DUK_STRIDX_NAN);
  63068. return;
  63069. } else if (c == DUK_FP_INFINITE) {
  63070. if (neg) {
  63071. /* -Infinity */
  63072. duk_push_hstring_stridx(ctx, DUK_STRIDX_MINUS_INFINITY);
  63073. } else {
  63074. /* Infinity */
  63075. duk_push_hstring_stridx(ctx, DUK_STRIDX_INFINITY);
  63076. }
  63077. return;
  63078. } else if (c == DUK_FP_ZERO) {
  63079. /* We can't shortcut zero here if it goes through special formatting
  63080. * (such as forced exponential notation).
  63081. */
  63082. ;
  63083. }
  63084. /*
  63085. * Handle integers in 32-bit range (that is, [-(2**32-1),2**32-1])
  63086. * specially, as they're very likely for embedded programs. This
  63087. * is now done for all radix values. We must be careful not to use
  63088. * the fast path when special formatting (e.g. forced exponential)
  63089. * is in force.
  63090. *
  63091. * XXX: could save space by supporting radix 10 only and using
  63092. * sprintf "%lu" for the fast path and for exponent formatting.
  63093. */
  63094. uval = (unsigned int) x;
  63095. if (((double) uval) == x && /* integer number in range */
  63096. flags == 0) { /* no special formatting */
  63097. /* use bigint area as a temp */
  63098. duk_uint8_t *buf = (duk_uint8_t *) (&nc_ctx->f);
  63099. duk_uint8_t *p = buf;
  63100. DUK_ASSERT(DUK__NUMCONV_CTX_BIGINTS_SIZE >= 32 + 1); /* max size: radix=2 + sign */
  63101. if (neg && uval != 0) {
  63102. /* no negative sign for zero */
  63103. *p++ = (duk_uint8_t) '-';
  63104. }
  63105. p += duk__dragon4_format_uint32(p, uval, radix);
  63106. duk_push_lstring(ctx, (const char *) buf, (duk_size_t) (p - buf));
  63107. return;
  63108. }
  63109. /*
  63110. * Dragon4 setup.
  63111. *
  63112. * Convert double from IEEE representation for conversion;
  63113. * normal finite values have an implicit leading 1-bit. The
  63114. * slow path algorithm doesn't handle zero, so zero is special
  63115. * cased here but still creates a valid nc_ctx, and goes
  63116. * through normal formatting in case special formatting has
  63117. * been requested (e.g. forced exponential format: 0 -> "0e+0").
  63118. */
  63119. /* Would be nice to bulk clear the allocation, but the context
  63120. * is 1-2 kilobytes and nothing should rely on it being zeroed.
  63121. */
  63122. #if 0
  63123. DUK_MEMZERO((void *) nc_ctx, sizeof(*nc_ctx)); /* slow init, do only for slow path cases */
  63124. #endif
  63125. nc_ctx->is_s2n = 0;
  63126. nc_ctx->b = 2;
  63127. nc_ctx->B = radix;
  63128. nc_ctx->abs_pos = 0;
  63129. if (flags & DUK_N2S_FLAG_FIXED_FORMAT) {
  63130. nc_ctx->is_fixed = 1;
  63131. if (flags & DUK_N2S_FLAG_FRACTION_DIGITS) {
  63132. /* absolute req_digits; e.g. digits = 1 -> last digit is 0,
  63133. * but add an extra digit for rounding.
  63134. */
  63135. nc_ctx->abs_pos = 1;
  63136. nc_ctx->req_digits = (-digits + 1) - 1;
  63137. } else {
  63138. nc_ctx->req_digits = digits + 1;
  63139. }
  63140. } else {
  63141. nc_ctx->is_fixed = 0;
  63142. nc_ctx->req_digits = 0;
  63143. }
  63144. if (c == DUK_FP_ZERO) {
  63145. /* Zero special case: fake requested number of zero digits; ensure
  63146. * no sign bit is printed. Relative and absolute fixed format
  63147. * require separate handling.
  63148. */
  63149. duk_small_int_t count;
  63150. if (nc_ctx->is_fixed) {
  63151. if (nc_ctx->abs_pos) {
  63152. count = digits + 2; /* lead zero + 'digits' fractions + 1 for rounding */
  63153. } else {
  63154. count = digits + 1; /* + 1 for rounding */
  63155. }
  63156. } else {
  63157. count = 1;
  63158. }
  63159. DUK_DDD(DUK_DDDPRINT("count=%ld", (long) count));
  63160. DUK_ASSERT(count >= 1);
  63161. DUK_MEMZERO((void *) nc_ctx->digits, count);
  63162. nc_ctx->count = count;
  63163. nc_ctx->k = 1; /* 0.000... */
  63164. neg = 0;
  63165. goto zero_skip;
  63166. }
  63167. duk__dragon4_double_to_ctx(nc_ctx, x); /* -> sets 'f' and 'e' */
  63168. DUK__BI_PRINT("f", &nc_ctx->f);
  63169. DUK_DDD(DUK_DDDPRINT("e=%ld", (long) nc_ctx->e));
  63170. /*
  63171. * Dragon4 slow path digit generation.
  63172. */
  63173. duk__dragon4_prepare(nc_ctx); /* setup many variables in nc_ctx */
  63174. DUK_DDD(DUK_DDDPRINT("after prepare:"));
  63175. DUK__BI_PRINT("r", &nc_ctx->r);
  63176. DUK__BI_PRINT("s", &nc_ctx->s);
  63177. DUK__BI_PRINT("mp", &nc_ctx->mp);
  63178. DUK__BI_PRINT("mm", &nc_ctx->mm);
  63179. duk__dragon4_scale(nc_ctx);
  63180. DUK_DDD(DUK_DDDPRINT("after scale; k=%ld", (long) nc_ctx->k));
  63181. DUK__BI_PRINT("r", &nc_ctx->r);
  63182. DUK__BI_PRINT("s", &nc_ctx->s);
  63183. DUK__BI_PRINT("mp", &nc_ctx->mp);
  63184. DUK__BI_PRINT("mm", &nc_ctx->mm);
  63185. duk__dragon4_generate(nc_ctx);
  63186. /*
  63187. * Convert and push final string.
  63188. */
  63189. zero_skip:
  63190. if (flags & DUK_N2S_FLAG_FIXED_FORMAT) {
  63191. /* Perform fixed-format rounding. */
  63192. duk_small_int_t roundpos;
  63193. if (flags & DUK_N2S_FLAG_FRACTION_DIGITS) {
  63194. /* 'roundpos' is relative to nc_ctx->k and increases to the right
  63195. * (opposite of how 'k' changes).
  63196. */
  63197. roundpos = -digits; /* absolute position for digit considered for rounding */
  63198. roundpos = nc_ctx->k - roundpos;
  63199. } else {
  63200. roundpos = digits;
  63201. }
  63202. DUK_DDD(DUK_DDDPRINT("rounding: k=%ld, count=%ld, digits=%ld, roundpos=%ld",
  63203. (long) nc_ctx->k, (long) nc_ctx->count, (long) digits, (long) roundpos));
  63204. (void) duk__dragon4_fixed_format_round(nc_ctx, roundpos);
  63205. /* Note: 'count' is currently not adjusted by rounding (i.e. the
  63206. * digits are not "chopped off". That shouldn't matter because
  63207. * the digit position (absolute or relative) is passed on to the
  63208. * convert-and-push function.
  63209. */
  63210. }
  63211. duk__dragon4_convert_and_push(nc_ctx, ctx, radix, digits, flags, neg);
  63212. }
  63213. /*
  63214. * Exposed string-to-number API
  63215. *
  63216. * Input: [ string ]
  63217. * Output: [ number ]
  63218. *
  63219. * If number parsing fails, a NaN is pushed as the result. If number parsing
  63220. * fails due to an internal error, an InternalError is thrown.
  63221. */
  63222. DUK_INTERNAL void duk_numconv_parse(duk_context *ctx, duk_small_int_t radix, duk_small_uint_t flags) {
  63223. duk_hthread *thr = (duk_hthread *) ctx;
  63224. duk__numconv_stringify_ctx nc_ctx_alloc; /* large context; around 2kB now */
  63225. duk__numconv_stringify_ctx *nc_ctx = &nc_ctx_alloc;
  63226. duk_double_t res;
  63227. duk_hstring *h_str;
  63228. duk_small_int_t expt;
  63229. duk_small_int_t expt_neg;
  63230. duk_small_int_t expt_adj;
  63231. duk_small_int_t neg;
  63232. duk_small_int_t dig;
  63233. duk_small_int_t dig_whole;
  63234. duk_small_int_t dig_lzero;
  63235. duk_small_int_t dig_frac;
  63236. duk_small_int_t dig_expt;
  63237. duk_small_int_t dig_prec;
  63238. const duk__exp_limits *explim;
  63239. const duk_uint8_t *p;
  63240. duk_small_int_t ch;
  63241. /* This seems to waste a lot of stack frame entries, but good compilers
  63242. * will compute these as needed below. Some of these initial flags are
  63243. * also modified in the code below, so they can't all be removed.
  63244. */
  63245. duk_small_int_t trim_white = (flags & DUK_S2N_FLAG_TRIM_WHITE);
  63246. duk_small_int_t allow_expt = (flags & DUK_S2N_FLAG_ALLOW_EXP);
  63247. duk_small_int_t allow_garbage = (flags & DUK_S2N_FLAG_ALLOW_GARBAGE);
  63248. duk_small_int_t allow_plus = (flags & DUK_S2N_FLAG_ALLOW_PLUS);
  63249. duk_small_int_t allow_minus = (flags & DUK_S2N_FLAG_ALLOW_MINUS);
  63250. duk_small_int_t allow_infinity = (flags & DUK_S2N_FLAG_ALLOW_INF);
  63251. duk_small_int_t allow_frac = (flags & DUK_S2N_FLAG_ALLOW_FRAC);
  63252. duk_small_int_t allow_naked_frac = (flags & DUK_S2N_FLAG_ALLOW_NAKED_FRAC);
  63253. duk_small_int_t allow_empty_frac = (flags & DUK_S2N_FLAG_ALLOW_EMPTY_FRAC);
  63254. duk_small_int_t allow_empty = (flags & DUK_S2N_FLAG_ALLOW_EMPTY_AS_ZERO);
  63255. duk_small_int_t allow_leading_zero = (flags & DUK_S2N_FLAG_ALLOW_LEADING_ZERO);
  63256. duk_small_int_t allow_auto_hex_int = (flags & DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT);
  63257. duk_small_int_t allow_auto_oct_int = (flags & DUK_S2N_FLAG_ALLOW_AUTO_OCT_INT);
  63258. DUK_DDD(DUK_DDDPRINT("parse number: %!T, radix=%ld, flags=0x%08lx",
  63259. (duk_tval *) duk_get_tval(ctx, -1),
  63260. (long) radix, (unsigned long) flags));
  63261. DUK_ASSERT(radix >= 2 && radix <= 36);
  63262. DUK_ASSERT(radix - 2 < (duk_small_int_t) sizeof(duk__str2num_digits_for_radix));
  63263. /*
  63264. * Preliminaries: trim, sign, Infinity check
  63265. *
  63266. * We rely on the interned string having a NUL terminator, which will
  63267. * cause a parse failure wherever it is encountered. As a result, we
  63268. * don't need separate pointer checks.
  63269. *
  63270. * There is no special parsing for 'NaN' in the specification although
  63271. * 'Infinity' (with an optional sign) is allowed in some contexts.
  63272. * Some contexts allow plus/minus sign, while others only allow the
  63273. * minus sign (like JSON.parse()).
  63274. *
  63275. * Automatic hex number detection (leading '0x' or '0X') and octal
  63276. * number detection (leading '0' followed by at least one octal digit)
  63277. * is done here too.
  63278. */
  63279. if (trim_white) {
  63280. /* Leading / trailing whitespace is sometimes accepted and
  63281. * sometimes not. After white space trimming, all valid input
  63282. * characters are pure ASCII.
  63283. */
  63284. duk_trim(ctx, -1);
  63285. }
  63286. h_str = duk_require_hstring(ctx, -1);
  63287. DUK_ASSERT(h_str != NULL);
  63288. p = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_str);
  63289. neg = 0;
  63290. ch = *p;
  63291. if (ch == (duk_small_int_t) '+') {
  63292. if (!allow_plus) {
  63293. DUK_DDD(DUK_DDDPRINT("parse failed: leading plus sign not allowed"));
  63294. goto parse_fail;
  63295. }
  63296. p++;
  63297. } else if (ch == (duk_small_int_t) '-') {
  63298. if (!allow_minus) {
  63299. DUK_DDD(DUK_DDDPRINT("parse failed: leading minus sign not allowed"));
  63300. goto parse_fail;
  63301. }
  63302. p++;
  63303. neg = 1;
  63304. }
  63305. ch = *p;
  63306. if (allow_infinity && ch == (duk_small_int_t) 'I') {
  63307. /* Don't check for Infinity unless the context allows it.
  63308. * 'Infinity' is a valid integer literal in e.g. base-36:
  63309. *
  63310. * parseInt('Infinity', 36)
  63311. * 1461559270678
  63312. */
  63313. const duk_uint8_t *q;
  63314. /* borrow literal Infinity from builtin string */
  63315. q = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(DUK_HTHREAD_STRING_INFINITY(thr));
  63316. if (DUK_STRNCMP((const char *) p, (const char *) q, 8) == 0) {
  63317. if (!allow_garbage && (p[8] != (duk_uint8_t) 0)) {
  63318. DUK_DDD(DUK_DDDPRINT("parse failed: trailing garbage after matching 'Infinity' not allowed"));
  63319. goto parse_fail;
  63320. } else {
  63321. res = DUK_DOUBLE_INFINITY;
  63322. goto negcheck_and_ret;
  63323. }
  63324. }
  63325. }
  63326. if (ch == (duk_small_int_t) '0') {
  63327. duk_small_int_t detect_radix = 0;
  63328. ch = p[1];
  63329. if (allow_auto_hex_int && (ch == (duk_small_int_t) 'x' || ch == (duk_small_int_t) 'X')) {
  63330. DUK_DDD(DUK_DDDPRINT("detected 0x/0X hex prefix, changing radix and preventing fractions and exponent"));
  63331. detect_radix = 16;
  63332. allow_empty = 0; /* interpret e.g. '0x' and '0xg' as a NaN (= parse error) */
  63333. p += 2;
  63334. } else if (allow_auto_oct_int && (ch >= (duk_small_int_t) '0' && ch <= (duk_small_int_t) '9')) {
  63335. DUK_DDD(DUK_DDDPRINT("detected 0n oct prefix, changing radix and preventing fractions and exponent"));
  63336. detect_radix = 8;
  63337. allow_empty = 1; /* interpret e.g. '09' as '0', not NaN */
  63338. p += 1;
  63339. }
  63340. if (detect_radix > 0) {
  63341. radix = detect_radix;
  63342. allow_expt = 0;
  63343. allow_frac = 0;
  63344. allow_naked_frac = 0;
  63345. allow_empty_frac = 0;
  63346. allow_leading_zero = 1; /* allow e.g. '0x0009' and '00077' */
  63347. }
  63348. }
  63349. /*
  63350. * Scan number and setup for Dragon4.
  63351. *
  63352. * The fast path case is detected during setup: an integer which
  63353. * can be converted without rounding, no net exponent. The fast
  63354. * path could be implemented as a separate scan, but may not really
  63355. * be worth it: the multiplications for building 'f' are not
  63356. * expensive when 'f' is small.
  63357. *
  63358. * The significand ('f') must contain enough bits of (apparent)
  63359. * accuracy, so that Dragon4 will generate enough binary output digits.
  63360. * For decimal numbers, this means generating a 20-digit significand,
  63361. * which should yield enough practical accuracy to parse IEEE doubles.
  63362. * In fact, the Ecmascript specification explicitly allows an
  63363. * implementation to treat digits beyond 20 as zeroes (and even
  63364. * to round the 20th digit upwards). For non-decimal numbers, the
  63365. * appropriate number of digits has been precomputed for comparable
  63366. * accuracy.
  63367. *
  63368. * Digit counts:
  63369. *
  63370. * [ dig_lzero ]
  63371. * |
  63372. * .+-..---[ dig_prec ]----.
  63373. * | || |
  63374. * 0000123.456789012345678901234567890e+123456
  63375. * | | | | | |
  63376. * `--+--' `------[ dig_frac ]-------' `-+--'
  63377. * | |
  63378. * [ dig_whole ] [ dig_expt ]
  63379. *
  63380. * dig_frac and dig_expt are -1 if not present
  63381. * dig_lzero is only computed for whole number part
  63382. *
  63383. * Parsing state
  63384. *
  63385. * Parsing whole part dig_frac < 0 AND dig_expt < 0
  63386. * Parsing fraction part dig_frac >= 0 AND dig_expt < 0
  63387. * Parsing exponent part dig_expt >= 0 (dig_frac may be < 0 or >= 0)
  63388. *
  63389. * Note: in case we hit an implementation limit (like exponent range),
  63390. * we should throw an error, NOT return NaN or Infinity. Even with
  63391. * very large exponent (or significand) values the final result may be
  63392. * finite, so NaN/Infinity would be incorrect.
  63393. */
  63394. duk__bi_set_small(&nc_ctx->f, 0);
  63395. dig_prec = 0;
  63396. dig_lzero = 0;
  63397. dig_whole = 0;
  63398. dig_frac = -1;
  63399. dig_expt = -1;
  63400. expt = 0;
  63401. expt_adj = 0; /* essentially tracks digit position of lowest 'f' digit */
  63402. expt_neg = 0;
  63403. for (;;) {
  63404. ch = *p++;
  63405. DUK_DDD(DUK_DDDPRINT("parse digits: p=%p, ch='%c' (%ld), expt=%ld, expt_adj=%ld, "
  63406. "dig_whole=%ld, dig_frac=%ld, dig_expt=%ld, dig_lzero=%ld, dig_prec=%ld",
  63407. (void *) p, (int) ((ch >= 0x20 && ch <= 0x7e) ? ch : '?'), (long) ch,
  63408. (long) expt, (long) expt_adj, (long) dig_whole, (long) dig_frac,
  63409. (long) dig_expt, (long) dig_lzero, (long) dig_prec));
  63410. DUK__BI_PRINT("f", &nc_ctx->f);
  63411. /* Most common cases first. */
  63412. if (ch >= (duk_small_int_t) '0' && ch <= (duk_small_int_t) '9') {
  63413. dig = (int) ch - '0' + 0;
  63414. } else if (ch == (duk_small_int_t) '.') {
  63415. /* A leading digit is not required in some cases, e.g. accept ".123".
  63416. * In other cases (JSON.parse()) a leading digit is required. This
  63417. * is checked for after the loop.
  63418. */
  63419. if (dig_frac >= 0 || dig_expt >= 0) {
  63420. if (allow_garbage) {
  63421. DUK_DDD(DUK_DDDPRINT("garbage termination (invalid period)"));
  63422. break;
  63423. } else {
  63424. DUK_DDD(DUK_DDDPRINT("parse failed: period not allowed"));
  63425. goto parse_fail;
  63426. }
  63427. }
  63428. if (!allow_frac) {
  63429. /* Some contexts don't allow fractions at all; this can't be a
  63430. * post-check because the state ('f' and expt) would be incorrect.
  63431. */
  63432. if (allow_garbage) {
  63433. DUK_DDD(DUK_DDDPRINT("garbage termination (invalid first period)"));
  63434. break;
  63435. } else {
  63436. DUK_DDD(DUK_DDDPRINT("parse failed: fraction part not allowed"));
  63437. }
  63438. }
  63439. DUK_DDD(DUK_DDDPRINT("start fraction part"));
  63440. dig_frac = 0;
  63441. continue;
  63442. } else if (ch == (duk_small_int_t) 0) {
  63443. DUK_DDD(DUK_DDDPRINT("NUL termination"));
  63444. break;
  63445. } else if (allow_expt && dig_expt < 0 && (ch == (duk_small_int_t) 'e' || ch == (duk_small_int_t) 'E')) {
  63446. /* Note: we don't parse back exponent notation for anything else
  63447. * than radix 10, so this is not an ambiguous check (e.g. hex
  63448. * exponent values may have 'e' either as a significand digit
  63449. * or as an exponent separator).
  63450. *
  63451. * If the exponent separator occurs twice, 'e' will be interpreted
  63452. * as a digit (= 14) and will be rejected as an invalid decimal
  63453. * digit.
  63454. */
  63455. DUK_DDD(DUK_DDDPRINT("start exponent part"));
  63456. /* Exponent without a sign or with a +/- sign is accepted
  63457. * by all call sites (even JSON.parse()).
  63458. */
  63459. ch = *p;
  63460. if (ch == (duk_small_int_t) '-') {
  63461. expt_neg = 1;
  63462. p++;
  63463. } else if (ch == (duk_small_int_t) '+') {
  63464. p++;
  63465. }
  63466. dig_expt = 0;
  63467. continue;
  63468. } else if (ch >= (duk_small_int_t) 'a' && ch <= (duk_small_int_t) 'z') {
  63469. dig = (duk_small_int_t) (ch - (duk_small_int_t) 'a' + 0x0a);
  63470. } else if (ch >= (duk_small_int_t) 'A' && ch <= (duk_small_int_t) 'Z') {
  63471. dig = (duk_small_int_t) (ch - (duk_small_int_t) 'A' + 0x0a);
  63472. } else {
  63473. dig = 255; /* triggers garbage digit check below */
  63474. }
  63475. DUK_ASSERT((dig >= 0 && dig <= 35) || dig == 255);
  63476. if (dig >= radix) {
  63477. if (allow_garbage) {
  63478. DUK_DDD(DUK_DDDPRINT("garbage termination"));
  63479. break;
  63480. } else {
  63481. DUK_DDD(DUK_DDDPRINT("parse failed: trailing garbage or invalid digit"));
  63482. goto parse_fail;
  63483. }
  63484. }
  63485. if (dig_expt < 0) {
  63486. /* whole or fraction digit */
  63487. if (dig_prec < duk__str2num_digits_for_radix[radix - 2]) {
  63488. /* significant from precision perspective */
  63489. duk_small_int_t f_zero = duk__bi_is_zero(&nc_ctx->f);
  63490. if (f_zero && dig == 0) {
  63491. /* Leading zero is not counted towards precision digits; not
  63492. * in the integer part, nor in the fraction part.
  63493. */
  63494. if (dig_frac < 0) {
  63495. dig_lzero++;
  63496. }
  63497. } else {
  63498. /* XXX: join these ops (multiply-accumulate), but only if
  63499. * code footprint decreases.
  63500. */
  63501. duk__bi_mul_small(&nc_ctx->t1, &nc_ctx->f, radix);
  63502. duk__bi_add_small(&nc_ctx->f, &nc_ctx->t1, dig);
  63503. dig_prec++;
  63504. }
  63505. } else {
  63506. /* Ignore digits beyond a radix-specific limit, but note them
  63507. * in expt_adj.
  63508. */
  63509. expt_adj++;
  63510. }
  63511. if (dig_frac >= 0) {
  63512. dig_frac++;
  63513. expt_adj--;
  63514. } else {
  63515. dig_whole++;
  63516. }
  63517. } else {
  63518. /* exponent digit */
  63519. expt = expt * radix + dig;
  63520. if (expt > DUK_S2N_MAX_EXPONENT) {
  63521. /* impose a reasonable exponent limit, so that exp
  63522. * doesn't need to get tracked using a bigint.
  63523. */
  63524. DUK_DDD(DUK_DDDPRINT("parse failed: exponent too large"));
  63525. goto parse_int_error;
  63526. }
  63527. dig_expt++;
  63528. }
  63529. }
  63530. /* Leading zero. */
  63531. if (dig_lzero > 0 && dig_whole > 1) {
  63532. if (!allow_leading_zero) {
  63533. DUK_DDD(DUK_DDDPRINT("parse failed: leading zeroes not allowed in integer part"));
  63534. goto parse_fail;
  63535. }
  63536. }
  63537. /* Validity checks for various fraction formats ("0.1", ".1", "1.", "."). */
  63538. if (dig_whole == 0) {
  63539. if (dig_frac == 0) {
  63540. /* "." is not accepted in any format */
  63541. DUK_DDD(DUK_DDDPRINT("parse failed: plain period without leading or trailing digits"));
  63542. goto parse_fail;
  63543. } else if (dig_frac > 0) {
  63544. /* ".123" */
  63545. if (!allow_naked_frac) {
  63546. DUK_DDD(DUK_DDDPRINT("parse failed: fraction part not allowed without "
  63547. "leading integer digit(s)"));
  63548. goto parse_fail;
  63549. }
  63550. } else {
  63551. /* empty ("") is allowed in some formats (e.g. Number(''), as zero */
  63552. if (!allow_empty) {
  63553. DUK_DDD(DUK_DDDPRINT("parse failed: empty string not allowed (as zero)"));
  63554. goto parse_fail;
  63555. }
  63556. }
  63557. } else {
  63558. if (dig_frac == 0) {
  63559. /* "123." is allowed in some formats */
  63560. if (!allow_empty_frac) {
  63561. DUK_DDD(DUK_DDDPRINT("parse failed: empty fractions"));
  63562. goto parse_fail;
  63563. }
  63564. } else if (dig_frac > 0) {
  63565. /* "123.456" */
  63566. ;
  63567. } else {
  63568. /* "123" */
  63569. ;
  63570. }
  63571. }
  63572. /* Exponent without digits (e.g. "1e" or "1e+"). If trailing garbage is
  63573. * allowed, ignore exponent part as garbage (= parse as "1", i.e. exp 0).
  63574. */
  63575. if (dig_expt == 0) {
  63576. if (!allow_garbage) {
  63577. DUK_DDD(DUK_DDDPRINT("parse failed: empty exponent"));
  63578. goto parse_fail;
  63579. }
  63580. DUK_ASSERT(expt == 0);
  63581. }
  63582. if (expt_neg) {
  63583. expt = -expt;
  63584. }
  63585. DUK_DDD(DUK_DDDPRINT("expt=%ld, expt_adj=%ld, net exponent -> %ld",
  63586. (long) expt, (long) expt_adj, (long) (expt + expt_adj)));
  63587. expt += expt_adj;
  63588. /* Fast path check. */
  63589. if (nc_ctx->f.n <= 1 && /* 32-bit value */
  63590. expt == 0 /* no net exponent */) {
  63591. /* Fast path is triggered for no exponent and also for balanced exponent
  63592. * and fraction parts, e.g. for "1.23e2" == "123". Remember to respect
  63593. * zero sign.
  63594. */
  63595. /* XXX: could accept numbers larger than 32 bits, e.g. up to 53 bits? */
  63596. DUK_DDD(DUK_DDDPRINT("fast path number parse"));
  63597. if (nc_ctx->f.n == 1) {
  63598. res = (double) nc_ctx->f.v[0];
  63599. } else {
  63600. res = 0.0;
  63601. }
  63602. goto negcheck_and_ret;
  63603. }
  63604. /* Significand ('f') padding. */
  63605. while (dig_prec < duk__str2num_digits_for_radix[radix - 2]) {
  63606. /* Pad significand with "virtual" zero digits so that Dragon4 will
  63607. * have enough (apparent) precision to work with.
  63608. */
  63609. DUK_DDD(DUK_DDDPRINT("dig_prec=%ld, pad significand with zero", (long) dig_prec));
  63610. duk__bi_mul_small_copy(&nc_ctx->f, radix, &nc_ctx->t1);
  63611. DUK__BI_PRINT("f", &nc_ctx->f);
  63612. expt--;
  63613. dig_prec++;
  63614. }
  63615. DUK_DDD(DUK_DDDPRINT("final exponent: %ld", (long) expt));
  63616. /* Detect zero special case. */
  63617. if (nc_ctx->f.n == 0) {
  63618. /* This may happen even after the fast path check, if exponent is
  63619. * not balanced (e.g. "0e1"). Remember to respect zero sign.
  63620. */
  63621. DUK_DDD(DUK_DDDPRINT("significand is zero"));
  63622. res = 0.0;
  63623. goto negcheck_and_ret;
  63624. }
  63625. /* Quick reject of too large or too small exponents. This check
  63626. * would be incorrect for zero (e.g. "0e1000" is zero, not Infinity)
  63627. * so zero check must be above.
  63628. */
  63629. explim = &duk__str2num_exp_limits[radix - 2];
  63630. if (expt > explim->upper) {
  63631. DUK_DDD(DUK_DDDPRINT("exponent too large -> infinite"));
  63632. res = (duk_double_t) DUK_DOUBLE_INFINITY;
  63633. goto negcheck_and_ret;
  63634. } else if (expt < explim->lower) {
  63635. DUK_DDD(DUK_DDDPRINT("exponent too small -> zero"));
  63636. res = (duk_double_t) 0.0;
  63637. goto negcheck_and_ret;
  63638. }
  63639. nc_ctx->is_s2n = 1;
  63640. nc_ctx->e = expt;
  63641. nc_ctx->b = radix;
  63642. nc_ctx->B = 2;
  63643. nc_ctx->is_fixed = 1;
  63644. nc_ctx->abs_pos = 0;
  63645. nc_ctx->req_digits = 53 + 1;
  63646. DUK__BI_PRINT("f", &nc_ctx->f);
  63647. DUK_DDD(DUK_DDDPRINT("e=%ld", (long) nc_ctx->e));
  63648. /*
  63649. * Dragon4 slow path (binary) digit generation.
  63650. * An extra digit is generated for rounding.
  63651. */
  63652. duk__dragon4_prepare(nc_ctx); /* setup many variables in nc_ctx */
  63653. DUK_DDD(DUK_DDDPRINT("after prepare:"));
  63654. DUK__BI_PRINT("r", &nc_ctx->r);
  63655. DUK__BI_PRINT("s", &nc_ctx->s);
  63656. DUK__BI_PRINT("mp", &nc_ctx->mp);
  63657. DUK__BI_PRINT("mm", &nc_ctx->mm);
  63658. duk__dragon4_scale(nc_ctx);
  63659. DUK_DDD(DUK_DDDPRINT("after scale; k=%ld", (long) nc_ctx->k));
  63660. DUK__BI_PRINT("r", &nc_ctx->r);
  63661. DUK__BI_PRINT("s", &nc_ctx->s);
  63662. DUK__BI_PRINT("mp", &nc_ctx->mp);
  63663. DUK__BI_PRINT("mm", &nc_ctx->mm);
  63664. duk__dragon4_generate(nc_ctx);
  63665. DUK_ASSERT(nc_ctx->count == 53 + 1);
  63666. /*
  63667. * Convert binary digits into an IEEE double. Need to handle
  63668. * denormals and rounding correctly.
  63669. */
  63670. duk__dragon4_ctx_to_double(nc_ctx, &res);
  63671. goto negcheck_and_ret;
  63672. negcheck_and_ret:
  63673. if (neg) {
  63674. res = -res;
  63675. }
  63676. duk_pop(ctx);
  63677. duk_push_number(ctx, (double) res);
  63678. DUK_DDD(DUK_DDDPRINT("result: %!T", (duk_tval *) duk_get_tval(ctx, -1)));
  63679. return;
  63680. parse_fail:
  63681. DUK_DDD(DUK_DDDPRINT("parse failed"));
  63682. duk_pop(ctx);
  63683. duk_push_nan(ctx);
  63684. return;
  63685. parse_int_error:
  63686. DUK_DDD(DUK_DDDPRINT("parse failed, internal error, can't return a value"));
  63687. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, "number parse error");
  63688. return;
  63689. }
  63690. #line 1 "duk_regexp_compiler.c"
  63691. /*
  63692. * Regexp compilation.
  63693. *
  63694. * See doc/regexp.txt for a discussion of the compilation approach and
  63695. * current limitations.
  63696. *
  63697. * Regexp bytecode assumes jumps can be expressed with signed 32-bit
  63698. * integers. Consequently the bytecode size must not exceed 0x7fffffffL.
  63699. * The implementation casts duk_size_t (buffer size) to duk_(u)int32_t
  63700. * in many places. Although this could be changed, the bytecode format
  63701. * limit would still prevent regexps exceeding the signed 32-bit limit
  63702. * from working.
  63703. *
  63704. * XXX: The implementation does not prevent bytecode from exceeding the
  63705. * maximum supported size. This could be done by limiting the maximum
  63706. * input string size (assuming an upper bound can be computed for number
  63707. * of bytecode bytes emitted per input byte) or checking buffer maximum
  63708. * size when emitting bytecode (slower).
  63709. */
  63710. /* include removed: duk_internal.h */
  63711. #ifdef DUK_USE_REGEXP_SUPPORT
  63712. /*
  63713. * Helper macros
  63714. */
  63715. #ifdef DUK__BUFLEN
  63716. #undef DUK__BUFLEN
  63717. #endif
  63718. #define DUK__BUFLEN(re_ctx) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) re_ctx->buf)
  63719. /*
  63720. * Disjunction struct: result of parsing a disjunction
  63721. */
  63722. typedef struct {
  63723. /* Number of characters that the atom matches (e.g. 3 for 'abc'),
  63724. * -1 if atom is complex and number of matched characters either
  63725. * varies or is not known.
  63726. */
  63727. duk_int32_t charlen;
  63728. #if 0
  63729. /* These are not needed to implement quantifier capture handling,
  63730. * but might be needed at some point.
  63731. */
  63732. /* re_ctx->captures at start and end of atom parsing.
  63733. * Since 'captures' indicates highest capture number emitted
  63734. * so far in a DUK_REOP_SAVE, the captures numbers saved by
  63735. * the atom are: ]start_captures,end_captures].
  63736. */
  63737. duk_uint32_t start_captures;
  63738. duk_uint32_t end_captures;
  63739. #endif
  63740. } duk__re_disjunction_info;
  63741. /*
  63742. * Encoding helpers
  63743. *
  63744. * Some of the typing is bytecode based, e.g. slice sizes are unsigned 32-bit
  63745. * even though the buffer operations will use duk_size_t.
  63746. */
  63747. /* XXX: the insert helpers should ensure that the bytecode result is not
  63748. * larger than expected (or at least assert for it). Many things in the
  63749. * bytecode, like skip offsets, won't work correctly if the bytecode is
  63750. * larger than say 2G.
  63751. */
  63752. DUK_LOCAL duk_uint32_t duk__encode_i32(duk_int32_t x) {
  63753. if (x < 0) {
  63754. return ((duk_uint32_t) (-x)) * 2 + 1;
  63755. } else {
  63756. return ((duk_uint32_t) x) * 2;
  63757. }
  63758. }
  63759. /* XXX: return type should probably be duk_size_t, or explicit checks are needed for
  63760. * maximum size.
  63761. */
  63762. DUK_LOCAL duk_uint32_t duk__insert_u32(duk_re_compiler_ctx *re_ctx, duk_uint32_t offset, duk_uint32_t x) {
  63763. return (duk_uint32_t) duk_hbuffer_insert_xutf8(re_ctx->thr, re_ctx->buf, offset, x);
  63764. }
  63765. DUK_LOCAL duk_uint32_t duk__append_u32(duk_re_compiler_ctx *re_ctx, duk_uint32_t x) {
  63766. return (duk_uint32_t) duk_hbuffer_append_xutf8(re_ctx->thr, re_ctx->buf, x);
  63767. }
  63768. DUK_LOCAL duk_uint32_t duk__insert_i32(duk_re_compiler_ctx *re_ctx, duk_uint32_t offset, duk_int32_t x) {
  63769. return (duk_uint32_t) duk_hbuffer_insert_xutf8(re_ctx->thr, re_ctx->buf, offset, duk__encode_i32(x));
  63770. }
  63771. #if 0 /* unused */
  63772. DUK_LOCAL duk_uint32_t duk__append_i32(duk_re_compiler_ctx *re_ctx, duk_int32_t x) {
  63773. return duk_hbuffer_append_xutf8(re_ctx->thr, re_ctx->buf, duk__encode_i32(x));
  63774. }
  63775. #endif
  63776. /* special helper for emitting u16 lists (used for character ranges for built-in char classes) */
  63777. DUK_LOCAL void duk__append_u16_list(duk_re_compiler_ctx *re_ctx, duk_uint16_t *values, duk_uint32_t count) {
  63778. /* Call sites don't need the result length so it's not accumulated. */
  63779. while (count > 0) {
  63780. (void) duk__append_u32(re_ctx, (duk_uint32_t) (*values++));
  63781. count--;
  63782. }
  63783. }
  63784. 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) {
  63785. duk_hbuffer_insert_slice(re_ctx->thr, re_ctx->buf, offset, data_offset, (duk_size_t) data_length);
  63786. }
  63787. DUK_LOCAL void duk__append_slice(duk_re_compiler_ctx *re_ctx, duk_uint32_t data_offset, duk_uint32_t data_length) {
  63788. duk_hbuffer_append_slice(re_ctx->thr, re_ctx->buf, data_offset, (duk_size_t) data_length);
  63789. }
  63790. DUK_LOCAL void duk__remove_slice(duk_re_compiler_ctx *re_ctx, duk_uint32_t offset, duk_uint32_t length) {
  63791. duk_hbuffer_remove_slice(re_ctx->thr, re_ctx->buf, offset, (duk_size_t) length);
  63792. }
  63793. /*
  63794. * Insert a jump offset at 'offset' to complete an instruction
  63795. * (the jump offset is always the last component of an instruction).
  63796. * The 'skip' argument must be computed relative to 'offset',
  63797. * -without- taking into account the skip field being inserted.
  63798. *
  63799. * ... A B C ins X Y Z ... (ins may be a JUMP, SPLIT1/SPLIT2, etc)
  63800. * => ... A B C ins SKIP X Y Z
  63801. *
  63802. * Computing the final (adjusted) skip value, which is relative to the
  63803. * first byte of the next instruction, is a bit tricky because of the
  63804. * variable length UTF-8 encoding. See doc/regexp.txt for discussion.
  63805. */
  63806. DUK_LOCAL duk_uint32_t duk__insert_jump_offset(duk_re_compiler_ctx *re_ctx, duk_uint32_t offset, duk_int32_t skip) {
  63807. duk_small_int_t len;
  63808. /* XXX: solve into closed form (smaller code) */
  63809. if (skip < 0) {
  63810. /* two encoding attempts suffices */
  63811. len = duk_unicode_get_xutf8_length((duk_codepoint_t) duk__encode_i32(skip));
  63812. len = duk_unicode_get_xutf8_length((duk_codepoint_t) duk__encode_i32(skip - (duk_int32_t) len));
  63813. DUK_ASSERT(duk_unicode_get_xutf8_length(duk__encode_i32(skip - (duk_int32_t) len)) == len); /* no change */
  63814. skip -= (duk_int32_t) len;
  63815. }
  63816. return duk__insert_i32(re_ctx, offset, skip);
  63817. }
  63818. DUK_LOCAL duk_uint32_t duk__append_jump_offset(duk_re_compiler_ctx *re_ctx, duk_int32_t skip) {
  63819. return (duk_uint32_t) duk__insert_jump_offset(re_ctx, (duk_uint32_t) DUK__BUFLEN(re_ctx), skip);
  63820. }
  63821. /*
  63822. * duk_re_range_callback for generating character class ranges.
  63823. *
  63824. * When ignoreCase is false, the range is simply emitted as is.
  63825. * We don't, for instance, eliminate duplicates or overlapping
  63826. * ranges in a character class.
  63827. *
  63828. * When ignoreCase is true, the range needs to be normalized through
  63829. * canonicalization. Unfortunately a canonicalized version of a
  63830. * continuous range is not necessarily continuous (e.g. [x-{] is
  63831. * continuous but [X-{] is not). The current algorithm creates the
  63832. * canonicalized range(s) space efficiently at the cost of compile
  63833. * time execution time (see doc/regexp.txt for discussion).
  63834. *
  63835. * Note that the ctx->nranges is a context-wide temporary value
  63836. * (this is OK because there cannot be multiple character classes
  63837. * being parsed simultaneously).
  63838. */
  63839. DUK_LOCAL void duk__generate_ranges(void *userdata, duk_codepoint_t r1, duk_codepoint_t r2, duk_bool_t direct) {
  63840. duk_re_compiler_ctx *re_ctx = (duk_re_compiler_ctx *) userdata;
  63841. DUK_DD(DUK_DDPRINT("duk__generate_ranges(): re_ctx=%p, range=[%ld,%ld] direct=%ld",
  63842. (void *) re_ctx, (long) r1, (long) r2, (long) direct));
  63843. if (!direct && (re_ctx->re_flags & DUK_RE_FLAG_IGNORE_CASE)) {
  63844. /*
  63845. * Canonicalize a range, generating result ranges as necessary.
  63846. * Needs to exhaustively scan the entire range (at most 65536
  63847. * code points). If 'direct' is set, caller (lexer) has ensured
  63848. * that the range is already canonicalization compatible (this
  63849. * is used to avoid unnecessary canonicalization of built-in
  63850. * ranges like \W, which are not affected by canonicalization).
  63851. *
  63852. * NOTE: here is one place where we don't want to support chars
  63853. * outside the BMP, because the exhaustive search would be
  63854. * massively larger.
  63855. */
  63856. duk_codepoint_t i;
  63857. duk_codepoint_t t;
  63858. duk_codepoint_t r_start, r_end;
  63859. r_start = duk_unicode_re_canonicalize_char(re_ctx->thr, r1);
  63860. r_end = r_start;
  63861. for (i = r1 + 1; i <= r2; i++) {
  63862. t = duk_unicode_re_canonicalize_char(re_ctx->thr, i);
  63863. if (t == r_end + 1) {
  63864. r_end = t;
  63865. } else {
  63866. DUK_DD(DUK_DDPRINT("canonicalized, emit range: [%ld,%ld]", (long) r_start, (long) r_end));
  63867. duk__append_u32(re_ctx, (duk_uint32_t) r_start);
  63868. duk__append_u32(re_ctx, (duk_uint32_t) r_end);
  63869. re_ctx->nranges++;
  63870. r_start = t;
  63871. r_end = t;
  63872. }
  63873. }
  63874. DUK_DD(DUK_DDPRINT("canonicalized, emit range: [%ld,%ld]", (long) r_start, (long) r_end));
  63875. duk__append_u32(re_ctx, (duk_uint32_t) r_start);
  63876. duk__append_u32(re_ctx, (duk_uint32_t) r_end);
  63877. re_ctx->nranges++;
  63878. } else {
  63879. DUK_DD(DUK_DDPRINT("direct, emit range: [%ld,%ld]", (long) r1, (long) r2));
  63880. duk__append_u32(re_ctx, (duk_uint32_t) r1);
  63881. duk__append_u32(re_ctx, (duk_uint32_t) r2);
  63882. re_ctx->nranges++;
  63883. }
  63884. }
  63885. /*
  63886. * Parse regexp Disjunction. Most of regexp compilation happens here.
  63887. *
  63888. * Handles Disjunction, Alternative, and Term productions directly without
  63889. * recursion. The only constructs requiring recursion are positive/negative
  63890. * lookaheads, capturing parentheses, and non-capturing parentheses.
  63891. *
  63892. * The function determines whether the entire disjunction is a 'simple atom'
  63893. * (see doc/regexp.txt discussion on 'simple quantifiers') and if so,
  63894. * returns the atom character length which is needed by the caller to keep
  63895. * track of its own atom character length. A disjunction with more than one
  63896. * alternative is never considered a simple atom (although in some cases
  63897. * that might be the case).
  63898. *
  63899. * Return value: simple atom character length or < 0 if not a simple atom.
  63900. * Appends the bytecode for the disjunction matcher to the end of the temp
  63901. * buffer.
  63902. *
  63903. * Regexp top level structure is:
  63904. *
  63905. * Disjunction = Term*
  63906. * | Term* | Disjunction
  63907. *
  63908. * Term = Assertion
  63909. * | Atom
  63910. * | Atom Quantifier
  63911. *
  63912. * An empty Term sequence is a valid disjunction alternative (e.g. /|||c||/).
  63913. *
  63914. * Notes:
  63915. *
  63916. * * Tracking of the 'simple-ness' of the current atom vs. the entire
  63917. * disjunction are separate matters. For instance, the disjunction
  63918. * may be complex, but individual atoms may be simple. Furthermore,
  63919. * simple quantifiers are used whenever possible, even if the
  63920. * disjunction as a whole is complex.
  63921. *
  63922. * * The estimate of whether an atom is simple is conservative now,
  63923. * and it would be possible to expand it. For instance, captures
  63924. * cause the disjunction to be marked complex, even though captures
  63925. * -can- be handled by simple quantifiers with some minor modifications.
  63926. *
  63927. * * Disjunction 'tainting' as 'complex' is handled at the end of the
  63928. * main for loop collectively for atoms. Assertions, quantifiers,
  63929. * and '|' tokens need to taint the result manually if necessary.
  63930. * Assertions cannot add to result char length, only atoms (and
  63931. * quantifiers) can; currently quantifiers will taint the result
  63932. * as complex though.
  63933. */
  63934. DUK_LOCAL void duk__parse_disjunction(duk_re_compiler_ctx *re_ctx, duk_bool_t expect_eof, duk__re_disjunction_info *out_atom_info) {
  63935. duk_int32_t atom_start_offset = -1; /* negative -> no atom matched on previous round */
  63936. duk_int32_t atom_char_length = 0; /* negative -> complex atom */
  63937. duk_uint32_t atom_start_captures = re_ctx->captures; /* value of re_ctx->captures at start of atom */
  63938. duk_int32_t unpatched_disjunction_split = -1;
  63939. duk_int32_t unpatched_disjunction_jump = -1;
  63940. duk_uint32_t entry_offset = (duk_uint32_t) DUK__BUFLEN(re_ctx);
  63941. duk_int32_t res_charlen = 0; /* -1 if disjunction is complex, char length if simple */
  63942. duk__re_disjunction_info tmp_disj;
  63943. DUK_ASSERT(out_atom_info != NULL);
  63944. if (re_ctx->recursion_depth >= re_ctx->recursion_limit) {
  63945. DUK_ERROR(re_ctx->thr, DUK_ERR_RANGE_ERROR,
  63946. DUK_STR_REGEXP_COMPILER_RECURSION_LIMIT);
  63947. }
  63948. re_ctx->recursion_depth++;
  63949. #if 0
  63950. out_atom_info->start_captures = re_ctx->captures;
  63951. #endif
  63952. for (;;) {
  63953. /* atom_char_length, atom_start_offset, atom_start_offset reflect the
  63954. * atom matched on the previous loop. If a quantifier is encountered
  63955. * on this loop, these are needed to handle the quantifier correctly.
  63956. * new_atom_char_length etc are for the atom parsed on this round;
  63957. * they're written to atom_char_length etc at the end of the round.
  63958. */
  63959. duk_int32_t new_atom_char_length; /* char length of the atom parsed in this loop */
  63960. duk_int32_t new_atom_start_offset; /* bytecode start offset of the atom parsed in this loop
  63961. * (allows quantifiers to copy the atom bytecode)
  63962. */
  63963. duk_uint32_t new_atom_start_captures; /* re_ctx->captures at the start of the atom parsed in this loop */
  63964. duk_lexer_parse_re_token(&re_ctx->lex, &re_ctx->curr_token);
  63965. DUK_DD(DUK_DDPRINT("re token: %ld (num=%ld, char=%c)",
  63966. (long) re_ctx->curr_token.t,
  63967. (long) re_ctx->curr_token.num,
  63968. (re_ctx->curr_token.num >= 0x20 && re_ctx->curr_token.num <= 0x7e) ?
  63969. (int) re_ctx->curr_token.num : (int) '?'));
  63970. /* set by atom case clauses */
  63971. new_atom_start_offset = -1;
  63972. new_atom_char_length = -1;
  63973. new_atom_start_captures = re_ctx->captures;
  63974. switch (re_ctx->curr_token.t) {
  63975. case DUK_RETOK_DISJUNCTION: {
  63976. /*
  63977. * The handling here is a bit tricky. If a previous '|' has been processed,
  63978. * we have a pending split1 and a pending jump (for a previous match). These
  63979. * need to be back-patched carefully. See docs for a detailed example.
  63980. */
  63981. /* patch pending jump and split */
  63982. if (unpatched_disjunction_jump >= 0) {
  63983. duk_uint32_t offset;
  63984. DUK_ASSERT(unpatched_disjunction_split >= 0);
  63985. offset = unpatched_disjunction_jump;
  63986. offset += duk__insert_jump_offset(re_ctx,
  63987. offset,
  63988. (duk_int32_t) (DUK__BUFLEN(re_ctx) - offset));
  63989. /* offset is now target of the pending split (right after jump) */
  63990. duk__insert_jump_offset(re_ctx,
  63991. unpatched_disjunction_split,
  63992. offset - unpatched_disjunction_split);
  63993. }
  63994. /* add a new pending split to the beginning of the entire disjunction */
  63995. (void) duk__insert_u32(re_ctx,
  63996. entry_offset,
  63997. DUK_REOP_SPLIT1); /* prefer direct execution */
  63998. unpatched_disjunction_split = entry_offset + 1; /* +1 for opcode */
  63999. /* add a new pending match jump for latest finished alternative */
  64000. duk__append_u32(re_ctx, DUK_REOP_JUMP);
  64001. unpatched_disjunction_jump = (duk_int32_t) DUK__BUFLEN(re_ctx);
  64002. /* 'taint' result as complex */
  64003. res_charlen = -1;
  64004. break;
  64005. }
  64006. case DUK_RETOK_QUANTIFIER: {
  64007. if (atom_start_offset < 0) {
  64008. DUK_ERROR(re_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64009. DUK_STR_INVALID_QUANTIFIER_NO_ATOM);
  64010. }
  64011. if (re_ctx->curr_token.qmin > re_ctx->curr_token.qmax) {
  64012. DUK_ERROR(re_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64013. DUK_STR_INVALID_QUANTIFIER_VALUES);
  64014. }
  64015. if (atom_char_length >= 0) {
  64016. /*
  64017. * Simple atom
  64018. *
  64019. * If atom_char_length is zero, we'll have unbounded execution time for e.g.
  64020. * /()*x/.exec('x'). We can't just skip the match because it might have some
  64021. * side effects (for instance, if we allowed captures in simple atoms, the
  64022. * capture needs to happen). The simple solution below is to force the
  64023. * quantifier to match at most once, since the additional matches have no effect.
  64024. *
  64025. * With a simple atom there can be no capture groups, so no captures need
  64026. * to be reset.
  64027. */
  64028. duk_int32_t atom_code_length;
  64029. duk_uint32_t offset;
  64030. duk_uint32_t qmin, qmax;
  64031. qmin = re_ctx->curr_token.qmin;
  64032. qmax = re_ctx->curr_token.qmax;
  64033. if (atom_char_length == 0) {
  64034. /* qmin and qmax will be 0 or 1 */
  64035. if (qmin > 1) {
  64036. qmin = 1;
  64037. }
  64038. if (qmax > 1) {
  64039. qmax = 1;
  64040. }
  64041. }
  64042. duk__append_u32(re_ctx, DUK_REOP_MATCH); /* complete 'sub atom' */
  64043. atom_code_length = (duk_int32_t) (DUK__BUFLEN(re_ctx) - atom_start_offset);
  64044. offset = atom_start_offset;
  64045. if (re_ctx->curr_token.greedy) {
  64046. offset += duk__insert_u32(re_ctx, offset, DUK_REOP_SQGREEDY);
  64047. offset += duk__insert_u32(re_ctx, offset, qmin);
  64048. offset += duk__insert_u32(re_ctx, offset, qmax);
  64049. offset += duk__insert_u32(re_ctx, offset, atom_char_length);
  64050. offset += duk__insert_jump_offset(re_ctx, offset, atom_code_length);
  64051. } else {
  64052. offset += duk__insert_u32(re_ctx, offset, DUK_REOP_SQMINIMAL);
  64053. offset += duk__insert_u32(re_ctx, offset, qmin);
  64054. offset += duk__insert_u32(re_ctx, offset, qmax);
  64055. offset += duk__insert_jump_offset(re_ctx, offset, atom_code_length);
  64056. }
  64057. DUK_UNREF(offset); /* silence scan-build warning */
  64058. } else {
  64059. /*
  64060. * Complex atom
  64061. *
  64062. * The original code is used as a template, and removed at the end
  64063. * (this differs from the handling of simple quantifiers).
  64064. *
  64065. * NOTE: there is no current solution for empty atoms in complex
  64066. * quantifiers. This would need some sort of a 'progress' instruction.
  64067. *
  64068. * XXX: impose limit on maximum result size, i.e. atom_code_len * atom_copies?
  64069. */
  64070. duk_int32_t atom_code_length;
  64071. duk_uint32_t atom_copies;
  64072. duk_uint32_t tmp_qmin, tmp_qmax;
  64073. /* pre-check how many atom copies we're willing to make (atom_copies not needed below) */
  64074. atom_copies = (re_ctx->curr_token.qmax == DUK_RE_QUANTIFIER_INFINITE) ?
  64075. re_ctx->curr_token.qmin : re_ctx->curr_token.qmax;
  64076. if (atom_copies > DUK_RE_MAX_ATOM_COPIES) {
  64077. DUK_ERROR(re_ctx->thr, DUK_ERR_RANGE_ERROR,
  64078. DUK_STR_QUANTIFIER_TOO_MANY_COPIES);
  64079. }
  64080. /* wipe the capture range made by the atom (if any) */
  64081. DUK_ASSERT(atom_start_captures <= re_ctx->captures);
  64082. if (atom_start_captures != re_ctx->captures) {
  64083. DUK_ASSERT(atom_start_captures < re_ctx->captures);
  64084. DUK_DDD(DUK_DDDPRINT("must wipe ]atom_start_captures,re_ctx->captures]: ]%ld,%ld]",
  64085. (long) atom_start_captures, (long) re_ctx->captures));
  64086. /* insert (DUK_REOP_WIPERANGE, start, count) in reverse order so the order ends up right */
  64087. duk__insert_u32(re_ctx, atom_start_offset, (re_ctx->captures - atom_start_captures) * 2);
  64088. duk__insert_u32(re_ctx, atom_start_offset, (atom_start_captures + 1) * 2);
  64089. duk__insert_u32(re_ctx, atom_start_offset, DUK_REOP_WIPERANGE);
  64090. } else {
  64091. DUK_DDD(DUK_DDDPRINT("no need to wipe captures: atom_start_captures == re_ctx->captures == %ld",
  64092. (long) atom_start_captures));
  64093. }
  64094. atom_code_length = (duk_int32_t) DUK__BUFLEN(re_ctx) - atom_start_offset;
  64095. /* insert the required matches (qmin) by copying the atom */
  64096. tmp_qmin = re_ctx->curr_token.qmin;
  64097. tmp_qmax = re_ctx->curr_token.qmax;
  64098. while (tmp_qmin > 0) {
  64099. duk__append_slice(re_ctx, atom_start_offset, atom_code_length);
  64100. tmp_qmin--;
  64101. if (tmp_qmax != DUK_RE_QUANTIFIER_INFINITE) {
  64102. tmp_qmax--;
  64103. }
  64104. }
  64105. DUK_ASSERT(tmp_qmin == 0);
  64106. /* insert code for matching the remainder - infinite or finite */
  64107. if (tmp_qmax == DUK_RE_QUANTIFIER_INFINITE) {
  64108. /* reuse last emitted atom for remaining 'infinite' quantifier */
  64109. if (re_ctx->curr_token.qmin == 0) {
  64110. /* Special case: original qmin was zero so there is nothing
  64111. * to repeat. Emit an atom copy but jump over it here.
  64112. */
  64113. duk__append_u32(re_ctx, DUK_REOP_JUMP);
  64114. duk__append_jump_offset(re_ctx, atom_code_length);
  64115. duk__append_slice(re_ctx, atom_start_offset, atom_code_length);
  64116. }
  64117. if (re_ctx->curr_token.greedy) {
  64118. duk__append_u32(re_ctx, DUK_REOP_SPLIT2); /* prefer jump */
  64119. } else {
  64120. duk__append_u32(re_ctx, DUK_REOP_SPLIT1); /* prefer direct */
  64121. }
  64122. duk__append_jump_offset(re_ctx, -atom_code_length - 1); /* -1 for opcode */
  64123. } else {
  64124. /*
  64125. * The remaining matches are emitted as sequence of SPLITs and atom
  64126. * copies; the SPLITs skip the remaining copies and match the sequel.
  64127. * This sequence needs to be emitted starting from the last copy
  64128. * because the SPLITs are variable length due to the variable length
  64129. * skip offset. This causes a lot of memory copying now.
  64130. *
  64131. * Example structure (greedy, match maximum # atoms):
  64132. *
  64133. * SPLIT1 LSEQ
  64134. * (atom)
  64135. * SPLIT1 LSEQ ; <- the byte length of this instruction is needed
  64136. * (atom) ; to encode the above SPLIT1 correctly
  64137. * ...
  64138. * LSEQ:
  64139. */
  64140. duk_uint32_t offset = (duk_uint32_t) DUK__BUFLEN(re_ctx);
  64141. while (tmp_qmax > 0) {
  64142. duk__insert_slice(re_ctx, offset, atom_start_offset, atom_code_length);
  64143. if (re_ctx->curr_token.greedy) {
  64144. duk__insert_u32(re_ctx, offset, DUK_REOP_SPLIT1); /* prefer direct */
  64145. } else {
  64146. duk__insert_u32(re_ctx, offset, DUK_REOP_SPLIT2); /* prefer jump */
  64147. }
  64148. duk__insert_jump_offset(re_ctx,
  64149. offset + 1, /* +1 for opcode */
  64150. (duk_int32_t) (DUK__BUFLEN(re_ctx) - (offset + 1)));
  64151. tmp_qmax--;
  64152. }
  64153. }
  64154. /* remove the original 'template' atom */
  64155. duk__remove_slice(re_ctx, atom_start_offset, atom_code_length);
  64156. }
  64157. /* 'taint' result as complex */
  64158. res_charlen = -1;
  64159. break;
  64160. }
  64161. case DUK_RETOK_ASSERT_START: {
  64162. duk__append_u32(re_ctx, DUK_REOP_ASSERT_START);
  64163. break;
  64164. }
  64165. case DUK_RETOK_ASSERT_END: {
  64166. duk__append_u32(re_ctx, DUK_REOP_ASSERT_END);
  64167. break;
  64168. }
  64169. case DUK_RETOK_ASSERT_WORD_BOUNDARY: {
  64170. duk__append_u32(re_ctx, DUK_REOP_ASSERT_WORD_BOUNDARY);
  64171. break;
  64172. }
  64173. case DUK_RETOK_ASSERT_NOT_WORD_BOUNDARY: {
  64174. duk__append_u32(re_ctx, DUK_REOP_ASSERT_NOT_WORD_BOUNDARY);
  64175. break;
  64176. }
  64177. case DUK_RETOK_ASSERT_START_POS_LOOKAHEAD:
  64178. case DUK_RETOK_ASSERT_START_NEG_LOOKAHEAD: {
  64179. duk_uint32_t offset;
  64180. duk_uint32_t opcode = (re_ctx->curr_token.t == DUK_RETOK_ASSERT_START_POS_LOOKAHEAD) ?
  64181. DUK_REOP_LOOKPOS : DUK_REOP_LOOKNEG;
  64182. offset = (duk_uint32_t) DUK__BUFLEN(re_ctx);
  64183. duk__parse_disjunction(re_ctx, 0, &tmp_disj);
  64184. duk__append_u32(re_ctx, DUK_REOP_MATCH);
  64185. (void) duk__insert_u32(re_ctx, offset, opcode);
  64186. (void) duk__insert_jump_offset(re_ctx,
  64187. offset + 1, /* +1 for opcode */
  64188. (duk_int32_t) (DUK__BUFLEN(re_ctx) - (offset + 1)));
  64189. /* 'taint' result as complex -- this is conservative,
  64190. * as lookaheads do not backtrack.
  64191. */
  64192. res_charlen = -1;
  64193. break;
  64194. }
  64195. case DUK_RETOK_ATOM_PERIOD: {
  64196. new_atom_char_length = 1;
  64197. new_atom_start_offset = (duk_int32_t) DUK__BUFLEN(re_ctx);
  64198. duk__append_u32(re_ctx, DUK_REOP_PERIOD);
  64199. break;
  64200. }
  64201. case DUK_RETOK_ATOM_CHAR: {
  64202. /* Note: successive characters could be joined into string matches
  64203. * but this is not trivial (consider e.g. '/xyz+/); see docs for
  64204. * more discussion.
  64205. */
  64206. duk_uint32_t ch;
  64207. new_atom_char_length = 1;
  64208. new_atom_start_offset = (duk_int32_t) DUK__BUFLEN(re_ctx);
  64209. duk__append_u32(re_ctx, DUK_REOP_CHAR);
  64210. ch = re_ctx->curr_token.num;
  64211. if (re_ctx->re_flags & DUK_RE_FLAG_IGNORE_CASE) {
  64212. ch = duk_unicode_re_canonicalize_char(re_ctx->thr, ch);
  64213. }
  64214. duk__append_u32(re_ctx, ch);
  64215. break;
  64216. }
  64217. case DUK_RETOK_ATOM_DIGIT:
  64218. case DUK_RETOK_ATOM_NOT_DIGIT: {
  64219. new_atom_char_length = 1;
  64220. new_atom_start_offset = (duk_int32_t) DUK__BUFLEN(re_ctx);
  64221. duk__append_u32(re_ctx,
  64222. (re_ctx->curr_token.t == DUK_RETOK_ATOM_DIGIT) ?
  64223. DUK_REOP_RANGES : DUK_REOP_INVRANGES);
  64224. duk__append_u32(re_ctx, sizeof(duk_unicode_re_ranges_digit) / (2 * sizeof(duk_uint16_t)));
  64225. duk__append_u16_list(re_ctx, duk_unicode_re_ranges_digit, sizeof(duk_unicode_re_ranges_digit) / sizeof(duk_uint16_t));
  64226. break;
  64227. }
  64228. case DUK_RETOK_ATOM_WHITE:
  64229. case DUK_RETOK_ATOM_NOT_WHITE: {
  64230. new_atom_char_length = 1;
  64231. new_atom_start_offset = (duk_int32_t) DUK__BUFLEN(re_ctx);
  64232. duk__append_u32(re_ctx,
  64233. (re_ctx->curr_token.t == DUK_RETOK_ATOM_WHITE) ?
  64234. DUK_REOP_RANGES : DUK_REOP_INVRANGES);
  64235. duk__append_u32(re_ctx, sizeof(duk_unicode_re_ranges_white) / (2 * sizeof(duk_uint16_t)));
  64236. duk__append_u16_list(re_ctx, duk_unicode_re_ranges_white, sizeof(duk_unicode_re_ranges_white) / sizeof(duk_uint16_t));
  64237. break;
  64238. }
  64239. case DUK_RETOK_ATOM_WORD_CHAR:
  64240. case DUK_RETOK_ATOM_NOT_WORD_CHAR: {
  64241. new_atom_char_length = 1;
  64242. new_atom_start_offset = (duk_int32_t) DUK__BUFLEN(re_ctx);
  64243. duk__append_u32(re_ctx,
  64244. (re_ctx->curr_token.t == DUK_RETOK_ATOM_WORD_CHAR) ?
  64245. DUK_REOP_RANGES : DUK_REOP_INVRANGES);
  64246. duk__append_u32(re_ctx, sizeof(duk_unicode_re_ranges_wordchar) / (2 * sizeof(duk_uint16_t)));
  64247. duk__append_u16_list(re_ctx, duk_unicode_re_ranges_wordchar, sizeof(duk_unicode_re_ranges_wordchar) / sizeof(duk_uint16_t));
  64248. break;
  64249. }
  64250. case DUK_RETOK_ATOM_BACKREFERENCE: {
  64251. duk_uint32_t backref = (duk_uint32_t) re_ctx->curr_token.num;
  64252. if (backref > re_ctx->highest_backref) {
  64253. re_ctx->highest_backref = backref;
  64254. }
  64255. new_atom_char_length = -1; /* mark as complex */
  64256. new_atom_start_offset = (duk_int32_t) DUK__BUFLEN(re_ctx);
  64257. duk__append_u32(re_ctx, DUK_REOP_BACKREFERENCE);
  64258. duk__append_u32(re_ctx, backref);
  64259. break;
  64260. }
  64261. case DUK_RETOK_ATOM_START_CAPTURE_GROUP: {
  64262. duk_uint32_t cap;
  64263. new_atom_char_length = -1; /* mark as complex (capture handling) */
  64264. new_atom_start_offset = (duk_int32_t) DUK__BUFLEN(re_ctx);
  64265. cap = ++re_ctx->captures;
  64266. duk__append_u32(re_ctx, DUK_REOP_SAVE);
  64267. duk__append_u32(re_ctx, cap * 2);
  64268. duk__parse_disjunction(re_ctx, 0, &tmp_disj); /* retval (sub-atom char length) unused, tainted as complex above */
  64269. duk__append_u32(re_ctx, DUK_REOP_SAVE);
  64270. duk__append_u32(re_ctx, cap * 2 + 1);
  64271. break;
  64272. }
  64273. case DUK_RETOK_ATOM_START_NONCAPTURE_GROUP: {
  64274. new_atom_start_offset = (duk_int32_t) DUK__BUFLEN(re_ctx);
  64275. duk__parse_disjunction(re_ctx, 0, &tmp_disj);
  64276. new_atom_char_length = tmp_disj.charlen;
  64277. break;
  64278. }
  64279. case DUK_RETOK_ATOM_START_CHARCLASS:
  64280. case DUK_RETOK_ATOM_START_CHARCLASS_INVERTED: {
  64281. /*
  64282. * Range parsing is done with a special lexer function which calls
  64283. * us for every range parsed. This is different from how rest of
  64284. * the parsing works, but avoids a heavy, arbitrary size intermediate
  64285. * value type to hold the ranges.
  64286. *
  64287. * Another complication is the handling of character ranges when
  64288. * case insensitive matching is used (see docs for discussion).
  64289. * The range handler callback given to the lexer takes care of this
  64290. * as well.
  64291. *
  64292. * Note that duplicate ranges are not eliminated when parsing character
  64293. * classes, so that canonicalization of
  64294. *
  64295. * [0-9a-fA-Fx-{]
  64296. *
  64297. * creates the result (note the duplicate ranges):
  64298. *
  64299. * [0-9A-FA-FX-Z{-{]
  64300. *
  64301. * where [x-{] is split as a result of canonicalization. The duplicate
  64302. * ranges are not a semantics issue: they work correctly.
  64303. */
  64304. duk_uint32_t offset;
  64305. DUK_DD(DUK_DDPRINT("character class"));
  64306. /* insert ranges instruction, range count patched in later */
  64307. new_atom_char_length = 1;
  64308. new_atom_start_offset = (duk_int32_t) DUK__BUFLEN(re_ctx);
  64309. duk__append_u32(re_ctx,
  64310. (re_ctx->curr_token.t == DUK_RETOK_ATOM_START_CHARCLASS) ?
  64311. DUK_REOP_RANGES : DUK_REOP_INVRANGES);
  64312. offset = (duk_uint32_t) DUK__BUFLEN(re_ctx); /* patch in range count later */
  64313. /* parse ranges until character class ends */
  64314. re_ctx->nranges = 0; /* note: ctx-wide temporary */
  64315. duk_lexer_parse_re_ranges(&re_ctx->lex, duk__generate_ranges, (void *) re_ctx);
  64316. /* insert range count */
  64317. duk__insert_u32(re_ctx, offset, re_ctx->nranges);
  64318. break;
  64319. }
  64320. case DUK_RETOK_ATOM_END_GROUP: {
  64321. if (expect_eof) {
  64322. DUK_ERROR(re_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64323. DUK_STR_UNEXPECTED_CLOSING_PAREN);
  64324. }
  64325. goto done;
  64326. }
  64327. case DUK_RETOK_EOF: {
  64328. if (!expect_eof) {
  64329. DUK_ERROR(re_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64330. DUK_STR_UNEXPECTED_END_OF_PATTERN);
  64331. }
  64332. goto done;
  64333. }
  64334. default: {
  64335. DUK_ERROR(re_ctx->thr, DUK_ERR_SYNTAX_ERROR,
  64336. DUK_STR_UNEXPECTED_REGEXP_TOKEN);
  64337. }
  64338. }
  64339. /* a complex (new) atom taints the result */
  64340. if (new_atom_start_offset >= 0) {
  64341. if (new_atom_char_length < 0) {
  64342. res_charlen = -1;
  64343. } else if (res_charlen >= 0) {
  64344. /* only advance if not tainted */
  64345. res_charlen += new_atom_char_length;
  64346. }
  64347. }
  64348. /* record previous atom info in case next token is a quantifier */
  64349. atom_start_offset = new_atom_start_offset;
  64350. atom_char_length = new_atom_char_length;
  64351. atom_start_captures = new_atom_start_captures;
  64352. }
  64353. done:
  64354. /* finish up pending jump and split for last alternative */
  64355. if (unpatched_disjunction_jump >= 0) {
  64356. duk_uint32_t offset;
  64357. DUK_ASSERT(unpatched_disjunction_split >= 0);
  64358. offset = unpatched_disjunction_jump;
  64359. offset += duk__insert_jump_offset(re_ctx,
  64360. offset,
  64361. (duk_int32_t) (DUK__BUFLEN(re_ctx) - offset));
  64362. /* offset is now target of the pending split (right after jump) */
  64363. duk__insert_jump_offset(re_ctx,
  64364. unpatched_disjunction_split,
  64365. offset - unpatched_disjunction_split);
  64366. }
  64367. #if 0
  64368. out_atom_info->end_captures = re_ctx->captures;
  64369. #endif
  64370. out_atom_info->charlen = res_charlen;
  64371. DUK_DDD(DUK_DDDPRINT("parse disjunction finished: charlen=%ld",
  64372. (long) out_atom_info->charlen));
  64373. re_ctx->recursion_depth--;
  64374. }
  64375. /*
  64376. * Flags parsing (see E5 Section 15.10.4.1).
  64377. */
  64378. DUK_LOCAL duk_uint32_t duk__parse_regexp_flags(duk_hthread *thr, duk_hstring *h) {
  64379. const duk_uint8_t *p;
  64380. const duk_uint8_t *p_end;
  64381. duk_uint32_t flags = 0;
  64382. p = DUK_HSTRING_GET_DATA(h);
  64383. p_end = p + DUK_HSTRING_GET_BYTELEN(h);
  64384. /* Note: can be safely scanned as bytes (undecoded) */
  64385. while (p < p_end) {
  64386. duk_uint8_t c = *p++;
  64387. switch ((int) c) {
  64388. case (int) 'g': {
  64389. if (flags & DUK_RE_FLAG_GLOBAL) {
  64390. goto error;
  64391. }
  64392. flags |= DUK_RE_FLAG_GLOBAL;
  64393. break;
  64394. }
  64395. case (int) 'i': {
  64396. if (flags & DUK_RE_FLAG_IGNORE_CASE) {
  64397. goto error;
  64398. }
  64399. flags |= DUK_RE_FLAG_IGNORE_CASE;
  64400. break;
  64401. }
  64402. case (int) 'm': {
  64403. if (flags & DUK_RE_FLAG_MULTILINE) {
  64404. goto error;
  64405. }
  64406. flags |= DUK_RE_FLAG_MULTILINE;
  64407. break;
  64408. }
  64409. default: {
  64410. goto error;
  64411. }
  64412. }
  64413. }
  64414. return flags;
  64415. error:
  64416. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_REGEXP_FLAGS);
  64417. return 0; /* never here */
  64418. }
  64419. /*
  64420. * Create escaped RegExp source (E5 Section 15.10.3).
  64421. *
  64422. * The current approach is to special case the empty RegExp
  64423. * ('' -> '(?:)') and otherwise replace unescaped '/' characters
  64424. * with '\/' regardless of where they occur in the regexp.
  64425. *
  64426. * Note that normalization does not seem to be necessary for
  64427. * RegExp literals (e.g. '/foo/') because to be acceptable as
  64428. * a RegExp literal, the text between forward slashes must
  64429. * already match the escaping requirements (e.g. must not contain
  64430. * unescaped forward slashes or be empty). Escaping IS needed
  64431. * for expressions like 'new Regexp("...", "")' however.
  64432. * Currently, we re-escape in either case.
  64433. *
  64434. * Also note that we process the source here in UTF-8 encoded
  64435. * form. This is correct, because any non-ASCII characters are
  64436. * passed through without change.
  64437. */
  64438. DUK_LOCAL void duk__create_escaped_source(duk_hthread *thr, int idx_pattern) {
  64439. duk_context *ctx = (duk_context *) thr;
  64440. duk_hstring *h;
  64441. duk_hbuffer_dynamic *buf;
  64442. const duk_uint8_t *p;
  64443. duk_size_t i, n;
  64444. duk_uint_fast8_t c_prev, c;
  64445. h = duk_get_hstring(ctx, idx_pattern);
  64446. DUK_ASSERT(h != NULL);
  64447. p = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h);
  64448. n = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h);
  64449. if (n == 0) {
  64450. /* return '(?:)' */
  64451. duk_push_hstring_stridx(ctx, DUK_STRIDX_ESCAPED_EMPTY_REGEXP);
  64452. return;
  64453. }
  64454. duk_push_dynamic_buffer(ctx, 0);
  64455. buf = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, -1);
  64456. DUK_ASSERT(buf != NULL);
  64457. c_prev = (duk_uint_fast8_t) 0;
  64458. for (i = 0; i < n; i++) {
  64459. c = p[i];
  64460. if (c == (duk_uint_fast8_t) '/' && c_prev != (duk_uint_fast8_t) '\\') {
  64461. /* Unescaped '/' ANYWHERE in the regexp (in disjunction,
  64462. * inside a character class, ...) => same escape works.
  64463. */
  64464. duk_hbuffer_append_byte(thr, buf, (duk_uint8_t) '\\');
  64465. }
  64466. duk_hbuffer_append_byte(thr, buf, (duk_uint8_t) c);
  64467. c_prev = c;
  64468. }
  64469. duk_to_string(ctx, -1); /* -> [ ... escaped_source ] */
  64470. }
  64471. /*
  64472. * Exposed regexp compilation primitive.
  64473. *
  64474. * Sets up a regexp compilation context, and calls duk__parse_disjunction() to do the
  64475. * actual parsing. Handles generation of the compiled regexp header and the
  64476. * "boilerplate" capture of the matching substring (save 0 and 1). Also does some
  64477. * global level regexp checks after recursive compilation has finished.
  64478. *
  64479. * An escaped version of the regexp source, suitable for use as a RegExp instance
  64480. * 'source' property (see E5 Section 15.10.3), is also left on the stack.
  64481. *
  64482. * Input stack: [ pattern flags ]
  64483. * Output stack: [ bytecode escaped_source ] (both as strings)
  64484. */
  64485. DUK_INTERNAL void duk_regexp_compile(duk_hthread *thr) {
  64486. duk_context *ctx = (duk_context *) thr;
  64487. duk_re_compiler_ctx re_ctx;
  64488. duk_lexer_point lex_point;
  64489. duk_hstring *h_pattern;
  64490. duk_hstring *h_flags;
  64491. duk_hbuffer_dynamic *h_buffer;
  64492. duk__re_disjunction_info ign_disj;
  64493. DUK_ASSERT(thr != NULL);
  64494. DUK_ASSERT(ctx != NULL);
  64495. /*
  64496. * Args validation
  64497. */
  64498. /* TypeError if fails */
  64499. h_pattern = duk_require_hstring(ctx, -2);
  64500. h_flags = duk_require_hstring(ctx, -1);
  64501. /*
  64502. * Create normalized 'source' property (E5 Section 15.10.3).
  64503. */
  64504. /* [ ... pattern flags ] */
  64505. duk__create_escaped_source(thr, -2);
  64506. /* [ ... pattern flags escaped_source ] */
  64507. /*
  64508. * Init compilation context
  64509. */
  64510. duk_push_dynamic_buffer(ctx, 0);
  64511. h_buffer = (duk_hbuffer_dynamic *) duk_require_hbuffer(ctx, -1);
  64512. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(h_buffer));
  64513. /* [ ... pattern flags escaped_source buffer ] */
  64514. DUK_MEMZERO(&re_ctx, sizeof(re_ctx));
  64515. DUK_LEXER_INITCTX(&re_ctx.lex); /* duplicate zeroing, expect for (possible) NULL inits */
  64516. re_ctx.thr = thr;
  64517. re_ctx.lex.thr = thr;
  64518. re_ctx.lex.input = DUK_HSTRING_GET_DATA(h_pattern);
  64519. re_ctx.lex.input_length = DUK_HSTRING_GET_BYTELEN(h_pattern);
  64520. re_ctx.lex.token_limit = DUK_RE_COMPILE_TOKEN_LIMIT;
  64521. re_ctx.buf = h_buffer;
  64522. re_ctx.recursion_limit = DUK_RE_COMPILE_RECURSION_LIMIT;
  64523. re_ctx.re_flags = duk__parse_regexp_flags(thr, h_flags);
  64524. DUK_DD(DUK_DDPRINT("regexp compiler ctx initialized, flags=0x%08lx, recursion_limit=%ld",
  64525. (unsigned long) re_ctx.re_flags, (long) re_ctx.recursion_limit));
  64526. /*
  64527. * Init lexer
  64528. */
  64529. lex_point.offset = 0; /* expensive init, just want to fill window */
  64530. lex_point.line = 1;
  64531. DUK_LEXER_SETPOINT(&re_ctx.lex, &lex_point);
  64532. /*
  64533. * Compilation
  64534. */
  64535. DUK_D(DUK_DPRINT("starting regexp compilation"));
  64536. duk__append_u32(&re_ctx, DUK_REOP_SAVE);
  64537. duk__append_u32(&re_ctx, 0);
  64538. duk__parse_disjunction(&re_ctx, 1 /*expect_eof*/, &ign_disj);
  64539. duk__append_u32(&re_ctx, DUK_REOP_SAVE);
  64540. duk__append_u32(&re_ctx, 1);
  64541. duk__append_u32(&re_ctx, DUK_REOP_MATCH);
  64542. DUK_D(DUK_DPRINT("regexp bytecode size (before header) is %ld bytes",
  64543. (long) DUK_HBUFFER_GET_SIZE(re_ctx.buf)));
  64544. /*
  64545. * Check for invalid backreferences; note that it is NOT an error
  64546. * to back-reference a capture group which has not yet been introduced
  64547. * in the pattern (as in /\1(foo)/); in fact, the backreference will
  64548. * always match! It IS an error to back-reference a capture group
  64549. * which will never be introduced in the pattern. Thus, we can check
  64550. * for such references only after parsing is complete.
  64551. */
  64552. if (re_ctx.highest_backref > re_ctx.captures) {
  64553. DUK_ERROR(thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_INVALID_BACKREFS);
  64554. }
  64555. /*
  64556. * Emit compiled regexp header: flags, ncaptures
  64557. * (insertion order inverted on purpose)
  64558. */
  64559. duk__insert_u32(&re_ctx, 0, (re_ctx.captures + 1) * 2);
  64560. duk__insert_u32(&re_ctx, 0, re_ctx.re_flags);
  64561. DUK_D(DUK_DPRINT("regexp bytecode size (after header) is %ld bytes",
  64562. (long) DUK_HBUFFER_GET_SIZE(re_ctx.buf)));
  64563. DUK_DDD(DUK_DDDPRINT("compiled regexp: %!xO", (duk_heaphdr *) re_ctx.buf));
  64564. /* [ ... pattern flags escaped_source buffer ] */
  64565. duk_to_string(ctx, -1); /* coerce to string */
  64566. /* [ ... pattern flags escaped_source bytecode ] */
  64567. /*
  64568. * Finalize stack
  64569. */
  64570. duk_remove(ctx, -4); /* -> [ ... flags escaped_source bytecode ] */
  64571. duk_remove(ctx, -3); /* -> [ ... escaped_source bytecode ] */
  64572. DUK_D(DUK_DPRINT("regexp compilation successful, bytecode: %!T, escaped source: %!T",
  64573. (duk_tval *) duk_get_tval(ctx, -1), (duk_tval *) duk_get_tval(ctx, -2)));
  64574. }
  64575. /*
  64576. * Create a RegExp instance (E5 Section 15.10.7).
  64577. *
  64578. * Note: the output stack left by duk_regexp_compile() is directly compatible
  64579. * with the input here.
  64580. *
  64581. * Input stack: [ escaped_source bytecode ] (both as strings)
  64582. * Output stack: [ RegExp ]
  64583. */
  64584. DUK_INTERNAL void duk_regexp_create_instance(duk_hthread *thr) {
  64585. duk_context *ctx = (duk_context *) thr;
  64586. duk_hobject *h;
  64587. duk_hstring *h_bc;
  64588. duk_small_int_t re_flags;
  64589. /* [ ... escape_source bytecode ] */
  64590. h_bc = duk_get_hstring(ctx, -1);
  64591. DUK_ASSERT(h_bc != NULL);
  64592. DUK_ASSERT(DUK_HSTRING_GET_BYTELEN(h_bc) >= 1); /* always at least the header */
  64593. DUK_ASSERT(DUK_HSTRING_GET_CHARLEN(h_bc) >= 1);
  64594. DUK_ASSERT((duk_small_int_t) DUK_HSTRING_GET_DATA(h_bc)[0] < 0x80); /* flags always encodes to 1 byte */
  64595. re_flags = (duk_small_int_t) DUK_HSTRING_GET_DATA(h_bc)[0];
  64596. /* [ ... escaped_source bytecode ] */
  64597. duk_push_object(ctx);
  64598. h = duk_get_hobject(ctx, -1);
  64599. DUK_ASSERT(h != NULL);
  64600. duk_insert(ctx, -3);
  64601. /* [ ... regexp_object escaped_source bytecode ] */
  64602. DUK_HOBJECT_SET_CLASS_NUMBER(h, DUK_HOBJECT_CLASS_REGEXP);
  64603. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, thr->builtins[DUK_BIDX_REGEXP_PROTOTYPE]);
  64604. duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_INT_BYTECODE, DUK_PROPDESC_FLAGS_NONE);
  64605. /* [ ... regexp_object escaped_source ] */
  64606. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_SOURCE, DUK_PROPDESC_FLAGS_NONE);
  64607. /* [ ... regexp_object ] */
  64608. duk_push_boolean(ctx, (re_flags & DUK_RE_FLAG_GLOBAL));
  64609. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_GLOBAL, DUK_PROPDESC_FLAGS_NONE);
  64610. duk_push_boolean(ctx, (re_flags & DUK_RE_FLAG_IGNORE_CASE));
  64611. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_IGNORE_CASE, DUK_PROPDESC_FLAGS_NONE);
  64612. duk_push_boolean(ctx, (re_flags & DUK_RE_FLAG_MULTILINE));
  64613. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_MULTILINE, DUK_PROPDESC_FLAGS_NONE);
  64614. duk_push_int(ctx, 0);
  64615. duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LAST_INDEX, DUK_PROPDESC_FLAGS_W);
  64616. /* [ ... regexp_object ] */
  64617. }
  64618. #undef DUK__BUFLEN
  64619. #else /* DUK_USE_REGEXP_SUPPORT */
  64620. /* regexp support disabled */
  64621. #endif /* DUK_USE_REGEXP_SUPPORT */
  64622. #line 1 "duk_regexp_executor.c"
  64623. /*
  64624. * Regexp executor.
  64625. *
  64626. * Safety: the Ecmascript executor should prevent user from reading and
  64627. * replacing regexp bytecode. Even so, the executor must validate all
  64628. * memory accesses etc. When an invalid access is detected (e.g. a 'save'
  64629. * opcode to invalid, unallocated index) it should fail with an internal
  64630. * error but not cause a segmentation fault.
  64631. *
  64632. * Notes:
  64633. *
  64634. * - Backtrack counts are limited to unsigned 32 bits but should
  64635. * technically be duk_size_t for strings longer than 4G chars.
  64636. * This also requires a regexp bytecode change.
  64637. */
  64638. /* include removed: duk_internal.h */
  64639. #ifdef DUK_USE_REGEXP_SUPPORT
  64640. /*
  64641. * Helpers for UTF-8 handling
  64642. *
  64643. * For bytecode readers the duk_uint32_t and duk_int32_t types are correct
  64644. * because they're used for more than just codepoints.
  64645. */
  64646. DUK_LOCAL duk_uint32_t duk__bc_get_u32(duk_re_matcher_ctx *re_ctx, const duk_uint8_t **pc) {
  64647. return (duk_uint32_t) duk_unicode_decode_xutf8_checked(re_ctx->thr, pc, re_ctx->bytecode, re_ctx->bytecode_end);
  64648. }
  64649. DUK_LOCAL duk_int32_t duk__bc_get_i32(duk_re_matcher_ctx *re_ctx, const duk_uint8_t **pc) {
  64650. duk_uint32_t t;
  64651. /* signed integer encoding needed to work with UTF-8 */
  64652. t = (duk_uint32_t) duk_unicode_decode_xutf8_checked(re_ctx->thr, pc, re_ctx->bytecode, re_ctx->bytecode_end);
  64653. if (t & 1) {
  64654. return -((duk_int32_t) (t >> 1));
  64655. } else {
  64656. return (duk_int32_t) (t >> 1);
  64657. }
  64658. }
  64659. 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) {
  64660. const duk_uint8_t *p;
  64661. /* Note: allow backtracking from p == ptr_end */
  64662. p = *ptr;
  64663. if (p < ptr_start || p > ptr_end) {
  64664. goto fail;
  64665. }
  64666. while (count > 0) {
  64667. for (;;) {
  64668. p--;
  64669. if (p < ptr_start) {
  64670. goto fail;
  64671. }
  64672. if ((*p & 0xc0) != 0x80) {
  64673. /* utf-8 continuation bytes have the form 10xx xxxx */
  64674. break;
  64675. }
  64676. }
  64677. count--;
  64678. }
  64679. *ptr = p;
  64680. return p;
  64681. fail:
  64682. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_REGEXP_BACKTRACK_FAILED);
  64683. return NULL; /* never here */
  64684. }
  64685. 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) {
  64686. const duk_uint8_t *p;
  64687. p = *ptr;
  64688. if (p < ptr_start || p >= ptr_end) {
  64689. goto fail;
  64690. }
  64691. while (count > 0) {
  64692. for (;;) {
  64693. p++;
  64694. /* Note: if encoding ends by hitting end of input, we don't check that
  64695. * the encoding is valid, we just assume it is.
  64696. */
  64697. if (p >= ptr_end || ((*p & 0xc0) != 0x80)) {
  64698. /* utf-8 continuation bytes have the form 10xx xxxx */
  64699. break;
  64700. }
  64701. }
  64702. count--;
  64703. }
  64704. *ptr = p;
  64705. return p;
  64706. fail:
  64707. DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_REGEXP_ADVANCE_FAILED);
  64708. return NULL; /* never here */
  64709. }
  64710. /*
  64711. * Helpers for dealing with the input string
  64712. */
  64713. /* Get a (possibly canonicalized) input character from current sp. The input
  64714. * itself is never modified, and captures always record non-canonicalized
  64715. * characters even in case-insensitive matching.
  64716. */
  64717. DUK_LOCAL duk_codepoint_t duk__inp_get_cp(duk_re_matcher_ctx *re_ctx, const duk_uint8_t **sp) {
  64718. duk_codepoint_t res = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(re_ctx->thr, sp, re_ctx->input, re_ctx->input_end);
  64719. if (re_ctx->re_flags & DUK_RE_FLAG_IGNORE_CASE) {
  64720. res = duk_unicode_re_canonicalize_char(re_ctx->thr, res);
  64721. }
  64722. return res;
  64723. }
  64724. 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) {
  64725. return duk__utf8_backtrack(re_ctx->thr, sp, re_ctx->input, re_ctx->input_end, count);
  64726. }
  64727. /* Backtrack utf-8 input and return a (possibly canonicalized) input character. */
  64728. DUK_LOCAL duk_codepoint_t duk__inp_get_prev_cp(duk_re_matcher_ctx *re_ctx, const duk_uint8_t *sp) {
  64729. /* note: caller 'sp' is intentionally not updated here */
  64730. (void) duk__inp_backtrack(re_ctx, &sp, (duk_uint_fast32_t) 1);
  64731. return duk__inp_get_cp(re_ctx, &sp);
  64732. }
  64733. /*
  64734. * Regexp recursive matching function.
  64735. *
  64736. * Returns 'sp' on successful match (points to character after last matched one),
  64737. * NULL otherwise.
  64738. *
  64739. * The C recursion depth limit check is only performed in this function, this
  64740. * suffices because the function is present in all true recursion required by
  64741. * regexp execution.
  64742. */
  64743. 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) {
  64744. if (re_ctx->recursion_depth >= re_ctx->recursion_limit) {
  64745. DUK_ERROR(re_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_REGEXP_EXECUTOR_RECURSION_LIMIT);
  64746. }
  64747. re_ctx->recursion_depth++;
  64748. for (;;) {
  64749. duk_small_int_t op;
  64750. if (re_ctx->steps_count >= re_ctx->steps_limit) {
  64751. DUK_ERROR(re_ctx->thr, DUK_ERR_RANGE_ERROR, DUK_STR_REGEXP_EXECUTOR_STEP_LIMIT);
  64752. }
  64753. re_ctx->steps_count++;
  64754. op = (duk_small_int_t) duk__bc_get_u32(re_ctx, &pc);
  64755. DUK_DDD(DUK_DDDPRINT("match: rec=%ld, steps=%ld, pc (after op)=%ld, sp=%ld, op=%ld",
  64756. (long) re_ctx->recursion_depth,
  64757. (long) re_ctx->steps_count,
  64758. (long) (pc - re_ctx->bytecode),
  64759. (long) (sp - re_ctx->input),
  64760. (long) op));
  64761. switch (op) {
  64762. case DUK_REOP_MATCH: {
  64763. goto match;
  64764. }
  64765. case DUK_REOP_CHAR: {
  64766. /*
  64767. * Byte-based matching would be possible for case-sensitive
  64768. * matching but not for case-insensitive matching. So, we
  64769. * match by decoding the input and bytecode character normally.
  64770. *
  64771. * Bytecode characters are assumed to be already canonicalized.
  64772. * Input characters are canonicalized automatically by
  64773. * duk__inp_get_cp() if necessary.
  64774. *
  64775. * There is no opcode for matching multiple characters. The
  64776. * regexp compiler has trouble joining strings efficiently
  64777. * during compilation. See doc/regexp.txt for more discussion.
  64778. */
  64779. duk_codepoint_t c1, c2;
  64780. c1 = (duk_codepoint_t) duk__bc_get_u32(re_ctx, &pc);
  64781. DUK_ASSERT(!(re_ctx->re_flags & DUK_RE_FLAG_IGNORE_CASE) ||
  64782. c1 == duk_unicode_re_canonicalize_char(re_ctx->thr, c1)); /* canonicalized by compiler */
  64783. if (sp >= re_ctx->input_end) {
  64784. goto fail;
  64785. }
  64786. c2 = duk__inp_get_cp(re_ctx, &sp);
  64787. DUK_DDD(DUK_DDDPRINT("char match, c1=%ld, c2=%ld", (long) c1, (long) c2));
  64788. if (c1 != c2) {
  64789. goto fail;
  64790. }
  64791. break;
  64792. }
  64793. case DUK_REOP_PERIOD: {
  64794. duk_codepoint_t c;
  64795. if (sp >= re_ctx->input_end) {
  64796. goto fail;
  64797. }
  64798. c = duk__inp_get_cp(re_ctx, &sp);
  64799. if (duk_unicode_is_line_terminator(c)) {
  64800. /* E5 Sections 15.10.2.8, 7.3 */
  64801. goto fail;
  64802. }
  64803. break;
  64804. }
  64805. case DUK_REOP_RANGES:
  64806. case DUK_REOP_INVRANGES: {
  64807. duk_uint32_t n;
  64808. duk_codepoint_t c;
  64809. duk_small_int_t match;
  64810. n = duk__bc_get_u32(re_ctx, &pc);
  64811. if (sp >= re_ctx->input_end) {
  64812. goto fail;
  64813. }
  64814. c = duk__inp_get_cp(re_ctx, &sp);
  64815. match = 0;
  64816. while (n) {
  64817. duk_codepoint_t r1, r2;
  64818. r1 = (duk_codepoint_t) duk__bc_get_u32(re_ctx, &pc);
  64819. r2 = (duk_codepoint_t) duk__bc_get_u32(re_ctx, &pc);
  64820. DUK_DDD(DUK_DDDPRINT("matching ranges/invranges, n=%ld, r1=%ld, r2=%ld, c=%ld",
  64821. (long) n, (long) r1, (long) r2, (long) c));
  64822. if (c >= r1 && c <= r2) {
  64823. /* Note: don't bail out early, we must read all the ranges from
  64824. * bytecode. Another option is to skip them efficiently after
  64825. * breaking out of here. Prefer smallest code.
  64826. */
  64827. match = 1;
  64828. }
  64829. n--;
  64830. }
  64831. if (op == DUK_REOP_RANGES) {
  64832. if (!match) {
  64833. goto fail;
  64834. }
  64835. } else {
  64836. DUK_ASSERT(op == DUK_REOP_INVRANGES);
  64837. if (match) {
  64838. goto fail;
  64839. }
  64840. }
  64841. break;
  64842. }
  64843. case DUK_REOP_ASSERT_START: {
  64844. duk_codepoint_t c;
  64845. if (sp <= re_ctx->input) {
  64846. break;
  64847. }
  64848. if (!(re_ctx->re_flags & DUK_RE_FLAG_MULTILINE)) {
  64849. goto fail;
  64850. }
  64851. c = duk__inp_get_prev_cp(re_ctx, sp);
  64852. if (duk_unicode_is_line_terminator(c)) {
  64853. /* E5 Sections 15.10.2.8, 7.3 */
  64854. break;
  64855. }
  64856. goto fail;
  64857. }
  64858. case DUK_REOP_ASSERT_END: {
  64859. duk_codepoint_t c;
  64860. const duk_uint8_t *tmp_sp;
  64861. if (sp >= re_ctx->input_end) {
  64862. break;
  64863. }
  64864. if (!(re_ctx->re_flags & DUK_RE_FLAG_MULTILINE)) {
  64865. goto fail;
  64866. }
  64867. tmp_sp = sp;
  64868. c = duk__inp_get_cp(re_ctx, &tmp_sp);
  64869. if (duk_unicode_is_line_terminator(c)) {
  64870. /* E5 Sections 15.10.2.8, 7.3 */
  64871. break;
  64872. }
  64873. goto fail;
  64874. }
  64875. case DUK_REOP_ASSERT_WORD_BOUNDARY:
  64876. case DUK_REOP_ASSERT_NOT_WORD_BOUNDARY: {
  64877. /*
  64878. * E5 Section 15.10.2.6. The previous and current character
  64879. * should -not- be canonicalized as they are now. However,
  64880. * canonicalization does not affect the result of IsWordChar()
  64881. * (which depends on Unicode characters never canonicalizing
  64882. * into ASCII characters) so this does not matter.
  64883. */
  64884. duk_small_int_t w1, w2;
  64885. if (sp <= re_ctx->input) {
  64886. w1 = 0; /* not a wordchar */
  64887. } else {
  64888. duk_codepoint_t c;
  64889. c = duk__inp_get_prev_cp(re_ctx, sp);
  64890. w1 = duk_unicode_re_is_wordchar(c);
  64891. }
  64892. if (sp >= re_ctx->input_end) {
  64893. w2 = 0; /* not a wordchar */
  64894. } else {
  64895. const duk_uint8_t *tmp_sp = sp; /* dummy so sp won't get updated */
  64896. duk_codepoint_t c;
  64897. c = duk__inp_get_cp(re_ctx, &tmp_sp);
  64898. w2 = duk_unicode_re_is_wordchar(c);
  64899. }
  64900. if (op == DUK_REOP_ASSERT_WORD_BOUNDARY) {
  64901. if (w1 == w2) {
  64902. goto fail;
  64903. }
  64904. } else {
  64905. DUK_ASSERT(op == DUK_REOP_ASSERT_NOT_WORD_BOUNDARY);
  64906. if (w1 != w2) {
  64907. goto fail;
  64908. }
  64909. }
  64910. break;
  64911. }
  64912. case DUK_REOP_JUMP: {
  64913. duk_int32_t skip;
  64914. skip = duk__bc_get_i32(re_ctx, &pc);
  64915. pc += skip;
  64916. break;
  64917. }
  64918. case DUK_REOP_SPLIT1: {
  64919. /* split1: prefer direct execution (no jump) */
  64920. const duk_uint8_t *sub_sp;
  64921. duk_int32_t skip;
  64922. skip = duk__bc_get_i32(re_ctx, &pc);
  64923. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  64924. if (sub_sp) {
  64925. sp = sub_sp;
  64926. goto match;
  64927. }
  64928. pc += skip;
  64929. break;
  64930. }
  64931. case DUK_REOP_SPLIT2: {
  64932. /* split2: prefer jump execution (not direct) */
  64933. const duk_uint8_t *sub_sp;
  64934. duk_int32_t skip;
  64935. skip = duk__bc_get_i32(re_ctx, &pc);
  64936. sub_sp = duk__match_regexp(re_ctx, pc + skip, sp);
  64937. if (sub_sp) {
  64938. sp = sub_sp;
  64939. goto match;
  64940. }
  64941. break;
  64942. }
  64943. case DUK_REOP_SQMINIMAL: {
  64944. duk_uint32_t q, qmin, qmax;
  64945. duk_int32_t skip;
  64946. const duk_uint8_t *sub_sp;
  64947. qmin = duk__bc_get_u32(re_ctx, &pc);
  64948. qmax = duk__bc_get_u32(re_ctx, &pc);
  64949. skip = duk__bc_get_i32(re_ctx, &pc);
  64950. DUK_DDD(DUK_DDDPRINT("minimal quantifier, qmin=%lu, qmax=%lu, skip=%ld",
  64951. (unsigned long) qmin, (unsigned long) qmax, (long) skip));
  64952. q = 0;
  64953. while (q <= qmax) {
  64954. if (q >= qmin) {
  64955. sub_sp = duk__match_regexp(re_ctx, pc + skip, sp);
  64956. if (sub_sp) {
  64957. sp = sub_sp;
  64958. goto match;
  64959. }
  64960. }
  64961. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  64962. if (!sub_sp) {
  64963. break;
  64964. }
  64965. sp = sub_sp;
  64966. q++;
  64967. }
  64968. goto fail;
  64969. }
  64970. case DUK_REOP_SQGREEDY: {
  64971. duk_uint32_t q, qmin, qmax, atomlen;
  64972. duk_int32_t skip;
  64973. const duk_uint8_t *sub_sp;
  64974. qmin = duk__bc_get_u32(re_ctx, &pc);
  64975. qmax = duk__bc_get_u32(re_ctx, &pc);
  64976. atomlen = duk__bc_get_u32(re_ctx, &pc);
  64977. skip = duk__bc_get_i32(re_ctx, &pc);
  64978. DUK_DDD(DUK_DDDPRINT("greedy quantifier, qmin=%lu, qmax=%lu, atomlen=%lu, skip=%ld",
  64979. (unsigned long) qmin, (unsigned long) qmax, (unsigned long) atomlen, (long) skip));
  64980. q = 0;
  64981. while (q < qmax) {
  64982. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  64983. if (!sub_sp) {
  64984. break;
  64985. }
  64986. sp = sub_sp;
  64987. q++;
  64988. }
  64989. while (q >= qmin) {
  64990. sub_sp = duk__match_regexp(re_ctx, pc + skip, sp);
  64991. if (sub_sp) {
  64992. sp = sub_sp;
  64993. goto match;
  64994. }
  64995. if (q == qmin) {
  64996. break;
  64997. }
  64998. /* Note: if atom were to contain e.g. captures, we would need to
  64999. * re-match the atom to get correct captures. Simply quantifiers
  65000. * do not allow captures in their atom now, so this is not an issue.
  65001. */
  65002. DUK_DDD(DUK_DDDPRINT("greedy quantifier, backtrack %ld characters (atomlen)",
  65003. (long) atomlen));
  65004. sp = duk__inp_backtrack(re_ctx, &sp, (duk_uint_fast32_t) atomlen);
  65005. q--;
  65006. }
  65007. goto fail;
  65008. }
  65009. case DUK_REOP_SAVE: {
  65010. duk_uint32_t idx;
  65011. const duk_uint8_t *old;
  65012. const duk_uint8_t *sub_sp;
  65013. idx = duk__bc_get_u32(re_ctx, &pc);
  65014. if (idx >= re_ctx->nsaved) {
  65015. /* idx is unsigned, < 0 check is not necessary */
  65016. DUK_D(DUK_DPRINT("internal error, regexp save index insane: idx=%ld", (long) idx));
  65017. goto internal_error;
  65018. }
  65019. old = re_ctx->saved[idx];
  65020. re_ctx->saved[idx] = sp;
  65021. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  65022. if (sub_sp) {
  65023. sp = sub_sp;
  65024. goto match;
  65025. }
  65026. re_ctx->saved[idx] = old;
  65027. goto fail;
  65028. }
  65029. case DUK_REOP_WIPERANGE: {
  65030. /* Wipe capture range and save old values for backtracking.
  65031. *
  65032. * XXX: this typically happens with a relatively small idx_count.
  65033. * It might be useful to handle cases where the count is small
  65034. * (say <= 8) by saving the values in stack instead. This would
  65035. * reduce memory churn and improve performance, at the cost of a
  65036. * slightly higher code footprint.
  65037. */
  65038. duk_uint32_t idx_start, idx_count;
  65039. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  65040. duk_uint32_t idx_end, idx;
  65041. #endif
  65042. duk_uint8_t **range_save;
  65043. const duk_uint8_t *sub_sp;
  65044. idx_start = duk__bc_get_u32(re_ctx, &pc);
  65045. idx_count = duk__bc_get_u32(re_ctx, &pc);
  65046. DUK_DDD(DUK_DDDPRINT("wipe saved range: start=%ld, count=%ld -> [%ld,%ld] (captures [%ld,%ld])",
  65047. (long) idx_start, (long) idx_count,
  65048. (long) idx_start, (long) (idx_start + idx_count - 1),
  65049. (long) (idx_start / 2), (long) ((idx_start + idx_count - 1) / 2)));
  65050. if (idx_start + idx_count > re_ctx->nsaved || idx_count == 0) {
  65051. /* idx is unsigned, < 0 check is not necessary */
  65052. DUK_D(DUK_DPRINT("internal error, regexp wipe indices insane: idx_start=%ld, idx_count=%ld",
  65053. (long) idx_start, (long) idx_count));
  65054. goto internal_error;
  65055. }
  65056. DUK_ASSERT(idx_count > 0);
  65057. duk_require_stack((duk_context *) re_ctx->thr, 1);
  65058. range_save = (duk_uint8_t **) duk_push_fixed_buffer((duk_context *) re_ctx->thr,
  65059. sizeof(duk_uint8_t *) * idx_count);
  65060. DUK_ASSERT(range_save != NULL);
  65061. DUK_MEMCPY(range_save, re_ctx->saved + idx_start, sizeof(duk_uint8_t *) * idx_count);
  65062. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  65063. idx_end = idx_start + idx_count;
  65064. for (idx = idx_start; idx < idx_end; idx++) {
  65065. re_ctx->saved[idx] = NULL;
  65066. }
  65067. #else
  65068. DUK_MEMZERO((void *) (re_ctx->saved + idx_start), sizeof(duk_uint8_t *) * idx_count);
  65069. #endif
  65070. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  65071. if (sub_sp) {
  65072. /* match: keep wiped/resaved values */
  65073. DUK_DDD(DUK_DDDPRINT("match: keep wiped/resaved values [%ld,%ld] (captures [%ld,%ld])",
  65074. (long) idx_start, (long) (idx_start + idx_count - 1),
  65075. (long) (idx_start / 2), (long) ((idx_start + idx_count - 1) / 2)));
  65076. duk_pop((duk_context *) re_ctx->thr);
  65077. sp = sub_sp;
  65078. goto match;
  65079. }
  65080. /* fail: restore saves */
  65081. DUK_DDD(DUK_DDDPRINT("fail: restore wiped/resaved values [%ld,%ld] (captures [%ld,%ld])",
  65082. (long) idx_start, (long) (idx_start + idx_count - 1),
  65083. (long) (idx_start / 2), (long) ((idx_start + idx_count - 1) / 2)));
  65084. DUK_MEMCPY((void *) (re_ctx->saved + idx_start),
  65085. (const void *) range_save,
  65086. sizeof(duk_uint8_t *) * idx_count);
  65087. duk_pop((duk_context *) re_ctx->thr);
  65088. goto fail;
  65089. }
  65090. case DUK_REOP_LOOKPOS:
  65091. case DUK_REOP_LOOKNEG: {
  65092. /*
  65093. * Needs a save of multiple saved[] entries depending on what range
  65094. * may be overwritten. Because the regexp parser does no such analysis,
  65095. * we currently save the entire saved array here. Lookaheads are thus
  65096. * a bit expensive. Note that the saved array is not needed for just
  65097. * the lookahead sub-match, but for the matching of the entire sequel.
  65098. *
  65099. * The temporary save buffer is pushed on to the valstack to handle
  65100. * errors correctly. Each lookahead causes a C recursion and pushes
  65101. * more stuff on the value stack. If the C recursion limit is less
  65102. * than the value stack spare, there is no need to check the stack.
  65103. * We do so regardless, just in case.
  65104. */
  65105. duk_int32_t skip;
  65106. duk_uint8_t **full_save;
  65107. const duk_uint8_t *sub_sp;
  65108. DUK_ASSERT(re_ctx->nsaved > 0);
  65109. duk_require_stack((duk_context *) re_ctx->thr, 1);
  65110. full_save = (duk_uint8_t **) duk_push_fixed_buffer((duk_context *) re_ctx->thr,
  65111. sizeof(duk_uint8_t *) * re_ctx->nsaved);
  65112. DUK_ASSERT(full_save != NULL);
  65113. DUK_MEMCPY(full_save, re_ctx->saved, sizeof(duk_uint8_t *) * re_ctx->nsaved);
  65114. skip = duk__bc_get_i32(re_ctx, &pc);
  65115. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  65116. if (op == DUK_REOP_LOOKPOS) {
  65117. if (!sub_sp) {
  65118. goto lookahead_fail;
  65119. }
  65120. } else {
  65121. if (sub_sp) {
  65122. goto lookahead_fail;
  65123. }
  65124. }
  65125. sub_sp = duk__match_regexp(re_ctx, pc + skip, sp);
  65126. if (sub_sp) {
  65127. /* match: keep saves */
  65128. duk_pop((duk_context *) re_ctx->thr);
  65129. sp = sub_sp;
  65130. goto match;
  65131. }
  65132. /* fall through */
  65133. lookahead_fail:
  65134. /* fail: restore saves */
  65135. DUK_MEMCPY((void *) re_ctx->saved,
  65136. (const void *) full_save,
  65137. sizeof(duk_uint8_t *) * re_ctx->nsaved);
  65138. duk_pop((duk_context *) re_ctx->thr);
  65139. goto fail;
  65140. }
  65141. case DUK_REOP_BACKREFERENCE: {
  65142. /*
  65143. * Byte matching for back-references would be OK in case-
  65144. * sensitive matching. In case-insensitive matching we need
  65145. * to canonicalize characters, so back-reference matching needs
  65146. * to be done with codepoints instead. So, we just decode
  65147. * everything normally here, too.
  65148. *
  65149. * Note: back-reference index which is 0 or higher than
  65150. * NCapturingParens (= number of capturing parens in the
  65151. * -entire- regexp) is a compile time error. However, a
  65152. * backreference referring to a valid capture which has
  65153. * not matched anything always succeeds! See E5 Section
  65154. * 15.10.2.9, step 5, sub-step 3.
  65155. */
  65156. duk_uint32_t idx;
  65157. const duk_uint8_t *p;
  65158. idx = duk__bc_get_u32(re_ctx, &pc);
  65159. idx = idx << 1; /* backref n -> saved indices [n*2, n*2+1] */
  65160. if (idx < 2 || idx + 1 >= re_ctx->nsaved) {
  65161. /* regexp compiler should catch these */
  65162. DUK_D(DUK_DPRINT("internal error, backreference index insane"));
  65163. goto internal_error;
  65164. }
  65165. if (!re_ctx->saved[idx] || !re_ctx->saved[idx+1]) {
  65166. /* capture is 'undefined', always matches! */
  65167. DUK_DDD(DUK_DDDPRINT("backreference: saved[%ld,%ld] not complete, always match",
  65168. (long) idx, (long) (idx + 1)));
  65169. break;
  65170. }
  65171. DUK_DDD(DUK_DDDPRINT("backreference: match saved[%ld,%ld]", (long) idx, (long) (idx + 1)));
  65172. p = re_ctx->saved[idx];
  65173. while (p < re_ctx->saved[idx+1]) {
  65174. duk_codepoint_t c1, c2;
  65175. /* Note: not necessary to check p against re_ctx->input_end:
  65176. * the memory access is checked by duk__inp_get_cp(), while
  65177. * valid compiled regexps cannot write a saved[] entry
  65178. * which points to outside the string.
  65179. */
  65180. if (sp >= re_ctx->input_end) {
  65181. goto fail;
  65182. }
  65183. c1 = duk__inp_get_cp(re_ctx, &p);
  65184. c2 = duk__inp_get_cp(re_ctx, &sp);
  65185. if (c1 != c2) {
  65186. goto fail;
  65187. }
  65188. }
  65189. break;
  65190. }
  65191. default: {
  65192. DUK_D(DUK_DPRINT("internal error, regexp opcode error: %ld", (long) op));
  65193. goto internal_error;
  65194. }
  65195. }
  65196. }
  65197. match:
  65198. re_ctx->recursion_depth--;
  65199. return sp;
  65200. fail:
  65201. re_ctx->recursion_depth--;
  65202. return NULL;
  65203. internal_error:
  65204. DUK_ERROR(re_ctx->thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_REGEXP_INTERNAL_ERROR);
  65205. return NULL; /* never here */
  65206. }
  65207. /*
  65208. * Exposed matcher function which provides the semantics of RegExp.prototype.exec().
  65209. *
  65210. * RegExp.prototype.test() has the same semantics as exec() but does not return the
  65211. * result object (which contains the matching string and capture groups). Currently
  65212. * there is no separate test() helper, so a temporary result object is created and
  65213. * discarded if test() is needed. This is intentional, to save code space.
  65214. *
  65215. * Input stack: [ ... re_obj input ]
  65216. * Output stack: [ ... result ]
  65217. */
  65218. DUK_LOCAL void duk__regexp_match_helper(duk_hthread *thr, duk_small_int_t force_global) {
  65219. duk_context *ctx = (duk_context *) thr;
  65220. duk_re_matcher_ctx re_ctx;
  65221. duk_hobject *h_regexp;
  65222. duk_hstring *h_bytecode;
  65223. duk_hstring *h_input;
  65224. const duk_uint8_t *pc;
  65225. const duk_uint8_t *sp;
  65226. duk_small_int_t match = 0;
  65227. duk_small_int_t global;
  65228. duk_uint_fast32_t i;
  65229. double d;
  65230. duk_uint32_t char_offset;
  65231. DUK_ASSERT(thr != NULL);
  65232. DUK_ASSERT(ctx != NULL);
  65233. DUK_DD(DUK_DDPRINT("regexp match: regexp=%!T, input=%!T",
  65234. (duk_tval *) duk_get_tval(ctx, -2),
  65235. (duk_tval *) duk_get_tval(ctx, -1)));
  65236. /*
  65237. * Regexp instance check, bytecode check, input coercion.
  65238. *
  65239. * See E5 Section 15.10.6.
  65240. */
  65241. /* TypeError if wrong; class check, see E5 Section 15.10.6 */
  65242. h_regexp = duk_require_hobject_with_class(ctx, -2, DUK_HOBJECT_CLASS_REGEXP);
  65243. DUK_ASSERT(h_regexp != NULL);
  65244. DUK_ASSERT(DUK_HOBJECT_GET_CLASS_NUMBER(h_regexp) == DUK_HOBJECT_CLASS_REGEXP);
  65245. DUK_UNREF(h_regexp);
  65246. duk_to_string(ctx, -1);
  65247. h_input = duk_get_hstring(ctx, -1);
  65248. DUK_ASSERT(h_input != NULL);
  65249. duk_get_prop_stridx(ctx, -2, DUK_STRIDX_INT_BYTECODE); /* [ ... re_obj input ] -> [ ... re_obj input bc ] */
  65250. h_bytecode = duk_require_hstring(ctx, -1); /* no regexp instance should exist without a non-configurable bytecode property */
  65251. DUK_ASSERT(h_bytecode != NULL);
  65252. /*
  65253. * Basic context initialization.
  65254. *
  65255. * Some init values are read from the bytecode header
  65256. * whose format is (UTF-8 codepoints):
  65257. *
  65258. * uint flags
  65259. * uint nsaved (even, 2n+2 where n = num captures)
  65260. */
  65261. /* [ ... re_obj input bc ] */
  65262. DUK_MEMZERO(&re_ctx, sizeof(re_ctx));
  65263. re_ctx.thr = thr;
  65264. re_ctx.input = (duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
  65265. re_ctx.input_end = re_ctx.input + DUK_HSTRING_GET_BYTELEN(h_input);
  65266. re_ctx.bytecode = (duk_uint8_t *) DUK_HSTRING_GET_DATA(h_bytecode);
  65267. re_ctx.bytecode_end = re_ctx.bytecode + DUK_HSTRING_GET_BYTELEN(h_bytecode);
  65268. re_ctx.saved = NULL;
  65269. re_ctx.recursion_limit = DUK_RE_EXECUTE_RECURSION_LIMIT;
  65270. re_ctx.steps_limit = DUK_RE_EXECUTE_STEPS_LIMIT;
  65271. /* read header */
  65272. pc = re_ctx.bytecode;
  65273. re_ctx.re_flags = duk__bc_get_u32(&re_ctx, &pc);
  65274. re_ctx.nsaved = duk__bc_get_u32(&re_ctx, &pc);
  65275. re_ctx.bytecode = pc;
  65276. DUK_ASSERT(DUK_RE_FLAG_GLOBAL < 0x10000UL); /* must fit into duk_small_int_t */
  65277. global = (duk_small_int_t) (force_global | (re_ctx.re_flags & DUK_RE_FLAG_GLOBAL));
  65278. DUK_ASSERT(re_ctx.nsaved >= 2);
  65279. DUK_ASSERT((re_ctx.nsaved % 2) == 0);
  65280. duk_push_fixed_buffer(ctx, sizeof(duk_uint8_t *) * re_ctx.nsaved);
  65281. re_ctx.saved = (const duk_uint8_t **) duk_get_buffer(ctx, -1, NULL);
  65282. DUK_ASSERT(re_ctx.saved != NULL);
  65283. /* [ ... re_obj input bc saved_buf ] */
  65284. /* buffer is automatically zeroed */
  65285. #ifdef DUK_USE_EXPLICIT_NULL_INIT
  65286. for (i = 0; i < re_ctx.nsaved; i++) {
  65287. re_ctx.saved[i] = (duk_uint8_t *) NULL;
  65288. }
  65289. #endif
  65290. DUK_DDD(DUK_DDDPRINT("regexp ctx initialized, flags=0x%08lx, nsaved=%ld, recursion_limit=%ld, steps_limit=%ld",
  65291. (unsigned long) re_ctx.re_flags, (long) re_ctx.nsaved, (long) re_ctx.recursion_limit,
  65292. (long) re_ctx.steps_limit));
  65293. /*
  65294. * Get starting character offset for match, and initialize 'sp' based on it.
  65295. *
  65296. * Note: lastIndex is non-configurable so it must be present (we check the
  65297. * internal class of the object above, so we know it is). User code can set
  65298. * its value to an arbitrary (garbage) value though; E5 requires that lastIndex
  65299. * be coerced to a number before using. The code below works even if the
  65300. * property is missing: the value will then be coerced to zero.
  65301. *
  65302. * Note: lastIndex may be outside Uint32 range even after ToInteger() coercion.
  65303. * For instance, ToInteger(+Infinity) = +Infinity. We track the match offset
  65304. * as an integer, but pre-check it to be inside the 32-bit range before the loop.
  65305. * If not, the check in E5 Section 15.10.6.2, step 9.a applies.
  65306. */
  65307. /* XXX: lastIndex handling produces a lot of asm */
  65308. /* [ ... re_obj input bc saved_buf ] */
  65309. duk_get_prop_stridx(ctx, -4, DUK_STRIDX_LAST_INDEX); /* -> [ ... re_obj input bc saved_buf lastIndex ] */
  65310. (void) duk_to_int(ctx, -1); /* ToInteger(lastIndex) */
  65311. d = duk_get_number(ctx, -1); /* integer, but may be +/- Infinite, +/- zero (not NaN, though) */
  65312. duk_pop(ctx);
  65313. if (global) {
  65314. if (d < 0.0 || d > (double) DUK_HSTRING_GET_CHARLEN(h_input)) {
  65315. /* match fail */
  65316. char_offset = 0; /* not really necessary */
  65317. DUK_ASSERT(match == 0);
  65318. goto match_over;
  65319. }
  65320. char_offset = (duk_uint32_t) d;
  65321. } else {
  65322. /* lastIndex must be ignored for non-global regexps, but get the
  65323. * value for (theoretical) side effects. No side effects can
  65324. * really occur, because lastIndex is a normal property and is
  65325. * always non-configurable for RegExp instances.
  65326. */
  65327. char_offset = (duk_uint32_t) 0;
  65328. }
  65329. sp = re_ctx.input + duk_heap_strcache_offset_char2byte(thr, h_input, char_offset);
  65330. /*
  65331. * Match loop.
  65332. *
  65333. * Try matching at different offsets until match found or input exhausted.
  65334. */
  65335. /* [ ... re_obj input bc saved_buf ] */
  65336. DUK_ASSERT(match == 0);
  65337. for (;;) {
  65338. /* char offset in [0, h_input->clen] (both ends inclusive), checked before entry */
  65339. DUK_ASSERT_DISABLE(char_offset >= 0);
  65340. DUK_ASSERT(char_offset <= DUK_HSTRING_GET_CHARLEN(h_input));
  65341. /* Note: ctx.steps is intentionally not reset, it applies to the entire unanchored match */
  65342. DUK_ASSERT(re_ctx.recursion_depth == 0);
  65343. DUK_DDD(DUK_DDDPRINT("attempt match at char offset %ld; %p [%p,%p]",
  65344. (long) char_offset, (void *) sp, (void *) re_ctx.input,
  65345. (void *) re_ctx.input_end));
  65346. /*
  65347. * Note:
  65348. *
  65349. * - duk__match_regexp() is required not to longjmp() in ordinary "non-match"
  65350. * conditions; a longjmp() will terminate the entire matching process.
  65351. *
  65352. * - Clearing saved[] is not necessary because backtracking does it
  65353. *
  65354. * - Backtracking also rewinds ctx.recursion back to zero, unless an
  65355. * internal/limit error occurs (which causes a longjmp())
  65356. *
  65357. * - If we supported anchored matches, we would break out here
  65358. * unconditionally; however, Ecmascript regexps don't have anchored
  65359. * matches. It might make sense to implement a fast bail-out if
  65360. * the regexp begins with '^' and sp is not 0: currently we'll just
  65361. * run through the entire input string, trivially failing the match
  65362. * at every non-zero offset.
  65363. */
  65364. if (duk__match_regexp(&re_ctx, re_ctx.bytecode, sp) != NULL) {
  65365. DUK_DDD(DUK_DDDPRINT("match at offset %ld", (long) char_offset));
  65366. match = 1;
  65367. break;
  65368. }
  65369. /* advance by one character (code point) and one char_offset */
  65370. char_offset++;
  65371. if (char_offset > DUK_HSTRING_GET_CHARLEN(h_input)) {
  65372. /*
  65373. * Note:
  65374. *
  65375. * - Intentionally attempt (empty) match at char_offset == k_input->clen
  65376. *
  65377. * - Negative char_offsets have been eliminated and char_offset is duk_uint32_t
  65378. * -> no need or use for a negative check
  65379. */
  65380. DUK_DDD(DUK_DDDPRINT("no match after trying all sp offsets"));
  65381. break;
  65382. }
  65383. /* avoid calling at end of input, will DUK_ERROR (above check suffices to avoid this) */
  65384. (void) duk__utf8_advance(thr, &sp, re_ctx.input, re_ctx.input_end, (duk_uint_fast32_t) 1);
  65385. }
  65386. match_over:
  65387. /*
  65388. * Matching complete, create result array or return a 'null'. Update lastIndex
  65389. * if necessary. See E5 Section 15.10.6.2.
  65390. *
  65391. * Because lastIndex is a character (not byte) offset, we need the character
  65392. * length of the match which we conveniently get as a side effect of interning
  65393. * the matching substring (0th index of result array).
  65394. *
  65395. * saved[0] start pointer (~ byte offset) of current match
  65396. * saved[1] end pointer (~ byte offset) of current match (exclusive)
  65397. * char_offset start character offset of current match (-> .index of result)
  65398. * char_end_offset end character offset (computed below)
  65399. */
  65400. /* [ ... re_obj input bc saved_buf ] */
  65401. if (match) {
  65402. #ifdef DUK_USE_ASSERTIONS
  65403. duk_hobject *h_res;
  65404. #endif
  65405. duk_uint32_t char_end_offset = 0;
  65406. DUK_DDD(DUK_DDDPRINT("regexp matches at char_offset %ld", (long) char_offset));
  65407. DUK_ASSERT(re_ctx.nsaved >= 2); /* must have start and end */
  65408. DUK_ASSERT((re_ctx.nsaved % 2) == 0); /* and even number */
  65409. /* XXX: Array size is known before and (2 * re_ctx.nsaved) but not taken
  65410. * advantage of now. The array is not compacted either, as regexp match
  65411. * objects are usually short lived.
  65412. */
  65413. duk_push_array(ctx);
  65414. #ifdef DUK_USE_ASSERTIONS
  65415. h_res = duk_require_hobject(ctx, -1);
  65416. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(h_res));
  65417. DUK_ASSERT(DUK_HOBJECT_HAS_EXOTIC_ARRAY(h_res));
  65418. DUK_ASSERT(DUK_HOBJECT_GET_CLASS_NUMBER(h_res) == DUK_HOBJECT_CLASS_ARRAY);
  65419. #endif
  65420. /* [ ... re_obj input bc saved_buf res_obj ] */
  65421. duk_push_u32(ctx, char_offset);
  65422. duk_xdef_prop_stridx_wec(ctx, -2, DUK_STRIDX_INDEX);
  65423. duk_dup(ctx, -4);
  65424. duk_xdef_prop_stridx_wec(ctx, -2, DUK_STRIDX_INPUT);
  65425. for (i = 0; i < re_ctx.nsaved; i += 2) {
  65426. /* Captures which are undefined have NULL pointers and are returned
  65427. * as 'undefined'. The same is done when saved[] pointers are insane
  65428. * (this should, of course, never happen in practice).
  65429. */
  65430. if (re_ctx.saved[i] && re_ctx.saved[i+1] && re_ctx.saved[i+1] >= re_ctx.saved[i]) {
  65431. duk_hstring *h_saved;
  65432. duk_push_lstring(ctx,
  65433. (char *) re_ctx.saved[i],
  65434. (duk_size_t) (re_ctx.saved[i+1] - re_ctx.saved[i]));
  65435. h_saved = duk_get_hstring(ctx, -1);
  65436. DUK_ASSERT(h_saved != NULL);
  65437. if (i == 0) {
  65438. /* Assumes that saved[0] and saved[1] are always
  65439. * set by regexp bytecode (if not, char_end_offset
  65440. * will be zero). Also assumes clen reflects the
  65441. * correct char length.
  65442. */
  65443. char_end_offset = char_offset + DUK_HSTRING_GET_CHARLEN(h_saved);
  65444. }
  65445. } else {
  65446. duk_push_undefined(ctx);
  65447. }
  65448. /* [ ... re_obj input bc saved_buf res_obj val ] */
  65449. duk_put_prop_index(ctx, -2, i / 2);
  65450. }
  65451. /* [ ... re_obj input bc saved_buf res_obj ] */
  65452. /* NB: 'length' property is automatically updated by the array setup loop */
  65453. if (global) {
  65454. /* global regexp: lastIndex updated on match */
  65455. duk_push_u32(ctx, char_end_offset);
  65456. duk_put_prop_stridx(ctx, -6, DUK_STRIDX_LAST_INDEX);
  65457. } else {
  65458. /* non-global regexp: lastIndex never updated on match */
  65459. ;
  65460. }
  65461. } else {
  65462. /*
  65463. * No match, E5 Section 15.10.6.2, step 9.a.i - 9.a.ii apply, regardless
  65464. * of 'global' flag of the RegExp. In particular, if lastIndex is invalid
  65465. * initially, it is reset to zero.
  65466. */
  65467. DUK_DDD(DUK_DDDPRINT("regexp does not match"));
  65468. duk_push_null(ctx);
  65469. /* [ ... re_obj input bc saved_buf res_obj ] */
  65470. duk_push_int(ctx, 0);
  65471. duk_put_prop_stridx(ctx, -6, DUK_STRIDX_LAST_INDEX);
  65472. }
  65473. /* [ ... re_obj input bc saved_buf res_obj ] */
  65474. duk_insert(ctx, -5);
  65475. /* [ ... res_obj re_obj input bc saved_buf ] */
  65476. duk_pop_n(ctx, 4);
  65477. /* [ ... res_obj ] */
  65478. /* XXX: these last tricks are unnecessary if the function is made
  65479. * a genuine native function.
  65480. */
  65481. }
  65482. DUK_INTERNAL void duk_regexp_match(duk_hthread *thr) {
  65483. duk__regexp_match_helper(thr, 0 /*force_global*/);
  65484. }
  65485. /* This variant is needed by String.prototype.split(); it needs to perform
  65486. * global-style matching on a cloned RegExp which is potentially non-global.
  65487. */
  65488. DUK_INTERNAL void duk_regexp_match_force_global(duk_hthread *thr) {
  65489. duk__regexp_match_helper(thr, 1 /*force_global*/);
  65490. }
  65491. #else /* DUK_USE_REGEXP_SUPPORT */
  65492. /* regexp support disabled */
  65493. #endif /* DUK_USE_REGEXP_SUPPORT */
  65494. #line 1 "duk_replacements.c"
  65495. /*
  65496. * Replacements for missing platform functions.
  65497. *
  65498. * Unlike the originals, fpclassify() and signbit() replacements don't
  65499. * work on any floating point types, only doubles. The C typing here
  65500. * mimics the standard prototypes.
  65501. */
  65502. /* include removed: duk_internal.h */
  65503. #ifdef DUK_USE_COMPUTED_NAN
  65504. DUK_INTERNAL double duk_computed_nan;
  65505. #endif
  65506. #ifdef DUK_USE_COMPUTED_INFINITY
  65507. DUK_INTERNAL double duk_computed_infinity;
  65508. #endif
  65509. #ifdef DUK_USE_REPL_FPCLASSIFY
  65510. DUK_INTERNAL int duk_repl_fpclassify(double x) {
  65511. duk_double_union u;
  65512. duk_uint_fast16_t expt;
  65513. duk_small_int_t mzero;
  65514. u.d = x;
  65515. expt = (duk_uint_fast16_t) (u.us[DUK_DBL_IDX_US0] & 0x7ff0UL);
  65516. if (expt > 0x0000UL && expt < 0x7ff0UL) {
  65517. /* expt values [0x001,0x7fe] = normal */
  65518. return DUK_FP_NORMAL;
  65519. }
  65520. mzero = (u.ui[DUK_DBL_IDX_UI1] == 0 && (u.ui[DUK_DBL_IDX_UI0] & 0x000fffffUL) == 0);
  65521. if (expt == 0x0000UL) {
  65522. /* expt 0x000 is zero/subnormal */
  65523. if (mzero) {
  65524. return DUK_FP_ZERO;
  65525. } else {
  65526. return DUK_FP_SUBNORMAL;
  65527. }
  65528. } else {
  65529. /* expt 0xfff is infinite/nan */
  65530. if (mzero) {
  65531. return DUK_FP_INFINITE;
  65532. } else {
  65533. return DUK_FP_NAN;
  65534. }
  65535. }
  65536. }
  65537. #endif
  65538. #ifdef DUK_USE_REPL_SIGNBIT
  65539. DUK_INTERNAL int duk_repl_signbit(double x) {
  65540. duk_double_union u;
  65541. u.d = x;
  65542. return (int) (u.uc[DUK_DBL_IDX_UC0] & 0x80UL);
  65543. }
  65544. #endif
  65545. #ifdef DUK_USE_REPL_ISFINITE
  65546. DUK_INTERNAL int duk_repl_isfinite(double x) {
  65547. int c = DUK_FPCLASSIFY(x);
  65548. if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) {
  65549. return 0;
  65550. } else {
  65551. return 1;
  65552. }
  65553. }
  65554. #endif
  65555. #ifdef DUK_USE_REPL_ISNAN
  65556. DUK_INTERNAL int duk_repl_isnan(double x) {
  65557. int c = DUK_FPCLASSIFY(x);
  65558. return (c == DUK_FP_NAN);
  65559. }
  65560. #endif
  65561. #ifdef DUK_USE_REPL_ISINF
  65562. DUK_INTERNAL int duk_repl_isinf(double x) {
  65563. int c = DUK_FPCLASSIFY(x);
  65564. return (c == DUK_FP_INFINITE);
  65565. }
  65566. #endif
  65567. #line 1 "duk_selftest.c"
  65568. /*
  65569. * Self tests to ensure execution environment is sane. Intended to catch
  65570. * compiler/platform problems which cannot be detected at compile time.
  65571. */
  65572. /* include removed: duk_internal.h */
  65573. #if defined(DUK_USE_SELF_TESTS)
  65574. /*
  65575. * Unions and structs for self tests
  65576. */
  65577. typedef union {
  65578. double d;
  65579. duk_uint8_t c[8];
  65580. } duk__test_double_union;
  65581. #define DUK__DBLUNION_CMP_TRUE(a,b) do { \
  65582. if (DUK_MEMCMP((void *) (a), (void *) (b), sizeof(duk__test_double_union)) != 0) { \
  65583. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: double union compares false (expected true)"); \
  65584. } \
  65585. } while (0)
  65586. #define DUK__DBLUNION_CMP_FALSE(a,b) do { \
  65587. if (DUK_MEMCMP((void *) (a), (void *) (b), sizeof(duk__test_double_union)) == 0) { \
  65588. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: double union compares true (expected false)"); \
  65589. } \
  65590. } while (0)
  65591. typedef union {
  65592. duk_uint32_t i;
  65593. duk_uint8_t c[8];
  65594. } duk__test_u32_union;
  65595. /*
  65596. * Various sanity checks for typing
  65597. */
  65598. DUK_LOCAL void duk__selftest_types(void) {
  65599. if (!(sizeof(duk_int8_t) == 1 &&
  65600. sizeof(duk_uint8_t) == 1 &&
  65601. sizeof(duk_int16_t) == 2 &&
  65602. sizeof(duk_uint16_t) == 2 &&
  65603. sizeof(duk_int32_t) == 4 &&
  65604. sizeof(duk_uint32_t) == 4)) {
  65605. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: duk_(u)int{8,16,32}_t size");
  65606. }
  65607. #if defined(DUK_USE_64BIT_OPS)
  65608. if (!(sizeof(duk_int64_t) == 8 &&
  65609. sizeof(duk_uint64_t) == 8)) {
  65610. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: duk_(u)int64_t size");
  65611. }
  65612. #endif
  65613. if (!(sizeof(duk_size_t) >= sizeof(duk_uint_t))) {
  65614. /* Some internal code now assumes that all duk_uint_t values
  65615. * can be expressed with a duk_size_t.
  65616. */
  65617. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: duk_size_t is smaller than duk_uint_t");
  65618. }
  65619. if (!(sizeof(duk_int_t) >= 4)) {
  65620. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: duk_int_t is not 32 bits");
  65621. }
  65622. }
  65623. /*
  65624. * Packed tval sanity
  65625. */
  65626. DUK_LOCAL void duk__selftest_packed_tval(void) {
  65627. #if defined(DUK_USE_PACKED_TVAL)
  65628. if (sizeof(void *) > 4) {
  65629. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: packed duk_tval in use but sizeof(void *) > 4");
  65630. }
  65631. #endif
  65632. }
  65633. /*
  65634. * Two's complement arithmetic.
  65635. */
  65636. DUK_LOCAL void duk__selftest_twos_complement(void) {
  65637. volatile int test;
  65638. test = -1;
  65639. if (((duk_uint8_t *) &test)[0] != (duk_uint8_t) 0xff) {
  65640. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: two's complement arithmetic");
  65641. }
  65642. }
  65643. /*
  65644. * Byte order. Important to self check, because on some exotic platforms
  65645. * there is no actual detection but rather assumption based on platform
  65646. * defines.
  65647. */
  65648. DUK_LOCAL void duk__selftest_byte_order(void) {
  65649. duk__test_u32_union u1;
  65650. duk__test_double_union u2;
  65651. /*
  65652. * >>> struct.pack('>d', 102030405060).encode('hex')
  65653. * '4237c17c6dc40000'
  65654. */
  65655. #if defined(DUK_USE_INTEGER_LE)
  65656. u1.c[0] = 0xef; u1.c[1] = 0xbe; u1.c[2] = 0xad; u1.c[3] = 0xde;
  65657. #elif defined(DUK_USE_INTEGER_ME)
  65658. #error integer mixed endian not supported now
  65659. #elif defined(DUK_USE_INTEGER_BE)
  65660. u1.c[0] = 0xde; u1.c[1] = 0xad; u1.c[2] = 0xbe; u1.c[3] = 0xef;
  65661. #else
  65662. #error unknown integer endianness
  65663. #endif
  65664. #if defined(DUK_USE_DOUBLE_LE)
  65665. u2.c[0] = 0x00; u2.c[1] = 0x00; u2.c[2] = 0xc4; u2.c[3] = 0x6d;
  65666. u2.c[4] = 0x7c; u2.c[5] = 0xc1; u2.c[6] = 0x37; u2.c[7] = 0x42;
  65667. #elif defined(DUK_USE_DOUBLE_ME)
  65668. u2.c[0] = 0x7c; u2.c[1] = 0xc1; u2.c[2] = 0x37; u2.c[3] = 0x42;
  65669. u2.c[4] = 0x00; u2.c[5] = 0x00; u2.c[6] = 0xc4; u2.c[7] = 0x6d;
  65670. #elif defined(DUK_USE_DOUBLE_BE)
  65671. u2.c[0] = 0x42; u2.c[1] = 0x37; u2.c[2] = 0xc1; u2.c[3] = 0x7c;
  65672. u2.c[4] = 0x6d; u2.c[5] = 0xc4; u2.c[6] = 0x00; u2.c[7] = 0x00;
  65673. #else
  65674. #error unknown double endianness
  65675. #endif
  65676. if (u1.i != (duk_uint32_t) 0xdeadbeefUL) {
  65677. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: duk_uint32_t byte order");
  65678. }
  65679. if (u2.d != (double) 102030405060.0) {
  65680. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: double byte order");
  65681. }
  65682. }
  65683. /*
  65684. * DUK_BSWAP macros
  65685. */
  65686. DUK_LOCAL void duk__selftest_bswap_macros(void) {
  65687. duk_uint32_t x32;
  65688. duk_uint16_t x16;
  65689. duk_double_union du;
  65690. duk_double_t du_diff;
  65691. x16 = 0xbeefUL;
  65692. x16 = DUK_BSWAP16(x16);
  65693. if (x16 != (duk_uint16_t) 0xefbeUL) {
  65694. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: DUK_BSWAP16");
  65695. }
  65696. x32 = 0xdeadbeefUL;
  65697. x32 = DUK_BSWAP32(x32);
  65698. if (x32 != (duk_uint32_t) 0xefbeaddeUL) {
  65699. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: DUK_BSWAP32");
  65700. }
  65701. /* >>> struct.unpack('>d', '4000112233445566'.decode('hex'))
  65702. * (2.008366013071895,)
  65703. */
  65704. du.uc[0] = 0x40; du.uc[1] = 0x00; du.uc[2] = 0x11; du.uc[3] = 0x22;
  65705. du.uc[4] = 0x33; du.uc[5] = 0x44; du.uc[6] = 0x55; du.uc[7] = 0x66;
  65706. DUK_DBLUNION_DOUBLE_NTOH(&du);
  65707. du_diff = du.d - 2.008366013071895;
  65708. #if 0
  65709. DUK_FPRINTF(DUK_STDERR, "du_diff: %lg\n", (double) du_diff);
  65710. #endif
  65711. if (du_diff > 1e-15) {
  65712. /* Allow very small lenience because some compilers won't parse
  65713. * exact IEEE double constants (happened in matrix testing with
  65714. * Linux gcc-4.8 -m32 at least).
  65715. */
  65716. #if 0
  65717. DUK_FPRINTF(DUK_STDERR, "Result of DUK_DBLUNION_DOUBLE_NTOH: %02x %02x %02x %02x %02x %02x %02x %02x\n",
  65718. (unsigned int) du.uc[0], (unsigned int) du.uc[1],
  65719. (unsigned int) du.uc[2], (unsigned int) du.uc[3],
  65720. (unsigned int) du.uc[4], (unsigned int) du.uc[5],
  65721. (unsigned int) du.uc[6], (unsigned int) du.uc[7]);
  65722. #endif
  65723. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: DUK_DBLUNION_DOUBLE_NTOH");
  65724. }
  65725. }
  65726. /*
  65727. * Basic double / byte union memory layout.
  65728. */
  65729. DUK_LOCAL void duk__selftest_double_union_size(void) {
  65730. if (sizeof(duk__test_double_union) != 8) {
  65731. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: invalid union size");
  65732. }
  65733. }
  65734. /*
  65735. * Union aliasing, see misc/clang_aliasing.c.
  65736. */
  65737. DUK_LOCAL void duk__selftest_double_aliasing(void) {
  65738. duk__test_double_union a, b;
  65739. /* This testcase fails when Emscripten-generated code runs on Firefox.
  65740. * It's not an issue because the failure should only affect packed
  65741. * duk_tval representation, which is not used with Emscripten.
  65742. */
  65743. #if defined(DUK_USE_NO_DOUBLE_ALIASING_SELFTEST)
  65744. #if defined(DUK_USE_PACKED_TVAL)
  65745. #error inconsistent defines: skipping double aliasing selftest when using packed duk_tval
  65746. #endif
  65747. return;
  65748. #endif
  65749. /* Test signaling NaN and alias assignment in all
  65750. * endianness combinations.
  65751. */
  65752. /* little endian */
  65753. a.c[0] = 0x11; a.c[1] = 0x22; a.c[2] = 0x33; a.c[3] = 0x44;
  65754. a.c[4] = 0x00; a.c[5] = 0x00; a.c[6] = 0xf1; a.c[7] = 0xff;
  65755. b = a;
  65756. DUK__DBLUNION_CMP_TRUE(&a, &b);
  65757. /* big endian */
  65758. a.c[0] = 0xff; a.c[1] = 0xf1; a.c[2] = 0x00; a.c[3] = 0x00;
  65759. a.c[4] = 0x44; a.c[5] = 0x33; a.c[6] = 0x22; a.c[7] = 0x11;
  65760. b = a;
  65761. DUK__DBLUNION_CMP_TRUE(&a, &b);
  65762. /* mixed endian */
  65763. a.c[0] = 0x00; a.c[1] = 0x00; a.c[2] = 0xf1; a.c[3] = 0xff;
  65764. a.c[4] = 0x11; a.c[5] = 0x22; a.c[6] = 0x33; a.c[7] = 0x44;
  65765. b = a;
  65766. DUK__DBLUNION_CMP_TRUE(&a, &b);
  65767. }
  65768. /*
  65769. * Zero sign, see misc/tcc_zerosign2.c.
  65770. */
  65771. DUK_LOCAL void duk__selftest_double_zero_sign(void) {
  65772. volatile duk__test_double_union a, b;
  65773. a.d = 0.0;
  65774. b.d = -a.d;
  65775. DUK__DBLUNION_CMP_FALSE(&a, &b);
  65776. }
  65777. /*
  65778. * Struct size/alignment if platform requires it
  65779. *
  65780. * There are some compiler specific struct padding pragmas etc in use, this
  65781. * selftest ensures they're correctly detected and used.
  65782. */
  65783. DUK_LOCAL void duk__selftest_struct_align(void) {
  65784. #if defined(DUK_USE_ALIGN_4)
  65785. if ((sizeof(duk_hbuffer_fixed) % 4) != 0) {
  65786. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: sizeof(duk_hbuffer_fixed) not aligned to 4");
  65787. }
  65788. #elif defined(DUK_USE_ALIGN_8)
  65789. if ((sizeof(duk_hbuffer_fixed) % 8) != 0) {
  65790. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: sizeof(duk_hbuffer_fixed) not aligned to 8");
  65791. }
  65792. #else
  65793. /* no check */
  65794. #endif
  65795. }
  65796. /*
  65797. * 64-bit arithmetic
  65798. *
  65799. * There are some platforms/compilers where 64-bit types are available
  65800. * but don't work correctly. Test for known cases.
  65801. */
  65802. DUK_LOCAL void duk__selftest_64bit_arithmetic(void) {
  65803. #if defined(DUK_USE_64BIT_OPS)
  65804. volatile duk_int64_t i;
  65805. volatile duk_double_t d;
  65806. /* Catch a double-to-int64 cast issue encountered in practice. */
  65807. d = 2147483648.0;
  65808. i = (duk_int64_t) d;
  65809. if (i != 0x80000000LL) {
  65810. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: casting 2147483648.0 to duk_int64_t failed");
  65811. }
  65812. #else
  65813. /* nop */
  65814. #endif
  65815. }
  65816. /*
  65817. * Casting
  65818. */
  65819. DUK_LOCAL void duk__selftest_cast_double_to_uint(void) {
  65820. /*
  65821. * https://github.com/svaarala/duktape/issues/127#issuecomment-77863473
  65822. */
  65823. duk_double_t d1, d2;
  65824. duk_small_uint_t u;
  65825. duk_double_t d1v, d2v;
  65826. duk_small_uint_t uv;
  65827. d1 = 1.0;
  65828. u = (duk_small_uint_t) d1;
  65829. d2 = (duk_double_t) u;
  65830. if (!(d1 == 1.0 && u == 1 && d2 == 1.0 && d1 == d2)) {
  65831. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: double to uint cast failed");
  65832. }
  65833. /* Same test with volatiles */
  65834. d1v = 1.0;
  65835. uv = (duk_small_uint_t) d1v;
  65836. d2v = (duk_double_t) uv;
  65837. if (!(d1v == 1.0 && uv == 1 && d2v == 1.0 && d1v == d2v)) {
  65838. DUK_PANIC(DUK_ERR_INTERNAL_ERROR, "self test failed: double to uint cast failed");
  65839. }
  65840. }
  65841. /*
  65842. * Self test main
  65843. */
  65844. DUK_INTERNAL void duk_selftest_run_tests(void) {
  65845. duk__selftest_types();
  65846. duk__selftest_packed_tval();
  65847. duk__selftest_twos_complement();
  65848. duk__selftest_byte_order();
  65849. duk__selftest_bswap_macros();
  65850. duk__selftest_double_union_size();
  65851. duk__selftest_double_aliasing();
  65852. duk__selftest_double_zero_sign();
  65853. duk__selftest_struct_align();
  65854. duk__selftest_64bit_arithmetic();
  65855. duk__selftest_cast_double_to_uint();
  65856. }
  65857. #undef DUK__DBLUNION_CMP_TRUE
  65858. #undef DUK__DBLUNION_CMP_FALSE
  65859. #endif /* DUK_USE_SELF_TESTS */
  65860. /* include removed: duk_internal.h */
  65861. #line 2 "duk_tval.c"
  65862. #if defined(DUK_USE_FASTINT)
  65863. /*
  65864. * Manually optimized double-to-fastint downgrade check.
  65865. *
  65866. * This check has a large impact on performance, especially for fastint
  65867. * slow paths, so must be changed carefully. The code should probably be
  65868. * optimized for the case where the result does not fit into a fastint,
  65869. * to minimize the penalty for "slow path code" dealing with fractions etc.
  65870. *
  65871. * At least on one tested soft float ARM platform double-to-int64 coercion
  65872. * is very slow (and sometimes produces incorrect results, see self tests).
  65873. * This algorithm combines a fastint compatibility check and extracting the
  65874. * integer value from an IEEE double for setting the tagged fastint. For
  65875. * other platforms a more naive approach might be better.
  65876. *
  65877. * See doc/fastint.rst for details.
  65878. */
  65879. DUK_INTERNAL DUK_ALWAYS_INLINE void duk_tval_set_number_chkfast(duk_tval *tv, duk_double_t x) {
  65880. duk_double_union du;
  65881. duk_int64_t i;
  65882. duk_small_int_t expt;
  65883. duk_small_int_t shift;
  65884. /* XXX: optimize for packed duk_tval directly? */
  65885. du.d = x;
  65886. i = (duk_int64_t) DUK_DBLUNION_GET_INT64(&du);
  65887. expt = (duk_small_int_t) ((i >> 52) & 0x07ff);
  65888. shift = expt - 1023;
  65889. if (shift >= 0 && shift <= 46) { /* exponents 1023 to 1069 */
  65890. duk_int64_t t;
  65891. if (((0x000fffffffffffffLL >> shift) & i) == 0) {
  65892. t = i | 0x0010000000000000LL; /* implicit leading one */
  65893. t = t & 0x001fffffffffffffLL;
  65894. t = t >> (52 - shift);
  65895. if (i < 0) {
  65896. t = -t;
  65897. }
  65898. DUK_TVAL_SET_FASTINT(tv, t);
  65899. return;
  65900. }
  65901. } else if (shift == -1023) { /* exponent 0 */
  65902. if (i >= 0 && (i & 0x000fffffffffffffLL) == 0) {
  65903. /* Note: reject negative zero. */
  65904. DUK_TVAL_SET_FASTINT(tv, (duk_int64_t) 0);
  65905. return;
  65906. }
  65907. } else if (shift == 47) { /* exponent 1070 */
  65908. if (i < 0 && (i & 0x000fffffffffffffLL) == 0) {
  65909. DUK_TVAL_SET_FASTINT(tv, (duk_int64_t) DUK_FASTINT_MIN);
  65910. return;
  65911. }
  65912. }
  65913. DUK_TVAL_SET_DOUBLE(tv, x);
  65914. return;
  65915. }
  65916. /*
  65917. * Manually optimized number-to-double conversion
  65918. */
  65919. #if defined(DUK_USE_FASTINT) && defined(DUK_USE_PACKED_TVAL)
  65920. DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_packed(duk_tval *tv) {
  65921. duk_double_union du;
  65922. duk_uint64_t t;
  65923. t = (duk_uint64_t) DUK_DBLUNION_GET_UINT64(tv);
  65924. if ((t >> 48) != DUK_TAG_FASTINT) {
  65925. return tv->d;
  65926. } else if (t & 0x0000800000000000ULL) {
  65927. t = (duk_uint64_t) (-((duk_int64_t) t)); /* avoid unary minus on unsigned */
  65928. t = t & 0x0000ffffffffffffULL; /* negative */
  65929. t |= 0xc330000000000000ULL;
  65930. DUK_DBLUNION_SET_UINT64(&du, t);
  65931. return du.d + 4503599627370496.0; /* 1 << 52 */
  65932. } else if (t != 0) {
  65933. t &= 0x0000ffffffffffffULL; /* positive */
  65934. t |= 0x4330000000000000ULL;
  65935. DUK_DBLUNION_SET_UINT64(&du, t);
  65936. return du.d - 4503599627370496.0; /* 1 << 52 */
  65937. } else {
  65938. return 0.0; /* zero */
  65939. }
  65940. }
  65941. #endif /* DUK_USE_FASTINT && DUK_USE_PACKED_TVAL */
  65942. #if 0 /* unused */
  65943. #if defined(DUK_USE_FASTINT) && !defined(DUK_USE_PACKED_TVAL)
  65944. DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_unpacked(duk_tval *tv) {
  65945. duk_double_union du;
  65946. duk_uint64_t t;
  65947. DUK_ASSERT(tv->t == DUK__TAG_NUMBER || tv->t == DUK_TAG_FASTINT);
  65948. if (tv->t == DUK_TAG_FASTINT) {
  65949. if (tv->v.fi >= 0) {
  65950. t = 0x4330000000000000ULL | (duk_uint64_t) tv->v.fi;
  65951. DUK_DBLUNION_SET_UINT64(&du, t);
  65952. return du.d - 4503599627370496.0; /* 1 << 52 */
  65953. } else {
  65954. t = 0xc330000000000000ULL | (duk_uint64_t) (-tv->v.fi);
  65955. DUK_DBLUNION_SET_UINT64(&du, t);
  65956. return du.d + 4503599627370496.0; /* 1 << 52 */
  65957. }
  65958. } else {
  65959. return tv->v.d;
  65960. }
  65961. }
  65962. #endif /* DUK_USE_FASTINT && DUK_USE_PACKED_TVAL */
  65963. #endif /* 0 */
  65964. #if defined(DUK_USE_FASTINT) && !defined(DUK_USE_PACKED_TVAL)
  65965. DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_unpacked_fastint(duk_tval *tv) {
  65966. duk_double_union du;
  65967. duk_uint64_t t;
  65968. DUK_ASSERT(tv->t == DUK_TAG_FASTINT);
  65969. if (tv->v.fi >= 0) {
  65970. t = 0x4330000000000000ULL | (duk_uint64_t) tv->v.fi;
  65971. DUK_DBLUNION_SET_UINT64(&du, t);
  65972. return du.d - 4503599627370496.0; /* 1 << 52 */
  65973. } else {
  65974. t = 0xc330000000000000ULL | (duk_uint64_t) (-tv->v.fi);
  65975. DUK_DBLUNION_SET_UINT64(&du, t);
  65976. return du.d + 4503599627370496.0; /* 1 << 52 */
  65977. }
  65978. }
  65979. #endif /* DUK_USE_FASTINT && DUK_USE_PACKED_TVAL */
  65980. #endif /* DUK_USE_FASTINT */
  65981. #line 1 "duk_unicode_tables.c"
  65982. /*
  65983. * Unicode support tables automatically generated during build.
  65984. */
  65985. /* include removed: duk_internal.h */
  65986. /*
  65987. * Unicode tables containing ranges of Unicode characters in a
  65988. * packed format. These tables are used to match non-ASCII
  65989. * characters of complex productions by resorting to a linear
  65990. * range-by-range comparison. This is very slow, but is expected
  65991. * to be very rare in practical Ecmascript source code, and thus
  65992. * compactness is most important.
  65993. *
  65994. * The tables are matched using uni_range_match() and the format
  65995. * is described in src/extract_chars.py.
  65996. */
  65997. #ifdef DUK_USE_SOURCE_NONBMP
  65998. /* IdentifierStart production with ASCII excluded */
  65999. /* duk_unicode_ids_noa[] */
  66000. /*
  66001. * Automatically generated by extract_chars.py, do not edit!
  66002. */
  66003. const duk_uint8_t duk_unicode_ids_noa[791] = {
  66004. 249,176,176,80,111,7,47,15,47,254,11,197,191,0,72,2,15,115,66,19,57,2,34,2,
  66005. 240,66,244,50,247,185,248,234,241,99,8,241,127,58,240,182,47,31,241,191,21,
  66006. 18,245,50,15,1,24,27,35,15,2,2,240,239,15,244,156,15,10,241,26,21,6,240,
  66007. 101,10,4,15,9,240,159,157,242,100,15,4,8,159,1,98,102,115,19,240,98,98,4,
  66008. 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,
  66009. 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,
  66010. 2,84,34,52,18,2,20,20,36,191,8,15,38,114,34,240,114,146,68,15,12,23,31,21,
  66011. 114,34,240,114,146,68,15,18,2,31,1,31,4,114,34,241,147,15,2,15,3,31,10,86,
  66012. 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,
  66013. 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,
  66014. 205,15,3,241,107,241,178,4,255,224,59,35,54,32,35,63,25,35,63,17,35,54,32,
  66015. 35,62,47,41,35,63,51,241,127,0,240,47,69,223,254,21,227,240,18,240,166,243,
  66016. 180,47,1,194,63,0,240,47,0,240,47,0,194,47,1,242,79,21,5,15,53,244,137,241,
  66017. 146,6,243,107,240,223,37,240,227,76,241,207,7,111,42,240,122,242,95,68,15,
  66018. 79,241,255,3,111,41,240,238,31,2,241,111,12,241,79,27,43,241,79,93,50,63,0,
  66019. 251,15,50,255,224,8,53,63,22,53,55,32,32,32,47,15,63,37,38,32,66,38,67,53,
  66020. 92,98,38,246,96,224,240,44,245,112,80,57,32,68,112,32,32,35,42,51,100,80,
  66021. 240,63,25,255,233,107,241,242,241,242,247,87,63,3,241,107,242,106,15,2,240,
  66022. 122,98,98,98,98,98,98,98,111,66,15,254,12,146,240,184,132,52,95,70,114,47,
  66023. 74,35,111,25,79,78,240,63,11,242,127,0,255,224,244,255,240,0,138,143,60,
  66024. 255,240,4,11,239,38,255,227,127,243,95,30,63,253,79,0,177,240,111,31,240,
  66025. 47,9,159,64,241,152,63,87,51,33,240,9,244,39,34,35,47,7,240,255,36,240,15,
  66026. 34,243,5,64,240,15,12,191,7,240,191,13,143,31,240,224,242,47,25,240,146,39,
  66027. 240,111,7,64,111,32,32,65,52,48,32,240,162,241,85,53,53,166,38,248,63,19,
  66028. 240,255,255,0,26,150,223,7,95,33,255,240,0,255,143,254,2,3,242,227,245,175,
  66029. 24,109,70,2,146,194,66,2,18,18,245,207,19,255,224,93,240,79,48,63,38,241,
  66030. 171,246,100,47,119,241,111,10,127,10,207,73,69,53,53,50,241,91,47,10,47,3,
  66031. 33,46,61,241,79,107,243,127,37,255,223,13,79,33,242,31,15,240,63,11,242,
  66032. 127,14,63,20,87,36,241,207,142,255,226,86,83,2,241,194,20,3,240,127,156,
  66033. 240,107,240,175,184,15,1,50,34,240,191,30,240,223,117,242,107,240,107,240,
  66034. 63,127,243,159,254,42,239,37,243,223,29,255,238,68,255,226,97,248,63,83,
  66035. 255,234,145,255,227,33,255,240,2,44,95,254,18,191,255,0,52,187,31,255,0,18,
  66036. 242,244,82,243,114,19,3,19,50,178,2,98,243,18,51,114,98,240,194,50,66,4,98,
  66037. 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,
  66038. 240,1,114,143,255,0,149,201,241,191,254,242,124,252,239,255,0,46,214,255,
  66039. 225,16,0,
  66040. };
  66041. #else
  66042. /* IdentifierStart production with ASCII and non-BMP excluded */
  66043. /* duk_unicode_ids_noabmp[] */
  66044. /*
  66045. * Automatically generated by extract_chars.py, do not edit!
  66046. */
  66047. const duk_uint8_t duk_unicode_ids_noabmp[611] = {
  66048. 249,176,176,80,111,7,47,15,47,254,11,197,191,0,72,2,15,115,66,19,57,2,34,2,
  66049. 240,66,244,50,247,185,248,234,241,99,8,241,127,58,240,182,47,31,241,191,21,
  66050. 18,245,50,15,1,24,27,35,15,2,2,240,239,15,244,156,15,10,241,26,21,6,240,
  66051. 101,10,4,15,9,240,159,157,242,100,15,4,8,159,1,98,102,115,19,240,98,98,4,
  66052. 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,
  66053. 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,
  66054. 2,84,34,52,18,2,20,20,36,191,8,15,38,114,34,240,114,146,68,15,12,23,31,21,
  66055. 114,34,240,114,146,68,15,18,2,31,1,31,4,114,34,241,147,15,2,15,3,31,10,86,
  66056. 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,
  66057. 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,
  66058. 205,15,3,241,107,241,178,4,255,224,59,35,54,32,35,63,25,35,63,17,35,54,32,
  66059. 35,62,47,41,35,63,51,241,127,0,240,47,69,223,254,21,227,240,18,240,166,243,
  66060. 180,47,1,194,63,0,240,47,0,240,47,0,194,47,1,242,79,21,5,15,53,244,137,241,
  66061. 146,6,243,107,240,223,37,240,227,76,241,207,7,111,42,240,122,242,95,68,15,
  66062. 79,241,255,3,111,41,240,238,31,2,241,111,12,241,79,27,43,241,79,93,50,63,0,
  66063. 251,15,50,255,224,8,53,63,22,53,55,32,32,32,47,15,63,37,38,32,66,38,67,53,
  66064. 92,98,38,246,96,224,240,44,245,112,80,57,32,68,112,32,32,35,42,51,100,80,
  66065. 240,63,25,255,233,107,241,242,241,242,247,87,63,3,241,107,242,106,15,2,240,
  66066. 122,98,98,98,98,98,98,98,111,66,15,254,12,146,240,184,132,52,95,70,114,47,
  66067. 74,35,111,25,79,78,240,63,11,242,127,0,255,224,244,255,240,0,138,143,60,
  66068. 255,240,4,11,239,38,255,227,127,243,95,30,63,253,79,0,177,240,111,31,240,
  66069. 47,9,159,64,241,152,63,87,51,33,240,9,244,39,34,35,47,7,240,255,36,240,15,
  66070. 34,243,5,64,240,15,12,191,7,240,191,13,143,31,240,224,242,47,25,240,146,39,
  66071. 240,111,7,64,111,32,32,65,52,48,32,240,162,241,85,53,53,166,38,248,63,19,
  66072. 240,255,255,0,26,150,223,7,95,33,255,240,0,255,143,254,2,3,242,227,245,175,
  66073. 24,109,70,2,146,194,66,2,18,18,245,207,19,255,224,93,240,79,48,63,38,241,
  66074. 171,246,100,47,119,241,111,10,127,10,207,73,69,53,53,50,0,
  66075. };
  66076. #endif
  66077. #ifdef DUK_USE_SOURCE_NONBMP
  66078. /* IdentifierStart production with Letter and ASCII excluded */
  66079. /* duk_unicode_ids_m_let_noa[] */
  66080. /*
  66081. * Automatically generated by extract_chars.py, do not edit!
  66082. */
  66083. const duk_uint8_t duk_unicode_ids_m_let_noa[42] = {
  66084. 255,240,0,94,18,255,233,99,241,51,63,254,215,32,240,184,240,2,255,240,6,89,
  66085. 249,255,240,4,148,79,37,255,224,192,9,15,120,79,255,0,15,30,245,48,
  66086. };
  66087. #else
  66088. /* IdentifierStart production with Letter, ASCII, and non-BMP excluded */
  66089. /* duk_unicode_ids_m_let_noabmp[] */
  66090. /*
  66091. * Automatically generated by extract_chars.py, do not edit!
  66092. */
  66093. const duk_uint8_t duk_unicode_ids_m_let_noabmp[24] = {
  66094. 255,240,0,94,18,255,233,99,241,51,63,254,215,32,240,184,240,2,255,240,6,89,
  66095. 249,0,
  66096. };
  66097. #endif
  66098. #ifdef DUK_USE_SOURCE_NONBMP
  66099. /* IdentifierPart production with IdentifierStart and ASCII excluded */
  66100. /* duk_unicode_idp_m_ids_noa[] */
  66101. /*
  66102. * Automatically generated by extract_chars.py, do not edit!
  66103. */
  66104. const duk_uint8_t duk_unicode_idp_m_ids_noa[397] = {
  66105. 255,225,243,246,15,254,0,116,255,191,29,32,33,33,32,243,170,242,47,15,112,
  66106. 245,118,53,49,35,57,240,144,241,15,11,244,218,240,25,241,56,241,67,40,34,
  66107. 36,241,210,249,99,242,130,47,2,38,177,57,240,50,242,160,38,49,50,160,177,
  66108. 57,240,50,242,160,36,81,50,64,240,107,64,194,242,160,39,34,34,240,97,57,
  66109. 240,50,242,160,38,49,50,145,177,57,240,64,242,212,66,35,160,240,9,240,50,
  66110. 242,198,34,35,129,193,57,240,65,242,160,38,34,35,129,193,57,240,65,242,198,
  66111. 34,35,160,177,57,240,65,243,128,85,32,39,240,65,242,240,54,215,41,244,144,
  66112. 53,33,197,57,243,1,121,192,32,32,81,242,63,4,33,106,47,20,160,245,111,4,41,
  66113. 211,82,34,54,67,235,46,255,225,179,47,254,42,98,240,242,240,241,241,1,243,
  66114. 79,14,160,57,241,50,57,248,16,246,139,91,185,245,47,1,129,121,242,244,242,
  66115. 185,47,13,58,121,245,132,242,31,1,201,240,56,210,241,9,105,241,237,242,47,
  66116. 4,153,121,246,130,47,5,80,80,251,255,23,240,115,255,225,0,31,35,31,5,15,
  66117. 109,197,4,191,254,175,34,247,240,245,47,16,255,225,30,95,91,31,255,0,100,
  66118. 121,159,55,13,31,100,31,254,0,64,64,80,240,148,244,161,242,79,1,201,127,2,
  66119. 240,9,240,231,240,188,241,227,242,29,240,25,244,29,208,145,57,241,48,242,
  66120. 96,34,49,97,32,255,224,21,114,19,159,255,0,62,24,15,254,29,95,0,240,38,209,
  66121. 240,162,251,41,241,112,255,225,177,15,254,25,105,255,228,75,34,22,63,26,37,
  66122. 15,254,75,66,242,126,241,25,240,34,241,250,255,240,10,249,228,69,151,54,
  66123. 241,3,248,98,255,228,125,242,47,255,12,23,244,254,0,
  66124. };
  66125. #else
  66126. /* IdentifierPart production with IdentifierStart, ASCII, and non-BMP excluded */
  66127. /* duk_unicode_idp_m_ids_noabmp[] */
  66128. /*
  66129. * Automatically generated by extract_chars.py, do not edit!
  66130. */
  66131. const duk_uint8_t duk_unicode_idp_m_ids_noabmp[348] = {
  66132. 255,225,243,246,15,254,0,116,255,191,29,32,33,33,32,243,170,242,47,15,112,
  66133. 245,118,53,49,35,57,240,144,241,15,11,244,218,240,25,241,56,241,67,40,34,
  66134. 36,241,210,249,99,242,130,47,2,38,177,57,240,50,242,160,38,49,50,160,177,
  66135. 57,240,50,242,160,36,81,50,64,240,107,64,194,242,160,39,34,34,240,97,57,
  66136. 240,50,242,160,38,49,50,145,177,57,240,64,242,212,66,35,160,240,9,240,50,
  66137. 242,198,34,35,129,193,57,240,65,242,160,38,34,35,129,193,57,240,65,242,198,
  66138. 34,35,160,177,57,240,65,243,128,85,32,39,240,65,242,240,54,215,41,244,144,
  66139. 53,33,197,57,243,1,121,192,32,32,81,242,63,4,33,106,47,20,160,245,111,4,41,
  66140. 211,82,34,54,67,235,46,255,225,179,47,254,42,98,240,242,240,241,241,1,243,
  66141. 79,14,160,57,241,50,57,248,16,246,139,91,185,245,47,1,129,121,242,244,242,
  66142. 185,47,13,58,121,245,132,242,31,1,201,240,56,210,241,9,105,241,237,242,47,
  66143. 4,153,121,246,130,47,5,80,80,251,255,23,240,115,255,225,0,31,35,31,5,15,
  66144. 109,197,4,191,254,175,34,247,240,245,47,16,255,225,30,95,91,31,255,0,100,
  66145. 121,159,55,13,31,100,31,254,0,64,64,80,240,148,244,161,242,79,1,201,127,2,
  66146. 240,9,240,231,240,188,241,227,242,29,240,25,244,29,208,145,57,241,48,242,
  66147. 96,34,49,97,32,255,224,21,114,19,159,255,0,62,24,15,254,29,95,0,240,38,209,
  66148. 240,162,251,41,241,112,0,
  66149. };
  66150. #endif
  66151. /*
  66152. * Case conversion tables generated using src/extract_caseconv.py.
  66153. */
  66154. /* duk_unicode_caseconv_uc[] */
  66155. /* duk_unicode_caseconv_lc[] */
  66156. /*
  66157. * Automatically generated by extract_caseconv.py, do not edit!
  66158. */
  66159. const duk_uint8_t duk_unicode_caseconv_uc[1288] = {
  66160. 132,3,128,3,0,184,7,192,6,192,112,35,242,199,224,64,74,192,49,32,128,162,
  66161. 128,108,65,1,189,129,254,131,3,173,3,136,6,7,98,7,34,68,15,12,14,140,72,30,
  66162. 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,
  66163. 31,24,12,62,64,62,80,32,124,192,124,224,64,250,0,250,64,97,246,1,246,129,3,
  66164. 238,3,247,64,135,220,135,242,2,15,187,15,237,2,31,120,31,248,4,62,244,63,
  66165. 212,8,125,240,127,232,16,253,128,253,192,33,253,1,253,128,67,252,3,253,0,
  66166. 136,92,8,88,8,18,104,18,91,26,44,48,44,0,94,90,0,33,64,155,253,7,252,132,
  66167. 212,0,32,32,32,6,0,76,192,76,129,128,157,0,156,136,1,75,1,74,46,2,244,2,
  66168. 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,
  66169. 132,128,252,128,252,20,2,35,2,34,18,4,142,4,140,20,13,196,13,192,16,30,200,
  66170. 30,192,192,70,16,70,2,32,145,96,145,70,193,48,129,48,67,130,104,130,104,44,
  66171. 30,1,30,0,150,61,66,61,64,192,125,68,125,100,33,99,65,99,56,50,200,18,200,
  66172. 6,69,157,133,157,96,169,144,105,144,11,211,64,211,64,12,167,35,167,34,15,
  66173. 78,103,78,100,126,157,234,157,228,21,59,253,59,240,90,122,26,122,0,163,128,
  66174. 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,
  66175. 128,63,112,69,142,101,142,64,130,1,136,1,135,4,3,114,3,112,8,26,120,202,
  66176. 120,176,65,1,30,1,29,130,2,105,1,150,5,255,96,22,160,115,128,31,224,47,0,
  66177. 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,
  66178. 64,71,160,51,192,68,0,53,0,52,224,55,224,62,224,59,160,49,192,62,96,62,32,
  66179. 74,5,141,224,74,37,141,160,74,69,142,0,74,96,48,32,74,128,48,192,75,32,49,
  66180. 224,75,96,50,0,76,0,50,96,76,96,50,128,76,180,241,160,77,0,50,224,77,101,
  66181. 140,64,78,37,141,192,78,64,51,160,78,160,51,224,79,165,140,128,81,0,53,192,
  66182. 81,32,72,128,81,128,72,160,82,64,54,224,104,160,115,32,110,224,110,192,117,
  66183. 128,112,192,120,64,116,96,121,128,113,128,122,0,114,64,122,32,115,0,122,
  66184. 160,116,192,122,192,116,0,122,224,121,224,126,0,115,64,126,32,116,32,126,
  66185. 64,127,32,126,160,114,160,153,224,152,3,175,52,239,163,175,165,140,99,211,
  66186. 99,204,3,247,192,115,35,252,163,253,132,41,196,38,68,48,132,48,101,140,37,
  66187. 140,5,140,160,71,69,140,192,71,217,128,55,224,5,48,5,48,20,152,10,240,1,56,
  66188. 7,194,0,74,3,12,3,144,192,230,64,194,0,192,64,236,48,58,80,48,128,48,16,88,
  66189. 120,20,212,21,72,122,90,0,72,3,49,30,151,128,21,0,194,7,166,32,5,112,48,
  66190. 161,233,152,1,100,12,40,122,106,0,65,2,190,31,80,128,233,64,196,199,212,
  66191. 176,58,80,49,48,48,1,245,76,14,148,12,76,12,4,125,91,3,165,3,19,3,66,31,
  66192. 128,135,194,0,230,71,224,97,240,144,57,145,248,40,124,40,14,100,126,14,31,
  66193. 11,3,153,31,132,135,195,0,230,71,225,97,240,208,57,145,248,104,124,56,14,
  66194. 100,126,30,31,15,3,153,31,136,135,194,0,230,71,226,97,240,144,57,145,248,
  66195. 168,124,40,14,100,126,46,31,11,3,153,31,140,135,195,0,230,71,227,97,240,
  66196. 208,57,145,248,232,124,56,14,100,126,62,31,15,3,153,31,144,135,202,0,230,
  66197. 71,228,97,242,144,57,145,249,40,124,168,14,100,126,78,31,43,3,153,31,148,
  66198. 135,203,0,230,71,229,97,242,208,57,145,249,104,124,184,14,100,126,94,31,47,
  66199. 3,153,31,152,135,202,0,230,71,230,97,242,144,57,145,249,168,124,168,14,100,
  66200. 126,110,31,43,3,153,31,156,135,203,0,230,71,231,97,242,208,57,145,249,232,
  66201. 124,184,14,100,126,126,31,47,3,153,31,160,135,218,0,230,71,232,97,246,144,
  66202. 57,145,250,40,125,168,14,100,126,142,31,107,3,153,31,164,135,219,0,230,71,
  66203. 233,97,246,208,57,145,250,104,125,184,14,100,126,158,31,111,3,153,31,168,
  66204. 135,218,0,230,71,234,97,246,144,57,145,250,168,125,168,14,100,126,174,31,
  66205. 107,3,153,31,172,135,219,0,230,71,235,97,246,208,57,145,250,232,125,184,14,
  66206. 100,126,190,31,111,3,153,31,178,135,238,128,230,71,236,224,57,16,57,145,
  66207. 251,72,14,24,14,100,126,218,3,145,3,66,31,183,192,228,64,208,128,230,71,
  66208. 239,32,57,16,57,145,252,40,127,40,14,100,127,14,3,151,3,153,31,196,128,226,
  66209. 64,230,71,241,160,57,112,52,33,252,124,14,92,13,8,14,100,127,50,3,151,3,
  66210. 153,31,210,192,230,64,194,0,192,7,244,240,57,144,48,128,48,17,253,104,14,
  66211. 100,13,8,127,95,3,153,3,8,3,66,31,226,192,233,64,194,0,192,7,248,240,58,80,
  66212. 48,128,48,17,254,72,14,132,12,76,127,154,3,165,3,66,31,231,192,233,64,194,
  66213. 0,208,135,252,161,255,160,57,145,255,56,14,164,14,100,127,210,3,143,3,153,
  66214. 31,246,128,234,64,208,135,253,240,58,144,52,32,57,145,255,200,14,164,14,
  66215. 103,236,2,0,70,0,70,251,1,128,17,128,18,126,192,160,4,96,4,207,176,60,1,24,
  66216. 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,
  66217. 177,56,21,16,21,27,236,82,5,68,5,53,251,21,129,81,1,78,254,197,160,84,224,
  66218. 84,111,177,120,21,16,20,244,
  66219. };
  66220. const duk_uint8_t duk_unicode_caseconv_lc[616] = {
  66221. 144,3,0,3,128,184,6,192,7,192,112,24,144,37,96,64,54,32,81,64,128,226,0,
  66222. 235,65,129,199,1,230,130,3,145,3,177,34,7,70,7,134,36,15,244,13,236,24,32,
  66223. 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,
  66224. 31,16,12,62,80,62,64,32,124,224,124,192,64,250,64,250,0,97,246,129,246,1,3,
  66225. 241,3,240,2,7,230,7,228,4,15,212,15,208,8,31,184,31,176,4,63,116,62,224,8,
  66226. 127,32,125,200,32,254,192,254,128,33,253,161,247,96,67,253,3,252,0,135,250,
  66227. 135,222,129,15,252,15,188,2,31,250,31,124,4,66,192,66,224,64,146,216,147,
  66228. 64,209,96,1,97,130,242,199,224,35,240,95,228,63,232,38,161,1,0,1,1,48,2,
  66229. 100,2,102,12,4,228,4,232,64,10,80,10,89,112,23,144,23,160,96,48,64,48,96,
  66230. 128,104,0,104,65,128,217,128,218,2,1,203,1,204,18,3,188,3,190,36,7,200,7,
  66231. 204,16,15,192,15,201,64,34,32,34,49,32,72,192,72,225,64,220,0,220,65,1,236,
  66232. 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,
  66233. 224,25,99,212,3,212,44,7,214,71,212,66,22,51,150,52,3,44,128,44,129,100,89,
  66234. 214,89,216,10,153,2,153,4,189,52,5,52,8,202,114,42,114,48,244,230,84,230,
  66235. 103,233,222,105,222,129,83,191,83,191,133,167,160,167,161,10,48,13,48,20,0,
  66236. 32,26,192,26,208,64,56,128,56,192,192,113,64,113,129,1,251,129,252,2,44,
  66237. 114,44,115,4,16,12,56,12,64,32,27,128,27,144,64,211,197,211,198,2,8,6,88,9,
  66238. 164,16,17,216,17,224,47,245,1,120,0,255,1,129,2,83,1,134,2,84,1,142,1,221,
  66239. 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,
  66240. 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,
  66241. 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,
  66242. 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,
  66243. 3,215,3,244,3,184,3,249,3,242,4,192,4,207,30,158,0,223,31,188,31,179,31,
  66244. 204,31,195,31,236,31,229,31,252,31,243,33,38,3,201,33,42,0,107,33,43,0,229,
  66245. 33,50,33,78,33,131,33,132,44,96,44,97,44,98,2,107,44,99,29,125,44,100,2,
  66246. 125,44,109,2,81,44,110,2,113,44,111,2,80,44,112,2,82,167,125,29,121,167,
  66247. 141,2,101,2,2,97,0,52,129,131,128,
  66248. };
  66249. #line 1 "duk_util_bitdecoder.c"
  66250. /*
  66251. * Bitstream decoder.
  66252. */
  66253. /* include removed: duk_internal.h */
  66254. /* Decode 'bits' bits from the input stream (bits must be 1...24).
  66255. * When reading past bitstream end, zeroes are shifted in. The result
  66256. * is signed to match duk_bd_decode_flagged.
  66257. */
  66258. DUK_INTERNAL duk_int32_t duk_bd_decode(duk_bitdecoder_ctx *ctx, duk_small_int_t bits) {
  66259. duk_small_int_t shift;
  66260. duk_uint32_t mask;
  66261. duk_uint32_t tmp;
  66262. /* Note: cannot read more than 24 bits without possibly shifting top bits out.
  66263. * Fixable, but adds complexity.
  66264. */
  66265. DUK_ASSERT(bits >= 1 && bits <= 24);
  66266. while (ctx->currbits < bits) {
  66267. #if 0
  66268. DUK_DDD(DUK_DDDPRINT("decode_bits: shift more data (bits=%ld, currbits=%ld)",
  66269. (long) bits, (long) ctx->currbits));
  66270. #endif
  66271. ctx->currval <<= 8;
  66272. if (ctx->offset < ctx->length) {
  66273. /* If ctx->offset >= ctx->length, we "shift zeroes in"
  66274. * instead of croaking.
  66275. */
  66276. ctx->currval |= ctx->data[ctx->offset++];
  66277. }
  66278. ctx->currbits += 8;
  66279. }
  66280. #if 0
  66281. DUK_DDD(DUK_DDDPRINT("decode_bits: bits=%ld, currbits=%ld, currval=0x%08lx",
  66282. (long) bits, (long) ctx->currbits, (unsigned long) ctx->currval));
  66283. #endif
  66284. /* Extract 'top' bits of currval; note that the extracted bits do not need
  66285. * to be cleared, we just ignore them on next round.
  66286. */
  66287. shift = ctx->currbits - bits;
  66288. mask = (1 << bits) - 1;
  66289. tmp = (ctx->currval >> shift) & mask;
  66290. ctx->currbits = shift; /* remaining */
  66291. #if 0
  66292. DUK_DDD(DUK_DDDPRINT("decode_bits: %ld bits -> 0x%08lx (%ld), currbits=%ld, currval=0x%08lx",
  66293. (long) bits, (unsigned long) tmp, (long) tmp, (long) ctx->currbits, (unsigned long) ctx->currval));
  66294. #endif
  66295. return tmp;
  66296. }
  66297. DUK_INTERNAL duk_small_int_t duk_bd_decode_flag(duk_bitdecoder_ctx *ctx) {
  66298. return (duk_small_int_t) duk_bd_decode(ctx, 1);
  66299. }
  66300. /* Decode a one-bit flag, and if set, decode a value of 'bits', otherwise return
  66301. * default value. Return value is signed so that negative marker value can be
  66302. * used by caller as a "not present" value.
  66303. */
  66304. DUK_INTERNAL duk_int32_t duk_bd_decode_flagged(duk_bitdecoder_ctx *ctx, duk_small_int_t bits, duk_int32_t def_value) {
  66305. if (duk_bd_decode_flag(ctx)) {
  66306. return (duk_int32_t) duk_bd_decode(ctx, bits);
  66307. } else {
  66308. return def_value;
  66309. }
  66310. }
  66311. #line 1 "duk_util_bitencoder.c"
  66312. /*
  66313. * Bitstream encoder.
  66314. */
  66315. /* include removed: duk_internal.h */
  66316. DUK_INTERNAL void duk_be_encode(duk_bitencoder_ctx *ctx, duk_uint32_t data, duk_small_int_t bits) {
  66317. duk_uint8_t tmp;
  66318. DUK_ASSERT(ctx != NULL);
  66319. DUK_ASSERT(ctx->currbits < 8);
  66320. /* This limitation would be fixable but adds unnecessary complexity. */
  66321. DUK_ASSERT(bits >= 1 && bits <= 24);
  66322. ctx->currval = (ctx->currval << bits) | data;
  66323. ctx->currbits += bits;
  66324. while (ctx->currbits >= 8) {
  66325. if (ctx->offset < ctx->length) {
  66326. tmp = (duk_uint8_t) ((ctx->currval >> (ctx->currbits - 8)) & 0xff);
  66327. ctx->data[ctx->offset++] = tmp;
  66328. } else {
  66329. /* If buffer has been exhausted, truncate bitstream */
  66330. ctx->truncated = 1;
  66331. }
  66332. ctx->currbits -= 8;
  66333. }
  66334. }
  66335. DUK_INTERNAL void duk_be_finish(duk_bitencoder_ctx *ctx) {
  66336. duk_small_int_t npad;
  66337. DUK_ASSERT(ctx != NULL);
  66338. DUK_ASSERT(ctx->currbits < 8);
  66339. npad = (duk_small_int_t) (8 - ctx->currbits);
  66340. if (npad > 0) {
  66341. duk_be_encode(ctx, 0, npad);
  66342. }
  66343. DUK_ASSERT(ctx->currbits == 0);
  66344. }
  66345. #line 1 "duk_util_hashbytes.c"
  66346. /*
  66347. * Hash function duk_util_hashbytes().
  66348. *
  66349. * Currently, 32-bit MurmurHash2.
  66350. *
  66351. * Don't rely on specific hash values; hash function may be endianness
  66352. * dependent, for instance.
  66353. */
  66354. /* include removed: duk_internal.h */
  66355. /* 'magic' constants for Murmurhash2 */
  66356. #define DUK__MAGIC_M ((duk_uint32_t) 0x5bd1e995UL)
  66357. #define DUK__MAGIC_R 24
  66358. DUK_INTERNAL duk_uint32_t duk_util_hashbytes(const duk_uint8_t *data, duk_size_t len, duk_uint32_t seed) {
  66359. duk_uint32_t h = seed ^ ((duk_uint32_t) len);
  66360. while (len >= 4) {
  66361. /* Portability workaround is required for platforms without
  66362. * unaligned access. The replacement code emulates little
  66363. * endian access even on big endian architectures, which is
  66364. * OK as long as it is consistent for a build.
  66365. */
  66366. #ifdef DUK_USE_HASHBYTES_UNALIGNED_U32_ACCESS
  66367. duk_uint32_t k = *((duk_uint32_t *) data);
  66368. #else
  66369. duk_uint32_t k = ((duk_uint32_t) data[0]) |
  66370. (((duk_uint32_t) data[1]) << 8) |
  66371. (((duk_uint32_t) data[2]) << 16) |
  66372. (((duk_uint32_t) data[3]) << 24);
  66373. #endif
  66374. k *= DUK__MAGIC_M;
  66375. k ^= k >> DUK__MAGIC_R;
  66376. k *= DUK__MAGIC_M;
  66377. h *= DUK__MAGIC_M;
  66378. h ^= k;
  66379. data += 4;
  66380. len -= 4;
  66381. }
  66382. switch (len) {
  66383. case 3: h ^= data[2] << 16;
  66384. case 2: h ^= data[1] << 8;
  66385. case 1: h ^= data[0];
  66386. h *= DUK__MAGIC_M;
  66387. }
  66388. h ^= h >> 13;
  66389. h *= DUK__MAGIC_M;
  66390. h ^= h >> 15;
  66391. return h;
  66392. }
  66393. #line 1 "duk_util_tinyrandom.c"
  66394. /*
  66395. * A tiny random number generator.
  66396. *
  66397. * Currently used for Math.random().
  66398. *
  66399. * http://www.woodmann.com/forum/archive/index.php/t-3100.html
  66400. */
  66401. /* include removed: duk_internal.h */
  66402. #define DUK__UPDATE_RND(rnd) do { \
  66403. (rnd) += ((rnd) * (rnd)) | 0x05; \
  66404. (rnd) = ((rnd) & 0xffffffffU); /* if duk_uint32_t is exactly 32 bits, this is a NOP */ \
  66405. } while (0)
  66406. #define DUK__RND_BIT(rnd) ((rnd) >> 31) /* only use the highest bit */
  66407. DUK_INTERNAL duk_uint32_t duk_util_tinyrandom_get_bits(duk_hthread *thr, duk_small_int_t n) {
  66408. duk_small_int_t i;
  66409. duk_uint32_t res = 0;
  66410. duk_uint32_t rnd;
  66411. rnd = thr->heap->rnd_state;
  66412. for (i = 0; i < n; i++) {
  66413. DUK__UPDATE_RND(rnd);
  66414. res <<= 1;
  66415. res += DUK__RND_BIT(rnd);
  66416. }
  66417. thr->heap->rnd_state = rnd;
  66418. return res;
  66419. }
  66420. DUK_INTERNAL duk_double_t duk_util_tinyrandom_get_double(duk_hthread *thr) {
  66421. duk_double_t t;
  66422. duk_small_int_t n;
  66423. duk_uint32_t rnd;
  66424. /*
  66425. * XXX: could make this a lot faster if we create the double memory
  66426. * representation directly. Feasible easily (must be uniform random).
  66427. */
  66428. rnd = thr->heap->rnd_state;
  66429. n = 53; /* enough to cover the whole mantissa */
  66430. t = 0.0;
  66431. do {
  66432. DUK__UPDATE_RND(rnd);
  66433. t += DUK__RND_BIT(rnd);
  66434. t /= 2.0;
  66435. } while (--n);
  66436. thr->heap->rnd_state = rnd;
  66437. DUK_ASSERT(t >= (duk_double_t) 0.0);
  66438. DUK_ASSERT(t < (duk_double_t) 1.0);
  66439. return t;
  66440. }