JavascriptOperators.cpp 500 KB

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