JavascriptOperators.cpp 485 KB

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