JavascriptOperators.cpp 473 KB

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