2
0

JavascriptOperators.cpp 526 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Copyright (c) ChakraCore Project Contributors. All rights reserved.
  4. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  5. //-------------------------------------------------------------------------------------------------------
  6. #include "RuntimeLanguagePch.h"
  7. #include "Types/PathTypeHandler.h"
  8. #include "Types/PropertyIndexRanges.h"
  9. #include "Types/UnscopablesWrapperObject.h"
  10. #include "Types/SpreadArgument.h"
  11. #include "Library/JavascriptPromise.h"
  12. #include "Library/JavascriptRegularExpression.h"
  13. #include "Library/ThrowErrorObject.h"
  14. #include "Library/JavascriptGeneratorFunction.h"
  15. #include "Library/JavascriptAsyncFunction.h"
  16. #include "Library/ForInObjectEnumerator.h"
  17. #include "Library/ES5Array.h"
  18. #include "Types/SimpleDictionaryPropertyDescriptor.h"
  19. #include "Types/SimpleDictionaryTypeHandler.h"
  20. #include "Language/ModuleNamespace.h"
  21. #ifndef SCRIPT_DIRECT_TYPE
  22. typedef enum JsNativeValueType: int
  23. {
  24. JsInt8Type,
  25. JsUint8Type,
  26. JsInt16Type,
  27. JsUint16Type,
  28. JsInt32Type,
  29. JsUint32Type,
  30. JsInt64Type,
  31. JsUint64Type,
  32. JsFloatType,
  33. JsDoubleType,
  34. JsNativeStringType
  35. } JsNativeValueType;
  36. typedef struct JsNativeString
  37. {
  38. unsigned int length;
  39. LPCWSTR str;
  40. } JsNativeString;
  41. #endif
  42. using namespace Js;
  43. DEFINE_RECYCLER_TRACKER_ARRAY_PERF_COUNTER(Var);
  44. DEFINE_RECYCLER_TRACKER_PERF_COUNTER(FrameDisplay);
  45. enum IndexType
  46. {
  47. IndexType_Number,
  48. IndexType_PropertyId,
  49. IndexType_JavascriptString
  50. };
  51. IndexType GetIndexTypeFromString(char16 const * propertyName, charcount_t propertyLength, ScriptContext* scriptContext, uint32* index, PropertyRecord const** propertyRecord, bool createIfNotFound)
  52. {
  53. if (JavascriptOperators::TryConvertToUInt32(propertyName, propertyLength, index) &&
  54. (*index != JavascriptArray::InvalidIndex))
  55. {
  56. return IndexType_Number;
  57. }
  58. else
  59. {
  60. if (createIfNotFound)
  61. {
  62. scriptContext->GetOrAddPropertyRecord(propertyName, propertyLength, propertyRecord);
  63. }
  64. else
  65. {
  66. scriptContext->FindPropertyRecord(propertyName, propertyLength, propertyRecord);
  67. }
  68. return IndexType_PropertyId;
  69. }
  70. }
  71. IndexType GetIndexTypeFromPrimitive(Var indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, JavascriptString ** propertyNameString, bool createIfNotFound, bool preferJavascriptStringOverPropertyRecord)
  72. {
  73. // CONSIDER: Only OP_SetElementI and OP_GetElementI use and take advantage of the
  74. // IndexType_JavascriptString result. Consider modifying other callers of GetIndexType to take
  75. // advantage of non-interned property strings where appropriate.
  76. if (TaggedInt::Is(indexVar))
  77. {
  78. int indexInt = TaggedInt::ToInt32(indexVar);
  79. if (indexInt >= 0)
  80. {
  81. *index = (uint)indexInt;
  82. return IndexType_Number;
  83. }
  84. else
  85. {
  86. char16 stringBuffer[22];
  87. int pos = TaggedInt::ToBuffer(indexInt, stringBuffer, _countof(stringBuffer));
  88. charcount_t length = (_countof(stringBuffer) - 1) - pos;
  89. if (createIfNotFound || preferJavascriptStringOverPropertyRecord)
  90. {
  91. // When preferring JavascriptString objects, just return a PropertyRecord instead
  92. // of creating temporary JavascriptString objects for every negative integer that
  93. // comes through here.
  94. scriptContext->GetOrAddPropertyRecord(stringBuffer + pos, length, propertyRecord);
  95. }
  96. else
  97. {
  98. scriptContext->FindPropertyRecord(stringBuffer + pos, length, propertyRecord);
  99. }
  100. return IndexType_PropertyId;
  101. }
  102. }
  103. if (JavascriptNumber::Is_NoTaggedIntCheck(indexVar))
  104. {
  105. // If this double can be a positive integer index, convert it.
  106. int32 value = 0;
  107. bool isInt32 = false;
  108. if (JavascriptNumber::TryGetInt32OrUInt32Value(JavascriptNumber::GetValue(indexVar), &value, &isInt32)
  109. && !isInt32
  110. && static_cast<uint32>(value) < JavascriptArray::InvalidIndex)
  111. {
  112. *index = static_cast<uint32>(value);
  113. return IndexType_Number;
  114. }
  115. // Fall through to slow string conversion.
  116. }
  117. JavascriptSymbol * symbol = JavascriptOperators::TryFromVar<JavascriptSymbol>(indexVar);
  118. if (symbol)
  119. {
  120. // JavascriptSymbols cannot add a new PropertyRecord - they correspond to one and only one existing PropertyRecord.
  121. // We already know what the PropertyRecord is since it is stored in the JavascriptSymbol itself so just return it.
  122. *propertyRecord = symbol->GetValue();
  123. return IndexType_PropertyId;
  124. }
  125. else
  126. {
  127. JavascriptString* indexStr = JavascriptConversion::ToString(indexVar, scriptContext);
  128. char16 const * propertyName = indexStr->GetString();
  129. charcount_t const propertyLength = indexStr->GetLength();
  130. if (!createIfNotFound && preferJavascriptStringOverPropertyRecord)
  131. {
  132. if (JavascriptOperators::TryConvertToUInt32(propertyName, propertyLength, index) &&
  133. (*index != JavascriptArray::InvalidIndex))
  134. {
  135. return IndexType_Number;
  136. }
  137. *propertyNameString = indexStr;
  138. return IndexType_JavascriptString;
  139. }
  140. return GetIndexTypeFromString(propertyName, propertyLength, scriptContext, index, propertyRecord, createIfNotFound);
  141. }
  142. }
  143. IndexType GetIndexTypeFromPrimitive(Var indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, bool createIfNotFound)
  144. {
  145. return GetIndexTypeFromPrimitive(indexVar, scriptContext, index, propertyRecord, nullptr, createIfNotFound, false);
  146. }
  147. IndexType GetIndexType(Var& indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, JavascriptString ** propertyNameString, bool createIfNotFound, bool preferJavascriptStringOverPropertyRecord)
  148. {
  149. indexVar = JavascriptConversion::ToPrimitive<JavascriptHint::HintString>(indexVar, scriptContext);
  150. return GetIndexTypeFromPrimitive(indexVar, scriptContext, index, propertyRecord, propertyNameString, createIfNotFound, preferJavascriptStringOverPropertyRecord);
  151. }
  152. IndexType GetIndexType(Var& indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, bool createIfNotFound)
  153. {
  154. return GetIndexType(indexVar, scriptContext, index, propertyRecord, nullptr, createIfNotFound, false);
  155. }
  156. BOOL FEqualDbl(double dbl1, double dbl2)
  157. {
  158. // If the low ulongs don't match, they can't be equal.
  159. if (Js::NumberUtilities::LuLoDbl(dbl1) != Js::NumberUtilities::LuLoDbl(dbl2))
  160. return FALSE;
  161. // If the high ulongs don't match, they can be equal iff one is -0 and
  162. // the other is +0.
  163. if (Js::NumberUtilities::LuHiDbl(dbl1) != Js::NumberUtilities::LuHiDbl(dbl2))
  164. {
  165. return 0x80000000 == (Js::NumberUtilities::LuHiDbl(dbl1) | Js::NumberUtilities::LuHiDbl(dbl2)) &&
  166. 0 == Js::NumberUtilities::LuLoDbl(dbl1);
  167. }
  168. // The bit patterns match. They are equal iff they are not Nan.
  169. return !Js::NumberUtilities::IsNan(dbl1);
  170. }
  171. Var JavascriptOperators::OP_ApplyArgs(Var func, Var instance, __in_xcount(8) void** stackPtr, CallInfo callInfo, ScriptContext* scriptContext)
  172. {
  173. JIT_HELPER_REENTRANT_HEADER(Op_OP_ApplyArgs);
  174. int argCount = callInfo.Count;
  175. ///
  176. /// Check func has internal [[Call]] property
  177. /// If not, throw TypeError
  178. ///
  179. if (!JavascriptConversion::IsCallable(func)) {
  180. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction);
  181. }
  182. // Fix callInfo: expect result/value, and none of other flags are currently applicable.
  183. // OP_ApplyArgs expects a result. Neither of {jit, interpreted} mode sends correct callFlags:
  184. // LdArgCnt -- jit sends whatever was passed to current function, interpreter always sends 0.
  185. // See Win8 bug 490489.
  186. callInfo.Flags = CallFlags_Value;
  187. RecyclableObject *funcPtr = UnsafeVarTo<RecyclableObject>(func);
  188. PROBE_STACK(scriptContext, Js::Constants::MinStackDefault + argCount * 4);
  189. JavascriptMethod entryPoint = funcPtr->GetEntryPoint();
  190. Var ret;
  191. switch (argCount) {
  192. case 0:
  193. Assert(false);
  194. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo);
  195. break;
  196. case 1:
  197. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance);
  198. break;
  199. case 2:
  200. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0]);
  201. break;
  202. case 3:
  203. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1]);
  204. break;
  205. case 4:
  206. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2]);
  207. break;
  208. case 5:
  209. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2], stackPtr[3]);
  210. break;
  211. case 6:
  212. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2], stackPtr[3], stackPtr[4]);
  213. break;
  214. case 7:
  215. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2], stackPtr[3], stackPtr[4], stackPtr[5]);
  216. break;
  217. default:
  218. {
  219. // Don't need stack probe here- we just did so above
  220. Arguments args(callInfo, stackPtr - 1);
  221. BEGIN_SAFE_REENTRANT_CALL(scriptContext->GetThreadContext())
  222. {
  223. ret = JavascriptFunction::CallFunction<false>(funcPtr, entryPoint, args);
  224. }
  225. END_SAFE_REENTRANT_CALL
  226. break;
  227. }
  228. }
  229. return ret;
  230. JIT_HELPER_END(Op_OP_ApplyArgs);
  231. }
  232. #ifdef _M_IX86
  233. // Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
  234. Var JavascriptOperators::Int32ToVar(int32 value, ScriptContext* scriptContext)
  235. {
  236. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_Int32ToAtom);
  237. return JavascriptNumber::ToVar(value, scriptContext);
  238. JIT_HELPER_END(Op_Int32ToAtom);
  239. }
  240. // Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
  241. Var JavascriptOperators::Int32ToVarInPlace(int32 value, ScriptContext* scriptContext, JavascriptNumber* result)
  242. {
  243. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_Int32ToAtomInPlace);
  244. return JavascriptNumber::ToVarInPlace(value, scriptContext, result);
  245. JIT_HELPER_END(Op_Int32ToAtomInPlace);
  246. }
  247. // Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
  248. Var JavascriptOperators::UInt32ToVar(uint32 value, ScriptContext* scriptContext)
  249. {
  250. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_UInt32ToAtom);
  251. return JavascriptNumber::ToVar(value, scriptContext);
  252. JIT_HELPER_END(Op_UInt32ToAtom);
  253. }
  254. // Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
  255. Var JavascriptOperators::UInt32ToVarInPlace(uint32 value, ScriptContext* scriptContext, JavascriptNumber* result)
  256. {
  257. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_UInt32ToAtomInPlace);
  258. return JavascriptNumber::ToVarInPlace(value, scriptContext, result);
  259. JIT_HELPER_END(Op_UInt32ToAtomInPlace);
  260. }
  261. #endif
  262. Var JavascriptOperators::OP_FinishOddDivBy2(uint32 value, ScriptContext *scriptContext)
  263. {
  264. return JavascriptNumber::New((double)(value + 0.5), scriptContext);
  265. }
  266. Var JavascriptOperators::ToNumberInPlace(Var aRight, ScriptContext* scriptContext, JavascriptNumber* result)
  267. {
  268. JIT_HELPER_REENTRANT_HEADER(Op_ConvNumberInPlace);
  269. if (TaggedInt::Is(aRight) || JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  270. {
  271. return aRight;
  272. }
  273. return JavascriptNumber::ToVarInPlace(JavascriptConversion::ToNumber(aRight, scriptContext), scriptContext, result);
  274. JIT_HELPER_END(Op_ConvNumberInPlace);
  275. }
  276. Var JavascriptOperators::ToNumericInPlace(Var aRight, ScriptContext* scriptContext, JavascriptNumber* result)
  277. {
  278. if (JavascriptOperators::GetTypeId(aRight) == TypeIds_BigInt)
  279. {
  280. return aRight;
  281. }
  282. return JavascriptOperators::ToNumberInPlace(aRight, scriptContext, result);
  283. }
  284. Var JavascriptOperators::Typeof(Var var, ScriptContext* scriptContext)
  285. {
  286. JIT_HELPER_REENTRANT_HEADER(Op_Typeof);
  287. switch (JavascriptOperators::GetTypeId(var))
  288. {
  289. case TypeIds_Undefined:
  290. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  291. case TypeIds_Null:
  292. //null
  293. return scriptContext->GetLibrary()->GetObjectTypeDisplayString();
  294. case TypeIds_Integer:
  295. case TypeIds_Number:
  296. case TypeIds_Int64Number:
  297. case TypeIds_UInt64Number:
  298. return scriptContext->GetLibrary()->GetNumberTypeDisplayString();
  299. default:
  300. // Falsy objects are typeof 'undefined'.
  301. if (VarTo<RecyclableObject>(var)->GetType()->IsFalsy())
  302. {
  303. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  304. }
  305. else
  306. {
  307. return VarTo<RecyclableObject>(var)->GetTypeOfString(scriptContext);
  308. }
  309. }
  310. JIT_HELPER_END(Op_Typeof);
  311. }
  312. Var JavascriptOperators::TypeofFld(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  313. {
  314. return TypeofFld_Internal(instance, false, propertyId, scriptContext);
  315. }
  316. Var JavascriptOperators::TypeofRootFld(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  317. {
  318. return TypeofFld_Internal(instance, true, propertyId, scriptContext);
  319. }
  320. Var JavascriptOperators::TypeofFld_Internal(Var instance, const bool isRoot, PropertyId propertyId, ScriptContext* scriptContext)
  321. {
  322. RecyclableObject* object = nullptr;
  323. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  324. {
  325. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined , scriptContext->GetPropertyName(propertyId)->GetBuffer());
  326. }
  327. Var value = nullptr;
  328. try
  329. {
  330. Js::JavascriptExceptionOperators::AutoCatchHandlerExists autoCatchHandlerExists(scriptContext);
  331. // In edge mode, spec compat is more important than backward compat. Use spec/web behavior here
  332. if (isRoot
  333. ? !JavascriptOperators::GetRootProperty(instance, propertyId, &value, scriptContext)
  334. : !JavascriptOperators::GetProperty(instance, object, propertyId, &value, scriptContext))
  335. {
  336. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  337. }
  338. if (!scriptContext->IsUndeclBlockVar(value))
  339. {
  340. return JavascriptOperators::Typeof(value, scriptContext);
  341. }
  342. }
  343. catch(const JavascriptException& err)
  344. {
  345. err.GetAndClear(); // discard exception object
  346. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  347. }
  348. Assert(scriptContext->IsUndeclBlockVar(value));
  349. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  350. }
  351. Var JavascriptOperators::TypeofElem_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  352. {
  353. JIT_HELPER_REENTRANT_HEADER(Op_TypeofElem_UInt32);
  354. if (JavascriptOperators::IsNumberFromNativeArray(instance, index, scriptContext))
  355. return scriptContext->GetLibrary()->GetNumberTypeDisplayString();
  356. #if FLOATVAR
  357. return TypeofElem(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  358. #else
  359. char buffer[sizeof(Js::JavascriptNumber)];
  360. return TypeofElem(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  361. (Js::JavascriptNumber *)buffer), scriptContext);
  362. #endif
  363. JIT_HELPER_END(Op_TypeofElem_UInt32);
  364. }
  365. Var JavascriptOperators::TypeofElem_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  366. {
  367. JIT_HELPER_REENTRANT_HEADER(Op_TypeofElem_Int32);
  368. if (JavascriptOperators::IsNumberFromNativeArray(instance, index, scriptContext))
  369. return scriptContext->GetLibrary()->GetNumberTypeDisplayString();
  370. #if FLOATVAR
  371. return TypeofElem(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  372. #else
  373. char buffer[sizeof(Js::JavascriptNumber)];
  374. return TypeofElem(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  375. (Js::JavascriptNumber *)buffer), scriptContext);
  376. #endif
  377. JIT_HELPER_END(Op_TypeofElem_Int32);
  378. }
  379. Js::JavascriptString* GetPropertyDisplayNameForError(Var prop, ScriptContext* scriptContext)
  380. {
  381. JavascriptString* str;
  382. JavascriptSymbol *symbol = JavascriptOperators::TryFromVar<JavascriptSymbol>(prop);
  383. if (symbol)
  384. {
  385. str = JavascriptSymbol::ToString(symbol->GetValue(), scriptContext);
  386. }
  387. else
  388. {
  389. str = JavascriptConversion::ToString(prop, scriptContext);
  390. }
  391. return str;
  392. }
  393. Var JavascriptOperators::TypeofElem(Var instance, Var index, ScriptContext* scriptContext)
  394. {
  395. JIT_HELPER_REENTRANT_HEADER(Op_TypeofElem);
  396. RecyclableObject* object = nullptr;
  397. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  398. {
  399. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined, GetPropertyDisplayNameForError(index, scriptContext));
  400. }
  401. Var member = nullptr;
  402. uint32 indexVal;
  403. PropertyRecord const * propertyRecord = nullptr;
  404. ThreadContext* threadContext = scriptContext->GetThreadContext();
  405. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  406. threadContext->ClearImplicitCallFlags();
  407. try
  408. {
  409. Js::JavascriptExceptionOperators::AutoCatchHandlerExists autoCatchHandlerExists(scriptContext);
  410. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, false);
  411. // For JS Objects, don't create the propertyId if not already added
  412. if (indexType == IndexType_Number)
  413. {
  414. // In edge mode, we don't need to worry about the special "unknown" behavior. If the item is not available from Get,
  415. // just return undefined.
  416. if (!JavascriptOperators::GetItem(instance, object, indexVal, &member, scriptContext))
  417. {
  418. // If the instance doesn't have the item, typeof result is "undefined".
  419. threadContext->CheckAndResetImplicitCallAccessorFlag();
  420. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  421. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  422. }
  423. }
  424. else
  425. {
  426. Assert(indexType == IndexType_PropertyId);
  427. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  428. {
  429. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
  430. Assert(indexType == IndexType_PropertyId);
  431. Assert(propertyRecord != nullptr);
  432. }
  433. if (propertyRecord != nullptr)
  434. {
  435. if (!JavascriptOperators::GetProperty(instance, object, propertyRecord->GetPropertyId(), &member, scriptContext))
  436. {
  437. // If the instance doesn't have the property, typeof result is "undefined".
  438. threadContext->CheckAndResetImplicitCallAccessorFlag();
  439. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  440. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  441. }
  442. }
  443. else
  444. {
  445. #if DBG
  446. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  447. PropertyRecord const * debugPropertyRecord;
  448. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  449. 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");
  450. #endif
  451. // If the instance doesn't have the property, typeof result is "undefined".
  452. threadContext->CheckAndResetImplicitCallAccessorFlag();
  453. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  454. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  455. }
  456. }
  457. threadContext->CheckAndResetImplicitCallAccessorFlag();
  458. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  459. return JavascriptOperators::Typeof(member, scriptContext);
  460. }
  461. catch(const JavascriptException& err)
  462. {
  463. err.GetAndClear(); // discard exception object
  464. threadContext->CheckAndResetImplicitCallAccessorFlag();
  465. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  466. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  467. }
  468. JIT_HELPER_END(Op_TypeofElem);
  469. }
  470. //
  471. // Delete the given Var
  472. //
  473. Var JavascriptOperators::Delete(Var var, ScriptContext* scriptContext)
  474. {
  475. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_Delete);
  476. return scriptContext->GetLibrary()->GetTrue();
  477. JIT_HELPER_END(Op_Delete);
  478. }
  479. BOOL JavascriptOperators::Equal_Full(Var aLeft, Var aRight, ScriptContext* requestContext)
  480. {
  481. JIT_HELPER_REENTRANT_HEADER(Op_Equal_Full);
  482. //
  483. // Fast-path SmInts and paired Number combinations.
  484. //
  485. if (aLeft == aRight)
  486. {
  487. if (JavascriptNumber::Is(aLeft) && JavascriptNumber::IsNan(JavascriptNumber::GetValue(aLeft)))
  488. {
  489. return false;
  490. }
  491. else
  492. {
  493. return true;
  494. }
  495. }
  496. BOOL result = false;
  497. if (TaggedInt::Is(aLeft))
  498. {
  499. if (TaggedInt::Is(aRight))
  500. {
  501. // If aLeft == aRight, we would already have returned true above.
  502. return false;
  503. }
  504. else if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  505. {
  506. return TaggedInt::ToDouble(aLeft) == JavascriptNumber::GetValue(aRight);
  507. }
  508. else
  509. {
  510. BOOL res = UnsafeVarTo<RecyclableObject>(aRight)->Equals(aLeft, &result, requestContext);
  511. AssertMsg(res, "Should have handled this");
  512. return result;
  513. }
  514. }
  515. else if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  516. {
  517. if (TaggedInt::Is(aRight))
  518. {
  519. return TaggedInt::ToDouble(aRight) == JavascriptNumber::GetValue(aLeft);
  520. }
  521. else if(JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  522. {
  523. return JavascriptNumber::GetValue(aLeft) == JavascriptNumber::GetValue(aRight);
  524. }
  525. else
  526. {
  527. BOOL res = UnsafeVarTo<RecyclableObject>(aRight)->Equals(aLeft, &result, requestContext);
  528. AssertMsg(res, "Should have handled this");
  529. return result;
  530. }
  531. }
  532. if (UnsafeVarTo<RecyclableObject>(aLeft)->Equals(aRight, &result, requestContext))
  533. {
  534. return result;
  535. }
  536. else
  537. {
  538. return false;
  539. }
  540. JIT_HELPER_END(Op_Equal_Full);
  541. }
  542. BOOL JavascriptOperators::Greater_Full(Var aLeft,Var aRight,ScriptContext* scriptContext)
  543. {
  544. JIT_HELPER_REENTRANT_HEADER(Op_Greater_Full);
  545. return RelationalComparisonHelper(aRight, aLeft, scriptContext, false, false);
  546. JIT_HELPER_END(Op_Greater_Full);
  547. }
  548. BOOL JavascriptOperators::Less_Full(Var aLeft, Var aRight, ScriptContext* scriptContext)
  549. {
  550. return RelationalComparisonHelper(aLeft, aRight, scriptContext, true, false);
  551. }
  552. BOOL JavascriptOperators::RelationalComparisonHelper(Var aLeft, Var aRight, ScriptContext* scriptContext, bool leftFirst, bool undefinedAs)
  553. {
  554. TypeId typeId = JavascriptOperators::GetTypeId(aLeft);
  555. if (typeId == TypeIds_Null)
  556. {
  557. aLeft=TaggedInt::ToVarUnchecked(0);
  558. }
  559. else if (typeId == TypeIds_Undefined)
  560. {
  561. aLeft=scriptContext->GetLibrary()->GetNaN();
  562. }
  563. typeId = JavascriptOperators::GetTypeId(aRight);
  564. if (typeId == TypeIds_Null)
  565. {
  566. aRight=TaggedInt::ToVarUnchecked(0);
  567. }
  568. else if (typeId == TypeIds_Undefined)
  569. {
  570. aRight=scriptContext->GetLibrary()->GetNaN();
  571. }
  572. double dblLeft, dblRight;
  573. TypeId leftType = JavascriptOperators::GetTypeId(aLeft);
  574. TypeId rightType = JavascriptOperators::GetTypeId(aRight);
  575. if ((leftType == TypeIds_BigInt) || (rightType == TypeIds_BigInt))
  576. {
  577. // TODO: support comparison with types other than BigInt
  578. AssertOrFailFastMsg(leftType == rightType, "do not support comparison with types other than BigInt");
  579. return JavascriptBigInt::LessThan(aLeft, aRight);
  580. }
  581. switch (leftType)
  582. {
  583. case TypeIds_Integer:
  584. dblLeft = TaggedInt::ToDouble(aLeft);
  585. switch (rightType)
  586. {
  587. case TypeIds_Integer:
  588. dblRight = TaggedInt::ToDouble(aRight);
  589. break;
  590. case TypeIds_Number:
  591. dblRight = JavascriptNumber::GetValue(aRight);
  592. break;
  593. default:
  594. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  595. break;
  596. }
  597. break;
  598. case TypeIds_Number:
  599. dblLeft = JavascriptNumber::GetValue(aLeft);
  600. switch (rightType)
  601. {
  602. case TypeIds_Integer:
  603. dblRight = TaggedInt::ToDouble(aRight);
  604. break;
  605. case TypeIds_Number:
  606. dblRight = JavascriptNumber::GetValue(aRight);
  607. break;
  608. default:
  609. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  610. break;
  611. }
  612. break;
  613. case TypeIds_Int64Number:
  614. {
  615. switch (rightType)
  616. {
  617. case TypeIds_Int64Number:
  618. {
  619. __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  620. __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
  621. return leftValue < rightValue;
  622. }
  623. break;
  624. case TypeIds_UInt64Number:
  625. {
  626. __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  627. unsigned __int64 rightValue = UnsafeVarTo<JavascriptUInt64Number>(aRight)->GetValue();
  628. if (rightValue <= INT_MAX && leftValue >= 0)
  629. {
  630. return leftValue < (__int64)rightValue;
  631. }
  632. }
  633. break;
  634. }
  635. dblLeft = (double)UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  636. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  637. }
  638. break;
  639. // we cannot do double conversion between 2 int64 numbers as we can get wrong result after conversion
  640. // i.e., two different numbers become the same after losing precision. We'll continue dbl comparison
  641. // if either number is not an int64 number.
  642. case TypeIds_UInt64Number:
  643. {
  644. switch (rightType)
  645. {
  646. case TypeIds_Int64Number:
  647. {
  648. unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  649. __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
  650. if (leftValue < INT_MAX && rightValue >= 0)
  651. {
  652. return (__int64)leftValue < rightValue;
  653. }
  654. }
  655. break;
  656. case TypeIds_UInt64Number:
  657. {
  658. unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  659. unsigned __int64 rightValue = UnsafeVarTo<JavascriptUInt64Number>(aRight)->GetValue();
  660. return leftValue < rightValue;
  661. }
  662. break;
  663. }
  664. dblLeft = (double)UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  665. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  666. }
  667. break;
  668. case TypeIds_String:
  669. switch (rightType)
  670. {
  671. case TypeIds_Integer:
  672. case TypeIds_Number:
  673. case TypeIds_Boolean:
  674. break;
  675. default:
  676. aRight = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aRight, scriptContext);
  677. rightType = JavascriptOperators::GetTypeId(aRight);
  678. if (rightType != TypeIds_String)
  679. {
  680. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  681. break;
  682. }
  683. case TypeIds_String:
  684. return JavascriptString::LessThan(aLeft, aRight);
  685. }
  686. dblLeft = JavascriptConversion::ToNumber(aLeft, scriptContext);
  687. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  688. break;
  689. case TypeIds_Boolean:
  690. case TypeIds_Null:
  691. case TypeIds_Undefined:
  692. case TypeIds_Symbol:
  693. dblLeft = JavascriptConversion::ToNumber(aLeft, scriptContext);
  694. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  695. break;
  696. default:
  697. if (leftFirst)
  698. {
  699. aLeft = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aLeft, scriptContext);
  700. aRight = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aRight, scriptContext);
  701. }
  702. else
  703. {
  704. aRight = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aRight, scriptContext);
  705. aLeft = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aLeft, scriptContext);
  706. }
  707. //BugFix: When @@ToPrimitive of an object is overridden with a function that returns null/undefined
  708. //this helper will fall into a inescapable goto loop as the checks for null/undefined were outside of the path
  709. return RelationalComparisonHelper(aLeft, aRight, scriptContext, leftFirst, undefinedAs);
  710. }
  711. //
  712. // And +0,-0 that is not implemented fully
  713. //
  714. if (JavascriptNumber::IsNan(dblLeft) || JavascriptNumber::IsNan(dblRight))
  715. {
  716. return undefinedAs;
  717. }
  718. // this will succeed for -0.0 == 0.0 case as well
  719. if (dblLeft == dblRight)
  720. {
  721. return false;
  722. }
  723. return dblLeft < dblRight;
  724. }
  725. BOOL JavascriptOperators::StrictEqualString(Var aLeft, JavascriptString* aRight)
  726. {
  727. JIT_HELPER_REENTRANT_HEADER(Op_StrictEqualString);
  728. JIT_HELPER_SAME_ATTRIBUTES(Op_StrictEqualString, Op_StrictEqual);
  729. JavascriptString* leftStr = TryFromVar<JavascriptString>(aLeft);
  730. if (!leftStr)
  731. {
  732. return false;
  733. }
  734. JIT_HELPER_REENTRANT_HEADER(Op_StrictEqualString);
  735. JIT_HELPER_SAME_ATTRIBUTES(Op_StrictEqualString, Op_StrictEqual);
  736. return JavascriptString::Equals(leftStr, aRight);
  737. JIT_HELPER_END(Op_StrictEqualString);
  738. }
  739. BOOL JavascriptOperators::StrictEqualEmptyString(Var aLeft)
  740. {
  741. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_StrictEqualEmptyString);
  742. JavascriptString * string = JavascriptOperators::TryFromVar<JavascriptString>(aLeft);
  743. if (!string)
  744. {
  745. return false;
  746. }
  747. Assert(string);
  748. return string->GetLength() == 0;
  749. JIT_HELPER_END(Op_StrictEqualEmptyString);
  750. }
  751. #ifdef _CHAKRACOREBUILD
  752. BOOL JavascriptOperators::StrictEqualNumberType(Var aLeft, Var aRight, TypeId leftType, TypeId rightType, ScriptContext *requestContext)
  753. {
  754. double dblLeft, dblRight;
  755. switch (leftType)
  756. {
  757. case TypeIds_Integer:
  758. switch (rightType)
  759. {
  760. case TypeIds_Integer:
  761. return aLeft == aRight;
  762. // we don't need to worry about int64: it cannot equal as we create
  763. // JavascriptInt64Number only in overflow scenarios.
  764. case TypeIds_Number:
  765. dblLeft = TaggedInt::ToDouble(aLeft);
  766. dblRight = JavascriptNumber::GetValue(aRight);
  767. goto CommonNumber;
  768. }
  769. return FALSE;
  770. case TypeIds_Int64Number:
  771. switch (rightType)
  772. {
  773. case TypeIds_Int64Number:
  774. {
  775. __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  776. __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
  777. return leftValue == rightValue;
  778. }
  779. case TypeIds_UInt64Number:
  780. {
  781. __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  782. unsigned __int64 rightValue = VarTo<JavascriptUInt64Number>(aRight)->GetValue();
  783. return ((unsigned __int64)leftValue == rightValue);
  784. }
  785. case TypeIds_Number:
  786. dblLeft = (double)UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  787. dblRight = JavascriptNumber::GetValue(aRight);
  788. goto CommonNumber;
  789. }
  790. return FALSE;
  791. case TypeIds_UInt64Number:
  792. switch (rightType)
  793. {
  794. case TypeIds_Int64Number:
  795. {
  796. unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  797. __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
  798. return (leftValue == (unsigned __int64)rightValue);
  799. }
  800. case TypeIds_UInt64Number:
  801. {
  802. unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  803. unsigned __int64 rightValue = VarTo<JavascriptUInt64Number>(aRight)->GetValue();
  804. return leftValue == rightValue;
  805. }
  806. case TypeIds_Number:
  807. dblLeft = (double)UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  808. dblRight = JavascriptNumber::GetValue(aRight);
  809. goto CommonNumber;
  810. }
  811. return FALSE;
  812. case TypeIds_Number:
  813. switch (rightType)
  814. {
  815. case TypeIds_Integer:
  816. dblLeft = JavascriptNumber::GetValue(aLeft);
  817. dblRight = TaggedInt::ToDouble(aRight);
  818. goto CommonNumber;
  819. case TypeIds_Int64Number:
  820. dblLeft = JavascriptNumber::GetValue(aLeft);
  821. dblRight = (double)VarTo<JavascriptInt64Number>(aRight)->GetValue();
  822. goto CommonNumber;
  823. case TypeIds_UInt64Number:
  824. dblLeft = JavascriptNumber::GetValue(aLeft);
  825. dblRight = (double)UnsafeVarTo<JavascriptUInt64Number>(aRight)->GetValue();
  826. goto CommonNumber;
  827. case TypeIds_Number:
  828. dblLeft = JavascriptNumber::GetValue(aLeft);
  829. dblRight = JavascriptNumber::GetValue(aRight);
  830. CommonNumber:
  831. return FEqualDbl(dblLeft, dblRight);
  832. }
  833. return FALSE;
  834. }
  835. Assert(0 && "Unreachable Code");
  836. return FALSE;
  837. }
  838. BOOL JavascriptOperators::StrictEqual(Var aLeft, Var aRight, ScriptContext* requestContext)
  839. {
  840. JIT_HELPER_REENTRANT_HEADER(Op_StrictEqual);
  841. TypeId rightType, leftType;
  842. leftType = JavascriptOperators::GetTypeId(aLeft);
  843. // Because NaN !== NaN, we may not return TRUE when typeId is Number
  844. if (aLeft == aRight && leftType != TypeIds_Number) return TRUE;
  845. rightType = JavascriptOperators::GetTypeId(aRight);
  846. if (leftType == TypeIds_String)
  847. {
  848. if (rightType == TypeIds_String)
  849. {
  850. return JavascriptString::Equals(UnsafeVarTo<JavascriptString>(aLeft), UnsafeVarTo<JavascriptString>(aRight));
  851. }
  852. return FALSE;
  853. }
  854. else if (leftType >= TypeIds_Integer && leftType <= TypeIds_UInt64Number)
  855. {
  856. return JavascriptOperators::StrictEqualNumberType(aLeft, aRight, leftType, rightType, requestContext);
  857. }
  858. else if (leftType == TypeIds_GlobalObject)
  859. {
  860. BOOL result;
  861. if (UnsafeVarTo<RecyclableObject>(aLeft)->StrictEquals(aRight, &result, requestContext))
  862. {
  863. return result;
  864. }
  865. return false;
  866. }
  867. else if (leftType == TypeIds_BigInt)
  868. {
  869. if (rightType == TypeIds_BigInt)
  870. {
  871. return JavascriptBigInt::Equals(aLeft, aRight);
  872. }
  873. return FALSE;
  874. }
  875. return aLeft == aRight;
  876. JIT_HELPER_END(Op_StrictEqual);
  877. }
  878. #else
  879. BOOL JavascriptOperators::StrictEqual(Var aLeft, Var aRight, ScriptContext* requestContext)
  880. {
  881. JIT_HELPER_REENTRANT_HEADER(Op_StrictEqual);
  882. double dblLeft, dblRight;
  883. TypeId rightType, leftType;
  884. leftType = JavascriptOperators::GetTypeId(aLeft);
  885. // Because NaN !== NaN, we may not return TRUE when typeId is Number
  886. if (aLeft == aRight && leftType != TypeIds_Number) return TRUE;
  887. rightType = JavascriptOperators::GetTypeId(aRight);
  888. switch (leftType)
  889. {
  890. case TypeIds_String:
  891. switch (rightType)
  892. {
  893. case TypeIds_String:
  894. return JavascriptString::Equals(UnsafeVarTo<JavascriptString>(aLeft), UnsafeVarTo<JavascriptString>(aRight));
  895. }
  896. return FALSE;
  897. case TypeIds_Integer:
  898. switch (rightType)
  899. {
  900. case TypeIds_Integer:
  901. return aLeft == aRight;
  902. // we don't need to worry about int64: it cannot equal as we create
  903. // JavascriptInt64Number only in overflow scenarios.
  904. case TypeIds_Number:
  905. dblLeft = TaggedInt::ToDouble(aLeft);
  906. dblRight = JavascriptNumber::GetValue(aRight);
  907. goto CommonNumber;
  908. }
  909. return FALSE;
  910. case TypeIds_Int64Number:
  911. switch (rightType)
  912. {
  913. case TypeIds_Int64Number:
  914. {
  915. __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  916. __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
  917. return leftValue == rightValue;
  918. }
  919. case TypeIds_UInt64Number:
  920. {
  921. __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  922. unsigned __int64 rightValue = VarTo<JavascriptUInt64Number>(aRight)->GetValue();
  923. return ((unsigned __int64)leftValue == rightValue);
  924. }
  925. case TypeIds_Number:
  926. dblLeft = (double)UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  927. dblRight = JavascriptNumber::GetValue(aRight);
  928. goto CommonNumber;
  929. }
  930. return FALSE;
  931. case TypeIds_UInt64Number:
  932. switch (rightType)
  933. {
  934. case TypeIds_Int64Number:
  935. {
  936. unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  937. __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
  938. return (leftValue == (unsigned __int64)rightValue);
  939. }
  940. case TypeIds_UInt64Number:
  941. {
  942. unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  943. unsigned __int64 rightValue = VarTo<JavascriptUInt64Number>(aRight)->GetValue();
  944. return leftValue == rightValue;
  945. }
  946. case TypeIds_Number:
  947. dblLeft = (double)UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  948. dblRight = JavascriptNumber::GetValue(aRight);
  949. goto CommonNumber;
  950. }
  951. return FALSE;
  952. case TypeIds_Number:
  953. switch (rightType)
  954. {
  955. case TypeIds_Integer:
  956. dblLeft = JavascriptNumber::GetValue(aLeft);
  957. dblRight = TaggedInt::ToDouble(aRight);
  958. goto CommonNumber;
  959. case TypeIds_Int64Number:
  960. dblLeft = JavascriptNumber::GetValue(aLeft);
  961. dblRight = (double)VarTo<JavascriptInt64Number>(aRight)->GetValue();
  962. goto CommonNumber;
  963. case TypeIds_UInt64Number:
  964. dblLeft = JavascriptNumber::GetValue(aLeft);
  965. dblRight = (double)UnsafeVarTo<JavascriptUInt64Number>(aRight)->GetValue();
  966. goto CommonNumber;
  967. case TypeIds_Number:
  968. dblLeft = JavascriptNumber::GetValue(aLeft);
  969. dblRight = JavascriptNumber::GetValue(aRight);
  970. CommonNumber:
  971. return FEqualDbl(dblLeft, dblRight);
  972. }
  973. return FALSE;
  974. case TypeIds_BigInt:
  975. switch (rightType)
  976. {
  977. case TypeIds_BigInt:
  978. return JavascriptBigInt::Equals(aLeft, aRight);
  979. }
  980. return FALSE;
  981. case TypeIds_Boolean:
  982. switch (rightType)
  983. {
  984. case TypeIds_Boolean:
  985. return aLeft == aRight;
  986. }
  987. return FALSE;
  988. case TypeIds_Undefined:
  989. return rightType == TypeIds_Undefined;
  990. case TypeIds_Null:
  991. return rightType == TypeIds_Null;
  992. case TypeIds_Array:
  993. return (rightType == TypeIds_Array && aLeft == aRight);
  994. #if DBG
  995. case TypeIds_Symbol:
  996. if (rightType == TypeIds_Symbol)
  997. {
  998. const PropertyRecord* leftValue = UnsafeVarTo<JavascriptSymbol>(aLeft)->GetValue();
  999. const PropertyRecord* rightValue = UnsafeVarTo<JavascriptSymbol>(aRight)->GetValue();
  1000. Assert(leftValue != rightValue);
  1001. }
  1002. break;
  1003. #endif
  1004. case TypeIds_GlobalObject:
  1005. case TypeIds_HostDispatch:
  1006. switch (rightType)
  1007. {
  1008. case TypeIds_HostDispatch:
  1009. case TypeIds_GlobalObject:
  1010. {
  1011. BOOL result;
  1012. if(UnsafeVarTo<RecyclableObject>(aLeft)->StrictEquals(aRight, &result, requestContext))
  1013. {
  1014. return result;
  1015. }
  1016. return false;
  1017. }
  1018. }
  1019. break;
  1020. }
  1021. if (VarTo<RecyclableObject>(aLeft)->IsExternal())
  1022. {
  1023. BOOL result;
  1024. if (VarTo<RecyclableObject>(aLeft)->StrictEquals(aRight, &result, requestContext))
  1025. {
  1026. if (result)
  1027. {
  1028. return TRUE;
  1029. }
  1030. }
  1031. }
  1032. if (!TaggedNumber::Is(aRight) && VarTo<RecyclableObject>(aRight)->IsExternal())
  1033. {
  1034. BOOL result;
  1035. if (VarTo<RecyclableObject>(aRight)->StrictEquals(aLeft, &result, requestContext))
  1036. {
  1037. if (result)
  1038. {
  1039. return TRUE;
  1040. }
  1041. }
  1042. }
  1043. return aLeft == aRight;
  1044. JIT_HELPER_END(Op_StrictEqual);
  1045. }
  1046. #endif
  1047. BOOL JavascriptOperators::HasOwnProperty(
  1048. Var instance,
  1049. PropertyId propertyId,
  1050. _In_ ScriptContext* requestContext,
  1051. _In_opt_ PropertyString* propString)
  1052. {
  1053. if (TaggedNumber::Is(instance))
  1054. {
  1055. return FALSE;
  1056. }
  1057. RecyclableObject* object = UnsafeVarTo<RecyclableObject>(instance);
  1058. if (VarIs<JavascriptProxy>(instance))
  1059. {
  1060. PropertyDescriptor desc;
  1061. return GetOwnPropertyDescriptor(object, propertyId, requestContext, &desc);
  1062. }
  1063. // If we have a PropertyString, attempt to shortcut the lookup by using its caches
  1064. if (propString != nullptr)
  1065. {
  1066. PropertyCacheOperationInfo info;
  1067. if (propString->GetLdElemInlineCache()->PretendTryGetProperty(object->GetType(), &info))
  1068. {
  1069. switch (info.cacheType)
  1070. {
  1071. case CacheType_Local:
  1072. Assert(object->HasOwnProperty(propertyId));
  1073. return TRUE;
  1074. case CacheType_Proto:
  1075. Assert(!object->HasOwnProperty(propertyId));
  1076. return FALSE;
  1077. default:
  1078. // We had a cache hit, but cache doesn't tell us if we have an own property
  1079. break;
  1080. }
  1081. }
  1082. if (propString->GetStElemInlineCache()->PretendTrySetProperty(object->GetType(), object->GetType(), &info))
  1083. {
  1084. switch (info.cacheType)
  1085. {
  1086. case CacheType_Local:
  1087. Assert(object->HasOwnProperty(propertyId));
  1088. return TRUE;
  1089. case CacheType_LocalWithoutProperty:
  1090. Assert(!object->HasOwnProperty(propertyId));
  1091. return FALSE;
  1092. default:
  1093. // We had a cache hit, but cache doesn't tell us if we have an own property
  1094. break;
  1095. }
  1096. }
  1097. }
  1098. return object && object->HasOwnProperty(propertyId);
  1099. }
  1100. BOOL JavascriptOperators::GetOwnAccessors(Var instance, PropertyId propertyId, Var* getter, Var* setter, ScriptContext * requestContext)
  1101. {
  1102. BOOL result;
  1103. if (TaggedNumber::Is(instance))
  1104. {
  1105. result = false;
  1106. }
  1107. else
  1108. {
  1109. RecyclableObject* object = UnsafeVarTo<RecyclableObject>(instance);
  1110. result = object && object->GetAccessors(propertyId, getter, setter, requestContext);
  1111. }
  1112. return result;
  1113. }
  1114. JavascriptArray* JavascriptOperators::GetOwnPropertyNames(Var instance, ScriptContext *scriptContext)
  1115. {
  1116. RecyclableObject *object = ToObject(instance, scriptContext);
  1117. AssertOrFailFast(VarIsCorrectType(object)); // Consider moving this check into ToObject
  1118. JavascriptProxy * proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  1119. if (proxy)
  1120. {
  1121. return proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::GetOwnPropertyNamesKind, scriptContext);
  1122. }
  1123. return JavascriptObject::CreateOwnStringPropertiesHelper(object, scriptContext);
  1124. }
  1125. JavascriptArray* JavascriptOperators::GetOwnPropertySymbols(Var instance, ScriptContext *scriptContext)
  1126. {
  1127. RecyclableObject *object = ToObject(instance, scriptContext);
  1128. AssertOrFailFast(VarIsCorrectType(object));
  1129. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_getOwnPropertySymbols);
  1130. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  1131. if (proxy)
  1132. {
  1133. return proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::GetOwnPropertySymbolKind, scriptContext);
  1134. }
  1135. return JavascriptObject::CreateOwnSymbolPropertiesHelper(object, scriptContext);
  1136. }
  1137. JavascriptArray* JavascriptOperators::GetOwnPropertyKeys(Var instance, ScriptContext* scriptContext)
  1138. {
  1139. RecyclableObject *object = ToObject(instance, scriptContext);
  1140. AssertOrFailFast(VarIsCorrectType(object));
  1141. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  1142. if (proxy)
  1143. {
  1144. return proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::KeysKind, scriptContext);
  1145. }
  1146. return JavascriptObject::CreateOwnStringSymbolPropertiesHelper(object, scriptContext);
  1147. }
  1148. JavascriptArray* JavascriptOperators::GetOwnEnumerablePropertyNames(RecyclableObject* object, ScriptContext* scriptContext)
  1149. {
  1150. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(object);
  1151. if (proxy)
  1152. {
  1153. JavascriptArray* proxyResult = proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::GetOwnPropertyNamesKind, scriptContext);
  1154. JavascriptArray* proxyResultToReturn = scriptContext->GetLibrary()->CreateArray(0);
  1155. // filter enumerable keys
  1156. uint32 resultLength = proxyResult->GetLength();
  1157. Var element;
  1158. const Js::PropertyRecord *propertyRecord = nullptr;
  1159. uint32 index = 0;
  1160. for (uint32 i = 0; i < resultLength; i++)
  1161. {
  1162. element = proxyResult->DirectGetItem(i);
  1163. Assert(!VarIs<JavascriptSymbol>(element));
  1164. PropertyDescriptor propertyDescriptor;
  1165. JavascriptConversion::ToPropertyKey(element, scriptContext, &propertyRecord, nullptr);
  1166. if (JavascriptOperators::GetOwnPropertyDescriptor(object, propertyRecord->GetPropertyId(), scriptContext, &propertyDescriptor))
  1167. {
  1168. if (propertyDescriptor.IsEnumerable())
  1169. {
  1170. proxyResultToReturn->DirectSetItemAt(index++, CrossSite::MarshalVar(scriptContext, element));
  1171. }
  1172. }
  1173. }
  1174. return proxyResultToReturn;
  1175. }
  1176. return JavascriptObject::CreateOwnEnumerableStringPropertiesHelper(object, scriptContext);
  1177. }
  1178. JavascriptArray* JavascriptOperators::GetOwnEnumerablePropertyNamesSymbols(RecyclableObject* object, ScriptContext* scriptContext)
  1179. {
  1180. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(object);
  1181. if (proxy)
  1182. {
  1183. return proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::KeysKind, scriptContext);
  1184. }
  1185. return JavascriptObject::CreateOwnEnumerableStringSymbolPropertiesHelper(object, scriptContext);
  1186. }
  1187. BOOL JavascriptOperators::GetOwnProperty(Var instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo * propertyValueInfo)
  1188. {
  1189. BOOL result;
  1190. if (TaggedNumber::Is(instance))
  1191. {
  1192. result = false;
  1193. }
  1194. else
  1195. {
  1196. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  1197. result = object && object->GetProperty(object, propertyId, value, propertyValueInfo, requestContext);
  1198. if (propertyValueInfo && result)
  1199. {
  1200. // 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
  1201. CacheOperators::CachePropertyRead(instance, object, false /* isRoot */, propertyId, false /* isMissing */, propertyValueInfo, requestContext);
  1202. }
  1203. }
  1204. return result;
  1205. }
  1206. BOOL JavascriptOperators::GetOwnPropertyDescriptor(RecyclableObject* obj, JavascriptString* propertyKey, ScriptContext* scriptContext, PropertyDescriptor* propertyDescriptor)
  1207. {
  1208. return JavascriptOperators::GetOwnPropertyDescriptor(obj, JavascriptOperators::GetPropertyId(propertyKey, scriptContext), scriptContext, propertyDescriptor);
  1209. }
  1210. // ES5's [[GetOwnProperty]].
  1211. // Return value:
  1212. // FALSE means "undefined" PD.
  1213. // TRUE means success. The propertyDescriptor parameter gets the descriptor.
  1214. //
  1215. BOOL JavascriptOperators::GetOwnPropertyDescriptor(RecyclableObject* obj, PropertyId propertyId, ScriptContext* scriptContext, PropertyDescriptor* propertyDescriptor)
  1216. {
  1217. Assert(obj);
  1218. Assert(scriptContext);
  1219. Assert(propertyDescriptor);
  1220. if (VarIs<JavascriptProxy>(obj))
  1221. {
  1222. return JavascriptProxy::GetOwnPropertyDescriptor(obj, propertyId, scriptContext, propertyDescriptor);
  1223. }
  1224. Var getter, setter;
  1225. if (false == JavascriptOperators::GetOwnAccessors(obj, propertyId, &getter, &setter, scriptContext))
  1226. {
  1227. Var value = nullptr;
  1228. if (false == JavascriptOperators::GetOwnProperty(obj, propertyId, &value, scriptContext, nullptr))
  1229. {
  1230. return FALSE;
  1231. }
  1232. if (nullptr != value)
  1233. {
  1234. propertyDescriptor->SetValue(value);
  1235. }
  1236. //CONSIDER : Its expensive to query for each flag from type system. Combine this with the GetOwnProperty to get all the flags
  1237. //at once. This will require a new API from type system and override in all the types which overrides IsEnumerable etc.
  1238. //Currently there is no performance tuning for ES5. This should be ok.
  1239. propertyDescriptor->SetWritable(FALSE != obj->IsWritable(propertyId));
  1240. }
  1241. else
  1242. {
  1243. if (nullptr == getter)
  1244. {
  1245. getter = scriptContext->GetLibrary()->GetUndefined();
  1246. }
  1247. propertyDescriptor->SetGetter(getter);
  1248. if (nullptr == setter)
  1249. {
  1250. setter = scriptContext->GetLibrary()->GetUndefined();
  1251. }
  1252. propertyDescriptor->SetSetter(setter);
  1253. }
  1254. propertyDescriptor->SetConfigurable(FALSE != obj->IsConfigurable(propertyId));
  1255. propertyDescriptor->SetEnumerable(FALSE != obj->IsEnumerable(propertyId));
  1256. return TRUE;
  1257. }
  1258. inline RecyclableObject* JavascriptOperators::GetPrototypeNoTrap(RecyclableObject* instance)
  1259. {
  1260. Type* type = instance->GetType();
  1261. if (type->HasSpecialPrototype())
  1262. {
  1263. if (type->GetTypeId() == TypeIds_Proxy)
  1264. {
  1265. // get back null
  1266. Assert(type->GetPrototype() == instance->GetScriptContext()->GetLibrary()->GetNull());
  1267. return type->GetPrototype();
  1268. }
  1269. else
  1270. {
  1271. return instance->GetPrototypeSpecial();
  1272. }
  1273. }
  1274. return type->GetPrototype();
  1275. }
  1276. BOOL JavascriptOperators::IsRemoteArray(RecyclableObject* instance)
  1277. {
  1278. TypeId remoteTypeId = TypeIds_Limit;
  1279. return (JavascriptOperators::GetRemoteTypeId(instance, &remoteTypeId) &&
  1280. DynamicObject::IsAnyArrayTypeId(remoteTypeId));
  1281. }
  1282. bool JavascriptOperators::IsArray(_In_ JavascriptProxy * instance)
  1283. {
  1284. // If it is a proxy, follow to the end of the proxy chain before checking if it is an array again.
  1285. JavascriptProxy * proxy = instance;
  1286. while (true)
  1287. {
  1288. RecyclableObject * targetInstance = proxy->GetTarget();
  1289. proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(targetInstance);
  1290. if (proxy == nullptr)
  1291. {
  1292. return DynamicObject::IsAnyArray(targetInstance) || IsRemoteArray(targetInstance);
  1293. }
  1294. }
  1295. }
  1296. bool JavascriptOperators::IsArray(_In_ RecyclableObject* instance)
  1297. {
  1298. if (DynamicObject::IsAnyArray(instance))
  1299. {
  1300. return TRUE;
  1301. }
  1302. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  1303. if (proxy)
  1304. {
  1305. return IsArray(proxy);
  1306. }
  1307. return IsRemoteArray(instance);
  1308. }
  1309. bool JavascriptOperators::IsArray(_In_ Var instanceVar)
  1310. {
  1311. RecyclableObject* instanceObj = TryFromVar<RecyclableObject>(instanceVar);
  1312. return instanceObj && IsArray(instanceObj);
  1313. }
  1314. bool JavascriptOperators::IsConstructor(_In_ JavascriptProxy * instance)
  1315. {
  1316. // If it is a proxy, follow to the end of the proxy chain before checking if it is a constructor again.
  1317. JavascriptProxy * proxy = instance;
  1318. while (true)
  1319. {
  1320. RecyclableObject* targetInstance = proxy->GetTarget();
  1321. proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(targetInstance);
  1322. if (proxy == nullptr)
  1323. {
  1324. JavascriptFunction* function = JavascriptOperators::TryFromVar<JavascriptFunction>(targetInstance);
  1325. return function && function->IsConstructor();
  1326. }
  1327. }
  1328. }
  1329. bool JavascriptOperators::IsConstructor(_In_ RecyclableObject* instanceObj)
  1330. {
  1331. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instanceObj);
  1332. if (proxy)
  1333. {
  1334. return IsConstructor(proxy);
  1335. }
  1336. JavascriptFunction* function = JavascriptOperators::TryFromVar<JavascriptFunction>(instanceObj);
  1337. return function && function->IsConstructor();
  1338. }
  1339. bool JavascriptOperators::IsConstructor(_In_ Var instanceVar)
  1340. {
  1341. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_IsConstructor);
  1342. RecyclableObject* instanceObj = TryFromVar<RecyclableObject>(instanceVar);
  1343. return instanceObj && IsConstructor(instanceObj);
  1344. JIT_HELPER_END(Op_IsConstructor);
  1345. }
  1346. BOOL JavascriptOperators::IsConcatSpreadable(Var instanceVar)
  1347. {
  1348. // an object is spreadable under two condition, either it is a JsArray
  1349. // or you define an isconcatSpreadable flag on it.
  1350. if (!JavascriptOperators::IsObject(instanceVar))
  1351. {
  1352. return false;
  1353. }
  1354. RecyclableObject* instance = UnsafeVarTo<RecyclableObject>(instanceVar);
  1355. ScriptContext* scriptContext = instance->GetScriptContext();
  1356. if (!PHASE_OFF1(IsConcatSpreadableCachePhase))
  1357. {
  1358. BOOL retVal = FALSE;
  1359. Type *instanceType = instance->GetType();
  1360. IsConcatSpreadableCache *isConcatSpreadableCache = scriptContext->GetThreadContext()->GetIsConcatSpreadableCache();
  1361. if (isConcatSpreadableCache->TryGetIsConcatSpreadable(instanceType, &retVal))
  1362. {
  1363. OUTPUT_TRACE(Phase::IsConcatSpreadableCachePhase, _u("IsConcatSpreadableCache hit: %p\n"), instanceType);
  1364. return retVal;
  1365. }
  1366. Var spreadable = nullptr;
  1367. BOOL hasUserDefinedSpreadable = JavascriptOperators::GetProperty(instance, instance, PropertyIds::_symbolIsConcatSpreadable, &spreadable, scriptContext);
  1368. if (hasUserDefinedSpreadable && spreadable != scriptContext->GetLibrary()->GetUndefined())
  1369. {
  1370. return JavascriptConversion::ToBoolean(spreadable, scriptContext);
  1371. }
  1372. retVal = JavascriptOperators::IsArray(instance);
  1373. if (!hasUserDefinedSpreadable)
  1374. {
  1375. OUTPUT_TRACE(Phase::IsConcatSpreadableCachePhase, _u("IsConcatSpreadableCache saved: %p\n"), instanceType);
  1376. isConcatSpreadableCache->CacheIsConcatSpreadable(instanceType, retVal);
  1377. }
  1378. return retVal;
  1379. }
  1380. Var spreadable = JavascriptOperators::GetProperty(instance, PropertyIds::_symbolIsConcatSpreadable, scriptContext);
  1381. if (spreadable != scriptContext->GetLibrary()->GetUndefined())
  1382. {
  1383. return JavascriptConversion::ToBoolean(spreadable, scriptContext);
  1384. }
  1385. return JavascriptOperators::IsArray(instance);
  1386. }
  1387. bool JavascriptOperators::IsConstructorSuperCall(Arguments args)
  1388. {
  1389. Var newTarget = args.GetNewTarget();
  1390. return args.IsNewCall() && newTarget != nullptr
  1391. && !JavascriptOperators::IsUndefined(newTarget);
  1392. }
  1393. bool JavascriptOperators::GetAndAssertIsConstructorSuperCall(Arguments args)
  1394. {
  1395. bool isCtorSuperCall = JavascriptOperators::IsConstructorSuperCall(args);
  1396. Assert(isCtorSuperCall || !args.IsNewCall()
  1397. || args[0] == nullptr || JavascriptOperators::GetTypeId(args[0]) == TypeIds_HostDispatch);
  1398. return isCtorSuperCall;
  1399. }
  1400. Var JavascriptOperators::OP_LdCustomSpreadIteratorList(Var aRight, ScriptContext* scriptContext)
  1401. {
  1402. JIT_HELPER_REENTRANT_HEADER(Op_ToSpreadedFunctionArgument);
  1403. #if ENABLE_COPYONACCESS_ARRAY
  1404. // We know we're going to read from this array. Do the conversion before we try to perform checks on the head segment.
  1405. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray(aRight);
  1406. #endif
  1407. #ifdef ENABLE_JS_BUILTINS
  1408. scriptContext->GetLibrary()->EnsureArrayBuiltInsAreReady();
  1409. #endif
  1410. RecyclableObject* function = GetIteratorFunction(aRight, scriptContext);
  1411. JavascriptMethod method = function->GetEntryPoint();
  1412. if (((JavascriptArray::IsNonES5Array(aRight) &&
  1413. (
  1414. JavascriptLibrary::IsDefaultArrayValuesFunction(function, scriptContext)
  1415. // Verify that the head segment of the array covers all elements with no gaps.
  1416. // Accessing an element on the prototype could have side-effects that would invalidate the optimization.
  1417. && UnsafeVarTo<JavascriptArray>(aRight)->GetHead()->next == nullptr
  1418. && UnsafeVarTo<JavascriptArray>(aRight)->GetHead()->left == 0
  1419. && UnsafeVarTo<JavascriptArray>(aRight)->GetHead()->length == VarTo<JavascriptArray>(aRight)->GetLength()
  1420. && UnsafeVarTo<JavascriptArray>(aRight)->HasNoMissingValues()
  1421. && !UnsafeVarTo<JavascriptArray>(aRight)->IsCrossSiteObject()
  1422. )) ||
  1423. (VarIs<TypedArrayBase>(aRight) && method == TypedArrayBase::EntryInfo::Values.GetOriginalEntryPoint()))
  1424. // We can't optimize away the iterator if the array iterator prototype is user defined.
  1425. && !JavascriptLibrary::ArrayIteratorPrototypeHasUserDefinedNext(scriptContext))
  1426. {
  1427. return RecyclerNew(scriptContext->GetRecycler(), SpreadArgument, aRight, true /*useDirectCall*/, scriptContext->GetLibrary()->GetSpreadArgumentType());
  1428. }
  1429. ThreadContext *threadContext = scriptContext->GetThreadContext();
  1430. Var iteratorVar =
  1431. threadContext->ExecuteImplicitCall(function, ImplicitCall_Accessor, [=]() -> Var
  1432. {
  1433. return CALL_FUNCTION(threadContext, function, CallInfo(Js::CallFlags_Value, 1), aRight);
  1434. });
  1435. if (!JavascriptOperators::IsObject(iteratorVar))
  1436. {
  1437. if (!threadContext->RecordImplicitException())
  1438. {
  1439. return scriptContext->GetLibrary()->GetUndefined();
  1440. }
  1441. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  1442. }
  1443. return RecyclerNew(scriptContext->GetRecycler(), SpreadArgument, iteratorVar, false /*useDirectCall*/, scriptContext->GetLibrary()->GetSpreadArgumentType());
  1444. JIT_HELPER_END(Op_ToSpreadedFunctionArgument);
  1445. }
  1446. BOOL JavascriptOperators::IsPropertyUnscopable(Var instanceVar, JavascriptString *propertyString)
  1447. {
  1448. // This never gets called.
  1449. Throw::InternalError();
  1450. }
  1451. BOOL JavascriptOperators::IsPropertyUnscopable(Var instanceVar, PropertyId propertyId)
  1452. {
  1453. RecyclableObject* instance = VarTo<RecyclableObject>(instanceVar);
  1454. ScriptContext * scriptContext = instance->GetScriptContext();
  1455. Var unscopables = JavascriptOperators::GetProperty(instance, PropertyIds::_symbolUnscopables, scriptContext);
  1456. if (JavascriptOperators::IsObject(unscopables))
  1457. {
  1458. DynamicObject *unscopablesList = VarTo<DynamicObject>(unscopables);
  1459. Var value = nullptr;
  1460. //8.1.1.2.1.9.c If blocked is not undefined
  1461. if (JavascriptOperators::GetProperty(unscopablesList, propertyId, &value, scriptContext))
  1462. {
  1463. return JavascriptConversion::ToBoolean(value, scriptContext);
  1464. }
  1465. }
  1466. return false;
  1467. }
  1468. BOOL JavascriptOperators::HasProperty(RecyclableObject* instance, PropertyId propertyId)
  1469. {
  1470. while (!JavascriptOperators::IsNull(instance))
  1471. {
  1472. PropertyQueryFlags result = instance->HasPropertyQuery(propertyId, nullptr /*info*/);
  1473. if (result != PropertyQueryFlags::Property_NotFound)
  1474. {
  1475. return JavascriptConversion::PropertyQueryFlagsToBoolean(result); // return false if instance is typed array and HasPropertyQuery() returns PropertyQueryFlags::Property_Found_Undefined
  1476. }
  1477. instance = JavascriptOperators::GetPrototypeNoTrap(instance);
  1478. }
  1479. return false;
  1480. }
  1481. BOOL JavascriptOperators::HasPropertyUnscopables(RecyclableObject* instance, PropertyId propertyId)
  1482. {
  1483. return JavascriptOperators::HasProperty(instance, propertyId)
  1484. && !IsPropertyUnscopable(instance, propertyId);
  1485. }
  1486. BOOL JavascriptOperators::HasRootProperty(RecyclableObject* instance, PropertyId propertyId)
  1487. {
  1488. Assert(VarIs<RootObjectBase>(instance));
  1489. RootObjectBase* rootObject = static_cast<RootObjectBase*>(instance);
  1490. if (rootObject->HasRootProperty(propertyId))
  1491. {
  1492. return true;
  1493. }
  1494. instance = instance->GetPrototype();
  1495. return HasProperty(instance, propertyId);
  1496. }
  1497. BOOL JavascriptOperators::HasProxyOrPrototypeInlineCacheProperty(RecyclableObject* instance, PropertyId propertyId)
  1498. {
  1499. TypeId typeId;
  1500. typeId = JavascriptOperators::GetTypeId(instance);
  1501. if (typeId == Js::TypeIds_Proxy)
  1502. {
  1503. // let's be more aggressive to disable inline prototype cache when proxy is presented in the prototypechain
  1504. return true;
  1505. }
  1506. do
  1507. {
  1508. instance = instance->GetPrototype();
  1509. typeId = JavascriptOperators::GetTypeId(instance);
  1510. if (typeId == Js::TypeIds_Proxy)
  1511. {
  1512. // let's be more aggressive to disable inline prototype cache when proxy is presented in the prototypechain
  1513. return true;
  1514. }
  1515. if (typeId == TypeIds_Null)
  1516. {
  1517. break;
  1518. }
  1519. /* We can rule out object with deferred type handler, because they would have expanded if they are in the cache */
  1520. if (!instance->HasDeferredTypeHandler() && instance->HasProperty(propertyId)) { return true; }
  1521. } while (typeId != TypeIds_Null);
  1522. return false;
  1523. }
  1524. BOOL JavascriptOperators::OP_HasProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  1525. {
  1526. JIT_HELPER_REENTRANT_HEADER(Op_HasProperty);
  1527. RecyclableObject* object = TaggedNumber::Is(instance) ?
  1528. scriptContext->GetLibrary()->GetNumberPrototype() :
  1529. VarTo<RecyclableObject>(instance);
  1530. BOOL result = HasProperty(object, propertyId);
  1531. return result;
  1532. JIT_HELPER_END(Op_HasProperty);
  1533. }
  1534. BOOL JavascriptOperators::OP_HasOwnProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext, _In_opt_ PropertyString * propString)
  1535. {
  1536. RecyclableObject* object = TaggedNumber::Is(instance) ?
  1537. scriptContext->GetLibrary()->GetNumberPrototype() :
  1538. VarTo<RecyclableObject>(instance);
  1539. BOOL result = HasOwnProperty(object, propertyId, scriptContext, propString);
  1540. return result;
  1541. }
  1542. // CONSIDER: Have logic similar to HasOwnPropertyNoHostObjectForHeapEnum
  1543. BOOL JavascriptOperators::HasOwnPropertyNoHostObject(Var instance, PropertyId propertyId)
  1544. {
  1545. AssertMsg(!TaggedNumber::Is(instance), "HasOwnPropertyNoHostObject int passed");
  1546. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  1547. return object && object->HasOwnPropertyNoHostObject(propertyId);
  1548. }
  1549. // CONSIDER: Remove HasOwnPropertyNoHostObjectForHeapEnum and use GetOwnPropertyNoHostObjectForHeapEnum in its place by changing it
  1550. // 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.
  1551. BOOL JavascriptOperators::HasOwnPropertyNoHostObjectForHeapEnum(Var instance, PropertyId propertyId, ScriptContext* requestContext, Var& getter, Var& setter)
  1552. {
  1553. AssertMsg(!TaggedNumber::Is(instance), "HasOwnPropertyNoHostObjectForHeapEnum int passed");
  1554. RecyclableObject * object = VarTo<RecyclableObject>(instance);
  1555. if (StaticType::Is(object->GetTypeId()))
  1556. {
  1557. return FALSE;
  1558. }
  1559. getter = setter = NULL;
  1560. DynamicObject* dynamicObject = VarTo<DynamicObject>(instance);
  1561. Assert(dynamicObject->GetScriptContext()->IsHeapEnumInProgress());
  1562. if (dynamicObject->UseDynamicObjectForNoHostObjectAccess())
  1563. {
  1564. if (!dynamicObject->DynamicObject::GetAccessors(propertyId, &getter, &setter, requestContext))
  1565. {
  1566. Var value = nullptr;
  1567. if (!JavascriptConversion::PropertyQueryFlagsToBoolean(dynamicObject->DynamicObject::GetPropertyQuery(instance, propertyId, &value, NULL, requestContext)) ||
  1568. (requestContext->IsUndeclBlockVar(value) && (VarIs<ActivationObject>(instance) || VarIs<RootObjectBase>(instance))))
  1569. {
  1570. return FALSE;
  1571. }
  1572. }
  1573. }
  1574. else
  1575. {
  1576. if (!object->GetAccessors(propertyId, &getter, &setter, requestContext))
  1577. {
  1578. Var value = nullptr;
  1579. if (!object->GetProperty(instance, propertyId, &value, NULL, requestContext) ||
  1580. (requestContext->IsUndeclBlockVar(value) && (VarIs<ActivationObject>(instance) || VarIs<RootObjectBase>(instance))))
  1581. {
  1582. return FALSE;
  1583. }
  1584. }
  1585. }
  1586. return TRUE;
  1587. }
  1588. Var JavascriptOperators::GetOwnPropertyNoHostObjectForHeapEnum(Var instance, PropertyId propertyId, ScriptContext* requestContext, Var& getter, Var& setter)
  1589. {
  1590. AssertMsg(!TaggedNumber::Is(instance), "GetDataPropertyNoHostObject int passed");
  1591. Assert(HasOwnPropertyNoHostObjectForHeapEnum(instance, propertyId, requestContext, getter, setter) || getter || setter);
  1592. DynamicObject* dynamicObject = VarTo<DynamicObject>(instance);
  1593. getter = setter = NULL;
  1594. if (NULL == dynamicObject)
  1595. {
  1596. return requestContext->GetLibrary()->GetUndefined();
  1597. }
  1598. Var returnVar = requestContext->GetLibrary()->GetUndefined();
  1599. BOOL result = FALSE;
  1600. if (dynamicObject->UseDynamicObjectForNoHostObjectAccess())
  1601. {
  1602. if (! dynamicObject->DynamicObject::GetAccessors(propertyId, &getter, &setter, requestContext))
  1603. {
  1604. result = JavascriptConversion::PropertyQueryFlagsToBoolean((dynamicObject->DynamicObject::GetPropertyQuery(instance, propertyId, &returnVar, NULL, requestContext)));
  1605. }
  1606. }
  1607. else
  1608. {
  1609. if (! dynamicObject->GetAccessors(propertyId, &getter, &setter, requestContext))
  1610. {
  1611. result = dynamicObject->GetProperty(instance, propertyId, &returnVar, NULL, requestContext);
  1612. }
  1613. }
  1614. if (result)
  1615. {
  1616. return returnVar;
  1617. }
  1618. return requestContext->GetLibrary()->GetUndefined();
  1619. }
  1620. BOOL JavascriptOperators::OP_HasOwnPropScoped(Var scope, PropertyId propertyId, Var defaultInstance, ScriptContext* scriptContext)
  1621. {
  1622. AssertMsg(scope == scriptContext->GetLibrary()->GetNull() || JavascriptArray::IsNonES5Array(scope),
  1623. "Invalid scope chain pointer passed - should be null or an array");
  1624. JavascriptArray* arrScope = JavascriptArray::TryVarToNonES5Array(scope);
  1625. if (arrScope)
  1626. {
  1627. Var instance = arrScope->DirectGetItem(0);
  1628. return JavascriptOperators::OP_HasOwnProperty(instance, propertyId, scriptContext);
  1629. }
  1630. return JavascriptOperators::OP_HasOwnProperty(defaultInstance, propertyId, scriptContext);
  1631. }
  1632. BOOL JavascriptOperators::GetPropertyUnscopable(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1633. {
  1634. return GetProperty_Internal<true>(instance, propertyObject, false, propertyId, value, requestContext, info);
  1635. }
  1636. BOOL JavascriptOperators::GetProperty(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1637. {
  1638. return GetProperty_Internal<false>(instance, propertyObject, false, propertyId, value, requestContext, info);
  1639. }
  1640. BOOL JavascriptOperators::GetRootProperty(Var instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1641. {
  1642. return GetProperty_Internal<false>(instance, VarTo<RecyclableObject>(instance), true, propertyId, value, requestContext, info);
  1643. }
  1644. BOOL JavascriptOperators::GetProperty_InternalSimple(Var instance, RecyclableObject* object, PropertyId propertyId, _Outptr_result_maybenull_ Var* value, ScriptContext* requestContext)
  1645. {
  1646. BOOL foundProperty = FALSE;
  1647. Assert(value != nullptr);
  1648. while (!JavascriptOperators::IsNull(object))
  1649. {
  1650. PropertyQueryFlags result = object->GetPropertyQuery(instance, propertyId, value, nullptr, requestContext);
  1651. if (result != PropertyQueryFlags::Property_NotFound)
  1652. {
  1653. foundProperty = JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  1654. break;
  1655. }
  1656. if (object->SkipsPrototype())
  1657. {
  1658. break;
  1659. }
  1660. object = JavascriptOperators::GetPrototypeNoTrap(object);
  1661. }
  1662. if (!foundProperty)
  1663. {
  1664. *value = requestContext->GetMissingPropertyResult();
  1665. }
  1666. return foundProperty;
  1667. }
  1668. template <bool unscopables>
  1669. BOOL JavascriptOperators::GetProperty_Internal(Var instance, RecyclableObject* propertyObject, const bool isRoot, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1670. {
  1671. if (TaggedNumber::Is(instance))
  1672. {
  1673. PropertyValueInfo::ClearCacheInfo(info);
  1674. }
  1675. RecyclableObject* object = propertyObject;
  1676. BOOL foundProperty = FALSE;
  1677. if (isRoot)
  1678. {
  1679. Assert(VarIs<RootObjectBase>(object));
  1680. RootObjectBase* rootObject = static_cast<RootObjectBase*>(object);
  1681. foundProperty = rootObject->GetRootProperty(instance, propertyId, value, info, requestContext);
  1682. }
  1683. while (!foundProperty && !JavascriptOperators::IsNull(object))
  1684. {
  1685. if (unscopables && IsPropertyUnscopable(object, propertyId))
  1686. {
  1687. break;
  1688. }
  1689. else
  1690. {
  1691. PropertyQueryFlags result = object->GetPropertyQuery(instance, propertyId, value, info, requestContext);
  1692. if (result != PropertyQueryFlags::Property_NotFound)
  1693. {
  1694. foundProperty = JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  1695. break;
  1696. }
  1697. }
  1698. if (object->SkipsPrototype())
  1699. {
  1700. break;
  1701. }
  1702. object = JavascriptOperators::GetPrototypeNoTrap(object);
  1703. }
  1704. if (foundProperty)
  1705. {
  1706. #if ENABLE_FIXED_FIELDS && DBG
  1707. if (DynamicObject::IsBaseDynamicObject(object))
  1708. {
  1709. DynamicObject* dynamicObject = (DynamicObject*)object;
  1710. DynamicTypeHandler* dynamicTypeHandler = dynamicObject->GetDynamicType()->GetTypeHandler();
  1711. Var property;
  1712. if (dynamicTypeHandler->CheckFixedProperty(requestContext->GetPropertyName(propertyId), &property, requestContext))
  1713. {
  1714. bool skipAssert = false;
  1715. if (value != nullptr && Js::VarIs<Js::RecyclableObject>(property))
  1716. {
  1717. Js::RecyclableObject* pObject = Js::VarTo<Js::RecyclableObject>(property);
  1718. Js::RecyclableObject* pValue = Js::VarTo<Js::RecyclableObject>(*value);
  1719. if (pValue->GetScriptContext() != pObject->GetScriptContext())
  1720. {
  1721. // value was marshaled. skip check
  1722. skipAssert = true;
  1723. }
  1724. }
  1725. Assert(skipAssert || value == nullptr || *value == property);
  1726. }
  1727. }
  1728. #endif
  1729. // Don't cache the information if the value is undecl block var
  1730. // REVIEW: We might want to only check this if we need to (For LdRootFld or ScopedLdFld)
  1731. // Also we might want to throw here instead of checking it again in the caller
  1732. if (value && !requestContext->IsUndeclBlockVar(*value) && !VarIs<UnscopablesWrapperObject>(object))
  1733. {
  1734. CacheOperators::CachePropertyRead(propertyObject, object, isRoot, propertyId, false, info, requestContext);
  1735. }
  1736. #ifdef TELEMETRY_JSO
  1737. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  1738. {
  1739. requestContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(instance, propertyId, value, /*successful: */true);
  1740. }
  1741. #endif
  1742. return TRUE;
  1743. }
  1744. else
  1745. {
  1746. #ifdef MISSING_PROPERTY_STATS
  1747. if (PHASE_STATS1(MissingPropertyCachePhase))
  1748. {
  1749. requestContext->RecordMissingPropertyMiss();
  1750. }
  1751. #endif
  1752. if (PHASE_TRACE1(MissingPropertyCachePhase))
  1753. {
  1754. Output::Print(_u("MissingPropertyCaching: Missing property %d on slow path.\n"), propertyId);
  1755. }
  1756. TryCacheMissingProperty(instance, propertyObject, isRoot, propertyId, requestContext, info);
  1757. #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.
  1758. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  1759. {
  1760. if (info && info->AllowResizingPolymorphicInlineCache()) // If in interpreted mode, not JIT.
  1761. {
  1762. requestContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(instance, propertyId, nullptr);
  1763. }
  1764. }
  1765. #endif
  1766. *value = requestContext->GetMissingPropertyResult();
  1767. return FALSE;
  1768. }
  1769. }
  1770. // If the given instance is a type where we can cache missing properties, then cache that the given property ID is missing.
  1771. // cacheInstance is used as startingObject in CachePropertyRead, and might be instance's proto if we are fetching a super property (see #3064).
  1772. void JavascriptOperators::TryCacheMissingProperty(Var instance, Var cacheInstance, bool isRoot, PropertyId propertyId, ScriptContext* requestContext, _Inout_ PropertyValueInfo * info)
  1773. {
  1774. // Here, any well-behaved subclasses of DynamicObject can opt in to getting included in the missing property cache.
  1775. // For now, we only include basic objects and arrays.
  1776. if (PHASE_OFF1(MissingPropertyCachePhase) || isRoot || !(DynamicObject::IsBaseDynamicObject(instance) || DynamicObject::IsAnyArray(instance)))
  1777. {
  1778. return;
  1779. }
  1780. // CustomExternalObject in particular is problematic because in some cases it can report missing when implicit callsare disabled.
  1781. // See CustomExternalObject::GetPropertyQuery for an example.
  1782. if (UnsafeVarTo<DynamicObject>(instance)->GetType()->IsJsrtExternal() && requestContext->GetThreadContext()->IsDisableImplicitCall())
  1783. {
  1784. return;
  1785. }
  1786. DynamicTypeHandler* handler = UnsafeVarTo<DynamicObject>(instance)->GetDynamicType()->GetTypeHandler();
  1787. // Only cache missing property lookups for non-root field loads on objects that have PathTypeHandlers, because only these types have the right behavior
  1788. // when the missing property is later added. DictionaryTypeHandler's introduce the possibility that a stale TypePropertyCache entry with isMissing==true can
  1789. // 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
  1790. // override special symbols make it unnecessary to introduce an invalidation scheme to deal with DictionaryTypeHandler's.
  1791. if (!handler->IsPathTypeHandler())
  1792. {
  1793. return;
  1794. }
  1795. #ifdef MISSING_PROPERTY_STATS
  1796. if (PHASE_STATS1(MissingPropertyCachePhase))
  1797. {
  1798. requestContext->RecordMissingPropertyCacheAttempt();
  1799. }
  1800. #endif
  1801. if (PHASE_TRACE1(MissingPropertyCachePhase))
  1802. {
  1803. Output::Print(_u("MissingPropertyCache: Caching missing property for property %d.\n"), propertyId);
  1804. }
  1805. PropertyValueInfo::Set(info, requestContext->GetLibrary()->GetMissingPropertyHolder(), 0);
  1806. CacheOperators::CachePropertyRead(cacheInstance, requestContext->GetLibrary()->GetMissingPropertyHolder(), isRoot, propertyId, true /*isMissing*/, info, requestContext);
  1807. }
  1808. template<bool OutputExistence, typename PropertyKeyType> PropertyQueryFlags QueryGetOrHasProperty(
  1809. Var originalInstance, RecyclableObject* object, PropertyKeyType propertyKey, Var* value, PropertyValueInfo* info, ScriptContext* requestContext);
  1810. template<> PropertyQueryFlags QueryGetOrHasProperty<false /*OutputExistence*/, PropertyId /*PropertyKeyType*/>(
  1811. Var originalInstance, RecyclableObject* object, PropertyId propertyKey, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1812. {
  1813. return object->GetPropertyQuery(originalInstance, propertyKey, value, info, requestContext);
  1814. }
  1815. template<> PropertyQueryFlags QueryGetOrHasProperty<false /*OutputExistence*/, JavascriptString* /*PropertyKeyType*/>(
  1816. Var originalInstance, RecyclableObject* object, JavascriptString* propertyKey, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1817. {
  1818. return object->GetPropertyQuery(originalInstance, propertyKey, value, info, requestContext);
  1819. }
  1820. template<> PropertyQueryFlags QueryGetOrHasProperty<true /*OutputExistence*/, PropertyId /*PropertyKeyType*/>(
  1821. Var originalInstance, RecyclableObject* object, PropertyId propertyKey, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1822. {
  1823. PropertyQueryFlags result = object->HasPropertyQuery(propertyKey, info);
  1824. *value = JavascriptBoolean::ToVar(JavascriptConversion::PropertyQueryFlagsToBoolean(result), requestContext);
  1825. return result;
  1826. }
  1827. template<bool OutputExistence, typename PropertyKeyType>
  1828. BOOL JavascriptOperators::GetPropertyWPCache(Var instance, RecyclableObject* propertyObject, PropertyKeyType propertyKey, Var* value, ScriptContext* requestContext, _Inout_ PropertyValueInfo * info)
  1829. {
  1830. Assert(value);
  1831. RecyclableObject* object = propertyObject;
  1832. while (!JavascriptOperators::IsNull(object))
  1833. {
  1834. PropertyQueryFlags result = QueryGetOrHasProperty<OutputExistence>(instance, object, propertyKey, value, info, requestContext);
  1835. if (result != PropertyQueryFlags::Property_NotFound)
  1836. {
  1837. if (!VarIs<UnscopablesWrapperObject>(object) && info->GetPropertyRecordUsageCache())
  1838. {
  1839. PropertyId propertyId = info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId();
  1840. CacheOperators::CachePropertyRead(instance, object, false, propertyId, false, info, requestContext);
  1841. }
  1842. return JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  1843. }
  1844. // SkipsPrototype refers only to the Get operation, not Has. Some objects like CustomExternalObject respond
  1845. // to HasPropertyQuery with info only about the object itself and GetPropertyQuery with info about its prototype chain.
  1846. // For consistency with the behavior of JavascriptOperators::HasProperty, don't skip prototypes when outputting existence.
  1847. if (!OutputExistence && object->SkipsPrototype())
  1848. {
  1849. break;
  1850. }
  1851. object = JavascriptOperators::GetPrototypeNoTrap(object);
  1852. }
  1853. if (info->GetPropertyRecordUsageCache())
  1854. {
  1855. TryCacheMissingProperty(instance, instance, false /*isRoot*/, info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId(), requestContext, info);
  1856. }
  1857. *value = OutputExistence
  1858. ? requestContext->GetLibrary()->GetFalse()
  1859. : requestContext->GetMissingPropertyResult();
  1860. return FALSE;
  1861. }
  1862. bool JavascriptOperators::GetPropertyObjectForElementAccess(
  1863. _In_ Var instance,
  1864. _In_ Var index,
  1865. _In_ ScriptContext* scriptContext,
  1866. _Out_ RecyclableObject** propertyObject,
  1867. _In_ rtErrors error)
  1868. {
  1869. BOOL isNullOrUndefined = !GetPropertyObject(instance, scriptContext, propertyObject);
  1870. Assert(*propertyObject == instance || TaggedNumber::Is(instance));
  1871. if (isNullOrUndefined)
  1872. {
  1873. if (!scriptContext->GetThreadContext()->RecordImplicitException())
  1874. {
  1875. return false;
  1876. }
  1877. JavascriptError::ThrowTypeError(scriptContext, error, GetPropertyDisplayNameForError(index, scriptContext));
  1878. }
  1879. return true;
  1880. }
  1881. bool JavascriptOperators::GetPropertyObjectForSetElementI(
  1882. _In_ Var instance,
  1883. _In_ Var index,
  1884. _In_ ScriptContext* scriptContext,
  1885. _Out_ RecyclableObject** propertyObject)
  1886. {
  1887. return GetPropertyObjectForElementAccess(instance, index, scriptContext, propertyObject, JSERR_Property_CannotSet_NullOrUndefined);
  1888. }
  1889. bool JavascriptOperators::GetPropertyObjectForGetElementI(
  1890. _In_ Var instance,
  1891. _In_ Var index,
  1892. _In_ ScriptContext* scriptContext,
  1893. _Out_ RecyclableObject** propertyObject)
  1894. {
  1895. return GetPropertyObjectForElementAccess(instance, index, scriptContext, propertyObject, JSERR_Property_CannotGet_NullOrUndefined);
  1896. }
  1897. BOOL JavascriptOperators::GetPropertyObject(Var instance, ScriptContext * scriptContext, RecyclableObject** propertyObject)
  1898. {
  1899. Assert(propertyObject);
  1900. if (TaggedNumber::Is(instance))
  1901. {
  1902. *propertyObject = scriptContext->GetLibrary()->GetNumberPrototype();
  1903. return TRUE;
  1904. }
  1905. RecyclableObject* object = UnsafeVarTo<RecyclableObject>(instance);
  1906. *propertyObject = object;
  1907. if (JavascriptOperators::IsUndefinedOrNull(object))
  1908. {
  1909. return FALSE;
  1910. }
  1911. return TRUE;
  1912. }
  1913. #if DBG
  1914. BOOL JavascriptOperators::IsPropertyObject(RecyclableObject * instance)
  1915. {
  1916. TypeId typeId = JavascriptOperators::GetTypeId(instance);
  1917. return (typeId != TypeIds_Integer && typeId != TypeIds_Null && typeId != TypeIds_Undefined);
  1918. }
  1919. #endif
  1920. Var JavascriptOperators::OP_GetProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  1921. {
  1922. JIT_HELPER_REENTRANT_HEADER(Op_GetProperty);
  1923. RecyclableObject* object = nullptr;
  1924. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  1925. {
  1926. if (scriptContext->GetThreadContext()->RecordImplicitException())
  1927. {
  1928. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined, scriptContext->GetPropertyName(propertyId)->GetBuffer());
  1929. }
  1930. else
  1931. {
  1932. return scriptContext->GetLibrary()->GetUndefined();
  1933. }
  1934. }
  1935. Var result = JavascriptOperators::GetPropertyNoCache(instance, object, propertyId, scriptContext);
  1936. AssertMsg(result != nullptr, "result null in OP_GetProperty");
  1937. return result;
  1938. JIT_HELPER_END(Op_GetProperty);
  1939. }
  1940. Var JavascriptOperators::OP_GetRootProperty(Var instance, PropertyId propertyId, PropertyValueInfo * info, ScriptContext* scriptContext)
  1941. {
  1942. AssertMsg(VarIs<RootObjectBase>(instance), "Root must be an object!");
  1943. Var value = nullptr;
  1944. if (JavascriptOperators::GetRootProperty(VarTo<RecyclableObject>(instance), propertyId, &value, scriptContext, info))
  1945. {
  1946. if (scriptContext->IsUndeclBlockVar(value) && scriptContext->GetThreadContext()->RecordImplicitException())
  1947. {
  1948. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  1949. }
  1950. return value;
  1951. }
  1952. const char16* propertyName = scriptContext->GetPropertyName(propertyId)->GetBuffer();
  1953. JavascriptFunction * caller = nullptr;
  1954. if (JavascriptStackWalker::GetCaller(&caller, scriptContext))
  1955. {
  1956. FunctionBody * callerBody = caller->GetFunctionBody();
  1957. if (callerBody && callerBody->GetUtf8SourceInfo()->GetIsXDomain())
  1958. {
  1959. propertyName = nullptr;
  1960. }
  1961. }
  1962. // Don't error if we disabled implicit calls
  1963. if (scriptContext->GetThreadContext()->RecordImplicitException())
  1964. {
  1965. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UndefVariable, propertyName);
  1966. }
  1967. return scriptContext->GetMissingPropertyResult();
  1968. }
  1969. Var JavascriptOperators::OP_GetThisScoped(FrameDisplay *pScope, Var defaultInstance, ScriptContext* scriptContext)
  1970. {
  1971. // NOTE: If changes are made to this logic be sure to update the debuggers as well
  1972. int length = pScope->GetLength();
  1973. for (int i = 0; i < length; i += 1)
  1974. {
  1975. Var value = nullptr;
  1976. RecyclableObject *obj = VarTo<RecyclableObject>(pScope->GetItem(i));
  1977. if (JavascriptOperators::GetProperty(obj, Js::PropertyIds::_this, &value, scriptContext))
  1978. {
  1979. return value;
  1980. }
  1981. }
  1982. return defaultInstance;
  1983. }
  1984. Var JavascriptOperators::OP_UnwrapWithObj(Var aValue)
  1985. {
  1986. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_UnwrapWithObj);
  1987. return VarTo<UnscopablesWrapperObject>(aValue)->GetWrappedObject();
  1988. JIT_HELPER_END(Op_UnwrapWithObj);
  1989. }
  1990. Var JavascriptOperators::OP_GetInstanceScoped(FrameDisplay *pScope, PropertyId propertyId, Var rootObject, Var* thisVar, ScriptContext* scriptContext)
  1991. {
  1992. JIT_HELPER_REENTRANT_HEADER(Op_GetInstanceScoped);
  1993. // Similar to GetPropertyScoped, but instead of returning the property value, we return the instance that
  1994. // owns it, or the global object if no instance is found.
  1995. int i;
  1996. int length = pScope->GetLength();
  1997. for (i = 0; i < length; i++)
  1998. {
  1999. RecyclableObject *obj = (RecyclableObject*)pScope->GetItem(i);
  2000. if (JavascriptOperators::HasProperty(obj, propertyId))
  2001. {
  2002. // HasProperty will call UnscopablesWrapperObject's HasProperty which will do the filtering
  2003. // All we have to do here is unwrap the object hence the api call
  2004. return obj->GetThisAndUnwrappedInstance(thisVar);
  2005. }
  2006. }
  2007. *thisVar = scriptContext->GetLibrary()->GetUndefined();
  2008. if (rootObject != scriptContext->GetGlobalObject())
  2009. {
  2010. if (JavascriptOperators::OP_HasProperty(rootObject, propertyId, scriptContext))
  2011. {
  2012. return rootObject;
  2013. }
  2014. }
  2015. return scriptContext->GetGlobalObject();
  2016. JIT_HELPER_END(Op_GetInstanceScoped);
  2017. }
  2018. Var JavascriptOperators::GetPropertyReference(RecyclableObject *instance, PropertyId propertyId, ScriptContext* requestContext)
  2019. {
  2020. Var value = nullptr;
  2021. PropertyValueInfo info;
  2022. if (JavascriptOperators::GetPropertyReference(instance, propertyId, &value, requestContext, &info))
  2023. {
  2024. Assert(value != nullptr);
  2025. return value;
  2026. }
  2027. return requestContext->GetMissingPropertyResult();
  2028. }
  2029. BOOL JavascriptOperators::GetPropertyReference(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  2030. {
  2031. return GetPropertyReference_Internal(instance, propertyObject, false, propertyId, value, requestContext, info);
  2032. }
  2033. BOOL JavascriptOperators::GetRootPropertyReference(RecyclableObject* instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  2034. {
  2035. return GetPropertyReference_Internal(instance, instance, true, propertyId, value, requestContext, info);
  2036. }
  2037. BOOL JavascriptOperators::PropertyReferenceWalkUnscopable(Var instance, RecyclableObject** propertyObject, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  2038. {
  2039. return PropertyReferenceWalk_Impl<true>(instance, propertyObject, propertyId, value, info, requestContext);
  2040. }
  2041. BOOL JavascriptOperators::PropertyReferenceWalk(Var instance, RecyclableObject** propertyObject, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  2042. {
  2043. return PropertyReferenceWalk_Impl<false>(instance, propertyObject, propertyId, value, info, requestContext);
  2044. }
  2045. template <bool unscopables>
  2046. BOOL JavascriptOperators::PropertyReferenceWalk_Impl(Var instance, RecyclableObject** propertyObject, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  2047. {
  2048. BOOL foundProperty = false;
  2049. RecyclableObject* object = *propertyObject;
  2050. while (!foundProperty && !JavascriptOperators::IsNull(object))
  2051. {
  2052. if (unscopables && JavascriptOperators::IsPropertyUnscopable(object, propertyId))
  2053. {
  2054. break;
  2055. }
  2056. else
  2057. {
  2058. PropertyQueryFlags result = object->GetPropertyReferenceQuery(instance, propertyId, value, info, requestContext);
  2059. if (result != PropertyQueryFlags::Property_NotFound)
  2060. {
  2061. foundProperty = JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  2062. break;
  2063. }
  2064. }
  2065. if (object->SkipsPrototype())
  2066. {
  2067. break; // will return false
  2068. }
  2069. object = JavascriptOperators::GetPrototypeNoTrap(object);
  2070. }
  2071. *propertyObject = object;
  2072. return foundProperty;
  2073. }
  2074. BOOL JavascriptOperators::GetPropertyReference_Internal(Var instance, RecyclableObject* propertyObject, const bool isRoot, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  2075. {
  2076. if (TaggedNumber::Is(instance))
  2077. {
  2078. PropertyValueInfo::ClearCacheInfo(info);
  2079. }
  2080. BOOL foundProperty = FALSE;
  2081. RecyclableObject* object = propertyObject;
  2082. if (isRoot)
  2083. {
  2084. foundProperty = VarTo<RootObjectBase>(object)->GetRootPropertyReference(instance, propertyId, value, info, requestContext);
  2085. }
  2086. if (!foundProperty)
  2087. {
  2088. foundProperty = PropertyReferenceWalk(instance, &object, propertyId, value, info, requestContext);
  2089. }
  2090. if (!foundProperty)
  2091. {
  2092. #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.
  2093. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  2094. {
  2095. if (info && info->AllowResizingPolymorphicInlineCache()) // If in interpreted mode, not JIT.
  2096. {
  2097. requestContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(instance, propertyId, nullptr);
  2098. }
  2099. }
  2100. #endif
  2101. *value = requestContext->GetMissingPropertyResult();
  2102. return foundProperty;
  2103. }
  2104. if (requestContext->IsUndeclBlockVar(*value))
  2105. {
  2106. JavascriptError::ThrowReferenceError(requestContext, JSERR_UseBeforeDeclaration);
  2107. }
  2108. #if ENABLE_FIXED_FIELDS && DBG
  2109. if (DynamicObject::IsBaseDynamicObject(object))
  2110. {
  2111. DynamicObject* dynamicObject = (DynamicObject*)object;
  2112. DynamicTypeHandler* dynamicTypeHandler = dynamicObject->GetDynamicType()->GetTypeHandler();
  2113. Var property = nullptr;
  2114. if (dynamicTypeHandler->CheckFixedProperty(requestContext->GetPropertyName(propertyId), &property, requestContext))
  2115. {
  2116. Assert(value == nullptr || *value == property);
  2117. }
  2118. }
  2119. #endif
  2120. CacheOperators::CachePropertyRead(instance, object, isRoot, propertyId, false, info, requestContext);
  2121. return TRUE;
  2122. }
  2123. template <typename PropertyKeyType, bool unscopable>
  2124. DescriptorFlags JavascriptOperators::GetterSetter_Impl(RecyclableObject* instance, PropertyKeyType propertyKey, Var* setterValue, PropertyValueInfo* info, ScriptContext* scriptContext)
  2125. {
  2126. DescriptorFlags flags = None;
  2127. RecyclableObject* object = instance;
  2128. while (flags == None && !JavascriptOperators::IsNull(object))
  2129. {
  2130. if (unscopable && IsPropertyUnscopable(object, propertyKey))
  2131. {
  2132. break;
  2133. }
  2134. else
  2135. {
  2136. flags = object->GetSetter(propertyKey, setterValue, info, scriptContext);
  2137. if (flags != None)
  2138. {
  2139. break;
  2140. }
  2141. }
  2142. // CONSIDER: we should add SkipsPrototype support. DOM has no ES 5 concepts built in that aren't
  2143. // already part of our prototype objects which are chakra objects.
  2144. object = object->GetPrototype();
  2145. }
  2146. return flags;
  2147. }
  2148. DescriptorFlags JavascriptOperators::GetterSetterUnscopable(RecyclableObject* instance, PropertyId propertyId, Var* setterValue, PropertyValueInfo* info, ScriptContext* scriptContext)
  2149. {
  2150. return GetterSetter_Impl<PropertyId, true>(instance, propertyId, setterValue, info, scriptContext);
  2151. }
  2152. DescriptorFlags JavascriptOperators::GetterSetter(RecyclableObject* instance, PropertyId propertyId, Var* setterValue, PropertyValueInfo* info, ScriptContext* scriptContext)
  2153. {
  2154. return GetterSetter_Impl<PropertyId, false>(instance, propertyId, setterValue, info, scriptContext);
  2155. }
  2156. DescriptorFlags JavascriptOperators::GetterSetter(RecyclableObject* instance, JavascriptString * propertyName, Var* setterValue, PropertyValueInfo* info, ScriptContext* scriptContext)
  2157. {
  2158. return GetterSetter_Impl<JavascriptString*, false>(instance, propertyName, setterValue, info, scriptContext);
  2159. }
  2160. void JavascriptOperators::OP_InvalidateProtoCaches(PropertyId propertyId, ScriptContext *scriptContext)
  2161. {
  2162. JIT_HELPER_NOT_REENTRANT_HEADER(InvalidateProtoCaches, reentrancylock, scriptContext->GetThreadContext());
  2163. scriptContext->InvalidateProtoCaches(propertyId);
  2164. JIT_HELPER_END(InvalidateProtoCaches);
  2165. }
  2166. // Checks to see if any object in the prototype chain has a property descriptor for the given index
  2167. // that specifies either an accessor or a non-writable attribute.
  2168. // If TRUE, check flags for details.
  2169. BOOL JavascriptOperators::CheckPrototypesForAccessorOrNonWritableItem(RecyclableObject* instance, uint32 index,
  2170. Var* setterValue, DescriptorFlags *flags, ScriptContext* scriptContext, BOOL skipPrototypeCheck /* = FALSE */)
  2171. {
  2172. Assert(setterValue);
  2173. Assert(flags);
  2174. // Do a quick walk up the prototype chain to see if any of the prototypes has ever had ANY setter or non-writable property.
  2175. if (CheckIfObjectAndPrototypeChainHasOnlyWritableDataProperties(instance))
  2176. {
  2177. return FALSE;
  2178. }
  2179. RecyclableObject* object = instance;
  2180. while (!JavascriptOperators::IsNull(object))
  2181. {
  2182. *flags = object->GetItemSetter(index, setterValue, scriptContext);
  2183. if (*flags != None || skipPrototypeCheck)
  2184. {
  2185. break;
  2186. }
  2187. object = object->GetPrototype();
  2188. }
  2189. return ((*flags & Accessor) == Accessor) || ((*flags & Proxy) == Proxy) || ((*flags & Data) == Data && (*flags & Writable) == None);
  2190. }
  2191. BOOL JavascriptOperators::SetGlobalPropertyNoHost(char16 const * propertyName, charcount_t propertyLength, Var value, ScriptContext * scriptContext)
  2192. {
  2193. GlobalObject * globalObject = scriptContext->GetGlobalObject();
  2194. uint32 index;
  2195. PropertyRecord const * propertyRecord = nullptr;
  2196. IndexType indexType = GetIndexTypeFromString(propertyName, propertyLength, scriptContext, &index, &propertyRecord, true);
  2197. if (indexType == IndexType_Number)
  2198. {
  2199. return globalObject->DynamicObject::SetItem(index, value, PropertyOperation_None);
  2200. }
  2201. return globalObject->DynamicObject::SetProperty(propertyRecord->GetPropertyId(), value, PropertyOperation_None, NULL);
  2202. }
  2203. template<typename PropertyKeyType>
  2204. BOOL JavascriptOperators::SetPropertyWPCache(Var receiver, RecyclableObject* object, PropertyKeyType propertyKey, Var newValue, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags, _Inout_ PropertyValueInfo * info)
  2205. {
  2206. if (receiver)
  2207. {
  2208. AnalysisAssert(object);
  2209. Assert(!TaggedNumber::Is(receiver));
  2210. Var setterValueOrProxy = nullptr;
  2211. DescriptorFlags flags = None;
  2212. if (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(object, propertyKey, &setterValueOrProxy, &flags, info, requestContext))
  2213. {
  2214. if ((flags & Accessor) == Accessor)
  2215. {
  2216. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext))
  2217. {
  2218. return TRUE;
  2219. }
  2220. if (setterValueOrProxy)
  2221. {
  2222. if (VarIs<UnscopablesWrapperObject>(receiver))
  2223. {
  2224. receiver = (UnsafeVarTo<UnscopablesWrapperObject>(receiver))->GetWrappedObject();
  2225. }
  2226. else if (info->GetPropertyRecordUsageCache() && !JavascriptOperators::IsUndefinedAccessor(setterValueOrProxy, requestContext))
  2227. {
  2228. CacheOperators::CachePropertyWrite(VarTo<RecyclableObject>(receiver), false, object->GetType(), info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId(), info, requestContext);
  2229. }
  2230. RecyclableObject* func = VarTo<RecyclableObject>(setterValueOrProxy);
  2231. JavascriptOperators::CallSetter(func, receiver, newValue, requestContext);
  2232. }
  2233. return TRUE;
  2234. }
  2235. else if ((flags & Proxy) == Proxy)
  2236. {
  2237. Assert(VarIs<JavascriptProxy>(setterValueOrProxy));
  2238. JavascriptProxy* proxy = VarTo<JavascriptProxy>(setterValueOrProxy);
  2239. auto fn = [&](RecyclableObject* target) -> BOOL {
  2240. return JavascriptOperators::SetPropertyWPCache(receiver, target, propertyKey, newValue, requestContext, propertyOperationFlags, info);
  2241. };
  2242. if (info->GetPropertyRecordUsageCache())
  2243. {
  2244. PropertyValueInfo::SetNoCache(info, proxy);
  2245. PropertyValueInfo::DisablePrototypeCache(info, proxy);
  2246. }
  2247. return proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetPropertyWPCacheKind, propertyKey, newValue, requestContext, propertyOperationFlags);
  2248. }
  2249. else
  2250. {
  2251. Assert((flags & Data) == Data && (flags & Writable) == None);
  2252. requestContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  2253. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2254. return FALSE;
  2255. }
  2256. }
  2257. else if (!JavascriptOperators::IsObject(receiver))
  2258. {
  2259. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2260. return FALSE;
  2261. }
  2262. RecyclableObject* receiverObject = VarTo<RecyclableObject>(receiver);
  2263. if (receiver != object)
  2264. {
  2265. // If the receiver object has the property and it is an accessor then return false
  2266. PropertyDescriptor existingDesc;
  2267. if (JavascriptOperators::GetOwnPropertyDescriptor(receiverObject, propertyKey, requestContext, &existingDesc)
  2268. && existingDesc.IsAccessorDescriptor())
  2269. {
  2270. return FALSE;
  2271. }
  2272. }
  2273. Type *typeWithoutProperty = object->GetType();
  2274. // in 9.1.9, step 5, we should return false if receiver is not object, and that will happen in default RecyclableObject operation anyhow.
  2275. if (receiverObject->SetProperty(propertyKey, newValue, propertyOperationFlags, info))
  2276. {
  2277. if (!VarIs<JavascriptProxy>(receiver) && info->GetPropertyRecordUsageCache() && info->GetFlags() != InlineCacheSetterFlag && !object->IsExternal())
  2278. {
  2279. CacheOperators::CachePropertyWrite(VarTo<RecyclableObject>(receiver), false, typeWithoutProperty, info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId(), info, requestContext);
  2280. if (info->GetInstance() == receiverObject)
  2281. {
  2282. PropertyValueInfo::SetCacheInfo(info, info->GetPropertyRecordUsageCache()->GetLdElemInlineCache(), info->AllowResizingPolymorphicInlineCache());
  2283. CacheOperators::CachePropertyRead(object, receiverObject, false, info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId(), false, info, requestContext);
  2284. }
  2285. }
  2286. return TRUE;
  2287. }
  2288. }
  2289. return FALSE;
  2290. }
  2291. BOOL JavascriptOperators::SetItemOnTaggedNumber(Var receiver, RecyclableObject* object, uint32 index, Var newValue, ScriptContext* requestContext,
  2292. PropertyOperationFlags propertyOperationFlags)
  2293. {
  2294. Assert(TaggedNumber::Is(receiver));
  2295. if (requestContext->optimizationOverrides.GetSideEffects() & SideEffects_Accessor)
  2296. {
  2297. Var setterValueOrProxy = nullptr;
  2298. DescriptorFlags flags = None;
  2299. if (object == nullptr)
  2300. {
  2301. GetPropertyObject(receiver, requestContext, &object);
  2302. }
  2303. if (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableItem(object, index, &setterValueOrProxy, &flags, requestContext))
  2304. {
  2305. if ((flags & Accessor) == Accessor)
  2306. {
  2307. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext))
  2308. {
  2309. return TRUE;
  2310. }
  2311. if (setterValueOrProxy)
  2312. {
  2313. RecyclableObject* func = VarTo<RecyclableObject>(setterValueOrProxy);
  2314. JavascriptOperators::CallSetter(func, receiver, newValue, requestContext);
  2315. return TRUE;
  2316. }
  2317. }
  2318. else if ((flags & Proxy) == Proxy)
  2319. {
  2320. Assert(VarIs<JavascriptProxy>(setterValueOrProxy));
  2321. JavascriptProxy* proxy = VarTo<JavascriptProxy>(setterValueOrProxy);
  2322. const PropertyRecord* propertyRecord = nullptr;
  2323. proxy->PropertyIdFromInt(index, &propertyRecord);
  2324. return proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetItemOnTaggedNumberKind, propertyRecord->GetPropertyId(), newValue, requestContext, propertyOperationFlags);
  2325. }
  2326. else
  2327. {
  2328. Assert((flags & Data) == Data && (flags & Writable) == None);
  2329. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2330. }
  2331. }
  2332. }
  2333. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2334. return FALSE;
  2335. }
  2336. BOOL JavascriptOperators::SetPropertyOnTaggedNumber(Var receiver, RecyclableObject* object, PropertyId propertyId, Var newValue, ScriptContext* requestContext,
  2337. PropertyOperationFlags propertyOperationFlags)
  2338. {
  2339. Assert (TaggedNumber::Is(receiver));
  2340. if (requestContext->optimizationOverrides.GetSideEffects() & SideEffects_Accessor)
  2341. {
  2342. Var setterValueOrProxy = nullptr;
  2343. PropertyValueInfo info;
  2344. DescriptorFlags flags = None;
  2345. if (object == nullptr)
  2346. {
  2347. GetPropertyObject(receiver, requestContext, &object);
  2348. }
  2349. if (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(object, propertyId, &setterValueOrProxy, &flags, &info, requestContext))
  2350. {
  2351. if ((flags & Accessor) == Accessor)
  2352. {
  2353. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext))
  2354. {
  2355. return TRUE;
  2356. }
  2357. if (setterValueOrProxy)
  2358. {
  2359. RecyclableObject* func = VarTo<RecyclableObject>(setterValueOrProxy);
  2360. Assert(info.GetFlags() == InlineCacheSetterFlag || info.GetPropertyIndex() == Constants::NoSlot);
  2361. JavascriptOperators::CallSetter(func, receiver, newValue, requestContext);
  2362. return TRUE;
  2363. }
  2364. }
  2365. else if ((flags & Proxy) == Proxy)
  2366. {
  2367. Assert(VarIs<JavascriptProxy>(setterValueOrProxy));
  2368. JavascriptProxy* proxy = VarTo<JavascriptProxy>(setterValueOrProxy);
  2369. return proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetPropertyOnTaggedNumberKind, propertyId, newValue, requestContext, propertyOperationFlags);
  2370. }
  2371. else
  2372. {
  2373. Assert((flags & Data) == Data && (flags & Writable) == None);
  2374. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2375. }
  2376. }
  2377. }
  2378. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  2379. requestContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  2380. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2381. return FALSE;
  2382. }
  2383. BOOL JavascriptOperators::SetPropertyUnscopable(Var instance, RecyclableObject* receiver, PropertyId propertyId, Var newValue, PropertyValueInfo * info, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  2384. {
  2385. return SetProperty_Internal<true>(instance, receiver, false, propertyId, newValue, info, requestContext, propertyOperationFlags);
  2386. }
  2387. BOOL JavascriptOperators::SetProperty(Var receiver, RecyclableObject* object, PropertyId propertyId, Var newValue, PropertyValueInfo * info, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  2388. {
  2389. return SetProperty_Internal<false>(receiver, object, false, propertyId, newValue, info, requestContext, propertyOperationFlags);
  2390. }
  2391. BOOL JavascriptOperators::SetRootProperty(RecyclableObject* instance, PropertyId propertyId, Var newValue, PropertyValueInfo * info, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  2392. {
  2393. return SetProperty_Internal<false>(instance, instance, true, propertyId, newValue, info, requestContext, propertyOperationFlags);
  2394. }
  2395. // Returns true if a result was written.
  2396. bool JavascriptOperators::SetAccessorOrNonWritableProperty(
  2397. Var receiver,
  2398. RecyclableObject* object,
  2399. PropertyId propertyId,
  2400. Var newValue,
  2401. PropertyValueInfo * info,
  2402. ScriptContext* requestContext,
  2403. PropertyOperationFlags propertyOperationFlags,
  2404. bool isRoot,
  2405. bool allowUndecInConsoleScope,
  2406. BOOL *result)
  2407. {
  2408. *result = FALSE;
  2409. Var setterValueOrProxy = nullptr;
  2410. DescriptorFlags flags = None;
  2411. bool receiverNonWritable = false;
  2412. if (receiver != object && !isRoot)
  2413. {
  2414. Var receiverSetter = nullptr;
  2415. PropertyValueInfo receiverInfo;
  2416. DescriptorFlags receiverFlags = VarTo<RecyclableObject>(receiver)->GetSetter(propertyId, &receiverSetter, &receiverInfo, requestContext);
  2417. receiverNonWritable = ((receiverFlags & Data) == Data && (receiverFlags & Writable) == None);
  2418. }
  2419. if ((isRoot && JavascriptOperators::CheckPrototypesForAccessorOrNonWritableRootProperty(object, propertyId, &setterValueOrProxy, &flags, info, requestContext)) ||
  2420. (!isRoot && (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(object, propertyId, &setterValueOrProxy, &flags, info, requestContext) ||
  2421. receiverNonWritable)))
  2422. {
  2423. if ((flags & Accessor) == Accessor)
  2424. {
  2425. if (JavascriptError::ThrowIfUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext, propertyId))
  2426. {
  2427. *result = TRUE;
  2428. return true;
  2429. }
  2430. if (setterValueOrProxy)
  2431. {
  2432. RecyclableObject* func = VarTo<RecyclableObject>(setterValueOrProxy);
  2433. Assert(!info || info->GetFlags() == InlineCacheSetterFlag || info->GetPropertyIndex() == Constants::NoSlot);
  2434. if (VarIs<UnscopablesWrapperObject>(receiver))
  2435. {
  2436. receiver = (UnsafeVarTo<UnscopablesWrapperObject>(receiver))->GetWrappedObject();
  2437. }
  2438. else if (!JavascriptOperators::IsUndefinedAccessor(setterValueOrProxy, requestContext))
  2439. {
  2440. CacheOperators::CachePropertyWrite(VarTo<RecyclableObject>(receiver), isRoot, object->GetType(), propertyId, info, requestContext);
  2441. }
  2442. #ifdef ENABLE_MUTATION_BREAKPOINT
  2443. if (MutationBreakpoint::IsFeatureEnabled(requestContext))
  2444. {
  2445. MutationBreakpoint::HandleSetProperty(requestContext, object, propertyId, newValue);
  2446. }
  2447. #endif
  2448. JavascriptOperators::CallSetter(func, receiver, newValue, requestContext);
  2449. }
  2450. *result = TRUE;
  2451. return true;
  2452. }
  2453. else if ((flags & Proxy) == Proxy)
  2454. {
  2455. Assert(VarIs<JavascriptProxy>(setterValueOrProxy));
  2456. JavascriptProxy* proxy = VarTo<JavascriptProxy>(setterValueOrProxy);
  2457. // 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
  2458. // invalidate when target, handler, or handler prototype has changed. We don't have a way to achieve this yet.
  2459. PropertyValueInfo::SetNoCache(info, proxy);
  2460. PropertyValueInfo::DisablePrototypeCache(info, proxy); // We can't cache prototype property either
  2461. *result = proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetPropertyKind, propertyId, newValue, requestContext, propertyOperationFlags);
  2462. return true;
  2463. }
  2464. else
  2465. {
  2466. Assert(((flags & Data) == Data && (flags & Writable) == None) || receiverNonWritable);
  2467. if (!allowUndecInConsoleScope)
  2468. {
  2469. if (flags & Const)
  2470. {
  2471. JavascriptError::ThrowTypeError(requestContext, ERRAssignmentToConst);
  2472. }
  2473. JavascriptError::ThrowCantAssign(propertyOperationFlags, requestContext, propertyId);
  2474. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2475. *result = FALSE;
  2476. return true;
  2477. }
  2478. }
  2479. }
  2480. return false;
  2481. }
  2482. template <bool unscopables>
  2483. BOOL JavascriptOperators::SetProperty_Internal(Var receiver, RecyclableObject* object, const bool isRoot, PropertyId propertyId, Var newValue, PropertyValueInfo * info, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  2484. {
  2485. if (receiver == nullptr)
  2486. {
  2487. return FALSE;
  2488. }
  2489. Assert(!TaggedNumber::Is(receiver));
  2490. BOOL setAccessorResult = FALSE;
  2491. if (SetAccessorOrNonWritableProperty(receiver, object, propertyId, newValue, info, requestContext, propertyOperationFlags, isRoot, false, &setAccessorResult))
  2492. {
  2493. return setAccessorResult;
  2494. }
  2495. else if (!JavascriptOperators::IsObject(receiver))
  2496. {
  2497. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2498. return FALSE;
  2499. }
  2500. #ifdef ENABLE_MUTATION_BREAKPOINT
  2501. // Break on mutation if needed
  2502. bool doNotUpdateCacheForMbp = MutationBreakpoint::IsFeatureEnabled(requestContext) ?
  2503. MutationBreakpoint::HandleSetProperty(requestContext, object, propertyId, newValue) : false;
  2504. #endif
  2505. // Get the original type before setting the property
  2506. Type *typeWithoutProperty = object->GetType();
  2507. BOOL didSetProperty = false;
  2508. if (isRoot)
  2509. {
  2510. AssertMsg(JavascriptOperators::GetTypeId(receiver) == TypeIds_GlobalObject
  2511. || JavascriptOperators::GetTypeId(receiver) == TypeIds_ModuleRoot,
  2512. "Root must be a global object!");
  2513. RootObjectBase* rootObject = static_cast<RootObjectBase*>(receiver);
  2514. didSetProperty = rootObject->SetRootProperty(propertyId, newValue, propertyOperationFlags, info);
  2515. }
  2516. else
  2517. {
  2518. RecyclableObject* instanceObject = VarTo<RecyclableObject>(receiver);
  2519. while (!JavascriptOperators::IsNull(instanceObject))
  2520. {
  2521. if (unscopables && JavascriptOperators::IsPropertyUnscopable(instanceObject, propertyId))
  2522. {
  2523. break;
  2524. }
  2525. else
  2526. {
  2527. didSetProperty = instanceObject->SetProperty(propertyId, newValue, propertyOperationFlags, info);
  2528. if (didSetProperty || !unscopables)
  2529. {
  2530. break;
  2531. }
  2532. }
  2533. instanceObject = JavascriptOperators::GetPrototypeNoTrap(instanceObject);
  2534. }
  2535. }
  2536. if (didSetProperty)
  2537. {
  2538. bool updateCache = true;
  2539. #ifdef ENABLE_MUTATION_BREAKPOINT
  2540. updateCache = updateCache && !doNotUpdateCacheForMbp;
  2541. #endif
  2542. if (updateCache)
  2543. {
  2544. if (!VarIs<JavascriptProxy>(receiver))
  2545. {
  2546. CacheOperators::CachePropertyWrite(VarTo<RecyclableObject>(receiver), isRoot, typeWithoutProperty, propertyId, info, requestContext);
  2547. }
  2548. }
  2549. return TRUE;
  2550. }
  2551. return FALSE;
  2552. }
  2553. BOOL JavascriptOperators::IsNumberFromNativeArray(Var instance, uint32 index, ScriptContext* scriptContext)
  2554. {
  2555. #if ENABLE_COPYONACCESS_ARRAY
  2556. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  2557. #endif
  2558. Js::TypeId instanceType = JavascriptOperators::GetTypeId(instance);
  2559. // Fast path for native and typed arrays.
  2560. bool isNativeArray = instanceType == TypeIds_NativeIntArray || instanceType == TypeIds_NativeFloatArray;
  2561. bool isTypedArray = instanceType >= TypeIds_Int8Array && instanceType <= TypeIds_Uint64Array;
  2562. if (isNativeArray || isTypedArray)
  2563. {
  2564. // Check if the typed array is detached to prevent an exception in GetOwnItem
  2565. if (isTypedArray && TypedArrayBase::IsDetachedTypedArray(instance))
  2566. {
  2567. return FALSE;
  2568. }
  2569. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  2570. Var member = nullptr;
  2571. // If the item is found in the array own body, then it is a number
  2572. if (JavascriptOperators::GetOwnItem(object, index, &member, scriptContext)
  2573. && !JavascriptOperators::IsUndefined(member))
  2574. {
  2575. return TRUE;
  2576. }
  2577. }
  2578. return FALSE;
  2579. }
  2580. BOOL _Check_return_ _Success_(return) JavascriptOperators::GetAccessors(RecyclableObject* instance, PropertyId propertyId, ScriptContext* requestContext, _Out_ Var* getter, _Out_ Var* setter)
  2581. {
  2582. RecyclableObject* object = instance;
  2583. while (!JavascriptOperators::IsNull(object))
  2584. {
  2585. if (object->GetAccessors(propertyId, getter, setter, requestContext))
  2586. {
  2587. *getter = JavascriptOperators::CanonicalizeAccessor(*getter, requestContext);
  2588. *setter = JavascriptOperators::CanonicalizeAccessor(*setter, requestContext);
  2589. return TRUE;
  2590. }
  2591. if (object->SkipsPrototype())
  2592. {
  2593. break;
  2594. }
  2595. object = JavascriptOperators::GetPrototype(object);
  2596. }
  2597. return FALSE;
  2598. }
  2599. BOOL JavascriptOperators::SetAccessors(RecyclableObject* instance, PropertyId propertyId, Var getter, Var setter, PropertyOperationFlags flags)
  2600. {
  2601. BOOL result = instance && instance->SetAccessors(propertyId, getter, setter, flags);
  2602. return result;
  2603. }
  2604. BOOL JavascriptOperators::OP_SetProperty(Var instance, PropertyId propertyId, Var newValue, ScriptContext* scriptContext, PropertyValueInfo * info, PropertyOperationFlags flags, Var thisInstance)
  2605. {
  2606. // The call into ToObject(dynamicObject) is avoided here by checking for null and undefined and doing nothing when dynamicObject is a primitive value.
  2607. if (thisInstance == nullptr)
  2608. {
  2609. thisInstance = instance;
  2610. }
  2611. TypeId typeId = JavascriptOperators::GetTypeId(instance);
  2612. if (JavascriptOperators::IsUndefinedOrNullType(typeId))
  2613. {
  2614. if (scriptContext->GetThreadContext()->RecordImplicitException())
  2615. {
  2616. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotSet_NullOrUndefined, scriptContext->GetPropertyName(propertyId)->GetBuffer());
  2617. }
  2618. return TRUE;
  2619. }
  2620. if (!TaggedNumber::Is(instance) && !TaggedNumber::Is(thisInstance))
  2621. {
  2622. return JavascriptOperators::SetProperty(UnsafeVarTo<RecyclableObject>(thisInstance), UnsafeVarTo<RecyclableObject>(instance), propertyId, newValue, info, scriptContext, flags);
  2623. }
  2624. JavascriptError::ThrowCantAssignIfStrictMode(flags, scriptContext);
  2625. return false;
  2626. }
  2627. BOOL JavascriptOperators::OP_StFunctionExpression(Var obj, PropertyId propertyId, Var newValue)
  2628. {
  2629. RecyclableObject* instance = VarTo<RecyclableObject>(obj);
  2630. JIT_HELPER_NOT_REENTRANT_HEADER(Op_StFunctionExpression, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2631. instance->SetProperty(propertyId, newValue, PropertyOperation_None, NULL);
  2632. instance->SetWritable(propertyId, FALSE);
  2633. instance->SetConfigurable(propertyId, FALSE);
  2634. return TRUE;
  2635. JIT_HELPER_END(Op_StFunctionExpression);
  2636. }
  2637. BOOL JavascriptOperators::OP_InitClassMember(Var obj, PropertyId propertyId, Var newValue)
  2638. {
  2639. JIT_HELPER_REENTRANT_HEADER(Op_InitClassMember);
  2640. RecyclableObject* instance = VarTo<RecyclableObject>(obj);
  2641. PropertyOperationFlags flags = PropertyOperation_None;
  2642. PropertyAttributes attributes = PropertyClassMemberDefaults;
  2643. instance->SetPropertyWithAttributes(propertyId, newValue, attributes, NULL, flags);
  2644. return TRUE;
  2645. JIT_HELPER_END(Op_InitClassMember);
  2646. }
  2647. BOOL JavascriptOperators::OP_InitLetProperty(Var obj, PropertyId propertyId, Var newValue)
  2648. {
  2649. JIT_HELPER_REENTRANT_HEADER(Op_InitLetFld);
  2650. RecyclableObject* instance = VarTo<RecyclableObject>(obj);
  2651. PropertyOperationFlags flags = instance->GetScriptContext()->IsUndeclBlockVar(newValue) ? PropertyOperation_SpecialValue : PropertyOperation_None;
  2652. PropertyAttributes attributes = PropertyLetDefaults;
  2653. if (VarIs<RootObjectBase>(instance))
  2654. {
  2655. attributes |= PropertyLetConstGlobal;
  2656. }
  2657. instance->SetPropertyWithAttributes(propertyId, newValue, attributes, NULL, (PropertyOperationFlags)(flags | PropertyOperation_AllowUndecl));
  2658. return TRUE;
  2659. JIT_HELPER_END(Op_InitLetFld);
  2660. }
  2661. BOOL JavascriptOperators::OP_InitConstProperty(Var obj, PropertyId propertyId, Var newValue)
  2662. {
  2663. RecyclableObject* instance = VarTo<RecyclableObject>(obj);
  2664. JIT_HELPER_REENTRANT_HEADER(Op_InitConstFld);
  2665. PropertyOperationFlags flags = instance->GetScriptContext()->IsUndeclBlockVar(newValue) ? PropertyOperation_SpecialValue : PropertyOperation_None;
  2666. PropertyAttributes attributes = PropertyConstDefaults;
  2667. if (VarIs<RootObjectBase>(instance))
  2668. {
  2669. attributes |= PropertyLetConstGlobal;
  2670. }
  2671. instance->SetPropertyWithAttributes(propertyId, newValue, attributes, NULL, (PropertyOperationFlags)(flags | PropertyOperation_AllowUndecl));
  2672. return TRUE;
  2673. JIT_HELPER_END(Op_InitConstFld);
  2674. }
  2675. BOOL JavascriptOperators::OP_InitUndeclRootLetProperty(Var obj, PropertyId propertyId)
  2676. {
  2677. RecyclableObject* instance = VarTo<RecyclableObject>(obj);
  2678. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitUndeclRootLetFld, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2679. PropertyOperationFlags flags = static_cast<PropertyOperationFlags>(PropertyOperation_SpecialValue | PropertyOperation_AllowUndecl);
  2680. PropertyAttributes attributes = PropertyLetDefaults | PropertyLetConstGlobal;
  2681. instance->SetPropertyWithAttributes(propertyId, instance->GetLibrary()->GetUndeclBlockVar(), attributes, NULL, flags);
  2682. return TRUE;
  2683. JIT_HELPER_END(Op_InitUndeclRootLetFld);
  2684. }
  2685. BOOL JavascriptOperators::OP_InitUndeclRootConstProperty(Var obj, PropertyId propertyId)
  2686. {
  2687. RecyclableObject* instance = VarTo<RecyclableObject>(obj);
  2688. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitUndeclRootConstFld, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2689. PropertyOperationFlags flags = static_cast<PropertyOperationFlags>(PropertyOperation_SpecialValue | PropertyOperation_AllowUndecl);
  2690. PropertyAttributes attributes = PropertyConstDefaults | PropertyLetConstGlobal;
  2691. instance->SetPropertyWithAttributes(propertyId, instance->GetLibrary()->GetUndeclBlockVar(), attributes, NULL, flags);
  2692. return TRUE;
  2693. JIT_HELPER_END(Op_InitUndeclRootConstFld);
  2694. }
  2695. BOOL JavascriptOperators::OP_InitUndeclConsoleLetProperty(Var obj, PropertyId propertyId)
  2696. {
  2697. FrameDisplay *pScope = (FrameDisplay*)obj;
  2698. AssertMsg(VarIs<ConsoleScopeActivationObject>((DynamicObject*)pScope->GetItem(pScope->GetLength() - 1)), "How come we got this opcode without ConsoleScopeActivationObject?");
  2699. RecyclableObject* instance = VarTo<RecyclableObject>(pScope->GetItem(0));
  2700. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitUndeclConsoleLetFld, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2701. PropertyOperationFlags flags = static_cast<PropertyOperationFlags>(PropertyOperation_SpecialValue | PropertyOperation_AllowUndecl);
  2702. PropertyAttributes attributes = PropertyLetDefaults;
  2703. instance->SetPropertyWithAttributes(propertyId, instance->GetLibrary()->GetUndeclBlockVar(), attributes, NULL, flags);
  2704. return TRUE;
  2705. JIT_HELPER_END(Op_InitUndeclConsoleLetFld);
  2706. }
  2707. BOOL JavascriptOperators::OP_InitUndeclConsoleConstProperty(Var obj, PropertyId propertyId)
  2708. {
  2709. FrameDisplay *pScope = (FrameDisplay*)obj;
  2710. AssertMsg(VarIs<ConsoleScopeActivationObject>((DynamicObject*)pScope->GetItem(pScope->GetLength() - 1)), "How come we got this opcode without ConsoleScopeActivationObject?");
  2711. RecyclableObject* instance = VarTo<RecyclableObject>(pScope->GetItem(0));
  2712. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitUndeclConsoleConstFld, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2713. PropertyOperationFlags flags = static_cast<PropertyOperationFlags>(PropertyOperation_SpecialValue | PropertyOperation_AllowUndecl);
  2714. PropertyAttributes attributes = PropertyConstDefaults;
  2715. instance->SetPropertyWithAttributes(propertyId, instance->GetLibrary()->GetUndeclBlockVar(), attributes, NULL, flags);
  2716. return TRUE;
  2717. JIT_HELPER_END(Op_InitUndeclConsoleConstFld);
  2718. }
  2719. BOOL JavascriptOperators::InitProperty(RecyclableObject* instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  2720. {
  2721. return instance && instance->InitProperty(propertyId, newValue, flags);
  2722. }
  2723. BOOL JavascriptOperators::OP_InitProperty(Var instance, PropertyId propertyId, Var newValue)
  2724. {
  2725. if(TaggedNumber::Is(instance)) { return false; }
  2726. return JavascriptOperators::InitProperty(VarTo<RecyclableObject>(instance), propertyId, newValue);
  2727. }
  2728. BOOL JavascriptOperators::DeleteProperty(RecyclableObject* instance, PropertyId propertyId, PropertyOperationFlags propertyOperationFlags)
  2729. {
  2730. return DeleteProperty_Impl<false>(instance, propertyId, propertyOperationFlags);
  2731. }
  2732. bool JavascriptOperators::ShouldTryDeleteProperty(RecyclableObject* instance, JavascriptString *propertyNameString, PropertyRecord const **pPropertyRecord)
  2733. {
  2734. PropertyRecord const *propertyRecord = nullptr;
  2735. if (!JavascriptOperators::CanShortcutOnUnknownPropertyName(instance))
  2736. {
  2737. instance->GetScriptContext()->GetOrAddPropertyRecord(propertyNameString, &propertyRecord);
  2738. }
  2739. else
  2740. {
  2741. instance->GetScriptContext()->FindPropertyRecord(propertyNameString, &propertyRecord);
  2742. }
  2743. if (propertyRecord == nullptr)
  2744. {
  2745. return false;
  2746. }
  2747. *pPropertyRecord = propertyRecord;
  2748. return true;
  2749. }
  2750. BOOL JavascriptOperators::DeleteProperty(RecyclableObject* instance, JavascriptString *propertyNameString, PropertyOperationFlags propertyOperationFlags)
  2751. {
  2752. #ifdef ENABLE_MUTATION_BREAKPOINT
  2753. ScriptContext *scriptContext = instance->GetScriptContext();
  2754. if (MutationBreakpoint::IsFeatureEnabled(scriptContext)
  2755. && scriptContext->HasMutationBreakpoints())
  2756. {
  2757. MutationBreakpoint::HandleDeleteProperty(scriptContext, instance, propertyNameString);
  2758. }
  2759. #endif
  2760. return instance->DeleteProperty(propertyNameString, propertyOperationFlags);
  2761. }
  2762. BOOL JavascriptOperators::DeletePropertyUnscopables(RecyclableObject* instance, PropertyId propertyId, PropertyOperationFlags propertyOperationFlags)
  2763. {
  2764. return DeleteProperty_Impl<true>(instance, propertyId, propertyOperationFlags);
  2765. }
  2766. template<bool unscopables>
  2767. BOOL JavascriptOperators::DeleteProperty_Impl(RecyclableObject* instance, PropertyId propertyId, PropertyOperationFlags propertyOperationFlags)
  2768. {
  2769. if (unscopables && JavascriptOperators::IsPropertyUnscopable(instance, propertyId))
  2770. {
  2771. return false;
  2772. }
  2773. #ifdef ENABLE_MUTATION_BREAKPOINT
  2774. ScriptContext *scriptContext = instance->GetScriptContext();
  2775. if (MutationBreakpoint::IsFeatureEnabled(scriptContext)
  2776. && scriptContext->HasMutationBreakpoints())
  2777. {
  2778. MutationBreakpoint::HandleDeleteProperty(scriptContext, instance, propertyId);
  2779. }
  2780. #endif
  2781. // !unscopables will hit the return statement on the first iteration
  2782. return instance->DeleteProperty(propertyId, propertyOperationFlags);
  2783. }
  2784. Var JavascriptOperators::OP_DeleteProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  2785. {
  2786. JIT_HELPER_REENTRANT_HEADER(Op_DeleteProperty);
  2787. if(TaggedNumber::Is(instance))
  2788. {
  2789. return scriptContext->GetLibrary()->GetTrue();
  2790. }
  2791. RecyclableObject* recyclableObject = VarTo<RecyclableObject>(instance);
  2792. if (JavascriptOperators::IsUndefinedOrNull(recyclableObject))
  2793. {
  2794. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotDelete_NullOrUndefined,
  2795. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  2796. }
  2797. return scriptContext->GetLibrary()->CreateBoolean(
  2798. JavascriptOperators::DeleteProperty(recyclableObject, propertyId, propertyOperationFlags));
  2799. JIT_HELPER_END(Op_DeleteProperty);
  2800. }
  2801. Var JavascriptOperators::OP_DeleteRootProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  2802. {
  2803. // In Edge the root is an External Object which can call Dispose and thus, can have reentrancy.
  2804. JIT_HELPER_REENTRANT_HEADER(Op_DeleteRootProperty);
  2805. AssertMsg(VarIs<RootObjectBase>(instance), "Root must be a global object!");
  2806. RootObjectBase* rootObject = static_cast<RootObjectBase*>(instance);
  2807. return scriptContext->GetLibrary()->CreateBoolean(
  2808. rootObject->DeleteRootProperty(propertyId, propertyOperationFlags));
  2809. JIT_HELPER_END(Op_DeleteRootProperty);
  2810. }
  2811. template <bool IsFromFullJit, class TInlineCache>
  2812. inline void JavascriptOperators::PatchSetPropertyScoped(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var newValue, Var defaultInstance, PropertyOperationFlags propertyOperationFlags)
  2813. {
  2814. JIT_HELPER_REENTRANT_HEADER(Op_PatchSetPropertyScoped);
  2815. // Set the property using a scope stack rather than an individual instance.
  2816. // Walk the stack until we find an instance that has the property and store
  2817. // the new value there.
  2818. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  2819. uint16 length = pDisplay->GetLength();
  2820. RecyclableObject *object;
  2821. PropertyValueInfo info;
  2822. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  2823. bool allowUndecInConsoleScope = (propertyOperationFlags & PropertyOperation_AllowUndeclInConsoleScope) == PropertyOperation_AllowUndeclInConsoleScope;
  2824. bool isLexicalThisSlotSymbol = (propertyId == PropertyIds::_this);
  2825. for (uint16 i = 0; i < length; i++)
  2826. {
  2827. object = UnsafeVarTo<RecyclableObject>(pDisplay->GetItem(i));
  2828. AssertMsg(!VarIs<ConsoleScopeActivationObject>(object) || (i == length - 1), "Invalid location for ConsoleScopeActivationObject");
  2829. Type* type = object->GetType();
  2830. if (CacheOperators::TrySetProperty<true, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  2831. object, false, propertyId, newValue, scriptContext, propertyOperationFlags, nullptr, &info))
  2832. {
  2833. return;
  2834. }
  2835. // In scoped set property, we need to set the property when it is available; it could be a setter
  2836. // or normal property. we need to check setter first, and if no setter is available, but HasProperty
  2837. // is true, this must be a normal property.
  2838. // TODO: merge OP_HasProperty and GetSetter in one pass if there is perf problem. In fastDOM we have quite
  2839. // a lot of setters so separating the two might be actually faster.
  2840. BOOL setAccessorResult = FALSE;
  2841. if (SetAccessorOrNonWritableProperty(object, object, propertyId, newValue, &info, scriptContext, propertyOperationFlags, false, allowUndecInConsoleScope, &setAccessorResult))
  2842. {
  2843. return;
  2844. }
  2845. else if (!JavascriptOperators::IsObject(object))
  2846. {
  2847. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, scriptContext);
  2848. }
  2849. // Need to do a "get" of the current value (if any) to make sure that we're not writing to
  2850. // let/const before declaration, but we need to disable implicit calls around the "get",
  2851. // so we need to do a "has" first to make sure the "get" is valid (e.g., "get" on a HostDispatch
  2852. // with implicit calls disabled will always "succeed").
  2853. if (JavascriptOperators::HasProperty(object, propertyId))
  2854. {
  2855. DisableImplicitFlags disableImplicitFlags = scriptContext->GetThreadContext()->GetDisableImplicitFlags();
  2856. scriptContext->GetThreadContext()->SetDisableImplicitFlags(DisableImplicitCallAndExceptionFlag);
  2857. Var value;
  2858. BOOL result = JavascriptOperators::GetProperty(object, propertyId, &value, scriptContext, nullptr);
  2859. scriptContext->GetThreadContext()->SetDisableImplicitFlags(disableImplicitFlags);
  2860. if (result && scriptContext->IsUndeclBlockVar(value) && !allowUndecInConsoleScope && !isLexicalThisSlotSymbol)
  2861. {
  2862. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  2863. }
  2864. PropertyValueInfo info2;
  2865. PropertyValueInfo::SetCacheInfo(&info2, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  2866. PropertyOperationFlags setPropertyOpFlags = allowUndecInConsoleScope ? PropertyOperation_AllowUndeclInConsoleScope : PropertyOperation_None;
  2867. object->SetProperty(propertyId, newValue, setPropertyOpFlags, &info2);
  2868. #if DBG_DUMP
  2869. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  2870. {
  2871. CacheOperators::TraceCache(inlineCache, _u("PatchSetPropertyScoped"), propertyId, scriptContext, object);
  2872. }
  2873. #endif
  2874. if (!VarIs<JavascriptProxy>(object) && !allowUndecInConsoleScope)
  2875. {
  2876. CacheOperators::CachePropertyWrite(object, false, type, propertyId, &info2, scriptContext);
  2877. }
  2878. return;
  2879. }
  2880. }
  2881. Assert(!isLexicalThisSlotSymbol);
  2882. // If we have console scope and no one in the scope had the property add it to console scope
  2883. if ((length > 0) && VarIs<ConsoleScopeActivationObject>(pDisplay->GetItem(length - 1)))
  2884. {
  2885. // CheckPrototypesForAccessorOrNonWritableProperty does not check for const in global object. We should check it here.
  2886. if (length > 1)
  2887. {
  2888. Js::GlobalObject * globalObject = JavascriptOperators::TryFromVar<Js::GlobalObject>(pDisplay->GetItem(length - 2));
  2889. if (globalObject)
  2890. {
  2891. Var setterValue = nullptr;
  2892. DescriptorFlags flags = JavascriptOperators::GetRootSetter(globalObject, propertyId, &setterValue, &info, scriptContext);
  2893. Assert((flags & Accessor) != Accessor);
  2894. Assert((flags & Proxy) != Proxy);
  2895. if ((flags & Data) == Data && (flags & Writable) == None)
  2896. {
  2897. if (!allowUndecInConsoleScope)
  2898. {
  2899. if (flags & Const)
  2900. {
  2901. JavascriptError::ThrowTypeError(scriptContext, ERRAssignmentToConst);
  2902. }
  2903. Assert(!isLexicalThisSlotSymbol);
  2904. return;
  2905. }
  2906. }
  2907. }
  2908. }
  2909. RecyclableObject* obj = VarTo<RecyclableObject>(pDisplay->GetItem(length - 1));
  2910. OUTPUT_TRACE(Js::ConsoleScopePhase, _u("Adding property '%s' to console scope object\n"), scriptContext->GetPropertyName(propertyId)->GetBuffer());
  2911. JavascriptOperators::SetProperty(obj, obj, propertyId, newValue, scriptContext, propertyOperationFlags);
  2912. return;
  2913. }
  2914. // No one in the scope stack has the property, so add it to the default instance provided by the caller.
  2915. AssertMsg(!TaggedNumber::Is(defaultInstance), "Root object is an int or tagged float?");
  2916. Assert(defaultInstance != nullptr);
  2917. RecyclableObject* obj = VarTo<RecyclableObject>(defaultInstance);
  2918. {
  2919. //SetPropertyScoped does not use inline cache for default instance
  2920. PropertyValueInfo info2;
  2921. JavascriptOperators::SetRootProperty(obj, propertyId, newValue, &info2, scriptContext, (PropertyOperationFlags)(propertyOperationFlags | PropertyOperation_Root));
  2922. }
  2923. JIT_HELPER_END(Op_PatchSetPropertyScoped);
  2924. }
  2925. JIT_HELPER_TEMPLATE(Op_PatchSetPropertyScoped, Op_ConsolePatchSetPropertyScoped)
  2926. 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);
  2927. 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);
  2928. 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);
  2929. 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);
  2930. BOOL JavascriptOperators::OP_InitFuncScoped(FrameDisplay *pScope, PropertyId propertyId, Var newValue, Var defaultInstance, ScriptContext* scriptContext)
  2931. {
  2932. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitFuncScoped, reentrancylock, scriptContext->GetThreadContext());
  2933. int i;
  2934. int length = pScope->GetLength();
  2935. DynamicObject *obj;
  2936. for (i = 0; i < length; i++)
  2937. {
  2938. obj = (DynamicObject*)pScope->GetItem(i);
  2939. if (obj->InitFuncScoped(propertyId, newValue))
  2940. {
  2941. return TRUE;
  2942. }
  2943. }
  2944. AssertMsg(!TaggedNumber::Is(defaultInstance), "Root object is an int or tagged float?");
  2945. return VarTo<RecyclableObject>(defaultInstance)->InitFuncScoped(propertyId, newValue);
  2946. JIT_HELPER_END(Op_InitFuncScoped);
  2947. }
  2948. BOOL JavascriptOperators::OP_InitPropertyScoped(FrameDisplay *pScope, PropertyId propertyId, Var newValue, Var defaultInstance, ScriptContext* scriptContext)
  2949. {
  2950. int i;
  2951. int length = pScope->GetLength();
  2952. DynamicObject *obj;
  2953. for (i = 0; i < length; i++)
  2954. {
  2955. obj = (DynamicObject*)pScope->GetItem(i);
  2956. if (obj->InitPropertyScoped(propertyId, newValue))
  2957. {
  2958. return TRUE;
  2959. }
  2960. }
  2961. AssertMsg(!TaggedNumber::Is(defaultInstance), "Root object is an int or tagged float?");
  2962. return VarTo<RecyclableObject>(defaultInstance)->InitPropertyScoped(propertyId, newValue);
  2963. }
  2964. Var JavascriptOperators::OP_DeletePropertyScoped(
  2965. FrameDisplay *pScope,
  2966. PropertyId propertyId,
  2967. Var defaultInstance,
  2968. ScriptContext* scriptContext,
  2969. PropertyOperationFlags propertyOperationFlags)
  2970. {
  2971. JIT_HELPER_REENTRANT_HEADER(Op_DeletePropertyScoped);
  2972. JIT_HELPER_SAME_ATTRIBUTES(Op_DeleteRootProperty, Op_DeletePropertyScoped);
  2973. int i;
  2974. int length = pScope->GetLength();
  2975. for (i = 0; i < length; i++)
  2976. {
  2977. DynamicObject *obj = (DynamicObject*)pScope->GetItem(i);
  2978. if (JavascriptOperators::HasProperty(obj, propertyId))
  2979. {
  2980. return scriptContext->GetLibrary()->CreateBoolean(JavascriptOperators::DeleteProperty(obj, propertyId, propertyOperationFlags));
  2981. }
  2982. }
  2983. return JavascriptOperators::OP_DeleteRootProperty(VarTo<RecyclableObject>(defaultInstance), propertyId, scriptContext, propertyOperationFlags);
  2984. JIT_HELPER_END(Op_DeletePropertyScoped);
  2985. }
  2986. Var JavascriptOperators::OP_TypeofPropertyScoped(FrameDisplay *pScope, PropertyId propertyId, Var defaultInstance, ScriptContext* scriptContext)
  2987. {
  2988. JIT_HELPER_REENTRANT_HEADER(Op_TypeofPropertyScoped);
  2989. int i;
  2990. int length = pScope->GetLength();
  2991. for (i = 0; i < length; i++)
  2992. {
  2993. DynamicObject *obj = (DynamicObject*)pScope->GetItem(i);
  2994. if (JavascriptOperators::HasProperty(obj, propertyId))
  2995. {
  2996. return JavascriptOperators::TypeofFld(obj, propertyId, scriptContext);
  2997. }
  2998. }
  2999. return JavascriptOperators::TypeofRootFld(VarTo<RecyclableObject>(defaultInstance), propertyId, scriptContext);
  3000. JIT_HELPER_END(Op_TypeofPropertyScoped);
  3001. }
  3002. BOOL JavascriptOperators::HasOwnItem(RecyclableObject* object, uint32 index)
  3003. {
  3004. return object->HasOwnItem(index);
  3005. }
  3006. BOOL JavascriptOperators::HasItem(RecyclableObject* object, uint64 index)
  3007. {
  3008. PropertyRecord const * propertyRecord = nullptr;
  3009. ScriptContext* scriptContext = object->GetScriptContext();
  3010. JavascriptOperators::GetPropertyIdForInt(index, scriptContext, &propertyRecord);
  3011. return JavascriptOperators::HasProperty(object, propertyRecord->GetPropertyId());
  3012. }
  3013. BOOL JavascriptOperators::HasItem(RecyclableObject* object, uint32 index)
  3014. {
  3015. #if ENABLE_COPYONACCESS_ARRAY
  3016. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(object);
  3017. #endif
  3018. while (!JavascriptOperators::IsNull(object))
  3019. {
  3020. PropertyQueryFlags result;
  3021. if ((result = object->HasItemQuery(index)) != PropertyQueryFlags::Property_NotFound)
  3022. {
  3023. return JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  3024. }
  3025. // CONSIDER: Numeric property values shouldn't be on the prototype for now but if this changes
  3026. // we should add SkipsPrototype support here as well
  3027. object = JavascriptOperators::GetPrototypeNoTrap(object);
  3028. }
  3029. return false;
  3030. }
  3031. BOOL JavascriptOperators::GetOwnItem(RecyclableObject* object, uint32 index, Var* value, ScriptContext* requestContext)
  3032. {
  3033. return object->GetItem(object, index, value, requestContext);
  3034. }
  3035. BOOL JavascriptOperators::GetItem(Var instance, RecyclableObject* propertyObject, uint32 index, Var* value, ScriptContext* requestContext)
  3036. {
  3037. RecyclableObject* object = propertyObject;
  3038. while (!JavascriptOperators::IsNull(object))
  3039. {
  3040. PropertyQueryFlags result;
  3041. if ((result = object->GetItemQuery(instance, index, value, requestContext)) != PropertyQueryFlags::Property_NotFound)
  3042. {
  3043. return JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  3044. }
  3045. if (object->SkipsPrototype())
  3046. {
  3047. break;
  3048. }
  3049. object = JavascriptOperators::GetPrototypeNoTrap(object);
  3050. }
  3051. *value = requestContext->GetMissingItemResult();
  3052. return false;
  3053. }
  3054. BOOL JavascriptOperators::GetItemReference(Var instance, RecyclableObject* propertyObject, uint32 index, Var* value, ScriptContext* requestContext)
  3055. {
  3056. RecyclableObject* object = propertyObject;
  3057. while (!JavascriptOperators::IsNull(object))
  3058. {
  3059. PropertyQueryFlags result;
  3060. if ((result = object->GetItemReferenceQuery(instance, index, value, requestContext)) != PropertyQueryFlags::Property_NotFound)
  3061. {
  3062. return JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  3063. }
  3064. if (object->SkipsPrototype())
  3065. {
  3066. break;
  3067. }
  3068. object = JavascriptOperators::GetPrototypeNoTrap(object);
  3069. }
  3070. *value = requestContext->GetMissingItemResult();
  3071. return false;
  3072. }
  3073. BOOL JavascriptOperators::SetItem(Var receiver, RecyclableObject* object, uint64 index, Var value, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  3074. {
  3075. PropertyRecord const * propertyRecord = nullptr;
  3076. JavascriptOperators::GetPropertyIdForInt(index, scriptContext, &propertyRecord);
  3077. return JavascriptOperators::SetProperty(receiver, object, propertyRecord->GetPropertyId(), value, scriptContext, propertyOperationFlags);
  3078. }
  3079. BOOL JavascriptOperators::SetItem(Var receiver, RecyclableObject* object, uint32 index, Var value, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags, BOOL skipPrototypeCheck /* = FALSE */)
  3080. {
  3081. Var setterValueOrProxy = nullptr;
  3082. DescriptorFlags flags = None;
  3083. Assert(!TaggedNumber::Is(receiver));
  3084. if (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableItem(object, index, &setterValueOrProxy, &flags, scriptContext, skipPrototypeCheck))
  3085. {
  3086. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  3087. if ((flags & Accessor) == Accessor)
  3088. {
  3089. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, scriptContext) ||
  3090. JavascriptError::ThrowIfNotExtensibleUndefinedSetter(propertyOperationFlags, setterValueOrProxy, scriptContext))
  3091. {
  3092. return TRUE;
  3093. }
  3094. if (setterValueOrProxy)
  3095. {
  3096. RecyclableObject* func = VarTo<RecyclableObject>(setterValueOrProxy);
  3097. JavascriptOperators::CallSetter(func, receiver, value, scriptContext);
  3098. }
  3099. return TRUE;
  3100. }
  3101. else if ((flags & Proxy) == Proxy)
  3102. {
  3103. Assert(VarIs<JavascriptProxy>(setterValueOrProxy));
  3104. JavascriptProxy* proxy = VarTo<JavascriptProxy>(setterValueOrProxy);
  3105. const PropertyRecord* propertyRecord = nullptr;
  3106. proxy->PropertyIdFromInt(index, &propertyRecord);
  3107. return proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetItemKind, propertyRecord->GetPropertyId(), value, scriptContext, propertyOperationFlags, skipPrototypeCheck);
  3108. }
  3109. else
  3110. {
  3111. Assert((flags & Data) == Data && (flags & Writable) == None);
  3112. if ((propertyOperationFlags & PropertyOperationFlags::PropertyOperation_ThrowIfNotExtensible) == PropertyOperationFlags::PropertyOperation_ThrowIfNotExtensible)
  3113. {
  3114. JavascriptError::ThrowTypeError(scriptContext, JSERR_NonExtensibleObject);
  3115. }
  3116. JavascriptError::ThrowCantAssign(propertyOperationFlags, scriptContext, index);
  3117. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, scriptContext);
  3118. return FALSE;
  3119. }
  3120. }
  3121. else if (!JavascriptOperators::IsObject(receiver))
  3122. {
  3123. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, scriptContext);
  3124. return FALSE;
  3125. }
  3126. return (VarTo<RecyclableObject>(receiver))->SetItem(index, value, propertyOperationFlags);
  3127. }
  3128. BOOL JavascriptOperators::DeleteItem(RecyclableObject* object, uint32 index, PropertyOperationFlags propertyOperationFlags)
  3129. {
  3130. return object->DeleteItem(index, propertyOperationFlags);
  3131. }
  3132. BOOL JavascriptOperators::DeleteItem(RecyclableObject* object, uint64 index, PropertyOperationFlags propertyOperationFlags)
  3133. {
  3134. PropertyRecord const * propertyRecord = nullptr;
  3135. JavascriptOperators::GetPropertyIdForInt(index, object->GetScriptContext(), &propertyRecord);
  3136. return JavascriptOperators::DeleteProperty(object, propertyRecord->GetPropertyId(), propertyOperationFlags);
  3137. }
  3138. BOOL JavascriptOperators::OP_HasItem(Var instance, Var index, ScriptContext* scriptContext)
  3139. {
  3140. RecyclableObject* object = TaggedNumber::Is(instance) ?
  3141. scriptContext->GetLibrary()->GetNumberPrototype() :
  3142. VarTo<RecyclableObject>(instance);
  3143. uint32 indexVal;
  3144. PropertyRecord const * propertyRecord = nullptr;
  3145. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, false);
  3146. if (indexType == IndexType_Number)
  3147. {
  3148. return HasItem(object, indexVal);
  3149. }
  3150. else
  3151. {
  3152. Assert(indexType == IndexType_PropertyId);
  3153. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  3154. {
  3155. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
  3156. Assert(indexType == IndexType_PropertyId);
  3157. Assert(propertyRecord != nullptr);
  3158. }
  3159. if (propertyRecord != nullptr)
  3160. {
  3161. return HasProperty(object, propertyRecord->GetPropertyId());
  3162. }
  3163. else
  3164. {
  3165. #if DBG
  3166. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  3167. PropertyRecord const * debugPropertyRecord;
  3168. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  3169. AssertMsg(!JavascriptOperators::HasProperty(object, debugPropertyRecord->GetPropertyId()), "how did this property come? See OS Bug 2727708 if you see this come from the web");
  3170. #endif
  3171. return FALSE;
  3172. }
  3173. }
  3174. }
  3175. #if ENABLE_PROFILE_INFO
  3176. void JavascriptOperators::UpdateNativeArrayProfileInfoToCreateVarArray(Var instance, const bool expectingNativeFloatArray, const bool expectingVarArray)
  3177. {
  3178. Assert(instance);
  3179. Assert(expectingNativeFloatArray ^ expectingVarArray);
  3180. JavascriptNativeArray * nativeArr = JavascriptOperators::TryFromVar<JavascriptNativeArray>(instance);
  3181. if (!nativeArr)
  3182. {
  3183. return;
  3184. }
  3185. ArrayCallSiteInfo *const arrayCallSiteInfo = nativeArr->GetArrayCallSiteInfo();
  3186. if (!arrayCallSiteInfo)
  3187. {
  3188. return;
  3189. }
  3190. if (expectingNativeFloatArray)
  3191. {
  3192. // Profile data is expecting a native float array. Ensure that at the array's creation site, that a native int array
  3193. // is not created, such that the profiled array type would be correct.
  3194. arrayCallSiteInfo->SetIsNotNativeIntArray();
  3195. }
  3196. else
  3197. {
  3198. // Profile data is expecting a var array. Ensure that at the array's creation site, that a native array is not
  3199. // created, such that the profiled array type would be correct.
  3200. Assert(expectingVarArray);
  3201. arrayCallSiteInfo->SetIsNotNativeArray();
  3202. }
  3203. }
  3204. bool JavascriptOperators::SetElementMayHaveImplicitCalls(ScriptContext *const scriptContext)
  3205. {
  3206. return
  3207. scriptContext->optimizationOverrides.GetArraySetElementFastPathVtable() ==
  3208. ScriptContextOptimizationOverrideInfo::InvalidVtable;
  3209. }
  3210. #endif
  3211. RecyclableObject *JavascriptOperators::GetCallableObjectOrThrow(const Var callee, ScriptContext *const scriptContext)
  3212. {
  3213. Assert(callee);
  3214. Assert(scriptContext);
  3215. if (TaggedNumber::Is(callee))
  3216. {
  3217. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction /* TODO-ERROR: get arg name - aFunc */);
  3218. }
  3219. return UnsafeVarTo<RecyclableObject>(callee);
  3220. }
  3221. Var JavascriptOperators::OP_GetElementI_JIT(Var instance, Var index, ScriptContext *scriptContext)
  3222. {
  3223. JIT_HELPER_REENTRANT_HEADER(Op_GetElementI);
  3224. #if ENABLE_NATIVE_CODEGEN
  3225. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  3226. #endif
  3227. return OP_GetElementI(instance, index, scriptContext);
  3228. JIT_HELPER_END(Op_GetElementI);
  3229. }
  3230. Var JavascriptOperators::OP_GetElementI_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  3231. {
  3232. JIT_HELPER_REENTRANT_HEADER(Op_GetElementI_UInt32);
  3233. #if FLOATVAR
  3234. return OP_GetElementI_JIT(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  3235. #else
  3236. char buffer[sizeof(Js::JavascriptNumber)];
  3237. return OP_GetElementI_JIT(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3238. (Js::JavascriptNumber *)buffer), scriptContext);
  3239. #endif
  3240. JIT_HELPER_END(Op_GetElementI_UInt32);
  3241. }
  3242. Var JavascriptOperators::OP_GetElementI_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  3243. {
  3244. JIT_HELPER_REENTRANT_HEADER(Op_GetElementI_Int32);
  3245. #if FLOATVAR
  3246. return OP_GetElementI_JIT(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  3247. #else
  3248. char buffer[sizeof(Js::JavascriptNumber)];
  3249. return OP_GetElementI_JIT(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3250. (Js::JavascriptNumber *)buffer), scriptContext);
  3251. #endif
  3252. JIT_HELPER_END(Op_GetElementI_Int32);
  3253. }
  3254. BOOL JavascriptOperators::GetItemFromArrayPrototype(JavascriptArray * arr, int32 indexInt, Var * result, ScriptContext * scriptContext)
  3255. {
  3256. // try get from Array prototype
  3257. RecyclableObject* prototype = arr->GetPrototype();
  3258. if (JavascriptOperators::GetTypeId(prototype) != TypeIds_Array) //This can be TypeIds_ES5Array (or any other object changed through __proto__).
  3259. {
  3260. return false;
  3261. }
  3262. JavascriptArray* arrayPrototype = UnsafeVarTo<JavascriptArray>(prototype); //Prototype must be Array.prototype (unless changed through __proto__)
  3263. if (arrayPrototype->GetLength() && arrayPrototype->GetItem(arrayPrototype, (uint32)indexInt, result, scriptContext))
  3264. {
  3265. return true;
  3266. }
  3267. prototype = arrayPrototype->GetPrototype(); //Its prototype must be Object.prototype (unless changed through __proto__)
  3268. if (prototype->GetScriptContext()->GetLibrary()->GetObjectPrototype() != prototype)
  3269. {
  3270. return false;
  3271. }
  3272. if (VarTo<DynamicObject>(prototype)->HasNonEmptyObjectArray())
  3273. {
  3274. if (prototype->GetItem(arr, (uint32)indexInt, result, scriptContext))
  3275. {
  3276. return true;
  3277. }
  3278. }
  3279. *result = scriptContext->GetMissingItemResult();
  3280. return true;
  3281. }
  3282. Var JavascriptOperators::GetElementIIntIndex(_In_ Var instance, _In_ Var index, _In_ ScriptContext* scriptContext)
  3283. {
  3284. Assert(TaggedInt::Is(index));
  3285. switch (JavascriptOperators::GetTypeId(instance))
  3286. {
  3287. case TypeIds_Array: //fast path for array
  3288. {
  3289. Var result;
  3290. if (OP_GetElementI_ArrayFastPath(UnsafeVarTo<JavascriptArray>(instance), TaggedInt::ToInt32(index), &result, scriptContext))
  3291. {
  3292. return result;
  3293. }
  3294. break;
  3295. }
  3296. case TypeIds_NativeIntArray:
  3297. {
  3298. #if ENABLE_COPYONACCESS_ARRAY
  3299. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  3300. #endif
  3301. Var result;
  3302. if (OP_GetElementI_ArrayFastPath(UnsafeVarTo<JavascriptNativeIntArray>(instance), TaggedInt::ToInt32(index), &result, scriptContext))
  3303. {
  3304. return result;
  3305. }
  3306. break;
  3307. }
  3308. case TypeIds_NativeFloatArray:
  3309. {
  3310. Var result;
  3311. if (OP_GetElementI_ArrayFastPath(UnsafeVarTo<JavascriptNativeFloatArray>(instance), TaggedInt::ToInt32(index), &result, scriptContext))
  3312. {
  3313. return result;
  3314. }
  3315. break;
  3316. }
  3317. case TypeIds_String: // fast path for string
  3318. {
  3319. charcount_t indexInt = TaggedInt::ToUInt32(index);
  3320. JavascriptString* string = UnsafeVarTo<JavascriptString>(instance);
  3321. Var result;
  3322. if (JavascriptConversion::PropertyQueryFlagsToBoolean(string->JavascriptString::GetItemQuery(instance, indexInt, &result, scriptContext)))
  3323. {
  3324. return result;
  3325. }
  3326. break;
  3327. }
  3328. case TypeIds_Int8Array:
  3329. {
  3330. // The typed array will deal with all possible values for the index
  3331. int32 indexInt = TaggedInt::ToInt32(index);
  3332. if (VirtualTableInfo<Int8VirtualArray>::HasVirtualTable(instance))
  3333. {
  3334. Int8VirtualArray* int8Array = UnsafeVarTo<Int8VirtualArray>(instance);
  3335. if (indexInt >= 0)
  3336. {
  3337. return int8Array->DirectGetItem(indexInt);
  3338. }
  3339. }
  3340. else if (VirtualTableInfo<Int8Array>::HasVirtualTable(instance))
  3341. {
  3342. Int8Array* int8Array = UnsafeVarTo<Int8Array>(instance);
  3343. if (indexInt >= 0)
  3344. {
  3345. return int8Array->DirectGetItem(indexInt);
  3346. }
  3347. }
  3348. break;
  3349. }
  3350. case TypeIds_Uint8Array:
  3351. {
  3352. // The typed array will deal with all possible values for the index
  3353. int32 indexInt = TaggedInt::ToInt32(index);
  3354. if (VirtualTableInfo<Uint8VirtualArray>::HasVirtualTable(instance))
  3355. {
  3356. Uint8VirtualArray* uint8Array = UnsafeVarTo<Uint8VirtualArray>(instance);
  3357. if (indexInt >= 0)
  3358. {
  3359. return uint8Array->DirectGetItem(indexInt);
  3360. }
  3361. }
  3362. else if (VirtualTableInfo<Uint8Array>::HasVirtualTable(instance))
  3363. {
  3364. Uint8Array* uint8Array = UnsafeVarTo<Uint8Array>(instance);
  3365. if (indexInt >= 0)
  3366. {
  3367. return uint8Array->DirectGetItem(indexInt);
  3368. }
  3369. }
  3370. break;
  3371. }
  3372. case TypeIds_Uint8ClampedArray:
  3373. {
  3374. // The typed array will deal with all possible values for the index
  3375. int32 indexInt = TaggedInt::ToInt32(index);
  3376. if (VirtualTableInfo<Uint8ClampedVirtualArray>::HasVirtualTable(instance))
  3377. {
  3378. Uint8ClampedVirtualArray* uint8ClampedArray = UnsafeVarTo<Uint8ClampedVirtualArray>(instance);
  3379. if (indexInt >= 0)
  3380. {
  3381. return uint8ClampedArray->DirectGetItem(indexInt);
  3382. }
  3383. }
  3384. else if (VirtualTableInfo<Uint8ClampedArray>::HasVirtualTable(instance))
  3385. {
  3386. Uint8ClampedArray* uint8ClampedArray = UnsafeVarTo<Uint8ClampedArray>(instance);
  3387. if (indexInt >= 0)
  3388. {
  3389. return uint8ClampedArray->DirectGetItem(indexInt);
  3390. }
  3391. }
  3392. break;
  3393. }
  3394. case TypeIds_Int16Array:
  3395. {
  3396. // The type array will deal with all possible values for the index
  3397. int32 indexInt = TaggedInt::ToInt32(index);
  3398. if (VirtualTableInfo<Int16VirtualArray>::HasVirtualTable(instance))
  3399. {
  3400. Int16VirtualArray* int16Array = UnsafeVarTo<Int16VirtualArray>(instance);
  3401. if (indexInt >= 0)
  3402. {
  3403. return int16Array->DirectGetItem(indexInt);
  3404. }
  3405. }
  3406. else if (VirtualTableInfo<Int16Array>::HasVirtualTable(instance))
  3407. {
  3408. Int16Array* int16Array = UnsafeVarTo<Int16Array>(instance);
  3409. if (indexInt >= 0)
  3410. {
  3411. return int16Array->DirectGetItem(indexInt);
  3412. }
  3413. }
  3414. break;
  3415. }
  3416. case TypeIds_Uint16Array:
  3417. {
  3418. // The type array will deal with all possible values for the index
  3419. int32 indexInt = TaggedInt::ToInt32(index);
  3420. if (VirtualTableInfo<Uint16VirtualArray>::HasVirtualTable(instance))
  3421. {
  3422. Uint16VirtualArray* uint16Array = UnsafeVarTo<Uint16VirtualArray>(instance);
  3423. if (indexInt >= 0)
  3424. {
  3425. return uint16Array->DirectGetItem(indexInt);
  3426. }
  3427. }
  3428. else if (VirtualTableInfo<Uint16Array>::HasVirtualTable(instance))
  3429. {
  3430. Uint16Array* uint16Array = UnsafeVarTo<Uint16Array>(instance);
  3431. if (indexInt >= 0)
  3432. {
  3433. return uint16Array->DirectGetItem(indexInt);
  3434. }
  3435. }
  3436. break;
  3437. }
  3438. case TypeIds_Int32Array:
  3439. {
  3440. // The type array will deal with all possible values for the index
  3441. int32 indexInt = TaggedInt::ToInt32(index);
  3442. if (VirtualTableInfo<Int32VirtualArray>::HasVirtualTable(instance))
  3443. {
  3444. Int32VirtualArray* int32Array = UnsafeVarTo<Int32VirtualArray>(instance);
  3445. if (indexInt >= 0)
  3446. {
  3447. return int32Array->DirectGetItem(indexInt);
  3448. }
  3449. }
  3450. else if (VirtualTableInfo<Int32Array>::HasVirtualTable(instance))
  3451. {
  3452. Int32Array* int32Array = UnsafeVarTo<Int32Array>(instance);
  3453. if (indexInt >= 0)
  3454. {
  3455. return int32Array->DirectGetItem(indexInt);
  3456. }
  3457. }
  3458. break;
  3459. }
  3460. case TypeIds_Uint32Array:
  3461. {
  3462. // The type array will deal with all possible values for the index
  3463. int32 indexInt = TaggedInt::ToInt32(index);
  3464. if (VirtualTableInfo<Uint32VirtualArray>::HasVirtualTable(instance))
  3465. {
  3466. Uint32VirtualArray* uint32Array = UnsafeVarTo<Uint32VirtualArray>(instance);
  3467. if (indexInt >= 0)
  3468. {
  3469. return uint32Array->DirectGetItem(indexInt);
  3470. }
  3471. }
  3472. else if (VirtualTableInfo<Uint32Array>::HasVirtualTable(instance))
  3473. {
  3474. Uint32Array* uint32Array = UnsafeVarTo<Uint32Array>(instance);
  3475. if (indexInt >= 0)
  3476. {
  3477. return uint32Array->DirectGetItem(indexInt);
  3478. }
  3479. }
  3480. break;
  3481. }
  3482. case TypeIds_Float32Array:
  3483. {
  3484. // The type array will deal with all possible values for the index
  3485. int32 indexInt = TaggedInt::ToInt32(index);
  3486. if (VirtualTableInfo<Float32VirtualArray>::HasVirtualTable(instance))
  3487. {
  3488. Float32VirtualArray* float32Array = UnsafeVarTo<Float32VirtualArray>(instance);
  3489. if (indexInt >= 0)
  3490. {
  3491. return float32Array->DirectGetItem(indexInt);
  3492. }
  3493. }
  3494. else if (VirtualTableInfo<Float32Array>::HasVirtualTable(instance))
  3495. {
  3496. Float32Array* float32Array = UnsafeVarTo<Float32Array>(instance);
  3497. if (indexInt >= 0)
  3498. {
  3499. return float32Array->DirectGetItem(indexInt);
  3500. }
  3501. }
  3502. break;
  3503. }
  3504. case TypeIds_Float64Array:
  3505. {
  3506. // The type array will deal with all possible values for the index
  3507. int32 indexInt = TaggedInt::ToInt32(index);
  3508. if (VirtualTableInfo<Float64VirtualArray>::HasVirtualTable(instance))
  3509. {
  3510. Float64VirtualArray* float64Array = UnsafeVarTo<Float64VirtualArray>(instance);
  3511. if (indexInt >= 0)
  3512. {
  3513. return float64Array->DirectGetItem(indexInt);
  3514. }
  3515. }
  3516. else if (VirtualTableInfo<Float64Array>::HasVirtualTable(instance))
  3517. {
  3518. Float64Array* float64Array = UnsafeVarTo<Float64Array>(instance);
  3519. if (indexInt >= 0)
  3520. {
  3521. return float64Array->DirectGetItem(indexInt);
  3522. }
  3523. }
  3524. break;
  3525. }
  3526. default:
  3527. break;
  3528. }
  3529. return JavascriptOperators::GetElementIHelper(instance, index, instance, scriptContext);
  3530. }
  3531. template <typename T>
  3532. BOOL JavascriptOperators::OP_GetElementI_ArrayFastPath(T * arr, int indexInt, Var * result, ScriptContext * scriptContext)
  3533. {
  3534. #if ENABLE_COPYONACCESS_ARRAY
  3535. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(arr);
  3536. #endif
  3537. if (indexInt >= 0)
  3538. {
  3539. if (!CrossSite::IsCrossSiteObjectTyped(arr))
  3540. {
  3541. if (arr->T::DirectGetVarItemAt((uint32)indexInt, result, scriptContext))
  3542. {
  3543. return true;
  3544. }
  3545. }
  3546. else
  3547. {
  3548. if (arr->GetItem(arr, (uint32)indexInt, result, scriptContext))
  3549. {
  3550. return true;
  3551. }
  3552. }
  3553. return GetItemFromArrayPrototype(arr, indexInt, result, scriptContext);
  3554. }
  3555. return false;
  3556. }
  3557. Var JavascriptOperators::OP_GetElementI(Var instance, Var index, ScriptContext* scriptContext)
  3558. {
  3559. #ifdef ENABLE_SPECTRE_RUNTIME_MITIGATIONS
  3560. instance = BreakSpeculation(instance);
  3561. #endif
  3562. if (TaggedInt::Is(index))
  3563. {
  3564. return GetElementIIntIndex(instance, index, scriptContext);
  3565. }
  3566. if (JavascriptNumber::Is_NoTaggedIntCheck(index))
  3567. {
  3568. uint32 uint32Index = JavascriptConversion::ToUInt32(index, scriptContext);
  3569. if ((double)uint32Index == JavascriptNumber::GetValue(index) && !TaggedInt::IsOverflow(uint32Index))
  3570. {
  3571. index = TaggedInt::ToVarUnchecked(uint32Index);
  3572. return GetElementIIntIndex(instance, index, scriptContext);
  3573. }
  3574. }
  3575. else if (VarIs<RecyclableObject>(instance))
  3576. {
  3577. RecyclableObject* cacheOwner;
  3578. PropertyRecordUsageCache* propertyRecordUsageCache;
  3579. if (GetPropertyRecordUsageCache(index, scriptContext, &propertyRecordUsageCache, &cacheOwner))
  3580. {
  3581. return GetElementIWithCache<false /* ReturnOperationInfo */>(instance, cacheOwner, propertyRecordUsageCache, scriptContext, nullptr);
  3582. }
  3583. }
  3584. return JavascriptOperators::GetElementIHelper(instance, index, instance, scriptContext);
  3585. }
  3586. _Success_(return) bool JavascriptOperators::GetPropertyRecordUsageCache(Var index, ScriptContext* scriptContext, _Outptr_ PropertyRecordUsageCache** propertyRecordUsageCache, _Outptr_ RecyclableObject** cacheOwner)
  3587. {
  3588. JavascriptString* string = JavascriptOperators::TryFromVar<JavascriptString>(index);
  3589. if (string)
  3590. {
  3591. PropertyString * propertyString = nullptr;
  3592. if (VirtualTableInfo<Js::PropertyString>::HasVirtualTable(string))
  3593. {
  3594. propertyString = (PropertyString*)string;
  3595. }
  3596. else if (VirtualTableInfo<Js::LiteralStringWithPropertyStringPtr>::HasVirtualTable(string))
  3597. {
  3598. LiteralStringWithPropertyStringPtr * strWithPtr = (LiteralStringWithPropertyStringPtr *)string;
  3599. if (!strWithPtr->HasPropertyRecord())
  3600. {
  3601. PropertyRecord const * propertyRecord;
  3602. strWithPtr->GetPropertyRecord(&propertyRecord); // lookup-cache propertyRecord
  3603. }
  3604. else
  3605. {
  3606. propertyString = strWithPtr->GetOrAddPropertyString();
  3607. // this is the second time this property string is used
  3608. // we already had created the propertyRecord..
  3609. // now create the propertyString!
  3610. }
  3611. }
  3612. if (propertyString != nullptr)
  3613. {
  3614. *propertyRecordUsageCache = propertyString->GetPropertyRecordUsageCache();
  3615. *cacheOwner = propertyString;
  3616. return true;
  3617. }
  3618. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  3619. if (PHASE_TRACE1(PropertyCachePhase))
  3620. {
  3621. Output::Print(_u("PropertyCache: GetElem No property string for '%s'\n"), string->GetString());
  3622. }
  3623. #endif
  3624. #if DBG_DUMP
  3625. scriptContext->forinNoCache++;
  3626. #endif
  3627. }
  3628. JavascriptSymbol* symbol = JavascriptOperators::TryFromVar<JavascriptSymbol>(index);
  3629. if (symbol)
  3630. {
  3631. *propertyRecordUsageCache = symbol->GetPropertyRecordUsageCache();
  3632. *cacheOwner = symbol;
  3633. return true;
  3634. }
  3635. return false;
  3636. }
  3637. bool JavascriptOperators::SetElementIOnTaggedNumber(
  3638. _In_ Var receiver,
  3639. _In_ RecyclableObject* object,
  3640. _In_ Var index,
  3641. _In_ Var value,
  3642. _In_ ScriptContext* requestContext,
  3643. _In_ PropertyOperationFlags propertyOperationFlags)
  3644. {
  3645. Assert(TaggedNumber::Is(receiver));
  3646. uint32 indexVal = 0;
  3647. PropertyRecord const * propertyRecord = nullptr;
  3648. IndexType indexType = GetIndexType(index, requestContext, &indexVal, &propertyRecord, true);
  3649. if (indexType == IndexType_Number)
  3650. {
  3651. return JavascriptOperators::SetItemOnTaggedNumber(receiver, object, indexVal, value, requestContext, propertyOperationFlags);
  3652. }
  3653. else
  3654. {
  3655. return JavascriptOperators::SetPropertyOnTaggedNumber(receiver, object, propertyRecord->GetPropertyId(), value, requestContext, propertyOperationFlags);
  3656. }
  3657. }
  3658. template <bool ReturnOperationInfo>
  3659. bool JavascriptOperators::SetElementIWithCache(
  3660. _In_ Var receiver,
  3661. _In_ RecyclableObject* object,
  3662. _In_ RecyclableObject* index,
  3663. _In_ Var value,
  3664. _In_ PropertyRecordUsageCache* propertyRecordUsageCache,
  3665. _In_ ScriptContext* scriptContext,
  3666. _In_ PropertyOperationFlags flags,
  3667. _Inout_opt_ PropertyCacheOperationInfo* operationInfo)
  3668. {
  3669. if (TaggedNumber::Is(receiver))
  3670. {
  3671. return JavascriptOperators::SetElementIOnTaggedNumber(receiver, object, index, value, scriptContext, flags);
  3672. }
  3673. PropertyRecord const * propertyRecord = propertyRecordUsageCache->GetPropertyRecord();
  3674. if (propertyRecord->IsNumeric())
  3675. {
  3676. return JavascriptOperators::SetItem(receiver, object, propertyRecord->GetNumericValue(), value, scriptContext, flags);
  3677. }
  3678. PropertyValueInfo info;
  3679. if (receiver == object)
  3680. {
  3681. if (propertyRecordUsageCache->TrySetPropertyFromCache<ReturnOperationInfo>(object, value, scriptContext, flags, &info, index, operationInfo))
  3682. {
  3683. return true;
  3684. }
  3685. }
  3686. PropertyId propId = propertyRecord->GetPropertyId();
  3687. if (propId == PropertyIds::NaN || propId == PropertyIds::Infinity)
  3688. {
  3689. // As we no longer convert o[x] into o.x for NaN and Infinity, we need to follow SetProperty convention for these,
  3690. // which would check for read-only properties, strict mode, etc.
  3691. // Note that "-Infinity" does not qualify as property name, so we don't have to take care of it.
  3692. return JavascriptOperators::SetProperty(receiver, object, propId, value, scriptContext, flags);
  3693. }
  3694. return JavascriptOperators::SetPropertyWPCache(receiver, object, propId, value, scriptContext, flags, &info);
  3695. }
  3696. template bool JavascriptOperators::SetElementIWithCache<false>(Var receiver, RecyclableObject* object, RecyclableObject* index, Var value, PropertyRecordUsageCache* propertyRecordUsageCache, ScriptContext* scriptContext, PropertyOperationFlags flags, PropertyCacheOperationInfo* operationInfo);
  3697. template bool JavascriptOperators::SetElementIWithCache<true>(Var receiver, RecyclableObject* object, RecyclableObject* index, Var value, PropertyRecordUsageCache* propertyRecordUsageCache, ScriptContext* scriptContext, PropertyOperationFlags flags, PropertyCacheOperationInfo* operationInfo);
  3698. template <bool ReturnOperationInfo>
  3699. Var JavascriptOperators::GetElementIWithCache(
  3700. _In_ Var instance,
  3701. _In_ RecyclableObject* index,
  3702. _In_ PropertyRecordUsageCache* propertyRecordUsageCache,
  3703. _In_ ScriptContext* scriptContext,
  3704. _Inout_opt_ PropertyCacheOperationInfo* operationInfo)
  3705. {
  3706. RecyclableObject* object = nullptr;
  3707. if (!JavascriptOperators::GetPropertyObjectForGetElementI(instance, index, scriptContext, &object))
  3708. {
  3709. return scriptContext->GetLibrary()->GetUndefined();
  3710. }
  3711. PropertyRecord const * propertyRecord = propertyRecordUsageCache->GetPropertyRecord();
  3712. Var value;
  3713. if (propertyRecord->IsNumeric())
  3714. {
  3715. if (JavascriptOperators::GetItem(instance, object, propertyRecord->GetNumericValue(), &value, scriptContext))
  3716. {
  3717. return value;
  3718. }
  3719. }
  3720. else
  3721. {
  3722. PropertyValueInfo info;
  3723. if (propertyRecordUsageCache->TryGetPropertyFromCache<false /* OwnPropertyOnly */, false /* OutputExistence */, ReturnOperationInfo>(instance, object, &value, scriptContext, &info, index, operationInfo))
  3724. {
  3725. return value;
  3726. }
  3727. if (JavascriptOperators::GetPropertyWPCache<false /* OutputExistence */>(instance, object, propertyRecord->GetPropertyId(), &value, scriptContext, &info))
  3728. {
  3729. return value;
  3730. }
  3731. }
  3732. return scriptContext->GetLibrary()->GetUndefined();
  3733. }
  3734. template Var JavascriptOperators::GetElementIWithCache<false>(Var instance, RecyclableObject* index, PropertyRecordUsageCache* propertyRecordUsageCache, ScriptContext* scriptContext, PropertyCacheOperationInfo* operationInfo);
  3735. template Var JavascriptOperators::GetElementIWithCache<true>(Var instance, RecyclableObject* index, PropertyRecordUsageCache* propertyRecordUsageCache, ScriptContext* scriptContext, PropertyCacheOperationInfo* operationInfo);
  3736. Var JavascriptOperators::GetElementIHelper(Var instance, Var index, Var receiver, ScriptContext* scriptContext)
  3737. {
  3738. RecyclableObject* object = nullptr;
  3739. if (!JavascriptOperators::GetPropertyObjectForGetElementI(instance, index, scriptContext, &object))
  3740. {
  3741. return scriptContext->GetLibrary()->GetUndefined();
  3742. }
  3743. uint32 indexVal;
  3744. PropertyRecord const * propertyRecord = nullptr;
  3745. JavascriptString * propertyNameString = nullptr;
  3746. Var value = nullptr;
  3747. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, &propertyNameString, false, true);
  3748. if (indexType == IndexType_Number)
  3749. {
  3750. if (JavascriptOperators::GetItem(receiver, object, indexVal, &value, scriptContext))
  3751. {
  3752. return value;
  3753. }
  3754. }
  3755. else if (indexType == IndexType_JavascriptString)
  3756. {
  3757. PropertyValueInfo info;
  3758. if (JavascriptOperators::GetPropertyWPCache<false /* OutputExistence */>(receiver, object, propertyNameString, &value, scriptContext, &info))
  3759. {
  3760. return value;
  3761. }
  3762. }
  3763. else
  3764. {
  3765. Assert(indexType == IndexType_PropertyId);
  3766. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  3767. {
  3768. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, &propertyNameString, true, true);
  3769. Assert(indexType == IndexType_PropertyId);
  3770. Assert(propertyRecord != nullptr);
  3771. }
  3772. if (propertyRecord != nullptr)
  3773. {
  3774. PropertyValueInfo info;
  3775. if (JavascriptOperators::GetPropertyWPCache<false /* OutputExistence */>(receiver, object, propertyRecord->GetPropertyId(), &value, scriptContext, &info))
  3776. {
  3777. return value;
  3778. }
  3779. }
  3780. #if DBG
  3781. else
  3782. {
  3783. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  3784. PropertyRecord const * debugPropertyRecord;
  3785. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  3786. 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");
  3787. }
  3788. #endif
  3789. }
  3790. return scriptContext->GetMissingItemResult();
  3791. }
  3792. int32 JavascriptOperators::OP_GetNativeIntElementI(Var instance, Var index)
  3793. {
  3794. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeIntElementI);
  3795. #if ENABLE_COPYONACCESS_ARRAY
  3796. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  3797. #endif
  3798. if (TaggedInt::Is(index))
  3799. {
  3800. int32 indexInt = TaggedInt::ToInt32(index);
  3801. if (indexInt < 0)
  3802. {
  3803. return JavascriptNativeIntArray::MissingItem;
  3804. }
  3805. JavascriptArray * arr = VarTo<JavascriptArray>(instance);
  3806. int32 result;
  3807. if (arr->DirectGetItemAt((uint32)indexInt, &result))
  3808. {
  3809. return result;
  3810. }
  3811. }
  3812. else if (JavascriptNumber::Is_NoTaggedIntCheck(index))
  3813. {
  3814. int32 indexInt;
  3815. bool isInt32;
  3816. double dIndex = JavascriptNumber::GetValue(index);
  3817. if (JavascriptNumber::TryGetInt32OrUInt32Value(dIndex, &indexInt, &isInt32))
  3818. {
  3819. if (isInt32 && indexInt < 0)
  3820. {
  3821. return JavascriptNativeIntArray::MissingItem;
  3822. }
  3823. JavascriptArray * arr = VarTo<JavascriptArray>(instance);
  3824. int32 result;
  3825. if (arr->DirectGetItemAt((uint32)indexInt, &result))
  3826. {
  3827. return result;
  3828. }
  3829. }
  3830. }
  3831. else
  3832. {
  3833. AssertMsg(false, "Non-numerical index in this helper?");
  3834. }
  3835. return JavascriptNativeIntArray::MissingItem;
  3836. JIT_HELPER_END(Op_GetNativeIntElementI);
  3837. }
  3838. int32 JavascriptOperators::OP_GetNativeIntElementI_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  3839. {
  3840. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeIntElementI_UInt32);
  3841. JIT_HELPER_SAME_ATTRIBUTES(Op_GetNativeIntElementI_UInt32, Op_GetNativeIntElementI);
  3842. #if FLOATVAR
  3843. return OP_GetNativeIntElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext));
  3844. #else
  3845. char buffer[sizeof(Js::JavascriptNumber)];
  3846. return OP_GetNativeIntElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3847. (Js::JavascriptNumber *)buffer));
  3848. #endif
  3849. JIT_HELPER_END(Op_GetNativeIntElementI_UInt32);
  3850. }
  3851. int32 JavascriptOperators::OP_GetNativeIntElementI_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  3852. {
  3853. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeIntElementI_Int32);
  3854. JIT_HELPER_SAME_ATTRIBUTES(Op_GetNativeIntElementI_Int32, 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_Int32);
  3863. }
  3864. double JavascriptOperators::OP_GetNativeFloatElementI(Var instance, Var index)
  3865. {
  3866. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeFloatElementI);
  3867. double result = 0;
  3868. if (TaggedInt::Is(index))
  3869. {
  3870. int32 indexInt = TaggedInt::ToInt32(index);
  3871. if (indexInt < 0)
  3872. {
  3873. result = JavascriptNativeFloatArray::MissingItem;
  3874. }
  3875. else
  3876. {
  3877. JavascriptArray * arr = VarTo<JavascriptArray>(instance);
  3878. if (!arr->DirectGetItemAt((uint32)indexInt, &result))
  3879. {
  3880. result = JavascriptNativeFloatArray::MissingItem;
  3881. }
  3882. }
  3883. }
  3884. else if (JavascriptNumber::Is_NoTaggedIntCheck(index))
  3885. {
  3886. int32 indexInt;
  3887. bool isInt32;
  3888. double dIndex = JavascriptNumber::GetValue(index);
  3889. if (JavascriptNumber::TryGetInt32OrUInt32Value(dIndex, &indexInt, &isInt32))
  3890. {
  3891. if (isInt32 && indexInt < 0)
  3892. {
  3893. result = JavascriptNativeFloatArray::MissingItem;
  3894. }
  3895. else
  3896. {
  3897. JavascriptArray * arr = VarTo<JavascriptArray>(instance);
  3898. if (!arr->DirectGetItemAt((uint32)indexInt, &result))
  3899. {
  3900. result = JavascriptNativeFloatArray::MissingItem;
  3901. }
  3902. }
  3903. }
  3904. }
  3905. else
  3906. {
  3907. AssertMsg(false, "Non-numerical index in this helper?");
  3908. }
  3909. return result;
  3910. JIT_HELPER_END(Op_GetNativeFloatElementI);
  3911. }
  3912. double JavascriptOperators::OP_GetNativeFloatElementI_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  3913. {
  3914. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeFloatElementI_UInt32);
  3915. JIT_HELPER_SAME_ATTRIBUTES(Op_GetNativeFloatElementI_UInt32, Op_GetNativeFloatElementI);
  3916. #if FLOATVAR
  3917. return OP_GetNativeFloatElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext));
  3918. #else
  3919. char buffer[sizeof(Js::JavascriptNumber)];
  3920. return OP_GetNativeFloatElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3921. (Js::JavascriptNumber *)buffer));
  3922. #endif
  3923. JIT_HELPER_END(Op_GetNativeFloatElementI_UInt32);
  3924. }
  3925. double JavascriptOperators::OP_GetNativeFloatElementI_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  3926. {
  3927. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeFloatElementI_Int32);
  3928. JIT_HELPER_SAME_ATTRIBUTES(Op_GetNativeFloatElementI_Int32, 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_Int32);
  3937. }
  3938. Var JavascriptOperators::OP_GetMethodElement_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  3939. {
  3940. JIT_HELPER_REENTRANT_HEADER(Op_GetMethodElement_UInt32);
  3941. JIT_HELPER_SAME_ATTRIBUTES(Op_GetMethodElement_UInt32, Op_GetMethodElement);
  3942. #if FLOATVAR
  3943. return OP_GetMethodElement(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  3944. #else
  3945. char buffer[sizeof(Js::JavascriptNumber)];
  3946. return OP_GetMethodElement(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3947. (Js::JavascriptNumber *)buffer), scriptContext);
  3948. #endif
  3949. JIT_HELPER_END(Op_GetMethodElement_UInt32);
  3950. }
  3951. Var JavascriptOperators::OP_GetMethodElement_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  3952. {
  3953. JIT_HELPER_REENTRANT_HEADER(Op_GetMethodElement_Int32);
  3954. JIT_HELPER_SAME_ATTRIBUTES(Op_GetMethodElement_Int32, Op_GetMethodElement);
  3955. #if FLOATVAR
  3956. return OP_GetElementI(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_Int32);
  3963. }
  3964. Var JavascriptOperators::OP_GetMethodElement(Var instance, Var index, ScriptContext* scriptContext)
  3965. {
  3966. JIT_HELPER_REENTRANT_HEADER(Op_GetMethodElement);
  3967. RecyclableObject* object = nullptr;
  3968. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  3969. {
  3970. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined, GetPropertyDisplayNameForError(index, scriptContext));
  3971. }
  3972. ThreadContext* threadContext = scriptContext->GetThreadContext();
  3973. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  3974. threadContext->ClearImplicitCallFlags();
  3975. uint32 indexVal;
  3976. PropertyRecord const * propertyRecord = nullptr;
  3977. Var value = NULL;
  3978. BOOL hasProperty = FALSE;
  3979. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, false);
  3980. if (indexType == IndexType_Number)
  3981. {
  3982. hasProperty = JavascriptOperators::GetItemReference(instance, object, indexVal, &value, scriptContext);
  3983. }
  3984. else
  3985. {
  3986. Assert(indexType == IndexType_PropertyId);
  3987. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  3988. {
  3989. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
  3990. Assert(indexType == IndexType_PropertyId);
  3991. Assert(propertyRecord != nullptr);
  3992. }
  3993. if (propertyRecord != nullptr)
  3994. {
  3995. hasProperty = JavascriptOperators::GetPropertyReference(instance, object, propertyRecord->GetPropertyId(), &value, scriptContext, NULL);
  3996. }
  3997. #if DBG
  3998. else
  3999. {
  4000. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  4001. PropertyRecord const * debugPropertyRecord;
  4002. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  4003. AssertMsg(!JavascriptOperators::GetPropertyReference(instance, object, debugPropertyRecord->GetPropertyId(), &value, scriptContext, NULL),
  4004. "how did this property come? See OS Bug 2727708 if you see this come from the web");
  4005. }
  4006. #endif
  4007. }
  4008. if (!hasProperty)
  4009. {
  4010. JavascriptString* varName = nullptr;
  4011. if (indexType == IndexType_PropertyId && propertyRecord != nullptr && propertyRecord->IsSymbol())
  4012. {
  4013. varName = JavascriptSymbol::ToString(propertyRecord, scriptContext);
  4014. }
  4015. else
  4016. {
  4017. varName = JavascriptConversion::ToString(index, scriptContext);
  4018. }
  4019. // 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
  4020. // first (#3). Postpone throwing error to invoke time.
  4021. value = ThrowErrorObject::CreateThrowTypeErrorObject(scriptContext, VBSERR_OLENoPropOrMethod, varName);
  4022. }
  4023. else if(!JavascriptConversion::IsCallable(value))
  4024. {
  4025. // 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
  4026. // first (#3). Postpone throwing error to invoke time.
  4027. JavascriptString* varName = JavascriptConversion::ToString(index, scriptContext);
  4028. value = ThrowErrorObject::CreateThrowTypeErrorObject(scriptContext, JSERR_Property_NeedFunction, varName);
  4029. }
  4030. threadContext->CheckAndResetImplicitCallAccessorFlag();
  4031. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  4032. return value;
  4033. JIT_HELPER_END(Op_GetMethodElement);
  4034. }
  4035. BOOL JavascriptOperators::OP_SetElementI_UInt32(Var instance, uint32 index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  4036. {
  4037. JIT_HELPER_REENTRANT_HEADER(Op_SetElementI_UInt32);
  4038. JIT_HELPER_SAME_ATTRIBUTES(Op_SetElementI_UInt32, Op_SetElementI);
  4039. #if FLOATVAR
  4040. return OP_SetElementI_JIT(instance, Js::JavascriptNumber::ToVar(index, scriptContext), value, scriptContext, flags);
  4041. #else
  4042. char buffer[sizeof(Js::JavascriptNumber)];
  4043. return OP_SetElementI_JIT(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  4044. (Js::JavascriptNumber *)buffer), value, scriptContext, flags);
  4045. #endif
  4046. JIT_HELPER_END(Op_SetElementI_UInt32);
  4047. }
  4048. BOOL JavascriptOperators::OP_SetElementI_Int32(Var instance, int32 index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  4049. {
  4050. JIT_HELPER_REENTRANT_HEADER(Op_SetElementI_Int32);
  4051. JIT_HELPER_SAME_ATTRIBUTES(Op_SetElementI_Int32, 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_Int32);
  4060. }
  4061. BOOL JavascriptOperators::OP_SetElementI_JIT(Var instance, Var index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  4062. {
  4063. JIT_HELPER_REENTRANT_HEADER(Op_SetElementI);
  4064. if (TaggedNumber::Is(instance))
  4065. {
  4066. return OP_SetElementI(instance, index, value, scriptContext, flags);
  4067. }
  4068. INT_PTR vt = VirtualTableInfoBase::GetVirtualTable(instance);
  4069. OP_SetElementI(instance, index, value, scriptContext, flags);
  4070. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4071. JIT_HELPER_END(Op_SetElementI);
  4072. }
  4073. BOOL JavascriptOperators::OP_SetElementI(Var instance, Var index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  4074. {
  4075. #if ENABLE_COPYONACCESS_ARRAY
  4076. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  4077. #endif
  4078. TypeId instanceType = JavascriptOperators::GetTypeId(instance);
  4079. bool isTypedArray = (instanceType >= TypeIds_Int8Array && instanceType <= TypeIds_Float64Array);
  4080. if (isTypedArray)
  4081. {
  4082. if (TaggedInt::Is(index) || JavascriptNumber::Is_NoTaggedIntCheck(index) || VarIs<JavascriptString>(index))
  4083. {
  4084. BOOL returnValue = FALSE;
  4085. bool isNumericIndex = false;
  4086. // CrossSite types will go down the slow path.
  4087. switch (instanceType)
  4088. {
  4089. case TypeIds_Int8Array:
  4090. {
  4091. // The typed array will deal with all possible values for the index
  4092. if (VirtualTableInfo<Int8VirtualArray>::HasVirtualTable(instance))
  4093. {
  4094. Int8VirtualArray* int8Array = UnsafeVarTo<Int8VirtualArray>(instance);
  4095. returnValue = int8Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4096. }
  4097. else if( VirtualTableInfo<Int8Array>::HasVirtualTable(instance))
  4098. {
  4099. Int8Array* int8Array = UnsafeVarTo<Int8Array>(instance);
  4100. returnValue = int8Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4101. }
  4102. break;
  4103. }
  4104. case TypeIds_Uint8Array:
  4105. {
  4106. // The typed array will deal with all possible values for the index
  4107. if (VirtualTableInfo<Uint8VirtualArray>::HasVirtualTable(instance))
  4108. {
  4109. Uint8VirtualArray* uint8Array = UnsafeVarTo<Uint8VirtualArray>(instance);
  4110. returnValue = uint8Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4111. }
  4112. else if (VirtualTableInfo<Uint8Array>::HasVirtualTable(instance))
  4113. {
  4114. Uint8Array* uint8Array = UnsafeVarTo<Uint8Array>(instance);
  4115. returnValue = uint8Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4116. }
  4117. break;
  4118. }
  4119. case TypeIds_Uint8ClampedArray:
  4120. {
  4121. // The typed array will deal with all possible values for the index
  4122. if (VirtualTableInfo<Uint8ClampedVirtualArray>::HasVirtualTable(instance))
  4123. {
  4124. Uint8ClampedVirtualArray* uint8ClampedArray = UnsafeVarTo<Uint8ClampedVirtualArray>(instance);
  4125. returnValue = uint8ClampedArray->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4126. }
  4127. else if(VirtualTableInfo<Uint8ClampedArray>::HasVirtualTable(instance))
  4128. {
  4129. Uint8ClampedArray* uint8ClampedArray = UnsafeVarTo<Uint8ClampedArray>(instance);
  4130. returnValue = uint8ClampedArray->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4131. }
  4132. break;
  4133. }
  4134. case TypeIds_Int16Array:
  4135. {
  4136. // The type array will deal with all possible values for the index
  4137. if (VirtualTableInfo<Int16VirtualArray>::HasVirtualTable(instance))
  4138. {
  4139. Int16VirtualArray* int16Array = UnsafeVarTo<Int16VirtualArray>(instance);
  4140. returnValue = int16Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4141. }
  4142. else if (VirtualTableInfo<Int16Array>::HasVirtualTable(instance))
  4143. {
  4144. Int16Array* int16Array = UnsafeVarTo<Int16Array>(instance);
  4145. returnValue = int16Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4146. }
  4147. break;
  4148. }
  4149. case TypeIds_Uint16Array:
  4150. {
  4151. // The type array will deal with all possible values for the index
  4152. if (VirtualTableInfo<Uint16VirtualArray>::HasVirtualTable(instance))
  4153. {
  4154. Uint16VirtualArray* uint16Array = UnsafeVarTo<Uint16VirtualArray>(instance);
  4155. returnValue = uint16Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4156. }
  4157. else if (VirtualTableInfo<Uint16Array>::HasVirtualTable(instance))
  4158. {
  4159. Uint16Array* uint16Array = UnsafeVarTo<Uint16Array>(instance);
  4160. returnValue = uint16Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4161. }
  4162. break;
  4163. }
  4164. case TypeIds_Int32Array:
  4165. {
  4166. // The type array will deal with all possible values for the index
  4167. if (VirtualTableInfo<Int32VirtualArray>::HasVirtualTable(instance))
  4168. {
  4169. Int32VirtualArray* int32Array = UnsafeVarTo<Int32VirtualArray>(instance);
  4170. returnValue = int32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4171. }
  4172. else if(VirtualTableInfo<Int32Array>::HasVirtualTable(instance))
  4173. {
  4174. Int32Array* int32Array = UnsafeVarTo<Int32Array>(instance);
  4175. returnValue = int32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4176. }
  4177. break;
  4178. }
  4179. case TypeIds_Uint32Array:
  4180. {
  4181. // The type array will deal with all possible values for the index
  4182. if (VirtualTableInfo<Uint32VirtualArray>::HasVirtualTable(instance))
  4183. {
  4184. Uint32VirtualArray* uint32Array = UnsafeVarTo<Uint32VirtualArray>(instance);
  4185. returnValue = uint32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4186. }
  4187. else if (VirtualTableInfo<Uint32Array>::HasVirtualTable(instance))
  4188. {
  4189. Uint32Array* uint32Array = UnsafeVarTo<Uint32Array>(instance);
  4190. returnValue = uint32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4191. }
  4192. break;
  4193. }
  4194. case TypeIds_Float32Array:
  4195. {
  4196. // The type array will deal with all possible values for the index
  4197. if (VirtualTableInfo<Float32VirtualArray>::HasVirtualTable(instance))
  4198. {
  4199. Float32VirtualArray* float32Array = UnsafeVarTo<Float32VirtualArray>(instance);
  4200. returnValue = float32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4201. }
  4202. else if (VirtualTableInfo<Float32Array>::HasVirtualTable(instance))
  4203. {
  4204. Float32Array* float32Array = UnsafeVarTo<Float32Array>(instance);
  4205. returnValue = float32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4206. }
  4207. break;
  4208. }
  4209. case TypeIds_Float64Array:
  4210. {
  4211. // The type array will deal with all possible values for the index
  4212. if (VirtualTableInfo<Float64VirtualArray>::HasVirtualTable(instance))
  4213. {
  4214. Float64VirtualArray* float64Array = UnsafeVarTo<Float64VirtualArray>(instance);
  4215. returnValue = float64Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4216. }
  4217. else if (VirtualTableInfo<Float64Array>::HasVirtualTable(instance))
  4218. {
  4219. Float64Array* float64Array = UnsafeVarTo<Float64Array>(instance);
  4220. returnValue = float64Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4221. }
  4222. break;
  4223. }
  4224. }
  4225. // if this was numeric index, return operation status else
  4226. // Return the result of calling the default ordinary object [[Set]] internal method (9.1.8) on O passing P, V, and Receiver as arguments.
  4227. if (isNumericIndex)
  4228. return returnValue;
  4229. }
  4230. }
  4231. else
  4232. {
  4233. if (TaggedInt::Is(index))
  4234. {
  4235. TaggedIntIndex:
  4236. switch (instanceType)
  4237. {
  4238. case TypeIds_NativeIntArray:
  4239. case TypeIds_NativeFloatArray:
  4240. case TypeIds_Array: // fast path for array
  4241. {
  4242. int indexInt = TaggedInt::ToInt32(index);
  4243. if (indexInt >= 0 && scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4244. {
  4245. UnsafeVarTo<JavascriptArray>(instance)->SetItem((uint32)indexInt, value, flags);
  4246. return TRUE;
  4247. }
  4248. break;
  4249. }
  4250. }
  4251. }
  4252. else if (JavascriptNumber::Is_NoTaggedIntCheck(index))
  4253. {
  4254. double dIndexValue = JavascriptNumber::GetValue(index);
  4255. uint32 uint32Index = JavascriptConversion::ToUInt32(index, scriptContext);
  4256. if ((double)uint32Index == dIndexValue && !TaggedInt::IsOverflow(uint32Index))
  4257. {
  4258. index = TaggedInt::ToVarUnchecked(uint32Index);
  4259. goto TaggedIntIndex;
  4260. }
  4261. }
  4262. }
  4263. RecyclableObject* object = nullptr;
  4264. if (!GetPropertyObjectForSetElementI(instance, index, scriptContext, &object))
  4265. {
  4266. return FALSE;
  4267. }
  4268. return JavascriptOperators::SetElementIHelper(instance, object, index, value, scriptContext, flags);
  4269. }
  4270. BOOL JavascriptOperators::SetElementIHelper(Var receiver, RecyclableObject* object, Var index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  4271. {
  4272. IndexType indexType;
  4273. uint32 indexVal = 0;
  4274. PropertyRecord const * propertyRecord = nullptr;
  4275. JavascriptString * propertyNameString = nullptr;
  4276. PropertyValueInfo propertyValueInfo;
  4277. RecyclableObject* cacheOwner;
  4278. PropertyRecordUsageCache* propertyRecordUsageCache;
  4279. if (JavascriptOperators::GetPropertyRecordUsageCache(index, scriptContext, &propertyRecordUsageCache, &cacheOwner))
  4280. {
  4281. return JavascriptOperators::SetElementIWithCache<false>(receiver, object, cacheOwner, value, propertyRecordUsageCache, scriptContext, flags, nullptr);
  4282. }
  4283. if (TaggedNumber::Is(receiver))
  4284. {
  4285. return JavascriptOperators::SetElementIOnTaggedNumber(receiver, object, index, value, scriptContext, flags);
  4286. }
  4287. #if DBG_DUMP
  4288. scriptContext->forinNoCache += (!TaggedInt::Is(index) && VarIs<JavascriptString>(index));
  4289. #endif
  4290. indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, &propertyNameString, false, true);
  4291. if (scriptContext->GetThreadContext()->IsDisableImplicitCall() &&
  4292. scriptContext->GetThreadContext()->GetImplicitCallFlags() != ImplicitCall_None)
  4293. {
  4294. // We hit an implicit call trying to convert the index, and implicit calls are disabled, so
  4295. // quit before we try to store the element.
  4296. return FALSE;
  4297. }
  4298. if (indexType == IndexType_Number)
  4299. {
  4300. SetElementIHelper_INDEX_TYPE_IS_NUMBER:
  4301. return JavascriptOperators::SetItem(receiver, object, indexVal, value, scriptContext, flags);
  4302. }
  4303. else if (indexType == IndexType_JavascriptString)
  4304. {
  4305. Assert(propertyNameString);
  4306. // At this point, we know that the propertyNameString is neither PropertyString
  4307. // or LiteralStringWithPropertyStringPtr.. Get PropertyRecord!
  4308. // we will get it anyways otherwise. (Also, 1:1 string comparison for Builtin types will be expensive.)
  4309. if (propertyRecord == nullptr)
  4310. {
  4311. scriptContext->GetOrAddPropertyRecord(propertyNameString, &propertyRecord);
  4312. if (propertyRecord->IsNumeric())
  4313. {
  4314. indexVal = propertyRecord->GetNumericValue();
  4315. goto SetElementIHelper_INDEX_TYPE_IS_NUMBER;
  4316. }
  4317. }
  4318. }
  4319. Assert(indexType == IndexType_PropertyId || indexType == IndexType_JavascriptString);
  4320. Assert(propertyRecord);
  4321. return JavascriptOperators::SetProperty(receiver, object, propertyRecord->GetPropertyId(), value, scriptContext, flags);
  4322. }
  4323. BOOL JavascriptOperators::OP_SetNativeIntElementI_NoConvert(
  4324. Var instance,
  4325. Var aElementIndex,
  4326. int32 iValue,
  4327. ScriptContext* scriptContext,
  4328. PropertyOperationFlags flags)
  4329. {
  4330. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_NoConvert);
  4331. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_NoConvert, Op_SetNativeIntElementI);
  4332. BOOL converted = OP_SetNativeIntElementI(instance, aElementIndex, iValue, scriptContext, flags);
  4333. if (converted)
  4334. {
  4335. AssertMsg(false, "Unexpected native array conversion");
  4336. Js::Throw::FatalInternalError();
  4337. }
  4338. return FALSE;
  4339. JIT_HELPER_END(Op_SetNativeIntElementI_NoConvert);
  4340. }
  4341. BOOL JavascriptOperators::OP_SetNativeIntElementI_UInt32_NoConvert(
  4342. Var instance,
  4343. uint32 aElementIndex,
  4344. int32 iValue,
  4345. ScriptContext* scriptContext,
  4346. PropertyOperationFlags flags)
  4347. {
  4348. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_UInt32_NoConvert);
  4349. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_UInt32_NoConvert, Op_SetNativeIntElementI_UInt32);
  4350. BOOL converted = OP_SetNativeIntElementI_UInt32(instance, aElementIndex, iValue, scriptContext, flags);
  4351. if (converted)
  4352. {
  4353. AssertMsg(false, "Unexpected native array conversion");
  4354. Js::Throw::FatalInternalError();
  4355. }
  4356. return FALSE;
  4357. JIT_HELPER_END(Op_SetNativeIntElementI_UInt32_NoConvert);
  4358. }
  4359. BOOL JavascriptOperators::OP_SetNativeIntElementI_Int32_NoConvert(
  4360. Var instance,
  4361. int32 aElementIndex,
  4362. int32 iValue,
  4363. ScriptContext* scriptContext,
  4364. PropertyOperationFlags flags)
  4365. {
  4366. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_Int32_NoConvert);
  4367. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_Int32_NoConvert, Op_SetNativeIntElementI_Int32);
  4368. BOOL converted = OP_SetNativeIntElementI_Int32(instance, aElementIndex, iValue, scriptContext, flags);
  4369. if (converted)
  4370. {
  4371. AssertMsg(false, "Unexpected native array conversion");
  4372. Js::Throw::FatalInternalError();
  4373. }
  4374. return FALSE;
  4375. JIT_HELPER_END(Op_SetNativeIntElementI_Int32_NoConvert);
  4376. }
  4377. BOOL JavascriptOperators::OP_SetNativeFloatElementI_NoConvert(
  4378. Var instance,
  4379. Var aElementIndex,
  4380. ScriptContext* scriptContext,
  4381. PropertyOperationFlags flags,
  4382. double dValue)
  4383. {
  4384. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_NoConvert);
  4385. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_NoConvert, Op_SetNativeFloatElementI);
  4386. BOOL converted = OP_SetNativeFloatElementI(instance, aElementIndex, scriptContext, flags, dValue);
  4387. if (converted)
  4388. {
  4389. AssertMsg(false, "Unexpected native array conversion");
  4390. Js::Throw::FatalInternalError();
  4391. }
  4392. return FALSE;
  4393. JIT_HELPER_END(Op_SetNativeFloatElementI_NoConvert);
  4394. }
  4395. BOOL JavascriptOperators::OP_SetNativeFloatElementI_UInt32_NoConvert(
  4396. Var instance,
  4397. uint32 aElementIndex,
  4398. ScriptContext* scriptContext,
  4399. PropertyOperationFlags flags,
  4400. double dValue)
  4401. {
  4402. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_UInt32_NoConvert);
  4403. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_NoConvert, Op_SetNativeFloatElementI_UInt32);
  4404. BOOL converted = OP_SetNativeFloatElementI_UInt32(instance, aElementIndex, scriptContext, flags, dValue);
  4405. if (converted)
  4406. {
  4407. AssertMsg(false, "Unexpected native array conversion");
  4408. Js::Throw::FatalInternalError();
  4409. }
  4410. return FALSE;
  4411. JIT_HELPER_END(Op_SetNativeFloatElementI_UInt32_NoConvert);
  4412. }
  4413. BOOL JavascriptOperators::OP_SetNativeFloatElementI_Int32_NoConvert(
  4414. Var instance,
  4415. int32 aElementIndex,
  4416. ScriptContext* scriptContext,
  4417. PropertyOperationFlags flags,
  4418. double dValue)
  4419. {
  4420. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_Int32_NoConvert);
  4421. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_NoConvert, Op_SetNativeFloatElementI_Int32);
  4422. BOOL converted = OP_SetNativeFloatElementI_Int32(instance, aElementIndex, scriptContext, flags, dValue);
  4423. if (converted)
  4424. {
  4425. AssertMsg(false, "Unexpected native array conversion");
  4426. Js::Throw::FatalInternalError();
  4427. }
  4428. return FALSE;
  4429. JIT_HELPER_END(Op_SetNativeFloatElementI_Int32_NoConvert);
  4430. }
  4431. BOOL JavascriptOperators::OP_SetNativeIntElementI(
  4432. Var instance,
  4433. Var aElementIndex,
  4434. int32 iValue,
  4435. ScriptContext* scriptContext,
  4436. PropertyOperationFlags flags)
  4437. {
  4438. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI);
  4439. INT_PTR vt = (INT_PTR)nullptr;
  4440. vt = VirtualTableInfoBase::GetVirtualTable(instance);
  4441. if (TaggedInt::Is(aElementIndex))
  4442. {
  4443. int32 indexInt = TaggedInt::ToInt32(aElementIndex);
  4444. if (indexInt >= 0 && scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4445. {
  4446. JavascriptNativeIntArray *arr = VarTo<JavascriptNativeIntArray>(instance);
  4447. if (!(arr->TryGrowHeadSegmentAndSetItem<int32, JavascriptNativeIntArray>((uint32)indexInt, iValue)))
  4448. {
  4449. arr->SetItem(indexInt, iValue);
  4450. }
  4451. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4452. }
  4453. }
  4454. JavascriptOperators::OP_SetElementI(instance, aElementIndex, JavascriptNumber::ToVar(iValue, scriptContext), scriptContext, flags);
  4455. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4456. JIT_HELPER_END(Op_SetNativeIntElementI);
  4457. }
  4458. BOOL JavascriptOperators::OP_SetNativeIntElementI_UInt32(
  4459. Var instance,
  4460. uint32 aElementIndex,
  4461. int32 iValue,
  4462. ScriptContext* scriptContext,
  4463. PropertyOperationFlags flags)
  4464. {
  4465. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_UInt32);
  4466. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_UInt32, Op_SetNativeIntElementI);
  4467. #if FLOATVAR
  4468. return OP_SetNativeIntElementI(instance, Js::JavascriptNumber::ToVar(aElementIndex, scriptContext), iValue, scriptContext, flags);
  4469. #else
  4470. char buffer[sizeof(Js::JavascriptNumber)];
  4471. return OP_SetNativeIntElementI(instance, Js::JavascriptNumber::ToVarInPlace(aElementIndex, scriptContext,
  4472. (Js::JavascriptNumber *)buffer), iValue, scriptContext, flags);
  4473. #endif
  4474. JIT_HELPER_END(Op_SetNativeIntElementI_UInt32);
  4475. }
  4476. BOOL JavascriptOperators::OP_SetNativeIntElementI_Int32(
  4477. Var instance,
  4478. int aElementIndex,
  4479. int32 iValue,
  4480. ScriptContext* scriptContext,
  4481. PropertyOperationFlags flags)
  4482. {
  4483. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_Int32);
  4484. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_Int32, Op_SetNativeIntElementI);
  4485. #if FLOATVAR
  4486. return OP_SetNativeIntElementI(instance, Js::JavascriptNumber::ToVar(aElementIndex, scriptContext), iValue, scriptContext, flags);
  4487. #else
  4488. char buffer[sizeof(Js::JavascriptNumber)];
  4489. return OP_SetNativeIntElementI(instance, Js::JavascriptNumber::ToVarInPlace(aElementIndex, scriptContext,
  4490. (Js::JavascriptNumber *)buffer), iValue, scriptContext, flags);
  4491. #endif
  4492. JIT_HELPER_END(Op_SetNativeIntElementI_Int32);
  4493. }
  4494. BOOL JavascriptOperators::OP_SetNativeFloatElementI(
  4495. Var instance,
  4496. Var aElementIndex,
  4497. ScriptContext* scriptContext,
  4498. PropertyOperationFlags flags,
  4499. double dValue)
  4500. {
  4501. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI);
  4502. INT_PTR vt = (INT_PTR)nullptr;
  4503. vt = VirtualTableInfoBase::GetVirtualTable(instance);
  4504. if (TaggedInt::Is(aElementIndex))
  4505. {
  4506. int32 indexInt = TaggedInt::ToInt32(aElementIndex);
  4507. if (indexInt >= 0 && scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4508. {
  4509. JavascriptNativeFloatArray *arr = VarTo<JavascriptNativeFloatArray>(instance);
  4510. if (!(arr->TryGrowHeadSegmentAndSetItem<double, JavascriptNativeFloatArray>((uint32)indexInt, dValue)))
  4511. {
  4512. arr->SetItem(indexInt, dValue);
  4513. }
  4514. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4515. }
  4516. }
  4517. JavascriptOperators::OP_SetElementI(instance, aElementIndex, JavascriptNumber::ToVarWithCheck(dValue, scriptContext), scriptContext, flags);
  4518. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4519. JIT_HELPER_END(Op_SetNativeFloatElementI);
  4520. }
  4521. BOOL JavascriptOperators::OP_SetNativeFloatElementI_UInt32(
  4522. Var instance,
  4523. uint32 aElementIndex,
  4524. ScriptContext* scriptContext,
  4525. PropertyOperationFlags flags,
  4526. double dValue)
  4527. {
  4528. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_UInt32);
  4529. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_UInt32, Op_SetNativeFloatElementI);
  4530. #if FLOATVAR
  4531. return OP_SetNativeFloatElementI(instance, JavascriptNumber::ToVar(aElementIndex, scriptContext), scriptContext, flags, dValue);
  4532. #else
  4533. char buffer[sizeof(Js::JavascriptNumber)];
  4534. return OP_SetNativeFloatElementI(instance, JavascriptNumber::ToVarInPlace(aElementIndex, scriptContext,
  4535. (Js::JavascriptNumber *)buffer), scriptContext, flags, dValue);
  4536. #endif
  4537. JIT_HELPER_END(Op_SetNativeFloatElementI_UInt32);
  4538. }
  4539. BOOL JavascriptOperators::OP_SetNativeFloatElementI_Int32(
  4540. Var instance,
  4541. int aElementIndex,
  4542. ScriptContext* scriptContext,
  4543. PropertyOperationFlags flags,
  4544. double dValue)
  4545. {
  4546. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_Int32);
  4547. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_Int32, Op_SetNativeFloatElementI);
  4548. #if FLOATVAR
  4549. return OP_SetNativeFloatElementI(instance, JavascriptNumber::ToVar(aElementIndex, scriptContext), scriptContext, flags, dValue);
  4550. #else
  4551. char buffer[sizeof(Js::JavascriptNumber)];
  4552. return OP_SetNativeFloatElementI(instance, JavascriptNumber::ToVarInPlace(aElementIndex, scriptContext,
  4553. (Js::JavascriptNumber *)buffer), scriptContext, flags, dValue);
  4554. #endif
  4555. JIT_HELPER_END(Op_SetNativeFloatElementI_Int32);
  4556. }
  4557. BOOL JavascriptOperators::OP_Memcopy(Var dstInstance, int32 dstStart, Var srcInstance, int32 srcStart, int32 length, ScriptContext* scriptContext)
  4558. {
  4559. JIT_HELPER_NOT_REENTRANT_HEADER(Op_Memcopy, reentrancylock, scriptContext->GetThreadContext());
  4560. if (length <= 0)
  4561. {
  4562. return false;
  4563. }
  4564. TypeId instanceType = JavascriptOperators::GetTypeId(srcInstance);
  4565. if (instanceType != JavascriptOperators::GetTypeId(dstInstance))
  4566. {
  4567. return false;
  4568. }
  4569. if (srcStart != dstStart)
  4570. {
  4571. return false;
  4572. }
  4573. BOOL returnValue = false;
  4574. #define MEMCOPY_TYPED_ARRAY(type, conversion) VarTo< type ## >(dstInstance)->DirectSetItemAtRange( VarTo< type ## >(srcInstance), srcStart, dstStart, length, JavascriptConversion:: ## conversion)
  4575. switch (instanceType)
  4576. {
  4577. case TypeIds_Int8Array:
  4578. {
  4579. returnValue = MEMCOPY_TYPED_ARRAY(Int8Array, ToInt8);
  4580. break;
  4581. }
  4582. case TypeIds_Uint8Array:
  4583. {
  4584. returnValue = MEMCOPY_TYPED_ARRAY(Uint8Array, ToUInt8);
  4585. break;
  4586. }
  4587. case TypeIds_Uint8ClampedArray:
  4588. {
  4589. returnValue = MEMCOPY_TYPED_ARRAY(Uint8ClampedArray, ToUInt8Clamped);
  4590. break;
  4591. }
  4592. case TypeIds_Int16Array:
  4593. {
  4594. returnValue = MEMCOPY_TYPED_ARRAY(Int16Array, ToInt16);
  4595. break;
  4596. }
  4597. case TypeIds_Uint16Array:
  4598. {
  4599. returnValue = MEMCOPY_TYPED_ARRAY(Uint16Array, ToUInt16);
  4600. break;
  4601. }
  4602. case TypeIds_Int32Array:
  4603. {
  4604. returnValue = MEMCOPY_TYPED_ARRAY(Int32Array, ToInt32);
  4605. break;
  4606. }
  4607. case TypeIds_Uint32Array:
  4608. {
  4609. returnValue = MEMCOPY_TYPED_ARRAY(Uint32Array, ToUInt32);
  4610. break;
  4611. }
  4612. case TypeIds_Float32Array:
  4613. {
  4614. returnValue = MEMCOPY_TYPED_ARRAY(Float32Array, ToFloat);
  4615. break;
  4616. }
  4617. case TypeIds_Float64Array:
  4618. {
  4619. returnValue = MEMCOPY_TYPED_ARRAY(Float64Array, ToNumber);
  4620. break;
  4621. }
  4622. case TypeIds_Array:
  4623. case TypeIds_NativeFloatArray:
  4624. case TypeIds_NativeIntArray:
  4625. {
  4626. if (dstStart < 0 || srcStart < 0)
  4627. {
  4628. // This is not supported, Bailout
  4629. break;
  4630. }
  4631. // Upper bounds check for source array
  4632. JavascriptArray* srcArray = UnsafeVarTo<JavascriptArray>(srcInstance);
  4633. JavascriptArray* dstArray = VarTo<JavascriptArray>(dstInstance);
  4634. if (scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4635. {
  4636. INT_PTR vt = VirtualTableInfoBase::GetVirtualTable(dstInstance);
  4637. if (instanceType == TypeIds_Array)
  4638. {
  4639. returnValue = dstArray->DirectSetItemAtRangeFromArray<Var>(dstStart, length, srcArray, srcStart);
  4640. }
  4641. else if (instanceType == TypeIds_NativeIntArray)
  4642. {
  4643. returnValue = dstArray->DirectSetItemAtRangeFromArray<int32>(dstStart, length, srcArray, srcStart);
  4644. }
  4645. else
  4646. {
  4647. returnValue = dstArray->DirectSetItemAtRangeFromArray<double>(dstStart, length, srcArray, srcStart);
  4648. }
  4649. returnValue &= vt == VirtualTableInfoBase::GetVirtualTable(dstInstance);
  4650. }
  4651. break;
  4652. }
  4653. default:
  4654. AssertMsg(false, "We don't support this type for memcopy yet.");
  4655. break;
  4656. }
  4657. #undef MEMCOPY_TYPED_ARRAY
  4658. return returnValue;
  4659. JIT_HELPER_END(Op_Memcopy);
  4660. }
  4661. template<typename T, T(*func)(Var, ScriptContext*)> bool MemsetConversion(Var value, ScriptContext* scriptContext, T* result)
  4662. {
  4663. ImplicitCallFlags flags = scriptContext->GetThreadContext()->TryWithDisabledImplicitCall([&]
  4664. {
  4665. *result = func(value, scriptContext);
  4666. });
  4667. return (flags & (~ImplicitCall_None)) == 0;
  4668. }
  4669. BOOL JavascriptOperators::OP_Memset(Var instance, int32 start, Var value, int32 length, ScriptContext* scriptContext)
  4670. {
  4671. JIT_HELPER_NOT_REENTRANT_HEADER(Op_Memset, reentrancylock, scriptContext->GetThreadContext());
  4672. if (length <= 0)
  4673. {
  4674. return false;
  4675. }
  4676. TypeId instanceType = JavascriptOperators::GetTypeId(instance);
  4677. BOOL returnValue = false;
  4678. // The typed array will deal with all possible values for the index
  4679. #define MEMSET_TYPED_ARRAY_CASE(type, conversion) \
  4680. case TypeIds_##type: \
  4681. { \
  4682. type## ::TypedArrayType typedValue = 0; \
  4683. if (!MemsetConversion<type## ::TypedArrayType, JavascriptConversion:: ##conversion>(value, scriptContext, &typedValue)) return false; \
  4684. returnValue = VarTo< type## >(instance)->DirectSetItemAtRange(start, length, typedValue); \
  4685. break; \
  4686. }
  4687. switch (instanceType)
  4688. {
  4689. MEMSET_TYPED_ARRAY_CASE(Int8Array, ToInt8)
  4690. MEMSET_TYPED_ARRAY_CASE(Uint8Array, ToUInt8)
  4691. MEMSET_TYPED_ARRAY_CASE(Uint8ClampedArray, ToUInt8Clamped)
  4692. MEMSET_TYPED_ARRAY_CASE(Int16Array, ToInt16)
  4693. MEMSET_TYPED_ARRAY_CASE(Uint16Array, ToUInt16)
  4694. MEMSET_TYPED_ARRAY_CASE(Int32Array, ToInt32)
  4695. MEMSET_TYPED_ARRAY_CASE(Uint32Array, ToUInt32)
  4696. MEMSET_TYPED_ARRAY_CASE(Float32Array, ToFloat)
  4697. MEMSET_TYPED_ARRAY_CASE(Float64Array, ToNumber)
  4698. case TypeIds_NativeFloatArray:
  4699. case TypeIds_NativeIntArray:
  4700. case TypeIds_Array:
  4701. {
  4702. if (start < 0)
  4703. {
  4704. for (start; start < 0 && length > 0; ++start, --length)
  4705. {
  4706. if (!OP_SetElementI(instance, JavascriptNumber::ToVar(start, scriptContext), value, scriptContext))
  4707. {
  4708. return false;
  4709. }
  4710. }
  4711. }
  4712. if (scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4713. {
  4714. INT_PTR vt = VirtualTableInfoBase::GetVirtualTable(instance);
  4715. if (instanceType == TypeIds_Array)
  4716. {
  4717. returnValue = UnsafeVarTo<JavascriptArray>(instance)->DirectSetItemAtRange<Var>(start, length, value);
  4718. }
  4719. else if (instanceType == TypeIds_NativeIntArray)
  4720. {
  4721. // Only accept tagged int.
  4722. if (!TaggedInt::Is(value))
  4723. {
  4724. return false;
  4725. }
  4726. int32 intValue = 0;
  4727. if (!MemsetConversion<int32, JavascriptConversion::ToInt32>(value, scriptContext, &intValue))
  4728. {
  4729. return false;
  4730. }
  4731. // Special case for missing item
  4732. if (SparseArraySegment<int32>::IsMissingItem(&intValue))
  4733. {
  4734. return false;
  4735. }
  4736. returnValue = UnsafeVarTo<JavascriptArray>(instance)->DirectSetItemAtRange<int32>(start, length, intValue);
  4737. }
  4738. else
  4739. {
  4740. // For native float arrays, the jit doesn't check the type of the source so we have to do it here
  4741. if (!JavascriptNumber::Is(value) && !TaggedNumber::Is(value))
  4742. {
  4743. return false;
  4744. }
  4745. double doubleValue = 0;
  4746. if (!MemsetConversion<double, JavascriptConversion::ToNumber>(value, scriptContext, &doubleValue))
  4747. {
  4748. return false;
  4749. }
  4750. // Special case for missing item
  4751. if (SparseArraySegment<double>::IsMissingItem(&doubleValue))
  4752. {
  4753. return false;
  4754. }
  4755. returnValue = UnsafeVarTo<JavascriptArray>(instance)->DirectSetItemAtRange<double>(start, length, doubleValue);
  4756. }
  4757. returnValue &= vt == VirtualTableInfoBase::GetVirtualTable(instance);
  4758. }
  4759. break;
  4760. }
  4761. default:
  4762. AssertMsg(false, "We don't support this type for memset yet.");
  4763. break;
  4764. }
  4765. #undef MEMSET_TYPED_ARRAY
  4766. return returnValue;
  4767. JIT_HELPER_END(Op_Memset);
  4768. }
  4769. Var JavascriptOperators::OP_DeleteElementI_UInt32(Var instance, uint32 index, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  4770. {
  4771. JIT_HELPER_REENTRANT_HEADER(Op_DeleteElementI_UInt32);
  4772. JIT_HELPER_SAME_ATTRIBUTES(Op_DeleteElementI_UInt32, Op_DeleteElementI);
  4773. #if FLOATVAR
  4774. return OP_DeleteElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext, propertyOperationFlags);
  4775. #else
  4776. char buffer[sizeof(Js::JavascriptNumber)];
  4777. return OP_DeleteElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  4778. (Js::JavascriptNumber *)buffer), scriptContext, propertyOperationFlags);
  4779. #endif
  4780. JIT_HELPER_END(Op_DeleteElementI_UInt32);
  4781. }
  4782. Var JavascriptOperators::OP_DeleteElementI_Int32(Var instance, int32 index, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  4783. {
  4784. JIT_HELPER_REENTRANT_HEADER(Op_DeleteElementI_Int32);
  4785. JIT_HELPER_SAME_ATTRIBUTES(Op_DeleteElementI_Int32, 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_Int32);
  4794. }
  4795. Var JavascriptOperators::OP_DeleteElementI(Var instance, Var index, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  4796. {
  4797. JIT_HELPER_REENTRANT_HEADER(Op_DeleteElementI);
  4798. if(TaggedNumber::Is(instance))
  4799. {
  4800. return scriptContext->GetLibrary()->GetTrue();
  4801. }
  4802. #if ENABLE_COPYONACCESS_ARRAY
  4803. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  4804. #endif
  4805. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  4806. if (JavascriptOperators::IsUndefinedOrNull(object))
  4807. {
  4808. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotDelete_NullOrUndefined, GetPropertyDisplayNameForError(index, scriptContext));
  4809. }
  4810. uint32 indexVal;
  4811. PropertyRecord const * propertyRecord = nullptr;
  4812. JavascriptString * propertyNameString = nullptr;
  4813. BOOL result = TRUE;
  4814. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, &propertyNameString, false, true);
  4815. if (indexType == IndexType_Number)
  4816. {
  4817. result = JavascriptOperators::DeleteItem(object, indexVal, propertyOperationFlags);
  4818. }
  4819. else if (indexType == IndexType_JavascriptString)
  4820. {
  4821. result = JavascriptOperators::DeleteProperty(object, propertyNameString, propertyOperationFlags);
  4822. }
  4823. else
  4824. {
  4825. Assert(indexType == IndexType_PropertyId);
  4826. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  4827. {
  4828. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
  4829. Assert(indexType == IndexType_PropertyId);
  4830. Assert(propertyRecord != nullptr);
  4831. }
  4832. if (propertyRecord != nullptr)
  4833. {
  4834. result = JavascriptOperators::DeleteProperty(object, propertyRecord->GetPropertyId(), propertyOperationFlags);
  4835. }
  4836. #if DBG
  4837. else
  4838. {
  4839. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  4840. PropertyRecord const * debugPropertyRecord;
  4841. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  4842. AssertMsg(JavascriptOperators::DeleteProperty(object, debugPropertyRecord->GetPropertyId(), propertyOperationFlags), "delete should have been true. See OS Bug 2727708 if you see this come from the web");
  4843. }
  4844. #endif
  4845. }
  4846. Assert(result || !(propertyOperationFlags & (PropertyOperation_StrictMode | PropertyOperation_ThrowOnDeleteIfNotConfig)));
  4847. return scriptContext->GetLibrary()->CreateBoolean(result);
  4848. JIT_HELPER_END(Op_DeleteElementI);
  4849. }
  4850. Var JavascriptOperators::OP_ToPropertyKey(Js::Var argument, ScriptContext* scriptContext)
  4851. {
  4852. JIT_HELPER_REENTRANT_HEADER(Op_ConvPropertyKey);
  4853. PropertyRecord const* unused = nullptr;
  4854. return JavascriptConversion::ToPropertyKey(argument, scriptContext, &unused, nullptr);
  4855. JIT_HELPER_END(Op_ConvPropertyKey);
  4856. }
  4857. Var JavascriptOperators::OP_GetLength(Var instance, ScriptContext* scriptContext)
  4858. {
  4859. return JavascriptOperators::OP_GetProperty(instance, PropertyIds::length, scriptContext);
  4860. }
  4861. Var JavascriptOperators::GetThisFromModuleRoot(Var thisVar)
  4862. {
  4863. RootObjectBase * rootObject = static_cast<RootObjectBase*>(thisVar);
  4864. RecyclableObject* hostObject = rootObject->GetHostObject();
  4865. //
  4866. // if the module root has the host object, use that as "this"
  4867. //
  4868. if (hostObject)
  4869. {
  4870. thisVar = hostObject->GetHostDispatchVar();
  4871. }
  4872. return thisVar;
  4873. }
  4874. inline void JavascriptOperators::TryLoadRoot(Var& thisVar, TypeId typeId, int moduleID, ScriptContextInfo* scriptContext)
  4875. {
  4876. bool loadRoot = false;
  4877. if (JavascriptOperators::IsUndefinedOrNullType(typeId) || typeId == TypeIds_ActivationObject)
  4878. {
  4879. loadRoot = true;
  4880. }
  4881. else if (typeId == TypeIds_HostDispatch)
  4882. {
  4883. TypeId remoteTypeId = TypeIds_Limit;
  4884. if (VarTo<RecyclableObject>(thisVar)->GetRemoteTypeId(&remoteTypeId))
  4885. {
  4886. if (remoteTypeId == TypeIds_Null || remoteTypeId == TypeIds_Undefined || remoteTypeId == TypeIds_ActivationObject)
  4887. {
  4888. loadRoot = true;
  4889. }
  4890. }
  4891. }
  4892. if (loadRoot)
  4893. {
  4894. if (moduleID == 0)
  4895. {
  4896. thisVar = (Js::Var)scriptContext->GetGlobalObjectThisAddr();
  4897. }
  4898. else
  4899. {
  4900. // TODO: OOP JIT, create a copy of module roots in server side
  4901. Js::ModuleRoot * moduleRoot = JavascriptOperators::GetModuleRoot(moduleID, (ScriptContext*)scriptContext);
  4902. if (moduleRoot == nullptr)
  4903. {
  4904. Assert(false);
  4905. thisVar = (Js::Var)scriptContext->GetUndefinedAddr();
  4906. }
  4907. else
  4908. {
  4909. thisVar = GetThisFromModuleRoot(moduleRoot);
  4910. }
  4911. }
  4912. }
  4913. }
  4914. Var JavascriptOperators::OP_GetThis(Var thisVar, int moduleID, ScriptContextInfo* scriptContext)
  4915. {
  4916. JIT_HELPER_REENTRANT_HEADER(LdThis);
  4917. //
  4918. // if "this" is null or undefined
  4919. // Pass the global object
  4920. // Else
  4921. // Pass ToObject(this)
  4922. //
  4923. TypeId typeId = JavascriptOperators::GetTypeId(thisVar);
  4924. Assert(!JavascriptOperators::IsThisSelf(typeId));
  4925. return JavascriptOperators::GetThisHelper(thisVar, typeId, moduleID, scriptContext);
  4926. JIT_HELPER_END(LdThis);
  4927. }
  4928. Var JavascriptOperators::OP_GetThisNoFastPath(Var thisVar, int moduleID, ScriptContext* scriptContext)
  4929. {
  4930. JIT_HELPER_REENTRANT_HEADER(LdThisNoFastPath);
  4931. TypeId typeId = JavascriptOperators::GetTypeId(thisVar);
  4932. if (JavascriptOperators::IsThisSelf(typeId))
  4933. {
  4934. Assert(typeId != TypeIds_GlobalObject || ((Js::GlobalObject*)thisVar)->ToThis() == thisVar);
  4935. Assert(typeId != TypeIds_ModuleRoot || JavascriptOperators::GetThisFromModuleRoot(thisVar) == thisVar);
  4936. return thisVar;
  4937. }
  4938. return JavascriptOperators::GetThisHelper(thisVar, typeId, moduleID, scriptContext);
  4939. JIT_HELPER_END(LdThisNoFastPath);
  4940. }
  4941. bool JavascriptOperators::IsThisSelf(TypeId typeId)
  4942. {
  4943. return (JavascriptOperators::IsObjectType(typeId) && ! JavascriptOperators::IsSpecialObjectType(typeId));
  4944. }
  4945. Var JavascriptOperators::GetThisHelper(Var thisVar, TypeId typeId, int moduleID, ScriptContextInfo *scriptContext)
  4946. {
  4947. if (! JavascriptOperators::IsObjectType(typeId) && ! JavascriptOperators::IsUndefinedOrNullType(typeId))
  4948. {
  4949. #if ENABLE_NATIVE_CODEGEN
  4950. Assert(!JITManager::GetJITManager()->IsJITServer());
  4951. #endif
  4952. #if !FLOATVAR
  4953. // We allowed stack number to be used as the "this" for getter and setter activation of
  4954. // n.x and n[prop], where n is the Javascript Number
  4955. return JavascriptOperators::ToObject(
  4956. JavascriptNumber::BoxStackNumber(thisVar, (ScriptContext*)scriptContext), (ScriptContext*)scriptContext);
  4957. #else
  4958. return JavascriptOperators::ToObject(thisVar, (ScriptContext*)scriptContext);
  4959. #endif
  4960. }
  4961. else
  4962. {
  4963. TryLoadRoot(thisVar, typeId, moduleID, scriptContext);
  4964. return thisVar;
  4965. }
  4966. }
  4967. BOOL JavascriptOperators::GetRemoteTypeId(Var aValue, __out TypeId* typeId)
  4968. {
  4969. *typeId = TypeIds_Limit;
  4970. if (GetTypeId(aValue) != TypeIds_HostDispatch)
  4971. {
  4972. return FALSE;
  4973. }
  4974. return VarTo<RecyclableObject>(aValue)->GetRemoteTypeId(typeId);
  4975. }
  4976. BOOL JavascriptOperators::IsJsNativeType(TypeId type)
  4977. {
  4978. switch(type)
  4979. {
  4980. case TypeIds_Object:
  4981. case TypeIds_Function:
  4982. case TypeIds_Array:
  4983. case TypeIds_NativeIntArray:
  4984. #if ENABLE_COPYONACCESS_ARRAY
  4985. case TypeIds_CopyOnAccessNativeIntArray:
  4986. #endif
  4987. case TypeIds_NativeFloatArray:
  4988. case TypeIds_ES5Array:
  4989. case TypeIds_Date:
  4990. case TypeIds_RegEx:
  4991. case TypeIds_Error:
  4992. case TypeIds_BooleanObject:
  4993. case TypeIds_NumberObject:
  4994. case TypeIds_StringObject:
  4995. case TypeIds_Symbol:
  4996. case TypeIds_SymbolObject:
  4997. //case TypeIds_GlobalObject:
  4998. //case TypeIds_ModuleRoot:
  4999. //case TypeIds_HostObject:
  5000. case TypeIds_Arguments:
  5001. case TypeIds_ActivationObject:
  5002. case TypeIds_Map:
  5003. case TypeIds_Set:
  5004. case TypeIds_WeakMap:
  5005. case TypeIds_WeakSet:
  5006. case TypeIds_ArrayIterator:
  5007. case TypeIds_MapIterator:
  5008. case TypeIds_SetIterator:
  5009. case TypeIds_StringIterator:
  5010. case TypeIds_Generator:
  5011. case TypeIds_AsyncGenerator:
  5012. case TypeIds_AsyncFromSyncIterator:
  5013. case TypeIds_Promise:
  5014. case TypeIds_Proxy:
  5015. return true;
  5016. default:
  5017. return false;
  5018. }
  5019. }
  5020. BOOL JavascriptOperators::IsJsNativeObject(Var instance)
  5021. {
  5022. return IsJsNativeType(GetTypeId(instance));
  5023. }
  5024. BOOL JavascriptOperators::IsJsNativeObject(_In_ RecyclableObject* instance)
  5025. {
  5026. return IsJsNativeType(GetTypeId(instance));
  5027. }
  5028. bool JavascriptOperators::CanShortcutOnUnknownPropertyName(RecyclableObject *instance)
  5029. {
  5030. if (!CanShortcutInstanceOnUnknownPropertyName(instance))
  5031. {
  5032. return false;
  5033. }
  5034. return CanShortcutPrototypeChainOnUnknownPropertyName(instance->GetPrototype());
  5035. }
  5036. bool JavascriptOperators::CanShortcutInstanceOnUnknownPropertyName(RecyclableObject *instance)
  5037. {
  5038. if (PHASE_OFF1(Js::OptUnknownElementNamePhase))
  5039. {
  5040. return false;
  5041. }
  5042. TypeId typeId = instance->GetTypeId();
  5043. if (typeId == TypeIds_Proxy || typeId == TypeIds_HostDispatch)
  5044. {
  5045. return false;
  5046. }
  5047. if (DynamicType::Is(typeId) &&
  5048. static_cast<DynamicObject*>(instance)->GetTypeHandler()->IsStringTypeHandler())
  5049. {
  5050. return false;
  5051. }
  5052. if (instance->IsExternal())
  5053. {
  5054. return false;
  5055. }
  5056. if (!(instance->HasDeferredTypeHandler()))
  5057. {
  5058. JavascriptFunction * function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  5059. return function && function->IsExternalFunction();
  5060. }
  5061. return false;
  5062. }
  5063. bool JavascriptOperators::CanShortcutPrototypeChainOnUnknownPropertyName(RecyclableObject *prototype)
  5064. {
  5065. Assert(prototype);
  5066. for (; !JavascriptOperators::IsNull(prototype); prototype = prototype->GetPrototype())
  5067. {
  5068. if (!CanShortcutInstanceOnUnknownPropertyName(prototype))
  5069. {
  5070. return false;
  5071. }
  5072. }
  5073. return true;
  5074. }
  5075. RecyclableObject* JavascriptOperators::GetPrototype(RecyclableObject* instance)
  5076. {
  5077. if (JavascriptOperators::GetTypeId(instance) == TypeIds_Null)
  5078. {
  5079. return instance;
  5080. }
  5081. return instance->GetPrototype();
  5082. }
  5083. RecyclableObject* JavascriptOperators::OP_GetPrototype(Var instance, ScriptContext* scriptContext)
  5084. {
  5085. if (TaggedNumber::Is(instance))
  5086. {
  5087. return scriptContext->GetLibrary()->GetNumberPrototype();
  5088. }
  5089. else
  5090. {
  5091. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  5092. if (JavascriptOperators::IsNull(object))
  5093. {
  5094. return object;
  5095. }
  5096. return JavascriptOperators::GetPrototype(object);
  5097. }
  5098. }
  5099. BOOL JavascriptOperators::OP_BrFncEqApply(Var instance, ScriptContext *scriptContext)
  5100. {
  5101. JIT_HELPER_NOT_REENTRANT_HEADER(Op_OP_BrFncEqApply, reentrancylock, scriptContext->GetThreadContext());
  5102. // JavascriptFunction && !HostDispatch
  5103. if (JavascriptOperators::GetTypeId(instance) == TypeIds_Function)
  5104. {
  5105. FunctionProxy *bod= ((JavascriptFunction*)instance)->GetFunctionProxy();
  5106. if (bod != nullptr)
  5107. {
  5108. return bod->GetDirectEntryPoint(bod->GetDefaultEntryPointInfo()) == &Js::JavascriptFunction::EntryApply;
  5109. }
  5110. else
  5111. {
  5112. FunctionInfo* info = ((JavascriptFunction *)instance)->GetFunctionInfo();
  5113. if (info != nullptr)
  5114. {
  5115. return &Js::JavascriptFunction::EntryApply == info->GetOriginalEntryPoint();
  5116. }
  5117. else
  5118. {
  5119. return false;
  5120. }
  5121. }
  5122. }
  5123. return false;
  5124. JIT_HELPER_END(Op_OP_BrFncEqApply);
  5125. }
  5126. BOOL JavascriptOperators::OP_BrFncNeqApply(Var instance, ScriptContext *scriptContext)
  5127. {
  5128. JIT_HELPER_NOT_REENTRANT_HEADER(Op_OP_BrFncNeqApply, reentrancylock, scriptContext->GetThreadContext());
  5129. // JavascriptFunction and !HostDispatch
  5130. if (JavascriptOperators::GetTypeId(instance) == TypeIds_Function)
  5131. {
  5132. FunctionProxy *bod = ((JavascriptFunction *)instance)->GetFunctionProxy();
  5133. if (bod != nullptr)
  5134. {
  5135. return bod->GetDirectEntryPoint(bod->GetDefaultEntryPointInfo()) != &Js::JavascriptFunction::EntryApply;
  5136. }
  5137. else
  5138. {
  5139. FunctionInfo* info = ((JavascriptFunction *)instance)->GetFunctionInfo();
  5140. if (info != nullptr)
  5141. {
  5142. return &Js::JavascriptFunction::EntryApply != info->GetOriginalEntryPoint();
  5143. }
  5144. else
  5145. {
  5146. return true;
  5147. }
  5148. }
  5149. }
  5150. return true;
  5151. JIT_HELPER_END(Op_OP_BrFncNeqApply);
  5152. }
  5153. BOOL JavascriptOperators::OP_BrHasSideEffects(int se, ScriptContext* scriptContext)
  5154. {
  5155. return (scriptContext->optimizationOverrides.GetSideEffects() & se) != SideEffects_None;
  5156. }
  5157. BOOL JavascriptOperators::OP_BrNotHasSideEffects(int se, ScriptContext* scriptContext)
  5158. {
  5159. return (scriptContext->optimizationOverrides.GetSideEffects() & se) == SideEffects_None;
  5160. }
  5161. // returns NULL if there is no more elements to enumerate.
  5162. Var JavascriptOperators::OP_BrOnEmpty(ForInObjectEnumerator * aEnumerator)
  5163. {
  5164. JIT_HELPER_REENTRANT_HEADER(Op_OP_BrOnEmpty);
  5165. PropertyId id;
  5166. return aEnumerator->MoveAndGetNext(id);
  5167. JIT_HELPER_END(Op_OP_BrOnEmpty);
  5168. }
  5169. void JavascriptOperators::OP_InitForInEnumerator(Var enumerable, ForInObjectEnumerator * enumerator, ScriptContext* scriptContext, EnumeratorCache * forInCache)
  5170. {
  5171. JIT_HELPER_REENTRANT_HEADER(Op_OP_InitForInEnumerator);
  5172. RecyclableObject* enumerableObject;
  5173. #if ENABLE_COPYONACCESS_ARRAY
  5174. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(enumerable);
  5175. #endif
  5176. if (!GetPropertyObject(enumerable, scriptContext, &enumerableObject))
  5177. {
  5178. enumerableObject = nullptr;
  5179. }
  5180. enumerator->Initialize(enumerableObject, scriptContext, false, forInCache);
  5181. JIT_HELPER_END(Op_OP_InitForInEnumerator);
  5182. }
  5183. Js::Var JavascriptOperators::OP_CmEq_A(Var a, Var b, ScriptContext* scriptContext)
  5184. {
  5185. JIT_HELPER_REENTRANT_HEADER(OP_CmEq_A);
  5186. return JavascriptBoolean::ToVar(JavascriptOperators::Equal(a, b, scriptContext), scriptContext);
  5187. JIT_HELPER_END(OP_CmEq_A);
  5188. }
  5189. Var JavascriptOperators::OP_CmNeq_A(Var a, Var b, ScriptContext* scriptContext)
  5190. {
  5191. JIT_HELPER_REENTRANT_HEADER(OP_CmNeq_A);
  5192. return JavascriptBoolean::ToVar(JavascriptOperators::NotEqual(a,b,scriptContext), scriptContext);
  5193. JIT_HELPER_END(OP_CmNeq_A);
  5194. }
  5195. Var JavascriptOperators::OP_CmSrEq_A(Var a, Var b, ScriptContext* scriptContext)
  5196. {
  5197. JIT_HELPER_REENTRANT_HEADER(OP_CmSrEq_A);
  5198. return JavascriptBoolean::ToVar(JavascriptOperators::StrictEqual(a, b, scriptContext), scriptContext);
  5199. JIT_HELPER_END(OP_CmSrEq_A);
  5200. }
  5201. Var JavascriptOperators::OP_CmSrEq_String(Var a, JavascriptString* b, ScriptContext *scriptContext)
  5202. {
  5203. JIT_HELPER_REENTRANT_HEADER(OP_CmSrEq_String);
  5204. return JavascriptBoolean::ToVar(JavascriptOperators::StrictEqualString(a, b), scriptContext);
  5205. JIT_HELPER_END(OP_CmSrEq_String);
  5206. }
  5207. Var JavascriptOperators::OP_CmSrEq_EmptyString(Var a, ScriptContext *scriptContext)
  5208. {
  5209. JIT_HELPER_REENTRANT_HEADER(OP_CmSrEq_EmptyString);
  5210. return JavascriptBoolean::ToVar(JavascriptOperators::StrictEqualEmptyString(a), scriptContext);
  5211. JIT_HELPER_END(OP_CmSrEq_EmptyString);
  5212. }
  5213. Var JavascriptOperators::OP_CmSrNeq_A(Var a, Var b, ScriptContext* scriptContext)
  5214. {
  5215. JIT_HELPER_REENTRANT_HEADER(OP_CmSrNeq_A);
  5216. return JavascriptBoolean::ToVar(JavascriptOperators::NotStrictEqual(a, b, scriptContext), scriptContext);
  5217. JIT_HELPER_END(OP_CmSrNeq_A);
  5218. }
  5219. Var JavascriptOperators::OP_CmLt_A(Var a, Var b, ScriptContext* scriptContext)
  5220. {
  5221. JIT_HELPER_REENTRANT_HEADER(OP_CmLt_A);
  5222. return JavascriptBoolean::ToVar(JavascriptOperators::Less(a, b, scriptContext), scriptContext);
  5223. JIT_HELPER_END(OP_CmLt_A);
  5224. }
  5225. Var JavascriptOperators::OP_CmLe_A(Var a, Var b, ScriptContext* scriptContext)
  5226. {
  5227. JIT_HELPER_REENTRANT_HEADER(OP_CmLe_A);
  5228. return JavascriptBoolean::ToVar(JavascriptOperators::LessEqual(a, b, scriptContext), scriptContext);
  5229. JIT_HELPER_END(OP_CmLe_A);
  5230. }
  5231. Var JavascriptOperators::OP_CmGt_A(Var a, Var b, ScriptContext* scriptContext)
  5232. {
  5233. JIT_HELPER_REENTRANT_HEADER(OP_CmGt_A);
  5234. return JavascriptBoolean::ToVar(JavascriptOperators::Greater(a, b, scriptContext), scriptContext);
  5235. JIT_HELPER_END(OP_CmGt_A);
  5236. }
  5237. Var JavascriptOperators::OP_CmGe_A(Var a, Var b, ScriptContext* scriptContext)
  5238. {
  5239. JIT_HELPER_REENTRANT_HEADER(OP_CmGe_A);
  5240. return JavascriptBoolean::ToVar(JavascriptOperators::GreaterEqual(a, b, scriptContext), scriptContext);
  5241. JIT_HELPER_END(OP_CmGe_A);
  5242. }
  5243. DetachedStateBase* JavascriptOperators::DetachVarAndGetState(Var var, bool queueForDelayFree/* = true*/)
  5244. {
  5245. switch (GetTypeId(var))
  5246. {
  5247. case TypeIds_ArrayBuffer:
  5248. return Js::VarTo<Js::ArrayBuffer>(var)->DetachAndGetState(queueForDelayFree);
  5249. default:
  5250. if (!Js::VarTo<Js::RecyclableObject>(var)->IsExternal())
  5251. {
  5252. AssertMsg(false, "We should explicitly have a case statement for each non-external object that can be detached.");
  5253. }
  5254. return nullptr;
  5255. }
  5256. }
  5257. bool JavascriptOperators::IsObjectDetached(Var var)
  5258. {
  5259. switch (GetTypeId(var))
  5260. {
  5261. case TypeIds_ArrayBuffer:
  5262. return Js::VarTo<Js::ArrayBuffer>(var)->IsDetached();
  5263. default:
  5264. return false;
  5265. }
  5266. }
  5267. Var JavascriptOperators::NewVarFromDetachedState(DetachedStateBase* state, JavascriptLibrary *library)
  5268. {
  5269. AssertOrFailFastMsg(state->GetTypeId() == TypeIds_ArrayBuffer, "We should only re-activate detached ArrayBuffer");
  5270. return Js::ArrayBuffer::NewFromDetachedState(state, library);
  5271. }
  5272. DynamicType *
  5273. JavascriptOperators::EnsureObjectLiteralType(ScriptContext* scriptContext, const Js::PropertyIdArray *propIds, Field(DynamicType*)* literalType)
  5274. {
  5275. JIT_HELPER_NOT_REENTRANT_HEADER(EnsureObjectLiteralType, reentrancylock, scriptContext->GetThreadContext());
  5276. DynamicType * newType = *literalType;
  5277. if (newType != nullptr)
  5278. {
  5279. if (!newType->GetIsShared())
  5280. {
  5281. newType->ShareType();
  5282. }
  5283. }
  5284. else
  5285. {
  5286. DynamicType* objectType =
  5287. FunctionBody::DoObjectHeaderInliningForObjectLiteral(propIds)
  5288. ? scriptContext->GetLibrary()->GetObjectHeaderInlinedLiteralType((uint16)propIds->count)
  5289. : scriptContext->GetLibrary()->GetObjectLiteralType(
  5290. static_cast<PropertyIndex>(
  5291. min(propIds->count, static_cast<uint32>(MaxPreInitializedObjectTypeInlineSlotCount))));
  5292. newType = PathTypeHandlerBase::CreateTypeForNewScObject(scriptContext, objectType, propIds, false);
  5293. *literalType = newType;
  5294. }
  5295. Assert(scriptContext);
  5296. Assert(GetLiteralInlineSlotCapacity(propIds) == newType->GetTypeHandler()->GetInlineSlotCapacity());
  5297. Assert(newType->GetTypeHandler()->GetSlotCapacity() >= 0);
  5298. Assert(GetLiteralSlotCapacity(propIds) == (uint)newType->GetTypeHandler()->GetSlotCapacity());
  5299. return newType;
  5300. JIT_HELPER_END(EnsureObjectLiteralType);
  5301. }
  5302. Var JavascriptOperators::NewScObjectLiteral(ScriptContext* scriptContext, const Js::PropertyIdArray *propIds, Field(DynamicType*)* literalType)
  5303. {
  5304. Assert(propIds->count != 0);
  5305. Assert(!propIds->hadDuplicates); // duplicates are removed by parser
  5306. #ifdef PROFILE_OBJECT_LITERALS
  5307. // Empty objects not counted in the object literal counts
  5308. scriptContext->objectLiteralInstanceCount++;
  5309. if (propIds->count > scriptContext->objectLiteralMaxLength)
  5310. {
  5311. scriptContext->objectLiteralMaxLength = propIds->count;
  5312. }
  5313. #endif
  5314. DynamicType* newType = EnsureObjectLiteralType(scriptContext, propIds, literalType);
  5315. DynamicObject* instance = DynamicObject::New(scriptContext->GetRecycler(), newType);
  5316. if (!newType->GetIsShared())
  5317. {
  5318. #if ENABLE_FIXED_FIELDS
  5319. newType->GetTypeHandler()->SetSingletonInstanceIfNeeded(instance);
  5320. #endif
  5321. }
  5322. #ifdef PROFILE_OBJECT_LITERALS
  5323. else
  5324. {
  5325. scriptContext->objectLiteralCacheCount++;
  5326. }
  5327. #endif
  5328. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(instance));
  5329. // can't auto-proxy here as object literal is not exactly "new" object and cannot be intercepted as proxy.
  5330. return instance;
  5331. }
  5332. uint JavascriptOperators::GetLiteralSlotCapacity(Js::PropertyIdArray const * propIds)
  5333. {
  5334. const uint inlineSlotCapacity = GetLiteralInlineSlotCapacity(propIds);
  5335. return DynamicTypeHandler::RoundUpSlotCapacity(propIds->count, static_cast<PropertyIndex>(inlineSlotCapacity));
  5336. }
  5337. uint JavascriptOperators::GetLiteralInlineSlotCapacity(
  5338. Js::PropertyIdArray const * propIds)
  5339. {
  5340. if (propIds->hadDuplicates)
  5341. {
  5342. return 0;
  5343. }
  5344. return
  5345. FunctionBody::DoObjectHeaderInliningForObjectLiteral(propIds)
  5346. ? DynamicTypeHandler::RoundUpObjectHeaderInlinedInlineSlotCapacity(static_cast<PropertyIndex>(propIds->count))
  5347. : DynamicTypeHandler::RoundUpInlineSlotCapacity(
  5348. static_cast<PropertyIndex>(
  5349. min(propIds->count, static_cast<uint32>(MaxPreInitializedObjectTypeInlineSlotCount))));
  5350. }
  5351. Var JavascriptOperators::OP_InitCachedScope(Var varFunc, const Js::PropertyIdArray *propIds, Field(DynamicType*)* literalType, bool formalsAreLetDecls, ScriptContext *scriptContext)
  5352. {
  5353. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitCachedScope, reentrancylock, scriptContext->GetThreadContext());
  5354. bool isGAFunction = VarIs<JavascriptFunction>(varFunc);
  5355. Assert(isGAFunction);
  5356. if (isGAFunction)
  5357. {
  5358. JavascriptFunction *function = VarTo<JavascriptFunction>(varFunc);
  5359. isGAFunction = JavascriptGeneratorFunction::Test(function) || JavascriptAsyncFunction::Test(function);
  5360. }
  5361. ScriptFunction *func = isGAFunction ?
  5362. VarTo<JavascriptGeneratorFunction>(varFunc)->GetGeneratorVirtualScriptFunction() :
  5363. VarTo<ScriptFunction>(varFunc);
  5364. #ifdef PROFILE_OBJECT_LITERALS
  5365. // Empty objects not counted in the object literal counts
  5366. scriptContext->objectLiteralInstanceCount++;
  5367. if (propIds->count > scriptContext->objectLiteralMaxLength)
  5368. {
  5369. scriptContext->objectLiteralMaxLength = propIds->count;
  5370. }
  5371. #endif
  5372. PropertyId cachedFuncCount = ActivationObjectEx::GetCachedFuncCount(propIds);
  5373. PropertyId firstFuncSlot = ActivationObjectEx::GetFirstFuncSlot(propIds);
  5374. PropertyId firstVarSlot = ActivationObjectEx::GetFirstVarSlot(propIds);
  5375. PropertyId lastFuncSlot = Constants::NoProperty;
  5376. if (firstFuncSlot != Constants::NoProperty)
  5377. {
  5378. if (firstVarSlot == Constants::NoProperty || firstVarSlot < firstFuncSlot)
  5379. {
  5380. lastFuncSlot = propIds->count - 1;
  5381. }
  5382. else
  5383. {
  5384. lastFuncSlot = firstVarSlot - 1;
  5385. }
  5386. }
  5387. DynamicType *type = *literalType;
  5388. if (type != nullptr)
  5389. {
  5390. #ifdef PROFILE_OBJECT_LITERALS
  5391. scriptContext->objectLiteralCacheCount++;
  5392. #endif
  5393. }
  5394. else
  5395. {
  5396. type = scriptContext->GetLibrary()->GetActivationObjectType();
  5397. if (formalsAreLetDecls)
  5398. {
  5399. uint formalsSlotLimit = (firstFuncSlot != Constants::NoProperty) ? (uint)firstFuncSlot :
  5400. (firstVarSlot != Constants::NoProperty) ? (uint)firstVarSlot :
  5401. propIds->count;
  5402. if (func->GetFunctionBody()->HasReferenceableBuiltInArguments())
  5403. {
  5404. type = PathTypeHandlerBase::CreateNewScopeObject<true>(scriptContext, type, propIds, PropertyLet, formalsSlotLimit);
  5405. }
  5406. else
  5407. {
  5408. type = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, type, propIds, PropertyLet, formalsSlotLimit);
  5409. }
  5410. }
  5411. else
  5412. {
  5413. type = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, type, propIds);
  5414. }
  5415. *literalType = type;
  5416. }
  5417. Var undef = scriptContext->GetLibrary()->GetUndefined();
  5418. ActivationObjectEx *scopeObjEx = func->GetCachedScope();
  5419. if (scopeObjEx && scopeObjEx->IsCommitted())
  5420. {
  5421. scopeObjEx->ReplaceType(type);
  5422. scopeObjEx->SetCommit(false);
  5423. #if DBG
  5424. for (uint i = firstVarSlot; i < propIds->count; i++)
  5425. {
  5426. AssertMsg(scopeObjEx->GetSlot(i) == undef, "Var attached to cached scope");
  5427. }
  5428. #endif
  5429. }
  5430. else
  5431. {
  5432. ActivationObjectEx *tmp = RecyclerNewPlus(scriptContext->GetRecycler(), (cachedFuncCount == 0 ? 0 : cachedFuncCount - 1) * sizeof(FuncCacheEntry), ActivationObjectEx, type, func, cachedFuncCount, firstFuncSlot, lastFuncSlot);
  5433. if (!scopeObjEx)
  5434. {
  5435. func->SetCachedScope(tmp);
  5436. }
  5437. scopeObjEx = tmp;
  5438. for (uint i = firstVarSlot; i < propIds->count; i++)
  5439. {
  5440. scopeObjEx->SetSlot(SetSlotArguments(propIds->elements[i], i, undef));
  5441. }
  5442. }
  5443. return scopeObjEx;
  5444. JIT_HELPER_END(OP_InitCachedScope);
  5445. }
  5446. void JavascriptOperators::OP_InvalidateCachedScope(void* varEnv, int32 envIndex)
  5447. {
  5448. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_InvalidateCachedScope);
  5449. FrameDisplay *disp = (FrameDisplay*)varEnv;
  5450. Var item = disp->GetItem(envIndex);
  5451. if (item != nullptr)
  5452. {
  5453. Assert(VarIs<ActivationObjectEx>(item));
  5454. RecyclableObject *objScope = VarTo<RecyclableObject>(item);
  5455. objScope->InvalidateCachedScope();
  5456. }
  5457. JIT_HELPER_END(OP_InvalidateCachedScope);
  5458. }
  5459. void JavascriptOperators::OP_InitCachedFuncs(Var varScope, FrameDisplay *pDisplay, const FuncInfoArray *info, ScriptContext *scriptContext)
  5460. {
  5461. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitCachedFuncs, reentrancylock, scriptContext->GetThreadContext());
  5462. ActivationObjectEx *scopeObj = VarTo<ActivationObjectEx>(varScope);
  5463. Assert(scopeObj->GetTypeHandler()->GetInlineSlotCapacity() == 0);
  5464. uint funcCount = info->count;
  5465. if (funcCount == 0)
  5466. {
  5467. // Degenerate case: no nested funcs at all
  5468. return;
  5469. }
  5470. if (scopeObj->HasCachedFuncs())
  5471. {
  5472. for (uint i = 0; i < funcCount; i++)
  5473. {
  5474. const FuncCacheEntry *entry = scopeObj->GetFuncCacheEntry(i);
  5475. ScriptFunction *func = entry->func;
  5476. FunctionProxy * proxy = func->GetFunctionProxy();
  5477. // Reset the function's type to the default type with no properties
  5478. // Use the cached type on the function proxy rather than the type in the func cache entry
  5479. // CONSIDER: Stop caching the function types in the scope object
  5480. func->ReplaceType(proxy->EnsureDeferredPrototypeType());
  5481. func->ResetConstructorCacheToDefault();
  5482. uint scopeSlot = info->elements[i].scopeSlot;
  5483. if (scopeSlot != Constants::NoProperty)
  5484. {
  5485. // CONSIDER: Store property IDs in FuncInfoArray in debug builds so we can properly assert in SetAuxSlot
  5486. scopeObj->SetAuxSlot(SetSlotArguments(Constants::NoProperty, scopeSlot, entry->func));
  5487. }
  5488. }
  5489. return;
  5490. }
  5491. // No cached functions, so create them and cache them.
  5492. JavascriptFunction *funcParent = scopeObj->GetParentFunc();
  5493. for (uint i = 0; i < funcCount; i++)
  5494. {
  5495. const FuncInfoEntry *entry = &info->elements[i];
  5496. uint nestedIndex = entry->nestedIndex;
  5497. uint scopeSlot = entry->scopeSlot;
  5498. FunctionProxy * proxy = funcParent->GetFunctionBody()->GetNestedFunctionProxy(nestedIndex);
  5499. ScriptFunction *func = scriptContext->GetLibrary()->CreateScriptFunction(proxy);
  5500. func->SetEnvironment(pDisplay);
  5501. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_FUNCTION(func, EtwTrace::GetFunctionId(proxy)));
  5502. scopeObj->SetCachedFunc(i, func);
  5503. if (scopeSlot != Constants::NoProperty)
  5504. {
  5505. // CONSIDER: Store property IDs in FuncInfoArray in debug builds so we can properly assert in SetAuxSlot
  5506. scopeObj->SetAuxSlot(SetSlotArguments(Constants::NoProperty, scopeSlot, func));
  5507. }
  5508. }
  5509. JIT_HELPER_END(OP_InitCachedFuncs);
  5510. }
  5511. Var JavascriptOperators::AddVarsToArraySegment(SparseArraySegment<Var> * segment, const Js::VarArray *vars)
  5512. {
  5513. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ArraySegmentVars);
  5514. uint32 count = vars->count;
  5515. Assert(segment->left == 0);
  5516. Assert(count <= segment->size);
  5517. if(count > segment->length)
  5518. {
  5519. segment->length = count;
  5520. segment->CheckLengthvsSize();
  5521. }
  5522. CopyArray(segment->elements, segment->length, vars->elements, count);
  5523. return segment;
  5524. JIT_HELPER_END(ArraySegmentVars);
  5525. }
  5526. void JavascriptOperators::AddIntsToArraySegment(SparseArraySegment<int32> * segment, const Js::AuxArray<int32> *ints)
  5527. {
  5528. uint32 count = ints->count;
  5529. Assert(segment->left == 0);
  5530. Assert(count <= segment->size);
  5531. if(count > segment->length)
  5532. {
  5533. segment->length = count;
  5534. segment->CheckLengthvsSize();
  5535. }
  5536. js_memcpy_s(segment->elements, sizeof(int32) * segment->length, ints->elements, sizeof(int32) * count);
  5537. }
  5538. void JavascriptOperators::AddFloatsToArraySegment(SparseArraySegment<double> * segment, const Js::AuxArray<double> *doubles)
  5539. {
  5540. uint32 count = doubles->count;
  5541. Assert(segment->left == 0);
  5542. Assert(count <= segment->size);
  5543. if(count > segment->length)
  5544. {
  5545. segment->length = count;
  5546. segment->CheckLengthvsSize();
  5547. }
  5548. js_memcpy_s(segment->elements, sizeof(double) * segment->length, doubles->elements, sizeof(double) * count);
  5549. }
  5550. RecyclableObject * JavascriptOperators::GetPrototypeObject(RecyclableObject * constructorFunction, ScriptContext * scriptContext)
  5551. {
  5552. Var prototypeProperty = JavascriptOperators::GetProperty(constructorFunction, PropertyIds::prototype, scriptContext);
  5553. RecyclableObject* prototypeObject;
  5554. PrototypeObject(prototypeProperty, constructorFunction, scriptContext, &prototypeObject);
  5555. return prototypeObject;
  5556. }
  5557. RecyclableObject * JavascriptOperators::GetPrototypeObjectForConstructorCache(RecyclableObject * constructor, ScriptContext* requestContext, bool& canBeCached)
  5558. {
  5559. PropertyValueInfo info;
  5560. Var prototypeValue;
  5561. RecyclableObject* prototypeObject;
  5562. canBeCached = false;
  5563. // Do a local property lookup. Since a function's prototype property is a non-configurable data property, we don't need to worry
  5564. // about the prototype being an accessor property, whose getter returns different values every time it's called.
  5565. if (constructor->GetProperty(constructor, PropertyIds::prototype, &prototypeValue, &info, requestContext))
  5566. {
  5567. if (!JavascriptOperators::PrototypeObject(prototypeValue, constructor, requestContext, &prototypeObject))
  5568. {
  5569. // The value returned by the property lookup is not a valid prototype object, default to object prototype.
  5570. Assert(prototypeObject == constructor->GetLibrary()->GetObjectPrototype());
  5571. }
  5572. // For these scenarios, we do not want to populate the cache.
  5573. if (constructor->GetScriptContext() != requestContext || info.GetInstance() != constructor)
  5574. {
  5575. return prototypeObject;
  5576. }
  5577. }
  5578. else
  5579. {
  5580. // It's ok to cache Object.prototype, because Object.prototype cannot be overwritten.
  5581. prototypeObject = constructor->GetLibrary()->GetObjectPrototype();
  5582. }
  5583. canBeCached = true;
  5584. return prototypeObject;
  5585. }
  5586. bool JavascriptOperators::PrototypeObject(Var prototypeProperty, RecyclableObject * constructorFunction, ScriptContext * scriptContext, RecyclableObject** prototypeObject)
  5587. {
  5588. TypeId prototypeType = JavascriptOperators::GetTypeId(prototypeProperty);
  5589. if (JavascriptOperators::IsObjectType(prototypeType))
  5590. {
  5591. *prototypeObject = VarTo<RecyclableObject>(prototypeProperty);
  5592. return true;
  5593. }
  5594. *prototypeObject = constructorFunction->GetLibrary()->GetObjectPrototype();
  5595. return false;
  5596. }
  5597. FunctionInfo* JavascriptOperators::GetConstructorFunctionInfo(Var instance, ScriptContext * scriptContext)
  5598. {
  5599. TypeId typeId = JavascriptOperators::GetTypeId(instance);
  5600. if (typeId == TypeIds_Function)
  5601. {
  5602. JavascriptFunction * function = UnsafeVarTo<JavascriptFunction>(instance);
  5603. return function->GetFunctionInfo();
  5604. }
  5605. if (typeId != TypeIds_HostDispatch && typeId != TypeIds_Proxy)
  5606. {
  5607. if (typeId == TypeIds_Null)
  5608. {
  5609. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  5610. }
  5611. JavascriptError::ThrowTypeError(scriptContext, VBSERR_ActionNotSupported);
  5612. }
  5613. return nullptr;
  5614. }
  5615. Var JavascriptOperators::NewJavascriptObjectNoArg(ScriptContext* requestContext)
  5616. {
  5617. JIT_HELPER_NOT_REENTRANT_HEADER(NewJavascriptObjectNoArg, reentrancylock, requestContext->GetThreadContext());
  5618. DynamicObject * newObject = requestContext->GetLibrary()->CreateObject(true);
  5619. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newObject));
  5620. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5621. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5622. {
  5623. newObject = VarTo<DynamicObject>(JavascriptProxy::AutoProxyWrapper(newObject));
  5624. }
  5625. #endif
  5626. return newObject;
  5627. JIT_HELPER_END(NewJavascriptObjectNoArg);
  5628. }
  5629. Var JavascriptOperators::NewJavascriptArrayNoArg(ScriptContext* requestContext)
  5630. {
  5631. JIT_HELPER_NOT_REENTRANT_HEADER(NewJavascriptArrayNoArg, reentrancylock, requestContext->GetThreadContext());
  5632. JavascriptArray * newArray = requestContext->GetLibrary()->CreateArray();
  5633. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newArray));
  5634. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5635. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5636. {
  5637. newArray = static_cast<JavascriptArray*>(JavascriptProxy::AutoProxyWrapper(newArray));
  5638. }
  5639. #endif
  5640. return newArray;
  5641. JIT_HELPER_END(NewJavascriptArrayNoArg);
  5642. }
  5643. Var JavascriptOperators::NewScObjectNoArgNoCtorFull(Var instance, ScriptContext* requestContext)
  5644. {
  5645. // This helper can be reentrant because although we don't call the Constructor, we might have to parse it if bytecode is missing
  5646. // In which case, we would leave script. When we leave script we DisposeObjects which can dispose of Edge objects that could
  5647. // have a javascript onDispose handler and call that handler.
  5648. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoArgNoCtorFull);
  5649. return NewScObjectNoArgNoCtorCommon(instance, requestContext, true);
  5650. JIT_HELPER_END(NewScObjectNoArgNoCtorFull);
  5651. }
  5652. Var JavascriptOperators::NewScObjectNoArgNoCtor(Var instance, ScriptContext* requestContext)
  5653. {
  5654. // This helper can be reentrant because although we don't call the Constructor, we might have to parse it if bytecode is missing
  5655. // In which case, we would leave script. When we leave script we DisposeObjects which can dispose of Edge objects that could
  5656. // have a javascript onDispose handler and call that handler.
  5657. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoArgNoCtor);
  5658. return NewScObjectNoArgNoCtorCommon(instance, requestContext, false);
  5659. JIT_HELPER_END(NewScObjectNoArgNoCtor);
  5660. }
  5661. Var JavascriptOperators::NewScObjectNoArgNoCtorCommon(Var instance, ScriptContext* requestContext, bool isBaseClassConstructorNewScObject)
  5662. {
  5663. RecyclableObject * object = VarTo<RecyclableObject>(instance);
  5664. FunctionInfo* functionInfo = JavascriptOperators::GetConstructorFunctionInfo(instance, requestContext);
  5665. Assert(functionInfo != &JavascriptObject::EntryInfo::NewInstance); // built-ins are not inlined
  5666. Assert(functionInfo != &JavascriptArray::EntryInfo::NewInstance); // built-ins are not inlined
  5667. return functionInfo != nullptr ?
  5668. JavascriptOperators::NewScObjectCommon(object, functionInfo, requestContext, isBaseClassConstructorNewScObject) :
  5669. JavascriptOperators::NewScObjectHostDispatchOrProxy(object, requestContext);
  5670. }
  5671. Var JavascriptOperators::NewScObjectNoArg(Var instance, ScriptContext * requestContext)
  5672. {
  5673. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoArg);
  5674. JavascriptProxy * proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  5675. if (proxy)
  5676. {
  5677. Var dummy = nullptr;
  5678. Arguments args(CallInfo(CallFlags_New, 1), &dummy);
  5679. return requestContext->GetThreadContext()->ExecuteImplicitCall(proxy, Js::ImplicitCall_Accessor, [=]()->Js::Var
  5680. {
  5681. return proxy->ConstructorTrap(args, requestContext, 0);
  5682. });
  5683. }
  5684. FunctionInfo* functionInfo = JavascriptOperators::GetConstructorFunctionInfo(instance, requestContext);
  5685. RecyclableObject * object = VarTo<RecyclableObject>(instance);
  5686. if (functionInfo == &JavascriptObject::EntryInfo::NewInstance)
  5687. {
  5688. // Fast path for new Object()
  5689. Assert((functionInfo->GetAttributes() & FunctionInfo::ErrorOnNew) == 0);
  5690. JavascriptLibrary* library = object->GetLibrary();
  5691. DynamicObject * newObject = library->CreateObject(true);
  5692. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newObject));
  5693. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5694. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5695. {
  5696. newObject = VarTo<DynamicObject>(JavascriptProxy::AutoProxyWrapper(newObject));
  5697. }
  5698. #endif
  5699. #if DBG
  5700. DynamicType* newObjectType = newObject->GetDynamicType();
  5701. Assert(newObjectType->GetIsShared());
  5702. JavascriptFunction* constructor = VarTo<JavascriptFunction>(instance);
  5703. Assert(!constructor->GetConstructorCache()->NeedsUpdateAfterCtor());
  5704. #endif
  5705. ScriptContext * scriptContext = library->GetScriptContext();
  5706. if (scriptContext != requestContext)
  5707. {
  5708. CrossSite::MarshalDynamicObjectAndPrototype(requestContext, newObject);
  5709. }
  5710. return newObject;
  5711. }
  5712. else if (functionInfo == &JavascriptArray::EntryInfo::NewInstance)
  5713. {
  5714. Assert((functionInfo->GetAttributes() & FunctionInfo::ErrorOnNew) == 0);
  5715. JavascriptLibrary* library = object->GetLibrary();
  5716. JavascriptArray * newArray = library->CreateArray();
  5717. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newArray));
  5718. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5719. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5720. {
  5721. newArray = static_cast<JavascriptArray*>(JavascriptProxy::AutoProxyWrapper(newArray));
  5722. }
  5723. #endif
  5724. #if DBG
  5725. DynamicType* newArrayType = newArray->GetDynamicType();
  5726. Assert(newArrayType->GetIsShared());
  5727. JavascriptFunction* constructor = VarTo<JavascriptFunction>(instance);
  5728. Assert(!constructor->GetConstructorCache()->NeedsUpdateAfterCtor());
  5729. #endif
  5730. ScriptContext * scriptContext = library->GetScriptContext();
  5731. if (scriptContext != requestContext)
  5732. {
  5733. CrossSite::MarshalDynamicObjectAndPrototype(requestContext, newArray);
  5734. }
  5735. return newArray;
  5736. }
  5737. Var newObject = functionInfo != nullptr ?
  5738. JavascriptOperators::NewScObjectCommon(object, functionInfo, requestContext) :
  5739. JavascriptOperators::NewScObjectHostDispatchOrProxy(object, requestContext);
  5740. ThreadContext * threadContext = object->GetScriptContext()->GetThreadContext();
  5741. Var returnVar = threadContext->ExecuteImplicitCall(object, Js::ImplicitCall_Accessor, [=]()->Js::Var
  5742. {
  5743. return CALL_FUNCTION(threadContext, object, CallInfo(CallFlags_New, 1), newObject);
  5744. });
  5745. if (JavascriptOperators::IsObject(returnVar))
  5746. {
  5747. newObject = returnVar;
  5748. }
  5749. ConstructorCache * constructorCache = nullptr;
  5750. JavascriptFunction *function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  5751. if (function)
  5752. {
  5753. constructorCache = function->GetConstructorCache();
  5754. }
  5755. if (constructorCache != nullptr && constructorCache->NeedsUpdateAfterCtor())
  5756. {
  5757. JavascriptOperators::UpdateNewScObjectCache(object, newObject, requestContext);
  5758. }
  5759. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5760. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5761. {
  5762. DynamicObject* newDynamicObject = VarTo<DynamicObject>(JavascriptProxy::AutoProxyWrapper(newObject));
  5763. // this might come from a different scriptcontext.
  5764. newObject = CrossSite::MarshalVar(requestContext, newDynamicObject, newDynamicObject->GetScriptContext());
  5765. }
  5766. #endif
  5767. return newObject;
  5768. JIT_HELPER_END(NewScObjectNoArg);
  5769. }
  5770. Var JavascriptOperators::NewScObjectNoCtorFull(Var instance, ScriptContext* requestContext)
  5771. {
  5772. // This helper can be reentrant because although we don't call the Constructor, we might have to parse it if bytecode is missing
  5773. // In which case, we would leave script. When we leave script we DisposeObjects which can dispose of Edge objects that could
  5774. // have a javascript onDispose handler and call that handler.
  5775. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoCtorFull);
  5776. return NewScObjectNoCtorCommon(instance, requestContext, true);
  5777. JIT_HELPER_END(NewScObjectNoCtorFull);
  5778. }
  5779. Var JavascriptOperators::NewScObjectNoCtor(Var instance, ScriptContext * requestContext)
  5780. {
  5781. // This helper can be reentrant because although we don't call the Constructor, we might have to parse it if bytecode is missing
  5782. // In which case, we would leave script. When we leave script we DisposeObjects which can dispose of Edge objects that could
  5783. // have a javascript onDispose handler and call that handler.
  5784. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoCtor);
  5785. // We can still call into NewScObjectNoCtor variations in JIT code for performance; however for proxy we don't
  5786. // really need the new object as the trap will handle the "this" pointer separately. pass back nullptr to ensure
  5787. // failure in invalid case.
  5788. return (VarIs<JavascriptProxy>(instance)) ? nullptr : NewScObjectNoCtorCommon(instance, requestContext, false);
  5789. JIT_HELPER_END(NewScObjectNoCtor);
  5790. }
  5791. Var JavascriptOperators::NewScObjectNoCtorCommon(Var instance, ScriptContext* requestContext, bool isBaseClassConstructorNewScObject)
  5792. {
  5793. FunctionInfo* functionInfo = JavascriptOperators::GetConstructorFunctionInfo(instance, requestContext);
  5794. if (functionInfo)
  5795. {
  5796. return JavascriptOperators::NewScObjectCommon(UnsafeVarTo<RecyclableObject>(instance), functionInfo, requestContext, isBaseClassConstructorNewScObject);
  5797. }
  5798. else
  5799. {
  5800. return JavascriptOperators::NewScObjectHostDispatchOrProxy(VarTo<RecyclableObject>(instance), requestContext);
  5801. }
  5802. }
  5803. Var JavascriptOperators::NewScObjectHostDispatchOrProxy(RecyclableObject * function, ScriptContext * requestContext)
  5804. {
  5805. ScriptContext* functionScriptContext = function->GetScriptContext();
  5806. RecyclableObject * prototype = JavascriptOperators::GetPrototypeObject(function, functionScriptContext);
  5807. prototype = VarTo<RecyclableObject>(CrossSite::MarshalVar(requestContext, prototype, functionScriptContext));
  5808. Var object = requestContext->GetLibrary()->CreateObject(prototype);
  5809. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(object));
  5810. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5811. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5812. {
  5813. object = VarTo<DynamicObject>(JavascriptProxy::AutoProxyWrapper(object));
  5814. }
  5815. #endif
  5816. return object;
  5817. }
  5818. Var JavascriptOperators::NewScObjectCommon(RecyclableObject * function, FunctionInfo* functionInfo, ScriptContext * requestContext, bool isBaseClassConstructorNewScObject)
  5819. {
  5820. // CONSIDER: Allow for the cache to be repopulated if the type got collected, and a new one got populated with
  5821. // the same number of inlined slots. This requires that the JIT-ed code actually load the type from the cache
  5822. // (instead of hard-coding it), but it can (and must) keep the hard-coded number of inline slots.
  5823. // CONSIDER: Consider also not pinning the type in the cache. This can be done by using a registration based
  5824. // weak reference (we need to control the memory address), which we don't yet have, or by allocating the cache from
  5825. // the inline cache arena to allow it to be zeroed, but retain a recycler-allocated portion to hold on to the size of
  5826. // inlined slots.
  5827. JavascriptFunction* constructor = UnsafeVarTo<JavascriptFunction>(function);
  5828. if (functionInfo->IsClassConstructor() && !isBaseClassConstructorNewScObject)
  5829. {
  5830. // If we are calling new on a class constructor, the contract is that we pass new.target as the 'this' argument.
  5831. // function is the constructor on which we called new - which is new.target.
  5832. // If we are trying to construct the object for a base class constructor as part of a super call, we should not
  5833. // store new.target in the 'this' argument.
  5834. return function;
  5835. }
  5836. ConstructorCache* constructorCache = constructor->GetConstructorCache();
  5837. AssertMsg(constructorCache->GetScriptContext() == nullptr || constructorCache->GetScriptContext() == constructor->GetScriptContext(),
  5838. "Why did we populate a constructor cache with a mismatched script context?");
  5839. Assert(constructorCache != nullptr);
  5840. DynamicType* type = constructorCache->GetGuardValueAsType();
  5841. if (type != nullptr && constructorCache->GetScriptContext() == requestContext)
  5842. {
  5843. #if DBG
  5844. bool cachedProtoCanBeCached;
  5845. Assert(type->GetPrototype() == JavascriptOperators::GetPrototypeObjectForConstructorCache(constructor, requestContext, cachedProtoCanBeCached));
  5846. Assert(cachedProtoCanBeCached);
  5847. Assert(type->GetIsShared());
  5848. #endif
  5849. #if DBG_DUMP
  5850. TraceUseConstructorCache(constructorCache, constructor, true);
  5851. #endif
  5852. Var object = DynamicObject::New(requestContext->GetRecycler(), type);
  5853. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(object));
  5854. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5855. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5856. {
  5857. object = VarTo<DynamicObject>(JavascriptProxy::AutoProxyWrapper(object));
  5858. }
  5859. #endif
  5860. return object;
  5861. }
  5862. if (constructorCache->SkipDefaultNewObject())
  5863. {
  5864. Assert(!constructorCache->NeedsUpdateAfterCtor());
  5865. #if DBG_DUMP
  5866. TraceUseConstructorCache(constructorCache, constructor, true);
  5867. #endif
  5868. if (isBaseClassConstructorNewScObject)
  5869. {
  5870. return JavascriptOperators::CreateFromConstructor(function, requestContext);
  5871. }
  5872. return nullptr;
  5873. }
  5874. #if DBG_DUMP
  5875. TraceUseConstructorCache(constructorCache, constructor, false);
  5876. #endif
  5877. ScriptContext* constructorScriptContext = function->GetScriptContext();
  5878. Assert(!constructorScriptContext->GetThreadContext()->IsDisableImplicitException());
  5879. // we shouldn't try to call the constructor if it's closed already.
  5880. constructorScriptContext->VerifyAlive(TRUE, requestContext);
  5881. FunctionInfo::Attributes attributes = functionInfo->GetAttributes();
  5882. if (attributes & FunctionInfo::ErrorOnNew)
  5883. {
  5884. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnNew);
  5885. }
  5886. // Slow path
  5887. FunctionProxy * ctorProxy = constructor->GetFunctionProxy();
  5888. FunctionBody * functionBody = ctorProxy != nullptr ? ctorProxy->EnsureDeserialized()->Parse() : nullptr;
  5889. if (attributes & FunctionInfo::SkipDefaultNewObject)
  5890. {
  5891. // The constructor doesn't use the default new object.
  5892. #pragma prefast(suppress:6236, "DevDiv bug 830883. False positive when PHASE_OFF is #defined as '(false)'.")
  5893. if (!PHASE_OFF1(ConstructorCachePhase) && (functionBody == nullptr || !PHASE_OFF(ConstructorCachePhase, functionBody)))
  5894. {
  5895. constructorCache = constructor->EnsureValidConstructorCache();
  5896. constructorCache->PopulateForSkipDefaultNewObject(constructorScriptContext);
  5897. #if DBG_DUMP
  5898. if ((functionBody != nullptr && PHASE_TRACE(Js::ConstructorCachePhase, functionBody)) || (functionBody == nullptr && PHASE_TRACE1(Js::ConstructorCachePhase)))
  5899. {
  5900. const char16* ctorName = functionBody != nullptr ? functionBody->GetDisplayName() : _u("<unknown>");
  5901. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5902. Output::Print(_u("CtorCache: populated cache (0x%p) for ctor %s (%s): "), constructorCache, ctorName,
  5903. functionBody ? functionBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"));
  5904. constructorCache->Dump();
  5905. Output::Print(_u("\n"));
  5906. Output::Flush();
  5907. }
  5908. #endif
  5909. }
  5910. Assert(!constructorCache->NeedsUpdateAfterCtor());
  5911. return nullptr;
  5912. }
  5913. // CONSIDER: Create some form of PatchGetProtoObjForCtorCache, which actually caches the prototype object in the constructor cache.
  5914. // 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
  5915. // after we fail the guard check. When invalidating the cache for proto change, make sure we zap the prototype field of the cache in
  5916. // addition to the guard value.
  5917. bool prototypeCanBeCached;
  5918. RecyclableObject* prototype = JavascriptOperators::GetPrototypeObjectForConstructorCache(
  5919. function, constructorScriptContext, prototypeCanBeCached);
  5920. prototype = VarTo<RecyclableObject>(CrossSite::MarshalVar(requestContext,
  5921. prototype, constructorScriptContext));
  5922. DynamicObject* newObject = requestContext->GetLibrary()->CreateObject(prototype, 8);
  5923. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newObject));
  5924. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5925. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5926. {
  5927. newObject = VarTo<DynamicObject>(JavascriptProxy::AutoProxyWrapper(newObject));
  5928. }
  5929. #endif
  5930. Assert(newObject->GetTypeHandler()->GetPropertyCount() == 0);
  5931. if (prototypeCanBeCached && functionBody != nullptr && requestContext == constructorScriptContext &&
  5932. !Js::VarIs<Js::JavascriptProxy>(newObject) &&
  5933. !PHASE_OFF1(ConstructorCachePhase) && !PHASE_OFF(ConstructorCachePhase, functionBody))
  5934. {
  5935. DynamicType* newObjectType = newObject->GetDynamicType();
  5936. // Initial type (without any properties) should always be shared up-front. This allows us to populate the cache right away.
  5937. Assert(newObjectType->GetIsShared());
  5938. // Populate the cache here and set the updateAfterCtor flag. This way, if the ctor is called recursively the
  5939. // recursive calls will hit the cache and use the initial type. On the unwind path, we will update the cache
  5940. // after the innermost ctor and clear the flag. After subsequent ctors we won't attempt an update anymore.
  5941. // As long as the updateAfterCtor flag is set it is safe to update the cache, because it would not have been
  5942. // hard-coded in the JIT-ed code.
  5943. constructorCache = constructor->EnsureValidConstructorCache();
  5944. constructorCache->Populate(newObjectType, constructorScriptContext, functionBody->GetHasNoExplicitReturnValue(), true);
  5945. Assert(constructorCache->IsConsistent());
  5946. #if DBG_DUMP
  5947. if ((functionBody != nullptr && PHASE_TRACE(Js::ConstructorCachePhase, functionBody)) || (functionBody == nullptr && PHASE_TRACE1(Js::ConstructorCachePhase)))
  5948. {
  5949. const char16* ctorName = functionBody != nullptr ? functionBody->GetDisplayName() : _u("<unknown>");
  5950. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5951. Output::Print(_u("CtorCache: populated cache (0x%p) for ctor %s (%s): "), constructorCache, ctorName,
  5952. functionBody ? functionBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"));
  5953. constructorCache->Dump();
  5954. Output::Print(_u("\n"));
  5955. Output::Flush();
  5956. }
  5957. #endif
  5958. }
  5959. else
  5960. {
  5961. #if DBG_DUMP
  5962. if ((functionBody != nullptr && PHASE_TRACE(Js::ConstructorCachePhase, functionBody)) || (functionBody == nullptr && PHASE_TRACE1(Js::ConstructorCachePhase)))
  5963. {
  5964. const char16* ctorName = functionBody != nullptr ? functionBody->GetDisplayName() : _u("<unknown>");
  5965. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5966. 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"),
  5967. constructorCache, ctorName, functionBody ? functionBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"),
  5968. !prototypeCanBeCached ? _u("prototype cannot be cached") :
  5969. functionBody == nullptr ? _u("function has no body") :
  5970. requestContext != constructorScriptContext ? _u("of cross-context call") : _u("constructor cache phase is off"),
  5971. prototype, functionBody, constructorScriptContext, requestContext);
  5972. Output::Print(_u("\n"));
  5973. Output::Flush();
  5974. }
  5975. #endif
  5976. }
  5977. return newObject;
  5978. }
  5979. void JavascriptOperators::UpdateNewScObjectCache(Var function, Var instance, ScriptContext* requestContext)
  5980. {
  5981. JIT_HELPER_NOT_REENTRANT_HEADER(UpdateNewScObjectCache, reentrancylock, requestContext->GetThreadContext());
  5982. JavascriptFunction* constructor = VarTo<JavascriptFunction>(function);
  5983. if(constructor->GetScriptContext() != requestContext)
  5984. {
  5985. // The cache is populated only when the constructor function's context is the same as the calling context. However,
  5986. // the cached type is not finalized yet and may not be until multiple calls to the constructor have been made (see
  5987. // flag ConstructorCallsRequiredToFinalizeCachedType). A subsequent call to the constructor may be made from a
  5988. // different context, so ignore those cross-context calls and wait for the constructor to be called from its own
  5989. // context again to finalize the cached type.
  5990. return;
  5991. }
  5992. // Review : What happens if the cache got invalidated between NewScObject and here?
  5993. // Should we allocate new? Should we mark it as polymorphic?
  5994. ConstructorCache* constructorCache = constructor->GetConstructorCache();
  5995. Assert(constructorCache->IsConsistent());
  5996. Assert(!ConstructorCache::IsDefault(constructorCache));
  5997. AssertMsg(constructorCache->GetScriptContext() == constructor->GetScriptContext(), "Why did we populate a constructor cache with a mismatched script context?");
  5998. AssertMsg(constructorCache->IsPopulated(), "Why are we updating a constructor cache that hasn't been populated?");
  5999. // The presence of the updateAfterCtor flag guarantees that this cache hasn't been used in JIT-ed fast path. Even, if the
  6000. // cache is invalidated, this flag is not changed.
  6001. AssertMsg(constructorCache->NeedsUpdateAfterCtor(), "Why are we updating a constructor cache that doesn't need to be updated?");
  6002. const bool finalizeCachedType =
  6003. constructorCache->CallCount() >= CONFIG_FLAG(ConstructorCallsRequiredToFinalizeCachedType);
  6004. if(!finalizeCachedType)
  6005. {
  6006. constructorCache->IncCallCount();
  6007. }
  6008. else
  6009. {
  6010. constructorCache->ClearUpdateAfterCtor();
  6011. }
  6012. FunctionBody* constructorBody = constructor->GetFunctionBody();
  6013. AssertMsg(constructorBody != nullptr, "Constructor function doesn't have a function body.");
  6014. Assert(VarIs<RecyclableObject>(instance));
  6015. // The cache might have been invalidated between NewScObjectCommon and UpdateNewScObjectCache. This could occur, for example, if
  6016. // the constructor updates its own prototype property. If that happens we don't want to re-populate it here. A new cache will
  6017. // be created when the constructor is called again.
  6018. if (constructorCache->IsInvalidated())
  6019. {
  6020. #if DBG_DUMP
  6021. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because cache is invalidated"));
  6022. #endif
  6023. return;
  6024. }
  6025. Assert(constructorCache->GetGuardValueAsType() != nullptr);
  6026. if (DynamicType::Is(VarTo<RecyclableObject>(instance)->GetTypeId()))
  6027. {
  6028. DynamicObject *object = UnsafeVarTo<DynamicObject>(instance);
  6029. DynamicType* type = object->GetDynamicType();
  6030. DynamicTypeHandler* typeHandler = type->GetTypeHandler();
  6031. if (constructorBody->GetHasOnlyThisStmts())
  6032. {
  6033. if (!typeHandler->IsSharable())
  6034. {
  6035. // Dynamic type created is not sharable.
  6036. // So in future don't try to check for "this assignment optimization".
  6037. constructorBody->SetHasOnlyThisStmts(false);
  6038. #if DBG_DUMP
  6039. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because final type is not shareable"));
  6040. #endif
  6041. }
  6042. else if (typeHandler->GetPropertyCount() >= Js::PropertyIndexRanges<PropertyIndex>::MaxValue)
  6043. {
  6044. // Dynamic type created has too many properties.
  6045. // So in future don't try to check for "this assignment optimization".
  6046. constructorBody->SetHasOnlyThisStmts(false);
  6047. #if DBG_DUMP
  6048. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because final type has too many properties"));
  6049. #endif
  6050. }
  6051. else
  6052. {
  6053. #if DBG
  6054. bool cachedProtoCanBeCached = false;
  6055. Assert(type->GetPrototype() == JavascriptOperators::GetPrototypeObjectForConstructorCache(constructor, requestContext, cachedProtoCanBeCached));
  6056. Assert(cachedProtoCanBeCached);
  6057. Assert(type->GetScriptContext() == constructorCache->GetScriptContext());
  6058. Assert(type->GetPrototype() == constructorCache->GetType()->GetPrototype());
  6059. #endif
  6060. typeHandler->SetMayBecomeShared();
  6061. // CONSIDER: Remove only this for delayed type sharing.
  6062. type->ShareType();
  6063. #if ENABLE_PROFILE_INFO
  6064. DynamicProfileInfo* profileInfo = constructorBody->HasDynamicProfileInfo() ? constructorBody->GetAnyDynamicProfileInfo() : nullptr;
  6065. if ((profileInfo != nullptr && profileInfo->GetImplicitCallFlags() <= ImplicitCall_None) ||
  6066. CheckIfPrototypeChainHasOnlyWritableDataProperties(type->GetPrototype()))
  6067. {
  6068. for (PropertyIndex pi = 0; pi < typeHandler->GetPropertyCount(); pi++)
  6069. {
  6070. requestContext->RegisterConstructorCache(typeHandler->GetPropertyId(requestContext, pi), constructorCache);
  6071. }
  6072. Assert(constructorBody->GetUtf8SourceInfo()->GetIsLibraryCode() || !constructor->GetScriptContext()->IsScriptContextInDebugMode());
  6073. if (constructorCache->TryUpdateAfterConstructor(type, constructor->GetScriptContext()))
  6074. {
  6075. #if DBG_DUMP
  6076. TraceUpdateConstructorCache(constructorCache, constructorBody, true, _u(""));
  6077. #endif
  6078. }
  6079. else
  6080. {
  6081. #if DBG_DUMP
  6082. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because number of slots > MaxCachedSlotCount"));
  6083. #endif
  6084. }
  6085. }
  6086. #if DBG_DUMP
  6087. else
  6088. {
  6089. if (profileInfo &&
  6090. ((profileInfo->GetImplicitCallFlags() & ~(Js::ImplicitCall_External | Js::ImplicitCall_Accessor)) == 0) &&
  6091. profileInfo != nullptr && CheckIfPrototypeChainHasOnlyWritableDataProperties(type->GetPrototype()) &&
  6092. Js::Configuration::Global.flags.Trace.IsEnabled(Js::HostOptPhase))
  6093. {
  6094. const char16* ctorName = constructorBody->GetDisplayName();
  6095. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  6096. Output::Print(_u("CtorCache: %s cache (0x%p) for ctor %s (#%u) did not update because external call"),
  6097. constructorCache, constructorBody, ctorName, constructorBody ? constructorBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"));
  6098. Output::Print(_u("\n"));
  6099. Output::Flush();
  6100. }
  6101. }
  6102. #endif
  6103. #endif
  6104. }
  6105. }
  6106. else
  6107. {
  6108. #if DBG_DUMP
  6109. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because ctor has not only this statements"));
  6110. #endif
  6111. }
  6112. }
  6113. else
  6114. {
  6115. // Even though this constructor apparently returned something other than the default object we created,
  6116. // it still makes sense to cache the parameters of the default object, since we must create it every time, anyway.
  6117. #if DBG_DUMP
  6118. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because ctor return a non-object value"));
  6119. #endif
  6120. return;
  6121. }
  6122. // Whatever the constructor returned, if we're caching a type we want to be sure we shrink its inline slot capacity.
  6123. if (finalizeCachedType && constructorCache->IsEnabled())
  6124. {
  6125. DynamicType* cachedType = constructorCache->NeedsTypeUpdate() ? constructorCache->GetPendingType() : constructorCache->GetType();
  6126. DynamicTypeHandler* cachedTypeHandler = cachedType->GetTypeHandler();
  6127. // Consider: We could delay inline slot capacity shrinking until the second time this constructor is invoked. In some cases
  6128. // this might permit more properties to remain inlined if the objects grow after constructor. This would require flagging
  6129. // the cache as special (already possible) and forcing the shrinking during work item creation if we happen to JIT this
  6130. // constructor while the cache is in this special state.
  6131. if (cachedTypeHandler->GetInlineSlotCapacity())
  6132. {
  6133. #if DBG_DUMP
  6134. int inlineSlotCapacityBeforeShrink = cachedTypeHandler->GetInlineSlotCapacity();
  6135. #endif
  6136. // Note that after the cache has been updated and might have been used in the JIT-ed code, it is no longer legal to
  6137. // shrink the inline slot capacity of the type. That's because we allocate memory for a fixed number of inlined properties
  6138. // and if that number changed on the type, this update wouldn't get reflected in JIT-ed code and we would allocate objects
  6139. // of a wrong size. This could conceivably happen if the original object got collected, and with it some of the successor
  6140. // types also. If then another constructor has the same prototype and needs to populate its own cache, it would attempt to
  6141. // shrink inlined slots again. If all surviving type handlers have smaller inline slot capacity, we would shrink it further.
  6142. // To address this problem the type handler has a bit indicating its inline slots have been shrunk already. If that bit is
  6143. // set ShrinkSlotAndInlineSlotCapacity does nothing.
  6144. cachedTypeHandler->ShrinkSlotAndInlineSlotCapacity();
  6145. constructorCache->UpdateInlineSlotCount();
  6146. #if DBG_DUMP
  6147. Assert(inlineSlotCapacityBeforeShrink >= cachedTypeHandler->GetInlineSlotCapacity());
  6148. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::InlineSlotsPhase))
  6149. {
  6150. if (inlineSlotCapacityBeforeShrink != cachedTypeHandler->GetInlineSlotCapacity())
  6151. {
  6152. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  6153. Output::Print(_u("Inline slot capacity shrunk: Function:%04s Before:%d After:%d\n"),
  6154. constructorBody->GetDebugNumberSet(debugStringBuffer), inlineSlotCapacityBeforeShrink, cachedTypeHandler->GetInlineSlotCapacity());
  6155. }
  6156. }
  6157. #endif
  6158. }
  6159. }
  6160. JIT_HELPER_END(UpdateNewScObjectCache);
  6161. }
  6162. void JavascriptOperators::TraceUseConstructorCache(const ConstructorCache* ctorCache, const JavascriptFunction* ctor, bool isHit)
  6163. {
  6164. #if DBG_DUMP
  6165. // We are under debug, so we can incur the extra check here.
  6166. FunctionProxy* ctorBody = ctor->GetFunctionProxy();
  6167. if (ctorBody != nullptr && !ctorBody->GetScriptContext()->IsClosed())
  6168. {
  6169. ctorBody = ctorBody->EnsureDeserialized();
  6170. }
  6171. if ((ctorBody != nullptr && PHASE_TRACE(Js::ConstructorCachePhase, ctorBody)) || (ctorBody == nullptr && PHASE_TRACE1(Js::ConstructorCachePhase)))
  6172. {
  6173. const char16* ctorName = ctorBody != nullptr ? ctorBody->GetDisplayName() : _u("<unknown>");
  6174. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  6175. Output::Print(_u("CtorCache: %s cache (0x%p) for ctor %s (%s): "), isHit ? _u("hit") : _u("missed"), ctorCache, ctorName,
  6176. ctorBody ? ctorBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"));
  6177. ctorCache->Dump();
  6178. Output::Print(_u("\n"));
  6179. Output::Flush();
  6180. }
  6181. #endif
  6182. }
  6183. void JavascriptOperators::TraceUpdateConstructorCache(const ConstructorCache* ctorCache, const FunctionBody* ctorBody, bool updated, const char16* reason)
  6184. {
  6185. #if DBG_DUMP
  6186. if (PHASE_TRACE(Js::ConstructorCachePhase, ctorBody))
  6187. {
  6188. const char16* ctorName = ctorBody->GetDisplayName();
  6189. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  6190. Output::Print(_u("CtorCache: %s cache (0x%p) for ctor %s (%s)%s %s: "),
  6191. updated ? _u("updated") : _u("did not update"), ctorBody, ctorName,
  6192. ctorBody ? const_cast<Js::FunctionBody *>(ctorBody)->GetDebugNumberSet(debugStringBuffer) : _u("(null)"),
  6193. updated ? _u("") : _u(", because") , reason);
  6194. ctorCache->Dump();
  6195. Output::Print(_u("\n"));
  6196. Output::Flush();
  6197. }
  6198. #endif
  6199. }
  6200. Var JavascriptOperators::NewScObject(const Var callee, const Arguments args, ScriptContext *const scriptContext, const Js::AuxArray<uint32> *spreadIndices)
  6201. {
  6202. Assert(callee);
  6203. Assert(args.Info.Count != 0);
  6204. Assert(scriptContext);
  6205. // Always save and restore implicit call flags when calling out
  6206. // REVIEW: Can we avoid it if we don't collect dynamic profile info?
  6207. ThreadContext *const threadContext = scriptContext->GetThreadContext();
  6208. const ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  6209. const Var newVarInstance = JavascriptFunction::CallAsConstructor(callee, /* overridingNewTarget = */nullptr, args, scriptContext, spreadIndices);
  6210. threadContext->SetImplicitCallFlags(savedImplicitCallFlags);
  6211. return newVarInstance;
  6212. }
  6213. Js::GlobalObject * JavascriptOperators::OP_LdRoot(ScriptContext* scriptContext)
  6214. {
  6215. return scriptContext->GetGlobalObject();
  6216. }
  6217. Js::ModuleRoot * JavascriptOperators::GetModuleRoot(int moduleID, ScriptContext* scriptContext)
  6218. {
  6219. Assert(moduleID != kmodGlobal);
  6220. JavascriptLibrary* library = scriptContext->GetLibrary();
  6221. HostObjectBase *hostObject = library->GetGlobalObject()->GetHostObject();
  6222. if (hostObject)
  6223. {
  6224. Js::ModuleRoot * moduleRoot = hostObject->GetModuleRoot(moduleID);
  6225. Assert(!CrossSite::NeedMarshalVar(moduleRoot, scriptContext));
  6226. return moduleRoot;
  6227. }
  6228. HostScriptContext *hostScriptContext = scriptContext->GetHostScriptContext();
  6229. if (hostScriptContext)
  6230. {
  6231. Js::ModuleRoot * moduleRoot = hostScriptContext->GetModuleRoot(moduleID);
  6232. Assert(!CrossSite::NeedMarshalVar(moduleRoot, scriptContext));
  6233. return moduleRoot;
  6234. }
  6235. Assert(FALSE);
  6236. return nullptr;
  6237. }
  6238. Var JavascriptOperators::OP_LoadModuleRoot(int moduleID, ScriptContext* scriptContext)
  6239. {
  6240. Js::ModuleRoot * moduleRoot = GetModuleRoot(moduleID, scriptContext);
  6241. if (moduleRoot)
  6242. {
  6243. return moduleRoot;
  6244. }
  6245. Assert(false);
  6246. return scriptContext->GetLibrary()->GetUndefined();
  6247. }
  6248. Var JavascriptOperators::OP_LdNull(ScriptContext* scriptContext)
  6249. {
  6250. return scriptContext->GetLibrary()->GetNull();
  6251. }
  6252. Var JavascriptOperators::OP_LdUndef(ScriptContext* scriptContext)
  6253. {
  6254. return scriptContext->GetLibrary()->GetUndefined();
  6255. }
  6256. Var JavascriptOperators::OP_LdNaN(ScriptContext* scriptContext)
  6257. {
  6258. return scriptContext->GetLibrary()->GetNaN();
  6259. }
  6260. Var JavascriptOperators::OP_LdChakraLib(ScriptContext* scriptContext)
  6261. {
  6262. return scriptContext->GetLibrary()->GetChakraLib();
  6263. }
  6264. Var JavascriptOperators::OP_LdInfinity(ScriptContext* scriptContext)
  6265. {
  6266. return scriptContext->GetLibrary()->GetPositiveInfinite();
  6267. }
  6268. void JavascriptOperators::BuildHandlerScope(Var argThis, RecyclableObject * hostObject, FrameDisplay * pDisplay, ScriptContext * scriptContext)
  6269. {
  6270. // Event handlers need implicit lookups of @@unscopables on parent scopes.
  6271. // We can intercept the property accesses by wrapping the object with the unscopables handler.
  6272. // WebIDL: https://heycam.github.io/webidl/#ref-for-Unscopable
  6273. Assert(argThis != nullptr);
  6274. pDisplay->SetItem(0, TaggedNumber::Is(argThis) ? scriptContext->GetLibrary()->CreateNumberObject(argThis) : ToUnscopablesWrapperObject(argThis, scriptContext));
  6275. uint16 i = 1;
  6276. Var aChild = argThis;
  6277. uint16 length = pDisplay->GetLength();
  6278. // Now add any parent scopes
  6279. // We need to support the namespace parent lookup in both fastDOM on and off scenario.
  6280. while (aChild != NULL)
  6281. {
  6282. Var aParent = hostObject->GetNamespaceParent(aChild);
  6283. if (aParent == nullptr)
  6284. {
  6285. break;
  6286. }
  6287. aParent = CrossSite::MarshalVar(scriptContext, aParent);
  6288. if (i == length)
  6289. {
  6290. length = UInt16Math::Add(length, 8);
  6291. FrameDisplay * tmp = RecyclerNewPlus(scriptContext->GetRecycler(), length * sizeof(void*), FrameDisplay, length);
  6292. js_memcpy_s((char*)tmp + tmp->GetOffsetOfScopes(), tmp->GetLength() * sizeof(void *), (char*)pDisplay + pDisplay->GetOffsetOfScopes(), pDisplay->GetLength() * sizeof(void*));
  6293. pDisplay = tmp;
  6294. }
  6295. Var aParentWrapped = ToUnscopablesWrapperObject(aParent, scriptContext);
  6296. pDisplay->SetItem(i, aParentWrapped);
  6297. aChild = aParent;
  6298. i++;
  6299. }
  6300. Assert(i <= pDisplay->GetLength());
  6301. pDisplay->SetLength(i);
  6302. }
  6303. FrameDisplay * JavascriptOperators::OP_LdHandlerScope(Var argThis, ScriptContext* scriptContext)
  6304. {
  6305. JIT_HELPER_REENTRANT_HEADER(ScrObj_LdHandlerScope);
  6306. // The idea here is to build a stack of nested scopes in the form of a JS array.
  6307. //
  6308. // The scope stack for an event handler looks like this:
  6309. //
  6310. // implicit "this"
  6311. // implicit namespace parent scopes
  6312. // Put the implicit "this"
  6313. if (argThis != NULL)
  6314. {
  6315. RecyclableObject* hostObject = scriptContext->GetGlobalObject()->GetHostObject();
  6316. if (hostObject == nullptr)
  6317. {
  6318. hostObject = scriptContext->GetGlobalObject()->GetDirectHostObject();
  6319. }
  6320. if (hostObject != nullptr)
  6321. {
  6322. uint16 length = 7;
  6323. FrameDisplay *pDisplay =
  6324. RecyclerNewPlus(scriptContext->GetRecycler(), length * sizeof(void*), FrameDisplay, length);
  6325. BuildHandlerScope(argThis, hostObject, pDisplay, scriptContext);
  6326. return pDisplay;
  6327. }
  6328. }
  6329. return const_cast<FrameDisplay *>(&Js::NullFrameDisplay);
  6330. JIT_HELPER_END(ScrObj_LdHandlerScope);
  6331. }
  6332. FrameDisplay* JavascriptOperators::OP_LdFrameDisplay(void *argHead, void *argEnv, ScriptContext* scriptContext)
  6333. {
  6334. JIT_HELPER_NOT_REENTRANT_HEADER(ScrObj_LdFrameDisplay, reentrancylock, scriptContext->GetThreadContext());
  6335. // Build a display of nested frame objects.
  6336. // argHead is the current scope; argEnv is either the lone trailing scope or an array of scopes
  6337. // which we append to the new display.
  6338. // Note that there are cases in which a function with no local frame must construct a display to pass
  6339. // to the function(s) nested within it. In such a case, argHead will be a null object, and it's not
  6340. // strictly necessary to include it. But such cases are rare and not perf critical, so it's not
  6341. // worth the extra complexity to notify the nested functions that they can "skip" this slot in the
  6342. // frame display when they're loading scopes nested outside it.
  6343. FrameDisplay *pDisplay = nullptr;
  6344. FrameDisplay *envDisplay = (FrameDisplay*)argEnv;
  6345. uint16 length = UInt16Math::Add(envDisplay->GetLength(), 1);
  6346. pDisplay = RecyclerNewPlus(scriptContext->GetRecycler(), length * sizeof(void*), FrameDisplay, length);
  6347. for (uint16 j = 0; j < length - 1; j++)
  6348. {
  6349. pDisplay->SetItem(j + 1, envDisplay->GetItem(j));
  6350. }
  6351. pDisplay->SetItem(0, argHead);
  6352. return pDisplay;
  6353. JIT_HELPER_END(ScrObj_LdFrameDisplay);
  6354. }
  6355. FrameDisplay* JavascriptOperators::OP_LdFrameDisplayNoParent(void *argHead, ScriptContext* scriptContext)
  6356. {
  6357. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdFrameDisplayNoParent);
  6358. return OP_LdFrameDisplay(argHead, (void*)&NullFrameDisplay, scriptContext);
  6359. JIT_HELPER_END(ScrObj_LdFrameDisplayNoParent);
  6360. }
  6361. FrameDisplay* JavascriptOperators::OP_LdStrictFrameDisplay(void *argHead, void *argEnv, ScriptContext* scriptContext)
  6362. {
  6363. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdStrictFrameDisplay);
  6364. FrameDisplay * pDisplay = OP_LdFrameDisplay(argHead, argEnv, scriptContext);
  6365. pDisplay->SetStrictMode(true);
  6366. return pDisplay;
  6367. JIT_HELPER_END(ScrObj_LdStrictFrameDisplay);
  6368. }
  6369. FrameDisplay* JavascriptOperators::OP_LdStrictFrameDisplayNoParent(void *argHead, ScriptContext* scriptContext)
  6370. {
  6371. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdStrictFrameDisplayNoParent);
  6372. return OP_LdStrictFrameDisplay(argHead, (void*)&StrictNullFrameDisplay, scriptContext);
  6373. JIT_HELPER_END(ScrObj_LdStrictFrameDisplayNoParent);
  6374. }
  6375. FrameDisplay* JavascriptOperators::OP_LdInnerFrameDisplay(void *argHead, void *argEnv, ScriptContext* scriptContext)
  6376. {
  6377. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdInnerFrameDisplay);
  6378. CheckInnerFrameDisplayArgument(argHead);
  6379. return OP_LdFrameDisplay(argHead, argEnv, scriptContext);
  6380. JIT_HELPER_END(ScrObj_LdInnerFrameDisplay);
  6381. }
  6382. FrameDisplay* JavascriptOperators::OP_LdInnerFrameDisplayNoParent(void *argHead, ScriptContext* scriptContext)
  6383. {
  6384. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdInnerFrameDisplayNoParent);
  6385. CheckInnerFrameDisplayArgument(argHead);
  6386. return OP_LdFrameDisplayNoParent(argHead, scriptContext);
  6387. JIT_HELPER_END(ScrObj_LdInnerFrameDisplayNoParent);
  6388. }
  6389. FrameDisplay* JavascriptOperators::OP_LdStrictInnerFrameDisplay(void *argHead, void *argEnv, ScriptContext* scriptContext)
  6390. {
  6391. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdStrictInnerFrameDisplay);
  6392. CheckInnerFrameDisplayArgument(argHead);
  6393. return OP_LdStrictFrameDisplay(argHead, argEnv, scriptContext);
  6394. JIT_HELPER_END(ScrObj_LdStrictInnerFrameDisplay);
  6395. }
  6396. FrameDisplay* JavascriptOperators::OP_LdStrictInnerFrameDisplayNoParent(void *argHead, ScriptContext* scriptContext)
  6397. {
  6398. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdStrictInnerFrameDisplayNoParent);
  6399. CheckInnerFrameDisplayArgument(argHead);
  6400. return OP_LdStrictFrameDisplayNoParent(argHead, scriptContext);
  6401. JIT_HELPER_END(ScrObj_LdStrictInnerFrameDisplayNoParent);
  6402. }
  6403. void JavascriptOperators::CheckInnerFrameDisplayArgument(void *argHead)
  6404. {
  6405. if (ThreadContext::IsOnStack(argHead))
  6406. {
  6407. AssertMsg(false, "Illegal byte code: stack object as with scope");
  6408. Js::Throw::FatalInternalError();
  6409. }
  6410. if (!VarIs<RecyclableObject>(argHead))
  6411. {
  6412. AssertMsg(false, "Illegal byte code: non-object as with scope");
  6413. Js::Throw::FatalInternalError();
  6414. }
  6415. }
  6416. Js::PropertyId JavascriptOperators::GetPropertyId(Var propertyName, ScriptContext* scriptContext)
  6417. {
  6418. PropertyRecord const * propertyRecord = nullptr;
  6419. JavascriptSymbol * symbol = JavascriptOperators::TryFromVar<Js::JavascriptSymbol>(propertyName);
  6420. if (symbol)
  6421. {
  6422. propertyRecord = symbol->GetValue();
  6423. }
  6424. else
  6425. {
  6426. JavascriptSymbolObject * symbolObject = JavascriptOperators::TryFromVar<JavascriptSymbolObject>(propertyName);
  6427. if (symbolObject)
  6428. {
  6429. propertyRecord = symbolObject->GetValue();
  6430. }
  6431. else
  6432. {
  6433. JavascriptString * indexStr = JavascriptConversion::ToString(propertyName, scriptContext);
  6434. scriptContext->GetOrAddPropertyRecord(indexStr, &propertyRecord);
  6435. }
  6436. }
  6437. return propertyRecord->GetPropertyId();
  6438. }
  6439. void JavascriptOperators::OP_InitSetter(Var object, PropertyId propertyId, Var setter)
  6440. {
  6441. AssertMsg(!TaggedNumber::Is(object), "SetMember on a non-object?");
  6442. RecyclableObject* recylableObject = VarTo<RecyclableObject>(object);
  6443. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitSetter, reentrancylock, recylableObject->GetScriptContext()->GetThreadContext());
  6444. recylableObject->SetAccessors(propertyId, nullptr, setter);
  6445. JIT_HELPER_END(OP_InitSetter);
  6446. }
  6447. void JavascriptOperators::OP_InitClassMemberSet(Var object, PropertyId propertyId, Var setter)
  6448. {
  6449. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_InitClassMemberSet);
  6450. JIT_HELPER_SAME_ATTRIBUTES(Op_InitClassMemberSet, OP_InitSetter);
  6451. JavascriptOperators::OP_InitSetter(object, propertyId, setter);
  6452. VarTo<RecyclableObject>(object)->SetAttributes(propertyId, PropertyClassMemberDefaults);
  6453. JIT_HELPER_END(Op_InitClassMemberSet);
  6454. }
  6455. Js::PropertyId JavascriptOperators::OP_InitElemSetter(Var object, Var elementName, Var setter, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6456. {
  6457. JIT_HELPER_REENTRANT_HEADER(OP_InitElemSetter);
  6458. AssertMsg(!TaggedNumber::Is(object), "SetMember on a non-object?");
  6459. PropertyId propertyId = JavascriptOperators::GetPropertyId(elementName, scriptContext);
  6460. VarTo<RecyclableObject>(object)->SetAccessors(propertyId, nullptr, setter);
  6461. return propertyId;
  6462. JIT_HELPER_END(OP_InitElemSetter);
  6463. }
  6464. Field(Var)* JavascriptOperators::OP_GetModuleExportSlotArrayAddress(uint moduleIndex, uint slotIndex, ScriptContextInfo* scriptContext)
  6465. {
  6466. return scriptContext->GetModuleExportSlotArrayAddress(moduleIndex, slotIndex);
  6467. }
  6468. Field(Var)* JavascriptOperators::OP_GetModuleExportSlotAddress(uint moduleIndex, uint slotIndex, ScriptContext* scriptContext)
  6469. {
  6470. Field(Var)* moduleRecordSlots = OP_GetModuleExportSlotArrayAddress(moduleIndex, slotIndex, scriptContext);
  6471. Assert(moduleRecordSlots != nullptr);
  6472. return &moduleRecordSlots[slotIndex];
  6473. }
  6474. Var JavascriptOperators::OP_LdModuleSlot(uint moduleIndex, uint slotIndex, ScriptContext* scriptContext)
  6475. {
  6476. Field(Var)* addr = OP_GetModuleExportSlotAddress(moduleIndex, slotIndex, scriptContext);
  6477. Assert(addr != nullptr);
  6478. return *addr;
  6479. }
  6480. void JavascriptOperators::OP_StModuleSlot(uint moduleIndex, uint slotIndex, Var value, ScriptContext* scriptContext)
  6481. {
  6482. Assert(value != nullptr);
  6483. Field(Var)* addr = OP_GetModuleExportSlotAddress(moduleIndex, slotIndex, scriptContext);
  6484. Assert(addr != nullptr);
  6485. *addr = value;
  6486. }
  6487. Var JavascriptOperators::OP_LdImportMeta(uint moduleIndex, ScriptContext* scriptContext)
  6488. {
  6489. JIT_HELPER_REENTRANT_HEADER(LdImportMeta);
  6490. return scriptContext->GetLibrary()->GetModuleRecord(moduleIndex)->GetImportMetaObject();
  6491. JIT_HELPER_END(LdImportMeta);
  6492. }
  6493. void JavascriptOperators::OP_InitClassMemberSetComputedName(Var object, Var elementName, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6494. {
  6495. JIT_HELPER_REENTRANT_HEADER(Op_InitClassMemberSetComputedName);
  6496. Js::PropertyId propertyId = JavascriptOperators::OP_InitElemSetter(object, elementName, value, scriptContext);
  6497. RecyclableObject* instance = VarTo<RecyclableObject>(object);
  6498. // instance will be a function if it is the class constructor (otherwise it would be an object)
  6499. if (VarIs<JavascriptFunction>(instance) && Js::PropertyIds::prototype == propertyId)
  6500. {
  6501. // It is a TypeError to have a static member with a computed name that evaluates to 'prototype'
  6502. JavascriptError::ThrowTypeError(scriptContext, JSERR_ClassStaticMethodCannotBePrototype);
  6503. }
  6504. instance->SetAttributes(propertyId, PropertyClassMemberDefaults);
  6505. JIT_HELPER_END(Op_InitClassMemberSetComputedName);
  6506. }
  6507. BOOL JavascriptOperators::IsClassConstructor(Var instance)
  6508. {
  6509. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_IsClassConstructor);
  6510. JavascriptFunction * function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  6511. return function && function->GetFunctionInfo()->IsClassConstructor();
  6512. JIT_HELPER_END(Op_IsClassConstructor);
  6513. }
  6514. BOOL JavascriptOperators::IsClassMethod(Var instance)
  6515. {
  6516. JavascriptFunction * function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  6517. return function && function->GetFunctionInfo()->IsClassMethod();
  6518. }
  6519. BOOL JavascriptOperators::IsBaseConstructorKind(Var instance)
  6520. {
  6521. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_IsBaseConstructorKind);
  6522. JavascriptFunction * function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  6523. return function && (function->GetFunctionInfo()->GetBaseConstructorKind());
  6524. JIT_HELPER_END(Op_IsBaseConstructorKind);
  6525. }
  6526. void JavascriptOperators::OP_InitGetter(Var object, PropertyId propertyId, Var getter)
  6527. {
  6528. AssertMsg(!TaggedNumber::Is(object), "GetMember on a non-object?");
  6529. RecyclableObject* recylableObject = VarTo<RecyclableObject>(object);
  6530. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitGetter, reentrancylock, recylableObject->GetScriptContext()->GetThreadContext());
  6531. recylableObject->SetAccessors(propertyId, getter, nullptr);
  6532. JIT_HELPER_END(OP_InitGetter);
  6533. }
  6534. void JavascriptOperators::OP_InitClassMemberGet(Var object, PropertyId propertyId, Var getter)
  6535. {
  6536. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_InitClassMemberGet);
  6537. JIT_HELPER_SAME_ATTRIBUTES(Op_InitClassMemberGet, OP_InitGetter);
  6538. JavascriptOperators::OP_InitGetter(object, propertyId, getter);
  6539. VarTo<RecyclableObject>(object)->SetAttributes(propertyId, PropertyClassMemberDefaults);
  6540. JIT_HELPER_END(Op_InitClassMemberGet);
  6541. }
  6542. Js::PropertyId JavascriptOperators::OP_InitElemGetter(Var object, Var elementName, Var getter, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6543. {
  6544. JIT_HELPER_REENTRANT_HEADER(OP_InitElemGetter);
  6545. AssertMsg(!TaggedNumber::Is(object), "GetMember on a non-object?");
  6546. PropertyId propertyId = JavascriptOperators::GetPropertyId(elementName, scriptContext);
  6547. VarTo<RecyclableObject>(object)->SetAccessors(propertyId, getter, nullptr);
  6548. return propertyId;
  6549. JIT_HELPER_END(OP_InitElemGetter);
  6550. }
  6551. void JavascriptOperators::OP_InitClassMemberGetComputedName(Var object, Var elementName, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6552. {
  6553. JIT_HELPER_REENTRANT_HEADER(Op_InitClassMemberGetComputedName);
  6554. Js::PropertyId propertyId = JavascriptOperators::OP_InitElemGetter(object, elementName, value, scriptContext);
  6555. RecyclableObject* instance = VarTo<RecyclableObject>(object);
  6556. // instance will be a function if it is the class constructor (otherwise it would be an object)
  6557. if (VarIs<JavascriptFunction>(instance) && Js::PropertyIds::prototype == propertyId)
  6558. {
  6559. // It is a TypeError to have a static member with a computed name that evaluates to 'prototype'
  6560. JavascriptError::ThrowTypeError(scriptContext, JSERR_ClassStaticMethodCannotBePrototype);
  6561. }
  6562. instance->SetAttributes(propertyId, PropertyClassMemberDefaults);
  6563. JIT_HELPER_END(Op_InitClassMemberGetComputedName);
  6564. }
  6565. void JavascriptOperators::OP_InitComputedProperty(Var object, Var elementName, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6566. {
  6567. JIT_HELPER_REENTRANT_HEADER(OP_InitComputedProperty);
  6568. PropertyId propertyId = JavascriptOperators::GetPropertyId(elementName, scriptContext);
  6569. VarTo<RecyclableObject>(object)->InitProperty(propertyId, value, flags);
  6570. JIT_HELPER_END(OP_InitComputedProperty);
  6571. }
  6572. void JavascriptOperators::OP_InitClassMemberComputedName(Var object, Var elementName, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6573. {
  6574. JIT_HELPER_REENTRANT_HEADER(Op_InitClassMemberComputedName);
  6575. PropertyId propertyId = JavascriptOperators::GetPropertyId(elementName, scriptContext);
  6576. RecyclableObject* instance = VarTo<RecyclableObject>(object);
  6577. // instance will be a function if it is the class constructor (otherwise it would be an object)
  6578. if (VarIs<JavascriptFunction>(instance) && Js::PropertyIds::prototype == propertyId)
  6579. {
  6580. // It is a TypeError to have a static member with a computed name that evaluates to 'prototype'
  6581. JavascriptError::ThrowTypeError(scriptContext, JSERR_ClassStaticMethodCannotBePrototype);
  6582. }
  6583. instance->SetPropertyWithAttributes(propertyId, value, PropertyClassMemberDefaults, NULL, flags);
  6584. JIT_HELPER_END(Op_InitClassMemberComputedName);
  6585. }
  6586. //
  6587. // Used by object literal {..., __proto__: ..., }.
  6588. //
  6589. void JavascriptOperators::OP_InitProto(Var instance, PropertyId propertyId, Var value)
  6590. {
  6591. AssertMsg(VarIs<RecyclableObject>(instance), "__proto__ member on a non-object?");
  6592. Assert(propertyId == PropertyIds::__proto__);
  6593. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  6594. ScriptContext* scriptContext = object->GetScriptContext();
  6595. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitProto, reentrancylock, scriptContext->GetThreadContext());
  6596. // B.3.1 __proto___ Property Names in Object Initializers
  6597. //6.If propKey is the string value "__proto__" and if isComputedPropertyName(propKey) is false, then
  6598. // a.If Type(v) is either Object or Null, then
  6599. // i.Return the result of calling the [[SetInheritance]] internal method of object with argument propValue.
  6600. // b.Return NormalCompletion(empty).
  6601. if (JavascriptOperators::IsObjectOrNull(value))
  6602. {
  6603. JavascriptObject::ChangePrototype(object, VarTo<RecyclableObject>(value), /*validate*/false, scriptContext);
  6604. }
  6605. JIT_HELPER_END(OP_InitProto);
  6606. }
  6607. Var JavascriptOperators::ConvertToUnmappedArguments(HeapArgumentsObject *argumentsObject,
  6608. uint32 paramCount,
  6609. Var *paramAddr,
  6610. DynamicObject* frameObject,
  6611. Js::PropertyIdArray *propIds,
  6612. uint32 formalsCount,
  6613. ScriptContext* scriptContext)
  6614. {
  6615. Var *paramIter = paramAddr;
  6616. uint32 i = 0;
  6617. for (paramIter = paramAddr + i; i < paramCount; i++, paramIter++)
  6618. {
  6619. JavascriptOperators::SetItem(argumentsObject, argumentsObject, i, *paramIter, scriptContext, PropertyOperation_None, /* skipPrototypeCheck = */ TRUE);
  6620. }
  6621. argumentsObject = argumentsObject->ConvertToUnmappedArgumentsObject();
  6622. // Now as the unmapping is done we need to fill those frame object with Undecl
  6623. for (i = 0; i < formalsCount; i++)
  6624. {
  6625. frameObject->SetSlot(SetSlotArguments(propIds != nullptr ? propIds->elements[i] : Js::Constants::NoProperty, i, scriptContext->GetLibrary()->GetUndeclBlockVar()));
  6626. }
  6627. return argumentsObject;
  6628. }
  6629. Var JavascriptOperators::LoadHeapArguments(JavascriptFunction *funcCallee, uint32 actualsCount, Var *paramAddr, Var frameObj, Var vArray, ScriptContext* scriptContext, bool nonSimpleParamList)
  6630. {
  6631. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LoadHeapArguments, reentrancylock, scriptContext->GetThreadContext());
  6632. AssertMsg(actualsCount != (unsigned int)-1, "Loading the arguments object in the global function?");
  6633. // Create and initialize the Arguments object.
  6634. uint32 formalsCount = 0;
  6635. Js::PropertyIdArray *propIds = nullptr;
  6636. if (vArray != scriptContext->GetLibrary()->GetNull())
  6637. {
  6638. propIds = (Js::PropertyIdArray *)vArray;
  6639. formalsCount = propIds->count;
  6640. Assert(formalsCount != 0 && propIds != nullptr);
  6641. }
  6642. HeapArgumentsObject *argsObj = JavascriptOperators::CreateHeapArguments(funcCallee, actualsCount, formalsCount, frameObj, scriptContext);
  6643. return FillScopeObject(funcCallee, actualsCount, formalsCount, frameObj, paramAddr, propIds, argsObj, scriptContext, nonSimpleParamList, false);
  6644. JIT_HELPER_END(Op_LoadHeapArguments);
  6645. }
  6646. Var JavascriptOperators::LoadHeapArgsCached(JavascriptFunction *funcCallee, uint32 actualsCount, uint32 formalsCount, Var *paramAddr, Var frameObj, ScriptContext* scriptContext, bool nonSimpleParamList)
  6647. {
  6648. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LoadHeapArgsCached, reentrancylock, scriptContext->GetThreadContext());
  6649. // Disregard the "this" param.
  6650. AssertMsg(actualsCount != (uint32)-1 && formalsCount != (uint32)-1,
  6651. "Loading the arguments object in the global function?");
  6652. HeapArgumentsObject *argsObj = JavascriptOperators::CreateHeapArguments(funcCallee, actualsCount, formalsCount, frameObj, scriptContext);
  6653. return FillScopeObject(funcCallee, actualsCount, formalsCount, frameObj, paramAddr, nullptr, argsObj, scriptContext, nonSimpleParamList, true);
  6654. JIT_HELPER_END(Op_LoadHeapArgsCached);
  6655. }
  6656. Var JavascriptOperators::FillScopeObject(JavascriptFunction *funcCallee, uint32 actualsCount, uint32 formalsCount, Var frameObj, Var * paramAddr,
  6657. Js::PropertyIdArray *propIds, HeapArgumentsObject * argsObj, ScriptContext * scriptContext, bool nonSimpleParamList, bool useCachedScope)
  6658. {
  6659. Assert(formalsCount == 0 || frameObj != nullptr);
  6660. // Transfer formal arguments (that were actually passed) from their ArgIn slots to the local frame object.
  6661. uint32 i;
  6662. Var *tmpAddr = paramAddr;
  6663. if (formalsCount != 0)
  6664. {
  6665. DynamicObject* frameObject = nullptr;
  6666. if (useCachedScope)
  6667. {
  6668. frameObject = VarTo<DynamicObject>(frameObj);
  6669. __analysis_assume((uint32)frameObject->GetDynamicType()->GetTypeHandler()->GetSlotCapacity() >= formalsCount);
  6670. }
  6671. else
  6672. {
  6673. frameObject = (DynamicObject*)frameObj;
  6674. // No fixed fields for formal parameters of the arguments object. Also, mark all fields as initialized up-front, because
  6675. // we will set them directly using SetSlot below, so the type handler will not have a chance to mark them as initialized later.
  6676. // CONSIDER : When we delay type sharing until the second instance is created, pass an argument indicating we want the types
  6677. // 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
  6678. // is ready for storing values directly to slots.
  6679. DynamicType* newType = nullptr;
  6680. if (nonSimpleParamList)
  6681. {
  6682. bool skipLetAttrForArguments = ( VarIs<JavascriptGeneratorFunction>(funcCallee) ?
  6683. UnsafeVarTo<JavascriptGeneratorFunction>(funcCallee)->GetGeneratorVirtualScriptFunction()->GetFunctionBody()->HasReferenceableBuiltInArguments()
  6684. : funcCallee->GetFunctionBody()->HasReferenceableBuiltInArguments());
  6685. if (skipLetAttrForArguments)
  6686. {
  6687. newType = PathTypeHandlerBase::CreateNewScopeObject<true>(scriptContext, frameObject->GetDynamicType(), propIds, PropertyLetDefaults);
  6688. }
  6689. else
  6690. {
  6691. newType = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, frameObject->GetDynamicType(), propIds, PropertyLetDefaults);
  6692. }
  6693. }
  6694. else
  6695. {
  6696. newType = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, frameObject->GetDynamicType(), propIds);
  6697. }
  6698. int oldSlotCapacity = frameObject->GetDynamicType()->GetTypeHandler()->GetSlotCapacity();
  6699. int newSlotCapacity = newType->GetTypeHandler()->GetSlotCapacity();
  6700. __analysis_assume((uint32)newSlotCapacity >= formalsCount);
  6701. frameObject->EnsureSlots(oldSlotCapacity, newSlotCapacity, scriptContext, newType->GetTypeHandler());
  6702. frameObject->ReplaceType(newType);
  6703. }
  6704. if (argsObj && nonSimpleParamList)
  6705. {
  6706. return ConvertToUnmappedArguments(argsObj, actualsCount, paramAddr, frameObject, propIds, formalsCount, scriptContext);
  6707. }
  6708. for (i = 0; i < formalsCount && i < actualsCount; i++, tmpAddr++)
  6709. {
  6710. frameObject->SetSlot(SetSlotArguments(propIds != nullptr? propIds->elements[i] : Constants::NoProperty, i, *tmpAddr));
  6711. }
  6712. if (i < formalsCount)
  6713. {
  6714. // The formals that weren't passed still need to be put in the frame object so that
  6715. // their names will be found. Initialize them to "undefined".
  6716. for (; i < formalsCount; i++)
  6717. {
  6718. frameObject->SetSlot(SetSlotArguments(propIds != nullptr? propIds->elements[i] : Constants::NoProperty, i, scriptContext->GetLibrary()->GetUndefined()));
  6719. }
  6720. }
  6721. }
  6722. if (argsObj != nullptr)
  6723. {
  6724. // Transfer the unnamed actual arguments, if any, to the Arguments object itself.
  6725. for (i = formalsCount, tmpAddr = paramAddr + i; i < actualsCount; i++, tmpAddr++)
  6726. {
  6727. // ES5 10.6.11: use [[DefineOwnProperty]] semantics (instead of [[Put]]):
  6728. // do not check whether property is non-writable/etc in the prototype.
  6729. // ES3 semantics is same.
  6730. JavascriptOperators::SetItem(argsObj, argsObj, i, *tmpAddr, scriptContext, PropertyOperation_None, /* skipPrototypeCheck = */ TRUE);
  6731. }
  6732. if (funcCallee->IsStrictMode())
  6733. {
  6734. // If the formals are let decls, then we just overwrote the frame object slots with
  6735. // Undecl sentinels, and we can use the original arguments that were passed to the HeapArgumentsObject.
  6736. return argsObj->ConvertToUnmappedArgumentsObject(!nonSimpleParamList);
  6737. }
  6738. }
  6739. return argsObj;
  6740. }
  6741. HeapArgumentsObject *JavascriptOperators::CreateHeapArguments(JavascriptFunction *funcCallee, uint32 actualsCount, uint32 formalsCount, Var frameObj, ScriptContext* scriptContext)
  6742. {
  6743. JavascriptLibrary *library = scriptContext->GetLibrary();
  6744. HeapArgumentsObject *argsObj = library->CreateHeapArguments(frameObj, formalsCount, !!funcCallee->IsStrictMode());
  6745. #if DBG
  6746. DynamicTypeHandler* typeHandler = argsObj->GetTypeHandler();
  6747. #endif
  6748. //
  6749. // Set the number of arguments of Arguments Object
  6750. //
  6751. argsObj->SetNumberOfArguments(actualsCount);
  6752. JavascriptOperators::SetProperty(argsObj, argsObj, PropertyIds::length, JavascriptNumber::ToVar(actualsCount, scriptContext), scriptContext);
  6753. JavascriptOperators::SetProperty(argsObj, argsObj, PropertyIds::_symbolIterator, library->EnsureArrayPrototypeValuesFunction(), scriptContext);
  6754. if (funcCallee->IsStrictMode())
  6755. {
  6756. JavascriptFunction* restrictedPropertyAccessor = library->GetThrowTypeErrorRestrictedPropertyAccessorFunction();
  6757. argsObj->SetAccessors(PropertyIds::callee, restrictedPropertyAccessor, restrictedPropertyAccessor, PropertyOperation_NonFixedValue);
  6758. }
  6759. else
  6760. {
  6761. JavascriptOperators::SetProperty(argsObj, argsObj, PropertyIds::callee,
  6762. StackScriptFunction::EnsureBoxed(BOX_PARAM(funcCallee, nullptr, _u("callee"))), scriptContext);
  6763. }
  6764. AssertMsg(argsObj->GetTypeHandler() == typeHandler || scriptContext->IsScriptContextInDebugMode(), "type handler should not transition because we initialized it correctly");
  6765. return argsObj;
  6766. }
  6767. Var JavascriptOperators::OP_NewScopeObject(ScriptContext* scriptContext)
  6768. {
  6769. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_NewScopeObject);
  6770. return scriptContext->GetLibrary()->CreateActivationObject();
  6771. JIT_HELPER_END(OP_NewScopeObject);
  6772. }
  6773. Var JavascriptOperators::OP_NewScopeObjectWithFormals(ScriptContext* scriptContext, FunctionBody * calleeBody, bool nonSimpleParamList)
  6774. {
  6775. JIT_HELPER_NOT_REENTRANT_HEADER(OP_NewScopeObjectWithFormals, reentrancylock, scriptContext->GetThreadContext());
  6776. Js::ActivationObject * frameObject = (ActivationObject*)OP_NewScopeObject(scriptContext);
  6777. // No fixed fields for formal parameters of the arguments object. Also, mark all fields as initialized up-front, because
  6778. // we will set them directly using SetSlot below, so the type handler will not have a chance to mark them as initialized later.
  6779. // CONSIDER : When we delay type sharing until the second instance is created, pass an argument indicating we want the types
  6780. // 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
  6781. // is ready for storing values directly to slots.
  6782. DynamicType* newType = nullptr;
  6783. if (nonSimpleParamList)
  6784. {
  6785. if (calleeBody->HasReferenceableBuiltInArguments())
  6786. {
  6787. newType = PathTypeHandlerBase::CreateNewScopeObject<true>(scriptContext, frameObject->GetDynamicType(), calleeBody->GetFormalsPropIdArray(), PropertyLetDefaults);
  6788. }
  6789. else
  6790. {
  6791. newType = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, frameObject->GetDynamicType(), calleeBody->GetFormalsPropIdArray(), PropertyLetDefaults);
  6792. }
  6793. }
  6794. else
  6795. {
  6796. newType = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, frameObject->GetDynamicType(), calleeBody->GetFormalsPropIdArray());
  6797. }
  6798. int oldSlotCapacity = frameObject->GetDynamicType()->GetTypeHandler()->GetSlotCapacity();
  6799. int newSlotCapacity = newType->GetTypeHandler()->GetSlotCapacity();
  6800. frameObject->EnsureSlots(oldSlotCapacity, newSlotCapacity, scriptContext, newType->GetTypeHandler());
  6801. frameObject->ReplaceType(newType);
  6802. return frameObject;
  6803. JIT_HELPER_END(OP_NewScopeObjectWithFormals);
  6804. }
  6805. Field(Var)* JavascriptOperators::OP_NewScopeSlots(unsigned int size, ScriptContext *scriptContext, Var scope)
  6806. {
  6807. JIT_HELPER_NOT_REENTRANT_HEADER(OP_NewScopeSlots, reentrancylock, scriptContext->GetThreadContext());
  6808. Assert(size > ScopeSlots::FirstSlotIndex); // Should never see empty slot array
  6809. Field(Var)* slotArray = RecyclerNewArray(scriptContext->GetRecycler(), Field(Var), size); // last initialized slot contains reference to array of propertyIds, correspondent to objects in previous slots
  6810. uint count = size - ScopeSlots::FirstSlotIndex;
  6811. ScopeSlots slots(slotArray);
  6812. slots.SetCount(count);
  6813. AssertMsg(!FunctionBody::Is(scope), "Scope should only be FunctionInfo or DebuggerScope, not FunctionBody");
  6814. slots.SetScopeMetadata(scope);
  6815. Var undef = scriptContext->GetLibrary()->GetUndefined();
  6816. for (unsigned int i = 0; i < count; i++)
  6817. {
  6818. slots.Set(i, undef);
  6819. }
  6820. return slotArray;
  6821. JIT_HELPER_END(OP_NewScopeSlots);
  6822. }
  6823. Field(Var)* JavascriptOperators::OP_NewScopeSlotsWithoutPropIds(unsigned int count, int scopeIndex, ScriptContext *scriptContext, FunctionBody *functionBody)
  6824. {
  6825. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_NewScopeSlotsWithoutPropIds);
  6826. DebuggerScope* scope = reinterpret_cast<DebuggerScope*>(Constants::FunctionBodyUnavailable);
  6827. if (scopeIndex != DebuggerScope::InvalidScopeIndex)
  6828. {
  6829. AssertMsg(functionBody->GetScopeObjectChain(), "A scope chain should always be created when there are new scope slots for blocks.");
  6830. scope = functionBody->GetScopeObjectChain()->pScopeChain->Item(scopeIndex);
  6831. }
  6832. return OP_NewScopeSlots(count, scriptContext, scope);
  6833. JIT_HELPER_END(OP_NewScopeSlotsWithoutPropIds);
  6834. }
  6835. Field(Var)* JavascriptOperators::OP_CloneScopeSlots(Field(Var) *slotArray, ScriptContext *scriptContext)
  6836. {
  6837. JIT_HELPER_NOT_REENTRANT_HEADER(OP_CloneInnerScopeSlots, reentrancylock, scriptContext->GetThreadContext());
  6838. ScopeSlots slots(slotArray);
  6839. uint size = ScopeSlots::FirstSlotIndex + static_cast<uint>(slots.GetCount());
  6840. Field(Var)* slotArrayClone = RecyclerNewArray(scriptContext->GetRecycler(), Field(Var), size);
  6841. CopyArray(slotArrayClone, size, slotArray, size);
  6842. return slotArrayClone;
  6843. JIT_HELPER_END(OP_CloneInnerScopeSlots);
  6844. }
  6845. Var JavascriptOperators::OP_NewPseudoScope(ScriptContext *scriptContext)
  6846. {
  6847. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_NewPseudoScope);
  6848. return scriptContext->GetLibrary()->CreatePseudoActivationObject();
  6849. JIT_HELPER_END(OP_NewPseudoScope);
  6850. }
  6851. Var JavascriptOperators::OP_NewBlockScope(ScriptContext *scriptContext)
  6852. {
  6853. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_NewBlockScope);
  6854. return scriptContext->GetLibrary()->CreateBlockActivationObject();
  6855. JIT_HELPER_END(OP_NewBlockScope);
  6856. }
  6857. Var JavascriptOperators::OP_CloneBlockScope(BlockActivationObject *blockScope, ScriptContext *scriptContext)
  6858. {
  6859. JIT_HELPER_NOT_REENTRANT_HEADER(OP_CloneBlockScope, reentrancylock, scriptContext->GetThreadContext());
  6860. return blockScope->Clone(scriptContext);
  6861. JIT_HELPER_END(OP_CloneBlockScope);
  6862. }
  6863. Var JavascriptOperators::OP_IsInst(Var instance, Var aClass, ScriptContext* scriptContext, IsInstInlineCache* inlineCache)
  6864. {
  6865. JIT_HELPER_REENTRANT_HEADER(ScrObj_OP_IsInst);
  6866. if (!VarIs<RecyclableObject>(aClass))
  6867. {
  6868. JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedFunction, _u("instanceof"));
  6869. }
  6870. RecyclableObject* constructor = VarTo<RecyclableObject>(aClass);
  6871. if (VarIs<JavascriptFunction>(constructor))
  6872. {
  6873. JavascriptFunction* func = VarTo<JavascriptFunction>(constructor);
  6874. if (func->IsBoundFunction())
  6875. {
  6876. BoundFunction* boundFunc = (BoundFunction*)func;
  6877. constructor = boundFunc->GetTargetFunction();
  6878. }
  6879. }
  6880. Var instOfHandler = JavascriptOperators::GetPropertyNoCache(constructor,
  6881. PropertyIds::_symbolHasInstance, scriptContext);
  6882. if (JavascriptOperators::IsUndefinedObject(instOfHandler))
  6883. {
  6884. return JavascriptBoolean::ToVar(constructor->HasInstance(instance, scriptContext, inlineCache), scriptContext);
  6885. }
  6886. else
  6887. {
  6888. if (!JavascriptConversion::IsCallable(instOfHandler))
  6889. {
  6890. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, _u("Symbol[Symbol.hasInstance]"));
  6891. }
  6892. ThreadContext * threadContext = scriptContext->GetThreadContext();
  6893. RecyclableObject *instFunc = VarTo<RecyclableObject>(instOfHandler);
  6894. Var result = threadContext->ExecuteImplicitCall(instFunc, ImplicitCall_Accessor, [=]()->Js::Var
  6895. {
  6896. return CALL_FUNCTION(scriptContext->GetThreadContext(), instFunc, CallInfo(CallFlags_Value, 2), constructor, instance);
  6897. });
  6898. return JavascriptBoolean::ToVar(JavascriptConversion::ToBoolean(result, scriptContext) ? TRUE : FALSE, scriptContext);
  6899. }
  6900. JIT_HELPER_END(ScrObj_OP_IsInst);
  6901. }
  6902. Var JavascriptOperators::OP_NewClassProto(Var protoParent, ScriptContext * scriptContext)
  6903. {
  6904. JIT_HELPER_NOT_REENTRANT_HEADER(Op_NewClassProto, reentrancylock, scriptContext->GetThreadContext());
  6905. return scriptContext->GetLibrary()->CreateClassPrototypeObject(VarTo<RecyclableObject>(protoParent));
  6906. JIT_HELPER_END(Op_NewClassProto);
  6907. }
  6908. void JavascriptOperators::OP_LoadUndefinedToElement(Var instance, PropertyId propertyId)
  6909. {
  6910. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LdElemUndef, reentrancylock, VarTo<RecyclableObject>(instance)->GetScriptContext()->GetThreadContext());
  6911. AssertMsg(!TaggedNumber::Is(instance), "Invalid scope/root object");
  6912. JavascriptOperators::EnsureProperty(instance, propertyId);
  6913. JIT_HELPER_END(Op_LdElemUndef);
  6914. }
  6915. void JavascriptOperators::OP_LoadUndefinedToElementScoped(FrameDisplay *pScope, PropertyId propertyId, Var defaultInstance, ScriptContext* scriptContext)
  6916. {
  6917. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LdElemUndefScoped, reentrancylock, scriptContext->GetThreadContext());
  6918. int i;
  6919. int length = pScope->GetLength();
  6920. Var argInstance;
  6921. for (i = 0; i < length; i++)
  6922. {
  6923. argInstance = pScope->GetItem(i);
  6924. if (JavascriptOperators::EnsureProperty(argInstance, propertyId))
  6925. {
  6926. return;
  6927. }
  6928. }
  6929. if (!JavascriptOperators::HasOwnPropertyNoHostObject(defaultInstance, propertyId))
  6930. {
  6931. // CONSIDER : Consider adding pre-initialization support to activation objects.
  6932. JavascriptOperators::OP_InitPropertyScoped(pScope, propertyId, scriptContext->GetLibrary()->GetUndefined(), defaultInstance, scriptContext);
  6933. }
  6934. JIT_HELPER_END(Op_LdElemUndefScoped);
  6935. }
  6936. void JavascriptOperators::OP_LoadUndefinedToElementDynamic(Var instance, PropertyId propertyId, ScriptContext *scriptContext)
  6937. {
  6938. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LdElemUndefDynamic, reentrancylock, scriptContext->GetThreadContext());
  6939. if (!JavascriptOperators::HasOwnPropertyNoHostObject(instance, propertyId))
  6940. {
  6941. VarTo<RecyclableObject>(instance)->InitPropertyScoped(propertyId, scriptContext->GetLibrary()->GetUndefined());
  6942. }
  6943. JIT_HELPER_END(Op_LdElemUndefDynamic);
  6944. }
  6945. BOOL JavascriptOperators::EnsureProperty(Var instance, PropertyId propertyId)
  6946. {
  6947. RecyclableObject *obj = VarTo<RecyclableObject>(instance);
  6948. return (obj && obj->EnsureProperty(propertyId));
  6949. }
  6950. void JavascriptOperators::OP_EnsureNoRootProperty(Var instance, PropertyId propertyId)
  6951. {
  6952. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_EnsureNoRootProperty);
  6953. Assert(VarIs<RootObjectBase>(instance));
  6954. RootObjectBase *obj = VarTo<RootObjectBase>(instance);
  6955. obj->EnsureNoProperty(propertyId);
  6956. JIT_HELPER_END(Op_EnsureNoRootProperty);
  6957. }
  6958. void JavascriptOperators::OP_EnsureNoRootRedeclProperty(Var instance, PropertyId propertyId)
  6959. {
  6960. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_EnsureNoRootRedeclProperty);
  6961. Assert(VarIs<RootObjectBase>(instance));
  6962. RecyclableObject *obj = VarTo<RecyclableObject>(instance);
  6963. obj->EnsureNoRedeclProperty(propertyId);
  6964. JIT_HELPER_END(Op_EnsureNoRootRedeclProperty);
  6965. }
  6966. void JavascriptOperators::OP_EnsureCanDeclGloFunc(Var instance, PropertyId propertyId)
  6967. {
  6968. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_EnsureCanDeclGloFunc);
  6969. Assert(VarIs<RootObjectBase>(instance));
  6970. RootObjectBase *obj = VarTo<RootObjectBase>(instance);
  6971. obj->EnsureCanDeclGloFunc(propertyId);
  6972. JIT_HELPER_END(Op_EnsureCanDeclGloFunc);
  6973. }
  6974. void JavascriptOperators::OP_ScopedEnsureNoRedeclProperty(FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance)
  6975. {
  6976. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_EnsureNoRedeclPropertyScoped);
  6977. int i;
  6978. int length = pDisplay->GetLength();
  6979. RecyclableObject *object;
  6980. for (i = 0; i < length; i++)
  6981. {
  6982. object = VarTo<RecyclableObject>(pDisplay->GetItem(i));
  6983. if (object->EnsureNoRedeclProperty(propertyId))
  6984. {
  6985. return;
  6986. }
  6987. }
  6988. object = VarTo<RecyclableObject>(defaultInstance);
  6989. object->EnsureNoRedeclProperty(propertyId);
  6990. JIT_HELPER_END(Op_EnsureNoRedeclPropertyScoped);
  6991. }
  6992. Var JavascriptOperators::IsIn(Var argProperty, Var instance, ScriptContext* scriptContext)
  6993. {
  6994. JIT_HELPER_REENTRANT_HEADER(Op_IsIn);
  6995. // Note that the fact that we haven't seen a given name before doesn't mean that the instance doesn't
  6996. if (!IsObject(instance))
  6997. {
  6998. JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedObject, _u("in"));
  6999. }
  7000. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  7001. BOOL result;
  7002. PropertyRecord const * propertyRecord = nullptr;
  7003. uint32 index;
  7004. IndexType indexType;
  7005. // Fast path for JavascriptSymbols and PropertyStrings
  7006. RecyclableObject* cacheOwner;
  7007. PropertyRecordUsageCache* propertyRecordUsageCache;
  7008. if (GetPropertyRecordUsageCache(argProperty, scriptContext, &propertyRecordUsageCache, &cacheOwner))
  7009. {
  7010. Var value;
  7011. propertyRecord = propertyRecordUsageCache->GetPropertyRecord();
  7012. if (!propertyRecord->IsNumeric())
  7013. {
  7014. PropertyValueInfo info;
  7015. if (propertyRecordUsageCache->TryGetPropertyFromCache<false /* OwnPropertyOnly */, true /* OutputExistence */, false /* ReturnOperationInfo */>(instance, object, &value, scriptContext, &info, cacheOwner, nullptr))
  7016. {
  7017. Assert(VarIs<JavascriptBoolean>(value));
  7018. return value;
  7019. }
  7020. result = JavascriptOperators::GetPropertyWPCache<true /* OutputExistence */>(instance, object, propertyRecordUsageCache->GetPropertyRecord()->GetPropertyId(), &value, scriptContext, &info);
  7021. Assert(value == JavascriptBoolean::ToVar(result, scriptContext));
  7022. return value;
  7023. }
  7024. // We don't cache numeric property lookups, so fall through to the IndexType_Number case
  7025. index = propertyRecord->GetNumericValue();
  7026. indexType = IndexType_Number;
  7027. }
  7028. else
  7029. {
  7030. indexType = GetIndexType(argProperty, scriptContext, &index, &propertyRecord, true);
  7031. }
  7032. if (indexType == IndexType_Number)
  7033. {
  7034. result = JavascriptOperators::HasItem(object, index);
  7035. }
  7036. else
  7037. {
  7038. result = JavascriptOperators::HasProperty(object, propertyRecord->GetPropertyId());
  7039. #ifdef TELEMETRY_JSO
  7040. {
  7041. Assert(indexType != Js::IndexType_JavascriptString);
  7042. if (indexType == Js::IndexType_PropertyId)
  7043. {
  7044. scriptContext->GetTelemetry().GetOpcodeTelemetry().IsIn(instance, propertyId, result != 0);
  7045. }
  7046. }
  7047. #endif
  7048. }
  7049. return JavascriptBoolean::ToVar(result, scriptContext);
  7050. JIT_HELPER_END(Op_IsIn);
  7051. }
  7052. template <bool IsFromFullJit, class TInlineCache>
  7053. inline Var JavascriptOperators::PatchGetValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7054. {
  7055. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetValue);
  7056. return PatchGetValueWithThisPtr<IsFromFullJit, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, instance);
  7057. JIT_HELPER_END(Op_PatchGetValue);
  7058. }
  7059. JIT_HELPER_TEMPLATE(Op_PatchGetValue, Op_PatchGetValuePolymorphic)
  7060. template <bool IsFromFullJit, class TInlineCache>
  7061. __forceinline Var JavascriptOperators::PatchGetValueWithThisPtr(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance)
  7062. {
  7063. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetValueWithThisPtr);
  7064. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7065. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  7066. RecyclableObject* object = nullptr;
  7067. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7068. {
  7069. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7070. {
  7071. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7072. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7073. }
  7074. else
  7075. {
  7076. return scriptContext->GetLibrary()->GetUndefined();
  7077. }
  7078. }
  7079. PropertyValueInfo info;
  7080. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7081. Var value;
  7082. if (CacheOperators::TryGetProperty<true, true, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7083. instance, false, object, propertyId, &value, scriptContext, nullptr, &info))
  7084. {
  7085. return value;
  7086. }
  7087. #if DBG_DUMP
  7088. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7089. {
  7090. CacheOperators::TraceCache(inlineCache, _u("PatchGetValue"), propertyId, scriptContext, object);
  7091. }
  7092. #endif
  7093. return JavascriptOperators::GetProperty(thisInstance, object, propertyId, scriptContext, &info);
  7094. JIT_HELPER_END(Op_PatchGetValueWithThisPtr);
  7095. }
  7096. JIT_HELPER_TEMPLATE(Op_PatchGetValueWithThisPtr, Op_PatchGetValuePolymorphicWithThisPtr)
  7097. template Var JavascriptOperators::PatchGetValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7098. template Var JavascriptOperators::PatchGetValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7099. template Var JavascriptOperators::PatchGetValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7100. template Var JavascriptOperators::PatchGetValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7101. template Var JavascriptOperators::PatchGetValueWithThisPtr<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance);
  7102. template Var JavascriptOperators::PatchGetValueWithThisPtr<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance);
  7103. template Var JavascriptOperators::PatchGetValueWithThisPtr<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance);
  7104. template Var JavascriptOperators::PatchGetValueWithThisPtr<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance);
  7105. template <bool IsFromFullJit, class TInlineCache>
  7106. Var JavascriptOperators::PatchGetValueForTypeOf(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7107. {
  7108. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetValueForTypeOf);
  7109. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7110. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  7111. RecyclableObject* object = nullptr;
  7112. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7113. {
  7114. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7115. {
  7116. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7117. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7118. }
  7119. else
  7120. {
  7121. return scriptContext->GetLibrary()->GetUndefined();
  7122. }
  7123. }
  7124. PropertyValueInfo info;
  7125. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7126. Var value;
  7127. if (CacheOperators::TryGetProperty<true, true, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7128. instance, false, object, propertyId, &value, scriptContext, nullptr, &info))
  7129. {
  7130. return value;
  7131. }
  7132. #if DBG_DUMP
  7133. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7134. {
  7135. CacheOperators::TraceCache(inlineCache, _u("PatchGetValueForTypeOf"), propertyId, scriptContext, object);
  7136. }
  7137. #endif
  7138. Var prop = nullptr;
  7139. BEGIN_TYPEOF_ERROR_HANDLER(scriptContext);
  7140. prop = JavascriptOperators::GetProperty(instance, object, propertyId, scriptContext, &info);
  7141. END_TYPEOF_ERROR_HANDLER(scriptContext, prop);
  7142. return prop;
  7143. JIT_HELPER_END(Op_PatchGetValueForTypeOf);
  7144. }
  7145. JIT_HELPER_TEMPLATE(Op_PatchGetValueForTypeOf, Op_PatchGetValuePolymorphicForTypeOf)
  7146. template Var JavascriptOperators::PatchGetValueForTypeOf<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7147. template Var JavascriptOperators::PatchGetValueForTypeOf<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7148. template Var JavascriptOperators::PatchGetValueForTypeOf<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7149. template Var JavascriptOperators::PatchGetValueForTypeOf<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7150. Var JavascriptOperators::PatchGetValueUsingSpecifiedInlineCache(InlineCache * inlineCache, Var instance, RecyclableObject * object, PropertyId propertyId, ScriptContext* scriptContext)
  7151. {
  7152. PropertyValueInfo info;
  7153. PropertyValueInfo::SetCacheInfo(&info, inlineCache);
  7154. Var value;
  7155. if (CacheOperators::TryGetProperty<true, true, true, true, false, true, !InlineCache::IsPolymorphic, InlineCache::IsPolymorphic, false, false>(
  7156. instance, false, object, propertyId, &value, scriptContext, nullptr, &info))
  7157. {
  7158. return value;
  7159. }
  7160. #if DBG_DUMP
  7161. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7162. {
  7163. CacheOperators::TraceCache(inlineCache, _u("PatchGetValue"), propertyId, scriptContext, object);
  7164. }
  7165. #endif
  7166. return JavascriptOperators::GetProperty(instance, object, propertyId, scriptContext, &info);
  7167. }
  7168. Var JavascriptOperators::PatchGetValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7169. {
  7170. return PatchGetValueWithThisPtrNoFastPath(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, instance);
  7171. }
  7172. Var JavascriptOperators::PatchGetValueWithThisPtrNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance)
  7173. {
  7174. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7175. RecyclableObject* object = nullptr;
  7176. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7177. {
  7178. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7179. {
  7180. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7181. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7182. }
  7183. else
  7184. {
  7185. return scriptContext->GetLibrary()->GetUndefined();
  7186. }
  7187. }
  7188. PropertyValueInfo info;
  7189. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7190. return JavascriptOperators::GetProperty(thisInstance, object, propertyId, scriptContext, &info);
  7191. }
  7192. template <bool IsFromFullJit, class TInlineCache>
  7193. inline Var JavascriptOperators::PatchGetRootValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId)
  7194. {
  7195. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetRootValue);
  7196. AssertMsg(VarIs<RootObjectBase>(object), "Root must be a global object!");
  7197. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7198. PropertyValueInfo info;
  7199. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7200. Var value;
  7201. if (CacheOperators::TryGetProperty<true, true, true, false, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7202. object, true, object, propertyId, &value, scriptContext, nullptr, &info))
  7203. {
  7204. return value;
  7205. }
  7206. #if DBG_DUMP
  7207. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7208. {
  7209. CacheOperators::TraceCache(inlineCache, _u("PatchGetRootValue"), propertyId, scriptContext, object);
  7210. }
  7211. #endif
  7212. return JavascriptOperators::OP_GetRootProperty(object, propertyId, &info, scriptContext);
  7213. JIT_HELPER_END(Op_PatchGetRootValue);
  7214. }
  7215. JIT_HELPER_TEMPLATE(Op_PatchGetRootValue, Op_PatchGetRootValuePolymorphic)
  7216. template Var JavascriptOperators::PatchGetRootValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7217. template Var JavascriptOperators::PatchGetRootValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7218. template Var JavascriptOperators::PatchGetRootValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7219. template Var JavascriptOperators::PatchGetRootValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7220. template <bool IsFromFullJit, class TInlineCache>
  7221. Var JavascriptOperators::PatchGetRootValueForTypeOf(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId)
  7222. {
  7223. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetRootValueForTypeOf);
  7224. AssertMsg(VarIs<RootObjectBase>(object), "Root must be a global object!");
  7225. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7226. PropertyValueInfo info;
  7227. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7228. Var value = nullptr;
  7229. if (CacheOperators::TryGetProperty<true, true, true, false, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7230. object, true, object, propertyId, &value, scriptContext, nullptr, &info))
  7231. {
  7232. return value;
  7233. }
  7234. #if DBG_DUMP
  7235. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7236. {
  7237. CacheOperators::TraceCache(inlineCache, _u("PatchGetRootValueForTypeOf"), propertyId, scriptContext, object);
  7238. }
  7239. #endif
  7240. value = nullptr;
  7241. BEGIN_TYPEOF_ERROR_HANDLER(scriptContext);
  7242. AssertOrFailFast(VarIsCorrectType(static_cast<RecyclableObject*>(object)));
  7243. if (JavascriptOperators::GetRootProperty(object, propertyId, &value, scriptContext, &info))
  7244. {
  7245. if (scriptContext->IsUndeclBlockVar(value))
  7246. {
  7247. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  7248. }
  7249. return value;
  7250. }
  7251. END_TYPEOF_ERROR_HANDLER(scriptContext, value);
  7252. value = scriptContext->GetLibrary()->GetUndefined();
  7253. return value;
  7254. JIT_HELPER_END(Op_PatchGetRootValueForTypeOf);
  7255. }
  7256. JIT_HELPER_TEMPLATE(Op_PatchGetRootValueForTypeOf, Op_PatchGetRootValuePolymorphicForTypeOf)
  7257. template Var JavascriptOperators::PatchGetRootValueForTypeOf<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7258. template Var JavascriptOperators::PatchGetRootValueForTypeOf<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7259. template Var JavascriptOperators::PatchGetRootValueForTypeOf<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7260. template Var JavascriptOperators::PatchGetRootValueForTypeOf<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7261. Var JavascriptOperators::PatchGetRootValueNoFastPath_Var(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7262. {
  7263. return
  7264. PatchGetRootValueNoFastPath(
  7265. functionBody,
  7266. inlineCache,
  7267. inlineCacheIndex,
  7268. VarTo<DynamicObject>(instance),
  7269. propertyId);
  7270. }
  7271. Var JavascriptOperators::PatchGetRootValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId)
  7272. {
  7273. AssertMsg(VarIs<RootObjectBase>(object), "Root must be a global object!");
  7274. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7275. PropertyValueInfo info;
  7276. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7277. return JavascriptOperators::OP_GetRootProperty(object, propertyId, &info, scriptContext);
  7278. }
  7279. template <bool IsFromFullJit, class TInlineCache>
  7280. inline Var JavascriptOperators::PatchGetPropertyScoped(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance)
  7281. {
  7282. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetPropertyScoped);
  7283. // Get the property, using a scope stack rather than an individual instance.
  7284. // Walk the stack until we find an instance that has the property.
  7285. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7286. uint16 length = pDisplay->GetLength();
  7287. PropertyValueInfo info;
  7288. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7289. for (uint16 i = 0; i < length; i++)
  7290. {
  7291. RecyclableObject* object = UnsafeVarTo<RecyclableObject>(pDisplay->GetItem(i));
  7292. Var value;
  7293. if (CacheOperators::TryGetProperty<true, true, true, false, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7294. object, false, object, propertyId, &value, scriptContext, nullptr, &info))
  7295. {
  7296. return value;
  7297. }
  7298. #if DBG_DUMP
  7299. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7300. {
  7301. CacheOperators::TraceCache(inlineCache, _u("PatchGetPropertyScoped"), propertyId, scriptContext, object);
  7302. }
  7303. #endif
  7304. if (JavascriptOperators::GetProperty(object, propertyId, &value, scriptContext, &info))
  7305. {
  7306. if (scriptContext->IsUndeclBlockVar(value) && propertyId != PropertyIds::_this)
  7307. {
  7308. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  7309. }
  7310. return value;
  7311. }
  7312. }
  7313. // There is no root decl for 'this', we should instead load the global 'this' value.
  7314. if (propertyId == PropertyIds::_this)
  7315. {
  7316. Var varNull = OP_LdNull(scriptContext);
  7317. return JavascriptOperators::OP_GetThis(varNull, functionBody->GetModuleID(), scriptContext);
  7318. }
  7319. else if (propertyId == PropertyIds::_super)
  7320. {
  7321. JavascriptError::ThrowReferenceError(scriptContext, JSERR_BadSuperReference);
  7322. }
  7323. // No one in the scope stack has the property, so get it from the default instance provided by the caller.
  7324. Var value = JavascriptOperators::PatchGetRootValue<IsFromFullJit>(functionBody, inlineCache, inlineCacheIndex, VarTo<DynamicObject>(defaultInstance), propertyId);
  7325. if (scriptContext->IsUndeclBlockVar(value))
  7326. {
  7327. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  7328. }
  7329. return value;
  7330. JIT_HELPER_END(Op_PatchGetPropertyScoped);
  7331. }
  7332. template Var JavascriptOperators::PatchGetPropertyScoped<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7333. template Var JavascriptOperators::PatchGetPropertyScoped<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7334. template Var JavascriptOperators::PatchGetPropertyScoped<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7335. template Var JavascriptOperators::PatchGetPropertyScoped<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7336. template <bool IsFromFullJit, class TInlineCache>
  7337. Var JavascriptOperators::PatchGetPropertyForTypeOfScoped(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance)
  7338. {
  7339. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetPropertyForTypeOfScoped);
  7340. Var value = nullptr;
  7341. ScriptContext *scriptContext = functionBody->GetScriptContext();
  7342. BEGIN_TYPEOF_ERROR_HANDLER(scriptContext);
  7343. value = JavascriptOperators::PatchGetPropertyScoped<IsFromFullJit, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, pDisplay, propertyId, defaultInstance);
  7344. END_TYPEOF_ERROR_HANDLER(scriptContext, value)
  7345. return value;
  7346. JIT_HELPER_END(Op_PatchGetPropertyForTypeOfScoped);
  7347. }
  7348. template Var JavascriptOperators::PatchGetPropertyForTypeOfScoped<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7349. template Var JavascriptOperators::PatchGetPropertyForTypeOfScoped<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7350. template Var JavascriptOperators::PatchGetPropertyForTypeOfScoped<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7351. template Var JavascriptOperators::PatchGetPropertyForTypeOfScoped<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7352. template <bool IsFromFullJit, class TInlineCache>
  7353. inline Var JavascriptOperators::PatchGetMethod(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7354. {
  7355. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetMethod);
  7356. Assert(inlineCache != nullptr);
  7357. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7358. RecyclableObject* object = nullptr;
  7359. #if ENABLE_COPYONACCESS_ARRAY
  7360. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  7361. #endif
  7362. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7363. {
  7364. // Don't error if we disabled implicit calls
  7365. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7366. {
  7367. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7368. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7369. }
  7370. else
  7371. {
  7372. #ifdef TELEMETRY_JSO
  7373. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  7374. {
  7375. // `successful` will be true as PatchGetMethod throws an exception if not found.
  7376. scriptContext->GetTelemetry().GetOpcodeTelemetry().GetMethodProperty(object, propertyId, value, /*successful:*/false);
  7377. }
  7378. #endif
  7379. return scriptContext->GetLibrary()->GetUndefined();
  7380. }
  7381. }
  7382. PropertyValueInfo info;
  7383. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7384. Var value;
  7385. if (CacheOperators::TryGetProperty<true, true, true, false, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7386. instance, false, object, propertyId, &value, scriptContext, nullptr, &info))
  7387. {
  7388. return value;
  7389. }
  7390. #if DBG_DUMP
  7391. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7392. {
  7393. CacheOperators::TraceCache(inlineCache, _u("PatchGetMethod"), propertyId, scriptContext, object);
  7394. }
  7395. #endif
  7396. value = Js::JavascriptOperators::PatchGetMethodFromObject(instance, object, propertyId, &info, scriptContext, false);
  7397. #ifdef TELEMETRY_JSO
  7398. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  7399. {
  7400. // `successful` will be true as PatchGetMethod throws an exception if not found.
  7401. scriptContext->GetTelemetry().GetOpcodeTelemetry().GetMethodProperty(object, propertyId, value, /*successful:*/true);
  7402. }
  7403. #endif
  7404. return value;
  7405. JIT_HELPER_END(Op_PatchGetMethod);
  7406. }
  7407. JIT_HELPER_TEMPLATE(Op_PatchGetMethod, Op_PatchGetMethodPolymorphic)
  7408. template Var JavascriptOperators::PatchGetMethod<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7409. template Var JavascriptOperators::PatchGetMethod<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7410. template Var JavascriptOperators::PatchGetMethod<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7411. template Var JavascriptOperators::PatchGetMethod<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7412. template <bool IsFromFullJit, class TInlineCache>
  7413. inline Var JavascriptOperators::PatchGetRootMethod(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId)
  7414. {
  7415. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetRootMethod);
  7416. Assert(inlineCache != nullptr);
  7417. AssertMsg(VarIs<RootObjectBase>(object), "Root must be a global object!");
  7418. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7419. PropertyValueInfo info;
  7420. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7421. Var value;
  7422. if (CacheOperators::TryGetProperty<true, true, true, false, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7423. object, true, object, propertyId, &value, scriptContext, nullptr, &info))
  7424. {
  7425. return value;
  7426. }
  7427. #if DBG_DUMP
  7428. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7429. {
  7430. CacheOperators::TraceCache(inlineCache, _u("PatchGetRootMethod"), propertyId, scriptContext, object);
  7431. }
  7432. #endif
  7433. value = Js::JavascriptOperators::PatchGetMethodFromObject(object, object, propertyId, &info, scriptContext, true);
  7434. #ifdef TELEMETRY_JSO
  7435. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  7436. {
  7437. // `successful` will be true as PatchGetMethod throws an exception if not found.
  7438. scriptContext->GetTelemetry().GetOpcodeTelemetry().GetMethodProperty(object, propertyId, value, /*successful:*/ true);
  7439. }
  7440. #endif
  7441. return value;
  7442. JIT_HELPER_END(Op_PatchGetRootMethod);
  7443. }
  7444. JIT_HELPER_TEMPLATE(Op_PatchGetRootMethod, Op_PatchGetRootMethodPolymorphic)
  7445. template Var JavascriptOperators::PatchGetRootMethod<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId);
  7446. template Var JavascriptOperators::PatchGetRootMethod<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId);
  7447. template Var JavascriptOperators::PatchGetRootMethod<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId);
  7448. template Var JavascriptOperators::PatchGetRootMethod<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId);
  7449. template <bool IsFromFullJit, class TInlineCache>
  7450. inline Var JavascriptOperators::PatchScopedGetMethod(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7451. {
  7452. JIT_HELPER_REENTRANT_HEADER(Op_ScopedGetMethod);
  7453. Assert(inlineCache != nullptr);
  7454. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7455. RecyclableObject* object = nullptr;
  7456. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7457. {
  7458. // Don't error if we disabled implicit calls
  7459. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7460. {
  7461. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7462. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7463. }
  7464. else
  7465. {
  7466. return scriptContext->GetLibrary()->GetUndefined();
  7467. }
  7468. }
  7469. PropertyValueInfo info;
  7470. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7471. const bool isRoot = VarIs<RootObjectBase>(object);
  7472. Var value;
  7473. if (CacheOperators::TryGetProperty<true, true, true, false, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7474. instance, isRoot, object, propertyId, &value, scriptContext, nullptr, &info))
  7475. {
  7476. return value;
  7477. }
  7478. #if DBG_DUMP
  7479. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7480. {
  7481. CacheOperators::TraceCache(inlineCache, _u("PatchGetMethod"), propertyId, scriptContext, object);
  7482. }
  7483. #endif
  7484. return Js::JavascriptOperators::PatchGetMethodFromObject(instance, object, propertyId, &info, scriptContext, isRoot);
  7485. JIT_HELPER_END(Op_ScopedGetMethod);
  7486. }
  7487. JIT_HELPER_TEMPLATE(Op_ScopedGetMethod, Op_ScopedGetMethodPolymorphic)
  7488. template Var JavascriptOperators::PatchScopedGetMethod<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7489. template Var JavascriptOperators::PatchScopedGetMethod<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7490. template Var JavascriptOperators::PatchScopedGetMethod<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7491. template Var JavascriptOperators::PatchScopedGetMethod<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7492. Var JavascriptOperators::PatchGetMethodNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7493. {
  7494. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7495. RecyclableObject* object = nullptr;
  7496. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7497. {
  7498. // Don't error if we disabled implicit calls
  7499. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7500. {
  7501. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7502. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7503. }
  7504. else
  7505. {
  7506. return scriptContext->GetLibrary()->GetUndefined();
  7507. }
  7508. }
  7509. PropertyValueInfo info;
  7510. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7511. return Js::JavascriptOperators::PatchGetMethodFromObject(instance, object, propertyId, &info, scriptContext, false);
  7512. }
  7513. Var JavascriptOperators::PatchGetRootMethodNoFastPath_Var(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7514. {
  7515. return
  7516. PatchGetRootMethodNoFastPath(
  7517. functionBody,
  7518. inlineCache,
  7519. inlineCacheIndex,
  7520. VarTo<DynamicObject>(instance),
  7521. propertyId);
  7522. }
  7523. Var JavascriptOperators::PatchGetRootMethodNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId)
  7524. {
  7525. AssertMsg(VarIs<RootObjectBase>(object), "Root must be a global object!");
  7526. PropertyValueInfo info;
  7527. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7528. return Js::JavascriptOperators::PatchGetMethodFromObject(object, object, propertyId, &info, functionBody->GetScriptContext(), true);
  7529. }
  7530. Var JavascriptOperators::PatchGetMethodFromObject(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, PropertyValueInfo * info, ScriptContext* scriptContext, bool isRootLd)
  7531. {
  7532. Assert(IsPropertyObject(propertyObject));
  7533. Var value = nullptr;
  7534. BOOL foundValue = FALSE;
  7535. if (isRootLd)
  7536. {
  7537. RootObjectBase* rootObject = VarTo<RootObjectBase>(instance);
  7538. foundValue = JavascriptOperators::GetRootPropertyReference(rootObject, propertyId, &value, scriptContext, info);
  7539. }
  7540. else
  7541. {
  7542. foundValue = JavascriptOperators::GetPropertyReference(instance, propertyObject, propertyId, &value, scriptContext, info);
  7543. }
  7544. if (!foundValue)
  7545. {
  7546. // Don't error if we disabled implicit calls
  7547. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7548. {
  7549. const char16* propertyName = scriptContext->GetPropertyName(propertyId)->GetBuffer();
  7550. value = scriptContext->GetLibrary()->GetUndefined();
  7551. JavascriptFunction * caller = NULL;
  7552. if (JavascriptStackWalker::GetCaller(&caller, scriptContext))
  7553. {
  7554. FunctionBody * callerBody = caller->GetFunctionBody();
  7555. if (callerBody && callerBody->GetUtf8SourceInfo()->GetIsXDomain())
  7556. {
  7557. propertyName = NULL;
  7558. }
  7559. }
  7560. // Prior to version 12 we had mistakenly immediately thrown an error for property reference method calls
  7561. // (i.e. <expr>.foo() form) when the target object is the global object. The spec says that a GetValue
  7562. // on a reference should throw if the reference is unresolved, of which a property reference can never be,
  7563. // however it can be unresolved in the case of an identifier expression, e.g. foo() with no qualification.
  7564. // Such a case would come down to the global object if foo was undefined, hence the check for root object,
  7565. // except that it should have been a check for isRootLd to be correct.
  7566. //
  7567. // // (at global scope)
  7568. // foo(x());
  7569. //
  7570. // should throw an error before evaluating x() if foo is not defined, but
  7571. //
  7572. // // (at global scope)
  7573. // this.foo(x());
  7574. //
  7575. // should evaluate x() before throwing an error if foo is not a property on the global object.
  7576. // Maintain old behavior prior to version 12.
  7577. bool isPropertyReference = !isRootLd;
  7578. if (!isPropertyReference)
  7579. {
  7580. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UndefVariable, propertyName);
  7581. }
  7582. else
  7583. {
  7584. // 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
  7585. // first (#3). Postpone throwing error to invoke time.
  7586. value = ThrowErrorObject::CreateThrowTypeErrorObject(scriptContext, VBSERR_OLENoPropOrMethod, propertyName);
  7587. }
  7588. }
  7589. }
  7590. return value;
  7591. }
  7592. template <bool IsFromFullJit, class TInlineCache>
  7593. inline void JavascriptOperators::PatchPutValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7594. {
  7595. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValue);
  7596. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValue, Op_PatchPutValueWithThisPtr);
  7597. PatchPutValueWithThisPtr<IsFromFullJit, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7598. JIT_HELPER_END(Op_PatchPutValue);
  7599. }
  7600. JIT_HELPER_TEMPLATE(Op_PatchPutValue, Op_PatchPutValuePolymorphic)
  7601. template <bool IsFromFullJit, class TInlineCache>
  7602. inline void JavascriptOperators::PatchPutValueWithThisPtr(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7603. {
  7604. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtr);
  7605. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7606. if (TaggedNumber::Is(instance))
  7607. {
  7608. JavascriptOperators::SetPropertyOnTaggedNumber(instance, nullptr, propertyId, newValue, scriptContext, flags);
  7609. return;
  7610. }
  7611. #if ENABLE_COPYONACCESS_ARRAY
  7612. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  7613. #endif
  7614. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  7615. PropertyValueInfo info;
  7616. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7617. if (CacheOperators::TrySetProperty<true, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7618. object, false, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7619. {
  7620. return;
  7621. }
  7622. #if DBG_DUMP
  7623. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7624. {
  7625. CacheOperators::TraceCache(inlineCache, _u("PatchPutValue"), propertyId, scriptContext, object);
  7626. }
  7627. #endif
  7628. ImplicitCallFlags prevImplicitCallFlags = ImplicitCall_None;
  7629. ImplicitCallFlags currImplicitCallFlags = ImplicitCall_None;
  7630. bool hasThisOnlyStatements = functionBody->GetHasOnlyThisStmts();
  7631. if (hasThisOnlyStatements)
  7632. {
  7633. prevImplicitCallFlags = CacheAndClearImplicitBit(scriptContext);
  7634. }
  7635. if (!JavascriptOperators::OP_SetProperty(object, propertyId, newValue, scriptContext, &info, flags, thisInstance))
  7636. {
  7637. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7638. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7639. }
  7640. if (hasThisOnlyStatements)
  7641. {
  7642. currImplicitCallFlags = CheckAndUpdateFunctionBodyWithImplicitFlag(functionBody);
  7643. RestoreImplicitFlag(scriptContext, prevImplicitCallFlags, currImplicitCallFlags);
  7644. }
  7645. JIT_HELPER_END(Op_PatchPutValueWithThisPtr);
  7646. }
  7647. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtr, Op_PatchPutValueWithThisPtrPolymorphic)
  7648. template void JavascriptOperators::PatchPutValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7649. template void JavascriptOperators::PatchPutValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7650. template void JavascriptOperators::PatchPutValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7651. template void JavascriptOperators::PatchPutValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7652. template <bool IsFromFullJit, class TInlineCache>
  7653. inline void JavascriptOperators::PatchPutRootValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7654. {
  7655. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutRootValue);
  7656. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7657. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  7658. PropertyValueInfo info;
  7659. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7660. if (CacheOperators::TrySetProperty<true, true, true, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7661. object, true, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7662. {
  7663. return;
  7664. }
  7665. #if DBG_DUMP
  7666. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7667. {
  7668. CacheOperators::TraceCache(inlineCache, _u("PatchPutRootValue"), propertyId, scriptContext, object);
  7669. }
  7670. #endif
  7671. ImplicitCallFlags prevImplicitCallFlags = ImplicitCall_None;
  7672. ImplicitCallFlags currImplicitCallFlags = ImplicitCall_None;
  7673. bool hasThisOnlyStatements = functionBody->GetHasOnlyThisStmts();
  7674. if (hasThisOnlyStatements)
  7675. {
  7676. prevImplicitCallFlags = CacheAndClearImplicitBit(scriptContext);
  7677. }
  7678. if (!JavascriptOperators::SetRootProperty(object, propertyId, newValue, &info, scriptContext, flags))
  7679. {
  7680. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7681. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7682. }
  7683. if (hasThisOnlyStatements)
  7684. {
  7685. currImplicitCallFlags = CheckAndUpdateFunctionBodyWithImplicitFlag(functionBody);
  7686. RestoreImplicitFlag(scriptContext, prevImplicitCallFlags, currImplicitCallFlags);
  7687. }
  7688. JIT_HELPER_END(Op_PatchPutRootValue);
  7689. }
  7690. JIT_HELPER_TEMPLATE(Op_PatchPutRootValue, Op_PatchPutRootValuePolymorphic)
  7691. template void JavascriptOperators::PatchPutRootValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7692. template void JavascriptOperators::PatchPutRootValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7693. template void JavascriptOperators::PatchPutRootValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7694. template void JavascriptOperators::PatchPutRootValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7695. template <bool IsFromFullJit, class TInlineCache>
  7696. inline void JavascriptOperators::PatchPutValueNoLocalFastPath(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7697. {
  7698. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueNoLocalFastPath);
  7699. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueNoLocalFastPath, Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7700. PatchPutValueWithThisPtrNoLocalFastPath<IsFromFullJit, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7701. JIT_HELPER_END(Op_PatchPutValueNoLocalFastPath);
  7702. }
  7703. JIT_HELPER_TEMPLATE(Op_PatchPutValueNoLocalFastPath, Op_PatchPutValueNoLocalFastPathPolymorphic)
  7704. template void JavascriptOperators::PatchPutValueNoLocalFastPath<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7705. template void JavascriptOperators::PatchPutValueNoLocalFastPath<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7706. template void JavascriptOperators::PatchPutValueNoLocalFastPath<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7707. template void JavascriptOperators::PatchPutValueNoLocalFastPath<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7708. template <bool IsFromFullJit, class TInlineCache>
  7709. inline void JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPath(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7710. {
  7711. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7712. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7713. if (TaggedNumber::Is(instance))
  7714. {
  7715. JavascriptOperators::SetPropertyOnTaggedNumber(instance,
  7716. nullptr,
  7717. propertyId,
  7718. newValue,
  7719. scriptContext,
  7720. flags);
  7721. return;
  7722. }
  7723. #if ENABLE_COPYONACCESS_ARRAY
  7724. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  7725. #endif
  7726. RecyclableObject *object = UnsafeVarTo<RecyclableObject>(instance);
  7727. PropertyValueInfo info;
  7728. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7729. if (CacheOperators::TrySetProperty<!TInlineCache::IsPolymorphic, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7730. object, false, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7731. {
  7732. return;
  7733. }
  7734. #if DBG_DUMP
  7735. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7736. {
  7737. CacheOperators::TraceCache(inlineCache, _u("PatchPutValueNoLocalFastPath"), propertyId, scriptContext, object);
  7738. }
  7739. #endif
  7740. ImplicitCallFlags prevImplicitCallFlags = ImplicitCall_None;
  7741. ImplicitCallFlags currImplicitCallFlags = ImplicitCall_None;
  7742. bool hasThisOnlyStatements = functionBody->GetHasOnlyThisStmts();
  7743. if (hasThisOnlyStatements)
  7744. {
  7745. prevImplicitCallFlags = CacheAndClearImplicitBit(scriptContext);
  7746. }
  7747. if (!JavascriptOperators::OP_SetProperty(instance, propertyId, newValue, scriptContext, &info, flags, thisInstance))
  7748. {
  7749. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7750. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7751. }
  7752. if (hasThisOnlyStatements)
  7753. {
  7754. currImplicitCallFlags = CheckAndUpdateFunctionBodyWithImplicitFlag(functionBody);
  7755. RestoreImplicitFlag(scriptContext, prevImplicitCallFlags, currImplicitCallFlags);
  7756. }
  7757. JIT_HELPER_END(Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7758. }
  7759. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtrNoLocalFastPath, Op_PatchPutValueWithThisPtrNoLocalFastPathPolymorphic)
  7760. 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);
  7761. 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);
  7762. 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);
  7763. 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);
  7764. template <bool IsFromFullJit, class TInlineCache>
  7765. inline void JavascriptOperators::PatchPutRootValueNoLocalFastPath(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7766. {
  7767. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutRootValueNoLocalFastPath);
  7768. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7769. RecyclableObject *object = VarTo<RecyclableObject>(instance);
  7770. PropertyValueInfo info;
  7771. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7772. if (CacheOperators::TrySetProperty<!TInlineCache::IsPolymorphic, true, true, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7773. object, true, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7774. {
  7775. return;
  7776. }
  7777. #if DBG_DUMP
  7778. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7779. {
  7780. CacheOperators::TraceCache(inlineCache, _u("PatchPutRootValueNoLocalFastPath"), propertyId, scriptContext, object);
  7781. }
  7782. #endif
  7783. ImplicitCallFlags prevImplicitCallFlags = ImplicitCall_None;
  7784. ImplicitCallFlags currImplicitCallFlags = ImplicitCall_None;
  7785. bool hasThisOnlyStatements = functionBody->GetHasOnlyThisStmts();
  7786. if (hasThisOnlyStatements)
  7787. {
  7788. prevImplicitCallFlags = CacheAndClearImplicitBit(scriptContext);
  7789. }
  7790. if (!JavascriptOperators::SetRootProperty(object, propertyId, newValue, &info, scriptContext, flags))
  7791. {
  7792. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7793. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7794. }
  7795. if (hasThisOnlyStatements)
  7796. {
  7797. currImplicitCallFlags = CheckAndUpdateFunctionBodyWithImplicitFlag(functionBody);
  7798. RestoreImplicitFlag(scriptContext, prevImplicitCallFlags, currImplicitCallFlags);
  7799. }
  7800. JIT_HELPER_END(Op_PatchPutRootValueNoLocalFastPath);
  7801. }
  7802. JIT_HELPER_TEMPLATE(Op_PatchPutRootValueNoLocalFastPath, Op_PatchPutRootValueNoLocalFastPathPolymorphic)
  7803. template void JavascriptOperators::PatchPutRootValueNoLocalFastPath<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7804. template void JavascriptOperators::PatchPutRootValueNoLocalFastPath<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7805. template void JavascriptOperators::PatchPutRootValueNoLocalFastPath<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7806. template void JavascriptOperators::PatchPutRootValueNoLocalFastPath<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7807. void JavascriptOperators::PatchPutValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7808. {
  7809. PatchPutValueWithThisPtrNoFastPath(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7810. }
  7811. void JavascriptOperators::PatchPutValueWithThisPtrNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7812. {
  7813. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7814. if (TaggedNumber::Is(instance))
  7815. {
  7816. JavascriptOperators::SetPropertyOnTaggedNumber(instance, nullptr, propertyId, newValue, scriptContext, flags);
  7817. return;
  7818. }
  7819. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  7820. PropertyValueInfo info;
  7821. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7822. if (!JavascriptOperators::OP_SetProperty(object, propertyId, newValue, scriptContext, &info, flags, thisInstance))
  7823. {
  7824. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7825. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7826. }
  7827. }
  7828. void JavascriptOperators::PatchPutRootValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7829. {
  7830. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7831. RecyclableObject* object = VarTo<RecyclableObject>(instance);
  7832. PropertyValueInfo info;
  7833. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7834. if (!JavascriptOperators::SetRootProperty(object, propertyId, newValue, &info, scriptContext, flags))
  7835. {
  7836. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7837. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7838. }
  7839. }
  7840. template <class TInlineCache>
  7841. inline bool JavascriptOperators::PatchPutValueCantChangeType(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7842. {
  7843. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueCantChangeType);
  7844. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueCantChangeType, Op_PatchPutValue);
  7845. Type * oldType = VarIs<DynamicObject>(instance) ? UnsafeVarTo<DynamicObject>(instance)->GetType() : nullptr;
  7846. PatchPutValueWithThisPtr<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7847. return (oldType != nullptr && oldType != UnsafeVarTo<DynamicObject>(instance)->GetType());
  7848. JIT_HELPER_END(Op_PatchPutValueCantChangeType);
  7849. }
  7850. JIT_HELPER_TEMPLATE(Op_PatchPutValueCantChangeType, Op_PatchPutValuePolymorphicCantChangeType);
  7851. template bool JavascriptOperators::PatchPutValueCantChangeType<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7852. template bool JavascriptOperators::PatchPutValueCantChangeType<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7853. template <class TInlineCache>
  7854. inline bool JavascriptOperators::PatchPutValueWithThisPtrCantChangeType(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7855. {
  7856. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtrCantChangeType);
  7857. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueWithThisPtrCantChangeType, Op_PatchPutValueWithThisPtr);
  7858. Type * oldType = VarIs<DynamicObject>(instance) ? UnsafeVarTo<DynamicObject>(instance)->GetType() : nullptr;
  7859. PatchPutValueWithThisPtr<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, thisInstance, flags);
  7860. return (oldType != nullptr && oldType != UnsafeVarTo<DynamicObject>(instance)->GetType());
  7861. JIT_HELPER_END(Op_PatchPutValueWithThisPtrCantChangeType);
  7862. }
  7863. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtrCantChangeType, Op_PatchPutValueWithThisPtrPolymorphicCantChangeType);
  7864. template bool JavascriptOperators::PatchPutValueWithThisPtrCantChangeType<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7865. template bool JavascriptOperators::PatchPutValueWithThisPtrCantChangeType<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7866. template <class TInlineCache>
  7867. inline bool JavascriptOperators::PatchPutValueNoLocalFastPathCantChangeType(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7868. {
  7869. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueNoLocalFastPathCantChangeType);
  7870. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueNoLocalFastPathCantChangeType, Op_PatchPutValueNoLocalFastPath);
  7871. Type * oldType = VarIs<DynamicObject>(instance) ? UnsafeVarTo<DynamicObject>(instance)->GetType() : nullptr;
  7872. PatchPutValueWithThisPtrNoLocalFastPath<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7873. return (oldType != nullptr && oldType != UnsafeVarTo<DynamicObject>(instance)->GetType());
  7874. JIT_HELPER_END(Op_PatchPutValueNoLocalFastPathCantChangeType);
  7875. }
  7876. JIT_HELPER_TEMPLATE(Op_PatchPutValueNoLocalFastPathCantChangeType, Op_PatchPutValueNoLocalFastPathPolymorphicCantChangeType);
  7877. template bool JavascriptOperators::PatchPutValueNoLocalFastPathCantChangeType<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7878. template bool JavascriptOperators::PatchPutValueNoLocalFastPathCantChangeType<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7879. template <class TInlineCache>
  7880. inline bool JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPathCantChangeType(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7881. {
  7882. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtrNoLocalFastPathCantChangeType);
  7883. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueWithThisPtrNoLocalFastPathCantChangeType, Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7884. Type * oldType = VarIs<DynamicObject>(instance) ? UnsafeVarTo<DynamicObject>(instance)->GetType() : nullptr;
  7885. PatchPutValueWithThisPtrNoLocalFastPath<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, thisInstance, flags);
  7886. return (oldType != nullptr && oldType != UnsafeVarTo<DynamicObject>(instance)->GetType());
  7887. JIT_HELPER_END(Op_PatchPutValueWithThisPtrNoLocalFastPathCantChangeType);
  7888. }
  7889. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtrNoLocalFastPathCantChangeType, Op_PatchPutValueWithThisPtrNoLocalFastPathPolymorphicCantChangeType);
  7890. template bool JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPathCantChangeType<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7891. template bool JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPathCantChangeType<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7892. template <class TInlineCache>
  7893. inline bool JavascriptOperators::PatchInitValueCantChangeType(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue)
  7894. {
  7895. JIT_HELPER_REENTRANT_HEADER(Op_PatchInitValueCantChangeType);
  7896. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchInitValueCantChangeType, Op_PatchInitValue);
  7897. Type * oldType = VarIs<DynamicObject>(object) ? UnsafeVarTo<DynamicObject>(object)->GetType() : nullptr;
  7898. PatchInitValue<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, object, propertyId, newValue);
  7899. return (oldType != nullptr && oldType != UnsafeVarTo<DynamicObject>(object)->GetType());
  7900. JIT_HELPER_END(Op_PatchInitValueCantChangeType);
  7901. }
  7902. JIT_HELPER_TEMPLATE(Op_PatchInitValueCantChangeType, Op_PatchInitValuePolymorphicCantChangeType);
  7903. template bool JavascriptOperators::PatchInitValueCantChangeType<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7904. template bool JavascriptOperators::PatchInitValueCantChangeType<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7905. template <class TInlineCache>
  7906. inline bool JavascriptOperators::PatchPutValueCheckLayout(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7907. {
  7908. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueCheckLayout);
  7909. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueCheckLayout, Op_PatchPutValue);
  7910. DynamicTypeHandler * oldTypeHandler = VarIs<DynamicObject>(instance) ? UnsafeVarTo<DynamicObject>(instance)->GetTypeHandler() : nullptr;
  7911. PatchPutValueWithThisPtr<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7912. return (oldTypeHandler != nullptr && LayoutChanged(UnsafeVarTo<DynamicObject>(instance), oldTypeHandler));
  7913. JIT_HELPER_END(Op_PatchPutValueCheckLayout);
  7914. }
  7915. JIT_HELPER_TEMPLATE(Op_PatchPutValueCheckLayout, Op_PatchPutValuePolymorphicCheckLayout);
  7916. template bool JavascriptOperators::PatchPutValueCheckLayout<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7917. template bool JavascriptOperators::PatchPutValueCheckLayout<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7918. template <class TInlineCache>
  7919. inline bool JavascriptOperators::PatchPutValueWithThisPtrCheckLayout(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7920. {
  7921. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtrCheckLayout);
  7922. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueWithThisPtrCheckLayout, Op_PatchPutValueWithThisPtr);
  7923. DynamicTypeHandler * oldTypeHandler = VarIs<DynamicObject>(instance) ? UnsafeVarTo<DynamicObject>(instance)->GetTypeHandler() : nullptr;
  7924. PatchPutValueWithThisPtr<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, thisInstance, flags);
  7925. return (oldTypeHandler != nullptr && LayoutChanged(UnsafeVarTo<DynamicObject>(instance), oldTypeHandler));
  7926. JIT_HELPER_END(Op_PatchPutValueWithThisPtrCheckLayout);
  7927. }
  7928. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtrCheckLayout, Op_PatchPutValueWithThisPtrPolymorphicCheckLayout);
  7929. template bool JavascriptOperators::PatchPutValueWithThisPtrCheckLayout<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7930. template bool JavascriptOperators::PatchPutValueWithThisPtrCheckLayout<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7931. template <class TInlineCache>
  7932. inline bool JavascriptOperators::PatchPutValueNoLocalFastPathCheckLayout(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7933. {
  7934. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueNoLocalFastPathCheckLayout);
  7935. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueNoLocalFastPathCheckLayout, Op_PatchPutValueNoLocalFastPath);
  7936. DynamicTypeHandler * oldTypeHandler = VarIs<DynamicObject>(instance) ? UnsafeVarTo<DynamicObject>(instance)->GetTypeHandler() : nullptr;
  7937. PatchPutValueWithThisPtrNoLocalFastPath<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7938. return (oldTypeHandler != nullptr && LayoutChanged(UnsafeVarTo<DynamicObject>(instance), oldTypeHandler));
  7939. JIT_HELPER_END(Op_PatchPutValueNoLocalFastPathCheckLayout);
  7940. }
  7941. JIT_HELPER_TEMPLATE(Op_PatchPutValueNoLocalFastPathCheckLayout, Op_PatchPutValueNoLocalFastPathPolymorphicCheckLayout);
  7942. template bool JavascriptOperators::PatchPutValueNoLocalFastPathCheckLayout<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7943. template bool JavascriptOperators::PatchPutValueNoLocalFastPathCheckLayout<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7944. template <class TInlineCache>
  7945. inline bool JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPathCheckLayout(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7946. {
  7947. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtrNoLocalFastPathCheckLayout);
  7948. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueWithThisPtrNoLocalFastPathCheckLayout, Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7949. DynamicTypeHandler * oldTypeHandler = VarIs<DynamicObject>(instance) ? UnsafeVarTo<DynamicObject>(instance)->GetTypeHandler() : nullptr;
  7950. PatchPutValueWithThisPtrNoLocalFastPath<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, thisInstance, flags);
  7951. return (oldTypeHandler != nullptr && LayoutChanged(UnsafeVarTo<DynamicObject>(instance), oldTypeHandler));
  7952. JIT_HELPER_END(Op_PatchPutValueWithThisPtrNoLocalFastPathCheckLayout);
  7953. }
  7954. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtrNoLocalFastPathCheckLayout, Op_PatchPutValueWithThisPtrNoLocalFastPathPolymorphicCheckLayout);
  7955. template bool JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPathCheckLayout<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7956. template bool JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPathCheckLayout<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7957. template <class TInlineCache>
  7958. inline bool JavascriptOperators::PatchInitValueCheckLayout(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue)
  7959. {
  7960. JIT_HELPER_REENTRANT_HEADER(Op_PatchInitValueCheckLayout);
  7961. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchInitValueCheckLayout, Op_PatchInitValue);
  7962. DynamicTypeHandler * oldTypeHandler = VarIs<DynamicObject>(object) ? UnsafeVarTo<DynamicObject>(object)->GetTypeHandler() : nullptr;
  7963. PatchInitValue<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, object, propertyId, newValue);
  7964. return (oldTypeHandler != nullptr && LayoutChanged(UnsafeVarTo<DynamicObject>(object), oldTypeHandler));
  7965. JIT_HELPER_END(Op_PatchInitValueCheckLayout);
  7966. }
  7967. JIT_HELPER_TEMPLATE(Op_PatchInitValueCheckLayout, Op_PatchInitValuePolymorphicCheckLayout);
  7968. template bool JavascriptOperators::PatchInitValueCheckLayout<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7969. template bool JavascriptOperators::PatchInitValueCheckLayout<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7970. bool JavascriptOperators::LayoutChanged(DynamicObject *const instance, DynamicTypeHandler *const oldTypeHandler)
  7971. {
  7972. DynamicTypeHandler * newTypeHandler = instance->GetTypeHandler();
  7973. return (oldTypeHandler != newTypeHandler &&
  7974. (oldTypeHandler->GetInlineSlotCapacity() != newTypeHandler->GetInlineSlotCapacity() ||
  7975. oldTypeHandler->GetOffsetOfInlineSlots() != newTypeHandler->GetOffsetOfInlineSlots()));
  7976. }
  7977. template <bool IsFromFullJit, class TInlineCache>
  7978. inline void JavascriptOperators::PatchInitValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue)
  7979. {
  7980. JIT_HELPER_REENTRANT_HEADER(Op_PatchInitValue);
  7981. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7982. const PropertyOperationFlags flags = newValue == NULL ? PropertyOperation_SpecialValue : PropertyOperation_None;
  7983. PropertyValueInfo info;
  7984. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7985. if (CacheOperators::TrySetProperty<true, true, false, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7986. object, false, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7987. {
  7988. return;
  7989. }
  7990. #if DBG_DUMP
  7991. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7992. {
  7993. CacheOperators::TraceCache(inlineCache, _u("PatchInitValue"), propertyId, scriptContext, object);
  7994. }
  7995. #endif
  7996. Type *typeWithoutProperty = object->GetType();
  7997. if (functionBody->IsEval())
  7998. {
  7999. if (object->InitPropertyInEval(propertyId, newValue, flags, &info))
  8000. {
  8001. CacheOperators::CachePropertyWrite(object, false, typeWithoutProperty, propertyId, &info, scriptContext);
  8002. return;
  8003. }
  8004. }
  8005. // Ideally the lowerer would emit a call to the right flavor of PatchInitValue, so that we can ensure that we only
  8006. // ever initialize to NULL in the right cases. But the backend uses the StFld opcode for initialization, and it
  8007. // would be cumbersome to thread the different helper calls all the way down
  8008. if (object->InitProperty(propertyId, newValue, flags, &info))
  8009. {
  8010. CacheOperators::CachePropertyWrite(object, false, typeWithoutProperty, propertyId, &info, scriptContext);
  8011. }
  8012. JIT_HELPER_END(Op_PatchInitValue);
  8013. }
  8014. JIT_HELPER_TEMPLATE(Op_PatchInitValue, Op_PatchInitValuePolymorphic)
  8015. template void JavascriptOperators::PatchInitValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  8016. template void JavascriptOperators::PatchInitValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  8017. template void JavascriptOperators::PatchInitValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  8018. template void JavascriptOperators::PatchInitValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  8019. void JavascriptOperators::PatchInitValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue)
  8020. {
  8021. PropertyValueInfo info;
  8022. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  8023. Type *typeWithoutProperty = object->GetType();
  8024. if (functionBody->IsEval())
  8025. {
  8026. if (object->InitPropertyInEval(propertyId, newValue, PropertyOperation_None, &info))
  8027. {
  8028. CacheOperators::CachePropertyWrite(object, false, typeWithoutProperty, propertyId, &info, functionBody->GetScriptContext());
  8029. return;
  8030. }
  8031. }
  8032. if (object->InitProperty(propertyId, newValue, PropertyOperation_None, &info))
  8033. {
  8034. CacheOperators::CachePropertyWrite(object, false, typeWithoutProperty, propertyId, &info, functionBody->GetScriptContext());
  8035. }
  8036. }
  8037. void JavascriptOperators::GetPropertyIdForInt(uint64 value, ScriptContext* scriptContext, PropertyRecord const ** propertyRecord)
  8038. {
  8039. char16 buffer[20];
  8040. ::_ui64tow_s(value, buffer, sizeof(buffer)/sizeof(char16), 10);
  8041. scriptContext->GetOrAddPropertyRecord(buffer, JavascriptString::GetBufferLength(buffer), propertyRecord);
  8042. }
  8043. void JavascriptOperators::GetPropertyIdForInt(uint32 value, ScriptContext* scriptContext, PropertyRecord const ** propertyRecord)
  8044. {
  8045. GetPropertyIdForInt(static_cast<uint64>(value), scriptContext, propertyRecord);
  8046. }
  8047. Var JavascriptOperators::FromPropertyDescriptor(const PropertyDescriptor& descriptor, ScriptContext* scriptContext)
  8048. {
  8049. DynamicObject* object = scriptContext->GetLibrary()->CreateObject();
  8050. // ES5 Section 8.10.4 specifies the order for adding these properties.
  8051. if (descriptor.IsDataDescriptor())
  8052. {
  8053. if (descriptor.ValueSpecified())
  8054. {
  8055. JavascriptOperators::InitProperty(object, PropertyIds::value, descriptor.GetValue());
  8056. }
  8057. if (descriptor.WritableSpecified())
  8058. {
  8059. JavascriptOperators::InitProperty(object, PropertyIds::writable, JavascriptBoolean::ToVar(descriptor.IsWritable(), scriptContext));
  8060. }
  8061. }
  8062. else if (descriptor.IsAccessorDescriptor())
  8063. {
  8064. JavascriptOperators::InitProperty(object, PropertyIds::get, JavascriptOperators::CanonicalizeAccessor(descriptor.GetGetter(), scriptContext));
  8065. JavascriptOperators::InitProperty(object, PropertyIds::set, JavascriptOperators::CanonicalizeAccessor(descriptor.GetSetter(), scriptContext));
  8066. }
  8067. if (descriptor.EnumerableSpecified())
  8068. {
  8069. JavascriptOperators::InitProperty(object, PropertyIds::enumerable, JavascriptBoolean::ToVar(descriptor.IsEnumerable(), scriptContext));
  8070. }
  8071. if (descriptor.ConfigurableSpecified())
  8072. {
  8073. JavascriptOperators::InitProperty(object, PropertyIds::configurable, JavascriptBoolean::ToVar(descriptor.IsConfigurable(), scriptContext));
  8074. }
  8075. return object;
  8076. }
  8077. // ES5 8.12.9 [[DefineOwnProperty]].
  8078. // Return value:
  8079. // - TRUE = success.
  8080. // - FALSE (can throw depending on throwOnError parameter) = unsuccessful.
  8081. BOOL JavascriptOperators::DefineOwnPropertyDescriptor(RecyclableObject* obj, PropertyId propId, const PropertyDescriptor& descriptor, bool throwOnError, ScriptContext* scriptContext, PropertyOperationFlags flags /* = Js::PropertyOperation_None */)
  8082. {
  8083. Assert(obj);
  8084. Assert(scriptContext);
  8085. if (VarIs<JavascriptProxy>(obj))
  8086. {
  8087. return JavascriptProxy::DefineOwnPropertyDescriptor(obj, propId, descriptor, throwOnError, scriptContext, flags);
  8088. }
  8089. #ifdef _CHAKRACOREBUILD
  8090. else if (VarIs<CustomExternalWrapperObject>(obj))
  8091. {
  8092. // See if there is a trap for defineProperty.
  8093. BOOL wrapperResult = CustomExternalWrapperObject::DefineOwnPropertyDescriptor(obj, propId, descriptor, throwOnError, scriptContext);
  8094. if (wrapperResult)
  8095. {
  8096. return TRUE;
  8097. }
  8098. }
  8099. #endif
  8100. PropertyDescriptor currentDescriptor;
  8101. BOOL isCurrentDescriptorDefined = JavascriptOperators::GetOwnPropertyDescriptor(obj, propId, scriptContext, &currentDescriptor);
  8102. bool isExtensible = !!obj->IsExtensible();
  8103. return ValidateAndApplyPropertyDescriptor<true>(obj, propId, descriptor, isCurrentDescriptorDefined ? &currentDescriptor : nullptr, isExtensible, throwOnError, scriptContext);
  8104. }
  8105. BOOL JavascriptOperators::IsCompatiblePropertyDescriptor(const PropertyDescriptor& descriptor, PropertyDescriptor* currentDescriptor, bool isExtensible, bool throwOnError, ScriptContext* scriptContext)
  8106. {
  8107. return ValidateAndApplyPropertyDescriptor<false>(nullptr, Constants::NoProperty, descriptor, currentDescriptor, isExtensible, throwOnError, scriptContext);
  8108. }
  8109. template<bool needToSetProperty>
  8110. BOOL JavascriptOperators::ValidateAndApplyPropertyDescriptor(RecyclableObject* obj, PropertyId propId, const PropertyDescriptor& descriptor,
  8111. PropertyDescriptor* currentDescriptor, bool isExtensible, bool throwOnError, ScriptContext* scriptContext)
  8112. {
  8113. Var defaultDataValue = scriptContext->GetLibrary()->GetUndefined();
  8114. Var defaultAccessorValue = scriptContext->GetLibrary()->GetDefaultAccessorFunction();
  8115. if (currentDescriptor == nullptr)
  8116. {
  8117. if (!isExtensible) // ES5 8.12.9.3.
  8118. {
  8119. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotExtensible, propId);
  8120. }
  8121. else // ES5 8.12.9.4.
  8122. {
  8123. if (needToSetProperty)
  8124. {
  8125. if (descriptor.IsGenericDescriptor() || descriptor.IsDataDescriptor())
  8126. {
  8127. // ES5 8.12.9.4a: Create an own data property named P of object O whose [[Value]], [[Writable]],
  8128. // [[Enumerable]] and [[Configurable]] attribute values are described by Desc.
  8129. // If the value of an attribute field of Desc is absent, the attribute of the newly created property
  8130. // is set to its default value.
  8131. PropertyDescriptor filledDescriptor = FillMissingPropertyDescriptorFields<false>(descriptor, scriptContext);
  8132. BOOL tempResult = obj->SetPropertyWithAttributes(propId, filledDescriptor.GetValue(), filledDescriptor.GetAttributes(), nullptr);
  8133. if (!obj->IsExternal() && !tempResult)
  8134. {
  8135. Assert(
  8136. // Arrays return false when length property is non-writable and property is numeric
  8137. // and greater than or equal to length
  8138. DynamicObject::IsAnyArray(obj) ||
  8139. // Typed arrays return false when canonical numeric index is not integer or out of range
  8140. DynamicObject::IsAnyTypedArray(obj)
  8141. );
  8142. return FALSE;
  8143. }
  8144. }
  8145. else
  8146. {
  8147. // ES5 8.12.9.4b: Create an own accessor property named P of object O whose [[Get]], [[Set]], [[Enumerable]]
  8148. // and [[Configurable]] attribute values are described by Desc. If the value of an attribute field of Desc is absent,
  8149. // the attribute of the newly created property is set to its default value.
  8150. Assert(descriptor.IsAccessorDescriptor());
  8151. PropertyDescriptor filledDescriptor = FillMissingPropertyDescriptorFields<true>(descriptor, scriptContext);
  8152. BOOL isSetAccessorsSuccess = obj->SetAccessors(propId, filledDescriptor.GetGetter(), filledDescriptor.GetSetter());
  8153. // It is valid for some objects to not-support getters and setters, specifically, for projection of an ABI method
  8154. // (CustomExternalObject => MapWithStringKey) which SetAccessors returns VBSErr_ActionNotSupported.
  8155. // But for non-external objects SetAccessors should succeed.
  8156. Assert(isSetAccessorsSuccess || obj->IsExternal());
  8157. // If SetAccessors failed, the property wasn't created, so no need to change the attributes.
  8158. if (isSetAccessorsSuccess)
  8159. {
  8160. JavascriptOperators::SetAttributes(obj, propId, filledDescriptor, true); // use 'force' as default attributes in type system are different from ES5.
  8161. }
  8162. }
  8163. }
  8164. return TRUE;
  8165. }
  8166. }
  8167. // ES5 8.12.9.5: Return true, if every field in Desc is absent.
  8168. if (!descriptor.ConfigurableSpecified() && !descriptor.EnumerableSpecified() && !descriptor.WritableSpecified() &&
  8169. !descriptor.ValueSpecified() && !descriptor.GetterSpecified() && !descriptor.SetterSpecified())
  8170. {
  8171. return TRUE;
  8172. }
  8173. // 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
  8174. // as the corresponding field in current when compared using the SameValue algorithm (9.12).
  8175. PropertyDescriptor filledDescriptor = descriptor.IsAccessorDescriptor() ? FillMissingPropertyDescriptorFields<true>(descriptor, scriptContext)
  8176. : FillMissingPropertyDescriptorFields<false>(descriptor, scriptContext);
  8177. if (JavascriptOperators::AreSamePropertyDescriptors(&filledDescriptor, currentDescriptor, scriptContext))
  8178. {
  8179. return TRUE;
  8180. }
  8181. if (!currentDescriptor->IsConfigurable()) // ES5 8.12.9.7.
  8182. {
  8183. if (descriptor.ConfigurableSpecified() && descriptor.IsConfigurable())
  8184. {
  8185. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8186. }
  8187. if (descriptor.EnumerableSpecified() && descriptor.IsEnumerable() != currentDescriptor->IsEnumerable())
  8188. {
  8189. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8190. }
  8191. }
  8192. // Whether to merge attributes from tempDescriptor into descriptor to keep original values
  8193. // of some attributes from the object/use tempDescriptor for SetAttributes, or just use descriptor.
  8194. // This is optimization to avoid 2 calls to SetAttributes.
  8195. bool mergeDescriptors = false;
  8196. // Whether to call SetAttributes with 'force' flag which forces setting all attributes
  8197. // rather than only specified or which have true values.
  8198. // This is to make sure that the object has correct attributes, as default values in the object are not for ES5.
  8199. bool forceSetAttributes = false;
  8200. PropertyDescriptor tempDescriptor;
  8201. // ES5 8.12.9.8: If IsGenericDescriptor(Desc) is true, then no further validation is required.
  8202. if (!descriptor.IsGenericDescriptor())
  8203. {
  8204. if (currentDescriptor->IsDataDescriptor() != descriptor.IsDataDescriptor())
  8205. {
  8206. // ES5 8.12.9.9: Else, if IsDataDescriptor(current) and IsDataDescriptor(Desc) have different results...
  8207. if (!currentDescriptor->IsConfigurable())
  8208. {
  8209. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8210. }
  8211. if (needToSetProperty)
  8212. {
  8213. if (currentDescriptor->IsDataDescriptor())
  8214. {
  8215. // ES5 8.12.9.9.b: Convert the property named P of object O from a data property to an accessor property.
  8216. // Preserve the existing values of the converted property's [[Configurable]] and [[Enumerable]] attributes
  8217. // and set the rest of the property's attributes to their default values.
  8218. PropertyAttributes preserveFromObject = currentDescriptor->GetAttributes() & (PropertyConfigurable | PropertyEnumerable);
  8219. BOOL isSetAccessorsSuccess = obj->SetAccessors(propId, defaultAccessorValue, defaultAccessorValue);
  8220. // It is valid for some objects to not-support getters and setters, specifically, for projection of an ABI method
  8221. // (CustomExternalObject => MapWithStringKey) which SetAccessors returns VBSErr_ActionNotSupported.
  8222. // But for non-external objects SetAccessors should succeed.
  8223. Assert(isSetAccessorsSuccess || obj->IsExternal());
  8224. if (isSetAccessorsSuccess)
  8225. {
  8226. tempDescriptor.SetAttributes(preserveFromObject, PropertyConfigurable | PropertyEnumerable);
  8227. forceSetAttributes = true; // use SetAttributes with 'force' as default attributes in type system are different from ES5.
  8228. mergeDescriptors = true;
  8229. }
  8230. }
  8231. else
  8232. {
  8233. // ES5 8.12.9.9.c: Convert the property named P of object O from an accessor property to a data property.
  8234. // Preserve the existing values of the converted property's [[Configurable]] and [[Enumerable]] attributes
  8235. // and set the rest of the property's attributes to their default values.
  8236. // Note: avoid using SetProperty/SetPropertyWithAttributes here because they has undesired side-effects:
  8237. // it calls previous setter and in some cases of attribute values throws.
  8238. // To walk around, call DeleteProperty and then AddProperty.
  8239. PropertyAttributes preserveFromObject = currentDescriptor->GetAttributes() & (PropertyConfigurable | PropertyEnumerable);
  8240. tempDescriptor.SetAttributes(preserveFromObject, PropertyConfigurable | PropertyEnumerable);
  8241. tempDescriptor.MergeFrom(descriptor); // Update only fields specified in 'descriptor'.
  8242. Var descriptorValue = descriptor.ValueSpecified() ? descriptor.GetValue() : defaultDataValue;
  8243. // Note: HostDispath'es implementation of DeleteProperty currently throws E_NOTIMPL.
  8244. obj->DeleteProperty(propId, PropertyOperation_None);
  8245. BOOL tempResult = obj->SetPropertyWithAttributes(propId, descriptorValue, tempDescriptor.GetAttributes(), NULL, PropertyOperation_Force);
  8246. Assert(tempResult);
  8247. // At this time we already set value and attributes to desired values,
  8248. // thus we can skip step ES5 8.12.9.12 and simply return true.
  8249. return TRUE;
  8250. }
  8251. }
  8252. }
  8253. else if (currentDescriptor->IsDataDescriptor() && descriptor.IsDataDescriptor())
  8254. {
  8255. // ES5 8.12.9.10: Else, if IsDataDescriptor(current) and IsDataDescriptor(Desc) are both true...
  8256. if (!currentDescriptor->IsConfigurable())
  8257. {
  8258. if (!currentDescriptor->IsWritable())
  8259. {
  8260. if (descriptor.WritableSpecified() && descriptor.IsWritable()) // ES5 8.12.9.10.a.i
  8261. {
  8262. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8263. }
  8264. else if (descriptor.ValueSpecified() &&
  8265. !JavascriptConversion::SameValue(descriptor.GetValue(), currentDescriptor->GetValue())) // ES5 8.12.9.10.a.ii
  8266. {
  8267. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotWritable, propId);
  8268. }
  8269. }
  8270. }
  8271. // ES5 8.12.9.10.b: else, the [[Configurable]] field of current is true, so any change is acceptable.
  8272. }
  8273. else
  8274. {
  8275. // ES5 8.12.9.11: Else, IsAccessorDescriptor(current) and IsAccessorDescriptor(Desc) are both true, so...
  8276. Assert(currentDescriptor->IsAccessorDescriptor() && descriptor.IsAccessorDescriptor());
  8277. if (!currentDescriptor->IsConfigurable())
  8278. {
  8279. if ((descriptor.SetterSpecified() &&
  8280. !JavascriptConversion::SameValue(
  8281. JavascriptOperators::CanonicalizeAccessor(descriptor.GetSetter(), scriptContext),
  8282. JavascriptOperators::CanonicalizeAccessor(currentDescriptor->GetSetter(), scriptContext))) ||
  8283. (descriptor.GetterSpecified() &&
  8284. !JavascriptConversion::SameValue(
  8285. JavascriptOperators::CanonicalizeAccessor(descriptor.GetGetter(), scriptContext),
  8286. JavascriptOperators::CanonicalizeAccessor(currentDescriptor->GetGetter(), scriptContext))))
  8287. {
  8288. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8289. }
  8290. }
  8291. }
  8292. // This part is only for non-generic descriptors:
  8293. // ES5 8.12.9.12: For each attribute field of Desc that is present,
  8294. // set the correspondingly named attribute of the property named P of object O to the value of the field.
  8295. if (descriptor.IsDataDescriptor())
  8296. {
  8297. if (descriptor.ValueSpecified() && needToSetProperty)
  8298. {
  8299. // Set just the value by passing the current attributes of the property.
  8300. // If the property's attributes are also changing (perhaps becoming non-writable),
  8301. // this will be taken care of in the call to JavascriptOperators::SetAttributes below.
  8302. // Built-in Function.prototype properties 'length', 'arguments', and 'caller' are special cases.
  8303. BOOL tempResult = obj->SetPropertyWithAttributes(propId, descriptor.GetValue(), currentDescriptor->GetAttributes(), nullptr);
  8304. AssertMsg(tempResult || JavascriptFunction::IsBuiltinProperty(obj, propId), "If you hit this assert, most likely there is something wrong with the object/type.");
  8305. }
  8306. }
  8307. else if (descriptor.IsAccessorDescriptor() && needToSetProperty)
  8308. {
  8309. Assert(descriptor.GetterSpecified() || descriptor.SetterSpecified());
  8310. Var oldGetter = defaultAccessorValue, oldSetter = defaultAccessorValue;
  8311. if (!descriptor.GetterSpecified() || !descriptor.SetterSpecified())
  8312. {
  8313. // Unless both getter and setter are specified, make sure we don't overwrite old accessor.
  8314. #pragma prefast(suppress:6031, "We defaulted oldGetter and oldSetter already, so ignoring the return value here is safe")
  8315. obj->GetAccessors(propId, &oldGetter, &oldSetter, scriptContext);
  8316. }
  8317. Var getter = descriptor.GetterSpecified() ? descriptor.GetGetter() : oldGetter;
  8318. Var setter = descriptor.SetterSpecified() ? descriptor.GetSetter() : oldSetter;
  8319. obj->SetAccessors(propId, getter, setter);
  8320. }
  8321. } // if (!descriptor.IsGenericDescriptor())
  8322. // Continue for all descriptors including generic:
  8323. // ES5 8.12.9.12: For each attribute field of Desc that is present,
  8324. // set the correspondingly named attribute of the property named P of object O to the value of the field.
  8325. if (needToSetProperty)
  8326. {
  8327. if (mergeDescriptors)
  8328. {
  8329. tempDescriptor.MergeFrom(descriptor);
  8330. JavascriptOperators::SetAttributes(obj, propId, tempDescriptor, forceSetAttributes);
  8331. }
  8332. else
  8333. {
  8334. JavascriptOperators::SetAttributes(obj, propId, descriptor, forceSetAttributes);
  8335. }
  8336. }
  8337. return TRUE;
  8338. }
  8339. template <bool isAccessor>
  8340. PropertyDescriptor JavascriptOperators::FillMissingPropertyDescriptorFields(PropertyDescriptor descriptor, ScriptContext* scriptContext)
  8341. {
  8342. PropertyDescriptor newDescriptor;
  8343. const PropertyDescriptor* defaultDescriptor = scriptContext->GetLibrary()->GetDefaultPropertyDescriptor();
  8344. if (isAccessor)
  8345. {
  8346. newDescriptor.SetGetter(descriptor.GetterSpecified() ? descriptor.GetGetter() : defaultDescriptor->GetGetter());
  8347. newDescriptor.SetSetter(descriptor.SetterSpecified() ? descriptor.GetSetter() : defaultDescriptor->GetSetter());
  8348. }
  8349. else
  8350. {
  8351. newDescriptor.SetValue(descriptor.ValueSpecified() ? descriptor.GetValue() : defaultDescriptor->GetValue());
  8352. newDescriptor.SetWritable(descriptor.WritableSpecified() ? descriptor.IsWritable() : defaultDescriptor->IsWritable());
  8353. }
  8354. newDescriptor.SetConfigurable(descriptor.ConfigurableSpecified() ? descriptor.IsConfigurable() : defaultDescriptor->IsConfigurable());
  8355. newDescriptor.SetEnumerable(descriptor.EnumerableSpecified() ? descriptor.IsEnumerable() : defaultDescriptor->IsEnumerable());
  8356. return newDescriptor;
  8357. }
  8358. // ES5: 15.4.5.1
  8359. BOOL JavascriptOperators::DefineOwnPropertyForArray(JavascriptArray* arr, PropertyId propId, const PropertyDescriptor& descriptor, bool throwOnError, ScriptContext* scriptContext)
  8360. {
  8361. if (propId == PropertyIds::length)
  8362. {
  8363. if (!descriptor.ValueSpecified())
  8364. {
  8365. return DefineOwnPropertyDescriptor(arr, PropertyIds::length, descriptor, throwOnError, scriptContext);
  8366. }
  8367. PropertyDescriptor newLenDesc = descriptor;
  8368. uint32 newLen = ES5Array::ToLengthValue(descriptor.GetValue(), scriptContext);
  8369. newLenDesc.SetValue(JavascriptNumber::ToVar(newLen, scriptContext));
  8370. uint32 oldLen = arr->GetLength();
  8371. if (newLen >= oldLen)
  8372. {
  8373. return DefineOwnPropertyDescriptor(arr, PropertyIds::length, newLenDesc, throwOnError, scriptContext);
  8374. }
  8375. BOOL oldLenWritable = arr->IsWritable(PropertyIds::length);
  8376. if (!oldLenWritable)
  8377. {
  8378. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotWritable, propId);
  8379. }
  8380. bool newWritable = (!newLenDesc.WritableSpecified() || newLenDesc.IsWritable());
  8381. if (!newWritable)
  8382. {
  8383. // Need to defer setting writable to false in case any elements cannot be deleted
  8384. newLenDesc.SetWritable(true);
  8385. }
  8386. BOOL succeeded = DefineOwnPropertyDescriptor(arr, PropertyIds::length, newLenDesc, throwOnError, scriptContext);
  8387. //
  8388. // Our SetProperty(length) is also responsible to trim elements. When succeeded is
  8389. //
  8390. // false:
  8391. // * length attributes rejected
  8392. // * elements not touched
  8393. // true:
  8394. // * length attributes are set successfully
  8395. // * elements trimming may be either completed or incompleted, length value is correct
  8396. //
  8397. // * Strict mode TODO: Currently SetProperty(length) does not throw. If that throws, we need
  8398. // to update here to set correct newWritable even on exception.
  8399. //
  8400. if (!succeeded)
  8401. {
  8402. return false;
  8403. }
  8404. if (!newWritable) // Now set requested newWritable.
  8405. {
  8406. PropertyDescriptor newWritableDesc;
  8407. newWritableDesc.SetWritable(false);
  8408. DefineOwnPropertyDescriptor(arr, PropertyIds::length, newWritableDesc, false, scriptContext);
  8409. }
  8410. if (arr->GetLength() > newLen) // Delete incompleted
  8411. {
  8412. // Since SetProperty(length) not throwing, we'll reject here
  8413. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_Default, propId);
  8414. }
  8415. return true;
  8416. }
  8417. uint32 index;
  8418. if (scriptContext->IsNumericPropertyId(propId, &index))
  8419. {
  8420. if (index >= arr->GetLength() && !arr->IsWritable(PropertyIds::length))
  8421. {
  8422. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_LengthNotWritable, propId);
  8423. }
  8424. BOOL succeeded = DefineOwnPropertyDescriptor(arr, propId, descriptor, false, scriptContext);
  8425. if (!succeeded)
  8426. {
  8427. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_Default, propId);
  8428. }
  8429. // Out SetItem takes care of growing "length". we are done.
  8430. return true;
  8431. }
  8432. return DefineOwnPropertyDescriptor(arr, propId, descriptor, throwOnError, scriptContext);
  8433. }
  8434. // ES2017: 9.4.5.3 https://tc39.github.io/ecma262/#sec-integer-indexed-exotic-objects-defineownproperty-p-desc
  8435. BOOL JavascriptOperators::DefineOwnPropertyForTypedArray(TypedArrayBase* typedArray, PropertyId propId, const PropertyDescriptor& descriptor, bool throwOnError, ScriptContext* scriptContext)
  8436. {
  8437. // 1. Assert: IsPropertyKey(P) is true.
  8438. // 2. Assert: Assert: O is an Object that has a [[ViewedArrayBuffer]] internal slot.
  8439. const PropertyRecord* propertyRecord = scriptContext->GetPropertyName(propId);
  8440. // 3. If Type(P) is String, then
  8441. // a. Let numericIndex be ! CanonicalNumericIndexString(P).
  8442. // b. If numericIndex is not undefined, then
  8443. // i. if IsInteger(numbericIndex), return false
  8444. // ii. if numbericIndex = -0, return false
  8445. // iii. If numericIndex < 0, return false.
  8446. if (propertyRecord->IsNumeric()) {
  8447. uint32 uint32Index = propertyRecord->GetNumericValue();
  8448. // iv. Let length be O.[[ArrayLength]].
  8449. uint32 length = typedArray->GetLength();
  8450. // v. If numericIndex >= length, return false.
  8451. if (uint32Index >= length)
  8452. {
  8453. return Reject(throwOnError, scriptContext, JSERR_InvalidTypedArrayIndex, propId);
  8454. }
  8455. // vi. If IsAccessorDescriptor(Desc) is true, return false.
  8456. // vii. If Desc has a[[Configurable]] field and if Desc.[[Configurable]] is true, return false.
  8457. // viii. If Desc has an[[Enumerable]] field and if Desc.[[Enumerable]] is false, return false.
  8458. // ix. If Desc has a[[Writable]] field and if Desc.[[Writable]] is false, return false.
  8459. if (descriptor.IsAccessorDescriptor()
  8460. || (descriptor.ConfigurableSpecified() && descriptor.IsConfigurable())
  8461. || (descriptor.EnumerableSpecified() && !descriptor.IsEnumerable())
  8462. || (descriptor.WritableSpecified() && !descriptor.IsWritable()))
  8463. {
  8464. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8465. } // x. If Desc has a[[Value]] field, then
  8466. // 1. Let value be Desc.[[Value]].
  8467. // 2. Return ? IntegerIndexedElementSet(O, numericIndex, value).
  8468. if (descriptor.ValueSpecified())
  8469. {
  8470. Js::Var value = descriptor.GetValue();
  8471. return typedArray->DirectSetItem(uint32Index, value);
  8472. }
  8473. // xi. Return true.
  8474. return true;
  8475. }
  8476. if (!propertyRecord->IsSymbol())
  8477. {
  8478. PropertyString *propertyString = scriptContext->GetPropertyString(propId);
  8479. double result;
  8480. if (JavascriptConversion::CanonicalNumericIndexString(propertyString, &result, scriptContext))
  8481. {
  8482. return Reject(throwOnError, scriptContext, JSERR_InvalidTypedArrayIndex, propId);
  8483. }
  8484. }
  8485. // 4. Return ! OrdinaryDefineOwnProperty(O, P, Desc).
  8486. return DefineOwnPropertyDescriptor(typedArray, propId, descriptor, throwOnError, scriptContext);
  8487. }
  8488. BOOL JavascriptOperators::SetPropertyDescriptor(RecyclableObject* object, PropertyId propId, const PropertyDescriptor& descriptor)
  8489. {
  8490. if (descriptor.ValueSpecified())
  8491. {
  8492. ScriptContext* requestContext = object->GetScriptContext(); // Real requestContext?
  8493. JavascriptOperators::SetProperty(object, object, propId, descriptor.GetValue(), requestContext);
  8494. }
  8495. else if (descriptor.GetterSpecified() || descriptor.SetterSpecified())
  8496. {
  8497. JavascriptOperators::SetAccessors(object, propId, descriptor.GetGetter(), descriptor.GetSetter());
  8498. }
  8499. if (descriptor.EnumerableSpecified())
  8500. {
  8501. object->SetEnumerable(propId, descriptor.IsEnumerable());
  8502. }
  8503. if (descriptor.ConfigurableSpecified())
  8504. {
  8505. object->SetConfigurable(propId, descriptor.IsConfigurable());
  8506. }
  8507. if (descriptor.WritableSpecified())
  8508. {
  8509. object->SetWritable(propId, descriptor.IsWritable());
  8510. }
  8511. return true;
  8512. }
  8513. BOOL JavascriptOperators::ToPropertyDescriptorForProxyObjects(Var propertySpec, PropertyDescriptor* descriptor, ScriptContext* scriptContext)
  8514. {
  8515. if (!JavascriptOperators::IsObject(propertySpec))
  8516. {
  8517. return FALSE;
  8518. }
  8519. Var value;
  8520. RecyclableObject* propertySpecObj = VarTo<RecyclableObject>(propertySpec);
  8521. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::enumerable) == TRUE)
  8522. {
  8523. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::enumerable, &value, scriptContext))
  8524. {
  8525. descriptor->SetEnumerable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8526. }
  8527. else
  8528. {
  8529. // The proxy said we have the property, so we try to read the property and get the default value.
  8530. descriptor->SetEnumerable(false);
  8531. }
  8532. }
  8533. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::configurable) == TRUE)
  8534. {
  8535. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::configurable, &value, scriptContext))
  8536. {
  8537. descriptor->SetConfigurable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8538. }
  8539. else
  8540. {
  8541. // The proxy said we have the property, so we try to read the property and get the default value.
  8542. descriptor->SetConfigurable(false);
  8543. }
  8544. }
  8545. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::value) == TRUE)
  8546. {
  8547. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::value, &value, scriptContext))
  8548. {
  8549. descriptor->SetValue(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->SetValue(scriptContext->GetLibrary()->GetUndefined());
  8555. }
  8556. }
  8557. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::writable) == TRUE)
  8558. {
  8559. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::writable, &value, scriptContext))
  8560. {
  8561. descriptor->SetWritable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8562. }
  8563. else
  8564. {
  8565. // The proxy said we have the property, so we try to read the property and get the default value.
  8566. descriptor->SetWritable(false);
  8567. }
  8568. }
  8569. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::get) == TRUE)
  8570. {
  8571. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::get, &value, scriptContext))
  8572. {
  8573. if (JavascriptOperators::GetTypeId(value) != TypeIds_Undefined && (false == JavascriptConversion::IsCallable(value)))
  8574. {
  8575. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::get)->GetBuffer());
  8576. }
  8577. descriptor->SetGetter(value);
  8578. }
  8579. else
  8580. {
  8581. // The proxy said we have the property, so we try to read the property and get the default value.
  8582. descriptor->SetGetter(scriptContext->GetLibrary()->GetUndefined());
  8583. }
  8584. }
  8585. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::set) == TRUE)
  8586. {
  8587. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::set, &value, scriptContext))
  8588. {
  8589. if (JavascriptOperators::GetTypeId(value) != TypeIds_Undefined && (false == JavascriptConversion::IsCallable(value)))
  8590. {
  8591. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::set)->GetBuffer());
  8592. }
  8593. descriptor->SetSetter(value);
  8594. }
  8595. else
  8596. {
  8597. // The proxy said we have the property, so we try to read the property and get the default value.
  8598. descriptor->SetSetter(scriptContext->GetLibrary()->GetUndefined());
  8599. }
  8600. }
  8601. return TRUE;
  8602. }
  8603. BOOL JavascriptOperators::ToPropertyDescriptorForGenericObjects(Var propertySpec, PropertyDescriptor* descriptor, ScriptContext* scriptContext)
  8604. {
  8605. if (!JavascriptOperators::IsObject(propertySpec))
  8606. {
  8607. return FALSE;
  8608. }
  8609. Var value;
  8610. RecyclableObject* propertySpecObj = VarTo<RecyclableObject>(propertySpec);
  8611. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::enumerable, &value, scriptContext))
  8612. {
  8613. descriptor->SetEnumerable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8614. }
  8615. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::configurable, &value, scriptContext))
  8616. {
  8617. descriptor->SetConfigurable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8618. }
  8619. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::value, &value, scriptContext))
  8620. {
  8621. descriptor->SetValue(value);
  8622. }
  8623. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::writable, &value, scriptContext))
  8624. {
  8625. descriptor->SetWritable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8626. }
  8627. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::get, &value, scriptContext))
  8628. {
  8629. if (JavascriptOperators::GetTypeId(value) != TypeIds_Undefined && (false == JavascriptConversion::IsCallable(value)))
  8630. {
  8631. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::get)->GetBuffer());
  8632. }
  8633. descriptor->SetGetter(value);
  8634. }
  8635. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::set, &value, scriptContext))
  8636. {
  8637. if (JavascriptOperators::GetTypeId(value) != TypeIds_Undefined && (false == JavascriptConversion::IsCallable(value)))
  8638. {
  8639. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::set)->GetBuffer());
  8640. }
  8641. descriptor->SetSetter(value);
  8642. }
  8643. return TRUE;
  8644. }
  8645. BOOL JavascriptOperators::ToPropertyDescriptor(Var propertySpec, PropertyDescriptor* descriptor, ScriptContext* scriptContext)
  8646. {
  8647. if (VarIs<JavascriptProxy>(propertySpec) || (
  8648. VarIs<RecyclableObject>(propertySpec) &&
  8649. JavascriptOperators::CheckIfPrototypeChainContainsProxyObject(VarTo<RecyclableObject>(propertySpec)->GetPrototype())))
  8650. {
  8651. if (ToPropertyDescriptorForProxyObjects(propertySpec, descriptor, scriptContext) == FALSE)
  8652. {
  8653. return FALSE;
  8654. }
  8655. }
  8656. else
  8657. {
  8658. if (ToPropertyDescriptorForGenericObjects(propertySpec, descriptor, scriptContext) == FALSE)
  8659. {
  8660. return FALSE;
  8661. }
  8662. }
  8663. if (descriptor->GetterSpecified() || descriptor->SetterSpecified())
  8664. {
  8665. if (descriptor->ValueSpecified())
  8666. {
  8667. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotHaveAccessorsAndValue);
  8668. }
  8669. if (descriptor->WritableSpecified())
  8670. {
  8671. int32 hCode = descriptor->IsWritable() ? JSERR_InvalidAttributeTrue : JSERR_InvalidAttributeFalse;
  8672. JavascriptError::ThrowTypeError(scriptContext, hCode, _u("writable"));
  8673. }
  8674. }
  8675. descriptor->SetOriginal(propertySpec);
  8676. return TRUE;
  8677. }
  8678. void JavascriptOperators::CompletePropertyDescriptor(PropertyDescriptor* resultDescriptor, PropertyDescriptor* likeDescriptor, ScriptContext* requestContext)
  8679. {
  8680. const PropertyDescriptor* likePropertyDescriptor = likeDescriptor;
  8681. // 1. Assert: LikeDesc is either a Property Descriptor or undefined.
  8682. // 2. ReturnIfAbrupt(Desc).
  8683. // 3. Assert : Desc is a Property Descriptor
  8684. // 4. If LikeDesc is undefined, then set LikeDesc to Record{ [[Value]]: undefined, [[Writable]] : false, [[Get]] : undefined, [[Set]] : undefined, [[Enumerable]] : false, [[Configurable]] : false }.
  8685. if (likePropertyDescriptor == nullptr)
  8686. {
  8687. likePropertyDescriptor = requestContext->GetLibrary()->GetDefaultPropertyDescriptor();
  8688. }
  8689. // 5. If either IsGenericDescriptor(Desc) or IsDataDescriptor(Desc) is true, then
  8690. if (resultDescriptor->IsDataDescriptor() || resultDescriptor->IsGenericDescriptor())
  8691. {
  8692. // a.If Desc does not have a[[Value]] field, then set Desc.[[Value]] to LikeDesc.[[Value]].
  8693. // b.If Desc does not have a[[Writable]] field, then set Desc.[[Writable]] to LikeDesc.[[Writable]].
  8694. if (!resultDescriptor->ValueSpecified())
  8695. {
  8696. resultDescriptor->SetValue(likePropertyDescriptor->GetValue());
  8697. }
  8698. if (!resultDescriptor->WritableSpecified())
  8699. {
  8700. resultDescriptor->SetWritable(likePropertyDescriptor->IsWritable());
  8701. }
  8702. }
  8703. else
  8704. {
  8705. // 6. Else,
  8706. // a.If Desc does not have a[[Get]] field, then set Desc.[[Get]] to LikeDesc.[[Get]].
  8707. // b.If Desc does not have a[[Set]] field, then set Desc.[[Set]] to LikeDesc.[[Set]].
  8708. if (!resultDescriptor->GetterSpecified())
  8709. {
  8710. resultDescriptor->SetGetter(likePropertyDescriptor->GetGetter());
  8711. }
  8712. if (!resultDescriptor->SetterSpecified())
  8713. {
  8714. resultDescriptor->SetSetter(likePropertyDescriptor->GetSetter());
  8715. }
  8716. }
  8717. // 7. If Desc does not have an[[Enumerable]] field, then set Desc.[[Enumerable]] to LikeDesc.[[Enumerable]].
  8718. // 8. If Desc does not have a[[Configurable]] field, then set Desc.[[Configurable]] to LikeDesc.[[Configurable]].
  8719. // 9. Return Desc.
  8720. if (!resultDescriptor->EnumerableSpecified())
  8721. {
  8722. resultDescriptor->SetEnumerable(likePropertyDescriptor->IsEnumerable());
  8723. }
  8724. if (!resultDescriptor->ConfigurableSpecified())
  8725. {
  8726. resultDescriptor->SetConfigurable(likePropertyDescriptor->IsConfigurable());
  8727. }
  8728. }
  8729. // Conformance to: ES5 8.6.1.
  8730. // Set attributes on the object as provided by property descriptor.
  8731. // If force parameter is true, we force SetAttributes call even if none of the attributes are defined by the descriptor.
  8732. // NOTE: does not set [[Get]], [Set]], [[Value]]
  8733. void JavascriptOperators::SetAttributes(RecyclableObject* object, PropertyId propId, const PropertyDescriptor& descriptor, bool force)
  8734. {
  8735. Assert(object);
  8736. BOOL isWritable = FALSE;
  8737. if (descriptor.IsDataDescriptor())
  8738. {
  8739. isWritable = descriptor.WritableSpecified() ? descriptor.IsWritable() : FALSE;
  8740. }
  8741. else if (descriptor.IsAccessorDescriptor())
  8742. {
  8743. // The reason is that JavascriptOperators::OP_SetProperty checks for VarTo<RecyclableObject>(instance)->IsWritableOrAccessor(propertyId),
  8744. // which should in fact check for 'is writable or accessor' but since there is no GetAttributes, we can't do that efficiently.
  8745. isWritable = TRUE;
  8746. }
  8747. // CONSIDER: call object->SetAttributes which is much more efficient as that's 1 call instead of 3.
  8748. // Can't do that now as object->SetAttributes doesn't provide a way which attributes to modify and which not.
  8749. if (force || descriptor.ConfigurableSpecified())
  8750. {
  8751. object->SetConfigurable(propId, descriptor.ConfigurableSpecified() ? descriptor.IsConfigurable() : FALSE);
  8752. }
  8753. if (force || descriptor.EnumerableSpecified())
  8754. {
  8755. object->SetEnumerable(propId, descriptor.EnumerableSpecified() ? descriptor.IsEnumerable() : FALSE);
  8756. }
  8757. if (force || descriptor.WritableSpecified() || isWritable)
  8758. {
  8759. object->SetWritable(propId, isWritable);
  8760. }
  8761. }
  8762. void JavascriptOperators::OP_ClearAttributes(Var instance, PropertyId propertyId)
  8763. {
  8764. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_ClearAttributes);
  8765. Assert(instance);
  8766. if (VarIs<RecyclableObject>(instance))
  8767. {
  8768. RecyclableObject* obj = VarTo<RecyclableObject>(instance);
  8769. obj->SetAttributes(propertyId, PropertyNone);
  8770. }
  8771. JIT_HELPER_END(OP_ClearAttributes);
  8772. }
  8773. BOOL JavascriptOperators::Reject(bool throwOnError, ScriptContext* scriptContext, int32 errorCode, PropertyId propertyId)
  8774. {
  8775. Assert(scriptContext);
  8776. if (throwOnError)
  8777. {
  8778. JavascriptError::ThrowTypeError(scriptContext, errorCode, scriptContext->GetThreadContext()->GetPropertyName(propertyId)->GetBuffer());
  8779. }
  8780. return FALSE;
  8781. }
  8782. bool JavascriptOperators::AreSamePropertyDescriptors(const PropertyDescriptor* x, const PropertyDescriptor* y, ScriptContext* scriptContext)
  8783. {
  8784. Assert(scriptContext);
  8785. if (x->ConfigurableSpecified() != y->ConfigurableSpecified() || x->IsConfigurable() != y->IsConfigurable() ||
  8786. x->EnumerableSpecified() != y->EnumerableSpecified() || x->IsEnumerable() != y->IsEnumerable())
  8787. {
  8788. return false;
  8789. }
  8790. if (x->IsDataDescriptor())
  8791. {
  8792. if (!y->IsDataDescriptor() || x->WritableSpecified() != y->WritableSpecified() || x->IsWritable() != y->IsWritable())
  8793. {
  8794. return false;
  8795. }
  8796. if (x->ValueSpecified())
  8797. {
  8798. if (!y->ValueSpecified() || !JavascriptConversion::SameValue(x->GetValue(), y->GetValue()))
  8799. {
  8800. return false;
  8801. }
  8802. }
  8803. }
  8804. else if (x->IsAccessorDescriptor())
  8805. {
  8806. if (!y->IsAccessorDescriptor())
  8807. {
  8808. return false;
  8809. }
  8810. if (x->GetterSpecified())
  8811. {
  8812. if (!y->GetterSpecified() || !JavascriptConversion::SameValue(
  8813. JavascriptOperators::CanonicalizeAccessor(x->GetGetter(), scriptContext),
  8814. JavascriptOperators::CanonicalizeAccessor(y->GetGetter(), scriptContext)))
  8815. {
  8816. return false;
  8817. }
  8818. }
  8819. if (x->SetterSpecified())
  8820. {
  8821. if (!y->SetterSpecified() || !JavascriptConversion::SameValue(
  8822. JavascriptOperators::CanonicalizeAccessor(x->GetSetter(), scriptContext),
  8823. JavascriptOperators::CanonicalizeAccessor(y->GetSetter(), scriptContext)))
  8824. {
  8825. return false;
  8826. }
  8827. }
  8828. }
  8829. return true;
  8830. }
  8831. // Check if an accessor is undefined (null or defaultAccessor)
  8832. bool JavascriptOperators::IsUndefinedAccessor(Var accessor, ScriptContext* scriptContext)
  8833. {
  8834. return nullptr == accessor || scriptContext->GetLibrary()->GetDefaultAccessorFunction() == accessor;
  8835. }
  8836. // Converts default accessor to undefined.
  8837. // Can be used when comparing accessors.
  8838. Var JavascriptOperators::CanonicalizeAccessor(Var accessor, ScriptContext* scriptContext)
  8839. {
  8840. Assert(scriptContext);
  8841. if (IsUndefinedAccessor(accessor, scriptContext))
  8842. {
  8843. return scriptContext->GetLibrary()->GetUndefined();
  8844. }
  8845. return accessor;
  8846. }
  8847. Var JavascriptOperators::DefaultAccessor(RecyclableObject* function, CallInfo callInfo, ...)
  8848. {
  8849. return function->GetLibrary()->GetUndefined();
  8850. }
  8851. void FrameDisplay::SetItem(uint index, void* item)
  8852. {
  8853. AssertMsg(index < this->length, "Invalid frame display access");
  8854. scopes[index] = item;
  8855. }
  8856. void *FrameDisplay::GetItem(uint index)
  8857. {
  8858. AssertMsg(index < this->length, "Invalid frame display access");
  8859. return scopes[index];
  8860. }
  8861. // Grab the "this" pointer, mapping a root object to its associated host object.
  8862. Var JavascriptOperators::RootToThisObject(const Var object, ScriptContext* scriptContext)
  8863. {
  8864. Js::Var thisVar = object;
  8865. TypeId typeId = Js::JavascriptOperators::GetTypeId(thisVar);
  8866. switch (typeId)
  8867. {
  8868. case Js::TypeIds_GlobalObject:
  8869. return ((Js::GlobalObject*)thisVar)->ToThis();
  8870. case Js::TypeIds_ModuleRoot:
  8871. return Js::JavascriptOperators::GetThisFromModuleRoot(thisVar);
  8872. default:
  8873. if (typeId == scriptContext->GetDirectHostTypeId())
  8874. {
  8875. return ((RecyclableObject*)thisVar)->GetLibrary()->GetGlobalObject()->ToThis();
  8876. }
  8877. }
  8878. return thisVar;
  8879. }
  8880. Var JavascriptOperators::CallGetter(RecyclableObject * const function, Var const object, ScriptContext * requestContext)
  8881. {
  8882. #if ENABLE_TTD
  8883. if(function->GetScriptContext()->ShouldSuppressGetterInvocationForDebuggerEvaluation())
  8884. {
  8885. return requestContext->GetLibrary()->GetUndefined();
  8886. }
  8887. #endif
  8888. ScriptContext * scriptContext = function->GetScriptContext();
  8889. ThreadContext * threadContext = scriptContext->GetThreadContext();
  8890. return threadContext->ExecuteImplicitCall(function, ImplicitCall_Accessor, [=]() -> Js::Var
  8891. {
  8892. // Stack object should have a pre-op bail on implicit call. We shouldn't see them here.
  8893. // Stack numbers are ok, as we will call ToObject to wrap it in a number object anyway
  8894. // See JavascriptOperators::GetThisHelper
  8895. Assert(JavascriptOperators::GetTypeId(object) == TypeIds_Integer ||
  8896. JavascriptOperators::GetTypeId(object) == TypeIds_Number ||
  8897. threadContext->HasNoSideEffect(function) ||
  8898. !ThreadContext::IsOnStack(object));
  8899. // Verify that the scriptcontext is alive before firing getter/setter
  8900. if (!scriptContext->VerifyAlive(!function->IsExternal(), requestContext))
  8901. {
  8902. return nullptr;
  8903. }
  8904. CallFlags flags = CallFlags_Value;
  8905. Var thisVar = RootToThisObject(object, scriptContext);
  8906. RecyclableObject* marshalledFunction = UnsafeVarTo<RecyclableObject>(
  8907. CrossSite::MarshalVar(requestContext, function, scriptContext));
  8908. Var result = CALL_ENTRYPOINT(threadContext, marshalledFunction->GetEntryPoint(), function, CallInfo(flags, 1), thisVar);
  8909. result = CrossSite::MarshalVar(requestContext, result);
  8910. // Set implicit call flags so we bail out if we're trying to propagate the value forward, e.g., from a compare. Subsequent calls
  8911. // to the getter may produce different results.
  8912. threadContext->AddImplicitCallFlags(ImplicitCall_Accessor);
  8913. return result;
  8914. });
  8915. }
  8916. void JavascriptOperators::CallSetter(RecyclableObject * const function, Var const object, Var const value, ScriptContext * requestContext)
  8917. {
  8918. ScriptContext * scriptContext = function->GetScriptContext();
  8919. ThreadContext * threadContext = scriptContext->GetThreadContext();
  8920. threadContext->ExecuteImplicitCall(function, ImplicitCall_Accessor, [=]() -> Js::Var
  8921. {
  8922. // Stack object should have a pre-op bail on implicit call. We shouldn't see them here.
  8923. // Stack numbers are ok, as we will call ToObject to wrap it in a number object anyway
  8924. // See JavascriptOperators::GetThisHelper
  8925. Assert(JavascriptOperators::GetTypeId(object) == TypeIds_Integer ||
  8926. JavascriptOperators::GetTypeId(object) == TypeIds_Number || !ThreadContext::IsOnStack(object));
  8927. // Verify that the scriptcontext is alive before firing getter/setter
  8928. if (!scriptContext->VerifyAlive(!function->IsExternal(), requestContext))
  8929. {
  8930. return nullptr;
  8931. }
  8932. CallFlags flags = CallFlags_Value;
  8933. Var putValue = value;
  8934. // CONSIDER: Have requestContext everywhere, even in the setProperty related codepath.
  8935. if (requestContext)
  8936. {
  8937. putValue = CrossSite::MarshalVar(requestContext, value);
  8938. }
  8939. Var thisVar = RootToThisObject(object, scriptContext);
  8940. RecyclableObject* marshalledFunction = function;
  8941. if (requestContext)
  8942. {
  8943. marshalledFunction = UnsafeVarTo<RecyclableObject>(CrossSite::MarshalVar(requestContext, function, function->GetScriptContext()));
  8944. }
  8945. Var result = CALL_ENTRYPOINT(threadContext, marshalledFunction->GetEntryPoint(), function, CallInfo(flags, 2), thisVar, putValue);
  8946. Assert(result);
  8947. // 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
  8948. // to produce the stored value on a LdFld.
  8949. threadContext->AddImplicitCallFlags(ImplicitCall_Accessor);
  8950. return nullptr;
  8951. });
  8952. }
  8953. void * JavascriptOperators::AllocMemForVarArray(size_t size, Recycler* recycler)
  8954. {
  8955. TRACK_ALLOC_INFO(recycler, Js::Var, Recycler, 0, (size_t)(size / sizeof(Js::Var)));
  8956. return recycler->AllocZero(size);
  8957. }
  8958. #if !FLOATVAR
  8959. void * JavascriptOperators::AllocUninitializedNumber(Js::RecyclerJavascriptNumberAllocator * allocator)
  8960. {
  8961. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(AllocUninitializedNumber);
  8962. TRACK_ALLOC_INFO(allocator->GetRecycler(), Js::JavascriptNumber, Recycler, 0, (size_t)-1);
  8963. return allocator->Alloc(sizeof(Js::JavascriptNumber));
  8964. JIT_HELPER_END(AllocUninitializedNumber);
  8965. }
  8966. #endif
  8967. void JavascriptOperators::ScriptAbort()
  8968. {
  8969. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScriptAbort);
  8970. throw ScriptAbortException();
  8971. JIT_HELPER_END(ScriptAbort);
  8972. }
  8973. JavascriptString * JavascriptOperators::Concat3(Var aLeft, Var aCenter, Var aRight, ScriptContext * scriptContext)
  8974. {
  8975. // Make sure we do the conversion in order from left to right
  8976. JavascriptString * strLeft = JavascriptConversion::ToPrimitiveString(aLeft, scriptContext);
  8977. JavascriptString * strCenter = JavascriptConversion::ToPrimitiveString(aCenter, scriptContext);
  8978. JavascriptString * strRight = JavascriptConversion::ToPrimitiveString(aRight, scriptContext);
  8979. return JavascriptString::Concat3(strLeft, strCenter, strRight);
  8980. }
  8981. JavascriptString *
  8982. JavascriptOperators::NewConcatStrMulti(Var a1, Var a2, uint count, ScriptContext * scriptContext)
  8983. {
  8984. // Make sure we do the conversion in order
  8985. JavascriptString * str1 = JavascriptConversion::ToPrimitiveString(a1, scriptContext);
  8986. JavascriptString * str2 = JavascriptConversion::ToPrimitiveString(a2, scriptContext);
  8987. return ConcatStringMulti::New(count, str1, str2, scriptContext);
  8988. }
  8989. void
  8990. JavascriptOperators::SetConcatStrMultiItem(Var concatStr, Var str, uint index, ScriptContext * scriptContext)
  8991. {
  8992. VarTo<ConcatStringMulti>(concatStr)->SetItem(index,
  8993. JavascriptConversion::ToPrimitiveString(str, scriptContext));
  8994. }
  8995. void
  8996. JavascriptOperators::SetConcatStrMultiItem2(Var concatStr, Var str1, Var str2, uint index, ScriptContext * scriptContext)
  8997. {
  8998. ConcatStringMulti * cs = VarTo<ConcatStringMulti>(concatStr);
  8999. cs->SetItem(index, JavascriptConversion::ToPrimitiveString(str1, scriptContext));
  9000. cs->SetItem(index + 1, JavascriptConversion::ToPrimitiveString(str2, scriptContext));
  9001. }
  9002. void JavascriptOperators::OP_SetComputedNameVar(Var method, Var computedNameVar)
  9003. {
  9004. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(SetComputedNameVar);
  9005. ScriptFunctionBase *scriptFunction = VarTo<ScriptFunctionBase>(method);
  9006. scriptFunction->SetComputedNameVar(computedNameVar);
  9007. JIT_HELPER_END(SetComputedNameVar);
  9008. }
  9009. void JavascriptOperators::OP_SetHomeObj(Var method, Var homeObj)
  9010. {
  9011. ScriptFunctionBase *scriptFunction = VarTo<ScriptFunctionBase>(method);
  9012. JIT_HELPER_NOT_REENTRANT_HEADER(SetHomeObj, reentrancylock, scriptFunction->GetScriptContext()->GetThreadContext());
  9013. scriptFunction->SetHomeObj(homeObj);
  9014. JIT_HELPER_END(SetHomeObj);
  9015. }
  9016. Var JavascriptOperators::OP_LdHomeObj(Var scriptFunction, ScriptContext * scriptContext)
  9017. {
  9018. JIT_HELPER_NOT_REENTRANT_HEADER(LdHomeObj, reentrancylock, scriptContext->GetThreadContext());
  9019. // Ensure this is not a stack ScriptFunction
  9020. if (!VarIs<ScriptFunction>(scriptFunction) || ThreadContext::IsOnStack(scriptFunction))
  9021. {
  9022. return scriptContext->GetLibrary()->GetUndefined();
  9023. }
  9024. ScriptFunction *instance = UnsafeVarTo<ScriptFunction>(scriptFunction);
  9025. // We keep a reference to the current class rather than its super prototype
  9026. // since the prototype could change.
  9027. Var homeObj = instance->GetHomeObj();
  9028. return (homeObj != nullptr) ? homeObj : scriptContext->GetLibrary()->GetUndefined();
  9029. JIT_HELPER_END(LdHomeObj);
  9030. }
  9031. Var JavascriptOperators::OP_LdHomeObjProto(Var homeObj, ScriptContext* scriptContext)
  9032. {
  9033. JIT_HELPER_NOT_REENTRANT_HEADER(LdHomeObjProto, reentrancylock, scriptContext->GetThreadContext());
  9034. if (homeObj == nullptr || !VarIs<RecyclableObject>(homeObj))
  9035. {
  9036. return scriptContext->GetLibrary()->GetUndefined();
  9037. }
  9038. RecyclableObject *thisObjPrototype = VarTo<RecyclableObject>(homeObj);
  9039. TypeId typeId = thisObjPrototype->GetTypeId();
  9040. if (typeId == TypeIds_Null || typeId == TypeIds_Undefined)
  9041. {
  9042. JavascriptError::ThrowReferenceError(scriptContext, JSERR_BadSuperReference);
  9043. }
  9044. Assert(thisObjPrototype != nullptr);
  9045. RecyclableObject *superBase = thisObjPrototype->GetPrototype();
  9046. if (superBase == nullptr || !VarIsCorrectType(superBase))
  9047. {
  9048. return scriptContext->GetLibrary()->GetUndefined();
  9049. }
  9050. return superBase;
  9051. JIT_HELPER_END(LdHomeObjProto);
  9052. }
  9053. Var JavascriptOperators::OP_LdFuncObj(Var scriptFunction, ScriptContext * scriptContext)
  9054. {
  9055. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(LdFuncObj);
  9056. // use self as value of [[FunctionObject]] - this is true only for constructors
  9057. Assert(VarIs<RecyclableObject>(scriptFunction));
  9058. return scriptFunction;
  9059. JIT_HELPER_END(LdFuncObj);
  9060. }
  9061. Var JavascriptOperators::OP_LdFuncObjProto(Var funcObj, ScriptContext* scriptContext)
  9062. {
  9063. JIT_HELPER_NOT_REENTRANT_HEADER(LdFuncObjProto, reentrancylock, scriptContext->GetThreadContext());
  9064. RecyclableObject *superCtor = VarTo<RecyclableObject>(funcObj)->GetPrototype();
  9065. if (superCtor == nullptr || !IsConstructor(superCtor))
  9066. {
  9067. JavascriptError::ThrowTypeError(scriptContext, JSERR_NotAConstructor);
  9068. }
  9069. return superCtor;
  9070. JIT_HELPER_END(LdFuncObjProto);
  9071. }
  9072. Var JavascriptOperators::OP_ImportCall(__in JavascriptFunction *function, __in Var specifier, __in ScriptContext* scriptContext)
  9073. {
  9074. JIT_HELPER_REENTRANT_HEADER(ImportCall);
  9075. ModuleRecordBase *moduleRecordBase = nullptr;
  9076. SourceTextModuleRecord *moduleRecord = nullptr;
  9077. FunctionBody* parentFuncBody = function->GetFunctionBody();
  9078. JavascriptString *specifierString = nullptr;
  9079. try
  9080. {
  9081. specifierString = JavascriptConversion::ToString(specifier, scriptContext);
  9082. }
  9083. catch (const JavascriptException &err)
  9084. {
  9085. Var errorObject = err.GetAndClear()->GetThrownObject(scriptContext);
  9086. AssertMsg(errorObject != nullptr, "OP_ImportCall: null error object thrown by ToString(specifier)");
  9087. if (errorObject != nullptr)
  9088. {
  9089. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, errorObject, scriptContext);
  9090. }
  9091. Throw::InternalError();
  9092. }
  9093. DWORD_PTR dwReferencingSourceContext = parentFuncBody->GetHostSourceContext();
  9094. if (!parentFuncBody->IsES6ModuleCode() && dwReferencingSourceContext == Js::Constants::NoHostSourceContext)
  9095. {
  9096. // import() called from eval
  9097. if (parentFuncBody->GetUtf8SourceInfo()->GetCallerUtf8SourceInfo() == nullptr)
  9098. {
  9099. JavascriptError *error = scriptContext->GetLibrary()->CreateError();
  9100. JavascriptError::SetErrorMessageProperties(error, E_FAIL, _u("Unable to locate active script or module that calls import()"), scriptContext);
  9101. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, error, scriptContext);
  9102. }
  9103. dwReferencingSourceContext = parentFuncBody->GetUtf8SourceInfo()->GetCallerUtf8SourceInfo()->GetSourceContextInfo()->dwHostSourceContext;
  9104. if (dwReferencingSourceContext == Js::Constants::NoHostSourceContext)
  9105. {
  9106. // Walk the call stack if caller function is neither module code nor having host source context
  9107. JavascriptFunction* caller = nullptr;
  9108. Js::JavascriptStackWalker walker(scriptContext);
  9109. walker.GetCaller(&caller);
  9110. do
  9111. {
  9112. if (walker.GetCaller(&caller) && caller != nullptr && caller->IsScriptFunction())
  9113. {
  9114. parentFuncBody = caller->GetFunctionBody();
  9115. dwReferencingSourceContext = parentFuncBody->GetHostSourceContext();
  9116. }
  9117. else
  9118. {
  9119. JavascriptError *error = scriptContext->GetLibrary()->CreateError();
  9120. JavascriptError::SetErrorMessageProperties(error, E_FAIL, _u("Unable to locate active script or module that calls import()"), scriptContext);
  9121. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, error, scriptContext);
  9122. }
  9123. } while (!parentFuncBody->IsES6ModuleCode() && dwReferencingSourceContext == Js::Constants::NoHostSourceContext);
  9124. }
  9125. }
  9126. LPCOLESTR moduleName = specifierString->GetSz();
  9127. HRESULT hr = 0;
  9128. if (parentFuncBody->IsES6ModuleCode())
  9129. {
  9130. SourceTextModuleRecord *referenceModuleRecord = parentFuncBody->GetScriptContext()->GetLibrary()->GetModuleRecord(parentFuncBody->GetModuleID());
  9131. BEGIN_LEAVE_SCRIPT(scriptContext);
  9132. BEGIN_TRANSLATE_TO_HRESULT(static_cast<ExceptionType>(ExceptionType_OutOfMemory | ExceptionType_StackOverflow));
  9133. hr = scriptContext->GetHostScriptContext()->FetchImportedModule(referenceModuleRecord, moduleName, &moduleRecordBase);
  9134. END_TRANSLATE_EXCEPTION_TO_HRESULT(hr);
  9135. END_LEAVE_SCRIPT(scriptContext);
  9136. }
  9137. else
  9138. {
  9139. Assert(dwReferencingSourceContext != Js::Constants::NoHostSourceContext);
  9140. BEGIN_LEAVE_SCRIPT(scriptContext);
  9141. BEGIN_TRANSLATE_TO_HRESULT(static_cast<ExceptionType>(ExceptionType_OutOfMemory | ExceptionType_StackOverflow));
  9142. hr = scriptContext->GetHostScriptContext()->FetchImportedModuleFromScript(dwReferencingSourceContext, moduleName, &moduleRecordBase);
  9143. END_TRANSLATE_EXCEPTION_TO_HRESULT(hr);
  9144. END_LEAVE_SCRIPT(scriptContext);
  9145. }
  9146. if (FAILED(hr))
  9147. {
  9148. // We cannot just use the buffer in the specifier string - need to make a copy here.
  9149. size_t length = wcslen(moduleName);
  9150. char16* allocatedString = RecyclerNewArrayLeaf(scriptContext->GetRecycler(), char16, length + 1);
  9151. wmemcpy_s(allocatedString, length + 1, moduleName, length);
  9152. allocatedString[length] = _u('\0');
  9153. Js::JavascriptError *error = scriptContext->GetLibrary()->CreateURIError();
  9154. JavascriptError::SetErrorMessageProperties(error, hr, allocatedString, scriptContext);
  9155. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, error, scriptContext);
  9156. }
  9157. moduleRecord = SourceTextModuleRecord::FromHost(moduleRecordBase);
  9158. if (moduleRecord->GetErrorObject() != nullptr)
  9159. {
  9160. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, moduleRecord->GetErrorObject(), scriptContext, moduleRecord);
  9161. }
  9162. else if (moduleRecord->WasEvaluated())
  9163. {
  9164. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(true, moduleRecord->GetNamespace(), scriptContext, moduleRecord);
  9165. }
  9166. return moduleRecord->PostProcessDynamicModuleImport();
  9167. JIT_HELPER_END(ImportCall);
  9168. }
  9169. Var JavascriptOperators::OP_NewAwaitObject(ScriptContext* scriptContext)
  9170. {
  9171. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(NewAwaitObject);
  9172. auto* awaitObject = DynamicObject::New(
  9173. scriptContext->GetRecycler(),
  9174. scriptContext->GetLibrary()->GetAwaitObjectType());
  9175. return awaitObject;
  9176. JIT_HELPER_END(NewAwaitObject);
  9177. }
  9178. Var JavascriptOperators::OP_NewAsyncFromSyncIterator(Var syncIterator, ScriptContext* scriptContext)
  9179. {
  9180. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(NewAsyncFromSyncIterator);
  9181. return scriptContext->GetLibrary()->CreateAsyncFromSyncIterator(VarTo<RecyclableObject>(syncIterator));
  9182. JIT_HELPER_END(NewAsyncFromSyncIterator);
  9183. }
  9184. Js::Var
  9185. JavascriptOperators::BoxStackInstance(Js::Var instance, ScriptContext * scriptContext, bool allowStackFunction, bool deepCopy)
  9186. {
  9187. if (!ThreadContext::IsOnStack(instance) || (allowStackFunction && !TaggedNumber::Is(instance) && (*(int*)instance & 1)))
  9188. {
  9189. return instance;
  9190. }
  9191. TypeId typeId = JavascriptOperators::GetTypeId(instance);
  9192. switch (typeId)
  9193. {
  9194. case Js::TypeIds_Number:
  9195. #if !FLOATVAR
  9196. return JavascriptNumber::BoxStackInstance(instance, scriptContext);
  9197. #endif
  9198. // fall-through
  9199. case Js::TypeIds_Integer:
  9200. return instance;
  9201. case Js::TypeIds_RegEx:
  9202. return JavascriptRegExp::BoxStackInstance(VarTo<JavascriptRegExp>(instance), deepCopy);
  9203. case Js::TypeIds_Object:
  9204. return DynamicObject::BoxStackInstance(VarTo<DynamicObject>(instance), deepCopy);
  9205. case Js::TypeIds_Array:
  9206. return JavascriptArray::BoxStackInstance(UnsafeVarTo<JavascriptArray>(instance), deepCopy);
  9207. case Js::TypeIds_NativeIntArray:
  9208. return JavascriptNativeIntArray::BoxStackInstance(UnsafeVarTo<JavascriptNativeIntArray>(instance), deepCopy);
  9209. case Js::TypeIds_NativeFloatArray:
  9210. return JavascriptNativeFloatArray::BoxStackInstance(UnsafeVarTo<JavascriptNativeFloatArray>(instance), deepCopy);
  9211. case Js::TypeIds_Function:
  9212. Assert(allowStackFunction);
  9213. // Stack functions are deal with not mar mark them, but by nested function escape analysis
  9214. // in the front end. No need to box here.
  9215. return instance;
  9216. #if ENABLE_COPYONACCESS_ARRAY
  9217. case Js::TypeIds_CopyOnAccessNativeIntArray:
  9218. Assert(false);
  9219. // fall-through
  9220. #endif
  9221. default:
  9222. Assert(false);
  9223. return instance;
  9224. };
  9225. }
  9226. ImplicitCallFlags
  9227. JavascriptOperators::CacheAndClearImplicitBit(ScriptContext* scriptContext)
  9228. {
  9229. ImplicitCallFlags prevImplicitCallFlags = scriptContext->GetThreadContext()->GetImplicitCallFlags();
  9230. scriptContext->GetThreadContext()->ClearImplicitCallFlags();
  9231. return prevImplicitCallFlags;
  9232. }
  9233. ImplicitCallFlags
  9234. JavascriptOperators::CheckAndUpdateFunctionBodyWithImplicitFlag(FunctionBody* functionBody)
  9235. {
  9236. ScriptContext* scriptContext = functionBody->GetScriptContext();
  9237. ImplicitCallFlags currImplicitCallFlags = scriptContext->GetThreadContext()->GetImplicitCallFlags();
  9238. if ((currImplicitCallFlags > ImplicitCall_None))
  9239. {
  9240. functionBody->SetHasOnlyThisStmts(false);
  9241. }
  9242. return currImplicitCallFlags;
  9243. }
  9244. void
  9245. JavascriptOperators::RestoreImplicitFlag(ScriptContext* scriptContext, ImplicitCallFlags prevImplicitCallFlags, ImplicitCallFlags currImplicitCallFlags)
  9246. {
  9247. scriptContext->GetThreadContext()->SetImplicitCallFlags((ImplicitCallFlags)(prevImplicitCallFlags | currImplicitCallFlags));
  9248. }
  9249. FunctionProxy*
  9250. JavascriptOperators::GetDeferredDeserializedFunctionProxy(JavascriptFunction* func)
  9251. {
  9252. FunctionProxy* proxy = func->GetFunctionProxy();
  9253. Assert(proxy->GetFunctionInfo()->GetFunctionProxy() != proxy);
  9254. return proxy;
  9255. }
  9256. template <>
  9257. Js::Var JavascriptOperators::GetElementAtIndex(Js::JavascriptArray* arrayObject, UINT index, Js::ScriptContext* scriptContext)
  9258. {
  9259. Js::Var result;
  9260. if (Js::JavascriptOperators::OP_GetElementI_ArrayFastPath(arrayObject, index, &result, scriptContext))
  9261. {
  9262. return result;
  9263. }
  9264. return scriptContext->GetMissingItemResult();
  9265. }
  9266. template<>
  9267. Js::Var JavascriptOperators::GetElementAtIndex(Js::JavascriptNativeIntArray* arrayObject, UINT index, Js::ScriptContext* scriptContext)
  9268. {
  9269. Js::Var result;
  9270. if (Js::JavascriptOperators::OP_GetElementI_ArrayFastPath(arrayObject, index, &result, scriptContext))
  9271. {
  9272. return result;
  9273. }
  9274. return scriptContext->GetMissingItemResult();
  9275. }
  9276. template<>
  9277. Js::Var JavascriptOperators::GetElementAtIndex(Js::JavascriptNativeFloatArray* arrayObject, UINT index, Js::ScriptContext* scriptContext)
  9278. {
  9279. Js::Var result;
  9280. if (Js::JavascriptOperators::OP_GetElementI_ArrayFastPath(arrayObject, index, &result, scriptContext))
  9281. {
  9282. return result;
  9283. }
  9284. return scriptContext->GetMissingItemResult();
  9285. }
  9286. template<>
  9287. Js::Var JavascriptOperators::GetElementAtIndex(Js::Var* arrayObject, UINT index, Js::ScriptContext* scriptContext)
  9288. {
  9289. return Js::JavascriptOperators::OP_GetElementI_Int32(*arrayObject, index, scriptContext);
  9290. }
  9291. template<typename T>
  9292. void JavascriptOperators::ObjectToNativeArray(T* arrayObject,
  9293. JsNativeValueType valueType,
  9294. __in UINT length,
  9295. __in UINT elementSize,
  9296. __out_bcount(length*elementSize) byte* buffer,
  9297. Js::ScriptContext* scriptContext)
  9298. {
  9299. Var element;
  9300. uint64 allocSize = UInt32Math::Mul(length, elementSize);
  9301. // TODO:further fast path the call for things like IntArray convert to int, floatarray convert to float etc.
  9302. // such that we don't need boxing.
  9303. switch (valueType)
  9304. {
  9305. case JsInt8Type:
  9306. AnalysisAssert(elementSize == sizeof(int8));
  9307. for (UINT i = 0; i < length; i++)
  9308. {
  9309. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9310. AnalysisAssert((i + 1) * sizeof(int8) <= allocSize);
  9311. #pragma prefast(suppress:22102)
  9312. ((int8*)buffer)[i] = Js::JavascriptConversion::ToInt8(element, scriptContext);
  9313. }
  9314. break;
  9315. case JsUint8Type:
  9316. AnalysisAssert(elementSize == sizeof(uint8));
  9317. for (UINT i = 0; i < length; i++)
  9318. {
  9319. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9320. AnalysisAssert((i + 1) * sizeof(uint8) <= allocSize);
  9321. ((uint8*)buffer)[i] = Js::JavascriptConversion::ToUInt8(element, scriptContext);
  9322. }
  9323. break;
  9324. case JsInt16Type:
  9325. AnalysisAssert(elementSize == sizeof(int16));
  9326. for (UINT i = 0; i < length; i++)
  9327. {
  9328. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9329. AnalysisAssert((i + 1) * sizeof(int16) <= allocSize);
  9330. ((int16*)buffer)[i] = Js::JavascriptConversion::ToInt16(element, scriptContext);
  9331. }
  9332. break;
  9333. case JsUint16Type:
  9334. AnalysisAssert(elementSize == sizeof(uint16));
  9335. for (UINT i = 0; i < length; i++)
  9336. {
  9337. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9338. AnalysisAssert((i + 1) * sizeof(uint16) <= allocSize);
  9339. ((uint16*)buffer)[i] = Js::JavascriptConversion::ToUInt16(element, scriptContext);
  9340. }
  9341. break;
  9342. case JsInt32Type:
  9343. AnalysisAssert(elementSize == sizeof(int32));
  9344. for (UINT i = 0; i < length; i++)
  9345. {
  9346. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9347. AnalysisAssert((i + 1) * sizeof(int32) <= allocSize);
  9348. ((int32*)buffer)[i] = Js::JavascriptConversion::ToInt32(element, scriptContext);
  9349. }
  9350. break;
  9351. case JsUint32Type:
  9352. AnalysisAssert(elementSize == sizeof(uint32));
  9353. for (UINT i = 0; i < length; i++)
  9354. {
  9355. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9356. AnalysisAssert((i + 1) * sizeof(uint32) <= allocSize);
  9357. ((uint32*)buffer)[i] = Js::JavascriptConversion::ToUInt32(element, scriptContext);
  9358. }
  9359. break;
  9360. case JsInt64Type:
  9361. AnalysisAssert(elementSize == sizeof(int64));
  9362. for (UINT i = 0; i < length; i++)
  9363. {
  9364. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9365. AnalysisAssert((i + 1) * sizeof(int64) <= allocSize);
  9366. ((int64*)buffer)[i] = Js::JavascriptConversion::ToInt64(element, scriptContext);
  9367. }
  9368. break;
  9369. case JsUint64Type:
  9370. AnalysisAssert(elementSize == sizeof(uint64));
  9371. for (UINT i = 0; i < length; i++)
  9372. {
  9373. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9374. AnalysisAssert((i + 1) * sizeof(uint64) <= allocSize);
  9375. ((uint64*)buffer)[i] = Js::JavascriptConversion::ToUInt64(element, scriptContext);
  9376. }
  9377. break;
  9378. case JsFloatType:
  9379. AnalysisAssert(elementSize == sizeof(float));
  9380. for (UINT i = 0; i < length; i++)
  9381. {
  9382. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9383. AnalysisAssert((i + 1) * sizeof(float) <= allocSize);
  9384. ((float*)buffer)[i] = Js::JavascriptConversion::ToFloat(element, scriptContext);
  9385. }
  9386. break;
  9387. case JsDoubleType:
  9388. AnalysisAssert(elementSize == sizeof(double));
  9389. for (UINT i = 0; i < length; i++)
  9390. {
  9391. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9392. AnalysisAssert((i + 1) * sizeof(double) <= allocSize);
  9393. ((double*)buffer)[i] = Js::JavascriptConversion::ToNumber(element, scriptContext);
  9394. }
  9395. break;
  9396. case JsNativeStringType:
  9397. AnalysisAssert(elementSize == sizeof(JsNativeString));
  9398. for (UINT i = 0; i < length; i++)
  9399. {
  9400. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9401. AnalysisAssert((i + 1) * sizeof(JsNativeString) <= allocSize);
  9402. Js::JavascriptString* string = Js::JavascriptConversion::ToString(element, scriptContext);
  9403. (((JsNativeString*)buffer)[i]).str = string->GetSz();
  9404. (((JsNativeString*)buffer)[i]).length = string->GetLength();
  9405. }
  9406. break;
  9407. default:
  9408. Assert(FALSE);
  9409. }
  9410. }
  9411. void JavascriptOperators::VarToNativeArray(Var arrayObject,
  9412. JsNativeValueType valueType,
  9413. __in UINT length,
  9414. __in UINT elementSize,
  9415. __out_bcount(length*elementSize) byte* buffer,
  9416. Js::ScriptContext* scriptContext)
  9417. {
  9418. Js::DynamicObject* dynamicObject = VarTo<DynamicObject>(arrayObject);
  9419. if (dynamicObject->IsCrossSiteObject() || Js::TaggedInt::IsOverflow(length))
  9420. {
  9421. Js::JavascriptOperators::ObjectToNativeArray(&arrayObject, valueType, length, elementSize, buffer, scriptContext);
  9422. }
  9423. else
  9424. {
  9425. #if ENABLE_COPYONACCESS_ARRAY
  9426. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(arrayObject);
  9427. #endif
  9428. switch (Js::JavascriptOperators::GetTypeId(arrayObject))
  9429. {
  9430. case TypeIds_Array:
  9431. Js::JavascriptOperators::ObjectToNativeArray(Js::UnsafeVarTo<Js::JavascriptArray>(arrayObject), valueType, length, elementSize, buffer, scriptContext);
  9432. break;
  9433. case TypeIds_NativeFloatArray:
  9434. Js::JavascriptOperators::ObjectToNativeArray(Js::UnsafeVarTo<Js::JavascriptNativeFloatArray>(arrayObject), valueType, length, elementSize, buffer, scriptContext);
  9435. break;
  9436. case TypeIds_NativeIntArray:
  9437. Js::JavascriptOperators::ObjectToNativeArray(Js::UnsafeVarTo<Js::JavascriptNativeIntArray>(arrayObject), valueType, length, elementSize, buffer, scriptContext);
  9438. break;
  9439. // We can have more specialized template if needed.
  9440. default:
  9441. Js::JavascriptOperators::ObjectToNativeArray(&arrayObject, valueType, length, elementSize, buffer, scriptContext);
  9442. }
  9443. }
  9444. }
  9445. // SpeciesConstructor abstract operation as described in ES6.0 Section 7.3.20
  9446. RecyclableObject* JavascriptOperators::SpeciesConstructor(_In_ RecyclableObject* object, _In_ JavascriptFunction* defaultConstructor, _In_ ScriptContext* scriptContext)
  9447. {
  9448. //1.Assert: Type(O) is Object.
  9449. Assert(JavascriptOperators::IsObject(object));
  9450. //2.Let C be Get(O, "constructor").
  9451. //3.ReturnIfAbrupt(C).
  9452. Var constructor = JavascriptOperators::GetProperty(object, PropertyIds::constructor, scriptContext);
  9453. //4.If C is undefined, return defaultConstructor.
  9454. if (JavascriptOperators::IsUndefinedObject(constructor))
  9455. {
  9456. return defaultConstructor;
  9457. }
  9458. //5.If Type(C) is not Object, throw a TypeError exception.
  9459. if (!JavascriptOperators::IsObject(constructor))
  9460. {
  9461. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject, _u("[constructor]"));
  9462. }
  9463. //6.Let S be Get(C, @@species).
  9464. //7.ReturnIfAbrupt(S).
  9465. Var species = nullptr;
  9466. if (!JavascriptOperators::GetProperty(VarTo<RecyclableObject>(constructor),
  9467. PropertyIds::_symbolSpecies, &species, scriptContext)
  9468. || JavascriptOperators::IsUndefinedOrNull(species))
  9469. {
  9470. //8.If S is either undefined or null, return defaultConstructor.
  9471. return defaultConstructor;
  9472. }
  9473. constructor = species;
  9474. //9.If IsConstructor(S) is true, return S.
  9475. RecyclableObject* constructorObj = JavascriptOperators::TryFromVar<RecyclableObject>(constructor);
  9476. if (constructorObj && JavascriptOperators::IsConstructor(constructorObj))
  9477. {
  9478. return constructorObj;
  9479. }
  9480. //10.Throw a TypeError exception.
  9481. JavascriptError::ThrowTypeError(scriptContext, JSERR_NotAConstructor, _u("constructor[Symbol.species]"));
  9482. }
  9483. BOOL JavascriptOperators::GreaterEqual(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9484. {
  9485. JIT_HELPER_REENTRANT_HEADER(Op_GreaterEqual);
  9486. if (TaggedInt::Is(aLeft))
  9487. {
  9488. if (TaggedInt::Is(aRight))
  9489. {
  9490. // Works whether it is TaggedInt31 or TaggedInt32
  9491. return ::Math::PointerCastToIntegralTruncate<int>(aLeft) >= ::Math::PointerCastToIntegralTruncate<int>(aRight);
  9492. }
  9493. if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9494. {
  9495. return TaggedInt::ToDouble(aLeft) >= JavascriptNumber::GetValue(aRight);
  9496. }
  9497. }
  9498. else if (TaggedInt::Is(aRight))
  9499. {
  9500. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  9501. {
  9502. return JavascriptNumber::GetValue(aLeft) >= TaggedInt::ToDouble(aRight);
  9503. }
  9504. }
  9505. else
  9506. {
  9507. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft) && JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9508. {
  9509. return JavascriptNumber::GetValue(aLeft) >= JavascriptNumber::GetValue(aRight);
  9510. }
  9511. }
  9512. return !RelationalComparisonHelper(aLeft, aRight, scriptContext, true, true);
  9513. JIT_HELPER_END(Op_GreaterEqual);
  9514. }
  9515. BOOL JavascriptOperators::LessEqual(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9516. {
  9517. JIT_HELPER_REENTRANT_HEADER(Op_LessEqual);
  9518. if (TaggedInt::Is(aLeft))
  9519. {
  9520. if (TaggedInt::Is(aRight))
  9521. {
  9522. // Works whether it is TaggedInt31 or TaggedInt32
  9523. return ::Math::PointerCastToIntegralTruncate<int>(aLeft) <= ::Math::PointerCastToIntegralTruncate<int>(aRight);
  9524. }
  9525. if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9526. {
  9527. return TaggedInt::ToDouble(aLeft) <= JavascriptNumber::GetValue(aRight);
  9528. }
  9529. }
  9530. else if (TaggedInt::Is(aRight))
  9531. {
  9532. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  9533. {
  9534. return JavascriptNumber::GetValue(aLeft) <= TaggedInt::ToDouble(aRight);
  9535. }
  9536. }
  9537. else
  9538. {
  9539. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft) && JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9540. {
  9541. return JavascriptNumber::GetValue(aLeft) <= JavascriptNumber::GetValue(aRight);
  9542. }
  9543. }
  9544. return !RelationalComparisonHelper(aRight, aLeft, scriptContext, false, true);
  9545. JIT_HELPER_END(Op_LessEqual);
  9546. }
  9547. BOOL JavascriptOperators::NotEqual(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9548. {
  9549. JIT_HELPER_REENTRANT_HEADER(Op_NotEqual);
  9550. JIT_HELPER_SAME_ATTRIBUTES(Op_NotEqual, Op_Equal);
  9551. //
  9552. // TODO: Change to use Abstract Equality Comparison Algorithm (ES3.0: S11.9.3):
  9553. // - Evaluate left, then right, operands to preserve correct evaluation order.
  9554. // - Call algorithm, potentially reversing arguments.
  9555. //
  9556. return !Equal(aLeft, aRight, scriptContext);
  9557. JIT_HELPER_END(Op_NotEqual);
  9558. }
  9559. // NotStrictEqual() returns whether the two vars have strict equality, as
  9560. // described in (ES3.0: S11.9.5, S11.9.6).
  9561. BOOL JavascriptOperators::NotStrictEqual(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9562. {
  9563. JIT_HELPER_REENTRANT_HEADER(Op_NotStrictEqual);
  9564. JIT_HELPER_SAME_ATTRIBUTES(Op_NotStrictEqual, Op_StrictEqual);
  9565. return !StrictEqual(aLeft, aRight, scriptContext);
  9566. JIT_HELPER_END(Op_NotStrictEqual);
  9567. }
  9568. bool JavascriptOperators::CheckIfObjectAndPrototypeChainHasOnlyWritableDataProperties(_In_ RecyclableObject* object)
  9569. {
  9570. return object->GetLibrary()->GetTypesWithOnlyWritablePropertyProtoChainCache()->Check(object);
  9571. }
  9572. bool JavascriptOperators::CheckIfPrototypeChainHasOnlyWritableDataProperties(_In_ RecyclableObject* prototype)
  9573. {
  9574. return prototype->GetLibrary()->GetTypesWithOnlyWritablePropertyProtoChainCache()->CheckProtoChain(prototype);
  9575. }
  9576. bool JavascriptOperators::CheckIfObjectAndProtoChainHasNoSpecialProperties(_In_ RecyclableObject* object)
  9577. {
  9578. return object->GetLibrary()->GetTypesWithNoSpecialPropertyProtoChainCache()->Check(object);
  9579. }
  9580. // Checks to see if the specified object (which should be a prototype object)
  9581. // contains a proxy anywhere in the prototype chain.
  9582. bool JavascriptOperators::CheckIfPrototypeChainContainsProxyObject(RecyclableObject* prototype)
  9583. {
  9584. if (prototype == nullptr)
  9585. {
  9586. return false;
  9587. }
  9588. Assert(JavascriptOperators::IsObjectOrNull(prototype));
  9589. while (prototype->GetTypeId() != TypeIds_Null)
  9590. {
  9591. if (prototype->GetTypeId() == TypeIds_Proxy)
  9592. {
  9593. return true;
  9594. }
  9595. prototype = prototype->GetPrototype();
  9596. }
  9597. return false;
  9598. }
  9599. BOOL JavascriptOperators::Equal(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9600. {
  9601. JIT_HELPER_REENTRANT_HEADER(Op_Equal);
  9602. JIT_HELPER_SAME_ATTRIBUTES(Op_Equal, Op_Equal_Full);
  9603. if (aLeft == aRight)
  9604. {
  9605. if (TaggedInt::Is(aLeft) || DynamicObject::IsBaseDynamicObject(aLeft))
  9606. {
  9607. return true;
  9608. }
  9609. else
  9610. {
  9611. return Equal_Full(aLeft, aRight, scriptContext);
  9612. }
  9613. }
  9614. if (VarIs<JavascriptString>(aLeft) && VarIs<JavascriptString>(aRight))
  9615. {
  9616. JavascriptString* left = (JavascriptString*)aLeft;
  9617. JavascriptString* right = (JavascriptString*)aRight;
  9618. if (left->GetLength() == right->GetLength())
  9619. {
  9620. if (left->UnsafeGetBuffer() != NULL && right->UnsafeGetBuffer() != NULL)
  9621. {
  9622. if (left->GetLength() == 1)
  9623. {
  9624. return left->UnsafeGetBuffer()[0] == right->UnsafeGetBuffer()[0];
  9625. }
  9626. return memcmp(left->UnsafeGetBuffer(), right->UnsafeGetBuffer(), left->GetLength() * sizeof(left->UnsafeGetBuffer()[0])) == 0;
  9627. }
  9628. // fall through to Equal_Full
  9629. }
  9630. else
  9631. {
  9632. return false;
  9633. }
  9634. }
  9635. return Equal_Full(aLeft, aRight, scriptContext);
  9636. JIT_HELPER_END(Op_Equal);
  9637. }
  9638. BOOL JavascriptOperators::Greater(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9639. {
  9640. JIT_HELPER_REENTRANT_HEADER(Op_Greater);
  9641. if (TaggedInt::Is(aLeft))
  9642. {
  9643. if (TaggedInt::Is(aRight))
  9644. {
  9645. // Works whether it is TaggedInt31 or TaggedInt32
  9646. return ::Math::PointerCastToIntegralTruncate<int>(aLeft) > ::Math::PointerCastToIntegralTruncate<int>(aRight);
  9647. }
  9648. if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9649. {
  9650. return TaggedInt::ToDouble(aLeft) > JavascriptNumber::GetValue(aRight);
  9651. }
  9652. }
  9653. else if (TaggedInt::Is(aRight))
  9654. {
  9655. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  9656. {
  9657. return JavascriptNumber::GetValue(aLeft) > TaggedInt::ToDouble(aRight);
  9658. }
  9659. }
  9660. else
  9661. {
  9662. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft) && JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9663. {
  9664. return JavascriptNumber::GetValue(aLeft) > JavascriptNumber::GetValue(aRight);
  9665. }
  9666. }
  9667. return Greater_Full(aLeft, aRight, scriptContext);
  9668. JIT_HELPER_END(Op_Greater);
  9669. }
  9670. BOOL JavascriptOperators::Less(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9671. {
  9672. JIT_HELPER_REENTRANT_HEADER(Op_Less);
  9673. if (TaggedInt::Is(aLeft))
  9674. {
  9675. if (TaggedInt::Is(aRight))
  9676. {
  9677. // Works whether it is TaggedInt31 or TaggedInt32
  9678. return ::Math::PointerCastToIntegralTruncate<int>(aLeft) < ::Math::PointerCastToIntegralTruncate<int>(aRight);
  9679. }
  9680. if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9681. {
  9682. return TaggedInt::ToDouble(aLeft) < JavascriptNumber::GetValue(aRight);
  9683. }
  9684. }
  9685. else if (TaggedInt::Is(aRight))
  9686. {
  9687. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  9688. {
  9689. return JavascriptNumber::GetValue(aLeft) < TaggedInt::ToDouble(aRight);
  9690. }
  9691. }
  9692. else
  9693. {
  9694. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft) && JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9695. {
  9696. return JavascriptNumber::GetValue(aLeft) < JavascriptNumber::GetValue(aRight);
  9697. }
  9698. }
  9699. return Less_Full(aLeft, aRight, scriptContext);
  9700. JIT_HELPER_END(Op_Less);
  9701. }
  9702. RecyclableObject* JavascriptOperators::ToObject(Var aRight, ScriptContext* scriptContext)
  9703. {
  9704. JIT_HELPER_NOT_REENTRANT_HEADER(Op_ConvObject, reentrancylock, scriptContext->GetThreadContext());
  9705. RecyclableObject* object = nullptr;
  9706. if (FALSE == JavascriptConversion::ToObject(aRight, scriptContext, &object))
  9707. {
  9708. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject /* TODO-ERROR: get arg name - aValue */);
  9709. }
  9710. return object;
  9711. JIT_HELPER_END(Op_ConvObject);
  9712. }
  9713. Var JavascriptOperators::ToUnscopablesWrapperObject(Var aRight, ScriptContext* scriptContext)
  9714. {
  9715. JIT_HELPER_NOT_REENTRANT_HEADER(Op_NewUnscopablesWrapperObject, reentrancylock, scriptContext->GetThreadContext());
  9716. RecyclableObject* object = VarTo<RecyclableObject>(aRight);
  9717. UnscopablesWrapperObject* withWrapper = RecyclerNew(scriptContext->GetRecycler(), UnscopablesWrapperObject, object, scriptContext->GetLibrary()->GetWithType());
  9718. return withWrapper;
  9719. JIT_HELPER_END(Op_NewUnscopablesWrapperObject);
  9720. }
  9721. Var JavascriptOperators::ToNumber(Var aRight, ScriptContext* scriptContext)
  9722. {
  9723. JIT_HELPER_REENTRANT_HEADER(Op_ConvNumber_Full);
  9724. if (TaggedInt::Is(aRight) || (JavascriptNumber::Is_NoTaggedIntCheck(aRight)))
  9725. {
  9726. return aRight;
  9727. }
  9728. return JavascriptNumber::ToVarIntCheck(JavascriptConversion::ToNumber_Full(aRight, scriptContext), scriptContext);
  9729. JIT_HELPER_END(Op_ConvNumber_Full);
  9730. }
  9731. Var JavascriptOperators::ToNumeric(Var aRight, ScriptContext* scriptContext)
  9732. {
  9733. if (JavascriptOperators::GetTypeId(aRight) == TypeIds_BigInt)
  9734. {
  9735. return aRight;
  9736. }
  9737. return JavascriptOperators::ToNumber(aRight, scriptContext);
  9738. }
  9739. BOOL JavascriptOperators::IsObject(_In_ RecyclableObject* instance)
  9740. {
  9741. return GetTypeId(instance) > TypeIds_LastJavascriptPrimitiveType;
  9742. }
  9743. BOOL JavascriptOperators::IsObject(_In_ Var instance)
  9744. {
  9745. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_IsObject);
  9746. return GetTypeId(instance) > TypeIds_LastJavascriptPrimitiveType;
  9747. JIT_HELPER_END(Op_IsObject);
  9748. }
  9749. BOOL JavascriptOperators::IsObjectType(TypeId typeId)
  9750. {
  9751. return typeId > TypeIds_LastJavascriptPrimitiveType;
  9752. }
  9753. BOOL JavascriptOperators::IsExposedType(TypeId typeId)
  9754. {
  9755. return typeId <= TypeIds_LastTrueJavascriptObjectType && typeId != TypeIds_HostDispatch;
  9756. }
  9757. BOOL JavascriptOperators::IsObjectOrNull(Var instance)
  9758. {
  9759. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_IsObjectOrNull);
  9760. TypeId typeId = GetTypeId(instance);
  9761. return IsObjectType(typeId) || typeId == TypeIds_Null;
  9762. JIT_HELPER_END(Op_IsObjectOrNull);
  9763. }
  9764. BOOL JavascriptOperators::IsUndefined(_In_ RecyclableObject* instance)
  9765. {
  9766. return JavascriptOperators::GetTypeId(instance) == TypeIds_Undefined;
  9767. }
  9768. BOOL JavascriptOperators::IsUndefined(Var instance)
  9769. {
  9770. return JavascriptOperators::GetTypeId(instance) == TypeIds_Undefined;
  9771. }
  9772. BOOL JavascriptOperators::IsUndefinedOrNullType(TypeId typeId)
  9773. {
  9774. return typeId <= TypeIds_UndefinedOrNull;
  9775. }
  9776. BOOL JavascriptOperators::IsUndefinedOrNull(Var instance)
  9777. {
  9778. return IsUndefinedOrNullType(JavascriptOperators::GetTypeId(instance));
  9779. }
  9780. BOOL JavascriptOperators::IsUndefinedOrNull(RecyclableObject* instance)
  9781. {
  9782. return JavascriptOperators::IsUndefinedOrNullType(instance->GetTypeId());
  9783. }
  9784. BOOL JavascriptOperators::IsUndefinedOrNull(Var instance, ScriptContext* scriptContext)
  9785. {
  9786. JavascriptLibrary* library = scriptContext->GetLibrary();
  9787. return IsUndefinedObject(instance, library) || IsNull(instance, library);
  9788. }
  9789. BOOL JavascriptOperators::IsUndefinedOrNull(Var instance, JavascriptLibrary* library)
  9790. {
  9791. return IsUndefinedObject(instance, library) || IsNull(instance, library);
  9792. }
  9793. BOOL JavascriptOperators::IsNull(Var instance)
  9794. {
  9795. return JavascriptOperators::GetTypeId(instance) == TypeIds_Null;
  9796. }
  9797. BOOL JavascriptOperators::IsNull(Var instance, ScriptContext* scriptContext)
  9798. {
  9799. return JavascriptOperators::IsNull(instance, scriptContext->GetLibrary());
  9800. }
  9801. BOOL JavascriptOperators::IsNull(Var instance, JavascriptLibrary* library)
  9802. {
  9803. Assert(!VarIs<RecyclableObject>(instance) ? TRUE : ((RecyclableObject*)instance)->GetScriptContext()->GetLibrary() == library );
  9804. return library->GetNull() == instance;
  9805. }
  9806. BOOL JavascriptOperators::IsNull(RecyclableObject* instance)
  9807. {
  9808. return instance->GetType()->GetTypeId() == TypeIds_Null;
  9809. }
  9810. BOOL JavascriptOperators::IsSpecialObjectType(TypeId typeId)
  9811. {
  9812. return typeId > TypeIds_LastTrueJavascriptObjectType;
  9813. }
  9814. BOOL JavascriptOperators::IsUndefinedObject(Var instance)
  9815. {
  9816. return JavascriptOperators::GetTypeId(instance) == TypeIds_Undefined;
  9817. }
  9818. BOOL JavascriptOperators::IsUndefinedObject(RecyclableObject* instance)
  9819. {
  9820. return instance->GetType()->GetTypeId() == TypeIds_Undefined;
  9821. }
  9822. BOOL JavascriptOperators::IsUndefinedObject(Var instance, RecyclableObject *libraryUndefined)
  9823. {
  9824. Assert(JavascriptOperators::IsUndefinedObject(libraryUndefined));
  9825. AssertMsg((instance == libraryUndefined)
  9826. == JavascriptOperators::IsUndefinedObject(instance), "Wrong ScriptContext?");
  9827. return instance == libraryUndefined;
  9828. }
  9829. BOOL JavascriptOperators::IsUndefinedObject(Var instance, ScriptContext *scriptContext)
  9830. {
  9831. return JavascriptOperators::IsUndefinedObject(instance, scriptContext->GetLibrary());
  9832. }
  9833. BOOL JavascriptOperators::IsUndefinedObject(Var instance, JavascriptLibrary* library)
  9834. {
  9835. Assert(!VarIs<RecyclableObject>(instance) ? TRUE : ((RecyclableObject*)instance)->GetScriptContext()->GetLibrary() == library );
  9836. return JavascriptOperators::IsUndefinedObject(instance, library->GetUndefined());
  9837. }
  9838. BOOL JavascriptOperators::IsAnyNumberValue(Var instance)
  9839. {
  9840. TypeId typeId = GetTypeId(instance);
  9841. return TypeIds_FirstNumberType <= typeId && typeId <= TypeIds_LastNumberType;
  9842. }
  9843. // GetIterator as described in ES6.0 (draft 22) Section 7.4.1
  9844. RecyclableObject* JavascriptOperators::GetIterator(Var iterable, ScriptContext* scriptContext, bool optional)
  9845. {
  9846. RecyclableObject* iterableObj = JavascriptOperators::ToObject(iterable, scriptContext);
  9847. return JavascriptOperators::GetIterator(iterableObj, scriptContext, optional);
  9848. }
  9849. RecyclableObject* JavascriptOperators::GetIteratorFunction(Var iterable, ScriptContext* scriptContext, bool optional)
  9850. {
  9851. RecyclableObject* iterableObj = JavascriptOperators::ToObject(iterable, scriptContext);
  9852. return JavascriptOperators::GetIteratorFunction(iterableObj, scriptContext, optional);
  9853. }
  9854. RecyclableObject* JavascriptOperators::GetIteratorFunction(RecyclableObject* instance, ScriptContext * scriptContext, bool optional)
  9855. {
  9856. Var func = JavascriptOperators::GetPropertyNoCache(instance, PropertyIds::_symbolIterator, scriptContext);
  9857. if (optional && JavascriptOperators::IsUndefinedOrNull(func))
  9858. {
  9859. return nullptr;
  9860. }
  9861. if (!JavascriptConversion::IsCallable(func))
  9862. {
  9863. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction);
  9864. }
  9865. RecyclableObject* function = VarTo<RecyclableObject>(func);
  9866. return function;
  9867. }
  9868. RecyclableObject* JavascriptOperators::GetIterator(RecyclableObject* instance, ScriptContext * scriptContext, bool optional)
  9869. {
  9870. RecyclableObject* function = GetIteratorFunction(instance, scriptContext, optional);
  9871. if (function == nullptr)
  9872. {
  9873. Assert(optional);
  9874. return nullptr;
  9875. }
  9876. Var iterator = scriptContext->GetThreadContext()->ExecuteImplicitCall(function, Js::ImplicitCall_Accessor, [=]()->Js::Var
  9877. {
  9878. return CALL_FUNCTION(scriptContext->GetThreadContext(), function, CallInfo(Js::CallFlags_Value, 1), instance);
  9879. });
  9880. if (!JavascriptOperators::IsObject(iterator))
  9881. {
  9882. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  9883. }
  9884. return VarTo<RecyclableObject>(iterator);
  9885. }
  9886. void JavascriptOperators::IteratorClose(RecyclableObject* iterator, ScriptContext* scriptContext)
  9887. {
  9888. try
  9889. {
  9890. Var func = JavascriptOperators::GetProperty(iterator, PropertyIds::return_, scriptContext);
  9891. if (JavascriptConversion::IsCallable(func))
  9892. {
  9893. RecyclableObject* callable = VarTo<RecyclableObject>(func);
  9894. scriptContext->GetThreadContext()->ExecuteImplicitCall(callable, ImplicitCall_Accessor, [=]()->Var
  9895. {
  9896. Js::Var args[] = { iterator };
  9897. Js::CallInfo callInfo(Js::CallFlags_Value, _countof(args));
  9898. return JavascriptFunction::CallFunction<true>(callable, callable->GetEntryPoint(), Js::Arguments(callInfo, args));
  9899. });
  9900. }
  9901. }
  9902. catch (const JavascriptException& err)
  9903. {
  9904. err.GetAndClear(); // discard exception object
  9905. // We have arrived in this function due to AbruptCompletion (which is an exception), so we don't need to
  9906. // propagate the exception of calling return function
  9907. }
  9908. }
  9909. RecyclableObject* JavascriptOperators::CacheIteratorNext(RecyclableObject* iterator, ScriptContext* scriptContext)
  9910. {
  9911. Var nextFunc = JavascriptOperators::GetPropertyNoCache(iterator, PropertyIds::next, scriptContext);
  9912. ThreadContext *threadContext = scriptContext->GetThreadContext();
  9913. if (!JavascriptConversion::IsCallable(nextFunc))
  9914. {
  9915. if (!threadContext->RecordImplicitException())
  9916. {
  9917. return scriptContext->GetLibrary()->GetUndefined();
  9918. }
  9919. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction);
  9920. }
  9921. return VarTo<RecyclableObject>(nextFunc);
  9922. }
  9923. // IteratorNext as described in ES6.0 (draft 22) Section 7.4.2
  9924. RecyclableObject* JavascriptOperators::IteratorNext(RecyclableObject* iterator, ScriptContext* scriptContext, RecyclableObject* nextFunc, Var value)
  9925. {
  9926. ThreadContext *threadContext = scriptContext->GetThreadContext();
  9927. Var result = threadContext->ExecuteImplicitCall(nextFunc, ImplicitCall_Accessor, [=]() -> Var
  9928. {
  9929. Js::Var args[] = { iterator, value };
  9930. Js::CallInfo callInfo(Js::CallFlags_Value, _countof(args) + (value == nullptr ? -1 : 0));
  9931. return JavascriptFunction::CallFunction<true>(nextFunc, nextFunc->GetEntryPoint(), Arguments(callInfo, args));
  9932. });
  9933. if (!JavascriptOperators::IsObject(result))
  9934. {
  9935. if (!threadContext->RecordImplicitException())
  9936. {
  9937. return scriptContext->GetLibrary()->GetUndefined();
  9938. }
  9939. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  9940. }
  9941. return VarTo<RecyclableObject>(result);
  9942. }
  9943. // IteratorComplete as described in ES6.0 (draft 22) Section 7.4.3
  9944. bool JavascriptOperators::IteratorComplete(RecyclableObject* iterResult, ScriptContext* scriptContext)
  9945. {
  9946. Var done = JavascriptOperators::GetPropertyNoCache(iterResult, Js::PropertyIds::done, scriptContext);
  9947. return JavascriptConversion::ToBool(done, scriptContext);
  9948. }
  9949. // IteratorValue as described in ES6.0 (draft 22) Section 7.4.4
  9950. Var JavascriptOperators::IteratorValue(RecyclableObject* iterResult, ScriptContext* scriptContext)
  9951. {
  9952. return JavascriptOperators::GetPropertyNoCache(iterResult, Js::PropertyIds::value, scriptContext);
  9953. }
  9954. // IteratorStep as described in ES6.0 (draft 22) Section 7.4.5
  9955. bool JavascriptOperators::IteratorStep(RecyclableObject* iterator, ScriptContext* scriptContext, RecyclableObject* nextFunc, RecyclableObject** result)
  9956. {
  9957. Assert(result);
  9958. *result = JavascriptOperators::IteratorNext(iterator, scriptContext, nextFunc);
  9959. return !JavascriptOperators::IteratorComplete(*result, scriptContext);
  9960. }
  9961. bool JavascriptOperators::IteratorStepAndValue(RecyclableObject* iterator, ScriptContext* scriptContext, RecyclableObject* nextFunc, Var* resultValue)
  9962. {
  9963. // CONSIDER: Fast-pathing for iterators that are built-ins?
  9964. RecyclableObject* result = JavascriptOperators::IteratorNext(iterator, scriptContext, nextFunc);
  9965. if (!JavascriptOperators::IteratorComplete(result, scriptContext))
  9966. {
  9967. *resultValue = JavascriptOperators::IteratorValue(result, scriptContext);
  9968. return true;
  9969. }
  9970. return false;
  9971. }
  9972. RecyclableObject* JavascriptOperators::CreateFromConstructor(RecyclableObject* constructor, ScriptContext* scriptContext)
  9973. {
  9974. // Create a regular object and set the internal proto from the constructor
  9975. return JavascriptOperators::OrdinaryCreateFromConstructor(constructor, scriptContext->GetLibrary()->CreateObject(), nullptr, scriptContext);
  9976. }
  9977. RecyclableObject* JavascriptOperators::OrdinaryCreateFromConstructor(RecyclableObject* constructor, RecyclableObject* obj, DynamicObject* intrinsicProto, ScriptContext* scriptContext)
  9978. {
  9979. // There isn't a good way for us to add internal properties to objects in Chakra.
  9980. // Thus, caller should take care to create obj with the correct internal properties.
  9981. Var proto = JavascriptOperators::GetPropertyNoCache(constructor, Js::PropertyIds::prototype, scriptContext);
  9982. // If constructor.prototype is an object, we should use that as the [[Prototype]] for our obj.
  9983. // Else, we set the [[Prototype]] internal slot of obj to %intrinsicProto% - which should be the default.
  9984. if (JavascriptOperators::IsObjectType(JavascriptOperators::GetTypeId(proto)) &&
  9985. VarTo<DynamicObject>(proto) != intrinsicProto)
  9986. {
  9987. JavascriptObject::ChangePrototype(obj, VarTo<RecyclableObject>(proto), /*validate*/true, scriptContext);
  9988. }
  9989. return obj;
  9990. }
  9991. Var JavascriptOperators::GetProperty(RecyclableObject* instance, PropertyId propertyId, ScriptContext* requestContext, PropertyValueInfo* info)
  9992. {
  9993. return JavascriptOperators::GetProperty(instance, instance, propertyId, requestContext, info);
  9994. }
  9995. BOOL JavascriptOperators::GetProperty(RecyclableObject* instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  9996. {
  9997. return JavascriptOperators::GetProperty(instance, instance, propertyId, value, requestContext, info);
  9998. }
  9999. Var JavascriptOperators::GetProperty(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, ScriptContext* requestContext, PropertyValueInfo* info)
  10000. {
  10001. Var value;
  10002. if (JavascriptOperators::GetProperty(instance, propertyObject, propertyId, &value, requestContext, info))
  10003. {
  10004. return value;
  10005. }
  10006. return requestContext->GetMissingPropertyResult();
  10007. }
  10008. Var JavascriptOperators::GetPropertyNoCache(RecyclableObject* instance, PropertyId propertyId, ScriptContext* requestContext)
  10009. {
  10010. return JavascriptOperators::GetPropertyNoCache(instance, instance, propertyId, requestContext);
  10011. }
  10012. Var JavascriptOperators::GetPropertyNoCache(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, ScriptContext* requestContext)
  10013. {
  10014. Var value;
  10015. JavascriptOperators::GetProperty_InternalSimple(instance, propertyObject, propertyId, &value, requestContext);
  10016. return value;
  10017. }
  10018. BOOL JavascriptOperators::GetPropertyNoCache(RecyclableObject* instance, PropertyId propertyId, Var* value, ScriptContext* requestContext)
  10019. {
  10020. return JavascriptOperators::GetPropertyNoCache(instance, instance, propertyId, value, requestContext);
  10021. }
  10022. BOOL JavascriptOperators::GetPropertyNoCache(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, Var* value, ScriptContext* requestContext)
  10023. {
  10024. return JavascriptOperators::GetProperty_InternalSimple(instance, propertyObject, propertyId, value, requestContext);
  10025. }
  10026. Var JavascriptOperators::GetRootProperty(RecyclableObject* instance, PropertyId propertyId, ScriptContext* requestContext, PropertyValueInfo* info)
  10027. {
  10028. Var value;
  10029. if (JavascriptOperators::GetRootProperty(instance, propertyId, &value, requestContext, info))
  10030. {
  10031. return value;
  10032. }
  10033. return requestContext->GetMissingPropertyResult();
  10034. }
  10035. BOOL JavascriptOperators::GetPropertyReference(RecyclableObject *instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  10036. {
  10037. return JavascriptOperators::GetPropertyReference(instance, instance, propertyId, value, requestContext, info);
  10038. }
  10039. Var JavascriptOperators::GetItem(RecyclableObject* instance, uint32 index, ScriptContext* requestContext)
  10040. {
  10041. Var value;
  10042. if (GetItem(instance, index, &value, requestContext))
  10043. {
  10044. return value;
  10045. }
  10046. return requestContext->GetMissingItemResult();
  10047. }
  10048. Var JavascriptOperators::GetItem(RecyclableObject* instance, uint64 index, ScriptContext* requestContext)
  10049. {
  10050. Var value;
  10051. if (GetItem(instance, index, &value, requestContext))
  10052. {
  10053. return value;
  10054. }
  10055. return requestContext->GetMissingItemResult();
  10056. }
  10057. BOOL JavascriptOperators::GetItem(RecyclableObject* instance, uint64 index, Var* value, ScriptContext* requestContext)
  10058. {
  10059. if (index < JavascriptArray::InvalidIndex)
  10060. {
  10061. // In case index fits in uint32, we can avoid the (slower) big-index path
  10062. return GetItem(instance, static_cast<uint32>(index), value, requestContext);
  10063. }
  10064. PropertyRecord const * propertyRecord = nullptr;
  10065. JavascriptOperators::GetPropertyIdForInt(index, requestContext, &propertyRecord);
  10066. return JavascriptOperators::GetProperty(instance, propertyRecord->GetPropertyId(), value, requestContext);
  10067. }
  10068. BOOL JavascriptOperators::GetItem(RecyclableObject* instance, uint32 index, Var* value, ScriptContext* requestContext)
  10069. {
  10070. return JavascriptOperators::GetItem(instance, instance, index, value, requestContext);
  10071. }
  10072. BOOL JavascriptOperators::GetItemReference(RecyclableObject* instance, uint32 index, Var* value, ScriptContext* requestContext)
  10073. {
  10074. return GetItemReference(instance, instance, index, value, requestContext);
  10075. }
  10076. BOOL JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(RecyclableObject* instance, PropertyId propertyId, Var* setterValue, DescriptorFlags* flags, PropertyValueInfo* info, ScriptContext* scriptContext)
  10077. {
  10078. if (propertyId == Js::PropertyIds::__proto__)
  10079. {
  10080. return CheckPrototypesForAccessorOrNonWritablePropertyCore<PropertyId, false, false>(instance, propertyId, setterValue, flags, info, scriptContext);
  10081. }
  10082. else
  10083. {
  10084. return CheckPrototypesForAccessorOrNonWritablePropertyCore<PropertyId, true, false>(instance, propertyId, setterValue, flags, info, scriptContext);
  10085. }
  10086. }
  10087. BOOL JavascriptOperators::CheckPrototypesForAccessorOrNonWritableRootProperty(RecyclableObject* instance, PropertyId propertyId, Var* setterValue, DescriptorFlags* flags, PropertyValueInfo* info, ScriptContext* scriptContext)
  10088. {
  10089. if (propertyId == Js::PropertyIds::__proto__)
  10090. {
  10091. return CheckPrototypesForAccessorOrNonWritablePropertyCore<PropertyId, false, true>(instance, propertyId, setterValue, flags, info, scriptContext);
  10092. }
  10093. else
  10094. {
  10095. return CheckPrototypesForAccessorOrNonWritablePropertyCore<PropertyId, true, true>(instance, propertyId, setterValue, flags, info, scriptContext);
  10096. }
  10097. }
  10098. BOOL JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(RecyclableObject* instance, JavascriptString* propertyNameString, Var* setterValue, DescriptorFlags* flags, PropertyValueInfo* info, ScriptContext* scriptContext)
  10099. {
  10100. Js::PropertyRecord const * localPropertyRecord;
  10101. propertyNameString->GetPropertyRecord(&localPropertyRecord);
  10102. PropertyId propertyId = localPropertyRecord->GetPropertyId();
  10103. return CheckPrototypesForAccessorOrNonWritableProperty(instance, propertyId, setterValue, flags, info, scriptContext);
  10104. }
  10105. BOOL JavascriptOperators::SetProperty(Var instance, RecyclableObject* object, PropertyId propertyId, Var newValue, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  10106. {
  10107. PropertyValueInfo info;
  10108. return JavascriptOperators::SetProperty(instance, object, propertyId, newValue, &info, requestContext, propertyOperationFlags);
  10109. }
  10110. BOOL JavascriptOperators::TryConvertToUInt32(const char16* str, int length, uint32* intVal)
  10111. {
  10112. return NumberUtilities::TryConvertToUInt32(str, length, intVal);
  10113. }
  10114. template <typename TPropertyKey>
  10115. DescriptorFlags JavascriptOperators::GetRootSetter(RecyclableObject* instance, TPropertyKey propertyKey, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  10116. {
  10117. // This is provided only so that CheckPrototypesForAccessorOrNonWritablePropertyCore will compile.
  10118. // It will never be called.
  10119. Throw::FatalInternalError();
  10120. }
  10121. template <>
  10122. inline DescriptorFlags JavascriptOperators::GetRootSetter(RecyclableObject* instance, PropertyId propertyId, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  10123. {
  10124. AssertMsg(JavascriptOperators::GetTypeId(instance) == TypeIds_GlobalObject
  10125. || JavascriptOperators::GetTypeId(instance) == TypeIds_ModuleRoot,
  10126. "Root must be a global object!");
  10127. RootObjectBase* rootObject = static_cast<RootObjectBase*>(instance);
  10128. return rootObject->GetRootSetter(propertyId, setterValue, info, requestContext);
  10129. }
  10130. // Helper to fetch @@species from a constructor object
  10131. Var JavascriptOperators::GetSpecies(RecyclableObject* constructor, ScriptContext* scriptContext)
  10132. {
  10133. Var species = nullptr;
  10134. // Let S be Get(C, @@species)
  10135. if (JavascriptOperators::GetProperty(constructor, PropertyIds::_symbolSpecies, &species, scriptContext)
  10136. && !JavascriptOperators::IsUndefinedOrNull(species))
  10137. {
  10138. // If S is neither undefined nor null, let C be S
  10139. return species;
  10140. }
  10141. return constructor;
  10142. }