JavascriptOperators.cpp 528 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeLanguagePch.h"
  6. #include "Types/PathTypeHandler.h"
  7. #include "Types/PropertyIndexRanges.h"
  8. #include "Types/UnscopablesWrapperObject.h"
  9. #include "Types/SpreadArgument.h"
  10. #include "Library/JavascriptPromise.h"
  11. #include "Library/JavascriptRegularExpression.h"
  12. #include "Library/ThrowErrorObject.h"
  13. #include "Library/JavascriptGeneratorFunction.h"
  14. #include "Library/ForInObjectEnumerator.h"
  15. #include "Library/ES5Array.h"
  16. #include "Types/SimpleDictionaryPropertyDescriptor.h"
  17. #include "Types/SimpleDictionaryTypeHandler.h"
  18. #include "Language/ModuleNamespace.h"
  19. #ifndef SCRIPT_DIRECT_TYPE
  20. typedef enum JsNativeValueType: int
  21. {
  22. JsInt8Type,
  23. JsUint8Type,
  24. JsInt16Type,
  25. JsUint16Type,
  26. JsInt32Type,
  27. JsUint32Type,
  28. JsInt64Type,
  29. JsUint64Type,
  30. JsFloatType,
  31. JsDoubleType,
  32. JsNativeStringType
  33. } JsNativeValueType;
  34. typedef struct JsNativeString
  35. {
  36. unsigned int length;
  37. LPCWSTR str;
  38. } JsNativeString;
  39. #endif
  40. using namespace Js;
  41. DEFINE_RECYCLER_TRACKER_ARRAY_PERF_COUNTER(Var);
  42. DEFINE_RECYCLER_TRACKER_PERF_COUNTER(FrameDisplay);
  43. enum IndexType
  44. {
  45. IndexType_Number,
  46. IndexType_PropertyId,
  47. IndexType_JavascriptString
  48. };
  49. IndexType GetIndexTypeFromString(char16 const * propertyName, charcount_t propertyLength, ScriptContext* scriptContext, uint32* index, PropertyRecord const** propertyRecord, bool createIfNotFound)
  50. {
  51. if (JavascriptOperators::TryConvertToUInt32(propertyName, propertyLength, index) &&
  52. (*index != JavascriptArray::InvalidIndex))
  53. {
  54. return IndexType_Number;
  55. }
  56. else
  57. {
  58. if (createIfNotFound)
  59. {
  60. scriptContext->GetOrAddPropertyRecord(propertyName, propertyLength, propertyRecord);
  61. }
  62. else
  63. {
  64. scriptContext->FindPropertyRecord(propertyName, propertyLength, propertyRecord);
  65. }
  66. return IndexType_PropertyId;
  67. }
  68. }
  69. IndexType GetIndexTypeFromPrimitive(Var indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, JavascriptString ** propertyNameString, bool createIfNotFound, bool preferJavascriptStringOverPropertyRecord)
  70. {
  71. // CONSIDER: Only OP_SetElementI and OP_GetElementI use and take advantage of the
  72. // IndexType_JavascriptString result. Consider modifying other callers of GetIndexType to take
  73. // advantage of non-interned property strings where appropriate.
  74. if (TaggedInt::Is(indexVar))
  75. {
  76. int indexInt = TaggedInt::ToInt32(indexVar);
  77. if (indexInt >= 0)
  78. {
  79. *index = (uint)indexInt;
  80. return IndexType_Number;
  81. }
  82. else
  83. {
  84. char16 stringBuffer[22];
  85. int pos = TaggedInt::ToBuffer(indexInt, stringBuffer, _countof(stringBuffer));
  86. charcount_t length = (_countof(stringBuffer) - 1) - pos;
  87. if (createIfNotFound || preferJavascriptStringOverPropertyRecord)
  88. {
  89. // When preferring JavascriptString objects, just return a PropertyRecord instead
  90. // of creating temporary JavascriptString objects for every negative integer that
  91. // comes through here.
  92. scriptContext->GetOrAddPropertyRecord(stringBuffer + pos, length, propertyRecord);
  93. }
  94. else
  95. {
  96. scriptContext->FindPropertyRecord(stringBuffer + pos, length, propertyRecord);
  97. }
  98. return IndexType_PropertyId;
  99. }
  100. }
  101. if (JavascriptNumber::Is_NoTaggedIntCheck(indexVar))
  102. {
  103. // If this double can be a positive integer index, convert it.
  104. int32 value = 0;
  105. bool isInt32 = false;
  106. if (JavascriptNumber::TryGetInt32OrUInt32Value(JavascriptNumber::GetValue(indexVar), &value, &isInt32)
  107. && !isInt32
  108. && static_cast<uint32>(value) < JavascriptArray::InvalidIndex)
  109. {
  110. *index = static_cast<uint32>(value);
  111. return IndexType_Number;
  112. }
  113. // Fall through to slow string conversion.
  114. }
  115. JavascriptSymbol * symbol = JavascriptOperators::TryFromVar<JavascriptSymbol>(indexVar);
  116. if (symbol)
  117. {
  118. // JavascriptSymbols cannot add a new PropertyRecord - they correspond to one and only one existing PropertyRecord.
  119. // We already know what the PropertyRecord is since it is stored in the JavascriptSymbol itself so just return it.
  120. *propertyRecord = symbol->GetValue();
  121. return IndexType_PropertyId;
  122. }
  123. else
  124. {
  125. JavascriptString* indexStr = JavascriptConversion::ToString(indexVar, scriptContext);
  126. char16 const * propertyName = indexStr->GetString();
  127. charcount_t const propertyLength = indexStr->GetLength();
  128. if (!createIfNotFound && preferJavascriptStringOverPropertyRecord)
  129. {
  130. if (JavascriptOperators::TryConvertToUInt32(propertyName, propertyLength, index) &&
  131. (*index != JavascriptArray::InvalidIndex))
  132. {
  133. return IndexType_Number;
  134. }
  135. *propertyNameString = indexStr;
  136. return IndexType_JavascriptString;
  137. }
  138. return GetIndexTypeFromString(propertyName, propertyLength, scriptContext, index, propertyRecord, createIfNotFound);
  139. }
  140. }
  141. IndexType GetIndexTypeFromPrimitive(Var indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, bool createIfNotFound)
  142. {
  143. return GetIndexTypeFromPrimitive(indexVar, scriptContext, index, propertyRecord, nullptr, createIfNotFound, false);
  144. }
  145. IndexType GetIndexType(Var& indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, JavascriptString ** propertyNameString, bool createIfNotFound, bool preferJavascriptStringOverPropertyRecord)
  146. {
  147. indexVar = JavascriptConversion::ToPrimitive<JavascriptHint::HintString>(indexVar, scriptContext);
  148. return GetIndexTypeFromPrimitive(indexVar, scriptContext, index, propertyRecord, propertyNameString, createIfNotFound, preferJavascriptStringOverPropertyRecord);
  149. }
  150. IndexType GetIndexType(Var& indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, bool createIfNotFound)
  151. {
  152. return GetIndexType(indexVar, scriptContext, index, propertyRecord, nullptr, createIfNotFound, false);
  153. }
  154. BOOL FEqualDbl(double dbl1, double dbl2)
  155. {
  156. // If the low ulongs don't match, they can't be equal.
  157. if (Js::NumberUtilities::LuLoDbl(dbl1) != Js::NumberUtilities::LuLoDbl(dbl2))
  158. return FALSE;
  159. // If the high ulongs don't match, they can be equal iff one is -0 and
  160. // the other is +0.
  161. if (Js::NumberUtilities::LuHiDbl(dbl1) != Js::NumberUtilities::LuHiDbl(dbl2))
  162. {
  163. return 0x80000000 == (Js::NumberUtilities::LuHiDbl(dbl1) | Js::NumberUtilities::LuHiDbl(dbl2)) &&
  164. 0 == Js::NumberUtilities::LuLoDbl(dbl1);
  165. }
  166. // The bit patterns match. They are equal iff they are not Nan.
  167. return !Js::NumberUtilities::IsNan(dbl1);
  168. }
  169. Var JavascriptOperators::OP_ApplyArgs(Var func, Var instance, __in_xcount(8) void** stackPtr, CallInfo callInfo, ScriptContext* scriptContext)
  170. {
  171. JIT_HELPER_REENTRANT_HEADER(Op_OP_ApplyArgs);
  172. int argCount = callInfo.Count;
  173. ///
  174. /// Check func has internal [[Call]] property
  175. /// If not, throw TypeError
  176. ///
  177. if (!JavascriptConversion::IsCallable(func)) {
  178. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction);
  179. }
  180. // Fix callInfo: expect result/value, and none of other flags are currently applicable.
  181. // OP_ApplyArgs expects a result. Neither of {jit, interpreted} mode sends correct callFlags:
  182. // LdArgCnt -- jit sends whatever was passed to current function, interpreter always sends 0.
  183. // See Win8 bug 490489.
  184. callInfo.Flags = CallFlags_Value;
  185. RecyclableObject *funcPtr = UnsafeVarTo<RecyclableObject>(func);
  186. PROBE_STACK(scriptContext, Js::Constants::MinStackDefault + argCount * 4);
  187. JavascriptMethod entryPoint = funcPtr->GetEntryPoint();
  188. Var ret;
  189. switch (argCount) {
  190. case 0:
  191. Assert(false);
  192. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo);
  193. break;
  194. case 1:
  195. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance);
  196. break;
  197. case 2:
  198. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0]);
  199. break;
  200. case 3:
  201. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1]);
  202. break;
  203. case 4:
  204. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2]);
  205. break;
  206. case 5:
  207. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2], stackPtr[3]);
  208. break;
  209. case 6:
  210. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2], stackPtr[3], stackPtr[4]);
  211. break;
  212. case 7:
  213. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2], stackPtr[3], stackPtr[4], stackPtr[5]);
  214. break;
  215. default:
  216. {
  217. // Don't need stack probe here- we just did so above
  218. Arguments args(callInfo, stackPtr - 1);
  219. BEGIN_SAFE_REENTRANT_CALL(scriptContext->GetThreadContext())
  220. {
  221. ret = JavascriptFunction::CallFunction<false>(funcPtr, entryPoint, args);
  222. }
  223. END_SAFE_REENTRANT_CALL
  224. break;
  225. }
  226. }
  227. return ret;
  228. JIT_HELPER_END(Op_OP_ApplyArgs);
  229. }
  230. #ifdef _M_IX86
  231. // Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
  232. Var JavascriptOperators::Int32ToVar(int32 value, ScriptContext* scriptContext)
  233. {
  234. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_Int32ToAtom);
  235. return JavascriptNumber::ToVar(value, scriptContext);
  236. JIT_HELPER_END(Op_Int32ToAtom);
  237. }
  238. // Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
  239. Var JavascriptOperators::Int32ToVarInPlace(int32 value, ScriptContext* scriptContext, JavascriptNumber* result)
  240. {
  241. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_Int32ToAtomInPlace);
  242. return JavascriptNumber::ToVarInPlace(value, scriptContext, result);
  243. JIT_HELPER_END(Op_Int32ToAtomInPlace);
  244. }
  245. // Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
  246. Var JavascriptOperators::UInt32ToVar(uint32 value, ScriptContext* scriptContext)
  247. {
  248. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_UInt32ToAtom);
  249. return JavascriptNumber::ToVar(value, scriptContext);
  250. JIT_HELPER_END(Op_UInt32ToAtom);
  251. }
  252. // Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
  253. Var JavascriptOperators::UInt32ToVarInPlace(uint32 value, ScriptContext* scriptContext, JavascriptNumber* result)
  254. {
  255. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_UInt32ToAtomInPlace);
  256. return JavascriptNumber::ToVarInPlace(value, scriptContext, result);
  257. JIT_HELPER_END(Op_UInt32ToAtomInPlace);
  258. }
  259. #endif
  260. Var JavascriptOperators::OP_FinishOddDivBy2(uint32 value, ScriptContext *scriptContext)
  261. {
  262. return JavascriptNumber::New((double)(value + 0.5), scriptContext);
  263. }
  264. Var JavascriptOperators::ToNumberInPlace(Var aRight, ScriptContext* scriptContext, JavascriptNumber* result)
  265. {
  266. JIT_HELPER_REENTRANT_HEADER(Op_ConvNumberInPlace);
  267. if (TaggedInt::Is(aRight) || JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  268. {
  269. return aRight;
  270. }
  271. return JavascriptNumber::ToVarInPlace(JavascriptConversion::ToNumber(aRight, scriptContext), scriptContext, result);
  272. JIT_HELPER_END(Op_ConvNumberInPlace);
  273. }
  274. Var JavascriptOperators::ToNumericInPlace(Var aRight, ScriptContext* scriptContext, JavascriptNumber* result)
  275. {
  276. if (JavascriptOperators::GetTypeId(aRight) == TypeIds_BigInt)
  277. {
  278. return aRight;
  279. }
  280. return JavascriptOperators::ToNumberInPlace(aRight, scriptContext, result);
  281. }
  282. Var JavascriptOperators::Typeof(Var var, ScriptContext* scriptContext)
  283. {
  284. JIT_HELPER_REENTRANT_HEADER(Op_Typeof);
  285. switch (JavascriptOperators::GetTypeId(var))
  286. {
  287. case TypeIds_Undefined:
  288. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  289. case TypeIds_Null:
  290. //null
  291. return scriptContext->GetLibrary()->GetObjectTypeDisplayString();
  292. case TypeIds_Integer:
  293. case TypeIds_Number:
  294. case TypeIds_Int64Number:
  295. case TypeIds_UInt64Number:
  296. return scriptContext->GetLibrary()->GetNumberTypeDisplayString();
  297. default:
  298. // Falsy objects are typeof 'undefined'.
  299. if (VarTo<RecyclableObject>(var)->GetType()->IsFalsy())
  300. {
  301. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  302. }
  303. else
  304. {
  305. return VarTo<RecyclableObject>(var)->GetTypeOfString(scriptContext);
  306. }
  307. }
  308. JIT_HELPER_END(Op_Typeof);
  309. }
  310. Var JavascriptOperators::TypeofFld(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  311. {
  312. return TypeofFld_Internal(instance, false, propertyId, scriptContext);
  313. }
  314. Var JavascriptOperators::TypeofRootFld(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  315. {
  316. return TypeofFld_Internal(instance, true, propertyId, scriptContext);
  317. }
  318. Var JavascriptOperators::TypeofFld_Internal(Var instance, const bool isRoot, PropertyId propertyId, ScriptContext* scriptContext)
  319. {
  320. RecyclableObject* object = nullptr;
  321. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  322. {
  323. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined , scriptContext->GetPropertyName(propertyId)->GetBuffer());
  324. }
  325. Var value = nullptr;
  326. try
  327. {
  328. Js::JavascriptExceptionOperators::AutoCatchHandlerExists autoCatchHandlerExists(scriptContext);
  329. // In edge mode, spec compat is more important than backward compat. Use spec/web behavior here
  330. if (isRoot
  331. ? !JavascriptOperators::GetRootProperty(instance, propertyId, &value, scriptContext)
  332. : !JavascriptOperators::GetProperty(instance, object, propertyId, &value, scriptContext))
  333. {
  334. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  335. }
  336. if (!scriptContext->IsUndeclBlockVar(value))
  337. {
  338. return JavascriptOperators::Typeof(value, scriptContext);
  339. }
  340. }
  341. catch(const JavascriptException& err)
  342. {
  343. err.GetAndClear(); // discard exception object
  344. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  345. }
  346. Assert(scriptContext->IsUndeclBlockVar(value));
  347. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  348. }
  349. Var JavascriptOperators::TypeofElem_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  350. {
  351. JIT_HELPER_REENTRANT_HEADER(Op_TypeofElem_UInt32);
  352. if (JavascriptOperators::IsNumberFromNativeArray(instance, index, scriptContext))
  353. return scriptContext->GetLibrary()->GetNumberTypeDisplayString();
  354. #if FLOATVAR
  355. return TypeofElem(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  356. #else
  357. char buffer[sizeof(Js::JavascriptNumber)];
  358. return TypeofElem(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  359. (Js::JavascriptNumber *)buffer), scriptContext);
  360. #endif
  361. JIT_HELPER_END(Op_TypeofElem_UInt32);
  362. }
  363. Var JavascriptOperators::TypeofElem_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  364. {
  365. JIT_HELPER_REENTRANT_HEADER(Op_TypeofElem_Int32);
  366. if (JavascriptOperators::IsNumberFromNativeArray(instance, index, scriptContext))
  367. return scriptContext->GetLibrary()->GetNumberTypeDisplayString();
  368. #if FLOATVAR
  369. return TypeofElem(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  370. #else
  371. char buffer[sizeof(Js::JavascriptNumber)];
  372. return TypeofElem(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  373. (Js::JavascriptNumber *)buffer), scriptContext);
  374. #endif
  375. JIT_HELPER_END(Op_TypeofElem_Int32);
  376. }
  377. Js::JavascriptString* GetPropertyDisplayNameForError(Var prop, ScriptContext* scriptContext)
  378. {
  379. JavascriptString* str;
  380. JavascriptSymbol *symbol = JavascriptOperators::TryFromVar<JavascriptSymbol>(prop);
  381. if (symbol)
  382. {
  383. str = JavascriptSymbol::ToString(symbol->GetValue(), scriptContext);
  384. }
  385. else
  386. {
  387. str = JavascriptConversion::ToString(prop, scriptContext);
  388. }
  389. return str;
  390. }
  391. Var JavascriptOperators::TypeofElem(Var instance, Var index, ScriptContext* scriptContext)
  392. {
  393. JIT_HELPER_REENTRANT_HEADER(Op_TypeofElem);
  394. RecyclableObject* object = nullptr;
  395. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  396. {
  397. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined, GetPropertyDisplayNameForError(index, scriptContext));
  398. }
  399. Var member = nullptr;
  400. uint32 indexVal;
  401. PropertyRecord const * propertyRecord = nullptr;
  402. ThreadContext* threadContext = scriptContext->GetThreadContext();
  403. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  404. threadContext->ClearImplicitCallFlags();
  405. try
  406. {
  407. Js::JavascriptExceptionOperators::AutoCatchHandlerExists autoCatchHandlerExists(scriptContext);
  408. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, false);
  409. // For JS Objects, don't create the propertyId if not already added
  410. if (indexType == IndexType_Number)
  411. {
  412. // In edge mode, we don't need to worry about the special "unknown" behavior. If the item is not available from Get,
  413. // just return undefined.
  414. if (!JavascriptOperators::GetItem(instance, object, indexVal, &member, scriptContext))
  415. {
  416. // If the instance doesn't have the item, typeof result is "undefined".
  417. threadContext->CheckAndResetImplicitCallAccessorFlag();
  418. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  419. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  420. }
  421. }
  422. else
  423. {
  424. Assert(indexType == IndexType_PropertyId);
  425. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  426. {
  427. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
  428. Assert(indexType == IndexType_PropertyId);
  429. Assert(propertyRecord != nullptr);
  430. }
  431. if (propertyRecord != nullptr)
  432. {
  433. if (!JavascriptOperators::GetProperty(instance, object, propertyRecord->GetPropertyId(), &member, scriptContext))
  434. {
  435. // If the instance doesn't have the property, typeof result is "undefined".
  436. threadContext->CheckAndResetImplicitCallAccessorFlag();
  437. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  438. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  439. }
  440. }
  441. else
  442. {
  443. #if DBG
  444. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  445. PropertyRecord const * debugPropertyRecord;
  446. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  447. AssertMsg(!JavascriptOperators::GetProperty(instance, object, debugPropertyRecord->GetPropertyId(), &member, scriptContext), "how did this property come? See OS Bug 2727708 if you see this come from the web");
  448. #endif
  449. // If the instance doesn't have the property, typeof result is "undefined".
  450. threadContext->CheckAndResetImplicitCallAccessorFlag();
  451. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  452. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  453. }
  454. }
  455. threadContext->CheckAndResetImplicitCallAccessorFlag();
  456. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  457. return JavascriptOperators::Typeof(member, scriptContext);
  458. }
  459. catch(const JavascriptException& err)
  460. {
  461. err.GetAndClear(); // discard exception object
  462. threadContext->CheckAndResetImplicitCallAccessorFlag();
  463. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  464. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  465. }
  466. JIT_HELPER_END(Op_TypeofElem);
  467. }
  468. //
  469. // Delete the given Var
  470. //
  471. Var JavascriptOperators::Delete(Var var, ScriptContext* scriptContext)
  472. {
  473. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_Delete);
  474. return scriptContext->GetLibrary()->GetTrue();
  475. JIT_HELPER_END(Op_Delete);
  476. }
  477. BOOL JavascriptOperators::Equal_Full(Var aLeft, Var aRight, ScriptContext* requestContext)
  478. {
  479. JIT_HELPER_REENTRANT_HEADER(Op_Equal_Full);
  480. //
  481. // Fast-path SmInts and paired Number combinations.
  482. //
  483. if (aLeft == aRight)
  484. {
  485. if (JavascriptNumber::Is(aLeft) && JavascriptNumber::IsNan(JavascriptNumber::GetValue(aLeft)))
  486. {
  487. return false;
  488. }
  489. else if (VarIs<JavascriptVariantDate>(aLeft) == false) // only need to check on aLeft - since they are the same var, aRight would do the same
  490. {
  491. return true;
  492. }
  493. else
  494. {
  495. //In ES5 mode strict equals (===) on same instance of object type VariantDate succeeds.
  496. //Hence equals needs to succeed.
  497. return true;
  498. }
  499. }
  500. BOOL result = false;
  501. if (TaggedInt::Is(aLeft))
  502. {
  503. if (TaggedInt::Is(aRight))
  504. {
  505. // If aLeft == aRight, we would already have returned true above.
  506. return false;
  507. }
  508. else if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  509. {
  510. return TaggedInt::ToDouble(aLeft) == JavascriptNumber::GetValue(aRight);
  511. }
  512. else
  513. {
  514. BOOL res = UnsafeVarTo<RecyclableObject>(aRight)->Equals(aLeft, &result, requestContext);
  515. AssertMsg(res, "Should have handled this");
  516. return result;
  517. }
  518. }
  519. else if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  520. {
  521. if (TaggedInt::Is(aRight))
  522. {
  523. return TaggedInt::ToDouble(aRight) == JavascriptNumber::GetValue(aLeft);
  524. }
  525. else if(JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  526. {
  527. return JavascriptNumber::GetValue(aLeft) == JavascriptNumber::GetValue(aRight);
  528. }
  529. else
  530. {
  531. BOOL res = UnsafeVarTo<RecyclableObject>(aRight)->Equals(aLeft, &result, requestContext);
  532. AssertMsg(res, "Should have handled this");
  533. return result;
  534. }
  535. }
  536. if (UnsafeVarTo<RecyclableObject>(aLeft)->Equals(aRight, &result, requestContext))
  537. {
  538. return result;
  539. }
  540. else
  541. {
  542. return false;
  543. }
  544. JIT_HELPER_END(Op_Equal_Full);
  545. }
  546. BOOL JavascriptOperators::Greater_Full(Var aLeft,Var aRight,ScriptContext* scriptContext)
  547. {
  548. JIT_HELPER_REENTRANT_HEADER(Op_Greater_Full);
  549. return RelationalComparisonHelper(aRight, aLeft, scriptContext, false, false);
  550. JIT_HELPER_END(Op_Greater_Full);
  551. }
  552. BOOL JavascriptOperators::Less_Full(Var aLeft, Var aRight, ScriptContext* scriptContext)
  553. {
  554. return RelationalComparisonHelper(aLeft, aRight, scriptContext, true, false);
  555. }
  556. BOOL JavascriptOperators::RelationalComparisonHelper(Var aLeft, Var aRight, ScriptContext* scriptContext, bool leftFirst, bool undefinedAs)
  557. {
  558. TypeId typeId = JavascriptOperators::GetTypeId(aLeft);
  559. if (typeId == TypeIds_Null)
  560. {
  561. aLeft=TaggedInt::ToVarUnchecked(0);
  562. }
  563. else if (typeId == TypeIds_Undefined)
  564. {
  565. aLeft=scriptContext->GetLibrary()->GetNaN();
  566. }
  567. typeId = JavascriptOperators::GetTypeId(aRight);
  568. if (typeId == TypeIds_Null)
  569. {
  570. aRight=TaggedInt::ToVarUnchecked(0);
  571. }
  572. else if (typeId == TypeIds_Undefined)
  573. {
  574. aRight=scriptContext->GetLibrary()->GetNaN();
  575. }
  576. double dblLeft, dblRight;
  577. TypeId leftType = JavascriptOperators::GetTypeId(aLeft);
  578. TypeId rightType = JavascriptOperators::GetTypeId(aRight);
  579. if ((leftType == TypeIds_BigInt) || (rightType == TypeIds_BigInt))
  580. {
  581. // TODO: support comparison with types other than BigInt
  582. AssertOrFailFastMsg(leftType == rightType, "do not support comparison with types other than BigInt");
  583. return JavascriptBigInt::LessThan(aLeft, aRight);
  584. }
  585. switch (leftType)
  586. {
  587. case TypeIds_Integer:
  588. dblLeft = TaggedInt::ToDouble(aLeft);
  589. switch (rightType)
  590. {
  591. case TypeIds_Integer:
  592. dblRight = TaggedInt::ToDouble(aRight);
  593. break;
  594. case TypeIds_Number:
  595. dblRight = JavascriptNumber::GetValue(aRight);
  596. break;
  597. default:
  598. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  599. break;
  600. }
  601. break;
  602. case TypeIds_Number:
  603. dblLeft = JavascriptNumber::GetValue(aLeft);
  604. switch (rightType)
  605. {
  606. case TypeIds_Integer:
  607. dblRight = TaggedInt::ToDouble(aRight);
  608. break;
  609. case TypeIds_Number:
  610. dblRight = JavascriptNumber::GetValue(aRight);
  611. break;
  612. default:
  613. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  614. break;
  615. }
  616. break;
  617. case TypeIds_Int64Number:
  618. {
  619. switch (rightType)
  620. {
  621. case TypeIds_Int64Number:
  622. {
  623. __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  624. __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
  625. return leftValue < rightValue;
  626. }
  627. break;
  628. case TypeIds_UInt64Number:
  629. {
  630. __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  631. unsigned __int64 rightValue = UnsafeVarTo<JavascriptUInt64Number>(aRight)->GetValue();
  632. if (rightValue <= INT_MAX && leftValue >= 0)
  633. {
  634. return leftValue < (__int64)rightValue;
  635. }
  636. }
  637. break;
  638. }
  639. dblLeft = (double)UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  640. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  641. }
  642. break;
  643. // we cannot do double conversion between 2 int64 numbers as we can get wrong result after conversion
  644. // i.e., two different numbers become the same after losing precision. We'll continue dbl comparison
  645. // if either number is not an int64 number.
  646. case TypeIds_UInt64Number:
  647. {
  648. switch (rightType)
  649. {
  650. case TypeIds_Int64Number:
  651. {
  652. unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  653. __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
  654. if (leftValue < INT_MAX && rightValue >= 0)
  655. {
  656. return (__int64)leftValue < rightValue;
  657. }
  658. }
  659. break;
  660. case TypeIds_UInt64Number:
  661. {
  662. unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  663. unsigned __int64 rightValue = UnsafeVarTo<JavascriptUInt64Number>(aRight)->GetValue();
  664. return leftValue < rightValue;
  665. }
  666. break;
  667. }
  668. dblLeft = (double)UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  669. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  670. }
  671. break;
  672. case TypeIds_String:
  673. switch (rightType)
  674. {
  675. case TypeIds_Integer:
  676. case TypeIds_Number:
  677. case TypeIds_Boolean:
  678. break;
  679. default:
  680. aRight = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aRight, scriptContext);
  681. rightType = JavascriptOperators::GetTypeId(aRight);
  682. if (rightType != TypeIds_String)
  683. {
  684. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  685. break;
  686. }
  687. case TypeIds_String:
  688. return JavascriptString::LessThan(aLeft, aRight);
  689. }
  690. dblLeft = JavascriptConversion::ToNumber(aLeft, scriptContext);
  691. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  692. break;
  693. case TypeIds_Boolean:
  694. case TypeIds_Null:
  695. case TypeIds_Undefined:
  696. case TypeIds_Symbol:
  697. dblLeft = JavascriptConversion::ToNumber(aLeft, scriptContext);
  698. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  699. break;
  700. default:
  701. if (leftFirst)
  702. {
  703. aLeft = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aLeft, scriptContext);
  704. aRight = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aRight, scriptContext);
  705. }
  706. else
  707. {
  708. aRight = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aRight, scriptContext);
  709. aLeft = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aLeft, scriptContext);
  710. }
  711. //BugFix: When @@ToPrimitive of an object is overridden with a function that returns null/undefined
  712. //this helper will fall into a inescapable goto loop as the checks for null/undefined were outside of the path
  713. return RelationalComparisonHelper(aLeft, aRight, scriptContext, leftFirst, undefinedAs);
  714. }
  715. //
  716. // And +0,-0 that is not implemented fully
  717. //
  718. if (JavascriptNumber::IsNan(dblLeft) || JavascriptNumber::IsNan(dblRight))
  719. {
  720. return undefinedAs;
  721. }
  722. // this will succeed for -0.0 == 0.0 case as well
  723. if (dblLeft == dblRight)
  724. {
  725. return false;
  726. }
  727. return dblLeft < dblRight;
  728. }
  729. BOOL JavascriptOperators::StrictEqualString(Var aLeft, JavascriptString* aRight)
  730. {
  731. JIT_HELPER_REENTRANT_HEADER(Op_StrictEqualString);
  732. JIT_HELPER_SAME_ATTRIBUTES(Op_StrictEqualString, Op_StrictEqual);
  733. JavascriptString* leftStr = TryFromVar<JavascriptString>(aLeft);
  734. if (!leftStr)
  735. {
  736. return false;
  737. }
  738. JIT_HELPER_REENTRANT_HEADER(Op_StrictEqualString);
  739. JIT_HELPER_SAME_ATTRIBUTES(Op_StrictEqualString, Op_StrictEqual);
  740. return JavascriptString::Equals(leftStr, aRight);
  741. JIT_HELPER_END(Op_StrictEqualString);
  742. }
  743. BOOL JavascriptOperators::StrictEqualEmptyString(Var aLeft)
  744. {
  745. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_StrictEqualEmptyString);
  746. JavascriptString * string = JavascriptOperators::TryFromVar<JavascriptString>(aLeft);
  747. if (!string)
  748. {
  749. return false;
  750. }
  751. Assert(string);
  752. return string->GetLength() == 0;
  753. JIT_HELPER_END(Op_StrictEqualEmptyString);
  754. }
  755. #ifdef _CHAKRACOREBUILD
  756. BOOL JavascriptOperators::StrictEqualNumberType(Var aLeft, Var aRight, TypeId leftType, TypeId rightType, ScriptContext *requestContext)
  757. {
  758. double dblLeft, dblRight;
  759. switch (leftType)
  760. {
  761. case TypeIds_Integer:
  762. switch (rightType)
  763. {
  764. case TypeIds_Integer:
  765. return aLeft == aRight;
  766. // we don't need to worry about int64: it cannot equal as we create
  767. // JavascriptInt64Number only in overflow scenarios.
  768. case TypeIds_Number:
  769. dblLeft = TaggedInt::ToDouble(aLeft);
  770. dblRight = JavascriptNumber::GetValue(aRight);
  771. goto CommonNumber;
  772. }
  773. return FALSE;
  774. case TypeIds_Int64Number:
  775. switch (rightType)
  776. {
  777. case TypeIds_Int64Number:
  778. {
  779. __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  780. __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
  781. return leftValue == rightValue;
  782. }
  783. case TypeIds_UInt64Number:
  784. {
  785. __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  786. unsigned __int64 rightValue = VarTo<JavascriptUInt64Number>(aRight)->GetValue();
  787. return ((unsigned __int64)leftValue == rightValue);
  788. }
  789. case TypeIds_Number:
  790. dblLeft = (double)UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  791. dblRight = JavascriptNumber::GetValue(aRight);
  792. goto CommonNumber;
  793. }
  794. return FALSE;
  795. case TypeIds_UInt64Number:
  796. switch (rightType)
  797. {
  798. case TypeIds_Int64Number:
  799. {
  800. unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  801. __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
  802. return (leftValue == (unsigned __int64)rightValue);
  803. }
  804. case TypeIds_UInt64Number:
  805. {
  806. unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  807. unsigned __int64 rightValue = VarTo<JavascriptUInt64Number>(aRight)->GetValue();
  808. return leftValue == rightValue;
  809. }
  810. case TypeIds_Number:
  811. dblLeft = (double)UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  812. dblRight = JavascriptNumber::GetValue(aRight);
  813. goto CommonNumber;
  814. }
  815. return FALSE;
  816. case TypeIds_Number:
  817. switch (rightType)
  818. {
  819. case TypeIds_Integer:
  820. dblLeft = JavascriptNumber::GetValue(aLeft);
  821. dblRight = TaggedInt::ToDouble(aRight);
  822. goto CommonNumber;
  823. case TypeIds_Int64Number:
  824. dblLeft = JavascriptNumber::GetValue(aLeft);
  825. dblRight = (double)VarTo<JavascriptInt64Number>(aRight)->GetValue();
  826. goto CommonNumber;
  827. case TypeIds_UInt64Number:
  828. dblLeft = JavascriptNumber::GetValue(aLeft);
  829. dblRight = (double)UnsafeVarTo<JavascriptUInt64Number>(aRight)->GetValue();
  830. goto CommonNumber;
  831. case TypeIds_Number:
  832. dblLeft = JavascriptNumber::GetValue(aLeft);
  833. dblRight = JavascriptNumber::GetValue(aRight);
  834. CommonNumber:
  835. return FEqualDbl(dblLeft, dblRight);
  836. }
  837. return FALSE;
  838. }
  839. Assert(0 && "Unreachable Code");
  840. return FALSE;
  841. }
  842. BOOL JavascriptOperators::StrictEqual(Var aLeft, Var aRight, ScriptContext* requestContext)
  843. {
  844. JIT_HELPER_REENTRANT_HEADER(Op_StrictEqual);
  845. TypeId rightType, leftType;
  846. leftType = JavascriptOperators::GetTypeId(aLeft);
  847. // Because NaN !== NaN, we may not return TRUE when typeId is Number
  848. if (aLeft == aRight && leftType != TypeIds_Number) return TRUE;
  849. rightType = JavascriptOperators::GetTypeId(aRight);
  850. if (leftType == TypeIds_String)
  851. {
  852. if (rightType == TypeIds_String)
  853. {
  854. return JavascriptString::Equals(UnsafeVarTo<JavascriptString>(aLeft), UnsafeVarTo<JavascriptString>(aRight));
  855. }
  856. return FALSE;
  857. }
  858. else if (leftType >= TypeIds_Integer && leftType <= TypeIds_UInt64Number)
  859. {
  860. return JavascriptOperators::StrictEqualNumberType(aLeft, aRight, leftType, rightType, requestContext);
  861. }
  862. else if (leftType == TypeIds_GlobalObject)
  863. {
  864. BOOL result;
  865. if (UnsafeVarTo<RecyclableObject>(aLeft)->StrictEquals(aRight, &result, requestContext))
  866. {
  867. return result;
  868. }
  869. return false;
  870. }
  871. else if (leftType == TypeIds_BigInt)
  872. {
  873. if (rightType == TypeIds_BigInt)
  874. {
  875. return JavascriptBigInt::Equals(aLeft, aRight);
  876. }
  877. return FALSE;
  878. }
  879. return aLeft == aRight;
  880. JIT_HELPER_END(Op_StrictEqual);
  881. }
  882. #else
  883. BOOL JavascriptOperators::StrictEqual(Var aLeft, Var aRight, ScriptContext* requestContext)
  884. {
  885. JIT_HELPER_REENTRANT_HEADER(Op_StrictEqual);
  886. double dblLeft, dblRight;
  887. TypeId rightType, leftType;
  888. leftType = JavascriptOperators::GetTypeId(aLeft);
  889. // Because NaN !== NaN, we may not return TRUE when typeId is Number
  890. if (aLeft == aRight && leftType != TypeIds_Number) return TRUE;
  891. rightType = JavascriptOperators::GetTypeId(aRight);
  892. switch (leftType)
  893. {
  894. case TypeIds_String:
  895. switch (rightType)
  896. {
  897. case TypeIds_String:
  898. return JavascriptString::Equals(UnsafeVarTo<JavascriptString>(aLeft), UnsafeVarTo<JavascriptString>(aRight));
  899. }
  900. return FALSE;
  901. case TypeIds_Integer:
  902. switch (rightType)
  903. {
  904. case TypeIds_Integer:
  905. return aLeft == aRight;
  906. // we don't need to worry about int64: it cannot equal as we create
  907. // JavascriptInt64Number only in overflow scenarios.
  908. case TypeIds_Number:
  909. dblLeft = TaggedInt::ToDouble(aLeft);
  910. dblRight = JavascriptNumber::GetValue(aRight);
  911. goto CommonNumber;
  912. }
  913. return FALSE;
  914. case TypeIds_Int64Number:
  915. switch (rightType)
  916. {
  917. case TypeIds_Int64Number:
  918. {
  919. __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  920. __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
  921. return leftValue == rightValue;
  922. }
  923. case TypeIds_UInt64Number:
  924. {
  925. __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  926. unsigned __int64 rightValue = VarTo<JavascriptUInt64Number>(aRight)->GetValue();
  927. return ((unsigned __int64)leftValue == rightValue);
  928. }
  929. case TypeIds_Number:
  930. dblLeft = (double)UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  931. dblRight = JavascriptNumber::GetValue(aRight);
  932. goto CommonNumber;
  933. }
  934. return FALSE;
  935. case TypeIds_UInt64Number:
  936. switch (rightType)
  937. {
  938. case TypeIds_Int64Number:
  939. {
  940. unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  941. __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
  942. return (leftValue == (unsigned __int64)rightValue);
  943. }
  944. case TypeIds_UInt64Number:
  945. {
  946. unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  947. unsigned __int64 rightValue = VarTo<JavascriptUInt64Number>(aRight)->GetValue();
  948. return leftValue == rightValue;
  949. }
  950. case TypeIds_Number:
  951. dblLeft = (double)UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  952. dblRight = JavascriptNumber::GetValue(aRight);
  953. goto CommonNumber;
  954. }
  955. return FALSE;
  956. case TypeIds_Number:
  957. switch (rightType)
  958. {
  959. case TypeIds_Integer:
  960. dblLeft = JavascriptNumber::GetValue(aLeft);
  961. dblRight = TaggedInt::ToDouble(aRight);
  962. goto CommonNumber;
  963. case TypeIds_Int64Number:
  964. dblLeft = JavascriptNumber::GetValue(aLeft);
  965. dblRight = (double)VarTo<JavascriptInt64Number>(aRight)->GetValue();
  966. goto CommonNumber;
  967. case TypeIds_UInt64Number:
  968. dblLeft = JavascriptNumber::GetValue(aLeft);
  969. dblRight = (double)UnsafeVarTo<JavascriptUInt64Number>(aRight)->GetValue();
  970. goto CommonNumber;
  971. case TypeIds_Number:
  972. dblLeft = JavascriptNumber::GetValue(aLeft);
  973. dblRight = JavascriptNumber::GetValue(aRight);
  974. CommonNumber:
  975. return FEqualDbl(dblLeft, dblRight);
  976. }
  977. return FALSE;
  978. case TypeIds_BigInt:
  979. switch (rightType)
  980. {
  981. case TypeIds_BigInt:
  982. return JavascriptBigInt::Equals(aLeft, aRight);
  983. }
  984. return FALSE;
  985. case TypeIds_Boolean:
  986. switch (rightType)
  987. {
  988. case TypeIds_Boolean:
  989. return aLeft == aRight;
  990. }
  991. return FALSE;
  992. case TypeIds_Undefined:
  993. return rightType == TypeIds_Undefined;
  994. case TypeIds_Null:
  995. return rightType == TypeIds_Null;
  996. case TypeIds_Array:
  997. return (rightType == TypeIds_Array && aLeft == aRight);
  998. #if DBG
  999. case TypeIds_Symbol:
  1000. if (rightType == TypeIds_Symbol)
  1001. {
  1002. const PropertyRecord* leftValue = UnsafeVarTo<JavascriptSymbol>(aLeft)->GetValue();
  1003. const PropertyRecord* rightValue = UnsafeVarTo<JavascriptSymbol>(aRight)->GetValue();
  1004. Assert(leftValue != rightValue);
  1005. }
  1006. break;
  1007. #endif
  1008. case TypeIds_GlobalObject:
  1009. case TypeIds_HostDispatch:
  1010. switch (rightType)
  1011. {
  1012. case TypeIds_HostDispatch:
  1013. case TypeIds_GlobalObject:
  1014. {
  1015. BOOL result;
  1016. if(UnsafeVarTo<RecyclableObject>(aLeft)->StrictEquals(aRight, &result, requestContext))
  1017. {
  1018. return result;
  1019. }
  1020. return false;
  1021. }
  1022. }
  1023. break;
  1024. }
  1025. if (VarTo<RecyclableObject>(aLeft)->IsExternal())
  1026. {
  1027. BOOL result;
  1028. if (VarTo<RecyclableObject>(aLeft)->StrictEquals(aRight, &result, requestContext))
  1029. {
  1030. if (result)
  1031. {
  1032. return TRUE;
  1033. }
  1034. }
  1035. }
  1036. if (!TaggedNumber::Is(aRight) && VarTo<RecyclableObject>(aRight)->IsExternal())
  1037. {
  1038. BOOL result;
  1039. if (VarTo<RecyclableObject>(aRight)->StrictEquals(aLeft, &result, requestContext))
  1040. {
  1041. if (result)
  1042. {
  1043. return TRUE;
  1044. }
  1045. }
  1046. }
  1047. return aLeft == aRight;
  1048. JIT_HELPER_END(Op_StrictEqual);
  1049. }
  1050. #endif
  1051. BOOL JavascriptOperators::HasOwnProperty(
  1052. Var instance,
  1053. PropertyId propertyId,
  1054. _In_ ScriptContext* requestContext,
  1055. _In_opt_ PropertyString* propString)
  1056. {
  1057. if (TaggedNumber::Is(instance))
  1058. {
  1059. return FALSE;
  1060. }
  1061. RecyclableObject* object = UnsafeVarTo<RecyclableObject>(instance);
  1062. if (VarIs<JavascriptProxy>(instance))
  1063. {
  1064. PropertyDescriptor desc;
  1065. return GetOwnPropertyDescriptor(object, propertyId, requestContext, &desc);
  1066. }
  1067. // If we have a PropertyString, attempt to shortcut the lookup by using its caches
  1068. if (propString != nullptr)
  1069. {
  1070. PropertyCacheOperationInfo info;
  1071. if (propString->GetLdElemInlineCache()->PretendTryGetProperty(object->GetType(), &info))
  1072. {
  1073. switch (info.cacheType)
  1074. {
  1075. case CacheType_Local:
  1076. Assert(object->HasOwnProperty(propertyId));
  1077. return TRUE;
  1078. case CacheType_Proto:
  1079. Assert(!object->HasOwnProperty(propertyId));
  1080. return FALSE;
  1081. default:
  1082. // We had a cache hit, but cache doesn't tell us if we have an own property
  1083. break;
  1084. }
  1085. }
  1086. if (propString->GetStElemInlineCache()->PretendTrySetProperty(object->GetType(), object->GetType(), &info))
  1087. {
  1088. switch (info.cacheType)
  1089. {
  1090. case CacheType_Local:
  1091. Assert(object->HasOwnProperty(propertyId));
  1092. return TRUE;
  1093. case CacheType_LocalWithoutProperty:
  1094. Assert(!object->HasOwnProperty(propertyId));
  1095. return FALSE;
  1096. default:
  1097. // We had a cache hit, but cache doesn't tell us if we have an own property
  1098. break;
  1099. }
  1100. }
  1101. }
  1102. return object && object->HasOwnProperty(propertyId);
  1103. }
  1104. BOOL JavascriptOperators::GetOwnAccessors(Var instance, PropertyId propertyId, Var* getter, Var* setter, ScriptContext * requestContext)
  1105. {
  1106. BOOL result;
  1107. if (TaggedNumber::Is(instance))
  1108. {
  1109. result = false;
  1110. }
  1111. else
  1112. {
  1113. RecyclableObject* object = UnsafeVarTo<RecyclableObject>(instance);
  1114. result = object && object->GetAccessors(propertyId, getter, setter, requestContext);
  1115. }
  1116. return result;
  1117. }
  1118. JavascriptArray* JavascriptOperators::GetOwnPropertyNames(Var instance, ScriptContext *scriptContext)
  1119. {
  1120. RecyclableObject *object = ToObject(instance, scriptContext);
  1121. AssertOrFailFast(VarIsCorrectType(object)); // Consider moving this check into ToObject
  1122. JavascriptProxy * proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  1123. if (proxy)
  1124. {
  1125. return proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::GetOwnPropertyNamesKind, scriptContext);
  1126. }
  1127. return JavascriptObject::CreateOwnStringPropertiesHelper(object, scriptContext);
  1128. }
  1129. JavascriptArray* JavascriptOperators::GetOwnPropertySymbols(Var instance, ScriptContext *scriptContext)
  1130. {
  1131. RecyclableObject *object = ToObject(instance, scriptContext);
  1132. AssertOrFailFast(VarIsCorrectType(object));
  1133. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_getOwnPropertySymbols);
  1134. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  1135. if (proxy)
  1136. {
  1137. return proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::GetOwnPropertySymbolKind, scriptContext);
  1138. }
  1139. return JavascriptObject::CreateOwnSymbolPropertiesHelper(object, scriptContext);
  1140. }
  1141. JavascriptArray* JavascriptOperators::GetOwnPropertyKeys(Var instance, ScriptContext* scriptContext)
  1142. {
  1143. RecyclableObject *object = ToObject(instance, scriptContext);
  1144. AssertOrFailFast(VarIsCorrectType(object));
  1145. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  1146. if (proxy)
  1147. {
  1148. return proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::KeysKind, scriptContext);
  1149. }
  1150. return JavascriptObject::CreateOwnStringSymbolPropertiesHelper(object, scriptContext);
  1151. }
  1152. JavascriptArray* JavascriptOperators::GetOwnEnumerablePropertyNames(RecyclableObject* object, ScriptContext* scriptContext)
  1153. {
  1154. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(object);
  1155. if (proxy)
  1156. {
  1157. JavascriptArray* proxyResult = proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::GetOwnPropertyNamesKind, scriptContext);
  1158. JavascriptArray* proxyResultToReturn = scriptContext->GetLibrary()->CreateArray(0);
  1159. // filter enumerable keys
  1160. uint32 resultLength = proxyResult->GetLength();
  1161. Var element;
  1162. const Js::PropertyRecord *propertyRecord = nullptr;
  1163. uint32 index = 0;
  1164. for (uint32 i = 0; i < resultLength; i++)
  1165. {
  1166. element = proxyResult->DirectGetItem(i);
  1167. Assert(!VarIs<JavascriptSymbol>(element));
  1168. PropertyDescriptor propertyDescriptor;
  1169. JavascriptConversion::ToPropertyKey(element, scriptContext, &propertyRecord, nullptr);
  1170. if (JavascriptOperators::GetOwnPropertyDescriptor(object, propertyRecord->GetPropertyId(), scriptContext, &propertyDescriptor))
  1171. {
  1172. if (propertyDescriptor.IsEnumerable())
  1173. {
  1174. proxyResultToReturn->DirectSetItemAt(index++, CrossSite::MarshalVar(scriptContext, element));
  1175. }
  1176. }
  1177. }
  1178. return proxyResultToReturn;
  1179. }
  1180. return JavascriptObject::CreateOwnEnumerableStringPropertiesHelper(object, scriptContext);
  1181. }
  1182. JavascriptArray* JavascriptOperators::GetOwnEnumerablePropertyNamesSymbols(RecyclableObject* object, ScriptContext* scriptContext)
  1183. {
  1184. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(object);
  1185. if (proxy)
  1186. {
  1187. return proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::KeysKind, scriptContext);
  1188. }
  1189. return JavascriptObject::CreateOwnEnumerableStringSymbolPropertiesHelper(object, scriptContext);
  1190. }
  1191. BOOL JavascriptOperators::GetOwnProperty(Var instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo * propertyValueInfo)
  1192. {
  1193. BOOL result;
  1194. if (TaggedNumber::Is(instance))
  1195. {
  1196. result = false;
  1197. }
  1198. else
  1199. {
  1200. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  1201. result = object && object->GetProperty(object, propertyId, value, propertyValueInfo, requestContext);
  1202. if (propertyValueInfo && result)
  1203. {
  1204. // We can only update the cache in case a property was found, because if it wasn't found, we don't know if it is missing or on a prototype
  1205. CacheOperators::CachePropertyRead(instance, object, false /* isRoot */, propertyId, false /* isMissing */, propertyValueInfo, requestContext);
  1206. }
  1207. }
  1208. return result;
  1209. }
  1210. BOOL JavascriptOperators::GetOwnPropertyDescriptor(RecyclableObject* obj, JavascriptString* propertyKey, ScriptContext* scriptContext, PropertyDescriptor* propertyDescriptor)
  1211. {
  1212. return JavascriptOperators::GetOwnPropertyDescriptor(obj, JavascriptOperators::GetPropertyId(propertyKey, scriptContext), scriptContext, propertyDescriptor);
  1213. }
  1214. // ES5's [[GetOwnProperty]].
  1215. // Return value:
  1216. // FALSE means "undefined" PD.
  1217. // TRUE means success. The propertyDescriptor parameter gets the descriptor.
  1218. //
  1219. BOOL JavascriptOperators::GetOwnPropertyDescriptor(RecyclableObject* obj, PropertyId propertyId, ScriptContext* scriptContext, PropertyDescriptor* propertyDescriptor)
  1220. {
  1221. Assert(obj);
  1222. Assert(scriptContext);
  1223. Assert(propertyDescriptor);
  1224. if (VarIs<JavascriptProxy>(obj))
  1225. {
  1226. return JavascriptProxy::GetOwnPropertyDescriptor(obj, propertyId, scriptContext, propertyDescriptor);
  1227. }
  1228. Var getter, setter;
  1229. if (false == JavascriptOperators::GetOwnAccessors(obj, propertyId, &getter, &setter, scriptContext))
  1230. {
  1231. Var value = nullptr;
  1232. if (false == JavascriptOperators::GetOwnProperty(obj, propertyId, &value, scriptContext, nullptr))
  1233. {
  1234. return FALSE;
  1235. }
  1236. if (nullptr != value)
  1237. {
  1238. propertyDescriptor->SetValue(value);
  1239. }
  1240. //CONSIDER : Its expensive to query for each flag from type system. Combine this with the GetOwnProperty to get all the flags
  1241. //at once. This will require a new API from type system and override in all the types which overrides IsEnumerable etc.
  1242. //Currently there is no performance tuning for ES5. This should be ok.
  1243. propertyDescriptor->SetWritable(FALSE != obj->IsWritable(propertyId));
  1244. }
  1245. else
  1246. {
  1247. if (nullptr == getter)
  1248. {
  1249. getter = scriptContext->GetLibrary()->GetUndefined();
  1250. }
  1251. propertyDescriptor->SetGetter(getter);
  1252. if (nullptr == setter)
  1253. {
  1254. setter = scriptContext->GetLibrary()->GetUndefined();
  1255. }
  1256. propertyDescriptor->SetSetter(setter);
  1257. }
  1258. propertyDescriptor->SetConfigurable(FALSE != obj->IsConfigurable(propertyId));
  1259. propertyDescriptor->SetEnumerable(FALSE != obj->IsEnumerable(propertyId));
  1260. return TRUE;
  1261. }
  1262. inline RecyclableObject* JavascriptOperators::GetPrototypeNoTrap(RecyclableObject* instance)
  1263. {
  1264. Type* type = instance->GetType();
  1265. if (type->HasSpecialPrototype())
  1266. {
  1267. if (type->GetTypeId() == TypeIds_Proxy)
  1268. {
  1269. // get back null
  1270. Assert(type->GetPrototype() == instance->GetScriptContext()->GetLibrary()->GetNull());
  1271. return type->GetPrototype();
  1272. }
  1273. else
  1274. {
  1275. return instance->GetPrototypeSpecial();
  1276. }
  1277. }
  1278. return type->GetPrototype();
  1279. }
  1280. BOOL JavascriptOperators::IsRemoteArray(RecyclableObject* instance)
  1281. {
  1282. TypeId remoteTypeId = TypeIds_Limit;
  1283. return (JavascriptOperators::GetRemoteTypeId(instance, &remoteTypeId) &&
  1284. DynamicObject::IsAnyArrayTypeId(remoteTypeId));
  1285. }
  1286. bool JavascriptOperators::IsArray(_In_ JavascriptProxy * instance)
  1287. {
  1288. // If it is a proxy, follow to the end of the proxy chain before checking if it is an array again.
  1289. JavascriptProxy * proxy = instance;
  1290. while (true)
  1291. {
  1292. RecyclableObject * targetInstance = proxy->GetTarget();
  1293. proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(targetInstance);
  1294. if (proxy == nullptr)
  1295. {
  1296. return DynamicObject::IsAnyArray(targetInstance) || IsRemoteArray(targetInstance);
  1297. }
  1298. }
  1299. }
  1300. bool JavascriptOperators::IsArray(_In_ RecyclableObject* instance)
  1301. {
  1302. if (DynamicObject::IsAnyArray(instance))
  1303. {
  1304. return TRUE;
  1305. }
  1306. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  1307. if (proxy)
  1308. {
  1309. return IsArray(proxy);
  1310. }
  1311. return IsRemoteArray(instance);
  1312. }
  1313. bool JavascriptOperators::IsArray(_In_ Var instanceVar)
  1314. {
  1315. RecyclableObject* instanceObj = TryFromVar<RecyclableObject>(instanceVar);
  1316. return instanceObj && IsArray(instanceObj);
  1317. }
  1318. bool JavascriptOperators::IsConstructor(_In_ JavascriptProxy * instance)
  1319. {
  1320. // If it is a proxy, follow to the end of the proxy chain before checking if it is a constructor again.
  1321. JavascriptProxy * proxy = instance;
  1322. while (true)
  1323. {
  1324. RecyclableObject* targetInstance = proxy->GetTarget();
  1325. proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(targetInstance);
  1326. if (proxy == nullptr)
  1327. {
  1328. JavascriptFunction* function = JavascriptOperators::TryFromVar<JavascriptFunction>(targetInstance);
  1329. return function && function->IsConstructor();
  1330. }
  1331. }
  1332. }
  1333. bool JavascriptOperators::IsConstructor(_In_ RecyclableObject* instanceObj)
  1334. {
  1335. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instanceObj);
  1336. if (proxy)
  1337. {
  1338. return IsConstructor(proxy);
  1339. }
  1340. JavascriptFunction* function = JavascriptOperators::TryFromVar<JavascriptFunction>(instanceObj);
  1341. return function && function->IsConstructor();
  1342. }
  1343. bool JavascriptOperators::IsConstructor(_In_ Var instanceVar)
  1344. {
  1345. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_IsConstructor);
  1346. RecyclableObject* instanceObj = TryFromVar<RecyclableObject>(instanceVar);
  1347. return instanceObj && IsConstructor(instanceObj);
  1348. JIT_HELPER_END(Op_IsConstructor);
  1349. }
  1350. BOOL JavascriptOperators::IsConcatSpreadable(Var instanceVar)
  1351. {
  1352. // an object is spreadable under two condition, either it is a JsArray
  1353. // or you define an isconcatSpreadable flag on it.
  1354. if (!JavascriptOperators::IsObject(instanceVar))
  1355. {
  1356. return false;
  1357. }
  1358. RecyclableObject* instance = UnsafeVarTo<RecyclableObject>(instanceVar);
  1359. ScriptContext* scriptContext = instance->GetScriptContext();
  1360. if (!PHASE_OFF1(IsConcatSpreadableCachePhase))
  1361. {
  1362. BOOL retVal = FALSE;
  1363. Type *instanceType = instance->GetType();
  1364. IsConcatSpreadableCache *isConcatSpreadableCache = scriptContext->GetThreadContext()->GetIsConcatSpreadableCache();
  1365. if (isConcatSpreadableCache->TryGetIsConcatSpreadable(instanceType, &retVal))
  1366. {
  1367. OUTPUT_TRACE(Phase::IsConcatSpreadableCachePhase, _u("IsConcatSpreadableCache hit: %p\n"), instanceType);
  1368. return retVal;
  1369. }
  1370. Var spreadable = nullptr;
  1371. BOOL hasUserDefinedSpreadable = JavascriptOperators::GetProperty(instance, instance, PropertyIds::_symbolIsConcatSpreadable, &spreadable, scriptContext);
  1372. if (hasUserDefinedSpreadable && spreadable != scriptContext->GetLibrary()->GetUndefined())
  1373. {
  1374. return JavascriptConversion::ToBoolean(spreadable, scriptContext);
  1375. }
  1376. retVal = JavascriptOperators::IsArray(instance);
  1377. if (!hasUserDefinedSpreadable)
  1378. {
  1379. OUTPUT_TRACE(Phase::IsConcatSpreadableCachePhase, _u("IsConcatSpreadableCache saved: %p\n"), instanceType);
  1380. isConcatSpreadableCache->CacheIsConcatSpreadable(instanceType, retVal);
  1381. }
  1382. return retVal;
  1383. }
  1384. Var spreadable = JavascriptOperators::GetProperty(instance, PropertyIds::_symbolIsConcatSpreadable, scriptContext);
  1385. if (spreadable != scriptContext->GetLibrary()->GetUndefined())
  1386. {
  1387. return JavascriptConversion::ToBoolean(spreadable, scriptContext);
  1388. }
  1389. return JavascriptOperators::IsArray(instance);
  1390. }
  1391. bool JavascriptOperators::IsConstructorSuperCall(Arguments args)
  1392. {
  1393. Var newTarget = args.GetNewTarget();
  1394. return args.IsNewCall() && newTarget != nullptr
  1395. && !JavascriptOperators::IsUndefined(newTarget);
  1396. }
  1397. bool JavascriptOperators::GetAndAssertIsConstructorSuperCall(Arguments args)
  1398. {
  1399. bool isCtorSuperCall = JavascriptOperators::IsConstructorSuperCall(args);
  1400. Assert(isCtorSuperCall || !args.IsNewCall()
  1401. || args[0] == nullptr || JavascriptOperators::GetTypeId(args[0]) == TypeIds_HostDispatch);
  1402. return isCtorSuperCall;
  1403. }
  1404. Var JavascriptOperators::OP_LdCustomSpreadIteratorList(Var aRight, ScriptContext* scriptContext)
  1405. {
  1406. JIT_HELPER_REENTRANT_HEADER(Op_ToSpreadedFunctionArgument);
  1407. #if ENABLE_COPYONACCESS_ARRAY
  1408. // We know we're going to read from this array. Do the conversion before we try to perform checks on the head segment.
  1409. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray(aRight);
  1410. #endif
  1411. #ifdef ENABLE_JS_BUILTINS
  1412. scriptContext->GetLibrary()->EnsureBuiltInEngineIsReady();
  1413. #endif
  1414. RecyclableObject* function = GetIteratorFunction(aRight, scriptContext);
  1415. JavascriptMethod method = function->GetEntryPoint();
  1416. if (((JavascriptArray::IsNonES5Array(aRight) &&
  1417. (
  1418. JavascriptLibrary::IsDefaultArrayValuesFunction(function, scriptContext)
  1419. // Verify that the head segment of the array covers all elements with no gaps.
  1420. // Accessing an element on the prototype could have side-effects that would invalidate the optimization.
  1421. && UnsafeVarTo<JavascriptArray>(aRight)->GetHead()->next == nullptr
  1422. && UnsafeVarTo<JavascriptArray>(aRight)->GetHead()->left == 0
  1423. && UnsafeVarTo<JavascriptArray>(aRight)->GetHead()->length == VarTo<JavascriptArray>(aRight)->GetLength()
  1424. && UnsafeVarTo<JavascriptArray>(aRight)->HasNoMissingValues()
  1425. && !UnsafeVarTo<JavascriptArray>(aRight)->IsCrossSiteObject()
  1426. )) ||
  1427. (VarIs<TypedArrayBase>(aRight) && method == TypedArrayBase::EntryInfo::Values.GetOriginalEntryPoint()))
  1428. // We can't optimize away the iterator if the array iterator prototype is user defined.
  1429. && !JavascriptLibrary::ArrayIteratorPrototypeHasUserDefinedNext(scriptContext))
  1430. {
  1431. return RecyclerNew(scriptContext->GetRecycler(), SpreadArgument, aRight, true /*useDirectCall*/, scriptContext->GetLibrary()->GetSpreadArgumentType());
  1432. }
  1433. ThreadContext *threadContext = scriptContext->GetThreadContext();
  1434. Var iteratorVar =
  1435. threadContext->ExecuteImplicitCall(function, ImplicitCall_Accessor, [=]() -> Var
  1436. {
  1437. return CALL_FUNCTION(threadContext, function, CallInfo(Js::CallFlags_Value, 1), aRight);
  1438. });
  1439. if (!JavascriptOperators::IsObject(iteratorVar))
  1440. {
  1441. if (!threadContext->RecordImplicitException())
  1442. {
  1443. return scriptContext->GetLibrary()->GetUndefined();
  1444. }
  1445. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  1446. }
  1447. return RecyclerNew(scriptContext->GetRecycler(), SpreadArgument, iteratorVar, false /*useDirectCall*/, scriptContext->GetLibrary()->GetSpreadArgumentType());
  1448. JIT_HELPER_END(Op_ToSpreadedFunctionArgument);
  1449. }
  1450. BOOL JavascriptOperators::IsPropertyUnscopable(Var instanceVar, JavascriptString *propertyString)
  1451. {
  1452. // This never gets called.
  1453. Throw::InternalError();
  1454. }
  1455. BOOL JavascriptOperators::IsPropertyUnscopable(Var instanceVar, PropertyId propertyId)
  1456. {
  1457. RecyclableObject* instance = VarTo<RecyclableObject>(instanceVar);
  1458. ScriptContext * scriptContext = instance->GetScriptContext();
  1459. Var unscopables = JavascriptOperators::GetProperty(instance, PropertyIds::_symbolUnscopables, scriptContext);
  1460. if (JavascriptOperators::IsObject(unscopables))
  1461. {
  1462. DynamicObject *unscopablesList = VarTo<DynamicObject>(unscopables);
  1463. Var value = nullptr;
  1464. //8.1.1.2.1.9.c If blocked is not undefined
  1465. if (JavascriptOperators::GetProperty(unscopablesList, propertyId, &value, scriptContext))
  1466. {
  1467. return JavascriptConversion::ToBoolean(value, scriptContext);
  1468. }
  1469. }
  1470. return false;
  1471. }
  1472. BOOL JavascriptOperators::HasProperty(RecyclableObject* instance, PropertyId propertyId)
  1473. {
  1474. while (!JavascriptOperators::IsNull(instance))
  1475. {
  1476. PropertyQueryFlags result = instance->HasPropertyQuery(propertyId, nullptr /*info*/);
  1477. if (result != PropertyQueryFlags::Property_NotFound)
  1478. {
  1479. return JavascriptConversion::PropertyQueryFlagsToBoolean(result); // return false if instance is typed array and HasPropertyQuery() returns PropertyQueryFlags::Property_Found_Undefined
  1480. }
  1481. instance = JavascriptOperators::GetPrototypeNoTrap(instance);
  1482. }
  1483. return false;
  1484. }
  1485. BOOL JavascriptOperators::HasPropertyUnscopables(RecyclableObject* instance, PropertyId propertyId)
  1486. {
  1487. return JavascriptOperators::HasProperty(instance, propertyId)
  1488. && !IsPropertyUnscopable(instance, propertyId);
  1489. }
  1490. BOOL JavascriptOperators::HasRootProperty(RecyclableObject* instance, PropertyId propertyId)
  1491. {
  1492. Assert(VarIs<RootObjectBase>(instance));
  1493. RootObjectBase* rootObject = static_cast<RootObjectBase*>(instance);
  1494. if (rootObject->HasRootProperty(propertyId))
  1495. {
  1496. return true;
  1497. }
  1498. instance = instance->GetPrototype();
  1499. return HasProperty(instance, propertyId);
  1500. }
  1501. BOOL JavascriptOperators::HasProxyOrPrototypeInlineCacheProperty(RecyclableObject* instance, PropertyId propertyId)
  1502. {
  1503. TypeId typeId;
  1504. typeId = JavascriptOperators::GetTypeId(instance);
  1505. if (typeId == Js::TypeIds_Proxy)
  1506. {
  1507. // let's be more aggressive to disable inline prototype cache when proxy is presented in the prototypechain
  1508. return true;
  1509. }
  1510. do
  1511. {
  1512. instance = instance->GetPrototype();
  1513. typeId = JavascriptOperators::GetTypeId(instance);
  1514. if (typeId == Js::TypeIds_Proxy)
  1515. {
  1516. // let's be more aggressive to disable inline prototype cache when proxy is presented in the prototypechain
  1517. return true;
  1518. }
  1519. if (typeId == TypeIds_Null)
  1520. {
  1521. break;
  1522. }
  1523. /* We can rule out object with deferred type handler, because they would have expanded if they are in the cache */
  1524. if (!instance->HasDeferredTypeHandler() && instance->HasProperty(propertyId)) { return true; }
  1525. } while (typeId != TypeIds_Null);
  1526. return false;
  1527. }
  1528. BOOL JavascriptOperators::OP_HasProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  1529. {
  1530. JIT_HELPER_REENTRANT_HEADER(Op_HasProperty);
  1531. RecyclableObject* object = TaggedNumber::Is(instance) ?
  1532. scriptContext->GetLibrary()->GetNumberPrototype() :
  1533. VarTo<RecyclableObject>(instance);
  1534. BOOL result = HasProperty(object, propertyId);
  1535. return result;
  1536. JIT_HELPER_END(Op_HasProperty);
  1537. }
  1538. BOOL JavascriptOperators::OP_HasOwnProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext, _In_opt_ PropertyString * propString)
  1539. {
  1540. RecyclableObject* object = TaggedNumber::Is(instance) ?
  1541. scriptContext->GetLibrary()->GetNumberPrototype() :
  1542. VarTo<RecyclableObject>(instance);
  1543. BOOL result = HasOwnProperty(object, propertyId, scriptContext, propString);
  1544. return result;
  1545. }
  1546. // CONSIDER: Have logic similar to HasOwnPropertyNoHostObjectForHeapEnum
  1547. BOOL JavascriptOperators::HasOwnPropertyNoHostObject(Var instance, PropertyId propertyId)
  1548. {
  1549. AssertMsg(!TaggedNumber::Is(instance), "HasOwnPropertyNoHostObject int passed");
  1550. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  1551. return object && object->HasOwnPropertyNoHostObject(propertyId);
  1552. }
  1553. // CONSIDER: Remove HasOwnPropertyNoHostObjectForHeapEnum and use GetOwnPropertyNoHostObjectForHeapEnum in its place by changing it
  1554. // to return BOOL, true or false with whether the property exists or not, and return the value if not getter/setter as an out param.
  1555. BOOL JavascriptOperators::HasOwnPropertyNoHostObjectForHeapEnum(Var instance, PropertyId propertyId, ScriptContext* requestContext, Var& getter, Var& setter)
  1556. {
  1557. AssertMsg(!TaggedNumber::Is(instance), "HasOwnPropertyNoHostObjectForHeapEnum int passed");
  1558. RecyclableObject * object = VarTo<RecyclableObject>(instance);
  1559. if (StaticType::Is(object->GetTypeId()))
  1560. {
  1561. return FALSE;
  1562. }
  1563. getter = setter = NULL;
  1564. DynamicObject* dynamicObject = VarTo<DynamicObject>(instance);
  1565. Assert(dynamicObject->GetScriptContext()->IsHeapEnumInProgress());
  1566. if (dynamicObject->UseDynamicObjectForNoHostObjectAccess())
  1567. {
  1568. if (!dynamicObject->DynamicObject::GetAccessors(propertyId, &getter, &setter, requestContext))
  1569. {
  1570. Var value = nullptr;
  1571. if (!JavascriptConversion::PropertyQueryFlagsToBoolean(dynamicObject->DynamicObject::GetPropertyQuery(instance, propertyId, &value, NULL, requestContext)) ||
  1572. (requestContext->IsUndeclBlockVar(value) && (VarIs<ActivationObject>(instance) || VarIs<RootObjectBase>(instance))))
  1573. {
  1574. return FALSE;
  1575. }
  1576. }
  1577. }
  1578. else
  1579. {
  1580. if (!object->GetAccessors(propertyId, &getter, &setter, requestContext))
  1581. {
  1582. Var value = nullptr;
  1583. if (!object->GetProperty(instance, propertyId, &value, NULL, requestContext) ||
  1584. (requestContext->IsUndeclBlockVar(value) && (VarIs<ActivationObject>(instance) || VarIs<RootObjectBase>(instance))))
  1585. {
  1586. return FALSE;
  1587. }
  1588. }
  1589. }
  1590. return TRUE;
  1591. }
  1592. Var JavascriptOperators::GetOwnPropertyNoHostObjectForHeapEnum(Var instance, PropertyId propertyId, ScriptContext* requestContext, Var& getter, Var& setter)
  1593. {
  1594. AssertMsg(!TaggedNumber::Is(instance), "GetDataPropertyNoHostObject int passed");
  1595. Assert(HasOwnPropertyNoHostObjectForHeapEnum(instance, propertyId, requestContext, getter, setter) || getter || setter);
  1596. DynamicObject* dynamicObject = VarTo<DynamicObject>(instance);
  1597. getter = setter = NULL;
  1598. if (NULL == dynamicObject)
  1599. {
  1600. return requestContext->GetLibrary()->GetUndefined();
  1601. }
  1602. Var returnVar = requestContext->GetLibrary()->GetUndefined();
  1603. BOOL result = FALSE;
  1604. if (dynamicObject->UseDynamicObjectForNoHostObjectAccess())
  1605. {
  1606. if (! dynamicObject->DynamicObject::GetAccessors(propertyId, &getter, &setter, requestContext))
  1607. {
  1608. result = JavascriptConversion::PropertyQueryFlagsToBoolean((dynamicObject->DynamicObject::GetPropertyQuery(instance, propertyId, &returnVar, NULL, requestContext)));
  1609. }
  1610. }
  1611. else
  1612. {
  1613. if (! dynamicObject->GetAccessors(propertyId, &getter, &setter, requestContext))
  1614. {
  1615. result = dynamicObject->GetProperty(instance, propertyId, &returnVar, NULL, requestContext);
  1616. }
  1617. }
  1618. if (result)
  1619. {
  1620. return returnVar;
  1621. }
  1622. return requestContext->GetLibrary()->GetUndefined();
  1623. }
  1624. BOOL JavascriptOperators::OP_HasOwnPropScoped(Var scope, PropertyId propertyId, Var defaultInstance, ScriptContext* scriptContext)
  1625. {
  1626. AssertMsg(scope == scriptContext->GetLibrary()->GetNull() || JavascriptArray::IsNonES5Array(scope),
  1627. "Invalid scope chain pointer passed - should be null or an array");
  1628. JavascriptArray* arrScope = JavascriptArray::TryVarToNonES5Array(scope);
  1629. if (arrScope)
  1630. {
  1631. Var instance = arrScope->DirectGetItem(0);
  1632. return JavascriptOperators::OP_HasOwnProperty(instance, propertyId, scriptContext);
  1633. }
  1634. return JavascriptOperators::OP_HasOwnProperty(defaultInstance, propertyId, scriptContext);
  1635. }
  1636. BOOL JavascriptOperators::GetPropertyUnscopable(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1637. {
  1638. return GetProperty_Internal<true>(instance, propertyObject, false, propertyId, value, requestContext, info);
  1639. }
  1640. BOOL JavascriptOperators::GetProperty(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1641. {
  1642. return GetProperty_Internal<false>(instance, propertyObject, false, propertyId, value, requestContext, info);
  1643. }
  1644. BOOL JavascriptOperators::GetRootProperty(Var instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1645. {
  1646. return GetProperty_Internal<false>(instance, VarTo<RecyclableObject>(instance), true, propertyId, value, requestContext, info);
  1647. }
  1648. BOOL JavascriptOperators::GetProperty_InternalSimple(Var instance, RecyclableObject* object, PropertyId propertyId, _Outptr_result_maybenull_ Var* value, ScriptContext* requestContext)
  1649. {
  1650. BOOL foundProperty = FALSE;
  1651. Assert(value != nullptr);
  1652. while (!JavascriptOperators::IsNull(object))
  1653. {
  1654. PropertyQueryFlags result = object->GetPropertyQuery(instance, propertyId, value, nullptr, requestContext);
  1655. if (result != PropertyQueryFlags::Property_NotFound)
  1656. {
  1657. foundProperty = JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  1658. break;
  1659. }
  1660. if (object->SkipsPrototype())
  1661. {
  1662. break;
  1663. }
  1664. object = JavascriptOperators::GetPrototypeNoTrap(object);
  1665. }
  1666. if (!foundProperty)
  1667. {
  1668. *value = requestContext->GetMissingPropertyResult();
  1669. }
  1670. return foundProperty;
  1671. }
  1672. template <bool unscopables>
  1673. BOOL JavascriptOperators::GetProperty_Internal(Var instance, RecyclableObject* propertyObject, const bool isRoot, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1674. {
  1675. if (TaggedNumber::Is(instance))
  1676. {
  1677. PropertyValueInfo::ClearCacheInfo(info);
  1678. }
  1679. RecyclableObject* object = propertyObject;
  1680. BOOL foundProperty = FALSE;
  1681. if (isRoot)
  1682. {
  1683. Assert(VarIs<RootObjectBase>(object));
  1684. RootObjectBase* rootObject = static_cast<RootObjectBase*>(object);
  1685. foundProperty = rootObject->GetRootProperty(instance, propertyId, value, info, requestContext);
  1686. }
  1687. while (!foundProperty && !JavascriptOperators::IsNull(object))
  1688. {
  1689. if (unscopables && IsPropertyUnscopable(object, propertyId))
  1690. {
  1691. break;
  1692. }
  1693. else
  1694. {
  1695. PropertyQueryFlags result = object->GetPropertyQuery(instance, propertyId, value, info, requestContext);
  1696. if (result != PropertyQueryFlags::Property_NotFound)
  1697. {
  1698. foundProperty = JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  1699. break;
  1700. }
  1701. }
  1702. if (object->SkipsPrototype())
  1703. {
  1704. break;
  1705. }
  1706. object = JavascriptOperators::GetPrototypeNoTrap(object);
  1707. }
  1708. if (foundProperty)
  1709. {
  1710. #if ENABLE_FIXED_FIELDS && DBG
  1711. if (DynamicObject::IsBaseDynamicObject(object))
  1712. {
  1713. DynamicObject* dynamicObject = (DynamicObject*)object;
  1714. DynamicTypeHandler* dynamicTypeHandler = dynamicObject->GetDynamicType()->GetTypeHandler();
  1715. Var property;
  1716. if (dynamicTypeHandler->CheckFixedProperty(requestContext->GetPropertyName(propertyId), &property, requestContext))
  1717. {
  1718. bool skipAssert = false;
  1719. if (value != nullptr && Js::VarIs<Js::RecyclableObject>(property))
  1720. {
  1721. Js::RecyclableObject* pObject = Js::VarTo<Js::RecyclableObject>(property);
  1722. Js::RecyclableObject* pValue = Js::VarTo<Js::RecyclableObject>(*value);
  1723. if (pValue->GetScriptContext() != pObject->GetScriptContext())
  1724. {
  1725. // value was marshaled. skip check
  1726. skipAssert = true;
  1727. }
  1728. }
  1729. Assert(skipAssert || value == nullptr || *value == property);
  1730. }
  1731. }
  1732. #endif
  1733. // Don't cache the information if the value is undecl block var
  1734. // REVIEW: We might want to only check this if we need to (For LdRootFld or ScopedLdFld)
  1735. // Also we might want to throw here instead of checking it again in the caller
  1736. if (value && !requestContext->IsUndeclBlockVar(*value) && !VarIs<UnscopablesWrapperObject>(object))
  1737. {
  1738. CacheOperators::CachePropertyRead(propertyObject, object, isRoot, propertyId, false, info, requestContext);
  1739. }
  1740. #ifdef TELEMETRY_JSO
  1741. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  1742. {
  1743. requestContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(instance, propertyId, value, /*successful: */true);
  1744. }
  1745. #endif
  1746. return TRUE;
  1747. }
  1748. else
  1749. {
  1750. #ifdef MISSING_PROPERTY_STATS
  1751. if (PHASE_STATS1(MissingPropertyCachePhase))
  1752. {
  1753. requestContext->RecordMissingPropertyMiss();
  1754. }
  1755. #endif
  1756. if (PHASE_TRACE1(MissingPropertyCachePhase))
  1757. {
  1758. Output::Print(_u("MissingPropertyCaching: Missing property %d on slow path.\n"), propertyId);
  1759. }
  1760. TryCacheMissingProperty(instance, propertyObject, isRoot, propertyId, requestContext, info);
  1761. #if defined(TELEMETRY_JSO) || defined(TELEMETRY_AddToCache) // enabled for `TELEMETRY_AddToCache`, because this is the property-not-found codepath where the normal TELEMETRY_AddToCache code wouldn't be executed.
  1762. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  1763. {
  1764. if (info && info->AllowResizingPolymorphicInlineCache()) // If in interpreted mode, not JIT.
  1765. {
  1766. requestContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(instance, propertyId, nullptr);
  1767. }
  1768. }
  1769. #endif
  1770. *value = requestContext->GetMissingPropertyResult();
  1771. return FALSE;
  1772. }
  1773. }
  1774. // If the given instance is a type where we can cache missing properties, then cache that the given property ID is missing.
  1775. // cacheInstance is used as startingObject in CachePropertyRead, and might be instance's proto if we are fetching a super property (see #3064).
  1776. void JavascriptOperators::TryCacheMissingProperty(Var instance, Var cacheInstance, bool isRoot, PropertyId propertyId, ScriptContext* requestContext, _Inout_ PropertyValueInfo * info)
  1777. {
  1778. // Here, any well-behaved subclasses of DynamicObject can opt in to getting included in the missing property cache.
  1779. // For now, we only include basic objects and arrays.
  1780. if (PHASE_OFF1(MissingPropertyCachePhase) || isRoot || !(DynamicObject::IsBaseDynamicObject(instance) || DynamicObject::IsAnyArray(instance)))
  1781. {
  1782. return;
  1783. }
  1784. // CustomExternalObject in particular is problematic because in some cases it can report missing when implicit callsare disabled.
  1785. // See CustomExternalObject::GetPropertyQuery for an example.
  1786. if (UnsafeVarTo<DynamicObject>(instance)->GetType()->IsJsrtExternal() && requestContext->GetThreadContext()->IsDisableImplicitCall())
  1787. {
  1788. return;
  1789. }
  1790. DynamicTypeHandler* handler = UnsafeVarTo<DynamicObject>(instance)->GetDynamicType()->GetTypeHandler();
  1791. // Only cache missing property lookups for non-root field loads on objects that have PathTypeHandlers, because only these types have the right behavior
  1792. // when the missing property is later added. DictionaryTypeHandler's introduce the possibility that a stale TypePropertyCache entry with isMissing==true can
  1793. // be left in the cache after the property has been installed in the object's prototype chain. Other changes to optimize accesses to objects that don't
  1794. // override special symbols make it unnecessary to introduce an invalidation scheme to deal with DictionaryTypeHandler's.
  1795. if (!handler->IsPathTypeHandler())
  1796. {
  1797. return;
  1798. }
  1799. #ifdef MISSING_PROPERTY_STATS
  1800. if (PHASE_STATS1(MissingPropertyCachePhase))
  1801. {
  1802. requestContext->RecordMissingPropertyCacheAttempt();
  1803. }
  1804. #endif
  1805. if (PHASE_TRACE1(MissingPropertyCachePhase))
  1806. {
  1807. Output::Print(_u("MissingPropertyCache: Caching missing property for property %d.\n"), propertyId);
  1808. }
  1809. PropertyValueInfo::Set(info, requestContext->GetLibrary()->GetMissingPropertyHolder(), 0);
  1810. CacheOperators::CachePropertyRead(cacheInstance, requestContext->GetLibrary()->GetMissingPropertyHolder(), isRoot, propertyId, true /*isMissing*/, info, requestContext);
  1811. }
  1812. template<bool OutputExistence, typename PropertyKeyType> PropertyQueryFlags QueryGetOrHasProperty(
  1813. Var originalInstance, RecyclableObject* object, PropertyKeyType propertyKey, Var* value, PropertyValueInfo* info, ScriptContext* requestContext);
  1814. template<> PropertyQueryFlags QueryGetOrHasProperty<false /*OutputExistence*/, PropertyId /*PropertyKeyType*/>(
  1815. Var originalInstance, RecyclableObject* object, PropertyId propertyKey, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1816. {
  1817. return object->GetPropertyQuery(originalInstance, propertyKey, value, info, requestContext);
  1818. }
  1819. template<> PropertyQueryFlags QueryGetOrHasProperty<false /*OutputExistence*/, JavascriptString* /*PropertyKeyType*/>(
  1820. Var originalInstance, RecyclableObject* object, JavascriptString* propertyKey, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1821. {
  1822. return object->GetPropertyQuery(originalInstance, propertyKey, value, info, requestContext);
  1823. }
  1824. template<> PropertyQueryFlags QueryGetOrHasProperty<true /*OutputExistence*/, PropertyId /*PropertyKeyType*/>(
  1825. Var originalInstance, RecyclableObject* object, PropertyId propertyKey, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1826. {
  1827. PropertyQueryFlags result = object->HasPropertyQuery(propertyKey, info);
  1828. *value = JavascriptBoolean::ToVar(JavascriptConversion::PropertyQueryFlagsToBoolean(result), requestContext);
  1829. return result;
  1830. }
  1831. template<bool OutputExistence, typename PropertyKeyType>
  1832. BOOL JavascriptOperators::GetPropertyWPCache(Var instance, RecyclableObject* propertyObject, PropertyKeyType propertyKey, Var* value, ScriptContext* requestContext, _Inout_ PropertyValueInfo * info)
  1833. {
  1834. Assert(value);
  1835. RecyclableObject* object = propertyObject;
  1836. while (!JavascriptOperators::IsNull(object))
  1837. {
  1838. PropertyQueryFlags result = QueryGetOrHasProperty<OutputExistence>(instance, object, propertyKey, value, info, requestContext);
  1839. if (result != PropertyQueryFlags::Property_NotFound)
  1840. {
  1841. if (!VarIs<UnscopablesWrapperObject>(object) && info->GetPropertyRecordUsageCache())
  1842. {
  1843. PropertyId propertyId = info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId();
  1844. CacheOperators::CachePropertyRead(instance, object, false, propertyId, false, info, requestContext);
  1845. }
  1846. return JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  1847. }
  1848. // SkipsPrototype refers only to the Get operation, not Has. Some objects like CustomExternalObject respond
  1849. // to HasPropertyQuery with info only about the object itself and GetPropertyQuery with info about its prototype chain.
  1850. // For consistency with the behavior of JavascriptOperators::HasProperty, don't skip prototypes when outputting existence.
  1851. if (!OutputExistence && object->SkipsPrototype())
  1852. {
  1853. break;
  1854. }
  1855. object = JavascriptOperators::GetPrototypeNoTrap(object);
  1856. }
  1857. if (info->GetPropertyRecordUsageCache())
  1858. {
  1859. TryCacheMissingProperty(instance, instance, false /*isRoot*/, info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId(), requestContext, info);
  1860. }
  1861. *value = OutputExistence
  1862. ? requestContext->GetLibrary()->GetFalse()
  1863. : requestContext->GetMissingPropertyResult();
  1864. return FALSE;
  1865. }
  1866. bool JavascriptOperators::GetPropertyObjectForElementAccess(
  1867. _In_ Var instance,
  1868. _In_ Var index,
  1869. _In_ ScriptContext* scriptContext,
  1870. _Out_ RecyclableObject** propertyObject,
  1871. _In_ rtErrors error)
  1872. {
  1873. BOOL isNullOrUndefined = !GetPropertyObject(instance, scriptContext, propertyObject);
  1874. Assert(*propertyObject == instance || TaggedNumber::Is(instance));
  1875. if (isNullOrUndefined)
  1876. {
  1877. if (!scriptContext->GetThreadContext()->RecordImplicitException())
  1878. {
  1879. return false;
  1880. }
  1881. JavascriptError::ThrowTypeError(scriptContext, error, GetPropertyDisplayNameForError(index, scriptContext));
  1882. }
  1883. return true;
  1884. }
  1885. bool JavascriptOperators::GetPropertyObjectForSetElementI(
  1886. _In_ Var instance,
  1887. _In_ Var index,
  1888. _In_ ScriptContext* scriptContext,
  1889. _Out_ RecyclableObject** propertyObject)
  1890. {
  1891. return GetPropertyObjectForElementAccess(instance, index, scriptContext, propertyObject, JSERR_Property_CannotSet_NullOrUndefined);
  1892. }
  1893. bool JavascriptOperators::GetPropertyObjectForGetElementI(
  1894. _In_ Var instance,
  1895. _In_ Var index,
  1896. _In_ ScriptContext* scriptContext,
  1897. _Out_ RecyclableObject** propertyObject)
  1898. {
  1899. return GetPropertyObjectForElementAccess(instance, index, scriptContext, propertyObject, JSERR_Property_CannotGet_NullOrUndefined);
  1900. }
  1901. BOOL JavascriptOperators::GetPropertyObject(Var instance, ScriptContext * scriptContext, RecyclableObject** propertyObject)
  1902. {
  1903. Assert(propertyObject);
  1904. if (TaggedNumber::Is(instance))
  1905. {
  1906. *propertyObject = scriptContext->GetLibrary()->GetNumberPrototype();
  1907. return TRUE;
  1908. }
  1909. RecyclableObject* object = UnsafeVarTo<RecyclableObject>(instance);
  1910. *propertyObject = object;
  1911. if (JavascriptOperators::IsUndefinedOrNull(object))
  1912. {
  1913. return FALSE;
  1914. }
  1915. return TRUE;
  1916. }
  1917. #if DBG
  1918. BOOL JavascriptOperators::IsPropertyObject(RecyclableObject * instance)
  1919. {
  1920. TypeId typeId = JavascriptOperators::GetTypeId(instance);
  1921. return (typeId != TypeIds_Integer && typeId != TypeIds_Null && typeId != TypeIds_Undefined);
  1922. }
  1923. #endif
  1924. Var JavascriptOperators::OP_GetProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  1925. {
  1926. JIT_HELPER_REENTRANT_HEADER(Op_GetProperty);
  1927. RecyclableObject* object = nullptr;
  1928. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  1929. {
  1930. if (scriptContext->GetThreadContext()->RecordImplicitException())
  1931. {
  1932. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined, scriptContext->GetPropertyName(propertyId)->GetBuffer());
  1933. }
  1934. else
  1935. {
  1936. return scriptContext->GetLibrary()->GetUndefined();
  1937. }
  1938. }
  1939. Var result = JavascriptOperators::GetPropertyNoCache(instance, object, propertyId, scriptContext);
  1940. AssertMsg(result != nullptr, "result null in OP_GetProperty");
  1941. return result;
  1942. JIT_HELPER_END(Op_GetProperty);
  1943. }
  1944. Var JavascriptOperators::OP_GetRootProperty(Var instance, PropertyId propertyId, PropertyValueInfo * info, ScriptContext* scriptContext)
  1945. {
  1946. AssertMsg(VarIs<RootObjectBase>(instance), "Root must be an object!");
  1947. Var value = nullptr;
  1948. if (JavascriptOperators::GetRootProperty(VarTo<RecyclableObject>(instance), propertyId, &value, scriptContext, info))
  1949. {
  1950. if (scriptContext->IsUndeclBlockVar(value) && scriptContext->GetThreadContext()->RecordImplicitException())
  1951. {
  1952. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  1953. }
  1954. return value;
  1955. }
  1956. const char16* propertyName = scriptContext->GetPropertyName(propertyId)->GetBuffer();
  1957. JavascriptFunction * caller = nullptr;
  1958. if (JavascriptStackWalker::GetCaller(&caller, scriptContext))
  1959. {
  1960. FunctionBody * callerBody = caller->GetFunctionBody();
  1961. if (callerBody && callerBody->GetUtf8SourceInfo()->GetIsXDomain())
  1962. {
  1963. propertyName = nullptr;
  1964. }
  1965. }
  1966. // Don't error if we disabled implicit calls
  1967. if (scriptContext->GetThreadContext()->RecordImplicitException())
  1968. {
  1969. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UndefVariable, propertyName);
  1970. }
  1971. return scriptContext->GetMissingPropertyResult();
  1972. }
  1973. Var JavascriptOperators::OP_GetThisScoped(FrameDisplay *pScope, Var defaultInstance, ScriptContext* scriptContext)
  1974. {
  1975. // NOTE: If changes are made to this logic be sure to update the debuggers as well
  1976. int length = pScope->GetLength();
  1977. for (int i = 0; i < length; i += 1)
  1978. {
  1979. Var value = nullptr;
  1980. RecyclableObject *obj = VarTo<RecyclableObject>(pScope->GetItem(i));
  1981. if (JavascriptOperators::GetProperty(obj, Js::PropertyIds::_this, &value, scriptContext))
  1982. {
  1983. return value;
  1984. }
  1985. }
  1986. return defaultInstance;
  1987. }
  1988. Var JavascriptOperators::OP_UnwrapWithObj(Var aValue)
  1989. {
  1990. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_UnwrapWithObj);
  1991. return VarTo<UnscopablesWrapperObject>(aValue)->GetWrappedObject();
  1992. JIT_HELPER_END(Op_UnwrapWithObj);
  1993. }
  1994. Var JavascriptOperators::OP_GetInstanceScoped(FrameDisplay *pScope, PropertyId propertyId, Var rootObject, Var* thisVar, ScriptContext* scriptContext)
  1995. {
  1996. JIT_HELPER_REENTRANT_HEADER(Op_GetInstanceScoped);
  1997. // Similar to GetPropertyScoped, but instead of returning the property value, we return the instance that
  1998. // owns it, or the global object if no instance is found.
  1999. int i;
  2000. int length = pScope->GetLength();
  2001. for (i = 0; i < length; i++)
  2002. {
  2003. RecyclableObject *obj = (RecyclableObject*)pScope->GetItem(i);
  2004. if (JavascriptOperators::HasProperty(obj, propertyId))
  2005. {
  2006. // HasProperty will call UnscopablesWrapperObject's HasProperty which will do the filtering
  2007. // All we have to do here is unwrap the object hence the api call
  2008. return obj->GetThisAndUnwrappedInstance(thisVar);
  2009. }
  2010. }
  2011. *thisVar = scriptContext->GetLibrary()->GetUndefined();
  2012. if (rootObject != scriptContext->GetGlobalObject())
  2013. {
  2014. if (JavascriptOperators::OP_HasProperty(rootObject, propertyId, scriptContext))
  2015. {
  2016. return rootObject;
  2017. }
  2018. }
  2019. return scriptContext->GetGlobalObject();
  2020. JIT_HELPER_END(Op_GetInstanceScoped);
  2021. }
  2022. Var JavascriptOperators::GetPropertyReference(RecyclableObject *instance, PropertyId propertyId, ScriptContext* requestContext)
  2023. {
  2024. Var value = nullptr;
  2025. PropertyValueInfo info;
  2026. if (JavascriptOperators::GetPropertyReference(instance, propertyId, &value, requestContext, &info))
  2027. {
  2028. Assert(value != nullptr);
  2029. return value;
  2030. }
  2031. return requestContext->GetMissingPropertyResult();
  2032. }
  2033. BOOL JavascriptOperators::GetPropertyReference(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  2034. {
  2035. return GetPropertyReference_Internal(instance, propertyObject, false, propertyId, value, requestContext, info);
  2036. }
  2037. BOOL JavascriptOperators::GetRootPropertyReference(RecyclableObject* instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  2038. {
  2039. return GetPropertyReference_Internal(instance, instance, true, propertyId, value, requestContext, info);
  2040. }
  2041. BOOL JavascriptOperators::PropertyReferenceWalkUnscopable(Var instance, RecyclableObject** propertyObject, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  2042. {
  2043. return PropertyReferenceWalk_Impl<true>(instance, propertyObject, propertyId, value, info, requestContext);
  2044. }
  2045. BOOL JavascriptOperators::PropertyReferenceWalk(Var instance, RecyclableObject** propertyObject, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  2046. {
  2047. return PropertyReferenceWalk_Impl<false>(instance, propertyObject, propertyId, value, info, requestContext);
  2048. }
  2049. template <bool unscopables>
  2050. BOOL JavascriptOperators::PropertyReferenceWalk_Impl(Var instance, RecyclableObject** propertyObject, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  2051. {
  2052. BOOL foundProperty = false;
  2053. RecyclableObject* object = *propertyObject;
  2054. while (!foundProperty && !JavascriptOperators::IsNull(object))
  2055. {
  2056. if (unscopables && JavascriptOperators::IsPropertyUnscopable(object, propertyId))
  2057. {
  2058. break;
  2059. }
  2060. else
  2061. {
  2062. PropertyQueryFlags result = object->GetPropertyReferenceQuery(instance, propertyId, value, info, requestContext);
  2063. if (result != PropertyQueryFlags::Property_NotFound)
  2064. {
  2065. foundProperty = JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  2066. break;
  2067. }
  2068. }
  2069. if (object->SkipsPrototype())
  2070. {
  2071. break; // will return false
  2072. }
  2073. object = JavascriptOperators::GetPrototypeNoTrap(object);
  2074. }
  2075. *propertyObject = object;
  2076. return foundProperty;
  2077. }
  2078. BOOL JavascriptOperators::GetPropertyReference_Internal(Var instance, RecyclableObject* propertyObject, const bool isRoot, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  2079. {
  2080. if (TaggedNumber::Is(instance))
  2081. {
  2082. PropertyValueInfo::ClearCacheInfo(info);
  2083. }
  2084. BOOL foundProperty = FALSE;
  2085. RecyclableObject* object = propertyObject;
  2086. if (isRoot)
  2087. {
  2088. foundProperty = VarTo<RootObjectBase>(object)->GetRootPropertyReference(instance, propertyId, value, info, requestContext);
  2089. }
  2090. if (!foundProperty)
  2091. {
  2092. foundProperty = PropertyReferenceWalk(instance, &object, propertyId, value, info, requestContext);
  2093. }
  2094. if (!foundProperty)
  2095. {
  2096. #if defined(TELEMETRY_JSO) || defined(TELEMETRY_AddToCache) // enabled for `TELEMETRY_AddToCache`, because this is the property-not-found codepath where the normal TELEMETRY_AddToCache code wouldn't be executed.
  2097. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  2098. {
  2099. if (info && info->AllowResizingPolymorphicInlineCache()) // If in interpreted mode, not JIT.
  2100. {
  2101. requestContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(instance, propertyId, nullptr);
  2102. }
  2103. }
  2104. #endif
  2105. *value = requestContext->GetMissingPropertyResult();
  2106. return foundProperty;
  2107. }
  2108. if (requestContext->IsUndeclBlockVar(*value))
  2109. {
  2110. JavascriptError::ThrowReferenceError(requestContext, JSERR_UseBeforeDeclaration);
  2111. }
  2112. #if ENABLE_FIXED_FIELDS && DBG
  2113. if (DynamicObject::IsBaseDynamicObject(object))
  2114. {
  2115. DynamicObject* dynamicObject = (DynamicObject*)object;
  2116. DynamicTypeHandler* dynamicTypeHandler = dynamicObject->GetDynamicType()->GetTypeHandler();
  2117. Var property = nullptr;
  2118. if (dynamicTypeHandler->CheckFixedProperty(requestContext->GetPropertyName(propertyId), &property, requestContext))
  2119. {
  2120. Assert(value == nullptr || *value == property);
  2121. }
  2122. }
  2123. #endif
  2124. CacheOperators::CachePropertyRead(instance, object, isRoot, propertyId, false, info, requestContext);
  2125. return TRUE;
  2126. }
  2127. template <typename PropertyKeyType, bool unscopable>
  2128. DescriptorFlags JavascriptOperators::GetterSetter_Impl(RecyclableObject* instance, PropertyKeyType propertyKey, Var* setterValue, PropertyValueInfo* info, ScriptContext* scriptContext)
  2129. {
  2130. DescriptorFlags flags = None;
  2131. RecyclableObject* object = instance;
  2132. while (flags == None && !JavascriptOperators::IsNull(object))
  2133. {
  2134. if (unscopable && IsPropertyUnscopable(object, propertyKey))
  2135. {
  2136. break;
  2137. }
  2138. else
  2139. {
  2140. flags = object->GetSetter(propertyKey, setterValue, info, scriptContext);
  2141. if (flags != None)
  2142. {
  2143. break;
  2144. }
  2145. }
  2146. // CONSIDER: we should add SkipsPrototype support. DOM has no ES 5 concepts built in that aren't
  2147. // already part of our prototype objects which are chakra objects.
  2148. object = object->GetPrototype();
  2149. }
  2150. return flags;
  2151. }
  2152. DescriptorFlags JavascriptOperators::GetterSetterUnscopable(RecyclableObject* instance, PropertyId propertyId, Var* setterValue, PropertyValueInfo* info, ScriptContext* scriptContext)
  2153. {
  2154. return GetterSetter_Impl<PropertyId, true>(instance, propertyId, setterValue, info, scriptContext);
  2155. }
  2156. DescriptorFlags JavascriptOperators::GetterSetter(RecyclableObject* instance, PropertyId propertyId, Var* setterValue, PropertyValueInfo* info, ScriptContext* scriptContext)
  2157. {
  2158. return GetterSetter_Impl<PropertyId, false>(instance, propertyId, setterValue, info, scriptContext);
  2159. }
  2160. DescriptorFlags JavascriptOperators::GetterSetter(RecyclableObject* instance, JavascriptString * propertyName, Var* setterValue, PropertyValueInfo* info, ScriptContext* scriptContext)
  2161. {
  2162. return GetterSetter_Impl<JavascriptString*, false>(instance, propertyName, setterValue, info, scriptContext);
  2163. }
  2164. void JavascriptOperators::OP_InvalidateProtoCaches(PropertyId propertyId, ScriptContext *scriptContext)
  2165. {
  2166. JIT_HELPER_NOT_REENTRANT_HEADER(InvalidateProtoCaches, reentrancylock, scriptContext->GetThreadContext());
  2167. scriptContext->InvalidateProtoCaches(propertyId);
  2168. JIT_HELPER_END(InvalidateProtoCaches);
  2169. }
  2170. // Checks to see if any object in the prototype chain has a property descriptor for the given index
  2171. // that specifies either an accessor or a non-writable attribute.
  2172. // If TRUE, check flags for details.
  2173. BOOL JavascriptOperators::CheckPrototypesForAccessorOrNonWritableItem(RecyclableObject* instance, uint32 index,
  2174. Var* setterValue, DescriptorFlags *flags, ScriptContext* scriptContext, BOOL skipPrototypeCheck /* = FALSE */)
  2175. {
  2176. Assert(setterValue);
  2177. Assert(flags);
  2178. // Do a quick walk up the prototype chain to see if any of the prototypes has ever had ANY setter or non-writable property.
  2179. if (CheckIfObjectAndPrototypeChainHasOnlyWritableDataProperties(instance))
  2180. {
  2181. return FALSE;
  2182. }
  2183. RecyclableObject* object = instance;
  2184. while (!JavascriptOperators::IsNull(object))
  2185. {
  2186. *flags = object->GetItemSetter(index, setterValue, scriptContext);
  2187. if (*flags != None || skipPrototypeCheck)
  2188. {
  2189. break;
  2190. }
  2191. object = object->GetPrototype();
  2192. }
  2193. return ((*flags & Accessor) == Accessor) || ((*flags & Proxy) == Proxy) || ((*flags & Data) == Data && (*flags & Writable) == None);
  2194. }
  2195. BOOL JavascriptOperators::SetGlobalPropertyNoHost(char16 const * propertyName, charcount_t propertyLength, Var value, ScriptContext * scriptContext)
  2196. {
  2197. GlobalObject * globalObject = scriptContext->GetGlobalObject();
  2198. uint32 index;
  2199. PropertyRecord const * propertyRecord = nullptr;
  2200. IndexType indexType = GetIndexTypeFromString(propertyName, propertyLength, scriptContext, &index, &propertyRecord, true);
  2201. if (indexType == IndexType_Number)
  2202. {
  2203. return globalObject->DynamicObject::SetItem(index, value, PropertyOperation_None);
  2204. }
  2205. return globalObject->DynamicObject::SetProperty(propertyRecord->GetPropertyId(), value, PropertyOperation_None, NULL);
  2206. }
  2207. template<typename PropertyKeyType>
  2208. BOOL JavascriptOperators::SetPropertyWPCache(Var receiver, RecyclableObject* object, PropertyKeyType propertyKey, Var newValue, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags, _Inout_ PropertyValueInfo * info)
  2209. {
  2210. if (receiver)
  2211. {
  2212. AnalysisAssert(object);
  2213. Assert(!TaggedNumber::Is(receiver));
  2214. Var setterValueOrProxy = nullptr;
  2215. DescriptorFlags flags = None;
  2216. if (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(object, propertyKey, &setterValueOrProxy, &flags, info, requestContext))
  2217. {
  2218. if ((flags & Accessor) == Accessor)
  2219. {
  2220. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext))
  2221. {
  2222. return TRUE;
  2223. }
  2224. if (setterValueOrProxy)
  2225. {
  2226. if (VarIs<UnscopablesWrapperObject>(receiver))
  2227. {
  2228. receiver = (UnsafeVarTo<UnscopablesWrapperObject>(receiver))->GetWrappedObject();
  2229. }
  2230. else if (info->GetPropertyRecordUsageCache() && !JavascriptOperators::IsUndefinedAccessor(setterValueOrProxy, requestContext))
  2231. {
  2232. CacheOperators::CachePropertyWrite(VarTo<RecyclableObject>(receiver), false, object->GetType(), info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId(), info, requestContext);
  2233. }
  2234. RecyclableObject* func = VarTo<RecyclableObject>(setterValueOrProxy);
  2235. JavascriptOperators::CallSetter(func, receiver, newValue, requestContext);
  2236. }
  2237. return TRUE;
  2238. }
  2239. else if ((flags & Proxy) == Proxy)
  2240. {
  2241. Assert(VarIs<JavascriptProxy>(setterValueOrProxy));
  2242. JavascriptProxy* proxy = VarTo<JavascriptProxy>(setterValueOrProxy);
  2243. auto fn = [&](RecyclableObject* target) -> BOOL {
  2244. return JavascriptOperators::SetPropertyWPCache(receiver, target, propertyKey, newValue, requestContext, propertyOperationFlags, info);
  2245. };
  2246. if (info->GetPropertyRecordUsageCache())
  2247. {
  2248. PropertyValueInfo::SetNoCache(info, proxy);
  2249. PropertyValueInfo::DisablePrototypeCache(info, proxy);
  2250. }
  2251. return proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetPropertyWPCacheKind, propertyKey, newValue, requestContext, propertyOperationFlags);
  2252. }
  2253. else
  2254. {
  2255. Assert((flags & Data) == Data && (flags & Writable) == None);
  2256. requestContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  2257. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2258. return FALSE;
  2259. }
  2260. }
  2261. else if (!JavascriptOperators::IsObject(receiver))
  2262. {
  2263. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2264. return FALSE;
  2265. }
  2266. RecyclableObject* receiverObject = VarTo<RecyclableObject>(receiver);
  2267. if (receiver != object)
  2268. {
  2269. // If the receiver object has the property and it is an accessor then return false
  2270. PropertyDescriptor existingDesc;
  2271. if (JavascriptOperators::GetOwnPropertyDescriptor(receiverObject, propertyKey, requestContext, &existingDesc)
  2272. && existingDesc.IsAccessorDescriptor())
  2273. {
  2274. return FALSE;
  2275. }
  2276. }
  2277. Type *typeWithoutProperty = object->GetType();
  2278. // in 9.1.9, step 5, we should return false if receiver is not object, and that will happen in default RecyclableObject operation anyhow.
  2279. if (receiverObject->SetProperty(propertyKey, newValue, propertyOperationFlags, info))
  2280. {
  2281. if (!VarIs<JavascriptProxy>(receiver) && info->GetPropertyRecordUsageCache() && info->GetFlags() != InlineCacheSetterFlag && !object->IsExternal())
  2282. {
  2283. CacheOperators::CachePropertyWrite(VarTo<RecyclableObject>(receiver), false, typeWithoutProperty, info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId(), info, requestContext);
  2284. if (info->GetInstance() == receiverObject)
  2285. {
  2286. PropertyValueInfo::SetCacheInfo(info, info->GetPropertyRecordUsageCache()->GetLdElemInlineCache(), info->AllowResizingPolymorphicInlineCache());
  2287. CacheOperators::CachePropertyRead(object, receiverObject, false, info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId(), false, info, requestContext);
  2288. }
  2289. }
  2290. return TRUE;
  2291. }
  2292. }
  2293. return FALSE;
  2294. }
  2295. BOOL JavascriptOperators::SetItemOnTaggedNumber(Var receiver, RecyclableObject* object, uint32 index, Var newValue, ScriptContext* requestContext,
  2296. PropertyOperationFlags propertyOperationFlags)
  2297. {
  2298. Assert(TaggedNumber::Is(receiver));
  2299. if (requestContext->optimizationOverrides.GetSideEffects() & SideEffects_Accessor)
  2300. {
  2301. Var setterValueOrProxy = nullptr;
  2302. DescriptorFlags flags = None;
  2303. if (object == nullptr)
  2304. {
  2305. GetPropertyObject(receiver, requestContext, &object);
  2306. }
  2307. if (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableItem(object, index, &setterValueOrProxy, &flags, requestContext))
  2308. {
  2309. if ((flags & Accessor) == Accessor)
  2310. {
  2311. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext))
  2312. {
  2313. return TRUE;
  2314. }
  2315. if (setterValueOrProxy)
  2316. {
  2317. RecyclableObject* func = VarTo<RecyclableObject>(setterValueOrProxy);
  2318. JavascriptOperators::CallSetter(func, receiver, newValue, requestContext);
  2319. return TRUE;
  2320. }
  2321. }
  2322. else if ((flags & Proxy) == Proxy)
  2323. {
  2324. Assert(VarIs<JavascriptProxy>(setterValueOrProxy));
  2325. JavascriptProxy* proxy = VarTo<JavascriptProxy>(setterValueOrProxy);
  2326. const PropertyRecord* propertyRecord = nullptr;
  2327. proxy->PropertyIdFromInt(index, &propertyRecord);
  2328. return proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetItemOnTaggedNumberKind, propertyRecord->GetPropertyId(), newValue, requestContext, propertyOperationFlags);
  2329. }
  2330. else
  2331. {
  2332. Assert((flags & Data) == Data && (flags & Writable) == None);
  2333. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2334. }
  2335. }
  2336. }
  2337. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2338. return FALSE;
  2339. }
  2340. BOOL JavascriptOperators::SetPropertyOnTaggedNumber(Var receiver, RecyclableObject* object, PropertyId propertyId, Var newValue, ScriptContext* requestContext,
  2341. PropertyOperationFlags propertyOperationFlags)
  2342. {
  2343. Assert (TaggedNumber::Is(receiver));
  2344. if (requestContext->optimizationOverrides.GetSideEffects() & SideEffects_Accessor)
  2345. {
  2346. Var setterValueOrProxy = nullptr;
  2347. PropertyValueInfo info;
  2348. DescriptorFlags flags = None;
  2349. if (object == nullptr)
  2350. {
  2351. GetPropertyObject(receiver, requestContext, &object);
  2352. }
  2353. if (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(object, propertyId, &setterValueOrProxy, &flags, &info, requestContext))
  2354. {
  2355. if ((flags & Accessor) == Accessor)
  2356. {
  2357. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext))
  2358. {
  2359. return TRUE;
  2360. }
  2361. if (setterValueOrProxy)
  2362. {
  2363. RecyclableObject* func = VarTo<RecyclableObject>(setterValueOrProxy);
  2364. Assert(info.GetFlags() == InlineCacheSetterFlag || info.GetPropertyIndex() == Constants::NoSlot);
  2365. JavascriptOperators::CallSetter(func, receiver, newValue, requestContext);
  2366. return TRUE;
  2367. }
  2368. }
  2369. else if ((flags & Proxy) == Proxy)
  2370. {
  2371. Assert(VarIs<JavascriptProxy>(setterValueOrProxy));
  2372. JavascriptProxy* proxy = VarTo<JavascriptProxy>(setterValueOrProxy);
  2373. return proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetPropertyOnTaggedNumberKind, propertyId, newValue, requestContext, propertyOperationFlags);
  2374. }
  2375. else
  2376. {
  2377. Assert((flags & Data) == Data && (flags & Writable) == None);
  2378. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2379. }
  2380. }
  2381. }
  2382. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  2383. requestContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  2384. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2385. return FALSE;
  2386. }
  2387. BOOL JavascriptOperators::SetPropertyUnscopable(Var instance, RecyclableObject* receiver, PropertyId propertyId, Var newValue, PropertyValueInfo * info, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  2388. {
  2389. return SetProperty_Internal<true>(instance, receiver, false, propertyId, newValue, info, requestContext, propertyOperationFlags);
  2390. }
  2391. BOOL JavascriptOperators::SetProperty(Var receiver, RecyclableObject* object, PropertyId propertyId, Var newValue, PropertyValueInfo * info, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  2392. {
  2393. return SetProperty_Internal<false>(receiver, object, false, propertyId, newValue, info, requestContext, propertyOperationFlags);
  2394. }
  2395. BOOL JavascriptOperators::SetRootProperty(RecyclableObject* instance, PropertyId propertyId, Var newValue, PropertyValueInfo * info, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  2396. {
  2397. return SetProperty_Internal<false>(instance, instance, true, propertyId, newValue, info, requestContext, propertyOperationFlags);
  2398. }
  2399. // Returns true if a result was written.
  2400. bool JavascriptOperators::SetAccessorOrNonWritableProperty(
  2401. Var receiver,
  2402. RecyclableObject* object,
  2403. PropertyId propertyId,
  2404. Var newValue,
  2405. PropertyValueInfo * info,
  2406. ScriptContext* requestContext,
  2407. PropertyOperationFlags propertyOperationFlags,
  2408. bool isRoot,
  2409. bool allowUndecInConsoleScope,
  2410. BOOL *result)
  2411. {
  2412. *result = FALSE;
  2413. Var setterValueOrProxy = nullptr;
  2414. DescriptorFlags flags = None;
  2415. bool receiverNonWritable = false;
  2416. if (receiver != object && !isRoot)
  2417. {
  2418. Var receiverSetter = nullptr;
  2419. PropertyValueInfo receiverInfo;
  2420. DescriptorFlags receiverFlags = VarTo<RecyclableObject>(receiver)->GetSetter(propertyId, &receiverSetter, &receiverInfo, requestContext);
  2421. receiverNonWritable = ((receiverFlags & Data) == Data && (receiverFlags & Writable) == None);
  2422. }
  2423. if ((isRoot && JavascriptOperators::CheckPrototypesForAccessorOrNonWritableRootProperty(object, propertyId, &setterValueOrProxy, &flags, info, requestContext)) ||
  2424. (!isRoot && (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(object, propertyId, &setterValueOrProxy, &flags, info, requestContext) ||
  2425. receiverNonWritable)))
  2426. {
  2427. if ((flags & Accessor) == Accessor)
  2428. {
  2429. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext) ||
  2430. JavascriptError::ThrowIfNotExtensibleUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext))
  2431. {
  2432. *result = TRUE;
  2433. return true;
  2434. }
  2435. if (setterValueOrProxy)
  2436. {
  2437. RecyclableObject* func = VarTo<RecyclableObject>(setterValueOrProxy);
  2438. Assert(!info || info->GetFlags() == InlineCacheSetterFlag || info->GetPropertyIndex() == Constants::NoSlot);
  2439. if (VarIs<UnscopablesWrapperObject>(receiver))
  2440. {
  2441. receiver = (UnsafeVarTo<UnscopablesWrapperObject>(receiver))->GetWrappedObject();
  2442. }
  2443. else if (!JavascriptOperators::IsUndefinedAccessor(setterValueOrProxy, requestContext))
  2444. {
  2445. CacheOperators::CachePropertyWrite(VarTo<RecyclableObject>(receiver), isRoot, object->GetType(), propertyId, info, requestContext);
  2446. }
  2447. #ifdef ENABLE_MUTATION_BREAKPOINT
  2448. if (MutationBreakpoint::IsFeatureEnabled(requestContext))
  2449. {
  2450. MutationBreakpoint::HandleSetProperty(requestContext, object, propertyId, newValue);
  2451. }
  2452. #endif
  2453. JavascriptOperators::CallSetter(func, receiver, newValue, requestContext);
  2454. }
  2455. *result = TRUE;
  2456. return true;
  2457. }
  2458. else if ((flags & Proxy) == Proxy)
  2459. {
  2460. Assert(VarIs<JavascriptProxy>(setterValueOrProxy));
  2461. JavascriptProxy* proxy = VarTo<JavascriptProxy>(setterValueOrProxy);
  2462. // We can't cache the property at this time. both target and handler can be changed outside of the proxy, so the inline cache needs to be
  2463. // invalidate when target, handler, or handler prototype has changed. We don't have a way to achieve this yet.
  2464. PropertyValueInfo::SetNoCache(info, proxy);
  2465. PropertyValueInfo::DisablePrototypeCache(info, proxy); // We can't cache prototype property either
  2466. *result = proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetPropertyKind, propertyId, newValue, requestContext, propertyOperationFlags);
  2467. return true;
  2468. }
  2469. else
  2470. {
  2471. Assert(((flags & Data) == Data && (flags & Writable) == None) || receiverNonWritable);
  2472. if (!allowUndecInConsoleScope)
  2473. {
  2474. if (flags & Const)
  2475. {
  2476. JavascriptError::ThrowTypeError(requestContext, ERRAssignmentToConst);
  2477. }
  2478. JavascriptError::ThrowCantAssign(propertyOperationFlags, requestContext, propertyId);
  2479. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2480. *result = FALSE;
  2481. return true;
  2482. }
  2483. }
  2484. }
  2485. return false;
  2486. }
  2487. template <bool unscopables>
  2488. BOOL JavascriptOperators::SetProperty_Internal(Var receiver, RecyclableObject* object, const bool isRoot, PropertyId propertyId, Var newValue, PropertyValueInfo * info, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  2489. {
  2490. if (receiver == nullptr)
  2491. {
  2492. return FALSE;
  2493. }
  2494. Assert(!TaggedNumber::Is(receiver));
  2495. BOOL setAccessorResult = FALSE;
  2496. if (SetAccessorOrNonWritableProperty(receiver, object, propertyId, newValue, info, requestContext, propertyOperationFlags, isRoot, false, &setAccessorResult))
  2497. {
  2498. return setAccessorResult;
  2499. }
  2500. else if (!JavascriptOperators::IsObject(receiver))
  2501. {
  2502. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2503. return FALSE;
  2504. }
  2505. #ifdef ENABLE_MUTATION_BREAKPOINT
  2506. // Break on mutation if needed
  2507. bool doNotUpdateCacheForMbp = MutationBreakpoint::IsFeatureEnabled(requestContext) ?
  2508. MutationBreakpoint::HandleSetProperty(requestContext, object, propertyId, newValue) : false;
  2509. #endif
  2510. // Get the original type before setting the property
  2511. Type *typeWithoutProperty = object->GetType();
  2512. BOOL didSetProperty = false;
  2513. if (isRoot)
  2514. {
  2515. AssertMsg(JavascriptOperators::GetTypeId(receiver) == TypeIds_GlobalObject
  2516. || JavascriptOperators::GetTypeId(receiver) == TypeIds_ModuleRoot,
  2517. "Root must be a global object!");
  2518. RootObjectBase* rootObject = static_cast<RootObjectBase*>(receiver);
  2519. didSetProperty = rootObject->SetRootProperty(propertyId, newValue, propertyOperationFlags, info);
  2520. }
  2521. else
  2522. {
  2523. RecyclableObject* instanceObject = VarTo<RecyclableObject>(receiver);
  2524. while (!JavascriptOperators::IsNull(instanceObject))
  2525. {
  2526. if (unscopables && JavascriptOperators::IsPropertyUnscopable(instanceObject, propertyId))
  2527. {
  2528. break;
  2529. }
  2530. else
  2531. {
  2532. didSetProperty = instanceObject->SetProperty(propertyId, newValue, propertyOperationFlags, info);
  2533. if (didSetProperty || !unscopables)
  2534. {
  2535. break;
  2536. }
  2537. }
  2538. instanceObject = JavascriptOperators::GetPrototypeNoTrap(instanceObject);
  2539. }
  2540. }
  2541. if (didSetProperty)
  2542. {
  2543. bool updateCache = true;
  2544. #ifdef ENABLE_MUTATION_BREAKPOINT
  2545. updateCache = updateCache && !doNotUpdateCacheForMbp;
  2546. #endif
  2547. if (updateCache)
  2548. {
  2549. if (!VarIs<JavascriptProxy>(receiver))
  2550. {
  2551. CacheOperators::CachePropertyWrite(VarTo<RecyclableObject>(receiver), isRoot, typeWithoutProperty, propertyId, info, requestContext);
  2552. }
  2553. }
  2554. return TRUE;
  2555. }
  2556. return FALSE;
  2557. }
  2558. BOOL JavascriptOperators::IsNumberFromNativeArray(Var instance, uint32 index, ScriptContext* scriptContext)
  2559. {
  2560. #if ENABLE_COPYONACCESS_ARRAY
  2561. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  2562. #endif
  2563. Js::TypeId instanceType = JavascriptOperators::GetTypeId(instance);
  2564. // Fast path for native and typed arrays.
  2565. bool isNativeArray = instanceType == TypeIds_NativeIntArray || instanceType == TypeIds_NativeFloatArray;
  2566. bool isTypedArray = instanceType >= TypeIds_Int8Array && instanceType <= TypeIds_Uint64Array;
  2567. if (isNativeArray || isTypedArray)
  2568. {
  2569. // Check if the typed array is detached to prevent an exception in GetOwnItem
  2570. if (isTypedArray && TypedArrayBase::IsDetachedTypedArray(instance))
  2571. {
  2572. return FALSE;
  2573. }
  2574. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  2575. Var member = nullptr;
  2576. // If the item is found in the array own body, then it is a number
  2577. if (JavascriptOperators::GetOwnItem(object, index, &member, scriptContext)
  2578. && !JavascriptOperators::IsUndefined(member))
  2579. {
  2580. return TRUE;
  2581. }
  2582. }
  2583. return FALSE;
  2584. }
  2585. BOOL _Check_return_ _Success_(return) JavascriptOperators::GetAccessors(RecyclableObject* instance, PropertyId propertyId, ScriptContext* requestContext, _Out_ Var* getter, _Out_ Var* setter)
  2586. {
  2587. RecyclableObject* object = instance;
  2588. while (!JavascriptOperators::IsNull(object))
  2589. {
  2590. if (object->GetAccessors(propertyId, getter, setter, requestContext))
  2591. {
  2592. *getter = JavascriptOperators::CanonicalizeAccessor(*getter, requestContext);
  2593. *setter = JavascriptOperators::CanonicalizeAccessor(*setter, requestContext);
  2594. return TRUE;
  2595. }
  2596. if (object->SkipsPrototype())
  2597. {
  2598. break;
  2599. }
  2600. object = JavascriptOperators::GetPrototype(object);
  2601. }
  2602. return FALSE;
  2603. }
  2604. BOOL JavascriptOperators::SetAccessors(RecyclableObject* instance, PropertyId propertyId, Var getter, Var setter, PropertyOperationFlags flags)
  2605. {
  2606. BOOL result = instance && instance->SetAccessors(propertyId, getter, setter, flags);
  2607. return result;
  2608. }
  2609. BOOL JavascriptOperators::OP_SetProperty(Var instance, PropertyId propertyId, Var newValue, ScriptContext* scriptContext, PropertyValueInfo * info, PropertyOperationFlags flags, Var thisInstance)
  2610. {
  2611. // The call into ToObject(dynamicObject) is avoided here by checking for null and undefined and doing nothing when dynamicObject is a primitive value.
  2612. if (thisInstance == nullptr)
  2613. {
  2614. thisInstance = instance;
  2615. }
  2616. TypeId typeId = JavascriptOperators::GetTypeId(instance);
  2617. if (JavascriptOperators::IsUndefinedOrNullType(typeId))
  2618. {
  2619. if (scriptContext->GetThreadContext()->RecordImplicitException())
  2620. {
  2621. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotSet_NullOrUndefined, scriptContext->GetPropertyName(propertyId)->GetBuffer());
  2622. }
  2623. return TRUE;
  2624. }
  2625. else if (typeId == TypeIds_VariantDate)
  2626. {
  2627. if (scriptContext->GetThreadContext()->RecordImplicitException())
  2628. {
  2629. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_VarDate, scriptContext->GetPropertyName(propertyId)->GetBuffer());
  2630. }
  2631. return TRUE;
  2632. }
  2633. if (!TaggedNumber::Is(instance) && !TaggedNumber::Is(thisInstance))
  2634. {
  2635. return JavascriptOperators::SetProperty(UnsafeVarTo<RecyclableObject>(thisInstance), UnsafeVarTo<RecyclableObject>(instance), propertyId, newValue, info, scriptContext, flags);
  2636. }
  2637. JavascriptError::ThrowCantAssignIfStrictMode(flags, scriptContext);
  2638. return false;
  2639. }
  2640. BOOL JavascriptOperators::OP_StFunctionExpression(Var obj, PropertyId propertyId, Var newValue)
  2641. {
  2642. RecyclableObject* instance = VarTo<RecyclableObject>(obj);
  2643. JIT_HELPER_NOT_REENTRANT_HEADER(Op_StFunctionExpression, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2644. instance->SetProperty(propertyId, newValue, PropertyOperation_None, NULL);
  2645. instance->SetWritable(propertyId, FALSE);
  2646. instance->SetConfigurable(propertyId, FALSE);
  2647. return TRUE;
  2648. JIT_HELPER_END(Op_StFunctionExpression);
  2649. }
  2650. BOOL JavascriptOperators::OP_InitClassMember(Var obj, PropertyId propertyId, Var newValue)
  2651. {
  2652. JIT_HELPER_REENTRANT_HEADER(Op_InitClassMember);
  2653. RecyclableObject* instance = VarTo<RecyclableObject>(obj);
  2654. PropertyOperationFlags flags = PropertyOperation_None;
  2655. PropertyAttributes attributes = PropertyClassMemberDefaults;
  2656. instance->SetPropertyWithAttributes(propertyId, newValue, attributes, NULL, flags);
  2657. return TRUE;
  2658. JIT_HELPER_END(Op_InitClassMember);
  2659. }
  2660. BOOL JavascriptOperators::OP_InitLetProperty(Var obj, PropertyId propertyId, Var newValue)
  2661. {
  2662. JIT_HELPER_REENTRANT_HEADER(Op_InitLetFld);
  2663. RecyclableObject* instance = VarTo<RecyclableObject>(obj);
  2664. PropertyOperationFlags flags = instance->GetScriptContext()->IsUndeclBlockVar(newValue) ? PropertyOperation_SpecialValue : PropertyOperation_None;
  2665. PropertyAttributes attributes = PropertyLetDefaults;
  2666. if (VarIs<RootObjectBase>(instance))
  2667. {
  2668. attributes |= PropertyLetConstGlobal;
  2669. }
  2670. instance->SetPropertyWithAttributes(propertyId, newValue, attributes, NULL, (PropertyOperationFlags)(flags | PropertyOperation_AllowUndecl));
  2671. return TRUE;
  2672. JIT_HELPER_END(Op_InitLetFld);
  2673. }
  2674. BOOL JavascriptOperators::OP_InitConstProperty(Var obj, PropertyId propertyId, Var newValue)
  2675. {
  2676. RecyclableObject* instance = VarTo<RecyclableObject>(obj);
  2677. JIT_HELPER_REENTRANT_HEADER(Op_InitConstFld);
  2678. PropertyOperationFlags flags = instance->GetScriptContext()->IsUndeclBlockVar(newValue) ? PropertyOperation_SpecialValue : PropertyOperation_None;
  2679. PropertyAttributes attributes = PropertyConstDefaults;
  2680. if (VarIs<RootObjectBase>(instance))
  2681. {
  2682. attributes |= PropertyLetConstGlobal;
  2683. }
  2684. instance->SetPropertyWithAttributes(propertyId, newValue, attributes, NULL, (PropertyOperationFlags)(flags | PropertyOperation_AllowUndecl));
  2685. return TRUE;
  2686. JIT_HELPER_END(Op_InitConstFld);
  2687. }
  2688. BOOL JavascriptOperators::OP_InitUndeclRootLetProperty(Var obj, PropertyId propertyId)
  2689. {
  2690. RecyclableObject* instance = VarTo<RecyclableObject>(obj);
  2691. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitUndeclRootLetFld, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2692. PropertyOperationFlags flags = static_cast<PropertyOperationFlags>(PropertyOperation_SpecialValue | PropertyOperation_AllowUndecl);
  2693. PropertyAttributes attributes = PropertyLetDefaults | PropertyLetConstGlobal;
  2694. instance->SetPropertyWithAttributes(propertyId, instance->GetLibrary()->GetUndeclBlockVar(), attributes, NULL, flags);
  2695. return TRUE;
  2696. JIT_HELPER_END(Op_InitUndeclRootLetFld);
  2697. }
  2698. BOOL JavascriptOperators::OP_InitUndeclRootConstProperty(Var obj, PropertyId propertyId)
  2699. {
  2700. RecyclableObject* instance = VarTo<RecyclableObject>(obj);
  2701. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitUndeclRootConstFld, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2702. PropertyOperationFlags flags = static_cast<PropertyOperationFlags>(PropertyOperation_SpecialValue | PropertyOperation_AllowUndecl);
  2703. PropertyAttributes attributes = PropertyConstDefaults | PropertyLetConstGlobal;
  2704. instance->SetPropertyWithAttributes(propertyId, instance->GetLibrary()->GetUndeclBlockVar(), attributes, NULL, flags);
  2705. return TRUE;
  2706. JIT_HELPER_END(Op_InitUndeclRootConstFld);
  2707. }
  2708. BOOL JavascriptOperators::OP_InitUndeclConsoleLetProperty(Var obj, PropertyId propertyId)
  2709. {
  2710. FrameDisplay *pScope = (FrameDisplay*)obj;
  2711. AssertMsg(VarIs<ConsoleScopeActivationObject>((DynamicObject*)pScope->GetItem(pScope->GetLength() - 1)), "How come we got this opcode without ConsoleScopeActivationObject?");
  2712. RecyclableObject* instance = VarTo<RecyclableObject>(pScope->GetItem(0));
  2713. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitUndeclConsoleLetFld, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2714. PropertyOperationFlags flags = static_cast<PropertyOperationFlags>(PropertyOperation_SpecialValue | PropertyOperation_AllowUndecl);
  2715. PropertyAttributes attributes = PropertyLetDefaults;
  2716. instance->SetPropertyWithAttributes(propertyId, instance->GetLibrary()->GetUndeclBlockVar(), attributes, NULL, flags);
  2717. return TRUE;
  2718. JIT_HELPER_END(Op_InitUndeclConsoleLetFld);
  2719. }
  2720. BOOL JavascriptOperators::OP_InitUndeclConsoleConstProperty(Var obj, PropertyId propertyId)
  2721. {
  2722. FrameDisplay *pScope = (FrameDisplay*)obj;
  2723. AssertMsg(VarIs<ConsoleScopeActivationObject>((DynamicObject*)pScope->GetItem(pScope->GetLength() - 1)), "How come we got this opcode without ConsoleScopeActivationObject?");
  2724. RecyclableObject* instance = VarTo<RecyclableObject>(pScope->GetItem(0));
  2725. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitUndeclConsoleConstFld, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2726. PropertyOperationFlags flags = static_cast<PropertyOperationFlags>(PropertyOperation_SpecialValue | PropertyOperation_AllowUndecl);
  2727. PropertyAttributes attributes = PropertyConstDefaults;
  2728. instance->SetPropertyWithAttributes(propertyId, instance->GetLibrary()->GetUndeclBlockVar(), attributes, NULL, flags);
  2729. return TRUE;
  2730. JIT_HELPER_END(Op_InitUndeclConsoleConstFld);
  2731. }
  2732. BOOL JavascriptOperators::InitProperty(RecyclableObject* instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  2733. {
  2734. return instance && instance->InitProperty(propertyId, newValue, flags);
  2735. }
  2736. BOOL JavascriptOperators::OP_InitProperty(Var instance, PropertyId propertyId, Var newValue)
  2737. {
  2738. if(TaggedNumber::Is(instance)) { return false; }
  2739. return JavascriptOperators::InitProperty(VarTo<RecyclableObject>(instance), propertyId, newValue);
  2740. }
  2741. BOOL JavascriptOperators::DeleteProperty(RecyclableObject* instance, PropertyId propertyId, PropertyOperationFlags propertyOperationFlags)
  2742. {
  2743. return DeleteProperty_Impl<false>(instance, propertyId, propertyOperationFlags);
  2744. }
  2745. bool JavascriptOperators::ShouldTryDeleteProperty(RecyclableObject* instance, JavascriptString *propertyNameString, PropertyRecord const **pPropertyRecord)
  2746. {
  2747. PropertyRecord const *propertyRecord = nullptr;
  2748. if (!JavascriptOperators::CanShortcutOnUnknownPropertyName(instance))
  2749. {
  2750. instance->GetScriptContext()->GetOrAddPropertyRecord(propertyNameString, &propertyRecord);
  2751. }
  2752. else
  2753. {
  2754. instance->GetScriptContext()->FindPropertyRecord(propertyNameString, &propertyRecord);
  2755. }
  2756. if (propertyRecord == nullptr)
  2757. {
  2758. return false;
  2759. }
  2760. *pPropertyRecord = propertyRecord;
  2761. return true;
  2762. }
  2763. BOOL JavascriptOperators::DeleteProperty(RecyclableObject* instance, JavascriptString *propertyNameString, PropertyOperationFlags propertyOperationFlags)
  2764. {
  2765. #ifdef ENABLE_MUTATION_BREAKPOINT
  2766. ScriptContext *scriptContext = instance->GetScriptContext();
  2767. if (MutationBreakpoint::IsFeatureEnabled(scriptContext)
  2768. && scriptContext->HasMutationBreakpoints())
  2769. {
  2770. MutationBreakpoint::HandleDeleteProperty(scriptContext, instance, propertyNameString);
  2771. }
  2772. #endif
  2773. return instance->DeleteProperty(propertyNameString, propertyOperationFlags);
  2774. }
  2775. BOOL JavascriptOperators::DeletePropertyUnscopables(RecyclableObject* instance, PropertyId propertyId, PropertyOperationFlags propertyOperationFlags)
  2776. {
  2777. return DeleteProperty_Impl<true>(instance, propertyId, propertyOperationFlags);
  2778. }
  2779. template<bool unscopables>
  2780. BOOL JavascriptOperators::DeleteProperty_Impl(RecyclableObject* instance, PropertyId propertyId, PropertyOperationFlags propertyOperationFlags)
  2781. {
  2782. if (unscopables && JavascriptOperators::IsPropertyUnscopable(instance, propertyId))
  2783. {
  2784. return false;
  2785. }
  2786. #ifdef ENABLE_MUTATION_BREAKPOINT
  2787. ScriptContext *scriptContext = instance->GetScriptContext();
  2788. if (MutationBreakpoint::IsFeatureEnabled(scriptContext)
  2789. && scriptContext->HasMutationBreakpoints())
  2790. {
  2791. MutationBreakpoint::HandleDeleteProperty(scriptContext, instance, propertyId);
  2792. }
  2793. #endif
  2794. // !unscopables will hit the return statement on the first iteration
  2795. return instance->DeleteProperty(propertyId, propertyOperationFlags);
  2796. }
  2797. Var JavascriptOperators::OP_DeleteProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  2798. {
  2799. JIT_HELPER_REENTRANT_HEADER(Op_DeleteProperty);
  2800. if(TaggedNumber::Is(instance))
  2801. {
  2802. return scriptContext->GetLibrary()->GetTrue();
  2803. }
  2804. RecyclableObject* recyclableObject = VarTo<RecyclableObject>(instance);
  2805. if (JavascriptOperators::IsUndefinedOrNull(recyclableObject))
  2806. {
  2807. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotDelete_NullOrUndefined,
  2808. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  2809. }
  2810. return scriptContext->GetLibrary()->CreateBoolean(
  2811. JavascriptOperators::DeleteProperty(recyclableObject, propertyId, propertyOperationFlags));
  2812. JIT_HELPER_END(Op_DeleteProperty);
  2813. }
  2814. Var JavascriptOperators::OP_DeleteRootProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  2815. {
  2816. // In Edge the root is an External Object which can call Dispose and thus, can have reentrancy.
  2817. JIT_HELPER_REENTRANT_HEADER(Op_DeleteRootProperty);
  2818. AssertMsg(VarIs<RootObjectBase>(instance), "Root must be a global object!");
  2819. RootObjectBase* rootObject = static_cast<RootObjectBase*>(instance);
  2820. return scriptContext->GetLibrary()->CreateBoolean(
  2821. rootObject->DeleteRootProperty(propertyId, propertyOperationFlags));
  2822. JIT_HELPER_END(Op_DeleteRootProperty);
  2823. }
  2824. template <bool IsFromFullJit, class TInlineCache>
  2825. inline void JavascriptOperators::PatchSetPropertyScoped(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var newValue, Var defaultInstance, PropertyOperationFlags propertyOperationFlags)
  2826. {
  2827. JIT_HELPER_REENTRANT_HEADER(Op_PatchSetPropertyScoped);
  2828. // Set the property using a scope stack rather than an individual instance.
  2829. // Walk the stack until we find an instance that has the property and store
  2830. // the new value there.
  2831. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  2832. uint16 length = pDisplay->GetLength();
  2833. RecyclableObject *object;
  2834. PropertyValueInfo info;
  2835. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  2836. bool allowUndecInConsoleScope = (propertyOperationFlags & PropertyOperation_AllowUndeclInConsoleScope) == PropertyOperation_AllowUndeclInConsoleScope;
  2837. bool isLexicalThisSlotSymbol = (propertyId == PropertyIds::_this);
  2838. for (uint16 i = 0; i < length; i++)
  2839. {
  2840. object = UnsafeVarTo<RecyclableObject>(pDisplay->GetItem(i));
  2841. AssertMsg(!VarIs<ConsoleScopeActivationObject>(object) || (i == length - 1), "Invalid location for ConsoleScopeActivationObject");
  2842. Type* type = object->GetType();
  2843. if (CacheOperators::TrySetProperty<true, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  2844. object, false, propertyId, newValue, scriptContext, propertyOperationFlags, nullptr, &info))
  2845. {
  2846. return;
  2847. }
  2848. // In scoped set property, we need to set the property when it is available; it could be a setter
  2849. // or normal property. we need to check setter first, and if no setter is available, but HasProperty
  2850. // is true, this must be a normal property.
  2851. // TODO: merge OP_HasProperty and GetSetter in one pass if there is perf problem. In fastDOM we have quite
  2852. // a lot of setters so separating the two might be actually faster.
  2853. BOOL setAccessorResult = FALSE;
  2854. if (SetAccessorOrNonWritableProperty(object, object, propertyId, newValue, &info, scriptContext, propertyOperationFlags, false, allowUndecInConsoleScope, &setAccessorResult))
  2855. {
  2856. return;
  2857. }
  2858. else if (!JavascriptOperators::IsObject(object))
  2859. {
  2860. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, scriptContext);
  2861. }
  2862. // Need to do a "get" of the current value (if any) to make sure that we're not writing to
  2863. // let/const before declaration, but we need to disable implicit calls around the "get",
  2864. // so we need to do a "has" first to make sure the "get" is valid (e.g., "get" on a HostDispatch
  2865. // with implicit calls disabled will always "succeed").
  2866. if (JavascriptOperators::HasProperty(object, propertyId))
  2867. {
  2868. DisableImplicitFlags disableImplicitFlags = scriptContext->GetThreadContext()->GetDisableImplicitFlags();
  2869. scriptContext->GetThreadContext()->SetDisableImplicitFlags(DisableImplicitCallAndExceptionFlag);
  2870. Var value;
  2871. BOOL result = JavascriptOperators::GetProperty(object, propertyId, &value, scriptContext, nullptr);
  2872. scriptContext->GetThreadContext()->SetDisableImplicitFlags(disableImplicitFlags);
  2873. if (result && scriptContext->IsUndeclBlockVar(value) && !allowUndecInConsoleScope && !isLexicalThisSlotSymbol)
  2874. {
  2875. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  2876. }
  2877. PropertyValueInfo info2;
  2878. PropertyValueInfo::SetCacheInfo(&info2, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  2879. PropertyOperationFlags setPropertyOpFlags = allowUndecInConsoleScope ? PropertyOperation_AllowUndeclInConsoleScope : PropertyOperation_None;
  2880. object->SetProperty(propertyId, newValue, setPropertyOpFlags, &info2);
  2881. #if DBG_DUMP
  2882. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  2883. {
  2884. CacheOperators::TraceCache(inlineCache, _u("PatchSetPropertyScoped"), propertyId, scriptContext, object);
  2885. }
  2886. #endif
  2887. if (!VarIs<JavascriptProxy>(object) && !allowUndecInConsoleScope)
  2888. {
  2889. CacheOperators::CachePropertyWrite(object, false, type, propertyId, &info2, scriptContext);
  2890. }
  2891. return;
  2892. }
  2893. }
  2894. Assert(!isLexicalThisSlotSymbol);
  2895. // If we have console scope and no one in the scope had the property add it to console scope
  2896. if ((length > 0) && VarIs<ConsoleScopeActivationObject>(pDisplay->GetItem(length - 1)))
  2897. {
  2898. // CheckPrototypesForAccessorOrNonWritableProperty does not check for const in global object. We should check it here.
  2899. if (length > 1)
  2900. {
  2901. Js::GlobalObject * globalObject = JavascriptOperators::TryFromVar<Js::GlobalObject>(pDisplay->GetItem(length - 2));
  2902. if (globalObject)
  2903. {
  2904. Var setterValue = nullptr;
  2905. DescriptorFlags flags = JavascriptOperators::GetRootSetter(globalObject, propertyId, &setterValue, &info, scriptContext);
  2906. Assert((flags & Accessor) != Accessor);
  2907. Assert((flags & Proxy) != Proxy);
  2908. if ((flags & Data) == Data && (flags & Writable) == None)
  2909. {
  2910. if (!allowUndecInConsoleScope)
  2911. {
  2912. if (flags & Const)
  2913. {
  2914. JavascriptError::ThrowTypeError(scriptContext, ERRAssignmentToConst);
  2915. }
  2916. Assert(!isLexicalThisSlotSymbol);
  2917. return;
  2918. }
  2919. }
  2920. }
  2921. }
  2922. RecyclableObject* obj = VarTo<RecyclableObject>(pDisplay->GetItem(length - 1));
  2923. OUTPUT_TRACE(Js::ConsoleScopePhase, _u("Adding property '%s' to console scope object\n"), scriptContext->GetPropertyName(propertyId)->GetBuffer());
  2924. JavascriptOperators::SetProperty(obj, obj, propertyId, newValue, scriptContext, propertyOperationFlags);
  2925. return;
  2926. }
  2927. // No one in the scope stack has the property, so add it to the default instance provided by the caller.
  2928. AssertMsg(!TaggedNumber::Is(defaultInstance), "Root object is an int or tagged float?");
  2929. Assert(defaultInstance != nullptr);
  2930. RecyclableObject* obj = VarTo<RecyclableObject>(defaultInstance);
  2931. {
  2932. //SetPropertyScoped does not use inline cache for default instance
  2933. PropertyValueInfo info2;
  2934. JavascriptOperators::SetRootProperty(obj, propertyId, newValue, &info2, scriptContext, (PropertyOperationFlags)(propertyOperationFlags | PropertyOperation_Root));
  2935. }
  2936. JIT_HELPER_END(Op_PatchSetPropertyScoped);
  2937. }
  2938. JIT_HELPER_TEMPLATE(Op_PatchSetPropertyScoped, Op_ConsolePatchSetPropertyScoped)
  2939. template void JavascriptOperators::PatchSetPropertyScoped<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var newValue, Var defaultInstance, PropertyOperationFlags propertyOperationFlags);
  2940. template void JavascriptOperators::PatchSetPropertyScoped<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var newValue, Var defaultInstance, PropertyOperationFlags propertyOperationFlags);
  2941. template void JavascriptOperators::PatchSetPropertyScoped<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var newValue, Var defaultInstance, PropertyOperationFlags propertyOperationFlags);
  2942. template void JavascriptOperators::PatchSetPropertyScoped<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var newValue, Var defaultInstance, PropertyOperationFlags propertyOperationFlags);
  2943. BOOL JavascriptOperators::OP_InitFuncScoped(FrameDisplay *pScope, PropertyId propertyId, Var newValue, Var defaultInstance, ScriptContext* scriptContext)
  2944. {
  2945. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitFuncScoped, reentrancylock, scriptContext->GetThreadContext());
  2946. int i;
  2947. int length = pScope->GetLength();
  2948. DynamicObject *obj;
  2949. for (i = 0; i < length; i++)
  2950. {
  2951. obj = (DynamicObject*)pScope->GetItem(i);
  2952. if (obj->InitFuncScoped(propertyId, newValue))
  2953. {
  2954. return TRUE;
  2955. }
  2956. }
  2957. AssertMsg(!TaggedNumber::Is(defaultInstance), "Root object is an int or tagged float?");
  2958. return VarTo<RecyclableObject>(defaultInstance)->InitFuncScoped(propertyId, newValue);
  2959. JIT_HELPER_END(Op_InitFuncScoped);
  2960. }
  2961. BOOL JavascriptOperators::OP_InitPropertyScoped(FrameDisplay *pScope, PropertyId propertyId, Var newValue, Var defaultInstance, ScriptContext* scriptContext)
  2962. {
  2963. int i;
  2964. int length = pScope->GetLength();
  2965. DynamicObject *obj;
  2966. for (i = 0; i < length; i++)
  2967. {
  2968. obj = (DynamicObject*)pScope->GetItem(i);
  2969. if (obj->InitPropertyScoped(propertyId, newValue))
  2970. {
  2971. return TRUE;
  2972. }
  2973. }
  2974. AssertMsg(!TaggedNumber::Is(defaultInstance), "Root object is an int or tagged float?");
  2975. return VarTo<RecyclableObject>(defaultInstance)->InitPropertyScoped(propertyId, newValue);
  2976. }
  2977. Var JavascriptOperators::OP_DeletePropertyScoped(
  2978. FrameDisplay *pScope,
  2979. PropertyId propertyId,
  2980. Var defaultInstance,
  2981. ScriptContext* scriptContext,
  2982. PropertyOperationFlags propertyOperationFlags)
  2983. {
  2984. JIT_HELPER_REENTRANT_HEADER(Op_DeletePropertyScoped);
  2985. JIT_HELPER_SAME_ATTRIBUTES(Op_DeleteRootProperty, Op_DeletePropertyScoped);
  2986. int i;
  2987. int length = pScope->GetLength();
  2988. for (i = 0; i < length; i++)
  2989. {
  2990. DynamicObject *obj = (DynamicObject*)pScope->GetItem(i);
  2991. if (JavascriptOperators::HasProperty(obj, propertyId))
  2992. {
  2993. return scriptContext->GetLibrary()->CreateBoolean(JavascriptOperators::DeleteProperty(obj, propertyId, propertyOperationFlags));
  2994. }
  2995. }
  2996. return JavascriptOperators::OP_DeleteRootProperty(VarTo<RecyclableObject>(defaultInstance), propertyId, scriptContext, propertyOperationFlags);
  2997. JIT_HELPER_END(Op_DeletePropertyScoped);
  2998. }
  2999. Var JavascriptOperators::OP_TypeofPropertyScoped(FrameDisplay *pScope, PropertyId propertyId, Var defaultInstance, ScriptContext* scriptContext)
  3000. {
  3001. JIT_HELPER_REENTRANT_HEADER(Op_TypeofPropertyScoped);
  3002. int i;
  3003. int length = pScope->GetLength();
  3004. for (i = 0; i < length; i++)
  3005. {
  3006. DynamicObject *obj = (DynamicObject*)pScope->GetItem(i);
  3007. if (JavascriptOperators::HasProperty(obj, propertyId))
  3008. {
  3009. return JavascriptOperators::TypeofFld(obj, propertyId, scriptContext);
  3010. }
  3011. }
  3012. return JavascriptOperators::TypeofRootFld(VarTo<RecyclableObject>(defaultInstance), propertyId, scriptContext);
  3013. JIT_HELPER_END(Op_TypeofPropertyScoped);
  3014. }
  3015. BOOL JavascriptOperators::HasOwnItem(RecyclableObject* object, uint32 index)
  3016. {
  3017. return object->HasOwnItem(index);
  3018. }
  3019. BOOL JavascriptOperators::HasItem(RecyclableObject* object, uint64 index)
  3020. {
  3021. PropertyRecord const * propertyRecord = nullptr;
  3022. ScriptContext* scriptContext = object->GetScriptContext();
  3023. JavascriptOperators::GetPropertyIdForInt(index, scriptContext, &propertyRecord);
  3024. return JavascriptOperators::HasProperty(object, propertyRecord->GetPropertyId());
  3025. }
  3026. BOOL JavascriptOperators::HasItem(RecyclableObject* object, uint32 index)
  3027. {
  3028. #if ENABLE_COPYONACCESS_ARRAY
  3029. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(object);
  3030. #endif
  3031. while (!JavascriptOperators::IsNull(object))
  3032. {
  3033. PropertyQueryFlags result;
  3034. if ((result = object->HasItemQuery(index)) != PropertyQueryFlags::Property_NotFound)
  3035. {
  3036. return JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  3037. }
  3038. // CONSIDER: Numeric property values shouldn't be on the prototype for now but if this changes
  3039. // we should add SkipsPrototype support here as well
  3040. object = JavascriptOperators::GetPrototypeNoTrap(object);
  3041. }
  3042. return false;
  3043. }
  3044. BOOL JavascriptOperators::GetOwnItem(RecyclableObject* object, uint32 index, Var* value, ScriptContext* requestContext)
  3045. {
  3046. return object->GetItem(object, index, value, requestContext);
  3047. }
  3048. BOOL JavascriptOperators::GetItem(Var instance, RecyclableObject* propertyObject, uint32 index, Var* value, ScriptContext* requestContext)
  3049. {
  3050. RecyclableObject* object = propertyObject;
  3051. while (!JavascriptOperators::IsNull(object))
  3052. {
  3053. PropertyQueryFlags result;
  3054. if ((result = object->GetItemQuery(instance, index, value, requestContext)) != PropertyQueryFlags::Property_NotFound)
  3055. {
  3056. return JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  3057. }
  3058. if (object->SkipsPrototype())
  3059. {
  3060. break;
  3061. }
  3062. object = JavascriptOperators::GetPrototypeNoTrap(object);
  3063. }
  3064. *value = requestContext->GetMissingItemResult();
  3065. return false;
  3066. }
  3067. BOOL JavascriptOperators::GetItemReference(Var instance, RecyclableObject* propertyObject, uint32 index, Var* value, ScriptContext* requestContext)
  3068. {
  3069. RecyclableObject* object = propertyObject;
  3070. while (!JavascriptOperators::IsNull(object))
  3071. {
  3072. PropertyQueryFlags result;
  3073. if ((result = object->GetItemReferenceQuery(instance, index, value, requestContext)) != PropertyQueryFlags::Property_NotFound)
  3074. {
  3075. return JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  3076. }
  3077. if (object->SkipsPrototype())
  3078. {
  3079. break;
  3080. }
  3081. object = JavascriptOperators::GetPrototypeNoTrap(object);
  3082. }
  3083. *value = requestContext->GetMissingItemResult();
  3084. return false;
  3085. }
  3086. BOOL JavascriptOperators::SetItem(Var receiver, RecyclableObject* object, uint64 index, Var value, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  3087. {
  3088. PropertyRecord const * propertyRecord = nullptr;
  3089. JavascriptOperators::GetPropertyIdForInt(index, scriptContext, &propertyRecord);
  3090. return JavascriptOperators::SetProperty(receiver, object, propertyRecord->GetPropertyId(), value, scriptContext, propertyOperationFlags);
  3091. }
  3092. BOOL JavascriptOperators::SetItem(Var receiver, RecyclableObject* object, uint32 index, Var value, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags, BOOL skipPrototypeCheck /* = FALSE */)
  3093. {
  3094. Var setterValueOrProxy = nullptr;
  3095. DescriptorFlags flags = None;
  3096. Assert(!TaggedNumber::Is(receiver));
  3097. if (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableItem(object, index, &setterValueOrProxy, &flags, scriptContext, skipPrototypeCheck))
  3098. {
  3099. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  3100. if ((flags & Accessor) == Accessor)
  3101. {
  3102. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, scriptContext) ||
  3103. JavascriptError::ThrowIfNotExtensibleUndefinedSetter(propertyOperationFlags, setterValueOrProxy, scriptContext))
  3104. {
  3105. return TRUE;
  3106. }
  3107. if (setterValueOrProxy)
  3108. {
  3109. RecyclableObject* func = VarTo<RecyclableObject>(setterValueOrProxy);
  3110. JavascriptOperators::CallSetter(func, receiver, value, scriptContext);
  3111. }
  3112. return TRUE;
  3113. }
  3114. else if ((flags & Proxy) == Proxy)
  3115. {
  3116. Assert(VarIs<JavascriptProxy>(setterValueOrProxy));
  3117. JavascriptProxy* proxy = VarTo<JavascriptProxy>(setterValueOrProxy);
  3118. const PropertyRecord* propertyRecord = nullptr;
  3119. proxy->PropertyIdFromInt(index, &propertyRecord);
  3120. return proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetItemKind, propertyRecord->GetPropertyId(), value, scriptContext, propertyOperationFlags, skipPrototypeCheck);
  3121. }
  3122. else
  3123. {
  3124. Assert((flags & Data) == Data && (flags & Writable) == None);
  3125. if ((propertyOperationFlags & PropertyOperationFlags::PropertyOperation_ThrowIfNotExtensible) == PropertyOperationFlags::PropertyOperation_ThrowIfNotExtensible)
  3126. {
  3127. JavascriptError::ThrowTypeError(scriptContext, JSERR_NonExtensibleObject);
  3128. }
  3129. JavascriptError::ThrowCantAssign(propertyOperationFlags, scriptContext, index);
  3130. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, scriptContext);
  3131. return FALSE;
  3132. }
  3133. }
  3134. else if (!JavascriptOperators::IsObject(receiver))
  3135. {
  3136. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, scriptContext);
  3137. return FALSE;
  3138. }
  3139. return (VarTo<RecyclableObject>(receiver))->SetItem(index, value, propertyOperationFlags);
  3140. }
  3141. BOOL JavascriptOperators::DeleteItem(RecyclableObject* object, uint32 index, PropertyOperationFlags propertyOperationFlags)
  3142. {
  3143. return object->DeleteItem(index, propertyOperationFlags);
  3144. }
  3145. BOOL JavascriptOperators::DeleteItem(RecyclableObject* object, uint64 index, PropertyOperationFlags propertyOperationFlags)
  3146. {
  3147. PropertyRecord const * propertyRecord = nullptr;
  3148. JavascriptOperators::GetPropertyIdForInt(index, object->GetScriptContext(), &propertyRecord);
  3149. return JavascriptOperators::DeleteProperty(object, propertyRecord->GetPropertyId(), propertyOperationFlags);
  3150. }
  3151. BOOL JavascriptOperators::OP_HasItem(Var instance, Var index, ScriptContext* scriptContext)
  3152. {
  3153. RecyclableObject* object = TaggedNumber::Is(instance) ?
  3154. scriptContext->GetLibrary()->GetNumberPrototype() :
  3155. VarTo<RecyclableObject>(instance);
  3156. uint32 indexVal;
  3157. PropertyRecord const * propertyRecord = nullptr;
  3158. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, false);
  3159. if (indexType == IndexType_Number)
  3160. {
  3161. return HasItem(object, indexVal);
  3162. }
  3163. else
  3164. {
  3165. Assert(indexType == IndexType_PropertyId);
  3166. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  3167. {
  3168. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
  3169. Assert(indexType == IndexType_PropertyId);
  3170. Assert(propertyRecord != nullptr);
  3171. }
  3172. if (propertyRecord != nullptr)
  3173. {
  3174. return HasProperty(object, propertyRecord->GetPropertyId());
  3175. }
  3176. else
  3177. {
  3178. #if DBG
  3179. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  3180. PropertyRecord const * debugPropertyRecord;
  3181. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  3182. AssertMsg(!JavascriptOperators::HasProperty(object, debugPropertyRecord->GetPropertyId()), "how did this property come? See OS Bug 2727708 if you see this come from the web");
  3183. #endif
  3184. return FALSE;
  3185. }
  3186. }
  3187. }
  3188. #if ENABLE_PROFILE_INFO
  3189. void JavascriptOperators::UpdateNativeArrayProfileInfoToCreateVarArray(Var instance, const bool expectingNativeFloatArray, const bool expectingVarArray)
  3190. {
  3191. Assert(instance);
  3192. Assert(expectingNativeFloatArray ^ expectingVarArray);
  3193. JavascriptNativeArray * nativeArr = JavascriptOperators::TryFromVar<JavascriptNativeArray>(instance);
  3194. if (!nativeArr)
  3195. {
  3196. return;
  3197. }
  3198. ArrayCallSiteInfo *const arrayCallSiteInfo = nativeArr->GetArrayCallSiteInfo();
  3199. if (!arrayCallSiteInfo)
  3200. {
  3201. return;
  3202. }
  3203. if (expectingNativeFloatArray)
  3204. {
  3205. // Profile data is expecting a native float array. Ensure that at the array's creation site, that a native int array
  3206. // is not created, such that the profiled array type would be correct.
  3207. arrayCallSiteInfo->SetIsNotNativeIntArray();
  3208. }
  3209. else
  3210. {
  3211. // Profile data is expecting a var array. Ensure that at the array's creation site, that a native array is not
  3212. // created, such that the profiled array type would be correct.
  3213. Assert(expectingVarArray);
  3214. arrayCallSiteInfo->SetIsNotNativeArray();
  3215. }
  3216. }
  3217. bool JavascriptOperators::SetElementMayHaveImplicitCalls(ScriptContext *const scriptContext)
  3218. {
  3219. return
  3220. scriptContext->optimizationOverrides.GetArraySetElementFastPathVtable() ==
  3221. ScriptContextOptimizationOverrideInfo::InvalidVtable;
  3222. }
  3223. #endif
  3224. RecyclableObject *JavascriptOperators::GetCallableObjectOrThrow(const Var callee, ScriptContext *const scriptContext)
  3225. {
  3226. Assert(callee);
  3227. Assert(scriptContext);
  3228. if (TaggedNumber::Is(callee))
  3229. {
  3230. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction /* TODO-ERROR: get arg name - aFunc */);
  3231. }
  3232. return UnsafeVarTo<RecyclableObject>(callee);
  3233. }
  3234. Var JavascriptOperators::OP_GetElementI_JIT(Var instance, Var index, ScriptContext *scriptContext)
  3235. {
  3236. JIT_HELPER_REENTRANT_HEADER(Op_GetElementI);
  3237. #if ENABLE_NATIVE_CODEGEN
  3238. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  3239. #endif
  3240. return OP_GetElementI(instance, index, scriptContext);
  3241. JIT_HELPER_END(Op_GetElementI);
  3242. }
  3243. Var JavascriptOperators::OP_GetElementI_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  3244. {
  3245. JIT_HELPER_REENTRANT_HEADER(Op_GetElementI_UInt32);
  3246. #if FLOATVAR
  3247. return OP_GetElementI_JIT(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  3248. #else
  3249. char buffer[sizeof(Js::JavascriptNumber)];
  3250. return OP_GetElementI_JIT(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3251. (Js::JavascriptNumber *)buffer), scriptContext);
  3252. #endif
  3253. JIT_HELPER_END(Op_GetElementI_UInt32);
  3254. }
  3255. Var JavascriptOperators::OP_GetElementI_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  3256. {
  3257. JIT_HELPER_REENTRANT_HEADER(Op_GetElementI_Int32);
  3258. #if FLOATVAR
  3259. return OP_GetElementI_JIT(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  3260. #else
  3261. char buffer[sizeof(Js::JavascriptNumber)];
  3262. return OP_GetElementI_JIT(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3263. (Js::JavascriptNumber *)buffer), scriptContext);
  3264. #endif
  3265. JIT_HELPER_END(Op_GetElementI_Int32);
  3266. }
  3267. BOOL JavascriptOperators::GetItemFromArrayPrototype(JavascriptArray * arr, int32 indexInt, Var * result, ScriptContext * scriptContext)
  3268. {
  3269. // try get from Array prototype
  3270. RecyclableObject* prototype = arr->GetPrototype();
  3271. if (JavascriptOperators::GetTypeId(prototype) != TypeIds_Array) //This can be TypeIds_ES5Array (or any other object changed through __proto__).
  3272. {
  3273. return false;
  3274. }
  3275. JavascriptArray* arrayPrototype = UnsafeVarTo<JavascriptArray>(prototype); //Prototype must be Array.prototype (unless changed through __proto__)
  3276. if (arrayPrototype->GetLength() && arrayPrototype->GetItem(arrayPrototype, (uint32)indexInt, result, scriptContext))
  3277. {
  3278. return true;
  3279. }
  3280. prototype = arrayPrototype->GetPrototype(); //Its prototype must be Object.prototype (unless changed through __proto__)
  3281. if (prototype->GetScriptContext()->GetLibrary()->GetObjectPrototype() != prototype)
  3282. {
  3283. return false;
  3284. }
  3285. if (VarTo<DynamicObject>(prototype)->HasNonEmptyObjectArray())
  3286. {
  3287. if (prototype->GetItem(arr, (uint32)indexInt, result, scriptContext))
  3288. {
  3289. return true;
  3290. }
  3291. }
  3292. *result = scriptContext->GetMissingItemResult();
  3293. return true;
  3294. }
  3295. Var JavascriptOperators::GetElementIIntIndex(_In_ Var instance, _In_ Var index, _In_ ScriptContext* scriptContext)
  3296. {
  3297. Assert(TaggedInt::Is(index));
  3298. switch (JavascriptOperators::GetTypeId(instance))
  3299. {
  3300. case TypeIds_Array: //fast path for array
  3301. {
  3302. Var result;
  3303. if (OP_GetElementI_ArrayFastPath(UnsafeVarTo<JavascriptArray>(instance), TaggedInt::ToInt32(index), &result, scriptContext))
  3304. {
  3305. return result;
  3306. }
  3307. break;
  3308. }
  3309. case TypeIds_NativeIntArray:
  3310. {
  3311. #if ENABLE_COPYONACCESS_ARRAY
  3312. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  3313. #endif
  3314. Var result;
  3315. if (OP_GetElementI_ArrayFastPath(UnsafeVarTo<JavascriptNativeIntArray>(instance), TaggedInt::ToInt32(index), &result, scriptContext))
  3316. {
  3317. return result;
  3318. }
  3319. break;
  3320. }
  3321. case TypeIds_NativeFloatArray:
  3322. {
  3323. Var result;
  3324. if (OP_GetElementI_ArrayFastPath(UnsafeVarTo<JavascriptNativeFloatArray>(instance), TaggedInt::ToInt32(index), &result, scriptContext))
  3325. {
  3326. return result;
  3327. }
  3328. break;
  3329. }
  3330. case TypeIds_String: // fast path for string
  3331. {
  3332. charcount_t indexInt = TaggedInt::ToUInt32(index);
  3333. JavascriptString* string = UnsafeVarTo<JavascriptString>(instance);
  3334. Var result;
  3335. if (JavascriptConversion::PropertyQueryFlagsToBoolean(string->JavascriptString::GetItemQuery(instance, indexInt, &result, scriptContext)))
  3336. {
  3337. return result;
  3338. }
  3339. break;
  3340. }
  3341. case TypeIds_Int8Array:
  3342. {
  3343. // The typed array will deal with all possible values for the index
  3344. int32 indexInt = TaggedInt::ToInt32(index);
  3345. if (VirtualTableInfo<Int8VirtualArray>::HasVirtualTable(instance))
  3346. {
  3347. Int8VirtualArray* int8Array = UnsafeVarTo<Int8VirtualArray>(instance);
  3348. if (indexInt >= 0)
  3349. {
  3350. return int8Array->DirectGetItem(indexInt);
  3351. }
  3352. }
  3353. else if (VirtualTableInfo<Int8Array>::HasVirtualTable(instance))
  3354. {
  3355. Int8Array* int8Array = UnsafeVarTo<Int8Array>(instance);
  3356. if (indexInt >= 0)
  3357. {
  3358. return int8Array->DirectGetItem(indexInt);
  3359. }
  3360. }
  3361. break;
  3362. }
  3363. case TypeIds_Uint8Array:
  3364. {
  3365. // The typed array will deal with all possible values for the index
  3366. int32 indexInt = TaggedInt::ToInt32(index);
  3367. if (VirtualTableInfo<Uint8VirtualArray>::HasVirtualTable(instance))
  3368. {
  3369. Uint8VirtualArray* uint8Array = UnsafeVarTo<Uint8VirtualArray>(instance);
  3370. if (indexInt >= 0)
  3371. {
  3372. return uint8Array->DirectGetItem(indexInt);
  3373. }
  3374. }
  3375. else if (VirtualTableInfo<Uint8Array>::HasVirtualTable(instance))
  3376. {
  3377. Uint8Array* uint8Array = UnsafeVarTo<Uint8Array>(instance);
  3378. if (indexInt >= 0)
  3379. {
  3380. return uint8Array->DirectGetItem(indexInt);
  3381. }
  3382. }
  3383. break;
  3384. }
  3385. case TypeIds_Uint8ClampedArray:
  3386. {
  3387. // The typed array will deal with all possible values for the index
  3388. int32 indexInt = TaggedInt::ToInt32(index);
  3389. if (VirtualTableInfo<Uint8ClampedVirtualArray>::HasVirtualTable(instance))
  3390. {
  3391. Uint8ClampedVirtualArray* uint8ClampedArray = UnsafeVarTo<Uint8ClampedVirtualArray>(instance);
  3392. if (indexInt >= 0)
  3393. {
  3394. return uint8ClampedArray->DirectGetItem(indexInt);
  3395. }
  3396. }
  3397. else if (VirtualTableInfo<Uint8ClampedArray>::HasVirtualTable(instance))
  3398. {
  3399. Uint8ClampedArray* uint8ClampedArray = UnsafeVarTo<Uint8ClampedArray>(instance);
  3400. if (indexInt >= 0)
  3401. {
  3402. return uint8ClampedArray->DirectGetItem(indexInt);
  3403. }
  3404. }
  3405. break;
  3406. }
  3407. case TypeIds_Int16Array:
  3408. {
  3409. // The type array will deal with all possible values for the index
  3410. int32 indexInt = TaggedInt::ToInt32(index);
  3411. if (VirtualTableInfo<Int16VirtualArray>::HasVirtualTable(instance))
  3412. {
  3413. Int16VirtualArray* int16Array = UnsafeVarTo<Int16VirtualArray>(instance);
  3414. if (indexInt >= 0)
  3415. {
  3416. return int16Array->DirectGetItem(indexInt);
  3417. }
  3418. }
  3419. else if (VirtualTableInfo<Int16Array>::HasVirtualTable(instance))
  3420. {
  3421. Int16Array* int16Array = UnsafeVarTo<Int16Array>(instance);
  3422. if (indexInt >= 0)
  3423. {
  3424. return int16Array->DirectGetItem(indexInt);
  3425. }
  3426. }
  3427. break;
  3428. }
  3429. case TypeIds_Uint16Array:
  3430. {
  3431. // The type array will deal with all possible values for the index
  3432. int32 indexInt = TaggedInt::ToInt32(index);
  3433. if (VirtualTableInfo<Uint16VirtualArray>::HasVirtualTable(instance))
  3434. {
  3435. Uint16VirtualArray* uint16Array = UnsafeVarTo<Uint16VirtualArray>(instance);
  3436. if (indexInt >= 0)
  3437. {
  3438. return uint16Array->DirectGetItem(indexInt);
  3439. }
  3440. }
  3441. else if (VirtualTableInfo<Uint16Array>::HasVirtualTable(instance))
  3442. {
  3443. Uint16Array* uint16Array = UnsafeVarTo<Uint16Array>(instance);
  3444. if (indexInt >= 0)
  3445. {
  3446. return uint16Array->DirectGetItem(indexInt);
  3447. }
  3448. }
  3449. break;
  3450. }
  3451. case TypeIds_Int32Array:
  3452. {
  3453. // The type array will deal with all possible values for the index
  3454. int32 indexInt = TaggedInt::ToInt32(index);
  3455. if (VirtualTableInfo<Int32VirtualArray>::HasVirtualTable(instance))
  3456. {
  3457. Int32VirtualArray* int32Array = UnsafeVarTo<Int32VirtualArray>(instance);
  3458. if (indexInt >= 0)
  3459. {
  3460. return int32Array->DirectGetItem(indexInt);
  3461. }
  3462. }
  3463. else if (VirtualTableInfo<Int32Array>::HasVirtualTable(instance))
  3464. {
  3465. Int32Array* int32Array = UnsafeVarTo<Int32Array>(instance);
  3466. if (indexInt >= 0)
  3467. {
  3468. return int32Array->DirectGetItem(indexInt);
  3469. }
  3470. }
  3471. break;
  3472. }
  3473. case TypeIds_Uint32Array:
  3474. {
  3475. // The type array will deal with all possible values for the index
  3476. int32 indexInt = TaggedInt::ToInt32(index);
  3477. if (VirtualTableInfo<Uint32VirtualArray>::HasVirtualTable(instance))
  3478. {
  3479. Uint32VirtualArray* uint32Array = UnsafeVarTo<Uint32VirtualArray>(instance);
  3480. if (indexInt >= 0)
  3481. {
  3482. return uint32Array->DirectGetItem(indexInt);
  3483. }
  3484. }
  3485. else if (VirtualTableInfo<Uint32Array>::HasVirtualTable(instance))
  3486. {
  3487. Uint32Array* uint32Array = UnsafeVarTo<Uint32Array>(instance);
  3488. if (indexInt >= 0)
  3489. {
  3490. return uint32Array->DirectGetItem(indexInt);
  3491. }
  3492. }
  3493. break;
  3494. }
  3495. case TypeIds_Float32Array:
  3496. {
  3497. // The type array will deal with all possible values for the index
  3498. int32 indexInt = TaggedInt::ToInt32(index);
  3499. if (VirtualTableInfo<Float32VirtualArray>::HasVirtualTable(instance))
  3500. {
  3501. Float32VirtualArray* float32Array = UnsafeVarTo<Float32VirtualArray>(instance);
  3502. if (indexInt >= 0)
  3503. {
  3504. return float32Array->DirectGetItem(indexInt);
  3505. }
  3506. }
  3507. else if (VirtualTableInfo<Float32Array>::HasVirtualTable(instance))
  3508. {
  3509. Float32Array* float32Array = UnsafeVarTo<Float32Array>(instance);
  3510. if (indexInt >= 0)
  3511. {
  3512. return float32Array->DirectGetItem(indexInt);
  3513. }
  3514. }
  3515. break;
  3516. }
  3517. case TypeIds_Float64Array:
  3518. {
  3519. // The type array will deal with all possible values for the index
  3520. int32 indexInt = TaggedInt::ToInt32(index);
  3521. if (VirtualTableInfo<Float64VirtualArray>::HasVirtualTable(instance))
  3522. {
  3523. Float64VirtualArray* float64Array = UnsafeVarTo<Float64VirtualArray>(instance);
  3524. if (indexInt >= 0)
  3525. {
  3526. return float64Array->DirectGetItem(indexInt);
  3527. }
  3528. }
  3529. else if (VirtualTableInfo<Float64Array>::HasVirtualTable(instance))
  3530. {
  3531. Float64Array* float64Array = UnsafeVarTo<Float64Array>(instance);
  3532. if (indexInt >= 0)
  3533. {
  3534. return float64Array->DirectGetItem(indexInt);
  3535. }
  3536. }
  3537. break;
  3538. }
  3539. default:
  3540. break;
  3541. }
  3542. return JavascriptOperators::GetElementIHelper(instance, index, instance, scriptContext);
  3543. }
  3544. template <typename T>
  3545. BOOL JavascriptOperators::OP_GetElementI_ArrayFastPath(T * arr, int indexInt, Var * result, ScriptContext * scriptContext)
  3546. {
  3547. #if ENABLE_COPYONACCESS_ARRAY
  3548. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(arr);
  3549. #endif
  3550. if (indexInt >= 0)
  3551. {
  3552. if (!CrossSite::IsCrossSiteObjectTyped(arr))
  3553. {
  3554. if (arr->T::DirectGetVarItemAt((uint32)indexInt, result, scriptContext))
  3555. {
  3556. return true;
  3557. }
  3558. }
  3559. else
  3560. {
  3561. if (arr->GetItem(arr, (uint32)indexInt, result, scriptContext))
  3562. {
  3563. return true;
  3564. }
  3565. }
  3566. return GetItemFromArrayPrototype(arr, indexInt, result, scriptContext);
  3567. }
  3568. return false;
  3569. }
  3570. Var JavascriptOperators::OP_GetElementI(Var instance, Var index, ScriptContext* scriptContext)
  3571. {
  3572. #ifdef ENABLE_SPECTRE_RUNTIME_MITIGATIONS
  3573. instance = BreakSpeculation(instance);
  3574. #endif
  3575. if (TaggedInt::Is(index))
  3576. {
  3577. return GetElementIIntIndex(instance, index, scriptContext);
  3578. }
  3579. if (JavascriptNumber::Is_NoTaggedIntCheck(index))
  3580. {
  3581. uint32 uint32Index = JavascriptConversion::ToUInt32(index, scriptContext);
  3582. if ((double)uint32Index == JavascriptNumber::GetValue(index) && !TaggedInt::IsOverflow(uint32Index))
  3583. {
  3584. index = TaggedInt::ToVarUnchecked(uint32Index);
  3585. return GetElementIIntIndex(instance, index, scriptContext);
  3586. }
  3587. }
  3588. else if (VarIs<RecyclableObject>(instance))
  3589. {
  3590. RecyclableObject* cacheOwner;
  3591. PropertyRecordUsageCache* propertyRecordUsageCache;
  3592. if (GetPropertyRecordUsageCache(index, scriptContext, &propertyRecordUsageCache, &cacheOwner))
  3593. {
  3594. return GetElementIWithCache<false /* ReturnOperationInfo */>(instance, cacheOwner, propertyRecordUsageCache, scriptContext, nullptr);
  3595. }
  3596. }
  3597. return JavascriptOperators::GetElementIHelper(instance, index, instance, scriptContext);
  3598. }
  3599. _Success_(return) bool JavascriptOperators::GetPropertyRecordUsageCache(Var index, ScriptContext* scriptContext, _Outptr_ PropertyRecordUsageCache** propertyRecordUsageCache, _Outptr_ RecyclableObject** cacheOwner)
  3600. {
  3601. JavascriptString* string = JavascriptOperators::TryFromVar<JavascriptString>(index);
  3602. if (string)
  3603. {
  3604. PropertyString * propertyString = nullptr;
  3605. if (VirtualTableInfo<Js::PropertyString>::HasVirtualTable(string))
  3606. {
  3607. propertyString = (PropertyString*)string;
  3608. }
  3609. else if (VirtualTableInfo<Js::LiteralStringWithPropertyStringPtr>::HasVirtualTable(string))
  3610. {
  3611. LiteralStringWithPropertyStringPtr * strWithPtr = (LiteralStringWithPropertyStringPtr *)string;
  3612. if (!strWithPtr->HasPropertyRecord())
  3613. {
  3614. PropertyRecord const * propertyRecord;
  3615. strWithPtr->GetPropertyRecord(&propertyRecord); // lookup-cache propertyRecord
  3616. }
  3617. else
  3618. {
  3619. propertyString = strWithPtr->GetOrAddPropertyString();
  3620. // this is the second time this property string is used
  3621. // we already had created the propertyRecord..
  3622. // now create the propertyString!
  3623. }
  3624. }
  3625. if (propertyString != nullptr)
  3626. {
  3627. *propertyRecordUsageCache = propertyString->GetPropertyRecordUsageCache();
  3628. *cacheOwner = propertyString;
  3629. return true;
  3630. }
  3631. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  3632. if (PHASE_TRACE1(PropertyCachePhase))
  3633. {
  3634. Output::Print(_u("PropertyCache: GetElem No property string for '%s'\n"), string->GetString());
  3635. }
  3636. #endif
  3637. #if DBG_DUMP
  3638. scriptContext->forinNoCache++;
  3639. #endif
  3640. }
  3641. JavascriptSymbol* symbol = JavascriptOperators::TryFromVar<JavascriptSymbol>(index);
  3642. if (symbol)
  3643. {
  3644. *propertyRecordUsageCache = symbol->GetPropertyRecordUsageCache();
  3645. *cacheOwner = symbol;
  3646. return true;
  3647. }
  3648. return false;
  3649. }
  3650. bool JavascriptOperators::SetElementIOnTaggedNumber(
  3651. _In_ Var receiver,
  3652. _In_ RecyclableObject* object,
  3653. _In_ Var index,
  3654. _In_ Var value,
  3655. _In_ ScriptContext* requestContext,
  3656. _In_ PropertyOperationFlags propertyOperationFlags)
  3657. {
  3658. Assert(TaggedNumber::Is(receiver));
  3659. uint32 indexVal = 0;
  3660. PropertyRecord const * propertyRecord = nullptr;
  3661. IndexType indexType = GetIndexType(index, requestContext, &indexVal, &propertyRecord, true);
  3662. if (indexType == IndexType_Number)
  3663. {
  3664. return JavascriptOperators::SetItemOnTaggedNumber(receiver, object, indexVal, value, requestContext, propertyOperationFlags);
  3665. }
  3666. else
  3667. {
  3668. return JavascriptOperators::SetPropertyOnTaggedNumber(receiver, object, propertyRecord->GetPropertyId(), value, requestContext, propertyOperationFlags);
  3669. }
  3670. }
  3671. template <bool ReturnOperationInfo>
  3672. bool JavascriptOperators::SetElementIWithCache(
  3673. _In_ Var receiver,
  3674. _In_ RecyclableObject* object,
  3675. _In_ RecyclableObject* index,
  3676. _In_ Var value,
  3677. _In_ PropertyRecordUsageCache* propertyRecordUsageCache,
  3678. _In_ ScriptContext* scriptContext,
  3679. _In_ PropertyOperationFlags flags,
  3680. _Inout_opt_ PropertyCacheOperationInfo* operationInfo)
  3681. {
  3682. if (TaggedNumber::Is(receiver))
  3683. {
  3684. return JavascriptOperators::SetElementIOnTaggedNumber(receiver, object, index, value, scriptContext, flags);
  3685. }
  3686. PropertyRecord const * propertyRecord = propertyRecordUsageCache->GetPropertyRecord();
  3687. if (propertyRecord->IsNumeric())
  3688. {
  3689. return JavascriptOperators::SetItem(receiver, object, propertyRecord->GetNumericValue(), value, scriptContext, flags);
  3690. }
  3691. PropertyValueInfo info;
  3692. if (receiver == object)
  3693. {
  3694. if (propertyRecordUsageCache->TrySetPropertyFromCache<ReturnOperationInfo>(object, value, scriptContext, flags, &info, index, operationInfo))
  3695. {
  3696. return true;
  3697. }
  3698. }
  3699. PropertyId propId = propertyRecord->GetPropertyId();
  3700. if (propId == PropertyIds::NaN || propId == PropertyIds::Infinity)
  3701. {
  3702. // As we no longer convert o[x] into o.x for NaN and Infinity, we need to follow SetProperty convention for these,
  3703. // which would check for read-only properties, strict mode, etc.
  3704. // Note that "-Infinity" does not qualify as property name, so we don't have to take care of it.
  3705. return JavascriptOperators::SetProperty(receiver, object, propId, value, scriptContext, flags);
  3706. }
  3707. return JavascriptOperators::SetPropertyWPCache(receiver, object, propId, value, scriptContext, flags, &info);
  3708. }
  3709. template bool JavascriptOperators::SetElementIWithCache<false>(Var receiver, RecyclableObject* object, RecyclableObject* index, Var value, PropertyRecordUsageCache* propertyRecordUsageCache, ScriptContext* scriptContext, PropertyOperationFlags flags, PropertyCacheOperationInfo* operationInfo);
  3710. template bool JavascriptOperators::SetElementIWithCache<true>(Var receiver, RecyclableObject* object, RecyclableObject* index, Var value, PropertyRecordUsageCache* propertyRecordUsageCache, ScriptContext* scriptContext, PropertyOperationFlags flags, PropertyCacheOperationInfo* operationInfo);
  3711. template <bool ReturnOperationInfo>
  3712. Var JavascriptOperators::GetElementIWithCache(
  3713. _In_ Var instance,
  3714. _In_ RecyclableObject* index,
  3715. _In_ PropertyRecordUsageCache* propertyRecordUsageCache,
  3716. _In_ ScriptContext* scriptContext,
  3717. _Inout_opt_ PropertyCacheOperationInfo* operationInfo)
  3718. {
  3719. RecyclableObject* object = nullptr;
  3720. if (!JavascriptOperators::GetPropertyObjectForGetElementI(instance, index, scriptContext, &object))
  3721. {
  3722. return scriptContext->GetLibrary()->GetUndefined();
  3723. }
  3724. PropertyRecord const * propertyRecord = propertyRecordUsageCache->GetPropertyRecord();
  3725. Var value;
  3726. if (propertyRecord->IsNumeric())
  3727. {
  3728. if (JavascriptOperators::GetItem(instance, object, propertyRecord->GetNumericValue(), &value, scriptContext))
  3729. {
  3730. return value;
  3731. }
  3732. }
  3733. else
  3734. {
  3735. PropertyValueInfo info;
  3736. if (propertyRecordUsageCache->TryGetPropertyFromCache<false /* OwnPropertyOnly */, false /* OutputExistence */, ReturnOperationInfo>(instance, object, &value, scriptContext, &info, index, operationInfo))
  3737. {
  3738. return value;
  3739. }
  3740. if (JavascriptOperators::GetPropertyWPCache<false /* OutputExistence */>(instance, object, propertyRecord->GetPropertyId(), &value, scriptContext, &info))
  3741. {
  3742. return value;
  3743. }
  3744. }
  3745. return scriptContext->GetLibrary()->GetUndefined();
  3746. }
  3747. template Var JavascriptOperators::GetElementIWithCache<false>(Var instance, RecyclableObject* index, PropertyRecordUsageCache* propertyRecordUsageCache, ScriptContext* scriptContext, PropertyCacheOperationInfo* operationInfo);
  3748. template Var JavascriptOperators::GetElementIWithCache<true>(Var instance, RecyclableObject* index, PropertyRecordUsageCache* propertyRecordUsageCache, ScriptContext* scriptContext, PropertyCacheOperationInfo* operationInfo);
  3749. Var JavascriptOperators::GetElementIHelper(Var instance, Var index, Var receiver, ScriptContext* scriptContext)
  3750. {
  3751. RecyclableObject* object = nullptr;
  3752. if (!JavascriptOperators::GetPropertyObjectForGetElementI(instance, index, scriptContext, &object))
  3753. {
  3754. return scriptContext->GetLibrary()->GetUndefined();
  3755. }
  3756. uint32 indexVal;
  3757. PropertyRecord const * propertyRecord = nullptr;
  3758. JavascriptString * propertyNameString = nullptr;
  3759. Var value = nullptr;
  3760. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, &propertyNameString, false, true);
  3761. if (indexType == IndexType_Number)
  3762. {
  3763. if (JavascriptOperators::GetItem(receiver, object, indexVal, &value, scriptContext))
  3764. {
  3765. return value;
  3766. }
  3767. }
  3768. else if (indexType == IndexType_JavascriptString)
  3769. {
  3770. PropertyValueInfo info;
  3771. if (JavascriptOperators::GetPropertyWPCache<false /* OutputExistence */>(receiver, object, propertyNameString, &value, scriptContext, &info))
  3772. {
  3773. return value;
  3774. }
  3775. }
  3776. else
  3777. {
  3778. Assert(indexType == IndexType_PropertyId);
  3779. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  3780. {
  3781. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, &propertyNameString, true, true);
  3782. Assert(indexType == IndexType_PropertyId);
  3783. Assert(propertyRecord != nullptr);
  3784. }
  3785. if (propertyRecord != nullptr)
  3786. {
  3787. PropertyValueInfo info;
  3788. if (JavascriptOperators::GetPropertyWPCache<false /* OutputExistence */>(receiver, object, propertyRecord->GetPropertyId(), &value, scriptContext, &info))
  3789. {
  3790. return value;
  3791. }
  3792. }
  3793. #if DBG
  3794. else
  3795. {
  3796. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  3797. PropertyRecord const * debugPropertyRecord;
  3798. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  3799. AssertMsg(!JavascriptOperators::GetProperty(receiver, object, debugPropertyRecord->GetPropertyId(), &value, scriptContext), "how did this property come? See OS Bug 2727708 if you see this come from the web");
  3800. }
  3801. #endif
  3802. }
  3803. return scriptContext->GetMissingItemResult();
  3804. }
  3805. int32 JavascriptOperators::OP_GetNativeIntElementI(Var instance, Var index)
  3806. {
  3807. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeIntElementI);
  3808. #if ENABLE_COPYONACCESS_ARRAY
  3809. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  3810. #endif
  3811. if (TaggedInt::Is(index))
  3812. {
  3813. int32 indexInt = TaggedInt::ToInt32(index);
  3814. if (indexInt < 0)
  3815. {
  3816. return JavascriptNativeIntArray::MissingItem;
  3817. }
  3818. JavascriptArray * arr = VarTo<JavascriptArray>(instance);
  3819. int32 result;
  3820. if (arr->DirectGetItemAt((uint32)indexInt, &result))
  3821. {
  3822. return result;
  3823. }
  3824. }
  3825. else if (JavascriptNumber::Is_NoTaggedIntCheck(index))
  3826. {
  3827. int32 indexInt;
  3828. bool isInt32;
  3829. double dIndex = JavascriptNumber::GetValue(index);
  3830. if (JavascriptNumber::TryGetInt32OrUInt32Value(dIndex, &indexInt, &isInt32))
  3831. {
  3832. if (isInt32 && indexInt < 0)
  3833. {
  3834. return JavascriptNativeIntArray::MissingItem;
  3835. }
  3836. JavascriptArray * arr = VarTo<JavascriptArray>(instance);
  3837. int32 result;
  3838. if (arr->DirectGetItemAt((uint32)indexInt, &result))
  3839. {
  3840. return result;
  3841. }
  3842. }
  3843. }
  3844. else
  3845. {
  3846. AssertMsg(false, "Non-numerical index in this helper?");
  3847. }
  3848. return JavascriptNativeIntArray::MissingItem;
  3849. JIT_HELPER_END(Op_GetNativeIntElementI);
  3850. }
  3851. int32 JavascriptOperators::OP_GetNativeIntElementI_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  3852. {
  3853. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeIntElementI_UInt32);
  3854. JIT_HELPER_SAME_ATTRIBUTES(Op_GetNativeIntElementI_UInt32, Op_GetNativeIntElementI);
  3855. #if FLOATVAR
  3856. return OP_GetNativeIntElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext));
  3857. #else
  3858. char buffer[sizeof(Js::JavascriptNumber)];
  3859. return OP_GetNativeIntElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3860. (Js::JavascriptNumber *)buffer));
  3861. #endif
  3862. JIT_HELPER_END(Op_GetNativeIntElementI_UInt32);
  3863. }
  3864. int32 JavascriptOperators::OP_GetNativeIntElementI_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  3865. {
  3866. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeIntElementI_Int32);
  3867. JIT_HELPER_SAME_ATTRIBUTES(Op_GetNativeIntElementI_Int32, Op_GetNativeIntElementI);
  3868. #if FLOATVAR
  3869. return OP_GetNativeIntElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext));
  3870. #else
  3871. char buffer[sizeof(Js::JavascriptNumber)];
  3872. return OP_GetNativeIntElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3873. (Js::JavascriptNumber *)buffer));
  3874. #endif
  3875. JIT_HELPER_END(Op_GetNativeIntElementI_Int32);
  3876. }
  3877. double JavascriptOperators::OP_GetNativeFloatElementI(Var instance, Var index)
  3878. {
  3879. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeFloatElementI);
  3880. double result = 0;
  3881. if (TaggedInt::Is(index))
  3882. {
  3883. int32 indexInt = TaggedInt::ToInt32(index);
  3884. if (indexInt < 0)
  3885. {
  3886. result = JavascriptNativeFloatArray::MissingItem;
  3887. }
  3888. else
  3889. {
  3890. JavascriptArray * arr = VarTo<JavascriptArray>(instance);
  3891. if (!arr->DirectGetItemAt((uint32)indexInt, &result))
  3892. {
  3893. result = JavascriptNativeFloatArray::MissingItem;
  3894. }
  3895. }
  3896. }
  3897. else if (JavascriptNumber::Is_NoTaggedIntCheck(index))
  3898. {
  3899. int32 indexInt;
  3900. bool isInt32;
  3901. double dIndex = JavascriptNumber::GetValue(index);
  3902. if (JavascriptNumber::TryGetInt32OrUInt32Value(dIndex, &indexInt, &isInt32))
  3903. {
  3904. if (isInt32 && indexInt < 0)
  3905. {
  3906. result = JavascriptNativeFloatArray::MissingItem;
  3907. }
  3908. else
  3909. {
  3910. JavascriptArray * arr = VarTo<JavascriptArray>(instance);
  3911. if (!arr->DirectGetItemAt((uint32)indexInt, &result))
  3912. {
  3913. result = JavascriptNativeFloatArray::MissingItem;
  3914. }
  3915. }
  3916. }
  3917. }
  3918. else
  3919. {
  3920. AssertMsg(false, "Non-numerical index in this helper?");
  3921. }
  3922. return result;
  3923. JIT_HELPER_END(Op_GetNativeFloatElementI);
  3924. }
  3925. double JavascriptOperators::OP_GetNativeFloatElementI_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  3926. {
  3927. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeFloatElementI_UInt32);
  3928. JIT_HELPER_SAME_ATTRIBUTES(Op_GetNativeFloatElementI_UInt32, Op_GetNativeFloatElementI);
  3929. #if FLOATVAR
  3930. return OP_GetNativeFloatElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext));
  3931. #else
  3932. char buffer[sizeof(Js::JavascriptNumber)];
  3933. return OP_GetNativeFloatElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3934. (Js::JavascriptNumber *)buffer));
  3935. #endif
  3936. JIT_HELPER_END(Op_GetNativeFloatElementI_UInt32);
  3937. }
  3938. double JavascriptOperators::OP_GetNativeFloatElementI_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  3939. {
  3940. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeFloatElementI_Int32);
  3941. JIT_HELPER_SAME_ATTRIBUTES(Op_GetNativeFloatElementI_Int32, Op_GetNativeFloatElementI);
  3942. #if FLOATVAR
  3943. return OP_GetNativeFloatElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext));
  3944. #else
  3945. char buffer[sizeof(Js::JavascriptNumber)];
  3946. return OP_GetNativeFloatElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3947. (Js::JavascriptNumber *)buffer));
  3948. #endif
  3949. JIT_HELPER_END(Op_GetNativeFloatElementI_Int32);
  3950. }
  3951. Var JavascriptOperators::OP_GetMethodElement_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  3952. {
  3953. JIT_HELPER_REENTRANT_HEADER(Op_GetMethodElement_UInt32);
  3954. JIT_HELPER_SAME_ATTRIBUTES(Op_GetMethodElement_UInt32, Op_GetMethodElement);
  3955. #if FLOATVAR
  3956. return OP_GetMethodElement(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  3957. #else
  3958. char buffer[sizeof(Js::JavascriptNumber)];
  3959. return OP_GetMethodElement(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3960. (Js::JavascriptNumber *)buffer), scriptContext);
  3961. #endif
  3962. JIT_HELPER_END(Op_GetMethodElement_UInt32);
  3963. }
  3964. Var JavascriptOperators::OP_GetMethodElement_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  3965. {
  3966. JIT_HELPER_REENTRANT_HEADER(Op_GetMethodElement_Int32);
  3967. JIT_HELPER_SAME_ATTRIBUTES(Op_GetMethodElement_Int32, Op_GetMethodElement);
  3968. #if FLOATVAR
  3969. return OP_GetElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  3970. #else
  3971. char buffer[sizeof(Js::JavascriptNumber)];
  3972. return OP_GetMethodElement(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3973. (Js::JavascriptNumber *)buffer), scriptContext);
  3974. #endif
  3975. JIT_HELPER_END(Op_GetMethodElement_Int32);
  3976. }
  3977. Var JavascriptOperators::OP_GetMethodElement(Var instance, Var index, ScriptContext* scriptContext)
  3978. {
  3979. JIT_HELPER_REENTRANT_HEADER(Op_GetMethodElement);
  3980. RecyclableObject* object = nullptr;
  3981. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  3982. {
  3983. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined, GetPropertyDisplayNameForError(index, scriptContext));
  3984. }
  3985. ThreadContext* threadContext = scriptContext->GetThreadContext();
  3986. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  3987. threadContext->ClearImplicitCallFlags();
  3988. uint32 indexVal;
  3989. PropertyRecord const * propertyRecord = nullptr;
  3990. Var value = NULL;
  3991. BOOL hasProperty = FALSE;
  3992. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, false);
  3993. if (indexType == IndexType_Number)
  3994. {
  3995. hasProperty = JavascriptOperators::GetItemReference(instance, object, indexVal, &value, scriptContext);
  3996. }
  3997. else
  3998. {
  3999. Assert(indexType == IndexType_PropertyId);
  4000. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  4001. {
  4002. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
  4003. Assert(indexType == IndexType_PropertyId);
  4004. Assert(propertyRecord != nullptr);
  4005. }
  4006. if (propertyRecord != nullptr)
  4007. {
  4008. hasProperty = JavascriptOperators::GetPropertyReference(instance, object, propertyRecord->GetPropertyId(), &value, scriptContext, NULL);
  4009. }
  4010. #if DBG
  4011. else
  4012. {
  4013. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  4014. PropertyRecord const * debugPropertyRecord;
  4015. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  4016. AssertMsg(!JavascriptOperators::GetPropertyReference(instance, object, debugPropertyRecord->GetPropertyId(), &value, scriptContext, NULL),
  4017. "how did this property come? See OS Bug 2727708 if you see this come from the web");
  4018. }
  4019. #endif
  4020. }
  4021. if (!hasProperty)
  4022. {
  4023. JavascriptString* varName = nullptr;
  4024. if (indexType == IndexType_PropertyId && propertyRecord != nullptr && propertyRecord->IsSymbol())
  4025. {
  4026. varName = JavascriptSymbol::ToString(propertyRecord, scriptContext);
  4027. }
  4028. else
  4029. {
  4030. varName = JavascriptConversion::ToString(index, scriptContext);
  4031. }
  4032. // ES5 11.2.3 #2: We evaluate the call target but don't throw yet if target member is missing. We need to evaluate argList
  4033. // first (#3). Postpone throwing error to invoke time.
  4034. value = ThrowErrorObject::CreateThrowTypeErrorObject(scriptContext, VBSERR_OLENoPropOrMethod, varName);
  4035. }
  4036. else if(!JavascriptConversion::IsCallable(value))
  4037. {
  4038. // ES5 11.2.3 #2: We evaluate the call target but don't throw yet if target member is missing. We need to evaluate argList
  4039. // first (#3). Postpone throwing error to invoke time.
  4040. JavascriptString* varName = JavascriptConversion::ToString(index, scriptContext);
  4041. value = ThrowErrorObject::CreateThrowTypeErrorObject(scriptContext, JSERR_Property_NeedFunction, varName);
  4042. }
  4043. threadContext->CheckAndResetImplicitCallAccessorFlag();
  4044. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  4045. return value;
  4046. JIT_HELPER_END(Op_GetMethodElement);
  4047. }
  4048. BOOL JavascriptOperators::OP_SetElementI_UInt32(Var instance, uint32 index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  4049. {
  4050. JIT_HELPER_REENTRANT_HEADER(Op_SetElementI_UInt32);
  4051. JIT_HELPER_SAME_ATTRIBUTES(Op_SetElementI_UInt32, Op_SetElementI);
  4052. #if FLOATVAR
  4053. return OP_SetElementI_JIT(instance, Js::JavascriptNumber::ToVar(index, scriptContext), value, scriptContext, flags);
  4054. #else
  4055. char buffer[sizeof(Js::JavascriptNumber)];
  4056. return OP_SetElementI_JIT(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  4057. (Js::JavascriptNumber *)buffer), value, scriptContext, flags);
  4058. #endif
  4059. JIT_HELPER_END(Op_SetElementI_UInt32);
  4060. }
  4061. BOOL JavascriptOperators::OP_SetElementI_Int32(Var instance, int32 index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  4062. {
  4063. JIT_HELPER_REENTRANT_HEADER(Op_SetElementI_Int32);
  4064. JIT_HELPER_SAME_ATTRIBUTES(Op_SetElementI_Int32, Op_SetElementI);
  4065. #if FLOATVAR
  4066. return OP_SetElementI_JIT(instance, Js::JavascriptNumber::ToVar(index, scriptContext), value, scriptContext, flags);
  4067. #else
  4068. char buffer[sizeof(Js::JavascriptNumber)];
  4069. return OP_SetElementI_JIT(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  4070. (Js::JavascriptNumber *)buffer), value, scriptContext, flags);
  4071. #endif
  4072. JIT_HELPER_END(Op_SetElementI_Int32);
  4073. }
  4074. BOOL JavascriptOperators::OP_SetElementI_JIT(Var instance, Var index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  4075. {
  4076. JIT_HELPER_REENTRANT_HEADER(Op_SetElementI);
  4077. if (TaggedNumber::Is(instance))
  4078. {
  4079. return OP_SetElementI(instance, index, value, scriptContext, flags);
  4080. }
  4081. INT_PTR vt = VirtualTableInfoBase::GetVirtualTable(instance);
  4082. OP_SetElementI(instance, index, value, scriptContext, flags);
  4083. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4084. JIT_HELPER_END(Op_SetElementI);
  4085. }
  4086. BOOL JavascriptOperators::OP_SetElementI(Var instance, Var index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  4087. {
  4088. #if ENABLE_COPYONACCESS_ARRAY
  4089. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  4090. #endif
  4091. TypeId instanceType = JavascriptOperators::GetTypeId(instance);
  4092. bool isTypedArray = (instanceType >= TypeIds_Int8Array && instanceType <= TypeIds_Float64Array);
  4093. if (isTypedArray)
  4094. {
  4095. if (TaggedInt::Is(index) || JavascriptNumber::Is_NoTaggedIntCheck(index) || VarIs<JavascriptString>(index))
  4096. {
  4097. BOOL returnValue = FALSE;
  4098. bool isNumericIndex = false;
  4099. // CrossSite types will go down the slow path.
  4100. switch (instanceType)
  4101. {
  4102. case TypeIds_Int8Array:
  4103. {
  4104. // The typed array will deal with all possible values for the index
  4105. if (VirtualTableInfo<Int8VirtualArray>::HasVirtualTable(instance))
  4106. {
  4107. Int8VirtualArray* int8Array = UnsafeVarTo<Int8VirtualArray>(instance);
  4108. returnValue = int8Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4109. }
  4110. else if( VirtualTableInfo<Int8Array>::HasVirtualTable(instance))
  4111. {
  4112. Int8Array* int8Array = UnsafeVarTo<Int8Array>(instance);
  4113. returnValue = int8Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4114. }
  4115. break;
  4116. }
  4117. case TypeIds_Uint8Array:
  4118. {
  4119. // The typed array will deal with all possible values for the index
  4120. if (VirtualTableInfo<Uint8VirtualArray>::HasVirtualTable(instance))
  4121. {
  4122. Uint8VirtualArray* uint8Array = UnsafeVarTo<Uint8VirtualArray>(instance);
  4123. returnValue = uint8Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4124. }
  4125. else if (VirtualTableInfo<Uint8Array>::HasVirtualTable(instance))
  4126. {
  4127. Uint8Array* uint8Array = UnsafeVarTo<Uint8Array>(instance);
  4128. returnValue = uint8Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4129. }
  4130. break;
  4131. }
  4132. case TypeIds_Uint8ClampedArray:
  4133. {
  4134. // The typed array will deal with all possible values for the index
  4135. if (VirtualTableInfo<Uint8ClampedVirtualArray>::HasVirtualTable(instance))
  4136. {
  4137. Uint8ClampedVirtualArray* uint8ClampedArray = UnsafeVarTo<Uint8ClampedVirtualArray>(instance);
  4138. returnValue = uint8ClampedArray->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4139. }
  4140. else if(VirtualTableInfo<Uint8ClampedArray>::HasVirtualTable(instance))
  4141. {
  4142. Uint8ClampedArray* uint8ClampedArray = UnsafeVarTo<Uint8ClampedArray>(instance);
  4143. returnValue = uint8ClampedArray->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4144. }
  4145. break;
  4146. }
  4147. case TypeIds_Int16Array:
  4148. {
  4149. // The type array will deal with all possible values for the index
  4150. if (VirtualTableInfo<Int16VirtualArray>::HasVirtualTable(instance))
  4151. {
  4152. Int16VirtualArray* int16Array = UnsafeVarTo<Int16VirtualArray>(instance);
  4153. returnValue = int16Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4154. }
  4155. else if (VirtualTableInfo<Int16Array>::HasVirtualTable(instance))
  4156. {
  4157. Int16Array* int16Array = UnsafeVarTo<Int16Array>(instance);
  4158. returnValue = int16Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4159. }
  4160. break;
  4161. }
  4162. case TypeIds_Uint16Array:
  4163. {
  4164. // The type array will deal with all possible values for the index
  4165. if (VirtualTableInfo<Uint16VirtualArray>::HasVirtualTable(instance))
  4166. {
  4167. Uint16VirtualArray* uint16Array = UnsafeVarTo<Uint16VirtualArray>(instance);
  4168. returnValue = uint16Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4169. }
  4170. else if (VirtualTableInfo<Uint16Array>::HasVirtualTable(instance))
  4171. {
  4172. Uint16Array* uint16Array = UnsafeVarTo<Uint16Array>(instance);
  4173. returnValue = uint16Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4174. }
  4175. break;
  4176. }
  4177. case TypeIds_Int32Array:
  4178. {
  4179. // The type array will deal with all possible values for the index
  4180. if (VirtualTableInfo<Int32VirtualArray>::HasVirtualTable(instance))
  4181. {
  4182. Int32VirtualArray* int32Array = UnsafeVarTo<Int32VirtualArray>(instance);
  4183. returnValue = int32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4184. }
  4185. else if(VirtualTableInfo<Int32Array>::HasVirtualTable(instance))
  4186. {
  4187. Int32Array* int32Array = UnsafeVarTo<Int32Array>(instance);
  4188. returnValue = int32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4189. }
  4190. break;
  4191. }
  4192. case TypeIds_Uint32Array:
  4193. {
  4194. // The type array will deal with all possible values for the index
  4195. if (VirtualTableInfo<Uint32VirtualArray>::HasVirtualTable(instance))
  4196. {
  4197. Uint32VirtualArray* uint32Array = UnsafeVarTo<Uint32VirtualArray>(instance);
  4198. returnValue = uint32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4199. }
  4200. else if (VirtualTableInfo<Uint32Array>::HasVirtualTable(instance))
  4201. {
  4202. Uint32Array* uint32Array = UnsafeVarTo<Uint32Array>(instance);
  4203. returnValue = uint32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4204. }
  4205. break;
  4206. }
  4207. case TypeIds_Float32Array:
  4208. {
  4209. // The type array will deal with all possible values for the index
  4210. if (VirtualTableInfo<Float32VirtualArray>::HasVirtualTable(instance))
  4211. {
  4212. Float32VirtualArray* float32Array = UnsafeVarTo<Float32VirtualArray>(instance);
  4213. returnValue = float32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4214. }
  4215. else if (VirtualTableInfo<Float32Array>::HasVirtualTable(instance))
  4216. {
  4217. Float32Array* float32Array = UnsafeVarTo<Float32Array>(instance);
  4218. returnValue = float32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4219. }
  4220. break;
  4221. }
  4222. case TypeIds_Float64Array:
  4223. {
  4224. // The type array will deal with all possible values for the index
  4225. if (VirtualTableInfo<Float64VirtualArray>::HasVirtualTable(instance))
  4226. {
  4227. Float64VirtualArray* float64Array = UnsafeVarTo<Float64VirtualArray>(instance);
  4228. returnValue = float64Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4229. }
  4230. else if (VirtualTableInfo<Float64Array>::HasVirtualTable(instance))
  4231. {
  4232. Float64Array* float64Array = UnsafeVarTo<Float64Array>(instance);
  4233. returnValue = float64Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4234. }
  4235. break;
  4236. }
  4237. }
  4238. // if this was numeric index, return operation status else
  4239. // Return the result of calling the default ordinary object [[Set]] internal method (9.1.8) on O passing P, V, and Receiver as arguments.
  4240. if (isNumericIndex)
  4241. return returnValue;
  4242. }
  4243. }
  4244. else
  4245. {
  4246. if (TaggedInt::Is(index))
  4247. {
  4248. TaggedIntIndex:
  4249. switch (instanceType)
  4250. {
  4251. case TypeIds_NativeIntArray:
  4252. case TypeIds_NativeFloatArray:
  4253. case TypeIds_Array: // fast path for array
  4254. {
  4255. int indexInt = TaggedInt::ToInt32(index);
  4256. if (indexInt >= 0 && scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4257. {
  4258. UnsafeVarTo<JavascriptArray>(instance)->SetItem((uint32)indexInt, value, flags);
  4259. return TRUE;
  4260. }
  4261. break;
  4262. }
  4263. }
  4264. }
  4265. else if (JavascriptNumber::Is_NoTaggedIntCheck(index))
  4266. {
  4267. double dIndexValue = JavascriptNumber::GetValue(index);
  4268. uint32 uint32Index = JavascriptConversion::ToUInt32(index, scriptContext);
  4269. if ((double)uint32Index == dIndexValue && !TaggedInt::IsOverflow(uint32Index))
  4270. {
  4271. index = TaggedInt::ToVarUnchecked(uint32Index);
  4272. goto TaggedIntIndex;
  4273. }
  4274. }
  4275. }
  4276. RecyclableObject* object = nullptr;
  4277. if (!GetPropertyObjectForSetElementI(instance, index, scriptContext, &object))
  4278. {
  4279. return FALSE;
  4280. }
  4281. return JavascriptOperators::SetElementIHelper(instance, object, index, value, scriptContext, flags);
  4282. }
  4283. BOOL JavascriptOperators::SetElementIHelper(Var receiver, RecyclableObject* object, Var index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  4284. {
  4285. IndexType indexType;
  4286. uint32 indexVal = 0;
  4287. PropertyRecord const * propertyRecord = nullptr;
  4288. JavascriptString * propertyNameString = nullptr;
  4289. PropertyValueInfo propertyValueInfo;
  4290. RecyclableObject* cacheOwner;
  4291. PropertyRecordUsageCache* propertyRecordUsageCache;
  4292. if (JavascriptOperators::GetPropertyRecordUsageCache(index, scriptContext, &propertyRecordUsageCache, &cacheOwner))
  4293. {
  4294. return JavascriptOperators::SetElementIWithCache<false>(receiver, object, cacheOwner, value, propertyRecordUsageCache, scriptContext, flags, nullptr);
  4295. }
  4296. if (TaggedNumber::Is(receiver))
  4297. {
  4298. return JavascriptOperators::SetElementIOnTaggedNumber(receiver, object, index, value, scriptContext, flags);
  4299. }
  4300. #if DBG_DUMP
  4301. scriptContext->forinNoCache += (!TaggedInt::Is(index) && VarIs<JavascriptString>(index));
  4302. #endif
  4303. indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, &propertyNameString, false, true);
  4304. if (scriptContext->GetThreadContext()->IsDisableImplicitCall() &&
  4305. scriptContext->GetThreadContext()->GetImplicitCallFlags() != ImplicitCall_None)
  4306. {
  4307. // We hit an implicit call trying to convert the index, and implicit calls are disabled, so
  4308. // quit before we try to store the element.
  4309. return FALSE;
  4310. }
  4311. if (indexType == IndexType_Number)
  4312. {
  4313. SetElementIHelper_INDEX_TYPE_IS_NUMBER:
  4314. return JavascriptOperators::SetItem(receiver, object, indexVal, value, scriptContext, flags);
  4315. }
  4316. else if (indexType == IndexType_JavascriptString)
  4317. {
  4318. Assert(propertyNameString);
  4319. // At this point, we know that the propertyNameString is neither PropertyString
  4320. // or LiteralStringWithPropertyStringPtr.. Get PropertyRecord!
  4321. // we will get it anyways otherwise. (Also, 1:1 string comparison for Builtin types will be expensive.)
  4322. if (propertyRecord == nullptr)
  4323. {
  4324. scriptContext->GetOrAddPropertyRecord(propertyNameString, &propertyRecord);
  4325. if (propertyRecord->IsNumeric())
  4326. {
  4327. indexVal = propertyRecord->GetNumericValue();
  4328. goto SetElementIHelper_INDEX_TYPE_IS_NUMBER;
  4329. }
  4330. }
  4331. }
  4332. Assert(indexType == IndexType_PropertyId || indexType == IndexType_JavascriptString);
  4333. Assert(propertyRecord);
  4334. return JavascriptOperators::SetProperty(receiver, object, propertyRecord->GetPropertyId(), value, scriptContext, flags);
  4335. }
  4336. BOOL JavascriptOperators::OP_SetNativeIntElementI_NoConvert(
  4337. Var instance,
  4338. Var aElementIndex,
  4339. int32 iValue,
  4340. ScriptContext* scriptContext,
  4341. PropertyOperationFlags flags)
  4342. {
  4343. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_NoConvert);
  4344. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_NoConvert, Op_SetNativeIntElementI);
  4345. BOOL converted = OP_SetNativeIntElementI(instance, aElementIndex, iValue, scriptContext, flags);
  4346. if (converted)
  4347. {
  4348. AssertMsg(false, "Unexpected native array conversion");
  4349. Js::Throw::FatalInternalError();
  4350. }
  4351. return FALSE;
  4352. JIT_HELPER_END(Op_SetNativeIntElementI_NoConvert);
  4353. }
  4354. BOOL JavascriptOperators::OP_SetNativeIntElementI_UInt32_NoConvert(
  4355. Var instance,
  4356. uint32 aElementIndex,
  4357. int32 iValue,
  4358. ScriptContext* scriptContext,
  4359. PropertyOperationFlags flags)
  4360. {
  4361. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_UInt32_NoConvert);
  4362. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_UInt32_NoConvert, Op_SetNativeIntElementI_UInt32);
  4363. BOOL converted = OP_SetNativeIntElementI_UInt32(instance, aElementIndex, iValue, scriptContext, flags);
  4364. if (converted)
  4365. {
  4366. AssertMsg(false, "Unexpected native array conversion");
  4367. Js::Throw::FatalInternalError();
  4368. }
  4369. return FALSE;
  4370. JIT_HELPER_END(Op_SetNativeIntElementI_UInt32_NoConvert);
  4371. }
  4372. BOOL JavascriptOperators::OP_SetNativeIntElementI_Int32_NoConvert(
  4373. Var instance,
  4374. int32 aElementIndex,
  4375. int32 iValue,
  4376. ScriptContext* scriptContext,
  4377. PropertyOperationFlags flags)
  4378. {
  4379. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_Int32_NoConvert);
  4380. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_Int32_NoConvert, Op_SetNativeIntElementI_Int32);
  4381. BOOL converted = OP_SetNativeIntElementI_Int32(instance, aElementIndex, iValue, scriptContext, flags);
  4382. if (converted)
  4383. {
  4384. AssertMsg(false, "Unexpected native array conversion");
  4385. Js::Throw::FatalInternalError();
  4386. }
  4387. return FALSE;
  4388. JIT_HELPER_END(Op_SetNativeIntElementI_Int32_NoConvert);
  4389. }
  4390. BOOL JavascriptOperators::OP_SetNativeFloatElementI_NoConvert(
  4391. Var instance,
  4392. Var aElementIndex,
  4393. ScriptContext* scriptContext,
  4394. PropertyOperationFlags flags,
  4395. double dValue)
  4396. {
  4397. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_NoConvert);
  4398. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_NoConvert, Op_SetNativeFloatElementI);
  4399. BOOL converted = OP_SetNativeFloatElementI(instance, aElementIndex, scriptContext, flags, dValue);
  4400. if (converted)
  4401. {
  4402. AssertMsg(false, "Unexpected native array conversion");
  4403. Js::Throw::FatalInternalError();
  4404. }
  4405. return FALSE;
  4406. JIT_HELPER_END(Op_SetNativeFloatElementI_NoConvert);
  4407. }
  4408. BOOL JavascriptOperators::OP_SetNativeFloatElementI_UInt32_NoConvert(
  4409. Var instance,
  4410. uint32 aElementIndex,
  4411. ScriptContext* scriptContext,
  4412. PropertyOperationFlags flags,
  4413. double dValue)
  4414. {
  4415. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_UInt32_NoConvert);
  4416. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_NoConvert, Op_SetNativeFloatElementI_UInt32);
  4417. BOOL converted = OP_SetNativeFloatElementI_UInt32(instance, aElementIndex, scriptContext, flags, dValue);
  4418. if (converted)
  4419. {
  4420. AssertMsg(false, "Unexpected native array conversion");
  4421. Js::Throw::FatalInternalError();
  4422. }
  4423. return FALSE;
  4424. JIT_HELPER_END(Op_SetNativeFloatElementI_UInt32_NoConvert);
  4425. }
  4426. BOOL JavascriptOperators::OP_SetNativeFloatElementI_Int32_NoConvert(
  4427. Var instance,
  4428. int32 aElementIndex,
  4429. ScriptContext* scriptContext,
  4430. PropertyOperationFlags flags,
  4431. double dValue)
  4432. {
  4433. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_Int32_NoConvert);
  4434. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_NoConvert, Op_SetNativeFloatElementI_Int32);
  4435. BOOL converted = OP_SetNativeFloatElementI_Int32(instance, aElementIndex, scriptContext, flags, dValue);
  4436. if (converted)
  4437. {
  4438. AssertMsg(false, "Unexpected native array conversion");
  4439. Js::Throw::FatalInternalError();
  4440. }
  4441. return FALSE;
  4442. JIT_HELPER_END(Op_SetNativeFloatElementI_Int32_NoConvert);
  4443. }
  4444. BOOL JavascriptOperators::OP_SetNativeIntElementI(
  4445. Var instance,
  4446. Var aElementIndex,
  4447. int32 iValue,
  4448. ScriptContext* scriptContext,
  4449. PropertyOperationFlags flags)
  4450. {
  4451. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI);
  4452. INT_PTR vt = (INT_PTR)nullptr;
  4453. vt = VirtualTableInfoBase::GetVirtualTable(instance);
  4454. if (TaggedInt::Is(aElementIndex))
  4455. {
  4456. int32 indexInt = TaggedInt::ToInt32(aElementIndex);
  4457. if (indexInt >= 0 && scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4458. {
  4459. JavascriptNativeIntArray *arr = VarTo<JavascriptNativeIntArray>(instance);
  4460. if (!(arr->TryGrowHeadSegmentAndSetItem<int32, JavascriptNativeIntArray>((uint32)indexInt, iValue)))
  4461. {
  4462. arr->SetItem(indexInt, iValue);
  4463. }
  4464. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4465. }
  4466. }
  4467. JavascriptOperators::OP_SetElementI(instance, aElementIndex, JavascriptNumber::ToVar(iValue, scriptContext), scriptContext, flags);
  4468. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4469. JIT_HELPER_END(Op_SetNativeIntElementI);
  4470. }
  4471. BOOL JavascriptOperators::OP_SetNativeIntElementI_UInt32(
  4472. Var instance,
  4473. uint32 aElementIndex,
  4474. int32 iValue,
  4475. ScriptContext* scriptContext,
  4476. PropertyOperationFlags flags)
  4477. {
  4478. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_UInt32);
  4479. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_UInt32, Op_SetNativeIntElementI);
  4480. #if FLOATVAR
  4481. return OP_SetNativeIntElementI(instance, Js::JavascriptNumber::ToVar(aElementIndex, scriptContext), iValue, scriptContext, flags);
  4482. #else
  4483. char buffer[sizeof(Js::JavascriptNumber)];
  4484. return OP_SetNativeIntElementI(instance, Js::JavascriptNumber::ToVarInPlace(aElementIndex, scriptContext,
  4485. (Js::JavascriptNumber *)buffer), iValue, scriptContext, flags);
  4486. #endif
  4487. JIT_HELPER_END(Op_SetNativeIntElementI_UInt32);
  4488. }
  4489. BOOL JavascriptOperators::OP_SetNativeIntElementI_Int32(
  4490. Var instance,
  4491. int aElementIndex,
  4492. int32 iValue,
  4493. ScriptContext* scriptContext,
  4494. PropertyOperationFlags flags)
  4495. {
  4496. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_Int32);
  4497. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_Int32, Op_SetNativeIntElementI);
  4498. #if FLOATVAR
  4499. return OP_SetNativeIntElementI(instance, Js::JavascriptNumber::ToVar(aElementIndex, scriptContext), iValue, scriptContext, flags);
  4500. #else
  4501. char buffer[sizeof(Js::JavascriptNumber)];
  4502. return OP_SetNativeIntElementI(instance, Js::JavascriptNumber::ToVarInPlace(aElementIndex, scriptContext,
  4503. (Js::JavascriptNumber *)buffer), iValue, scriptContext, flags);
  4504. #endif
  4505. JIT_HELPER_END(Op_SetNativeIntElementI_Int32);
  4506. }
  4507. BOOL JavascriptOperators::OP_SetNativeFloatElementI(
  4508. Var instance,
  4509. Var aElementIndex,
  4510. ScriptContext* scriptContext,
  4511. PropertyOperationFlags flags,
  4512. double dValue)
  4513. {
  4514. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI);
  4515. INT_PTR vt = (INT_PTR)nullptr;
  4516. vt = VirtualTableInfoBase::GetVirtualTable(instance);
  4517. if (TaggedInt::Is(aElementIndex))
  4518. {
  4519. int32 indexInt = TaggedInt::ToInt32(aElementIndex);
  4520. if (indexInt >= 0 && scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4521. {
  4522. JavascriptNativeFloatArray *arr = VarTo<JavascriptNativeFloatArray>(instance);
  4523. if (!(arr->TryGrowHeadSegmentAndSetItem<double, JavascriptNativeFloatArray>((uint32)indexInt, dValue)))
  4524. {
  4525. arr->SetItem(indexInt, dValue);
  4526. }
  4527. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4528. }
  4529. }
  4530. JavascriptOperators::OP_SetElementI(instance, aElementIndex, JavascriptNumber::ToVarWithCheck(dValue, scriptContext), scriptContext, flags);
  4531. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4532. JIT_HELPER_END(Op_SetNativeFloatElementI);
  4533. }
  4534. BOOL JavascriptOperators::OP_SetNativeFloatElementI_UInt32(
  4535. Var instance,
  4536. uint32 aElementIndex,
  4537. ScriptContext* scriptContext,
  4538. PropertyOperationFlags flags,
  4539. double dValue)
  4540. {
  4541. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_UInt32);
  4542. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_UInt32, Op_SetNativeFloatElementI);
  4543. #if FLOATVAR
  4544. return OP_SetNativeFloatElementI(instance, JavascriptNumber::ToVar(aElementIndex, scriptContext), scriptContext, flags, dValue);
  4545. #else
  4546. char buffer[sizeof(Js::JavascriptNumber)];
  4547. return OP_SetNativeFloatElementI(instance, JavascriptNumber::ToVarInPlace(aElementIndex, scriptContext,
  4548. (Js::JavascriptNumber *)buffer), scriptContext, flags, dValue);
  4549. #endif
  4550. JIT_HELPER_END(Op_SetNativeFloatElementI_UInt32);
  4551. }
  4552. BOOL JavascriptOperators::OP_SetNativeFloatElementI_Int32(
  4553. Var instance,
  4554. int aElementIndex,
  4555. ScriptContext* scriptContext,
  4556. PropertyOperationFlags flags,
  4557. double dValue)
  4558. {
  4559. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_Int32);
  4560. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_Int32, Op_SetNativeFloatElementI);
  4561. #if FLOATVAR
  4562. return OP_SetNativeFloatElementI(instance, JavascriptNumber::ToVar(aElementIndex, scriptContext), scriptContext, flags, dValue);
  4563. #else
  4564. char buffer[sizeof(Js::JavascriptNumber)];
  4565. return OP_SetNativeFloatElementI(instance, JavascriptNumber::ToVarInPlace(aElementIndex, scriptContext,
  4566. (Js::JavascriptNumber *)buffer), scriptContext, flags, dValue);
  4567. #endif
  4568. JIT_HELPER_END(Op_SetNativeFloatElementI_Int32);
  4569. }
  4570. BOOL JavascriptOperators::OP_Memcopy(Var dstInstance, int32 dstStart, Var srcInstance, int32 srcStart, int32 length, ScriptContext* scriptContext)
  4571. {
  4572. JIT_HELPER_NOT_REENTRANT_HEADER(Op_Memcopy, reentrancylock, scriptContext->GetThreadContext());
  4573. if (length <= 0)
  4574. {
  4575. return false;
  4576. }
  4577. TypeId instanceType = JavascriptOperators::GetTypeId(srcInstance);
  4578. if (instanceType != JavascriptOperators::GetTypeId(dstInstance))
  4579. {
  4580. return false;
  4581. }
  4582. if (srcStart != dstStart)
  4583. {
  4584. return false;
  4585. }
  4586. BOOL returnValue = false;
  4587. #define MEMCOPY_TYPED_ARRAY(type, conversion) VarTo< type ## >(dstInstance)->DirectSetItemAtRange( VarTo< type ## >(srcInstance), srcStart, dstStart, length, JavascriptConversion:: ## conversion)
  4588. switch (instanceType)
  4589. {
  4590. case TypeIds_Int8Array:
  4591. {
  4592. returnValue = MEMCOPY_TYPED_ARRAY(Int8Array, ToInt8);
  4593. break;
  4594. }
  4595. case TypeIds_Uint8Array:
  4596. {
  4597. returnValue = MEMCOPY_TYPED_ARRAY(Uint8Array, ToUInt8);
  4598. break;
  4599. }
  4600. case TypeIds_Uint8ClampedArray:
  4601. {
  4602. returnValue = MEMCOPY_TYPED_ARRAY(Uint8ClampedArray, ToUInt8Clamped);
  4603. break;
  4604. }
  4605. case TypeIds_Int16Array:
  4606. {
  4607. returnValue = MEMCOPY_TYPED_ARRAY(Int16Array, ToInt16);
  4608. break;
  4609. }
  4610. case TypeIds_Uint16Array:
  4611. {
  4612. returnValue = MEMCOPY_TYPED_ARRAY(Uint16Array, ToUInt16);
  4613. break;
  4614. }
  4615. case TypeIds_Int32Array:
  4616. {
  4617. returnValue = MEMCOPY_TYPED_ARRAY(Int32Array, ToInt32);
  4618. break;
  4619. }
  4620. case TypeIds_Uint32Array:
  4621. {
  4622. returnValue = MEMCOPY_TYPED_ARRAY(Uint32Array, ToUInt32);
  4623. break;
  4624. }
  4625. case TypeIds_Float32Array:
  4626. {
  4627. returnValue = MEMCOPY_TYPED_ARRAY(Float32Array, ToFloat);
  4628. break;
  4629. }
  4630. case TypeIds_Float64Array:
  4631. {
  4632. returnValue = MEMCOPY_TYPED_ARRAY(Float64Array, ToNumber);
  4633. break;
  4634. }
  4635. case TypeIds_Array:
  4636. case TypeIds_NativeFloatArray:
  4637. case TypeIds_NativeIntArray:
  4638. {
  4639. if (dstStart < 0 || srcStart < 0)
  4640. {
  4641. // This is not supported, Bailout
  4642. break;
  4643. }
  4644. // Upper bounds check for source array
  4645. JavascriptArray* srcArray = UnsafeVarTo<JavascriptArray>(srcInstance);
  4646. JavascriptArray* dstArray = VarTo<JavascriptArray>(dstInstance);
  4647. if (scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4648. {
  4649. INT_PTR vt = VirtualTableInfoBase::GetVirtualTable(dstInstance);
  4650. if (instanceType == TypeIds_Array)
  4651. {
  4652. returnValue = dstArray->DirectSetItemAtRangeFromArray<Var>(dstStart, length, srcArray, srcStart);
  4653. }
  4654. else if (instanceType == TypeIds_NativeIntArray)
  4655. {
  4656. returnValue = dstArray->DirectSetItemAtRangeFromArray<int32>(dstStart, length, srcArray, srcStart);
  4657. }
  4658. else
  4659. {
  4660. returnValue = dstArray->DirectSetItemAtRangeFromArray<double>(dstStart, length, srcArray, srcStart);
  4661. }
  4662. returnValue &= vt == VirtualTableInfoBase::GetVirtualTable(dstInstance);
  4663. }
  4664. break;
  4665. }
  4666. default:
  4667. AssertMsg(false, "We don't support this type for memcopy yet.");
  4668. break;
  4669. }
  4670. #undef MEMCOPY_TYPED_ARRAY
  4671. return returnValue;
  4672. JIT_HELPER_END(Op_Memcopy);
  4673. }
  4674. template<typename T, T(*func)(Var, ScriptContext*)> bool MemsetConversion(Var value, ScriptContext* scriptContext, T* result)
  4675. {
  4676. ImplicitCallFlags flags = scriptContext->GetThreadContext()->TryWithDisabledImplicitCall([&]
  4677. {
  4678. *result = func(value, scriptContext);
  4679. });
  4680. return (flags & (~ImplicitCall_None)) == 0;
  4681. }
  4682. BOOL JavascriptOperators::OP_Memset(Var instance, int32 start, Var value, int32 length, ScriptContext* scriptContext)
  4683. {
  4684. JIT_HELPER_NOT_REENTRANT_HEADER(Op_Memset, reentrancylock, scriptContext->GetThreadContext());
  4685. if (length <= 0)
  4686. {
  4687. return false;
  4688. }
  4689. TypeId instanceType = JavascriptOperators::GetTypeId(instance);
  4690. BOOL returnValue = false;
  4691. // The typed array will deal with all possible values for the index
  4692. #define MEMSET_TYPED_ARRAY_CASE(type, conversion) \
  4693. case TypeIds_##type: \
  4694. { \
  4695. type## ::TypedArrayType typedValue = 0; \
  4696. if (!MemsetConversion<type## ::TypedArrayType, JavascriptConversion:: ##conversion>(value, scriptContext, &typedValue)) return false; \
  4697. returnValue = VarTo< type## >(instance)->DirectSetItemAtRange(start, length, typedValue); \
  4698. break; \
  4699. }
  4700. switch (instanceType)
  4701. {
  4702. MEMSET_TYPED_ARRAY_CASE(Int8Array, ToInt8)
  4703. MEMSET_TYPED_ARRAY_CASE(Uint8Array, ToUInt8)
  4704. MEMSET_TYPED_ARRAY_CASE(Uint8ClampedArray, ToUInt8Clamped)
  4705. MEMSET_TYPED_ARRAY_CASE(Int16Array, ToInt16)
  4706. MEMSET_TYPED_ARRAY_CASE(Uint16Array, ToUInt16)
  4707. MEMSET_TYPED_ARRAY_CASE(Int32Array, ToInt32)
  4708. MEMSET_TYPED_ARRAY_CASE(Uint32Array, ToUInt32)
  4709. MEMSET_TYPED_ARRAY_CASE(Float32Array, ToFloat)
  4710. MEMSET_TYPED_ARRAY_CASE(Float64Array, ToNumber)
  4711. case TypeIds_NativeFloatArray:
  4712. case TypeIds_NativeIntArray:
  4713. case TypeIds_Array:
  4714. {
  4715. if (start < 0)
  4716. {
  4717. for (start; start < 0 && length > 0; ++start, --length)
  4718. {
  4719. if (!OP_SetElementI(instance, JavascriptNumber::ToVar(start, scriptContext), value, scriptContext))
  4720. {
  4721. return false;
  4722. }
  4723. }
  4724. }
  4725. if (scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4726. {
  4727. INT_PTR vt = VirtualTableInfoBase::GetVirtualTable(instance);
  4728. if (instanceType == TypeIds_Array)
  4729. {
  4730. returnValue = UnsafeVarTo<JavascriptArray>(instance)->DirectSetItemAtRange<Var>(start, length, value);
  4731. }
  4732. else if (instanceType == TypeIds_NativeIntArray)
  4733. {
  4734. // Only accept tagged int.
  4735. if (!TaggedInt::Is(value))
  4736. {
  4737. return false;
  4738. }
  4739. int32 intValue = 0;
  4740. if (!MemsetConversion<int32, JavascriptConversion::ToInt32>(value, scriptContext, &intValue))
  4741. {
  4742. return false;
  4743. }
  4744. // Special case for missing item
  4745. if (SparseArraySegment<int32>::IsMissingItem(&intValue))
  4746. {
  4747. return false;
  4748. }
  4749. returnValue = UnsafeVarTo<JavascriptArray>(instance)->DirectSetItemAtRange<int32>(start, length, intValue);
  4750. }
  4751. else
  4752. {
  4753. // For native float arrays, the jit doesn't check the type of the source so we have to do it here
  4754. if (!JavascriptNumber::Is(value) && !TaggedNumber::Is(value))
  4755. {
  4756. return false;
  4757. }
  4758. double doubleValue = 0;
  4759. if (!MemsetConversion<double, JavascriptConversion::ToNumber>(value, scriptContext, &doubleValue))
  4760. {
  4761. return false;
  4762. }
  4763. // Special case for missing item
  4764. if (SparseArraySegment<double>::IsMissingItem(&doubleValue))
  4765. {
  4766. return false;
  4767. }
  4768. returnValue = UnsafeVarTo<JavascriptArray>(instance)->DirectSetItemAtRange<double>(start, length, doubleValue);
  4769. }
  4770. returnValue &= vt == VirtualTableInfoBase::GetVirtualTable(instance);
  4771. }
  4772. break;
  4773. }
  4774. default:
  4775. AssertMsg(false, "We don't support this type for memset yet.");
  4776. break;
  4777. }
  4778. #undef MEMSET_TYPED_ARRAY
  4779. return returnValue;
  4780. JIT_HELPER_END(Op_Memset);
  4781. }
  4782. Var JavascriptOperators::OP_DeleteElementI_UInt32(Var instance, uint32 index, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  4783. {
  4784. JIT_HELPER_REENTRANT_HEADER(Op_DeleteElementI_UInt32);
  4785. JIT_HELPER_SAME_ATTRIBUTES(Op_DeleteElementI_UInt32, Op_DeleteElementI);
  4786. #if FLOATVAR
  4787. return OP_DeleteElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext, propertyOperationFlags);
  4788. #else
  4789. char buffer[sizeof(Js::JavascriptNumber)];
  4790. return OP_DeleteElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  4791. (Js::JavascriptNumber *)buffer), scriptContext, propertyOperationFlags);
  4792. #endif
  4793. JIT_HELPER_END(Op_DeleteElementI_UInt32);
  4794. }
  4795. Var JavascriptOperators::OP_DeleteElementI_Int32(Var instance, int32 index, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  4796. {
  4797. JIT_HELPER_REENTRANT_HEADER(Op_DeleteElementI_Int32);
  4798. JIT_HELPER_SAME_ATTRIBUTES(Op_DeleteElementI_Int32, Op_DeleteElementI);
  4799. #if FLOATVAR
  4800. return OP_DeleteElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext, propertyOperationFlags);
  4801. #else
  4802. char buffer[sizeof(Js::JavascriptNumber)];
  4803. return OP_DeleteElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  4804. (Js::JavascriptNumber *)buffer), scriptContext, propertyOperationFlags);
  4805. #endif
  4806. JIT_HELPER_END(Op_DeleteElementI_Int32);
  4807. }
  4808. Var JavascriptOperators::OP_DeleteElementI(Var instance, Var index, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  4809. {
  4810. JIT_HELPER_REENTRANT_HEADER(Op_DeleteElementI);
  4811. if(TaggedNumber::Is(instance))
  4812. {
  4813. return scriptContext->GetLibrary()->GetTrue();
  4814. }
  4815. #if ENABLE_COPYONACCESS_ARRAY
  4816. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  4817. #endif
  4818. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  4819. if (JavascriptOperators::IsUndefinedOrNull(object))
  4820. {
  4821. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotDelete_NullOrUndefined, GetPropertyDisplayNameForError(index, scriptContext));
  4822. }
  4823. uint32 indexVal;
  4824. PropertyRecord const * propertyRecord = nullptr;
  4825. JavascriptString * propertyNameString = nullptr;
  4826. BOOL result = TRUE;
  4827. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, &propertyNameString, false, true);
  4828. if (indexType == IndexType_Number)
  4829. {
  4830. result = JavascriptOperators::DeleteItem(object, indexVal, propertyOperationFlags);
  4831. }
  4832. else if (indexType == IndexType_JavascriptString)
  4833. {
  4834. result = JavascriptOperators::DeleteProperty(object, propertyNameString, propertyOperationFlags);
  4835. }
  4836. else
  4837. {
  4838. Assert(indexType == IndexType_PropertyId);
  4839. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  4840. {
  4841. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
  4842. Assert(indexType == IndexType_PropertyId);
  4843. Assert(propertyRecord != nullptr);
  4844. }
  4845. if (propertyRecord != nullptr)
  4846. {
  4847. result = JavascriptOperators::DeleteProperty(object, propertyRecord->GetPropertyId(), propertyOperationFlags);
  4848. }
  4849. #if DBG
  4850. else
  4851. {
  4852. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  4853. PropertyRecord const * debugPropertyRecord;
  4854. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  4855. AssertMsg(JavascriptOperators::DeleteProperty(object, debugPropertyRecord->GetPropertyId(), propertyOperationFlags), "delete should have been true. See OS Bug 2727708 if you see this come from the web");
  4856. }
  4857. #endif
  4858. }
  4859. Assert(result || !(propertyOperationFlags & (PropertyOperation_StrictMode | PropertyOperation_ThrowOnDeleteIfNotConfig)));
  4860. return scriptContext->GetLibrary()->CreateBoolean(result);
  4861. JIT_HELPER_END(Op_DeleteElementI);
  4862. }
  4863. Var JavascriptOperators::OP_ToPropertyKey(Js::Var argument, ScriptContext* scriptContext)
  4864. {
  4865. JIT_HELPER_REENTRANT_HEADER(Op_ConvPropertyKey);
  4866. PropertyRecord const* unused = nullptr;
  4867. return JavascriptConversion::ToPropertyKey(argument, scriptContext, &unused, nullptr);
  4868. JIT_HELPER_END(Op_ConvPropertyKey);
  4869. }
  4870. Var JavascriptOperators::OP_GetLength(Var instance, ScriptContext* scriptContext)
  4871. {
  4872. return JavascriptOperators::OP_GetProperty(instance, PropertyIds::length, scriptContext);
  4873. }
  4874. Var JavascriptOperators::GetThisFromModuleRoot(Var thisVar)
  4875. {
  4876. RootObjectBase * rootObject = static_cast<RootObjectBase*>(thisVar);
  4877. RecyclableObject* hostObject = rootObject->GetHostObject();
  4878. //
  4879. // if the module root has the host object, use that as "this"
  4880. //
  4881. if (hostObject)
  4882. {
  4883. thisVar = hostObject->GetHostDispatchVar();
  4884. }
  4885. return thisVar;
  4886. }
  4887. inline void JavascriptOperators::TryLoadRoot(Var& thisVar, TypeId typeId, int moduleID, ScriptContextInfo* scriptContext)
  4888. {
  4889. bool loadRoot = false;
  4890. if (JavascriptOperators::IsUndefinedOrNullType(typeId) || typeId == TypeIds_ActivationObject)
  4891. {
  4892. loadRoot = true;
  4893. }
  4894. else if (typeId == TypeIds_HostDispatch)
  4895. {
  4896. TypeId remoteTypeId = TypeIds_Limit;
  4897. if (VarTo<RecyclableObject>(thisVar)->GetRemoteTypeId(&remoteTypeId))
  4898. {
  4899. if (remoteTypeId == TypeIds_Null || remoteTypeId == TypeIds_Undefined || remoteTypeId == TypeIds_ActivationObject)
  4900. {
  4901. loadRoot = true;
  4902. }
  4903. }
  4904. }
  4905. if (loadRoot)
  4906. {
  4907. if (moduleID == 0)
  4908. {
  4909. thisVar = (Js::Var)scriptContext->GetGlobalObjectThisAddr();
  4910. }
  4911. else
  4912. {
  4913. // TODO: OOP JIT, create a copy of module roots in server side
  4914. Js::ModuleRoot * moduleRoot = JavascriptOperators::GetModuleRoot(moduleID, (ScriptContext*)scriptContext);
  4915. if (moduleRoot == nullptr)
  4916. {
  4917. Assert(false);
  4918. thisVar = (Js::Var)scriptContext->GetUndefinedAddr();
  4919. }
  4920. else
  4921. {
  4922. thisVar = GetThisFromModuleRoot(moduleRoot);
  4923. }
  4924. }
  4925. }
  4926. }
  4927. Var JavascriptOperators::OP_GetThis(Var thisVar, int moduleID, ScriptContextInfo* scriptContext)
  4928. {
  4929. JIT_HELPER_REENTRANT_HEADER(LdThis);
  4930. //
  4931. // if "this" is null or undefined
  4932. // Pass the global object
  4933. // Else
  4934. // Pass ToObject(this)
  4935. //
  4936. TypeId typeId = JavascriptOperators::GetTypeId(thisVar);
  4937. Assert(!JavascriptOperators::IsThisSelf(typeId));
  4938. return JavascriptOperators::GetThisHelper(thisVar, typeId, moduleID, scriptContext);
  4939. JIT_HELPER_END(LdThis);
  4940. }
  4941. Var JavascriptOperators::OP_GetThisNoFastPath(Var thisVar, int moduleID, ScriptContext* scriptContext)
  4942. {
  4943. JIT_HELPER_REENTRANT_HEADER(LdThisNoFastPath);
  4944. TypeId typeId = JavascriptOperators::GetTypeId(thisVar);
  4945. if (JavascriptOperators::IsThisSelf(typeId))
  4946. {
  4947. Assert(typeId != TypeIds_GlobalObject || ((Js::GlobalObject*)thisVar)->ToThis() == thisVar);
  4948. Assert(typeId != TypeIds_ModuleRoot || JavascriptOperators::GetThisFromModuleRoot(thisVar) == thisVar);
  4949. return thisVar;
  4950. }
  4951. return JavascriptOperators::GetThisHelper(thisVar, typeId, moduleID, scriptContext);
  4952. JIT_HELPER_END(LdThisNoFastPath);
  4953. }
  4954. bool JavascriptOperators::IsThisSelf(TypeId typeId)
  4955. {
  4956. return (JavascriptOperators::IsObjectType(typeId) && ! JavascriptOperators::IsSpecialObjectType(typeId));
  4957. }
  4958. Var JavascriptOperators::GetThisHelper(Var thisVar, TypeId typeId, int moduleID, ScriptContextInfo *scriptContext)
  4959. {
  4960. if (! JavascriptOperators::IsObjectType(typeId) && ! JavascriptOperators::IsUndefinedOrNullType(typeId))
  4961. {
  4962. #if ENABLE_NATIVE_CODEGEN
  4963. Assert(!JITManager::GetJITManager()->IsJITServer());
  4964. #endif
  4965. #if !FLOATVAR
  4966. // We allowed stack number to be used as the "this" for getter and setter activation of
  4967. // n.x and n[prop], where n is the Javascript Number
  4968. return JavascriptOperators::ToObject(
  4969. JavascriptNumber::BoxStackNumber(thisVar, (ScriptContext*)scriptContext), (ScriptContext*)scriptContext);
  4970. #else
  4971. return JavascriptOperators::ToObject(thisVar, (ScriptContext*)scriptContext);
  4972. #endif
  4973. }
  4974. else
  4975. {
  4976. TryLoadRoot(thisVar, typeId, moduleID, scriptContext);
  4977. return thisVar;
  4978. }
  4979. }
  4980. BOOL JavascriptOperators::GetRemoteTypeId(Var aValue, __out TypeId* typeId)
  4981. {
  4982. *typeId = TypeIds_Limit;
  4983. if (GetTypeId(aValue) != TypeIds_HostDispatch)
  4984. {
  4985. return FALSE;
  4986. }
  4987. return VarTo<RecyclableObject>(aValue)->GetRemoteTypeId(typeId);
  4988. }
  4989. BOOL JavascriptOperators::IsJsNativeType(TypeId type)
  4990. {
  4991. switch(type)
  4992. {
  4993. case TypeIds_Object:
  4994. case TypeIds_Function:
  4995. case TypeIds_Array:
  4996. case TypeIds_NativeIntArray:
  4997. #if ENABLE_COPYONACCESS_ARRAY
  4998. case TypeIds_CopyOnAccessNativeIntArray:
  4999. #endif
  5000. case TypeIds_NativeFloatArray:
  5001. case TypeIds_ES5Array:
  5002. case TypeIds_Date:
  5003. case TypeIds_WinRTDate:
  5004. case TypeIds_RegEx:
  5005. case TypeIds_Error:
  5006. case TypeIds_BooleanObject:
  5007. case TypeIds_NumberObject:
  5008. case TypeIds_StringObject:
  5009. case TypeIds_Symbol:
  5010. case TypeIds_SymbolObject:
  5011. //case TypeIds_GlobalObject:
  5012. //case TypeIds_ModuleRoot:
  5013. //case TypeIds_HostObject:
  5014. case TypeIds_Arguments:
  5015. case TypeIds_ActivationObject:
  5016. case TypeIds_Map:
  5017. case TypeIds_Set:
  5018. case TypeIds_WeakMap:
  5019. case TypeIds_WeakSet:
  5020. case TypeIds_ArrayIterator:
  5021. case TypeIds_MapIterator:
  5022. case TypeIds_SetIterator:
  5023. case TypeIds_StringIterator:
  5024. case TypeIds_Generator:
  5025. case TypeIds_AsyncFromSyncIterator:
  5026. case TypeIds_Promise:
  5027. case TypeIds_Proxy:
  5028. return true;
  5029. default:
  5030. return false;
  5031. }
  5032. }
  5033. BOOL JavascriptOperators::IsJsNativeObject(Var instance)
  5034. {
  5035. return IsJsNativeType(GetTypeId(instance));
  5036. }
  5037. BOOL JavascriptOperators::IsJsNativeObject(_In_ RecyclableObject* instance)
  5038. {
  5039. return IsJsNativeType(GetTypeId(instance));
  5040. }
  5041. bool JavascriptOperators::CanShortcutOnUnknownPropertyName(RecyclableObject *instance)
  5042. {
  5043. if (!CanShortcutInstanceOnUnknownPropertyName(instance))
  5044. {
  5045. return false;
  5046. }
  5047. return CanShortcutPrototypeChainOnUnknownPropertyName(instance->GetPrototype());
  5048. }
  5049. bool JavascriptOperators::CanShortcutInstanceOnUnknownPropertyName(RecyclableObject *instance)
  5050. {
  5051. if (PHASE_OFF1(Js::OptUnknownElementNamePhase))
  5052. {
  5053. return false;
  5054. }
  5055. TypeId typeId = instance->GetTypeId();
  5056. if (typeId == TypeIds_Proxy || typeId == TypeIds_HostDispatch)
  5057. {
  5058. return false;
  5059. }
  5060. if (DynamicType::Is(typeId) &&
  5061. static_cast<DynamicObject*>(instance)->GetTypeHandler()->IsStringTypeHandler())
  5062. {
  5063. return false;
  5064. }
  5065. if (instance->IsExternal())
  5066. {
  5067. return false;
  5068. }
  5069. if (!(instance->HasDeferredTypeHandler()))
  5070. {
  5071. JavascriptFunction * function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  5072. return function && function->IsExternalFunction();
  5073. }
  5074. return false;
  5075. }
  5076. bool JavascriptOperators::CanShortcutPrototypeChainOnUnknownPropertyName(RecyclableObject *prototype)
  5077. {
  5078. Assert(prototype);
  5079. for (; !JavascriptOperators::IsNull(prototype); prototype = prototype->GetPrototype())
  5080. {
  5081. if (!CanShortcutInstanceOnUnknownPropertyName(prototype))
  5082. {
  5083. return false;
  5084. }
  5085. }
  5086. return true;
  5087. }
  5088. RecyclableObject* JavascriptOperators::GetPrototype(RecyclableObject* instance)
  5089. {
  5090. if (JavascriptOperators::GetTypeId(instance) == TypeIds_Null)
  5091. {
  5092. return instance;
  5093. }
  5094. return instance->GetPrototype();
  5095. }
  5096. RecyclableObject* JavascriptOperators::OP_GetPrototype(Var instance, ScriptContext* scriptContext)
  5097. {
  5098. if (TaggedNumber::Is(instance))
  5099. {
  5100. return scriptContext->GetLibrary()->GetNumberPrototype();
  5101. }
  5102. else
  5103. {
  5104. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  5105. if (JavascriptOperators::IsNull(object))
  5106. {
  5107. return object;
  5108. }
  5109. return JavascriptOperators::GetPrototype(object);
  5110. }
  5111. }
  5112. BOOL JavascriptOperators::OP_BrFncEqApply(Var instance, ScriptContext *scriptContext)
  5113. {
  5114. JIT_HELPER_NOT_REENTRANT_HEADER(Op_OP_BrFncEqApply, reentrancylock, scriptContext->GetThreadContext());
  5115. // JavascriptFunction && !HostDispatch
  5116. if (JavascriptOperators::GetTypeId(instance) == TypeIds_Function)
  5117. {
  5118. FunctionProxy *bod= ((JavascriptFunction*)instance)->GetFunctionProxy();
  5119. if (bod != nullptr)
  5120. {
  5121. return bod->GetDirectEntryPoint(bod->GetDefaultEntryPointInfo()) == &Js::JavascriptFunction::EntryApply;
  5122. }
  5123. else
  5124. {
  5125. FunctionInfo* info = ((JavascriptFunction *)instance)->GetFunctionInfo();
  5126. if (info != nullptr)
  5127. {
  5128. return &Js::JavascriptFunction::EntryApply == info->GetOriginalEntryPoint();
  5129. }
  5130. else
  5131. {
  5132. return false;
  5133. }
  5134. }
  5135. }
  5136. return false;
  5137. JIT_HELPER_END(Op_OP_BrFncEqApply);
  5138. }
  5139. BOOL JavascriptOperators::OP_BrFncNeqApply(Var instance, ScriptContext *scriptContext)
  5140. {
  5141. JIT_HELPER_NOT_REENTRANT_HEADER(Op_OP_BrFncNeqApply, reentrancylock, scriptContext->GetThreadContext());
  5142. // JavascriptFunction and !HostDispatch
  5143. if (JavascriptOperators::GetTypeId(instance) == TypeIds_Function)
  5144. {
  5145. FunctionProxy *bod = ((JavascriptFunction *)instance)->GetFunctionProxy();
  5146. if (bod != nullptr)
  5147. {
  5148. return bod->GetDirectEntryPoint(bod->GetDefaultEntryPointInfo()) != &Js::JavascriptFunction::EntryApply;
  5149. }
  5150. else
  5151. {
  5152. FunctionInfo* info = ((JavascriptFunction *)instance)->GetFunctionInfo();
  5153. if (info != nullptr)
  5154. {
  5155. return &Js::JavascriptFunction::EntryApply != info->GetOriginalEntryPoint();
  5156. }
  5157. else
  5158. {
  5159. return true;
  5160. }
  5161. }
  5162. }
  5163. return true;
  5164. JIT_HELPER_END(Op_OP_BrFncNeqApply);
  5165. }
  5166. BOOL JavascriptOperators::OP_BrHasSideEffects(int se, ScriptContext* scriptContext)
  5167. {
  5168. return (scriptContext->optimizationOverrides.GetSideEffects() & se) != SideEffects_None;
  5169. }
  5170. BOOL JavascriptOperators::OP_BrNotHasSideEffects(int se, ScriptContext* scriptContext)
  5171. {
  5172. return (scriptContext->optimizationOverrides.GetSideEffects() & se) == SideEffects_None;
  5173. }
  5174. // returns NULL if there is no more elements to enumerate.
  5175. Var JavascriptOperators::OP_BrOnEmpty(ForInObjectEnumerator * aEnumerator)
  5176. {
  5177. JIT_HELPER_REENTRANT_HEADER(Op_OP_BrOnEmpty);
  5178. PropertyId id;
  5179. return aEnumerator->MoveAndGetNext(id);
  5180. JIT_HELPER_END(Op_OP_BrOnEmpty);
  5181. }
  5182. void JavascriptOperators::OP_InitForInEnumerator(Var enumerable, ForInObjectEnumerator * enumerator, ScriptContext* scriptContext, EnumeratorCache * forInCache)
  5183. {
  5184. JIT_HELPER_REENTRANT_HEADER(Op_OP_InitForInEnumerator);
  5185. RecyclableObject* enumerableObject;
  5186. #if ENABLE_COPYONACCESS_ARRAY
  5187. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(enumerable);
  5188. #endif
  5189. if (!GetPropertyObject(enumerable, scriptContext, &enumerableObject))
  5190. {
  5191. enumerableObject = nullptr;
  5192. }
  5193. enumerator->Initialize(enumerableObject, scriptContext, false, forInCache);
  5194. JIT_HELPER_END(Op_OP_InitForInEnumerator);
  5195. }
  5196. Js::Var JavascriptOperators::OP_CmEq_A(Var a, Var b, ScriptContext* scriptContext)
  5197. {
  5198. JIT_HELPER_REENTRANT_HEADER(OP_CmEq_A);
  5199. return JavascriptBoolean::ToVar(JavascriptOperators::Equal(a, b, scriptContext), scriptContext);
  5200. JIT_HELPER_END(OP_CmEq_A);
  5201. }
  5202. Var JavascriptOperators::OP_CmNeq_A(Var a, Var b, ScriptContext* scriptContext)
  5203. {
  5204. JIT_HELPER_REENTRANT_HEADER(OP_CmNeq_A);
  5205. return JavascriptBoolean::ToVar(JavascriptOperators::NotEqual(a,b,scriptContext), scriptContext);
  5206. JIT_HELPER_END(OP_CmNeq_A);
  5207. }
  5208. Var JavascriptOperators::OP_CmSrEq_A(Var a, Var b, ScriptContext* scriptContext)
  5209. {
  5210. JIT_HELPER_REENTRANT_HEADER(OP_CmSrEq_A);
  5211. return JavascriptBoolean::ToVar(JavascriptOperators::StrictEqual(a, b, scriptContext), scriptContext);
  5212. JIT_HELPER_END(OP_CmSrEq_A);
  5213. }
  5214. Var JavascriptOperators::OP_CmSrEq_String(Var a, JavascriptString* b, ScriptContext *scriptContext)
  5215. {
  5216. JIT_HELPER_REENTRANT_HEADER(OP_CmSrEq_String);
  5217. return JavascriptBoolean::ToVar(JavascriptOperators::StrictEqualString(a, b), scriptContext);
  5218. JIT_HELPER_END(OP_CmSrEq_String);
  5219. }
  5220. Var JavascriptOperators::OP_CmSrEq_EmptyString(Var a, ScriptContext *scriptContext)
  5221. {
  5222. JIT_HELPER_REENTRANT_HEADER(OP_CmSrEq_EmptyString);
  5223. return JavascriptBoolean::ToVar(JavascriptOperators::StrictEqualEmptyString(a), scriptContext);
  5224. JIT_HELPER_END(OP_CmSrEq_EmptyString);
  5225. }
  5226. Var JavascriptOperators::OP_CmSrNeq_A(Var a, Var b, ScriptContext* scriptContext)
  5227. {
  5228. JIT_HELPER_REENTRANT_HEADER(OP_CmSrNeq_A);
  5229. return JavascriptBoolean::ToVar(JavascriptOperators::NotStrictEqual(a, b, scriptContext), scriptContext);
  5230. JIT_HELPER_END(OP_CmSrNeq_A);
  5231. }
  5232. Var JavascriptOperators::OP_CmLt_A(Var a, Var b, ScriptContext* scriptContext)
  5233. {
  5234. JIT_HELPER_REENTRANT_HEADER(OP_CmLt_A);
  5235. return JavascriptBoolean::ToVar(JavascriptOperators::Less(a, b, scriptContext), scriptContext);
  5236. JIT_HELPER_END(OP_CmLt_A);
  5237. }
  5238. Var JavascriptOperators::OP_CmLe_A(Var a, Var b, ScriptContext* scriptContext)
  5239. {
  5240. JIT_HELPER_REENTRANT_HEADER(OP_CmLe_A);
  5241. return JavascriptBoolean::ToVar(JavascriptOperators::LessEqual(a, b, scriptContext), scriptContext);
  5242. JIT_HELPER_END(OP_CmLe_A);
  5243. }
  5244. Var JavascriptOperators::OP_CmGt_A(Var a, Var b, ScriptContext* scriptContext)
  5245. {
  5246. JIT_HELPER_REENTRANT_HEADER(OP_CmGt_A);
  5247. return JavascriptBoolean::ToVar(JavascriptOperators::Greater(a, b, scriptContext), scriptContext);
  5248. JIT_HELPER_END(OP_CmGt_A);
  5249. }
  5250. Var JavascriptOperators::OP_CmGe_A(Var a, Var b, ScriptContext* scriptContext)
  5251. {
  5252. JIT_HELPER_REENTRANT_HEADER(OP_CmGe_A);
  5253. return JavascriptBoolean::ToVar(JavascriptOperators::GreaterEqual(a, b, scriptContext), scriptContext);
  5254. JIT_HELPER_END(OP_CmGe_A);
  5255. }
  5256. DetachedStateBase* JavascriptOperators::DetachVarAndGetState(Var var, bool queueForDelayFree/* = true*/)
  5257. {
  5258. switch (GetTypeId(var))
  5259. {
  5260. case TypeIds_ArrayBuffer:
  5261. return Js::VarTo<Js::ArrayBuffer>(var)->DetachAndGetState(queueForDelayFree);
  5262. default:
  5263. if (!Js::VarTo<Js::RecyclableObject>(var)->IsExternal())
  5264. {
  5265. AssertMsg(false, "We should explicitly have a case statement for each non-external object that can be detached.");
  5266. }
  5267. return nullptr;
  5268. }
  5269. }
  5270. bool JavascriptOperators::IsObjectDetached(Var var)
  5271. {
  5272. switch (GetTypeId(var))
  5273. {
  5274. case TypeIds_ArrayBuffer:
  5275. return Js::VarTo<Js::ArrayBuffer>(var)->IsDetached();
  5276. default:
  5277. return false;
  5278. }
  5279. }
  5280. Var JavascriptOperators::NewVarFromDetachedState(DetachedStateBase* state, JavascriptLibrary *library)
  5281. {
  5282. AssertOrFailFastMsg(state->GetTypeId() == TypeIds_ArrayBuffer, "We should only re-activate detached ArrayBuffer");
  5283. return Js::ArrayBuffer::NewFromDetachedState(state, library);
  5284. }
  5285. DynamicType *
  5286. JavascriptOperators::EnsureObjectLiteralType(ScriptContext* scriptContext, const Js::PropertyIdArray *propIds, Field(DynamicType*)* literalType)
  5287. {
  5288. JIT_HELPER_NOT_REENTRANT_HEADER(EnsureObjectLiteralType, reentrancylock, scriptContext->GetThreadContext());
  5289. DynamicType * newType = *literalType;
  5290. if (newType != nullptr)
  5291. {
  5292. if (!newType->GetIsShared())
  5293. {
  5294. newType->ShareType();
  5295. }
  5296. }
  5297. else
  5298. {
  5299. DynamicType* objectType =
  5300. FunctionBody::DoObjectHeaderInliningForObjectLiteral(propIds)
  5301. ? scriptContext->GetLibrary()->GetObjectHeaderInlinedLiteralType((uint16)propIds->count)
  5302. : scriptContext->GetLibrary()->GetObjectLiteralType(
  5303. static_cast<PropertyIndex>(
  5304. min(propIds->count, static_cast<uint32>(MaxPreInitializedObjectTypeInlineSlotCount))));
  5305. newType = PathTypeHandlerBase::CreateTypeForNewScObject(scriptContext, objectType, propIds, false);
  5306. *literalType = newType;
  5307. }
  5308. Assert(scriptContext);
  5309. Assert(GetLiteralInlineSlotCapacity(propIds) == newType->GetTypeHandler()->GetInlineSlotCapacity());
  5310. Assert(newType->GetTypeHandler()->GetSlotCapacity() >= 0);
  5311. Assert(GetLiteralSlotCapacity(propIds) == (uint)newType->GetTypeHandler()->GetSlotCapacity());
  5312. return newType;
  5313. JIT_HELPER_END(EnsureObjectLiteralType);
  5314. }
  5315. Var JavascriptOperators::NewScObjectLiteral(ScriptContext* scriptContext, const Js::PropertyIdArray *propIds, Field(DynamicType*)* literalType)
  5316. {
  5317. Assert(propIds->count != 0);
  5318. Assert(!propIds->hadDuplicates); // duplicates are removed by parser
  5319. #ifdef PROFILE_OBJECT_LITERALS
  5320. // Empty objects not counted in the object literal counts
  5321. scriptContext->objectLiteralInstanceCount++;
  5322. if (propIds->count > scriptContext->objectLiteralMaxLength)
  5323. {
  5324. scriptContext->objectLiteralMaxLength = propIds->count;
  5325. }
  5326. #endif
  5327. DynamicType* newType = EnsureObjectLiteralType(scriptContext, propIds, literalType);
  5328. DynamicObject* instance = DynamicObject::New(scriptContext->GetRecycler(), newType);
  5329. if (!newType->GetIsShared())
  5330. {
  5331. #if ENABLE_FIXED_FIELDS
  5332. newType->GetTypeHandler()->SetSingletonInstanceIfNeeded(instance);
  5333. #endif
  5334. }
  5335. #ifdef PROFILE_OBJECT_LITERALS
  5336. else
  5337. {
  5338. scriptContext->objectLiteralCacheCount++;
  5339. }
  5340. #endif
  5341. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(instance));
  5342. // can't auto-proxy here as object literal is not exactly "new" object and cannot be intercepted as proxy.
  5343. return instance;
  5344. }
  5345. uint JavascriptOperators::GetLiteralSlotCapacity(Js::PropertyIdArray const * propIds)
  5346. {
  5347. const uint inlineSlotCapacity = GetLiteralInlineSlotCapacity(propIds);
  5348. return DynamicTypeHandler::RoundUpSlotCapacity(propIds->count, static_cast<PropertyIndex>(inlineSlotCapacity));
  5349. }
  5350. uint JavascriptOperators::GetLiteralInlineSlotCapacity(
  5351. Js::PropertyIdArray const * propIds)
  5352. {
  5353. if (propIds->hadDuplicates)
  5354. {
  5355. return 0;
  5356. }
  5357. return
  5358. FunctionBody::DoObjectHeaderInliningForObjectLiteral(propIds)
  5359. ? DynamicTypeHandler::RoundUpObjectHeaderInlinedInlineSlotCapacity(static_cast<PropertyIndex>(propIds->count))
  5360. : DynamicTypeHandler::RoundUpInlineSlotCapacity(
  5361. static_cast<PropertyIndex>(
  5362. min(propIds->count, static_cast<uint32>(MaxPreInitializedObjectTypeInlineSlotCount))));
  5363. }
  5364. Var JavascriptOperators::OP_InitCachedScope(Var varFunc, const Js::PropertyIdArray *propIds, Field(DynamicType*)* literalType, bool formalsAreLetDecls, ScriptContext *scriptContext)
  5365. {
  5366. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitCachedScope, reentrancylock, scriptContext->GetThreadContext());
  5367. bool isGAFunction = VarIs<JavascriptFunction>(varFunc);
  5368. Assert(isGAFunction);
  5369. if (isGAFunction)
  5370. {
  5371. JavascriptFunction *function = VarTo<JavascriptFunction>(varFunc);
  5372. isGAFunction = JavascriptGeneratorFunction::Test(function) || JavascriptAsyncFunction::Test(function);
  5373. }
  5374. ScriptFunction *func = isGAFunction ?
  5375. VarTo<JavascriptGeneratorFunction>(varFunc)->GetGeneratorVirtualScriptFunction() :
  5376. VarTo<ScriptFunction>(varFunc);
  5377. #ifdef PROFILE_OBJECT_LITERALS
  5378. // Empty objects not counted in the object literal counts
  5379. scriptContext->objectLiteralInstanceCount++;
  5380. if (propIds->count > scriptContext->objectLiteralMaxLength)
  5381. {
  5382. scriptContext->objectLiteralMaxLength = propIds->count;
  5383. }
  5384. #endif
  5385. PropertyId cachedFuncCount = ActivationObjectEx::GetCachedFuncCount(propIds);
  5386. PropertyId firstFuncSlot = ActivationObjectEx::GetFirstFuncSlot(propIds);
  5387. PropertyId firstVarSlot = ActivationObjectEx::GetFirstVarSlot(propIds);
  5388. PropertyId lastFuncSlot = Constants::NoProperty;
  5389. if (firstFuncSlot != Constants::NoProperty)
  5390. {
  5391. if (firstVarSlot == Constants::NoProperty || firstVarSlot < firstFuncSlot)
  5392. {
  5393. lastFuncSlot = propIds->count - 1;
  5394. }
  5395. else
  5396. {
  5397. lastFuncSlot = firstVarSlot - 1;
  5398. }
  5399. }
  5400. DynamicType *type = *literalType;
  5401. if (type != nullptr)
  5402. {
  5403. #ifdef PROFILE_OBJECT_LITERALS
  5404. scriptContext->objectLiteralCacheCount++;
  5405. #endif
  5406. }
  5407. else
  5408. {
  5409. type = scriptContext->GetLibrary()->GetActivationObjectType();
  5410. if (formalsAreLetDecls)
  5411. {
  5412. uint formalsSlotLimit = (firstFuncSlot != Constants::NoProperty) ? (uint)firstFuncSlot :
  5413. (firstVarSlot != Constants::NoProperty) ? (uint)firstVarSlot :
  5414. propIds->count;
  5415. if (func->GetFunctionBody()->HasReferenceableBuiltInArguments())
  5416. {
  5417. type = PathTypeHandlerBase::CreateNewScopeObject<true>(scriptContext, type, propIds, PropertyLet, formalsSlotLimit);
  5418. }
  5419. else
  5420. {
  5421. type = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, type, propIds, PropertyLet, formalsSlotLimit);
  5422. }
  5423. }
  5424. else
  5425. {
  5426. type = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, type, propIds);
  5427. }
  5428. *literalType = type;
  5429. }
  5430. Var undef = scriptContext->GetLibrary()->GetUndefined();
  5431. ActivationObjectEx *scopeObjEx = func->GetCachedScope();
  5432. if (scopeObjEx && scopeObjEx->IsCommitted())
  5433. {
  5434. scopeObjEx->ReplaceType(type);
  5435. scopeObjEx->SetCommit(false);
  5436. #if DBG
  5437. for (uint i = firstVarSlot; i < propIds->count; i++)
  5438. {
  5439. AssertMsg(scopeObjEx->GetSlot(i) == undef, "Var attached to cached scope");
  5440. }
  5441. #endif
  5442. }
  5443. else
  5444. {
  5445. ActivationObjectEx *tmp = RecyclerNewPlus(scriptContext->GetRecycler(), (cachedFuncCount == 0 ? 0 : cachedFuncCount - 1) * sizeof(FuncCacheEntry), ActivationObjectEx, type, func, cachedFuncCount, firstFuncSlot, lastFuncSlot);
  5446. if (!scopeObjEx)
  5447. {
  5448. func->SetCachedScope(tmp);
  5449. }
  5450. scopeObjEx = tmp;
  5451. for (uint i = firstVarSlot; i < propIds->count; i++)
  5452. {
  5453. scopeObjEx->SetSlot(SetSlotArguments(propIds->elements[i], i, undef));
  5454. }
  5455. }
  5456. return scopeObjEx;
  5457. JIT_HELPER_END(OP_InitCachedScope);
  5458. }
  5459. void JavascriptOperators::OP_InvalidateCachedScope(void* varEnv, int32 envIndex)
  5460. {
  5461. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_InvalidateCachedScope);
  5462. FrameDisplay *disp = (FrameDisplay*)varEnv;
  5463. Var item = disp->GetItem(envIndex);
  5464. if (item != nullptr)
  5465. {
  5466. Assert(VarIs<ActivationObjectEx>(item));
  5467. RecyclableObject *objScope = VarTo<RecyclableObject>(item);
  5468. objScope->InvalidateCachedScope();
  5469. }
  5470. JIT_HELPER_END(OP_InvalidateCachedScope);
  5471. }
  5472. void JavascriptOperators::OP_InitCachedFuncs(Var varScope, FrameDisplay *pDisplay, const FuncInfoArray *info, ScriptContext *scriptContext)
  5473. {
  5474. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitCachedFuncs, reentrancylock, scriptContext->GetThreadContext());
  5475. ActivationObjectEx *scopeObj = VarTo<ActivationObjectEx>(varScope);
  5476. Assert(scopeObj->GetTypeHandler()->GetInlineSlotCapacity() == 0);
  5477. uint funcCount = info->count;
  5478. if (funcCount == 0)
  5479. {
  5480. // Degenerate case: no nested funcs at all
  5481. return;
  5482. }
  5483. if (scopeObj->HasCachedFuncs())
  5484. {
  5485. for (uint i = 0; i < funcCount; i++)
  5486. {
  5487. const FuncCacheEntry *entry = scopeObj->GetFuncCacheEntry(i);
  5488. ScriptFunction *func = entry->func;
  5489. FunctionProxy * proxy = func->GetFunctionProxy();
  5490. // Reset the function's type to the default type with no properties
  5491. // Use the cached type on the function proxy rather than the type in the func cache entry
  5492. // CONSIDER: Stop caching the function types in the scope object
  5493. func->ReplaceType(proxy->EnsureDeferredPrototypeType());
  5494. func->ResetConstructorCacheToDefault();
  5495. uint scopeSlot = info->elements[i].scopeSlot;
  5496. if (scopeSlot != Constants::NoProperty)
  5497. {
  5498. // CONSIDER: Store property IDs in FuncInfoArray in debug builds so we can properly assert in SetAuxSlot
  5499. scopeObj->SetAuxSlot(SetSlotArguments(Constants::NoProperty, scopeSlot, entry->func));
  5500. }
  5501. }
  5502. return;
  5503. }
  5504. // No cached functions, so create them and cache them.
  5505. JavascriptFunction *funcParent = scopeObj->GetParentFunc();
  5506. for (uint i = 0; i < funcCount; i++)
  5507. {
  5508. const FuncInfoEntry *entry = &info->elements[i];
  5509. uint nestedIndex = entry->nestedIndex;
  5510. uint scopeSlot = entry->scopeSlot;
  5511. FunctionProxy * proxy = funcParent->GetFunctionBody()->GetNestedFunctionProxy(nestedIndex);
  5512. ScriptFunction *func = scriptContext->GetLibrary()->CreateScriptFunction(proxy);
  5513. func->SetEnvironment(pDisplay);
  5514. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_FUNCTION(func, EtwTrace::GetFunctionId(proxy)));
  5515. scopeObj->SetCachedFunc(i, func);
  5516. if (scopeSlot != Constants::NoProperty)
  5517. {
  5518. // CONSIDER: Store property IDs in FuncInfoArray in debug builds so we can properly assert in SetAuxSlot
  5519. scopeObj->SetAuxSlot(SetSlotArguments(Constants::NoProperty, scopeSlot, func));
  5520. }
  5521. }
  5522. JIT_HELPER_END(OP_InitCachedFuncs);
  5523. }
  5524. Var JavascriptOperators::AddVarsToArraySegment(SparseArraySegment<Var> * segment, const Js::VarArray *vars)
  5525. {
  5526. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ArraySegmentVars);
  5527. uint32 count = vars->count;
  5528. Assert(segment->left == 0);
  5529. Assert(count <= segment->size);
  5530. if(count > segment->length)
  5531. {
  5532. segment->length = count;
  5533. segment->CheckLengthvsSize();
  5534. }
  5535. CopyArray(segment->elements, segment->length, vars->elements, count);
  5536. return segment;
  5537. JIT_HELPER_END(ArraySegmentVars);
  5538. }
  5539. void JavascriptOperators::AddIntsToArraySegment(SparseArraySegment<int32> * segment, const Js::AuxArray<int32> *ints)
  5540. {
  5541. uint32 count = ints->count;
  5542. Assert(segment->left == 0);
  5543. Assert(count <= segment->size);
  5544. if(count > segment->length)
  5545. {
  5546. segment->length = count;
  5547. segment->CheckLengthvsSize();
  5548. }
  5549. js_memcpy_s(segment->elements, sizeof(int32) * segment->length, ints->elements, sizeof(int32) * count);
  5550. }
  5551. void JavascriptOperators::AddFloatsToArraySegment(SparseArraySegment<double> * segment, const Js::AuxArray<double> *doubles)
  5552. {
  5553. uint32 count = doubles->count;
  5554. Assert(segment->left == 0);
  5555. Assert(count <= segment->size);
  5556. if(count > segment->length)
  5557. {
  5558. segment->length = count;
  5559. segment->CheckLengthvsSize();
  5560. }
  5561. js_memcpy_s(segment->elements, sizeof(double) * segment->length, doubles->elements, sizeof(double) * count);
  5562. }
  5563. RecyclableObject * JavascriptOperators::GetPrototypeObject(RecyclableObject * constructorFunction, ScriptContext * scriptContext)
  5564. {
  5565. Var prototypeProperty = JavascriptOperators::GetProperty(constructorFunction, PropertyIds::prototype, scriptContext);
  5566. RecyclableObject* prototypeObject;
  5567. PrototypeObject(prototypeProperty, constructorFunction, scriptContext, &prototypeObject);
  5568. return prototypeObject;
  5569. }
  5570. RecyclableObject * JavascriptOperators::GetPrototypeObjectForConstructorCache(RecyclableObject * constructor, ScriptContext* requestContext, bool& canBeCached)
  5571. {
  5572. PropertyValueInfo info;
  5573. Var prototypeValue;
  5574. RecyclableObject* prototypeObject;
  5575. canBeCached = false;
  5576. // Do a local property lookup. Since a function's prototype property is a non-configurable data property, we don't need to worry
  5577. // about the prototype being an accessor property, whose getter returns different values every time it's called.
  5578. if (constructor->GetProperty(constructor, PropertyIds::prototype, &prototypeValue, &info, requestContext))
  5579. {
  5580. if (!JavascriptOperators::PrototypeObject(prototypeValue, constructor, requestContext, &prototypeObject))
  5581. {
  5582. // The value returned by the property lookup is not a valid prototype object, default to object prototype.
  5583. Assert(prototypeObject == constructor->GetLibrary()->GetObjectPrototype());
  5584. }
  5585. // For these scenarios, we do not want to populate the cache.
  5586. if (constructor->GetScriptContext() != requestContext || info.GetInstance() != constructor)
  5587. {
  5588. return prototypeObject;
  5589. }
  5590. }
  5591. else
  5592. {
  5593. // It's ok to cache Object.prototype, because Object.prototype cannot be overwritten.
  5594. prototypeObject = constructor->GetLibrary()->GetObjectPrototype();
  5595. }
  5596. canBeCached = true;
  5597. return prototypeObject;
  5598. }
  5599. bool JavascriptOperators::PrototypeObject(Var prototypeProperty, RecyclableObject * constructorFunction, ScriptContext * scriptContext, RecyclableObject** prototypeObject)
  5600. {
  5601. TypeId prototypeType = JavascriptOperators::GetTypeId(prototypeProperty);
  5602. if (JavascriptOperators::IsObjectType(prototypeType))
  5603. {
  5604. *prototypeObject = VarTo<RecyclableObject>(prototypeProperty);
  5605. return true;
  5606. }
  5607. *prototypeObject = constructorFunction->GetLibrary()->GetObjectPrototype();
  5608. return false;
  5609. }
  5610. FunctionInfo* JavascriptOperators::GetConstructorFunctionInfo(Var instance, ScriptContext * scriptContext)
  5611. {
  5612. TypeId typeId = JavascriptOperators::GetTypeId(instance);
  5613. if (typeId == TypeIds_Function)
  5614. {
  5615. JavascriptFunction * function = UnsafeVarTo<JavascriptFunction>(instance);
  5616. return function->GetFunctionInfo();
  5617. }
  5618. if (typeId != TypeIds_HostDispatch && typeId != TypeIds_Proxy)
  5619. {
  5620. if (typeId == TypeIds_Null)
  5621. {
  5622. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  5623. }
  5624. JavascriptError::ThrowTypeError(scriptContext, VBSERR_ActionNotSupported);
  5625. }
  5626. return nullptr;
  5627. }
  5628. Var JavascriptOperators::NewJavascriptObjectNoArg(ScriptContext* requestContext)
  5629. {
  5630. JIT_HELPER_NOT_REENTRANT_HEADER(NewJavascriptObjectNoArg, reentrancylock, requestContext->GetThreadContext());
  5631. DynamicObject * newObject = requestContext->GetLibrary()->CreateObject(true);
  5632. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newObject));
  5633. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5634. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5635. {
  5636. newObject = VarTo<DynamicObject>(JavascriptProxy::AutoProxyWrapper(newObject));
  5637. }
  5638. #endif
  5639. return newObject;
  5640. JIT_HELPER_END(NewJavascriptObjectNoArg);
  5641. }
  5642. Var JavascriptOperators::NewJavascriptArrayNoArg(ScriptContext* requestContext)
  5643. {
  5644. JIT_HELPER_NOT_REENTRANT_HEADER(NewJavascriptArrayNoArg, reentrancylock, requestContext->GetThreadContext());
  5645. JavascriptArray * newArray = requestContext->GetLibrary()->CreateArray();
  5646. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newArray));
  5647. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5648. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5649. {
  5650. newArray = static_cast<JavascriptArray*>(JavascriptProxy::AutoProxyWrapper(newArray));
  5651. }
  5652. #endif
  5653. return newArray;
  5654. JIT_HELPER_END(NewJavascriptArrayNoArg);
  5655. }
  5656. Var JavascriptOperators::NewScObjectNoArgNoCtorFull(Var instance, ScriptContext* requestContext)
  5657. {
  5658. // This helper can be reentrant because although we don't call the Constructor, we might have to parse it if bytecode is missing
  5659. // In which case, we would leave script. When we leave script we DisposeObjects which can dispose of Edge objects that could
  5660. // have a javascript onDispose handler and call that handler.
  5661. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoArgNoCtorFull);
  5662. return NewScObjectNoArgNoCtorCommon(instance, requestContext, true);
  5663. JIT_HELPER_END(NewScObjectNoArgNoCtorFull);
  5664. }
  5665. Var JavascriptOperators::NewScObjectNoArgNoCtor(Var instance, ScriptContext* requestContext)
  5666. {
  5667. // This helper can be reentrant because although we don't call the Constructor, we might have to parse it if bytecode is missing
  5668. // In which case, we would leave script. When we leave script we DisposeObjects which can dispose of Edge objects that could
  5669. // have a javascript onDispose handler and call that handler.
  5670. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoArgNoCtor);
  5671. return NewScObjectNoArgNoCtorCommon(instance, requestContext, false);
  5672. JIT_HELPER_END(NewScObjectNoArgNoCtor);
  5673. }
  5674. Var JavascriptOperators::NewScObjectNoArgNoCtorCommon(Var instance, ScriptContext* requestContext, bool isBaseClassConstructorNewScObject)
  5675. {
  5676. RecyclableObject * object = VarTo<RecyclableObject>(instance);
  5677. FunctionInfo* functionInfo = JavascriptOperators::GetConstructorFunctionInfo(instance, requestContext);
  5678. Assert(functionInfo != &JavascriptObject::EntryInfo::NewInstance); // built-ins are not inlined
  5679. Assert(functionInfo != &JavascriptArray::EntryInfo::NewInstance); // built-ins are not inlined
  5680. return functionInfo != nullptr ?
  5681. JavascriptOperators::NewScObjectCommon(object, functionInfo, requestContext, isBaseClassConstructorNewScObject) :
  5682. JavascriptOperators::NewScObjectHostDispatchOrProxy(object, requestContext);
  5683. }
  5684. Var JavascriptOperators::NewScObjectNoArg(Var instance, ScriptContext * requestContext)
  5685. {
  5686. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoArg);
  5687. JavascriptProxy * proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  5688. if (proxy)
  5689. {
  5690. Var dummy = nullptr;
  5691. Arguments args(CallInfo(CallFlags_New, 1), &dummy);
  5692. return requestContext->GetThreadContext()->ExecuteImplicitCall(proxy, Js::ImplicitCall_Accessor, [=]()->Js::Var
  5693. {
  5694. return proxy->ConstructorTrap(args, requestContext, 0);
  5695. });
  5696. }
  5697. FunctionInfo* functionInfo = JavascriptOperators::GetConstructorFunctionInfo(instance, requestContext);
  5698. RecyclableObject * object = VarTo<RecyclableObject>(instance);
  5699. if (functionInfo == &JavascriptObject::EntryInfo::NewInstance)
  5700. {
  5701. // Fast path for new Object()
  5702. Assert((functionInfo->GetAttributes() & FunctionInfo::ErrorOnNew) == 0);
  5703. JavascriptLibrary* library = object->GetLibrary();
  5704. DynamicObject * newObject = library->CreateObject(true);
  5705. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newObject));
  5706. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5707. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5708. {
  5709. newObject = VarTo<DynamicObject>(JavascriptProxy::AutoProxyWrapper(newObject));
  5710. }
  5711. #endif
  5712. #if DBG
  5713. DynamicType* newObjectType = newObject->GetDynamicType();
  5714. Assert(newObjectType->GetIsShared());
  5715. JavascriptFunction* constructor = VarTo<JavascriptFunction>(instance);
  5716. Assert(!constructor->GetConstructorCache()->NeedsUpdateAfterCtor());
  5717. #endif
  5718. ScriptContext * scriptContext = library->GetScriptContext();
  5719. if (scriptContext != requestContext)
  5720. {
  5721. CrossSite::MarshalDynamicObjectAndPrototype(requestContext, newObject);
  5722. }
  5723. return newObject;
  5724. }
  5725. else if (functionInfo == &JavascriptArray::EntryInfo::NewInstance)
  5726. {
  5727. Assert((functionInfo->GetAttributes() & FunctionInfo::ErrorOnNew) == 0);
  5728. JavascriptLibrary* library = object->GetLibrary();
  5729. JavascriptArray * newArray = library->CreateArray();
  5730. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newArray));
  5731. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5732. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5733. {
  5734. newArray = static_cast<JavascriptArray*>(JavascriptProxy::AutoProxyWrapper(newArray));
  5735. }
  5736. #endif
  5737. #if DBG
  5738. DynamicType* newArrayType = newArray->GetDynamicType();
  5739. Assert(newArrayType->GetIsShared());
  5740. JavascriptFunction* constructor = VarTo<JavascriptFunction>(instance);
  5741. Assert(!constructor->GetConstructorCache()->NeedsUpdateAfterCtor());
  5742. #endif
  5743. ScriptContext * scriptContext = library->GetScriptContext();
  5744. if (scriptContext != requestContext)
  5745. {
  5746. CrossSite::MarshalDynamicObjectAndPrototype(requestContext, newArray);
  5747. }
  5748. return newArray;
  5749. }
  5750. Var newObject = functionInfo != nullptr ?
  5751. JavascriptOperators::NewScObjectCommon(object, functionInfo, requestContext) :
  5752. JavascriptOperators::NewScObjectHostDispatchOrProxy(object, requestContext);
  5753. ThreadContext * threadContext = object->GetScriptContext()->GetThreadContext();
  5754. Var returnVar = threadContext->ExecuteImplicitCall(object, Js::ImplicitCall_Accessor, [=]()->Js::Var
  5755. {
  5756. return CALL_FUNCTION(threadContext, object, CallInfo(CallFlags_New, 1), newObject);
  5757. });
  5758. if (JavascriptOperators::IsObject(returnVar))
  5759. {
  5760. newObject = returnVar;
  5761. }
  5762. ConstructorCache * constructorCache = nullptr;
  5763. JavascriptFunction *function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  5764. if (function)
  5765. {
  5766. constructorCache = function->GetConstructorCache();
  5767. }
  5768. if (constructorCache != nullptr && constructorCache->NeedsUpdateAfterCtor())
  5769. {
  5770. JavascriptOperators::UpdateNewScObjectCache(object, newObject, requestContext);
  5771. }
  5772. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5773. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5774. {
  5775. DynamicObject* newDynamicObject = VarTo<DynamicObject>(JavascriptProxy::AutoProxyWrapper(newObject));
  5776. // this might come from a different scriptcontext.
  5777. newObject = CrossSite::MarshalVar(requestContext, newDynamicObject, newDynamicObject->GetScriptContext());
  5778. }
  5779. #endif
  5780. return newObject;
  5781. JIT_HELPER_END(NewScObjectNoArg);
  5782. }
  5783. Var JavascriptOperators::NewScObjectNoCtorFull(Var instance, ScriptContext* requestContext)
  5784. {
  5785. // This helper can be reentrant because although we don't call the Constructor, we might have to parse it if bytecode is missing
  5786. // In which case, we would leave script. When we leave script we DisposeObjects which can dispose of Edge objects that could
  5787. // have a javascript onDispose handler and call that handler.
  5788. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoCtorFull);
  5789. return NewScObjectNoCtorCommon(instance, requestContext, true);
  5790. JIT_HELPER_END(NewScObjectNoCtorFull);
  5791. }
  5792. Var JavascriptOperators::NewScObjectNoCtor(Var instance, ScriptContext * requestContext)
  5793. {
  5794. // This helper can be reentrant because although we don't call the Constructor, we might have to parse it if bytecode is missing
  5795. // In which case, we would leave script. When we leave script we DisposeObjects which can dispose of Edge objects that could
  5796. // have a javascript onDispose handler and call that handler.
  5797. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoCtor);
  5798. // We can still call into NewScObjectNoCtor variations in JIT code for performance; however for proxy we don't
  5799. // really need the new object as the trap will handle the "this" pointer separately. pass back nullptr to ensure
  5800. // failure in invalid case.
  5801. return (VarIs<JavascriptProxy>(instance)) ? nullptr : NewScObjectNoCtorCommon(instance, requestContext, false);
  5802. JIT_HELPER_END(NewScObjectNoCtor);
  5803. }
  5804. Var JavascriptOperators::NewScObjectNoCtorCommon(Var instance, ScriptContext* requestContext, bool isBaseClassConstructorNewScObject)
  5805. {
  5806. FunctionInfo* functionInfo = JavascriptOperators::GetConstructorFunctionInfo(instance, requestContext);
  5807. if (functionInfo)
  5808. {
  5809. return JavascriptOperators::NewScObjectCommon(UnsafeVarTo<RecyclableObject>(instance), functionInfo, requestContext, isBaseClassConstructorNewScObject);
  5810. }
  5811. else
  5812. {
  5813. return JavascriptOperators::NewScObjectHostDispatchOrProxy(VarTo<RecyclableObject>(instance), requestContext);
  5814. }
  5815. }
  5816. Var JavascriptOperators::NewScObjectHostDispatchOrProxy(RecyclableObject * function, ScriptContext * requestContext)
  5817. {
  5818. ScriptContext* functionScriptContext = function->GetScriptContext();
  5819. RecyclableObject * prototype = JavascriptOperators::GetPrototypeObject(function, functionScriptContext);
  5820. prototype = VarTo<RecyclableObject>(CrossSite::MarshalVar(requestContext, prototype, functionScriptContext));
  5821. Var object = requestContext->GetLibrary()->CreateObject(prototype);
  5822. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(object));
  5823. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5824. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5825. {
  5826. object = VarTo<DynamicObject>(JavascriptProxy::AutoProxyWrapper(object));
  5827. }
  5828. #endif
  5829. return object;
  5830. }
  5831. Var JavascriptOperators::NewScObjectCommon(RecyclableObject * function, FunctionInfo* functionInfo, ScriptContext * requestContext, bool isBaseClassConstructorNewScObject)
  5832. {
  5833. // CONSIDER: Allow for the cache to be repopulated if the type got collected, and a new one got populated with
  5834. // the same number of inlined slots. This requires that the JIT-ed code actually load the type from the cache
  5835. // (instead of hard-coding it), but it can (and must) keep the hard-coded number of inline slots.
  5836. // CONSIDER: Consider also not pinning the type in the cache. This can be done by using a registration based
  5837. // weak reference (we need to control the memory address), which we don't yet have, or by allocating the cache from
  5838. // the inline cache arena to allow it to be zeroed, but retain a recycler-allocated portion to hold on to the size of
  5839. // inlined slots.
  5840. JavascriptFunction* constructor = UnsafeVarTo<JavascriptFunction>(function);
  5841. if (functionInfo->IsClassConstructor() && !isBaseClassConstructorNewScObject)
  5842. {
  5843. // If we are calling new on a class constructor, the contract is that we pass new.target as the 'this' argument.
  5844. // function is the constructor on which we called new - which is new.target.
  5845. // If we are trying to construct the object for a base class constructor as part of a super call, we should not
  5846. // store new.target in the 'this' argument.
  5847. return function;
  5848. }
  5849. ConstructorCache* constructorCache = constructor->GetConstructorCache();
  5850. AssertMsg(constructorCache->GetScriptContext() == nullptr || constructorCache->GetScriptContext() == constructor->GetScriptContext(),
  5851. "Why did we populate a constructor cache with a mismatched script context?");
  5852. Assert(constructorCache != nullptr);
  5853. DynamicType* type = constructorCache->GetGuardValueAsType();
  5854. if (type != nullptr && constructorCache->GetScriptContext() == requestContext)
  5855. {
  5856. #if DBG
  5857. bool cachedProtoCanBeCached;
  5858. Assert(type->GetPrototype() == JavascriptOperators::GetPrototypeObjectForConstructorCache(constructor, requestContext, cachedProtoCanBeCached));
  5859. Assert(cachedProtoCanBeCached);
  5860. Assert(type->GetIsShared());
  5861. #endif
  5862. #if DBG_DUMP
  5863. TraceUseConstructorCache(constructorCache, constructor, true);
  5864. #endif
  5865. Var object = DynamicObject::New(requestContext->GetRecycler(), type);
  5866. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(object));
  5867. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5868. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5869. {
  5870. object = VarTo<DynamicObject>(JavascriptProxy::AutoProxyWrapper(object));
  5871. }
  5872. #endif
  5873. return object;
  5874. }
  5875. if (constructorCache->SkipDefaultNewObject())
  5876. {
  5877. Assert(!constructorCache->NeedsUpdateAfterCtor());
  5878. #if DBG_DUMP
  5879. TraceUseConstructorCache(constructorCache, constructor, true);
  5880. #endif
  5881. if (isBaseClassConstructorNewScObject)
  5882. {
  5883. return JavascriptOperators::CreateFromConstructor(function, requestContext);
  5884. }
  5885. return nullptr;
  5886. }
  5887. #if DBG_DUMP
  5888. TraceUseConstructorCache(constructorCache, constructor, false);
  5889. #endif
  5890. ScriptContext* constructorScriptContext = function->GetScriptContext();
  5891. Assert(!constructorScriptContext->GetThreadContext()->IsDisableImplicitException());
  5892. // we shouldn't try to call the constructor if it's closed already.
  5893. constructorScriptContext->VerifyAlive(TRUE, requestContext);
  5894. FunctionInfo::Attributes attributes = functionInfo->GetAttributes();
  5895. if (attributes & FunctionInfo::ErrorOnNew)
  5896. {
  5897. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnNew);
  5898. }
  5899. // Slow path
  5900. FunctionProxy * ctorProxy = constructor->GetFunctionProxy();
  5901. FunctionBody * functionBody = ctorProxy != nullptr ? ctorProxy->EnsureDeserialized()->Parse() : nullptr;
  5902. if (attributes & FunctionInfo::SkipDefaultNewObject)
  5903. {
  5904. // The constructor doesn't use the default new object.
  5905. #pragma prefast(suppress:6236, "DevDiv bug 830883. False positive when PHASE_OFF is #defined as '(false)'.")
  5906. if (!PHASE_OFF1(ConstructorCachePhase) && (functionBody == nullptr || !PHASE_OFF(ConstructorCachePhase, functionBody)))
  5907. {
  5908. constructorCache = constructor->EnsureValidConstructorCache();
  5909. constructorCache->PopulateForSkipDefaultNewObject(constructorScriptContext);
  5910. #if DBG_DUMP
  5911. if ((functionBody != nullptr && PHASE_TRACE(Js::ConstructorCachePhase, functionBody)) || (functionBody == nullptr && PHASE_TRACE1(Js::ConstructorCachePhase)))
  5912. {
  5913. const char16* ctorName = functionBody != nullptr ? functionBody->GetDisplayName() : _u("<unknown>");
  5914. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5915. Output::Print(_u("CtorCache: populated cache (0x%p) for ctor %s (%s): "), constructorCache, ctorName,
  5916. functionBody ? functionBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"));
  5917. constructorCache->Dump();
  5918. Output::Print(_u("\n"));
  5919. Output::Flush();
  5920. }
  5921. #endif
  5922. }
  5923. Assert(!constructorCache->NeedsUpdateAfterCtor());
  5924. return nullptr;
  5925. }
  5926. // CONSIDER: Create some form of PatchGetProtoObjForCtorCache, which actually caches the prototype object in the constructor cache.
  5927. // Make sure that it does NOT populate the guard field. On the slow path (the only path for cross-context calls) we can do a faster lookup
  5928. // after we fail the guard check. When invalidating the cache for proto change, make sure we zap the prototype field of the cache in
  5929. // addition to the guard value.
  5930. bool prototypeCanBeCached;
  5931. RecyclableObject* prototype = JavascriptOperators::GetPrototypeObjectForConstructorCache(
  5932. function, constructorScriptContext, prototypeCanBeCached);
  5933. prototype = VarTo<RecyclableObject>(CrossSite::MarshalVar(requestContext,
  5934. prototype, constructorScriptContext));
  5935. DynamicObject* newObject = requestContext->GetLibrary()->CreateObject(prototype, 8);
  5936. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newObject));
  5937. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5938. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5939. {
  5940. newObject = VarTo<DynamicObject>(JavascriptProxy::AutoProxyWrapper(newObject));
  5941. }
  5942. #endif
  5943. Assert(newObject->GetTypeHandler()->GetPropertyCount() == 0);
  5944. if (prototypeCanBeCached && functionBody != nullptr && requestContext == constructorScriptContext &&
  5945. !Js::VarIs<Js::JavascriptProxy>(newObject) &&
  5946. !PHASE_OFF1(ConstructorCachePhase) && !PHASE_OFF(ConstructorCachePhase, functionBody))
  5947. {
  5948. DynamicType* newObjectType = newObject->GetDynamicType();
  5949. // Initial type (without any properties) should always be shared up-front. This allows us to populate the cache right away.
  5950. Assert(newObjectType->GetIsShared());
  5951. // Populate the cache here and set the updateAfterCtor flag. This way, if the ctor is called recursively the
  5952. // recursive calls will hit the cache and use the initial type. On the unwind path, we will update the cache
  5953. // after the innermost ctor and clear the flag. After subsequent ctors we won't attempt an update anymore.
  5954. // As long as the updateAfterCtor flag is set it is safe to update the cache, because it would not have been
  5955. // hard-coded in the JIT-ed code.
  5956. constructorCache = constructor->EnsureValidConstructorCache();
  5957. constructorCache->Populate(newObjectType, constructorScriptContext, functionBody->GetHasNoExplicitReturnValue(), true);
  5958. Assert(constructorCache->IsConsistent());
  5959. #if DBG_DUMP
  5960. if ((functionBody != nullptr && PHASE_TRACE(Js::ConstructorCachePhase, functionBody)) || (functionBody == nullptr && PHASE_TRACE1(Js::ConstructorCachePhase)))
  5961. {
  5962. const char16* ctorName = functionBody != nullptr ? functionBody->GetDisplayName() : _u("<unknown>");
  5963. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5964. Output::Print(_u("CtorCache: populated cache (0x%p) for ctor %s (%s): "), constructorCache, ctorName,
  5965. functionBody ? functionBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"));
  5966. constructorCache->Dump();
  5967. Output::Print(_u("\n"));
  5968. Output::Flush();
  5969. }
  5970. #endif
  5971. }
  5972. else
  5973. {
  5974. #if DBG_DUMP
  5975. if ((functionBody != nullptr && PHASE_TRACE(Js::ConstructorCachePhase, functionBody)) || (functionBody == nullptr && PHASE_TRACE1(Js::ConstructorCachePhase)))
  5976. {
  5977. const char16* ctorName = functionBody != nullptr ? functionBody->GetDisplayName() : _u("<unknown>");
  5978. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5979. Output::Print(_u("CtorCache: did not populate cache (0x%p) for ctor %s (%s), because %s: prototype = 0x%p, functionBody = 0x%p, ctor context = 0x%p, request context = 0x%p"),
  5980. constructorCache, ctorName, functionBody ? functionBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"),
  5981. !prototypeCanBeCached ? _u("prototype cannot be cached") :
  5982. functionBody == nullptr ? _u("function has no body") :
  5983. requestContext != constructorScriptContext ? _u("of cross-context call") : _u("constructor cache phase is off"),
  5984. prototype, functionBody, constructorScriptContext, requestContext);
  5985. Output::Print(_u("\n"));
  5986. Output::Flush();
  5987. }
  5988. #endif
  5989. }
  5990. return newObject;
  5991. }
  5992. void JavascriptOperators::UpdateNewScObjectCache(Var function, Var instance, ScriptContext* requestContext)
  5993. {
  5994. JIT_HELPER_NOT_REENTRANT_HEADER(UpdateNewScObjectCache, reentrancylock, requestContext->GetThreadContext());
  5995. JavascriptFunction* constructor = VarTo<JavascriptFunction>(function);
  5996. if(constructor->GetScriptContext() != requestContext)
  5997. {
  5998. // The cache is populated only when the constructor function's context is the same as the calling context. However,
  5999. // the cached type is not finalized yet and may not be until multiple calls to the constructor have been made (see
  6000. // flag ConstructorCallsRequiredToFinalizeCachedType). A subsequent call to the constructor may be made from a
  6001. // different context, so ignore those cross-context calls and wait for the constructor to be called from its own
  6002. // context again to finalize the cached type.
  6003. return;
  6004. }
  6005. // Review : What happens if the cache got invalidated between NewScObject and here?
  6006. // Should we allocate new? Should we mark it as polymorphic?
  6007. ConstructorCache* constructorCache = constructor->GetConstructorCache();
  6008. Assert(constructorCache->IsConsistent());
  6009. Assert(!ConstructorCache::IsDefault(constructorCache));
  6010. AssertMsg(constructorCache->GetScriptContext() == constructor->GetScriptContext(), "Why did we populate a constructor cache with a mismatched script context?");
  6011. AssertMsg(constructorCache->IsPopulated(), "Why are we updating a constructor cache that hasn't been populated?");
  6012. // The presence of the updateAfterCtor flag guarantees that this cache hasn't been used in JIT-ed fast path. Even, if the
  6013. // cache is invalidated, this flag is not changed.
  6014. AssertMsg(constructorCache->NeedsUpdateAfterCtor(), "Why are we updating a constructor cache that doesn't need to be updated?");
  6015. const bool finalizeCachedType =
  6016. constructorCache->CallCount() >= CONFIG_FLAG(ConstructorCallsRequiredToFinalizeCachedType);
  6017. if(!finalizeCachedType)
  6018. {
  6019. constructorCache->IncCallCount();
  6020. }
  6021. else
  6022. {
  6023. constructorCache->ClearUpdateAfterCtor();
  6024. }
  6025. FunctionBody* constructorBody = constructor->GetFunctionBody();
  6026. AssertMsg(constructorBody != nullptr, "Constructor function doesn't have a function body.");
  6027. Assert(VarIs<RecyclableObject>(instance));
  6028. // The cache might have been invalidated between NewScObjectCommon and UpdateNewScObjectCache. This could occur, for example, if
  6029. // the constructor updates its own prototype property. If that happens we don't want to re-populate it here. A new cache will
  6030. // be created when the constructor is called again.
  6031. if (constructorCache->IsInvalidated())
  6032. {
  6033. #if DBG_DUMP
  6034. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because cache is invalidated"));
  6035. #endif
  6036. return;
  6037. }
  6038. Assert(constructorCache->GetGuardValueAsType() != nullptr);
  6039. if (DynamicType::Is(VarTo<RecyclableObject>(instance)->GetTypeId()))
  6040. {
  6041. DynamicObject *object = UnsafeVarTo<DynamicObject>(instance);
  6042. DynamicType* type = object->GetDynamicType();
  6043. DynamicTypeHandler* typeHandler = type->GetTypeHandler();
  6044. if (constructorBody->GetHasOnlyThisStmts())
  6045. {
  6046. if (!typeHandler->IsSharable())
  6047. {
  6048. // Dynamic type created is not sharable.
  6049. // So in future don't try to check for "this assignment optimization".
  6050. constructorBody->SetHasOnlyThisStmts(false);
  6051. #if DBG_DUMP
  6052. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because final type is not shareable"));
  6053. #endif
  6054. }
  6055. else if (typeHandler->GetPropertyCount() >= Js::PropertyIndexRanges<PropertyIndex>::MaxValue)
  6056. {
  6057. // Dynamic type created has too many properties.
  6058. // So in future don't try to check for "this assignment optimization".
  6059. constructorBody->SetHasOnlyThisStmts(false);
  6060. #if DBG_DUMP
  6061. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because final type has too many properties"));
  6062. #endif
  6063. }
  6064. else
  6065. {
  6066. #if DBG
  6067. bool cachedProtoCanBeCached = false;
  6068. Assert(type->GetPrototype() == JavascriptOperators::GetPrototypeObjectForConstructorCache(constructor, requestContext, cachedProtoCanBeCached));
  6069. Assert(cachedProtoCanBeCached);
  6070. Assert(type->GetScriptContext() == constructorCache->GetScriptContext());
  6071. Assert(type->GetPrototype() == constructorCache->GetType()->GetPrototype());
  6072. #endif
  6073. typeHandler->SetMayBecomeShared();
  6074. // CONSIDER: Remove only this for delayed type sharing.
  6075. type->ShareType();
  6076. #if ENABLE_PROFILE_INFO
  6077. DynamicProfileInfo* profileInfo = constructorBody->HasDynamicProfileInfo() ? constructorBody->GetAnyDynamicProfileInfo() : nullptr;
  6078. if ((profileInfo != nullptr && profileInfo->GetImplicitCallFlags() <= ImplicitCall_None) ||
  6079. CheckIfPrototypeChainHasOnlyWritableDataProperties(type->GetPrototype()))
  6080. {
  6081. for (PropertyIndex pi = 0; pi < typeHandler->GetPropertyCount(); pi++)
  6082. {
  6083. requestContext->RegisterConstructorCache(typeHandler->GetPropertyId(requestContext, pi), constructorCache);
  6084. }
  6085. Assert(constructorBody->GetUtf8SourceInfo()->GetIsLibraryCode() || !constructor->GetScriptContext()->IsScriptContextInDebugMode());
  6086. if (constructorCache->TryUpdateAfterConstructor(type, constructor->GetScriptContext()))
  6087. {
  6088. #if DBG_DUMP
  6089. TraceUpdateConstructorCache(constructorCache, constructorBody, true, _u(""));
  6090. #endif
  6091. }
  6092. else
  6093. {
  6094. #if DBG_DUMP
  6095. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because number of slots > MaxCachedSlotCount"));
  6096. #endif
  6097. }
  6098. }
  6099. #if DBG_DUMP
  6100. else
  6101. {
  6102. if (profileInfo &&
  6103. ((profileInfo->GetImplicitCallFlags() & ~(Js::ImplicitCall_External | Js::ImplicitCall_Accessor)) == 0) &&
  6104. profileInfo != nullptr && CheckIfPrototypeChainHasOnlyWritableDataProperties(type->GetPrototype()) &&
  6105. Js::Configuration::Global.flags.Trace.IsEnabled(Js::HostOptPhase))
  6106. {
  6107. const char16* ctorName = constructorBody->GetDisplayName();
  6108. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  6109. Output::Print(_u("CtorCache: %s cache (0x%p) for ctor %s (#%u) did not update because external call"),
  6110. constructorCache, constructorBody, ctorName, constructorBody ? constructorBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"));
  6111. Output::Print(_u("\n"));
  6112. Output::Flush();
  6113. }
  6114. }
  6115. #endif
  6116. #endif
  6117. }
  6118. }
  6119. else
  6120. {
  6121. #if DBG_DUMP
  6122. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because ctor has not only this statements"));
  6123. #endif
  6124. }
  6125. }
  6126. else
  6127. {
  6128. // Even though this constructor apparently returned something other than the default object we created,
  6129. // it still makes sense to cache the parameters of the default object, since we must create it every time, anyway.
  6130. #if DBG_DUMP
  6131. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because ctor return a non-object value"));
  6132. #endif
  6133. return;
  6134. }
  6135. // Whatever the constructor returned, if we're caching a type we want to be sure we shrink its inline slot capacity.
  6136. if (finalizeCachedType && constructorCache->IsEnabled())
  6137. {
  6138. DynamicType* cachedType = constructorCache->NeedsTypeUpdate() ? constructorCache->GetPendingType() : constructorCache->GetType();
  6139. DynamicTypeHandler* cachedTypeHandler = cachedType->GetTypeHandler();
  6140. // Consider: We could delay inline slot capacity shrinking until the second time this constructor is invoked. In some cases
  6141. // this might permit more properties to remain inlined if the objects grow after constructor. This would require flagging
  6142. // the cache as special (already possible) and forcing the shrinking during work item creation if we happen to JIT this
  6143. // constructor while the cache is in this special state.
  6144. if (cachedTypeHandler->GetInlineSlotCapacity())
  6145. {
  6146. #if DBG_DUMP
  6147. int inlineSlotCapacityBeforeShrink = cachedTypeHandler->GetInlineSlotCapacity();
  6148. #endif
  6149. // Note that after the cache has been updated and might have been used in the JIT-ed code, it is no longer legal to
  6150. // shrink the inline slot capacity of the type. That's because we allocate memory for a fixed number of inlined properties
  6151. // and if that number changed on the type, this update wouldn't get reflected in JIT-ed code and we would allocate objects
  6152. // of a wrong size. This could conceivably happen if the original object got collected, and with it some of the successor
  6153. // types also. If then another constructor has the same prototype and needs to populate its own cache, it would attempt to
  6154. // shrink inlined slots again. If all surviving type handlers have smaller inline slot capacity, we would shrink it further.
  6155. // To address this problem the type handler has a bit indicating its inline slots have been shrunk already. If that bit is
  6156. // set ShrinkSlotAndInlineSlotCapacity does nothing.
  6157. cachedTypeHandler->ShrinkSlotAndInlineSlotCapacity();
  6158. constructorCache->UpdateInlineSlotCount();
  6159. #if DBG_DUMP
  6160. Assert(inlineSlotCapacityBeforeShrink >= cachedTypeHandler->GetInlineSlotCapacity());
  6161. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::InlineSlotsPhase))
  6162. {
  6163. if (inlineSlotCapacityBeforeShrink != cachedTypeHandler->GetInlineSlotCapacity())
  6164. {
  6165. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  6166. Output::Print(_u("Inline slot capacity shrunk: Function:%04s Before:%d After:%d\n"),
  6167. constructorBody->GetDebugNumberSet(debugStringBuffer), inlineSlotCapacityBeforeShrink, cachedTypeHandler->GetInlineSlotCapacity());
  6168. }
  6169. }
  6170. #endif
  6171. }
  6172. }
  6173. JIT_HELPER_END(UpdateNewScObjectCache);
  6174. }
  6175. void JavascriptOperators::TraceUseConstructorCache(const ConstructorCache* ctorCache, const JavascriptFunction* ctor, bool isHit)
  6176. {
  6177. #if DBG_DUMP
  6178. // We are under debug, so we can incur the extra check here.
  6179. FunctionProxy* ctorBody = ctor->GetFunctionProxy();
  6180. if (ctorBody != nullptr && !ctorBody->GetScriptContext()->IsClosed())
  6181. {
  6182. ctorBody = ctorBody->EnsureDeserialized();
  6183. }
  6184. if ((ctorBody != nullptr && PHASE_TRACE(Js::ConstructorCachePhase, ctorBody)) || (ctorBody == nullptr && PHASE_TRACE1(Js::ConstructorCachePhase)))
  6185. {
  6186. const char16* ctorName = ctorBody != nullptr ? ctorBody->GetDisplayName() : _u("<unknown>");
  6187. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  6188. Output::Print(_u("CtorCache: %s cache (0x%p) for ctor %s (%s): "), isHit ? _u("hit") : _u("missed"), ctorCache, ctorName,
  6189. ctorBody ? ctorBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"));
  6190. ctorCache->Dump();
  6191. Output::Print(_u("\n"));
  6192. Output::Flush();
  6193. }
  6194. #endif
  6195. }
  6196. void JavascriptOperators::TraceUpdateConstructorCache(const ConstructorCache* ctorCache, const FunctionBody* ctorBody, bool updated, const char16* reason)
  6197. {
  6198. #if DBG_DUMP
  6199. if (PHASE_TRACE(Js::ConstructorCachePhase, ctorBody))
  6200. {
  6201. const char16* ctorName = ctorBody->GetDisplayName();
  6202. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  6203. Output::Print(_u("CtorCache: %s cache (0x%p) for ctor %s (%s)%s %s: "),
  6204. updated ? _u("updated") : _u("did not update"), ctorBody, ctorName,
  6205. ctorBody ? const_cast<Js::FunctionBody *>(ctorBody)->GetDebugNumberSet(debugStringBuffer) : _u("(null)"),
  6206. updated ? _u("") : _u(", because") , reason);
  6207. ctorCache->Dump();
  6208. Output::Print(_u("\n"));
  6209. Output::Flush();
  6210. }
  6211. #endif
  6212. }
  6213. Var JavascriptOperators::NewScObject(const Var callee, const Arguments args, ScriptContext *const scriptContext, const Js::AuxArray<uint32> *spreadIndices)
  6214. {
  6215. Assert(callee);
  6216. Assert(args.Info.Count != 0);
  6217. Assert(scriptContext);
  6218. // Always save and restore implicit call flags when calling out
  6219. // REVIEW: Can we avoid it if we don't collect dynamic profile info?
  6220. ThreadContext *const threadContext = scriptContext->GetThreadContext();
  6221. const ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  6222. const Var newVarInstance = JavascriptFunction::CallAsConstructor(callee, /* overridingNewTarget = */nullptr, args, scriptContext, spreadIndices);
  6223. threadContext->SetImplicitCallFlags(savedImplicitCallFlags);
  6224. return newVarInstance;
  6225. }
  6226. Js::GlobalObject * JavascriptOperators::OP_LdRoot(ScriptContext* scriptContext)
  6227. {
  6228. return scriptContext->GetGlobalObject();
  6229. }
  6230. Js::ModuleRoot * JavascriptOperators::GetModuleRoot(int moduleID, ScriptContext* scriptContext)
  6231. {
  6232. Assert(moduleID != kmodGlobal);
  6233. JavascriptLibrary* library = scriptContext->GetLibrary();
  6234. HostObjectBase *hostObject = library->GetGlobalObject()->GetHostObject();
  6235. if (hostObject)
  6236. {
  6237. Js::ModuleRoot * moduleRoot = hostObject->GetModuleRoot(moduleID);
  6238. Assert(!CrossSite::NeedMarshalVar(moduleRoot, scriptContext));
  6239. return moduleRoot;
  6240. }
  6241. HostScriptContext *hostScriptContext = scriptContext->GetHostScriptContext();
  6242. if (hostScriptContext)
  6243. {
  6244. Js::ModuleRoot * moduleRoot = hostScriptContext->GetModuleRoot(moduleID);
  6245. Assert(!CrossSite::NeedMarshalVar(moduleRoot, scriptContext));
  6246. return moduleRoot;
  6247. }
  6248. Assert(FALSE);
  6249. return nullptr;
  6250. }
  6251. Var JavascriptOperators::OP_LoadModuleRoot(int moduleID, ScriptContext* scriptContext)
  6252. {
  6253. Js::ModuleRoot * moduleRoot = GetModuleRoot(moduleID, scriptContext);
  6254. if (moduleRoot)
  6255. {
  6256. return moduleRoot;
  6257. }
  6258. Assert(false);
  6259. return scriptContext->GetLibrary()->GetUndefined();
  6260. }
  6261. Var JavascriptOperators::OP_LdNull(ScriptContext* scriptContext)
  6262. {
  6263. return scriptContext->GetLibrary()->GetNull();
  6264. }
  6265. Var JavascriptOperators::OP_LdUndef(ScriptContext* scriptContext)
  6266. {
  6267. return scriptContext->GetLibrary()->GetUndefined();
  6268. }
  6269. Var JavascriptOperators::OP_LdNaN(ScriptContext* scriptContext)
  6270. {
  6271. return scriptContext->GetLibrary()->GetNaN();
  6272. }
  6273. Var JavascriptOperators::OP_LdChakraLib(ScriptContext* scriptContext)
  6274. {
  6275. return scriptContext->GetLibrary()->GetChakraLib();
  6276. }
  6277. Var JavascriptOperators::OP_LdInfinity(ScriptContext* scriptContext)
  6278. {
  6279. return scriptContext->GetLibrary()->GetPositiveInfinite();
  6280. }
  6281. void JavascriptOperators::BuildHandlerScope(Var argThis, RecyclableObject * hostObject, FrameDisplay * pDisplay, ScriptContext * scriptContext)
  6282. {
  6283. // Event handlers need implicit lookups of @@unscopables on parent scopes.
  6284. // We can intercept the property accesses by wrapping the object with the unscopables handler.
  6285. // WebIDL: https://heycam.github.io/webidl/#ref-for-Unscopable
  6286. Assert(argThis != nullptr);
  6287. pDisplay->SetItem(0, TaggedNumber::Is(argThis) ? scriptContext->GetLibrary()->CreateNumberObject(argThis) : ToUnscopablesWrapperObject(argThis, scriptContext));
  6288. uint16 i = 1;
  6289. Var aChild = argThis;
  6290. uint16 length = pDisplay->GetLength();
  6291. // Now add any parent scopes
  6292. // We need to support the namespace parent lookup in both fastDOM on and off scenario.
  6293. while (aChild != NULL)
  6294. {
  6295. Var aParent = hostObject->GetNamespaceParent(aChild);
  6296. if (aParent == nullptr)
  6297. {
  6298. break;
  6299. }
  6300. aParent = CrossSite::MarshalVar(scriptContext, aParent);
  6301. if (i == length)
  6302. {
  6303. length = UInt16Math::Add(length, 8);
  6304. FrameDisplay * tmp = RecyclerNewPlus(scriptContext->GetRecycler(), length * sizeof(void*), FrameDisplay, length);
  6305. js_memcpy_s((char*)tmp + tmp->GetOffsetOfScopes(), tmp->GetLength() * sizeof(void *), (char*)pDisplay + pDisplay->GetOffsetOfScopes(), pDisplay->GetLength() * sizeof(void*));
  6306. pDisplay = tmp;
  6307. }
  6308. Var aParentWrapped = ToUnscopablesWrapperObject(aParent, scriptContext);
  6309. pDisplay->SetItem(i, aParentWrapped);
  6310. aChild = aParent;
  6311. i++;
  6312. }
  6313. Assert(i <= pDisplay->GetLength());
  6314. pDisplay->SetLength(i);
  6315. }
  6316. FrameDisplay * JavascriptOperators::OP_LdHandlerScope(Var argThis, ScriptContext* scriptContext)
  6317. {
  6318. JIT_HELPER_REENTRANT_HEADER(ScrObj_LdHandlerScope);
  6319. // The idea here is to build a stack of nested scopes in the form of a JS array.
  6320. //
  6321. // The scope stack for an event handler looks like this:
  6322. //
  6323. // implicit "this"
  6324. // implicit namespace parent scopes
  6325. // Put the implicit "this"
  6326. if (argThis != NULL)
  6327. {
  6328. RecyclableObject* hostObject = scriptContext->GetGlobalObject()->GetHostObject();
  6329. if (hostObject == nullptr)
  6330. {
  6331. hostObject = scriptContext->GetGlobalObject()->GetDirectHostObject();
  6332. }
  6333. if (hostObject != nullptr)
  6334. {
  6335. uint16 length = 7;
  6336. FrameDisplay *pDisplay =
  6337. RecyclerNewPlus(scriptContext->GetRecycler(), length * sizeof(void*), FrameDisplay, length);
  6338. BuildHandlerScope(argThis, hostObject, pDisplay, scriptContext);
  6339. return pDisplay;
  6340. }
  6341. }
  6342. return const_cast<FrameDisplay *>(&Js::NullFrameDisplay);
  6343. JIT_HELPER_END(ScrObj_LdHandlerScope);
  6344. }
  6345. FrameDisplay* JavascriptOperators::OP_LdFrameDisplay(void *argHead, void *argEnv, ScriptContext* scriptContext)
  6346. {
  6347. JIT_HELPER_NOT_REENTRANT_HEADER(ScrObj_LdFrameDisplay, reentrancylock, scriptContext->GetThreadContext());
  6348. // Build a display of nested frame objects.
  6349. // argHead is the current scope; argEnv is either the lone trailing scope or an array of scopes
  6350. // which we append to the new display.
  6351. // Note that there are cases in which a function with no local frame must construct a display to pass
  6352. // to the function(s) nested within it. In such a case, argHead will be a null object, and it's not
  6353. // strictly necessary to include it. But such cases are rare and not perf critical, so it's not
  6354. // worth the extra complexity to notify the nested functions that they can "skip" this slot in the
  6355. // frame display when they're loading scopes nested outside it.
  6356. FrameDisplay *pDisplay = nullptr;
  6357. FrameDisplay *envDisplay = (FrameDisplay*)argEnv;
  6358. uint16 length = UInt16Math::Add(envDisplay->GetLength(), 1);
  6359. pDisplay = RecyclerNewPlus(scriptContext->GetRecycler(), length * sizeof(void*), FrameDisplay, length);
  6360. for (uint16 j = 0; j < length - 1; j++)
  6361. {
  6362. pDisplay->SetItem(j + 1, envDisplay->GetItem(j));
  6363. }
  6364. pDisplay->SetItem(0, argHead);
  6365. return pDisplay;
  6366. JIT_HELPER_END(ScrObj_LdFrameDisplay);
  6367. }
  6368. FrameDisplay* JavascriptOperators::OP_LdFrameDisplayNoParent(void *argHead, ScriptContext* scriptContext)
  6369. {
  6370. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdFrameDisplayNoParent);
  6371. return OP_LdFrameDisplay(argHead, (void*)&NullFrameDisplay, scriptContext);
  6372. JIT_HELPER_END(ScrObj_LdFrameDisplayNoParent);
  6373. }
  6374. FrameDisplay* JavascriptOperators::OP_LdStrictFrameDisplay(void *argHead, void *argEnv, ScriptContext* scriptContext)
  6375. {
  6376. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdStrictFrameDisplay);
  6377. FrameDisplay * pDisplay = OP_LdFrameDisplay(argHead, argEnv, scriptContext);
  6378. pDisplay->SetStrictMode(true);
  6379. return pDisplay;
  6380. JIT_HELPER_END(ScrObj_LdStrictFrameDisplay);
  6381. }
  6382. FrameDisplay* JavascriptOperators::OP_LdStrictFrameDisplayNoParent(void *argHead, ScriptContext* scriptContext)
  6383. {
  6384. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdStrictFrameDisplayNoParent);
  6385. return OP_LdStrictFrameDisplay(argHead, (void*)&StrictNullFrameDisplay, scriptContext);
  6386. JIT_HELPER_END(ScrObj_LdStrictFrameDisplayNoParent);
  6387. }
  6388. FrameDisplay* JavascriptOperators::OP_LdInnerFrameDisplay(void *argHead, void *argEnv, ScriptContext* scriptContext)
  6389. {
  6390. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdInnerFrameDisplay);
  6391. CheckInnerFrameDisplayArgument(argHead);
  6392. return OP_LdFrameDisplay(argHead, argEnv, scriptContext);
  6393. JIT_HELPER_END(ScrObj_LdInnerFrameDisplay);
  6394. }
  6395. FrameDisplay* JavascriptOperators::OP_LdInnerFrameDisplayNoParent(void *argHead, ScriptContext* scriptContext)
  6396. {
  6397. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdInnerFrameDisplayNoParent);
  6398. CheckInnerFrameDisplayArgument(argHead);
  6399. return OP_LdFrameDisplayNoParent(argHead, scriptContext);
  6400. JIT_HELPER_END(ScrObj_LdInnerFrameDisplayNoParent);
  6401. }
  6402. FrameDisplay* JavascriptOperators::OP_LdStrictInnerFrameDisplay(void *argHead, void *argEnv, ScriptContext* scriptContext)
  6403. {
  6404. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdStrictInnerFrameDisplay);
  6405. CheckInnerFrameDisplayArgument(argHead);
  6406. return OP_LdStrictFrameDisplay(argHead, argEnv, scriptContext);
  6407. JIT_HELPER_END(ScrObj_LdStrictInnerFrameDisplay);
  6408. }
  6409. FrameDisplay* JavascriptOperators::OP_LdStrictInnerFrameDisplayNoParent(void *argHead, ScriptContext* scriptContext)
  6410. {
  6411. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdStrictInnerFrameDisplayNoParent);
  6412. CheckInnerFrameDisplayArgument(argHead);
  6413. return OP_LdStrictFrameDisplayNoParent(argHead, scriptContext);
  6414. JIT_HELPER_END(ScrObj_LdStrictInnerFrameDisplayNoParent);
  6415. }
  6416. void JavascriptOperators::CheckInnerFrameDisplayArgument(void *argHead)
  6417. {
  6418. if (ThreadContext::IsOnStack(argHead))
  6419. {
  6420. AssertMsg(false, "Illegal byte code: stack object as with scope");
  6421. Js::Throw::FatalInternalError();
  6422. }
  6423. if (!VarIs<RecyclableObject>(argHead))
  6424. {
  6425. AssertMsg(false, "Illegal byte code: non-object as with scope");
  6426. Js::Throw::FatalInternalError();
  6427. }
  6428. }
  6429. Js::PropertyId JavascriptOperators::GetPropertyId(Var propertyName, ScriptContext* scriptContext)
  6430. {
  6431. PropertyRecord const * propertyRecord = nullptr;
  6432. JavascriptSymbol * symbol = JavascriptOperators::TryFromVar<Js::JavascriptSymbol>(propertyName);
  6433. if (symbol)
  6434. {
  6435. propertyRecord = symbol->GetValue();
  6436. }
  6437. else
  6438. {
  6439. JavascriptSymbolObject * symbolObject = JavascriptOperators::TryFromVar<JavascriptSymbolObject>(propertyName);
  6440. if (symbolObject)
  6441. {
  6442. propertyRecord = symbolObject->GetValue();
  6443. }
  6444. else
  6445. {
  6446. JavascriptString * indexStr = JavascriptConversion::ToString(propertyName, scriptContext);
  6447. scriptContext->GetOrAddPropertyRecord(indexStr, &propertyRecord);
  6448. }
  6449. }
  6450. return propertyRecord->GetPropertyId();
  6451. }
  6452. void JavascriptOperators::OP_InitSetter(Var object, PropertyId propertyId, Var setter)
  6453. {
  6454. AssertMsg(!TaggedNumber::Is(object), "SetMember on a non-object?");
  6455. RecyclableObject* recylableObject = VarTo<RecyclableObject>(object);
  6456. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitSetter, reentrancylock, recylableObject->GetScriptContext()->GetThreadContext());
  6457. recylableObject->SetAccessors(propertyId, nullptr, setter);
  6458. JIT_HELPER_END(OP_InitSetter);
  6459. }
  6460. void JavascriptOperators::OP_InitClassMemberSet(Var object, PropertyId propertyId, Var setter)
  6461. {
  6462. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_InitClassMemberSet);
  6463. JIT_HELPER_SAME_ATTRIBUTES(Op_InitClassMemberSet, OP_InitSetter);
  6464. JavascriptOperators::OP_InitSetter(object, propertyId, setter);
  6465. VarTo<RecyclableObject>(object)->SetAttributes(propertyId, PropertyClassMemberDefaults);
  6466. JIT_HELPER_END(Op_InitClassMemberSet);
  6467. }
  6468. Js::PropertyId JavascriptOperators::OP_InitElemSetter(Var object, Var elementName, Var setter, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6469. {
  6470. JIT_HELPER_REENTRANT_HEADER(OP_InitElemSetter);
  6471. AssertMsg(!TaggedNumber::Is(object), "SetMember on a non-object?");
  6472. PropertyId propertyId = JavascriptOperators::GetPropertyId(elementName, scriptContext);
  6473. VarTo<RecyclableObject>(object)->SetAccessors(propertyId, nullptr, setter);
  6474. return propertyId;
  6475. JIT_HELPER_END(OP_InitElemSetter);
  6476. }
  6477. Field(Var)* JavascriptOperators::OP_GetModuleExportSlotArrayAddress(uint moduleIndex, uint slotIndex, ScriptContextInfo* scriptContext)
  6478. {
  6479. return scriptContext->GetModuleExportSlotArrayAddress(moduleIndex, slotIndex);
  6480. }
  6481. Field(Var)* JavascriptOperators::OP_GetModuleExportSlotAddress(uint moduleIndex, uint slotIndex, ScriptContext* scriptContext)
  6482. {
  6483. Field(Var)* moduleRecordSlots = OP_GetModuleExportSlotArrayAddress(moduleIndex, slotIndex, scriptContext);
  6484. Assert(moduleRecordSlots != nullptr);
  6485. return &moduleRecordSlots[slotIndex];
  6486. }
  6487. Var JavascriptOperators::OP_LdModuleSlot(uint moduleIndex, uint slotIndex, ScriptContext* scriptContext)
  6488. {
  6489. Field(Var)* addr = OP_GetModuleExportSlotAddress(moduleIndex, slotIndex, scriptContext);
  6490. Assert(addr != nullptr);
  6491. return *addr;
  6492. }
  6493. void JavascriptOperators::OP_StModuleSlot(uint moduleIndex, uint slotIndex, Var value, ScriptContext* scriptContext)
  6494. {
  6495. Assert(value != nullptr);
  6496. Field(Var)* addr = OP_GetModuleExportSlotAddress(moduleIndex, slotIndex, scriptContext);
  6497. Assert(addr != nullptr);
  6498. *addr = value;
  6499. }
  6500. Var JavascriptOperators::OP_LdImportMeta(uint moduleIndex, ScriptContext* scriptContext)
  6501. {
  6502. JIT_HELPER_REENTRANT_HEADER(LdImportMeta);
  6503. return scriptContext->GetLibrary()->GetModuleRecord(moduleIndex)->GetImportMetaObject();
  6504. JIT_HELPER_END(LdImportMeta);
  6505. }
  6506. void JavascriptOperators::OP_InitClassMemberSetComputedName(Var object, Var elementName, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6507. {
  6508. JIT_HELPER_REENTRANT_HEADER(Op_InitClassMemberSetComputedName);
  6509. Js::PropertyId propertyId = JavascriptOperators::OP_InitElemSetter(object, elementName, value, scriptContext);
  6510. RecyclableObject* instance = VarTo<RecyclableObject>(object);
  6511. // instance will be a function if it is the class constructor (otherwise it would be an object)
  6512. if (VarIs<JavascriptFunction>(instance) && Js::PropertyIds::prototype == propertyId)
  6513. {
  6514. // It is a TypeError to have a static member with a computed name that evaluates to 'prototype'
  6515. JavascriptError::ThrowTypeError(scriptContext, JSERR_ClassStaticMethodCannotBePrototype);
  6516. }
  6517. instance->SetAttributes(propertyId, PropertyClassMemberDefaults);
  6518. JIT_HELPER_END(Op_InitClassMemberSetComputedName);
  6519. }
  6520. BOOL JavascriptOperators::IsClassConstructor(Var instance)
  6521. {
  6522. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_IsClassConstructor);
  6523. JavascriptFunction * function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  6524. return function && (function->GetFunctionInfo()->IsClassConstructor() || (!function->IsScriptFunction() && !function->IsExternalFunction()));
  6525. JIT_HELPER_END(Op_IsClassConstructor);
  6526. }
  6527. BOOL JavascriptOperators::IsClassMethod(Var instance)
  6528. {
  6529. JavascriptFunction * function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  6530. return function && function->GetFunctionInfo()->IsClassMethod();
  6531. }
  6532. BOOL JavascriptOperators::IsBaseConstructorKind(Var instance)
  6533. {
  6534. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_IsBaseConstructorKind);
  6535. JavascriptFunction * function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  6536. return function && (function->GetFunctionInfo()->GetBaseConstructorKind());
  6537. JIT_HELPER_END(Op_IsBaseConstructorKind);
  6538. }
  6539. void JavascriptOperators::OP_InitGetter(Var object, PropertyId propertyId, Var getter)
  6540. {
  6541. AssertMsg(!TaggedNumber::Is(object), "GetMember on a non-object?");
  6542. RecyclableObject* recylableObject = VarTo<RecyclableObject>(object);
  6543. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitGetter, reentrancylock, recylableObject->GetScriptContext()->GetThreadContext());
  6544. recylableObject->SetAccessors(propertyId, getter, nullptr);
  6545. JIT_HELPER_END(OP_InitGetter);
  6546. }
  6547. void JavascriptOperators::OP_InitClassMemberGet(Var object, PropertyId propertyId, Var getter)
  6548. {
  6549. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_InitClassMemberGet);
  6550. JIT_HELPER_SAME_ATTRIBUTES(Op_InitClassMemberGet, OP_InitGetter);
  6551. JavascriptOperators::OP_InitGetter(object, propertyId, getter);
  6552. VarTo<RecyclableObject>(object)->SetAttributes(propertyId, PropertyClassMemberDefaults);
  6553. JIT_HELPER_END(Op_InitClassMemberGet);
  6554. }
  6555. Js::PropertyId JavascriptOperators::OP_InitElemGetter(Var object, Var elementName, Var getter, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6556. {
  6557. JIT_HELPER_REENTRANT_HEADER(OP_InitElemGetter);
  6558. AssertMsg(!TaggedNumber::Is(object), "GetMember on a non-object?");
  6559. PropertyId propertyId = JavascriptOperators::GetPropertyId(elementName, scriptContext);
  6560. VarTo<RecyclableObject>(object)->SetAccessors(propertyId, getter, nullptr);
  6561. return propertyId;
  6562. JIT_HELPER_END(OP_InitElemGetter);
  6563. }
  6564. void JavascriptOperators::OP_InitClassMemberGetComputedName(Var object, Var elementName, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6565. {
  6566. JIT_HELPER_REENTRANT_HEADER(Op_InitClassMemberGetComputedName);
  6567. Js::PropertyId propertyId = JavascriptOperators::OP_InitElemGetter(object, elementName, value, scriptContext);
  6568. RecyclableObject* instance = VarTo<RecyclableObject>(object);
  6569. // instance will be a function if it is the class constructor (otherwise it would be an object)
  6570. if (VarIs<JavascriptFunction>(instance) && Js::PropertyIds::prototype == propertyId)
  6571. {
  6572. // It is a TypeError to have a static member with a computed name that evaluates to 'prototype'
  6573. JavascriptError::ThrowTypeError(scriptContext, JSERR_ClassStaticMethodCannotBePrototype);
  6574. }
  6575. instance->SetAttributes(propertyId, PropertyClassMemberDefaults);
  6576. JIT_HELPER_END(Op_InitClassMemberGetComputedName);
  6577. }
  6578. void JavascriptOperators::OP_InitComputedProperty(Var object, Var elementName, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6579. {
  6580. JIT_HELPER_REENTRANT_HEADER(OP_InitComputedProperty);
  6581. PropertyId propertyId = JavascriptOperators::GetPropertyId(elementName, scriptContext);
  6582. VarTo<RecyclableObject>(object)->InitProperty(propertyId, value, flags);
  6583. JIT_HELPER_END(OP_InitComputedProperty);
  6584. }
  6585. void JavascriptOperators::OP_InitClassMemberComputedName(Var object, Var elementName, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6586. {
  6587. JIT_HELPER_REENTRANT_HEADER(Op_InitClassMemberComputedName);
  6588. PropertyId propertyId = JavascriptOperators::GetPropertyId(elementName, scriptContext);
  6589. RecyclableObject* instance = VarTo<RecyclableObject>(object);
  6590. // instance will be a function if it is the class constructor (otherwise it would be an object)
  6591. if (VarIs<JavascriptFunction>(instance) && Js::PropertyIds::prototype == propertyId)
  6592. {
  6593. // It is a TypeError to have a static member with a computed name that evaluates to 'prototype'
  6594. JavascriptError::ThrowTypeError(scriptContext, JSERR_ClassStaticMethodCannotBePrototype);
  6595. }
  6596. instance->SetPropertyWithAttributes(propertyId, value, PropertyClassMemberDefaults, NULL, flags);
  6597. JIT_HELPER_END(Op_InitClassMemberComputedName);
  6598. }
  6599. //
  6600. // Used by object literal {..., __proto__: ..., }.
  6601. //
  6602. void JavascriptOperators::OP_InitProto(Var instance, PropertyId propertyId, Var value)
  6603. {
  6604. AssertMsg(VarIs<RecyclableObject>(instance), "__proto__ member on a non-object?");
  6605. Assert(propertyId == PropertyIds::__proto__);
  6606. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  6607. ScriptContext* scriptContext = object->GetScriptContext();
  6608. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitProto, reentrancylock, scriptContext->GetThreadContext());
  6609. // B.3.1 __proto___ Property Names in Object Initializers
  6610. //6.If propKey is the string value "__proto__" and if isComputedPropertyName(propKey) is false, then
  6611. // a.If Type(v) is either Object or Null, then
  6612. // i.Return the result of calling the [[SetInheritance]] internal method of object with argument propValue.
  6613. // b.Return NormalCompletion(empty).
  6614. if (JavascriptOperators::IsObjectOrNull(value))
  6615. {
  6616. JavascriptObject::ChangePrototype(object, VarTo<RecyclableObject>(value), /*validate*/false, scriptContext);
  6617. }
  6618. JIT_HELPER_END(OP_InitProto);
  6619. }
  6620. Var JavascriptOperators::ConvertToUnmappedArguments(HeapArgumentsObject *argumentsObject,
  6621. uint32 paramCount,
  6622. Var *paramAddr,
  6623. DynamicObject* frameObject,
  6624. Js::PropertyIdArray *propIds,
  6625. uint32 formalsCount,
  6626. ScriptContext* scriptContext)
  6627. {
  6628. Var *paramIter = paramAddr;
  6629. uint32 i = 0;
  6630. for (paramIter = paramAddr + i; i < paramCount; i++, paramIter++)
  6631. {
  6632. JavascriptOperators::SetItem(argumentsObject, argumentsObject, i, *paramIter, scriptContext, PropertyOperation_None, /* skipPrototypeCheck = */ TRUE);
  6633. }
  6634. argumentsObject = argumentsObject->ConvertToUnmappedArgumentsObject();
  6635. // Now as the unmapping is done we need to fill those frame object with Undecl
  6636. for (i = 0; i < formalsCount; i++)
  6637. {
  6638. frameObject->SetSlot(SetSlotArguments(propIds != nullptr ? propIds->elements[i] : Js::Constants::NoProperty, i, scriptContext->GetLibrary()->GetUndeclBlockVar()));
  6639. }
  6640. return argumentsObject;
  6641. }
  6642. Var JavascriptOperators::LoadHeapArguments(JavascriptFunction *funcCallee, uint32 actualsCount, Var *paramAddr, Var frameObj, Var vArray, ScriptContext* scriptContext, bool nonSimpleParamList)
  6643. {
  6644. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LoadHeapArguments, reentrancylock, scriptContext->GetThreadContext());
  6645. AssertMsg(actualsCount != (unsigned int)-1, "Loading the arguments object in the global function?");
  6646. // Create and initialize the Arguments object.
  6647. uint32 formalsCount = 0;
  6648. Js::PropertyIdArray *propIds = nullptr;
  6649. if (vArray != scriptContext->GetLibrary()->GetNull())
  6650. {
  6651. propIds = (Js::PropertyIdArray *)vArray;
  6652. formalsCount = propIds->count;
  6653. Assert(formalsCount != 0 && propIds != nullptr);
  6654. }
  6655. HeapArgumentsObject *argsObj = JavascriptOperators::CreateHeapArguments(funcCallee, actualsCount, formalsCount, frameObj, scriptContext);
  6656. return FillScopeObject(funcCallee, actualsCount, formalsCount, frameObj, paramAddr, propIds, argsObj, scriptContext, nonSimpleParamList, false);
  6657. JIT_HELPER_END(Op_LoadHeapArguments);
  6658. }
  6659. Var JavascriptOperators::LoadHeapArgsCached(JavascriptFunction *funcCallee, uint32 actualsCount, uint32 formalsCount, Var *paramAddr, Var frameObj, ScriptContext* scriptContext, bool nonSimpleParamList)
  6660. {
  6661. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LoadHeapArgsCached, reentrancylock, scriptContext->GetThreadContext());
  6662. // Disregard the "this" param.
  6663. AssertMsg(actualsCount != (uint32)-1 && formalsCount != (uint32)-1,
  6664. "Loading the arguments object in the global function?");
  6665. HeapArgumentsObject *argsObj = JavascriptOperators::CreateHeapArguments(funcCallee, actualsCount, formalsCount, frameObj, scriptContext);
  6666. return FillScopeObject(funcCallee, actualsCount, formalsCount, frameObj, paramAddr, nullptr, argsObj, scriptContext, nonSimpleParamList, true);
  6667. JIT_HELPER_END(Op_LoadHeapArgsCached);
  6668. }
  6669. Var JavascriptOperators::FillScopeObject(JavascriptFunction *funcCallee, uint32 actualsCount, uint32 formalsCount, Var frameObj, Var * paramAddr,
  6670. Js::PropertyIdArray *propIds, HeapArgumentsObject * argsObj, ScriptContext * scriptContext, bool nonSimpleParamList, bool useCachedScope)
  6671. {
  6672. Assert(formalsCount == 0 || frameObj != nullptr);
  6673. // Transfer formal arguments (that were actually passed) from their ArgIn slots to the local frame object.
  6674. uint32 i;
  6675. Var *tmpAddr = paramAddr;
  6676. if (formalsCount != 0)
  6677. {
  6678. DynamicObject* frameObject = nullptr;
  6679. if (useCachedScope)
  6680. {
  6681. frameObject = VarTo<DynamicObject>(frameObj);
  6682. __analysis_assume((uint32)frameObject->GetDynamicType()->GetTypeHandler()->GetSlotCapacity() >= formalsCount);
  6683. }
  6684. else
  6685. {
  6686. frameObject = (DynamicObject*)frameObj;
  6687. // No fixed fields for formal parameters of the arguments object. Also, mark all fields as initialized up-front, because
  6688. // we will set them directly using SetSlot below, so the type handler will not have a chance to mark them as initialized later.
  6689. // CONSIDER : When we delay type sharing until the second instance is created, pass an argument indicating we want the types
  6690. // and handlers created here to be marked as shared up-front. This is to ensure we don't get any fixed fields and that the handler
  6691. // is ready for storing values directly to slots.
  6692. DynamicType* newType = nullptr;
  6693. if (nonSimpleParamList)
  6694. {
  6695. bool skipLetAttrForArguments = ((JavascriptGeneratorFunction::IsBaseGeneratorFunction(funcCallee) || VarIs<JavascriptAsyncFunction>(funcCallee)) ?
  6696. VarTo<JavascriptGeneratorFunction>(funcCallee)->GetGeneratorVirtualScriptFunction()->GetFunctionBody()->HasReferenceableBuiltInArguments()
  6697. : funcCallee->GetFunctionBody()->HasReferenceableBuiltInArguments());
  6698. if (skipLetAttrForArguments)
  6699. {
  6700. newType = PathTypeHandlerBase::CreateNewScopeObject<true>(scriptContext, frameObject->GetDynamicType(), propIds, PropertyLetDefaults);
  6701. }
  6702. else
  6703. {
  6704. newType = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, frameObject->GetDynamicType(), propIds, PropertyLetDefaults);
  6705. }
  6706. }
  6707. else
  6708. {
  6709. newType = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, frameObject->GetDynamicType(), propIds);
  6710. }
  6711. int oldSlotCapacity = frameObject->GetDynamicType()->GetTypeHandler()->GetSlotCapacity();
  6712. int newSlotCapacity = newType->GetTypeHandler()->GetSlotCapacity();
  6713. __analysis_assume((uint32)newSlotCapacity >= formalsCount);
  6714. frameObject->EnsureSlots(oldSlotCapacity, newSlotCapacity, scriptContext, newType->GetTypeHandler());
  6715. frameObject->ReplaceType(newType);
  6716. }
  6717. if (argsObj && nonSimpleParamList)
  6718. {
  6719. return ConvertToUnmappedArguments(argsObj, actualsCount, paramAddr, frameObject, propIds, formalsCount, scriptContext);
  6720. }
  6721. for (i = 0; i < formalsCount && i < actualsCount; i++, tmpAddr++)
  6722. {
  6723. frameObject->SetSlot(SetSlotArguments(propIds != nullptr? propIds->elements[i] : Constants::NoProperty, i, *tmpAddr));
  6724. }
  6725. if (i < formalsCount)
  6726. {
  6727. // The formals that weren't passed still need to be put in the frame object so that
  6728. // their names will be found. Initialize them to "undefined".
  6729. for (; i < formalsCount; i++)
  6730. {
  6731. frameObject->SetSlot(SetSlotArguments(propIds != nullptr? propIds->elements[i] : Constants::NoProperty, i, scriptContext->GetLibrary()->GetUndefined()));
  6732. }
  6733. }
  6734. }
  6735. if (argsObj != nullptr)
  6736. {
  6737. // Transfer the unnamed actual arguments, if any, to the Arguments object itself.
  6738. for (i = formalsCount, tmpAddr = paramAddr + i; i < actualsCount; i++, tmpAddr++)
  6739. {
  6740. // ES5 10.6.11: use [[DefineOwnProperty]] semantics (instead of [[Put]]):
  6741. // do not check whether property is non-writable/etc in the prototype.
  6742. // ES3 semantics is same.
  6743. JavascriptOperators::SetItem(argsObj, argsObj, i, *tmpAddr, scriptContext, PropertyOperation_None, /* skipPrototypeCheck = */ TRUE);
  6744. }
  6745. if (funcCallee->IsStrictMode())
  6746. {
  6747. // If the formals are let decls, then we just overwrote the frame object slots with
  6748. // Undecl sentinels, and we can use the original arguments that were passed to the HeapArgumentsObject.
  6749. return argsObj->ConvertToUnmappedArgumentsObject(!nonSimpleParamList);
  6750. }
  6751. }
  6752. return argsObj;
  6753. }
  6754. HeapArgumentsObject *JavascriptOperators::CreateHeapArguments(JavascriptFunction *funcCallee, uint32 actualsCount, uint32 formalsCount, Var frameObj, ScriptContext* scriptContext)
  6755. {
  6756. JavascriptLibrary *library = scriptContext->GetLibrary();
  6757. HeapArgumentsObject *argsObj = library->CreateHeapArguments(frameObj, formalsCount, !!funcCallee->IsStrictMode());
  6758. #if DBG
  6759. DynamicTypeHandler* typeHandler = argsObj->GetTypeHandler();
  6760. #endif
  6761. //
  6762. // Set the number of arguments of Arguments Object
  6763. //
  6764. argsObj->SetNumberOfArguments(actualsCount);
  6765. JavascriptOperators::SetProperty(argsObj, argsObj, PropertyIds::length, JavascriptNumber::ToVar(actualsCount, scriptContext), scriptContext);
  6766. JavascriptOperators::SetProperty(argsObj, argsObj, PropertyIds::_symbolIterator, library->EnsureArrayPrototypeValuesFunction(), scriptContext);
  6767. if (funcCallee->IsStrictMode())
  6768. {
  6769. JavascriptFunction* restrictedPropertyAccessor = library->GetThrowTypeErrorRestrictedPropertyAccessorFunction();
  6770. argsObj->SetAccessors(PropertyIds::callee, restrictedPropertyAccessor, restrictedPropertyAccessor, PropertyOperation_NonFixedValue);
  6771. }
  6772. else
  6773. {
  6774. JavascriptOperators::SetProperty(argsObj, argsObj, PropertyIds::callee,
  6775. StackScriptFunction::EnsureBoxed(BOX_PARAM(funcCallee, nullptr, _u("callee"))), scriptContext);
  6776. }
  6777. AssertMsg(argsObj->GetTypeHandler() == typeHandler || scriptContext->IsScriptContextInDebugMode(), "type handler should not transition because we initialized it correctly");
  6778. return argsObj;
  6779. }
  6780. Var JavascriptOperators::OP_NewScopeObject(ScriptContext* scriptContext)
  6781. {
  6782. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_NewScopeObject);
  6783. return scriptContext->GetLibrary()->CreateActivationObject();
  6784. JIT_HELPER_END(OP_NewScopeObject);
  6785. }
  6786. Var JavascriptOperators::OP_NewScopeObjectWithFormals(ScriptContext* scriptContext, FunctionBody * calleeBody, bool nonSimpleParamList)
  6787. {
  6788. JIT_HELPER_NOT_REENTRANT_HEADER(OP_NewScopeObjectWithFormals, reentrancylock, scriptContext->GetThreadContext());
  6789. Js::ActivationObject * frameObject = (ActivationObject*)OP_NewScopeObject(scriptContext);
  6790. // No fixed fields for formal parameters of the arguments object. Also, mark all fields as initialized up-front, because
  6791. // we will set them directly using SetSlot below, so the type handler will not have a chance to mark them as initialized later.
  6792. // CONSIDER : When we delay type sharing until the second instance is created, pass an argument indicating we want the types
  6793. // and handlers created here to be marked as shared up-front. This is to ensure we don't get any fixed fields and that the handler
  6794. // is ready for storing values directly to slots.
  6795. DynamicType* newType = nullptr;
  6796. if (nonSimpleParamList)
  6797. {
  6798. if (calleeBody->HasReferenceableBuiltInArguments())
  6799. {
  6800. newType = PathTypeHandlerBase::CreateNewScopeObject<true>(scriptContext, frameObject->GetDynamicType(), calleeBody->GetFormalsPropIdArray(), PropertyLetDefaults);
  6801. }
  6802. else
  6803. {
  6804. newType = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, frameObject->GetDynamicType(), calleeBody->GetFormalsPropIdArray(), PropertyLetDefaults);
  6805. }
  6806. }
  6807. else
  6808. {
  6809. newType = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, frameObject->GetDynamicType(), calleeBody->GetFormalsPropIdArray());
  6810. }
  6811. int oldSlotCapacity = frameObject->GetDynamicType()->GetTypeHandler()->GetSlotCapacity();
  6812. int newSlotCapacity = newType->GetTypeHandler()->GetSlotCapacity();
  6813. frameObject->EnsureSlots(oldSlotCapacity, newSlotCapacity, scriptContext, newType->GetTypeHandler());
  6814. frameObject->ReplaceType(newType);
  6815. return frameObject;
  6816. JIT_HELPER_END(OP_NewScopeObjectWithFormals);
  6817. }
  6818. Field(Var)* JavascriptOperators::OP_NewScopeSlots(unsigned int size, ScriptContext *scriptContext, Var scope)
  6819. {
  6820. JIT_HELPER_NOT_REENTRANT_HEADER(OP_NewScopeSlots, reentrancylock, scriptContext->GetThreadContext());
  6821. Assert(size > ScopeSlots::FirstSlotIndex); // Should never see empty slot array
  6822. Field(Var)* slotArray = RecyclerNewArray(scriptContext->GetRecycler(), Field(Var), size); // last initialized slot contains reference to array of propertyIds, correspondent to objects in previous slots
  6823. uint count = size - ScopeSlots::FirstSlotIndex;
  6824. ScopeSlots slots(slotArray);
  6825. slots.SetCount(count);
  6826. AssertMsg(!FunctionBody::Is(scope), "Scope should only be FunctionInfo or DebuggerScope, not FunctionBody");
  6827. slots.SetScopeMetadata(scope);
  6828. Var undef = scriptContext->GetLibrary()->GetUndefined();
  6829. for (unsigned int i = 0; i < count; i++)
  6830. {
  6831. slots.Set(i, undef);
  6832. }
  6833. return slotArray;
  6834. JIT_HELPER_END(OP_NewScopeSlots);
  6835. }
  6836. Field(Var)* JavascriptOperators::OP_NewScopeSlotsWithoutPropIds(unsigned int count, int scopeIndex, ScriptContext *scriptContext, FunctionBody *functionBody)
  6837. {
  6838. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_NewScopeSlotsWithoutPropIds);
  6839. DebuggerScope* scope = reinterpret_cast<DebuggerScope*>(Constants::FunctionBodyUnavailable);
  6840. if (scopeIndex != DebuggerScope::InvalidScopeIndex)
  6841. {
  6842. AssertMsg(functionBody->GetScopeObjectChain(), "A scope chain should always be created when there are new scope slots for blocks.");
  6843. scope = functionBody->GetScopeObjectChain()->pScopeChain->Item(scopeIndex);
  6844. }
  6845. return OP_NewScopeSlots(count, scriptContext, scope);
  6846. JIT_HELPER_END(OP_NewScopeSlotsWithoutPropIds);
  6847. }
  6848. Field(Var)* JavascriptOperators::OP_CloneScopeSlots(Field(Var) *slotArray, ScriptContext *scriptContext)
  6849. {
  6850. JIT_HELPER_NOT_REENTRANT_HEADER(OP_CloneInnerScopeSlots, reentrancylock, scriptContext->GetThreadContext());
  6851. ScopeSlots slots(slotArray);
  6852. uint size = ScopeSlots::FirstSlotIndex + static_cast<uint>(slots.GetCount());
  6853. Field(Var)* slotArrayClone = RecyclerNewArray(scriptContext->GetRecycler(), Field(Var), size);
  6854. CopyArray(slotArrayClone, size, slotArray, size);
  6855. return slotArrayClone;
  6856. JIT_HELPER_END(OP_CloneInnerScopeSlots);
  6857. }
  6858. Var JavascriptOperators::OP_NewPseudoScope(ScriptContext *scriptContext)
  6859. {
  6860. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_NewPseudoScope);
  6861. return scriptContext->GetLibrary()->CreatePseudoActivationObject();
  6862. JIT_HELPER_END(OP_NewPseudoScope);
  6863. }
  6864. Var JavascriptOperators::OP_NewBlockScope(ScriptContext *scriptContext)
  6865. {
  6866. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_NewBlockScope);
  6867. return scriptContext->GetLibrary()->CreateBlockActivationObject();
  6868. JIT_HELPER_END(OP_NewBlockScope);
  6869. }
  6870. Var JavascriptOperators::OP_CloneBlockScope(BlockActivationObject *blockScope, ScriptContext *scriptContext)
  6871. {
  6872. JIT_HELPER_NOT_REENTRANT_HEADER(OP_CloneBlockScope, reentrancylock, scriptContext->GetThreadContext());
  6873. return blockScope->Clone(scriptContext);
  6874. JIT_HELPER_END(OP_CloneBlockScope);
  6875. }
  6876. Var JavascriptOperators::OP_IsInst(Var instance, Var aClass, ScriptContext* scriptContext, IsInstInlineCache* inlineCache)
  6877. {
  6878. JIT_HELPER_REENTRANT_HEADER(ScrObj_OP_IsInst);
  6879. if (!VarIs<RecyclableObject>(aClass))
  6880. {
  6881. JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedFunction, _u("instanceof"));
  6882. }
  6883. RecyclableObject* constructor = VarTo<RecyclableObject>(aClass);
  6884. if (scriptContext->GetConfig()->IsES6HasInstanceEnabled())
  6885. {
  6886. if (VarIs<JavascriptFunction>(constructor))
  6887. {
  6888. JavascriptFunction* func = VarTo<JavascriptFunction>(constructor);
  6889. if (func->IsBoundFunction())
  6890. {
  6891. BoundFunction* boundFunc = (BoundFunction*)func;
  6892. constructor = boundFunc->GetTargetFunction();
  6893. }
  6894. }
  6895. Var instOfHandler = JavascriptOperators::GetPropertyNoCache(constructor,
  6896. PropertyIds::_symbolHasInstance, scriptContext);
  6897. if (JavascriptOperators::IsUndefinedObject(instOfHandler)
  6898. || instOfHandler == scriptContext->GetBuiltInLibraryFunction(JavascriptFunction::EntryInfo::SymbolHasInstance.GetOriginalEntryPoint()))
  6899. {
  6900. return JavascriptBoolean::ToVar(constructor->HasInstance(instance, scriptContext, inlineCache), scriptContext);
  6901. }
  6902. else
  6903. {
  6904. if (!JavascriptConversion::IsCallable(instOfHandler))
  6905. {
  6906. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, _u("Symbol[Symbol.hasInstance]"));
  6907. }
  6908. ThreadContext * threadContext = scriptContext->GetThreadContext();
  6909. RecyclableObject *instFunc = VarTo<RecyclableObject>(instOfHandler);
  6910. Var result = threadContext->ExecuteImplicitCall(instFunc, ImplicitCall_Accessor, [=]()->Js::Var
  6911. {
  6912. return CALL_FUNCTION(scriptContext->GetThreadContext(), instFunc, CallInfo(CallFlags_Value, 2), constructor, instance);
  6913. });
  6914. return JavascriptBoolean::ToVar(JavascriptConversion::ToBoolean(result, scriptContext) ? TRUE : FALSE, scriptContext);
  6915. }
  6916. }
  6917. else
  6918. {
  6919. return JavascriptBoolean::ToVar(constructor->HasInstance(instance, scriptContext, inlineCache), scriptContext);
  6920. }
  6921. JIT_HELPER_END(ScrObj_OP_IsInst);
  6922. }
  6923. Var JavascriptOperators::OP_NewClassProto(Var protoParent, ScriptContext * scriptContext)
  6924. {
  6925. JIT_HELPER_NOT_REENTRANT_HEADER(Op_NewClassProto, reentrancylock, scriptContext->GetThreadContext());
  6926. return scriptContext->GetLibrary()->CreateClassPrototypeObject(VarTo<RecyclableObject>(protoParent));
  6927. JIT_HELPER_END(Op_NewClassProto);
  6928. }
  6929. void JavascriptOperators::OP_LoadUndefinedToElement(Var instance, PropertyId propertyId)
  6930. {
  6931. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LdElemUndef, reentrancylock, VarTo<RecyclableObject>(instance)->GetScriptContext()->GetThreadContext());
  6932. AssertMsg(!TaggedNumber::Is(instance), "Invalid scope/root object");
  6933. JavascriptOperators::EnsureProperty(instance, propertyId);
  6934. JIT_HELPER_END(Op_LdElemUndef);
  6935. }
  6936. void JavascriptOperators::OP_LoadUndefinedToElementScoped(FrameDisplay *pScope, PropertyId propertyId, Var defaultInstance, ScriptContext* scriptContext)
  6937. {
  6938. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LdElemUndefScoped, reentrancylock, scriptContext->GetThreadContext());
  6939. int i;
  6940. int length = pScope->GetLength();
  6941. Var argInstance;
  6942. for (i = 0; i < length; i++)
  6943. {
  6944. argInstance = pScope->GetItem(i);
  6945. if (JavascriptOperators::EnsureProperty(argInstance, propertyId))
  6946. {
  6947. return;
  6948. }
  6949. }
  6950. if (!JavascriptOperators::HasOwnPropertyNoHostObject(defaultInstance, propertyId))
  6951. {
  6952. // CONSIDER : Consider adding pre-initialization support to activation objects.
  6953. JavascriptOperators::OP_InitPropertyScoped(pScope, propertyId, scriptContext->GetLibrary()->GetUndefined(), defaultInstance, scriptContext);
  6954. }
  6955. JIT_HELPER_END(Op_LdElemUndefScoped);
  6956. }
  6957. void JavascriptOperators::OP_LoadUndefinedToElementDynamic(Var instance, PropertyId propertyId, ScriptContext *scriptContext)
  6958. {
  6959. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LdElemUndefDynamic, reentrancylock, scriptContext->GetThreadContext());
  6960. if (!JavascriptOperators::HasOwnPropertyNoHostObject(instance, propertyId))
  6961. {
  6962. VarTo<RecyclableObject>(instance)->InitPropertyScoped(propertyId, scriptContext->GetLibrary()->GetUndefined());
  6963. }
  6964. JIT_HELPER_END(Op_LdElemUndefDynamic);
  6965. }
  6966. BOOL JavascriptOperators::EnsureProperty(Var instance, PropertyId propertyId)
  6967. {
  6968. RecyclableObject *obj = VarTo<RecyclableObject>(instance);
  6969. return (obj && obj->EnsureProperty(propertyId));
  6970. }
  6971. void JavascriptOperators::OP_EnsureNoRootProperty(Var instance, PropertyId propertyId)
  6972. {
  6973. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_EnsureNoRootProperty);
  6974. Assert(VarIs<RootObjectBase>(instance));
  6975. RootObjectBase *obj = VarTo<RootObjectBase>(instance);
  6976. obj->EnsureNoProperty(propertyId);
  6977. JIT_HELPER_END(Op_EnsureNoRootProperty);
  6978. }
  6979. void JavascriptOperators::OP_EnsureNoRootRedeclProperty(Var instance, PropertyId propertyId)
  6980. {
  6981. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_EnsureNoRootRedeclProperty);
  6982. Assert(VarIs<RootObjectBase>(instance));
  6983. RecyclableObject *obj = VarTo<RecyclableObject>(instance);
  6984. obj->EnsureNoRedeclProperty(propertyId);
  6985. JIT_HELPER_END(Op_EnsureNoRootRedeclProperty);
  6986. }
  6987. void JavascriptOperators::OP_EnsureCanDeclGloFunc(Var instance, PropertyId propertyId)
  6988. {
  6989. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_EnsureCanDeclGloFunc);
  6990. Assert(VarIs<RootObjectBase>(instance));
  6991. RootObjectBase *obj = VarTo<RootObjectBase>(instance);
  6992. obj->EnsureCanDeclGloFunc(propertyId);
  6993. JIT_HELPER_END(Op_EnsureCanDeclGloFunc);
  6994. }
  6995. void JavascriptOperators::OP_ScopedEnsureNoRedeclProperty(FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance)
  6996. {
  6997. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_EnsureNoRedeclPropertyScoped);
  6998. int i;
  6999. int length = pDisplay->GetLength();
  7000. RecyclableObject *object;
  7001. for (i = 0; i < length; i++)
  7002. {
  7003. object = VarTo<RecyclableObject>(pDisplay->GetItem(i));
  7004. if (object->EnsureNoRedeclProperty(propertyId))
  7005. {
  7006. return;
  7007. }
  7008. }
  7009. object = VarTo<RecyclableObject>(defaultInstance);
  7010. object->EnsureNoRedeclProperty(propertyId);
  7011. JIT_HELPER_END(Op_EnsureNoRedeclPropertyScoped);
  7012. }
  7013. Var JavascriptOperators::IsIn(Var argProperty, Var instance, ScriptContext* scriptContext)
  7014. {
  7015. JIT_HELPER_REENTRANT_HEADER(Op_IsIn);
  7016. // Note that the fact that we haven't seen a given name before doesn't mean that the instance doesn't
  7017. if (!IsObject(instance))
  7018. {
  7019. JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedObject, _u("in"));
  7020. }
  7021. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  7022. BOOL result;
  7023. PropertyRecord const * propertyRecord = nullptr;
  7024. uint32 index;
  7025. IndexType indexType;
  7026. // Fast path for JavascriptSymbols and PropertyStrings
  7027. RecyclableObject* cacheOwner;
  7028. PropertyRecordUsageCache* propertyRecordUsageCache;
  7029. if (GetPropertyRecordUsageCache(argProperty, scriptContext, &propertyRecordUsageCache, &cacheOwner))
  7030. {
  7031. Var value;
  7032. propertyRecord = propertyRecordUsageCache->GetPropertyRecord();
  7033. if (!propertyRecord->IsNumeric())
  7034. {
  7035. PropertyValueInfo info;
  7036. if (propertyRecordUsageCache->TryGetPropertyFromCache<false /* OwnPropertyOnly */, true /* OutputExistence */, false /* ReturnOperationInfo */>(instance, object, &value, scriptContext, &info, cacheOwner, nullptr))
  7037. {
  7038. Assert(VarIs<JavascriptBoolean>(value));
  7039. return value;
  7040. }
  7041. result = JavascriptOperators::GetPropertyWPCache<true /* OutputExistence */>(instance, object, propertyRecordUsageCache->GetPropertyRecord()->GetPropertyId(), &value, scriptContext, &info);
  7042. Assert(value == JavascriptBoolean::ToVar(result, scriptContext));
  7043. return value;
  7044. }
  7045. // We don't cache numeric property lookups, so fall through to the IndexType_Number case
  7046. index = propertyRecord->GetNumericValue();
  7047. indexType = IndexType_Number;
  7048. }
  7049. else
  7050. {
  7051. indexType = GetIndexType(argProperty, scriptContext, &index, &propertyRecord, true);
  7052. }
  7053. if (indexType == IndexType_Number)
  7054. {
  7055. result = JavascriptOperators::HasItem(object, index);
  7056. }
  7057. else
  7058. {
  7059. result = JavascriptOperators::HasProperty(object, propertyRecord->GetPropertyId());
  7060. #ifdef TELEMETRY_JSO
  7061. {
  7062. Assert(indexType != Js::IndexType_JavascriptString);
  7063. if (indexType == Js::IndexType_PropertyId)
  7064. {
  7065. scriptContext->GetTelemetry().GetOpcodeTelemetry().IsIn(instance, propertyId, result != 0);
  7066. }
  7067. }
  7068. #endif
  7069. }
  7070. return JavascriptBoolean::ToVar(result, scriptContext);
  7071. JIT_HELPER_END(Op_IsIn);
  7072. }
  7073. template <bool IsFromFullJit, class TInlineCache>
  7074. inline Var JavascriptOperators::PatchGetValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7075. {
  7076. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetValue);
  7077. return PatchGetValueWithThisPtr<IsFromFullJit, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, instance);
  7078. JIT_HELPER_END(Op_PatchGetValue);
  7079. }
  7080. JIT_HELPER_TEMPLATE(Op_PatchGetValue, Op_PatchGetValuePolymorphic)
  7081. template <bool IsFromFullJit, class TInlineCache>
  7082. __forceinline Var JavascriptOperators::PatchGetValueWithThisPtr(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance)
  7083. {
  7084. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetValueWithThisPtr);
  7085. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7086. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  7087. RecyclableObject* object = nullptr;
  7088. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7089. {
  7090. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7091. {
  7092. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7093. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7094. }
  7095. else
  7096. {
  7097. return scriptContext->GetLibrary()->GetUndefined();
  7098. }
  7099. }
  7100. PropertyValueInfo info;
  7101. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7102. Var value;
  7103. if (CacheOperators::TryGetProperty<true, true, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7104. instance, false, object, propertyId, &value, scriptContext, nullptr, &info))
  7105. {
  7106. return value;
  7107. }
  7108. #if DBG_DUMP
  7109. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7110. {
  7111. CacheOperators::TraceCache(inlineCache, _u("PatchGetValue"), propertyId, scriptContext, object);
  7112. }
  7113. #endif
  7114. return JavascriptOperators::GetProperty(thisInstance, object, propertyId, scriptContext, &info);
  7115. JIT_HELPER_END(Op_PatchGetValueWithThisPtr);
  7116. }
  7117. JIT_HELPER_TEMPLATE(Op_PatchGetValueWithThisPtr, Op_PatchGetValuePolymorphicWithThisPtr)
  7118. template Var JavascriptOperators::PatchGetValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7119. template Var JavascriptOperators::PatchGetValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7120. template Var JavascriptOperators::PatchGetValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7121. template Var JavascriptOperators::PatchGetValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7122. template Var JavascriptOperators::PatchGetValueWithThisPtr<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance);
  7123. template Var JavascriptOperators::PatchGetValueWithThisPtr<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance);
  7124. template Var JavascriptOperators::PatchGetValueWithThisPtr<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance);
  7125. template Var JavascriptOperators::PatchGetValueWithThisPtr<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance);
  7126. template <bool IsFromFullJit, class TInlineCache>
  7127. Var JavascriptOperators::PatchGetValueForTypeOf(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7128. {
  7129. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetValueForTypeOf);
  7130. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7131. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  7132. RecyclableObject* object = nullptr;
  7133. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7134. {
  7135. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7136. {
  7137. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7138. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7139. }
  7140. else
  7141. {
  7142. return scriptContext->GetLibrary()->GetUndefined();
  7143. }
  7144. }
  7145. PropertyValueInfo info;
  7146. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7147. Var value;
  7148. if (CacheOperators::TryGetProperty<true, true, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7149. instance, false, object, propertyId, &value, scriptContext, nullptr, &info))
  7150. {
  7151. return value;
  7152. }
  7153. #if DBG_DUMP
  7154. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7155. {
  7156. CacheOperators::TraceCache(inlineCache, _u("PatchGetValueForTypeOf"), propertyId, scriptContext, object);
  7157. }
  7158. #endif
  7159. Var prop = nullptr;
  7160. BEGIN_TYPEOF_ERROR_HANDLER(scriptContext);
  7161. prop = JavascriptOperators::GetProperty(instance, object, propertyId, scriptContext, &info);
  7162. END_TYPEOF_ERROR_HANDLER(scriptContext, prop);
  7163. return prop;
  7164. JIT_HELPER_END(Op_PatchGetValueForTypeOf);
  7165. }
  7166. JIT_HELPER_TEMPLATE(Op_PatchGetValueForTypeOf, Op_PatchGetValuePolymorphicForTypeOf)
  7167. template Var JavascriptOperators::PatchGetValueForTypeOf<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7168. template Var JavascriptOperators::PatchGetValueForTypeOf<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7169. template Var JavascriptOperators::PatchGetValueForTypeOf<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7170. template Var JavascriptOperators::PatchGetValueForTypeOf<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7171. Var JavascriptOperators::PatchGetValueUsingSpecifiedInlineCache(InlineCache * inlineCache, Var instance, RecyclableObject * object, PropertyId propertyId, ScriptContext* scriptContext)
  7172. {
  7173. PropertyValueInfo info;
  7174. PropertyValueInfo::SetCacheInfo(&info, inlineCache);
  7175. Var value;
  7176. if (CacheOperators::TryGetProperty<true, true, true, true, false, true, !InlineCache::IsPolymorphic, InlineCache::IsPolymorphic, false, false>(
  7177. instance, false, object, propertyId, &value, scriptContext, nullptr, &info))
  7178. {
  7179. return value;
  7180. }
  7181. #if DBG_DUMP
  7182. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7183. {
  7184. CacheOperators::TraceCache(inlineCache, _u("PatchGetValue"), propertyId, scriptContext, object);
  7185. }
  7186. #endif
  7187. return JavascriptOperators::GetProperty(instance, object, propertyId, scriptContext, &info);
  7188. }
  7189. Var JavascriptOperators::PatchGetValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7190. {
  7191. return PatchGetValueWithThisPtrNoFastPath(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, instance);
  7192. }
  7193. Var JavascriptOperators::PatchGetValueWithThisPtrNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance)
  7194. {
  7195. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7196. RecyclableObject* object = nullptr;
  7197. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7198. {
  7199. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7200. {
  7201. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7202. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7203. }
  7204. else
  7205. {
  7206. return scriptContext->GetLibrary()->GetUndefined();
  7207. }
  7208. }
  7209. PropertyValueInfo info;
  7210. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7211. return JavascriptOperators::GetProperty(thisInstance, object, propertyId, scriptContext, &info);
  7212. }
  7213. template <bool IsFromFullJit, class TInlineCache>
  7214. inline Var JavascriptOperators::PatchGetRootValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId)
  7215. {
  7216. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetRootValue);
  7217. AssertMsg(VarIs<RootObjectBase>(object), "Root must be a global object!");
  7218. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7219. PropertyValueInfo info;
  7220. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7221. Var value;
  7222. if (CacheOperators::TryGetProperty<true, true, true, false, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7223. object, true, object, propertyId, &value, scriptContext, nullptr, &info))
  7224. {
  7225. return value;
  7226. }
  7227. #if DBG_DUMP
  7228. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7229. {
  7230. CacheOperators::TraceCache(inlineCache, _u("PatchGetRootValue"), propertyId, scriptContext, object);
  7231. }
  7232. #endif
  7233. return JavascriptOperators::OP_GetRootProperty(object, propertyId, &info, scriptContext);
  7234. JIT_HELPER_END(Op_PatchGetRootValue);
  7235. }
  7236. JIT_HELPER_TEMPLATE(Op_PatchGetRootValue, Op_PatchGetRootValuePolymorphic)
  7237. template Var JavascriptOperators::PatchGetRootValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7238. template Var JavascriptOperators::PatchGetRootValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7239. template Var JavascriptOperators::PatchGetRootValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7240. template Var JavascriptOperators::PatchGetRootValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7241. template <bool IsFromFullJit, class TInlineCache>
  7242. Var JavascriptOperators::PatchGetRootValueForTypeOf(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId)
  7243. {
  7244. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetRootValueForTypeOf);
  7245. AssertMsg(VarIs<RootObjectBase>(object), "Root must be a global object!");
  7246. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7247. PropertyValueInfo info;
  7248. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7249. Var value = nullptr;
  7250. if (CacheOperators::TryGetProperty<true, true, true, false, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7251. object, true, object, propertyId, &value, scriptContext, nullptr, &info))
  7252. {
  7253. return value;
  7254. }
  7255. #if DBG_DUMP
  7256. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7257. {
  7258. CacheOperators::TraceCache(inlineCache, _u("PatchGetRootValueForTypeOf"), propertyId, scriptContext, object);
  7259. }
  7260. #endif
  7261. value = nullptr;
  7262. BEGIN_TYPEOF_ERROR_HANDLER(scriptContext);
  7263. AssertOrFailFast(VarIsCorrectType(static_cast<RecyclableObject*>(object)));
  7264. if (JavascriptOperators::GetRootProperty(object, propertyId, &value, scriptContext, &info))
  7265. {
  7266. if (scriptContext->IsUndeclBlockVar(value))
  7267. {
  7268. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  7269. }
  7270. return value;
  7271. }
  7272. END_TYPEOF_ERROR_HANDLER(scriptContext, value);
  7273. value = scriptContext->GetLibrary()->GetUndefined();
  7274. return value;
  7275. JIT_HELPER_END(Op_PatchGetRootValueForTypeOf);
  7276. }
  7277. JIT_HELPER_TEMPLATE(Op_PatchGetRootValueForTypeOf, Op_PatchGetRootValuePolymorphicForTypeOf)
  7278. template Var JavascriptOperators::PatchGetRootValueForTypeOf<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7279. template Var JavascriptOperators::PatchGetRootValueForTypeOf<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7280. template Var JavascriptOperators::PatchGetRootValueForTypeOf<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7281. template Var JavascriptOperators::PatchGetRootValueForTypeOf<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7282. Var JavascriptOperators::PatchGetRootValueNoFastPath_Var(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7283. {
  7284. return
  7285. PatchGetRootValueNoFastPath(
  7286. functionBody,
  7287. inlineCache,
  7288. inlineCacheIndex,
  7289. VarTo<DynamicObject>(instance),
  7290. propertyId);
  7291. }
  7292. Var JavascriptOperators::PatchGetRootValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId)
  7293. {
  7294. AssertMsg(VarIs<RootObjectBase>(object), "Root must be a global object!");
  7295. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7296. PropertyValueInfo info;
  7297. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7298. return JavascriptOperators::OP_GetRootProperty(object, propertyId, &info, scriptContext);
  7299. }
  7300. template <bool IsFromFullJit, class TInlineCache>
  7301. inline Var JavascriptOperators::PatchGetPropertyScoped(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance)
  7302. {
  7303. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetPropertyScoped);
  7304. // Get the property, using a scope stack rather than an individual instance.
  7305. // Walk the stack until we find an instance that has the property.
  7306. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7307. uint16 length = pDisplay->GetLength();
  7308. PropertyValueInfo info;
  7309. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7310. for (uint16 i = 0; i < length; i++)
  7311. {
  7312. RecyclableObject* object = UnsafeVarTo<RecyclableObject>(pDisplay->GetItem(i));
  7313. Var value;
  7314. if (CacheOperators::TryGetProperty<true, true, true, false, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7315. object, false, object, propertyId, &value, scriptContext, nullptr, &info))
  7316. {
  7317. return value;
  7318. }
  7319. #if DBG_DUMP
  7320. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7321. {
  7322. CacheOperators::TraceCache(inlineCache, _u("PatchGetPropertyScoped"), propertyId, scriptContext, object);
  7323. }
  7324. #endif
  7325. if (JavascriptOperators::GetProperty(object, propertyId, &value, scriptContext, &info))
  7326. {
  7327. if (scriptContext->IsUndeclBlockVar(value) && propertyId != PropertyIds::_this)
  7328. {
  7329. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  7330. }
  7331. return value;
  7332. }
  7333. }
  7334. // There is no root decl for 'this', we should instead load the global 'this' value.
  7335. if (propertyId == PropertyIds::_this)
  7336. {
  7337. Var varNull = OP_LdNull(scriptContext);
  7338. return JavascriptOperators::OP_GetThis(varNull, functionBody->GetModuleID(), scriptContext);
  7339. }
  7340. else if (propertyId == PropertyIds::_super)
  7341. {
  7342. JavascriptError::ThrowReferenceError(scriptContext, JSERR_BadSuperReference);
  7343. }
  7344. // No one in the scope stack has the property, so get it from the default instance provided by the caller.
  7345. Var value = JavascriptOperators::PatchGetRootValue<IsFromFullJit>(functionBody, inlineCache, inlineCacheIndex, VarTo<DynamicObject>(defaultInstance), propertyId);
  7346. if (scriptContext->IsUndeclBlockVar(value))
  7347. {
  7348. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  7349. }
  7350. return value;
  7351. JIT_HELPER_END(Op_PatchGetPropertyScoped);
  7352. }
  7353. template Var JavascriptOperators::PatchGetPropertyScoped<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7354. template Var JavascriptOperators::PatchGetPropertyScoped<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7355. template Var JavascriptOperators::PatchGetPropertyScoped<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7356. template Var JavascriptOperators::PatchGetPropertyScoped<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7357. template <bool IsFromFullJit, class TInlineCache>
  7358. Var JavascriptOperators::PatchGetPropertyForTypeOfScoped(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance)
  7359. {
  7360. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetPropertyForTypeOfScoped);
  7361. Var value = nullptr;
  7362. ScriptContext *scriptContext = functionBody->GetScriptContext();
  7363. BEGIN_TYPEOF_ERROR_HANDLER(scriptContext);
  7364. value = JavascriptOperators::PatchGetPropertyScoped<IsFromFullJit, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, pDisplay, propertyId, defaultInstance);
  7365. END_TYPEOF_ERROR_HANDLER(scriptContext, value)
  7366. return value;
  7367. JIT_HELPER_END(Op_PatchGetPropertyForTypeOfScoped);
  7368. }
  7369. template Var JavascriptOperators::PatchGetPropertyForTypeOfScoped<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7370. template Var JavascriptOperators::PatchGetPropertyForTypeOfScoped<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7371. template Var JavascriptOperators::PatchGetPropertyForTypeOfScoped<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7372. template Var JavascriptOperators::PatchGetPropertyForTypeOfScoped<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7373. template <bool IsFromFullJit, class TInlineCache>
  7374. inline Var JavascriptOperators::PatchGetMethod(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7375. {
  7376. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetMethod);
  7377. Assert(inlineCache != nullptr);
  7378. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7379. RecyclableObject* object = nullptr;
  7380. #if ENABLE_COPYONACCESS_ARRAY
  7381. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  7382. #endif
  7383. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7384. {
  7385. // Don't error if we disabled implicit calls
  7386. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7387. {
  7388. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7389. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7390. }
  7391. else
  7392. {
  7393. #ifdef TELEMETRY_JSO
  7394. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  7395. {
  7396. // `successful` will be true as PatchGetMethod throws an exception if not found.
  7397. scriptContext->GetTelemetry().GetOpcodeTelemetry().GetMethodProperty(object, propertyId, value, /*successful:*/false);
  7398. }
  7399. #endif
  7400. return scriptContext->GetLibrary()->GetUndefined();
  7401. }
  7402. }
  7403. PropertyValueInfo info;
  7404. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7405. Var value;
  7406. if (CacheOperators::TryGetProperty<true, true, true, false, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7407. instance, false, object, propertyId, &value, scriptContext, nullptr, &info))
  7408. {
  7409. return value;
  7410. }
  7411. #if DBG_DUMP
  7412. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7413. {
  7414. CacheOperators::TraceCache(inlineCache, _u("PatchGetMethod"), propertyId, scriptContext, object);
  7415. }
  7416. #endif
  7417. value = Js::JavascriptOperators::PatchGetMethodFromObject(instance, object, propertyId, &info, scriptContext, false);
  7418. #ifdef TELEMETRY_JSO
  7419. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  7420. {
  7421. // `successful` will be true as PatchGetMethod throws an exception if not found.
  7422. scriptContext->GetTelemetry().GetOpcodeTelemetry().GetMethodProperty(object, propertyId, value, /*successful:*/true);
  7423. }
  7424. #endif
  7425. return value;
  7426. JIT_HELPER_END(Op_PatchGetMethod);
  7427. }
  7428. JIT_HELPER_TEMPLATE(Op_PatchGetMethod, Op_PatchGetMethodPolymorphic)
  7429. template Var JavascriptOperators::PatchGetMethod<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7430. template Var JavascriptOperators::PatchGetMethod<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7431. template Var JavascriptOperators::PatchGetMethod<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7432. template Var JavascriptOperators::PatchGetMethod<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7433. template <bool IsFromFullJit, class TInlineCache>
  7434. inline Var JavascriptOperators::PatchGetRootMethod(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId)
  7435. {
  7436. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetRootMethod);
  7437. Assert(inlineCache != nullptr);
  7438. AssertMsg(VarIs<RootObjectBase>(object), "Root must be a global object!");
  7439. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7440. PropertyValueInfo info;
  7441. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7442. Var value;
  7443. if (CacheOperators::TryGetProperty<true, true, true, false, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7444. object, true, object, propertyId, &value, scriptContext, nullptr, &info))
  7445. {
  7446. return value;
  7447. }
  7448. #if DBG_DUMP
  7449. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7450. {
  7451. CacheOperators::TraceCache(inlineCache, _u("PatchGetRootMethod"), propertyId, scriptContext, object);
  7452. }
  7453. #endif
  7454. value = Js::JavascriptOperators::PatchGetMethodFromObject(object, object, propertyId, &info, scriptContext, true);
  7455. #ifdef TELEMETRY_JSO
  7456. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  7457. {
  7458. // `successful` will be true as PatchGetMethod throws an exception if not found.
  7459. scriptContext->GetTelemetry().GetOpcodeTelemetry().GetMethodProperty(object, propertyId, value, /*successful:*/ true);
  7460. }
  7461. #endif
  7462. return value;
  7463. JIT_HELPER_END(Op_PatchGetRootMethod);
  7464. }
  7465. JIT_HELPER_TEMPLATE(Op_PatchGetRootMethod, Op_PatchGetRootMethodPolymorphic)
  7466. template Var JavascriptOperators::PatchGetRootMethod<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId);
  7467. template Var JavascriptOperators::PatchGetRootMethod<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId);
  7468. template Var JavascriptOperators::PatchGetRootMethod<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId);
  7469. template Var JavascriptOperators::PatchGetRootMethod<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId);
  7470. template <bool IsFromFullJit, class TInlineCache>
  7471. inline Var JavascriptOperators::PatchScopedGetMethod(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7472. {
  7473. JIT_HELPER_REENTRANT_HEADER(Op_ScopedGetMethod);
  7474. Assert(inlineCache != nullptr);
  7475. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7476. RecyclableObject* object = nullptr;
  7477. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7478. {
  7479. // Don't error if we disabled implicit calls
  7480. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7481. {
  7482. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7483. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7484. }
  7485. else
  7486. {
  7487. return scriptContext->GetLibrary()->GetUndefined();
  7488. }
  7489. }
  7490. PropertyValueInfo info;
  7491. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7492. const bool isRoot = VarIs<RootObjectBase>(object);
  7493. Var value;
  7494. if (CacheOperators::TryGetProperty<true, true, true, false, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7495. instance, isRoot, object, propertyId, &value, scriptContext, nullptr, &info))
  7496. {
  7497. return value;
  7498. }
  7499. #if DBG_DUMP
  7500. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7501. {
  7502. CacheOperators::TraceCache(inlineCache, _u("PatchGetMethod"), propertyId, scriptContext, object);
  7503. }
  7504. #endif
  7505. return Js::JavascriptOperators::PatchGetMethodFromObject(instance, object, propertyId, &info, scriptContext, isRoot);
  7506. JIT_HELPER_END(Op_ScopedGetMethod);
  7507. }
  7508. JIT_HELPER_TEMPLATE(Op_ScopedGetMethod, Op_ScopedGetMethodPolymorphic)
  7509. template Var JavascriptOperators::PatchScopedGetMethod<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7510. template Var JavascriptOperators::PatchScopedGetMethod<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7511. template Var JavascriptOperators::PatchScopedGetMethod<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7512. template Var JavascriptOperators::PatchScopedGetMethod<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7513. Var JavascriptOperators::PatchGetMethodNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7514. {
  7515. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7516. RecyclableObject* object = nullptr;
  7517. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7518. {
  7519. // Don't error if we disabled implicit calls
  7520. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7521. {
  7522. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7523. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7524. }
  7525. else
  7526. {
  7527. return scriptContext->GetLibrary()->GetUndefined();
  7528. }
  7529. }
  7530. PropertyValueInfo info;
  7531. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7532. return Js::JavascriptOperators::PatchGetMethodFromObject(instance, object, propertyId, &info, scriptContext, false);
  7533. }
  7534. Var JavascriptOperators::PatchGetRootMethodNoFastPath_Var(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7535. {
  7536. return
  7537. PatchGetRootMethodNoFastPath(
  7538. functionBody,
  7539. inlineCache,
  7540. inlineCacheIndex,
  7541. VarTo<DynamicObject>(instance),
  7542. propertyId);
  7543. }
  7544. Var JavascriptOperators::PatchGetRootMethodNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId)
  7545. {
  7546. AssertMsg(VarIs<RootObjectBase>(object), "Root must be a global object!");
  7547. PropertyValueInfo info;
  7548. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7549. return Js::JavascriptOperators::PatchGetMethodFromObject(object, object, propertyId, &info, functionBody->GetScriptContext(), true);
  7550. }
  7551. Var JavascriptOperators::PatchGetMethodFromObject(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, PropertyValueInfo * info, ScriptContext* scriptContext, bool isRootLd)
  7552. {
  7553. Assert(IsPropertyObject(propertyObject));
  7554. Var value = nullptr;
  7555. BOOL foundValue = FALSE;
  7556. if (isRootLd)
  7557. {
  7558. RootObjectBase* rootObject = VarTo<RootObjectBase>(instance);
  7559. foundValue = JavascriptOperators::GetRootPropertyReference(rootObject, propertyId, &value, scriptContext, info);
  7560. }
  7561. else
  7562. {
  7563. foundValue = JavascriptOperators::GetPropertyReference(instance, propertyObject, propertyId, &value, scriptContext, info);
  7564. }
  7565. if (!foundValue)
  7566. {
  7567. // Don't error if we disabled implicit calls
  7568. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7569. {
  7570. const char16* propertyName = scriptContext->GetPropertyName(propertyId)->GetBuffer();
  7571. value = scriptContext->GetLibrary()->GetUndefined();
  7572. JavascriptFunction * caller = NULL;
  7573. if (JavascriptStackWalker::GetCaller(&caller, scriptContext))
  7574. {
  7575. FunctionBody * callerBody = caller->GetFunctionBody();
  7576. if (callerBody && callerBody->GetUtf8SourceInfo()->GetIsXDomain())
  7577. {
  7578. propertyName = NULL;
  7579. }
  7580. }
  7581. // Prior to version 12 we had mistakenly immediately thrown an error for property reference method calls
  7582. // (i.e. <expr>.foo() form) when the target object is the global object. The spec says that a GetValue
  7583. // on a reference should throw if the reference is unresolved, of which a property reference can never be,
  7584. // however it can be unresolved in the case of an identifier expression, e.g. foo() with no qualification.
  7585. // Such a case would come down to the global object if foo was undefined, hence the check for root object,
  7586. // except that it should have been a check for isRootLd to be correct.
  7587. //
  7588. // // (at global scope)
  7589. // foo(x());
  7590. //
  7591. // should throw an error before evaluating x() if foo is not defined, but
  7592. //
  7593. // // (at global scope)
  7594. // this.foo(x());
  7595. //
  7596. // should evaluate x() before throwing an error if foo is not a property on the global object.
  7597. // Maintain old behavior prior to version 12.
  7598. bool isPropertyReference = !isRootLd;
  7599. if (!isPropertyReference)
  7600. {
  7601. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UndefVariable, propertyName);
  7602. }
  7603. else
  7604. {
  7605. // ES5 11.2.3 #2: We evaluate the call target but don't throw yet if target member is missing. We need to evaluate argList
  7606. // first (#3). Postpone throwing error to invoke time.
  7607. value = ThrowErrorObject::CreateThrowTypeErrorObject(scriptContext, VBSERR_OLENoPropOrMethod, propertyName);
  7608. }
  7609. }
  7610. }
  7611. return value;
  7612. }
  7613. template <bool IsFromFullJit, class TInlineCache>
  7614. inline void JavascriptOperators::PatchPutValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7615. {
  7616. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValue);
  7617. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValue, Op_PatchPutValueWithThisPtr);
  7618. PatchPutValueWithThisPtr<IsFromFullJit, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7619. JIT_HELPER_END(Op_PatchPutValue);
  7620. }
  7621. JIT_HELPER_TEMPLATE(Op_PatchPutValue, Op_PatchPutValuePolymorphic)
  7622. template <bool IsFromFullJit, class TInlineCache>
  7623. inline void JavascriptOperators::PatchPutValueWithThisPtr(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7624. {
  7625. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtr);
  7626. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7627. if (TaggedNumber::Is(instance))
  7628. {
  7629. JavascriptOperators::SetPropertyOnTaggedNumber(instance, nullptr, propertyId, newValue, scriptContext, flags);
  7630. return;
  7631. }
  7632. #if ENABLE_COPYONACCESS_ARRAY
  7633. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  7634. #endif
  7635. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  7636. PropertyValueInfo info;
  7637. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7638. if (CacheOperators::TrySetProperty<true, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7639. object, false, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7640. {
  7641. return;
  7642. }
  7643. #if DBG_DUMP
  7644. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7645. {
  7646. CacheOperators::TraceCache(inlineCache, _u("PatchPutValue"), propertyId, scriptContext, object);
  7647. }
  7648. #endif
  7649. ImplicitCallFlags prevImplicitCallFlags = ImplicitCall_None;
  7650. ImplicitCallFlags currImplicitCallFlags = ImplicitCall_None;
  7651. bool hasThisOnlyStatements = functionBody->GetHasOnlyThisStmts();
  7652. if (hasThisOnlyStatements)
  7653. {
  7654. prevImplicitCallFlags = CacheAndClearImplicitBit(scriptContext);
  7655. }
  7656. if (!JavascriptOperators::OP_SetProperty(object, propertyId, newValue, scriptContext, &info, flags, thisInstance))
  7657. {
  7658. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7659. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7660. }
  7661. if (hasThisOnlyStatements)
  7662. {
  7663. currImplicitCallFlags = CheckAndUpdateFunctionBodyWithImplicitFlag(functionBody);
  7664. RestoreImplicitFlag(scriptContext, prevImplicitCallFlags, currImplicitCallFlags);
  7665. }
  7666. JIT_HELPER_END(Op_PatchPutValueWithThisPtr);
  7667. }
  7668. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtr, Op_PatchPutValueWithThisPtrPolymorphic)
  7669. template void JavascriptOperators::PatchPutValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7670. template void JavascriptOperators::PatchPutValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7671. template void JavascriptOperators::PatchPutValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7672. template void JavascriptOperators::PatchPutValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7673. template <bool IsFromFullJit, class TInlineCache>
  7674. inline void JavascriptOperators::PatchPutRootValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7675. {
  7676. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutRootValue);
  7677. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7678. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  7679. PropertyValueInfo info;
  7680. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7681. if (CacheOperators::TrySetProperty<true, true, true, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7682. object, true, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7683. {
  7684. return;
  7685. }
  7686. #if DBG_DUMP
  7687. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7688. {
  7689. CacheOperators::TraceCache(inlineCache, _u("PatchPutRootValue"), propertyId, scriptContext, object);
  7690. }
  7691. #endif
  7692. ImplicitCallFlags prevImplicitCallFlags = ImplicitCall_None;
  7693. ImplicitCallFlags currImplicitCallFlags = ImplicitCall_None;
  7694. bool hasThisOnlyStatements = functionBody->GetHasOnlyThisStmts();
  7695. if (hasThisOnlyStatements)
  7696. {
  7697. prevImplicitCallFlags = CacheAndClearImplicitBit(scriptContext);
  7698. }
  7699. if (!JavascriptOperators::SetRootProperty(object, propertyId, newValue, &info, scriptContext, flags))
  7700. {
  7701. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7702. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7703. }
  7704. if (hasThisOnlyStatements)
  7705. {
  7706. currImplicitCallFlags = CheckAndUpdateFunctionBodyWithImplicitFlag(functionBody);
  7707. RestoreImplicitFlag(scriptContext, prevImplicitCallFlags, currImplicitCallFlags);
  7708. }
  7709. JIT_HELPER_END(Op_PatchPutRootValue);
  7710. }
  7711. JIT_HELPER_TEMPLATE(Op_PatchPutRootValue, Op_PatchPutRootValuePolymorphic)
  7712. template void JavascriptOperators::PatchPutRootValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7713. template void JavascriptOperators::PatchPutRootValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7714. template void JavascriptOperators::PatchPutRootValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7715. template void JavascriptOperators::PatchPutRootValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7716. template <bool IsFromFullJit, class TInlineCache>
  7717. inline void JavascriptOperators::PatchPutValueNoLocalFastPath(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7718. {
  7719. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueNoLocalFastPath);
  7720. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueNoLocalFastPath, Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7721. PatchPutValueWithThisPtrNoLocalFastPath<IsFromFullJit, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7722. JIT_HELPER_END(Op_PatchPutValueNoLocalFastPath);
  7723. }
  7724. JIT_HELPER_TEMPLATE(Op_PatchPutValueNoLocalFastPath, Op_PatchPutValueNoLocalFastPathPolymorphic)
  7725. template void JavascriptOperators::PatchPutValueNoLocalFastPath<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7726. template void JavascriptOperators::PatchPutValueNoLocalFastPath<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7727. template void JavascriptOperators::PatchPutValueNoLocalFastPath<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7728. template void JavascriptOperators::PatchPutValueNoLocalFastPath<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7729. template <bool IsFromFullJit, class TInlineCache>
  7730. inline void JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPath(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7731. {
  7732. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7733. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7734. if (TaggedNumber::Is(instance))
  7735. {
  7736. JavascriptOperators::SetPropertyOnTaggedNumber(instance,
  7737. nullptr,
  7738. propertyId,
  7739. newValue,
  7740. scriptContext,
  7741. flags);
  7742. return;
  7743. }
  7744. #if ENABLE_COPYONACCESS_ARRAY
  7745. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  7746. #endif
  7747. RecyclableObject *object = UnsafeVarTo<RecyclableObject>(instance);
  7748. PropertyValueInfo info;
  7749. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7750. if (CacheOperators::TrySetProperty<!TInlineCache::IsPolymorphic, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7751. object, false, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7752. {
  7753. return;
  7754. }
  7755. #if DBG_DUMP
  7756. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7757. {
  7758. CacheOperators::TraceCache(inlineCache, _u("PatchPutValueNoLocalFastPath"), propertyId, scriptContext, object);
  7759. }
  7760. #endif
  7761. ImplicitCallFlags prevImplicitCallFlags = ImplicitCall_None;
  7762. ImplicitCallFlags currImplicitCallFlags = ImplicitCall_None;
  7763. bool hasThisOnlyStatements = functionBody->GetHasOnlyThisStmts();
  7764. if (hasThisOnlyStatements)
  7765. {
  7766. prevImplicitCallFlags = CacheAndClearImplicitBit(scriptContext);
  7767. }
  7768. if (!JavascriptOperators::OP_SetProperty(instance, propertyId, newValue, scriptContext, &info, flags, thisInstance))
  7769. {
  7770. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7771. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7772. }
  7773. if (hasThisOnlyStatements)
  7774. {
  7775. currImplicitCallFlags = CheckAndUpdateFunctionBodyWithImplicitFlag(functionBody);
  7776. RestoreImplicitFlag(scriptContext, prevImplicitCallFlags, currImplicitCallFlags);
  7777. }
  7778. JIT_HELPER_END(Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7779. }
  7780. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtrNoLocalFastPath, Op_PatchPutValueWithThisPtrNoLocalFastPathPolymorphic)
  7781. template void JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPath<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7782. template void JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPath<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7783. template void JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPath<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7784. template void JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPath<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7785. template <bool IsFromFullJit, class TInlineCache>
  7786. inline void JavascriptOperators::PatchPutRootValueNoLocalFastPath(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7787. {
  7788. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutRootValueNoLocalFastPath);
  7789. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7790. RecyclableObject *object = VarTo<RecyclableObject>(instance);
  7791. PropertyValueInfo info;
  7792. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7793. if (CacheOperators::TrySetProperty<!TInlineCache::IsPolymorphic, true, true, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7794. object, true, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7795. {
  7796. return;
  7797. }
  7798. #if DBG_DUMP
  7799. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7800. {
  7801. CacheOperators::TraceCache(inlineCache, _u("PatchPutRootValueNoLocalFastPath"), propertyId, scriptContext, object);
  7802. }
  7803. #endif
  7804. ImplicitCallFlags prevImplicitCallFlags = ImplicitCall_None;
  7805. ImplicitCallFlags currImplicitCallFlags = ImplicitCall_None;
  7806. bool hasThisOnlyStatements = functionBody->GetHasOnlyThisStmts();
  7807. if (hasThisOnlyStatements)
  7808. {
  7809. prevImplicitCallFlags = CacheAndClearImplicitBit(scriptContext);
  7810. }
  7811. if (!JavascriptOperators::SetRootProperty(object, propertyId, newValue, &info, scriptContext, flags))
  7812. {
  7813. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7814. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7815. }
  7816. if (hasThisOnlyStatements)
  7817. {
  7818. currImplicitCallFlags = CheckAndUpdateFunctionBodyWithImplicitFlag(functionBody);
  7819. RestoreImplicitFlag(scriptContext, prevImplicitCallFlags, currImplicitCallFlags);
  7820. }
  7821. JIT_HELPER_END(Op_PatchPutRootValueNoLocalFastPath);
  7822. }
  7823. JIT_HELPER_TEMPLATE(Op_PatchPutRootValueNoLocalFastPath, Op_PatchPutRootValueNoLocalFastPathPolymorphic)
  7824. template void JavascriptOperators::PatchPutRootValueNoLocalFastPath<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7825. template void JavascriptOperators::PatchPutRootValueNoLocalFastPath<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7826. template void JavascriptOperators::PatchPutRootValueNoLocalFastPath<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7827. template void JavascriptOperators::PatchPutRootValueNoLocalFastPath<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7828. void JavascriptOperators::PatchPutValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7829. {
  7830. PatchPutValueWithThisPtrNoFastPath(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7831. }
  7832. void JavascriptOperators::PatchPutValueWithThisPtrNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7833. {
  7834. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7835. if (TaggedNumber::Is(instance))
  7836. {
  7837. JavascriptOperators::SetPropertyOnTaggedNumber(instance, nullptr, propertyId, newValue, scriptContext, flags);
  7838. return;
  7839. }
  7840. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  7841. PropertyValueInfo info;
  7842. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7843. if (!JavascriptOperators::OP_SetProperty(object, propertyId, newValue, scriptContext, &info, flags, thisInstance))
  7844. {
  7845. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7846. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7847. }
  7848. }
  7849. void JavascriptOperators::PatchPutRootValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7850. {
  7851. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7852. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  7853. PropertyValueInfo info;
  7854. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7855. if (!JavascriptOperators::SetRootProperty(object, propertyId, newValue, &info, scriptContext, flags))
  7856. {
  7857. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7858. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7859. }
  7860. }
  7861. template <class TInlineCache>
  7862. inline bool JavascriptOperators::PatchPutValueCheckLayout(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7863. {
  7864. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueCheckLayout);
  7865. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueCheckLayout, Op_PatchPutValue);
  7866. DynamicTypeHandler * oldTypeHandler = VarIs<DynamicObject>(instance) ? UnsafeVarTo<DynamicObject>(instance)->GetTypeHandler() : nullptr;
  7867. PatchPutValueWithThisPtr<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7868. return (oldTypeHandler != nullptr && LayoutChanged(UnsafeVarTo<DynamicObject>(instance), oldTypeHandler));
  7869. JIT_HELPER_END(Op_PatchPutValueCheckLayout);
  7870. }
  7871. JIT_HELPER_TEMPLATE(Op_PatchPutValueCheckLayout, Op_PatchPutValuePolymorphicCheckLayout);
  7872. template bool JavascriptOperators::PatchPutValueCheckLayout<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7873. template bool JavascriptOperators::PatchPutValueCheckLayout<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7874. template <class TInlineCache>
  7875. inline bool JavascriptOperators::PatchPutValueWithThisPtrCheckLayout(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7876. {
  7877. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtrCheckLayout);
  7878. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueWithThisPtrCheckLayout, Op_PatchPutValueWithThisPtr);
  7879. DynamicTypeHandler * oldTypeHandler = VarIs<DynamicObject>(instance) ? UnsafeVarTo<DynamicObject>(instance)->GetTypeHandler() : nullptr;
  7880. PatchPutValueWithThisPtr<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, thisInstance, flags);
  7881. return (oldTypeHandler != nullptr && LayoutChanged(UnsafeVarTo<DynamicObject>(instance), oldTypeHandler));
  7882. JIT_HELPER_END(Op_PatchPutValueWithThisPtrCheckLayout);
  7883. }
  7884. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtrCheckLayout, Op_PatchPutValueWithThisPtrPolymorphicCheckLayout);
  7885. template bool JavascriptOperators::PatchPutValueWithThisPtrCheckLayout<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7886. template bool JavascriptOperators::PatchPutValueWithThisPtrCheckLayout<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7887. template <class TInlineCache>
  7888. inline bool JavascriptOperators::PatchPutValueNoLocalFastPathCheckLayout(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7889. {
  7890. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueNoLocalFastPathCheckLayout);
  7891. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueNoLocalFastPathCheckLayout, Op_PatchPutValueNoLocalFastPath);
  7892. DynamicTypeHandler * oldTypeHandler = VarIs<DynamicObject>(instance) ? UnsafeVarTo<DynamicObject>(instance)->GetTypeHandler() : nullptr;
  7893. PatchPutValueWithThisPtrNoLocalFastPath<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7894. return (oldTypeHandler != nullptr && LayoutChanged(UnsafeVarTo<DynamicObject>(instance), oldTypeHandler));
  7895. JIT_HELPER_END(Op_PatchPutValueNoLocalFastPathCheckLayout);
  7896. }
  7897. JIT_HELPER_TEMPLATE(Op_PatchPutValueNoLocalFastPathCheckLayout, Op_PatchPutValueNoLocalFastPathPolymorphicCheckLayout);
  7898. template bool JavascriptOperators::PatchPutValueNoLocalFastPathCheckLayout<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7899. template bool JavascriptOperators::PatchPutValueNoLocalFastPathCheckLayout<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7900. template <class TInlineCache>
  7901. inline bool JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPathCheckLayout(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7902. {
  7903. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtrNoLocalFastPathCheckLayout);
  7904. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueWithThisPtrNoLocalFastPathCheckLayout, Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7905. DynamicTypeHandler * oldTypeHandler = VarIs<DynamicObject>(instance) ? UnsafeVarTo<DynamicObject>(instance)->GetTypeHandler() : nullptr;
  7906. PatchPutValueWithThisPtrNoLocalFastPath<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, thisInstance, flags);
  7907. return (oldTypeHandler != nullptr && LayoutChanged(UnsafeVarTo<DynamicObject>(instance), oldTypeHandler));
  7908. JIT_HELPER_END(Op_PatchPutValueWithThisPtrNoLocalFastPathCheckLayout);
  7909. }
  7910. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtrNoLocalFastPathCheckLayout, Op_PatchPutValueWithThisPtrNoLocalFastPathPolymorphicCheckLayout);
  7911. template bool JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPathCheckLayout<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7912. template bool JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPathCheckLayout<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7913. template <class TInlineCache>
  7914. inline bool JavascriptOperators::PatchInitValueCheckLayout(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue)
  7915. {
  7916. JIT_HELPER_REENTRANT_HEADER(Op_PatchInitValueCheckLayout);
  7917. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchInitValueCheckLayout, Op_PatchInitValue);
  7918. DynamicTypeHandler * oldTypeHandler = VarIs<DynamicObject>(object) ? UnsafeVarTo<DynamicObject>(object)->GetTypeHandler() : nullptr;
  7919. PatchInitValue<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, object, propertyId, newValue);
  7920. return (oldTypeHandler != nullptr && LayoutChanged(UnsafeVarTo<DynamicObject>(object), oldTypeHandler));
  7921. JIT_HELPER_END(Op_PatchInitValueCheckLayout);
  7922. }
  7923. JIT_HELPER_TEMPLATE(Op_PatchInitValueCheckLayout, Op_PatchInitValuePolymorphicCheckLayout);
  7924. template bool JavascriptOperators::PatchInitValueCheckLayout<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7925. template bool JavascriptOperators::PatchInitValueCheckLayout<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7926. bool JavascriptOperators::LayoutChanged(DynamicObject *const instance, DynamicTypeHandler *const oldTypeHandler)
  7927. {
  7928. DynamicTypeHandler * newTypeHandler = instance->GetTypeHandler();
  7929. return (oldTypeHandler != newTypeHandler &&
  7930. (oldTypeHandler->GetInlineSlotCapacity() != newTypeHandler->GetInlineSlotCapacity() ||
  7931. oldTypeHandler->GetOffsetOfInlineSlots() != newTypeHandler->GetOffsetOfInlineSlots()));
  7932. }
  7933. template <bool IsFromFullJit, class TInlineCache>
  7934. inline void JavascriptOperators::PatchInitValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue)
  7935. {
  7936. JIT_HELPER_REENTRANT_HEADER(Op_PatchInitValue);
  7937. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7938. const PropertyOperationFlags flags = newValue == NULL ? PropertyOperation_SpecialValue : PropertyOperation_None;
  7939. PropertyValueInfo info;
  7940. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7941. if (CacheOperators::TrySetProperty<true, true, false, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7942. object, false, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7943. {
  7944. return;
  7945. }
  7946. #if DBG_DUMP
  7947. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7948. {
  7949. CacheOperators::TraceCache(inlineCache, _u("PatchInitValue"), propertyId, scriptContext, object);
  7950. }
  7951. #endif
  7952. Type *typeWithoutProperty = object->GetType();
  7953. if (functionBody->IsEval())
  7954. {
  7955. if (object->InitPropertyInEval(propertyId, newValue, flags, &info))
  7956. {
  7957. CacheOperators::CachePropertyWrite(object, false, typeWithoutProperty, propertyId, &info, scriptContext);
  7958. return;
  7959. }
  7960. }
  7961. // Ideally the lowerer would emit a call to the right flavor of PatchInitValue, so that we can ensure that we only
  7962. // ever initialize to NULL in the right cases. But the backend uses the StFld opcode for initialization, and it
  7963. // would be cumbersome to thread the different helper calls all the way down
  7964. if (object->InitProperty(propertyId, newValue, flags, &info))
  7965. {
  7966. CacheOperators::CachePropertyWrite(object, false, typeWithoutProperty, propertyId, &info, scriptContext);
  7967. }
  7968. JIT_HELPER_END(Op_PatchInitValue);
  7969. }
  7970. JIT_HELPER_TEMPLATE(Op_PatchInitValue, Op_PatchInitValuePolymorphic)
  7971. template void JavascriptOperators::PatchInitValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7972. template void JavascriptOperators::PatchInitValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7973. template void JavascriptOperators::PatchInitValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7974. template void JavascriptOperators::PatchInitValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7975. void JavascriptOperators::PatchInitValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue)
  7976. {
  7977. PropertyValueInfo info;
  7978. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7979. Type *typeWithoutProperty = object->GetType();
  7980. if (functionBody->IsEval())
  7981. {
  7982. if (object->InitPropertyInEval(propertyId, newValue, PropertyOperation_None, &info))
  7983. {
  7984. CacheOperators::CachePropertyWrite(object, false, typeWithoutProperty, propertyId, &info, functionBody->GetScriptContext());
  7985. return;
  7986. }
  7987. }
  7988. if (object->InitProperty(propertyId, newValue, PropertyOperation_None, &info))
  7989. {
  7990. CacheOperators::CachePropertyWrite(object, false, typeWithoutProperty, propertyId, &info, functionBody->GetScriptContext());
  7991. }
  7992. }
  7993. void JavascriptOperators::GetPropertyIdForInt(uint64 value, ScriptContext* scriptContext, PropertyRecord const ** propertyRecord)
  7994. {
  7995. char16 buffer[20];
  7996. ::_ui64tow_s(value, buffer, sizeof(buffer)/sizeof(char16), 10);
  7997. scriptContext->GetOrAddPropertyRecord(buffer, JavascriptString::GetBufferLength(buffer), propertyRecord);
  7998. }
  7999. void JavascriptOperators::GetPropertyIdForInt(uint32 value, ScriptContext* scriptContext, PropertyRecord const ** propertyRecord)
  8000. {
  8001. GetPropertyIdForInt(static_cast<uint64>(value), scriptContext, propertyRecord);
  8002. }
  8003. Var JavascriptOperators::FromPropertyDescriptor(const PropertyDescriptor& descriptor, ScriptContext* scriptContext)
  8004. {
  8005. DynamicObject* object = scriptContext->GetLibrary()->CreateObject();
  8006. // ES5 Section 8.10.4 specifies the order for adding these properties.
  8007. if (descriptor.IsDataDescriptor())
  8008. {
  8009. if (descriptor.ValueSpecified())
  8010. {
  8011. JavascriptOperators::InitProperty(object, PropertyIds::value, descriptor.GetValue());
  8012. }
  8013. if (descriptor.WritableSpecified())
  8014. {
  8015. JavascriptOperators::InitProperty(object, PropertyIds::writable, JavascriptBoolean::ToVar(descriptor.IsWritable(), scriptContext));
  8016. }
  8017. }
  8018. else if (descriptor.IsAccessorDescriptor())
  8019. {
  8020. JavascriptOperators::InitProperty(object, PropertyIds::get, JavascriptOperators::CanonicalizeAccessor(descriptor.GetGetter(), scriptContext));
  8021. JavascriptOperators::InitProperty(object, PropertyIds::set, JavascriptOperators::CanonicalizeAccessor(descriptor.GetSetter(), scriptContext));
  8022. }
  8023. if (descriptor.EnumerableSpecified())
  8024. {
  8025. JavascriptOperators::InitProperty(object, PropertyIds::enumerable, JavascriptBoolean::ToVar(descriptor.IsEnumerable(), scriptContext));
  8026. }
  8027. if (descriptor.ConfigurableSpecified())
  8028. {
  8029. JavascriptOperators::InitProperty(object, PropertyIds::configurable, JavascriptBoolean::ToVar(descriptor.IsConfigurable(), scriptContext));
  8030. }
  8031. return object;
  8032. }
  8033. // ES5 8.12.9 [[DefineOwnProperty]].
  8034. // Return value:
  8035. // - TRUE = success.
  8036. // - FALSE (can throw depending on throwOnError parameter) = unsuccessful.
  8037. BOOL JavascriptOperators::DefineOwnPropertyDescriptor(RecyclableObject* obj, PropertyId propId, const PropertyDescriptor& descriptor, bool throwOnError, ScriptContext* scriptContext)
  8038. {
  8039. Assert(obj);
  8040. Assert(scriptContext);
  8041. if (VarIs<JavascriptProxy>(obj))
  8042. {
  8043. return JavascriptProxy::DefineOwnPropertyDescriptor(obj, propId, descriptor, throwOnError, scriptContext);
  8044. }
  8045. #ifdef _CHAKRACOREBUILD
  8046. else if (VarIs<CustomExternalWrapperObject>(obj))
  8047. {
  8048. // See if there is a trap for defineProperty.
  8049. BOOL wrapperResult = CustomExternalWrapperObject::DefineOwnPropertyDescriptor(obj, propId, descriptor, throwOnError, scriptContext);
  8050. if (wrapperResult)
  8051. {
  8052. return TRUE;
  8053. }
  8054. }
  8055. #endif
  8056. PropertyDescriptor currentDescriptor;
  8057. BOOL isCurrentDescriptorDefined = JavascriptOperators::GetOwnPropertyDescriptor(obj, propId, scriptContext, &currentDescriptor);
  8058. bool isExtensible = !!obj->IsExtensible();
  8059. return ValidateAndApplyPropertyDescriptor<true>(obj, propId, descriptor, isCurrentDescriptorDefined ? &currentDescriptor : nullptr, isExtensible, throwOnError, scriptContext);
  8060. }
  8061. BOOL JavascriptOperators::IsCompatiblePropertyDescriptor(const PropertyDescriptor& descriptor, PropertyDescriptor* currentDescriptor, bool isExtensible, bool throwOnError, ScriptContext* scriptContext)
  8062. {
  8063. return ValidateAndApplyPropertyDescriptor<false>(nullptr, Constants::NoProperty, descriptor, currentDescriptor, isExtensible, throwOnError, scriptContext);
  8064. }
  8065. template<bool needToSetProperty>
  8066. BOOL JavascriptOperators::ValidateAndApplyPropertyDescriptor(RecyclableObject* obj, PropertyId propId, const PropertyDescriptor& descriptor,
  8067. PropertyDescriptor* currentDescriptor, bool isExtensible, bool throwOnError, ScriptContext* scriptContext)
  8068. {
  8069. Var defaultDataValue = scriptContext->GetLibrary()->GetUndefined();
  8070. Var defaultAccessorValue = scriptContext->GetLibrary()->GetDefaultAccessorFunction();
  8071. if (currentDescriptor == nullptr)
  8072. {
  8073. if (!isExtensible) // ES5 8.12.9.3.
  8074. {
  8075. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotExtensible, propId);
  8076. }
  8077. else // ES5 8.12.9.4.
  8078. {
  8079. if (needToSetProperty)
  8080. {
  8081. if (descriptor.IsGenericDescriptor() || descriptor.IsDataDescriptor())
  8082. {
  8083. // ES5 8.12.9.4a: Create an own data property named P of object O whose [[Value]], [[Writable]],
  8084. // [[Enumerable]] and [[Configurable]] attribute values are described by Desc.
  8085. // If the value of an attribute field of Desc is absent, the attribute of the newly created property
  8086. // is set to its default value.
  8087. PropertyDescriptor filledDescriptor = FillMissingPropertyDescriptorFields<false>(descriptor, scriptContext);
  8088. BOOL tempResult = obj->SetPropertyWithAttributes(propId, filledDescriptor.GetValue(), filledDescriptor.GetAttributes(), nullptr);
  8089. if (!obj->IsExternal() && !tempResult)
  8090. {
  8091. Assert(
  8092. // Arrays return false when length property is non-writable and property is numeric
  8093. // and greater than or equal to length
  8094. DynamicObject::IsAnyArray(obj) ||
  8095. // Typed arrays return false when canonical numeric index is not integer or out of range
  8096. DynamicObject::IsAnyTypedArray(obj)
  8097. );
  8098. return FALSE;
  8099. }
  8100. }
  8101. else
  8102. {
  8103. // ES5 8.12.9.4b: Create an own accessor property named P of object O whose [[Get]], [[Set]], [[Enumerable]]
  8104. // and [[Configurable]] attribute values are described by Desc. If the value of an attribute field of Desc is absent,
  8105. // the attribute of the newly created property is set to its default value.
  8106. Assert(descriptor.IsAccessorDescriptor());
  8107. PropertyDescriptor filledDescriptor = FillMissingPropertyDescriptorFields<true>(descriptor, scriptContext);
  8108. BOOL isSetAccessorsSuccess = obj->SetAccessors(propId, filledDescriptor.GetGetter(), filledDescriptor.GetSetter());
  8109. // It is valid for some objects to not-support getters and setters, specifically, for projection of an ABI method
  8110. // (CustomExternalObject => MapWithStringKey) which SetAccessors returns VBSErr_ActionNotSupported.
  8111. // But for non-external objects SetAccessors should succeed.
  8112. Assert(isSetAccessorsSuccess || obj->IsExternal());
  8113. // If SetAccessors failed, the property wasn't created, so no need to change the attributes.
  8114. if (isSetAccessorsSuccess)
  8115. {
  8116. JavascriptOperators::SetAttributes(obj, propId, filledDescriptor, true); // use 'force' as default attributes in type system are different from ES5.
  8117. }
  8118. }
  8119. }
  8120. return TRUE;
  8121. }
  8122. }
  8123. // ES5 8.12.9.5: Return true, if every field in Desc is absent.
  8124. if (!descriptor.ConfigurableSpecified() && !descriptor.EnumerableSpecified() && !descriptor.WritableSpecified() &&
  8125. !descriptor.ValueSpecified() && !descriptor.GetterSpecified() && !descriptor.SetterSpecified())
  8126. {
  8127. return TRUE;
  8128. }
  8129. // ES5 8.12.9.6: Return true, if every field in Desc also occurs in current and the value of every field in Desc is the same value
  8130. // as the corresponding field in current when compared using the SameValue algorithm (9.12).
  8131. PropertyDescriptor filledDescriptor = descriptor.IsAccessorDescriptor() ? FillMissingPropertyDescriptorFields<true>(descriptor, scriptContext)
  8132. : FillMissingPropertyDescriptorFields<false>(descriptor, scriptContext);
  8133. if (JavascriptOperators::AreSamePropertyDescriptors(&filledDescriptor, currentDescriptor, scriptContext))
  8134. {
  8135. return TRUE;
  8136. }
  8137. if (!currentDescriptor->IsConfigurable()) // ES5 8.12.9.7.
  8138. {
  8139. if (descriptor.ConfigurableSpecified() && descriptor.IsConfigurable())
  8140. {
  8141. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8142. }
  8143. if (descriptor.EnumerableSpecified() && descriptor.IsEnumerable() != currentDescriptor->IsEnumerable())
  8144. {
  8145. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8146. }
  8147. }
  8148. // Whether to merge attributes from tempDescriptor into descriptor to keep original values
  8149. // of some attributes from the object/use tempDescriptor for SetAttributes, or just use descriptor.
  8150. // This is optimization to avoid 2 calls to SetAttributes.
  8151. bool mergeDescriptors = false;
  8152. // Whether to call SetAttributes with 'force' flag which forces setting all attributes
  8153. // rather than only specified or which have true values.
  8154. // This is to make sure that the object has correct attributes, as default values in the object are not for ES5.
  8155. bool forceSetAttributes = false;
  8156. PropertyDescriptor tempDescriptor;
  8157. // ES5 8.12.9.8: If IsGenericDescriptor(Desc) is true, then no further validation is required.
  8158. if (!descriptor.IsGenericDescriptor())
  8159. {
  8160. if (currentDescriptor->IsDataDescriptor() != descriptor.IsDataDescriptor())
  8161. {
  8162. // ES5 8.12.9.9: Else, if IsDataDescriptor(current) and IsDataDescriptor(Desc) have different results...
  8163. if (!currentDescriptor->IsConfigurable())
  8164. {
  8165. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8166. }
  8167. if (needToSetProperty)
  8168. {
  8169. if (currentDescriptor->IsDataDescriptor())
  8170. {
  8171. // ES5 8.12.9.9.b: Convert the property named P of object O from a data property to an accessor property.
  8172. // Preserve the existing values of the converted property's [[Configurable]] and [[Enumerable]] attributes
  8173. // and set the rest of the property's attributes to their default values.
  8174. PropertyAttributes preserveFromObject = currentDescriptor->GetAttributes() & (PropertyConfigurable | PropertyEnumerable);
  8175. BOOL isSetAccessorsSuccess = obj->SetAccessors(propId, defaultAccessorValue, defaultAccessorValue);
  8176. // It is valid for some objects to not-support getters and setters, specifically, for projection of an ABI method
  8177. // (CustomExternalObject => MapWithStringKey) which SetAccessors returns VBSErr_ActionNotSupported.
  8178. // But for non-external objects SetAccessors should succeed.
  8179. Assert(isSetAccessorsSuccess || obj->IsExternal());
  8180. if (isSetAccessorsSuccess)
  8181. {
  8182. tempDescriptor.SetAttributes(preserveFromObject, PropertyConfigurable | PropertyEnumerable);
  8183. forceSetAttributes = true; // use SetAttributes with 'force' as default attributes in type system are different from ES5.
  8184. mergeDescriptors = true;
  8185. }
  8186. }
  8187. else
  8188. {
  8189. // ES5 8.12.9.9.c: Convert the property named P of object O from an accessor property to a data property.
  8190. // Preserve the existing values of the converted property's [[Configurable]] and [[Enumerable]] attributes
  8191. // and set the rest of the property's attributes to their default values.
  8192. // Note: avoid using SetProperty/SetPropertyWithAttributes here because they has undesired side-effects:
  8193. // it calls previous setter and in some cases of attribute values throws.
  8194. // To walk around, call DeleteProperty and then AddProperty.
  8195. PropertyAttributes preserveFromObject = currentDescriptor->GetAttributes() & (PropertyConfigurable | PropertyEnumerable);
  8196. tempDescriptor.SetAttributes(preserveFromObject, PropertyConfigurable | PropertyEnumerable);
  8197. tempDescriptor.MergeFrom(descriptor); // Update only fields specified in 'descriptor'.
  8198. Var descriptorValue = descriptor.ValueSpecified() ? descriptor.GetValue() : defaultDataValue;
  8199. // Note: HostDispath'es implementation of DeleteProperty currently throws E_NOTIMPL.
  8200. obj->DeleteProperty(propId, PropertyOperation_None);
  8201. BOOL tempResult = obj->SetPropertyWithAttributes(propId, descriptorValue, tempDescriptor.GetAttributes(), NULL, PropertyOperation_Force);
  8202. Assert(tempResult);
  8203. // At this time we already set value and attributes to desired values,
  8204. // thus we can skip step ES5 8.12.9.12 and simply return true.
  8205. return TRUE;
  8206. }
  8207. }
  8208. }
  8209. else if (currentDescriptor->IsDataDescriptor() && descriptor.IsDataDescriptor())
  8210. {
  8211. // ES5 8.12.9.10: Else, if IsDataDescriptor(current) and IsDataDescriptor(Desc) are both true...
  8212. if (!currentDescriptor->IsConfigurable())
  8213. {
  8214. if (!currentDescriptor->IsWritable())
  8215. {
  8216. if (descriptor.WritableSpecified() && descriptor.IsWritable()) // ES5 8.12.9.10.a.i
  8217. {
  8218. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8219. }
  8220. else if (descriptor.ValueSpecified() &&
  8221. !JavascriptConversion::SameValue(descriptor.GetValue(), currentDescriptor->GetValue())) // ES5 8.12.9.10.a.ii
  8222. {
  8223. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotWritable, propId);
  8224. }
  8225. }
  8226. }
  8227. // ES5 8.12.9.10.b: else, the [[Configurable]] field of current is true, so any change is acceptable.
  8228. }
  8229. else
  8230. {
  8231. // ES5 8.12.9.11: Else, IsAccessorDescriptor(current) and IsAccessorDescriptor(Desc) are both true, so...
  8232. Assert(currentDescriptor->IsAccessorDescriptor() && descriptor.IsAccessorDescriptor());
  8233. if (!currentDescriptor->IsConfigurable())
  8234. {
  8235. if ((descriptor.SetterSpecified() &&
  8236. !JavascriptConversion::SameValue(
  8237. JavascriptOperators::CanonicalizeAccessor(descriptor.GetSetter(), scriptContext),
  8238. JavascriptOperators::CanonicalizeAccessor(currentDescriptor->GetSetter(), scriptContext))) ||
  8239. (descriptor.GetterSpecified() &&
  8240. !JavascriptConversion::SameValue(
  8241. JavascriptOperators::CanonicalizeAccessor(descriptor.GetGetter(), scriptContext),
  8242. JavascriptOperators::CanonicalizeAccessor(currentDescriptor->GetGetter(), scriptContext))))
  8243. {
  8244. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8245. }
  8246. }
  8247. }
  8248. // This part is only for non-generic descriptors:
  8249. // ES5 8.12.9.12: For each attribute field of Desc that is present,
  8250. // set the correspondingly named attribute of the property named P of object O to the value of the field.
  8251. if (descriptor.IsDataDescriptor())
  8252. {
  8253. if (descriptor.ValueSpecified() && needToSetProperty)
  8254. {
  8255. // Set just the value by passing the current attributes of the property.
  8256. // If the property's attributes are also changing (perhaps becoming non-writable),
  8257. // this will be taken care of in the call to JavascriptOperators::SetAttributes below.
  8258. // Built-in Function.prototype properties 'length', 'arguments', and 'caller' are special cases.
  8259. BOOL tempResult = obj->SetPropertyWithAttributes(propId, descriptor.GetValue(), currentDescriptor->GetAttributes(), nullptr);
  8260. AssertMsg(tempResult || JavascriptFunction::IsBuiltinProperty(obj, propId), "If you hit this assert, most likely there is something wrong with the object/type.");
  8261. }
  8262. }
  8263. else if (descriptor.IsAccessorDescriptor() && needToSetProperty)
  8264. {
  8265. Assert(descriptor.GetterSpecified() || descriptor.SetterSpecified());
  8266. Var oldGetter = defaultAccessorValue, oldSetter = defaultAccessorValue;
  8267. if (!descriptor.GetterSpecified() || !descriptor.SetterSpecified())
  8268. {
  8269. // Unless both getter and setter are specified, make sure we don't overwrite old accessor.
  8270. #pragma prefast(suppress:6031, "We defaulted oldGetter and oldSetter already, so ignoring the return value here is safe")
  8271. obj->GetAccessors(propId, &oldGetter, &oldSetter, scriptContext);
  8272. }
  8273. Var getter = descriptor.GetterSpecified() ? descriptor.GetGetter() : oldGetter;
  8274. Var setter = descriptor.SetterSpecified() ? descriptor.GetSetter() : oldSetter;
  8275. obj->SetAccessors(propId, getter, setter);
  8276. }
  8277. } // if (!descriptor.IsGenericDescriptor())
  8278. // Continue for all descriptors including generic:
  8279. // ES5 8.12.9.12: For each attribute field of Desc that is present,
  8280. // set the correspondingly named attribute of the property named P of object O to the value of the field.
  8281. if (needToSetProperty)
  8282. {
  8283. if (mergeDescriptors)
  8284. {
  8285. tempDescriptor.MergeFrom(descriptor);
  8286. JavascriptOperators::SetAttributes(obj, propId, tempDescriptor, forceSetAttributes);
  8287. }
  8288. else
  8289. {
  8290. JavascriptOperators::SetAttributes(obj, propId, descriptor, forceSetAttributes);
  8291. }
  8292. }
  8293. return TRUE;
  8294. }
  8295. template <bool isAccessor>
  8296. PropertyDescriptor JavascriptOperators::FillMissingPropertyDescriptorFields(PropertyDescriptor descriptor, ScriptContext* scriptContext)
  8297. {
  8298. PropertyDescriptor newDescriptor;
  8299. const PropertyDescriptor* defaultDescriptor = scriptContext->GetLibrary()->GetDefaultPropertyDescriptor();
  8300. if (isAccessor)
  8301. {
  8302. newDescriptor.SetGetter(descriptor.GetterSpecified() ? descriptor.GetGetter() : defaultDescriptor->GetGetter());
  8303. newDescriptor.SetSetter(descriptor.SetterSpecified() ? descriptor.GetSetter() : defaultDescriptor->GetSetter());
  8304. }
  8305. else
  8306. {
  8307. newDescriptor.SetValue(descriptor.ValueSpecified() ? descriptor.GetValue() : defaultDescriptor->GetValue());
  8308. newDescriptor.SetWritable(descriptor.WritableSpecified() ? descriptor.IsWritable() : defaultDescriptor->IsWritable());
  8309. }
  8310. newDescriptor.SetConfigurable(descriptor.ConfigurableSpecified() ? descriptor.IsConfigurable() : defaultDescriptor->IsConfigurable());
  8311. newDescriptor.SetEnumerable(descriptor.EnumerableSpecified() ? descriptor.IsEnumerable() : defaultDescriptor->IsEnumerable());
  8312. return newDescriptor;
  8313. }
  8314. // ES5: 15.4.5.1
  8315. BOOL JavascriptOperators::DefineOwnPropertyForArray(JavascriptArray* arr, PropertyId propId, const PropertyDescriptor& descriptor, bool throwOnError, ScriptContext* scriptContext)
  8316. {
  8317. if (propId == PropertyIds::length)
  8318. {
  8319. if (!descriptor.ValueSpecified())
  8320. {
  8321. return DefineOwnPropertyDescriptor(arr, PropertyIds::length, descriptor, throwOnError, scriptContext);
  8322. }
  8323. PropertyDescriptor newLenDesc = descriptor;
  8324. uint32 newLen = ES5Array::ToLengthValue(descriptor.GetValue(), scriptContext);
  8325. newLenDesc.SetValue(JavascriptNumber::ToVar(newLen, scriptContext));
  8326. uint32 oldLen = arr->GetLength();
  8327. if (newLen >= oldLen)
  8328. {
  8329. return DefineOwnPropertyDescriptor(arr, PropertyIds::length, newLenDesc, throwOnError, scriptContext);
  8330. }
  8331. BOOL oldLenWritable = arr->IsWritable(PropertyIds::length);
  8332. if (!oldLenWritable)
  8333. {
  8334. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotWritable, propId);
  8335. }
  8336. bool newWritable = (!newLenDesc.WritableSpecified() || newLenDesc.IsWritable());
  8337. if (!newWritable)
  8338. {
  8339. // Need to defer setting writable to false in case any elements cannot be deleted
  8340. newLenDesc.SetWritable(true);
  8341. }
  8342. BOOL succeeded = DefineOwnPropertyDescriptor(arr, PropertyIds::length, newLenDesc, throwOnError, scriptContext);
  8343. //
  8344. // Our SetProperty(length) is also responsible to trim elements. When succeeded is
  8345. //
  8346. // false:
  8347. // * length attributes rejected
  8348. // * elements not touched
  8349. // true:
  8350. // * length attributes are set successfully
  8351. // * elements trimming may be either completed or incompleted, length value is correct
  8352. //
  8353. // * Strict mode TODO: Currently SetProperty(length) does not throw. If that throws, we need
  8354. // to update here to set correct newWritable even on exception.
  8355. //
  8356. if (!succeeded)
  8357. {
  8358. return false;
  8359. }
  8360. if (!newWritable) // Now set requested newWritable.
  8361. {
  8362. PropertyDescriptor newWritableDesc;
  8363. newWritableDesc.SetWritable(false);
  8364. DefineOwnPropertyDescriptor(arr, PropertyIds::length, newWritableDesc, false, scriptContext);
  8365. }
  8366. if (arr->GetLength() > newLen) // Delete incompleted
  8367. {
  8368. // Since SetProperty(length) not throwing, we'll reject here
  8369. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_Default, propId);
  8370. }
  8371. return true;
  8372. }
  8373. uint32 index;
  8374. if (scriptContext->IsNumericPropertyId(propId, &index))
  8375. {
  8376. if (index >= arr->GetLength() && !arr->IsWritable(PropertyIds::length))
  8377. {
  8378. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_LengthNotWritable, propId);
  8379. }
  8380. BOOL succeeded = DefineOwnPropertyDescriptor(arr, propId, descriptor, false, scriptContext);
  8381. if (!succeeded)
  8382. {
  8383. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_Default, propId);
  8384. }
  8385. // Out SetItem takes care of growing "length". we are done.
  8386. return true;
  8387. }
  8388. return DefineOwnPropertyDescriptor(arr, propId, descriptor, throwOnError, scriptContext);
  8389. }
  8390. // ES2017: 9.4.5.3 https://tc39.github.io/ecma262/#sec-integer-indexed-exotic-objects-defineownproperty-p-desc
  8391. BOOL JavascriptOperators::DefineOwnPropertyForTypedArray(TypedArrayBase* typedArray, PropertyId propId, const PropertyDescriptor& descriptor, bool throwOnError, ScriptContext* scriptContext)
  8392. {
  8393. // 1. Assert: IsPropertyKey(P) is true.
  8394. // 2. Assert: Assert: O is an Object that has a [[ViewedArrayBuffer]] internal slot.
  8395. const PropertyRecord* propertyRecord = scriptContext->GetPropertyName(propId);
  8396. // 3. If Type(P) is String, then
  8397. // a. Let numericIndex be ! CanonicalNumericIndexString(P).
  8398. // b. If numericIndex is not undefined, then
  8399. // i. if IsInteger(numbericIndex), return false
  8400. // ii. if numbericIndex = -0, return false
  8401. // iii. If numericIndex < 0, return false.
  8402. if (propertyRecord->IsNumeric()) {
  8403. uint32 uint32Index = propertyRecord->GetNumericValue();
  8404. // iv. Let length be O.[[ArrayLength]].
  8405. uint32 length = typedArray->GetLength();
  8406. // v. If numericIndex >= length, return false.
  8407. if (uint32Index >= length)
  8408. {
  8409. return Reject(throwOnError, scriptContext, JSERR_InvalidTypedArrayIndex, propId);
  8410. }
  8411. // vi. If IsAccessorDescriptor(Desc) is true, return false.
  8412. // vii. If Desc has a[[Configurable]] field and if Desc.[[Configurable]] is true, return false.
  8413. // viii. If Desc has an[[Enumerable]] field and if Desc.[[Enumerable]] is false, return false.
  8414. // ix. If Desc has a[[Writable]] field and if Desc.[[Writable]] is false, return false.
  8415. if (descriptor.IsAccessorDescriptor()
  8416. || (descriptor.ConfigurableSpecified() && descriptor.IsConfigurable())
  8417. || (descriptor.EnumerableSpecified() && !descriptor.IsEnumerable())
  8418. || (descriptor.WritableSpecified() && !descriptor.IsWritable()))
  8419. {
  8420. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8421. } // x. If Desc has a[[Value]] field, then
  8422. // 1. Let value be Desc.[[Value]].
  8423. // 2. Return ? IntegerIndexedElementSet(O, numericIndex, value).
  8424. if (descriptor.ValueSpecified())
  8425. {
  8426. Js::Var value = descriptor.GetValue();
  8427. return typedArray->DirectSetItem(uint32Index, value);
  8428. }
  8429. // xi. Return true.
  8430. return true;
  8431. }
  8432. if (!propertyRecord->IsSymbol())
  8433. {
  8434. PropertyString *propertyString = scriptContext->GetPropertyString(propId);
  8435. double result;
  8436. if (JavascriptConversion::CanonicalNumericIndexString(propertyString, &result, scriptContext))
  8437. {
  8438. return Reject(throwOnError, scriptContext, JSERR_InvalidTypedArrayIndex, propId);
  8439. }
  8440. }
  8441. // 4. Return ! OrdinaryDefineOwnProperty(O, P, Desc).
  8442. return DefineOwnPropertyDescriptor(typedArray, propId, descriptor, throwOnError, scriptContext);
  8443. }
  8444. BOOL JavascriptOperators::SetPropertyDescriptor(RecyclableObject* object, PropertyId propId, const PropertyDescriptor& descriptor)
  8445. {
  8446. if (descriptor.ValueSpecified())
  8447. {
  8448. ScriptContext* requestContext = object->GetScriptContext(); // Real requestContext?
  8449. JavascriptOperators::SetProperty(object, object, propId, descriptor.GetValue(), requestContext);
  8450. }
  8451. else if (descriptor.GetterSpecified() || descriptor.SetterSpecified())
  8452. {
  8453. JavascriptOperators::SetAccessors(object, propId, descriptor.GetGetter(), descriptor.GetSetter());
  8454. }
  8455. if (descriptor.EnumerableSpecified())
  8456. {
  8457. object->SetEnumerable(propId, descriptor.IsEnumerable());
  8458. }
  8459. if (descriptor.ConfigurableSpecified())
  8460. {
  8461. object->SetConfigurable(propId, descriptor.IsConfigurable());
  8462. }
  8463. if (descriptor.WritableSpecified())
  8464. {
  8465. object->SetWritable(propId, descriptor.IsWritable());
  8466. }
  8467. return true;
  8468. }
  8469. BOOL JavascriptOperators::ToPropertyDescriptorForProxyObjects(Var propertySpec, PropertyDescriptor* descriptor, ScriptContext* scriptContext)
  8470. {
  8471. if (!JavascriptOperators::IsObject(propertySpec))
  8472. {
  8473. return FALSE;
  8474. }
  8475. Var value;
  8476. RecyclableObject* propertySpecObj = VarTo<RecyclableObject>(propertySpec);
  8477. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::enumerable) == TRUE)
  8478. {
  8479. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::enumerable, &value, scriptContext))
  8480. {
  8481. descriptor->SetEnumerable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8482. }
  8483. else
  8484. {
  8485. // The proxy said we have the property, so we try to read the property and get the default value.
  8486. descriptor->SetEnumerable(false);
  8487. }
  8488. }
  8489. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::configurable) == TRUE)
  8490. {
  8491. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::configurable, &value, scriptContext))
  8492. {
  8493. descriptor->SetConfigurable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8494. }
  8495. else
  8496. {
  8497. // The proxy said we have the property, so we try to read the property and get the default value.
  8498. descriptor->SetConfigurable(false);
  8499. }
  8500. }
  8501. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::value) == TRUE)
  8502. {
  8503. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::value, &value, scriptContext))
  8504. {
  8505. descriptor->SetValue(value);
  8506. }
  8507. else
  8508. {
  8509. // The proxy said we have the property, so we try to read the property and get the default value.
  8510. descriptor->SetValue(scriptContext->GetLibrary()->GetUndefined());
  8511. }
  8512. }
  8513. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::writable) == TRUE)
  8514. {
  8515. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::writable, &value, scriptContext))
  8516. {
  8517. descriptor->SetWritable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8518. }
  8519. else
  8520. {
  8521. // The proxy said we have the property, so we try to read the property and get the default value.
  8522. descriptor->SetWritable(false);
  8523. }
  8524. }
  8525. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::get) == TRUE)
  8526. {
  8527. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::get, &value, scriptContext))
  8528. {
  8529. if (JavascriptOperators::GetTypeId(value) != TypeIds_Undefined && (false == JavascriptConversion::IsCallable(value)))
  8530. {
  8531. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::get)->GetBuffer());
  8532. }
  8533. descriptor->SetGetter(value);
  8534. }
  8535. else
  8536. {
  8537. // The proxy said we have the property, so we try to read the property and get the default value.
  8538. descriptor->SetGetter(scriptContext->GetLibrary()->GetUndefined());
  8539. }
  8540. }
  8541. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::set) == TRUE)
  8542. {
  8543. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::set, &value, scriptContext))
  8544. {
  8545. if (JavascriptOperators::GetTypeId(value) != TypeIds_Undefined && (false == JavascriptConversion::IsCallable(value)))
  8546. {
  8547. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::set)->GetBuffer());
  8548. }
  8549. descriptor->SetSetter(value);
  8550. }
  8551. else
  8552. {
  8553. // The proxy said we have the property, so we try to read the property and get the default value.
  8554. descriptor->SetSetter(scriptContext->GetLibrary()->GetUndefined());
  8555. }
  8556. }
  8557. return TRUE;
  8558. }
  8559. BOOL JavascriptOperators::ToPropertyDescriptorForGenericObjects(Var propertySpec, PropertyDescriptor* descriptor, ScriptContext* scriptContext)
  8560. {
  8561. if (!JavascriptOperators::IsObject(propertySpec))
  8562. {
  8563. return FALSE;
  8564. }
  8565. Var value;
  8566. RecyclableObject* propertySpecObj = VarTo<RecyclableObject>(propertySpec);
  8567. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::enumerable, &value, scriptContext))
  8568. {
  8569. descriptor->SetEnumerable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8570. }
  8571. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::configurable, &value, scriptContext))
  8572. {
  8573. descriptor->SetConfigurable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8574. }
  8575. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::value, &value, scriptContext))
  8576. {
  8577. descriptor->SetValue(value);
  8578. }
  8579. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::writable, &value, scriptContext))
  8580. {
  8581. descriptor->SetWritable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8582. }
  8583. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::get, &value, scriptContext))
  8584. {
  8585. if (JavascriptOperators::GetTypeId(value) != TypeIds_Undefined && (false == JavascriptConversion::IsCallable(value)))
  8586. {
  8587. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::get)->GetBuffer());
  8588. }
  8589. descriptor->SetGetter(value);
  8590. }
  8591. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::set, &value, scriptContext))
  8592. {
  8593. if (JavascriptOperators::GetTypeId(value) != TypeIds_Undefined && (false == JavascriptConversion::IsCallable(value)))
  8594. {
  8595. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::set)->GetBuffer());
  8596. }
  8597. descriptor->SetSetter(value);
  8598. }
  8599. return TRUE;
  8600. }
  8601. BOOL JavascriptOperators::ToPropertyDescriptor(Var propertySpec, PropertyDescriptor* descriptor, ScriptContext* scriptContext)
  8602. {
  8603. if (VarIs<JavascriptProxy>(propertySpec) || (
  8604. VarIs<RecyclableObject>(propertySpec) &&
  8605. JavascriptOperators::CheckIfPrototypeChainContainsProxyObject(VarTo<RecyclableObject>(propertySpec)->GetPrototype())))
  8606. {
  8607. if (ToPropertyDescriptorForProxyObjects(propertySpec, descriptor, scriptContext) == FALSE)
  8608. {
  8609. return FALSE;
  8610. }
  8611. }
  8612. else
  8613. {
  8614. if (ToPropertyDescriptorForGenericObjects(propertySpec, descriptor, scriptContext) == FALSE)
  8615. {
  8616. return FALSE;
  8617. }
  8618. }
  8619. if (descriptor->GetterSpecified() || descriptor->SetterSpecified())
  8620. {
  8621. if (descriptor->ValueSpecified())
  8622. {
  8623. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotHaveAccessorsAndValue);
  8624. }
  8625. if (descriptor->WritableSpecified())
  8626. {
  8627. int32 hCode = descriptor->IsWritable() ? JSERR_InvalidAttributeTrue : JSERR_InvalidAttributeFalse;
  8628. JavascriptError::ThrowTypeError(scriptContext, hCode, _u("writable"));
  8629. }
  8630. }
  8631. descriptor->SetOriginal(propertySpec);
  8632. return TRUE;
  8633. }
  8634. void JavascriptOperators::CompletePropertyDescriptor(PropertyDescriptor* resultDescriptor, PropertyDescriptor* likeDescriptor, ScriptContext* requestContext)
  8635. {
  8636. const PropertyDescriptor* likePropertyDescriptor = likeDescriptor;
  8637. // 1. Assert: LikeDesc is either a Property Descriptor or undefined.
  8638. // 2. ReturnIfAbrupt(Desc).
  8639. // 3. Assert : Desc is a Property Descriptor
  8640. // 4. If LikeDesc is undefined, then set LikeDesc to Record{ [[Value]]: undefined, [[Writable]] : false, [[Get]] : undefined, [[Set]] : undefined, [[Enumerable]] : false, [[Configurable]] : false }.
  8641. if (likePropertyDescriptor == nullptr)
  8642. {
  8643. likePropertyDescriptor = requestContext->GetLibrary()->GetDefaultPropertyDescriptor();
  8644. }
  8645. // 5. If either IsGenericDescriptor(Desc) or IsDataDescriptor(Desc) is true, then
  8646. if (resultDescriptor->IsDataDescriptor() || resultDescriptor->IsGenericDescriptor())
  8647. {
  8648. // a.If Desc does not have a[[Value]] field, then set Desc.[[Value]] to LikeDesc.[[Value]].
  8649. // b.If Desc does not have a[[Writable]] field, then set Desc.[[Writable]] to LikeDesc.[[Writable]].
  8650. if (!resultDescriptor->ValueSpecified())
  8651. {
  8652. resultDescriptor->SetValue(likePropertyDescriptor->GetValue());
  8653. }
  8654. if (!resultDescriptor->WritableSpecified())
  8655. {
  8656. resultDescriptor->SetWritable(likePropertyDescriptor->IsWritable());
  8657. }
  8658. }
  8659. else
  8660. {
  8661. // 6. Else,
  8662. // a.If Desc does not have a[[Get]] field, then set Desc.[[Get]] to LikeDesc.[[Get]].
  8663. // b.If Desc does not have a[[Set]] field, then set Desc.[[Set]] to LikeDesc.[[Set]].
  8664. if (!resultDescriptor->GetterSpecified())
  8665. {
  8666. resultDescriptor->SetGetter(likePropertyDescriptor->GetGetter());
  8667. }
  8668. if (!resultDescriptor->SetterSpecified())
  8669. {
  8670. resultDescriptor->SetSetter(likePropertyDescriptor->GetSetter());
  8671. }
  8672. }
  8673. // 7. If Desc does not have an[[Enumerable]] field, then set Desc.[[Enumerable]] to LikeDesc.[[Enumerable]].
  8674. // 8. If Desc does not have a[[Configurable]] field, then set Desc.[[Configurable]] to LikeDesc.[[Configurable]].
  8675. // 9. Return Desc.
  8676. if (!resultDescriptor->EnumerableSpecified())
  8677. {
  8678. resultDescriptor->SetEnumerable(likePropertyDescriptor->IsEnumerable());
  8679. }
  8680. if (!resultDescriptor->ConfigurableSpecified())
  8681. {
  8682. resultDescriptor->SetConfigurable(likePropertyDescriptor->IsConfigurable());
  8683. }
  8684. }
  8685. // Conformance to: ES5 8.6.1.
  8686. // Set attributes on the object as provided by property descriptor.
  8687. // If force parameter is true, we force SetAttributes call even if none of the attributes are defined by the descriptor.
  8688. // NOTE: does not set [[Get]], [Set]], [[Value]]
  8689. void JavascriptOperators::SetAttributes(RecyclableObject* object, PropertyId propId, const PropertyDescriptor& descriptor, bool force)
  8690. {
  8691. Assert(object);
  8692. BOOL isWritable = FALSE;
  8693. if (descriptor.IsDataDescriptor())
  8694. {
  8695. isWritable = descriptor.WritableSpecified() ? descriptor.IsWritable() : FALSE;
  8696. }
  8697. else if (descriptor.IsAccessorDescriptor())
  8698. {
  8699. // The reason is that JavascriptOperators::OP_SetProperty checks for VarTo<RecyclableObject>(instance)->IsWritableOrAccessor(propertyId),
  8700. // which should in fact check for 'is writable or accessor' but since there is no GetAttributes, we can't do that efficiently.
  8701. isWritable = TRUE;
  8702. }
  8703. // CONSIDER: call object->SetAttributes which is much more efficient as that's 1 call instead of 3.
  8704. // Can't do that now as object->SetAttributes doesn't provide a way which attributes to modify and which not.
  8705. if (force || descriptor.ConfigurableSpecified())
  8706. {
  8707. object->SetConfigurable(propId, descriptor.ConfigurableSpecified() ? descriptor.IsConfigurable() : FALSE);
  8708. }
  8709. if (force || descriptor.EnumerableSpecified())
  8710. {
  8711. object->SetEnumerable(propId, descriptor.EnumerableSpecified() ? descriptor.IsEnumerable() : FALSE);
  8712. }
  8713. if (force || descriptor.WritableSpecified() || isWritable)
  8714. {
  8715. object->SetWritable(propId, isWritable);
  8716. }
  8717. }
  8718. void JavascriptOperators::OP_ClearAttributes(Var instance, PropertyId propertyId)
  8719. {
  8720. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_ClearAttributes);
  8721. Assert(instance);
  8722. if (VarIs<RecyclableObject>(instance))
  8723. {
  8724. RecyclableObject* obj = VarTo<RecyclableObject>(instance);
  8725. obj->SetAttributes(propertyId, PropertyNone);
  8726. }
  8727. JIT_HELPER_END(OP_ClearAttributes);
  8728. }
  8729. BOOL JavascriptOperators::Reject(bool throwOnError, ScriptContext* scriptContext, int32 errorCode, PropertyId propertyId)
  8730. {
  8731. Assert(scriptContext);
  8732. if (throwOnError)
  8733. {
  8734. JavascriptError::ThrowTypeError(scriptContext, errorCode, scriptContext->GetThreadContext()->GetPropertyName(propertyId)->GetBuffer());
  8735. }
  8736. return FALSE;
  8737. }
  8738. bool JavascriptOperators::AreSamePropertyDescriptors(const PropertyDescriptor* x, const PropertyDescriptor* y, ScriptContext* scriptContext)
  8739. {
  8740. Assert(scriptContext);
  8741. if (x->ConfigurableSpecified() != y->ConfigurableSpecified() || x->IsConfigurable() != y->IsConfigurable() ||
  8742. x->EnumerableSpecified() != y->EnumerableSpecified() || x->IsEnumerable() != y->IsEnumerable())
  8743. {
  8744. return false;
  8745. }
  8746. if (x->IsDataDescriptor())
  8747. {
  8748. if (!y->IsDataDescriptor() || x->WritableSpecified() != y->WritableSpecified() || x->IsWritable() != y->IsWritable())
  8749. {
  8750. return false;
  8751. }
  8752. if (x->ValueSpecified())
  8753. {
  8754. if (!y->ValueSpecified() || !JavascriptConversion::SameValue(x->GetValue(), y->GetValue()))
  8755. {
  8756. return false;
  8757. }
  8758. }
  8759. }
  8760. else if (x->IsAccessorDescriptor())
  8761. {
  8762. if (!y->IsAccessorDescriptor())
  8763. {
  8764. return false;
  8765. }
  8766. if (x->GetterSpecified())
  8767. {
  8768. if (!y->GetterSpecified() || !JavascriptConversion::SameValue(
  8769. JavascriptOperators::CanonicalizeAccessor(x->GetGetter(), scriptContext),
  8770. JavascriptOperators::CanonicalizeAccessor(y->GetGetter(), scriptContext)))
  8771. {
  8772. return false;
  8773. }
  8774. }
  8775. if (x->SetterSpecified())
  8776. {
  8777. if (!y->SetterSpecified() || !JavascriptConversion::SameValue(
  8778. JavascriptOperators::CanonicalizeAccessor(x->GetSetter(), scriptContext),
  8779. JavascriptOperators::CanonicalizeAccessor(y->GetSetter(), scriptContext)))
  8780. {
  8781. return false;
  8782. }
  8783. }
  8784. }
  8785. return true;
  8786. }
  8787. // Check if an accessor is undefined (null or defaultAccessor)
  8788. bool JavascriptOperators::IsUndefinedAccessor(Var accessor, ScriptContext* scriptContext)
  8789. {
  8790. return nullptr == accessor || scriptContext->GetLibrary()->GetDefaultAccessorFunction() == accessor;
  8791. }
  8792. // Converts default accessor to undefined.
  8793. // Can be used when comparing accessors.
  8794. Var JavascriptOperators::CanonicalizeAccessor(Var accessor, ScriptContext* scriptContext)
  8795. {
  8796. Assert(scriptContext);
  8797. if (IsUndefinedAccessor(accessor, scriptContext))
  8798. {
  8799. return scriptContext->GetLibrary()->GetUndefined();
  8800. }
  8801. return accessor;
  8802. }
  8803. Var JavascriptOperators::DefaultAccessor(RecyclableObject* function, CallInfo callInfo, ...)
  8804. {
  8805. return function->GetLibrary()->GetUndefined();
  8806. }
  8807. void FrameDisplay::SetItem(uint index, void* item)
  8808. {
  8809. AssertMsg(index < this->length, "Invalid frame display access");
  8810. scopes[index] = item;
  8811. }
  8812. void *FrameDisplay::GetItem(uint index)
  8813. {
  8814. AssertMsg(index < this->length, "Invalid frame display access");
  8815. return scopes[index];
  8816. }
  8817. // Grab the "this" pointer, mapping a root object to its associated host object.
  8818. Var JavascriptOperators::RootToThisObject(const Var object, ScriptContext* scriptContext)
  8819. {
  8820. Js::Var thisVar = object;
  8821. TypeId typeId = Js::JavascriptOperators::GetTypeId(thisVar);
  8822. switch (typeId)
  8823. {
  8824. case Js::TypeIds_GlobalObject:
  8825. return ((Js::GlobalObject*)thisVar)->ToThis();
  8826. case Js::TypeIds_ModuleRoot:
  8827. return Js::JavascriptOperators::GetThisFromModuleRoot(thisVar);
  8828. default:
  8829. if (typeId == scriptContext->GetDirectHostTypeId())
  8830. {
  8831. return ((RecyclableObject*)thisVar)->GetLibrary()->GetGlobalObject()->ToThis();
  8832. }
  8833. }
  8834. return thisVar;
  8835. }
  8836. Var JavascriptOperators::CallGetter(RecyclableObject * const function, Var const object, ScriptContext * requestContext)
  8837. {
  8838. #if ENABLE_TTD
  8839. if(function->GetScriptContext()->ShouldSuppressGetterInvocationForDebuggerEvaluation())
  8840. {
  8841. return requestContext->GetLibrary()->GetUndefined();
  8842. }
  8843. #endif
  8844. ScriptContext * scriptContext = function->GetScriptContext();
  8845. ThreadContext * threadContext = scriptContext->GetThreadContext();
  8846. return threadContext->ExecuteImplicitCall(function, ImplicitCall_Accessor, [=]() -> Js::Var
  8847. {
  8848. // Stack object should have a pre-op bail on implicit call. We shouldn't see them here.
  8849. // Stack numbers are ok, as we will call ToObject to wrap it in a number object anyway
  8850. // See JavascriptOperators::GetThisHelper
  8851. Assert(JavascriptOperators::GetTypeId(object) == TypeIds_Integer ||
  8852. JavascriptOperators::GetTypeId(object) == TypeIds_Number ||
  8853. threadContext->HasNoSideEffect(function) ||
  8854. !ThreadContext::IsOnStack(object));
  8855. // Verify that the scriptcontext is alive before firing getter/setter
  8856. if (!scriptContext->VerifyAlive(!function->IsExternal(), requestContext))
  8857. {
  8858. return nullptr;
  8859. }
  8860. CallFlags flags = CallFlags_Value;
  8861. Var thisVar = RootToThisObject(object, scriptContext);
  8862. RecyclableObject* marshalledFunction = UnsafeVarTo<RecyclableObject>(
  8863. CrossSite::MarshalVar(requestContext, function, scriptContext));
  8864. Var result = CALL_ENTRYPOINT(threadContext, marshalledFunction->GetEntryPoint(), function, CallInfo(flags, 1), thisVar);
  8865. result = CrossSite::MarshalVar(requestContext, result);
  8866. // Set implicit call flags so we bail out if we're trying to propagate the value forward, e.g., from a compare. Subsequent calls
  8867. // to the getter may produce different results.
  8868. threadContext->AddImplicitCallFlags(ImplicitCall_Accessor);
  8869. return result;
  8870. });
  8871. }
  8872. void JavascriptOperators::CallSetter(RecyclableObject * const function, Var const object, Var const value, ScriptContext * requestContext)
  8873. {
  8874. ScriptContext * scriptContext = function->GetScriptContext();
  8875. ThreadContext * threadContext = scriptContext->GetThreadContext();
  8876. threadContext->ExecuteImplicitCall(function, ImplicitCall_Accessor, [=]() -> Js::Var
  8877. {
  8878. // Stack object should have a pre-op bail on implicit call. We shouldn't see them here.
  8879. // Stack numbers are ok, as we will call ToObject to wrap it in a number object anyway
  8880. // See JavascriptOperators::GetThisHelper
  8881. Assert(JavascriptOperators::GetTypeId(object) == TypeIds_Integer ||
  8882. JavascriptOperators::GetTypeId(object) == TypeIds_Number || !ThreadContext::IsOnStack(object));
  8883. // Verify that the scriptcontext is alive before firing getter/setter
  8884. if (!scriptContext->VerifyAlive(!function->IsExternal(), requestContext))
  8885. {
  8886. return nullptr;
  8887. }
  8888. CallFlags flags = CallFlags_Value;
  8889. Var putValue = value;
  8890. // CONSIDER: Have requestContext everywhere, even in the setProperty related codepath.
  8891. if (requestContext)
  8892. {
  8893. putValue = CrossSite::MarshalVar(requestContext, value);
  8894. }
  8895. Var thisVar = RootToThisObject(object, scriptContext);
  8896. RecyclableObject* marshalledFunction = function;
  8897. if (requestContext)
  8898. {
  8899. marshalledFunction = UnsafeVarTo<RecyclableObject>(CrossSite::MarshalVar(requestContext, function, function->GetScriptContext()));
  8900. }
  8901. Var result = CALL_ENTRYPOINT(threadContext, marshalledFunction->GetEntryPoint(), function, CallInfo(flags, 2), thisVar, putValue);
  8902. Assert(result);
  8903. // Set implicit call flags so we bail out if we're trying to propagate the stored value forward. We can't count on the getter/setter
  8904. // to produce the stored value on a LdFld.
  8905. threadContext->AddImplicitCallFlags(ImplicitCall_Accessor);
  8906. return nullptr;
  8907. });
  8908. }
  8909. void * JavascriptOperators::AllocMemForVarArray(size_t size, Recycler* recycler)
  8910. {
  8911. TRACK_ALLOC_INFO(recycler, Js::Var, Recycler, 0, (size_t)(size / sizeof(Js::Var)));
  8912. return recycler->AllocZero(size);
  8913. }
  8914. #if !FLOATVAR
  8915. void * JavascriptOperators::AllocUninitializedNumber(Js::RecyclerJavascriptNumberAllocator * allocator)
  8916. {
  8917. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(AllocUninitializedNumber);
  8918. TRACK_ALLOC_INFO(allocator->GetRecycler(), Js::JavascriptNumber, Recycler, 0, (size_t)-1);
  8919. return allocator->Alloc(sizeof(Js::JavascriptNumber));
  8920. JIT_HELPER_END(AllocUninitializedNumber);
  8921. }
  8922. #endif
  8923. void JavascriptOperators::ScriptAbort()
  8924. {
  8925. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScriptAbort);
  8926. throw ScriptAbortException();
  8927. JIT_HELPER_END(ScriptAbort);
  8928. }
  8929. JavascriptString * JavascriptOperators::Concat3(Var aLeft, Var aCenter, Var aRight, ScriptContext * scriptContext)
  8930. {
  8931. // Make sure we do the conversion in order from left to right
  8932. JavascriptString * strLeft = JavascriptConversion::ToPrimitiveString(aLeft, scriptContext);
  8933. JavascriptString * strCenter = JavascriptConversion::ToPrimitiveString(aCenter, scriptContext);
  8934. JavascriptString * strRight = JavascriptConversion::ToPrimitiveString(aRight, scriptContext);
  8935. return JavascriptString::Concat3(strLeft, strCenter, strRight);
  8936. }
  8937. JavascriptString *
  8938. JavascriptOperators::NewConcatStrMulti(Var a1, Var a2, uint count, ScriptContext * scriptContext)
  8939. {
  8940. // Make sure we do the conversion in order
  8941. JavascriptString * str1 = JavascriptConversion::ToPrimitiveString(a1, scriptContext);
  8942. JavascriptString * str2 = JavascriptConversion::ToPrimitiveString(a2, scriptContext);
  8943. return ConcatStringMulti::New(count, str1, str2, scriptContext);
  8944. }
  8945. void
  8946. JavascriptOperators::SetConcatStrMultiItem(Var concatStr, Var str, uint index, ScriptContext * scriptContext)
  8947. {
  8948. VarTo<ConcatStringMulti>(concatStr)->SetItem(index,
  8949. JavascriptConversion::ToPrimitiveString(str, scriptContext));
  8950. }
  8951. void
  8952. JavascriptOperators::SetConcatStrMultiItem2(Var concatStr, Var str1, Var str2, uint index, ScriptContext * scriptContext)
  8953. {
  8954. ConcatStringMulti * cs = VarTo<ConcatStringMulti>(concatStr);
  8955. cs->SetItem(index, JavascriptConversion::ToPrimitiveString(str1, scriptContext));
  8956. cs->SetItem(index + 1, JavascriptConversion::ToPrimitiveString(str2, scriptContext));
  8957. }
  8958. void JavascriptOperators::OP_SetComputedNameVar(Var method, Var computedNameVar)
  8959. {
  8960. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(SetComputedNameVar);
  8961. ScriptFunctionBase *scriptFunction = VarTo<ScriptFunctionBase>(method);
  8962. scriptFunction->SetComputedNameVar(computedNameVar);
  8963. JIT_HELPER_END(SetComputedNameVar);
  8964. }
  8965. void JavascriptOperators::OP_SetHomeObj(Var method, Var homeObj)
  8966. {
  8967. ScriptFunctionBase *scriptFunction = VarTo<ScriptFunctionBase>(method);
  8968. JIT_HELPER_NOT_REENTRANT_HEADER(SetHomeObj, reentrancylock, scriptFunction->GetScriptContext()->GetThreadContext());
  8969. scriptFunction->SetHomeObj(homeObj);
  8970. JIT_HELPER_END(SetHomeObj);
  8971. }
  8972. Var JavascriptOperators::OP_LdHomeObj(Var scriptFunction, ScriptContext * scriptContext)
  8973. {
  8974. JIT_HELPER_NOT_REENTRANT_HEADER(LdHomeObj, reentrancylock, scriptContext->GetThreadContext());
  8975. // Ensure this is not a stack ScriptFunction
  8976. if (!VarIs<ScriptFunction>(scriptFunction) || ThreadContext::IsOnStack(scriptFunction))
  8977. {
  8978. return scriptContext->GetLibrary()->GetUndefined();
  8979. }
  8980. ScriptFunction *instance = UnsafeVarTo<ScriptFunction>(scriptFunction);
  8981. // We keep a reference to the current class rather than its super prototype
  8982. // since the prototype could change.
  8983. Var homeObj = instance->GetHomeObj();
  8984. return (homeObj != nullptr) ? homeObj : scriptContext->GetLibrary()->GetUndefined();
  8985. JIT_HELPER_END(LdHomeObj);
  8986. }
  8987. Var JavascriptOperators::OP_LdHomeObjProto(Var homeObj, ScriptContext* scriptContext)
  8988. {
  8989. JIT_HELPER_NOT_REENTRANT_HEADER(LdHomeObjProto, reentrancylock, scriptContext->GetThreadContext());
  8990. if (homeObj == nullptr || !VarIs<RecyclableObject>(homeObj))
  8991. {
  8992. return scriptContext->GetLibrary()->GetUndefined();
  8993. }
  8994. RecyclableObject *thisObjPrototype = VarTo<RecyclableObject>(homeObj);
  8995. TypeId typeId = thisObjPrototype->GetTypeId();
  8996. if (typeId == TypeIds_Null || typeId == TypeIds_Undefined)
  8997. {
  8998. JavascriptError::ThrowReferenceError(scriptContext, JSERR_BadSuperReference);
  8999. }
  9000. Assert(thisObjPrototype != nullptr);
  9001. RecyclableObject *superBase = thisObjPrototype->GetPrototype();
  9002. if (superBase == nullptr || !VarIsCorrectType(superBase))
  9003. {
  9004. return scriptContext->GetLibrary()->GetUndefined();
  9005. }
  9006. return superBase;
  9007. JIT_HELPER_END(LdHomeObjProto);
  9008. }
  9009. Var JavascriptOperators::OP_LdFuncObj(Var scriptFunction, ScriptContext * scriptContext)
  9010. {
  9011. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(LdFuncObj);
  9012. // use self as value of [[FunctionObject]] - this is true only for constructors
  9013. Assert(VarIs<RecyclableObject>(scriptFunction));
  9014. return scriptFunction;
  9015. JIT_HELPER_END(LdFuncObj);
  9016. }
  9017. Var JavascriptOperators::OP_LdFuncObjProto(Var funcObj, ScriptContext* scriptContext)
  9018. {
  9019. JIT_HELPER_NOT_REENTRANT_HEADER(LdFuncObjProto, reentrancylock, scriptContext->GetThreadContext());
  9020. RecyclableObject *superCtor = VarTo<RecyclableObject>(funcObj)->GetPrototype();
  9021. if (superCtor == nullptr || !IsConstructor(superCtor))
  9022. {
  9023. JavascriptError::ThrowTypeError(scriptContext, JSERR_NotAConstructor);
  9024. }
  9025. return superCtor;
  9026. JIT_HELPER_END(LdFuncObjProto);
  9027. }
  9028. Var JavascriptOperators::OP_ImportCall(__in JavascriptFunction *function, __in Var specifier, __in ScriptContext* scriptContext)
  9029. {
  9030. JIT_HELPER_REENTRANT_HEADER(ImportCall);
  9031. ModuleRecordBase *moduleRecordBase = nullptr;
  9032. SourceTextModuleRecord *moduleRecord = nullptr;
  9033. FunctionBody* parentFuncBody = function->GetFunctionBody();
  9034. JavascriptString *specifierString = nullptr;
  9035. try
  9036. {
  9037. specifierString = JavascriptConversion::ToString(specifier, scriptContext);
  9038. }
  9039. catch (const JavascriptException &err)
  9040. {
  9041. Var errorObject = err.GetAndClear()->GetThrownObject(scriptContext);
  9042. AssertMsg(errorObject != nullptr, "OP_ImportCall: null error object thrown by ToString(specifier)");
  9043. if (errorObject != nullptr)
  9044. {
  9045. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, errorObject, scriptContext);
  9046. }
  9047. Throw::InternalError();
  9048. }
  9049. DWORD_PTR dwReferencingSourceContext = parentFuncBody->GetHostSourceContext();
  9050. if (!parentFuncBody->IsES6ModuleCode() && dwReferencingSourceContext == Js::Constants::NoHostSourceContext)
  9051. {
  9052. // import() called from eval
  9053. if (parentFuncBody->GetUtf8SourceInfo()->GetCallerUtf8SourceInfo() == nullptr)
  9054. {
  9055. JavascriptError *error = scriptContext->GetLibrary()->CreateError();
  9056. JavascriptError::SetErrorMessageProperties(error, E_FAIL, _u("Unable to locate active script or module that calls import()"), scriptContext);
  9057. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, error, scriptContext);
  9058. }
  9059. dwReferencingSourceContext = parentFuncBody->GetUtf8SourceInfo()->GetCallerUtf8SourceInfo()->GetSourceContextInfo()->dwHostSourceContext;
  9060. if (dwReferencingSourceContext == Js::Constants::NoHostSourceContext)
  9061. {
  9062. // Walk the call stack if caller function is neither module code nor having host source context
  9063. JavascriptFunction* caller = nullptr;
  9064. Js::JavascriptStackWalker walker(scriptContext);
  9065. walker.GetCaller(&caller);
  9066. do
  9067. {
  9068. if (walker.GetCaller(&caller) && caller != nullptr && caller->IsScriptFunction())
  9069. {
  9070. parentFuncBody = caller->GetFunctionBody();
  9071. dwReferencingSourceContext = parentFuncBody->GetHostSourceContext();
  9072. }
  9073. else
  9074. {
  9075. JavascriptError *error = scriptContext->GetLibrary()->CreateError();
  9076. JavascriptError::SetErrorMessageProperties(error, E_FAIL, _u("Unable to locate active script or module that calls import()"), scriptContext);
  9077. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, error, scriptContext);
  9078. }
  9079. } while (!parentFuncBody->IsES6ModuleCode() && dwReferencingSourceContext == Js::Constants::NoHostSourceContext);
  9080. }
  9081. }
  9082. LPCOLESTR moduleName = specifierString->GetSz();
  9083. HRESULT hr = 0;
  9084. if (parentFuncBody->IsES6ModuleCode())
  9085. {
  9086. SourceTextModuleRecord *referenceModuleRecord = parentFuncBody->GetScriptContext()->GetLibrary()->GetModuleRecord(parentFuncBody->GetModuleID());
  9087. BEGIN_LEAVE_SCRIPT(scriptContext);
  9088. BEGIN_TRANSLATE_TO_HRESULT(static_cast<ExceptionType>(ExceptionType_OutOfMemory | ExceptionType_StackOverflow));
  9089. hr = scriptContext->GetHostScriptContext()->FetchImportedModule(referenceModuleRecord, moduleName, &moduleRecordBase);
  9090. END_TRANSLATE_EXCEPTION_TO_HRESULT(hr);
  9091. END_LEAVE_SCRIPT(scriptContext);
  9092. }
  9093. else
  9094. {
  9095. Assert(dwReferencingSourceContext != Js::Constants::NoHostSourceContext);
  9096. BEGIN_LEAVE_SCRIPT(scriptContext);
  9097. BEGIN_TRANSLATE_TO_HRESULT(static_cast<ExceptionType>(ExceptionType_OutOfMemory | ExceptionType_StackOverflow));
  9098. hr = scriptContext->GetHostScriptContext()->FetchImportedModuleFromScript(dwReferencingSourceContext, moduleName, &moduleRecordBase);
  9099. END_TRANSLATE_EXCEPTION_TO_HRESULT(hr);
  9100. END_LEAVE_SCRIPT(scriptContext);
  9101. }
  9102. if (FAILED(hr))
  9103. {
  9104. // We cannot just use the buffer in the specifier string - need to make a copy here.
  9105. size_t length = wcslen(moduleName);
  9106. char16* allocatedString = RecyclerNewArrayLeaf(scriptContext->GetRecycler(), char16, length + 1);
  9107. wmemcpy_s(allocatedString, length + 1, moduleName, length);
  9108. allocatedString[length] = _u('\0');
  9109. Js::JavascriptError *error = scriptContext->GetLibrary()->CreateURIError();
  9110. JavascriptError::SetErrorMessageProperties(error, hr, allocatedString, scriptContext);
  9111. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, error, scriptContext);
  9112. }
  9113. moduleRecord = SourceTextModuleRecord::FromHost(moduleRecordBase);
  9114. if (moduleRecord->GetErrorObject() != nullptr)
  9115. {
  9116. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, moduleRecord->GetErrorObject(), scriptContext, moduleRecord);
  9117. }
  9118. else if (moduleRecord->WasEvaluated())
  9119. {
  9120. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(true, moduleRecord->GetNamespace(), scriptContext, moduleRecord);
  9121. }
  9122. return moduleRecord->PostProcessDynamicModuleImport();
  9123. JIT_HELPER_END(ImportCall);
  9124. }
  9125. void JavascriptOperators::OP_Await(JavascriptGenerator* generator, Var value, ScriptContext* scriptContext)
  9126. {
  9127. //#await
  9128. // 1. Let asyncContext be the running execution context.
  9129. // 2. Let promise be be ? PromiseResolve(%Promise%, << completion.[[Value]] >>).
  9130. JavascriptPromise* promise = JavascriptPromise::InternalPromiseResolve(value, scriptContext);
  9131. // 3. Let stepsFulfilled be the algorithm steps defined in Await Fulfilled Functions.
  9132. // 4. Let onFulfilled be CreateBuiltinFunction(stepsFulfilled, << [[AsyncContext]] >>).
  9133. // 5. Set onFulfilled.[[AsyncContext]] to asyncContext.
  9134. // 6. Let stepsRejected be the algorithm steps defined in Await Rejected Functions.
  9135. // 7. Let onRejected be CreateBuiltinFunction(stepsRejected, << [[AsyncContext]] >>).
  9136. // 8. Set onRejected.[[AsyncContext]] to asyncContext.
  9137. // 9. Perform ! PerformPromiseThen(promise, onFulfilled, onRejected).
  9138. JavascriptPromiseCapability* unused = JavascriptPromise::UnusedPromiseCapability(scriptContext);
  9139. JavascriptPromise::PerformPromiseThen(promise, unused, generator->GetAwaitNextFunction(), generator->GetAwaitThrowFunction(), scriptContext);
  9140. // 10. Remove asyncContext from the execution context stack and restore the execution context that is at the top of the execution context stack as the running execution context.
  9141. // 11. Set the code evaluation state of asyncContext such that when evaluation is resumed with a Completion completion, the following steps of the algorithm that invoked Await will be performed, with completion available.
  9142. }
  9143. void JavascriptOperators::OP_AsyncYieldStar(JavascriptGenerator* generator, Var value, ScriptContext* scriptContext)
  9144. {
  9145. JavascriptPromise* promise = JavascriptPromise::InternalPromiseResolve(value, scriptContext);
  9146. JavascriptPromiseCapability* unused = JavascriptPromise::UnusedPromiseCapability(scriptContext);
  9147. JavascriptPromise::PerformPromiseThen(promise, unused, generator->EnsureAwaitYieldStarFunction(), generator->GetAwaitThrowFunction(), scriptContext);
  9148. }
  9149. void JavascriptOperators::OP_AsyncYield(JavascriptGenerator* generator, Var value, ScriptContext* scriptContext)
  9150. {
  9151. JavascriptPromise* promise = JavascriptPromise::InternalPromiseResolve(value, scriptContext);
  9152. JavascriptPromiseCapability* unused = JavascriptPromise::UnusedPromiseCapability(scriptContext);
  9153. JavascriptPromise::PerformPromiseThen(promise, unused, generator->GetAwaitYieldFunction(), generator->GetAwaitThrowFunction(), scriptContext);
  9154. }
  9155. Var JavascriptOperators::OP_AsyncYieldIsReturn(ResumeYieldData* yieldData)
  9156. {
  9157. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(AsyncYieldIsReturn);
  9158. JavascriptLibrary* library = yieldData->generator->GetScriptContext()->GetLibrary();
  9159. return (yieldData->exceptionObj != nullptr && yieldData->exceptionObj->IsGeneratorReturnException()) ?
  9160. library->GetTrue() : library->GetFalse();
  9161. JIT_HELPER_END(AsyncYieldIsReturn);
  9162. }
  9163. Var JavascriptOperators::OP_ResumeYield(ResumeYieldData* yieldData, RecyclableObject* iterator)
  9164. {
  9165. JIT_HELPER_REENTRANT_HEADER(ResumeYield);
  9166. bool isNext = yieldData->exceptionObj == nullptr;
  9167. bool isThrow = !isNext && !yieldData->exceptionObj->IsGeneratorReturnException();
  9168. if (iterator != nullptr) // yield*
  9169. {
  9170. ScriptContext* scriptContext = iterator->GetScriptContext();
  9171. PropertyId propertyId = isNext ? PropertyIds::next : isThrow ? PropertyIds::throw_ : PropertyIds::return_;
  9172. Var prop = JavascriptOperators::GetProperty(iterator, propertyId, scriptContext);
  9173. if (!isNext && JavascriptOperators::IsUndefinedOrNull(prop))
  9174. {
  9175. if (isThrow)
  9176. {
  9177. // 5.b.iii.2
  9178. // NOTE: If iterator does not have a throw method, this throw is going to terminate the yield* loop.
  9179. // But first we need to give iterator a chance to clean up.
  9180. prop = JavascriptOperators::GetProperty(iterator, PropertyIds::return_, scriptContext);
  9181. if (!JavascriptOperators::IsUndefinedOrNull(prop))
  9182. {
  9183. if (!JavascriptConversion::IsCallable(prop))
  9184. {
  9185. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, _u("return"));
  9186. }
  9187. Var result = nullptr;
  9188. RecyclableObject* method = VarTo<RecyclableObject>(prop);
  9189. BEGIN_SAFE_REENTRANT_CALL(scriptContext->GetThreadContext())
  9190. {
  9191. Var args[] = { iterator, yieldData->data };
  9192. CallInfo callInfo(CallFlags_Value, _countof(args));
  9193. result = JavascriptFunction::CallFunction<true>(method, method->GetEntryPoint(), Arguments(callInfo, args));
  9194. }
  9195. END_SAFE_REENTRANT_CALL
  9196. if (!JavascriptOperators::IsObject(result))
  9197. {
  9198. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  9199. }
  9200. }
  9201. // 5.b.iii.3
  9202. // NOTE: The next step throws a TypeError to indicate that there was a yield* protocol violation:
  9203. // iterator does not have a throw method.
  9204. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, _u("throw"));
  9205. }
  9206. // Do not use ThrowExceptionObject for return() API exceptions since these exceptions are not real exceptions
  9207. JavascriptExceptionOperators::DoThrow(yieldData->exceptionObj, scriptContext);
  9208. }
  9209. if (!JavascriptConversion::IsCallable(prop))
  9210. {
  9211. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, isNext ? _u("next") : isThrow ? _u("throw") : _u("return"));
  9212. }
  9213. RecyclableObject* method = VarTo<RecyclableObject>(prop);
  9214. Var result = scriptContext->GetThreadContext()->ExecuteImplicitCall(method, Js::ImplicitCall_Accessor, [=]()->Js::Var
  9215. {
  9216. Var args[] = { iterator, yieldData->data };
  9217. CallInfo callInfo(CallFlags_Value, _countof(args));
  9218. return JavascriptFunction::CallFunction<true>(method, method->GetEntryPoint(), Arguments(callInfo, args));
  9219. });
  9220. if (yieldData->generator == nullptr && !JavascriptOperators::IsObject(result))
  9221. {
  9222. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  9223. }
  9224. if (isThrow || isNext || yieldData->generator != nullptr)
  9225. {
  9226. // 5.b.ii.2
  9227. // NOTE: Exceptions from the inner iterator throw method are propagated.
  9228. // Normal completions from an inner throw method are processed similarly to an inner next.
  9229. return result;
  9230. }
  9231. RecyclableObject* obj = VarTo<RecyclableObject>(result);
  9232. Var done = JavascriptOperators::GetProperty(obj, PropertyIds::done, scriptContext);
  9233. if (done == iterator->GetLibrary()->GetTrue())
  9234. {
  9235. Var value = JavascriptOperators::GetProperty(obj, PropertyIds::value, scriptContext);
  9236. yieldData->exceptionObj->SetThrownObject(value);
  9237. // Do not use ThrowExceptionObject for return() API exceptions since these exceptions are not real exceptions
  9238. JavascriptExceptionOperators::DoThrow(yieldData->exceptionObj, scriptContext);
  9239. }
  9240. return result;
  9241. }
  9242. // CONSIDER: Fast path this early out return path in JITed code before helper call to avoid the helper call overhead in the common case e.g. next() calls.
  9243. if (isNext)
  9244. {
  9245. return yieldData->data;
  9246. }
  9247. if (isThrow)
  9248. {
  9249. // Use ThrowExceptionObject() to get debugger support for breaking on throw
  9250. JavascriptExceptionOperators::ThrowExceptionObject(yieldData->exceptionObj, yieldData->exceptionObj->GetScriptContext(), true);
  9251. }
  9252. // CONSIDER: Using an exception to carry the return value and force finally code to execute is a bit of a janky
  9253. // solution since we have to override the value here in the case of yield* expressions. It works but is there
  9254. // a more elegant way?
  9255. //
  9256. // Instead what if ResumeYield was a "set Dst then optionally branch" opcode, that could also throw? Then we could
  9257. // avoid using a special exception entirely with byte code something like this:
  9258. //
  9259. // ;; Ry is the yieldData
  9260. //
  9261. // ResumeYield Rx Ry $returnPathLabel
  9262. // ... code like normal
  9263. // $returnPathLabel:
  9264. // Ld_A R0 Rx
  9265. // Br $exitFinallyAndReturn
  9266. //
  9267. // This would probably give better performance for the common case of calling next() on generators since we wouldn't
  9268. // have to wrap the call to the generator code in a try catch.
  9269. // Do not use ThrowExceptionObject for return() API exceptions since these exceptions are not real exceptions
  9270. JavascriptExceptionOperators::DoThrow(yieldData->exceptionObj, yieldData->exceptionObj->GetScriptContext());
  9271. JIT_HELPER_END(ResumeYield);
  9272. }
  9273. Var JavascriptOperators::OP_NewAsyncFromSyncIterator(Var syncIterator, ScriptContext* scriptContext)
  9274. {
  9275. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(NewAsyncFromSyncIterator);
  9276. return scriptContext->GetLibrary()->CreateAsyncFromSyncIterator(VarTo<RecyclableObject>(syncIterator));
  9277. JIT_HELPER_END(NewAsyncFromSyncIterator);
  9278. }
  9279. Js::Var
  9280. JavascriptOperators::BoxStackInstance(Js::Var instance, ScriptContext * scriptContext, bool allowStackFunction, bool deepCopy)
  9281. {
  9282. if (!ThreadContext::IsOnStack(instance) || (allowStackFunction && !TaggedNumber::Is(instance) && (*(int*)instance & 1)))
  9283. {
  9284. return instance;
  9285. }
  9286. TypeId typeId = JavascriptOperators::GetTypeId(instance);
  9287. switch (typeId)
  9288. {
  9289. case Js::TypeIds_Number:
  9290. #if !FLOATVAR
  9291. return JavascriptNumber::BoxStackInstance(instance, scriptContext);
  9292. #endif
  9293. // fall-through
  9294. case Js::TypeIds_Integer:
  9295. return instance;
  9296. case Js::TypeIds_RegEx:
  9297. return JavascriptRegExp::BoxStackInstance(VarTo<JavascriptRegExp>(instance), deepCopy);
  9298. case Js::TypeIds_Object:
  9299. return DynamicObject::BoxStackInstance(VarTo<DynamicObject>(instance), deepCopy);
  9300. case Js::TypeIds_Array:
  9301. return JavascriptArray::BoxStackInstance(UnsafeVarTo<JavascriptArray>(instance), deepCopy);
  9302. case Js::TypeIds_NativeIntArray:
  9303. return JavascriptNativeIntArray::BoxStackInstance(UnsafeVarTo<JavascriptNativeIntArray>(instance), deepCopy);
  9304. case Js::TypeIds_NativeFloatArray:
  9305. return JavascriptNativeFloatArray::BoxStackInstance(UnsafeVarTo<JavascriptNativeFloatArray>(instance), deepCopy);
  9306. case Js::TypeIds_Function:
  9307. Assert(allowStackFunction);
  9308. // Stack functions are deal with not mar mark them, but by nested function escape analysis
  9309. // in the front end. No need to box here.
  9310. return instance;
  9311. #if ENABLE_COPYONACCESS_ARRAY
  9312. case Js::TypeIds_CopyOnAccessNativeIntArray:
  9313. Assert(false);
  9314. // fall-through
  9315. #endif
  9316. default:
  9317. Assert(false);
  9318. return instance;
  9319. };
  9320. }
  9321. ImplicitCallFlags
  9322. JavascriptOperators::CacheAndClearImplicitBit(ScriptContext* scriptContext)
  9323. {
  9324. ImplicitCallFlags prevImplicitCallFlags = scriptContext->GetThreadContext()->GetImplicitCallFlags();
  9325. scriptContext->GetThreadContext()->ClearImplicitCallFlags();
  9326. return prevImplicitCallFlags;
  9327. }
  9328. ImplicitCallFlags
  9329. JavascriptOperators::CheckAndUpdateFunctionBodyWithImplicitFlag(FunctionBody* functionBody)
  9330. {
  9331. ScriptContext* scriptContext = functionBody->GetScriptContext();
  9332. ImplicitCallFlags currImplicitCallFlags = scriptContext->GetThreadContext()->GetImplicitCallFlags();
  9333. if ((currImplicitCallFlags > ImplicitCall_None))
  9334. {
  9335. functionBody->SetHasOnlyThisStmts(false);
  9336. }
  9337. return currImplicitCallFlags;
  9338. }
  9339. void
  9340. JavascriptOperators::RestoreImplicitFlag(ScriptContext* scriptContext, ImplicitCallFlags prevImplicitCallFlags, ImplicitCallFlags currImplicitCallFlags)
  9341. {
  9342. scriptContext->GetThreadContext()->SetImplicitCallFlags((ImplicitCallFlags)(prevImplicitCallFlags | currImplicitCallFlags));
  9343. }
  9344. FunctionProxy*
  9345. JavascriptOperators::GetDeferredDeserializedFunctionProxy(JavascriptFunction* func)
  9346. {
  9347. FunctionProxy* proxy = func->GetFunctionProxy();
  9348. Assert(proxy->GetFunctionInfo()->GetFunctionProxy() != proxy);
  9349. return proxy;
  9350. }
  9351. template <>
  9352. Js::Var JavascriptOperators::GetElementAtIndex(Js::JavascriptArray* arrayObject, UINT index, Js::ScriptContext* scriptContext)
  9353. {
  9354. Js::Var result;
  9355. if (Js::JavascriptOperators::OP_GetElementI_ArrayFastPath(arrayObject, index, &result, scriptContext))
  9356. {
  9357. return result;
  9358. }
  9359. return scriptContext->GetMissingItemResult();
  9360. }
  9361. template<>
  9362. Js::Var JavascriptOperators::GetElementAtIndex(Js::JavascriptNativeIntArray* arrayObject, UINT index, Js::ScriptContext* scriptContext)
  9363. {
  9364. Js::Var result;
  9365. if (Js::JavascriptOperators::OP_GetElementI_ArrayFastPath(arrayObject, index, &result, scriptContext))
  9366. {
  9367. return result;
  9368. }
  9369. return scriptContext->GetMissingItemResult();
  9370. }
  9371. template<>
  9372. Js::Var JavascriptOperators::GetElementAtIndex(Js::JavascriptNativeFloatArray* arrayObject, UINT index, Js::ScriptContext* scriptContext)
  9373. {
  9374. Js::Var result;
  9375. if (Js::JavascriptOperators::OP_GetElementI_ArrayFastPath(arrayObject, index, &result, scriptContext))
  9376. {
  9377. return result;
  9378. }
  9379. return scriptContext->GetMissingItemResult();
  9380. }
  9381. template<>
  9382. Js::Var JavascriptOperators::GetElementAtIndex(Js::Var* arrayObject, UINT index, Js::ScriptContext* scriptContext)
  9383. {
  9384. return Js::JavascriptOperators::OP_GetElementI_Int32(*arrayObject, index, scriptContext);
  9385. }
  9386. template<typename T>
  9387. void JavascriptOperators::ObjectToNativeArray(T* arrayObject,
  9388. JsNativeValueType valueType,
  9389. __in UINT length,
  9390. __in UINT elementSize,
  9391. __out_bcount(length*elementSize) byte* buffer,
  9392. Js::ScriptContext* scriptContext)
  9393. {
  9394. Var element;
  9395. uint64 allocSize = UInt32Math::Mul(length, elementSize);
  9396. // TODO:further fast path the call for things like IntArray convert to int, floatarray convert to float etc.
  9397. // such that we don't need boxing.
  9398. switch (valueType)
  9399. {
  9400. case JsInt8Type:
  9401. AnalysisAssert(elementSize == sizeof(int8));
  9402. for (UINT i = 0; i < length; i++)
  9403. {
  9404. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9405. AnalysisAssert((i + 1) * sizeof(int8) <= allocSize);
  9406. #pragma prefast(suppress:22102)
  9407. ((int8*)buffer)[i] = Js::JavascriptConversion::ToInt8(element, scriptContext);
  9408. }
  9409. break;
  9410. case JsUint8Type:
  9411. AnalysisAssert(elementSize == sizeof(uint8));
  9412. for (UINT i = 0; i < length; i++)
  9413. {
  9414. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9415. AnalysisAssert((i + 1) * sizeof(uint8) <= allocSize);
  9416. ((uint8*)buffer)[i] = Js::JavascriptConversion::ToUInt8(element, scriptContext);
  9417. }
  9418. break;
  9419. case JsInt16Type:
  9420. AnalysisAssert(elementSize == sizeof(int16));
  9421. for (UINT i = 0; i < length; i++)
  9422. {
  9423. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9424. AnalysisAssert((i + 1) * sizeof(int16) <= allocSize);
  9425. ((int16*)buffer)[i] = Js::JavascriptConversion::ToInt16(element, scriptContext);
  9426. }
  9427. break;
  9428. case JsUint16Type:
  9429. AnalysisAssert(elementSize == sizeof(uint16));
  9430. for (UINT i = 0; i < length; i++)
  9431. {
  9432. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9433. AnalysisAssert((i + 1) * sizeof(uint16) <= allocSize);
  9434. ((uint16*)buffer)[i] = Js::JavascriptConversion::ToUInt16(element, scriptContext);
  9435. }
  9436. break;
  9437. case JsInt32Type:
  9438. AnalysisAssert(elementSize == sizeof(int32));
  9439. for (UINT i = 0; i < length; i++)
  9440. {
  9441. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9442. AnalysisAssert((i + 1) * sizeof(int32) <= allocSize);
  9443. ((int32*)buffer)[i] = Js::JavascriptConversion::ToInt32(element, scriptContext);
  9444. }
  9445. break;
  9446. case JsUint32Type:
  9447. AnalysisAssert(elementSize == sizeof(uint32));
  9448. for (UINT i = 0; i < length; i++)
  9449. {
  9450. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9451. AnalysisAssert((i + 1) * sizeof(uint32) <= allocSize);
  9452. ((uint32*)buffer)[i] = Js::JavascriptConversion::ToUInt32(element, scriptContext);
  9453. }
  9454. break;
  9455. case JsInt64Type:
  9456. AnalysisAssert(elementSize == sizeof(int64));
  9457. for (UINT i = 0; i < length; i++)
  9458. {
  9459. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9460. AnalysisAssert((i + 1) * sizeof(int64) <= allocSize);
  9461. ((int64*)buffer)[i] = Js::JavascriptConversion::ToInt64(element, scriptContext);
  9462. }
  9463. break;
  9464. case JsUint64Type:
  9465. AnalysisAssert(elementSize == sizeof(uint64));
  9466. for (UINT i = 0; i < length; i++)
  9467. {
  9468. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9469. AnalysisAssert((i + 1) * sizeof(uint64) <= allocSize);
  9470. ((uint64*)buffer)[i] = Js::JavascriptConversion::ToUInt64(element, scriptContext);
  9471. }
  9472. break;
  9473. case JsFloatType:
  9474. AnalysisAssert(elementSize == sizeof(float));
  9475. for (UINT i = 0; i < length; i++)
  9476. {
  9477. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9478. AnalysisAssert((i + 1) * sizeof(float) <= allocSize);
  9479. ((float*)buffer)[i] = Js::JavascriptConversion::ToFloat(element, scriptContext);
  9480. }
  9481. break;
  9482. case JsDoubleType:
  9483. AnalysisAssert(elementSize == sizeof(double));
  9484. for (UINT i = 0; i < length; i++)
  9485. {
  9486. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9487. AnalysisAssert((i + 1) * sizeof(double) <= allocSize);
  9488. ((double*)buffer)[i] = Js::JavascriptConversion::ToNumber(element, scriptContext);
  9489. }
  9490. break;
  9491. case JsNativeStringType:
  9492. AnalysisAssert(elementSize == sizeof(JsNativeString));
  9493. for (UINT i = 0; i < length; i++)
  9494. {
  9495. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9496. AnalysisAssert((i + 1) * sizeof(JsNativeString) <= allocSize);
  9497. Js::JavascriptString* string = Js::JavascriptConversion::ToString(element, scriptContext);
  9498. (((JsNativeString*)buffer)[i]).str = string->GetSz();
  9499. (((JsNativeString*)buffer)[i]).length = string->GetLength();
  9500. }
  9501. break;
  9502. default:
  9503. Assert(FALSE);
  9504. }
  9505. }
  9506. void JavascriptOperators::VarToNativeArray(Var arrayObject,
  9507. JsNativeValueType valueType,
  9508. __in UINT length,
  9509. __in UINT elementSize,
  9510. __out_bcount(length*elementSize) byte* buffer,
  9511. Js::ScriptContext* scriptContext)
  9512. {
  9513. Js::DynamicObject* dynamicObject = VarTo<DynamicObject>(arrayObject);
  9514. if (dynamicObject->IsCrossSiteObject() || Js::TaggedInt::IsOverflow(length))
  9515. {
  9516. Js::JavascriptOperators::ObjectToNativeArray(&arrayObject, valueType, length, elementSize, buffer, scriptContext);
  9517. }
  9518. else
  9519. {
  9520. #if ENABLE_COPYONACCESS_ARRAY
  9521. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(arrayObject);
  9522. #endif
  9523. switch (Js::JavascriptOperators::GetTypeId(arrayObject))
  9524. {
  9525. case TypeIds_Array:
  9526. Js::JavascriptOperators::ObjectToNativeArray(Js::UnsafeVarTo<Js::JavascriptArray>(arrayObject), valueType, length, elementSize, buffer, scriptContext);
  9527. break;
  9528. case TypeIds_NativeFloatArray:
  9529. Js::JavascriptOperators::ObjectToNativeArray(Js::UnsafeVarTo<Js::JavascriptNativeFloatArray>(arrayObject), valueType, length, elementSize, buffer, scriptContext);
  9530. break;
  9531. case TypeIds_NativeIntArray:
  9532. Js::JavascriptOperators::ObjectToNativeArray(Js::UnsafeVarTo<Js::JavascriptNativeIntArray>(arrayObject), valueType, length, elementSize, buffer, scriptContext);
  9533. break;
  9534. // We can have more specialized template if needed.
  9535. default:
  9536. Js::JavascriptOperators::ObjectToNativeArray(&arrayObject, valueType, length, elementSize, buffer, scriptContext);
  9537. }
  9538. }
  9539. }
  9540. // SpeciesConstructor abstract operation as described in ES6.0 Section 7.3.20
  9541. RecyclableObject* JavascriptOperators::SpeciesConstructor(_In_ RecyclableObject* object, _In_ JavascriptFunction* defaultConstructor, _In_ ScriptContext* scriptContext)
  9542. {
  9543. //1.Assert: Type(O) is Object.
  9544. Assert(JavascriptOperators::IsObject(object));
  9545. //2.Let C be Get(O, "constructor").
  9546. //3.ReturnIfAbrupt(C).
  9547. Var constructor = JavascriptOperators::GetProperty(object, PropertyIds::constructor, scriptContext);
  9548. if (scriptContext->GetConfig()->IsES6SpeciesEnabled())
  9549. {
  9550. //4.If C is undefined, return defaultConstructor.
  9551. if (JavascriptOperators::IsUndefinedObject(constructor))
  9552. {
  9553. return defaultConstructor;
  9554. }
  9555. //5.If Type(C) is not Object, throw a TypeError exception.
  9556. if (!JavascriptOperators::IsObject(constructor))
  9557. {
  9558. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject, _u("[constructor]"));
  9559. }
  9560. //6.Let S be Get(C, @@species).
  9561. //7.ReturnIfAbrupt(S).
  9562. Var species = nullptr;
  9563. if (!JavascriptOperators::GetProperty(VarTo<RecyclableObject>(constructor),
  9564. PropertyIds::_symbolSpecies, &species, scriptContext)
  9565. || JavascriptOperators::IsUndefinedOrNull(species))
  9566. {
  9567. //8.If S is either undefined or null, return defaultConstructor.
  9568. return defaultConstructor;
  9569. }
  9570. constructor = species;
  9571. }
  9572. //9.If IsConstructor(S) is true, return S.
  9573. RecyclableObject* constructorObj = JavascriptOperators::TryFromVar<RecyclableObject>(constructor);
  9574. if (constructorObj && JavascriptOperators::IsConstructor(constructorObj))
  9575. {
  9576. return constructorObj;
  9577. }
  9578. //10.Throw a TypeError exception.
  9579. JavascriptError::ThrowTypeError(scriptContext, JSERR_NotAConstructor, _u("constructor[Symbol.species]"));
  9580. }
  9581. BOOL JavascriptOperators::GreaterEqual(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9582. {
  9583. JIT_HELPER_REENTRANT_HEADER(Op_GreaterEqual);
  9584. if (TaggedInt::Is(aLeft))
  9585. {
  9586. if (TaggedInt::Is(aRight))
  9587. {
  9588. // Works whether it is TaggedInt31 or TaggedInt32
  9589. return ::Math::PointerCastToIntegralTruncate<int>(aLeft) >= ::Math::PointerCastToIntegralTruncate<int>(aRight);
  9590. }
  9591. if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9592. {
  9593. return TaggedInt::ToDouble(aLeft) >= JavascriptNumber::GetValue(aRight);
  9594. }
  9595. }
  9596. else if (TaggedInt::Is(aRight))
  9597. {
  9598. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  9599. {
  9600. return JavascriptNumber::GetValue(aLeft) >= TaggedInt::ToDouble(aRight);
  9601. }
  9602. }
  9603. else
  9604. {
  9605. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft) && JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9606. {
  9607. return JavascriptNumber::GetValue(aLeft) >= JavascriptNumber::GetValue(aRight);
  9608. }
  9609. }
  9610. return !RelationalComparisonHelper(aLeft, aRight, scriptContext, true, true);
  9611. JIT_HELPER_END(Op_GreaterEqual);
  9612. }
  9613. BOOL JavascriptOperators::LessEqual(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9614. {
  9615. JIT_HELPER_REENTRANT_HEADER(Op_LessEqual);
  9616. if (TaggedInt::Is(aLeft))
  9617. {
  9618. if (TaggedInt::Is(aRight))
  9619. {
  9620. // Works whether it is TaggedInt31 or TaggedInt32
  9621. return ::Math::PointerCastToIntegralTruncate<int>(aLeft) <= ::Math::PointerCastToIntegralTruncate<int>(aRight);
  9622. }
  9623. if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9624. {
  9625. return TaggedInt::ToDouble(aLeft) <= JavascriptNumber::GetValue(aRight);
  9626. }
  9627. }
  9628. else if (TaggedInt::Is(aRight))
  9629. {
  9630. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  9631. {
  9632. return JavascriptNumber::GetValue(aLeft) <= TaggedInt::ToDouble(aRight);
  9633. }
  9634. }
  9635. else
  9636. {
  9637. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft) && JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9638. {
  9639. return JavascriptNumber::GetValue(aLeft) <= JavascriptNumber::GetValue(aRight);
  9640. }
  9641. }
  9642. return !RelationalComparisonHelper(aRight, aLeft, scriptContext, false, true);
  9643. JIT_HELPER_END(Op_LessEqual);
  9644. }
  9645. BOOL JavascriptOperators::NotEqual(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9646. {
  9647. JIT_HELPER_REENTRANT_HEADER(Op_NotEqual);
  9648. JIT_HELPER_SAME_ATTRIBUTES(Op_NotEqual, Op_Equal);
  9649. //
  9650. // TODO: Change to use Abstract Equality Comparison Algorithm (ES3.0: S11.9.3):
  9651. // - Evaluate left, then right, operands to preserve correct evaluation order.
  9652. // - Call algorithm, potentially reversing arguments.
  9653. //
  9654. return !Equal(aLeft, aRight, scriptContext);
  9655. JIT_HELPER_END(Op_NotEqual);
  9656. }
  9657. // NotStrictEqual() returns whether the two vars have strict equality, as
  9658. // described in (ES3.0: S11.9.5, S11.9.6).
  9659. BOOL JavascriptOperators::NotStrictEqual(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9660. {
  9661. JIT_HELPER_REENTRANT_HEADER(Op_NotStrictEqual);
  9662. JIT_HELPER_SAME_ATTRIBUTES(Op_NotStrictEqual, Op_StrictEqual);
  9663. return !StrictEqual(aLeft, aRight, scriptContext);
  9664. JIT_HELPER_END(Op_NotStrictEqual);
  9665. }
  9666. bool JavascriptOperators::CheckIfObjectAndPrototypeChainHasOnlyWritableDataProperties(_In_ RecyclableObject* object)
  9667. {
  9668. return object->GetLibrary()->GetTypesWithOnlyWritablePropertyProtoChainCache()->Check(object);
  9669. }
  9670. bool JavascriptOperators::CheckIfPrototypeChainHasOnlyWritableDataProperties(_In_ RecyclableObject* prototype)
  9671. {
  9672. return prototype->GetLibrary()->GetTypesWithOnlyWritablePropertyProtoChainCache()->CheckProtoChain(prototype);
  9673. }
  9674. bool JavascriptOperators::CheckIfObjectAndProtoChainHasNoSpecialProperties(_In_ RecyclableObject* object)
  9675. {
  9676. return object->GetLibrary()->GetTypesWithNoSpecialPropertyProtoChainCache()->Check(object);
  9677. }
  9678. // Checks to see if the specified object (which should be a prototype object)
  9679. // contains a proxy anywhere in the prototype chain.
  9680. bool JavascriptOperators::CheckIfPrototypeChainContainsProxyObject(RecyclableObject* prototype)
  9681. {
  9682. if (prototype == nullptr)
  9683. {
  9684. return false;
  9685. }
  9686. Assert(JavascriptOperators::IsObjectOrNull(prototype));
  9687. while (prototype->GetTypeId() != TypeIds_Null)
  9688. {
  9689. if (prototype->GetTypeId() == TypeIds_Proxy)
  9690. {
  9691. return true;
  9692. }
  9693. prototype = prototype->GetPrototype();
  9694. }
  9695. return false;
  9696. }
  9697. BOOL JavascriptOperators::Equal(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9698. {
  9699. JIT_HELPER_REENTRANT_HEADER(Op_Equal);
  9700. JIT_HELPER_SAME_ATTRIBUTES(Op_Equal, Op_Equal_Full);
  9701. if (aLeft == aRight)
  9702. {
  9703. if (TaggedInt::Is(aLeft) || DynamicObject::IsBaseDynamicObject(aLeft))
  9704. {
  9705. return true;
  9706. }
  9707. else
  9708. {
  9709. return Equal_Full(aLeft, aRight, scriptContext);
  9710. }
  9711. }
  9712. if (VarIs<JavascriptString>(aLeft) && VarIs<JavascriptString>(aRight))
  9713. {
  9714. JavascriptString* left = (JavascriptString*)aLeft;
  9715. JavascriptString* right = (JavascriptString*)aRight;
  9716. if (left->GetLength() == right->GetLength())
  9717. {
  9718. if (left->UnsafeGetBuffer() != NULL && right->UnsafeGetBuffer() != NULL)
  9719. {
  9720. if (left->GetLength() == 1)
  9721. {
  9722. return left->UnsafeGetBuffer()[0] == right->UnsafeGetBuffer()[0];
  9723. }
  9724. return memcmp(left->UnsafeGetBuffer(), right->UnsafeGetBuffer(), left->GetLength() * sizeof(left->UnsafeGetBuffer()[0])) == 0;
  9725. }
  9726. // fall through to Equal_Full
  9727. }
  9728. else
  9729. {
  9730. return false;
  9731. }
  9732. }
  9733. return Equal_Full(aLeft, aRight, scriptContext);
  9734. JIT_HELPER_END(Op_Equal);
  9735. }
  9736. BOOL JavascriptOperators::Greater(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9737. {
  9738. JIT_HELPER_REENTRANT_HEADER(Op_Greater);
  9739. if (TaggedInt::Is(aLeft))
  9740. {
  9741. if (TaggedInt::Is(aRight))
  9742. {
  9743. // Works whether it is TaggedInt31 or TaggedInt32
  9744. return ::Math::PointerCastToIntegralTruncate<int>(aLeft) > ::Math::PointerCastToIntegralTruncate<int>(aRight);
  9745. }
  9746. if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9747. {
  9748. return TaggedInt::ToDouble(aLeft) > JavascriptNumber::GetValue(aRight);
  9749. }
  9750. }
  9751. else if (TaggedInt::Is(aRight))
  9752. {
  9753. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  9754. {
  9755. return JavascriptNumber::GetValue(aLeft) > TaggedInt::ToDouble(aRight);
  9756. }
  9757. }
  9758. else
  9759. {
  9760. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft) && JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9761. {
  9762. return JavascriptNumber::GetValue(aLeft) > JavascriptNumber::GetValue(aRight);
  9763. }
  9764. }
  9765. return Greater_Full(aLeft, aRight, scriptContext);
  9766. JIT_HELPER_END(Op_Greater);
  9767. }
  9768. BOOL JavascriptOperators::Less(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9769. {
  9770. JIT_HELPER_REENTRANT_HEADER(Op_Less);
  9771. if (TaggedInt::Is(aLeft))
  9772. {
  9773. if (TaggedInt::Is(aRight))
  9774. {
  9775. // Works whether it is TaggedInt31 or TaggedInt32
  9776. return ::Math::PointerCastToIntegralTruncate<int>(aLeft) < ::Math::PointerCastToIntegralTruncate<int>(aRight);
  9777. }
  9778. if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9779. {
  9780. return TaggedInt::ToDouble(aLeft) < JavascriptNumber::GetValue(aRight);
  9781. }
  9782. }
  9783. else if (TaggedInt::Is(aRight))
  9784. {
  9785. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  9786. {
  9787. return JavascriptNumber::GetValue(aLeft) < TaggedInt::ToDouble(aRight);
  9788. }
  9789. }
  9790. else
  9791. {
  9792. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft) && JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9793. {
  9794. return JavascriptNumber::GetValue(aLeft) < JavascriptNumber::GetValue(aRight);
  9795. }
  9796. }
  9797. return Less_Full(aLeft, aRight, scriptContext);
  9798. JIT_HELPER_END(Op_Less);
  9799. }
  9800. RecyclableObject* JavascriptOperators::ToObject(Var aRight, ScriptContext* scriptContext)
  9801. {
  9802. JIT_HELPER_NOT_REENTRANT_HEADER(Op_ConvObject, reentrancylock, scriptContext->GetThreadContext());
  9803. RecyclableObject* object = nullptr;
  9804. if (FALSE == JavascriptConversion::ToObject(aRight, scriptContext, &object))
  9805. {
  9806. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject /* TODO-ERROR: get arg name - aValue */);
  9807. }
  9808. return object;
  9809. JIT_HELPER_END(Op_ConvObject);
  9810. }
  9811. Var JavascriptOperators::ToUnscopablesWrapperObject(Var aRight, ScriptContext* scriptContext)
  9812. {
  9813. JIT_HELPER_NOT_REENTRANT_HEADER(Op_NewUnscopablesWrapperObject, reentrancylock, scriptContext->GetThreadContext());
  9814. RecyclableObject* object = VarTo<RecyclableObject>(aRight);
  9815. UnscopablesWrapperObject* withWrapper = RecyclerNew(scriptContext->GetRecycler(), UnscopablesWrapperObject, object, scriptContext->GetLibrary()->GetWithType());
  9816. return withWrapper;
  9817. JIT_HELPER_END(Op_NewUnscopablesWrapperObject);
  9818. }
  9819. Var JavascriptOperators::ToNumber(Var aRight, ScriptContext* scriptContext)
  9820. {
  9821. JIT_HELPER_REENTRANT_HEADER(Op_ConvNumber_Full);
  9822. if (TaggedInt::Is(aRight) || (JavascriptNumber::Is_NoTaggedIntCheck(aRight)))
  9823. {
  9824. return aRight;
  9825. }
  9826. return JavascriptNumber::ToVarIntCheck(JavascriptConversion::ToNumber_Full(aRight, scriptContext), scriptContext);
  9827. JIT_HELPER_END(Op_ConvNumber_Full);
  9828. }
  9829. Var JavascriptOperators::ToNumeric(Var aRight, ScriptContext* scriptContext)
  9830. {
  9831. if (JavascriptOperators::GetTypeId(aRight) == TypeIds_BigInt)
  9832. {
  9833. return aRight;
  9834. }
  9835. return JavascriptOperators::ToNumber(aRight, scriptContext);
  9836. }
  9837. BOOL JavascriptOperators::IsObject(_In_ RecyclableObject* instance)
  9838. {
  9839. return GetTypeId(instance) > TypeIds_LastJavascriptPrimitiveType;
  9840. }
  9841. BOOL JavascriptOperators::IsObject(_In_ Var instance)
  9842. {
  9843. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_IsObject);
  9844. return GetTypeId(instance) > TypeIds_LastJavascriptPrimitiveType;
  9845. JIT_HELPER_END(Op_IsObject);
  9846. }
  9847. BOOL JavascriptOperators::IsObjectType(TypeId typeId)
  9848. {
  9849. return typeId > TypeIds_LastJavascriptPrimitiveType;
  9850. }
  9851. BOOL JavascriptOperators::IsExposedType(TypeId typeId)
  9852. {
  9853. return typeId <= TypeIds_LastTrueJavascriptObjectType && typeId != TypeIds_HostDispatch;
  9854. }
  9855. BOOL JavascriptOperators::IsObjectOrNull(Var instance)
  9856. {
  9857. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_IsObjectOrNull);
  9858. TypeId typeId = GetTypeId(instance);
  9859. return IsObjectType(typeId) || typeId == TypeIds_Null;
  9860. JIT_HELPER_END(Op_IsObjectOrNull);
  9861. }
  9862. BOOL JavascriptOperators::IsUndefined(_In_ RecyclableObject* instance)
  9863. {
  9864. return JavascriptOperators::GetTypeId(instance) == TypeIds_Undefined;
  9865. }
  9866. BOOL JavascriptOperators::IsUndefined(Var instance)
  9867. {
  9868. return JavascriptOperators::GetTypeId(instance) == TypeIds_Undefined;
  9869. }
  9870. BOOL JavascriptOperators::IsUndefinedOrNullType(TypeId typeId)
  9871. {
  9872. return typeId <= TypeIds_UndefinedOrNull;
  9873. }
  9874. BOOL JavascriptOperators::IsUndefinedOrNull(Var instance)
  9875. {
  9876. return IsUndefinedOrNullType(JavascriptOperators::GetTypeId(instance));
  9877. }
  9878. BOOL JavascriptOperators::IsUndefinedOrNull(RecyclableObject* instance)
  9879. {
  9880. return JavascriptOperators::IsUndefinedOrNullType(instance->GetTypeId());
  9881. }
  9882. BOOL JavascriptOperators::IsUndefinedOrNull(Var instance, ScriptContext* scriptContext)
  9883. {
  9884. JavascriptLibrary* library = scriptContext->GetLibrary();
  9885. return IsUndefinedObject(instance, library) || IsNull(instance, library);
  9886. }
  9887. BOOL JavascriptOperators::IsUndefinedOrNull(Var instance, JavascriptLibrary* library)
  9888. {
  9889. return IsUndefinedObject(instance, library) || IsNull(instance, library);
  9890. }
  9891. BOOL JavascriptOperators::IsNull(Var instance)
  9892. {
  9893. return JavascriptOperators::GetTypeId(instance) == TypeIds_Null;
  9894. }
  9895. BOOL JavascriptOperators::IsNull(Var instance, ScriptContext* scriptContext)
  9896. {
  9897. return JavascriptOperators::IsNull(instance, scriptContext->GetLibrary());
  9898. }
  9899. BOOL JavascriptOperators::IsNull(Var instance, JavascriptLibrary* library)
  9900. {
  9901. Assert(!VarIs<RecyclableObject>(instance) ? TRUE : ((RecyclableObject*)instance)->GetScriptContext()->GetLibrary() == library );
  9902. return library->GetNull() == instance;
  9903. }
  9904. BOOL JavascriptOperators::IsNull(RecyclableObject* instance)
  9905. {
  9906. return instance->GetType()->GetTypeId() == TypeIds_Null;
  9907. }
  9908. BOOL JavascriptOperators::IsSpecialObjectType(TypeId typeId)
  9909. {
  9910. return typeId > TypeIds_LastTrueJavascriptObjectType;
  9911. }
  9912. BOOL JavascriptOperators::IsUndefinedObject(Var instance)
  9913. {
  9914. return JavascriptOperators::GetTypeId(instance) == TypeIds_Undefined;
  9915. }
  9916. BOOL JavascriptOperators::IsUndefinedObject(RecyclableObject* instance)
  9917. {
  9918. return instance->GetType()->GetTypeId() == TypeIds_Undefined;
  9919. }
  9920. BOOL JavascriptOperators::IsUndefinedObject(Var instance, RecyclableObject *libraryUndefined)
  9921. {
  9922. Assert(JavascriptOperators::IsUndefinedObject(libraryUndefined));
  9923. AssertMsg((instance == libraryUndefined)
  9924. == JavascriptOperators::IsUndefinedObject(instance), "Wrong ScriptContext?");
  9925. return instance == libraryUndefined;
  9926. }
  9927. BOOL JavascriptOperators::IsUndefinedObject(Var instance, ScriptContext *scriptContext)
  9928. {
  9929. return JavascriptOperators::IsUndefinedObject(instance, scriptContext->GetLibrary());
  9930. }
  9931. BOOL JavascriptOperators::IsUndefinedObject(Var instance, JavascriptLibrary* library)
  9932. {
  9933. Assert(!VarIs<RecyclableObject>(instance) ? TRUE : ((RecyclableObject*)instance)->GetScriptContext()->GetLibrary() == library );
  9934. return JavascriptOperators::IsUndefinedObject(instance, library->GetUndefined());
  9935. }
  9936. BOOL JavascriptOperators::IsAnyNumberValue(Var instance)
  9937. {
  9938. TypeId typeId = GetTypeId(instance);
  9939. return TypeIds_FirstNumberType <= typeId && typeId <= TypeIds_LastNumberType;
  9940. }
  9941. // GetIterator as described in ES6.0 (draft 22) Section 7.4.1
  9942. RecyclableObject* JavascriptOperators::GetIterator(Var iterable, ScriptContext* scriptContext, bool optional)
  9943. {
  9944. RecyclableObject* iterableObj = JavascriptOperators::ToObject(iterable, scriptContext);
  9945. return JavascriptOperators::GetIterator(iterableObj, scriptContext, optional);
  9946. }
  9947. RecyclableObject* JavascriptOperators::GetIteratorFunction(Var iterable, ScriptContext* scriptContext, bool optional)
  9948. {
  9949. RecyclableObject* iterableObj = JavascriptOperators::ToObject(iterable, scriptContext);
  9950. return JavascriptOperators::GetIteratorFunction(iterableObj, scriptContext, optional);
  9951. }
  9952. RecyclableObject* JavascriptOperators::GetIteratorFunction(RecyclableObject* instance, ScriptContext * scriptContext, bool optional)
  9953. {
  9954. Var func = JavascriptOperators::GetPropertyNoCache(instance, PropertyIds::_symbolIterator, scriptContext);
  9955. if (optional && JavascriptOperators::IsUndefinedOrNull(func))
  9956. {
  9957. return nullptr;
  9958. }
  9959. if (!JavascriptConversion::IsCallable(func))
  9960. {
  9961. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction);
  9962. }
  9963. RecyclableObject* function = VarTo<RecyclableObject>(func);
  9964. return function;
  9965. }
  9966. RecyclableObject* JavascriptOperators::GetIterator(RecyclableObject* instance, ScriptContext * scriptContext, bool optional)
  9967. {
  9968. RecyclableObject* function = GetIteratorFunction(instance, scriptContext, optional);
  9969. if (function == nullptr)
  9970. {
  9971. Assert(optional);
  9972. return nullptr;
  9973. }
  9974. Var iterator = scriptContext->GetThreadContext()->ExecuteImplicitCall(function, Js::ImplicitCall_Accessor, [=]()->Js::Var
  9975. {
  9976. return CALL_FUNCTION(scriptContext->GetThreadContext(), function, CallInfo(Js::CallFlags_Value, 1), instance);
  9977. });
  9978. if (!JavascriptOperators::IsObject(iterator))
  9979. {
  9980. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  9981. }
  9982. return VarTo<RecyclableObject>(iterator);
  9983. }
  9984. void JavascriptOperators::IteratorClose(RecyclableObject* iterator, ScriptContext* scriptContext)
  9985. {
  9986. try
  9987. {
  9988. Var func = JavascriptOperators::GetProperty(iterator, PropertyIds::return_, scriptContext);
  9989. if (JavascriptConversion::IsCallable(func))
  9990. {
  9991. RecyclableObject* callable = VarTo<RecyclableObject>(func);
  9992. scriptContext->GetThreadContext()->ExecuteImplicitCall(callable, ImplicitCall_Accessor, [=]()->Var
  9993. {
  9994. Js::Var args[] = { iterator };
  9995. Js::CallInfo callInfo(Js::CallFlags_Value, _countof(args));
  9996. return JavascriptFunction::CallFunction<true>(callable, callable->GetEntryPoint(), Js::Arguments(callInfo, args));
  9997. });
  9998. }
  9999. }
  10000. catch (const JavascriptException& err)
  10001. {
  10002. err.GetAndClear(); // discard exception object
  10003. // We have arrived in this function due to AbruptCompletion (which is an exception), so we don't need to
  10004. // propagate the exception of calling return function
  10005. }
  10006. }
  10007. // IteratorNext as described in ES6.0 (draft 22) Section 7.4.2
  10008. RecyclableObject* JavascriptOperators::IteratorNext(RecyclableObject* iterator, ScriptContext* scriptContext, Var value)
  10009. {
  10010. Var func = JavascriptOperators::GetPropertyNoCache(iterator, PropertyIds::next, scriptContext);
  10011. ThreadContext *threadContext = scriptContext->GetThreadContext();
  10012. if (!JavascriptConversion::IsCallable(func))
  10013. {
  10014. if (!threadContext->RecordImplicitException())
  10015. {
  10016. return scriptContext->GetLibrary()->GetUndefined();
  10017. }
  10018. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction);
  10019. }
  10020. RecyclableObject* callable = VarTo<RecyclableObject>(func);
  10021. Var result = threadContext->ExecuteImplicitCall(callable, ImplicitCall_Accessor, [=]() -> Var
  10022. {
  10023. Js::Var args[] = { iterator, value };
  10024. Js::CallInfo callInfo(Js::CallFlags_Value, _countof(args) + (value == nullptr ? -1 : 0));
  10025. return JavascriptFunction::CallFunction<true>(callable, callable->GetEntryPoint(), Arguments(callInfo, args));
  10026. });
  10027. if (!JavascriptOperators::IsObject(result))
  10028. {
  10029. if (!threadContext->RecordImplicitException())
  10030. {
  10031. return scriptContext->GetLibrary()->GetUndefined();
  10032. }
  10033. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  10034. }
  10035. return VarTo<RecyclableObject>(result);
  10036. }
  10037. // IteratorComplete as described in ES6.0 (draft 22) Section 7.4.3
  10038. bool JavascriptOperators::IteratorComplete(RecyclableObject* iterResult, ScriptContext* scriptContext)
  10039. {
  10040. Var done = JavascriptOperators::GetPropertyNoCache(iterResult, Js::PropertyIds::done, scriptContext);
  10041. return JavascriptConversion::ToBool(done, scriptContext);
  10042. }
  10043. // IteratorValue as described in ES6.0 (draft 22) Section 7.4.4
  10044. Var JavascriptOperators::IteratorValue(RecyclableObject* iterResult, ScriptContext* scriptContext)
  10045. {
  10046. return JavascriptOperators::GetPropertyNoCache(iterResult, Js::PropertyIds::value, scriptContext);
  10047. }
  10048. // IteratorStep as described in ES6.0 (draft 22) Section 7.4.5
  10049. bool JavascriptOperators::IteratorStep(RecyclableObject* iterator, ScriptContext* scriptContext, RecyclableObject** result)
  10050. {
  10051. Assert(result);
  10052. *result = JavascriptOperators::IteratorNext(iterator, scriptContext);
  10053. return !JavascriptOperators::IteratorComplete(*result, scriptContext);
  10054. }
  10055. bool JavascriptOperators::IteratorStepAndValue(RecyclableObject* iterator, ScriptContext* scriptContext, Var* resultValue)
  10056. {
  10057. // CONSIDER: Fast-pathing for iterators that are built-ins?
  10058. RecyclableObject* result = JavascriptOperators::IteratorNext(iterator, scriptContext);
  10059. if (!JavascriptOperators::IteratorComplete(result, scriptContext))
  10060. {
  10061. *resultValue = JavascriptOperators::IteratorValue(result, scriptContext);
  10062. return true;
  10063. }
  10064. return false;
  10065. }
  10066. RecyclableObject* JavascriptOperators::CreateFromConstructor(RecyclableObject* constructor, ScriptContext* scriptContext)
  10067. {
  10068. // Create a regular object and set the internal proto from the constructor
  10069. return JavascriptOperators::OrdinaryCreateFromConstructor(constructor, scriptContext->GetLibrary()->CreateObject(), nullptr, scriptContext);
  10070. }
  10071. RecyclableObject* JavascriptOperators::OrdinaryCreateFromConstructor(RecyclableObject* constructor, RecyclableObject* obj, DynamicObject* intrinsicProto, ScriptContext* scriptContext)
  10072. {
  10073. // There isn't a good way for us to add internal properties to objects in Chakra.
  10074. // Thus, caller should take care to create obj with the correct internal properties.
  10075. Var proto = JavascriptOperators::GetPropertyNoCache(constructor, Js::PropertyIds::prototype, scriptContext);
  10076. // If constructor.prototype is an object, we should use that as the [[Prototype]] for our obj.
  10077. // Else, we set the [[Prototype]] internal slot of obj to %intrinsicProto% - which should be the default.
  10078. if (JavascriptOperators::IsObjectType(JavascriptOperators::GetTypeId(proto)) &&
  10079. VarTo<DynamicObject>(proto) != intrinsicProto)
  10080. {
  10081. JavascriptObject::ChangePrototype(obj, VarTo<RecyclableObject>(proto), /*validate*/true, scriptContext);
  10082. }
  10083. return obj;
  10084. }
  10085. Var JavascriptOperators::GetProperty(RecyclableObject* instance, PropertyId propertyId, ScriptContext* requestContext, PropertyValueInfo* info)
  10086. {
  10087. return JavascriptOperators::GetProperty(instance, instance, propertyId, requestContext, info);
  10088. }
  10089. BOOL JavascriptOperators::GetProperty(RecyclableObject* instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  10090. {
  10091. return JavascriptOperators::GetProperty(instance, instance, propertyId, value, requestContext, info);
  10092. }
  10093. Var JavascriptOperators::GetProperty(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, ScriptContext* requestContext, PropertyValueInfo* info)
  10094. {
  10095. Var value;
  10096. if (JavascriptOperators::GetProperty(instance, propertyObject, propertyId, &value, requestContext, info))
  10097. {
  10098. return value;
  10099. }
  10100. return requestContext->GetMissingPropertyResult();
  10101. }
  10102. Var JavascriptOperators::GetPropertyNoCache(RecyclableObject* instance, PropertyId propertyId, ScriptContext* requestContext)
  10103. {
  10104. return JavascriptOperators::GetPropertyNoCache(instance, instance, propertyId, requestContext);
  10105. }
  10106. Var JavascriptOperators::GetPropertyNoCache(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, ScriptContext* requestContext)
  10107. {
  10108. Var value;
  10109. JavascriptOperators::GetProperty_InternalSimple(instance, propertyObject, propertyId, &value, requestContext);
  10110. return value;
  10111. }
  10112. BOOL JavascriptOperators::GetPropertyNoCache(RecyclableObject* instance, PropertyId propertyId, Var* value, ScriptContext* requestContext)
  10113. {
  10114. return JavascriptOperators::GetPropertyNoCache(instance, instance, propertyId, value, requestContext);
  10115. }
  10116. BOOL JavascriptOperators::GetPropertyNoCache(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, Var* value, ScriptContext* requestContext)
  10117. {
  10118. return JavascriptOperators::GetProperty_InternalSimple(instance, propertyObject, propertyId, value, requestContext);
  10119. }
  10120. Var JavascriptOperators::GetRootProperty(RecyclableObject* instance, PropertyId propertyId, ScriptContext* requestContext, PropertyValueInfo* info)
  10121. {
  10122. Var value;
  10123. if (JavascriptOperators::GetRootProperty(instance, propertyId, &value, requestContext, info))
  10124. {
  10125. return value;
  10126. }
  10127. return requestContext->GetMissingPropertyResult();
  10128. }
  10129. BOOL JavascriptOperators::GetPropertyReference(RecyclableObject *instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  10130. {
  10131. return JavascriptOperators::GetPropertyReference(instance, instance, propertyId, value, requestContext, info);
  10132. }
  10133. Var JavascriptOperators::GetItem(RecyclableObject* instance, uint32 index, ScriptContext* requestContext)
  10134. {
  10135. Var value;
  10136. if (GetItem(instance, index, &value, requestContext))
  10137. {
  10138. return value;
  10139. }
  10140. return requestContext->GetMissingItemResult();
  10141. }
  10142. Var JavascriptOperators::GetItem(RecyclableObject* instance, uint64 index, ScriptContext* requestContext)
  10143. {
  10144. Var value;
  10145. if (GetItem(instance, index, &value, requestContext))
  10146. {
  10147. return value;
  10148. }
  10149. return requestContext->GetMissingItemResult();
  10150. }
  10151. BOOL JavascriptOperators::GetItem(RecyclableObject* instance, uint64 index, Var* value, ScriptContext* requestContext)
  10152. {
  10153. if (index < JavascriptArray::InvalidIndex)
  10154. {
  10155. // In case index fits in uint32, we can avoid the (slower) big-index path
  10156. return GetItem(instance, static_cast<uint32>(index), value, requestContext);
  10157. }
  10158. PropertyRecord const * propertyRecord = nullptr;
  10159. JavascriptOperators::GetPropertyIdForInt(index, requestContext, &propertyRecord);
  10160. return JavascriptOperators::GetProperty(instance, propertyRecord->GetPropertyId(), value, requestContext);
  10161. }
  10162. BOOL JavascriptOperators::GetItem(RecyclableObject* instance, uint32 index, Var* value, ScriptContext* requestContext)
  10163. {
  10164. return JavascriptOperators::GetItem(instance, instance, index, value, requestContext);
  10165. }
  10166. BOOL JavascriptOperators::GetItemReference(RecyclableObject* instance, uint32 index, Var* value, ScriptContext* requestContext)
  10167. {
  10168. return GetItemReference(instance, instance, index, value, requestContext);
  10169. }
  10170. BOOL JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(RecyclableObject* instance, PropertyId propertyId, Var* setterValue, DescriptorFlags* flags, PropertyValueInfo* info, ScriptContext* scriptContext)
  10171. {
  10172. if (propertyId == Js::PropertyIds::__proto__)
  10173. {
  10174. return CheckPrototypesForAccessorOrNonWritablePropertyCore<PropertyId, false, false>(instance, propertyId, setterValue, flags, info, scriptContext);
  10175. }
  10176. else
  10177. {
  10178. return CheckPrototypesForAccessorOrNonWritablePropertyCore<PropertyId, true, false>(instance, propertyId, setterValue, flags, info, scriptContext);
  10179. }
  10180. }
  10181. BOOL JavascriptOperators::CheckPrototypesForAccessorOrNonWritableRootProperty(RecyclableObject* instance, PropertyId propertyId, Var* setterValue, DescriptorFlags* flags, PropertyValueInfo* info, ScriptContext* scriptContext)
  10182. {
  10183. if (propertyId == Js::PropertyIds::__proto__)
  10184. {
  10185. return CheckPrototypesForAccessorOrNonWritablePropertyCore<PropertyId, false, true>(instance, propertyId, setterValue, flags, info, scriptContext);
  10186. }
  10187. else
  10188. {
  10189. return CheckPrototypesForAccessorOrNonWritablePropertyCore<PropertyId, true, true>(instance, propertyId, setterValue, flags, info, scriptContext);
  10190. }
  10191. }
  10192. BOOL JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(RecyclableObject* instance, JavascriptString* propertyNameString, Var* setterValue, DescriptorFlags* flags, PropertyValueInfo* info, ScriptContext* scriptContext)
  10193. {
  10194. Js::PropertyRecord const * localPropertyRecord;
  10195. propertyNameString->GetPropertyRecord(&localPropertyRecord);
  10196. PropertyId propertyId = localPropertyRecord->GetPropertyId();
  10197. return CheckPrototypesForAccessorOrNonWritableProperty(instance, propertyId, setterValue, flags, info, scriptContext);
  10198. }
  10199. BOOL JavascriptOperators::SetProperty(Var instance, RecyclableObject* object, PropertyId propertyId, Var newValue, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  10200. {
  10201. PropertyValueInfo info;
  10202. return JavascriptOperators::SetProperty(instance, object, propertyId, newValue, &info, requestContext, propertyOperationFlags);
  10203. }
  10204. BOOL JavascriptOperators::TryConvertToUInt32(const char16* str, int length, uint32* intVal)
  10205. {
  10206. return NumberUtilities::TryConvertToUInt32(str, length, intVal);
  10207. }
  10208. template <typename TPropertyKey>
  10209. DescriptorFlags JavascriptOperators::GetRootSetter(RecyclableObject* instance, TPropertyKey propertyKey, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  10210. {
  10211. // This is provided only so that CheckPrototypesForAccessorOrNonWritablePropertyCore will compile.
  10212. // It will never be called.
  10213. Throw::FatalInternalError();
  10214. }
  10215. template <>
  10216. inline DescriptorFlags JavascriptOperators::GetRootSetter(RecyclableObject* instance, PropertyId propertyId, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  10217. {
  10218. AssertMsg(JavascriptOperators::GetTypeId(instance) == TypeIds_GlobalObject
  10219. || JavascriptOperators::GetTypeId(instance) == TypeIds_ModuleRoot,
  10220. "Root must be a global object!");
  10221. RootObjectBase* rootObject = static_cast<RootObjectBase*>(instance);
  10222. return rootObject->GetRootSetter(propertyId, setterValue, info, requestContext);
  10223. }
  10224. // Helper to fetch @@species from a constructor object
  10225. Var JavascriptOperators::GetSpecies(RecyclableObject* constructor, ScriptContext* scriptContext)
  10226. {
  10227. if (scriptContext->GetConfig()->IsES6SpeciesEnabled())
  10228. {
  10229. Var species = nullptr;
  10230. // Let S be Get(C, @@species)
  10231. if (JavascriptOperators::GetProperty(constructor, PropertyIds::_symbolSpecies, &species, scriptContext)
  10232. && !JavascriptOperators::IsUndefinedOrNull(species))
  10233. {
  10234. // If S is neither undefined nor null, let C be S
  10235. return species;
  10236. }
  10237. }
  10238. return constructor;
  10239. }