JavascriptOperators.cpp 502 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeLanguagePch.h"
  6. #include "Types/PathTypeHandler.h"
  7. #include "Types/PropertyIndexRanges.h"
  8. #include "Types/UnscopablesWrapperObject.h"
  9. #include "Types/SpreadArgument.h"
  10. #include "Library/JavascriptPromise.h"
  11. #include "Library/JavascriptRegularExpression.h"
  12. #include "Library/ThrowErrorObject.h"
  13. #include "Library/JavascriptGeneratorFunction.h"
  14. #include "Library/ForInObjectEnumerator.h"
  15. #include "Library/ES5Array.h"
  16. #include "Types/SimpleDictionaryPropertyDescriptor.h"
  17. #include "Types/SimpleDictionaryTypeHandler.h"
  18. #include "Language/ModuleNamespace.h"
  19. #ifndef SCRIPT_DIRECT_TYPE
  20. typedef enum JsNativeValueType: int
  21. {
  22. JsInt8Type,
  23. JsUint8Type,
  24. JsInt16Type,
  25. JsUint16Type,
  26. JsInt32Type,
  27. JsUint32Type,
  28. JsInt64Type,
  29. JsUint64Type,
  30. JsFloatType,
  31. JsDoubleType,
  32. JsNativeStringType
  33. } JsNativeValueType;
  34. typedef struct JsNativeString
  35. {
  36. unsigned int length;
  37. LPCWSTR str;
  38. } JsNativeString;
  39. #endif
  40. using namespace Js;
  41. DEFINE_RECYCLER_TRACKER_ARRAY_PERF_COUNTER(Var);
  42. DEFINE_RECYCLER_TRACKER_PERF_COUNTER(FrameDisplay);
  43. enum IndexType
  44. {
  45. IndexType_Number,
  46. IndexType_PropertyId,
  47. IndexType_JavascriptString
  48. };
  49. IndexType GetIndexTypeFromString(char16 const * propertyName, charcount_t propertyLength, ScriptContext* scriptContext, uint32* index, PropertyRecord const** propertyRecord, bool createIfNotFound)
  50. {
  51. if (JavascriptOperators::TryConvertToUInt32(propertyName, propertyLength, index) &&
  52. (*index != JavascriptArray::InvalidIndex))
  53. {
  54. return IndexType_Number;
  55. }
  56. else
  57. {
  58. if (createIfNotFound)
  59. {
  60. scriptContext->GetOrAddPropertyRecord(propertyName, propertyLength, propertyRecord);
  61. }
  62. else
  63. {
  64. scriptContext->FindPropertyRecord(propertyName, propertyLength, propertyRecord);
  65. }
  66. return IndexType_PropertyId;
  67. }
  68. }
  69. IndexType GetIndexTypeFromPrimitive(Var indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, JavascriptString ** propertyNameString, bool createIfNotFound, bool preferJavascriptStringOverPropertyRecord)
  70. {
  71. // CONSIDER: Only OP_SetElementI and OP_GetElementI use and take advantage of the
  72. // IndexType_JavascriptString result. Consider modifying other callers of GetIndexType to take
  73. // advantage of non-interned property strings where appropriate.
  74. if (TaggedInt::Is(indexVar))
  75. {
  76. int indexInt = TaggedInt::ToInt32(indexVar);
  77. if (indexInt >= 0)
  78. {
  79. *index = (uint)indexInt;
  80. return IndexType_Number;
  81. }
  82. else
  83. {
  84. char16 stringBuffer[22];
  85. int pos = TaggedInt::ToBuffer(indexInt, stringBuffer, _countof(stringBuffer));
  86. charcount_t length = (_countof(stringBuffer) - 1) - pos;
  87. if (createIfNotFound || preferJavascriptStringOverPropertyRecord)
  88. {
  89. // When preferring JavascriptString objects, just return a PropertyRecord instead
  90. // of creating temporary JavascriptString objects for every negative integer that
  91. // comes through here.
  92. scriptContext->GetOrAddPropertyRecord(stringBuffer + pos, length, propertyRecord);
  93. }
  94. else
  95. {
  96. scriptContext->FindPropertyRecord(stringBuffer + pos, length, propertyRecord);
  97. }
  98. return IndexType_PropertyId;
  99. }
  100. }
  101. if (JavascriptNumber::Is_NoTaggedIntCheck(indexVar))
  102. {
  103. // If this double can be a positive integer index, convert it.
  104. int32 value = 0;
  105. bool isInt32 = false;
  106. if (JavascriptNumber::TryGetInt32OrUInt32Value(JavascriptNumber::GetValue(indexVar), &value, &isInt32)
  107. && !isInt32
  108. && static_cast<uint32>(value) < JavascriptArray::InvalidIndex)
  109. {
  110. *index = static_cast<uint32>(value);
  111. return IndexType_Number;
  112. }
  113. // Fall through to slow string conversion.
  114. }
  115. JavascriptSymbol * symbol = JavascriptOperators::TryFromVar<JavascriptSymbol>(indexVar);
  116. if (symbol)
  117. {
  118. // JavascriptSymbols cannot add a new PropertyRecord - they correspond to one and only one existing PropertyRecord.
  119. // We already know what the PropertyRecord is since it is stored in the JavascriptSymbol itself so just return it.
  120. *propertyRecord = symbol->GetValue();
  121. return IndexType_PropertyId;
  122. }
  123. else
  124. {
  125. JavascriptString* indexStr = JavascriptConversion::ToString(indexVar, scriptContext);
  126. char16 const * propertyName = indexStr->GetString();
  127. charcount_t const propertyLength = indexStr->GetLength();
  128. if (!createIfNotFound && preferJavascriptStringOverPropertyRecord)
  129. {
  130. if (JavascriptOperators::TryConvertToUInt32(propertyName, propertyLength, index) &&
  131. (*index != JavascriptArray::InvalidIndex))
  132. {
  133. return IndexType_Number;
  134. }
  135. *propertyNameString = indexStr;
  136. return IndexType_JavascriptString;
  137. }
  138. return GetIndexTypeFromString(propertyName, propertyLength, scriptContext, index, propertyRecord, createIfNotFound);
  139. }
  140. }
  141. IndexType GetIndexTypeFromPrimitive(Var indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, bool createIfNotFound)
  142. {
  143. return GetIndexTypeFromPrimitive(indexVar, scriptContext, index, propertyRecord, nullptr, createIfNotFound, false);
  144. }
  145. IndexType GetIndexType(Var& indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, JavascriptString ** propertyNameString, bool createIfNotFound, bool preferJavascriptStringOverPropertyRecord)
  146. {
  147. indexVar = JavascriptConversion::ToPrimitive<JavascriptHint::HintString>(indexVar, scriptContext);
  148. return GetIndexTypeFromPrimitive(indexVar, scriptContext, index, propertyRecord, propertyNameString, createIfNotFound, preferJavascriptStringOverPropertyRecord);
  149. }
  150. IndexType GetIndexType(Var& indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, bool createIfNotFound)
  151. {
  152. return GetIndexType(indexVar, scriptContext, index, propertyRecord, nullptr, createIfNotFound, false);
  153. }
  154. BOOL FEqualDbl(double dbl1, double dbl2)
  155. {
  156. // If the low ulongs don't match, they can't be equal.
  157. if (Js::NumberUtilities::LuLoDbl(dbl1) != Js::NumberUtilities::LuLoDbl(dbl2))
  158. return FALSE;
  159. // If the high ulongs don't match, they can be equal iff one is -0 and
  160. // the other is +0.
  161. if (Js::NumberUtilities::LuHiDbl(dbl1) != Js::NumberUtilities::LuHiDbl(dbl2))
  162. {
  163. return 0x80000000 == (Js::NumberUtilities::LuHiDbl(dbl1) | Js::NumberUtilities::LuHiDbl(dbl2)) &&
  164. 0 == Js::NumberUtilities::LuLoDbl(dbl1);
  165. }
  166. // The bit patterns match. They are equal iff they are not Nan.
  167. return !Js::NumberUtilities::IsNan(dbl1);
  168. }
  169. Var JavascriptOperators::OP_ApplyArgs(Var func, Var instance, __in_xcount(8) void** stackPtr, CallInfo callInfo, ScriptContext* scriptContext)
  170. {
  171. JIT_HELPER_REENTRANT_HEADER(Op_OP_ApplyArgs);
  172. int argCount = callInfo.Count;
  173. ///
  174. /// Check func has internal [[Call]] property
  175. /// If not, throw TypeError
  176. ///
  177. if (!JavascriptConversion::IsCallable(func)) {
  178. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction);
  179. }
  180. // Fix callInfo: expect result/value, and none of other flags are currently applicable.
  181. // OP_ApplyArgs expects a result. Neither of {jit, interpreted} mode sends correct callFlags:
  182. // LdArgCnt -- jit sends whatever was passed to current function, interpreter always sends 0.
  183. // See Win8 bug 490489.
  184. callInfo.Flags = CallFlags_Value;
  185. RecyclableObject *funcPtr = RecyclableObject::UnsafeFromVar(func);
  186. PROBE_STACK(scriptContext, Js::Constants::MinStackDefault + argCount * 4);
  187. JavascriptMethod entryPoint = funcPtr->GetEntryPoint();
  188. Var ret;
  189. switch (argCount) {
  190. case 0:
  191. Assert(false);
  192. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo);
  193. break;
  194. case 1:
  195. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance);
  196. break;
  197. case 2:
  198. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0]);
  199. break;
  200. case 3:
  201. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1]);
  202. break;
  203. case 4:
  204. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2]);
  205. break;
  206. case 5:
  207. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2], stackPtr[3]);
  208. break;
  209. case 6:
  210. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2], stackPtr[3], stackPtr[4]);
  211. break;
  212. case 7:
  213. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2], stackPtr[3], stackPtr[4], stackPtr[5]);
  214. break;
  215. default:
  216. {
  217. // Don't need stack probe here- we just did so above
  218. Arguments args(callInfo, stackPtr - 1);
  219. BEGIN_SAFE_REENTRANT_CALL(scriptContext->GetThreadContext())
  220. {
  221. ret = JavascriptFunction::CallFunction<false>(funcPtr, entryPoint, args);
  222. }
  223. END_SAFE_REENTRANT_CALL
  224. break;
  225. }
  226. }
  227. return ret;
  228. JIT_HELPER_END(Op_OP_ApplyArgs);
  229. }
  230. #ifdef _M_IX86
  231. // Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
  232. Var JavascriptOperators::Int32ToVar(int32 value, ScriptContext* scriptContext)
  233. {
  234. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_Int32ToAtom);
  235. return JavascriptNumber::ToVar(value, scriptContext);
  236. JIT_HELPER_END(Op_Int32ToAtom);
  237. }
  238. // Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
  239. Var JavascriptOperators::Int32ToVarInPlace(int32 value, ScriptContext* scriptContext, JavascriptNumber* result)
  240. {
  241. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_Int32ToAtomInPlace);
  242. return JavascriptNumber::ToVarInPlace(value, scriptContext, result);
  243. JIT_HELPER_END(Op_Int32ToAtomInPlace);
  244. }
  245. // Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
  246. Var JavascriptOperators::UInt32ToVar(uint32 value, ScriptContext* scriptContext)
  247. {
  248. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_UInt32ToAtom);
  249. return JavascriptNumber::ToVar(value, scriptContext);
  250. JIT_HELPER_END(Op_UInt32ToAtom);
  251. }
  252. // Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
  253. Var JavascriptOperators::UInt32ToVarInPlace(uint32 value, ScriptContext* scriptContext, JavascriptNumber* result)
  254. {
  255. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_UInt32ToAtomInPlace);
  256. return JavascriptNumber::ToVarInPlace(value, scriptContext, result);
  257. JIT_HELPER_END(Op_UInt32ToAtomInPlace);
  258. }
  259. #endif
  260. Var JavascriptOperators::OP_FinishOddDivBy2(uint32 value, ScriptContext *scriptContext)
  261. {
  262. return JavascriptNumber::New((double)(value + 0.5), scriptContext);
  263. }
  264. Var JavascriptOperators::ToNumberInPlace(Var aRight, ScriptContext* scriptContext, JavascriptNumber* result)
  265. {
  266. JIT_HELPER_REENTRANT_HEADER(Op_ConvNumberInPlace);
  267. if (TaggedInt::Is(aRight) || JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  268. {
  269. return aRight;
  270. }
  271. return JavascriptNumber::ToVarInPlace(JavascriptConversion::ToNumber(aRight, scriptContext), scriptContext, result);
  272. JIT_HELPER_END(Op_ConvNumberInPlace);
  273. }
  274. Var JavascriptOperators::Typeof(Var var, ScriptContext* scriptContext)
  275. {
  276. JIT_HELPER_REENTRANT_HEADER(Op_Typeof);
  277. switch (JavascriptOperators::GetTypeId(var))
  278. {
  279. case TypeIds_Undefined:
  280. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  281. case TypeIds_Null:
  282. //null
  283. return scriptContext->GetLibrary()->GetObjectTypeDisplayString();
  284. case TypeIds_Integer:
  285. case TypeIds_Number:
  286. case TypeIds_Int64Number:
  287. case TypeIds_UInt64Number:
  288. return scriptContext->GetLibrary()->GetNumberTypeDisplayString();
  289. default:
  290. // Falsy objects are typeof 'undefined'.
  291. if (RecyclableObject::FromVar(var)->GetType()->IsFalsy())
  292. {
  293. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  294. }
  295. else
  296. {
  297. return RecyclableObject::FromVar(var)->GetTypeOfString(scriptContext);
  298. }
  299. }
  300. JIT_HELPER_END(Op_Typeof);
  301. }
  302. Var JavascriptOperators::TypeofFld(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  303. {
  304. return TypeofFld_Internal(instance, false, propertyId, scriptContext);
  305. }
  306. Var JavascriptOperators::TypeofRootFld(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  307. {
  308. return TypeofFld_Internal(instance, true, propertyId, scriptContext);
  309. }
  310. Var JavascriptOperators::TypeofFld_Internal(Var instance, const bool isRoot, PropertyId propertyId, ScriptContext* scriptContext)
  311. {
  312. RecyclableObject* object = nullptr;
  313. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  314. {
  315. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined , scriptContext->GetPropertyName(propertyId)->GetBuffer());
  316. }
  317. Var value = nullptr;
  318. try
  319. {
  320. Js::JavascriptExceptionOperators::AutoCatchHandlerExists autoCatchHandlerExists(scriptContext);
  321. // In edge mode, spec compat is more important than backward compat. Use spec/web behavior here
  322. if (isRoot
  323. ? !JavascriptOperators::GetRootProperty(instance, propertyId, &value, scriptContext)
  324. : !JavascriptOperators::GetProperty(instance, object, propertyId, &value, scriptContext))
  325. {
  326. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  327. }
  328. if (!scriptContext->IsUndeclBlockVar(value))
  329. {
  330. return JavascriptOperators::Typeof(value, scriptContext);
  331. }
  332. }
  333. catch(const JavascriptException& err)
  334. {
  335. err.GetAndClear(); // discard exception object
  336. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  337. }
  338. Assert(scriptContext->IsUndeclBlockVar(value));
  339. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  340. }
  341. Var JavascriptOperators::TypeofElem_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  342. {
  343. JIT_HELPER_REENTRANT_HEADER(Op_TypeofElem_UInt32);
  344. if (JavascriptOperators::IsNumberFromNativeArray(instance, index, scriptContext))
  345. return scriptContext->GetLibrary()->GetNumberTypeDisplayString();
  346. #if FLOATVAR
  347. return TypeofElem(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  348. #else
  349. char buffer[sizeof(Js::JavascriptNumber)];
  350. return TypeofElem(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  351. (Js::JavascriptNumber *)buffer), scriptContext);
  352. #endif
  353. JIT_HELPER_END(Op_TypeofElem_UInt32);
  354. }
  355. Var JavascriptOperators::TypeofElem_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  356. {
  357. JIT_HELPER_REENTRANT_HEADER(Op_TypeofElem_Int32);
  358. if (JavascriptOperators::IsNumberFromNativeArray(instance, index, scriptContext))
  359. return scriptContext->GetLibrary()->GetNumberTypeDisplayString();
  360. #if FLOATVAR
  361. return TypeofElem(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  362. #else
  363. char buffer[sizeof(Js::JavascriptNumber)];
  364. return TypeofElem(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  365. (Js::JavascriptNumber *)buffer), scriptContext);
  366. #endif
  367. JIT_HELPER_END(Op_TypeofElem_Int32);
  368. }
  369. Js::JavascriptString* GetPropertyDisplayNameForError(Var prop, ScriptContext* scriptContext)
  370. {
  371. JavascriptString* str;
  372. JavascriptSymbol *symbol = JavascriptOperators::TryFromVar<JavascriptSymbol>(prop);
  373. if (symbol)
  374. {
  375. str = JavascriptSymbol::ToString(symbol->GetValue(), scriptContext);
  376. }
  377. else
  378. {
  379. str = JavascriptConversion::ToString(prop, scriptContext);
  380. }
  381. return str;
  382. }
  383. Var JavascriptOperators::TypeofElem(Var instance, Var index, ScriptContext* scriptContext)
  384. {
  385. JIT_HELPER_REENTRANT_HEADER(Op_TypeofElem);
  386. RecyclableObject* object = nullptr;
  387. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  388. {
  389. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined, GetPropertyDisplayNameForError(index, scriptContext));
  390. }
  391. Var member = nullptr;
  392. uint32 indexVal;
  393. PropertyRecord const * propertyRecord = nullptr;
  394. ThreadContext* threadContext = scriptContext->GetThreadContext();
  395. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  396. threadContext->ClearImplicitCallFlags();
  397. try
  398. {
  399. Js::JavascriptExceptionOperators::AutoCatchHandlerExists autoCatchHandlerExists(scriptContext);
  400. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, false);
  401. // For JS Objects, don't create the propertyId if not already added
  402. if (indexType == IndexType_Number)
  403. {
  404. // In edge mode, we don't need to worry about the special "unknown" behavior. If the item is not available from Get,
  405. // just return undefined.
  406. if (!JavascriptOperators::GetItem(instance, object, indexVal, &member, scriptContext))
  407. {
  408. // If the instance doesn't have the item, typeof result is "undefined".
  409. threadContext->CheckAndResetImplicitCallAccessorFlag();
  410. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  411. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  412. }
  413. }
  414. else
  415. {
  416. Assert(indexType == IndexType_PropertyId);
  417. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  418. {
  419. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
  420. Assert(indexType == IndexType_PropertyId);
  421. Assert(propertyRecord != nullptr);
  422. }
  423. if (propertyRecord != nullptr)
  424. {
  425. if (!JavascriptOperators::GetProperty(instance, object, propertyRecord->GetPropertyId(), &member, scriptContext))
  426. {
  427. // If the instance doesn't have the property, typeof result is "undefined".
  428. threadContext->CheckAndResetImplicitCallAccessorFlag();
  429. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  430. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  431. }
  432. }
  433. else
  434. {
  435. #if DBG
  436. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  437. PropertyRecord const * debugPropertyRecord;
  438. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  439. 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");
  440. #endif
  441. // If the instance doesn't have the property, typeof result is "undefined".
  442. threadContext->CheckAndResetImplicitCallAccessorFlag();
  443. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  444. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  445. }
  446. }
  447. threadContext->CheckAndResetImplicitCallAccessorFlag();
  448. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  449. return JavascriptOperators::Typeof(member, scriptContext);
  450. }
  451. catch(const JavascriptException& err)
  452. {
  453. err.GetAndClear(); // discard exception object
  454. threadContext->CheckAndResetImplicitCallAccessorFlag();
  455. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  456. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  457. }
  458. JIT_HELPER_END(Op_TypeofElem);
  459. }
  460. //
  461. // Delete the given Var
  462. //
  463. Var JavascriptOperators::Delete(Var var, ScriptContext* scriptContext)
  464. {
  465. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_Delete);
  466. return scriptContext->GetLibrary()->GetTrue();
  467. JIT_HELPER_END(Op_Delete);
  468. }
  469. BOOL JavascriptOperators::Equal_Full(Var aLeft, Var aRight, ScriptContext* requestContext)
  470. {
  471. JIT_HELPER_REENTRANT_HEADER(Op_Equal_Full);
  472. //
  473. // Fast-path SmInts and paired Number combinations.
  474. //
  475. if (aLeft == aRight)
  476. {
  477. if (JavascriptNumber::Is(aLeft) && JavascriptNumber::IsNan(JavascriptNumber::GetValue(aLeft)))
  478. {
  479. return false;
  480. }
  481. else if (JavascriptVariantDate::Is(aLeft) == false) // only need to check on aLeft - since they are the same var, aRight would do the same
  482. {
  483. return true;
  484. }
  485. else
  486. {
  487. //In ES5 mode strict equals (===) on same instance of object type VariantDate succeeds.
  488. //Hence equals needs to succeed.
  489. return true;
  490. }
  491. }
  492. BOOL result = false;
  493. if (TaggedInt::Is(aLeft))
  494. {
  495. if (TaggedInt::Is(aRight))
  496. {
  497. // If aLeft == aRight, we would already have returned true above.
  498. return false;
  499. }
  500. else if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  501. {
  502. return TaggedInt::ToDouble(aLeft) == JavascriptNumber::GetValue(aRight);
  503. }
  504. else
  505. {
  506. BOOL res = RecyclableObject::UnsafeFromVar(aRight)->Equals(aLeft, &result, requestContext);
  507. AssertMsg(res, "Should have handled this");
  508. return result;
  509. }
  510. }
  511. else if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  512. {
  513. if (TaggedInt::Is(aRight))
  514. {
  515. return TaggedInt::ToDouble(aRight) == JavascriptNumber::GetValue(aLeft);
  516. }
  517. else if(JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  518. {
  519. return JavascriptNumber::GetValue(aLeft) == JavascriptNumber::GetValue(aRight);
  520. }
  521. else
  522. {
  523. BOOL res = RecyclableObject::UnsafeFromVar(aRight)->Equals(aLeft, &result, requestContext);
  524. AssertMsg(res, "Should have handled this");
  525. return result;
  526. }
  527. }
  528. if (RecyclableObject::UnsafeFromVar(aLeft)->Equals(aRight, &result, requestContext))
  529. {
  530. return result;
  531. }
  532. else
  533. {
  534. return false;
  535. }
  536. JIT_HELPER_END(Op_Equal_Full);
  537. }
  538. BOOL JavascriptOperators::Greater_Full(Var aLeft,Var aRight,ScriptContext* scriptContext)
  539. {
  540. JIT_HELPER_REENTRANT_HEADER(Op_Greater_Full);
  541. return RelationalComparisonHelper(aRight, aLeft, scriptContext, false, false);
  542. JIT_HELPER_END(Op_Greater_Full);
  543. }
  544. BOOL JavascriptOperators::Less_Full(Var aLeft, Var aRight, ScriptContext* scriptContext)
  545. {
  546. return RelationalComparisonHelper(aLeft, aRight, scriptContext, true, false);
  547. }
  548. BOOL JavascriptOperators::RelationalComparisonHelper(Var aLeft, Var aRight, ScriptContext* scriptContext, bool leftFirst, bool undefinedAs)
  549. {
  550. TypeId typeId = JavascriptOperators::GetTypeId(aLeft);
  551. if (typeId == TypeIds_Null)
  552. {
  553. aLeft=TaggedInt::ToVarUnchecked(0);
  554. }
  555. else if (typeId == TypeIds_Undefined)
  556. {
  557. aLeft=scriptContext->GetLibrary()->GetNaN();
  558. }
  559. typeId = JavascriptOperators::GetTypeId(aRight);
  560. if (typeId == TypeIds_Null)
  561. {
  562. aRight=TaggedInt::ToVarUnchecked(0);
  563. }
  564. else if (typeId == TypeIds_Undefined)
  565. {
  566. aRight=scriptContext->GetLibrary()->GetNaN();
  567. }
  568. double dblLeft, dblRight;
  569. TypeId leftType = JavascriptOperators::GetTypeId(aLeft);
  570. TypeId rightType = JavascriptOperators::GetTypeId(aRight);
  571. switch (leftType)
  572. {
  573. case TypeIds_Integer:
  574. dblLeft = TaggedInt::ToDouble(aLeft);
  575. switch (rightType)
  576. {
  577. case TypeIds_Integer:
  578. dblRight = TaggedInt::ToDouble(aRight);
  579. break;
  580. case TypeIds_Number:
  581. dblRight = JavascriptNumber::GetValue(aRight);
  582. break;
  583. default:
  584. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  585. break;
  586. }
  587. break;
  588. case TypeIds_Number:
  589. dblLeft = JavascriptNumber::GetValue(aLeft);
  590. switch (rightType)
  591. {
  592. case TypeIds_Integer:
  593. dblRight = TaggedInt::ToDouble(aRight);
  594. break;
  595. case TypeIds_Number:
  596. dblRight = JavascriptNumber::GetValue(aRight);
  597. break;
  598. default:
  599. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  600. break;
  601. }
  602. break;
  603. case TypeIds_Int64Number:
  604. {
  605. switch (rightType)
  606. {
  607. case TypeIds_Int64Number:
  608. {
  609. __int64 leftValue = JavascriptInt64Number::UnsafeFromVar(aLeft)->GetValue();
  610. __int64 rightValue = JavascriptInt64Number::UnsafeFromVar(aRight)->GetValue();
  611. return leftValue < rightValue;
  612. }
  613. break;
  614. case TypeIds_UInt64Number:
  615. {
  616. __int64 leftValue = JavascriptInt64Number::UnsafeFromVar(aLeft)->GetValue();
  617. unsigned __int64 rightValue = JavascriptUInt64Number::UnsafeFromVar(aRight)->GetValue();
  618. if (rightValue <= INT_MAX && leftValue >= 0)
  619. {
  620. return leftValue < (__int64)rightValue;
  621. }
  622. }
  623. break;
  624. }
  625. dblLeft = (double)JavascriptInt64Number::UnsafeFromVar(aLeft)->GetValue();
  626. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  627. }
  628. break;
  629. // we cannot do double conversion between 2 int64 numbers as we can get wrong result after conversion
  630. // i.e., two different numbers become the same after losing precision. We'll continue dbl comparison
  631. // if either number is not an int64 number.
  632. case TypeIds_UInt64Number:
  633. {
  634. switch (rightType)
  635. {
  636. case TypeIds_Int64Number:
  637. {
  638. unsigned __int64 leftValue = JavascriptUInt64Number::UnsafeFromVar(aLeft)->GetValue();
  639. __int64 rightValue = JavascriptInt64Number::UnsafeFromVar(aRight)->GetValue();
  640. if (leftValue < INT_MAX && rightValue >= 0)
  641. {
  642. return (__int64)leftValue < rightValue;
  643. }
  644. }
  645. break;
  646. case TypeIds_UInt64Number:
  647. {
  648. unsigned __int64 leftValue = JavascriptUInt64Number::UnsafeFromVar(aLeft)->GetValue();
  649. unsigned __int64 rightValue = JavascriptUInt64Number::UnsafeFromVar(aRight)->GetValue();
  650. return leftValue < rightValue;
  651. }
  652. break;
  653. }
  654. dblLeft = (double)JavascriptUInt64Number::UnsafeFromVar(aLeft)->GetValue();
  655. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  656. }
  657. break;
  658. case TypeIds_String:
  659. switch (rightType)
  660. {
  661. case TypeIds_Integer:
  662. case TypeIds_Number:
  663. case TypeIds_Boolean:
  664. break;
  665. default:
  666. aRight = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aRight, scriptContext);
  667. rightType = JavascriptOperators::GetTypeId(aRight);
  668. if (rightType != TypeIds_String)
  669. {
  670. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  671. break;
  672. }
  673. case TypeIds_String:
  674. return JavascriptString::LessThan(aLeft, aRight);
  675. }
  676. dblLeft = JavascriptConversion::ToNumber(aLeft, scriptContext);
  677. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  678. break;
  679. case TypeIds_Boolean:
  680. case TypeIds_Null:
  681. case TypeIds_Undefined:
  682. case TypeIds_Symbol:
  683. dblLeft = JavascriptConversion::ToNumber(aLeft, scriptContext);
  684. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  685. break;
  686. default:
  687. if (leftFirst)
  688. {
  689. aLeft = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aLeft, scriptContext);
  690. aRight = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aRight, scriptContext);
  691. }
  692. else
  693. {
  694. aRight = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aRight, scriptContext);
  695. aLeft = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aLeft, scriptContext);
  696. }
  697. //BugFix: When @@ToPrimitive of an object is overridden with a function that returns null/undefined
  698. //this helper will fall into a inescapable goto loop as the checks for null/undefined were outside of the path
  699. return RelationalComparisonHelper(aLeft, aRight, scriptContext, leftFirst, undefinedAs);
  700. }
  701. //
  702. // And +0,-0 that is not implemented fully
  703. //
  704. if (JavascriptNumber::IsNan(dblLeft) || JavascriptNumber::IsNan(dblRight))
  705. {
  706. return undefinedAs;
  707. }
  708. // this will succeed for -0.0 == 0.0 case as well
  709. if (dblLeft == dblRight)
  710. {
  711. return false;
  712. }
  713. return dblLeft < dblRight;
  714. }
  715. BOOL JavascriptOperators::StrictEqualString(Var aLeft, JavascriptString* aRight)
  716. {
  717. JIT_HELPER_REENTRANT_HEADER(Op_StrictEqualString);
  718. JIT_HELPER_SAME_ATTRIBUTES(Op_StrictEqualString, Op_StrictEqual);
  719. JavascriptString* leftStr = TryFromVar<JavascriptString>(aLeft);
  720. if (!leftStr)
  721. {
  722. return false;
  723. }
  724. JIT_HELPER_REENTRANT_HEADER(Op_StrictEqualString);
  725. JIT_HELPER_SAME_ATTRIBUTES(Op_StrictEqualString, Op_StrictEqual);
  726. return JavascriptString::Equals(leftStr, aRight);
  727. JIT_HELPER_END(Op_StrictEqualString);
  728. }
  729. BOOL JavascriptOperators::StrictEqualEmptyString(Var aLeft)
  730. {
  731. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_StrictEqualEmptyString);
  732. JavascriptString * string = JavascriptOperators::TryFromVar<JavascriptString>(aLeft);
  733. if (!string)
  734. {
  735. return false;
  736. }
  737. Assert(string);
  738. return string->GetLength() == 0;
  739. JIT_HELPER_END(Op_StrictEqualEmptyString);
  740. }
  741. BOOL JavascriptOperators::StrictEqual(Var aLeft, Var aRight, ScriptContext* requestContext)
  742. {
  743. JIT_HELPER_REENTRANT_HEADER(Op_StrictEqual);
  744. double dblLeft, dblRight;
  745. TypeId rightType, leftType;
  746. leftType = JavascriptOperators::GetTypeId(aLeft);
  747. // Because NaN !== NaN, we may not return TRUE when typeId is Number
  748. if (aLeft == aRight && leftType != TypeIds_Number) return TRUE;
  749. rightType = JavascriptOperators::GetTypeId(aRight);
  750. switch (leftType)
  751. {
  752. case TypeIds_String:
  753. switch (rightType)
  754. {
  755. case TypeIds_String:
  756. return JavascriptString::Equals(JavascriptString::UnsafeFromVar(aLeft), JavascriptString::UnsafeFromVar(aRight));
  757. }
  758. return FALSE;
  759. case TypeIds_Integer:
  760. switch (rightType)
  761. {
  762. case TypeIds_Integer:
  763. return aLeft == aRight;
  764. // we don't need to worry about int64: it cannot equal as we create
  765. // JavascriptInt64Number only in overflow scenarios.
  766. case TypeIds_Number:
  767. dblLeft = TaggedInt::ToDouble(aLeft);
  768. dblRight = JavascriptNumber::GetValue(aRight);
  769. goto CommonNumber;
  770. }
  771. return FALSE;
  772. case TypeIds_Int64Number:
  773. switch (rightType)
  774. {
  775. case TypeIds_Int64Number:
  776. {
  777. __int64 leftValue = JavascriptInt64Number::UnsafeFromVar(aLeft)->GetValue();
  778. __int64 rightValue = JavascriptInt64Number::UnsafeFromVar(aRight)->GetValue();
  779. return leftValue == rightValue;
  780. }
  781. case TypeIds_UInt64Number:
  782. {
  783. __int64 leftValue = JavascriptInt64Number::UnsafeFromVar(aLeft)->GetValue();
  784. unsigned __int64 rightValue = JavascriptInt64Number::FromVar(aRight)->GetValue();
  785. return ((unsigned __int64)leftValue == rightValue);
  786. }
  787. case TypeIds_Number:
  788. dblLeft = (double)JavascriptInt64Number::UnsafeFromVar(aLeft)->GetValue();
  789. dblRight = JavascriptNumber::GetValue(aRight);
  790. goto CommonNumber;
  791. }
  792. return FALSE;
  793. case TypeIds_UInt64Number:
  794. switch (rightType)
  795. {
  796. case TypeIds_Int64Number:
  797. {
  798. unsigned __int64 leftValue = JavascriptUInt64Number::UnsafeFromVar(aLeft)->GetValue();
  799. __int64 rightValue = JavascriptInt64Number::UnsafeFromVar(aRight)->GetValue();
  800. return (leftValue == (unsigned __int64)rightValue);
  801. }
  802. case TypeIds_UInt64Number:
  803. {
  804. unsigned __int64 leftValue = JavascriptUInt64Number::UnsafeFromVar(aLeft)->GetValue();
  805. unsigned __int64 rightValue = JavascriptInt64Number::FromVar(aRight)->GetValue();
  806. return leftValue == rightValue;
  807. }
  808. case TypeIds_Number:
  809. dblLeft = (double)JavascriptUInt64Number::UnsafeFromVar(aLeft)->GetValue();
  810. dblRight = JavascriptNumber::GetValue(aRight);
  811. goto CommonNumber;
  812. }
  813. return FALSE;
  814. case TypeIds_Number:
  815. switch (rightType)
  816. {
  817. case TypeIds_Integer:
  818. dblLeft = JavascriptNumber::GetValue(aLeft);
  819. dblRight = TaggedInt::ToDouble(aRight);
  820. goto CommonNumber;
  821. case TypeIds_Int64Number:
  822. dblLeft = JavascriptNumber::GetValue(aLeft);
  823. dblRight = (double)JavascriptInt64Number::FromVar(aRight)->GetValue();
  824. goto CommonNumber;
  825. case TypeIds_UInt64Number:
  826. dblLeft = JavascriptNumber::GetValue(aLeft);
  827. dblRight = (double)JavascriptUInt64Number::UnsafeFromVar(aRight)->GetValue();
  828. goto CommonNumber;
  829. case TypeIds_Number:
  830. dblLeft = JavascriptNumber::GetValue(aLeft);
  831. dblRight = JavascriptNumber::GetValue(aRight);
  832. CommonNumber:
  833. return FEqualDbl(dblLeft, dblRight);
  834. }
  835. return FALSE;
  836. case TypeIds_Boolean:
  837. switch (rightType)
  838. {
  839. case TypeIds_Boolean:
  840. return aLeft == aRight;
  841. }
  842. return FALSE;
  843. case TypeIds_Undefined:
  844. return rightType == TypeIds_Undefined;
  845. case TypeIds_Null:
  846. return rightType == TypeIds_Null;
  847. case TypeIds_Array:
  848. return (rightType == TypeIds_Array && aLeft == aRight);
  849. #if DBG
  850. case TypeIds_Symbol:
  851. if (rightType == TypeIds_Symbol)
  852. {
  853. const PropertyRecord* leftValue = JavascriptSymbol::UnsafeFromVar(aLeft)->GetValue();
  854. const PropertyRecord* rightValue = JavascriptSymbol::UnsafeFromVar(aRight)->GetValue();
  855. Assert(leftValue != rightValue);
  856. }
  857. break;
  858. #endif
  859. case TypeIds_GlobalObject:
  860. case TypeIds_HostDispatch:
  861. switch (rightType)
  862. {
  863. case TypeIds_HostDispatch:
  864. case TypeIds_GlobalObject:
  865. {
  866. BOOL result;
  867. if(RecyclableObject::UnsafeFromVar(aLeft)->StrictEquals(aRight, &result, requestContext))
  868. {
  869. return result;
  870. }
  871. return false;
  872. }
  873. }
  874. break;
  875. }
  876. if (RecyclableObject::FromVar(aLeft)->IsExternal())
  877. {
  878. BOOL result;
  879. if (RecyclableObject::FromVar(aLeft)->StrictEquals(aRight, &result, requestContext))
  880. {
  881. if (result)
  882. {
  883. return TRUE;
  884. }
  885. }
  886. }
  887. if (!TaggedNumber::Is(aRight) && RecyclableObject::FromVar(aRight)->IsExternal())
  888. {
  889. BOOL result;
  890. if (RecyclableObject::FromVar(aRight)->StrictEquals(aLeft, &result, requestContext))
  891. {
  892. if (result)
  893. {
  894. return TRUE;
  895. }
  896. }
  897. }
  898. return aLeft == aRight;
  899. JIT_HELPER_END(Op_StrictEqual);
  900. }
  901. BOOL JavascriptOperators::HasOwnProperty(
  902. Var instance,
  903. PropertyId propertyId,
  904. _In_ ScriptContext* requestContext,
  905. _In_opt_ PropertyString* propString)
  906. {
  907. if (TaggedNumber::Is(instance))
  908. {
  909. return FALSE;
  910. }
  911. RecyclableObject* object = RecyclableObject::UnsafeFromVar(instance);
  912. if (JavascriptProxy::Is(instance))
  913. {
  914. PropertyDescriptor desc;
  915. return GetOwnPropertyDescriptor(object, propertyId, requestContext, &desc);
  916. }
  917. // If we have a PropertyString, attempt to shortcut the lookup by using its caches
  918. if (propString != nullptr)
  919. {
  920. PropertyCacheOperationInfo info;
  921. if (propString->GetLdElemInlineCache()->PretendTryGetProperty(object->GetType(), &info))
  922. {
  923. switch (info.cacheType)
  924. {
  925. case CacheType_Local:
  926. Assert(object->HasOwnProperty(propertyId));
  927. return TRUE;
  928. case CacheType_Proto:
  929. Assert(!object->HasOwnProperty(propertyId));
  930. return FALSE;
  931. default:
  932. // We had a cache hit, but cache doesn't tell us if we have an own property
  933. break;
  934. }
  935. }
  936. if (propString->GetStElemInlineCache()->PretendTrySetProperty(object->GetType(), object->GetType(), &info))
  937. {
  938. switch (info.cacheType)
  939. {
  940. case CacheType_Local:
  941. Assert(object->HasOwnProperty(propertyId));
  942. return TRUE;
  943. case CacheType_LocalWithoutProperty:
  944. Assert(!object->HasOwnProperty(propertyId));
  945. return FALSE;
  946. default:
  947. // We had a cache hit, but cache doesn't tell us if we have an own property
  948. break;
  949. }
  950. }
  951. }
  952. return object && object->HasOwnProperty(propertyId);
  953. }
  954. BOOL JavascriptOperators::GetOwnAccessors(Var instance, PropertyId propertyId, Var* getter, Var* setter, ScriptContext * requestContext)
  955. {
  956. BOOL result;
  957. if (TaggedNumber::Is(instance))
  958. {
  959. result = false;
  960. }
  961. else
  962. {
  963. RecyclableObject* object = RecyclableObject::UnsafeFromVar(instance);
  964. result = object && object->GetAccessors(propertyId, getter, setter, requestContext);
  965. }
  966. return result;
  967. }
  968. JavascriptArray* JavascriptOperators::GetOwnPropertyNames(Var instance, ScriptContext *scriptContext)
  969. {
  970. RecyclableObject *object = RecyclableObject::FromVar(ToObject(instance, scriptContext));
  971. JavascriptProxy * proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  972. if (proxy)
  973. {
  974. return proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::GetOwnPropertyNamesKind, scriptContext);
  975. }
  976. return JavascriptObject::CreateOwnStringPropertiesHelper(object, scriptContext);
  977. }
  978. JavascriptArray* JavascriptOperators::GetOwnPropertySymbols(Var instance, ScriptContext *scriptContext)
  979. {
  980. RecyclableObject *object = RecyclableObject::FromVar(ToObject(instance, scriptContext));
  981. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_getOwnPropertySymbols);
  982. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  983. if (proxy)
  984. {
  985. return proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::GetOwnPropertySymbolKind, scriptContext);
  986. }
  987. return JavascriptObject::CreateOwnSymbolPropertiesHelper(object, scriptContext);
  988. }
  989. JavascriptArray* JavascriptOperators::GetOwnPropertyKeys(Var instance, ScriptContext* scriptContext)
  990. {
  991. RecyclableObject *object = RecyclableObject::FromVar(ToObject(instance, scriptContext));
  992. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  993. if (proxy)
  994. {
  995. return proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::KeysKind, scriptContext);
  996. }
  997. return JavascriptObject::CreateOwnStringSymbolPropertiesHelper(object, scriptContext);
  998. }
  999. JavascriptArray* JavascriptOperators::GetOwnEnumerablePropertyNames(RecyclableObject* object, ScriptContext* scriptContext)
  1000. {
  1001. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(object);
  1002. if (proxy)
  1003. {
  1004. JavascriptArray* proxyResult = proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::GetOwnPropertyNamesKind, scriptContext);
  1005. JavascriptArray* proxyResultToReturn = scriptContext->GetLibrary()->CreateArray(0);
  1006. // filter enumerable keys
  1007. uint32 resultLength = proxyResult->GetLength();
  1008. Var element;
  1009. const Js::PropertyRecord *propertyRecord = nullptr;
  1010. uint32 index = 0;
  1011. for (uint32 i = 0; i < resultLength; i++)
  1012. {
  1013. element = proxyResult->DirectGetItem(i);
  1014. Assert(!JavascriptSymbol::Is(element));
  1015. PropertyDescriptor propertyDescriptor;
  1016. JavascriptConversion::ToPropertyKey(element, scriptContext, &propertyRecord, nullptr);
  1017. if (JavascriptOperators::GetOwnPropertyDescriptor(object, propertyRecord->GetPropertyId(), scriptContext, &propertyDescriptor))
  1018. {
  1019. if (propertyDescriptor.IsEnumerable())
  1020. {
  1021. proxyResultToReturn->DirectSetItemAt(index++, CrossSite::MarshalVar(scriptContext, element));
  1022. }
  1023. }
  1024. }
  1025. return proxyResultToReturn;
  1026. }
  1027. return JavascriptObject::CreateOwnEnumerableStringPropertiesHelper(object, scriptContext);
  1028. }
  1029. JavascriptArray* JavascriptOperators::GetOwnEnumerablePropertyNamesSymbols(RecyclableObject* object, ScriptContext* scriptContext)
  1030. {
  1031. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(object);
  1032. if (proxy)
  1033. {
  1034. return proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::KeysKind, scriptContext);
  1035. }
  1036. return JavascriptObject::CreateOwnEnumerableStringSymbolPropertiesHelper(object, scriptContext);
  1037. }
  1038. BOOL JavascriptOperators::GetOwnProperty(Var instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo * propertyValueInfo)
  1039. {
  1040. BOOL result;
  1041. if (TaggedNumber::Is(instance))
  1042. {
  1043. result = false;
  1044. }
  1045. else
  1046. {
  1047. RecyclableObject* object = RecyclableObject::FromVar(instance);
  1048. result = object && object->GetProperty(object, propertyId, value, propertyValueInfo, requestContext);
  1049. if (propertyValueInfo && result)
  1050. {
  1051. // 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
  1052. CacheOperators::CachePropertyRead(instance, object, false /* isRoot */, propertyId, false /* isMissing */, propertyValueInfo, requestContext);
  1053. }
  1054. }
  1055. return result;
  1056. }
  1057. BOOL JavascriptOperators::GetOwnPropertyDescriptor(RecyclableObject* obj, JavascriptString* propertyKey, ScriptContext* scriptContext, PropertyDescriptor* propertyDescriptor)
  1058. {
  1059. return JavascriptOperators::GetOwnPropertyDescriptor(obj, JavascriptOperators::GetPropertyId(propertyKey, scriptContext), scriptContext, propertyDescriptor);
  1060. }
  1061. // ES5's [[GetOwnProperty]].
  1062. // Return value:
  1063. // FALSE means "undefined" PD.
  1064. // TRUE means success. The propertyDescriptor parameter gets the descriptor.
  1065. //
  1066. BOOL JavascriptOperators::GetOwnPropertyDescriptor(RecyclableObject* obj, PropertyId propertyId, ScriptContext* scriptContext, PropertyDescriptor* propertyDescriptor)
  1067. {
  1068. Assert(obj);
  1069. Assert(scriptContext);
  1070. Assert(propertyDescriptor);
  1071. if (JavascriptProxy::Is(obj))
  1072. {
  1073. return JavascriptProxy::GetOwnPropertyDescriptor(obj, propertyId, scriptContext, propertyDescriptor);
  1074. }
  1075. Var getter, setter;
  1076. if (false == JavascriptOperators::GetOwnAccessors(obj, propertyId, &getter, &setter, scriptContext))
  1077. {
  1078. Var value = nullptr;
  1079. if (false == JavascriptOperators::GetOwnProperty(obj, propertyId, &value, scriptContext, nullptr))
  1080. {
  1081. return FALSE;
  1082. }
  1083. if (nullptr != value)
  1084. {
  1085. propertyDescriptor->SetValue(value);
  1086. }
  1087. //CONSIDER : Its expensive to query for each flag from type system. Combine this with the GetOwnProperty to get all the flags
  1088. //at once. This will require a new API from type system and override in all the types which overrides IsEnumerable etc.
  1089. //Currently there is no performance tuning for ES5. This should be ok.
  1090. propertyDescriptor->SetWritable(FALSE != obj->IsWritable(propertyId));
  1091. }
  1092. else
  1093. {
  1094. if (nullptr == getter)
  1095. {
  1096. getter = scriptContext->GetLibrary()->GetUndefined();
  1097. }
  1098. propertyDescriptor->SetGetter(getter);
  1099. if (nullptr == setter)
  1100. {
  1101. setter = scriptContext->GetLibrary()->GetUndefined();
  1102. }
  1103. propertyDescriptor->SetSetter(setter);
  1104. }
  1105. propertyDescriptor->SetConfigurable(FALSE != obj->IsConfigurable(propertyId));
  1106. propertyDescriptor->SetEnumerable(FALSE != obj->IsEnumerable(propertyId));
  1107. return TRUE;
  1108. }
  1109. inline RecyclableObject* JavascriptOperators::GetPrototypeNoTrap(RecyclableObject* instance)
  1110. {
  1111. Type* type = instance->GetType();
  1112. if (type->HasSpecialPrototype())
  1113. {
  1114. if (type->GetTypeId() == TypeIds_Proxy)
  1115. {
  1116. // get back null
  1117. Assert(type->GetPrototype() == instance->GetScriptContext()->GetLibrary()->GetNull());
  1118. return type->GetPrototype();
  1119. }
  1120. else
  1121. {
  1122. return instance->GetPrototypeSpecial();
  1123. }
  1124. }
  1125. return type->GetPrototype();
  1126. }
  1127. BOOL JavascriptOperators::IsRemoteArray(RecyclableObject* instance)
  1128. {
  1129. TypeId remoteTypeId = TypeIds_Limit;
  1130. return (JavascriptOperators::GetRemoteTypeId(instance, &remoteTypeId) &&
  1131. DynamicObject::IsAnyArrayTypeId(remoteTypeId));
  1132. }
  1133. bool JavascriptOperators::IsArray(_In_ JavascriptProxy * instance)
  1134. {
  1135. // If it is a proxy, follow to the end of the proxy chain before checking if it is an array again.
  1136. JavascriptProxy * proxy = instance;
  1137. while (true)
  1138. {
  1139. RecyclableObject * targetInstance = proxy->GetTarget();
  1140. proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(targetInstance);
  1141. if (proxy == nullptr)
  1142. {
  1143. return DynamicObject::IsAnyArray(targetInstance) || IsRemoteArray(targetInstance);
  1144. }
  1145. }
  1146. }
  1147. bool JavascriptOperators::IsArray(_In_ RecyclableObject* instance)
  1148. {
  1149. if (DynamicObject::IsAnyArray(instance))
  1150. {
  1151. return TRUE;
  1152. }
  1153. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  1154. if (proxy)
  1155. {
  1156. return IsArray(proxy);
  1157. }
  1158. return IsRemoteArray(instance);
  1159. }
  1160. bool JavascriptOperators::IsArray(_In_ Var instanceVar)
  1161. {
  1162. RecyclableObject* instanceObj = TryFromVar<RecyclableObject>(instanceVar);
  1163. return instanceObj && IsArray(instanceObj);
  1164. }
  1165. bool JavascriptOperators::IsConstructor(_In_ JavascriptProxy * instance)
  1166. {
  1167. // If it is a proxy, follow to the end of the proxy chain before checking if it is a constructor again.
  1168. JavascriptProxy * proxy = instance;
  1169. while (true)
  1170. {
  1171. RecyclableObject* targetInstance = proxy->GetTarget();
  1172. proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(targetInstance);
  1173. if (proxy == nullptr)
  1174. {
  1175. JavascriptFunction* function = JavascriptOperators::TryFromVar<JavascriptFunction>(targetInstance);
  1176. return function && function->IsConstructor();
  1177. }
  1178. }
  1179. }
  1180. bool JavascriptOperators::IsConstructor(_In_ RecyclableObject* instanceObj)
  1181. {
  1182. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instanceObj);
  1183. if (proxy)
  1184. {
  1185. return IsConstructor(proxy);
  1186. }
  1187. JavascriptFunction* function = JavascriptOperators::TryFromVar<JavascriptFunction>(instanceObj);
  1188. return function && function->IsConstructor();
  1189. }
  1190. bool JavascriptOperators::IsConstructor(_In_ Var instanceVar)
  1191. {
  1192. RecyclableObject* instanceObj = TryFromVar<RecyclableObject>(instanceVar);
  1193. return instanceObj && IsConstructor(instanceObj);
  1194. }
  1195. BOOL JavascriptOperators::IsConcatSpreadable(Var instanceVar)
  1196. {
  1197. // an object is spreadable under two condition, either it is a JsArray
  1198. // or you define an isconcatSpreadable flag on it.
  1199. if (!JavascriptOperators::IsObject(instanceVar))
  1200. {
  1201. return false;
  1202. }
  1203. RecyclableObject* instance = RecyclableObject::UnsafeFromVar(instanceVar);
  1204. ScriptContext* scriptContext = instance->GetScriptContext();
  1205. if (!PHASE_OFF1(IsConcatSpreadableCachePhase))
  1206. {
  1207. BOOL retVal = FALSE;
  1208. Type *instanceType = instance->GetType();
  1209. IsConcatSpreadableCache *isConcatSpreadableCache = scriptContext->GetThreadContext()->GetIsConcatSpreadableCache();
  1210. if (isConcatSpreadableCache->TryGetIsConcatSpreadable(instanceType, &retVal))
  1211. {
  1212. OUTPUT_TRACE(Phase::IsConcatSpreadableCachePhase, _u("IsConcatSpreadableCache hit: %p\n"), instanceType);
  1213. return retVal;
  1214. }
  1215. Var spreadable = nullptr;
  1216. BOOL hasUserDefinedSpreadable = JavascriptOperators::GetProperty(instance, instance, PropertyIds::_symbolIsConcatSpreadable, &spreadable, scriptContext);
  1217. if (hasUserDefinedSpreadable && spreadable != scriptContext->GetLibrary()->GetUndefined())
  1218. {
  1219. return JavascriptConversion::ToBoolean(spreadable, scriptContext);
  1220. }
  1221. retVal = JavascriptOperators::IsArray(instance);
  1222. if (!hasUserDefinedSpreadable)
  1223. {
  1224. OUTPUT_TRACE(Phase::IsConcatSpreadableCachePhase, _u("IsConcatSpreadableCache saved: %p\n"), instanceType);
  1225. isConcatSpreadableCache->CacheIsConcatSpreadable(instanceType, retVal);
  1226. }
  1227. return retVal;
  1228. }
  1229. Var spreadable = JavascriptOperators::GetProperty(instance, PropertyIds::_symbolIsConcatSpreadable, scriptContext);
  1230. if (spreadable != scriptContext->GetLibrary()->GetUndefined())
  1231. {
  1232. return JavascriptConversion::ToBoolean(spreadable, scriptContext);
  1233. }
  1234. return JavascriptOperators::IsArray(instance);
  1235. }
  1236. bool JavascriptOperators::IsConstructorSuperCall(Arguments args)
  1237. {
  1238. Var newTarget = args.GetNewTarget();
  1239. return args.IsNewCall() && newTarget != nullptr
  1240. && !JavascriptOperators::IsUndefined(newTarget);
  1241. }
  1242. bool JavascriptOperators::GetAndAssertIsConstructorSuperCall(Arguments args)
  1243. {
  1244. bool isCtorSuperCall = JavascriptOperators::IsConstructorSuperCall(args);
  1245. Assert(isCtorSuperCall || !args.IsNewCall()
  1246. || args[0] == nullptr || JavascriptOperators::GetTypeId(args[0]) == TypeIds_HostDispatch);
  1247. return isCtorSuperCall;
  1248. }
  1249. Var JavascriptOperators::OP_LdCustomSpreadIteratorList(Var aRight, ScriptContext* scriptContext)
  1250. {
  1251. JIT_HELPER_REENTRANT_HEADER(Op_ToSpreadedFunctionArgument);
  1252. #if ENABLE_COPYONACCESS_ARRAY
  1253. // We know we're going to read from this array. Do the conversion before we try to perform checks on the head segment.
  1254. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray(aRight);
  1255. #endif
  1256. #ifdef ENABLE_JS_BUILTINS
  1257. scriptContext->GetLibrary()->EnsureBuiltInEngineIsReady();
  1258. #endif
  1259. RecyclableObject* function = GetIteratorFunction(aRight, scriptContext);
  1260. JavascriptMethod method = function->GetEntryPoint();
  1261. if (((JavascriptArray::Is(aRight) &&
  1262. (
  1263. JavascriptLibrary::IsDefaultArrayValuesFunction(function, scriptContext)
  1264. // Verify that the head segment of the array covers all elements with no gaps.
  1265. // Accessing an element on the prototype could have side-effects that would invalidate the optimization.
  1266. && JavascriptArray::UnsafeFromVar(aRight)->GetHead()->next == nullptr
  1267. && JavascriptArray::UnsafeFromVar(aRight)->GetHead()->left == 0
  1268. && JavascriptArray::UnsafeFromVar(aRight)->GetHead()->length == JavascriptArray::FromVar(aRight)->GetLength()
  1269. && JavascriptArray::UnsafeFromVar(aRight)->HasNoMissingValues()
  1270. && !JavascriptArray::UnsafeFromVar(aRight)->IsCrossSiteObject()
  1271. )) ||
  1272. (TypedArrayBase::Is(aRight) && method == TypedArrayBase::EntryInfo::Values.GetOriginalEntryPoint()))
  1273. // We can't optimize away the iterator if the array iterator prototype is user defined.
  1274. && !JavascriptLibrary::ArrayIteratorPrototypeHasUserDefinedNext(scriptContext))
  1275. {
  1276. return RecyclerNew(scriptContext->GetRecycler(), SpreadArgument, aRight, true /*useDirectCall*/, scriptContext->GetLibrary()->GetSpreadArgumentType());
  1277. }
  1278. ThreadContext *threadContext = scriptContext->GetThreadContext();
  1279. Var iteratorVar =
  1280. threadContext->ExecuteImplicitCall(function, ImplicitCall_Accessor, [=]() -> Var
  1281. {
  1282. return CALL_FUNCTION(threadContext, function, CallInfo(Js::CallFlags_Value, 1), aRight);
  1283. });
  1284. if (!JavascriptOperators::IsObject(iteratorVar))
  1285. {
  1286. if (!threadContext->RecordImplicitException())
  1287. {
  1288. return scriptContext->GetLibrary()->GetUndefined();
  1289. }
  1290. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  1291. }
  1292. return RecyclerNew(scriptContext->GetRecycler(), SpreadArgument, iteratorVar, false /*useDirectCall*/, scriptContext->GetLibrary()->GetSpreadArgumentType());
  1293. JIT_HELPER_END(Op_ToSpreadedFunctionArgument);
  1294. }
  1295. BOOL JavascriptOperators::IsPropertyUnscopable(Var instanceVar, JavascriptString *propertyString)
  1296. {
  1297. // This never gets called.
  1298. Throw::InternalError();
  1299. }
  1300. BOOL JavascriptOperators::IsPropertyUnscopable(Var instanceVar, PropertyId propertyId)
  1301. {
  1302. RecyclableObject* instance = RecyclableObject::FromVar(instanceVar);
  1303. ScriptContext * scriptContext = instance->GetScriptContext();
  1304. Var unscopables = JavascriptOperators::GetProperty(instance, PropertyIds::_symbolUnscopables, scriptContext);
  1305. if (JavascriptOperators::IsObject(unscopables))
  1306. {
  1307. DynamicObject *unscopablesList = DynamicObject::FromVar(unscopables);
  1308. Var value = nullptr;
  1309. //8.1.1.2.1.9.c If blocked is not undefined
  1310. if (JavascriptOperators::GetProperty(unscopablesList, propertyId, &value, scriptContext))
  1311. {
  1312. return JavascriptConversion::ToBoolean(value, scriptContext);
  1313. }
  1314. }
  1315. return false;
  1316. }
  1317. BOOL JavascriptOperators::HasProperty(RecyclableObject* instance, PropertyId propertyId)
  1318. {
  1319. while (!JavascriptOperators::IsNull(instance))
  1320. {
  1321. PropertyQueryFlags result = instance->HasPropertyQuery(propertyId, nullptr /*info*/);
  1322. if (result != PropertyQueryFlags::Property_NotFound)
  1323. {
  1324. return JavascriptConversion::PropertyQueryFlagsToBoolean(result); // return false if instance is typed array and HasPropertyQuery() returns PropertyQueryFlags::Property_Found_Undefined
  1325. }
  1326. instance = JavascriptOperators::GetPrototypeNoTrap(instance);
  1327. }
  1328. return false;
  1329. }
  1330. BOOL JavascriptOperators::HasPropertyUnscopables(RecyclableObject* instance, PropertyId propertyId)
  1331. {
  1332. return JavascriptOperators::HasProperty(instance, propertyId)
  1333. && !IsPropertyUnscopable(instance, propertyId);
  1334. }
  1335. BOOL JavascriptOperators::HasRootProperty(RecyclableObject* instance, PropertyId propertyId)
  1336. {
  1337. Assert(RootObjectBase::Is(instance));
  1338. RootObjectBase* rootObject = static_cast<RootObjectBase*>(instance);
  1339. if (rootObject->HasRootProperty(propertyId))
  1340. {
  1341. return true;
  1342. }
  1343. instance = instance->GetPrototype();
  1344. return HasProperty(instance, propertyId);
  1345. }
  1346. BOOL JavascriptOperators::HasProxyOrPrototypeInlineCacheProperty(RecyclableObject* instance, PropertyId propertyId)
  1347. {
  1348. TypeId typeId;
  1349. typeId = JavascriptOperators::GetTypeId(instance);
  1350. if (typeId == Js::TypeIds_Proxy)
  1351. {
  1352. // let's be more aggressive to disable inline prototype cache when proxy is presented in the prototypechain
  1353. return true;
  1354. }
  1355. do
  1356. {
  1357. instance = instance->GetPrototype();
  1358. typeId = JavascriptOperators::GetTypeId(instance);
  1359. if (typeId == Js::TypeIds_Proxy)
  1360. {
  1361. // let's be more aggressive to disable inline prototype cache when proxy is presented in the prototypechain
  1362. return true;
  1363. }
  1364. if (typeId == TypeIds_Null)
  1365. {
  1366. break;
  1367. }
  1368. /* We can rule out object with deferred type handler, because they would have expanded if they are in the cache */
  1369. if (!instance->HasDeferredTypeHandler() && instance->HasProperty(propertyId)) { return true; }
  1370. } while (typeId != TypeIds_Null);
  1371. return false;
  1372. }
  1373. BOOL JavascriptOperators::OP_HasProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  1374. {
  1375. JIT_HELPER_REENTRANT_HEADER(Op_HasProperty);
  1376. RecyclableObject* object = TaggedNumber::Is(instance) ?
  1377. scriptContext->GetLibrary()->GetNumberPrototype() :
  1378. RecyclableObject::FromVar(instance);
  1379. BOOL result = HasProperty(object, propertyId);
  1380. return result;
  1381. JIT_HELPER_END(Op_HasProperty);
  1382. }
  1383. BOOL JavascriptOperators::OP_HasOwnProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  1384. {
  1385. RecyclableObject* object = TaggedNumber::Is(instance) ?
  1386. scriptContext->GetLibrary()->GetNumberPrototype() :
  1387. RecyclableObject::FromVar(instance);
  1388. BOOL result = HasOwnProperty(object, propertyId, scriptContext, nullptr);
  1389. return result;
  1390. }
  1391. // CONSIDER: Have logic similar to HasOwnPropertyNoHostObjectForHeapEnum
  1392. BOOL JavascriptOperators::HasOwnPropertyNoHostObject(Var instance, PropertyId propertyId)
  1393. {
  1394. AssertMsg(!TaggedNumber::Is(instance), "HasOwnPropertyNoHostObject int passed");
  1395. RecyclableObject* object = RecyclableObject::FromVar(instance);
  1396. return object && object->HasOwnPropertyNoHostObject(propertyId);
  1397. }
  1398. // CONSIDER: Remove HasOwnPropertyNoHostObjectForHeapEnum and use GetOwnPropertyNoHostObjectForHeapEnum in its place by changing it
  1399. // 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.
  1400. BOOL JavascriptOperators::HasOwnPropertyNoHostObjectForHeapEnum(Var instance, PropertyId propertyId, ScriptContext* requestContext, Var& getter, Var& setter)
  1401. {
  1402. AssertMsg(!TaggedNumber::Is(instance), "HasOwnPropertyNoHostObjectForHeapEnum int passed");
  1403. RecyclableObject * object = RecyclableObject::FromVar(instance);
  1404. if (StaticType::Is(object->GetTypeId()))
  1405. {
  1406. return FALSE;
  1407. }
  1408. getter = setter = NULL;
  1409. DynamicObject* dynamicObject = DynamicObject::FromVar(instance);
  1410. Assert(dynamicObject->GetScriptContext()->IsHeapEnumInProgress());
  1411. if (dynamicObject->UseDynamicObjectForNoHostObjectAccess())
  1412. {
  1413. if (!dynamicObject->DynamicObject::GetAccessors(propertyId, &getter, &setter, requestContext))
  1414. {
  1415. Var value = nullptr;
  1416. if (!JavascriptConversion::PropertyQueryFlagsToBoolean(dynamicObject->DynamicObject::GetPropertyQuery(instance, propertyId, &value, NULL, requestContext)) ||
  1417. (requestContext->IsUndeclBlockVar(value) && (ActivationObject::Is(instance) || RootObjectBase::Is(instance))))
  1418. {
  1419. return FALSE;
  1420. }
  1421. }
  1422. }
  1423. else
  1424. {
  1425. if (!object->GetAccessors(propertyId, &getter, &setter, requestContext))
  1426. {
  1427. Var value = nullptr;
  1428. if (!object->GetProperty(instance, propertyId, &value, NULL, requestContext) ||
  1429. (requestContext->IsUndeclBlockVar(value) && (ActivationObject::Is(instance) || RootObjectBase::Is(instance))))
  1430. {
  1431. return FALSE;
  1432. }
  1433. }
  1434. }
  1435. return TRUE;
  1436. }
  1437. Var JavascriptOperators::GetOwnPropertyNoHostObjectForHeapEnum(Var instance, PropertyId propertyId, ScriptContext* requestContext, Var& getter, Var& setter)
  1438. {
  1439. AssertMsg(!TaggedNumber::Is(instance), "GetDataPropertyNoHostObject int passed");
  1440. Assert(HasOwnPropertyNoHostObjectForHeapEnum(instance, propertyId, requestContext, getter, setter) || getter || setter);
  1441. DynamicObject* dynamicObject = DynamicObject::FromVar(instance);
  1442. getter = setter = NULL;
  1443. if (NULL == dynamicObject)
  1444. {
  1445. return requestContext->GetLibrary()->GetUndefined();
  1446. }
  1447. Var returnVar = requestContext->GetLibrary()->GetUndefined();
  1448. BOOL result = FALSE;
  1449. if (dynamicObject->UseDynamicObjectForNoHostObjectAccess())
  1450. {
  1451. if (! dynamicObject->DynamicObject::GetAccessors(propertyId, &getter, &setter, requestContext))
  1452. {
  1453. result = JavascriptConversion::PropertyQueryFlagsToBoolean((dynamicObject->DynamicObject::GetPropertyQuery(instance, propertyId, &returnVar, NULL, requestContext)));
  1454. }
  1455. }
  1456. else
  1457. {
  1458. if (! dynamicObject->GetAccessors(propertyId, &getter, &setter, requestContext))
  1459. {
  1460. result = dynamicObject->GetProperty(instance, propertyId, &returnVar, NULL, requestContext);
  1461. }
  1462. }
  1463. if (result)
  1464. {
  1465. return returnVar;
  1466. }
  1467. return requestContext->GetLibrary()->GetUndefined();
  1468. }
  1469. BOOL JavascriptOperators::OP_HasOwnPropScoped(Var scope, PropertyId propertyId, Var defaultInstance, ScriptContext* scriptContext)
  1470. {
  1471. AssertMsg(scope == scriptContext->GetLibrary()->GetNull() || JavascriptArray::Is(scope),
  1472. "Invalid scope chain pointer passed - should be null or an array");
  1473. JavascriptArray* arrScope = JavascriptOperators::TryFromVar<JavascriptArray>(scope);
  1474. if (arrScope)
  1475. {
  1476. Var instance = arrScope->DirectGetItem(0);
  1477. return JavascriptOperators::OP_HasOwnProperty(instance, propertyId, scriptContext);
  1478. }
  1479. return JavascriptOperators::OP_HasOwnProperty(defaultInstance, propertyId, scriptContext);
  1480. }
  1481. BOOL JavascriptOperators::GetPropertyUnscopable(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1482. {
  1483. return GetProperty_Internal<true>(instance, propertyObject, false, propertyId, value, requestContext, info);
  1484. }
  1485. BOOL JavascriptOperators::GetProperty(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1486. {
  1487. return GetProperty_Internal<false>(instance, propertyObject, false, propertyId, value, requestContext, info);
  1488. }
  1489. BOOL JavascriptOperators::GetRootProperty(Var instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1490. {
  1491. return GetProperty_Internal<false>(instance, RecyclableObject::FromVar(instance), true, propertyId, value, requestContext, info);
  1492. }
  1493. BOOL JavascriptOperators::GetProperty_InternalSimple(Var instance, RecyclableObject* object, PropertyId propertyId, _Outptr_result_maybenull_ Var* value, ScriptContext* requestContext)
  1494. {
  1495. BOOL foundProperty = FALSE;
  1496. Assert(value != nullptr);
  1497. while (!JavascriptOperators::IsNull(object))
  1498. {
  1499. PropertyQueryFlags result = object->GetPropertyQuery(instance, propertyId, value, nullptr, requestContext);
  1500. if (result != PropertyQueryFlags::Property_NotFound)
  1501. {
  1502. foundProperty = JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  1503. break;
  1504. }
  1505. if (object->SkipsPrototype())
  1506. {
  1507. break;
  1508. }
  1509. object = JavascriptOperators::GetPrototypeNoTrap(object);
  1510. }
  1511. if (!foundProperty)
  1512. {
  1513. *value = requestContext->GetMissingPropertyResult();
  1514. }
  1515. return foundProperty;
  1516. }
  1517. template <bool unscopables>
  1518. BOOL JavascriptOperators::GetProperty_Internal(Var instance, RecyclableObject* propertyObject, const bool isRoot, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1519. {
  1520. if (TaggedNumber::Is(instance))
  1521. {
  1522. PropertyValueInfo::ClearCacheInfo(info);
  1523. }
  1524. RecyclableObject* object = propertyObject;
  1525. BOOL foundProperty = FALSE;
  1526. if (isRoot)
  1527. {
  1528. Assert(RootObjectBase::Is(object));
  1529. RootObjectBase* rootObject = static_cast<RootObjectBase*>(object);
  1530. foundProperty = rootObject->GetRootProperty(instance, propertyId, value, info, requestContext);
  1531. }
  1532. while (!foundProperty && !JavascriptOperators::IsNull(object))
  1533. {
  1534. if (unscopables && IsPropertyUnscopable(object, propertyId))
  1535. {
  1536. break;
  1537. }
  1538. else
  1539. {
  1540. PropertyQueryFlags result = object->GetPropertyQuery(instance, propertyId, value, info, requestContext);
  1541. if (result != PropertyQueryFlags::Property_NotFound)
  1542. {
  1543. foundProperty = JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  1544. break;
  1545. }
  1546. }
  1547. if (object->SkipsPrototype())
  1548. {
  1549. break;
  1550. }
  1551. object = JavascriptOperators::GetPrototypeNoTrap(object);
  1552. }
  1553. if (foundProperty)
  1554. {
  1555. #if ENABLE_FIXED_FIELDS && DBG
  1556. if (DynamicObject::Is(object))
  1557. {
  1558. DynamicObject* dynamicObject = (DynamicObject*)object;
  1559. DynamicTypeHandler* dynamicTypeHandler = dynamicObject->GetDynamicType()->GetTypeHandler();
  1560. Var property;
  1561. if (dynamicTypeHandler->CheckFixedProperty(requestContext->GetPropertyName(propertyId), &property, requestContext))
  1562. {
  1563. bool skipAssert = false;
  1564. if (value != nullptr && Js::RecyclableObject::Is(property))
  1565. {
  1566. Js::RecyclableObject* pObject = Js::RecyclableObject::FromVar(property);
  1567. Js::RecyclableObject* pValue = Js::RecyclableObject::FromVar(*value);
  1568. if (pValue->GetScriptContext() != pObject->GetScriptContext())
  1569. {
  1570. // value was marshaled. skip check
  1571. skipAssert = true;
  1572. }
  1573. }
  1574. Assert(skipAssert || value == nullptr || *value == property);
  1575. }
  1576. }
  1577. #endif
  1578. // Don't cache the information if the value is undecl block var
  1579. // REVIEW: We might want to only check this if we need to (For LdRootFld or ScopedLdFld)
  1580. // Also we might want to throw here instead of checking it again in the caller
  1581. if (value && !requestContext->IsUndeclBlockVar(*value) && !UnscopablesWrapperObject::Is(object))
  1582. {
  1583. CacheOperators::CachePropertyRead(propertyObject, object, isRoot, propertyId, false, info, requestContext);
  1584. }
  1585. #ifdef TELEMETRY_JSO
  1586. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  1587. {
  1588. requestContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(instance, propertyId, value, /*successful: */true);
  1589. }
  1590. #endif
  1591. return TRUE;
  1592. }
  1593. else
  1594. {
  1595. #ifdef MISSING_PROPERTY_STATS
  1596. if (PHASE_STATS1(MissingPropertyCachePhase))
  1597. {
  1598. requestContext->RecordMissingPropertyMiss();
  1599. }
  1600. #endif
  1601. if (PHASE_TRACE1(MissingPropertyCachePhase))
  1602. {
  1603. Output::Print(_u("MissingPropertyCaching: Missing property %d on slow path.\n"), propertyId);
  1604. }
  1605. TryCacheMissingProperty(instance, propertyObject, isRoot, propertyId, requestContext, info);
  1606. #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.
  1607. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  1608. {
  1609. if (info && info->AllowResizingPolymorphicInlineCache()) // If in interpreted mode, not JIT.
  1610. {
  1611. requestContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(instance, propertyId, nullptr);
  1612. }
  1613. }
  1614. #endif
  1615. *value = requestContext->GetMissingPropertyResult();
  1616. return FALSE;
  1617. }
  1618. }
  1619. // If the given instance is a type where we can cache missing properties, then cache that the given property ID is missing.
  1620. // cacheInstance is used as startingObject in CachePropertyRead, and might be instance's proto if we are fetching a super property (see #3064).
  1621. void JavascriptOperators::TryCacheMissingProperty(Var instance, Var cacheInstance, bool isRoot, PropertyId propertyId, ScriptContext* requestContext, _Inout_ PropertyValueInfo * info)
  1622. {
  1623. // Here, any well-behaved subclasses of DynamicObject can opt in to getting included in the missing property cache.
  1624. // For now, we only include basic objects and arrays. CustomExternalObject in particular is problematic because in
  1625. // some cases it can add new properties without transitioning its type handler.
  1626. if (PHASE_OFF1(MissingPropertyCachePhase) || isRoot || !(DynamicObject::Is(instance) || DynamicObject::IsAnyArray(instance)))
  1627. {
  1628. return;
  1629. }
  1630. DynamicTypeHandler* handler = DynamicObject::UnsafeFromVar(instance)->GetDynamicType()->GetTypeHandler();
  1631. // Only cache missing property lookups for non-root field loads on objects that have PathTypeHandlers, because only these types have the right behavior
  1632. // when the missing property is later added. DictionaryTypeHandler's introduce the possibility that a stale TypePropertyCache entry with isMissing==true can
  1633. // 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
  1634. // override special symbols make it unnecessary to introduce an invalidation scheme to deal with DictionaryTypeHandler's.
  1635. if (!handler->IsPathTypeHandler())
  1636. {
  1637. return;
  1638. }
  1639. #ifdef MISSING_PROPERTY_STATS
  1640. if (PHASE_STATS1(MissingPropertyCachePhase))
  1641. {
  1642. requestContext->RecordMissingPropertyCacheAttempt();
  1643. }
  1644. #endif
  1645. if (PHASE_TRACE1(MissingPropertyCachePhase))
  1646. {
  1647. Output::Print(_u("MissingPropertyCache: Caching missing property for property %d.\n"), propertyId);
  1648. }
  1649. PropertyValueInfo::Set(info, requestContext->GetLibrary()->GetMissingPropertyHolder(), 0);
  1650. CacheOperators::CachePropertyRead(cacheInstance, requestContext->GetLibrary()->GetMissingPropertyHolder(), isRoot, propertyId, true /*isMissing*/, info, requestContext);
  1651. }
  1652. template<bool OutputExistence, typename PropertyKeyType> PropertyQueryFlags QueryGetOrHasProperty(
  1653. Var originalInstance, RecyclableObject* object, PropertyKeyType propertyKey, Var* value, PropertyValueInfo* info, ScriptContext* requestContext);
  1654. template<> PropertyQueryFlags QueryGetOrHasProperty<false /*OutputExistence*/, PropertyId /*PropertyKeyType*/>(
  1655. Var originalInstance, RecyclableObject* object, PropertyId propertyKey, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1656. {
  1657. return object->GetPropertyQuery(originalInstance, propertyKey, value, info, requestContext);
  1658. }
  1659. template<> PropertyQueryFlags QueryGetOrHasProperty<false /*OutputExistence*/, JavascriptString* /*PropertyKeyType*/>(
  1660. Var originalInstance, RecyclableObject* object, JavascriptString* propertyKey, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1661. {
  1662. return object->GetPropertyQuery(originalInstance, propertyKey, value, info, requestContext);
  1663. }
  1664. template<> PropertyQueryFlags QueryGetOrHasProperty<true /*OutputExistence*/, PropertyId /*PropertyKeyType*/>(
  1665. Var originalInstance, RecyclableObject* object, PropertyId propertyKey, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1666. {
  1667. PropertyQueryFlags result = object->HasPropertyQuery(propertyKey, info);
  1668. *value = JavascriptBoolean::ToVar(JavascriptConversion::PropertyQueryFlagsToBoolean(result), requestContext);
  1669. return result;
  1670. }
  1671. template<bool OutputExistence, typename PropertyKeyType>
  1672. BOOL JavascriptOperators::GetPropertyWPCache(Var instance, RecyclableObject* propertyObject, PropertyKeyType propertyKey, Var* value, ScriptContext* requestContext, _Inout_ PropertyValueInfo * info)
  1673. {
  1674. Assert(value);
  1675. RecyclableObject* object = propertyObject;
  1676. while (!JavascriptOperators::IsNull(object))
  1677. {
  1678. PropertyQueryFlags result = QueryGetOrHasProperty<OutputExistence>(instance, object, propertyKey, value, info, requestContext);
  1679. if (result != PropertyQueryFlags::Property_NotFound)
  1680. {
  1681. if (!UnscopablesWrapperObject::Is(object) && info->GetPropertyRecordUsageCache())
  1682. {
  1683. PropertyId propertyId = info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId();
  1684. CacheOperators::CachePropertyRead(instance, object, false, propertyId, false, info, requestContext);
  1685. }
  1686. return JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  1687. }
  1688. // SkipsPrototype refers only to the Get operation, not Has. Some objects like CustomExternalObject respond
  1689. // to HasPropertyQuery with info only about the object itself and GetPropertyQuery with info about its prototype chain.
  1690. // For consistency with the behavior of JavascriptOperators::HasProperty, don't skip prototypes when outputting existence.
  1691. if (!OutputExistence && object->SkipsPrototype())
  1692. {
  1693. break;
  1694. }
  1695. object = JavascriptOperators::GetPrototypeNoTrap(object);
  1696. }
  1697. if (info->GetPropertyRecordUsageCache())
  1698. {
  1699. TryCacheMissingProperty(instance, instance, false /*isRoot*/, info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId(), requestContext, info);
  1700. }
  1701. *value = OutputExistence
  1702. ? requestContext->GetLibrary()->GetFalse()
  1703. : requestContext->GetMissingPropertyResult();
  1704. return FALSE;
  1705. }
  1706. bool JavascriptOperators::GetPropertyObjectForElementAccess(
  1707. _In_ Var instance,
  1708. _In_ Var index,
  1709. _In_ ScriptContext* scriptContext,
  1710. _Out_ RecyclableObject** propertyObject,
  1711. _In_ rtErrors error)
  1712. {
  1713. BOOL isNullOrUndefined = !GetPropertyObject(instance, scriptContext, propertyObject);
  1714. Assert(*propertyObject == instance || TaggedNumber::Is(instance));
  1715. if (isNullOrUndefined)
  1716. {
  1717. if (!scriptContext->GetThreadContext()->RecordImplicitException())
  1718. {
  1719. return false;
  1720. }
  1721. JavascriptError::ThrowTypeError(scriptContext, error, GetPropertyDisplayNameForError(index, scriptContext));
  1722. }
  1723. return true;
  1724. }
  1725. bool JavascriptOperators::GetPropertyObjectForSetElementI(
  1726. _In_ Var instance,
  1727. _In_ Var index,
  1728. _In_ ScriptContext* scriptContext,
  1729. _Out_ RecyclableObject** propertyObject)
  1730. {
  1731. return GetPropertyObjectForElementAccess(instance, index, scriptContext, propertyObject, JSERR_Property_CannotSet_NullOrUndefined);
  1732. }
  1733. bool JavascriptOperators::GetPropertyObjectForGetElementI(
  1734. _In_ Var instance,
  1735. _In_ Var index,
  1736. _In_ ScriptContext* scriptContext,
  1737. _Out_ RecyclableObject** propertyObject)
  1738. {
  1739. return GetPropertyObjectForElementAccess(instance, index, scriptContext, propertyObject, JSERR_Property_CannotGet_NullOrUndefined);
  1740. }
  1741. BOOL JavascriptOperators::GetPropertyObject(Var instance, ScriptContext * scriptContext, RecyclableObject** propertyObject)
  1742. {
  1743. Assert(propertyObject);
  1744. if (TaggedNumber::Is(instance))
  1745. {
  1746. *propertyObject = scriptContext->GetLibrary()->GetNumberPrototype();
  1747. return TRUE;
  1748. }
  1749. RecyclableObject* object = RecyclableObject::UnsafeFromVar(instance);
  1750. *propertyObject = object;
  1751. if (JavascriptOperators::IsUndefinedOrNull(object))
  1752. {
  1753. return FALSE;
  1754. }
  1755. return TRUE;
  1756. }
  1757. #if DBG
  1758. BOOL JavascriptOperators::IsPropertyObject(RecyclableObject * instance)
  1759. {
  1760. TypeId typeId = JavascriptOperators::GetTypeId(instance);
  1761. return (typeId != TypeIds_Integer && typeId != TypeIds_Null && typeId != TypeIds_Undefined);
  1762. }
  1763. #endif
  1764. Var JavascriptOperators::OP_GetProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  1765. {
  1766. JIT_HELPER_REENTRANT_HEADER(Op_GetProperty);
  1767. RecyclableObject* object = nullptr;
  1768. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  1769. {
  1770. if (scriptContext->GetThreadContext()->RecordImplicitException())
  1771. {
  1772. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined, scriptContext->GetPropertyName(propertyId)->GetBuffer());
  1773. }
  1774. else
  1775. {
  1776. return scriptContext->GetLibrary()->GetUndefined();
  1777. }
  1778. }
  1779. Var result = JavascriptOperators::GetPropertyNoCache(instance, object, propertyId, scriptContext);
  1780. AssertMsg(result != nullptr, "result null in OP_GetProperty");
  1781. return result;
  1782. JIT_HELPER_END(Op_GetProperty);
  1783. }
  1784. Var JavascriptOperators::OP_GetRootProperty(Var instance, PropertyId propertyId, PropertyValueInfo * info, ScriptContext* scriptContext)
  1785. {
  1786. AssertMsg(RootObjectBase::Is(instance), "Root must be an object!");
  1787. Var value = nullptr;
  1788. if (JavascriptOperators::GetRootProperty(RecyclableObject::FromVar(instance), propertyId, &value, scriptContext, info))
  1789. {
  1790. if (scriptContext->IsUndeclBlockVar(value) && scriptContext->GetThreadContext()->RecordImplicitException())
  1791. {
  1792. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  1793. }
  1794. return value;
  1795. }
  1796. const char16* propertyName = scriptContext->GetPropertyName(propertyId)->GetBuffer();
  1797. JavascriptFunction * caller = nullptr;
  1798. if (JavascriptStackWalker::GetCaller(&caller, scriptContext))
  1799. {
  1800. FunctionBody * callerBody = caller->GetFunctionBody();
  1801. if (callerBody && callerBody->GetUtf8SourceInfo()->GetIsXDomain())
  1802. {
  1803. propertyName = nullptr;
  1804. }
  1805. }
  1806. // Don't error if we disabled implicit calls
  1807. if (scriptContext->GetThreadContext()->RecordImplicitException())
  1808. {
  1809. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UndefVariable, propertyName);
  1810. }
  1811. return scriptContext->GetMissingPropertyResult();
  1812. }
  1813. Var JavascriptOperators::OP_GetThisScoped(FrameDisplay *pScope, Var defaultInstance, ScriptContext* scriptContext)
  1814. {
  1815. // NOTE: If changes are made to this logic be sure to update the debuggers as well
  1816. int length = pScope->GetLength();
  1817. for (int i = 0; i < length; i += 1)
  1818. {
  1819. Var value = nullptr;
  1820. RecyclableObject *obj = RecyclableObject::FromVar(pScope->GetItem(i));
  1821. if (JavascriptOperators::GetProperty(obj, Js::PropertyIds::_this, &value, scriptContext))
  1822. {
  1823. return value;
  1824. }
  1825. }
  1826. return defaultInstance;
  1827. }
  1828. Var JavascriptOperators::OP_UnwrapWithObj(Var aValue)
  1829. {
  1830. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_UnwrapWithObj);
  1831. return RecyclableObject::FromVar(aValue)->GetThisObjectOrUnWrap();
  1832. JIT_HELPER_END(Op_UnwrapWithObj);
  1833. }
  1834. Var JavascriptOperators::OP_GetInstanceScoped(FrameDisplay *pScope, PropertyId propertyId, Var rootObject, Var* thisVar, ScriptContext* scriptContext)
  1835. {
  1836. JIT_HELPER_REENTRANT_HEADER(Op_GetInstanceScoped);
  1837. // Similar to GetPropertyScoped, but instead of returning the property value, we return the instance that
  1838. // owns it, or the global object if no instance is found.
  1839. int i;
  1840. int length = pScope->GetLength();
  1841. for (i = 0; i < length; i++)
  1842. {
  1843. RecyclableObject *obj = (RecyclableObject*)pScope->GetItem(i);
  1844. if (JavascriptOperators::HasProperty(obj, propertyId))
  1845. {
  1846. // HasProperty will call UnscopablesWrapperObject's HasProperty which will do the filtering
  1847. // All we have to do here is unwrap the object hence the api call
  1848. *thisVar = obj->GetThisObjectOrUnWrap();
  1849. return *thisVar;
  1850. }
  1851. }
  1852. *thisVar = scriptContext->GetLibrary()->GetUndefined();
  1853. if (rootObject != scriptContext->GetGlobalObject())
  1854. {
  1855. if (JavascriptOperators::OP_HasProperty(rootObject, propertyId, scriptContext))
  1856. {
  1857. return rootObject;
  1858. }
  1859. }
  1860. return scriptContext->GetGlobalObject();
  1861. JIT_HELPER_END(Op_GetInstanceScoped);
  1862. }
  1863. Var JavascriptOperators::GetPropertyReference(RecyclableObject *instance, PropertyId propertyId, ScriptContext* requestContext)
  1864. {
  1865. Var value = nullptr;
  1866. PropertyValueInfo info;
  1867. if (JavascriptOperators::GetPropertyReference(instance, propertyId, &value, requestContext, &info))
  1868. {
  1869. Assert(value != nullptr);
  1870. return value;
  1871. }
  1872. return requestContext->GetMissingPropertyResult();
  1873. }
  1874. BOOL JavascriptOperators::GetPropertyReference(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1875. {
  1876. return GetPropertyReference_Internal(instance, propertyObject, false, propertyId, value, requestContext, info);
  1877. }
  1878. BOOL JavascriptOperators::GetRootPropertyReference(RecyclableObject* instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1879. {
  1880. return GetPropertyReference_Internal(instance, instance, true, propertyId, value, requestContext, info);
  1881. }
  1882. BOOL JavascriptOperators::PropertyReferenceWalkUnscopable(Var instance, RecyclableObject** propertyObject, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1883. {
  1884. return PropertyReferenceWalk_Impl<true>(instance, propertyObject, propertyId, value, info, requestContext);
  1885. }
  1886. BOOL JavascriptOperators::PropertyReferenceWalk(Var instance, RecyclableObject** propertyObject, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1887. {
  1888. return PropertyReferenceWalk_Impl<false>(instance, propertyObject, propertyId, value, info, requestContext);
  1889. }
  1890. template <bool unscopables>
  1891. BOOL JavascriptOperators::PropertyReferenceWalk_Impl(Var instance, RecyclableObject** propertyObject, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1892. {
  1893. BOOL foundProperty = false;
  1894. RecyclableObject* object = *propertyObject;
  1895. while (!foundProperty && !JavascriptOperators::IsNull(object))
  1896. {
  1897. if (unscopables && JavascriptOperators::IsPropertyUnscopable(object, propertyId))
  1898. {
  1899. break;
  1900. }
  1901. else
  1902. {
  1903. PropertyQueryFlags result = object->GetPropertyReferenceQuery(instance, propertyId, value, info, requestContext);
  1904. if (result != PropertyQueryFlags::Property_NotFound)
  1905. {
  1906. foundProperty = JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  1907. break;
  1908. }
  1909. }
  1910. if (object->SkipsPrototype())
  1911. {
  1912. break; // will return false
  1913. }
  1914. object = JavascriptOperators::GetPrototypeNoTrap(object);
  1915. }
  1916. *propertyObject = object;
  1917. return foundProperty;
  1918. }
  1919. BOOL JavascriptOperators::GetPropertyReference_Internal(Var instance, RecyclableObject* propertyObject, const bool isRoot, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1920. {
  1921. if (TaggedNumber::Is(instance))
  1922. {
  1923. PropertyValueInfo::ClearCacheInfo(info);
  1924. }
  1925. BOOL foundProperty = FALSE;
  1926. RecyclableObject* object = propertyObject;
  1927. if (isRoot)
  1928. {
  1929. foundProperty = RootObjectBase::FromVar(object)->GetRootPropertyReference(instance, propertyId, value, info, requestContext);
  1930. }
  1931. if (!foundProperty)
  1932. {
  1933. foundProperty = PropertyReferenceWalk(instance, &object, propertyId, value, info, requestContext);
  1934. }
  1935. if (!foundProperty)
  1936. {
  1937. #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.
  1938. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  1939. {
  1940. if (info && info->AllowResizingPolymorphicInlineCache()) // If in interpreted mode, not JIT.
  1941. {
  1942. requestContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(instance, propertyId, nullptr);
  1943. }
  1944. }
  1945. #endif
  1946. *value = requestContext->GetMissingPropertyResult();
  1947. return foundProperty;
  1948. }
  1949. if (requestContext->IsUndeclBlockVar(*value))
  1950. {
  1951. JavascriptError::ThrowReferenceError(requestContext, JSERR_UseBeforeDeclaration);
  1952. }
  1953. #if ENABLE_FIXED_FIELDS && DBG
  1954. if (DynamicObject::Is(object))
  1955. {
  1956. DynamicObject* dynamicObject = (DynamicObject*)object;
  1957. DynamicTypeHandler* dynamicTypeHandler = dynamicObject->GetDynamicType()->GetTypeHandler();
  1958. Var property = nullptr;
  1959. if (dynamicTypeHandler->CheckFixedProperty(requestContext->GetPropertyName(propertyId), &property, requestContext))
  1960. {
  1961. Assert(value == nullptr || *value == property);
  1962. }
  1963. }
  1964. #endif
  1965. CacheOperators::CachePropertyRead(instance, object, isRoot, propertyId, false, info, requestContext);
  1966. return TRUE;
  1967. }
  1968. template <typename PropertyKeyType, bool unscopable>
  1969. DescriptorFlags JavascriptOperators::GetterSetter_Impl(RecyclableObject* instance, PropertyKeyType propertyKey, Var* setterValue, PropertyValueInfo* info, ScriptContext* scriptContext)
  1970. {
  1971. DescriptorFlags flags = None;
  1972. RecyclableObject* object = instance;
  1973. while (flags == None && !JavascriptOperators::IsNull(object))
  1974. {
  1975. if (unscopable && IsPropertyUnscopable(object, propertyKey))
  1976. {
  1977. break;
  1978. }
  1979. else
  1980. {
  1981. flags = object->GetSetter(propertyKey, setterValue, info, scriptContext);
  1982. if (flags != None)
  1983. {
  1984. break;
  1985. }
  1986. }
  1987. // CONSIDER: we should add SkipsPrototype support. DOM has no ES 5 concepts built in that aren't
  1988. // already part of our prototype objects which are chakra objects.
  1989. object = object->GetPrototype();
  1990. }
  1991. return flags;
  1992. }
  1993. DescriptorFlags JavascriptOperators::GetterSetterUnscopable(RecyclableObject* instance, PropertyId propertyId, Var* setterValue, PropertyValueInfo* info, ScriptContext* scriptContext)
  1994. {
  1995. return GetterSetter_Impl<PropertyId, true>(instance, propertyId, setterValue, info, scriptContext);
  1996. }
  1997. DescriptorFlags JavascriptOperators::GetterSetter(RecyclableObject* instance, PropertyId propertyId, Var* setterValue, PropertyValueInfo* info, ScriptContext* scriptContext)
  1998. {
  1999. return GetterSetter_Impl<PropertyId, false>(instance, propertyId, setterValue, info, scriptContext);
  2000. }
  2001. DescriptorFlags JavascriptOperators::GetterSetter(RecyclableObject* instance, JavascriptString * propertyName, Var* setterValue, PropertyValueInfo* info, ScriptContext* scriptContext)
  2002. {
  2003. return GetterSetter_Impl<JavascriptString*, false>(instance, propertyName, setterValue, info, scriptContext);
  2004. }
  2005. void JavascriptOperators::OP_InvalidateProtoCaches(PropertyId propertyId, ScriptContext *scriptContext)
  2006. {
  2007. JIT_HELPER_NOT_REENTRANT_HEADER(InvalidateProtoCaches, reentrancylock, scriptContext->GetThreadContext());
  2008. scriptContext->InvalidateProtoCaches(propertyId);
  2009. JIT_HELPER_END(InvalidateProtoCaches);
  2010. }
  2011. // Checks to see if any object in the prototype chain has a property descriptor for the given index
  2012. // that specifies either an accessor or a non-writable attribute.
  2013. // If TRUE, check flags for details.
  2014. BOOL JavascriptOperators::CheckPrototypesForAccessorOrNonWritableItem(RecyclableObject* instance, uint32 index,
  2015. Var* setterValue, DescriptorFlags *flags, ScriptContext* scriptContext, BOOL skipPrototypeCheck /* = FALSE */)
  2016. {
  2017. Assert(setterValue);
  2018. Assert(flags);
  2019. // Do a quick walk up the prototype chain to see if any of the prototypes has ever had ANY setter or non-writable property.
  2020. if (CheckIfObjectAndPrototypeChainHasOnlyWritableDataProperties(instance))
  2021. {
  2022. return FALSE;
  2023. }
  2024. RecyclableObject* object = instance;
  2025. while (!JavascriptOperators::IsNull(object))
  2026. {
  2027. *flags = object->GetItemSetter(index, setterValue, scriptContext);
  2028. if (*flags != None || skipPrototypeCheck)
  2029. {
  2030. break;
  2031. }
  2032. object = object->GetPrototype();
  2033. }
  2034. return ((*flags & Accessor) == Accessor) || ((*flags & Proxy) == Proxy) || ((*flags & Data) == Data && (*flags & Writable) == None);
  2035. }
  2036. BOOL JavascriptOperators::SetGlobalPropertyNoHost(char16 const * propertyName, charcount_t propertyLength, Var value, ScriptContext * scriptContext)
  2037. {
  2038. GlobalObject * globalObject = scriptContext->GetGlobalObject();
  2039. uint32 index;
  2040. PropertyRecord const * propertyRecord = nullptr;
  2041. IndexType indexType = GetIndexTypeFromString(propertyName, propertyLength, scriptContext, &index, &propertyRecord, true);
  2042. if (indexType == IndexType_Number)
  2043. {
  2044. return globalObject->DynamicObject::SetItem(index, value, PropertyOperation_None);
  2045. }
  2046. return globalObject->DynamicObject::SetProperty(propertyRecord->GetPropertyId(), value, PropertyOperation_None, NULL);
  2047. }
  2048. template<typename PropertyKeyType>
  2049. BOOL JavascriptOperators::SetPropertyWPCache(Var receiver, RecyclableObject* object, PropertyKeyType propertyKey, Var newValue, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags, _Inout_ PropertyValueInfo * info)
  2050. {
  2051. if (receiver)
  2052. {
  2053. AnalysisAssert(object);
  2054. Assert(!TaggedNumber::Is(receiver));
  2055. Var setterValueOrProxy = nullptr;
  2056. DescriptorFlags flags = None;
  2057. if (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(object, propertyKey, &setterValueOrProxy, &flags, info, requestContext))
  2058. {
  2059. if ((flags & Accessor) == Accessor)
  2060. {
  2061. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext))
  2062. {
  2063. return TRUE;
  2064. }
  2065. if (setterValueOrProxy)
  2066. {
  2067. if (!UnscopablesWrapperObject::Is(receiver) && info->GetPropertyRecordUsageCache() && !JavascriptOperators::IsUndefinedAccessor(setterValueOrProxy, requestContext))
  2068. {
  2069. CacheOperators::CachePropertyWrite(RecyclableObject::FromVar(receiver), false, object->GetType(), info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId(), info, requestContext);
  2070. }
  2071. receiver = (RecyclableObject::FromVar(receiver))->GetThisObjectOrUnWrap();
  2072. RecyclableObject* func = RecyclableObject::FromVar(setterValueOrProxy);
  2073. JavascriptOperators::CallSetter(func, receiver, newValue, requestContext);
  2074. }
  2075. return TRUE;
  2076. }
  2077. else if ((flags & Proxy) == Proxy)
  2078. {
  2079. Assert(JavascriptProxy::Is(setterValueOrProxy));
  2080. JavascriptProxy* proxy = JavascriptProxy::FromVar(setterValueOrProxy);
  2081. auto fn = [&](RecyclableObject* target) -> BOOL {
  2082. return JavascriptOperators::SetPropertyWPCache(receiver, target, propertyKey, newValue, requestContext, propertyOperationFlags, info);
  2083. };
  2084. if (info->GetPropertyRecordUsageCache())
  2085. {
  2086. PropertyValueInfo::SetNoCache(info, proxy);
  2087. PropertyValueInfo::DisablePrototypeCache(info, proxy);
  2088. }
  2089. return proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetPropertyWPCacheKind, propertyKey, newValue, requestContext, propertyOperationFlags);
  2090. }
  2091. else
  2092. {
  2093. Assert((flags & Data) == Data && (flags & Writable) == None);
  2094. requestContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  2095. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2096. return FALSE;
  2097. }
  2098. }
  2099. else if (!JavascriptOperators::IsObject(receiver))
  2100. {
  2101. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2102. return FALSE;
  2103. }
  2104. RecyclableObject* receiverObject = RecyclableObject::FromVar(receiver);
  2105. if (receiver != object)
  2106. {
  2107. // If the receiver object has the property and it is an accessor then return false
  2108. PropertyDescriptor existingDesc;
  2109. if (JavascriptOperators::GetOwnPropertyDescriptor(receiverObject, propertyKey, requestContext, &existingDesc)
  2110. && existingDesc.IsAccessorDescriptor())
  2111. {
  2112. return FALSE;
  2113. }
  2114. }
  2115. Type *typeWithoutProperty = object->GetType();
  2116. // in 9.1.9, step 5, we should return false if receiver is not object, and that will happen in default RecyclableObject operation anyhow.
  2117. if (receiverObject->SetProperty(propertyKey, newValue, propertyOperationFlags, info))
  2118. {
  2119. if (!JavascriptProxy::Is(receiver) && info->GetPropertyRecordUsageCache() && info->GetFlags() != InlineCacheSetterFlag && !object->IsExternal())
  2120. {
  2121. CacheOperators::CachePropertyWrite(RecyclableObject::FromVar(receiver), false, typeWithoutProperty, info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId(), info, requestContext);
  2122. if (info->GetInstance() == receiverObject)
  2123. {
  2124. PropertyValueInfo::SetCacheInfo(info, info->GetPropertyRecordUsageCache()->GetLdElemInlineCache(), info->AllowResizingPolymorphicInlineCache());
  2125. CacheOperators::CachePropertyRead(object, receiverObject, false, info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId(), false, info, requestContext);
  2126. }
  2127. }
  2128. return TRUE;
  2129. }
  2130. }
  2131. return FALSE;
  2132. }
  2133. BOOL JavascriptOperators::SetItemOnTaggedNumber(Var receiver, RecyclableObject* object, uint32 index, Var newValue, ScriptContext* requestContext,
  2134. PropertyOperationFlags propertyOperationFlags)
  2135. {
  2136. Assert(TaggedNumber::Is(receiver));
  2137. if (requestContext->optimizationOverrides.GetSideEffects() & SideEffects_Accessor)
  2138. {
  2139. Var setterValueOrProxy = nullptr;
  2140. DescriptorFlags flags = None;
  2141. if (object == nullptr)
  2142. {
  2143. GetPropertyObject(receiver, requestContext, &object);
  2144. }
  2145. if (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableItem(object, index, &setterValueOrProxy, &flags, requestContext))
  2146. {
  2147. if ((flags & Accessor) == Accessor)
  2148. {
  2149. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext))
  2150. {
  2151. return TRUE;
  2152. }
  2153. if (setterValueOrProxy)
  2154. {
  2155. RecyclableObject* func = RecyclableObject::FromVar(setterValueOrProxy);
  2156. JavascriptOperators::CallSetter(func, receiver, newValue, requestContext);
  2157. return TRUE;
  2158. }
  2159. }
  2160. else if ((flags & Proxy) == Proxy)
  2161. {
  2162. Assert(JavascriptProxy::Is(setterValueOrProxy));
  2163. JavascriptProxy* proxy = JavascriptProxy::FromVar(setterValueOrProxy);
  2164. const PropertyRecord* propertyRecord = nullptr;
  2165. proxy->PropertyIdFromInt(index, &propertyRecord);
  2166. return proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetItemOnTaggedNumberKind, propertyRecord->GetPropertyId(), newValue, requestContext, propertyOperationFlags);
  2167. }
  2168. else
  2169. {
  2170. Assert((flags & Data) == Data && (flags & Writable) == None);
  2171. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2172. }
  2173. }
  2174. }
  2175. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2176. return FALSE;
  2177. }
  2178. BOOL JavascriptOperators::SetPropertyOnTaggedNumber(Var receiver, RecyclableObject* object, PropertyId propertyId, Var newValue, ScriptContext* requestContext,
  2179. PropertyOperationFlags propertyOperationFlags)
  2180. {
  2181. Assert (TaggedNumber::Is(receiver));
  2182. if (requestContext->optimizationOverrides.GetSideEffects() & SideEffects_Accessor)
  2183. {
  2184. Var setterValueOrProxy = nullptr;
  2185. PropertyValueInfo info;
  2186. DescriptorFlags flags = None;
  2187. if (object == nullptr)
  2188. {
  2189. GetPropertyObject(receiver, requestContext, &object);
  2190. }
  2191. if (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(object, propertyId, &setterValueOrProxy, &flags, &info, requestContext))
  2192. {
  2193. if ((flags & Accessor) == Accessor)
  2194. {
  2195. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext))
  2196. {
  2197. return TRUE;
  2198. }
  2199. if (setterValueOrProxy)
  2200. {
  2201. RecyclableObject* func = RecyclableObject::FromVar(setterValueOrProxy);
  2202. Assert(info.GetFlags() == InlineCacheSetterFlag || info.GetPropertyIndex() == Constants::NoSlot);
  2203. JavascriptOperators::CallSetter(func, receiver, newValue, requestContext);
  2204. return TRUE;
  2205. }
  2206. }
  2207. else if ((flags & Proxy) == Proxy)
  2208. {
  2209. Assert(JavascriptProxy::Is(setterValueOrProxy));
  2210. JavascriptProxy* proxy = JavascriptProxy::FromVar(setterValueOrProxy);
  2211. return proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetPropertyOnTaggedNumberKind, propertyId, newValue, requestContext, propertyOperationFlags);
  2212. }
  2213. else
  2214. {
  2215. Assert((flags & Data) == Data && (flags & Writable) == None);
  2216. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2217. }
  2218. }
  2219. }
  2220. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  2221. requestContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  2222. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2223. return FALSE;
  2224. }
  2225. BOOL JavascriptOperators::SetPropertyUnscopable(Var instance, RecyclableObject* receiver, PropertyId propertyId, Var newValue, PropertyValueInfo * info, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  2226. {
  2227. return SetProperty_Internal<true>(instance, receiver, false, propertyId, newValue, info, requestContext, propertyOperationFlags);
  2228. }
  2229. BOOL JavascriptOperators::SetProperty(Var receiver, RecyclableObject* object, PropertyId propertyId, Var newValue, PropertyValueInfo * info, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  2230. {
  2231. return SetProperty_Internal<false>(receiver, object, false, propertyId, newValue, info, requestContext, propertyOperationFlags);
  2232. }
  2233. BOOL JavascriptOperators::SetRootProperty(RecyclableObject* instance, PropertyId propertyId, Var newValue, PropertyValueInfo * info, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  2234. {
  2235. return SetProperty_Internal<false>(instance, instance, true, propertyId, newValue, info, requestContext, propertyOperationFlags);
  2236. }
  2237. // Returns true if a result was written.
  2238. bool JavascriptOperators::SetAccessorOrNonWritableProperty(
  2239. Var receiver,
  2240. RecyclableObject* object,
  2241. PropertyId propertyId,
  2242. Var newValue,
  2243. PropertyValueInfo * info,
  2244. ScriptContext* requestContext,
  2245. PropertyOperationFlags propertyOperationFlags,
  2246. bool isRoot,
  2247. bool allowUndecInConsoleScope,
  2248. BOOL *result)
  2249. {
  2250. *result = FALSE;
  2251. Var setterValueOrProxy = nullptr;
  2252. DescriptorFlags flags = None;
  2253. if ((isRoot && JavascriptOperators::CheckPrototypesForAccessorOrNonWritableRootProperty(object, propertyId, &setterValueOrProxy, &flags, info, requestContext)) ||
  2254. (!isRoot && JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(object, propertyId, &setterValueOrProxy, &flags, info, requestContext)))
  2255. {
  2256. if ((flags & Accessor) == Accessor)
  2257. {
  2258. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext) ||
  2259. JavascriptError::ThrowIfNotExtensibleUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext))
  2260. {
  2261. *result = TRUE;
  2262. return true;
  2263. }
  2264. if (setterValueOrProxy)
  2265. {
  2266. RecyclableObject* func = RecyclableObject::FromVar(setterValueOrProxy);
  2267. Assert(!info || info->GetFlags() == InlineCacheSetterFlag || info->GetPropertyIndex() == Constants::NoSlot);
  2268. if (UnscopablesWrapperObject::Is(receiver))
  2269. {
  2270. receiver = (RecyclableObject::FromVar(receiver))->GetThisObjectOrUnWrap();
  2271. }
  2272. else if (!JavascriptOperators::IsUndefinedAccessor(setterValueOrProxy, requestContext))
  2273. {
  2274. CacheOperators::CachePropertyWrite(RecyclableObject::FromVar(receiver), isRoot, object->GetType(), propertyId, info, requestContext);
  2275. }
  2276. #ifdef ENABLE_MUTATION_BREAKPOINT
  2277. if (MutationBreakpoint::IsFeatureEnabled(requestContext))
  2278. {
  2279. MutationBreakpoint::HandleSetProperty(requestContext, object, propertyId, newValue);
  2280. }
  2281. #endif
  2282. JavascriptOperators::CallSetter(func, receiver, newValue, requestContext);
  2283. }
  2284. *result = TRUE;
  2285. return true;
  2286. }
  2287. else if ((flags & Proxy) == Proxy)
  2288. {
  2289. Assert(JavascriptProxy::Is(setterValueOrProxy));
  2290. JavascriptProxy* proxy = JavascriptProxy::FromVar(setterValueOrProxy);
  2291. // 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
  2292. // invalidate when target, handler, or handler prototype has changed. We don't have a way to achieve this yet.
  2293. PropertyValueInfo::SetNoCache(info, proxy);
  2294. PropertyValueInfo::DisablePrototypeCache(info, proxy); // We can't cache prototype property either
  2295. *result = proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetPropertyKind, propertyId, newValue, requestContext, propertyOperationFlags);
  2296. return true;
  2297. }
  2298. else
  2299. {
  2300. Assert((flags & Data) == Data && (flags & Writable) == None);
  2301. if (!allowUndecInConsoleScope)
  2302. {
  2303. if (flags & Const)
  2304. {
  2305. JavascriptError::ThrowTypeError(requestContext, ERRAssignmentToConst);
  2306. }
  2307. JavascriptError::ThrowCantAssign(propertyOperationFlags, requestContext, propertyId);
  2308. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2309. *result = FALSE;
  2310. return true;
  2311. }
  2312. }
  2313. }
  2314. return false;
  2315. }
  2316. template <bool unscopables>
  2317. BOOL JavascriptOperators::SetProperty_Internal(Var receiver, RecyclableObject* object, const bool isRoot, PropertyId propertyId, Var newValue, PropertyValueInfo * info, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  2318. {
  2319. if (receiver == nullptr)
  2320. {
  2321. return FALSE;
  2322. }
  2323. Assert(!TaggedNumber::Is(receiver));
  2324. BOOL setAccessorResult = FALSE;
  2325. if (SetAccessorOrNonWritableProperty(receiver, object, propertyId, newValue, info, requestContext, propertyOperationFlags, isRoot, false, &setAccessorResult))
  2326. {
  2327. return setAccessorResult;
  2328. }
  2329. else if (!JavascriptOperators::IsObject(receiver))
  2330. {
  2331. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2332. return FALSE;
  2333. }
  2334. #ifdef ENABLE_MUTATION_BREAKPOINT
  2335. // Break on mutation if needed
  2336. bool doNotUpdateCacheForMbp = MutationBreakpoint::IsFeatureEnabled(requestContext) ?
  2337. MutationBreakpoint::HandleSetProperty(requestContext, object, propertyId, newValue) : false;
  2338. #endif
  2339. // Get the original type before setting the property
  2340. Type *typeWithoutProperty = object->GetType();
  2341. BOOL didSetProperty = false;
  2342. if (isRoot)
  2343. {
  2344. AssertMsg(JavascriptOperators::GetTypeId(receiver) == TypeIds_GlobalObject
  2345. || JavascriptOperators::GetTypeId(receiver) == TypeIds_ModuleRoot,
  2346. "Root must be a global object!");
  2347. RootObjectBase* rootObject = static_cast<RootObjectBase*>(receiver);
  2348. didSetProperty = rootObject->SetRootProperty(propertyId, newValue, propertyOperationFlags, info);
  2349. }
  2350. else
  2351. {
  2352. RecyclableObject* instanceObject = RecyclableObject::FromVar(receiver);
  2353. while (!JavascriptOperators::IsNull(instanceObject))
  2354. {
  2355. if (unscopables && JavascriptOperators::IsPropertyUnscopable(instanceObject, propertyId))
  2356. {
  2357. break;
  2358. }
  2359. else
  2360. {
  2361. didSetProperty = instanceObject->SetProperty(propertyId, newValue, propertyOperationFlags, info);
  2362. if (didSetProperty || !unscopables)
  2363. {
  2364. break;
  2365. }
  2366. }
  2367. instanceObject = JavascriptOperators::GetPrototypeNoTrap(instanceObject);
  2368. }
  2369. }
  2370. if (didSetProperty)
  2371. {
  2372. bool updateCache = true;
  2373. #ifdef ENABLE_MUTATION_BREAKPOINT
  2374. updateCache = updateCache && !doNotUpdateCacheForMbp;
  2375. #endif
  2376. if (updateCache)
  2377. {
  2378. if (!JavascriptProxy::Is(receiver))
  2379. {
  2380. CacheOperators::CachePropertyWrite(RecyclableObject::FromVar(receiver), isRoot, typeWithoutProperty, propertyId, info, requestContext);
  2381. }
  2382. }
  2383. return TRUE;
  2384. }
  2385. return FALSE;
  2386. }
  2387. BOOL JavascriptOperators::IsNumberFromNativeArray(Var instance, uint32 index, ScriptContext* scriptContext)
  2388. {
  2389. #if ENABLE_COPYONACCESS_ARRAY
  2390. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  2391. #endif
  2392. Js::TypeId instanceType = JavascriptOperators::GetTypeId(instance);
  2393. // Fast path for native and typed arrays.
  2394. bool isNativeArray = instanceType == TypeIds_NativeIntArray || instanceType == TypeIds_NativeFloatArray;
  2395. bool isTypedArray = instanceType >= TypeIds_Int8Array && instanceType <= TypeIds_Uint64Array;
  2396. if (isNativeArray || isTypedArray)
  2397. {
  2398. // Check if the typed array is detached to prevent an exception in GetOwnItem
  2399. if (isTypedArray && TypedArrayBase::IsDetachedTypedArray(instance))
  2400. {
  2401. return FALSE;
  2402. }
  2403. RecyclableObject* object = RecyclableObject::FromVar(instance);
  2404. Var member = nullptr;
  2405. // If the item is found in the array own body, then it is a number
  2406. if (JavascriptOperators::GetOwnItem(object, index, &member, scriptContext)
  2407. && !JavascriptOperators::IsUndefined(member))
  2408. {
  2409. return TRUE;
  2410. }
  2411. }
  2412. return FALSE;
  2413. }
  2414. BOOL _Check_return_ _Success_(return) JavascriptOperators::GetAccessors(RecyclableObject* instance, PropertyId propertyId, ScriptContext* requestContext, _Out_ Var* getter, _Out_ Var* setter)
  2415. {
  2416. RecyclableObject* object = instance;
  2417. while (!JavascriptOperators::IsNull(object))
  2418. {
  2419. if (object->GetAccessors(propertyId, getter, setter, requestContext))
  2420. {
  2421. *getter = JavascriptOperators::CanonicalizeAccessor(*getter, requestContext);
  2422. *setter = JavascriptOperators::CanonicalizeAccessor(*setter, requestContext);
  2423. return TRUE;
  2424. }
  2425. if (object->SkipsPrototype())
  2426. {
  2427. break;
  2428. }
  2429. object = JavascriptOperators::GetPrototype(object);
  2430. }
  2431. return FALSE;
  2432. }
  2433. BOOL JavascriptOperators::SetAccessors(RecyclableObject* instance, PropertyId propertyId, Var getter, Var setter, PropertyOperationFlags flags)
  2434. {
  2435. BOOL result = instance && instance->SetAccessors(propertyId, getter, setter, flags);
  2436. return result;
  2437. }
  2438. BOOL JavascriptOperators::OP_SetProperty(Var instance, PropertyId propertyId, Var newValue, ScriptContext* scriptContext, PropertyValueInfo * info, PropertyOperationFlags flags, Var thisInstance)
  2439. {
  2440. // The call into ToObject(dynamicObject) is avoided here by checking for null and undefined and doing nothing when dynamicObject is a primitive value.
  2441. if (thisInstance == nullptr)
  2442. {
  2443. thisInstance = instance;
  2444. }
  2445. TypeId typeId = JavascriptOperators::GetTypeId(instance);
  2446. if (JavascriptOperators::IsUndefinedOrNullType(typeId))
  2447. {
  2448. if (scriptContext->GetThreadContext()->RecordImplicitException())
  2449. {
  2450. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotSet_NullOrUndefined, scriptContext->GetPropertyName(propertyId)->GetBuffer());
  2451. }
  2452. return TRUE;
  2453. }
  2454. else if (typeId == TypeIds_VariantDate)
  2455. {
  2456. if (scriptContext->GetThreadContext()->RecordImplicitException())
  2457. {
  2458. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_VarDate, scriptContext->GetPropertyName(propertyId)->GetBuffer());
  2459. }
  2460. return TRUE;
  2461. }
  2462. if (!TaggedNumber::Is(instance) && !TaggedNumber::Is(thisInstance))
  2463. {
  2464. return JavascriptOperators::SetProperty(RecyclableObject::UnsafeFromVar(thisInstance), RecyclableObject::UnsafeFromVar(instance), propertyId, newValue, info, scriptContext, flags);
  2465. }
  2466. JavascriptError::ThrowCantAssignIfStrictMode(flags, scriptContext);
  2467. return false;
  2468. }
  2469. BOOL JavascriptOperators::OP_StFunctionExpression(Var obj, PropertyId propertyId, Var newValue)
  2470. {
  2471. RecyclableObject* instance = RecyclableObject::FromVar(obj);
  2472. JIT_HELPER_NOT_REENTRANT_HEADER(Op_StFunctionExpression, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2473. instance->SetProperty(propertyId, newValue, PropertyOperation_None, NULL);
  2474. instance->SetWritable(propertyId, FALSE);
  2475. instance->SetConfigurable(propertyId, FALSE);
  2476. return TRUE;
  2477. JIT_HELPER_END(Op_StFunctionExpression);
  2478. }
  2479. BOOL JavascriptOperators::OP_InitClassMember(Var obj, PropertyId propertyId, Var newValue)
  2480. {
  2481. JIT_HELPER_REENTRANT_HEADER(Op_InitClassMember);
  2482. RecyclableObject* instance = RecyclableObject::FromVar(obj);
  2483. PropertyOperationFlags flags = PropertyOperation_None;
  2484. PropertyAttributes attributes = PropertyClassMemberDefaults;
  2485. instance->SetPropertyWithAttributes(propertyId, newValue, attributes, NULL, flags);
  2486. return TRUE;
  2487. JIT_HELPER_END(Op_InitClassMember);
  2488. }
  2489. BOOL JavascriptOperators::OP_InitLetProperty(Var obj, PropertyId propertyId, Var newValue)
  2490. {
  2491. JIT_HELPER_REENTRANT_HEADER(Op_InitLetFld);
  2492. RecyclableObject* instance = RecyclableObject::FromVar(obj);
  2493. PropertyOperationFlags flags = instance->GetScriptContext()->IsUndeclBlockVar(newValue) ? PropertyOperation_SpecialValue : PropertyOperation_None;
  2494. PropertyAttributes attributes = PropertyLetDefaults;
  2495. if (RootObjectBase::Is(instance))
  2496. {
  2497. attributes |= PropertyLetConstGlobal;
  2498. }
  2499. instance->SetPropertyWithAttributes(propertyId, newValue, attributes, NULL, (PropertyOperationFlags)(flags | PropertyOperation_AllowUndecl));
  2500. return TRUE;
  2501. JIT_HELPER_END(Op_InitLetFld);
  2502. }
  2503. BOOL JavascriptOperators::OP_InitConstProperty(Var obj, PropertyId propertyId, Var newValue)
  2504. {
  2505. RecyclableObject* instance = RecyclableObject::FromVar(obj);
  2506. JIT_HELPER_REENTRANT_HEADER(Op_InitConstFld);
  2507. PropertyOperationFlags flags = instance->GetScriptContext()->IsUndeclBlockVar(newValue) ? PropertyOperation_SpecialValue : PropertyOperation_None;
  2508. PropertyAttributes attributes = PropertyConstDefaults;
  2509. if (RootObjectBase::Is(instance))
  2510. {
  2511. attributes |= PropertyLetConstGlobal;
  2512. }
  2513. instance->SetPropertyWithAttributes(propertyId, newValue, attributes, NULL, (PropertyOperationFlags)(flags | PropertyOperation_AllowUndecl));
  2514. return TRUE;
  2515. JIT_HELPER_END(Op_InitConstFld);
  2516. }
  2517. BOOL JavascriptOperators::OP_InitUndeclRootLetProperty(Var obj, PropertyId propertyId)
  2518. {
  2519. RecyclableObject* instance = RecyclableObject::FromVar(obj);
  2520. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitUndeclRootLetFld, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2521. PropertyOperationFlags flags = static_cast<PropertyOperationFlags>(PropertyOperation_SpecialValue | PropertyOperation_AllowUndecl);
  2522. PropertyAttributes attributes = PropertyLetDefaults | PropertyLetConstGlobal;
  2523. instance->SetPropertyWithAttributes(propertyId, instance->GetLibrary()->GetUndeclBlockVar(), attributes, NULL, flags);
  2524. return TRUE;
  2525. JIT_HELPER_END(Op_InitUndeclRootLetFld);
  2526. }
  2527. BOOL JavascriptOperators::OP_InitUndeclRootConstProperty(Var obj, PropertyId propertyId)
  2528. {
  2529. RecyclableObject* instance = RecyclableObject::FromVar(obj);
  2530. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitUndeclRootConstFld, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2531. PropertyOperationFlags flags = static_cast<PropertyOperationFlags>(PropertyOperation_SpecialValue | PropertyOperation_AllowUndecl);
  2532. PropertyAttributes attributes = PropertyConstDefaults | PropertyLetConstGlobal;
  2533. instance->SetPropertyWithAttributes(propertyId, instance->GetLibrary()->GetUndeclBlockVar(), attributes, NULL, flags);
  2534. return TRUE;
  2535. JIT_HELPER_END(Op_InitUndeclRootConstFld);
  2536. }
  2537. BOOL JavascriptOperators::OP_InitUndeclConsoleLetProperty(Var obj, PropertyId propertyId)
  2538. {
  2539. FrameDisplay *pScope = (FrameDisplay*)obj;
  2540. AssertMsg(ConsoleScopeActivationObject::Is((DynamicObject*)pScope->GetItem(pScope->GetLength() - 1)), "How come we got this opcode without ConsoleScopeActivationObject?");
  2541. RecyclableObject* instance = RecyclableObject::FromVar(pScope->GetItem(0));
  2542. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitUndeclConsoleLetFld, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2543. PropertyOperationFlags flags = static_cast<PropertyOperationFlags>(PropertyOperation_SpecialValue | PropertyOperation_AllowUndecl);
  2544. PropertyAttributes attributes = PropertyLetDefaults;
  2545. instance->SetPropertyWithAttributes(propertyId, instance->GetLibrary()->GetUndeclBlockVar(), attributes, NULL, flags);
  2546. return TRUE;
  2547. JIT_HELPER_END(Op_InitUndeclConsoleLetFld);
  2548. }
  2549. BOOL JavascriptOperators::OP_InitUndeclConsoleConstProperty(Var obj, PropertyId propertyId)
  2550. {
  2551. FrameDisplay *pScope = (FrameDisplay*)obj;
  2552. AssertMsg(ConsoleScopeActivationObject::Is((DynamicObject*)pScope->GetItem(pScope->GetLength() - 1)), "How come we got this opcode without ConsoleScopeActivationObject?");
  2553. RecyclableObject* instance = RecyclableObject::FromVar(pScope->GetItem(0));
  2554. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitUndeclConsoleConstFld, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2555. PropertyOperationFlags flags = static_cast<PropertyOperationFlags>(PropertyOperation_SpecialValue | PropertyOperation_AllowUndecl);
  2556. PropertyAttributes attributes = PropertyConstDefaults;
  2557. instance->SetPropertyWithAttributes(propertyId, instance->GetLibrary()->GetUndeclBlockVar(), attributes, NULL, flags);
  2558. return TRUE;
  2559. JIT_HELPER_END(Op_InitUndeclConsoleConstFld);
  2560. }
  2561. BOOL JavascriptOperators::InitProperty(RecyclableObject* instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  2562. {
  2563. return instance && instance->InitProperty(propertyId, newValue, flags);
  2564. }
  2565. BOOL JavascriptOperators::OP_InitProperty(Var instance, PropertyId propertyId, Var newValue)
  2566. {
  2567. if(TaggedNumber::Is(instance)) { return false; }
  2568. return JavascriptOperators::InitProperty(RecyclableObject::FromVar(instance), propertyId, newValue);
  2569. }
  2570. BOOL JavascriptOperators::DeleteProperty(RecyclableObject* instance, PropertyId propertyId, PropertyOperationFlags propertyOperationFlags)
  2571. {
  2572. return DeleteProperty_Impl<false>(instance, propertyId, propertyOperationFlags);
  2573. }
  2574. bool JavascriptOperators::ShouldTryDeleteProperty(RecyclableObject* instance, JavascriptString *propertyNameString, PropertyRecord const **pPropertyRecord)
  2575. {
  2576. PropertyRecord const *propertyRecord = nullptr;
  2577. if (!JavascriptOperators::CanShortcutOnUnknownPropertyName(instance))
  2578. {
  2579. instance->GetScriptContext()->GetOrAddPropertyRecord(propertyNameString, &propertyRecord);
  2580. }
  2581. else
  2582. {
  2583. instance->GetScriptContext()->FindPropertyRecord(propertyNameString, &propertyRecord);
  2584. }
  2585. if (propertyRecord == nullptr)
  2586. {
  2587. return false;
  2588. }
  2589. *pPropertyRecord = propertyRecord;
  2590. return true;
  2591. }
  2592. BOOL JavascriptOperators::DeleteProperty(RecyclableObject* instance, JavascriptString *propertyNameString, PropertyOperationFlags propertyOperationFlags)
  2593. {
  2594. #ifdef ENABLE_MUTATION_BREAKPOINT
  2595. ScriptContext *scriptContext = instance->GetScriptContext();
  2596. if (MutationBreakpoint::IsFeatureEnabled(scriptContext)
  2597. && scriptContext->HasMutationBreakpoints())
  2598. {
  2599. MutationBreakpoint::HandleDeleteProperty(scriptContext, instance, propertyNameString);
  2600. }
  2601. #endif
  2602. return instance->DeleteProperty(propertyNameString, propertyOperationFlags);
  2603. }
  2604. BOOL JavascriptOperators::DeletePropertyUnscopables(RecyclableObject* instance, PropertyId propertyId, PropertyOperationFlags propertyOperationFlags)
  2605. {
  2606. return DeleteProperty_Impl<true>(instance, propertyId, propertyOperationFlags);
  2607. }
  2608. template<bool unscopables>
  2609. BOOL JavascriptOperators::DeleteProperty_Impl(RecyclableObject* instance, PropertyId propertyId, PropertyOperationFlags propertyOperationFlags)
  2610. {
  2611. if (unscopables && JavascriptOperators::IsPropertyUnscopable(instance, propertyId))
  2612. {
  2613. return false;
  2614. }
  2615. #ifdef ENABLE_MUTATION_BREAKPOINT
  2616. ScriptContext *scriptContext = instance->GetScriptContext();
  2617. if (MutationBreakpoint::IsFeatureEnabled(scriptContext)
  2618. && scriptContext->HasMutationBreakpoints())
  2619. {
  2620. MutationBreakpoint::HandleDeleteProperty(scriptContext, instance, propertyId);
  2621. }
  2622. #endif
  2623. // !unscopables will hit the return statement on the first iteration
  2624. return instance->DeleteProperty(propertyId, propertyOperationFlags);
  2625. }
  2626. Var JavascriptOperators::OP_DeleteProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  2627. {
  2628. JIT_HELPER_REENTRANT_HEADER(Op_DeleteProperty);
  2629. if(TaggedNumber::Is(instance))
  2630. {
  2631. return scriptContext->GetLibrary()->GetTrue();
  2632. }
  2633. RecyclableObject* recyclableObject = RecyclableObject::FromVar(instance);
  2634. if (JavascriptOperators::IsUndefinedOrNull(recyclableObject))
  2635. {
  2636. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotDelete_NullOrUndefined,
  2637. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  2638. }
  2639. return scriptContext->GetLibrary()->CreateBoolean(
  2640. JavascriptOperators::DeleteProperty(recyclableObject, propertyId, propertyOperationFlags));
  2641. JIT_HELPER_END(Op_DeleteProperty);
  2642. }
  2643. Var JavascriptOperators::OP_DeleteRootProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  2644. {
  2645. // In Edge the root is an External Object which can call Dispose and thus, can have reentrancy.
  2646. JIT_HELPER_REENTRANT_HEADER(Op_DeleteRootProperty);
  2647. AssertMsg(RootObjectBase::Is(instance), "Root must be a global object!");
  2648. RootObjectBase* rootObject = static_cast<RootObjectBase*>(instance);
  2649. return scriptContext->GetLibrary()->CreateBoolean(
  2650. rootObject->DeleteRootProperty(propertyId, propertyOperationFlags));
  2651. JIT_HELPER_END(Op_DeleteRootProperty);
  2652. }
  2653. template <bool IsFromFullJit, class TInlineCache>
  2654. inline void JavascriptOperators::PatchSetPropertyScoped(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var newValue, Var defaultInstance, PropertyOperationFlags propertyOperationFlags)
  2655. {
  2656. JIT_HELPER_REENTRANT_HEADER(Op_PatchSetPropertyScoped);
  2657. // Set the property using a scope stack rather than an individual instance.
  2658. // Walk the stack until we find an instance that has the property and store
  2659. // the new value there.
  2660. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  2661. uint16 length = pDisplay->GetLength();
  2662. RecyclableObject *object;
  2663. PropertyValueInfo info;
  2664. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  2665. bool allowUndecInConsoleScope = (propertyOperationFlags & PropertyOperation_AllowUndeclInConsoleScope) == PropertyOperation_AllowUndeclInConsoleScope;
  2666. bool isLexicalThisSlotSymbol = (propertyId == PropertyIds::_this);
  2667. for (uint16 i = 0; i < length; i++)
  2668. {
  2669. object = RecyclableObject::UnsafeFromVar(pDisplay->GetItem(i));
  2670. AssertMsg(!ConsoleScopeActivationObject::Is(object) || (i == length - 1), "Invalid location for ConsoleScopeActivationObject");
  2671. Type* type = object->GetType();
  2672. if (CacheOperators::TrySetProperty<true, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  2673. object, false, propertyId, newValue, scriptContext, propertyOperationFlags, nullptr, &info))
  2674. {
  2675. return;
  2676. }
  2677. // In scoped set property, we need to set the property when it is available; it could be a setter
  2678. // or normal property. we need to check setter first, and if no setter is available, but HasProperty
  2679. // is true, this must be a normal property.
  2680. // TODO: merge OP_HasProperty and GetSetter in one pass if there is perf problem. In fastDOM we have quite
  2681. // a lot of setters so separating the two might be actually faster.
  2682. BOOL setAccessorResult = FALSE;
  2683. if (SetAccessorOrNonWritableProperty(object, object, propertyId, newValue, &info, scriptContext, propertyOperationFlags, false, allowUndecInConsoleScope, &setAccessorResult))
  2684. {
  2685. return;
  2686. }
  2687. else if (!JavascriptOperators::IsObject(object))
  2688. {
  2689. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, scriptContext);
  2690. }
  2691. // Need to do a "get" of the current value (if any) to make sure that we're not writing to
  2692. // let/const before declaration, but we need to disable implicit calls around the "get",
  2693. // so we need to do a "has" first to make sure the "get" is valid (e.g., "get" on a HostDispatch
  2694. // with implicit calls disabled will always "succeed").
  2695. if (JavascriptOperators::HasProperty(object, propertyId))
  2696. {
  2697. DisableImplicitFlags disableImplicitFlags = scriptContext->GetThreadContext()->GetDisableImplicitFlags();
  2698. scriptContext->GetThreadContext()->SetDisableImplicitFlags(DisableImplicitCallAndExceptionFlag);
  2699. Var value;
  2700. BOOL result = JavascriptOperators::GetProperty(object, propertyId, &value, scriptContext, nullptr);
  2701. scriptContext->GetThreadContext()->SetDisableImplicitFlags(disableImplicitFlags);
  2702. if (result && scriptContext->IsUndeclBlockVar(value) && !allowUndecInConsoleScope && !isLexicalThisSlotSymbol)
  2703. {
  2704. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  2705. }
  2706. PropertyValueInfo info2;
  2707. PropertyValueInfo::SetCacheInfo(&info2, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  2708. PropertyOperationFlags setPropertyOpFlags = allowUndecInConsoleScope ? PropertyOperation_AllowUndeclInConsoleScope : PropertyOperation_None;
  2709. object->SetProperty(propertyId, newValue, setPropertyOpFlags, &info2);
  2710. #if DBG_DUMP
  2711. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  2712. {
  2713. CacheOperators::TraceCache(inlineCache, _u("PatchSetPropertyScoped"), propertyId, scriptContext, object);
  2714. }
  2715. #endif
  2716. if (!JavascriptProxy::Is(object) && !allowUndecInConsoleScope)
  2717. {
  2718. CacheOperators::CachePropertyWrite(object, false, type, propertyId, &info2, scriptContext);
  2719. }
  2720. return;
  2721. }
  2722. }
  2723. Assert(!isLexicalThisSlotSymbol);
  2724. // If we have console scope and no one in the scope had the property add it to console scope
  2725. if ((length > 0) && ConsoleScopeActivationObject::Is(pDisplay->GetItem(length - 1)))
  2726. {
  2727. // CheckPrototypesForAccessorOrNonWritableProperty does not check for const in global object. We should check it here.
  2728. if (length > 1)
  2729. {
  2730. Js::GlobalObject * globalObject = JavascriptOperators::TryFromVar<Js::GlobalObject>(pDisplay->GetItem(length - 2));
  2731. if (globalObject)
  2732. {
  2733. Var setterValue = nullptr;
  2734. DescriptorFlags flags = JavascriptOperators::GetRootSetter(globalObject, propertyId, &setterValue, &info, scriptContext);
  2735. Assert((flags & Accessor) != Accessor);
  2736. Assert((flags & Proxy) != Proxy);
  2737. if ((flags & Data) == Data && (flags & Writable) == None)
  2738. {
  2739. if (!allowUndecInConsoleScope)
  2740. {
  2741. if (flags & Const)
  2742. {
  2743. JavascriptError::ThrowTypeError(scriptContext, ERRAssignmentToConst);
  2744. }
  2745. Assert(!isLexicalThisSlotSymbol);
  2746. return;
  2747. }
  2748. }
  2749. }
  2750. }
  2751. RecyclableObject* obj = RecyclableObject::FromVar((DynamicObject*)pDisplay->GetItem(length - 1));
  2752. OUTPUT_TRACE(Js::ConsoleScopePhase, _u("Adding property '%s' to console scope object\n"), scriptContext->GetPropertyName(propertyId)->GetBuffer());
  2753. JavascriptOperators::SetProperty(obj, obj, propertyId, newValue, scriptContext, propertyOperationFlags);
  2754. return;
  2755. }
  2756. // No one in the scope stack has the property, so add it to the default instance provided by the caller.
  2757. AssertMsg(!TaggedNumber::Is(defaultInstance), "Root object is an int or tagged float?");
  2758. Assert(defaultInstance != nullptr);
  2759. RecyclableObject* obj = RecyclableObject::FromVar(defaultInstance);
  2760. {
  2761. //SetPropertyScoped does not use inline cache for default instance
  2762. PropertyValueInfo info2;
  2763. JavascriptOperators::SetRootProperty(obj, propertyId, newValue, &info2, scriptContext, (PropertyOperationFlags)(propertyOperationFlags | PropertyOperation_Root));
  2764. }
  2765. JIT_HELPER_END(Op_PatchSetPropertyScoped);
  2766. }
  2767. JIT_HELPER_TEMPLATE(Op_PatchSetPropertyScoped, Op_ConsolePatchSetPropertyScoped)
  2768. 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);
  2769. 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);
  2770. 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);
  2771. 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);
  2772. BOOL JavascriptOperators::OP_InitFuncScoped(FrameDisplay *pScope, PropertyId propertyId, Var newValue, Var defaultInstance, ScriptContext* scriptContext)
  2773. {
  2774. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitFuncScoped, reentrancylock, scriptContext->GetThreadContext());
  2775. int i;
  2776. int length = pScope->GetLength();
  2777. DynamicObject *obj;
  2778. for (i = 0; i < length; i++)
  2779. {
  2780. obj = (DynamicObject*)pScope->GetItem(i);
  2781. if (obj->InitFuncScoped(propertyId, newValue))
  2782. {
  2783. return TRUE;
  2784. }
  2785. }
  2786. AssertMsg(!TaggedNumber::Is(defaultInstance), "Root object is an int or tagged float?");
  2787. return RecyclableObject::FromVar(defaultInstance)->InitFuncScoped(propertyId, newValue);
  2788. JIT_HELPER_END(Op_InitFuncScoped);
  2789. }
  2790. BOOL JavascriptOperators::OP_InitPropertyScoped(FrameDisplay *pScope, PropertyId propertyId, Var newValue, Var defaultInstance, ScriptContext* scriptContext)
  2791. {
  2792. int i;
  2793. int length = pScope->GetLength();
  2794. DynamicObject *obj;
  2795. for (i = 0; i < length; i++)
  2796. {
  2797. obj = (DynamicObject*)pScope->GetItem(i);
  2798. if (obj->InitPropertyScoped(propertyId, newValue))
  2799. {
  2800. return TRUE;
  2801. }
  2802. }
  2803. AssertMsg(!TaggedNumber::Is(defaultInstance), "Root object is an int or tagged float?");
  2804. return RecyclableObject::FromVar(defaultInstance)->InitPropertyScoped(propertyId, newValue);
  2805. }
  2806. Var JavascriptOperators::OP_DeletePropertyScoped(
  2807. FrameDisplay *pScope,
  2808. PropertyId propertyId,
  2809. Var defaultInstance,
  2810. ScriptContext* scriptContext,
  2811. PropertyOperationFlags propertyOperationFlags)
  2812. {
  2813. JIT_HELPER_REENTRANT_HEADER(Op_DeletePropertyScoped);
  2814. JIT_HELPER_SAME_ATTRIBUTES(Op_DeleteRootProperty, Op_DeletePropertyScoped);
  2815. int i;
  2816. int length = pScope->GetLength();
  2817. for (i = 0; i < length; i++)
  2818. {
  2819. DynamicObject *obj = (DynamicObject*)pScope->GetItem(i);
  2820. if (JavascriptOperators::HasProperty(obj, propertyId))
  2821. {
  2822. return scriptContext->GetLibrary()->CreateBoolean(JavascriptOperators::DeleteProperty(obj, propertyId, propertyOperationFlags));
  2823. }
  2824. }
  2825. return JavascriptOperators::OP_DeleteRootProperty(RecyclableObject::FromVar(defaultInstance), propertyId, scriptContext, propertyOperationFlags);
  2826. JIT_HELPER_END(Op_DeletePropertyScoped);
  2827. }
  2828. Var JavascriptOperators::OP_TypeofPropertyScoped(FrameDisplay *pScope, PropertyId propertyId, Var defaultInstance, ScriptContext* scriptContext)
  2829. {
  2830. JIT_HELPER_REENTRANT_HEADER(Op_TypeofPropertyScoped);
  2831. int i;
  2832. int length = pScope->GetLength();
  2833. for (i = 0; i < length; i++)
  2834. {
  2835. DynamicObject *obj = (DynamicObject*)pScope->GetItem(i);
  2836. if (JavascriptOperators::HasProperty(obj, propertyId))
  2837. {
  2838. return JavascriptOperators::TypeofFld(obj, propertyId, scriptContext);
  2839. }
  2840. }
  2841. return JavascriptOperators::TypeofRootFld(RecyclableObject::FromVar(defaultInstance), propertyId, scriptContext);
  2842. JIT_HELPER_END(Op_TypeofPropertyScoped);
  2843. }
  2844. BOOL JavascriptOperators::HasOwnItem(RecyclableObject* object, uint32 index)
  2845. {
  2846. return object->HasOwnItem(index);
  2847. }
  2848. BOOL JavascriptOperators::HasItem(RecyclableObject* object, uint64 index)
  2849. {
  2850. PropertyRecord const * propertyRecord = nullptr;
  2851. ScriptContext* scriptContext = object->GetScriptContext();
  2852. JavascriptOperators::GetPropertyIdForInt(index, scriptContext, &propertyRecord);
  2853. return JavascriptOperators::HasProperty(object, propertyRecord->GetPropertyId());
  2854. }
  2855. BOOL JavascriptOperators::HasItem(RecyclableObject* object, uint32 index)
  2856. {
  2857. #if ENABLE_COPYONACCESS_ARRAY
  2858. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(object);
  2859. #endif
  2860. while (!JavascriptOperators::IsNull(object))
  2861. {
  2862. PropertyQueryFlags result;
  2863. if ((result = object->HasItemQuery(index)) != PropertyQueryFlags::Property_NotFound)
  2864. {
  2865. return JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  2866. }
  2867. // CONSIDER: Numeric property values shouldn't be on the prototype for now but if this changes
  2868. // we should add SkipsPrototype support here as well
  2869. object = JavascriptOperators::GetPrototypeNoTrap(object);
  2870. }
  2871. return false;
  2872. }
  2873. BOOL JavascriptOperators::GetOwnItem(RecyclableObject* object, uint32 index, Var* value, ScriptContext* requestContext)
  2874. {
  2875. return object->GetItem(object, index, value, requestContext);
  2876. }
  2877. BOOL JavascriptOperators::GetItem(Var instance, RecyclableObject* propertyObject, uint32 index, Var* value, ScriptContext* requestContext)
  2878. {
  2879. RecyclableObject* object = propertyObject;
  2880. while (!JavascriptOperators::IsNull(object))
  2881. {
  2882. PropertyQueryFlags result;
  2883. if ((result = object->GetItemQuery(instance, index, value, requestContext)) != PropertyQueryFlags::Property_NotFound)
  2884. {
  2885. return JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  2886. }
  2887. if (object->SkipsPrototype())
  2888. {
  2889. break;
  2890. }
  2891. object = JavascriptOperators::GetPrototypeNoTrap(object);
  2892. }
  2893. *value = requestContext->GetMissingItemResult();
  2894. return false;
  2895. }
  2896. BOOL JavascriptOperators::GetItemReference(Var instance, RecyclableObject* propertyObject, uint32 index, Var* value, ScriptContext* requestContext)
  2897. {
  2898. RecyclableObject* object = propertyObject;
  2899. while (!JavascriptOperators::IsNull(object))
  2900. {
  2901. PropertyQueryFlags result;
  2902. if ((result = object->GetItemReferenceQuery(instance, index, value, requestContext)) != PropertyQueryFlags::Property_NotFound)
  2903. {
  2904. return JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  2905. }
  2906. if (object->SkipsPrototype())
  2907. {
  2908. break;
  2909. }
  2910. object = JavascriptOperators::GetPrototypeNoTrap(object);
  2911. }
  2912. *value = requestContext->GetMissingItemResult();
  2913. return false;
  2914. }
  2915. BOOL JavascriptOperators::SetItem(Var receiver, RecyclableObject* object, uint64 index, Var value, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  2916. {
  2917. PropertyRecord const * propertyRecord = nullptr;
  2918. JavascriptOperators::GetPropertyIdForInt(index, scriptContext, &propertyRecord);
  2919. return JavascriptOperators::SetProperty(receiver, object, propertyRecord->GetPropertyId(), value, scriptContext, propertyOperationFlags);
  2920. }
  2921. BOOL JavascriptOperators::SetItem(Var receiver, RecyclableObject* object, uint32 index, Var value, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags, BOOL skipPrototypeCheck /* = FALSE */)
  2922. {
  2923. Var setterValueOrProxy = nullptr;
  2924. DescriptorFlags flags = None;
  2925. Assert(!TaggedNumber::Is(receiver));
  2926. if (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableItem(object, index, &setterValueOrProxy, &flags, scriptContext, skipPrototypeCheck))
  2927. {
  2928. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  2929. if ((flags & Accessor) == Accessor)
  2930. {
  2931. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, scriptContext) ||
  2932. JavascriptError::ThrowIfNotExtensibleUndefinedSetter(propertyOperationFlags, setterValueOrProxy, scriptContext))
  2933. {
  2934. return TRUE;
  2935. }
  2936. if (setterValueOrProxy)
  2937. {
  2938. RecyclableObject* func = RecyclableObject::FromVar(setterValueOrProxy);
  2939. JavascriptOperators::CallSetter(func, receiver, value, scriptContext);
  2940. }
  2941. return TRUE;
  2942. }
  2943. else if ((flags & Proxy) == Proxy)
  2944. {
  2945. Assert(JavascriptProxy::Is(setterValueOrProxy));
  2946. JavascriptProxy* proxy = JavascriptProxy::FromVar(setterValueOrProxy);
  2947. const PropertyRecord* propertyRecord = nullptr;
  2948. proxy->PropertyIdFromInt(index, &propertyRecord);
  2949. return proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetItemKind, propertyRecord->GetPropertyId(), value, scriptContext, propertyOperationFlags, skipPrototypeCheck);
  2950. }
  2951. else
  2952. {
  2953. Assert((flags & Data) == Data && (flags & Writable) == None);
  2954. if ((propertyOperationFlags & PropertyOperationFlags::PropertyOperation_ThrowIfNotExtensible) == PropertyOperationFlags::PropertyOperation_ThrowIfNotExtensible)
  2955. {
  2956. JavascriptError::ThrowTypeError(scriptContext, JSERR_NonExtensibleObject);
  2957. }
  2958. JavascriptError::ThrowCantAssign(propertyOperationFlags, scriptContext, index);
  2959. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, scriptContext);
  2960. return FALSE;
  2961. }
  2962. }
  2963. else if (!JavascriptOperators::IsObject(receiver))
  2964. {
  2965. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, scriptContext);
  2966. return FALSE;
  2967. }
  2968. return (RecyclableObject::FromVar(receiver))->SetItem(index, value, propertyOperationFlags);
  2969. }
  2970. BOOL JavascriptOperators::DeleteItem(RecyclableObject* object, uint32 index, PropertyOperationFlags propertyOperationFlags)
  2971. {
  2972. return object->DeleteItem(index, propertyOperationFlags);
  2973. }
  2974. BOOL JavascriptOperators::DeleteItem(RecyclableObject* object, uint64 index, PropertyOperationFlags propertyOperationFlags)
  2975. {
  2976. PropertyRecord const * propertyRecord = nullptr;
  2977. JavascriptOperators::GetPropertyIdForInt(index, object->GetScriptContext(), &propertyRecord);
  2978. return JavascriptOperators::DeleteProperty(object, propertyRecord->GetPropertyId(), propertyOperationFlags);
  2979. }
  2980. BOOL JavascriptOperators::OP_HasItem(Var instance, Var index, ScriptContext* scriptContext)
  2981. {
  2982. RecyclableObject* object = TaggedNumber::Is(instance) ?
  2983. scriptContext->GetLibrary()->GetNumberPrototype() :
  2984. RecyclableObject::FromVar(instance);
  2985. uint32 indexVal;
  2986. PropertyRecord const * propertyRecord = nullptr;
  2987. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, false);
  2988. if (indexType == IndexType_Number)
  2989. {
  2990. return HasItem(object, indexVal);
  2991. }
  2992. else
  2993. {
  2994. Assert(indexType == IndexType_PropertyId);
  2995. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  2996. {
  2997. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
  2998. Assert(indexType == IndexType_PropertyId);
  2999. Assert(propertyRecord != nullptr);
  3000. }
  3001. if (propertyRecord != nullptr)
  3002. {
  3003. return HasProperty(object, propertyRecord->GetPropertyId());
  3004. }
  3005. else
  3006. {
  3007. #if DBG
  3008. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  3009. PropertyRecord const * debugPropertyRecord;
  3010. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  3011. AssertMsg(!JavascriptOperators::HasProperty(object, debugPropertyRecord->GetPropertyId()), "how did this property come? See OS Bug 2727708 if you see this come from the web");
  3012. #endif
  3013. return FALSE;
  3014. }
  3015. }
  3016. }
  3017. #if ENABLE_PROFILE_INFO
  3018. void JavascriptOperators::UpdateNativeArrayProfileInfoToCreateVarArray(Var instance, const bool expectingNativeFloatArray, const bool expectingVarArray)
  3019. {
  3020. Assert(instance);
  3021. Assert(expectingNativeFloatArray ^ expectingVarArray);
  3022. JavascriptNativeArray * nativeArr = JavascriptOperators::TryFromVar<JavascriptNativeArray>(instance);
  3023. if (!nativeArr)
  3024. {
  3025. return;
  3026. }
  3027. ArrayCallSiteInfo *const arrayCallSiteInfo = nativeArr->GetArrayCallSiteInfo();
  3028. if (!arrayCallSiteInfo)
  3029. {
  3030. return;
  3031. }
  3032. if (expectingNativeFloatArray)
  3033. {
  3034. // Profile data is expecting a native float array. Ensure that at the array's creation site, that a native int array
  3035. // is not created, such that the profiled array type would be correct.
  3036. arrayCallSiteInfo->SetIsNotNativeIntArray();
  3037. }
  3038. else
  3039. {
  3040. // Profile data is expecting a var array. Ensure that at the array's creation site, that a native array is not
  3041. // created, such that the profiled array type would be correct.
  3042. Assert(expectingVarArray);
  3043. arrayCallSiteInfo->SetIsNotNativeArray();
  3044. }
  3045. }
  3046. bool JavascriptOperators::SetElementMayHaveImplicitCalls(ScriptContext *const scriptContext)
  3047. {
  3048. return
  3049. scriptContext->optimizationOverrides.GetArraySetElementFastPathVtable() ==
  3050. ScriptContextOptimizationOverrideInfo::InvalidVtable;
  3051. }
  3052. #endif
  3053. RecyclableObject *JavascriptOperators::GetCallableObjectOrThrow(const Var callee, ScriptContext *const scriptContext)
  3054. {
  3055. Assert(callee);
  3056. Assert(scriptContext);
  3057. if (TaggedNumber::Is(callee))
  3058. {
  3059. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction /* TODO-ERROR: get arg name - aFunc */);
  3060. }
  3061. return RecyclableObject::UnsafeFromVar(callee);
  3062. }
  3063. Var JavascriptOperators::OP_GetElementI_JIT(Var instance, Var index, ScriptContext *scriptContext)
  3064. {
  3065. JIT_HELPER_REENTRANT_HEADER(Op_GetElementI);
  3066. #if ENABLE_NATIVE_CODEGEN
  3067. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  3068. #endif
  3069. return OP_GetElementI(instance, index, scriptContext);
  3070. JIT_HELPER_END(Op_GetElementI);
  3071. }
  3072. Var JavascriptOperators::OP_GetElementI_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  3073. {
  3074. JIT_HELPER_REENTRANT_HEADER(Op_GetElementI_UInt32);
  3075. #if FLOATVAR
  3076. return OP_GetElementI_JIT(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  3077. #else
  3078. char buffer[sizeof(Js::JavascriptNumber)];
  3079. return OP_GetElementI_JIT(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3080. (Js::JavascriptNumber *)buffer), scriptContext);
  3081. #endif
  3082. JIT_HELPER_END(Op_GetElementI_UInt32);
  3083. }
  3084. Var JavascriptOperators::OP_GetElementI_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  3085. {
  3086. JIT_HELPER_REENTRANT_HEADER(Op_GetElementI_Int32);
  3087. #if FLOATVAR
  3088. return OP_GetElementI_JIT(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  3089. #else
  3090. char buffer[sizeof(Js::JavascriptNumber)];
  3091. return OP_GetElementI_JIT(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3092. (Js::JavascriptNumber *)buffer), scriptContext);
  3093. #endif
  3094. JIT_HELPER_END(Op_GetElementI_Int32);
  3095. }
  3096. BOOL JavascriptOperators::GetItemFromArrayPrototype(JavascriptArray * arr, int32 indexInt, Var * result, ScriptContext * scriptContext)
  3097. {
  3098. // try get from Array prototype
  3099. RecyclableObject* prototype = arr->GetPrototype();
  3100. if (JavascriptOperators::GetTypeId(prototype) != TypeIds_Array) //This can be TypeIds_ES5Array (or any other object changed through __proto__).
  3101. {
  3102. return false;
  3103. }
  3104. JavascriptArray* arrayPrototype = JavascriptArray::UnsafeFromVar(prototype); //Prototype must be Array.prototype (unless changed through __proto__)
  3105. if (arrayPrototype->GetLength() && arrayPrototype->GetItem(arrayPrototype, (uint32)indexInt, result, scriptContext))
  3106. {
  3107. return true;
  3108. }
  3109. prototype = arrayPrototype->GetPrototype(); //Its prototype must be Object.prototype (unless changed through __proto__)
  3110. if (prototype->GetScriptContext()->GetLibrary()->GetObjectPrototype() != prototype)
  3111. {
  3112. return false;
  3113. }
  3114. if (DynamicObject::FromVar(prototype)->HasNonEmptyObjectArray())
  3115. {
  3116. if (prototype->GetItem(arr, (uint32)indexInt, result, scriptContext))
  3117. {
  3118. return true;
  3119. }
  3120. }
  3121. *result = scriptContext->GetMissingItemResult();
  3122. return true;
  3123. }
  3124. Var JavascriptOperators::GetElementIIntIndex(_In_ Var instance, _In_ Var index, _In_ ScriptContext* scriptContext)
  3125. {
  3126. Assert(TaggedInt::Is(index));
  3127. switch (JavascriptOperators::GetTypeId(instance))
  3128. {
  3129. case TypeIds_Array: //fast path for array
  3130. {
  3131. Var result;
  3132. if (OP_GetElementI_ArrayFastPath(JavascriptArray::UnsafeFromVar(instance), TaggedInt::ToInt32(index), &result, scriptContext))
  3133. {
  3134. return result;
  3135. }
  3136. break;
  3137. }
  3138. case TypeIds_NativeIntArray:
  3139. {
  3140. #if ENABLE_COPYONACCESS_ARRAY
  3141. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  3142. #endif
  3143. Var result;
  3144. if (OP_GetElementI_ArrayFastPath(JavascriptNativeIntArray::UnsafeFromVar(instance), TaggedInt::ToInt32(index), &result, scriptContext))
  3145. {
  3146. return result;
  3147. }
  3148. break;
  3149. }
  3150. case TypeIds_NativeFloatArray:
  3151. {
  3152. Var result;
  3153. if (OP_GetElementI_ArrayFastPath(JavascriptNativeFloatArray::UnsafeFromVar(instance), TaggedInt::ToInt32(index), &result, scriptContext))
  3154. {
  3155. return result;
  3156. }
  3157. break;
  3158. }
  3159. case TypeIds_String: // fast path for string
  3160. {
  3161. charcount_t indexInt = TaggedInt::ToUInt32(index);
  3162. JavascriptString* string = JavascriptString::UnsafeFromVar(instance);
  3163. Var result;
  3164. if (JavascriptConversion::PropertyQueryFlagsToBoolean(string->JavascriptString::GetItemQuery(instance, indexInt, &result, scriptContext)))
  3165. {
  3166. return result;
  3167. }
  3168. break;
  3169. }
  3170. case TypeIds_Int8Array:
  3171. {
  3172. // The typed array will deal with all possible values for the index
  3173. int32 indexInt = TaggedInt::ToInt32(index);
  3174. if (VirtualTableInfo<Int8VirtualArray>::HasVirtualTable(instance))
  3175. {
  3176. Int8VirtualArray* int8Array = Int8VirtualArray::UnsafeFromVar(instance);
  3177. if (indexInt >= 0)
  3178. {
  3179. return int8Array->DirectGetItem(indexInt);
  3180. }
  3181. }
  3182. else if (VirtualTableInfo<Int8Array>::HasVirtualTable(instance))
  3183. {
  3184. Int8Array* int8Array = Int8Array::UnsafeFromVar(instance);
  3185. if (indexInt >= 0)
  3186. {
  3187. return int8Array->DirectGetItem(indexInt);
  3188. }
  3189. }
  3190. break;
  3191. }
  3192. case TypeIds_Uint8Array:
  3193. {
  3194. // The typed array will deal with all possible values for the index
  3195. int32 indexInt = TaggedInt::ToInt32(index);
  3196. if (VirtualTableInfo<Uint8VirtualArray>::HasVirtualTable(instance))
  3197. {
  3198. Uint8VirtualArray* uint8Array = Uint8VirtualArray::UnsafeFromVar(instance);
  3199. if (indexInt >= 0)
  3200. {
  3201. return uint8Array->DirectGetItem(indexInt);
  3202. }
  3203. }
  3204. else if (VirtualTableInfo<Uint8Array>::HasVirtualTable(instance))
  3205. {
  3206. Uint8Array* uint8Array = Uint8Array::UnsafeFromVar(instance);
  3207. if (indexInt >= 0)
  3208. {
  3209. return uint8Array->DirectGetItem(indexInt);
  3210. }
  3211. }
  3212. break;
  3213. }
  3214. case TypeIds_Uint8ClampedArray:
  3215. {
  3216. // The typed array will deal with all possible values for the index
  3217. int32 indexInt = TaggedInt::ToInt32(index);
  3218. if (VirtualTableInfo<Uint8ClampedVirtualArray>::HasVirtualTable(instance))
  3219. {
  3220. Uint8ClampedVirtualArray* uint8ClampedArray = Uint8ClampedVirtualArray::UnsafeFromVar(instance);
  3221. if (indexInt >= 0)
  3222. {
  3223. return uint8ClampedArray->DirectGetItem(indexInt);
  3224. }
  3225. }
  3226. else if (VirtualTableInfo<Uint8ClampedArray>::HasVirtualTable(instance))
  3227. {
  3228. Uint8ClampedArray* uint8ClampedArray = Uint8ClampedArray::UnsafeFromVar(instance);
  3229. if (indexInt >= 0)
  3230. {
  3231. return uint8ClampedArray->DirectGetItem(indexInt);
  3232. }
  3233. }
  3234. break;
  3235. }
  3236. case TypeIds_Int16Array:
  3237. {
  3238. // The type array will deal with all possible values for the index
  3239. int32 indexInt = TaggedInt::ToInt32(index);
  3240. if (VirtualTableInfo<Int16VirtualArray>::HasVirtualTable(instance))
  3241. {
  3242. Int16VirtualArray* int16Array = Int16VirtualArray::UnsafeFromVar(instance);
  3243. if (indexInt >= 0)
  3244. {
  3245. return int16Array->DirectGetItem(indexInt);
  3246. }
  3247. }
  3248. else if (VirtualTableInfo<Int16Array>::HasVirtualTable(instance))
  3249. {
  3250. Int16Array* int16Array = Int16Array::UnsafeFromVar(instance);
  3251. if (indexInt >= 0)
  3252. {
  3253. return int16Array->DirectGetItem(indexInt);
  3254. }
  3255. }
  3256. break;
  3257. }
  3258. case TypeIds_Uint16Array:
  3259. {
  3260. // The type array will deal with all possible values for the index
  3261. int32 indexInt = TaggedInt::ToInt32(index);
  3262. if (VirtualTableInfo<Uint16VirtualArray>::HasVirtualTable(instance))
  3263. {
  3264. Uint16VirtualArray* uint16Array = Uint16VirtualArray::UnsafeFromVar(instance);
  3265. if (indexInt >= 0)
  3266. {
  3267. return uint16Array->DirectGetItem(indexInt);
  3268. }
  3269. }
  3270. else if (VirtualTableInfo<Uint16Array>::HasVirtualTable(instance))
  3271. {
  3272. Uint16Array* uint16Array = Uint16Array::UnsafeFromVar(instance);
  3273. if (indexInt >= 0)
  3274. {
  3275. return uint16Array->DirectGetItem(indexInt);
  3276. }
  3277. }
  3278. break;
  3279. }
  3280. case TypeIds_Int32Array:
  3281. {
  3282. // The type array will deal with all possible values for the index
  3283. int32 indexInt = TaggedInt::ToInt32(index);
  3284. if (VirtualTableInfo<Int32VirtualArray>::HasVirtualTable(instance))
  3285. {
  3286. Int32VirtualArray* int32Array = Int32VirtualArray::UnsafeFromVar(instance);
  3287. if (indexInt >= 0)
  3288. {
  3289. return int32Array->DirectGetItem(indexInt);
  3290. }
  3291. }
  3292. else if (VirtualTableInfo<Int32Array>::HasVirtualTable(instance))
  3293. {
  3294. Int32Array* int32Array = Int32Array::UnsafeFromVar(instance);
  3295. if (indexInt >= 0)
  3296. {
  3297. return int32Array->DirectGetItem(indexInt);
  3298. }
  3299. }
  3300. break;
  3301. }
  3302. case TypeIds_Uint32Array:
  3303. {
  3304. // The type array will deal with all possible values for the index
  3305. int32 indexInt = TaggedInt::ToInt32(index);
  3306. if (VirtualTableInfo<Uint32VirtualArray>::HasVirtualTable(instance))
  3307. {
  3308. Uint32VirtualArray* uint32Array = Uint32VirtualArray::UnsafeFromVar(instance);
  3309. if (indexInt >= 0)
  3310. {
  3311. return uint32Array->DirectGetItem(indexInt);
  3312. }
  3313. }
  3314. else if (VirtualTableInfo<Uint32Array>::HasVirtualTable(instance))
  3315. {
  3316. Uint32Array* uint32Array = Uint32Array::UnsafeFromVar(instance);
  3317. if (indexInt >= 0)
  3318. {
  3319. return uint32Array->DirectGetItem(indexInt);
  3320. }
  3321. }
  3322. break;
  3323. }
  3324. case TypeIds_Float32Array:
  3325. {
  3326. // The type array will deal with all possible values for the index
  3327. int32 indexInt = TaggedInt::ToInt32(index);
  3328. if (VirtualTableInfo<Float32VirtualArray>::HasVirtualTable(instance))
  3329. {
  3330. Float32VirtualArray* float32Array = Float32VirtualArray::UnsafeFromVar(instance);
  3331. if (indexInt >= 0)
  3332. {
  3333. return float32Array->DirectGetItem(indexInt);
  3334. }
  3335. }
  3336. else if (VirtualTableInfo<Float32Array>::HasVirtualTable(instance))
  3337. {
  3338. Float32Array* float32Array = Float32Array::UnsafeFromVar(instance);
  3339. if (indexInt >= 0)
  3340. {
  3341. return float32Array->DirectGetItem(indexInt);
  3342. }
  3343. }
  3344. break;
  3345. }
  3346. case TypeIds_Float64Array:
  3347. {
  3348. // The type array will deal with all possible values for the index
  3349. int32 indexInt = TaggedInt::ToInt32(index);
  3350. if (VirtualTableInfo<Float64VirtualArray>::HasVirtualTable(instance))
  3351. {
  3352. Float64VirtualArray* float64Array = Float64VirtualArray::UnsafeFromVar(instance);
  3353. if (indexInt >= 0)
  3354. {
  3355. return float64Array->DirectGetItem(indexInt);
  3356. }
  3357. }
  3358. else if (VirtualTableInfo<Float64Array>::HasVirtualTable(instance))
  3359. {
  3360. Float64Array* float64Array = Float64Array::UnsafeFromVar(instance);
  3361. if (indexInt >= 0)
  3362. {
  3363. return float64Array->DirectGetItem(indexInt);
  3364. }
  3365. }
  3366. break;
  3367. }
  3368. default:
  3369. break;
  3370. }
  3371. return JavascriptOperators::GetElementIHelper(instance, index, instance, scriptContext);
  3372. }
  3373. template <typename T>
  3374. BOOL JavascriptOperators::OP_GetElementI_ArrayFastPath(T * arr, int indexInt, Var * result, ScriptContext * scriptContext)
  3375. {
  3376. #if ENABLE_COPYONACCESS_ARRAY
  3377. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(arr);
  3378. #endif
  3379. if (indexInt >= 0)
  3380. {
  3381. if (!CrossSite::IsCrossSiteObjectTyped(arr))
  3382. {
  3383. if (arr->T::DirectGetVarItemAt((uint32)indexInt, result, scriptContext))
  3384. {
  3385. return true;
  3386. }
  3387. }
  3388. else
  3389. {
  3390. if (arr->GetItem(arr, (uint32)indexInt, result, scriptContext))
  3391. {
  3392. return true;
  3393. }
  3394. }
  3395. return GetItemFromArrayPrototype(arr, indexInt, result, scriptContext);
  3396. }
  3397. return false;
  3398. }
  3399. Var JavascriptOperators::OP_GetElementI(Var instance, Var index, ScriptContext* scriptContext)
  3400. {
  3401. instance = BreakSpeculation(instance);
  3402. if (TaggedInt::Is(index))
  3403. {
  3404. return GetElementIIntIndex(instance, index, scriptContext);
  3405. }
  3406. if (JavascriptNumber::Is_NoTaggedIntCheck(index))
  3407. {
  3408. uint32 uint32Index = JavascriptConversion::ToUInt32(index, scriptContext);
  3409. if ((double)uint32Index == JavascriptNumber::GetValue(index) && !TaggedInt::IsOverflow(uint32Index))
  3410. {
  3411. index = TaggedInt::ToVarUnchecked(uint32Index);
  3412. return GetElementIIntIndex(instance, index, scriptContext);
  3413. }
  3414. }
  3415. else if (RecyclableObject::Is(instance))
  3416. {
  3417. RecyclableObject* cacheOwner;
  3418. PropertyRecordUsageCache* propertyRecordUsageCache;
  3419. if (GetPropertyRecordUsageCache(index, scriptContext, &propertyRecordUsageCache, &cacheOwner))
  3420. {
  3421. return GetElementIWithCache<false /* ReturnOperationInfo */>(instance, cacheOwner, propertyRecordUsageCache, scriptContext, nullptr);
  3422. }
  3423. }
  3424. return JavascriptOperators::GetElementIHelper(instance, index, instance, scriptContext);
  3425. }
  3426. _Success_(return) bool JavascriptOperators::GetPropertyRecordUsageCache(Var index, ScriptContext* scriptContext, _Outptr_ PropertyRecordUsageCache** propertyRecordUsageCache, _Outptr_ RecyclableObject** cacheOwner)
  3427. {
  3428. JavascriptString* string = JavascriptOperators::TryFromVar<JavascriptString>(index);
  3429. if (string)
  3430. {
  3431. PropertyString * propertyString = nullptr;
  3432. if (VirtualTableInfo<Js::PropertyString>::HasVirtualTable(string))
  3433. {
  3434. propertyString = (PropertyString*)string;
  3435. }
  3436. else if (VirtualTableInfo<Js::LiteralStringWithPropertyStringPtr>::HasVirtualTable(string))
  3437. {
  3438. LiteralStringWithPropertyStringPtr * strWithPtr = (LiteralStringWithPropertyStringPtr *)string;
  3439. if (!strWithPtr->HasPropertyRecord())
  3440. {
  3441. PropertyRecord const * propertyRecord;
  3442. strWithPtr->GetPropertyRecord(&propertyRecord); // lookup-cache propertyRecord
  3443. }
  3444. else
  3445. {
  3446. propertyString = strWithPtr->GetOrAddPropertyString();
  3447. // this is the second time this property string is used
  3448. // we already had created the propertyRecord..
  3449. // now create the propertyString!
  3450. }
  3451. }
  3452. if (propertyString != nullptr)
  3453. {
  3454. *propertyRecordUsageCache = propertyString->GetPropertyRecordUsageCache();
  3455. *cacheOwner = propertyString;
  3456. return true;
  3457. }
  3458. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  3459. if (PHASE_TRACE1(PropertyCachePhase))
  3460. {
  3461. Output::Print(_u("PropertyCache: GetElem No property string for '%s'\n"), string->GetString());
  3462. }
  3463. #endif
  3464. #if DBG_DUMP
  3465. scriptContext->forinNoCache++;
  3466. #endif
  3467. }
  3468. JavascriptSymbol* symbol = JavascriptOperators::TryFromVar<JavascriptSymbol>(index);
  3469. if (symbol)
  3470. {
  3471. *propertyRecordUsageCache = symbol->GetPropertyRecordUsageCache();
  3472. *cacheOwner = symbol;
  3473. return true;
  3474. }
  3475. return false;
  3476. }
  3477. bool JavascriptOperators::SetElementIOnTaggedNumber(
  3478. _In_ Var receiver,
  3479. _In_ RecyclableObject* object,
  3480. _In_ Var index,
  3481. _In_ Var value,
  3482. _In_ ScriptContext* requestContext,
  3483. _In_ PropertyOperationFlags propertyOperationFlags)
  3484. {
  3485. Assert(TaggedNumber::Is(receiver));
  3486. uint32 indexVal = 0;
  3487. PropertyRecord const * propertyRecord = nullptr;
  3488. IndexType indexType = GetIndexType(index, requestContext, &indexVal, &propertyRecord, true);
  3489. if (indexType == IndexType_Number)
  3490. {
  3491. return JavascriptOperators::SetItemOnTaggedNumber(receiver, object, indexVal, value, requestContext, propertyOperationFlags);
  3492. }
  3493. else
  3494. {
  3495. return JavascriptOperators::SetPropertyOnTaggedNumber(receiver, object, propertyRecord->GetPropertyId(), value, requestContext, propertyOperationFlags);
  3496. }
  3497. }
  3498. template <bool ReturnOperationInfo>
  3499. bool JavascriptOperators::SetElementIWithCache(
  3500. _In_ Var receiver,
  3501. _In_ RecyclableObject* object,
  3502. _In_ RecyclableObject* index,
  3503. _In_ Var value,
  3504. _In_ PropertyRecordUsageCache* propertyRecordUsageCache,
  3505. _In_ ScriptContext* scriptContext,
  3506. _In_ PropertyOperationFlags flags,
  3507. _Inout_opt_ PropertyCacheOperationInfo* operationInfo)
  3508. {
  3509. if (TaggedNumber::Is(receiver))
  3510. {
  3511. return JavascriptOperators::SetElementIOnTaggedNumber(receiver, object, index, value, scriptContext, flags);
  3512. }
  3513. PropertyRecord const * propertyRecord = propertyRecordUsageCache->GetPropertyRecord();
  3514. if (propertyRecord->IsNumeric())
  3515. {
  3516. return JavascriptOperators::SetItem(receiver, object, propertyRecord->GetNumericValue(), value, scriptContext, flags);
  3517. }
  3518. PropertyValueInfo info;
  3519. if (receiver == object)
  3520. {
  3521. if (propertyRecordUsageCache->TrySetPropertyFromCache<ReturnOperationInfo>(object, value, scriptContext, flags, &info, index, operationInfo))
  3522. {
  3523. return true;
  3524. }
  3525. }
  3526. PropertyId propId = propertyRecord->GetPropertyId();
  3527. if (propId == PropertyIds::NaN || propId == PropertyIds::Infinity)
  3528. {
  3529. // As we no longer convert o[x] into o.x for NaN and Infinity, we need to follow SetProperty convention for these,
  3530. // which would check for read-only properties, strict mode, etc.
  3531. // Note that "-Infinity" does not qualify as property name, so we don't have to take care of it.
  3532. return JavascriptOperators::SetProperty(receiver, object, propId, value, scriptContext, flags);
  3533. }
  3534. return JavascriptOperators::SetPropertyWPCache(receiver, object, propId, value, scriptContext, flags, &info);
  3535. }
  3536. template bool JavascriptOperators::SetElementIWithCache<false>(Var receiver, RecyclableObject* object, RecyclableObject* index, Var value, PropertyRecordUsageCache* propertyRecordUsageCache, ScriptContext* scriptContext, PropertyOperationFlags flags, PropertyCacheOperationInfo* operationInfo);
  3537. template bool JavascriptOperators::SetElementIWithCache<true>(Var receiver, RecyclableObject* object, RecyclableObject* index, Var value, PropertyRecordUsageCache* propertyRecordUsageCache, ScriptContext* scriptContext, PropertyOperationFlags flags, PropertyCacheOperationInfo* operationInfo);
  3538. template <bool ReturnOperationInfo>
  3539. Var JavascriptOperators::GetElementIWithCache(
  3540. _In_ Var instance,
  3541. _In_ RecyclableObject* index,
  3542. _In_ PropertyRecordUsageCache* propertyRecordUsageCache,
  3543. _In_ ScriptContext* scriptContext,
  3544. _Inout_opt_ PropertyCacheOperationInfo* operationInfo)
  3545. {
  3546. RecyclableObject* object = nullptr;
  3547. if (!JavascriptOperators::GetPropertyObjectForGetElementI(instance, index, scriptContext, &object))
  3548. {
  3549. return scriptContext->GetLibrary()->GetUndefined();
  3550. }
  3551. PropertyRecord const * propertyRecord = propertyRecordUsageCache->GetPropertyRecord();
  3552. Var value;
  3553. if (propertyRecord->IsNumeric())
  3554. {
  3555. if (JavascriptOperators::GetItem(instance, object, propertyRecord->GetNumericValue(), &value, scriptContext))
  3556. {
  3557. return value;
  3558. }
  3559. }
  3560. else
  3561. {
  3562. PropertyValueInfo info;
  3563. if (propertyRecordUsageCache->TryGetPropertyFromCache<false /* OwnPropertyOnly */, false /* OutputExistence */, ReturnOperationInfo>(instance, object, &value, scriptContext, &info, index, operationInfo))
  3564. {
  3565. return value;
  3566. }
  3567. if (JavascriptOperators::GetPropertyWPCache<false /* OutputExistence */>(instance, object, propertyRecord->GetPropertyId(), &value, scriptContext, &info))
  3568. {
  3569. return value;
  3570. }
  3571. }
  3572. return scriptContext->GetLibrary()->GetUndefined();
  3573. }
  3574. template Var JavascriptOperators::GetElementIWithCache<false>(Var instance, RecyclableObject* index, PropertyRecordUsageCache* propertyRecordUsageCache, ScriptContext* scriptContext, PropertyCacheOperationInfo* operationInfo);
  3575. template Var JavascriptOperators::GetElementIWithCache<true>(Var instance, RecyclableObject* index, PropertyRecordUsageCache* propertyRecordUsageCache, ScriptContext* scriptContext, PropertyCacheOperationInfo* operationInfo);
  3576. Var JavascriptOperators::GetElementIHelper(Var instance, Var index, Var receiver, ScriptContext* scriptContext)
  3577. {
  3578. RecyclableObject* object = nullptr;
  3579. if (!JavascriptOperators::GetPropertyObjectForGetElementI(instance, index, scriptContext, &object))
  3580. {
  3581. return scriptContext->GetLibrary()->GetUndefined();
  3582. }
  3583. uint32 indexVal;
  3584. PropertyRecord const * propertyRecord = nullptr;
  3585. JavascriptString * propertyNameString = nullptr;
  3586. Var value = nullptr;
  3587. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, &propertyNameString, false, true);
  3588. if (indexType == IndexType_Number)
  3589. {
  3590. if (JavascriptOperators::GetItem(receiver, object, indexVal, &value, scriptContext))
  3591. {
  3592. return value;
  3593. }
  3594. }
  3595. else if (indexType == IndexType_JavascriptString)
  3596. {
  3597. PropertyValueInfo info;
  3598. if (JavascriptOperators::GetPropertyWPCache<false /* OutputExistence */>(receiver, object, propertyNameString, &value, scriptContext, &info))
  3599. {
  3600. return value;
  3601. }
  3602. }
  3603. else
  3604. {
  3605. Assert(indexType == IndexType_PropertyId);
  3606. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  3607. {
  3608. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, &propertyNameString, true, true);
  3609. Assert(indexType == IndexType_PropertyId);
  3610. Assert(propertyRecord != nullptr);
  3611. }
  3612. if (propertyRecord != nullptr)
  3613. {
  3614. PropertyValueInfo info;
  3615. if (JavascriptOperators::GetPropertyWPCache<false /* OutputExistence */>(receiver, object, propertyRecord->GetPropertyId(), &value, scriptContext, &info))
  3616. {
  3617. return value;
  3618. }
  3619. }
  3620. #if DBG
  3621. else
  3622. {
  3623. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  3624. PropertyRecord const * debugPropertyRecord;
  3625. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  3626. 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");
  3627. }
  3628. #endif
  3629. }
  3630. return scriptContext->GetMissingItemResult();
  3631. }
  3632. int32 JavascriptOperators::OP_GetNativeIntElementI(Var instance, Var index)
  3633. {
  3634. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeIntElementI);
  3635. #if ENABLE_COPYONACCESS_ARRAY
  3636. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  3637. #endif
  3638. if (TaggedInt::Is(index))
  3639. {
  3640. int32 indexInt = TaggedInt::ToInt32(index);
  3641. if (indexInt < 0)
  3642. {
  3643. return JavascriptNativeIntArray::MissingItem;
  3644. }
  3645. JavascriptArray * arr = JavascriptArray::FromVar(instance);
  3646. int32 result;
  3647. if (arr->DirectGetItemAt((uint32)indexInt, &result))
  3648. {
  3649. return result;
  3650. }
  3651. }
  3652. else if (JavascriptNumber::Is_NoTaggedIntCheck(index))
  3653. {
  3654. int32 indexInt;
  3655. bool isInt32;
  3656. double dIndex = JavascriptNumber::GetValue(index);
  3657. if (JavascriptNumber::TryGetInt32OrUInt32Value(dIndex, &indexInt, &isInt32))
  3658. {
  3659. if (isInt32 && indexInt < 0)
  3660. {
  3661. return JavascriptNativeIntArray::MissingItem;
  3662. }
  3663. JavascriptArray * arr = JavascriptArray::FromVar(instance);
  3664. int32 result;
  3665. if (arr->DirectGetItemAt((uint32)indexInt, &result))
  3666. {
  3667. return result;
  3668. }
  3669. }
  3670. }
  3671. else
  3672. {
  3673. AssertMsg(false, "Non-numerical index in this helper?");
  3674. }
  3675. return JavascriptNativeIntArray::MissingItem;
  3676. JIT_HELPER_END(Op_GetNativeIntElementI);
  3677. }
  3678. int32 JavascriptOperators::OP_GetNativeIntElementI_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  3679. {
  3680. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeIntElementI_UInt32);
  3681. JIT_HELPER_SAME_ATTRIBUTES(Op_GetNativeIntElementI_UInt32, Op_GetNativeIntElementI);
  3682. #if FLOATVAR
  3683. return OP_GetNativeIntElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext));
  3684. #else
  3685. char buffer[sizeof(Js::JavascriptNumber)];
  3686. return OP_GetNativeIntElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3687. (Js::JavascriptNumber *)buffer));
  3688. #endif
  3689. JIT_HELPER_END(Op_GetNativeIntElementI_UInt32);
  3690. }
  3691. int32 JavascriptOperators::OP_GetNativeIntElementI_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  3692. {
  3693. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeIntElementI_Int32);
  3694. JIT_HELPER_SAME_ATTRIBUTES(Op_GetNativeIntElementI_Int32, Op_GetNativeIntElementI);
  3695. #if FLOATVAR
  3696. return OP_GetNativeIntElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext));
  3697. #else
  3698. char buffer[sizeof(Js::JavascriptNumber)];
  3699. return OP_GetNativeIntElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3700. (Js::JavascriptNumber *)buffer));
  3701. #endif
  3702. JIT_HELPER_END(Op_GetNativeIntElementI_Int32);
  3703. }
  3704. double JavascriptOperators::OP_GetNativeFloatElementI(Var instance, Var index)
  3705. {
  3706. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeFloatElementI);
  3707. double result = 0;
  3708. if (TaggedInt::Is(index))
  3709. {
  3710. int32 indexInt = TaggedInt::ToInt32(index);
  3711. if (indexInt < 0)
  3712. {
  3713. result = JavascriptNativeFloatArray::MissingItem;
  3714. }
  3715. else
  3716. {
  3717. JavascriptArray * arr = JavascriptArray::FromVar(instance);
  3718. if (!arr->DirectGetItemAt((uint32)indexInt, &result))
  3719. {
  3720. result = JavascriptNativeFloatArray::MissingItem;
  3721. }
  3722. }
  3723. }
  3724. else if (JavascriptNumber::Is_NoTaggedIntCheck(index))
  3725. {
  3726. int32 indexInt;
  3727. bool isInt32;
  3728. double dIndex = JavascriptNumber::GetValue(index);
  3729. if (JavascriptNumber::TryGetInt32OrUInt32Value(dIndex, &indexInt, &isInt32))
  3730. {
  3731. if (isInt32 && indexInt < 0)
  3732. {
  3733. result = JavascriptNativeFloatArray::MissingItem;
  3734. }
  3735. else
  3736. {
  3737. JavascriptArray * arr = JavascriptArray::FromVar(instance);
  3738. if (!arr->DirectGetItemAt((uint32)indexInt, &result))
  3739. {
  3740. result = JavascriptNativeFloatArray::MissingItem;
  3741. }
  3742. }
  3743. }
  3744. }
  3745. else
  3746. {
  3747. AssertMsg(false, "Non-numerical index in this helper?");
  3748. }
  3749. return result;
  3750. JIT_HELPER_END(Op_GetNativeFloatElementI);
  3751. }
  3752. double JavascriptOperators::OP_GetNativeFloatElementI_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  3753. {
  3754. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeFloatElementI_UInt32);
  3755. JIT_HELPER_SAME_ATTRIBUTES(Op_GetNativeFloatElementI_UInt32, Op_GetNativeFloatElementI);
  3756. #if FLOATVAR
  3757. return OP_GetNativeFloatElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext));
  3758. #else
  3759. char buffer[sizeof(Js::JavascriptNumber)];
  3760. return OP_GetNativeFloatElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3761. (Js::JavascriptNumber *)buffer));
  3762. #endif
  3763. JIT_HELPER_END(Op_GetNativeFloatElementI_UInt32);
  3764. }
  3765. double JavascriptOperators::OP_GetNativeFloatElementI_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  3766. {
  3767. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeFloatElementI_Int32);
  3768. JIT_HELPER_SAME_ATTRIBUTES(Op_GetNativeFloatElementI_Int32, Op_GetNativeFloatElementI);
  3769. #if FLOATVAR
  3770. return OP_GetNativeFloatElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext));
  3771. #else
  3772. char buffer[sizeof(Js::JavascriptNumber)];
  3773. return OP_GetNativeFloatElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3774. (Js::JavascriptNumber *)buffer));
  3775. #endif
  3776. JIT_HELPER_END(Op_GetNativeFloatElementI_Int32);
  3777. }
  3778. Var JavascriptOperators::OP_GetMethodElement_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  3779. {
  3780. JIT_HELPER_REENTRANT_HEADER(Op_GetMethodElement_UInt32);
  3781. JIT_HELPER_SAME_ATTRIBUTES(Op_GetMethodElement_UInt32, Op_GetMethodElement);
  3782. #if FLOATVAR
  3783. return OP_GetMethodElement(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  3784. #else
  3785. char buffer[sizeof(Js::JavascriptNumber)];
  3786. return OP_GetMethodElement(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3787. (Js::JavascriptNumber *)buffer), scriptContext);
  3788. #endif
  3789. JIT_HELPER_END(Op_GetMethodElement_UInt32);
  3790. }
  3791. Var JavascriptOperators::OP_GetMethodElement_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  3792. {
  3793. JIT_HELPER_REENTRANT_HEADER(Op_GetMethodElement_Int32);
  3794. JIT_HELPER_SAME_ATTRIBUTES(Op_GetMethodElement_Int32, Op_GetMethodElement);
  3795. #if FLOATVAR
  3796. return OP_GetElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  3797. #else
  3798. char buffer[sizeof(Js::JavascriptNumber)];
  3799. return OP_GetMethodElement(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3800. (Js::JavascriptNumber *)buffer), scriptContext);
  3801. #endif
  3802. JIT_HELPER_END(Op_GetMethodElement_Int32);
  3803. }
  3804. Var JavascriptOperators::OP_GetMethodElement(Var instance, Var index, ScriptContext* scriptContext)
  3805. {
  3806. JIT_HELPER_REENTRANT_HEADER(Op_GetMethodElement);
  3807. RecyclableObject* object = nullptr;
  3808. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  3809. {
  3810. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined, GetPropertyDisplayNameForError(index, scriptContext));
  3811. }
  3812. ThreadContext* threadContext = scriptContext->GetThreadContext();
  3813. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  3814. threadContext->ClearImplicitCallFlags();
  3815. uint32 indexVal;
  3816. PropertyRecord const * propertyRecord = nullptr;
  3817. Var value = NULL;
  3818. BOOL hasProperty = FALSE;
  3819. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, false);
  3820. if (indexType == IndexType_Number)
  3821. {
  3822. hasProperty = JavascriptOperators::GetItemReference(instance, object, indexVal, &value, scriptContext);
  3823. }
  3824. else
  3825. {
  3826. Assert(indexType == IndexType_PropertyId);
  3827. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  3828. {
  3829. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
  3830. Assert(indexType == IndexType_PropertyId);
  3831. Assert(propertyRecord != nullptr);
  3832. }
  3833. if (propertyRecord != nullptr)
  3834. {
  3835. hasProperty = JavascriptOperators::GetPropertyReference(instance, object, propertyRecord->GetPropertyId(), &value, scriptContext, NULL);
  3836. }
  3837. #if DBG
  3838. else
  3839. {
  3840. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  3841. PropertyRecord const * debugPropertyRecord;
  3842. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  3843. AssertMsg(!JavascriptOperators::GetPropertyReference(instance, object, debugPropertyRecord->GetPropertyId(), &value, scriptContext, NULL),
  3844. "how did this property come? See OS Bug 2727708 if you see this come from the web");
  3845. }
  3846. #endif
  3847. }
  3848. if (!hasProperty)
  3849. {
  3850. JavascriptString* varName = nullptr;
  3851. if (indexType == IndexType_PropertyId && propertyRecord != nullptr && propertyRecord->IsSymbol())
  3852. {
  3853. varName = JavascriptSymbol::ToString(propertyRecord, scriptContext);
  3854. }
  3855. else
  3856. {
  3857. varName = JavascriptConversion::ToString(index, scriptContext);
  3858. }
  3859. // 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
  3860. // first (#3). Postpone throwing error to invoke time.
  3861. value = ThrowErrorObject::CreateThrowTypeErrorObject(scriptContext, VBSERR_OLENoPropOrMethod, varName);
  3862. }
  3863. else if(!JavascriptConversion::IsCallable(value))
  3864. {
  3865. // 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
  3866. // first (#3). Postpone throwing error to invoke time.
  3867. JavascriptString* varName = JavascriptConversion::ToString(index, scriptContext);
  3868. value = ThrowErrorObject::CreateThrowTypeErrorObject(scriptContext, JSERR_Property_NeedFunction, varName);
  3869. }
  3870. threadContext->CheckAndResetImplicitCallAccessorFlag();
  3871. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  3872. return value;
  3873. JIT_HELPER_END(Op_GetMethodElement);
  3874. }
  3875. BOOL JavascriptOperators::OP_SetElementI_UInt32(Var instance, uint32 index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  3876. {
  3877. JIT_HELPER_REENTRANT_HEADER(Op_SetElementI_UInt32);
  3878. JIT_HELPER_SAME_ATTRIBUTES(Op_SetElementI_UInt32, Op_SetElementI);
  3879. #if FLOATVAR
  3880. return OP_SetElementI_JIT(instance, Js::JavascriptNumber::ToVar(index, scriptContext), value, scriptContext, flags);
  3881. #else
  3882. char buffer[sizeof(Js::JavascriptNumber)];
  3883. return OP_SetElementI_JIT(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3884. (Js::JavascriptNumber *)buffer), value, scriptContext, flags);
  3885. #endif
  3886. JIT_HELPER_END(Op_SetElementI_UInt32);
  3887. }
  3888. BOOL JavascriptOperators::OP_SetElementI_Int32(Var instance, int32 index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  3889. {
  3890. JIT_HELPER_REENTRANT_HEADER(Op_SetElementI_Int32);
  3891. JIT_HELPER_SAME_ATTRIBUTES(Op_SetElementI_Int32, Op_SetElementI);
  3892. #if FLOATVAR
  3893. return OP_SetElementI_JIT(instance, Js::JavascriptNumber::ToVar(index, scriptContext), value, scriptContext, flags);
  3894. #else
  3895. char buffer[sizeof(Js::JavascriptNumber)];
  3896. return OP_SetElementI_JIT(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3897. (Js::JavascriptNumber *)buffer), value, scriptContext, flags);
  3898. #endif
  3899. JIT_HELPER_END(Op_SetElementI_Int32);
  3900. }
  3901. BOOL JavascriptOperators::OP_SetElementI_JIT(Var instance, Var index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  3902. {
  3903. JIT_HELPER_REENTRANT_HEADER(Op_SetElementI);
  3904. if (TaggedNumber::Is(instance))
  3905. {
  3906. return OP_SetElementI(instance, index, value, scriptContext, flags);
  3907. }
  3908. INT_PTR vt = VirtualTableInfoBase::GetVirtualTable(instance);
  3909. OP_SetElementI(instance, index, value, scriptContext, flags);
  3910. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  3911. JIT_HELPER_END(Op_SetElementI);
  3912. }
  3913. BOOL JavascriptOperators::OP_SetElementI(Var instance, Var index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  3914. {
  3915. #if ENABLE_COPYONACCESS_ARRAY
  3916. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  3917. #endif
  3918. TypeId instanceType = JavascriptOperators::GetTypeId(instance);
  3919. bool isTypedArray = (instanceType >= TypeIds_Int8Array && instanceType <= TypeIds_Float64Array);
  3920. if (isTypedArray)
  3921. {
  3922. if (TaggedInt::Is(index) || JavascriptNumber::Is_NoTaggedIntCheck(index) || JavascriptString::Is(index))
  3923. {
  3924. BOOL returnValue = FALSE;
  3925. bool isNumericIndex = false;
  3926. // CrossSite types will go down the slow path.
  3927. switch (instanceType)
  3928. {
  3929. case TypeIds_Int8Array:
  3930. {
  3931. // The typed array will deal with all possible values for the index
  3932. if (VirtualTableInfo<Int8VirtualArray>::HasVirtualTable(instance))
  3933. {
  3934. Int8VirtualArray* int8Array = Int8VirtualArray::UnsafeFromVar(instance);
  3935. returnValue = int8Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3936. }
  3937. else if( VirtualTableInfo<Int8Array>::HasVirtualTable(instance))
  3938. {
  3939. Int8Array* int8Array = Int8Array::UnsafeFromVar(instance);
  3940. returnValue = int8Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3941. }
  3942. break;
  3943. }
  3944. case TypeIds_Uint8Array:
  3945. {
  3946. // The typed array will deal with all possible values for the index
  3947. if (VirtualTableInfo<Uint8VirtualArray>::HasVirtualTable(instance))
  3948. {
  3949. Uint8VirtualArray* uint8Array = Uint8VirtualArray::UnsafeFromVar(instance);
  3950. returnValue = uint8Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3951. }
  3952. else if (VirtualTableInfo<Uint8Array>::HasVirtualTable(instance))
  3953. {
  3954. Uint8Array* uint8Array = Uint8Array::UnsafeFromVar(instance);
  3955. returnValue = uint8Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3956. }
  3957. break;
  3958. }
  3959. case TypeIds_Uint8ClampedArray:
  3960. {
  3961. // The typed array will deal with all possible values for the index
  3962. if (VirtualTableInfo<Uint8ClampedVirtualArray>::HasVirtualTable(instance))
  3963. {
  3964. Uint8ClampedVirtualArray* uint8ClampedArray = Uint8ClampedVirtualArray::UnsafeFromVar(instance);
  3965. returnValue = uint8ClampedArray->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3966. }
  3967. else if(VirtualTableInfo<Uint8ClampedArray>::HasVirtualTable(instance))
  3968. {
  3969. Uint8ClampedArray* uint8ClampedArray = Uint8ClampedArray::UnsafeFromVar(instance);
  3970. returnValue = uint8ClampedArray->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3971. }
  3972. break;
  3973. }
  3974. case TypeIds_Int16Array:
  3975. {
  3976. // The type array will deal with all possible values for the index
  3977. if (VirtualTableInfo<Int16VirtualArray>::HasVirtualTable(instance))
  3978. {
  3979. Int16VirtualArray* int16Array = Int16VirtualArray::UnsafeFromVar(instance);
  3980. returnValue = int16Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3981. }
  3982. else if (VirtualTableInfo<Int16Array>::HasVirtualTable(instance))
  3983. {
  3984. Int16Array* int16Array = Int16Array::UnsafeFromVar(instance);
  3985. returnValue = int16Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3986. }
  3987. break;
  3988. }
  3989. case TypeIds_Uint16Array:
  3990. {
  3991. // The type array will deal with all possible values for the index
  3992. if (VirtualTableInfo<Uint16VirtualArray>::HasVirtualTable(instance))
  3993. {
  3994. Uint16VirtualArray* uint16Array = Uint16VirtualArray::UnsafeFromVar(instance);
  3995. returnValue = uint16Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3996. }
  3997. else if (VirtualTableInfo<Uint16Array>::HasVirtualTable(instance))
  3998. {
  3999. Uint16Array* uint16Array = Uint16Array::UnsafeFromVar(instance);
  4000. returnValue = uint16Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4001. }
  4002. break;
  4003. }
  4004. case TypeIds_Int32Array:
  4005. {
  4006. // The type array will deal with all possible values for the index
  4007. if (VirtualTableInfo<Int32VirtualArray>::HasVirtualTable(instance))
  4008. {
  4009. Int32VirtualArray* int32Array = Int32VirtualArray::UnsafeFromVar(instance);
  4010. returnValue = int32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4011. }
  4012. else if(VirtualTableInfo<Int32Array>::HasVirtualTable(instance))
  4013. {
  4014. Int32Array* int32Array = Int32Array::UnsafeFromVar(instance);
  4015. returnValue = int32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4016. }
  4017. break;
  4018. }
  4019. case TypeIds_Uint32Array:
  4020. {
  4021. // The type array will deal with all possible values for the index
  4022. if (VirtualTableInfo<Uint32VirtualArray>::HasVirtualTable(instance))
  4023. {
  4024. Uint32VirtualArray* uint32Array = Uint32VirtualArray::UnsafeFromVar(instance);
  4025. returnValue = uint32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4026. }
  4027. else if (VirtualTableInfo<Uint32Array>::HasVirtualTable(instance))
  4028. {
  4029. Uint32Array* uint32Array = Uint32Array::UnsafeFromVar(instance);
  4030. returnValue = uint32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4031. }
  4032. break;
  4033. }
  4034. case TypeIds_Float32Array:
  4035. {
  4036. // The type array will deal with all possible values for the index
  4037. if (VirtualTableInfo<Float32VirtualArray>::HasVirtualTable(instance))
  4038. {
  4039. Float32VirtualArray* float32Array = Float32VirtualArray::UnsafeFromVar(instance);
  4040. returnValue = float32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4041. }
  4042. else if (VirtualTableInfo<Float32Array>::HasVirtualTable(instance))
  4043. {
  4044. Float32Array* float32Array = Float32Array::UnsafeFromVar(instance);
  4045. returnValue = float32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4046. }
  4047. break;
  4048. }
  4049. case TypeIds_Float64Array:
  4050. {
  4051. // The type array will deal with all possible values for the index
  4052. if (VirtualTableInfo<Float64VirtualArray>::HasVirtualTable(instance))
  4053. {
  4054. Float64VirtualArray* float64Array = Float64VirtualArray::UnsafeFromVar(instance);
  4055. returnValue = float64Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4056. }
  4057. else if (VirtualTableInfo<Float64Array>::HasVirtualTable(instance))
  4058. {
  4059. Float64Array* float64Array = Float64Array::UnsafeFromVar(instance);
  4060. returnValue = float64Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4061. }
  4062. break;
  4063. }
  4064. }
  4065. // if this was numeric index, return operation status else
  4066. // Return the result of calling the default ordinary object [[Set]] internal method (9.1.8) on O passing P, V, and Receiver as arguments.
  4067. if (isNumericIndex)
  4068. return returnValue;
  4069. }
  4070. }
  4071. else
  4072. {
  4073. if (TaggedInt::Is(index))
  4074. {
  4075. TaggedIntIndex:
  4076. switch (instanceType)
  4077. {
  4078. case TypeIds_NativeIntArray:
  4079. case TypeIds_NativeFloatArray:
  4080. case TypeIds_Array: // fast path for array
  4081. {
  4082. int indexInt = TaggedInt::ToInt32(index);
  4083. if (indexInt >= 0 && scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4084. {
  4085. JavascriptArray::UnsafeFromVar(instance)->SetItem((uint32)indexInt, value, flags);
  4086. return TRUE;
  4087. }
  4088. break;
  4089. }
  4090. }
  4091. }
  4092. else if (JavascriptNumber::Is_NoTaggedIntCheck(index))
  4093. {
  4094. double dIndexValue = JavascriptNumber::GetValue(index);
  4095. uint32 uint32Index = JavascriptConversion::ToUInt32(index, scriptContext);
  4096. if ((double)uint32Index == dIndexValue && !TaggedInt::IsOverflow(uint32Index))
  4097. {
  4098. index = TaggedInt::ToVarUnchecked(uint32Index);
  4099. goto TaggedIntIndex;
  4100. }
  4101. }
  4102. }
  4103. RecyclableObject* object = nullptr;
  4104. if (!GetPropertyObjectForSetElementI(instance, index, scriptContext, &object))
  4105. {
  4106. return FALSE;
  4107. }
  4108. return JavascriptOperators::SetElementIHelper(instance, object, index, value, scriptContext, flags);
  4109. }
  4110. BOOL JavascriptOperators::SetElementIHelper(Var receiver, RecyclableObject* object, Var index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  4111. {
  4112. IndexType indexType;
  4113. uint32 indexVal = 0;
  4114. PropertyRecord const * propertyRecord = nullptr;
  4115. JavascriptString * propertyNameString = nullptr;
  4116. PropertyValueInfo propertyValueInfo;
  4117. RecyclableObject* cacheOwner;
  4118. PropertyRecordUsageCache* propertyRecordUsageCache;
  4119. if (JavascriptOperators::GetPropertyRecordUsageCache(index, scriptContext, &propertyRecordUsageCache, &cacheOwner))
  4120. {
  4121. return JavascriptOperators::SetElementIWithCache<false>(receiver, object, cacheOwner, value, propertyRecordUsageCache, scriptContext, flags, nullptr);
  4122. }
  4123. if (TaggedNumber::Is(receiver))
  4124. {
  4125. return JavascriptOperators::SetElementIOnTaggedNumber(receiver, object, index, value, scriptContext, flags);
  4126. }
  4127. #if DBG_DUMP
  4128. scriptContext->forinNoCache += (!TaggedInt::Is(index) && JavascriptString::Is(index));
  4129. #endif
  4130. indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, &propertyNameString, false, true);
  4131. if (scriptContext->GetThreadContext()->IsDisableImplicitCall() &&
  4132. scriptContext->GetThreadContext()->GetImplicitCallFlags() != ImplicitCall_None)
  4133. {
  4134. // We hit an implicit call trying to convert the index, and implicit calls are disabled, so
  4135. // quit before we try to store the element.
  4136. return FALSE;
  4137. }
  4138. if (indexType == IndexType_Number)
  4139. {
  4140. SetElementIHelper_INDEX_TYPE_IS_NUMBER:
  4141. return JavascriptOperators::SetItem(receiver, object, indexVal, value, scriptContext, flags);
  4142. }
  4143. else if (indexType == IndexType_JavascriptString)
  4144. {
  4145. Assert(propertyNameString);
  4146. // At this point, we know that the propertyNameString is neither PropertyString
  4147. // or LiteralStringWithPropertyStringPtr.. Get PropertyRecord!
  4148. // we will get it anyways otherwise. (Also, 1:1 string comparison for Builtin types will be expensive.)
  4149. if (propertyRecord == nullptr)
  4150. {
  4151. scriptContext->GetOrAddPropertyRecord(propertyNameString, &propertyRecord);
  4152. if (propertyRecord->IsNumeric())
  4153. {
  4154. indexVal = propertyRecord->GetNumericValue();
  4155. goto SetElementIHelper_INDEX_TYPE_IS_NUMBER;
  4156. }
  4157. }
  4158. }
  4159. Assert(indexType == IndexType_PropertyId || indexType == IndexType_JavascriptString);
  4160. Assert(propertyRecord);
  4161. return JavascriptOperators::SetProperty(receiver, object, propertyRecord->GetPropertyId(), value, scriptContext, flags);
  4162. }
  4163. BOOL JavascriptOperators::OP_SetNativeIntElementI(
  4164. Var instance,
  4165. Var aElementIndex,
  4166. int32 iValue,
  4167. ScriptContext* scriptContext,
  4168. PropertyOperationFlags flags)
  4169. {
  4170. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI);
  4171. INT_PTR vt = (INT_PTR)nullptr;
  4172. vt = VirtualTableInfoBase::GetVirtualTable(instance);
  4173. if (TaggedInt::Is(aElementIndex))
  4174. {
  4175. int32 indexInt = TaggedInt::ToInt32(aElementIndex);
  4176. if (indexInt >= 0 && scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4177. {
  4178. JavascriptNativeIntArray *arr = JavascriptNativeIntArray::FromVar(instance);
  4179. if (!(arr->TryGrowHeadSegmentAndSetItem<int32, JavascriptNativeIntArray>((uint32)indexInt, iValue)))
  4180. {
  4181. arr->SetItem(indexInt, iValue);
  4182. }
  4183. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4184. }
  4185. }
  4186. JavascriptOperators::OP_SetElementI(instance, aElementIndex, JavascriptNumber::ToVar(iValue, scriptContext), scriptContext, flags);
  4187. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4188. JIT_HELPER_END(Op_SetNativeIntElementI);
  4189. }
  4190. BOOL JavascriptOperators::OP_SetNativeIntElementI_UInt32(
  4191. Var instance,
  4192. uint32 aElementIndex,
  4193. int32 iValue,
  4194. ScriptContext* scriptContext,
  4195. PropertyOperationFlags flags)
  4196. {
  4197. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_UInt32);
  4198. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_UInt32, Op_SetNativeIntElementI);
  4199. #if FLOATVAR
  4200. return OP_SetNativeIntElementI(instance, Js::JavascriptNumber::ToVar(aElementIndex, scriptContext), iValue, scriptContext, flags);
  4201. #else
  4202. char buffer[sizeof(Js::JavascriptNumber)];
  4203. return OP_SetNativeIntElementI(instance, Js::JavascriptNumber::ToVarInPlace(aElementIndex, scriptContext,
  4204. (Js::JavascriptNumber *)buffer), iValue, scriptContext, flags);
  4205. #endif
  4206. JIT_HELPER_END(Op_SetNativeIntElementI_UInt32);
  4207. }
  4208. BOOL JavascriptOperators::OP_SetNativeIntElementI_Int32(
  4209. Var instance,
  4210. int aElementIndex,
  4211. int32 iValue,
  4212. ScriptContext* scriptContext,
  4213. PropertyOperationFlags flags)
  4214. {
  4215. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_Int32);
  4216. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_Int32, Op_SetNativeIntElementI);
  4217. #if FLOATVAR
  4218. return OP_SetNativeIntElementI(instance, Js::JavascriptNumber::ToVar(aElementIndex, scriptContext), iValue, scriptContext, flags);
  4219. #else
  4220. char buffer[sizeof(Js::JavascriptNumber)];
  4221. return OP_SetNativeIntElementI(instance, Js::JavascriptNumber::ToVarInPlace(aElementIndex, scriptContext,
  4222. (Js::JavascriptNumber *)buffer), iValue, scriptContext, flags);
  4223. #endif
  4224. JIT_HELPER_END(Op_SetNativeIntElementI_Int32);
  4225. }
  4226. BOOL JavascriptOperators::OP_SetNativeFloatElementI(
  4227. Var instance,
  4228. Var aElementIndex,
  4229. ScriptContext* scriptContext,
  4230. PropertyOperationFlags flags,
  4231. double dValue)
  4232. {
  4233. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI);
  4234. INT_PTR vt = (INT_PTR)nullptr;
  4235. vt = VirtualTableInfoBase::GetVirtualTable(instance);
  4236. if (TaggedInt::Is(aElementIndex))
  4237. {
  4238. int32 indexInt = TaggedInt::ToInt32(aElementIndex);
  4239. if (indexInt >= 0 && scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4240. {
  4241. JavascriptNativeFloatArray *arr = JavascriptNativeFloatArray::FromVar(instance);
  4242. if (!(arr->TryGrowHeadSegmentAndSetItem<double, JavascriptNativeFloatArray>((uint32)indexInt, dValue)))
  4243. {
  4244. arr->SetItem(indexInt, dValue);
  4245. }
  4246. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4247. }
  4248. }
  4249. JavascriptOperators::OP_SetElementI(instance, aElementIndex, JavascriptNumber::ToVarWithCheck(dValue, scriptContext), scriptContext, flags);
  4250. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4251. JIT_HELPER_END(Op_SetNativeFloatElementI);
  4252. }
  4253. BOOL JavascriptOperators::OP_SetNativeFloatElementI_UInt32(
  4254. Var instance,
  4255. uint32 aElementIndex,
  4256. ScriptContext* scriptContext,
  4257. PropertyOperationFlags flags,
  4258. double dValue)
  4259. {
  4260. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_UInt32);
  4261. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_UInt32, Op_SetNativeFloatElementI);
  4262. #if FLOATVAR
  4263. return OP_SetNativeFloatElementI(instance, JavascriptNumber::ToVar(aElementIndex, scriptContext), scriptContext, flags, dValue);
  4264. #else
  4265. char buffer[sizeof(Js::JavascriptNumber)];
  4266. return OP_SetNativeFloatElementI(instance, JavascriptNumber::ToVarInPlace(aElementIndex, scriptContext,
  4267. (Js::JavascriptNumber *)buffer), scriptContext, flags, dValue);
  4268. #endif
  4269. JIT_HELPER_END(Op_SetNativeFloatElementI_UInt32);
  4270. }
  4271. BOOL JavascriptOperators::OP_SetNativeFloatElementI_Int32(
  4272. Var instance,
  4273. int aElementIndex,
  4274. ScriptContext* scriptContext,
  4275. PropertyOperationFlags flags,
  4276. double dValue)
  4277. {
  4278. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_Int32);
  4279. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_Int32, Op_SetNativeFloatElementI);
  4280. #if FLOATVAR
  4281. return OP_SetNativeFloatElementI(instance, JavascriptNumber::ToVar(aElementIndex, scriptContext), scriptContext, flags, dValue);
  4282. #else
  4283. char buffer[sizeof(Js::JavascriptNumber)];
  4284. return OP_SetNativeFloatElementI(instance, JavascriptNumber::ToVarInPlace(aElementIndex, scriptContext,
  4285. (Js::JavascriptNumber *)buffer), scriptContext, flags, dValue);
  4286. #endif
  4287. JIT_HELPER_END(Op_SetNativeFloatElementI_Int32);
  4288. }
  4289. BOOL JavascriptOperators::OP_Memcopy(Var dstInstance, int32 dstStart, Var srcInstance, int32 srcStart, int32 length, ScriptContext* scriptContext)
  4290. {
  4291. JIT_HELPER_NOT_REENTRANT_HEADER(Op_Memcopy, reentrancylock, scriptContext->GetThreadContext());
  4292. if (length <= 0)
  4293. {
  4294. return false;
  4295. }
  4296. TypeId instanceType = JavascriptOperators::GetTypeId(srcInstance);
  4297. if (instanceType != JavascriptOperators::GetTypeId(dstInstance))
  4298. {
  4299. return false;
  4300. }
  4301. if (srcStart != dstStart)
  4302. {
  4303. return false;
  4304. }
  4305. BOOL returnValue = false;
  4306. #define MEMCOPY_TYPED_ARRAY(type, conversion) type ## ::FromVar(dstInstance)->DirectSetItemAtRange( type ## ::FromVar(srcInstance), srcStart, dstStart, length, JavascriptConversion:: ## conversion)
  4307. switch (instanceType)
  4308. {
  4309. case TypeIds_Int8Array:
  4310. {
  4311. returnValue = MEMCOPY_TYPED_ARRAY(Int8Array, ToInt8);
  4312. break;
  4313. }
  4314. case TypeIds_Uint8Array:
  4315. {
  4316. returnValue = MEMCOPY_TYPED_ARRAY(Uint8Array, ToUInt8);
  4317. break;
  4318. }
  4319. case TypeIds_Uint8ClampedArray:
  4320. {
  4321. returnValue = MEMCOPY_TYPED_ARRAY(Uint8ClampedArray, ToUInt8Clamped);
  4322. break;
  4323. }
  4324. case TypeIds_Int16Array:
  4325. {
  4326. returnValue = MEMCOPY_TYPED_ARRAY(Int16Array, ToInt16);
  4327. break;
  4328. }
  4329. case TypeIds_Uint16Array:
  4330. {
  4331. returnValue = MEMCOPY_TYPED_ARRAY(Uint16Array, ToUInt16);
  4332. break;
  4333. }
  4334. case TypeIds_Int32Array:
  4335. {
  4336. returnValue = MEMCOPY_TYPED_ARRAY(Int32Array, ToInt32);
  4337. break;
  4338. }
  4339. case TypeIds_Uint32Array:
  4340. {
  4341. returnValue = MEMCOPY_TYPED_ARRAY(Uint32Array, ToUInt32);
  4342. break;
  4343. }
  4344. case TypeIds_Float32Array:
  4345. {
  4346. returnValue = MEMCOPY_TYPED_ARRAY(Float32Array, ToFloat);
  4347. break;
  4348. }
  4349. case TypeIds_Float64Array:
  4350. {
  4351. returnValue = MEMCOPY_TYPED_ARRAY(Float64Array, ToNumber);
  4352. break;
  4353. }
  4354. case TypeIds_Array:
  4355. case TypeIds_NativeFloatArray:
  4356. case TypeIds_NativeIntArray:
  4357. {
  4358. if (dstStart < 0 || srcStart < 0)
  4359. {
  4360. // This is not supported, Bailout
  4361. break;
  4362. }
  4363. // Upper bounds check for source array
  4364. JavascriptArray* srcArray = JavascriptArray::UnsafeFromVar(srcInstance);
  4365. JavascriptArray* dstArray = JavascriptArray::FromVar(dstInstance);
  4366. if (scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4367. {
  4368. INT_PTR vt = VirtualTableInfoBase::GetVirtualTable(dstInstance);
  4369. if (instanceType == TypeIds_Array)
  4370. {
  4371. returnValue = dstArray->DirectSetItemAtRangeFromArray<Var>(dstStart, length, srcArray, srcStart);
  4372. }
  4373. else if (instanceType == TypeIds_NativeIntArray)
  4374. {
  4375. returnValue = dstArray->DirectSetItemAtRangeFromArray<int32>(dstStart, length, srcArray, srcStart);
  4376. }
  4377. else
  4378. {
  4379. returnValue = dstArray->DirectSetItemAtRangeFromArray<double>(dstStart, length, srcArray, srcStart);
  4380. }
  4381. returnValue &= vt == VirtualTableInfoBase::GetVirtualTable(dstInstance);
  4382. }
  4383. break;
  4384. }
  4385. default:
  4386. AssertMsg(false, "We don't support this type for memcopy yet.");
  4387. break;
  4388. }
  4389. #undef MEMCOPY_TYPED_ARRAY
  4390. return returnValue;
  4391. JIT_HELPER_END(Op_Memcopy);
  4392. }
  4393. template<typename T, T(*func)(Var, ScriptContext*)> bool MemsetConversion(Var value, ScriptContext* scriptContext, T* result)
  4394. {
  4395. ImplicitCallFlags flags = scriptContext->GetThreadContext()->TryWithDisabledImplicitCall([&]
  4396. {
  4397. *result = func(value, scriptContext);
  4398. });
  4399. return (flags & (~ImplicitCall_None)) == 0;
  4400. }
  4401. BOOL JavascriptOperators::OP_Memset(Var instance, int32 start, Var value, int32 length, ScriptContext* scriptContext)
  4402. {
  4403. JIT_HELPER_NOT_REENTRANT_HEADER(Op_Memset, reentrancylock, scriptContext->GetThreadContext());
  4404. if (length <= 0)
  4405. {
  4406. return false;
  4407. }
  4408. TypeId instanceType = JavascriptOperators::GetTypeId(instance);
  4409. BOOL returnValue = false;
  4410. // The typed array will deal with all possible values for the index
  4411. #define MEMSET_TYPED_ARRAY_CASE(type, conversion) \
  4412. case TypeIds_##type: \
  4413. { \
  4414. type## ::TypedArrayType typedValue = 0; \
  4415. if (!MemsetConversion<type## ::TypedArrayType, JavascriptConversion:: ##conversion>(value, scriptContext, &typedValue)) return false; \
  4416. returnValue = type## ::FromVar(instance)->DirectSetItemAtRange(start, length, typedValue); \
  4417. break; \
  4418. }
  4419. switch (instanceType)
  4420. {
  4421. MEMSET_TYPED_ARRAY_CASE(Int8Array, ToInt8)
  4422. MEMSET_TYPED_ARRAY_CASE(Uint8Array, ToUInt8)
  4423. MEMSET_TYPED_ARRAY_CASE(Uint8ClampedArray, ToUInt8Clamped)
  4424. MEMSET_TYPED_ARRAY_CASE(Int16Array, ToInt16)
  4425. MEMSET_TYPED_ARRAY_CASE(Uint16Array, ToUInt16)
  4426. MEMSET_TYPED_ARRAY_CASE(Int32Array, ToInt32)
  4427. MEMSET_TYPED_ARRAY_CASE(Uint32Array, ToUInt32)
  4428. MEMSET_TYPED_ARRAY_CASE(Float32Array, ToFloat)
  4429. MEMSET_TYPED_ARRAY_CASE(Float64Array, ToNumber)
  4430. case TypeIds_NativeFloatArray:
  4431. case TypeIds_NativeIntArray:
  4432. case TypeIds_Array:
  4433. {
  4434. if (start < 0)
  4435. {
  4436. for (start; start < 0 && length > 0; ++start, --length)
  4437. {
  4438. if (!OP_SetElementI(instance, JavascriptNumber::ToVar(start, scriptContext), value, scriptContext))
  4439. {
  4440. return false;
  4441. }
  4442. }
  4443. }
  4444. if (scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4445. {
  4446. INT_PTR vt = VirtualTableInfoBase::GetVirtualTable(instance);
  4447. if (instanceType == TypeIds_Array)
  4448. {
  4449. returnValue = JavascriptArray::UnsafeFromVar(instance)->DirectSetItemAtRange<Var>(start, length, value);
  4450. }
  4451. else if (instanceType == TypeIds_NativeIntArray)
  4452. {
  4453. // Only accept tagged int.
  4454. if (!TaggedInt::Is(value))
  4455. {
  4456. return false;
  4457. }
  4458. int32 intValue = 0;
  4459. if (!MemsetConversion<int32, JavascriptConversion::ToInt32>(value, scriptContext, &intValue))
  4460. {
  4461. return false;
  4462. }
  4463. // Special case for missing item
  4464. if (SparseArraySegment<int32>::IsMissingItem(&intValue))
  4465. {
  4466. return false;
  4467. }
  4468. returnValue = JavascriptArray::UnsafeFromVar(instance)->DirectSetItemAtRange<int32>(start, length, intValue);
  4469. }
  4470. else
  4471. {
  4472. // For native float arrays, the jit doesn't check the type of the source so we have to do it here
  4473. if (!JavascriptNumber::Is(value) && !TaggedNumber::Is(value))
  4474. {
  4475. return false;
  4476. }
  4477. double doubleValue = 0;
  4478. if (!MemsetConversion<double, JavascriptConversion::ToNumber>(value, scriptContext, &doubleValue))
  4479. {
  4480. return false;
  4481. }
  4482. // Special case for missing item
  4483. if (SparseArraySegment<double>::IsMissingItem(&doubleValue))
  4484. {
  4485. return false;
  4486. }
  4487. returnValue = JavascriptArray::UnsafeFromVar(instance)->DirectSetItemAtRange<double>(start, length, doubleValue);
  4488. }
  4489. returnValue &= vt == VirtualTableInfoBase::GetVirtualTable(instance);
  4490. }
  4491. break;
  4492. }
  4493. default:
  4494. AssertMsg(false, "We don't support this type for memset yet.");
  4495. break;
  4496. }
  4497. #undef MEMSET_TYPED_ARRAY
  4498. return returnValue;
  4499. JIT_HELPER_END(Op_Memset);
  4500. }
  4501. Var JavascriptOperators::OP_DeleteElementI_UInt32(Var instance, uint32 index, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  4502. {
  4503. JIT_HELPER_REENTRANT_HEADER(Op_DeleteElementI_UInt32);
  4504. JIT_HELPER_SAME_ATTRIBUTES(Op_DeleteElementI_UInt32, Op_DeleteElementI);
  4505. #if FLOATVAR
  4506. return OP_DeleteElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext, propertyOperationFlags);
  4507. #else
  4508. char buffer[sizeof(Js::JavascriptNumber)];
  4509. return OP_DeleteElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  4510. (Js::JavascriptNumber *)buffer), scriptContext, propertyOperationFlags);
  4511. #endif
  4512. JIT_HELPER_END(Op_DeleteElementI_UInt32);
  4513. }
  4514. Var JavascriptOperators::OP_DeleteElementI_Int32(Var instance, int32 index, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  4515. {
  4516. JIT_HELPER_REENTRANT_HEADER(Op_DeleteElementI_Int32);
  4517. JIT_HELPER_SAME_ATTRIBUTES(Op_DeleteElementI_Int32, Op_DeleteElementI);
  4518. #if FLOATVAR
  4519. return OP_DeleteElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext, propertyOperationFlags);
  4520. #else
  4521. char buffer[sizeof(Js::JavascriptNumber)];
  4522. return OP_DeleteElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  4523. (Js::JavascriptNumber *)buffer), scriptContext, propertyOperationFlags);
  4524. #endif
  4525. JIT_HELPER_END(Op_DeleteElementI_Int32);
  4526. }
  4527. Var JavascriptOperators::OP_DeleteElementI(Var instance, Var index, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  4528. {
  4529. JIT_HELPER_REENTRANT_HEADER(Op_DeleteElementI);
  4530. if(TaggedNumber::Is(instance))
  4531. {
  4532. return scriptContext->GetLibrary()->GetTrue();
  4533. }
  4534. #if ENABLE_COPYONACCESS_ARRAY
  4535. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  4536. #endif
  4537. RecyclableObject* object = RecyclableObject::FromVar(instance);
  4538. if (JavascriptOperators::IsUndefinedOrNull(object))
  4539. {
  4540. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotDelete_NullOrUndefined, GetPropertyDisplayNameForError(index, scriptContext));
  4541. }
  4542. uint32 indexVal;
  4543. PropertyRecord const * propertyRecord = nullptr;
  4544. JavascriptString * propertyNameString = nullptr;
  4545. BOOL result = TRUE;
  4546. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, &propertyNameString, false, true);
  4547. if (indexType == IndexType_Number)
  4548. {
  4549. result = JavascriptOperators::DeleteItem(object, indexVal, propertyOperationFlags);
  4550. }
  4551. else if (indexType == IndexType_JavascriptString)
  4552. {
  4553. result = JavascriptOperators::DeleteProperty(object, propertyNameString, propertyOperationFlags);
  4554. }
  4555. else
  4556. {
  4557. Assert(indexType == IndexType_PropertyId);
  4558. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  4559. {
  4560. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
  4561. Assert(indexType == IndexType_PropertyId);
  4562. Assert(propertyRecord != nullptr);
  4563. }
  4564. if (propertyRecord != nullptr)
  4565. {
  4566. result = JavascriptOperators::DeleteProperty(object, propertyRecord->GetPropertyId(), propertyOperationFlags);
  4567. }
  4568. #if DBG
  4569. else
  4570. {
  4571. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  4572. PropertyRecord const * debugPropertyRecord;
  4573. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  4574. AssertMsg(JavascriptOperators::DeleteProperty(object, debugPropertyRecord->GetPropertyId(), propertyOperationFlags), "delete should have been true. See OS Bug 2727708 if you see this come from the web");
  4575. }
  4576. #endif
  4577. }
  4578. Assert(result || !(propertyOperationFlags & (PropertyOperation_StrictMode | PropertyOperation_ThrowOnDeleteIfNotConfig)));
  4579. return scriptContext->GetLibrary()->CreateBoolean(result);
  4580. JIT_HELPER_END(Op_DeleteElementI);
  4581. }
  4582. Var JavascriptOperators::OP_GetLength(Var instance, ScriptContext* scriptContext)
  4583. {
  4584. return JavascriptOperators::OP_GetProperty(instance, PropertyIds::length, scriptContext);
  4585. }
  4586. Var JavascriptOperators::GetThisFromModuleRoot(Var thisVar)
  4587. {
  4588. RootObjectBase * rootObject = static_cast<RootObjectBase*>(thisVar);
  4589. RecyclableObject* hostObject = rootObject->GetHostObject();
  4590. //
  4591. // if the module root has the host object, use that as "this"
  4592. //
  4593. if (hostObject)
  4594. {
  4595. thisVar = hostObject->GetHostDispatchVar();
  4596. }
  4597. return thisVar;
  4598. }
  4599. inline void JavascriptOperators::TryLoadRoot(Var& thisVar, TypeId typeId, int moduleID, ScriptContextInfo* scriptContext)
  4600. {
  4601. bool loadRoot = false;
  4602. if (JavascriptOperators::IsUndefinedOrNullType(typeId) || typeId == TypeIds_ActivationObject)
  4603. {
  4604. loadRoot = true;
  4605. }
  4606. else if (typeId == TypeIds_HostDispatch)
  4607. {
  4608. TypeId remoteTypeId = TypeIds_Limit;
  4609. if (RecyclableObject::FromVar(thisVar)->GetRemoteTypeId(&remoteTypeId))
  4610. {
  4611. if (remoteTypeId == TypeIds_Null || remoteTypeId == TypeIds_Undefined || remoteTypeId == TypeIds_ActivationObject)
  4612. {
  4613. loadRoot = true;
  4614. }
  4615. }
  4616. }
  4617. if (loadRoot)
  4618. {
  4619. if (moduleID == 0)
  4620. {
  4621. thisVar = (Js::Var)scriptContext->GetGlobalObjectThisAddr();
  4622. }
  4623. else
  4624. {
  4625. // TODO: OOP JIT, create a copy of module roots in server side
  4626. Js::ModuleRoot * moduleRoot = JavascriptOperators::GetModuleRoot(moduleID, (ScriptContext*)scriptContext);
  4627. if (moduleRoot == nullptr)
  4628. {
  4629. Assert(false);
  4630. thisVar = (Js::Var)scriptContext->GetUndefinedAddr();
  4631. }
  4632. else
  4633. {
  4634. thisVar = GetThisFromModuleRoot(moduleRoot);
  4635. }
  4636. }
  4637. }
  4638. }
  4639. Var JavascriptOperators::OP_GetThis(Var thisVar, int moduleID, ScriptContextInfo* scriptContext)
  4640. {
  4641. JIT_HELPER_REENTRANT_HEADER(LdThis);
  4642. //
  4643. // if "this" is null or undefined
  4644. // Pass the global object
  4645. // Else
  4646. // Pass ToObject(this)
  4647. //
  4648. TypeId typeId = JavascriptOperators::GetTypeId(thisVar);
  4649. Assert(!JavascriptOperators::IsThisSelf(typeId));
  4650. return JavascriptOperators::GetThisHelper(thisVar, typeId, moduleID, scriptContext);
  4651. JIT_HELPER_END(LdThis);
  4652. }
  4653. Var JavascriptOperators::OP_GetThisNoFastPath(Var thisVar, int moduleID, ScriptContext* scriptContext)
  4654. {
  4655. JIT_HELPER_REENTRANT_HEADER(LdThisNoFastPath);
  4656. TypeId typeId = JavascriptOperators::GetTypeId(thisVar);
  4657. if (JavascriptOperators::IsThisSelf(typeId))
  4658. {
  4659. Assert(typeId != TypeIds_GlobalObject || ((Js::GlobalObject*)thisVar)->ToThis() == thisVar);
  4660. Assert(typeId != TypeIds_ModuleRoot || JavascriptOperators::GetThisFromModuleRoot(thisVar) == thisVar);
  4661. return thisVar;
  4662. }
  4663. return JavascriptOperators::GetThisHelper(thisVar, typeId, moduleID, scriptContext);
  4664. JIT_HELPER_END(LdThisNoFastPath);
  4665. }
  4666. bool JavascriptOperators::IsThisSelf(TypeId typeId)
  4667. {
  4668. return (JavascriptOperators::IsObjectType(typeId) && ! JavascriptOperators::IsSpecialObjectType(typeId));
  4669. }
  4670. Var JavascriptOperators::GetThisHelper(Var thisVar, TypeId typeId, int moduleID, ScriptContextInfo *scriptContext)
  4671. {
  4672. if (! JavascriptOperators::IsObjectType(typeId) && ! JavascriptOperators::IsUndefinedOrNullType(typeId))
  4673. {
  4674. #if ENABLE_NATIVE_CODEGEN
  4675. Assert(!JITManager::GetJITManager()->IsJITServer());
  4676. #endif
  4677. #if !FLOATVAR
  4678. // We allowed stack number to be used as the "this" for getter and setter activation of
  4679. // n.x and n[prop], where n is the Javascript Number
  4680. return JavascriptOperators::ToObject(
  4681. JavascriptNumber::BoxStackNumber(thisVar, (ScriptContext*)scriptContext), (ScriptContext*)scriptContext);
  4682. #else
  4683. return JavascriptOperators::ToObject(thisVar, (ScriptContext*)scriptContext);
  4684. #endif
  4685. }
  4686. else
  4687. {
  4688. TryLoadRoot(thisVar, typeId, moduleID, scriptContext);
  4689. return thisVar;
  4690. }
  4691. }
  4692. Var JavascriptOperators::OP_StrictGetThis(Var thisVar, ScriptContext* scriptContext)
  4693. {
  4694. JIT_HELPER_NOT_REENTRANT_HEADER(StrictLdThis, reentrancylock, scriptContext->GetThreadContext());
  4695. TypeId typeId = JavascriptOperators::GetTypeId(thisVar);
  4696. if (typeId == TypeIds_ActivationObject)
  4697. {
  4698. return scriptContext->GetLibrary()->GetUndefined();
  4699. }
  4700. return thisVar;
  4701. JIT_HELPER_END(StrictLdThis);
  4702. }
  4703. BOOL JavascriptOperators::GetRemoteTypeId(Var aValue, __out TypeId* typeId)
  4704. {
  4705. *typeId = TypeIds_Limit;
  4706. if (GetTypeId(aValue) != TypeIds_HostDispatch)
  4707. {
  4708. return FALSE;
  4709. }
  4710. return RecyclableObject::FromVar(aValue)->GetRemoteTypeId(typeId);
  4711. }
  4712. BOOL JavascriptOperators::IsJsNativeType(TypeId type)
  4713. {
  4714. switch(type)
  4715. {
  4716. case TypeIds_Object:
  4717. case TypeIds_Function:
  4718. case TypeIds_Array:
  4719. case TypeIds_NativeIntArray:
  4720. #if ENABLE_COPYONACCESS_ARRAY
  4721. case TypeIds_CopyOnAccessNativeIntArray:
  4722. #endif
  4723. case TypeIds_NativeFloatArray:
  4724. case TypeIds_ES5Array:
  4725. case TypeIds_Date:
  4726. case TypeIds_WinRTDate:
  4727. case TypeIds_RegEx:
  4728. case TypeIds_Error:
  4729. case TypeIds_BooleanObject:
  4730. case TypeIds_NumberObject:
  4731. case TypeIds_StringObject:
  4732. case TypeIds_Symbol:
  4733. case TypeIds_SymbolObject:
  4734. //case TypeIds_GlobalObject:
  4735. //case TypeIds_ModuleRoot:
  4736. //case TypeIds_HostObject:
  4737. case TypeIds_Arguments:
  4738. case TypeIds_ActivationObject:
  4739. case TypeIds_Map:
  4740. case TypeIds_Set:
  4741. case TypeIds_WeakMap:
  4742. case TypeIds_WeakSet:
  4743. case TypeIds_ArrayIterator:
  4744. case TypeIds_MapIterator:
  4745. case TypeIds_SetIterator:
  4746. case TypeIds_StringIterator:
  4747. case TypeIds_Generator:
  4748. case TypeIds_Promise:
  4749. case TypeIds_Proxy:
  4750. return true;
  4751. default:
  4752. return false;
  4753. }
  4754. }
  4755. BOOL JavascriptOperators::IsJsNativeObject(Var instance)
  4756. {
  4757. return IsJsNativeType(GetTypeId(instance));
  4758. }
  4759. BOOL JavascriptOperators::IsJsNativeObject(_In_ RecyclableObject* instance)
  4760. {
  4761. return IsJsNativeType(GetTypeId(instance));
  4762. }
  4763. bool JavascriptOperators::CanShortcutOnUnknownPropertyName(RecyclableObject *instance)
  4764. {
  4765. if (!CanShortcutInstanceOnUnknownPropertyName(instance))
  4766. {
  4767. return false;
  4768. }
  4769. return CanShortcutPrototypeChainOnUnknownPropertyName(instance->GetPrototype());
  4770. }
  4771. bool JavascriptOperators::CanShortcutInstanceOnUnknownPropertyName(RecyclableObject *instance)
  4772. {
  4773. if (PHASE_OFF1(Js::OptUnknownElementNamePhase))
  4774. {
  4775. return false;
  4776. }
  4777. TypeId typeId = instance->GetTypeId();
  4778. if (typeId == TypeIds_Proxy || typeId == TypeIds_HostDispatch)
  4779. {
  4780. return false;
  4781. }
  4782. if (DynamicType::Is(typeId) &&
  4783. static_cast<DynamicObject*>(instance)->GetTypeHandler()->IsStringTypeHandler())
  4784. {
  4785. return false;
  4786. }
  4787. if (instance->IsExternal())
  4788. {
  4789. return false;
  4790. }
  4791. if (!(instance->HasDeferredTypeHandler()))
  4792. {
  4793. JavascriptFunction * function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  4794. return function && function->IsExternalFunction();
  4795. }
  4796. return false;
  4797. }
  4798. bool JavascriptOperators::CanShortcutPrototypeChainOnUnknownPropertyName(RecyclableObject *prototype)
  4799. {
  4800. Assert(prototype);
  4801. for (; !JavascriptOperators::IsNull(prototype); prototype = prototype->GetPrototype())
  4802. {
  4803. if (!CanShortcutInstanceOnUnknownPropertyName(prototype))
  4804. {
  4805. return false;
  4806. }
  4807. }
  4808. return true;
  4809. }
  4810. RecyclableObject* JavascriptOperators::GetPrototype(RecyclableObject* instance)
  4811. {
  4812. if (JavascriptOperators::GetTypeId(instance) == TypeIds_Null)
  4813. {
  4814. return instance;
  4815. }
  4816. return instance->GetPrototype();
  4817. }
  4818. RecyclableObject* JavascriptOperators::OP_GetPrototype(Var instance, ScriptContext* scriptContext)
  4819. {
  4820. if (TaggedNumber::Is(instance))
  4821. {
  4822. return scriptContext->GetLibrary()->GetNumberPrototype();
  4823. }
  4824. else
  4825. {
  4826. RecyclableObject* object = RecyclableObject::FromVar(instance);
  4827. if (JavascriptOperators::IsNull(object))
  4828. {
  4829. return object;
  4830. }
  4831. return JavascriptOperators::GetPrototype(object);
  4832. }
  4833. }
  4834. BOOL JavascriptOperators::OP_BrFncEqApply(Var instance, ScriptContext *scriptContext)
  4835. {
  4836. JIT_HELPER_NOT_REENTRANT_HEADER(Op_OP_BrFncEqApply, reentrancylock, scriptContext->GetThreadContext());
  4837. // JavascriptFunction && !HostDispatch
  4838. if (JavascriptOperators::GetTypeId(instance) == TypeIds_Function)
  4839. {
  4840. FunctionProxy *bod= ((JavascriptFunction*)instance)->GetFunctionProxy();
  4841. if (bod != nullptr)
  4842. {
  4843. return bod->GetDirectEntryPoint(bod->GetDefaultEntryPointInfo()) == &Js::JavascriptFunction::EntryApply;
  4844. }
  4845. else
  4846. {
  4847. FunctionInfo* info = ((JavascriptFunction *)instance)->GetFunctionInfo();
  4848. if (info != nullptr)
  4849. {
  4850. return &Js::JavascriptFunction::EntryApply == info->GetOriginalEntryPoint();
  4851. }
  4852. else
  4853. {
  4854. return false;
  4855. }
  4856. }
  4857. }
  4858. return false;
  4859. JIT_HELPER_END(Op_OP_BrFncEqApply);
  4860. }
  4861. BOOL JavascriptOperators::OP_BrFncNeqApply(Var instance, ScriptContext *scriptContext)
  4862. {
  4863. JIT_HELPER_NOT_REENTRANT_HEADER(Op_OP_BrFncNeqApply, reentrancylock, scriptContext->GetThreadContext());
  4864. // JavascriptFunction and !HostDispatch
  4865. if (JavascriptOperators::GetTypeId(instance) == TypeIds_Function)
  4866. {
  4867. FunctionProxy *bod = ((JavascriptFunction *)instance)->GetFunctionProxy();
  4868. if (bod != nullptr)
  4869. {
  4870. return bod->GetDirectEntryPoint(bod->GetDefaultEntryPointInfo()) != &Js::JavascriptFunction::EntryApply;
  4871. }
  4872. else
  4873. {
  4874. FunctionInfo* info = ((JavascriptFunction *)instance)->GetFunctionInfo();
  4875. if (info != nullptr)
  4876. {
  4877. return &Js::JavascriptFunction::EntryApply != info->GetOriginalEntryPoint();
  4878. }
  4879. else
  4880. {
  4881. return true;
  4882. }
  4883. }
  4884. }
  4885. return true;
  4886. JIT_HELPER_END(Op_OP_BrFncNeqApply);
  4887. }
  4888. BOOL JavascriptOperators::OP_BrHasSideEffects(int se, ScriptContext* scriptContext)
  4889. {
  4890. return (scriptContext->optimizationOverrides.GetSideEffects() & se) != SideEffects_None;
  4891. }
  4892. BOOL JavascriptOperators::OP_BrNotHasSideEffects(int se, ScriptContext* scriptContext)
  4893. {
  4894. return (scriptContext->optimizationOverrides.GetSideEffects() & se) == SideEffects_None;
  4895. }
  4896. // returns NULL if there is no more elements to enumerate.
  4897. Var JavascriptOperators::OP_BrOnEmpty(ForInObjectEnumerator * aEnumerator)
  4898. {
  4899. JIT_HELPER_REENTRANT_HEADER(Op_OP_BrOnEmpty);
  4900. PropertyId id;
  4901. return aEnumerator->MoveAndGetNext(id);
  4902. JIT_HELPER_END(Op_OP_BrOnEmpty);
  4903. }
  4904. void JavascriptOperators::OP_InitForInEnumerator(Var enumerable, ForInObjectEnumerator * enumerator, ScriptContext* scriptContext, EnumeratorCache * forInCache)
  4905. {
  4906. JIT_HELPER_REENTRANT_HEADER(Op_OP_InitForInEnumerator);
  4907. RecyclableObject* enumerableObject;
  4908. #if ENABLE_COPYONACCESS_ARRAY
  4909. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(enumerable);
  4910. #endif
  4911. if (!GetPropertyObject(enumerable, scriptContext, &enumerableObject))
  4912. {
  4913. enumerableObject = nullptr;
  4914. }
  4915. enumerator->Initialize(enumerableObject, scriptContext, false, forInCache);
  4916. JIT_HELPER_END(Op_OP_InitForInEnumerator);
  4917. }
  4918. Js::Var JavascriptOperators::OP_CmEq_A(Var a, Var b, ScriptContext* scriptContext)
  4919. {
  4920. JIT_HELPER_REENTRANT_HEADER(OP_CmEq_A);
  4921. return JavascriptBoolean::ToVar(JavascriptOperators::Equal(a, b, scriptContext), scriptContext);
  4922. JIT_HELPER_END(OP_CmEq_A);
  4923. }
  4924. Var JavascriptOperators::OP_CmNeq_A(Var a, Var b, ScriptContext* scriptContext)
  4925. {
  4926. JIT_HELPER_REENTRANT_HEADER(OP_CmNeq_A);
  4927. return JavascriptBoolean::ToVar(JavascriptOperators::NotEqual(a,b,scriptContext), scriptContext);
  4928. JIT_HELPER_END(OP_CmNeq_A);
  4929. }
  4930. Var JavascriptOperators::OP_CmSrEq_A(Var a, Var b, ScriptContext* scriptContext)
  4931. {
  4932. JIT_HELPER_REENTRANT_HEADER(OP_CmSrEq_A);
  4933. return JavascriptBoolean::ToVar(JavascriptOperators::StrictEqual(a, b, scriptContext), scriptContext);
  4934. JIT_HELPER_END(OP_CmSrEq_A);
  4935. }
  4936. Var JavascriptOperators::OP_CmSrEq_String(Var a, JavascriptString* b, ScriptContext *scriptContext)
  4937. {
  4938. JIT_HELPER_REENTRANT_HEADER(OP_CmSrEq_String);
  4939. return JavascriptBoolean::ToVar(JavascriptOperators::StrictEqualString(a, b), scriptContext);
  4940. JIT_HELPER_END(OP_CmSrEq_String);
  4941. }
  4942. Var JavascriptOperators::OP_CmSrEq_EmptyString(Var a, ScriptContext *scriptContext)
  4943. {
  4944. JIT_HELPER_REENTRANT_HEADER(OP_CmSrEq_EmptyString);
  4945. return JavascriptBoolean::ToVar(JavascriptOperators::StrictEqualEmptyString(a), scriptContext);
  4946. JIT_HELPER_END(OP_CmSrEq_EmptyString);
  4947. }
  4948. Var JavascriptOperators::OP_CmSrNeq_A(Var a, Var b, ScriptContext* scriptContext)
  4949. {
  4950. JIT_HELPER_REENTRANT_HEADER(OP_CmSrNeq_A);
  4951. return JavascriptBoolean::ToVar(JavascriptOperators::NotStrictEqual(a, b, scriptContext), scriptContext);
  4952. JIT_HELPER_END(OP_CmSrNeq_A);
  4953. }
  4954. Var JavascriptOperators::OP_CmLt_A(Var a, Var b, ScriptContext* scriptContext)
  4955. {
  4956. JIT_HELPER_REENTRANT_HEADER(OP_CmLt_A);
  4957. return JavascriptBoolean::ToVar(JavascriptOperators::Less(a, b, scriptContext), scriptContext);
  4958. JIT_HELPER_END(OP_CmLt_A);
  4959. }
  4960. Var JavascriptOperators::OP_CmLe_A(Var a, Var b, ScriptContext* scriptContext)
  4961. {
  4962. JIT_HELPER_REENTRANT_HEADER(OP_CmLe_A);
  4963. return JavascriptBoolean::ToVar(JavascriptOperators::LessEqual(a, b, scriptContext), scriptContext);
  4964. JIT_HELPER_END(OP_CmLe_A);
  4965. }
  4966. Var JavascriptOperators::OP_CmGt_A(Var a, Var b, ScriptContext* scriptContext)
  4967. {
  4968. JIT_HELPER_REENTRANT_HEADER(OP_CmGt_A);
  4969. return JavascriptBoolean::ToVar(JavascriptOperators::Greater(a, b, scriptContext), scriptContext);
  4970. JIT_HELPER_END(OP_CmGt_A);
  4971. }
  4972. Var JavascriptOperators::OP_CmGe_A(Var a, Var b, ScriptContext* scriptContext)
  4973. {
  4974. JIT_HELPER_REENTRANT_HEADER(OP_CmGe_A);
  4975. return JavascriptBoolean::ToVar(JavascriptOperators::GreaterEqual(a, b, scriptContext), scriptContext);
  4976. JIT_HELPER_END(OP_CmGe_A);
  4977. }
  4978. DetachedStateBase* JavascriptOperators::DetachVarAndGetState(Var var, bool queueForDelayFree/* = true*/)
  4979. {
  4980. switch (GetTypeId(var))
  4981. {
  4982. case TypeIds_ArrayBuffer:
  4983. return Js::ArrayBuffer::FromVar(var)->DetachAndGetState(queueForDelayFree);
  4984. default:
  4985. if (!Js::RecyclableObject::FromVar(var)->IsExternal())
  4986. {
  4987. AssertMsg(false, "We should explicitly have a case statement for each non-external object that can be detached.");
  4988. }
  4989. return nullptr;
  4990. }
  4991. }
  4992. bool JavascriptOperators::IsObjectDetached(Var var)
  4993. {
  4994. switch (GetTypeId(var))
  4995. {
  4996. case TypeIds_ArrayBuffer:
  4997. return Js::ArrayBuffer::FromVar(var)->IsDetached();
  4998. default:
  4999. return false;
  5000. }
  5001. }
  5002. Var JavascriptOperators::NewVarFromDetachedState(DetachedStateBase* state, JavascriptLibrary *library)
  5003. {
  5004. AssertOrFailFastMsg(state->GetTypeId() == TypeIds_ArrayBuffer, "We should only re-activate detached ArrayBuffer");
  5005. return Js::ArrayBuffer::NewFromDetachedState(state, library);
  5006. }
  5007. DynamicType *
  5008. JavascriptOperators::EnsureObjectLiteralType(ScriptContext* scriptContext, const Js::PropertyIdArray *propIds, Field(DynamicType*)* literalType)
  5009. {
  5010. JIT_HELPER_NOT_REENTRANT_HEADER(EnsureObjectLiteralType, reentrancylock, scriptContext->GetThreadContext());
  5011. DynamicType * newType = *literalType;
  5012. if (newType != nullptr)
  5013. {
  5014. if (!newType->GetIsShared())
  5015. {
  5016. newType->ShareType();
  5017. }
  5018. }
  5019. else
  5020. {
  5021. DynamicType* objectType =
  5022. FunctionBody::DoObjectHeaderInliningForObjectLiteral(propIds)
  5023. ? scriptContext->GetLibrary()->GetObjectHeaderInlinedLiteralType((uint16)propIds->count)
  5024. : scriptContext->GetLibrary()->GetObjectLiteralType(
  5025. static_cast<PropertyIndex>(
  5026. min(propIds->count, static_cast<uint32>(MaxPreInitializedObjectTypeInlineSlotCount))));
  5027. newType = PathTypeHandlerBase::CreateTypeForNewScObject(scriptContext, objectType, propIds, false);
  5028. *literalType = newType;
  5029. }
  5030. Assert(scriptContext);
  5031. Assert(GetLiteralInlineSlotCapacity(propIds) == newType->GetTypeHandler()->GetInlineSlotCapacity());
  5032. Assert(newType->GetTypeHandler()->GetSlotCapacity() >= 0);
  5033. Assert(GetLiteralSlotCapacity(propIds) == (uint)newType->GetTypeHandler()->GetSlotCapacity());
  5034. return newType;
  5035. JIT_HELPER_END(EnsureObjectLiteralType);
  5036. }
  5037. Var JavascriptOperators::NewScObjectLiteral(ScriptContext* scriptContext, const Js::PropertyIdArray *propIds, Field(DynamicType*)* literalType)
  5038. {
  5039. Assert(propIds->count != 0);
  5040. Assert(!propIds->hadDuplicates); // duplicates are removed by parser
  5041. #ifdef PROFILE_OBJECT_LITERALS
  5042. // Empty objects not counted in the object literal counts
  5043. scriptContext->objectLiteralInstanceCount++;
  5044. if (propIds->count > scriptContext->objectLiteralMaxLength)
  5045. {
  5046. scriptContext->objectLiteralMaxLength = propIds->count;
  5047. }
  5048. #endif
  5049. DynamicType* newType = EnsureObjectLiteralType(scriptContext, propIds, literalType);
  5050. DynamicObject* instance = DynamicObject::New(scriptContext->GetRecycler(), newType);
  5051. if (!newType->GetIsShared())
  5052. {
  5053. #if ENABLE_FIXED_FIELDS
  5054. newType->GetTypeHandler()->SetSingletonInstanceIfNeeded(instance);
  5055. #endif
  5056. }
  5057. #ifdef PROFILE_OBJECT_LITERALS
  5058. else
  5059. {
  5060. scriptContext->objectLiteralCacheCount++;
  5061. }
  5062. #endif
  5063. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(instance));
  5064. // can't auto-proxy here as object literal is not exactly "new" object and cannot be intercepted as proxy.
  5065. return instance;
  5066. }
  5067. uint JavascriptOperators::GetLiteralSlotCapacity(Js::PropertyIdArray const * propIds)
  5068. {
  5069. const uint inlineSlotCapacity = GetLiteralInlineSlotCapacity(propIds);
  5070. return DynamicTypeHandler::RoundUpSlotCapacity(propIds->count, static_cast<PropertyIndex>(inlineSlotCapacity));
  5071. }
  5072. uint JavascriptOperators::GetLiteralInlineSlotCapacity(
  5073. Js::PropertyIdArray const * propIds)
  5074. {
  5075. if (propIds->hadDuplicates)
  5076. {
  5077. return 0;
  5078. }
  5079. return
  5080. FunctionBody::DoObjectHeaderInliningForObjectLiteral(propIds)
  5081. ? DynamicTypeHandler::RoundUpObjectHeaderInlinedInlineSlotCapacity(static_cast<PropertyIndex>(propIds->count))
  5082. : DynamicTypeHandler::RoundUpInlineSlotCapacity(
  5083. static_cast<PropertyIndex>(
  5084. min(propIds->count, static_cast<uint32>(MaxPreInitializedObjectTypeInlineSlotCount))));
  5085. }
  5086. Var JavascriptOperators::OP_InitCachedScope(Var varFunc, const Js::PropertyIdArray *propIds, Field(DynamicType*)* literalType, bool formalsAreLetDecls, ScriptContext *scriptContext)
  5087. {
  5088. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitCachedScope, reentrancylock, scriptContext->GetThreadContext());
  5089. bool isGAFunction = JavascriptFunction::Is(varFunc);
  5090. Assert(isGAFunction);
  5091. if (isGAFunction)
  5092. {
  5093. JavascriptFunction *function = JavascriptFunction::FromVar(varFunc);
  5094. isGAFunction = JavascriptGeneratorFunction::Test(function) || JavascriptAsyncFunction::Test(function);
  5095. }
  5096. ScriptFunction *func = isGAFunction ?
  5097. JavascriptGeneratorFunction::FromVar(varFunc)->GetGeneratorVirtualScriptFunction() :
  5098. ScriptFunction::FromVar(varFunc);
  5099. #ifdef PROFILE_OBJECT_LITERALS
  5100. // Empty objects not counted in the object literal counts
  5101. scriptContext->objectLiteralInstanceCount++;
  5102. if (propIds->count > scriptContext->objectLiteralMaxLength)
  5103. {
  5104. scriptContext->objectLiteralMaxLength = propIds->count;
  5105. }
  5106. #endif
  5107. PropertyId cachedFuncCount = ActivationObjectEx::GetCachedFuncCount(propIds);
  5108. PropertyId firstFuncSlot = ActivationObjectEx::GetFirstFuncSlot(propIds);
  5109. PropertyId firstVarSlot = ActivationObjectEx::GetFirstVarSlot(propIds);
  5110. PropertyId lastFuncSlot = Constants::NoProperty;
  5111. if (firstFuncSlot != Constants::NoProperty)
  5112. {
  5113. if (firstVarSlot == Constants::NoProperty || firstVarSlot < firstFuncSlot)
  5114. {
  5115. lastFuncSlot = propIds->count - 1;
  5116. }
  5117. else
  5118. {
  5119. lastFuncSlot = firstVarSlot - 1;
  5120. }
  5121. }
  5122. DynamicType *type = *literalType;
  5123. if (type != nullptr)
  5124. {
  5125. #ifdef PROFILE_OBJECT_LITERALS
  5126. scriptContext->objectLiteralCacheCount++;
  5127. #endif
  5128. }
  5129. else
  5130. {
  5131. type = scriptContext->GetLibrary()->GetActivationObjectType();
  5132. if (formalsAreLetDecls)
  5133. {
  5134. uint formalsSlotLimit = (firstFuncSlot != Constants::NoProperty) ? (uint)firstFuncSlot :
  5135. (firstVarSlot != Constants::NoProperty) ? (uint)firstVarSlot :
  5136. propIds->count;
  5137. if (func->GetFunctionBody()->HasReferenceableBuiltInArguments())
  5138. {
  5139. type = PathTypeHandlerBase::CreateNewScopeObject<true>(scriptContext, type, propIds, PropertyLet, formalsSlotLimit);
  5140. }
  5141. else
  5142. {
  5143. type = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, type, propIds, PropertyLet, formalsSlotLimit);
  5144. }
  5145. }
  5146. else
  5147. {
  5148. type = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, type, propIds);
  5149. }
  5150. *literalType = type;
  5151. }
  5152. Var undef = scriptContext->GetLibrary()->GetUndefined();
  5153. ActivationObjectEx *scopeObjEx = func->GetCachedScope();
  5154. if (scopeObjEx && scopeObjEx->IsCommitted())
  5155. {
  5156. scopeObjEx->ReplaceType(type);
  5157. scopeObjEx->SetCommit(false);
  5158. #if DBG
  5159. for (uint i = firstVarSlot; i < propIds->count; i++)
  5160. {
  5161. AssertMsg(scopeObjEx->GetSlot(i) == undef, "Var attached to cached scope");
  5162. }
  5163. #endif
  5164. }
  5165. else
  5166. {
  5167. ActivationObjectEx *tmp = RecyclerNewPlus(scriptContext->GetRecycler(), (cachedFuncCount == 0 ? 0 : cachedFuncCount - 1) * sizeof(FuncCacheEntry), ActivationObjectEx, type, func, cachedFuncCount, firstFuncSlot, lastFuncSlot);
  5168. if (!scopeObjEx)
  5169. {
  5170. func->SetCachedScope(tmp);
  5171. }
  5172. scopeObjEx = tmp;
  5173. for (uint i = firstVarSlot; i < propIds->count; i++)
  5174. {
  5175. scopeObjEx->SetSlot(SetSlotArguments(propIds->elements[i], i, undef));
  5176. }
  5177. }
  5178. return scopeObjEx;
  5179. JIT_HELPER_END(OP_InitCachedScope);
  5180. }
  5181. void JavascriptOperators::OP_InvalidateCachedScope(void* varEnv, int32 envIndex)
  5182. {
  5183. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_InvalidateCachedScope);
  5184. FrameDisplay *disp = (FrameDisplay*)varEnv;
  5185. Var item = disp->GetItem(envIndex);
  5186. if (item != nullptr)
  5187. {
  5188. Assert(ActivationObjectEx::Is(item));
  5189. RecyclableObject *objScope = RecyclableObject::FromVar(item);
  5190. objScope->InvalidateCachedScope();
  5191. }
  5192. JIT_HELPER_END(OP_InvalidateCachedScope);
  5193. }
  5194. void JavascriptOperators::OP_InitCachedFuncs(Var varScope, FrameDisplay *pDisplay, const FuncInfoArray *info, ScriptContext *scriptContext)
  5195. {
  5196. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitCachedFuncs, reentrancylock, scriptContext->GetThreadContext());
  5197. ActivationObjectEx *scopeObj = ActivationObjectEx::FromVar(varScope);
  5198. Assert(scopeObj->GetTypeHandler()->GetInlineSlotCapacity() == 0);
  5199. uint funcCount = info->count;
  5200. if (funcCount == 0)
  5201. {
  5202. // Degenerate case: no nested funcs at all
  5203. return;
  5204. }
  5205. if (scopeObj->HasCachedFuncs())
  5206. {
  5207. for (uint i = 0; i < funcCount; i++)
  5208. {
  5209. const FuncCacheEntry *entry = scopeObj->GetFuncCacheEntry(i);
  5210. ScriptFunction *func = entry->func;
  5211. FunctionProxy * proxy = func->GetFunctionProxy();
  5212. // Reset the function's type to the default type with no properties
  5213. // Use the cached type on the function proxy rather than the type in the func cache entry
  5214. // CONSIDER: Stop caching the function types in the scope object
  5215. func->ReplaceType(proxy->EnsureDeferredPrototypeType());
  5216. func->ResetConstructorCacheToDefault();
  5217. uint scopeSlot = info->elements[i].scopeSlot;
  5218. if (scopeSlot != Constants::NoProperty)
  5219. {
  5220. // CONSIDER: Store property IDs in FuncInfoArray in debug builds so we can properly assert in SetAuxSlot
  5221. scopeObj->SetAuxSlot(SetSlotArguments(Constants::NoProperty, scopeSlot, entry->func));
  5222. }
  5223. }
  5224. return;
  5225. }
  5226. // No cached functions, so create them and cache them.
  5227. JavascriptFunction *funcParent = scopeObj->GetParentFunc();
  5228. for (uint i = 0; i < funcCount; i++)
  5229. {
  5230. const FuncInfoEntry *entry = &info->elements[i];
  5231. uint nestedIndex = entry->nestedIndex;
  5232. uint scopeSlot = entry->scopeSlot;
  5233. FunctionProxy * proxy = funcParent->GetFunctionBody()->GetNestedFunctionProxy(nestedIndex);
  5234. ScriptFunction *func = scriptContext->GetLibrary()->CreateScriptFunction(proxy);
  5235. func->SetEnvironment(pDisplay);
  5236. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_FUNCTION(func, EtwTrace::GetFunctionId(proxy)));
  5237. scopeObj->SetCachedFunc(i, func);
  5238. if (scopeSlot != Constants::NoProperty)
  5239. {
  5240. // CONSIDER: Store property IDs in FuncInfoArray in debug builds so we can properly assert in SetAuxSlot
  5241. scopeObj->SetAuxSlot(SetSlotArguments(Constants::NoProperty, scopeSlot, func));
  5242. }
  5243. }
  5244. JIT_HELPER_END(OP_InitCachedFuncs);
  5245. }
  5246. Var JavascriptOperators::AddVarsToArraySegment(SparseArraySegment<Var> * segment, const Js::VarArray *vars)
  5247. {
  5248. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ArraySegmentVars);
  5249. uint32 count = vars->count;
  5250. Assert(segment->left == 0);
  5251. Assert(count <= segment->size);
  5252. if(count > segment->length)
  5253. {
  5254. segment->length = count;
  5255. segment->CheckLengthvsSize();
  5256. }
  5257. CopyArray(segment->elements, segment->length, vars->elements, count);
  5258. return segment;
  5259. JIT_HELPER_END(ArraySegmentVars);
  5260. }
  5261. void JavascriptOperators::AddIntsToArraySegment(SparseArraySegment<int32> * segment, const Js::AuxArray<int32> *ints)
  5262. {
  5263. uint32 count = ints->count;
  5264. Assert(segment->left == 0);
  5265. Assert(count <= segment->size);
  5266. if(count > segment->length)
  5267. {
  5268. segment->length = count;
  5269. segment->CheckLengthvsSize();
  5270. }
  5271. js_memcpy_s(segment->elements, sizeof(int32) * segment->length, ints->elements, sizeof(int32) * count);
  5272. }
  5273. void JavascriptOperators::AddFloatsToArraySegment(SparseArraySegment<double> * segment, const Js::AuxArray<double> *doubles)
  5274. {
  5275. uint32 count = doubles->count;
  5276. Assert(segment->left == 0);
  5277. Assert(count <= segment->size);
  5278. if(count > segment->length)
  5279. {
  5280. segment->length = count;
  5281. segment->CheckLengthvsSize();
  5282. }
  5283. js_memcpy_s(segment->elements, sizeof(double) * segment->length, doubles->elements, sizeof(double) * count);
  5284. }
  5285. RecyclableObject * JavascriptOperators::GetPrototypeObject(RecyclableObject * constructorFunction, ScriptContext * scriptContext)
  5286. {
  5287. Var prototypeProperty = JavascriptOperators::GetProperty(constructorFunction, PropertyIds::prototype, scriptContext);
  5288. RecyclableObject* prototypeObject;
  5289. PrototypeObject(prototypeProperty, constructorFunction, scriptContext, &prototypeObject);
  5290. return prototypeObject;
  5291. }
  5292. RecyclableObject * JavascriptOperators::GetPrototypeObjectForConstructorCache(RecyclableObject * constructor, ScriptContext* requestContext, bool& canBeCached)
  5293. {
  5294. PropertyValueInfo info;
  5295. Var prototypeValue;
  5296. RecyclableObject* prototypeObject;
  5297. canBeCached = false;
  5298. // Do a local property lookup. Since a function's prototype property is a non-configurable data property, we don't need to worry
  5299. // about the prototype being an accessor property, whose getter returns different values every time it's called.
  5300. if (constructor->GetProperty(constructor, PropertyIds::prototype, &prototypeValue, &info, requestContext))
  5301. {
  5302. if (!JavascriptOperators::PrototypeObject(prototypeValue, constructor, requestContext, &prototypeObject))
  5303. {
  5304. // The value returned by the property lookup is not a valid prototype object, default to object prototype.
  5305. Assert(prototypeObject == constructor->GetLibrary()->GetObjectPrototype());
  5306. }
  5307. // For these scenarios, we do not want to populate the cache.
  5308. if (constructor->GetScriptContext() != requestContext || info.GetInstance() != constructor)
  5309. {
  5310. return prototypeObject;
  5311. }
  5312. }
  5313. else
  5314. {
  5315. // It's ok to cache Object.prototype, because Object.prototype cannot be overwritten.
  5316. prototypeObject = constructor->GetLibrary()->GetObjectPrototype();
  5317. }
  5318. canBeCached = true;
  5319. return prototypeObject;
  5320. }
  5321. bool JavascriptOperators::PrototypeObject(Var prototypeProperty, RecyclableObject * constructorFunction, ScriptContext * scriptContext, RecyclableObject** prototypeObject)
  5322. {
  5323. TypeId prototypeType = JavascriptOperators::GetTypeId(prototypeProperty);
  5324. if (JavascriptOperators::IsObjectType(prototypeType))
  5325. {
  5326. *prototypeObject = RecyclableObject::FromVar(prototypeProperty);
  5327. return true;
  5328. }
  5329. *prototypeObject = constructorFunction->GetLibrary()->GetObjectPrototype();
  5330. return false;
  5331. }
  5332. FunctionInfo* JavascriptOperators::GetConstructorFunctionInfo(Var instance, ScriptContext * scriptContext)
  5333. {
  5334. TypeId typeId = JavascriptOperators::GetTypeId(instance);
  5335. if (typeId == TypeIds_Function)
  5336. {
  5337. JavascriptFunction * function = JavascriptFunction::UnsafeFromVar(instance);
  5338. return function->GetFunctionInfo();
  5339. }
  5340. if (typeId != TypeIds_HostDispatch && typeId != TypeIds_Proxy)
  5341. {
  5342. if (typeId == TypeIds_Null)
  5343. {
  5344. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  5345. }
  5346. JavascriptError::ThrowTypeError(scriptContext, VBSERR_ActionNotSupported);
  5347. }
  5348. return nullptr;
  5349. }
  5350. Var JavascriptOperators::NewJavascriptObjectNoArg(ScriptContext* requestContext)
  5351. {
  5352. JIT_HELPER_NOT_REENTRANT_HEADER(NewJavascriptObjectNoArg, reentrancylock, requestContext->GetThreadContext());
  5353. DynamicObject * newObject = requestContext->GetLibrary()->CreateObject(true);
  5354. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newObject));
  5355. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5356. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5357. {
  5358. newObject = DynamicObject::FromVar(JavascriptProxy::AutoProxyWrapper(newObject));
  5359. }
  5360. #endif
  5361. return newObject;
  5362. JIT_HELPER_END(NewJavascriptObjectNoArg);
  5363. }
  5364. Var JavascriptOperators::NewJavascriptArrayNoArg(ScriptContext* requestContext)
  5365. {
  5366. JIT_HELPER_NOT_REENTRANT_HEADER(NewJavascriptArrayNoArg, reentrancylock, requestContext->GetThreadContext());
  5367. JavascriptArray * newArray = requestContext->GetLibrary()->CreateArray();
  5368. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newArray));
  5369. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5370. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5371. {
  5372. newArray = static_cast<JavascriptArray*>(JavascriptProxy::AutoProxyWrapper(newArray));
  5373. }
  5374. #endif
  5375. return newArray;
  5376. JIT_HELPER_END(NewJavascriptArrayNoArg);
  5377. }
  5378. Var JavascriptOperators::NewScObjectNoArgNoCtorFull(Var instance, ScriptContext* requestContext)
  5379. {
  5380. // This helper can be reentrant because although we don't call the Constructor, we might have to parse it if bytecode is missing
  5381. // In which case, we would leave script. When we leave script we DisposeObjects which can dispose of Edge objects that could
  5382. // have a javascript onDispose handler and call that handler.
  5383. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoArgNoCtorFull);
  5384. return NewScObjectNoArgNoCtorCommon(instance, requestContext, true);
  5385. JIT_HELPER_END(NewScObjectNoArgNoCtorFull);
  5386. }
  5387. Var JavascriptOperators::NewScObjectNoArgNoCtor(Var instance, ScriptContext* requestContext)
  5388. {
  5389. // This helper can be reentrant because although we don't call the Constructor, we might have to parse it if bytecode is missing
  5390. // In which case, we would leave script. When we leave script we DisposeObjects which can dispose of Edge objects that could
  5391. // have a javascript onDispose handler and call that handler.
  5392. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoArgNoCtor);
  5393. return NewScObjectNoArgNoCtorCommon(instance, requestContext, false);
  5394. JIT_HELPER_END(NewScObjectNoArgNoCtor);
  5395. }
  5396. Var JavascriptOperators::NewScObjectNoArgNoCtorCommon(Var instance, ScriptContext* requestContext, bool isBaseClassConstructorNewScObject)
  5397. {
  5398. RecyclableObject * object = RecyclableObject::FromVar(instance);
  5399. FunctionInfo* functionInfo = JavascriptOperators::GetConstructorFunctionInfo(instance, requestContext);
  5400. Assert(functionInfo != &JavascriptObject::EntryInfo::NewInstance); // built-ins are not inlined
  5401. Assert(functionInfo != &JavascriptArray::EntryInfo::NewInstance); // built-ins are not inlined
  5402. return functionInfo != nullptr ?
  5403. JavascriptOperators::NewScObjectCommon(object, functionInfo, requestContext, isBaseClassConstructorNewScObject) :
  5404. JavascriptOperators::NewScObjectHostDispatchOrProxy(object, requestContext);
  5405. }
  5406. Var JavascriptOperators::NewScObjectNoArg(Var instance, ScriptContext * requestContext)
  5407. {
  5408. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoArg);
  5409. JavascriptProxy * proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  5410. if (proxy)
  5411. {
  5412. Arguments args(CallInfo(CallFlags_New, 1), &instance);
  5413. return requestContext->GetThreadContext()->ExecuteImplicitCall(proxy, Js::ImplicitCall_Accessor, [=]()->Js::Var
  5414. {
  5415. return proxy->ConstructorTrap(args, requestContext, 0);
  5416. });
  5417. }
  5418. FunctionInfo* functionInfo = JavascriptOperators::GetConstructorFunctionInfo(instance, requestContext);
  5419. RecyclableObject * object = RecyclableObject::FromVar(instance);
  5420. if (functionInfo == &JavascriptObject::EntryInfo::NewInstance)
  5421. {
  5422. // Fast path for new Object()
  5423. Assert((functionInfo->GetAttributes() & FunctionInfo::ErrorOnNew) == 0);
  5424. JavascriptLibrary* library = object->GetLibrary();
  5425. DynamicObject * newObject = library->CreateObject(true);
  5426. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newObject));
  5427. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5428. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5429. {
  5430. newObject = DynamicObject::FromVar(JavascriptProxy::AutoProxyWrapper(newObject));
  5431. }
  5432. #endif
  5433. #if DBG
  5434. DynamicType* newObjectType = newObject->GetDynamicType();
  5435. Assert(newObjectType->GetIsShared());
  5436. JavascriptFunction* constructor = JavascriptFunction::FromVar(instance);
  5437. Assert(!constructor->GetConstructorCache()->NeedsUpdateAfterCtor());
  5438. #endif
  5439. ScriptContext * scriptContext = library->GetScriptContext();
  5440. if (scriptContext != requestContext)
  5441. {
  5442. CrossSite::MarshalDynamicObjectAndPrototype(requestContext, newObject);
  5443. }
  5444. return newObject;
  5445. }
  5446. else if (functionInfo == &JavascriptArray::EntryInfo::NewInstance)
  5447. {
  5448. Assert((functionInfo->GetAttributes() & FunctionInfo::ErrorOnNew) == 0);
  5449. JavascriptLibrary* library = object->GetLibrary();
  5450. JavascriptArray * newArray = library->CreateArray();
  5451. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newArray));
  5452. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5453. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5454. {
  5455. newArray = static_cast<JavascriptArray*>(JavascriptProxy::AutoProxyWrapper(newArray));
  5456. }
  5457. #endif
  5458. #if DBG
  5459. DynamicType* newArrayType = newArray->GetDynamicType();
  5460. Assert(newArrayType->GetIsShared());
  5461. JavascriptFunction* constructor = JavascriptFunction::FromVar(instance);
  5462. Assert(!constructor->GetConstructorCache()->NeedsUpdateAfterCtor());
  5463. #endif
  5464. ScriptContext * scriptContext = library->GetScriptContext();
  5465. if (scriptContext != requestContext)
  5466. {
  5467. CrossSite::MarshalDynamicObjectAndPrototype(requestContext, newArray);
  5468. }
  5469. return newArray;
  5470. }
  5471. Var newObject = functionInfo != nullptr ?
  5472. JavascriptOperators::NewScObjectCommon(object, functionInfo, requestContext) :
  5473. JavascriptOperators::NewScObjectHostDispatchOrProxy(object, requestContext);
  5474. ThreadContext * threadContext = object->GetScriptContext()->GetThreadContext();
  5475. Var returnVar = threadContext->ExecuteImplicitCall(object, Js::ImplicitCall_Accessor, [=]()->Js::Var
  5476. {
  5477. return CALL_FUNCTION(threadContext, object, CallInfo(CallFlags_New, 1), newObject);
  5478. });
  5479. if (JavascriptOperators::IsObject(returnVar))
  5480. {
  5481. newObject = returnVar;
  5482. }
  5483. ConstructorCache * constructorCache = nullptr;
  5484. JavascriptFunction *function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  5485. if (function)
  5486. {
  5487. constructorCache = function->GetConstructorCache();
  5488. }
  5489. if (constructorCache != nullptr && constructorCache->NeedsUpdateAfterCtor())
  5490. {
  5491. JavascriptOperators::UpdateNewScObjectCache(object, newObject, requestContext);
  5492. }
  5493. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5494. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5495. {
  5496. DynamicObject* newDynamicObject = DynamicObject::FromVar(JavascriptProxy::AutoProxyWrapper(newObject));
  5497. // this might come from a different scriptcontext.
  5498. newObject = CrossSite::MarshalVar(requestContext, newDynamicObject, newDynamicObject->GetScriptContext());
  5499. }
  5500. #endif
  5501. return newObject;
  5502. JIT_HELPER_END(NewScObjectNoArg);
  5503. }
  5504. Var JavascriptOperators::NewScObjectNoCtorFull(Var instance, ScriptContext* requestContext)
  5505. {
  5506. // This helper can be reentrant because although we don't call the Constructor, we might have to parse it if bytecode is missing
  5507. // In which case, we would leave script. When we leave script we DisposeObjects which can dispose of Edge objects that could
  5508. // have a javascript onDispose handler and call that handler.
  5509. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoCtorFull);
  5510. return NewScObjectNoCtorCommon(instance, requestContext, true);
  5511. JIT_HELPER_END(NewScObjectNoCtorFull);
  5512. }
  5513. Var JavascriptOperators::NewScObjectNoCtor(Var instance, ScriptContext * requestContext)
  5514. {
  5515. // This helper can be reentrant because although we don't call the Constructor, we might have to parse it if bytecode is missing
  5516. // In which case, we would leave script. When we leave script we DisposeObjects which can dispose of Edge objects that could
  5517. // have a javascript onDispose handler and call that handler.
  5518. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoCtor);
  5519. // We can still call into NewScObjectNoCtor variations in JIT code for performance; however for proxy we don't
  5520. // really need the new object as the trap will handle the "this" pointer separately. pass back nullptr to ensure
  5521. // failure in invalid case.
  5522. return (JavascriptProxy::Is(instance)) ? nullptr : NewScObjectNoCtorCommon(instance, requestContext, false);
  5523. JIT_HELPER_END(NewScObjectNoCtor);
  5524. }
  5525. Var JavascriptOperators::NewScObjectNoCtorCommon(Var instance, ScriptContext* requestContext, bool isBaseClassConstructorNewScObject)
  5526. {
  5527. FunctionInfo* functionInfo = JavascriptOperators::GetConstructorFunctionInfo(instance, requestContext);
  5528. if (functionInfo)
  5529. {
  5530. return JavascriptOperators::NewScObjectCommon(RecyclableObject::UnsafeFromVar(instance), functionInfo, requestContext, isBaseClassConstructorNewScObject);
  5531. }
  5532. else
  5533. {
  5534. return JavascriptOperators::NewScObjectHostDispatchOrProxy(RecyclableObject::FromVar(instance), requestContext);
  5535. }
  5536. }
  5537. Var JavascriptOperators::NewScObjectHostDispatchOrProxy(RecyclableObject * function, ScriptContext * requestContext)
  5538. {
  5539. ScriptContext* functionScriptContext = function->GetScriptContext();
  5540. RecyclableObject * prototype = JavascriptOperators::GetPrototypeObject(function, functionScriptContext);
  5541. prototype = RecyclableObject::FromVar(CrossSite::MarshalVar(requestContext, prototype, functionScriptContext));
  5542. Var object = requestContext->GetLibrary()->CreateObject(prototype);
  5543. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(object));
  5544. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5545. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5546. {
  5547. object = DynamicObject::FromVar(JavascriptProxy::AutoProxyWrapper(object));
  5548. }
  5549. #endif
  5550. return object;
  5551. }
  5552. Var JavascriptOperators::NewScObjectCommon(RecyclableObject * function, FunctionInfo* functionInfo, ScriptContext * requestContext, bool isBaseClassConstructorNewScObject)
  5553. {
  5554. // CONSIDER: Allow for the cache to be repopulated if the type got collected, and a new one got populated with
  5555. // the same number of inlined slots. This requires that the JIT-ed code actually load the type from the cache
  5556. // (instead of hard-coding it), but it can (and must) keep the hard-coded number of inline slots.
  5557. // CONSIDER: Consider also not pinning the type in the cache. This can be done by using a registration based
  5558. // weak reference (we need to control the memory address), which we don't yet have, or by allocating the cache from
  5559. // the inline cache arena to allow it to be zeroed, but retain a recycler-allocated portion to hold on to the size of
  5560. // inlined slots.
  5561. JavascriptFunction* constructor = JavascriptFunction::UnsafeFromVar(function);
  5562. if (functionInfo->IsClassConstructor() && !isBaseClassConstructorNewScObject)
  5563. {
  5564. // If we are calling new on a class constructor, the contract is that we pass new.target as the 'this' argument.
  5565. // function is the constructor on which we called new - which is new.target.
  5566. // If we are trying to construct the object for a base class constructor as part of a super call, we should not
  5567. // store new.target in the 'this' argument.
  5568. return function;
  5569. }
  5570. ConstructorCache* constructorCache = constructor->GetConstructorCache();
  5571. AssertMsg(constructorCache->GetScriptContext() == nullptr || constructorCache->GetScriptContext() == constructor->GetScriptContext(),
  5572. "Why did we populate a constructor cache with a mismatched script context?");
  5573. Assert(constructorCache != nullptr);
  5574. DynamicType* type = constructorCache->GetGuardValueAsType();
  5575. if (type != nullptr && constructorCache->GetScriptContext() == requestContext)
  5576. {
  5577. #if DBG
  5578. bool cachedProtoCanBeCached;
  5579. Assert(type->GetPrototype() == JavascriptOperators::GetPrototypeObjectForConstructorCache(constructor, requestContext, cachedProtoCanBeCached));
  5580. Assert(cachedProtoCanBeCached);
  5581. Assert(type->GetIsShared());
  5582. #endif
  5583. #if DBG_DUMP
  5584. TraceUseConstructorCache(constructorCache, constructor, true);
  5585. #endif
  5586. Var object = DynamicObject::New(requestContext->GetRecycler(), type);
  5587. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(object));
  5588. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5589. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5590. {
  5591. object = DynamicObject::FromVar(JavascriptProxy::AutoProxyWrapper(object));
  5592. }
  5593. #endif
  5594. return object;
  5595. }
  5596. if (constructorCache->SkipDefaultNewObject())
  5597. {
  5598. Assert(!constructorCache->NeedsUpdateAfterCtor());
  5599. #if DBG_DUMP
  5600. TraceUseConstructorCache(constructorCache, constructor, true);
  5601. #endif
  5602. if (isBaseClassConstructorNewScObject)
  5603. {
  5604. return JavascriptOperators::CreateFromConstructor(function, requestContext);
  5605. }
  5606. return nullptr;
  5607. }
  5608. #if DBG_DUMP
  5609. TraceUseConstructorCache(constructorCache, constructor, false);
  5610. #endif
  5611. ScriptContext* constructorScriptContext = function->GetScriptContext();
  5612. Assert(!constructorScriptContext->GetThreadContext()->IsDisableImplicitException());
  5613. // we shouldn't try to call the constructor if it's closed already.
  5614. constructorScriptContext->VerifyAlive(TRUE, requestContext);
  5615. FunctionInfo::Attributes attributes = functionInfo->GetAttributes();
  5616. if (attributes & FunctionInfo::ErrorOnNew)
  5617. {
  5618. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnNew);
  5619. }
  5620. // Slow path
  5621. FunctionProxy * ctorProxy = constructor->GetFunctionProxy();
  5622. FunctionBody * functionBody = ctorProxy != nullptr ? ctorProxy->EnsureDeserialized()->Parse() : nullptr;
  5623. if (attributes & FunctionInfo::SkipDefaultNewObject)
  5624. {
  5625. // The constructor doesn't use the default new object.
  5626. #pragma prefast(suppress:6236, "DevDiv bug 830883. False positive when PHASE_OFF is #defined as '(false)'.")
  5627. if (!PHASE_OFF1(ConstructorCachePhase) && (functionBody == nullptr || !PHASE_OFF(ConstructorCachePhase, functionBody)))
  5628. {
  5629. constructorCache = constructor->EnsureValidConstructorCache();
  5630. constructorCache->PopulateForSkipDefaultNewObject(constructorScriptContext);
  5631. #if DBG_DUMP
  5632. if ((functionBody != nullptr && PHASE_TRACE(Js::ConstructorCachePhase, functionBody)) || (functionBody == nullptr && PHASE_TRACE1(Js::ConstructorCachePhase)))
  5633. {
  5634. const char16* ctorName = functionBody != nullptr ? functionBody->GetDisplayName() : _u("<unknown>");
  5635. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5636. Output::Print(_u("CtorCache: populated cache (0x%p) for ctor %s (%s): "), constructorCache, ctorName,
  5637. functionBody ? functionBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"));
  5638. constructorCache->Dump();
  5639. Output::Print(_u("\n"));
  5640. Output::Flush();
  5641. }
  5642. #endif
  5643. }
  5644. Assert(!constructorCache->NeedsUpdateAfterCtor());
  5645. return nullptr;
  5646. }
  5647. // CONSIDER: Create some form of PatchGetProtoObjForCtorCache, which actually caches the prototype object in the constructor cache.
  5648. // 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
  5649. // after we fail the guard check. When invalidating the cache for proto change, make sure we zap the prototype field of the cache in
  5650. // addition to the guard value.
  5651. bool prototypeCanBeCached;
  5652. RecyclableObject* prototype = JavascriptOperators::GetPrototypeObjectForConstructorCache(
  5653. function, constructorScriptContext, prototypeCanBeCached);
  5654. prototype = RecyclableObject::FromVar(CrossSite::MarshalVar(requestContext,
  5655. prototype, constructorScriptContext));
  5656. DynamicObject* newObject = requestContext->GetLibrary()->CreateObject(prototype, 8);
  5657. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newObject));
  5658. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5659. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5660. {
  5661. newObject = DynamicObject::FromVar(JavascriptProxy::AutoProxyWrapper(newObject));
  5662. }
  5663. #endif
  5664. Assert(newObject->GetTypeHandler()->GetPropertyCount() == 0);
  5665. if (prototypeCanBeCached && functionBody != nullptr && requestContext == constructorScriptContext &&
  5666. !Js::JavascriptProxy::Is(newObject) &&
  5667. !PHASE_OFF1(ConstructorCachePhase) && !PHASE_OFF(ConstructorCachePhase, functionBody))
  5668. {
  5669. DynamicType* newObjectType = newObject->GetDynamicType();
  5670. // Initial type (without any properties) should always be shared up-front. This allows us to populate the cache right away.
  5671. Assert(newObjectType->GetIsShared());
  5672. // Populate the cache here and set the updateAfterCtor flag. This way, if the ctor is called recursively the
  5673. // recursive calls will hit the cache and use the initial type. On the unwind path, we will update the cache
  5674. // after the innermost ctor and clear the flag. After subsequent ctors we won't attempt an update anymore.
  5675. // As long as the updateAfterCtor flag is set it is safe to update the cache, because it would not have been
  5676. // hard-coded in the JIT-ed code.
  5677. constructorCache = constructor->EnsureValidConstructorCache();
  5678. constructorCache->Populate(newObjectType, constructorScriptContext, functionBody->GetHasNoExplicitReturnValue(), true);
  5679. Assert(constructorCache->IsConsistent());
  5680. #if DBG_DUMP
  5681. if ((functionBody != nullptr && PHASE_TRACE(Js::ConstructorCachePhase, functionBody)) || (functionBody == nullptr && PHASE_TRACE1(Js::ConstructorCachePhase)))
  5682. {
  5683. const char16* ctorName = functionBody != nullptr ? functionBody->GetDisplayName() : _u("<unknown>");
  5684. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5685. Output::Print(_u("CtorCache: populated cache (0x%p) for ctor %s (%s): "), constructorCache, ctorName,
  5686. functionBody ? functionBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"));
  5687. constructorCache->Dump();
  5688. Output::Print(_u("\n"));
  5689. Output::Flush();
  5690. }
  5691. #endif
  5692. }
  5693. else
  5694. {
  5695. #if DBG_DUMP
  5696. if ((functionBody != nullptr && PHASE_TRACE(Js::ConstructorCachePhase, functionBody)) || (functionBody == nullptr && PHASE_TRACE1(Js::ConstructorCachePhase)))
  5697. {
  5698. const char16* ctorName = functionBody != nullptr ? functionBody->GetDisplayName() : _u("<unknown>");
  5699. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5700. 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"),
  5701. constructorCache, ctorName, functionBody ? functionBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"),
  5702. !prototypeCanBeCached ? _u("prototype cannot be cached") :
  5703. functionBody == nullptr ? _u("function has no body") :
  5704. requestContext != constructorScriptContext ? _u("of cross-context call") : _u("constructor cache phase is off"),
  5705. prototype, functionBody, constructorScriptContext, requestContext);
  5706. Output::Print(_u("\n"));
  5707. Output::Flush();
  5708. }
  5709. #endif
  5710. }
  5711. return newObject;
  5712. }
  5713. void JavascriptOperators::UpdateNewScObjectCache(Var function, Var instance, ScriptContext* requestContext)
  5714. {
  5715. JIT_HELPER_NOT_REENTRANT_HEADER(UpdateNewScObjectCache, reentrancylock, requestContext->GetThreadContext());
  5716. JavascriptFunction* constructor = JavascriptFunction::FromVar(function);
  5717. if(constructor->GetScriptContext() != requestContext)
  5718. {
  5719. // The cache is populated only when the constructor function's context is the same as the calling context. However,
  5720. // the cached type is not finalized yet and may not be until multiple calls to the constructor have been made (see
  5721. // flag ConstructorCallsRequiredToFinalizeCachedType). A subsequent call to the constructor may be made from a
  5722. // different context, so ignore those cross-context calls and wait for the constructor to be called from its own
  5723. // context again to finalize the cached type.
  5724. return;
  5725. }
  5726. // Review : What happens if the cache got invalidated between NewScObject and here?
  5727. // Should we allocate new? Should we mark it as polymorphic?
  5728. ConstructorCache* constructorCache = constructor->GetConstructorCache();
  5729. Assert(constructorCache->IsConsistent());
  5730. Assert(!ConstructorCache::IsDefault(constructorCache));
  5731. AssertMsg(constructorCache->GetScriptContext() == constructor->GetScriptContext(), "Why did we populate a constructor cache with a mismatched script context?");
  5732. AssertMsg(constructorCache->IsPopulated(), "Why are we updating a constructor cache that hasn't been populated?");
  5733. // The presence of the updateAfterCtor flag guarantees that this cache hasn't been used in JIT-ed fast path. Even, if the
  5734. // cache is invalidated, this flag is not changed.
  5735. AssertMsg(constructorCache->NeedsUpdateAfterCtor(), "Why are we updating a constructor cache that doesn't need to be updated?");
  5736. const bool finalizeCachedType =
  5737. constructorCache->CallCount() >= CONFIG_FLAG(ConstructorCallsRequiredToFinalizeCachedType);
  5738. if(!finalizeCachedType)
  5739. {
  5740. constructorCache->IncCallCount();
  5741. }
  5742. else
  5743. {
  5744. constructorCache->ClearUpdateAfterCtor();
  5745. }
  5746. FunctionBody* constructorBody = constructor->GetFunctionBody();
  5747. AssertMsg(constructorBody != nullptr, "Constructor function doesn't have a function body.");
  5748. Assert(RecyclableObject::Is(instance));
  5749. // The cache might have been invalidated between NewScObjectCommon and UpdateNewScObjectCache. This could occur, for example, if
  5750. // the constructor updates its own prototype property. If that happens we don't want to re-populate it here. A new cache will
  5751. // be created when the constructor is called again.
  5752. if (constructorCache->IsInvalidated())
  5753. {
  5754. #if DBG_DUMP
  5755. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because cache is invalidated"));
  5756. #endif
  5757. return;
  5758. }
  5759. Assert(constructorCache->GetGuardValueAsType() != nullptr);
  5760. if (DynamicType::Is(RecyclableObject::FromVar(instance)->GetTypeId()))
  5761. {
  5762. DynamicObject *object = DynamicObject::UnsafeFromVar(instance);
  5763. DynamicType* type = object->GetDynamicType();
  5764. DynamicTypeHandler* typeHandler = type->GetTypeHandler();
  5765. if (constructorBody->GetHasOnlyThisStmts())
  5766. {
  5767. if (typeHandler->IsSharable())
  5768. {
  5769. #if DBG
  5770. bool cachedProtoCanBeCached = false;
  5771. Assert(type->GetPrototype() == JavascriptOperators::GetPrototypeObjectForConstructorCache(constructor, requestContext, cachedProtoCanBeCached));
  5772. Assert(cachedProtoCanBeCached);
  5773. Assert(type->GetScriptContext() == constructorCache->GetScriptContext());
  5774. Assert(type->GetPrototype() == constructorCache->GetType()->GetPrototype());
  5775. #endif
  5776. typeHandler->SetMayBecomeShared();
  5777. // CONSIDER: Remove only this for delayed type sharing.
  5778. type->ShareType();
  5779. #if ENABLE_PROFILE_INFO
  5780. DynamicProfileInfo* profileInfo = constructorBody->HasDynamicProfileInfo() ? constructorBody->GetAnyDynamicProfileInfo() : nullptr;
  5781. if ((profileInfo != nullptr && profileInfo->GetImplicitCallFlags() <= ImplicitCall_None) ||
  5782. CheckIfPrototypeChainHasOnlyWritableDataProperties(type->GetPrototype()))
  5783. {
  5784. Assert(typeHandler->GetPropertyCount() < Js::PropertyIndexRanges<PropertyIndex>::MaxValue);
  5785. for (PropertyIndex pi = 0; pi < typeHandler->GetPropertyCount(); pi++)
  5786. {
  5787. requestContext->RegisterConstructorCache(typeHandler->GetPropertyId(requestContext, pi), constructorCache);
  5788. }
  5789. Assert(constructorBody->GetUtf8SourceInfo()->GetIsLibraryCode() || !constructor->GetScriptContext()->IsScriptContextInDebugMode());
  5790. if (constructorCache->TryUpdateAfterConstructor(type, constructor->GetScriptContext()))
  5791. {
  5792. #if DBG_DUMP
  5793. TraceUpdateConstructorCache(constructorCache, constructorBody, true, _u(""));
  5794. #endif
  5795. }
  5796. else
  5797. {
  5798. #if DBG_DUMP
  5799. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because number of slots > MaxCachedSlotCount"));
  5800. #endif
  5801. }
  5802. }
  5803. #if DBG_DUMP
  5804. else
  5805. {
  5806. if (profileInfo &&
  5807. ((profileInfo->GetImplicitCallFlags() & ~(Js::ImplicitCall_External | Js::ImplicitCall_Accessor)) == 0) &&
  5808. profileInfo != nullptr && CheckIfPrototypeChainHasOnlyWritableDataProperties(type->GetPrototype()) &&
  5809. Js::Configuration::Global.flags.Trace.IsEnabled(Js::HostOptPhase))
  5810. {
  5811. const char16* ctorName = constructorBody->GetDisplayName();
  5812. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5813. Output::Print(_u("CtorCache: %s cache (0x%p) for ctor %s (#%u) did not update because external call"),
  5814. constructorCache, constructorBody, ctorName, constructorBody ? constructorBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"));
  5815. Output::Print(_u("\n"));
  5816. Output::Flush();
  5817. }
  5818. }
  5819. #endif
  5820. #endif
  5821. }
  5822. else
  5823. {
  5824. // Dynamic type created is not sharable.
  5825. // So in future don't try to check for "this assignment optimization".
  5826. constructorBody->SetHasOnlyThisStmts(false);
  5827. #if DBG_DUMP
  5828. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because final type is not shareable"));
  5829. #endif
  5830. }
  5831. }
  5832. else
  5833. {
  5834. #if DBG_DUMP
  5835. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because ctor has not only this statements"));
  5836. #endif
  5837. }
  5838. }
  5839. else
  5840. {
  5841. // Even though this constructor apparently returned something other than the default object we created,
  5842. // it still makes sense to cache the parameters of the default object, since we must create it every time, anyway.
  5843. #if DBG_DUMP
  5844. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because ctor return a non-object value"));
  5845. #endif
  5846. return;
  5847. }
  5848. // Whatever the constructor returned, if we're caching a type we want to be sure we shrink its inline slot capacity.
  5849. if (finalizeCachedType && constructorCache->IsEnabled())
  5850. {
  5851. DynamicType* cachedType = constructorCache->NeedsTypeUpdate() ? constructorCache->GetPendingType() : constructorCache->GetType();
  5852. DynamicTypeHandler* cachedTypeHandler = cachedType->GetTypeHandler();
  5853. // Consider: We could delay inline slot capacity shrinking until the second time this constructor is invoked. In some cases
  5854. // this might permit more properties to remain inlined if the objects grow after constructor. This would require flagging
  5855. // the cache as special (already possible) and forcing the shrinking during work item creation if we happen to JIT this
  5856. // constructor while the cache is in this special state.
  5857. if (cachedTypeHandler->GetInlineSlotCapacity())
  5858. {
  5859. #if DBG_DUMP
  5860. int inlineSlotCapacityBeforeShrink = cachedTypeHandler->GetInlineSlotCapacity();
  5861. #endif
  5862. // Note that after the cache has been updated and might have been used in the JIT-ed code, it is no longer legal to
  5863. // shrink the inline slot capacity of the type. That's because we allocate memory for a fixed number of inlined properties
  5864. // and if that number changed on the type, this update wouldn't get reflected in JIT-ed code and we would allocate objects
  5865. // of a wrong size. This could conceivably happen if the original object got collected, and with it some of the successor
  5866. // types also. If then another constructor has the same prototype and needs to populate its own cache, it would attempt to
  5867. // shrink inlined slots again. If all surviving type handlers have smaller inline slot capacity, we would shrink it further.
  5868. // To address this problem the type handler has a bit indicating its inline slots have been shrunk already. If that bit is
  5869. // set ShrinkSlotAndInlineSlotCapacity does nothing.
  5870. cachedTypeHandler->ShrinkSlotAndInlineSlotCapacity();
  5871. constructorCache->UpdateInlineSlotCount();
  5872. #if DBG_DUMP
  5873. Assert(inlineSlotCapacityBeforeShrink >= cachedTypeHandler->GetInlineSlotCapacity());
  5874. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::InlineSlotsPhase))
  5875. {
  5876. if (inlineSlotCapacityBeforeShrink != cachedTypeHandler->GetInlineSlotCapacity())
  5877. {
  5878. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5879. Output::Print(_u("Inline slot capacity shrunk: Function:%04s Before:%d After:%d\n"),
  5880. constructorBody->GetDebugNumberSet(debugStringBuffer), inlineSlotCapacityBeforeShrink, cachedTypeHandler->GetInlineSlotCapacity());
  5881. }
  5882. }
  5883. #endif
  5884. }
  5885. }
  5886. JIT_HELPER_END(UpdateNewScObjectCache);
  5887. }
  5888. void JavascriptOperators::TraceUseConstructorCache(const ConstructorCache* ctorCache, const JavascriptFunction* ctor, bool isHit)
  5889. {
  5890. #if DBG_DUMP
  5891. // We are under debug, so we can incur the extra check here.
  5892. FunctionProxy* ctorBody = ctor->GetFunctionProxy();
  5893. if (ctorBody != nullptr && !ctorBody->GetScriptContext()->IsClosed())
  5894. {
  5895. ctorBody = ctorBody->EnsureDeserialized();
  5896. }
  5897. if ((ctorBody != nullptr && PHASE_TRACE(Js::ConstructorCachePhase, ctorBody)) || (ctorBody == nullptr && PHASE_TRACE1(Js::ConstructorCachePhase)))
  5898. {
  5899. const char16* ctorName = ctorBody != nullptr ? ctorBody->GetDisplayName() : _u("<unknown>");
  5900. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5901. Output::Print(_u("CtorCache: %s cache (0x%p) for ctor %s (%s): "), isHit ? _u("hit") : _u("missed"), ctorCache, ctorName,
  5902. ctorBody ? ctorBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"));
  5903. ctorCache->Dump();
  5904. Output::Print(_u("\n"));
  5905. Output::Flush();
  5906. }
  5907. #endif
  5908. }
  5909. void JavascriptOperators::TraceUpdateConstructorCache(const ConstructorCache* ctorCache, const FunctionBody* ctorBody, bool updated, const char16* reason)
  5910. {
  5911. #if DBG_DUMP
  5912. if (PHASE_TRACE(Js::ConstructorCachePhase, ctorBody))
  5913. {
  5914. const char16* ctorName = ctorBody->GetDisplayName();
  5915. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5916. Output::Print(_u("CtorCache: %s cache (0x%p) for ctor %s (%s)%s %s: "),
  5917. updated ? _u("updated") : _u("did not update"), ctorBody, ctorName,
  5918. ctorBody ? const_cast<Js::FunctionBody *>(ctorBody)->GetDebugNumberSet(debugStringBuffer) : _u("(null)"),
  5919. updated ? _u("") : _u(", because") , reason);
  5920. ctorCache->Dump();
  5921. Output::Print(_u("\n"));
  5922. Output::Flush();
  5923. }
  5924. #endif
  5925. }
  5926. Var JavascriptOperators::NewScObject(const Var callee, const Arguments args, ScriptContext *const scriptContext, const Js::AuxArray<uint32> *spreadIndices)
  5927. {
  5928. Assert(callee);
  5929. Assert(args.Info.Count != 0);
  5930. Assert(scriptContext);
  5931. // Always save and restore implicit call flags when calling out
  5932. // REVIEW: Can we avoid it if we don't collect dynamic profile info?
  5933. ThreadContext *const threadContext = scriptContext->GetThreadContext();
  5934. const ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  5935. const Var newVarInstance = JavascriptFunction::CallAsConstructor(callee, /* overridingNewTarget = */nullptr, args, scriptContext, spreadIndices);
  5936. threadContext->SetImplicitCallFlags(savedImplicitCallFlags);
  5937. return newVarInstance;
  5938. }
  5939. Js::GlobalObject * JavascriptOperators::OP_LdRoot(ScriptContext* scriptContext)
  5940. {
  5941. return scriptContext->GetGlobalObject();
  5942. }
  5943. Js::ModuleRoot * JavascriptOperators::GetModuleRoot(int moduleID, ScriptContext* scriptContext)
  5944. {
  5945. Assert(moduleID != kmodGlobal);
  5946. JavascriptLibrary* library = scriptContext->GetLibrary();
  5947. HostObjectBase *hostObject = library->GetGlobalObject()->GetHostObject();
  5948. if (hostObject)
  5949. {
  5950. Js::ModuleRoot * moduleRoot = hostObject->GetModuleRoot(moduleID);
  5951. Assert(!CrossSite::NeedMarshalVar(moduleRoot, scriptContext));
  5952. return moduleRoot;
  5953. }
  5954. HostScriptContext *hostScriptContext = scriptContext->GetHostScriptContext();
  5955. if (hostScriptContext)
  5956. {
  5957. Js::ModuleRoot * moduleRoot = hostScriptContext->GetModuleRoot(moduleID);
  5958. Assert(!CrossSite::NeedMarshalVar(moduleRoot, scriptContext));
  5959. return moduleRoot;
  5960. }
  5961. Assert(FALSE);
  5962. return nullptr;
  5963. }
  5964. Var JavascriptOperators::OP_LoadModuleRoot(int moduleID, ScriptContext* scriptContext)
  5965. {
  5966. Js::ModuleRoot * moduleRoot = GetModuleRoot(moduleID, scriptContext);
  5967. if (moduleRoot)
  5968. {
  5969. return moduleRoot;
  5970. }
  5971. Assert(false);
  5972. return scriptContext->GetLibrary()->GetUndefined();
  5973. }
  5974. Var JavascriptOperators::OP_LdNull(ScriptContext* scriptContext)
  5975. {
  5976. return scriptContext->GetLibrary()->GetNull();
  5977. }
  5978. Var JavascriptOperators::OP_LdUndef(ScriptContext* scriptContext)
  5979. {
  5980. return scriptContext->GetLibrary()->GetUndefined();
  5981. }
  5982. Var JavascriptOperators::OP_LdNaN(ScriptContext* scriptContext)
  5983. {
  5984. return scriptContext->GetLibrary()->GetNaN();
  5985. }
  5986. Var JavascriptOperators::OP_LdChakraLib(ScriptContext* scriptContext)
  5987. {
  5988. return scriptContext->GetLibrary()->GetChakraLib();
  5989. }
  5990. Var JavascriptOperators::OP_LdInfinity(ScriptContext* scriptContext)
  5991. {
  5992. return scriptContext->GetLibrary()->GetPositiveInfinite();
  5993. }
  5994. void JavascriptOperators::BuildHandlerScope(Var argThis, RecyclableObject * hostObject, FrameDisplay * pDisplay, ScriptContext * scriptContext)
  5995. {
  5996. // Event handlers need implicit lookups of @@unscopables on parent scopes.
  5997. // We can intercept the property accesses by wrapping the object with the unscopables handler.
  5998. // WebIDL: https://heycam.github.io/webidl/#ref-for-Unscopable
  5999. Assert(argThis != nullptr);
  6000. pDisplay->SetItem(0, TaggedNumber::Is(argThis) ? scriptContext->GetLibrary()->CreateNumberObject(argThis) : ToUnscopablesWrapperObject(argThis, scriptContext));
  6001. uint16 i = 1;
  6002. Var aChild = argThis;
  6003. uint16 length = pDisplay->GetLength();
  6004. // Now add any parent scopes
  6005. // We need to support the namespace parent lookup in both fastDOM on and off scenario.
  6006. while (aChild != NULL)
  6007. {
  6008. Var aParent = hostObject->GetNamespaceParent(aChild);
  6009. if (aParent == nullptr)
  6010. {
  6011. break;
  6012. }
  6013. aParent = CrossSite::MarshalVar(scriptContext, aParent);
  6014. if (i == length)
  6015. {
  6016. length = UInt16Math::Add(length, 8);
  6017. FrameDisplay * tmp = RecyclerNewPlus(scriptContext->GetRecycler(), length * sizeof(void*), FrameDisplay, length);
  6018. js_memcpy_s((char*)tmp + tmp->GetOffsetOfScopes(), tmp->GetLength() * sizeof(void *), (char*)pDisplay + pDisplay->GetOffsetOfScopes(), pDisplay->GetLength() * sizeof(void*));
  6019. pDisplay = tmp;
  6020. }
  6021. Var aParentWrapped = ToUnscopablesWrapperObject(aParent, scriptContext);
  6022. pDisplay->SetItem(i, aParentWrapped);
  6023. aChild = aParent;
  6024. i++;
  6025. }
  6026. Assert(i <= pDisplay->GetLength());
  6027. pDisplay->SetLength(i);
  6028. }
  6029. FrameDisplay * JavascriptOperators::OP_LdHandlerScope(Var argThis, ScriptContext* scriptContext)
  6030. {
  6031. JIT_HELPER_REENTRANT_HEADER(ScrObj_LdHandlerScope);
  6032. // The idea here is to build a stack of nested scopes in the form of a JS array.
  6033. //
  6034. // The scope stack for an event handler looks like this:
  6035. //
  6036. // implicit "this"
  6037. // implicit namespace parent scopes
  6038. // Put the implicit "this"
  6039. if (argThis != NULL)
  6040. {
  6041. RecyclableObject* hostObject = scriptContext->GetGlobalObject()->GetHostObject();
  6042. if (hostObject == nullptr)
  6043. {
  6044. hostObject = scriptContext->GetGlobalObject()->GetDirectHostObject();
  6045. }
  6046. if (hostObject != nullptr)
  6047. {
  6048. uint16 length = 7;
  6049. FrameDisplay *pDisplay =
  6050. RecyclerNewPlus(scriptContext->GetRecycler(), length * sizeof(void*), FrameDisplay, length);
  6051. BuildHandlerScope(argThis, hostObject, pDisplay, scriptContext);
  6052. return pDisplay;
  6053. }
  6054. }
  6055. return const_cast<FrameDisplay *>(&Js::NullFrameDisplay);
  6056. JIT_HELPER_END(ScrObj_LdHandlerScope);
  6057. }
  6058. FrameDisplay* JavascriptOperators::OP_LdFrameDisplay(void *argHead, void *argEnv, ScriptContext* scriptContext)
  6059. {
  6060. JIT_HELPER_NOT_REENTRANT_HEADER(ScrObj_LdFrameDisplay, reentrancylock, scriptContext->GetThreadContext());
  6061. // Build a display of nested frame objects.
  6062. // argHead is the current scope; argEnv is either the lone trailing scope or an array of scopes
  6063. // which we append to the new display.
  6064. // Note that there are cases in which a function with no local frame must construct a display to pass
  6065. // to the function(s) nested within it. In such a case, argHead will be a null object, and it's not
  6066. // strictly necessary to include it. But such cases are rare and not perf critical, so it's not
  6067. // worth the extra complexity to notify the nested functions that they can "skip" this slot in the
  6068. // frame display when they're loading scopes nested outside it.
  6069. FrameDisplay *pDisplay = nullptr;
  6070. FrameDisplay *envDisplay = (FrameDisplay*)argEnv;
  6071. uint16 length = UInt16Math::Add(envDisplay->GetLength(), 1);
  6072. pDisplay = RecyclerNewPlus(scriptContext->GetRecycler(), length * sizeof(void*), FrameDisplay, length);
  6073. for (uint16 j = 0; j < length - 1; j++)
  6074. {
  6075. pDisplay->SetItem(j + 1, envDisplay->GetItem(j));
  6076. }
  6077. pDisplay->SetItem(0, argHead);
  6078. return pDisplay;
  6079. JIT_HELPER_END(ScrObj_LdFrameDisplay);
  6080. }
  6081. FrameDisplay* JavascriptOperators::OP_LdFrameDisplayNoParent(void *argHead, ScriptContext* scriptContext)
  6082. {
  6083. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdFrameDisplayNoParent);
  6084. return OP_LdFrameDisplay(argHead, (void*)&NullFrameDisplay, scriptContext);
  6085. JIT_HELPER_END(ScrObj_LdFrameDisplayNoParent);
  6086. }
  6087. FrameDisplay* JavascriptOperators::OP_LdStrictFrameDisplay(void *argHead, void *argEnv, ScriptContext* scriptContext)
  6088. {
  6089. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdStrictFrameDisplay);
  6090. FrameDisplay * pDisplay = OP_LdFrameDisplay(argHead, argEnv, scriptContext);
  6091. pDisplay->SetStrictMode(true);
  6092. return pDisplay;
  6093. JIT_HELPER_END(ScrObj_LdStrictFrameDisplay);
  6094. }
  6095. FrameDisplay* JavascriptOperators::OP_LdStrictFrameDisplayNoParent(void *argHead, ScriptContext* scriptContext)
  6096. {
  6097. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdStrictFrameDisplayNoParent);
  6098. return OP_LdStrictFrameDisplay(argHead, (void*)&StrictNullFrameDisplay, scriptContext);
  6099. JIT_HELPER_END(ScrObj_LdStrictFrameDisplayNoParent);
  6100. }
  6101. FrameDisplay* JavascriptOperators::OP_LdInnerFrameDisplay(void *argHead, void *argEnv, ScriptContext* scriptContext)
  6102. {
  6103. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdInnerFrameDisplay);
  6104. CheckInnerFrameDisplayArgument(argHead);
  6105. return OP_LdFrameDisplay(argHead, argEnv, scriptContext);
  6106. JIT_HELPER_END(ScrObj_LdInnerFrameDisplay);
  6107. }
  6108. FrameDisplay* JavascriptOperators::OP_LdInnerFrameDisplayNoParent(void *argHead, ScriptContext* scriptContext)
  6109. {
  6110. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdInnerFrameDisplayNoParent);
  6111. CheckInnerFrameDisplayArgument(argHead);
  6112. return OP_LdFrameDisplayNoParent(argHead, scriptContext);
  6113. JIT_HELPER_END(ScrObj_LdInnerFrameDisplayNoParent);
  6114. }
  6115. FrameDisplay* JavascriptOperators::OP_LdStrictInnerFrameDisplay(void *argHead, void *argEnv, ScriptContext* scriptContext)
  6116. {
  6117. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdStrictInnerFrameDisplay);
  6118. CheckInnerFrameDisplayArgument(argHead);
  6119. return OP_LdStrictFrameDisplay(argHead, argEnv, scriptContext);
  6120. JIT_HELPER_END(ScrObj_LdStrictInnerFrameDisplay);
  6121. }
  6122. FrameDisplay* JavascriptOperators::OP_LdStrictInnerFrameDisplayNoParent(void *argHead, ScriptContext* scriptContext)
  6123. {
  6124. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdStrictInnerFrameDisplayNoParent);
  6125. CheckInnerFrameDisplayArgument(argHead);
  6126. return OP_LdStrictFrameDisplayNoParent(argHead, scriptContext);
  6127. JIT_HELPER_END(ScrObj_LdStrictInnerFrameDisplayNoParent);
  6128. }
  6129. void JavascriptOperators::CheckInnerFrameDisplayArgument(void *argHead)
  6130. {
  6131. if (ThreadContext::IsOnStack(argHead))
  6132. {
  6133. AssertMsg(false, "Illegal byte code: stack object as with scope");
  6134. Js::Throw::FatalInternalError();
  6135. }
  6136. if (!RecyclableObject::Is(argHead))
  6137. {
  6138. AssertMsg(false, "Illegal byte code: non-object as with scope");
  6139. Js::Throw::FatalInternalError();
  6140. }
  6141. }
  6142. Js::PropertyId JavascriptOperators::GetPropertyId(Var propertyName, ScriptContext* scriptContext)
  6143. {
  6144. PropertyRecord const * propertyRecord = nullptr;
  6145. JavascriptSymbol * symbol = JavascriptOperators::TryFromVar<Js::JavascriptSymbol>(propertyName);
  6146. if (symbol)
  6147. {
  6148. propertyRecord = symbol->GetValue();
  6149. }
  6150. else
  6151. {
  6152. JavascriptSymbolObject * symbolObject = JavascriptOperators::TryFromVar<JavascriptSymbolObject>(propertyName);
  6153. if (symbolObject)
  6154. {
  6155. propertyRecord = symbolObject->GetValue();
  6156. }
  6157. else
  6158. {
  6159. JavascriptString * indexStr = JavascriptConversion::ToString(propertyName, scriptContext);
  6160. scriptContext->GetOrAddPropertyRecord(indexStr, &propertyRecord);
  6161. }
  6162. }
  6163. return propertyRecord->GetPropertyId();
  6164. }
  6165. void JavascriptOperators::OP_InitSetter(Var object, PropertyId propertyId, Var setter)
  6166. {
  6167. AssertMsg(!TaggedNumber::Is(object), "SetMember on a non-object?");
  6168. RecyclableObject* recylableObject = RecyclableObject::FromVar(object);
  6169. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitSetter, reentrancylock, recylableObject->GetScriptContext()->GetThreadContext());
  6170. recylableObject->SetAccessors(propertyId, nullptr, setter);
  6171. JIT_HELPER_END(OP_InitSetter);
  6172. }
  6173. void JavascriptOperators::OP_InitClassMemberSet(Var object, PropertyId propertyId, Var setter)
  6174. {
  6175. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_InitClassMemberSet);
  6176. JIT_HELPER_SAME_ATTRIBUTES(Op_InitClassMemberSet, OP_InitSetter);
  6177. JavascriptOperators::OP_InitSetter(object, propertyId, setter);
  6178. RecyclableObject::FromVar(object)->SetAttributes(propertyId, PropertyClassMemberDefaults);
  6179. JIT_HELPER_END(Op_InitClassMemberSet);
  6180. }
  6181. Js::PropertyId JavascriptOperators::OP_InitElemSetter(Var object, Var elementName, Var setter, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6182. {
  6183. JIT_HELPER_REENTRANT_HEADER(OP_InitElemSetter);
  6184. AssertMsg(!TaggedNumber::Is(object), "SetMember on a non-object?");
  6185. PropertyId propertyId = JavascriptOperators::GetPropertyId(elementName, scriptContext);
  6186. RecyclableObject::FromVar(object)->SetAccessors(propertyId, nullptr, setter);
  6187. return propertyId;
  6188. JIT_HELPER_END(OP_InitElemSetter);
  6189. }
  6190. Field(Var)* JavascriptOperators::OP_GetModuleExportSlotArrayAddress(uint moduleIndex, uint slotIndex, ScriptContextInfo* scriptContext)
  6191. {
  6192. return scriptContext->GetModuleExportSlotArrayAddress(moduleIndex, slotIndex);
  6193. }
  6194. Field(Var)* JavascriptOperators::OP_GetModuleExportSlotAddress(uint moduleIndex, uint slotIndex, ScriptContext* scriptContext)
  6195. {
  6196. Field(Var)* moduleRecordSlots = OP_GetModuleExportSlotArrayAddress(moduleIndex, slotIndex, scriptContext);
  6197. Assert(moduleRecordSlots != nullptr);
  6198. return &moduleRecordSlots[slotIndex];
  6199. }
  6200. Var JavascriptOperators::OP_LdModuleSlot(uint moduleIndex, uint slotIndex, ScriptContext* scriptContext)
  6201. {
  6202. Field(Var)* addr = OP_GetModuleExportSlotAddress(moduleIndex, slotIndex, scriptContext);
  6203. Assert(addr != nullptr);
  6204. return *addr;
  6205. }
  6206. void JavascriptOperators::OP_StModuleSlot(uint moduleIndex, uint slotIndex, Var value, ScriptContext* scriptContext)
  6207. {
  6208. Assert(value != nullptr);
  6209. Field(Var)* addr = OP_GetModuleExportSlotAddress(moduleIndex, slotIndex, scriptContext);
  6210. Assert(addr != nullptr);
  6211. *addr = value;
  6212. }
  6213. void JavascriptOperators::OP_InitClassMemberSetComputedName(Var object, Var elementName, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6214. {
  6215. JIT_HELPER_REENTRANT_HEADER(Op_InitClassMemberSetComputedName);
  6216. Js::PropertyId propertyId = JavascriptOperators::OP_InitElemSetter(object, elementName, value, scriptContext);
  6217. RecyclableObject* instance = RecyclableObject::FromVar(object);
  6218. // instance will be a function if it is the class constructor (otherwise it would be an object)
  6219. if (JavascriptFunction::Is(instance) && Js::PropertyIds::prototype == propertyId)
  6220. {
  6221. // It is a TypeError to have a static member with a computed name that evaluates to 'prototype'
  6222. JavascriptError::ThrowTypeError(scriptContext, JSERR_ClassStaticMethodCannotBePrototype);
  6223. }
  6224. instance->SetAttributes(propertyId, PropertyClassMemberDefaults);
  6225. JIT_HELPER_END(Op_InitClassMemberSetComputedName);
  6226. }
  6227. BOOL JavascriptOperators::IsClassConstructor(Var instance)
  6228. {
  6229. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_IsClassConstructor);
  6230. JavascriptFunction * function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  6231. return function && (function->GetFunctionInfo()->IsClassConstructor() || (!function->IsScriptFunction() && !function->IsExternalFunction()));
  6232. JIT_HELPER_END(Op_IsClassConstructor);
  6233. }
  6234. BOOL JavascriptOperators::IsClassMethod(Var instance)
  6235. {
  6236. JavascriptFunction * function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  6237. return function && function->GetFunctionInfo()->IsClassMethod();
  6238. }
  6239. BOOL JavascriptOperators::IsBaseConstructorKind(Var instance)
  6240. {
  6241. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_IsBaseConstructorKind);
  6242. JavascriptFunction * function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  6243. return function && (function->GetFunctionInfo()->GetBaseConstructorKind());
  6244. JIT_HELPER_END(Op_IsBaseConstructorKind);
  6245. }
  6246. void JavascriptOperators::OP_InitGetter(Var object, PropertyId propertyId, Var getter)
  6247. {
  6248. AssertMsg(!TaggedNumber::Is(object), "GetMember on a non-object?");
  6249. RecyclableObject* recylableObject = RecyclableObject::FromVar(object);
  6250. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitGetter, reentrancylock, recylableObject->GetScriptContext()->GetThreadContext());
  6251. recylableObject->SetAccessors(propertyId, getter, nullptr);
  6252. JIT_HELPER_END(OP_InitGetter);
  6253. }
  6254. void JavascriptOperators::OP_InitClassMemberGet(Var object, PropertyId propertyId, Var getter)
  6255. {
  6256. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_InitClassMemberGet);
  6257. JIT_HELPER_SAME_ATTRIBUTES(Op_InitClassMemberGet, OP_InitGetter);
  6258. JavascriptOperators::OP_InitGetter(object, propertyId, getter);
  6259. RecyclableObject::FromVar(object)->SetAttributes(propertyId, PropertyClassMemberDefaults);
  6260. JIT_HELPER_END(Op_InitClassMemberGet);
  6261. }
  6262. Js::PropertyId JavascriptOperators::OP_InitElemGetter(Var object, Var elementName, Var getter, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6263. {
  6264. JIT_HELPER_REENTRANT_HEADER(OP_InitElemGetter);
  6265. AssertMsg(!TaggedNumber::Is(object), "GetMember on a non-object?");
  6266. PropertyId propertyId = JavascriptOperators::GetPropertyId(elementName, scriptContext);
  6267. RecyclableObject::FromVar(object)->SetAccessors(propertyId, getter, nullptr);
  6268. return propertyId;
  6269. JIT_HELPER_END(OP_InitElemGetter);
  6270. }
  6271. void JavascriptOperators::OP_InitClassMemberGetComputedName(Var object, Var elementName, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6272. {
  6273. JIT_HELPER_REENTRANT_HEADER(Op_InitClassMemberGetComputedName);
  6274. Js::PropertyId propertyId = JavascriptOperators::OP_InitElemGetter(object, elementName, value, scriptContext);
  6275. RecyclableObject* instance = RecyclableObject::FromVar(object);
  6276. // instance will be a function if it is the class constructor (otherwise it would be an object)
  6277. if (JavascriptFunction::Is(instance) && Js::PropertyIds::prototype == propertyId)
  6278. {
  6279. // It is a TypeError to have a static member with a computed name that evaluates to 'prototype'
  6280. JavascriptError::ThrowTypeError(scriptContext, JSERR_ClassStaticMethodCannotBePrototype);
  6281. }
  6282. instance->SetAttributes(propertyId, PropertyClassMemberDefaults);
  6283. JIT_HELPER_END(Op_InitClassMemberGetComputedName);
  6284. }
  6285. void JavascriptOperators::OP_InitComputedProperty(Var object, Var elementName, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6286. {
  6287. JIT_HELPER_REENTRANT_HEADER(OP_InitComputedProperty);
  6288. PropertyId propertyId = JavascriptOperators::GetPropertyId(elementName, scriptContext);
  6289. RecyclableObject::FromVar(object)->InitProperty(propertyId, value, flags);
  6290. JIT_HELPER_END(OP_InitComputedProperty);
  6291. }
  6292. void JavascriptOperators::OP_InitClassMemberComputedName(Var object, Var elementName, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6293. {
  6294. JIT_HELPER_REENTRANT_HEADER(Op_InitClassMemberComputedName);
  6295. PropertyId propertyId = JavascriptOperators::GetPropertyId(elementName, scriptContext);
  6296. RecyclableObject* instance = RecyclableObject::FromVar(object);
  6297. // instance will be a function if it is the class constructor (otherwise it would be an object)
  6298. if (JavascriptFunction::Is(instance) && Js::PropertyIds::prototype == propertyId)
  6299. {
  6300. // It is a TypeError to have a static member with a computed name that evaluates to 'prototype'
  6301. JavascriptError::ThrowTypeError(scriptContext, JSERR_ClassStaticMethodCannotBePrototype);
  6302. }
  6303. instance->SetPropertyWithAttributes(propertyId, value, PropertyClassMemberDefaults, NULL, flags);
  6304. JIT_HELPER_END(Op_InitClassMemberComputedName);
  6305. }
  6306. //
  6307. // Used by object literal {..., __proto__: ..., }.
  6308. //
  6309. void JavascriptOperators::OP_InitProto(Var instance, PropertyId propertyId, Var value)
  6310. {
  6311. AssertMsg(RecyclableObject::Is(instance), "__proto__ member on a non-object?");
  6312. Assert(propertyId == PropertyIds::__proto__);
  6313. RecyclableObject* object = RecyclableObject::FromVar(instance);
  6314. ScriptContext* scriptContext = object->GetScriptContext();
  6315. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitProto, reentrancylock, scriptContext->GetThreadContext());
  6316. // B.3.1 __proto___ Property Names in Object Initializers
  6317. //6.If propKey is the string value "__proto__" and if isComputedPropertyName(propKey) is false, then
  6318. // a.If Type(v) is either Object or Null, then
  6319. // i.Return the result of calling the [[SetInheritance]] internal method of object with argument propValue.
  6320. // b.Return NormalCompletion(empty).
  6321. if (JavascriptOperators::IsObjectOrNull(value))
  6322. {
  6323. JavascriptObject::ChangePrototype(object, RecyclableObject::FromVar(value), /*validate*/false, scriptContext);
  6324. }
  6325. JIT_HELPER_END(OP_InitProto);
  6326. }
  6327. Var JavascriptOperators::ConvertToUnmappedArguments(HeapArgumentsObject *argumentsObject,
  6328. uint32 paramCount,
  6329. Var *paramAddr,
  6330. DynamicObject* frameObject,
  6331. Js::PropertyIdArray *propIds,
  6332. uint32 formalsCount,
  6333. ScriptContext* scriptContext)
  6334. {
  6335. Var *paramIter = paramAddr;
  6336. uint32 i = 0;
  6337. for (paramIter = paramAddr + i; i < paramCount; i++, paramIter++)
  6338. {
  6339. JavascriptOperators::SetItem(argumentsObject, argumentsObject, i, *paramIter, scriptContext, PropertyOperation_None, /* skipPrototypeCheck = */ TRUE);
  6340. }
  6341. argumentsObject = argumentsObject->ConvertToUnmappedArgumentsObject();
  6342. // Now as the unmapping is done we need to fill those frame object with Undecl
  6343. for (i = 0; i < formalsCount; i++)
  6344. {
  6345. frameObject->SetSlot(SetSlotArguments(propIds != nullptr ? propIds->elements[i] : Js::Constants::NoProperty, i, scriptContext->GetLibrary()->GetUndeclBlockVar()));
  6346. }
  6347. return argumentsObject;
  6348. }
  6349. Var JavascriptOperators::LoadHeapArguments(JavascriptFunction *funcCallee, uint32 actualsCount, Var *paramAddr, Var frameObj, Var vArray, ScriptContext* scriptContext, bool nonSimpleParamList)
  6350. {
  6351. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LoadHeapArguments, reentrancylock, scriptContext->GetThreadContext());
  6352. AssertMsg(actualsCount != (unsigned int)-1, "Loading the arguments object in the global function?");
  6353. // Create and initialize the Arguments object.
  6354. uint32 formalsCount = 0;
  6355. Js::PropertyIdArray *propIds = nullptr;
  6356. if (vArray != scriptContext->GetLibrary()->GetNull())
  6357. {
  6358. propIds = (Js::PropertyIdArray *)vArray;
  6359. formalsCount = propIds->count;
  6360. Assert(formalsCount != 0 && propIds != nullptr);
  6361. }
  6362. HeapArgumentsObject *argsObj = JavascriptOperators::CreateHeapArguments(funcCallee, actualsCount, formalsCount, frameObj, scriptContext);
  6363. return FillScopeObject(funcCallee, actualsCount, formalsCount, frameObj, paramAddr, propIds, argsObj, scriptContext, nonSimpleParamList, false);
  6364. JIT_HELPER_END(Op_LoadHeapArguments);
  6365. }
  6366. Var JavascriptOperators::LoadHeapArgsCached(JavascriptFunction *funcCallee, uint32 actualsCount, uint32 formalsCount, Var *paramAddr, Var frameObj, ScriptContext* scriptContext, bool nonSimpleParamList)
  6367. {
  6368. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LoadHeapArgsCached, reentrancylock, scriptContext->GetThreadContext());
  6369. // Disregard the "this" param.
  6370. AssertMsg(actualsCount != (uint32)-1 && formalsCount != (uint32)-1,
  6371. "Loading the arguments object in the global function?");
  6372. HeapArgumentsObject *argsObj = JavascriptOperators::CreateHeapArguments(funcCallee, actualsCount, formalsCount, frameObj, scriptContext);
  6373. return FillScopeObject(funcCallee, actualsCount, formalsCount, frameObj, paramAddr, nullptr, argsObj, scriptContext, nonSimpleParamList, true);
  6374. JIT_HELPER_END(Op_LoadHeapArgsCached);
  6375. }
  6376. Var JavascriptOperators::FillScopeObject(JavascriptFunction *funcCallee, uint32 actualsCount, uint32 formalsCount, Var frameObj, Var * paramAddr,
  6377. Js::PropertyIdArray *propIds, HeapArgumentsObject * argsObj, ScriptContext * scriptContext, bool nonSimpleParamList, bool useCachedScope)
  6378. {
  6379. Assert(formalsCount == 0 || frameObj != nullptr);
  6380. // Transfer formal arguments (that were actually passed) from their ArgIn slots to the local frame object.
  6381. uint32 i;
  6382. Var *tmpAddr = paramAddr;
  6383. if (formalsCount != 0)
  6384. {
  6385. DynamicObject* frameObject = nullptr;
  6386. if (useCachedScope)
  6387. {
  6388. frameObject = DynamicObject::FromVar(frameObj);
  6389. __analysis_assume((uint32)frameObject->GetDynamicType()->GetTypeHandler()->GetSlotCapacity() >= formalsCount);
  6390. }
  6391. else
  6392. {
  6393. frameObject = (DynamicObject*)frameObj;
  6394. // No fixed fields for formal parameters of the arguments object. Also, mark all fields as initialized up-front, because
  6395. // we will set them directly using SetSlot below, so the type handler will not have a chance to mark them as initialized later.
  6396. // CONSIDER : When we delay type sharing until the second instance is created, pass an argument indicating we want the types
  6397. // 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
  6398. // is ready for storing values directly to slots.
  6399. DynamicType* newType = nullptr;
  6400. if (nonSimpleParamList)
  6401. {
  6402. bool skipLetAttrForArguments = ((JavascriptGeneratorFunction::Is(funcCallee) || JavascriptAsyncFunction::Is(funcCallee)) ?
  6403. JavascriptGeneratorFunction::FromVar(funcCallee)->GetGeneratorVirtualScriptFunction()->GetFunctionBody()->HasReferenceableBuiltInArguments()
  6404. : funcCallee->GetFunctionBody()->HasReferenceableBuiltInArguments());
  6405. if (skipLetAttrForArguments)
  6406. {
  6407. newType = PathTypeHandlerBase::CreateNewScopeObject<true>(scriptContext, frameObject->GetDynamicType(), propIds, PropertyLetDefaults);
  6408. }
  6409. else
  6410. {
  6411. newType = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, frameObject->GetDynamicType(), propIds, PropertyLetDefaults);
  6412. }
  6413. }
  6414. else
  6415. {
  6416. newType = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, frameObject->GetDynamicType(), propIds);
  6417. }
  6418. int oldSlotCapacity = frameObject->GetDynamicType()->GetTypeHandler()->GetSlotCapacity();
  6419. int newSlotCapacity = newType->GetTypeHandler()->GetSlotCapacity();
  6420. __analysis_assume((uint32)newSlotCapacity >= formalsCount);
  6421. frameObject->EnsureSlots(oldSlotCapacity, newSlotCapacity, scriptContext, newType->GetTypeHandler());
  6422. frameObject->ReplaceType(newType);
  6423. }
  6424. if (argsObj && nonSimpleParamList)
  6425. {
  6426. return ConvertToUnmappedArguments(argsObj, actualsCount, paramAddr, frameObject, propIds, formalsCount, scriptContext);
  6427. }
  6428. for (i = 0; i < formalsCount && i < actualsCount; i++, tmpAddr++)
  6429. {
  6430. frameObject->SetSlot(SetSlotArguments(propIds != nullptr? propIds->elements[i] : Constants::NoProperty, i, *tmpAddr));
  6431. }
  6432. if (i < formalsCount)
  6433. {
  6434. // The formals that weren't passed still need to be put in the frame object so that
  6435. // their names will be found. Initialize them to "undefined".
  6436. for (; i < formalsCount; i++)
  6437. {
  6438. frameObject->SetSlot(SetSlotArguments(propIds != nullptr? propIds->elements[i] : Constants::NoProperty, i, scriptContext->GetLibrary()->GetUndefined()));
  6439. }
  6440. }
  6441. }
  6442. if (argsObj != nullptr)
  6443. {
  6444. // Transfer the unnamed actual arguments, if any, to the Arguments object itself.
  6445. for (i = formalsCount, tmpAddr = paramAddr + i; i < actualsCount; i++, tmpAddr++)
  6446. {
  6447. // ES5 10.6.11: use [[DefineOwnProperty]] semantics (instead of [[Put]]):
  6448. // do not check whether property is non-writable/etc in the prototype.
  6449. // ES3 semantics is same.
  6450. JavascriptOperators::SetItem(argsObj, argsObj, i, *tmpAddr, scriptContext, PropertyOperation_None, /* skipPrototypeCheck = */ TRUE);
  6451. }
  6452. if (funcCallee->IsStrictMode())
  6453. {
  6454. // If the formals are let decls, then we just overwrote the frame object slots with
  6455. // Undecl sentinels, and we can use the original arguments that were passed to the HeapArgumentsObject.
  6456. return argsObj->ConvertToUnmappedArgumentsObject(!nonSimpleParamList);
  6457. }
  6458. }
  6459. return argsObj;
  6460. }
  6461. HeapArgumentsObject *JavascriptOperators::CreateHeapArguments(JavascriptFunction *funcCallee, uint32 actualsCount, uint32 formalsCount, Var frameObj, ScriptContext* scriptContext)
  6462. {
  6463. JavascriptLibrary *library = scriptContext->GetLibrary();
  6464. HeapArgumentsObject *argsObj = library->CreateHeapArguments(frameObj, formalsCount, !!funcCallee->IsStrictMode());
  6465. #if DBG
  6466. DynamicTypeHandler* typeHandler = argsObj->GetTypeHandler();
  6467. #endif
  6468. //
  6469. // Set the number of arguments of Arguments Object
  6470. //
  6471. argsObj->SetNumberOfArguments(actualsCount);
  6472. JavascriptOperators::SetProperty(argsObj, argsObj, PropertyIds::length, JavascriptNumber::ToVar(actualsCount, scriptContext), scriptContext);
  6473. JavascriptOperators::SetProperty(argsObj, argsObj, PropertyIds::_symbolIterator, library->EnsureArrayPrototypeValuesFunction(), scriptContext);
  6474. if (funcCallee->IsStrictMode())
  6475. {
  6476. JavascriptFunction* restrictedPropertyAccessor = library->GetThrowTypeErrorRestrictedPropertyAccessorFunction();
  6477. argsObj->SetAccessors(PropertyIds::callee, restrictedPropertyAccessor, restrictedPropertyAccessor, PropertyOperation_NonFixedValue);
  6478. }
  6479. else
  6480. {
  6481. JavascriptOperators::SetProperty(argsObj, argsObj, PropertyIds::callee,
  6482. StackScriptFunction::EnsureBoxed(BOX_PARAM(funcCallee, nullptr, _u("callee"))), scriptContext);
  6483. }
  6484. AssertMsg(argsObj->GetTypeHandler() == typeHandler || scriptContext->IsScriptContextInDebugMode(), "type handler should not transition because we initialized it correctly");
  6485. return argsObj;
  6486. }
  6487. Var JavascriptOperators::OP_NewScopeObject(ScriptContext* scriptContext)
  6488. {
  6489. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_NewScopeObject);
  6490. return scriptContext->GetLibrary()->CreateActivationObject();
  6491. JIT_HELPER_END(OP_NewScopeObject);
  6492. }
  6493. Var JavascriptOperators::OP_NewScopeObjectWithFormals(ScriptContext* scriptContext, FunctionBody * calleeBody, bool nonSimpleParamList)
  6494. {
  6495. JIT_HELPER_NOT_REENTRANT_HEADER(OP_NewScopeObjectWithFormals, reentrancylock, scriptContext->GetThreadContext());
  6496. Js::ActivationObject * frameObject = (ActivationObject*)OP_NewScopeObject(scriptContext);
  6497. // No fixed fields for formal parameters of the arguments object. Also, mark all fields as initialized up-front, because
  6498. // we will set them directly using SetSlot below, so the type handler will not have a chance to mark them as initialized later.
  6499. // CONSIDER : When we delay type sharing until the second instance is created, pass an argument indicating we want the types
  6500. // 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
  6501. // is ready for storing values directly to slots.
  6502. DynamicType* newType = nullptr;
  6503. if (nonSimpleParamList)
  6504. {
  6505. if (calleeBody->HasReferenceableBuiltInArguments())
  6506. {
  6507. newType = PathTypeHandlerBase::CreateNewScopeObject<true>(scriptContext, frameObject->GetDynamicType(), calleeBody->GetFormalsPropIdArray(), PropertyLetDefaults);
  6508. }
  6509. else
  6510. {
  6511. newType = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, frameObject->GetDynamicType(), calleeBody->GetFormalsPropIdArray(), PropertyLetDefaults);
  6512. }
  6513. }
  6514. else
  6515. {
  6516. newType = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, frameObject->GetDynamicType(), calleeBody->GetFormalsPropIdArray());
  6517. }
  6518. int oldSlotCapacity = frameObject->GetDynamicType()->GetTypeHandler()->GetSlotCapacity();
  6519. int newSlotCapacity = newType->GetTypeHandler()->GetSlotCapacity();
  6520. frameObject->EnsureSlots(oldSlotCapacity, newSlotCapacity, scriptContext, newType->GetTypeHandler());
  6521. frameObject->ReplaceType(newType);
  6522. return frameObject;
  6523. JIT_HELPER_END(OP_NewScopeObjectWithFormals);
  6524. }
  6525. Field(Var)* JavascriptOperators::OP_NewScopeSlots(unsigned int size, ScriptContext *scriptContext, Var scope)
  6526. {
  6527. JIT_HELPER_NOT_REENTRANT_HEADER(OP_NewScopeSlots, reentrancylock, scriptContext->GetThreadContext());
  6528. Assert(size > ScopeSlots::FirstSlotIndex); // Should never see empty slot array
  6529. Field(Var)* slotArray = RecyclerNewArray(scriptContext->GetRecycler(), Field(Var), size); // last initialized slot contains reference to array of propertyIds, correspondent to objects in previous slots
  6530. uint count = size - ScopeSlots::FirstSlotIndex;
  6531. ScopeSlots slots(slotArray);
  6532. slots.SetCount(count);
  6533. AssertMsg(!FunctionBody::Is(scope), "Scope should only be FunctionInfo or DebuggerScope, not FunctionBody");
  6534. slots.SetScopeMetadata(scope);
  6535. Var undef = scriptContext->GetLibrary()->GetUndefined();
  6536. for (unsigned int i = 0; i < count; i++)
  6537. {
  6538. slots.Set(i, undef);
  6539. }
  6540. return slotArray;
  6541. JIT_HELPER_END(OP_NewScopeSlots);
  6542. }
  6543. Field(Var)* JavascriptOperators::OP_NewScopeSlotsWithoutPropIds(unsigned int count, int scopeIndex, ScriptContext *scriptContext, FunctionBody *functionBody)
  6544. {
  6545. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_NewScopeSlotsWithoutPropIds);
  6546. DebuggerScope* scope = reinterpret_cast<DebuggerScope*>(Constants::FunctionBodyUnavailable);
  6547. if (scopeIndex != DebuggerScope::InvalidScopeIndex)
  6548. {
  6549. AssertMsg(functionBody->GetScopeObjectChain(), "A scope chain should always be created when there are new scope slots for blocks.");
  6550. scope = functionBody->GetScopeObjectChain()->pScopeChain->Item(scopeIndex);
  6551. }
  6552. return OP_NewScopeSlots(count, scriptContext, scope);
  6553. JIT_HELPER_END(OP_NewScopeSlotsWithoutPropIds);
  6554. }
  6555. Field(Var)* JavascriptOperators::OP_CloneScopeSlots(Field(Var) *slotArray, ScriptContext *scriptContext)
  6556. {
  6557. JIT_HELPER_NOT_REENTRANT_HEADER(OP_CloneInnerScopeSlots, reentrancylock, scriptContext->GetThreadContext());
  6558. ScopeSlots slots(slotArray);
  6559. uint size = ScopeSlots::FirstSlotIndex + static_cast<uint>(slots.GetCount());
  6560. Field(Var)* slotArrayClone = RecyclerNewArray(scriptContext->GetRecycler(), Field(Var), size);
  6561. CopyArray(slotArrayClone, size, slotArray, size);
  6562. return slotArrayClone;
  6563. JIT_HELPER_END(OP_CloneInnerScopeSlots);
  6564. }
  6565. Var JavascriptOperators::OP_NewPseudoScope(ScriptContext *scriptContext)
  6566. {
  6567. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_NewPseudoScope);
  6568. return scriptContext->GetLibrary()->CreatePseudoActivationObject();
  6569. JIT_HELPER_END(OP_NewPseudoScope);
  6570. }
  6571. Var JavascriptOperators::OP_NewBlockScope(ScriptContext *scriptContext)
  6572. {
  6573. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_NewBlockScope);
  6574. return scriptContext->GetLibrary()->CreateBlockActivationObject();
  6575. JIT_HELPER_END(OP_NewBlockScope);
  6576. }
  6577. Var JavascriptOperators::OP_CloneBlockScope(BlockActivationObject *blockScope, ScriptContext *scriptContext)
  6578. {
  6579. JIT_HELPER_NOT_REENTRANT_HEADER(OP_CloneBlockScope, reentrancylock, scriptContext->GetThreadContext());
  6580. return blockScope->Clone(scriptContext);
  6581. JIT_HELPER_END(OP_CloneBlockScope);
  6582. }
  6583. Var JavascriptOperators::OP_IsInst(Var instance, Var aClass, ScriptContext* scriptContext, IsInstInlineCache* inlineCache)
  6584. {
  6585. JIT_HELPER_REENTRANT_HEADER(ScrObj_OP_IsInst);
  6586. if (!RecyclableObject::Is(aClass))
  6587. {
  6588. JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedFunction, _u("instanceof"));
  6589. }
  6590. RecyclableObject* constructor = RecyclableObject::FromVar(aClass);
  6591. if (scriptContext->GetConfig()->IsES6HasInstanceEnabled())
  6592. {
  6593. Var instOfHandler = JavascriptOperators::GetPropertyNoCache(constructor,
  6594. PropertyIds::_symbolHasInstance, scriptContext);
  6595. if (JavascriptOperators::IsUndefinedObject(instOfHandler)
  6596. || instOfHandler == scriptContext->GetBuiltInLibraryFunction(JavascriptFunction::EntryInfo::SymbolHasInstance.GetOriginalEntryPoint()))
  6597. {
  6598. return JavascriptBoolean::ToVar(constructor->HasInstance(instance, scriptContext, inlineCache), scriptContext);
  6599. }
  6600. else
  6601. {
  6602. if (!JavascriptConversion::IsCallable(instOfHandler))
  6603. {
  6604. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, _u("Symbol[Symbol.hasInstance]"));
  6605. }
  6606. ThreadContext * threadContext = scriptContext->GetThreadContext();
  6607. RecyclableObject *instFunc = RecyclableObject::FromVar(instOfHandler);
  6608. Var result = threadContext->ExecuteImplicitCall(instFunc, ImplicitCall_Accessor, [=]()->Js::Var
  6609. {
  6610. return CALL_FUNCTION(scriptContext->GetThreadContext(), instFunc, CallInfo(CallFlags_Value, 2), constructor, instance);
  6611. });
  6612. return JavascriptBoolean::ToVar(JavascriptConversion::ToBoolean(result, scriptContext) ? TRUE : FALSE, scriptContext);
  6613. }
  6614. }
  6615. else
  6616. {
  6617. return JavascriptBoolean::ToVar(constructor->HasInstance(instance, scriptContext, inlineCache), scriptContext);
  6618. }
  6619. JIT_HELPER_END(ScrObj_OP_IsInst);
  6620. }
  6621. void JavascriptOperators::OP_InitClass(Var constructor, Var extends, ScriptContext * scriptContext)
  6622. {
  6623. JIT_HELPER_REENTRANT_HEADER(OP_InitClass);
  6624. if (JavascriptOperators::GetTypeId(constructor) != Js::TypeId::TypeIds_Function)
  6625. {
  6626. JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedFunction, _u("class"));
  6627. }
  6628. RecyclableObject * ctor = RecyclableObject::FromVar(constructor);
  6629. if (extends)
  6630. {
  6631. switch (JavascriptOperators::GetTypeId(extends))
  6632. {
  6633. case Js::TypeId::TypeIds_Null:
  6634. {
  6635. Var ctorProto = JavascriptOperators::GetProperty(constructor, ctor, Js::PropertyIds::prototype, scriptContext);
  6636. RecyclableObject * ctorProtoObj = RecyclableObject::FromVar(ctorProto);
  6637. ctorProtoObj->SetPrototype(RecyclableObject::FromVar(extends));
  6638. ctorProtoObj->EnsureProperty(Js::PropertyIds::constructor);
  6639. ctorProtoObj->SetEnumerable(Js::PropertyIds::constructor, FALSE);
  6640. break;
  6641. }
  6642. default:
  6643. {
  6644. if (!RecyclableObject::Is(extends))
  6645. {
  6646. JavascriptError::ThrowTypeError(scriptContext, JSERR_ErrorOnNew);
  6647. }
  6648. RecyclableObject * extendsObj = RecyclableObject::FromVar(extends);
  6649. if (!JavascriptOperators::IsConstructor(extendsObj))
  6650. {
  6651. JavascriptError::ThrowTypeError(scriptContext, JSERR_ErrorOnNew);
  6652. }
  6653. if (!extendsObj->HasProperty(Js::PropertyIds::prototype))
  6654. {
  6655. JavascriptError::ThrowTypeError(scriptContext, JSERR_InvalidPrototype);
  6656. }
  6657. Var extendsProto = JavascriptOperators::GetPropertyNoCache(extends, extendsObj, Js::PropertyIds::prototype, scriptContext);
  6658. uint extendsProtoTypeId = JavascriptOperators::GetTypeId(extendsProto);
  6659. if (extendsProtoTypeId <= Js::TypeId::TypeIds_LastJavascriptPrimitiveType && extendsProtoTypeId != Js::TypeId::TypeIds_Null)
  6660. {
  6661. JavascriptError::ThrowTypeError(scriptContext, JSERR_InvalidPrototype);
  6662. }
  6663. Var ctorProto = JavascriptOperators::GetPropertyNoCache(constructor, ctor, Js::PropertyIds::prototype, scriptContext);
  6664. RecyclableObject * ctorProtoObj = RecyclableObject::FromVar(ctorProto);
  6665. ctorProtoObj->SetPrototype(RecyclableObject::FromVar(extendsProto));
  6666. ctorProtoObj->EnsureProperty(Js::PropertyIds::constructor);
  6667. ctorProtoObj->SetEnumerable(Js::PropertyIds::constructor, FALSE);
  6668. Var protoCtor = JavascriptOperators::GetPropertyNoCache(ctorProto, ctorProtoObj, Js::PropertyIds::constructor, scriptContext);
  6669. RecyclableObject * protoCtorObj = RecyclableObject::FromVar(protoCtor);
  6670. protoCtorObj->SetPrototype(extendsObj);
  6671. break;
  6672. }
  6673. }
  6674. }
  6675. Var proto = JavascriptOperators::GetProperty(constructor, ctor, Js::PropertyIds::prototype, scriptContext);
  6676. JavascriptOperators::OP_SetHomeObj(constructor, proto);
  6677. JIT_HELPER_END(OP_InitClass);
  6678. }
  6679. void JavascriptOperators::OP_LoadUndefinedToElement(Var instance, PropertyId propertyId)
  6680. {
  6681. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LdElemUndef, reentrancylock, RecyclableObject::FromVar(instance)->GetScriptContext()->GetThreadContext());
  6682. AssertMsg(!TaggedNumber::Is(instance), "Invalid scope/root object");
  6683. JavascriptOperators::EnsureProperty(instance, propertyId);
  6684. JIT_HELPER_END(Op_LdElemUndef);
  6685. }
  6686. void JavascriptOperators::OP_LoadUndefinedToElementScoped(FrameDisplay *pScope, PropertyId propertyId, Var defaultInstance, ScriptContext* scriptContext)
  6687. {
  6688. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LdElemUndefScoped, reentrancylock, scriptContext->GetThreadContext());
  6689. int i;
  6690. int length = pScope->GetLength();
  6691. Var argInstance;
  6692. for (i = 0; i < length; i++)
  6693. {
  6694. argInstance = pScope->GetItem(i);
  6695. if (JavascriptOperators::EnsureProperty(argInstance, propertyId))
  6696. {
  6697. return;
  6698. }
  6699. }
  6700. if (!JavascriptOperators::HasOwnPropertyNoHostObject(defaultInstance, propertyId))
  6701. {
  6702. // CONSIDER : Consider adding pre-initialization support to activation objects.
  6703. JavascriptOperators::OP_InitPropertyScoped(pScope, propertyId, scriptContext->GetLibrary()->GetUndefined(), defaultInstance, scriptContext);
  6704. }
  6705. JIT_HELPER_END(Op_LdElemUndefScoped);
  6706. }
  6707. void JavascriptOperators::OP_LoadUndefinedToElementDynamic(Var instance, PropertyId propertyId, ScriptContext *scriptContext)
  6708. {
  6709. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LdElemUndefDynamic, reentrancylock, scriptContext->GetThreadContext());
  6710. if (!JavascriptOperators::HasOwnPropertyNoHostObject(instance, propertyId))
  6711. {
  6712. RecyclableObject::FromVar(instance)->InitPropertyScoped(propertyId, scriptContext->GetLibrary()->GetUndefined());
  6713. }
  6714. JIT_HELPER_END(Op_LdElemUndefDynamic);
  6715. }
  6716. BOOL JavascriptOperators::EnsureProperty(Var instance, PropertyId propertyId)
  6717. {
  6718. RecyclableObject *obj = RecyclableObject::FromVar(instance);
  6719. return (obj && obj->EnsureProperty(propertyId));
  6720. }
  6721. void JavascriptOperators::OP_EnsureNoRootProperty(Var instance, PropertyId propertyId)
  6722. {
  6723. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_EnsureNoRootProperty);
  6724. Assert(RootObjectBase::Is(instance));
  6725. RootObjectBase *obj = RootObjectBase::FromVar(instance);
  6726. obj->EnsureNoProperty(propertyId);
  6727. JIT_HELPER_END(Op_EnsureNoRootProperty);
  6728. }
  6729. void JavascriptOperators::OP_EnsureNoRootRedeclProperty(Var instance, PropertyId propertyId)
  6730. {
  6731. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_EnsureNoRootRedeclProperty);
  6732. Assert(RootObjectBase::Is(instance));
  6733. RecyclableObject *obj = RecyclableObject::FromVar(instance);
  6734. obj->EnsureNoRedeclProperty(propertyId);
  6735. JIT_HELPER_END(Op_EnsureNoRootRedeclProperty);
  6736. }
  6737. void JavascriptOperators::OP_ScopedEnsureNoRedeclProperty(FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance)
  6738. {
  6739. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_EnsureNoRedeclPropertyScoped);
  6740. int i;
  6741. int length = pDisplay->GetLength();
  6742. RecyclableObject *object;
  6743. for (i = 0; i < length; i++)
  6744. {
  6745. object = RecyclableObject::FromVar(pDisplay->GetItem(i));
  6746. if (object->EnsureNoRedeclProperty(propertyId))
  6747. {
  6748. return;
  6749. }
  6750. }
  6751. object = RecyclableObject::FromVar(defaultInstance);
  6752. object->EnsureNoRedeclProperty(propertyId);
  6753. JIT_HELPER_END(Op_EnsureNoRedeclPropertyScoped);
  6754. }
  6755. Var JavascriptOperators::IsIn(Var argProperty, Var instance, ScriptContext* scriptContext)
  6756. {
  6757. JIT_HELPER_REENTRANT_HEADER(Op_IsIn);
  6758. // Note that the fact that we haven't seen a given name before doesn't mean that the instance doesn't
  6759. if (!IsObject(instance))
  6760. {
  6761. JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedObject, _u("in"));
  6762. }
  6763. RecyclableObject* object = RecyclableObject::FromVar(instance);
  6764. BOOL result;
  6765. PropertyRecord const * propertyRecord = nullptr;
  6766. uint32 index;
  6767. IndexType indexType;
  6768. // Fast path for JavascriptSymbols and PropertyStrings
  6769. RecyclableObject* cacheOwner;
  6770. PropertyRecordUsageCache* propertyRecordUsageCache;
  6771. if (GetPropertyRecordUsageCache(argProperty, scriptContext, &propertyRecordUsageCache, &cacheOwner))
  6772. {
  6773. Var value;
  6774. propertyRecord = propertyRecordUsageCache->GetPropertyRecord();
  6775. if (!propertyRecord->IsNumeric())
  6776. {
  6777. PropertyValueInfo info;
  6778. if (propertyRecordUsageCache->TryGetPropertyFromCache<false /* OwnPropertyOnly */, true /* OutputExistence */, false /* ReturnOperationInfo */>(instance, object, &value, scriptContext, &info, cacheOwner, nullptr))
  6779. {
  6780. Assert(JavascriptBoolean::Is(value));
  6781. return value;
  6782. }
  6783. result = JavascriptOperators::GetPropertyWPCache<true /* OutputExistence */>(instance, object, propertyRecordUsageCache->GetPropertyRecord()->GetPropertyId(), &value, scriptContext, &info);
  6784. Assert(value == JavascriptBoolean::ToVar(result, scriptContext));
  6785. return value;
  6786. }
  6787. // We don't cache numeric property lookups, so fall through to the IndexType_Number case
  6788. index = propertyRecord->GetNumericValue();
  6789. indexType = IndexType_Number;
  6790. }
  6791. else
  6792. {
  6793. indexType = GetIndexType(argProperty, scriptContext, &index, &propertyRecord, true);
  6794. }
  6795. if (indexType == IndexType_Number)
  6796. {
  6797. result = JavascriptOperators::HasItem(object, index);
  6798. }
  6799. else
  6800. {
  6801. result = JavascriptOperators::HasProperty(object, propertyRecord->GetPropertyId());
  6802. #ifdef TELEMETRY_JSO
  6803. {
  6804. Assert(indexType != Js::IndexType_JavascriptString);
  6805. if (indexType == Js::IndexType_PropertyId)
  6806. {
  6807. scriptContext->GetTelemetry().GetOpcodeTelemetry().IsIn(instance, propertyId, result != 0);
  6808. }
  6809. }
  6810. #endif
  6811. }
  6812. return JavascriptBoolean::ToVar(result, scriptContext);
  6813. JIT_HELPER_END(Op_IsIn);
  6814. }
  6815. template <bool IsFromFullJit, class TInlineCache>
  6816. inline Var JavascriptOperators::PatchGetValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  6817. {
  6818. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetValue);
  6819. return PatchGetValueWithThisPtr<IsFromFullJit, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, instance);
  6820. JIT_HELPER_END(Op_PatchGetValue);
  6821. }
  6822. JIT_HELPER_TEMPLATE(Op_PatchGetValue, Op_PatchGetValuePolymorphic)
  6823. template <bool IsFromFullJit, class TInlineCache>
  6824. __forceinline Var JavascriptOperators::PatchGetValueWithThisPtr(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance)
  6825. {
  6826. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetValueWithThisPtr);
  6827. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  6828. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  6829. RecyclableObject* object = nullptr;
  6830. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  6831. {
  6832. if (scriptContext->GetThreadContext()->RecordImplicitException())
  6833. {
  6834. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  6835. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  6836. }
  6837. else
  6838. {
  6839. return scriptContext->GetLibrary()->GetUndefined();
  6840. }
  6841. }
  6842. PropertyValueInfo info;
  6843. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  6844. Var value;
  6845. if (CacheOperators::TryGetProperty<true, true, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  6846. instance, false, object, propertyId, &value, scriptContext, nullptr, &info))
  6847. {
  6848. return value;
  6849. }
  6850. #if DBG_DUMP
  6851. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  6852. {
  6853. CacheOperators::TraceCache(inlineCache, _u("PatchGetValue"), propertyId, scriptContext, object);
  6854. }
  6855. #endif
  6856. return JavascriptOperators::GetProperty(thisInstance, object, propertyId, scriptContext, &info);
  6857. JIT_HELPER_END(Op_PatchGetValueWithThisPtr);
  6858. }
  6859. JIT_HELPER_TEMPLATE(Op_PatchGetValueWithThisPtr, Op_PatchGetValuePolymorphicWithThisPtr)
  6860. template Var JavascriptOperators::PatchGetValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  6861. template Var JavascriptOperators::PatchGetValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  6862. template Var JavascriptOperators::PatchGetValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  6863. template Var JavascriptOperators::PatchGetValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  6864. template Var JavascriptOperators::PatchGetValueWithThisPtr<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance);
  6865. template Var JavascriptOperators::PatchGetValueWithThisPtr<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance);
  6866. template Var JavascriptOperators::PatchGetValueWithThisPtr<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance);
  6867. template Var JavascriptOperators::PatchGetValueWithThisPtr<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance);
  6868. template <bool IsFromFullJit, class TInlineCache>
  6869. Var JavascriptOperators::PatchGetValueForTypeOf(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  6870. {
  6871. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetValueForTypeOf);
  6872. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  6873. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  6874. RecyclableObject* object = nullptr;
  6875. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  6876. {
  6877. if (scriptContext->GetThreadContext()->RecordImplicitException())
  6878. {
  6879. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  6880. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  6881. }
  6882. else
  6883. {
  6884. return scriptContext->GetLibrary()->GetUndefined();
  6885. }
  6886. }
  6887. PropertyValueInfo info;
  6888. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  6889. Var value;
  6890. if (CacheOperators::TryGetProperty<true, true, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  6891. instance, false, object, propertyId, &value, scriptContext, nullptr, &info))
  6892. {
  6893. return value;
  6894. }
  6895. #if DBG_DUMP
  6896. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  6897. {
  6898. CacheOperators::TraceCache(inlineCache, _u("PatchGetValueForTypeOf"), propertyId, scriptContext, object);
  6899. }
  6900. #endif
  6901. Var prop = nullptr;
  6902. BEGIN_TYPEOF_ERROR_HANDLER(scriptContext);
  6903. prop = JavascriptOperators::GetProperty(instance, object, propertyId, scriptContext, &info);
  6904. END_TYPEOF_ERROR_HANDLER(scriptContext, prop);
  6905. return prop;
  6906. JIT_HELPER_END(Op_PatchGetValueForTypeOf);
  6907. }
  6908. JIT_HELPER_TEMPLATE(Op_PatchGetValueForTypeOf, Op_PatchGetValuePolymorphicForTypeOf)
  6909. template Var JavascriptOperators::PatchGetValueForTypeOf<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  6910. template Var JavascriptOperators::PatchGetValueForTypeOf<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  6911. template Var JavascriptOperators::PatchGetValueForTypeOf<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  6912. template Var JavascriptOperators::PatchGetValueForTypeOf<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  6913. Var JavascriptOperators::PatchGetValueUsingSpecifiedInlineCache(InlineCache * inlineCache, Var instance, RecyclableObject * object, PropertyId propertyId, ScriptContext* scriptContext)
  6914. {
  6915. PropertyValueInfo info;
  6916. PropertyValueInfo::SetCacheInfo(&info, inlineCache);
  6917. Var value;
  6918. if (CacheOperators::TryGetProperty<true, true, true, true, false, true, !InlineCache::IsPolymorphic, InlineCache::IsPolymorphic, false, false>(
  6919. instance, false, object, propertyId, &value, scriptContext, nullptr, &info))
  6920. {
  6921. return value;
  6922. }
  6923. #if DBG_DUMP
  6924. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  6925. {
  6926. CacheOperators::TraceCache(inlineCache, _u("PatchGetValue"), propertyId, scriptContext, object);
  6927. }
  6928. #endif
  6929. return JavascriptOperators::GetProperty(instance, object, propertyId, scriptContext, &info);
  6930. }
  6931. Var JavascriptOperators::PatchGetValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  6932. {
  6933. return PatchGetValueWithThisPtrNoFastPath(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, instance);
  6934. }
  6935. Var JavascriptOperators::PatchGetValueWithThisPtrNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance)
  6936. {
  6937. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  6938. RecyclableObject* object = nullptr;
  6939. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  6940. {
  6941. if (scriptContext->GetThreadContext()->RecordImplicitException())
  6942. {
  6943. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  6944. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  6945. }
  6946. else
  6947. {
  6948. return scriptContext->GetLibrary()->GetUndefined();
  6949. }
  6950. }
  6951. PropertyValueInfo info;
  6952. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  6953. return JavascriptOperators::GetProperty(thisInstance, object, propertyId, scriptContext, &info);
  6954. }
  6955. template <bool IsFromFullJit, class TInlineCache>
  6956. inline Var JavascriptOperators::PatchGetRootValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId)
  6957. {
  6958. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetRootValue);
  6959. AssertMsg(RootObjectBase::Is(object), "Root must be a global object!");
  6960. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  6961. PropertyValueInfo info;
  6962. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  6963. Var value;
  6964. if (CacheOperators::TryGetProperty<true, true, true, false, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  6965. object, true, object, propertyId, &value, scriptContext, nullptr, &info))
  6966. {
  6967. return value;
  6968. }
  6969. #if DBG_DUMP
  6970. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  6971. {
  6972. CacheOperators::TraceCache(inlineCache, _u("PatchGetRootValue"), propertyId, scriptContext, object);
  6973. }
  6974. #endif
  6975. return JavascriptOperators::OP_GetRootProperty(object, propertyId, &info, scriptContext);
  6976. JIT_HELPER_END(Op_PatchGetRootValue);
  6977. }
  6978. JIT_HELPER_TEMPLATE(Op_PatchGetRootValue, Op_PatchGetRootValuePolymorphic)
  6979. template Var JavascriptOperators::PatchGetRootValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  6980. template Var JavascriptOperators::PatchGetRootValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  6981. template Var JavascriptOperators::PatchGetRootValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  6982. template Var JavascriptOperators::PatchGetRootValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  6983. template <bool IsFromFullJit, class TInlineCache>
  6984. Var JavascriptOperators::PatchGetRootValueForTypeOf(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId)
  6985. {
  6986. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetRootValueForTypeOf);
  6987. AssertMsg(RootObjectBase::Is(object), "Root must be a global object!");
  6988. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  6989. PropertyValueInfo info;
  6990. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  6991. Var value = nullptr;
  6992. if (CacheOperators::TryGetProperty<true, true, true, false, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  6993. object, true, object, propertyId, &value, scriptContext, nullptr, &info))
  6994. {
  6995. return value;
  6996. }
  6997. #if DBG_DUMP
  6998. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  6999. {
  7000. CacheOperators::TraceCache(inlineCache, _u("PatchGetRootValueForTypeOf"), propertyId, scriptContext, object);
  7001. }
  7002. #endif
  7003. value = nullptr;
  7004. BEGIN_TYPEOF_ERROR_HANDLER(scriptContext);
  7005. if (JavascriptOperators::GetRootProperty(RecyclableObject::FromVar(object), propertyId, &value, scriptContext, &info))
  7006. {
  7007. if (scriptContext->IsUndeclBlockVar(value))
  7008. {
  7009. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  7010. }
  7011. return value;
  7012. }
  7013. END_TYPEOF_ERROR_HANDLER(scriptContext, value);
  7014. value = scriptContext->GetLibrary()->GetUndefined();
  7015. return value;
  7016. JIT_HELPER_END(Op_PatchGetRootValueForTypeOf);
  7017. }
  7018. JIT_HELPER_TEMPLATE(Op_PatchGetRootValueForTypeOf, Op_PatchGetRootValuePolymorphicForTypeOf)
  7019. template Var JavascriptOperators::PatchGetRootValueForTypeOf<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7020. template Var JavascriptOperators::PatchGetRootValueForTypeOf<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7021. template Var JavascriptOperators::PatchGetRootValueForTypeOf<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7022. template Var JavascriptOperators::PatchGetRootValueForTypeOf<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7023. Var JavascriptOperators::PatchGetRootValueNoFastPath_Var(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7024. {
  7025. return
  7026. PatchGetRootValueNoFastPath(
  7027. functionBody,
  7028. inlineCache,
  7029. inlineCacheIndex,
  7030. DynamicObject::FromVar(instance),
  7031. propertyId);
  7032. }
  7033. Var JavascriptOperators::PatchGetRootValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId)
  7034. {
  7035. AssertMsg(RootObjectBase::Is(object), "Root must be a global object!");
  7036. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7037. PropertyValueInfo info;
  7038. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7039. return JavascriptOperators::OP_GetRootProperty(object, propertyId, &info, scriptContext);
  7040. }
  7041. template <bool IsFromFullJit, class TInlineCache>
  7042. inline Var JavascriptOperators::PatchGetPropertyScoped(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance)
  7043. {
  7044. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetPropertyScoped);
  7045. // Get the property, using a scope stack rather than an individual instance.
  7046. // Walk the stack until we find an instance that has the property.
  7047. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7048. uint16 length = pDisplay->GetLength();
  7049. PropertyValueInfo info;
  7050. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7051. for (uint16 i = 0; i < length; i++)
  7052. {
  7053. RecyclableObject* object = RecyclableObject::UnsafeFromVar(pDisplay->GetItem(i));
  7054. Var value;
  7055. if (CacheOperators::TryGetProperty<true, true, true, false, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7056. object, false, object, propertyId, &value, scriptContext, nullptr, &info))
  7057. {
  7058. return value;
  7059. }
  7060. #if DBG_DUMP
  7061. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7062. {
  7063. CacheOperators::TraceCache(inlineCache, _u("PatchGetPropertyScoped"), propertyId, scriptContext, object);
  7064. }
  7065. #endif
  7066. if (JavascriptOperators::GetProperty(object, propertyId, &value, scriptContext, &info))
  7067. {
  7068. if (scriptContext->IsUndeclBlockVar(value) && propertyId != PropertyIds::_this)
  7069. {
  7070. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  7071. }
  7072. return value;
  7073. }
  7074. }
  7075. // There is no root decl for 'this', we should instead load the global 'this' value.
  7076. if (propertyId == PropertyIds::_this)
  7077. {
  7078. Var varNull = OP_LdNull(scriptContext);
  7079. return JavascriptOperators::OP_GetThis(varNull, functionBody->GetModuleID(), scriptContext);
  7080. }
  7081. else if (propertyId == PropertyIds::_super)
  7082. {
  7083. JavascriptError::ThrowReferenceError(scriptContext, JSERR_BadSuperReference);
  7084. }
  7085. // No one in the scope stack has the property, so get it from the default instance provided by the caller.
  7086. Var value = JavascriptOperators::PatchGetRootValue<IsFromFullJit>(functionBody, inlineCache, inlineCacheIndex, DynamicObject::FromVar(defaultInstance), propertyId);
  7087. if (scriptContext->IsUndeclBlockVar(value))
  7088. {
  7089. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  7090. }
  7091. return value;
  7092. JIT_HELPER_END(Op_PatchGetPropertyScoped);
  7093. }
  7094. template Var JavascriptOperators::PatchGetPropertyScoped<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7095. template Var JavascriptOperators::PatchGetPropertyScoped<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7096. template Var JavascriptOperators::PatchGetPropertyScoped<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7097. template Var JavascriptOperators::PatchGetPropertyScoped<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7098. template <bool IsFromFullJit, class TInlineCache>
  7099. Var JavascriptOperators::PatchGetPropertyForTypeOfScoped(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance)
  7100. {
  7101. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetPropertyForTypeOfScoped);
  7102. Var value = nullptr;
  7103. ScriptContext *scriptContext = functionBody->GetScriptContext();
  7104. BEGIN_TYPEOF_ERROR_HANDLER(scriptContext);
  7105. value = JavascriptOperators::PatchGetPropertyScoped<IsFromFullJit, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, pDisplay, propertyId, defaultInstance);
  7106. END_TYPEOF_ERROR_HANDLER(scriptContext, value)
  7107. return value;
  7108. JIT_HELPER_END(Op_PatchGetPropertyForTypeOfScoped);
  7109. }
  7110. template Var JavascriptOperators::PatchGetPropertyForTypeOfScoped<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7111. template Var JavascriptOperators::PatchGetPropertyForTypeOfScoped<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7112. template Var JavascriptOperators::PatchGetPropertyForTypeOfScoped<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7113. template Var JavascriptOperators::PatchGetPropertyForTypeOfScoped<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7114. template <bool IsFromFullJit, class TInlineCache>
  7115. inline Var JavascriptOperators::PatchGetMethod(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7116. {
  7117. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetMethod);
  7118. Assert(inlineCache != nullptr);
  7119. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7120. RecyclableObject* object = nullptr;
  7121. #if ENABLE_COPYONACCESS_ARRAY
  7122. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  7123. #endif
  7124. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7125. {
  7126. // Don't error if we disabled implicit calls
  7127. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7128. {
  7129. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7130. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7131. }
  7132. else
  7133. {
  7134. #ifdef TELEMETRY_JSO
  7135. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  7136. {
  7137. // `successful` will be true as PatchGetMethod throws an exception if not found.
  7138. scriptContext->GetTelemetry().GetOpcodeTelemetry().GetMethodProperty(object, propertyId, value, /*successful:*/false);
  7139. }
  7140. #endif
  7141. return scriptContext->GetLibrary()->GetUndefined();
  7142. }
  7143. }
  7144. PropertyValueInfo info;
  7145. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7146. Var value;
  7147. if (CacheOperators::TryGetProperty<true, true, true, false, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7148. instance, false, object, propertyId, &value, scriptContext, nullptr, &info))
  7149. {
  7150. return value;
  7151. }
  7152. #if DBG_DUMP
  7153. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7154. {
  7155. CacheOperators::TraceCache(inlineCache, _u("PatchGetMethod"), propertyId, scriptContext, object);
  7156. }
  7157. #endif
  7158. value = Js::JavascriptOperators::PatchGetMethodFromObject(instance, object, propertyId, &info, scriptContext, false);
  7159. #ifdef TELEMETRY_JSO
  7160. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  7161. {
  7162. // `successful` will be true as PatchGetMethod throws an exception if not found.
  7163. scriptContext->GetTelemetry().GetOpcodeTelemetry().GetMethodProperty(object, propertyId, value, /*successful:*/true);
  7164. }
  7165. #endif
  7166. return value;
  7167. JIT_HELPER_END(Op_PatchGetMethod);
  7168. }
  7169. JIT_HELPER_TEMPLATE(Op_PatchGetMethod, Op_PatchGetMethodPolymorphic)
  7170. template Var JavascriptOperators::PatchGetMethod<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7171. template Var JavascriptOperators::PatchGetMethod<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7172. template Var JavascriptOperators::PatchGetMethod<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7173. template Var JavascriptOperators::PatchGetMethod<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7174. template <bool IsFromFullJit, class TInlineCache>
  7175. inline Var JavascriptOperators::PatchGetRootMethod(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId)
  7176. {
  7177. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetRootMethod);
  7178. Assert(inlineCache != nullptr);
  7179. AssertMsg(RootObjectBase::Is(object), "Root must be a global object!");
  7180. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7181. PropertyValueInfo info;
  7182. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7183. Var value;
  7184. if (CacheOperators::TryGetProperty<true, true, true, false, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7185. object, true, object, propertyId, &value, scriptContext, nullptr, &info))
  7186. {
  7187. return value;
  7188. }
  7189. #if DBG_DUMP
  7190. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7191. {
  7192. CacheOperators::TraceCache(inlineCache, _u("PatchGetRootMethod"), propertyId, scriptContext, object);
  7193. }
  7194. #endif
  7195. value = Js::JavascriptOperators::PatchGetMethodFromObject(object, object, propertyId, &info, scriptContext, true);
  7196. #ifdef TELEMETRY_JSO
  7197. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  7198. {
  7199. // `successful` will be true as PatchGetMethod throws an exception if not found.
  7200. scriptContext->GetTelemetry().GetOpcodeTelemetry().GetMethodProperty(object, propertyId, value, /*successful:*/ true);
  7201. }
  7202. #endif
  7203. return value;
  7204. JIT_HELPER_END(Op_PatchGetRootMethod);
  7205. }
  7206. JIT_HELPER_TEMPLATE(Op_PatchGetRootMethod, Op_PatchGetRootMethodPolymorphic)
  7207. template Var JavascriptOperators::PatchGetRootMethod<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId);
  7208. template Var JavascriptOperators::PatchGetRootMethod<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId);
  7209. template Var JavascriptOperators::PatchGetRootMethod<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId);
  7210. template Var JavascriptOperators::PatchGetRootMethod<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId);
  7211. template <bool IsFromFullJit, class TInlineCache>
  7212. inline Var JavascriptOperators::PatchScopedGetMethod(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7213. {
  7214. JIT_HELPER_REENTRANT_HEADER(Op_ScopedGetMethod);
  7215. Assert(inlineCache != nullptr);
  7216. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7217. RecyclableObject* object = nullptr;
  7218. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7219. {
  7220. // Don't error if we disabled implicit calls
  7221. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7222. {
  7223. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7224. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7225. }
  7226. else
  7227. {
  7228. return scriptContext->GetLibrary()->GetUndefined();
  7229. }
  7230. }
  7231. PropertyValueInfo info;
  7232. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7233. const bool isRoot = RootObjectBase::Is(object);
  7234. Var value;
  7235. if (CacheOperators::TryGetProperty<true, true, true, false, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7236. instance, isRoot, object, propertyId, &value, scriptContext, nullptr, &info))
  7237. {
  7238. return value;
  7239. }
  7240. #if DBG_DUMP
  7241. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7242. {
  7243. CacheOperators::TraceCache(inlineCache, _u("PatchGetMethod"), propertyId, scriptContext, object);
  7244. }
  7245. #endif
  7246. return Js::JavascriptOperators::PatchGetMethodFromObject(instance, object, propertyId, &info, scriptContext, isRoot);
  7247. JIT_HELPER_END(Op_ScopedGetMethod);
  7248. }
  7249. JIT_HELPER_TEMPLATE(Op_ScopedGetMethod, Op_ScopedGetMethodPolymorphic)
  7250. template Var JavascriptOperators::PatchScopedGetMethod<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7251. template Var JavascriptOperators::PatchScopedGetMethod<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7252. template Var JavascriptOperators::PatchScopedGetMethod<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7253. template Var JavascriptOperators::PatchScopedGetMethod<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7254. Var JavascriptOperators::PatchGetMethodNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7255. {
  7256. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7257. RecyclableObject* object = nullptr;
  7258. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7259. {
  7260. // Don't error if we disabled implicit calls
  7261. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7262. {
  7263. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7264. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7265. }
  7266. else
  7267. {
  7268. return scriptContext->GetLibrary()->GetUndefined();
  7269. }
  7270. }
  7271. PropertyValueInfo info;
  7272. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7273. return Js::JavascriptOperators::PatchGetMethodFromObject(instance, object, propertyId, &info, scriptContext, false);
  7274. }
  7275. Var JavascriptOperators::PatchGetRootMethodNoFastPath_Var(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7276. {
  7277. return
  7278. PatchGetRootMethodNoFastPath(
  7279. functionBody,
  7280. inlineCache,
  7281. inlineCacheIndex,
  7282. DynamicObject::FromVar(instance),
  7283. propertyId);
  7284. }
  7285. Var JavascriptOperators::PatchGetRootMethodNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId)
  7286. {
  7287. AssertMsg(RootObjectBase::Is(object), "Root must be a global object!");
  7288. PropertyValueInfo info;
  7289. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7290. return Js::JavascriptOperators::PatchGetMethodFromObject(object, object, propertyId, &info, functionBody->GetScriptContext(), true);
  7291. }
  7292. Var JavascriptOperators::PatchGetMethodFromObject(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, PropertyValueInfo * info, ScriptContext* scriptContext, bool isRootLd)
  7293. {
  7294. Assert(IsPropertyObject(propertyObject));
  7295. Var value = nullptr;
  7296. BOOL foundValue = FALSE;
  7297. if (isRootLd)
  7298. {
  7299. RootObjectBase* rootObject = RootObjectBase::FromVar(instance);
  7300. foundValue = JavascriptOperators::GetRootPropertyReference(rootObject, propertyId, &value, scriptContext, info);
  7301. }
  7302. else
  7303. {
  7304. foundValue = JavascriptOperators::GetPropertyReference(instance, propertyObject, propertyId, &value, scriptContext, info);
  7305. }
  7306. if (!foundValue)
  7307. {
  7308. // Don't error if we disabled implicit calls
  7309. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7310. {
  7311. const char16* propertyName = scriptContext->GetPropertyName(propertyId)->GetBuffer();
  7312. value = scriptContext->GetLibrary()->GetUndefined();
  7313. JavascriptFunction * caller = NULL;
  7314. if (JavascriptStackWalker::GetCaller(&caller, scriptContext))
  7315. {
  7316. FunctionBody * callerBody = caller->GetFunctionBody();
  7317. if (callerBody && callerBody->GetUtf8SourceInfo()->GetIsXDomain())
  7318. {
  7319. propertyName = NULL;
  7320. }
  7321. }
  7322. // Prior to version 12 we had mistakenly immediately thrown an error for property reference method calls
  7323. // (i.e. <expr>.foo() form) when the target object is the global object. The spec says that a GetValue
  7324. // on a reference should throw if the reference is unresolved, of which a property reference can never be,
  7325. // however it can be unresolved in the case of an identifier expression, e.g. foo() with no qualification.
  7326. // Such a case would come down to the global object if foo was undefined, hence the check for root object,
  7327. // except that it should have been a check for isRootLd to be correct.
  7328. //
  7329. // // (at global scope)
  7330. // foo(x());
  7331. //
  7332. // should throw an error before evaluating x() if foo is not defined, but
  7333. //
  7334. // // (at global scope)
  7335. // this.foo(x());
  7336. //
  7337. // should evaluate x() before throwing an error if foo is not a property on the global object.
  7338. // Maintain old behavior prior to version 12.
  7339. bool isPropertyReference = !isRootLd;
  7340. if (!isPropertyReference)
  7341. {
  7342. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UndefVariable, propertyName);
  7343. }
  7344. else
  7345. {
  7346. // 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
  7347. // first (#3). Postpone throwing error to invoke time.
  7348. value = ThrowErrorObject::CreateThrowTypeErrorObject(scriptContext, VBSERR_OLENoPropOrMethod, propertyName);
  7349. }
  7350. }
  7351. }
  7352. return value;
  7353. }
  7354. template <bool IsFromFullJit, class TInlineCache>
  7355. inline void JavascriptOperators::PatchPutValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7356. {
  7357. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValue);
  7358. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValue, Op_PatchPutValueWithThisPtr);
  7359. return PatchPutValueWithThisPtr<IsFromFullJit, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7360. JIT_HELPER_END(Op_PatchPutValue);
  7361. }
  7362. JIT_HELPER_TEMPLATE(Op_PatchPutValue, Op_PatchPutValuePolymorphic)
  7363. template <bool IsFromFullJit, class TInlineCache>
  7364. inline void JavascriptOperators::PatchPutValueWithThisPtr(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7365. {
  7366. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtr);
  7367. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7368. if (TaggedNumber::Is(instance))
  7369. {
  7370. JavascriptOperators::SetPropertyOnTaggedNumber(instance, nullptr, propertyId, newValue, scriptContext, flags);
  7371. return;
  7372. }
  7373. #if ENABLE_COPYONACCESS_ARRAY
  7374. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  7375. #endif
  7376. RecyclableObject* object = RecyclableObject::FromVar(instance);
  7377. PropertyValueInfo info;
  7378. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7379. if (CacheOperators::TrySetProperty<true, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7380. object, false, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7381. {
  7382. return;
  7383. }
  7384. #if DBG_DUMP
  7385. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7386. {
  7387. CacheOperators::TraceCache(inlineCache, _u("PatchPutValue"), propertyId, scriptContext, object);
  7388. }
  7389. #endif
  7390. ImplicitCallFlags prevImplicitCallFlags = ImplicitCall_None;
  7391. ImplicitCallFlags currImplicitCallFlags = ImplicitCall_None;
  7392. bool hasThisOnlyStatements = functionBody->GetHasOnlyThisStmts();
  7393. if (hasThisOnlyStatements)
  7394. {
  7395. prevImplicitCallFlags = CacheAndClearImplicitBit(scriptContext);
  7396. }
  7397. if (!JavascriptOperators::OP_SetProperty(object, propertyId, newValue, scriptContext, &info, flags, thisInstance))
  7398. {
  7399. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7400. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7401. }
  7402. if (hasThisOnlyStatements)
  7403. {
  7404. currImplicitCallFlags = CheckAndUpdateFunctionBodyWithImplicitFlag(functionBody);
  7405. RestoreImplicitFlag(scriptContext, prevImplicitCallFlags, currImplicitCallFlags);
  7406. }
  7407. JIT_HELPER_END(Op_PatchPutValueWithThisPtr);
  7408. }
  7409. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtr, Op_PatchPutValueWithThisPtrPolymorphic)
  7410. template void JavascriptOperators::PatchPutValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7411. template void JavascriptOperators::PatchPutValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7412. template void JavascriptOperators::PatchPutValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7413. template void JavascriptOperators::PatchPutValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7414. template <bool IsFromFullJit, class TInlineCache>
  7415. inline void JavascriptOperators::PatchPutRootValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7416. {
  7417. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutRootValue);
  7418. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7419. RecyclableObject* object = RecyclableObject::FromVar(instance);
  7420. PropertyValueInfo info;
  7421. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7422. if (CacheOperators::TrySetProperty<true, true, true, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7423. object, true, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7424. {
  7425. return;
  7426. }
  7427. #if DBG_DUMP
  7428. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7429. {
  7430. CacheOperators::TraceCache(inlineCache, _u("PatchPutRootValue"), propertyId, scriptContext, object);
  7431. }
  7432. #endif
  7433. ImplicitCallFlags prevImplicitCallFlags = ImplicitCall_None;
  7434. ImplicitCallFlags currImplicitCallFlags = ImplicitCall_None;
  7435. bool hasThisOnlyStatements = functionBody->GetHasOnlyThisStmts();
  7436. if (hasThisOnlyStatements)
  7437. {
  7438. prevImplicitCallFlags = CacheAndClearImplicitBit(scriptContext);
  7439. }
  7440. if (!JavascriptOperators::SetRootProperty(object, propertyId, newValue, &info, scriptContext, flags))
  7441. {
  7442. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7443. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7444. }
  7445. if (hasThisOnlyStatements)
  7446. {
  7447. currImplicitCallFlags = CheckAndUpdateFunctionBodyWithImplicitFlag(functionBody);
  7448. RestoreImplicitFlag(scriptContext, prevImplicitCallFlags, currImplicitCallFlags);
  7449. }
  7450. JIT_HELPER_END(Op_PatchPutRootValue);
  7451. }
  7452. JIT_HELPER_TEMPLATE(Op_PatchPutRootValue, Op_PatchPutRootValuePolymorphic)
  7453. template void JavascriptOperators::PatchPutRootValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7454. template void JavascriptOperators::PatchPutRootValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7455. template void JavascriptOperators::PatchPutRootValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7456. template void JavascriptOperators::PatchPutRootValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7457. template <bool IsFromFullJit, class TInlineCache>
  7458. inline void JavascriptOperators::PatchPutValueNoLocalFastPath(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7459. {
  7460. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueNoLocalFastPath);
  7461. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueNoLocalFastPath, Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7462. PatchPutValueWithThisPtrNoLocalFastPath<IsFromFullJit, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7463. JIT_HELPER_END(Op_PatchPutValueNoLocalFastPath);
  7464. }
  7465. JIT_HELPER_TEMPLATE(Op_PatchPutValueNoLocalFastPath, Op_PatchPutValueNoLocalFastPathPolymorphic)
  7466. template void JavascriptOperators::PatchPutValueNoLocalFastPath<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7467. template void JavascriptOperators::PatchPutValueNoLocalFastPath<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7468. template void JavascriptOperators::PatchPutValueNoLocalFastPath<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7469. template void JavascriptOperators::PatchPutValueNoLocalFastPath<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7470. template <bool IsFromFullJit, class TInlineCache>
  7471. inline void JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPath(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7472. {
  7473. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7474. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7475. if (TaggedNumber::Is(instance))
  7476. {
  7477. JavascriptOperators::SetPropertyOnTaggedNumber(instance,
  7478. nullptr,
  7479. propertyId,
  7480. newValue,
  7481. scriptContext,
  7482. flags);
  7483. return;
  7484. }
  7485. #if ENABLE_COPYONACCESS_ARRAY
  7486. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  7487. #endif
  7488. RecyclableObject *object = RecyclableObject::UnsafeFromVar(instance);
  7489. PropertyValueInfo info;
  7490. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7491. if (CacheOperators::TrySetProperty<!TInlineCache::IsPolymorphic, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7492. object, false, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7493. {
  7494. return;
  7495. }
  7496. #if DBG_DUMP
  7497. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7498. {
  7499. CacheOperators::TraceCache(inlineCache, _u("PatchPutValueNoLocalFastPath"), propertyId, scriptContext, object);
  7500. }
  7501. #endif
  7502. ImplicitCallFlags prevImplicitCallFlags = ImplicitCall_None;
  7503. ImplicitCallFlags currImplicitCallFlags = ImplicitCall_None;
  7504. bool hasThisOnlyStatements = functionBody->GetHasOnlyThisStmts();
  7505. if (hasThisOnlyStatements)
  7506. {
  7507. prevImplicitCallFlags = CacheAndClearImplicitBit(scriptContext);
  7508. }
  7509. if (!JavascriptOperators::OP_SetProperty(instance, propertyId, newValue, scriptContext, &info, flags, thisInstance))
  7510. {
  7511. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7512. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7513. }
  7514. if (hasThisOnlyStatements)
  7515. {
  7516. currImplicitCallFlags = CheckAndUpdateFunctionBodyWithImplicitFlag(functionBody);
  7517. RestoreImplicitFlag(scriptContext, prevImplicitCallFlags, currImplicitCallFlags);
  7518. }
  7519. JIT_HELPER_END(Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7520. }
  7521. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtrNoLocalFastPath, Op_PatchPutValueWithThisPtrNoLocalFastPathPolymorphic)
  7522. 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);
  7523. 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);
  7524. 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);
  7525. 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);
  7526. template <bool IsFromFullJit, class TInlineCache>
  7527. inline void JavascriptOperators::PatchPutRootValueNoLocalFastPath(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7528. {
  7529. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutRootValueNoLocalFastPath);
  7530. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7531. RecyclableObject *object = RecyclableObject::FromVar(instance);
  7532. PropertyValueInfo info;
  7533. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7534. if (CacheOperators::TrySetProperty<!TInlineCache::IsPolymorphic, true, true, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7535. object, true, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7536. {
  7537. return;
  7538. }
  7539. #if DBG_DUMP
  7540. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7541. {
  7542. CacheOperators::TraceCache(inlineCache, _u("PatchPutRootValueNoLocalFastPath"), propertyId, scriptContext, object);
  7543. }
  7544. #endif
  7545. ImplicitCallFlags prevImplicitCallFlags = ImplicitCall_None;
  7546. ImplicitCallFlags currImplicitCallFlags = ImplicitCall_None;
  7547. bool hasThisOnlyStatements = functionBody->GetHasOnlyThisStmts();
  7548. if (hasThisOnlyStatements)
  7549. {
  7550. prevImplicitCallFlags = CacheAndClearImplicitBit(scriptContext);
  7551. }
  7552. if (!JavascriptOperators::SetRootProperty(object, propertyId, newValue, &info, scriptContext, flags))
  7553. {
  7554. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7555. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7556. }
  7557. if (hasThisOnlyStatements)
  7558. {
  7559. currImplicitCallFlags = CheckAndUpdateFunctionBodyWithImplicitFlag(functionBody);
  7560. RestoreImplicitFlag(scriptContext, prevImplicitCallFlags, currImplicitCallFlags);
  7561. }
  7562. JIT_HELPER_END(Op_PatchPutRootValueNoLocalFastPath);
  7563. }
  7564. JIT_HELPER_TEMPLATE(Op_PatchPutRootValueNoLocalFastPath, Op_PatchPutRootValueNoLocalFastPathPolymorphic)
  7565. template void JavascriptOperators::PatchPutRootValueNoLocalFastPath<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7566. template void JavascriptOperators::PatchPutRootValueNoLocalFastPath<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7567. template void JavascriptOperators::PatchPutRootValueNoLocalFastPath<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7568. template void JavascriptOperators::PatchPutRootValueNoLocalFastPath<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7569. void JavascriptOperators::PatchPutValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7570. {
  7571. PatchPutValueWithThisPtrNoFastPath(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7572. }
  7573. void JavascriptOperators::PatchPutValueWithThisPtrNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7574. {
  7575. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7576. if (TaggedNumber::Is(instance))
  7577. {
  7578. JavascriptOperators::SetPropertyOnTaggedNumber(instance, nullptr, propertyId, newValue, scriptContext, flags);
  7579. return;
  7580. }
  7581. RecyclableObject* object = RecyclableObject::FromVar(instance);
  7582. PropertyValueInfo info;
  7583. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7584. if (!JavascriptOperators::OP_SetProperty(object, propertyId, newValue, scriptContext, &info, flags, thisInstance))
  7585. {
  7586. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7587. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7588. }
  7589. }
  7590. void JavascriptOperators::PatchPutRootValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7591. {
  7592. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7593. RecyclableObject* object = RecyclableObject::FromVar(instance);
  7594. PropertyValueInfo info;
  7595. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7596. if (!JavascriptOperators::SetRootProperty(object, propertyId, newValue, &info, scriptContext, flags))
  7597. {
  7598. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7599. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7600. }
  7601. }
  7602. template <bool IsFromFullJit, class TInlineCache>
  7603. inline void JavascriptOperators::PatchInitValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue)
  7604. {
  7605. JIT_HELPER_REENTRANT_HEADER(Op_PatchInitValue);
  7606. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7607. const PropertyOperationFlags flags = newValue == NULL ? PropertyOperation_SpecialValue : PropertyOperation_None;
  7608. PropertyValueInfo info;
  7609. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7610. if (CacheOperators::TrySetProperty<true, true, false, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7611. object, false, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7612. {
  7613. return;
  7614. }
  7615. #if DBG_DUMP
  7616. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7617. {
  7618. CacheOperators::TraceCache(inlineCache, _u("PatchInitValue"), propertyId, scriptContext, object);
  7619. }
  7620. #endif
  7621. Type *typeWithoutProperty = object->GetType();
  7622. if (functionBody->IsEval())
  7623. {
  7624. if (object->InitPropertyInEval(propertyId, newValue, flags, &info))
  7625. {
  7626. CacheOperators::CachePropertyWrite(object, false, typeWithoutProperty, propertyId, &info, scriptContext);
  7627. return;
  7628. }
  7629. }
  7630. // Ideally the lowerer would emit a call to the right flavor of PatchInitValue, so that we can ensure that we only
  7631. // ever initialize to NULL in the right cases. But the backend uses the StFld opcode for initialization, and it
  7632. // would be cumbersome to thread the different helper calls all the way down
  7633. if (object->InitProperty(propertyId, newValue, flags, &info))
  7634. {
  7635. CacheOperators::CachePropertyWrite(object, false, typeWithoutProperty, propertyId, &info, scriptContext);
  7636. }
  7637. JIT_HELPER_END(Op_PatchInitValue);
  7638. }
  7639. JIT_HELPER_TEMPLATE(Op_PatchInitValue, Op_PatchInitValuePolymorphic)
  7640. template void JavascriptOperators::PatchInitValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7641. template void JavascriptOperators::PatchInitValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7642. template void JavascriptOperators::PatchInitValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7643. template void JavascriptOperators::PatchInitValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7644. void JavascriptOperators::PatchInitValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue)
  7645. {
  7646. PropertyValueInfo info;
  7647. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7648. Type *typeWithoutProperty = object->GetType();
  7649. if (functionBody->IsEval())
  7650. {
  7651. if (object->InitPropertyInEval(propertyId, newValue, PropertyOperation_None, &info))
  7652. {
  7653. CacheOperators::CachePropertyWrite(object, false, typeWithoutProperty, propertyId, &info, functionBody->GetScriptContext());
  7654. return;
  7655. }
  7656. }
  7657. if (object->InitProperty(propertyId, newValue, PropertyOperation_None, &info))
  7658. {
  7659. CacheOperators::CachePropertyWrite(object, false, typeWithoutProperty, propertyId, &info, functionBody->GetScriptContext());
  7660. }
  7661. }
  7662. void JavascriptOperators::GetPropertyIdForInt(uint64 value, ScriptContext* scriptContext, PropertyRecord const ** propertyRecord)
  7663. {
  7664. char16 buffer[20];
  7665. ::_ui64tow_s(value, buffer, sizeof(buffer)/sizeof(char16), 10);
  7666. scriptContext->GetOrAddPropertyRecord(buffer, JavascriptString::GetBufferLength(buffer), propertyRecord);
  7667. }
  7668. void JavascriptOperators::GetPropertyIdForInt(uint32 value, ScriptContext* scriptContext, PropertyRecord const ** propertyRecord)
  7669. {
  7670. GetPropertyIdForInt(static_cast<uint64>(value), scriptContext, propertyRecord);
  7671. }
  7672. Var JavascriptOperators::FromPropertyDescriptor(const PropertyDescriptor& descriptor, ScriptContext* scriptContext)
  7673. {
  7674. DynamicObject* object = scriptContext->GetLibrary()->CreateObject();
  7675. // ES5 Section 8.10.4 specifies the order for adding these properties.
  7676. if (descriptor.IsDataDescriptor())
  7677. {
  7678. if (descriptor.ValueSpecified())
  7679. {
  7680. JavascriptOperators::InitProperty(object, PropertyIds::value, descriptor.GetValue());
  7681. }
  7682. JavascriptOperators::InitProperty(object, PropertyIds::writable, JavascriptBoolean::ToVar(descriptor.IsWritable(),scriptContext));
  7683. }
  7684. else if (descriptor.IsAccessorDescriptor())
  7685. {
  7686. JavascriptOperators::InitProperty(object, PropertyIds::get, JavascriptOperators::CanonicalizeAccessor(descriptor.GetGetter(), scriptContext));
  7687. JavascriptOperators::InitProperty(object, PropertyIds::set, JavascriptOperators::CanonicalizeAccessor(descriptor.GetSetter(), scriptContext));
  7688. }
  7689. if (descriptor.EnumerableSpecified())
  7690. {
  7691. JavascriptOperators::InitProperty(object, PropertyIds::enumerable, JavascriptBoolean::ToVar(descriptor.IsEnumerable(), scriptContext));
  7692. }
  7693. if (descriptor.ConfigurableSpecified())
  7694. {
  7695. JavascriptOperators::InitProperty(object, PropertyIds::configurable, JavascriptBoolean::ToVar(descriptor.IsConfigurable(), scriptContext));
  7696. }
  7697. return object;
  7698. }
  7699. // ES5 8.12.9 [[DefineOwnProperty]].
  7700. // Return value:
  7701. // - TRUE = success.
  7702. // - FALSE (can throw depending on throwOnError parameter) = unsuccessful.
  7703. BOOL JavascriptOperators::DefineOwnPropertyDescriptor(RecyclableObject* obj, PropertyId propId, const PropertyDescriptor& descriptor, bool throwOnError, ScriptContext* scriptContext)
  7704. {
  7705. Assert(obj);
  7706. Assert(scriptContext);
  7707. if (JavascriptProxy::Is(obj))
  7708. {
  7709. return JavascriptProxy::DefineOwnPropertyDescriptor(obj, propId, descriptor, throwOnError, scriptContext);
  7710. }
  7711. PropertyDescriptor currentDescriptor;
  7712. BOOL isCurrentDescriptorDefined = JavascriptOperators::GetOwnPropertyDescriptor(obj, propId, scriptContext, &currentDescriptor);
  7713. bool isExtensible = !!obj->IsExtensible();
  7714. return ValidateAndApplyPropertyDescriptor<true>(obj, propId, descriptor, isCurrentDescriptorDefined ? &currentDescriptor : nullptr, isExtensible, throwOnError, scriptContext);
  7715. }
  7716. BOOL JavascriptOperators::IsCompatiblePropertyDescriptor(const PropertyDescriptor& descriptor, PropertyDescriptor* currentDescriptor, bool isExtensible, bool throwOnError, ScriptContext* scriptContext)
  7717. {
  7718. return ValidateAndApplyPropertyDescriptor<false>(nullptr, Constants::NoProperty, descriptor, currentDescriptor, isExtensible, throwOnError, scriptContext);
  7719. }
  7720. template<bool needToSetProperty>
  7721. BOOL JavascriptOperators::ValidateAndApplyPropertyDescriptor(RecyclableObject* obj, PropertyId propId, const PropertyDescriptor& descriptor,
  7722. PropertyDescriptor* currentDescriptor, bool isExtensible, bool throwOnError, ScriptContext* scriptContext)
  7723. {
  7724. Var defaultDataValue = scriptContext->GetLibrary()->GetUndefined();
  7725. Var defaultAccessorValue = scriptContext->GetLibrary()->GetDefaultAccessorFunction();
  7726. if (currentDescriptor == nullptr)
  7727. {
  7728. if (!isExtensible) // ES5 8.12.9.3.
  7729. {
  7730. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotExtensible, propId);
  7731. }
  7732. else // ES5 8.12.9.4.
  7733. {
  7734. if (needToSetProperty)
  7735. {
  7736. if (descriptor.IsGenericDescriptor() || descriptor.IsDataDescriptor())
  7737. {
  7738. // ES5 8.12.9.4a: Create an own data property named P of object O whose [[Value]], [[Writable]],
  7739. // [[Enumerable]] and [[Configurable]] attribute values are described by Desc.
  7740. // If the value of an attribute field of Desc is absent, the attribute of the newly created property
  7741. // is set to its default value.
  7742. PropertyDescriptor filledDescriptor = FillMissingPropertyDescriptorFields<false>(descriptor, scriptContext);
  7743. BOOL tempResult = obj->SetPropertyWithAttributes(propId, filledDescriptor.GetValue(), filledDescriptor.GetAttributes(), nullptr);
  7744. if (!obj->IsExternal() && !tempResult)
  7745. {
  7746. Assert(TypedArrayBase::Is(obj)); // typed array returns false when canonical numeric index is not integer or out of range
  7747. return FALSE;
  7748. }
  7749. }
  7750. else
  7751. {
  7752. // ES5 8.12.9.4b: Create an own accessor property named P of object O whose [[Get]], [[Set]], [[Enumerable]]
  7753. // and [[Configurable]] attribute values are described by Desc. If the value of an attribute field of Desc is absent,
  7754. // the attribute of the newly created property is set to its default value.
  7755. Assert(descriptor.IsAccessorDescriptor());
  7756. PropertyDescriptor filledDescriptor = FillMissingPropertyDescriptorFields<true>(descriptor, scriptContext);
  7757. BOOL isSetAccessorsSuccess = obj->SetAccessors(propId, filledDescriptor.GetGetter(), filledDescriptor.GetSetter());
  7758. // It is valid for some objects to not-support getters and setters, specifically, for projection of an ABI method
  7759. // (CustomExternalObject => MapWithStringKey) which SetAccessors returns VBSErr_ActionNotSupported.
  7760. // But for non-external objects SetAccessors should succeed.
  7761. Assert(isSetAccessorsSuccess || obj->IsExternal());
  7762. // If SetAccessors failed, the property wasn't created, so no need to change the attributes.
  7763. if (isSetAccessorsSuccess)
  7764. {
  7765. JavascriptOperators::SetAttributes(obj, propId, filledDescriptor, true); // use 'force' as default attributes in type system are different from ES5.
  7766. }
  7767. }
  7768. }
  7769. return TRUE;
  7770. }
  7771. }
  7772. // ES5 8.12.9.5: Return true, if every field in Desc is absent.
  7773. if (!descriptor.ConfigurableSpecified() && !descriptor.EnumerableSpecified() && !descriptor.WritableSpecified() &&
  7774. !descriptor.ValueSpecified() && !descriptor.GetterSpecified() && !descriptor.SetterSpecified())
  7775. {
  7776. return TRUE;
  7777. }
  7778. // 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
  7779. // as the corresponding field in current when compared using the SameValue algorithm (9.12).
  7780. PropertyDescriptor filledDescriptor = descriptor.IsAccessorDescriptor() ? FillMissingPropertyDescriptorFields<true>(descriptor, scriptContext)
  7781. : FillMissingPropertyDescriptorFields<false>(descriptor, scriptContext);
  7782. if (JavascriptOperators::AreSamePropertyDescriptors(&filledDescriptor, currentDescriptor, scriptContext))
  7783. {
  7784. return TRUE;
  7785. }
  7786. if (!currentDescriptor->IsConfigurable()) // ES5 8.12.9.7.
  7787. {
  7788. if (descriptor.ConfigurableSpecified() && descriptor.IsConfigurable())
  7789. {
  7790. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  7791. }
  7792. if (descriptor.EnumerableSpecified() && descriptor.IsEnumerable() != currentDescriptor->IsEnumerable())
  7793. {
  7794. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  7795. }
  7796. }
  7797. // Whether to merge attributes from tempDescriptor into descriptor to keep original values
  7798. // of some attributes from the object/use tempDescriptor for SetAttributes, or just use descriptor.
  7799. // This is optimization to avoid 2 calls to SetAttributes.
  7800. bool mergeDescriptors = false;
  7801. // Whether to call SetAttributes with 'force' flag which forces setting all attributes
  7802. // rather than only specified or which have true values.
  7803. // This is to make sure that the object has correct attributes, as default values in the object are not for ES5.
  7804. bool forceSetAttributes = false;
  7805. PropertyDescriptor tempDescriptor;
  7806. // ES5 8.12.9.8: If IsGenericDescriptor(Desc) is true, then no further validation is required.
  7807. if (!descriptor.IsGenericDescriptor())
  7808. {
  7809. if (currentDescriptor->IsDataDescriptor() != descriptor.IsDataDescriptor())
  7810. {
  7811. // ES5 8.12.9.9: Else, if IsDataDescriptor(current) and IsDataDescriptor(Desc) have different results...
  7812. if (!currentDescriptor->IsConfigurable())
  7813. {
  7814. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  7815. }
  7816. if (needToSetProperty)
  7817. {
  7818. if (currentDescriptor->IsDataDescriptor())
  7819. {
  7820. // ES5 8.12.9.9.b: Convert the property named P of object O from a data property to an accessor property.
  7821. // Preserve the existing values of the converted property's [[Configurable]] and [[Enumerable]] attributes
  7822. // and set the rest of the property's attributes to their default values.
  7823. PropertyAttributes preserveFromObject = currentDescriptor->GetAttributes() & (PropertyConfigurable | PropertyEnumerable);
  7824. BOOL isSetAccessorsSuccess = obj->SetAccessors(propId, defaultAccessorValue, defaultAccessorValue);
  7825. // It is valid for some objects to not-support getters and setters, specifically, for projection of an ABI method
  7826. // (CustomExternalObject => MapWithStringKey) which SetAccessors returns VBSErr_ActionNotSupported.
  7827. // But for non-external objects SetAccessors should succeed.
  7828. Assert(isSetAccessorsSuccess || obj->IsExternal());
  7829. if (isSetAccessorsSuccess)
  7830. {
  7831. tempDescriptor.SetAttributes(preserveFromObject, PropertyConfigurable | PropertyEnumerable);
  7832. forceSetAttributes = true; // use SetAttrbiutes with 'force' as default attributes in type system are different from ES5.
  7833. mergeDescriptors = true;
  7834. }
  7835. }
  7836. else
  7837. {
  7838. // ES5 8.12.9.9.c: Convert the property named P of object O from an accessor property to a data property.
  7839. // Preserve the existing values of the converted property's [[Configurable]] and [[Enumerable]] attributes
  7840. // and set the rest of the property's attributes to their default values.
  7841. // Note: avoid using SetProperty/SetPropertyWithAttributes here because they has undesired side-effects:
  7842. // it calls previous setter and in some cases of attribute values throws.
  7843. // To walk around, call DeleteProperty and then AddProperty.
  7844. PropertyAttributes preserveFromObject = currentDescriptor->GetAttributes() & (PropertyConfigurable | PropertyEnumerable);
  7845. tempDescriptor.SetAttributes(preserveFromObject, PropertyConfigurable | PropertyEnumerable);
  7846. tempDescriptor.MergeFrom(descriptor); // Update only fields specified in 'descriptor'.
  7847. Var descriptorValue = descriptor.ValueSpecified() ? descriptor.GetValue() : defaultDataValue;
  7848. // Note: HostDispath'es implementation of DeleteProperty currently throws E_NOTIMPL.
  7849. obj->DeleteProperty(propId, PropertyOperation_None);
  7850. BOOL tempResult = obj->SetPropertyWithAttributes(propId, descriptorValue, tempDescriptor.GetAttributes(), NULL, PropertyOperation_Force);
  7851. Assert(tempResult);
  7852. // At this time we already set value and attributes to desired values,
  7853. // thus we can skip step ES5 8.12.9.12 and simply return true.
  7854. return TRUE;
  7855. }
  7856. }
  7857. }
  7858. else if (currentDescriptor->IsDataDescriptor() && descriptor.IsDataDescriptor())
  7859. {
  7860. // ES5 8.12.9.10: Else, if IsDataDescriptor(current) and IsDataDescriptor(Desc) are both true...
  7861. if (!currentDescriptor->IsConfigurable())
  7862. {
  7863. if (!currentDescriptor->IsWritable())
  7864. {
  7865. if (descriptor.WritableSpecified() && descriptor.IsWritable()) // ES5 8.12.9.10.a.i
  7866. {
  7867. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  7868. }
  7869. else if (descriptor.ValueSpecified() &&
  7870. !JavascriptConversion::SameValue(descriptor.GetValue(), currentDescriptor->GetValue())) // ES5 8.12.9.10.a.ii
  7871. {
  7872. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotWritable, propId);
  7873. }
  7874. }
  7875. }
  7876. // ES5 8.12.9.10.b: else, the [[Configurable]] field of current is true, so any change is acceptable.
  7877. }
  7878. else
  7879. {
  7880. // ES5 8.12.9.11: Else, IsAccessorDescriptor(current) and IsAccessorDescriptor(Desc) are both true, so...
  7881. Assert(currentDescriptor->IsAccessorDescriptor() && descriptor.IsAccessorDescriptor());
  7882. if (!currentDescriptor->IsConfigurable())
  7883. {
  7884. if ((descriptor.SetterSpecified() &&
  7885. !JavascriptConversion::SameValue(
  7886. JavascriptOperators::CanonicalizeAccessor(descriptor.GetSetter(), scriptContext),
  7887. JavascriptOperators::CanonicalizeAccessor(currentDescriptor->GetSetter(), scriptContext))) ||
  7888. (descriptor.GetterSpecified() &&
  7889. !JavascriptConversion::SameValue(
  7890. JavascriptOperators::CanonicalizeAccessor(descriptor.GetGetter(), scriptContext),
  7891. JavascriptOperators::CanonicalizeAccessor(currentDescriptor->GetGetter(), scriptContext))))
  7892. {
  7893. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  7894. }
  7895. }
  7896. }
  7897. // This part is only for non-generic descriptors:
  7898. // ES5 8.12.9.12: For each attribute field of Desc that is present,
  7899. // set the correspondingly named attribute of the property named P of object O to the value of the field.
  7900. if (descriptor.IsDataDescriptor())
  7901. {
  7902. if (descriptor.ValueSpecified() && needToSetProperty)
  7903. {
  7904. // Set just the value by passing the current attributes of the property.
  7905. // If the property's attributes are also changing (perhaps becoming non-writable),
  7906. // this will be taken care of in the call to JavascriptOperators::SetAttributes below.
  7907. // Built-in Function.prototype properties 'length', 'arguments', and 'caller' are special cases.
  7908. BOOL tempResult = obj->SetPropertyWithAttributes(propId, descriptor.GetValue(), currentDescriptor->GetAttributes(), nullptr);
  7909. AssertMsg(tempResult || JavascriptFunction::IsBuiltinProperty(obj, propId), "If you hit this assert, most likely there is something wrong with the object/type.");
  7910. }
  7911. }
  7912. else if (descriptor.IsAccessorDescriptor() && needToSetProperty)
  7913. {
  7914. Assert(descriptor.GetterSpecified() || descriptor.SetterSpecified());
  7915. Var oldGetter = defaultAccessorValue, oldSetter = defaultAccessorValue;
  7916. if (!descriptor.GetterSpecified() || !descriptor.SetterSpecified())
  7917. {
  7918. // Unless both getter and setter are specified, make sure we don't overwrite old accessor.
  7919. #pragma prefast(suppress:6031, "We defaulted oldGetter and oldSetter already, so ignoring the return value here is safe")
  7920. obj->GetAccessors(propId, &oldGetter, &oldSetter, scriptContext);
  7921. }
  7922. Var getter = descriptor.GetterSpecified() ? descriptor.GetGetter() : oldGetter;
  7923. Var setter = descriptor.SetterSpecified() ? descriptor.GetSetter() : oldSetter;
  7924. obj->SetAccessors(propId, getter, setter);
  7925. }
  7926. } // if (!descriptor.IsGenericDescriptor())
  7927. // Continue for all descriptors including generic:
  7928. // ES5 8.12.9.12: For each attribute field of Desc that is present,
  7929. // set the correspondingly named attribute of the property named P of object O to the value of the field.
  7930. if (needToSetProperty)
  7931. {
  7932. if (mergeDescriptors)
  7933. {
  7934. tempDescriptor.MergeFrom(descriptor);
  7935. JavascriptOperators::SetAttributes(obj, propId, tempDescriptor, forceSetAttributes);
  7936. }
  7937. else
  7938. {
  7939. JavascriptOperators::SetAttributes(obj, propId, descriptor, forceSetAttributes);
  7940. }
  7941. }
  7942. return TRUE;
  7943. }
  7944. template <bool isAccessor>
  7945. PropertyDescriptor JavascriptOperators::FillMissingPropertyDescriptorFields(PropertyDescriptor descriptor, ScriptContext* scriptContext)
  7946. {
  7947. PropertyDescriptor newDescriptor;
  7948. const PropertyDescriptor* defaultDescriptor = scriptContext->GetLibrary()->GetDefaultPropertyDescriptor();
  7949. if (isAccessor)
  7950. {
  7951. newDescriptor.SetGetter(descriptor.GetterSpecified() ? descriptor.GetGetter() : defaultDescriptor->GetGetter());
  7952. newDescriptor.SetSetter(descriptor.SetterSpecified() ? descriptor.GetSetter() : defaultDescriptor->GetSetter());
  7953. }
  7954. else
  7955. {
  7956. newDescriptor.SetValue(descriptor.ValueSpecified() ? descriptor.GetValue() : defaultDescriptor->GetValue());
  7957. newDescriptor.SetWritable(descriptor.WritableSpecified() ? descriptor.IsWritable() : defaultDescriptor->IsWritable());
  7958. }
  7959. newDescriptor.SetConfigurable(descriptor.ConfigurableSpecified() ? descriptor.IsConfigurable() : defaultDescriptor->IsConfigurable());
  7960. newDescriptor.SetEnumerable(descriptor.EnumerableSpecified() ? descriptor.IsEnumerable() : defaultDescriptor->IsEnumerable());
  7961. return newDescriptor;
  7962. }
  7963. // ES5: 15.4.5.1
  7964. BOOL JavascriptOperators::DefineOwnPropertyForArray(JavascriptArray* arr, PropertyId propId, const PropertyDescriptor& descriptor, bool throwOnError, ScriptContext* scriptContext)
  7965. {
  7966. if (propId == PropertyIds::length)
  7967. {
  7968. if (!descriptor.ValueSpecified())
  7969. {
  7970. return DefineOwnPropertyDescriptor(arr, PropertyIds::length, descriptor, throwOnError, scriptContext);
  7971. }
  7972. PropertyDescriptor newLenDesc = descriptor;
  7973. uint32 newLen = ES5Array::ToLengthValue(descriptor.GetValue(), scriptContext);
  7974. newLenDesc.SetValue(JavascriptNumber::ToVar(newLen, scriptContext));
  7975. uint32 oldLen = arr->GetLength();
  7976. if (newLen >= oldLen)
  7977. {
  7978. return DefineOwnPropertyDescriptor(arr, PropertyIds::length, newLenDesc, throwOnError, scriptContext);
  7979. }
  7980. BOOL oldLenWritable = arr->IsWritable(PropertyIds::length);
  7981. if (!oldLenWritable)
  7982. {
  7983. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotWritable, propId);
  7984. }
  7985. bool newWritable = (!newLenDesc.WritableSpecified() || newLenDesc.IsWritable());
  7986. if (!newWritable)
  7987. {
  7988. // Need to defer setting writable to false in case any elements cannot be deleted
  7989. newLenDesc.SetWritable(true);
  7990. }
  7991. BOOL succeeded = DefineOwnPropertyDescriptor(arr, PropertyIds::length, newLenDesc, throwOnError, scriptContext);
  7992. //
  7993. // Our SetProperty(length) is also responsible to trim elements. When succeeded is
  7994. //
  7995. // false:
  7996. // * length attributes rejected
  7997. // * elements not touched
  7998. // true:
  7999. // * length attributes are set successfully
  8000. // * elements trimming may be either completed or incompleted, length value is correct
  8001. //
  8002. // * Strict mode TODO: Currently SetProperty(length) does not throw. If that throws, we need
  8003. // to update here to set correct newWritable even on exception.
  8004. //
  8005. if (!succeeded)
  8006. {
  8007. return false;
  8008. }
  8009. if (!newWritable) // Now set requested newWritable.
  8010. {
  8011. PropertyDescriptor newWritableDesc;
  8012. newWritableDesc.SetWritable(false);
  8013. DefineOwnPropertyDescriptor(arr, PropertyIds::length, newWritableDesc, false, scriptContext);
  8014. }
  8015. if (arr->GetLength() > newLen) // Delete incompleted
  8016. {
  8017. // Since SetProperty(length) not throwing, we'll reject here
  8018. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_Default, propId);
  8019. }
  8020. return true;
  8021. }
  8022. uint32 index;
  8023. if (scriptContext->IsNumericPropertyId(propId, &index))
  8024. {
  8025. if (index >= arr->GetLength() && !arr->IsWritable(PropertyIds::length))
  8026. {
  8027. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_LengthNotWritable, propId);
  8028. }
  8029. BOOL succeeded = DefineOwnPropertyDescriptor(arr, propId, descriptor, false, scriptContext);
  8030. if (!succeeded)
  8031. {
  8032. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_Default, propId);
  8033. }
  8034. // Out SetItem takes care of growing "length". we are done.
  8035. return true;
  8036. }
  8037. return DefineOwnPropertyDescriptor(arr, propId, descriptor, throwOnError, scriptContext);
  8038. }
  8039. BOOL JavascriptOperators::SetPropertyDescriptor(RecyclableObject* object, PropertyId propId, const PropertyDescriptor& descriptor)
  8040. {
  8041. if (descriptor.ValueSpecified())
  8042. {
  8043. ScriptContext* requestContext = object->GetScriptContext(); // Real requestContext?
  8044. JavascriptOperators::SetProperty(object, object, propId, descriptor.GetValue(), requestContext);
  8045. }
  8046. else if (descriptor.GetterSpecified() || descriptor.SetterSpecified())
  8047. {
  8048. JavascriptOperators::SetAccessors(object, propId, descriptor.GetGetter(), descriptor.GetSetter());
  8049. }
  8050. if (descriptor.EnumerableSpecified())
  8051. {
  8052. object->SetEnumerable(propId, descriptor.IsEnumerable());
  8053. }
  8054. if (descriptor.ConfigurableSpecified())
  8055. {
  8056. object->SetConfigurable(propId, descriptor.IsConfigurable());
  8057. }
  8058. if (descriptor.WritableSpecified())
  8059. {
  8060. object->SetWritable(propId, descriptor.IsWritable());
  8061. }
  8062. return true;
  8063. }
  8064. BOOL JavascriptOperators::ToPropertyDescriptorForProxyObjects(Var propertySpec, PropertyDescriptor* descriptor, ScriptContext* scriptContext)
  8065. {
  8066. if (!JavascriptOperators::IsObject(propertySpec))
  8067. {
  8068. return FALSE;
  8069. }
  8070. Var value;
  8071. RecyclableObject* propertySpecObj = RecyclableObject::FromVar(propertySpec);
  8072. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::enumerable) == TRUE)
  8073. {
  8074. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::enumerable, &value, scriptContext))
  8075. {
  8076. descriptor->SetEnumerable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8077. }
  8078. else
  8079. {
  8080. // The proxy said we have the property, so we try to read the property and get the default value.
  8081. descriptor->SetEnumerable(false);
  8082. }
  8083. }
  8084. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::configurable) == TRUE)
  8085. {
  8086. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::configurable, &value, scriptContext))
  8087. {
  8088. descriptor->SetConfigurable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8089. }
  8090. else
  8091. {
  8092. // The proxy said we have the property, so we try to read the property and get the default value.
  8093. descriptor->SetConfigurable(false);
  8094. }
  8095. }
  8096. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::value) == TRUE)
  8097. {
  8098. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::value, &value, scriptContext))
  8099. {
  8100. descriptor->SetValue(value);
  8101. }
  8102. else
  8103. {
  8104. // The proxy said we have the property, so we try to read the property and get the default value.
  8105. descriptor->SetValue(scriptContext->GetLibrary()->GetUndefined());
  8106. }
  8107. }
  8108. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::writable) == TRUE)
  8109. {
  8110. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::writable, &value, scriptContext))
  8111. {
  8112. descriptor->SetWritable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8113. }
  8114. else
  8115. {
  8116. // The proxy said we have the property, so we try to read the property and get the default value.
  8117. descriptor->SetWritable(false);
  8118. }
  8119. }
  8120. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::get) == TRUE)
  8121. {
  8122. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::get, &value, scriptContext))
  8123. {
  8124. if (JavascriptOperators::GetTypeId(value) != TypeIds_Undefined && (false == JavascriptConversion::IsCallable(value)))
  8125. {
  8126. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::get)->GetBuffer());
  8127. }
  8128. descriptor->SetGetter(value);
  8129. }
  8130. else
  8131. {
  8132. // The proxy said we have the property, so we try to read the property and get the default value.
  8133. descriptor->SetGetter(scriptContext->GetLibrary()->GetUndefined());
  8134. }
  8135. }
  8136. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::set) == TRUE)
  8137. {
  8138. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::set, &value, scriptContext))
  8139. {
  8140. if (JavascriptOperators::GetTypeId(value) != TypeIds_Undefined && (false == JavascriptConversion::IsCallable(value)))
  8141. {
  8142. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::set)->GetBuffer());
  8143. }
  8144. descriptor->SetSetter(value);
  8145. }
  8146. else
  8147. {
  8148. // The proxy said we have the property, so we try to read the property and get the default value.
  8149. descriptor->SetSetter(scriptContext->GetLibrary()->GetUndefined());
  8150. }
  8151. }
  8152. return TRUE;
  8153. }
  8154. BOOL JavascriptOperators::ToPropertyDescriptorForGenericObjects(Var propertySpec, PropertyDescriptor* descriptor, ScriptContext* scriptContext)
  8155. {
  8156. if (!JavascriptOperators::IsObject(propertySpec))
  8157. {
  8158. return FALSE;
  8159. }
  8160. Var value;
  8161. RecyclableObject* propertySpecObj = RecyclableObject::FromVar(propertySpec);
  8162. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::enumerable, &value, scriptContext))
  8163. {
  8164. descriptor->SetEnumerable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8165. }
  8166. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::configurable, &value, scriptContext))
  8167. {
  8168. descriptor->SetConfigurable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8169. }
  8170. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::value, &value, scriptContext))
  8171. {
  8172. descriptor->SetValue(value);
  8173. }
  8174. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::writable, &value, scriptContext))
  8175. {
  8176. descriptor->SetWritable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8177. }
  8178. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::get, &value, scriptContext))
  8179. {
  8180. if (JavascriptOperators::GetTypeId(value) != TypeIds_Undefined && (false == JavascriptConversion::IsCallable(value)))
  8181. {
  8182. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::get)->GetBuffer());
  8183. }
  8184. descriptor->SetGetter(value);
  8185. }
  8186. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::set, &value, scriptContext))
  8187. {
  8188. if (JavascriptOperators::GetTypeId(value) != TypeIds_Undefined && (false == JavascriptConversion::IsCallable(value)))
  8189. {
  8190. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::set)->GetBuffer());
  8191. }
  8192. descriptor->SetSetter(value);
  8193. }
  8194. return TRUE;
  8195. }
  8196. BOOL JavascriptOperators::ToPropertyDescriptor(Var propertySpec, PropertyDescriptor* descriptor, ScriptContext* scriptContext)
  8197. {
  8198. if (JavascriptProxy::Is(propertySpec) || (
  8199. RecyclableObject::Is(propertySpec) &&
  8200. JavascriptOperators::CheckIfPrototypeChainContainsProxyObject(RecyclableObject::FromVar(propertySpec)->GetPrototype())))
  8201. {
  8202. if (ToPropertyDescriptorForProxyObjects(propertySpec, descriptor, scriptContext) == FALSE)
  8203. {
  8204. return FALSE;
  8205. }
  8206. }
  8207. else
  8208. {
  8209. if (ToPropertyDescriptorForGenericObjects(propertySpec, descriptor, scriptContext) == FALSE)
  8210. {
  8211. return FALSE;
  8212. }
  8213. }
  8214. if (descriptor->GetterSpecified() || descriptor->SetterSpecified())
  8215. {
  8216. if (descriptor->ValueSpecified())
  8217. {
  8218. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotHaveAccessorsAndValue);
  8219. }
  8220. if (descriptor->WritableSpecified())
  8221. {
  8222. int32 hCode = descriptor->IsWritable() ? JSERR_InvalidAttributeTrue : JSERR_InvalidAttributeFalse;
  8223. JavascriptError::ThrowTypeError(scriptContext, hCode, _u("writable"));
  8224. }
  8225. }
  8226. descriptor->SetOriginal(propertySpec);
  8227. return TRUE;
  8228. }
  8229. void JavascriptOperators::CompletePropertyDescriptor(PropertyDescriptor* resultDescriptor, PropertyDescriptor* likeDescriptor, ScriptContext* requestContext)
  8230. {
  8231. const PropertyDescriptor* likePropertyDescriptor = likeDescriptor;
  8232. // 1. Assert: LikeDesc is either a Property Descriptor or undefined.
  8233. // 2. ReturnIfAbrupt(Desc).
  8234. // 3. Assert : Desc is a Property Descriptor
  8235. // 4. If LikeDesc is undefined, then set LikeDesc to Record{ [[Value]]: undefined, [[Writable]] : false, [[Get]] : undefined, [[Set]] : undefined, [[Enumerable]] : false, [[Configurable]] : false }.
  8236. if (likePropertyDescriptor == nullptr)
  8237. {
  8238. likePropertyDescriptor = requestContext->GetLibrary()->GetDefaultPropertyDescriptor();
  8239. }
  8240. // 5. If either IsGenericDescriptor(Desc) or IsDataDescriptor(Desc) is true, then
  8241. if (resultDescriptor->IsDataDescriptor() || resultDescriptor->IsGenericDescriptor())
  8242. {
  8243. // a.If Desc does not have a[[Value]] field, then set Desc.[[Value]] to LikeDesc.[[Value]].
  8244. // b.If Desc does not have a[[Writable]] field, then set Desc.[[Writable]] to LikeDesc.[[Writable]].
  8245. if (!resultDescriptor->ValueSpecified())
  8246. {
  8247. resultDescriptor->SetValue(likePropertyDescriptor->GetValue());
  8248. }
  8249. if (!resultDescriptor->WritableSpecified())
  8250. {
  8251. resultDescriptor->SetWritable(likePropertyDescriptor->IsWritable());
  8252. }
  8253. }
  8254. else
  8255. {
  8256. // 6. Else,
  8257. // a.If Desc does not have a[[Get]] field, then set Desc.[[Get]] to LikeDesc.[[Get]].
  8258. // b.If Desc does not have a[[Set]] field, then set Desc.[[Set]] to LikeDesc.[[Set]].
  8259. if (!resultDescriptor->GetterSpecified())
  8260. {
  8261. resultDescriptor->SetGetter(likePropertyDescriptor->GetGetter());
  8262. }
  8263. if (!resultDescriptor->SetterSpecified())
  8264. {
  8265. resultDescriptor->SetSetter(likePropertyDescriptor->GetSetter());
  8266. }
  8267. }
  8268. // 7. If Desc does not have an[[Enumerable]] field, then set Desc.[[Enumerable]] to LikeDesc.[[Enumerable]].
  8269. // 8. If Desc does not have a[[Configurable]] field, then set Desc.[[Configurable]] to LikeDesc.[[Configurable]].
  8270. // 9. Return Desc.
  8271. if (!resultDescriptor->EnumerableSpecified())
  8272. {
  8273. resultDescriptor->SetEnumerable(likePropertyDescriptor->IsEnumerable());
  8274. }
  8275. if (!resultDescriptor->ConfigurableSpecified())
  8276. {
  8277. resultDescriptor->SetConfigurable(likePropertyDescriptor->IsConfigurable());
  8278. }
  8279. }
  8280. // Conformance to: ES5 8.6.1.
  8281. // Set attributes on the object as provided by property descriptor.
  8282. // If force parameter is true, we force SetAttributes call even if none of the attributes are defined by the descriptor.
  8283. // NOTE: does not set [[Get]], [Set]], [[Value]]
  8284. void JavascriptOperators::SetAttributes(RecyclableObject* object, PropertyId propId, const PropertyDescriptor& descriptor, bool force)
  8285. {
  8286. Assert(object);
  8287. BOOL isWritable = FALSE;
  8288. if (descriptor.IsDataDescriptor())
  8289. {
  8290. isWritable = descriptor.WritableSpecified() ? descriptor.IsWritable() : FALSE;
  8291. }
  8292. else if (descriptor.IsAccessorDescriptor())
  8293. {
  8294. // The reason is that JavascriptOperators::OP_SetProperty checks for RecyclableObject::FromVar(instance)->IsWritableOrAccessor(propertyId),
  8295. // which should in fact check for 'is writable or accessor' but since there is no GetAttributes, we can't do that efficiently.
  8296. isWritable = TRUE;
  8297. }
  8298. // CONSIDER: call object->SetAttributes which is much more efficient as that's 1 call instead of 3.
  8299. // Can't do that now as object->SetAttributes doesn't provide a way which attributes to modify and which not.
  8300. if (force || descriptor.ConfigurableSpecified())
  8301. {
  8302. object->SetConfigurable(propId, descriptor.ConfigurableSpecified() ? descriptor.IsConfigurable() : FALSE);
  8303. }
  8304. if (force || descriptor.EnumerableSpecified())
  8305. {
  8306. object->SetEnumerable(propId, descriptor.EnumerableSpecified() ? descriptor.IsEnumerable() : FALSE);
  8307. }
  8308. if (force || descriptor.WritableSpecified() || isWritable)
  8309. {
  8310. object->SetWritable(propId, isWritable);
  8311. }
  8312. }
  8313. void JavascriptOperators::OP_ClearAttributes(Var instance, PropertyId propertyId)
  8314. {
  8315. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_ClearAttributes);
  8316. Assert(instance);
  8317. if (RecyclableObject::Is(instance))
  8318. {
  8319. RecyclableObject* obj = RecyclableObject::FromVar(instance);
  8320. obj->SetAttributes(propertyId, PropertyNone);
  8321. }
  8322. JIT_HELPER_END(OP_ClearAttributes);
  8323. }
  8324. BOOL JavascriptOperators::Reject(bool throwOnError, ScriptContext* scriptContext, int32 errorCode, PropertyId propertyId)
  8325. {
  8326. Assert(scriptContext);
  8327. if (throwOnError)
  8328. {
  8329. JavascriptError::ThrowTypeError(scriptContext, errorCode, scriptContext->GetThreadContext()->GetPropertyName(propertyId)->GetBuffer());
  8330. }
  8331. return FALSE;
  8332. }
  8333. bool JavascriptOperators::AreSamePropertyDescriptors(const PropertyDescriptor* x, const PropertyDescriptor* y, ScriptContext* scriptContext)
  8334. {
  8335. Assert(scriptContext);
  8336. if (x->ConfigurableSpecified() != y->ConfigurableSpecified() || x->IsConfigurable() != y->IsConfigurable() ||
  8337. x->EnumerableSpecified() != y->EnumerableSpecified() || x->IsEnumerable() != y->IsEnumerable())
  8338. {
  8339. return false;
  8340. }
  8341. if (x->IsDataDescriptor())
  8342. {
  8343. if (!y->IsDataDescriptor() || x->WritableSpecified() != y->WritableSpecified() || x->IsWritable() != y->IsWritable())
  8344. {
  8345. return false;
  8346. }
  8347. if (x->ValueSpecified())
  8348. {
  8349. if (!y->ValueSpecified() || !JavascriptConversion::SameValue(x->GetValue(), y->GetValue()))
  8350. {
  8351. return false;
  8352. }
  8353. }
  8354. }
  8355. else if (x->IsAccessorDescriptor())
  8356. {
  8357. if (!y->IsAccessorDescriptor())
  8358. {
  8359. return false;
  8360. }
  8361. if (x->GetterSpecified())
  8362. {
  8363. if (!y->GetterSpecified() || !JavascriptConversion::SameValue(
  8364. JavascriptOperators::CanonicalizeAccessor(x->GetGetter(), scriptContext),
  8365. JavascriptOperators::CanonicalizeAccessor(y->GetGetter(), scriptContext)))
  8366. {
  8367. return false;
  8368. }
  8369. }
  8370. if (x->SetterSpecified())
  8371. {
  8372. if (!y->SetterSpecified() || !JavascriptConversion::SameValue(
  8373. JavascriptOperators::CanonicalizeAccessor(x->GetSetter(), scriptContext),
  8374. JavascriptOperators::CanonicalizeAccessor(y->GetSetter(), scriptContext)))
  8375. {
  8376. return false;
  8377. }
  8378. }
  8379. }
  8380. return true;
  8381. }
  8382. // Check if an accessor is undefined (null or defaultAccessor)
  8383. bool JavascriptOperators::IsUndefinedAccessor(Var accessor, ScriptContext* scriptContext)
  8384. {
  8385. return nullptr == accessor || scriptContext->GetLibrary()->GetDefaultAccessorFunction() == accessor;
  8386. }
  8387. // Converts default accessor to undefined.
  8388. // Can be used when comparing accessors.
  8389. Var JavascriptOperators::CanonicalizeAccessor(Var accessor, ScriptContext* scriptContext)
  8390. {
  8391. Assert(scriptContext);
  8392. if (IsUndefinedAccessor(accessor, scriptContext))
  8393. {
  8394. return scriptContext->GetLibrary()->GetUndefined();
  8395. }
  8396. return accessor;
  8397. }
  8398. Var JavascriptOperators::DefaultAccessor(RecyclableObject* function, CallInfo callInfo, ...)
  8399. {
  8400. return function->GetLibrary()->GetUndefined();
  8401. }
  8402. void FrameDisplay::SetItem(uint index, void* item)
  8403. {
  8404. AssertMsg(index < this->length, "Invalid frame display access");
  8405. scopes[index] = item;
  8406. }
  8407. void *FrameDisplay::GetItem(uint index)
  8408. {
  8409. AssertMsg(index < this->length, "Invalid frame display access");
  8410. return scopes[index];
  8411. }
  8412. // Grab the "this" pointer, mapping a root object to its associated host object.
  8413. Var JavascriptOperators::RootToThisObject(const Var object, ScriptContext* scriptContext)
  8414. {
  8415. Js::Var thisVar = object;
  8416. TypeId typeId = Js::JavascriptOperators::GetTypeId(thisVar);
  8417. switch (typeId)
  8418. {
  8419. case Js::TypeIds_GlobalObject:
  8420. return ((Js::GlobalObject*)thisVar)->ToThis();
  8421. case Js::TypeIds_ModuleRoot:
  8422. return Js::JavascriptOperators::GetThisFromModuleRoot(thisVar);
  8423. default:
  8424. if (typeId == scriptContext->GetDirectHostTypeId())
  8425. {
  8426. return ((RecyclableObject*)thisVar)->GetLibrary()->GetGlobalObject()->ToThis();
  8427. }
  8428. }
  8429. return thisVar;
  8430. }
  8431. Var JavascriptOperators::CallGetter(RecyclableObject * const function, Var const object, ScriptContext * requestContext)
  8432. {
  8433. #if ENABLE_TTD
  8434. if(function->GetScriptContext()->ShouldSuppressGetterInvocationForDebuggerEvaluation())
  8435. {
  8436. return requestContext->GetLibrary()->GetUndefined();
  8437. }
  8438. #endif
  8439. ScriptContext * scriptContext = function->GetScriptContext();
  8440. ThreadContext * threadContext = scriptContext->GetThreadContext();
  8441. return threadContext->ExecuteImplicitCall(function, ImplicitCall_Accessor, [=]() -> Js::Var
  8442. {
  8443. // Stack object should have a pre-op bail on implicit call. We shouldn't see them here.
  8444. // Stack numbers are ok, as we will call ToObject to wrap it in a number object anyway
  8445. // See JavascriptOperators::GetThisHelper
  8446. Assert(JavascriptOperators::GetTypeId(object) == TypeIds_Integer ||
  8447. JavascriptOperators::GetTypeId(object) == TypeIds_Number ||
  8448. threadContext->HasNoSideEffect(function) ||
  8449. !ThreadContext::IsOnStack(object));
  8450. // Verify that the scriptcontext is alive before firing getter/setter
  8451. if (!scriptContext->VerifyAlive(!function->IsExternal(), requestContext))
  8452. {
  8453. return nullptr;
  8454. }
  8455. CallFlags flags = CallFlags_Value;
  8456. Var thisVar = RootToThisObject(object, scriptContext);
  8457. RecyclableObject* marshalledFunction = RecyclableObject::UnsafeFromVar(
  8458. CrossSite::MarshalVar(requestContext, function, scriptContext));
  8459. Var result = CALL_ENTRYPOINT(threadContext, marshalledFunction->GetEntryPoint(), function, CallInfo(flags, 1), thisVar);
  8460. result = CrossSite::MarshalVar(requestContext, result);
  8461. return result;
  8462. });
  8463. }
  8464. void JavascriptOperators::CallSetter(RecyclableObject * const function, Var const object, Var const value, ScriptContext * requestContext)
  8465. {
  8466. ScriptContext * scriptContext = function->GetScriptContext();
  8467. ThreadContext * threadContext = scriptContext->GetThreadContext();
  8468. threadContext->ExecuteImplicitCall(function, ImplicitCall_Accessor, [=]() -> Js::Var
  8469. {
  8470. // Stack object should have a pre-op bail on implicit call. We shouldn't see them here.
  8471. // Stack numbers are ok, as we will call ToObject to wrap it in a number object anyway
  8472. // See JavascriptOperators::GetThisHelper
  8473. Assert(JavascriptOperators::GetTypeId(object) == TypeIds_Integer ||
  8474. JavascriptOperators::GetTypeId(object) == TypeIds_Number || !ThreadContext::IsOnStack(object));
  8475. // Verify that the scriptcontext is alive before firing getter/setter
  8476. if (!scriptContext->VerifyAlive(!function->IsExternal(), requestContext))
  8477. {
  8478. return nullptr;
  8479. }
  8480. CallFlags flags = CallFlags_Value;
  8481. Var putValue = value;
  8482. // CONSIDER: Have requestContext everywhere, even in the setProperty related codepath.
  8483. if (requestContext)
  8484. {
  8485. putValue = CrossSite::MarshalVar(requestContext, value);
  8486. }
  8487. Var thisVar = RootToThisObject(object, scriptContext);
  8488. RecyclableObject* marshalledFunction = function;
  8489. if (requestContext)
  8490. {
  8491. marshalledFunction = RecyclableObject::UnsafeFromVar(CrossSite::MarshalVar(requestContext, function, function->GetScriptContext()));
  8492. }
  8493. Var result = CALL_ENTRYPOINT(threadContext, marshalledFunction->GetEntryPoint(), function, CallInfo(flags, 2), thisVar, putValue);
  8494. Assert(result);
  8495. return nullptr;
  8496. });
  8497. }
  8498. void * JavascriptOperators::AllocMemForVarArray(size_t size, Recycler* recycler)
  8499. {
  8500. TRACK_ALLOC_INFO(recycler, Js::Var, Recycler, 0, (size_t)(size / sizeof(Js::Var)));
  8501. return recycler->AllocZero(size);
  8502. }
  8503. #if !FLOATVAR
  8504. void * JavascriptOperators::AllocUninitializedNumber(Js::RecyclerJavascriptNumberAllocator * allocator)
  8505. {
  8506. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(AllocUninitializedNumber);
  8507. TRACK_ALLOC_INFO(allocator->GetRecycler(), Js::JavascriptNumber, Recycler, 0, (size_t)-1);
  8508. return allocator->Alloc(sizeof(Js::JavascriptNumber));
  8509. JIT_HELPER_END(AllocUninitializedNumber);
  8510. }
  8511. #endif
  8512. void JavascriptOperators::ScriptAbort()
  8513. {
  8514. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScriptAbort);
  8515. throw ScriptAbortException();
  8516. JIT_HELPER_END(ScriptAbort);
  8517. }
  8518. JavascriptString * JavascriptOperators::Concat3(Var aLeft, Var aCenter, Var aRight, ScriptContext * scriptContext)
  8519. {
  8520. // Make sure we do the conversion in order from left to right
  8521. JavascriptString * strLeft = JavascriptConversion::ToPrimitiveString(aLeft, scriptContext);
  8522. JavascriptString * strCenter = JavascriptConversion::ToPrimitiveString(aCenter, scriptContext);
  8523. JavascriptString * strRight = JavascriptConversion::ToPrimitiveString(aRight, scriptContext);
  8524. return JavascriptString::Concat3(strLeft, strCenter, strRight);
  8525. }
  8526. JavascriptString *
  8527. JavascriptOperators::NewConcatStrMulti(Var a1, Var a2, uint count, ScriptContext * scriptContext)
  8528. {
  8529. // Make sure we do the conversion in order
  8530. JavascriptString * str1 = JavascriptConversion::ToPrimitiveString(a1, scriptContext);
  8531. JavascriptString * str2 = JavascriptConversion::ToPrimitiveString(a2, scriptContext);
  8532. return ConcatStringMulti::New(count, str1, str2, scriptContext);
  8533. }
  8534. void
  8535. JavascriptOperators::SetConcatStrMultiItem(Var concatStr, Var str, uint index, ScriptContext * scriptContext)
  8536. {
  8537. ConcatStringMulti::FromVar(concatStr)->SetItem(index,
  8538. JavascriptConversion::ToPrimitiveString(str, scriptContext));
  8539. }
  8540. void
  8541. JavascriptOperators::SetConcatStrMultiItem2(Var concatStr, Var str1, Var str2, uint index, ScriptContext * scriptContext)
  8542. {
  8543. ConcatStringMulti * cs = ConcatStringMulti::FromVar(concatStr);
  8544. cs->SetItem(index, JavascriptConversion::ToPrimitiveString(str1, scriptContext));
  8545. cs->SetItem(index + 1, JavascriptConversion::ToPrimitiveString(str2, scriptContext));
  8546. }
  8547. void JavascriptOperators::OP_SetComputedNameVar(Var method, Var computedNameVar)
  8548. {
  8549. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(SetComputedNameVar);
  8550. ScriptFunctionBase *scriptFunction = ScriptFunctionBase::FromVar(method);
  8551. scriptFunction->SetComputedNameVar(computedNameVar);
  8552. JIT_HELPER_END(SetComputedNameVar);
  8553. }
  8554. void JavascriptOperators::OP_SetHomeObj(Var method, Var homeObj)
  8555. {
  8556. ScriptFunctionBase *scriptFunction = ScriptFunctionBase::FromVar(method);
  8557. JIT_HELPER_NOT_REENTRANT_HEADER(SetHomeObj, reentrancylock, scriptFunction->GetScriptContext()->GetThreadContext());
  8558. scriptFunction->SetHomeObj(homeObj);
  8559. JIT_HELPER_END(SetHomeObj);
  8560. }
  8561. Var JavascriptOperators::OP_LdHomeObj(Var scriptFunction, ScriptContext * scriptContext)
  8562. {
  8563. JIT_HELPER_NOT_REENTRANT_HEADER(LdHomeObj, reentrancylock, scriptContext->GetThreadContext());
  8564. // Ensure this is not a stack ScriptFunction
  8565. if (!ScriptFunction::Is(scriptFunction) || ThreadContext::IsOnStack(scriptFunction))
  8566. {
  8567. return scriptContext->GetLibrary()->GetUndefined();
  8568. }
  8569. ScriptFunction *instance = ScriptFunction::UnsafeFromVar(scriptFunction);
  8570. // We keep a reference to the current class rather than its super prototype
  8571. // since the prototype could change.
  8572. Var homeObj = instance->GetHomeObj();
  8573. return (homeObj != nullptr) ? homeObj : scriptContext->GetLibrary()->GetUndefined();
  8574. JIT_HELPER_END(LdHomeObj);
  8575. }
  8576. Var JavascriptOperators::OP_LdHomeObjProto(Var homeObj, ScriptContext* scriptContext)
  8577. {
  8578. JIT_HELPER_NOT_REENTRANT_HEADER(LdHomeObjProto, reentrancylock, scriptContext->GetThreadContext());
  8579. if (homeObj == nullptr || !RecyclableObject::Is(homeObj))
  8580. {
  8581. return scriptContext->GetLibrary()->GetUndefined();
  8582. }
  8583. RecyclableObject *thisObjPrototype = RecyclableObject::FromVar(homeObj);
  8584. TypeId typeId = thisObjPrototype->GetTypeId();
  8585. if (typeId == TypeIds_Null || typeId == TypeIds_Undefined)
  8586. {
  8587. JavascriptError::ThrowReferenceError(scriptContext, JSERR_BadSuperReference);
  8588. }
  8589. Assert(thisObjPrototype != nullptr);
  8590. RecyclableObject *superBase = thisObjPrototype->GetPrototype();
  8591. if (superBase == nullptr || !RecyclableObject::Is(superBase))
  8592. {
  8593. return scriptContext->GetLibrary()->GetUndefined();
  8594. }
  8595. return superBase;
  8596. JIT_HELPER_END(LdHomeObjProto);
  8597. }
  8598. Var JavascriptOperators::OP_LdFuncObj(Var scriptFunction, ScriptContext * scriptContext)
  8599. {
  8600. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(LdFuncObj);
  8601. // use self as value of [[FunctionObject]] - this is true only for constructors
  8602. Assert(RecyclableObject::Is(scriptFunction));
  8603. return scriptFunction;
  8604. JIT_HELPER_END(LdFuncObj);
  8605. }
  8606. Var JavascriptOperators::OP_LdFuncObjProto(Var funcObj, ScriptContext* scriptContext)
  8607. {
  8608. JIT_HELPER_NOT_REENTRANT_HEADER(LdFuncObjProto, reentrancylock, scriptContext->GetThreadContext());
  8609. RecyclableObject *superCtor = RecyclableObject::FromVar(funcObj)->GetPrototype();
  8610. if (superCtor == nullptr || !IsConstructor(superCtor))
  8611. {
  8612. JavascriptError::ThrowTypeError(scriptContext, JSERR_NotAConstructor);
  8613. }
  8614. return superCtor;
  8615. JIT_HELPER_END(LdFuncObjProto);
  8616. }
  8617. Var JavascriptOperators::OP_ImportCall(__in JavascriptFunction *function, __in Var specifier, __in ScriptContext* scriptContext)
  8618. {
  8619. JIT_HELPER_REENTRANT_HEADER(ImportCall);
  8620. ModuleRecordBase *moduleRecordBase = nullptr;
  8621. SourceTextModuleRecord *moduleRecord = nullptr;
  8622. FunctionBody* parentFuncBody = function->GetFunctionBody();
  8623. JavascriptString *specifierString = nullptr;
  8624. try
  8625. {
  8626. specifierString = JavascriptConversion::ToString(specifier, scriptContext);
  8627. }
  8628. catch (const JavascriptException &err)
  8629. {
  8630. Var errorObject = err.GetAndClear()->GetThrownObject(scriptContext);
  8631. AssertMsg(errorObject != nullptr, "OP_ImportCall: null error object thrown by ToString(specifier)");
  8632. if (errorObject != nullptr)
  8633. {
  8634. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, errorObject, scriptContext);
  8635. }
  8636. Throw::InternalError();
  8637. }
  8638. DWORD_PTR dwReferencingSourceContext = parentFuncBody->GetHostSourceContext();
  8639. if (!parentFuncBody->IsES6ModuleCode() && dwReferencingSourceContext == Js::Constants::NoHostSourceContext)
  8640. {
  8641. // import() called from eval
  8642. if (parentFuncBody->GetUtf8SourceInfo()->GetCallerUtf8SourceInfo() == nullptr)
  8643. {
  8644. JavascriptError *error = scriptContext->GetLibrary()->CreateError();
  8645. JavascriptError::SetErrorMessageProperties(error, E_FAIL, _u("Unable to locate active script or module that calls import()"), scriptContext);
  8646. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, error, scriptContext);
  8647. }
  8648. dwReferencingSourceContext = parentFuncBody->GetUtf8SourceInfo()->GetCallerUtf8SourceInfo()->GetSourceContextInfo()->dwHostSourceContext;
  8649. if (dwReferencingSourceContext == Js::Constants::NoHostSourceContext)
  8650. {
  8651. // Walk the call stack if caller function is neither module code nor having host source context
  8652. JavascriptFunction* caller = nullptr;
  8653. Js::JavascriptStackWalker walker(scriptContext);
  8654. walker.GetCaller(&caller);
  8655. do
  8656. {
  8657. if (walker.GetCaller(&caller) && caller != nullptr && caller->IsScriptFunction())
  8658. {
  8659. parentFuncBody = caller->GetFunctionBody();
  8660. dwReferencingSourceContext = parentFuncBody->GetHostSourceContext();
  8661. }
  8662. else
  8663. {
  8664. JavascriptError *error = scriptContext->GetLibrary()->CreateError();
  8665. JavascriptError::SetErrorMessageProperties(error, E_FAIL, _u("Unable to locate active script or module that calls import()"), scriptContext);
  8666. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, error, scriptContext);
  8667. }
  8668. } while (!parentFuncBody->IsES6ModuleCode() && dwReferencingSourceContext == Js::Constants::NoHostSourceContext);
  8669. }
  8670. }
  8671. LPCOLESTR moduleName = specifierString->GetSz();
  8672. HRESULT hr = 0;
  8673. if (parentFuncBody->IsES6ModuleCode())
  8674. {
  8675. SourceTextModuleRecord *referenceModuleRecord = parentFuncBody->GetScriptContext()->GetLibrary()->GetModuleRecord(parentFuncBody->GetModuleID());
  8676. BEGIN_LEAVE_SCRIPT(scriptContext);
  8677. BEGIN_TRANSLATE_TO_HRESULT(static_cast<ExceptionType>(ExceptionType_OutOfMemory | ExceptionType_StackOverflow));
  8678. hr = scriptContext->GetHostScriptContext()->FetchImportedModule(referenceModuleRecord, moduleName, &moduleRecordBase);
  8679. END_TRANSLATE_EXCEPTION_TO_HRESULT(hr);
  8680. END_LEAVE_SCRIPT(scriptContext);
  8681. }
  8682. else
  8683. {
  8684. Assert(dwReferencingSourceContext != Js::Constants::NoHostSourceContext);
  8685. BEGIN_LEAVE_SCRIPT(scriptContext);
  8686. BEGIN_TRANSLATE_TO_HRESULT(static_cast<ExceptionType>(ExceptionType_OutOfMemory | ExceptionType_StackOverflow));
  8687. hr = scriptContext->GetHostScriptContext()->FetchImportedModuleFromScript(dwReferencingSourceContext, moduleName, &moduleRecordBase);
  8688. END_TRANSLATE_EXCEPTION_TO_HRESULT(hr);
  8689. END_LEAVE_SCRIPT(scriptContext);
  8690. }
  8691. if (FAILED(hr))
  8692. {
  8693. // We cannot just use the buffer in the specifier string - need to make a copy here.
  8694. size_t length = wcslen(moduleName);
  8695. char16* allocatedString = RecyclerNewArrayLeaf(scriptContext->GetRecycler(), char16, length + 1);
  8696. wmemcpy_s(allocatedString, length + 1, moduleName, length);
  8697. allocatedString[length] = _u('\0');
  8698. Js::JavascriptError *error = scriptContext->GetLibrary()->CreateURIError();
  8699. JavascriptError::SetErrorMessageProperties(error, hr, allocatedString, scriptContext);
  8700. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, error, scriptContext);
  8701. }
  8702. moduleRecord = SourceTextModuleRecord::FromHost(moduleRecordBase);
  8703. if (moduleRecord->GetErrorObject() != nullptr)
  8704. {
  8705. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, moduleRecord->GetErrorObject(), scriptContext, moduleRecord);
  8706. }
  8707. else if (moduleRecord->WasEvaluated())
  8708. {
  8709. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(true, moduleRecord->GetNamespace(), scriptContext, moduleRecord);
  8710. }
  8711. return moduleRecord->PostProcessDynamicModuleImport();
  8712. JIT_HELPER_END(ImportCall);
  8713. }
  8714. Var JavascriptOperators::OP_ResumeYield(ResumeYieldData* yieldData, RecyclableObject* iterator)
  8715. {
  8716. JIT_HELPER_REENTRANT_HEADER(ResumeYield);
  8717. bool isNext = yieldData->exceptionObj == nullptr;
  8718. bool isThrow = !isNext && !yieldData->exceptionObj->IsGeneratorReturnException();
  8719. if (iterator != nullptr) // yield*
  8720. {
  8721. ScriptContext* scriptContext = iterator->GetScriptContext();
  8722. PropertyId propertyId = isNext ? PropertyIds::next : isThrow ? PropertyIds::throw_ : PropertyIds::return_;
  8723. Var prop = JavascriptOperators::GetProperty(iterator, propertyId, scriptContext);
  8724. if (!isNext && JavascriptOperators::IsUndefinedOrNull(prop))
  8725. {
  8726. if (isThrow)
  8727. {
  8728. // 5.b.iii.2
  8729. // NOTE: If iterator does not have a throw method, this throw is going to terminate the yield* loop.
  8730. // But first we need to give iterator a chance to clean up.
  8731. prop = JavascriptOperators::GetProperty(iterator, PropertyIds::return_, scriptContext);
  8732. if (!JavascriptOperators::IsUndefinedOrNull(prop))
  8733. {
  8734. if (!JavascriptConversion::IsCallable(prop))
  8735. {
  8736. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, _u("return"));
  8737. }
  8738. Var result = nullptr;
  8739. RecyclableObject* method = RecyclableObject::FromVar(prop);
  8740. BEGIN_SAFE_REENTRANT_CALL(scriptContext->GetThreadContext())
  8741. {
  8742. Var args[] = { iterator, yieldData->data };
  8743. CallInfo callInfo(CallFlags_Value, _countof(args));
  8744. result = JavascriptFunction::CallFunction<true>(method, method->GetEntryPoint(), Arguments(callInfo, args));
  8745. }
  8746. END_SAFE_REENTRANT_CALL
  8747. if (!JavascriptOperators::IsObject(result))
  8748. {
  8749. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  8750. }
  8751. }
  8752. // 5.b.iii.3
  8753. // NOTE: The next step throws a TypeError to indicate that there was a yield* protocol violation:
  8754. // iterator does not have a throw method.
  8755. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, _u("throw"));
  8756. }
  8757. // Do not use ThrowExceptionObject for return() API exceptions since these exceptions are not real exceptions
  8758. JavascriptExceptionOperators::DoThrow(yieldData->exceptionObj, scriptContext);
  8759. }
  8760. if (!JavascriptConversion::IsCallable(prop))
  8761. {
  8762. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, isNext ? _u("next") : isThrow ? _u("throw") : _u("return"));
  8763. }
  8764. RecyclableObject* method = RecyclableObject::FromVar(prop);
  8765. Var result = scriptContext->GetThreadContext()->ExecuteImplicitCall(method, Js::ImplicitCall_Accessor, [=]()->Js::Var
  8766. {
  8767. Var args[] = { iterator, yieldData->data };
  8768. CallInfo callInfo(CallFlags_Value, _countof(args));
  8769. return JavascriptFunction::CallFunction<true>(method, method->GetEntryPoint(), Arguments(callInfo, args));
  8770. });
  8771. if (!JavascriptOperators::IsObject(result))
  8772. {
  8773. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  8774. }
  8775. if (isThrow || isNext)
  8776. {
  8777. // 5.b.ii.2
  8778. // NOTE: Exceptions from the inner iterator throw method are propagated.
  8779. // Normal completions from an inner throw method are processed similarly to an inner next.
  8780. return result;
  8781. }
  8782. RecyclableObject* obj = RecyclableObject::FromVar(result);
  8783. Var done = JavascriptOperators::GetProperty(obj, PropertyIds::done, scriptContext);
  8784. if (done == iterator->GetLibrary()->GetTrue())
  8785. {
  8786. Var value = JavascriptOperators::GetProperty(obj, PropertyIds::value, scriptContext);
  8787. yieldData->exceptionObj->SetThrownObject(value);
  8788. // Do not use ThrowExceptionObject for return() API exceptions since these exceptions are not real exceptions
  8789. JavascriptExceptionOperators::DoThrow(yieldData->exceptionObj, scriptContext);
  8790. }
  8791. return result;
  8792. }
  8793. // CONSIDER: Fast path this early out return path in JITed code before helper call to avoid the helper call overhead in the common case e.g. next() calls.
  8794. if (isNext)
  8795. {
  8796. return yieldData->data;
  8797. }
  8798. if (isThrow)
  8799. {
  8800. // Use ThrowExceptionObject() to get debugger support for breaking on throw
  8801. JavascriptExceptionOperators::ThrowExceptionObject(yieldData->exceptionObj, yieldData->exceptionObj->GetScriptContext(), true);
  8802. }
  8803. // CONSIDER: Using an exception to carry the return value and force finally code to execute is a bit of a janky
  8804. // solution since we have to override the value here in the case of yield* expressions. It works but is there
  8805. // a more elegant way?
  8806. //
  8807. // Instead what if ResumeYield was a "set Dst then optionally branch" opcode, that could also throw? Then we could
  8808. // avoid using a special exception entirely with byte code something like this:
  8809. //
  8810. // ;; Ry is the yieldData
  8811. //
  8812. // ResumeYield Rx Ry $returnPathLabel
  8813. // ... code like normal
  8814. // $returnPathLabel:
  8815. // Ld_A R0 Rx
  8816. // Br $exitFinallyAndReturn
  8817. //
  8818. // This would probably give better performance for the common case of calling next() on generators since we wouldn't
  8819. // have to wrap the call to the generator code in a try catch.
  8820. // Do not use ThrowExceptionObject for return() API exceptions since these exceptions are not real exceptions
  8821. JavascriptExceptionOperators::DoThrow(yieldData->exceptionObj, yieldData->exceptionObj->GetScriptContext());
  8822. JIT_HELPER_END(ResumeYield);
  8823. }
  8824. Js::Var
  8825. JavascriptOperators::BoxStackInstance(Js::Var instance, ScriptContext * scriptContext, bool allowStackFunction, bool deepCopy)
  8826. {
  8827. if (!ThreadContext::IsOnStack(instance) || (allowStackFunction && !TaggedNumber::Is(instance) && (*(int*)instance & 1)))
  8828. {
  8829. return instance;
  8830. }
  8831. TypeId typeId = JavascriptOperators::GetTypeId(instance);
  8832. switch (typeId)
  8833. {
  8834. case Js::TypeIds_Number:
  8835. #if !FLOATVAR
  8836. return JavascriptNumber::BoxStackInstance(instance, scriptContext);
  8837. #endif
  8838. // fall-through
  8839. case Js::TypeIds_Integer:
  8840. return instance;
  8841. case Js::TypeIds_RegEx:
  8842. return JavascriptRegExp::BoxStackInstance(JavascriptRegExp::FromVar(instance), deepCopy);
  8843. case Js::TypeIds_Object:
  8844. return DynamicObject::BoxStackInstance(DynamicObject::FromVar(instance), deepCopy);
  8845. case Js::TypeIds_Array:
  8846. return JavascriptArray::BoxStackInstance(JavascriptArray::UnsafeFromVar(instance), deepCopy);
  8847. case Js::TypeIds_NativeIntArray:
  8848. return JavascriptNativeIntArray::BoxStackInstance(JavascriptNativeIntArray::UnsafeFromVar(instance), deepCopy);
  8849. case Js::TypeIds_NativeFloatArray:
  8850. return JavascriptNativeFloatArray::BoxStackInstance(JavascriptNativeFloatArray::UnsafeFromVar(instance), deepCopy);
  8851. case Js::TypeIds_Function:
  8852. Assert(allowStackFunction);
  8853. // Stack functions are deal with not mar mark them, but by nested function escape analysis
  8854. // in the front end. No need to box here.
  8855. return instance;
  8856. #if ENABLE_COPYONACCESS_ARRAY
  8857. case Js::TypeIds_CopyOnAccessNativeIntArray:
  8858. Assert(false);
  8859. // fall-through
  8860. #endif
  8861. default:
  8862. Assert(false);
  8863. return instance;
  8864. };
  8865. }
  8866. ImplicitCallFlags
  8867. JavascriptOperators::CacheAndClearImplicitBit(ScriptContext* scriptContext)
  8868. {
  8869. ImplicitCallFlags prevImplicitCallFlags = scriptContext->GetThreadContext()->GetImplicitCallFlags();
  8870. scriptContext->GetThreadContext()->ClearImplicitCallFlags();
  8871. return prevImplicitCallFlags;
  8872. }
  8873. ImplicitCallFlags
  8874. JavascriptOperators::CheckAndUpdateFunctionBodyWithImplicitFlag(FunctionBody* functionBody)
  8875. {
  8876. ScriptContext* scriptContext = functionBody->GetScriptContext();
  8877. ImplicitCallFlags currImplicitCallFlags = scriptContext->GetThreadContext()->GetImplicitCallFlags();
  8878. if ((currImplicitCallFlags > ImplicitCall_None))
  8879. {
  8880. functionBody->SetHasOnlyThisStmts(false);
  8881. }
  8882. return currImplicitCallFlags;
  8883. }
  8884. void
  8885. JavascriptOperators::RestoreImplicitFlag(ScriptContext* scriptContext, ImplicitCallFlags prevImplicitCallFlags, ImplicitCallFlags currImplicitCallFlags)
  8886. {
  8887. scriptContext->GetThreadContext()->SetImplicitCallFlags((ImplicitCallFlags)(prevImplicitCallFlags | currImplicitCallFlags));
  8888. }
  8889. FunctionProxy*
  8890. JavascriptOperators::GetDeferredDeserializedFunctionProxy(JavascriptFunction* func)
  8891. {
  8892. FunctionProxy* proxy = func->GetFunctionProxy();
  8893. Assert(proxy->GetFunctionInfo()->GetFunctionProxy() != proxy);
  8894. return proxy;
  8895. }
  8896. template <>
  8897. Js::Var JavascriptOperators::GetElementAtIndex(Js::JavascriptArray* arrayObject, UINT index, Js::ScriptContext* scriptContext)
  8898. {
  8899. Js::Var result;
  8900. if (Js::JavascriptOperators::OP_GetElementI_ArrayFastPath(arrayObject, index, &result, scriptContext))
  8901. {
  8902. return result;
  8903. }
  8904. return scriptContext->GetMissingItemResult();
  8905. }
  8906. template<>
  8907. Js::Var JavascriptOperators::GetElementAtIndex(Js::JavascriptNativeIntArray* arrayObject, UINT index, Js::ScriptContext* scriptContext)
  8908. {
  8909. Js::Var result;
  8910. if (Js::JavascriptOperators::OP_GetElementI_ArrayFastPath(arrayObject, index, &result, scriptContext))
  8911. {
  8912. return result;
  8913. }
  8914. return scriptContext->GetMissingItemResult();
  8915. }
  8916. template<>
  8917. Js::Var JavascriptOperators::GetElementAtIndex(Js::JavascriptNativeFloatArray* arrayObject, UINT index, Js::ScriptContext* scriptContext)
  8918. {
  8919. Js::Var result;
  8920. if (Js::JavascriptOperators::OP_GetElementI_ArrayFastPath(arrayObject, index, &result, scriptContext))
  8921. {
  8922. return result;
  8923. }
  8924. return scriptContext->GetMissingItemResult();
  8925. }
  8926. template<>
  8927. Js::Var JavascriptOperators::GetElementAtIndex(Js::Var* arrayObject, UINT index, Js::ScriptContext* scriptContext)
  8928. {
  8929. return Js::JavascriptOperators::OP_GetElementI_Int32(*arrayObject, index, scriptContext);
  8930. }
  8931. template<typename T>
  8932. void JavascriptOperators::ObjectToNativeArray(T* arrayObject,
  8933. JsNativeValueType valueType,
  8934. __in UINT length,
  8935. __in UINT elementSize,
  8936. __out_bcount(length*elementSize) byte* buffer,
  8937. Js::ScriptContext* scriptContext)
  8938. {
  8939. Var element;
  8940. uint64 allocSize = UInt32Math::Mul(length, elementSize);
  8941. // TODO:further fast path the call for things like IntArray convert to int, floatarray convert to float etc.
  8942. // such that we don't need boxing.
  8943. switch (valueType)
  8944. {
  8945. case JsInt8Type:
  8946. AnalysisAssert(elementSize == sizeof(int8));
  8947. for (UINT i = 0; i < length; i++)
  8948. {
  8949. element = GetElementAtIndex(arrayObject, i, scriptContext);
  8950. AnalysisAssert((i + 1) * sizeof(int8) <= allocSize);
  8951. #pragma prefast(suppress:22102)
  8952. ((int8*)buffer)[i] = Js::JavascriptConversion::ToInt8(element, scriptContext);
  8953. }
  8954. break;
  8955. case JsUint8Type:
  8956. AnalysisAssert(elementSize == sizeof(uint8));
  8957. for (UINT i = 0; i < length; i++)
  8958. {
  8959. element = GetElementAtIndex(arrayObject, i, scriptContext);
  8960. AnalysisAssert((i + 1) * sizeof(uint8) <= allocSize);
  8961. ((uint8*)buffer)[i] = Js::JavascriptConversion::ToUInt8(element, scriptContext);
  8962. }
  8963. break;
  8964. case JsInt16Type:
  8965. AnalysisAssert(elementSize == sizeof(int16));
  8966. for (UINT i = 0; i < length; i++)
  8967. {
  8968. element = GetElementAtIndex(arrayObject, i, scriptContext);
  8969. AnalysisAssert((i + 1) * sizeof(int16) <= allocSize);
  8970. ((int16*)buffer)[i] = Js::JavascriptConversion::ToInt16(element, scriptContext);
  8971. }
  8972. break;
  8973. case JsUint16Type:
  8974. AnalysisAssert(elementSize == sizeof(uint16));
  8975. for (UINT i = 0; i < length; i++)
  8976. {
  8977. element = GetElementAtIndex(arrayObject, i, scriptContext);
  8978. AnalysisAssert((i + 1) * sizeof(uint16) <= allocSize);
  8979. ((uint16*)buffer)[i] = Js::JavascriptConversion::ToUInt16(element, scriptContext);
  8980. }
  8981. break;
  8982. case JsInt32Type:
  8983. AnalysisAssert(elementSize == sizeof(int32));
  8984. for (UINT i = 0; i < length; i++)
  8985. {
  8986. element = GetElementAtIndex(arrayObject, i, scriptContext);
  8987. AnalysisAssert((i + 1) * sizeof(int32) <= allocSize);
  8988. ((int32*)buffer)[i] = Js::JavascriptConversion::ToInt32(element, scriptContext);
  8989. }
  8990. break;
  8991. case JsUint32Type:
  8992. AnalysisAssert(elementSize == sizeof(uint32));
  8993. for (UINT i = 0; i < length; i++)
  8994. {
  8995. element = GetElementAtIndex(arrayObject, i, scriptContext);
  8996. AnalysisAssert((i + 1) * sizeof(uint32) <= allocSize);
  8997. ((uint32*)buffer)[i] = Js::JavascriptConversion::ToUInt32(element, scriptContext);
  8998. }
  8999. break;
  9000. case JsInt64Type:
  9001. AnalysisAssert(elementSize == sizeof(int64));
  9002. for (UINT i = 0; i < length; i++)
  9003. {
  9004. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9005. AnalysisAssert((i + 1) * sizeof(int64) <= allocSize);
  9006. ((int64*)buffer)[i] = Js::JavascriptConversion::ToInt64(element, scriptContext);
  9007. }
  9008. break;
  9009. case JsUint64Type:
  9010. AnalysisAssert(elementSize == sizeof(uint64));
  9011. for (UINT i = 0; i < length; i++)
  9012. {
  9013. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9014. AnalysisAssert((i + 1) * sizeof(uint64) <= allocSize);
  9015. ((uint64*)buffer)[i] = Js::JavascriptConversion::ToUInt64(element, scriptContext);
  9016. }
  9017. break;
  9018. case JsFloatType:
  9019. AnalysisAssert(elementSize == sizeof(float));
  9020. for (UINT i = 0; i < length; i++)
  9021. {
  9022. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9023. AnalysisAssert((i + 1) * sizeof(float) <= allocSize);
  9024. ((float*)buffer)[i] = Js::JavascriptConversion::ToFloat(element, scriptContext);
  9025. }
  9026. break;
  9027. case JsDoubleType:
  9028. AnalysisAssert(elementSize == sizeof(double));
  9029. for (UINT i = 0; i < length; i++)
  9030. {
  9031. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9032. AnalysisAssert((i + 1) * sizeof(double) <= allocSize);
  9033. ((double*)buffer)[i] = Js::JavascriptConversion::ToNumber(element, scriptContext);
  9034. }
  9035. break;
  9036. case JsNativeStringType:
  9037. AnalysisAssert(elementSize == sizeof(JsNativeString));
  9038. for (UINT i = 0; i < length; i++)
  9039. {
  9040. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9041. AnalysisAssert((i + 1) * sizeof(JsNativeString) <= allocSize);
  9042. Js::JavascriptString* string = Js::JavascriptConversion::ToString(element, scriptContext);
  9043. (((JsNativeString*)buffer)[i]).str = string->GetSz();
  9044. (((JsNativeString*)buffer)[i]).length = string->GetLength();
  9045. }
  9046. break;
  9047. default:
  9048. Assert(FALSE);
  9049. }
  9050. }
  9051. void JavascriptOperators::VarToNativeArray(Var arrayObject,
  9052. JsNativeValueType valueType,
  9053. __in UINT length,
  9054. __in UINT elementSize,
  9055. __out_bcount(length*elementSize) byte* buffer,
  9056. Js::ScriptContext* scriptContext)
  9057. {
  9058. Js::DynamicObject* dynamicObject = DynamicObject::FromVar(arrayObject);
  9059. if (dynamicObject->IsCrossSiteObject() || Js::TaggedInt::IsOverflow(length))
  9060. {
  9061. Js::JavascriptOperators::ObjectToNativeArray(&arrayObject, valueType, length, elementSize, buffer, scriptContext);
  9062. }
  9063. else
  9064. {
  9065. #if ENABLE_COPYONACCESS_ARRAY
  9066. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(arrayObject);
  9067. #endif
  9068. switch (Js::JavascriptOperators::GetTypeId(arrayObject))
  9069. {
  9070. case TypeIds_Array:
  9071. Js::JavascriptOperators::ObjectToNativeArray(Js::JavascriptArray::UnsafeFromVar(arrayObject), valueType, length, elementSize, buffer, scriptContext);
  9072. break;
  9073. case TypeIds_NativeFloatArray:
  9074. Js::JavascriptOperators::ObjectToNativeArray(Js::JavascriptNativeFloatArray::UnsafeFromVar(arrayObject), valueType, length, elementSize, buffer, scriptContext);
  9075. break;
  9076. case TypeIds_NativeIntArray:
  9077. Js::JavascriptOperators::ObjectToNativeArray(Js::JavascriptNativeIntArray::UnsafeFromVar(arrayObject), valueType, length, elementSize, buffer, scriptContext);
  9078. break;
  9079. // We can have more specialized template if needed.
  9080. default:
  9081. Js::JavascriptOperators::ObjectToNativeArray(&arrayObject, valueType, length, elementSize, buffer, scriptContext);
  9082. }
  9083. }
  9084. }
  9085. // SpeciesConstructor abstract operation as described in ES6.0 Section 7.3.20
  9086. RecyclableObject* JavascriptOperators::SpeciesConstructor(_In_ RecyclableObject* object, _In_ JavascriptFunction* defaultConstructor, _In_ ScriptContext* scriptContext)
  9087. {
  9088. //1.Assert: Type(O) is Object.
  9089. Assert(JavascriptOperators::IsObject(object));
  9090. //2.Let C be Get(O, "constructor").
  9091. //3.ReturnIfAbrupt(C).
  9092. Var constructor = JavascriptOperators::GetProperty(object, PropertyIds::constructor, scriptContext);
  9093. if (scriptContext->GetConfig()->IsES6SpeciesEnabled())
  9094. {
  9095. //4.If C is undefined, return defaultConstructor.
  9096. if (JavascriptOperators::IsUndefinedObject(constructor))
  9097. {
  9098. return defaultConstructor;
  9099. }
  9100. //5.If Type(C) is not Object, throw a TypeError exception.
  9101. if (!JavascriptOperators::IsObject(constructor))
  9102. {
  9103. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject, _u("[constructor]"));
  9104. }
  9105. //6.Let S be Get(C, @@species).
  9106. //7.ReturnIfAbrupt(S).
  9107. Var species = nullptr;
  9108. if (!JavascriptOperators::GetProperty(RecyclableObject::FromVar(constructor),
  9109. PropertyIds::_symbolSpecies, &species, scriptContext)
  9110. || JavascriptOperators::IsUndefinedOrNull(species))
  9111. {
  9112. //8.If S is either undefined or null, return defaultConstructor.
  9113. return defaultConstructor;
  9114. }
  9115. constructor = species;
  9116. }
  9117. //9.If IsConstructor(S) is true, return S.
  9118. RecyclableObject* constructorObj = JavascriptOperators::TryFromVar<RecyclableObject>(constructor);
  9119. if (constructorObj && JavascriptOperators::IsConstructor(constructorObj))
  9120. {
  9121. return constructorObj;
  9122. }
  9123. //10.Throw a TypeError exception.
  9124. JavascriptError::ThrowTypeError(scriptContext, JSERR_NotAConstructor, _u("constructor[Symbol.species]"));
  9125. }
  9126. BOOL JavascriptOperators::GreaterEqual(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9127. {
  9128. JIT_HELPER_REENTRANT_HEADER(Op_GreaterEqual);
  9129. if (TaggedInt::Is(aLeft))
  9130. {
  9131. if (TaggedInt::Is(aRight))
  9132. {
  9133. // Works whether it is TaggedInt31 or TaggedInt32
  9134. return ::Math::PointerCastToIntegralTruncate<int>(aLeft) >= ::Math::PointerCastToIntegralTruncate<int>(aRight);
  9135. }
  9136. if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9137. {
  9138. return TaggedInt::ToDouble(aLeft) >= JavascriptNumber::GetValue(aRight);
  9139. }
  9140. }
  9141. else if (TaggedInt::Is(aRight))
  9142. {
  9143. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  9144. {
  9145. return JavascriptNumber::GetValue(aLeft) >= TaggedInt::ToDouble(aRight);
  9146. }
  9147. }
  9148. else
  9149. {
  9150. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft) && JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9151. {
  9152. return JavascriptNumber::GetValue(aLeft) >= JavascriptNumber::GetValue(aRight);
  9153. }
  9154. }
  9155. return !RelationalComparisonHelper(aLeft, aRight, scriptContext, true, true);
  9156. JIT_HELPER_END(Op_GreaterEqual);
  9157. }
  9158. BOOL JavascriptOperators::LessEqual(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9159. {
  9160. JIT_HELPER_REENTRANT_HEADER(Op_LessEqual);
  9161. if (TaggedInt::Is(aLeft))
  9162. {
  9163. if (TaggedInt::Is(aRight))
  9164. {
  9165. // Works whether it is TaggedInt31 or TaggedInt32
  9166. return ::Math::PointerCastToIntegralTruncate<int>(aLeft) <= ::Math::PointerCastToIntegralTruncate<int>(aRight);
  9167. }
  9168. if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9169. {
  9170. return TaggedInt::ToDouble(aLeft) <= JavascriptNumber::GetValue(aRight);
  9171. }
  9172. }
  9173. else if (TaggedInt::Is(aRight))
  9174. {
  9175. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  9176. {
  9177. return JavascriptNumber::GetValue(aLeft) <= TaggedInt::ToDouble(aRight);
  9178. }
  9179. }
  9180. else
  9181. {
  9182. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft) && JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9183. {
  9184. return JavascriptNumber::GetValue(aLeft) <= JavascriptNumber::GetValue(aRight);
  9185. }
  9186. }
  9187. return !RelationalComparisonHelper(aRight, aLeft, scriptContext, false, true);
  9188. JIT_HELPER_END(Op_LessEqual);
  9189. }
  9190. BOOL JavascriptOperators::NotEqual(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9191. {
  9192. JIT_HELPER_REENTRANT_HEADER(Op_NotEqual);
  9193. JIT_HELPER_SAME_ATTRIBUTES(Op_NotEqual, Op_Equal);
  9194. //
  9195. // TODO: Change to use Abstract Equality Comparison Algorithm (ES3.0: S11.9.3):
  9196. // - Evaluate left, then right, operands to preserve correct evaluation order.
  9197. // - Call algorithm, potentially reversing arguments.
  9198. //
  9199. return !Equal(aLeft, aRight, scriptContext);
  9200. JIT_HELPER_END(Op_NotEqual);
  9201. }
  9202. // NotStrictEqual() returns whether the two vars have strict equality, as
  9203. // described in (ES3.0: S11.9.5, S11.9.6).
  9204. BOOL JavascriptOperators::NotStrictEqual(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9205. {
  9206. JIT_HELPER_REENTRANT_HEADER(Op_NotStrictEqual);
  9207. JIT_HELPER_SAME_ATTRIBUTES(Op_NotStrictEqual, Op_StrictEqual);
  9208. return !StrictEqual(aLeft, aRight, scriptContext);
  9209. JIT_HELPER_END(Op_NotStrictEqual);
  9210. }
  9211. bool JavascriptOperators::CheckIfObjectAndPrototypeChainHasOnlyWritableDataProperties(_In_ RecyclableObject* object)
  9212. {
  9213. return object->GetLibrary()->GetTypesWithOnlyWritablePropertyProtoChainCache()->Check(object);
  9214. }
  9215. bool JavascriptOperators::CheckIfPrototypeChainHasOnlyWritableDataProperties(_In_ RecyclableObject* prototype)
  9216. {
  9217. return prototype->GetLibrary()->GetTypesWithOnlyWritablePropertyProtoChainCache()->CheckProtoChain(prototype);
  9218. }
  9219. bool JavascriptOperators::CheckIfObjectAndProtoChainHasNoSpecialProperties(_In_ RecyclableObject* object)
  9220. {
  9221. return object->GetLibrary()->GetTypesWithNoSpecialPropertyProtoChainCache()->Check(object);
  9222. }
  9223. // Checks to see if the specified object (which should be a prototype object)
  9224. // contains a proxy anywhere in the prototype chain.
  9225. bool JavascriptOperators::CheckIfPrototypeChainContainsProxyObject(RecyclableObject* prototype)
  9226. {
  9227. if (prototype == nullptr)
  9228. {
  9229. return false;
  9230. }
  9231. Assert(JavascriptOperators::IsObjectOrNull(prototype));
  9232. while (prototype->GetTypeId() != TypeIds_Null)
  9233. {
  9234. if (prototype->GetTypeId() == TypeIds_Proxy)
  9235. {
  9236. return true;
  9237. }
  9238. prototype = prototype->GetPrototype();
  9239. }
  9240. return false;
  9241. }
  9242. BOOL JavascriptOperators::Equal(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9243. {
  9244. JIT_HELPER_REENTRANT_HEADER(Op_Equal);
  9245. JIT_HELPER_SAME_ATTRIBUTES(Op_Equal, Op_Equal_Full);
  9246. if (aLeft == aRight)
  9247. {
  9248. if (TaggedInt::Is(aLeft) || JavascriptObject::Is(aLeft))
  9249. {
  9250. return true;
  9251. }
  9252. else
  9253. {
  9254. return Equal_Full(aLeft, aRight, scriptContext);
  9255. }
  9256. }
  9257. if (JavascriptString::Is(aLeft) && JavascriptString::Is(aRight))
  9258. {
  9259. JavascriptString* left = (JavascriptString*)aLeft;
  9260. JavascriptString* right = (JavascriptString*)aRight;
  9261. if (left->GetLength() == right->GetLength())
  9262. {
  9263. if (left->UnsafeGetBuffer() != NULL && right->UnsafeGetBuffer() != NULL)
  9264. {
  9265. if (left->GetLength() == 1)
  9266. {
  9267. return left->UnsafeGetBuffer()[0] == right->UnsafeGetBuffer()[0];
  9268. }
  9269. return memcmp(left->UnsafeGetBuffer(), right->UnsafeGetBuffer(), left->GetLength() * sizeof(left->UnsafeGetBuffer()[0])) == 0;
  9270. }
  9271. // fall through to Equal_Full
  9272. }
  9273. else
  9274. {
  9275. return false;
  9276. }
  9277. }
  9278. return Equal_Full(aLeft, aRight, scriptContext);
  9279. JIT_HELPER_END(Op_Equal);
  9280. }
  9281. BOOL JavascriptOperators::Greater(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9282. {
  9283. JIT_HELPER_REENTRANT_HEADER(Op_Greater);
  9284. if (TaggedInt::Is(aLeft))
  9285. {
  9286. if (TaggedInt::Is(aRight))
  9287. {
  9288. // Works whether it is TaggedInt31 or TaggedInt32
  9289. return ::Math::PointerCastToIntegralTruncate<int>(aLeft) > ::Math::PointerCastToIntegralTruncate<int>(aRight);
  9290. }
  9291. if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9292. {
  9293. return TaggedInt::ToDouble(aLeft) > JavascriptNumber::GetValue(aRight);
  9294. }
  9295. }
  9296. else if (TaggedInt::Is(aRight))
  9297. {
  9298. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  9299. {
  9300. return JavascriptNumber::GetValue(aLeft) > TaggedInt::ToDouble(aRight);
  9301. }
  9302. }
  9303. else
  9304. {
  9305. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft) && JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9306. {
  9307. return JavascriptNumber::GetValue(aLeft) > JavascriptNumber::GetValue(aRight);
  9308. }
  9309. }
  9310. return Greater_Full(aLeft, aRight, scriptContext);
  9311. JIT_HELPER_END(Op_Greater);
  9312. }
  9313. BOOL JavascriptOperators::Less(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9314. {
  9315. JIT_HELPER_REENTRANT_HEADER(Op_Less);
  9316. if (TaggedInt::Is(aLeft))
  9317. {
  9318. if (TaggedInt::Is(aRight))
  9319. {
  9320. // Works whether it is TaggedInt31 or TaggedInt32
  9321. return ::Math::PointerCastToIntegralTruncate<int>(aLeft) < ::Math::PointerCastToIntegralTruncate<int>(aRight);
  9322. }
  9323. if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9324. {
  9325. return TaggedInt::ToDouble(aLeft) < JavascriptNumber::GetValue(aRight);
  9326. }
  9327. }
  9328. else if (TaggedInt::Is(aRight))
  9329. {
  9330. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  9331. {
  9332. return JavascriptNumber::GetValue(aLeft) < TaggedInt::ToDouble(aRight);
  9333. }
  9334. }
  9335. else
  9336. {
  9337. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft) && JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9338. {
  9339. return JavascriptNumber::GetValue(aLeft) < JavascriptNumber::GetValue(aRight);
  9340. }
  9341. }
  9342. return Less_Full(aLeft, aRight, scriptContext);
  9343. JIT_HELPER_END(Op_Less);
  9344. }
  9345. RecyclableObject* JavascriptOperators::ToObject(Var aRight, ScriptContext* scriptContext)
  9346. {
  9347. JIT_HELPER_NOT_REENTRANT_HEADER(Op_ConvObject, reentrancylock, scriptContext->GetThreadContext());
  9348. RecyclableObject* object = nullptr;
  9349. if (FALSE == JavascriptConversion::ToObject(aRight, scriptContext, &object))
  9350. {
  9351. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject /* TODO-ERROR: get arg name - aValue */);
  9352. }
  9353. return object;
  9354. JIT_HELPER_END(Op_ConvObject);
  9355. }
  9356. Var JavascriptOperators::ToUnscopablesWrapperObject(Var aRight, ScriptContext* scriptContext)
  9357. {
  9358. JIT_HELPER_NOT_REENTRANT_HEADER(Op_NewUnscopablesWrapperObject, reentrancylock, scriptContext->GetThreadContext());
  9359. RecyclableObject* object = RecyclableObject::FromVar(aRight);
  9360. UnscopablesWrapperObject* withWrapper = RecyclerNew(scriptContext->GetRecycler(), UnscopablesWrapperObject, object, scriptContext->GetLibrary()->GetWithType());
  9361. return withWrapper;
  9362. JIT_HELPER_END(Op_NewUnscopablesWrapperObject);
  9363. }
  9364. Var JavascriptOperators::ToNumber(Var aRight, ScriptContext* scriptContext)
  9365. {
  9366. JIT_HELPER_REENTRANT_HEADER(Op_ConvNumber_Full);
  9367. if (TaggedInt::Is(aRight) || (JavascriptNumber::Is_NoTaggedIntCheck(aRight)))
  9368. {
  9369. return aRight;
  9370. }
  9371. return JavascriptNumber::ToVarIntCheck(JavascriptConversion::ToNumber_Full(aRight, scriptContext), scriptContext);
  9372. JIT_HELPER_END(Op_ConvNumber_Full);
  9373. }
  9374. BOOL JavascriptOperators::IsObject(_In_ RecyclableObject* instance)
  9375. {
  9376. return GetTypeId(instance) > TypeIds_LastJavascriptPrimitiveType;
  9377. }
  9378. BOOL JavascriptOperators::IsObject(_In_ Var instance)
  9379. {
  9380. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_IsObject);
  9381. return GetTypeId(instance) > TypeIds_LastJavascriptPrimitiveType;
  9382. JIT_HELPER_END(Op_IsObject);
  9383. }
  9384. BOOL JavascriptOperators::IsObjectType(TypeId typeId)
  9385. {
  9386. return typeId > TypeIds_LastJavascriptPrimitiveType;
  9387. }
  9388. BOOL JavascriptOperators::IsExposedType(TypeId typeId)
  9389. {
  9390. return typeId <= TypeIds_LastTrueJavascriptObjectType && typeId != TypeIds_HostDispatch;
  9391. }
  9392. BOOL JavascriptOperators::IsObjectOrNull(Var instance)
  9393. {
  9394. TypeId typeId = GetTypeId(instance);
  9395. return IsObjectType(typeId) || typeId == TypeIds_Null;
  9396. }
  9397. BOOL JavascriptOperators::IsUndefined(_In_ RecyclableObject* instance)
  9398. {
  9399. return JavascriptOperators::GetTypeId(instance) == TypeIds_Undefined;
  9400. }
  9401. BOOL JavascriptOperators::IsUndefined(Var instance)
  9402. {
  9403. return JavascriptOperators::GetTypeId(instance) == TypeIds_Undefined;
  9404. }
  9405. BOOL JavascriptOperators::IsUndefinedOrNullType(TypeId typeId)
  9406. {
  9407. return typeId <= TypeIds_UndefinedOrNull;
  9408. }
  9409. BOOL JavascriptOperators::IsUndefinedOrNull(Var instance)
  9410. {
  9411. return IsUndefinedOrNullType(JavascriptOperators::GetTypeId(instance));
  9412. }
  9413. BOOL JavascriptOperators::IsUndefinedOrNull(RecyclableObject* instance)
  9414. {
  9415. return JavascriptOperators::IsUndefinedOrNullType(instance->GetTypeId());
  9416. }
  9417. BOOL JavascriptOperators::IsUndefinedOrNull(Var instance, ScriptContext* scriptContext)
  9418. {
  9419. JavascriptLibrary* library = scriptContext->GetLibrary();
  9420. return IsUndefinedObject(instance, library) || IsNull(instance, library);
  9421. }
  9422. BOOL JavascriptOperators::IsUndefinedOrNull(Var instance, JavascriptLibrary* library)
  9423. {
  9424. return IsUndefinedObject(instance, library) || IsNull(instance, library);
  9425. }
  9426. BOOL JavascriptOperators::IsNull(Var instance)
  9427. {
  9428. return JavascriptOperators::GetTypeId(instance) == TypeIds_Null;
  9429. }
  9430. BOOL JavascriptOperators::IsNull(Var instance, ScriptContext* scriptContext)
  9431. {
  9432. return JavascriptOperators::IsNull(instance, scriptContext->GetLibrary());
  9433. }
  9434. BOOL JavascriptOperators::IsNull(Var instance, JavascriptLibrary* library)
  9435. {
  9436. Assert(!RecyclableObject::Is(instance) ? TRUE : ((RecyclableObject*)instance)->GetScriptContext()->GetLibrary() == library );
  9437. return library->GetNull() == instance;
  9438. }
  9439. BOOL JavascriptOperators::IsNull(RecyclableObject* instance)
  9440. {
  9441. return instance->GetType()->GetTypeId() == TypeIds_Null;
  9442. }
  9443. BOOL JavascriptOperators::IsSpecialObjectType(TypeId typeId)
  9444. {
  9445. return typeId > TypeIds_LastTrueJavascriptObjectType;
  9446. }
  9447. BOOL JavascriptOperators::IsUndefinedObject(Var instance)
  9448. {
  9449. return JavascriptOperators::GetTypeId(instance) == TypeIds_Undefined;
  9450. }
  9451. BOOL JavascriptOperators::IsUndefinedObject(RecyclableObject* instance)
  9452. {
  9453. return instance->GetType()->GetTypeId() == TypeIds_Undefined;
  9454. }
  9455. BOOL JavascriptOperators::IsUndefinedObject(Var instance, RecyclableObject *libraryUndefined)
  9456. {
  9457. Assert(JavascriptOperators::IsUndefinedObject(libraryUndefined));
  9458. AssertMsg((instance == libraryUndefined)
  9459. == JavascriptOperators::IsUndefinedObject(instance), "Wrong ScriptContext?");
  9460. return instance == libraryUndefined;
  9461. }
  9462. BOOL JavascriptOperators::IsUndefinedObject(Var instance, ScriptContext *scriptContext)
  9463. {
  9464. return JavascriptOperators::IsUndefinedObject(instance, scriptContext->GetLibrary());
  9465. }
  9466. BOOL JavascriptOperators::IsUndefinedObject(Var instance, JavascriptLibrary* library)
  9467. {
  9468. Assert(!RecyclableObject::Is(instance) ? TRUE : ((RecyclableObject*)instance)->GetScriptContext()->GetLibrary() == library );
  9469. return JavascriptOperators::IsUndefinedObject(instance, library->GetUndefined());
  9470. }
  9471. BOOL JavascriptOperators::IsAnyNumberValue(Var instance)
  9472. {
  9473. TypeId typeId = GetTypeId(instance);
  9474. return TypeIds_FirstNumberType <= typeId && typeId <= TypeIds_LastNumberType;
  9475. }
  9476. // GetIterator as described in ES6.0 (draft 22) Section 7.4.1
  9477. RecyclableObject* JavascriptOperators::GetIterator(Var iterable, ScriptContext* scriptContext, bool optional)
  9478. {
  9479. RecyclableObject* iterableObj = JavascriptOperators::ToObject(iterable, scriptContext);
  9480. return JavascriptOperators::GetIterator(iterableObj, scriptContext, optional);
  9481. }
  9482. RecyclableObject* JavascriptOperators::GetIteratorFunction(Var iterable, ScriptContext* scriptContext, bool optional)
  9483. {
  9484. RecyclableObject* iterableObj = JavascriptOperators::ToObject(iterable, scriptContext);
  9485. return JavascriptOperators::GetIteratorFunction(iterableObj, scriptContext, optional);
  9486. }
  9487. RecyclableObject* JavascriptOperators::GetIteratorFunction(RecyclableObject* instance, ScriptContext * scriptContext, bool optional)
  9488. {
  9489. Var func = JavascriptOperators::GetPropertyNoCache(instance, PropertyIds::_symbolIterator, scriptContext);
  9490. if (optional && JavascriptOperators::IsUndefinedOrNull(func))
  9491. {
  9492. return nullptr;
  9493. }
  9494. if (!JavascriptConversion::IsCallable(func))
  9495. {
  9496. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction);
  9497. }
  9498. RecyclableObject* function = RecyclableObject::FromVar(func);
  9499. return function;
  9500. }
  9501. RecyclableObject* JavascriptOperators::GetIterator(RecyclableObject* instance, ScriptContext * scriptContext, bool optional)
  9502. {
  9503. RecyclableObject* function = GetIteratorFunction(instance, scriptContext, optional);
  9504. if (function == nullptr)
  9505. {
  9506. Assert(optional);
  9507. return nullptr;
  9508. }
  9509. Var iterator = scriptContext->GetThreadContext()->ExecuteImplicitCall(function, Js::ImplicitCall_Accessor, [=]()->Js::Var
  9510. {
  9511. return CALL_FUNCTION(scriptContext->GetThreadContext(), function, CallInfo(Js::CallFlags_Value, 1), instance);
  9512. });
  9513. if (!JavascriptOperators::IsObject(iterator))
  9514. {
  9515. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  9516. }
  9517. return RecyclableObject::FromVar(iterator);
  9518. }
  9519. void JavascriptOperators::IteratorClose(RecyclableObject* iterator, ScriptContext* scriptContext)
  9520. {
  9521. try
  9522. {
  9523. Var func = JavascriptOperators::GetProperty(iterator, PropertyIds::return_, scriptContext);
  9524. if (JavascriptConversion::IsCallable(func))
  9525. {
  9526. RecyclableObject* callable = RecyclableObject::FromVar(func);
  9527. scriptContext->GetThreadContext()->ExecuteImplicitCall(callable, ImplicitCall_Accessor, [=]()->Var
  9528. {
  9529. Js::Var args[] = { iterator };
  9530. Js::CallInfo callInfo(Js::CallFlags_Value, _countof(args));
  9531. return JavascriptFunction::CallFunction<true>(callable, callable->GetEntryPoint(), Js::Arguments(callInfo, args));
  9532. });
  9533. }
  9534. }
  9535. catch (const JavascriptException& err)
  9536. {
  9537. err.GetAndClear(); // discard exception object
  9538. // We have arrived in this function due to AbruptCompletion (which is an exception), so we don't need to
  9539. // propagate the exception of calling return function
  9540. }
  9541. }
  9542. // IteratorNext as described in ES6.0 (draft 22) Section 7.4.2
  9543. RecyclableObject* JavascriptOperators::IteratorNext(RecyclableObject* iterator, ScriptContext* scriptContext, Var value)
  9544. {
  9545. Var func = JavascriptOperators::GetPropertyNoCache(iterator, PropertyIds::next, scriptContext);
  9546. ThreadContext *threadContext = scriptContext->GetThreadContext();
  9547. if (!JavascriptConversion::IsCallable(func))
  9548. {
  9549. if (!threadContext->RecordImplicitException())
  9550. {
  9551. return scriptContext->GetLibrary()->GetUndefined();
  9552. }
  9553. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction);
  9554. }
  9555. RecyclableObject* callable = RecyclableObject::FromVar(func);
  9556. Var result = threadContext->ExecuteImplicitCall(callable, ImplicitCall_Accessor, [=]() -> Var
  9557. {
  9558. Js::Var args[] = { iterator, value };
  9559. Js::CallInfo callInfo(Js::CallFlags_Value, _countof(args) + (value == nullptr ? -1 : 0));
  9560. return JavascriptFunction::CallFunction<true>(callable, callable->GetEntryPoint(), Arguments(callInfo, args));
  9561. });
  9562. if (!JavascriptOperators::IsObject(result))
  9563. {
  9564. if (!threadContext->RecordImplicitException())
  9565. {
  9566. return scriptContext->GetLibrary()->GetUndefined();
  9567. }
  9568. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  9569. }
  9570. return RecyclableObject::FromVar(result);
  9571. }
  9572. // IteratorComplete as described in ES6.0 (draft 22) Section 7.4.3
  9573. bool JavascriptOperators::IteratorComplete(RecyclableObject* iterResult, ScriptContext* scriptContext)
  9574. {
  9575. Var done = JavascriptOperators::GetPropertyNoCache(iterResult, Js::PropertyIds::done, scriptContext);
  9576. return JavascriptConversion::ToBool(done, scriptContext);
  9577. }
  9578. // IteratorValue as described in ES6.0 (draft 22) Section 7.4.4
  9579. Var JavascriptOperators::IteratorValue(RecyclableObject* iterResult, ScriptContext* scriptContext)
  9580. {
  9581. return JavascriptOperators::GetPropertyNoCache(iterResult, Js::PropertyIds::value, scriptContext);
  9582. }
  9583. // IteratorStep as described in ES6.0 (draft 22) Section 7.4.5
  9584. bool JavascriptOperators::IteratorStep(RecyclableObject* iterator, ScriptContext* scriptContext, RecyclableObject** result)
  9585. {
  9586. Assert(result);
  9587. *result = JavascriptOperators::IteratorNext(iterator, scriptContext);
  9588. return !JavascriptOperators::IteratorComplete(*result, scriptContext);
  9589. }
  9590. bool JavascriptOperators::IteratorStepAndValue(RecyclableObject* iterator, ScriptContext* scriptContext, Var* resultValue)
  9591. {
  9592. // CONSIDER: Fast-pathing for iterators that are built-ins?
  9593. RecyclableObject* result = JavascriptOperators::IteratorNext(iterator, scriptContext);
  9594. if (!JavascriptOperators::IteratorComplete(result, scriptContext))
  9595. {
  9596. *resultValue = JavascriptOperators::IteratorValue(result, scriptContext);
  9597. return true;
  9598. }
  9599. return false;
  9600. }
  9601. RecyclableObject* JavascriptOperators::CreateFromConstructor(RecyclableObject* constructor, ScriptContext* scriptContext)
  9602. {
  9603. // Create a regular object and set the internal proto from the constructor
  9604. return JavascriptOperators::OrdinaryCreateFromConstructor(constructor, scriptContext->GetLibrary()->CreateObject(), nullptr, scriptContext);
  9605. }
  9606. RecyclableObject* JavascriptOperators::OrdinaryCreateFromConstructor(RecyclableObject* constructor, RecyclableObject* obj, DynamicObject* intrinsicProto, ScriptContext* scriptContext)
  9607. {
  9608. // There isn't a good way for us to add internal properties to objects in Chakra.
  9609. // Thus, caller should take care to create obj with the correct internal properties.
  9610. Var proto = JavascriptOperators::GetPropertyNoCache(constructor, Js::PropertyIds::prototype, scriptContext);
  9611. // If constructor.prototype is an object, we should use that as the [[Prototype]] for our obj.
  9612. // Else, we set the [[Prototype]] internal slot of obj to %intrinsicProto% - which should be the default.
  9613. if (JavascriptOperators::IsObjectType(JavascriptOperators::GetTypeId(proto)) &&
  9614. DynamicObject::FromVar(proto) != intrinsicProto)
  9615. {
  9616. JavascriptObject::ChangePrototype(obj, RecyclableObject::FromVar(proto), /*validate*/true, scriptContext);
  9617. }
  9618. return obj;
  9619. }
  9620. Var JavascriptOperators::GetProperty(RecyclableObject* instance, PropertyId propertyId, ScriptContext* requestContext, PropertyValueInfo* info)
  9621. {
  9622. return JavascriptOperators::GetProperty(instance, instance, propertyId, requestContext, info);
  9623. }
  9624. BOOL JavascriptOperators::GetProperty(RecyclableObject* instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  9625. {
  9626. return JavascriptOperators::GetProperty(instance, instance, propertyId, value, requestContext, info);
  9627. }
  9628. Var JavascriptOperators::GetProperty(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, ScriptContext* requestContext, PropertyValueInfo* info)
  9629. {
  9630. Var value;
  9631. if (JavascriptOperators::GetProperty(instance, propertyObject, propertyId, &value, requestContext, info))
  9632. {
  9633. return value;
  9634. }
  9635. return requestContext->GetMissingPropertyResult();
  9636. }
  9637. Var JavascriptOperators::GetPropertyNoCache(RecyclableObject* instance, PropertyId propertyId, ScriptContext* requestContext)
  9638. {
  9639. return JavascriptOperators::GetPropertyNoCache(instance, instance, propertyId, requestContext);
  9640. }
  9641. Var JavascriptOperators::GetPropertyNoCache(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, ScriptContext* requestContext)
  9642. {
  9643. Var value;
  9644. JavascriptOperators::GetProperty_InternalSimple(instance, propertyObject, propertyId, &value, requestContext);
  9645. return value;
  9646. }
  9647. BOOL JavascriptOperators::GetPropertyNoCache(RecyclableObject* instance, PropertyId propertyId, Var* value, ScriptContext* requestContext)
  9648. {
  9649. return JavascriptOperators::GetPropertyNoCache(instance, instance, propertyId, value, requestContext);
  9650. }
  9651. BOOL JavascriptOperators::GetPropertyNoCache(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, Var* value, ScriptContext* requestContext)
  9652. {
  9653. return JavascriptOperators::GetProperty_InternalSimple(instance, propertyObject, propertyId, value, requestContext);
  9654. }
  9655. Var JavascriptOperators::GetRootProperty(RecyclableObject* instance, PropertyId propertyId, ScriptContext* requestContext, PropertyValueInfo* info)
  9656. {
  9657. Var value;
  9658. if (JavascriptOperators::GetRootProperty(instance, propertyId, &value, requestContext, info))
  9659. {
  9660. return value;
  9661. }
  9662. return requestContext->GetMissingPropertyResult();
  9663. }
  9664. BOOL JavascriptOperators::GetPropertyReference(RecyclableObject *instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  9665. {
  9666. return JavascriptOperators::GetPropertyReference(instance, instance, propertyId, value, requestContext, info);
  9667. }
  9668. Var JavascriptOperators::GetItem(RecyclableObject* instance, uint32 index, ScriptContext* requestContext)
  9669. {
  9670. Var value;
  9671. if (GetItem(instance, index, &value, requestContext))
  9672. {
  9673. return value;
  9674. }
  9675. return requestContext->GetMissingItemResult();
  9676. }
  9677. Var JavascriptOperators::GetItem(RecyclableObject* instance, uint64 index, ScriptContext* requestContext)
  9678. {
  9679. Var value;
  9680. if (GetItem(instance, index, &value, requestContext))
  9681. {
  9682. return value;
  9683. }
  9684. return requestContext->GetMissingItemResult();
  9685. }
  9686. BOOL JavascriptOperators::GetItem(RecyclableObject* instance, uint64 index, Var* value, ScriptContext* requestContext)
  9687. {
  9688. if (index < JavascriptArray::InvalidIndex)
  9689. {
  9690. // In case index fits in uint32, we can avoid the (slower) big-index path
  9691. return GetItem(instance, static_cast<uint32>(index), value, requestContext);
  9692. }
  9693. PropertyRecord const * propertyRecord = nullptr;
  9694. JavascriptOperators::GetPropertyIdForInt(index, requestContext, &propertyRecord);
  9695. return JavascriptOperators::GetProperty(instance, propertyRecord->GetPropertyId(), value, requestContext);
  9696. }
  9697. BOOL JavascriptOperators::GetItem(RecyclableObject* instance, uint32 index, Var* value, ScriptContext* requestContext)
  9698. {
  9699. return JavascriptOperators::GetItem(instance, instance, index, value, requestContext);
  9700. }
  9701. BOOL JavascriptOperators::GetItemReference(RecyclableObject* instance, uint32 index, Var* value, ScriptContext* requestContext)
  9702. {
  9703. return GetItemReference(instance, instance, index, value, requestContext);
  9704. }
  9705. BOOL JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(RecyclableObject* instance, PropertyId propertyId, Var* setterValue, DescriptorFlags* flags, PropertyValueInfo* info, ScriptContext* scriptContext)
  9706. {
  9707. if (propertyId == Js::PropertyIds::__proto__)
  9708. {
  9709. return CheckPrototypesForAccessorOrNonWritablePropertyCore<PropertyId, false, false>(instance, propertyId, setterValue, flags, info, scriptContext);
  9710. }
  9711. else
  9712. {
  9713. return CheckPrototypesForAccessorOrNonWritablePropertyCore<PropertyId, true, false>(instance, propertyId, setterValue, flags, info, scriptContext);
  9714. }
  9715. }
  9716. BOOL JavascriptOperators::CheckPrototypesForAccessorOrNonWritableRootProperty(RecyclableObject* instance, PropertyId propertyId, Var* setterValue, DescriptorFlags* flags, PropertyValueInfo* info, ScriptContext* scriptContext)
  9717. {
  9718. if (propertyId == Js::PropertyIds::__proto__)
  9719. {
  9720. return CheckPrototypesForAccessorOrNonWritablePropertyCore<PropertyId, false, true>(instance, propertyId, setterValue, flags, info, scriptContext);
  9721. }
  9722. else
  9723. {
  9724. return CheckPrototypesForAccessorOrNonWritablePropertyCore<PropertyId, true, true>(instance, propertyId, setterValue, flags, info, scriptContext);
  9725. }
  9726. }
  9727. BOOL JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(RecyclableObject* instance, JavascriptString* propertyNameString, Var* setterValue, DescriptorFlags* flags, PropertyValueInfo* info, ScriptContext* scriptContext)
  9728. {
  9729. Js::PropertyRecord const * localPropertyRecord;
  9730. propertyNameString->GetPropertyRecord(&localPropertyRecord);
  9731. PropertyId propertyId = localPropertyRecord->GetPropertyId();
  9732. return CheckPrototypesForAccessorOrNonWritableProperty(instance, propertyId, setterValue, flags, info, scriptContext);
  9733. }
  9734. BOOL JavascriptOperators::SetProperty(Var instance, RecyclableObject* object, PropertyId propertyId, Var newValue, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  9735. {
  9736. PropertyValueInfo info;
  9737. return JavascriptOperators::SetProperty(instance, object, propertyId, newValue, &info, requestContext, propertyOperationFlags);
  9738. }
  9739. BOOL JavascriptOperators::TryConvertToUInt32(const char16* str, int length, uint32* intVal)
  9740. {
  9741. return NumberUtilities::TryConvertToUInt32(str, length, intVal);
  9742. }
  9743. template <typename TPropertyKey>
  9744. DescriptorFlags JavascriptOperators::GetRootSetter(RecyclableObject* instance, TPropertyKey propertyKey, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  9745. {
  9746. // This is provided only so that CheckPrototypesForAccessorOrNonWritablePropertyCore will compile.
  9747. // It will never be called.
  9748. Throw::FatalInternalError();
  9749. }
  9750. template <>
  9751. inline DescriptorFlags JavascriptOperators::GetRootSetter(RecyclableObject* instance, PropertyId propertyId, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  9752. {
  9753. AssertMsg(JavascriptOperators::GetTypeId(instance) == TypeIds_GlobalObject
  9754. || JavascriptOperators::GetTypeId(instance) == TypeIds_ModuleRoot,
  9755. "Root must be a global object!");
  9756. RootObjectBase* rootObject = static_cast<RootObjectBase*>(instance);
  9757. return rootObject->GetRootSetter(propertyId, setterValue, info, requestContext);
  9758. }
  9759. // Helper to fetch @@species from a constructor object
  9760. Var JavascriptOperators::GetSpecies(RecyclableObject* constructor, ScriptContext* scriptContext)
  9761. {
  9762. if (scriptContext->GetConfig()->IsES6SpeciesEnabled())
  9763. {
  9764. Var species = nullptr;
  9765. // Let S be Get(C, @@species)
  9766. if (JavascriptOperators::GetProperty(constructor, PropertyIds::_symbolSpecies, &species, scriptContext)
  9767. && !JavascriptOperators::IsUndefinedOrNull(species))
  9768. {
  9769. // If S is neither undefined nor null, let C be S
  9770. return species;
  9771. }
  9772. }
  9773. return constructor;
  9774. }