JavascriptOperators.cpp 467 KB

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